KVM: PPC: Book3S HV: Add ICP real mode counters
[linux-2.6-block.git] / arch / powerpc / kvm / book3s_64_mmu_hv.c
CommitLineData
de56a948
PM
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.
5 *
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.
10 *
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.
14 *
15 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
16 */
17
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>
8936dda4 26#include <linux/vmalloc.h>
2c9097e4 27#include <linux/srcu.h>
a2932923
PM
28#include <linux/anon_inodes.h>
29#include <linux/file.h>
de56a948
PM
30
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>
39
3c78f78a
SW
40#include "trace_hv.h"
41
32fad281
PM
42/* Power architecture requires HPT is at least 256kB */
43#define PPC_MIN_HPT_ORDER 18
44
7ed661bf
PM
45static 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);
a64fd707 48static void kvmppc_rmap_reset(struct kvm *kvm);
7ed661bf 49
32fad281 50long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp)
de56a948 51{
792fc497 52 unsigned long hpt = 0;
8936dda4 53 struct revmap_entry *rev;
fa61a4e3
AK
54 struct page *page = NULL;
55 long order = KVM_DEFAULT_HPT_ORDER;
de56a948 56
32fad281
PM
57 if (htab_orderp) {
58 order = *htab_orderp;
59 if (order < PPC_MIN_HPT_ORDER)
60 order = PPC_MIN_HPT_ORDER;
61 }
62
fa61a4e3 63 kvm->arch.hpt_cma_alloc = 0;
02a68d05 64 page = kvm_alloc_hpt(1ul << (order - PAGE_SHIFT));
792fc497
AK
65 if (page) {
66 hpt = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
02a68d05 67 memset((void *)hpt, 0, (1ul << order));
792fc497 68 kvm->arch.hpt_cma_alloc = 1;
de56a948 69 }
32fad281
PM
70
71 /* Lastly try successively smaller sizes from the page allocator */
72 while (!hpt && order > PPC_MIN_HPT_ORDER) {
73 hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
74 __GFP_NOWARN, order - PAGE_SHIFT);
75 if (!hpt)
76 --order;
77 }
78
79 if (!hpt)
80 return -ENOMEM;
81
de56a948 82 kvm->arch.hpt_virt = hpt;
32fad281
PM
83 kvm->arch.hpt_order = order;
84 /* HPTEs are 2**4 bytes long */
85 kvm->arch.hpt_npte = 1ul << (order - 4);
86 /* 128 (2**7) bytes in each HPTEG */
87 kvm->arch.hpt_mask = (1ul << (order - 7)) - 1;
de56a948 88
8936dda4 89 /* Allocate reverse map array */
32fad281 90 rev = vmalloc(sizeof(struct revmap_entry) * kvm->arch.hpt_npte);
8936dda4
PM
91 if (!rev) {
92 pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
93 goto out_freehpt;
94 }
95 kvm->arch.revmap = rev;
32fad281 96 kvm->arch.sdr1 = __pa(hpt) | (order - 18);
8936dda4 97
32fad281
PM
98 pr_info("KVM guest htab at %lx (order %ld), LPID %x\n",
99 hpt, order, kvm->arch.lpid);
de56a948 100
32fad281
PM
101 if (htab_orderp)
102 *htab_orderp = order;
de56a948 103 return 0;
8936dda4 104
8936dda4 105 out_freehpt:
fa61a4e3
AK
106 if (kvm->arch.hpt_cma_alloc)
107 kvm_release_hpt(page, 1 << (order - PAGE_SHIFT));
32fad281
PM
108 else
109 free_pages(hpt, order - PAGE_SHIFT);
8936dda4 110 return -ENOMEM;
de56a948
PM
111}
112
32fad281
PM
113long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp)
114{
115 long err = -EBUSY;
116 long order;
117
118 mutex_lock(&kvm->lock);
31037eca
AK
119 if (kvm->arch.hpte_setup_done) {
120 kvm->arch.hpte_setup_done = 0;
121 /* order hpte_setup_done vs. vcpus_running */
32fad281
PM
122 smp_mb();
123 if (atomic_read(&kvm->arch.vcpus_running)) {
31037eca 124 kvm->arch.hpte_setup_done = 1;
32fad281
PM
125 goto out;
126 }
127 }
128 if (kvm->arch.hpt_virt) {
129 order = kvm->arch.hpt_order;
130 /* Set the entire HPT to 0, i.e. invalid HPTEs */
131 memset((void *)kvm->arch.hpt_virt, 0, 1ul << order);
a64fd707
PM
132 /*
133 * Reset all the reverse-mapping chains for all memslots
134 */
135 kvmppc_rmap_reset(kvm);
1b400ba0
PM
136 /* Ensure that each vcpu will flush its TLB on next entry. */
137 cpumask_setall(&kvm->arch.need_tlb_flush);
32fad281
PM
138 *htab_orderp = order;
139 err = 0;
140 } else {
141 err = kvmppc_alloc_hpt(kvm, htab_orderp);
142 order = *htab_orderp;
143 }
144 out:
145 mutex_unlock(&kvm->lock);
146 return err;
147}
148
de56a948
PM
149void kvmppc_free_hpt(struct kvm *kvm)
150{
043cc4d7 151 kvmppc_free_lpid(kvm->arch.lpid);
8936dda4 152 vfree(kvm->arch.revmap);
fa61a4e3
AK
153 if (kvm->arch.hpt_cma_alloc)
154 kvm_release_hpt(virt_to_page(kvm->arch.hpt_virt),
155 1 << (kvm->arch.hpt_order - PAGE_SHIFT));
d2a1b483 156 else
32fad281
PM
157 free_pages(kvm->arch.hpt_virt,
158 kvm->arch.hpt_order - PAGE_SHIFT);
de56a948
PM
159}
160
da9d1d7f
PM
161/* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
162static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
163{
164 return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
165}
166
167/* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
168static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
169{
170 return (pgsize == 0x10000) ? 0x1000 : 0;
171}
172
173void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
174 unsigned long porder)
de56a948
PM
175{
176 unsigned long i;
b2b2f165 177 unsigned long npages;
c77162de
PM
178 unsigned long hp_v, hp_r;
179 unsigned long addr, hash;
da9d1d7f
PM
180 unsigned long psize;
181 unsigned long hp0, hp1;
7ed661bf 182 unsigned long idx_ret;
c77162de 183 long ret;
32fad281 184 struct kvm *kvm = vcpu->kvm;
de56a948 185
da9d1d7f
PM
186 psize = 1ul << porder;
187 npages = memslot->npages >> (porder - PAGE_SHIFT);
de56a948
PM
188
189 /* VRMA can't be > 1TB */
8936dda4
PM
190 if (npages > 1ul << (40 - porder))
191 npages = 1ul << (40 - porder);
de56a948 192 /* Can't use more than 1 HPTE per HPTEG */
32fad281
PM
193 if (npages > kvm->arch.hpt_mask + 1)
194 npages = kvm->arch.hpt_mask + 1;
de56a948 195
da9d1d7f
PM
196 hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
197 HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
198 hp1 = hpte1_pgsize_encoding(psize) |
199 HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
200
de56a948 201 for (i = 0; i < npages; ++i) {
c77162de 202 addr = i << porder;
de56a948 203 /* can't use hpt_hash since va > 64 bits */
32fad281 204 hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & kvm->arch.hpt_mask;
de56a948
PM
205 /*
206 * We assume that the hash table is empty and no
207 * vcpus are using it at this stage. Since we create
208 * at most one HPTE per HPTEG, we just assume entry 7
209 * is available and use it.
210 */
8936dda4 211 hash = (hash << 3) + 7;
da9d1d7f
PM
212 hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
213 hp_r = hp1 | addr;
7ed661bf
PM
214 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r,
215 &idx_ret);
c77162de
PM
216 if (ret != H_SUCCESS) {
217 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
218 addr, ret);
219 break;
220 }
de56a948
PM
221 }
222}
223
224int kvmppc_mmu_hv_init(void)
225{
9e368f29
PM
226 unsigned long host_lpid, rsvd_lpid;
227
228 if (!cpu_has_feature(CPU_FTR_HVMODE))
de56a948 229 return -EINVAL;
9e368f29 230
c17b98cf
PM
231 /* POWER7 has 10-bit LPIDs (12-bit in POWER8) */
232 host_lpid = mfspr(SPRN_LPID);
233 rsvd_lpid = LPID_RSVD;
9e368f29 234
043cc4d7
SW
235 kvmppc_init_lpid(rsvd_lpid + 1);
236
237 kvmppc_claim_lpid(host_lpid);
9e368f29 238 /* rsvd_lpid is reserved for use in partition switching */
043cc4d7 239 kvmppc_claim_lpid(rsvd_lpid);
de56a948
PM
240
241 return 0;
242}
243
de56a948
PM
244static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
245{
e4e38121
MN
246 unsigned long msr = vcpu->arch.intr_msr;
247
248 /* If transactional, change to suspend mode on IRQ delivery */
249 if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr))
250 msr |= MSR_TS_S;
251 else
252 msr |= vcpu->arch.shregs.msr & MSR_TS_MASK;
253 kvmppc_set_msr(vcpu, msr);
de56a948
PM
254}
255
7ed661bf
PM
256long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
257 long pte_index, unsigned long pteh,
258 unsigned long ptel, unsigned long *pte_idx_ret)
c77162de 259{
c77162de
PM
260 long ret;
261
342d3db7
PM
262 /* Protect linux PTE lookup from page table destruction */
263 rcu_read_lock_sched(); /* this disables preemption too */
7ed661bf
PM
264 ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
265 current->mm->pgd, false, pte_idx_ret);
342d3db7 266 rcu_read_unlock_sched();
c77162de
PM
267 if (ret == H_TOO_HARD) {
268 /* this can't happen */
269 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
270 ret = H_RESOURCE; /* or something */
271 }
272 return ret;
273
274}
275
697d3899
PM
276static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
277 gva_t eaddr)
278{
279 u64 mask;
280 int i;
281
282 for (i = 0; i < vcpu->arch.slb_nr; i++) {
283 if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
284 continue;
285
286 if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
287 mask = ESID_MASK_1T;
288 else
289 mask = ESID_MASK;
290
291 if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
292 return &vcpu->arch.slb[i];
293 }
294 return NULL;
295}
296
297static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
298 unsigned long ea)
299{
300 unsigned long ra_mask;
301
302 ra_mask = hpte_page_size(v, r) - 1;
303 return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
304}
305
de56a948 306static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
93b159b4 307 struct kvmppc_pte *gpte, bool data, bool iswrite)
de56a948 308{
697d3899
PM
309 struct kvm *kvm = vcpu->kvm;
310 struct kvmppc_slb *slbe;
311 unsigned long slb_v;
312 unsigned long pp, key;
313 unsigned long v, gr;
6f22bd32 314 __be64 *hptep;
697d3899
PM
315 int index;
316 int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
317
318 /* Get SLB entry */
319 if (virtmode) {
320 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
321 if (!slbe)
322 return -EINVAL;
323 slb_v = slbe->origv;
324 } else {
325 /* real mode access */
326 slb_v = vcpu->kvm->arch.vrma_slb_v;
327 }
328
91648ec0 329 preempt_disable();
697d3899
PM
330 /* Find the HPTE in the hash table */
331 index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
332 HPTE_V_VALID | HPTE_V_ABSENT);
91648ec0 333 if (index < 0) {
334 preempt_enable();
697d3899 335 return -ENOENT;
91648ec0 336 }
6f22bd32
AG
337 hptep = (__be64 *)(kvm->arch.hpt_virt + (index << 4));
338 v = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
697d3899
PM
339 gr = kvm->arch.revmap[index].guest_rpte;
340
a4bd6eb0 341 unlock_hpte(hptep, v);
91648ec0 342 preempt_enable();
697d3899
PM
343
344 gpte->eaddr = eaddr;
345 gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
346
347 /* Get PP bits and key for permission check */
348 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
349 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
350 key &= slb_v;
351
352 /* Calculate permissions */
353 gpte->may_read = hpte_read_permission(pp, key);
354 gpte->may_write = hpte_write_permission(pp, key);
355 gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
356
357 /* Storage key permission check for POWER7 */
c17b98cf 358 if (data && virtmode) {
697d3899
PM
359 int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
360 if (amrfield & 1)
361 gpte->may_read = 0;
362 if (amrfield & 2)
363 gpte->may_write = 0;
364 }
365
366 /* Get the guest physical address */
367 gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
368 return 0;
369}
370
371/*
372 * Quick test for whether an instruction is a load or a store.
373 * If the instruction is a load or a store, then this will indicate
374 * which it is, at least on server processors. (Embedded processors
375 * have some external PID instructions that don't follow the rule
376 * embodied here.) If the instruction isn't a load or store, then
377 * this doesn't return anything useful.
378 */
379static int instruction_is_store(unsigned int instr)
380{
381 unsigned int mask;
382
383 mask = 0x10000000;
384 if ((instr & 0xfc000000) == 0x7c000000)
385 mask = 0x100; /* major opcode 31 */
386 return (instr & mask) != 0;
387}
388
389static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
6020c0f6 390 unsigned long gpa, gva_t ea, int is_store)
697d3899 391{
697d3899 392 u32 last_inst;
697d3899 393
51f04726 394 /*
697d3899
PM
395 * If we fail, we just return to the guest and try executing it again.
396 */
51f04726
MC
397 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
398 EMULATE_DONE)
399 return RESUME_GUEST;
697d3899
PM
400
401 /*
402 * WARNING: We do not know for sure whether the instruction we just
403 * read from memory is the same that caused the fault in the first
404 * place. If the instruction we read is neither an load or a store,
405 * then it can't access memory, so we don't need to worry about
406 * enforcing access permissions. So, assuming it is a load or
407 * store, we just check that its direction (load or store) is
408 * consistent with the original fault, since that's what we
409 * checked the access permissions against. If there is a mismatch
410 * we just return and retry the instruction.
411 */
412
51f04726 413 if (instruction_is_store(last_inst) != !!is_store)
697d3899
PM
414 return RESUME_GUEST;
415
416 /*
417 * Emulated accesses are emulated by looking at the hash for
418 * translation once, then performing the access later. The
419 * translation could be invalidated in the meantime in which
420 * point performing the subsequent memory access on the old
421 * physical address could possibly be a security hole for the
422 * guest (but not the host).
423 *
424 * This is less of an issue for MMIO stores since they aren't
425 * globally visible. It could be an issue for MMIO loads to
426 * a certain extent but we'll ignore it for now.
427 */
428
429 vcpu->arch.paddr_accessed = gpa;
6020c0f6 430 vcpu->arch.vaddr_accessed = ea;
697d3899
PM
431 return kvmppc_emulate_mmio(run, vcpu);
432}
433
434int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
435 unsigned long ea, unsigned long dsisr)
436{
437 struct kvm *kvm = vcpu->kvm;
6f22bd32
AG
438 unsigned long hpte[3], r;
439 __be64 *hptep;
342d3db7 440 unsigned long mmu_seq, psize, pte_size;
1066f772 441 unsigned long gpa_base, gfn_base;
70bddfef 442 unsigned long gpa, gfn, hva, pfn;
697d3899 443 struct kvm_memory_slot *memslot;
342d3db7 444 unsigned long *rmap;
697d3899 445 struct revmap_entry *rev;
342d3db7
PM
446 struct page *page, *pages[1];
447 long index, ret, npages;
448 unsigned long is_io;
4cf302bc 449 unsigned int writing, write_ok;
342d3db7 450 struct vm_area_struct *vma;
bad3b507 451 unsigned long rcbits;
697d3899
PM
452
453 /*
454 * Real-mode code has already searched the HPT and found the
455 * entry we're interested in. Lock the entry and check that
456 * it hasn't changed. If it has, just return and re-execute the
457 * instruction.
458 */
459 if (ea != vcpu->arch.pgfault_addr)
460 return RESUME_GUEST;
461 index = vcpu->arch.pgfault_index;
6f22bd32 462 hptep = (__be64 *)(kvm->arch.hpt_virt + (index << 4));
697d3899
PM
463 rev = &kvm->arch.revmap[index];
464 preempt_disable();
465 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
466 cpu_relax();
6f22bd32
AG
467 hpte[0] = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
468 hpte[1] = be64_to_cpu(hptep[1]);
342d3db7 469 hpte[2] = r = rev->guest_rpte;
a4bd6eb0 470 unlock_hpte(hptep, hpte[0]);
697d3899
PM
471 preempt_enable();
472
473 if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
474 hpte[1] != vcpu->arch.pgfault_hpte[1])
475 return RESUME_GUEST;
476
477 /* Translate the logical address and get the page */
342d3db7 478 psize = hpte_page_size(hpte[0], r);
1066f772
PM
479 gpa_base = r & HPTE_R_RPN & ~(psize - 1);
480 gfn_base = gpa_base >> PAGE_SHIFT;
481 gpa = gpa_base | (ea & (psize - 1));
70bddfef 482 gfn = gpa >> PAGE_SHIFT;
697d3899
PM
483 memslot = gfn_to_memslot(kvm, gfn);
484
3c78f78a
SW
485 trace_kvm_page_fault_enter(vcpu, hpte, memslot, ea, dsisr);
486
697d3899 487 /* No memslot means it's an emulated MMIO region */
70bddfef 488 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
6020c0f6 489 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
697d3899 490 dsisr & DSISR_ISSTORE);
697d3899 491
1066f772
PM
492 /*
493 * This should never happen, because of the slot_is_aligned()
494 * check in kvmppc_do_h_enter().
495 */
496 if (gfn_base < memslot->base_gfn)
497 return -EFAULT;
498
342d3db7
PM
499 /* used to check for invalidations in progress */
500 mmu_seq = kvm->mmu_notifier_seq;
501 smp_rmb();
502
3c78f78a 503 ret = -EFAULT;
342d3db7
PM
504 is_io = 0;
505 pfn = 0;
506 page = NULL;
507 pte_size = PAGE_SIZE;
4cf302bc
PM
508 writing = (dsisr & DSISR_ISSTORE) != 0;
509 /* If writing != 0, then the HPTE must allow writing, if we get here */
510 write_ok = writing;
342d3db7 511 hva = gfn_to_hva_memslot(memslot, gfn);
4cf302bc 512 npages = get_user_pages_fast(hva, 1, writing, pages);
342d3db7
PM
513 if (npages < 1) {
514 /* Check if it's an I/O mapping */
515 down_read(&current->mm->mmap_sem);
516 vma = find_vma(current->mm, hva);
517 if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
518 (vma->vm_flags & VM_PFNMAP)) {
519 pfn = vma->vm_pgoff +
520 ((hva - vma->vm_start) >> PAGE_SHIFT);
521 pte_size = psize;
522 is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
4cf302bc 523 write_ok = vma->vm_flags & VM_WRITE;
342d3db7
PM
524 }
525 up_read(&current->mm->mmap_sem);
526 if (!pfn)
3c78f78a 527 goto out_put;
342d3db7
PM
528 } else {
529 page = pages[0];
caaa4c80 530 pfn = page_to_pfn(page);
342d3db7
PM
531 if (PageHuge(page)) {
532 page = compound_head(page);
533 pte_size <<= compound_order(page);
534 }
4cf302bc
PM
535 /* if the guest wants write access, see if that is OK */
536 if (!writing && hpte_is_writable(r)) {
db7cb5b9 537 unsigned int hugepage_shift;
4cf302bc
PM
538 pte_t *ptep, pte;
539
540 /*
541 * We need to protect against page table destruction
542 * while looking up and updating the pte.
543 */
544 rcu_read_lock_sched();
545 ptep = find_linux_pte_or_hugepte(current->mm->pgd,
db7cb5b9
AK
546 hva, &hugepage_shift);
547 if (ptep) {
548 pte = kvmppc_read_update_linux_pte(ptep, 1,
549 hugepage_shift);
4cf302bc
PM
550 if (pte_write(pte))
551 write_ok = 1;
552 }
553 rcu_read_unlock_sched();
554 }
342d3db7
PM
555 }
556
342d3db7
PM
557 if (psize > pte_size)
558 goto out_put;
559
560 /* Check WIMG vs. the actual page we're accessing */
561 if (!hpte_cache_flags_ok(r, is_io)) {
562 if (is_io)
3c78f78a
SW
563 goto out_put;
564
342d3db7
PM
565 /*
566 * Allow guest to map emulated device memory as
567 * uncacheable, but actually make it cacheable.
568 */
569 r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
570 }
571
caaa4c80
PM
572 /*
573 * Set the HPTE to point to pfn.
574 * Since the pfn is at PAGE_SIZE granularity, make sure we
575 * don't mask out lower-order bits if psize < PAGE_SIZE.
576 */
577 if (psize < PAGE_SIZE)
578 psize = PAGE_SIZE;
579 r = (r & ~(HPTE_R_PP0 - psize)) | ((pfn << PAGE_SHIFT) & ~(psize - 1));
4cf302bc
PM
580 if (hpte_is_writable(r) && !write_ok)
581 r = hpte_make_readonly(r);
342d3db7
PM
582 ret = RESUME_GUEST;
583 preempt_disable();
584 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
585 cpu_relax();
6f22bd32
AG
586 if ((be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK) != hpte[0] ||
587 be64_to_cpu(hptep[1]) != hpte[1] ||
588 rev->guest_rpte != hpte[2])
342d3db7
PM
589 /* HPTE has been changed under us; let the guest retry */
590 goto out_unlock;
591 hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
592
1066f772
PM
593 /* Always put the HPTE in the rmap chain for the page base address */
594 rmap = &memslot->arch.rmap[gfn_base - memslot->base_gfn];
342d3db7
PM
595 lock_rmap(rmap);
596
597 /* Check if we might have been invalidated; let the guest retry if so */
598 ret = RESUME_GUEST;
8ca40a70 599 if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) {
342d3db7
PM
600 unlock_rmap(rmap);
601 goto out_unlock;
602 }
4cf302bc 603
bad3b507
PM
604 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
605 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
606 r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
607
6f22bd32 608 if (be64_to_cpu(hptep[0]) & HPTE_V_VALID) {
4cf302bc
PM
609 /* HPTE was previously valid, so we need to invalidate it */
610 unlock_rmap(rmap);
6f22bd32 611 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
4cf302bc 612 kvmppc_invalidate_hpte(kvm, hptep, index);
bad3b507 613 /* don't lose previous R and C bits */
6f22bd32 614 r |= be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
4cf302bc
PM
615 } else {
616 kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
617 }
342d3db7 618
6f22bd32 619 hptep[1] = cpu_to_be64(r);
342d3db7 620 eieio();
a4bd6eb0 621 __unlock_hpte(hptep, hpte[0]);
342d3db7
PM
622 asm volatile("ptesync" : : : "memory");
623 preempt_enable();
4cf302bc 624 if (page && hpte_is_writable(r))
342d3db7
PM
625 SetPageDirty(page);
626
627 out_put:
3c78f78a
SW
628 trace_kvm_page_fault_exit(vcpu, hpte, ret);
629
de6c0b02
DG
630 if (page) {
631 /*
632 * We drop pages[0] here, not page because page might
633 * have been set to the head page of a compound, but
634 * we have to drop the reference on the correct tail
635 * page to match the get inside gup()
636 */
637 put_page(pages[0]);
638 }
342d3db7
PM
639 return ret;
640
641 out_unlock:
a4bd6eb0 642 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
342d3db7
PM
643 preempt_enable();
644 goto out_put;
645}
646
a64fd707
PM
647static void kvmppc_rmap_reset(struct kvm *kvm)
648{
649 struct kvm_memslots *slots;
650 struct kvm_memory_slot *memslot;
651 int srcu_idx;
652
653 srcu_idx = srcu_read_lock(&kvm->srcu);
654 slots = kvm->memslots;
655 kvm_for_each_memslot(memslot, slots) {
656 /*
657 * This assumes it is acceptable to lose reference and
658 * change bits across a reset.
659 */
660 memset(memslot->arch.rmap, 0,
661 memslot->npages * sizeof(*memslot->arch.rmap));
662 }
663 srcu_read_unlock(&kvm->srcu, srcu_idx);
664}
665
84504ef3
TY
666static int kvm_handle_hva_range(struct kvm *kvm,
667 unsigned long start,
668 unsigned long end,
669 int (*handler)(struct kvm *kvm,
670 unsigned long *rmapp,
671 unsigned long gfn))
342d3db7
PM
672{
673 int ret;
674 int retval = 0;
675 struct kvm_memslots *slots;
676 struct kvm_memory_slot *memslot;
677
678 slots = kvm_memslots(kvm);
679 kvm_for_each_memslot(memslot, slots) {
84504ef3
TY
680 unsigned long hva_start, hva_end;
681 gfn_t gfn, gfn_end;
682
683 hva_start = max(start, memslot->userspace_addr);
684 hva_end = min(end, memslot->userspace_addr +
685 (memslot->npages << PAGE_SHIFT));
686 if (hva_start >= hva_end)
687 continue;
688 /*
689 * {gfn(page) | page intersects with [hva_start, hva_end)} =
690 * {gfn, gfn+1, ..., gfn_end-1}.
691 */
692 gfn = hva_to_gfn_memslot(hva_start, memslot);
693 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
342d3db7 694
84504ef3 695 for (; gfn < gfn_end; ++gfn) {
d19a748b 696 gfn_t gfn_offset = gfn - memslot->base_gfn;
342d3db7 697
d89cc617 698 ret = handler(kvm, &memslot->arch.rmap[gfn_offset], gfn);
342d3db7
PM
699 retval |= ret;
700 }
701 }
702
703 return retval;
704}
705
84504ef3
TY
706static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
707 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
708 unsigned long gfn))
709{
710 return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
711}
712
342d3db7
PM
713static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
714 unsigned long gfn)
715{
716 struct revmap_entry *rev = kvm->arch.revmap;
717 unsigned long h, i, j;
6f22bd32 718 __be64 *hptep;
bad3b507 719 unsigned long ptel, psize, rcbits;
342d3db7
PM
720
721 for (;;) {
bad3b507 722 lock_rmap(rmapp);
342d3db7 723 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
bad3b507 724 unlock_rmap(rmapp);
342d3db7
PM
725 break;
726 }
727
728 /*
729 * To avoid an ABBA deadlock with the HPTE lock bit,
bad3b507
PM
730 * we can't spin on the HPTE lock while holding the
731 * rmap chain lock.
342d3db7
PM
732 */
733 i = *rmapp & KVMPPC_RMAP_INDEX;
6f22bd32 734 hptep = (__be64 *) (kvm->arch.hpt_virt + (i << 4));
bad3b507
PM
735 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
736 /* unlock rmap before spinning on the HPTE lock */
737 unlock_rmap(rmapp);
6f22bd32 738 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
bad3b507
PM
739 cpu_relax();
740 continue;
741 }
342d3db7
PM
742 j = rev[i].forw;
743 if (j == i) {
744 /* chain is now empty */
bad3b507 745 *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
342d3db7
PM
746 } else {
747 /* remove i from chain */
748 h = rev[i].back;
749 rev[h].forw = j;
750 rev[j].back = h;
751 rev[i].forw = rev[i].back = i;
bad3b507 752 *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
342d3db7 753 }
342d3db7 754
bad3b507 755 /* Now check and modify the HPTE */
342d3db7 756 ptel = rev[i].guest_rpte;
6f22bd32
AG
757 psize = hpte_page_size(be64_to_cpu(hptep[0]), ptel);
758 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
342d3db7 759 hpte_rpn(ptel, psize) == gfn) {
c17b98cf 760 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
bad3b507
PM
761 kvmppc_invalidate_hpte(kvm, hptep, i);
762 /* Harvest R and C */
6f22bd32 763 rcbits = be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
bad3b507 764 *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
a1b4a0f6
PM
765 if (rcbits & ~rev[i].guest_rpte) {
766 rev[i].guest_rpte = ptel | rcbits;
767 note_hpte_modification(kvm, &rev[i]);
768 }
342d3db7 769 }
bad3b507 770 unlock_rmap(rmapp);
a4bd6eb0 771 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
342d3db7
PM
772 }
773 return 0;
774}
775
3a167bea 776int kvm_unmap_hva_hv(struct kvm *kvm, unsigned long hva)
342d3db7 777{
c17b98cf 778 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
342d3db7
PM
779 return 0;
780}
781
3a167bea 782int kvm_unmap_hva_range_hv(struct kvm *kvm, unsigned long start, unsigned long end)
b3ae2096 783{
c17b98cf 784 kvm_handle_hva_range(kvm, start, end, kvm_unmap_rmapp);
b3ae2096
TY
785 return 0;
786}
787
3a167bea
AK
788void kvmppc_core_flush_memslot_hv(struct kvm *kvm,
789 struct kvm_memory_slot *memslot)
dfe49dbd
PM
790{
791 unsigned long *rmapp;
792 unsigned long gfn;
793 unsigned long n;
794
795 rmapp = memslot->arch.rmap;
796 gfn = memslot->base_gfn;
797 for (n = memslot->npages; n; --n) {
798 /*
799 * Testing the present bit without locking is OK because
800 * the memslot has been marked invalid already, and hence
801 * no new HPTEs referencing this page can be created,
802 * thus the present bit can't go from 0 to 1.
803 */
804 if (*rmapp & KVMPPC_RMAP_PRESENT)
805 kvm_unmap_rmapp(kvm, rmapp, gfn);
806 ++rmapp;
807 ++gfn;
808 }
809}
810
342d3db7
PM
811static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
812 unsigned long gfn)
813{
55514893
PM
814 struct revmap_entry *rev = kvm->arch.revmap;
815 unsigned long head, i, j;
6f22bd32 816 __be64 *hptep;
55514893
PM
817 int ret = 0;
818
819 retry:
820 lock_rmap(rmapp);
821 if (*rmapp & KVMPPC_RMAP_REFERENCED) {
822 *rmapp &= ~KVMPPC_RMAP_REFERENCED;
823 ret = 1;
824 }
825 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
826 unlock_rmap(rmapp);
827 return ret;
828 }
829
830 i = head = *rmapp & KVMPPC_RMAP_INDEX;
831 do {
6f22bd32 832 hptep = (__be64 *) (kvm->arch.hpt_virt + (i << 4));
55514893
PM
833 j = rev[i].forw;
834
835 /* If this HPTE isn't referenced, ignore it */
6f22bd32 836 if (!(be64_to_cpu(hptep[1]) & HPTE_R_R))
55514893
PM
837 continue;
838
839 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
840 /* unlock rmap before spinning on the HPTE lock */
841 unlock_rmap(rmapp);
6f22bd32 842 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
55514893
PM
843 cpu_relax();
844 goto retry;
845 }
846
847 /* Now check and modify the HPTE */
6f22bd32
AG
848 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
849 (be64_to_cpu(hptep[1]) & HPTE_R_R)) {
55514893 850 kvmppc_clear_ref_hpte(kvm, hptep, i);
a1b4a0f6
PM
851 if (!(rev[i].guest_rpte & HPTE_R_R)) {
852 rev[i].guest_rpte |= HPTE_R_R;
853 note_hpte_modification(kvm, &rev[i]);
854 }
55514893
PM
855 ret = 1;
856 }
a4bd6eb0 857 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
55514893
PM
858 } while ((i = j) != head);
859
860 unlock_rmap(rmapp);
861 return ret;
342d3db7
PM
862}
863
57128468 864int kvm_age_hva_hv(struct kvm *kvm, unsigned long start, unsigned long end)
342d3db7 865{
57128468 866 return kvm_handle_hva_range(kvm, start, end, kvm_age_rmapp);
342d3db7
PM
867}
868
869static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
870 unsigned long gfn)
871{
55514893
PM
872 struct revmap_entry *rev = kvm->arch.revmap;
873 unsigned long head, i, j;
874 unsigned long *hp;
875 int ret = 1;
876
877 if (*rmapp & KVMPPC_RMAP_REFERENCED)
878 return 1;
879
880 lock_rmap(rmapp);
881 if (*rmapp & KVMPPC_RMAP_REFERENCED)
882 goto out;
883
884 if (*rmapp & KVMPPC_RMAP_PRESENT) {
885 i = head = *rmapp & KVMPPC_RMAP_INDEX;
886 do {
887 hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4));
888 j = rev[i].forw;
6f22bd32 889 if (be64_to_cpu(hp[1]) & HPTE_R_R)
55514893
PM
890 goto out;
891 } while ((i = j) != head);
892 }
893 ret = 0;
894
895 out:
896 unlock_rmap(rmapp);
897 return ret;
342d3db7
PM
898}
899
3a167bea 900int kvm_test_age_hva_hv(struct kvm *kvm, unsigned long hva)
342d3db7 901{
342d3db7
PM
902 return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
903}
904
3a167bea 905void kvm_set_spte_hva_hv(struct kvm *kvm, unsigned long hva, pte_t pte)
342d3db7 906{
342d3db7 907 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
de56a948
PM
908}
909
6c576e74
PM
910static int vcpus_running(struct kvm *kvm)
911{
912 return atomic_read(&kvm->arch.vcpus_running) != 0;
913}
914
687414be
AK
915/*
916 * Returns the number of system pages that are dirty.
917 * This can be more than 1 if we find a huge-page HPTE.
918 */
919static int kvm_test_clear_dirty_npages(struct kvm *kvm, unsigned long *rmapp)
82ed3616
PM
920{
921 struct revmap_entry *rev = kvm->arch.revmap;
922 unsigned long head, i, j;
687414be 923 unsigned long n;
6c576e74 924 unsigned long v, r;
6f22bd32 925 __be64 *hptep;
687414be 926 int npages_dirty = 0;
82ed3616
PM
927
928 retry:
929 lock_rmap(rmapp);
930 if (*rmapp & KVMPPC_RMAP_CHANGED) {
931 *rmapp &= ~KVMPPC_RMAP_CHANGED;
687414be 932 npages_dirty = 1;
82ed3616
PM
933 }
934 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
935 unlock_rmap(rmapp);
687414be 936 return npages_dirty;
82ed3616
PM
937 }
938
939 i = head = *rmapp & KVMPPC_RMAP_INDEX;
940 do {
6f22bd32
AG
941 unsigned long hptep1;
942 hptep = (__be64 *) (kvm->arch.hpt_virt + (i << 4));
82ed3616
PM
943 j = rev[i].forw;
944
6c576e74
PM
945 /*
946 * Checking the C (changed) bit here is racy since there
947 * is no guarantee about when the hardware writes it back.
948 * If the HPTE is not writable then it is stable since the
949 * page can't be written to, and we would have done a tlbie
950 * (which forces the hardware to complete any writeback)
951 * when making the HPTE read-only.
952 * If vcpus are running then this call is racy anyway
953 * since the page could get dirtied subsequently, so we
954 * expect there to be a further call which would pick up
955 * any delayed C bit writeback.
956 * Otherwise we need to do the tlbie even if C==0 in
957 * order to pick up any delayed writeback of C.
958 */
6f22bd32
AG
959 hptep1 = be64_to_cpu(hptep[1]);
960 if (!(hptep1 & HPTE_R_C) &&
961 (!hpte_is_writable(hptep1) || vcpus_running(kvm)))
82ed3616
PM
962 continue;
963
964 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
965 /* unlock rmap before spinning on the HPTE lock */
966 unlock_rmap(rmapp);
6f22bd32 967 while (hptep[0] & cpu_to_be64(HPTE_V_HVLOCK))
82ed3616
PM
968 cpu_relax();
969 goto retry;
970 }
971
972 /* Now check and modify the HPTE */
f6fb9e84 973 if (!(hptep[0] & cpu_to_be64(HPTE_V_VALID))) {
a4bd6eb0 974 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
6c576e74 975 continue;
f6fb9e84 976 }
6c576e74
PM
977
978 /* need to make it temporarily absent so C is stable */
6f22bd32 979 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
6c576e74 980 kvmppc_invalidate_hpte(kvm, hptep, i);
6f22bd32
AG
981 v = be64_to_cpu(hptep[0]);
982 r = be64_to_cpu(hptep[1]);
6c576e74 983 if (r & HPTE_R_C) {
6f22bd32 984 hptep[1] = cpu_to_be64(r & ~HPTE_R_C);
a1b4a0f6
PM
985 if (!(rev[i].guest_rpte & HPTE_R_C)) {
986 rev[i].guest_rpte |= HPTE_R_C;
987 note_hpte_modification(kvm, &rev[i]);
988 }
6c576e74 989 n = hpte_page_size(v, r);
687414be
AK
990 n = (n + PAGE_SIZE - 1) >> PAGE_SHIFT;
991 if (n > npages_dirty)
992 npages_dirty = n;
6c576e74 993 eieio();
82ed3616 994 }
a4bd6eb0 995 v &= ~HPTE_V_ABSENT;
6c576e74 996 v |= HPTE_V_VALID;
a4bd6eb0 997 __unlock_hpte(hptep, v);
82ed3616
PM
998 } while ((i = j) != head);
999
1000 unlock_rmap(rmapp);
687414be 1001 return npages_dirty;
82ed3616
PM
1002}
1003
c35635ef
PM
1004static void harvest_vpa_dirty(struct kvmppc_vpa *vpa,
1005 struct kvm_memory_slot *memslot,
1006 unsigned long *map)
1007{
1008 unsigned long gfn;
1009
1010 if (!vpa->dirty || !vpa->pinned_addr)
1011 return;
1012 gfn = vpa->gpa >> PAGE_SHIFT;
1013 if (gfn < memslot->base_gfn ||
1014 gfn >= memslot->base_gfn + memslot->npages)
1015 return;
1016
1017 vpa->dirty = false;
1018 if (map)
1019 __set_bit_le(gfn - memslot->base_gfn, map);
1020}
1021
dfe49dbd
PM
1022long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot,
1023 unsigned long *map)
82ed3616 1024{
687414be 1025 unsigned long i, j;
dfe49dbd 1026 unsigned long *rmapp;
c35635ef 1027 struct kvm_vcpu *vcpu;
82ed3616
PM
1028
1029 preempt_disable();
d89cc617 1030 rmapp = memslot->arch.rmap;
82ed3616 1031 for (i = 0; i < memslot->npages; ++i) {
687414be
AK
1032 int npages = kvm_test_clear_dirty_npages(kvm, rmapp);
1033 /*
1034 * Note that if npages > 0 then i must be a multiple of npages,
1035 * since we always put huge-page HPTEs in the rmap chain
1036 * corresponding to their page base address.
1037 */
1038 if (npages && map)
1039 for (j = i; npages; ++j, --npages)
1040 __set_bit_le(j, map);
82ed3616
PM
1041 ++rmapp;
1042 }
c35635ef
PM
1043
1044 /* Harvest dirty bits from VPA and DTL updates */
1045 /* Note: we never modify the SLB shadow buffer areas */
1046 kvm_for_each_vcpu(i, vcpu, kvm) {
1047 spin_lock(&vcpu->arch.vpa_update_lock);
1048 harvest_vpa_dirty(&vcpu->arch.vpa, memslot, map);
1049 harvest_vpa_dirty(&vcpu->arch.dtl, memslot, map);
1050 spin_unlock(&vcpu->arch.vpa_update_lock);
1051 }
82ed3616
PM
1052 preempt_enable();
1053 return 0;
1054}
1055
93e60249
PM
1056void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
1057 unsigned long *nb_ret)
1058{
1059 struct kvm_memory_slot *memslot;
1060 unsigned long gfn = gpa >> PAGE_SHIFT;
342d3db7
PM
1061 struct page *page, *pages[1];
1062 int npages;
c35635ef 1063 unsigned long hva, offset;
2c9097e4 1064 int srcu_idx;
93e60249 1065
2c9097e4 1066 srcu_idx = srcu_read_lock(&kvm->srcu);
93e60249
PM
1067 memslot = gfn_to_memslot(kvm, gfn);
1068 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2c9097e4 1069 goto err;
c17b98cf
PM
1070 hva = gfn_to_hva_memslot(memslot, gfn);
1071 npages = get_user_pages_fast(hva, 1, 1, pages);
1072 if (npages < 1)
1073 goto err;
1074 page = pages[0];
2c9097e4
PM
1075 srcu_read_unlock(&kvm->srcu, srcu_idx);
1076
c35635ef 1077 offset = gpa & (PAGE_SIZE - 1);
93e60249 1078 if (nb_ret)
c35635ef 1079 *nb_ret = PAGE_SIZE - offset;
93e60249 1080 return page_address(page) + offset;
2c9097e4
PM
1081
1082 err:
1083 srcu_read_unlock(&kvm->srcu, srcu_idx);
1084 return NULL;
93e60249
PM
1085}
1086
c35635ef
PM
1087void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa,
1088 bool dirty)
93e60249
PM
1089{
1090 struct page *page = virt_to_page(va);
c35635ef
PM
1091 struct kvm_memory_slot *memslot;
1092 unsigned long gfn;
1093 unsigned long *rmap;
1094 int srcu_idx;
93e60249 1095
93e60249 1096 put_page(page);
c35635ef 1097
c17b98cf 1098 if (!dirty)
c35635ef
PM
1099 return;
1100
1101 /* We need to mark this page dirty in the rmap chain */
1102 gfn = gpa >> PAGE_SHIFT;
1103 srcu_idx = srcu_read_lock(&kvm->srcu);
1104 memslot = gfn_to_memslot(kvm, gfn);
1105 if (memslot) {
1106 rmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
1107 lock_rmap(rmap);
1108 *rmap |= KVMPPC_RMAP_CHANGED;
1109 unlock_rmap(rmap);
1110 }
1111 srcu_read_unlock(&kvm->srcu, srcu_idx);
93e60249
PM
1112}
1113
a2932923
PM
1114/*
1115 * Functions for reading and writing the hash table via reads and
1116 * writes on a file descriptor.
1117 *
1118 * Reads return the guest view of the hash table, which has to be
1119 * pieced together from the real hash table and the guest_rpte
1120 * values in the revmap array.
1121 *
1122 * On writes, each HPTE written is considered in turn, and if it
1123 * is valid, it is written to the HPT as if an H_ENTER with the
1124 * exact flag set was done. When the invalid count is non-zero
1125 * in the header written to the stream, the kernel will make
1126 * sure that that many HPTEs are invalid, and invalidate them
1127 * if not.
1128 */
1129
1130struct kvm_htab_ctx {
1131 unsigned long index;
1132 unsigned long flags;
1133 struct kvm *kvm;
1134 int first_pass;
1135};
1136
1137#define HPTE_SIZE (2 * sizeof(unsigned long))
1138
a1b4a0f6
PM
1139/*
1140 * Returns 1 if this HPT entry has been modified or has pending
1141 * R/C bit changes.
1142 */
6f22bd32 1143static int hpte_dirty(struct revmap_entry *revp, __be64 *hptp)
a1b4a0f6
PM
1144{
1145 unsigned long rcbits_unset;
1146
1147 if (revp->guest_rpte & HPTE_GR_MODIFIED)
1148 return 1;
1149
1150 /* Also need to consider changes in reference and changed bits */
1151 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
6f22bd32
AG
1152 if ((be64_to_cpu(hptp[0]) & HPTE_V_VALID) &&
1153 (be64_to_cpu(hptp[1]) & rcbits_unset))
a1b4a0f6
PM
1154 return 1;
1155
1156 return 0;
1157}
1158
6f22bd32 1159static long record_hpte(unsigned long flags, __be64 *hptp,
a2932923
PM
1160 unsigned long *hpte, struct revmap_entry *revp,
1161 int want_valid, int first_pass)
1162{
1163 unsigned long v, r;
a1b4a0f6 1164 unsigned long rcbits_unset;
a2932923
PM
1165 int ok = 1;
1166 int valid, dirty;
1167
1168 /* Unmodified entries are uninteresting except on the first pass */
a1b4a0f6 1169 dirty = hpte_dirty(revp, hptp);
a2932923
PM
1170 if (!first_pass && !dirty)
1171 return 0;
1172
1173 valid = 0;
6f22bd32 1174 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) {
a2932923
PM
1175 valid = 1;
1176 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) &&
6f22bd32 1177 !(be64_to_cpu(hptp[0]) & HPTE_V_BOLTED))
a2932923
PM
1178 valid = 0;
1179 }
1180 if (valid != want_valid)
1181 return 0;
1182
1183 v = r = 0;
1184 if (valid || dirty) {
1185 /* lock the HPTE so it's stable and read it */
1186 preempt_disable();
1187 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
1188 cpu_relax();
6f22bd32 1189 v = be64_to_cpu(hptp[0]);
a1b4a0f6
PM
1190
1191 /* re-evaluate valid and dirty from synchronized HPTE value */
1192 valid = !!(v & HPTE_V_VALID);
1193 dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
1194
1195 /* Harvest R and C into guest view if necessary */
1196 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
6f22bd32
AG
1197 if (valid && (rcbits_unset & be64_to_cpu(hptp[1]))) {
1198 revp->guest_rpte |= (be64_to_cpu(hptp[1]) &
1199 (HPTE_R_R | HPTE_R_C)) | HPTE_GR_MODIFIED;
a1b4a0f6
PM
1200 dirty = 1;
1201 }
1202
a2932923
PM
1203 if (v & HPTE_V_ABSENT) {
1204 v &= ~HPTE_V_ABSENT;
1205 v |= HPTE_V_VALID;
a1b4a0f6 1206 valid = 1;
a2932923 1207 }
a2932923
PM
1208 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED))
1209 valid = 0;
a1b4a0f6
PM
1210
1211 r = revp->guest_rpte;
a2932923
PM
1212 /* only clear modified if this is the right sort of entry */
1213 if (valid == want_valid && dirty) {
1214 r &= ~HPTE_GR_MODIFIED;
1215 revp->guest_rpte = r;
1216 }
a4bd6eb0 1217 unlock_hpte(hptp, be64_to_cpu(hptp[0]));
a2932923
PM
1218 preempt_enable();
1219 if (!(valid == want_valid && (first_pass || dirty)))
1220 ok = 0;
1221 }
6f22bd32
AG
1222 hpte[0] = cpu_to_be64(v);
1223 hpte[1] = cpu_to_be64(r);
a2932923
PM
1224 return ok;
1225}
1226
1227static ssize_t kvm_htab_read(struct file *file, char __user *buf,
1228 size_t count, loff_t *ppos)
1229{
1230 struct kvm_htab_ctx *ctx = file->private_data;
1231 struct kvm *kvm = ctx->kvm;
1232 struct kvm_get_htab_header hdr;
6f22bd32 1233 __be64 *hptp;
a2932923
PM
1234 struct revmap_entry *revp;
1235 unsigned long i, nb, nw;
1236 unsigned long __user *lbuf;
1237 struct kvm_get_htab_header __user *hptr;
1238 unsigned long flags;
1239 int first_pass;
1240 unsigned long hpte[2];
1241
1242 if (!access_ok(VERIFY_WRITE, buf, count))
1243 return -EFAULT;
1244
1245 first_pass = ctx->first_pass;
1246 flags = ctx->flags;
1247
1248 i = ctx->index;
6f22bd32 1249 hptp = (__be64 *)(kvm->arch.hpt_virt + (i * HPTE_SIZE));
a2932923
PM
1250 revp = kvm->arch.revmap + i;
1251 lbuf = (unsigned long __user *)buf;
1252
1253 nb = 0;
1254 while (nb + sizeof(hdr) + HPTE_SIZE < count) {
1255 /* Initialize header */
1256 hptr = (struct kvm_get_htab_header __user *)buf;
a2932923
PM
1257 hdr.n_valid = 0;
1258 hdr.n_invalid = 0;
1259 nw = nb;
1260 nb += sizeof(hdr);
1261 lbuf = (unsigned long __user *)(buf + sizeof(hdr));
1262
1263 /* Skip uninteresting entries, i.e. clean on not-first pass */
1264 if (!first_pass) {
1265 while (i < kvm->arch.hpt_npte &&
a1b4a0f6 1266 !hpte_dirty(revp, hptp)) {
a2932923
PM
1267 ++i;
1268 hptp += 2;
1269 ++revp;
1270 }
1271 }
05dd85f7 1272 hdr.index = i;
a2932923
PM
1273
1274 /* Grab a series of valid entries */
1275 while (i < kvm->arch.hpt_npte &&
1276 hdr.n_valid < 0xffff &&
1277 nb + HPTE_SIZE < count &&
1278 record_hpte(flags, hptp, hpte, revp, 1, first_pass)) {
1279 /* valid entry, write it out */
1280 ++hdr.n_valid;
1281 if (__put_user(hpte[0], lbuf) ||
1282 __put_user(hpte[1], lbuf + 1))
1283 return -EFAULT;
1284 nb += HPTE_SIZE;
1285 lbuf += 2;
1286 ++i;
1287 hptp += 2;
1288 ++revp;
1289 }
1290 /* Now skip invalid entries while we can */
1291 while (i < kvm->arch.hpt_npte &&
1292 hdr.n_invalid < 0xffff &&
1293 record_hpte(flags, hptp, hpte, revp, 0, first_pass)) {
1294 /* found an invalid entry */
1295 ++hdr.n_invalid;
1296 ++i;
1297 hptp += 2;
1298 ++revp;
1299 }
1300
1301 if (hdr.n_valid || hdr.n_invalid) {
1302 /* write back the header */
1303 if (__copy_to_user(hptr, &hdr, sizeof(hdr)))
1304 return -EFAULT;
1305 nw = nb;
1306 buf = (char __user *)lbuf;
1307 } else {
1308 nb = nw;
1309 }
1310
1311 /* Check if we've wrapped around the hash table */
1312 if (i >= kvm->arch.hpt_npte) {
1313 i = 0;
1314 ctx->first_pass = 0;
1315 break;
1316 }
1317 }
1318
1319 ctx->index = i;
1320
1321 return nb;
1322}
1323
1324static ssize_t kvm_htab_write(struct file *file, const char __user *buf,
1325 size_t count, loff_t *ppos)
1326{
1327 struct kvm_htab_ctx *ctx = file->private_data;
1328 struct kvm *kvm = ctx->kvm;
1329 struct kvm_get_htab_header hdr;
1330 unsigned long i, j;
1331 unsigned long v, r;
1332 unsigned long __user *lbuf;
6f22bd32 1333 __be64 *hptp;
a2932923
PM
1334 unsigned long tmp[2];
1335 ssize_t nb;
1336 long int err, ret;
31037eca 1337 int hpte_setup;
a2932923
PM
1338
1339 if (!access_ok(VERIFY_READ, buf, count))
1340 return -EFAULT;
1341
1342 /* lock out vcpus from running while we're doing this */
1343 mutex_lock(&kvm->lock);
31037eca
AK
1344 hpte_setup = kvm->arch.hpte_setup_done;
1345 if (hpte_setup) {
1346 kvm->arch.hpte_setup_done = 0; /* temporarily */
1347 /* order hpte_setup_done vs. vcpus_running */
a2932923
PM
1348 smp_mb();
1349 if (atomic_read(&kvm->arch.vcpus_running)) {
31037eca 1350 kvm->arch.hpte_setup_done = 1;
a2932923
PM
1351 mutex_unlock(&kvm->lock);
1352 return -EBUSY;
1353 }
1354 }
1355
1356 err = 0;
1357 for (nb = 0; nb + sizeof(hdr) <= count; ) {
1358 err = -EFAULT;
1359 if (__copy_from_user(&hdr, buf, sizeof(hdr)))
1360 break;
1361
1362 err = 0;
1363 if (nb + hdr.n_valid * HPTE_SIZE > count)
1364 break;
1365
1366 nb += sizeof(hdr);
1367 buf += sizeof(hdr);
1368
1369 err = -EINVAL;
1370 i = hdr.index;
1371 if (i >= kvm->arch.hpt_npte ||
1372 i + hdr.n_valid + hdr.n_invalid > kvm->arch.hpt_npte)
1373 break;
1374
6f22bd32 1375 hptp = (__be64 *)(kvm->arch.hpt_virt + (i * HPTE_SIZE));
a2932923
PM
1376 lbuf = (unsigned long __user *)buf;
1377 for (j = 0; j < hdr.n_valid; ++j) {
ffada016
CLG
1378 __be64 hpte_v;
1379 __be64 hpte_r;
1380
a2932923 1381 err = -EFAULT;
ffada016
CLG
1382 if (__get_user(hpte_v, lbuf) ||
1383 __get_user(hpte_r, lbuf + 1))
a2932923 1384 goto out;
ffada016
CLG
1385 v = be64_to_cpu(hpte_v);
1386 r = be64_to_cpu(hpte_r);
a2932923
PM
1387 err = -EINVAL;
1388 if (!(v & HPTE_V_VALID))
1389 goto out;
1390 lbuf += 2;
1391 nb += HPTE_SIZE;
1392
6f22bd32 1393 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
a2932923
PM
1394 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1395 err = -EIO;
1396 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r,
1397 tmp);
1398 if (ret != H_SUCCESS) {
1399 pr_err("kvm_htab_write ret %ld i=%ld v=%lx "
1400 "r=%lx\n", ret, i, v, r);
1401 goto out;
1402 }
31037eca 1403 if (!hpte_setup && is_vrma_hpte(v)) {
341acbb3 1404 unsigned long psize = hpte_base_page_size(v, r);
a2932923
PM
1405 unsigned long senc = slb_pgsize_encoding(psize);
1406 unsigned long lpcr;
1407
1408 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1409 (VRMA_VSID << SLB_VSID_SHIFT_1T);
a0144e2a
PM
1410 lpcr = senc << (LPCR_VRMASD_SH - 4);
1411 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
31037eca 1412 hpte_setup = 1;
a2932923
PM
1413 }
1414 ++i;
1415 hptp += 2;
1416 }
1417
1418 for (j = 0; j < hdr.n_invalid; ++j) {
6f22bd32 1419 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
a2932923
PM
1420 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1421 ++i;
1422 hptp += 2;
1423 }
1424 err = 0;
1425 }
1426
1427 out:
31037eca 1428 /* Order HPTE updates vs. hpte_setup_done */
a2932923 1429 smp_wmb();
31037eca 1430 kvm->arch.hpte_setup_done = hpte_setup;
a2932923
PM
1431 mutex_unlock(&kvm->lock);
1432
1433 if (err)
1434 return err;
1435 return nb;
1436}
1437
1438static int kvm_htab_release(struct inode *inode, struct file *filp)
1439{
1440 struct kvm_htab_ctx *ctx = filp->private_data;
1441
1442 filp->private_data = NULL;
1443 if (!(ctx->flags & KVM_GET_HTAB_WRITE))
1444 atomic_dec(&ctx->kvm->arch.hpte_mod_interest);
1445 kvm_put_kvm(ctx->kvm);
1446 kfree(ctx);
1447 return 0;
1448}
1449
75ef9de1 1450static const struct file_operations kvm_htab_fops = {
a2932923
PM
1451 .read = kvm_htab_read,
1452 .write = kvm_htab_write,
1453 .llseek = default_llseek,
1454 .release = kvm_htab_release,
1455};
1456
1457int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf)
1458{
1459 int ret;
1460 struct kvm_htab_ctx *ctx;
1461 int rwflag;
1462
1463 /* reject flags we don't recognize */
1464 if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE))
1465 return -EINVAL;
1466 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1467 if (!ctx)
1468 return -ENOMEM;
1469 kvm_get_kvm(kvm);
1470 ctx->kvm = kvm;
1471 ctx->index = ghf->start_index;
1472 ctx->flags = ghf->flags;
1473 ctx->first_pass = 1;
1474
1475 rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY;
2f84d5ea 1476 ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC);
a2932923
PM
1477 if (ret < 0) {
1478 kvm_put_kvm(kvm);
1479 return ret;
1480 }
1481
1482 if (rwflag == O_RDONLY) {
1483 mutex_lock(&kvm->slots_lock);
1484 atomic_inc(&kvm->arch.hpte_mod_interest);
1485 /* make sure kvmppc_do_h_enter etc. see the increment */
1486 synchronize_srcu_expedited(&kvm->srcu);
1487 mutex_unlock(&kvm->slots_lock);
1488 }
1489
1490 return ret;
1491}
1492
de56a948
PM
1493void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
1494{
1495 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
1496
c17b98cf 1497 vcpu->arch.slb_nr = 32; /* POWER7/POWER8 */
de56a948
PM
1498
1499 mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
1500 mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
1501
1502 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
1503}