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