2 * kvm nested virtualization support for s390x
4 * Copyright IBM Corp. 2016
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
10 * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
12 #include <linux/vmalloc.h>
13 #include <linux/kvm_host.h>
14 #include <linux/bug.h>
15 #include <linux/list.h>
16 #include <linux/bitmap.h>
17 #include <linux/sched/signal.h>
20 #include <asm/mmu_context.h>
28 struct kvm_s390_sie_block scb_s; /* 0x0000 */
30 * the backup info for machine check. ensure it's at
31 * the same offset as that in struct sie_page!
33 struct mcck_volatile_info mcck_info; /* 0x0200 */
35 * The pinned original scb. Be aware that other VCPUs can modify
36 * it while we read from it. Values that are used for conditions or
37 * are reused conditionally, should be accessed via READ_ONCE.
39 struct kvm_s390_sie_block *scb_o; /* 0x0218 */
40 /* the shadow gmap in use by the vsie_page */
41 struct gmap *gmap; /* 0x0220 */
42 /* address of the last reported fault to guest2 */
43 unsigned long fault_addr; /* 0x0228 */
44 __u8 reserved[0x0700 - 0x0230]; /* 0x0230 */
45 struct kvm_s390_crypto_cb crycb; /* 0x0700 */
46 __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */
49 /* trigger a validity icpt for the given scb */
50 static int set_validity_icpt(struct kvm_s390_sie_block *scb,
54 scb->ipb = ((__u32) reason_code) << 16;
55 scb->icptcode = ICPT_VALIDITY;
59 /* mark the prefix as unmapped, this will block the VSIE */
60 static void prefix_unmapped(struct vsie_page *vsie_page)
62 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
65 /* mark the prefix as unmapped and wait until the VSIE has been left */
66 static void prefix_unmapped_sync(struct vsie_page *vsie_page)
68 prefix_unmapped(vsie_page);
69 if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
70 atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
71 while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
75 /* mark the prefix as mapped, this will allow the VSIE to run */
76 static void prefix_mapped(struct vsie_page *vsie_page)
78 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
81 /* test if the prefix is mapped into the gmap shadow */
82 static int prefix_is_mapped(struct vsie_page *vsie_page)
84 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
87 /* copy the updated intervention request bits into the shadow scb */
88 static void update_intervention_requests(struct vsie_page *vsie_page)
90 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
93 cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
94 atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
95 atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
98 /* shadow (filter and validate) the cpuflags */
99 static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
101 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
102 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
103 int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
105 /* we don't allow ESA/390 guests */
106 if (!(cpuflags & CPUSTAT_ZARCH))
107 return set_validity_icpt(scb_s, 0x0001U);
109 if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
110 return set_validity_icpt(scb_s, 0x0001U);
111 else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
112 return set_validity_icpt(scb_s, 0x0007U);
114 /* intervention requests will be set later */
115 newflags = CPUSTAT_ZARCH;
116 if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8))
117 newflags |= CPUSTAT_GED;
118 if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) {
119 if (cpuflags & CPUSTAT_GED)
120 return set_validity_icpt(scb_s, 0x0001U);
121 newflags |= CPUSTAT_GED2;
123 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE))
124 newflags |= cpuflags & CPUSTAT_P;
125 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS))
126 newflags |= cpuflags & CPUSTAT_SM;
127 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS))
128 newflags |= cpuflags & CPUSTAT_IBS;
129 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_KSS))
130 newflags |= cpuflags & CPUSTAT_KSS;
132 atomic_set(&scb_s->cpuflags, newflags);
137 * Create a shadow copy of the crycb block and setup key wrapping, if
138 * requested for guest 3 and enabled for guest 2.
140 * We only accept format-1 (no AP in g2), but convert it into format-2
141 * There is nothing to do for format-0.
143 * Returns: - 0 if shadowed or nothing to do
144 * - > 0 if control has to be given to guest 2
146 static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
148 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
149 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
150 const uint32_t crycbd_o = READ_ONCE(scb_o->crycbd);
151 const u32 crycb_addr = crycbd_o & 0x7ffffff8U;
152 unsigned long *b1, *b2;
156 if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
158 /* format-1 is supported with message-security-assist extension 3 */
159 if (!test_kvm_facility(vcpu->kvm, 76))
161 /* we may only allow it if enabled for guest 2 */
162 ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
163 (ECB3_AES | ECB3_DEA);
167 if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
168 return set_validity_icpt(scb_s, 0x003CU);
169 else if (!crycb_addr)
170 return set_validity_icpt(scb_s, 0x0039U);
172 /* copy only the wrapping keys */
173 if (read_guest_real(vcpu, crycb_addr + 72,
174 vsie_page->crycb.dea_wrapping_key_mask, 56))
175 return set_validity_icpt(scb_s, 0x0035U);
177 scb_s->ecb3 |= ecb3_flags;
178 scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
181 /* xor both blocks in one run */
182 b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
183 b2 = (unsigned long *)
184 vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
185 /* as 56%8 == 0, bitmap_xor won't overwrite any data */
186 bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
190 /* shadow (round up/down) the ibc to avoid validity icpt */
191 static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
193 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
194 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
195 /* READ_ONCE does not work on bitfields - use a temporary variable */
196 const uint32_t __new_ibc = scb_o->ibc;
197 const uint32_t new_ibc = READ_ONCE(__new_ibc) & 0x0fffU;
198 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
201 /* ibc installed in g2 and requested for g3 */
202 if (vcpu->kvm->arch.model.ibc && new_ibc) {
203 scb_s->ibc = new_ibc;
204 /* takte care of the minimum ibc level of the machine */
205 if (scb_s->ibc < min_ibc)
206 scb_s->ibc = min_ibc;
207 /* take care of the maximum ibc level set for the guest */
208 if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
209 scb_s->ibc = vcpu->kvm->arch.model.ibc;
213 /* unshadow the scb, copying parameters back to the real scb */
214 static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
216 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
217 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
220 scb_o->icptcode = scb_s->icptcode;
221 scb_o->icptstatus = scb_s->icptstatus;
222 scb_o->ipa = scb_s->ipa;
223 scb_o->ipb = scb_s->ipb;
224 scb_o->gbea = scb_s->gbea;
227 scb_o->cputm = scb_s->cputm;
228 scb_o->ckc = scb_s->ckc;
229 scb_o->todpr = scb_s->todpr;
232 scb_o->gpsw = scb_s->gpsw;
233 scb_o->gg14 = scb_s->gg14;
234 scb_o->gg15 = scb_s->gg15;
235 memcpy(scb_o->gcr, scb_s->gcr, 128);
236 scb_o->pp = scb_s->pp;
238 /* branch prediction */
239 if (test_kvm_facility(vcpu->kvm, 82)) {
240 scb_o->fpf &= ~FPF_BPBC;
241 scb_o->fpf |= scb_s->fpf & FPF_BPBC;
244 /* interrupt intercept */
245 switch (scb_s->icptcode) {
249 memcpy((void *)((u64)scb_o + 0xc0),
250 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
254 memcpy((void *)((u64)scb_o + 0xc0),
255 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
259 if (scb_s->ihcpu != 0xffffU)
260 scb_o->ihcpu = scb_s->ihcpu;
264 * Setup the shadow scb by copying and checking the relevant parts of the g2
267 * Returns: - 0 if the scb has been shadowed
268 * - > 0 if control has to be given to guest 2
270 static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
272 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
273 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
274 /* READ_ONCE does not work on bitfields - use a temporary variable */
275 const uint32_t __new_prefix = scb_o->prefix;
276 const uint32_t new_prefix = READ_ONCE(__new_prefix);
277 const bool wants_tx = READ_ONCE(scb_o->ecb) & ECB_TE;
278 bool had_tx = scb_s->ecb & ECB_TE;
279 unsigned long new_mso = 0;
282 /* make sure we don't have any leftovers when reusing the scb */
292 rc = prepare_cpuflags(vcpu, vsie_page);
297 scb_s->cputm = scb_o->cputm;
298 scb_s->ckc = scb_o->ckc;
299 scb_s->todpr = scb_o->todpr;
300 scb_s->epoch = scb_o->epoch;
303 scb_s->gpsw = scb_o->gpsw;
304 scb_s->gg14 = scb_o->gg14;
305 scb_s->gg15 = scb_o->gg15;
306 memcpy(scb_s->gcr, scb_o->gcr, 128);
307 scb_s->pp = scb_o->pp;
309 /* interception / execution handling */
310 scb_s->gbea = scb_o->gbea;
311 scb_s->lctl = scb_o->lctl;
312 scb_s->svcc = scb_o->svcc;
313 scb_s->ictl = scb_o->ictl;
315 * SKEY handling functions can't deal with false setting of PTE invalid
316 * bits. Therefore we cannot provide interpretation and would later
317 * have to provide own emulation handlers.
319 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
320 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
322 scb_s->icpua = scb_o->icpua;
324 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
325 new_mso = READ_ONCE(scb_o->mso) & 0xfffffffffff00000UL;
326 /* if the hva of the prefix changes, we have to remap the prefix */
327 if (scb_s->mso != new_mso || scb_s->prefix != new_prefix)
328 prefix_unmapped(vsie_page);
329 /* SIE will do mso/msl validity and exception checks for us */
330 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
331 scb_s->mso = new_mso;
332 scb_s->prefix = new_prefix;
334 /* We have to definetly flush the tlb if this scb never ran */
335 if (scb_s->ihcpu != 0xffffU)
336 scb_s->ihcpu = scb_o->ihcpu;
338 /* MVPG and Protection Exception Interpretation are always available */
339 scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
340 /* Host-protection-interruption introduced with ESOP */
341 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
342 scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
343 /* transactional execution */
344 if (test_kvm_facility(vcpu->kvm, 73) && wants_tx) {
345 /* remap the prefix is tx is toggled on */
347 prefix_unmapped(vsie_page);
348 scb_s->ecb |= ECB_TE;
350 /* branch prediction */
351 if (test_kvm_facility(vcpu->kvm, 82))
352 scb_s->fpf |= scb_o->fpf & FPF_BPBC;
354 if (test_kvm_facility(vcpu->kvm, 129)) {
355 scb_s->eca |= scb_o->eca & ECA_VX;
356 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
358 /* Run-time-Instrumentation */
359 if (test_kvm_facility(vcpu->kvm, 64))
360 scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
361 /* Instruction Execution Prevention */
362 if (test_kvm_facility(vcpu->kvm, 130))
363 scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
364 /* Guarded Storage */
365 if (test_kvm_facility(vcpu->kvm, 133)) {
366 scb_s->ecb |= scb_o->ecb & ECB_GS;
367 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
369 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
370 scb_s->eca |= scb_o->eca & ECA_SII;
371 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
372 scb_s->eca |= scb_o->eca & ECA_IB;
373 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
374 scb_s->eca |= scb_o->eca & ECA_CEI;
375 /* Epoch Extension */
376 if (test_kvm_facility(vcpu->kvm, 139))
377 scb_s->ecd |= scb_o->ecd & ECD_MEF;
379 prepare_ibc(vcpu, vsie_page);
380 rc = shadow_crycb(vcpu, vsie_page);
383 unshadow_scb(vcpu, vsie_page);
387 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
390 struct kvm *kvm = gmap->private;
391 struct vsie_page *cur;
392 unsigned long prefix;
396 if (!gmap_is_shadow(gmap))
398 if (start >= 1UL << 31)
399 /* We are only interested in prefix pages */
403 * Only new shadow blocks are added to the list during runtime,
404 * therefore we can safely reference them all the time.
406 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
407 page = READ_ONCE(kvm->arch.vsie.pages[i]);
410 cur = page_to_virt(page);
411 if (READ_ONCE(cur->gmap) != gmap)
413 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
414 /* with mso/msl, the prefix lies at an offset */
415 prefix += cur->scb_s.mso;
416 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
417 prefix_unmapped_sync(cur);
422 * Map the first prefix page and if tx is enabled also the second prefix page.
424 * The prefix will be protected, a gmap notifier will inform about unmaps.
425 * The shadow scb must not be executed until the prefix is remapped, this is
426 * guaranteed by properly handling PROG_REQUEST.
428 * Returns: - 0 on if successfully mapped or already mapped
429 * - > 0 if control has to be given to guest 2
430 * - -EAGAIN if the caller can retry immediately
431 * - -ENOMEM if out of memory
433 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
435 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
436 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
439 if (prefix_is_mapped(vsie_page))
442 /* mark it as mapped so we can catch any concurrent unmappers */
443 prefix_mapped(vsie_page);
445 /* with mso/msl, the prefix lies at offset *mso* */
446 prefix += scb_s->mso;
448 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
449 if (!rc && (scb_s->ecb & ECB_TE))
450 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
453 * We don't have to mprotect, we will be called for all unshadows.
454 * SIE will detect if protection applies and trigger a validity.
457 prefix_unmapped(vsie_page);
458 if (rc > 0 || rc == -EFAULT)
459 rc = set_validity_icpt(scb_s, 0x0037U);
464 * Pin the guest page given by gpa and set hpa to the pinned host address.
465 * Will always be pinned writable.
467 * Returns: - 0 on success
468 * - -EINVAL if the gpa is not valid guest storage
469 * - -ENOMEM if out of memory
471 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
477 hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
478 if (kvm_is_error_hva(hva))
480 rc = get_user_pages_fast(hva, 1, 1, &page);
485 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
489 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
490 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
494 page = virt_to_page(hpa);
495 set_page_dirty_lock(page);
497 /* mark the page always as dirty for migration */
498 mark_page_dirty(kvm, gpa_to_gfn(gpa));
501 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
502 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
504 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
505 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
509 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
511 gpa = scb_o->scaol & ~0xfUL;
512 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
513 gpa |= (u64) scb_o->scaoh << 32;
514 unpin_guest_page(vcpu->kvm, gpa, hpa);
521 gpa = scb_o->itdba & ~0xffUL;
522 unpin_guest_page(vcpu->kvm, gpa, hpa);
528 gpa = scb_o->gvrd & ~0x1ffUL;
529 unpin_guest_page(vcpu->kvm, gpa, hpa);
535 gpa = scb_o->riccbd & ~0x3fUL;
536 unpin_guest_page(vcpu->kvm, gpa, hpa);
543 unpin_guest_page(vcpu->kvm, gpa, hpa);
549 * Instead of shadowing some blocks, we can simply forward them because the
550 * addresses in the scb are 64 bit long.
552 * This works as long as the data lies in one page. If blocks ever exceed one
553 * page, we have to fall back to shadowing.
555 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
556 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
558 * Returns: - 0 if all blocks were pinned.
559 * - > 0 if control has to be given to guest 2
560 * - -ENOMEM if out of memory
562 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
564 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
565 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
570 gpa = READ_ONCE(scb_o->scaol) & ~0xfUL;
571 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
572 gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32;
574 if (!(gpa & ~0x1fffUL))
575 rc = set_validity_icpt(scb_s, 0x0038U);
576 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
577 rc = set_validity_icpt(scb_s, 0x0011U);
578 else if ((gpa & PAGE_MASK) !=
579 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
580 rc = set_validity_icpt(scb_s, 0x003bU);
582 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
584 rc = set_validity_icpt(scb_s, 0x0034U);
588 scb_s->scaoh = (u32)((u64)hpa >> 32);
589 scb_s->scaol = (u32)(u64)hpa;
592 gpa = READ_ONCE(scb_o->itdba) & ~0xffUL;
593 if (gpa && (scb_s->ecb & ECB_TE)) {
594 if (!(gpa & ~0x1fffUL)) {
595 rc = set_validity_icpt(scb_s, 0x0080U);
598 /* 256 bytes cannot cross page boundaries */
599 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
601 rc = set_validity_icpt(scb_s, 0x0080U);
607 gpa = READ_ONCE(scb_o->gvrd) & ~0x1ffUL;
608 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
609 if (!(gpa & ~0x1fffUL)) {
610 rc = set_validity_icpt(scb_s, 0x1310U);
614 * 512 bytes vector registers cannot cross page boundaries
615 * if this block gets bigger, we have to shadow it.
617 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
619 rc = set_validity_icpt(scb_s, 0x1310U);
625 gpa = READ_ONCE(scb_o->riccbd) & ~0x3fUL;
626 if (gpa && (scb_s->ecb3 & ECB3_RI)) {
627 if (!(gpa & ~0x1fffUL)) {
628 rc = set_validity_icpt(scb_s, 0x0043U);
631 /* 64 bytes cannot cross page boundaries */
632 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
634 rc = set_validity_icpt(scb_s, 0x0043U);
635 /* Validity 0x0044 will be checked by SIE */
640 if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
643 gpa = READ_ONCE(scb_o->sdnxo) & ~0xfUL;
644 sdnxc = READ_ONCE(scb_o->sdnxo) & 0xfUL;
645 if (!gpa || !(gpa & ~0x1fffUL)) {
646 rc = set_validity_icpt(scb_s, 0x10b0U);
649 if (sdnxc < 6 || sdnxc > 12) {
650 rc = set_validity_icpt(scb_s, 0x10b1U);
653 if (gpa & ((1 << sdnxc) - 1)) {
654 rc = set_validity_icpt(scb_s, 0x10b2U);
657 /* Due to alignment rules (checked above) this cannot
658 * cross page boundaries
660 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
662 rc = set_validity_icpt(scb_s, 0x10b0U);
665 scb_s->sdnxo = hpa | sdnxc;
669 unpin_blocks(vcpu, vsie_page);
673 /* unpin the scb provided by guest 2, marking it as dirty */
674 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
677 hpa_t hpa = (hpa_t) vsie_page->scb_o;
680 unpin_guest_page(vcpu->kvm, gpa, hpa);
681 vsie_page->scb_o = NULL;
685 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
687 * Returns: - 0 if the scb was pinned.
688 * - > 0 if control has to be given to guest 2
689 * - -ENOMEM if out of memory
691 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
697 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
699 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
704 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
709 * Inject a fault into guest 2.
711 * Returns: - > 0 if control has to be given to guest 2
712 * < 0 if an error occurred during injection.
714 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
717 struct kvm_s390_pgm_info pgm = {
720 /* 0-51: virtual address */
721 (vaddr & 0xfffffffffffff000UL) |
722 /* 52-53: store / fetch */
723 (((unsigned int) !write_flag) + 1) << 10,
724 /* 62-63: asce id (alway primary == 0) */
725 .exc_access_id = 0, /* always primary */
726 .op_access_id = 0, /* not MVPG */
730 if (code == PGM_PROTECTION)
731 pgm.trans_exc_code |= 0x4UL;
733 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
738 * Handle a fault during vsie execution on a gmap shadow.
740 * Returns: - 0 if the fault was resolved
741 * - > 0 if control has to be given to guest 2
742 * - < 0 if an error occurred
744 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
748 if (current->thread.gmap_int_code == PGM_PROTECTION)
749 /* we can directly forward all protection exceptions */
750 return inject_fault(vcpu, PGM_PROTECTION,
751 current->thread.gmap_addr, 1);
753 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
754 current->thread.gmap_addr);
756 rc = inject_fault(vcpu, rc,
757 current->thread.gmap_addr,
758 current->thread.gmap_write_flag);
760 vsie_page->fault_addr = current->thread.gmap_addr;
766 * Retry the previous fault that required guest 2 intervention. This avoids
767 * one superfluous SIE re-entry and direct exit.
769 * Will ignore any errors. The next SIE fault will do proper fault handling.
771 static void handle_last_fault(struct kvm_vcpu *vcpu,
772 struct vsie_page *vsie_page)
774 if (vsie_page->fault_addr)
775 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
776 vsie_page->fault_addr);
777 vsie_page->fault_addr = 0;
780 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
782 vsie_page->scb_s.icptcode = 0;
785 /* rewind the psw and clear the vsie icpt, so we can retry execution */
786 static void retry_vsie_icpt(struct vsie_page *vsie_page)
788 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
789 int ilen = insn_length(scb_s->ipa >> 8);
791 /* take care of EXECUTE instructions */
792 if (scb_s->icptstatus & 1) {
793 ilen = (scb_s->icptstatus >> 4) & 0x6;
797 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
798 clear_vsie_icpt(vsie_page);
802 * Try to shadow + enable the guest 2 provided facility list.
803 * Retry instruction execution if enabled for and provided by guest 2.
805 * Returns: - 0 if handled (retry or guest 2 icpt)
806 * - > 0 if control has to be given to guest 2
808 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
810 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
811 __u32 fac = READ_ONCE(vsie_page->scb_o->fac) & 0x7ffffff8U;
813 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
814 retry_vsie_icpt(vsie_page);
815 if (read_guest_real(vcpu, fac, &vsie_page->fac,
816 sizeof(vsie_page->fac)))
817 return set_validity_icpt(scb_s, 0x1090U);
818 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
824 * Run the vsie on a shadow scb and a shadow gmap, without any further
825 * sanity checks, handling SIE faults.
827 * Returns: - 0 everything went fine
828 * - > 0 if control has to be given to guest 2
829 * - < 0 if an error occurred
831 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
833 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
834 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
835 int guest_bp_isolation;
838 handle_last_fault(vcpu, vsie_page);
842 if (test_cpu_flag(CIF_MCCK_PENDING))
845 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
847 /* save current guest state of bp isolation override */
848 guest_bp_isolation = test_thread_flag(TIF_ISOLATE_BP_GUEST);
851 * The guest is running with BPBC, so we have to force it on for our
852 * nested guest. This is done by enabling BPBC globally, so the BPBC
853 * control in the SCB (which the nested guest can modify) is simply
856 if (test_kvm_facility(vcpu->kvm, 82) &&
857 vcpu->arch.sie_block->fpf & FPF_BPBC)
858 set_thread_flag(TIF_ISOLATE_BP_GUEST);
861 guest_enter_irqoff();
864 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
870 /* restore guest state for bp isolation override */
871 if (!guest_bp_isolation)
872 clear_thread_flag(TIF_ISOLATE_BP_GUEST);
874 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
877 VCPU_EVENT(vcpu, 3, "%s", "machine check");
878 kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
883 rc = 0; /* we could still have an icpt */
884 else if (rc == -EFAULT)
885 return handle_fault(vcpu, vsie_page);
887 switch (scb_s->icptcode) {
889 if (scb_s->ipa == 0xb2b0)
890 rc = handle_stfle(vcpu, vsie_page);
893 /* stop not requested by g2 - must have been a kick */
894 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
895 clear_vsie_icpt(vsie_page);
898 if ((scb_s->ipa & 0xf000) != 0xf000)
899 scb_s->ipa += 0x1000;
905 static void release_gmap_shadow(struct vsie_page *vsie_page)
908 gmap_put(vsie_page->gmap);
909 WRITE_ONCE(vsie_page->gmap, NULL);
910 prefix_unmapped(vsie_page);
913 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
914 struct vsie_page *vsie_page)
921 asce = vcpu->arch.sie_block->gcr[1];
922 cr0.val = vcpu->arch.sie_block->gcr[0];
923 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
924 edat += edat && test_kvm_facility(vcpu->kvm, 78);
927 * ASCE or EDAT could have changed since last icpt, or the gmap
928 * we're holding has been unshadowed. If the gmap is still valid,
929 * we can safely reuse it.
931 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
934 /* release the old shadow - if any, and mark the prefix as unmapped */
935 release_gmap_shadow(vsie_page);
936 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
938 return PTR_ERR(gmap);
939 gmap->private = vcpu->kvm;
940 WRITE_ONCE(vsie_page->gmap, gmap);
945 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
947 static void register_shadow_scb(struct kvm_vcpu *vcpu,
948 struct vsie_page *vsie_page)
950 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
952 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
954 * External calls have to lead to a kick of the vcpu and
955 * therefore the vsie -> Simulate Wait state.
957 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
959 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
960 * automatically be adjusted on tod clock changes via kvm_sync_clock.
963 scb_s->epoch += vcpu->kvm->arch.epoch;
965 if (scb_s->ecd & ECD_MEF) {
966 scb_s->epdx += vcpu->kvm->arch.epdx;
967 if (scb_s->epoch < vcpu->kvm->arch.epoch)
975 * Unregister a shadow scb from a VCPU.
977 static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
979 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
980 WRITE_ONCE(vcpu->arch.vsie_block, NULL);
984 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
985 * prefix pages and faults.
987 * Returns: - 0 if no errors occurred
988 * - > 0 if control has to be given to guest 2
989 * - -ENOMEM if out of memory
991 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
993 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
997 rc = acquire_gmap_shadow(vcpu, vsie_page);
999 rc = map_prefix(vcpu, vsie_page);
1001 gmap_enable(vsie_page->gmap);
1002 update_intervention_requests(vsie_page);
1003 rc = do_vsie_run(vcpu, vsie_page);
1004 gmap_enable(vcpu->arch.gmap);
1006 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
1010 if (rc || scb_s->icptcode || signal_pending(current) ||
1011 kvm_s390_vcpu_has_irq(vcpu, 0))
1015 if (rc == -EFAULT) {
1017 * Addressing exceptions are always presentes as intercepts.
1018 * As addressing exceptions are suppressing and our guest 3 PSW
1019 * points at the responsible instruction, we have to
1020 * forward the PSW and set the ilc. If we can't read guest 3
1021 * instruction, we can use an arbitrary ilc. Let's always use
1022 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
1023 * memory. (we could also fake the shadow so the hardware
1026 scb_s->icptcode = ICPT_PROGI;
1027 scb_s->iprcc = PGM_ADDRESSING;
1029 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
1036 * Get or create a vsie page for a scb address.
1038 * Returns: - address of a vsie page (cached or new one)
1039 * - NULL if the same scb address is already used by another VCPU
1040 * - ERR_PTR(-ENOMEM) if out of memory
1042 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
1044 struct vsie_page *vsie_page;
1049 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1052 if (page_ref_inc_return(page) == 2)
1053 return page_to_virt(page);
1058 * We want at least #online_vcpus shadows, so every VCPU can execute
1059 * the VSIE in parallel.
1061 nr_vcpus = atomic_read(&kvm->online_vcpus);
1063 mutex_lock(&kvm->arch.vsie.mutex);
1064 if (kvm->arch.vsie.page_count < nr_vcpus) {
1065 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
1067 mutex_unlock(&kvm->arch.vsie.mutex);
1068 return ERR_PTR(-ENOMEM);
1071 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1072 kvm->arch.vsie.page_count++;
1074 /* reuse an existing entry that belongs to nobody */
1076 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1077 if (page_ref_inc_return(page) == 2)
1080 kvm->arch.vsie.next++;
1081 kvm->arch.vsie.next %= nr_vcpus;
1083 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1086 /* double use of the same address */
1087 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1089 mutex_unlock(&kvm->arch.vsie.mutex);
1092 mutex_unlock(&kvm->arch.vsie.mutex);
1094 vsie_page = page_to_virt(page);
1095 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
1096 release_gmap_shadow(vsie_page);
1097 vsie_page->fault_addr = 0;
1098 vsie_page->scb_s.ihcpu = 0xffffU;
1102 /* put a vsie page acquired via get_vsie_page */
1103 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1105 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1110 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1112 struct vsie_page *vsie_page;
1113 unsigned long scb_addr;
1116 vcpu->stat.instruction_sie++;
1117 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1119 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1120 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1122 BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
1123 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1125 /* 512 byte alignment */
1126 if (unlikely(scb_addr & 0x1ffUL))
1127 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1129 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1132 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1133 if (IS_ERR(vsie_page))
1134 return PTR_ERR(vsie_page);
1135 else if (!vsie_page)
1136 /* double use of sie control block - simply do nothing */
1139 rc = pin_scb(vcpu, vsie_page, scb_addr);
1142 rc = shadow_scb(vcpu, vsie_page);
1145 rc = pin_blocks(vcpu, vsie_page);
1148 register_shadow_scb(vcpu, vsie_page);
1149 rc = vsie_run(vcpu, vsie_page);
1150 unregister_shadow_scb(vcpu);
1151 unpin_blocks(vcpu, vsie_page);
1153 unshadow_scb(vcpu, vsie_page);
1155 unpin_scb(vcpu, vsie_page, scb_addr);
1157 put_vsie_page(vcpu->kvm, vsie_page);
1159 return rc < 0 ? rc : 0;
1162 /* Init the vsie data structures. To be called when a vm is initialized. */
1163 void kvm_s390_vsie_init(struct kvm *kvm)
1165 mutex_init(&kvm->arch.vsie.mutex);
1166 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1169 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
1170 void kvm_s390_vsie_destroy(struct kvm *kvm)
1172 struct vsie_page *vsie_page;
1176 mutex_lock(&kvm->arch.vsie.mutex);
1177 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1178 page = kvm->arch.vsie.pages[i];
1179 kvm->arch.vsie.pages[i] = NULL;
1180 vsie_page = page_to_virt(page);
1181 release_gmap_shadow(vsie_page);
1182 /* free the radix tree entry */
1183 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1186 kvm->arch.vsie.page_count = 0;
1187 mutex_unlock(&kvm->arch.vsie.mutex);
1190 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1192 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1195 * Even if the VCPU lets go of the shadow sie block reference, it is
1196 * still valid in the cache. So we can safely kick it.
1199 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1200 if (scb->prog0c & PROG_IN_SIE)
1201 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);