GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / 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
379         prepare_ibc(vcpu, vsie_page);
380         rc = shadow_crycb(vcpu, vsie_page);
381 out:
382         if (rc)
383                 unshadow_scb(vcpu, vsie_page);
384         return rc;
385 }
386
387 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
388                                  unsigned long end)
389 {
390         struct kvm *kvm = gmap->private;
391         struct vsie_page *cur;
392         unsigned long prefix;
393         struct page *page;
394         int i;
395
396         if (!gmap_is_shadow(gmap))
397                 return;
398         if (start >= 1UL << 31)
399                 /* We are only interested in prefix pages */
400                 return;
401
402         /*
403          * Only new shadow blocks are added to the list during runtime,
404          * therefore we can safely reference them all the time.
405          */
406         for (i = 0; i < kvm->arch.vsie.page_count; i++) {
407                 page = READ_ONCE(kvm->arch.vsie.pages[i]);
408                 if (!page)
409                         continue;
410                 cur = page_to_virt(page);
411                 if (READ_ONCE(cur->gmap) != gmap)
412                         continue;
413                 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
414                 /* with mso/msl, the prefix lies at an offset */
415                 prefix += cur->scb_s.mso;
416                 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
417                         prefix_unmapped_sync(cur);
418         }
419 }
420
421 /*
422  * Map the first prefix page and if tx is enabled also the second prefix page.
423  *
424  * The prefix will be protected, a gmap notifier will inform about unmaps.
425  * The shadow scb must not be executed until the prefix is remapped, this is
426  * guaranteed by properly handling PROG_REQUEST.
427  *
428  * Returns: - 0 on if successfully mapped or already mapped
429  *          - > 0 if control has to be given to guest 2
430  *          - -EAGAIN if the caller can retry immediately
431  *          - -ENOMEM if out of memory
432  */
433 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
434 {
435         struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
436         u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
437         int rc;
438
439         if (prefix_is_mapped(vsie_page))
440                 return 0;
441
442         /* mark it as mapped so we can catch any concurrent unmappers */
443         prefix_mapped(vsie_page);
444
445         /* with mso/msl, the prefix lies at offset *mso* */
446         prefix += scb_s->mso;
447
448         rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
449         if (!rc && (scb_s->ecb & ECB_TE))
450                 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
451                                            prefix + PAGE_SIZE);
452         /*
453          * We don't have to mprotect, we will be called for all unshadows.
454          * SIE will detect if protection applies and trigger a validity.
455          */
456         if (rc)
457                 prefix_unmapped(vsie_page);
458         if (rc > 0 || rc == -EFAULT)
459                 rc = set_validity_icpt(scb_s, 0x0037U);
460         return rc;
461 }
462
463 /*
464  * Pin the guest page given by gpa and set hpa to the pinned host address.
465  * Will always be pinned writable.
466  *
467  * Returns: - 0 on success
468  *          - -EINVAL if the gpa is not valid guest storage
469  *          - -ENOMEM if out of memory
470  */
471 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
472 {
473         struct page *page;
474         hva_t hva;
475         int rc;
476
477         hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
478         if (kvm_is_error_hva(hva))
479                 return -EINVAL;
480         rc = get_user_pages_fast(hva, 1, 1, &page);
481         if (rc < 0)
482                 return rc;
483         else if (rc != 1)
484                 return -ENOMEM;
485         *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
486         return 0;
487 }
488
489 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
490 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
491 {
492         struct page *page;
493
494         page = virt_to_page(hpa);
495         set_page_dirty_lock(page);
496         put_page(page);
497         /* mark the page always as dirty for migration */
498         mark_page_dirty(kvm, gpa_to_gfn(gpa));
499 }
500
501 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
502 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
503 {
504         struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
505         struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
506         hpa_t hpa;
507         gpa_t gpa;
508
509         hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
510         if (hpa) {
511                 gpa = scb_o->scaol & ~0xfUL;
512                 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
513                         gpa |= (u64) scb_o->scaoh << 32;
514                 unpin_guest_page(vcpu->kvm, gpa, hpa);
515                 scb_s->scaol = 0;
516                 scb_s->scaoh = 0;
517         }
518
519         hpa = scb_s->itdba;
520         if (hpa) {
521                 gpa = scb_o->itdba & ~0xffUL;
522                 unpin_guest_page(vcpu->kvm, gpa, hpa);
523                 scb_s->itdba = 0;
524         }
525
526         hpa = scb_s->gvrd;
527         if (hpa) {
528                 gpa = scb_o->gvrd & ~0x1ffUL;
529                 unpin_guest_page(vcpu->kvm, gpa, hpa);
530                 scb_s->gvrd = 0;
531         }
532
533         hpa = scb_s->riccbd;
534         if (hpa) {
535                 gpa = scb_o->riccbd & ~0x3fUL;
536                 unpin_guest_page(vcpu->kvm, gpa, hpa);
537                 scb_s->riccbd = 0;
538         }
539
540         hpa = scb_s->sdnxo;
541         if (hpa) {
542                 gpa = scb_o->sdnxo;
543                 unpin_guest_page(vcpu->kvm, gpa, hpa);
544                 scb_s->sdnxo = 0;
545         }
546 }
547
548 /*
549  * Instead of shadowing some blocks, we can simply forward them because the
550  * addresses in the scb are 64 bit long.
551  *
552  * This works as long as the data lies in one page. If blocks ever exceed one
553  * page, we have to fall back to shadowing.
554  *
555  * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
556  * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
557  *
558  * Returns: - 0 if all blocks were pinned.
559  *          - > 0 if control has to be given to guest 2
560  *          - -ENOMEM if out of memory
561  */
562 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
563 {
564         struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
565         struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
566         hpa_t hpa;
567         gpa_t gpa;
568         int rc = 0;
569
570         gpa = READ_ONCE(scb_o->scaol) & ~0xfUL;
571         if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
572                 gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32;
573         if (gpa) {
574                 if (!(gpa & ~0x1fffUL))
575                         rc = set_validity_icpt(scb_s, 0x0038U);
576                 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
577                         rc = set_validity_icpt(scb_s, 0x0011U);
578                 else if ((gpa & PAGE_MASK) !=
579                          ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
580                         rc = set_validity_icpt(scb_s, 0x003bU);
581                 if (!rc) {
582                         rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
583                         if (rc == -EINVAL)
584                                 rc = set_validity_icpt(scb_s, 0x0034U);
585                 }
586                 if (rc)
587                         goto unpin;
588                 scb_s->scaoh = (u32)((u64)hpa >> 32);
589                 scb_s->scaol = (u32)(u64)hpa;
590         }
591
592         gpa = READ_ONCE(scb_o->itdba) & ~0xffUL;
593         if (gpa && (scb_s->ecb & ECB_TE)) {
594                 if (!(gpa & ~0x1fffUL)) {
595                         rc = set_validity_icpt(scb_s, 0x0080U);
596                         goto unpin;
597                 }
598                 /* 256 bytes cannot cross page boundaries */
599                 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
600                 if (rc == -EINVAL)
601                         rc = set_validity_icpt(scb_s, 0x0080U);
602                 if (rc)
603                         goto unpin;
604                 scb_s->itdba = hpa;
605         }
606
607         gpa = READ_ONCE(scb_o->gvrd) & ~0x1ffUL;
608         if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
609                 if (!(gpa & ~0x1fffUL)) {
610                         rc = set_validity_icpt(scb_s, 0x1310U);
611                         goto unpin;
612                 }
613                 /*
614                  * 512 bytes vector registers cannot cross page boundaries
615                  * if this block gets bigger, we have to shadow it.
616                  */
617                 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
618                 if (rc == -EINVAL)
619                         rc = set_validity_icpt(scb_s, 0x1310U);
620                 if (rc)
621                         goto unpin;
622                 scb_s->gvrd = hpa;
623         }
624
625         gpa = READ_ONCE(scb_o->riccbd) & ~0x3fUL;
626         if (gpa && (scb_s->ecb3 & ECB3_RI)) {
627                 if (!(gpa & ~0x1fffUL)) {
628                         rc = set_validity_icpt(scb_s, 0x0043U);
629                         goto unpin;
630                 }
631                 /* 64 bytes cannot cross page boundaries */
632                 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
633                 if (rc == -EINVAL)
634                         rc = set_validity_icpt(scb_s, 0x0043U);
635                 /* Validity 0x0044 will be checked by SIE */
636                 if (rc)
637                         goto unpin;
638                 scb_s->riccbd = hpa;
639         }
640         if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
641                 unsigned long sdnxc;
642
643                 gpa = READ_ONCE(scb_o->sdnxo) & ~0xfUL;
644                 sdnxc = READ_ONCE(scb_o->sdnxo) & 0xfUL;
645                 if (!gpa || !(gpa & ~0x1fffUL)) {
646                         rc = set_validity_icpt(scb_s, 0x10b0U);
647                         goto unpin;
648                 }
649                 if (sdnxc < 6 || sdnxc > 12) {
650                         rc = set_validity_icpt(scb_s, 0x10b1U);
651                         goto unpin;
652                 }
653                 if (gpa & ((1 << sdnxc) - 1)) {
654                         rc = set_validity_icpt(scb_s, 0x10b2U);
655                         goto unpin;
656                 }
657                 /* Due to alignment rules (checked above) this cannot
658                  * cross page boundaries
659                  */
660                 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
661                 if (rc == -EINVAL)
662                         rc = set_validity_icpt(scb_s, 0x10b0U);
663                 if (rc)
664                         goto unpin;
665                 scb_s->sdnxo = hpa | sdnxc;
666         }
667         return 0;
668 unpin:
669         unpin_blocks(vcpu, vsie_page);
670         return rc;
671 }
672
673 /* unpin the scb provided by guest 2, marking it as dirty */
674 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
675                       gpa_t gpa)
676 {
677         hpa_t hpa = (hpa_t) vsie_page->scb_o;
678
679         if (hpa)
680                 unpin_guest_page(vcpu->kvm, gpa, hpa);
681         vsie_page->scb_o = NULL;
682 }
683
684 /*
685  * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
686  *
687  * Returns: - 0 if the scb was pinned.
688  *          - > 0 if control has to be given to guest 2
689  *          - -ENOMEM if out of memory
690  */
691 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
692                    gpa_t gpa)
693 {
694         hpa_t hpa;
695         int rc;
696
697         rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
698         if (rc == -EINVAL) {
699                 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
700                 if (!rc)
701                         rc = 1;
702         }
703         if (!rc)
704                 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
705         return rc;
706 }
707
708 /*
709  * Inject a fault into guest 2.
710  *
711  * Returns: - > 0 if control has to be given to guest 2
712  *            < 0 if an error occurred during injection.
713  */
714 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
715                         bool write_flag)
716 {
717         struct kvm_s390_pgm_info pgm = {
718                 .code = code,
719                 .trans_exc_code =
720                         /* 0-51: virtual address */
721                         (vaddr & 0xfffffffffffff000UL) |
722                         /* 52-53: store / fetch */
723                         (((unsigned int) !write_flag) + 1) << 10,
724                         /* 62-63: asce id (alway primary == 0) */
725                 .exc_access_id = 0, /* always primary */
726                 .op_access_id = 0, /* not MVPG */
727         };
728         int rc;
729
730         if (code == PGM_PROTECTION)
731                 pgm.trans_exc_code |= 0x4UL;
732
733         rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
734         return rc ? rc : 1;
735 }
736
737 /*
738  * Handle a fault during vsie execution on a gmap shadow.
739  *
740  * Returns: - 0 if the fault was resolved
741  *          - > 0 if control has to be given to guest 2
742  *          - < 0 if an error occurred
743  */
744 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
745 {
746         int rc;
747
748         if (current->thread.gmap_int_code == PGM_PROTECTION)
749                 /* we can directly forward all protection exceptions */
750                 return inject_fault(vcpu, PGM_PROTECTION,
751                                     current->thread.gmap_addr, 1);
752
753         rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
754                                    current->thread.gmap_addr);
755         if (rc > 0) {
756                 rc = inject_fault(vcpu, rc,
757                                   current->thread.gmap_addr,
758                                   current->thread.gmap_write_flag);
759                 if (rc >= 0)
760                         vsie_page->fault_addr = current->thread.gmap_addr;
761         }
762         return rc;
763 }
764
765 /*
766  * Retry the previous fault that required guest 2 intervention. This avoids
767  * one superfluous SIE re-entry and direct exit.
768  *
769  * Will ignore any errors. The next SIE fault will do proper fault handling.
770  */
771 static void handle_last_fault(struct kvm_vcpu *vcpu,
772                               struct vsie_page *vsie_page)
773 {
774         if (vsie_page->fault_addr)
775                 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
776                                       vsie_page->fault_addr);
777         vsie_page->fault_addr = 0;
778 }
779
780 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
781 {
782         vsie_page->scb_s.icptcode = 0;
783 }
784
785 /* rewind the psw and clear the vsie icpt, so we can retry execution */
786 static void retry_vsie_icpt(struct vsie_page *vsie_page)
787 {
788         struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
789         int ilen = insn_length(scb_s->ipa >> 8);
790
791         /* take care of EXECUTE instructions */
792         if (scb_s->icptstatus & 1) {
793                 ilen = (scb_s->icptstatus >> 4) & 0x6;
794                 if (!ilen)
795                         ilen = 4;
796         }
797         scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
798         clear_vsie_icpt(vsie_page);
799 }
800
801 /*
802  * Try to shadow + enable the guest 2 provided facility list.
803  * Retry instruction execution if enabled for and provided by guest 2.
804  *
805  * Returns: - 0 if handled (retry or guest 2 icpt)
806  *          - > 0 if control has to be given to guest 2
807  */
808 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
809 {
810         struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
811         __u32 fac = READ_ONCE(vsie_page->scb_o->fac) & 0x7ffffff8U;
812
813         if (fac && test_kvm_facility(vcpu->kvm, 7)) {
814                 retry_vsie_icpt(vsie_page);
815                 if (read_guest_real(vcpu, fac, &vsie_page->fac,
816                                     sizeof(vsie_page->fac)))
817                         return set_validity_icpt(scb_s, 0x1090U);
818                 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
819         }
820         return 0;
821 }
822
823 /*
824  * Run the vsie on a shadow scb and a shadow gmap, without any further
825  * sanity checks, handling SIE faults.
826  *
827  * Returns: - 0 everything went fine
828  *          - > 0 if control has to be given to guest 2
829  *          - < 0 if an error occurred
830  */
831 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
832 {
833         struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
834         struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
835         int guest_bp_isolation;
836         int rc;
837
838         handle_last_fault(vcpu, vsie_page);
839
840         if (need_resched())
841                 schedule();
842         if (test_cpu_flag(CIF_MCCK_PENDING))
843                 s390_handle_mcck();
844
845         srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
846
847         /* save current guest state of bp isolation override */
848         guest_bp_isolation = test_thread_flag(TIF_ISOLATE_BP_GUEST);
849
850         /*
851          * The guest is running with BPBC, so we have to force it on for our
852          * nested guest. This is done by enabling BPBC globally, so the BPBC
853          * control in the SCB (which the nested guest can modify) is simply
854          * ignored.
855          */
856         if (test_kvm_facility(vcpu->kvm, 82) &&
857             vcpu->arch.sie_block->fpf & FPF_BPBC)
858                 set_thread_flag(TIF_ISOLATE_BP_GUEST);
859
860         local_irq_disable();
861         guest_enter_irqoff();
862         local_irq_enable();
863
864         rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
865
866         local_irq_disable();
867         guest_exit_irqoff();
868         local_irq_enable();
869
870         /* restore guest state for bp isolation override */
871         if (!guest_bp_isolation)
872                 clear_thread_flag(TIF_ISOLATE_BP_GUEST);
873
874         vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
875
876         if (rc == -EINTR) {
877                 VCPU_EVENT(vcpu, 3, "%s", "machine check");
878                 kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
879                 return 0;
880         }
881
882         if (rc > 0)
883                 rc = 0; /* we could still have an icpt */
884         else if (rc == -EFAULT)
885                 return handle_fault(vcpu, vsie_page);
886
887         switch (scb_s->icptcode) {
888         case ICPT_INST:
889                 if (scb_s->ipa == 0xb2b0)
890                         rc = handle_stfle(vcpu, vsie_page);
891                 break;
892         case ICPT_STOP:
893                 /* stop not requested by g2 - must have been a kick */
894                 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
895                         clear_vsie_icpt(vsie_page);
896                 break;
897         case ICPT_VALIDITY:
898                 if ((scb_s->ipa & 0xf000) != 0xf000)
899                         scb_s->ipa += 0x1000;
900                 break;
901         }
902         return rc;
903 }
904
905 static void release_gmap_shadow(struct vsie_page *vsie_page)
906 {
907         if (vsie_page->gmap)
908                 gmap_put(vsie_page->gmap);
909         WRITE_ONCE(vsie_page->gmap, NULL);
910         prefix_unmapped(vsie_page);
911 }
912
913 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
914                                struct vsie_page *vsie_page)
915 {
916         unsigned long asce;
917         union ctlreg0 cr0;
918         struct gmap *gmap;
919         int edat;
920
921         asce = vcpu->arch.sie_block->gcr[1];
922         cr0.val = vcpu->arch.sie_block->gcr[0];
923         edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
924         edat += edat && test_kvm_facility(vcpu->kvm, 78);
925
926         /*
927          * ASCE or EDAT could have changed since last icpt, or the gmap
928          * we're holding has been unshadowed. If the gmap is still valid,
929          * we can safely reuse it.
930          */
931         if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
932                 return 0;
933
934         /* release the old shadow - if any, and mark the prefix as unmapped */
935         release_gmap_shadow(vsie_page);
936         gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
937         if (IS_ERR(gmap))
938                 return PTR_ERR(gmap);
939         gmap->private = vcpu->kvm;
940         WRITE_ONCE(vsie_page->gmap, gmap);
941         return 0;
942 }
943
944 /*
945  * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
946  */
947 static void register_shadow_scb(struct kvm_vcpu *vcpu,
948                                 struct vsie_page *vsie_page)
949 {
950         struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
951
952         WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
953         /*
954          * External calls have to lead to a kick of the vcpu and
955          * therefore the vsie -> Simulate Wait state.
956          */
957         atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
958         /*
959          * We have to adjust the g3 epoch by the g2 epoch. The epoch will
960          * automatically be adjusted on tod clock changes via kvm_sync_clock.
961          */
962         preempt_disable();
963         scb_s->epoch += vcpu->kvm->arch.epoch;
964
965         if (scb_s->ecd & ECD_MEF) {
966                 scb_s->epdx += vcpu->kvm->arch.epdx;
967                 if (scb_s->epoch < vcpu->kvm->arch.epoch)
968                         scb_s->epdx += 1;
969         }
970
971         preempt_enable();
972 }
973
974 /*
975  * Unregister a shadow scb from a VCPU.
976  */
977 static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
978 {
979         atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
980         WRITE_ONCE(vcpu->arch.vsie_block, NULL);
981 }
982
983 /*
984  * Run the vsie on a shadowed scb, managing the gmap shadow, handling
985  * prefix pages and faults.
986  *
987  * Returns: - 0 if no errors occurred
988  *          - > 0 if control has to be given to guest 2
989  *          - -ENOMEM if out of memory
990  */
991 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
992 {
993         struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
994         int rc = 0;
995
996         while (1) {
997                 rc = acquire_gmap_shadow(vcpu, vsie_page);
998                 if (!rc)
999                         rc = map_prefix(vcpu, vsie_page);
1000                 if (!rc) {
1001                         gmap_enable(vsie_page->gmap);
1002                         update_intervention_requests(vsie_page);
1003                         rc = do_vsie_run(vcpu, vsie_page);
1004                         gmap_enable(vcpu->arch.gmap);
1005                 }
1006                 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
1007
1008                 if (rc == -EAGAIN)
1009                         rc = 0;
1010                 if (rc || scb_s->icptcode || signal_pending(current) ||
1011                     kvm_s390_vcpu_has_irq(vcpu, 0))
1012                         break;
1013         }
1014
1015         if (rc == -EFAULT) {
1016                 /*
1017                  * Addressing exceptions are always presentes as intercepts.
1018                  * As addressing exceptions are suppressing and our guest 3 PSW
1019                  * points at the responsible instruction, we have to
1020                  * forward the PSW and set the ilc. If we can't read guest 3
1021                  * instruction, we can use an arbitrary ilc. Let's always use
1022                  * ilen = 4 for now, so we can avoid reading in guest 3 virtual
1023                  * memory. (we could also fake the shadow so the hardware
1024                  * handles it).
1025                  */
1026                 scb_s->icptcode = ICPT_PROGI;
1027                 scb_s->iprcc = PGM_ADDRESSING;
1028                 scb_s->pgmilc = 4;
1029                 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
1030                 rc = 1;
1031         }
1032         return rc;
1033 }
1034
1035 /*
1036  * Get or create a vsie page for a scb address.
1037  *
1038  * Returns: - address of a vsie page (cached or new one)
1039  *          - NULL if the same scb address is already used by another VCPU
1040  *          - ERR_PTR(-ENOMEM) if out of memory
1041  */
1042 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
1043 {
1044         struct vsie_page *vsie_page;
1045         struct page *page;
1046         int nr_vcpus;
1047
1048         rcu_read_lock();
1049         page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1050         rcu_read_unlock();
1051         if (page) {
1052                 if (page_ref_inc_return(page) == 2)
1053                         return page_to_virt(page);
1054                 page_ref_dec(page);
1055         }
1056
1057         /*
1058          * We want at least #online_vcpus shadows, so every VCPU can execute
1059          * the VSIE in parallel.
1060          */
1061         nr_vcpus = atomic_read(&kvm->online_vcpus);
1062
1063         mutex_lock(&kvm->arch.vsie.mutex);
1064         if (kvm->arch.vsie.page_count < nr_vcpus) {
1065                 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
1066                 if (!page) {
1067                         mutex_unlock(&kvm->arch.vsie.mutex);
1068                         return ERR_PTR(-ENOMEM);
1069                 }
1070                 page_ref_inc(page);
1071                 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1072                 kvm->arch.vsie.page_count++;
1073         } else {
1074                 /* reuse an existing entry that belongs to nobody */
1075                 while (true) {
1076                         page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1077                         if (page_ref_inc_return(page) == 2)
1078                                 break;
1079                         page_ref_dec(page);
1080                         kvm->arch.vsie.next++;
1081                         kvm->arch.vsie.next %= nr_vcpus;
1082                 }
1083                 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1084         }
1085         page->index = addr;
1086         /* double use of the same address */
1087         if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1088                 page_ref_dec(page);
1089                 mutex_unlock(&kvm->arch.vsie.mutex);
1090                 return NULL;
1091         }
1092         mutex_unlock(&kvm->arch.vsie.mutex);
1093
1094         vsie_page = page_to_virt(page);
1095         memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
1096         release_gmap_shadow(vsie_page);
1097         vsie_page->fault_addr = 0;
1098         vsie_page->scb_s.ihcpu = 0xffffU;
1099         return vsie_page;
1100 }
1101
1102 /* put a vsie page acquired via get_vsie_page */
1103 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1104 {
1105         struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1106
1107         page_ref_dec(page);
1108 }
1109
1110 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1111 {
1112         struct vsie_page *vsie_page;
1113         unsigned long scb_addr;
1114         int rc;
1115
1116         vcpu->stat.instruction_sie++;
1117         if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1118                 return -EOPNOTSUPP;
1119         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1120                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1121
1122         BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
1123         scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1124
1125         /* 512 byte alignment */
1126         if (unlikely(scb_addr & 0x1ffUL))
1127                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1128
1129         if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1130                 return 0;
1131
1132         vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1133         if (IS_ERR(vsie_page))
1134                 return PTR_ERR(vsie_page);
1135         else if (!vsie_page)
1136                 /* double use of sie control block - simply do nothing */
1137                 return 0;
1138
1139         rc = pin_scb(vcpu, vsie_page, scb_addr);
1140         if (rc)
1141                 goto out_put;
1142         rc = shadow_scb(vcpu, vsie_page);
1143         if (rc)
1144                 goto out_unpin_scb;
1145         rc = pin_blocks(vcpu, vsie_page);
1146         if (rc)
1147                 goto out_unshadow;
1148         register_shadow_scb(vcpu, vsie_page);
1149         rc = vsie_run(vcpu, vsie_page);
1150         unregister_shadow_scb(vcpu);
1151         unpin_blocks(vcpu, vsie_page);
1152 out_unshadow:
1153         unshadow_scb(vcpu, vsie_page);
1154 out_unpin_scb:
1155         unpin_scb(vcpu, vsie_page, scb_addr);
1156 out_put:
1157         put_vsie_page(vcpu->kvm, vsie_page);
1158
1159         return rc < 0 ? rc : 0;
1160 }
1161
1162 /* Init the vsie data structures. To be called when a vm is initialized. */
1163 void kvm_s390_vsie_init(struct kvm *kvm)
1164 {
1165         mutex_init(&kvm->arch.vsie.mutex);
1166         INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1167 }
1168
1169 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
1170 void kvm_s390_vsie_destroy(struct kvm *kvm)
1171 {
1172         struct vsie_page *vsie_page;
1173         struct page *page;
1174         int i;
1175
1176         mutex_lock(&kvm->arch.vsie.mutex);
1177         for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1178                 page = kvm->arch.vsie.pages[i];
1179                 kvm->arch.vsie.pages[i] = NULL;
1180                 vsie_page = page_to_virt(page);
1181                 release_gmap_shadow(vsie_page);
1182                 /* free the radix tree entry */
1183                 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1184                 __free_page(page);
1185         }
1186         kvm->arch.vsie.page_count = 0;
1187         mutex_unlock(&kvm->arch.vsie.mutex);
1188 }
1189
1190 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1191 {
1192         struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1193
1194         /*
1195          * Even if the VCPU lets go of the shadow sie block reference, it is
1196          * still valid in the cache. So we can safely kick it.
1197          */
1198         if (scb) {
1199                 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1200                 if (scb->prog0c & PROG_IN_SIE)
1201                         atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);
1202         }
1203 }