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