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