x86/efi: Enforce CONFIG_RELOCATABLE for EFI boot stub
[linux/fpc-iii.git] / arch / powerpc / kvm / book3s_64_mmu_hv.c
blob46ff25149282ed7ca50a04dbd40da82819a15df5
1 /*
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>
31 #include <asm/tlbflush.h>
32 #include <asm/kvm_ppc.h>
33 #include <asm/kvm_book3s.h>
34 #include <asm/mmu-hash64.h>
35 #include <asm/hvcall.h>
36 #include <asm/synch.h>
37 #include <asm/ppc-opcode.h>
38 #include <asm/cputable.h>
40 #include "book3s_hv_cma.h"
42 /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
43 #define MAX_LPID_970 63
45 /* Power architecture requires HPT is at least 256kB */
46 #define PPC_MIN_HPT_ORDER 18
48 static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
49 long pte_index, unsigned long pteh,
50 unsigned long ptel, unsigned long *pte_idx_ret);
51 static void kvmppc_rmap_reset(struct kvm *kvm);
53 long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp)
55 unsigned long hpt;
56 struct revmap_entry *rev;
57 struct page *page = NULL;
58 long order = KVM_DEFAULT_HPT_ORDER;
60 if (htab_orderp) {
61 order = *htab_orderp;
62 if (order < PPC_MIN_HPT_ORDER)
63 order = PPC_MIN_HPT_ORDER;
66 kvm->arch.hpt_cma_alloc = 0;
68 * try first to allocate it from the kernel page allocator.
69 * We keep the CMA reserved for failed allocation.
71 hpt = __get_free_pages(GFP_KERNEL | __GFP_ZERO | __GFP_REPEAT |
72 __GFP_NOWARN, order - PAGE_SHIFT);
74 /* Next try to allocate from the preallocated pool */
75 if (!hpt) {
76 VM_BUG_ON(order < KVM_CMA_CHUNK_ORDER);
77 page = kvm_alloc_hpt(1 << (order - PAGE_SHIFT));
78 if (page) {
79 hpt = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
80 kvm->arch.hpt_cma_alloc = 1;
81 } else
82 --order;
85 /* Lastly try successively smaller sizes from the page allocator */
86 while (!hpt && order > PPC_MIN_HPT_ORDER) {
87 hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
88 __GFP_NOWARN, order - PAGE_SHIFT);
89 if (!hpt)
90 --order;
93 if (!hpt)
94 return -ENOMEM;
96 kvm->arch.hpt_virt = hpt;
97 kvm->arch.hpt_order = order;
98 /* HPTEs are 2**4 bytes long */
99 kvm->arch.hpt_npte = 1ul << (order - 4);
100 /* 128 (2**7) bytes in each HPTEG */
101 kvm->arch.hpt_mask = (1ul << (order - 7)) - 1;
103 /* Allocate reverse map array */
104 rev = vmalloc(sizeof(struct revmap_entry) * kvm->arch.hpt_npte);
105 if (!rev) {
106 pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
107 goto out_freehpt;
109 kvm->arch.revmap = rev;
110 kvm->arch.sdr1 = __pa(hpt) | (order - 18);
112 pr_info("KVM guest htab at %lx (order %ld), LPID %x\n",
113 hpt, order, kvm->arch.lpid);
115 if (htab_orderp)
116 *htab_orderp = order;
117 return 0;
119 out_freehpt:
120 if (kvm->arch.hpt_cma_alloc)
121 kvm_release_hpt(page, 1 << (order - PAGE_SHIFT));
122 else
123 free_pages(hpt, order - PAGE_SHIFT);
124 return -ENOMEM;
127 long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp)
129 long err = -EBUSY;
130 long order;
132 mutex_lock(&kvm->lock);
133 if (kvm->arch.rma_setup_done) {
134 kvm->arch.rma_setup_done = 0;
135 /* order rma_setup_done vs. vcpus_running */
136 smp_mb();
137 if (atomic_read(&kvm->arch.vcpus_running)) {
138 kvm->arch.rma_setup_done = 1;
139 goto out;
142 if (kvm->arch.hpt_virt) {
143 order = kvm->arch.hpt_order;
144 /* Set the entire HPT to 0, i.e. invalid HPTEs */
145 memset((void *)kvm->arch.hpt_virt, 0, 1ul << order);
147 * Reset all the reverse-mapping chains for all memslots
149 kvmppc_rmap_reset(kvm);
150 /* Ensure that each vcpu will flush its TLB on next entry. */
151 cpumask_setall(&kvm->arch.need_tlb_flush);
152 *htab_orderp = order;
153 err = 0;
154 } else {
155 err = kvmppc_alloc_hpt(kvm, htab_orderp);
156 order = *htab_orderp;
158 out:
159 mutex_unlock(&kvm->lock);
160 return err;
163 void kvmppc_free_hpt(struct kvm *kvm)
165 kvmppc_free_lpid(kvm->arch.lpid);
166 vfree(kvm->arch.revmap);
167 if (kvm->arch.hpt_cma_alloc)
168 kvm_release_hpt(virt_to_page(kvm->arch.hpt_virt),
169 1 << (kvm->arch.hpt_order - PAGE_SHIFT));
170 else
171 free_pages(kvm->arch.hpt_virt,
172 kvm->arch.hpt_order - PAGE_SHIFT);
175 /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
176 static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
178 return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
181 /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
182 static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
184 return (pgsize == 0x10000) ? 0x1000 : 0;
187 void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
188 unsigned long porder)
190 unsigned long i;
191 unsigned long npages;
192 unsigned long hp_v, hp_r;
193 unsigned long addr, hash;
194 unsigned long psize;
195 unsigned long hp0, hp1;
196 unsigned long idx_ret;
197 long ret;
198 struct kvm *kvm = vcpu->kvm;
200 psize = 1ul << porder;
201 npages = memslot->npages >> (porder - PAGE_SHIFT);
203 /* VRMA can't be > 1TB */
204 if (npages > 1ul << (40 - porder))
205 npages = 1ul << (40 - porder);
206 /* Can't use more than 1 HPTE per HPTEG */
207 if (npages > kvm->arch.hpt_mask + 1)
208 npages = kvm->arch.hpt_mask + 1;
210 hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
211 HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
212 hp1 = hpte1_pgsize_encoding(psize) |
213 HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
215 for (i = 0; i < npages; ++i) {
216 addr = i << porder;
217 /* can't use hpt_hash since va > 64 bits */
218 hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & kvm->arch.hpt_mask;
220 * We assume that the hash table is empty and no
221 * vcpus are using it at this stage. Since we create
222 * at most one HPTE per HPTEG, we just assume entry 7
223 * is available and use it.
225 hash = (hash << 3) + 7;
226 hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
227 hp_r = hp1 | addr;
228 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r,
229 &idx_ret);
230 if (ret != H_SUCCESS) {
231 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
232 addr, ret);
233 break;
238 int kvmppc_mmu_hv_init(void)
240 unsigned long host_lpid, rsvd_lpid;
242 if (!cpu_has_feature(CPU_FTR_HVMODE))
243 return -EINVAL;
245 /* POWER7 has 10-bit LPIDs, PPC970 and e500mc have 6-bit LPIDs */
246 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
247 host_lpid = mfspr(SPRN_LPID); /* POWER7 */
248 rsvd_lpid = LPID_RSVD;
249 } else {
250 host_lpid = 0; /* PPC970 */
251 rsvd_lpid = MAX_LPID_970;
254 kvmppc_init_lpid(rsvd_lpid + 1);
256 kvmppc_claim_lpid(host_lpid);
257 /* rsvd_lpid is reserved for use in partition switching */
258 kvmppc_claim_lpid(rsvd_lpid);
260 return 0;
263 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
267 static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
269 kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
273 * This is called to get a reference to a guest page if there isn't
274 * one already in the memslot->arch.slot_phys[] array.
276 static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn,
277 struct kvm_memory_slot *memslot,
278 unsigned long psize)
280 unsigned long start;
281 long np, err;
282 struct page *page, *hpage, *pages[1];
283 unsigned long s, pgsize;
284 unsigned long *physp;
285 unsigned int is_io, got, pgorder;
286 struct vm_area_struct *vma;
287 unsigned long pfn, i, npages;
289 physp = memslot->arch.slot_phys;
290 if (!physp)
291 return -EINVAL;
292 if (physp[gfn - memslot->base_gfn])
293 return 0;
295 is_io = 0;
296 got = 0;
297 page = NULL;
298 pgsize = psize;
299 err = -EINVAL;
300 start = gfn_to_hva_memslot(memslot, gfn);
302 /* Instantiate and get the page we want access to */
303 np = get_user_pages_fast(start, 1, 1, pages);
304 if (np != 1) {
305 /* Look up the vma for the page */
306 down_read(&current->mm->mmap_sem);
307 vma = find_vma(current->mm, start);
308 if (!vma || vma->vm_start > start ||
309 start + psize > vma->vm_end ||
310 !(vma->vm_flags & VM_PFNMAP))
311 goto up_err;
312 is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
313 pfn = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
314 /* check alignment of pfn vs. requested page size */
315 if (psize > PAGE_SIZE && (pfn & ((psize >> PAGE_SHIFT) - 1)))
316 goto up_err;
317 up_read(&current->mm->mmap_sem);
319 } else {
320 page = pages[0];
321 got = KVMPPC_GOT_PAGE;
323 /* See if this is a large page */
324 s = PAGE_SIZE;
325 if (PageHuge(page)) {
326 hpage = compound_head(page);
327 s <<= compound_order(hpage);
328 /* Get the whole large page if slot alignment is ok */
329 if (s > psize && slot_is_aligned(memslot, s) &&
330 !(memslot->userspace_addr & (s - 1))) {
331 start &= ~(s - 1);
332 pgsize = s;
333 get_page(hpage);
334 put_page(page);
335 page = hpage;
338 if (s < psize)
339 goto out;
340 pfn = page_to_pfn(page);
343 npages = pgsize >> PAGE_SHIFT;
344 pgorder = __ilog2(npages);
345 physp += (gfn - memslot->base_gfn) & ~(npages - 1);
346 spin_lock(&kvm->arch.slot_phys_lock);
347 for (i = 0; i < npages; ++i) {
348 if (!physp[i]) {
349 physp[i] = ((pfn + i) << PAGE_SHIFT) +
350 got + is_io + pgorder;
351 got = 0;
354 spin_unlock(&kvm->arch.slot_phys_lock);
355 err = 0;
357 out:
358 if (got)
359 put_page(page);
360 return err;
362 up_err:
363 up_read(&current->mm->mmap_sem);
364 return err;
367 long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
368 long pte_index, unsigned long pteh,
369 unsigned long ptel, unsigned long *pte_idx_ret)
371 unsigned long psize, gpa, gfn;
372 struct kvm_memory_slot *memslot;
373 long ret;
375 if (kvm->arch.using_mmu_notifiers)
376 goto do_insert;
378 psize = hpte_page_size(pteh, ptel);
379 if (!psize)
380 return H_PARAMETER;
382 pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
384 /* Find the memslot (if any) for this address */
385 gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
386 gfn = gpa >> PAGE_SHIFT;
387 memslot = gfn_to_memslot(kvm, gfn);
388 if (memslot && !(memslot->flags & KVM_MEMSLOT_INVALID)) {
389 if (!slot_is_aligned(memslot, psize))
390 return H_PARAMETER;
391 if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0)
392 return H_PARAMETER;
395 do_insert:
396 /* Protect linux PTE lookup from page table destruction */
397 rcu_read_lock_sched(); /* this disables preemption too */
398 ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
399 current->mm->pgd, false, pte_idx_ret);
400 rcu_read_unlock_sched();
401 if (ret == H_TOO_HARD) {
402 /* this can't happen */
403 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
404 ret = H_RESOURCE; /* or something */
406 return ret;
411 * We come here on a H_ENTER call from the guest when we are not
412 * using mmu notifiers and we don't have the requested page pinned
413 * already.
415 long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
416 long pte_index, unsigned long pteh,
417 unsigned long ptel)
419 return kvmppc_virtmode_do_h_enter(vcpu->kvm, flags, pte_index,
420 pteh, ptel, &vcpu->arch.gpr[4]);
423 static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
424 gva_t eaddr)
426 u64 mask;
427 int i;
429 for (i = 0; i < vcpu->arch.slb_nr; i++) {
430 if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
431 continue;
433 if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
434 mask = ESID_MASK_1T;
435 else
436 mask = ESID_MASK;
438 if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
439 return &vcpu->arch.slb[i];
441 return NULL;
444 static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
445 unsigned long ea)
447 unsigned long ra_mask;
449 ra_mask = hpte_page_size(v, r) - 1;
450 return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
453 static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
454 struct kvmppc_pte *gpte, bool data)
456 struct kvm *kvm = vcpu->kvm;
457 struct kvmppc_slb *slbe;
458 unsigned long slb_v;
459 unsigned long pp, key;
460 unsigned long v, gr;
461 unsigned long *hptep;
462 int index;
463 int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
465 /* Get SLB entry */
466 if (virtmode) {
467 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
468 if (!slbe)
469 return -EINVAL;
470 slb_v = slbe->origv;
471 } else {
472 /* real mode access */
473 slb_v = vcpu->kvm->arch.vrma_slb_v;
476 preempt_disable();
477 /* Find the HPTE in the hash table */
478 index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
479 HPTE_V_VALID | HPTE_V_ABSENT);
480 if (index < 0) {
481 preempt_enable();
482 return -ENOENT;
484 hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
485 v = hptep[0] & ~HPTE_V_HVLOCK;
486 gr = kvm->arch.revmap[index].guest_rpte;
488 /* Unlock the HPTE */
489 asm volatile("lwsync" : : : "memory");
490 hptep[0] = v;
491 preempt_enable();
493 gpte->eaddr = eaddr;
494 gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
496 /* Get PP bits and key for permission check */
497 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
498 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
499 key &= slb_v;
501 /* Calculate permissions */
502 gpte->may_read = hpte_read_permission(pp, key);
503 gpte->may_write = hpte_write_permission(pp, key);
504 gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
506 /* Storage key permission check for POWER7 */
507 if (data && virtmode && cpu_has_feature(CPU_FTR_ARCH_206)) {
508 int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
509 if (amrfield & 1)
510 gpte->may_read = 0;
511 if (amrfield & 2)
512 gpte->may_write = 0;
515 /* Get the guest physical address */
516 gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
517 return 0;
521 * Quick test for whether an instruction is a load or a store.
522 * If the instruction is a load or a store, then this will indicate
523 * which it is, at least on server processors. (Embedded processors
524 * have some external PID instructions that don't follow the rule
525 * embodied here.) If the instruction isn't a load or store, then
526 * this doesn't return anything useful.
528 static int instruction_is_store(unsigned int instr)
530 unsigned int mask;
532 mask = 0x10000000;
533 if ((instr & 0xfc000000) == 0x7c000000)
534 mask = 0x100; /* major opcode 31 */
535 return (instr & mask) != 0;
538 static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
539 unsigned long gpa, gva_t ea, int is_store)
541 int ret;
542 u32 last_inst;
543 unsigned long srr0 = kvmppc_get_pc(vcpu);
545 /* We try to load the last instruction. We don't let
546 * emulate_instruction do it as it doesn't check what
547 * kvmppc_ld returns.
548 * If we fail, we just return to the guest and try executing it again.
550 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) {
551 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
552 if (ret != EMULATE_DONE || last_inst == KVM_INST_FETCH_FAILED)
553 return RESUME_GUEST;
554 vcpu->arch.last_inst = last_inst;
558 * WARNING: We do not know for sure whether the instruction we just
559 * read from memory is the same that caused the fault in the first
560 * place. If the instruction we read is neither an load or a store,
561 * then it can't access memory, so we don't need to worry about
562 * enforcing access permissions. So, assuming it is a load or
563 * store, we just check that its direction (load or store) is
564 * consistent with the original fault, since that's what we
565 * checked the access permissions against. If there is a mismatch
566 * we just return and retry the instruction.
569 if (instruction_is_store(vcpu->arch.last_inst) != !!is_store)
570 return RESUME_GUEST;
573 * Emulated accesses are emulated by looking at the hash for
574 * translation once, then performing the access later. The
575 * translation could be invalidated in the meantime in which
576 * point performing the subsequent memory access on the old
577 * physical address could possibly be a security hole for the
578 * guest (but not the host).
580 * This is less of an issue for MMIO stores since they aren't
581 * globally visible. It could be an issue for MMIO loads to
582 * a certain extent but we'll ignore it for now.
585 vcpu->arch.paddr_accessed = gpa;
586 vcpu->arch.vaddr_accessed = ea;
587 return kvmppc_emulate_mmio(run, vcpu);
590 int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
591 unsigned long ea, unsigned long dsisr)
593 struct kvm *kvm = vcpu->kvm;
594 unsigned long *hptep, hpte[3], r;
595 unsigned long mmu_seq, psize, pte_size;
596 unsigned long gpa, gfn, hva, pfn;
597 struct kvm_memory_slot *memslot;
598 unsigned long *rmap;
599 struct revmap_entry *rev;
600 struct page *page, *pages[1];
601 long index, ret, npages;
602 unsigned long is_io;
603 unsigned int writing, write_ok;
604 struct vm_area_struct *vma;
605 unsigned long rcbits;
608 * Real-mode code has already searched the HPT and found the
609 * entry we're interested in. Lock the entry and check that
610 * it hasn't changed. If it has, just return and re-execute the
611 * instruction.
613 if (ea != vcpu->arch.pgfault_addr)
614 return RESUME_GUEST;
615 index = vcpu->arch.pgfault_index;
616 hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
617 rev = &kvm->arch.revmap[index];
618 preempt_disable();
619 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
620 cpu_relax();
621 hpte[0] = hptep[0] & ~HPTE_V_HVLOCK;
622 hpte[1] = hptep[1];
623 hpte[2] = r = rev->guest_rpte;
624 asm volatile("lwsync" : : : "memory");
625 hptep[0] = hpte[0];
626 preempt_enable();
628 if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
629 hpte[1] != vcpu->arch.pgfault_hpte[1])
630 return RESUME_GUEST;
632 /* Translate the logical address and get the page */
633 psize = hpte_page_size(hpte[0], r);
634 gpa = (r & HPTE_R_RPN & ~(psize - 1)) | (ea & (psize - 1));
635 gfn = gpa >> PAGE_SHIFT;
636 memslot = gfn_to_memslot(kvm, gfn);
638 /* No memslot means it's an emulated MMIO region */
639 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
640 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
641 dsisr & DSISR_ISSTORE);
643 if (!kvm->arch.using_mmu_notifiers)
644 return -EFAULT; /* should never get here */
646 /* used to check for invalidations in progress */
647 mmu_seq = kvm->mmu_notifier_seq;
648 smp_rmb();
650 is_io = 0;
651 pfn = 0;
652 page = NULL;
653 pte_size = PAGE_SIZE;
654 writing = (dsisr & DSISR_ISSTORE) != 0;
655 /* If writing != 0, then the HPTE must allow writing, if we get here */
656 write_ok = writing;
657 hva = gfn_to_hva_memslot(memslot, gfn);
658 npages = get_user_pages_fast(hva, 1, writing, pages);
659 if (npages < 1) {
660 /* Check if it's an I/O mapping */
661 down_read(&current->mm->mmap_sem);
662 vma = find_vma(current->mm, hva);
663 if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
664 (vma->vm_flags & VM_PFNMAP)) {
665 pfn = vma->vm_pgoff +
666 ((hva - vma->vm_start) >> PAGE_SHIFT);
667 pte_size = psize;
668 is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
669 write_ok = vma->vm_flags & VM_WRITE;
671 up_read(&current->mm->mmap_sem);
672 if (!pfn)
673 return -EFAULT;
674 } else {
675 page = pages[0];
676 if (PageHuge(page)) {
677 page = compound_head(page);
678 pte_size <<= compound_order(page);
680 /* if the guest wants write access, see if that is OK */
681 if (!writing && hpte_is_writable(r)) {
682 unsigned int hugepage_shift;
683 pte_t *ptep, pte;
686 * We need to protect against page table destruction
687 * while looking up and updating the pte.
689 rcu_read_lock_sched();
690 ptep = find_linux_pte_or_hugepte(current->mm->pgd,
691 hva, &hugepage_shift);
692 if (ptep) {
693 pte = kvmppc_read_update_linux_pte(ptep, 1,
694 hugepage_shift);
695 if (pte_write(pte))
696 write_ok = 1;
698 rcu_read_unlock_sched();
700 pfn = page_to_pfn(page);
703 ret = -EFAULT;
704 if (psize > pte_size)
705 goto out_put;
707 /* Check WIMG vs. the actual page we're accessing */
708 if (!hpte_cache_flags_ok(r, is_io)) {
709 if (is_io)
710 return -EFAULT;
712 * Allow guest to map emulated device memory as
713 * uncacheable, but actually make it cacheable.
715 r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
718 /* Set the HPTE to point to pfn */
719 r = (r & ~(HPTE_R_PP0 - pte_size)) | (pfn << PAGE_SHIFT);
720 if (hpte_is_writable(r) && !write_ok)
721 r = hpte_make_readonly(r);
722 ret = RESUME_GUEST;
723 preempt_disable();
724 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
725 cpu_relax();
726 if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] ||
727 rev->guest_rpte != hpte[2])
728 /* HPTE has been changed under us; let the guest retry */
729 goto out_unlock;
730 hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
732 rmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
733 lock_rmap(rmap);
735 /* Check if we might have been invalidated; let the guest retry if so */
736 ret = RESUME_GUEST;
737 if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) {
738 unlock_rmap(rmap);
739 goto out_unlock;
742 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
743 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
744 r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
746 if (hptep[0] & HPTE_V_VALID) {
747 /* HPTE was previously valid, so we need to invalidate it */
748 unlock_rmap(rmap);
749 hptep[0] |= HPTE_V_ABSENT;
750 kvmppc_invalidate_hpte(kvm, hptep, index);
751 /* don't lose previous R and C bits */
752 r |= hptep[1] & (HPTE_R_R | HPTE_R_C);
753 } else {
754 kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
757 hptep[1] = r;
758 eieio();
759 hptep[0] = hpte[0];
760 asm volatile("ptesync" : : : "memory");
761 preempt_enable();
762 if (page && hpte_is_writable(r))
763 SetPageDirty(page);
765 out_put:
766 if (page) {
768 * We drop pages[0] here, not page because page might
769 * have been set to the head page of a compound, but
770 * we have to drop the reference on the correct tail
771 * page to match the get inside gup()
773 put_page(pages[0]);
775 return ret;
777 out_unlock:
778 hptep[0] &= ~HPTE_V_HVLOCK;
779 preempt_enable();
780 goto out_put;
783 static void kvmppc_rmap_reset(struct kvm *kvm)
785 struct kvm_memslots *slots;
786 struct kvm_memory_slot *memslot;
787 int srcu_idx;
789 srcu_idx = srcu_read_lock(&kvm->srcu);
790 slots = kvm->memslots;
791 kvm_for_each_memslot(memslot, slots) {
793 * This assumes it is acceptable to lose reference and
794 * change bits across a reset.
796 memset(memslot->arch.rmap, 0,
797 memslot->npages * sizeof(*memslot->arch.rmap));
799 srcu_read_unlock(&kvm->srcu, srcu_idx);
802 static int kvm_handle_hva_range(struct kvm *kvm,
803 unsigned long start,
804 unsigned long end,
805 int (*handler)(struct kvm *kvm,
806 unsigned long *rmapp,
807 unsigned long gfn))
809 int ret;
810 int retval = 0;
811 struct kvm_memslots *slots;
812 struct kvm_memory_slot *memslot;
814 slots = kvm_memslots(kvm);
815 kvm_for_each_memslot(memslot, slots) {
816 unsigned long hva_start, hva_end;
817 gfn_t gfn, gfn_end;
819 hva_start = max(start, memslot->userspace_addr);
820 hva_end = min(end, memslot->userspace_addr +
821 (memslot->npages << PAGE_SHIFT));
822 if (hva_start >= hva_end)
823 continue;
825 * {gfn(page) | page intersects with [hva_start, hva_end)} =
826 * {gfn, gfn+1, ..., gfn_end-1}.
828 gfn = hva_to_gfn_memslot(hva_start, memslot);
829 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
831 for (; gfn < gfn_end; ++gfn) {
832 gfn_t gfn_offset = gfn - memslot->base_gfn;
834 ret = handler(kvm, &memslot->arch.rmap[gfn_offset], gfn);
835 retval |= ret;
839 return retval;
842 static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
843 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
844 unsigned long gfn))
846 return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
849 static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
850 unsigned long gfn)
852 struct revmap_entry *rev = kvm->arch.revmap;
853 unsigned long h, i, j;
854 unsigned long *hptep;
855 unsigned long ptel, psize, rcbits;
857 for (;;) {
858 lock_rmap(rmapp);
859 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
860 unlock_rmap(rmapp);
861 break;
865 * To avoid an ABBA deadlock with the HPTE lock bit,
866 * we can't spin on the HPTE lock while holding the
867 * rmap chain lock.
869 i = *rmapp & KVMPPC_RMAP_INDEX;
870 hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
871 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
872 /* unlock rmap before spinning on the HPTE lock */
873 unlock_rmap(rmapp);
874 while (hptep[0] & HPTE_V_HVLOCK)
875 cpu_relax();
876 continue;
878 j = rev[i].forw;
879 if (j == i) {
880 /* chain is now empty */
881 *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
882 } else {
883 /* remove i from chain */
884 h = rev[i].back;
885 rev[h].forw = j;
886 rev[j].back = h;
887 rev[i].forw = rev[i].back = i;
888 *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
891 /* Now check and modify the HPTE */
892 ptel = rev[i].guest_rpte;
893 psize = hpte_page_size(hptep[0], ptel);
894 if ((hptep[0] & HPTE_V_VALID) &&
895 hpte_rpn(ptel, psize) == gfn) {
896 if (kvm->arch.using_mmu_notifiers)
897 hptep[0] |= HPTE_V_ABSENT;
898 kvmppc_invalidate_hpte(kvm, hptep, i);
899 /* Harvest R and C */
900 rcbits = hptep[1] & (HPTE_R_R | HPTE_R_C);
901 *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
902 if (rcbits & ~rev[i].guest_rpte) {
903 rev[i].guest_rpte = ptel | rcbits;
904 note_hpte_modification(kvm, &rev[i]);
907 unlock_rmap(rmapp);
908 hptep[0] &= ~HPTE_V_HVLOCK;
910 return 0;
913 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
915 if (kvm->arch.using_mmu_notifiers)
916 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
917 return 0;
920 int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
922 if (kvm->arch.using_mmu_notifiers)
923 kvm_handle_hva_range(kvm, start, end, kvm_unmap_rmapp);
924 return 0;
927 void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
929 unsigned long *rmapp;
930 unsigned long gfn;
931 unsigned long n;
933 rmapp = memslot->arch.rmap;
934 gfn = memslot->base_gfn;
935 for (n = memslot->npages; n; --n) {
937 * Testing the present bit without locking is OK because
938 * the memslot has been marked invalid already, and hence
939 * no new HPTEs referencing this page can be created,
940 * thus the present bit can't go from 0 to 1.
942 if (*rmapp & KVMPPC_RMAP_PRESENT)
943 kvm_unmap_rmapp(kvm, rmapp, gfn);
944 ++rmapp;
945 ++gfn;
949 static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
950 unsigned long gfn)
952 struct revmap_entry *rev = kvm->arch.revmap;
953 unsigned long head, i, j;
954 unsigned long *hptep;
955 int ret = 0;
957 retry:
958 lock_rmap(rmapp);
959 if (*rmapp & KVMPPC_RMAP_REFERENCED) {
960 *rmapp &= ~KVMPPC_RMAP_REFERENCED;
961 ret = 1;
963 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
964 unlock_rmap(rmapp);
965 return ret;
968 i = head = *rmapp & KVMPPC_RMAP_INDEX;
969 do {
970 hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
971 j = rev[i].forw;
973 /* If this HPTE isn't referenced, ignore it */
974 if (!(hptep[1] & HPTE_R_R))
975 continue;
977 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
978 /* unlock rmap before spinning on the HPTE lock */
979 unlock_rmap(rmapp);
980 while (hptep[0] & HPTE_V_HVLOCK)
981 cpu_relax();
982 goto retry;
985 /* Now check and modify the HPTE */
986 if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_R)) {
987 kvmppc_clear_ref_hpte(kvm, hptep, i);
988 if (!(rev[i].guest_rpte & HPTE_R_R)) {
989 rev[i].guest_rpte |= HPTE_R_R;
990 note_hpte_modification(kvm, &rev[i]);
992 ret = 1;
994 hptep[0] &= ~HPTE_V_HVLOCK;
995 } while ((i = j) != head);
997 unlock_rmap(rmapp);
998 return ret;
1001 int kvm_age_hva(struct kvm *kvm, unsigned long hva)
1003 if (!kvm->arch.using_mmu_notifiers)
1004 return 0;
1005 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
1008 static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
1009 unsigned long gfn)
1011 struct revmap_entry *rev = kvm->arch.revmap;
1012 unsigned long head, i, j;
1013 unsigned long *hp;
1014 int ret = 1;
1016 if (*rmapp & KVMPPC_RMAP_REFERENCED)
1017 return 1;
1019 lock_rmap(rmapp);
1020 if (*rmapp & KVMPPC_RMAP_REFERENCED)
1021 goto out;
1023 if (*rmapp & KVMPPC_RMAP_PRESENT) {
1024 i = head = *rmapp & KVMPPC_RMAP_INDEX;
1025 do {
1026 hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4));
1027 j = rev[i].forw;
1028 if (hp[1] & HPTE_R_R)
1029 goto out;
1030 } while ((i = j) != head);
1032 ret = 0;
1034 out:
1035 unlock_rmap(rmapp);
1036 return ret;
1039 int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1041 if (!kvm->arch.using_mmu_notifiers)
1042 return 0;
1043 return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
1046 void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1048 if (!kvm->arch.using_mmu_notifiers)
1049 return;
1050 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
1053 static int kvm_test_clear_dirty(struct kvm *kvm, unsigned long *rmapp)
1055 struct revmap_entry *rev = kvm->arch.revmap;
1056 unsigned long head, i, j;
1057 unsigned long *hptep;
1058 int ret = 0;
1060 retry:
1061 lock_rmap(rmapp);
1062 if (*rmapp & KVMPPC_RMAP_CHANGED) {
1063 *rmapp &= ~KVMPPC_RMAP_CHANGED;
1064 ret = 1;
1066 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
1067 unlock_rmap(rmapp);
1068 return ret;
1071 i = head = *rmapp & KVMPPC_RMAP_INDEX;
1072 do {
1073 hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
1074 j = rev[i].forw;
1076 if (!(hptep[1] & HPTE_R_C))
1077 continue;
1079 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
1080 /* unlock rmap before spinning on the HPTE lock */
1081 unlock_rmap(rmapp);
1082 while (hptep[0] & HPTE_V_HVLOCK)
1083 cpu_relax();
1084 goto retry;
1087 /* Now check and modify the HPTE */
1088 if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_C)) {
1089 /* need to make it temporarily absent to clear C */
1090 hptep[0] |= HPTE_V_ABSENT;
1091 kvmppc_invalidate_hpte(kvm, hptep, i);
1092 hptep[1] &= ~HPTE_R_C;
1093 eieio();
1094 hptep[0] = (hptep[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
1095 if (!(rev[i].guest_rpte & HPTE_R_C)) {
1096 rev[i].guest_rpte |= HPTE_R_C;
1097 note_hpte_modification(kvm, &rev[i]);
1099 ret = 1;
1101 hptep[0] &= ~HPTE_V_HVLOCK;
1102 } while ((i = j) != head);
1104 unlock_rmap(rmapp);
1105 return ret;
1108 static void harvest_vpa_dirty(struct kvmppc_vpa *vpa,
1109 struct kvm_memory_slot *memslot,
1110 unsigned long *map)
1112 unsigned long gfn;
1114 if (!vpa->dirty || !vpa->pinned_addr)
1115 return;
1116 gfn = vpa->gpa >> PAGE_SHIFT;
1117 if (gfn < memslot->base_gfn ||
1118 gfn >= memslot->base_gfn + memslot->npages)
1119 return;
1121 vpa->dirty = false;
1122 if (map)
1123 __set_bit_le(gfn - memslot->base_gfn, map);
1126 long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot,
1127 unsigned long *map)
1129 unsigned long i;
1130 unsigned long *rmapp;
1131 struct kvm_vcpu *vcpu;
1133 preempt_disable();
1134 rmapp = memslot->arch.rmap;
1135 for (i = 0; i < memslot->npages; ++i) {
1136 if (kvm_test_clear_dirty(kvm, rmapp) && map)
1137 __set_bit_le(i, map);
1138 ++rmapp;
1141 /* Harvest dirty bits from VPA and DTL updates */
1142 /* Note: we never modify the SLB shadow buffer areas */
1143 kvm_for_each_vcpu(i, vcpu, kvm) {
1144 spin_lock(&vcpu->arch.vpa_update_lock);
1145 harvest_vpa_dirty(&vcpu->arch.vpa, memslot, map);
1146 harvest_vpa_dirty(&vcpu->arch.dtl, memslot, map);
1147 spin_unlock(&vcpu->arch.vpa_update_lock);
1149 preempt_enable();
1150 return 0;
1153 void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
1154 unsigned long *nb_ret)
1156 struct kvm_memory_slot *memslot;
1157 unsigned long gfn = gpa >> PAGE_SHIFT;
1158 struct page *page, *pages[1];
1159 int npages;
1160 unsigned long hva, offset;
1161 unsigned long pa;
1162 unsigned long *physp;
1163 int srcu_idx;
1165 srcu_idx = srcu_read_lock(&kvm->srcu);
1166 memslot = gfn_to_memslot(kvm, gfn);
1167 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
1168 goto err;
1169 if (!kvm->arch.using_mmu_notifiers) {
1170 physp = memslot->arch.slot_phys;
1171 if (!physp)
1172 goto err;
1173 physp += gfn - memslot->base_gfn;
1174 pa = *physp;
1175 if (!pa) {
1176 if (kvmppc_get_guest_page(kvm, gfn, memslot,
1177 PAGE_SIZE) < 0)
1178 goto err;
1179 pa = *physp;
1181 page = pfn_to_page(pa >> PAGE_SHIFT);
1182 get_page(page);
1183 } else {
1184 hva = gfn_to_hva_memslot(memslot, gfn);
1185 npages = get_user_pages_fast(hva, 1, 1, pages);
1186 if (npages < 1)
1187 goto err;
1188 page = pages[0];
1190 srcu_read_unlock(&kvm->srcu, srcu_idx);
1192 offset = gpa & (PAGE_SIZE - 1);
1193 if (nb_ret)
1194 *nb_ret = PAGE_SIZE - offset;
1195 return page_address(page) + offset;
1197 err:
1198 srcu_read_unlock(&kvm->srcu, srcu_idx);
1199 return NULL;
1202 void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa,
1203 bool dirty)
1205 struct page *page = virt_to_page(va);
1206 struct kvm_memory_slot *memslot;
1207 unsigned long gfn;
1208 unsigned long *rmap;
1209 int srcu_idx;
1211 put_page(page);
1213 if (!dirty || !kvm->arch.using_mmu_notifiers)
1214 return;
1216 /* We need to mark this page dirty in the rmap chain */
1217 gfn = gpa >> PAGE_SHIFT;
1218 srcu_idx = srcu_read_lock(&kvm->srcu);
1219 memslot = gfn_to_memslot(kvm, gfn);
1220 if (memslot) {
1221 rmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
1222 lock_rmap(rmap);
1223 *rmap |= KVMPPC_RMAP_CHANGED;
1224 unlock_rmap(rmap);
1226 srcu_read_unlock(&kvm->srcu, srcu_idx);
1230 * Functions for reading and writing the hash table via reads and
1231 * writes on a file descriptor.
1233 * Reads return the guest view of the hash table, which has to be
1234 * pieced together from the real hash table and the guest_rpte
1235 * values in the revmap array.
1237 * On writes, each HPTE written is considered in turn, and if it
1238 * is valid, it is written to the HPT as if an H_ENTER with the
1239 * exact flag set was done. When the invalid count is non-zero
1240 * in the header written to the stream, the kernel will make
1241 * sure that that many HPTEs are invalid, and invalidate them
1242 * if not.
1245 struct kvm_htab_ctx {
1246 unsigned long index;
1247 unsigned long flags;
1248 struct kvm *kvm;
1249 int first_pass;
1252 #define HPTE_SIZE (2 * sizeof(unsigned long))
1255 * Returns 1 if this HPT entry has been modified or has pending
1256 * R/C bit changes.
1258 static int hpte_dirty(struct revmap_entry *revp, unsigned long *hptp)
1260 unsigned long rcbits_unset;
1262 if (revp->guest_rpte & HPTE_GR_MODIFIED)
1263 return 1;
1265 /* Also need to consider changes in reference and changed bits */
1266 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
1267 if ((hptp[0] & HPTE_V_VALID) && (hptp[1] & rcbits_unset))
1268 return 1;
1270 return 0;
1273 static long record_hpte(unsigned long flags, unsigned long *hptp,
1274 unsigned long *hpte, struct revmap_entry *revp,
1275 int want_valid, int first_pass)
1277 unsigned long v, r;
1278 unsigned long rcbits_unset;
1279 int ok = 1;
1280 int valid, dirty;
1282 /* Unmodified entries are uninteresting except on the first pass */
1283 dirty = hpte_dirty(revp, hptp);
1284 if (!first_pass && !dirty)
1285 return 0;
1287 valid = 0;
1288 if (hptp[0] & (HPTE_V_VALID | HPTE_V_ABSENT)) {
1289 valid = 1;
1290 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) &&
1291 !(hptp[0] & HPTE_V_BOLTED))
1292 valid = 0;
1294 if (valid != want_valid)
1295 return 0;
1297 v = r = 0;
1298 if (valid || dirty) {
1299 /* lock the HPTE so it's stable and read it */
1300 preempt_disable();
1301 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
1302 cpu_relax();
1303 v = hptp[0];
1305 /* re-evaluate valid and dirty from synchronized HPTE value */
1306 valid = !!(v & HPTE_V_VALID);
1307 dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
1309 /* Harvest R and C into guest view if necessary */
1310 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
1311 if (valid && (rcbits_unset & hptp[1])) {
1312 revp->guest_rpte |= (hptp[1] & (HPTE_R_R | HPTE_R_C)) |
1313 HPTE_GR_MODIFIED;
1314 dirty = 1;
1317 if (v & HPTE_V_ABSENT) {
1318 v &= ~HPTE_V_ABSENT;
1319 v |= HPTE_V_VALID;
1320 valid = 1;
1322 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED))
1323 valid = 0;
1325 r = revp->guest_rpte;
1326 /* only clear modified if this is the right sort of entry */
1327 if (valid == want_valid && dirty) {
1328 r &= ~HPTE_GR_MODIFIED;
1329 revp->guest_rpte = r;
1331 asm volatile(PPC_RELEASE_BARRIER "" : : : "memory");
1332 hptp[0] &= ~HPTE_V_HVLOCK;
1333 preempt_enable();
1334 if (!(valid == want_valid && (first_pass || dirty)))
1335 ok = 0;
1337 hpte[0] = v;
1338 hpte[1] = r;
1339 return ok;
1342 static ssize_t kvm_htab_read(struct file *file, char __user *buf,
1343 size_t count, loff_t *ppos)
1345 struct kvm_htab_ctx *ctx = file->private_data;
1346 struct kvm *kvm = ctx->kvm;
1347 struct kvm_get_htab_header hdr;
1348 unsigned long *hptp;
1349 struct revmap_entry *revp;
1350 unsigned long i, nb, nw;
1351 unsigned long __user *lbuf;
1352 struct kvm_get_htab_header __user *hptr;
1353 unsigned long flags;
1354 int first_pass;
1355 unsigned long hpte[2];
1357 if (!access_ok(VERIFY_WRITE, buf, count))
1358 return -EFAULT;
1360 first_pass = ctx->first_pass;
1361 flags = ctx->flags;
1363 i = ctx->index;
1364 hptp = (unsigned long *)(kvm->arch.hpt_virt + (i * HPTE_SIZE));
1365 revp = kvm->arch.revmap + i;
1366 lbuf = (unsigned long __user *)buf;
1368 nb = 0;
1369 while (nb + sizeof(hdr) + HPTE_SIZE < count) {
1370 /* Initialize header */
1371 hptr = (struct kvm_get_htab_header __user *)buf;
1372 hdr.n_valid = 0;
1373 hdr.n_invalid = 0;
1374 nw = nb;
1375 nb += sizeof(hdr);
1376 lbuf = (unsigned long __user *)(buf + sizeof(hdr));
1378 /* Skip uninteresting entries, i.e. clean on not-first pass */
1379 if (!first_pass) {
1380 while (i < kvm->arch.hpt_npte &&
1381 !hpte_dirty(revp, hptp)) {
1382 ++i;
1383 hptp += 2;
1384 ++revp;
1387 hdr.index = i;
1389 /* Grab a series of valid entries */
1390 while (i < kvm->arch.hpt_npte &&
1391 hdr.n_valid < 0xffff &&
1392 nb + HPTE_SIZE < count &&
1393 record_hpte(flags, hptp, hpte, revp, 1, first_pass)) {
1394 /* valid entry, write it out */
1395 ++hdr.n_valid;
1396 if (__put_user(hpte[0], lbuf) ||
1397 __put_user(hpte[1], lbuf + 1))
1398 return -EFAULT;
1399 nb += HPTE_SIZE;
1400 lbuf += 2;
1401 ++i;
1402 hptp += 2;
1403 ++revp;
1405 /* Now skip invalid entries while we can */
1406 while (i < kvm->arch.hpt_npte &&
1407 hdr.n_invalid < 0xffff &&
1408 record_hpte(flags, hptp, hpte, revp, 0, first_pass)) {
1409 /* found an invalid entry */
1410 ++hdr.n_invalid;
1411 ++i;
1412 hptp += 2;
1413 ++revp;
1416 if (hdr.n_valid || hdr.n_invalid) {
1417 /* write back the header */
1418 if (__copy_to_user(hptr, &hdr, sizeof(hdr)))
1419 return -EFAULT;
1420 nw = nb;
1421 buf = (char __user *)lbuf;
1422 } else {
1423 nb = nw;
1426 /* Check if we've wrapped around the hash table */
1427 if (i >= kvm->arch.hpt_npte) {
1428 i = 0;
1429 ctx->first_pass = 0;
1430 break;
1434 ctx->index = i;
1436 return nb;
1439 static ssize_t kvm_htab_write(struct file *file, const char __user *buf,
1440 size_t count, loff_t *ppos)
1442 struct kvm_htab_ctx *ctx = file->private_data;
1443 struct kvm *kvm = ctx->kvm;
1444 struct kvm_get_htab_header hdr;
1445 unsigned long i, j;
1446 unsigned long v, r;
1447 unsigned long __user *lbuf;
1448 unsigned long *hptp;
1449 unsigned long tmp[2];
1450 ssize_t nb;
1451 long int err, ret;
1452 int rma_setup;
1454 if (!access_ok(VERIFY_READ, buf, count))
1455 return -EFAULT;
1457 /* lock out vcpus from running while we're doing this */
1458 mutex_lock(&kvm->lock);
1459 rma_setup = kvm->arch.rma_setup_done;
1460 if (rma_setup) {
1461 kvm->arch.rma_setup_done = 0; /* temporarily */
1462 /* order rma_setup_done vs. vcpus_running */
1463 smp_mb();
1464 if (atomic_read(&kvm->arch.vcpus_running)) {
1465 kvm->arch.rma_setup_done = 1;
1466 mutex_unlock(&kvm->lock);
1467 return -EBUSY;
1471 err = 0;
1472 for (nb = 0; nb + sizeof(hdr) <= count; ) {
1473 err = -EFAULT;
1474 if (__copy_from_user(&hdr, buf, sizeof(hdr)))
1475 break;
1477 err = 0;
1478 if (nb + hdr.n_valid * HPTE_SIZE > count)
1479 break;
1481 nb += sizeof(hdr);
1482 buf += sizeof(hdr);
1484 err = -EINVAL;
1485 i = hdr.index;
1486 if (i >= kvm->arch.hpt_npte ||
1487 i + hdr.n_valid + hdr.n_invalid > kvm->arch.hpt_npte)
1488 break;
1490 hptp = (unsigned long *)(kvm->arch.hpt_virt + (i * HPTE_SIZE));
1491 lbuf = (unsigned long __user *)buf;
1492 for (j = 0; j < hdr.n_valid; ++j) {
1493 err = -EFAULT;
1494 if (__get_user(v, lbuf) || __get_user(r, lbuf + 1))
1495 goto out;
1496 err = -EINVAL;
1497 if (!(v & HPTE_V_VALID))
1498 goto out;
1499 lbuf += 2;
1500 nb += HPTE_SIZE;
1502 if (hptp[0] & (HPTE_V_VALID | HPTE_V_ABSENT))
1503 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1504 err = -EIO;
1505 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r,
1506 tmp);
1507 if (ret != H_SUCCESS) {
1508 pr_err("kvm_htab_write ret %ld i=%ld v=%lx "
1509 "r=%lx\n", ret, i, v, r);
1510 goto out;
1512 if (!rma_setup && is_vrma_hpte(v)) {
1513 unsigned long psize = hpte_page_size(v, r);
1514 unsigned long senc = slb_pgsize_encoding(psize);
1515 unsigned long lpcr;
1517 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1518 (VRMA_VSID << SLB_VSID_SHIFT_1T);
1519 lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
1520 lpcr |= senc << (LPCR_VRMASD_SH - 4);
1521 kvm->arch.lpcr = lpcr;
1522 rma_setup = 1;
1524 ++i;
1525 hptp += 2;
1528 for (j = 0; j < hdr.n_invalid; ++j) {
1529 if (hptp[0] & (HPTE_V_VALID | HPTE_V_ABSENT))
1530 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1531 ++i;
1532 hptp += 2;
1534 err = 0;
1537 out:
1538 /* Order HPTE updates vs. rma_setup_done */
1539 smp_wmb();
1540 kvm->arch.rma_setup_done = rma_setup;
1541 mutex_unlock(&kvm->lock);
1543 if (err)
1544 return err;
1545 return nb;
1548 static int kvm_htab_release(struct inode *inode, struct file *filp)
1550 struct kvm_htab_ctx *ctx = filp->private_data;
1552 filp->private_data = NULL;
1553 if (!(ctx->flags & KVM_GET_HTAB_WRITE))
1554 atomic_dec(&ctx->kvm->arch.hpte_mod_interest);
1555 kvm_put_kvm(ctx->kvm);
1556 kfree(ctx);
1557 return 0;
1560 static const struct file_operations kvm_htab_fops = {
1561 .read = kvm_htab_read,
1562 .write = kvm_htab_write,
1563 .llseek = default_llseek,
1564 .release = kvm_htab_release,
1567 int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf)
1569 int ret;
1570 struct kvm_htab_ctx *ctx;
1571 int rwflag;
1573 /* reject flags we don't recognize */
1574 if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE))
1575 return -EINVAL;
1576 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1577 if (!ctx)
1578 return -ENOMEM;
1579 kvm_get_kvm(kvm);
1580 ctx->kvm = kvm;
1581 ctx->index = ghf->start_index;
1582 ctx->flags = ghf->flags;
1583 ctx->first_pass = 1;
1585 rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY;
1586 ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC);
1587 if (ret < 0) {
1588 kvm_put_kvm(kvm);
1589 return ret;
1592 if (rwflag == O_RDONLY) {
1593 mutex_lock(&kvm->slots_lock);
1594 atomic_inc(&kvm->arch.hpte_mod_interest);
1595 /* make sure kvmppc_do_h_enter etc. see the increment */
1596 synchronize_srcu_expedited(&kvm->srcu);
1597 mutex_unlock(&kvm->slots_lock);
1600 return ret;
1603 void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
1605 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
1607 if (cpu_has_feature(CPU_FTR_ARCH_206))
1608 vcpu->arch.slb_nr = 32; /* POWER7 */
1609 else
1610 vcpu->arch.slb_nr = 64;
1612 mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
1613 mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
1615 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;