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>
22 #include <asm/plpar_wrappers.h>
23 #include <asm/firmware.h>
25 static struct patb_entry *pseries_partition_tb;
27 static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp);
28 static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free);
30 void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
32 struct kvmppc_vcore *vc = vcpu->arch.vcore;
34 hr->pcr = vc->pcr | PCR_MASK;
35 hr->dpdes = vc->dpdes;
36 hr->hfscr = vcpu->arch.hfscr;
37 hr->tb_offset = vc->tb_offset;
38 hr->dawr0 = vcpu->arch.dawr0;
39 hr->dawrx0 = vcpu->arch.dawrx0;
40 hr->ciabr = vcpu->arch.ciabr;
41 hr->purr = vcpu->arch.purr;
42 hr->spurr = vcpu->arch.spurr;
43 hr->ic = vcpu->arch.ic;
45 hr->srr0 = vcpu->arch.shregs.srr0;
46 hr->srr1 = vcpu->arch.shregs.srr1;
47 hr->sprg[0] = vcpu->arch.shregs.sprg0;
48 hr->sprg[1] = vcpu->arch.shregs.sprg1;
49 hr->sprg[2] = vcpu->arch.shregs.sprg2;
50 hr->sprg[3] = vcpu->arch.shregs.sprg3;
51 hr->pidr = vcpu->arch.pid;
52 hr->cfar = vcpu->arch.cfar;
53 hr->ppr = vcpu->arch.ppr;
54 hr->dawr1 = vcpu->arch.dawr1;
55 hr->dawrx1 = vcpu->arch.dawrx1;
58 /* Use noinline_for_stack due to https://bugs.llvm.org/show_bug.cgi?id=49610 */
59 static noinline_for_stack void byteswap_pt_regs(struct pt_regs *regs)
61 unsigned long *addr = (unsigned long *) regs;
63 for (; addr < ((unsigned long *) (regs + 1)); addr++)
64 *addr = swab64(*addr);
67 static void byteswap_hv_regs(struct hv_guest_state *hr)
69 hr->version = swab64(hr->version);
70 hr->lpid = swab32(hr->lpid);
71 hr->vcpu_token = swab32(hr->vcpu_token);
72 hr->lpcr = swab64(hr->lpcr);
73 hr->pcr = swab64(hr->pcr) | PCR_MASK;
74 hr->amor = swab64(hr->amor);
75 hr->dpdes = swab64(hr->dpdes);
76 hr->hfscr = swab64(hr->hfscr);
77 hr->tb_offset = swab64(hr->tb_offset);
78 hr->dawr0 = swab64(hr->dawr0);
79 hr->dawrx0 = swab64(hr->dawrx0);
80 hr->ciabr = swab64(hr->ciabr);
81 hr->hdec_expiry = swab64(hr->hdec_expiry);
82 hr->purr = swab64(hr->purr);
83 hr->spurr = swab64(hr->spurr);
84 hr->ic = swab64(hr->ic);
85 hr->vtb = swab64(hr->vtb);
86 hr->hdar = swab64(hr->hdar);
87 hr->hdsisr = swab64(hr->hdsisr);
88 hr->heir = swab64(hr->heir);
89 hr->asdr = swab64(hr->asdr);
90 hr->srr0 = swab64(hr->srr0);
91 hr->srr1 = swab64(hr->srr1);
92 hr->sprg[0] = swab64(hr->sprg[0]);
93 hr->sprg[1] = swab64(hr->sprg[1]);
94 hr->sprg[2] = swab64(hr->sprg[2]);
95 hr->sprg[3] = swab64(hr->sprg[3]);
96 hr->pidr = swab64(hr->pidr);
97 hr->cfar = swab64(hr->cfar);
98 hr->ppr = swab64(hr->ppr);
99 hr->dawr1 = swab64(hr->dawr1);
100 hr->dawrx1 = swab64(hr->dawrx1);
103 static void save_hv_return_state(struct kvm_vcpu *vcpu,
104 struct hv_guest_state *hr)
106 struct kvmppc_vcore *vc = vcpu->arch.vcore;
108 hr->dpdes = vc->dpdes;
109 hr->purr = vcpu->arch.purr;
110 hr->spurr = vcpu->arch.spurr;
111 hr->ic = vcpu->arch.ic;
113 hr->srr0 = vcpu->arch.shregs.srr0;
114 hr->srr1 = vcpu->arch.shregs.srr1;
115 hr->sprg[0] = vcpu->arch.shregs.sprg0;
116 hr->sprg[1] = vcpu->arch.shregs.sprg1;
117 hr->sprg[2] = vcpu->arch.shregs.sprg2;
118 hr->sprg[3] = vcpu->arch.shregs.sprg3;
119 hr->pidr = vcpu->arch.pid;
120 hr->cfar = vcpu->arch.cfar;
121 hr->ppr = vcpu->arch.ppr;
122 switch (vcpu->arch.trap) {
123 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
124 hr->hdar = vcpu->arch.fault_dar;
125 hr->hdsisr = vcpu->arch.fault_dsisr;
126 hr->asdr = vcpu->arch.fault_gpa;
128 case BOOK3S_INTERRUPT_H_INST_STORAGE:
129 hr->asdr = vcpu->arch.fault_gpa;
131 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
132 hr->hfscr = ((~HFSCR_INTR_CAUSE & hr->hfscr) |
133 (HFSCR_INTR_CAUSE & vcpu->arch.hfscr));
135 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
136 hr->heir = vcpu->arch.emul_inst;
141 static void restore_hv_regs(struct kvm_vcpu *vcpu, const struct hv_guest_state *hr)
143 struct kvmppc_vcore *vc = vcpu->arch.vcore;
145 vc->pcr = hr->pcr | PCR_MASK;
146 vc->dpdes = hr->dpdes;
147 vcpu->arch.hfscr = hr->hfscr;
148 vcpu->arch.dawr0 = hr->dawr0;
149 vcpu->arch.dawrx0 = hr->dawrx0;
150 vcpu->arch.ciabr = hr->ciabr;
151 vcpu->arch.purr = hr->purr;
152 vcpu->arch.spurr = hr->spurr;
153 vcpu->arch.ic = hr->ic;
155 vcpu->arch.shregs.srr0 = hr->srr0;
156 vcpu->arch.shregs.srr1 = hr->srr1;
157 vcpu->arch.shregs.sprg0 = hr->sprg[0];
158 vcpu->arch.shregs.sprg1 = hr->sprg[1];
159 vcpu->arch.shregs.sprg2 = hr->sprg[2];
160 vcpu->arch.shregs.sprg3 = hr->sprg[3];
161 vcpu->arch.pid = hr->pidr;
162 vcpu->arch.cfar = hr->cfar;
163 vcpu->arch.ppr = hr->ppr;
164 vcpu->arch.dawr1 = hr->dawr1;
165 vcpu->arch.dawrx1 = hr->dawrx1;
168 void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,
169 struct hv_guest_state *hr)
171 struct kvmppc_vcore *vc = vcpu->arch.vcore;
173 vc->dpdes = hr->dpdes;
174 vcpu->arch.hfscr = hr->hfscr;
175 vcpu->arch.purr = hr->purr;
176 vcpu->arch.spurr = hr->spurr;
177 vcpu->arch.ic = hr->ic;
179 vcpu->arch.fault_dar = hr->hdar;
180 vcpu->arch.fault_dsisr = hr->hdsisr;
181 vcpu->arch.fault_gpa = hr->asdr;
182 vcpu->arch.emul_inst = hr->heir;
183 vcpu->arch.shregs.srr0 = hr->srr0;
184 vcpu->arch.shregs.srr1 = hr->srr1;
185 vcpu->arch.shregs.sprg0 = hr->sprg[0];
186 vcpu->arch.shregs.sprg1 = hr->sprg[1];
187 vcpu->arch.shregs.sprg2 = hr->sprg[2];
188 vcpu->arch.shregs.sprg3 = hr->sprg[3];
189 vcpu->arch.pid = hr->pidr;
190 vcpu->arch.cfar = hr->cfar;
191 vcpu->arch.ppr = hr->ppr;
194 static void kvmhv_nested_mmio_needed(struct kvm_vcpu *vcpu, u64 regs_ptr)
196 /* No need to reflect the page fault to L1, we've handled it */
200 * Since the L2 gprs have already been written back into L1 memory when
201 * we complete the mmio, store the L1 memory location of the L2 gpr
202 * being loaded into by the mmio so that the loaded value can be
203 * written there in kvmppc_complete_mmio_load()
205 if (((vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) == KVM_MMIO_REG_GPR)
206 && (vcpu->mmio_is_write == 0)) {
207 vcpu->arch.nested_io_gpr = (gpa_t) regs_ptr +
208 offsetof(struct pt_regs,
209 gpr[vcpu->arch.io_gpr]);
210 vcpu->arch.io_gpr = KVM_MMIO_REG_NESTED_GPR;
214 static int kvmhv_read_guest_state_and_regs(struct kvm_vcpu *vcpu,
215 struct hv_guest_state *l2_hv,
216 struct pt_regs *l2_regs,
217 u64 hv_ptr, u64 regs_ptr)
221 if (kvm_vcpu_read_guest(vcpu, hv_ptr, &l2_hv->version,
222 sizeof(l2_hv->version)))
225 if (kvmppc_need_byteswap(vcpu))
226 l2_hv->version = swab64(l2_hv->version);
228 size = hv_guest_state_size(l2_hv->version);
232 return kvm_vcpu_read_guest(vcpu, hv_ptr, l2_hv, size) ||
233 kvm_vcpu_read_guest(vcpu, regs_ptr, l2_regs,
234 sizeof(struct pt_regs));
237 static int kvmhv_write_guest_state_and_regs(struct kvm_vcpu *vcpu,
238 struct hv_guest_state *l2_hv,
239 struct pt_regs *l2_regs,
240 u64 hv_ptr, u64 regs_ptr)
244 size = hv_guest_state_size(l2_hv->version);
248 return kvm_vcpu_write_guest(vcpu, hv_ptr, l2_hv, size) ||
249 kvm_vcpu_write_guest(vcpu, regs_ptr, l2_regs,
250 sizeof(struct pt_regs));
253 static void load_l2_hv_regs(struct kvm_vcpu *vcpu,
254 const struct hv_guest_state *l2_hv,
255 const struct hv_guest_state *l1_hv, u64 *lpcr)
257 struct kvmppc_vcore *vc = vcpu->arch.vcore;
260 restore_hv_regs(vcpu, l2_hv);
263 * Don't let L1 change LPCR bits for the L2 except these:
265 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD | LPCR_MER;
268 * Additional filtering is required depending on hardware
271 *lpcr = kvmppc_filter_lpcr_hv(vcpu->kvm,
272 (vc->lpcr & ~mask) | (*lpcr & mask));
275 * Don't let L1 enable features for L2 which we don't allow for L1,
276 * but preserve the interrupt cause field.
278 vcpu->arch.hfscr = l2_hv->hfscr & (HFSCR_INTR_CAUSE | vcpu->arch.hfscr_permitted);
280 /* Don't let data address watchpoint match in hypervisor state */
281 vcpu->arch.dawrx0 = l2_hv->dawrx0 & ~DAWRX_HYP;
282 vcpu->arch.dawrx1 = l2_hv->dawrx1 & ~DAWRX_HYP;
284 /* Don't let completed instruction address breakpt match in HV state */
285 if ((l2_hv->ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
286 vcpu->arch.ciabr = l2_hv->ciabr & ~CIABR_PRIV;
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 if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr))
306 /* copy parameters in */
307 hv_ptr = kvmppc_get_gpr(vcpu, 4);
308 regs_ptr = kvmppc_get_gpr(vcpu, 5);
309 kvm_vcpu_srcu_read_lock(vcpu);
310 err = kvmhv_read_guest_state_and_regs(vcpu, &l2_hv, &l2_regs,
312 kvm_vcpu_srcu_read_unlock(vcpu);
316 if (kvmppc_need_byteswap(vcpu))
317 byteswap_hv_regs(&l2_hv);
318 if (l2_hv.version > HV_GUEST_STATE_VERSION)
321 if (kvmppc_need_byteswap(vcpu))
322 byteswap_pt_regs(&l2_regs);
323 if (l2_hv.vcpu_token >= NR_CPUS)
327 * L1 must have set up a suspended state to enter the L2 in a
328 * transactional state, and only in that case. These have to be
329 * filtered out here to prevent causing a TM Bad Thing in the
330 * host HRFID. We could synthesize a TM Bad Thing back to the L1
331 * here but there doesn't seem like much point.
333 if (MSR_TM_SUSPENDED(vcpu->arch.shregs.msr)) {
334 if (!MSR_TM_ACTIVE(l2_regs.msr))
337 if (l2_regs.msr & MSR_TS_MASK)
339 if (WARN_ON_ONCE(vcpu->arch.shregs.msr & MSR_TS_MASK))
344 l2 = kvmhv_get_nested(vcpu->kvm, l2_hv.lpid, true);
347 if (!l2->l1_gr_to_hr) {
348 mutex_lock(&l2->tlb_lock);
349 kvmhv_update_ptbl_cache(l2);
350 mutex_unlock(&l2->tlb_lock);
353 /* save l1 values of things */
354 vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
355 saved_l1_regs = vcpu->arch.regs;
356 kvmhv_save_hv_regs(vcpu, &saved_l1_hv);
358 /* convert TB values/offsets to host (L0) values */
359 hdec_exp = l2_hv.hdec_expiry - vc->tb_offset;
360 vc->tb_offset += l2_hv.tb_offset;
361 vcpu->arch.dec_expires += l2_hv.tb_offset;
363 /* set L1 state to L2 state */
364 vcpu->arch.nested = l2;
365 vcpu->arch.nested_vcpu_id = l2_hv.vcpu_token;
366 vcpu->arch.nested_hfscr = l2_hv.hfscr;
367 vcpu->arch.regs = l2_regs;
369 /* Guest must always run with ME enabled, HV disabled. */
370 vcpu->arch.shregs.msr = (vcpu->arch.regs.msr | MSR_ME) & ~MSR_HV;
373 load_l2_hv_regs(vcpu, &l2_hv, &saved_l1_hv, &lpcr);
375 vcpu->arch.ret = RESUME_GUEST;
378 r = kvmhv_run_single_vcpu(vcpu, hdec_exp, lpcr);
379 } while (is_kvmppc_resume_guest(r));
381 /* save L2 state for return */
382 l2_regs = vcpu->arch.regs;
383 l2_regs.msr = vcpu->arch.shregs.msr;
384 delta_purr = vcpu->arch.purr - l2_hv.purr;
385 delta_spurr = vcpu->arch.spurr - l2_hv.spurr;
386 delta_ic = vcpu->arch.ic - l2_hv.ic;
387 delta_vtb = vc->vtb - l2_hv.vtb;
388 save_hv_return_state(vcpu, &l2_hv);
390 /* restore L1 state */
391 vcpu->arch.nested = NULL;
392 vcpu->arch.regs = saved_l1_regs;
393 vcpu->arch.shregs.msr = saved_l1_regs.msr & ~MSR_TS_MASK;
394 /* set L1 MSR TS field according to L2 transaction state */
395 if (l2_regs.msr & MSR_TS_MASK)
396 vcpu->arch.shregs.msr |= MSR_TS_S;
397 vc->tb_offset = saved_l1_hv.tb_offset;
398 /* XXX: is this always the same delta as saved_l1_hv.tb_offset? */
399 vcpu->arch.dec_expires -= l2_hv.tb_offset;
400 restore_hv_regs(vcpu, &saved_l1_hv);
401 vcpu->arch.purr += delta_purr;
402 vcpu->arch.spurr += delta_spurr;
403 vcpu->arch.ic += delta_ic;
404 vc->vtb += delta_vtb;
406 kvmhv_put_nested(l2);
408 /* copy l2_hv_state and regs back to guest */
409 if (kvmppc_need_byteswap(vcpu)) {
410 byteswap_hv_regs(&l2_hv);
411 byteswap_pt_regs(&l2_regs);
413 kvm_vcpu_srcu_read_lock(vcpu);
414 err = kvmhv_write_guest_state_and_regs(vcpu, &l2_hv, &l2_regs,
416 kvm_vcpu_srcu_read_unlock(vcpu);
423 if (vcpu->mmio_needed) {
424 kvmhv_nested_mmio_needed(vcpu, regs_ptr);
428 return vcpu->arch.trap;
431 unsigned long nested_capabilities;
433 long kvmhv_nested_init(void)
436 unsigned long ptcr, host_capabilities;
439 if (!kvmhv_on_pseries())
441 if (!radix_enabled())
444 rc = plpar_guest_get_capabilities(0, &host_capabilities);
445 if (rc == H_SUCCESS) {
446 unsigned long capabilities = 0;
448 if (cpu_has_feature(CPU_FTR_ARCH_31))
449 capabilities |= H_GUEST_CAP_POWER10;
450 if (cpu_has_feature(CPU_FTR_ARCH_300))
451 capabilities |= H_GUEST_CAP_POWER9;
453 nested_capabilities = capabilities & host_capabilities;
454 rc = plpar_guest_set_capabilities(0, nested_capabilities);
455 if (rc != H_SUCCESS) {
456 pr_err("kvm-hv: Could not configure parent hypervisor capabilities (rc=%ld)",
461 static_branch_enable(&__kvmhv_is_nestedv2);
465 pr_info("kvm-hv: nestedv2 get capabilities hcall failed, falling back to nestedv1 (rc=%ld)\n",
467 /* Partition table entry is 1<<4 bytes in size, hence the 4. */
468 ptb_order = KVM_MAX_NESTED_GUESTS_SHIFT + 4;
469 /* Minimum partition table size is 1<<12 bytes */
472 pseries_partition_tb = kmalloc(sizeof(struct patb_entry) << ptb_order,
474 if (!pseries_partition_tb) {
475 pr_err("kvm-hv: failed to allocated nested partition table\n");
479 ptcr = __pa(pseries_partition_tb) | (ptb_order - 12);
480 rc = plpar_hcall_norets(H_SET_PARTITION_TABLE, ptcr);
481 if (rc != H_SUCCESS) {
482 pr_err("kvm-hv: Parent hypervisor does not support nesting (rc=%ld)\n",
484 kfree(pseries_partition_tb);
485 pseries_partition_tb = NULL;
492 void kvmhv_nested_exit(void)
495 * N.B. the kvmhv_on_pseries() test is there because it enables
496 * the compiler to remove the call to plpar_hcall_norets()
497 * when CONFIG_PPC_PSERIES=n.
499 if (kvmhv_on_pseries() && pseries_partition_tb) {
500 plpar_hcall_norets(H_SET_PARTITION_TABLE, 0);
501 kfree(pseries_partition_tb);
502 pseries_partition_tb = NULL;
506 static void kvmhv_flush_lpid(u64 lpid)
510 if (!kvmhv_on_pseries()) {
511 radix__flush_all_lpid(lpid);
515 if (!firmware_has_feature(FW_FEATURE_RPT_INVALIDATE))
516 rc = plpar_hcall_norets(H_TLB_INVALIDATE, H_TLBIE_P1_ENC(2, 0, 1),
517 lpid, TLBIEL_INVAL_SET_LPID);
519 rc = pseries_rpt_invalidate(lpid, H_RPTI_TARGET_CMMU,
521 H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
523 H_RPTI_PAGE_ALL, 0, -1UL);
525 pr_err("KVM: TLB LPID invalidation hcall failed, rc=%ld\n", rc);
528 void kvmhv_set_ptbl_entry(u64 lpid, u64 dw0, u64 dw1)
530 if (!kvmhv_on_pseries()) {
531 mmu_partition_table_set_entry(lpid, dw0, dw1, true);
535 if (kvmhv_is_nestedv1()) {
536 pseries_partition_tb[lpid].patb0 = cpu_to_be64(dw0);
537 pseries_partition_tb[lpid].patb1 = cpu_to_be64(dw1);
538 /* L0 will do the necessary barriers */
539 kvmhv_flush_lpid(lpid);
542 if (kvmhv_is_nestedv2())
543 kvmhv_nestedv2_set_ptbl_entry(lpid, dw0, dw1);
546 static void kvmhv_set_nested_ptbl(struct kvm_nested_guest *gp)
550 dw0 = PATB_HR | radix__get_tree_size() |
551 __pa(gp->shadow_pgtable) | RADIX_PGD_INDEX_SIZE;
552 kvmhv_set_ptbl_entry(gp->shadow_lpid, dw0, gp->process_table);
556 * Handle the H_SET_PARTITION_TABLE hcall.
557 * r4 = guest real address of partition table + log_2(size) - 12
558 * (formatted as for the PTCR).
560 long kvmhv_set_partition_table(struct kvm_vcpu *vcpu)
562 struct kvm *kvm = vcpu->kvm;
563 unsigned long ptcr = kvmppc_get_gpr(vcpu, 4);
565 long ret = H_SUCCESS;
567 srcu_idx = srcu_read_lock(&kvm->srcu);
568 /* Check partition size and base address. */
569 if ((ptcr & PRTS_MASK) + 12 - 4 > KVM_MAX_NESTED_GUESTS_SHIFT ||
570 !kvm_is_visible_gfn(vcpu->kvm, (ptcr & PRTB_MASK) >> PAGE_SHIFT))
572 srcu_read_unlock(&kvm->srcu, srcu_idx);
573 if (ret == H_SUCCESS)
574 kvm->arch.l1_ptcr = ptcr;
580 * Handle the H_COPY_TOFROM_GUEST hcall.
581 * r4 = L1 lpid of nested guest
583 * r6 = eaddr to access
584 * r7 = to buffer (L1 gpa)
585 * r8 = from buffer (L1 gpa)
586 * r9 = n bytes to copy
588 long kvmhv_copy_tofrom_guest_nested(struct kvm_vcpu *vcpu)
590 struct kvm_nested_guest *gp;
591 int l1_lpid = kvmppc_get_gpr(vcpu, 4);
592 int pid = kvmppc_get_gpr(vcpu, 5);
593 gva_t eaddr = kvmppc_get_gpr(vcpu, 6);
594 gpa_t gp_to = (gpa_t) kvmppc_get_gpr(vcpu, 7);
595 gpa_t gp_from = (gpa_t) kvmppc_get_gpr(vcpu, 8);
597 unsigned long n = kvmppc_get_gpr(vcpu, 9);
598 bool is_load = !!gp_to;
601 if (gp_to && gp_from) /* One must be NULL to determine the direction */
604 if (eaddr & (0xFFFUL << 52))
607 buf = kzalloc(n, GFP_KERNEL | __GFP_NOWARN);
611 gp = kvmhv_get_nested(vcpu->kvm, l1_lpid, false);
617 mutex_lock(&gp->tlb_lock);
620 /* Load from the nested guest into our buffer */
621 rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
622 eaddr, buf, NULL, n);
626 /* Write what was loaded into our buffer back to the L1 guest */
627 kvm_vcpu_srcu_read_lock(vcpu);
628 rc = kvm_vcpu_write_guest(vcpu, gp_to, buf, n);
629 kvm_vcpu_srcu_read_unlock(vcpu);
633 /* Load the data to be stored from the L1 guest into our buf */
634 kvm_vcpu_srcu_read_lock(vcpu);
635 rc = kvm_vcpu_read_guest(vcpu, gp_from, buf, n);
636 kvm_vcpu_srcu_read_unlock(vcpu);
640 /* Store from our buffer into the nested guest */
641 rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
642 eaddr, NULL, buf, n);
648 mutex_unlock(&gp->tlb_lock);
649 kvmhv_put_nested(gp);
659 * Reload the partition table entry for a guest.
660 * Caller must hold gp->tlb_lock.
662 static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp)
665 struct patb_entry ptbl_entry;
666 unsigned long ptbl_addr;
667 struct kvm *kvm = gp->l1_host;
670 ptbl_addr = (kvm->arch.l1_ptcr & PRTB_MASK) + (gp->l1_lpid << 4);
671 if (gp->l1_lpid < (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 12 - 4))) {
672 int srcu_idx = srcu_read_lock(&kvm->srcu);
673 ret = kvm_read_guest(kvm, ptbl_addr,
674 &ptbl_entry, sizeof(ptbl_entry));
675 srcu_read_unlock(&kvm->srcu, srcu_idx);
679 gp->process_table = 0;
681 gp->l1_gr_to_hr = be64_to_cpu(ptbl_entry.patb0);
682 gp->process_table = be64_to_cpu(ptbl_entry.patb1);
684 kvmhv_set_nested_ptbl(gp);
687 void kvmhv_vm_nested_init(struct kvm *kvm)
689 idr_init(&kvm->arch.kvm_nested_guest_idr);
692 static struct kvm_nested_guest *__find_nested(struct kvm *kvm, int lpid)
694 return idr_find(&kvm->arch.kvm_nested_guest_idr, lpid);
697 static bool __prealloc_nested(struct kvm *kvm, int lpid)
699 if (idr_alloc(&kvm->arch.kvm_nested_guest_idr,
700 NULL, lpid, lpid + 1, GFP_KERNEL) != lpid)
705 static void __add_nested(struct kvm *kvm, int lpid, struct kvm_nested_guest *gp)
707 if (idr_replace(&kvm->arch.kvm_nested_guest_idr, gp, lpid))
711 static void __remove_nested(struct kvm *kvm, int lpid)
713 idr_remove(&kvm->arch.kvm_nested_guest_idr, lpid);
716 static struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid)
718 struct kvm_nested_guest *gp;
721 gp = kzalloc(sizeof(*gp), GFP_KERNEL);
726 mutex_init(&gp->tlb_lock);
727 gp->shadow_pgtable = pgd_alloc(kvm->mm);
728 if (!gp->shadow_pgtable)
730 shadow_lpid = kvmppc_alloc_lpid();
733 gp->shadow_lpid = shadow_lpid;
736 memset(gp->prev_cpu, -1, sizeof(gp->prev_cpu));
741 pgd_free(kvm->mm, gp->shadow_pgtable);
748 * Free up any resources allocated for a nested guest.
750 static void kvmhv_release_nested(struct kvm_nested_guest *gp)
752 struct kvm *kvm = gp->l1_host;
754 if (gp->shadow_pgtable) {
756 * No vcpu is using this struct and no call to
757 * kvmhv_get_nested can find this struct,
758 * so we don't need to hold kvm->mmu_lock.
760 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
762 pgd_free(kvm->mm, gp->shadow_pgtable);
764 kvmhv_set_ptbl_entry(gp->shadow_lpid, 0, 0);
765 kvmppc_free_lpid(gp->shadow_lpid);
769 static void kvmhv_remove_nested(struct kvm_nested_guest *gp)
771 struct kvm *kvm = gp->l1_host;
772 int lpid = gp->l1_lpid;
775 spin_lock(&kvm->mmu_lock);
776 if (gp == __find_nested(kvm, lpid)) {
777 __remove_nested(kvm, lpid);
781 spin_unlock(&kvm->mmu_lock);
783 kvmhv_release_nested(gp);
787 * Free up all nested resources allocated for this guest.
788 * This is called with no vcpus of the guest running, when
789 * switching the guest to HPT mode or when destroying the
792 void kvmhv_release_all_nested(struct kvm *kvm)
795 struct kvm_nested_guest *gp;
796 struct kvm_nested_guest *freelist = NULL;
797 struct kvm_memory_slot *memslot;
800 spin_lock(&kvm->mmu_lock);
801 idr_for_each_entry(&kvm->arch.kvm_nested_guest_idr, gp, lpid) {
802 __remove_nested(kvm, lpid);
803 if (--gp->refcnt == 0) {
808 idr_destroy(&kvm->arch.kvm_nested_guest_idr);
809 /* idr is empty and may be reused at this point */
810 spin_unlock(&kvm->mmu_lock);
811 while ((gp = freelist) != NULL) {
813 kvmhv_release_nested(gp);
816 srcu_idx = srcu_read_lock(&kvm->srcu);
817 kvm_for_each_memslot(memslot, bkt, kvm_memslots(kvm))
818 kvmhv_free_memslot_nest_rmap(memslot);
819 srcu_read_unlock(&kvm->srcu, srcu_idx);
822 /* caller must hold gp->tlb_lock */
823 static void kvmhv_flush_nested(struct kvm_nested_guest *gp)
825 struct kvm *kvm = gp->l1_host;
827 spin_lock(&kvm->mmu_lock);
828 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable, gp->shadow_lpid);
829 spin_unlock(&kvm->mmu_lock);
830 kvmhv_flush_lpid(gp->shadow_lpid);
831 kvmhv_update_ptbl_cache(gp);
832 if (gp->l1_gr_to_hr == 0)
833 kvmhv_remove_nested(gp);
836 struct kvm_nested_guest *kvmhv_get_nested(struct kvm *kvm, int l1_lpid,
839 struct kvm_nested_guest *gp, *newgp;
841 if (l1_lpid >= (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 12 - 4)))
844 spin_lock(&kvm->mmu_lock);
845 gp = __find_nested(kvm, l1_lpid);
848 spin_unlock(&kvm->mmu_lock);
853 newgp = kvmhv_alloc_nested(kvm, l1_lpid);
857 if (!__prealloc_nested(kvm, l1_lpid)) {
858 kvmhv_release_nested(newgp);
862 spin_lock(&kvm->mmu_lock);
863 gp = __find_nested(kvm, l1_lpid);
865 __add_nested(kvm, l1_lpid, newgp);
871 spin_unlock(&kvm->mmu_lock);
874 kvmhv_release_nested(newgp);
879 void kvmhv_put_nested(struct kvm_nested_guest *gp)
881 struct kvm *kvm = gp->l1_host;
884 spin_lock(&kvm->mmu_lock);
886 spin_unlock(&kvm->mmu_lock);
888 kvmhv_release_nested(gp);
891 pte_t *find_kvm_nested_guest_pte(struct kvm *kvm, unsigned long lpid,
892 unsigned long ea, unsigned *hshift)
894 struct kvm_nested_guest *gp;
897 gp = __find_nested(kvm, lpid);
901 VM_WARN(!spin_is_locked(&kvm->mmu_lock),
902 "%s called with kvm mmu_lock not held \n", __func__);
903 pte = __find_linux_pte(gp->shadow_pgtable, ea, NULL, hshift);
908 static inline bool kvmhv_n_rmap_is_equal(u64 rmap_1, u64 rmap_2)
910 return !((rmap_1 ^ rmap_2) & (RMAP_NESTED_LPID_MASK |
911 RMAP_NESTED_GPA_MASK));
914 void kvmhv_insert_nest_rmap(struct kvm *kvm, unsigned long *rmapp,
915 struct rmap_nested **n_rmap)
917 struct llist_node *entry = ((struct llist_head *) rmapp)->first;
918 struct rmap_nested *cursor;
919 u64 rmap, new_rmap = (*n_rmap)->rmap;
921 /* Are there any existing entries? */
923 /* No -> use the rmap as a single entry */
924 *rmapp = new_rmap | RMAP_NESTED_IS_SINGLE_ENTRY;
928 /* Do any entries match what we're trying to insert? */
929 for_each_nest_rmap_safe(cursor, entry, &rmap) {
930 if (kvmhv_n_rmap_is_equal(rmap, new_rmap))
934 /* Do we need to create a list or just add the new entry? */
936 if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
938 llist_add(&((*n_rmap)->list), (struct llist_head *) rmapp);
939 if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
940 (*n_rmap)->list.next = (struct llist_node *) rmap;
942 /* Set NULL so not freed by caller */
946 static void kvmhv_update_nest_rmap_rc(struct kvm *kvm, u64 n_rmap,
947 unsigned long clr, unsigned long set,
948 unsigned long hpa, unsigned long mask)
951 unsigned int shift, lpid;
954 gpa = n_rmap & RMAP_NESTED_GPA_MASK;
955 lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
958 ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
960 * If the pte is present and the pfn is still the same, update the pte.
961 * If the pfn has changed then this is a stale rmap entry, the nested
962 * gpa actually points somewhere else now, and there is nothing to do.
963 * XXX A future optimisation would be to remove the rmap entry here.
965 if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa)) {
966 __radix_pte_update(ptep, clr, set);
967 kvmppc_radix_tlbie_page(kvm, gpa, shift, lpid);
972 * For a given list of rmap entries, update the rc bits in all ptes in shadow
973 * page tables for nested guests which are referenced by the rmap list.
975 void kvmhv_update_nest_rmap_rc_list(struct kvm *kvm, unsigned long *rmapp,
976 unsigned long clr, unsigned long set,
977 unsigned long hpa, unsigned long nbytes)
979 struct llist_node *entry = ((struct llist_head *) rmapp)->first;
980 struct rmap_nested *cursor;
981 unsigned long rmap, mask;
983 if ((clr | set) & ~(_PAGE_DIRTY | _PAGE_ACCESSED))
986 mask = PTE_RPN_MASK & ~(nbytes - 1);
989 for_each_nest_rmap_safe(cursor, entry, &rmap)
990 kvmhv_update_nest_rmap_rc(kvm, rmap, clr, set, hpa, mask);
993 static void kvmhv_remove_nest_rmap(struct kvm *kvm, u64 n_rmap,
994 unsigned long hpa, unsigned long mask)
996 struct kvm_nested_guest *gp;
998 unsigned int shift, lpid;
1001 gpa = n_rmap & RMAP_NESTED_GPA_MASK;
1002 lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
1003 gp = __find_nested(kvm, lpid);
1007 /* Find and invalidate the pte */
1008 ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
1009 /* Don't spuriously invalidate ptes if the pfn has changed */
1010 if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa))
1011 kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
1014 static void kvmhv_remove_nest_rmap_list(struct kvm *kvm, unsigned long *rmapp,
1015 unsigned long hpa, unsigned long mask)
1017 struct llist_node *entry = llist_del_all((struct llist_head *) rmapp);
1018 struct rmap_nested *cursor;
1021 for_each_nest_rmap_safe(cursor, entry, &rmap) {
1022 kvmhv_remove_nest_rmap(kvm, rmap, hpa, mask);
1027 /* called with kvm->mmu_lock held */
1028 void kvmhv_remove_nest_rmap_range(struct kvm *kvm,
1029 const struct kvm_memory_slot *memslot,
1030 unsigned long gpa, unsigned long hpa,
1031 unsigned long nbytes)
1033 unsigned long gfn, end_gfn;
1034 unsigned long addr_mask;
1038 gfn = (gpa >> PAGE_SHIFT) - memslot->base_gfn;
1039 end_gfn = gfn + (nbytes >> PAGE_SHIFT);
1041 addr_mask = PTE_RPN_MASK & ~(nbytes - 1);
1044 for (; gfn < end_gfn; gfn++) {
1045 unsigned long *rmap = &memslot->arch.rmap[gfn];
1046 kvmhv_remove_nest_rmap_list(kvm, rmap, hpa, addr_mask);
1050 static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free)
1054 for (page = 0; page < free->npages; page++) {
1055 unsigned long rmap, *rmapp = &free->arch.rmap[page];
1056 struct rmap_nested *cursor;
1057 struct llist_node *entry;
1059 entry = llist_del_all((struct llist_head *) rmapp);
1060 for_each_nest_rmap_safe(cursor, entry, &rmap)
1065 static bool kvmhv_invalidate_shadow_pte(struct kvm_vcpu *vcpu,
1066 struct kvm_nested_guest *gp,
1067 long gpa, int *shift_ret)
1069 struct kvm *kvm = vcpu->kvm;
1074 spin_lock(&kvm->mmu_lock);
1075 ptep = find_kvm_nested_guest_pte(kvm, gp->l1_lpid, gpa, &shift);
1078 if (ptep && pte_present(*ptep)) {
1079 kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
1082 spin_unlock(&kvm->mmu_lock);
1089 static inline int get_ric(unsigned int instr)
1091 return (instr >> 18) & 0x3;
1094 static inline int get_prs(unsigned int instr)
1096 return (instr >> 17) & 0x1;
1099 static inline int get_r(unsigned int instr)
1101 return (instr >> 16) & 0x1;
1104 static inline int get_lpid(unsigned long r_val)
1106 return r_val & 0xffffffff;
1109 static inline int get_is(unsigned long r_val)
1111 return (r_val >> 10) & 0x3;
1114 static inline int get_ap(unsigned long r_val)
1116 return (r_val >> 5) & 0x7;
1119 static inline long get_epn(unsigned long r_val)
1124 static int kvmhv_emulate_tlbie_tlb_addr(struct kvm_vcpu *vcpu, int lpid,
1127 struct kvm *kvm = vcpu->kvm;
1128 struct kvm_nested_guest *gp;
1130 int shift, shadow_shift;
1133 shift = ap_to_shift(ap);
1136 /* Invalid ap encoding */
1139 addr &= ~((1UL << shift) - 1);
1140 npages = 1UL << (shift - PAGE_SHIFT);
1142 gp = kvmhv_get_nested(kvm, lpid, false);
1143 if (!gp) /* No such guest -> nothing to do */
1145 mutex_lock(&gp->tlb_lock);
1147 /* There may be more than one host page backing this single guest pte */
1149 kvmhv_invalidate_shadow_pte(vcpu, gp, addr, &shadow_shift);
1151 npages -= 1UL << (shadow_shift - PAGE_SHIFT);
1152 addr += 1UL << shadow_shift;
1153 } while (npages > 0);
1155 mutex_unlock(&gp->tlb_lock);
1156 kvmhv_put_nested(gp);
1160 static void kvmhv_emulate_tlbie_lpid(struct kvm_vcpu *vcpu,
1161 struct kvm_nested_guest *gp, int ric)
1163 struct kvm *kvm = vcpu->kvm;
1165 mutex_lock(&gp->tlb_lock);
1168 /* Invalidate TLB */
1169 spin_lock(&kvm->mmu_lock);
1170 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
1172 kvmhv_flush_lpid(gp->shadow_lpid);
1173 spin_unlock(&kvm->mmu_lock);
1178 * We don't cache this -> nothing to do
1182 /* Invalidate TLB, PWC and caching of partition table entries */
1183 kvmhv_flush_nested(gp);
1188 mutex_unlock(&gp->tlb_lock);
1191 static void kvmhv_emulate_tlbie_all_lpid(struct kvm_vcpu *vcpu, int ric)
1193 struct kvm *kvm = vcpu->kvm;
1194 struct kvm_nested_guest *gp;
1197 spin_lock(&kvm->mmu_lock);
1198 idr_for_each_entry(&kvm->arch.kvm_nested_guest_idr, gp, lpid) {
1199 spin_unlock(&kvm->mmu_lock);
1200 kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1201 spin_lock(&kvm->mmu_lock);
1203 spin_unlock(&kvm->mmu_lock);
1206 static int kvmhv_emulate_priv_tlbie(struct kvm_vcpu *vcpu, unsigned int instr,
1207 unsigned long rsval, unsigned long rbval)
1209 struct kvm *kvm = vcpu->kvm;
1210 struct kvm_nested_guest *gp;
1211 int r, ric, prs, is, ap;
1216 ric = get_ric(instr);
1217 prs = get_prs(instr);
1219 lpid = get_lpid(rsval);
1223 * These cases are invalid and are not handled:
1224 * r != 1 -> Only radix supported
1225 * prs == 1 -> Not HV privileged
1226 * ric == 3 -> No cluster bombs for radix
1227 * is == 1 -> Partition scoped translations not associated with pid
1228 * (!is) && (ric == 1 || ric == 2) -> Not supported by ISA
1230 if ((!r) || (prs) || (ric == 3) || (is == 1) ||
1231 ((!is) && (ric == 1 || ric == 2)))
1238 * Invalidate TLB for a given target address
1240 epn = get_epn(rbval);
1242 ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap, epn);
1245 /* Invalidate matching LPID */
1246 gp = kvmhv_get_nested(kvm, lpid, false);
1248 kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1249 kvmhv_put_nested(gp);
1253 /* Invalidate ALL LPIDs */
1254 kvmhv_emulate_tlbie_all_lpid(vcpu, ric);
1265 * This handles the H_TLB_INVALIDATE hcall.
1266 * Parameters are (r4) tlbie instruction code, (r5) rS contents,
1269 long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu)
1273 ret = kvmhv_emulate_priv_tlbie(vcpu, kvmppc_get_gpr(vcpu, 4),
1274 kvmppc_get_gpr(vcpu, 5), kvmppc_get_gpr(vcpu, 6));
1280 static long do_tlb_invalidate_nested_all(struct kvm_vcpu *vcpu,
1281 unsigned long lpid, unsigned long ric)
1283 struct kvm *kvm = vcpu->kvm;
1284 struct kvm_nested_guest *gp;
1286 gp = kvmhv_get_nested(kvm, lpid, false);
1288 kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1289 kvmhv_put_nested(gp);
1295 * Number of pages above which we invalidate the entire LPID rather than
1296 * flush individual pages.
1298 static unsigned long tlb_range_flush_page_ceiling __read_mostly = 33;
1300 static long do_tlb_invalidate_nested_tlb(struct kvm_vcpu *vcpu,
1302 unsigned long pg_sizes,
1303 unsigned long start,
1307 unsigned long addr, nr_pages;
1308 struct mmu_psize_def *def;
1309 unsigned long psize, ap, page_size;
1312 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
1313 def = &mmu_psize_defs[psize];
1314 if (!(pg_sizes & def->h_rpt_pgsize))
1317 nr_pages = (end - start) >> def->shift;
1318 flush_lpid = nr_pages > tlb_range_flush_page_ceiling;
1320 return do_tlb_invalidate_nested_all(vcpu, lpid,
1323 ap = mmu_get_ap(psize);
1324 page_size = 1UL << def->shift;
1326 ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap,
1331 } while (addr < end);
1337 * Performs partition-scoped invalidations for nested guests
1338 * as part of H_RPT_INVALIDATE hcall.
1340 long do_h_rpt_invalidate_pat(struct kvm_vcpu *vcpu, unsigned long lpid,
1341 unsigned long type, unsigned long pg_sizes,
1342 unsigned long start, unsigned long end)
1345 * If L2 lpid isn't valid, we need to return H_PARAMETER.
1347 * However, nested KVM issues a L2 lpid flush call when creating
1348 * partition table entries for L2. This happens even before the
1349 * corresponding shadow lpid is created in HV which happens in
1350 * H_ENTER_NESTED call. Since we can't differentiate this case from
1351 * the invalid case, we ignore such flush requests and return success.
1353 if (!__find_nested(vcpu->kvm, lpid))
1357 * A flush all request can be handled by a full lpid flush only.
1359 if ((type & H_RPTI_TYPE_NESTED_ALL) == H_RPTI_TYPE_NESTED_ALL)
1360 return do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_ALL);
1363 * We don't need to handle a PWC flush like process table here,
1364 * because intermediate partition scoped table in nested guest doesn't
1365 * really have PWC. Only level we have PWC is in L0 and for nested
1366 * invalidate at L0 we always do kvm_flush_lpid() which does
1367 * radix__flush_all_lpid(). For range invalidate at any level, we
1368 * are not removing the higher level page tables and hence there is
1369 * no PWC invalidate needed.
1371 * if (type & H_RPTI_TYPE_PWC) {
1372 * ret = do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_PWC);
1378 if (start == 0 && end == -1)
1379 return do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_TLB);
1381 if (type & H_RPTI_TYPE_TLB)
1382 return do_tlb_invalidate_nested_tlb(vcpu, lpid, pg_sizes,
1387 /* Used to convert a nested guest real address to a L1 guest real address */
1388 static int kvmhv_translate_addr_nested(struct kvm_vcpu *vcpu,
1389 struct kvm_nested_guest *gp,
1390 unsigned long n_gpa, unsigned long dsisr,
1391 struct kvmppc_pte *gpte_p)
1393 u64 fault_addr, flags = dsisr & DSISR_ISSTORE;
1396 ret = kvmppc_mmu_walk_radix_tree(vcpu, n_gpa, gpte_p, gp->l1_gr_to_hr,
1400 /* We didn't find a pte */
1401 if (ret == -EINVAL) {
1402 /* Unsupported mmu config */
1403 flags |= DSISR_UNSUPP_MMU;
1404 } else if (ret == -ENOENT) {
1405 /* No translation found */
1406 flags |= DSISR_NOHPTE;
1407 } else if (ret == -EFAULT) {
1408 /* Couldn't access L1 real address */
1409 flags |= DSISR_PRTABLE_FAULT;
1410 vcpu->arch.fault_gpa = fault_addr;
1417 /* We found a pte -> check permissions */
1418 if (dsisr & DSISR_ISSTORE) {
1420 if (!gpte_p->may_write) {
1421 flags |= DSISR_PROTFAULT;
1424 } else if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
1425 /* Can we execute? */
1426 if (!gpte_p->may_execute) {
1427 flags |= SRR1_ISI_N_G_OR_CIP;
1432 if (!gpte_p->may_read && !gpte_p->may_write) {
1433 flags |= DSISR_PROTFAULT;
1442 vcpu->arch.fault_dsisr = flags;
1443 if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
1444 vcpu->arch.shregs.msr &= SRR1_MSR_BITS;
1445 vcpu->arch.shregs.msr |= flags;
1450 static long kvmhv_handle_nested_set_rc(struct kvm_vcpu *vcpu,
1451 struct kvm_nested_guest *gp,
1452 unsigned long n_gpa,
1453 struct kvmppc_pte gpte,
1454 unsigned long dsisr)
1456 struct kvm *kvm = vcpu->kvm;
1457 bool writing = !!(dsisr & DSISR_ISSTORE);
1461 /* Are the rc bits set in the L1 partition scoped pte? */
1462 pgflags = _PAGE_ACCESSED;
1464 pgflags |= _PAGE_DIRTY;
1465 if (pgflags & ~gpte.rc)
1468 spin_lock(&kvm->mmu_lock);
1469 /* Set the rc bit in the pte of our (L0) pgtable for the L1 guest */
1470 ret = kvmppc_hv_handle_set_rc(kvm, false, writing,
1471 gpte.raddr, kvm->arch.lpid);
1477 /* Set the rc bit in the pte of the shadow_pgtable for the nest guest */
1478 ret = kvmppc_hv_handle_set_rc(kvm, true, writing,
1479 n_gpa, gp->l1_lpid);
1486 spin_unlock(&kvm->mmu_lock);
1490 static inline int kvmppc_radix_level_to_shift(int level)
1502 static inline int kvmppc_radix_shift_to_level(int shift)
1504 if (shift == PUD_SHIFT)
1506 if (shift == PMD_SHIFT)
1508 if (shift == PAGE_SHIFT)
1514 /* called with gp->tlb_lock held */
1515 static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu,
1516 struct kvm_nested_guest *gp)
1518 struct kvm *kvm = vcpu->kvm;
1519 struct kvm_memory_slot *memslot;
1520 struct rmap_nested *n_rmap;
1521 struct kvmppc_pte gpte;
1523 unsigned long mmu_seq;
1524 unsigned long dsisr = vcpu->arch.fault_dsisr;
1525 unsigned long ea = vcpu->arch.fault_dar;
1526 unsigned long *rmapp;
1527 unsigned long n_gpa, gpa, gfn, perm = 0UL;
1528 unsigned int shift, l1_shift, level;
1529 bool writing = !!(dsisr & DSISR_ISSTORE);
1530 bool kvm_ro = false;
1533 if (!gp->l1_gr_to_hr) {
1534 kvmhv_update_ptbl_cache(gp);
1535 if (!gp->l1_gr_to_hr)
1539 /* Convert the nested guest real address into a L1 guest real address */
1541 n_gpa = vcpu->arch.fault_gpa & ~0xF000000000000FFFULL;
1542 if (!(dsisr & DSISR_PRTABLE_FAULT))
1543 n_gpa |= ea & 0xFFF;
1544 ret = kvmhv_translate_addr_nested(vcpu, gp, n_gpa, dsisr, &gpte);
1547 * If the hardware found a translation but we don't now have a usable
1548 * translation in the l1 partition-scoped tree, remove the shadow pte
1549 * and let the guest retry.
1551 if (ret == RESUME_HOST &&
1552 (dsisr & (DSISR_PROTFAULT | DSISR_BADACCESS | DSISR_NOEXEC_OR_G |
1553 DSISR_BAD_COPYPASTE)))
1558 /* Failed to set the reference/change bits */
1559 if (dsisr & DSISR_SET_RC) {
1560 ret = kvmhv_handle_nested_set_rc(vcpu, gp, n_gpa, gpte, dsisr);
1561 if (ret == RESUME_HOST)
1565 dsisr &= ~DSISR_SET_RC;
1566 if (!(dsisr & (DSISR_BAD_FAULT_64S | DSISR_NOHPTE |
1568 return RESUME_GUEST;
1572 * We took an HISI or HDSI while we were running a nested guest which
1573 * means we have no partition scoped translation for that. This means
1574 * we need to insert a pte for the mapping into our shadow_pgtable.
1577 l1_shift = gpte.page_shift;
1578 if (l1_shift < PAGE_SHIFT) {
1579 /* We don't support l1 using a page size smaller than our own */
1580 pr_err("KVM: L1 guest page shift (%d) less than our own (%d)\n",
1581 l1_shift, PAGE_SHIFT);
1585 gfn = gpa >> PAGE_SHIFT;
1587 /* 1. Get the corresponding host memslot */
1589 memslot = gfn_to_memslot(kvm, gfn);
1590 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
1591 if (dsisr & (DSISR_PRTABLE_FAULT | DSISR_BADACCESS)) {
1592 /* unusual error -> reflect to the guest as a DSI */
1593 kvmppc_core_queue_data_storage(vcpu,
1594 kvmppc_get_msr(vcpu) & SRR1_PREFIXED,
1596 return RESUME_GUEST;
1599 /* passthrough of emulated MMIO case */
1600 return kvmppc_hv_emulate_mmio(vcpu, gpa, ea, writing);
1602 if (memslot->flags & KVM_MEM_READONLY) {
1604 /* Give the guest a DSI */
1605 kvmppc_core_queue_data_storage(vcpu,
1606 kvmppc_get_msr(vcpu) & SRR1_PREFIXED,
1607 ea, DSISR_ISSTORE | DSISR_PROTFAULT);
1608 return RESUME_GUEST;
1613 /* 2. Find the host pte for this L1 guest real address */
1615 /* Used to check for invalidations in progress */
1616 mmu_seq = kvm->mmu_invalidate_seq;
1619 /* See if can find translation in our partition scoped tables for L1 */
1621 spin_lock(&kvm->mmu_lock);
1622 pte_p = find_kvm_secondary_pte(kvm, gpa, &shift);
1627 spin_unlock(&kvm->mmu_lock);
1629 if (!pte_present(pte) || (writing && !(pte_val(pte) & _PAGE_WRITE))) {
1630 /* No suitable pte found -> try to insert a mapping */
1631 ret = kvmppc_book3s_instantiate_page(vcpu, gpa, memslot,
1632 writing, kvm_ro, &pte, &level);
1634 return RESUME_GUEST;
1637 shift = kvmppc_radix_level_to_shift(level);
1639 /* Align gfn to the start of the page */
1640 gfn = (gpa & ~((1UL << shift) - 1)) >> PAGE_SHIFT;
1642 /* 3. Compute the pte we need to insert for nest_gpa -> host r_addr */
1644 /* The permissions is the combination of the host and l1 guest ptes */
1645 perm |= gpte.may_read ? 0UL : _PAGE_READ;
1646 perm |= gpte.may_write ? 0UL : _PAGE_WRITE;
1647 perm |= gpte.may_execute ? 0UL : _PAGE_EXEC;
1648 /* Only set accessed/dirty (rc) bits if set in host and l1 guest ptes */
1649 perm |= (gpte.rc & _PAGE_ACCESSED) ? 0UL : _PAGE_ACCESSED;
1650 perm |= ((gpte.rc & _PAGE_DIRTY) && writing) ? 0UL : _PAGE_DIRTY;
1651 pte = __pte(pte_val(pte) & ~perm);
1653 /* What size pte can we insert? */
1654 if (shift > l1_shift) {
1656 unsigned int actual_shift = PAGE_SHIFT;
1657 if (PMD_SHIFT < l1_shift)
1658 actual_shift = PMD_SHIFT;
1659 mask = (1UL << shift) - (1UL << actual_shift);
1660 pte = __pte(pte_val(pte) | (gpa & mask));
1661 shift = actual_shift;
1663 level = kvmppc_radix_shift_to_level(shift);
1664 n_gpa &= ~((1UL << shift) - 1);
1666 /* 4. Insert the pte into our shadow_pgtable */
1668 n_rmap = kzalloc(sizeof(*n_rmap), GFP_KERNEL);
1670 return RESUME_GUEST; /* Let the guest try again */
1671 n_rmap->rmap = (n_gpa & RMAP_NESTED_GPA_MASK) |
1672 (((unsigned long) gp->l1_lpid) << RMAP_NESTED_LPID_SHIFT);
1673 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1674 ret = kvmppc_create_pte(kvm, gp->shadow_pgtable, pte, n_gpa, level,
1675 mmu_seq, gp->shadow_lpid, rmapp, &n_rmap);
1678 ret = RESUME_GUEST; /* Let the guest try again */
1683 kvmhv_invalidate_shadow_pte(vcpu, gp, n_gpa, NULL);
1684 return RESUME_GUEST;
1687 long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu)
1689 struct kvm_nested_guest *gp = vcpu->arch.nested;
1692 mutex_lock(&gp->tlb_lock);
1693 ret = __kvmhv_nested_page_fault(vcpu, gp);
1694 mutex_unlock(&gp->tlb_lock);
1698 int kvmhv_nested_next_lpid(struct kvm *kvm, int lpid)
1702 spin_lock(&kvm->mmu_lock);
1703 if (!idr_get_next(&kvm->arch.kvm_nested_guest_idr, &ret))
1705 spin_unlock(&kvm->mmu_lock);