1 // SPDX-License-Identifier: GPL-2.0
3 * kvm nested virtualization support for s390x
5 * Copyright IBM Corp. 2016
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 */
31 /* the pinned originial scb */
32 struct kvm_s390_sie_block *scb_o; /* 0x0218 */
33 /* the shadow gmap in use by the vsie_page */
34 struct gmap *gmap; /* 0x0220 */
35 /* address of the last reported fault to guest2 */
36 unsigned long fault_addr; /* 0x0228 */
37 __u8 reserved[0x0700 - 0x0230]; /* 0x0230 */
38 struct kvm_s390_crypto_cb crycb; /* 0x0700 */
39 __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */
42 /* trigger a validity icpt for the given scb */
43 static int set_validity_icpt(struct kvm_s390_sie_block *scb,
47 scb->ipb = ((__u32) reason_code) << 16;
48 scb->icptcode = ICPT_VALIDITY;
52 /* mark the prefix as unmapped, this will block the VSIE */
53 static void prefix_unmapped(struct vsie_page *vsie_page)
55 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
58 /* mark the prefix as unmapped and wait until the VSIE has been left */
59 static void prefix_unmapped_sync(struct vsie_page *vsie_page)
61 prefix_unmapped(vsie_page);
62 if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
63 atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
64 while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
68 /* mark the prefix as mapped, this will allow the VSIE to run */
69 static void prefix_mapped(struct vsie_page *vsie_page)
71 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
74 /* test if the prefix is mapped into the gmap shadow */
75 static int prefix_is_mapped(struct vsie_page *vsie_page)
77 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
80 /* copy the updated intervention request bits into the shadow scb */
81 static void update_intervention_requests(struct vsie_page *vsie_page)
83 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
86 cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
87 atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
88 atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
91 /* shadow (filter and validate) the cpuflags */
92 static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
94 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
95 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
96 int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
98 /* we don't allow ESA/390 guests */
99 if (!(cpuflags & CPUSTAT_ZARCH))
100 return set_validity_icpt(scb_s, 0x0001U);
102 if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
103 return set_validity_icpt(scb_s, 0x0001U);
104 else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
105 return set_validity_icpt(scb_s, 0x0007U);
107 /* intervention requests will be set later */
108 newflags = CPUSTAT_ZARCH;
109 if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8))
110 newflags |= CPUSTAT_GED;
111 if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) {
112 if (cpuflags & CPUSTAT_GED)
113 return set_validity_icpt(scb_s, 0x0001U);
114 newflags |= CPUSTAT_GED2;
116 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE))
117 newflags |= cpuflags & CPUSTAT_P;
118 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS))
119 newflags |= cpuflags & CPUSTAT_SM;
120 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS))
121 newflags |= cpuflags & CPUSTAT_IBS;
122 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_KSS))
123 newflags |= cpuflags & CPUSTAT_KSS;
125 atomic_set(&scb_s->cpuflags, newflags);
130 * Create a shadow copy of the crycb block and setup key wrapping, if
131 * requested for guest 3 and enabled for guest 2.
133 * We only accept format-1 (no AP in g2), but convert it into format-2
134 * There is nothing to do for format-0.
136 * Returns: - 0 if shadowed or nothing to do
137 * - > 0 if control has to be given to guest 2
139 static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
141 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
142 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
143 u32 crycb_addr = scb_o->crycbd & 0x7ffffff8U;
144 unsigned long *b1, *b2;
148 if (!(scb_o->crycbd & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
150 /* format-1 is supported with message-security-assist extension 3 */
151 if (!test_kvm_facility(vcpu->kvm, 76))
153 /* we may only allow it if enabled for guest 2 */
154 ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
155 (ECB3_AES | ECB3_DEA);
159 if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
160 return set_validity_icpt(scb_s, 0x003CU);
161 else if (!crycb_addr)
162 return set_validity_icpt(scb_s, 0x0039U);
164 /* copy only the wrapping keys */
165 if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
166 return set_validity_icpt(scb_s, 0x0035U);
168 scb_s->ecb3 |= ecb3_flags;
169 scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
172 /* xor both blocks in one run */
173 b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
174 b2 = (unsigned long *)
175 vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
176 /* as 56%8 == 0, bitmap_xor won't overwrite any data */
177 bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
181 /* shadow (round up/down) the ibc to avoid validity icpt */
182 static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
184 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
185 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
186 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
189 /* ibc installed in g2 and requested for g3 */
190 if (vcpu->kvm->arch.model.ibc && (scb_o->ibc & 0x0fffU)) {
191 scb_s->ibc = scb_o->ibc & 0x0fffU;
192 /* takte care of the minimum ibc level of the machine */
193 if (scb_s->ibc < min_ibc)
194 scb_s->ibc = min_ibc;
195 /* take care of the maximum ibc level set for the guest */
196 if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
197 scb_s->ibc = vcpu->kvm->arch.model.ibc;
201 /* unshadow the scb, copying parameters back to the real scb */
202 static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
204 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
205 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
208 scb_o->icptcode = scb_s->icptcode;
209 scb_o->icptstatus = scb_s->icptstatus;
210 scb_o->ipa = scb_s->ipa;
211 scb_o->ipb = scb_s->ipb;
212 scb_o->gbea = scb_s->gbea;
215 scb_o->cputm = scb_s->cputm;
216 scb_o->ckc = scb_s->ckc;
217 scb_o->todpr = scb_s->todpr;
220 scb_o->gpsw = scb_s->gpsw;
221 scb_o->gg14 = scb_s->gg14;
222 scb_o->gg15 = scb_s->gg15;
223 memcpy(scb_o->gcr, scb_s->gcr, 128);
224 scb_o->pp = scb_s->pp;
226 /* branch prediction */
227 if (test_kvm_facility(vcpu->kvm, 82)) {
228 scb_o->fpf &= ~FPF_BPBC;
229 scb_o->fpf |= scb_s->fpf & FPF_BPBC;
232 /* interrupt intercept */
233 switch (scb_s->icptcode) {
237 memcpy((void *)((u64)scb_o + 0xc0),
238 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
242 memcpy((void *)((u64)scb_o + 0xc0),
243 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
247 if (scb_s->ihcpu != 0xffffU)
248 scb_o->ihcpu = scb_s->ihcpu;
252 * Setup the shadow scb by copying and checking the relevant parts of the g2
255 * Returns: - 0 if the scb has been shadowed
256 * - > 0 if control has to be given to guest 2
258 static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
260 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
261 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
262 bool had_tx = scb_s->ecb & ECB_TE;
263 unsigned long new_mso = 0;
266 /* make sure we don't have any leftovers when reusing the scb */
276 rc = prepare_cpuflags(vcpu, vsie_page);
281 scb_s->cputm = scb_o->cputm;
282 scb_s->ckc = scb_o->ckc;
283 scb_s->todpr = scb_o->todpr;
284 scb_s->epoch = scb_o->epoch;
287 scb_s->gpsw = scb_o->gpsw;
288 scb_s->gg14 = scb_o->gg14;
289 scb_s->gg15 = scb_o->gg15;
290 memcpy(scb_s->gcr, scb_o->gcr, 128);
291 scb_s->pp = scb_o->pp;
293 /* interception / execution handling */
294 scb_s->gbea = scb_o->gbea;
295 scb_s->lctl = scb_o->lctl;
296 scb_s->svcc = scb_o->svcc;
297 scb_s->ictl = scb_o->ictl;
299 * SKEY handling functions can't deal with false setting of PTE invalid
300 * bits. Therefore we cannot provide interpretation and would later
301 * have to provide own emulation handlers.
303 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
304 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
306 scb_s->icpua = scb_o->icpua;
308 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
309 new_mso = scb_o->mso & 0xfffffffffff00000UL;
310 /* if the hva of the prefix changes, we have to remap the prefix */
311 if (scb_s->mso != new_mso || scb_s->prefix != scb_o->prefix)
312 prefix_unmapped(vsie_page);
313 /* SIE will do mso/msl validity and exception checks for us */
314 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
315 scb_s->mso = new_mso;
316 scb_s->prefix = scb_o->prefix;
318 /* We have to definetly flush the tlb if this scb never ran */
319 if (scb_s->ihcpu != 0xffffU)
320 scb_s->ihcpu = scb_o->ihcpu;
322 /* MVPG and Protection Exception Interpretation are always available */
323 scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
324 /* Host-protection-interruption introduced with ESOP */
325 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
326 scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
327 /* transactional execution */
328 if (test_kvm_facility(vcpu->kvm, 73)) {
329 /* remap the prefix is tx is toggled on */
330 if ((scb_o->ecb & ECB_TE) && !had_tx)
331 prefix_unmapped(vsie_page);
332 scb_s->ecb |= scb_o->ecb & ECB_TE;
334 /* branch prediction */
335 if (test_kvm_facility(vcpu->kvm, 82))
336 scb_s->fpf |= scb_o->fpf & FPF_BPBC;
338 if (test_kvm_facility(vcpu->kvm, 129)) {
339 scb_s->eca |= scb_o->eca & ECA_VX;
340 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
342 /* Run-time-Instrumentation */
343 if (test_kvm_facility(vcpu->kvm, 64))
344 scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
345 /* Instruction Execution Prevention */
346 if (test_kvm_facility(vcpu->kvm, 130))
347 scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
348 /* Guarded Storage */
349 if (test_kvm_facility(vcpu->kvm, 133)) {
350 scb_s->ecb |= scb_o->ecb & ECB_GS;
351 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
353 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
354 scb_s->eca |= scb_o->eca & ECA_SII;
355 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
356 scb_s->eca |= scb_o->eca & ECA_IB;
357 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
358 scb_s->eca |= scb_o->eca & ECA_CEI;
359 /* Epoch Extension */
360 if (test_kvm_facility(vcpu->kvm, 139))
361 scb_s->ecd |= scb_o->ecd & ECD_MEF;
363 prepare_ibc(vcpu, vsie_page);
364 rc = shadow_crycb(vcpu, vsie_page);
367 unshadow_scb(vcpu, vsie_page);
371 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
374 struct kvm *kvm = gmap->private;
375 struct vsie_page *cur;
376 unsigned long prefix;
380 if (!gmap_is_shadow(gmap))
382 if (start >= 1UL << 31)
383 /* We are only interested in prefix pages */
387 * Only new shadow blocks are added to the list during runtime,
388 * therefore we can safely reference them all the time.
390 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
391 page = READ_ONCE(kvm->arch.vsie.pages[i]);
394 cur = page_to_virt(page);
395 if (READ_ONCE(cur->gmap) != gmap)
397 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
398 /* with mso/msl, the prefix lies at an offset */
399 prefix += cur->scb_s.mso;
400 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
401 prefix_unmapped_sync(cur);
406 * Map the first prefix page and if tx is enabled also the second prefix page.
408 * The prefix will be protected, a gmap notifier will inform about unmaps.
409 * The shadow scb must not be executed until the prefix is remapped, this is
410 * guaranteed by properly handling PROG_REQUEST.
412 * Returns: - 0 on if successfully mapped or already mapped
413 * - > 0 if control has to be given to guest 2
414 * - -EAGAIN if the caller can retry immediately
415 * - -ENOMEM if out of memory
417 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
419 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
420 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
423 if (prefix_is_mapped(vsie_page))
426 /* mark it as mapped so we can catch any concurrent unmappers */
427 prefix_mapped(vsie_page);
429 /* with mso/msl, the prefix lies at offset *mso* */
430 prefix += scb_s->mso;
432 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
433 if (!rc && (scb_s->ecb & ECB_TE))
434 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
437 * We don't have to mprotect, we will be called for all unshadows.
438 * SIE will detect if protection applies and trigger a validity.
441 prefix_unmapped(vsie_page);
442 if (rc > 0 || rc == -EFAULT)
443 rc = set_validity_icpt(scb_s, 0x0037U);
448 * Pin the guest page given by gpa and set hpa to the pinned host address.
449 * Will always be pinned writable.
451 * Returns: - 0 on success
452 * - -EINVAL if the gpa is not valid guest storage
454 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
458 page = gfn_to_page(kvm, gpa_to_gfn(gpa));
459 if (is_error_page(page))
461 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
465 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
466 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
468 kvm_release_pfn_dirty(hpa >> PAGE_SHIFT);
469 /* mark the page always as dirty for migration */
470 mark_page_dirty(kvm, gpa_to_gfn(gpa));
473 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
474 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
476 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
477 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
481 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
483 gpa = scb_o->scaol & ~0xfUL;
484 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
485 gpa |= (u64) scb_o->scaoh << 32;
486 unpin_guest_page(vcpu->kvm, gpa, hpa);
493 gpa = scb_o->itdba & ~0xffUL;
494 unpin_guest_page(vcpu->kvm, gpa, hpa);
500 gpa = scb_o->gvrd & ~0x1ffUL;
501 unpin_guest_page(vcpu->kvm, gpa, hpa);
507 gpa = scb_o->riccbd & ~0x3fUL;
508 unpin_guest_page(vcpu->kvm, gpa, hpa);
515 unpin_guest_page(vcpu->kvm, gpa, hpa);
521 * Instead of shadowing some blocks, we can simply forward them because the
522 * addresses in the scb are 64 bit long.
524 * This works as long as the data lies in one page. If blocks ever exceed one
525 * page, we have to fall back to shadowing.
527 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
528 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
530 * Returns: - 0 if all blocks were pinned.
531 * - > 0 if control has to be given to guest 2
532 * - -ENOMEM if out of memory
534 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
536 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
537 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
542 gpa = scb_o->scaol & ~0xfUL;
543 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
544 gpa |= (u64) scb_o->scaoh << 32;
546 if (!(gpa & ~0x1fffUL))
547 rc = set_validity_icpt(scb_s, 0x0038U);
548 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
549 rc = set_validity_icpt(scb_s, 0x0011U);
550 else if ((gpa & PAGE_MASK) !=
551 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
552 rc = set_validity_icpt(scb_s, 0x003bU);
554 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
556 rc = set_validity_icpt(scb_s, 0x0034U);
560 scb_s->scaoh = (u32)((u64)hpa >> 32);
561 scb_s->scaol = (u32)(u64)hpa;
564 gpa = scb_o->itdba & ~0xffUL;
565 if (gpa && (scb_s->ecb & ECB_TE)) {
566 if (!(gpa & ~0x1fffU)) {
567 rc = set_validity_icpt(scb_s, 0x0080U);
570 /* 256 bytes cannot cross page boundaries */
571 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
573 rc = set_validity_icpt(scb_s, 0x0080U);
579 gpa = scb_o->gvrd & ~0x1ffUL;
580 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
581 if (!(gpa & ~0x1fffUL)) {
582 rc = set_validity_icpt(scb_s, 0x1310U);
586 * 512 bytes vector registers cannot cross page boundaries
587 * if this block gets bigger, we have to shadow it.
589 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
591 rc = set_validity_icpt(scb_s, 0x1310U);
597 gpa = scb_o->riccbd & ~0x3fUL;
598 if (gpa && (scb_s->ecb3 & ECB3_RI)) {
599 if (!(gpa & ~0x1fffUL)) {
600 rc = set_validity_icpt(scb_s, 0x0043U);
603 /* 64 bytes cannot cross page boundaries */
604 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
606 rc = set_validity_icpt(scb_s, 0x0043U);
609 /* Validity 0x0044 will be checked by SIE */
612 if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
615 gpa = scb_o->sdnxo & ~0xfUL;
616 sdnxc = scb_o->sdnxo & 0xfUL;
617 if (!gpa || !(gpa & ~0x1fffUL)) {
618 rc = set_validity_icpt(scb_s, 0x10b0U);
621 if (sdnxc < 6 || sdnxc > 12) {
622 rc = set_validity_icpt(scb_s, 0x10b1U);
625 if (gpa & ((1 << sdnxc) - 1)) {
626 rc = set_validity_icpt(scb_s, 0x10b2U);
629 /* Due to alignment rules (checked above) this cannot
630 * cross page boundaries
632 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
634 rc = set_validity_icpt(scb_s, 0x10b0U);
637 scb_s->sdnxo = hpa | sdnxc;
641 unpin_blocks(vcpu, vsie_page);
645 /* unpin the scb provided by guest 2, marking it as dirty */
646 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
649 hpa_t hpa = (hpa_t) vsie_page->scb_o;
652 unpin_guest_page(vcpu->kvm, gpa, hpa);
653 vsie_page->scb_o = NULL;
657 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
659 * Returns: - 0 if the scb was pinned.
660 * - > 0 if control has to be given to guest 2
662 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
668 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
670 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
674 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
679 * Inject a fault into guest 2.
681 * Returns: - > 0 if control has to be given to guest 2
682 * < 0 if an error occurred during injection.
684 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
687 struct kvm_s390_pgm_info pgm = {
690 /* 0-51: virtual address */
691 (vaddr & 0xfffffffffffff000UL) |
692 /* 52-53: store / fetch */
693 (((unsigned int) !write_flag) + 1) << 10,
694 /* 62-63: asce id (alway primary == 0) */
695 .exc_access_id = 0, /* always primary */
696 .op_access_id = 0, /* not MVPG */
700 if (code == PGM_PROTECTION)
701 pgm.trans_exc_code |= 0x4UL;
703 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
708 * Handle a fault during vsie execution on a gmap shadow.
710 * Returns: - 0 if the fault was resolved
711 * - > 0 if control has to be given to guest 2
712 * - < 0 if an error occurred
714 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
718 if (current->thread.gmap_int_code == PGM_PROTECTION)
719 /* we can directly forward all protection exceptions */
720 return inject_fault(vcpu, PGM_PROTECTION,
721 current->thread.gmap_addr, 1);
723 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
724 current->thread.gmap_addr);
726 rc = inject_fault(vcpu, rc,
727 current->thread.gmap_addr,
728 current->thread.gmap_write_flag);
730 vsie_page->fault_addr = current->thread.gmap_addr;
736 * Retry the previous fault that required guest 2 intervention. This avoids
737 * one superfluous SIE re-entry and direct exit.
739 * Will ignore any errors. The next SIE fault will do proper fault handling.
741 static void handle_last_fault(struct kvm_vcpu *vcpu,
742 struct vsie_page *vsie_page)
744 if (vsie_page->fault_addr)
745 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
746 vsie_page->fault_addr);
747 vsie_page->fault_addr = 0;
750 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
752 vsie_page->scb_s.icptcode = 0;
755 /* rewind the psw and clear the vsie icpt, so we can retry execution */
756 static void retry_vsie_icpt(struct vsie_page *vsie_page)
758 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
759 int ilen = insn_length(scb_s->ipa >> 8);
761 /* take care of EXECUTE instructions */
762 if (scb_s->icptstatus & 1) {
763 ilen = (scb_s->icptstatus >> 4) & 0x6;
767 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
768 clear_vsie_icpt(vsie_page);
772 * Try to shadow + enable the guest 2 provided facility list.
773 * Retry instruction execution if enabled for and provided by guest 2.
775 * Returns: - 0 if handled (retry or guest 2 icpt)
776 * - > 0 if control has to be given to guest 2
778 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
780 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
781 __u32 fac = vsie_page->scb_o->fac & 0x7ffffff8U;
783 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
784 retry_vsie_icpt(vsie_page);
785 if (read_guest_real(vcpu, fac, &vsie_page->fac,
786 sizeof(vsie_page->fac)))
787 return set_validity_icpt(scb_s, 0x1090U);
788 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
794 * Run the vsie on a shadow scb and a shadow gmap, without any further
795 * sanity checks, handling SIE faults.
797 * Returns: - 0 everything went fine
798 * - > 0 if control has to be given to guest 2
799 * - < 0 if an error occurred
801 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
803 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
804 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
807 handle_last_fault(vcpu, vsie_page);
811 if (test_cpu_flag(CIF_MCCK_PENDING))
814 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
816 guest_enter_irqoff();
819 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
824 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
827 VCPU_EVENT(vcpu, 3, "%s", "machine check");
828 kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
833 rc = 0; /* we could still have an icpt */
834 else if (rc == -EFAULT)
835 return handle_fault(vcpu, vsie_page);
837 switch (scb_s->icptcode) {
839 if (scb_s->ipa == 0xb2b0)
840 rc = handle_stfle(vcpu, vsie_page);
843 /* stop not requested by g2 - must have been a kick */
844 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
845 clear_vsie_icpt(vsie_page);
848 if ((scb_s->ipa & 0xf000) != 0xf000)
849 scb_s->ipa += 0x1000;
855 static void release_gmap_shadow(struct vsie_page *vsie_page)
858 gmap_put(vsie_page->gmap);
859 WRITE_ONCE(vsie_page->gmap, NULL);
860 prefix_unmapped(vsie_page);
863 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
864 struct vsie_page *vsie_page)
871 asce = vcpu->arch.sie_block->gcr[1];
872 cr0.val = vcpu->arch.sie_block->gcr[0];
873 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
874 edat += edat && test_kvm_facility(vcpu->kvm, 78);
877 * ASCE or EDAT could have changed since last icpt, or the gmap
878 * we're holding has been unshadowed. If the gmap is still valid,
879 * we can safely reuse it.
881 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
884 /* release the old shadow - if any, and mark the prefix as unmapped */
885 release_gmap_shadow(vsie_page);
886 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
888 return PTR_ERR(gmap);
889 gmap->private = vcpu->kvm;
890 WRITE_ONCE(vsie_page->gmap, gmap);
895 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
897 static void register_shadow_scb(struct kvm_vcpu *vcpu,
898 struct vsie_page *vsie_page)
900 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
902 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
904 * External calls have to lead to a kick of the vcpu and
905 * therefore the vsie -> Simulate Wait state.
907 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
909 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
910 * automatically be adjusted on tod clock changes via kvm_sync_clock.
913 scb_s->epoch += vcpu->kvm->arch.epoch;
915 if (scb_s->ecd & ECD_MEF) {
916 scb_s->epdx += vcpu->kvm->arch.epdx;
917 if (scb_s->epoch < vcpu->kvm->arch.epoch)
925 * Unregister a shadow scb from a VCPU.
927 static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
929 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
930 WRITE_ONCE(vcpu->arch.vsie_block, NULL);
934 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
935 * prefix pages and faults.
937 * Returns: - 0 if no errors occurred
938 * - > 0 if control has to be given to guest 2
939 * - -ENOMEM if out of memory
941 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
943 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
947 rc = acquire_gmap_shadow(vcpu, vsie_page);
949 rc = map_prefix(vcpu, vsie_page);
951 gmap_enable(vsie_page->gmap);
952 update_intervention_requests(vsie_page);
953 rc = do_vsie_run(vcpu, vsie_page);
954 gmap_enable(vcpu->arch.gmap);
956 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
960 if (rc || scb_s->icptcode || signal_pending(current) ||
961 kvm_s390_vcpu_has_irq(vcpu, 0))
967 * Addressing exceptions are always presentes as intercepts.
968 * As addressing exceptions are suppressing and our guest 3 PSW
969 * points at the responsible instruction, we have to
970 * forward the PSW and set the ilc. If we can't read guest 3
971 * instruction, we can use an arbitrary ilc. Let's always use
972 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
973 * memory. (we could also fake the shadow so the hardware
976 scb_s->icptcode = ICPT_PROGI;
977 scb_s->iprcc = PGM_ADDRESSING;
979 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
985 * Get or create a vsie page for a scb address.
987 * Returns: - address of a vsie page (cached or new one)
988 * - NULL if the same scb address is already used by another VCPU
989 * - ERR_PTR(-ENOMEM) if out of memory
991 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
993 struct vsie_page *vsie_page;
998 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1001 if (page_ref_inc_return(page) == 2)
1002 return page_to_virt(page);
1007 * We want at least #online_vcpus shadows, so every VCPU can execute
1008 * the VSIE in parallel.
1010 nr_vcpus = atomic_read(&kvm->online_vcpus);
1012 mutex_lock(&kvm->arch.vsie.mutex);
1013 if (kvm->arch.vsie.page_count < nr_vcpus) {
1014 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
1016 mutex_unlock(&kvm->arch.vsie.mutex);
1017 return ERR_PTR(-ENOMEM);
1020 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1021 kvm->arch.vsie.page_count++;
1023 /* reuse an existing entry that belongs to nobody */
1025 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1026 if (page_ref_inc_return(page) == 2)
1029 kvm->arch.vsie.next++;
1030 kvm->arch.vsie.next %= nr_vcpus;
1032 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1035 /* double use of the same address */
1036 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1038 mutex_unlock(&kvm->arch.vsie.mutex);
1041 mutex_unlock(&kvm->arch.vsie.mutex);
1043 vsie_page = page_to_virt(page);
1044 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
1045 release_gmap_shadow(vsie_page);
1046 vsie_page->fault_addr = 0;
1047 vsie_page->scb_s.ihcpu = 0xffffU;
1051 /* put a vsie page acquired via get_vsie_page */
1052 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1054 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1059 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1061 struct vsie_page *vsie_page;
1062 unsigned long scb_addr;
1065 vcpu->stat.instruction_sie++;
1066 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1068 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1069 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1071 BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
1072 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1074 /* 512 byte alignment */
1075 if (unlikely(scb_addr & 0x1ffUL))
1076 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1078 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1081 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1082 if (IS_ERR(vsie_page))
1083 return PTR_ERR(vsie_page);
1084 else if (!vsie_page)
1085 /* double use of sie control block - simply do nothing */
1088 rc = pin_scb(vcpu, vsie_page, scb_addr);
1091 rc = shadow_scb(vcpu, vsie_page);
1094 rc = pin_blocks(vcpu, vsie_page);
1097 register_shadow_scb(vcpu, vsie_page);
1098 rc = vsie_run(vcpu, vsie_page);
1099 unregister_shadow_scb(vcpu);
1100 unpin_blocks(vcpu, vsie_page);
1102 unshadow_scb(vcpu, vsie_page);
1104 unpin_scb(vcpu, vsie_page, scb_addr);
1106 put_vsie_page(vcpu->kvm, vsie_page);
1108 return rc < 0 ? rc : 0;
1111 /* Init the vsie data structures. To be called when a vm is initialized. */
1112 void kvm_s390_vsie_init(struct kvm *kvm)
1114 mutex_init(&kvm->arch.vsie.mutex);
1115 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1118 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
1119 void kvm_s390_vsie_destroy(struct kvm *kvm)
1121 struct vsie_page *vsie_page;
1125 mutex_lock(&kvm->arch.vsie.mutex);
1126 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1127 page = kvm->arch.vsie.pages[i];
1128 kvm->arch.vsie.pages[i] = NULL;
1129 vsie_page = page_to_virt(page);
1130 release_gmap_shadow(vsie_page);
1131 /* free the radix tree entry */
1132 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1135 kvm->arch.vsie.page_count = 0;
1136 mutex_unlock(&kvm->arch.vsie.mutex);
1139 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1141 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1144 * Even if the VCPU lets go of the shadow sie block reference, it is
1145 * still valid in the cache. So we can safely kick it.
1148 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1149 if (scb->prog0c & PROG_IN_SIE)
1150 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);