1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright SUSE Linux Products GmbH 2009
6 * Authors: Alexander Graf <agraf@suse.de>
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/kvm.h>
12 #include <linux/kvm_host.h>
13 #include <linux/highmem.h>
15 #include <asm/kvm_ppc.h>
16 #include <asm/kvm_book3s.h>
17 #include <asm/book3s/64/mmu-hash.h>
19 /* #define DEBUG_MMU */
22 #define dprintk(X...) printk(KERN_INFO X)
24 #define dprintk(X...) do { } while(0)
27 static struct kvmppc_slb
*kvmppc_mmu_book3s_64_find_slbe(
28 struct kvm_vcpu
*vcpu
,
32 u64 esid
= GET_ESID(eaddr
);
33 u64 esid_1t
= GET_ESID_1T(eaddr
);
35 for (i
= 0; i
< vcpu
->arch
.slb_nr
; i
++) {
38 if (!vcpu
->arch
.slb
[i
].valid
)
41 if (vcpu
->arch
.slb
[i
].tb
)
44 if (vcpu
->arch
.slb
[i
].esid
== cmp_esid
)
45 return &vcpu
->arch
.slb
[i
];
48 dprintk("KVM: No SLB entry found for 0x%lx [%llx | %llx]\n",
49 eaddr
, esid
, esid_1t
);
50 for (i
= 0; i
< vcpu
->arch
.slb_nr
; i
++) {
51 if (vcpu
->arch
.slb
[i
].vsid
)
52 dprintk(" %d: %c%c%c %llx %llx\n", i
,
53 vcpu
->arch
.slb
[i
].valid
? 'v' : ' ',
54 vcpu
->arch
.slb
[i
].large
? 'l' : ' ',
55 vcpu
->arch
.slb
[i
].tb
? 't' : ' ',
56 vcpu
->arch
.slb
[i
].esid
,
57 vcpu
->arch
.slb
[i
].vsid
);
63 static int kvmppc_slb_sid_shift(struct kvmppc_slb
*slbe
)
65 return slbe
->tb
? SID_SHIFT_1T
: SID_SHIFT
;
68 static u64
kvmppc_slb_offset_mask(struct kvmppc_slb
*slbe
)
70 return (1ul << kvmppc_slb_sid_shift(slbe
)) - 1;
73 static u64
kvmppc_slb_calc_vpn(struct kvmppc_slb
*slb
, gva_t eaddr
)
75 eaddr
&= kvmppc_slb_offset_mask(slb
);
77 return (eaddr
>> VPN_SHIFT
) |
78 ((slb
->vsid
) << (kvmppc_slb_sid_shift(slb
) - VPN_SHIFT
));
81 static u64
kvmppc_mmu_book3s_64_ea_to_vp(struct kvm_vcpu
*vcpu
, gva_t eaddr
,
84 struct kvmppc_slb
*slb
;
86 slb
= kvmppc_mmu_book3s_64_find_slbe(vcpu
, eaddr
);
90 return kvmppc_slb_calc_vpn(slb
, eaddr
);
93 static int mmu_pagesize(int mmu_pg
)
104 static int kvmppc_mmu_book3s_64_get_pagesize(struct kvmppc_slb
*slbe
)
106 return mmu_pagesize(slbe
->base_page_size
);
109 static u32
kvmppc_mmu_book3s_64_get_page(struct kvmppc_slb
*slbe
, gva_t eaddr
)
111 int p
= kvmppc_mmu_book3s_64_get_pagesize(slbe
);
113 return ((eaddr
& kvmppc_slb_offset_mask(slbe
)) >> p
);
116 static hva_t
kvmppc_mmu_book3s_64_get_pteg(struct kvm_vcpu
*vcpu
,
117 struct kvmppc_slb
*slbe
, gva_t eaddr
,
120 struct kvmppc_vcpu_book3s
*vcpu_book3s
= to_book3s(vcpu
);
121 u64 hash
, pteg
, htabsize
;
126 htabsize
= ((1 << ((vcpu_book3s
->sdr1
& 0x1f) + 11)) - 1);
128 vpn
= kvmppc_slb_calc_vpn(slbe
, eaddr
);
129 ssize
= slbe
->tb
? MMU_SEGSIZE_1T
: MMU_SEGSIZE_256M
;
130 hash
= hpt_hash(vpn
, kvmppc_mmu_book3s_64_get_pagesize(slbe
), ssize
);
133 hash
&= ((1ULL << 39ULL) - 1ULL);
137 pteg
= vcpu_book3s
->sdr1
& 0xfffffffffffc0000ULL
;
140 dprintk("MMU: page=0x%x sdr1=0x%llx pteg=0x%llx vsid=0x%llx\n",
141 page
, vcpu_book3s
->sdr1
, pteg
, slbe
->vsid
);
143 /* When running a PAPR guest, SDR1 contains a HVA address instead
145 if (vcpu
->arch
.papr_enabled
)
148 r
= gfn_to_hva(vcpu
->kvm
, pteg
>> PAGE_SHIFT
);
150 if (kvm_is_error_hva(r
))
152 return r
| (pteg
& ~PAGE_MASK
);
155 static u64
kvmppc_mmu_book3s_64_get_avpn(struct kvmppc_slb
*slbe
, gva_t eaddr
)
157 int p
= kvmppc_mmu_book3s_64_get_pagesize(slbe
);
160 avpn
= kvmppc_mmu_book3s_64_get_page(slbe
, eaddr
);
161 avpn
|= slbe
->vsid
<< (kvmppc_slb_sid_shift(slbe
) - p
);
164 avpn
>>= ((80 - p
) - 56) - 8; /* 16 - p */
172 * Return page size encoded in the second word of a HPTE, or
173 * -1 for an invalid encoding for the base page size indicated by
174 * the SLB entry. This doesn't handle mixed pagesize segments yet.
176 static int decode_pagesize(struct kvmppc_slb
*slbe
, u64 r
)
178 switch (slbe
->base_page_size
) {
180 if ((r
& 0xf000) == 0x1000)
184 if ((r
& 0xff000) == 0)
191 static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu
*vcpu
, gva_t eaddr
,
192 struct kvmppc_pte
*gpte
, bool data
,
195 struct kvmppc_slb
*slbe
;
207 ulong mp_ea
= vcpu
->arch
.magic_page_ea
;
209 /* Magic page override */
210 if (unlikely(mp_ea
) &&
211 unlikely((eaddr
& ~0xfffULL
) == (mp_ea
& ~0xfffULL
)) &&
212 !(kvmppc_get_msr(vcpu
) & MSR_PR
)) {
214 gpte
->vpage
= kvmppc_mmu_book3s_64_ea_to_vp(vcpu
, eaddr
, data
);
215 gpte
->raddr
= vcpu
->arch
.magic_page_pa
| (gpte
->raddr
& 0xfff);
216 gpte
->raddr
&= KVM_PAM
;
217 gpte
->may_execute
= true;
218 gpte
->may_read
= true;
219 gpte
->may_write
= true;
220 gpte
->page_size
= MMU_PAGE_4K
;
221 gpte
->wimg
= HPTE_R_M
;
226 slbe
= kvmppc_mmu_book3s_64_find_slbe(vcpu
, eaddr
);
230 avpn
= kvmppc_mmu_book3s_64_get_avpn(slbe
, eaddr
);
231 v_val
= avpn
& HPTE_V_AVPN
;
234 v_val
|= SLB_VSID_B_1T
;
236 v_val
|= HPTE_V_LARGE
;
237 v_val
|= HPTE_V_VALID
;
239 v_mask
= SLB_VSID_B
| HPTE_V_AVPN
| HPTE_V_LARGE
| HPTE_V_VALID
|
242 pgsize
= slbe
->large
? MMU_PAGE_16M
: MMU_PAGE_4K
;
244 mutex_lock(&vcpu
->kvm
->arch
.hpt_mutex
);
247 ptegp
= kvmppc_mmu_book3s_64_get_pteg(vcpu
, slbe
, eaddr
, second
);
248 if (kvm_is_error_hva(ptegp
))
251 if(copy_from_user(pteg
, (void __user
*)ptegp
, sizeof(pteg
))) {
252 printk_ratelimited(KERN_ERR
253 "KVM: Can't copy data from 0x%lx!\n", ptegp
);
257 if ((kvmppc_get_msr(vcpu
) & MSR_PR
) && slbe
->Kp
)
259 else if (!(kvmppc_get_msr(vcpu
) & MSR_PR
) && slbe
->Ks
)
262 for (i
=0; i
<16; i
+=2) {
263 u64 pte0
= be64_to_cpu(pteg
[i
]);
264 u64 pte1
= be64_to_cpu(pteg
[i
+ 1]);
266 /* Check all relevant fields of 1st dword */
267 if ((pte0
& v_mask
) == v_val
) {
268 /* If large page bit is set, check pgsize encoding */
270 (vcpu
->arch
.hflags
& BOOK3S_HFLAG_MULTI_PGSIZE
)) {
271 pgsize
= decode_pagesize(slbe
, pte1
);
283 v_val
|= HPTE_V_SECONDARY
;
288 v
= be64_to_cpu(pteg
[i
]);
289 r
= be64_to_cpu(pteg
[i
+1]);
290 pp
= (r
& HPTE_R_PP
) | key
;
295 gpte
->vpage
= kvmppc_mmu_book3s_64_ea_to_vp(vcpu
, eaddr
, data
);
297 eaddr_mask
= (1ull << mmu_pagesize(pgsize
)) - 1;
298 gpte
->raddr
= (r
& HPTE_R_RPN
& ~eaddr_mask
) | (eaddr
& eaddr_mask
);
299 gpte
->page_size
= pgsize
;
300 gpte
->may_execute
= ((r
& HPTE_R_N
) ? false : true);
301 if (unlikely(vcpu
->arch
.disable_kernel_nx
) &&
302 !(kvmppc_get_msr(vcpu
) & MSR_PR
))
303 gpte
->may_execute
= true;
304 gpte
->may_read
= false;
305 gpte
->may_write
= false;
306 gpte
->wimg
= r
& HPTE_R_WIMG
;
313 gpte
->may_write
= true;
319 gpte
->may_read
= true;
323 dprintk("KVM MMU: Translated 0x%lx [0x%llx] -> 0x%llx "
325 eaddr
, avpn
, gpte
->vpage
, gpte
->raddr
);
327 /* Update PTE R and C bits, so the guest's swapper knows we used the
329 if (gpte
->may_read
&& !(r
& HPTE_R_R
)) {
331 * Set the accessed flag.
332 * We have to write this back with a single byte write
333 * because another vcpu may be accessing this on
334 * non-PAPR platforms such as mac99, and this is
335 * what real hardware does.
337 char __user
*addr
= (char __user
*) (ptegp
+ (i
+ 1) * sizeof(u64
));
339 put_user(r
>> 8, addr
+ 6);
341 if (iswrite
&& gpte
->may_write
&& !(r
& HPTE_R_C
)) {
342 /* Set the dirty flag */
343 /* Use a single byte write */
344 char __user
*addr
= (char __user
*) (ptegp
+ (i
+ 1) * sizeof(u64
));
346 put_user(r
, addr
+ 7);
349 mutex_unlock(&vcpu
->kvm
->arch
.hpt_mutex
);
351 if (!gpte
->may_read
|| (iswrite
&& !gpte
->may_write
))
356 mutex_unlock(&vcpu
->kvm
->arch
.hpt_mutex
);
360 dprintk("KVM MMU: Trigger segment fault\n");
364 static void kvmppc_mmu_book3s_64_slbmte(struct kvm_vcpu
*vcpu
, u64 rs
, u64 rb
)
368 struct kvmppc_slb
*slbe
;
370 dprintk("KVM MMU: slbmte(0x%llx, 0x%llx)\n", rs
, rb
);
373 esid_1t
= GET_ESID_1T(rb
);
376 if (slb_nr
> vcpu
->arch
.slb_nr
)
379 slbe
= &vcpu
->arch
.slb
[slb_nr
];
381 slbe
->large
= (rs
& SLB_VSID_L
) ? 1 : 0;
382 slbe
->tb
= (rs
& SLB_VSID_B_1T
) ? 1 : 0;
383 slbe
->esid
= slbe
->tb
? esid_1t
: esid
;
384 slbe
->vsid
= (rs
& ~SLB_VSID_B
) >> (kvmppc_slb_sid_shift(slbe
) - 16);
385 slbe
->valid
= (rb
& SLB_ESID_V
) ? 1 : 0;
386 slbe
->Ks
= (rs
& SLB_VSID_KS
) ? 1 : 0;
387 slbe
->Kp
= (rs
& SLB_VSID_KP
) ? 1 : 0;
388 slbe
->nx
= (rs
& SLB_VSID_N
) ? 1 : 0;
389 slbe
->class = (rs
& SLB_VSID_C
) ? 1 : 0;
391 slbe
->base_page_size
= MMU_PAGE_4K
;
393 if (vcpu
->arch
.hflags
& BOOK3S_HFLAG_MULTI_PGSIZE
) {
394 switch (rs
& SLB_VSID_LP
) {
396 slbe
->base_page_size
= MMU_PAGE_16M
;
399 slbe
->base_page_size
= MMU_PAGE_64K
;
403 slbe
->base_page_size
= MMU_PAGE_16M
;
406 slbe
->orige
= rb
& (ESID_MASK
| SLB_ESID_V
);
409 /* Map the new segment */
410 kvmppc_mmu_map_segment(vcpu
, esid
<< SID_SHIFT
);
413 static int kvmppc_mmu_book3s_64_slbfee(struct kvm_vcpu
*vcpu
, gva_t eaddr
,
416 struct kvmppc_slb
*slbe
= kvmppc_mmu_book3s_64_find_slbe(vcpu
, eaddr
);
419 *ret_slb
= slbe
->origv
;
426 static u64
kvmppc_mmu_book3s_64_slbmfee(struct kvm_vcpu
*vcpu
, u64 slb_nr
)
428 struct kvmppc_slb
*slbe
;
430 if (slb_nr
> vcpu
->arch
.slb_nr
)
433 slbe
= &vcpu
->arch
.slb
[slb_nr
];
438 static u64
kvmppc_mmu_book3s_64_slbmfev(struct kvm_vcpu
*vcpu
, u64 slb_nr
)
440 struct kvmppc_slb
*slbe
;
442 if (slb_nr
> vcpu
->arch
.slb_nr
)
445 slbe
= &vcpu
->arch
.slb
[slb_nr
];
450 static void kvmppc_mmu_book3s_64_slbie(struct kvm_vcpu
*vcpu
, u64 ea
)
452 struct kvmppc_slb
*slbe
;
455 dprintk("KVM MMU: slbie(0x%llx)\n", ea
);
457 slbe
= kvmppc_mmu_book3s_64_find_slbe(vcpu
, ea
);
462 dprintk("KVM MMU: slbie(0x%llx, 0x%llx)\n", ea
, slbe
->esid
);
468 seg_size
= 1ull << kvmppc_slb_sid_shift(slbe
);
469 kvmppc_mmu_flush_segment(vcpu
, ea
& ~(seg_size
- 1), seg_size
);
472 static void kvmppc_mmu_book3s_64_slbia(struct kvm_vcpu
*vcpu
)
476 dprintk("KVM MMU: slbia()\n");
478 for (i
= 1; i
< vcpu
->arch
.slb_nr
; i
++) {
479 vcpu
->arch
.slb
[i
].valid
= false;
480 vcpu
->arch
.slb
[i
].orige
= 0;
481 vcpu
->arch
.slb
[i
].origv
= 0;
484 if (kvmppc_get_msr(vcpu
) & MSR_IR
) {
485 kvmppc_mmu_flush_segments(vcpu
);
486 kvmppc_mmu_map_segment(vcpu
, kvmppc_get_pc(vcpu
));
490 static void kvmppc_mmu_book3s_64_mtsrin(struct kvm_vcpu
*vcpu
, u32 srnum
,
496 * According to Book3 2.01 mtsrin is implemented as:
498 * The SLB entry specified by (RB)32:35 is loaded from register
501 * SLBE Bit Source SLB Field
503 * 0:31 0x0000_0000 ESID-0:31
504 * 32:35 (RB)32:35 ESID-32:35
506 * 37:61 0x00_0000|| 0b0 VSID-0:24
507 * 62:88 (RS)37:63 VSID-25:51
508 * 89:91 (RS)33:35 Ks Kp N
509 * 92 (RS)36 L ((RS)36 must be 0b0)
513 dprintk("KVM MMU: mtsrin(0x%x, 0x%lx)\n", srnum
, value
);
516 rb
|= (srnum
& 0xf) << 28;
517 /* Set the valid bit */
523 rs
|= (value
& 0xfffffff) << 12;
525 rs
|= ((value
>> 28) & 0x7) << 9;
527 kvmppc_mmu_book3s_64_slbmte(vcpu
, rs
, rb
);
530 static void kvmppc_mmu_book3s_64_tlbie(struct kvm_vcpu
*vcpu
, ulong va
,
533 u64 mask
= 0xFFFFFFFFFULL
;
537 dprintk("KVM MMU: tlbie(0x%lx)\n", va
);
540 * The tlbie instruction changed behaviour starting with
541 * POWER6. POWER6 and later don't have the large page flag
542 * in the instruction but in the RB value, along with bits
543 * indicating page and segment sizes.
545 if (vcpu
->arch
.hflags
& BOOK3S_HFLAG_NEW_TLBIE
) {
546 /* POWER6 or later */
547 if (va
& 1) { /* L bit */
548 if ((va
& 0xf000) == 0x1000)
549 mask
= 0xFFFFFFFF0ULL
; /* 64k page */
551 mask
= 0xFFFFFF000ULL
; /* 16M page */
554 /* older processors, e.g. PPC970 */
556 mask
= 0xFFFFFF000ULL
;
558 /* flush this VA on all vcpus */
559 kvm_for_each_vcpu(i
, v
, vcpu
->kvm
)
560 kvmppc_mmu_pte_vflush(v
, va
>> 12, mask
);
563 #ifdef CONFIG_PPC_64K_PAGES
564 static int segment_contains_magic_page(struct kvm_vcpu
*vcpu
, ulong esid
)
566 ulong mp_ea
= vcpu
->arch
.magic_page_ea
;
568 return mp_ea
&& !(kvmppc_get_msr(vcpu
) & MSR_PR
) &&
569 (mp_ea
>> SID_SHIFT
) == esid
;
573 static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu
*vcpu
, ulong esid
,
576 ulong ea
= esid
<< SID_SHIFT
;
577 struct kvmppc_slb
*slb
;
579 ulong mp_ea
= vcpu
->arch
.magic_page_ea
;
580 int pagesize
= MMU_PAGE_64K
;
581 u64 msr
= kvmppc_get_msr(vcpu
);
583 if (msr
& (MSR_DR
|MSR_IR
)) {
584 slb
= kvmppc_mmu_book3s_64_find_slbe(vcpu
, ea
);
587 pagesize
= slb
->base_page_size
;
589 gvsid
<<= SID_SHIFT_1T
- SID_SHIFT
;
590 gvsid
|= esid
& ((1ul << (SID_SHIFT_1T
- SID_SHIFT
)) - 1);
596 switch (msr
& (MSR_DR
|MSR_IR
)) {
598 gvsid
= VSID_REAL
| esid
;
601 gvsid
|= VSID_REAL_IR
;
604 gvsid
|= VSID_REAL_DR
;
616 #ifdef CONFIG_PPC_64K_PAGES
618 * Mark this as a 64k segment if the host is using
619 * 64k pages, the host MMU supports 64k pages and
620 * the guest segment page size is >= 64k,
621 * but not if this segment contains the magic page.
623 if (pagesize
>= MMU_PAGE_64K
&&
624 mmu_psize_defs
[MMU_PAGE_64K
].shift
&&
625 !segment_contains_magic_page(vcpu
, esid
))
629 if (kvmppc_get_msr(vcpu
) & MSR_PR
)
636 /* Catch magic page case */
637 if (unlikely(mp_ea
) &&
638 unlikely(esid
== (mp_ea
>> SID_SHIFT
)) &&
639 !(kvmppc_get_msr(vcpu
) & MSR_PR
)) {
640 *vsid
= VSID_REAL
| esid
;
647 static bool kvmppc_mmu_book3s_64_is_dcbz32(struct kvm_vcpu
*vcpu
)
649 return (to_book3s(vcpu
)->hid
[5] & 0x80);
652 void kvmppc_mmu_book3s_64_init(struct kvm_vcpu
*vcpu
)
654 struct kvmppc_mmu
*mmu
= &vcpu
->arch
.mmu
;
657 mmu
->mtsrin
= kvmppc_mmu_book3s_64_mtsrin
;
658 mmu
->slbmte
= kvmppc_mmu_book3s_64_slbmte
;
659 mmu
->slbmfee
= kvmppc_mmu_book3s_64_slbmfee
;
660 mmu
->slbmfev
= kvmppc_mmu_book3s_64_slbmfev
;
661 mmu
->slbfee
= kvmppc_mmu_book3s_64_slbfee
;
662 mmu
->slbie
= kvmppc_mmu_book3s_64_slbie
;
663 mmu
->slbia
= kvmppc_mmu_book3s_64_slbia
;
664 mmu
->xlate
= kvmppc_mmu_book3s_64_xlate
;
665 mmu
->tlbie
= kvmppc_mmu_book3s_64_tlbie
;
666 mmu
->esid_to_vsid
= kvmppc_mmu_book3s_64_esid_to_vsid
;
667 mmu
->ea_to_vp
= kvmppc_mmu_book3s_64_ea_to_vp
;
668 mmu
->is_dcbz32
= kvmppc_mmu_book3s_64_is_dcbz32
;
670 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_SLB
;