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