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 __u8 reserved
[0x0700 - 0x0210]; /* 0x0210 */
32 struct kvm_s390_crypto_cb crycb
; /* 0x0700 */
33 __u8 fac
[S390_ARCH_FAC_LIST_SIZE_BYTE
]; /* 0x0800 */
36 /* trigger a validity icpt for the given scb */
37 static int set_validity_icpt(struct kvm_s390_sie_block
*scb
,
41 scb
->ipb
= ((__u32
) reason_code
) << 16;
42 scb
->icptcode
= ICPT_VALIDITY
;
46 /* mark the prefix as unmapped, this will block the VSIE */
47 static void prefix_unmapped(struct vsie_page
*vsie_page
)
49 atomic_or(PROG_REQUEST
, &vsie_page
->scb_s
.prog20
);
52 /* mark the prefix as unmapped and wait until the VSIE has been left */
53 static void prefix_unmapped_sync(struct vsie_page
*vsie_page
)
55 prefix_unmapped(vsie_page
);
56 if (vsie_page
->scb_s
.prog0c
& PROG_IN_SIE
)
57 atomic_or(CPUSTAT_STOP_INT
, &vsie_page
->scb_s
.cpuflags
);
58 while (vsie_page
->scb_s
.prog0c
& PROG_IN_SIE
)
62 /* mark the prefix as mapped, this will allow the VSIE to run */
63 static void prefix_mapped(struct vsie_page
*vsie_page
)
65 atomic_andnot(PROG_REQUEST
, &vsie_page
->scb_s
.prog20
);
68 /* test if the prefix is mapped into the gmap shadow */
69 static int prefix_is_mapped(struct vsie_page
*vsie_page
)
71 return !(atomic_read(&vsie_page
->scb_s
.prog20
) & PROG_REQUEST
);
74 /* copy the updated intervention request bits into the shadow scb */
75 static void update_intervention_requests(struct vsie_page
*vsie_page
)
77 const int bits
= CPUSTAT_STOP_INT
| CPUSTAT_IO_INT
| CPUSTAT_EXT_INT
;
80 cpuflags
= atomic_read(&vsie_page
->scb_o
->cpuflags
);
81 atomic_andnot(bits
, &vsie_page
->scb_s
.cpuflags
);
82 atomic_or(cpuflags
& bits
, &vsie_page
->scb_s
.cpuflags
);
85 /* shadow (filter and validate) the cpuflags */
86 static int prepare_cpuflags(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
88 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
89 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
90 int newflags
, cpuflags
= atomic_read(&scb_o
->cpuflags
);
92 /* we don't allow ESA/390 guests */
93 if (!(cpuflags
& CPUSTAT_ZARCH
))
94 return set_validity_icpt(scb_s
, 0x0001U
);
96 if (cpuflags
& (CPUSTAT_RRF
| CPUSTAT_MCDS
))
97 return set_validity_icpt(scb_s
, 0x0001U
);
98 else if (cpuflags
& (CPUSTAT_SLSV
| CPUSTAT_SLSR
))
99 return set_validity_icpt(scb_s
, 0x0007U
);
101 /* intervention requests will be set later */
102 newflags
= CPUSTAT_ZARCH
;
103 if (cpuflags
& CPUSTAT_GED
&& test_kvm_facility(vcpu
->kvm
, 8))
104 newflags
|= CPUSTAT_GED
;
105 if (cpuflags
& CPUSTAT_GED2
&& test_kvm_facility(vcpu
->kvm
, 78)) {
106 if (cpuflags
& CPUSTAT_GED
)
107 return set_validity_icpt(scb_s
, 0x0001U
);
108 newflags
|= CPUSTAT_GED2
;
110 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_GPERE
))
111 newflags
|= cpuflags
& CPUSTAT_P
;
112 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_GSLS
))
113 newflags
|= cpuflags
& CPUSTAT_SM
;
115 atomic_set(&scb_s
->cpuflags
, newflags
);
120 * Create a shadow copy of the crycb block and setup key wrapping, if
121 * requested for guest 3 and enabled for guest 2.
123 * We only accept format-1 (no AP in g2), but convert it into format-2
124 * There is nothing to do for format-0.
126 * Returns: - 0 if shadowed or nothing to do
127 * - > 0 if control has to be given to guest 2
129 static int shadow_crycb(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
131 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
132 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
133 u32 crycb_addr
= scb_o
->crycbd
& 0x7ffffff8U
;
134 unsigned long *b1
, *b2
;
138 if (!(scb_o
->crycbd
& vcpu
->arch
.sie_block
->crycbd
& CRYCB_FORMAT1
))
140 /* format-1 is supported with message-security-assist extension 3 */
141 if (!test_kvm_facility(vcpu
->kvm
, 76))
143 /* we may only allow it if enabled for guest 2 */
144 ecb3_flags
= scb_o
->ecb3
& vcpu
->arch
.sie_block
->ecb3
&
145 (ECB3_AES
| ECB3_DEA
);
149 if ((crycb_addr
& PAGE_MASK
) != ((crycb_addr
+ 128) & PAGE_MASK
))
150 return set_validity_icpt(scb_s
, 0x003CU
);
151 else if (!crycb_addr
)
152 return set_validity_icpt(scb_s
, 0x0039U
);
154 /* copy only the wrapping keys */
155 if (read_guest_real(vcpu
, crycb_addr
+ 72, &vsie_page
->crycb
, 56))
156 return set_validity_icpt(scb_s
, 0x0035U
);
158 scb_s
->ecb3
|= ecb3_flags
;
159 scb_s
->crycbd
= ((__u32
)(__u64
) &vsie_page
->crycb
) | CRYCB_FORMAT1
|
162 /* xor both blocks in one run */
163 b1
= (unsigned long *) vsie_page
->crycb
.dea_wrapping_key_mask
;
164 b2
= (unsigned long *)
165 vcpu
->kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
;
166 /* as 56%8 == 0, bitmap_xor won't overwrite any data */
167 bitmap_xor(b1
, b1
, b2
, BITS_PER_BYTE
* 56);
171 /* shadow (round up/down) the ibc to avoid validity icpt */
172 static void prepare_ibc(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
174 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
175 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
176 __u64 min_ibc
= (sclp
.ibc
>> 16) & 0x0fffU
;
179 /* ibc installed in g2 and requested for g3 */
180 if (vcpu
->kvm
->arch
.model
.ibc
&& (scb_o
->ibc
& 0x0fffU
)) {
181 scb_s
->ibc
= scb_o
->ibc
& 0x0fffU
;
182 /* takte care of the minimum ibc level of the machine */
183 if (scb_s
->ibc
< min_ibc
)
184 scb_s
->ibc
= min_ibc
;
185 /* take care of the maximum ibc level set for the guest */
186 if (scb_s
->ibc
> vcpu
->kvm
->arch
.model
.ibc
)
187 scb_s
->ibc
= vcpu
->kvm
->arch
.model
.ibc
;
191 /* unshadow the scb, copying parameters back to the real scb */
192 static void unshadow_scb(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
194 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
195 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
198 scb_o
->icptcode
= scb_s
->icptcode
;
199 scb_o
->icptstatus
= scb_s
->icptstatus
;
200 scb_o
->ipa
= scb_s
->ipa
;
201 scb_o
->ipb
= scb_s
->ipb
;
202 scb_o
->gbea
= scb_s
->gbea
;
205 scb_o
->cputm
= scb_s
->cputm
;
206 scb_o
->ckc
= scb_s
->ckc
;
207 scb_o
->todpr
= scb_s
->todpr
;
210 scb_o
->gpsw
= scb_s
->gpsw
;
211 scb_o
->gg14
= scb_s
->gg14
;
212 scb_o
->gg15
= scb_s
->gg15
;
213 memcpy(scb_o
->gcr
, scb_s
->gcr
, 128);
214 scb_o
->pp
= scb_s
->pp
;
216 /* interrupt intercept */
217 switch (scb_s
->icptcode
) {
221 memcpy((void *)((u64
)scb_o
+ 0xc0),
222 (void *)((u64
)scb_s
+ 0xc0), 0xf0 - 0xc0);
226 memcpy((void *)((u64
)scb_o
+ 0xc0),
227 (void *)((u64
)scb_s
+ 0xc0), 0xd0 - 0xc0);
231 if (scb_s
->ihcpu
!= 0xffffU
)
232 scb_o
->ihcpu
= scb_s
->ihcpu
;
236 * Setup the shadow scb by copying and checking the relevant parts of the g2
239 * Returns: - 0 if the scb has been shadowed
240 * - > 0 if control has to be given to guest 2
242 static int shadow_scb(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
244 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
245 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
246 bool had_tx
= scb_s
->ecb
& 0x10U
;
247 unsigned long new_mso
= 0;
250 /* make sure we don't have any leftovers when reusing the scb */
259 rc
= prepare_cpuflags(vcpu
, vsie_page
);
264 scb_s
->cputm
= scb_o
->cputm
;
265 scb_s
->ckc
= scb_o
->ckc
;
266 scb_s
->todpr
= scb_o
->todpr
;
267 scb_s
->epoch
= scb_o
->epoch
;
270 scb_s
->gpsw
= scb_o
->gpsw
;
271 scb_s
->gg14
= scb_o
->gg14
;
272 scb_s
->gg15
= scb_o
->gg15
;
273 memcpy(scb_s
->gcr
, scb_o
->gcr
, 128);
274 scb_s
->pp
= scb_o
->pp
;
276 /* interception / execution handling */
277 scb_s
->gbea
= scb_o
->gbea
;
278 scb_s
->lctl
= scb_o
->lctl
;
279 scb_s
->svcc
= scb_o
->svcc
;
280 scb_s
->ictl
= scb_o
->ictl
;
282 * SKEY handling functions can't deal with false setting of PTE invalid
283 * bits. Therefore we cannot provide interpretation and would later
284 * have to provide own emulation handlers.
286 scb_s
->ictl
|= ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
;
287 scb_s
->icpua
= scb_o
->icpua
;
289 if (!(atomic_read(&scb_s
->cpuflags
) & CPUSTAT_SM
))
290 new_mso
= scb_o
->mso
& 0xfffffffffff00000UL
;
291 /* if the hva of the prefix changes, we have to remap the prefix */
292 if (scb_s
->mso
!= new_mso
|| scb_s
->prefix
!= scb_o
->prefix
)
293 prefix_unmapped(vsie_page
);
294 /* SIE will do mso/msl validity and exception checks for us */
295 scb_s
->msl
= scb_o
->msl
& 0xfffffffffff00000UL
;
296 scb_s
->mso
= new_mso
;
297 scb_s
->prefix
= scb_o
->prefix
;
299 /* We have to definetly flush the tlb if this scb never ran */
300 if (scb_s
->ihcpu
!= 0xffffU
)
301 scb_s
->ihcpu
= scb_o
->ihcpu
;
303 /* MVPG and Protection Exception Interpretation are always available */
304 scb_s
->eca
|= scb_o
->eca
& 0x01002000U
;
305 /* Host-protection-interruption introduced with ESOP */
306 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_ESOP
))
307 scb_s
->ecb
|= scb_o
->ecb
& 0x02U
;
308 /* transactional execution */
309 if (test_kvm_facility(vcpu
->kvm
, 73)) {
310 /* remap the prefix is tx is toggled on */
311 if ((scb_o
->ecb
& 0x10U
) && !had_tx
)
312 prefix_unmapped(vsie_page
);
313 scb_s
->ecb
|= scb_o
->ecb
& 0x10U
;
316 if (test_kvm_facility(vcpu
->kvm
, 129)) {
317 scb_s
->eca
|= scb_o
->eca
& 0x00020000U
;
318 scb_s
->ecd
|= scb_o
->ecd
& 0x20000000U
;
320 /* Run-time-Instrumentation */
321 if (test_kvm_facility(vcpu
->kvm
, 64))
322 scb_s
->ecb3
|= scb_o
->ecb3
& 0x01U
;
323 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_SIIF
))
324 scb_s
->eca
|= scb_o
->eca
& 0x00000001U
;
326 prepare_ibc(vcpu
, vsie_page
);
327 rc
= shadow_crycb(vcpu
, vsie_page
);
330 unshadow_scb(vcpu
, vsie_page
);
334 void kvm_s390_vsie_gmap_notifier(struct gmap
*gmap
, unsigned long start
,
337 struct kvm
*kvm
= gmap
->private;
338 struct vsie_page
*cur
;
339 unsigned long prefix
;
343 if (!gmap_is_shadow(gmap
))
345 if (start
>= 1UL << 31)
346 /* We are only interested in prefix pages */
350 * Only new shadow blocks are added to the list during runtime,
351 * therefore we can safely reference them all the time.
353 for (i
= 0; i
< kvm
->arch
.vsie
.page_count
; i
++) {
354 page
= READ_ONCE(kvm
->arch
.vsie
.pages
[i
]);
357 cur
= page_to_virt(page
);
358 if (READ_ONCE(cur
->gmap
) != gmap
)
360 prefix
= cur
->scb_s
.prefix
<< GUEST_PREFIX_SHIFT
;
361 /* with mso/msl, the prefix lies at an offset */
362 prefix
+= cur
->scb_s
.mso
;
363 if (prefix
<= end
&& start
<= prefix
+ 2 * PAGE_SIZE
- 1)
364 prefix_unmapped_sync(cur
);
369 * Map the first prefix page and if tx is enabled also the second prefix page.
371 * The prefix will be protected, a gmap notifier will inform about unmaps.
372 * The shadow scb must not be executed until the prefix is remapped, this is
373 * guaranteed by properly handling PROG_REQUEST.
375 * Returns: - 0 on if successfully mapped or already mapped
376 * - > 0 if control has to be given to guest 2
377 * - -EAGAIN if the caller can retry immediately
378 * - -ENOMEM if out of memory
380 static int map_prefix(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
382 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
383 u64 prefix
= scb_s
->prefix
<< GUEST_PREFIX_SHIFT
;
386 if (prefix_is_mapped(vsie_page
))
389 /* mark it as mapped so we can catch any concurrent unmappers */
390 prefix_mapped(vsie_page
);
392 /* with mso/msl, the prefix lies at offset *mso* */
393 prefix
+= scb_s
->mso
;
395 rc
= kvm_s390_shadow_fault(vcpu
, vsie_page
->gmap
, prefix
);
396 if (!rc
&& (scb_s
->ecb
& 0x10U
))
397 rc
= kvm_s390_shadow_fault(vcpu
, vsie_page
->gmap
,
400 * We don't have to mprotect, we will be called for all unshadows.
401 * SIE will detect if protection applies and trigger a validity.
404 prefix_unmapped(vsie_page
);
405 if (rc
> 0 || rc
== -EFAULT
)
406 rc
= set_validity_icpt(scb_s
, 0x0037U
);
411 * Pin the guest page given by gpa and set hpa to the pinned host address.
412 * Will always be pinned writable.
414 * Returns: - 0 on success
415 * - -EINVAL if the gpa is not valid guest storage
416 * - -ENOMEM if out of memory
418 static int pin_guest_page(struct kvm
*kvm
, gpa_t gpa
, hpa_t
*hpa
)
424 hva
= gfn_to_hva(kvm
, gpa_to_gfn(gpa
));
425 if (kvm_is_error_hva(hva
))
427 rc
= get_user_pages_fast(hva
, 1, 1, &page
);
432 *hpa
= (hpa_t
) page_to_virt(page
) + (gpa
& ~PAGE_MASK
);
436 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
437 static void unpin_guest_page(struct kvm
*kvm
, gpa_t gpa
, hpa_t hpa
)
441 page
= virt_to_page(hpa
);
442 set_page_dirty_lock(page
);
444 /* mark the page always as dirty for migration */
445 mark_page_dirty(kvm
, gpa_to_gfn(gpa
));
448 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
449 static void unpin_blocks(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
451 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
452 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
456 hpa
= (u64
) scb_s
->scaoh
<< 32 | scb_s
->scaol
;
458 gpa
= scb_o
->scaol
& ~0xfUL
;
459 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_64BSCAO
))
460 gpa
|= (u64
) scb_o
->scaoh
<< 32;
461 unpin_guest_page(vcpu
->kvm
, gpa
, hpa
);
468 gpa
= scb_o
->itdba
& ~0xffUL
;
469 unpin_guest_page(vcpu
->kvm
, gpa
, hpa
);
475 gpa
= scb_o
->gvrd
& ~0x1ffUL
;
476 unpin_guest_page(vcpu
->kvm
, gpa
, hpa
);
482 gpa
= scb_o
->riccbd
& ~0x3fUL
;
483 unpin_guest_page(vcpu
->kvm
, gpa
, hpa
);
489 * Instead of shadowing some blocks, we can simply forward them because the
490 * addresses in the scb are 64 bit long.
492 * This works as long as the data lies in one page. If blocks ever exceed one
493 * page, we have to fall back to shadowing.
495 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
496 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
498 * Returns: - 0 if all blocks were pinned.
499 * - > 0 if control has to be given to guest 2
500 * - -ENOMEM if out of memory
502 static int pin_blocks(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
504 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
505 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
510 gpa
= scb_o
->scaol
& ~0xfUL
;
511 if (test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_64BSCAO
))
512 gpa
|= (u64
) scb_o
->scaoh
<< 32;
514 if (!(gpa
& ~0x1fffUL
))
515 rc
= set_validity_icpt(scb_s
, 0x0038U
);
516 else if ((gpa
& ~0x1fffUL
) == kvm_s390_get_prefix(vcpu
))
517 rc
= set_validity_icpt(scb_s
, 0x0011U
);
518 else if ((gpa
& PAGE_MASK
) !=
519 ((gpa
+ sizeof(struct bsca_block
) - 1) & PAGE_MASK
))
520 rc
= set_validity_icpt(scb_s
, 0x003bU
);
522 rc
= pin_guest_page(vcpu
->kvm
, gpa
, &hpa
);
524 rc
= set_validity_icpt(scb_s
, 0x0034U
);
528 scb_s
->scaoh
= (u32
)((u64
)hpa
>> 32);
529 scb_s
->scaol
= (u32
)(u64
)hpa
;
532 gpa
= scb_o
->itdba
& ~0xffUL
;
533 if (gpa
&& (scb_s
->ecb
& 0x10U
)) {
534 if (!(gpa
& ~0x1fffU
)) {
535 rc
= set_validity_icpt(scb_s
, 0x0080U
);
538 /* 256 bytes cannot cross page boundaries */
539 rc
= pin_guest_page(vcpu
->kvm
, gpa
, &hpa
);
541 rc
= set_validity_icpt(scb_s
, 0x0080U
);
547 gpa
= scb_o
->gvrd
& ~0x1ffUL
;
548 if (gpa
&& (scb_s
->eca
& 0x00020000U
) &&
549 !(scb_s
->ecd
& 0x20000000U
)) {
550 if (!(gpa
& ~0x1fffUL
)) {
551 rc
= set_validity_icpt(scb_s
, 0x1310U
);
555 * 512 bytes vector registers cannot cross page boundaries
556 * if this block gets bigger, we have to shadow it.
558 rc
= pin_guest_page(vcpu
->kvm
, gpa
, &hpa
);
560 rc
= set_validity_icpt(scb_s
, 0x1310U
);
566 gpa
= scb_o
->riccbd
& ~0x3fUL
;
567 if (gpa
&& (scb_s
->ecb3
& 0x01U
)) {
568 if (!(gpa
& ~0x1fffUL
)) {
569 rc
= set_validity_icpt(scb_s
, 0x0043U
);
572 /* 64 bytes cannot cross page boundaries */
573 rc
= pin_guest_page(vcpu
->kvm
, gpa
, &hpa
);
575 rc
= set_validity_icpt(scb_s
, 0x0043U
);
576 /* Validity 0x0044 will be checked by SIE */
583 unpin_blocks(vcpu
, vsie_page
);
587 /* unpin the scb provided by guest 2, marking it as dirty */
588 static void unpin_scb(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
,
591 hpa_t hpa
= (hpa_t
) vsie_page
->scb_o
;
594 unpin_guest_page(vcpu
->kvm
, gpa
, hpa
);
595 vsie_page
->scb_o
= NULL
;
599 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
601 * Returns: - 0 if the scb was pinned.
602 * - > 0 if control has to be given to guest 2
603 * - -ENOMEM if out of memory
605 static int pin_scb(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
,
611 rc
= pin_guest_page(vcpu
->kvm
, gpa
, &hpa
);
613 rc
= kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
618 vsie_page
->scb_o
= (struct kvm_s390_sie_block
*) hpa
;
623 * Inject a fault into guest 2.
625 * Returns: - > 0 if control has to be given to guest 2
626 * < 0 if an error occurred during injection.
628 static int inject_fault(struct kvm_vcpu
*vcpu
, __u16 code
, __u64 vaddr
,
631 struct kvm_s390_pgm_info pgm
= {
634 /* 0-51: virtual address */
635 (vaddr
& 0xfffffffffffff000UL
) |
636 /* 52-53: store / fetch */
637 (((unsigned int) !write_flag
) + 1) << 10,
638 /* 62-63: asce id (alway primary == 0) */
639 .exc_access_id
= 0, /* always primary */
640 .op_access_id
= 0, /* not MVPG */
644 if (code
== PGM_PROTECTION
)
645 pgm
.trans_exc_code
|= 0x4UL
;
647 rc
= kvm_s390_inject_prog_irq(vcpu
, &pgm
);
652 * Handle a fault during vsie execution on a gmap shadow.
654 * Returns: - 0 if the fault was resolved
655 * - > 0 if control has to be given to guest 2
656 * - < 0 if an error occurred
658 static int handle_fault(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
662 if (current
->thread
.gmap_int_code
== PGM_PROTECTION
)
663 /* we can directly forward all protection exceptions */
664 return inject_fault(vcpu
, PGM_PROTECTION
,
665 current
->thread
.gmap_addr
, 1);
667 rc
= kvm_s390_shadow_fault(vcpu
, vsie_page
->gmap
,
668 current
->thread
.gmap_addr
);
670 rc
= inject_fault(vcpu
, rc
,
671 current
->thread
.gmap_addr
,
672 current
->thread
.gmap_write_flag
);
677 static inline void clear_vsie_icpt(struct vsie_page
*vsie_page
)
679 vsie_page
->scb_s
.icptcode
= 0;
682 /* rewind the psw and clear the vsie icpt, so we can retry execution */
683 static void retry_vsie_icpt(struct vsie_page
*vsie_page
)
685 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
686 int ilen
= insn_length(scb_s
->ipa
>> 8);
688 /* take care of EXECUTE instructions */
689 if (scb_s
->icptstatus
& 1) {
690 ilen
= (scb_s
->icptstatus
>> 4) & 0x6;
694 scb_s
->gpsw
.addr
= __rewind_psw(scb_s
->gpsw
, ilen
);
695 clear_vsie_icpt(vsie_page
);
699 * Try to shadow + enable the guest 2 provided facility list.
700 * Retry instruction execution if enabled for and provided by guest 2.
702 * Returns: - 0 if handled (retry or guest 2 icpt)
703 * - > 0 if control has to be given to guest 2
705 static int handle_stfle(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
707 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
708 __u32 fac
= vsie_page
->scb_o
->fac
& 0x7ffffff8U
;
710 if (fac
&& test_kvm_facility(vcpu
->kvm
, 7)) {
711 retry_vsie_icpt(vsie_page
);
712 if (read_guest_real(vcpu
, fac
, &vsie_page
->fac
,
713 sizeof(vsie_page
->fac
)))
714 return set_validity_icpt(scb_s
, 0x1090U
);
715 scb_s
->fac
= (__u32
)(__u64
) &vsie_page
->fac
;
721 * Run the vsie on a shadow scb and a shadow gmap, without any further
722 * sanity checks, handling SIE faults.
724 * Returns: - 0 everything went fine
725 * - > 0 if control has to be given to guest 2
726 * - < 0 if an error occurred
728 static int do_vsie_run(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
730 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
731 struct kvm_s390_sie_block
*scb_o
= vsie_page
->scb_o
;
736 if (test_cpu_flag(CIF_MCCK_PENDING
))
739 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
744 rc
= sie64a(scb_s
, vcpu
->run
->s
.regs
.gprs
);
749 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
752 rc
= 0; /* we could still have an icpt */
753 else if (rc
== -EFAULT
)
754 return handle_fault(vcpu
, vsie_page
);
756 switch (scb_s
->icptcode
) {
758 if (scb_s
->ipa
== 0xb2b0)
759 rc
= handle_stfle(vcpu
, vsie_page
);
762 /* stop not requested by g2 - must have been a kick */
763 if (!(atomic_read(&scb_o
->cpuflags
) & CPUSTAT_STOP_INT
))
764 clear_vsie_icpt(vsie_page
);
767 if ((scb_s
->ipa
& 0xf000) != 0xf000)
768 scb_s
->ipa
+= 0x1000;
774 static void release_gmap_shadow(struct vsie_page
*vsie_page
)
777 gmap_put(vsie_page
->gmap
);
778 WRITE_ONCE(vsie_page
->gmap
, NULL
);
779 prefix_unmapped(vsie_page
);
782 static int acquire_gmap_shadow(struct kvm_vcpu
*vcpu
,
783 struct vsie_page
*vsie_page
)
790 asce
= vcpu
->arch
.sie_block
->gcr
[1];
791 cr0
.val
= vcpu
->arch
.sie_block
->gcr
[0];
792 edat
= cr0
.edat
&& test_kvm_facility(vcpu
->kvm
, 8);
793 edat
+= edat
&& test_kvm_facility(vcpu
->kvm
, 78);
796 * ASCE or EDAT could have changed since last icpt, or the gmap
797 * we're holding has been unshadowed. If the gmap is still valid,
798 * we can safely reuse it.
800 if (vsie_page
->gmap
&& gmap_shadow_valid(vsie_page
->gmap
, asce
, edat
))
803 /* release the old shadow - if any, and mark the prefix as unmapped */
804 release_gmap_shadow(vsie_page
);
805 gmap
= gmap_shadow(vcpu
->arch
.gmap
, asce
, edat
);
807 return PTR_ERR(gmap
);
808 gmap
->private = vcpu
->kvm
;
809 WRITE_ONCE(vsie_page
->gmap
, gmap
);
814 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
815 * prefix pages and faults.
817 * Returns: - 0 if no errors occurred
818 * - > 0 if control has to be given to guest 2
819 * - -ENOMEM if out of memory
821 static int vsie_run(struct kvm_vcpu
*vcpu
, struct vsie_page
*vsie_page
)
823 struct kvm_s390_sie_block
*scb_s
= &vsie_page
->scb_s
;
827 rc
= acquire_gmap_shadow(vcpu
, vsie_page
);
829 rc
= map_prefix(vcpu
, vsie_page
);
831 gmap_enable(vsie_page
->gmap
);
832 update_intervention_requests(vsie_page
);
833 rc
= do_vsie_run(vcpu
, vsie_page
);
834 gmap_enable(vcpu
->arch
.gmap
);
839 if (rc
|| scb_s
->icptcode
|| signal_pending(current
) ||
840 kvm_s390_vcpu_has_irq(vcpu
, 0))
846 * Addressing exceptions are always presentes as intercepts.
847 * As addressing exceptions are suppressing and our guest 3 PSW
848 * points at the responsible instruction, we have to
849 * forward the PSW and set the ilc. If we can't read guest 3
850 * instruction, we can use an arbitrary ilc. Let's always use
851 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
852 * memory. (we could also fake the shadow so the hardware
855 scb_s
->icptcode
= ICPT_PROGI
;
856 scb_s
->iprcc
= PGM_ADDRESSING
;
858 scb_s
->gpsw
.addr
= __rewind_psw(scb_s
->gpsw
, 4);
864 * Get or create a vsie page for a scb address.
866 * Returns: - address of a vsie page (cached or new one)
867 * - NULL if the same scb address is already used by another VCPU
868 * - ERR_PTR(-ENOMEM) if out of memory
870 static struct vsie_page
*get_vsie_page(struct kvm
*kvm
, unsigned long addr
)
872 struct vsie_page
*vsie_page
;
877 page
= radix_tree_lookup(&kvm
->arch
.vsie
.addr_to_page
, addr
>> 9);
880 if (page_ref_inc_return(page
) == 2)
881 return page_to_virt(page
);
886 * We want at least #online_vcpus shadows, so every VCPU can execute
887 * the VSIE in parallel.
889 nr_vcpus
= atomic_read(&kvm
->online_vcpus
);
891 mutex_lock(&kvm
->arch
.vsie
.mutex
);
892 if (kvm
->arch
.vsie
.page_count
< nr_vcpus
) {
893 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
| GFP_DMA
);
895 mutex_unlock(&kvm
->arch
.vsie
.mutex
);
896 return ERR_PTR(-ENOMEM
);
899 kvm
->arch
.vsie
.pages
[kvm
->arch
.vsie
.page_count
] = page
;
900 kvm
->arch
.vsie
.page_count
++;
902 /* reuse an existing entry that belongs to nobody */
904 page
= kvm
->arch
.vsie
.pages
[kvm
->arch
.vsie
.next
];
905 if (page_ref_inc_return(page
) == 2)
908 kvm
->arch
.vsie
.next
++;
909 kvm
->arch
.vsie
.next
%= nr_vcpus
;
911 radix_tree_delete(&kvm
->arch
.vsie
.addr_to_page
, page
->index
>> 9);
914 /* double use of the same address */
915 if (radix_tree_insert(&kvm
->arch
.vsie
.addr_to_page
, addr
>> 9, page
)) {
917 mutex_unlock(&kvm
->arch
.vsie
.mutex
);
920 mutex_unlock(&kvm
->arch
.vsie
.mutex
);
922 vsie_page
= page_to_virt(page
);
923 memset(&vsie_page
->scb_s
, 0, sizeof(struct kvm_s390_sie_block
));
924 release_gmap_shadow(vsie_page
);
925 vsie_page
->scb_s
.ihcpu
= 0xffffU
;
929 /* put a vsie page acquired via get_vsie_page */
930 static void put_vsie_page(struct kvm
*kvm
, struct vsie_page
*vsie_page
)
932 struct page
*page
= pfn_to_page(__pa(vsie_page
) >> PAGE_SHIFT
);
937 int kvm_s390_handle_vsie(struct kvm_vcpu
*vcpu
)
939 struct vsie_page
*vsie_page
;
940 unsigned long scb_addr
;
943 vcpu
->stat
.instruction_sie
++;
944 if (!test_kvm_cpu_feat(vcpu
->kvm
, KVM_S390_VM_CPU_FEAT_SIEF2
))
946 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
947 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
949 BUILD_BUG_ON(sizeof(struct vsie_page
) != 4096);
950 scb_addr
= kvm_s390_get_base_disp_s(vcpu
, NULL
);
952 /* 512 byte alignment */
953 if (unlikely(scb_addr
& 0x1ffUL
))
954 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
956 if (signal_pending(current
) || kvm_s390_vcpu_has_irq(vcpu
, 0))
959 vsie_page
= get_vsie_page(vcpu
->kvm
, scb_addr
);
960 if (IS_ERR(vsie_page
))
961 return PTR_ERR(vsie_page
);
963 /* double use of sie control block - simply do nothing */
966 rc
= pin_scb(vcpu
, vsie_page
, scb_addr
);
969 rc
= shadow_scb(vcpu
, vsie_page
);
972 rc
= pin_blocks(vcpu
, vsie_page
);
975 rc
= vsie_run(vcpu
, vsie_page
);
976 unpin_blocks(vcpu
, vsie_page
);
978 unshadow_scb(vcpu
, vsie_page
);
980 unpin_scb(vcpu
, vsie_page
, scb_addr
);
982 put_vsie_page(vcpu
->kvm
, vsie_page
);
984 return rc
< 0 ? rc
: 0;
987 /* Init the vsie data structures. To be called when a vm is initialized. */
988 void kvm_s390_vsie_init(struct kvm
*kvm
)
990 mutex_init(&kvm
->arch
.vsie
.mutex
);
991 INIT_RADIX_TREE(&kvm
->arch
.vsie
.addr_to_page
, GFP_KERNEL
);
994 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
995 void kvm_s390_vsie_destroy(struct kvm
*kvm
)
997 struct vsie_page
*vsie_page
;
1001 mutex_lock(&kvm
->arch
.vsie
.mutex
);
1002 for (i
= 0; i
< kvm
->arch
.vsie
.page_count
; i
++) {
1003 page
= kvm
->arch
.vsie
.pages
[i
];
1004 kvm
->arch
.vsie
.pages
[i
] = NULL
;
1005 vsie_page
= page_to_virt(page
);
1006 release_gmap_shadow(vsie_page
);
1007 /* free the radix tree entry */
1008 radix_tree_delete(&kvm
->arch
.vsie
.addr_to_page
, page
->index
>> 9);
1011 kvm
->arch
.vsie
.page_count
= 0;
1012 mutex_unlock(&kvm
->arch
.vsie
.mutex
);