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