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