This source file includes following definitions.
- set_validity_icpt
- prefix_unmapped
- prefix_unmapped_sync
- prefix_mapped
- prefix_is_mapped
- update_intervention_requests
- prepare_cpuflags
- setup_apcb10
- setup_apcb00
- setup_apcb11
- setup_apcb
- shadow_crycb
- prepare_ibc
- unshadow_scb
- shadow_scb
- kvm_s390_vsie_gmap_notifier
- map_prefix
- pin_guest_page
- unpin_guest_page
- unpin_blocks
- pin_blocks
- unpin_scb
- pin_scb
- inject_fault
- handle_fault
- handle_last_fault
- clear_vsie_icpt
- retry_vsie_icpt
- handle_stfle
- do_vsie_run
- release_gmap_shadow
- acquire_gmap_shadow
- register_shadow_scb
- unregister_shadow_scb
- vsie_run
- get_vsie_page
- put_vsie_page
- kvm_s390_handle_vsie
- kvm_s390_vsie_init
- kvm_s390_vsie_destroy
- kvm_s390_vsie_kick
1
2
3
4
5
6
7
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;
26
27
28
29
30 struct mcck_volatile_info mcck_info;
31
32
33
34
35
36 struct kvm_s390_sie_block *scb_o;
37
38 struct gmap *gmap;
39
40 unsigned long fault_addr;
41
42 gpa_t sca_gpa;
43 gpa_t itdba_gpa;
44 gpa_t gvrd_gpa;
45 gpa_t riccbd_gpa;
46 gpa_t sdnx_gpa;
47 __u8 reserved[0x0700 - 0x0258];
48 struct kvm_s390_crypto_cb crycb;
49 __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE];
50 };
51
52
53 static int set_validity_icpt(struct kvm_s390_sie_block *scb,
54 __u16 reason_code)
55 {
56 scb->ipa = 0x1000;
57 scb->ipb = ((__u32) reason_code) << 16;
58 scb->icptcode = ICPT_VALIDITY;
59 return 1;
60 }
61
62
63 static void prefix_unmapped(struct vsie_page *vsie_page)
64 {
65 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
66 }
67
68
69 static void prefix_unmapped_sync(struct vsie_page *vsie_page)
70 {
71 prefix_unmapped(vsie_page);
72 if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
73 atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
74 while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
75 cpu_relax();
76 }
77
78
79 static void prefix_mapped(struct vsie_page *vsie_page)
80 {
81 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
82 }
83
84
85 static int prefix_is_mapped(struct vsie_page *vsie_page)
86 {
87 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
88 }
89
90
91 static void update_intervention_requests(struct vsie_page *vsie_page)
92 {
93 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
94 int cpuflags;
95
96 cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
97 atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
98 atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
99 }
100
101
102 static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
103 {
104 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
105 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
106 int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
107
108
109 if (!(cpuflags & CPUSTAT_ZARCH))
110 return set_validity_icpt(scb_s, 0x0001U);
111
112 if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
113 return set_validity_icpt(scb_s, 0x0001U);
114 else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
115 return set_validity_icpt(scb_s, 0x0007U);
116
117
118 newflags = CPUSTAT_ZARCH;
119 if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8))
120 newflags |= CPUSTAT_GED;
121 if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) {
122 if (cpuflags & CPUSTAT_GED)
123 return set_validity_icpt(scb_s, 0x0001U);
124 newflags |= CPUSTAT_GED2;
125 }
126 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE))
127 newflags |= cpuflags & CPUSTAT_P;
128 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS))
129 newflags |= cpuflags & CPUSTAT_SM;
130 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS))
131 newflags |= cpuflags & CPUSTAT_IBS;
132 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_KSS))
133 newflags |= cpuflags & CPUSTAT_KSS;
134
135 atomic_set(&scb_s->cpuflags, newflags);
136 return 0;
137 }
138
139 static int setup_apcb10(struct kvm_vcpu *vcpu, struct kvm_s390_apcb1 *apcb_s,
140 unsigned long apcb_o, struct kvm_s390_apcb1 *apcb_h)
141 {
142 struct kvm_s390_apcb0 tmp;
143
144 if (read_guest_real(vcpu, apcb_o, &tmp, sizeof(struct kvm_s390_apcb0)))
145 return -EFAULT;
146
147 apcb_s->apm[0] = apcb_h->apm[0] & tmp.apm[0];
148 apcb_s->aqm[0] = apcb_h->aqm[0] & tmp.aqm[0] & 0xffff000000000000UL;
149 apcb_s->adm[0] = apcb_h->adm[0] & tmp.adm[0] & 0xffff000000000000UL;
150
151 return 0;
152
153 }
154
155
156
157
158
159
160
161
162
163
164 static int setup_apcb00(struct kvm_vcpu *vcpu, unsigned long *apcb_s,
165 unsigned long apcb_o, unsigned long *apcb_h)
166 {
167 if (read_guest_real(vcpu, apcb_o, apcb_s,
168 sizeof(struct kvm_s390_apcb0)))
169 return -EFAULT;
170
171 bitmap_and(apcb_s, apcb_s, apcb_h, sizeof(struct kvm_s390_apcb0));
172
173 return 0;
174 }
175
176
177
178
179
180
181
182
183
184
185 static int setup_apcb11(struct kvm_vcpu *vcpu, unsigned long *apcb_s,
186 unsigned long apcb_o,
187 unsigned long *apcb_h)
188 {
189 if (read_guest_real(vcpu, apcb_o, apcb_s,
190 sizeof(struct kvm_s390_apcb1)))
191 return -EFAULT;
192
193 bitmap_and(apcb_s, apcb_s, apcb_h, sizeof(struct kvm_s390_apcb1));
194
195 return 0;
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 static int setup_apcb(struct kvm_vcpu *vcpu, struct kvm_s390_crypto_cb *crycb_s,
213 const u32 crycb_o,
214 struct kvm_s390_crypto_cb *crycb_h,
215 int fmt_o, int fmt_h)
216 {
217 struct kvm_s390_crypto_cb *crycb;
218
219 crycb = (struct kvm_s390_crypto_cb *) (unsigned long)crycb_o;
220
221 switch (fmt_o) {
222 case CRYCB_FORMAT2:
223 if ((crycb_o & PAGE_MASK) != ((crycb_o + 256) & PAGE_MASK))
224 return -EACCES;
225 if (fmt_h != CRYCB_FORMAT2)
226 return -EINVAL;
227 return setup_apcb11(vcpu, (unsigned long *)&crycb_s->apcb1,
228 (unsigned long) &crycb->apcb1,
229 (unsigned long *)&crycb_h->apcb1);
230 case CRYCB_FORMAT1:
231 switch (fmt_h) {
232 case CRYCB_FORMAT2:
233 return setup_apcb10(vcpu, &crycb_s->apcb1,
234 (unsigned long) &crycb->apcb0,
235 &crycb_h->apcb1);
236 case CRYCB_FORMAT1:
237 return setup_apcb00(vcpu,
238 (unsigned long *) &crycb_s->apcb0,
239 (unsigned long) &crycb->apcb0,
240 (unsigned long *) &crycb_h->apcb0);
241 }
242 break;
243 case CRYCB_FORMAT0:
244 if ((crycb_o & PAGE_MASK) != ((crycb_o + 32) & PAGE_MASK))
245 return -EACCES;
246
247 switch (fmt_h) {
248 case CRYCB_FORMAT2:
249 return setup_apcb10(vcpu, &crycb_s->apcb1,
250 (unsigned long) &crycb->apcb0,
251 &crycb_h->apcb1);
252 case CRYCB_FORMAT1:
253 case CRYCB_FORMAT0:
254 return setup_apcb00(vcpu,
255 (unsigned long *) &crycb_s->apcb0,
256 (unsigned long) &crycb->apcb0,
257 (unsigned long *) &crycb_h->apcb0);
258 }
259 }
260 return -EINVAL;
261 }
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
284 {
285 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
286 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
287 const uint32_t crycbd_o = READ_ONCE(scb_o->crycbd);
288 const u32 crycb_addr = crycbd_o & 0x7ffffff8U;
289 unsigned long *b1, *b2;
290 u8 ecb3_flags;
291 u32 ecd_flags;
292 int apie_h;
293 int apie_s;
294 int key_msk = test_kvm_facility(vcpu->kvm, 76);
295 int fmt_o = crycbd_o & CRYCB_FORMAT_MASK;
296 int fmt_h = vcpu->arch.sie_block->crycbd & CRYCB_FORMAT_MASK;
297 int ret = 0;
298
299 scb_s->crycbd = 0;
300
301 apie_h = vcpu->arch.sie_block->eca & ECA_APIE;
302 apie_s = apie_h & scb_o->eca;
303 if (!apie_s && (!key_msk || (fmt_o == CRYCB_FORMAT0)))
304 return 0;
305
306 if (!crycb_addr)
307 return set_validity_icpt(scb_s, 0x0039U);
308
309 if (fmt_o == CRYCB_FORMAT1)
310 if ((crycb_addr & PAGE_MASK) !=
311 ((crycb_addr + 128) & PAGE_MASK))
312 return set_validity_icpt(scb_s, 0x003CU);
313
314 if (apie_s) {
315 ret = setup_apcb(vcpu, &vsie_page->crycb, crycb_addr,
316 vcpu->kvm->arch.crypto.crycb,
317 fmt_o, fmt_h);
318 if (ret)
319 goto end;
320 scb_s->eca |= scb_o->eca & ECA_APIE;
321 }
322
323
324 ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
325 (ECB3_AES | ECB3_DEA);
326 ecd_flags = scb_o->ecd & vcpu->arch.sie_block->ecd & ECD_ECC;
327 if (!ecb3_flags && !ecd_flags)
328 goto end;
329
330
331 if (read_guest_real(vcpu, crycb_addr + 72,
332 vsie_page->crycb.dea_wrapping_key_mask, 56))
333 return set_validity_icpt(scb_s, 0x0035U);
334
335 scb_s->ecb3 |= ecb3_flags;
336 scb_s->ecd |= ecd_flags;
337
338
339 b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
340 b2 = (unsigned long *)
341 vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
342
343 bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
344 end:
345 switch (ret) {
346 case -EINVAL:
347 return set_validity_icpt(scb_s, 0x0022U);
348 case -EFAULT:
349 return set_validity_icpt(scb_s, 0x0035U);
350 case -EACCES:
351 return set_validity_icpt(scb_s, 0x003CU);
352 }
353 scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT2;
354 return 0;
355 }
356
357
358 static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
359 {
360 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
361 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
362
363 const uint32_t __new_ibc = scb_o->ibc;
364 const uint32_t new_ibc = READ_ONCE(__new_ibc) & 0x0fffU;
365 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
366
367 scb_s->ibc = 0;
368
369 if (vcpu->kvm->arch.model.ibc && new_ibc) {
370 scb_s->ibc = new_ibc;
371
372 if (scb_s->ibc < min_ibc)
373 scb_s->ibc = min_ibc;
374
375 if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
376 scb_s->ibc = vcpu->kvm->arch.model.ibc;
377 }
378 }
379
380
381 static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
382 {
383 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
384 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
385
386
387 scb_o->icptcode = scb_s->icptcode;
388 scb_o->icptstatus = scb_s->icptstatus;
389 scb_o->ipa = scb_s->ipa;
390 scb_o->ipb = scb_s->ipb;
391 scb_o->gbea = scb_s->gbea;
392
393
394 scb_o->cputm = scb_s->cputm;
395 scb_o->ckc = scb_s->ckc;
396 scb_o->todpr = scb_s->todpr;
397
398
399 scb_o->gpsw = scb_s->gpsw;
400 scb_o->gg14 = scb_s->gg14;
401 scb_o->gg15 = scb_s->gg15;
402 memcpy(scb_o->gcr, scb_s->gcr, 128);
403 scb_o->pp = scb_s->pp;
404
405
406 if (test_kvm_facility(vcpu->kvm, 82)) {
407 scb_o->fpf &= ~FPF_BPBC;
408 scb_o->fpf |= scb_s->fpf & FPF_BPBC;
409 }
410
411
412 switch (scb_s->icptcode) {
413 case ICPT_PROGI:
414 case ICPT_INSTPROGI:
415 case ICPT_EXTINT:
416 memcpy((void *)((u64)scb_o + 0xc0),
417 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
418 break;
419 case ICPT_PARTEXEC:
420
421 memcpy((void *)((u64)scb_o + 0xc0),
422 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
423 break;
424 }
425
426 if (scb_s->ihcpu != 0xffffU)
427 scb_o->ihcpu = scb_s->ihcpu;
428 }
429
430
431
432
433
434
435
436
437 static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
438 {
439 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
440 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
441
442 const uint32_t __new_prefix = scb_o->prefix;
443 const uint32_t new_prefix = READ_ONCE(__new_prefix);
444 const bool wants_tx = READ_ONCE(scb_o->ecb) & ECB_TE;
445 bool had_tx = scb_s->ecb & ECB_TE;
446 unsigned long new_mso = 0;
447 int rc;
448
449
450 scb_s->icptcode = 0;
451 scb_s->eca = 0;
452 scb_s->ecb = 0;
453 scb_s->ecb2 = 0;
454 scb_s->ecb3 = 0;
455 scb_s->ecd = 0;
456 scb_s->fac = 0;
457 scb_s->fpf = 0;
458
459 rc = prepare_cpuflags(vcpu, vsie_page);
460 if (rc)
461 goto out;
462
463
464 scb_s->cputm = scb_o->cputm;
465 scb_s->ckc = scb_o->ckc;
466 scb_s->todpr = scb_o->todpr;
467 scb_s->epoch = scb_o->epoch;
468
469
470 scb_s->gpsw = scb_o->gpsw;
471 scb_s->gg14 = scb_o->gg14;
472 scb_s->gg15 = scb_o->gg15;
473 memcpy(scb_s->gcr, scb_o->gcr, 128);
474 scb_s->pp = scb_o->pp;
475
476
477 scb_s->gbea = scb_o->gbea;
478 scb_s->lctl = scb_o->lctl;
479 scb_s->svcc = scb_o->svcc;
480 scb_s->ictl = scb_o->ictl;
481
482
483
484
485
486 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
487 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
488
489 scb_s->icpua = scb_o->icpua;
490
491 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
492 new_mso = READ_ONCE(scb_o->mso) & 0xfffffffffff00000UL;
493
494 if (scb_s->mso != new_mso || scb_s->prefix != new_prefix)
495 prefix_unmapped(vsie_page);
496
497 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
498 scb_s->mso = new_mso;
499 scb_s->prefix = new_prefix;
500
501
502 if (scb_s->ihcpu != 0xffffU)
503 scb_s->ihcpu = scb_o->ihcpu;
504
505
506 scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
507
508 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
509 scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
510
511 if (test_kvm_facility(vcpu->kvm, 73) && wants_tx) {
512
513 if (!had_tx)
514 prefix_unmapped(vsie_page);
515 scb_s->ecb |= ECB_TE;
516 }
517
518 if (test_kvm_facility(vcpu->kvm, 82))
519 scb_s->fpf |= scb_o->fpf & FPF_BPBC;
520
521 if (test_kvm_facility(vcpu->kvm, 129)) {
522 scb_s->eca |= scb_o->eca & ECA_VX;
523 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
524 }
525
526 if (test_kvm_facility(vcpu->kvm, 64))
527 scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
528
529 if (test_kvm_facility(vcpu->kvm, 130))
530 scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
531
532 if (test_kvm_facility(vcpu->kvm, 133)) {
533 scb_s->ecb |= scb_o->ecb & ECB_GS;
534 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
535 }
536 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
537 scb_s->eca |= scb_o->eca & ECA_SII;
538 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
539 scb_s->eca |= scb_o->eca & ECA_IB;
540 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
541 scb_s->eca |= scb_o->eca & ECA_CEI;
542
543 if (test_kvm_facility(vcpu->kvm, 139))
544 scb_s->ecd |= scb_o->ecd & ECD_MEF;
545
546
547 if (test_kvm_facility(vcpu->kvm, 156))
548 scb_s->ecd |= scb_o->ecd & ECD_ETOKENF;
549
550 scb_s->hpid = HPID_VSIE;
551
552 prepare_ibc(vcpu, vsie_page);
553 rc = shadow_crycb(vcpu, vsie_page);
554 out:
555 if (rc)
556 unshadow_scb(vcpu, vsie_page);
557 return rc;
558 }
559
560 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
561 unsigned long end)
562 {
563 struct kvm *kvm = gmap->private;
564 struct vsie_page *cur;
565 unsigned long prefix;
566 struct page *page;
567 int i;
568
569 if (!gmap_is_shadow(gmap))
570 return;
571 if (start >= 1UL << 31)
572
573 return;
574
575
576
577
578
579 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
580 page = READ_ONCE(kvm->arch.vsie.pages[i]);
581 if (!page)
582 continue;
583 cur = page_to_virt(page);
584 if (READ_ONCE(cur->gmap) != gmap)
585 continue;
586 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
587
588 prefix += cur->scb_s.mso;
589 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
590 prefix_unmapped_sync(cur);
591 }
592 }
593
594
595
596
597
598
599
600
601
602
603
604
605
606 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
607 {
608 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
609 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
610 int rc;
611
612 if (prefix_is_mapped(vsie_page))
613 return 0;
614
615
616 prefix_mapped(vsie_page);
617
618
619 prefix += scb_s->mso;
620
621 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
622 if (!rc && (scb_s->ecb & ECB_TE))
623 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
624 prefix + PAGE_SIZE);
625
626
627
628
629 if (rc)
630 prefix_unmapped(vsie_page);
631 if (rc > 0 || rc == -EFAULT)
632 rc = set_validity_icpt(scb_s, 0x0037U);
633 return rc;
634 }
635
636
637
638
639
640
641
642
643 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
644 {
645 struct page *page;
646
647 page = gfn_to_page(kvm, gpa_to_gfn(gpa));
648 if (is_error_page(page))
649 return -EINVAL;
650 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
651 return 0;
652 }
653
654
655 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
656 {
657 kvm_release_pfn_dirty(hpa >> PAGE_SHIFT);
658
659 mark_page_dirty(kvm, gpa_to_gfn(gpa));
660 }
661
662
663 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
664 {
665 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
666 hpa_t hpa;
667
668 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
669 if (hpa) {
670 unpin_guest_page(vcpu->kvm, vsie_page->sca_gpa, hpa);
671 vsie_page->sca_gpa = 0;
672 scb_s->scaol = 0;
673 scb_s->scaoh = 0;
674 }
675
676 hpa = scb_s->itdba;
677 if (hpa) {
678 unpin_guest_page(vcpu->kvm, vsie_page->itdba_gpa, hpa);
679 vsie_page->itdba_gpa = 0;
680 scb_s->itdba = 0;
681 }
682
683 hpa = scb_s->gvrd;
684 if (hpa) {
685 unpin_guest_page(vcpu->kvm, vsie_page->gvrd_gpa, hpa);
686 vsie_page->gvrd_gpa = 0;
687 scb_s->gvrd = 0;
688 }
689
690 hpa = scb_s->riccbd;
691 if (hpa) {
692 unpin_guest_page(vcpu->kvm, vsie_page->riccbd_gpa, hpa);
693 vsie_page->riccbd_gpa = 0;
694 scb_s->riccbd = 0;
695 }
696
697 hpa = scb_s->sdnxo;
698 if (hpa) {
699 unpin_guest_page(vcpu->kvm, vsie_page->sdnx_gpa, hpa);
700 vsie_page->sdnx_gpa = 0;
701 scb_s->sdnxo = 0;
702 }
703 }
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
720 {
721 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
722 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
723 hpa_t hpa;
724 gpa_t gpa;
725 int rc = 0;
726
727 gpa = READ_ONCE(scb_o->scaol) & ~0xfUL;
728 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
729 gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32;
730 if (gpa) {
731 if (gpa < 2 * PAGE_SIZE)
732 rc = set_validity_icpt(scb_s, 0x0038U);
733 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
734 rc = set_validity_icpt(scb_s, 0x0011U);
735 else if ((gpa & PAGE_MASK) !=
736 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
737 rc = set_validity_icpt(scb_s, 0x003bU);
738 if (!rc) {
739 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
740 if (rc)
741 rc = set_validity_icpt(scb_s, 0x0034U);
742 }
743 if (rc)
744 goto unpin;
745 vsie_page->sca_gpa = gpa;
746 scb_s->scaoh = (u32)((u64)hpa >> 32);
747 scb_s->scaol = (u32)(u64)hpa;
748 }
749
750 gpa = READ_ONCE(scb_o->itdba) & ~0xffUL;
751 if (gpa && (scb_s->ecb & ECB_TE)) {
752 if (gpa < 2 * PAGE_SIZE) {
753 rc = set_validity_icpt(scb_s, 0x0080U);
754 goto unpin;
755 }
756
757 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
758 if (rc) {
759 rc = set_validity_icpt(scb_s, 0x0080U);
760 goto unpin;
761 }
762 vsie_page->itdba_gpa = gpa;
763 scb_s->itdba = hpa;
764 }
765
766 gpa = READ_ONCE(scb_o->gvrd) & ~0x1ffUL;
767 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
768 if (gpa < 2 * PAGE_SIZE) {
769 rc = set_validity_icpt(scb_s, 0x1310U);
770 goto unpin;
771 }
772
773
774
775
776 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
777 if (rc) {
778 rc = set_validity_icpt(scb_s, 0x1310U);
779 goto unpin;
780 }
781 vsie_page->gvrd_gpa = gpa;
782 scb_s->gvrd = hpa;
783 }
784
785 gpa = READ_ONCE(scb_o->riccbd) & ~0x3fUL;
786 if (gpa && (scb_s->ecb3 & ECB3_RI)) {
787 if (gpa < 2 * PAGE_SIZE) {
788 rc = set_validity_icpt(scb_s, 0x0043U);
789 goto unpin;
790 }
791
792 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
793 if (rc) {
794 rc = set_validity_icpt(scb_s, 0x0043U);
795 goto unpin;
796 }
797
798 vsie_page->riccbd_gpa = gpa;
799 scb_s->riccbd = hpa;
800 }
801 if (((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) ||
802 (scb_s->ecd & ECD_ETOKENF)) {
803 unsigned long sdnxc;
804
805 gpa = READ_ONCE(scb_o->sdnxo) & ~0xfUL;
806 sdnxc = READ_ONCE(scb_o->sdnxo) & 0xfUL;
807 if (!gpa || gpa < 2 * PAGE_SIZE) {
808 rc = set_validity_icpt(scb_s, 0x10b0U);
809 goto unpin;
810 }
811 if (sdnxc < 6 || sdnxc > 12) {
812 rc = set_validity_icpt(scb_s, 0x10b1U);
813 goto unpin;
814 }
815 if (gpa & ((1 << sdnxc) - 1)) {
816 rc = set_validity_icpt(scb_s, 0x10b2U);
817 goto unpin;
818 }
819
820
821
822 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
823 if (rc) {
824 rc = set_validity_icpt(scb_s, 0x10b0U);
825 goto unpin;
826 }
827 vsie_page->sdnx_gpa = gpa;
828 scb_s->sdnxo = hpa | sdnxc;
829 }
830 return 0;
831 unpin:
832 unpin_blocks(vcpu, vsie_page);
833 return rc;
834 }
835
836
837 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
838 gpa_t gpa)
839 {
840 hpa_t hpa = (hpa_t) vsie_page->scb_o;
841
842 if (hpa)
843 unpin_guest_page(vcpu->kvm, gpa, hpa);
844 vsie_page->scb_o = NULL;
845 }
846
847
848
849
850
851
852
853 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
854 gpa_t gpa)
855 {
856 hpa_t hpa;
857 int rc;
858
859 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
860 if (rc) {
861 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
862 WARN_ON_ONCE(rc);
863 return 1;
864 }
865 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
866 return 0;
867 }
868
869
870
871
872
873
874
875 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
876 bool write_flag)
877 {
878 struct kvm_s390_pgm_info pgm = {
879 .code = code,
880 .trans_exc_code =
881
882 (vaddr & 0xfffffffffffff000UL) |
883
884 (((unsigned int) !write_flag) + 1) << 10,
885
886 .exc_access_id = 0,
887 .op_access_id = 0,
888 };
889 int rc;
890
891 if (code == PGM_PROTECTION)
892 pgm.trans_exc_code |= 0x4UL;
893
894 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
895 return rc ? rc : 1;
896 }
897
898
899
900
901
902
903
904
905 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
906 {
907 int rc;
908
909 if (current->thread.gmap_int_code == PGM_PROTECTION)
910
911 return inject_fault(vcpu, PGM_PROTECTION,
912 current->thread.gmap_addr, 1);
913
914 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
915 current->thread.gmap_addr);
916 if (rc > 0) {
917 rc = inject_fault(vcpu, rc,
918 current->thread.gmap_addr,
919 current->thread.gmap_write_flag);
920 if (rc >= 0)
921 vsie_page->fault_addr = current->thread.gmap_addr;
922 }
923 return rc;
924 }
925
926
927
928
929
930
931
932 static void handle_last_fault(struct kvm_vcpu *vcpu,
933 struct vsie_page *vsie_page)
934 {
935 if (vsie_page->fault_addr)
936 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
937 vsie_page->fault_addr);
938 vsie_page->fault_addr = 0;
939 }
940
941 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
942 {
943 vsie_page->scb_s.icptcode = 0;
944 }
945
946
947 static void retry_vsie_icpt(struct vsie_page *vsie_page)
948 {
949 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
950 int ilen = insn_length(scb_s->ipa >> 8);
951
952
953 if (scb_s->icptstatus & 1) {
954 ilen = (scb_s->icptstatus >> 4) & 0x6;
955 if (!ilen)
956 ilen = 4;
957 }
958 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
959 clear_vsie_icpt(vsie_page);
960 }
961
962
963
964
965
966
967
968
969 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
970 {
971 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
972 __u32 fac = READ_ONCE(vsie_page->scb_o->fac) & 0x7ffffff8U;
973
974 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
975 retry_vsie_icpt(vsie_page);
976 if (read_guest_real(vcpu, fac, &vsie_page->fac,
977 sizeof(vsie_page->fac)))
978 return set_validity_icpt(scb_s, 0x1090U);
979 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
980 }
981 return 0;
982 }
983
984
985
986
987
988
989
990
991
992 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
993 __releases(vcpu->kvm->srcu)
994 __acquires(vcpu->kvm->srcu)
995 {
996 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
997 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
998 int guest_bp_isolation;
999 int rc = 0;
1000
1001 handle_last_fault(vcpu, vsie_page);
1002
1003 if (need_resched())
1004 schedule();
1005 if (test_cpu_flag(CIF_MCCK_PENDING))
1006 s390_handle_mcck();
1007
1008 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
1009
1010
1011 guest_bp_isolation = test_thread_flag(TIF_ISOLATE_BP_GUEST);
1012
1013
1014
1015
1016
1017
1018
1019 if (test_kvm_facility(vcpu->kvm, 82) &&
1020 vcpu->arch.sie_block->fpf & FPF_BPBC)
1021 set_thread_flag(TIF_ISOLATE_BP_GUEST);
1022
1023 local_irq_disable();
1024 guest_enter_irqoff();
1025 local_irq_enable();
1026
1027
1028
1029
1030
1031
1032
1033 vcpu->arch.sie_block->prog0c |= PROG_IN_SIE;
1034 barrier();
1035 if (!kvm_s390_vcpu_sie_inhibited(vcpu))
1036 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
1037 barrier();
1038 vcpu->arch.sie_block->prog0c &= ~PROG_IN_SIE;
1039
1040 local_irq_disable();
1041 guest_exit_irqoff();
1042 local_irq_enable();
1043
1044
1045 if (!guest_bp_isolation)
1046 clear_thread_flag(TIF_ISOLATE_BP_GUEST);
1047
1048 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1049
1050 if (rc == -EINTR) {
1051 VCPU_EVENT(vcpu, 3, "%s", "machine check");
1052 kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
1053 return 0;
1054 }
1055
1056 if (rc > 0)
1057 rc = 0;
1058 else if (rc == -EFAULT)
1059 return handle_fault(vcpu, vsie_page);
1060
1061 switch (scb_s->icptcode) {
1062 case ICPT_INST:
1063 if (scb_s->ipa == 0xb2b0)
1064 rc = handle_stfle(vcpu, vsie_page);
1065 break;
1066 case ICPT_STOP:
1067
1068 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
1069 clear_vsie_icpt(vsie_page);
1070 break;
1071 case ICPT_VALIDITY:
1072 if ((scb_s->ipa & 0xf000) != 0xf000)
1073 scb_s->ipa += 0x1000;
1074 break;
1075 }
1076 return rc;
1077 }
1078
1079 static void release_gmap_shadow(struct vsie_page *vsie_page)
1080 {
1081 if (vsie_page->gmap)
1082 gmap_put(vsie_page->gmap);
1083 WRITE_ONCE(vsie_page->gmap, NULL);
1084 prefix_unmapped(vsie_page);
1085 }
1086
1087 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
1088 struct vsie_page *vsie_page)
1089 {
1090 unsigned long asce;
1091 union ctlreg0 cr0;
1092 struct gmap *gmap;
1093 int edat;
1094
1095 asce = vcpu->arch.sie_block->gcr[1];
1096 cr0.val = vcpu->arch.sie_block->gcr[0];
1097 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
1098 edat += edat && test_kvm_facility(vcpu->kvm, 78);
1099
1100
1101
1102
1103
1104
1105 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
1106 return 0;
1107
1108
1109 release_gmap_shadow(vsie_page);
1110 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
1111 if (IS_ERR(gmap))
1112 return PTR_ERR(gmap);
1113 gmap->private = vcpu->kvm;
1114 WRITE_ONCE(vsie_page->gmap, gmap);
1115 return 0;
1116 }
1117
1118
1119
1120
1121 static void register_shadow_scb(struct kvm_vcpu *vcpu,
1122 struct vsie_page *vsie_page)
1123 {
1124 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
1125
1126 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
1127
1128
1129
1130
1131 kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
1132
1133
1134
1135
1136 preempt_disable();
1137 scb_s->epoch += vcpu->kvm->arch.epoch;
1138
1139 if (scb_s->ecd & ECD_MEF) {
1140 scb_s->epdx += vcpu->kvm->arch.epdx;
1141 if (scb_s->epoch < vcpu->kvm->arch.epoch)
1142 scb_s->epdx += 1;
1143 }
1144
1145 preempt_enable();
1146 }
1147
1148
1149
1150
1151 static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
1152 {
1153 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_WAIT);
1154 WRITE_ONCE(vcpu->arch.vsie_block, NULL);
1155 }
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
1166 {
1167 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
1168 int rc = 0;
1169
1170 while (1) {
1171 rc = acquire_gmap_shadow(vcpu, vsie_page);
1172 if (!rc)
1173 rc = map_prefix(vcpu, vsie_page);
1174 if (!rc) {
1175 gmap_enable(vsie_page->gmap);
1176 update_intervention_requests(vsie_page);
1177 rc = do_vsie_run(vcpu, vsie_page);
1178 gmap_enable(vcpu->arch.gmap);
1179 }
1180 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
1181
1182 if (rc == -EAGAIN)
1183 rc = 0;
1184 if (rc || scb_s->icptcode || signal_pending(current) ||
1185 kvm_s390_vcpu_has_irq(vcpu, 0) ||
1186 kvm_s390_vcpu_sie_inhibited(vcpu))
1187 break;
1188 }
1189
1190 if (rc == -EFAULT) {
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201 scb_s->icptcode = ICPT_PROGI;
1202 scb_s->iprcc = PGM_ADDRESSING;
1203 scb_s->pgmilc = 4;
1204 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
1205 rc = 1;
1206 }
1207 return rc;
1208 }
1209
1210
1211
1212
1213
1214
1215
1216
1217 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
1218 {
1219 struct vsie_page *vsie_page;
1220 struct page *page;
1221 int nr_vcpus;
1222
1223 rcu_read_lock();
1224 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1225 rcu_read_unlock();
1226 if (page) {
1227 if (page_ref_inc_return(page) == 2)
1228 return page_to_virt(page);
1229 page_ref_dec(page);
1230 }
1231
1232
1233
1234
1235
1236 nr_vcpus = atomic_read(&kvm->online_vcpus);
1237
1238 mutex_lock(&kvm->arch.vsie.mutex);
1239 if (kvm->arch.vsie.page_count < nr_vcpus) {
1240 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
1241 if (!page) {
1242 mutex_unlock(&kvm->arch.vsie.mutex);
1243 return ERR_PTR(-ENOMEM);
1244 }
1245 page_ref_inc(page);
1246 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1247 kvm->arch.vsie.page_count++;
1248 } else {
1249
1250 while (true) {
1251 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1252 if (page_ref_inc_return(page) == 2)
1253 break;
1254 page_ref_dec(page);
1255 kvm->arch.vsie.next++;
1256 kvm->arch.vsie.next %= nr_vcpus;
1257 }
1258 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1259 }
1260 page->index = addr;
1261
1262 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1263 page_ref_dec(page);
1264 mutex_unlock(&kvm->arch.vsie.mutex);
1265 return NULL;
1266 }
1267 mutex_unlock(&kvm->arch.vsie.mutex);
1268
1269 vsie_page = page_to_virt(page);
1270 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
1271 release_gmap_shadow(vsie_page);
1272 vsie_page->fault_addr = 0;
1273 vsie_page->scb_s.ihcpu = 0xffffU;
1274 return vsie_page;
1275 }
1276
1277
1278 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1279 {
1280 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1281
1282 page_ref_dec(page);
1283 }
1284
1285 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1286 {
1287 struct vsie_page *vsie_page;
1288 unsigned long scb_addr;
1289 int rc;
1290
1291 vcpu->stat.instruction_sie++;
1292 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1293 return -EOPNOTSUPP;
1294 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1295 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1296
1297 BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
1298 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1299
1300
1301 if (unlikely(scb_addr & 0x1ffUL))
1302 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1303
1304 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0) ||
1305 kvm_s390_vcpu_sie_inhibited(vcpu))
1306 return 0;
1307
1308 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1309 if (IS_ERR(vsie_page))
1310 return PTR_ERR(vsie_page);
1311 else if (!vsie_page)
1312
1313 return 0;
1314
1315 rc = pin_scb(vcpu, vsie_page, scb_addr);
1316 if (rc)
1317 goto out_put;
1318 rc = shadow_scb(vcpu, vsie_page);
1319 if (rc)
1320 goto out_unpin_scb;
1321 rc = pin_blocks(vcpu, vsie_page);
1322 if (rc)
1323 goto out_unshadow;
1324 register_shadow_scb(vcpu, vsie_page);
1325 rc = vsie_run(vcpu, vsie_page);
1326 unregister_shadow_scb(vcpu);
1327 unpin_blocks(vcpu, vsie_page);
1328 out_unshadow:
1329 unshadow_scb(vcpu, vsie_page);
1330 out_unpin_scb:
1331 unpin_scb(vcpu, vsie_page, scb_addr);
1332 out_put:
1333 put_vsie_page(vcpu->kvm, vsie_page);
1334
1335 return rc < 0 ? rc : 0;
1336 }
1337
1338
1339 void kvm_s390_vsie_init(struct kvm *kvm)
1340 {
1341 mutex_init(&kvm->arch.vsie.mutex);
1342 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1343 }
1344
1345
1346 void kvm_s390_vsie_destroy(struct kvm *kvm)
1347 {
1348 struct vsie_page *vsie_page;
1349 struct page *page;
1350 int i;
1351
1352 mutex_lock(&kvm->arch.vsie.mutex);
1353 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1354 page = kvm->arch.vsie.pages[i];
1355 kvm->arch.vsie.pages[i] = NULL;
1356 vsie_page = page_to_virt(page);
1357 release_gmap_shadow(vsie_page);
1358
1359 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1360 __free_page(page);
1361 }
1362 kvm->arch.vsie.page_count = 0;
1363 mutex_unlock(&kvm->arch.vsie.mutex);
1364 }
1365
1366 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1367 {
1368 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1369
1370
1371
1372
1373
1374 if (scb) {
1375 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1376 if (scb->prog0c & PROG_IN_SIE)
1377 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);
1378 }
1379 }