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