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