xref: /openbmc/linux/arch/s390/kvm/vsie.c (revision 5303b8d3)
1 /*
2  * kvm nested virtualization support for s390x
3  *
4  * Copyright IBM Corp. 2016
5  *
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.
9  *
10  *    Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
11  */
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>
18 
19 #include <asm/gmap.h>
20 #include <asm/mmu_context.h>
21 #include <asm/sclp.h>
22 #include <asm/nmi.h>
23 #include <asm/dis.h>
24 #include "kvm-s390.h"
25 #include "gaccess.h"
26 
27 struct vsie_page {
28 	struct kvm_s390_sie_block scb_s;	/* 0x0000 */
29 	/*
30 	 * the backup info for machine check. ensure it's at
31 	 * the same offset as that in struct sie_page!
32 	 */
33 	struct mcck_volatile_info mcck_info;    /* 0x0200 */
34 	/* the pinned originial scb */
35 	struct kvm_s390_sie_block *scb_o;	/* 0x0218 */
36 	/* the shadow gmap in use by the vsie_page */
37 	struct gmap *gmap;			/* 0x0220 */
38 	/* address of the last reported fault to guest2 */
39 	unsigned long fault_addr;		/* 0x0228 */
40 	__u8 reserved[0x0700 - 0x0230];		/* 0x0230 */
41 	struct kvm_s390_crypto_cb crycb;	/* 0x0700 */
42 	__u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE];	/* 0x0800 */
43 };
44 
45 /* trigger a validity icpt for the given scb */
46 static int set_validity_icpt(struct kvm_s390_sie_block *scb,
47 			     __u16 reason_code)
48 {
49 	scb->ipa = 0x1000;
50 	scb->ipb = ((__u32) reason_code) << 16;
51 	scb->icptcode = ICPT_VALIDITY;
52 	return 1;
53 }
54 
55 /* mark the prefix as unmapped, this will block the VSIE */
56 static void prefix_unmapped(struct vsie_page *vsie_page)
57 {
58 	atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
59 }
60 
61 /* mark the prefix as unmapped and wait until the VSIE has been left */
62 static void prefix_unmapped_sync(struct vsie_page *vsie_page)
63 {
64 	prefix_unmapped(vsie_page);
65 	if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
66 		atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
67 	while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
68 		cpu_relax();
69 }
70 
71 /* mark the prefix as mapped, this will allow the VSIE to run */
72 static void prefix_mapped(struct vsie_page *vsie_page)
73 {
74 	atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
75 }
76 
77 /* test if the prefix is mapped into the gmap shadow */
78 static int prefix_is_mapped(struct vsie_page *vsie_page)
79 {
80 	return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
81 }
82 
83 /* copy the updated intervention request bits into the shadow scb */
84 static void update_intervention_requests(struct vsie_page *vsie_page)
85 {
86 	const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
87 	int cpuflags;
88 
89 	cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
90 	atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
91 	atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
92 }
93 
94 /* shadow (filter and validate) the cpuflags  */
95 static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
96 {
97 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
98 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
99 	int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
100 
101 	/* we don't allow ESA/390 guests */
102 	if (!(cpuflags & CPUSTAT_ZARCH))
103 		return set_validity_icpt(scb_s, 0x0001U);
104 
105 	if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
106 		return set_validity_icpt(scb_s, 0x0001U);
107 	else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
108 		return set_validity_icpt(scb_s, 0x0007U);
109 
110 	/* intervention requests will be set later */
111 	newflags = CPUSTAT_ZARCH;
112 	if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8))
113 		newflags |= CPUSTAT_GED;
114 	if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) {
115 		if (cpuflags & CPUSTAT_GED)
116 			return set_validity_icpt(scb_s, 0x0001U);
117 		newflags |= CPUSTAT_GED2;
118 	}
119 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE))
120 		newflags |= cpuflags & CPUSTAT_P;
121 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS))
122 		newflags |= cpuflags & CPUSTAT_SM;
123 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS))
124 		newflags |= cpuflags & CPUSTAT_IBS;
125 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_KSS))
126 		newflags |= cpuflags & CPUSTAT_KSS;
127 
128 	atomic_set(&scb_s->cpuflags, newflags);
129 	return 0;
130 }
131 
132 /*
133  * Create a shadow copy of the crycb block and setup key wrapping, if
134  * requested for guest 3 and enabled for guest 2.
135  *
136  * We only accept format-1 (no AP in g2), but convert it into format-2
137  * There is nothing to do for format-0.
138  *
139  * Returns: - 0 if shadowed or nothing to do
140  *          - > 0 if control has to be given to guest 2
141  */
142 static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
143 {
144 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
145 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
146 	u32 crycb_addr = scb_o->crycbd & 0x7ffffff8U;
147 	unsigned long *b1, *b2;
148 	u8 ecb3_flags;
149 
150 	scb_s->crycbd = 0;
151 	if (!(scb_o->crycbd & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
152 		return 0;
153 	/* format-1 is supported with message-security-assist extension 3 */
154 	if (!test_kvm_facility(vcpu->kvm, 76))
155 		return 0;
156 	/* we may only allow it if enabled for guest 2 */
157 	ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
158 		     (ECB3_AES | ECB3_DEA);
159 	if (!ecb3_flags)
160 		return 0;
161 
162 	if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
163 		return set_validity_icpt(scb_s, 0x003CU);
164 	else if (!crycb_addr)
165 		return set_validity_icpt(scb_s, 0x0039U);
166 
167 	/* copy only the wrapping keys */
168 	if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
169 		return set_validity_icpt(scb_s, 0x0035U);
170 
171 	scb_s->ecb3 |= ecb3_flags;
172 	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
173 			CRYCB_FORMAT2;
174 
175 	/* xor both blocks in one run */
176 	b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
177 	b2 = (unsigned long *)
178 			    vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
179 	/* as 56%8 == 0, bitmap_xor won't overwrite any data */
180 	bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
181 	return 0;
182 }
183 
184 /* shadow (round up/down) the ibc to avoid validity icpt */
185 static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
186 {
187 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
188 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
189 	__u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
190 
191 	scb_s->ibc = 0;
192 	/* ibc installed in g2 and requested for g3 */
193 	if (vcpu->kvm->arch.model.ibc && (scb_o->ibc & 0x0fffU)) {
194 		scb_s->ibc = scb_o->ibc & 0x0fffU;
195 		/* takte care of the minimum ibc level of the machine */
196 		if (scb_s->ibc < min_ibc)
197 			scb_s->ibc = min_ibc;
198 		/* take care of the maximum ibc level set for the guest */
199 		if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
200 			scb_s->ibc = vcpu->kvm->arch.model.ibc;
201 	}
202 }
203 
204 /* unshadow the scb, copying parameters back to the real scb */
205 static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
206 {
207 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
208 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
209 
210 	/* interception */
211 	scb_o->icptcode = scb_s->icptcode;
212 	scb_o->icptstatus = scb_s->icptstatus;
213 	scb_o->ipa = scb_s->ipa;
214 	scb_o->ipb = scb_s->ipb;
215 	scb_o->gbea = scb_s->gbea;
216 
217 	/* timer */
218 	scb_o->cputm = scb_s->cputm;
219 	scb_o->ckc = scb_s->ckc;
220 	scb_o->todpr = scb_s->todpr;
221 
222 	/* guest state */
223 	scb_o->gpsw = scb_s->gpsw;
224 	scb_o->gg14 = scb_s->gg14;
225 	scb_o->gg15 = scb_s->gg15;
226 	memcpy(scb_o->gcr, scb_s->gcr, 128);
227 	scb_o->pp = scb_s->pp;
228 
229 	/* interrupt intercept */
230 	switch (scb_s->icptcode) {
231 	case ICPT_PROGI:
232 	case ICPT_INSTPROGI:
233 	case ICPT_EXTINT:
234 		memcpy((void *)((u64)scb_o + 0xc0),
235 		       (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
236 		break;
237 	case ICPT_PARTEXEC:
238 		/* MVPG only */
239 		memcpy((void *)((u64)scb_o + 0xc0),
240 		       (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
241 		break;
242 	}
243 
244 	if (scb_s->ihcpu != 0xffffU)
245 		scb_o->ihcpu = scb_s->ihcpu;
246 }
247 
248 /*
249  * Setup the shadow scb by copying and checking the relevant parts of the g2
250  * provided scb.
251  *
252  * Returns: - 0 if the scb has been shadowed
253  *          - > 0 if control has to be given to guest 2
254  */
255 static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
256 {
257 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
258 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
259 	bool had_tx = scb_s->ecb & ECB_TE;
260 	unsigned long new_mso = 0;
261 	int rc;
262 
263 	/* make sure we don't have any leftovers when reusing the scb */
264 	scb_s->icptcode = 0;
265 	scb_s->eca = 0;
266 	scb_s->ecb = 0;
267 	scb_s->ecb2 = 0;
268 	scb_s->ecb3 = 0;
269 	scb_s->ecd = 0;
270 	scb_s->fac = 0;
271 
272 	rc = prepare_cpuflags(vcpu, vsie_page);
273 	if (rc)
274 		goto out;
275 
276 	/* timer */
277 	scb_s->cputm = scb_o->cputm;
278 	scb_s->ckc = scb_o->ckc;
279 	scb_s->todpr = scb_o->todpr;
280 	scb_s->epoch = scb_o->epoch;
281 
282 	/* guest state */
283 	scb_s->gpsw = scb_o->gpsw;
284 	scb_s->gg14 = scb_o->gg14;
285 	scb_s->gg15 = scb_o->gg15;
286 	memcpy(scb_s->gcr, scb_o->gcr, 128);
287 	scb_s->pp = scb_o->pp;
288 
289 	/* interception / execution handling */
290 	scb_s->gbea = scb_o->gbea;
291 	scb_s->lctl = scb_o->lctl;
292 	scb_s->svcc = scb_o->svcc;
293 	scb_s->ictl = scb_o->ictl;
294 	/*
295 	 * SKEY handling functions can't deal with false setting of PTE invalid
296 	 * bits. Therefore we cannot provide interpretation and would later
297 	 * have to provide own emulation handlers.
298 	 */
299 	if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
300 		scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
301 
302 	scb_s->icpua = scb_o->icpua;
303 
304 	if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
305 		new_mso = scb_o->mso & 0xfffffffffff00000UL;
306 	/* if the hva of the prefix changes, we have to remap the prefix */
307 	if (scb_s->mso != new_mso || scb_s->prefix != scb_o->prefix)
308 		prefix_unmapped(vsie_page);
309 	 /* SIE will do mso/msl validity and exception checks for us */
310 	scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
311 	scb_s->mso = new_mso;
312 	scb_s->prefix = scb_o->prefix;
313 
314 	/* We have to definetly flush the tlb if this scb never ran */
315 	if (scb_s->ihcpu != 0xffffU)
316 		scb_s->ihcpu = scb_o->ihcpu;
317 
318 	/* MVPG and Protection Exception Interpretation are always available */
319 	scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
320 	/* Host-protection-interruption introduced with ESOP */
321 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
322 		scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
323 	/* transactional execution */
324 	if (test_kvm_facility(vcpu->kvm, 73)) {
325 		/* remap the prefix is tx is toggled on */
326 		if ((scb_o->ecb & ECB_TE) && !had_tx)
327 			prefix_unmapped(vsie_page);
328 		scb_s->ecb |= scb_o->ecb & ECB_TE;
329 	}
330 	/* SIMD */
331 	if (test_kvm_facility(vcpu->kvm, 129)) {
332 		scb_s->eca |= scb_o->eca & ECA_VX;
333 		scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
334 	}
335 	/* Run-time-Instrumentation */
336 	if (test_kvm_facility(vcpu->kvm, 64))
337 		scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
338 	/* Instruction Execution Prevention */
339 	if (test_kvm_facility(vcpu->kvm, 130))
340 		scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
341 	/* Guarded Storage */
342 	if (test_kvm_facility(vcpu->kvm, 133)) {
343 		scb_s->ecb |= scb_o->ecb & ECB_GS;
344 		scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
345 	}
346 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
347 		scb_s->eca |= scb_o->eca & ECA_SII;
348 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
349 		scb_s->eca |= scb_o->eca & ECA_IB;
350 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
351 		scb_s->eca |= scb_o->eca & ECA_CEI;
352 
353 	prepare_ibc(vcpu, vsie_page);
354 	rc = shadow_crycb(vcpu, vsie_page);
355 out:
356 	if (rc)
357 		unshadow_scb(vcpu, vsie_page);
358 	return rc;
359 }
360 
361 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
362 				 unsigned long end)
363 {
364 	struct kvm *kvm = gmap->private;
365 	struct vsie_page *cur;
366 	unsigned long prefix;
367 	struct page *page;
368 	int i;
369 
370 	if (!gmap_is_shadow(gmap))
371 		return;
372 	if (start >= 1UL << 31)
373 		/* We are only interested in prefix pages */
374 		return;
375 
376 	/*
377 	 * Only new shadow blocks are added to the list during runtime,
378 	 * therefore we can safely reference them all the time.
379 	 */
380 	for (i = 0; i < kvm->arch.vsie.page_count; i++) {
381 		page = READ_ONCE(kvm->arch.vsie.pages[i]);
382 		if (!page)
383 			continue;
384 		cur = page_to_virt(page);
385 		if (READ_ONCE(cur->gmap) != gmap)
386 			continue;
387 		prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
388 		/* with mso/msl, the prefix lies at an offset */
389 		prefix += cur->scb_s.mso;
390 		if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
391 			prefix_unmapped_sync(cur);
392 	}
393 }
394 
395 /*
396  * Map the first prefix page and if tx is enabled also the second prefix page.
397  *
398  * The prefix will be protected, a gmap notifier will inform about unmaps.
399  * The shadow scb must not be executed until the prefix is remapped, this is
400  * guaranteed by properly handling PROG_REQUEST.
401  *
402  * Returns: - 0 on if successfully mapped or already mapped
403  *          - > 0 if control has to be given to guest 2
404  *          - -EAGAIN if the caller can retry immediately
405  *          - -ENOMEM if out of memory
406  */
407 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
408 {
409 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
410 	u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
411 	int rc;
412 
413 	if (prefix_is_mapped(vsie_page))
414 		return 0;
415 
416 	/* mark it as mapped so we can catch any concurrent unmappers */
417 	prefix_mapped(vsie_page);
418 
419 	/* with mso/msl, the prefix lies at offset *mso* */
420 	prefix += scb_s->mso;
421 
422 	rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
423 	if (!rc && (scb_s->ecb & ECB_TE))
424 		rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
425 					   prefix + PAGE_SIZE);
426 	/*
427 	 * We don't have to mprotect, we will be called for all unshadows.
428 	 * SIE will detect if protection applies and trigger a validity.
429 	 */
430 	if (rc)
431 		prefix_unmapped(vsie_page);
432 	if (rc > 0 || rc == -EFAULT)
433 		rc = set_validity_icpt(scb_s, 0x0037U);
434 	return rc;
435 }
436 
437 /*
438  * Pin the guest page given by gpa and set hpa to the pinned host address.
439  * Will always be pinned writable.
440  *
441  * Returns: - 0 on success
442  *          - -EINVAL if the gpa is not valid guest storage
443  *          - -ENOMEM if out of memory
444  */
445 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
446 {
447 	struct page *page;
448 	hva_t hva;
449 	int rc;
450 
451 	hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
452 	if (kvm_is_error_hva(hva))
453 		return -EINVAL;
454 	rc = get_user_pages_fast(hva, 1, 1, &page);
455 	if (rc < 0)
456 		return rc;
457 	else if (rc != 1)
458 		return -ENOMEM;
459 	*hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
460 	return 0;
461 }
462 
463 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
464 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
465 {
466 	struct page *page;
467 
468 	page = virt_to_page(hpa);
469 	set_page_dirty_lock(page);
470 	put_page(page);
471 	/* mark the page always as dirty for migration */
472 	mark_page_dirty(kvm, gpa_to_gfn(gpa));
473 }
474 
475 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
476 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
477 {
478 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
479 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
480 	hpa_t hpa;
481 	gpa_t gpa;
482 
483 	hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
484 	if (hpa) {
485 		gpa = scb_o->scaol & ~0xfUL;
486 		if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
487 			gpa |= (u64) scb_o->scaoh << 32;
488 		unpin_guest_page(vcpu->kvm, gpa, hpa);
489 		scb_s->scaol = 0;
490 		scb_s->scaoh = 0;
491 	}
492 
493 	hpa = scb_s->itdba;
494 	if (hpa) {
495 		gpa = scb_o->itdba & ~0xffUL;
496 		unpin_guest_page(vcpu->kvm, gpa, hpa);
497 		scb_s->itdba = 0;
498 	}
499 
500 	hpa = scb_s->gvrd;
501 	if (hpa) {
502 		gpa = scb_o->gvrd & ~0x1ffUL;
503 		unpin_guest_page(vcpu->kvm, gpa, hpa);
504 		scb_s->gvrd = 0;
505 	}
506 
507 	hpa = scb_s->riccbd;
508 	if (hpa) {
509 		gpa = scb_o->riccbd & ~0x3fUL;
510 		unpin_guest_page(vcpu->kvm, gpa, hpa);
511 		scb_s->riccbd = 0;
512 	}
513 
514 	hpa = scb_s->sdnxo;
515 	if (hpa) {
516 		gpa = scb_o->sdnxo;
517 		unpin_guest_page(vcpu->kvm, gpa, hpa);
518 		scb_s->sdnxo = 0;
519 	}
520 }
521 
522 /*
523  * Instead of shadowing some blocks, we can simply forward them because the
524  * addresses in the scb are 64 bit long.
525  *
526  * This works as long as the data lies in one page. If blocks ever exceed one
527  * page, we have to fall back to shadowing.
528  *
529  * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
530  * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
531  *
532  * Returns: - 0 if all blocks were pinned.
533  *          - > 0 if control has to be given to guest 2
534  *          - -ENOMEM if out of memory
535  */
536 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
537 {
538 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
539 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
540 	hpa_t hpa;
541 	gpa_t gpa;
542 	int rc = 0;
543 
544 	gpa = scb_o->scaol & ~0xfUL;
545 	if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
546 		gpa |= (u64) scb_o->scaoh << 32;
547 	if (gpa) {
548 		if (!(gpa & ~0x1fffUL))
549 			rc = set_validity_icpt(scb_s, 0x0038U);
550 		else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
551 			rc = set_validity_icpt(scb_s, 0x0011U);
552 		else if ((gpa & PAGE_MASK) !=
553 			 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
554 			rc = set_validity_icpt(scb_s, 0x003bU);
555 		if (!rc) {
556 			rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
557 			if (rc == -EINVAL)
558 				rc = set_validity_icpt(scb_s, 0x0034U);
559 		}
560 		if (rc)
561 			goto unpin;
562 		scb_s->scaoh = (u32)((u64)hpa >> 32);
563 		scb_s->scaol = (u32)(u64)hpa;
564 	}
565 
566 	gpa = scb_o->itdba & ~0xffUL;
567 	if (gpa && (scb_s->ecb & ECB_TE)) {
568 		if (!(gpa & ~0x1fffU)) {
569 			rc = set_validity_icpt(scb_s, 0x0080U);
570 			goto unpin;
571 		}
572 		/* 256 bytes cannot cross page boundaries */
573 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
574 		if (rc == -EINVAL)
575 			rc = set_validity_icpt(scb_s, 0x0080U);
576 		if (rc)
577 			goto unpin;
578 		scb_s->itdba = hpa;
579 	}
580 
581 	gpa = scb_o->gvrd & ~0x1ffUL;
582 	if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
583 		if (!(gpa & ~0x1fffUL)) {
584 			rc = set_validity_icpt(scb_s, 0x1310U);
585 			goto unpin;
586 		}
587 		/*
588 		 * 512 bytes vector registers cannot cross page boundaries
589 		 * if this block gets bigger, we have to shadow it.
590 		 */
591 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
592 		if (rc == -EINVAL)
593 			rc = set_validity_icpt(scb_s, 0x1310U);
594 		if (rc)
595 			goto unpin;
596 		scb_s->gvrd = hpa;
597 	}
598 
599 	gpa = scb_o->riccbd & ~0x3fUL;
600 	if (gpa && (scb_s->ecb3 & ECB3_RI)) {
601 		if (!(gpa & ~0x1fffUL)) {
602 			rc = set_validity_icpt(scb_s, 0x0043U);
603 			goto unpin;
604 		}
605 		/* 64 bytes cannot cross page boundaries */
606 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
607 		if (rc == -EINVAL)
608 			rc = set_validity_icpt(scb_s, 0x0043U);
609 		/* Validity 0x0044 will be checked by SIE */
610 		if (rc)
611 			goto unpin;
612 		scb_s->riccbd = hpa;
613 	}
614 	if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
615 		unsigned long sdnxc;
616 
617 		gpa = scb_o->sdnxo & ~0xfUL;
618 		sdnxc = scb_o->sdnxo & 0xfUL;
619 		if (!gpa || !(gpa & ~0x1fffUL)) {
620 			rc = set_validity_icpt(scb_s, 0x10b0U);
621 			goto unpin;
622 		}
623 		if (sdnxc < 6 || sdnxc > 12) {
624 			rc = set_validity_icpt(scb_s, 0x10b1U);
625 			goto unpin;
626 		}
627 		if (gpa & ((1 << sdnxc) - 1)) {
628 			rc = set_validity_icpt(scb_s, 0x10b2U);
629 			goto unpin;
630 		}
631 		/* Due to alignment rules (checked above) this cannot
632 		 * cross page boundaries
633 		 */
634 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
635 		if (rc == -EINVAL)
636 			rc = set_validity_icpt(scb_s, 0x10b0U);
637 		if (rc)
638 			goto unpin;
639 		scb_s->sdnxo = hpa | sdnxc;
640 	}
641 	return 0;
642 unpin:
643 	unpin_blocks(vcpu, vsie_page);
644 	return rc;
645 }
646 
647 /* unpin the scb provided by guest 2, marking it as dirty */
648 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
649 		      gpa_t gpa)
650 {
651 	hpa_t hpa = (hpa_t) vsie_page->scb_o;
652 
653 	if (hpa)
654 		unpin_guest_page(vcpu->kvm, gpa, hpa);
655 	vsie_page->scb_o = NULL;
656 }
657 
658 /*
659  * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
660  *
661  * Returns: - 0 if the scb was pinned.
662  *          - > 0 if control has to be given to guest 2
663  *          - -ENOMEM if out of memory
664  */
665 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
666 		   gpa_t gpa)
667 {
668 	hpa_t hpa;
669 	int rc;
670 
671 	rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
672 	if (rc == -EINVAL) {
673 		rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
674 		if (!rc)
675 			rc = 1;
676 	}
677 	if (!rc)
678 		vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
679 	return rc;
680 }
681 
682 /*
683  * Inject a fault into guest 2.
684  *
685  * Returns: - > 0 if control has to be given to guest 2
686  *            < 0 if an error occurred during injection.
687  */
688 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
689 			bool write_flag)
690 {
691 	struct kvm_s390_pgm_info pgm = {
692 		.code = code,
693 		.trans_exc_code =
694 			/* 0-51: virtual address */
695 			(vaddr & 0xfffffffffffff000UL) |
696 			/* 52-53: store / fetch */
697 			(((unsigned int) !write_flag) + 1) << 10,
698 			/* 62-63: asce id (alway primary == 0) */
699 		.exc_access_id = 0, /* always primary */
700 		.op_access_id = 0, /* not MVPG */
701 	};
702 	int rc;
703 
704 	if (code == PGM_PROTECTION)
705 		pgm.trans_exc_code |= 0x4UL;
706 
707 	rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
708 	return rc ? rc : 1;
709 }
710 
711 /*
712  * Handle a fault during vsie execution on a gmap shadow.
713  *
714  * Returns: - 0 if the fault was resolved
715  *          - > 0 if control has to be given to guest 2
716  *          - < 0 if an error occurred
717  */
718 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
719 {
720 	int rc;
721 
722 	if (current->thread.gmap_int_code == PGM_PROTECTION)
723 		/* we can directly forward all protection exceptions */
724 		return inject_fault(vcpu, PGM_PROTECTION,
725 				    current->thread.gmap_addr, 1);
726 
727 	rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
728 				   current->thread.gmap_addr);
729 	if (rc > 0) {
730 		rc = inject_fault(vcpu, rc,
731 				  current->thread.gmap_addr,
732 				  current->thread.gmap_write_flag);
733 		if (rc >= 0)
734 			vsie_page->fault_addr = current->thread.gmap_addr;
735 	}
736 	return rc;
737 }
738 
739 /*
740  * Retry the previous fault that required guest 2 intervention. This avoids
741  * one superfluous SIE re-entry and direct exit.
742  *
743  * Will ignore any errors. The next SIE fault will do proper fault handling.
744  */
745 static void handle_last_fault(struct kvm_vcpu *vcpu,
746 			      struct vsie_page *vsie_page)
747 {
748 	if (vsie_page->fault_addr)
749 		kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
750 				      vsie_page->fault_addr);
751 	vsie_page->fault_addr = 0;
752 }
753 
754 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
755 {
756 	vsie_page->scb_s.icptcode = 0;
757 }
758 
759 /* rewind the psw and clear the vsie icpt, so we can retry execution */
760 static void retry_vsie_icpt(struct vsie_page *vsie_page)
761 {
762 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
763 	int ilen = insn_length(scb_s->ipa >> 8);
764 
765 	/* take care of EXECUTE instructions */
766 	if (scb_s->icptstatus & 1) {
767 		ilen = (scb_s->icptstatus >> 4) & 0x6;
768 		if (!ilen)
769 			ilen = 4;
770 	}
771 	scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
772 	clear_vsie_icpt(vsie_page);
773 }
774 
775 /*
776  * Try to shadow + enable the guest 2 provided facility list.
777  * Retry instruction execution if enabled for and provided by guest 2.
778  *
779  * Returns: - 0 if handled (retry or guest 2 icpt)
780  *          - > 0 if control has to be given to guest 2
781  */
782 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
783 {
784 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
785 	__u32 fac = vsie_page->scb_o->fac & 0x7ffffff8U;
786 
787 	if (fac && test_kvm_facility(vcpu->kvm, 7)) {
788 		retry_vsie_icpt(vsie_page);
789 		if (read_guest_real(vcpu, fac, &vsie_page->fac,
790 				    sizeof(vsie_page->fac)))
791 			return set_validity_icpt(scb_s, 0x1090U);
792 		scb_s->fac = (__u32)(__u64) &vsie_page->fac;
793 	}
794 	return 0;
795 }
796 
797 /*
798  * Run the vsie on a shadow scb and a shadow gmap, without any further
799  * sanity checks, handling SIE faults.
800  *
801  * Returns: - 0 everything went fine
802  *          - > 0 if control has to be given to guest 2
803  *          - < 0 if an error occurred
804  */
805 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
806 {
807 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
808 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
809 	struct mcck_volatile_info *mcck_info;
810 	struct sie_page *sie_page;
811 	int rc;
812 
813 	handle_last_fault(vcpu, vsie_page);
814 
815 	if (need_resched())
816 		schedule();
817 	if (test_cpu_flag(CIF_MCCK_PENDING))
818 		s390_handle_mcck();
819 
820 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
821 	local_irq_disable();
822 	guest_enter_irqoff();
823 	local_irq_enable();
824 
825 	rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
826 
827 	local_irq_disable();
828 	guest_exit_irqoff();
829 	local_irq_enable();
830 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
831 
832 	if (rc == -EINTR) {
833 		VCPU_EVENT(vcpu, 3, "%s", "machine check");
834 		sie_page = container_of(scb_s, struct sie_page, sie_block);
835 		mcck_info = &sie_page->mcck_info;
836 		kvm_s390_reinject_machine_check(vcpu, mcck_info);
837 		return 0;
838 	}
839 
840 	if (rc > 0)
841 		rc = 0; /* we could still have an icpt */
842 	else if (rc == -EFAULT)
843 		return handle_fault(vcpu, vsie_page);
844 
845 	switch (scb_s->icptcode) {
846 	case ICPT_INST:
847 		if (scb_s->ipa == 0xb2b0)
848 			rc = handle_stfle(vcpu, vsie_page);
849 		break;
850 	case ICPT_STOP:
851 		/* stop not requested by g2 - must have been a kick */
852 		if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
853 			clear_vsie_icpt(vsie_page);
854 		break;
855 	case ICPT_VALIDITY:
856 		if ((scb_s->ipa & 0xf000) != 0xf000)
857 			scb_s->ipa += 0x1000;
858 		break;
859 	}
860 	return rc;
861 }
862 
863 static void release_gmap_shadow(struct vsie_page *vsie_page)
864 {
865 	if (vsie_page->gmap)
866 		gmap_put(vsie_page->gmap);
867 	WRITE_ONCE(vsie_page->gmap, NULL);
868 	prefix_unmapped(vsie_page);
869 }
870 
871 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
872 			       struct vsie_page *vsie_page)
873 {
874 	unsigned long asce;
875 	union ctlreg0 cr0;
876 	struct gmap *gmap;
877 	int edat;
878 
879 	asce = vcpu->arch.sie_block->gcr[1];
880 	cr0.val = vcpu->arch.sie_block->gcr[0];
881 	edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
882 	edat += edat && test_kvm_facility(vcpu->kvm, 78);
883 
884 	/*
885 	 * ASCE or EDAT could have changed since last icpt, or the gmap
886 	 * we're holding has been unshadowed. If the gmap is still valid,
887 	 * we can safely reuse it.
888 	 */
889 	if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
890 		return 0;
891 
892 	/* release the old shadow - if any, and mark the prefix as unmapped */
893 	release_gmap_shadow(vsie_page);
894 	gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
895 	if (IS_ERR(gmap))
896 		return PTR_ERR(gmap);
897 	gmap->private = vcpu->kvm;
898 	WRITE_ONCE(vsie_page->gmap, gmap);
899 	return 0;
900 }
901 
902 /*
903  * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
904  */
905 static void register_shadow_scb(struct kvm_vcpu *vcpu,
906 				struct vsie_page *vsie_page)
907 {
908 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
909 
910 	WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
911 	/*
912 	 * External calls have to lead to a kick of the vcpu and
913 	 * therefore the vsie -> Simulate Wait state.
914 	 */
915 	atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
916 	/*
917 	 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
918 	 * automatically be adjusted on tod clock changes via kvm_sync_clock.
919 	 */
920 	preempt_disable();
921 	scb_s->epoch += vcpu->kvm->arch.epoch;
922 	preempt_enable();
923 }
924 
925 /*
926  * Unregister a shadow scb from a VCPU.
927  */
928 static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
929 {
930 	atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
931 	WRITE_ONCE(vcpu->arch.vsie_block, NULL);
932 }
933 
934 /*
935  * Run the vsie on a shadowed scb, managing the gmap shadow, handling
936  * prefix pages and faults.
937  *
938  * Returns: - 0 if no errors occurred
939  *          - > 0 if control has to be given to guest 2
940  *          - -ENOMEM if out of memory
941  */
942 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
943 {
944 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
945 	int rc = 0;
946 
947 	while (1) {
948 		rc = acquire_gmap_shadow(vcpu, vsie_page);
949 		if (!rc)
950 			rc = map_prefix(vcpu, vsie_page);
951 		if (!rc) {
952 			gmap_enable(vsie_page->gmap);
953 			update_intervention_requests(vsie_page);
954 			rc = do_vsie_run(vcpu, vsie_page);
955 			gmap_enable(vcpu->arch.gmap);
956 		}
957 		atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
958 
959 		if (rc == -EAGAIN)
960 			rc = 0;
961 		if (rc || scb_s->icptcode || signal_pending(current) ||
962 		    kvm_s390_vcpu_has_irq(vcpu, 0))
963 			break;
964 	}
965 
966 	if (rc == -EFAULT) {
967 		/*
968 		 * Addressing exceptions are always presentes as intercepts.
969 		 * As addressing exceptions are suppressing and our guest 3 PSW
970 		 * points at the responsible instruction, we have to
971 		 * forward the PSW and set the ilc. If we can't read guest 3
972 		 * instruction, we can use an arbitrary ilc. Let's always use
973 		 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
974 		 * memory. (we could also fake the shadow so the hardware
975 		 * handles it).
976 		 */
977 		scb_s->icptcode = ICPT_PROGI;
978 		scb_s->iprcc = PGM_ADDRESSING;
979 		scb_s->pgmilc = 4;
980 		scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
981 	}
982 	return rc;
983 }
984 
985 /*
986  * Get or create a vsie page for a scb address.
987  *
988  * Returns: - address of a vsie page (cached or new one)
989  *          - NULL if the same scb address is already used by another VCPU
990  *          - ERR_PTR(-ENOMEM) if out of memory
991  */
992 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
993 {
994 	struct vsie_page *vsie_page;
995 	struct page *page;
996 	int nr_vcpus;
997 
998 	rcu_read_lock();
999 	page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1000 	rcu_read_unlock();
1001 	if (page) {
1002 		if (page_ref_inc_return(page) == 2)
1003 			return page_to_virt(page);
1004 		page_ref_dec(page);
1005 	}
1006 
1007 	/*
1008 	 * We want at least #online_vcpus shadows, so every VCPU can execute
1009 	 * the VSIE in parallel.
1010 	 */
1011 	nr_vcpus = atomic_read(&kvm->online_vcpus);
1012 
1013 	mutex_lock(&kvm->arch.vsie.mutex);
1014 	if (kvm->arch.vsie.page_count < nr_vcpus) {
1015 		page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
1016 		if (!page) {
1017 			mutex_unlock(&kvm->arch.vsie.mutex);
1018 			return ERR_PTR(-ENOMEM);
1019 		}
1020 		page_ref_inc(page);
1021 		kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1022 		kvm->arch.vsie.page_count++;
1023 	} else {
1024 		/* reuse an existing entry that belongs to nobody */
1025 		while (true) {
1026 			page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1027 			if (page_ref_inc_return(page) == 2)
1028 				break;
1029 			page_ref_dec(page);
1030 			kvm->arch.vsie.next++;
1031 			kvm->arch.vsie.next %= nr_vcpus;
1032 		}
1033 		radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1034 	}
1035 	page->index = addr;
1036 	/* double use of the same address */
1037 	if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1038 		page_ref_dec(page);
1039 		mutex_unlock(&kvm->arch.vsie.mutex);
1040 		return NULL;
1041 	}
1042 	mutex_unlock(&kvm->arch.vsie.mutex);
1043 
1044 	vsie_page = page_to_virt(page);
1045 	memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
1046 	release_gmap_shadow(vsie_page);
1047 	vsie_page->fault_addr = 0;
1048 	vsie_page->scb_s.ihcpu = 0xffffU;
1049 	return vsie_page;
1050 }
1051 
1052 /* put a vsie page acquired via get_vsie_page */
1053 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1054 {
1055 	struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1056 
1057 	page_ref_dec(page);
1058 }
1059 
1060 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1061 {
1062 	struct vsie_page *vsie_page;
1063 	unsigned long scb_addr;
1064 	int rc;
1065 
1066 	vcpu->stat.instruction_sie++;
1067 	if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1068 		return -EOPNOTSUPP;
1069 	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1070 		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1071 
1072 	BUILD_BUG_ON(sizeof(struct vsie_page) != 4096);
1073 	scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1074 
1075 	/* 512 byte alignment */
1076 	if (unlikely(scb_addr & 0x1ffUL))
1077 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1078 
1079 	if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1080 		return 0;
1081 
1082 	vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1083 	if (IS_ERR(vsie_page))
1084 		return PTR_ERR(vsie_page);
1085 	else if (!vsie_page)
1086 		/* double use of sie control block - simply do nothing */
1087 		return 0;
1088 
1089 	rc = pin_scb(vcpu, vsie_page, scb_addr);
1090 	if (rc)
1091 		goto out_put;
1092 	rc = shadow_scb(vcpu, vsie_page);
1093 	if (rc)
1094 		goto out_unpin_scb;
1095 	rc = pin_blocks(vcpu, vsie_page);
1096 	if (rc)
1097 		goto out_unshadow;
1098 	register_shadow_scb(vcpu, vsie_page);
1099 	rc = vsie_run(vcpu, vsie_page);
1100 	unregister_shadow_scb(vcpu);
1101 	unpin_blocks(vcpu, vsie_page);
1102 out_unshadow:
1103 	unshadow_scb(vcpu, vsie_page);
1104 out_unpin_scb:
1105 	unpin_scb(vcpu, vsie_page, scb_addr);
1106 out_put:
1107 	put_vsie_page(vcpu->kvm, vsie_page);
1108 
1109 	return rc < 0 ? rc : 0;
1110 }
1111 
1112 /* Init the vsie data structures. To be called when a vm is initialized. */
1113 void kvm_s390_vsie_init(struct kvm *kvm)
1114 {
1115 	mutex_init(&kvm->arch.vsie.mutex);
1116 	INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1117 }
1118 
1119 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
1120 void kvm_s390_vsie_destroy(struct kvm *kvm)
1121 {
1122 	struct vsie_page *vsie_page;
1123 	struct page *page;
1124 	int i;
1125 
1126 	mutex_lock(&kvm->arch.vsie.mutex);
1127 	for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1128 		page = kvm->arch.vsie.pages[i];
1129 		kvm->arch.vsie.pages[i] = NULL;
1130 		vsie_page = page_to_virt(page);
1131 		release_gmap_shadow(vsie_page);
1132 		/* free the radix tree entry */
1133 		radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1134 		__free_page(page);
1135 	}
1136 	kvm->arch.vsie.page_count = 0;
1137 	mutex_unlock(&kvm->arch.vsie.mutex);
1138 }
1139 
1140 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1141 {
1142 	struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1143 
1144 	/*
1145 	 * Even if the VCPU lets go of the shadow sie block reference, it is
1146 	 * still valid in the cache. So we can safely kick it.
1147 	 */
1148 	if (scb) {
1149 		atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1150 		if (scb->prog0c & PROG_IN_SIE)
1151 			atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);
1152 	}
1153 }
1154