1 // SPDX-License-Identifier: GPL-2.0
3 * kvm nested virtualization support for s390x
5 * Copyright IBM Corp. 2016, 2018
7 * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
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>
17 #include <asm/mmu_context.h>
21 #include <asm/fpu/api.h>
26 struct kvm_s390_sie_block scb_s; /* 0x0000 */
28 * the backup info for machine check. ensure it's at
29 * the same offset as that in struct sie_page!
31 struct mcck_volatile_info mcck_info; /* 0x0200 */
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.
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 */
53 /* trigger a validity icpt for the given scb */
54 static int set_validity_icpt(struct kvm_s390_sie_block *scb,
58 scb->ipb = ((__u32) reason_code) << 16;
59 scb->icptcode = ICPT_VALIDITY;
63 /* mark the prefix as unmapped, this will block the VSIE */
64 static void prefix_unmapped(struct vsie_page *vsie_page)
66 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
69 /* mark the prefix as unmapped and wait until the VSIE has been left */
70 static void prefix_unmapped_sync(struct vsie_page *vsie_page)
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)
79 /* mark the prefix as mapped, this will allow the VSIE to run */
80 static void prefix_mapped(struct vsie_page *vsie_page)
82 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
85 /* test if the prefix is mapped into the gmap shadow */
86 static int prefix_is_mapped(struct vsie_page *vsie_page)
88 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
91 /* copy the updated intervention request bits into the shadow scb */
92 static void update_intervention_requests(struct vsie_page *vsie_page)
94 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
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);
102 /* shadow (filter and validate) the cpuflags */
103 static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
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);
109 /* we don't allow ESA/390 guests */
110 if (!(cpuflags & CPUSTAT_ZARCH))
111 return set_validity_icpt(scb_s, 0x0001U);
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);
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;
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;
136 atomic_set(&scb_s->cpuflags, newflags);
139 /* Copy to APCB FORMAT1 from APCB FORMAT0 */
140 static int setup_apcb10(struct kvm_vcpu *vcpu, struct kvm_s390_apcb1 *apcb_s,
141 unsigned long apcb_o, struct kvm_s390_apcb1 *apcb_h)
143 struct kvm_s390_apcb0 tmp;
145 if (read_guest_real(vcpu, apcb_o, &tmp, sizeof(struct kvm_s390_apcb0)))
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;
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
163 * Returns 0 and -EFAULT on error reading guest apcb
165 static int setup_apcb00(struct kvm_vcpu *vcpu, unsigned long *apcb_s,
166 unsigned long apcb_o, unsigned long *apcb_h)
168 if (read_guest_real(vcpu, apcb_o, apcb_s,
169 sizeof(struct kvm_s390_apcb0)))
172 bitmap_and(apcb_s, apcb_s, apcb_h, sizeof(struct kvm_s390_apcb0));
178 * setup_apcb11 - Copy the FORMAT1 APCB from the guest to the shadow CRYCB
179 * @vcpu: pointer to the virtual CPU
180 * @apcb_s: pointer to start of apcb in the shadow crycb
181 * @apcb_o: pointer to start of original guest apcb
182 * @apcb_h: pointer to start of apcb in the host
184 * Returns 0 and -EFAULT on error reading guest apcb
186 static int setup_apcb11(struct kvm_vcpu *vcpu, unsigned long *apcb_s,
187 unsigned long apcb_o,
188 unsigned long *apcb_h)
190 if (read_guest_real(vcpu, apcb_o, apcb_s,
191 sizeof(struct kvm_s390_apcb1)))
194 bitmap_and(apcb_s, apcb_s, apcb_h, sizeof(struct kvm_s390_apcb1));
200 * setup_apcb - Create a shadow copy of the apcb.
201 * @vcpu: pointer to the virtual CPU
202 * @crycb_s: pointer to shadow crycb
203 * @crycb_o: pointer to original guest crycb
204 * @crycb_h: pointer to the host crycb
205 * @fmt_o: format of the original guest crycb.
206 * @fmt_h: format of the host crycb.
208 * Checks the compatibility between the guest and host crycb and calls the
209 * appropriate copy function.
211 * Return 0 or an error number if the guest and host crycb are incompatible.
213 static int setup_apcb(struct kvm_vcpu *vcpu, struct kvm_s390_crypto_cb *crycb_s,
215 struct kvm_s390_crypto_cb *crycb_h,
216 int fmt_o, int fmt_h)
218 struct kvm_s390_crypto_cb *crycb;
220 crycb = (struct kvm_s390_crypto_cb *) (unsigned long)crycb_o;
224 if ((crycb_o & PAGE_MASK) != ((crycb_o + 256) & PAGE_MASK))
226 if (fmt_h != CRYCB_FORMAT2)
228 return setup_apcb11(vcpu, (unsigned long *)&crycb_s->apcb1,
229 (unsigned long) &crycb->apcb1,
230 (unsigned long *)&crycb_h->apcb1);
234 return setup_apcb10(vcpu, &crycb_s->apcb1,
235 (unsigned long) &crycb->apcb0,
238 return setup_apcb00(vcpu,
239 (unsigned long *) &crycb_s->apcb0,
240 (unsigned long) &crycb->apcb0,
241 (unsigned long *) &crycb_h->apcb0);
245 if ((crycb_o & PAGE_MASK) != ((crycb_o + 32) & PAGE_MASK))
250 return setup_apcb10(vcpu, &crycb_s->apcb1,
251 (unsigned long) &crycb->apcb0,
255 return setup_apcb00(vcpu,
256 (unsigned long *) &crycb_s->apcb0,
257 (unsigned long) &crycb->apcb0,
258 (unsigned long *) &crycb_h->apcb0);
265 * shadow_crycb - Create a shadow copy of the crycb block
266 * @vcpu: a pointer to the virtual CPU
267 * @vsie_page: a pointer to internal date used for the vSIE
269 * Create a shadow copy of the crycb block and setup key wrapping, if
270 * requested for guest 3 and enabled for guest 2.
272 * We accept format-1 or format-2, but we convert format-1 into format-2
273 * in the shadow CRYCB.
274 * Using format-2 enables the firmware to choose the right format when
275 * scheduling the SIE.
276 * There is nothing to do for format-0.
278 * This function centralize the issuing of set_validity_icpt() for all
279 * the subfunctions working on the crycb.
281 * Returns: - 0 if shadowed or nothing to do
282 * - > 0 if control has to be given to guest 2
284 static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
286 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
287 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
288 const uint32_t crycbd_o = READ_ONCE(scb_o->crycbd);
289 const u32 crycb_addr = crycbd_o & 0x7ffffff8U;
290 unsigned long *b1, *b2;
295 int key_msk = test_kvm_facility(vcpu->kvm, 76);
296 int fmt_o = crycbd_o & CRYCB_FORMAT_MASK;
297 int fmt_h = vcpu->arch.sie_block->crycbd & CRYCB_FORMAT_MASK;
302 apie_h = vcpu->arch.sie_block->eca & ECA_APIE;
303 apie_s = apie_h & scb_o->eca;
304 if (!apie_s && (!key_msk || (fmt_o == CRYCB_FORMAT0)))
308 return set_validity_icpt(scb_s, 0x0039U);
310 if (fmt_o == CRYCB_FORMAT1)
311 if ((crycb_addr & PAGE_MASK) !=
312 ((crycb_addr + 128) & PAGE_MASK))
313 return set_validity_icpt(scb_s, 0x003CU);
316 ret = setup_apcb(vcpu, &vsie_page->crycb, crycb_addr,
317 vcpu->kvm->arch.crypto.crycb,
321 scb_s->eca |= scb_o->eca & ECA_APIE;
324 /* we may only allow it if enabled for guest 2 */
325 ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
326 (ECB3_AES | ECB3_DEA);
327 ecd_flags = scb_o->ecd & vcpu->arch.sie_block->ecd & ECD_ECC;
328 if (!ecb3_flags && !ecd_flags)
331 /* copy only the wrapping keys */
332 if (read_guest_real(vcpu, crycb_addr + 72,
333 vsie_page->crycb.dea_wrapping_key_mask, 56))
334 return set_validity_icpt(scb_s, 0x0035U);
336 scb_s->ecb3 |= ecb3_flags;
337 scb_s->ecd |= ecd_flags;
339 /* xor both blocks in one run */
340 b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
341 b2 = (unsigned long *)
342 vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
343 /* as 56%8 == 0, bitmap_xor won't overwrite any data */
344 bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
348 return set_validity_icpt(scb_s, 0x0022U);
350 return set_validity_icpt(scb_s, 0x0035U);
352 return set_validity_icpt(scb_s, 0x003CU);
354 scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT2;
358 /* shadow (round up/down) the ibc to avoid validity icpt */
359 static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
361 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
362 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
363 /* READ_ONCE does not work on bitfields - use a temporary variable */
364 const uint32_t __new_ibc = scb_o->ibc;
365 const uint32_t new_ibc = READ_ONCE(__new_ibc) & 0x0fffU;
366 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
369 /* ibc installed in g2 and requested for g3 */
370 if (vcpu->kvm->arch.model.ibc && new_ibc) {
371 scb_s->ibc = new_ibc;
372 /* takte care of the minimum ibc level of the machine */
373 if (scb_s->ibc < min_ibc)
374 scb_s->ibc = min_ibc;
375 /* take care of the maximum ibc level set for the guest */
376 if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
377 scb_s->ibc = vcpu->kvm->arch.model.ibc;
381 /* unshadow the scb, copying parameters back to the real scb */
382 static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
384 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
385 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
388 scb_o->icptcode = scb_s->icptcode;
389 scb_o->icptstatus = scb_s->icptstatus;
390 scb_o->ipa = scb_s->ipa;
391 scb_o->ipb = scb_s->ipb;
392 scb_o->gbea = scb_s->gbea;
395 scb_o->cputm = scb_s->cputm;
396 scb_o->ckc = scb_s->ckc;
397 scb_o->todpr = scb_s->todpr;
400 scb_o->gpsw = scb_s->gpsw;
401 scb_o->gg14 = scb_s->gg14;
402 scb_o->gg15 = scb_s->gg15;
403 memcpy(scb_o->gcr, scb_s->gcr, 128);
404 scb_o->pp = scb_s->pp;
406 /* branch prediction */
407 if (test_kvm_facility(vcpu->kvm, 82)) {
408 scb_o->fpf &= ~FPF_BPBC;
409 scb_o->fpf |= scb_s->fpf & FPF_BPBC;
412 /* interrupt intercept */
413 switch (scb_s->icptcode) {
417 memcpy((void *)((u64)scb_o + 0xc0),
418 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
422 if (scb_s->ihcpu != 0xffffU)
423 scb_o->ihcpu = scb_s->ihcpu;
427 * Setup the shadow scb by copying and checking the relevant parts of the g2
430 * Returns: - 0 if the scb has been shadowed
431 * - > 0 if control has to be given to guest 2
433 static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
435 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
436 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
437 /* READ_ONCE does not work on bitfields - use a temporary variable */
438 const uint32_t __new_prefix = scb_o->prefix;
439 const uint32_t new_prefix = READ_ONCE(__new_prefix);
440 const bool wants_tx = READ_ONCE(scb_o->ecb) & ECB_TE;
441 bool had_tx = scb_s->ecb & ECB_TE;
442 unsigned long new_mso = 0;
445 /* make sure we don't have any leftovers when reusing the scb */
455 rc = prepare_cpuflags(vcpu, vsie_page);
460 scb_s->cputm = scb_o->cputm;
461 scb_s->ckc = scb_o->ckc;
462 scb_s->todpr = scb_o->todpr;
463 scb_s->epoch = scb_o->epoch;
466 scb_s->gpsw = scb_o->gpsw;
467 scb_s->gg14 = scb_o->gg14;
468 scb_s->gg15 = scb_o->gg15;
469 memcpy(scb_s->gcr, scb_o->gcr, 128);
470 scb_s->pp = scb_o->pp;
472 /* interception / execution handling */
473 scb_s->gbea = scb_o->gbea;
474 scb_s->lctl = scb_o->lctl;
475 scb_s->svcc = scb_o->svcc;
476 scb_s->ictl = scb_o->ictl;
478 * SKEY handling functions can't deal with false setting of PTE invalid
479 * bits. Therefore we cannot provide interpretation and would later
480 * have to provide own emulation handlers.
482 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
483 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
485 scb_s->icpua = scb_o->icpua;
487 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
488 new_mso = READ_ONCE(scb_o->mso) & 0xfffffffffff00000UL;
489 /* if the hva of the prefix changes, we have to remap the prefix */
490 if (scb_s->mso != new_mso || scb_s->prefix != new_prefix)
491 prefix_unmapped(vsie_page);
492 /* SIE will do mso/msl validity and exception checks for us */
493 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
494 scb_s->mso = new_mso;
495 scb_s->prefix = new_prefix;
497 /* We have to definetly flush the tlb if this scb never ran */
498 if (scb_s->ihcpu != 0xffffU)
499 scb_s->ihcpu = scb_o->ihcpu;
501 /* MVPG and Protection Exception Interpretation are always available */
502 scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
503 /* Host-protection-interruption introduced with ESOP */
504 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
505 scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
506 /* transactional execution */
507 if (test_kvm_facility(vcpu->kvm, 73) && wants_tx) {
508 /* remap the prefix is tx is toggled on */
510 prefix_unmapped(vsie_page);
511 scb_s->ecb |= ECB_TE;
513 /* branch prediction */
514 if (test_kvm_facility(vcpu->kvm, 82))
515 scb_s->fpf |= scb_o->fpf & FPF_BPBC;
517 if (test_kvm_facility(vcpu->kvm, 129)) {
518 scb_s->eca |= scb_o->eca & ECA_VX;
519 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
521 /* Run-time-Instrumentation */
522 if (test_kvm_facility(vcpu->kvm, 64))
523 scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
524 /* Instruction Execution Prevention */
525 if (test_kvm_facility(vcpu->kvm, 130))
526 scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
527 /* Guarded Storage */
528 if (test_kvm_facility(vcpu->kvm, 133)) {
529 scb_s->ecb |= scb_o->ecb & ECB_GS;
530 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
532 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
533 scb_s->eca |= scb_o->eca & ECA_SII;
534 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
535 scb_s->eca |= scb_o->eca & ECA_IB;
536 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
537 scb_s->eca |= scb_o->eca & ECA_CEI;
538 /* Epoch Extension */
539 if (test_kvm_facility(vcpu->kvm, 139))
540 scb_s->ecd |= scb_o->ecd & ECD_MEF;
543 if (test_kvm_facility(vcpu->kvm, 156))
544 scb_s->ecd |= scb_o->ecd & ECD_ETOKENF;
546 scb_s->hpid = HPID_VSIE;
547 scb_s->cpnc = scb_o->cpnc;
549 prepare_ibc(vcpu, vsie_page);
550 rc = shadow_crycb(vcpu, vsie_page);
553 unshadow_scb(vcpu, vsie_page);
557 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
560 struct kvm *kvm = gmap->private;
561 struct vsie_page *cur;
562 unsigned long prefix;
566 if (!gmap_is_shadow(gmap))
568 if (start >= 1UL << 31)
569 /* We are only interested in prefix pages */
573 * Only new shadow blocks are added to the list during runtime,
574 * therefore we can safely reference them all the time.
576 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
577 page = READ_ONCE(kvm->arch.vsie.pages[i]);
580 cur = page_to_virt(page);
581 if (READ_ONCE(cur->gmap) != gmap)
583 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
584 /* with mso/msl, the prefix lies at an offset */
585 prefix += cur->scb_s.mso;
586 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
587 prefix_unmapped_sync(cur);
592 * Map the first prefix page and if tx is enabled also the second prefix page.
594 * The prefix will be protected, a gmap notifier will inform about unmaps.
595 * The shadow scb must not be executed until the prefix is remapped, this is
596 * guaranteed by properly handling PROG_REQUEST.
598 * Returns: - 0 on if successfully mapped or already mapped
599 * - > 0 if control has to be given to guest 2
600 * - -EAGAIN if the caller can retry immediately
601 * - -ENOMEM if out of memory
603 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
605 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
606 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
609 if (prefix_is_mapped(vsie_page))
612 /* mark it as mapped so we can catch any concurrent unmappers */
613 prefix_mapped(vsie_page);
615 /* with mso/msl, the prefix lies at offset *mso* */
616 prefix += scb_s->mso;
618 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix, NULL);
619 if (!rc && (scb_s->ecb & ECB_TE))
620 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
621 prefix + PAGE_SIZE, NULL);
623 * We don't have to mprotect, we will be called for all unshadows.
624 * SIE will detect if protection applies and trigger a validity.
627 prefix_unmapped(vsie_page);
628 if (rc > 0 || rc == -EFAULT)
629 rc = set_validity_icpt(scb_s, 0x0037U);
634 * Pin the guest page given by gpa and set hpa to the pinned host address.
635 * Will always be pinned writable.
637 * Returns: - 0 on success
638 * - -EINVAL if the gpa is not valid guest storage
640 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
644 page = gfn_to_page(kvm, gpa_to_gfn(gpa));
645 if (is_error_page(page))
647 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
651 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
652 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
654 kvm_release_pfn_dirty(hpa >> PAGE_SHIFT);
655 /* mark the page always as dirty for migration */
656 mark_page_dirty(kvm, gpa_to_gfn(gpa));
659 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
660 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
662 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
665 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
667 unpin_guest_page(vcpu->kvm, vsie_page->sca_gpa, hpa);
668 vsie_page->sca_gpa = 0;
675 unpin_guest_page(vcpu->kvm, vsie_page->itdba_gpa, hpa);
676 vsie_page->itdba_gpa = 0;
682 unpin_guest_page(vcpu->kvm, vsie_page->gvrd_gpa, hpa);
683 vsie_page->gvrd_gpa = 0;
689 unpin_guest_page(vcpu->kvm, vsie_page->riccbd_gpa, hpa);
690 vsie_page->riccbd_gpa = 0;
696 unpin_guest_page(vcpu->kvm, vsie_page->sdnx_gpa, hpa);
697 vsie_page->sdnx_gpa = 0;
703 * Instead of shadowing some blocks, we can simply forward them because the
704 * addresses in the scb are 64 bit long.
706 * This works as long as the data lies in one page. If blocks ever exceed one
707 * page, we have to fall back to shadowing.
709 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
710 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
712 * Returns: - 0 if all blocks were pinned.
713 * - > 0 if control has to be given to guest 2
714 * - -ENOMEM if out of memory
716 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
718 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
719 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
724 gpa = READ_ONCE(scb_o->scaol) & ~0xfUL;
725 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
726 gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32;
728 if (gpa < 2 * PAGE_SIZE)
729 rc = set_validity_icpt(scb_s, 0x0038U);
730 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
731 rc = set_validity_icpt(scb_s, 0x0011U);
732 else if ((gpa & PAGE_MASK) !=
733 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
734 rc = set_validity_icpt(scb_s, 0x003bU);
736 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
738 rc = set_validity_icpt(scb_s, 0x0034U);
742 vsie_page->sca_gpa = gpa;
743 scb_s->scaoh = (u32)((u64)hpa >> 32);
744 scb_s->scaol = (u32)(u64)hpa;
747 gpa = READ_ONCE(scb_o->itdba) & ~0xffUL;
748 if (gpa && (scb_s->ecb & ECB_TE)) {
749 if (gpa < 2 * PAGE_SIZE) {
750 rc = set_validity_icpt(scb_s, 0x0080U);
753 /* 256 bytes cannot cross page boundaries */
754 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
756 rc = set_validity_icpt(scb_s, 0x0080U);
759 vsie_page->itdba_gpa = gpa;
763 gpa = READ_ONCE(scb_o->gvrd) & ~0x1ffUL;
764 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
765 if (gpa < 2 * PAGE_SIZE) {
766 rc = set_validity_icpt(scb_s, 0x1310U);
770 * 512 bytes vector registers cannot cross page boundaries
771 * if this block gets bigger, we have to shadow it.
773 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
775 rc = set_validity_icpt(scb_s, 0x1310U);
778 vsie_page->gvrd_gpa = gpa;
782 gpa = READ_ONCE(scb_o->riccbd) & ~0x3fUL;
783 if (gpa && (scb_s->ecb3 & ECB3_RI)) {
784 if (gpa < 2 * PAGE_SIZE) {
785 rc = set_validity_icpt(scb_s, 0x0043U);
788 /* 64 bytes cannot cross page boundaries */
789 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
791 rc = set_validity_icpt(scb_s, 0x0043U);
794 /* Validity 0x0044 will be checked by SIE */
795 vsie_page->riccbd_gpa = gpa;
798 if (((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) ||
799 (scb_s->ecd & ECD_ETOKENF)) {
802 gpa = READ_ONCE(scb_o->sdnxo) & ~0xfUL;
803 sdnxc = READ_ONCE(scb_o->sdnxo) & 0xfUL;
804 if (!gpa || gpa < 2 * PAGE_SIZE) {
805 rc = set_validity_icpt(scb_s, 0x10b0U);
808 if (sdnxc < 6 || sdnxc > 12) {
809 rc = set_validity_icpt(scb_s, 0x10b1U);
812 if (gpa & ((1 << sdnxc) - 1)) {
813 rc = set_validity_icpt(scb_s, 0x10b2U);
816 /* Due to alignment rules (checked above) this cannot
817 * cross page boundaries
819 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
821 rc = set_validity_icpt(scb_s, 0x10b0U);
824 vsie_page->sdnx_gpa = gpa;
825 scb_s->sdnxo = hpa | sdnxc;
829 unpin_blocks(vcpu, vsie_page);
833 /* unpin the scb provided by guest 2, marking it as dirty */
834 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
837 hpa_t hpa = (hpa_t) vsie_page->scb_o;
840 unpin_guest_page(vcpu->kvm, gpa, hpa);
841 vsie_page->scb_o = NULL;
845 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
847 * Returns: - 0 if the scb was pinned.
848 * - > 0 if control has to be given to guest 2
850 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
856 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
858 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
862 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
867 * Inject a fault into guest 2.
869 * Returns: - > 0 if control has to be given to guest 2
870 * < 0 if an error occurred during injection.
872 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
875 struct kvm_s390_pgm_info pgm = {
878 /* 0-51: virtual address */
879 (vaddr & 0xfffffffffffff000UL) |
880 /* 52-53: store / fetch */
881 (((unsigned int) !write_flag) + 1) << 10,
882 /* 62-63: asce id (alway primary == 0) */
883 .exc_access_id = 0, /* always primary */
884 .op_access_id = 0, /* not MVPG */
888 if (code == PGM_PROTECTION)
889 pgm.trans_exc_code |= 0x4UL;
891 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
896 * Handle a fault during vsie execution on a gmap shadow.
898 * Returns: - 0 if the fault was resolved
899 * - > 0 if control has to be given to guest 2
900 * - < 0 if an error occurred
902 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
906 if (current->thread.gmap_int_code == PGM_PROTECTION)
907 /* we can directly forward all protection exceptions */
908 return inject_fault(vcpu, PGM_PROTECTION,
909 current->thread.gmap_addr, 1);
911 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
912 current->thread.gmap_addr, NULL);
914 rc = inject_fault(vcpu, rc,
915 current->thread.gmap_addr,
916 current->thread.gmap_write_flag);
918 vsie_page->fault_addr = current->thread.gmap_addr;
924 * Retry the previous fault that required guest 2 intervention. This avoids
925 * one superfluous SIE re-entry and direct exit.
927 * Will ignore any errors. The next SIE fault will do proper fault handling.
929 static void handle_last_fault(struct kvm_vcpu *vcpu,
930 struct vsie_page *vsie_page)
932 if (vsie_page->fault_addr)
933 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
934 vsie_page->fault_addr, NULL);
935 vsie_page->fault_addr = 0;
938 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
940 vsie_page->scb_s.icptcode = 0;
943 /* rewind the psw and clear the vsie icpt, so we can retry execution */
944 static void retry_vsie_icpt(struct vsie_page *vsie_page)
946 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
947 int ilen = insn_length(scb_s->ipa >> 8);
949 /* take care of EXECUTE instructions */
950 if (scb_s->icptstatus & 1) {
951 ilen = (scb_s->icptstatus >> 4) & 0x6;
955 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
956 clear_vsie_icpt(vsie_page);
960 * Try to shadow + enable the guest 2 provided facility list.
961 * Retry instruction execution if enabled for and provided by guest 2.
963 * Returns: - 0 if handled (retry or guest 2 icpt)
964 * - > 0 if control has to be given to guest 2
966 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
968 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
969 __u32 fac = READ_ONCE(vsie_page->scb_o->fac) & 0x7ffffff8U;
971 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
972 retry_vsie_icpt(vsie_page);
973 if (read_guest_real(vcpu, fac, &vsie_page->fac,
974 sizeof(vsie_page->fac)))
975 return set_validity_icpt(scb_s, 0x1090U);
976 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
982 * Get a register for a nested guest.
983 * @vcpu the vcpu of the guest
984 * @vsie_page the vsie_page for the nested guest
985 * @reg the register number, the upper 4 bits are ignored.
986 * returns: the value of the register.
988 static u64 vsie_get_register(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, u8 reg)
990 /* no need to validate the parameter and/or perform error handling */
994 return vsie_page->scb_s.gg15;
996 return vsie_page->scb_s.gg14;
998 return vcpu->run->s.regs.gprs[reg];
1002 static int vsie_handle_mvpg(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
1004 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
1005 unsigned long pei_dest, pei_src, src, dest, mask, prefix;
1006 u64 *pei_block = &vsie_page->scb_o->mcic;
1007 int edat, rc_dest, rc_src;
1010 cr0.val = vcpu->arch.sie_block->gcr[0];
1011 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
1012 mask = _kvm_s390_logical_to_effective(&scb_s->gpsw, PAGE_MASK);
1013 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
1015 dest = vsie_get_register(vcpu, vsie_page, scb_s->ipb >> 20) & mask;
1016 dest = _kvm_s390_real_to_abs(prefix, dest) + scb_s->mso;
1017 src = vsie_get_register(vcpu, vsie_page, scb_s->ipb >> 16) & mask;
1018 src = _kvm_s390_real_to_abs(prefix, src) + scb_s->mso;
1020 rc_dest = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, dest, &pei_dest);
1021 rc_src = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, src, &pei_src);
1023 * Either everything went well, or something non-critical went wrong
1024 * e.g. because of a race. In either case, simply retry.
1026 if (rc_dest == -EAGAIN || rc_src == -EAGAIN || (!rc_dest && !rc_src)) {
1027 retry_vsie_icpt(vsie_page);
1030 /* Something more serious went wrong, propagate the error */
1036 /* The only possible suppressing exception: just deliver it */
1037 if (rc_dest == PGM_TRANSLATION_SPEC || rc_src == PGM_TRANSLATION_SPEC) {
1038 clear_vsie_icpt(vsie_page);
1039 rc_dest = kvm_s390_inject_program_int(vcpu, PGM_TRANSLATION_SPEC);
1040 WARN_ON_ONCE(rc_dest);
1045 * Forward the PEI intercept to the guest if it was a page fault, or
1046 * also for segment and region table faults if EDAT applies.
1049 rc_dest = rc_dest == PGM_ASCE_TYPE ? rc_dest : 0;
1050 rc_src = rc_src == PGM_ASCE_TYPE ? rc_src : 0;
1052 rc_dest = rc_dest != PGM_PAGE_TRANSLATION ? rc_dest : 0;
1053 rc_src = rc_src != PGM_PAGE_TRANSLATION ? rc_src : 0;
1055 if (!rc_dest && !rc_src) {
1056 pei_block[0] = pei_dest;
1057 pei_block[1] = pei_src;
1061 retry_vsie_icpt(vsie_page);
1064 * The host has edat, and the guest does not, or it was an ASCE type
1065 * exception. The host needs to inject the appropriate DAT interrupts
1069 return inject_fault(vcpu, rc_dest, dest, 1);
1070 return inject_fault(vcpu, rc_src, src, 0);
1074 * Run the vsie on a shadow scb and a shadow gmap, without any further
1075 * sanity checks, handling SIE faults.
1077 * Returns: - 0 everything went fine
1078 * - > 0 if control has to be given to guest 2
1079 * - < 0 if an error occurred
1081 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
1082 __releases(vcpu->kvm->srcu)
1083 __acquires(vcpu->kvm->srcu)
1085 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
1086 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
1087 int guest_bp_isolation;
1090 handle_last_fault(vcpu, vsie_page);
1092 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
1094 /* save current guest state of bp isolation override */
1095 guest_bp_isolation = test_thread_flag(TIF_ISOLATE_BP_GUEST);
1098 * The guest is running with BPBC, so we have to force it on for our
1099 * nested guest. This is done by enabling BPBC globally, so the BPBC
1100 * control in the SCB (which the nested guest can modify) is simply
1103 if (test_kvm_facility(vcpu->kvm, 82) &&
1104 vcpu->arch.sie_block->fpf & FPF_BPBC)
1105 set_thread_flag(TIF_ISOLATE_BP_GUEST);
1107 local_irq_disable();
1108 guest_enter_irqoff();
1112 * Simulate a SIE entry of the VCPU (see sie64a), so VCPU blocking
1113 * and VCPU requests also hinder the vSIE from running and lead
1114 * to an immediate exit. kvm_s390_vsie_kick() has to be used to
1115 * also kick the vSIE.
1117 vcpu->arch.sie_block->prog0c |= PROG_IN_SIE;
1119 if (test_cpu_flag(CIF_FPU))
1121 if (!kvm_s390_vcpu_sie_inhibited(vcpu))
1122 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
1124 vcpu->arch.sie_block->prog0c &= ~PROG_IN_SIE;
1126 local_irq_disable();
1127 guest_exit_irqoff();
1130 /* restore guest state for bp isolation override */
1131 if (!guest_bp_isolation)
1132 clear_thread_flag(TIF_ISOLATE_BP_GUEST);
1134 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1137 VCPU_EVENT(vcpu, 3, "%s", "machine check");
1138 kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
1143 rc = 0; /* we could still have an icpt */
1144 else if (rc == -EFAULT)
1145 return handle_fault(vcpu, vsie_page);
1147 switch (scb_s->icptcode) {
1149 if (scb_s->ipa == 0xb2b0)
1150 rc = handle_stfle(vcpu, vsie_page);
1153 /* stop not requested by g2 - must have been a kick */
1154 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
1155 clear_vsie_icpt(vsie_page);
1158 if ((scb_s->ipa & 0xf000) != 0xf000)
1159 scb_s->ipa += 0x1000;
1162 if (scb_s->ipa == 0xb254)
1163 rc = vsie_handle_mvpg(vcpu, vsie_page);
1169 static void release_gmap_shadow(struct vsie_page *vsie_page)
1171 if (vsie_page->gmap)
1172 gmap_put(vsie_page->gmap);
1173 WRITE_ONCE(vsie_page->gmap, NULL);
1174 prefix_unmapped(vsie_page);
1177 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
1178 struct vsie_page *vsie_page)
1185 asce = vcpu->arch.sie_block->gcr[1];
1186 cr0.val = vcpu->arch.sie_block->gcr[0];
1187 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
1188 edat += edat && test_kvm_facility(vcpu->kvm, 78);
1191 * ASCE or EDAT could have changed since last icpt, or the gmap
1192 * we're holding has been unshadowed. If the gmap is still valid,
1193 * we can safely reuse it.
1195 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
1198 /* release the old shadow - if any, and mark the prefix as unmapped */
1199 release_gmap_shadow(vsie_page);
1200 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
1202 return PTR_ERR(gmap);
1203 gmap->private = vcpu->kvm;
1204 WRITE_ONCE(vsie_page->gmap, gmap);
1209 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
1211 static void register_shadow_scb(struct kvm_vcpu *vcpu,
1212 struct vsie_page *vsie_page)
1214 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
1216 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
1218 * External calls have to lead to a kick of the vcpu and
1219 * therefore the vsie -> Simulate Wait state.
1221 kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
1223 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
1224 * automatically be adjusted on tod clock changes via kvm_sync_clock.
1227 scb_s->epoch += vcpu->kvm->arch.epoch;
1229 if (scb_s->ecd & ECD_MEF) {
1230 scb_s->epdx += vcpu->kvm->arch.epdx;
1231 if (scb_s->epoch < vcpu->kvm->arch.epoch)
1239 * Unregister a shadow scb from a VCPU.
1241 static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
1243 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_WAIT);
1244 WRITE_ONCE(vcpu->arch.vsie_block, NULL);
1248 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
1249 * prefix pages and faults.
1251 * Returns: - 0 if no errors occurred
1252 * - > 0 if control has to be given to guest 2
1253 * - -ENOMEM if out of memory
1255 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
1257 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
1261 rc = acquire_gmap_shadow(vcpu, vsie_page);
1263 rc = map_prefix(vcpu, vsie_page);
1265 gmap_enable(vsie_page->gmap);
1266 update_intervention_requests(vsie_page);
1267 rc = do_vsie_run(vcpu, vsie_page);
1268 gmap_enable(vcpu->arch.gmap);
1270 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
1274 if (rc || scb_s->icptcode || signal_pending(current) ||
1275 kvm_s390_vcpu_has_irq(vcpu, 0) ||
1276 kvm_s390_vcpu_sie_inhibited(vcpu))
1281 if (rc == -EFAULT) {
1283 * Addressing exceptions are always presentes as intercepts.
1284 * As addressing exceptions are suppressing and our guest 3 PSW
1285 * points at the responsible instruction, we have to
1286 * forward the PSW and set the ilc. If we can't read guest 3
1287 * instruction, we can use an arbitrary ilc. Let's always use
1288 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
1289 * memory. (we could also fake the shadow so the hardware
1292 scb_s->icptcode = ICPT_PROGI;
1293 scb_s->iprcc = PGM_ADDRESSING;
1295 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
1302 * Get or create a vsie page for a scb address.
1304 * Returns: - address of a vsie page (cached or new one)
1305 * - NULL if the same scb address is already used by another VCPU
1306 * - ERR_PTR(-ENOMEM) if out of memory
1308 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
1310 struct vsie_page *vsie_page;
1315 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1318 if (page_ref_inc_return(page) == 2)
1319 return page_to_virt(page);
1324 * We want at least #online_vcpus shadows, so every VCPU can execute
1325 * the VSIE in parallel.
1327 nr_vcpus = atomic_read(&kvm->online_vcpus);
1329 mutex_lock(&kvm->arch.vsie.mutex);
1330 if (kvm->arch.vsie.page_count < nr_vcpus) {
1331 page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO | GFP_DMA);
1333 mutex_unlock(&kvm->arch.vsie.mutex);
1334 return ERR_PTR(-ENOMEM);
1337 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1338 kvm->arch.vsie.page_count++;
1340 /* reuse an existing entry that belongs to nobody */
1342 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1343 if (page_ref_inc_return(page) == 2)
1346 kvm->arch.vsie.next++;
1347 kvm->arch.vsie.next %= nr_vcpus;
1349 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1352 /* double use of the same address */
1353 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1355 mutex_unlock(&kvm->arch.vsie.mutex);
1358 mutex_unlock(&kvm->arch.vsie.mutex);
1360 vsie_page = page_to_virt(page);
1361 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
1362 release_gmap_shadow(vsie_page);
1363 vsie_page->fault_addr = 0;
1364 vsie_page->scb_s.ihcpu = 0xffffU;
1368 /* put a vsie page acquired via get_vsie_page */
1369 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1371 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1376 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1378 struct vsie_page *vsie_page;
1379 unsigned long scb_addr;
1382 vcpu->stat.instruction_sie++;
1383 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1385 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1386 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1388 BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
1389 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1391 /* 512 byte alignment */
1392 if (unlikely(scb_addr & 0x1ffUL))
1393 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1395 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0) ||
1396 kvm_s390_vcpu_sie_inhibited(vcpu))
1399 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1400 if (IS_ERR(vsie_page))
1401 return PTR_ERR(vsie_page);
1402 else if (!vsie_page)
1403 /* double use of sie control block - simply do nothing */
1406 rc = pin_scb(vcpu, vsie_page, scb_addr);
1409 rc = shadow_scb(vcpu, vsie_page);
1412 rc = pin_blocks(vcpu, vsie_page);
1415 register_shadow_scb(vcpu, vsie_page);
1416 rc = vsie_run(vcpu, vsie_page);
1417 unregister_shadow_scb(vcpu);
1418 unpin_blocks(vcpu, vsie_page);
1420 unshadow_scb(vcpu, vsie_page);
1422 unpin_scb(vcpu, vsie_page, scb_addr);
1424 put_vsie_page(vcpu->kvm, vsie_page);
1426 return rc < 0 ? rc : 0;
1429 /* Init the vsie data structures. To be called when a vm is initialized. */
1430 void kvm_s390_vsie_init(struct kvm *kvm)
1432 mutex_init(&kvm->arch.vsie.mutex);
1433 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL_ACCOUNT);
1436 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
1437 void kvm_s390_vsie_destroy(struct kvm *kvm)
1439 struct vsie_page *vsie_page;
1443 mutex_lock(&kvm->arch.vsie.mutex);
1444 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1445 page = kvm->arch.vsie.pages[i];
1446 kvm->arch.vsie.pages[i] = NULL;
1447 vsie_page = page_to_virt(page);
1448 release_gmap_shadow(vsie_page);
1449 /* free the radix tree entry */
1450 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1453 kvm->arch.vsie.page_count = 0;
1454 mutex_unlock(&kvm->arch.vsie.mutex);
1457 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1459 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1462 * Even if the VCPU lets go of the shadow sie block reference, it is
1463 * still valid in the cache. So we can safely kick it.
1466 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1467 if (scb->prog0c & PROG_IN_SIE)
1468 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);