xref: /linux/arch/s390/kvm/vsie.c (revision 84b9b44b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * kvm nested virtualization support for s390x
4  *
5  * Copyright IBM Corp. 2016, 2018
6  *
7  *    Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
8  */
9 #include <linux/vmalloc.h>
10 #include <linux/kvm_host.h>
11 #include <linux/bug.h>
12 #include <linux/list.h>
13 #include <linux/bitmap.h>
14 #include <linux/sched/signal.h>
15 
16 #include <asm/gmap.h>
17 #include <asm/mmu_context.h>
18 #include <asm/sclp.h>
19 #include <asm/nmi.h>
20 #include <asm/dis.h>
21 #include <asm/fpu/api.h>
22 #include "kvm-s390.h"
23 #include "gaccess.h"
24 
25 struct vsie_page {
26 	struct kvm_s390_sie_block scb_s;	/* 0x0000 */
27 	/*
28 	 * the backup info for machine check. ensure it's at
29 	 * the same offset as that in struct sie_page!
30 	 */
31 	struct mcck_volatile_info mcck_info;    /* 0x0200 */
32 	/*
33 	 * The pinned original scb. Be aware that other VCPUs can modify
34 	 * it while we read from it. Values that are used for conditions or
35 	 * are reused conditionally, should be accessed via READ_ONCE.
36 	 */
37 	struct kvm_s390_sie_block *scb_o;	/* 0x0218 */
38 	/* the shadow gmap in use by the vsie_page */
39 	struct gmap *gmap;			/* 0x0220 */
40 	/* address of the last reported fault to guest2 */
41 	unsigned long fault_addr;		/* 0x0228 */
42 	/* calculated guest addresses of satellite control blocks */
43 	gpa_t sca_gpa;				/* 0x0230 */
44 	gpa_t itdba_gpa;			/* 0x0238 */
45 	gpa_t gvrd_gpa;				/* 0x0240 */
46 	gpa_t riccbd_gpa;			/* 0x0248 */
47 	gpa_t sdnx_gpa;				/* 0x0250 */
48 	__u8 reserved[0x0700 - 0x0258];		/* 0x0258 */
49 	struct kvm_s390_crypto_cb crycb;	/* 0x0700 */
50 	__u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE];	/* 0x0800 */
51 };
52 
53 /* trigger a validity icpt for the given scb */
54 static int set_validity_icpt(struct kvm_s390_sie_block *scb,
55 			     __u16 reason_code)
56 {
57 	scb->ipa = 0x1000;
58 	scb->ipb = ((__u32) reason_code) << 16;
59 	scb->icptcode = ICPT_VALIDITY;
60 	return 1;
61 }
62 
63 /* mark the prefix as unmapped, this will block the VSIE */
64 static void prefix_unmapped(struct vsie_page *vsie_page)
65 {
66 	atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
67 }
68 
69 /* mark the prefix as unmapped and wait until the VSIE has been left */
70 static void prefix_unmapped_sync(struct vsie_page *vsie_page)
71 {
72 	prefix_unmapped(vsie_page);
73 	if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
74 		atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
75 	while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
76 		cpu_relax();
77 }
78 
79 /* mark the prefix as mapped, this will allow the VSIE to run */
80 static void prefix_mapped(struct vsie_page *vsie_page)
81 {
82 	atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
83 }
84 
85 /* test if the prefix is mapped into the gmap shadow */
86 static int prefix_is_mapped(struct vsie_page *vsie_page)
87 {
88 	return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
89 }
90 
91 /* copy the updated intervention request bits into the shadow scb */
92 static void update_intervention_requests(struct vsie_page *vsie_page)
93 {
94 	const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
95 	int cpuflags;
96 
97 	cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
98 	atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
99 	atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
100 }
101 
102 /* shadow (filter and validate) the cpuflags  */
103 static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
104 {
105 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
106 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
107 	int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
108 
109 	/* we don't allow ESA/390 guests */
110 	if (!(cpuflags & CPUSTAT_ZARCH))
111 		return set_validity_icpt(scb_s, 0x0001U);
112 
113 	if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
114 		return set_validity_icpt(scb_s, 0x0001U);
115 	else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
116 		return set_validity_icpt(scb_s, 0x0007U);
117 
118 	/* intervention requests will be set later */
119 	newflags = CPUSTAT_ZARCH;
120 	if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8))
121 		newflags |= CPUSTAT_GED;
122 	if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) {
123 		if (cpuflags & CPUSTAT_GED)
124 			return set_validity_icpt(scb_s, 0x0001U);
125 		newflags |= CPUSTAT_GED2;
126 	}
127 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE))
128 		newflags |= cpuflags & CPUSTAT_P;
129 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS))
130 		newflags |= cpuflags & CPUSTAT_SM;
131 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS))
132 		newflags |= cpuflags & CPUSTAT_IBS;
133 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_KSS))
134 		newflags |= cpuflags & CPUSTAT_KSS;
135 
136 	atomic_set(&scb_s->cpuflags, newflags);
137 	return 0;
138 }
139 /* Copy to APCB FORMAT1 from APCB FORMAT0 */
140 static int setup_apcb10(struct kvm_vcpu *vcpu, struct kvm_s390_apcb1 *apcb_s,
141 			unsigned long crycb_gpa, struct kvm_s390_apcb1 *apcb_h)
142 {
143 	struct kvm_s390_apcb0 tmp;
144 	unsigned long apcb_gpa;
145 
146 	apcb_gpa = crycb_gpa + offsetof(struct kvm_s390_crypto_cb, apcb0);
147 
148 	if (read_guest_real(vcpu, apcb_gpa, &tmp,
149 			    sizeof(struct kvm_s390_apcb0)))
150 		return -EFAULT;
151 
152 	apcb_s->apm[0] = apcb_h->apm[0] & tmp.apm[0];
153 	apcb_s->aqm[0] = apcb_h->aqm[0] & tmp.aqm[0] & 0xffff000000000000UL;
154 	apcb_s->adm[0] = apcb_h->adm[0] & tmp.adm[0] & 0xffff000000000000UL;
155 
156 	return 0;
157 
158 }
159 
160 /**
161  * setup_apcb00 - Copy to APCB FORMAT0 from APCB FORMAT0
162  * @vcpu: pointer to the virtual CPU
163  * @apcb_s: pointer to start of apcb in the shadow crycb
164  * @crycb_gpa: guest physical address to start of original guest crycb
165  * @apcb_h: pointer to start of apcb in the guest1
166  *
167  * Returns 0 and -EFAULT on error reading guest apcb
168  */
169 static int setup_apcb00(struct kvm_vcpu *vcpu, unsigned long *apcb_s,
170 			unsigned long crycb_gpa, unsigned long *apcb_h)
171 {
172 	unsigned long apcb_gpa;
173 
174 	apcb_gpa = crycb_gpa + offsetof(struct kvm_s390_crypto_cb, apcb0);
175 
176 	if (read_guest_real(vcpu, apcb_gpa, apcb_s,
177 			    sizeof(struct kvm_s390_apcb0)))
178 		return -EFAULT;
179 
180 	bitmap_and(apcb_s, apcb_s, apcb_h, sizeof(struct kvm_s390_apcb0));
181 
182 	return 0;
183 }
184 
185 /**
186  * setup_apcb11 - Copy the FORMAT1 APCB from the guest to the shadow CRYCB
187  * @vcpu: pointer to the virtual CPU
188  * @apcb_s: pointer to start of apcb in the shadow crycb
189  * @crycb_gpa: guest physical address to start of original guest crycb
190  * @apcb_h: pointer to start of apcb in the host
191  *
192  * Returns 0 and -EFAULT on error reading guest apcb
193  */
194 static int setup_apcb11(struct kvm_vcpu *vcpu, unsigned long *apcb_s,
195 			unsigned long crycb_gpa,
196 			unsigned long *apcb_h)
197 {
198 	unsigned long apcb_gpa;
199 
200 	apcb_gpa = crycb_gpa + offsetof(struct kvm_s390_crypto_cb, apcb1);
201 
202 	if (read_guest_real(vcpu, apcb_gpa, apcb_s,
203 			    sizeof(struct kvm_s390_apcb1)))
204 		return -EFAULT;
205 
206 	bitmap_and(apcb_s, apcb_s, apcb_h, sizeof(struct kvm_s390_apcb1));
207 
208 	return 0;
209 }
210 
211 /**
212  * setup_apcb - Create a shadow copy of the apcb.
213  * @vcpu: pointer to the virtual CPU
214  * @crycb_s: pointer to shadow crycb
215  * @crycb_gpa: guest physical address of original guest crycb
216  * @crycb_h: pointer to the host crycb
217  * @fmt_o: format of the original guest crycb.
218  * @fmt_h: format of the host crycb.
219  *
220  * Checks the compatibility between the guest and host crycb and calls the
221  * appropriate copy function.
222  *
223  * Return 0 or an error number if the guest and host crycb are incompatible.
224  */
225 static int setup_apcb(struct kvm_vcpu *vcpu, struct kvm_s390_crypto_cb *crycb_s,
226 	       const u32 crycb_gpa,
227 	       struct kvm_s390_crypto_cb *crycb_h,
228 	       int fmt_o, int fmt_h)
229 {
230 	switch (fmt_o) {
231 	case CRYCB_FORMAT2:
232 		if ((crycb_gpa & PAGE_MASK) != ((crycb_gpa + 256) & PAGE_MASK))
233 			return -EACCES;
234 		if (fmt_h != CRYCB_FORMAT2)
235 			return -EINVAL;
236 		return setup_apcb11(vcpu, (unsigned long *)&crycb_s->apcb1,
237 				    crycb_gpa,
238 				    (unsigned long *)&crycb_h->apcb1);
239 	case CRYCB_FORMAT1:
240 		switch (fmt_h) {
241 		case CRYCB_FORMAT2:
242 			return setup_apcb10(vcpu, &crycb_s->apcb1,
243 					    crycb_gpa,
244 					    &crycb_h->apcb1);
245 		case CRYCB_FORMAT1:
246 			return setup_apcb00(vcpu,
247 					    (unsigned long *) &crycb_s->apcb0,
248 					    crycb_gpa,
249 					    (unsigned long *) &crycb_h->apcb0);
250 		}
251 		break;
252 	case CRYCB_FORMAT0:
253 		if ((crycb_gpa & PAGE_MASK) != ((crycb_gpa + 32) & PAGE_MASK))
254 			return -EACCES;
255 
256 		switch (fmt_h) {
257 		case CRYCB_FORMAT2:
258 			return setup_apcb10(vcpu, &crycb_s->apcb1,
259 					    crycb_gpa,
260 					    &crycb_h->apcb1);
261 		case CRYCB_FORMAT1:
262 		case CRYCB_FORMAT0:
263 			return setup_apcb00(vcpu,
264 					    (unsigned long *) &crycb_s->apcb0,
265 					    crycb_gpa,
266 					    (unsigned long *) &crycb_h->apcb0);
267 		}
268 	}
269 	return -EINVAL;
270 }
271 
272 /**
273  * shadow_crycb - Create a shadow copy of the crycb block
274  * @vcpu: a pointer to the virtual CPU
275  * @vsie_page: a pointer to internal date used for the vSIE
276  *
277  * Create a shadow copy of the crycb block and setup key wrapping, if
278  * requested for guest 3 and enabled for guest 2.
279  *
280  * We accept format-1 or format-2, but we convert format-1 into format-2
281  * in the shadow CRYCB.
282  * Using format-2 enables the firmware to choose the right format when
283  * scheduling the SIE.
284  * There is nothing to do for format-0.
285  *
286  * This function centralize the issuing of set_validity_icpt() for all
287  * the subfunctions working on the crycb.
288  *
289  * Returns: - 0 if shadowed or nothing to do
290  *          - > 0 if control has to be given to guest 2
291  */
292 static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
293 {
294 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
295 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
296 	const uint32_t crycbd_o = READ_ONCE(scb_o->crycbd);
297 	const u32 crycb_addr = crycbd_o & 0x7ffffff8U;
298 	unsigned long *b1, *b2;
299 	u8 ecb3_flags;
300 	u32 ecd_flags;
301 	int apie_h;
302 	int apie_s;
303 	int key_msk = test_kvm_facility(vcpu->kvm, 76);
304 	int fmt_o = crycbd_o & CRYCB_FORMAT_MASK;
305 	int fmt_h = vcpu->arch.sie_block->crycbd & CRYCB_FORMAT_MASK;
306 	int ret = 0;
307 
308 	scb_s->crycbd = 0;
309 
310 	apie_h = vcpu->arch.sie_block->eca & ECA_APIE;
311 	apie_s = apie_h & scb_o->eca;
312 	if (!apie_s && (!key_msk || (fmt_o == CRYCB_FORMAT0)))
313 		return 0;
314 
315 	if (!crycb_addr)
316 		return set_validity_icpt(scb_s, 0x0039U);
317 
318 	if (fmt_o == CRYCB_FORMAT1)
319 		if ((crycb_addr & PAGE_MASK) !=
320 		    ((crycb_addr + 128) & PAGE_MASK))
321 			return set_validity_icpt(scb_s, 0x003CU);
322 
323 	if (apie_s) {
324 		ret = setup_apcb(vcpu, &vsie_page->crycb, crycb_addr,
325 				 vcpu->kvm->arch.crypto.crycb,
326 				 fmt_o, fmt_h);
327 		if (ret)
328 			goto end;
329 		scb_s->eca |= scb_o->eca & ECA_APIE;
330 	}
331 
332 	/* we may only allow it if enabled for guest 2 */
333 	ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
334 		     (ECB3_AES | ECB3_DEA);
335 	ecd_flags = scb_o->ecd & vcpu->arch.sie_block->ecd & ECD_ECC;
336 	if (!ecb3_flags && !ecd_flags)
337 		goto end;
338 
339 	/* copy only the wrapping keys */
340 	if (read_guest_real(vcpu, crycb_addr + 72,
341 			    vsie_page->crycb.dea_wrapping_key_mask, 56))
342 		return set_validity_icpt(scb_s, 0x0035U);
343 
344 	scb_s->ecb3 |= ecb3_flags;
345 	scb_s->ecd |= ecd_flags;
346 
347 	/* xor both blocks in one run */
348 	b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
349 	b2 = (unsigned long *)
350 			    vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
351 	/* as 56%8 == 0, bitmap_xor won't overwrite any data */
352 	bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
353 end:
354 	switch (ret) {
355 	case -EINVAL:
356 		return set_validity_icpt(scb_s, 0x0022U);
357 	case -EFAULT:
358 		return set_validity_icpt(scb_s, 0x0035U);
359 	case -EACCES:
360 		return set_validity_icpt(scb_s, 0x003CU);
361 	}
362 	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT2;
363 	return 0;
364 }
365 
366 /* shadow (round up/down) the ibc to avoid validity icpt */
367 static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
368 {
369 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
370 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
371 	/* READ_ONCE does not work on bitfields - use a temporary variable */
372 	const uint32_t __new_ibc = scb_o->ibc;
373 	const uint32_t new_ibc = READ_ONCE(__new_ibc) & 0x0fffU;
374 	__u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
375 
376 	scb_s->ibc = 0;
377 	/* ibc installed in g2 and requested for g3 */
378 	if (vcpu->kvm->arch.model.ibc && new_ibc) {
379 		scb_s->ibc = new_ibc;
380 		/* takte care of the minimum ibc level of the machine */
381 		if (scb_s->ibc < min_ibc)
382 			scb_s->ibc = min_ibc;
383 		/* take care of the maximum ibc level set for the guest */
384 		if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
385 			scb_s->ibc = vcpu->kvm->arch.model.ibc;
386 	}
387 }
388 
389 /* unshadow the scb, copying parameters back to the real scb */
390 static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
391 {
392 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
393 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
394 
395 	/* interception */
396 	scb_o->icptcode = scb_s->icptcode;
397 	scb_o->icptstatus = scb_s->icptstatus;
398 	scb_o->ipa = scb_s->ipa;
399 	scb_o->ipb = scb_s->ipb;
400 	scb_o->gbea = scb_s->gbea;
401 
402 	/* timer */
403 	scb_o->cputm = scb_s->cputm;
404 	scb_o->ckc = scb_s->ckc;
405 	scb_o->todpr = scb_s->todpr;
406 
407 	/* guest state */
408 	scb_o->gpsw = scb_s->gpsw;
409 	scb_o->gg14 = scb_s->gg14;
410 	scb_o->gg15 = scb_s->gg15;
411 	memcpy(scb_o->gcr, scb_s->gcr, 128);
412 	scb_o->pp = scb_s->pp;
413 
414 	/* branch prediction */
415 	if (test_kvm_facility(vcpu->kvm, 82)) {
416 		scb_o->fpf &= ~FPF_BPBC;
417 		scb_o->fpf |= scb_s->fpf & FPF_BPBC;
418 	}
419 
420 	/* interrupt intercept */
421 	switch (scb_s->icptcode) {
422 	case ICPT_PROGI:
423 	case ICPT_INSTPROGI:
424 	case ICPT_EXTINT:
425 		memcpy((void *)((u64)scb_o + 0xc0),
426 		       (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
427 		break;
428 	}
429 
430 	if (scb_s->ihcpu != 0xffffU)
431 		scb_o->ihcpu = scb_s->ihcpu;
432 }
433 
434 /*
435  * Setup the shadow scb by copying and checking the relevant parts of the g2
436  * provided scb.
437  *
438  * Returns: - 0 if the scb has been shadowed
439  *          - > 0 if control has to be given to guest 2
440  */
441 static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
442 {
443 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
444 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
445 	/* READ_ONCE does not work on bitfields - use a temporary variable */
446 	const uint32_t __new_prefix = scb_o->prefix;
447 	const uint32_t new_prefix = READ_ONCE(__new_prefix);
448 	const bool wants_tx = READ_ONCE(scb_o->ecb) & ECB_TE;
449 	bool had_tx = scb_s->ecb & ECB_TE;
450 	unsigned long new_mso = 0;
451 	int rc;
452 
453 	/* make sure we don't have any leftovers when reusing the scb */
454 	scb_s->icptcode = 0;
455 	scb_s->eca = 0;
456 	scb_s->ecb = 0;
457 	scb_s->ecb2 = 0;
458 	scb_s->ecb3 = 0;
459 	scb_s->ecd = 0;
460 	scb_s->fac = 0;
461 	scb_s->fpf = 0;
462 
463 	rc = prepare_cpuflags(vcpu, vsie_page);
464 	if (rc)
465 		goto out;
466 
467 	/* timer */
468 	scb_s->cputm = scb_o->cputm;
469 	scb_s->ckc = scb_o->ckc;
470 	scb_s->todpr = scb_o->todpr;
471 	scb_s->epoch = scb_o->epoch;
472 
473 	/* guest state */
474 	scb_s->gpsw = scb_o->gpsw;
475 	scb_s->gg14 = scb_o->gg14;
476 	scb_s->gg15 = scb_o->gg15;
477 	memcpy(scb_s->gcr, scb_o->gcr, 128);
478 	scb_s->pp = scb_o->pp;
479 
480 	/* interception / execution handling */
481 	scb_s->gbea = scb_o->gbea;
482 	scb_s->lctl = scb_o->lctl;
483 	scb_s->svcc = scb_o->svcc;
484 	scb_s->ictl = scb_o->ictl;
485 	/*
486 	 * SKEY handling functions can't deal with false setting of PTE invalid
487 	 * bits. Therefore we cannot provide interpretation and would later
488 	 * have to provide own emulation handlers.
489 	 */
490 	if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
491 		scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
492 
493 	scb_s->icpua = scb_o->icpua;
494 
495 	if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
496 		new_mso = READ_ONCE(scb_o->mso) & 0xfffffffffff00000UL;
497 	/* if the hva of the prefix changes, we have to remap the prefix */
498 	if (scb_s->mso != new_mso || scb_s->prefix != new_prefix)
499 		prefix_unmapped(vsie_page);
500 	 /* SIE will do mso/msl validity and exception checks for us */
501 	scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
502 	scb_s->mso = new_mso;
503 	scb_s->prefix = new_prefix;
504 
505 	/* We have to definetly flush the tlb if this scb never ran */
506 	if (scb_s->ihcpu != 0xffffU)
507 		scb_s->ihcpu = scb_o->ihcpu;
508 
509 	/* MVPG and Protection Exception Interpretation are always available */
510 	scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
511 	/* Host-protection-interruption introduced with ESOP */
512 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
513 		scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
514 	/*
515 	 * CPU Topology
516 	 * This facility only uses the utility field of the SCA and none of
517 	 * the cpu entries that are problematic with the other interpretation
518 	 * facilities so we can pass it through
519 	 */
520 	if (test_kvm_facility(vcpu->kvm, 11))
521 		scb_s->ecb |= scb_o->ecb & ECB_PTF;
522 	/* transactional execution */
523 	if (test_kvm_facility(vcpu->kvm, 73) && wants_tx) {
524 		/* remap the prefix is tx is toggled on */
525 		if (!had_tx)
526 			prefix_unmapped(vsie_page);
527 		scb_s->ecb |= ECB_TE;
528 	}
529 	/* specification exception interpretation */
530 	scb_s->ecb |= scb_o->ecb & ECB_SPECI;
531 	/* branch prediction */
532 	if (test_kvm_facility(vcpu->kvm, 82))
533 		scb_s->fpf |= scb_o->fpf & FPF_BPBC;
534 	/* SIMD */
535 	if (test_kvm_facility(vcpu->kvm, 129)) {
536 		scb_s->eca |= scb_o->eca & ECA_VX;
537 		scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
538 	}
539 	/* Run-time-Instrumentation */
540 	if (test_kvm_facility(vcpu->kvm, 64))
541 		scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
542 	/* Instruction Execution Prevention */
543 	if (test_kvm_facility(vcpu->kvm, 130))
544 		scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
545 	/* Guarded Storage */
546 	if (test_kvm_facility(vcpu->kvm, 133)) {
547 		scb_s->ecb |= scb_o->ecb & ECB_GS;
548 		scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
549 	}
550 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
551 		scb_s->eca |= scb_o->eca & ECA_SII;
552 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
553 		scb_s->eca |= scb_o->eca & ECA_IB;
554 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
555 		scb_s->eca |= scb_o->eca & ECA_CEI;
556 	/* Epoch Extension */
557 	if (test_kvm_facility(vcpu->kvm, 139)) {
558 		scb_s->ecd |= scb_o->ecd & ECD_MEF;
559 		scb_s->epdx = scb_o->epdx;
560 	}
561 
562 	/* etoken */
563 	if (test_kvm_facility(vcpu->kvm, 156))
564 		scb_s->ecd |= scb_o->ecd & ECD_ETOKENF;
565 
566 	scb_s->hpid = HPID_VSIE;
567 	scb_s->cpnc = scb_o->cpnc;
568 
569 	prepare_ibc(vcpu, vsie_page);
570 	rc = shadow_crycb(vcpu, vsie_page);
571 out:
572 	if (rc)
573 		unshadow_scb(vcpu, vsie_page);
574 	return rc;
575 }
576 
577 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
578 				 unsigned long end)
579 {
580 	struct kvm *kvm = gmap->private;
581 	struct vsie_page *cur;
582 	unsigned long prefix;
583 	struct page *page;
584 	int i;
585 
586 	if (!gmap_is_shadow(gmap))
587 		return;
588 	if (start >= 1UL << 31)
589 		/* We are only interested in prefix pages */
590 		return;
591 
592 	/*
593 	 * Only new shadow blocks are added to the list during runtime,
594 	 * therefore we can safely reference them all the time.
595 	 */
596 	for (i = 0; i < kvm->arch.vsie.page_count; i++) {
597 		page = READ_ONCE(kvm->arch.vsie.pages[i]);
598 		if (!page)
599 			continue;
600 		cur = page_to_virt(page);
601 		if (READ_ONCE(cur->gmap) != gmap)
602 			continue;
603 		prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
604 		/* with mso/msl, the prefix lies at an offset */
605 		prefix += cur->scb_s.mso;
606 		if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
607 			prefix_unmapped_sync(cur);
608 	}
609 }
610 
611 /*
612  * Map the first prefix page and if tx is enabled also the second prefix page.
613  *
614  * The prefix will be protected, a gmap notifier will inform about unmaps.
615  * The shadow scb must not be executed until the prefix is remapped, this is
616  * guaranteed by properly handling PROG_REQUEST.
617  *
618  * Returns: - 0 on if successfully mapped or already mapped
619  *          - > 0 if control has to be given to guest 2
620  *          - -EAGAIN if the caller can retry immediately
621  *          - -ENOMEM if out of memory
622  */
623 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
624 {
625 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
626 	u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
627 	int rc;
628 
629 	if (prefix_is_mapped(vsie_page))
630 		return 0;
631 
632 	/* mark it as mapped so we can catch any concurrent unmappers */
633 	prefix_mapped(vsie_page);
634 
635 	/* with mso/msl, the prefix lies at offset *mso* */
636 	prefix += scb_s->mso;
637 
638 	rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix, NULL);
639 	if (!rc && (scb_s->ecb & ECB_TE))
640 		rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
641 					   prefix + PAGE_SIZE, NULL);
642 	/*
643 	 * We don't have to mprotect, we will be called for all unshadows.
644 	 * SIE will detect if protection applies and trigger a validity.
645 	 */
646 	if (rc)
647 		prefix_unmapped(vsie_page);
648 	if (rc > 0 || rc == -EFAULT)
649 		rc = set_validity_icpt(scb_s, 0x0037U);
650 	return rc;
651 }
652 
653 /*
654  * Pin the guest page given by gpa and set hpa to the pinned host address.
655  * Will always be pinned writable.
656  *
657  * Returns: - 0 on success
658  *          - -EINVAL if the gpa is not valid guest storage
659  */
660 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
661 {
662 	struct page *page;
663 
664 	page = gfn_to_page(kvm, gpa_to_gfn(gpa));
665 	if (is_error_page(page))
666 		return -EINVAL;
667 	*hpa = (hpa_t)page_to_phys(page) + (gpa & ~PAGE_MASK);
668 	return 0;
669 }
670 
671 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
672 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
673 {
674 	kvm_release_pfn_dirty(hpa >> PAGE_SHIFT);
675 	/* mark the page always as dirty for migration */
676 	mark_page_dirty(kvm, gpa_to_gfn(gpa));
677 }
678 
679 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
680 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
681 {
682 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
683 	hpa_t hpa;
684 
685 	hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
686 	if (hpa) {
687 		unpin_guest_page(vcpu->kvm, vsie_page->sca_gpa, hpa);
688 		vsie_page->sca_gpa = 0;
689 		scb_s->scaol = 0;
690 		scb_s->scaoh = 0;
691 	}
692 
693 	hpa = scb_s->itdba;
694 	if (hpa) {
695 		unpin_guest_page(vcpu->kvm, vsie_page->itdba_gpa, hpa);
696 		vsie_page->itdba_gpa = 0;
697 		scb_s->itdba = 0;
698 	}
699 
700 	hpa = scb_s->gvrd;
701 	if (hpa) {
702 		unpin_guest_page(vcpu->kvm, vsie_page->gvrd_gpa, hpa);
703 		vsie_page->gvrd_gpa = 0;
704 		scb_s->gvrd = 0;
705 	}
706 
707 	hpa = scb_s->riccbd;
708 	if (hpa) {
709 		unpin_guest_page(vcpu->kvm, vsie_page->riccbd_gpa, hpa);
710 		vsie_page->riccbd_gpa = 0;
711 		scb_s->riccbd = 0;
712 	}
713 
714 	hpa = scb_s->sdnxo;
715 	if (hpa) {
716 		unpin_guest_page(vcpu->kvm, vsie_page->sdnx_gpa, hpa);
717 		vsie_page->sdnx_gpa = 0;
718 		scb_s->sdnxo = 0;
719 	}
720 }
721 
722 /*
723  * Instead of shadowing some blocks, we can simply forward them because the
724  * addresses in the scb are 64 bit long.
725  *
726  * This works as long as the data lies in one page. If blocks ever exceed one
727  * page, we have to fall back to shadowing.
728  *
729  * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
730  * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
731  *
732  * Returns: - 0 if all blocks were pinned.
733  *          - > 0 if control has to be given to guest 2
734  *          - -ENOMEM if out of memory
735  */
736 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
737 {
738 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
739 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
740 	hpa_t hpa;
741 	gpa_t gpa;
742 	int rc = 0;
743 
744 	gpa = READ_ONCE(scb_o->scaol) & ~0xfUL;
745 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
746 		gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32;
747 	if (gpa) {
748 		if (gpa < 2 * PAGE_SIZE)
749 			rc = set_validity_icpt(scb_s, 0x0038U);
750 		else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
751 			rc = set_validity_icpt(scb_s, 0x0011U);
752 		else if ((gpa & PAGE_MASK) !=
753 			 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
754 			rc = set_validity_icpt(scb_s, 0x003bU);
755 		if (!rc) {
756 			rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
757 			if (rc)
758 				rc = set_validity_icpt(scb_s, 0x0034U);
759 		}
760 		if (rc)
761 			goto unpin;
762 		vsie_page->sca_gpa = gpa;
763 		scb_s->scaoh = (u32)((u64)hpa >> 32);
764 		scb_s->scaol = (u32)(u64)hpa;
765 	}
766 
767 	gpa = READ_ONCE(scb_o->itdba) & ~0xffUL;
768 	if (gpa && (scb_s->ecb & ECB_TE)) {
769 		if (gpa < 2 * PAGE_SIZE) {
770 			rc = set_validity_icpt(scb_s, 0x0080U);
771 			goto unpin;
772 		}
773 		/* 256 bytes cannot cross page boundaries */
774 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
775 		if (rc) {
776 			rc = set_validity_icpt(scb_s, 0x0080U);
777 			goto unpin;
778 		}
779 		vsie_page->itdba_gpa = gpa;
780 		scb_s->itdba = hpa;
781 	}
782 
783 	gpa = READ_ONCE(scb_o->gvrd) & ~0x1ffUL;
784 	if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
785 		if (gpa < 2 * PAGE_SIZE) {
786 			rc = set_validity_icpt(scb_s, 0x1310U);
787 			goto unpin;
788 		}
789 		/*
790 		 * 512 bytes vector registers cannot cross page boundaries
791 		 * if this block gets bigger, we have to shadow it.
792 		 */
793 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
794 		if (rc) {
795 			rc = set_validity_icpt(scb_s, 0x1310U);
796 			goto unpin;
797 		}
798 		vsie_page->gvrd_gpa = gpa;
799 		scb_s->gvrd = hpa;
800 	}
801 
802 	gpa = READ_ONCE(scb_o->riccbd) & ~0x3fUL;
803 	if (gpa && (scb_s->ecb3 & ECB3_RI)) {
804 		if (gpa < 2 * PAGE_SIZE) {
805 			rc = set_validity_icpt(scb_s, 0x0043U);
806 			goto unpin;
807 		}
808 		/* 64 bytes cannot cross page boundaries */
809 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
810 		if (rc) {
811 			rc = set_validity_icpt(scb_s, 0x0043U);
812 			goto unpin;
813 		}
814 		/* Validity 0x0044 will be checked by SIE */
815 		vsie_page->riccbd_gpa = gpa;
816 		scb_s->riccbd = hpa;
817 	}
818 	if (((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) ||
819 	    (scb_s->ecd & ECD_ETOKENF)) {
820 		unsigned long sdnxc;
821 
822 		gpa = READ_ONCE(scb_o->sdnxo) & ~0xfUL;
823 		sdnxc = READ_ONCE(scb_o->sdnxo) & 0xfUL;
824 		if (!gpa || gpa < 2 * PAGE_SIZE) {
825 			rc = set_validity_icpt(scb_s, 0x10b0U);
826 			goto unpin;
827 		}
828 		if (sdnxc < 6 || sdnxc > 12) {
829 			rc = set_validity_icpt(scb_s, 0x10b1U);
830 			goto unpin;
831 		}
832 		if (gpa & ((1 << sdnxc) - 1)) {
833 			rc = set_validity_icpt(scb_s, 0x10b2U);
834 			goto unpin;
835 		}
836 		/* Due to alignment rules (checked above) this cannot
837 		 * cross page boundaries
838 		 */
839 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
840 		if (rc) {
841 			rc = set_validity_icpt(scb_s, 0x10b0U);
842 			goto unpin;
843 		}
844 		vsie_page->sdnx_gpa = gpa;
845 		scb_s->sdnxo = hpa | sdnxc;
846 	}
847 	return 0;
848 unpin:
849 	unpin_blocks(vcpu, vsie_page);
850 	return rc;
851 }
852 
853 /* unpin the scb provided by guest 2, marking it as dirty */
854 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
855 		      gpa_t gpa)
856 {
857 	hpa_t hpa = (hpa_t) vsie_page->scb_o;
858 
859 	if (hpa)
860 		unpin_guest_page(vcpu->kvm, gpa, hpa);
861 	vsie_page->scb_o = NULL;
862 }
863 
864 /*
865  * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
866  *
867  * Returns: - 0 if the scb was pinned.
868  *          - > 0 if control has to be given to guest 2
869  */
870 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
871 		   gpa_t gpa)
872 {
873 	hpa_t hpa;
874 	int rc;
875 
876 	rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
877 	if (rc) {
878 		rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
879 		WARN_ON_ONCE(rc);
880 		return 1;
881 	}
882 	vsie_page->scb_o = phys_to_virt(hpa);
883 	return 0;
884 }
885 
886 /*
887  * Inject a fault into guest 2.
888  *
889  * Returns: - > 0 if control has to be given to guest 2
890  *            < 0 if an error occurred during injection.
891  */
892 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
893 			bool write_flag)
894 {
895 	struct kvm_s390_pgm_info pgm = {
896 		.code = code,
897 		.trans_exc_code =
898 			/* 0-51: virtual address */
899 			(vaddr & 0xfffffffffffff000UL) |
900 			/* 52-53: store / fetch */
901 			(((unsigned int) !write_flag) + 1) << 10,
902 			/* 62-63: asce id (alway primary == 0) */
903 		.exc_access_id = 0, /* always primary */
904 		.op_access_id = 0, /* not MVPG */
905 	};
906 	int rc;
907 
908 	if (code == PGM_PROTECTION)
909 		pgm.trans_exc_code |= 0x4UL;
910 
911 	rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
912 	return rc ? rc : 1;
913 }
914 
915 /*
916  * Handle a fault during vsie execution on a gmap shadow.
917  *
918  * Returns: - 0 if the fault was resolved
919  *          - > 0 if control has to be given to guest 2
920  *          - < 0 if an error occurred
921  */
922 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
923 {
924 	int rc;
925 
926 	if (current->thread.gmap_int_code == PGM_PROTECTION)
927 		/* we can directly forward all protection exceptions */
928 		return inject_fault(vcpu, PGM_PROTECTION,
929 				    current->thread.gmap_addr, 1);
930 
931 	rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
932 				   current->thread.gmap_addr, NULL);
933 	if (rc > 0) {
934 		rc = inject_fault(vcpu, rc,
935 				  current->thread.gmap_addr,
936 				  current->thread.gmap_write_flag);
937 		if (rc >= 0)
938 			vsie_page->fault_addr = current->thread.gmap_addr;
939 	}
940 	return rc;
941 }
942 
943 /*
944  * Retry the previous fault that required guest 2 intervention. This avoids
945  * one superfluous SIE re-entry and direct exit.
946  *
947  * Will ignore any errors. The next SIE fault will do proper fault handling.
948  */
949 static void handle_last_fault(struct kvm_vcpu *vcpu,
950 			      struct vsie_page *vsie_page)
951 {
952 	if (vsie_page->fault_addr)
953 		kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
954 				      vsie_page->fault_addr, NULL);
955 	vsie_page->fault_addr = 0;
956 }
957 
958 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
959 {
960 	vsie_page->scb_s.icptcode = 0;
961 }
962 
963 /* rewind the psw and clear the vsie icpt, so we can retry execution */
964 static void retry_vsie_icpt(struct vsie_page *vsie_page)
965 {
966 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
967 	int ilen = insn_length(scb_s->ipa >> 8);
968 
969 	/* take care of EXECUTE instructions */
970 	if (scb_s->icptstatus & 1) {
971 		ilen = (scb_s->icptstatus >> 4) & 0x6;
972 		if (!ilen)
973 			ilen = 4;
974 	}
975 	scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
976 	clear_vsie_icpt(vsie_page);
977 }
978 
979 /*
980  * Try to shadow + enable the guest 2 provided facility list.
981  * Retry instruction execution if enabled for and provided by guest 2.
982  *
983  * Returns: - 0 if handled (retry or guest 2 icpt)
984  *          - > 0 if control has to be given to guest 2
985  */
986 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
987 {
988 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
989 	__u32 fac = READ_ONCE(vsie_page->scb_o->fac) & 0x7ffffff8U;
990 
991 	if (fac && test_kvm_facility(vcpu->kvm, 7)) {
992 		retry_vsie_icpt(vsie_page);
993 		if (read_guest_real(vcpu, fac, &vsie_page->fac,
994 				    sizeof(vsie_page->fac)))
995 			return set_validity_icpt(scb_s, 0x1090U);
996 		scb_s->fac = (__u32)(__u64) &vsie_page->fac;
997 	}
998 	return 0;
999 }
1000 
1001 /*
1002  * Get a register for a nested guest.
1003  * @vcpu the vcpu of the guest
1004  * @vsie_page the vsie_page for the nested guest
1005  * @reg the register number, the upper 4 bits are ignored.
1006  * returns: the value of the register.
1007  */
1008 static u64 vsie_get_register(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, u8 reg)
1009 {
1010 	/* no need to validate the parameter and/or perform error handling */
1011 	reg &= 0xf;
1012 	switch (reg) {
1013 	case 15:
1014 		return vsie_page->scb_s.gg15;
1015 	case 14:
1016 		return vsie_page->scb_s.gg14;
1017 	default:
1018 		return vcpu->run->s.regs.gprs[reg];
1019 	}
1020 }
1021 
1022 static int vsie_handle_mvpg(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
1023 {
1024 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
1025 	unsigned long pei_dest, pei_src, src, dest, mask, prefix;
1026 	u64 *pei_block = &vsie_page->scb_o->mcic;
1027 	int edat, rc_dest, rc_src;
1028 	union ctlreg0 cr0;
1029 
1030 	cr0.val = vcpu->arch.sie_block->gcr[0];
1031 	edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
1032 	mask = _kvm_s390_logical_to_effective(&scb_s->gpsw, PAGE_MASK);
1033 	prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
1034 
1035 	dest = vsie_get_register(vcpu, vsie_page, scb_s->ipb >> 20) & mask;
1036 	dest = _kvm_s390_real_to_abs(prefix, dest) + scb_s->mso;
1037 	src = vsie_get_register(vcpu, vsie_page, scb_s->ipb >> 16) & mask;
1038 	src = _kvm_s390_real_to_abs(prefix, src) + scb_s->mso;
1039 
1040 	rc_dest = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, dest, &pei_dest);
1041 	rc_src = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, src, &pei_src);
1042 	/*
1043 	 * Either everything went well, or something non-critical went wrong
1044 	 * e.g. because of a race. In either case, simply retry.
1045 	 */
1046 	if (rc_dest == -EAGAIN || rc_src == -EAGAIN || (!rc_dest && !rc_src)) {
1047 		retry_vsie_icpt(vsie_page);
1048 		return -EAGAIN;
1049 	}
1050 	/* Something more serious went wrong, propagate the error */
1051 	if (rc_dest < 0)
1052 		return rc_dest;
1053 	if (rc_src < 0)
1054 		return rc_src;
1055 
1056 	/* The only possible suppressing exception: just deliver it */
1057 	if (rc_dest == PGM_TRANSLATION_SPEC || rc_src == PGM_TRANSLATION_SPEC) {
1058 		clear_vsie_icpt(vsie_page);
1059 		rc_dest = kvm_s390_inject_program_int(vcpu, PGM_TRANSLATION_SPEC);
1060 		WARN_ON_ONCE(rc_dest);
1061 		return 1;
1062 	}
1063 
1064 	/*
1065 	 * Forward the PEI intercept to the guest if it was a page fault, or
1066 	 * also for segment and region table faults if EDAT applies.
1067 	 */
1068 	if (edat) {
1069 		rc_dest = rc_dest == PGM_ASCE_TYPE ? rc_dest : 0;
1070 		rc_src = rc_src == PGM_ASCE_TYPE ? rc_src : 0;
1071 	} else {
1072 		rc_dest = rc_dest != PGM_PAGE_TRANSLATION ? rc_dest : 0;
1073 		rc_src = rc_src != PGM_PAGE_TRANSLATION ? rc_src : 0;
1074 	}
1075 	if (!rc_dest && !rc_src) {
1076 		pei_block[0] = pei_dest;
1077 		pei_block[1] = pei_src;
1078 		return 1;
1079 	}
1080 
1081 	retry_vsie_icpt(vsie_page);
1082 
1083 	/*
1084 	 * The host has edat, and the guest does not, or it was an ASCE type
1085 	 * exception. The host needs to inject the appropriate DAT interrupts
1086 	 * into the guest.
1087 	 */
1088 	if (rc_dest)
1089 		return inject_fault(vcpu, rc_dest, dest, 1);
1090 	return inject_fault(vcpu, rc_src, src, 0);
1091 }
1092 
1093 /*
1094  * Run the vsie on a shadow scb and a shadow gmap, without any further
1095  * sanity checks, handling SIE faults.
1096  *
1097  * Returns: - 0 everything went fine
1098  *          - > 0 if control has to be given to guest 2
1099  *          - < 0 if an error occurred
1100  */
1101 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
1102 	__releases(vcpu->kvm->srcu)
1103 	__acquires(vcpu->kvm->srcu)
1104 {
1105 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
1106 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
1107 	int guest_bp_isolation;
1108 	int rc = 0;
1109 
1110 	handle_last_fault(vcpu, vsie_page);
1111 
1112 	kvm_vcpu_srcu_read_unlock(vcpu);
1113 
1114 	/* save current guest state of bp isolation override */
1115 	guest_bp_isolation = test_thread_flag(TIF_ISOLATE_BP_GUEST);
1116 
1117 	/*
1118 	 * The guest is running with BPBC, so we have to force it on for our
1119 	 * nested guest. This is done by enabling BPBC globally, so the BPBC
1120 	 * control in the SCB (which the nested guest can modify) is simply
1121 	 * ignored.
1122 	 */
1123 	if (test_kvm_facility(vcpu->kvm, 82) &&
1124 	    vcpu->arch.sie_block->fpf & FPF_BPBC)
1125 		set_thread_flag(TIF_ISOLATE_BP_GUEST);
1126 
1127 	local_irq_disable();
1128 	guest_enter_irqoff();
1129 	local_irq_enable();
1130 
1131 	/*
1132 	 * Simulate a SIE entry of the VCPU (see sie64a), so VCPU blocking
1133 	 * and VCPU requests also hinder the vSIE from running and lead
1134 	 * to an immediate exit. kvm_s390_vsie_kick() has to be used to
1135 	 * also kick the vSIE.
1136 	 */
1137 	vcpu->arch.sie_block->prog0c |= PROG_IN_SIE;
1138 	barrier();
1139 	if (test_cpu_flag(CIF_FPU))
1140 		load_fpu_regs();
1141 	if (!kvm_s390_vcpu_sie_inhibited(vcpu))
1142 		rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
1143 	barrier();
1144 	vcpu->arch.sie_block->prog0c &= ~PROG_IN_SIE;
1145 
1146 	local_irq_disable();
1147 	guest_exit_irqoff();
1148 	local_irq_enable();
1149 
1150 	/* restore guest state for bp isolation override */
1151 	if (!guest_bp_isolation)
1152 		clear_thread_flag(TIF_ISOLATE_BP_GUEST);
1153 
1154 	kvm_vcpu_srcu_read_lock(vcpu);
1155 
1156 	if (rc == -EINTR) {
1157 		VCPU_EVENT(vcpu, 3, "%s", "machine check");
1158 		kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
1159 		return 0;
1160 	}
1161 
1162 	if (rc > 0)
1163 		rc = 0; /* we could still have an icpt */
1164 	else if (rc == -EFAULT)
1165 		return handle_fault(vcpu, vsie_page);
1166 
1167 	switch (scb_s->icptcode) {
1168 	case ICPT_INST:
1169 		if (scb_s->ipa == 0xb2b0)
1170 			rc = handle_stfle(vcpu, vsie_page);
1171 		break;
1172 	case ICPT_STOP:
1173 		/* stop not requested by g2 - must have been a kick */
1174 		if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
1175 			clear_vsie_icpt(vsie_page);
1176 		break;
1177 	case ICPT_VALIDITY:
1178 		if ((scb_s->ipa & 0xf000) != 0xf000)
1179 			scb_s->ipa += 0x1000;
1180 		break;
1181 	case ICPT_PARTEXEC:
1182 		if (scb_s->ipa == 0xb254)
1183 			rc = vsie_handle_mvpg(vcpu, vsie_page);
1184 		break;
1185 	}
1186 	return rc;
1187 }
1188 
1189 static void release_gmap_shadow(struct vsie_page *vsie_page)
1190 {
1191 	if (vsie_page->gmap)
1192 		gmap_put(vsie_page->gmap);
1193 	WRITE_ONCE(vsie_page->gmap, NULL);
1194 	prefix_unmapped(vsie_page);
1195 }
1196 
1197 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
1198 			       struct vsie_page *vsie_page)
1199 {
1200 	unsigned long asce;
1201 	union ctlreg0 cr0;
1202 	struct gmap *gmap;
1203 	int edat;
1204 
1205 	asce = vcpu->arch.sie_block->gcr[1];
1206 	cr0.val = vcpu->arch.sie_block->gcr[0];
1207 	edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
1208 	edat += edat && test_kvm_facility(vcpu->kvm, 78);
1209 
1210 	/*
1211 	 * ASCE or EDAT could have changed since last icpt, or the gmap
1212 	 * we're holding has been unshadowed. If the gmap is still valid,
1213 	 * we can safely reuse it.
1214 	 */
1215 	if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
1216 		return 0;
1217 
1218 	/* release the old shadow - if any, and mark the prefix as unmapped */
1219 	release_gmap_shadow(vsie_page);
1220 	gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
1221 	if (IS_ERR(gmap))
1222 		return PTR_ERR(gmap);
1223 	gmap->private = vcpu->kvm;
1224 	WRITE_ONCE(vsie_page->gmap, gmap);
1225 	return 0;
1226 }
1227 
1228 /*
1229  * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
1230  */
1231 static void register_shadow_scb(struct kvm_vcpu *vcpu,
1232 				struct vsie_page *vsie_page)
1233 {
1234 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
1235 
1236 	WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
1237 	/*
1238 	 * External calls have to lead to a kick of the vcpu and
1239 	 * therefore the vsie -> Simulate Wait state.
1240 	 */
1241 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
1242 	/*
1243 	 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
1244 	 * automatically be adjusted on tod clock changes via kvm_sync_clock.
1245 	 */
1246 	preempt_disable();
1247 	scb_s->epoch += vcpu->kvm->arch.epoch;
1248 
1249 	if (scb_s->ecd & ECD_MEF) {
1250 		scb_s->epdx += vcpu->kvm->arch.epdx;
1251 		if (scb_s->epoch < vcpu->kvm->arch.epoch)
1252 			scb_s->epdx += 1;
1253 	}
1254 
1255 	preempt_enable();
1256 }
1257 
1258 /*
1259  * Unregister a shadow scb from a VCPU.
1260  */
1261 static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
1262 {
1263 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_WAIT);
1264 	WRITE_ONCE(vcpu->arch.vsie_block, NULL);
1265 }
1266 
1267 /*
1268  * Run the vsie on a shadowed scb, managing the gmap shadow, handling
1269  * prefix pages and faults.
1270  *
1271  * Returns: - 0 if no errors occurred
1272  *          - > 0 if control has to be given to guest 2
1273  *          - -ENOMEM if out of memory
1274  */
1275 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
1276 {
1277 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
1278 	int rc = 0;
1279 
1280 	while (1) {
1281 		rc = acquire_gmap_shadow(vcpu, vsie_page);
1282 		if (!rc)
1283 			rc = map_prefix(vcpu, vsie_page);
1284 		if (!rc) {
1285 			gmap_enable(vsie_page->gmap);
1286 			update_intervention_requests(vsie_page);
1287 			rc = do_vsie_run(vcpu, vsie_page);
1288 			gmap_enable(vcpu->arch.gmap);
1289 		}
1290 		atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
1291 
1292 		if (rc == -EAGAIN)
1293 			rc = 0;
1294 		if (rc || scb_s->icptcode || signal_pending(current) ||
1295 		    kvm_s390_vcpu_has_irq(vcpu, 0) ||
1296 		    kvm_s390_vcpu_sie_inhibited(vcpu))
1297 			break;
1298 		cond_resched();
1299 	}
1300 
1301 	if (rc == -EFAULT) {
1302 		/*
1303 		 * Addressing exceptions are always presentes as intercepts.
1304 		 * As addressing exceptions are suppressing and our guest 3 PSW
1305 		 * points at the responsible instruction, we have to
1306 		 * forward the PSW and set the ilc. If we can't read guest 3
1307 		 * instruction, we can use an arbitrary ilc. Let's always use
1308 		 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
1309 		 * memory. (we could also fake the shadow so the hardware
1310 		 * handles it).
1311 		 */
1312 		scb_s->icptcode = ICPT_PROGI;
1313 		scb_s->iprcc = PGM_ADDRESSING;
1314 		scb_s->pgmilc = 4;
1315 		scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
1316 		rc = 1;
1317 	}
1318 	return rc;
1319 }
1320 
1321 /*
1322  * Get or create a vsie page for a scb address.
1323  *
1324  * Returns: - address of a vsie page (cached or new one)
1325  *          - NULL if the same scb address is already used by another VCPU
1326  *          - ERR_PTR(-ENOMEM) if out of memory
1327  */
1328 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
1329 {
1330 	struct vsie_page *vsie_page;
1331 	struct page *page;
1332 	int nr_vcpus;
1333 
1334 	rcu_read_lock();
1335 	page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1336 	rcu_read_unlock();
1337 	if (page) {
1338 		if (page_ref_inc_return(page) == 2)
1339 			return page_to_virt(page);
1340 		page_ref_dec(page);
1341 	}
1342 
1343 	/*
1344 	 * We want at least #online_vcpus shadows, so every VCPU can execute
1345 	 * the VSIE in parallel.
1346 	 */
1347 	nr_vcpus = atomic_read(&kvm->online_vcpus);
1348 
1349 	mutex_lock(&kvm->arch.vsie.mutex);
1350 	if (kvm->arch.vsie.page_count < nr_vcpus) {
1351 		page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA);
1352 		if (!page) {
1353 			mutex_unlock(&kvm->arch.vsie.mutex);
1354 			return ERR_PTR(-ENOMEM);
1355 		}
1356 		page_ref_inc(page);
1357 		kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1358 		kvm->arch.vsie.page_count++;
1359 	} else {
1360 		/* reuse an existing entry that belongs to nobody */
1361 		while (true) {
1362 			page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1363 			if (page_ref_inc_return(page) == 2)
1364 				break;
1365 			page_ref_dec(page);
1366 			kvm->arch.vsie.next++;
1367 			kvm->arch.vsie.next %= nr_vcpus;
1368 		}
1369 		radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1370 	}
1371 	page->index = addr;
1372 	/* double use of the same address */
1373 	if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1374 		page_ref_dec(page);
1375 		mutex_unlock(&kvm->arch.vsie.mutex);
1376 		return NULL;
1377 	}
1378 	mutex_unlock(&kvm->arch.vsie.mutex);
1379 
1380 	vsie_page = page_to_virt(page);
1381 	memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
1382 	release_gmap_shadow(vsie_page);
1383 	vsie_page->fault_addr = 0;
1384 	vsie_page->scb_s.ihcpu = 0xffffU;
1385 	return vsie_page;
1386 }
1387 
1388 /* put a vsie page acquired via get_vsie_page */
1389 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1390 {
1391 	struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1392 
1393 	page_ref_dec(page);
1394 }
1395 
1396 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1397 {
1398 	struct vsie_page *vsie_page;
1399 	unsigned long scb_addr;
1400 	int rc;
1401 
1402 	vcpu->stat.instruction_sie++;
1403 	if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1404 		return -EOPNOTSUPP;
1405 	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1406 		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1407 
1408 	BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
1409 	scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1410 
1411 	/* 512 byte alignment */
1412 	if (unlikely(scb_addr & 0x1ffUL))
1413 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1414 
1415 	if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0) ||
1416 	    kvm_s390_vcpu_sie_inhibited(vcpu))
1417 		return 0;
1418 
1419 	vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1420 	if (IS_ERR(vsie_page))
1421 		return PTR_ERR(vsie_page);
1422 	else if (!vsie_page)
1423 		/* double use of sie control block - simply do nothing */
1424 		return 0;
1425 
1426 	rc = pin_scb(vcpu, vsie_page, scb_addr);
1427 	if (rc)
1428 		goto out_put;
1429 	rc = shadow_scb(vcpu, vsie_page);
1430 	if (rc)
1431 		goto out_unpin_scb;
1432 	rc = pin_blocks(vcpu, vsie_page);
1433 	if (rc)
1434 		goto out_unshadow;
1435 	register_shadow_scb(vcpu, vsie_page);
1436 	rc = vsie_run(vcpu, vsie_page);
1437 	unregister_shadow_scb(vcpu);
1438 	unpin_blocks(vcpu, vsie_page);
1439 out_unshadow:
1440 	unshadow_scb(vcpu, vsie_page);
1441 out_unpin_scb:
1442 	unpin_scb(vcpu, vsie_page, scb_addr);
1443 out_put:
1444 	put_vsie_page(vcpu->kvm, vsie_page);
1445 
1446 	return rc < 0 ? rc : 0;
1447 }
1448 
1449 /* Init the vsie data structures. To be called when a vm is initialized. */
1450 void kvm_s390_vsie_init(struct kvm *kvm)
1451 {
1452 	mutex_init(&kvm->arch.vsie.mutex);
1453 	INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL_ACCOUNT);
1454 }
1455 
1456 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
1457 void kvm_s390_vsie_destroy(struct kvm *kvm)
1458 {
1459 	struct vsie_page *vsie_page;
1460 	struct page *page;
1461 	int i;
1462 
1463 	mutex_lock(&kvm->arch.vsie.mutex);
1464 	for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1465 		page = kvm->arch.vsie.pages[i];
1466 		kvm->arch.vsie.pages[i] = NULL;
1467 		vsie_page = page_to_virt(page);
1468 		release_gmap_shadow(vsie_page);
1469 		/* free the radix tree entry */
1470 		radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1471 		__free_page(page);
1472 	}
1473 	kvm->arch.vsie.page_count = 0;
1474 	mutex_unlock(&kvm->arch.vsie.mutex);
1475 }
1476 
1477 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1478 {
1479 	struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1480 
1481 	/*
1482 	 * Even if the VCPU lets go of the shadow sie block reference, it is
1483 	 * still valid in the cache. So we can safely kick it.
1484 	 */
1485 	if (scb) {
1486 		atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1487 		if (scb->prog0c & PROG_IN_SIE)
1488 			atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);
1489 	}
1490 }
1491