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