1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corporation, 2018
4 * Authors Suraj Jitindar Singh <sjitindarsingh@gmail.com>
5 * Paul Mackerras <paulus@ozlabs.org>
7 * Description: KVM functions specific to running nested KVM-HV guests
8 * on Book3S processors (specifically POWER9 and later).
11 #include <linux/kernel.h>
12 #include <linux/kvm_host.h>
13 #include <linux/llist.h>
14 #include <linux/pgtable.h>
16 #include <asm/kvm_ppc.h>
17 #include <asm/kvm_book3s.h>
19 #include <asm/pgalloc.h>
20 #include <asm/pte-walk.h>
23 static struct patb_entry *pseries_partition_tb;
25 static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp);
26 static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free);
28 void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
30 struct kvmppc_vcore *vc = vcpu->arch.vcore;
32 hr->pcr = vc->pcr | PCR_MASK;
33 hr->dpdes = vc->dpdes;
34 hr->hfscr = vcpu->arch.hfscr;
35 hr->tb_offset = vc->tb_offset;
36 hr->dawr0 = vcpu->arch.dawr0;
37 hr->dawrx0 = vcpu->arch.dawrx0;
38 hr->ciabr = vcpu->arch.ciabr;
39 hr->purr = vcpu->arch.purr;
40 hr->spurr = vcpu->arch.spurr;
41 hr->ic = vcpu->arch.ic;
43 hr->srr0 = vcpu->arch.shregs.srr0;
44 hr->srr1 = vcpu->arch.shregs.srr1;
45 hr->sprg[0] = vcpu->arch.shregs.sprg0;
46 hr->sprg[1] = vcpu->arch.shregs.sprg1;
47 hr->sprg[2] = vcpu->arch.shregs.sprg2;
48 hr->sprg[3] = vcpu->arch.shregs.sprg3;
49 hr->pidr = vcpu->arch.pid;
50 hr->cfar = vcpu->arch.cfar;
51 hr->ppr = vcpu->arch.ppr;
52 hr->dawr1 = vcpu->arch.dawr1;
53 hr->dawrx1 = vcpu->arch.dawrx1;
56 static void byteswap_pt_regs(struct pt_regs *regs)
58 unsigned long *addr = (unsigned long *) regs;
60 for (; addr < ((unsigned long *) (regs + 1)); addr++)
61 *addr = swab64(*addr);
64 static void byteswap_hv_regs(struct hv_guest_state *hr)
66 hr->version = swab64(hr->version);
67 hr->lpid = swab32(hr->lpid);
68 hr->vcpu_token = swab32(hr->vcpu_token);
69 hr->lpcr = swab64(hr->lpcr);
70 hr->pcr = swab64(hr->pcr) | PCR_MASK;
71 hr->amor = swab64(hr->amor);
72 hr->dpdes = swab64(hr->dpdes);
73 hr->hfscr = swab64(hr->hfscr);
74 hr->tb_offset = swab64(hr->tb_offset);
75 hr->dawr0 = swab64(hr->dawr0);
76 hr->dawrx0 = swab64(hr->dawrx0);
77 hr->ciabr = swab64(hr->ciabr);
78 hr->hdec_expiry = swab64(hr->hdec_expiry);
79 hr->purr = swab64(hr->purr);
80 hr->spurr = swab64(hr->spurr);
81 hr->ic = swab64(hr->ic);
82 hr->vtb = swab64(hr->vtb);
83 hr->hdar = swab64(hr->hdar);
84 hr->hdsisr = swab64(hr->hdsisr);
85 hr->heir = swab64(hr->heir);
86 hr->asdr = swab64(hr->asdr);
87 hr->srr0 = swab64(hr->srr0);
88 hr->srr1 = swab64(hr->srr1);
89 hr->sprg[0] = swab64(hr->sprg[0]);
90 hr->sprg[1] = swab64(hr->sprg[1]);
91 hr->sprg[2] = swab64(hr->sprg[2]);
92 hr->sprg[3] = swab64(hr->sprg[3]);
93 hr->pidr = swab64(hr->pidr);
94 hr->cfar = swab64(hr->cfar);
95 hr->ppr = swab64(hr->ppr);
96 hr->dawr1 = swab64(hr->dawr1);
97 hr->dawrx1 = swab64(hr->dawrx1);
100 static void save_hv_return_state(struct kvm_vcpu *vcpu, int trap,
101 struct hv_guest_state *hr)
103 struct kvmppc_vcore *vc = vcpu->arch.vcore;
105 hr->dpdes = vc->dpdes;
106 hr->hfscr = vcpu->arch.hfscr;
107 hr->purr = vcpu->arch.purr;
108 hr->spurr = vcpu->arch.spurr;
109 hr->ic = vcpu->arch.ic;
111 hr->srr0 = vcpu->arch.shregs.srr0;
112 hr->srr1 = vcpu->arch.shregs.srr1;
113 hr->sprg[0] = vcpu->arch.shregs.sprg0;
114 hr->sprg[1] = vcpu->arch.shregs.sprg1;
115 hr->sprg[2] = vcpu->arch.shregs.sprg2;
116 hr->sprg[3] = vcpu->arch.shregs.sprg3;
117 hr->pidr = vcpu->arch.pid;
118 hr->cfar = vcpu->arch.cfar;
119 hr->ppr = vcpu->arch.ppr;
121 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
122 hr->hdar = vcpu->arch.fault_dar;
123 hr->hdsisr = vcpu->arch.fault_dsisr;
124 hr->asdr = vcpu->arch.fault_gpa;
126 case BOOK3S_INTERRUPT_H_INST_STORAGE:
127 hr->asdr = vcpu->arch.fault_gpa;
129 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
130 hr->heir = vcpu->arch.emul_inst;
136 * This can result in some L0 HV register state being leaked to an L1
137 * hypervisor when the hv_guest_state is copied back to the guest after
138 * being modified here.
140 * There is no known problem with such a leak, and in many cases these
141 * register settings could be derived by the guest by observing behaviour
142 * and timing, interrupts, etc., but it is an issue to consider.
144 static void sanitise_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
146 struct kvmppc_vcore *vc = vcpu->arch.vcore;
150 * Don't let L1 change LPCR bits for the L2 except these:
152 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD |
153 LPCR_LPES | LPCR_MER;
156 * Additional filtering is required depending on hardware
159 hr->lpcr = kvmppc_filter_lpcr_hv(vcpu->kvm,
160 (vc->lpcr & ~mask) | (hr->lpcr & mask));
163 * Don't let L1 enable features for L2 which we've disabled for L1,
164 * but preserve the interrupt cause field.
166 hr->hfscr &= (HFSCR_INTR_CAUSE | vcpu->arch.hfscr);
168 /* Don't let data address watchpoint match in hypervisor state */
169 hr->dawrx0 &= ~DAWRX_HYP;
170 hr->dawrx1 &= ~DAWRX_HYP;
172 /* Don't let completed instruction address breakpt match in HV state */
173 if ((hr->ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
174 hr->ciabr &= ~CIABR_PRIV;
177 static void restore_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
179 struct kvmppc_vcore *vc = vcpu->arch.vcore;
181 vc->pcr = hr->pcr | PCR_MASK;
182 vc->dpdes = hr->dpdes;
183 vcpu->arch.hfscr = hr->hfscr;
184 vcpu->arch.dawr0 = hr->dawr0;
185 vcpu->arch.dawrx0 = hr->dawrx0;
186 vcpu->arch.ciabr = hr->ciabr;
187 vcpu->arch.purr = hr->purr;
188 vcpu->arch.spurr = hr->spurr;
189 vcpu->arch.ic = hr->ic;
191 vcpu->arch.shregs.srr0 = hr->srr0;
192 vcpu->arch.shregs.srr1 = hr->srr1;
193 vcpu->arch.shregs.sprg0 = hr->sprg[0];
194 vcpu->arch.shregs.sprg1 = hr->sprg[1];
195 vcpu->arch.shregs.sprg2 = hr->sprg[2];
196 vcpu->arch.shregs.sprg3 = hr->sprg[3];
197 vcpu->arch.pid = hr->pidr;
198 vcpu->arch.cfar = hr->cfar;
199 vcpu->arch.ppr = hr->ppr;
200 vcpu->arch.dawr1 = hr->dawr1;
201 vcpu->arch.dawrx1 = hr->dawrx1;
204 void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,
205 struct hv_guest_state *hr)
207 struct kvmppc_vcore *vc = vcpu->arch.vcore;
209 vc->dpdes = hr->dpdes;
210 vcpu->arch.hfscr = hr->hfscr;
211 vcpu->arch.purr = hr->purr;
212 vcpu->arch.spurr = hr->spurr;
213 vcpu->arch.ic = hr->ic;
215 vcpu->arch.fault_dar = hr->hdar;
216 vcpu->arch.fault_dsisr = hr->hdsisr;
217 vcpu->arch.fault_gpa = hr->asdr;
218 vcpu->arch.emul_inst = hr->heir;
219 vcpu->arch.shregs.srr0 = hr->srr0;
220 vcpu->arch.shregs.srr1 = hr->srr1;
221 vcpu->arch.shregs.sprg0 = hr->sprg[0];
222 vcpu->arch.shregs.sprg1 = hr->sprg[1];
223 vcpu->arch.shregs.sprg2 = hr->sprg[2];
224 vcpu->arch.shregs.sprg3 = hr->sprg[3];
225 vcpu->arch.pid = hr->pidr;
226 vcpu->arch.cfar = hr->cfar;
227 vcpu->arch.ppr = hr->ppr;
230 static void kvmhv_nested_mmio_needed(struct kvm_vcpu *vcpu, u64 regs_ptr)
232 /* No need to reflect the page fault to L1, we've handled it */
236 * Since the L2 gprs have already been written back into L1 memory when
237 * we complete the mmio, store the L1 memory location of the L2 gpr
238 * being loaded into by the mmio so that the loaded value can be
239 * written there in kvmppc_complete_mmio_load()
241 if (((vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) == KVM_MMIO_REG_GPR)
242 && (vcpu->mmio_is_write == 0)) {
243 vcpu->arch.nested_io_gpr = (gpa_t) regs_ptr +
244 offsetof(struct pt_regs,
245 gpr[vcpu->arch.io_gpr]);
246 vcpu->arch.io_gpr = KVM_MMIO_REG_NESTED_GPR;
250 static int kvmhv_read_guest_state_and_regs(struct kvm_vcpu *vcpu,
251 struct hv_guest_state *l2_hv,
252 struct pt_regs *l2_regs,
253 u64 hv_ptr, u64 regs_ptr)
257 if (kvm_vcpu_read_guest(vcpu, hv_ptr, &l2_hv->version,
258 sizeof(l2_hv->version)))
261 if (kvmppc_need_byteswap(vcpu))
262 l2_hv->version = swab64(l2_hv->version);
264 size = hv_guest_state_size(l2_hv->version);
268 return kvm_vcpu_read_guest(vcpu, hv_ptr, l2_hv, size) ||
269 kvm_vcpu_read_guest(vcpu, regs_ptr, l2_regs,
270 sizeof(struct pt_regs));
273 static int kvmhv_write_guest_state_and_regs(struct kvm_vcpu *vcpu,
274 struct hv_guest_state *l2_hv,
275 struct pt_regs *l2_regs,
276 u64 hv_ptr, u64 regs_ptr)
280 size = hv_guest_state_size(l2_hv->version);
284 return kvm_vcpu_write_guest(vcpu, hv_ptr, l2_hv, size) ||
285 kvm_vcpu_write_guest(vcpu, regs_ptr, l2_regs,
286 sizeof(struct pt_regs));
289 long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)
292 struct kvm_nested_guest *l2;
293 struct pt_regs l2_regs, saved_l1_regs;
294 struct hv_guest_state l2_hv = {0}, saved_l1_hv;
295 struct kvmppc_vcore *vc = vcpu->arch.vcore;
296 u64 hv_ptr, regs_ptr;
298 s64 delta_purr, delta_spurr, delta_ic, delta_vtb;
300 if (vcpu->kvm->arch.l1_ptcr == 0)
301 return H_NOT_AVAILABLE;
303 /* copy parameters in */
304 hv_ptr = kvmppc_get_gpr(vcpu, 4);
305 regs_ptr = kvmppc_get_gpr(vcpu, 5);
306 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
307 err = kvmhv_read_guest_state_and_regs(vcpu, &l2_hv, &l2_regs,
309 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
313 if (kvmppc_need_byteswap(vcpu))
314 byteswap_hv_regs(&l2_hv);
315 if (l2_hv.version > HV_GUEST_STATE_VERSION)
318 if (kvmppc_need_byteswap(vcpu))
319 byteswap_pt_regs(&l2_regs);
320 if (l2_hv.vcpu_token >= NR_CPUS)
324 l2 = kvmhv_get_nested(vcpu->kvm, l2_hv.lpid, true);
327 if (!l2->l1_gr_to_hr) {
328 mutex_lock(&l2->tlb_lock);
329 kvmhv_update_ptbl_cache(l2);
330 mutex_unlock(&l2->tlb_lock);
333 /* save l1 values of things */
334 vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
335 saved_l1_regs = vcpu->arch.regs;
336 kvmhv_save_hv_regs(vcpu, &saved_l1_hv);
338 /* convert TB values/offsets to host (L0) values */
339 hdec_exp = l2_hv.hdec_expiry - vc->tb_offset;
340 vc->tb_offset += l2_hv.tb_offset;
342 /* set L1 state to L2 state */
343 vcpu->arch.nested = l2;
344 vcpu->arch.nested_vcpu_id = l2_hv.vcpu_token;
345 vcpu->arch.regs = l2_regs;
347 /* Guest must always run with ME enabled, HV disabled. */
348 vcpu->arch.shregs.msr = (vcpu->arch.regs.msr | MSR_ME) & ~MSR_HV;
350 sanitise_hv_regs(vcpu, &l2_hv);
351 restore_hv_regs(vcpu, &l2_hv);
353 vcpu->arch.ret = RESUME_GUEST;
356 if (mftb() >= hdec_exp) {
357 vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER;
361 r = kvmhv_run_single_vcpu(vcpu, hdec_exp, l2_hv.lpcr);
362 } while (is_kvmppc_resume_guest(r));
364 /* save L2 state for return */
365 l2_regs = vcpu->arch.regs;
366 l2_regs.msr = vcpu->arch.shregs.msr;
367 delta_purr = vcpu->arch.purr - l2_hv.purr;
368 delta_spurr = vcpu->arch.spurr - l2_hv.spurr;
369 delta_ic = vcpu->arch.ic - l2_hv.ic;
370 delta_vtb = vc->vtb - l2_hv.vtb;
371 save_hv_return_state(vcpu, vcpu->arch.trap, &l2_hv);
373 /* restore L1 state */
374 vcpu->arch.nested = NULL;
375 vcpu->arch.regs = saved_l1_regs;
376 vcpu->arch.shregs.msr = saved_l1_regs.msr & ~MSR_TS_MASK;
377 /* set L1 MSR TS field according to L2 transaction state */
378 if (l2_regs.msr & MSR_TS_MASK)
379 vcpu->arch.shregs.msr |= MSR_TS_S;
380 vc->tb_offset = saved_l1_hv.tb_offset;
381 restore_hv_regs(vcpu, &saved_l1_hv);
382 vcpu->arch.purr += delta_purr;
383 vcpu->arch.spurr += delta_spurr;
384 vcpu->arch.ic += delta_ic;
385 vc->vtb += delta_vtb;
387 kvmhv_put_nested(l2);
389 /* copy l2_hv_state and regs back to guest */
390 if (kvmppc_need_byteswap(vcpu)) {
391 byteswap_hv_regs(&l2_hv);
392 byteswap_pt_regs(&l2_regs);
394 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
395 err = kvmhv_write_guest_state_and_regs(vcpu, &l2_hv, &l2_regs,
397 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
404 if (vcpu->mmio_needed) {
405 kvmhv_nested_mmio_needed(vcpu, regs_ptr);
409 return vcpu->arch.trap;
412 long kvmhv_nested_init(void)
418 if (!kvmhv_on_pseries())
420 if (!radix_enabled())
423 /* find log base 2 of KVMPPC_NR_LPIDS, rounding up */
424 ptb_order = __ilog2(KVMPPC_NR_LPIDS - 1) + 1;
427 pseries_partition_tb = kmalloc(sizeof(struct patb_entry) << ptb_order,
429 if (!pseries_partition_tb) {
430 pr_err("kvm-hv: failed to allocated nested partition table\n");
434 ptcr = __pa(pseries_partition_tb) | (ptb_order - 8);
435 rc = plpar_hcall_norets(H_SET_PARTITION_TABLE, ptcr);
436 if (rc != H_SUCCESS) {
437 pr_err("kvm-hv: Parent hypervisor does not support nesting (rc=%ld)\n",
439 kfree(pseries_partition_tb);
440 pseries_partition_tb = NULL;
447 void kvmhv_nested_exit(void)
450 * N.B. the kvmhv_on_pseries() test is there because it enables
451 * the compiler to remove the call to plpar_hcall_norets()
452 * when CONFIG_PPC_PSERIES=n.
454 if (kvmhv_on_pseries() && pseries_partition_tb) {
455 plpar_hcall_norets(H_SET_PARTITION_TABLE, 0);
456 kfree(pseries_partition_tb);
457 pseries_partition_tb = NULL;
461 static void kvmhv_flush_lpid(unsigned int lpid)
465 if (!kvmhv_on_pseries()) {
466 radix__flush_all_lpid(lpid);
470 rc = plpar_hcall_norets(H_TLB_INVALIDATE, H_TLBIE_P1_ENC(2, 0, 1),
471 lpid, TLBIEL_INVAL_SET_LPID);
473 pr_err("KVM: TLB LPID invalidation hcall failed, rc=%ld\n", rc);
476 void kvmhv_set_ptbl_entry(unsigned int lpid, u64 dw0, u64 dw1)
478 if (!kvmhv_on_pseries()) {
479 mmu_partition_table_set_entry(lpid, dw0, dw1, true);
483 pseries_partition_tb[lpid].patb0 = cpu_to_be64(dw0);
484 pseries_partition_tb[lpid].patb1 = cpu_to_be64(dw1);
485 /* L0 will do the necessary barriers */
486 kvmhv_flush_lpid(lpid);
489 static void kvmhv_set_nested_ptbl(struct kvm_nested_guest *gp)
493 dw0 = PATB_HR | radix__get_tree_size() |
494 __pa(gp->shadow_pgtable) | RADIX_PGD_INDEX_SIZE;
495 kvmhv_set_ptbl_entry(gp->shadow_lpid, dw0, gp->process_table);
498 void kvmhv_vm_nested_init(struct kvm *kvm)
500 kvm->arch.max_nested_lpid = -1;
504 * Handle the H_SET_PARTITION_TABLE hcall.
505 * r4 = guest real address of partition table + log_2(size) - 12
506 * (formatted as for the PTCR).
508 long kvmhv_set_partition_table(struct kvm_vcpu *vcpu)
510 struct kvm *kvm = vcpu->kvm;
511 unsigned long ptcr = kvmppc_get_gpr(vcpu, 4);
513 long ret = H_SUCCESS;
515 srcu_idx = srcu_read_lock(&kvm->srcu);
517 * Limit the partition table to 4096 entries (because that's what
518 * hardware supports), and check the base address.
520 if ((ptcr & PRTS_MASK) > 12 - 8 ||
521 !kvm_is_visible_gfn(vcpu->kvm, (ptcr & PRTB_MASK) >> PAGE_SHIFT))
523 srcu_read_unlock(&kvm->srcu, srcu_idx);
524 if (ret == H_SUCCESS)
525 kvm->arch.l1_ptcr = ptcr;
530 * Handle the H_COPY_TOFROM_GUEST hcall.
531 * r4 = L1 lpid of nested guest
533 * r6 = eaddr to access
534 * r7 = to buffer (L1 gpa)
535 * r8 = from buffer (L1 gpa)
536 * r9 = n bytes to copy
538 long kvmhv_copy_tofrom_guest_nested(struct kvm_vcpu *vcpu)
540 struct kvm_nested_guest *gp;
541 int l1_lpid = kvmppc_get_gpr(vcpu, 4);
542 int pid = kvmppc_get_gpr(vcpu, 5);
543 gva_t eaddr = kvmppc_get_gpr(vcpu, 6);
544 gpa_t gp_to = (gpa_t) kvmppc_get_gpr(vcpu, 7);
545 gpa_t gp_from = (gpa_t) kvmppc_get_gpr(vcpu, 8);
547 unsigned long n = kvmppc_get_gpr(vcpu, 9);
548 bool is_load = !!gp_to;
551 if (gp_to && gp_from) /* One must be NULL to determine the direction */
554 if (eaddr & (0xFFFUL << 52))
557 buf = kzalloc(n, GFP_KERNEL);
561 gp = kvmhv_get_nested(vcpu->kvm, l1_lpid, false);
567 mutex_lock(&gp->tlb_lock);
570 /* Load from the nested guest into our buffer */
571 rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
572 eaddr, buf, NULL, n);
576 /* Write what was loaded into our buffer back to the L1 guest */
577 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
578 rc = kvm_vcpu_write_guest(vcpu, gp_to, buf, n);
579 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
583 /* Load the data to be stored from the L1 guest into our buf */
584 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
585 rc = kvm_vcpu_read_guest(vcpu, gp_from, buf, n);
586 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
590 /* Store from our buffer into the nested guest */
591 rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
592 eaddr, NULL, buf, n);
598 mutex_unlock(&gp->tlb_lock);
599 kvmhv_put_nested(gp);
609 * Reload the partition table entry for a guest.
610 * Caller must hold gp->tlb_lock.
612 static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp)
615 struct patb_entry ptbl_entry;
616 unsigned long ptbl_addr;
617 struct kvm *kvm = gp->l1_host;
620 ptbl_addr = (kvm->arch.l1_ptcr & PRTB_MASK) + (gp->l1_lpid << 4);
621 if (gp->l1_lpid < (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 8))) {
622 int srcu_idx = srcu_read_lock(&kvm->srcu);
623 ret = kvm_read_guest(kvm, ptbl_addr,
624 &ptbl_entry, sizeof(ptbl_entry));
625 srcu_read_unlock(&kvm->srcu, srcu_idx);
629 gp->process_table = 0;
631 gp->l1_gr_to_hr = be64_to_cpu(ptbl_entry.patb0);
632 gp->process_table = be64_to_cpu(ptbl_entry.patb1);
634 kvmhv_set_nested_ptbl(gp);
637 static struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid)
639 struct kvm_nested_guest *gp;
642 gp = kzalloc(sizeof(*gp), GFP_KERNEL);
647 mutex_init(&gp->tlb_lock);
648 gp->shadow_pgtable = pgd_alloc(kvm->mm);
649 if (!gp->shadow_pgtable)
651 shadow_lpid = kvmppc_alloc_lpid();
654 gp->shadow_lpid = shadow_lpid;
657 memset(gp->prev_cpu, -1, sizeof(gp->prev_cpu));
662 pgd_free(kvm->mm, gp->shadow_pgtable);
669 * Free up any resources allocated for a nested guest.
671 static void kvmhv_release_nested(struct kvm_nested_guest *gp)
673 struct kvm *kvm = gp->l1_host;
675 if (gp->shadow_pgtable) {
677 * No vcpu is using this struct and no call to
678 * kvmhv_get_nested can find this struct,
679 * so we don't need to hold kvm->mmu_lock.
681 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
683 pgd_free(kvm->mm, gp->shadow_pgtable);
685 kvmhv_set_ptbl_entry(gp->shadow_lpid, 0, 0);
686 kvmppc_free_lpid(gp->shadow_lpid);
690 static void kvmhv_remove_nested(struct kvm_nested_guest *gp)
692 struct kvm *kvm = gp->l1_host;
693 int lpid = gp->l1_lpid;
696 spin_lock(&kvm->mmu_lock);
697 if (gp == kvm->arch.nested_guests[lpid]) {
698 kvm->arch.nested_guests[lpid] = NULL;
699 if (lpid == kvm->arch.max_nested_lpid) {
700 while (--lpid >= 0 && !kvm->arch.nested_guests[lpid])
702 kvm->arch.max_nested_lpid = lpid;
707 spin_unlock(&kvm->mmu_lock);
709 kvmhv_release_nested(gp);
713 * Free up all nested resources allocated for this guest.
714 * This is called with no vcpus of the guest running, when
715 * switching the guest to HPT mode or when destroying the
718 void kvmhv_release_all_nested(struct kvm *kvm)
721 struct kvm_nested_guest *gp;
722 struct kvm_nested_guest *freelist = NULL;
723 struct kvm_memory_slot *memslot;
726 spin_lock(&kvm->mmu_lock);
727 for (i = 0; i <= kvm->arch.max_nested_lpid; i++) {
728 gp = kvm->arch.nested_guests[i];
731 kvm->arch.nested_guests[i] = NULL;
732 if (--gp->refcnt == 0) {
737 kvm->arch.max_nested_lpid = -1;
738 spin_unlock(&kvm->mmu_lock);
739 while ((gp = freelist) != NULL) {
741 kvmhv_release_nested(gp);
744 srcu_idx = srcu_read_lock(&kvm->srcu);
745 kvm_for_each_memslot(memslot, kvm_memslots(kvm))
746 kvmhv_free_memslot_nest_rmap(memslot);
747 srcu_read_unlock(&kvm->srcu, srcu_idx);
750 /* caller must hold gp->tlb_lock */
751 static void kvmhv_flush_nested(struct kvm_nested_guest *gp)
753 struct kvm *kvm = gp->l1_host;
755 spin_lock(&kvm->mmu_lock);
756 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable, gp->shadow_lpid);
757 spin_unlock(&kvm->mmu_lock);
758 kvmhv_flush_lpid(gp->shadow_lpid);
759 kvmhv_update_ptbl_cache(gp);
760 if (gp->l1_gr_to_hr == 0)
761 kvmhv_remove_nested(gp);
764 struct kvm_nested_guest *kvmhv_get_nested(struct kvm *kvm, int l1_lpid,
767 struct kvm_nested_guest *gp, *newgp;
769 if (l1_lpid >= KVM_MAX_NESTED_GUESTS ||
770 l1_lpid >= (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 12 - 4)))
773 spin_lock(&kvm->mmu_lock);
774 gp = kvm->arch.nested_guests[l1_lpid];
777 spin_unlock(&kvm->mmu_lock);
782 newgp = kvmhv_alloc_nested(kvm, l1_lpid);
785 spin_lock(&kvm->mmu_lock);
786 if (kvm->arch.nested_guests[l1_lpid]) {
787 /* someone else beat us to it */
788 gp = kvm->arch.nested_guests[l1_lpid];
790 kvm->arch.nested_guests[l1_lpid] = newgp;
794 if (l1_lpid > kvm->arch.max_nested_lpid)
795 kvm->arch.max_nested_lpid = l1_lpid;
798 spin_unlock(&kvm->mmu_lock);
801 kvmhv_release_nested(newgp);
806 void kvmhv_put_nested(struct kvm_nested_guest *gp)
808 struct kvm *kvm = gp->l1_host;
811 spin_lock(&kvm->mmu_lock);
813 spin_unlock(&kvm->mmu_lock);
815 kvmhv_release_nested(gp);
818 static struct kvm_nested_guest *kvmhv_find_nested(struct kvm *kvm, int lpid)
820 if (lpid > kvm->arch.max_nested_lpid)
822 return kvm->arch.nested_guests[lpid];
825 pte_t *find_kvm_nested_guest_pte(struct kvm *kvm, unsigned long lpid,
826 unsigned long ea, unsigned *hshift)
828 struct kvm_nested_guest *gp;
831 gp = kvmhv_find_nested(kvm, lpid);
835 VM_WARN(!spin_is_locked(&kvm->mmu_lock),
836 "%s called with kvm mmu_lock not held \n", __func__);
837 pte = __find_linux_pte(gp->shadow_pgtable, ea, NULL, hshift);
842 static inline bool kvmhv_n_rmap_is_equal(u64 rmap_1, u64 rmap_2)
844 return !((rmap_1 ^ rmap_2) & (RMAP_NESTED_LPID_MASK |
845 RMAP_NESTED_GPA_MASK));
848 void kvmhv_insert_nest_rmap(struct kvm *kvm, unsigned long *rmapp,
849 struct rmap_nested **n_rmap)
851 struct llist_node *entry = ((struct llist_head *) rmapp)->first;
852 struct rmap_nested *cursor;
853 u64 rmap, new_rmap = (*n_rmap)->rmap;
855 /* Are there any existing entries? */
857 /* No -> use the rmap as a single entry */
858 *rmapp = new_rmap | RMAP_NESTED_IS_SINGLE_ENTRY;
862 /* Do any entries match what we're trying to insert? */
863 for_each_nest_rmap_safe(cursor, entry, &rmap) {
864 if (kvmhv_n_rmap_is_equal(rmap, new_rmap))
868 /* Do we need to create a list or just add the new entry? */
870 if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
872 llist_add(&((*n_rmap)->list), (struct llist_head *) rmapp);
873 if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
874 (*n_rmap)->list.next = (struct llist_node *) rmap;
876 /* Set NULL so not freed by caller */
880 static void kvmhv_update_nest_rmap_rc(struct kvm *kvm, u64 n_rmap,
881 unsigned long clr, unsigned long set,
882 unsigned long hpa, unsigned long mask)
885 unsigned int shift, lpid;
888 gpa = n_rmap & RMAP_NESTED_GPA_MASK;
889 lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
892 ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
894 * If the pte is present and the pfn is still the same, update the pte.
895 * If the pfn has changed then this is a stale rmap entry, the nested
896 * gpa actually points somewhere else now, and there is nothing to do.
897 * XXX A future optimisation would be to remove the rmap entry here.
899 if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa)) {
900 __radix_pte_update(ptep, clr, set);
901 kvmppc_radix_tlbie_page(kvm, gpa, shift, lpid);
906 * For a given list of rmap entries, update the rc bits in all ptes in shadow
907 * page tables for nested guests which are referenced by the rmap list.
909 void kvmhv_update_nest_rmap_rc_list(struct kvm *kvm, unsigned long *rmapp,
910 unsigned long clr, unsigned long set,
911 unsigned long hpa, unsigned long nbytes)
913 struct llist_node *entry = ((struct llist_head *) rmapp)->first;
914 struct rmap_nested *cursor;
915 unsigned long rmap, mask;
917 if ((clr | set) & ~(_PAGE_DIRTY | _PAGE_ACCESSED))
920 mask = PTE_RPN_MASK & ~(nbytes - 1);
923 for_each_nest_rmap_safe(cursor, entry, &rmap)
924 kvmhv_update_nest_rmap_rc(kvm, rmap, clr, set, hpa, mask);
927 static void kvmhv_remove_nest_rmap(struct kvm *kvm, u64 n_rmap,
928 unsigned long hpa, unsigned long mask)
930 struct kvm_nested_guest *gp;
932 unsigned int shift, lpid;
935 gpa = n_rmap & RMAP_NESTED_GPA_MASK;
936 lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
937 gp = kvmhv_find_nested(kvm, lpid);
941 /* Find and invalidate the pte */
942 ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
943 /* Don't spuriously invalidate ptes if the pfn has changed */
944 if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa))
945 kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
948 static void kvmhv_remove_nest_rmap_list(struct kvm *kvm, unsigned long *rmapp,
949 unsigned long hpa, unsigned long mask)
951 struct llist_node *entry = llist_del_all((struct llist_head *) rmapp);
952 struct rmap_nested *cursor;
955 for_each_nest_rmap_safe(cursor, entry, &rmap) {
956 kvmhv_remove_nest_rmap(kvm, rmap, hpa, mask);
961 /* called with kvm->mmu_lock held */
962 void kvmhv_remove_nest_rmap_range(struct kvm *kvm,
963 const struct kvm_memory_slot *memslot,
964 unsigned long gpa, unsigned long hpa,
965 unsigned long nbytes)
967 unsigned long gfn, end_gfn;
968 unsigned long addr_mask;
972 gfn = (gpa >> PAGE_SHIFT) - memslot->base_gfn;
973 end_gfn = gfn + (nbytes >> PAGE_SHIFT);
975 addr_mask = PTE_RPN_MASK & ~(nbytes - 1);
978 for (; gfn < end_gfn; gfn++) {
979 unsigned long *rmap = &memslot->arch.rmap[gfn];
980 kvmhv_remove_nest_rmap_list(kvm, rmap, hpa, addr_mask);
984 static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free)
988 for (page = 0; page < free->npages; page++) {
989 unsigned long rmap, *rmapp = &free->arch.rmap[page];
990 struct rmap_nested *cursor;
991 struct llist_node *entry;
993 entry = llist_del_all((struct llist_head *) rmapp);
994 for_each_nest_rmap_safe(cursor, entry, &rmap)
999 static bool kvmhv_invalidate_shadow_pte(struct kvm_vcpu *vcpu,
1000 struct kvm_nested_guest *gp,
1001 long gpa, int *shift_ret)
1003 struct kvm *kvm = vcpu->kvm;
1008 spin_lock(&kvm->mmu_lock);
1009 ptep = find_kvm_nested_guest_pte(kvm, gp->l1_lpid, gpa, &shift);
1012 if (ptep && pte_present(*ptep)) {
1013 kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
1016 spin_unlock(&kvm->mmu_lock);
1023 static inline int get_ric(unsigned int instr)
1025 return (instr >> 18) & 0x3;
1028 static inline int get_prs(unsigned int instr)
1030 return (instr >> 17) & 0x1;
1033 static inline int get_r(unsigned int instr)
1035 return (instr >> 16) & 0x1;
1038 static inline int get_lpid(unsigned long r_val)
1040 return r_val & 0xffffffff;
1043 static inline int get_is(unsigned long r_val)
1045 return (r_val >> 10) & 0x3;
1048 static inline int get_ap(unsigned long r_val)
1050 return (r_val >> 5) & 0x7;
1053 static inline long get_epn(unsigned long r_val)
1058 static int kvmhv_emulate_tlbie_tlb_addr(struct kvm_vcpu *vcpu, int lpid,
1061 struct kvm *kvm = vcpu->kvm;
1062 struct kvm_nested_guest *gp;
1064 int shift, shadow_shift;
1067 shift = ap_to_shift(ap);
1070 /* Invalid ap encoding */
1073 addr &= ~((1UL << shift) - 1);
1074 npages = 1UL << (shift - PAGE_SHIFT);
1076 gp = kvmhv_get_nested(kvm, lpid, false);
1077 if (!gp) /* No such guest -> nothing to do */
1079 mutex_lock(&gp->tlb_lock);
1081 /* There may be more than one host page backing this single guest pte */
1083 kvmhv_invalidate_shadow_pte(vcpu, gp, addr, &shadow_shift);
1085 npages -= 1UL << (shadow_shift - PAGE_SHIFT);
1086 addr += 1UL << shadow_shift;
1087 } while (npages > 0);
1089 mutex_unlock(&gp->tlb_lock);
1090 kvmhv_put_nested(gp);
1094 static void kvmhv_emulate_tlbie_lpid(struct kvm_vcpu *vcpu,
1095 struct kvm_nested_guest *gp, int ric)
1097 struct kvm *kvm = vcpu->kvm;
1099 mutex_lock(&gp->tlb_lock);
1102 /* Invalidate TLB */
1103 spin_lock(&kvm->mmu_lock);
1104 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
1106 kvmhv_flush_lpid(gp->shadow_lpid);
1107 spin_unlock(&kvm->mmu_lock);
1112 * We don't cache this -> nothing to do
1116 /* Invalidate TLB, PWC and caching of partition table entries */
1117 kvmhv_flush_nested(gp);
1122 mutex_unlock(&gp->tlb_lock);
1125 static void kvmhv_emulate_tlbie_all_lpid(struct kvm_vcpu *vcpu, int ric)
1127 struct kvm *kvm = vcpu->kvm;
1128 struct kvm_nested_guest *gp;
1131 spin_lock(&kvm->mmu_lock);
1132 for (i = 0; i <= kvm->arch.max_nested_lpid; i++) {
1133 gp = kvm->arch.nested_guests[i];
1135 spin_unlock(&kvm->mmu_lock);
1136 kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1137 spin_lock(&kvm->mmu_lock);
1140 spin_unlock(&kvm->mmu_lock);
1143 static int kvmhv_emulate_priv_tlbie(struct kvm_vcpu *vcpu, unsigned int instr,
1144 unsigned long rsval, unsigned long rbval)
1146 struct kvm *kvm = vcpu->kvm;
1147 struct kvm_nested_guest *gp;
1148 int r, ric, prs, is, ap;
1153 ric = get_ric(instr);
1154 prs = get_prs(instr);
1156 lpid = get_lpid(rsval);
1160 * These cases are invalid and are not handled:
1161 * r != 1 -> Only radix supported
1162 * prs == 1 -> Not HV privileged
1163 * ric == 3 -> No cluster bombs for radix
1164 * is == 1 -> Partition scoped translations not associated with pid
1165 * (!is) && (ric == 1 || ric == 2) -> Not supported by ISA
1167 if ((!r) || (prs) || (ric == 3) || (is == 1) ||
1168 ((!is) && (ric == 1 || ric == 2)))
1175 * Invalidate TLB for a given target address
1177 epn = get_epn(rbval);
1179 ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap, epn);
1182 /* Invalidate matching LPID */
1183 gp = kvmhv_get_nested(kvm, lpid, false);
1185 kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1186 kvmhv_put_nested(gp);
1190 /* Invalidate ALL LPIDs */
1191 kvmhv_emulate_tlbie_all_lpid(vcpu, ric);
1202 * This handles the H_TLB_INVALIDATE hcall.
1203 * Parameters are (r4) tlbie instruction code, (r5) rS contents,
1206 long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu)
1210 ret = kvmhv_emulate_priv_tlbie(vcpu, kvmppc_get_gpr(vcpu, 4),
1211 kvmppc_get_gpr(vcpu, 5), kvmppc_get_gpr(vcpu, 6));
1217 static long do_tlb_invalidate_nested_all(struct kvm_vcpu *vcpu,
1218 unsigned long lpid, unsigned long ric)
1220 struct kvm *kvm = vcpu->kvm;
1221 struct kvm_nested_guest *gp;
1223 gp = kvmhv_get_nested(kvm, lpid, false);
1225 kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1226 kvmhv_put_nested(gp);
1232 * Number of pages above which we invalidate the entire LPID rather than
1233 * flush individual pages.
1235 static unsigned long tlb_range_flush_page_ceiling __read_mostly = 33;
1237 static long do_tlb_invalidate_nested_tlb(struct kvm_vcpu *vcpu,
1239 unsigned long pg_sizes,
1240 unsigned long start,
1244 unsigned long addr, nr_pages;
1245 struct mmu_psize_def *def;
1246 unsigned long psize, ap, page_size;
1249 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
1250 def = &mmu_psize_defs[psize];
1251 if (!(pg_sizes & def->h_rpt_pgsize))
1254 nr_pages = (end - start) >> def->shift;
1255 flush_lpid = nr_pages > tlb_range_flush_page_ceiling;
1257 return do_tlb_invalidate_nested_all(vcpu, lpid,
1260 ap = mmu_get_ap(psize);
1261 page_size = 1UL << def->shift;
1263 ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap,
1268 } while (addr < end);
1274 * Performs partition-scoped invalidations for nested guests
1275 * as part of H_RPT_INVALIDATE hcall.
1277 long do_h_rpt_invalidate_pat(struct kvm_vcpu *vcpu, unsigned long lpid,
1278 unsigned long type, unsigned long pg_sizes,
1279 unsigned long start, unsigned long end)
1282 * If L2 lpid isn't valid, we need to return H_PARAMETER.
1284 * However, nested KVM issues a L2 lpid flush call when creating
1285 * partition table entries for L2. This happens even before the
1286 * corresponding shadow lpid is created in HV which happens in
1287 * H_ENTER_NESTED call. Since we can't differentiate this case from
1288 * the invalid case, we ignore such flush requests and return success.
1290 if (!kvmhv_find_nested(vcpu->kvm, lpid))
1294 * A flush all request can be handled by a full lpid flush only.
1296 if ((type & H_RPTI_TYPE_NESTED_ALL) == H_RPTI_TYPE_NESTED_ALL)
1297 return do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_ALL);
1300 * We don't need to handle a PWC flush like process table here,
1301 * because intermediate partition scoped table in nested guest doesn't
1302 * really have PWC. Only level we have PWC is in L0 and for nested
1303 * invalidate at L0 we always do kvm_flush_lpid() which does
1304 * radix__flush_all_lpid(). For range invalidate at any level, we
1305 * are not removing the higher level page tables and hence there is
1306 * no PWC invalidate needed.
1308 * if (type & H_RPTI_TYPE_PWC) {
1309 * ret = do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_PWC);
1315 if (start == 0 && end == -1)
1316 return do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_TLB);
1318 if (type & H_RPTI_TYPE_TLB)
1319 return do_tlb_invalidate_nested_tlb(vcpu, lpid, pg_sizes,
1324 /* Used to convert a nested guest real address to a L1 guest real address */
1325 static int kvmhv_translate_addr_nested(struct kvm_vcpu *vcpu,
1326 struct kvm_nested_guest *gp,
1327 unsigned long n_gpa, unsigned long dsisr,
1328 struct kvmppc_pte *gpte_p)
1330 u64 fault_addr, flags = dsisr & DSISR_ISSTORE;
1333 ret = kvmppc_mmu_walk_radix_tree(vcpu, n_gpa, gpte_p, gp->l1_gr_to_hr,
1337 /* We didn't find a pte */
1338 if (ret == -EINVAL) {
1339 /* Unsupported mmu config */
1340 flags |= DSISR_UNSUPP_MMU;
1341 } else if (ret == -ENOENT) {
1342 /* No translation found */
1343 flags |= DSISR_NOHPTE;
1344 } else if (ret == -EFAULT) {
1345 /* Couldn't access L1 real address */
1346 flags |= DSISR_PRTABLE_FAULT;
1347 vcpu->arch.fault_gpa = fault_addr;
1354 /* We found a pte -> check permissions */
1355 if (dsisr & DSISR_ISSTORE) {
1357 if (!gpte_p->may_write) {
1358 flags |= DSISR_PROTFAULT;
1361 } else if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
1362 /* Can we execute? */
1363 if (!gpte_p->may_execute) {
1364 flags |= SRR1_ISI_N_G_OR_CIP;
1369 if (!gpte_p->may_read && !gpte_p->may_write) {
1370 flags |= DSISR_PROTFAULT;
1379 vcpu->arch.fault_dsisr = flags;
1380 if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
1381 vcpu->arch.shregs.msr &= SRR1_MSR_BITS;
1382 vcpu->arch.shregs.msr |= flags;
1387 static long kvmhv_handle_nested_set_rc(struct kvm_vcpu *vcpu,
1388 struct kvm_nested_guest *gp,
1389 unsigned long n_gpa,
1390 struct kvmppc_pte gpte,
1391 unsigned long dsisr)
1393 struct kvm *kvm = vcpu->kvm;
1394 bool writing = !!(dsisr & DSISR_ISSTORE);
1398 /* Are the rc bits set in the L1 partition scoped pte? */
1399 pgflags = _PAGE_ACCESSED;
1401 pgflags |= _PAGE_DIRTY;
1402 if (pgflags & ~gpte.rc)
1405 spin_lock(&kvm->mmu_lock);
1406 /* Set the rc bit in the pte of our (L0) pgtable for the L1 guest */
1407 ret = kvmppc_hv_handle_set_rc(kvm, false, writing,
1408 gpte.raddr, kvm->arch.lpid);
1414 /* Set the rc bit in the pte of the shadow_pgtable for the nest guest */
1415 ret = kvmppc_hv_handle_set_rc(kvm, true, writing,
1416 n_gpa, gp->l1_lpid);
1423 spin_unlock(&kvm->mmu_lock);
1427 static inline int kvmppc_radix_level_to_shift(int level)
1439 static inline int kvmppc_radix_shift_to_level(int shift)
1441 if (shift == PUD_SHIFT)
1443 if (shift == PMD_SHIFT)
1445 if (shift == PAGE_SHIFT)
1451 /* called with gp->tlb_lock held */
1452 static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu,
1453 struct kvm_nested_guest *gp)
1455 struct kvm *kvm = vcpu->kvm;
1456 struct kvm_memory_slot *memslot;
1457 struct rmap_nested *n_rmap;
1458 struct kvmppc_pte gpte;
1460 unsigned long mmu_seq;
1461 unsigned long dsisr = vcpu->arch.fault_dsisr;
1462 unsigned long ea = vcpu->arch.fault_dar;
1463 unsigned long *rmapp;
1464 unsigned long n_gpa, gpa, gfn, perm = 0UL;
1465 unsigned int shift, l1_shift, level;
1466 bool writing = !!(dsisr & DSISR_ISSTORE);
1467 bool kvm_ro = false;
1470 if (!gp->l1_gr_to_hr) {
1471 kvmhv_update_ptbl_cache(gp);
1472 if (!gp->l1_gr_to_hr)
1476 /* Convert the nested guest real address into a L1 guest real address */
1478 n_gpa = vcpu->arch.fault_gpa & ~0xF000000000000FFFULL;
1479 if (!(dsisr & DSISR_PRTABLE_FAULT))
1480 n_gpa |= ea & 0xFFF;
1481 ret = kvmhv_translate_addr_nested(vcpu, gp, n_gpa, dsisr, &gpte);
1484 * If the hardware found a translation but we don't now have a usable
1485 * translation in the l1 partition-scoped tree, remove the shadow pte
1486 * and let the guest retry.
1488 if (ret == RESUME_HOST &&
1489 (dsisr & (DSISR_PROTFAULT | DSISR_BADACCESS | DSISR_NOEXEC_OR_G |
1490 DSISR_BAD_COPYPASTE)))
1495 /* Failed to set the reference/change bits */
1496 if (dsisr & DSISR_SET_RC) {
1497 ret = kvmhv_handle_nested_set_rc(vcpu, gp, n_gpa, gpte, dsisr);
1498 if (ret == RESUME_HOST)
1502 dsisr &= ~DSISR_SET_RC;
1503 if (!(dsisr & (DSISR_BAD_FAULT_64S | DSISR_NOHPTE |
1505 return RESUME_GUEST;
1509 * We took an HISI or HDSI while we were running a nested guest which
1510 * means we have no partition scoped translation for that. This means
1511 * we need to insert a pte for the mapping into our shadow_pgtable.
1514 l1_shift = gpte.page_shift;
1515 if (l1_shift < PAGE_SHIFT) {
1516 /* We don't support l1 using a page size smaller than our own */
1517 pr_err("KVM: L1 guest page shift (%d) less than our own (%d)\n",
1518 l1_shift, PAGE_SHIFT);
1522 gfn = gpa >> PAGE_SHIFT;
1524 /* 1. Get the corresponding host memslot */
1526 memslot = gfn_to_memslot(kvm, gfn);
1527 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
1528 if (dsisr & (DSISR_PRTABLE_FAULT | DSISR_BADACCESS)) {
1529 /* unusual error -> reflect to the guest as a DSI */
1530 kvmppc_core_queue_data_storage(vcpu, ea, dsisr);
1531 return RESUME_GUEST;
1534 /* passthrough of emulated MMIO case */
1535 return kvmppc_hv_emulate_mmio(vcpu, gpa, ea, writing);
1537 if (memslot->flags & KVM_MEM_READONLY) {
1539 /* Give the guest a DSI */
1540 kvmppc_core_queue_data_storage(vcpu, ea,
1541 DSISR_ISSTORE | DSISR_PROTFAULT);
1542 return RESUME_GUEST;
1547 /* 2. Find the host pte for this L1 guest real address */
1549 /* Used to check for invalidations in progress */
1550 mmu_seq = kvm->mmu_notifier_seq;
1553 /* See if can find translation in our partition scoped tables for L1 */
1555 spin_lock(&kvm->mmu_lock);
1556 pte_p = find_kvm_secondary_pte(kvm, gpa, &shift);
1561 spin_unlock(&kvm->mmu_lock);
1563 if (!pte_present(pte) || (writing && !(pte_val(pte) & _PAGE_WRITE))) {
1564 /* No suitable pte found -> try to insert a mapping */
1565 ret = kvmppc_book3s_instantiate_page(vcpu, gpa, memslot,
1566 writing, kvm_ro, &pte, &level);
1568 return RESUME_GUEST;
1571 shift = kvmppc_radix_level_to_shift(level);
1573 /* Align gfn to the start of the page */
1574 gfn = (gpa & ~((1UL << shift) - 1)) >> PAGE_SHIFT;
1576 /* 3. Compute the pte we need to insert for nest_gpa -> host r_addr */
1578 /* The permissions is the combination of the host and l1 guest ptes */
1579 perm |= gpte.may_read ? 0UL : _PAGE_READ;
1580 perm |= gpte.may_write ? 0UL : _PAGE_WRITE;
1581 perm |= gpte.may_execute ? 0UL : _PAGE_EXEC;
1582 /* Only set accessed/dirty (rc) bits if set in host and l1 guest ptes */
1583 perm |= (gpte.rc & _PAGE_ACCESSED) ? 0UL : _PAGE_ACCESSED;
1584 perm |= ((gpte.rc & _PAGE_DIRTY) && writing) ? 0UL : _PAGE_DIRTY;
1585 pte = __pte(pte_val(pte) & ~perm);
1587 /* What size pte can we insert? */
1588 if (shift > l1_shift) {
1590 unsigned int actual_shift = PAGE_SHIFT;
1591 if (PMD_SHIFT < l1_shift)
1592 actual_shift = PMD_SHIFT;
1593 mask = (1UL << shift) - (1UL << actual_shift);
1594 pte = __pte(pte_val(pte) | (gpa & mask));
1595 shift = actual_shift;
1597 level = kvmppc_radix_shift_to_level(shift);
1598 n_gpa &= ~((1UL << shift) - 1);
1600 /* 4. Insert the pte into our shadow_pgtable */
1602 n_rmap = kzalloc(sizeof(*n_rmap), GFP_KERNEL);
1604 return RESUME_GUEST; /* Let the guest try again */
1605 n_rmap->rmap = (n_gpa & RMAP_NESTED_GPA_MASK) |
1606 (((unsigned long) gp->l1_lpid) << RMAP_NESTED_LPID_SHIFT);
1607 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1608 ret = kvmppc_create_pte(kvm, gp->shadow_pgtable, pte, n_gpa, level,
1609 mmu_seq, gp->shadow_lpid, rmapp, &n_rmap);
1612 ret = RESUME_GUEST; /* Let the guest try again */
1617 kvmhv_invalidate_shadow_pte(vcpu, gp, n_gpa, NULL);
1618 return RESUME_GUEST;
1621 long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu)
1623 struct kvm_nested_guest *gp = vcpu->arch.nested;
1626 mutex_lock(&gp->tlb_lock);
1627 ret = __kvmhv_nested_page_fault(vcpu, gp);
1628 mutex_unlock(&gp->tlb_lock);
1632 int kvmhv_nested_next_lpid(struct kvm *kvm, int lpid)
1636 spin_lock(&kvm->mmu_lock);
1637 while (++lpid <= kvm->arch.max_nested_lpid) {
1638 if (kvm->arch.nested_guests[lpid]) {
1643 spin_unlock(&kvm->mmu_lock);