blob: d471910bd9ea5c864f4b4fd03358cf9658c0495f [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010017#include <linux/sched/signal.h>
18
David Hildenbranda3508fb2015-07-08 13:19:48 +020019#include <asm/gmap.h>
20#include <asm/mmu_context.h>
21#include <asm/sclp.h>
22#include <asm/nmi.h>
David Hildenbrand66b630d2015-11-26 14:11:19 +010023#include <asm/dis.h>
David Hildenbranda3508fb2015-07-08 13:19:48 +020024#include "kvm-s390.h"
25#include "gaccess.h"
26
27struct vsie_page {
28 struct kvm_s390_sie_block scb_s; /* 0x0000 */
QingFeng Haod52cd202017-06-07 12:11:18 +020029 /*
30 * the backup info for machine check. ensure it's at
31 * the same offset as that in struct sie_page!
32 */
33 struct mcck_volatile_info mcck_info; /* 0x0200 */
David Hildenbranda3508fb2015-07-08 13:19:48 +020034 /* the pinned originial scb */
QingFeng Haod52cd202017-06-07 12:11:18 +020035 struct kvm_s390_sie_block *scb_o; /* 0x0218 */
David Hildenbranda3508fb2015-07-08 13:19:48 +020036 /* the shadow gmap in use by the vsie_page */
QingFeng Haod52cd202017-06-07 12:11:18 +020037 struct gmap *gmap; /* 0x0220 */
David Hildenbrand1b7029b2015-07-08 13:25:31 +020038 /* address of the last reported fault to guest2 */
QingFeng Haod52cd202017-06-07 12:11:18 +020039 unsigned long fault_addr; /* 0x0228 */
40 __u8 reserved[0x0700 - 0x0230]; /* 0x0230 */
David Hildenbrandbbeaa582015-11-26 13:11:42 +010041 struct kvm_s390_crypto_cb crycb; /* 0x0700 */
David Hildenbrand66b630d2015-11-26 14:11:19 +010042 __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */
Martin Schwidefsky1cae0252017-06-21 16:49:15 +020043};
David Hildenbranda3508fb2015-07-08 13:19:48 +020044
45/* trigger a validity icpt for the given scb */
46static int set_validity_icpt(struct kvm_s390_sie_block *scb,
47 __u16 reason_code)
48{
49 scb->ipa = 0x1000;
50 scb->ipb = ((__u32) reason_code) << 16;
51 scb->icptcode = ICPT_VALIDITY;
52 return 1;
53}
54
55/* mark the prefix as unmapped, this will block the VSIE */
56static void prefix_unmapped(struct vsie_page *vsie_page)
57{
58 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
59}
60
61/* mark the prefix as unmapped and wait until the VSIE has been left */
62static void prefix_unmapped_sync(struct vsie_page *vsie_page)
63{
64 prefix_unmapped(vsie_page);
65 if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
66 atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
67 while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
68 cpu_relax();
69}
70
71/* mark the prefix as mapped, this will allow the VSIE to run */
72static void prefix_mapped(struct vsie_page *vsie_page)
73{
74 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
75}
76
David Hildenbrand06d68a62016-04-22 13:50:09 +020077/* test if the prefix is mapped into the gmap shadow */
78static int prefix_is_mapped(struct vsie_page *vsie_page)
79{
80 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
81}
David Hildenbranda3508fb2015-07-08 13:19:48 +020082
83/* copy the updated intervention request bits into the shadow scb */
84static void update_intervention_requests(struct vsie_page *vsie_page)
85{
86 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
87 int cpuflags;
88
89 cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
90 atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
91 atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
92}
93
94/* shadow (filter and validate) the cpuflags */
95static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
96{
97 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
98 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
99 int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
100
101 /* we don't allow ESA/390 guests */
102 if (!(cpuflags & CPUSTAT_ZARCH))
103 return set_validity_icpt(scb_s, 0x0001U);
104
105 if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
106 return set_validity_icpt(scb_s, 0x0001U);
107 else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
108 return set_validity_icpt(scb_s, 0x0007U);
109
110 /* intervention requests will be set later */
111 newflags = CPUSTAT_ZARCH;
David Hildenbrand535ef812016-02-12 12:24:20 +0100112 if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8))
113 newflags |= CPUSTAT_GED;
114 if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) {
115 if (cpuflags & CPUSTAT_GED)
116 return set_validity_icpt(scb_s, 0x0001U);
117 newflags |= CPUSTAT_GED2;
118 }
David Hildenbrand77d18f62015-11-24 16:32:35 +0100119 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE))
120 newflags |= cpuflags & CPUSTAT_P;
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100121 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS))
122 newflags |= cpuflags & CPUSTAT_SM;
David Hildenbrand7fd7f392015-11-24 16:56:23 +0100123 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS))
124 newflags |= cpuflags & CPUSTAT_IBS;
Farhan Ali730cd632017-02-24 16:12:56 -0500125 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_KSS))
126 newflags |= cpuflags & CPUSTAT_KSS;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200127
128 atomic_set(&scb_s->cpuflags, newflags);
129 return 0;
130}
131
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100132/*
133 * Create a shadow copy of the crycb block and setup key wrapping, if
134 * requested for guest 3 and enabled for guest 2.
135 *
136 * We only accept format-1 (no AP in g2), but convert it into format-2
137 * There is nothing to do for format-0.
138 *
139 * Returns: - 0 if shadowed or nothing to do
140 * - > 0 if control has to be given to guest 2
141 */
142static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
143{
144 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
145 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
146 u32 crycb_addr = scb_o->crycbd & 0x7ffffff8U;
147 unsigned long *b1, *b2;
148 u8 ecb3_flags;
149
150 scb_s->crycbd = 0;
151 if (!(scb_o->crycbd & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
152 return 0;
153 /* format-1 is supported with message-security-assist extension 3 */
154 if (!test_kvm_facility(vcpu->kvm, 76))
155 return 0;
156 /* we may only allow it if enabled for guest 2 */
157 ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
158 (ECB3_AES | ECB3_DEA);
159 if (!ecb3_flags)
160 return 0;
161
162 if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
163 return set_validity_icpt(scb_s, 0x003CU);
164 else if (!crycb_addr)
165 return set_validity_icpt(scb_s, 0x0039U);
166
167 /* copy only the wrapping keys */
168 if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
169 return set_validity_icpt(scb_s, 0x0035U);
170
171 scb_s->ecb3 |= ecb3_flags;
172 scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
173 CRYCB_FORMAT2;
174
175 /* xor both blocks in one run */
176 b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
177 b2 = (unsigned long *)
178 vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
179 /* as 56%8 == 0, bitmap_xor won't overwrite any data */
180 bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
181 return 0;
182}
183
David Hildenbrand35736022016-02-19 10:11:24 +0100184/* shadow (round up/down) the ibc to avoid validity icpt */
185static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
186{
187 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
188 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
189 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
190
191 scb_s->ibc = 0;
192 /* ibc installed in g2 and requested for g3 */
193 if (vcpu->kvm->arch.model.ibc && (scb_o->ibc & 0x0fffU)) {
194 scb_s->ibc = scb_o->ibc & 0x0fffU;
195 /* takte care of the minimum ibc level of the machine */
196 if (scb_s->ibc < min_ibc)
197 scb_s->ibc = min_ibc;
198 /* take care of the maximum ibc level set for the guest */
199 if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
200 scb_s->ibc = vcpu->kvm->arch.model.ibc;
201 }
202}
203
David Hildenbranda3508fb2015-07-08 13:19:48 +0200204/* unshadow the scb, copying parameters back to the real scb */
205static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
206{
207 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
208 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
209
210 /* interception */
211 scb_o->icptcode = scb_s->icptcode;
212 scb_o->icptstatus = scb_s->icptstatus;
213 scb_o->ipa = scb_s->ipa;
214 scb_o->ipb = scb_s->ipb;
215 scb_o->gbea = scb_s->gbea;
216
217 /* timer */
218 scb_o->cputm = scb_s->cputm;
219 scb_o->ckc = scb_s->ckc;
220 scb_o->todpr = scb_s->todpr;
221
222 /* guest state */
223 scb_o->gpsw = scb_s->gpsw;
224 scb_o->gg14 = scb_s->gg14;
225 scb_o->gg15 = scb_s->gg15;
226 memcpy(scb_o->gcr, scb_s->gcr, 128);
227 scb_o->pp = scb_s->pp;
228
229 /* interrupt intercept */
230 switch (scb_s->icptcode) {
231 case ICPT_PROGI:
232 case ICPT_INSTPROGI:
233 case ICPT_EXTINT:
234 memcpy((void *)((u64)scb_o + 0xc0),
235 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
236 break;
237 case ICPT_PARTEXEC:
238 /* MVPG only */
239 memcpy((void *)((u64)scb_o + 0xc0),
240 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
241 break;
242 }
243
244 if (scb_s->ihcpu != 0xffffU)
245 scb_o->ihcpu = scb_s->ihcpu;
246}
247
248/*
249 * Setup the shadow scb by copying and checking the relevant parts of the g2
250 * provided scb.
251 *
252 * Returns: - 0 if the scb has been shadowed
253 * - > 0 if control has to be given to guest 2
254 */
255static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
256{
257 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
258 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100259 bool had_tx = scb_s->ecb & ECB_TE;
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100260 unsigned long new_mso = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200261 int rc;
262
263 /* make sure we don't have any leftovers when reusing the scb */
264 scb_s->icptcode = 0;
265 scb_s->eca = 0;
266 scb_s->ecb = 0;
267 scb_s->ecb2 = 0;
268 scb_s->ecb3 = 0;
269 scb_s->ecd = 0;
David Hildenbrand66b630d2015-11-26 14:11:19 +0100270 scb_s->fac = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200271
272 rc = prepare_cpuflags(vcpu, vsie_page);
273 if (rc)
274 goto out;
275
276 /* timer */
277 scb_s->cputm = scb_o->cputm;
278 scb_s->ckc = scb_o->ckc;
279 scb_s->todpr = scb_o->todpr;
280 scb_s->epoch = scb_o->epoch;
281
282 /* guest state */
283 scb_s->gpsw = scb_o->gpsw;
284 scb_s->gg14 = scb_o->gg14;
285 scb_s->gg15 = scb_o->gg15;
286 memcpy(scb_s->gcr, scb_o->gcr, 128);
287 scb_s->pp = scb_o->pp;
288
289 /* interception / execution handling */
290 scb_s->gbea = scb_o->gbea;
291 scb_s->lctl = scb_o->lctl;
292 scb_s->svcc = scb_o->svcc;
293 scb_s->ictl = scb_o->ictl;
294 /*
295 * SKEY handling functions can't deal with false setting of PTE invalid
296 * bits. Therefore we cannot provide interpretation and would later
297 * have to provide own emulation handlers.
298 */
Farhan Ali730cd632017-02-24 16:12:56 -0500299 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
300 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
301
David Hildenbranda3508fb2015-07-08 13:19:48 +0200302 scb_s->icpua = scb_o->icpua;
303
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100304 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
305 new_mso = scb_o->mso & 0xfffffffffff00000UL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200306 /* if the hva of the prefix changes, we have to remap the prefix */
307 if (scb_s->mso != new_mso || scb_s->prefix != scb_o->prefix)
308 prefix_unmapped(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200309 /* SIE will do mso/msl validity and exception checks for us */
310 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200311 scb_s->mso = new_mso;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200312 scb_s->prefix = scb_o->prefix;
313
314 /* We have to definetly flush the tlb if this scb never ran */
315 if (scb_s->ihcpu != 0xffffU)
316 scb_s->ihcpu = scb_o->ihcpu;
317
318 /* MVPG and Protection Exception Interpretation are always available */
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100319 scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
David Hildenbrand4ceafa92015-11-27 12:34:28 +0100320 /* Host-protection-interruption introduced with ESOP */
321 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100322 scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100323 /* transactional execution */
324 if (test_kvm_facility(vcpu->kvm, 73)) {
325 /* remap the prefix is tx is toggled on */
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100326 if ((scb_o->ecb & ECB_TE) && !had_tx)
David Hildenbrand166ecb32015-11-25 11:13:32 +0100327 prefix_unmapped(vsie_page);
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100328 scb_s->ecb |= scb_o->ecb & ECB_TE;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100329 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100330 /* SIMD */
331 if (test_kvm_facility(vcpu->kvm, 129)) {
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100332 scb_s->eca |= scb_o->eca & ECA_VX;
333 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100334 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100335 /* Run-time-Instrumentation */
336 if (test_kvm_facility(vcpu->kvm, 64))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100337 scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
Janosch Frankcd1836f2016-08-04 09:57:36 +0200338 /* Instruction Execution Prevention */
339 if (test_kvm_facility(vcpu->kvm, 130))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100340 scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100341 /* Guarded Storage */
342 if (test_kvm_facility(vcpu->kvm, 133)) {
343 scb_s->ecb |= scb_o->ecb & ECB_GS;
344 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
345 }
David Hildenbrand0615a322015-11-25 09:59:49 +0100346 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100347 scb_s->eca |= scb_o->eca & ECA_SII;
David Hildenbrand5630a8e2015-11-24 16:53:51 +0100348 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100349 scb_s->eca |= scb_o->eca & ECA_IB;
David Hildenbrand13ee3f62015-11-24 16:54:37 +0100350 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100351 scb_s->eca |= scb_o->eca & ECA_CEI;
Collin L. Walling8fa16962016-07-26 15:29:44 -0400352 /* Epoch Extension */
353 if (test_kvm_facility(vcpu->kvm, 139))
354 scb_s->ecd |= scb_o->ecd & ECD_MEF;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200355
David Hildenbrand35736022016-02-19 10:11:24 +0100356 prepare_ibc(vcpu, vsie_page);
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100357 rc = shadow_crycb(vcpu, vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200358out:
359 if (rc)
360 unshadow_scb(vcpu, vsie_page);
361 return rc;
362}
363
364void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
365 unsigned long end)
366{
367 struct kvm *kvm = gmap->private;
368 struct vsie_page *cur;
369 unsigned long prefix;
370 struct page *page;
371 int i;
372
373 if (!gmap_is_shadow(gmap))
374 return;
375 if (start >= 1UL << 31)
376 /* We are only interested in prefix pages */
377 return;
378
379 /*
380 * Only new shadow blocks are added to the list during runtime,
381 * therefore we can safely reference them all the time.
382 */
383 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
384 page = READ_ONCE(kvm->arch.vsie.pages[i]);
385 if (!page)
386 continue;
387 cur = page_to_virt(page);
388 if (READ_ONCE(cur->gmap) != gmap)
389 continue;
390 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
391 /* with mso/msl, the prefix lies at an offset */
392 prefix += cur->scb_s.mso;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100393 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
David Hildenbranda3508fb2015-07-08 13:19:48 +0200394 prefix_unmapped_sync(cur);
395 }
396}
397
398/*
David Hildenbrand166ecb32015-11-25 11:13:32 +0100399 * Map the first prefix page and if tx is enabled also the second prefix page.
David Hildenbranda3508fb2015-07-08 13:19:48 +0200400 *
401 * The prefix will be protected, a gmap notifier will inform about unmaps.
402 * The shadow scb must not be executed until the prefix is remapped, this is
403 * guaranteed by properly handling PROG_REQUEST.
404 *
405 * Returns: - 0 on if successfully mapped or already mapped
406 * - > 0 if control has to be given to guest 2
407 * - -EAGAIN if the caller can retry immediately
408 * - -ENOMEM if out of memory
409 */
410static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
411{
412 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
413 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
414 int rc;
415
David Hildenbrand06d68a62016-04-22 13:50:09 +0200416 if (prefix_is_mapped(vsie_page))
417 return 0;
418
David Hildenbranda3508fb2015-07-08 13:19:48 +0200419 /* mark it as mapped so we can catch any concurrent unmappers */
420 prefix_mapped(vsie_page);
421
422 /* with mso/msl, the prefix lies at offset *mso* */
423 prefix += scb_s->mso;
424
425 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100426 if (!rc && (scb_s->ecb & ECB_TE))
David Hildenbrand166ecb32015-11-25 11:13:32 +0100427 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
428 prefix + PAGE_SIZE);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200429 /*
430 * We don't have to mprotect, we will be called for all unshadows.
431 * SIE will detect if protection applies and trigger a validity.
432 */
433 if (rc)
434 prefix_unmapped(vsie_page);
435 if (rc > 0 || rc == -EFAULT)
436 rc = set_validity_icpt(scb_s, 0x0037U);
437 return rc;
438}
439
440/*
441 * Pin the guest page given by gpa and set hpa to the pinned host address.
442 * Will always be pinned writable.
443 *
444 * Returns: - 0 on success
445 * - -EINVAL if the gpa is not valid guest storage
446 * - -ENOMEM if out of memory
447 */
448static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
449{
450 struct page *page;
451 hva_t hva;
452 int rc;
453
454 hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
455 if (kvm_is_error_hva(hva))
456 return -EINVAL;
457 rc = get_user_pages_fast(hva, 1, 1, &page);
458 if (rc < 0)
459 return rc;
460 else if (rc != 1)
461 return -ENOMEM;
462 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
463 return 0;
464}
465
466/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
467static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
468{
469 struct page *page;
470
471 page = virt_to_page(hpa);
472 set_page_dirty_lock(page);
473 put_page(page);
474 /* mark the page always as dirty for migration */
475 mark_page_dirty(kvm, gpa_to_gfn(gpa));
476}
477
478/* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
479static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
480{
481 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
482 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
483 hpa_t hpa;
484 gpa_t gpa;
485
486 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
487 if (hpa) {
488 gpa = scb_o->scaol & ~0xfUL;
David Hildenbrand19c439b2015-11-25 11:02:26 +0100489 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
490 gpa |= (u64) scb_o->scaoh << 32;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200491 unpin_guest_page(vcpu->kvm, gpa, hpa);
492 scb_s->scaol = 0;
493 scb_s->scaoh = 0;
494 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100495
496 hpa = scb_s->itdba;
497 if (hpa) {
498 gpa = scb_o->itdba & ~0xffUL;
499 unpin_guest_page(vcpu->kvm, gpa, hpa);
500 scb_s->itdba = 0;
501 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100502
503 hpa = scb_s->gvrd;
504 if (hpa) {
505 gpa = scb_o->gvrd & ~0x1ffUL;
506 unpin_guest_page(vcpu->kvm, gpa, hpa);
507 scb_s->gvrd = 0;
508 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100509
510 hpa = scb_s->riccbd;
511 if (hpa) {
512 gpa = scb_o->riccbd & ~0x3fUL;
513 unpin_guest_page(vcpu->kvm, gpa, hpa);
514 scb_s->riccbd = 0;
515 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100516
517 hpa = scb_s->sdnxo;
518 if (hpa) {
519 gpa = scb_o->sdnxo;
520 unpin_guest_page(vcpu->kvm, gpa, hpa);
521 scb_s->sdnxo = 0;
522 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200523}
524
525/*
526 * Instead of shadowing some blocks, we can simply forward them because the
527 * addresses in the scb are 64 bit long.
528 *
529 * This works as long as the data lies in one page. If blocks ever exceed one
530 * page, we have to fall back to shadowing.
531 *
532 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
533 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
534 *
535 * Returns: - 0 if all blocks were pinned.
536 * - > 0 if control has to be given to guest 2
537 * - -ENOMEM if out of memory
538 */
539static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
540{
541 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
542 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
543 hpa_t hpa;
544 gpa_t gpa;
545 int rc = 0;
546
547 gpa = scb_o->scaol & ~0xfUL;
David Hildenbrand19c439b2015-11-25 11:02:26 +0100548 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
549 gpa |= (u64) scb_o->scaoh << 32;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200550 if (gpa) {
551 if (!(gpa & ~0x1fffUL))
552 rc = set_validity_icpt(scb_s, 0x0038U);
553 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
554 rc = set_validity_icpt(scb_s, 0x0011U);
555 else if ((gpa & PAGE_MASK) !=
556 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
557 rc = set_validity_icpt(scb_s, 0x003bU);
558 if (!rc) {
559 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
560 if (rc == -EINVAL)
561 rc = set_validity_icpt(scb_s, 0x0034U);
562 }
563 if (rc)
564 goto unpin;
565 scb_s->scaoh = (u32)((u64)hpa >> 32);
566 scb_s->scaol = (u32)(u64)hpa;
567 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100568
569 gpa = scb_o->itdba & ~0xffUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100570 if (gpa && (scb_s->ecb & ECB_TE)) {
David Hildenbrand166ecb32015-11-25 11:13:32 +0100571 if (!(gpa & ~0x1fffU)) {
572 rc = set_validity_icpt(scb_s, 0x0080U);
573 goto unpin;
574 }
575 /* 256 bytes cannot cross page boundaries */
576 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
577 if (rc == -EINVAL)
578 rc = set_validity_icpt(scb_s, 0x0080U);
579 if (rc)
580 goto unpin;
581 scb_s->itdba = hpa;
582 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100583
584 gpa = scb_o->gvrd & ~0x1ffUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100585 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100586 if (!(gpa & ~0x1fffUL)) {
587 rc = set_validity_icpt(scb_s, 0x1310U);
588 goto unpin;
589 }
590 /*
591 * 512 bytes vector registers cannot cross page boundaries
592 * if this block gets bigger, we have to shadow it.
593 */
594 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
595 if (rc == -EINVAL)
596 rc = set_validity_icpt(scb_s, 0x1310U);
597 if (rc)
598 goto unpin;
599 scb_s->gvrd = hpa;
600 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100601
602 gpa = scb_o->riccbd & ~0x3fUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100603 if (gpa && (scb_s->ecb3 & ECB3_RI)) {
David Hildenbrand588438c2016-01-26 12:51:06 +0100604 if (!(gpa & ~0x1fffUL)) {
605 rc = set_validity_icpt(scb_s, 0x0043U);
606 goto unpin;
607 }
608 /* 64 bytes cannot cross page boundaries */
609 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
610 if (rc == -EINVAL)
611 rc = set_validity_icpt(scb_s, 0x0043U);
612 /* Validity 0x0044 will be checked by SIE */
613 if (rc)
614 goto unpin;
David Hildenbrand4d21cef32016-09-02 12:33:49 +0200615 scb_s->riccbd = hpa;
David Hildenbrand588438c2016-01-26 12:51:06 +0100616 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100617 if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
618 unsigned long sdnxc;
619
620 gpa = scb_o->sdnxo & ~0xfUL;
621 sdnxc = scb_o->sdnxo & 0xfUL;
622 if (!gpa || !(gpa & ~0x1fffUL)) {
623 rc = set_validity_icpt(scb_s, 0x10b0U);
624 goto unpin;
625 }
626 if (sdnxc < 6 || sdnxc > 12) {
627 rc = set_validity_icpt(scb_s, 0x10b1U);
628 goto unpin;
629 }
630 if (gpa & ((1 << sdnxc) - 1)) {
631 rc = set_validity_icpt(scb_s, 0x10b2U);
632 goto unpin;
633 }
634 /* Due to alignment rules (checked above) this cannot
635 * cross page boundaries
636 */
637 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
638 if (rc == -EINVAL)
639 rc = set_validity_icpt(scb_s, 0x10b0U);
640 if (rc)
641 goto unpin;
Christian Borntraegerfe722d12017-04-07 14:23:13 +0200642 scb_s->sdnxo = hpa | sdnxc;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100643 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200644 return 0;
645unpin:
646 unpin_blocks(vcpu, vsie_page);
647 return rc;
648}
649
650/* unpin the scb provided by guest 2, marking it as dirty */
651static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
652 gpa_t gpa)
653{
654 hpa_t hpa = (hpa_t) vsie_page->scb_o;
655
656 if (hpa)
657 unpin_guest_page(vcpu->kvm, gpa, hpa);
658 vsie_page->scb_o = NULL;
659}
660
661/*
662 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
663 *
664 * Returns: - 0 if the scb was pinned.
665 * - > 0 if control has to be given to guest 2
666 * - -ENOMEM if out of memory
667 */
668static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
669 gpa_t gpa)
670{
671 hpa_t hpa;
672 int rc;
673
674 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
675 if (rc == -EINVAL) {
676 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
677 if (!rc)
678 rc = 1;
679 }
680 if (!rc)
681 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
682 return rc;
683}
684
685/*
686 * Inject a fault into guest 2.
687 *
688 * Returns: - > 0 if control has to be given to guest 2
689 * < 0 if an error occurred during injection.
690 */
691static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
692 bool write_flag)
693{
694 struct kvm_s390_pgm_info pgm = {
695 .code = code,
696 .trans_exc_code =
697 /* 0-51: virtual address */
698 (vaddr & 0xfffffffffffff000UL) |
699 /* 52-53: store / fetch */
700 (((unsigned int) !write_flag) + 1) << 10,
701 /* 62-63: asce id (alway primary == 0) */
702 .exc_access_id = 0, /* always primary */
703 .op_access_id = 0, /* not MVPG */
704 };
705 int rc;
706
707 if (code == PGM_PROTECTION)
708 pgm.trans_exc_code |= 0x4UL;
709
710 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
711 return rc ? rc : 1;
712}
713
714/*
715 * Handle a fault during vsie execution on a gmap shadow.
716 *
717 * Returns: - 0 if the fault was resolved
718 * - > 0 if control has to be given to guest 2
719 * - < 0 if an error occurred
720 */
721static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
722{
723 int rc;
724
725 if (current->thread.gmap_int_code == PGM_PROTECTION)
726 /* we can directly forward all protection exceptions */
727 return inject_fault(vcpu, PGM_PROTECTION,
728 current->thread.gmap_addr, 1);
729
730 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
731 current->thread.gmap_addr);
732 if (rc > 0) {
733 rc = inject_fault(vcpu, rc,
734 current->thread.gmap_addr,
735 current->thread.gmap_write_flag);
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200736 if (rc >= 0)
737 vsie_page->fault_addr = current->thread.gmap_addr;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200738 }
739 return rc;
740}
741
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200742/*
743 * Retry the previous fault that required guest 2 intervention. This avoids
744 * one superfluous SIE re-entry and direct exit.
745 *
746 * Will ignore any errors. The next SIE fault will do proper fault handling.
747 */
748static void handle_last_fault(struct kvm_vcpu *vcpu,
749 struct vsie_page *vsie_page)
750{
751 if (vsie_page->fault_addr)
752 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
753 vsie_page->fault_addr);
754 vsie_page->fault_addr = 0;
755}
756
David Hildenbranda3508fb2015-07-08 13:19:48 +0200757static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
758{
759 vsie_page->scb_s.icptcode = 0;
760}
761
David Hildenbrand66b630d2015-11-26 14:11:19 +0100762/* rewind the psw and clear the vsie icpt, so we can retry execution */
763static void retry_vsie_icpt(struct vsie_page *vsie_page)
764{
765 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
766 int ilen = insn_length(scb_s->ipa >> 8);
767
768 /* take care of EXECUTE instructions */
769 if (scb_s->icptstatus & 1) {
770 ilen = (scb_s->icptstatus >> 4) & 0x6;
771 if (!ilen)
772 ilen = 4;
773 }
774 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
775 clear_vsie_icpt(vsie_page);
776}
777
778/*
779 * Try to shadow + enable the guest 2 provided facility list.
780 * Retry instruction execution if enabled for and provided by guest 2.
781 *
782 * Returns: - 0 if handled (retry or guest 2 icpt)
783 * - > 0 if control has to be given to guest 2
784 */
785static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
786{
787 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
788 __u32 fac = vsie_page->scb_o->fac & 0x7ffffff8U;
789
790 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
791 retry_vsie_icpt(vsie_page);
792 if (read_guest_real(vcpu, fac, &vsie_page->fac,
793 sizeof(vsie_page->fac)))
794 return set_validity_icpt(scb_s, 0x1090U);
795 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
796 }
797 return 0;
798}
799
David Hildenbranda3508fb2015-07-08 13:19:48 +0200800/*
801 * Run the vsie on a shadow scb and a shadow gmap, without any further
802 * sanity checks, handling SIE faults.
803 *
804 * Returns: - 0 everything went fine
805 * - > 0 if control has to be given to guest 2
806 * - < 0 if an error occurred
807 */
808static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
809{
810 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
811 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
QingFeng Haod52cd202017-06-07 12:11:18 +0200812 struct mcck_volatile_info *mcck_info;
813 struct sie_page *sie_page;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200814 int rc;
815
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200816 handle_last_fault(vcpu, vsie_page);
817
David Hildenbranda3508fb2015-07-08 13:19:48 +0200818 if (need_resched())
819 schedule();
820 if (test_cpu_flag(CIF_MCCK_PENDING))
821 s390_handle_mcck();
822
823 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
824 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200825 guest_enter_irqoff();
David Hildenbranda3508fb2015-07-08 13:19:48 +0200826 local_irq_enable();
827
828 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
829
830 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200831 guest_exit_irqoff();
David Hildenbranda3508fb2015-07-08 13:19:48 +0200832 local_irq_enable();
833 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
834
QingFeng Haod52cd202017-06-07 12:11:18 +0200835 if (rc == -EINTR) {
836 VCPU_EVENT(vcpu, 3, "%s", "machine check");
837 sie_page = container_of(scb_s, struct sie_page, sie_block);
838 mcck_info = &sie_page->mcck_info;
839 kvm_s390_reinject_machine_check(vcpu, mcck_info);
840 return 0;
841 }
842
David Hildenbranda3508fb2015-07-08 13:19:48 +0200843 if (rc > 0)
844 rc = 0; /* we could still have an icpt */
845 else if (rc == -EFAULT)
846 return handle_fault(vcpu, vsie_page);
847
848 switch (scb_s->icptcode) {
David Hildenbrand66b630d2015-11-26 14:11:19 +0100849 case ICPT_INST:
850 if (scb_s->ipa == 0xb2b0)
851 rc = handle_stfle(vcpu, vsie_page);
852 break;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200853 case ICPT_STOP:
854 /* stop not requested by g2 - must have been a kick */
855 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
856 clear_vsie_icpt(vsie_page);
857 break;
858 case ICPT_VALIDITY:
859 if ((scb_s->ipa & 0xf000) != 0xf000)
860 scb_s->ipa += 0x1000;
861 break;
862 }
863 return rc;
864}
865
866static void release_gmap_shadow(struct vsie_page *vsie_page)
867{
868 if (vsie_page->gmap)
869 gmap_put(vsie_page->gmap);
870 WRITE_ONCE(vsie_page->gmap, NULL);
David Hildenbrand06d68a62016-04-22 13:50:09 +0200871 prefix_unmapped(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200872}
873
874static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
875 struct vsie_page *vsie_page)
876{
877 unsigned long asce;
878 union ctlreg0 cr0;
879 struct gmap *gmap;
880 int edat;
881
882 asce = vcpu->arch.sie_block->gcr[1];
883 cr0.val = vcpu->arch.sie_block->gcr[0];
884 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
885 edat += edat && test_kvm_facility(vcpu->kvm, 78);
886
David Hildenbrand06d68a62016-04-22 13:50:09 +0200887 /*
888 * ASCE or EDAT could have changed since last icpt, or the gmap
889 * we're holding has been unshadowed. If the gmap is still valid,
890 * we can safely reuse it.
891 */
892 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
893 return 0;
894
895 /* release the old shadow - if any, and mark the prefix as unmapped */
896 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200897 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
898 if (IS_ERR(gmap))
899 return PTR_ERR(gmap);
900 gmap->private = vcpu->kvm;
901 WRITE_ONCE(vsie_page->gmap, gmap);
902 return 0;
903}
904
905/*
David Hildenbrandadbf1692016-05-27 22:03:52 +0200906 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
907 */
908static void register_shadow_scb(struct kvm_vcpu *vcpu,
909 struct vsie_page *vsie_page)
910{
David Hildenbrand91473b42015-10-29 10:30:36 +0100911 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
912
David Hildenbrandadbf1692016-05-27 22:03:52 +0200913 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
David Hildenbrandb917ae52015-07-07 20:39:35 +0200914 /*
915 * External calls have to lead to a kick of the vcpu and
916 * therefore the vsie -> Simulate Wait state.
917 */
918 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand91473b42015-10-29 10:30:36 +0100919 /*
920 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
921 * automatically be adjusted on tod clock changes via kvm_sync_clock.
922 */
923 preempt_disable();
924 scb_s->epoch += vcpu->kvm->arch.epoch;
Collin L. Walling8fa16962016-07-26 15:29:44 -0400925
926 if (scb_s->ecd & ECD_MEF) {
927 scb_s->epdx += vcpu->kvm->arch.epdx;
928 if (scb_s->epoch < vcpu->kvm->arch.epoch)
929 scb_s->epdx += 1;
930 }
931
David Hildenbrand91473b42015-10-29 10:30:36 +0100932 preempt_enable();
David Hildenbrandadbf1692016-05-27 22:03:52 +0200933}
934
935/*
936 * Unregister a shadow scb from a VCPU.
937 */
938static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
939{
David Hildenbrandb917ae52015-07-07 20:39:35 +0200940 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
David Hildenbrandadbf1692016-05-27 22:03:52 +0200941 WRITE_ONCE(vcpu->arch.vsie_block, NULL);
942}
943
944/*
David Hildenbranda3508fb2015-07-08 13:19:48 +0200945 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
946 * prefix pages and faults.
947 *
948 * Returns: - 0 if no errors occurred
949 * - > 0 if control has to be given to guest 2
950 * - -ENOMEM if out of memory
951 */
952static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
953{
954 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
955 int rc = 0;
956
957 while (1) {
958 rc = acquire_gmap_shadow(vcpu, vsie_page);
959 if (!rc)
960 rc = map_prefix(vcpu, vsie_page);
961 if (!rc) {
962 gmap_enable(vsie_page->gmap);
963 update_intervention_requests(vsie_page);
964 rc = do_vsie_run(vcpu, vsie_page);
965 gmap_enable(vcpu->arch.gmap);
966 }
David Hildenbrandadbf1692016-05-27 22:03:52 +0200967 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200968
969 if (rc == -EAGAIN)
970 rc = 0;
971 if (rc || scb_s->icptcode || signal_pending(current) ||
972 kvm_s390_vcpu_has_irq(vcpu, 0))
973 break;
Heiko Carstens0b925152017-01-02 08:51:02 +0100974 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200975
976 if (rc == -EFAULT) {
977 /*
978 * Addressing exceptions are always presentes as intercepts.
979 * As addressing exceptions are suppressing and our guest 3 PSW
980 * points at the responsible instruction, we have to
981 * forward the PSW and set the ilc. If we can't read guest 3
982 * instruction, we can use an arbitrary ilc. Let's always use
983 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
984 * memory. (we could also fake the shadow so the hardware
985 * handles it).
986 */
987 scb_s->icptcode = ICPT_PROGI;
988 scb_s->iprcc = PGM_ADDRESSING;
989 scb_s->pgmilc = 4;
990 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
991 }
992 return rc;
993}
994
995/*
996 * Get or create a vsie page for a scb address.
997 *
998 * Returns: - address of a vsie page (cached or new one)
999 * - NULL if the same scb address is already used by another VCPU
1000 * - ERR_PTR(-ENOMEM) if out of memory
1001 */
1002static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
1003{
1004 struct vsie_page *vsie_page;
1005 struct page *page;
1006 int nr_vcpus;
1007
1008 rcu_read_lock();
1009 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1010 rcu_read_unlock();
1011 if (page) {
1012 if (page_ref_inc_return(page) == 2)
1013 return page_to_virt(page);
1014 page_ref_dec(page);
1015 }
1016
1017 /*
1018 * We want at least #online_vcpus shadows, so every VCPU can execute
1019 * the VSIE in parallel.
1020 */
1021 nr_vcpus = atomic_read(&kvm->online_vcpus);
1022
1023 mutex_lock(&kvm->arch.vsie.mutex);
1024 if (kvm->arch.vsie.page_count < nr_vcpus) {
David Hildenbrand66b630d2015-11-26 14:11:19 +01001025 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001026 if (!page) {
1027 mutex_unlock(&kvm->arch.vsie.mutex);
1028 return ERR_PTR(-ENOMEM);
1029 }
1030 page_ref_inc(page);
1031 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1032 kvm->arch.vsie.page_count++;
1033 } else {
1034 /* reuse an existing entry that belongs to nobody */
1035 while (true) {
1036 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1037 if (page_ref_inc_return(page) == 2)
1038 break;
1039 page_ref_dec(page);
1040 kvm->arch.vsie.next++;
1041 kvm->arch.vsie.next %= nr_vcpus;
1042 }
1043 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1044 }
1045 page->index = addr;
1046 /* double use of the same address */
1047 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1048 page_ref_dec(page);
1049 mutex_unlock(&kvm->arch.vsie.mutex);
1050 return NULL;
1051 }
1052 mutex_unlock(&kvm->arch.vsie.mutex);
1053
1054 vsie_page = page_to_virt(page);
1055 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
David Hildenbrand06d68a62016-04-22 13:50:09 +02001056 release_gmap_shadow(vsie_page);
David Hildenbrand1b7029b2015-07-08 13:25:31 +02001057 vsie_page->fault_addr = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +02001058 vsie_page->scb_s.ihcpu = 0xffffU;
1059 return vsie_page;
1060}
1061
1062/* put a vsie page acquired via get_vsie_page */
1063static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1064{
1065 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1066
1067 page_ref_dec(page);
1068}
1069
1070int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1071{
1072 struct vsie_page *vsie_page;
1073 unsigned long scb_addr;
1074 int rc;
1075
1076 vcpu->stat.instruction_sie++;
1077 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1078 return -EOPNOTSUPP;
1079 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1080 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1081
1082 BUILD_BUG_ON(sizeof(struct vsie_page) != 4096);
1083 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1084
1085 /* 512 byte alignment */
1086 if (unlikely(scb_addr & 0x1ffUL))
1087 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1088
1089 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1090 return 0;
1091
1092 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1093 if (IS_ERR(vsie_page))
1094 return PTR_ERR(vsie_page);
1095 else if (!vsie_page)
1096 /* double use of sie control block - simply do nothing */
1097 return 0;
1098
1099 rc = pin_scb(vcpu, vsie_page, scb_addr);
1100 if (rc)
1101 goto out_put;
1102 rc = shadow_scb(vcpu, vsie_page);
1103 if (rc)
1104 goto out_unpin_scb;
1105 rc = pin_blocks(vcpu, vsie_page);
1106 if (rc)
1107 goto out_unshadow;
David Hildenbrandadbf1692016-05-27 22:03:52 +02001108 register_shadow_scb(vcpu, vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001109 rc = vsie_run(vcpu, vsie_page);
David Hildenbrandadbf1692016-05-27 22:03:52 +02001110 unregister_shadow_scb(vcpu);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001111 unpin_blocks(vcpu, vsie_page);
1112out_unshadow:
1113 unshadow_scb(vcpu, vsie_page);
1114out_unpin_scb:
1115 unpin_scb(vcpu, vsie_page, scb_addr);
1116out_put:
1117 put_vsie_page(vcpu->kvm, vsie_page);
1118
1119 return rc < 0 ? rc : 0;
1120}
1121
1122/* Init the vsie data structures. To be called when a vm is initialized. */
1123void kvm_s390_vsie_init(struct kvm *kvm)
1124{
1125 mutex_init(&kvm->arch.vsie.mutex);
1126 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1127}
1128
1129/* Destroy the vsie data structures. To be called when a vm is destroyed. */
1130void kvm_s390_vsie_destroy(struct kvm *kvm)
1131{
David Hildenbrand06d68a62016-04-22 13:50:09 +02001132 struct vsie_page *vsie_page;
David Hildenbranda3508fb2015-07-08 13:19:48 +02001133 struct page *page;
1134 int i;
1135
1136 mutex_lock(&kvm->arch.vsie.mutex);
1137 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1138 page = kvm->arch.vsie.pages[i];
1139 kvm->arch.vsie.pages[i] = NULL;
David Hildenbrand06d68a62016-04-22 13:50:09 +02001140 vsie_page = page_to_virt(page);
1141 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001142 /* free the radix tree entry */
1143 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1144 __free_page(page);
1145 }
1146 kvm->arch.vsie.page_count = 0;
1147 mutex_unlock(&kvm->arch.vsie.mutex);
1148}
David Hildenbrandadbf1692016-05-27 22:03:52 +02001149
1150void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1151{
1152 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1153
1154 /*
1155 * Even if the VCPU lets go of the shadow sie block reference, it is
1156 * still valid in the cache. So we can safely kick it.
1157 */
1158 if (scb) {
1159 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1160 if (scb->prog0c & PROG_IN_SIE)
1161 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);
1162 }
1163}