1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
7 #include <linux/types.h>
8 #include <linux/string.h>
10 #include <linux/kvm_host.h>
11 #include <linux/highmem.h>
12 #include <linux/gfp.h>
13 #include <linux/slab.h>
14 #include <linux/hugetlb.h>
15 #include <linux/vmalloc.h>
16 #include <linux/srcu.h>
17 #include <linux/anon_inodes.h>
18 #include <linux/file.h>
19 #include <linux/debugfs.h>
21 #include <asm/kvm_ppc.h>
22 #include <asm/kvm_book3s.h>
23 #include <asm/book3s/64/mmu-hash.h>
24 #include <asm/hvcall.h>
25 #include <asm/synch.h>
26 #include <asm/ppc-opcode.h>
27 #include <asm/cputable.h>
28 #include <asm/pte-walk.h>
32 //#define DEBUG_RESIZE_HPT 1
34 #ifdef DEBUG_RESIZE_HPT
35 #define resize_hpt_debug(resize, ...) \
37 printk(KERN_DEBUG "RESIZE HPT %p: ", resize); \
38 printk(__VA_ARGS__); \
41 #define resize_hpt_debug(resize, ...) \
45 static long kvmppc_virtmode_do_h_enter(struct kvm
*kvm
, unsigned long flags
,
46 long pte_index
, unsigned long pteh
,
47 unsigned long ptel
, unsigned long *pte_idx_ret
);
49 struct kvm_resize_hpt
{
50 /* These fields read-only after init */
52 struct work_struct work
;
55 /* These fields protected by kvm->arch.mmu_setup_lock */
57 /* Possible values and their usage:
58 * <0 an error occurred during allocation,
59 * -EBUSY allocation is in the progress,
60 * 0 allocation made successfuly.
64 /* Private to the work thread, until error != -EBUSY,
65 * then protected by kvm->arch.mmu_setup_lock.
67 struct kvm_hpt_info hpt
;
70 int kvmppc_allocate_hpt(struct kvm_hpt_info
*info
, u32 order
)
72 unsigned long hpt
= 0;
74 struct page
*page
= NULL
;
75 struct revmap_entry
*rev
;
78 if ((order
< PPC_MIN_HPT_ORDER
) || (order
> PPC_MAX_HPT_ORDER
))
81 page
= kvm_alloc_hpt_cma(1ul << (order
- PAGE_SHIFT
));
83 hpt
= (unsigned long)pfn_to_kaddr(page_to_pfn(page
));
84 memset((void *)hpt
, 0, (1ul << order
));
89 hpt
= __get_free_pages(GFP_KERNEL
|__GFP_ZERO
|__GFP_RETRY_MAYFAIL
90 |__GFP_NOWARN
, order
- PAGE_SHIFT
);
95 /* HPTEs are 2**4 bytes long */
96 npte
= 1ul << (order
- 4);
98 /* Allocate reverse map array */
99 rev
= vmalloc(array_size(npte
, sizeof(struct revmap_entry
)));
102 kvm_free_hpt_cma(page
, 1 << (order
- PAGE_SHIFT
));
104 free_pages(hpt
, order
- PAGE_SHIFT
);
116 void kvmppc_set_hpt(struct kvm
*kvm
, struct kvm_hpt_info
*info
)
118 atomic64_set(&kvm
->arch
.mmio_update
, 0);
119 kvm
->arch
.hpt
= *info
;
120 kvm
->arch
.sdr1
= __pa(info
->virt
) | (info
->order
- 18);
122 pr_debug("KVM guest htab at %lx (order %ld), LPID %x\n",
123 info
->virt
, (long)info
->order
, kvm
->arch
.lpid
);
126 long kvmppc_alloc_reset_hpt(struct kvm
*kvm
, int order
)
129 struct kvm_hpt_info info
;
131 mutex_lock(&kvm
->arch
.mmu_setup_lock
);
132 if (kvm
->arch
.mmu_ready
) {
133 kvm
->arch
.mmu_ready
= 0;
134 /* order mmu_ready vs. vcpus_running */
136 if (atomic_read(&kvm
->arch
.vcpus_running
)) {
137 kvm
->arch
.mmu_ready
= 1;
141 if (kvm_is_radix(kvm
)) {
142 err
= kvmppc_switch_mmu_to_hpt(kvm
);
147 if (kvm
->arch
.hpt
.order
== order
) {
148 /* We already have a suitable HPT */
150 /* Set the entire HPT to 0, i.e. invalid HPTEs */
151 memset((void *)kvm
->arch
.hpt
.virt
, 0, 1ul << order
);
153 * Reset all the reverse-mapping chains for all memslots
155 kvmppc_rmap_reset(kvm
);
160 if (kvm
->arch
.hpt
.virt
) {
161 kvmppc_free_hpt(&kvm
->arch
.hpt
);
162 kvmppc_rmap_reset(kvm
);
165 err
= kvmppc_allocate_hpt(&info
, order
);
168 kvmppc_set_hpt(kvm
, &info
);
172 /* Ensure that each vcpu will flush its TLB on next entry. */
173 cpumask_setall(&kvm
->arch
.need_tlb_flush
);
175 mutex_unlock(&kvm
->arch
.mmu_setup_lock
);
179 void kvmppc_free_hpt(struct kvm_hpt_info
*info
)
184 kvm_free_hpt_cma(virt_to_page(info
->virt
),
185 1 << (info
->order
- PAGE_SHIFT
));
187 free_pages(info
->virt
, info
->order
- PAGE_SHIFT
);
192 /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
193 static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize
)
195 return (pgsize
> 0x1000) ? HPTE_V_LARGE
: 0;
198 /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
199 static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize
)
201 return (pgsize
== 0x10000) ? 0x1000 : 0;
204 void kvmppc_map_vrma(struct kvm_vcpu
*vcpu
, struct kvm_memory_slot
*memslot
,
205 unsigned long porder
)
208 unsigned long npages
;
209 unsigned long hp_v
, hp_r
;
210 unsigned long addr
, hash
;
212 unsigned long hp0
, hp1
;
213 unsigned long idx_ret
;
215 struct kvm
*kvm
= vcpu
->kvm
;
217 psize
= 1ul << porder
;
218 npages
= memslot
->npages
>> (porder
- PAGE_SHIFT
);
220 /* VRMA can't be > 1TB */
221 if (npages
> 1ul << (40 - porder
))
222 npages
= 1ul << (40 - porder
);
223 /* Can't use more than 1 HPTE per HPTEG */
224 if (npages
> kvmppc_hpt_mask(&kvm
->arch
.hpt
) + 1)
225 npages
= kvmppc_hpt_mask(&kvm
->arch
.hpt
) + 1;
227 hp0
= HPTE_V_1TB_SEG
| (VRMA_VSID
<< (40 - 16)) |
228 HPTE_V_BOLTED
| hpte0_pgsize_encoding(psize
);
229 hp1
= hpte1_pgsize_encoding(psize
) |
230 HPTE_R_R
| HPTE_R_C
| HPTE_R_M
| PP_RWXX
;
232 for (i
= 0; i
< npages
; ++i
) {
234 /* can't use hpt_hash since va > 64 bits */
235 hash
= (i
^ (VRMA_VSID
^ (VRMA_VSID
<< 25)))
236 & kvmppc_hpt_mask(&kvm
->arch
.hpt
);
238 * We assume that the hash table is empty and no
239 * vcpus are using it at this stage. Since we create
240 * at most one HPTE per HPTEG, we just assume entry 7
241 * is available and use it.
243 hash
= (hash
<< 3) + 7;
244 hp_v
= hp0
| ((addr
>> 16) & ~0x7fUL
);
246 ret
= kvmppc_virtmode_do_h_enter(kvm
, H_EXACT
, hash
, hp_v
, hp_r
,
248 if (ret
!= H_SUCCESS
) {
249 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
256 int kvmppc_mmu_hv_init(void)
258 unsigned long host_lpid
, rsvd_lpid
;
260 if (!mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE
))
264 if (cpu_has_feature(CPU_FTR_HVMODE
))
265 host_lpid
= mfspr(SPRN_LPID
);
267 /* POWER8 and above have 12-bit LPIDs (10-bit in POWER7) */
268 if (cpu_has_feature(CPU_FTR_ARCH_207S
))
269 rsvd_lpid
= LPID_RSVD
;
271 rsvd_lpid
= LPID_RSVD_POWER7
;
273 kvmppc_init_lpid(rsvd_lpid
+ 1);
275 kvmppc_claim_lpid(host_lpid
);
276 /* rsvd_lpid is reserved for use in partition switching */
277 kvmppc_claim_lpid(rsvd_lpid
);
282 static long kvmppc_virtmode_do_h_enter(struct kvm
*kvm
, unsigned long flags
,
283 long pte_index
, unsigned long pteh
,
284 unsigned long ptel
, unsigned long *pte_idx_ret
)
289 ret
= kvmppc_do_h_enter(kvm
, flags
, pte_index
, pteh
, ptel
,
290 kvm
->mm
->pgd
, false, pte_idx_ret
);
292 if (ret
== H_TOO_HARD
) {
293 /* this can't happen */
294 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
295 ret
= H_RESOURCE
; /* or something */
301 static struct kvmppc_slb
*kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu
*vcpu
,
307 for (i
= 0; i
< vcpu
->arch
.slb_nr
; i
++) {
308 if (!(vcpu
->arch
.slb
[i
].orige
& SLB_ESID_V
))
311 if (vcpu
->arch
.slb
[i
].origv
& SLB_VSID_B_1T
)
316 if (((vcpu
->arch
.slb
[i
].orige
^ eaddr
) & mask
) == 0)
317 return &vcpu
->arch
.slb
[i
];
322 static unsigned long kvmppc_mmu_get_real_addr(unsigned long v
, unsigned long r
,
325 unsigned long ra_mask
;
327 ra_mask
= kvmppc_actual_pgsz(v
, r
) - 1;
328 return (r
& HPTE_R_RPN
& ~ra_mask
) | (ea
& ra_mask
);
331 static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu
*vcpu
, gva_t eaddr
,
332 struct kvmppc_pte
*gpte
, bool data
, bool iswrite
)
334 struct kvm
*kvm
= vcpu
->kvm
;
335 struct kvmppc_slb
*slbe
;
337 unsigned long pp
, key
;
338 unsigned long v
, orig_v
, gr
;
341 int virtmode
= vcpu
->arch
.shregs
.msr
& (data
? MSR_DR
: MSR_IR
);
343 if (kvm_is_radix(vcpu
->kvm
))
344 return kvmppc_mmu_radix_xlate(vcpu
, eaddr
, gpte
, data
, iswrite
);
348 slbe
= kvmppc_mmu_book3s_hv_find_slbe(vcpu
, eaddr
);
353 /* real mode access */
354 slb_v
= vcpu
->kvm
->arch
.vrma_slb_v
;
358 /* Find the HPTE in the hash table */
359 index
= kvmppc_hv_find_lock_hpte(kvm
, eaddr
, slb_v
,
360 HPTE_V_VALID
| HPTE_V_ABSENT
);
365 hptep
= (__be64
*)(kvm
->arch
.hpt
.virt
+ (index
<< 4));
366 v
= orig_v
= be64_to_cpu(hptep
[0]) & ~HPTE_V_HVLOCK
;
367 if (cpu_has_feature(CPU_FTR_ARCH_300
))
368 v
= hpte_new_to_old_v(v
, be64_to_cpu(hptep
[1]));
369 gr
= kvm
->arch
.hpt
.rev
[index
].guest_rpte
;
371 unlock_hpte(hptep
, orig_v
);
375 gpte
->vpage
= ((v
& HPTE_V_AVPN
) << 4) | ((eaddr
>> 12) & 0xfff);
377 /* Get PP bits and key for permission check */
378 pp
= gr
& (HPTE_R_PP0
| HPTE_R_PP
);
379 key
= (vcpu
->arch
.shregs
.msr
& MSR_PR
) ? SLB_VSID_KP
: SLB_VSID_KS
;
382 /* Calculate permissions */
383 gpte
->may_read
= hpte_read_permission(pp
, key
);
384 gpte
->may_write
= hpte_write_permission(pp
, key
);
385 gpte
->may_execute
= gpte
->may_read
&& !(gr
& (HPTE_R_N
| HPTE_R_G
));
387 /* Storage key permission check for POWER7 */
388 if (data
&& virtmode
) {
389 int amrfield
= hpte_get_skey_perm(gr
, vcpu
->arch
.amr
);
396 /* Get the guest physical address */
397 gpte
->raddr
= kvmppc_mmu_get_real_addr(v
, gr
, eaddr
);
402 * Quick test for whether an instruction is a load or a store.
403 * If the instruction is a load or a store, then this will indicate
404 * which it is, at least on server processors. (Embedded processors
405 * have some external PID instructions that don't follow the rule
406 * embodied here.) If the instruction isn't a load or store, then
407 * this doesn't return anything useful.
409 static int instruction_is_store(unsigned int instr
)
414 if ((instr
& 0xfc000000) == 0x7c000000)
415 mask
= 0x100; /* major opcode 31 */
416 return (instr
& mask
) != 0;
419 int kvmppc_hv_emulate_mmio(struct kvm_vcpu
*vcpu
,
420 unsigned long gpa
, gva_t ea
, int is_store
)
425 * Fast path - check if the guest physical address corresponds to a
426 * device on the FAST_MMIO_BUS, if so we can avoid loading the
427 * instruction all together, then we can just handle it and return.
432 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
433 ret
= kvm_io_bus_write(vcpu
, KVM_FAST_MMIO_BUS
, (gpa_t
) gpa
, 0,
435 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
437 kvmppc_set_pc(vcpu
, kvmppc_get_pc(vcpu
) + 4);
443 * If we fail, we just return to the guest and try executing it again.
445 if (kvmppc_get_last_inst(vcpu
, INST_GENERIC
, &last_inst
) !=
450 * WARNING: We do not know for sure whether the instruction we just
451 * read from memory is the same that caused the fault in the first
452 * place. If the instruction we read is neither an load or a store,
453 * then it can't access memory, so we don't need to worry about
454 * enforcing access permissions. So, assuming it is a load or
455 * store, we just check that its direction (load or store) is
456 * consistent with the original fault, since that's what we
457 * checked the access permissions against. If there is a mismatch
458 * we just return and retry the instruction.
461 if (instruction_is_store(last_inst
) != !!is_store
)
465 * Emulated accesses are emulated by looking at the hash for
466 * translation once, then performing the access later. The
467 * translation could be invalidated in the meantime in which
468 * point performing the subsequent memory access on the old
469 * physical address could possibly be a security hole for the
470 * guest (but not the host).
472 * This is less of an issue for MMIO stores since they aren't
473 * globally visible. It could be an issue for MMIO loads to
474 * a certain extent but we'll ignore it for now.
477 vcpu
->arch
.paddr_accessed
= gpa
;
478 vcpu
->arch
.vaddr_accessed
= ea
;
479 return kvmppc_emulate_mmio(vcpu
);
482 int kvmppc_book3s_hv_page_fault(struct kvm_vcpu
*vcpu
,
483 unsigned long ea
, unsigned long dsisr
)
485 struct kvm
*kvm
= vcpu
->kvm
;
486 unsigned long hpte
[3], r
;
487 unsigned long hnow_v
, hnow_r
;
489 unsigned long mmu_seq
, psize
, pte_size
;
490 unsigned long gpa_base
, gfn_base
;
491 unsigned long gpa
, gfn
, hva
, pfn
, hpa
;
492 struct kvm_memory_slot
*memslot
;
494 struct revmap_entry
*rev
;
498 bool writing
, write_ok
;
500 unsigned long rcbits
;
504 if (kvm_is_radix(kvm
))
505 return kvmppc_book3s_radix_page_fault(vcpu
, ea
, dsisr
);
508 * Real-mode code has already searched the HPT and found the
509 * entry we're interested in. Lock the entry and check that
510 * it hasn't changed. If it has, just return and re-execute the
513 if (ea
!= vcpu
->arch
.pgfault_addr
)
516 if (vcpu
->arch
.pgfault_cache
) {
517 mmio_update
= atomic64_read(&kvm
->arch
.mmio_update
);
518 if (mmio_update
== vcpu
->arch
.pgfault_cache
->mmio_update
) {
519 r
= vcpu
->arch
.pgfault_cache
->rpte
;
520 psize
= kvmppc_actual_pgsz(vcpu
->arch
.pgfault_hpte
[0],
522 gpa_base
= r
& HPTE_R_RPN
& ~(psize
- 1);
523 gfn_base
= gpa_base
>> PAGE_SHIFT
;
524 gpa
= gpa_base
| (ea
& (psize
- 1));
525 return kvmppc_hv_emulate_mmio(vcpu
, gpa
, ea
,
526 dsisr
& DSISR_ISSTORE
);
529 index
= vcpu
->arch
.pgfault_index
;
530 hptep
= (__be64
*)(kvm
->arch
.hpt
.virt
+ (index
<< 4));
531 rev
= &kvm
->arch
.hpt
.rev
[index
];
533 while (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
))
535 hpte
[0] = be64_to_cpu(hptep
[0]) & ~HPTE_V_HVLOCK
;
536 hpte
[1] = be64_to_cpu(hptep
[1]);
537 hpte
[2] = r
= rev
->guest_rpte
;
538 unlock_hpte(hptep
, hpte
[0]);
541 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
542 hpte
[0] = hpte_new_to_old_v(hpte
[0], hpte
[1]);
543 hpte
[1] = hpte_new_to_old_r(hpte
[1]);
545 if (hpte
[0] != vcpu
->arch
.pgfault_hpte
[0] ||
546 hpte
[1] != vcpu
->arch
.pgfault_hpte
[1])
549 /* Translate the logical address and get the page */
550 psize
= kvmppc_actual_pgsz(hpte
[0], r
);
551 gpa_base
= r
& HPTE_R_RPN
& ~(psize
- 1);
552 gfn_base
= gpa_base
>> PAGE_SHIFT
;
553 gpa
= gpa_base
| (ea
& (psize
- 1));
554 gfn
= gpa
>> PAGE_SHIFT
;
555 memslot
= gfn_to_memslot(kvm
, gfn
);
557 trace_kvm_page_fault_enter(vcpu
, hpte
, memslot
, ea
, dsisr
);
559 /* No memslot means it's an emulated MMIO region */
560 if (!memslot
|| (memslot
->flags
& KVM_MEMSLOT_INVALID
))
561 return kvmppc_hv_emulate_mmio(vcpu
, gpa
, ea
,
562 dsisr
& DSISR_ISSTORE
);
565 * This should never happen, because of the slot_is_aligned()
566 * check in kvmppc_do_h_enter().
568 if (gfn_base
< memslot
->base_gfn
)
571 /* used to check for invalidations in progress */
572 mmu_seq
= kvm
->mmu_notifier_seq
;
577 writing
= (dsisr
& DSISR_ISSTORE
) != 0;
578 /* If writing != 0, then the HPTE must allow writing, if we get here */
580 hva
= gfn_to_hva_memslot(memslot
, gfn
);
583 * Do a fast check first, since __gfn_to_pfn_memslot doesn't
584 * do it with !atomic && !async, which is how we call it.
585 * We always ask for write permission since the common case
586 * is that the page is writable.
588 if (get_user_page_fast_only(hva
, FOLL_WRITE
, &page
)) {
591 /* Call KVM generic code to do the slow-path check */
592 pfn
= __gfn_to_pfn_memslot(memslot
, gfn
, false, NULL
,
594 if (is_error_noslot_pfn(pfn
))
597 if (pfn_valid(pfn
)) {
598 page
= pfn_to_page(pfn
);
599 if (PageReserved(page
))
605 * Read the PTE from the process' radix tree and use that
606 * so we get the shift and attribute bits.
608 spin_lock(&kvm
->mmu_lock
);
609 ptep
= find_kvm_host_pte(kvm
, mmu_seq
, hva
, &shift
);
612 pte
= READ_ONCE(*ptep
);
613 spin_unlock(&kvm
->mmu_lock
);
615 * If the PTE disappeared temporarily due to a THP
616 * collapse, just return and let the guest try again.
618 if (!pte_present(pte
)) {
623 hpa
= pte_pfn(pte
) << PAGE_SHIFT
;
624 pte_size
= PAGE_SIZE
;
626 pte_size
= 1ul << shift
;
629 if (psize
> pte_size
)
631 if (pte_size
> psize
)
632 hpa
|= hva
& (pte_size
- psize
);
634 /* Check WIMG vs. the actual page we're accessing */
635 if (!hpte_cache_flags_ok(r
, is_ci
)) {
639 * Allow guest to map emulated device memory as
640 * uncacheable, but actually make it cacheable.
642 r
= (r
& ~(HPTE_R_W
|HPTE_R_I
|HPTE_R_G
)) | HPTE_R_M
;
646 * Set the HPTE to point to hpa.
647 * Since the hpa is at PAGE_SIZE granularity, make sure we
648 * don't mask out lower-order bits if psize < PAGE_SIZE.
650 if (psize
< PAGE_SIZE
)
652 r
= (r
& HPTE_R_KEY_HI
) | (r
& ~(HPTE_R_PP0
- psize
)) | hpa
;
653 if (hpte_is_writable(r
) && !write_ok
)
654 r
= hpte_make_readonly(r
);
657 while (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
))
659 hnow_v
= be64_to_cpu(hptep
[0]);
660 hnow_r
= be64_to_cpu(hptep
[1]);
661 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
662 hnow_v
= hpte_new_to_old_v(hnow_v
, hnow_r
);
663 hnow_r
= hpte_new_to_old_r(hnow_r
);
667 * If the HPT is being resized, don't update the HPTE,
668 * instead let the guest retry after the resize operation is complete.
669 * The synchronization for mmu_ready test vs. set is provided
672 if (!kvm
->arch
.mmu_ready
)
675 if ((hnow_v
& ~HPTE_V_HVLOCK
) != hpte
[0] || hnow_r
!= hpte
[1] ||
676 rev
->guest_rpte
!= hpte
[2])
677 /* HPTE has been changed under us; let the guest retry */
679 hpte
[0] = (hpte
[0] & ~HPTE_V_ABSENT
) | HPTE_V_VALID
;
681 /* Always put the HPTE in the rmap chain for the page base address */
682 rmap
= &memslot
->arch
.rmap
[gfn_base
- memslot
->base_gfn
];
685 /* Check if we might have been invalidated; let the guest retry if so */
687 if (mmu_notifier_retry(vcpu
->kvm
, mmu_seq
)) {
692 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
693 rcbits
= *rmap
>> KVMPPC_RMAP_RC_SHIFT
;
694 r
&= rcbits
| ~(HPTE_R_R
| HPTE_R_C
);
696 if (be64_to_cpu(hptep
[0]) & HPTE_V_VALID
) {
697 /* HPTE was previously valid, so we need to invalidate it */
699 hptep
[0] |= cpu_to_be64(HPTE_V_ABSENT
);
700 kvmppc_invalidate_hpte(kvm
, hptep
, index
);
701 /* don't lose previous R and C bits */
702 r
|= be64_to_cpu(hptep
[1]) & (HPTE_R_R
| HPTE_R_C
);
704 kvmppc_add_revmap_chain(kvm
, rev
, rmap
, index
, 0);
707 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
708 r
= hpte_old_to_new_r(hpte
[0], r
);
709 hpte
[0] = hpte_old_to_new_v(hpte
[0]);
711 hptep
[1] = cpu_to_be64(r
);
713 __unlock_hpte(hptep
, hpte
[0]);
714 asm volatile("ptesync" : : : "memory");
716 if (page
&& hpte_is_writable(r
))
717 set_page_dirty_lock(page
);
720 trace_kvm_page_fault_exit(vcpu
, hpte
, ret
);
727 __unlock_hpte(hptep
, be64_to_cpu(hptep
[0]));
732 void kvmppc_rmap_reset(struct kvm
*kvm
)
734 struct kvm_memslots
*slots
;
735 struct kvm_memory_slot
*memslot
;
738 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
739 slots
= kvm_memslots(kvm
);
740 kvm_for_each_memslot(memslot
, slots
) {
741 /* Mutual exclusion with kvm_unmap_hva_range etc. */
742 spin_lock(&kvm
->mmu_lock
);
744 * This assumes it is acceptable to lose reference and
745 * change bits across a reset.
747 memset(memslot
->arch
.rmap
, 0,
748 memslot
->npages
* sizeof(*memslot
->arch
.rmap
));
749 spin_unlock(&kvm
->mmu_lock
);
751 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
754 typedef int (*hva_handler_fn
)(struct kvm
*kvm
, struct kvm_memory_slot
*memslot
,
757 static int kvm_handle_hva_range(struct kvm
*kvm
,
760 hva_handler_fn handler
)
764 struct kvm_memslots
*slots
;
765 struct kvm_memory_slot
*memslot
;
767 slots
= kvm_memslots(kvm
);
768 kvm_for_each_memslot(memslot
, slots
) {
769 unsigned long hva_start
, hva_end
;
772 hva_start
= max(start
, memslot
->userspace_addr
);
773 hva_end
= min(end
, memslot
->userspace_addr
+
774 (memslot
->npages
<< PAGE_SHIFT
));
775 if (hva_start
>= hva_end
)
778 * {gfn(page) | page intersects with [hva_start, hva_end)} =
779 * {gfn, gfn+1, ..., gfn_end-1}.
781 gfn
= hva_to_gfn_memslot(hva_start
, memslot
);
782 gfn_end
= hva_to_gfn_memslot(hva_end
+ PAGE_SIZE
- 1, memslot
);
784 for (; gfn
< gfn_end
; ++gfn
) {
785 ret
= handler(kvm
, memslot
, gfn
);
793 static int kvm_handle_hva(struct kvm
*kvm
, unsigned long hva
,
794 hva_handler_fn handler
)
796 return kvm_handle_hva_range(kvm
, hva
, hva
+ 1, handler
);
799 /* Must be called with both HPTE and rmap locked */
800 static void kvmppc_unmap_hpte(struct kvm
*kvm
, unsigned long i
,
801 struct kvm_memory_slot
*memslot
,
802 unsigned long *rmapp
, unsigned long gfn
)
804 __be64
*hptep
= (__be64
*) (kvm
->arch
.hpt
.virt
+ (i
<< 4));
805 struct revmap_entry
*rev
= kvm
->arch
.hpt
.rev
;
807 unsigned long ptel
, psize
, rcbits
;
811 /* chain is now empty */
812 *rmapp
&= ~(KVMPPC_RMAP_PRESENT
| KVMPPC_RMAP_INDEX
);
814 /* remove i from chain */
818 rev
[i
].forw
= rev
[i
].back
= i
;
819 *rmapp
= (*rmapp
& ~KVMPPC_RMAP_INDEX
) | j
;
822 /* Now check and modify the HPTE */
823 ptel
= rev
[i
].guest_rpte
;
824 psize
= kvmppc_actual_pgsz(be64_to_cpu(hptep
[0]), ptel
);
825 if ((be64_to_cpu(hptep
[0]) & HPTE_V_VALID
) &&
826 hpte_rpn(ptel
, psize
) == gfn
) {
827 hptep
[0] |= cpu_to_be64(HPTE_V_ABSENT
);
828 kvmppc_invalidate_hpte(kvm
, hptep
, i
);
829 hptep
[1] &= ~cpu_to_be64(HPTE_R_KEY_HI
| HPTE_R_KEY_LO
);
830 /* Harvest R and C */
831 rcbits
= be64_to_cpu(hptep
[1]) & (HPTE_R_R
| HPTE_R_C
);
832 *rmapp
|= rcbits
<< KVMPPC_RMAP_RC_SHIFT
;
833 if ((rcbits
& HPTE_R_C
) && memslot
->dirty_bitmap
)
834 kvmppc_update_dirty_map(memslot
, gfn
, psize
);
835 if (rcbits
& ~rev
[i
].guest_rpte
) {
836 rev
[i
].guest_rpte
= ptel
| rcbits
;
837 note_hpte_modification(kvm
, &rev
[i
]);
842 static int kvm_unmap_rmapp(struct kvm
*kvm
, struct kvm_memory_slot
*memslot
,
847 unsigned long *rmapp
;
849 rmapp
= &memslot
->arch
.rmap
[gfn
- memslot
->base_gfn
];
852 if (!(*rmapp
& KVMPPC_RMAP_PRESENT
)) {
858 * To avoid an ABBA deadlock with the HPTE lock bit,
859 * we can't spin on the HPTE lock while holding the
862 i
= *rmapp
& KVMPPC_RMAP_INDEX
;
863 hptep
= (__be64
*) (kvm
->arch
.hpt
.virt
+ (i
<< 4));
864 if (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
)) {
865 /* unlock rmap before spinning on the HPTE lock */
867 while (be64_to_cpu(hptep
[0]) & HPTE_V_HVLOCK
)
872 kvmppc_unmap_hpte(kvm
, i
, memslot
, rmapp
, gfn
);
874 __unlock_hpte(hptep
, be64_to_cpu(hptep
[0]));
879 int kvm_unmap_hva_range_hv(struct kvm
*kvm
, unsigned long start
, unsigned long end
)
881 hva_handler_fn handler
;
883 handler
= kvm_is_radix(kvm
) ? kvm_unmap_radix
: kvm_unmap_rmapp
;
884 kvm_handle_hva_range(kvm
, start
, end
, handler
);
888 void kvmppc_core_flush_memslot_hv(struct kvm
*kvm
,
889 struct kvm_memory_slot
*memslot
)
893 unsigned long *rmapp
;
895 gfn
= memslot
->base_gfn
;
896 rmapp
= memslot
->arch
.rmap
;
897 if (kvm_is_radix(kvm
)) {
898 kvmppc_radix_flush_memslot(kvm
, memslot
);
902 for (n
= memslot
->npages
; n
; --n
, ++gfn
) {
904 * Testing the present bit without locking is OK because
905 * the memslot has been marked invalid already, and hence
906 * no new HPTEs referencing this page can be created,
907 * thus the present bit can't go from 0 to 1.
909 if (*rmapp
& KVMPPC_RMAP_PRESENT
)
910 kvm_unmap_rmapp(kvm
, memslot
, gfn
);
915 static int kvm_age_rmapp(struct kvm
*kvm
, struct kvm_memory_slot
*memslot
,
918 struct revmap_entry
*rev
= kvm
->arch
.hpt
.rev
;
919 unsigned long head
, i
, j
;
922 unsigned long *rmapp
;
924 rmapp
= &memslot
->arch
.rmap
[gfn
- memslot
->base_gfn
];
927 if (*rmapp
& KVMPPC_RMAP_REFERENCED
) {
928 *rmapp
&= ~KVMPPC_RMAP_REFERENCED
;
931 if (!(*rmapp
& KVMPPC_RMAP_PRESENT
)) {
936 i
= head
= *rmapp
& KVMPPC_RMAP_INDEX
;
938 hptep
= (__be64
*) (kvm
->arch
.hpt
.virt
+ (i
<< 4));
941 /* If this HPTE isn't referenced, ignore it */
942 if (!(be64_to_cpu(hptep
[1]) & HPTE_R_R
))
945 if (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
)) {
946 /* unlock rmap before spinning on the HPTE lock */
948 while (be64_to_cpu(hptep
[0]) & HPTE_V_HVLOCK
)
953 /* Now check and modify the HPTE */
954 if ((be64_to_cpu(hptep
[0]) & HPTE_V_VALID
) &&
955 (be64_to_cpu(hptep
[1]) & HPTE_R_R
)) {
956 kvmppc_clear_ref_hpte(kvm
, hptep
, i
);
957 if (!(rev
[i
].guest_rpte
& HPTE_R_R
)) {
958 rev
[i
].guest_rpte
|= HPTE_R_R
;
959 note_hpte_modification(kvm
, &rev
[i
]);
963 __unlock_hpte(hptep
, be64_to_cpu(hptep
[0]));
964 } while ((i
= j
) != head
);
970 int kvm_age_hva_hv(struct kvm
*kvm
, unsigned long start
, unsigned long end
)
972 hva_handler_fn handler
;
974 handler
= kvm_is_radix(kvm
) ? kvm_age_radix
: kvm_age_rmapp
;
975 return kvm_handle_hva_range(kvm
, start
, end
, handler
);
978 static int kvm_test_age_rmapp(struct kvm
*kvm
, struct kvm_memory_slot
*memslot
,
981 struct revmap_entry
*rev
= kvm
->arch
.hpt
.rev
;
982 unsigned long head
, i
, j
;
985 unsigned long *rmapp
;
987 rmapp
= &memslot
->arch
.rmap
[gfn
- memslot
->base_gfn
];
988 if (*rmapp
& KVMPPC_RMAP_REFERENCED
)
992 if (*rmapp
& KVMPPC_RMAP_REFERENCED
)
995 if (*rmapp
& KVMPPC_RMAP_PRESENT
) {
996 i
= head
= *rmapp
& KVMPPC_RMAP_INDEX
;
998 hp
= (unsigned long *)(kvm
->arch
.hpt
.virt
+ (i
<< 4));
1000 if (be64_to_cpu(hp
[1]) & HPTE_R_R
)
1002 } while ((i
= j
) != head
);
1011 int kvm_test_age_hva_hv(struct kvm
*kvm
, unsigned long hva
)
1013 hva_handler_fn handler
;
1015 handler
= kvm_is_radix(kvm
) ? kvm_test_age_radix
: kvm_test_age_rmapp
;
1016 return kvm_handle_hva(kvm
, hva
, handler
);
1019 void kvm_set_spte_hva_hv(struct kvm
*kvm
, unsigned long hva
, pte_t pte
)
1021 hva_handler_fn handler
;
1023 handler
= kvm_is_radix(kvm
) ? kvm_unmap_radix
: kvm_unmap_rmapp
;
1024 kvm_handle_hva(kvm
, hva
, handler
);
1027 static int vcpus_running(struct kvm
*kvm
)
1029 return atomic_read(&kvm
->arch
.vcpus_running
) != 0;
1033 * Returns the number of system pages that are dirty.
1034 * This can be more than 1 if we find a huge-page HPTE.
1036 static int kvm_test_clear_dirty_npages(struct kvm
*kvm
, unsigned long *rmapp
)
1038 struct revmap_entry
*rev
= kvm
->arch
.hpt
.rev
;
1039 unsigned long head
, i
, j
;
1043 int npages_dirty
= 0;
1047 if (!(*rmapp
& KVMPPC_RMAP_PRESENT
)) {
1049 return npages_dirty
;
1052 i
= head
= *rmapp
& KVMPPC_RMAP_INDEX
;
1054 unsigned long hptep1
;
1055 hptep
= (__be64
*) (kvm
->arch
.hpt
.virt
+ (i
<< 4));
1059 * Checking the C (changed) bit here is racy since there
1060 * is no guarantee about when the hardware writes it back.
1061 * If the HPTE is not writable then it is stable since the
1062 * page can't be written to, and we would have done a tlbie
1063 * (which forces the hardware to complete any writeback)
1064 * when making the HPTE read-only.
1065 * If vcpus are running then this call is racy anyway
1066 * since the page could get dirtied subsequently, so we
1067 * expect there to be a further call which would pick up
1068 * any delayed C bit writeback.
1069 * Otherwise we need to do the tlbie even if C==0 in
1070 * order to pick up any delayed writeback of C.
1072 hptep1
= be64_to_cpu(hptep
[1]);
1073 if (!(hptep1
& HPTE_R_C
) &&
1074 (!hpte_is_writable(hptep1
) || vcpus_running(kvm
)))
1077 if (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
)) {
1078 /* unlock rmap before spinning on the HPTE lock */
1080 while (hptep
[0] & cpu_to_be64(HPTE_V_HVLOCK
))
1085 /* Now check and modify the HPTE */
1086 if (!(hptep
[0] & cpu_to_be64(HPTE_V_VALID
))) {
1087 __unlock_hpte(hptep
, be64_to_cpu(hptep
[0]));
1091 /* need to make it temporarily absent so C is stable */
1092 hptep
[0] |= cpu_to_be64(HPTE_V_ABSENT
);
1093 kvmppc_invalidate_hpte(kvm
, hptep
, i
);
1094 v
= be64_to_cpu(hptep
[0]);
1095 r
= be64_to_cpu(hptep
[1]);
1097 hptep
[1] = cpu_to_be64(r
& ~HPTE_R_C
);
1098 if (!(rev
[i
].guest_rpte
& HPTE_R_C
)) {
1099 rev
[i
].guest_rpte
|= HPTE_R_C
;
1100 note_hpte_modification(kvm
, &rev
[i
]);
1102 n
= kvmppc_actual_pgsz(v
, r
);
1103 n
= (n
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1104 if (n
> npages_dirty
)
1108 v
&= ~HPTE_V_ABSENT
;
1110 __unlock_hpte(hptep
, v
);
1111 } while ((i
= j
) != head
);
1114 return npages_dirty
;
1117 void kvmppc_harvest_vpa_dirty(struct kvmppc_vpa
*vpa
,
1118 struct kvm_memory_slot
*memslot
,
1123 if (!vpa
->dirty
|| !vpa
->pinned_addr
)
1125 gfn
= vpa
->gpa
>> PAGE_SHIFT
;
1126 if (gfn
< memslot
->base_gfn
||
1127 gfn
>= memslot
->base_gfn
+ memslot
->npages
)
1132 __set_bit_le(gfn
- memslot
->base_gfn
, map
);
1135 long kvmppc_hv_get_dirty_log_hpt(struct kvm
*kvm
,
1136 struct kvm_memory_slot
*memslot
, unsigned long *map
)
1139 unsigned long *rmapp
;
1142 rmapp
= memslot
->arch
.rmap
;
1143 for (i
= 0; i
< memslot
->npages
; ++i
) {
1144 int npages
= kvm_test_clear_dirty_npages(kvm
, rmapp
);
1146 * Note that if npages > 0 then i must be a multiple of npages,
1147 * since we always put huge-page HPTEs in the rmap chain
1148 * corresponding to their page base address.
1151 set_dirty_bits(map
, i
, npages
);
1158 void *kvmppc_pin_guest_page(struct kvm
*kvm
, unsigned long gpa
,
1159 unsigned long *nb_ret
)
1161 struct kvm_memory_slot
*memslot
;
1162 unsigned long gfn
= gpa
>> PAGE_SHIFT
;
1163 struct page
*page
, *pages
[1];
1165 unsigned long hva
, offset
;
1168 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1169 memslot
= gfn_to_memslot(kvm
, gfn
);
1170 if (!memslot
|| (memslot
->flags
& KVM_MEMSLOT_INVALID
))
1172 hva
= gfn_to_hva_memslot(memslot
, gfn
);
1173 npages
= get_user_pages_fast(hva
, 1, FOLL_WRITE
, pages
);
1177 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1179 offset
= gpa
& (PAGE_SIZE
- 1);
1181 *nb_ret
= PAGE_SIZE
- offset
;
1182 return page_address(page
) + offset
;
1185 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1189 void kvmppc_unpin_guest_page(struct kvm
*kvm
, void *va
, unsigned long gpa
,
1192 struct page
*page
= virt_to_page(va
);
1193 struct kvm_memory_slot
*memslot
;
1202 /* We need to mark this page dirty in the memslot dirty_bitmap, if any */
1203 gfn
= gpa
>> PAGE_SHIFT
;
1204 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1205 memslot
= gfn_to_memslot(kvm
, gfn
);
1206 if (memslot
&& memslot
->dirty_bitmap
)
1207 set_bit_le(gfn
- memslot
->base_gfn
, memslot
->dirty_bitmap
);
1208 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1214 static int resize_hpt_allocate(struct kvm_resize_hpt
*resize
)
1218 rc
= kvmppc_allocate_hpt(&resize
->hpt
, resize
->order
);
1222 resize_hpt_debug(resize
, "resize_hpt_allocate(): HPT @ 0x%lx\n",
1228 static unsigned long resize_hpt_rehash_hpte(struct kvm_resize_hpt
*resize
,
1231 struct kvm
*kvm
= resize
->kvm
;
1232 struct kvm_hpt_info
*old
= &kvm
->arch
.hpt
;
1233 struct kvm_hpt_info
*new = &resize
->hpt
;
1234 unsigned long old_hash_mask
= (1ULL << (old
->order
- 7)) - 1;
1235 unsigned long new_hash_mask
= (1ULL << (new->order
- 7)) - 1;
1236 __be64
*hptep
, *new_hptep
;
1237 unsigned long vpte
, rpte
, guest_rpte
;
1239 struct revmap_entry
*rev
;
1240 unsigned long apsize
, avpn
, pteg
, hash
;
1241 unsigned long new_idx
, new_pteg
, replace_vpte
;
1244 hptep
= (__be64
*)(old
->virt
+ (idx
<< 4));
1246 /* Guest is stopped, so new HPTEs can't be added or faulted
1247 * in, only unmapped or altered by host actions. So, it's
1248 * safe to check this before we take the HPTE lock */
1249 vpte
= be64_to_cpu(hptep
[0]);
1250 if (!(vpte
& HPTE_V_VALID
) && !(vpte
& HPTE_V_ABSENT
))
1251 return 0; /* nothing to do */
1253 while (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
))
1256 vpte
= be64_to_cpu(hptep
[0]);
1259 if (!(vpte
& HPTE_V_VALID
) && !(vpte
& HPTE_V_ABSENT
))
1263 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
1264 rpte
= be64_to_cpu(hptep
[1]);
1265 vpte
= hpte_new_to_old_v(vpte
, rpte
);
1269 rev
= &old
->rev
[idx
];
1270 guest_rpte
= rev
->guest_rpte
;
1273 apsize
= kvmppc_actual_pgsz(vpte
, guest_rpte
);
1277 if (vpte
& HPTE_V_VALID
) {
1278 unsigned long gfn
= hpte_rpn(guest_rpte
, apsize
);
1279 int srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1280 struct kvm_memory_slot
*memslot
=
1281 __gfn_to_memslot(kvm_memslots(kvm
), gfn
);
1284 unsigned long *rmapp
;
1285 rmapp
= &memslot
->arch
.rmap
[gfn
- memslot
->base_gfn
];
1288 kvmppc_unmap_hpte(kvm
, idx
, memslot
, rmapp
, gfn
);
1292 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1295 /* Reload PTE after unmap */
1296 vpte
= be64_to_cpu(hptep
[0]);
1297 BUG_ON(vpte
& HPTE_V_VALID
);
1298 BUG_ON(!(vpte
& HPTE_V_ABSENT
));
1301 if (!(vpte
& HPTE_V_BOLTED
))
1304 rpte
= be64_to_cpu(hptep
[1]);
1306 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
1307 vpte
= hpte_new_to_old_v(vpte
, rpte
);
1308 rpte
= hpte_new_to_old_r(rpte
);
1311 pshift
= kvmppc_hpte_base_page_shift(vpte
, rpte
);
1312 avpn
= HPTE_V_AVPN_VAL(vpte
) & ~(((1ul << pshift
) - 1) >> 23);
1313 pteg
= idx
/ HPTES_PER_GROUP
;
1314 if (vpte
& HPTE_V_SECONDARY
)
1317 if (!(vpte
& HPTE_V_1TB_SEG
)) {
1318 unsigned long offset
, vsid
;
1320 /* We only have 28 - 23 bits of offset in avpn */
1321 offset
= (avpn
& 0x1f) << 23;
1323 /* We can find more bits from the pteg value */
1325 offset
|= ((vsid
^ pteg
) & old_hash_mask
) << pshift
;
1327 hash
= vsid
^ (offset
>> pshift
);
1329 unsigned long offset
, vsid
;
1331 /* We only have 40 - 23 bits of seg_off in avpn */
1332 offset
= (avpn
& 0x1ffff) << 23;
1335 offset
|= ((vsid
^ (vsid
<< 25) ^ pteg
) & old_hash_mask
) << pshift
;
1337 hash
= vsid
^ (vsid
<< 25) ^ (offset
>> pshift
);
1340 new_pteg
= hash
& new_hash_mask
;
1341 if (vpte
& HPTE_V_SECONDARY
)
1342 new_pteg
= ~hash
& new_hash_mask
;
1344 new_idx
= new_pteg
* HPTES_PER_GROUP
+ (idx
% HPTES_PER_GROUP
);
1345 new_hptep
= (__be64
*)(new->virt
+ (new_idx
<< 4));
1347 replace_vpte
= be64_to_cpu(new_hptep
[0]);
1348 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
1349 unsigned long replace_rpte
= be64_to_cpu(new_hptep
[1]);
1350 replace_vpte
= hpte_new_to_old_v(replace_vpte
, replace_rpte
);
1353 if (replace_vpte
& (HPTE_V_VALID
| HPTE_V_ABSENT
)) {
1354 BUG_ON(new->order
>= old
->order
);
1356 if (replace_vpte
& HPTE_V_BOLTED
) {
1357 if (vpte
& HPTE_V_BOLTED
)
1358 /* Bolted collision, nothing we can do */
1360 /* Discard the new HPTE */
1364 /* Discard the previous HPTE */
1367 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
1368 rpte
= hpte_old_to_new_r(vpte
, rpte
);
1369 vpte
= hpte_old_to_new_v(vpte
);
1372 new_hptep
[1] = cpu_to_be64(rpte
);
1373 new->rev
[new_idx
].guest_rpte
= guest_rpte
;
1374 /* No need for a barrier, since new HPT isn't active */
1375 new_hptep
[0] = cpu_to_be64(vpte
);
1376 unlock_hpte(new_hptep
, vpte
);
1379 unlock_hpte(hptep
, vpte
);
1383 static int resize_hpt_rehash(struct kvm_resize_hpt
*resize
)
1385 struct kvm
*kvm
= resize
->kvm
;
1389 for (i
= 0; i
< kvmppc_hpt_npte(&kvm
->arch
.hpt
); i
++) {
1390 rc
= resize_hpt_rehash_hpte(resize
, i
);
1398 static void resize_hpt_pivot(struct kvm_resize_hpt
*resize
)
1400 struct kvm
*kvm
= resize
->kvm
;
1401 struct kvm_hpt_info hpt_tmp
;
1403 /* Exchange the pending tables in the resize structure with
1404 * the active tables */
1406 resize_hpt_debug(resize
, "resize_hpt_pivot()\n");
1408 spin_lock(&kvm
->mmu_lock
);
1409 asm volatile("ptesync" : : : "memory");
1411 hpt_tmp
= kvm
->arch
.hpt
;
1412 kvmppc_set_hpt(kvm
, &resize
->hpt
);
1413 resize
->hpt
= hpt_tmp
;
1415 spin_unlock(&kvm
->mmu_lock
);
1417 synchronize_srcu_expedited(&kvm
->srcu
);
1419 if (cpu_has_feature(CPU_FTR_ARCH_300
))
1420 kvmppc_setup_partition_table(kvm
);
1422 resize_hpt_debug(resize
, "resize_hpt_pivot() done\n");
1425 static void resize_hpt_release(struct kvm
*kvm
, struct kvm_resize_hpt
*resize
)
1427 if (WARN_ON(!mutex_is_locked(&kvm
->arch
.mmu_setup_lock
)))
1433 if (resize
->error
!= -EBUSY
) {
1434 if (resize
->hpt
.virt
)
1435 kvmppc_free_hpt(&resize
->hpt
);
1439 if (kvm
->arch
.resize_hpt
== resize
)
1440 kvm
->arch
.resize_hpt
= NULL
;
1443 static void resize_hpt_prepare_work(struct work_struct
*work
)
1445 struct kvm_resize_hpt
*resize
= container_of(work
,
1446 struct kvm_resize_hpt
,
1448 struct kvm
*kvm
= resize
->kvm
;
1451 if (WARN_ON(resize
->error
!= -EBUSY
))
1454 mutex_lock(&kvm
->arch
.mmu_setup_lock
);
1456 /* Request is still current? */
1457 if (kvm
->arch
.resize_hpt
== resize
) {
1458 /* We may request large allocations here:
1459 * do not sleep with kvm->arch.mmu_setup_lock held for a while.
1461 mutex_unlock(&kvm
->arch
.mmu_setup_lock
);
1463 resize_hpt_debug(resize
, "resize_hpt_prepare_work(): order = %d\n",
1466 err
= resize_hpt_allocate(resize
);
1468 /* We have strict assumption about -EBUSY
1469 * when preparing for HPT resize.
1471 if (WARN_ON(err
== -EBUSY
))
1474 mutex_lock(&kvm
->arch
.mmu_setup_lock
);
1475 /* It is possible that kvm->arch.resize_hpt != resize
1476 * after we grab kvm->arch.mmu_setup_lock again.
1480 resize
->error
= err
;
1482 if (kvm
->arch
.resize_hpt
!= resize
)
1483 resize_hpt_release(kvm
, resize
);
1485 mutex_unlock(&kvm
->arch
.mmu_setup_lock
);
1488 long kvm_vm_ioctl_resize_hpt_prepare(struct kvm
*kvm
,
1489 struct kvm_ppc_resize_hpt
*rhpt
)
1491 unsigned long flags
= rhpt
->flags
;
1492 unsigned long shift
= rhpt
->shift
;
1493 struct kvm_resize_hpt
*resize
;
1496 if (flags
!= 0 || kvm_is_radix(kvm
))
1499 if (shift
&& ((shift
< 18) || (shift
> 46)))
1502 mutex_lock(&kvm
->arch
.mmu_setup_lock
);
1504 resize
= kvm
->arch
.resize_hpt
;
1507 if (resize
->order
== shift
) {
1508 /* Suitable resize in progress? */
1509 ret
= resize
->error
;
1511 ret
= 100; /* estimated time in ms */
1513 resize_hpt_release(kvm
, resize
);
1518 /* not suitable, cancel it */
1519 resize_hpt_release(kvm
, resize
);
1524 goto out
; /* nothing to do */
1526 /* start new resize */
1528 resize
= kzalloc(sizeof(*resize
), GFP_KERNEL
);
1534 resize
->error
= -EBUSY
;
1535 resize
->order
= shift
;
1537 INIT_WORK(&resize
->work
, resize_hpt_prepare_work
);
1538 kvm
->arch
.resize_hpt
= resize
;
1540 schedule_work(&resize
->work
);
1542 ret
= 100; /* estimated time in ms */
1545 mutex_unlock(&kvm
->arch
.mmu_setup_lock
);
1549 static void resize_hpt_boot_vcpu(void *opaque
)
1551 /* Nothing to do, just force a KVM exit */
1554 long kvm_vm_ioctl_resize_hpt_commit(struct kvm
*kvm
,
1555 struct kvm_ppc_resize_hpt
*rhpt
)
1557 unsigned long flags
= rhpt
->flags
;
1558 unsigned long shift
= rhpt
->shift
;
1559 struct kvm_resize_hpt
*resize
;
1562 if (flags
!= 0 || kvm_is_radix(kvm
))
1565 if (shift
&& ((shift
< 18) || (shift
> 46)))
1568 mutex_lock(&kvm
->arch
.mmu_setup_lock
);
1570 resize
= kvm
->arch
.resize_hpt
;
1572 /* This shouldn't be possible */
1574 if (WARN_ON(!kvm
->arch
.mmu_ready
))
1577 /* Stop VCPUs from running while we mess with the HPT */
1578 kvm
->arch
.mmu_ready
= 0;
1581 /* Boot all CPUs out of the guest so they re-read
1583 on_each_cpu(resize_hpt_boot_vcpu
, NULL
, 1);
1586 if (!resize
|| (resize
->order
!= shift
))
1589 ret
= resize
->error
;
1593 ret
= resize_hpt_rehash(resize
);
1597 resize_hpt_pivot(resize
);
1600 /* Let VCPUs run again */
1601 kvm
->arch
.mmu_ready
= 1;
1604 resize_hpt_release(kvm
, resize
);
1605 mutex_unlock(&kvm
->arch
.mmu_setup_lock
);
1610 * Functions for reading and writing the hash table via reads and
1611 * writes on a file descriptor.
1613 * Reads return the guest view of the hash table, which has to be
1614 * pieced together from the real hash table and the guest_rpte
1615 * values in the revmap array.
1617 * On writes, each HPTE written is considered in turn, and if it
1618 * is valid, it is written to the HPT as if an H_ENTER with the
1619 * exact flag set was done. When the invalid count is non-zero
1620 * in the header written to the stream, the kernel will make
1621 * sure that that many HPTEs are invalid, and invalidate them
1625 struct kvm_htab_ctx
{
1626 unsigned long index
;
1627 unsigned long flags
;
1632 #define HPTE_SIZE (2 * sizeof(unsigned long))
1635 * Returns 1 if this HPT entry has been modified or has pending
1638 static int hpte_dirty(struct revmap_entry
*revp
, __be64
*hptp
)
1640 unsigned long rcbits_unset
;
1642 if (revp
->guest_rpte
& HPTE_GR_MODIFIED
)
1645 /* Also need to consider changes in reference and changed bits */
1646 rcbits_unset
= ~revp
->guest_rpte
& (HPTE_R_R
| HPTE_R_C
);
1647 if ((be64_to_cpu(hptp
[0]) & HPTE_V_VALID
) &&
1648 (be64_to_cpu(hptp
[1]) & rcbits_unset
))
1654 static long record_hpte(unsigned long flags
, __be64
*hptp
,
1655 unsigned long *hpte
, struct revmap_entry
*revp
,
1656 int want_valid
, int first_pass
)
1658 unsigned long v
, r
, hr
;
1659 unsigned long rcbits_unset
;
1663 /* Unmodified entries are uninteresting except on the first pass */
1664 dirty
= hpte_dirty(revp
, hptp
);
1665 if (!first_pass
&& !dirty
)
1669 if (be64_to_cpu(hptp
[0]) & (HPTE_V_VALID
| HPTE_V_ABSENT
)) {
1671 if ((flags
& KVM_GET_HTAB_BOLTED_ONLY
) &&
1672 !(be64_to_cpu(hptp
[0]) & HPTE_V_BOLTED
))
1675 if (valid
!= want_valid
)
1679 if (valid
|| dirty
) {
1680 /* lock the HPTE so it's stable and read it */
1682 while (!try_lock_hpte(hptp
, HPTE_V_HVLOCK
))
1684 v
= be64_to_cpu(hptp
[0]);
1685 hr
= be64_to_cpu(hptp
[1]);
1686 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
1687 v
= hpte_new_to_old_v(v
, hr
);
1688 hr
= hpte_new_to_old_r(hr
);
1691 /* re-evaluate valid and dirty from synchronized HPTE value */
1692 valid
= !!(v
& HPTE_V_VALID
);
1693 dirty
= !!(revp
->guest_rpte
& HPTE_GR_MODIFIED
);
1695 /* Harvest R and C into guest view if necessary */
1696 rcbits_unset
= ~revp
->guest_rpte
& (HPTE_R_R
| HPTE_R_C
);
1697 if (valid
&& (rcbits_unset
& hr
)) {
1698 revp
->guest_rpte
|= (hr
&
1699 (HPTE_R_R
| HPTE_R_C
)) | HPTE_GR_MODIFIED
;
1703 if (v
& HPTE_V_ABSENT
) {
1704 v
&= ~HPTE_V_ABSENT
;
1708 if ((flags
& KVM_GET_HTAB_BOLTED_ONLY
) && !(v
& HPTE_V_BOLTED
))
1711 r
= revp
->guest_rpte
;
1712 /* only clear modified if this is the right sort of entry */
1713 if (valid
== want_valid
&& dirty
) {
1714 r
&= ~HPTE_GR_MODIFIED
;
1715 revp
->guest_rpte
= r
;
1717 unlock_hpte(hptp
, be64_to_cpu(hptp
[0]));
1719 if (!(valid
== want_valid
&& (first_pass
|| dirty
)))
1722 hpte
[0] = cpu_to_be64(v
);
1723 hpte
[1] = cpu_to_be64(r
);
1727 static ssize_t
kvm_htab_read(struct file
*file
, char __user
*buf
,
1728 size_t count
, loff_t
*ppos
)
1730 struct kvm_htab_ctx
*ctx
= file
->private_data
;
1731 struct kvm
*kvm
= ctx
->kvm
;
1732 struct kvm_get_htab_header hdr
;
1734 struct revmap_entry
*revp
;
1735 unsigned long i
, nb
, nw
;
1736 unsigned long __user
*lbuf
;
1737 struct kvm_get_htab_header __user
*hptr
;
1738 unsigned long flags
;
1740 unsigned long hpte
[2];
1742 if (!access_ok(buf
, count
))
1744 if (kvm_is_radix(kvm
))
1747 first_pass
= ctx
->first_pass
;
1751 hptp
= (__be64
*)(kvm
->arch
.hpt
.virt
+ (i
* HPTE_SIZE
));
1752 revp
= kvm
->arch
.hpt
.rev
+ i
;
1753 lbuf
= (unsigned long __user
*)buf
;
1756 while (nb
+ sizeof(hdr
) + HPTE_SIZE
< count
) {
1757 /* Initialize header */
1758 hptr
= (struct kvm_get_htab_header __user
*)buf
;
1763 lbuf
= (unsigned long __user
*)(buf
+ sizeof(hdr
));
1765 /* Skip uninteresting entries, i.e. clean on not-first pass */
1767 while (i
< kvmppc_hpt_npte(&kvm
->arch
.hpt
) &&
1768 !hpte_dirty(revp
, hptp
)) {
1776 /* Grab a series of valid entries */
1777 while (i
< kvmppc_hpt_npte(&kvm
->arch
.hpt
) &&
1778 hdr
.n_valid
< 0xffff &&
1779 nb
+ HPTE_SIZE
< count
&&
1780 record_hpte(flags
, hptp
, hpte
, revp
, 1, first_pass
)) {
1781 /* valid entry, write it out */
1783 if (__put_user(hpte
[0], lbuf
) ||
1784 __put_user(hpte
[1], lbuf
+ 1))
1792 /* Now skip invalid entries while we can */
1793 while (i
< kvmppc_hpt_npte(&kvm
->arch
.hpt
) &&
1794 hdr
.n_invalid
< 0xffff &&
1795 record_hpte(flags
, hptp
, hpte
, revp
, 0, first_pass
)) {
1796 /* found an invalid entry */
1803 if (hdr
.n_valid
|| hdr
.n_invalid
) {
1804 /* write back the header */
1805 if (__copy_to_user(hptr
, &hdr
, sizeof(hdr
)))
1808 buf
= (char __user
*)lbuf
;
1813 /* Check if we've wrapped around the hash table */
1814 if (i
>= kvmppc_hpt_npte(&kvm
->arch
.hpt
)) {
1816 ctx
->first_pass
= 0;
1826 static ssize_t
kvm_htab_write(struct file
*file
, const char __user
*buf
,
1827 size_t count
, loff_t
*ppos
)
1829 struct kvm_htab_ctx
*ctx
= file
->private_data
;
1830 struct kvm
*kvm
= ctx
->kvm
;
1831 struct kvm_get_htab_header hdr
;
1834 unsigned long __user
*lbuf
;
1836 unsigned long tmp
[2];
1842 if (!access_ok(buf
, count
))
1844 if (kvm_is_radix(kvm
))
1847 /* lock out vcpus from running while we're doing this */
1848 mutex_lock(&kvm
->arch
.mmu_setup_lock
);
1849 mmu_ready
= kvm
->arch
.mmu_ready
;
1851 kvm
->arch
.mmu_ready
= 0; /* temporarily */
1852 /* order mmu_ready vs. vcpus_running */
1854 if (atomic_read(&kvm
->arch
.vcpus_running
)) {
1855 kvm
->arch
.mmu_ready
= 1;
1856 mutex_unlock(&kvm
->arch
.mmu_setup_lock
);
1862 for (nb
= 0; nb
+ sizeof(hdr
) <= count
; ) {
1864 if (__copy_from_user(&hdr
, buf
, sizeof(hdr
)))
1868 if (nb
+ hdr
.n_valid
* HPTE_SIZE
> count
)
1876 if (i
>= kvmppc_hpt_npte(&kvm
->arch
.hpt
) ||
1877 i
+ hdr
.n_valid
+ hdr
.n_invalid
> kvmppc_hpt_npte(&kvm
->arch
.hpt
))
1880 hptp
= (__be64
*)(kvm
->arch
.hpt
.virt
+ (i
* HPTE_SIZE
));
1881 lbuf
= (unsigned long __user
*)buf
;
1882 for (j
= 0; j
< hdr
.n_valid
; ++j
) {
1887 if (__get_user(hpte_v
, lbuf
) ||
1888 __get_user(hpte_r
, lbuf
+ 1))
1890 v
= be64_to_cpu(hpte_v
);
1891 r
= be64_to_cpu(hpte_r
);
1893 if (!(v
& HPTE_V_VALID
))
1895 pshift
= kvmppc_hpte_base_page_shift(v
, r
);
1901 if (be64_to_cpu(hptp
[0]) & (HPTE_V_VALID
| HPTE_V_ABSENT
))
1902 kvmppc_do_h_remove(kvm
, 0, i
, 0, tmp
);
1904 ret
= kvmppc_virtmode_do_h_enter(kvm
, H_EXACT
, i
, v
, r
,
1906 if (ret
!= H_SUCCESS
) {
1907 pr_err("kvm_htab_write ret %ld i=%ld v=%lx "
1908 "r=%lx\n", ret
, i
, v
, r
);
1911 if (!mmu_ready
&& is_vrma_hpte(v
)) {
1912 unsigned long senc
, lpcr
;
1914 senc
= slb_pgsize_encoding(1ul << pshift
);
1915 kvm
->arch
.vrma_slb_v
= senc
| SLB_VSID_B_1T
|
1916 (VRMA_VSID
<< SLB_VSID_SHIFT_1T
);
1917 if (!cpu_has_feature(CPU_FTR_ARCH_300
)) {
1918 lpcr
= senc
<< (LPCR_VRMASD_SH
- 4);
1919 kvmppc_update_lpcr(kvm
, lpcr
,
1922 kvmppc_setup_partition_table(kvm
);
1930 for (j
= 0; j
< hdr
.n_invalid
; ++j
) {
1931 if (be64_to_cpu(hptp
[0]) & (HPTE_V_VALID
| HPTE_V_ABSENT
))
1932 kvmppc_do_h_remove(kvm
, 0, i
, 0, tmp
);
1940 /* Order HPTE updates vs. mmu_ready */
1942 kvm
->arch
.mmu_ready
= mmu_ready
;
1943 mutex_unlock(&kvm
->arch
.mmu_setup_lock
);
1950 static int kvm_htab_release(struct inode
*inode
, struct file
*filp
)
1952 struct kvm_htab_ctx
*ctx
= filp
->private_data
;
1954 filp
->private_data
= NULL
;
1955 if (!(ctx
->flags
& KVM_GET_HTAB_WRITE
))
1956 atomic_dec(&ctx
->kvm
->arch
.hpte_mod_interest
);
1957 kvm_put_kvm(ctx
->kvm
);
1962 static const struct file_operations kvm_htab_fops
= {
1963 .read
= kvm_htab_read
,
1964 .write
= kvm_htab_write
,
1965 .llseek
= default_llseek
,
1966 .release
= kvm_htab_release
,
1969 int kvm_vm_ioctl_get_htab_fd(struct kvm
*kvm
, struct kvm_get_htab_fd
*ghf
)
1972 struct kvm_htab_ctx
*ctx
;
1975 /* reject flags we don't recognize */
1976 if (ghf
->flags
& ~(KVM_GET_HTAB_BOLTED_ONLY
| KVM_GET_HTAB_WRITE
))
1978 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
1983 ctx
->index
= ghf
->start_index
;
1984 ctx
->flags
= ghf
->flags
;
1985 ctx
->first_pass
= 1;
1987 rwflag
= (ghf
->flags
& KVM_GET_HTAB_WRITE
) ? O_WRONLY
: O_RDONLY
;
1988 ret
= anon_inode_getfd("kvm-htab", &kvm_htab_fops
, ctx
, rwflag
| O_CLOEXEC
);
1991 kvm_put_kvm_no_destroy(kvm
);
1995 if (rwflag
== O_RDONLY
) {
1996 mutex_lock(&kvm
->slots_lock
);
1997 atomic_inc(&kvm
->arch
.hpte_mod_interest
);
1998 /* make sure kvmppc_do_h_enter etc. see the increment */
1999 synchronize_srcu_expedited(&kvm
->srcu
);
2000 mutex_unlock(&kvm
->slots_lock
);
2006 struct debugfs_htab_state
{
2009 unsigned long hpt_index
;
2015 static int debugfs_htab_open(struct inode
*inode
, struct file
*file
)
2017 struct kvm
*kvm
= inode
->i_private
;
2018 struct debugfs_htab_state
*p
;
2020 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
2026 mutex_init(&p
->mutex
);
2027 file
->private_data
= p
;
2029 return nonseekable_open(inode
, file
);
2032 static int debugfs_htab_release(struct inode
*inode
, struct file
*file
)
2034 struct debugfs_htab_state
*p
= file
->private_data
;
2036 kvm_put_kvm(p
->kvm
);
2041 static ssize_t
debugfs_htab_read(struct file
*file
, char __user
*buf
,
2042 size_t len
, loff_t
*ppos
)
2044 struct debugfs_htab_state
*p
= file
->private_data
;
2047 unsigned long v
, hr
, gr
;
2052 if (kvm_is_radix(kvm
))
2055 ret
= mutex_lock_interruptible(&p
->mutex
);
2059 if (p
->chars_left
) {
2063 r
= copy_to_user(buf
, p
->buf
+ p
->buf_index
, n
);
2078 hptp
= (__be64
*)(kvm
->arch
.hpt
.virt
+ (i
* HPTE_SIZE
));
2079 for (; len
!= 0 && i
< kvmppc_hpt_npte(&kvm
->arch
.hpt
);
2081 if (!(be64_to_cpu(hptp
[0]) & (HPTE_V_VALID
| HPTE_V_ABSENT
)))
2084 /* lock the HPTE so it's stable and read it */
2086 while (!try_lock_hpte(hptp
, HPTE_V_HVLOCK
))
2088 v
= be64_to_cpu(hptp
[0]) & ~HPTE_V_HVLOCK
;
2089 hr
= be64_to_cpu(hptp
[1]);
2090 gr
= kvm
->arch
.hpt
.rev
[i
].guest_rpte
;
2091 unlock_hpte(hptp
, v
);
2094 if (!(v
& (HPTE_V_VALID
| HPTE_V_ABSENT
)))
2097 n
= scnprintf(p
->buf
, sizeof(p
->buf
),
2098 "%6lx %.16lx %.16lx %.16lx\n",
2103 r
= copy_to_user(buf
, p
->buf
, n
);
2119 mutex_unlock(&p
->mutex
);
2123 static ssize_t
debugfs_htab_write(struct file
*file
, const char __user
*buf
,
2124 size_t len
, loff_t
*ppos
)
2129 static const struct file_operations debugfs_htab_fops
= {
2130 .owner
= THIS_MODULE
,
2131 .open
= debugfs_htab_open
,
2132 .release
= debugfs_htab_release
,
2133 .read
= debugfs_htab_read
,
2134 .write
= debugfs_htab_write
,
2135 .llseek
= generic_file_llseek
,
2138 void kvmppc_mmu_debugfs_init(struct kvm
*kvm
)
2140 debugfs_create_file("htab", 0400, kvm
->arch
.debugfs_dir
, kvm
,
2141 &debugfs_htab_fops
);
2144 void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu
*vcpu
)
2146 struct kvmppc_mmu
*mmu
= &vcpu
->arch
.mmu
;
2148 vcpu
->arch
.slb_nr
= 32; /* POWER7/POWER8 */
2150 mmu
->xlate
= kvmppc_mmu_book3s_64_hv_xlate
;
2152 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_SLB
;