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 r = be64_to_cpu(pteg[i+1]);
289 pp = (r & HPTE_R_PP) | key;
294 gpte->vpage = kvmppc_mmu_book3s_64_ea_to_vp(vcpu, eaddr, data);
296 eaddr_mask = (1ull << mmu_pagesize(pgsize)) - 1;
297 gpte->raddr = (r & HPTE_R_RPN & ~eaddr_mask) | (eaddr & eaddr_mask);
298 gpte->page_size = pgsize;
299 gpte->may_execute = ((r & HPTE_R_N) ? false : true);
300 if (unlikely(vcpu->arch.disable_kernel_nx) &&
301 !(kvmppc_get_msr(vcpu) & MSR_PR))
302 gpte->may_execute = true;
303 gpte->may_read = false;
304 gpte->may_write = false;
305 gpte->wimg = r & HPTE_R_WIMG;
312 gpte->may_write = true;
318 gpte->may_read = true;
322 dprintk("KVM MMU: Translated 0x%lx [0x%llx] -> 0x%llx "
324 eaddr, avpn, gpte->vpage, gpte->raddr);
326 /* Update PTE R and C bits, so the guest's swapper knows we used the
328 if (gpte->may_read && !(r & HPTE_R_R)) {
330 * Set the accessed flag.
331 * We have to write this back with a single byte write
332 * because another vcpu may be accessing this on
333 * non-PAPR platforms such as mac99, and this is
334 * what real hardware does.
336 char __user *addr = (char __user *) (ptegp + (i + 1) * sizeof(u64));
338 put_user(r >> 8, addr + 6);
340 if (iswrite && gpte->may_write && !(r & HPTE_R_C)) {
341 /* Set the dirty flag */
342 /* Use a single byte write */
343 char __user *addr = (char __user *) (ptegp + (i + 1) * sizeof(u64));
345 put_user(r, addr + 7);
348 mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
350 if (!gpte->may_read || (iswrite && !gpte->may_write))
355 mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
359 dprintk("KVM MMU: Trigger segment fault\n");
363 static void kvmppc_mmu_book3s_64_slbmte(struct kvm_vcpu *vcpu, u64 rs, u64 rb)
367 struct kvmppc_slb *slbe;
369 dprintk("KVM MMU: slbmte(0x%llx, 0x%llx)\n", rs, rb);
372 esid_1t = GET_ESID_1T(rb);
375 if (slb_nr > vcpu->arch.slb_nr)
378 slbe = &vcpu->arch.slb[slb_nr];
380 slbe->large = (rs & SLB_VSID_L) ? 1 : 0;
381 slbe->tb = (rs & SLB_VSID_B_1T) ? 1 : 0;
382 slbe->esid = slbe->tb ? esid_1t : esid;
383 slbe->vsid = (rs & ~SLB_VSID_B) >> (kvmppc_slb_sid_shift(slbe) - 16);
384 slbe->valid = (rb & SLB_ESID_V) ? 1 : 0;
385 slbe->Ks = (rs & SLB_VSID_KS) ? 1 : 0;
386 slbe->Kp = (rs & SLB_VSID_KP) ? 1 : 0;
387 slbe->nx = (rs & SLB_VSID_N) ? 1 : 0;
388 slbe->class = (rs & SLB_VSID_C) ? 1 : 0;
390 slbe->base_page_size = MMU_PAGE_4K;
392 if (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE) {
393 switch (rs & SLB_VSID_LP) {
395 slbe->base_page_size = MMU_PAGE_16M;
398 slbe->base_page_size = MMU_PAGE_64K;
402 slbe->base_page_size = MMU_PAGE_16M;
405 slbe->orige = rb & (ESID_MASK | SLB_ESID_V);
408 /* Map the new segment */
409 kvmppc_mmu_map_segment(vcpu, esid << SID_SHIFT);
412 static int kvmppc_mmu_book3s_64_slbfee(struct kvm_vcpu *vcpu, gva_t eaddr,
415 struct kvmppc_slb *slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu, eaddr);
418 *ret_slb = slbe->origv;
425 static u64 kvmppc_mmu_book3s_64_slbmfee(struct kvm_vcpu *vcpu, u64 slb_nr)
427 struct kvmppc_slb *slbe;
429 if (slb_nr > vcpu->arch.slb_nr)
432 slbe = &vcpu->arch.slb[slb_nr];
437 static u64 kvmppc_mmu_book3s_64_slbmfev(struct kvm_vcpu *vcpu, u64 slb_nr)
439 struct kvmppc_slb *slbe;
441 if (slb_nr > vcpu->arch.slb_nr)
444 slbe = &vcpu->arch.slb[slb_nr];
449 static void kvmppc_mmu_book3s_64_slbie(struct kvm_vcpu *vcpu, u64 ea)
451 struct kvmppc_slb *slbe;
454 dprintk("KVM MMU: slbie(0x%llx)\n", ea);
456 slbe = kvmppc_mmu_book3s_64_find_slbe(vcpu, ea);
461 dprintk("KVM MMU: slbie(0x%llx, 0x%llx)\n", ea, slbe->esid);
467 seg_size = 1ull << kvmppc_slb_sid_shift(slbe);
468 kvmppc_mmu_flush_segment(vcpu, ea & ~(seg_size - 1), seg_size);
471 static void kvmppc_mmu_book3s_64_slbia(struct kvm_vcpu *vcpu)
475 dprintk("KVM MMU: slbia()\n");
477 for (i = 1; i < vcpu->arch.slb_nr; i++) {
478 vcpu->arch.slb[i].valid = false;
479 vcpu->arch.slb[i].orige = 0;
480 vcpu->arch.slb[i].origv = 0;
483 if (kvmppc_get_msr(vcpu) & MSR_IR) {
484 kvmppc_mmu_flush_segments(vcpu);
485 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
489 static void kvmppc_mmu_book3s_64_mtsrin(struct kvm_vcpu *vcpu, u32 srnum,
495 * According to Book3 2.01 mtsrin is implemented as:
497 * The SLB entry specified by (RB)32:35 is loaded from register
500 * SLBE Bit Source SLB Field
502 * 0:31 0x0000_0000 ESID-0:31
503 * 32:35 (RB)32:35 ESID-32:35
505 * 37:61 0x00_0000|| 0b0 VSID-0:24
506 * 62:88 (RS)37:63 VSID-25:51
507 * 89:91 (RS)33:35 Ks Kp N
508 * 92 (RS)36 L ((RS)36 must be 0b0)
512 dprintk("KVM MMU: mtsrin(0x%x, 0x%lx)\n", srnum, value);
515 rb |= (srnum & 0xf) << 28;
516 /* Set the valid bit */
522 rs |= (value & 0xfffffff) << 12;
524 rs |= ((value >> 28) & 0x7) << 9;
526 kvmppc_mmu_book3s_64_slbmte(vcpu, rs, rb);
529 static void kvmppc_mmu_book3s_64_tlbie(struct kvm_vcpu *vcpu, ulong va,
532 u64 mask = 0xFFFFFFFFFULL;
536 dprintk("KVM MMU: tlbie(0x%lx)\n", va);
539 * The tlbie instruction changed behaviour starting with
540 * POWER6. POWER6 and later don't have the large page flag
541 * in the instruction but in the RB value, along with bits
542 * indicating page and segment sizes.
544 if (vcpu->arch.hflags & BOOK3S_HFLAG_NEW_TLBIE) {
545 /* POWER6 or later */
546 if (va & 1) { /* L bit */
547 if ((va & 0xf000) == 0x1000)
548 mask = 0xFFFFFFFF0ULL; /* 64k page */
550 mask = 0xFFFFFF000ULL; /* 16M page */
553 /* older processors, e.g. PPC970 */
555 mask = 0xFFFFFF000ULL;
557 /* flush this VA on all vcpus */
558 kvm_for_each_vcpu(i, v, vcpu->kvm)
559 kvmppc_mmu_pte_vflush(v, va >> 12, mask);
562 #ifdef CONFIG_PPC_64K_PAGES
563 static int segment_contains_magic_page(struct kvm_vcpu *vcpu, ulong esid)
565 ulong mp_ea = vcpu->arch.magic_page_ea;
567 return mp_ea && !(kvmppc_get_msr(vcpu) & MSR_PR) &&
568 (mp_ea >> SID_SHIFT) == esid;
572 static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu *vcpu, ulong esid,
575 ulong ea = esid << SID_SHIFT;
576 struct kvmppc_slb *slb;
578 ulong mp_ea = vcpu->arch.magic_page_ea;
579 int pagesize = MMU_PAGE_64K;
580 u64 msr = kvmppc_get_msr(vcpu);
582 if (msr & (MSR_DR|MSR_IR)) {
583 slb = kvmppc_mmu_book3s_64_find_slbe(vcpu, ea);
586 pagesize = slb->base_page_size;
588 gvsid <<= SID_SHIFT_1T - SID_SHIFT;
589 gvsid |= esid & ((1ul << (SID_SHIFT_1T - SID_SHIFT)) - 1);
595 switch (msr & (MSR_DR|MSR_IR)) {
597 gvsid = VSID_REAL | esid;
600 gvsid |= VSID_REAL_IR;
603 gvsid |= VSID_REAL_DR;
615 #ifdef CONFIG_PPC_64K_PAGES
617 * Mark this as a 64k segment if the host is using
618 * 64k pages, the host MMU supports 64k pages and
619 * the guest segment page size is >= 64k,
620 * but not if this segment contains the magic page.
622 if (pagesize >= MMU_PAGE_64K &&
623 mmu_psize_defs[MMU_PAGE_64K].shift &&
624 !segment_contains_magic_page(vcpu, esid))
628 if (kvmppc_get_msr(vcpu) & MSR_PR)
635 /* Catch magic page case */
636 if (unlikely(mp_ea) &&
637 unlikely(esid == (mp_ea >> SID_SHIFT)) &&
638 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
639 *vsid = VSID_REAL | esid;
646 static bool kvmppc_mmu_book3s_64_is_dcbz32(struct kvm_vcpu *vcpu)
648 return (to_book3s(vcpu)->hid[5] & 0x80);
651 void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu)
653 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
656 mmu->mtsrin = kvmppc_mmu_book3s_64_mtsrin;
657 mmu->slbmte = kvmppc_mmu_book3s_64_slbmte;
658 mmu->slbmfee = kvmppc_mmu_book3s_64_slbmfee;
659 mmu->slbmfev = kvmppc_mmu_book3s_64_slbmfev;
660 mmu->slbfee = kvmppc_mmu_book3s_64_slbfee;
661 mmu->slbie = kvmppc_mmu_book3s_64_slbie;
662 mmu->slbia = kvmppc_mmu_book3s_64_slbia;
663 mmu->xlate = kvmppc_mmu_book3s_64_xlate;
664 mmu->tlbie = kvmppc_mmu_book3s_64_tlbie;
665 mmu->esid_to_vsid = kvmppc_mmu_book3s_64_esid_to_vsid;
666 mmu->ea_to_vp = kvmppc_mmu_book3s_64_ea_to_vp;
667 mmu->is_dcbz32 = kvmppc_mmu_book3s_64_is_dcbz32;
669 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;