blob: 6d9f4058ce157287fe8383f6d400ada073cf03a4 [file] [log] [blame]
David Hildenbranda3508fb2015-07-08 13:19:48 +02001/*
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>
David Hildenbrand66b630d2015-11-26 14:11:19 +010021#include <asm/dis.h>
David Hildenbranda3508fb2015-07-08 13:19:48 +020022#include "kvm-s390.h"
23#include "gaccess.h"
24
25struct 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 */
David Hildenbrandbbeaa582015-11-26 13:11:42 +010031 __u8 reserved[0x0700 - 0x0210]; /* 0x0210 */
32 struct kvm_s390_crypto_cb crycb; /* 0x0700 */
David Hildenbrand66b630d2015-11-26 14:11:19 +010033 __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */
David Hildenbranda3508fb2015-07-08 13:19:48 +020034} __packed;
35
36/* trigger a validity icpt for the given scb */
37static 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 */
47static 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 */
53static 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 */
63static void prefix_mapped(struct vsie_page *vsie_page)
64{
65 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
66}
67
David Hildenbrand06d68a62016-04-22 13:50:09 +020068/* test if the prefix is mapped into the gmap shadow */
69static int prefix_is_mapped(struct vsie_page *vsie_page)
70{
71 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
72}
David Hildenbranda3508fb2015-07-08 13:19:48 +020073
74/* copy the updated intervention request bits into the shadow scb */
75static 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 */
86static 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;
David Hildenbrand535ef812016-02-12 12:24:20 +0100103 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 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200110
111 atomic_set(&scb_s->cpuflags, newflags);
112 return 0;
113}
114
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100115/*
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 */
125static 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
David Hildenbrand35736022016-02-19 10:11:24 +0100167/* shadow (round up/down) the ibc to avoid validity icpt */
168static 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
David Hildenbranda3508fb2015-07-08 13:19:48 +0200187/* unshadow the scb, copying parameters back to the real scb */
188static 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 */
238static 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;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100242 bool had_tx = scb_s->ecb & 0x10U;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200243 unsigned long new_mso;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200244 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;
David Hildenbrand66b630d2015-11-26 14:11:19 +0100253 scb_s->fac = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200254
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
David Hildenbrand06d68a62016-04-22 13:50:09 +0200285 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);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200289 /* SIE will do mso/msl validity and exception checks for us */
290 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200291 scb_s->mso = new_mso;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200292 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;
David Hildenbrand4ceafa92015-11-27 12:34:28 +0100300 /* 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;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100303 /* 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 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100310 /* 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 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200315
David Hildenbrand35736022016-02-19 10:11:24 +0100316 prepare_ibc(vcpu, vsie_page);
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100317 rc = shadow_crycb(vcpu, vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200318out:
319 if (rc)
320 unshadow_scb(vcpu, vsie_page);
321 return rc;
322}
323
324void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
325 unsigned long end)
326{
327 struct kvm *kvm = gmap->private;
328 struct vsie_page *cur;
329 unsigned long prefix;
330 struct page *page;
331 int i;
332
333 if (!gmap_is_shadow(gmap))
334 return;
335 if (start >= 1UL << 31)
336 /* We are only interested in prefix pages */
337 return;
338
339 /*
340 * Only new shadow blocks are added to the list during runtime,
341 * therefore we can safely reference them all the time.
342 */
343 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
344 page = READ_ONCE(kvm->arch.vsie.pages[i]);
345 if (!page)
346 continue;
347 cur = page_to_virt(page);
348 if (READ_ONCE(cur->gmap) != gmap)
349 continue;
350 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
351 /* with mso/msl, the prefix lies at an offset */
352 prefix += cur->scb_s.mso;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100353 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
David Hildenbranda3508fb2015-07-08 13:19:48 +0200354 prefix_unmapped_sync(cur);
355 }
356}
357
358/*
David Hildenbrand166ecb32015-11-25 11:13:32 +0100359 * Map the first prefix page and if tx is enabled also the second prefix page.
David Hildenbranda3508fb2015-07-08 13:19:48 +0200360 *
361 * The prefix will be protected, a gmap notifier will inform about unmaps.
362 * The shadow scb must not be executed until the prefix is remapped, this is
363 * guaranteed by properly handling PROG_REQUEST.
364 *
365 * Returns: - 0 on if successfully mapped or already mapped
366 * - > 0 if control has to be given to guest 2
367 * - -EAGAIN if the caller can retry immediately
368 * - -ENOMEM if out of memory
369 */
370static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
371{
372 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
373 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
374 int rc;
375
David Hildenbrand06d68a62016-04-22 13:50:09 +0200376 if (prefix_is_mapped(vsie_page))
377 return 0;
378
David Hildenbranda3508fb2015-07-08 13:19:48 +0200379 /* mark it as mapped so we can catch any concurrent unmappers */
380 prefix_mapped(vsie_page);
381
382 /* with mso/msl, the prefix lies at offset *mso* */
383 prefix += scb_s->mso;
384
385 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
David Hildenbrand166ecb32015-11-25 11:13:32 +0100386 if (!rc && (scb_s->ecb & 0x10U))
387 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
388 prefix + PAGE_SIZE);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200389 /*
390 * We don't have to mprotect, we will be called for all unshadows.
391 * SIE will detect if protection applies and trigger a validity.
392 */
393 if (rc)
394 prefix_unmapped(vsie_page);
395 if (rc > 0 || rc == -EFAULT)
396 rc = set_validity_icpt(scb_s, 0x0037U);
397 return rc;
398}
399
400/*
401 * Pin the guest page given by gpa and set hpa to the pinned host address.
402 * Will always be pinned writable.
403 *
404 * Returns: - 0 on success
405 * - -EINVAL if the gpa is not valid guest storage
406 * - -ENOMEM if out of memory
407 */
408static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
409{
410 struct page *page;
411 hva_t hva;
412 int rc;
413
414 hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
415 if (kvm_is_error_hva(hva))
416 return -EINVAL;
417 rc = get_user_pages_fast(hva, 1, 1, &page);
418 if (rc < 0)
419 return rc;
420 else if (rc != 1)
421 return -ENOMEM;
422 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
423 return 0;
424}
425
426/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
427static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
428{
429 struct page *page;
430
431 page = virt_to_page(hpa);
432 set_page_dirty_lock(page);
433 put_page(page);
434 /* mark the page always as dirty for migration */
435 mark_page_dirty(kvm, gpa_to_gfn(gpa));
436}
437
438/* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
439static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
440{
441 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
442 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
443 hpa_t hpa;
444 gpa_t gpa;
445
446 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
447 if (hpa) {
448 gpa = scb_o->scaol & ~0xfUL;
449 unpin_guest_page(vcpu->kvm, gpa, hpa);
450 scb_s->scaol = 0;
451 scb_s->scaoh = 0;
452 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100453
454 hpa = scb_s->itdba;
455 if (hpa) {
456 gpa = scb_o->itdba & ~0xffUL;
457 unpin_guest_page(vcpu->kvm, gpa, hpa);
458 scb_s->itdba = 0;
459 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100460
461 hpa = scb_s->gvrd;
462 if (hpa) {
463 gpa = scb_o->gvrd & ~0x1ffUL;
464 unpin_guest_page(vcpu->kvm, gpa, hpa);
465 scb_s->gvrd = 0;
466 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200467}
468
469/*
470 * Instead of shadowing some blocks, we can simply forward them because the
471 * addresses in the scb are 64 bit long.
472 *
473 * This works as long as the data lies in one page. If blocks ever exceed one
474 * page, we have to fall back to shadowing.
475 *
476 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
477 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
478 *
479 * Returns: - 0 if all blocks were pinned.
480 * - > 0 if control has to be given to guest 2
481 * - -ENOMEM if out of memory
482 */
483static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
484{
485 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
486 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
487 hpa_t hpa;
488 gpa_t gpa;
489 int rc = 0;
490
491 gpa = scb_o->scaol & ~0xfUL;
492 if (gpa) {
493 if (!(gpa & ~0x1fffUL))
494 rc = set_validity_icpt(scb_s, 0x0038U);
495 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
496 rc = set_validity_icpt(scb_s, 0x0011U);
497 else if ((gpa & PAGE_MASK) !=
498 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
499 rc = set_validity_icpt(scb_s, 0x003bU);
500 if (!rc) {
501 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
502 if (rc == -EINVAL)
503 rc = set_validity_icpt(scb_s, 0x0034U);
504 }
505 if (rc)
506 goto unpin;
507 scb_s->scaoh = (u32)((u64)hpa >> 32);
508 scb_s->scaol = (u32)(u64)hpa;
509 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100510
511 gpa = scb_o->itdba & ~0xffUL;
512 if (gpa && (scb_s->ecb & 0x10U)) {
513 if (!(gpa & ~0x1fffU)) {
514 rc = set_validity_icpt(scb_s, 0x0080U);
515 goto unpin;
516 }
517 /* 256 bytes cannot cross page boundaries */
518 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
519 if (rc == -EINVAL)
520 rc = set_validity_icpt(scb_s, 0x0080U);
521 if (rc)
522 goto unpin;
523 scb_s->itdba = hpa;
524 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100525
526 gpa = scb_o->gvrd & ~0x1ffUL;
527 if (gpa && (scb_s->eca & 0x00020000U) &&
528 !(scb_s->ecd & 0x20000000U)) {
529 if (!(gpa & ~0x1fffUL)) {
530 rc = set_validity_icpt(scb_s, 0x1310U);
531 goto unpin;
532 }
533 /*
534 * 512 bytes vector registers cannot cross page boundaries
535 * if this block gets bigger, we have to shadow it.
536 */
537 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
538 if (rc == -EINVAL)
539 rc = set_validity_icpt(scb_s, 0x1310U);
540 if (rc)
541 goto unpin;
542 scb_s->gvrd = hpa;
543 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200544 return 0;
545unpin:
546 unpin_blocks(vcpu, vsie_page);
547 return rc;
548}
549
550/* unpin the scb provided by guest 2, marking it as dirty */
551static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
552 gpa_t gpa)
553{
554 hpa_t hpa = (hpa_t) vsie_page->scb_o;
555
556 if (hpa)
557 unpin_guest_page(vcpu->kvm, gpa, hpa);
558 vsie_page->scb_o = NULL;
559}
560
561/*
562 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
563 *
564 * Returns: - 0 if the scb was pinned.
565 * - > 0 if control has to be given to guest 2
566 * - -ENOMEM if out of memory
567 */
568static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
569 gpa_t gpa)
570{
571 hpa_t hpa;
572 int rc;
573
574 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
575 if (rc == -EINVAL) {
576 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
577 if (!rc)
578 rc = 1;
579 }
580 if (!rc)
581 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
582 return rc;
583}
584
585/*
586 * Inject a fault into guest 2.
587 *
588 * Returns: - > 0 if control has to be given to guest 2
589 * < 0 if an error occurred during injection.
590 */
591static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
592 bool write_flag)
593{
594 struct kvm_s390_pgm_info pgm = {
595 .code = code,
596 .trans_exc_code =
597 /* 0-51: virtual address */
598 (vaddr & 0xfffffffffffff000UL) |
599 /* 52-53: store / fetch */
600 (((unsigned int) !write_flag) + 1) << 10,
601 /* 62-63: asce id (alway primary == 0) */
602 .exc_access_id = 0, /* always primary */
603 .op_access_id = 0, /* not MVPG */
604 };
605 int rc;
606
607 if (code == PGM_PROTECTION)
608 pgm.trans_exc_code |= 0x4UL;
609
610 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
611 return rc ? rc : 1;
612}
613
614/*
615 * Handle a fault during vsie execution on a gmap shadow.
616 *
617 * Returns: - 0 if the fault was resolved
618 * - > 0 if control has to be given to guest 2
619 * - < 0 if an error occurred
620 */
621static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
622{
623 int rc;
624
625 if (current->thread.gmap_int_code == PGM_PROTECTION)
626 /* we can directly forward all protection exceptions */
627 return inject_fault(vcpu, PGM_PROTECTION,
628 current->thread.gmap_addr, 1);
629
630 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
631 current->thread.gmap_addr);
632 if (rc > 0) {
633 rc = inject_fault(vcpu, rc,
634 current->thread.gmap_addr,
635 current->thread.gmap_write_flag);
636 }
637 return rc;
638}
639
640static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
641{
642 vsie_page->scb_s.icptcode = 0;
643}
644
David Hildenbrand66b630d2015-11-26 14:11:19 +0100645/* rewind the psw and clear the vsie icpt, so we can retry execution */
646static void retry_vsie_icpt(struct vsie_page *vsie_page)
647{
648 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
649 int ilen = insn_length(scb_s->ipa >> 8);
650
651 /* take care of EXECUTE instructions */
652 if (scb_s->icptstatus & 1) {
653 ilen = (scb_s->icptstatus >> 4) & 0x6;
654 if (!ilen)
655 ilen = 4;
656 }
657 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
658 clear_vsie_icpt(vsie_page);
659}
660
661/*
662 * Try to shadow + enable the guest 2 provided facility list.
663 * Retry instruction execution if enabled for and provided by guest 2.
664 *
665 * Returns: - 0 if handled (retry or guest 2 icpt)
666 * - > 0 if control has to be given to guest 2
667 */
668static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
669{
670 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
671 __u32 fac = vsie_page->scb_o->fac & 0x7ffffff8U;
672
673 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
674 retry_vsie_icpt(vsie_page);
675 if (read_guest_real(vcpu, fac, &vsie_page->fac,
676 sizeof(vsie_page->fac)))
677 return set_validity_icpt(scb_s, 0x1090U);
678 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
679 }
680 return 0;
681}
682
David Hildenbranda3508fb2015-07-08 13:19:48 +0200683/*
684 * Run the vsie on a shadow scb and a shadow gmap, without any further
685 * sanity checks, handling SIE faults.
686 *
687 * Returns: - 0 everything went fine
688 * - > 0 if control has to be given to guest 2
689 * - < 0 if an error occurred
690 */
691static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
692{
693 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
694 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
695 int rc;
696
697 if (need_resched())
698 schedule();
699 if (test_cpu_flag(CIF_MCCK_PENDING))
700 s390_handle_mcck();
701
702 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
703 local_irq_disable();
704 kvm_guest_enter();
705 local_irq_enable();
706
707 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
708
709 local_irq_disable();
710 kvm_guest_exit();
711 local_irq_enable();
712 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
713
714 if (rc > 0)
715 rc = 0; /* we could still have an icpt */
716 else if (rc == -EFAULT)
717 return handle_fault(vcpu, vsie_page);
718
719 switch (scb_s->icptcode) {
David Hildenbrand66b630d2015-11-26 14:11:19 +0100720 case ICPT_INST:
721 if (scb_s->ipa == 0xb2b0)
722 rc = handle_stfle(vcpu, vsie_page);
723 break;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200724 case ICPT_STOP:
725 /* stop not requested by g2 - must have been a kick */
726 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
727 clear_vsie_icpt(vsie_page);
728 break;
729 case ICPT_VALIDITY:
730 if ((scb_s->ipa & 0xf000) != 0xf000)
731 scb_s->ipa += 0x1000;
732 break;
733 }
734 return rc;
735}
736
737static void release_gmap_shadow(struct vsie_page *vsie_page)
738{
739 if (vsie_page->gmap)
740 gmap_put(vsie_page->gmap);
741 WRITE_ONCE(vsie_page->gmap, NULL);
David Hildenbrand06d68a62016-04-22 13:50:09 +0200742 prefix_unmapped(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200743}
744
745static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
746 struct vsie_page *vsie_page)
747{
748 unsigned long asce;
749 union ctlreg0 cr0;
750 struct gmap *gmap;
751 int edat;
752
753 asce = vcpu->arch.sie_block->gcr[1];
754 cr0.val = vcpu->arch.sie_block->gcr[0];
755 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
756 edat += edat && test_kvm_facility(vcpu->kvm, 78);
757
David Hildenbrand06d68a62016-04-22 13:50:09 +0200758 /*
759 * ASCE or EDAT could have changed since last icpt, or the gmap
760 * we're holding has been unshadowed. If the gmap is still valid,
761 * we can safely reuse it.
762 */
763 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
764 return 0;
765
766 /* release the old shadow - if any, and mark the prefix as unmapped */
767 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200768 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
769 if (IS_ERR(gmap))
770 return PTR_ERR(gmap);
771 gmap->private = vcpu->kvm;
772 WRITE_ONCE(vsie_page->gmap, gmap);
773 return 0;
774}
775
776/*
777 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
778 * prefix pages and faults.
779 *
780 * Returns: - 0 if no errors occurred
781 * - > 0 if control has to be given to guest 2
782 * - -ENOMEM if out of memory
783 */
784static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
785{
786 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
787 int rc = 0;
788
789 while (1) {
790 rc = acquire_gmap_shadow(vcpu, vsie_page);
791 if (!rc)
792 rc = map_prefix(vcpu, vsie_page);
793 if (!rc) {
794 gmap_enable(vsie_page->gmap);
795 update_intervention_requests(vsie_page);
796 rc = do_vsie_run(vcpu, vsie_page);
797 gmap_enable(vcpu->arch.gmap);
798 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200799
800 if (rc == -EAGAIN)
801 rc = 0;
802 if (rc || scb_s->icptcode || signal_pending(current) ||
803 kvm_s390_vcpu_has_irq(vcpu, 0))
804 break;
805 };
806
807 if (rc == -EFAULT) {
808 /*
809 * Addressing exceptions are always presentes as intercepts.
810 * As addressing exceptions are suppressing and our guest 3 PSW
811 * points at the responsible instruction, we have to
812 * forward the PSW and set the ilc. If we can't read guest 3
813 * instruction, we can use an arbitrary ilc. Let's always use
814 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
815 * memory. (we could also fake the shadow so the hardware
816 * handles it).
817 */
818 scb_s->icptcode = ICPT_PROGI;
819 scb_s->iprcc = PGM_ADDRESSING;
820 scb_s->pgmilc = 4;
821 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
822 }
823 return rc;
824}
825
826/*
827 * Get or create a vsie page for a scb address.
828 *
829 * Returns: - address of a vsie page (cached or new one)
830 * - NULL if the same scb address is already used by another VCPU
831 * - ERR_PTR(-ENOMEM) if out of memory
832 */
833static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
834{
835 struct vsie_page *vsie_page;
836 struct page *page;
837 int nr_vcpus;
838
839 rcu_read_lock();
840 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
841 rcu_read_unlock();
842 if (page) {
843 if (page_ref_inc_return(page) == 2)
844 return page_to_virt(page);
845 page_ref_dec(page);
846 }
847
848 /*
849 * We want at least #online_vcpus shadows, so every VCPU can execute
850 * the VSIE in parallel.
851 */
852 nr_vcpus = atomic_read(&kvm->online_vcpus);
853
854 mutex_lock(&kvm->arch.vsie.mutex);
855 if (kvm->arch.vsie.page_count < nr_vcpus) {
David Hildenbrand66b630d2015-11-26 14:11:19 +0100856 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200857 if (!page) {
858 mutex_unlock(&kvm->arch.vsie.mutex);
859 return ERR_PTR(-ENOMEM);
860 }
861 page_ref_inc(page);
862 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
863 kvm->arch.vsie.page_count++;
864 } else {
865 /* reuse an existing entry that belongs to nobody */
866 while (true) {
867 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
868 if (page_ref_inc_return(page) == 2)
869 break;
870 page_ref_dec(page);
871 kvm->arch.vsie.next++;
872 kvm->arch.vsie.next %= nr_vcpus;
873 }
874 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
875 }
876 page->index = addr;
877 /* double use of the same address */
878 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
879 page_ref_dec(page);
880 mutex_unlock(&kvm->arch.vsie.mutex);
881 return NULL;
882 }
883 mutex_unlock(&kvm->arch.vsie.mutex);
884
885 vsie_page = page_to_virt(page);
886 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
David Hildenbrand06d68a62016-04-22 13:50:09 +0200887 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200888 vsie_page->scb_s.ihcpu = 0xffffU;
889 return vsie_page;
890}
891
892/* put a vsie page acquired via get_vsie_page */
893static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
894{
895 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
896
897 page_ref_dec(page);
898}
899
900int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
901{
902 struct vsie_page *vsie_page;
903 unsigned long scb_addr;
904 int rc;
905
906 vcpu->stat.instruction_sie++;
907 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
908 return -EOPNOTSUPP;
909 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
910 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
911
912 BUILD_BUG_ON(sizeof(struct vsie_page) != 4096);
913 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
914
915 /* 512 byte alignment */
916 if (unlikely(scb_addr & 0x1ffUL))
917 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
918
919 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
920 return 0;
921
922 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
923 if (IS_ERR(vsie_page))
924 return PTR_ERR(vsie_page);
925 else if (!vsie_page)
926 /* double use of sie control block - simply do nothing */
927 return 0;
928
929 rc = pin_scb(vcpu, vsie_page, scb_addr);
930 if (rc)
931 goto out_put;
932 rc = shadow_scb(vcpu, vsie_page);
933 if (rc)
934 goto out_unpin_scb;
935 rc = pin_blocks(vcpu, vsie_page);
936 if (rc)
937 goto out_unshadow;
938 rc = vsie_run(vcpu, vsie_page);
939 unpin_blocks(vcpu, vsie_page);
940out_unshadow:
941 unshadow_scb(vcpu, vsie_page);
942out_unpin_scb:
943 unpin_scb(vcpu, vsie_page, scb_addr);
944out_put:
945 put_vsie_page(vcpu->kvm, vsie_page);
946
947 return rc < 0 ? rc : 0;
948}
949
950/* Init the vsie data structures. To be called when a vm is initialized. */
951void kvm_s390_vsie_init(struct kvm *kvm)
952{
953 mutex_init(&kvm->arch.vsie.mutex);
954 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
955}
956
957/* Destroy the vsie data structures. To be called when a vm is destroyed. */
958void kvm_s390_vsie_destroy(struct kvm *kvm)
959{
David Hildenbrand06d68a62016-04-22 13:50:09 +0200960 struct vsie_page *vsie_page;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200961 struct page *page;
962 int i;
963
964 mutex_lock(&kvm->arch.vsie.mutex);
965 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
966 page = kvm->arch.vsie.pages[i];
967 kvm->arch.vsie.pages[i] = NULL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200968 vsie_page = page_to_virt(page);
969 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200970 /* free the radix tree entry */
971 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
972 __free_page(page);
973 }
974 kvm->arch.vsie.page_count = 0;
975 mutex_unlock(&kvm->arch.vsie.mutex);
976}