18 #include <linux/types.h>
19 #include <linux/string.h>
24 #include <linux/slab.h>
28 #include <asm/tlbflush.h>
35 #include <asm/cputable.h>
38 #define MAX_LPID_970 63
41 #define PPC_MIN_HPT_ORDER 18
73 kvm->
arch.hpt_li = li;
89 kvm->
arch.hpt_virt = hpt;
92 kvm->
arch.hpt_npte = 1ul << (order - 4);
94 kvm->
arch.hpt_mask = (1ul << (order - 7)) - 1;
99 pr_err(
"kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
103 kvm->
arch.sdr1 =
__pa(hpt) | (order - 18);
105 pr_info(
"KVM guest htab at %lx (order %ld), LPID %x\n",
106 hpt, order, kvm->
arch.lpid);
109 *htab_orderp =
order;
113 if (kvm->
arch.hpt_li)
126 if (kvm->
arch.rma_setup_done) {
127 kvm->
arch.rma_setup_done = 0;
131 kvm->
arch.rma_setup_done = 1;
135 if (kvm->
arch.hpt_virt) {
136 order = kvm->
arch.hpt_order;
138 memset((
void *)kvm->
arch.hpt_virt, 0, 1ul << order);
144 *htab_orderp =
order;
148 order = *htab_orderp;
159 if (kvm->
arch.hpt_li)
167 static inline unsigned long hpte0_pgsize_encoding(
unsigned long pgsize)
173 static inline unsigned long hpte1_pgsize_encoding(
unsigned long pgsize)
175 return (pgsize == 0x10000) ? 0x1000 : 0;
179 unsigned long porder)
183 unsigned long hp_v, hp_r;
186 unsigned long hp0, hp1;
190 psize = 1ul << porder;
194 if (npages > 1ul << (40 - porder))
195 npages = 1ul << (40 - porder);
197 if (npages > kvm->
arch.hpt_mask + 1)
198 npages = kvm->
arch.hpt_mask + 1;
202 hp1 = hpte1_pgsize_encoding(psize) |
205 for (i = 0; i < npages; ++
i) {
215 hash = (hash << 3) + 7;
216 hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
219 if (ret != H_SUCCESS) {
220 pr_err(
"KVM: map_vrma at %lx failed, ret=%ld\n",
229 unsigned long host_lpid, rsvd_lpid;
236 host_lpid =
mfspr(SPRN_LPID);
237 rsvd_lpid = LPID_RSVD;
256 static void kvmppc_mmu_book3s_64_hv_reset_msr(
struct kvm_vcpu *vcpu)
265 static long kvmppc_get_guest_page(
struct kvm *
kvm,
unsigned long gfn,
272 unsigned long s, pgsize;
273 unsigned long *physp;
274 unsigned int is_io, got, pgorder;
276 unsigned long pfn,
i, npages;
278 physp = kvm->
arch.slot_phys[memslot->
id];
297 if (!vma || vma->
vm_start > start ||
298 start + psize > vma->
vm_end ||
315 hpage = compound_head(page);
316 s <<= compound_order(hpage);
318 if (s > psize && slot_is_aligned(memslot, s) &&
333 pgorder = __ilog2(npages);
334 physp += (gfn - memslot->
base_gfn) & ~(npages - 1);
335 spin_lock(&kvm->
arch.slot_phys_lock);
336 for (i = 0; i < npages; ++
i) {
338 physp[
i] = ((pfn +
i) << PAGE_SHIFT) +
339 got + is_io + pgorder;
343 spin_unlock(&kvm->
arch.slot_phys_lock);
362 long pte_index,
unsigned long pteh,
unsigned long ptel)
364 struct kvm *kvm = vcpu->
kvm;
365 unsigned long psize, gpa, gfn;
369 if (kvm->
arch.using_mmu_notifiers)
372 psize = hpte_page_size(pteh, ptel);
383 if (!slot_is_aligned(memslot, psize))
385 if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0)
391 rcu_read_lock_sched();
394 rcu_read_unlock_sched();
395 if (ret == H_TOO_HARD) {
397 pr_err(
"KVM: Oops, kvmppc_h_enter returned too hard!\n");
410 for (i = 0; i < vcpu->
arch.slb_nr; i++) {
419 if (((vcpu->
arch.slb[i].orige ^ eaddr) & mask) == 0)
420 return &vcpu->
arch.slb[
i];
425 static unsigned long kvmppc_mmu_get_real_addr(
unsigned long v,
unsigned long r,
428 unsigned long ra_mask;
430 ra_mask = hpte_page_size(v, r) - 1;
431 return (r &
HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
434 static int kvmppc_mmu_book3s_64_hv_xlate(
struct kvm_vcpu *vcpu,
gva_t eaddr,
437 struct kvm *kvm = vcpu->
kvm;
440 unsigned long pp,
key;
442 unsigned long *hptep;
444 int virtmode = vcpu->
arch.shregs.msr & (data ? MSR_DR : MSR_IR);
448 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
454 slb_v = vcpu->
kvm->arch.vrma_slb_v;
462 hptep = (
unsigned long *)(kvm->
arch.hpt_virt + (index << 4));
467 asm volatile(
"lwsync" : : :
"memory");
479 gpte->
may_read = hpte_read_permission(pp, key);
480 gpte->
may_write = hpte_write_permission(pp, key);
485 int amrfield = hpte_get_skey_perm(gr, vcpu->
arch.amr);
493 gpte->
raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
505 static int instruction_is_store(
unsigned int instr)
510 if ((instr & 0xfc000000) == 0x7c000000)
512 return (instr & mask) != 0;
516 unsigned long gpa,
gva_t ea,
int is_store)
520 unsigned long srr0 = kvmppc_get_pc(vcpu);
528 ret =
kvmppc_ld(vcpu, &srr0,
sizeof(
u32), &last_inst,
false);
531 vcpu->
arch.last_inst = last_inst;
546 if (instruction_is_store(vcpu->
arch.last_inst) != !!is_store)
562 vcpu->
arch.paddr_accessed = gpa;
563 vcpu->
arch.vaddr_accessed =
ea;
568 unsigned long ea,
unsigned long dsisr)
570 struct kvm *kvm = vcpu->
kvm;
571 unsigned long *hptep, hpte[3],
r;
572 unsigned long mmu_seq, psize, pte_size;
573 unsigned long gfn, hva, pfn;
577 struct page *
page, *pages[1];
580 unsigned int writing, write_ok;
582 unsigned long rcbits;
590 if (ea != vcpu->
arch.pgfault_addr)
592 index = vcpu->
arch.pgfault_index;
593 hptep = (
unsigned long *)(kvm->
arch.hpt_virt + (index << 4));
601 asm volatile(
"lwsync" : : :
"memory");
605 if (hpte[0] != vcpu->
arch.pgfault_hpte[0] ||
606 hpte[1] != vcpu->
arch.pgfault_hpte[1])
610 psize = hpte_page_size(hpte[0], r);
611 gfn = hpte_rpn(r, psize);
616 unsigned long gpa = (gfn <<
PAGE_SHIFT) | (ea & (psize - 1));
617 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
618 dsisr & DSISR_ISSTORE);
621 if (!kvm->
arch.using_mmu_notifiers)
625 mmu_seq = kvm->mmu_notifier_seq;
632 writing = (dsisr & DSISR_ISSTORE) != 0;
644 ((hva - vma->
vm_start) >> PAGE_SHIFT);
647 write_ok = vma->
vm_flags & VM_WRITE;
655 page = compound_head(page);
656 pte_size <<= compound_order(page);
659 if (!writing && hpte_is_writable(r)) {
666 rcu_read_lock_sched();
670 pte = kvmppc_read_update_linux_pte(ptep, 1);
674 rcu_read_unlock_sched();
680 if (psize > pte_size)
684 if (!hpte_cache_flags_ok(r, is_io)) {
696 if (hpte_is_writable(r) && !write_ok)
697 r = hpte_make_readonly(r);
702 if ((hptep[0] & ~
HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] ||
713 if (mmu_notifier_retry(vcpu, mmu_seq)) {
736 asm volatile(
"ptesync" : : :
"memory");
738 if (page && hpte_is_writable(r))
759 static int kvm_handle_hva_range(
struct kvm *kvm,
762 int (*handler)(
struct kvm *kvm,
763 unsigned long *rmapp,
773 unsigned long hva_start, hva_end;
778 (memslot->
npages << PAGE_SHIFT));
779 if (hva_start >= hva_end)
785 gfn = hva_to_gfn_memslot(hva_start, memslot);
786 gfn_end = hva_to_gfn_memslot(hva_end +
PAGE_SIZE - 1, memslot);
788 for (; gfn < gfn_end; ++gfn) {
791 ret =
handler(kvm, &memslot->
arch.rmap[gfn_offset], gfn);
799 static int kvm_handle_hva(
struct kvm *kvm,
unsigned long hva,
800 int (*handler)(
struct kvm *kvm,
unsigned long *rmapp,
803 return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
806 static int kvm_unmap_rmapp(
struct kvm *kvm,
unsigned long *rmapp,
810 unsigned long h,
i,
j;
811 unsigned long *hptep;
812 unsigned long ptel, psize, rcbits;
827 hptep = (
unsigned long *) (kvm->
arch.hpt_virt + (i << 4));
845 *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
850 psize = hpte_page_size(hptep[0], ptel);
852 hpte_rpn(ptel, psize) == gfn) {
868 if (kvm->
arch.using_mmu_notifiers)
869 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
875 if (kvm->
arch.using_mmu_notifiers)
876 kvm_handle_hva_range(kvm, start, end, kvm_unmap_rmapp);
880 static int kvm_age_rmapp(
struct kvm *kvm,
unsigned long *rmapp,
885 unsigned long *hptep;
891 *rmapp &= ~KVMPPC_RMAP_REFERENCED;
894 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
901 hptep = (
unsigned long *) (kvm->
arch.hpt_virt + (i << 4));
917 if ((hptep[0] &
HPTE_V_VALID) && (hptep[1] & HPTE_R_R)) {
923 }
while ((i = j) != head);
931 if (!kvm->
arch.using_mmu_notifiers)
933 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
936 static int kvm_test_age_rmapp(
struct kvm *kvm,
unsigned long *rmapp,
944 if (*rmapp & KVMPPC_RMAP_REFERENCED)
948 if (*rmapp & KVMPPC_RMAP_REFERENCED)
951 if (*rmapp & KVMPPC_RMAP_PRESENT) {
954 hp = (
unsigned long *)(kvm->
arch.hpt_virt + (i << 4));
956 if (hp[1] & HPTE_R_R)
958 }
while ((i = j) !=
head);
969 if (!kvm->
arch.using_mmu_notifiers)
971 return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
976 if (!kvm->
arch.using_mmu_notifiers)
978 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
981 static int kvm_test_clear_dirty(
struct kvm *kvm,
unsigned long *rmapp)
985 unsigned long *hptep;
991 *rmapp &= ~KVMPPC_RMAP_CHANGED;
994 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
1001 hptep = (
unsigned long *) (kvm->
arch.hpt_virt + (i << 4));
1016 if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_C)) {
1020 hptep[1] &= ~HPTE_R_C;
1027 }
while ((i = j) !=
head);
1036 unsigned long *rmapp, *
map;
1039 rmapp = memslot->
arch.rmap;
1041 for (i = 0; i < memslot->
npages; ++
i) {
1042 if (kvm_test_clear_dirty(kvm, rmapp))
1043 __set_bit_le(i, map);
1051 unsigned long *nb_ret)
1055 struct page *
page, *pages[1];
1057 unsigned long hva, psize,
offset;
1059 unsigned long *physp;
1064 if (!kvm->
arch.using_mmu_notifiers) {
1065 physp = kvm->
arch.slot_phys[memslot->
id];
1071 if (kvmppc_get_guest_page(kvm, gfn, memslot,
1087 page = compound_head(page);
1088 psize <<= compound_order(page);
1090 offset = gpa & (psize - 1);
1092 *nb_ret = psize -
offset;
1108 vcpu->
arch.slb_nr = 32;
1110 vcpu->
arch.slb_nr = 64;
1112 mmu->
xlate = kvmppc_mmu_book3s_64_hv_xlate;
1113 mmu->
reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;