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