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