blob: b2cc3eaec61861f7b2c05a38a444a2798148554e [file] [log] [blame]
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright IBM Corporation, 2018
4 * Authors Suraj Jitindar Singh <sjitindarsingh@gmail.com>
5 * Paul Mackerras <paulus@ozlabs.org>
6 *
7 * Description: KVM functions specific to running nested KVM-HV guests
8 * on Book3S processors (specifically POWER9 and later).
9 */
10
11#include <linux/kernel.h>
12#include <linux/kvm_host.h>
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +110013#include <linux/llist.h>
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +110014
15#include <asm/kvm_ppc.h>
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +110016#include <asm/kvm_book3s.h>
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +110017#include <asm/mmu.h>
18#include <asm/pgtable.h>
19#include <asm/pgalloc.h>
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +110020#include <asm/pte-walk.h>
21#include <asm/reg.h>
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +110022
23static struct patb_entry *pseries_partition_tb;
24
25static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp);
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +110026static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free);
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +110027
Paul Mackerras360cae32018-10-08 16:31:04 +110028void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
29{
30 struct kvmppc_vcore *vc = vcpu->arch.vcore;
31
Jordan Niethe13c7bb32019-09-17 10:46:05 +100032 hr->pcr = vc->pcr | PCR_MASK;
Paul Mackerras360cae32018-10-08 16:31:04 +110033 hr->dpdes = vc->dpdes;
34 hr->hfscr = vcpu->arch.hfscr;
35 hr->tb_offset = vc->tb_offset;
36 hr->dawr0 = vcpu->arch.dawr;
37 hr->dawrx0 = vcpu->arch.dawrx;
38 hr->ciabr = vcpu->arch.ciabr;
39 hr->purr = vcpu->arch.purr;
40 hr->spurr = vcpu->arch.spurr;
41 hr->ic = vcpu->arch.ic;
42 hr->vtb = vc->vtb;
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}
53
Suraj Jitindar Singh10b50222018-10-08 16:31:15 +110054static void byteswap_pt_regs(struct pt_regs *regs)
55{
56 unsigned long *addr = (unsigned long *) regs;
57
58 for (; addr < ((unsigned long *) (regs + 1)); addr++)
59 *addr = swab64(*addr);
60}
61
62static void byteswap_hv_regs(struct hv_guest_state *hr)
63{
64 hr->version = swab64(hr->version);
65 hr->lpid = swab32(hr->lpid);
66 hr->vcpu_token = swab32(hr->vcpu_token);
67 hr->lpcr = swab64(hr->lpcr);
Jordan Niethe13c7bb32019-09-17 10:46:05 +100068 hr->pcr = swab64(hr->pcr) | PCR_MASK;
Suraj Jitindar Singh10b50222018-10-08 16:31:15 +110069 hr->amor = swab64(hr->amor);
70 hr->dpdes = swab64(hr->dpdes);
71 hr->hfscr = swab64(hr->hfscr);
72 hr->tb_offset = swab64(hr->tb_offset);
73 hr->dawr0 = swab64(hr->dawr0);
74 hr->dawrx0 = swab64(hr->dawrx0);
75 hr->ciabr = swab64(hr->ciabr);
76 hr->hdec_expiry = swab64(hr->hdec_expiry);
77 hr->purr = swab64(hr->purr);
78 hr->spurr = swab64(hr->spurr);
79 hr->ic = swab64(hr->ic);
80 hr->vtb = swab64(hr->vtb);
81 hr->hdar = swab64(hr->hdar);
82 hr->hdsisr = swab64(hr->hdsisr);
83 hr->heir = swab64(hr->heir);
84 hr->asdr = swab64(hr->asdr);
85 hr->srr0 = swab64(hr->srr0);
86 hr->srr1 = swab64(hr->srr1);
87 hr->sprg[0] = swab64(hr->sprg[0]);
88 hr->sprg[1] = swab64(hr->sprg[1]);
89 hr->sprg[2] = swab64(hr->sprg[2]);
90 hr->sprg[3] = swab64(hr->sprg[3]);
91 hr->pidr = swab64(hr->pidr);
92 hr->cfar = swab64(hr->cfar);
93 hr->ppr = swab64(hr->ppr);
94}
95
Paul Mackerras360cae32018-10-08 16:31:04 +110096static void save_hv_return_state(struct kvm_vcpu *vcpu, int trap,
97 struct hv_guest_state *hr)
98{
99 struct kvmppc_vcore *vc = vcpu->arch.vcore;
100
101 hr->dpdes = vc->dpdes;
102 hr->hfscr = vcpu->arch.hfscr;
103 hr->purr = vcpu->arch.purr;
104 hr->spurr = vcpu->arch.spurr;
105 hr->ic = vcpu->arch.ic;
106 hr->vtb = vc->vtb;
107 hr->srr0 = vcpu->arch.shregs.srr0;
108 hr->srr1 = vcpu->arch.shregs.srr1;
109 hr->sprg[0] = vcpu->arch.shregs.sprg0;
110 hr->sprg[1] = vcpu->arch.shregs.sprg1;
111 hr->sprg[2] = vcpu->arch.shregs.sprg2;
112 hr->sprg[3] = vcpu->arch.shregs.sprg3;
113 hr->pidr = vcpu->arch.pid;
114 hr->cfar = vcpu->arch.cfar;
115 hr->ppr = vcpu->arch.ppr;
116 switch (trap) {
117 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
118 hr->hdar = vcpu->arch.fault_dar;
119 hr->hdsisr = vcpu->arch.fault_dsisr;
120 hr->asdr = vcpu->arch.fault_gpa;
121 break;
122 case BOOK3S_INTERRUPT_H_INST_STORAGE:
123 hr->asdr = vcpu->arch.fault_gpa;
124 break;
125 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
126 hr->heir = vcpu->arch.emul_inst;
127 break;
128 }
129}
130
Suraj Jitindar Singh73937de2018-10-08 16:31:14 +1100131static void sanitise_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
132{
133 /*
134 * Don't let L1 enable features for L2 which we've disabled for L1,
135 * but preserve the interrupt cause field.
136 */
137 hr->hfscr &= (HFSCR_INTR_CAUSE | vcpu->arch.hfscr);
138
139 /* Don't let data address watchpoint match in hypervisor state */
140 hr->dawrx0 &= ~DAWRX_HYP;
141
142 /* Don't let completed instruction address breakpt match in HV state */
143 if ((hr->ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
144 hr->ciabr &= ~CIABR_PRIV;
145}
146
Paul Mackerras360cae32018-10-08 16:31:04 +1100147static void restore_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
148{
149 struct kvmppc_vcore *vc = vcpu->arch.vcore;
150
Jordan Niethe13c7bb32019-09-17 10:46:05 +1000151 vc->pcr = hr->pcr | PCR_MASK;
Paul Mackerras360cae32018-10-08 16:31:04 +1100152 vc->dpdes = hr->dpdes;
153 vcpu->arch.hfscr = hr->hfscr;
154 vcpu->arch.dawr = hr->dawr0;
155 vcpu->arch.dawrx = hr->dawrx0;
156 vcpu->arch.ciabr = hr->ciabr;
157 vcpu->arch.purr = hr->purr;
158 vcpu->arch.spurr = hr->spurr;
159 vcpu->arch.ic = hr->ic;
160 vc->vtb = hr->vtb;
161 vcpu->arch.shregs.srr0 = hr->srr0;
162 vcpu->arch.shregs.srr1 = hr->srr1;
163 vcpu->arch.shregs.sprg0 = hr->sprg[0];
164 vcpu->arch.shregs.sprg1 = hr->sprg[1];
165 vcpu->arch.shregs.sprg2 = hr->sprg[2];
166 vcpu->arch.shregs.sprg3 = hr->sprg[3];
167 vcpu->arch.pid = hr->pidr;
168 vcpu->arch.cfar = hr->cfar;
169 vcpu->arch.ppr = hr->ppr;
170}
171
172void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,
173 struct hv_guest_state *hr)
174{
175 struct kvmppc_vcore *vc = vcpu->arch.vcore;
176
177 vc->dpdes = hr->dpdes;
178 vcpu->arch.hfscr = hr->hfscr;
179 vcpu->arch.purr = hr->purr;
180 vcpu->arch.spurr = hr->spurr;
181 vcpu->arch.ic = hr->ic;
182 vc->vtb = hr->vtb;
183 vcpu->arch.fault_dar = hr->hdar;
184 vcpu->arch.fault_dsisr = hr->hdsisr;
185 vcpu->arch.fault_gpa = hr->asdr;
186 vcpu->arch.emul_inst = hr->heir;
187 vcpu->arch.shregs.srr0 = hr->srr0;
188 vcpu->arch.shregs.srr1 = hr->srr1;
189 vcpu->arch.shregs.sprg0 = hr->sprg[0];
190 vcpu->arch.shregs.sprg1 = hr->sprg[1];
191 vcpu->arch.shregs.sprg2 = hr->sprg[2];
192 vcpu->arch.shregs.sprg3 = hr->sprg[3];
193 vcpu->arch.pid = hr->pidr;
194 vcpu->arch.cfar = hr->cfar;
195 vcpu->arch.ppr = hr->ppr;
196}
197
Suraj Jitindar Singh873db2c2018-12-14 16:29:08 +1100198static void kvmhv_nested_mmio_needed(struct kvm_vcpu *vcpu, u64 regs_ptr)
199{
200 /* No need to reflect the page fault to L1, we've handled it */
201 vcpu->arch.trap = 0;
202
203 /*
204 * Since the L2 gprs have already been written back into L1 memory when
205 * we complete the mmio, store the L1 memory location of the L2 gpr
206 * being loaded into by the mmio so that the loaded value can be
207 * written there in kvmppc_complete_mmio_load()
208 */
209 if (((vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) == KVM_MMIO_REG_GPR)
210 && (vcpu->mmio_is_write == 0)) {
211 vcpu->arch.nested_io_gpr = (gpa_t) regs_ptr +
212 offsetof(struct pt_regs,
213 gpr[vcpu->arch.io_gpr]);
214 vcpu->arch.io_gpr = KVM_MMIO_REG_NESTED_GPR;
215 }
216}
217
Paul Mackerras360cae32018-10-08 16:31:04 +1100218long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)
219{
220 long int err, r;
221 struct kvm_nested_guest *l2;
222 struct pt_regs l2_regs, saved_l1_regs;
223 struct hv_guest_state l2_hv, saved_l1_hv;
224 struct kvmppc_vcore *vc = vcpu->arch.vcore;
225 u64 hv_ptr, regs_ptr;
226 u64 hdec_exp;
227 s64 delta_purr, delta_spurr, delta_ic, delta_vtb;
228 u64 mask;
229 unsigned long lpcr;
230
231 if (vcpu->kvm->arch.l1_ptcr == 0)
232 return H_NOT_AVAILABLE;
233
234 /* copy parameters in */
235 hv_ptr = kvmppc_get_gpr(vcpu, 4);
236 err = kvm_vcpu_read_guest(vcpu, hv_ptr, &l2_hv,
237 sizeof(struct hv_guest_state));
238 if (err)
239 return H_PARAMETER;
Suraj Jitindar Singh10b50222018-10-08 16:31:15 +1100240 if (kvmppc_need_byteswap(vcpu))
241 byteswap_hv_regs(&l2_hv);
Paul Mackerras360cae32018-10-08 16:31:04 +1100242 if (l2_hv.version != HV_GUEST_STATE_VERSION)
243 return H_P2;
244
245 regs_ptr = kvmppc_get_gpr(vcpu, 5);
246 err = kvm_vcpu_read_guest(vcpu, regs_ptr, &l2_regs,
247 sizeof(struct pt_regs));
248 if (err)
249 return H_PARAMETER;
Suraj Jitindar Singh10b50222018-10-08 16:31:15 +1100250 if (kvmppc_need_byteswap(vcpu))
251 byteswap_pt_regs(&l2_regs);
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +1100252 if (l2_hv.vcpu_token >= NR_CPUS)
253 return H_PARAMETER;
254
Paul Mackerras360cae32018-10-08 16:31:04 +1100255 /* translate lpid */
256 l2 = kvmhv_get_nested(vcpu->kvm, l2_hv.lpid, true);
257 if (!l2)
258 return H_PARAMETER;
259 if (!l2->l1_gr_to_hr) {
260 mutex_lock(&l2->tlb_lock);
261 kvmhv_update_ptbl_cache(l2);
262 mutex_unlock(&l2->tlb_lock);
263 }
264
265 /* save l1 values of things */
266 vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
267 saved_l1_regs = vcpu->arch.regs;
268 kvmhv_save_hv_regs(vcpu, &saved_l1_hv);
269
270 /* convert TB values/offsets to host (L0) values */
271 hdec_exp = l2_hv.hdec_expiry - vc->tb_offset;
272 vc->tb_offset += l2_hv.tb_offset;
273
274 /* set L1 state to L2 state */
275 vcpu->arch.nested = l2;
276 vcpu->arch.nested_vcpu_id = l2_hv.vcpu_token;
277 vcpu->arch.regs = l2_regs;
278 vcpu->arch.shregs.msr = vcpu->arch.regs.msr;
279 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD |
280 LPCR_LPES | LPCR_MER;
281 lpcr = (vc->lpcr & ~mask) | (l2_hv.lpcr & mask);
Suraj Jitindar Singh73937de2018-10-08 16:31:14 +1100282 sanitise_hv_regs(vcpu, &l2_hv);
Paul Mackerras360cae32018-10-08 16:31:04 +1100283 restore_hv_regs(vcpu, &l2_hv);
284
285 vcpu->arch.ret = RESUME_GUEST;
286 vcpu->arch.trap = 0;
287 do {
288 if (mftb() >= hdec_exp) {
289 vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER;
290 r = RESUME_HOST;
291 break;
292 }
293 r = kvmhv_run_single_vcpu(vcpu->arch.kvm_run, vcpu, hdec_exp,
294 lpcr);
295 } while (is_kvmppc_resume_guest(r));
296
297 /* save L2 state for return */
298 l2_regs = vcpu->arch.regs;
299 l2_regs.msr = vcpu->arch.shregs.msr;
300 delta_purr = vcpu->arch.purr - l2_hv.purr;
301 delta_spurr = vcpu->arch.spurr - l2_hv.spurr;
302 delta_ic = vcpu->arch.ic - l2_hv.ic;
303 delta_vtb = vc->vtb - l2_hv.vtb;
304 save_hv_return_state(vcpu, vcpu->arch.trap, &l2_hv);
305
306 /* restore L1 state */
307 vcpu->arch.nested = NULL;
308 vcpu->arch.regs = saved_l1_regs;
309 vcpu->arch.shregs.msr = saved_l1_regs.msr & ~MSR_TS_MASK;
310 /* set L1 MSR TS field according to L2 transaction state */
311 if (l2_regs.msr & MSR_TS_MASK)
312 vcpu->arch.shregs.msr |= MSR_TS_S;
313 vc->tb_offset = saved_l1_hv.tb_offset;
314 restore_hv_regs(vcpu, &saved_l1_hv);
315 vcpu->arch.purr += delta_purr;
316 vcpu->arch.spurr += delta_spurr;
317 vcpu->arch.ic += delta_ic;
318 vc->vtb += delta_vtb;
319
320 kvmhv_put_nested(l2);
321
322 /* copy l2_hv_state and regs back to guest */
Suraj Jitindar Singh10b50222018-10-08 16:31:15 +1100323 if (kvmppc_need_byteswap(vcpu)) {
324 byteswap_hv_regs(&l2_hv);
325 byteswap_pt_regs(&l2_regs);
326 }
Paul Mackerras360cae32018-10-08 16:31:04 +1100327 err = kvm_vcpu_write_guest(vcpu, hv_ptr, &l2_hv,
328 sizeof(struct hv_guest_state));
329 if (err)
330 return H_AUTHORITY;
331 err = kvm_vcpu_write_guest(vcpu, regs_ptr, &l2_regs,
332 sizeof(struct pt_regs));
333 if (err)
334 return H_AUTHORITY;
335
336 if (r == -EINTR)
337 return H_INTERRUPT;
338
Suraj Jitindar Singh873db2c2018-12-14 16:29:08 +1100339 if (vcpu->mmio_needed) {
340 kvmhv_nested_mmio_needed(vcpu, regs_ptr);
341 return H_TOO_HARD;
342 }
343
Paul Mackerras360cae32018-10-08 16:31:04 +1100344 return vcpu->arch.trap;
345}
346
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100347long kvmhv_nested_init(void)
348{
349 long int ptb_order;
350 unsigned long ptcr;
351 long rc;
352
353 if (!kvmhv_on_pseries())
354 return 0;
355 if (!radix_enabled())
356 return -ENODEV;
357
358 /* find log base 2 of KVMPPC_NR_LPIDS, rounding up */
359 ptb_order = __ilog2(KVMPPC_NR_LPIDS - 1) + 1;
360 if (ptb_order < 8)
361 ptb_order = 8;
362 pseries_partition_tb = kmalloc(sizeof(struct patb_entry) << ptb_order,
363 GFP_KERNEL);
364 if (!pseries_partition_tb) {
365 pr_err("kvm-hv: failed to allocated nested partition table\n");
366 return -ENOMEM;
367 }
368
369 ptcr = __pa(pseries_partition_tb) | (ptb_order - 8);
370 rc = plpar_hcall_norets(H_SET_PARTITION_TABLE, ptcr);
371 if (rc != H_SUCCESS) {
372 pr_err("kvm-hv: Parent hypervisor does not support nesting (rc=%ld)\n",
373 rc);
374 kfree(pseries_partition_tb);
375 pseries_partition_tb = NULL;
376 return -ENODEV;
377 }
378
379 return 0;
380}
381
382void kvmhv_nested_exit(void)
383{
384 /*
385 * N.B. the kvmhv_on_pseries() test is there because it enables
386 * the compiler to remove the call to plpar_hcall_norets()
387 * when CONFIG_PPC_PSERIES=n.
388 */
389 if (kvmhv_on_pseries() && pseries_partition_tb) {
390 plpar_hcall_norets(H_SET_PARTITION_TABLE, 0);
391 kfree(pseries_partition_tb);
392 pseries_partition_tb = NULL;
393 }
394}
395
Paul Mackerras690ed4c2018-10-08 16:31:10 +1100396static void kvmhv_flush_lpid(unsigned int lpid)
397{
398 long rc;
399
400 if (!kvmhv_on_pseries()) {
Nicholas Piggin99161de2019-09-03 01:29:27 +1000401 radix__flush_all_lpid(lpid);
Paul Mackerras690ed4c2018-10-08 16:31:10 +1100402 return;
403 }
404
405 rc = plpar_hcall_norets(H_TLB_INVALIDATE, H_TLBIE_P1_ENC(2, 0, 1),
406 lpid, TLBIEL_INVAL_SET_LPID);
407 if (rc)
408 pr_err("KVM: TLB LPID invalidation hcall failed, rc=%ld\n", rc);
409}
410
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100411void kvmhv_set_ptbl_entry(unsigned int lpid, u64 dw0, u64 dw1)
412{
Paul Mackerras690ed4c2018-10-08 16:31:10 +1100413 if (!kvmhv_on_pseries()) {
Nicholas Pigginfd13dae2019-09-03 01:29:28 +1000414 mmu_partition_table_set_entry(lpid, dw0, dw1, true);
Paul Mackerras690ed4c2018-10-08 16:31:10 +1100415 return;
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100416 }
Paul Mackerras690ed4c2018-10-08 16:31:10 +1100417
418 pseries_partition_tb[lpid].patb0 = cpu_to_be64(dw0);
419 pseries_partition_tb[lpid].patb1 = cpu_to_be64(dw1);
420 /* L0 will do the necessary barriers */
421 kvmhv_flush_lpid(lpid);
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100422}
423
424static void kvmhv_set_nested_ptbl(struct kvm_nested_guest *gp)
425{
426 unsigned long dw0;
427
428 dw0 = PATB_HR | radix__get_tree_size() |
429 __pa(gp->shadow_pgtable) | RADIX_PGD_INDEX_SIZE;
430 kvmhv_set_ptbl_entry(gp->shadow_lpid, dw0, gp->process_table);
431}
432
433void kvmhv_vm_nested_init(struct kvm *kvm)
434{
435 kvm->arch.max_nested_lpid = -1;
436}
437
438/*
439 * Handle the H_SET_PARTITION_TABLE hcall.
440 * r4 = guest real address of partition table + log_2(size) - 12
441 * (formatted as for the PTCR).
442 */
443long kvmhv_set_partition_table(struct kvm_vcpu *vcpu)
444{
445 struct kvm *kvm = vcpu->kvm;
446 unsigned long ptcr = kvmppc_get_gpr(vcpu, 4);
447 int srcu_idx;
448 long ret = H_SUCCESS;
449
450 srcu_idx = srcu_read_lock(&kvm->srcu);
451 /*
452 * Limit the partition table to 4096 entries (because that's what
453 * hardware supports), and check the base address.
454 */
455 if ((ptcr & PRTS_MASK) > 12 - 8 ||
456 !kvm_is_visible_gfn(vcpu->kvm, (ptcr & PRTB_MASK) >> PAGE_SHIFT))
457 ret = H_PARAMETER;
458 srcu_read_unlock(&kvm->srcu, srcu_idx);
459 if (ret == H_SUCCESS)
460 kvm->arch.l1_ptcr = ptcr;
461 return ret;
462}
463
464/*
Suraj Jitindar Singh6ff887b2018-12-14 16:29:09 +1100465 * Handle the H_COPY_TOFROM_GUEST hcall.
466 * r4 = L1 lpid of nested guest
467 * r5 = pid
468 * r6 = eaddr to access
469 * r7 = to buffer (L1 gpa)
470 * r8 = from buffer (L1 gpa)
471 * r9 = n bytes to copy
472 */
473long kvmhv_copy_tofrom_guest_nested(struct kvm_vcpu *vcpu)
474{
475 struct kvm_nested_guest *gp;
476 int l1_lpid = kvmppc_get_gpr(vcpu, 4);
477 int pid = kvmppc_get_gpr(vcpu, 5);
478 gva_t eaddr = kvmppc_get_gpr(vcpu, 6);
479 gpa_t gp_to = (gpa_t) kvmppc_get_gpr(vcpu, 7);
480 gpa_t gp_from = (gpa_t) kvmppc_get_gpr(vcpu, 8);
481 void *buf;
482 unsigned long n = kvmppc_get_gpr(vcpu, 9);
483 bool is_load = !!gp_to;
484 long rc;
485
486 if (gp_to && gp_from) /* One must be NULL to determine the direction */
487 return H_PARAMETER;
488
489 if (eaddr & (0xFFFUL << 52))
490 return H_PARAMETER;
491
492 buf = kzalloc(n, GFP_KERNEL);
493 if (!buf)
494 return H_NO_MEM;
495
496 gp = kvmhv_get_nested(vcpu->kvm, l1_lpid, false);
497 if (!gp) {
498 rc = H_PARAMETER;
499 goto out_free;
500 }
501
502 mutex_lock(&gp->tlb_lock);
503
504 if (is_load) {
505 /* Load from the nested guest into our buffer */
506 rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
507 eaddr, buf, NULL, n);
508 if (rc)
509 goto not_found;
510
511 /* Write what was loaded into our buffer back to the L1 guest */
512 rc = kvm_vcpu_write_guest(vcpu, gp_to, buf, n);
513 if (rc)
514 goto not_found;
515 } else {
516 /* Load the data to be stored from the L1 guest into our buf */
517 rc = kvm_vcpu_read_guest(vcpu, gp_from, buf, n);
518 if (rc)
519 goto not_found;
520
521 /* Store from our buffer into the nested guest */
522 rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
523 eaddr, NULL, buf, n);
524 if (rc)
525 goto not_found;
526 }
527
528out_unlock:
529 mutex_unlock(&gp->tlb_lock);
530 kvmhv_put_nested(gp);
531out_free:
532 kfree(buf);
533 return rc;
534not_found:
535 rc = H_NOT_FOUND;
536 goto out_unlock;
537}
538
539/*
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100540 * Reload the partition table entry for a guest.
541 * Caller must hold gp->tlb_lock.
542 */
543static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp)
544{
545 int ret;
546 struct patb_entry ptbl_entry;
547 unsigned long ptbl_addr;
548 struct kvm *kvm = gp->l1_host;
549
550 ret = -EFAULT;
551 ptbl_addr = (kvm->arch.l1_ptcr & PRTB_MASK) + (gp->l1_lpid << 4);
552 if (gp->l1_lpid < (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 8)))
553 ret = kvm_read_guest(kvm, ptbl_addr,
554 &ptbl_entry, sizeof(ptbl_entry));
555 if (ret) {
556 gp->l1_gr_to_hr = 0;
557 gp->process_table = 0;
558 } else {
559 gp->l1_gr_to_hr = be64_to_cpu(ptbl_entry.patb0);
560 gp->process_table = be64_to_cpu(ptbl_entry.patb1);
561 }
562 kvmhv_set_nested_ptbl(gp);
563}
564
565struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid)
566{
567 struct kvm_nested_guest *gp;
568 long shadow_lpid;
569
570 gp = kzalloc(sizeof(*gp), GFP_KERNEL);
571 if (!gp)
572 return NULL;
573 gp->l1_host = kvm;
574 gp->l1_lpid = lpid;
575 mutex_init(&gp->tlb_lock);
576 gp->shadow_pgtable = pgd_alloc(kvm->mm);
577 if (!gp->shadow_pgtable)
578 goto out_free;
579 shadow_lpid = kvmppc_alloc_lpid();
580 if (shadow_lpid < 0)
581 goto out_free2;
582 gp->shadow_lpid = shadow_lpid;
Suraj Jitindar Singhd232afe2018-12-14 16:29:04 +1100583 gp->radix = 1;
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100584
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +1100585 memset(gp->prev_cpu, -1, sizeof(gp->prev_cpu));
586
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100587 return gp;
588
589 out_free2:
590 pgd_free(kvm->mm, gp->shadow_pgtable);
591 out_free:
592 kfree(gp);
593 return NULL;
594}
595
596/*
597 * Free up any resources allocated for a nested guest.
598 */
599static void kvmhv_release_nested(struct kvm_nested_guest *gp)
600{
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +1100601 struct kvm *kvm = gp->l1_host;
602
603 if (gp->shadow_pgtable) {
604 /*
605 * No vcpu is using this struct and no call to
606 * kvmhv_get_nested can find this struct,
607 * so we don't need to hold kvm->mmu_lock.
608 */
609 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
610 gp->shadow_lpid);
611 pgd_free(kvm->mm, gp->shadow_pgtable);
612 }
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100613 kvmhv_set_ptbl_entry(gp->shadow_lpid, 0, 0);
614 kvmppc_free_lpid(gp->shadow_lpid);
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100615 kfree(gp);
616}
617
618static void kvmhv_remove_nested(struct kvm_nested_guest *gp)
619{
620 struct kvm *kvm = gp->l1_host;
621 int lpid = gp->l1_lpid;
622 long ref;
623
624 spin_lock(&kvm->mmu_lock);
625 if (gp == kvm->arch.nested_guests[lpid]) {
626 kvm->arch.nested_guests[lpid] = NULL;
627 if (lpid == kvm->arch.max_nested_lpid) {
628 while (--lpid >= 0 && !kvm->arch.nested_guests[lpid])
629 ;
630 kvm->arch.max_nested_lpid = lpid;
631 }
632 --gp->refcnt;
633 }
634 ref = gp->refcnt;
635 spin_unlock(&kvm->mmu_lock);
636 if (ref == 0)
637 kvmhv_release_nested(gp);
638}
639
640/*
641 * Free up all nested resources allocated for this guest.
642 * This is called with no vcpus of the guest running, when
643 * switching the guest to HPT mode or when destroying the
644 * guest.
645 */
646void kvmhv_release_all_nested(struct kvm *kvm)
647{
648 int i;
649 struct kvm_nested_guest *gp;
650 struct kvm_nested_guest *freelist = NULL;
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +1100651 struct kvm_memory_slot *memslot;
652 int srcu_idx;
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100653
654 spin_lock(&kvm->mmu_lock);
655 for (i = 0; i <= kvm->arch.max_nested_lpid; i++) {
656 gp = kvm->arch.nested_guests[i];
657 if (!gp)
658 continue;
659 kvm->arch.nested_guests[i] = NULL;
660 if (--gp->refcnt == 0) {
661 gp->next = freelist;
662 freelist = gp;
663 }
664 }
665 kvm->arch.max_nested_lpid = -1;
666 spin_unlock(&kvm->mmu_lock);
667 while ((gp = freelist) != NULL) {
668 freelist = gp->next;
669 kvmhv_release_nested(gp);
670 }
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +1100671
672 srcu_idx = srcu_read_lock(&kvm->srcu);
673 kvm_for_each_memslot(memslot, kvm_memslots(kvm))
674 kvmhv_free_memslot_nest_rmap(memslot);
675 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100676}
677
678/* caller must hold gp->tlb_lock */
Suraj Jitindar Singhe3b6b462018-10-08 16:31:09 +1100679static void kvmhv_flush_nested(struct kvm_nested_guest *gp)
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100680{
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +1100681 struct kvm *kvm = gp->l1_host;
682
683 spin_lock(&kvm->mmu_lock);
684 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable, gp->shadow_lpid);
685 spin_unlock(&kvm->mmu_lock);
Paul Mackerras690ed4c2018-10-08 16:31:10 +1100686 kvmhv_flush_lpid(gp->shadow_lpid);
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +1100687 kvmhv_update_ptbl_cache(gp);
688 if (gp->l1_gr_to_hr == 0)
689 kvmhv_remove_nested(gp);
690}
691
692struct kvm_nested_guest *kvmhv_get_nested(struct kvm *kvm, int l1_lpid,
693 bool create)
694{
695 struct kvm_nested_guest *gp, *newgp;
696
697 if (l1_lpid >= KVM_MAX_NESTED_GUESTS ||
698 l1_lpid >= (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 12 - 4)))
699 return NULL;
700
701 spin_lock(&kvm->mmu_lock);
702 gp = kvm->arch.nested_guests[l1_lpid];
703 if (gp)
704 ++gp->refcnt;
705 spin_unlock(&kvm->mmu_lock);
706
707 if (gp || !create)
708 return gp;
709
710 newgp = kvmhv_alloc_nested(kvm, l1_lpid);
711 if (!newgp)
712 return NULL;
713 spin_lock(&kvm->mmu_lock);
714 if (kvm->arch.nested_guests[l1_lpid]) {
715 /* someone else beat us to it */
716 gp = kvm->arch.nested_guests[l1_lpid];
717 } else {
718 kvm->arch.nested_guests[l1_lpid] = newgp;
719 ++newgp->refcnt;
720 gp = newgp;
721 newgp = NULL;
722 if (l1_lpid > kvm->arch.max_nested_lpid)
723 kvm->arch.max_nested_lpid = l1_lpid;
724 }
725 ++gp->refcnt;
726 spin_unlock(&kvm->mmu_lock);
727
728 if (newgp)
729 kvmhv_release_nested(newgp);
730
731 return gp;
732}
733
734void kvmhv_put_nested(struct kvm_nested_guest *gp)
735{
736 struct kvm *kvm = gp->l1_host;
737 long ref;
738
739 spin_lock(&kvm->mmu_lock);
740 ref = --gp->refcnt;
741 spin_unlock(&kvm->mmu_lock);
742 if (ref == 0)
743 kvmhv_release_nested(gp);
744}
Paul Mackerras360cae32018-10-08 16:31:04 +1100745
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +1100746static struct kvm_nested_guest *kvmhv_find_nested(struct kvm *kvm, int lpid)
747{
748 if (lpid > kvm->arch.max_nested_lpid)
749 return NULL;
750 return kvm->arch.nested_guests[lpid];
751}
752
Aneesh Kumar K.Vdc891842020-05-05 12:47:17 +0530753static pte_t *find_kvm_nested_guest_pte(struct kvm *kvm, unsigned long lpid,
754 unsigned long ea, unsigned *hshift)
755{
756 struct kvm_nested_guest *gp;
757 pte_t *pte;
758
759 gp = kvmhv_find_nested(kvm, lpid);
760 if (!gp)
761 return NULL;
762
763 VM_WARN(!spin_is_locked(&kvm->mmu_lock),
764 "%s called with kvm mmu_lock not held \n", __func__);
765 pte = __find_linux_pte(gp->shadow_pgtable, ea, NULL, hshift);
766
767 return pte;
768}
769
770
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +1100771static inline bool kvmhv_n_rmap_is_equal(u64 rmap_1, u64 rmap_2)
772{
773 return !((rmap_1 ^ rmap_2) & (RMAP_NESTED_LPID_MASK |
774 RMAP_NESTED_GPA_MASK));
775}
776
777void kvmhv_insert_nest_rmap(struct kvm *kvm, unsigned long *rmapp,
778 struct rmap_nested **n_rmap)
779{
780 struct llist_node *entry = ((struct llist_head *) rmapp)->first;
781 struct rmap_nested *cursor;
782 u64 rmap, new_rmap = (*n_rmap)->rmap;
783
784 /* Are there any existing entries? */
785 if (!(*rmapp)) {
786 /* No -> use the rmap as a single entry */
787 *rmapp = new_rmap | RMAP_NESTED_IS_SINGLE_ENTRY;
788 return;
789 }
790
791 /* Do any entries match what we're trying to insert? */
792 for_each_nest_rmap_safe(cursor, entry, &rmap) {
793 if (kvmhv_n_rmap_is_equal(rmap, new_rmap))
794 return;
795 }
796
797 /* Do we need to create a list or just add the new entry? */
798 rmap = *rmapp;
799 if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
800 *rmapp = 0UL;
801 llist_add(&((*n_rmap)->list), (struct llist_head *) rmapp);
802 if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
803 (*n_rmap)->list.next = (struct llist_node *) rmap;
804
805 /* Set NULL so not freed by caller */
806 *n_rmap = NULL;
807}
808
Suraj Jitindar Singh90165d32018-12-21 14:28:42 +1100809static void kvmhv_update_nest_rmap_rc(struct kvm *kvm, u64 n_rmap,
810 unsigned long clr, unsigned long set,
811 unsigned long hpa, unsigned long mask)
812{
Suraj Jitindar Singh90165d32018-12-21 14:28:42 +1100813 unsigned long gpa;
814 unsigned int shift, lpid;
815 pte_t *ptep;
816
817 gpa = n_rmap & RMAP_NESTED_GPA_MASK;
818 lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
Suraj Jitindar Singh90165d32018-12-21 14:28:42 +1100819
820 /* Find the pte */
Aneesh Kumar K.Vdc891842020-05-05 12:47:17 +0530821 ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
Suraj Jitindar Singh90165d32018-12-21 14:28:42 +1100822 /*
823 * If the pte is present and the pfn is still the same, update the pte.
824 * If the pfn has changed then this is a stale rmap entry, the nested
825 * gpa actually points somewhere else now, and there is nothing to do.
826 * XXX A future optimisation would be to remove the rmap entry here.
827 */
828 if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa)) {
829 __radix_pte_update(ptep, clr, set);
830 kvmppc_radix_tlbie_page(kvm, gpa, shift, lpid);
831 }
832}
833
834/*
835 * For a given list of rmap entries, update the rc bits in all ptes in shadow
836 * page tables for nested guests which are referenced by the rmap list.
837 */
838void kvmhv_update_nest_rmap_rc_list(struct kvm *kvm, unsigned long *rmapp,
839 unsigned long clr, unsigned long set,
840 unsigned long hpa, unsigned long nbytes)
841{
842 struct llist_node *entry = ((struct llist_head *) rmapp)->first;
843 struct rmap_nested *cursor;
844 unsigned long rmap, mask;
845
846 if ((clr | set) & ~(_PAGE_DIRTY | _PAGE_ACCESSED))
847 return;
848
849 mask = PTE_RPN_MASK & ~(nbytes - 1);
850 hpa &= mask;
851
852 for_each_nest_rmap_safe(cursor, entry, &rmap)
853 kvmhv_update_nest_rmap_rc(kvm, rmap, clr, set, hpa, mask);
854}
855
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +1100856static void kvmhv_remove_nest_rmap(struct kvm *kvm, u64 n_rmap,
857 unsigned long hpa, unsigned long mask)
858{
859 struct kvm_nested_guest *gp;
860 unsigned long gpa;
861 unsigned int shift, lpid;
862 pte_t *ptep;
863
864 gpa = n_rmap & RMAP_NESTED_GPA_MASK;
865 lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
866 gp = kvmhv_find_nested(kvm, lpid);
867 if (!gp)
868 return;
869
870 /* Find and invalidate the pte */
Aneesh Kumar K.Vdc891842020-05-05 12:47:17 +0530871 ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +1100872 /* Don't spuriously invalidate ptes if the pfn has changed */
873 if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa))
874 kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
875}
876
877static void kvmhv_remove_nest_rmap_list(struct kvm *kvm, unsigned long *rmapp,
878 unsigned long hpa, unsigned long mask)
879{
880 struct llist_node *entry = llist_del_all((struct llist_head *) rmapp);
881 struct rmap_nested *cursor;
882 unsigned long rmap;
883
884 for_each_nest_rmap_safe(cursor, entry, &rmap) {
885 kvmhv_remove_nest_rmap(kvm, rmap, hpa, mask);
886 kfree(cursor);
887 }
888}
889
890/* called with kvm->mmu_lock held */
891void kvmhv_remove_nest_rmap_range(struct kvm *kvm,
Paul Mackerrasc43c3a82018-12-12 15:16:48 +1100892 const struct kvm_memory_slot *memslot,
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +1100893 unsigned long gpa, unsigned long hpa,
894 unsigned long nbytes)
895{
896 unsigned long gfn, end_gfn;
897 unsigned long addr_mask;
898
899 if (!memslot)
900 return;
901 gfn = (gpa >> PAGE_SHIFT) - memslot->base_gfn;
902 end_gfn = gfn + (nbytes >> PAGE_SHIFT);
903
904 addr_mask = PTE_RPN_MASK & ~(nbytes - 1);
905 hpa &= addr_mask;
906
907 for (; gfn < end_gfn; gfn++) {
908 unsigned long *rmap = &memslot->arch.rmap[gfn];
909 kvmhv_remove_nest_rmap_list(kvm, rmap, hpa, addr_mask);
910 }
911}
912
913static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free)
914{
915 unsigned long page;
916
917 for (page = 0; page < free->npages; page++) {
918 unsigned long rmap, *rmapp = &free->arch.rmap[page];
919 struct rmap_nested *cursor;
920 struct llist_node *entry;
921
922 entry = llist_del_all((struct llist_head *) rmapp);
923 for_each_nest_rmap_safe(cursor, entry, &rmap)
924 kfree(cursor);
925 }
926}
927
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +1100928static bool kvmhv_invalidate_shadow_pte(struct kvm_vcpu *vcpu,
929 struct kvm_nested_guest *gp,
930 long gpa, int *shift_ret)
Paul Mackerras360cae32018-10-08 16:31:04 +1100931{
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +1100932 struct kvm *kvm = vcpu->kvm;
933 bool ret = false;
934 pte_t *ptep;
935 int shift;
936
937 spin_lock(&kvm->mmu_lock);
Aneesh Kumar K.Vdc891842020-05-05 12:47:17 +0530938 ptep = find_kvm_nested_guest_pte(kvm, gp->l1_lpid, gpa, &shift);
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +1100939 if (!shift)
940 shift = PAGE_SHIFT;
941 if (ptep && pte_present(*ptep)) {
942 kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
943 ret = true;
944 }
945 spin_unlock(&kvm->mmu_lock);
946
947 if (shift_ret)
948 *shift_ret = shift;
949 return ret;
950}
951
Suraj Jitindar Singhe3b6b462018-10-08 16:31:09 +1100952static inline int get_ric(unsigned int instr)
953{
954 return (instr >> 18) & 0x3;
955}
956
957static inline int get_prs(unsigned int instr)
958{
959 return (instr >> 17) & 0x1;
960}
961
962static inline int get_r(unsigned int instr)
963{
964 return (instr >> 16) & 0x1;
965}
966
967static inline int get_lpid(unsigned long r_val)
968{
969 return r_val & 0xffffffff;
970}
971
972static inline int get_is(unsigned long r_val)
973{
974 return (r_val >> 10) & 0x3;
975}
976
977static inline int get_ap(unsigned long r_val)
978{
979 return (r_val >> 5) & 0x7;
980}
981
982static inline long get_epn(unsigned long r_val)
983{
984 return r_val >> 12;
985}
986
987static int kvmhv_emulate_tlbie_tlb_addr(struct kvm_vcpu *vcpu, int lpid,
988 int ap, long epn)
989{
990 struct kvm *kvm = vcpu->kvm;
991 struct kvm_nested_guest *gp;
992 long npages;
993 int shift, shadow_shift;
994 unsigned long addr;
995
996 shift = ap_to_shift(ap);
997 addr = epn << 12;
998 if (shift < 0)
999 /* Invalid ap encoding */
1000 return -EINVAL;
1001
1002 addr &= ~((1UL << shift) - 1);
1003 npages = 1UL << (shift - PAGE_SHIFT);
1004
1005 gp = kvmhv_get_nested(kvm, lpid, false);
1006 if (!gp) /* No such guest -> nothing to do */
1007 return 0;
1008 mutex_lock(&gp->tlb_lock);
1009
1010 /* There may be more than one host page backing this single guest pte */
1011 do {
1012 kvmhv_invalidate_shadow_pte(vcpu, gp, addr, &shadow_shift);
1013
1014 npages -= 1UL << (shadow_shift - PAGE_SHIFT);
1015 addr += 1UL << shadow_shift;
1016 } while (npages > 0);
1017
1018 mutex_unlock(&gp->tlb_lock);
1019 kvmhv_put_nested(gp);
1020 return 0;
1021}
1022
1023static void kvmhv_emulate_tlbie_lpid(struct kvm_vcpu *vcpu,
1024 struct kvm_nested_guest *gp, int ric)
1025{
1026 struct kvm *kvm = vcpu->kvm;
1027
1028 mutex_lock(&gp->tlb_lock);
1029 switch (ric) {
1030 case 0:
1031 /* Invalidate TLB */
1032 spin_lock(&kvm->mmu_lock);
1033 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
1034 gp->shadow_lpid);
Paul Mackerras690ed4c2018-10-08 16:31:10 +11001035 kvmhv_flush_lpid(gp->shadow_lpid);
Suraj Jitindar Singhe3b6b462018-10-08 16:31:09 +11001036 spin_unlock(&kvm->mmu_lock);
1037 break;
1038 case 1:
1039 /*
1040 * Invalidate PWC
1041 * We don't cache this -> nothing to do
1042 */
1043 break;
1044 case 2:
1045 /* Invalidate TLB, PWC and caching of partition table entries */
1046 kvmhv_flush_nested(gp);
1047 break;
1048 default:
1049 break;
1050 }
1051 mutex_unlock(&gp->tlb_lock);
1052}
1053
1054static void kvmhv_emulate_tlbie_all_lpid(struct kvm_vcpu *vcpu, int ric)
1055{
1056 struct kvm *kvm = vcpu->kvm;
1057 struct kvm_nested_guest *gp;
1058 int i;
1059
1060 spin_lock(&kvm->mmu_lock);
1061 for (i = 0; i <= kvm->arch.max_nested_lpid; i++) {
1062 gp = kvm->arch.nested_guests[i];
1063 if (gp) {
1064 spin_unlock(&kvm->mmu_lock);
1065 kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1066 spin_lock(&kvm->mmu_lock);
1067 }
1068 }
1069 spin_unlock(&kvm->mmu_lock);
1070}
1071
1072static int kvmhv_emulate_priv_tlbie(struct kvm_vcpu *vcpu, unsigned int instr,
1073 unsigned long rsval, unsigned long rbval)
1074{
1075 struct kvm *kvm = vcpu->kvm;
1076 struct kvm_nested_guest *gp;
1077 int r, ric, prs, is, ap;
1078 int lpid;
1079 long epn;
1080 int ret = 0;
1081
1082 ric = get_ric(instr);
1083 prs = get_prs(instr);
1084 r = get_r(instr);
1085 lpid = get_lpid(rsval);
1086 is = get_is(rbval);
1087
1088 /*
1089 * These cases are invalid and are not handled:
1090 * r != 1 -> Only radix supported
1091 * prs == 1 -> Not HV privileged
1092 * ric == 3 -> No cluster bombs for radix
1093 * is == 1 -> Partition scoped translations not associated with pid
1094 * (!is) && (ric == 1 || ric == 2) -> Not supported by ISA
1095 */
1096 if ((!r) || (prs) || (ric == 3) || (is == 1) ||
1097 ((!is) && (ric == 1 || ric == 2)))
1098 return -EINVAL;
1099
1100 switch (is) {
1101 case 0:
1102 /*
1103 * We know ric == 0
1104 * Invalidate TLB for a given target address
1105 */
1106 epn = get_epn(rbval);
1107 ap = get_ap(rbval);
1108 ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap, epn);
1109 break;
1110 case 2:
1111 /* Invalidate matching LPID */
1112 gp = kvmhv_get_nested(kvm, lpid, false);
1113 if (gp) {
1114 kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1115 kvmhv_put_nested(gp);
1116 }
1117 break;
1118 case 3:
1119 /* Invalidate ALL LPIDs */
1120 kvmhv_emulate_tlbie_all_lpid(vcpu, ric);
1121 break;
1122 default:
1123 ret = -EINVAL;
1124 break;
1125 }
1126
1127 return ret;
1128}
1129
1130/*
1131 * This handles the H_TLB_INVALIDATE hcall.
1132 * Parameters are (r4) tlbie instruction code, (r5) rS contents,
1133 * (r6) rB contents.
1134 */
1135long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu)
1136{
1137 int ret;
1138
1139 ret = kvmhv_emulate_priv_tlbie(vcpu, kvmppc_get_gpr(vcpu, 4),
1140 kvmppc_get_gpr(vcpu, 5), kvmppc_get_gpr(vcpu, 6));
1141 if (ret)
1142 return H_PARAMETER;
1143 return H_SUCCESS;
1144}
1145
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001146/* Used to convert a nested guest real address to a L1 guest real address */
1147static int kvmhv_translate_addr_nested(struct kvm_vcpu *vcpu,
1148 struct kvm_nested_guest *gp,
1149 unsigned long n_gpa, unsigned long dsisr,
1150 struct kvmppc_pte *gpte_p)
1151{
1152 u64 fault_addr, flags = dsisr & DSISR_ISSTORE;
1153 int ret;
1154
1155 ret = kvmppc_mmu_walk_radix_tree(vcpu, n_gpa, gpte_p, gp->l1_gr_to_hr,
1156 &fault_addr);
1157
1158 if (ret) {
1159 /* We didn't find a pte */
1160 if (ret == -EINVAL) {
1161 /* Unsupported mmu config */
1162 flags |= DSISR_UNSUPP_MMU;
1163 } else if (ret == -ENOENT) {
1164 /* No translation found */
1165 flags |= DSISR_NOHPTE;
1166 } else if (ret == -EFAULT) {
1167 /* Couldn't access L1 real address */
1168 flags |= DSISR_PRTABLE_FAULT;
1169 vcpu->arch.fault_gpa = fault_addr;
1170 } else {
1171 /* Unknown error */
1172 return ret;
1173 }
1174 goto forward_to_l1;
1175 } else {
1176 /* We found a pte -> check permissions */
1177 if (dsisr & DSISR_ISSTORE) {
1178 /* Can we write? */
1179 if (!gpte_p->may_write) {
1180 flags |= DSISR_PROTFAULT;
1181 goto forward_to_l1;
1182 }
1183 } else if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
1184 /* Can we execute? */
1185 if (!gpte_p->may_execute) {
1186 flags |= SRR1_ISI_N_OR_G;
1187 goto forward_to_l1;
1188 }
1189 } else {
1190 /* Can we read? */
1191 if (!gpte_p->may_read && !gpte_p->may_write) {
1192 flags |= DSISR_PROTFAULT;
1193 goto forward_to_l1;
1194 }
1195 }
1196 }
1197
1198 return 0;
1199
1200forward_to_l1:
1201 vcpu->arch.fault_dsisr = flags;
1202 if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
Nicholas Piggin9ee64712019-10-02 16:00:21 +10001203 vcpu->arch.shregs.msr &= SRR1_MSR_BITS;
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001204 vcpu->arch.shregs.msr |= flags;
1205 }
Paul Mackerras360cae32018-10-08 16:31:04 +11001206 return RESUME_HOST;
1207}
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001208
1209static long kvmhv_handle_nested_set_rc(struct kvm_vcpu *vcpu,
1210 struct kvm_nested_guest *gp,
1211 unsigned long n_gpa,
1212 struct kvmppc_pte gpte,
1213 unsigned long dsisr)
1214{
1215 struct kvm *kvm = vcpu->kvm;
1216 bool writing = !!(dsisr & DSISR_ISSTORE);
1217 u64 pgflags;
Suraj Jitindar Singhbec6e032018-12-21 14:28:39 +11001218 long ret;
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001219
1220 /* Are the rc bits set in the L1 partition scoped pte? */
1221 pgflags = _PAGE_ACCESSED;
1222 if (writing)
1223 pgflags |= _PAGE_DIRTY;
1224 if (pgflags & ~gpte.rc)
1225 return RESUME_HOST;
1226
1227 spin_lock(&kvm->mmu_lock);
1228 /* Set the rc bit in the pte of our (L0) pgtable for the L1 guest */
1229 ret = kvmppc_hv_handle_set_rc(kvm, kvm->arch.pgtable, writing,
1230 gpte.raddr, kvm->arch.lpid);
Suraj Jitindar Singhbec6e032018-12-21 14:28:39 +11001231 if (!ret) {
1232 ret = -EINVAL;
1233 goto out_unlock;
1234 }
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001235
1236 /* Set the rc bit in the pte of the shadow_pgtable for the nest guest */
1237 ret = kvmppc_hv_handle_set_rc(kvm, gp->shadow_pgtable, writing, n_gpa,
1238 gp->shadow_lpid);
1239 if (!ret)
Suraj Jitindar Singhbec6e032018-12-21 14:28:39 +11001240 ret = -EINVAL;
1241 else
1242 ret = 0;
1243
1244out_unlock:
1245 spin_unlock(&kvm->mmu_lock);
1246 return ret;
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001247}
1248
1249static inline int kvmppc_radix_level_to_shift(int level)
1250{
1251 switch (level) {
1252 case 2:
1253 return PUD_SHIFT;
1254 case 1:
1255 return PMD_SHIFT;
1256 default:
1257 return PAGE_SHIFT;
1258 }
1259}
1260
1261static inline int kvmppc_radix_shift_to_level(int shift)
1262{
1263 if (shift == PUD_SHIFT)
1264 return 2;
1265 if (shift == PMD_SHIFT)
1266 return 1;
1267 if (shift == PAGE_SHIFT)
1268 return 0;
1269 WARN_ON_ONCE(1);
1270 return 0;
1271}
1272
1273/* called with gp->tlb_lock held */
Suraj Jitindar Singh873db2c2018-12-14 16:29:08 +11001274static long int __kvmhv_nested_page_fault(struct kvm_run *run,
1275 struct kvm_vcpu *vcpu,
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001276 struct kvm_nested_guest *gp)
1277{
1278 struct kvm *kvm = vcpu->kvm;
1279 struct kvm_memory_slot *memslot;
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +11001280 struct rmap_nested *n_rmap;
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001281 struct kvmppc_pte gpte;
1282 pte_t pte, *pte_p;
1283 unsigned long mmu_seq;
1284 unsigned long dsisr = vcpu->arch.fault_dsisr;
1285 unsigned long ea = vcpu->arch.fault_dar;
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +11001286 unsigned long *rmapp;
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001287 unsigned long n_gpa, gpa, gfn, perm = 0UL;
1288 unsigned int shift, l1_shift, level;
1289 bool writing = !!(dsisr & DSISR_ISSTORE);
1290 bool kvm_ro = false;
1291 long int ret;
1292
1293 if (!gp->l1_gr_to_hr) {
1294 kvmhv_update_ptbl_cache(gp);
1295 if (!gp->l1_gr_to_hr)
1296 return RESUME_HOST;
1297 }
1298
1299 /* Convert the nested guest real address into a L1 guest real address */
1300
1301 n_gpa = vcpu->arch.fault_gpa & ~0xF000000000000FFFULL;
1302 if (!(dsisr & DSISR_PRTABLE_FAULT))
1303 n_gpa |= ea & 0xFFF;
1304 ret = kvmhv_translate_addr_nested(vcpu, gp, n_gpa, dsisr, &gpte);
1305
1306 /*
1307 * If the hardware found a translation but we don't now have a usable
1308 * translation in the l1 partition-scoped tree, remove the shadow pte
1309 * and let the guest retry.
1310 */
1311 if (ret == RESUME_HOST &&
1312 (dsisr & (DSISR_PROTFAULT | DSISR_BADACCESS | DSISR_NOEXEC_OR_G |
1313 DSISR_BAD_COPYPASTE)))
1314 goto inval;
1315 if (ret)
1316 return ret;
1317
1318 /* Failed to set the reference/change bits */
1319 if (dsisr & DSISR_SET_RC) {
1320 ret = kvmhv_handle_nested_set_rc(vcpu, gp, n_gpa, gpte, dsisr);
1321 if (ret == RESUME_HOST)
1322 return ret;
1323 if (ret)
1324 goto inval;
1325 dsisr &= ~DSISR_SET_RC;
1326 if (!(dsisr & (DSISR_BAD_FAULT_64S | DSISR_NOHPTE |
1327 DSISR_PROTFAULT)))
1328 return RESUME_GUEST;
1329 }
1330
1331 /*
1332 * We took an HISI or HDSI while we were running a nested guest which
1333 * means we have no partition scoped translation for that. This means
1334 * we need to insert a pte for the mapping into our shadow_pgtable.
1335 */
1336
1337 l1_shift = gpte.page_shift;
1338 if (l1_shift < PAGE_SHIFT) {
1339 /* We don't support l1 using a page size smaller than our own */
1340 pr_err("KVM: L1 guest page shift (%d) less than our own (%d)\n",
1341 l1_shift, PAGE_SHIFT);
1342 return -EINVAL;
1343 }
1344 gpa = gpte.raddr;
1345 gfn = gpa >> PAGE_SHIFT;
1346
1347 /* 1. Get the corresponding host memslot */
1348
1349 memslot = gfn_to_memslot(kvm, gfn);
1350 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
1351 if (dsisr & (DSISR_PRTABLE_FAULT | DSISR_BADACCESS)) {
1352 /* unusual error -> reflect to the guest as a DSI */
1353 kvmppc_core_queue_data_storage(vcpu, ea, dsisr);
1354 return RESUME_GUEST;
1355 }
Suraj Jitindar Singh873db2c2018-12-14 16:29:08 +11001356
1357 /* passthrough of emulated MMIO case */
Suraj Jitindar Singh873db2c2018-12-14 16:29:08 +11001358 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea, writing);
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001359 }
1360 if (memslot->flags & KVM_MEM_READONLY) {
1361 if (writing) {
1362 /* Give the guest a DSI */
1363 kvmppc_core_queue_data_storage(vcpu, ea,
1364 DSISR_ISSTORE | DSISR_PROTFAULT);
1365 return RESUME_GUEST;
1366 }
1367 kvm_ro = true;
1368 }
1369
1370 /* 2. Find the host pte for this L1 guest real address */
1371
1372 /* Used to check for invalidations in progress */
1373 mmu_seq = kvm->mmu_notifier_seq;
1374 smp_rmb();
1375
1376 /* See if can find translation in our partition scoped tables for L1 */
1377 pte = __pte(0);
1378 spin_lock(&kvm->mmu_lock);
Aneesh Kumar K.V4b994122020-05-05 12:47:16 +05301379 pte_p = find_kvm_secondary_pte(kvm, gpa, &shift);
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001380 if (!shift)
1381 shift = PAGE_SHIFT;
1382 if (pte_p)
1383 pte = *pte_p;
1384 spin_unlock(&kvm->mmu_lock);
1385
1386 if (!pte_present(pte) || (writing && !(pte_val(pte) & _PAGE_WRITE))) {
1387 /* No suitable pte found -> try to insert a mapping */
1388 ret = kvmppc_book3s_instantiate_page(vcpu, gpa, memslot,
1389 writing, kvm_ro, &pte, &level);
1390 if (ret == -EAGAIN)
1391 return RESUME_GUEST;
1392 else if (ret)
1393 return ret;
1394 shift = kvmppc_radix_level_to_shift(level);
1395 }
Suraj Jitindar Singh8400f872018-12-21 14:28:40 +11001396 /* Align gfn to the start of the page */
1397 gfn = (gpa & ~((1UL << shift) - 1)) >> PAGE_SHIFT;
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001398
1399 /* 3. Compute the pte we need to insert for nest_gpa -> host r_addr */
1400
1401 /* The permissions is the combination of the host and l1 guest ptes */
1402 perm |= gpte.may_read ? 0UL : _PAGE_READ;
1403 perm |= gpte.may_write ? 0UL : _PAGE_WRITE;
1404 perm |= gpte.may_execute ? 0UL : _PAGE_EXEC;
Suraj Jitindar Singh8b23eee2018-12-21 14:28:41 +11001405 /* Only set accessed/dirty (rc) bits if set in host and l1 guest ptes */
1406 perm |= (gpte.rc & _PAGE_ACCESSED) ? 0UL : _PAGE_ACCESSED;
1407 perm |= ((gpte.rc & _PAGE_DIRTY) && writing) ? 0UL : _PAGE_DIRTY;
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001408 pte = __pte(pte_val(pte) & ~perm);
1409
1410 /* What size pte can we insert? */
1411 if (shift > l1_shift) {
1412 u64 mask;
1413 unsigned int actual_shift = PAGE_SHIFT;
1414 if (PMD_SHIFT < l1_shift)
1415 actual_shift = PMD_SHIFT;
1416 mask = (1UL << shift) - (1UL << actual_shift);
1417 pte = __pte(pte_val(pte) | (gpa & mask));
1418 shift = actual_shift;
1419 }
1420 level = kvmppc_radix_shift_to_level(shift);
1421 n_gpa &= ~((1UL << shift) - 1);
1422
1423 /* 4. Insert the pte into our shadow_pgtable */
1424
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +11001425 n_rmap = kzalloc(sizeof(*n_rmap), GFP_KERNEL);
1426 if (!n_rmap)
1427 return RESUME_GUEST; /* Let the guest try again */
1428 n_rmap->rmap = (n_gpa & RMAP_NESTED_GPA_MASK) |
1429 (((unsigned long) gp->l1_lpid) << RMAP_NESTED_LPID_SHIFT);
1430 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001431 ret = kvmppc_create_pte(kvm, gp->shadow_pgtable, pte, n_gpa, level,
Suraj Jitindar Singh8cf531e2018-10-08 16:31:08 +11001432 mmu_seq, gp->shadow_lpid, rmapp, &n_rmap);
1433 if (n_rmap)
1434 kfree(n_rmap);
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001435 if (ret == -EAGAIN)
1436 ret = RESUME_GUEST; /* Let the guest try again */
1437
1438 return ret;
1439
1440 inval:
1441 kvmhv_invalidate_shadow_pte(vcpu, gp, n_gpa, NULL);
1442 return RESUME_GUEST;
1443}
1444
Suraj Jitindar Singh873db2c2018-12-14 16:29:08 +11001445long int kvmhv_nested_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu)
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001446{
1447 struct kvm_nested_guest *gp = vcpu->arch.nested;
1448 long int ret;
1449
1450 mutex_lock(&gp->tlb_lock);
Suraj Jitindar Singh873db2c2018-12-14 16:29:08 +11001451 ret = __kvmhv_nested_page_fault(run, vcpu, gp);
Suraj Jitindar Singhfd10be22018-10-08 16:31:07 +11001452 mutex_unlock(&gp->tlb_lock);
1453 return ret;
1454}
Paul Mackerras83a05512018-10-08 16:31:17 +11001455
1456int kvmhv_nested_next_lpid(struct kvm *kvm, int lpid)
1457{
1458 int ret = -1;
1459
1460 spin_lock(&kvm->mmu_lock);
1461 while (++lpid <= kvm->arch.max_nested_lpid) {
1462 if (kvm->arch.nested_guests[lpid]) {
1463 ret = lpid;
1464 break;
1465 }
1466 }
1467 spin_unlock(&kvm->mmu_lock);
1468 return ret;
1469}