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