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