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>
18 #include <asm/mmu_context.h>
26 struct kvm_s390_sie_block scb_s; /* 0x0000 */
27 /* the pinned originial scb */
28 struct kvm_s390_sie_block *scb_o; /* 0x0200 */
29 /* the shadow gmap in use by the vsie_page */
30 struct gmap *gmap; /* 0x0208 */
31 __u8 reserved[0x0700 - 0x0210]; /* 0x0210 */
32 struct kvm_s390_crypto_cb crycb; /* 0x0700 */
33 __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */
36 /* trigger a validity icpt for the given scb */
37 static int set_validity_icpt(struct kvm_s390_sie_block *scb,
41 scb->ipb = ((__u32) reason_code) << 16;
42 scb->icptcode = ICPT_VALIDITY;
46 /* mark the prefix as unmapped, this will block the VSIE */
47 static void prefix_unmapped(struct vsie_page *vsie_page)
49 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
52 /* mark the prefix as unmapped and wait until the VSIE has been left */
53 static void prefix_unmapped_sync(struct vsie_page *vsie_page)
55 prefix_unmapped(vsie_page);
56 if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
57 atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
58 while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
62 /* mark the prefix as mapped, this will allow the VSIE to run */
63 static void prefix_mapped(struct vsie_page *vsie_page)
65 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
68 /* test if the prefix is mapped into the gmap shadow */
69 static int prefix_is_mapped(struct vsie_page *vsie_page)
71 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
74 /* copy the updated intervention request bits into the shadow scb */
75 static void update_intervention_requests(struct vsie_page *vsie_page)
77 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
80 cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
81 atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
82 atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
85 /* shadow (filter and validate) the cpuflags */
86 static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
88 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
89 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
90 int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
92 /* we don't allow ESA/390 guests */
93 if (!(cpuflags & CPUSTAT_ZARCH))
94 return set_validity_icpt(scb_s, 0x0001U);
96 if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
97 return set_validity_icpt(scb_s, 0x0001U);
98 else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
99 return set_validity_icpt(scb_s, 0x0007U);
101 /* intervention requests will be set later */
102 newflags = CPUSTAT_ZARCH;
103 if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8))
104 newflags |= CPUSTAT_GED;
105 if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) {
106 if (cpuflags & CPUSTAT_GED)
107 return set_validity_icpt(scb_s, 0x0001U);
108 newflags |= CPUSTAT_GED2;
110 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE))
111 newflags |= cpuflags & CPUSTAT_P;
113 atomic_set(&scb_s->cpuflags, newflags);
118 * Create a shadow copy of the crycb block and setup key wrapping, if
119 * requested for guest 3 and enabled for guest 2.
121 * We only accept format-1 (no AP in g2), but convert it into format-2
122 * There is nothing to do for format-0.
124 * Returns: - 0 if shadowed or nothing to do
125 * - > 0 if control has to be given to guest 2
127 static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
129 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
130 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
131 u32 crycb_addr = scb_o->crycbd & 0x7ffffff8U;
132 unsigned long *b1, *b2;
136 if (!(scb_o->crycbd & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
138 /* format-1 is supported with message-security-assist extension 3 */
139 if (!test_kvm_facility(vcpu->kvm, 76))
141 /* we may only allow it if enabled for guest 2 */
142 ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
143 (ECB3_AES | ECB3_DEA);
147 if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
148 return set_validity_icpt(scb_s, 0x003CU);
149 else if (!crycb_addr)
150 return set_validity_icpt(scb_s, 0x0039U);
152 /* copy only the wrapping keys */
153 if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
154 return set_validity_icpt(scb_s, 0x0035U);
156 scb_s->ecb3 |= ecb3_flags;
157 scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
160 /* xor both blocks in one run */
161 b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
162 b2 = (unsigned long *)
163 vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
164 /* as 56%8 == 0, bitmap_xor won't overwrite any data */
165 bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
169 /* shadow (round up/down) the ibc to avoid validity icpt */
170 static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
172 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
173 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
174 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
177 /* ibc installed in g2 and requested for g3 */
178 if (vcpu->kvm->arch.model.ibc && (scb_o->ibc & 0x0fffU)) {
179 scb_s->ibc = scb_o->ibc & 0x0fffU;
180 /* takte care of the minimum ibc level of the machine */
181 if (scb_s->ibc < min_ibc)
182 scb_s->ibc = min_ibc;
183 /* take care of the maximum ibc level set for the guest */
184 if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
185 scb_s->ibc = vcpu->kvm->arch.model.ibc;
189 /* unshadow the scb, copying parameters back to the real scb */
190 static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
192 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
193 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
196 scb_o->icptcode = scb_s->icptcode;
197 scb_o->icptstatus = scb_s->icptstatus;
198 scb_o->ipa = scb_s->ipa;
199 scb_o->ipb = scb_s->ipb;
200 scb_o->gbea = scb_s->gbea;
203 scb_o->cputm = scb_s->cputm;
204 scb_o->ckc = scb_s->ckc;
205 scb_o->todpr = scb_s->todpr;
208 scb_o->gpsw = scb_s->gpsw;
209 scb_o->gg14 = scb_s->gg14;
210 scb_o->gg15 = scb_s->gg15;
211 memcpy(scb_o->gcr, scb_s->gcr, 128);
212 scb_o->pp = scb_s->pp;
214 /* interrupt intercept */
215 switch (scb_s->icptcode) {
219 memcpy((void *)((u64)scb_o + 0xc0),
220 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
224 memcpy((void *)((u64)scb_o + 0xc0),
225 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
229 if (scb_s->ihcpu != 0xffffU)
230 scb_o->ihcpu = scb_s->ihcpu;
234 * Setup the shadow scb by copying and checking the relevant parts of the g2
237 * Returns: - 0 if the scb has been shadowed
238 * - > 0 if control has to be given to guest 2
240 static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
242 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
243 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
244 bool had_tx = scb_s->ecb & 0x10U;
245 unsigned long new_mso;
248 /* make sure we don't have any leftovers when reusing the scb */
257 rc = prepare_cpuflags(vcpu, vsie_page);
262 scb_s->cputm = scb_o->cputm;
263 scb_s->ckc = scb_o->ckc;
264 scb_s->todpr = scb_o->todpr;
265 scb_s->epoch = scb_o->epoch;
268 scb_s->gpsw = scb_o->gpsw;
269 scb_s->gg14 = scb_o->gg14;
270 scb_s->gg15 = scb_o->gg15;
271 memcpy(scb_s->gcr, scb_o->gcr, 128);
272 scb_s->pp = scb_o->pp;
274 /* interception / execution handling */
275 scb_s->gbea = scb_o->gbea;
276 scb_s->lctl = scb_o->lctl;
277 scb_s->svcc = scb_o->svcc;
278 scb_s->ictl = scb_o->ictl;
280 * SKEY handling functions can't deal with false setting of PTE invalid
281 * bits. Therefore we cannot provide interpretation and would later
282 * have to provide own emulation handlers.
284 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
285 scb_s->icpua = scb_o->icpua;
287 new_mso = scb_o->mso & 0xfffffffffff00000UL;
288 /* if the hva of the prefix changes, we have to remap the prefix */
289 if (scb_s->mso != new_mso || scb_s->prefix != scb_o->prefix)
290 prefix_unmapped(vsie_page);
291 /* SIE will do mso/msl validity and exception checks for us */
292 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
293 scb_s->mso = new_mso;
294 scb_s->prefix = scb_o->prefix;
296 /* We have to definetly flush the tlb if this scb never ran */
297 if (scb_s->ihcpu != 0xffffU)
298 scb_s->ihcpu = scb_o->ihcpu;
300 /* MVPG and Protection Exception Interpretation are always available */
301 scb_s->eca |= scb_o->eca & 0x01002000U;
302 /* Host-protection-interruption introduced with ESOP */
303 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
304 scb_s->ecb |= scb_o->ecb & 0x02U;
305 /* transactional execution */
306 if (test_kvm_facility(vcpu->kvm, 73)) {
307 /* remap the prefix is tx is toggled on */
308 if ((scb_o->ecb & 0x10U) && !had_tx)
309 prefix_unmapped(vsie_page);
310 scb_s->ecb |= scb_o->ecb & 0x10U;
313 if (test_kvm_facility(vcpu->kvm, 129)) {
314 scb_s->eca |= scb_o->eca & 0x00020000U;
315 scb_s->ecd |= scb_o->ecd & 0x20000000U;
317 /* Run-time-Instrumentation */
318 if (test_kvm_facility(vcpu->kvm, 64))
319 scb_s->ecb3 |= scb_o->ecb3 & 0x01U;
320 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
321 scb_s->eca |= scb_o->eca & 0x00000001U;
323 prepare_ibc(vcpu, vsie_page);
324 rc = shadow_crycb(vcpu, vsie_page);
327 unshadow_scb(vcpu, vsie_page);
331 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
334 struct kvm *kvm = gmap->private;
335 struct vsie_page *cur;
336 unsigned long prefix;
340 if (!gmap_is_shadow(gmap))
342 if (start >= 1UL << 31)
343 /* We are only interested in prefix pages */
347 * Only new shadow blocks are added to the list during runtime,
348 * therefore we can safely reference them all the time.
350 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
351 page = READ_ONCE(kvm->arch.vsie.pages[i]);
354 cur = page_to_virt(page);
355 if (READ_ONCE(cur->gmap) != gmap)
357 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
358 /* with mso/msl, the prefix lies at an offset */
359 prefix += cur->scb_s.mso;
360 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
361 prefix_unmapped_sync(cur);
366 * Map the first prefix page and if tx is enabled also the second prefix page.
368 * The prefix will be protected, a gmap notifier will inform about unmaps.
369 * The shadow scb must not be executed until the prefix is remapped, this is
370 * guaranteed by properly handling PROG_REQUEST.
372 * Returns: - 0 on if successfully mapped or already mapped
373 * - > 0 if control has to be given to guest 2
374 * - -EAGAIN if the caller can retry immediately
375 * - -ENOMEM if out of memory
377 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
379 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
380 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
383 if (prefix_is_mapped(vsie_page))
386 /* mark it as mapped so we can catch any concurrent unmappers */
387 prefix_mapped(vsie_page);
389 /* with mso/msl, the prefix lies at offset *mso* */
390 prefix += scb_s->mso;
392 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
393 if (!rc && (scb_s->ecb & 0x10U))
394 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
397 * We don't have to mprotect, we will be called for all unshadows.
398 * SIE will detect if protection applies and trigger a validity.
401 prefix_unmapped(vsie_page);
402 if (rc > 0 || rc == -EFAULT)
403 rc = set_validity_icpt(scb_s, 0x0037U);
408 * Pin the guest page given by gpa and set hpa to the pinned host address.
409 * Will always be pinned writable.
411 * Returns: - 0 on success
412 * - -EINVAL if the gpa is not valid guest storage
413 * - -ENOMEM if out of memory
415 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
421 hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
422 if (kvm_is_error_hva(hva))
424 rc = get_user_pages_fast(hva, 1, 1, &page);
429 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
433 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
434 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
438 page = virt_to_page(hpa);
439 set_page_dirty_lock(page);
441 /* mark the page always as dirty for migration */
442 mark_page_dirty(kvm, gpa_to_gfn(gpa));
445 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
446 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
448 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
449 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
453 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
455 gpa = scb_o->scaol & ~0xfUL;
456 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
457 gpa |= (u64) scb_o->scaoh << 32;
458 unpin_guest_page(vcpu->kvm, gpa, hpa);
465 gpa = scb_o->itdba & ~0xffUL;
466 unpin_guest_page(vcpu->kvm, gpa, hpa);
472 gpa = scb_o->gvrd & ~0x1ffUL;
473 unpin_guest_page(vcpu->kvm, gpa, hpa);
479 gpa = scb_o->riccbd & ~0x3fUL;
480 unpin_guest_page(vcpu->kvm, gpa, hpa);
486 * Instead of shadowing some blocks, we can simply forward them because the
487 * addresses in the scb are 64 bit long.
489 * This works as long as the data lies in one page. If blocks ever exceed one
490 * page, we have to fall back to shadowing.
492 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
493 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
495 * Returns: - 0 if all blocks were pinned.
496 * - > 0 if control has to be given to guest 2
497 * - -ENOMEM if out of memory
499 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
501 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
502 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
507 gpa = scb_o->scaol & ~0xfUL;
508 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
509 gpa |= (u64) scb_o->scaoh << 32;
511 if (!(gpa & ~0x1fffUL))
512 rc = set_validity_icpt(scb_s, 0x0038U);
513 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
514 rc = set_validity_icpt(scb_s, 0x0011U);
515 else if ((gpa & PAGE_MASK) !=
516 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
517 rc = set_validity_icpt(scb_s, 0x003bU);
519 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
521 rc = set_validity_icpt(scb_s, 0x0034U);
525 scb_s->scaoh = (u32)((u64)hpa >> 32);
526 scb_s->scaol = (u32)(u64)hpa;
529 gpa = scb_o->itdba & ~0xffUL;
530 if (gpa && (scb_s->ecb & 0x10U)) {
531 if (!(gpa & ~0x1fffU)) {
532 rc = set_validity_icpt(scb_s, 0x0080U);
535 /* 256 bytes cannot cross page boundaries */
536 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
538 rc = set_validity_icpt(scb_s, 0x0080U);
544 gpa = scb_o->gvrd & ~0x1ffUL;
545 if (gpa && (scb_s->eca & 0x00020000U) &&
546 !(scb_s->ecd & 0x20000000U)) {
547 if (!(gpa & ~0x1fffUL)) {
548 rc = set_validity_icpt(scb_s, 0x1310U);
552 * 512 bytes vector registers cannot cross page boundaries
553 * if this block gets bigger, we have to shadow it.
555 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
557 rc = set_validity_icpt(scb_s, 0x1310U);
563 gpa = scb_o->riccbd & ~0x3fUL;
564 if (gpa && (scb_s->ecb3 & 0x01U)) {
565 if (!(gpa & ~0x1fffUL)) {
566 rc = set_validity_icpt(scb_s, 0x0043U);
569 /* 64 bytes cannot cross page boundaries */
570 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
572 rc = set_validity_icpt(scb_s, 0x0043U);
573 /* Validity 0x0044 will be checked by SIE */
580 unpin_blocks(vcpu, vsie_page);
584 /* unpin the scb provided by guest 2, marking it as dirty */
585 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
588 hpa_t hpa = (hpa_t) vsie_page->scb_o;
591 unpin_guest_page(vcpu->kvm, gpa, hpa);
592 vsie_page->scb_o = NULL;
596 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
598 * Returns: - 0 if the scb was pinned.
599 * - > 0 if control has to be given to guest 2
600 * - -ENOMEM if out of memory
602 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
608 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
610 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
615 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
620 * Inject a fault into guest 2.
622 * Returns: - > 0 if control has to be given to guest 2
623 * < 0 if an error occurred during injection.
625 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
628 struct kvm_s390_pgm_info pgm = {
631 /* 0-51: virtual address */
632 (vaddr & 0xfffffffffffff000UL) |
633 /* 52-53: store / fetch */
634 (((unsigned int) !write_flag) + 1) << 10,
635 /* 62-63: asce id (alway primary == 0) */
636 .exc_access_id = 0, /* always primary */
637 .op_access_id = 0, /* not MVPG */
641 if (code == PGM_PROTECTION)
642 pgm.trans_exc_code |= 0x4UL;
644 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
649 * Handle a fault during vsie execution on a gmap shadow.
651 * Returns: - 0 if the fault was resolved
652 * - > 0 if control has to be given to guest 2
653 * - < 0 if an error occurred
655 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
659 if (current->thread.gmap_int_code == PGM_PROTECTION)
660 /* we can directly forward all protection exceptions */
661 return inject_fault(vcpu, PGM_PROTECTION,
662 current->thread.gmap_addr, 1);
664 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
665 current->thread.gmap_addr);
667 rc = inject_fault(vcpu, rc,
668 current->thread.gmap_addr,
669 current->thread.gmap_write_flag);
674 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
676 vsie_page->scb_s.icptcode = 0;
679 /* rewind the psw and clear the vsie icpt, so we can retry execution */
680 static void retry_vsie_icpt(struct vsie_page *vsie_page)
682 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
683 int ilen = insn_length(scb_s->ipa >> 8);
685 /* take care of EXECUTE instructions */
686 if (scb_s->icptstatus & 1) {
687 ilen = (scb_s->icptstatus >> 4) & 0x6;
691 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
692 clear_vsie_icpt(vsie_page);
696 * Try to shadow + enable the guest 2 provided facility list.
697 * Retry instruction execution if enabled for and provided by guest 2.
699 * Returns: - 0 if handled (retry or guest 2 icpt)
700 * - > 0 if control has to be given to guest 2
702 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
704 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
705 __u32 fac = vsie_page->scb_o->fac & 0x7ffffff8U;
707 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
708 retry_vsie_icpt(vsie_page);
709 if (read_guest_real(vcpu, fac, &vsie_page->fac,
710 sizeof(vsie_page->fac)))
711 return set_validity_icpt(scb_s, 0x1090U);
712 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
718 * Run the vsie on a shadow scb and a shadow gmap, without any further
719 * sanity checks, handling SIE faults.
721 * Returns: - 0 everything went fine
722 * - > 0 if control has to be given to guest 2
723 * - < 0 if an error occurred
725 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
727 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
728 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
733 if (test_cpu_flag(CIF_MCCK_PENDING))
736 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
741 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
746 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
749 rc = 0; /* we could still have an icpt */
750 else if (rc == -EFAULT)
751 return handle_fault(vcpu, vsie_page);
753 switch (scb_s->icptcode) {
755 if (scb_s->ipa == 0xb2b0)
756 rc = handle_stfle(vcpu, vsie_page);
759 /* stop not requested by g2 - must have been a kick */
760 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
761 clear_vsie_icpt(vsie_page);
764 if ((scb_s->ipa & 0xf000) != 0xf000)
765 scb_s->ipa += 0x1000;
771 static void release_gmap_shadow(struct vsie_page *vsie_page)
774 gmap_put(vsie_page->gmap);
775 WRITE_ONCE(vsie_page->gmap, NULL);
776 prefix_unmapped(vsie_page);
779 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
780 struct vsie_page *vsie_page)
787 asce = vcpu->arch.sie_block->gcr[1];
788 cr0.val = vcpu->arch.sie_block->gcr[0];
789 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
790 edat += edat && test_kvm_facility(vcpu->kvm, 78);
793 * ASCE or EDAT could have changed since last icpt, or the gmap
794 * we're holding has been unshadowed. If the gmap is still valid,
795 * we can safely reuse it.
797 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
800 /* release the old shadow - if any, and mark the prefix as unmapped */
801 release_gmap_shadow(vsie_page);
802 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
804 return PTR_ERR(gmap);
805 gmap->private = vcpu->kvm;
806 WRITE_ONCE(vsie_page->gmap, gmap);
811 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
812 * prefix pages and faults.
814 * Returns: - 0 if no errors occurred
815 * - > 0 if control has to be given to guest 2
816 * - -ENOMEM if out of memory
818 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
820 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
824 rc = acquire_gmap_shadow(vcpu, vsie_page);
826 rc = map_prefix(vcpu, vsie_page);
828 gmap_enable(vsie_page->gmap);
829 update_intervention_requests(vsie_page);
830 rc = do_vsie_run(vcpu, vsie_page);
831 gmap_enable(vcpu->arch.gmap);
836 if (rc || scb_s->icptcode || signal_pending(current) ||
837 kvm_s390_vcpu_has_irq(vcpu, 0))
843 * Addressing exceptions are always presentes as intercepts.
844 * As addressing exceptions are suppressing and our guest 3 PSW
845 * points at the responsible instruction, we have to
846 * forward the PSW and set the ilc. If we can't read guest 3
847 * instruction, we can use an arbitrary ilc. Let's always use
848 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
849 * memory. (we could also fake the shadow so the hardware
852 scb_s->icptcode = ICPT_PROGI;
853 scb_s->iprcc = PGM_ADDRESSING;
855 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
861 * Get or create a vsie page for a scb address.
863 * Returns: - address of a vsie page (cached or new one)
864 * - NULL if the same scb address is already used by another VCPU
865 * - ERR_PTR(-ENOMEM) if out of memory
867 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
869 struct vsie_page *vsie_page;
874 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
877 if (page_ref_inc_return(page) == 2)
878 return page_to_virt(page);
883 * We want at least #online_vcpus shadows, so every VCPU can execute
884 * the VSIE in parallel.
886 nr_vcpus = atomic_read(&kvm->online_vcpus);
888 mutex_lock(&kvm->arch.vsie.mutex);
889 if (kvm->arch.vsie.page_count < nr_vcpus) {
890 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
892 mutex_unlock(&kvm->arch.vsie.mutex);
893 return ERR_PTR(-ENOMEM);
896 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
897 kvm->arch.vsie.page_count++;
899 /* reuse an existing entry that belongs to nobody */
901 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
902 if (page_ref_inc_return(page) == 2)
905 kvm->arch.vsie.next++;
906 kvm->arch.vsie.next %= nr_vcpus;
908 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
911 /* double use of the same address */
912 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
914 mutex_unlock(&kvm->arch.vsie.mutex);
917 mutex_unlock(&kvm->arch.vsie.mutex);
919 vsie_page = page_to_virt(page);
920 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
921 release_gmap_shadow(vsie_page);
922 vsie_page->scb_s.ihcpu = 0xffffU;
926 /* put a vsie page acquired via get_vsie_page */
927 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
929 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
934 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
936 struct vsie_page *vsie_page;
937 unsigned long scb_addr;
940 vcpu->stat.instruction_sie++;
941 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
943 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
944 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
946 BUILD_BUG_ON(sizeof(struct vsie_page) != 4096);
947 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
949 /* 512 byte alignment */
950 if (unlikely(scb_addr & 0x1ffUL))
951 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
953 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
956 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
957 if (IS_ERR(vsie_page))
958 return PTR_ERR(vsie_page);
960 /* double use of sie control block - simply do nothing */
963 rc = pin_scb(vcpu, vsie_page, scb_addr);
966 rc = shadow_scb(vcpu, vsie_page);
969 rc = pin_blocks(vcpu, vsie_page);
972 rc = vsie_run(vcpu, vsie_page);
973 unpin_blocks(vcpu, vsie_page);
975 unshadow_scb(vcpu, vsie_page);
977 unpin_scb(vcpu, vsie_page, scb_addr);
979 put_vsie_page(vcpu->kvm, vsie_page);
981 return rc < 0 ? rc : 0;
984 /* Init the vsie data structures. To be called when a vm is initialized. */
985 void kvm_s390_vsie_init(struct kvm *kvm)
987 mutex_init(&kvm->arch.vsie.mutex);
988 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
991 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
992 void kvm_s390_vsie_destroy(struct kvm *kvm)
994 struct vsie_page *vsie_page;
998 mutex_lock(&kvm->arch.vsie.mutex);
999 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1000 page = kvm->arch.vsie.pages[i];
1001 kvm->arch.vsie.pages[i] = NULL;
1002 vsie_page = page_to_virt(page);
1003 release_gmap_shadow(vsie_page);
1004 /* free the radix tree entry */
1005 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1008 kvm->arch.vsie.page_count = 0;
1009 mutex_unlock(&kvm->arch.vsie.mutex);