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