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