blob: 87b3416390636a8455387532c59cb28d4b9bbfa6 [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
240 /* interrupt intercept */
241 switch (scb_s->icptcode) {
242 case ICPT_PROGI:
243 case ICPT_INSTPROGI:
244 case ICPT_EXTINT:
245 memcpy((void *)((u64)scb_o + 0xc0),
246 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
247 break;
248 case ICPT_PARTEXEC:
249 /* MVPG only */
250 memcpy((void *)((u64)scb_o + 0xc0),
251 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
252 break;
253 }
254
255 if (scb_s->ihcpu != 0xffffU)
256 scb_o->ihcpu = scb_s->ihcpu;
257}
258
259/*
260 * Setup the shadow scb by copying and checking the relevant parts of the g2
261 * provided scb.
262 *
263 * Returns: - 0 if the scb has been shadowed
264 * - > 0 if control has to be given to guest 2
265 */
266static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
267{
268 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
269 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100270 /* READ_ONCE does not work on bitfields - use a temporary variable */
271 const uint32_t __new_prefix = scb_o->prefix;
272 const uint32_t new_prefix = READ_ONCE(__new_prefix);
273 const bool wants_tx = READ_ONCE(scb_o->ecb) & ECB_TE;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100274 bool had_tx = scb_s->ecb & ECB_TE;
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100275 unsigned long new_mso = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200276 int rc;
277
278 /* make sure we don't have any leftovers when reusing the scb */
279 scb_s->icptcode = 0;
280 scb_s->eca = 0;
281 scb_s->ecb = 0;
282 scb_s->ecb2 = 0;
283 scb_s->ecb3 = 0;
284 scb_s->ecd = 0;
David Hildenbrand66b630d2015-11-26 14:11:19 +0100285 scb_s->fac = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200286
287 rc = prepare_cpuflags(vcpu, vsie_page);
288 if (rc)
289 goto out;
290
291 /* timer */
292 scb_s->cputm = scb_o->cputm;
293 scb_s->ckc = scb_o->ckc;
294 scb_s->todpr = scb_o->todpr;
295 scb_s->epoch = scb_o->epoch;
296
297 /* guest state */
298 scb_s->gpsw = scb_o->gpsw;
299 scb_s->gg14 = scb_o->gg14;
300 scb_s->gg15 = scb_o->gg15;
301 memcpy(scb_s->gcr, scb_o->gcr, 128);
302 scb_s->pp = scb_o->pp;
303
304 /* interception / execution handling */
305 scb_s->gbea = scb_o->gbea;
306 scb_s->lctl = scb_o->lctl;
307 scb_s->svcc = scb_o->svcc;
308 scb_s->ictl = scb_o->ictl;
309 /*
310 * SKEY handling functions can't deal with false setting of PTE invalid
311 * bits. Therefore we cannot provide interpretation and would later
312 * have to provide own emulation handlers.
313 */
Farhan Ali730cd632017-02-24 16:12:56 -0500314 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
315 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
316
David Hildenbranda3508fb2015-07-08 13:19:48 +0200317 scb_s->icpua = scb_o->icpua;
318
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100319 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100320 new_mso = READ_ONCE(scb_o->mso) & 0xfffffffffff00000UL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200321 /* if the hva of the prefix changes, we have to remap the prefix */
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100322 if (scb_s->mso != new_mso || scb_s->prefix != new_prefix)
David Hildenbrand06d68a62016-04-22 13:50:09 +0200323 prefix_unmapped(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200324 /* SIE will do mso/msl validity and exception checks for us */
325 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200326 scb_s->mso = new_mso;
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100327 scb_s->prefix = new_prefix;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200328
329 /* We have to definetly flush the tlb if this scb never ran */
330 if (scb_s->ihcpu != 0xffffU)
331 scb_s->ihcpu = scb_o->ihcpu;
332
333 /* MVPG and Protection Exception Interpretation are always available */
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100334 scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
David Hildenbrand4ceafa92015-11-27 12:34:28 +0100335 /* Host-protection-interruption introduced with ESOP */
336 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100337 scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100338 /* transactional execution */
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100339 if (test_kvm_facility(vcpu->kvm, 73) && wants_tx) {
David Hildenbrand166ecb32015-11-25 11:13:32 +0100340 /* remap the prefix is tx is toggled on */
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100341 if (!had_tx)
David Hildenbrand166ecb32015-11-25 11:13:32 +0100342 prefix_unmapped(vsie_page);
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100343 scb_s->ecb |= ECB_TE;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100344 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100345 /* SIMD */
346 if (test_kvm_facility(vcpu->kvm, 129)) {
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100347 scb_s->eca |= scb_o->eca & ECA_VX;
348 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100349 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100350 /* Run-time-Instrumentation */
351 if (test_kvm_facility(vcpu->kvm, 64))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100352 scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
Janosch Frankcd1836f2016-08-04 09:57:36 +0200353 /* Instruction Execution Prevention */
354 if (test_kvm_facility(vcpu->kvm, 130))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100355 scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100356 /* Guarded Storage */
357 if (test_kvm_facility(vcpu->kvm, 133)) {
358 scb_s->ecb |= scb_o->ecb & ECB_GS;
359 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
360 }
David Hildenbrand0615a322015-11-25 09:59:49 +0100361 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100362 scb_s->eca |= scb_o->eca & ECA_SII;
David Hildenbrand5630a8e2015-11-24 16:53:51 +0100363 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100364 scb_s->eca |= scb_o->eca & ECA_IB;
David Hildenbrand13ee3f62015-11-24 16:54:37 +0100365 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100366 scb_s->eca |= scb_o->eca & ECA_CEI;
Collin L. Walling8fa16962016-07-26 15:29:44 -0400367 /* Epoch Extension */
368 if (test_kvm_facility(vcpu->kvm, 139))
369 scb_s->ecd |= scb_o->ecd & ECD_MEF;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200370
David Hildenbrand35736022016-02-19 10:11:24 +0100371 prepare_ibc(vcpu, vsie_page);
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100372 rc = shadow_crycb(vcpu, vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200373out:
374 if (rc)
375 unshadow_scb(vcpu, vsie_page);
376 return rc;
377}
378
379void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
380 unsigned long end)
381{
382 struct kvm *kvm = gmap->private;
383 struct vsie_page *cur;
384 unsigned long prefix;
385 struct page *page;
386 int i;
387
388 if (!gmap_is_shadow(gmap))
389 return;
390 if (start >= 1UL << 31)
391 /* We are only interested in prefix pages */
392 return;
393
394 /*
395 * Only new shadow blocks are added to the list during runtime,
396 * therefore we can safely reference them all the time.
397 */
398 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
399 page = READ_ONCE(kvm->arch.vsie.pages[i]);
400 if (!page)
401 continue;
402 cur = page_to_virt(page);
403 if (READ_ONCE(cur->gmap) != gmap)
404 continue;
405 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
406 /* with mso/msl, the prefix lies at an offset */
407 prefix += cur->scb_s.mso;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100408 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
David Hildenbranda3508fb2015-07-08 13:19:48 +0200409 prefix_unmapped_sync(cur);
410 }
411}
412
413/*
David Hildenbrand166ecb32015-11-25 11:13:32 +0100414 * Map the first prefix page and if tx is enabled also the second prefix page.
David Hildenbranda3508fb2015-07-08 13:19:48 +0200415 *
416 * The prefix will be protected, a gmap notifier will inform about unmaps.
417 * The shadow scb must not be executed until the prefix is remapped, this is
418 * guaranteed by properly handling PROG_REQUEST.
419 *
420 * Returns: - 0 on if successfully mapped or already mapped
421 * - > 0 if control has to be given to guest 2
422 * - -EAGAIN if the caller can retry immediately
423 * - -ENOMEM if out of memory
424 */
425static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
426{
427 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
428 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
429 int rc;
430
David Hildenbrand06d68a62016-04-22 13:50:09 +0200431 if (prefix_is_mapped(vsie_page))
432 return 0;
433
David Hildenbranda3508fb2015-07-08 13:19:48 +0200434 /* mark it as mapped so we can catch any concurrent unmappers */
435 prefix_mapped(vsie_page);
436
437 /* with mso/msl, the prefix lies at offset *mso* */
438 prefix += scb_s->mso;
439
440 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100441 if (!rc && (scb_s->ecb & ECB_TE))
David Hildenbrand166ecb32015-11-25 11:13:32 +0100442 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
443 prefix + PAGE_SIZE);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200444 /*
445 * We don't have to mprotect, we will be called for all unshadows.
446 * SIE will detect if protection applies and trigger a validity.
447 */
448 if (rc)
449 prefix_unmapped(vsie_page);
450 if (rc > 0 || rc == -EFAULT)
451 rc = set_validity_icpt(scb_s, 0x0037U);
452 return rc;
453}
454
455/*
456 * Pin the guest page given by gpa and set hpa to the pinned host address.
457 * Will always be pinned writable.
458 *
459 * Returns: - 0 on success
460 * - -EINVAL if the gpa is not valid guest storage
David Hildenbranda3508fb2015-07-08 13:19:48 +0200461 */
462static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
463{
464 struct page *page;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200465
David Hildenbrandf7a65092017-09-01 17:11:43 +0200466 page = gfn_to_page(kvm, gpa_to_gfn(gpa));
467 if (is_error_page(page))
David Hildenbranda3508fb2015-07-08 13:19:48 +0200468 return -EINVAL;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200469 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
470 return 0;
471}
472
473/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
474static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
475{
David Hildenbrandf7a65092017-09-01 17:11:43 +0200476 kvm_release_pfn_dirty(hpa >> PAGE_SHIFT);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200477 /* mark the page always as dirty for migration */
478 mark_page_dirty(kvm, gpa_to_gfn(gpa));
479}
480
481/* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
482static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
483{
David Hildenbranda3508fb2015-07-08 13:19:48 +0200484 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
485 hpa_t hpa;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200486
487 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
488 if (hpa) {
David Hildenbrand15e50202018-01-16 18:15:26 +0100489 unpin_guest_page(vcpu->kvm, vsie_page->sca_gpa, hpa);
490 vsie_page->sca_gpa = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200491 scb_s->scaol = 0;
492 scb_s->scaoh = 0;
493 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100494
495 hpa = scb_s->itdba;
496 if (hpa) {
David Hildenbrand15e50202018-01-16 18:15:26 +0100497 unpin_guest_page(vcpu->kvm, vsie_page->itdba_gpa, hpa);
498 vsie_page->itdba_gpa = 0;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100499 scb_s->itdba = 0;
500 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100501
502 hpa = scb_s->gvrd;
503 if (hpa) {
David Hildenbrand15e50202018-01-16 18:15:26 +0100504 unpin_guest_page(vcpu->kvm, vsie_page->gvrd_gpa, hpa);
505 vsie_page->gvrd_gpa = 0;
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100506 scb_s->gvrd = 0;
507 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100508
509 hpa = scb_s->riccbd;
510 if (hpa) {
David Hildenbrand15e50202018-01-16 18:15:26 +0100511 unpin_guest_page(vcpu->kvm, vsie_page->riccbd_gpa, hpa);
512 vsie_page->riccbd_gpa = 0;
David Hildenbrand588438c2016-01-26 12:51:06 +0100513 scb_s->riccbd = 0;
514 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100515
516 hpa = scb_s->sdnxo;
517 if (hpa) {
David Hildenbrand15e50202018-01-16 18:15:26 +0100518 unpin_guest_page(vcpu->kvm, vsie_page->sdnx_gpa, hpa);
519 vsie_page->sdnx_gpa = 0;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100520 scb_s->sdnxo = 0;
521 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200522}
523
524/*
525 * Instead of shadowing some blocks, we can simply forward them because the
526 * addresses in the scb are 64 bit long.
527 *
528 * This works as long as the data lies in one page. If blocks ever exceed one
529 * page, we have to fall back to shadowing.
530 *
531 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
532 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
533 *
534 * Returns: - 0 if all blocks were pinned.
535 * - > 0 if control has to be given to guest 2
536 * - -ENOMEM if out of memory
537 */
538static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
539{
540 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
541 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
542 hpa_t hpa;
543 gpa_t gpa;
544 int rc = 0;
545
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100546 gpa = READ_ONCE(scb_o->scaol) & ~0xfUL;
David Hildenbrand19c439b2015-11-25 11:02:26 +0100547 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100548 gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200549 if (gpa) {
550 if (!(gpa & ~0x1fffUL))
551 rc = set_validity_icpt(scb_s, 0x0038U);
552 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
553 rc = set_validity_icpt(scb_s, 0x0011U);
554 else if ((gpa & PAGE_MASK) !=
555 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
556 rc = set_validity_icpt(scb_s, 0x003bU);
557 if (!rc) {
558 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200559 if (rc)
David Hildenbranda3508fb2015-07-08 13:19:48 +0200560 rc = set_validity_icpt(scb_s, 0x0034U);
561 }
562 if (rc)
563 goto unpin;
David Hildenbrand15e50202018-01-16 18:15:26 +0100564 vsie_page->sca_gpa = gpa;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200565 scb_s->scaoh = (u32)((u64)hpa >> 32);
566 scb_s->scaol = (u32)(u64)hpa;
567 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100568
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100569 gpa = READ_ONCE(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);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200577 if (rc) {
David Hildenbrand166ecb32015-11-25 11:13:32 +0100578 rc = set_validity_icpt(scb_s, 0x0080U);
David Hildenbrand166ecb32015-11-25 11:13:32 +0100579 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200580 }
David Hildenbrand15e50202018-01-16 18:15:26 +0100581 vsie_page->itdba_gpa = gpa;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100582 scb_s->itdba = hpa;
583 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100584
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100585 gpa = READ_ONCE(scb_o->gvrd) & ~0x1ffUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100586 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100587 if (!(gpa & ~0x1fffUL)) {
588 rc = set_validity_icpt(scb_s, 0x1310U);
589 goto unpin;
590 }
591 /*
592 * 512 bytes vector registers cannot cross page boundaries
593 * if this block gets bigger, we have to shadow it.
594 */
595 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200596 if (rc) {
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100597 rc = set_validity_icpt(scb_s, 0x1310U);
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100598 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200599 }
David Hildenbrand15e50202018-01-16 18:15:26 +0100600 vsie_page->gvrd_gpa = gpa;
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100601 scb_s->gvrd = hpa;
602 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100603
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100604 gpa = READ_ONCE(scb_o->riccbd) & ~0x3fUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100605 if (gpa && (scb_s->ecb3 & ECB3_RI)) {
David Hildenbrand588438c2016-01-26 12:51:06 +0100606 if (!(gpa & ~0x1fffUL)) {
607 rc = set_validity_icpt(scb_s, 0x0043U);
608 goto unpin;
609 }
610 /* 64 bytes cannot cross page boundaries */
611 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200612 if (rc) {
David Hildenbrand588438c2016-01-26 12:51:06 +0100613 rc = set_validity_icpt(scb_s, 0x0043U);
David Hildenbrand588438c2016-01-26 12:51:06 +0100614 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200615 }
616 /* Validity 0x0044 will be checked by SIE */
David Hildenbrand15e50202018-01-16 18:15:26 +0100617 vsie_page->riccbd_gpa = gpa;
David Hildenbrand4d21cef32016-09-02 12:33:49 +0200618 scb_s->riccbd = hpa;
David Hildenbrand588438c2016-01-26 12:51:06 +0100619 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100620 if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
621 unsigned long sdnxc;
622
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100623 gpa = READ_ONCE(scb_o->sdnxo) & ~0xfUL;
624 sdnxc = READ_ONCE(scb_o->sdnxo) & 0xfUL;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100625 if (!gpa || !(gpa & ~0x1fffUL)) {
626 rc = set_validity_icpt(scb_s, 0x10b0U);
627 goto unpin;
628 }
629 if (sdnxc < 6 || sdnxc > 12) {
630 rc = set_validity_icpt(scb_s, 0x10b1U);
631 goto unpin;
632 }
633 if (gpa & ((1 << sdnxc) - 1)) {
634 rc = set_validity_icpt(scb_s, 0x10b2U);
635 goto unpin;
636 }
637 /* Due to alignment rules (checked above) this cannot
638 * cross page boundaries
639 */
640 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200641 if (rc) {
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100642 rc = set_validity_icpt(scb_s, 0x10b0U);
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100643 goto unpin;
David Hildenbrandf7a65092017-09-01 17:11:43 +0200644 }
David Hildenbrand15e50202018-01-16 18:15:26 +0100645 vsie_page->sdnx_gpa = gpa;
Christian Borntraegerfe722d12017-04-07 14:23:13 +0200646 scb_s->sdnxo = hpa | sdnxc;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100647 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200648 return 0;
649unpin:
650 unpin_blocks(vcpu, vsie_page);
651 return rc;
652}
653
654/* unpin the scb provided by guest 2, marking it as dirty */
655static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
656 gpa_t gpa)
657{
658 hpa_t hpa = (hpa_t) vsie_page->scb_o;
659
660 if (hpa)
661 unpin_guest_page(vcpu->kvm, gpa, hpa);
662 vsie_page->scb_o = NULL;
663}
664
665/*
666 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
667 *
668 * Returns: - 0 if the scb was pinned.
669 * - > 0 if control has to be given to guest 2
David Hildenbranda3508fb2015-07-08 13:19:48 +0200670 */
671static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
672 gpa_t gpa)
673{
674 hpa_t hpa;
675 int rc;
676
677 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200678 if (rc) {
David Hildenbranda3508fb2015-07-08 13:19:48 +0200679 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
David Hildenbrandf7a65092017-09-01 17:11:43 +0200680 WARN_ON_ONCE(rc);
681 return 1;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200682 }
David Hildenbrandf7a65092017-09-01 17:11:43 +0200683 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
684 return 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200685}
686
687/*
688 * Inject a fault into guest 2.
689 *
690 * Returns: - > 0 if control has to be given to guest 2
691 * < 0 if an error occurred during injection.
692 */
693static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
694 bool write_flag)
695{
696 struct kvm_s390_pgm_info pgm = {
697 .code = code,
698 .trans_exc_code =
699 /* 0-51: virtual address */
700 (vaddr & 0xfffffffffffff000UL) |
701 /* 52-53: store / fetch */
702 (((unsigned int) !write_flag) + 1) << 10,
703 /* 62-63: asce id (alway primary == 0) */
704 .exc_access_id = 0, /* always primary */
705 .op_access_id = 0, /* not MVPG */
706 };
707 int rc;
708
709 if (code == PGM_PROTECTION)
710 pgm.trans_exc_code |= 0x4UL;
711
712 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
713 return rc ? rc : 1;
714}
715
716/*
717 * Handle a fault during vsie execution on a gmap shadow.
718 *
719 * Returns: - 0 if the fault was resolved
720 * - > 0 if control has to be given to guest 2
721 * - < 0 if an error occurred
722 */
723static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
724{
725 int rc;
726
727 if (current->thread.gmap_int_code == PGM_PROTECTION)
728 /* we can directly forward all protection exceptions */
729 return inject_fault(vcpu, PGM_PROTECTION,
730 current->thread.gmap_addr, 1);
731
732 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
733 current->thread.gmap_addr);
734 if (rc > 0) {
735 rc = inject_fault(vcpu, rc,
736 current->thread.gmap_addr,
737 current->thread.gmap_write_flag);
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200738 if (rc >= 0)
739 vsie_page->fault_addr = current->thread.gmap_addr;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200740 }
741 return rc;
742}
743
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200744/*
745 * Retry the previous fault that required guest 2 intervention. This avoids
746 * one superfluous SIE re-entry and direct exit.
747 *
748 * Will ignore any errors. The next SIE fault will do proper fault handling.
749 */
750static void handle_last_fault(struct kvm_vcpu *vcpu,
751 struct vsie_page *vsie_page)
752{
753 if (vsie_page->fault_addr)
754 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
755 vsie_page->fault_addr);
756 vsie_page->fault_addr = 0;
757}
758
David Hildenbranda3508fb2015-07-08 13:19:48 +0200759static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
760{
761 vsie_page->scb_s.icptcode = 0;
762}
763
David Hildenbrand66b630d2015-11-26 14:11:19 +0100764/* rewind the psw and clear the vsie icpt, so we can retry execution */
765static void retry_vsie_icpt(struct vsie_page *vsie_page)
766{
767 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
768 int ilen = insn_length(scb_s->ipa >> 8);
769
770 /* take care of EXECUTE instructions */
771 if (scb_s->icptstatus & 1) {
772 ilen = (scb_s->icptstatus >> 4) & 0x6;
773 if (!ilen)
774 ilen = 4;
775 }
776 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
777 clear_vsie_icpt(vsie_page);
778}
779
780/*
781 * Try to shadow + enable the guest 2 provided facility list.
782 * Retry instruction execution if enabled for and provided by guest 2.
783 *
784 * Returns: - 0 if handled (retry or guest 2 icpt)
785 * - > 0 if control has to be given to guest 2
786 */
787static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
788{
789 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
David Hildenbrandb3ecd4a2018-01-16 18:15:25 +0100790 __u32 fac = READ_ONCE(vsie_page->scb_o->fac) & 0x7ffffff8U;
David Hildenbrand66b630d2015-11-26 14:11:19 +0100791
792 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
793 retry_vsie_icpt(vsie_page);
794 if (read_guest_real(vcpu, fac, &vsie_page->fac,
795 sizeof(vsie_page->fac)))
796 return set_validity_icpt(scb_s, 0x1090U);
797 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
798 }
799 return 0;
800}
801
David Hildenbranda3508fb2015-07-08 13:19:48 +0200802/*
803 * Run the vsie on a shadow scb and a shadow gmap, without any further
804 * sanity checks, handling SIE faults.
805 *
806 * Returns: - 0 everything went fine
807 * - > 0 if control has to be given to guest 2
808 * - < 0 if an error occurred
809 */
810static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
811{
812 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
813 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
814 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");
David Hildenbrandc95c8952017-08-30 18:06:02 +0200837 kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info);
QingFeng Haod52cd202017-06-07 12:11:18 +0200838 return 0;
839 }
840
David Hildenbranda3508fb2015-07-08 13:19:48 +0200841 if (rc > 0)
842 rc = 0; /* we could still have an icpt */
843 else if (rc == -EFAULT)
844 return handle_fault(vcpu, vsie_page);
845
846 switch (scb_s->icptcode) {
David Hildenbrand66b630d2015-11-26 14:11:19 +0100847 case ICPT_INST:
848 if (scb_s->ipa == 0xb2b0)
849 rc = handle_stfle(vcpu, vsie_page);
850 break;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200851 case ICPT_STOP:
852 /* stop not requested by g2 - must have been a kick */
853 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
854 clear_vsie_icpt(vsie_page);
855 break;
856 case ICPT_VALIDITY:
857 if ((scb_s->ipa & 0xf000) != 0xf000)
858 scb_s->ipa += 0x1000;
859 break;
860 }
861 return rc;
862}
863
864static void release_gmap_shadow(struct vsie_page *vsie_page)
865{
866 if (vsie_page->gmap)
867 gmap_put(vsie_page->gmap);
868 WRITE_ONCE(vsie_page->gmap, NULL);
David Hildenbrand06d68a62016-04-22 13:50:09 +0200869 prefix_unmapped(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200870}
871
872static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
873 struct vsie_page *vsie_page)
874{
875 unsigned long asce;
876 union ctlreg0 cr0;
877 struct gmap *gmap;
878 int edat;
879
880 asce = vcpu->arch.sie_block->gcr[1];
881 cr0.val = vcpu->arch.sie_block->gcr[0];
882 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
883 edat += edat && test_kvm_facility(vcpu->kvm, 78);
884
David Hildenbrand06d68a62016-04-22 13:50:09 +0200885 /*
886 * ASCE or EDAT could have changed since last icpt, or the gmap
887 * we're holding has been unshadowed. If the gmap is still valid,
888 * we can safely reuse it.
889 */
890 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
891 return 0;
892
893 /* release the old shadow - if any, and mark the prefix as unmapped */
894 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200895 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
896 if (IS_ERR(gmap))
897 return PTR_ERR(gmap);
898 gmap->private = vcpu->kvm;
899 WRITE_ONCE(vsie_page->gmap, gmap);
900 return 0;
901}
902
903/*
David Hildenbrandadbf1692016-05-27 22:03:52 +0200904 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
905 */
906static void register_shadow_scb(struct kvm_vcpu *vcpu,
907 struct vsie_page *vsie_page)
908{
David Hildenbrand91473b42015-10-29 10:30:36 +0100909 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
910
David Hildenbrandadbf1692016-05-27 22:03:52 +0200911 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
David Hildenbrandb917ae52015-07-07 20:39:35 +0200912 /*
913 * External calls have to lead to a kick of the vcpu and
914 * therefore the vsie -> Simulate Wait state.
915 */
David Hildenbrandef8f4f42018-01-23 18:05:29 +0100916 kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
David Hildenbrand91473b42015-10-29 10:30:36 +0100917 /*
918 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
919 * automatically be adjusted on tod clock changes via kvm_sync_clock.
920 */
921 preempt_disable();
922 scb_s->epoch += vcpu->kvm->arch.epoch;
Collin L. Walling8fa16962016-07-26 15:29:44 -0400923
924 if (scb_s->ecd & ECD_MEF) {
925 scb_s->epdx += vcpu->kvm->arch.epdx;
926 if (scb_s->epoch < vcpu->kvm->arch.epoch)
927 scb_s->epdx += 1;
928 }
929
David Hildenbrand91473b42015-10-29 10:30:36 +0100930 preempt_enable();
David Hildenbrandadbf1692016-05-27 22:03:52 +0200931}
932
933/*
934 * Unregister a shadow scb from a VCPU.
935 */
936static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
937{
David Hildenbrandb917ae52015-07-07 20:39:35 +0200938 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
David Hildenbrandadbf1692016-05-27 22:03:52 +0200939 WRITE_ONCE(vcpu->arch.vsie_block, NULL);
940}
941
942/*
David Hildenbranda3508fb2015-07-08 13:19:48 +0200943 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
944 * prefix pages and faults.
945 *
946 * Returns: - 0 if no errors occurred
947 * - > 0 if control has to be given to guest 2
948 * - -ENOMEM if out of memory
949 */
950static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
951{
952 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
953 int rc = 0;
954
955 while (1) {
956 rc = acquire_gmap_shadow(vcpu, vsie_page);
957 if (!rc)
958 rc = map_prefix(vcpu, vsie_page);
959 if (!rc) {
960 gmap_enable(vsie_page->gmap);
961 update_intervention_requests(vsie_page);
962 rc = do_vsie_run(vcpu, vsie_page);
963 gmap_enable(vcpu->arch.gmap);
964 }
David Hildenbrandadbf1692016-05-27 22:03:52 +0200965 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200966
967 if (rc == -EAGAIN)
968 rc = 0;
969 if (rc || scb_s->icptcode || signal_pending(current) ||
970 kvm_s390_vcpu_has_irq(vcpu, 0))
971 break;
Heiko Carstens0b925152017-01-02 08:51:02 +0100972 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200973
974 if (rc == -EFAULT) {
975 /*
976 * Addressing exceptions are always presentes as intercepts.
977 * As addressing exceptions are suppressing and our guest 3 PSW
978 * points at the responsible instruction, we have to
979 * forward the PSW and set the ilc. If we can't read guest 3
980 * instruction, we can use an arbitrary ilc. Let's always use
981 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
982 * memory. (we could also fake the shadow so the hardware
983 * handles it).
984 */
985 scb_s->icptcode = ICPT_PROGI;
986 scb_s->iprcc = PGM_ADDRESSING;
987 scb_s->pgmilc = 4;
988 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
989 }
990 return rc;
991}
992
993/*
994 * Get or create a vsie page for a scb address.
995 *
996 * Returns: - address of a vsie page (cached or new one)
997 * - NULL if the same scb address is already used by another VCPU
998 * - ERR_PTR(-ENOMEM) if out of memory
999 */
1000static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
1001{
1002 struct vsie_page *vsie_page;
1003 struct page *page;
1004 int nr_vcpus;
1005
1006 rcu_read_lock();
1007 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
1008 rcu_read_unlock();
1009 if (page) {
1010 if (page_ref_inc_return(page) == 2)
1011 return page_to_virt(page);
1012 page_ref_dec(page);
1013 }
1014
1015 /*
1016 * We want at least #online_vcpus shadows, so every VCPU can execute
1017 * the VSIE in parallel.
1018 */
1019 nr_vcpus = atomic_read(&kvm->online_vcpus);
1020
1021 mutex_lock(&kvm->arch.vsie.mutex);
1022 if (kvm->arch.vsie.page_count < nr_vcpus) {
David Hildenbrand66b630d2015-11-26 14:11:19 +01001023 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001024 if (!page) {
1025 mutex_unlock(&kvm->arch.vsie.mutex);
1026 return ERR_PTR(-ENOMEM);
1027 }
1028 page_ref_inc(page);
1029 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
1030 kvm->arch.vsie.page_count++;
1031 } else {
1032 /* reuse an existing entry that belongs to nobody */
1033 while (true) {
1034 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
1035 if (page_ref_inc_return(page) == 2)
1036 break;
1037 page_ref_dec(page);
1038 kvm->arch.vsie.next++;
1039 kvm->arch.vsie.next %= nr_vcpus;
1040 }
1041 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1042 }
1043 page->index = addr;
1044 /* double use of the same address */
1045 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
1046 page_ref_dec(page);
1047 mutex_unlock(&kvm->arch.vsie.mutex);
1048 return NULL;
1049 }
1050 mutex_unlock(&kvm->arch.vsie.mutex);
1051
1052 vsie_page = page_to_virt(page);
1053 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
David Hildenbrand06d68a62016-04-22 13:50:09 +02001054 release_gmap_shadow(vsie_page);
David Hildenbrand1b7029b2015-07-08 13:25:31 +02001055 vsie_page->fault_addr = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +02001056 vsie_page->scb_s.ihcpu = 0xffffU;
1057 return vsie_page;
1058}
1059
1060/* put a vsie page acquired via get_vsie_page */
1061static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
1062{
1063 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
1064
1065 page_ref_dec(page);
1066}
1067
1068int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1069{
1070 struct vsie_page *vsie_page;
1071 unsigned long scb_addr;
1072 int rc;
1073
1074 vcpu->stat.instruction_sie++;
1075 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1076 return -EOPNOTSUPP;
1077 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1078 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1079
Heiko Carstens58cdf5e2017-07-05 07:37:14 +02001080 BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001081 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1082
1083 /* 512 byte alignment */
1084 if (unlikely(scb_addr & 0x1ffUL))
1085 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1086
1087 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1088 return 0;
1089
1090 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1091 if (IS_ERR(vsie_page))
1092 return PTR_ERR(vsie_page);
1093 else if (!vsie_page)
1094 /* double use of sie control block - simply do nothing */
1095 return 0;
1096
1097 rc = pin_scb(vcpu, vsie_page, scb_addr);
1098 if (rc)
1099 goto out_put;
1100 rc = shadow_scb(vcpu, vsie_page);
1101 if (rc)
1102 goto out_unpin_scb;
1103 rc = pin_blocks(vcpu, vsie_page);
1104 if (rc)
1105 goto out_unshadow;
David Hildenbrandadbf1692016-05-27 22:03:52 +02001106 register_shadow_scb(vcpu, vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001107 rc = vsie_run(vcpu, vsie_page);
David Hildenbrandadbf1692016-05-27 22:03:52 +02001108 unregister_shadow_scb(vcpu);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001109 unpin_blocks(vcpu, vsie_page);
1110out_unshadow:
1111 unshadow_scb(vcpu, vsie_page);
1112out_unpin_scb:
1113 unpin_scb(vcpu, vsie_page, scb_addr);
1114out_put:
1115 put_vsie_page(vcpu->kvm, vsie_page);
1116
1117 return rc < 0 ? rc : 0;
1118}
1119
1120/* Init the vsie data structures. To be called when a vm is initialized. */
1121void kvm_s390_vsie_init(struct kvm *kvm)
1122{
1123 mutex_init(&kvm->arch.vsie.mutex);
1124 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1125}
1126
1127/* Destroy the vsie data structures. To be called when a vm is destroyed. */
1128void kvm_s390_vsie_destroy(struct kvm *kvm)
1129{
David Hildenbrand06d68a62016-04-22 13:50:09 +02001130 struct vsie_page *vsie_page;
David Hildenbranda3508fb2015-07-08 13:19:48 +02001131 struct page *page;
1132 int i;
1133
1134 mutex_lock(&kvm->arch.vsie.mutex);
1135 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1136 page = kvm->arch.vsie.pages[i];
1137 kvm->arch.vsie.pages[i] = NULL;
David Hildenbrand06d68a62016-04-22 13:50:09 +02001138 vsie_page = page_to_virt(page);
1139 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001140 /* free the radix tree entry */
1141 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1142 __free_page(page);
1143 }
1144 kvm->arch.vsie.page_count = 0;
1145 mutex_unlock(&kvm->arch.vsie.mutex);
1146}
David Hildenbrandadbf1692016-05-27 22:03:52 +02001147
1148void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1149{
1150 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1151
1152 /*
1153 * Even if the VCPU lets go of the shadow sie block reference, it is
1154 * still valid in the cache. So we can safely kick it.
1155 */
1156 if (scb) {
1157 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1158 if (scb->prog0c & PROG_IN_SIE)
1159 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);
1160 }
1161}