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