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