blob: d2b7886da4440510d1d51204c364c26f9e6201ca [file] [log] [blame]
Greg Kroah-Hartmand809aa22017-11-24 15:00:33 +01001// SPDX-License-Identifier: GPL-2.0
David Hildenbranda3508fb2015-07-08 13:19:48 +02002/*
3 * kvm nested virtualization support for s390x
4 *
5 * Copyright IBM Corp. 2016
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License (version 2 only)
9 * as published by the Free Software Foundation.
10 *
11 * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
12 */
13#include <linux/vmalloc.h>
14#include <linux/kvm_host.h>
15#include <linux/bug.h>
16#include <linux/list.h>
17#include <linux/bitmap.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010018#include <linux/sched/signal.h>
19
David Hildenbranda3508fb2015-07-08 13:19:48 +020020#include <asm/gmap.h>
21#include <asm/mmu_context.h>
22#include <asm/sclp.h>
23#include <asm/nmi.h>
David Hildenbrand66b630d2015-11-26 14:11:19 +010024#include <asm/dis.h>
David Hildenbranda3508fb2015-07-08 13:19:48 +020025#include "kvm-s390.h"
26#include "gaccess.h"
27
28struct vsie_page {
29 struct kvm_s390_sie_block scb_s; /* 0x0000 */
QingFeng Haod52cd202017-06-07 12:11:18 +020030 /*
31 * the backup info for machine check. ensure it's at
32 * the same offset as that in struct sie_page!
33 */
34 struct mcck_volatile_info mcck_info; /* 0x0200 */
David Hildenbranda3508fb2015-07-08 13:19:48 +020035 /* the pinned originial scb */
QingFeng Haod52cd202017-06-07 12:11:18 +020036 struct kvm_s390_sie_block *scb_o; /* 0x0218 */
David Hildenbranda3508fb2015-07-08 13:19:48 +020037 /* the shadow gmap in use by the vsie_page */
QingFeng Haod52cd202017-06-07 12:11:18 +020038 struct gmap *gmap; /* 0x0220 */
David Hildenbrand1b7029b2015-07-08 13:25:31 +020039 /* address of the last reported fault to guest2 */
QingFeng Haod52cd202017-06-07 12:11:18 +020040 unsigned long fault_addr; /* 0x0228 */
41 __u8 reserved[0x0700 - 0x0230]; /* 0x0230 */
David Hildenbrandbbeaa582015-11-26 13:11:42 +010042 struct kvm_s390_crypto_cb crycb; /* 0x0700 */
David Hildenbrand66b630d2015-11-26 14:11:19 +010043 __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */
Martin Schwidefsky1cae0252017-06-21 16:49:15 +020044};
David Hildenbranda3508fb2015-07-08 13:19:48 +020045
46/* trigger a validity icpt for the given scb */
47static int set_validity_icpt(struct kvm_s390_sie_block *scb,
48 __u16 reason_code)
49{
50 scb->ipa = 0x1000;
51 scb->ipb = ((__u32) reason_code) << 16;
52 scb->icptcode = ICPT_VALIDITY;
53 return 1;
54}
55
56/* mark the prefix as unmapped, this will block the VSIE */
57static void prefix_unmapped(struct vsie_page *vsie_page)
58{
59 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
60}
61
62/* mark the prefix as unmapped and wait until the VSIE has been left */
63static void prefix_unmapped_sync(struct vsie_page *vsie_page)
64{
65 prefix_unmapped(vsie_page);
66 if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
67 atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
68 while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
69 cpu_relax();
70}
71
72/* mark the prefix as mapped, this will allow the VSIE to run */
73static void prefix_mapped(struct vsie_page *vsie_page)
74{
75 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
76}
77
David Hildenbrand06d68a62016-04-22 13:50:09 +020078/* test if the prefix is mapped into the gmap shadow */
79static int prefix_is_mapped(struct vsie_page *vsie_page)
80{
81 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
82}
David Hildenbranda3508fb2015-07-08 13:19:48 +020083
84/* copy the updated intervention request bits into the shadow scb */
85static void update_intervention_requests(struct vsie_page *vsie_page)
86{
87 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
88 int cpuflags;
89
90 cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
91 atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
92 atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
93}
94
95/* shadow (filter and validate) the cpuflags */
96static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
97{
98 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
99 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
100 int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
101
102 /* we don't allow ESA/390 guests */
103 if (!(cpuflags & CPUSTAT_ZARCH))
104 return set_validity_icpt(scb_s, 0x0001U);
105
106 if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
107 return set_validity_icpt(scb_s, 0x0001U);
108 else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
109 return set_validity_icpt(scb_s, 0x0007U);
110
111 /* intervention requests will be set later */
112 newflags = CPUSTAT_ZARCH;
David Hildenbrand535ef812016-02-12 12:24:20 +0100113 if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8))
114 newflags |= CPUSTAT_GED;
115 if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) {
116 if (cpuflags & CPUSTAT_GED)
117 return set_validity_icpt(scb_s, 0x0001U);
118 newflags |= CPUSTAT_GED2;
119 }
David Hildenbrand77d18f62015-11-24 16:32:35 +0100120 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE))
121 newflags |= cpuflags & CPUSTAT_P;
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100122 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS))
123 newflags |= cpuflags & CPUSTAT_SM;
David Hildenbrand7fd7f392015-11-24 16:56:23 +0100124 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS))
125 newflags |= cpuflags & CPUSTAT_IBS;
Farhan Ali730cd632017-02-24 16:12:56 -0500126 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_KSS))
127 newflags |= cpuflags & CPUSTAT_KSS;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200128
129 atomic_set(&scb_s->cpuflags, newflags);
130 return 0;
131}
132
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100133/*
134 * Create a shadow copy of the crycb block and setup key wrapping, if
135 * requested for guest 3 and enabled for guest 2.
136 *
137 * We only accept format-1 (no AP in g2), but convert it into format-2
138 * There is nothing to do for format-0.
139 *
140 * Returns: - 0 if shadowed or nothing to do
141 * - > 0 if control has to be given to guest 2
142 */
143static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
144{
145 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
146 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
147 u32 crycb_addr = scb_o->crycbd & 0x7ffffff8U;
148 unsigned long *b1, *b2;
149 u8 ecb3_flags;
150
151 scb_s->crycbd = 0;
152 if (!(scb_o->crycbd & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
153 return 0;
154 /* format-1 is supported with message-security-assist extension 3 */
155 if (!test_kvm_facility(vcpu->kvm, 76))
156 return 0;
157 /* we may only allow it if enabled for guest 2 */
158 ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
159 (ECB3_AES | ECB3_DEA);
160 if (!ecb3_flags)
161 return 0;
162
163 if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
164 return set_validity_icpt(scb_s, 0x003CU);
165 else if (!crycb_addr)
166 return set_validity_icpt(scb_s, 0x0039U);
167
168 /* copy only the wrapping keys */
169 if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
170 return set_validity_icpt(scb_s, 0x0035U);
171
172 scb_s->ecb3 |= ecb3_flags;
173 scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
174 CRYCB_FORMAT2;
175
176 /* xor both blocks in one run */
177 b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
178 b2 = (unsigned long *)
179 vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
180 /* as 56%8 == 0, bitmap_xor won't overwrite any data */
181 bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
182 return 0;
183}
184
David Hildenbrand35736022016-02-19 10:11:24 +0100185/* shadow (round up/down) the ibc to avoid validity icpt */
186static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
187{
188 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
189 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
190 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
191
192 scb_s->ibc = 0;
193 /* ibc installed in g2 and requested for g3 */
194 if (vcpu->kvm->arch.model.ibc && (scb_o->ibc & 0x0fffU)) {
195 scb_s->ibc = scb_o->ibc & 0x0fffU;
196 /* takte care of the minimum ibc level of the machine */
197 if (scb_s->ibc < min_ibc)
198 scb_s->ibc = min_ibc;
199 /* take care of the maximum ibc level set for the guest */
200 if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
201 scb_s->ibc = vcpu->kvm->arch.model.ibc;
202 }
203}
204
David Hildenbranda3508fb2015-07-08 13:19:48 +0200205/* unshadow the scb, copying parameters back to the real scb */
206static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
207{
208 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
209 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
210
211 /* interception */
212 scb_o->icptcode = scb_s->icptcode;
213 scb_o->icptstatus = scb_s->icptstatus;
214 scb_o->ipa = scb_s->ipa;
215 scb_o->ipb = scb_s->ipb;
216 scb_o->gbea = scb_s->gbea;
217
218 /* timer */
219 scb_o->cputm = scb_s->cputm;
220 scb_o->ckc = scb_s->ckc;
221 scb_o->todpr = scb_s->todpr;
222
223 /* guest state */
224 scb_o->gpsw = scb_s->gpsw;
225 scb_o->gg14 = scb_s->gg14;
226 scb_o->gg15 = scb_s->gg15;
227 memcpy(scb_o->gcr, scb_s->gcr, 128);
228 scb_o->pp = scb_s->pp;
229
230 /* interrupt intercept */
231 switch (scb_s->icptcode) {
232 case ICPT_PROGI:
233 case ICPT_INSTPROGI:
234 case ICPT_EXTINT:
235 memcpy((void *)((u64)scb_o + 0xc0),
236 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
237 break;
238 case ICPT_PARTEXEC:
239 /* MVPG only */
240 memcpy((void *)((u64)scb_o + 0xc0),
241 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
242 break;
243 }
244
245 if (scb_s->ihcpu != 0xffffU)
246 scb_o->ihcpu = scb_s->ihcpu;
247}
248
249/*
250 * Setup the shadow scb by copying and checking the relevant parts of the g2
251 * provided scb.
252 *
253 * Returns: - 0 if the scb has been shadowed
254 * - > 0 if control has to be given to guest 2
255 */
256static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
257{
258 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
259 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100260 bool had_tx = scb_s->ecb & ECB_TE;
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100261 unsigned long new_mso = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200262 int rc;
263
264 /* make sure we don't have any leftovers when reusing the scb */
265 scb_s->icptcode = 0;
266 scb_s->eca = 0;
267 scb_s->ecb = 0;
268 scb_s->ecb2 = 0;
269 scb_s->ecb3 = 0;
270 scb_s->ecd = 0;
David Hildenbrand66b630d2015-11-26 14:11:19 +0100271 scb_s->fac = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200272
273 rc = prepare_cpuflags(vcpu, vsie_page);
274 if (rc)
275 goto out;
276
277 /* timer */
278 scb_s->cputm = scb_o->cputm;
279 scb_s->ckc = scb_o->ckc;
280 scb_s->todpr = scb_o->todpr;
281 scb_s->epoch = scb_o->epoch;
282
283 /* guest state */
284 scb_s->gpsw = scb_o->gpsw;
285 scb_s->gg14 = scb_o->gg14;
286 scb_s->gg15 = scb_o->gg15;
287 memcpy(scb_s->gcr, scb_o->gcr, 128);
288 scb_s->pp = scb_o->pp;
289
290 /* interception / execution handling */
291 scb_s->gbea = scb_o->gbea;
292 scb_s->lctl = scb_o->lctl;
293 scb_s->svcc = scb_o->svcc;
294 scb_s->ictl = scb_o->ictl;
295 /*
296 * SKEY handling functions can't deal with false setting of PTE invalid
297 * bits. Therefore we cannot provide interpretation and would later
298 * have to provide own emulation handlers.
299 */
Farhan Ali730cd632017-02-24 16:12:56 -0500300 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
301 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
302
David Hildenbranda3508fb2015-07-08 13:19:48 +0200303 scb_s->icpua = scb_o->icpua;
304
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100305 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
306 new_mso = scb_o->mso & 0xfffffffffff00000UL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200307 /* if the hva of the prefix changes, we have to remap the prefix */
308 if (scb_s->mso != new_mso || scb_s->prefix != scb_o->prefix)
309 prefix_unmapped(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200310 /* SIE will do mso/msl validity and exception checks for us */
311 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200312 scb_s->mso = new_mso;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200313 scb_s->prefix = scb_o->prefix;
314
315 /* We have to definetly flush the tlb if this scb never ran */
316 if (scb_s->ihcpu != 0xffffU)
317 scb_s->ihcpu = scb_o->ihcpu;
318
319 /* MVPG and Protection Exception Interpretation are always available */
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100320 scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
David Hildenbrand4ceafa92015-11-27 12:34:28 +0100321 /* Host-protection-interruption introduced with ESOP */
322 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100323 scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100324 /* transactional execution */
325 if (test_kvm_facility(vcpu->kvm, 73)) {
326 /* remap the prefix is tx is toggled on */
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100327 if ((scb_o->ecb & ECB_TE) && !had_tx)
David Hildenbrand166ecb32015-11-25 11:13:32 +0100328 prefix_unmapped(vsie_page);
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100329 scb_s->ecb |= scb_o->ecb & ECB_TE;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100330 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100331 /* SIMD */
332 if (test_kvm_facility(vcpu->kvm, 129)) {
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100333 scb_s->eca |= scb_o->eca & ECA_VX;
334 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100335 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100336 /* Run-time-Instrumentation */
337 if (test_kvm_facility(vcpu->kvm, 64))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100338 scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
Janosch Frankcd1836f2016-08-04 09:57:36 +0200339 /* Instruction Execution Prevention */
340 if (test_kvm_facility(vcpu->kvm, 130))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100341 scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100342 /* Guarded Storage */
343 if (test_kvm_facility(vcpu->kvm, 133)) {
344 scb_s->ecb |= scb_o->ecb & ECB_GS;
345 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
346 }
David Hildenbrand0615a322015-11-25 09:59:49 +0100347 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100348 scb_s->eca |= scb_o->eca & ECA_SII;
David Hildenbrand5630a8e2015-11-24 16:53:51 +0100349 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100350 scb_s->eca |= scb_o->eca & ECA_IB;
David Hildenbrand13ee3f62015-11-24 16:54:37 +0100351 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100352 scb_s->eca |= scb_o->eca & ECA_CEI;
Collin L. Walling8fa16962016-07-26 15:29:44 -0400353 /* Epoch Extension */
354 if (test_kvm_facility(vcpu->kvm, 139))
355 scb_s->ecd |= scb_o->ecd & ECD_MEF;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200356
David Hildenbrand35736022016-02-19 10:11:24 +0100357 prepare_ibc(vcpu, vsie_page);
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100358 rc = shadow_crycb(vcpu, vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200359out:
360 if (rc)
361 unshadow_scb(vcpu, vsie_page);
362 return rc;
363}
364
365void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
366 unsigned long end)
367{
368 struct kvm *kvm = gmap->private;
369 struct vsie_page *cur;
370 unsigned long prefix;
371 struct page *page;
372 int i;
373
374 if (!gmap_is_shadow(gmap))
375 return;
376 if (start >= 1UL << 31)
377 /* We are only interested in prefix pages */
378 return;
379
380 /*
381 * Only new shadow blocks are added to the list during runtime,
382 * therefore we can safely reference them all the time.
383 */
384 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
385 page = READ_ONCE(kvm->arch.vsie.pages[i]);
386 if (!page)
387 continue;
388 cur = page_to_virt(page);
389 if (READ_ONCE(cur->gmap) != gmap)
390 continue;
391 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
392 /* with mso/msl, the prefix lies at an offset */
393 prefix += cur->scb_s.mso;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100394 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
David Hildenbranda3508fb2015-07-08 13:19:48 +0200395 prefix_unmapped_sync(cur);
396 }
397}
398
399/*
David Hildenbrand166ecb32015-11-25 11:13:32 +0100400 * Map the first prefix page and if tx is enabled also the second prefix page.
David Hildenbranda3508fb2015-07-08 13:19:48 +0200401 *
402 * The prefix will be protected, a gmap notifier will inform about unmaps.
403 * The shadow scb must not be executed until the prefix is remapped, this is
404 * guaranteed by properly handling PROG_REQUEST.
405 *
406 * Returns: - 0 on if successfully mapped or already mapped
407 * - > 0 if control has to be given to guest 2
408 * - -EAGAIN if the caller can retry immediately
409 * - -ENOMEM if out of memory
410 */
411static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
412{
413 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
414 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
415 int rc;
416
David Hildenbrand06d68a62016-04-22 13:50:09 +0200417 if (prefix_is_mapped(vsie_page))
418 return 0;
419
David Hildenbranda3508fb2015-07-08 13:19:48 +0200420 /* mark it as mapped so we can catch any concurrent unmappers */
421 prefix_mapped(vsie_page);
422
423 /* with mso/msl, the prefix lies at offset *mso* */
424 prefix += scb_s->mso;
425
426 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100427 if (!rc && (scb_s->ecb & ECB_TE))
David Hildenbrand166ecb32015-11-25 11:13:32 +0100428 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
429 prefix + PAGE_SIZE);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200430 /*
431 * We don't have to mprotect, we will be called for all unshadows.
432 * SIE will detect if protection applies and trigger a validity.
433 */
434 if (rc)
435 prefix_unmapped(vsie_page);
436 if (rc > 0 || rc == -EFAULT)
437 rc = set_validity_icpt(scb_s, 0x0037U);
438 return rc;
439}
440
441/*
442 * Pin the guest page given by gpa and set hpa to the pinned host address.
443 * Will always be pinned writable.
444 *
445 * Returns: - 0 on success
446 * - -EINVAL if the gpa is not valid guest storage
David Hildenbranda3508fb2015-07-08 13:19:48 +0200447 */
448static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
449{
450 struct page *page;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200451
David Hildenbrandf7a65092017-09-01 17:11:43 +0200452 page = gfn_to_page(kvm, gpa_to_gfn(gpa));
453 if (is_error_page(page))
David Hildenbranda3508fb2015-07-08 13:19:48 +0200454 return -EINVAL;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200455 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
456 return 0;
457}
458
459/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
460static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
461{
David Hildenbrandf7a65092017-09-01 17:11:43 +0200462 kvm_release_pfn_dirty(hpa >> PAGE_SHIFT);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200463 /* mark the page always as dirty for migration */
464 mark_page_dirty(kvm, gpa_to_gfn(gpa));
465}
466
467/* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
468static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
469{
470 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
471 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
472 hpa_t hpa;
473 gpa_t gpa;
474
475 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
476 if (hpa) {
477 gpa = scb_o->scaol & ~0xfUL;
David Hildenbrand19c439b2015-11-25 11:02:26 +0100478 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
479 gpa |= (u64) scb_o->scaoh << 32;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200480 unpin_guest_page(vcpu->kvm, gpa, hpa);
481 scb_s->scaol = 0;
482 scb_s->scaoh = 0;
483 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100484
485 hpa = scb_s->itdba;
486 if (hpa) {
487 gpa = scb_o->itdba & ~0xffUL;
488 unpin_guest_page(vcpu->kvm, gpa, hpa);
489 scb_s->itdba = 0;
490 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100491
492 hpa = scb_s->gvrd;
493 if (hpa) {
494 gpa = scb_o->gvrd & ~0x1ffUL;
495 unpin_guest_page(vcpu->kvm, gpa, hpa);
496 scb_s->gvrd = 0;
497 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100498
499 hpa = scb_s->riccbd;
500 if (hpa) {
501 gpa = scb_o->riccbd & ~0x3fUL;
502 unpin_guest_page(vcpu->kvm, gpa, hpa);
503 scb_s->riccbd = 0;
504 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100505
506 hpa = scb_s->sdnxo;
507 if (hpa) {
508 gpa = scb_o->sdnxo;
509 unpin_guest_page(vcpu->kvm, gpa, hpa);
510 scb_s->sdnxo = 0;
511 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200512}
513
514/*
515 * Instead of shadowing some blocks, we can simply forward them because the
516 * addresses in the scb are 64 bit long.
517 *
518 * This works as long as the data lies in one page. If blocks ever exceed one
519 * page, we have to fall back to shadowing.
520 *
521 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
522 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
523 *
524 * Returns: - 0 if all blocks were pinned.
525 * - > 0 if control has to be given to guest 2
526 * - -ENOMEM if out of memory
527 */
528static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
529{
530 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
531 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
532 hpa_t hpa;
533 gpa_t gpa;
534 int rc = 0;
535
536 gpa = scb_o->scaol & ~0xfUL;
David Hildenbrand19c439b2015-11-25 11:02:26 +0100537 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
538 gpa |= (u64) scb_o->scaoh << 32;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200539 if (gpa) {
540 if (!(gpa & ~0x1fffUL))
541 rc = set_validity_icpt(scb_s, 0x0038U);
542 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
543 rc = set_validity_icpt(scb_s, 0x0011U);
544 else if ((gpa & PAGE_MASK) !=
545 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
546 rc = set_validity_icpt(scb_s, 0x003bU);
547 if (!rc) {
548 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200549 if (rc)
David Hildenbranda3508fb2015-07-08 13:19:48 +0200550 rc = set_validity_icpt(scb_s, 0x0034U);
551 }
552 if (rc)
553 goto unpin;
554 scb_s->scaoh = (u32)((u64)hpa >> 32);
555 scb_s->scaol = (u32)(u64)hpa;
556 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100557
558 gpa = scb_o->itdba & ~0xffUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100559 if (gpa && (scb_s->ecb & ECB_TE)) {
David Hildenbrand166ecb32015-11-25 11:13:32 +0100560 if (!(gpa & ~0x1fffU)) {
561 rc = set_validity_icpt(scb_s, 0x0080U);
562 goto unpin;
563 }
564 /* 256 bytes cannot cross page boundaries */
565 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200566 if (rc) {
David Hildenbrand166ecb32015-11-25 11:13:32 +0100567 rc = set_validity_icpt(scb_s, 0x0080U);
David Hildenbrand166ecb32015-11-25 11:13:32 +0100568 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200569 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100570 scb_s->itdba = hpa;
571 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100572
573 gpa = scb_o->gvrd & ~0x1ffUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100574 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100575 if (!(gpa & ~0x1fffUL)) {
576 rc = set_validity_icpt(scb_s, 0x1310U);
577 goto unpin;
578 }
579 /*
580 * 512 bytes vector registers cannot cross page boundaries
581 * if this block gets bigger, we have to shadow it.
582 */
583 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200584 if (rc) {
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100585 rc = set_validity_icpt(scb_s, 0x1310U);
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100586 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200587 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100588 scb_s->gvrd = hpa;
589 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100590
591 gpa = scb_o->riccbd & ~0x3fUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100592 if (gpa && (scb_s->ecb3 & ECB3_RI)) {
David Hildenbrand588438c2016-01-26 12:51:06 +0100593 if (!(gpa & ~0x1fffUL)) {
594 rc = set_validity_icpt(scb_s, 0x0043U);
595 goto unpin;
596 }
597 /* 64 bytes cannot cross page boundaries */
598 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200599 if (rc) {
David Hildenbrand588438c2016-01-26 12:51:06 +0100600 rc = set_validity_icpt(scb_s, 0x0043U);
David Hildenbrand588438c2016-01-26 12:51:06 +0100601 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200602 }
603 /* Validity 0x0044 will be checked by SIE */
David Hildenbrand4d21cef32016-09-02 12:33:49 +0200604 scb_s->riccbd = hpa;
David Hildenbrand588438c2016-01-26 12:51:06 +0100605 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100606 if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
607 unsigned long sdnxc;
608
609 gpa = scb_o->sdnxo & ~0xfUL;
610 sdnxc = scb_o->sdnxo & 0xfUL;
611 if (!gpa || !(gpa & ~0x1fffUL)) {
612 rc = set_validity_icpt(scb_s, 0x10b0U);
613 goto unpin;
614 }
615 if (sdnxc < 6 || sdnxc > 12) {
616 rc = set_validity_icpt(scb_s, 0x10b1U);
617 goto unpin;
618 }
619 if (gpa & ((1 << sdnxc) - 1)) {
620 rc = set_validity_icpt(scb_s, 0x10b2U);
621 goto unpin;
622 }
623 /* Due to alignment rules (checked above) this cannot
624 * cross page boundaries
625 */
626 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200627 if (rc) {
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100628 rc = set_validity_icpt(scb_s, 0x10b0U);
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100629 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200630 }
Christian Borntraegerfe722d12017-04-07 14:23:13 +0200631 scb_s->sdnxo = hpa | sdnxc;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100632 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200633 return 0;
634unpin:
635 unpin_blocks(vcpu, vsie_page);
636 return rc;
637}
638
639/* unpin the scb provided by guest 2, marking it as dirty */
640static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
641 gpa_t gpa)
642{
643 hpa_t hpa = (hpa_t) vsie_page->scb_o;
644
645 if (hpa)
646 unpin_guest_page(vcpu->kvm, gpa, hpa);
647 vsie_page->scb_o = NULL;
648}
649
650/*
651 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
652 *
653 * Returns: - 0 if the scb was pinned.
654 * - > 0 if control has to be given to guest 2
David Hildenbranda3508fb2015-07-08 13:19:48 +0200655 */
656static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
657 gpa_t gpa)
658{
659 hpa_t hpa;
660 int rc;
661
662 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200663 if (rc) {
David Hildenbranda3508fb2015-07-08 13:19:48 +0200664 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200665 WARN_ON_ONCE(rc);
666 return 1;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200667 }
David Hildenbrandf7a65092017-09-01 17:11:43 +0200668 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
669 return 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200670}
671
672/*
673 * Inject a fault into guest 2.
674 *
675 * Returns: - > 0 if control has to be given to guest 2
676 * < 0 if an error occurred during injection.
677 */
678static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
679 bool write_flag)
680{
681 struct kvm_s390_pgm_info pgm = {
682 .code = code,
683 .trans_exc_code =
684 /* 0-51: virtual address */
685 (vaddr & 0xfffffffffffff000UL) |
686 /* 52-53: store / fetch */
687 (((unsigned int) !write_flag) + 1) << 10,
688 /* 62-63: asce id (alway primary == 0) */
689 .exc_access_id = 0, /* always primary */
690 .op_access_id = 0, /* not MVPG */
691 };
692 int rc;
693
694 if (code == PGM_PROTECTION)
695 pgm.trans_exc_code |= 0x4UL;
696
697 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
698 return rc ? rc : 1;
699}
700
701/*
702 * Handle a fault during vsie execution on a gmap shadow.
703 *
704 * Returns: - 0 if the fault was resolved
705 * - > 0 if control has to be given to guest 2
706 * - < 0 if an error occurred
707 */
708static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
709{
710 int rc;
711
712 if (current->thread.gmap_int_code == PGM_PROTECTION)
713 /* we can directly forward all protection exceptions */
714 return inject_fault(vcpu, PGM_PROTECTION,
715 current->thread.gmap_addr, 1);
716
717 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
718 current->thread.gmap_addr);
719 if (rc > 0) {
720 rc = inject_fault(vcpu, rc,
721 current->thread.gmap_addr,
722 current->thread.gmap_write_flag);
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200723 if (rc >= 0)
724 vsie_page->fault_addr = current->thread.gmap_addr;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200725 }
726 return rc;
727}
728
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200729/*
730 * Retry the previous fault that required guest 2 intervention. This avoids
731 * one superfluous SIE re-entry and direct exit.
732 *
733 * Will ignore any errors. The next SIE fault will do proper fault handling.
734 */
735static void handle_last_fault(struct kvm_vcpu *vcpu,
736 struct vsie_page *vsie_page)
737{
738 if (vsie_page->fault_addr)
739 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
740 vsie_page->fault_addr);
741 vsie_page->fault_addr = 0;
742}
743
David Hildenbranda3508fb2015-07-08 13:19:48 +0200744static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
745{
746 vsie_page->scb_s.icptcode = 0;
747}
748
David Hildenbrand66b630d2015-11-26 14:11:19 +0100749/* rewind the psw and clear the vsie icpt, so we can retry execution */
750static void retry_vsie_icpt(struct vsie_page *vsie_page)
751{
752 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
753 int ilen = insn_length(scb_s->ipa >> 8);
754
755 /* take care of EXECUTE instructions */
756 if (scb_s->icptstatus & 1) {
757 ilen = (scb_s->icptstatus >> 4) & 0x6;
758 if (!ilen)
759 ilen = 4;
760 }
761 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
762 clear_vsie_icpt(vsie_page);
763}
764
765/*
766 * Try to shadow + enable the guest 2 provided facility list.
767 * Retry instruction execution if enabled for and provided by guest 2.
768 *
769 * Returns: - 0 if handled (retry or guest 2 icpt)
770 * - > 0 if control has to be given to guest 2
771 */
772static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
773{
774 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
775 __u32 fac = vsie_page->scb_o->fac & 0x7ffffff8U;
776
777 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
778 retry_vsie_icpt(vsie_page);
779 if (read_guest_real(vcpu, fac, &vsie_page->fac,
780 sizeof(vsie_page->fac)))
781 return set_validity_icpt(scb_s, 0x1090U);
782 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
783 }
784 return 0;
785}
786
David Hildenbranda3508fb2015-07-08 13:19:48 +0200787/*
788 * Run the vsie on a shadow scb and a shadow gmap, without any further
789 * sanity checks, handling SIE faults.
790 *
791 * Returns: - 0 everything went fine
792 * - > 0 if control has to be given to guest 2
793 * - < 0 if an error occurred
794 */
795static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
796{
797 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
798 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
799 int rc;
800
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200801 handle_last_fault(vcpu, vsie_page);
802
David Hildenbranda3508fb2015-07-08 13:19:48 +0200803 if (need_resched())
804 schedule();
805 if (test_cpu_flag(CIF_MCCK_PENDING))
806 s390_handle_mcck();
807
808 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
809 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200810 guest_enter_irqoff();
David Hildenbranda3508fb2015-07-08 13:19:48 +0200811 local_irq_enable();
812
813 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
814
815 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200816 guest_exit_irqoff();
David Hildenbranda3508fb2015-07-08 13:19:48 +0200817 local_irq_enable();
818 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
819
QingFeng Haod52cd202017-06-07 12:11:18 +0200820 if (rc == -EINTR) {
821 VCPU_EVENT(vcpu, 3, "%s", "machine check");
David Hildenbrandc95c8952017-08-30 18:06:02 +0200822 kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
QingFeng Haod52cd202017-06-07 12:11:18 +0200823 return 0;
824 }
825
David Hildenbranda3508fb2015-07-08 13:19:48 +0200826 if (rc > 0)
827 rc = 0; /* we could still have an icpt */
828 else if (rc == -EFAULT)
829 return handle_fault(vcpu, vsie_page);
830
831 switch (scb_s->icptcode) {
David Hildenbrand66b630d2015-11-26 14:11:19 +0100832 case ICPT_INST:
833 if (scb_s->ipa == 0xb2b0)
834 rc = handle_stfle(vcpu, vsie_page);
835 break;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200836 case ICPT_STOP:
837 /* stop not requested by g2 - must have been a kick */
838 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
839 clear_vsie_icpt(vsie_page);
840 break;
841 case ICPT_VALIDITY:
842 if ((scb_s->ipa & 0xf000) != 0xf000)
843 scb_s->ipa += 0x1000;
844 break;
845 }
846 return rc;
847}
848
849static void release_gmap_shadow(struct vsie_page *vsie_page)
850{
851 if (vsie_page->gmap)
852 gmap_put(vsie_page->gmap);
853 WRITE_ONCE(vsie_page->gmap, NULL);
David Hildenbrand06d68a62016-04-22 13:50:09 +0200854 prefix_unmapped(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200855}
856
857static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
858 struct vsie_page *vsie_page)
859{
860 unsigned long asce;
861 union ctlreg0 cr0;
862 struct gmap *gmap;
863 int edat;
864
865 asce = vcpu->arch.sie_block->gcr[1];
866 cr0.val = vcpu->arch.sie_block->gcr[0];
867 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
868 edat += edat && test_kvm_facility(vcpu->kvm, 78);
869
David Hildenbrand06d68a62016-04-22 13:50:09 +0200870 /*
871 * ASCE or EDAT could have changed since last icpt, or the gmap
872 * we're holding has been unshadowed. If the gmap is still valid,
873 * we can safely reuse it.
874 */
875 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
876 return 0;
877
878 /* release the old shadow - if any, and mark the prefix as unmapped */
879 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200880 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
881 if (IS_ERR(gmap))
882 return PTR_ERR(gmap);
883 gmap->private = vcpu->kvm;
884 WRITE_ONCE(vsie_page->gmap, gmap);
885 return 0;
886}
887
888/*
David Hildenbrandadbf1692016-05-27 22:03:52 +0200889 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
890 */
891static void register_shadow_scb(struct kvm_vcpu *vcpu,
892 struct vsie_page *vsie_page)
893{
David Hildenbrand91473b42015-10-29 10:30:36 +0100894 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
895
David Hildenbrandadbf1692016-05-27 22:03:52 +0200896 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
David Hildenbrandb917ae52015-07-07 20:39:35 +0200897 /*
898 * External calls have to lead to a kick of the vcpu and
899 * therefore the vsie -> Simulate Wait state.
900 */
901 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand91473b42015-10-29 10:30:36 +0100902 /*
903 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
904 * automatically be adjusted on tod clock changes via kvm_sync_clock.
905 */
906 preempt_disable();
907 scb_s->epoch += vcpu->kvm->arch.epoch;
Collin L. Walling8fa16962016-07-26 15:29:44 -0400908
909 if (scb_s->ecd & ECD_MEF) {
910 scb_s->epdx += vcpu->kvm->arch.epdx;
911 if (scb_s->epoch < vcpu->kvm->arch.epoch)
912 scb_s->epdx += 1;
913 }
914
David Hildenbrand91473b42015-10-29 10:30:36 +0100915 preempt_enable();
David Hildenbrandadbf1692016-05-27 22:03:52 +0200916}
917
918/*
919 * Unregister a shadow scb from a VCPU.
920 */
921static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
922{
David Hildenbrandb917ae52015-07-07 20:39:35 +0200923 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
David Hildenbrandadbf1692016-05-27 22:03:52 +0200924 WRITE_ONCE(vcpu->arch.vsie_block, NULL);
925}
926
927/*
David Hildenbranda3508fb2015-07-08 13:19:48 +0200928 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
929 * prefix pages and faults.
930 *
931 * Returns: - 0 if no errors occurred
932 * - > 0 if control has to be given to guest 2
933 * - -ENOMEM if out of memory
934 */
935static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
936{
937 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
938 int rc = 0;
939
940 while (1) {
941 rc = acquire_gmap_shadow(vcpu, vsie_page);
942 if (!rc)
943 rc = map_prefix(vcpu, vsie_page);
944 if (!rc) {
945 gmap_enable(vsie_page->gmap);
946 update_intervention_requests(vsie_page);
947 rc = do_vsie_run(vcpu, vsie_page);
948 gmap_enable(vcpu->arch.gmap);
949 }
David Hildenbrandadbf1692016-05-27 22:03:52 +0200950 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200951
952 if (rc == -EAGAIN)
953 rc = 0;
954 if (rc || scb_s->icptcode || signal_pending(current) ||
955 kvm_s390_vcpu_has_irq(vcpu, 0))
956 break;
Heiko Carstens0b925152017-01-02 08:51:02 +0100957 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200958
959 if (rc == -EFAULT) {
960 /*
961 * Addressing exceptions are always presentes as intercepts.
962 * As addressing exceptions are suppressing and our guest 3 PSW
963 * points at the responsible instruction, we have to
964 * forward the PSW and set the ilc. If we can't read guest 3
965 * instruction, we can use an arbitrary ilc. Let's always use
966 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
967 * memory. (we could also fake the shadow so the hardware
968 * handles it).
969 */
970 scb_s->icptcode = ICPT_PROGI;
971 scb_s->iprcc = PGM_ADDRESSING;
972 scb_s->pgmilc = 4;
973 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
974 }
975 return rc;
976}
977
978/*
979 * Get or create a vsie page for a scb address.
980 *
981 * Returns: - address of a vsie page (cached or new one)
982 * - NULL if the same scb address is already used by another VCPU
983 * - ERR_PTR(-ENOMEM) if out of memory
984 */
985static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
986{
987 struct vsie_page *vsie_page;
988 struct page *page;
989 int nr_vcpus;
990
991 rcu_read_lock();
992 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
993 rcu_read_unlock();
994 if (page) {
995 if (page_ref_inc_return(page) == 2)
996 return page_to_virt(page);
997 page_ref_dec(page);
998 }
999
1000 /*
1001 * We want at least #online_vcpus shadows, so every VCPU can execute
1002 * the VSIE in parallel.
1003 */
1004 nr_vcpus = atomic_read(&kvm->online_vcpus);
1005
1006 mutex_lock(&kvm->arch.vsie.mutex);
1007 if (kvm->arch.vsie.page_count < nr_vcpus) {
David Hildenbrand66b630d2015-11-26 14:11:19 +01001008 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001009 if (!page) {
1010 mutex_unlock(&kvm->arch.vsie.mutex);
1011 return ERR_PTR(-ENOMEM);
1012 }
1013 page_ref_inc(page);
1014 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1015 kvm->arch.vsie.page_count++;
1016 } else {
1017 /* reuse an existing entry that belongs to nobody */
1018 while (true) {
1019 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1020 if (page_ref_inc_return(page) == 2)
1021 break;
1022 page_ref_dec(page);
1023 kvm->arch.vsie.next++;
1024 kvm->arch.vsie.next %= nr_vcpus;
1025 }
1026 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1027 }
1028 page->index = addr;
1029 /* double use of the same address */
1030 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1031 page_ref_dec(page);
1032 mutex_unlock(&kvm->arch.vsie.mutex);
1033 return NULL;
1034 }
1035 mutex_unlock(&kvm->arch.vsie.mutex);
1036
1037 vsie_page = page_to_virt(page);
1038 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
David Hildenbrand06d68a62016-04-22 13:50:09 +02001039 release_gmap_shadow(vsie_page);
David Hildenbrand1b7029b2015-07-08 13:25:31 +02001040 vsie_page->fault_addr = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +02001041 vsie_page->scb_s.ihcpu = 0xffffU;
1042 return vsie_page;
1043}
1044
1045/* put a vsie page acquired via get_vsie_page */
1046static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1047{
1048 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1049
1050 page_ref_dec(page);
1051}
1052
1053int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1054{
1055 struct vsie_page *vsie_page;
1056 unsigned long scb_addr;
1057 int rc;
1058
1059 vcpu->stat.instruction_sie++;
1060 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1061 return -EOPNOTSUPP;
1062 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1063 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1064
Heiko Carstens58cdf5e2017-07-05 07:37:14 +02001065 BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001066 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1067
1068 /* 512 byte alignment */
1069 if (unlikely(scb_addr & 0x1ffUL))
1070 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1071
1072 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1073 return 0;
1074
1075 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1076 if (IS_ERR(vsie_page))
1077 return PTR_ERR(vsie_page);
1078 else if (!vsie_page)
1079 /* double use of sie control block - simply do nothing */
1080 return 0;
1081
1082 rc = pin_scb(vcpu, vsie_page, scb_addr);
1083 if (rc)
1084 goto out_put;
1085 rc = shadow_scb(vcpu, vsie_page);
1086 if (rc)
1087 goto out_unpin_scb;
1088 rc = pin_blocks(vcpu, vsie_page);
1089 if (rc)
1090 goto out_unshadow;
David Hildenbrandadbf1692016-05-27 22:03:52 +02001091 register_shadow_scb(vcpu, vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001092 rc = vsie_run(vcpu, vsie_page);
David Hildenbrandadbf1692016-05-27 22:03:52 +02001093 unregister_shadow_scb(vcpu);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001094 unpin_blocks(vcpu, vsie_page);
1095out_unshadow:
1096 unshadow_scb(vcpu, vsie_page);
1097out_unpin_scb:
1098 unpin_scb(vcpu, vsie_page, scb_addr);
1099out_put:
1100 put_vsie_page(vcpu->kvm, vsie_page);
1101
1102 return rc < 0 ? rc : 0;
1103}
1104
1105/* Init the vsie data structures. To be called when a vm is initialized. */
1106void kvm_s390_vsie_init(struct kvm *kvm)
1107{
1108 mutex_init(&kvm->arch.vsie.mutex);
1109 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1110}
1111
1112/* Destroy the vsie data structures. To be called when a vm is destroyed. */
1113void kvm_s390_vsie_destroy(struct kvm *kvm)
1114{
David Hildenbrand06d68a62016-04-22 13:50:09 +02001115 struct vsie_page *vsie_page;
David Hildenbranda3508fb2015-07-08 13:19:48 +02001116 struct page *page;
1117 int i;
1118
1119 mutex_lock(&kvm->arch.vsie.mutex);
1120 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1121 page = kvm->arch.vsie.pages[i];
1122 kvm->arch.vsie.pages[i] = NULL;
David Hildenbrand06d68a62016-04-22 13:50:09 +02001123 vsie_page = page_to_virt(page);
1124 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001125 /* free the radix tree entry */
1126 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1127 __free_page(page);
1128 }
1129 kvm->arch.vsie.page_count = 0;
1130 mutex_unlock(&kvm->arch.vsie.mutex);
1131}
David Hildenbrandadbf1692016-05-27 22:03:52 +02001132
1133void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1134{
1135 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1136
1137 /*
1138 * Even if the VCPU lets go of the shadow sie block reference, it is
1139 * still valid in the cache. So we can safely kick it.
1140 */
1141 if (scb) {
1142 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1143 if (scb->prog0c & PROG_IN_SIE)
1144 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);
1145 }
1146}