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>
25 struct kvm_s390_sie_block scb_s; /* 0x0000 */
27 * the backup info for machine check. ensure it's at
28 * the same offset as that in struct sie_page!
30 struct mcck_volatile_info mcck_info; /* 0x0200 */
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.
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 */
52 /* trigger a validity icpt for the given scb */
53 static int set_validity_icpt(struct kvm_s390_sie_block *scb,
57 scb->ipb = ((__u32) reason_code) << 16;
58 scb->icptcode = ICPT_VALIDITY;
62 /* mark the prefix as unmapped, this will block the VSIE */
63 static void prefix_unmapped(struct vsie_page *vsie_page)
65 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
68 /* mark the prefix as unmapped and wait until the VSIE has been left */
69 static void prefix_unmapped_sync(struct vsie_page *vsie_page)
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)
78 /* mark the prefix as mapped, this will allow the VSIE to run */
79 static void prefix_mapped(struct vsie_page *vsie_page)
81 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
84 /* test if the prefix is mapped into the gmap shadow */
85 static int prefix_is_mapped(struct vsie_page *vsie_page)
87 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
90 /* copy the updated intervention request bits into the shadow scb */
91 static void update_intervention_requests(struct vsie_page *vsie_page)
93 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
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);
101 /* shadow (filter and validate) the cpuflags */
102 static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
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);
108 /* we don't allow ESA/390 guests */
109 if (!(cpuflags & CPUSTAT_ZARCH))
110 return set_validity_icpt(scb_s, 0x0001U);
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);
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;
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;
135 atomic_set(&scb_s->cpuflags, newflags);
140 * Create a shadow copy of the crycb block and setup key wrapping, if
141 * requested for guest 3 and enabled for guest 2.
143 * We only accept format-1 (no AP in g2), but convert it into format-2
144 * There is nothing to do for format-0.
146 * Returns: - 0 if shadowed or nothing to do
147 * - > 0 if control has to be given to guest 2
149 static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
151 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
152 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
153 const uint32_t crycbd_o = READ_ONCE(scb_o->crycbd);
154 const u32 crycb_addr = crycbd_o & 0x7ffffff8U;
155 unsigned long *b1, *b2;
159 if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
161 /* format-1 is supported with message-security-assist extension 3 */
162 if (!test_kvm_facility(vcpu->kvm, 76))
164 /* we may only allow it if enabled for guest 2 */
165 ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
166 (ECB3_AES | ECB3_DEA);
170 if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
171 return set_validity_icpt(scb_s, 0x003CU);
172 else if (!crycb_addr)
173 return set_validity_icpt(scb_s, 0x0039U);
175 /* copy only the wrapping keys */
176 if (read_guest_real(vcpu, crycb_addr + 72,
177 vsie_page->crycb.dea_wrapping_key_mask, 56))
178 return set_validity_icpt(scb_s, 0x0035U);
180 scb_s->ecb3 |= ecb3_flags;
181 scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
184 /* xor both blocks in one run */
185 b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
186 b2 = (unsigned long *)
187 vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
188 /* as 56%8 == 0, bitmap_xor won't overwrite any data */
189 bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
193 /* shadow (round up/down) the ibc to avoid validity icpt */
194 static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
196 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
197 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
198 /* READ_ONCE does not work on bitfields - use a temporary variable */
199 const uint32_t __new_ibc = scb_o->ibc;
200 const uint32_t new_ibc = READ_ONCE(__new_ibc) & 0x0fffU;
201 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
204 /* ibc installed in g2 and requested for g3 */
205 if (vcpu->kvm->arch.model.ibc && new_ibc) {
206 scb_s->ibc = new_ibc;
207 /* takte care of the minimum ibc level of the machine */
208 if (scb_s->ibc < min_ibc)
209 scb_s->ibc = min_ibc;
210 /* take care of the maximum ibc level set for the guest */
211 if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
212 scb_s->ibc = vcpu->kvm->arch.model.ibc;
216 /* unshadow the scb, copying parameters back to the real scb */
217 static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
219 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
220 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
223 scb_o->icptcode = scb_s->icptcode;
224 scb_o->icptstatus = scb_s->icptstatus;
225 scb_o->ipa = scb_s->ipa;
226 scb_o->ipb = scb_s->ipb;
227 scb_o->gbea = scb_s->gbea;
230 scb_o->cputm = scb_s->cputm;
231 scb_o->ckc = scb_s->ckc;
232 scb_o->todpr = scb_s->todpr;
235 scb_o->gpsw = scb_s->gpsw;
236 scb_o->gg14 = scb_s->gg14;
237 scb_o->gg15 = scb_s->gg15;
238 memcpy(scb_o->gcr, scb_s->gcr, 128);
239 scb_o->pp = scb_s->pp;
241 /* branch prediction */
242 if (test_kvm_facility(vcpu->kvm, 82)) {
243 scb_o->fpf &= ~FPF_BPBC;
244 scb_o->fpf |= scb_s->fpf & FPF_BPBC;
247 /* interrupt intercept */
248 switch (scb_s->icptcode) {
252 memcpy((void *)((u64)scb_o + 0xc0),
253 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
257 memcpy((void *)((u64)scb_o + 0xc0),
258 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
262 if (scb_s->ihcpu != 0xffffU)
263 scb_o->ihcpu = scb_s->ihcpu;
267 * Setup the shadow scb by copying and checking the relevant parts of the g2
270 * Returns: - 0 if the scb has been shadowed
271 * - > 0 if control has to be given to guest 2
273 static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
275 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
276 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
277 /* READ_ONCE does not work on bitfields - use a temporary variable */
278 const uint32_t __new_prefix = scb_o->prefix;
279 const uint32_t new_prefix = READ_ONCE(__new_prefix);
280 const bool wants_tx = READ_ONCE(scb_o->ecb) & ECB_TE;
281 bool had_tx = scb_s->ecb & ECB_TE;
282 unsigned long new_mso = 0;
285 /* make sure we don't have any leftovers when reusing the scb */
295 rc = prepare_cpuflags(vcpu, vsie_page);
300 scb_s->cputm = scb_o->cputm;
301 scb_s->ckc = scb_o->ckc;
302 scb_s->todpr = scb_o->todpr;
303 scb_s->epoch = scb_o->epoch;
306 scb_s->gpsw = scb_o->gpsw;
307 scb_s->gg14 = scb_o->gg14;
308 scb_s->gg15 = scb_o->gg15;
309 memcpy(scb_s->gcr, scb_o->gcr, 128);
310 scb_s->pp = scb_o->pp;
312 /* interception / execution handling */
313 scb_s->gbea = scb_o->gbea;
314 scb_s->lctl = scb_o->lctl;
315 scb_s->svcc = scb_o->svcc;
316 scb_s->ictl = scb_o->ictl;
318 * SKEY handling functions can't deal with false setting of PTE invalid
319 * bits. Therefore we cannot provide interpretation and would later
320 * have to provide own emulation handlers.
322 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
323 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
325 scb_s->icpua = scb_o->icpua;
327 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
328 new_mso = READ_ONCE(scb_o->mso) & 0xfffffffffff00000UL;
329 /* if the hva of the prefix changes, we have to remap the prefix */
330 if (scb_s->mso != new_mso || scb_s->prefix != new_prefix)
331 prefix_unmapped(vsie_page);
332 /* SIE will do mso/msl validity and exception checks for us */
333 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
334 scb_s->mso = new_mso;
335 scb_s->prefix = new_prefix;
337 /* We have to definetly flush the tlb if this scb never ran */
338 if (scb_s->ihcpu != 0xffffU)
339 scb_s->ihcpu = scb_o->ihcpu;
341 /* MVPG and Protection Exception Interpretation are always available */
342 scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
343 /* Host-protection-interruption introduced with ESOP */
344 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
345 scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
346 /* transactional execution */
347 if (test_kvm_facility(vcpu->kvm, 73) && wants_tx) {
348 /* remap the prefix is tx is toggled on */
350 prefix_unmapped(vsie_page);
351 scb_s->ecb |= ECB_TE;
353 /* branch prediction */
354 if (test_kvm_facility(vcpu->kvm, 82))
355 scb_s->fpf |= scb_o->fpf & FPF_BPBC;
357 if (test_kvm_facility(vcpu->kvm, 129)) {
358 scb_s->eca |= scb_o->eca & ECA_VX;
359 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
361 /* Run-time-Instrumentation */
362 if (test_kvm_facility(vcpu->kvm, 64))
363 scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
364 /* Instruction Execution Prevention */
365 if (test_kvm_facility(vcpu->kvm, 130))
366 scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
367 /* Guarded Storage */
368 if (test_kvm_facility(vcpu->kvm, 133)) {
369 scb_s->ecb |= scb_o->ecb & ECB_GS;
370 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
372 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
373 scb_s->eca |= scb_o->eca & ECA_SII;
374 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
375 scb_s->eca |= scb_o->eca & ECA_IB;
376 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
377 scb_s->eca |= scb_o->eca & ECA_CEI;
378 /* Epoch Extension */
379 if (test_kvm_facility(vcpu->kvm, 139)) {
380 scb_s->ecd |= scb_o->ecd & ECD_MEF;
381 scb_s->epdx = scb_o->epdx;
385 if (test_kvm_facility(vcpu->kvm, 156))
386 scb_s->ecd |= scb_o->ecd & ECD_ETOKENF;
388 prepare_ibc(vcpu, vsie_page);
389 rc = shadow_crycb(vcpu, vsie_page);
392 unshadow_scb(vcpu, vsie_page);
396 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
399 struct kvm *kvm = gmap->private;
400 struct vsie_page *cur;
401 unsigned long prefix;
405 if (!gmap_is_shadow(gmap))
407 if (start >= 1UL << 31)
408 /* We are only interested in prefix pages */
412 * Only new shadow blocks are added to the list during runtime,
413 * therefore we can safely reference them all the time.
415 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
416 page = READ_ONCE(kvm->arch.vsie.pages[i]);
419 cur = page_to_virt(page);
420 if (READ_ONCE(cur->gmap) != gmap)
422 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
423 /* with mso/msl, the prefix lies at an offset */
424 prefix += cur->scb_s.mso;
425 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
426 prefix_unmapped_sync(cur);
431 * Map the first prefix page and if tx is enabled also the second prefix page.
433 * The prefix will be protected, a gmap notifier will inform about unmaps.
434 * The shadow scb must not be executed until the prefix is remapped, this is
435 * guaranteed by properly handling PROG_REQUEST.
437 * Returns: - 0 on if successfully mapped or already mapped
438 * - > 0 if control has to be given to guest 2
439 * - -EAGAIN if the caller can retry immediately
440 * - -ENOMEM if out of memory
442 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
444 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
445 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
448 if (prefix_is_mapped(vsie_page))
451 /* mark it as mapped so we can catch any concurrent unmappers */
452 prefix_mapped(vsie_page);
454 /* with mso/msl, the prefix lies at offset *mso* */
455 prefix += scb_s->mso;
457 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
458 if (!rc && (scb_s->ecb & ECB_TE))
459 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
462 * We don't have to mprotect, we will be called for all unshadows.
463 * SIE will detect if protection applies and trigger a validity.
466 prefix_unmapped(vsie_page);
467 if (rc > 0 || rc == -EFAULT)
468 rc = set_validity_icpt(scb_s, 0x0037U);
473 * Pin the guest page given by gpa and set hpa to the pinned host address.
474 * Will always be pinned writable.
476 * Returns: - 0 on success
477 * - -EINVAL if the gpa is not valid guest storage
479 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
483 page = gfn_to_page(kvm, gpa_to_gfn(gpa));
484 if (is_error_page(page))
486 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
490 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
491 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
493 kvm_release_pfn_dirty(hpa >> PAGE_SHIFT);
494 /* mark the page always as dirty for migration */
495 mark_page_dirty(kvm, gpa_to_gfn(gpa));
498 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
499 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
501 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
504 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
506 unpin_guest_page(vcpu->kvm, vsie_page->sca_gpa, hpa);
507 vsie_page->sca_gpa = 0;
514 unpin_guest_page(vcpu->kvm, vsie_page->itdba_gpa, hpa);
515 vsie_page->itdba_gpa = 0;
521 unpin_guest_page(vcpu->kvm, vsie_page->gvrd_gpa, hpa);
522 vsie_page->gvrd_gpa = 0;
528 unpin_guest_page(vcpu->kvm, vsie_page->riccbd_gpa, hpa);
529 vsie_page->riccbd_gpa = 0;
535 unpin_guest_page(vcpu->kvm, vsie_page->sdnx_gpa, hpa);
536 vsie_page->sdnx_gpa = 0;
542 * Instead of shadowing some blocks, we can simply forward them because the
543 * addresses in the scb are 64 bit long.
545 * This works as long as the data lies in one page. If blocks ever exceed one
546 * page, we have to fall back to shadowing.
548 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
549 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
551 * Returns: - 0 if all blocks were pinned.
552 * - > 0 if control has to be given to guest 2
553 * - -ENOMEM if out of memory
555 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
557 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
558 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
563 gpa = READ_ONCE(scb_o->scaol) & ~0xfUL;
564 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
565 gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32;
567 if (gpa < 2 * PAGE_SIZE)
568 rc = set_validity_icpt(scb_s, 0x0038U);
569 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
570 rc = set_validity_icpt(scb_s, 0x0011U);
571 else if ((gpa & PAGE_MASK) !=
572 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
573 rc = set_validity_icpt(scb_s, 0x003bU);
575 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
577 rc = set_validity_icpt(scb_s, 0x0034U);
581 vsie_page->sca_gpa = gpa;
582 scb_s->scaoh = (u32)((u64)hpa >> 32);
583 scb_s->scaol = (u32)(u64)hpa;
586 gpa = READ_ONCE(scb_o->itdba) & ~0xffUL;
587 if (gpa && (scb_s->ecb & ECB_TE)) {
588 if (gpa < 2 * PAGE_SIZE) {
589 rc = set_validity_icpt(scb_s, 0x0080U);
592 /* 256 bytes cannot cross page boundaries */
593 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
595 rc = set_validity_icpt(scb_s, 0x0080U);
598 vsie_page->itdba_gpa = gpa;
602 gpa = READ_ONCE(scb_o->gvrd) & ~0x1ffUL;
603 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
604 if (gpa < 2 * PAGE_SIZE) {
605 rc = set_validity_icpt(scb_s, 0x1310U);
609 * 512 bytes vector registers cannot cross page boundaries
610 * if this block gets bigger, we have to shadow it.
612 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
614 rc = set_validity_icpt(scb_s, 0x1310U);
617 vsie_page->gvrd_gpa = gpa;
621 gpa = READ_ONCE(scb_o->riccbd) & ~0x3fUL;
622 if (gpa && (scb_s->ecb3 & ECB3_RI)) {
623 if (gpa < 2 * PAGE_SIZE) {
624 rc = set_validity_icpt(scb_s, 0x0043U);
627 /* 64 bytes cannot cross page boundaries */
628 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
630 rc = set_validity_icpt(scb_s, 0x0043U);
633 /* Validity 0x0044 will be checked by SIE */
634 vsie_page->riccbd_gpa = gpa;
637 if (((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) ||
638 (scb_s->ecd & ECD_ETOKENF)) {
641 gpa = READ_ONCE(scb_o->sdnxo) & ~0xfUL;
642 sdnxc = READ_ONCE(scb_o->sdnxo) & 0xfUL;
643 if (!gpa || gpa < 2 * PAGE_SIZE) {
644 rc = set_validity_icpt(scb_s, 0x10b0U);
647 if (sdnxc < 6 || sdnxc > 12) {
648 rc = set_validity_icpt(scb_s, 0x10b1U);
651 if (gpa & ((1 << sdnxc) - 1)) {
652 rc = set_validity_icpt(scb_s, 0x10b2U);
655 /* Due to alignment rules (checked above) this cannot
656 * cross page boundaries
658 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
660 rc = set_validity_icpt(scb_s, 0x10b0U);
663 vsie_page->sdnx_gpa = gpa;
664 scb_s->sdnxo = hpa | sdnxc;
668 unpin_blocks(vcpu, vsie_page);
672 /* unpin the scb provided by guest 2, marking it as dirty */
673 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
676 hpa_t hpa = (hpa_t) vsie_page->scb_o;
679 unpin_guest_page(vcpu->kvm, gpa, hpa);
680 vsie_page->scb_o = NULL;
684 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
686 * Returns: - 0 if the scb was pinned.
687 * - > 0 if control has to be given to guest 2
689 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
695 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
697 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
701 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
706 * Inject a fault into guest 2.
708 * Returns: - > 0 if control has to be given to guest 2
709 * < 0 if an error occurred during injection.
711 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
714 struct kvm_s390_pgm_info pgm = {
717 /* 0-51: virtual address */
718 (vaddr & 0xfffffffffffff000UL) |
719 /* 52-53: store / fetch */
720 (((unsigned int) !write_flag) + 1) << 10,
721 /* 62-63: asce id (alway primary == 0) */
722 .exc_access_id = 0, /* always primary */
723 .op_access_id = 0, /* not MVPG */
727 if (code == PGM_PROTECTION)
728 pgm.trans_exc_code |= 0x4UL;
730 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
735 * Handle a fault during vsie execution on a gmap shadow.
737 * Returns: - 0 if the fault was resolved
738 * - > 0 if control has to be given to guest 2
739 * - < 0 if an error occurred
741 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
745 if (current->thread.gmap_int_code == PGM_PROTECTION)
746 /* we can directly forward all protection exceptions */
747 return inject_fault(vcpu, PGM_PROTECTION,
748 current->thread.gmap_addr, 1);
750 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
751 current->thread.gmap_addr);
753 rc = inject_fault(vcpu, rc,
754 current->thread.gmap_addr,
755 current->thread.gmap_write_flag);
757 vsie_page->fault_addr = current->thread.gmap_addr;
763 * Retry the previous fault that required guest 2 intervention. This avoids
764 * one superfluous SIE re-entry and direct exit.
766 * Will ignore any errors. The next SIE fault will do proper fault handling.
768 static void handle_last_fault(struct kvm_vcpu *vcpu,
769 struct vsie_page *vsie_page)
771 if (vsie_page->fault_addr)
772 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
773 vsie_page->fault_addr);
774 vsie_page->fault_addr = 0;
777 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
779 vsie_page->scb_s.icptcode = 0;
782 /* rewind the psw and clear the vsie icpt, so we can retry execution */
783 static void retry_vsie_icpt(struct vsie_page *vsie_page)
785 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
786 int ilen = insn_length(scb_s->ipa >> 8);
788 /* take care of EXECUTE instructions */
789 if (scb_s->icptstatus & 1) {
790 ilen = (scb_s->icptstatus >> 4) & 0x6;
794 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
795 clear_vsie_icpt(vsie_page);
799 * Try to shadow + enable the guest 2 provided facility list.
800 * Retry instruction execution if enabled for and provided by guest 2.
802 * Returns: - 0 if handled (retry or guest 2 icpt)
803 * - > 0 if control has to be given to guest 2
805 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
807 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
808 __u32 fac = READ_ONCE(vsie_page->scb_o->fac) & 0x7ffffff8U;
810 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
811 retry_vsie_icpt(vsie_page);
812 if (read_guest_real(vcpu, fac, &vsie_page->fac,
813 sizeof(vsie_page->fac)))
814 return set_validity_icpt(scb_s, 0x1090U);
815 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
821 * Run the vsie on a shadow scb and a shadow gmap, without any further
822 * sanity checks, handling SIE faults.
824 * Returns: - 0 everything went fine
825 * - > 0 if control has to be given to guest 2
826 * - < 0 if an error occurred
828 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
829 __releases(vcpu->kvm->srcu)
830 __acquires(vcpu->kvm->srcu)
832 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
833 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
834 int guest_bp_isolation;
837 handle_last_fault(vcpu, vsie_page);
841 if (test_cpu_flag(CIF_MCCK_PENDING))
844 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
846 /* save current guest state of bp isolation override */
847 guest_bp_isolation = test_thread_flag(TIF_ISOLATE_BP_GUEST);
850 * The guest is running with BPBC, so we have to force it on for our
851 * nested guest. This is done by enabling BPBC globally, so the BPBC
852 * control in the SCB (which the nested guest can modify) is simply
855 if (test_kvm_facility(vcpu->kvm, 82) &&
856 vcpu->arch.sie_block->fpf & FPF_BPBC)
857 set_thread_flag(TIF_ISOLATE_BP_GUEST);
860 guest_enter_irqoff();
863 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
869 /* restore guest state for bp isolation override */
870 if (!guest_bp_isolation)
871 clear_thread_flag(TIF_ISOLATE_BP_GUEST);
873 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
876 VCPU_EVENT(vcpu, 3, "%s", "machine check");
877 kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
882 rc = 0; /* we could still have an icpt */
883 else if (rc == -EFAULT)
884 return handle_fault(vcpu, vsie_page);
886 switch (scb_s->icptcode) {
888 if (scb_s->ipa == 0xb2b0)
889 rc = handle_stfle(vcpu, vsie_page);
892 /* stop not requested by g2 - must have been a kick */
893 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
894 clear_vsie_icpt(vsie_page);
897 if ((scb_s->ipa & 0xf000) != 0xf000)
898 scb_s->ipa += 0x1000;
904 static void release_gmap_shadow(struct vsie_page *vsie_page)
907 gmap_put(vsie_page->gmap);
908 WRITE_ONCE(vsie_page->gmap, NULL);
909 prefix_unmapped(vsie_page);
912 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
913 struct vsie_page *vsie_page)
920 asce = vcpu->arch.sie_block->gcr[1];
921 cr0.val = vcpu->arch.sie_block->gcr[0];
922 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
923 edat += edat && test_kvm_facility(vcpu->kvm, 78);
926 * ASCE or EDAT could have changed since last icpt, or the gmap
927 * we're holding has been unshadowed. If the gmap is still valid,
928 * we can safely reuse it.
930 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
933 /* release the old shadow - if any, and mark the prefix as unmapped */
934 release_gmap_shadow(vsie_page);
935 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
937 return PTR_ERR(gmap);
938 gmap->private = vcpu->kvm;
939 WRITE_ONCE(vsie_page->gmap, gmap);
944 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
946 static void register_shadow_scb(struct kvm_vcpu *vcpu,
947 struct vsie_page *vsie_page)
949 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
951 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
953 * External calls have to lead to a kick of the vcpu and
954 * therefore the vsie -> Simulate Wait state.
956 kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
958 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
959 * automatically be adjusted on tod clock changes via kvm_sync_clock.
962 scb_s->epoch += vcpu->kvm->arch.epoch;
964 if (scb_s->ecd & ECD_MEF) {
965 scb_s->epdx += vcpu->kvm->arch.epdx;
966 if (scb_s->epoch < vcpu->kvm->arch.epoch)
974 * Unregister a shadow scb from a VCPU.
976 static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
978 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_WAIT);
979 WRITE_ONCE(vcpu->arch.vsie_block, NULL);
983 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
984 * prefix pages and faults.
986 * Returns: - 0 if no errors occurred
987 * - > 0 if control has to be given to guest 2
988 * - -ENOMEM if out of memory
990 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
992 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
996 rc = acquire_gmap_shadow(vcpu, vsie_page);
998 rc = map_prefix(vcpu, vsie_page);
1000 gmap_enable(vsie_page->gmap);
1001 update_intervention_requests(vsie_page);
1002 rc = do_vsie_run(vcpu, vsie_page);
1003 gmap_enable(vcpu->arch.gmap);
1005 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
1009 if (rc || scb_s->icptcode || signal_pending(current) ||
1010 kvm_s390_vcpu_has_irq(vcpu, 0))
1014 if (rc == -EFAULT) {
1016 * Addressing exceptions are always presentes as intercepts.
1017 * As addressing exceptions are suppressing and our guest 3 PSW
1018 * points at the responsible instruction, we have to
1019 * forward the PSW and set the ilc. If we can't read guest 3
1020 * instruction, we can use an arbitrary ilc. Let's always use
1021 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
1022 * memory. (we could also fake the shadow so the hardware
1025 scb_s->icptcode = ICPT_PROGI;
1026 scb_s->iprcc = PGM_ADDRESSING;
1028 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
1035 * Get or create a vsie page for a scb address.
1037 * Returns: - address of a vsie page (cached or new one)
1038 * - NULL if the same scb address is already used by another VCPU
1039 * - ERR_PTR(-ENOMEM) if out of memory
1041 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
1043 struct vsie_page *vsie_page;
1048 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1051 if (page_ref_inc_return(page) == 2)
1052 return page_to_virt(page);
1057 * We want at least #online_vcpus shadows, so every VCPU can execute
1058 * the VSIE in parallel.
1060 nr_vcpus = atomic_read(&kvm->online_vcpus);
1062 mutex_lock(&kvm->arch.vsie.mutex);
1063 if (kvm->arch.vsie.page_count < nr_vcpus) {
1064 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
1066 mutex_unlock(&kvm->arch.vsie.mutex);
1067 return ERR_PTR(-ENOMEM);
1070 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1071 kvm->arch.vsie.page_count++;
1073 /* reuse an existing entry that belongs to nobody */
1075 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1076 if (page_ref_inc_return(page) == 2)
1079 kvm->arch.vsie.next++;
1080 kvm->arch.vsie.next %= nr_vcpus;
1082 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1085 /* double use of the same address */
1086 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1088 mutex_unlock(&kvm->arch.vsie.mutex);
1091 mutex_unlock(&kvm->arch.vsie.mutex);
1093 vsie_page = page_to_virt(page);
1094 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
1095 release_gmap_shadow(vsie_page);
1096 vsie_page->fault_addr = 0;
1097 vsie_page->scb_s.ihcpu = 0xffffU;
1101 /* put a vsie page acquired via get_vsie_page */
1102 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1104 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1109 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1111 struct vsie_page *vsie_page;
1112 unsigned long scb_addr;
1115 vcpu->stat.instruction_sie++;
1116 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1118 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1119 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1121 BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
1122 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1124 /* 512 byte alignment */
1125 if (unlikely(scb_addr & 0x1ffUL))
1126 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1128 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1131 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1132 if (IS_ERR(vsie_page))
1133 return PTR_ERR(vsie_page);
1134 else if (!vsie_page)
1135 /* double use of sie control block - simply do nothing */
1138 rc = pin_scb(vcpu, vsie_page, scb_addr);
1141 rc = shadow_scb(vcpu, vsie_page);
1144 rc = pin_blocks(vcpu, vsie_page);
1147 register_shadow_scb(vcpu, vsie_page);
1148 rc = vsie_run(vcpu, vsie_page);
1149 unregister_shadow_scb(vcpu);
1150 unpin_blocks(vcpu, vsie_page);
1152 unshadow_scb(vcpu, vsie_page);
1154 unpin_scb(vcpu, vsie_page, scb_addr);
1156 put_vsie_page(vcpu->kvm, vsie_page);
1158 return rc < 0 ? rc : 0;
1161 /* Init the vsie data structures. To be called when a vm is initialized. */
1162 void kvm_s390_vsie_init(struct kvm *kvm)
1164 mutex_init(&kvm->arch.vsie.mutex);
1165 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1168 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
1169 void kvm_s390_vsie_destroy(struct kvm *kvm)
1171 struct vsie_page *vsie_page;
1175 mutex_lock(&kvm->arch.vsie.mutex);
1176 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1177 page = kvm->arch.vsie.pages[i];
1178 kvm->arch.vsie.pages[i] = NULL;
1179 vsie_page = page_to_virt(page);
1180 release_gmap_shadow(vsie_page);
1181 /* free the radix tree entry */
1182 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1185 kvm->arch.vsie.page_count = 0;
1186 mutex_unlock(&kvm->arch.vsie.mutex);
1189 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1191 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1194 * Even if the VCPU lets go of the shadow sie block reference, it is
1195 * still valid in the cache. So we can safely kick it.
1198 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1199 if (scb->prog0c & PROG_IN_SIE)
1200 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);