2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_host.h>
22 #include <linux/highmem.h>
23 #include <linux/gfp.h>
24 #include <linux/slab.h>
25 #include <linux/hugetlb.h>
26 #include <linux/vmalloc.h>
28 #include <asm/tlbflush.h>
29 #include <asm/kvm_ppc.h>
30 #include <asm/kvm_book3s.h>
31 #include <asm/mmu-hash64.h>
32 #include <asm/hvcall.h>
33 #include <asm/synch.h>
34 #include <asm/ppc-opcode.h>
35 #include <asm/cputable.h>
37 /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
38 #define MAX_LPID_970 63
40 /* Power architecture requires HPT is at least 256kB */
41 #define PPC_MIN_HPT_ORDER 18
43 long kvmppc_alloc_hpt(struct kvm
*kvm
, u32
*htab_orderp
)
46 struct revmap_entry
*rev
;
47 struct kvmppc_linear_info
*li
;
48 long order
= kvm_hpt_order
;
52 if (order
< PPC_MIN_HPT_ORDER
)
53 order
= PPC_MIN_HPT_ORDER
;
57 * If the user wants a different size from default,
58 * try first to allocate it from the kernel page allocator.
61 if (order
!= kvm_hpt_order
) {
62 hpt
= __get_free_pages(GFP_KERNEL
|__GFP_ZERO
|__GFP_REPEAT
|
63 __GFP_NOWARN
, order
- PAGE_SHIFT
);
68 /* Next try to allocate from the preallocated pool */
72 hpt
= (ulong
)li
->base_virt
;
73 kvm
->arch
.hpt_li
= li
;
74 order
= kvm_hpt_order
;
78 /* Lastly try successively smaller sizes from the page allocator */
79 while (!hpt
&& order
> PPC_MIN_HPT_ORDER
) {
80 hpt
= __get_free_pages(GFP_KERNEL
|__GFP_ZERO
|__GFP_REPEAT
|
81 __GFP_NOWARN
, order
- PAGE_SHIFT
);
89 kvm
->arch
.hpt_virt
= hpt
;
90 kvm
->arch
.hpt_order
= order
;
91 /* HPTEs are 2**4 bytes long */
92 kvm
->arch
.hpt_npte
= 1ul << (order
- 4);
93 /* 128 (2**7) bytes in each HPTEG */
94 kvm
->arch
.hpt_mask
= (1ul << (order
- 7)) - 1;
96 /* Allocate reverse map array */
97 rev
= vmalloc(sizeof(struct revmap_entry
) * kvm
->arch
.hpt_npte
);
99 pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
102 kvm
->arch
.revmap
= rev
;
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
)
114 kvm_release_hpt(kvm
->arch
.hpt_li
);
116 free_pages(hpt
, order
- PAGE_SHIFT
);
120 long kvmppc_alloc_reset_hpt(struct kvm
*kvm
, u32
*htab_orderp
)
125 mutex_lock(&kvm
->lock
);
126 if (kvm
->arch
.rma_setup_done
) {
127 kvm
->arch
.rma_setup_done
= 0;
128 /* order rma_setup_done vs. vcpus_running */
130 if (atomic_read(&kvm
->arch
.vcpus_running
)) {
131 kvm
->arch
.rma_setup_done
= 1;
135 if (kvm
->arch
.hpt_virt
) {
136 order
= kvm
->arch
.hpt_order
;
137 /* Set the entire HPT to 0, i.e. invalid HPTEs */
138 memset((void *)kvm
->arch
.hpt_virt
, 0, 1ul << order
);
140 * Set the whole last_vcpu array to an invalid vcpu number.
141 * This ensures that each vcpu will flush its TLB on next entry.
143 memset(kvm
->arch
.last_vcpu
, 0xff, sizeof(kvm
->arch
.last_vcpu
));
144 *htab_orderp
= order
;
147 err
= kvmppc_alloc_hpt(kvm
, htab_orderp
);
148 order
= *htab_orderp
;
151 mutex_unlock(&kvm
->lock
);
155 void kvmppc_free_hpt(struct kvm
*kvm
)
157 kvmppc_free_lpid(kvm
->arch
.lpid
);
158 vfree(kvm
->arch
.revmap
);
159 if (kvm
->arch
.hpt_li
)
160 kvm_release_hpt(kvm
->arch
.hpt_li
);
162 free_pages(kvm
->arch
.hpt_virt
,
163 kvm
->arch
.hpt_order
- PAGE_SHIFT
);
166 /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
167 static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize
)
169 return (pgsize
> 0x1000) ? HPTE_V_LARGE
: 0;
172 /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
173 static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize
)
175 return (pgsize
== 0x10000) ? 0x1000 : 0;
178 void kvmppc_map_vrma(struct kvm_vcpu
*vcpu
, struct kvm_memory_slot
*memslot
,
179 unsigned long porder
)
182 unsigned long npages
;
183 unsigned long hp_v
, hp_r
;
184 unsigned long addr
, hash
;
186 unsigned long hp0
, hp1
;
188 struct kvm
*kvm
= vcpu
->kvm
;
190 psize
= 1ul << porder
;
191 npages
= memslot
->npages
>> (porder
- PAGE_SHIFT
);
193 /* VRMA can't be > 1TB */
194 if (npages
> 1ul << (40 - porder
))
195 npages
= 1ul << (40 - porder
);
196 /* Can't use more than 1 HPTE per HPTEG */
197 if (npages
> kvm
->arch
.hpt_mask
+ 1)
198 npages
= kvm
->arch
.hpt_mask
+ 1;
200 hp0
= HPTE_V_1TB_SEG
| (VRMA_VSID
<< (40 - 16)) |
201 HPTE_V_BOLTED
| hpte0_pgsize_encoding(psize
);
202 hp1
= hpte1_pgsize_encoding(psize
) |
203 HPTE_R_R
| HPTE_R_C
| HPTE_R_M
| PP_RWXX
;
205 for (i
= 0; i
< npages
; ++i
) {
207 /* can't use hpt_hash since va > 64 bits */
208 hash
= (i
^ (VRMA_VSID
^ (VRMA_VSID
<< 25))) & kvm
->arch
.hpt_mask
;
210 * We assume that the hash table is empty and no
211 * vcpus are using it at this stage. Since we create
212 * at most one HPTE per HPTEG, we just assume entry 7
213 * is available and use it.
215 hash
= (hash
<< 3) + 7;
216 hp_v
= hp0
| ((addr
>> 16) & ~0x7fUL
);
218 ret
= kvmppc_virtmode_h_enter(vcpu
, H_EXACT
, hash
, hp_v
, hp_r
);
219 if (ret
!= H_SUCCESS
) {
220 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
227 int kvmppc_mmu_hv_init(void)
229 unsigned long host_lpid
, rsvd_lpid
;
231 if (!cpu_has_feature(CPU_FTR_HVMODE
))
234 /* POWER7 has 10-bit LPIDs, PPC970 and e500mc have 6-bit LPIDs */
235 if (cpu_has_feature(CPU_FTR_ARCH_206
)) {
236 host_lpid
= mfspr(SPRN_LPID
); /* POWER7 */
237 rsvd_lpid
= LPID_RSVD
;
239 host_lpid
= 0; /* PPC970 */
240 rsvd_lpid
= MAX_LPID_970
;
243 kvmppc_init_lpid(rsvd_lpid
+ 1);
245 kvmppc_claim_lpid(host_lpid
);
246 /* rsvd_lpid is reserved for use in partition switching */
247 kvmppc_claim_lpid(rsvd_lpid
);
252 void kvmppc_mmu_destroy(struct kvm_vcpu
*vcpu
)
256 static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu
*vcpu
)
258 kvmppc_set_msr(vcpu
, MSR_SF
| MSR_ME
);
262 * This is called to get a reference to a guest page if there isn't
263 * one already in the kvm->arch.slot_phys[][] arrays.
265 static long kvmppc_get_guest_page(struct kvm
*kvm
, unsigned long gfn
,
266 struct kvm_memory_slot
*memslot
,
271 struct page
*page
, *hpage
, *pages
[1];
272 unsigned long s
, pgsize
;
273 unsigned long *physp
;
274 unsigned int is_io
, got
, pgorder
;
275 struct vm_area_struct
*vma
;
276 unsigned long pfn
, i
, npages
;
278 physp
= kvm
->arch
.slot_phys
[memslot
->id
];
281 if (physp
[gfn
- memslot
->base_gfn
])
289 start
= gfn_to_hva_memslot(memslot
, gfn
);
291 /* Instantiate and get the page we want access to */
292 np
= get_user_pages_fast(start
, 1, 1, pages
);
294 /* Look up the vma for the page */
295 down_read(¤t
->mm
->mmap_sem
);
296 vma
= find_vma(current
->mm
, start
);
297 if (!vma
|| vma
->vm_start
> start
||
298 start
+ psize
> vma
->vm_end
||
299 !(vma
->vm_flags
& VM_PFNMAP
))
301 is_io
= hpte_cache_bits(pgprot_val(vma
->vm_page_prot
));
302 pfn
= vma
->vm_pgoff
+ ((start
- vma
->vm_start
) >> PAGE_SHIFT
);
303 /* check alignment of pfn vs. requested page size */
304 if (psize
> PAGE_SIZE
&& (pfn
& ((psize
>> PAGE_SHIFT
) - 1)))
306 up_read(¤t
->mm
->mmap_sem
);
310 got
= KVMPPC_GOT_PAGE
;
312 /* See if this is a large page */
314 if (PageHuge(page
)) {
315 hpage
= compound_head(page
);
316 s
<<= compound_order(hpage
);
317 /* Get the whole large page if slot alignment is ok */
318 if (s
> psize
&& slot_is_aligned(memslot
, s
) &&
319 !(memslot
->userspace_addr
& (s
- 1))) {
329 pfn
= page_to_pfn(page
);
332 npages
= pgsize
>> PAGE_SHIFT
;
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
);
352 up_read(¤t
->mm
->mmap_sem
);
357 * We come here on a H_ENTER call from the guest when we are not
358 * using mmu notifiers and we don't have the requested page pinned
361 long kvmppc_virtmode_h_enter(struct kvm_vcpu
*vcpu
, unsigned long flags
,
362 long pte_index
, unsigned long pteh
, unsigned long ptel
)
364 struct kvm
*kvm
= vcpu
->kvm
;
365 unsigned long psize
, gpa
, gfn
;
366 struct kvm_memory_slot
*memslot
;
369 if (kvm
->arch
.using_mmu_notifiers
)
372 psize
= hpte_page_size(pteh
, ptel
);
376 pteh
&= ~(HPTE_V_HVLOCK
| HPTE_V_ABSENT
| HPTE_V_VALID
);
378 /* Find the memslot (if any) for this address */
379 gpa
= (ptel
& HPTE_R_RPN
) & ~(psize
- 1);
380 gfn
= gpa
>> PAGE_SHIFT
;
381 memslot
= gfn_to_memslot(kvm
, gfn
);
382 if (memslot
&& !(memslot
->flags
& KVM_MEMSLOT_INVALID
)) {
383 if (!slot_is_aligned(memslot
, psize
))
385 if (kvmppc_get_guest_page(kvm
, gfn
, memslot
, psize
) < 0)
390 /* Protect linux PTE lookup from page table destruction */
391 rcu_read_lock_sched(); /* this disables preemption too */
392 vcpu
->arch
.pgdir
= current
->mm
->pgd
;
393 ret
= kvmppc_h_enter(vcpu
, flags
, pte_index
, pteh
, ptel
);
394 rcu_read_unlock_sched();
395 if (ret
== H_TOO_HARD
) {
396 /* this can't happen */
397 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
398 ret
= H_RESOURCE
; /* or something */
404 static struct kvmppc_slb
*kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu
*vcpu
,
410 for (i
= 0; i
< vcpu
->arch
.slb_nr
; i
++) {
411 if (!(vcpu
->arch
.slb
[i
].orige
& SLB_ESID_V
))
414 if (vcpu
->arch
.slb
[i
].origv
& SLB_VSID_B_1T
)
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
,
435 struct kvmppc_pte
*gpte
, bool data
)
437 struct kvm
*kvm
= vcpu
->kvm
;
438 struct kvmppc_slb
*slbe
;
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
);
453 /* real mode access */
454 slb_v
= vcpu
->kvm
->arch
.vrma_slb_v
;
457 /* Find the HPTE in the hash table */
458 index
= kvmppc_hv_find_lock_hpte(kvm
, eaddr
, slb_v
,
459 HPTE_V_VALID
| HPTE_V_ABSENT
);
462 hptep
= (unsigned long *)(kvm
->arch
.hpt_virt
+ (index
<< 4));
463 v
= hptep
[0] & ~HPTE_V_HVLOCK
;
464 gr
= kvm
->arch
.revmap
[index
].guest_rpte
;
466 /* Unlock the HPTE */
467 asm volatile("lwsync" : : : "memory");
471 gpte
->vpage
= ((v
& HPTE_V_AVPN
) << 4) | ((eaddr
>> 12) & 0xfff);
473 /* Get PP bits and key for permission check */
474 pp
= gr
& (HPTE_R_PP0
| HPTE_R_PP
);
475 key
= (vcpu
->arch
.shregs
.msr
& MSR_PR
) ? SLB_VSID_KP
: SLB_VSID_KS
;
478 /* Calculate permissions */
479 gpte
->may_read
= hpte_read_permission(pp
, key
);
480 gpte
->may_write
= hpte_write_permission(pp
, key
);
481 gpte
->may_execute
= gpte
->may_read
&& !(gr
& (HPTE_R_N
| HPTE_R_G
));
483 /* Storage key permission check for POWER7 */
484 if (data
&& virtmode
&& cpu_has_feature(CPU_FTR_ARCH_206
)) {
485 int amrfield
= hpte_get_skey_perm(gr
, vcpu
->arch
.amr
);
492 /* Get the guest physical address */
493 gpte
->raddr
= kvmppc_mmu_get_real_addr(v
, gr
, eaddr
);
498 * Quick test for whether an instruction is a load or a store.
499 * If the instruction is a load or a store, then this will indicate
500 * which it is, at least on server processors. (Embedded processors
501 * have some external PID instructions that don't follow the rule
502 * embodied here.) If the instruction isn't a load or store, then
503 * this doesn't return anything useful.
505 static int instruction_is_store(unsigned int instr
)
510 if ((instr
& 0xfc000000) == 0x7c000000)
511 mask
= 0x100; /* major opcode 31 */
512 return (instr
& mask
) != 0;
515 static int kvmppc_hv_emulate_mmio(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
516 unsigned long gpa
, gva_t ea
, int is_store
)
520 unsigned long srr0
= kvmppc_get_pc(vcpu
);
522 /* We try to load the last instruction. We don't let
523 * emulate_instruction do it as it doesn't check what
525 * If we fail, we just return to the guest and try executing it again.
527 if (vcpu
->arch
.last_inst
== KVM_INST_FETCH_FAILED
) {
528 ret
= kvmppc_ld(vcpu
, &srr0
, sizeof(u32
), &last_inst
, false);
529 if (ret
!= EMULATE_DONE
|| last_inst
== KVM_INST_FETCH_FAILED
)
531 vcpu
->arch
.last_inst
= last_inst
;
535 * WARNING: We do not know for sure whether the instruction we just
536 * read from memory is the same that caused the fault in the first
537 * place. If the instruction we read is neither an load or a store,
538 * then it can't access memory, so we don't need to worry about
539 * enforcing access permissions. So, assuming it is a load or
540 * store, we just check that its direction (load or store) is
541 * consistent with the original fault, since that's what we
542 * checked the access permissions against. If there is a mismatch
543 * we just return and retry the instruction.
546 if (instruction_is_store(vcpu
->arch
.last_inst
) != !!is_store
)
550 * Emulated accesses are emulated by looking at the hash for
551 * translation once, then performing the access later. The
552 * translation could be invalidated in the meantime in which
553 * point performing the subsequent memory access on the old
554 * physical address could possibly be a security hole for the
555 * guest (but not the host).
557 * This is less of an issue for MMIO stores since they aren't
558 * globally visible. It could be an issue for MMIO loads to
559 * a certain extent but we'll ignore it for now.
562 vcpu
->arch
.paddr_accessed
= gpa
;
563 vcpu
->arch
.vaddr_accessed
= ea
;
564 return kvmppc_emulate_mmio(run
, vcpu
);
567 int kvmppc_book3s_hv_page_fault(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
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
;
574 struct kvm_memory_slot
*memslot
;
576 struct revmap_entry
*rev
;
577 struct page
*page
, *pages
[1];
578 long index
, ret
, npages
;
580 unsigned int writing
, write_ok
;
581 struct vm_area_struct
*vma
;
582 unsigned long rcbits
;
585 * Real-mode code has already searched the HPT and found the
586 * entry we're interested in. Lock the entry and check that
587 * it hasn't changed. If it has, just return and re-execute the
590 if (ea
!= vcpu
->arch
.pgfault_addr
)
592 index
= vcpu
->arch
.pgfault_index
;
593 hptep
= (unsigned long *)(kvm
->arch
.hpt_virt
+ (index
<< 4));
594 rev
= &kvm
->arch
.revmap
[index
];
596 while (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
))
598 hpte
[0] = hptep
[0] & ~HPTE_V_HVLOCK
;
600 hpte
[2] = r
= rev
->guest_rpte
;
601 asm volatile("lwsync" : : : "memory");
605 if (hpte
[0] != vcpu
->arch
.pgfault_hpte
[0] ||
606 hpte
[1] != vcpu
->arch
.pgfault_hpte
[1])
609 /* Translate the logical address and get the page */
610 psize
= hpte_page_size(hpte
[0], r
);
611 gfn
= hpte_rpn(r
, psize
);
612 memslot
= gfn_to_memslot(kvm
, gfn
);
614 /* No memslot means it's an emulated MMIO region */
615 if (!memslot
|| (memslot
->flags
& KVM_MEMSLOT_INVALID
)) {
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
)
622 return -EFAULT
; /* should never get here */
624 /* used to check for invalidations in progress */
625 mmu_seq
= kvm
->mmu_notifier_seq
;
631 pte_size
= PAGE_SIZE
;
632 writing
= (dsisr
& DSISR_ISSTORE
) != 0;
633 /* If writing != 0, then the HPTE must allow writing, if we get here */
635 hva
= gfn_to_hva_memslot(memslot
, gfn
);
636 npages
= get_user_pages_fast(hva
, 1, writing
, pages
);
638 /* Check if it's an I/O mapping */
639 down_read(¤t
->mm
->mmap_sem
);
640 vma
= find_vma(current
->mm
, hva
);
641 if (vma
&& vma
->vm_start
<= hva
&& hva
+ psize
<= vma
->vm_end
&&
642 (vma
->vm_flags
& VM_PFNMAP
)) {
643 pfn
= vma
->vm_pgoff
+
644 ((hva
- vma
->vm_start
) >> PAGE_SHIFT
);
646 is_io
= hpte_cache_bits(pgprot_val(vma
->vm_page_prot
));
647 write_ok
= vma
->vm_flags
& VM_WRITE
;
649 up_read(¤t
->mm
->mmap_sem
);
654 if (PageHuge(page
)) {
655 page
= compound_head(page
);
656 pte_size
<<= compound_order(page
);
658 /* if the guest wants write access, see if that is OK */
659 if (!writing
&& hpte_is_writable(r
)) {
663 * We need to protect against page table destruction
664 * while looking up and updating the pte.
666 rcu_read_lock_sched();
667 ptep
= find_linux_pte_or_hugepte(current
->mm
->pgd
,
669 if (ptep
&& pte_present(*ptep
)) {
670 pte
= kvmppc_read_update_linux_pte(ptep
, 1);
674 rcu_read_unlock_sched();
676 pfn
= page_to_pfn(page
);
680 if (psize
> pte_size
)
683 /* Check WIMG vs. the actual page we're accessing */
684 if (!hpte_cache_flags_ok(r
, is_io
)) {
688 * Allow guest to map emulated device memory as
689 * uncacheable, but actually make it cacheable.
691 r
= (r
& ~(HPTE_R_W
|HPTE_R_I
|HPTE_R_G
)) | HPTE_R_M
;
694 /* Set the HPTE to point to pfn */
695 r
= (r
& ~(HPTE_R_PP0
- pte_size
)) | (pfn
<< PAGE_SHIFT
);
696 if (hpte_is_writable(r
) && !write_ok
)
697 r
= hpte_make_readonly(r
);
700 while (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
))
702 if ((hptep
[0] & ~HPTE_V_HVLOCK
) != hpte
[0] || hptep
[1] != hpte
[1] ||
703 rev
->guest_rpte
!= hpte
[2])
704 /* HPTE has been changed under us; let the guest retry */
706 hpte
[0] = (hpte
[0] & ~HPTE_V_ABSENT
) | HPTE_V_VALID
;
708 rmap
= &memslot
->rmap
[gfn
- memslot
->base_gfn
];
711 /* Check if we might have been invalidated; let the guest retry if so */
713 if (mmu_notifier_retry(vcpu
, mmu_seq
)) {
718 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
719 rcbits
= *rmap
>> KVMPPC_RMAP_RC_SHIFT
;
720 r
&= rcbits
| ~(HPTE_R_R
| HPTE_R_C
);
722 if (hptep
[0] & HPTE_V_VALID
) {
723 /* HPTE was previously valid, so we need to invalidate it */
725 hptep
[0] |= HPTE_V_ABSENT
;
726 kvmppc_invalidate_hpte(kvm
, hptep
, index
);
727 /* don't lose previous R and C bits */
728 r
|= hptep
[1] & (HPTE_R_R
| HPTE_R_C
);
730 kvmppc_add_revmap_chain(kvm
, rev
, rmap
, index
, 0);
736 asm volatile("ptesync" : : : "memory");
738 if (page
&& hpte_is_writable(r
))
744 * We drop pages[0] here, not page because page might
745 * have been set to the head page of a compound, but
746 * we have to drop the reference on the correct tail
747 * page to match the get inside gup()
754 hptep
[0] &= ~HPTE_V_HVLOCK
;
759 static int kvm_handle_hva(struct kvm
*kvm
, unsigned long hva
,
760 int (*handler
)(struct kvm
*kvm
, unsigned long *rmapp
,
765 struct kvm_memslots
*slots
;
766 struct kvm_memory_slot
*memslot
;
768 slots
= kvm_memslots(kvm
);
769 kvm_for_each_memslot(memslot
, slots
) {
770 unsigned long start
= memslot
->userspace_addr
;
773 end
= start
+ (memslot
->npages
<< PAGE_SHIFT
);
774 if (hva
>= start
&& hva
< end
) {
775 gfn_t gfn_offset
= (hva
- start
) >> PAGE_SHIFT
;
777 ret
= handler(kvm
, &memslot
->rmap
[gfn_offset
],
778 memslot
->base_gfn
+ gfn_offset
);
786 static int kvm_unmap_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
789 struct revmap_entry
*rev
= kvm
->arch
.revmap
;
790 unsigned long h
, i
, j
;
791 unsigned long *hptep
;
792 unsigned long ptel
, psize
, rcbits
;
796 if (!(*rmapp
& KVMPPC_RMAP_PRESENT
)) {
802 * To avoid an ABBA deadlock with the HPTE lock bit,
803 * we can't spin on the HPTE lock while holding the
806 i
= *rmapp
& KVMPPC_RMAP_INDEX
;
807 hptep
= (unsigned long *) (kvm
->arch
.hpt_virt
+ (i
<< 4));
808 if (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
)) {
809 /* unlock rmap before spinning on the HPTE lock */
811 while (hptep
[0] & HPTE_V_HVLOCK
)
817 /* chain is now empty */
818 *rmapp
&= ~(KVMPPC_RMAP_PRESENT
| KVMPPC_RMAP_INDEX
);
820 /* remove i from chain */
824 rev
[i
].forw
= rev
[i
].back
= i
;
825 *rmapp
= (*rmapp
& ~KVMPPC_RMAP_INDEX
) | j
;
828 /* Now check and modify the HPTE */
829 ptel
= rev
[i
].guest_rpte
;
830 psize
= hpte_page_size(hptep
[0], ptel
);
831 if ((hptep
[0] & HPTE_V_VALID
) &&
832 hpte_rpn(ptel
, psize
) == gfn
) {
833 hptep
[0] |= HPTE_V_ABSENT
;
834 kvmppc_invalidate_hpte(kvm
, hptep
, i
);
835 /* Harvest R and C */
836 rcbits
= hptep
[1] & (HPTE_R_R
| HPTE_R_C
);
837 *rmapp
|= rcbits
<< KVMPPC_RMAP_RC_SHIFT
;
838 rev
[i
].guest_rpte
= ptel
| rcbits
;
841 hptep
[0] &= ~HPTE_V_HVLOCK
;
846 int kvm_unmap_hva(struct kvm
*kvm
, unsigned long hva
)
848 if (kvm
->arch
.using_mmu_notifiers
)
849 kvm_handle_hva(kvm
, hva
, kvm_unmap_rmapp
);
853 static int kvm_age_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
856 struct revmap_entry
*rev
= kvm
->arch
.revmap
;
857 unsigned long head
, i
, j
;
858 unsigned long *hptep
;
863 if (*rmapp
& KVMPPC_RMAP_REFERENCED
) {
864 *rmapp
&= ~KVMPPC_RMAP_REFERENCED
;
867 if (!(*rmapp
& KVMPPC_RMAP_PRESENT
)) {
872 i
= head
= *rmapp
& KVMPPC_RMAP_INDEX
;
874 hptep
= (unsigned long *) (kvm
->arch
.hpt_virt
+ (i
<< 4));
877 /* If this HPTE isn't referenced, ignore it */
878 if (!(hptep
[1] & HPTE_R_R
))
881 if (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
)) {
882 /* unlock rmap before spinning on the HPTE lock */
884 while (hptep
[0] & HPTE_V_HVLOCK
)
889 /* Now check and modify the HPTE */
890 if ((hptep
[0] & HPTE_V_VALID
) && (hptep
[1] & HPTE_R_R
)) {
891 kvmppc_clear_ref_hpte(kvm
, hptep
, i
);
892 rev
[i
].guest_rpte
|= HPTE_R_R
;
895 hptep
[0] &= ~HPTE_V_HVLOCK
;
896 } while ((i
= j
) != head
);
902 int kvm_age_hva(struct kvm
*kvm
, unsigned long hva
)
904 if (!kvm
->arch
.using_mmu_notifiers
)
906 return kvm_handle_hva(kvm
, hva
, kvm_age_rmapp
);
909 static int kvm_test_age_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
912 struct revmap_entry
*rev
= kvm
->arch
.revmap
;
913 unsigned long head
, i
, j
;
917 if (*rmapp
& KVMPPC_RMAP_REFERENCED
)
921 if (*rmapp
& KVMPPC_RMAP_REFERENCED
)
924 if (*rmapp
& KVMPPC_RMAP_PRESENT
) {
925 i
= head
= *rmapp
& KVMPPC_RMAP_INDEX
;
927 hp
= (unsigned long *)(kvm
->arch
.hpt_virt
+ (i
<< 4));
929 if (hp
[1] & HPTE_R_R
)
931 } while ((i
= j
) != head
);
940 int kvm_test_age_hva(struct kvm
*kvm
, unsigned long hva
)
942 if (!kvm
->arch
.using_mmu_notifiers
)
944 return kvm_handle_hva(kvm
, hva
, kvm_test_age_rmapp
);
947 void kvm_set_spte_hva(struct kvm
*kvm
, unsigned long hva
, pte_t pte
)
949 if (!kvm
->arch
.using_mmu_notifiers
)
951 kvm_handle_hva(kvm
, hva
, kvm_unmap_rmapp
);
954 static int kvm_test_clear_dirty(struct kvm
*kvm
, unsigned long *rmapp
)
956 struct revmap_entry
*rev
= kvm
->arch
.revmap
;
957 unsigned long head
, i
, j
;
958 unsigned long *hptep
;
963 if (*rmapp
& KVMPPC_RMAP_CHANGED
) {
964 *rmapp
&= ~KVMPPC_RMAP_CHANGED
;
967 if (!(*rmapp
& KVMPPC_RMAP_PRESENT
)) {
972 i
= head
= *rmapp
& KVMPPC_RMAP_INDEX
;
974 hptep
= (unsigned long *) (kvm
->arch
.hpt_virt
+ (i
<< 4));
977 if (!(hptep
[1] & HPTE_R_C
))
980 if (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
)) {
981 /* unlock rmap before spinning on the HPTE lock */
983 while (hptep
[0] & HPTE_V_HVLOCK
)
988 /* Now check and modify the HPTE */
989 if ((hptep
[0] & HPTE_V_VALID
) && (hptep
[1] & HPTE_R_C
)) {
990 /* need to make it temporarily absent to clear C */
991 hptep
[0] |= HPTE_V_ABSENT
;
992 kvmppc_invalidate_hpte(kvm
, hptep
, i
);
993 hptep
[1] &= ~HPTE_R_C
;
995 hptep
[0] = (hptep
[0] & ~HPTE_V_ABSENT
) | HPTE_V_VALID
;
996 rev
[i
].guest_rpte
|= HPTE_R_C
;
999 hptep
[0] &= ~HPTE_V_HVLOCK
;
1000 } while ((i
= j
) != head
);
1006 long kvmppc_hv_get_dirty_log(struct kvm
*kvm
, struct kvm_memory_slot
*memslot
)
1009 unsigned long *rmapp
, *map
;
1012 rmapp
= memslot
->rmap
;
1013 map
= memslot
->dirty_bitmap
;
1014 for (i
= 0; i
< memslot
->npages
; ++i
) {
1015 if (kvm_test_clear_dirty(kvm
, rmapp
))
1016 __set_bit_le(i
, map
);
1023 void *kvmppc_pin_guest_page(struct kvm
*kvm
, unsigned long gpa
,
1024 unsigned long *nb_ret
)
1026 struct kvm_memory_slot
*memslot
;
1027 unsigned long gfn
= gpa
>> PAGE_SHIFT
;
1028 struct page
*page
, *pages
[1];
1030 unsigned long hva
, psize
, offset
;
1032 unsigned long *physp
;
1034 memslot
= gfn_to_memslot(kvm
, gfn
);
1035 if (!memslot
|| (memslot
->flags
& KVM_MEMSLOT_INVALID
))
1037 if (!kvm
->arch
.using_mmu_notifiers
) {
1038 physp
= kvm
->arch
.slot_phys
[memslot
->id
];
1041 physp
+= gfn
- memslot
->base_gfn
;
1044 if (kvmppc_get_guest_page(kvm
, gfn
, memslot
,
1049 page
= pfn_to_page(pa
>> PAGE_SHIFT
);
1052 hva
= gfn_to_hva_memslot(memslot
, gfn
);
1053 npages
= get_user_pages_fast(hva
, 1, 1, pages
);
1059 if (PageHuge(page
)) {
1060 page
= compound_head(page
);
1061 psize
<<= compound_order(page
);
1063 offset
= gpa
& (psize
- 1);
1065 *nb_ret
= psize
- offset
;
1066 return page_address(page
) + offset
;
1069 void kvmppc_unpin_guest_page(struct kvm
*kvm
, void *va
)
1071 struct page
*page
= virt_to_page(va
);
1076 void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu
*vcpu
)
1078 struct kvmppc_mmu
*mmu
= &vcpu
->arch
.mmu
;
1080 if (cpu_has_feature(CPU_FTR_ARCH_206
))
1081 vcpu
->arch
.slb_nr
= 32; /* POWER7 */
1083 vcpu
->arch
.slb_nr
= 64;
1085 mmu
->xlate
= kvmppc_mmu_book3s_64_hv_xlate
;
1086 mmu
->reset_msr
= kvmppc_mmu_book3s_64_hv_reset_msr
;
1088 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_SLB
;