blob: d91f1df5a8544cc458a84c95df0f7454567ca005 [file] [log] [blame]
David Hildenbranda3508fb2015-07-08 13:19:48 +02001/*
2 * kvm nested virtualization support for s390x
3 *
4 * Copyright IBM Corp. 2016
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
11 */
12#include <linux/vmalloc.h>
13#include <linux/kvm_host.h>
14#include <linux/bug.h>
15#include <linux/list.h>
16#include <linux/bitmap.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010017#include <linux/sched/signal.h>
18
David Hildenbranda3508fb2015-07-08 13:19:48 +020019#include <asm/gmap.h>
20#include <asm/mmu_context.h>
21#include <asm/sclp.h>
22#include <asm/nmi.h>
David Hildenbrand66b630d2015-11-26 14:11:19 +010023#include <asm/dis.h>
David Hildenbranda3508fb2015-07-08 13:19:48 +020024#include "kvm-s390.h"
25#include "gaccess.h"
26
27struct vsie_page {
28 struct kvm_s390_sie_block scb_s; /* 0x0000 */
29 /* the pinned originial scb */
30 struct kvm_s390_sie_block *scb_o; /* 0x0200 */
31 /* the shadow gmap in use by the vsie_page */
32 struct gmap *gmap; /* 0x0208 */
David Hildenbrand1b7029b2015-07-08 13:25:31 +020033 /* address of the last reported fault to guest2 */
34 unsigned long fault_addr; /* 0x0210 */
35 __u8 reserved[0x0700 - 0x0218]; /* 0x0218 */
David Hildenbrandbbeaa582015-11-26 13:11:42 +010036 struct kvm_s390_crypto_cb crycb; /* 0x0700 */
David Hildenbrand66b630d2015-11-26 14:11:19 +010037 __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */
David Hildenbranda3508fb2015-07-08 13:19:48 +020038} __packed;
39
40/* trigger a validity icpt for the given scb */
41static int set_validity_icpt(struct kvm_s390_sie_block *scb,
42 __u16 reason_code)
43{
44 scb->ipa = 0x1000;
45 scb->ipb = ((__u32) reason_code) << 16;
46 scb->icptcode = ICPT_VALIDITY;
47 return 1;
48}
49
50/* mark the prefix as unmapped, this will block the VSIE */
51static void prefix_unmapped(struct vsie_page *vsie_page)
52{
53 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
54}
55
56/* mark the prefix as unmapped and wait until the VSIE has been left */
57static void prefix_unmapped_sync(struct vsie_page *vsie_page)
58{
59 prefix_unmapped(vsie_page);
60 if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
61 atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
62 while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
63 cpu_relax();
64}
65
66/* mark the prefix as mapped, this will allow the VSIE to run */
67static void prefix_mapped(struct vsie_page *vsie_page)
68{
69 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
70}
71
David Hildenbrand06d68a62016-04-22 13:50:09 +020072/* test if the prefix is mapped into the gmap shadow */
73static int prefix_is_mapped(struct vsie_page *vsie_page)
74{
75 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
76}
David Hildenbranda3508fb2015-07-08 13:19:48 +020077
78/* copy the updated intervention request bits into the shadow scb */
79static void update_intervention_requests(struct vsie_page *vsie_page)
80{
81 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
82 int cpuflags;
83
84 cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
85 atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
86 atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
87}
88
89/* shadow (filter and validate) the cpuflags */
90static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
91{
92 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
93 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
94 int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
95
96 /* we don't allow ESA/390 guests */
97 if (!(cpuflags & CPUSTAT_ZARCH))
98 return set_validity_icpt(scb_s, 0x0001U);
99
100 if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
101 return set_validity_icpt(scb_s, 0x0001U);
102 else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
103 return set_validity_icpt(scb_s, 0x0007U);
104
105 /* intervention requests will be set later */
106 newflags = CPUSTAT_ZARCH;
David Hildenbrand535ef812016-02-12 12:24:20 +0100107 if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8))
108 newflags |= CPUSTAT_GED;
109 if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) {
110 if (cpuflags & CPUSTAT_GED)
111 return set_validity_icpt(scb_s, 0x0001U);
112 newflags |= CPUSTAT_GED2;
113 }
David Hildenbrand77d18f62015-11-24 16:32:35 +0100114 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE))
115 newflags |= cpuflags & CPUSTAT_P;
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100116 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS))
117 newflags |= cpuflags & CPUSTAT_SM;
David Hildenbrand7fd7f392015-11-24 16:56:23 +0100118 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS))
119 newflags |= cpuflags & CPUSTAT_IBS;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200120
121 atomic_set(&scb_s->cpuflags, newflags);
122 return 0;
123}
124
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100125/*
126 * Create a shadow copy of the crycb block and setup key wrapping, if
127 * requested for guest 3 and enabled for guest 2.
128 *
129 * We only accept format-1 (no AP in g2), but convert it into format-2
130 * There is nothing to do for format-0.
131 *
132 * Returns: - 0 if shadowed or nothing to do
133 * - > 0 if control has to be given to guest 2
134 */
135static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
136{
137 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
138 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
139 u32 crycb_addr = scb_o->crycbd & 0x7ffffff8U;
140 unsigned long *b1, *b2;
141 u8 ecb3_flags;
142
143 scb_s->crycbd = 0;
144 if (!(scb_o->crycbd & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
145 return 0;
146 /* format-1 is supported with message-security-assist extension 3 */
147 if (!test_kvm_facility(vcpu->kvm, 76))
148 return 0;
149 /* we may only allow it if enabled for guest 2 */
150 ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
151 (ECB3_AES | ECB3_DEA);
152 if (!ecb3_flags)
153 return 0;
154
155 if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
156 return set_validity_icpt(scb_s, 0x003CU);
157 else if (!crycb_addr)
158 return set_validity_icpt(scb_s, 0x0039U);
159
160 /* copy only the wrapping keys */
161 if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
162 return set_validity_icpt(scb_s, 0x0035U);
163
164 scb_s->ecb3 |= ecb3_flags;
165 scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
166 CRYCB_FORMAT2;
167
168 /* xor both blocks in one run */
169 b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
170 b2 = (unsigned long *)
171 vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
172 /* as 56%8 == 0, bitmap_xor won't overwrite any data */
173 bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
174 return 0;
175}
176
David Hildenbrand35736022016-02-19 10:11:24 +0100177/* shadow (round up/down) the ibc to avoid validity icpt */
178static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
179{
180 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
181 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
182 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
183
184 scb_s->ibc = 0;
185 /* ibc installed in g2 and requested for g3 */
186 if (vcpu->kvm->arch.model.ibc && (scb_o->ibc & 0x0fffU)) {
187 scb_s->ibc = scb_o->ibc & 0x0fffU;
188 /* takte care of the minimum ibc level of the machine */
189 if (scb_s->ibc < min_ibc)
190 scb_s->ibc = min_ibc;
191 /* take care of the maximum ibc level set for the guest */
192 if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
193 scb_s->ibc = vcpu->kvm->arch.model.ibc;
194 }
195}
196
David Hildenbranda3508fb2015-07-08 13:19:48 +0200197/* unshadow the scb, copying parameters back to the real scb */
198static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
199{
200 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
201 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
202
203 /* interception */
204 scb_o->icptcode = scb_s->icptcode;
205 scb_o->icptstatus = scb_s->icptstatus;
206 scb_o->ipa = scb_s->ipa;
207 scb_o->ipb = scb_s->ipb;
208 scb_o->gbea = scb_s->gbea;
209
210 /* timer */
211 scb_o->cputm = scb_s->cputm;
212 scb_o->ckc = scb_s->ckc;
213 scb_o->todpr = scb_s->todpr;
214
215 /* guest state */
216 scb_o->gpsw = scb_s->gpsw;
217 scb_o->gg14 = scb_s->gg14;
218 scb_o->gg15 = scb_s->gg15;
219 memcpy(scb_o->gcr, scb_s->gcr, 128);
220 scb_o->pp = scb_s->pp;
221
222 /* interrupt intercept */
223 switch (scb_s->icptcode) {
224 case ICPT_PROGI:
225 case ICPT_INSTPROGI:
226 case ICPT_EXTINT:
227 memcpy((void *)((u64)scb_o + 0xc0),
228 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
229 break;
230 case ICPT_PARTEXEC:
231 /* MVPG only */
232 memcpy((void *)((u64)scb_o + 0xc0),
233 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
234 break;
235 }
236
237 if (scb_s->ihcpu != 0xffffU)
238 scb_o->ihcpu = scb_s->ihcpu;
239}
240
241/*
242 * Setup the shadow scb by copying and checking the relevant parts of the g2
243 * provided scb.
244 *
245 * Returns: - 0 if the scb has been shadowed
246 * - > 0 if control has to be given to guest 2
247 */
248static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
249{
250 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
251 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100252 bool had_tx = scb_s->ecb & ECB_TE;
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100253 unsigned long new_mso = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200254 int rc;
255
256 /* make sure we don't have any leftovers when reusing the scb */
257 scb_s->icptcode = 0;
258 scb_s->eca = 0;
259 scb_s->ecb = 0;
260 scb_s->ecb2 = 0;
261 scb_s->ecb3 = 0;
262 scb_s->ecd = 0;
David Hildenbrand66b630d2015-11-26 14:11:19 +0100263 scb_s->fac = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200264
265 rc = prepare_cpuflags(vcpu, vsie_page);
266 if (rc)
267 goto out;
268
269 /* timer */
270 scb_s->cputm = scb_o->cputm;
271 scb_s->ckc = scb_o->ckc;
272 scb_s->todpr = scb_o->todpr;
273 scb_s->epoch = scb_o->epoch;
274
275 /* guest state */
276 scb_s->gpsw = scb_o->gpsw;
277 scb_s->gg14 = scb_o->gg14;
278 scb_s->gg15 = scb_o->gg15;
279 memcpy(scb_s->gcr, scb_o->gcr, 128);
280 scb_s->pp = scb_o->pp;
281
282 /* interception / execution handling */
283 scb_s->gbea = scb_o->gbea;
284 scb_s->lctl = scb_o->lctl;
285 scb_s->svcc = scb_o->svcc;
286 scb_s->ictl = scb_o->ictl;
287 /*
288 * SKEY handling functions can't deal with false setting of PTE invalid
289 * bits. Therefore we cannot provide interpretation and would later
290 * have to provide own emulation handlers.
291 */
292 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
293 scb_s->icpua = scb_o->icpua;
294
David Hildenbranda1b7b9b2015-11-24 16:41:33 +0100295 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
296 new_mso = scb_o->mso & 0xfffffffffff00000UL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200297 /* if the hva of the prefix changes, we have to remap the prefix */
298 if (scb_s->mso != new_mso || scb_s->prefix != scb_o->prefix)
299 prefix_unmapped(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200300 /* SIE will do mso/msl validity and exception checks for us */
301 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
David Hildenbrand06d68a62016-04-22 13:50:09 +0200302 scb_s->mso = new_mso;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200303 scb_s->prefix = scb_o->prefix;
304
305 /* We have to definetly flush the tlb if this scb never ran */
306 if (scb_s->ihcpu != 0xffffU)
307 scb_s->ihcpu = scb_o->ihcpu;
308
309 /* MVPG and Protection Exception Interpretation are always available */
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100310 scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
David Hildenbrand4ceafa92015-11-27 12:34:28 +0100311 /* Host-protection-interruption introduced with ESOP */
312 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100313 scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100314 /* transactional execution */
315 if (test_kvm_facility(vcpu->kvm, 73)) {
316 /* remap the prefix is tx is toggled on */
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100317 if ((scb_o->ecb & ECB_TE) && !had_tx)
David Hildenbrand166ecb32015-11-25 11:13:32 +0100318 prefix_unmapped(vsie_page);
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100319 scb_s->ecb |= scb_o->ecb & ECB_TE;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100320 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100321 /* SIMD */
322 if (test_kvm_facility(vcpu->kvm, 129)) {
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100323 scb_s->eca |= scb_o->eca & ECA_VX;
324 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100325 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100326 /* Run-time-Instrumentation */
327 if (test_kvm_facility(vcpu->kvm, 64))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100328 scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
Janosch Frankcd1836f2016-08-04 09:57:36 +0200329 /* Instruction Execution Prevention */
330 if (test_kvm_facility(vcpu->kvm, 130))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100331 scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
David Hildenbrand0615a322015-11-25 09:59:49 +0100332 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100333 scb_s->eca |= scb_o->eca & ECA_SII;
David Hildenbrand5630a8e2015-11-24 16:53:51 +0100334 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100335 scb_s->eca |= scb_o->eca & ECA_IB;
David Hildenbrand13ee3f62015-11-24 16:54:37 +0100336 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100337 scb_s->eca |= scb_o->eca & ECA_CEI;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200338
David Hildenbrand35736022016-02-19 10:11:24 +0100339 prepare_ibc(vcpu, vsie_page);
David Hildenbrandbbeaa582015-11-26 13:11:42 +0100340 rc = shadow_crycb(vcpu, vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200341out:
342 if (rc)
343 unshadow_scb(vcpu, vsie_page);
344 return rc;
345}
346
347void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
348 unsigned long end)
349{
350 struct kvm *kvm = gmap->private;
351 struct vsie_page *cur;
352 unsigned long prefix;
353 struct page *page;
354 int i;
355
356 if (!gmap_is_shadow(gmap))
357 return;
358 if (start >= 1UL << 31)
359 /* We are only interested in prefix pages */
360 return;
361
362 /*
363 * Only new shadow blocks are added to the list during runtime,
364 * therefore we can safely reference them all the time.
365 */
366 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
367 page = READ_ONCE(kvm->arch.vsie.pages[i]);
368 if (!page)
369 continue;
370 cur = page_to_virt(page);
371 if (READ_ONCE(cur->gmap) != gmap)
372 continue;
373 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
374 /* with mso/msl, the prefix lies at an offset */
375 prefix += cur->scb_s.mso;
David Hildenbrand166ecb32015-11-25 11:13:32 +0100376 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
David Hildenbranda3508fb2015-07-08 13:19:48 +0200377 prefix_unmapped_sync(cur);
378 }
379}
380
381/*
David Hildenbrand166ecb32015-11-25 11:13:32 +0100382 * Map the first prefix page and if tx is enabled also the second prefix page.
David Hildenbranda3508fb2015-07-08 13:19:48 +0200383 *
384 * The prefix will be protected, a gmap notifier will inform about unmaps.
385 * The shadow scb must not be executed until the prefix is remapped, this is
386 * guaranteed by properly handling PROG_REQUEST.
387 *
388 * Returns: - 0 on if successfully mapped or already mapped
389 * - > 0 if control has to be given to guest 2
390 * - -EAGAIN if the caller can retry immediately
391 * - -ENOMEM if out of memory
392 */
393static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
394{
395 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
396 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
397 int rc;
398
David Hildenbrand06d68a62016-04-22 13:50:09 +0200399 if (prefix_is_mapped(vsie_page))
400 return 0;
401
David Hildenbranda3508fb2015-07-08 13:19:48 +0200402 /* mark it as mapped so we can catch any concurrent unmappers */
403 prefix_mapped(vsie_page);
404
405 /* with mso/msl, the prefix lies at offset *mso* */
406 prefix += scb_s->mso;
407
408 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100409 if (!rc && (scb_s->ecb & ECB_TE))
David Hildenbrand166ecb32015-11-25 11:13:32 +0100410 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
411 prefix + PAGE_SIZE);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200412 /*
413 * We don't have to mprotect, we will be called for all unshadows.
414 * SIE will detect if protection applies and trigger a validity.
415 */
416 if (rc)
417 prefix_unmapped(vsie_page);
418 if (rc > 0 || rc == -EFAULT)
419 rc = set_validity_icpt(scb_s, 0x0037U);
420 return rc;
421}
422
423/*
424 * Pin the guest page given by gpa and set hpa to the pinned host address.
425 * Will always be pinned writable.
426 *
427 * Returns: - 0 on success
428 * - -EINVAL if the gpa is not valid guest storage
429 * - -ENOMEM if out of memory
430 */
431static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
432{
433 struct page *page;
434 hva_t hva;
435 int rc;
436
437 hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
438 if (kvm_is_error_hva(hva))
439 return -EINVAL;
440 rc = get_user_pages_fast(hva, 1, 1, &page);
441 if (rc < 0)
442 return rc;
443 else if (rc != 1)
444 return -ENOMEM;
445 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
446 return 0;
447}
448
449/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
450static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
451{
452 struct page *page;
453
454 page = virt_to_page(hpa);
455 set_page_dirty_lock(page);
456 put_page(page);
457 /* mark the page always as dirty for migration */
458 mark_page_dirty(kvm, gpa_to_gfn(gpa));
459}
460
461/* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
462static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
463{
464 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
465 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
466 hpa_t hpa;
467 gpa_t gpa;
468
469 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
470 if (hpa) {
471 gpa = scb_o->scaol & ~0xfUL;
David Hildenbrand19c439b2015-11-25 11:02:26 +0100472 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
473 gpa |= (u64) scb_o->scaoh << 32;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200474 unpin_guest_page(vcpu->kvm, gpa, hpa);
475 scb_s->scaol = 0;
476 scb_s->scaoh = 0;
477 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100478
479 hpa = scb_s->itdba;
480 if (hpa) {
481 gpa = scb_o->itdba & ~0xffUL;
482 unpin_guest_page(vcpu->kvm, gpa, hpa);
483 scb_s->itdba = 0;
484 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100485
486 hpa = scb_s->gvrd;
487 if (hpa) {
488 gpa = scb_o->gvrd & ~0x1ffUL;
489 unpin_guest_page(vcpu->kvm, gpa, hpa);
490 scb_s->gvrd = 0;
491 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100492
493 hpa = scb_s->riccbd;
494 if (hpa) {
495 gpa = scb_o->riccbd & ~0x3fUL;
496 unpin_guest_page(vcpu->kvm, gpa, hpa);
497 scb_s->riccbd = 0;
498 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200499}
500
501/*
502 * Instead of shadowing some blocks, we can simply forward them because the
503 * addresses in the scb are 64 bit long.
504 *
505 * This works as long as the data lies in one page. If blocks ever exceed one
506 * page, we have to fall back to shadowing.
507 *
508 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
509 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
510 *
511 * Returns: - 0 if all blocks were pinned.
512 * - > 0 if control has to be given to guest 2
513 * - -ENOMEM if out of memory
514 */
515static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
516{
517 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
518 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
519 hpa_t hpa;
520 gpa_t gpa;
521 int rc = 0;
522
523 gpa = scb_o->scaol & ~0xfUL;
David Hildenbrand19c439b2015-11-25 11:02:26 +0100524 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
525 gpa |= (u64) scb_o->scaoh << 32;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200526 if (gpa) {
527 if (!(gpa & ~0x1fffUL))
528 rc = set_validity_icpt(scb_s, 0x0038U);
529 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
530 rc = set_validity_icpt(scb_s, 0x0011U);
531 else if ((gpa & PAGE_MASK) !=
532 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
533 rc = set_validity_icpt(scb_s, 0x003bU);
534 if (!rc) {
535 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
536 if (rc == -EINVAL)
537 rc = set_validity_icpt(scb_s, 0x0034U);
538 }
539 if (rc)
540 goto unpin;
541 scb_s->scaoh = (u32)((u64)hpa >> 32);
542 scb_s->scaol = (u32)(u64)hpa;
543 }
David Hildenbrand166ecb32015-11-25 11:13:32 +0100544
545 gpa = scb_o->itdba & ~0xffUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100546 if (gpa && (scb_s->ecb & ECB_TE)) {
David Hildenbrand166ecb32015-11-25 11:13:32 +0100547 if (!(gpa & ~0x1fffU)) {
548 rc = set_validity_icpt(scb_s, 0x0080U);
549 goto unpin;
550 }
551 /* 256 bytes cannot cross page boundaries */
552 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
553 if (rc == -EINVAL)
554 rc = set_validity_icpt(scb_s, 0x0080U);
555 if (rc)
556 goto unpin;
557 scb_s->itdba = hpa;
558 }
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100559
560 gpa = scb_o->gvrd & ~0x1ffUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100561 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
David Hildenbrandc9bc1ea2015-11-25 11:08:32 +0100562 if (!(gpa & ~0x1fffUL)) {
563 rc = set_validity_icpt(scb_s, 0x1310U);
564 goto unpin;
565 }
566 /*
567 * 512 bytes vector registers cannot cross page boundaries
568 * if this block gets bigger, we have to shadow it.
569 */
570 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
571 if (rc == -EINVAL)
572 rc = set_validity_icpt(scb_s, 0x1310U);
573 if (rc)
574 goto unpin;
575 scb_s->gvrd = hpa;
576 }
David Hildenbrand588438c2016-01-26 12:51:06 +0100577
578 gpa = scb_o->riccbd & ~0x3fUL;
David Hildenbrand0c9d8682017-03-13 11:48:28 +0100579 if (gpa && (scb_s->ecb3 & ECB3_RI)) {
David Hildenbrand588438c2016-01-26 12:51:06 +0100580 if (!(gpa & ~0x1fffUL)) {
581 rc = set_validity_icpt(scb_s, 0x0043U);
582 goto unpin;
583 }
584 /* 64 bytes cannot cross page boundaries */
585 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
586 if (rc == -EINVAL)
587 rc = set_validity_icpt(scb_s, 0x0043U);
588 /* Validity 0x0044 will be checked by SIE */
589 if (rc)
590 goto unpin;
David Hildenbrand4d21cef32016-09-02 12:33:49 +0200591 scb_s->riccbd = hpa;
David Hildenbrand588438c2016-01-26 12:51:06 +0100592 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200593 return 0;
594unpin:
595 unpin_blocks(vcpu, vsie_page);
596 return rc;
597}
598
599/* unpin the scb provided by guest 2, marking it as dirty */
600static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
601 gpa_t gpa)
602{
603 hpa_t hpa = (hpa_t) vsie_page->scb_o;
604
605 if (hpa)
606 unpin_guest_page(vcpu->kvm, gpa, hpa);
607 vsie_page->scb_o = NULL;
608}
609
610/*
611 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
612 *
613 * Returns: - 0 if the scb was pinned.
614 * - > 0 if control has to be given to guest 2
615 * - -ENOMEM if out of memory
616 */
617static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
618 gpa_t gpa)
619{
620 hpa_t hpa;
621 int rc;
622
623 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
624 if (rc == -EINVAL) {
625 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
626 if (!rc)
627 rc = 1;
628 }
629 if (!rc)
630 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
631 return rc;
632}
633
634/*
635 * Inject a fault into guest 2.
636 *
637 * Returns: - > 0 if control has to be given to guest 2
638 * < 0 if an error occurred during injection.
639 */
640static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
641 bool write_flag)
642{
643 struct kvm_s390_pgm_info pgm = {
644 .code = code,
645 .trans_exc_code =
646 /* 0-51: virtual address */
647 (vaddr & 0xfffffffffffff000UL) |
648 /* 52-53: store / fetch */
649 (((unsigned int) !write_flag) + 1) << 10,
650 /* 62-63: asce id (alway primary == 0) */
651 .exc_access_id = 0, /* always primary */
652 .op_access_id = 0, /* not MVPG */
653 };
654 int rc;
655
656 if (code == PGM_PROTECTION)
657 pgm.trans_exc_code |= 0x4UL;
658
659 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
660 return rc ? rc : 1;
661}
662
663/*
664 * Handle a fault during vsie execution on a gmap shadow.
665 *
666 * Returns: - 0 if the fault was resolved
667 * - > 0 if control has to be given to guest 2
668 * - < 0 if an error occurred
669 */
670static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
671{
672 int rc;
673
674 if (current->thread.gmap_int_code == PGM_PROTECTION)
675 /* we can directly forward all protection exceptions */
676 return inject_fault(vcpu, PGM_PROTECTION,
677 current->thread.gmap_addr, 1);
678
679 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
680 current->thread.gmap_addr);
681 if (rc > 0) {
682 rc = inject_fault(vcpu, rc,
683 current->thread.gmap_addr,
684 current->thread.gmap_write_flag);
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200685 if (rc >= 0)
686 vsie_page->fault_addr = current->thread.gmap_addr;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200687 }
688 return rc;
689}
690
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200691/*
692 * Retry the previous fault that required guest 2 intervention. This avoids
693 * one superfluous SIE re-entry and direct exit.
694 *
695 * Will ignore any errors. The next SIE fault will do proper fault handling.
696 */
697static void handle_last_fault(struct kvm_vcpu *vcpu,
698 struct vsie_page *vsie_page)
699{
700 if (vsie_page->fault_addr)
701 kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
702 vsie_page->fault_addr);
703 vsie_page->fault_addr = 0;
704}
705
David Hildenbranda3508fb2015-07-08 13:19:48 +0200706static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
707{
708 vsie_page->scb_s.icptcode = 0;
709}
710
David Hildenbrand66b630d2015-11-26 14:11:19 +0100711/* rewind the psw and clear the vsie icpt, so we can retry execution */
712static void retry_vsie_icpt(struct vsie_page *vsie_page)
713{
714 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
715 int ilen = insn_length(scb_s->ipa >> 8);
716
717 /* take care of EXECUTE instructions */
718 if (scb_s->icptstatus & 1) {
719 ilen = (scb_s->icptstatus >> 4) & 0x6;
720 if (!ilen)
721 ilen = 4;
722 }
723 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
724 clear_vsie_icpt(vsie_page);
725}
726
727/*
728 * Try to shadow + enable the guest 2 provided facility list.
729 * Retry instruction execution if enabled for and provided by guest 2.
730 *
731 * Returns: - 0 if handled (retry or guest 2 icpt)
732 * - > 0 if control has to be given to guest 2
733 */
734static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
735{
736 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
737 __u32 fac = vsie_page->scb_o->fac & 0x7ffffff8U;
738
739 if (fac && test_kvm_facility(vcpu->kvm, 7)) {
740 retry_vsie_icpt(vsie_page);
741 if (read_guest_real(vcpu, fac, &vsie_page->fac,
742 sizeof(vsie_page->fac)))
743 return set_validity_icpt(scb_s, 0x1090U);
744 scb_s->fac = (__u32)(__u64) &vsie_page->fac;
745 }
746 return 0;
747}
748
David Hildenbranda3508fb2015-07-08 13:19:48 +0200749/*
750 * Run the vsie on a shadow scb and a shadow gmap, without any further
751 * sanity checks, handling SIE faults.
752 *
753 * Returns: - 0 everything went fine
754 * - > 0 if control has to be given to guest 2
755 * - < 0 if an error occurred
756 */
757static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
758{
759 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
760 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
761 int rc;
762
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200763 handle_last_fault(vcpu, vsie_page);
764
David Hildenbranda3508fb2015-07-08 13:19:48 +0200765 if (need_resched())
766 schedule();
767 if (test_cpu_flag(CIF_MCCK_PENDING))
768 s390_handle_mcck();
769
770 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
771 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200772 guest_enter_irqoff();
David Hildenbranda3508fb2015-07-08 13:19:48 +0200773 local_irq_enable();
774
775 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
776
777 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200778 guest_exit_irqoff();
David Hildenbranda3508fb2015-07-08 13:19:48 +0200779 local_irq_enable();
780 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
781
782 if (rc > 0)
783 rc = 0; /* we could still have an icpt */
784 else if (rc == -EFAULT)
785 return handle_fault(vcpu, vsie_page);
786
787 switch (scb_s->icptcode) {
David Hildenbrand66b630d2015-11-26 14:11:19 +0100788 case ICPT_INST:
789 if (scb_s->ipa == 0xb2b0)
790 rc = handle_stfle(vcpu, vsie_page);
791 break;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200792 case ICPT_STOP:
793 /* stop not requested by g2 - must have been a kick */
794 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
795 clear_vsie_icpt(vsie_page);
796 break;
797 case ICPT_VALIDITY:
798 if ((scb_s->ipa & 0xf000) != 0xf000)
799 scb_s->ipa += 0x1000;
800 break;
801 }
802 return rc;
803}
804
805static void release_gmap_shadow(struct vsie_page *vsie_page)
806{
807 if (vsie_page->gmap)
808 gmap_put(vsie_page->gmap);
809 WRITE_ONCE(vsie_page->gmap, NULL);
David Hildenbrand06d68a62016-04-22 13:50:09 +0200810 prefix_unmapped(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200811}
812
813static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
814 struct vsie_page *vsie_page)
815{
816 unsigned long asce;
817 union ctlreg0 cr0;
818 struct gmap *gmap;
819 int edat;
820
821 asce = vcpu->arch.sie_block->gcr[1];
822 cr0.val = vcpu->arch.sie_block->gcr[0];
823 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
824 edat += edat && test_kvm_facility(vcpu->kvm, 78);
825
David Hildenbrand06d68a62016-04-22 13:50:09 +0200826 /*
827 * ASCE or EDAT could have changed since last icpt, or the gmap
828 * we're holding has been unshadowed. If the gmap is still valid,
829 * we can safely reuse it.
830 */
831 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
832 return 0;
833
834 /* release the old shadow - if any, and mark the prefix as unmapped */
835 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200836 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
837 if (IS_ERR(gmap))
838 return PTR_ERR(gmap);
839 gmap->private = vcpu->kvm;
840 WRITE_ONCE(vsie_page->gmap, gmap);
841 return 0;
842}
843
844/*
David Hildenbrandadbf1692016-05-27 22:03:52 +0200845 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
846 */
847static void register_shadow_scb(struct kvm_vcpu *vcpu,
848 struct vsie_page *vsie_page)
849{
David Hildenbrand91473b42015-10-29 10:30:36 +0100850 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
851
David Hildenbrandadbf1692016-05-27 22:03:52 +0200852 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
David Hildenbrandb917ae52015-07-07 20:39:35 +0200853 /*
854 * External calls have to lead to a kick of the vcpu and
855 * therefore the vsie -> Simulate Wait state.
856 */
857 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand91473b42015-10-29 10:30:36 +0100858 /*
859 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
860 * automatically be adjusted on tod clock changes via kvm_sync_clock.
861 */
862 preempt_disable();
863 scb_s->epoch += vcpu->kvm->arch.epoch;
864 preempt_enable();
David Hildenbrandadbf1692016-05-27 22:03:52 +0200865}
866
867/*
868 * Unregister a shadow scb from a VCPU.
869 */
870static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
871{
David Hildenbrandb917ae52015-07-07 20:39:35 +0200872 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
David Hildenbrandadbf1692016-05-27 22:03:52 +0200873 WRITE_ONCE(vcpu->arch.vsie_block, NULL);
874}
875
876/*
David Hildenbranda3508fb2015-07-08 13:19:48 +0200877 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
878 * prefix pages and faults.
879 *
880 * Returns: - 0 if no errors occurred
881 * - > 0 if control has to be given to guest 2
882 * - -ENOMEM if out of memory
883 */
884static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
885{
886 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
887 int rc = 0;
888
889 while (1) {
890 rc = acquire_gmap_shadow(vcpu, vsie_page);
891 if (!rc)
892 rc = map_prefix(vcpu, vsie_page);
893 if (!rc) {
894 gmap_enable(vsie_page->gmap);
895 update_intervention_requests(vsie_page);
896 rc = do_vsie_run(vcpu, vsie_page);
897 gmap_enable(vcpu->arch.gmap);
898 }
David Hildenbrandadbf1692016-05-27 22:03:52 +0200899 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200900
901 if (rc == -EAGAIN)
902 rc = 0;
903 if (rc || scb_s->icptcode || signal_pending(current) ||
904 kvm_s390_vcpu_has_irq(vcpu, 0))
905 break;
Heiko Carstens0b925152017-01-02 08:51:02 +0100906 }
David Hildenbranda3508fb2015-07-08 13:19:48 +0200907
908 if (rc == -EFAULT) {
909 /*
910 * Addressing exceptions are always presentes as intercepts.
911 * As addressing exceptions are suppressing and our guest 3 PSW
912 * points at the responsible instruction, we have to
913 * forward the PSW and set the ilc. If we can't read guest 3
914 * instruction, we can use an arbitrary ilc. Let's always use
915 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
916 * memory. (we could also fake the shadow so the hardware
917 * handles it).
918 */
919 scb_s->icptcode = ICPT_PROGI;
920 scb_s->iprcc = PGM_ADDRESSING;
921 scb_s->pgmilc = 4;
922 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
923 }
924 return rc;
925}
926
927/*
928 * Get or create a vsie page for a scb address.
929 *
930 * Returns: - address of a vsie page (cached or new one)
931 * - NULL if the same scb address is already used by another VCPU
932 * - ERR_PTR(-ENOMEM) if out of memory
933 */
934static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
935{
936 struct vsie_page *vsie_page;
937 struct page *page;
938 int nr_vcpus;
939
940 rcu_read_lock();
941 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
942 rcu_read_unlock();
943 if (page) {
944 if (page_ref_inc_return(page) == 2)
945 return page_to_virt(page);
946 page_ref_dec(page);
947 }
948
949 /*
950 * We want at least #online_vcpus shadows, so every VCPU can execute
951 * the VSIE in parallel.
952 */
953 nr_vcpus = atomic_read(&kvm->online_vcpus);
954
955 mutex_lock(&kvm->arch.vsie.mutex);
956 if (kvm->arch.vsie.page_count < nr_vcpus) {
David Hildenbrand66b630d2015-11-26 14:11:19 +0100957 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
David Hildenbranda3508fb2015-07-08 13:19:48 +0200958 if (!page) {
959 mutex_unlock(&kvm->arch.vsie.mutex);
960 return ERR_PTR(-ENOMEM);
961 }
962 page_ref_inc(page);
963 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
964 kvm->arch.vsie.page_count++;
965 } else {
966 /* reuse an existing entry that belongs to nobody */
967 while (true) {
968 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
969 if (page_ref_inc_return(page) == 2)
970 break;
971 page_ref_dec(page);
972 kvm->arch.vsie.next++;
973 kvm->arch.vsie.next %= nr_vcpus;
974 }
975 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
976 }
977 page->index = addr;
978 /* double use of the same address */
979 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
980 page_ref_dec(page);
981 mutex_unlock(&kvm->arch.vsie.mutex);
982 return NULL;
983 }
984 mutex_unlock(&kvm->arch.vsie.mutex);
985
986 vsie_page = page_to_virt(page);
987 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
David Hildenbrand06d68a62016-04-22 13:50:09 +0200988 release_gmap_shadow(vsie_page);
David Hildenbrand1b7029b2015-07-08 13:25:31 +0200989 vsie_page->fault_addr = 0;
David Hildenbranda3508fb2015-07-08 13:19:48 +0200990 vsie_page->scb_s.ihcpu = 0xffffU;
991 return vsie_page;
992}
993
994/* put a vsie page acquired via get_vsie_page */
995static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
996{
997 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
998
999 page_ref_dec(page);
1000}
1001
1002int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
1003{
1004 struct vsie_page *vsie_page;
1005 unsigned long scb_addr;
1006 int rc;
1007
1008 vcpu->stat.instruction_sie++;
1009 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
1010 return -EOPNOTSUPP;
1011 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1012 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1013
1014 BUILD_BUG_ON(sizeof(struct vsie_page) != 4096);
1015 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
1016
1017 /* 512 byte alignment */
1018 if (unlikely(scb_addr & 0x1ffUL))
1019 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1020
1021 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
1022 return 0;
1023
1024 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
1025 if (IS_ERR(vsie_page))
1026 return PTR_ERR(vsie_page);
1027 else if (!vsie_page)
1028 /* double use of sie control block - simply do nothing */
1029 return 0;
1030
1031 rc = pin_scb(vcpu, vsie_page, scb_addr);
1032 if (rc)
1033 goto out_put;
1034 rc = shadow_scb(vcpu, vsie_page);
1035 if (rc)
1036 goto out_unpin_scb;
1037 rc = pin_blocks(vcpu, vsie_page);
1038 if (rc)
1039 goto out_unshadow;
David Hildenbrandadbf1692016-05-27 22:03:52 +02001040 register_shadow_scb(vcpu, vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001041 rc = vsie_run(vcpu, vsie_page);
David Hildenbrandadbf1692016-05-27 22:03:52 +02001042 unregister_shadow_scb(vcpu);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001043 unpin_blocks(vcpu, vsie_page);
1044out_unshadow:
1045 unshadow_scb(vcpu, vsie_page);
1046out_unpin_scb:
1047 unpin_scb(vcpu, vsie_page, scb_addr);
1048out_put:
1049 put_vsie_page(vcpu->kvm, vsie_page);
1050
1051 return rc < 0 ? rc : 0;
1052}
1053
1054/* Init the vsie data structures. To be called when a vm is initialized. */
1055void kvm_s390_vsie_init(struct kvm *kvm)
1056{
1057 mutex_init(&kvm->arch.vsie.mutex);
1058 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
1059}
1060
1061/* Destroy the vsie data structures. To be called when a vm is destroyed. */
1062void kvm_s390_vsie_destroy(struct kvm *kvm)
1063{
David Hildenbrand06d68a62016-04-22 13:50:09 +02001064 struct vsie_page *vsie_page;
David Hildenbranda3508fb2015-07-08 13:19:48 +02001065 struct page *page;
1066 int i;
1067
1068 mutex_lock(&kvm->arch.vsie.mutex);
1069 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
1070 page = kvm->arch.vsie.pages[i];
1071 kvm->arch.vsie.pages[i] = NULL;
David Hildenbrand06d68a62016-04-22 13:50:09 +02001072 vsie_page = page_to_virt(page);
1073 release_gmap_shadow(vsie_page);
David Hildenbranda3508fb2015-07-08 13:19:48 +02001074 /* free the radix tree entry */
1075 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
1076 __free_page(page);
1077 }
1078 kvm->arch.vsie.page_count = 0;
1079 mutex_unlock(&kvm->arch.vsie.mutex);
1080}
David Hildenbrandadbf1692016-05-27 22:03:52 +02001081
1082void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
1083{
1084 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
1085
1086 /*
1087 * Even if the VCPU lets go of the shadow sie block reference, it is
1088 * still valid in the cache. So we can safely kick it.
1089 */
1090 if (scb) {
1091 atomic_or(PROG_BLOCK_SIE, &scb->prog20);
1092 if (scb->prog0c & PROG_IN_SIE)
1093 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);
1094 }
1095}