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