blob: ec772700ff9659350543534272a92eb531bbd181 [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 *
David Hildenbranda3508fb2015-07-08 13:19:48 +02007 * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
8 */
9#include <linux/vmalloc.h>
10#include <linux/kvm_host.h>
11#include <linux/bug.h>
12#include <linux/list.h>
13#include <linux/bitmap.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010014#include <linux/sched/signal.h>
15
David Hildenbranda3508fb2015-07-08 13:19:48 +020016#include <asm/gmap.h>
17#include <asm/mmu_context.h>
18#include <asm/sclp.h>
19#include <asm/nmi.h>
David Hildenbrand66b630d2015-11-26 14:11:19 +010020#include <asm/dis.h>
David Hildenbranda3508fb2015-07-08 13:19:48 +020021#include "kvm-s390.h"
22#include "gaccess.h"
23
24struct vsie_page {
25 struct kvm_s390_sie_block scb_s; /* 0x0000 */
QingFeng Haod52cd202017-06-07 12:11:18 +020026 /*
27 * the backup info for machine check. ensure it's at
28 * the same offset as that in struct sie_page!
29 */
30 struct mcck_volatile_info mcck_info; /* 0x0200 */
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +010031 /*
32 * The pinned original scb. Be aware that other VCPUs can modify
33 * it while we read from it. Values that are used for conditions or
34 * are reused conditionally, should be accessed via READ_ONCE.
35 */
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 */
David Hildenbrand15e50202018-01-16 18:15:26 +010041 /* calculated guest addresses of satellite control blocks */
42 gpa_t sca_gpa; /* 0x0230 */
43 gpa_t itdba_gpa; /* 0x0238 */
44 gpa_t gvrd_gpa; /* 0x0240 */
45 gpa_t riccbd_gpa; /* 0x0248 */
46 gpa_t sdnx_gpa; /* 0x0250 */
47 __u8 reserved[0x0700 - 0x0258]; /* 0x0258 */
David Hildenbrandbbeaa582015-11-26 13:11:42 +010048 struct kvm_s390_crypto_cb crycb; /* 0x0700 */
David Hildenbrand66b630d2015-11-26 14:11:19 +010049 __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */
Martin Schwidefsky1cae0252017-06-21 16:49:15 +020050};
David Hildenbranda3508fb2015-07-08 13:19:48 +020051
52/* trigger a validity icpt for the given scb */
53static int set_validity_icpt(struct kvm_s390_sie_block *scb,
54 __u16 reason_code)
55{
56 scb->ipa = 0x1000;
57 scb->ipb = ((__u32) reason_code) << 16;
58 scb->icptcode = ICPT_VALIDITY;
59 return 1;
60}
61
62/* mark the prefix as unmapped, this will block the VSIE */
63static void prefix_unmapped(struct vsie_page *vsie_page)
64{
65 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
66}
67
68/* mark the prefix as unmapped and wait until the VSIE has been left */
69static void prefix_unmapped_sync(struct vsie_page *vsie_page)
70{
71 prefix_unmapped(vsie_page);
72 if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
73 atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
74 while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
75 cpu_relax();
76}
77
78/* mark the prefix as mapped, this will allow the VSIE to run */
79static void prefix_mapped(struct vsie_page *vsie_page)
80{
81 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
82}
83
David Hildenbrand06d68a62016-04-22 13:50:09 +020084/* test if the prefix is mapped into the gmap shadow */
85static int prefix_is_mapped(struct vsie_page *vsie_page)
86{
87 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
88}
David Hildenbranda3508fb2015-07-08 13:19:48 +020089
90/* copy the updated intervention request bits into the shadow scb */
91static void update_intervention_requests(struct vsie_page *vsie_page)
92{
93 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
94 int cpuflags;
95
96 cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
97 atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
98 atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
99}
100
101/* shadow (filter and validate) the cpuflags */
102static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
103{
104 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
105 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
106 int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
107
108 /* we don't allow ESA/390 guests */
109 if (!(cpuflags & CPUSTAT_ZARCH))
110 return set_validity_icpt(scb_s, 0x0001U);
111
112 if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
113 return set_validity_icpt(scb_s, 0x0001U);
114 else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
115 return set_validity_icpt(scb_s, 0x0007U);
116
117 /* intervention requests will be set later */
118 newflags = CPUSTAT_ZARCH;
David Hildenbrand535ef812016-02-12 12:24:20 +0100119 if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8))
120 newflags |= CPUSTAT_GED;
121 if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) {
122 if (cpuflags & CPUSTAT_GED)
123 return set_validity_icpt(scb_s, 0x0001U);
124 newflags |= CPUSTAT_GED2;
125 }
David Hildenbrand77d18f62015-11-24 16:32:35 +0100126 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE))
127 newflags |= cpuflags & CPUSTAT_P;
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100128 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS))
129 newflags |= cpuflags & CPUSTAT_SM;
David Hildenbrand7fd7f392015-11-24 16:56:23 +0100130 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS))
131 newflags |= cpuflags & CPUSTAT_IBS;
Farhan Ali730cd632017-02-24 16:12:56 -0500132 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_KSS))
133 newflags |= cpuflags & CPUSTAT_KSS;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200134
135 atomic_set(&scb_s->cpuflags, newflags);
136 return 0;
137}
138
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100139/*
140 * Create a shadow copy of the crycb block and setup key wrapping, if
141 * requested for guest 3 and enabled for guest 2.
142 *
143 * We only accept format-1 (no AP in g2), but convert it into format-2
144 * There is nothing to do for format-0.
145 *
146 * Returns: - 0 if shadowed or nothing to do
147 * - > 0 if control has to be given to guest 2
148 */
149static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
150{
151 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
152 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100153 const uint32_t crycbd_o = READ_ONCE(scb_o->crycbd);
154 const u32 crycb_addr = crycbd_o & 0x7ffffff8U;
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100155 unsigned long *b1, *b2;
156 u8 ecb3_flags;
157
158 scb_s->crycbd = 0;
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100159 if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100160 return 0;
161 /* format-1 is supported with message-security-assist extension 3 */
162 if (!test_kvm_facility(vcpu->kvm, 76))
163 return 0;
164 /* we may only allow it if enabled for guest 2 */
165 ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
166 (ECB3_AES | ECB3_DEA);
167 if (!ecb3_flags)
168 return 0;
169
170 if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
171 return set_validity_icpt(scb_s, 0x003CU);
172 else if (!crycb_addr)
173 return set_validity_icpt(scb_s, 0x0039U);
174
175 /* copy only the wrapping keys */
176 if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
177 return set_validity_icpt(scb_s, 0x0035U);
178
179 scb_s->ecb3 |= ecb3_flags;
180 scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
181 CRYCB_FORMAT2;
182
183 /* xor both blocks in one run */
184 b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
185 b2 = (unsigned long *)
186 vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
187 /* as 56%8 == 0, bitmap_xor won't overwrite any data */
188 bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
189 return 0;
190}
191
David Hildenbrand35736022016-02-19 10:11:24 +0100192/* shadow (round up/down) the ibc to avoid validity icpt */
193static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
194{
195 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
196 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100197 /* READ_ONCE does not work on bitfields - use a temporary variable */
198 const uint32_t __new_ibc = scb_o->ibc;
199 const uint32_t new_ibc = READ_ONCE(__new_ibc) & 0x0fffU;
David Hildenbrand35736022016-02-19 10:11:24 +0100200 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
201
202 scb_s->ibc = 0;
203 /* ibc installed in g2 and requested for g3 */
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100204 if (vcpu->kvm->arch.model.ibc && new_ibc) {
205 scb_s->ibc = new_ibc;
David Hildenbrand35736022016-02-19 10:11:24 +0100206 /* takte care of the minimum ibc level of the machine */
207 if (scb_s->ibc < min_ibc)
208 scb_s->ibc = min_ibc;
209 /* take care of the maximum ibc level set for the guest */
210 if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
211 scb_s->ibc = vcpu->kvm->arch.model.ibc;
212 }
213}
214
David Hildenbranda3508fb2015-07-08 13:19:48 +0200215/* unshadow the scb, copying parameters back to the real scb */
216static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
217{
218 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
219 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
220
221 /* interception */
222 scb_o->icptcode = scb_s->icptcode;
223 scb_o->icptstatus = scb_s->icptstatus;
224 scb_o->ipa = scb_s->ipa;
225 scb_o->ipb = scb_s->ipb;
226 scb_o->gbea = scb_s->gbea;
227
228 /* timer */
229 scb_o->cputm = scb_s->cputm;
230 scb_o->ckc = scb_s->ckc;
231 scb_o->todpr = scb_s->todpr;
232
233 /* guest state */
234 scb_o->gpsw = scb_s->gpsw;
235 scb_o->gg14 = scb_s->gg14;
236 scb_o->gg15 = scb_s->gg15;
237 memcpy(scb_o->gcr, scb_s->gcr, 128);
238 scb_o->pp = scb_s->pp;
239
Christian Borntraeger35b3fde2018-01-17 14:44:34 +0100240 /* branch prediction */
241 if (test_kvm_facility(vcpu->kvm, 82)) {
242 scb_o->fpf &= ~FPF_BPBC;
243 scb_o->fpf |= scb_s->fpf & FPF_BPBC;
244 }
245
David Hildenbranda3508fb2015-07-08 13:19:48 +0200246 /* interrupt intercept */
247 switch (scb_s->icptcode) {
248 case ICPT_PROGI:
249 case ICPT_INSTPROGI:
250 case ICPT_EXTINT:
251 memcpy((void *)((u64)scb_o + 0xc0),
252 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
253 break;
254 case ICPT_PARTEXEC:
255 /* MVPG only */
256 memcpy((void *)((u64)scb_o + 0xc0),
257 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
258 break;
259 }
260
261 if (scb_s->ihcpu != 0xffffU)
262 scb_o->ihcpu = scb_s->ihcpu;
263}
264
265/*
266 * Setup the shadow scb by copying and checking the relevant parts of the g2
267 * provided scb.
268 *
269 * Returns: - 0 if the scb has been shadowed
270 * - > 0 if control has to be given to guest 2
271 */
272static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
273{
274 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
275 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100276 /* READ_ONCE does not work on bitfields - use a temporary variable */
277 const uint32_t __new_prefix = scb_o->prefix;
278 const uint32_t new_prefix = READ_ONCE(__new_prefix);
279 const bool wants_tx = READ_ONCE(scb_o->ecb) & ECB_TE;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100280 bool had_tx = scb_s->ecb & ECB_TE;
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100281 unsigned long new_mso = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200282 int rc;
283
284 /* make sure we don't have any leftovers when reusing the scb */
285 scb_s->icptcode = 0;
286 scb_s->eca = 0;
287 scb_s->ecb = 0;
288 scb_s->ecb2 = 0;
289 scb_s->ecb3 = 0;
290 scb_s->ecd = 0;
David Hildenbrand66b630d2015-11-26 14:11:19 +0100291 scb_s->fac = 0;
Christian Borntraeger35b3fde2018-01-17 14:44:34 +0100292 scb_s->fpf = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200293
294 rc = prepare_cpuflags(vcpu, vsie_page);
295 if (rc)
296 goto out;
297
298 /* timer */
299 scb_s->cputm = scb_o->cputm;
300 scb_s->ckc = scb_o->ckc;
301 scb_s->todpr = scb_o->todpr;
302 scb_s->epoch = scb_o->epoch;
303
304 /* guest state */
305 scb_s->gpsw = scb_o->gpsw;
306 scb_s->gg14 = scb_o->gg14;
307 scb_s->gg15 = scb_o->gg15;
308 memcpy(scb_s->gcr, scb_o->gcr, 128);
309 scb_s->pp = scb_o->pp;
310
311 /* interception / execution handling */
312 scb_s->gbea = scb_o->gbea;
313 scb_s->lctl = scb_o->lctl;
314 scb_s->svcc = scb_o->svcc;
315 scb_s->ictl = scb_o->ictl;
316 /*
317 * SKEY handling functions can't deal with false setting of PTE invalid
318 * bits. Therefore we cannot provide interpretation and would later
319 * have to provide own emulation handlers.
320 */
Farhan Ali730cd632017-02-24 16:12:56 -0500321 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
322 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
323
David Hildenbranda3508fb2015-07-08 13:19:48 +0200324 scb_s->icpua = scb_o->icpua;
325
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100326 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100327 new_mso = READ_ONCE(scb_o->mso) & 0xfffffffffff00000UL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200328 /* if the hva of the prefix changes, we have to remap the prefix */
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100329 if (scb_s->mso != new_mso || scb_s->prefix != new_prefix)
David Hildenbrand06d68a62016-04-22 13:50:09 +0200330 prefix_unmapped(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200331 /* SIE will do mso/msl validity and exception checks for us */
332 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200333 scb_s->mso = new_mso;
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100334 scb_s->prefix = new_prefix;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200335
336 /* We have to definetly flush the tlb if this scb never ran */
337 if (scb_s->ihcpu != 0xffffU)
338 scb_s->ihcpu = scb_o->ihcpu;
339
340 /* MVPG and Protection Exception Interpretation are always available */
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100341 scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
David Hildenbrand4ceafa92015-11-27 12:34:28 +0100342 /* Host-protection-interruption introduced with ESOP */
343 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100344 scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100345 /* transactional execution */
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100346 if (test_kvm_facility(vcpu->kvm, 73) && wants_tx) {
David Hildenbrand166ecb32015-11-25 11:13:32 +0100347 /* remap the prefix is tx is toggled on */
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100348 if (!had_tx)
David Hildenbrand166ecb32015-11-25 11:13:32 +0100349 prefix_unmapped(vsie_page);
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100350 scb_s->ecb |= ECB_TE;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100351 }
Christian Borntraeger35b3fde2018-01-17 14:44:34 +0100352 /* branch prediction */
353 if (test_kvm_facility(vcpu->kvm, 82))
354 scb_s->fpf |= scb_o->fpf & FPF_BPBC;
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100355 /* SIMD */
356 if (test_kvm_facility(vcpu->kvm, 129)) {
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100357 scb_s->eca |= scb_o->eca & ECA_VX;
358 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100359 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100360 /* Run-time-Instrumentation */
361 if (test_kvm_facility(vcpu->kvm, 64))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100362 scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
Janosch Frankcd1836f2016-08-04 09:57:36 +0200363 /* Instruction Execution Prevention */
364 if (test_kvm_facility(vcpu->kvm, 130))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100365 scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100366 /* Guarded Storage */
367 if (test_kvm_facility(vcpu->kvm, 133)) {
368 scb_s->ecb |= scb_o->ecb & ECB_GS;
369 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
370 }
David Hildenbrand0615a322015-11-25 09:59:49 +0100371 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100372 scb_s->eca |= scb_o->eca & ECA_SII;
David Hildenbrand5630a8e2015-11-24 16:53:51 +0100373 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100374 scb_s->eca |= scb_o->eca & ECA_IB;
David Hildenbrand13ee3f62015-11-24 16:54:37 +0100375 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100376 scb_s->eca |= scb_o->eca & ECA_CEI;
Collin L. Walling8fa16962016-07-26 15:29:44 -0400377 /* Epoch Extension */
378 if (test_kvm_facility(vcpu->kvm, 139))
379 scb_s->ecd |= scb_o->ecd & ECD_MEF;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200380
David Hildenbrand35736022016-02-19 10:11:24 +0100381 prepare_ibc(vcpu, vsie_page);
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100382 rc = shadow_crycb(vcpu, vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200383out:
384 if (rc)
385 unshadow_scb(vcpu, vsie_page);
386 return rc;
387}
388
389void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
390 unsigned long end)
391{
392 struct kvm *kvm = gmap->private;
393 struct vsie_page *cur;
394 unsigned long prefix;
395 struct page *page;
396 int i;
397
398 if (!gmap_is_shadow(gmap))
399 return;
400 if (start >= 1UL << 31)
401 /* We are only interested in prefix pages */
402 return;
403
404 /*
405 * Only new shadow blocks are added to the list during runtime,
406 * therefore we can safely reference them all the time.
407 */
408 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
409 page = READ_ONCE(kvm->arch.vsie.pages[i]);
410 if (!page)
411 continue;
412 cur = page_to_virt(page);
413 if (READ_ONCE(cur->gmap) != gmap)
414 continue;
415 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
416 /* with mso/msl, the prefix lies at an offset */
417 prefix += cur->scb_s.mso;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100418 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
David Hildenbranda3508fb2015-07-08 13:19:48 +0200419 prefix_unmapped_sync(cur);
420 }
421}
422
423/*
David Hildenbrand166ecb32015-11-25 11:13:32 +0100424 * Map the first prefix page and if tx is enabled also the second prefix page.
David Hildenbranda3508fb2015-07-08 13:19:48 +0200425 *
426 * The prefix will be protected, a gmap notifier will inform about unmaps.
427 * The shadow scb must not be executed until the prefix is remapped, this is
428 * guaranteed by properly handling PROG_REQUEST.
429 *
430 * Returns: - 0 on if successfully mapped or already mapped
431 * - > 0 if control has to be given to guest 2
432 * - -EAGAIN if the caller can retry immediately
433 * - -ENOMEM if out of memory
434 */
435static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
436{
437 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
438 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
439 int rc;
440
David Hildenbrand06d68a62016-04-22 13:50:09 +0200441 if (prefix_is_mapped(vsie_page))
442 return 0;
443
David Hildenbranda3508fb2015-07-08 13:19:48 +0200444 /* mark it as mapped so we can catch any concurrent unmappers */
445 prefix_mapped(vsie_page);
446
447 /* with mso/msl, the prefix lies at offset *mso* */
448 prefix += scb_s->mso;
449
450 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100451 if (!rc && (scb_s->ecb & ECB_TE))
David Hildenbrand166ecb32015-11-25 11:13:32 +0100452 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
453 prefix + PAGE_SIZE);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200454 /*
455 * We don't have to mprotect, we will be called for all unshadows.
456 * SIE will detect if protection applies and trigger a validity.
457 */
458 if (rc)
459 prefix_unmapped(vsie_page);
460 if (rc > 0 || rc == -EFAULT)
461 rc = set_validity_icpt(scb_s, 0x0037U);
462 return rc;
463}
464
465/*
466 * Pin the guest page given by gpa and set hpa to the pinned host address.
467 * Will always be pinned writable.
468 *
469 * Returns: - 0 on success
470 * - -EINVAL if the gpa is not valid guest storage
David Hildenbranda3508fb2015-07-08 13:19:48 +0200471 */
472static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
473{
474 struct page *page;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200475
David Hildenbrandf7a65092017-09-01 17:11:43 +0200476 page = gfn_to_page(kvm, gpa_to_gfn(gpa));
477 if (is_error_page(page))
David Hildenbranda3508fb2015-07-08 13:19:48 +0200478 return -EINVAL;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200479 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
480 return 0;
481}
482
483/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
484static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
485{
David Hildenbrandf7a65092017-09-01 17:11:43 +0200486 kvm_release_pfn_dirty(hpa >> PAGE_SHIFT);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200487 /* mark the page always as dirty for migration */
488 mark_page_dirty(kvm, gpa_to_gfn(gpa));
489}
490
491/* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
492static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
493{
David Hildenbranda3508fb2015-07-08 13:19:48 +0200494 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
495 hpa_t hpa;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200496
497 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
498 if (hpa) {
David Hildenbrand15e50202018-01-16 18:15:26 +0100499 unpin_guest_page(vcpu->kvm, vsie_page->sca_gpa, hpa);
500 vsie_page->sca_gpa = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200501 scb_s->scaol = 0;
502 scb_s->scaoh = 0;
503 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100504
505 hpa = scb_s->itdba;
506 if (hpa) {
David Hildenbrand15e50202018-01-16 18:15:26 +0100507 unpin_guest_page(vcpu->kvm, vsie_page->itdba_gpa, hpa);
508 vsie_page->itdba_gpa = 0;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100509 scb_s->itdba = 0;
510 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100511
512 hpa = scb_s->gvrd;
513 if (hpa) {
David Hildenbrand15e50202018-01-16 18:15:26 +0100514 unpin_guest_page(vcpu->kvm, vsie_page->gvrd_gpa, hpa);
515 vsie_page->gvrd_gpa = 0;
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100516 scb_s->gvrd = 0;
517 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100518
519 hpa = scb_s->riccbd;
520 if (hpa) {
David Hildenbrand15e50202018-01-16 18:15:26 +0100521 unpin_guest_page(vcpu->kvm, vsie_page->riccbd_gpa, hpa);
522 vsie_page->riccbd_gpa = 0;
David Hildenbrand588438c2016-01-26 12:51:06 +0100523 scb_s->riccbd = 0;
524 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100525
526 hpa = scb_s->sdnxo;
527 if (hpa) {
David Hildenbrand15e50202018-01-16 18:15:26 +0100528 unpin_guest_page(vcpu->kvm, vsie_page->sdnx_gpa, hpa);
529 vsie_page->sdnx_gpa = 0;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100530 scb_s->sdnxo = 0;
531 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200532}
533
534/*
535 * Instead of shadowing some blocks, we can simply forward them because the
536 * addresses in the scb are 64 bit long.
537 *
538 * This works as long as the data lies in one page. If blocks ever exceed one
539 * page, we have to fall back to shadowing.
540 *
541 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
542 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
543 *
544 * Returns: - 0 if all blocks were pinned.
545 * - > 0 if control has to be given to guest 2
546 * - -ENOMEM if out of memory
547 */
548static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
549{
550 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
551 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
552 hpa_t hpa;
553 gpa_t gpa;
554 int rc = 0;
555
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100556 gpa = READ_ONCE(scb_o->scaol) & ~0xfUL;
David Hildenbrand19c439b2015-11-25 11:02:26 +0100557 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100558 gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200559 if (gpa) {
560 if (!(gpa & ~0x1fffUL))
561 rc = set_validity_icpt(scb_s, 0x0038U);
562 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
563 rc = set_validity_icpt(scb_s, 0x0011U);
564 else if ((gpa & PAGE_MASK) !=
565 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
566 rc = set_validity_icpt(scb_s, 0x003bU);
567 if (!rc) {
568 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200569 if (rc)
David Hildenbranda3508fb2015-07-08 13:19:48 +0200570 rc = set_validity_icpt(scb_s, 0x0034U);
571 }
572 if (rc)
573 goto unpin;
David Hildenbrand15e50202018-01-16 18:15:26 +0100574 vsie_page->sca_gpa = gpa;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200575 scb_s->scaoh = (u32)((u64)hpa >> 32);
576 scb_s->scaol = (u32)(u64)hpa;
577 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100578
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100579 gpa = READ_ONCE(scb_o->itdba) & ~0xffUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100580 if (gpa && (scb_s->ecb & ECB_TE)) {
David Hildenbrand166ecb32015-11-25 11:13:32 +0100581 if (!(gpa & ~0x1fffU)) {
582 rc = set_validity_icpt(scb_s, 0x0080U);
583 goto unpin;
584 }
585 /* 256 bytes cannot cross page boundaries */
586 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200587 if (rc) {
David Hildenbrand166ecb32015-11-25 11:13:32 +0100588 rc = set_validity_icpt(scb_s, 0x0080U);
David Hildenbrand166ecb32015-11-25 11:13:32 +0100589 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200590 }
David Hildenbrand15e50202018-01-16 18:15:26 +0100591 vsie_page->itdba_gpa = gpa;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100592 scb_s->itdba = hpa;
593 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100594
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100595 gpa = READ_ONCE(scb_o->gvrd) & ~0x1ffUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100596 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100597 if (!(gpa & ~0x1fffUL)) {
598 rc = set_validity_icpt(scb_s, 0x1310U);
599 goto unpin;
600 }
601 /*
602 * 512 bytes vector registers cannot cross page boundaries
603 * if this block gets bigger, we have to shadow it.
604 */
605 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200606 if (rc) {
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100607 rc = set_validity_icpt(scb_s, 0x1310U);
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100608 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200609 }
David Hildenbrand15e50202018-01-16 18:15:26 +0100610 vsie_page->gvrd_gpa = gpa;
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100611 scb_s->gvrd = hpa;
612 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100613
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100614 gpa = READ_ONCE(scb_o->riccbd) & ~0x3fUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100615 if (gpa && (scb_s->ecb3 & ECB3_RI)) {
David Hildenbrand588438c2016-01-26 12:51:06 +0100616 if (!(gpa & ~0x1fffUL)) {
617 rc = set_validity_icpt(scb_s, 0x0043U);
618 goto unpin;
619 }
620 /* 64 bytes cannot cross page boundaries */
621 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200622 if (rc) {
David Hildenbrand588438c2016-01-26 12:51:06 +0100623 rc = set_validity_icpt(scb_s, 0x0043U);
David Hildenbrand588438c2016-01-26 12:51:06 +0100624 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200625 }
626 /* Validity 0x0044 will be checked by SIE */
David Hildenbrand15e50202018-01-16 18:15:26 +0100627 vsie_page->riccbd_gpa = gpa;
David Hildenbrand4d21cef32016-09-02 12:33:49 +0200628 scb_s->riccbd = hpa;
David Hildenbrand588438c2016-01-26 12:51:06 +0100629 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100630 if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
631 unsigned long sdnxc;
632
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100633 gpa = READ_ONCE(scb_o->sdnxo) & ~0xfUL;
634 sdnxc = READ_ONCE(scb_o->sdnxo) & 0xfUL;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100635 if (!gpa || !(gpa & ~0x1fffUL)) {
636 rc = set_validity_icpt(scb_s, 0x10b0U);
637 goto unpin;
638 }
639 if (sdnxc < 6 || sdnxc > 12) {
640 rc = set_validity_icpt(scb_s, 0x10b1U);
641 goto unpin;
642 }
643 if (gpa & ((1 << sdnxc) - 1)) {
644 rc = set_validity_icpt(scb_s, 0x10b2U);
645 goto unpin;
646 }
647 /* Due to alignment rules (checked above) this cannot
648 * cross page boundaries
649 */
650 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200651 if (rc) {
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100652 rc = set_validity_icpt(scb_s, 0x10b0U);
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100653 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200654 }
David Hildenbrand15e50202018-01-16 18:15:26 +0100655 vsie_page->sdnx_gpa = gpa;
Christian Borntraegerfe722d12017-04-07 14:23:13 +0200656 scb_s->sdnxo = hpa | sdnxc;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100657 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200658 return 0;
659unpin:
660 unpin_blocks(vcpu, vsie_page);
661 return rc;
662}
663
664/* unpin the scb provided by guest 2, marking it as dirty */
665static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
666 gpa_t gpa)
667{
668 hpa_t hpa = (hpa_t) vsie_page->scb_o;
669
670 if (hpa)
671 unpin_guest_page(vcpu->kvm, gpa, hpa);
672 vsie_page->scb_o = NULL;
673}
674
675/*
676 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
677 *
678 * Returns: - 0 if the scb was pinned.
679 * - > 0 if control has to be given to guest 2
David Hildenbranda3508fb2015-07-08 13:19:48 +0200680 */
681static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
682 gpa_t gpa)
683{
684 hpa_t hpa;
685 int rc;
686
687 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200688 if (rc) {
David Hildenbranda3508fb2015-07-08 13:19:48 +0200689 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200690 WARN_ON_ONCE(rc);
691 return 1;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200692 }
David Hildenbrandf7a65092017-09-01 17:11:43 +0200693 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
694 return 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200695}
696
697/*
698 * Inject a fault into guest 2.
699 *
700 * Returns: - > 0 if control has to be given to guest 2
701 * < 0 if an error occurred during injection.
702 */
703static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
704 bool write_flag)
705{
706 struct kvm_s390_pgm_info pgm = {
707 .code = code,
708 .trans_exc_code =
709 /* 0-51: virtual address */
710 (vaddr & 0xfffffffffffff000UL) |
711 /* 52-53: store / fetch */
712 (((unsigned int) !write_flag) + 1) << 10,
713 /* 62-63: asce id (alway primary == 0) */
714 .exc_access_id = 0, /* always primary */
715 .op_access_id = 0, /* not MVPG */
716 };
717 int rc;
718
719 if (code == PGM_PROTECTION)
720 pgm.trans_exc_code |= 0x4UL;
721
722 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
723 return rc ? rc : 1;
724}
725
726/*
727 * Handle a fault during vsie execution on a gmap shadow.
728 *
729 * Returns: - 0 if the fault was resolved
730 * - > 0 if control has to be given to guest 2
731 * - < 0 if an error occurred
732 */
733static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
734{
735 int rc;
736
737 if (current->thread.gmap_int_code == PGM_PROTECTION)
738 /* we can directly forward all protection exceptions */
739 return inject_fault(vcpu, PGM_PROTECTION,
740 current->thread.gmap_addr, 1);
741
742 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
743 current->thread.gmap_addr);
744 if (rc > 0) {
745 rc = inject_fault(vcpu, rc,
746 current->thread.gmap_addr,
747 current->thread.gmap_write_flag);
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200748 if (rc >= 0)
749 vsie_page->fault_addr = current->thread.gmap_addr;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200750 }
751 return rc;
752}
753
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200754/*
755 * Retry the previous fault that required guest 2 intervention. This avoids
756 * one superfluous SIE re-entry and direct exit.
757 *
758 * Will ignore any errors. The next SIE fault will do proper fault handling.
759 */
760static void handle_last_fault(struct kvm_vcpu *vcpu,
761 struct vsie_page *vsie_page)
762{
763 if (vsie_page->fault_addr)
764 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
765 vsie_page->fault_addr);
766 vsie_page->fault_addr = 0;
767}
768
David Hildenbranda3508fb2015-07-08 13:19:48 +0200769static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
770{
771 vsie_page->scb_s.icptcode = 0;
772}
773
David Hildenbrand66b630d2015-11-26 14:11:19 +0100774/* rewind the psw and clear the vsie icpt, so we can retry execution */
775static void retry_vsie_icpt(struct vsie_page *vsie_page)
776{
777 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
778 int ilen = insn_length(scb_s->ipa >> 8);
779
780 /* take care of EXECUTE instructions */
781 if (scb_s->icptstatus & 1) {
782 ilen = (scb_s->icptstatus >> 4) & 0x6;
783 if (!ilen)
784 ilen = 4;
785 }
786 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
787 clear_vsie_icpt(vsie_page);
788}
789
790/*
791 * Try to shadow + enable the guest 2 provided facility list.
792 * Retry instruction execution if enabled for and provided by guest 2.
793 *
794 * Returns: - 0 if handled (retry or guest 2 icpt)
795 * - > 0 if control has to be given to guest 2
796 */
797static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
798{
799 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100800 __u32 fac = READ_ONCE(vsie_page->scb_o->fac) & 0x7ffffff8U;
David Hildenbrand66b630d2015-11-26 14:11:19 +0100801
802 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
803 retry_vsie_icpt(vsie_page);
804 if (read_guest_real(vcpu, fac, &vsie_page->fac,
805 sizeof(vsie_page->fac)))
806 return set_validity_icpt(scb_s, 0x1090U);
807 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
808 }
809 return 0;
810}
811
David Hildenbranda3508fb2015-07-08 13:19:48 +0200812/*
813 * Run the vsie on a shadow scb and a shadow gmap, without any further
814 * sanity checks, handling SIE faults.
815 *
816 * Returns: - 0 everything went fine
817 * - > 0 if control has to be given to guest 2
818 * - < 0 if an error occurred
819 */
820static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
821{
822 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
823 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
824 int rc;
825
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200826 handle_last_fault(vcpu, vsie_page);
827
David Hildenbranda3508fb2015-07-08 13:19:48 +0200828 if (need_resched())
829 schedule();
830 if (test_cpu_flag(CIF_MCCK_PENDING))
831 s390_handle_mcck();
832
833 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
834 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200835 guest_enter_irqoff();
David Hildenbranda3508fb2015-07-08 13:19:48 +0200836 local_irq_enable();
837
838 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
839
840 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200841 guest_exit_irqoff();
David Hildenbranda3508fb2015-07-08 13:19:48 +0200842 local_irq_enable();
843 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
844
QingFeng Haod52cd202017-06-07 12:11:18 +0200845 if (rc == -EINTR) {
846 VCPU_EVENT(vcpu, 3, "%s", "machine check");
David Hildenbrandc95c8952017-08-30 18:06:02 +0200847 kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
QingFeng Haod52cd202017-06-07 12:11:18 +0200848 return 0;
849 }
850
David Hildenbranda3508fb2015-07-08 13:19:48 +0200851 if (rc > 0)
852 rc = 0; /* we could still have an icpt */
853 else if (rc == -EFAULT)
854 return handle_fault(vcpu, vsie_page);
855
856 switch (scb_s->icptcode) {
David Hildenbrand66b630d2015-11-26 14:11:19 +0100857 case ICPT_INST:
858 if (scb_s->ipa == 0xb2b0)
859 rc = handle_stfle(vcpu, vsie_page);
860 break;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200861 case ICPT_STOP:
862 /* stop not requested by g2 - must have been a kick */
863 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
864 clear_vsie_icpt(vsie_page);
865 break;
866 case ICPT_VALIDITY:
867 if ((scb_s->ipa & 0xf000) != 0xf000)
868 scb_s->ipa += 0x1000;
869 break;
870 }
871 return rc;
872}
873
874static void release_gmap_shadow(struct vsie_page *vsie_page)
875{
876 if (vsie_page->gmap)
877 gmap_put(vsie_page->gmap);
878 WRITE_ONCE(vsie_page->gmap, NULL);
David Hildenbrand06d68a62016-04-22 13:50:09 +0200879 prefix_unmapped(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200880}
881
882static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
883 struct vsie_page *vsie_page)
884{
885 unsigned long asce;
886 union ctlreg0 cr0;
887 struct gmap *gmap;
888 int edat;
889
890 asce = vcpu->arch.sie_block->gcr[1];
891 cr0.val = vcpu->arch.sie_block->gcr[0];
892 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
893 edat += edat && test_kvm_facility(vcpu->kvm, 78);
894
David Hildenbrand06d68a62016-04-22 13:50:09 +0200895 /*
896 * ASCE or EDAT could have changed since last icpt, or the gmap
897 * we're holding has been unshadowed. If the gmap is still valid,
898 * we can safely reuse it.
899 */
900 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
901 return 0;
902
903 /* release the old shadow - if any, and mark the prefix as unmapped */
904 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200905 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
906 if (IS_ERR(gmap))
907 return PTR_ERR(gmap);
908 gmap->private = vcpu->kvm;
909 WRITE_ONCE(vsie_page->gmap, gmap);
910 return 0;
911}
912
913/*
David Hildenbrandadbf1692016-05-27 22:03:52 +0200914 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
915 */
916static void register_shadow_scb(struct kvm_vcpu *vcpu,
917 struct vsie_page *vsie_page)
918{
David Hildenbrand91473b42015-10-29 10:30:36 +0100919 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
920
David Hildenbrandadbf1692016-05-27 22:03:52 +0200921 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
David Hildenbrandb917ae52015-07-07 20:39:35 +0200922 /*
923 * External calls have to lead to a kick of the vcpu and
924 * therefore the vsie -> Simulate Wait state.
925 */
David Hildenbrandef8f4f42018-01-23 18:05:29 +0100926 kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
David Hildenbrand91473b42015-10-29 10:30:36 +0100927 /*
928 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
929 * automatically be adjusted on tod clock changes via kvm_sync_clock.
930 */
931 preempt_disable();
932 scb_s->epoch += vcpu->kvm->arch.epoch;
Collin L. Walling8fa16962016-07-26 15:29:44 -0400933
934 if (scb_s->ecd & ECD_MEF) {
935 scb_s->epdx += vcpu->kvm->arch.epdx;
936 if (scb_s->epoch < vcpu->kvm->arch.epoch)
937 scb_s->epdx += 1;
938 }
939
David Hildenbrand91473b42015-10-29 10:30:36 +0100940 preempt_enable();
David Hildenbrandadbf1692016-05-27 22:03:52 +0200941}
942
943/*
944 * Unregister a shadow scb from a VCPU.
945 */
946static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
947{
David Hildenbrand9daecfc2018-01-23 18:05:30 +0100948 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_WAIT);
David Hildenbrandadbf1692016-05-27 22:03:52 +0200949 WRITE_ONCE(vcpu->arch.vsie_block, NULL);
950}
951
952/*
David Hildenbranda3508fb2015-07-08 13:19:48 +0200953 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
954 * prefix pages and faults.
955 *
956 * Returns: - 0 if no errors occurred
957 * - > 0 if control has to be given to guest 2
958 * - -ENOMEM if out of memory
959 */
960static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
961{
962 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
963 int rc = 0;
964
965 while (1) {
966 rc = acquire_gmap_shadow(vcpu, vsie_page);
967 if (!rc)
968 rc = map_prefix(vcpu, vsie_page);
969 if (!rc) {
970 gmap_enable(vsie_page->gmap);
971 update_intervention_requests(vsie_page);
972 rc = do_vsie_run(vcpu, vsie_page);
973 gmap_enable(vcpu->arch.gmap);
974 }
David Hildenbrandadbf1692016-05-27 22:03:52 +0200975 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200976
977 if (rc == -EAGAIN)
978 rc = 0;
979 if (rc || scb_s->icptcode || signal_pending(current) ||
980 kvm_s390_vcpu_has_irq(vcpu, 0))
981 break;
Heiko Carstens0b925152017-01-02 08:51:02 +0100982 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200983
984 if (rc == -EFAULT) {
985 /*
986 * Addressing exceptions are always presentes as intercepts.
987 * As addressing exceptions are suppressing and our guest 3 PSW
988 * points at the responsible instruction, we have to
989 * forward the PSW and set the ilc. If we can't read guest 3
990 * instruction, we can use an arbitrary ilc. Let's always use
991 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
992 * memory. (we could also fake the shadow so the hardware
993 * handles it).
994 */
995 scb_s->icptcode = ICPT_PROGI;
996 scb_s->iprcc = PGM_ADDRESSING;
997 scb_s->pgmilc = 4;
998 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
999 }
1000 return rc;
1001}
1002
1003/*
1004 * Get or create a vsie page for a scb address.
1005 *
1006 * Returns: - address of a vsie page (cached or new one)
1007 * - NULL if the same scb address is already used by another VCPU
1008 * - ERR_PTR(-ENOMEM) if out of memory
1009 */
1010static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
1011{
1012 struct vsie_page *vsie_page;
1013 struct page *page;
1014 int nr_vcpus;
1015
1016 rcu_read_lock();
1017 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1018 rcu_read_unlock();
1019 if (page) {
1020 if (page_ref_inc_return(page) == 2)
1021 return page_to_virt(page);
1022 page_ref_dec(page);
1023 }
1024
1025 /*
1026 * We want at least #online_vcpus shadows, so every VCPU can execute
1027 * the VSIE in parallel.
1028 */
1029 nr_vcpus = atomic_read(&kvm->online_vcpus);
1030
1031 mutex_lock(&kvm->arch.vsie.mutex);
1032 if (kvm->arch.vsie.page_count < nr_vcpus) {
David Hildenbrand66b630d2015-11-26 14:11:19 +01001033 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001034 if (!page) {
1035 mutex_unlock(&kvm->arch.vsie.mutex);
1036 return ERR_PTR(-ENOMEM);
1037 }
1038 page_ref_inc(page);
1039 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1040 kvm->arch.vsie.page_count++;
1041 } else {
1042 /* reuse an existing entry that belongs to nobody */
1043 while (true) {
1044 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1045 if (page_ref_inc_return(page) == 2)
1046 break;
1047 page_ref_dec(page);
1048 kvm->arch.vsie.next++;
1049 kvm->arch.vsie.next %= nr_vcpus;
1050 }
1051 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1052 }
1053 page->index = addr;
1054 /* double use of the same address */
1055 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1056 page_ref_dec(page);
1057 mutex_unlock(&kvm->arch.vsie.mutex);
1058 return NULL;
1059 }
1060 mutex_unlock(&kvm->arch.vsie.mutex);
1061
1062 vsie_page = page_to_virt(page);
1063 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
David Hildenbrand06d68a62016-04-22 13:50:09 +02001064 release_gmap_shadow(vsie_page);
David Hildenbrand1b7029b2015-07-08 13:25:31 +02001065 vsie_page->fault_addr = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +02001066 vsie_page->scb_s.ihcpu = 0xffffU;
1067 return vsie_page;
1068}
1069
1070/* put a vsie page acquired via get_vsie_page */
1071static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1072{
1073 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1074
1075 page_ref_dec(page);
1076}
1077
1078int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1079{
1080 struct vsie_page *vsie_page;
1081 unsigned long scb_addr;
1082 int rc;
1083
1084 vcpu->stat.instruction_sie++;
1085 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1086 return -EOPNOTSUPP;
1087 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1088 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1089
Heiko Carstens58cdf5e2017-07-05 07:37:14 +02001090 BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001091 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1092
1093 /* 512 byte alignment */
1094 if (unlikely(scb_addr & 0x1ffUL))
1095 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1096
1097 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1098 return 0;
1099
1100 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1101 if (IS_ERR(vsie_page))
1102 return PTR_ERR(vsie_page);
1103 else if (!vsie_page)
1104 /* double use of sie control block - simply do nothing */
1105 return 0;
1106
1107 rc = pin_scb(vcpu, vsie_page, scb_addr);
1108 if (rc)
1109 goto out_put;
1110 rc = shadow_scb(vcpu, vsie_page);
1111 if (rc)
1112 goto out_unpin_scb;
1113 rc = pin_blocks(vcpu, vsie_page);
1114 if (rc)
1115 goto out_unshadow;
David Hildenbrandadbf1692016-05-27 22:03:52 +02001116 register_shadow_scb(vcpu, vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001117 rc = vsie_run(vcpu, vsie_page);
David Hildenbrandadbf1692016-05-27 22:03:52 +02001118 unregister_shadow_scb(vcpu);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001119 unpin_blocks(vcpu, vsie_page);
1120out_unshadow:
1121 unshadow_scb(vcpu, vsie_page);
1122out_unpin_scb:
1123 unpin_scb(vcpu, vsie_page, scb_addr);
1124out_put:
1125 put_vsie_page(vcpu->kvm, vsie_page);
1126
1127 return rc < 0 ? rc : 0;
1128}
1129
1130/* Init the vsie data structures. To be called when a vm is initialized. */
1131void kvm_s390_vsie_init(struct kvm *kvm)
1132{
1133 mutex_init(&kvm->arch.vsie.mutex);
1134 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1135}
1136
1137/* Destroy the vsie data structures. To be called when a vm is destroyed. */
1138void kvm_s390_vsie_destroy(struct kvm *kvm)
1139{
David Hildenbrand06d68a62016-04-22 13:50:09 +02001140 struct vsie_page *vsie_page;
David Hildenbranda3508fb2015-07-08 13:19:48 +02001141 struct page *page;
1142 int i;
1143
1144 mutex_lock(&kvm->arch.vsie.mutex);
1145 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1146 page = kvm->arch.vsie.pages[i];
1147 kvm->arch.vsie.pages[i] = NULL;
David Hildenbrand06d68a62016-04-22 13:50:09 +02001148 vsie_page = page_to_virt(page);
1149 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001150 /* free the radix tree entry */
1151 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1152 __free_page(page);
1153 }
1154 kvm->arch.vsie.page_count = 0;
1155 mutex_unlock(&kvm->arch.vsie.mutex);
1156}
David Hildenbrandadbf1692016-05-27 22:03:52 +02001157
1158void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1159{
1160 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1161
1162 /*
1163 * Even if the VCPU lets go of the shadow sie block reference, it is
1164 * still valid in the cache. So we can safely kick it.
1165 */
1166 if (scb) {
1167 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1168 if (scb->prog0c & PROG_IN_SIE)
1169 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);
1170 }
1171}