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>
27 #include <linux/srcu.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/file.h>
30 #include <linux/debugfs.h>
32 #include <asm/tlbflush.h>
33 #include <asm/kvm_ppc.h>
34 #include <asm/kvm_book3s.h>
35 #include <asm/mmu-hash64.h>
36 #include <asm/hvcall.h>
37 #include <asm/synch.h>
38 #include <asm/ppc-opcode.h>
39 #include <asm/cputable.h>
43 /* Power architecture requires HPT is at least 256kB */
44 #define PPC_MIN_HPT_ORDER 18
46 static long kvmppc_virtmode_do_h_enter(struct kvm
*kvm
, unsigned long flags
,
47 long pte_index
, unsigned long pteh
,
48 unsigned long ptel
, unsigned long *pte_idx_ret
);
49 static void kvmppc_rmap_reset(struct kvm
*kvm
);
51 long kvmppc_alloc_hpt(struct kvm
*kvm
, u32
*htab_orderp
)
53 unsigned long hpt
= 0;
54 struct revmap_entry
*rev
;
55 struct page
*page
= NULL
;
56 long order
= KVM_DEFAULT_HPT_ORDER
;
60 if (order
< PPC_MIN_HPT_ORDER
)
61 order
= PPC_MIN_HPT_ORDER
;
64 kvm
->arch
.hpt_cma_alloc
= 0;
65 page
= kvm_alloc_hpt(1ul << (order
- PAGE_SHIFT
));
67 hpt
= (unsigned long)pfn_to_kaddr(page_to_pfn(page
));
68 memset((void *)hpt
, 0, (1ul << order
));
69 kvm
->arch
.hpt_cma_alloc
= 1;
72 /* Lastly try successively smaller sizes from the page allocator */
73 while (!hpt
&& order
> PPC_MIN_HPT_ORDER
) {
74 hpt
= __get_free_pages(GFP_KERNEL
|__GFP_ZERO
|__GFP_REPEAT
|
75 __GFP_NOWARN
, order
- PAGE_SHIFT
);
83 kvm
->arch
.hpt_virt
= hpt
;
84 kvm
->arch
.hpt_order
= order
;
85 /* HPTEs are 2**4 bytes long */
86 kvm
->arch
.hpt_npte
= 1ul << (order
- 4);
87 /* 128 (2**7) bytes in each HPTEG */
88 kvm
->arch
.hpt_mask
= (1ul << (order
- 7)) - 1;
90 /* Allocate reverse map array */
91 rev
= vmalloc(sizeof(struct revmap_entry
) * kvm
->arch
.hpt_npte
);
93 pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
96 kvm
->arch
.revmap
= rev
;
97 kvm
->arch
.sdr1
= __pa(hpt
) | (order
- 18);
99 pr_info("KVM guest htab at %lx (order %ld), LPID %x\n",
100 hpt
, order
, kvm
->arch
.lpid
);
103 *htab_orderp
= order
;
107 if (kvm
->arch
.hpt_cma_alloc
)
108 kvm_release_hpt(page
, 1 << (order
- PAGE_SHIFT
));
110 free_pages(hpt
, order
- PAGE_SHIFT
);
114 long kvmppc_alloc_reset_hpt(struct kvm
*kvm
, u32
*htab_orderp
)
119 mutex_lock(&kvm
->lock
);
120 if (kvm
->arch
.hpte_setup_done
) {
121 kvm
->arch
.hpte_setup_done
= 0;
122 /* order hpte_setup_done vs. vcpus_running */
124 if (atomic_read(&kvm
->arch
.vcpus_running
)) {
125 kvm
->arch
.hpte_setup_done
= 1;
129 if (kvm
->arch
.hpt_virt
) {
130 order
= kvm
->arch
.hpt_order
;
131 /* Set the entire HPT to 0, i.e. invalid HPTEs */
132 memset((void *)kvm
->arch
.hpt_virt
, 0, 1ul << order
);
134 * Reset all the reverse-mapping chains for all memslots
136 kvmppc_rmap_reset(kvm
);
137 /* Ensure that each vcpu will flush its TLB on next entry. */
138 cpumask_setall(&kvm
->arch
.need_tlb_flush
);
139 *htab_orderp
= order
;
142 err
= kvmppc_alloc_hpt(kvm
, htab_orderp
);
143 order
= *htab_orderp
;
146 mutex_unlock(&kvm
->lock
);
150 void kvmppc_free_hpt(struct kvm
*kvm
)
152 kvmppc_free_lpid(kvm
->arch
.lpid
);
153 vfree(kvm
->arch
.revmap
);
154 if (kvm
->arch
.hpt_cma_alloc
)
155 kvm_release_hpt(virt_to_page(kvm
->arch
.hpt_virt
),
156 1 << (kvm
->arch
.hpt_order
- PAGE_SHIFT
));
158 free_pages(kvm
->arch
.hpt_virt
,
159 kvm
->arch
.hpt_order
- PAGE_SHIFT
);
162 /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
163 static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize
)
165 return (pgsize
> 0x1000) ? HPTE_V_LARGE
: 0;
168 /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
169 static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize
)
171 return (pgsize
== 0x10000) ? 0x1000 : 0;
174 void kvmppc_map_vrma(struct kvm_vcpu
*vcpu
, struct kvm_memory_slot
*memslot
,
175 unsigned long porder
)
178 unsigned long npages
;
179 unsigned long hp_v
, hp_r
;
180 unsigned long addr
, hash
;
182 unsigned long hp0
, hp1
;
183 unsigned long idx_ret
;
185 struct kvm
*kvm
= vcpu
->kvm
;
187 psize
= 1ul << porder
;
188 npages
= memslot
->npages
>> (porder
- PAGE_SHIFT
);
190 /* VRMA can't be > 1TB */
191 if (npages
> 1ul << (40 - porder
))
192 npages
= 1ul << (40 - porder
);
193 /* Can't use more than 1 HPTE per HPTEG */
194 if (npages
> kvm
->arch
.hpt_mask
+ 1)
195 npages
= kvm
->arch
.hpt_mask
+ 1;
197 hp0
= HPTE_V_1TB_SEG
| (VRMA_VSID
<< (40 - 16)) |
198 HPTE_V_BOLTED
| hpte0_pgsize_encoding(psize
);
199 hp1
= hpte1_pgsize_encoding(psize
) |
200 HPTE_R_R
| HPTE_R_C
| HPTE_R_M
| PP_RWXX
;
202 for (i
= 0; i
< npages
; ++i
) {
204 /* can't use hpt_hash since va > 64 bits */
205 hash
= (i
^ (VRMA_VSID
^ (VRMA_VSID
<< 25))) & kvm
->arch
.hpt_mask
;
207 * We assume that the hash table is empty and no
208 * vcpus are using it at this stage. Since we create
209 * at most one HPTE per HPTEG, we just assume entry 7
210 * is available and use it.
212 hash
= (hash
<< 3) + 7;
213 hp_v
= hp0
| ((addr
>> 16) & ~0x7fUL
);
215 ret
= kvmppc_virtmode_do_h_enter(kvm
, H_EXACT
, hash
, hp_v
, hp_r
,
217 if (ret
!= H_SUCCESS
) {
218 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
225 int kvmppc_mmu_hv_init(void)
227 unsigned long host_lpid
, rsvd_lpid
;
229 if (!cpu_has_feature(CPU_FTR_HVMODE
))
232 /* POWER7 has 10-bit LPIDs (12-bit in POWER8) */
233 host_lpid
= mfspr(SPRN_LPID
);
234 rsvd_lpid
= LPID_RSVD
;
236 kvmppc_init_lpid(rsvd_lpid
+ 1);
238 kvmppc_claim_lpid(host_lpid
);
239 /* rsvd_lpid is reserved for use in partition switching */
240 kvmppc_claim_lpid(rsvd_lpid
);
245 static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu
*vcpu
)
247 unsigned long msr
= vcpu
->arch
.intr_msr
;
249 /* If transactional, change to suspend mode on IRQ delivery */
250 if (MSR_TM_TRANSACTIONAL(vcpu
->arch
.shregs
.msr
))
253 msr
|= vcpu
->arch
.shregs
.msr
& MSR_TS_MASK
;
254 kvmppc_set_msr(vcpu
, msr
);
257 long kvmppc_virtmode_do_h_enter(struct kvm
*kvm
, unsigned long flags
,
258 long pte_index
, unsigned long pteh
,
259 unsigned long ptel
, unsigned long *pte_idx_ret
)
263 /* Protect linux PTE lookup from page table destruction */
264 rcu_read_lock_sched(); /* this disables preemption too */
265 ret
= kvmppc_do_h_enter(kvm
, flags
, pte_index
, pteh
, ptel
,
266 current
->mm
->pgd
, false, pte_idx_ret
);
267 rcu_read_unlock_sched();
268 if (ret
== H_TOO_HARD
) {
269 /* this can't happen */
270 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
271 ret
= H_RESOURCE
; /* or something */
277 static struct kvmppc_slb
*kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu
*vcpu
,
283 for (i
= 0; i
< vcpu
->arch
.slb_nr
; i
++) {
284 if (!(vcpu
->arch
.slb
[i
].orige
& SLB_ESID_V
))
287 if (vcpu
->arch
.slb
[i
].origv
& SLB_VSID_B_1T
)
292 if (((vcpu
->arch
.slb
[i
].orige
^ eaddr
) & mask
) == 0)
293 return &vcpu
->arch
.slb
[i
];
298 static unsigned long kvmppc_mmu_get_real_addr(unsigned long v
, unsigned long r
,
301 unsigned long ra_mask
;
303 ra_mask
= hpte_page_size(v
, r
) - 1;
304 return (r
& HPTE_R_RPN
& ~ra_mask
) | (ea
& ra_mask
);
307 static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu
*vcpu
, gva_t eaddr
,
308 struct kvmppc_pte
*gpte
, bool data
, bool iswrite
)
310 struct kvm
*kvm
= vcpu
->kvm
;
311 struct kvmppc_slb
*slbe
;
313 unsigned long pp
, key
;
317 int virtmode
= vcpu
->arch
.shregs
.msr
& (data
? MSR_DR
: MSR_IR
);
321 slbe
= kvmppc_mmu_book3s_hv_find_slbe(vcpu
, eaddr
);
326 /* real mode access */
327 slb_v
= vcpu
->kvm
->arch
.vrma_slb_v
;
331 /* Find the HPTE in the hash table */
332 index
= kvmppc_hv_find_lock_hpte(kvm
, eaddr
, slb_v
,
333 HPTE_V_VALID
| HPTE_V_ABSENT
);
338 hptep
= (__be64
*)(kvm
->arch
.hpt_virt
+ (index
<< 4));
339 v
= be64_to_cpu(hptep
[0]) & ~HPTE_V_HVLOCK
;
340 gr
= kvm
->arch
.revmap
[index
].guest_rpte
;
342 unlock_hpte(hptep
, v
);
346 gpte
->vpage
= ((v
& HPTE_V_AVPN
) << 4) | ((eaddr
>> 12) & 0xfff);
348 /* Get PP bits and key for permission check */
349 pp
= gr
& (HPTE_R_PP0
| HPTE_R_PP
);
350 key
= (vcpu
->arch
.shregs
.msr
& MSR_PR
) ? SLB_VSID_KP
: SLB_VSID_KS
;
353 /* Calculate permissions */
354 gpte
->may_read
= hpte_read_permission(pp
, key
);
355 gpte
->may_write
= hpte_write_permission(pp
, key
);
356 gpte
->may_execute
= gpte
->may_read
&& !(gr
& (HPTE_R_N
| HPTE_R_G
));
358 /* Storage key permission check for POWER7 */
359 if (data
&& virtmode
) {
360 int amrfield
= hpte_get_skey_perm(gr
, vcpu
->arch
.amr
);
367 /* Get the guest physical address */
368 gpte
->raddr
= kvmppc_mmu_get_real_addr(v
, gr
, eaddr
);
373 * Quick test for whether an instruction is a load or a store.
374 * If the instruction is a load or a store, then this will indicate
375 * which it is, at least on server processors. (Embedded processors
376 * have some external PID instructions that don't follow the rule
377 * embodied here.) If the instruction isn't a load or store, then
378 * this doesn't return anything useful.
380 static int instruction_is_store(unsigned int instr
)
385 if ((instr
& 0xfc000000) == 0x7c000000)
386 mask
= 0x100; /* major opcode 31 */
387 return (instr
& mask
) != 0;
390 static int kvmppc_hv_emulate_mmio(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
391 unsigned long gpa
, gva_t ea
, int is_store
)
396 * If we fail, we just return to the guest and try executing it again.
398 if (kvmppc_get_last_inst(vcpu
, INST_GENERIC
, &last_inst
) !=
403 * WARNING: We do not know for sure whether the instruction we just
404 * read from memory is the same that caused the fault in the first
405 * place. If the instruction we read is neither an load or a store,
406 * then it can't access memory, so we don't need to worry about
407 * enforcing access permissions. So, assuming it is a load or
408 * store, we just check that its direction (load or store) is
409 * consistent with the original fault, since that's what we
410 * checked the access permissions against. If there is a mismatch
411 * we just return and retry the instruction.
414 if (instruction_is_store(last_inst
) != !!is_store
)
418 * Emulated accesses are emulated by looking at the hash for
419 * translation once, then performing the access later. The
420 * translation could be invalidated in the meantime in which
421 * point performing the subsequent memory access on the old
422 * physical address could possibly be a security hole for the
423 * guest (but not the host).
425 * This is less of an issue for MMIO stores since they aren't
426 * globally visible. It could be an issue for MMIO loads to
427 * a certain extent but we'll ignore it for now.
430 vcpu
->arch
.paddr_accessed
= gpa
;
431 vcpu
->arch
.vaddr_accessed
= ea
;
432 return kvmppc_emulate_mmio(run
, vcpu
);
435 int kvmppc_book3s_hv_page_fault(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
436 unsigned long ea
, unsigned long dsisr
)
438 struct kvm
*kvm
= vcpu
->kvm
;
439 unsigned long hpte
[3], r
;
441 unsigned long mmu_seq
, psize
, pte_size
;
442 unsigned long gpa_base
, gfn_base
;
443 unsigned long gpa
, gfn
, hva
, pfn
;
444 struct kvm_memory_slot
*memslot
;
446 struct revmap_entry
*rev
;
447 struct page
*page
, *pages
[1];
448 long index
, ret
, npages
;
450 unsigned int writing
, write_ok
;
451 struct vm_area_struct
*vma
;
452 unsigned long rcbits
;
455 * Real-mode code has already searched the HPT and found the
456 * entry we're interested in. Lock the entry and check that
457 * it hasn't changed. If it has, just return and re-execute the
460 if (ea
!= vcpu
->arch
.pgfault_addr
)
462 index
= vcpu
->arch
.pgfault_index
;
463 hptep
= (__be64
*)(kvm
->arch
.hpt_virt
+ (index
<< 4));
464 rev
= &kvm
->arch
.revmap
[index
];
466 while (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
))
468 hpte
[0] = be64_to_cpu(hptep
[0]) & ~HPTE_V_HVLOCK
;
469 hpte
[1] = be64_to_cpu(hptep
[1]);
470 hpte
[2] = r
= rev
->guest_rpte
;
471 unlock_hpte(hptep
, hpte
[0]);
474 if (hpte
[0] != vcpu
->arch
.pgfault_hpte
[0] ||
475 hpte
[1] != vcpu
->arch
.pgfault_hpte
[1])
478 /* Translate the logical address and get the page */
479 psize
= hpte_page_size(hpte
[0], r
);
480 gpa_base
= r
& HPTE_R_RPN
& ~(psize
- 1);
481 gfn_base
= gpa_base
>> PAGE_SHIFT
;
482 gpa
= gpa_base
| (ea
& (psize
- 1));
483 gfn
= gpa
>> PAGE_SHIFT
;
484 memslot
= gfn_to_memslot(kvm
, gfn
);
486 trace_kvm_page_fault_enter(vcpu
, hpte
, memslot
, ea
, dsisr
);
488 /* No memslot means it's an emulated MMIO region */
489 if (!memslot
|| (memslot
->flags
& KVM_MEMSLOT_INVALID
))
490 return kvmppc_hv_emulate_mmio(run
, vcpu
, gpa
, ea
,
491 dsisr
& DSISR_ISSTORE
);
494 * This should never happen, because of the slot_is_aligned()
495 * check in kvmppc_do_h_enter().
497 if (gfn_base
< memslot
->base_gfn
)
500 /* used to check for invalidations in progress */
501 mmu_seq
= kvm
->mmu_notifier_seq
;
508 pte_size
= PAGE_SIZE
;
509 writing
= (dsisr
& DSISR_ISSTORE
) != 0;
510 /* If writing != 0, then the HPTE must allow writing, if we get here */
512 hva
= gfn_to_hva_memslot(memslot
, gfn
);
513 npages
= get_user_pages_fast(hva
, 1, writing
, pages
);
515 /* Check if it's an I/O mapping */
516 down_read(¤t
->mm
->mmap_sem
);
517 vma
= find_vma(current
->mm
, hva
);
518 if (vma
&& vma
->vm_start
<= hva
&& hva
+ psize
<= vma
->vm_end
&&
519 (vma
->vm_flags
& VM_PFNMAP
)) {
520 pfn
= vma
->vm_pgoff
+
521 ((hva
- vma
->vm_start
) >> PAGE_SHIFT
);
523 is_io
= hpte_cache_bits(pgprot_val(vma
->vm_page_prot
));
524 write_ok
= vma
->vm_flags
& VM_WRITE
;
526 up_read(¤t
->mm
->mmap_sem
);
531 pfn
= page_to_pfn(page
);
532 if (PageHuge(page
)) {
533 page
= compound_head(page
);
534 pte_size
<<= compound_order(page
);
536 /* if the guest wants write access, see if that is OK */
537 if (!writing
&& hpte_is_writable(r
)) {
541 * We need to protect against page table destruction
542 * hugepage split and collapse.
544 local_irq_save(flags
);
545 ptep
= find_linux_pte_or_hugepte(current
->mm
->pgd
,
548 pte
= kvmppc_read_update_linux_pte(ptep
, 1);
552 local_irq_restore(flags
);
556 if (psize
> pte_size
)
559 /* Check WIMG vs. the actual page we're accessing */
560 if (!hpte_cache_flags_ok(r
, is_io
)) {
565 * Allow guest to map emulated device memory as
566 * uncacheable, but actually make it cacheable.
568 r
= (r
& ~(HPTE_R_W
|HPTE_R_I
|HPTE_R_G
)) | HPTE_R_M
;
572 * Set the HPTE to point to pfn.
573 * Since the pfn is at PAGE_SIZE granularity, make sure we
574 * don't mask out lower-order bits if psize < PAGE_SIZE.
576 if (psize
< PAGE_SIZE
)
578 r
= (r
& ~(HPTE_R_PP0
- psize
)) | ((pfn
<< PAGE_SHIFT
) & ~(psize
- 1));
579 if (hpte_is_writable(r
) && !write_ok
)
580 r
= hpte_make_readonly(r
);
583 while (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
))
585 if ((be64_to_cpu(hptep
[0]) & ~HPTE_V_HVLOCK
) != hpte
[0] ||
586 be64_to_cpu(hptep
[1]) != hpte
[1] ||
587 rev
->guest_rpte
!= hpte
[2])
588 /* HPTE has been changed under us; let the guest retry */
590 hpte
[0] = (hpte
[0] & ~HPTE_V_ABSENT
) | HPTE_V_VALID
;
592 /* Always put the HPTE in the rmap chain for the page base address */
593 rmap
= &memslot
->arch
.rmap
[gfn_base
- memslot
->base_gfn
];
596 /* Check if we might have been invalidated; let the guest retry if so */
598 if (mmu_notifier_retry(vcpu
->kvm
, mmu_seq
)) {
603 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
604 rcbits
= *rmap
>> KVMPPC_RMAP_RC_SHIFT
;
605 r
&= rcbits
| ~(HPTE_R_R
| HPTE_R_C
);
607 if (be64_to_cpu(hptep
[0]) & HPTE_V_VALID
) {
608 /* HPTE was previously valid, so we need to invalidate it */
610 hptep
[0] |= cpu_to_be64(HPTE_V_ABSENT
);
611 kvmppc_invalidate_hpte(kvm
, hptep
, index
);
612 /* don't lose previous R and C bits */
613 r
|= be64_to_cpu(hptep
[1]) & (HPTE_R_R
| HPTE_R_C
);
615 kvmppc_add_revmap_chain(kvm
, rev
, rmap
, index
, 0);
618 hptep
[1] = cpu_to_be64(r
);
620 __unlock_hpte(hptep
, hpte
[0]);
621 asm volatile("ptesync" : : : "memory");
623 if (page
&& hpte_is_writable(r
))
627 trace_kvm_page_fault_exit(vcpu
, hpte
, ret
);
631 * We drop pages[0] here, not page because page might
632 * have been set to the head page of a compound, but
633 * we have to drop the reference on the correct tail
634 * page to match the get inside gup()
641 __unlock_hpte(hptep
, be64_to_cpu(hptep
[0]));
646 static void kvmppc_rmap_reset(struct kvm
*kvm
)
648 struct kvm_memslots
*slots
;
649 struct kvm_memory_slot
*memslot
;
652 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
653 slots
= kvm
->memslots
;
654 kvm_for_each_memslot(memslot
, slots
) {
656 * This assumes it is acceptable to lose reference and
657 * change bits across a reset.
659 memset(memslot
->arch
.rmap
, 0,
660 memslot
->npages
* sizeof(*memslot
->arch
.rmap
));
662 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
665 static int kvm_handle_hva_range(struct kvm
*kvm
,
668 int (*handler
)(struct kvm
*kvm
,
669 unsigned long *rmapp
,
674 struct kvm_memslots
*slots
;
675 struct kvm_memory_slot
*memslot
;
677 slots
= kvm_memslots(kvm
);
678 kvm_for_each_memslot(memslot
, slots
) {
679 unsigned long hva_start
, hva_end
;
682 hva_start
= max(start
, memslot
->userspace_addr
);
683 hva_end
= min(end
, memslot
->userspace_addr
+
684 (memslot
->npages
<< PAGE_SHIFT
));
685 if (hva_start
>= hva_end
)
688 * {gfn(page) | page intersects with [hva_start, hva_end)} =
689 * {gfn, gfn+1, ..., gfn_end-1}.
691 gfn
= hva_to_gfn_memslot(hva_start
, memslot
);
692 gfn_end
= hva_to_gfn_memslot(hva_end
+ PAGE_SIZE
- 1, memslot
);
694 for (; gfn
< gfn_end
; ++gfn
) {
695 gfn_t gfn_offset
= gfn
- memslot
->base_gfn
;
697 ret
= handler(kvm
, &memslot
->arch
.rmap
[gfn_offset
], gfn
);
705 static int kvm_handle_hva(struct kvm
*kvm
, unsigned long hva
,
706 int (*handler
)(struct kvm
*kvm
, unsigned long *rmapp
,
709 return kvm_handle_hva_range(kvm
, hva
, hva
+ 1, handler
);
712 static int kvm_unmap_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
715 struct revmap_entry
*rev
= kvm
->arch
.revmap
;
716 unsigned long h
, i
, j
;
718 unsigned long ptel
, psize
, rcbits
;
722 if (!(*rmapp
& KVMPPC_RMAP_PRESENT
)) {
728 * To avoid an ABBA deadlock with the HPTE lock bit,
729 * we can't spin on the HPTE lock while holding the
732 i
= *rmapp
& KVMPPC_RMAP_INDEX
;
733 hptep
= (__be64
*) (kvm
->arch
.hpt_virt
+ (i
<< 4));
734 if (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
)) {
735 /* unlock rmap before spinning on the HPTE lock */
737 while (be64_to_cpu(hptep
[0]) & HPTE_V_HVLOCK
)
743 /* chain is now empty */
744 *rmapp
&= ~(KVMPPC_RMAP_PRESENT
| KVMPPC_RMAP_INDEX
);
746 /* remove i from chain */
750 rev
[i
].forw
= rev
[i
].back
= i
;
751 *rmapp
= (*rmapp
& ~KVMPPC_RMAP_INDEX
) | j
;
754 /* Now check and modify the HPTE */
755 ptel
= rev
[i
].guest_rpte
;
756 psize
= hpte_page_size(be64_to_cpu(hptep
[0]), ptel
);
757 if ((be64_to_cpu(hptep
[0]) & HPTE_V_VALID
) &&
758 hpte_rpn(ptel
, psize
) == gfn
) {
759 hptep
[0] |= cpu_to_be64(HPTE_V_ABSENT
);
760 kvmppc_invalidate_hpte(kvm
, hptep
, i
);
761 /* Harvest R and C */
762 rcbits
= be64_to_cpu(hptep
[1]) & (HPTE_R_R
| HPTE_R_C
);
763 *rmapp
|= rcbits
<< KVMPPC_RMAP_RC_SHIFT
;
764 if (rcbits
& ~rev
[i
].guest_rpte
) {
765 rev
[i
].guest_rpte
= ptel
| rcbits
;
766 note_hpte_modification(kvm
, &rev
[i
]);
770 __unlock_hpte(hptep
, be64_to_cpu(hptep
[0]));
775 int kvm_unmap_hva_hv(struct kvm
*kvm
, unsigned long hva
)
777 kvm_handle_hva(kvm
, hva
, kvm_unmap_rmapp
);
781 int kvm_unmap_hva_range_hv(struct kvm
*kvm
, unsigned long start
, unsigned long end
)
783 kvm_handle_hva_range(kvm
, start
, end
, kvm_unmap_rmapp
);
787 void kvmppc_core_flush_memslot_hv(struct kvm
*kvm
,
788 struct kvm_memory_slot
*memslot
)
790 unsigned long *rmapp
;
794 rmapp
= memslot
->arch
.rmap
;
795 gfn
= memslot
->base_gfn
;
796 for (n
= memslot
->npages
; n
; --n
) {
798 * Testing the present bit without locking is OK because
799 * the memslot has been marked invalid already, and hence
800 * no new HPTEs referencing this page can be created,
801 * thus the present bit can't go from 0 to 1.
803 if (*rmapp
& KVMPPC_RMAP_PRESENT
)
804 kvm_unmap_rmapp(kvm
, rmapp
, gfn
);
810 static int kvm_age_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
813 struct revmap_entry
*rev
= kvm
->arch
.revmap
;
814 unsigned long head
, i
, j
;
820 if (*rmapp
& KVMPPC_RMAP_REFERENCED
) {
821 *rmapp
&= ~KVMPPC_RMAP_REFERENCED
;
824 if (!(*rmapp
& KVMPPC_RMAP_PRESENT
)) {
829 i
= head
= *rmapp
& KVMPPC_RMAP_INDEX
;
831 hptep
= (__be64
*) (kvm
->arch
.hpt_virt
+ (i
<< 4));
834 /* If this HPTE isn't referenced, ignore it */
835 if (!(be64_to_cpu(hptep
[1]) & HPTE_R_R
))
838 if (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
)) {
839 /* unlock rmap before spinning on the HPTE lock */
841 while (be64_to_cpu(hptep
[0]) & HPTE_V_HVLOCK
)
846 /* Now check and modify the HPTE */
847 if ((be64_to_cpu(hptep
[0]) & HPTE_V_VALID
) &&
848 (be64_to_cpu(hptep
[1]) & HPTE_R_R
)) {
849 kvmppc_clear_ref_hpte(kvm
, hptep
, i
);
850 if (!(rev
[i
].guest_rpte
& HPTE_R_R
)) {
851 rev
[i
].guest_rpte
|= HPTE_R_R
;
852 note_hpte_modification(kvm
, &rev
[i
]);
856 __unlock_hpte(hptep
, be64_to_cpu(hptep
[0]));
857 } while ((i
= j
) != head
);
863 int kvm_age_hva_hv(struct kvm
*kvm
, unsigned long start
, unsigned long end
)
865 return kvm_handle_hva_range(kvm
, start
, end
, kvm_age_rmapp
);
868 static int kvm_test_age_rmapp(struct kvm
*kvm
, unsigned long *rmapp
,
871 struct revmap_entry
*rev
= kvm
->arch
.revmap
;
872 unsigned long head
, i
, j
;
876 if (*rmapp
& KVMPPC_RMAP_REFERENCED
)
880 if (*rmapp
& KVMPPC_RMAP_REFERENCED
)
883 if (*rmapp
& KVMPPC_RMAP_PRESENT
) {
884 i
= head
= *rmapp
& KVMPPC_RMAP_INDEX
;
886 hp
= (unsigned long *)(kvm
->arch
.hpt_virt
+ (i
<< 4));
888 if (be64_to_cpu(hp
[1]) & HPTE_R_R
)
890 } while ((i
= j
) != head
);
899 int kvm_test_age_hva_hv(struct kvm
*kvm
, unsigned long hva
)
901 return kvm_handle_hva(kvm
, hva
, kvm_test_age_rmapp
);
904 void kvm_set_spte_hva_hv(struct kvm
*kvm
, unsigned long hva
, pte_t pte
)
906 kvm_handle_hva(kvm
, hva
, kvm_unmap_rmapp
);
909 static int vcpus_running(struct kvm
*kvm
)
911 return atomic_read(&kvm
->arch
.vcpus_running
) != 0;
915 * Returns the number of system pages that are dirty.
916 * This can be more than 1 if we find a huge-page HPTE.
918 static int kvm_test_clear_dirty_npages(struct kvm
*kvm
, unsigned long *rmapp
)
920 struct revmap_entry
*rev
= kvm
->arch
.revmap
;
921 unsigned long head
, i
, j
;
925 int npages_dirty
= 0;
929 if (*rmapp
& KVMPPC_RMAP_CHANGED
) {
930 *rmapp
&= ~KVMPPC_RMAP_CHANGED
;
933 if (!(*rmapp
& KVMPPC_RMAP_PRESENT
)) {
938 i
= head
= *rmapp
& KVMPPC_RMAP_INDEX
;
940 unsigned long hptep1
;
941 hptep
= (__be64
*) (kvm
->arch
.hpt_virt
+ (i
<< 4));
945 * Checking the C (changed) bit here is racy since there
946 * is no guarantee about when the hardware writes it back.
947 * If the HPTE is not writable then it is stable since the
948 * page can't be written to, and we would have done a tlbie
949 * (which forces the hardware to complete any writeback)
950 * when making the HPTE read-only.
951 * If vcpus are running then this call is racy anyway
952 * since the page could get dirtied subsequently, so we
953 * expect there to be a further call which would pick up
954 * any delayed C bit writeback.
955 * Otherwise we need to do the tlbie even if C==0 in
956 * order to pick up any delayed writeback of C.
958 hptep1
= be64_to_cpu(hptep
[1]);
959 if (!(hptep1
& HPTE_R_C
) &&
960 (!hpte_is_writable(hptep1
) || vcpus_running(kvm
)))
963 if (!try_lock_hpte(hptep
, HPTE_V_HVLOCK
)) {
964 /* unlock rmap before spinning on the HPTE lock */
966 while (hptep
[0] & cpu_to_be64(HPTE_V_HVLOCK
))
971 /* Now check and modify the HPTE */
972 if (!(hptep
[0] & cpu_to_be64(HPTE_V_VALID
))) {
973 __unlock_hpte(hptep
, be64_to_cpu(hptep
[0]));
977 /* need to make it temporarily absent so C is stable */
978 hptep
[0] |= cpu_to_be64(HPTE_V_ABSENT
);
979 kvmppc_invalidate_hpte(kvm
, hptep
, i
);
980 v
= be64_to_cpu(hptep
[0]);
981 r
= be64_to_cpu(hptep
[1]);
983 hptep
[1] = cpu_to_be64(r
& ~HPTE_R_C
);
984 if (!(rev
[i
].guest_rpte
& HPTE_R_C
)) {
985 rev
[i
].guest_rpte
|= HPTE_R_C
;
986 note_hpte_modification(kvm
, &rev
[i
]);
988 n
= hpte_page_size(v
, r
);
989 n
= (n
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
990 if (n
> npages_dirty
)
996 __unlock_hpte(hptep
, v
);
997 } while ((i
= j
) != head
);
1000 return npages_dirty
;
1003 static void harvest_vpa_dirty(struct kvmppc_vpa
*vpa
,
1004 struct kvm_memory_slot
*memslot
,
1009 if (!vpa
->dirty
|| !vpa
->pinned_addr
)
1011 gfn
= vpa
->gpa
>> PAGE_SHIFT
;
1012 if (gfn
< memslot
->base_gfn
||
1013 gfn
>= memslot
->base_gfn
+ memslot
->npages
)
1018 __set_bit_le(gfn
- memslot
->base_gfn
, map
);
1021 long kvmppc_hv_get_dirty_log(struct kvm
*kvm
, struct kvm_memory_slot
*memslot
,
1025 unsigned long *rmapp
;
1026 struct kvm_vcpu
*vcpu
;
1029 rmapp
= memslot
->arch
.rmap
;
1030 for (i
= 0; i
< memslot
->npages
; ++i
) {
1031 int npages
= kvm_test_clear_dirty_npages(kvm
, rmapp
);
1033 * Note that if npages > 0 then i must be a multiple of npages,
1034 * since we always put huge-page HPTEs in the rmap chain
1035 * corresponding to their page base address.
1038 for (j
= i
; npages
; ++j
, --npages
)
1039 __set_bit_le(j
, map
);
1043 /* Harvest dirty bits from VPA and DTL updates */
1044 /* Note: we never modify the SLB shadow buffer areas */
1045 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
1046 spin_lock(&vcpu
->arch
.vpa_update_lock
);
1047 harvest_vpa_dirty(&vcpu
->arch
.vpa
, memslot
, map
);
1048 harvest_vpa_dirty(&vcpu
->arch
.dtl
, memslot
, map
);
1049 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
1055 void *kvmppc_pin_guest_page(struct kvm
*kvm
, unsigned long gpa
,
1056 unsigned long *nb_ret
)
1058 struct kvm_memory_slot
*memslot
;
1059 unsigned long gfn
= gpa
>> PAGE_SHIFT
;
1060 struct page
*page
, *pages
[1];
1062 unsigned long hva
, offset
;
1065 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1066 memslot
= gfn_to_memslot(kvm
, gfn
);
1067 if (!memslot
|| (memslot
->flags
& KVM_MEMSLOT_INVALID
))
1069 hva
= gfn_to_hva_memslot(memslot
, gfn
);
1070 npages
= get_user_pages_fast(hva
, 1, 1, pages
);
1074 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1076 offset
= gpa
& (PAGE_SIZE
- 1);
1078 *nb_ret
= PAGE_SIZE
- offset
;
1079 return page_address(page
) + offset
;
1082 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1086 void kvmppc_unpin_guest_page(struct kvm
*kvm
, void *va
, unsigned long gpa
,
1089 struct page
*page
= virt_to_page(va
);
1090 struct kvm_memory_slot
*memslot
;
1092 unsigned long *rmap
;
1100 /* We need to mark this page dirty in the rmap chain */
1101 gfn
= gpa
>> PAGE_SHIFT
;
1102 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1103 memslot
= gfn_to_memslot(kvm
, gfn
);
1105 rmap
= &memslot
->arch
.rmap
[gfn
- memslot
->base_gfn
];
1107 *rmap
|= KVMPPC_RMAP_CHANGED
;
1110 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1114 * Functions for reading and writing the hash table via reads and
1115 * writes on a file descriptor.
1117 * Reads return the guest view of the hash table, which has to be
1118 * pieced together from the real hash table and the guest_rpte
1119 * values in the revmap array.
1121 * On writes, each HPTE written is considered in turn, and if it
1122 * is valid, it is written to the HPT as if an H_ENTER with the
1123 * exact flag set was done. When the invalid count is non-zero
1124 * in the header written to the stream, the kernel will make
1125 * sure that that many HPTEs are invalid, and invalidate them
1129 struct kvm_htab_ctx
{
1130 unsigned long index
;
1131 unsigned long flags
;
1136 #define HPTE_SIZE (2 * sizeof(unsigned long))
1139 * Returns 1 if this HPT entry has been modified or has pending
1142 static int hpte_dirty(struct revmap_entry
*revp
, __be64
*hptp
)
1144 unsigned long rcbits_unset
;
1146 if (revp
->guest_rpte
& HPTE_GR_MODIFIED
)
1149 /* Also need to consider changes in reference and changed bits */
1150 rcbits_unset
= ~revp
->guest_rpte
& (HPTE_R_R
| HPTE_R_C
);
1151 if ((be64_to_cpu(hptp
[0]) & HPTE_V_VALID
) &&
1152 (be64_to_cpu(hptp
[1]) & rcbits_unset
))
1158 static long record_hpte(unsigned long flags
, __be64
*hptp
,
1159 unsigned long *hpte
, struct revmap_entry
*revp
,
1160 int want_valid
, int first_pass
)
1163 unsigned long rcbits_unset
;
1167 /* Unmodified entries are uninteresting except on the first pass */
1168 dirty
= hpte_dirty(revp
, hptp
);
1169 if (!first_pass
&& !dirty
)
1173 if (be64_to_cpu(hptp
[0]) & (HPTE_V_VALID
| HPTE_V_ABSENT
)) {
1175 if ((flags
& KVM_GET_HTAB_BOLTED_ONLY
) &&
1176 !(be64_to_cpu(hptp
[0]) & HPTE_V_BOLTED
))
1179 if (valid
!= want_valid
)
1183 if (valid
|| dirty
) {
1184 /* lock the HPTE so it's stable and read it */
1186 while (!try_lock_hpte(hptp
, HPTE_V_HVLOCK
))
1188 v
= be64_to_cpu(hptp
[0]);
1190 /* re-evaluate valid and dirty from synchronized HPTE value */
1191 valid
= !!(v
& HPTE_V_VALID
);
1192 dirty
= !!(revp
->guest_rpte
& HPTE_GR_MODIFIED
);
1194 /* Harvest R and C into guest view if necessary */
1195 rcbits_unset
= ~revp
->guest_rpte
& (HPTE_R_R
| HPTE_R_C
);
1196 if (valid
&& (rcbits_unset
& be64_to_cpu(hptp
[1]))) {
1197 revp
->guest_rpte
|= (be64_to_cpu(hptp
[1]) &
1198 (HPTE_R_R
| HPTE_R_C
)) | HPTE_GR_MODIFIED
;
1202 if (v
& HPTE_V_ABSENT
) {
1203 v
&= ~HPTE_V_ABSENT
;
1207 if ((flags
& KVM_GET_HTAB_BOLTED_ONLY
) && !(v
& HPTE_V_BOLTED
))
1210 r
= revp
->guest_rpte
;
1211 /* only clear modified if this is the right sort of entry */
1212 if (valid
== want_valid
&& dirty
) {
1213 r
&= ~HPTE_GR_MODIFIED
;
1214 revp
->guest_rpte
= r
;
1216 unlock_hpte(hptp
, be64_to_cpu(hptp
[0]));
1218 if (!(valid
== want_valid
&& (first_pass
|| dirty
)))
1221 hpte
[0] = cpu_to_be64(v
);
1222 hpte
[1] = cpu_to_be64(r
);
1226 static ssize_t
kvm_htab_read(struct file
*file
, char __user
*buf
,
1227 size_t count
, loff_t
*ppos
)
1229 struct kvm_htab_ctx
*ctx
= file
->private_data
;
1230 struct kvm
*kvm
= ctx
->kvm
;
1231 struct kvm_get_htab_header hdr
;
1233 struct revmap_entry
*revp
;
1234 unsigned long i
, nb
, nw
;
1235 unsigned long __user
*lbuf
;
1236 struct kvm_get_htab_header __user
*hptr
;
1237 unsigned long flags
;
1239 unsigned long hpte
[2];
1241 if (!access_ok(VERIFY_WRITE
, buf
, count
))
1244 first_pass
= ctx
->first_pass
;
1248 hptp
= (__be64
*)(kvm
->arch
.hpt_virt
+ (i
* HPTE_SIZE
));
1249 revp
= kvm
->arch
.revmap
+ i
;
1250 lbuf
= (unsigned long __user
*)buf
;
1253 while (nb
+ sizeof(hdr
) + HPTE_SIZE
< count
) {
1254 /* Initialize header */
1255 hptr
= (struct kvm_get_htab_header __user
*)buf
;
1260 lbuf
= (unsigned long __user
*)(buf
+ sizeof(hdr
));
1262 /* Skip uninteresting entries, i.e. clean on not-first pass */
1264 while (i
< kvm
->arch
.hpt_npte
&&
1265 !hpte_dirty(revp
, hptp
)) {
1273 /* Grab a series of valid entries */
1274 while (i
< kvm
->arch
.hpt_npte
&&
1275 hdr
.n_valid
< 0xffff &&
1276 nb
+ HPTE_SIZE
< count
&&
1277 record_hpte(flags
, hptp
, hpte
, revp
, 1, first_pass
)) {
1278 /* valid entry, write it out */
1280 if (__put_user(hpte
[0], lbuf
) ||
1281 __put_user(hpte
[1], lbuf
+ 1))
1289 /* Now skip invalid entries while we can */
1290 while (i
< kvm
->arch
.hpt_npte
&&
1291 hdr
.n_invalid
< 0xffff &&
1292 record_hpte(flags
, hptp
, hpte
, revp
, 0, first_pass
)) {
1293 /* found an invalid entry */
1300 if (hdr
.n_valid
|| hdr
.n_invalid
) {
1301 /* write back the header */
1302 if (__copy_to_user(hptr
, &hdr
, sizeof(hdr
)))
1305 buf
= (char __user
*)lbuf
;
1310 /* Check if we've wrapped around the hash table */
1311 if (i
>= kvm
->arch
.hpt_npte
) {
1313 ctx
->first_pass
= 0;
1323 static ssize_t
kvm_htab_write(struct file
*file
, const char __user
*buf
,
1324 size_t count
, loff_t
*ppos
)
1326 struct kvm_htab_ctx
*ctx
= file
->private_data
;
1327 struct kvm
*kvm
= ctx
->kvm
;
1328 struct kvm_get_htab_header hdr
;
1331 unsigned long __user
*lbuf
;
1333 unsigned long tmp
[2];
1338 if (!access_ok(VERIFY_READ
, buf
, count
))
1341 /* lock out vcpus from running while we're doing this */
1342 mutex_lock(&kvm
->lock
);
1343 hpte_setup
= kvm
->arch
.hpte_setup_done
;
1345 kvm
->arch
.hpte_setup_done
= 0; /* temporarily */
1346 /* order hpte_setup_done vs. vcpus_running */
1348 if (atomic_read(&kvm
->arch
.vcpus_running
)) {
1349 kvm
->arch
.hpte_setup_done
= 1;
1350 mutex_unlock(&kvm
->lock
);
1356 for (nb
= 0; nb
+ sizeof(hdr
) <= count
; ) {
1358 if (__copy_from_user(&hdr
, buf
, sizeof(hdr
)))
1362 if (nb
+ hdr
.n_valid
* HPTE_SIZE
> count
)
1370 if (i
>= kvm
->arch
.hpt_npte
||
1371 i
+ hdr
.n_valid
+ hdr
.n_invalid
> kvm
->arch
.hpt_npte
)
1374 hptp
= (__be64
*)(kvm
->arch
.hpt_virt
+ (i
* HPTE_SIZE
));
1375 lbuf
= (unsigned long __user
*)buf
;
1376 for (j
= 0; j
< hdr
.n_valid
; ++j
) {
1381 if (__get_user(hpte_v
, lbuf
) ||
1382 __get_user(hpte_r
, lbuf
+ 1))
1384 v
= be64_to_cpu(hpte_v
);
1385 r
= be64_to_cpu(hpte_r
);
1387 if (!(v
& HPTE_V_VALID
))
1392 if (be64_to_cpu(hptp
[0]) & (HPTE_V_VALID
| HPTE_V_ABSENT
))
1393 kvmppc_do_h_remove(kvm
, 0, i
, 0, tmp
);
1395 ret
= kvmppc_virtmode_do_h_enter(kvm
, H_EXACT
, i
, v
, r
,
1397 if (ret
!= H_SUCCESS
) {
1398 pr_err("kvm_htab_write ret %ld i=%ld v=%lx "
1399 "r=%lx\n", ret
, i
, v
, r
);
1402 if (!hpte_setup
&& is_vrma_hpte(v
)) {
1403 unsigned long psize
= hpte_base_page_size(v
, r
);
1404 unsigned long senc
= slb_pgsize_encoding(psize
);
1407 kvm
->arch
.vrma_slb_v
= senc
| SLB_VSID_B_1T
|
1408 (VRMA_VSID
<< SLB_VSID_SHIFT_1T
);
1409 lpcr
= senc
<< (LPCR_VRMASD_SH
- 4);
1410 kvmppc_update_lpcr(kvm
, lpcr
, LPCR_VRMASD
);
1417 for (j
= 0; j
< hdr
.n_invalid
; ++j
) {
1418 if (be64_to_cpu(hptp
[0]) & (HPTE_V_VALID
| HPTE_V_ABSENT
))
1419 kvmppc_do_h_remove(kvm
, 0, i
, 0, tmp
);
1427 /* Order HPTE updates vs. hpte_setup_done */
1429 kvm
->arch
.hpte_setup_done
= hpte_setup
;
1430 mutex_unlock(&kvm
->lock
);
1437 static int kvm_htab_release(struct inode
*inode
, struct file
*filp
)
1439 struct kvm_htab_ctx
*ctx
= filp
->private_data
;
1441 filp
->private_data
= NULL
;
1442 if (!(ctx
->flags
& KVM_GET_HTAB_WRITE
))
1443 atomic_dec(&ctx
->kvm
->arch
.hpte_mod_interest
);
1444 kvm_put_kvm(ctx
->kvm
);
1449 static const struct file_operations kvm_htab_fops
= {
1450 .read
= kvm_htab_read
,
1451 .write
= kvm_htab_write
,
1452 .llseek
= default_llseek
,
1453 .release
= kvm_htab_release
,
1456 int kvm_vm_ioctl_get_htab_fd(struct kvm
*kvm
, struct kvm_get_htab_fd
*ghf
)
1459 struct kvm_htab_ctx
*ctx
;
1462 /* reject flags we don't recognize */
1463 if (ghf
->flags
& ~(KVM_GET_HTAB_BOLTED_ONLY
| KVM_GET_HTAB_WRITE
))
1465 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
1470 ctx
->index
= ghf
->start_index
;
1471 ctx
->flags
= ghf
->flags
;
1472 ctx
->first_pass
= 1;
1474 rwflag
= (ghf
->flags
& KVM_GET_HTAB_WRITE
) ? O_WRONLY
: O_RDONLY
;
1475 ret
= anon_inode_getfd("kvm-htab", &kvm_htab_fops
, ctx
, rwflag
| O_CLOEXEC
);
1481 if (rwflag
== O_RDONLY
) {
1482 mutex_lock(&kvm
->slots_lock
);
1483 atomic_inc(&kvm
->arch
.hpte_mod_interest
);
1484 /* make sure kvmppc_do_h_enter etc. see the increment */
1485 synchronize_srcu_expedited(&kvm
->srcu
);
1486 mutex_unlock(&kvm
->slots_lock
);
1492 struct debugfs_htab_state
{
1495 unsigned long hpt_index
;
1501 static int debugfs_htab_open(struct inode
*inode
, struct file
*file
)
1503 struct kvm
*kvm
= inode
->i_private
;
1504 struct debugfs_htab_state
*p
;
1506 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
1512 mutex_init(&p
->mutex
);
1513 file
->private_data
= p
;
1515 return nonseekable_open(inode
, file
);
1518 static int debugfs_htab_release(struct inode
*inode
, struct file
*file
)
1520 struct debugfs_htab_state
*p
= file
->private_data
;
1522 kvm_put_kvm(p
->kvm
);
1527 static ssize_t
debugfs_htab_read(struct file
*file
, char __user
*buf
,
1528 size_t len
, loff_t
*ppos
)
1530 struct debugfs_htab_state
*p
= file
->private_data
;
1533 unsigned long v
, hr
, gr
;
1537 ret
= mutex_lock_interruptible(&p
->mutex
);
1541 if (p
->chars_left
) {
1545 r
= copy_to_user(buf
, p
->buf
+ p
->buf_index
, n
);
1561 hptp
= (__be64
*)(kvm
->arch
.hpt_virt
+ (i
* HPTE_SIZE
));
1562 for (; len
!= 0 && i
< kvm
->arch
.hpt_npte
; ++i
, hptp
+= 2) {
1563 if (!(be64_to_cpu(hptp
[0]) & (HPTE_V_VALID
| HPTE_V_ABSENT
)))
1566 /* lock the HPTE so it's stable and read it */
1568 while (!try_lock_hpte(hptp
, HPTE_V_HVLOCK
))
1570 v
= be64_to_cpu(hptp
[0]) & ~HPTE_V_HVLOCK
;
1571 hr
= be64_to_cpu(hptp
[1]);
1572 gr
= kvm
->arch
.revmap
[i
].guest_rpte
;
1573 unlock_hpte(hptp
, v
);
1576 if (!(v
& (HPTE_V_VALID
| HPTE_V_ABSENT
)))
1579 n
= scnprintf(p
->buf
, sizeof(p
->buf
),
1580 "%6lx %.16lx %.16lx %.16lx\n",
1585 r
= copy_to_user(buf
, p
->buf
, n
);
1601 mutex_unlock(&p
->mutex
);
1605 ssize_t
debugfs_htab_write(struct file
*file
, const char __user
*buf
,
1606 size_t len
, loff_t
*ppos
)
1611 static const struct file_operations debugfs_htab_fops
= {
1612 .owner
= THIS_MODULE
,
1613 .open
= debugfs_htab_open
,
1614 .release
= debugfs_htab_release
,
1615 .read
= debugfs_htab_read
,
1616 .write
= debugfs_htab_write
,
1617 .llseek
= generic_file_llseek
,
1620 void kvmppc_mmu_debugfs_init(struct kvm
*kvm
)
1622 kvm
->arch
.htab_dentry
= debugfs_create_file("htab", 0400,
1623 kvm
->arch
.debugfs_dir
, kvm
,
1624 &debugfs_htab_fops
);
1627 void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu
*vcpu
)
1629 struct kvmppc_mmu
*mmu
= &vcpu
->arch
.mmu
;
1631 vcpu
->arch
.slb_nr
= 32; /* POWER7/POWER8 */
1633 mmu
->xlate
= kvmppc_mmu_book3s_64_hv_xlate
;
1634 mmu
->reset_msr
= kvmppc_mmu_book3s_64_hv_reset_msr
;
1636 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_SLB
;