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