2 * kvm nested virtualization support for s390x
4 * Copyright IBM Corp. 2016
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.
10 * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
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>
18 #include <asm/mmu_context.h>
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 */
31 /* address of the last reported fault to guest2 */
32 unsigned long fault_addr
; /* 0x0210 */
33 __u8 reserved
[0x0700 - 0x0218]; /* 0x0218 */
34 struct kvm_s390_crypto_cb crycb
; /* 0x0700 */
35 __u8 fac
[S390_ARCH_FAC_LIST_SIZE_BYTE
]; /* 0x0800 */
38 /* trigger a validity icpt for the given scb */
39 static int set_validity_icpt(struct kvm_s390_sie_block
*scb
,
43 scb
->ipb
= ((__u32
) reason_code
) << 16;
44 scb
->icptcode
= ICPT_VALIDITY
;
48 /* mark the prefix as unmapped, this will block the VSIE */
49 static void prefix_unmapped(struct vsie_page
*vsie_page
)
51 atomic_or(PROG_REQUEST
, &vsie_page
->scb_s
.prog20
);
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
)
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
)
64 /* mark the prefix as mapped, this will allow the VSIE to run */
65 static void prefix_mapped(struct vsie_page
*vsie_page
)
67 atomic_andnot(PROG_REQUEST
, &vsie_page
->scb_s
.prog20
);
70 /* test if the prefix is mapped into the gmap shadow */
71 static int prefix_is_mapped(struct vsie_page
*vsie_page
)
73 return !(atomic_read(&vsie_page
->scb_s
.prog20
) & PROG_REQUEST
);
76 /* copy the updated intervention request bits into the shadow scb */
77 static void update_intervention_requests(struct vsie_page
*vsie_page
)
79 const int bits
= CPUSTAT_STOP_INT
| CPUSTAT_IO_INT
| CPUSTAT_EXT_INT
;
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
);
87 /* shadow (filter and validate) the cpuflags */
88 static int prepare_cpuflags(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
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
);
94 /* we don't allow ESA/390 guests */
95 if (!(cpuflags
& CPUSTAT_ZARCH
))
96 return set_validity_icpt(scb_s
, 0x0001U
);
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
);
103 /* intervention requests will be set later */
104 newflags
= CPUSTAT_ZARCH
;
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
;
112 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_GPERE
))
113 newflags
|= cpuflags
& CPUSTAT_P
;
114 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_GSLS
))
115 newflags
|= cpuflags
& CPUSTAT_SM
;
116 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_IBS
))
117 newflags
|= cpuflags
& CPUSTAT_IBS
;
119 atomic_set(&scb_s
->cpuflags
, newflags
);
124 * Create a shadow copy of the crycb block and setup key wrapping, if
125 * requested for guest 3 and enabled for guest 2.
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.
130 * Returns: - 0 if shadowed or nothing to do
131 * - > 0 if control has to be given to guest 2
133 static int shadow_crycb(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
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
;
142 if (!(scb_o
->crycbd
& vcpu
->arch
.sie_block
->crycbd
& CRYCB_FORMAT1
))
144 /* format-1 is supported with message-security-assist extension 3 */
145 if (!test_kvm_facility(vcpu
->kvm
, 76))
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
);
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
);
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
);
162 scb_s
->ecb3
|= ecb3_flags
;
163 scb_s
->crycbd
= ((__u32
)(__u64
) &vsie_page
->crycb
) | CRYCB_FORMAT1
|
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);
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
)
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
;
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
;
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
)
198 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
199 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
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
;
209 scb_o
->cputm
= scb_s
->cputm
;
210 scb_o
->ckc
= scb_s
->ckc
;
211 scb_o
->todpr
= scb_s
->todpr
;
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
;
220 /* interrupt intercept */
221 switch (scb_s
->icptcode
) {
225 memcpy((void *)((u64
)scb_o
+ 0xc0),
226 (void *)((u64
)scb_s
+ 0xc0), 0xf0 - 0xc0);
230 memcpy((void *)((u64
)scb_o
+ 0xc0),
231 (void *)((u64
)scb_s
+ 0xc0), 0xd0 - 0xc0);
235 if (scb_s
->ihcpu
!= 0xffffU
)
236 scb_o
->ihcpu
= scb_s
->ihcpu
;
240 * Setup the shadow scb by copying and checking the relevant parts of the g2
243 * Returns: - 0 if the scb has been shadowed
244 * - > 0 if control has to be given to guest 2
246 static int shadow_scb(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
248 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
249 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
250 bool had_tx
= scb_s
->ecb
& 0x10U
;
251 unsigned long new_mso
= 0;
254 /* make sure we don't have any leftovers when reusing the scb */
263 rc
= prepare_cpuflags(vcpu
, vsie_page
);
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
;
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
;
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
;
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.
290 scb_s
->ictl
|= ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
;
291 scb_s
->icpua
= scb_o
->icpua
;
293 if (!(atomic_read(&scb_s
->cpuflags
) & CPUSTAT_SM
))
294 new_mso
= scb_o
->mso
& 0xfffffffffff00000UL
;
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
);
298 /* SIE will do mso/msl validity and exception checks for us */
299 scb_s
->msl
= scb_o
->msl
& 0xfffffffffff00000UL
;
300 scb_s
->mso
= new_mso
;
301 scb_s
->prefix
= scb_o
->prefix
;
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
;
307 /* MVPG and Protection Exception Interpretation are always available */
308 scb_s
->eca
|= scb_o
->eca
& 0x01002000U
;
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
;
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
;
320 if (test_kvm_facility(vcpu
->kvm
, 129)) {
321 scb_s
->eca
|= scb_o
->eca
& 0x00020000U
;
322 scb_s
->ecd
|= scb_o
->ecd
& 0x20000000U
;
324 /* Run-time-Instrumentation */
325 if (test_kvm_facility(vcpu
->kvm
, 64))
326 scb_s
->ecb3
|= scb_o
->ecb3
& 0x01U
;
327 /* Instruction Execution Prevention */
328 if (test_kvm_facility(vcpu
->kvm
, 130))
329 scb_s
->ecb2
|= scb_o
->ecb2
& 0x20U
;
330 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_SIIF
))
331 scb_s
->eca
|= scb_o
->eca
& 0x00000001U
;
332 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_IB
))
333 scb_s
->eca
|= scb_o
->eca
& 0x40000000U
;
334 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_CEI
))
335 scb_s
->eca
|= scb_o
->eca
& 0x80000000U
;
337 prepare_ibc(vcpu
, vsie_page
);
338 rc
= shadow_crycb(vcpu
, vsie_page
);
341 unshadow_scb(vcpu
, vsie_page
);
345 void kvm_s390_vsie_gmap_notifier(struct gmap
*gmap
, unsigned long start
,
348 struct kvm
*kvm
= gmap
->private;
349 struct vsie_page
*cur
;
350 unsigned long prefix
;
354 if (!gmap_is_shadow(gmap
))
356 if (start
>= 1UL << 31)
357 /* We are only interested in prefix pages */
361 * Only new shadow blocks are added to the list during runtime,
362 * therefore we can safely reference them all the time.
364 for (i
= 0; i
< kvm
->arch
.vsie
.page_count
; i
++) {
365 page
= READ_ONCE(kvm
->arch
.vsie
.pages
[i
]);
368 cur
= page_to_virt(page
);
369 if (READ_ONCE(cur
->gmap
) != gmap
)
371 prefix
= cur
->scb_s
.prefix
<< GUEST_PREFIX_SHIFT
;
372 /* with mso/msl, the prefix lies at an offset */
373 prefix
+= cur
->scb_s
.mso
;
374 if (prefix
<= end
&& start
<= prefix
+ 2 * PAGE_SIZE
- 1)
375 prefix_unmapped_sync(cur
);
380 * Map the first prefix page and if tx is enabled also the second prefix page.
382 * The prefix will be protected, a gmap notifier will inform about unmaps.
383 * The shadow scb must not be executed until the prefix is remapped, this is
384 * guaranteed by properly handling PROG_REQUEST.
386 * Returns: - 0 on if successfully mapped or already mapped
387 * - > 0 if control has to be given to guest 2
388 * - -EAGAIN if the caller can retry immediately
389 * - -ENOMEM if out of memory
391 static int map_prefix(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
393 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
394 u64 prefix
= scb_s
->prefix
<< GUEST_PREFIX_SHIFT
;
397 if (prefix_is_mapped(vsie_page
))
400 /* mark it as mapped so we can catch any concurrent unmappers */
401 prefix_mapped(vsie_page
);
403 /* with mso/msl, the prefix lies at offset *mso* */
404 prefix
+= scb_s
->mso
;
406 rc
= kvm_s390_shadow_fault(vcpu
, vsie_page
->gmap
, prefix
);
407 if (!rc
&& (scb_s
->ecb
& 0x10U
))
408 rc
= kvm_s390_shadow_fault(vcpu
, vsie_page
->gmap
,
411 * We don't have to mprotect, we will be called for all unshadows.
412 * SIE will detect if protection applies and trigger a validity.
415 prefix_unmapped(vsie_page
);
416 if (rc
> 0 || rc
== -EFAULT
)
417 rc
= set_validity_icpt(scb_s
, 0x0037U
);
422 * Pin the guest page given by gpa and set hpa to the pinned host address.
423 * Will always be pinned writable.
425 * Returns: - 0 on success
426 * - -EINVAL if the gpa is not valid guest storage
427 * - -ENOMEM if out of memory
429 static int pin_guest_page(struct kvm
*kvm
, gpa_t gpa
, hpa_t
*hpa
)
435 hva
= gfn_to_hva(kvm
, gpa_to_gfn(gpa
));
436 if (kvm_is_error_hva(hva
))
438 rc
= get_user_pages_fast(hva
, 1, 1, &page
);
443 *hpa
= (hpa_t
) page_to_virt(page
) + (gpa
& ~PAGE_MASK
);
447 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
448 static void unpin_guest_page(struct kvm
*kvm
, gpa_t gpa
, hpa_t hpa
)
452 page
= virt_to_page(hpa
);
453 set_page_dirty_lock(page
);
455 /* mark the page always as dirty for migration */
456 mark_page_dirty(kvm
, gpa_to_gfn(gpa
));
459 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
460 static void unpin_blocks(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
462 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
463 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
467 hpa
= (u64
) scb_s
->scaoh
<< 32 | scb_s
->scaol
;
469 gpa
= scb_o
->scaol
& ~0xfUL
;
470 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_64BSCAO
))
471 gpa
|= (u64
) scb_o
->scaoh
<< 32;
472 unpin_guest_page(vcpu
->kvm
, gpa
, hpa
);
479 gpa
= scb_o
->itdba
& ~0xffUL
;
480 unpin_guest_page(vcpu
->kvm
, gpa
, hpa
);
486 gpa
= scb_o
->gvrd
& ~0x1ffUL
;
487 unpin_guest_page(vcpu
->kvm
, gpa
, hpa
);
493 gpa
= scb_o
->riccbd
& ~0x3fUL
;
494 unpin_guest_page(vcpu
->kvm
, gpa
, hpa
);
500 * Instead of shadowing some blocks, we can simply forward them because the
501 * addresses in the scb are 64 bit long.
503 * This works as long as the data lies in one page. If blocks ever exceed one
504 * page, we have to fall back to shadowing.
506 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
507 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
509 * Returns: - 0 if all blocks were pinned.
510 * - > 0 if control has to be given to guest 2
511 * - -ENOMEM if out of memory
513 static int pin_blocks(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
515 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
516 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
521 gpa
= scb_o
->scaol
& ~0xfUL
;
522 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_64BSCAO
))
523 gpa
|= (u64
) scb_o
->scaoh
<< 32;
525 if (!(gpa
& ~0x1fffUL
))
526 rc
= set_validity_icpt(scb_s
, 0x0038U
);
527 else if ((gpa
& ~0x1fffUL
) == kvm_s390_get_prefix(vcpu
))
528 rc
= set_validity_icpt(scb_s
, 0x0011U
);
529 else if ((gpa
& PAGE_MASK
) !=
530 ((gpa
+ sizeof(struct bsca_block
) - 1) & PAGE_MASK
))
531 rc
= set_validity_icpt(scb_s
, 0x003bU
);
533 rc
= pin_guest_page(vcpu
->kvm
, gpa
, &hpa
);
535 rc
= set_validity_icpt(scb_s
, 0x0034U
);
539 scb_s
->scaoh
= (u32
)((u64
)hpa
>> 32);
540 scb_s
->scaol
= (u32
)(u64
)hpa
;
543 gpa
= scb_o
->itdba
& ~0xffUL
;
544 if (gpa
&& (scb_s
->ecb
& 0x10U
)) {
545 if (!(gpa
& ~0x1fffU
)) {
546 rc
= set_validity_icpt(scb_s
, 0x0080U
);
549 /* 256 bytes cannot cross page boundaries */
550 rc
= pin_guest_page(vcpu
->kvm
, gpa
, &hpa
);
552 rc
= set_validity_icpt(scb_s
, 0x0080U
);
558 gpa
= scb_o
->gvrd
& ~0x1ffUL
;
559 if (gpa
&& (scb_s
->eca
& 0x00020000U
) &&
560 !(scb_s
->ecd
& 0x20000000U
)) {
561 if (!(gpa
& ~0x1fffUL
)) {
562 rc
= set_validity_icpt(scb_s
, 0x1310U
);
566 * 512 bytes vector registers cannot cross page boundaries
567 * if this block gets bigger, we have to shadow it.
569 rc
= pin_guest_page(vcpu
->kvm
, gpa
, &hpa
);
571 rc
= set_validity_icpt(scb_s
, 0x1310U
);
577 gpa
= scb_o
->riccbd
& ~0x3fUL
;
578 if (gpa
&& (scb_s
->ecb3
& 0x01U
)) {
579 if (!(gpa
& ~0x1fffUL
)) {
580 rc
= set_validity_icpt(scb_s
, 0x0043U
);
583 /* 64 bytes cannot cross page boundaries */
584 rc
= pin_guest_page(vcpu
->kvm
, gpa
, &hpa
);
586 rc
= set_validity_icpt(scb_s
, 0x0043U
);
587 /* Validity 0x0044 will be checked by SIE */
594 unpin_blocks(vcpu
, vsie_page
);
598 /* unpin the scb provided by guest 2, marking it as dirty */
599 static void unpin_scb(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
,
602 hpa_t hpa
= (hpa_t
) vsie_page
->scb_o
;
605 unpin_guest_page(vcpu
->kvm
, gpa
, hpa
);
606 vsie_page
->scb_o
= NULL
;
610 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
612 * Returns: - 0 if the scb was pinned.
613 * - > 0 if control has to be given to guest 2
614 * - -ENOMEM if out of memory
616 static int pin_scb(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
,
622 rc
= pin_guest_page(vcpu
->kvm
, gpa
, &hpa
);
624 rc
= kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
629 vsie_page
->scb_o
= (struct kvm_s390_sie_block
*) hpa
;
634 * Inject a fault into guest 2.
636 * Returns: - > 0 if control has to be given to guest 2
637 * < 0 if an error occurred during injection.
639 static int inject_fault(struct kvm_vcpu
*vcpu
, __u16 code
, __u64 vaddr
,
642 struct kvm_s390_pgm_info pgm
= {
645 /* 0-51: virtual address */
646 (vaddr
& 0xfffffffffffff000UL
) |
647 /* 52-53: store / fetch */
648 (((unsigned int) !write_flag
) + 1) << 10,
649 /* 62-63: asce id (alway primary == 0) */
650 .exc_access_id
= 0, /* always primary */
651 .op_access_id
= 0, /* not MVPG */
655 if (code
== PGM_PROTECTION
)
656 pgm
.trans_exc_code
|= 0x4UL
;
658 rc
= kvm_s390_inject_prog_irq(vcpu
, &pgm
);
663 * Handle a fault during vsie execution on a gmap shadow.
665 * Returns: - 0 if the fault was resolved
666 * - > 0 if control has to be given to guest 2
667 * - < 0 if an error occurred
669 static int handle_fault(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
673 if (current
->thread
.gmap_int_code
== PGM_PROTECTION
)
674 /* we can directly forward all protection exceptions */
675 return inject_fault(vcpu
, PGM_PROTECTION
,
676 current
->thread
.gmap_addr
, 1);
678 rc
= kvm_s390_shadow_fault(vcpu
, vsie_page
->gmap
,
679 current
->thread
.gmap_addr
);
681 rc
= inject_fault(vcpu
, rc
,
682 current
->thread
.gmap_addr
,
683 current
->thread
.gmap_write_flag
);
685 vsie_page
->fault_addr
= current
->thread
.gmap_addr
;
691 * Retry the previous fault that required guest 2 intervention. This avoids
692 * one superfluous SIE re-entry and direct exit.
694 * Will ignore any errors. The next SIE fault will do proper fault handling.
696 static void handle_last_fault(struct kvm_vcpu
*vcpu
,
697 struct vsie_page
*vsie_page
)
699 if (vsie_page
->fault_addr
)
700 kvm_s390_shadow_fault(vcpu
, vsie_page
->gmap
,
701 vsie_page
->fault_addr
);
702 vsie_page
->fault_addr
= 0;
705 static inline void clear_vsie_icpt(struct vsie_page
*vsie_page
)
707 vsie_page
->scb_s
.icptcode
= 0;
710 /* rewind the psw and clear the vsie icpt, so we can retry execution */
711 static void retry_vsie_icpt(struct vsie_page
*vsie_page
)
713 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
714 int ilen
= insn_length(scb_s
->ipa
>> 8);
716 /* take care of EXECUTE instructions */
717 if (scb_s
->icptstatus
& 1) {
718 ilen
= (scb_s
->icptstatus
>> 4) & 0x6;
722 scb_s
->gpsw
.addr
= __rewind_psw(scb_s
->gpsw
, ilen
);
723 clear_vsie_icpt(vsie_page
);
727 * Try to shadow + enable the guest 2 provided facility list.
728 * Retry instruction execution if enabled for and provided by guest 2.
730 * Returns: - 0 if handled (retry or guest 2 icpt)
731 * - > 0 if control has to be given to guest 2
733 static int handle_stfle(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
735 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
736 __u32 fac
= vsie_page
->scb_o
->fac
& 0x7ffffff8U
;
738 if (fac
&& test_kvm_facility(vcpu
->kvm
, 7)) {
739 retry_vsie_icpt(vsie_page
);
740 if (read_guest_real(vcpu
, fac
, &vsie_page
->fac
,
741 sizeof(vsie_page
->fac
)))
742 return set_validity_icpt(scb_s
, 0x1090U
);
743 scb_s
->fac
= (__u32
)(__u64
) &vsie_page
->fac
;
749 * Run the vsie on a shadow scb and a shadow gmap, without any further
750 * sanity checks, handling SIE faults.
752 * Returns: - 0 everything went fine
753 * - > 0 if control has to be given to guest 2
754 * - < 0 if an error occurred
756 static int do_vsie_run(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
758 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
759 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
762 handle_last_fault(vcpu
, vsie_page
);
766 if (test_cpu_flag(CIF_MCCK_PENDING
))
769 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
771 guest_enter_irqoff();
774 rc
= sie64a(scb_s
, vcpu
->run
->s
.regs
.gprs
);
779 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
782 rc
= 0; /* we could still have an icpt */
783 else if (rc
== -EFAULT
)
784 return handle_fault(vcpu
, vsie_page
);
786 switch (scb_s
->icptcode
) {
788 if (scb_s
->ipa
== 0xb2b0)
789 rc
= handle_stfle(vcpu
, vsie_page
);
792 /* stop not requested by g2 - must have been a kick */
793 if (!(atomic_read(&scb_o
->cpuflags
) & CPUSTAT_STOP_INT
))
794 clear_vsie_icpt(vsie_page
);
797 if ((scb_s
->ipa
& 0xf000) != 0xf000)
798 scb_s
->ipa
+= 0x1000;
804 static void release_gmap_shadow(struct vsie_page
*vsie_page
)
807 gmap_put(vsie_page
->gmap
);
808 WRITE_ONCE(vsie_page
->gmap
, NULL
);
809 prefix_unmapped(vsie_page
);
812 static int acquire_gmap_shadow(struct kvm_vcpu
*vcpu
,
813 struct vsie_page
*vsie_page
)
820 asce
= vcpu
->arch
.sie_block
->gcr
[1];
821 cr0
.val
= vcpu
->arch
.sie_block
->gcr
[0];
822 edat
= cr0
.edat
&& test_kvm_facility(vcpu
->kvm
, 8);
823 edat
+= edat
&& test_kvm_facility(vcpu
->kvm
, 78);
826 * ASCE or EDAT could have changed since last icpt, or the gmap
827 * we're holding has been unshadowed. If the gmap is still valid,
828 * we can safely reuse it.
830 if (vsie_page
->gmap
&& gmap_shadow_valid(vsie_page
->gmap
, asce
, edat
))
833 /* release the old shadow - if any, and mark the prefix as unmapped */
834 release_gmap_shadow(vsie_page
);
835 gmap
= gmap_shadow(vcpu
->arch
.gmap
, asce
, edat
);
837 return PTR_ERR(gmap
);
838 gmap
->private = vcpu
->kvm
;
839 WRITE_ONCE(vsie_page
->gmap
, gmap
);
844 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
846 static void register_shadow_scb(struct kvm_vcpu
*vcpu
,
847 struct vsie_page
*vsie_page
)
849 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
851 WRITE_ONCE(vcpu
->arch
.vsie_block
, &vsie_page
->scb_s
);
853 * External calls have to lead to a kick of the vcpu and
854 * therefore the vsie -> Simulate Wait state.
856 atomic_or(CPUSTAT_WAIT
, &vcpu
->arch
.sie_block
->cpuflags
);
858 * We have to adjust the g3 epoch by the g2 epoch. The epoch will
859 * automatically be adjusted on tod clock changes via kvm_sync_clock.
862 scb_s
->epoch
+= vcpu
->kvm
->arch
.epoch
;
867 * Unregister a shadow scb from a VCPU.
869 static void unregister_shadow_scb(struct kvm_vcpu
*vcpu
)
871 atomic_andnot(CPUSTAT_WAIT
, &vcpu
->arch
.sie_block
->cpuflags
);
872 WRITE_ONCE(vcpu
->arch
.vsie_block
, NULL
);
876 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
877 * prefix pages and faults.
879 * Returns: - 0 if no errors occurred
880 * - > 0 if control has to be given to guest 2
881 * - -ENOMEM if out of memory
883 static int vsie_run(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
885 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
889 rc
= acquire_gmap_shadow(vcpu
, vsie_page
);
891 rc
= map_prefix(vcpu
, vsie_page
);
893 gmap_enable(vsie_page
->gmap
);
894 update_intervention_requests(vsie_page
);
895 rc
= do_vsie_run(vcpu
, vsie_page
);
896 gmap_enable(vcpu
->arch
.gmap
);
898 atomic_andnot(PROG_BLOCK_SIE
, &scb_s
->prog20
);
902 if (rc
|| scb_s
->icptcode
|| signal_pending(current
) ||
903 kvm_s390_vcpu_has_irq(vcpu
, 0))
909 * Addressing exceptions are always presentes as intercepts.
910 * As addressing exceptions are suppressing and our guest 3 PSW
911 * points at the responsible instruction, we have to
912 * forward the PSW and set the ilc. If we can't read guest 3
913 * instruction, we can use an arbitrary ilc. Let's always use
914 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
915 * memory. (we could also fake the shadow so the hardware
918 scb_s
->icptcode
= ICPT_PROGI
;
919 scb_s
->iprcc
= PGM_ADDRESSING
;
921 scb_s
->gpsw
.addr
= __rewind_psw(scb_s
->gpsw
, 4);
927 * Get or create a vsie page for a scb address.
929 * Returns: - address of a vsie page (cached or new one)
930 * - NULL if the same scb address is already used by another VCPU
931 * - ERR_PTR(-ENOMEM) if out of memory
933 static struct vsie_page
*get_vsie_page(struct kvm
*kvm
, unsigned long addr
)
935 struct vsie_page
*vsie_page
;
940 page
= radix_tree_lookup(&kvm
->arch
.vsie
.addr_to_page
, addr
>> 9);
943 if (page_ref_inc_return(page
) == 2)
944 return page_to_virt(page
);
949 * We want at least #online_vcpus shadows, so every VCPU can execute
950 * the VSIE in parallel.
952 nr_vcpus
= atomic_read(&kvm
->online_vcpus
);
954 mutex_lock(&kvm
->arch
.vsie
.mutex
);
955 if (kvm
->arch
.vsie
.page_count
< nr_vcpus
) {
956 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
| GFP_DMA
);
958 mutex_unlock(&kvm
->arch
.vsie
.mutex
);
959 return ERR_PTR(-ENOMEM
);
962 kvm
->arch
.vsie
.pages
[kvm
->arch
.vsie
.page_count
] = page
;
963 kvm
->arch
.vsie
.page_count
++;
965 /* reuse an existing entry that belongs to nobody */
967 page
= kvm
->arch
.vsie
.pages
[kvm
->arch
.vsie
.next
];
968 if (page_ref_inc_return(page
) == 2)
971 kvm
->arch
.vsie
.next
++;
972 kvm
->arch
.vsie
.next
%= nr_vcpus
;
974 radix_tree_delete(&kvm
->arch
.vsie
.addr_to_page
, page
->index
>> 9);
977 /* double use of the same address */
978 if (radix_tree_insert(&kvm
->arch
.vsie
.addr_to_page
, addr
>> 9, page
)) {
980 mutex_unlock(&kvm
->arch
.vsie
.mutex
);
983 mutex_unlock(&kvm
->arch
.vsie
.mutex
);
985 vsie_page
= page_to_virt(page
);
986 memset(&vsie_page
->scb_s
, 0, sizeof(struct kvm_s390_sie_block
));
987 release_gmap_shadow(vsie_page
);
988 vsie_page
->fault_addr
= 0;
989 vsie_page
->scb_s
.ihcpu
= 0xffffU
;
993 /* put a vsie page acquired via get_vsie_page */
994 static void put_vsie_page(struct kvm
*kvm
, struct vsie_page
*vsie_page
)
996 struct page
*page
= pfn_to_page(__pa(vsie_page
) >> PAGE_SHIFT
);
1001 int kvm_s390_handle_vsie(struct kvm_vcpu
*vcpu
)
1003 struct vsie_page
*vsie_page
;
1004 unsigned long scb_addr
;
1007 vcpu
->stat
.instruction_sie
++;
1008 if (!test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_SIEF2
))
1010 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1011 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1013 BUILD_BUG_ON(sizeof(struct vsie_page
) != 4096);
1014 scb_addr
= kvm_s390_get_base_disp_s(vcpu
, NULL
);
1016 /* 512 byte alignment */
1017 if (unlikely(scb_addr
& 0x1ffUL
))
1018 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
1020 if (signal_pending(current
) || kvm_s390_vcpu_has_irq(vcpu
, 0))
1023 vsie_page
= get_vsie_page(vcpu
->kvm
, scb_addr
);
1024 if (IS_ERR(vsie_page
))
1025 return PTR_ERR(vsie_page
);
1026 else if (!vsie_page
)
1027 /* double use of sie control block - simply do nothing */
1030 rc
= pin_scb(vcpu
, vsie_page
, scb_addr
);
1033 rc
= shadow_scb(vcpu
, vsie_page
);
1036 rc
= pin_blocks(vcpu
, vsie_page
);
1039 register_shadow_scb(vcpu
, vsie_page
);
1040 rc
= vsie_run(vcpu
, vsie_page
);
1041 unregister_shadow_scb(vcpu
);
1042 unpin_blocks(vcpu
, vsie_page
);
1044 unshadow_scb(vcpu
, vsie_page
);
1046 unpin_scb(vcpu
, vsie_page
, scb_addr
);
1048 put_vsie_page(vcpu
->kvm
, vsie_page
);
1050 return rc
< 0 ? rc
: 0;
1053 /* Init the vsie data structures. To be called when a vm is initialized. */
1054 void kvm_s390_vsie_init(struct kvm
*kvm
)
1056 mutex_init(&kvm
->arch
.vsie
.mutex
);
1057 INIT_RADIX_TREE(&kvm
->arch
.vsie
.addr_to_page
, GFP_KERNEL
);
1060 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
1061 void kvm_s390_vsie_destroy(struct kvm
*kvm
)
1063 struct vsie_page
*vsie_page
;
1067 mutex_lock(&kvm
->arch
.vsie
.mutex
);
1068 for (i
= 0; i
< kvm
->arch
.vsie
.page_count
; i
++) {
1069 page
= kvm
->arch
.vsie
.pages
[i
];
1070 kvm
->arch
.vsie
.pages
[i
] = NULL
;
1071 vsie_page
= page_to_virt(page
);
1072 release_gmap_shadow(vsie_page
);
1073 /* free the radix tree entry */
1074 radix_tree_delete(&kvm
->arch
.vsie
.addr_to_page
, page
->index
>> 9);
1077 kvm
->arch
.vsie
.page_count
= 0;
1078 mutex_unlock(&kvm
->arch
.vsie
.mutex
);
1081 void kvm_s390_vsie_kick(struct kvm_vcpu
*vcpu
)
1083 struct kvm_s390_sie_block
*scb
= READ_ONCE(vcpu
->arch
.vsie_block
);
1086 * Even if the VCPU lets go of the shadow sie block reference, it is
1087 * still valid in the cache. So we can safely kick it.
1090 atomic_or(PROG_BLOCK_SIE
, &scb
->prog20
);
1091 if (scb
->prog0c
& PROG_IN_SIE
)
1092 atomic_or(CPUSTAT_STOP_INT
, &scb
->cpuflags
);