xref: /linux/arch/powerpc/kvm/book3s_hv_nested.c (revision 81468083)
1 // 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>
13 #include <linux/llist.h>
14 #include <linux/pgtable.h>
15 
16 #include <asm/kvm_ppc.h>
17 #include <asm/kvm_book3s.h>
18 #include <asm/mmu.h>
19 #include <asm/pgalloc.h>
20 #include <asm/pte-walk.h>
21 #include <asm/reg.h>
22 #include <asm/plpar_wrappers.h>
23 
24 static struct patb_entry *pseries_partition_tb;
25 
26 static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp);
27 static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free);
28 
29 void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
30 {
31 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
32 
33 	hr->pcr = vc->pcr | PCR_MASK;
34 	hr->dpdes = vc->dpdes;
35 	hr->hfscr = vcpu->arch.hfscr;
36 	hr->tb_offset = vc->tb_offset;
37 	hr->dawr0 = vcpu->arch.dawr0;
38 	hr->dawrx0 = vcpu->arch.dawrx0;
39 	hr->ciabr = vcpu->arch.ciabr;
40 	hr->purr = vcpu->arch.purr;
41 	hr->spurr = vcpu->arch.spurr;
42 	hr->ic = vcpu->arch.ic;
43 	hr->vtb = vc->vtb;
44 	hr->srr0 = vcpu->arch.shregs.srr0;
45 	hr->srr1 = vcpu->arch.shregs.srr1;
46 	hr->sprg[0] = vcpu->arch.shregs.sprg0;
47 	hr->sprg[1] = vcpu->arch.shregs.sprg1;
48 	hr->sprg[2] = vcpu->arch.shregs.sprg2;
49 	hr->sprg[3] = vcpu->arch.shregs.sprg3;
50 	hr->pidr = vcpu->arch.pid;
51 	hr->cfar = vcpu->arch.cfar;
52 	hr->ppr = vcpu->arch.ppr;
53 	hr->dawr1 = vcpu->arch.dawr1;
54 	hr->dawrx1 = vcpu->arch.dawrx1;
55 }
56 
57 static void byteswap_pt_regs(struct pt_regs *regs)
58 {
59 	unsigned long *addr = (unsigned long *) regs;
60 
61 	for (; addr < ((unsigned long *) (regs + 1)); addr++)
62 		*addr = swab64(*addr);
63 }
64 
65 static void byteswap_hv_regs(struct hv_guest_state *hr)
66 {
67 	hr->version = swab64(hr->version);
68 	hr->lpid = swab32(hr->lpid);
69 	hr->vcpu_token = swab32(hr->vcpu_token);
70 	hr->lpcr = swab64(hr->lpcr);
71 	hr->pcr = swab64(hr->pcr) | PCR_MASK;
72 	hr->amor = swab64(hr->amor);
73 	hr->dpdes = swab64(hr->dpdes);
74 	hr->hfscr = swab64(hr->hfscr);
75 	hr->tb_offset = swab64(hr->tb_offset);
76 	hr->dawr0 = swab64(hr->dawr0);
77 	hr->dawrx0 = swab64(hr->dawrx0);
78 	hr->ciabr = swab64(hr->ciabr);
79 	hr->hdec_expiry = swab64(hr->hdec_expiry);
80 	hr->purr = swab64(hr->purr);
81 	hr->spurr = swab64(hr->spurr);
82 	hr->ic = swab64(hr->ic);
83 	hr->vtb = swab64(hr->vtb);
84 	hr->hdar = swab64(hr->hdar);
85 	hr->hdsisr = swab64(hr->hdsisr);
86 	hr->heir = swab64(hr->heir);
87 	hr->asdr = swab64(hr->asdr);
88 	hr->srr0 = swab64(hr->srr0);
89 	hr->srr1 = swab64(hr->srr1);
90 	hr->sprg[0] = swab64(hr->sprg[0]);
91 	hr->sprg[1] = swab64(hr->sprg[1]);
92 	hr->sprg[2] = swab64(hr->sprg[2]);
93 	hr->sprg[3] = swab64(hr->sprg[3]);
94 	hr->pidr = swab64(hr->pidr);
95 	hr->cfar = swab64(hr->cfar);
96 	hr->ppr = swab64(hr->ppr);
97 	hr->dawr1 = swab64(hr->dawr1);
98 	hr->dawrx1 = swab64(hr->dawrx1);
99 }
100 
101 static void save_hv_return_state(struct kvm_vcpu *vcpu, int trap,
102 				 struct hv_guest_state *hr)
103 {
104 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
105 
106 	hr->dpdes = vc->dpdes;
107 	hr->hfscr = vcpu->arch.hfscr;
108 	hr->purr = vcpu->arch.purr;
109 	hr->spurr = vcpu->arch.spurr;
110 	hr->ic = vcpu->arch.ic;
111 	hr->vtb = vc->vtb;
112 	hr->srr0 = vcpu->arch.shregs.srr0;
113 	hr->srr1 = vcpu->arch.shregs.srr1;
114 	hr->sprg[0] = vcpu->arch.shregs.sprg0;
115 	hr->sprg[1] = vcpu->arch.shregs.sprg1;
116 	hr->sprg[2] = vcpu->arch.shregs.sprg2;
117 	hr->sprg[3] = vcpu->arch.shregs.sprg3;
118 	hr->pidr = vcpu->arch.pid;
119 	hr->cfar = vcpu->arch.cfar;
120 	hr->ppr = vcpu->arch.ppr;
121 	switch (trap) {
122 	case BOOK3S_INTERRUPT_H_DATA_STORAGE:
123 		hr->hdar = vcpu->arch.fault_dar;
124 		hr->hdsisr = vcpu->arch.fault_dsisr;
125 		hr->asdr = vcpu->arch.fault_gpa;
126 		break;
127 	case BOOK3S_INTERRUPT_H_INST_STORAGE:
128 		hr->asdr = vcpu->arch.fault_gpa;
129 		break;
130 	case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
131 		hr->heir = vcpu->arch.emul_inst;
132 		break;
133 	}
134 }
135 
136 /*
137  * This can result in some L0 HV register state being leaked to an L1
138  * hypervisor when the hv_guest_state is copied back to the guest after
139  * being modified here.
140  *
141  * There is no known problem with such a leak, and in many cases these
142  * register settings could be derived by the guest by observing behaviour
143  * and timing, interrupts, etc., but it is an issue to consider.
144  */
145 static void sanitise_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
146 {
147 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
148 	u64 mask;
149 
150 	/*
151 	 * Don't let L1 change LPCR bits for the L2 except these:
152 	 */
153 	mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD |
154 		LPCR_LPES | LPCR_MER;
155 
156 	/*
157 	 * Additional filtering is required depending on hardware
158 	 * and configuration.
159 	 */
160 	hr->lpcr = kvmppc_filter_lpcr_hv(vcpu->kvm,
161 			(vc->lpcr & ~mask) | (hr->lpcr & mask));
162 
163 	/*
164 	 * Don't let L1 enable features for L2 which we've disabled for L1,
165 	 * but preserve the interrupt cause field.
166 	 */
167 	hr->hfscr &= (HFSCR_INTR_CAUSE | vcpu->arch.hfscr);
168 
169 	/* Don't let data address watchpoint match in hypervisor state */
170 	hr->dawrx0 &= ~DAWRX_HYP;
171 	hr->dawrx1 &= ~DAWRX_HYP;
172 
173 	/* Don't let completed instruction address breakpt match in HV state */
174 	if ((hr->ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
175 		hr->ciabr &= ~CIABR_PRIV;
176 }
177 
178 static void restore_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
179 {
180 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
181 
182 	vc->pcr = hr->pcr | PCR_MASK;
183 	vc->dpdes = hr->dpdes;
184 	vcpu->arch.hfscr = hr->hfscr;
185 	vcpu->arch.dawr0 = hr->dawr0;
186 	vcpu->arch.dawrx0 = hr->dawrx0;
187 	vcpu->arch.ciabr = hr->ciabr;
188 	vcpu->arch.purr = hr->purr;
189 	vcpu->arch.spurr = hr->spurr;
190 	vcpu->arch.ic = hr->ic;
191 	vc->vtb = hr->vtb;
192 	vcpu->arch.shregs.srr0 = hr->srr0;
193 	vcpu->arch.shregs.srr1 = hr->srr1;
194 	vcpu->arch.shregs.sprg0 = hr->sprg[0];
195 	vcpu->arch.shregs.sprg1 = hr->sprg[1];
196 	vcpu->arch.shregs.sprg2 = hr->sprg[2];
197 	vcpu->arch.shregs.sprg3 = hr->sprg[3];
198 	vcpu->arch.pid = hr->pidr;
199 	vcpu->arch.cfar = hr->cfar;
200 	vcpu->arch.ppr = hr->ppr;
201 	vcpu->arch.dawr1 = hr->dawr1;
202 	vcpu->arch.dawrx1 = hr->dawrx1;
203 }
204 
205 void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,
206 				   struct hv_guest_state *hr)
207 {
208 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
209 
210 	vc->dpdes = hr->dpdes;
211 	vcpu->arch.hfscr = hr->hfscr;
212 	vcpu->arch.purr = hr->purr;
213 	vcpu->arch.spurr = hr->spurr;
214 	vcpu->arch.ic = hr->ic;
215 	vc->vtb = hr->vtb;
216 	vcpu->arch.fault_dar = hr->hdar;
217 	vcpu->arch.fault_dsisr = hr->hdsisr;
218 	vcpu->arch.fault_gpa = hr->asdr;
219 	vcpu->arch.emul_inst = hr->heir;
220 	vcpu->arch.shregs.srr0 = hr->srr0;
221 	vcpu->arch.shregs.srr1 = hr->srr1;
222 	vcpu->arch.shregs.sprg0 = hr->sprg[0];
223 	vcpu->arch.shregs.sprg1 = hr->sprg[1];
224 	vcpu->arch.shregs.sprg2 = hr->sprg[2];
225 	vcpu->arch.shregs.sprg3 = hr->sprg[3];
226 	vcpu->arch.pid = hr->pidr;
227 	vcpu->arch.cfar = hr->cfar;
228 	vcpu->arch.ppr = hr->ppr;
229 }
230 
231 static void kvmhv_nested_mmio_needed(struct kvm_vcpu *vcpu, u64 regs_ptr)
232 {
233 	/* No need to reflect the page fault to L1, we've handled it */
234 	vcpu->arch.trap = 0;
235 
236 	/*
237 	 * Since the L2 gprs have already been written back into L1 memory when
238 	 * we complete the mmio, store the L1 memory location of the L2 gpr
239 	 * being loaded into by the mmio so that the loaded value can be
240 	 * written there in kvmppc_complete_mmio_load()
241 	 */
242 	if (((vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) == KVM_MMIO_REG_GPR)
243 	    && (vcpu->mmio_is_write == 0)) {
244 		vcpu->arch.nested_io_gpr = (gpa_t) regs_ptr +
245 					   offsetof(struct pt_regs,
246 						    gpr[vcpu->arch.io_gpr]);
247 		vcpu->arch.io_gpr = KVM_MMIO_REG_NESTED_GPR;
248 	}
249 }
250 
251 static int kvmhv_read_guest_state_and_regs(struct kvm_vcpu *vcpu,
252 					   struct hv_guest_state *l2_hv,
253 					   struct pt_regs *l2_regs,
254 					   u64 hv_ptr, u64 regs_ptr)
255 {
256 	int size;
257 
258 	if (kvm_vcpu_read_guest(vcpu, hv_ptr, &l2_hv->version,
259 				sizeof(l2_hv->version)))
260 		return -1;
261 
262 	if (kvmppc_need_byteswap(vcpu))
263 		l2_hv->version = swab64(l2_hv->version);
264 
265 	size = hv_guest_state_size(l2_hv->version);
266 	if (size < 0)
267 		return -1;
268 
269 	return kvm_vcpu_read_guest(vcpu, hv_ptr, l2_hv, size) ||
270 		kvm_vcpu_read_guest(vcpu, regs_ptr, l2_regs,
271 				    sizeof(struct pt_regs));
272 }
273 
274 static int kvmhv_write_guest_state_and_regs(struct kvm_vcpu *vcpu,
275 					    struct hv_guest_state *l2_hv,
276 					    struct pt_regs *l2_regs,
277 					    u64 hv_ptr, u64 regs_ptr)
278 {
279 	int size;
280 
281 	size = hv_guest_state_size(l2_hv->version);
282 	if (size < 0)
283 		return -1;
284 
285 	return kvm_vcpu_write_guest(vcpu, hv_ptr, l2_hv, size) ||
286 		kvm_vcpu_write_guest(vcpu, regs_ptr, l2_regs,
287 				     sizeof(struct pt_regs));
288 }
289 
290 long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)
291 {
292 	long int err, r;
293 	struct kvm_nested_guest *l2;
294 	struct pt_regs l2_regs, saved_l1_regs;
295 	struct hv_guest_state l2_hv = {0}, saved_l1_hv;
296 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
297 	u64 hv_ptr, regs_ptr;
298 	u64 hdec_exp;
299 	s64 delta_purr, delta_spurr, delta_ic, delta_vtb;
300 
301 	if (vcpu->kvm->arch.l1_ptcr == 0)
302 		return H_NOT_AVAILABLE;
303 
304 	/* copy parameters in */
305 	hv_ptr = kvmppc_get_gpr(vcpu, 4);
306 	regs_ptr = kvmppc_get_gpr(vcpu, 5);
307 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
308 	err = kvmhv_read_guest_state_and_regs(vcpu, &l2_hv, &l2_regs,
309 					      hv_ptr, regs_ptr);
310 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
311 	if (err)
312 		return H_PARAMETER;
313 
314 	if (kvmppc_need_byteswap(vcpu))
315 		byteswap_hv_regs(&l2_hv);
316 	if (l2_hv.version > HV_GUEST_STATE_VERSION)
317 		return H_P2;
318 
319 	if (kvmppc_need_byteswap(vcpu))
320 		byteswap_pt_regs(&l2_regs);
321 	if (l2_hv.vcpu_token >= NR_CPUS)
322 		return H_PARAMETER;
323 
324 	/* translate lpid */
325 	l2 = kvmhv_get_nested(vcpu->kvm, l2_hv.lpid, true);
326 	if (!l2)
327 		return H_PARAMETER;
328 	if (!l2->l1_gr_to_hr) {
329 		mutex_lock(&l2->tlb_lock);
330 		kvmhv_update_ptbl_cache(l2);
331 		mutex_unlock(&l2->tlb_lock);
332 	}
333 
334 	/* save l1 values of things */
335 	vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
336 	saved_l1_regs = vcpu->arch.regs;
337 	kvmhv_save_hv_regs(vcpu, &saved_l1_hv);
338 
339 	/* convert TB values/offsets to host (L0) values */
340 	hdec_exp = l2_hv.hdec_expiry - vc->tb_offset;
341 	vc->tb_offset += l2_hv.tb_offset;
342 
343 	/* set L1 state to L2 state */
344 	vcpu->arch.nested = l2;
345 	vcpu->arch.nested_vcpu_id = l2_hv.vcpu_token;
346 	vcpu->arch.regs = l2_regs;
347 
348 	/* Guest must always run with ME enabled, HV disabled. */
349 	vcpu->arch.shregs.msr = (vcpu->arch.regs.msr | MSR_ME) & ~MSR_HV;
350 
351 	sanitise_hv_regs(vcpu, &l2_hv);
352 	restore_hv_regs(vcpu, &l2_hv);
353 
354 	vcpu->arch.ret = RESUME_GUEST;
355 	vcpu->arch.trap = 0;
356 	do {
357 		if (mftb() >= hdec_exp) {
358 			vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER;
359 			r = RESUME_HOST;
360 			break;
361 		}
362 		r = kvmhv_run_single_vcpu(vcpu, hdec_exp, l2_hv.lpcr);
363 	} while (is_kvmppc_resume_guest(r));
364 
365 	/* save L2 state for return */
366 	l2_regs = vcpu->arch.regs;
367 	l2_regs.msr = vcpu->arch.shregs.msr;
368 	delta_purr = vcpu->arch.purr - l2_hv.purr;
369 	delta_spurr = vcpu->arch.spurr - l2_hv.spurr;
370 	delta_ic = vcpu->arch.ic - l2_hv.ic;
371 	delta_vtb = vc->vtb - l2_hv.vtb;
372 	save_hv_return_state(vcpu, vcpu->arch.trap, &l2_hv);
373 
374 	/* restore L1 state */
375 	vcpu->arch.nested = NULL;
376 	vcpu->arch.regs = saved_l1_regs;
377 	vcpu->arch.shregs.msr = saved_l1_regs.msr & ~MSR_TS_MASK;
378 	/* set L1 MSR TS field according to L2 transaction state */
379 	if (l2_regs.msr & MSR_TS_MASK)
380 		vcpu->arch.shregs.msr |= MSR_TS_S;
381 	vc->tb_offset = saved_l1_hv.tb_offset;
382 	restore_hv_regs(vcpu, &saved_l1_hv);
383 	vcpu->arch.purr += delta_purr;
384 	vcpu->arch.spurr += delta_spurr;
385 	vcpu->arch.ic += delta_ic;
386 	vc->vtb += delta_vtb;
387 
388 	kvmhv_put_nested(l2);
389 
390 	/* copy l2_hv_state and regs back to guest */
391 	if (kvmppc_need_byteswap(vcpu)) {
392 		byteswap_hv_regs(&l2_hv);
393 		byteswap_pt_regs(&l2_regs);
394 	}
395 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
396 	err = kvmhv_write_guest_state_and_regs(vcpu, &l2_hv, &l2_regs,
397 					       hv_ptr, regs_ptr);
398 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
399 	if (err)
400 		return H_AUTHORITY;
401 
402 	if (r == -EINTR)
403 		return H_INTERRUPT;
404 
405 	if (vcpu->mmio_needed) {
406 		kvmhv_nested_mmio_needed(vcpu, regs_ptr);
407 		return H_TOO_HARD;
408 	}
409 
410 	return vcpu->arch.trap;
411 }
412 
413 long kvmhv_nested_init(void)
414 {
415 	long int ptb_order;
416 	unsigned long ptcr;
417 	long rc;
418 
419 	if (!kvmhv_on_pseries())
420 		return 0;
421 	if (!radix_enabled())
422 		return -ENODEV;
423 
424 	/* find log base 2 of KVMPPC_NR_LPIDS, rounding up */
425 	ptb_order = __ilog2(KVMPPC_NR_LPIDS - 1) + 1;
426 	if (ptb_order < 8)
427 		ptb_order = 8;
428 	pseries_partition_tb = kmalloc(sizeof(struct patb_entry) << ptb_order,
429 				       GFP_KERNEL);
430 	if (!pseries_partition_tb) {
431 		pr_err("kvm-hv: failed to allocated nested partition table\n");
432 		return -ENOMEM;
433 	}
434 
435 	ptcr = __pa(pseries_partition_tb) | (ptb_order - 8);
436 	rc = plpar_hcall_norets(H_SET_PARTITION_TABLE, ptcr);
437 	if (rc != H_SUCCESS) {
438 		pr_err("kvm-hv: Parent hypervisor does not support nesting (rc=%ld)\n",
439 		       rc);
440 		kfree(pseries_partition_tb);
441 		pseries_partition_tb = NULL;
442 		return -ENODEV;
443 	}
444 
445 	return 0;
446 }
447 
448 void kvmhv_nested_exit(void)
449 {
450 	/*
451 	 * N.B. the kvmhv_on_pseries() test is there because it enables
452 	 * the compiler to remove the call to plpar_hcall_norets()
453 	 * when CONFIG_PPC_PSERIES=n.
454 	 */
455 	if (kvmhv_on_pseries() && pseries_partition_tb) {
456 		plpar_hcall_norets(H_SET_PARTITION_TABLE, 0);
457 		kfree(pseries_partition_tb);
458 		pseries_partition_tb = NULL;
459 	}
460 }
461 
462 static void kvmhv_flush_lpid(unsigned int lpid)
463 {
464 	long rc;
465 
466 	if (!kvmhv_on_pseries()) {
467 		radix__flush_all_lpid(lpid);
468 		return;
469 	}
470 
471 	if (!firmware_has_feature(FW_FEATURE_RPT_INVALIDATE))
472 		rc = plpar_hcall_norets(H_TLB_INVALIDATE, H_TLBIE_P1_ENC(2, 0, 1),
473 					lpid, TLBIEL_INVAL_SET_LPID);
474 	else
475 		rc = pseries_rpt_invalidate(lpid, H_RPTI_TARGET_CMMU,
476 					    H_RPTI_TYPE_NESTED |
477 					    H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
478 					    H_RPTI_TYPE_PAT,
479 					    H_RPTI_PAGE_ALL, 0, -1UL);
480 	if (rc)
481 		pr_err("KVM: TLB LPID invalidation hcall failed, rc=%ld\n", rc);
482 }
483 
484 void kvmhv_set_ptbl_entry(unsigned int lpid, u64 dw0, u64 dw1)
485 {
486 	if (!kvmhv_on_pseries()) {
487 		mmu_partition_table_set_entry(lpid, dw0, dw1, true);
488 		return;
489 	}
490 
491 	pseries_partition_tb[lpid].patb0 = cpu_to_be64(dw0);
492 	pseries_partition_tb[lpid].patb1 = cpu_to_be64(dw1);
493 	/* L0 will do the necessary barriers */
494 	kvmhv_flush_lpid(lpid);
495 }
496 
497 static void kvmhv_set_nested_ptbl(struct kvm_nested_guest *gp)
498 {
499 	unsigned long dw0;
500 
501 	dw0 = PATB_HR | radix__get_tree_size() |
502 		__pa(gp->shadow_pgtable) | RADIX_PGD_INDEX_SIZE;
503 	kvmhv_set_ptbl_entry(gp->shadow_lpid, dw0, gp->process_table);
504 }
505 
506 void kvmhv_vm_nested_init(struct kvm *kvm)
507 {
508 	kvm->arch.max_nested_lpid = -1;
509 }
510 
511 /*
512  * Handle the H_SET_PARTITION_TABLE hcall.
513  * r4 = guest real address of partition table + log_2(size) - 12
514  * (formatted as for the PTCR).
515  */
516 long kvmhv_set_partition_table(struct kvm_vcpu *vcpu)
517 {
518 	struct kvm *kvm = vcpu->kvm;
519 	unsigned long ptcr = kvmppc_get_gpr(vcpu, 4);
520 	int srcu_idx;
521 	long ret = H_SUCCESS;
522 
523 	srcu_idx = srcu_read_lock(&kvm->srcu);
524 	/*
525 	 * Limit the partition table to 4096 entries (because that's what
526 	 * hardware supports), and check the base address.
527 	 */
528 	if ((ptcr & PRTS_MASK) > 12 - 8 ||
529 	    !kvm_is_visible_gfn(vcpu->kvm, (ptcr & PRTB_MASK) >> PAGE_SHIFT))
530 		ret = H_PARAMETER;
531 	srcu_read_unlock(&kvm->srcu, srcu_idx);
532 	if (ret == H_SUCCESS)
533 		kvm->arch.l1_ptcr = ptcr;
534 	return ret;
535 }
536 
537 /*
538  * Handle the H_COPY_TOFROM_GUEST hcall.
539  * r4 = L1 lpid of nested guest
540  * r5 = pid
541  * r6 = eaddr to access
542  * r7 = to buffer (L1 gpa)
543  * r8 = from buffer (L1 gpa)
544  * r9 = n bytes to copy
545  */
546 long kvmhv_copy_tofrom_guest_nested(struct kvm_vcpu *vcpu)
547 {
548 	struct kvm_nested_guest *gp;
549 	int l1_lpid = kvmppc_get_gpr(vcpu, 4);
550 	int pid = kvmppc_get_gpr(vcpu, 5);
551 	gva_t eaddr = kvmppc_get_gpr(vcpu, 6);
552 	gpa_t gp_to = (gpa_t) kvmppc_get_gpr(vcpu, 7);
553 	gpa_t gp_from = (gpa_t) kvmppc_get_gpr(vcpu, 8);
554 	void *buf;
555 	unsigned long n = kvmppc_get_gpr(vcpu, 9);
556 	bool is_load = !!gp_to;
557 	long rc;
558 
559 	if (gp_to && gp_from) /* One must be NULL to determine the direction */
560 		return H_PARAMETER;
561 
562 	if (eaddr & (0xFFFUL << 52))
563 		return H_PARAMETER;
564 
565 	buf = kzalloc(n, GFP_KERNEL);
566 	if (!buf)
567 		return H_NO_MEM;
568 
569 	gp = kvmhv_get_nested(vcpu->kvm, l1_lpid, false);
570 	if (!gp) {
571 		rc = H_PARAMETER;
572 		goto out_free;
573 	}
574 
575 	mutex_lock(&gp->tlb_lock);
576 
577 	if (is_load) {
578 		/* Load from the nested guest into our buffer */
579 		rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
580 						     eaddr, buf, NULL, n);
581 		if (rc)
582 			goto not_found;
583 
584 		/* Write what was loaded into our buffer back to the L1 guest */
585 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
586 		rc = kvm_vcpu_write_guest(vcpu, gp_to, buf, n);
587 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
588 		if (rc)
589 			goto not_found;
590 	} else {
591 		/* Load the data to be stored from the L1 guest into our buf */
592 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
593 		rc = kvm_vcpu_read_guest(vcpu, gp_from, buf, n);
594 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
595 		if (rc)
596 			goto not_found;
597 
598 		/* Store from our buffer into the nested guest */
599 		rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
600 						     eaddr, NULL, buf, n);
601 		if (rc)
602 			goto not_found;
603 	}
604 
605 out_unlock:
606 	mutex_unlock(&gp->tlb_lock);
607 	kvmhv_put_nested(gp);
608 out_free:
609 	kfree(buf);
610 	return rc;
611 not_found:
612 	rc = H_NOT_FOUND;
613 	goto out_unlock;
614 }
615 
616 /*
617  * Reload the partition table entry for a guest.
618  * Caller must hold gp->tlb_lock.
619  */
620 static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp)
621 {
622 	int ret;
623 	struct patb_entry ptbl_entry;
624 	unsigned long ptbl_addr;
625 	struct kvm *kvm = gp->l1_host;
626 
627 	ret = -EFAULT;
628 	ptbl_addr = (kvm->arch.l1_ptcr & PRTB_MASK) + (gp->l1_lpid << 4);
629 	if (gp->l1_lpid < (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 8))) {
630 		int srcu_idx = srcu_read_lock(&kvm->srcu);
631 		ret = kvm_read_guest(kvm, ptbl_addr,
632 				     &ptbl_entry, sizeof(ptbl_entry));
633 		srcu_read_unlock(&kvm->srcu, srcu_idx);
634 	}
635 	if (ret) {
636 		gp->l1_gr_to_hr = 0;
637 		gp->process_table = 0;
638 	} else {
639 		gp->l1_gr_to_hr = be64_to_cpu(ptbl_entry.patb0);
640 		gp->process_table = be64_to_cpu(ptbl_entry.patb1);
641 	}
642 	kvmhv_set_nested_ptbl(gp);
643 }
644 
645 static struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid)
646 {
647 	struct kvm_nested_guest *gp;
648 	long shadow_lpid;
649 
650 	gp = kzalloc(sizeof(*gp), GFP_KERNEL);
651 	if (!gp)
652 		return NULL;
653 	gp->l1_host = kvm;
654 	gp->l1_lpid = lpid;
655 	mutex_init(&gp->tlb_lock);
656 	gp->shadow_pgtable = pgd_alloc(kvm->mm);
657 	if (!gp->shadow_pgtable)
658 		goto out_free;
659 	shadow_lpid = kvmppc_alloc_lpid();
660 	if (shadow_lpid < 0)
661 		goto out_free2;
662 	gp->shadow_lpid = shadow_lpid;
663 	gp->radix = 1;
664 
665 	memset(gp->prev_cpu, -1, sizeof(gp->prev_cpu));
666 
667 	return gp;
668 
669  out_free2:
670 	pgd_free(kvm->mm, gp->shadow_pgtable);
671  out_free:
672 	kfree(gp);
673 	return NULL;
674 }
675 
676 /*
677  * Free up any resources allocated for a nested guest.
678  */
679 static void kvmhv_release_nested(struct kvm_nested_guest *gp)
680 {
681 	struct kvm *kvm = gp->l1_host;
682 
683 	if (gp->shadow_pgtable) {
684 		/*
685 		 * No vcpu is using this struct and no call to
686 		 * kvmhv_get_nested can find this struct,
687 		 * so we don't need to hold kvm->mmu_lock.
688 		 */
689 		kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
690 					  gp->shadow_lpid);
691 		pgd_free(kvm->mm, gp->shadow_pgtable);
692 	}
693 	kvmhv_set_ptbl_entry(gp->shadow_lpid, 0, 0);
694 	kvmppc_free_lpid(gp->shadow_lpid);
695 	kfree(gp);
696 }
697 
698 static void kvmhv_remove_nested(struct kvm_nested_guest *gp)
699 {
700 	struct kvm *kvm = gp->l1_host;
701 	int lpid = gp->l1_lpid;
702 	long ref;
703 
704 	spin_lock(&kvm->mmu_lock);
705 	if (gp == kvm->arch.nested_guests[lpid]) {
706 		kvm->arch.nested_guests[lpid] = NULL;
707 		if (lpid == kvm->arch.max_nested_lpid) {
708 			while (--lpid >= 0 && !kvm->arch.nested_guests[lpid])
709 				;
710 			kvm->arch.max_nested_lpid = lpid;
711 		}
712 		--gp->refcnt;
713 	}
714 	ref = gp->refcnt;
715 	spin_unlock(&kvm->mmu_lock);
716 	if (ref == 0)
717 		kvmhv_release_nested(gp);
718 }
719 
720 /*
721  * Free up all nested resources allocated for this guest.
722  * This is called with no vcpus of the guest running, when
723  * switching the guest to HPT mode or when destroying the
724  * guest.
725  */
726 void kvmhv_release_all_nested(struct kvm *kvm)
727 {
728 	int i;
729 	struct kvm_nested_guest *gp;
730 	struct kvm_nested_guest *freelist = NULL;
731 	struct kvm_memory_slot *memslot;
732 	int srcu_idx;
733 
734 	spin_lock(&kvm->mmu_lock);
735 	for (i = 0; i <= kvm->arch.max_nested_lpid; i++) {
736 		gp = kvm->arch.nested_guests[i];
737 		if (!gp)
738 			continue;
739 		kvm->arch.nested_guests[i] = NULL;
740 		if (--gp->refcnt == 0) {
741 			gp->next = freelist;
742 			freelist = gp;
743 		}
744 	}
745 	kvm->arch.max_nested_lpid = -1;
746 	spin_unlock(&kvm->mmu_lock);
747 	while ((gp = freelist) != NULL) {
748 		freelist = gp->next;
749 		kvmhv_release_nested(gp);
750 	}
751 
752 	srcu_idx = srcu_read_lock(&kvm->srcu);
753 	kvm_for_each_memslot(memslot, kvm_memslots(kvm))
754 		kvmhv_free_memslot_nest_rmap(memslot);
755 	srcu_read_unlock(&kvm->srcu, srcu_idx);
756 }
757 
758 /* caller must hold gp->tlb_lock */
759 static void kvmhv_flush_nested(struct kvm_nested_guest *gp)
760 {
761 	struct kvm *kvm = gp->l1_host;
762 
763 	spin_lock(&kvm->mmu_lock);
764 	kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable, gp->shadow_lpid);
765 	spin_unlock(&kvm->mmu_lock);
766 	kvmhv_flush_lpid(gp->shadow_lpid);
767 	kvmhv_update_ptbl_cache(gp);
768 	if (gp->l1_gr_to_hr == 0)
769 		kvmhv_remove_nested(gp);
770 }
771 
772 struct kvm_nested_guest *kvmhv_get_nested(struct kvm *kvm, int l1_lpid,
773 					  bool create)
774 {
775 	struct kvm_nested_guest *gp, *newgp;
776 
777 	if (l1_lpid >= KVM_MAX_NESTED_GUESTS ||
778 	    l1_lpid >= (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 12 - 4)))
779 		return NULL;
780 
781 	spin_lock(&kvm->mmu_lock);
782 	gp = kvm->arch.nested_guests[l1_lpid];
783 	if (gp)
784 		++gp->refcnt;
785 	spin_unlock(&kvm->mmu_lock);
786 
787 	if (gp || !create)
788 		return gp;
789 
790 	newgp = kvmhv_alloc_nested(kvm, l1_lpid);
791 	if (!newgp)
792 		return NULL;
793 	spin_lock(&kvm->mmu_lock);
794 	if (kvm->arch.nested_guests[l1_lpid]) {
795 		/* someone else beat us to it */
796 		gp = kvm->arch.nested_guests[l1_lpid];
797 	} else {
798 		kvm->arch.nested_guests[l1_lpid] = newgp;
799 		++newgp->refcnt;
800 		gp = newgp;
801 		newgp = NULL;
802 		if (l1_lpid > kvm->arch.max_nested_lpid)
803 			kvm->arch.max_nested_lpid = l1_lpid;
804 	}
805 	++gp->refcnt;
806 	spin_unlock(&kvm->mmu_lock);
807 
808 	if (newgp)
809 		kvmhv_release_nested(newgp);
810 
811 	return gp;
812 }
813 
814 void kvmhv_put_nested(struct kvm_nested_guest *gp)
815 {
816 	struct kvm *kvm = gp->l1_host;
817 	long ref;
818 
819 	spin_lock(&kvm->mmu_lock);
820 	ref = --gp->refcnt;
821 	spin_unlock(&kvm->mmu_lock);
822 	if (ref == 0)
823 		kvmhv_release_nested(gp);
824 }
825 
826 static struct kvm_nested_guest *kvmhv_find_nested(struct kvm *kvm, int lpid)
827 {
828 	if (lpid > kvm->arch.max_nested_lpid)
829 		return NULL;
830 	return kvm->arch.nested_guests[lpid];
831 }
832 
833 pte_t *find_kvm_nested_guest_pte(struct kvm *kvm, unsigned long lpid,
834 				 unsigned long ea, unsigned *hshift)
835 {
836 	struct kvm_nested_guest *gp;
837 	pte_t *pte;
838 
839 	gp = kvmhv_find_nested(kvm, lpid);
840 	if (!gp)
841 		return NULL;
842 
843 	VM_WARN(!spin_is_locked(&kvm->mmu_lock),
844 		"%s called with kvm mmu_lock not held \n", __func__);
845 	pte = __find_linux_pte(gp->shadow_pgtable, ea, NULL, hshift);
846 
847 	return pte;
848 }
849 
850 static inline bool kvmhv_n_rmap_is_equal(u64 rmap_1, u64 rmap_2)
851 {
852 	return !((rmap_1 ^ rmap_2) & (RMAP_NESTED_LPID_MASK |
853 				       RMAP_NESTED_GPA_MASK));
854 }
855 
856 void kvmhv_insert_nest_rmap(struct kvm *kvm, unsigned long *rmapp,
857 			    struct rmap_nested **n_rmap)
858 {
859 	struct llist_node *entry = ((struct llist_head *) rmapp)->first;
860 	struct rmap_nested *cursor;
861 	u64 rmap, new_rmap = (*n_rmap)->rmap;
862 
863 	/* Are there any existing entries? */
864 	if (!(*rmapp)) {
865 		/* No -> use the rmap as a single entry */
866 		*rmapp = new_rmap | RMAP_NESTED_IS_SINGLE_ENTRY;
867 		return;
868 	}
869 
870 	/* Do any entries match what we're trying to insert? */
871 	for_each_nest_rmap_safe(cursor, entry, &rmap) {
872 		if (kvmhv_n_rmap_is_equal(rmap, new_rmap))
873 			return;
874 	}
875 
876 	/* Do we need to create a list or just add the new entry? */
877 	rmap = *rmapp;
878 	if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
879 		*rmapp = 0UL;
880 	llist_add(&((*n_rmap)->list), (struct llist_head *) rmapp);
881 	if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
882 		(*n_rmap)->list.next = (struct llist_node *) rmap;
883 
884 	/* Set NULL so not freed by caller */
885 	*n_rmap = NULL;
886 }
887 
888 static void kvmhv_update_nest_rmap_rc(struct kvm *kvm, u64 n_rmap,
889 				      unsigned long clr, unsigned long set,
890 				      unsigned long hpa, unsigned long mask)
891 {
892 	unsigned long gpa;
893 	unsigned int shift, lpid;
894 	pte_t *ptep;
895 
896 	gpa = n_rmap & RMAP_NESTED_GPA_MASK;
897 	lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
898 
899 	/* Find the pte */
900 	ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
901 	/*
902 	 * If the pte is present and the pfn is still the same, update the pte.
903 	 * If the pfn has changed then this is a stale rmap entry, the nested
904 	 * gpa actually points somewhere else now, and there is nothing to do.
905 	 * XXX A future optimisation would be to remove the rmap entry here.
906 	 */
907 	if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa)) {
908 		__radix_pte_update(ptep, clr, set);
909 		kvmppc_radix_tlbie_page(kvm, gpa, shift, lpid);
910 	}
911 }
912 
913 /*
914  * For a given list of rmap entries, update the rc bits in all ptes in shadow
915  * page tables for nested guests which are referenced by the rmap list.
916  */
917 void kvmhv_update_nest_rmap_rc_list(struct kvm *kvm, unsigned long *rmapp,
918 				    unsigned long clr, unsigned long set,
919 				    unsigned long hpa, unsigned long nbytes)
920 {
921 	struct llist_node *entry = ((struct llist_head *) rmapp)->first;
922 	struct rmap_nested *cursor;
923 	unsigned long rmap, mask;
924 
925 	if ((clr | set) & ~(_PAGE_DIRTY | _PAGE_ACCESSED))
926 		return;
927 
928 	mask = PTE_RPN_MASK & ~(nbytes - 1);
929 	hpa &= mask;
930 
931 	for_each_nest_rmap_safe(cursor, entry, &rmap)
932 		kvmhv_update_nest_rmap_rc(kvm, rmap, clr, set, hpa, mask);
933 }
934 
935 static void kvmhv_remove_nest_rmap(struct kvm *kvm, u64 n_rmap,
936 				   unsigned long hpa, unsigned long mask)
937 {
938 	struct kvm_nested_guest *gp;
939 	unsigned long gpa;
940 	unsigned int shift, lpid;
941 	pte_t *ptep;
942 
943 	gpa = n_rmap & RMAP_NESTED_GPA_MASK;
944 	lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
945 	gp = kvmhv_find_nested(kvm, lpid);
946 	if (!gp)
947 		return;
948 
949 	/* Find and invalidate the pte */
950 	ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
951 	/* Don't spuriously invalidate ptes if the pfn has changed */
952 	if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa))
953 		kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
954 }
955 
956 static void kvmhv_remove_nest_rmap_list(struct kvm *kvm, unsigned long *rmapp,
957 					unsigned long hpa, unsigned long mask)
958 {
959 	struct llist_node *entry = llist_del_all((struct llist_head *) rmapp);
960 	struct rmap_nested *cursor;
961 	unsigned long rmap;
962 
963 	for_each_nest_rmap_safe(cursor, entry, &rmap) {
964 		kvmhv_remove_nest_rmap(kvm, rmap, hpa, mask);
965 		kfree(cursor);
966 	}
967 }
968 
969 /* called with kvm->mmu_lock held */
970 void kvmhv_remove_nest_rmap_range(struct kvm *kvm,
971 				  const struct kvm_memory_slot *memslot,
972 				  unsigned long gpa, unsigned long hpa,
973 				  unsigned long nbytes)
974 {
975 	unsigned long gfn, end_gfn;
976 	unsigned long addr_mask;
977 
978 	if (!memslot)
979 		return;
980 	gfn = (gpa >> PAGE_SHIFT) - memslot->base_gfn;
981 	end_gfn = gfn + (nbytes >> PAGE_SHIFT);
982 
983 	addr_mask = PTE_RPN_MASK & ~(nbytes - 1);
984 	hpa &= addr_mask;
985 
986 	for (; gfn < end_gfn; gfn++) {
987 		unsigned long *rmap = &memslot->arch.rmap[gfn];
988 		kvmhv_remove_nest_rmap_list(kvm, rmap, hpa, addr_mask);
989 	}
990 }
991 
992 static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free)
993 {
994 	unsigned long page;
995 
996 	for (page = 0; page < free->npages; page++) {
997 		unsigned long rmap, *rmapp = &free->arch.rmap[page];
998 		struct rmap_nested *cursor;
999 		struct llist_node *entry;
1000 
1001 		entry = llist_del_all((struct llist_head *) rmapp);
1002 		for_each_nest_rmap_safe(cursor, entry, &rmap)
1003 			kfree(cursor);
1004 	}
1005 }
1006 
1007 static bool kvmhv_invalidate_shadow_pte(struct kvm_vcpu *vcpu,
1008 					struct kvm_nested_guest *gp,
1009 					long gpa, int *shift_ret)
1010 {
1011 	struct kvm *kvm = vcpu->kvm;
1012 	bool ret = false;
1013 	pte_t *ptep;
1014 	int shift;
1015 
1016 	spin_lock(&kvm->mmu_lock);
1017 	ptep = find_kvm_nested_guest_pte(kvm, gp->l1_lpid, gpa, &shift);
1018 	if (!shift)
1019 		shift = PAGE_SHIFT;
1020 	if (ptep && pte_present(*ptep)) {
1021 		kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
1022 		ret = true;
1023 	}
1024 	spin_unlock(&kvm->mmu_lock);
1025 
1026 	if (shift_ret)
1027 		*shift_ret = shift;
1028 	return ret;
1029 }
1030 
1031 static inline int get_ric(unsigned int instr)
1032 {
1033 	return (instr >> 18) & 0x3;
1034 }
1035 
1036 static inline int get_prs(unsigned int instr)
1037 {
1038 	return (instr >> 17) & 0x1;
1039 }
1040 
1041 static inline int get_r(unsigned int instr)
1042 {
1043 	return (instr >> 16) & 0x1;
1044 }
1045 
1046 static inline int get_lpid(unsigned long r_val)
1047 {
1048 	return r_val & 0xffffffff;
1049 }
1050 
1051 static inline int get_is(unsigned long r_val)
1052 {
1053 	return (r_val >> 10) & 0x3;
1054 }
1055 
1056 static inline int get_ap(unsigned long r_val)
1057 {
1058 	return (r_val >> 5) & 0x7;
1059 }
1060 
1061 static inline long get_epn(unsigned long r_val)
1062 {
1063 	return r_val >> 12;
1064 }
1065 
1066 static int kvmhv_emulate_tlbie_tlb_addr(struct kvm_vcpu *vcpu, int lpid,
1067 					int ap, long epn)
1068 {
1069 	struct kvm *kvm = vcpu->kvm;
1070 	struct kvm_nested_guest *gp;
1071 	long npages;
1072 	int shift, shadow_shift;
1073 	unsigned long addr;
1074 
1075 	shift = ap_to_shift(ap);
1076 	addr = epn << 12;
1077 	if (shift < 0)
1078 		/* Invalid ap encoding */
1079 		return -EINVAL;
1080 
1081 	addr &= ~((1UL << shift) - 1);
1082 	npages = 1UL << (shift - PAGE_SHIFT);
1083 
1084 	gp = kvmhv_get_nested(kvm, lpid, false);
1085 	if (!gp) /* No such guest -> nothing to do */
1086 		return 0;
1087 	mutex_lock(&gp->tlb_lock);
1088 
1089 	/* There may be more than one host page backing this single guest pte */
1090 	do {
1091 		kvmhv_invalidate_shadow_pte(vcpu, gp, addr, &shadow_shift);
1092 
1093 		npages -= 1UL << (shadow_shift - PAGE_SHIFT);
1094 		addr += 1UL << shadow_shift;
1095 	} while (npages > 0);
1096 
1097 	mutex_unlock(&gp->tlb_lock);
1098 	kvmhv_put_nested(gp);
1099 	return 0;
1100 }
1101 
1102 static void kvmhv_emulate_tlbie_lpid(struct kvm_vcpu *vcpu,
1103 				     struct kvm_nested_guest *gp, int ric)
1104 {
1105 	struct kvm *kvm = vcpu->kvm;
1106 
1107 	mutex_lock(&gp->tlb_lock);
1108 	switch (ric) {
1109 	case 0:
1110 		/* Invalidate TLB */
1111 		spin_lock(&kvm->mmu_lock);
1112 		kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
1113 					  gp->shadow_lpid);
1114 		kvmhv_flush_lpid(gp->shadow_lpid);
1115 		spin_unlock(&kvm->mmu_lock);
1116 		break;
1117 	case 1:
1118 		/*
1119 		 * Invalidate PWC
1120 		 * We don't cache this -> nothing to do
1121 		 */
1122 		break;
1123 	case 2:
1124 		/* Invalidate TLB, PWC and caching of partition table entries */
1125 		kvmhv_flush_nested(gp);
1126 		break;
1127 	default:
1128 		break;
1129 	}
1130 	mutex_unlock(&gp->tlb_lock);
1131 }
1132 
1133 static void kvmhv_emulate_tlbie_all_lpid(struct kvm_vcpu *vcpu, int ric)
1134 {
1135 	struct kvm *kvm = vcpu->kvm;
1136 	struct kvm_nested_guest *gp;
1137 	int i;
1138 
1139 	spin_lock(&kvm->mmu_lock);
1140 	for (i = 0; i <= kvm->arch.max_nested_lpid; i++) {
1141 		gp = kvm->arch.nested_guests[i];
1142 		if (gp) {
1143 			spin_unlock(&kvm->mmu_lock);
1144 			kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1145 			spin_lock(&kvm->mmu_lock);
1146 		}
1147 	}
1148 	spin_unlock(&kvm->mmu_lock);
1149 }
1150 
1151 static int kvmhv_emulate_priv_tlbie(struct kvm_vcpu *vcpu, unsigned int instr,
1152 				    unsigned long rsval, unsigned long rbval)
1153 {
1154 	struct kvm *kvm = vcpu->kvm;
1155 	struct kvm_nested_guest *gp;
1156 	int r, ric, prs, is, ap;
1157 	int lpid;
1158 	long epn;
1159 	int ret = 0;
1160 
1161 	ric = get_ric(instr);
1162 	prs = get_prs(instr);
1163 	r = get_r(instr);
1164 	lpid = get_lpid(rsval);
1165 	is = get_is(rbval);
1166 
1167 	/*
1168 	 * These cases are invalid and are not handled:
1169 	 * r   != 1 -> Only radix supported
1170 	 * prs == 1 -> Not HV privileged
1171 	 * ric == 3 -> No cluster bombs for radix
1172 	 * is  == 1 -> Partition scoped translations not associated with pid
1173 	 * (!is) && (ric == 1 || ric == 2) -> Not supported by ISA
1174 	 */
1175 	if ((!r) || (prs) || (ric == 3) || (is == 1) ||
1176 	    ((!is) && (ric == 1 || ric == 2)))
1177 		return -EINVAL;
1178 
1179 	switch (is) {
1180 	case 0:
1181 		/*
1182 		 * We know ric == 0
1183 		 * Invalidate TLB for a given target address
1184 		 */
1185 		epn = get_epn(rbval);
1186 		ap = get_ap(rbval);
1187 		ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap, epn);
1188 		break;
1189 	case 2:
1190 		/* Invalidate matching LPID */
1191 		gp = kvmhv_get_nested(kvm, lpid, false);
1192 		if (gp) {
1193 			kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1194 			kvmhv_put_nested(gp);
1195 		}
1196 		break;
1197 	case 3:
1198 		/* Invalidate ALL LPIDs */
1199 		kvmhv_emulate_tlbie_all_lpid(vcpu, ric);
1200 		break;
1201 	default:
1202 		ret = -EINVAL;
1203 		break;
1204 	}
1205 
1206 	return ret;
1207 }
1208 
1209 /*
1210  * This handles the H_TLB_INVALIDATE hcall.
1211  * Parameters are (r4) tlbie instruction code, (r5) rS contents,
1212  * (r6) rB contents.
1213  */
1214 long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu)
1215 {
1216 	int ret;
1217 
1218 	ret = kvmhv_emulate_priv_tlbie(vcpu, kvmppc_get_gpr(vcpu, 4),
1219 			kvmppc_get_gpr(vcpu, 5), kvmppc_get_gpr(vcpu, 6));
1220 	if (ret)
1221 		return H_PARAMETER;
1222 	return H_SUCCESS;
1223 }
1224 
1225 static long do_tlb_invalidate_nested_all(struct kvm_vcpu *vcpu,
1226 					 unsigned long lpid, unsigned long ric)
1227 {
1228 	struct kvm *kvm = vcpu->kvm;
1229 	struct kvm_nested_guest *gp;
1230 
1231 	gp = kvmhv_get_nested(kvm, lpid, false);
1232 	if (gp) {
1233 		kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
1234 		kvmhv_put_nested(gp);
1235 	}
1236 	return H_SUCCESS;
1237 }
1238 
1239 /*
1240  * Number of pages above which we invalidate the entire LPID rather than
1241  * flush individual pages.
1242  */
1243 static unsigned long tlb_range_flush_page_ceiling __read_mostly = 33;
1244 
1245 static long do_tlb_invalidate_nested_tlb(struct kvm_vcpu *vcpu,
1246 					 unsigned long lpid,
1247 					 unsigned long pg_sizes,
1248 					 unsigned long start,
1249 					 unsigned long end)
1250 {
1251 	int ret = H_P4;
1252 	unsigned long addr, nr_pages;
1253 	struct mmu_psize_def *def;
1254 	unsigned long psize, ap, page_size;
1255 	bool flush_lpid;
1256 
1257 	for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
1258 		def = &mmu_psize_defs[psize];
1259 		if (!(pg_sizes & def->h_rpt_pgsize))
1260 			continue;
1261 
1262 		nr_pages = (end - start) >> def->shift;
1263 		flush_lpid = nr_pages > tlb_range_flush_page_ceiling;
1264 		if (flush_lpid)
1265 			return do_tlb_invalidate_nested_all(vcpu, lpid,
1266 							RIC_FLUSH_TLB);
1267 		addr = start;
1268 		ap = mmu_get_ap(psize);
1269 		page_size = 1UL << def->shift;
1270 		do {
1271 			ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap,
1272 						   get_epn(addr));
1273 			if (ret)
1274 				return H_P4;
1275 			addr += page_size;
1276 		} while (addr < end);
1277 	}
1278 	return ret;
1279 }
1280 
1281 /*
1282  * Performs partition-scoped invalidations for nested guests
1283  * as part of H_RPT_INVALIDATE hcall.
1284  */
1285 long do_h_rpt_invalidate_pat(struct kvm_vcpu *vcpu, unsigned long lpid,
1286 			     unsigned long type, unsigned long pg_sizes,
1287 			     unsigned long start, unsigned long end)
1288 {
1289 	/*
1290 	 * If L2 lpid isn't valid, we need to return H_PARAMETER.
1291 	 *
1292 	 * However, nested KVM issues a L2 lpid flush call when creating
1293 	 * partition table entries for L2. This happens even before the
1294 	 * corresponding shadow lpid is created in HV which happens in
1295 	 * H_ENTER_NESTED call. Since we can't differentiate this case from
1296 	 * the invalid case, we ignore such flush requests and return success.
1297 	 */
1298 	if (!kvmhv_find_nested(vcpu->kvm, lpid))
1299 		return H_SUCCESS;
1300 
1301 	/*
1302 	 * A flush all request can be handled by a full lpid flush only.
1303 	 */
1304 	if ((type & H_RPTI_TYPE_NESTED_ALL) == H_RPTI_TYPE_NESTED_ALL)
1305 		return do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_ALL);
1306 
1307 	/*
1308 	 * We don't need to handle a PWC flush like process table here,
1309 	 * because intermediate partition scoped table in nested guest doesn't
1310 	 * really have PWC. Only level we have PWC is in L0 and for nested
1311 	 * invalidate at L0 we always do kvm_flush_lpid() which does
1312 	 * radix__flush_all_lpid(). For range invalidate at any level, we
1313 	 * are not removing the higher level page tables and hence there is
1314 	 * no PWC invalidate needed.
1315 	 *
1316 	 * if (type & H_RPTI_TYPE_PWC) {
1317 	 *	ret = do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_PWC);
1318 	 *	if (ret)
1319 	 *		return H_P4;
1320 	 * }
1321 	 */
1322 
1323 	if (start == 0 && end == -1)
1324 		return do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_TLB);
1325 
1326 	if (type & H_RPTI_TYPE_TLB)
1327 		return do_tlb_invalidate_nested_tlb(vcpu, lpid, pg_sizes,
1328 						    start, end);
1329 	return H_SUCCESS;
1330 }
1331 
1332 /* Used to convert a nested guest real address to a L1 guest real address */
1333 static int kvmhv_translate_addr_nested(struct kvm_vcpu *vcpu,
1334 				       struct kvm_nested_guest *gp,
1335 				       unsigned long n_gpa, unsigned long dsisr,
1336 				       struct kvmppc_pte *gpte_p)
1337 {
1338 	u64 fault_addr, flags = dsisr & DSISR_ISSTORE;
1339 	int ret;
1340 
1341 	ret = kvmppc_mmu_walk_radix_tree(vcpu, n_gpa, gpte_p, gp->l1_gr_to_hr,
1342 					 &fault_addr);
1343 
1344 	if (ret) {
1345 		/* We didn't find a pte */
1346 		if (ret == -EINVAL) {
1347 			/* Unsupported mmu config */
1348 			flags |= DSISR_UNSUPP_MMU;
1349 		} else if (ret == -ENOENT) {
1350 			/* No translation found */
1351 			flags |= DSISR_NOHPTE;
1352 		} else if (ret == -EFAULT) {
1353 			/* Couldn't access L1 real address */
1354 			flags |= DSISR_PRTABLE_FAULT;
1355 			vcpu->arch.fault_gpa = fault_addr;
1356 		} else {
1357 			/* Unknown error */
1358 			return ret;
1359 		}
1360 		goto forward_to_l1;
1361 	} else {
1362 		/* We found a pte -> check permissions */
1363 		if (dsisr & DSISR_ISSTORE) {
1364 			/* Can we write? */
1365 			if (!gpte_p->may_write) {
1366 				flags |= DSISR_PROTFAULT;
1367 				goto forward_to_l1;
1368 			}
1369 		} else if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
1370 			/* Can we execute? */
1371 			if (!gpte_p->may_execute) {
1372 				flags |= SRR1_ISI_N_G_OR_CIP;
1373 				goto forward_to_l1;
1374 			}
1375 		} else {
1376 			/* Can we read? */
1377 			if (!gpte_p->may_read && !gpte_p->may_write) {
1378 				flags |= DSISR_PROTFAULT;
1379 				goto forward_to_l1;
1380 			}
1381 		}
1382 	}
1383 
1384 	return 0;
1385 
1386 forward_to_l1:
1387 	vcpu->arch.fault_dsisr = flags;
1388 	if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
1389 		vcpu->arch.shregs.msr &= SRR1_MSR_BITS;
1390 		vcpu->arch.shregs.msr |= flags;
1391 	}
1392 	return RESUME_HOST;
1393 }
1394 
1395 static long kvmhv_handle_nested_set_rc(struct kvm_vcpu *vcpu,
1396 				       struct kvm_nested_guest *gp,
1397 				       unsigned long n_gpa,
1398 				       struct kvmppc_pte gpte,
1399 				       unsigned long dsisr)
1400 {
1401 	struct kvm *kvm = vcpu->kvm;
1402 	bool writing = !!(dsisr & DSISR_ISSTORE);
1403 	u64 pgflags;
1404 	long ret;
1405 
1406 	/* Are the rc bits set in the L1 partition scoped pte? */
1407 	pgflags = _PAGE_ACCESSED;
1408 	if (writing)
1409 		pgflags |= _PAGE_DIRTY;
1410 	if (pgflags & ~gpte.rc)
1411 		return RESUME_HOST;
1412 
1413 	spin_lock(&kvm->mmu_lock);
1414 	/* Set the rc bit in the pte of our (L0) pgtable for the L1 guest */
1415 	ret = kvmppc_hv_handle_set_rc(kvm, false, writing,
1416 				      gpte.raddr, kvm->arch.lpid);
1417 	if (!ret) {
1418 		ret = -EINVAL;
1419 		goto out_unlock;
1420 	}
1421 
1422 	/* Set the rc bit in the pte of the shadow_pgtable for the nest guest */
1423 	ret = kvmppc_hv_handle_set_rc(kvm, true, writing,
1424 				      n_gpa, gp->l1_lpid);
1425 	if (!ret)
1426 		ret = -EINVAL;
1427 	else
1428 		ret = 0;
1429 
1430 out_unlock:
1431 	spin_unlock(&kvm->mmu_lock);
1432 	return ret;
1433 }
1434 
1435 static inline int kvmppc_radix_level_to_shift(int level)
1436 {
1437 	switch (level) {
1438 	case 2:
1439 		return PUD_SHIFT;
1440 	case 1:
1441 		return PMD_SHIFT;
1442 	default:
1443 		return PAGE_SHIFT;
1444 	}
1445 }
1446 
1447 static inline int kvmppc_radix_shift_to_level(int shift)
1448 {
1449 	if (shift == PUD_SHIFT)
1450 		return 2;
1451 	if (shift == PMD_SHIFT)
1452 		return 1;
1453 	if (shift == PAGE_SHIFT)
1454 		return 0;
1455 	WARN_ON_ONCE(1);
1456 	return 0;
1457 }
1458 
1459 /* called with gp->tlb_lock held */
1460 static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu,
1461 					  struct kvm_nested_guest *gp)
1462 {
1463 	struct kvm *kvm = vcpu->kvm;
1464 	struct kvm_memory_slot *memslot;
1465 	struct rmap_nested *n_rmap;
1466 	struct kvmppc_pte gpte;
1467 	pte_t pte, *pte_p;
1468 	unsigned long mmu_seq;
1469 	unsigned long dsisr = vcpu->arch.fault_dsisr;
1470 	unsigned long ea = vcpu->arch.fault_dar;
1471 	unsigned long *rmapp;
1472 	unsigned long n_gpa, gpa, gfn, perm = 0UL;
1473 	unsigned int shift, l1_shift, level;
1474 	bool writing = !!(dsisr & DSISR_ISSTORE);
1475 	bool kvm_ro = false;
1476 	long int ret;
1477 
1478 	if (!gp->l1_gr_to_hr) {
1479 		kvmhv_update_ptbl_cache(gp);
1480 		if (!gp->l1_gr_to_hr)
1481 			return RESUME_HOST;
1482 	}
1483 
1484 	/* Convert the nested guest real address into a L1 guest real address */
1485 
1486 	n_gpa = vcpu->arch.fault_gpa & ~0xF000000000000FFFULL;
1487 	if (!(dsisr & DSISR_PRTABLE_FAULT))
1488 		n_gpa |= ea & 0xFFF;
1489 	ret = kvmhv_translate_addr_nested(vcpu, gp, n_gpa, dsisr, &gpte);
1490 
1491 	/*
1492 	 * If the hardware found a translation but we don't now have a usable
1493 	 * translation in the l1 partition-scoped tree, remove the shadow pte
1494 	 * and let the guest retry.
1495 	 */
1496 	if (ret == RESUME_HOST &&
1497 	    (dsisr & (DSISR_PROTFAULT | DSISR_BADACCESS | DSISR_NOEXEC_OR_G |
1498 		      DSISR_BAD_COPYPASTE)))
1499 		goto inval;
1500 	if (ret)
1501 		return ret;
1502 
1503 	/* Failed to set the reference/change bits */
1504 	if (dsisr & DSISR_SET_RC) {
1505 		ret = kvmhv_handle_nested_set_rc(vcpu, gp, n_gpa, gpte, dsisr);
1506 		if (ret == RESUME_HOST)
1507 			return ret;
1508 		if (ret)
1509 			goto inval;
1510 		dsisr &= ~DSISR_SET_RC;
1511 		if (!(dsisr & (DSISR_BAD_FAULT_64S | DSISR_NOHPTE |
1512 			       DSISR_PROTFAULT)))
1513 			return RESUME_GUEST;
1514 	}
1515 
1516 	/*
1517 	 * We took an HISI or HDSI while we were running a nested guest which
1518 	 * means we have no partition scoped translation for that. This means
1519 	 * we need to insert a pte for the mapping into our shadow_pgtable.
1520 	 */
1521 
1522 	l1_shift = gpte.page_shift;
1523 	if (l1_shift < PAGE_SHIFT) {
1524 		/* We don't support l1 using a page size smaller than our own */
1525 		pr_err("KVM: L1 guest page shift (%d) less than our own (%d)\n",
1526 			l1_shift, PAGE_SHIFT);
1527 		return -EINVAL;
1528 	}
1529 	gpa = gpte.raddr;
1530 	gfn = gpa >> PAGE_SHIFT;
1531 
1532 	/* 1. Get the corresponding host memslot */
1533 
1534 	memslot = gfn_to_memslot(kvm, gfn);
1535 	if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
1536 		if (dsisr & (DSISR_PRTABLE_FAULT | DSISR_BADACCESS)) {
1537 			/* unusual error -> reflect to the guest as a DSI */
1538 			kvmppc_core_queue_data_storage(vcpu, ea, dsisr);
1539 			return RESUME_GUEST;
1540 		}
1541 
1542 		/* passthrough of emulated MMIO case */
1543 		return kvmppc_hv_emulate_mmio(vcpu, gpa, ea, writing);
1544 	}
1545 	if (memslot->flags & KVM_MEM_READONLY) {
1546 		if (writing) {
1547 			/* Give the guest a DSI */
1548 			kvmppc_core_queue_data_storage(vcpu, ea,
1549 					DSISR_ISSTORE | DSISR_PROTFAULT);
1550 			return RESUME_GUEST;
1551 		}
1552 		kvm_ro = true;
1553 	}
1554 
1555 	/* 2. Find the host pte for this L1 guest real address */
1556 
1557 	/* Used to check for invalidations in progress */
1558 	mmu_seq = kvm->mmu_notifier_seq;
1559 	smp_rmb();
1560 
1561 	/* See if can find translation in our partition scoped tables for L1 */
1562 	pte = __pte(0);
1563 	spin_lock(&kvm->mmu_lock);
1564 	pte_p = find_kvm_secondary_pte(kvm, gpa, &shift);
1565 	if (!shift)
1566 		shift = PAGE_SHIFT;
1567 	if (pte_p)
1568 		pte = *pte_p;
1569 	spin_unlock(&kvm->mmu_lock);
1570 
1571 	if (!pte_present(pte) || (writing && !(pte_val(pte) & _PAGE_WRITE))) {
1572 		/* No suitable pte found -> try to insert a mapping */
1573 		ret = kvmppc_book3s_instantiate_page(vcpu, gpa, memslot,
1574 					writing, kvm_ro, &pte, &level);
1575 		if (ret == -EAGAIN)
1576 			return RESUME_GUEST;
1577 		else if (ret)
1578 			return ret;
1579 		shift = kvmppc_radix_level_to_shift(level);
1580 	}
1581 	/* Align gfn to the start of the page */
1582 	gfn = (gpa & ~((1UL << shift) - 1)) >> PAGE_SHIFT;
1583 
1584 	/* 3. Compute the pte we need to insert for nest_gpa -> host r_addr */
1585 
1586 	/* The permissions is the combination of the host and l1 guest ptes */
1587 	perm |= gpte.may_read ? 0UL : _PAGE_READ;
1588 	perm |= gpte.may_write ? 0UL : _PAGE_WRITE;
1589 	perm |= gpte.may_execute ? 0UL : _PAGE_EXEC;
1590 	/* Only set accessed/dirty (rc) bits if set in host and l1 guest ptes */
1591 	perm |= (gpte.rc & _PAGE_ACCESSED) ? 0UL : _PAGE_ACCESSED;
1592 	perm |= ((gpte.rc & _PAGE_DIRTY) && writing) ? 0UL : _PAGE_DIRTY;
1593 	pte = __pte(pte_val(pte) & ~perm);
1594 
1595 	/* What size pte can we insert? */
1596 	if (shift > l1_shift) {
1597 		u64 mask;
1598 		unsigned int actual_shift = PAGE_SHIFT;
1599 		if (PMD_SHIFT < l1_shift)
1600 			actual_shift = PMD_SHIFT;
1601 		mask = (1UL << shift) - (1UL << actual_shift);
1602 		pte = __pte(pte_val(pte) | (gpa & mask));
1603 		shift = actual_shift;
1604 	}
1605 	level = kvmppc_radix_shift_to_level(shift);
1606 	n_gpa &= ~((1UL << shift) - 1);
1607 
1608 	/* 4. Insert the pte into our shadow_pgtable */
1609 
1610 	n_rmap = kzalloc(sizeof(*n_rmap), GFP_KERNEL);
1611 	if (!n_rmap)
1612 		return RESUME_GUEST; /* Let the guest try again */
1613 	n_rmap->rmap = (n_gpa & RMAP_NESTED_GPA_MASK) |
1614 		(((unsigned long) gp->l1_lpid) << RMAP_NESTED_LPID_SHIFT);
1615 	rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
1616 	ret = kvmppc_create_pte(kvm, gp->shadow_pgtable, pte, n_gpa, level,
1617 				mmu_seq, gp->shadow_lpid, rmapp, &n_rmap);
1618 	kfree(n_rmap);
1619 	if (ret == -EAGAIN)
1620 		ret = RESUME_GUEST;	/* Let the guest try again */
1621 
1622 	return ret;
1623 
1624  inval:
1625 	kvmhv_invalidate_shadow_pte(vcpu, gp, n_gpa, NULL);
1626 	return RESUME_GUEST;
1627 }
1628 
1629 long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu)
1630 {
1631 	struct kvm_nested_guest *gp = vcpu->arch.nested;
1632 	long int ret;
1633 
1634 	mutex_lock(&gp->tlb_lock);
1635 	ret = __kvmhv_nested_page_fault(vcpu, gp);
1636 	mutex_unlock(&gp->tlb_lock);
1637 	return ret;
1638 }
1639 
1640 int kvmhv_nested_next_lpid(struct kvm *kvm, int lpid)
1641 {
1642 	int ret = -1;
1643 
1644 	spin_lock(&kvm->mmu_lock);
1645 	while (++lpid <= kvm->arch.max_nested_lpid) {
1646 		if (kvm->arch.nested_guests[lpid]) {
1647 			ret = lpid;
1648 			break;
1649 		}
1650 	}
1651 	spin_unlock(&kvm->mmu_lock);
1652 	return ret;
1653 }
1654