KVM: PPC: Book3S HV: Unify dirty page map between HPT and radix
[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>
e23a808b 30#include <linux/debugfs.h>
de56a948
PM
31
32#include <asm/tlbflush.h>
33#include <asm/kvm_ppc.h>
34#include <asm/kvm_book3s.h>
f64e8084 35#include <asm/book3s/64/mmu-hash.h>
de56a948
PM
36#include <asm/hvcall.h>
37#include <asm/synch.h>
38#include <asm/ppc-opcode.h>
39#include <asm/cputable.h>
94171b19 40#include <asm/pte-walk.h>
de56a948 41
3c78f78a
SW
42#include "trace_hv.h"
43
5e985969
DG
44//#define DEBUG_RESIZE_HPT 1
45
46#ifdef DEBUG_RESIZE_HPT
47#define resize_hpt_debug(resize, ...) \
48 do { \
49 printk(KERN_DEBUG "RESIZE HPT %p: ", resize); \
50 printk(__VA_ARGS__); \
51 } while (0)
52#else
53#define resize_hpt_debug(resize, ...) \
54 do { } while (0)
55#endif
56
7ed661bf
PM
57static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
58 long pte_index, unsigned long pteh,
59 unsigned long ptel, unsigned long *pte_idx_ret);
5e985969
DG
60
61struct kvm_resize_hpt {
62 /* These fields read-only after init */
63 struct kvm *kvm;
64 struct work_struct work;
65 u32 order;
66
67 /* These fields protected by kvm->lock */
68 int error;
69 bool prepare_done;
b5baa687
DG
70
71 /* Private to the work thread, until prepare_done is true,
72 * then protected by kvm->resize_hpt_sem */
73 struct kvm_hpt_info hpt;
5e985969
DG
74};
75
a64fd707 76static void kvmppc_rmap_reset(struct kvm *kvm);
7ed661bf 77
aae0777f 78int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order)
de56a948 79{
792fc497 80 unsigned long hpt = 0;
aae0777f 81 int cma = 0;
fa61a4e3 82 struct page *page = NULL;
aae0777f
DG
83 struct revmap_entry *rev;
84 unsigned long npte;
de56a948 85
aae0777f
DG
86 if ((order < PPC_MIN_HPT_ORDER) || (order > PPC_MAX_HPT_ORDER))
87 return -EINVAL;
32fad281 88
db9a290d 89 page = kvm_alloc_hpt_cma(1ul << (order - PAGE_SHIFT));
792fc497
AK
90 if (page) {
91 hpt = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
02a68d05 92 memset((void *)hpt, 0, (1ul << order));
aae0777f 93 cma = 1;
de56a948 94 }
32fad281 95
aae0777f 96 if (!hpt)
dcda9b04 97 hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_RETRY_MAYFAIL
aae0777f 98 |__GFP_NOWARN, order - PAGE_SHIFT);
32fad281
PM
99
100 if (!hpt)
101 return -ENOMEM;
102
aae0777f
DG
103 /* HPTEs are 2**4 bytes long */
104 npte = 1ul << (order - 4);
a56ee9f8 105
8936dda4 106 /* Allocate reverse map array */
aae0777f 107 rev = vmalloc(sizeof(struct revmap_entry) * npte);
8936dda4 108 if (!rev) {
aae0777f
DG
109 if (cma)
110 kvm_free_hpt_cma(page, 1 << (order - PAGE_SHIFT));
111 else
112 free_pages(hpt, order - PAGE_SHIFT);
113 return -ENOMEM;
8936dda4 114 }
8936dda4 115
aae0777f
DG
116 info->order = order;
117 info->virt = hpt;
118 info->cma = cma;
119 info->rev = rev;
de56a948 120
de56a948 121 return 0;
aae0777f 122}
8936dda4 123
aae0777f
DG
124void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info)
125{
126 atomic64_set(&kvm->arch.mmio_update, 0);
127 kvm->arch.hpt = *info;
128 kvm->arch.sdr1 = __pa(info->virt) | (info->order - 18);
129
3a4f1760
TH
130 pr_debug("KVM guest htab at %lx (order %ld), LPID %x\n",
131 info->virt, (long)info->order, kvm->arch.lpid);
de56a948
PM
132}
133
f98a8bf9 134long kvmppc_alloc_reset_hpt(struct kvm *kvm, int order)
32fad281
PM
135{
136 long err = -EBUSY;
f98a8bf9 137 struct kvm_hpt_info info;
32fad281 138
9e04ba69
PM
139 if (kvm_is_radix(kvm))
140 return -EINVAL;
141
32fad281 142 mutex_lock(&kvm->lock);
1b151ce4
PM
143 if (kvm->arch.mmu_ready) {
144 kvm->arch.mmu_ready = 0;
145 /* order mmu_ready vs. vcpus_running */
32fad281
PM
146 smp_mb();
147 if (atomic_read(&kvm->arch.vcpus_running)) {
1b151ce4 148 kvm->arch.mmu_ready = 1;
32fad281
PM
149 goto out;
150 }
151 }
f98a8bf9
DG
152 if (kvm->arch.hpt.order == order) {
153 /* We already have a suitable HPT */
154
32fad281 155 /* Set the entire HPT to 0, i.e. invalid HPTEs */
3f9d4f5a 156 memset((void *)kvm->arch.hpt.virt, 0, 1ul << order);
a64fd707
PM
157 /*
158 * Reset all the reverse-mapping chains for all memslots
159 */
160 kvmppc_rmap_reset(kvm);
1b400ba0
PM
161 /* Ensure that each vcpu will flush its TLB on next entry. */
162 cpumask_setall(&kvm->arch.need_tlb_flush);
32fad281 163 err = 0;
f98a8bf9 164 goto out;
32fad281 165 }
f98a8bf9 166
ef427198 167 if (kvm->arch.hpt.virt) {
f98a8bf9 168 kvmppc_free_hpt(&kvm->arch.hpt);
ef427198
PM
169 kvmppc_rmap_reset(kvm);
170 }
f98a8bf9
DG
171
172 err = kvmppc_allocate_hpt(&info, order);
173 if (err < 0)
174 goto out;
175 kvmppc_set_hpt(kvm, &info);
176
177out:
32fad281
PM
178 mutex_unlock(&kvm->lock);
179 return err;
180}
181
aae0777f 182void kvmppc_free_hpt(struct kvm_hpt_info *info)
de56a948 183{
aae0777f
DG
184 vfree(info->rev);
185 if (info->cma)
186 kvm_free_hpt_cma(virt_to_page(info->virt),
187 1 << (info->order - PAGE_SHIFT));
188 else if (info->virt)
189 free_pages(info->virt, info->order - PAGE_SHIFT);
190 info->virt = 0;
191 info->order = 0;
de56a948
PM
192}
193
da9d1d7f
PM
194/* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
195static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
196{
197 return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
198}
199
200/* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
201static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
202{
203 return (pgsize == 0x10000) ? 0x1000 : 0;
204}
205
206void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
207 unsigned long porder)
de56a948
PM
208{
209 unsigned long i;
b2b2f165 210 unsigned long npages;
c77162de
PM
211 unsigned long hp_v, hp_r;
212 unsigned long addr, hash;
da9d1d7f
PM
213 unsigned long psize;
214 unsigned long hp0, hp1;
7ed661bf 215 unsigned long idx_ret;
c77162de 216 long ret;
32fad281 217 struct kvm *kvm = vcpu->kvm;
de56a948 218
da9d1d7f
PM
219 psize = 1ul << porder;
220 npages = memslot->npages >> (porder - PAGE_SHIFT);
de56a948
PM
221
222 /* VRMA can't be > 1TB */
8936dda4
PM
223 if (npages > 1ul << (40 - porder))
224 npages = 1ul << (40 - porder);
de56a948 225 /* Can't use more than 1 HPTE per HPTEG */
3d089f84
DG
226 if (npages > kvmppc_hpt_mask(&kvm->arch.hpt) + 1)
227 npages = kvmppc_hpt_mask(&kvm->arch.hpt) + 1;
de56a948 228
da9d1d7f
PM
229 hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
230 HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
231 hp1 = hpte1_pgsize_encoding(psize) |
232 HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
233
de56a948 234 for (i = 0; i < npages; ++i) {
c77162de 235 addr = i << porder;
de56a948 236 /* can't use hpt_hash since va > 64 bits */
3d089f84
DG
237 hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25)))
238 & kvmppc_hpt_mask(&kvm->arch.hpt);
de56a948
PM
239 /*
240 * We assume that the hash table is empty and no
241 * vcpus are using it at this stage. Since we create
242 * at most one HPTE per HPTEG, we just assume entry 7
243 * is available and use it.
244 */
8936dda4 245 hash = (hash << 3) + 7;
da9d1d7f
PM
246 hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
247 hp_r = hp1 | addr;
7ed661bf
PM
248 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r,
249 &idx_ret);
c77162de
PM
250 if (ret != H_SUCCESS) {
251 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
252 addr, ret);
253 break;
254 }
de56a948
PM
255 }
256}
257
258int kvmppc_mmu_hv_init(void)
259{
9e368f29
PM
260 unsigned long host_lpid, rsvd_lpid;
261
262 if (!cpu_has_feature(CPU_FTR_HVMODE))
de56a948 263 return -EINVAL;
9e368f29 264
c17b98cf
PM
265 /* POWER7 has 10-bit LPIDs (12-bit in POWER8) */
266 host_lpid = mfspr(SPRN_LPID);
267 rsvd_lpid = LPID_RSVD;
9e368f29 268
043cc4d7
SW
269 kvmppc_init_lpid(rsvd_lpid + 1);
270
271 kvmppc_claim_lpid(host_lpid);
9e368f29 272 /* rsvd_lpid is reserved for use in partition switching */
043cc4d7 273 kvmppc_claim_lpid(rsvd_lpid);
de56a948
PM
274
275 return 0;
276}
277
de56a948
PM
278static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
279{
e4e38121
MN
280 unsigned long msr = vcpu->arch.intr_msr;
281
282 /* If transactional, change to suspend mode on IRQ delivery */
283 if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr))
284 msr |= MSR_TS_S;
285 else
286 msr |= vcpu->arch.shregs.msr & MSR_TS_MASK;
287 kvmppc_set_msr(vcpu, msr);
de56a948
PM
288}
289
025c9511 290static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
7ed661bf
PM
291 long pte_index, unsigned long pteh,
292 unsigned long ptel, unsigned long *pte_idx_ret)
c77162de 293{
c77162de
PM
294 long ret;
295
342d3db7
PM
296 /* Protect linux PTE lookup from page table destruction */
297 rcu_read_lock_sched(); /* this disables preemption too */
7ed661bf
PM
298 ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
299 current->mm->pgd, false, pte_idx_ret);
342d3db7 300 rcu_read_unlock_sched();
c77162de
PM
301 if (ret == H_TOO_HARD) {
302 /* this can't happen */
303 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
304 ret = H_RESOURCE; /* or something */
305 }
306 return ret;
307
308}
309
697d3899
PM
310static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
311 gva_t eaddr)
312{
313 u64 mask;
314 int i;
315
316 for (i = 0; i < vcpu->arch.slb_nr; i++) {
317 if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
318 continue;
319
320 if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
321 mask = ESID_MASK_1T;
322 else
323 mask = ESID_MASK;
324
325 if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
326 return &vcpu->arch.slb[i];
327 }
328 return NULL;
329}
330
331static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
332 unsigned long ea)
333{
334 unsigned long ra_mask;
335
8dc6cca5 336 ra_mask = kvmppc_actual_pgsz(v, r) - 1;
697d3899
PM
337 return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
338}
339
de56a948 340static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
93b159b4 341 struct kvmppc_pte *gpte, bool data, bool iswrite)
de56a948 342{
697d3899
PM
343 struct kvm *kvm = vcpu->kvm;
344 struct kvmppc_slb *slbe;
345 unsigned long slb_v;
346 unsigned long pp, key;
abb7c7dd 347 unsigned long v, orig_v, gr;
6f22bd32 348 __be64 *hptep;
697d3899
PM
349 int index;
350 int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
351
352 /* Get SLB entry */
353 if (virtmode) {
354 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
355 if (!slbe)
356 return -EINVAL;
357 slb_v = slbe->origv;
358 } else {
359 /* real mode access */
360 slb_v = vcpu->kvm->arch.vrma_slb_v;
361 }
362
91648ec0 363 preempt_disable();
697d3899
PM
364 /* Find the HPTE in the hash table */
365 index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
366 HPTE_V_VALID | HPTE_V_ABSENT);
91648ec0 367 if (index < 0) {
368 preempt_enable();
697d3899 369 return -ENOENT;
91648ec0 370 }
3f9d4f5a 371 hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
abb7c7dd
PM
372 v = orig_v = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
373 if (cpu_has_feature(CPU_FTR_ARCH_300))
374 v = hpte_new_to_old_v(v, be64_to_cpu(hptep[1]));
3f9d4f5a 375 gr = kvm->arch.hpt.rev[index].guest_rpte;
697d3899 376
abb7c7dd 377 unlock_hpte(hptep, orig_v);
91648ec0 378 preempt_enable();
697d3899
PM
379
380 gpte->eaddr = eaddr;
381 gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
382
383 /* Get PP bits and key for permission check */
384 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
385 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
386 key &= slb_v;
387
388 /* Calculate permissions */
389 gpte->may_read = hpte_read_permission(pp, key);
390 gpte->may_write = hpte_write_permission(pp, key);
391 gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
392
393 /* Storage key permission check for POWER7 */
c17b98cf 394 if (data && virtmode) {
697d3899
PM
395 int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
396 if (amrfield & 1)
397 gpte->may_read = 0;
398 if (amrfield & 2)
399 gpte->may_write = 0;
400 }
401
402 /* Get the guest physical address */
403 gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
404 return 0;
405}
406
407/*
408 * Quick test for whether an instruction is a load or a store.
409 * If the instruction is a load or a store, then this will indicate
410 * which it is, at least on server processors. (Embedded processors
411 * have some external PID instructions that don't follow the rule
412 * embodied here.) If the instruction isn't a load or store, then
413 * this doesn't return anything useful.
414 */
415static int instruction_is_store(unsigned int instr)
416{
417 unsigned int mask;
418
419 mask = 0x10000000;
420 if ((instr & 0xfc000000) == 0x7c000000)
421 mask = 0x100; /* major opcode 31 */
422 return (instr & mask) != 0;
423}
424
5a319350
PM
425int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
426 unsigned long gpa, gva_t ea, int is_store)
697d3899 427{
697d3899 428 u32 last_inst;
697d3899 429
51f04726 430 /*
697d3899
PM
431 * If we fail, we just return to the guest and try executing it again.
432 */
51f04726
MC
433 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
434 EMULATE_DONE)
435 return RESUME_GUEST;
697d3899
PM
436
437 /*
438 * WARNING: We do not know for sure whether the instruction we just
439 * read from memory is the same that caused the fault in the first
440 * place. If the instruction we read is neither an load or a store,
441 * then it can't access memory, so we don't need to worry about
442 * enforcing access permissions. So, assuming it is a load or
443 * store, we just check that its direction (load or store) is
444 * consistent with the original fault, since that's what we
445 * checked the access permissions against. If there is a mismatch
446 * we just return and retry the instruction.
447 */
448
51f04726 449 if (instruction_is_store(last_inst) != !!is_store)
697d3899
PM
450 return RESUME_GUEST;
451
452 /*
453 * Emulated accesses are emulated by looking at the hash for
454 * translation once, then performing the access later. The
455 * translation could be invalidated in the meantime in which
456 * point performing the subsequent memory access on the old
457 * physical address could possibly be a security hole for the
458 * guest (but not the host).
459 *
460 * This is less of an issue for MMIO stores since they aren't
461 * globally visible. It could be an issue for MMIO loads to
462 * a certain extent but we'll ignore it for now.
463 */
464
465 vcpu->arch.paddr_accessed = gpa;
6020c0f6 466 vcpu->arch.vaddr_accessed = ea;
697d3899
PM
467 return kvmppc_emulate_mmio(run, vcpu);
468}
469
470int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
471 unsigned long ea, unsigned long dsisr)
472{
473 struct kvm *kvm = vcpu->kvm;
6f22bd32 474 unsigned long hpte[3], r;
abb7c7dd 475 unsigned long hnow_v, hnow_r;
6f22bd32 476 __be64 *hptep;
342d3db7 477 unsigned long mmu_seq, psize, pte_size;
1066f772 478 unsigned long gpa_base, gfn_base;
70bddfef 479 unsigned long gpa, gfn, hva, pfn;
697d3899 480 struct kvm_memory_slot *memslot;
342d3db7 481 unsigned long *rmap;
697d3899 482 struct revmap_entry *rev;
342d3db7
PM
483 struct page *page, *pages[1];
484 long index, ret, npages;
30bda41a 485 bool is_ci;
4cf302bc 486 unsigned int writing, write_ok;
342d3db7 487 struct vm_area_struct *vma;
bad3b507 488 unsigned long rcbits;
a56ee9f8 489 long mmio_update;
697d3899 490
5a319350
PM
491 if (kvm_is_radix(kvm))
492 return kvmppc_book3s_radix_page_fault(run, vcpu, ea, dsisr);
493
697d3899
PM
494 /*
495 * Real-mode code has already searched the HPT and found the
496 * entry we're interested in. Lock the entry and check that
497 * it hasn't changed. If it has, just return and re-execute the
498 * instruction.
499 */
500 if (ea != vcpu->arch.pgfault_addr)
501 return RESUME_GUEST;
a56ee9f8
YX
502
503 if (vcpu->arch.pgfault_cache) {
504 mmio_update = atomic64_read(&kvm->arch.mmio_update);
505 if (mmio_update == vcpu->arch.pgfault_cache->mmio_update) {
506 r = vcpu->arch.pgfault_cache->rpte;
8dc6cca5
PM
507 psize = kvmppc_actual_pgsz(vcpu->arch.pgfault_hpte[0],
508 r);
a56ee9f8
YX
509 gpa_base = r & HPTE_R_RPN & ~(psize - 1);
510 gfn_base = gpa_base >> PAGE_SHIFT;
511 gpa = gpa_base | (ea & (psize - 1));
512 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
513 dsisr & DSISR_ISSTORE);
514 }
515 }
697d3899 516 index = vcpu->arch.pgfault_index;
3f9d4f5a
DG
517 hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
518 rev = &kvm->arch.hpt.rev[index];
697d3899
PM
519 preempt_disable();
520 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
521 cpu_relax();
6f22bd32
AG
522 hpte[0] = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
523 hpte[1] = be64_to_cpu(hptep[1]);
342d3db7 524 hpte[2] = r = rev->guest_rpte;
a4bd6eb0 525 unlock_hpte(hptep, hpte[0]);
697d3899
PM
526 preempt_enable();
527
abb7c7dd
PM
528 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
529 hpte[0] = hpte_new_to_old_v(hpte[0], hpte[1]);
530 hpte[1] = hpte_new_to_old_r(hpte[1]);
531 }
697d3899
PM
532 if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
533 hpte[1] != vcpu->arch.pgfault_hpte[1])
534 return RESUME_GUEST;
535
536 /* Translate the logical address and get the page */
8dc6cca5 537 psize = kvmppc_actual_pgsz(hpte[0], r);
1066f772
PM
538 gpa_base = r & HPTE_R_RPN & ~(psize - 1);
539 gfn_base = gpa_base >> PAGE_SHIFT;
540 gpa = gpa_base | (ea & (psize - 1));
70bddfef 541 gfn = gpa >> PAGE_SHIFT;
697d3899
PM
542 memslot = gfn_to_memslot(kvm, gfn);
543
3c78f78a
SW
544 trace_kvm_page_fault_enter(vcpu, hpte, memslot, ea, dsisr);
545
697d3899 546 /* No memslot means it's an emulated MMIO region */
70bddfef 547 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
6020c0f6 548 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
697d3899 549 dsisr & DSISR_ISSTORE);
697d3899 550
1066f772
PM
551 /*
552 * This should never happen, because of the slot_is_aligned()
553 * check in kvmppc_do_h_enter().
554 */
555 if (gfn_base < memslot->base_gfn)
556 return -EFAULT;
557
342d3db7
PM
558 /* used to check for invalidations in progress */
559 mmu_seq = kvm->mmu_notifier_seq;
560 smp_rmb();
561
3c78f78a 562 ret = -EFAULT;
30bda41a 563 is_ci = false;
342d3db7
PM
564 pfn = 0;
565 page = NULL;
566 pte_size = PAGE_SIZE;
4cf302bc
PM
567 writing = (dsisr & DSISR_ISSTORE) != 0;
568 /* If writing != 0, then the HPTE must allow writing, if we get here */
569 write_ok = writing;
342d3db7 570 hva = gfn_to_hva_memslot(memslot, gfn);
4cf302bc 571 npages = get_user_pages_fast(hva, 1, writing, pages);
342d3db7
PM
572 if (npages < 1) {
573 /* Check if it's an I/O mapping */
574 down_read(&current->mm->mmap_sem);
575 vma = find_vma(current->mm, hva);
576 if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
577 (vma->vm_flags & VM_PFNMAP)) {
578 pfn = vma->vm_pgoff +
579 ((hva - vma->vm_start) >> PAGE_SHIFT);
580 pte_size = psize;
30bda41a 581 is_ci = pte_ci(__pte((pgprot_val(vma->vm_page_prot))));
4cf302bc 582 write_ok = vma->vm_flags & VM_WRITE;
342d3db7
PM
583 }
584 up_read(&current->mm->mmap_sem);
585 if (!pfn)
3c78f78a 586 goto out_put;
342d3db7
PM
587 } else {
588 page = pages[0];
caaa4c80 589 pfn = page_to_pfn(page);
342d3db7
PM
590 if (PageHuge(page)) {
591 page = compound_head(page);
592 pte_size <<= compound_order(page);
593 }
4cf302bc
PM
594 /* if the guest wants write access, see if that is OK */
595 if (!writing && hpte_is_writable(r)) {
596 pte_t *ptep, pte;
691e95fd 597 unsigned long flags;
4cf302bc
PM
598 /*
599 * We need to protect against page table destruction
7d6e7f7f 600 * hugepage split and collapse.
4cf302bc 601 */
691e95fd 602 local_irq_save(flags);
94171b19
AK
603 ptep = find_current_mm_pte(current->mm->pgd,
604 hva, NULL, NULL);
db7cb5b9 605 if (ptep) {
7d6e7f7f 606 pte = kvmppc_read_update_linux_pte(ptep, 1);
d19469e8 607 if (__pte_write(pte))
4cf302bc
PM
608 write_ok = 1;
609 }
691e95fd 610 local_irq_restore(flags);
4cf302bc 611 }
342d3db7
PM
612 }
613
342d3db7
PM
614 if (psize > pte_size)
615 goto out_put;
616
617 /* Check WIMG vs. the actual page we're accessing */
30bda41a
AK
618 if (!hpte_cache_flags_ok(r, is_ci)) {
619 if (is_ci)
3c78f78a 620 goto out_put;
342d3db7
PM
621 /*
622 * Allow guest to map emulated device memory as
623 * uncacheable, but actually make it cacheable.
624 */
625 r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
626 }
627
caaa4c80
PM
628 /*
629 * Set the HPTE to point to pfn.
630 * Since the pfn is at PAGE_SIZE granularity, make sure we
631 * don't mask out lower-order bits if psize < PAGE_SIZE.
632 */
633 if (psize < PAGE_SIZE)
634 psize = PAGE_SIZE;
f0585982
YX
635 r = (r & HPTE_R_KEY_HI) | (r & ~(HPTE_R_PP0 - psize)) |
636 ((pfn << PAGE_SHIFT) & ~(psize - 1));
4cf302bc
PM
637 if (hpte_is_writable(r) && !write_ok)
638 r = hpte_make_readonly(r);
342d3db7
PM
639 ret = RESUME_GUEST;
640 preempt_disable();
641 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
642 cpu_relax();
abb7c7dd
PM
643 hnow_v = be64_to_cpu(hptep[0]);
644 hnow_r = be64_to_cpu(hptep[1]);
645 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
646 hnow_v = hpte_new_to_old_v(hnow_v, hnow_r);
647 hnow_r = hpte_new_to_old_r(hnow_r);
648 }
649 if ((hnow_v & ~HPTE_V_HVLOCK) != hpte[0] || hnow_r != hpte[1] ||
650 rev->guest_rpte != hpte[2])
342d3db7
PM
651 /* HPTE has been changed under us; let the guest retry */
652 goto out_unlock;
653 hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
654
1066f772
PM
655 /* Always put the HPTE in the rmap chain for the page base address */
656 rmap = &memslot->arch.rmap[gfn_base - memslot->base_gfn];
342d3db7
PM
657 lock_rmap(rmap);
658
659 /* Check if we might have been invalidated; let the guest retry if so */
660 ret = RESUME_GUEST;
8ca40a70 661 if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) {
342d3db7
PM
662 unlock_rmap(rmap);
663 goto out_unlock;
664 }
4cf302bc 665
bad3b507
PM
666 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
667 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
668 r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
669
6f22bd32 670 if (be64_to_cpu(hptep[0]) & HPTE_V_VALID) {
4cf302bc
PM
671 /* HPTE was previously valid, so we need to invalidate it */
672 unlock_rmap(rmap);
6f22bd32 673 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
4cf302bc 674 kvmppc_invalidate_hpte(kvm, hptep, index);
bad3b507 675 /* don't lose previous R and C bits */
6f22bd32 676 r |= be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
4cf302bc
PM
677 } else {
678 kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
679 }
342d3db7 680
abb7c7dd
PM
681 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
682 r = hpte_old_to_new_r(hpte[0], r);
683 hpte[0] = hpte_old_to_new_v(hpte[0]);
684 }
6f22bd32 685 hptep[1] = cpu_to_be64(r);
342d3db7 686 eieio();
a4bd6eb0 687 __unlock_hpte(hptep, hpte[0]);
342d3db7
PM
688 asm volatile("ptesync" : : : "memory");
689 preempt_enable();
4cf302bc 690 if (page && hpte_is_writable(r))
342d3db7
PM
691 SetPageDirty(page);
692
693 out_put:
3c78f78a
SW
694 trace_kvm_page_fault_exit(vcpu, hpte, ret);
695
de6c0b02
DG
696 if (page) {
697 /*
698 * We drop pages[0] here, not page because page might
699 * have been set to the head page of a compound, but
700 * we have to drop the reference on the correct tail
701 * page to match the get inside gup()
702 */
703 put_page(pages[0]);
704 }
342d3db7
PM
705 return ret;
706
707 out_unlock:
a4bd6eb0 708 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
342d3db7
PM
709 preempt_enable();
710 goto out_put;
711}
712
a64fd707
PM
713static void kvmppc_rmap_reset(struct kvm *kvm)
714{
715 struct kvm_memslots *slots;
716 struct kvm_memory_slot *memslot;
717 int srcu_idx;
718
719 srcu_idx = srcu_read_lock(&kvm->srcu);
9f6b8029 720 slots = kvm_memslots(kvm);
a64fd707
PM
721 kvm_for_each_memslot(memslot, slots) {
722 /*
723 * This assumes it is acceptable to lose reference and
724 * change bits across a reset.
725 */
726 memset(memslot->arch.rmap, 0,
727 memslot->npages * sizeof(*memslot->arch.rmap));
728 }
729 srcu_read_unlock(&kvm->srcu, srcu_idx);
730}
731
01756099
PM
732typedef int (*hva_handler_fn)(struct kvm *kvm, struct kvm_memory_slot *memslot,
733 unsigned long gfn);
734
84504ef3
TY
735static int kvm_handle_hva_range(struct kvm *kvm,
736 unsigned long start,
737 unsigned long end,
01756099 738 hva_handler_fn handler)
342d3db7
PM
739{
740 int ret;
741 int retval = 0;
742 struct kvm_memslots *slots;
743 struct kvm_memory_slot *memslot;
744
745 slots = kvm_memslots(kvm);
746 kvm_for_each_memslot(memslot, slots) {
84504ef3
TY
747 unsigned long hva_start, hva_end;
748 gfn_t gfn, gfn_end;
749
750 hva_start = max(start, memslot->userspace_addr);
751 hva_end = min(end, memslot->userspace_addr +
752 (memslot->npages << PAGE_SHIFT));
753 if (hva_start >= hva_end)
754 continue;
755 /*
756 * {gfn(page) | page intersects with [hva_start, hva_end)} =
757 * {gfn, gfn+1, ..., gfn_end-1}.
758 */
759 gfn = hva_to_gfn_memslot(hva_start, memslot);
760 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
342d3db7 761
84504ef3 762 for (; gfn < gfn_end; ++gfn) {
01756099 763 ret = handler(kvm, memslot, gfn);
342d3db7
PM
764 retval |= ret;
765 }
766 }
767
768 return retval;
769}
770
84504ef3 771static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
01756099 772 hva_handler_fn handler)
84504ef3
TY
773{
774 return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
775}
776
639e4597
DG
777/* Must be called with both HPTE and rmap locked */
778static void kvmppc_unmap_hpte(struct kvm *kvm, unsigned long i,
e641a317 779 struct kvm_memory_slot *memslot,
639e4597
DG
780 unsigned long *rmapp, unsigned long gfn)
781{
782 __be64 *hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
783 struct revmap_entry *rev = kvm->arch.hpt.rev;
784 unsigned long j, h;
785 unsigned long ptel, psize, rcbits;
786
787 j = rev[i].forw;
788 if (j == i) {
789 /* chain is now empty */
790 *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
791 } else {
792 /* remove i from chain */
793 h = rev[i].back;
794 rev[h].forw = j;
795 rev[j].back = h;
796 rev[i].forw = rev[i].back = i;
797 *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
798 }
799
800 /* Now check and modify the HPTE */
801 ptel = rev[i].guest_rpte;
8dc6cca5 802 psize = kvmppc_actual_pgsz(be64_to_cpu(hptep[0]), ptel);
639e4597
DG
803 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
804 hpte_rpn(ptel, psize) == gfn) {
805 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
806 kvmppc_invalidate_hpte(kvm, hptep, i);
807 hptep[1] &= ~cpu_to_be64(HPTE_R_KEY_HI | HPTE_R_KEY_LO);
808 /* Harvest R and C */
809 rcbits = be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
810 *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
e641a317
PM
811 if ((rcbits & HPTE_R_C) && memslot->dirty_bitmap)
812 kvmppc_update_dirty_map(memslot, gfn, psize);
639e4597
DG
813 if (rcbits & ~rev[i].guest_rpte) {
814 rev[i].guest_rpte = ptel | rcbits;
815 note_hpte_modification(kvm, &rev[i]);
816 }
817 }
818}
819
01756099 820static int kvm_unmap_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
342d3db7
PM
821 unsigned long gfn)
822{
639e4597 823 unsigned long i;
6f22bd32 824 __be64 *hptep;
01756099 825 unsigned long *rmapp;
342d3db7 826
01756099 827 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
342d3db7 828 for (;;) {
bad3b507 829 lock_rmap(rmapp);
342d3db7 830 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
bad3b507 831 unlock_rmap(rmapp);
342d3db7
PM
832 break;
833 }
834
835 /*
836 * To avoid an ABBA deadlock with the HPTE lock bit,
bad3b507
PM
837 * we can't spin on the HPTE lock while holding the
838 * rmap chain lock.
342d3db7
PM
839 */
840 i = *rmapp & KVMPPC_RMAP_INDEX;
3f9d4f5a 841 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
bad3b507
PM
842 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
843 /* unlock rmap before spinning on the HPTE lock */
844 unlock_rmap(rmapp);
6f22bd32 845 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
bad3b507
PM
846 cpu_relax();
847 continue;
848 }
342d3db7 849
e641a317 850 kvmppc_unmap_hpte(kvm, i, memslot, rmapp, gfn);
bad3b507 851 unlock_rmap(rmapp);
a4bd6eb0 852 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
342d3db7
PM
853 }
854 return 0;
855}
856
3a167bea 857int kvm_unmap_hva_hv(struct kvm *kvm, unsigned long hva)
342d3db7 858{
01756099
PM
859 hva_handler_fn handler;
860
861 handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp;
862 kvm_handle_hva(kvm, hva, handler);
342d3db7
PM
863 return 0;
864}
865
3a167bea 866int kvm_unmap_hva_range_hv(struct kvm *kvm, unsigned long start, unsigned long end)
b3ae2096 867{
01756099
PM
868 hva_handler_fn handler;
869
870 handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp;
871 kvm_handle_hva_range(kvm, start, end, handler);
b3ae2096
TY
872 return 0;
873}
874
3a167bea
AK
875void kvmppc_core_flush_memslot_hv(struct kvm *kvm,
876 struct kvm_memory_slot *memslot)
dfe49dbd 877{
dfe49dbd
PM
878 unsigned long gfn;
879 unsigned long n;
01756099 880 unsigned long *rmapp;
dfe49dbd 881
dfe49dbd 882 gfn = memslot->base_gfn;
01756099
PM
883 rmapp = memslot->arch.rmap;
884 for (n = memslot->npages; n; --n, ++gfn) {
885 if (kvm_is_radix(kvm)) {
886 kvm_unmap_radix(kvm, memslot, gfn);
887 continue;
888 }
dfe49dbd
PM
889 /*
890 * Testing the present bit without locking is OK because
891 * the memslot has been marked invalid already, and hence
892 * no new HPTEs referencing this page can be created,
893 * thus the present bit can't go from 0 to 1.
894 */
895 if (*rmapp & KVMPPC_RMAP_PRESENT)
01756099 896 kvm_unmap_rmapp(kvm, memslot, gfn);
dfe49dbd 897 ++rmapp;
dfe49dbd
PM
898 }
899}
900
01756099 901static int kvm_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
342d3db7
PM
902 unsigned long gfn)
903{
3f9d4f5a 904 struct revmap_entry *rev = kvm->arch.hpt.rev;
55514893 905 unsigned long head, i, j;
6f22bd32 906 __be64 *hptep;
55514893 907 int ret = 0;
01756099 908 unsigned long *rmapp;
55514893 909
01756099 910 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
55514893
PM
911 retry:
912 lock_rmap(rmapp);
913 if (*rmapp & KVMPPC_RMAP_REFERENCED) {
914 *rmapp &= ~KVMPPC_RMAP_REFERENCED;
915 ret = 1;
916 }
917 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
918 unlock_rmap(rmapp);
919 return ret;
920 }
921
922 i = head = *rmapp & KVMPPC_RMAP_INDEX;
923 do {
3f9d4f5a 924 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
55514893
PM
925 j = rev[i].forw;
926
927 /* If this HPTE isn't referenced, ignore it */
6f22bd32 928 if (!(be64_to_cpu(hptep[1]) & HPTE_R_R))
55514893
PM
929 continue;
930
931 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
932 /* unlock rmap before spinning on the HPTE lock */
933 unlock_rmap(rmapp);
6f22bd32 934 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
55514893
PM
935 cpu_relax();
936 goto retry;
937 }
938
939 /* Now check and modify the HPTE */
6f22bd32
AG
940 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
941 (be64_to_cpu(hptep[1]) & HPTE_R_R)) {
55514893 942 kvmppc_clear_ref_hpte(kvm, hptep, i);
a1b4a0f6
PM
943 if (!(rev[i].guest_rpte & HPTE_R_R)) {
944 rev[i].guest_rpte |= HPTE_R_R;
945 note_hpte_modification(kvm, &rev[i]);
946 }
55514893
PM
947 ret = 1;
948 }
a4bd6eb0 949 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
55514893
PM
950 } while ((i = j) != head);
951
952 unlock_rmap(rmapp);
953 return ret;
342d3db7
PM
954}
955
57128468 956int kvm_age_hva_hv(struct kvm *kvm, unsigned long start, unsigned long end)
342d3db7 957{
01756099
PM
958 hva_handler_fn handler;
959
960 handler = kvm_is_radix(kvm) ? kvm_age_radix : kvm_age_rmapp;
961 return kvm_handle_hva_range(kvm, start, end, handler);
342d3db7
PM
962}
963
01756099 964static int kvm_test_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
342d3db7
PM
965 unsigned long gfn)
966{
3f9d4f5a 967 struct revmap_entry *rev = kvm->arch.hpt.rev;
55514893
PM
968 unsigned long head, i, j;
969 unsigned long *hp;
970 int ret = 1;
01756099 971 unsigned long *rmapp;
55514893 972
01756099 973 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
55514893
PM
974 if (*rmapp & KVMPPC_RMAP_REFERENCED)
975 return 1;
976
977 lock_rmap(rmapp);
978 if (*rmapp & KVMPPC_RMAP_REFERENCED)
979 goto out;
980
981 if (*rmapp & KVMPPC_RMAP_PRESENT) {
982 i = head = *rmapp & KVMPPC_RMAP_INDEX;
983 do {
3f9d4f5a 984 hp = (unsigned long *)(kvm->arch.hpt.virt + (i << 4));
55514893 985 j = rev[i].forw;
6f22bd32 986 if (be64_to_cpu(hp[1]) & HPTE_R_R)
55514893
PM
987 goto out;
988 } while ((i = j) != head);
989 }
990 ret = 0;
991
992 out:
993 unlock_rmap(rmapp);
994 return ret;
342d3db7
PM
995}
996
3a167bea 997int kvm_test_age_hva_hv(struct kvm *kvm, unsigned long hva)
342d3db7 998{
01756099
PM
999 hva_handler_fn handler;
1000
1001 handler = kvm_is_radix(kvm) ? kvm_test_age_radix : kvm_test_age_rmapp;
1002 return kvm_handle_hva(kvm, hva, handler);
342d3db7
PM
1003}
1004
3a167bea 1005void kvm_set_spte_hva_hv(struct kvm *kvm, unsigned long hva, pte_t pte)
342d3db7 1006{
01756099
PM
1007 hva_handler_fn handler;
1008
1009 handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp;
1010 kvm_handle_hva(kvm, hva, handler);
de56a948
PM
1011}
1012
6c576e74
PM
1013static int vcpus_running(struct kvm *kvm)
1014{
1015 return atomic_read(&kvm->arch.vcpus_running) != 0;
1016}
1017
687414be
AK
1018/*
1019 * Returns the number of system pages that are dirty.
1020 * This can be more than 1 if we find a huge-page HPTE.
1021 */
1022static int kvm_test_clear_dirty_npages(struct kvm *kvm, unsigned long *rmapp)
82ed3616 1023{
3f9d4f5a 1024 struct revmap_entry *rev = kvm->arch.hpt.rev;
82ed3616 1025 unsigned long head, i, j;
687414be 1026 unsigned long n;
6c576e74 1027 unsigned long v, r;
6f22bd32 1028 __be64 *hptep;
687414be 1029 int npages_dirty = 0;
82ed3616
PM
1030
1031 retry:
1032 lock_rmap(rmapp);
82ed3616
PM
1033 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
1034 unlock_rmap(rmapp);
687414be 1035 return npages_dirty;
82ed3616
PM
1036 }
1037
1038 i = head = *rmapp & KVMPPC_RMAP_INDEX;
1039 do {
6f22bd32 1040 unsigned long hptep1;
3f9d4f5a 1041 hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
82ed3616
PM
1042 j = rev[i].forw;
1043
6c576e74
PM
1044 /*
1045 * Checking the C (changed) bit here is racy since there
1046 * is no guarantee about when the hardware writes it back.
1047 * If the HPTE is not writable then it is stable since the
1048 * page can't be written to, and we would have done a tlbie
1049 * (which forces the hardware to complete any writeback)
1050 * when making the HPTE read-only.
1051 * If vcpus are running then this call is racy anyway
1052 * since the page could get dirtied subsequently, so we
1053 * expect there to be a further call which would pick up
1054 * any delayed C bit writeback.
1055 * Otherwise we need to do the tlbie even if C==0 in
1056 * order to pick up any delayed writeback of C.
1057 */
6f22bd32
AG
1058 hptep1 = be64_to_cpu(hptep[1]);
1059 if (!(hptep1 & HPTE_R_C) &&
1060 (!hpte_is_writable(hptep1) || vcpus_running(kvm)))
82ed3616
PM
1061 continue;
1062
1063 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
1064 /* unlock rmap before spinning on the HPTE lock */
1065 unlock_rmap(rmapp);
6f22bd32 1066 while (hptep[0] & cpu_to_be64(HPTE_V_HVLOCK))
82ed3616
PM
1067 cpu_relax();
1068 goto retry;
1069 }
1070
1071 /* Now check and modify the HPTE */
f6fb9e84 1072 if (!(hptep[0] & cpu_to_be64(HPTE_V_VALID))) {
a4bd6eb0 1073 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
6c576e74 1074 continue;
f6fb9e84 1075 }
6c576e74
PM
1076
1077 /* need to make it temporarily absent so C is stable */
6f22bd32 1078 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
6c576e74 1079 kvmppc_invalidate_hpte(kvm, hptep, i);
6f22bd32
AG
1080 v = be64_to_cpu(hptep[0]);
1081 r = be64_to_cpu(hptep[1]);
6c576e74 1082 if (r & HPTE_R_C) {
6f22bd32 1083 hptep[1] = cpu_to_be64(r & ~HPTE_R_C);
a1b4a0f6
PM
1084 if (!(rev[i].guest_rpte & HPTE_R_C)) {
1085 rev[i].guest_rpte |= HPTE_R_C;
1086 note_hpte_modification(kvm, &rev[i]);
1087 }
8dc6cca5 1088 n = kvmppc_actual_pgsz(v, r);
687414be
AK
1089 n = (n + PAGE_SIZE - 1) >> PAGE_SHIFT;
1090 if (n > npages_dirty)
1091 npages_dirty = n;
6c576e74 1092 eieio();
82ed3616 1093 }
a4bd6eb0 1094 v &= ~HPTE_V_ABSENT;
6c576e74 1095 v |= HPTE_V_VALID;
a4bd6eb0 1096 __unlock_hpte(hptep, v);
82ed3616
PM
1097 } while ((i = j) != head);
1098
1099 unlock_rmap(rmapp);
687414be 1100 return npages_dirty;
82ed3616
PM
1101}
1102
8f7b79b8 1103void kvmppc_harvest_vpa_dirty(struct kvmppc_vpa *vpa,
c35635ef
PM
1104 struct kvm_memory_slot *memslot,
1105 unsigned long *map)
1106{
1107 unsigned long gfn;
1108
1109 if (!vpa->dirty || !vpa->pinned_addr)
1110 return;
1111 gfn = vpa->gpa >> PAGE_SHIFT;
1112 if (gfn < memslot->base_gfn ||
1113 gfn >= memslot->base_gfn + memslot->npages)
1114 return;
1115
1116 vpa->dirty = false;
1117 if (map)
1118 __set_bit_le(gfn - memslot->base_gfn, map);
1119}
1120
8f7b79b8
PM
1121long kvmppc_hv_get_dirty_log_hpt(struct kvm *kvm,
1122 struct kvm_memory_slot *memslot, unsigned long *map)
82ed3616 1123{
e641a317 1124 unsigned long i;
dfe49dbd 1125 unsigned long *rmapp;
82ed3616
PM
1126
1127 preempt_disable();
d89cc617 1128 rmapp = memslot->arch.rmap;
82ed3616 1129 for (i = 0; i < memslot->npages; ++i) {
687414be
AK
1130 int npages = kvm_test_clear_dirty_npages(kvm, rmapp);
1131 /*
1132 * Note that if npages > 0 then i must be a multiple of npages,
1133 * since we always put huge-page HPTEs in the rmap chain
1134 * corresponding to their page base address.
1135 */
e641a317
PM
1136 if (npages)
1137 set_dirty_bits(map, i, npages);
82ed3616
PM
1138 ++rmapp;
1139 }
1140 preempt_enable();
1141 return 0;
1142}
1143
93e60249
PM
1144void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
1145 unsigned long *nb_ret)
1146{
1147 struct kvm_memory_slot *memslot;
1148 unsigned long gfn = gpa >> PAGE_SHIFT;
342d3db7
PM
1149 struct page *page, *pages[1];
1150 int npages;
c35635ef 1151 unsigned long hva, offset;
2c9097e4 1152 int srcu_idx;
93e60249 1153
2c9097e4 1154 srcu_idx = srcu_read_lock(&kvm->srcu);
93e60249
PM
1155 memslot = gfn_to_memslot(kvm, gfn);
1156 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2c9097e4 1157 goto err;
c17b98cf
PM
1158 hva = gfn_to_hva_memslot(memslot, gfn);
1159 npages = get_user_pages_fast(hva, 1, 1, pages);
1160 if (npages < 1)
1161 goto err;
1162 page = pages[0];
2c9097e4
PM
1163 srcu_read_unlock(&kvm->srcu, srcu_idx);
1164
c35635ef 1165 offset = gpa & (PAGE_SIZE - 1);
93e60249 1166 if (nb_ret)
c35635ef 1167 *nb_ret = PAGE_SIZE - offset;
93e60249 1168 return page_address(page) + offset;
2c9097e4
PM
1169
1170 err:
1171 srcu_read_unlock(&kvm->srcu, srcu_idx);
1172 return NULL;
93e60249
PM
1173}
1174
c35635ef
PM
1175void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa,
1176 bool dirty)
93e60249
PM
1177{
1178 struct page *page = virt_to_page(va);
c35635ef
PM
1179 struct kvm_memory_slot *memslot;
1180 unsigned long gfn;
c35635ef 1181 int srcu_idx;
93e60249 1182
93e60249 1183 put_page(page);
c35635ef 1184
c17b98cf 1185 if (!dirty)
c35635ef
PM
1186 return;
1187
e641a317 1188 /* We need to mark this page dirty in the memslot dirty_bitmap, if any */
c35635ef
PM
1189 gfn = gpa >> PAGE_SHIFT;
1190 srcu_idx = srcu_read_lock(&kvm->srcu);
1191 memslot = gfn_to_memslot(kvm, gfn);
e641a317
PM
1192 if (memslot && memslot->dirty_bitmap)
1193 set_bit_le(gfn - memslot->base_gfn, memslot->dirty_bitmap);
c35635ef 1194 srcu_read_unlock(&kvm->srcu, srcu_idx);
93e60249
PM
1195}
1196
5e985969
DG
1197/*
1198 * HPT resizing
1199 */
1200static int resize_hpt_allocate(struct kvm_resize_hpt *resize)
1201{
b5baa687
DG
1202 int rc;
1203
1204 rc = kvmppc_allocate_hpt(&resize->hpt, resize->order);
1205 if (rc < 0)
1206 return rc;
1207
1208 resize_hpt_debug(resize, "resize_hpt_allocate(): HPT @ 0x%lx\n",
1209 resize->hpt.virt);
1210
5e985969
DG
1211 return 0;
1212}
1213
b5baa687
DG
1214static unsigned long resize_hpt_rehash_hpte(struct kvm_resize_hpt *resize,
1215 unsigned long idx)
1216{
1217 struct kvm *kvm = resize->kvm;
1218 struct kvm_hpt_info *old = &kvm->arch.hpt;
1219 struct kvm_hpt_info *new = &resize->hpt;
1220 unsigned long old_hash_mask = (1ULL << (old->order - 7)) - 1;
1221 unsigned long new_hash_mask = (1ULL << (new->order - 7)) - 1;
1222 __be64 *hptep, *new_hptep;
1223 unsigned long vpte, rpte, guest_rpte;
1224 int ret;
1225 struct revmap_entry *rev;
1226 unsigned long apsize, psize, avpn, pteg, hash;
1227 unsigned long new_idx, new_pteg, replace_vpte;
1228
1229 hptep = (__be64 *)(old->virt + (idx << 4));
1230
1231 /* Guest is stopped, so new HPTEs can't be added or faulted
1232 * in, only unmapped or altered by host actions. So, it's
1233 * safe to check this before we take the HPTE lock */
1234 vpte = be64_to_cpu(hptep[0]);
1235 if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
1236 return 0; /* nothing to do */
1237
1238 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
1239 cpu_relax();
1240
1241 vpte = be64_to_cpu(hptep[0]);
1242
1243 ret = 0;
1244 if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
1245 /* Nothing to do */
1246 goto out;
1247
1248 /* Unmap */
1249 rev = &old->rev[idx];
1250 guest_rpte = rev->guest_rpte;
1251
1252 ret = -EIO;
8dc6cca5 1253 apsize = kvmppc_actual_pgsz(vpte, guest_rpte);
b5baa687
DG
1254 if (!apsize)
1255 goto out;
1256
1257 if (vpte & HPTE_V_VALID) {
1258 unsigned long gfn = hpte_rpn(guest_rpte, apsize);
1259 int srcu_idx = srcu_read_lock(&kvm->srcu);
1260 struct kvm_memory_slot *memslot =
1261 __gfn_to_memslot(kvm_memslots(kvm), gfn);
1262
1263 if (memslot) {
1264 unsigned long *rmapp;
1265 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1266
1267 lock_rmap(rmapp);
e641a317 1268 kvmppc_unmap_hpte(kvm, idx, memslot, rmapp, gfn);
b5baa687
DG
1269 unlock_rmap(rmapp);
1270 }
1271
1272 srcu_read_unlock(&kvm->srcu, srcu_idx);
1273 }
1274
1275 /* Reload PTE after unmap */
1276 vpte = be64_to_cpu(hptep[0]);
1277
1278 BUG_ON(vpte & HPTE_V_VALID);
1279 BUG_ON(!(vpte & HPTE_V_ABSENT));
1280
1281 ret = 0;
1282 if (!(vpte & HPTE_V_BOLTED))
1283 goto out;
1284
1285 rpte = be64_to_cpu(hptep[1]);
1286 psize = hpte_base_page_size(vpte, rpte);
1287 avpn = HPTE_V_AVPN_VAL(vpte) & ~((psize - 1) >> 23);
1288 pteg = idx / HPTES_PER_GROUP;
1289 if (vpte & HPTE_V_SECONDARY)
1290 pteg = ~pteg;
1291
1292 if (!(vpte & HPTE_V_1TB_SEG)) {
1293 unsigned long offset, vsid;
1294
1295 /* We only have 28 - 23 bits of offset in avpn */
1296 offset = (avpn & 0x1f) << 23;
1297 vsid = avpn >> 5;
1298 /* We can find more bits from the pteg value */
1299 if (psize < (1ULL << 23))
1300 offset |= ((vsid ^ pteg) & old_hash_mask) * psize;
1301
1302 hash = vsid ^ (offset / psize);
1303 } else {
1304 unsigned long offset, vsid;
1305
1306 /* We only have 40 - 23 bits of seg_off in avpn */
1307 offset = (avpn & 0x1ffff) << 23;
1308 vsid = avpn >> 17;
1309 if (psize < (1ULL << 23))
1310 offset |= ((vsid ^ (vsid << 25) ^ pteg) & old_hash_mask) * psize;
1311
1312 hash = vsid ^ (vsid << 25) ^ (offset / psize);
1313 }
1314
1315 new_pteg = hash & new_hash_mask;
1316 if (vpte & HPTE_V_SECONDARY) {
1317 BUG_ON(~pteg != (hash & old_hash_mask));
1318 new_pteg = ~new_pteg;
1319 } else {
1320 BUG_ON(pteg != (hash & old_hash_mask));
1321 }
1322
1323 new_idx = new_pteg * HPTES_PER_GROUP + (idx % HPTES_PER_GROUP);
1324 new_hptep = (__be64 *)(new->virt + (new_idx << 4));
1325
1326 replace_vpte = be64_to_cpu(new_hptep[0]);
1327
1328 if (replace_vpte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
1329 BUG_ON(new->order >= old->order);
1330
1331 if (replace_vpte & HPTE_V_BOLTED) {
1332 if (vpte & HPTE_V_BOLTED)
1333 /* Bolted collision, nothing we can do */
1334 ret = -ENOSPC;
1335 /* Discard the new HPTE */
1336 goto out;
1337 }
1338
1339 /* Discard the previous HPTE */
1340 }
1341
1342 new_hptep[1] = cpu_to_be64(rpte);
1343 new->rev[new_idx].guest_rpte = guest_rpte;
1344 /* No need for a barrier, since new HPT isn't active */
1345 new_hptep[0] = cpu_to_be64(vpte);
1346 unlock_hpte(new_hptep, vpte);
1347
1348out:
1349 unlock_hpte(hptep, vpte);
1350 return ret;
1351}
1352
5e985969
DG
1353static int resize_hpt_rehash(struct kvm_resize_hpt *resize)
1354{
b5baa687
DG
1355 struct kvm *kvm = resize->kvm;
1356 unsigned long i;
1357 int rc;
1358
bcd3bb63
PM
1359 /*
1360 * resize_hpt_rehash_hpte() doesn't handle the new-format HPTEs
1361 * that POWER9 uses, and could well hit a BUG_ON on POWER9.
1362 */
1363 if (cpu_has_feature(CPU_FTR_ARCH_300))
1364 return -EIO;
b5baa687
DG
1365 for (i = 0; i < kvmppc_hpt_npte(&kvm->arch.hpt); i++) {
1366 rc = resize_hpt_rehash_hpte(resize, i);
1367 if (rc != 0)
1368 return rc;
1369 }
1370
1371 return 0;
5e985969
DG
1372}
1373
1374static void resize_hpt_pivot(struct kvm_resize_hpt *resize)
1375{
b5baa687
DG
1376 struct kvm *kvm = resize->kvm;
1377 struct kvm_hpt_info hpt_tmp;
1378
1379 /* Exchange the pending tables in the resize structure with
1380 * the active tables */
1381
1382 resize_hpt_debug(resize, "resize_hpt_pivot()\n");
1383
1384 spin_lock(&kvm->mmu_lock);
1385 asm volatile("ptesync" : : : "memory");
1386
1387 hpt_tmp = kvm->arch.hpt;
1388 kvmppc_set_hpt(kvm, &resize->hpt);
1389 resize->hpt = hpt_tmp;
1390
1391 spin_unlock(&kvm->mmu_lock);
1392
1393 synchronize_srcu_expedited(&kvm->srcu);
1394
1395 resize_hpt_debug(resize, "resize_hpt_pivot() done\n");
5e985969
DG
1396}
1397
1398static void resize_hpt_release(struct kvm *kvm, struct kvm_resize_hpt *resize)
1399{
1400 BUG_ON(kvm->arch.resize_hpt != resize);
b5baa687 1401
5b73d634
DG
1402 if (!resize)
1403 return;
1404
b5baa687
DG
1405 if (resize->hpt.virt)
1406 kvmppc_free_hpt(&resize->hpt);
1407
5e985969
DG
1408 kvm->arch.resize_hpt = NULL;
1409 kfree(resize);
1410}
1411
1412static void resize_hpt_prepare_work(struct work_struct *work)
1413{
1414 struct kvm_resize_hpt *resize = container_of(work,
1415 struct kvm_resize_hpt,
1416 work);
1417 struct kvm *kvm = resize->kvm;
1418 int err;
1419
1420 resize_hpt_debug(resize, "resize_hpt_prepare_work(): order = %d\n",
1421 resize->order);
1422
1423 err = resize_hpt_allocate(resize);
1424
1425 mutex_lock(&kvm->lock);
1426
1427 resize->error = err;
1428 resize->prepare_done = true;
1429
1430 mutex_unlock(&kvm->lock);
1431}
1432
1433long kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm,
1434 struct kvm_ppc_resize_hpt *rhpt)
1435{
1436 unsigned long flags = rhpt->flags;
1437 unsigned long shift = rhpt->shift;
1438 struct kvm_resize_hpt *resize;
1439 int ret;
1440
891f1ebf 1441 if (flags != 0 || kvm_is_radix(kvm))
5e985969
DG
1442 return -EINVAL;
1443
1444 if (shift && ((shift < 18) || (shift > 46)))
1445 return -EINVAL;
1446
1447 mutex_lock(&kvm->lock);
1448
1449 resize = kvm->arch.resize_hpt;
1450
1451 if (resize) {
1452 if (resize->order == shift) {
1453 /* Suitable resize in progress */
1454 if (resize->prepare_done) {
1455 ret = resize->error;
1456 if (ret != 0)
1457 resize_hpt_release(kvm, resize);
1458 } else {
1459 ret = 100; /* estimated time in ms */
1460 }
1461
1462 goto out;
1463 }
1464
1465 /* not suitable, cancel it */
1466 resize_hpt_release(kvm, resize);
1467 }
1468
1469 ret = 0;
1470 if (!shift)
1471 goto out; /* nothing to do */
1472
1473 /* start new resize */
1474
1475 resize = kzalloc(sizeof(*resize), GFP_KERNEL);
abd80dcb
DC
1476 if (!resize) {
1477 ret = -ENOMEM;
1478 goto out;
1479 }
5e985969
DG
1480 resize->order = shift;
1481 resize->kvm = kvm;
1482 INIT_WORK(&resize->work, resize_hpt_prepare_work);
1483 kvm->arch.resize_hpt = resize;
1484
1485 schedule_work(&resize->work);
1486
1487 ret = 100; /* estimated time in ms */
1488
1489out:
1490 mutex_unlock(&kvm->lock);
1491 return ret;
1492}
1493
1494static void resize_hpt_boot_vcpu(void *opaque)
1495{
1496 /* Nothing to do, just force a KVM exit */
1497}
1498
1499long kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm,
1500 struct kvm_ppc_resize_hpt *rhpt)
1501{
1502 unsigned long flags = rhpt->flags;
1503 unsigned long shift = rhpt->shift;
1504 struct kvm_resize_hpt *resize;
1505 long ret;
1506
891f1ebf 1507 if (flags != 0 || kvm_is_radix(kvm))
5e985969
DG
1508 return -EINVAL;
1509
1510 if (shift && ((shift < 18) || (shift > 46)))
1511 return -EINVAL;
1512
1513 mutex_lock(&kvm->lock);
1514
1515 resize = kvm->arch.resize_hpt;
1516
1517 /* This shouldn't be possible */
1518 ret = -EIO;
1b151ce4 1519 if (WARN_ON(!kvm->arch.mmu_ready))
5e985969
DG
1520 goto out_no_hpt;
1521
1522 /* Stop VCPUs from running while we mess with the HPT */
1b151ce4 1523 kvm->arch.mmu_ready = 0;
5e985969
DG
1524 smp_mb();
1525
1526 /* Boot all CPUs out of the guest so they re-read
1b151ce4 1527 * mmu_ready */
5e985969
DG
1528 on_each_cpu(resize_hpt_boot_vcpu, NULL, 1);
1529
1530 ret = -ENXIO;
1531 if (!resize || (resize->order != shift))
1532 goto out;
1533
1534 ret = -EBUSY;
1535 if (!resize->prepare_done)
1536 goto out;
1537
1538 ret = resize->error;
1539 if (ret != 0)
1540 goto out;
1541
1542 ret = resize_hpt_rehash(resize);
1543 if (ret != 0)
1544 goto out;
1545
1546 resize_hpt_pivot(resize);
1547
1548out:
1549 /* Let VCPUs run again */
1b151ce4 1550 kvm->arch.mmu_ready = 1;
5e985969
DG
1551 smp_mb();
1552out_no_hpt:
1553 resize_hpt_release(kvm, resize);
1554 mutex_unlock(&kvm->lock);
1555 return ret;
1556}
1557
a2932923
PM
1558/*
1559 * Functions for reading and writing the hash table via reads and
1560 * writes on a file descriptor.
1561 *
1562 * Reads return the guest view of the hash table, which has to be
1563 * pieced together from the real hash table and the guest_rpte
1564 * values in the revmap array.
1565 *
1566 * On writes, each HPTE written is considered in turn, and if it
1567 * is valid, it is written to the HPT as if an H_ENTER with the
1568 * exact flag set was done. When the invalid count is non-zero
1569 * in the header written to the stream, the kernel will make
1570 * sure that that many HPTEs are invalid, and invalidate them
1571 * if not.
1572 */
1573
1574struct kvm_htab_ctx {
1575 unsigned long index;
1576 unsigned long flags;
1577 struct kvm *kvm;
1578 int first_pass;
1579};
1580
1581#define HPTE_SIZE (2 * sizeof(unsigned long))
1582
a1b4a0f6
PM
1583/*
1584 * Returns 1 if this HPT entry has been modified or has pending
1585 * R/C bit changes.
1586 */
6f22bd32 1587static int hpte_dirty(struct revmap_entry *revp, __be64 *hptp)
a1b4a0f6
PM
1588{
1589 unsigned long rcbits_unset;
1590
1591 if (revp->guest_rpte & HPTE_GR_MODIFIED)
1592 return 1;
1593
1594 /* Also need to consider changes in reference and changed bits */
1595 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
6f22bd32
AG
1596 if ((be64_to_cpu(hptp[0]) & HPTE_V_VALID) &&
1597 (be64_to_cpu(hptp[1]) & rcbits_unset))
a1b4a0f6
PM
1598 return 1;
1599
1600 return 0;
1601}
1602
6f22bd32 1603static long record_hpte(unsigned long flags, __be64 *hptp,
a2932923
PM
1604 unsigned long *hpte, struct revmap_entry *revp,
1605 int want_valid, int first_pass)
1606{
abb7c7dd 1607 unsigned long v, r, hr;
a1b4a0f6 1608 unsigned long rcbits_unset;
a2932923
PM
1609 int ok = 1;
1610 int valid, dirty;
1611
1612 /* Unmodified entries are uninteresting except on the first pass */
a1b4a0f6 1613 dirty = hpte_dirty(revp, hptp);
a2932923
PM
1614 if (!first_pass && !dirty)
1615 return 0;
1616
1617 valid = 0;
6f22bd32 1618 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) {
a2932923
PM
1619 valid = 1;
1620 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) &&
6f22bd32 1621 !(be64_to_cpu(hptp[0]) & HPTE_V_BOLTED))
a2932923
PM
1622 valid = 0;
1623 }
1624 if (valid != want_valid)
1625 return 0;
1626
1627 v = r = 0;
1628 if (valid || dirty) {
1629 /* lock the HPTE so it's stable and read it */
1630 preempt_disable();
1631 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
1632 cpu_relax();
6f22bd32 1633 v = be64_to_cpu(hptp[0]);
abb7c7dd
PM
1634 hr = be64_to_cpu(hptp[1]);
1635 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1636 v = hpte_new_to_old_v(v, hr);
1637 hr = hpte_new_to_old_r(hr);
1638 }
a1b4a0f6
PM
1639
1640 /* re-evaluate valid and dirty from synchronized HPTE value */
1641 valid = !!(v & HPTE_V_VALID);
1642 dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
1643
1644 /* Harvest R and C into guest view if necessary */
1645 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
abb7c7dd
PM
1646 if (valid && (rcbits_unset & hr)) {
1647 revp->guest_rpte |= (hr &
6f22bd32 1648 (HPTE_R_R | HPTE_R_C)) | HPTE_GR_MODIFIED;
a1b4a0f6
PM
1649 dirty = 1;
1650 }
1651
a2932923
PM
1652 if (v & HPTE_V_ABSENT) {
1653 v &= ~HPTE_V_ABSENT;
1654 v |= HPTE_V_VALID;
a1b4a0f6 1655 valid = 1;
a2932923 1656 }
a2932923
PM
1657 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED))
1658 valid = 0;
a1b4a0f6
PM
1659
1660 r = revp->guest_rpte;
a2932923
PM
1661 /* only clear modified if this is the right sort of entry */
1662 if (valid == want_valid && dirty) {
1663 r &= ~HPTE_GR_MODIFIED;
1664 revp->guest_rpte = r;
1665 }
a4bd6eb0 1666 unlock_hpte(hptp, be64_to_cpu(hptp[0]));
a2932923
PM
1667 preempt_enable();
1668 if (!(valid == want_valid && (first_pass || dirty)))
1669 ok = 0;
1670 }
6f22bd32
AG
1671 hpte[0] = cpu_to_be64(v);
1672 hpte[1] = cpu_to_be64(r);
a2932923
PM
1673 return ok;
1674}
1675
1676static ssize_t kvm_htab_read(struct file *file, char __user *buf,
1677 size_t count, loff_t *ppos)
1678{
1679 struct kvm_htab_ctx *ctx = file->private_data;
1680 struct kvm *kvm = ctx->kvm;
1681 struct kvm_get_htab_header hdr;
6f22bd32 1682 __be64 *hptp;
a2932923
PM
1683 struct revmap_entry *revp;
1684 unsigned long i, nb, nw;
1685 unsigned long __user *lbuf;
1686 struct kvm_get_htab_header __user *hptr;
1687 unsigned long flags;
1688 int first_pass;
1689 unsigned long hpte[2];
1690
1691 if (!access_ok(VERIFY_WRITE, buf, count))
1692 return -EFAULT;
891f1ebf
PM
1693 if (kvm_is_radix(kvm))
1694 return 0;
a2932923
PM
1695
1696 first_pass = ctx->first_pass;
1697 flags = ctx->flags;
1698
1699 i = ctx->index;
3f9d4f5a
DG
1700 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
1701 revp = kvm->arch.hpt.rev + i;
a2932923
PM
1702 lbuf = (unsigned long __user *)buf;
1703
1704 nb = 0;
1705 while (nb + sizeof(hdr) + HPTE_SIZE < count) {
1706 /* Initialize header */
1707 hptr = (struct kvm_get_htab_header __user *)buf;
a2932923
PM
1708 hdr.n_valid = 0;
1709 hdr.n_invalid = 0;
1710 nw = nb;
1711 nb += sizeof(hdr);
1712 lbuf = (unsigned long __user *)(buf + sizeof(hdr));
1713
1714 /* Skip uninteresting entries, i.e. clean on not-first pass */
1715 if (!first_pass) {
3d089f84 1716 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
a1b4a0f6 1717 !hpte_dirty(revp, hptp)) {
a2932923
PM
1718 ++i;
1719 hptp += 2;
1720 ++revp;
1721 }
1722 }
05dd85f7 1723 hdr.index = i;
a2932923
PM
1724
1725 /* Grab a series of valid entries */
3d089f84 1726 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
a2932923
PM
1727 hdr.n_valid < 0xffff &&
1728 nb + HPTE_SIZE < count &&
1729 record_hpte(flags, hptp, hpte, revp, 1, first_pass)) {
1730 /* valid entry, write it out */
1731 ++hdr.n_valid;
1732 if (__put_user(hpte[0], lbuf) ||
1733 __put_user(hpte[1], lbuf + 1))
1734 return -EFAULT;
1735 nb += HPTE_SIZE;
1736 lbuf += 2;
1737 ++i;
1738 hptp += 2;
1739 ++revp;
1740 }
1741 /* Now skip invalid entries while we can */
3d089f84 1742 while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
a2932923
PM
1743 hdr.n_invalid < 0xffff &&
1744 record_hpte(flags, hptp, hpte, revp, 0, first_pass)) {
1745 /* found an invalid entry */
1746 ++hdr.n_invalid;
1747 ++i;
1748 hptp += 2;
1749 ++revp;
1750 }
1751
1752 if (hdr.n_valid || hdr.n_invalid) {
1753 /* write back the header */
1754 if (__copy_to_user(hptr, &hdr, sizeof(hdr)))
1755 return -EFAULT;
1756 nw = nb;
1757 buf = (char __user *)lbuf;
1758 } else {
1759 nb = nw;
1760 }
1761
1762 /* Check if we've wrapped around the hash table */
3d089f84 1763 if (i >= kvmppc_hpt_npte(&kvm->arch.hpt)) {
a2932923
PM
1764 i = 0;
1765 ctx->first_pass = 0;
1766 break;
1767 }
1768 }
1769
1770 ctx->index = i;
1771
1772 return nb;
1773}
1774
1775static ssize_t kvm_htab_write(struct file *file, const char __user *buf,
1776 size_t count, loff_t *ppos)
1777{
1778 struct kvm_htab_ctx *ctx = file->private_data;
1779 struct kvm *kvm = ctx->kvm;
1780 struct kvm_get_htab_header hdr;
1781 unsigned long i, j;
1782 unsigned long v, r;
1783 unsigned long __user *lbuf;
6f22bd32 1784 __be64 *hptp;
a2932923
PM
1785 unsigned long tmp[2];
1786 ssize_t nb;
1787 long int err, ret;
1b151ce4 1788 int mmu_ready;
a2932923
PM
1789
1790 if (!access_ok(VERIFY_READ, buf, count))
1791 return -EFAULT;
891f1ebf
PM
1792 if (kvm_is_radix(kvm))
1793 return -EINVAL;
a2932923
PM
1794
1795 /* lock out vcpus from running while we're doing this */
1796 mutex_lock(&kvm->lock);
1b151ce4
PM
1797 mmu_ready = kvm->arch.mmu_ready;
1798 if (mmu_ready) {
1799 kvm->arch.mmu_ready = 0; /* temporarily */
1800 /* order mmu_ready vs. vcpus_running */
a2932923
PM
1801 smp_mb();
1802 if (atomic_read(&kvm->arch.vcpus_running)) {
1b151ce4 1803 kvm->arch.mmu_ready = 1;
a2932923
PM
1804 mutex_unlock(&kvm->lock);
1805 return -EBUSY;
1806 }
1807 }
1808
1809 err = 0;
1810 for (nb = 0; nb + sizeof(hdr) <= count; ) {
1811 err = -EFAULT;
1812 if (__copy_from_user(&hdr, buf, sizeof(hdr)))
1813 break;
1814
1815 err = 0;
1816 if (nb + hdr.n_valid * HPTE_SIZE > count)
1817 break;
1818
1819 nb += sizeof(hdr);
1820 buf += sizeof(hdr);
1821
1822 err = -EINVAL;
1823 i = hdr.index;
3d089f84
DG
1824 if (i >= kvmppc_hpt_npte(&kvm->arch.hpt) ||
1825 i + hdr.n_valid + hdr.n_invalid > kvmppc_hpt_npte(&kvm->arch.hpt))
a2932923
PM
1826 break;
1827
3f9d4f5a 1828 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
a2932923
PM
1829 lbuf = (unsigned long __user *)buf;
1830 for (j = 0; j < hdr.n_valid; ++j) {
ffada016
CLG
1831 __be64 hpte_v;
1832 __be64 hpte_r;
1833
a2932923 1834 err = -EFAULT;
ffada016
CLG
1835 if (__get_user(hpte_v, lbuf) ||
1836 __get_user(hpte_r, lbuf + 1))
a2932923 1837 goto out;
ffada016
CLG
1838 v = be64_to_cpu(hpte_v);
1839 r = be64_to_cpu(hpte_r);
a2932923
PM
1840 err = -EINVAL;
1841 if (!(v & HPTE_V_VALID))
1842 goto out;
1843 lbuf += 2;
1844 nb += HPTE_SIZE;
1845
6f22bd32 1846 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
a2932923
PM
1847 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1848 err = -EIO;
1849 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r,
1850 tmp);
1851 if (ret != H_SUCCESS) {
1852 pr_err("kvm_htab_write ret %ld i=%ld v=%lx "
1853 "r=%lx\n", ret, i, v, r);
1854 goto out;
1855 }
1b151ce4 1856 if (!mmu_ready && is_vrma_hpte(v)) {
341acbb3 1857 unsigned long psize = hpte_base_page_size(v, r);
a2932923
PM
1858 unsigned long senc = slb_pgsize_encoding(psize);
1859 unsigned long lpcr;
1860
1861 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1862 (VRMA_VSID << SLB_VSID_SHIFT_1T);
a0144e2a
PM
1863 lpcr = senc << (LPCR_VRMASD_SH - 4);
1864 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
1b151ce4 1865 mmu_ready = 1;
a2932923
PM
1866 }
1867 ++i;
1868 hptp += 2;
1869 }
1870
1871 for (j = 0; j < hdr.n_invalid; ++j) {
6f22bd32 1872 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
a2932923
PM
1873 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1874 ++i;
1875 hptp += 2;
1876 }
1877 err = 0;
1878 }
1879
1880 out:
1b151ce4 1881 /* Order HPTE updates vs. mmu_ready */
a2932923 1882 smp_wmb();
1b151ce4 1883 kvm->arch.mmu_ready = mmu_ready;
a2932923
PM
1884 mutex_unlock(&kvm->lock);
1885
1886 if (err)
1887 return err;
1888 return nb;
1889}
1890
1891static int kvm_htab_release(struct inode *inode, struct file *filp)
1892{
1893 struct kvm_htab_ctx *ctx = filp->private_data;
1894
1895 filp->private_data = NULL;
1896 if (!(ctx->flags & KVM_GET_HTAB_WRITE))
1897 atomic_dec(&ctx->kvm->arch.hpte_mod_interest);
1898 kvm_put_kvm(ctx->kvm);
1899 kfree(ctx);
1900 return 0;
1901}
1902
75ef9de1 1903static const struct file_operations kvm_htab_fops = {
a2932923
PM
1904 .read = kvm_htab_read,
1905 .write = kvm_htab_write,
1906 .llseek = default_llseek,
1907 .release = kvm_htab_release,
1908};
1909
1910int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf)
1911{
1912 int ret;
1913 struct kvm_htab_ctx *ctx;
1914 int rwflag;
1915
1916 /* reject flags we don't recognize */
1917 if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE))
1918 return -EINVAL;
1919 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1920 if (!ctx)
1921 return -ENOMEM;
1922 kvm_get_kvm(kvm);
1923 ctx->kvm = kvm;
1924 ctx->index = ghf->start_index;
1925 ctx->flags = ghf->flags;
1926 ctx->first_pass = 1;
1927
1928 rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY;
2f84d5ea 1929 ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC);
a2932923 1930 if (ret < 0) {
43f6b0cf 1931 kfree(ctx);
a2932923
PM
1932 kvm_put_kvm(kvm);
1933 return ret;
1934 }
1935
1936 if (rwflag == O_RDONLY) {
1937 mutex_lock(&kvm->slots_lock);
1938 atomic_inc(&kvm->arch.hpte_mod_interest);
1939 /* make sure kvmppc_do_h_enter etc. see the increment */
1940 synchronize_srcu_expedited(&kvm->srcu);
1941 mutex_unlock(&kvm->slots_lock);
1942 }
1943
1944 return ret;
1945}
1946
e23a808b
PM
1947struct debugfs_htab_state {
1948 struct kvm *kvm;
1949 struct mutex mutex;
1950 unsigned long hpt_index;
1951 int chars_left;
1952 int buf_index;
1953 char buf[64];
1954};
1955
1956static int debugfs_htab_open(struct inode *inode, struct file *file)
1957{
1958 struct kvm *kvm = inode->i_private;
1959 struct debugfs_htab_state *p;
1960
1961 p = kzalloc(sizeof(*p), GFP_KERNEL);
1962 if (!p)
1963 return -ENOMEM;
1964
1965 kvm_get_kvm(kvm);
1966 p->kvm = kvm;
1967 mutex_init(&p->mutex);
1968 file->private_data = p;
1969
1970 return nonseekable_open(inode, file);
1971}
1972
1973static int debugfs_htab_release(struct inode *inode, struct file *file)
1974{
1975 struct debugfs_htab_state *p = file->private_data;
1976
1977 kvm_put_kvm(p->kvm);
1978 kfree(p);
1979 return 0;
1980}
1981
1982static ssize_t debugfs_htab_read(struct file *file, char __user *buf,
1983 size_t len, loff_t *ppos)
1984{
1985 struct debugfs_htab_state *p = file->private_data;
1986 ssize_t ret, r;
1987 unsigned long i, n;
1988 unsigned long v, hr, gr;
1989 struct kvm *kvm;
1990 __be64 *hptp;
1991
891f1ebf
PM
1992 kvm = p->kvm;
1993 if (kvm_is_radix(kvm))
1994 return 0;
1995
e23a808b
PM
1996 ret = mutex_lock_interruptible(&p->mutex);
1997 if (ret)
1998 return ret;
1999
2000 if (p->chars_left) {
2001 n = p->chars_left;
2002 if (n > len)
2003 n = len;
2004 r = copy_to_user(buf, p->buf + p->buf_index, n);
2005 n -= r;
2006 p->chars_left -= n;
2007 p->buf_index += n;
2008 buf += n;
2009 len -= n;
2010 ret = n;
2011 if (r) {
2012 if (!n)
2013 ret = -EFAULT;
2014 goto out;
2015 }
2016 }
2017
e23a808b 2018 i = p->hpt_index;
3f9d4f5a 2019 hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
3d089f84
DG
2020 for (; len != 0 && i < kvmppc_hpt_npte(&kvm->arch.hpt);
2021 ++i, hptp += 2) {
e23a808b
PM
2022 if (!(be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)))
2023 continue;
2024
2025 /* lock the HPTE so it's stable and read it */
2026 preempt_disable();
2027 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
2028 cpu_relax();
2029 v = be64_to_cpu(hptp[0]) & ~HPTE_V_HVLOCK;
2030 hr = be64_to_cpu(hptp[1]);
3f9d4f5a 2031 gr = kvm->arch.hpt.rev[i].guest_rpte;
e23a808b
PM
2032 unlock_hpte(hptp, v);
2033 preempt_enable();
2034
2035 if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
2036 continue;
2037
2038 n = scnprintf(p->buf, sizeof(p->buf),
2039 "%6lx %.16lx %.16lx %.16lx\n",
2040 i, v, hr, gr);
2041 p->chars_left = n;
2042 if (n > len)
2043 n = len;
2044 r = copy_to_user(buf, p->buf, n);
2045 n -= r;
2046 p->chars_left -= n;
2047 p->buf_index = n;
2048 buf += n;
2049 len -= n;
2050 ret += n;
2051 if (r) {
2052 if (!ret)
2053 ret = -EFAULT;
2054 goto out;
2055 }
2056 }
2057 p->hpt_index = i;
2058
2059 out:
2060 mutex_unlock(&p->mutex);
2061 return ret;
2062}
2063
025c9511 2064static ssize_t debugfs_htab_write(struct file *file, const char __user *buf,
e23a808b
PM
2065 size_t len, loff_t *ppos)
2066{
2067 return -EACCES;
2068}
2069
2070static const struct file_operations debugfs_htab_fops = {
2071 .owner = THIS_MODULE,
2072 .open = debugfs_htab_open,
2073 .release = debugfs_htab_release,
2074 .read = debugfs_htab_read,
2075 .write = debugfs_htab_write,
2076 .llseek = generic_file_llseek,
2077};
2078
2079void kvmppc_mmu_debugfs_init(struct kvm *kvm)
2080{
2081 kvm->arch.htab_dentry = debugfs_create_file("htab", 0400,
2082 kvm->arch.debugfs_dir, kvm,
2083 &debugfs_htab_fops);
2084}
2085
de56a948
PM
2086void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
2087{
2088 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
2089
c17b98cf 2090 vcpu->arch.slb_nr = 32; /* POWER7/POWER8 */
de56a948 2091
9e04ba69
PM
2092 if (kvm_is_radix(vcpu->kvm))
2093 mmu->xlate = kvmppc_mmu_radix_xlate;
2094 else
2095 mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
de56a948
PM
2096 mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
2097
2098 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
2099}