xref: /linux/arch/s390/kvm/vsie.c (revision 140eb5227767c6754742020a16d2691222b9c19b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * kvm nested virtualization support for s390x
4  *
5  * Copyright IBM Corp. 2016
6  *
7  *    Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
8  */
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>
15 
16 #include <asm/gmap.h>
17 #include <asm/mmu_context.h>
18 #include <asm/sclp.h>
19 #include <asm/nmi.h>
20 #include <asm/dis.h>
21 #include "kvm-s390.h"
22 #include "gaccess.h"
23 
24 struct vsie_page {
25 	struct kvm_s390_sie_block scb_s;	/* 0x0000 */
26 	/*
27 	 * the backup info for machine check. ensure it's at
28 	 * the same offset as that in struct sie_page!
29 	 */
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 */
40 };
41 
42 /* trigger a validity icpt for the given scb */
43 static int set_validity_icpt(struct kvm_s390_sie_block *scb,
44 			     __u16 reason_code)
45 {
46 	scb->ipa = 0x1000;
47 	scb->ipb = ((__u32) reason_code) << 16;
48 	scb->icptcode = ICPT_VALIDITY;
49 	return 1;
50 }
51 
52 /* mark the prefix as unmapped, this will block the VSIE */
53 static void prefix_unmapped(struct vsie_page *vsie_page)
54 {
55 	atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
56 }
57 
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)
60 {
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)
65 		cpu_relax();
66 }
67 
68 /* mark the prefix as mapped, this will allow the VSIE to run */
69 static void prefix_mapped(struct vsie_page *vsie_page)
70 {
71 	atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
72 }
73 
74 /* test if the prefix is mapped into the gmap shadow */
75 static int prefix_is_mapped(struct vsie_page *vsie_page)
76 {
77 	return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
78 }
79 
80 /* copy the updated intervention request bits into the shadow scb */
81 static void update_intervention_requests(struct vsie_page *vsie_page)
82 {
83 	const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
84 	int cpuflags;
85 
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);
89 }
90 
91 /* shadow (filter and validate) the cpuflags  */
92 static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
93 {
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);
97 
98 	/* we don't allow ESA/390 guests */
99 	if (!(cpuflags & CPUSTAT_ZARCH))
100 		return set_validity_icpt(scb_s, 0x0001U);
101 
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);
106 
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;
115 	}
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;
124 
125 	atomic_set(&scb_s->cpuflags, newflags);
126 	return 0;
127 }
128 
129 /*
130  * Create a shadow copy of the crycb block and setup key wrapping, if
131  * requested for guest 3 and enabled for guest 2.
132  *
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.
135  *
136  * Returns: - 0 if shadowed or nothing to do
137  *          - > 0 if control has to be given to guest 2
138  */
139 static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
140 {
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;
145 	u8 ecb3_flags;
146 
147 	scb_s->crycbd = 0;
148 	if (!(scb_o->crycbd & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
149 		return 0;
150 	/* format-1 is supported with message-security-assist extension 3 */
151 	if (!test_kvm_facility(vcpu->kvm, 76))
152 		return 0;
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);
156 	if (!ecb3_flags)
157 		return 0;
158 
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);
163 
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);
167 
168 	scb_s->ecb3 |= ecb3_flags;
169 	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
170 			CRYCB_FORMAT2;
171 
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);
178 	return 0;
179 }
180 
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)
183 {
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;
187 
188 	scb_s->ibc = 0;
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;
198 	}
199 }
200 
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)
203 {
204 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
205 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
206 
207 	/* interception */
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;
213 
214 	/* timer */
215 	scb_o->cputm = scb_s->cputm;
216 	scb_o->ckc = scb_s->ckc;
217 	scb_o->todpr = scb_s->todpr;
218 
219 	/* guest state */
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;
225 
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;
230 	}
231 
232 	/* interrupt intercept */
233 	switch (scb_s->icptcode) {
234 	case ICPT_PROGI:
235 	case ICPT_INSTPROGI:
236 	case ICPT_EXTINT:
237 		memcpy((void *)((u64)scb_o + 0xc0),
238 		       (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
239 		break;
240 	case ICPT_PARTEXEC:
241 		/* MVPG only */
242 		memcpy((void *)((u64)scb_o + 0xc0),
243 		       (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
244 		break;
245 	}
246 
247 	if (scb_s->ihcpu != 0xffffU)
248 		scb_o->ihcpu = scb_s->ihcpu;
249 }
250 
251 /*
252  * Setup the shadow scb by copying and checking the relevant parts of the g2
253  * provided scb.
254  *
255  * Returns: - 0 if the scb has been shadowed
256  *          - > 0 if control has to be given to guest 2
257  */
258 static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
259 {
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;
264 	int rc;
265 
266 	/* make sure we don't have any leftovers when reusing the scb */
267 	scb_s->icptcode = 0;
268 	scb_s->eca = 0;
269 	scb_s->ecb = 0;
270 	scb_s->ecb2 = 0;
271 	scb_s->ecb3 = 0;
272 	scb_s->ecd = 0;
273 	scb_s->fac = 0;
274 	scb_s->fpf = 0;
275 
276 	rc = prepare_cpuflags(vcpu, vsie_page);
277 	if (rc)
278 		goto out;
279 
280 	/* timer */
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;
285 
286 	/* guest state */
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;
292 
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;
298 	/*
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.
302 	 */
303 	if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
304 		scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
305 
306 	scb_s->icpua = scb_o->icpua;
307 
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;
317 
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;
321 
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;
333 	}
334 	/* branch prediction */
335 	if (test_kvm_facility(vcpu->kvm, 82))
336 		scb_s->fpf |= scb_o->fpf & FPF_BPBC;
337 	/* SIMD */
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;
341 	}
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;
352 	}
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;
362 
363 	prepare_ibc(vcpu, vsie_page);
364 	rc = shadow_crycb(vcpu, vsie_page);
365 out:
366 	if (rc)
367 		unshadow_scb(vcpu, vsie_page);
368 	return rc;
369 }
370 
371 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
372 				 unsigned long end)
373 {
374 	struct kvm *kvm = gmap->private;
375 	struct vsie_page *cur;
376 	unsigned long prefix;
377 	struct page *page;
378 	int i;
379 
380 	if (!gmap_is_shadow(gmap))
381 		return;
382 	if (start >= 1UL << 31)
383 		/* We are only interested in prefix pages */
384 		return;
385 
386 	/*
387 	 * Only new shadow blocks are added to the list during runtime,
388 	 * therefore we can safely reference them all the time.
389 	 */
390 	for (i = 0; i < kvm->arch.vsie.page_count; i++) {
391 		page = READ_ONCE(kvm->arch.vsie.pages[i]);
392 		if (!page)
393 			continue;
394 		cur = page_to_virt(page);
395 		if (READ_ONCE(cur->gmap) != gmap)
396 			continue;
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);
402 	}
403 }
404 
405 /*
406  * Map the first prefix page and if tx is enabled also the second prefix page.
407  *
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.
411  *
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
416  */
417 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
418 {
419 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
420 	u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
421 	int rc;
422 
423 	if (prefix_is_mapped(vsie_page))
424 		return 0;
425 
426 	/* mark it as mapped so we can catch any concurrent unmappers */
427 	prefix_mapped(vsie_page);
428 
429 	/* with mso/msl, the prefix lies at offset *mso* */
430 	prefix += scb_s->mso;
431 
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,
435 					   prefix + PAGE_SIZE);
436 	/*
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.
439 	 */
440 	if (rc)
441 		prefix_unmapped(vsie_page);
442 	if (rc > 0 || rc == -EFAULT)
443 		rc = set_validity_icpt(scb_s, 0x0037U);
444 	return rc;
445 }
446 
447 /*
448  * Pin the guest page given by gpa and set hpa to the pinned host address.
449  * Will always be pinned writable.
450  *
451  * Returns: - 0 on success
452  *          - -EINVAL if the gpa is not valid guest storage
453  */
454 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
455 {
456 	struct page *page;
457 
458 	page = gfn_to_page(kvm, gpa_to_gfn(gpa));
459 	if (is_error_page(page))
460 		return -EINVAL;
461 	*hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
462 	return 0;
463 }
464 
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)
467 {
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));
471 }
472 
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)
475 {
476 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
477 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
478 	hpa_t hpa;
479 	gpa_t gpa;
480 
481 	hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
482 	if (hpa) {
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);
487 		scb_s->scaol = 0;
488 		scb_s->scaoh = 0;
489 	}
490 
491 	hpa = scb_s->itdba;
492 	if (hpa) {
493 		gpa = scb_o->itdba & ~0xffUL;
494 		unpin_guest_page(vcpu->kvm, gpa, hpa);
495 		scb_s->itdba = 0;
496 	}
497 
498 	hpa = scb_s->gvrd;
499 	if (hpa) {
500 		gpa = scb_o->gvrd & ~0x1ffUL;
501 		unpin_guest_page(vcpu->kvm, gpa, hpa);
502 		scb_s->gvrd = 0;
503 	}
504 
505 	hpa = scb_s->riccbd;
506 	if (hpa) {
507 		gpa = scb_o->riccbd & ~0x3fUL;
508 		unpin_guest_page(vcpu->kvm, gpa, hpa);
509 		scb_s->riccbd = 0;
510 	}
511 
512 	hpa = scb_s->sdnxo;
513 	if (hpa) {
514 		gpa = scb_o->sdnxo;
515 		unpin_guest_page(vcpu->kvm, gpa, hpa);
516 		scb_s->sdnxo = 0;
517 	}
518 }
519 
520 /*
521  * Instead of shadowing some blocks, we can simply forward them because the
522  * addresses in the scb are 64 bit long.
523  *
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.
526  *
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).
529  *
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
533  */
534 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
535 {
536 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
537 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
538 	hpa_t hpa;
539 	gpa_t gpa;
540 	int rc = 0;
541 
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;
545 	if (gpa) {
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);
553 		if (!rc) {
554 			rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
555 			if (rc)
556 				rc = set_validity_icpt(scb_s, 0x0034U);
557 		}
558 		if (rc)
559 			goto unpin;
560 		scb_s->scaoh = (u32)((u64)hpa >> 32);
561 		scb_s->scaol = (u32)(u64)hpa;
562 	}
563 
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);
568 			goto unpin;
569 		}
570 		/* 256 bytes cannot cross page boundaries */
571 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
572 		if (rc) {
573 			rc = set_validity_icpt(scb_s, 0x0080U);
574 			goto unpin;
575 		}
576 		scb_s->itdba = hpa;
577 	}
578 
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);
583 			goto unpin;
584 		}
585 		/*
586 		 * 512 bytes vector registers cannot cross page boundaries
587 		 * if this block gets bigger, we have to shadow it.
588 		 */
589 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
590 		if (rc) {
591 			rc = set_validity_icpt(scb_s, 0x1310U);
592 			goto unpin;
593 		}
594 		scb_s->gvrd = hpa;
595 	}
596 
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);
601 			goto unpin;
602 		}
603 		/* 64 bytes cannot cross page boundaries */
604 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
605 		if (rc) {
606 			rc = set_validity_icpt(scb_s, 0x0043U);
607 			goto unpin;
608 		}
609 		/* Validity 0x0044 will be checked by SIE */
610 		scb_s->riccbd = hpa;
611 	}
612 	if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
613 		unsigned long sdnxc;
614 
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);
619 			goto unpin;
620 		}
621 		if (sdnxc < 6 || sdnxc > 12) {
622 			rc = set_validity_icpt(scb_s, 0x10b1U);
623 			goto unpin;
624 		}
625 		if (gpa & ((1 << sdnxc) - 1)) {
626 			rc = set_validity_icpt(scb_s, 0x10b2U);
627 			goto unpin;
628 		}
629 		/* Due to alignment rules (checked above) this cannot
630 		 * cross page boundaries
631 		 */
632 		rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
633 		if (rc) {
634 			rc = set_validity_icpt(scb_s, 0x10b0U);
635 			goto unpin;
636 		}
637 		scb_s->sdnxo = hpa | sdnxc;
638 	}
639 	return 0;
640 unpin:
641 	unpin_blocks(vcpu, vsie_page);
642 	return rc;
643 }
644 
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,
647 		      gpa_t gpa)
648 {
649 	hpa_t hpa = (hpa_t) vsie_page->scb_o;
650 
651 	if (hpa)
652 		unpin_guest_page(vcpu->kvm, gpa, hpa);
653 	vsie_page->scb_o = NULL;
654 }
655 
656 /*
657  * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
658  *
659  * Returns: - 0 if the scb was pinned.
660  *          - > 0 if control has to be given to guest 2
661  */
662 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
663 		   gpa_t gpa)
664 {
665 	hpa_t hpa;
666 	int rc;
667 
668 	rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
669 	if (rc) {
670 		rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
671 		WARN_ON_ONCE(rc);
672 		return 1;
673 	}
674 	vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
675 	return 0;
676 }
677 
678 /*
679  * Inject a fault into guest 2.
680  *
681  * Returns: - > 0 if control has to be given to guest 2
682  *            < 0 if an error occurred during injection.
683  */
684 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
685 			bool write_flag)
686 {
687 	struct kvm_s390_pgm_info pgm = {
688 		.code = code,
689 		.trans_exc_code =
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 */
697 	};
698 	int rc;
699 
700 	if (code == PGM_PROTECTION)
701 		pgm.trans_exc_code |= 0x4UL;
702 
703 	rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
704 	return rc ? rc : 1;
705 }
706 
707 /*
708  * Handle a fault during vsie execution on a gmap shadow.
709  *
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
713  */
714 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
715 {
716 	int rc;
717 
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);
722 
723 	rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
724 				   current->thread.gmap_addr);
725 	if (rc > 0) {
726 		rc = inject_fault(vcpu, rc,
727 				  current->thread.gmap_addr,
728 				  current->thread.gmap_write_flag);
729 		if (rc >= 0)
730 			vsie_page->fault_addr = current->thread.gmap_addr;
731 	}
732 	return rc;
733 }
734 
735 /*
736  * Retry the previous fault that required guest 2 intervention. This avoids
737  * one superfluous SIE re-entry and direct exit.
738  *
739  * Will ignore any errors. The next SIE fault will do proper fault handling.
740  */
741 static void handle_last_fault(struct kvm_vcpu *vcpu,
742 			      struct vsie_page *vsie_page)
743 {
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;
748 }
749 
750 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
751 {
752 	vsie_page->scb_s.icptcode = 0;
753 }
754 
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)
757 {
758 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
759 	int ilen = insn_length(scb_s->ipa >> 8);
760 
761 	/* take care of EXECUTE instructions */
762 	if (scb_s->icptstatus & 1) {
763 		ilen = (scb_s->icptstatus >> 4) & 0x6;
764 		if (!ilen)
765 			ilen = 4;
766 	}
767 	scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
768 	clear_vsie_icpt(vsie_page);
769 }
770 
771 /*
772  * Try to shadow + enable the guest 2 provided facility list.
773  * Retry instruction execution if enabled for and provided by guest 2.
774  *
775  * Returns: - 0 if handled (retry or guest 2 icpt)
776  *          - > 0 if control has to be given to guest 2
777  */
778 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
779 {
780 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
781 	__u32 fac = vsie_page->scb_o->fac & 0x7ffffff8U;
782 
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;
789 	}
790 	return 0;
791 }
792 
793 /*
794  * Run the vsie on a shadow scb and a shadow gmap, without any further
795  * sanity checks, handling SIE faults.
796  *
797  * Returns: - 0 everything went fine
798  *          - > 0 if control has to be given to guest 2
799  *          - < 0 if an error occurred
800  */
801 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
802 {
803 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
804 	struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
805 	int rc;
806 
807 	handle_last_fault(vcpu, vsie_page);
808 
809 	if (need_resched())
810 		schedule();
811 	if (test_cpu_flag(CIF_MCCK_PENDING))
812 		s390_handle_mcck();
813 
814 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
815 	local_irq_disable();
816 	guest_enter_irqoff();
817 	local_irq_enable();
818 
819 	rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
820 
821 	local_irq_disable();
822 	guest_exit_irqoff();
823 	local_irq_enable();
824 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
825 
826 	if (rc == -EINTR) {
827 		VCPU_EVENT(vcpu, 3, "%s", "machine check");
828 		kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
829 		return 0;
830 	}
831 
832 	if (rc > 0)
833 		rc = 0; /* we could still have an icpt */
834 	else if (rc == -EFAULT)
835 		return handle_fault(vcpu, vsie_page);
836 
837 	switch (scb_s->icptcode) {
838 	case ICPT_INST:
839 		if (scb_s->ipa == 0xb2b0)
840 			rc = handle_stfle(vcpu, vsie_page);
841 		break;
842 	case ICPT_STOP:
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);
846 		break;
847 	case ICPT_VALIDITY:
848 		if ((scb_s->ipa & 0xf000) != 0xf000)
849 			scb_s->ipa += 0x1000;
850 		break;
851 	}
852 	return rc;
853 }
854 
855 static void release_gmap_shadow(struct vsie_page *vsie_page)
856 {
857 	if (vsie_page->gmap)
858 		gmap_put(vsie_page->gmap);
859 	WRITE_ONCE(vsie_page->gmap, NULL);
860 	prefix_unmapped(vsie_page);
861 }
862 
863 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
864 			       struct vsie_page *vsie_page)
865 {
866 	unsigned long asce;
867 	union ctlreg0 cr0;
868 	struct gmap *gmap;
869 	int edat;
870 
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);
875 
876 	/*
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.
880 	 */
881 	if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
882 		return 0;
883 
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);
887 	if (IS_ERR(gmap))
888 		return PTR_ERR(gmap);
889 	gmap->private = vcpu->kvm;
890 	WRITE_ONCE(vsie_page->gmap, gmap);
891 	return 0;
892 }
893 
894 /*
895  * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
896  */
897 static void register_shadow_scb(struct kvm_vcpu *vcpu,
898 				struct vsie_page *vsie_page)
899 {
900 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
901 
902 	WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
903 	/*
904 	 * External calls have to lead to a kick of the vcpu and
905 	 * therefore the vsie -> Simulate Wait state.
906 	 */
907 	atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
908 	/*
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.
911 	 */
912 	preempt_disable();
913 	scb_s->epoch += vcpu->kvm->arch.epoch;
914 
915 	if (scb_s->ecd & ECD_MEF) {
916 		scb_s->epdx += vcpu->kvm->arch.epdx;
917 		if (scb_s->epoch < vcpu->kvm->arch.epoch)
918 			scb_s->epdx += 1;
919 	}
920 
921 	preempt_enable();
922 }
923 
924 /*
925  * Unregister a shadow scb from a VCPU.
926  */
927 static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
928 {
929 	atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
930 	WRITE_ONCE(vcpu->arch.vsie_block, NULL);
931 }
932 
933 /*
934  * Run the vsie on a shadowed scb, managing the gmap shadow, handling
935  * prefix pages and faults.
936  *
937  * Returns: - 0 if no errors occurred
938  *          - > 0 if control has to be given to guest 2
939  *          - -ENOMEM if out of memory
940  */
941 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
942 {
943 	struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
944 	int rc = 0;
945 
946 	while (1) {
947 		rc = acquire_gmap_shadow(vcpu, vsie_page);
948 		if (!rc)
949 			rc = map_prefix(vcpu, vsie_page);
950 		if (!rc) {
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);
955 		}
956 		atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
957 
958 		if (rc == -EAGAIN)
959 			rc = 0;
960 		if (rc || scb_s->icptcode || signal_pending(current) ||
961 		    kvm_s390_vcpu_has_irq(vcpu, 0))
962 			break;
963 	}
964 
965 	if (rc == -EFAULT) {
966 		/*
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
974 		 * handles it).
975 		 */
976 		scb_s->icptcode = ICPT_PROGI;
977 		scb_s->iprcc = PGM_ADDRESSING;
978 		scb_s->pgmilc = 4;
979 		scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
980 	}
981 	return rc;
982 }
983 
984 /*
985  * Get or create a vsie page for a scb address.
986  *
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
990  */
991 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
992 {
993 	struct vsie_page *vsie_page;
994 	struct page *page;
995 	int nr_vcpus;
996 
997 	rcu_read_lock();
998 	page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
999 	rcu_read_unlock();
1000 	if (page) {
1001 		if (page_ref_inc_return(page) == 2)
1002 			return page_to_virt(page);
1003 		page_ref_dec(page);
1004 	}
1005 
1006 	/*
1007 	 * We want at least #online_vcpus shadows, so every VCPU can execute
1008 	 * the VSIE in parallel.
1009 	 */
1010 	nr_vcpus = atomic_read(&kvm->online_vcpus);
1011 
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);
1015 		if (!page) {
1016 			mutex_unlock(&kvm->arch.vsie.mutex);
1017 			return ERR_PTR(-ENOMEM);
1018 		}
1019 		page_ref_inc(page);
1020 		kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1021 		kvm->arch.vsie.page_count++;
1022 	} else {
1023 		/* reuse an existing entry that belongs to nobody */
1024 		while (true) {
1025 			page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1026 			if (page_ref_inc_return(page) == 2)
1027 				break;
1028 			page_ref_dec(page);
1029 			kvm->arch.vsie.next++;
1030 			kvm->arch.vsie.next %= nr_vcpus;
1031 		}
1032 		radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1033 	}
1034 	page->index = addr;
1035 	/* double use of the same address */
1036 	if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1037 		page_ref_dec(page);
1038 		mutex_unlock(&kvm->arch.vsie.mutex);
1039 		return NULL;
1040 	}
1041 	mutex_unlock(&kvm->arch.vsie.mutex);
1042 
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;
1048 	return vsie_page;
1049 }
1050 
1051 /* put a vsie page acquired via get_vsie_page */
1052 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1053 {
1054 	struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1055 
1056 	page_ref_dec(page);
1057 }
1058 
1059 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1060 {
1061 	struct vsie_page *vsie_page;
1062 	unsigned long scb_addr;
1063 	int rc;
1064 
1065 	vcpu->stat.instruction_sie++;
1066 	if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1067 		return -EOPNOTSUPP;
1068 	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1069 		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1070 
1071 	BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
1072 	scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1073 
1074 	/* 512 byte alignment */
1075 	if (unlikely(scb_addr & 0x1ffUL))
1076 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1077 
1078 	if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1079 		return 0;
1080 
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 */
1086 		return 0;
1087 
1088 	rc = pin_scb(vcpu, vsie_page, scb_addr);
1089 	if (rc)
1090 		goto out_put;
1091 	rc = shadow_scb(vcpu, vsie_page);
1092 	if (rc)
1093 		goto out_unpin_scb;
1094 	rc = pin_blocks(vcpu, vsie_page);
1095 	if (rc)
1096 		goto out_unshadow;
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);
1101 out_unshadow:
1102 	unshadow_scb(vcpu, vsie_page);
1103 out_unpin_scb:
1104 	unpin_scb(vcpu, vsie_page, scb_addr);
1105 out_put:
1106 	put_vsie_page(vcpu->kvm, vsie_page);
1107 
1108 	return rc < 0 ? rc : 0;
1109 }
1110 
1111 /* Init the vsie data structures. To be called when a vm is initialized. */
1112 void kvm_s390_vsie_init(struct kvm *kvm)
1113 {
1114 	mutex_init(&kvm->arch.vsie.mutex);
1115 	INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1116 }
1117 
1118 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
1119 void kvm_s390_vsie_destroy(struct kvm *kvm)
1120 {
1121 	struct vsie_page *vsie_page;
1122 	struct page *page;
1123 	int i;
1124 
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);
1133 		__free_page(page);
1134 	}
1135 	kvm->arch.vsie.page_count = 0;
1136 	mutex_unlock(&kvm->arch.vsie.mutex);
1137 }
1138 
1139 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1140 {
1141 	struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1142 
1143 	/*
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.
1146 	 */
1147 	if (scb) {
1148 		atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1149 		if (scb->prog0c & PROG_IN_SIE)
1150 			atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);
1151 	}
1152 }
1153