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;
378 scb_s->epdx = scb_o->epdx;
381 prepare_ibc(vcpu, vsie_page);
382 rc = shadow_crycb(vcpu, vsie_page);
385 unshadow_scb(vcpu, vsie_page);
389 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
392 struct kvm *kvm = gmap->private;
393 struct vsie_page *cur;
394 unsigned long prefix;
398 if (!gmap_is_shadow(gmap))
400 if (start >= 1UL << 31)
401 /* We are only interested in prefix pages */
405 * Only new shadow blocks are added to the list during runtime,
406 * therefore we can safely reference them all the time.
408 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
409 page = READ_ONCE(kvm->arch.vsie.pages[i]);
412 cur = page_to_virt(page);
413 if (READ_ONCE(cur->gmap) != gmap)
415 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
416 /* with mso/msl, the prefix lies at an offset */
417 prefix += cur->scb_s.mso;
418 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
419 prefix_unmapped_sync(cur);
424 * Map the first prefix page and if tx is enabled also the second prefix page.
426 * The prefix will be protected, a gmap notifier will inform about unmaps.
427 * The shadow scb must not be executed until the prefix is remapped, this is
428 * guaranteed by properly handling PROG_REQUEST.
430 * Returns: - 0 on if successfully mapped or already mapped
431 * - > 0 if control has to be given to guest 2
432 * - -EAGAIN if the caller can retry immediately
433 * - -ENOMEM if out of memory
435 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
437 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
438 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
441 if (prefix_is_mapped(vsie_page))
444 /* mark it as mapped so we can catch any concurrent unmappers */
445 prefix_mapped(vsie_page);
447 /* with mso/msl, the prefix lies at offset *mso* */
448 prefix += scb_s->mso;
450 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
451 if (!rc && (scb_s->ecb & ECB_TE))
452 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
455 * We don't have to mprotect, we will be called for all unshadows.
456 * SIE will detect if protection applies and trigger a validity.
459 prefix_unmapped(vsie_page);
460 if (rc > 0 || rc == -EFAULT)
461 rc = set_validity_icpt(scb_s, 0x0037U);
466 * Pin the guest page given by gpa and set hpa to the pinned host address.
467 * Will always be pinned writable.
469 * Returns: - 0 on success
470 * - -EINVAL if the gpa is not valid guest storage
471 * - -ENOMEM if out of memory
473 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
479 hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
480 if (kvm_is_error_hva(hva))
482 rc = get_user_pages_fast(hva, 1, 1, &page);
487 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
491 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
492 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
496 page = virt_to_page(hpa);
497 set_page_dirty_lock(page);
499 /* mark the page always as dirty for migration */
500 mark_page_dirty(kvm, gpa_to_gfn(gpa));
503 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
504 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
506 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
507 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
511 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
513 gpa = scb_o->scaol & ~0xfUL;
514 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
515 gpa |= (u64) scb_o->scaoh << 32;
516 unpin_guest_page(vcpu->kvm, gpa, hpa);
523 gpa = scb_o->itdba & ~0xffUL;
524 unpin_guest_page(vcpu->kvm, gpa, hpa);
530 gpa = scb_o->gvrd & ~0x1ffUL;
531 unpin_guest_page(vcpu->kvm, gpa, hpa);
537 gpa = scb_o->riccbd & ~0x3fUL;
538 unpin_guest_page(vcpu->kvm, gpa, hpa);
545 unpin_guest_page(vcpu->kvm, gpa, hpa);
551 * Instead of shadowing some blocks, we can simply forward them because the
552 * addresses in the scb are 64 bit long.
554 * This works as long as the data lies in one page. If blocks ever exceed one
555 * page, we have to fall back to shadowing.
557 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
558 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
560 * Returns: - 0 if all blocks were pinned.
561 * - > 0 if control has to be given to guest 2
562 * - -ENOMEM if out of memory
564 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
566 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
567 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
572 gpa = READ_ONCE(scb_o->scaol) & ~0xfUL;
573 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
574 gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32;
576 if (!(gpa & ~0x1fffUL))
577 rc = set_validity_icpt(scb_s, 0x0038U);
578 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
579 rc = set_validity_icpt(scb_s, 0x0011U);
580 else if ((gpa & PAGE_MASK) !=
581 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
582 rc = set_validity_icpt(scb_s, 0x003bU);
584 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
586 rc = set_validity_icpt(scb_s, 0x0034U);
590 scb_s->scaoh = (u32)((u64)hpa >> 32);
591 scb_s->scaol = (u32)(u64)hpa;
594 gpa = READ_ONCE(scb_o->itdba) & ~0xffUL;
595 if (gpa && (scb_s->ecb & ECB_TE)) {
596 if (!(gpa & ~0x1fffUL)) {
597 rc = set_validity_icpt(scb_s, 0x0080U);
600 /* 256 bytes cannot cross page boundaries */
601 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
603 rc = set_validity_icpt(scb_s, 0x0080U);
609 gpa = READ_ONCE(scb_o->gvrd) & ~0x1ffUL;
610 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
611 if (!(gpa & ~0x1fffUL)) {
612 rc = set_validity_icpt(scb_s, 0x1310U);
616 * 512 bytes vector registers cannot cross page boundaries
617 * if this block gets bigger, we have to shadow it.
619 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
621 rc = set_validity_icpt(scb_s, 0x1310U);
627 gpa = READ_ONCE(scb_o->riccbd) & ~0x3fUL;
628 if (gpa && (scb_s->ecb3 & ECB3_RI)) {
629 if (!(gpa & ~0x1fffUL)) {
630 rc = set_validity_icpt(scb_s, 0x0043U);
633 /* 64 bytes cannot cross page boundaries */
634 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
636 rc = set_validity_icpt(scb_s, 0x0043U);
637 /* Validity 0x0044 will be checked by SIE */
642 if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
645 gpa = READ_ONCE(scb_o->sdnxo) & ~0xfUL;
646 sdnxc = READ_ONCE(scb_o->sdnxo) & 0xfUL;
647 if (!gpa || !(gpa & ~0x1fffUL)) {
648 rc = set_validity_icpt(scb_s, 0x10b0U);
651 if (sdnxc < 6 || sdnxc > 12) {
652 rc = set_validity_icpt(scb_s, 0x10b1U);
655 if (gpa & ((1 << sdnxc) - 1)) {
656 rc = set_validity_icpt(scb_s, 0x10b2U);
659 /* Due to alignment rules (checked above) this cannot
660 * cross page boundaries
662 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
664 rc = set_validity_icpt(scb_s, 0x10b0U);
667 scb_s->sdnxo = hpa | sdnxc;
671 unpin_blocks(vcpu, vsie_page);
675 /* unpin the scb provided by guest 2, marking it as dirty */
676 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
679 hpa_t hpa = (hpa_t) vsie_page->scb_o;
682 unpin_guest_page(vcpu->kvm, gpa, hpa);
683 vsie_page->scb_o = NULL;
687 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
689 * Returns: - 0 if the scb was pinned.
690 * - > 0 if control has to be given to guest 2
691 * - -ENOMEM if out of memory
693 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
699 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
701 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
706 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
711 * Inject a fault into guest 2.
713 * Returns: - > 0 if control has to be given to guest 2
714 * < 0 if an error occurred during injection.
716 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
719 struct kvm_s390_pgm_info pgm = {
722 /* 0-51: virtual address */
723 (vaddr & 0xfffffffffffff000UL) |
724 /* 52-53: store / fetch */
725 (((unsigned int) !write_flag) + 1) << 10,
726 /* 62-63: asce id (alway primary == 0) */
727 .exc_access_id = 0, /* always primary */
728 .op_access_id = 0, /* not MVPG */
732 if (code == PGM_PROTECTION)
733 pgm.trans_exc_code |= 0x4UL;
735 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
740 * Handle a fault during vsie execution on a gmap shadow.
742 * Returns: - 0 if the fault was resolved
743 * - > 0 if control has to be given to guest 2
744 * - < 0 if an error occurred
746 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
750 if (current->thread.gmap_int_code == PGM_PROTECTION)
751 /* we can directly forward all protection exceptions */
752 return inject_fault(vcpu, PGM_PROTECTION,
753 current->thread.gmap_addr, 1);
755 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
756 current->thread.gmap_addr);
758 rc = inject_fault(vcpu, rc,
759 current->thread.gmap_addr,
760 current->thread.gmap_write_flag);
762 vsie_page->fault_addr = current->thread.gmap_addr;
768 * Retry the previous fault that required guest 2 intervention. This avoids
769 * one superfluous SIE re-entry and direct exit.
771 * Will ignore any errors. The next SIE fault will do proper fault handling.
773 static void handle_last_fault(struct kvm_vcpu *vcpu,
774 struct vsie_page *vsie_page)
776 if (vsie_page->fault_addr)
777 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
778 vsie_page->fault_addr);
779 vsie_page->fault_addr = 0;
782 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
784 vsie_page->scb_s.icptcode = 0;
787 /* rewind the psw and clear the vsie icpt, so we can retry execution */
788 static void retry_vsie_icpt(struct vsie_page *vsie_page)
790 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
791 int ilen = insn_length(scb_s->ipa >> 8);
793 /* take care of EXECUTE instructions */
794 if (scb_s->icptstatus & 1) {
795 ilen = (scb_s->icptstatus >> 4) & 0x6;
799 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
800 clear_vsie_icpt(vsie_page);
804 * Try to shadow + enable the guest 2 provided facility list.
805 * Retry instruction execution if enabled for and provided by guest 2.
807 * Returns: - 0 if handled (retry or guest 2 icpt)
808 * - > 0 if control has to be given to guest 2
810 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
812 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
813 __u32 fac = READ_ONCE(vsie_page->scb_o->fac) & 0x7ffffff8U;
815 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
816 retry_vsie_icpt(vsie_page);
817 if (read_guest_real(vcpu, fac, &vsie_page->fac,
818 sizeof(vsie_page->fac)))
819 return set_validity_icpt(scb_s, 0x1090U);
820 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
826 * Run the vsie on a shadow scb and a shadow gmap, without any further
827 * sanity checks, handling SIE faults.
829 * Returns: - 0 everything went fine
830 * - > 0 if control has to be given to guest 2
831 * - < 0 if an error occurred
833 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
835 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
836 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
837 int guest_bp_isolation;
840 handle_last_fault(vcpu, vsie_page);
844 if (test_cpu_flag(CIF_MCCK_PENDING))
847 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
849 /* save current guest state of bp isolation override */
850 guest_bp_isolation = test_thread_flag(TIF_ISOLATE_BP_GUEST);
853 * The guest is running with BPBC, so we have to force it on for our
854 * nested guest. This is done by enabling BPBC globally, so the BPBC
855 * control in the SCB (which the nested guest can modify) is simply
858 if (test_kvm_facility(vcpu->kvm, 82) &&
859 vcpu->arch.sie_block->fpf & FPF_BPBC)
860 set_thread_flag(TIF_ISOLATE_BP_GUEST);
863 guest_enter_irqoff();
866 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
872 /* restore guest state for bp isolation override */
873 if (!guest_bp_isolation)
874 clear_thread_flag(TIF_ISOLATE_BP_GUEST);
876 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
879 VCPU_EVENT(vcpu, 3, "%s", "machine check");
880 kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
885 rc = 0; /* we could still have an icpt */
886 else if (rc == -EFAULT)
887 return handle_fault(vcpu, vsie_page);
889 switch (scb_s->icptcode) {
891 if (scb_s->ipa == 0xb2b0)
892 rc = handle_stfle(vcpu, vsie_page);
895 /* stop not requested by g2 - must have been a kick */
896 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
897 clear_vsie_icpt(vsie_page);
900 if ((scb_s->ipa & 0xf000) != 0xf000)
901 scb_s->ipa += 0x1000;
907 static void release_gmap_shadow(struct vsie_page *vsie_page)
910 gmap_put(vsie_page->gmap);
911 WRITE_ONCE(vsie_page->gmap, NULL);
912 prefix_unmapped(vsie_page);
915 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
916 struct vsie_page *vsie_page)
923 asce = vcpu->arch.sie_block->gcr[1];
924 cr0.val = vcpu->arch.sie_block->gcr[0];
925 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
926 edat += edat && test_kvm_facility(vcpu->kvm, 78);
929 * ASCE or EDAT could have changed since last icpt, or the gmap
930 * we're holding has been unshadowed. If the gmap is still valid,
931 * we can safely reuse it.
933 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
936 /* release the old shadow - if any, and mark the prefix as unmapped */
937 release_gmap_shadow(vsie_page);
938 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
940 return PTR_ERR(gmap);
941 gmap->private = vcpu->kvm;
942 WRITE_ONCE(vsie_page->gmap, gmap);
947 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
949 static void register_shadow_scb(struct kvm_vcpu *vcpu,
950 struct vsie_page *vsie_page)
952 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
954 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
956 * External calls have to lead to a kick of the vcpu and
957 * therefore the vsie -> Simulate Wait state.
959 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
961 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
962 * automatically be adjusted on tod clock changes via kvm_sync_clock.
965 scb_s->epoch += vcpu->kvm->arch.epoch;
967 if (scb_s->ecd & ECD_MEF) {
968 scb_s->epdx += vcpu->kvm->arch.epdx;
969 if (scb_s->epoch < vcpu->kvm->arch.epoch)
977 * Unregister a shadow scb from a VCPU.
979 static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
981 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
982 WRITE_ONCE(vcpu->arch.vsie_block, NULL);
986 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
987 * prefix pages and faults.
989 * Returns: - 0 if no errors occurred
990 * - > 0 if control has to be given to guest 2
991 * - -ENOMEM if out of memory
993 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
995 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
999 rc = acquire_gmap_shadow(vcpu, vsie_page);
1001 rc = map_prefix(vcpu, vsie_page);
1003 gmap_enable(vsie_page->gmap);
1004 update_intervention_requests(vsie_page);
1005 rc = do_vsie_run(vcpu, vsie_page);
1006 gmap_enable(vcpu->arch.gmap);
1008 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
1012 if (rc || scb_s->icptcode || signal_pending(current) ||
1013 kvm_s390_vcpu_has_irq(vcpu, 0))
1017 if (rc == -EFAULT) {
1019 * Addressing exceptions are always presentes as intercepts.
1020 * As addressing exceptions are suppressing and our guest 3 PSW
1021 * points at the responsible instruction, we have to
1022 * forward the PSW and set the ilc. If we can't read guest 3
1023 * instruction, we can use an arbitrary ilc. Let's always use
1024 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
1025 * memory. (we could also fake the shadow so the hardware
1028 scb_s->icptcode = ICPT_PROGI;
1029 scb_s->iprcc = PGM_ADDRESSING;
1031 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
1038 * Get or create a vsie page for a scb address.
1040 * Returns: - address of a vsie page (cached or new one)
1041 * - NULL if the same scb address is already used by another VCPU
1042 * - ERR_PTR(-ENOMEM) if out of memory
1044 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
1046 struct vsie_page *vsie_page;
1051 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1054 if (page_ref_inc_return(page) == 2)
1055 return page_to_virt(page);
1060 * We want at least #online_vcpus shadows, so every VCPU can execute
1061 * the VSIE in parallel.
1063 nr_vcpus = atomic_read(&kvm->online_vcpus);
1065 mutex_lock(&kvm->arch.vsie.mutex);
1066 if (kvm->arch.vsie.page_count < nr_vcpus) {
1067 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
1069 mutex_unlock(&kvm->arch.vsie.mutex);
1070 return ERR_PTR(-ENOMEM);
1073 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1074 kvm->arch.vsie.page_count++;
1076 /* reuse an existing entry that belongs to nobody */
1078 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1079 if (page_ref_inc_return(page) == 2)
1082 kvm->arch.vsie.next++;
1083 kvm->arch.vsie.next %= nr_vcpus;
1085 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1088 /* double use of the same address */
1089 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1091 mutex_unlock(&kvm->arch.vsie.mutex);
1094 mutex_unlock(&kvm->arch.vsie.mutex);
1096 vsie_page = page_to_virt(page);
1097 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
1098 release_gmap_shadow(vsie_page);
1099 vsie_page->fault_addr = 0;
1100 vsie_page->scb_s.ihcpu = 0xffffU;
1104 /* put a vsie page acquired via get_vsie_page */
1105 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1107 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1112 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1114 struct vsie_page *vsie_page;
1115 unsigned long scb_addr;
1118 vcpu->stat.instruction_sie++;
1119 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1121 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1122 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1124 BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
1125 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1127 /* 512 byte alignment */
1128 if (unlikely(scb_addr & 0x1ffUL))
1129 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1131 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1134 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1135 if (IS_ERR(vsie_page))
1136 return PTR_ERR(vsie_page);
1137 else if (!vsie_page)
1138 /* double use of sie control block - simply do nothing */
1141 rc = pin_scb(vcpu, vsie_page, scb_addr);
1144 rc = shadow_scb(vcpu, vsie_page);
1147 rc = pin_blocks(vcpu, vsie_page);
1150 register_shadow_scb(vcpu, vsie_page);
1151 rc = vsie_run(vcpu, vsie_page);
1152 unregister_shadow_scb(vcpu);
1153 unpin_blocks(vcpu, vsie_page);
1155 unshadow_scb(vcpu, vsie_page);
1157 unpin_scb(vcpu, vsie_page, scb_addr);
1159 put_vsie_page(vcpu->kvm, vsie_page);
1161 return rc < 0 ? rc : 0;
1164 /* Init the vsie data structures. To be called when a vm is initialized. */
1165 void kvm_s390_vsie_init(struct kvm *kvm)
1167 mutex_init(&kvm->arch.vsie.mutex);
1168 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1171 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
1172 void kvm_s390_vsie_destroy(struct kvm *kvm)
1174 struct vsie_page *vsie_page;
1178 mutex_lock(&kvm->arch.vsie.mutex);
1179 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1180 page = kvm->arch.vsie.pages[i];
1181 kvm->arch.vsie.pages[i] = NULL;
1182 vsie_page = page_to_virt(page);
1183 release_gmap_shadow(vsie_page);
1184 /* free the radix tree entry */
1185 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1188 kvm->arch.vsie.page_count = 0;
1189 mutex_unlock(&kvm->arch.vsie.mutex);
1192 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1194 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1197 * Even if the VCPU lets go of the shadow sie block reference, it is
1198 * still valid in the cache. So we can safely kick it.
1201 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1202 if (scb->prog0c & PROG_IN_SIE)
1203 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);