2 * QEMU S390x KVM implementation
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp. 2012
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include <sys/ioctl.h>
24 #include <linux/kvm.h>
25 #include <asm/ptrace.h>
28 #include "s390x-internal.h"
29 #include "kvm_s390x.h"
30 #include "sysemu/kvm_int.h"
31 #include "qemu/cutils.h"
32 #include "qapi/error.h"
33 #include "qemu/error-report.h"
34 #include "qemu/timer.h"
35 #include "qemu/units.h"
36 #include "qemu/main-loop.h"
37 #include "qemu/mmap-alloc.h"
39 #include "sysemu/sysemu.h"
40 #include "sysemu/hw_accel.h"
41 #include "sysemu/runstate.h"
42 #include "sysemu/device_tree.h"
43 #include "exec/gdbstub.h"
44 #include "exec/ram_addr.h"
46 #include "hw/s390x/s390-pci-inst.h"
47 #include "hw/s390x/s390-pci-bus.h"
48 #include "hw/s390x/ipl.h"
49 #include "hw/s390x/ebcdic.h"
50 #include "exec/memattrs.h"
51 #include "hw/s390x/s390-virtio-ccw.h"
52 #include "hw/s390x/s390-virtio-hcall.h"
53 #include "target/s390x/kvm/pv.h"
55 #define kvm_vm_check_mem_attr(s, attr) \
56 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
58 #define IPA0_DIAG 0x8300
59 #define IPA0_SIGP 0xae00
60 #define IPA0_B2 0xb200
61 #define IPA0_B9 0xb900
62 #define IPA0_EB 0xeb00
63 #define IPA0_E3 0xe300
65 #define PRIV_B2_SCLP_CALL 0x20
66 #define PRIV_B2_CSCH 0x30
67 #define PRIV_B2_HSCH 0x31
68 #define PRIV_B2_MSCH 0x32
69 #define PRIV_B2_SSCH 0x33
70 #define PRIV_B2_STSCH 0x34
71 #define PRIV_B2_TSCH 0x35
72 #define PRIV_B2_TPI 0x36
73 #define PRIV_B2_SAL 0x37
74 #define PRIV_B2_RSCH 0x38
75 #define PRIV_B2_STCRW 0x39
76 #define PRIV_B2_STCPS 0x3a
77 #define PRIV_B2_RCHP 0x3b
78 #define PRIV_B2_SCHM 0x3c
79 #define PRIV_B2_CHSC 0x5f
80 #define PRIV_B2_SIGA 0x74
81 #define PRIV_B2_XSCH 0x76
83 #define PRIV_EB_SQBS 0x8a
84 #define PRIV_EB_PCISTB 0xd0
85 #define PRIV_EB_SIC 0xd1
87 #define PRIV_B9_EQBS 0x9c
88 #define PRIV_B9_CLP 0xa0
89 #define PRIV_B9_PTF 0xa2
90 #define PRIV_B9_PCISTG 0xd0
91 #define PRIV_B9_PCILG 0xd2
92 #define PRIV_B9_RPCIT 0xd3
94 #define PRIV_E3_MPCIFC 0xd0
95 #define PRIV_E3_STPCIFC 0xd4
97 #define DIAG_TIMEREVENT 0x288
98 #define DIAG_IPL 0x308
99 #define DIAG_SET_CONTROL_PROGRAM_CODES 0x318
100 #define DIAG_KVM_HYPERCALL 0x500
101 #define DIAG_KVM_BREAKPOINT 0x501
103 #define ICPT_INSTRUCTION 0x04
104 #define ICPT_PROGRAM 0x08
105 #define ICPT_EXT_INT 0x14
106 #define ICPT_WAITPSW 0x1c
107 #define ICPT_SOFT_INTERCEPT 0x24
108 #define ICPT_CPU_STOP 0x28
109 #define ICPT_OPEREXC 0x2c
111 #define ICPT_PV_INSTR 0x68
112 #define ICPT_PV_INSTR_NOTIFICATION 0x6c
114 #define NR_LOCAL_IRQS 32
116 * Needs to be big enough to contain max_cpus emergency signals
117 * and in addition NR_LOCAL_IRQS interrupts
119 #define VCPU_IRQ_BUF_SIZE(max_cpus) (sizeof(struct kvm_s390_irq) * \
120 (max_cpus + NR_LOCAL_IRQS))
122 * KVM does only support memory slots up to KVM_MEM_MAX_NR_PAGES pages
123 * as the dirty bitmap must be managed by bitops that take an int as
124 * position indicator. This would end at an unaligned address
125 * (0x7fffff00000). As future variants might provide larger pages
126 * and to make all addresses properly aligned, let us split at 4TB.
128 #define KVM_SLOT_MAX_BYTES (4UL * TiB)
130 static CPUWatchpoint hw_watchpoint
;
132 * We don't use a list because this structure is also used to transmit the
133 * hardware breakpoints to the kernel.
135 static struct kvm_hw_breakpoint
*hw_breakpoints
;
136 static int nb_hw_breakpoints
;
138 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
142 static int cap_async_pf
;
143 static int cap_mem_op
;
144 static int cap_mem_op_extension
;
145 static int cap_s390_irq
;
147 static int cap_hpage_1m
;
148 static int cap_vcpu_resets
;
149 static int cap_protected
;
150 static int cap_zpci_op
;
151 static int cap_protected_dump
;
153 static bool mem_op_storage_key_support
;
155 static int active_cmma
;
157 static int kvm_s390_query_mem_limit(uint64_t *memory_limit
)
159 struct kvm_device_attr attr
= {
160 .group
= KVM_S390_VM_MEM_CTRL
,
161 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
162 .addr
= (uint64_t) memory_limit
,
165 return kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
168 int kvm_s390_set_mem_limit(uint64_t new_limit
, uint64_t *hw_limit
)
172 struct kvm_device_attr attr
= {
173 .group
= KVM_S390_VM_MEM_CTRL
,
174 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
175 .addr
= (uint64_t) &new_limit
,
178 if (!kvm_vm_check_mem_attr(kvm_state
, KVM_S390_VM_MEM_LIMIT_SIZE
)) {
182 rc
= kvm_s390_query_mem_limit(hw_limit
);
185 } else if (*hw_limit
< new_limit
) {
189 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
192 int kvm_s390_cmma_active(void)
197 static bool kvm_s390_cmma_available(void)
199 static bool initialized
, value
;
203 value
= kvm_vm_check_mem_attr(kvm_state
, KVM_S390_VM_MEM_ENABLE_CMMA
) &&
204 kvm_vm_check_mem_attr(kvm_state
, KVM_S390_VM_MEM_CLR_CMMA
);
209 void kvm_s390_cmma_reset(void)
212 struct kvm_device_attr attr
= {
213 .group
= KVM_S390_VM_MEM_CTRL
,
214 .attr
= KVM_S390_VM_MEM_CLR_CMMA
,
217 if (!kvm_s390_cmma_active()) {
221 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
222 trace_kvm_clear_cmma(rc
);
225 static void kvm_s390_enable_cmma(void)
228 struct kvm_device_attr attr
= {
229 .group
= KVM_S390_VM_MEM_CTRL
,
230 .attr
= KVM_S390_VM_MEM_ENABLE_CMMA
,
234 warn_report("CMM will not be enabled because it is not "
235 "compatible with huge memory backings.");
238 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
240 trace_kvm_enable_cmma(rc
);
243 static void kvm_s390_set_crypto_attr(uint64_t attr
)
245 struct kvm_device_attr attribute
= {
246 .group
= KVM_S390_VM_CRYPTO
,
250 int ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attribute
);
253 error_report("Failed to set crypto device attribute %lu: %s",
254 attr
, strerror(-ret
));
258 static void kvm_s390_init_aes_kw(void)
260 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_AES_KW
;
262 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
264 attr
= KVM_S390_VM_CRYPTO_ENABLE_AES_KW
;
267 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
268 kvm_s390_set_crypto_attr(attr
);
272 static void kvm_s390_init_dea_kw(void)
274 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
;
276 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
278 attr
= KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
;
281 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
282 kvm_s390_set_crypto_attr(attr
);
286 void kvm_s390_crypto_reset(void)
288 if (s390_has_feat(S390_FEAT_MSA_EXT_3
)) {
289 kvm_s390_init_aes_kw();
290 kvm_s390_init_dea_kw();
294 void kvm_s390_set_max_pagesize(uint64_t pagesize
, Error
**errp
)
296 if (pagesize
== 4 * KiB
) {
300 if (!hpage_1m_allowed()) {
301 error_setg(errp
, "This QEMU machine does not support huge page "
306 if (pagesize
!= 1 * MiB
) {
307 error_setg(errp
, "Memory backing with 2G pages was specified, "
308 "but KVM does not support this memory backing");
312 if (kvm_vm_enable_cap(kvm_state
, KVM_CAP_S390_HPAGE_1M
, 0)) {
313 error_setg(errp
, "Memory backing with 1M pages was specified, "
314 "but KVM does not support this memory backing");
321 int kvm_s390_get_hpage_1m(void)
326 static void ccw_machine_class_foreach(ObjectClass
*oc
, void *opaque
)
328 MachineClass
*mc
= MACHINE_CLASS(oc
);
330 mc
->default_cpu_type
= S390_CPU_TYPE_NAME("host");
333 int kvm_arch_get_default_type(MachineState
*ms
)
338 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
340 int required_caps
[] = {
345 for (int i
= 0; i
< ARRAY_SIZE(required_caps
); i
++) {
346 if (!kvm_check_extension(s
, required_caps
[i
])) {
347 error_report("KVM is missing capability #%d - "
348 "please use kernel 3.15 or newer", required_caps
[i
]);
353 object_class_foreach(ccw_machine_class_foreach
, TYPE_S390_CCW_MACHINE
,
356 if (!kvm_check_extension(s
, KVM_CAP_S390_COW
)) {
357 error_report("KVM is missing capability KVM_CAP_S390_COW - "
358 "unsupported environment");
362 cap_async_pf
= kvm_check_extension(s
, KVM_CAP_ASYNC_PF
);
363 cap_mem_op
= kvm_check_extension(s
, KVM_CAP_S390_MEM_OP
);
364 cap_mem_op_extension
= kvm_check_extension(s
, KVM_CAP_S390_MEM_OP_EXTENSION
);
365 mem_op_storage_key_support
= cap_mem_op_extension
> 0;
366 cap_s390_irq
= kvm_check_extension(s
, KVM_CAP_S390_INJECT_IRQ
);
367 cap_vcpu_resets
= kvm_check_extension(s
, KVM_CAP_S390_VCPU_RESETS
);
368 cap_protected
= kvm_check_extension(s
, KVM_CAP_S390_PROTECTED
);
369 cap_zpci_op
= kvm_check_extension(s
, KVM_CAP_S390_ZPCI_OP
);
370 cap_protected_dump
= kvm_check_extension(s
, KVM_CAP_S390_PROTECTED_DUMP
);
372 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_SIGP
, 0);
373 kvm_vm_enable_cap(s
, KVM_CAP_S390_VECTOR_REGISTERS
, 0);
374 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_STSI
, 0);
375 kvm_vm_enable_cap(s
, KVM_CAP_S390_CPU_TOPOLOGY
, 0);
377 if (kvm_vm_enable_cap(s
, KVM_CAP_S390_RI
, 0) == 0) {
381 if (cpu_model_allowed()) {
382 kvm_vm_enable_cap(s
, KVM_CAP_S390_GS
, 0);
386 * The migration interface for ais was introduced with kernel 4.13
387 * but the capability itself had been active since 4.12. As migration
388 * support is considered necessary, we only try to enable this for
389 * newer machine types if KVM_CAP_S390_AIS_MIGRATION is available.
391 if (cpu_model_allowed() && kvm_kernel_irqchip_allowed() &&
392 kvm_check_extension(s
, KVM_CAP_S390_AIS_MIGRATION
)) {
393 kvm_vm_enable_cap(s
, KVM_CAP_S390_AIS
, 0);
396 kvm_set_max_memslot_size(KVM_SLOT_MAX_BYTES
);
400 int kvm_arch_irqchip_create(KVMState
*s
)
405 unsigned long kvm_arch_vcpu_id(CPUState
*cpu
)
407 return cpu
->cpu_index
;
410 int kvm_arch_init_vcpu(CPUState
*cs
)
412 unsigned int max_cpus
= MACHINE(qdev_get_machine())->smp
.max_cpus
;
413 S390CPU
*cpu
= S390_CPU(cs
);
414 kvm_s390_set_cpu_state(cpu
, cpu
->env
.cpu_state
);
415 cpu
->irqstate
= g_malloc0(VCPU_IRQ_BUF_SIZE(max_cpus
));
419 int kvm_arch_destroy_vcpu(CPUState
*cs
)
421 S390CPU
*cpu
= S390_CPU(cs
);
423 g_free(cpu
->irqstate
);
424 cpu
->irqstate
= NULL
;
429 static void kvm_s390_reset_vcpu(S390CPU
*cpu
, unsigned long type
)
431 CPUState
*cs
= CPU(cpu
);
434 * The reset call is needed here to reset in-kernel vcpu data that
435 * we can't access directly from QEMU (i.e. with older kernels
436 * which don't support sync_regs/ONE_REG). Before this ioctl
437 * cpu_synchronize_state() is called in common kvm code
440 if (kvm_vcpu_ioctl(cs
, type
)) {
441 error_report("CPU reset failed on CPU %i type %lx",
442 cs
->cpu_index
, type
);
446 void kvm_s390_reset_vcpu_initial(S390CPU
*cpu
)
448 kvm_s390_reset_vcpu(cpu
, KVM_S390_INITIAL_RESET
);
451 void kvm_s390_reset_vcpu_clear(S390CPU
*cpu
)
453 if (cap_vcpu_resets
) {
454 kvm_s390_reset_vcpu(cpu
, KVM_S390_CLEAR_RESET
);
456 kvm_s390_reset_vcpu(cpu
, KVM_S390_INITIAL_RESET
);
460 void kvm_s390_reset_vcpu_normal(S390CPU
*cpu
)
462 if (cap_vcpu_resets
) {
463 kvm_s390_reset_vcpu(cpu
, KVM_S390_NORMAL_RESET
);
467 static int can_sync_regs(CPUState
*cs
, int regs
)
469 return (cs
->kvm_run
->kvm_valid_regs
& regs
) == regs
;
472 #define KVM_SYNC_REQUIRED_REGS (KVM_SYNC_GPRS | KVM_SYNC_ACRS | \
473 KVM_SYNC_CRS | KVM_SYNC_PREFIX)
475 int kvm_arch_put_registers(CPUState
*cs
, int level
)
477 S390CPU
*cpu
= S390_CPU(cs
);
478 CPUS390XState
*env
= &cpu
->env
;
479 struct kvm_fpu fpu
= {};
483 g_assert(can_sync_regs(cs
, KVM_SYNC_REQUIRED_REGS
));
485 /* always save the PSW and the GPRS*/
486 cs
->kvm_run
->psw_addr
= env
->psw
.addr
;
487 cs
->kvm_run
->psw_mask
= env
->psw
.mask
;
489 memcpy(cs
->kvm_run
->s
.regs
.gprs
, env
->regs
, sizeof(cs
->kvm_run
->s
.regs
.gprs
));
490 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_GPRS
;
492 if (can_sync_regs(cs
, KVM_SYNC_VRS
)) {
493 for (i
= 0; i
< 32; i
++) {
494 cs
->kvm_run
->s
.regs
.vrs
[i
][0] = env
->vregs
[i
][0];
495 cs
->kvm_run
->s
.regs
.vrs
[i
][1] = env
->vregs
[i
][1];
497 cs
->kvm_run
->s
.regs
.fpc
= env
->fpc
;
498 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_VRS
;
499 } else if (can_sync_regs(cs
, KVM_SYNC_FPRS
)) {
500 for (i
= 0; i
< 16; i
++) {
501 cs
->kvm_run
->s
.regs
.fprs
[i
] = *get_freg(env
, i
);
503 cs
->kvm_run
->s
.regs
.fpc
= env
->fpc
;
504 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_FPRS
;
507 for (i
= 0; i
< 16; i
++) {
508 fpu
.fprs
[i
] = *get_freg(env
, i
);
512 r
= kvm_vcpu_ioctl(cs
, KVM_SET_FPU
, &fpu
);
518 /* Do we need to save more than that? */
519 if (level
== KVM_PUT_RUNTIME_STATE
) {
524 * Access registers, control registers and the prefix - these are
525 * always available via kvm_sync_regs in the kernels that we support
527 memcpy(cs
->kvm_run
->s
.regs
.acrs
, env
->aregs
, sizeof(cs
->kvm_run
->s
.regs
.acrs
));
528 memcpy(cs
->kvm_run
->s
.regs
.crs
, env
->cregs
, sizeof(cs
->kvm_run
->s
.regs
.crs
));
529 cs
->kvm_run
->s
.regs
.prefix
= env
->psa
;
530 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ACRS
| KVM_SYNC_CRS
| KVM_SYNC_PREFIX
;
532 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
533 cs
->kvm_run
->s
.regs
.cputm
= env
->cputm
;
534 cs
->kvm_run
->s
.regs
.ckc
= env
->ckc
;
535 cs
->kvm_run
->s
.regs
.todpr
= env
->todpr
;
536 cs
->kvm_run
->s
.regs
.gbea
= env
->gbea
;
537 cs
->kvm_run
->s
.regs
.pp
= env
->pp
;
538 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ARCH0
;
541 * These ONE_REGS are not protected by a capability. As they are only
542 * necessary for migration we just trace a possible error, but don't
543 * return with an error return code.
545 kvm_set_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
546 kvm_set_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
547 kvm_set_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
548 kvm_set_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
549 kvm_set_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
552 if (can_sync_regs(cs
, KVM_SYNC_RICCB
)) {
553 memcpy(cs
->kvm_run
->s
.regs
.riccb
, env
->riccb
, 64);
554 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_RICCB
;
557 /* pfault parameters */
558 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
559 cs
->kvm_run
->s
.regs
.pft
= env
->pfault_token
;
560 cs
->kvm_run
->s
.regs
.pfs
= env
->pfault_select
;
561 cs
->kvm_run
->s
.regs
.pfc
= env
->pfault_compare
;
562 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PFAULT
;
563 } else if (cap_async_pf
) {
564 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
568 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
572 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
578 if (can_sync_regs(cs
, KVM_SYNC_GSCB
)) {
579 memcpy(cs
->kvm_run
->s
.regs
.gscb
, env
->gscb
, 32);
580 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_GSCB
;
583 if (can_sync_regs(cs
, KVM_SYNC_BPBC
)) {
584 cs
->kvm_run
->s
.regs
.bpbc
= env
->bpbc
;
585 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_BPBC
;
588 if (can_sync_regs(cs
, KVM_SYNC_ETOKEN
)) {
589 cs
->kvm_run
->s
.regs
.etoken
= env
->etoken
;
590 cs
->kvm_run
->s
.regs
.etoken_extension
= env
->etoken_extension
;
591 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ETOKEN
;
594 if (can_sync_regs(cs
, KVM_SYNC_DIAG318
)) {
595 cs
->kvm_run
->s
.regs
.diag318
= env
->diag318_info
;
596 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_DIAG318
;
602 int kvm_arch_get_registers(CPUState
*cs
)
604 S390CPU
*cpu
= S390_CPU(cs
);
605 CPUS390XState
*env
= &cpu
->env
;
610 env
->psw
.addr
= cs
->kvm_run
->psw_addr
;
611 env
->psw
.mask
= cs
->kvm_run
->psw_mask
;
613 /* the GPRS, ACRS and CRS */
614 g_assert(can_sync_regs(cs
, KVM_SYNC_REQUIRED_REGS
));
615 memcpy(env
->regs
, cs
->kvm_run
->s
.regs
.gprs
, sizeof(env
->regs
));
616 memcpy(env
->aregs
, cs
->kvm_run
->s
.regs
.acrs
, sizeof(env
->aregs
));
617 memcpy(env
->cregs
, cs
->kvm_run
->s
.regs
.crs
, sizeof(env
->cregs
));
620 env
->psa
= cs
->kvm_run
->s
.regs
.prefix
;
622 /* Floating point and vector registers */
623 if (can_sync_regs(cs
, KVM_SYNC_VRS
)) {
624 for (i
= 0; i
< 32; i
++) {
625 env
->vregs
[i
][0] = cs
->kvm_run
->s
.regs
.vrs
[i
][0];
626 env
->vregs
[i
][1] = cs
->kvm_run
->s
.regs
.vrs
[i
][1];
628 env
->fpc
= cs
->kvm_run
->s
.regs
.fpc
;
629 } else if (can_sync_regs(cs
, KVM_SYNC_FPRS
)) {
630 for (i
= 0; i
< 16; i
++) {
631 *get_freg(env
, i
) = cs
->kvm_run
->s
.regs
.fprs
[i
];
633 env
->fpc
= cs
->kvm_run
->s
.regs
.fpc
;
635 r
= kvm_vcpu_ioctl(cs
, KVM_GET_FPU
, &fpu
);
639 for (i
= 0; i
< 16; i
++) {
640 *get_freg(env
, i
) = fpu
.fprs
[i
];
645 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
646 env
->cputm
= cs
->kvm_run
->s
.regs
.cputm
;
647 env
->ckc
= cs
->kvm_run
->s
.regs
.ckc
;
648 env
->todpr
= cs
->kvm_run
->s
.regs
.todpr
;
649 env
->gbea
= cs
->kvm_run
->s
.regs
.gbea
;
650 env
->pp
= cs
->kvm_run
->s
.regs
.pp
;
653 * These ONE_REGS are not protected by a capability. As they are only
654 * necessary for migration we just trace a possible error, but don't
655 * return with an error return code.
657 kvm_get_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
658 kvm_get_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
659 kvm_get_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
660 kvm_get_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
661 kvm_get_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
664 if (can_sync_regs(cs
, KVM_SYNC_RICCB
)) {
665 memcpy(env
->riccb
, cs
->kvm_run
->s
.regs
.riccb
, 64);
668 if (can_sync_regs(cs
, KVM_SYNC_GSCB
)) {
669 memcpy(env
->gscb
, cs
->kvm_run
->s
.regs
.gscb
, 32);
672 if (can_sync_regs(cs
, KVM_SYNC_BPBC
)) {
673 env
->bpbc
= cs
->kvm_run
->s
.regs
.bpbc
;
676 if (can_sync_regs(cs
, KVM_SYNC_ETOKEN
)) {
677 env
->etoken
= cs
->kvm_run
->s
.regs
.etoken
;
678 env
->etoken_extension
= cs
->kvm_run
->s
.regs
.etoken_extension
;
681 /* pfault parameters */
682 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
683 env
->pfault_token
= cs
->kvm_run
->s
.regs
.pft
;
684 env
->pfault_select
= cs
->kvm_run
->s
.regs
.pfs
;
685 env
->pfault_compare
= cs
->kvm_run
->s
.regs
.pfc
;
686 } else if (cap_async_pf
) {
687 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
691 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
695 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
701 if (can_sync_regs(cs
, KVM_SYNC_DIAG318
)) {
702 env
->diag318_info
= cs
->kvm_run
->s
.regs
.diag318
;
708 int kvm_s390_get_clock(uint8_t *tod_high
, uint64_t *tod_low
)
711 struct kvm_device_attr attr
= {
712 .group
= KVM_S390_VM_TOD
,
713 .attr
= KVM_S390_VM_TOD_LOW
,
714 .addr
= (uint64_t)tod_low
,
717 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
722 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
723 attr
.addr
= (uint64_t)tod_high
;
724 return kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
727 int kvm_s390_get_clock_ext(uint8_t *tod_high
, uint64_t *tod_low
)
730 struct kvm_s390_vm_tod_clock gtod
;
731 struct kvm_device_attr attr
= {
732 .group
= KVM_S390_VM_TOD
,
733 .attr
= KVM_S390_VM_TOD_EXT
,
734 .addr
= (uint64_t)>od
,
737 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
738 *tod_high
= gtod
.epoch_idx
;
744 int kvm_s390_set_clock(uint8_t tod_high
, uint64_t tod_low
)
747 struct kvm_device_attr attr
= {
748 .group
= KVM_S390_VM_TOD
,
749 .attr
= KVM_S390_VM_TOD_LOW
,
750 .addr
= (uint64_t)&tod_low
,
753 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
758 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
759 attr
.addr
= (uint64_t)&tod_high
;
760 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
763 int kvm_s390_set_clock_ext(uint8_t tod_high
, uint64_t tod_low
)
765 struct kvm_s390_vm_tod_clock gtod
= {
766 .epoch_idx
= tod_high
,
769 struct kvm_device_attr attr
= {
770 .group
= KVM_S390_VM_TOD
,
771 .attr
= KVM_S390_VM_TOD_EXT
,
772 .addr
= (uint64_t)>od
,
775 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
780 * @addr: the logical start address in guest memory
781 * @ar: the access register number
782 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
783 * @len: length that should be transferred
784 * @is_write: true = write, false = read
785 * Returns: 0 on success, non-zero if an exception or error occurred
787 * Use KVM ioctl to read/write from/to guest memory. An access exception
788 * is injected into the vCPU in case of translation errors.
790 int kvm_s390_mem_op(S390CPU
*cpu
, vaddr addr
, uint8_t ar
, void *hostbuf
,
791 int len
, bool is_write
)
793 struct kvm_s390_mem_op mem_op
= {
795 .flags
= KVM_S390_MEMOP_F_INJECT_EXCEPTION
,
797 .op
= is_write
? KVM_S390_MEMOP_LOGICAL_WRITE
798 : KVM_S390_MEMOP_LOGICAL_READ
,
799 .buf
= (uint64_t)hostbuf
,
801 .key
= (cpu
->env
.psw
.mask
& PSW_MASK_KEY
) >> PSW_SHIFT_KEY
,
809 mem_op
.flags
|= KVM_S390_MEMOP_F_CHECK_ONLY
;
811 if (mem_op_storage_key_support
) {
812 mem_op
.flags
|= KVM_S390_MEMOP_F_SKEY_PROTECTION
;
815 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_S390_MEM_OP
, &mem_op
);
817 warn_report("KVM_S390_MEM_OP failed: %s", strerror(-ret
));
822 int kvm_s390_mem_op_pv(S390CPU
*cpu
, uint64_t offset
, void *hostbuf
,
823 int len
, bool is_write
)
825 struct kvm_s390_mem_op mem_op
= {
826 .sida_offset
= offset
,
828 .op
= is_write
? KVM_S390_MEMOP_SIDA_WRITE
829 : KVM_S390_MEMOP_SIDA_READ
,
830 .buf
= (uint64_t)hostbuf
,
834 if (!cap_mem_op
|| !cap_protected
) {
838 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_S390_MEM_OP
, &mem_op
);
840 error_report("KVM_S390_MEM_OP failed: %s", strerror(-ret
));
846 static uint8_t const *sw_bp_inst
;
847 static uint8_t sw_bp_ilen
;
849 static void determine_sw_breakpoint_instr(void)
851 /* DIAG 501 is used for sw breakpoints with old kernels */
852 static const uint8_t diag_501
[] = {0x83, 0x24, 0x05, 0x01};
853 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
854 static const uint8_t instr_0x0000
[] = {0x00, 0x00};
859 if (kvm_vm_enable_cap(kvm_state
, KVM_CAP_S390_USER_INSTR0
, 0)) {
860 sw_bp_inst
= diag_501
;
861 sw_bp_ilen
= sizeof(diag_501
);
862 trace_kvm_sw_breakpoint(4);
864 sw_bp_inst
= instr_0x0000
;
865 sw_bp_ilen
= sizeof(instr_0x0000
);
866 trace_kvm_sw_breakpoint(2);
870 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
872 determine_sw_breakpoint_instr();
874 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
876 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)sw_bp_inst
, sw_bp_ilen
, 1)) {
882 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
886 if (cpu_memory_rw_debug(cs
, bp
->pc
, t
, sw_bp_ilen
, 0)) {
888 } else if (memcmp(t
, sw_bp_inst
, sw_bp_ilen
)) {
890 } else if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
898 static struct kvm_hw_breakpoint
*find_hw_breakpoint(target_ulong addr
,
903 for (n
= 0; n
< nb_hw_breakpoints
; n
++) {
904 if (hw_breakpoints
[n
].addr
== addr
&& hw_breakpoints
[n
].type
== type
&&
905 (hw_breakpoints
[n
].len
== len
|| len
== -1)) {
906 return &hw_breakpoints
[n
];
913 static int insert_hw_breakpoint(target_ulong addr
, int len
, int type
)
917 if (find_hw_breakpoint(addr
, len
, type
)) {
921 size
= (nb_hw_breakpoints
+ 1) * sizeof(struct kvm_hw_breakpoint
);
923 if (!hw_breakpoints
) {
924 nb_hw_breakpoints
= 0;
925 hw_breakpoints
= (struct kvm_hw_breakpoint
*)g_try_malloc(size
);
928 (struct kvm_hw_breakpoint
*)g_try_realloc(hw_breakpoints
, size
);
931 if (!hw_breakpoints
) {
932 nb_hw_breakpoints
= 0;
936 hw_breakpoints
[nb_hw_breakpoints
].addr
= addr
;
937 hw_breakpoints
[nb_hw_breakpoints
].len
= len
;
938 hw_breakpoints
[nb_hw_breakpoints
].type
= type
;
945 int kvm_arch_insert_hw_breakpoint(vaddr addr
, vaddr len
, int type
)
948 case GDB_BREAKPOINT_HW
:
951 case GDB_WATCHPOINT_WRITE
:
955 type
= KVM_HW_WP_WRITE
;
960 return insert_hw_breakpoint(addr
, len
, type
);
963 int kvm_arch_remove_hw_breakpoint(vaddr addr
, vaddr len
, int type
)
966 struct kvm_hw_breakpoint
*bp
= find_hw_breakpoint(addr
, len
, type
);
973 if (nb_hw_breakpoints
> 0) {
975 * In order to trim the array, move the last element to the position to
976 * be removed - if necessary.
978 if (bp
!= &hw_breakpoints
[nb_hw_breakpoints
]) {
979 *bp
= hw_breakpoints
[nb_hw_breakpoints
];
981 size
= nb_hw_breakpoints
* sizeof(struct kvm_hw_breakpoint
);
983 g_realloc(hw_breakpoints
, size
);
985 g_free(hw_breakpoints
);
986 hw_breakpoints
= NULL
;
992 void kvm_arch_remove_all_hw_breakpoints(void)
994 nb_hw_breakpoints
= 0;
995 g_free(hw_breakpoints
);
996 hw_breakpoints
= NULL
;
999 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
1003 if (nb_hw_breakpoints
> 0) {
1004 dbg
->arch
.nr_hw_bp
= nb_hw_breakpoints
;
1005 dbg
->arch
.hw_bp
= hw_breakpoints
;
1007 for (i
= 0; i
< nb_hw_breakpoints
; ++i
) {
1008 hw_breakpoints
[i
].phys_addr
= s390_cpu_get_phys_addr_debug(cpu
,
1009 hw_breakpoints
[i
].addr
);
1011 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
1013 dbg
->arch
.nr_hw_bp
= 0;
1014 dbg
->arch
.hw_bp
= NULL
;
1018 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
1022 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
1024 return MEMTXATTRS_UNSPECIFIED
;
1027 int kvm_arch_process_async_events(CPUState
*cs
)
1032 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq
*irq
,
1033 struct kvm_s390_interrupt
*interrupt
)
1037 interrupt
->type
= irq
->type
;
1038 switch (irq
->type
) {
1039 case KVM_S390_INT_VIRTIO
:
1040 interrupt
->parm
= irq
->u
.ext
.ext_params
;
1042 case KVM_S390_INT_PFAULT_INIT
:
1043 case KVM_S390_INT_PFAULT_DONE
:
1044 interrupt
->parm64
= irq
->u
.ext
.ext_params2
;
1046 case KVM_S390_PROGRAM_INT
:
1047 interrupt
->parm
= irq
->u
.pgm
.code
;
1049 case KVM_S390_SIGP_SET_PREFIX
:
1050 interrupt
->parm
= irq
->u
.prefix
.address
;
1052 case KVM_S390_INT_SERVICE
:
1053 interrupt
->parm
= irq
->u
.ext
.ext_params
;
1056 interrupt
->parm
= irq
->u
.mchk
.cr14
;
1057 interrupt
->parm64
= irq
->u
.mchk
.mcic
;
1059 case KVM_S390_INT_EXTERNAL_CALL
:
1060 interrupt
->parm
= irq
->u
.extcall
.code
;
1062 case KVM_S390_INT_EMERGENCY
:
1063 interrupt
->parm
= irq
->u
.emerg
.code
;
1065 case KVM_S390_SIGP_STOP
:
1066 case KVM_S390_RESTART
:
1067 break; /* These types have no parameters */
1068 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1069 interrupt
->parm
= irq
->u
.io
.subchannel_id
<< 16;
1070 interrupt
->parm
|= irq
->u
.io
.subchannel_nr
;
1071 interrupt
->parm64
= (uint64_t)irq
->u
.io
.io_int_parm
<< 32;
1072 interrupt
->parm64
|= irq
->u
.io
.io_int_word
;
1081 static void inject_vcpu_irq_legacy(CPUState
*cs
, struct kvm_s390_irq
*irq
)
1083 struct kvm_s390_interrupt kvmint
= {};
1086 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
1088 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
1092 r
= kvm_vcpu_ioctl(cs
, KVM_S390_INTERRUPT
, &kvmint
);
1094 fprintf(stderr
, "KVM failed to inject interrupt\n");
1099 void kvm_s390_vcpu_interrupt(S390CPU
*cpu
, struct kvm_s390_irq
*irq
)
1101 CPUState
*cs
= CPU(cpu
);
1105 r
= kvm_vcpu_ioctl(cs
, KVM_S390_IRQ
, irq
);
1109 error_report("KVM failed to inject interrupt %llx", irq
->type
);
1113 inject_vcpu_irq_legacy(cs
, irq
);
1116 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq
*irq
)
1118 struct kvm_s390_interrupt kvmint
= {};
1121 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
1123 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
1127 r
= kvm_vm_ioctl(kvm_state
, KVM_S390_INTERRUPT
, &kvmint
);
1129 fprintf(stderr
, "KVM failed to inject interrupt\n");
1134 void kvm_s390_program_interrupt(S390CPU
*cpu
, uint16_t code
)
1136 struct kvm_s390_irq irq
= {
1137 .type
= KVM_S390_PROGRAM_INT
,
1140 qemu_log_mask(CPU_LOG_INT
, "program interrupt at %#" PRIx64
"\n",
1142 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1145 void kvm_s390_access_exception(S390CPU
*cpu
, uint16_t code
, uint64_t te_code
)
1147 struct kvm_s390_irq irq
= {
1148 .type
= KVM_S390_PROGRAM_INT
,
1150 .u
.pgm
.trans_exc_code
= te_code
,
1151 .u
.pgm
.exc_access_id
= te_code
& 3,
1154 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1157 static void kvm_sclp_service_call(S390CPU
*cpu
, struct kvm_run
*run
,
1160 CPUS390XState
*env
= &cpu
->env
;
1165 sccb
= env
->regs
[ipbh0
& 0xf];
1166 code
= env
->regs
[(ipbh0
& 0xf0) >> 4];
1168 switch (run
->s390_sieic
.icptcode
) {
1169 case ICPT_PV_INSTR_NOTIFICATION
:
1170 g_assert(s390_is_pv());
1171 /* The notification intercepts are currently handled by KVM */
1172 error_report("unexpected SCLP PV notification");
1176 g_assert(s390_is_pv());
1177 sclp_service_call_protected(cpu
, sccb
, code
);
1178 /* Setting the CC is done by the Ultravisor. */
1180 case ICPT_INSTRUCTION
:
1181 g_assert(!s390_is_pv());
1182 r
= sclp_service_call(cpu
, sccb
, code
);
1184 kvm_s390_program_interrupt(cpu
, -r
);
1191 static int handle_b2(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1193 CPUS390XState
*env
= &cpu
->env
;
1195 uint16_t ipbh0
= (run
->s390_sieic
.ipb
& 0xffff0000) >> 16;
1199 ioinst_handle_xsch(cpu
, env
->regs
[1], RA_IGNORED
);
1202 ioinst_handle_csch(cpu
, env
->regs
[1], RA_IGNORED
);
1205 ioinst_handle_hsch(cpu
, env
->regs
[1], RA_IGNORED
);
1208 ioinst_handle_msch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1211 ioinst_handle_ssch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1214 ioinst_handle_stcrw(cpu
, run
->s390_sieic
.ipb
, RA_IGNORED
);
1217 ioinst_handle_stsch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1220 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1221 fprintf(stderr
, "Spurious tsch intercept\n");
1224 ioinst_handle_chsc(cpu
, run
->s390_sieic
.ipb
, RA_IGNORED
);
1227 /* This should have been handled by kvm already. */
1228 fprintf(stderr
, "Spurious tpi intercept\n");
1231 ioinst_handle_schm(cpu
, env
->regs
[1], env
->regs
[2],
1232 run
->s390_sieic
.ipb
, RA_IGNORED
);
1235 ioinst_handle_rsch(cpu
, env
->regs
[1], RA_IGNORED
);
1238 ioinst_handle_rchp(cpu
, env
->regs
[1], RA_IGNORED
);
1241 /* We do not provide this instruction, it is suppressed. */
1244 ioinst_handle_sal(cpu
, env
->regs
[1], RA_IGNORED
);
1247 /* Not provided, set CC = 3 for subchannel not operational */
1250 case PRIV_B2_SCLP_CALL
:
1251 kvm_sclp_service_call(cpu
, run
, ipbh0
);
1255 trace_kvm_insn_unhandled_priv(ipa1
);
1262 static uint64_t get_base_disp_rxy(S390CPU
*cpu
, struct kvm_run
*run
,
1265 CPUS390XState
*env
= &cpu
->env
;
1266 uint32_t x2
= (run
->s390_sieic
.ipa
& 0x000f);
1267 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1268 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1269 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1271 if (disp2
& 0x80000) {
1272 disp2
+= 0xfff00000;
1278 return (base2
? env
->regs
[base2
] : 0) +
1279 (x2
? env
->regs
[x2
] : 0) + (long)(int)disp2
;
1282 static uint64_t get_base_disp_rsy(S390CPU
*cpu
, struct kvm_run
*run
,
1285 CPUS390XState
*env
= &cpu
->env
;
1286 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1287 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1288 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1290 if (disp2
& 0x80000) {
1291 disp2
+= 0xfff00000;
1297 return (base2
? env
->regs
[base2
] : 0) + (long)(int)disp2
;
1300 static int kvm_clp_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1302 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1304 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1305 return clp_service_call(cpu
, r2
, RA_IGNORED
);
1311 static int kvm_pcilg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1313 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1314 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1316 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1317 return pcilg_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1323 static int kvm_pcistg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1325 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1326 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1328 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1329 return pcistg_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1335 static int kvm_stpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1337 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1341 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1342 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1344 return stpcifc_service_call(cpu
, r1
, fiba
, ar
, RA_IGNORED
);
1350 static int kvm_sic_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1352 CPUS390XState
*env
= &cpu
->env
;
1353 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1354 uint8_t r3
= run
->s390_sieic
.ipa
& 0x000f;
1359 mode
= env
->regs
[r1
] & 0xffff;
1360 isc
= (env
->regs
[r3
] >> 27) & 0x7;
1361 r
= css_do_sic(cpu
, isc
, mode
);
1363 kvm_s390_program_interrupt(cpu
, -r
);
1369 static int kvm_rpcit_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1371 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1372 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1374 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1375 return rpcit_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1381 static int kvm_pcistb_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1383 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1384 uint8_t r3
= run
->s390_sieic
.ipa
& 0x000f;
1388 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1389 gaddr
= get_base_disp_rsy(cpu
, run
, &ar
);
1391 return pcistb_service_call(cpu
, r1
, r3
, gaddr
, ar
, RA_IGNORED
);
1397 static int kvm_mpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1399 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1403 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1404 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1406 return mpcifc_service_call(cpu
, r1
, fiba
, ar
, RA_IGNORED
);
1412 static void kvm_handle_ptf(S390CPU
*cpu
, struct kvm_run
*run
)
1414 uint8_t r1
= (run
->s390_sieic
.ipb
>> 20) & 0x0f;
1416 s390_handle_ptf(cpu
, r1
, RA_IGNORED
);
1419 static int handle_b9(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1425 r
= kvm_clp_service_call(cpu
, run
);
1427 case PRIV_B9_PCISTG
:
1428 r
= kvm_pcistg_service_call(cpu
, run
);
1431 r
= kvm_pcilg_service_call(cpu
, run
);
1434 r
= kvm_rpcit_service_call(cpu
, run
);
1437 kvm_handle_ptf(cpu
, run
);
1440 /* just inject exception */
1445 trace_kvm_insn_unhandled_priv(ipa1
);
1452 static int handle_eb(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1457 case PRIV_EB_PCISTB
:
1458 r
= kvm_pcistb_service_call(cpu
, run
);
1461 r
= kvm_sic_service_call(cpu
, run
);
1464 /* just inject exception */
1469 trace_kvm_insn_unhandled_priv(ipbl
);
1476 static int handle_e3(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1481 case PRIV_E3_MPCIFC
:
1482 r
= kvm_mpcifc_service_call(cpu
, run
);
1484 case PRIV_E3_STPCIFC
:
1485 r
= kvm_stpcifc_service_call(cpu
, run
);
1489 trace_kvm_insn_unhandled_priv(ipbl
);
1496 static int handle_hypercall(S390CPU
*cpu
, struct kvm_run
*run
)
1498 CPUS390XState
*env
= &cpu
->env
;
1501 ret
= s390_virtio_hypercall(env
);
1502 if (ret
== -EINVAL
) {
1503 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1510 static void kvm_handle_diag_288(S390CPU
*cpu
, struct kvm_run
*run
)
1515 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1516 r3
= run
->s390_sieic
.ipa
& 0x000f;
1517 rc
= handle_diag_288(&cpu
->env
, r1
, r3
);
1519 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1523 static void kvm_handle_diag_308(S390CPU
*cpu
, struct kvm_run
*run
)
1527 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1528 r3
= run
->s390_sieic
.ipa
& 0x000f;
1529 handle_diag_308(&cpu
->env
, r1
, r3
, RA_IGNORED
);
1532 static int handle_sw_breakpoint(S390CPU
*cpu
, struct kvm_run
*run
)
1534 CPUS390XState
*env
= &cpu
->env
;
1537 pc
= env
->psw
.addr
- sw_bp_ilen
;
1538 if (kvm_find_sw_breakpoint(CPU(cpu
), pc
)) {
1546 void kvm_s390_set_diag318(CPUState
*cs
, uint64_t diag318_info
)
1548 CPUS390XState
*env
= &S390_CPU(cs
)->env
;
1550 /* Feat bit is set only if KVM supports sync for diag318 */
1551 if (s390_has_feat(S390_FEAT_DIAG_318
)) {
1552 env
->diag318_info
= diag318_info
;
1553 cs
->kvm_run
->s
.regs
.diag318
= diag318_info
;
1554 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_DIAG318
;
1556 * diag 318 info is zeroed during a clear reset and
1557 * diag 308 IPL subcodes.
1562 static void handle_diag_318(S390CPU
*cpu
, struct kvm_run
*run
)
1564 uint64_t reg
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1565 uint64_t diag318_info
= run
->s
.regs
.gprs
[reg
];
1569 * DIAG 318 can only be enabled with KVM support. As such, let's
1570 * ensure a guest cannot execute this instruction erroneously.
1572 if (!s390_has_feat(S390_FEAT_DIAG_318
)) {
1573 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1578 run_on_cpu(t
, s390_do_cpu_set_diag318
,
1579 RUN_ON_CPU_HOST_ULONG(diag318_info
));
1583 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1585 static int handle_diag(S390CPU
*cpu
, struct kvm_run
*run
, uint32_t ipb
)
1591 * For any diagnose call we support, bits 48-63 of the resulting
1592 * address specify the function code; the remainder is ignored.
1594 func_code
= decode_basedisp_rs(&cpu
->env
, ipb
, NULL
) & DIAG_KVM_CODE_MASK
;
1595 switch (func_code
) {
1596 case DIAG_TIMEREVENT
:
1597 kvm_handle_diag_288(cpu
, run
);
1600 kvm_handle_diag_308(cpu
, run
);
1602 case DIAG_SET_CONTROL_PROGRAM_CODES
:
1603 handle_diag_318(cpu
, run
);
1605 case DIAG_KVM_HYPERCALL
:
1606 r
= handle_hypercall(cpu
, run
);
1608 case DIAG_KVM_BREAKPOINT
:
1609 r
= handle_sw_breakpoint(cpu
, run
);
1612 trace_kvm_insn_diag(func_code
);
1613 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1620 static int kvm_s390_handle_sigp(S390CPU
*cpu
, uint8_t ipa1
, uint32_t ipb
)
1622 CPUS390XState
*env
= &cpu
->env
;
1623 const uint8_t r1
= ipa1
>> 4;
1624 const uint8_t r3
= ipa1
& 0x0f;
1628 /* get order code */
1629 order
= decode_basedisp_rs(env
, ipb
, NULL
) & SIGP_ORDER_MASK
;
1631 ret
= handle_sigp(env
, order
, r1
, r3
);
1636 static int handle_instruction(S390CPU
*cpu
, struct kvm_run
*run
)
1638 unsigned int ipa0
= (run
->s390_sieic
.ipa
& 0xff00);
1639 uint8_t ipa1
= run
->s390_sieic
.ipa
& 0x00ff;
1642 trace_kvm_insn(run
->s390_sieic
.ipa
, run
->s390_sieic
.ipb
);
1645 r
= handle_b2(cpu
, run
, ipa1
);
1648 r
= handle_b9(cpu
, run
, ipa1
);
1651 r
= handle_eb(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1654 r
= handle_e3(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1657 r
= handle_diag(cpu
, run
, run
->s390_sieic
.ipb
);
1660 r
= kvm_s390_handle_sigp(cpu
, ipa1
, run
->s390_sieic
.ipb
);
1666 kvm_s390_program_interrupt(cpu
, PGM_OPERATION
);
1672 static void unmanageable_intercept(S390CPU
*cpu
, S390CrashReason reason
,
1675 CPUState
*cs
= CPU(cpu
);
1678 cpu
->env
.crash_reason
= reason
;
1679 qemu_system_guest_panicked(cpu_get_crash_info(cs
));
1682 /* try to detect pgm check loops */
1683 static int handle_oper_loop(S390CPU
*cpu
, struct kvm_run
*run
)
1685 CPUState
*cs
= CPU(cpu
);
1688 newpsw
.mask
= ldq_phys(cs
->as
, cpu
->env
.psa
+
1689 offsetof(LowCore
, program_new_psw
));
1690 newpsw
.addr
= ldq_phys(cs
->as
, cpu
->env
.psa
+
1691 offsetof(LowCore
, program_new_psw
) + 8);
1692 oldpsw
.mask
= run
->psw_mask
;
1693 oldpsw
.addr
= run
->psw_addr
;
1695 * Avoid endless loops of operation exceptions, if the pgm new
1696 * PSW will cause a new operation exception.
1697 * The heuristic checks if the pgm new psw is within 6 bytes before
1698 * the faulting psw address (with same DAT, AS settings) and the
1699 * new psw is not a wait psw and the fault was not triggered by
1700 * problem state. In that case go into crashed state.
1703 if (oldpsw
.addr
- newpsw
.addr
<= 6 &&
1704 !(newpsw
.mask
& PSW_MASK_WAIT
) &&
1705 !(oldpsw
.mask
& PSW_MASK_PSTATE
) &&
1706 (newpsw
.mask
& PSW_MASK_ASC
) == (oldpsw
.mask
& PSW_MASK_ASC
) &&
1707 (newpsw
.mask
& PSW_MASK_DAT
) == (oldpsw
.mask
& PSW_MASK_DAT
)) {
1708 unmanageable_intercept(cpu
, S390_CRASH_REASON_OPINT_LOOP
,
1709 offsetof(LowCore
, program_new_psw
));
1715 static int handle_intercept(S390CPU
*cpu
)
1717 CPUState
*cs
= CPU(cpu
);
1718 struct kvm_run
*run
= cs
->kvm_run
;
1719 int icpt_code
= run
->s390_sieic
.icptcode
;
1722 trace_kvm_intercept(icpt_code
, (long)run
->psw_addr
);
1723 switch (icpt_code
) {
1724 case ICPT_INSTRUCTION
:
1726 case ICPT_PV_INSTR_NOTIFICATION
:
1727 r
= handle_instruction(cpu
, run
);
1730 unmanageable_intercept(cpu
, S390_CRASH_REASON_PGMINT_LOOP
,
1731 offsetof(LowCore
, program_new_psw
));
1735 unmanageable_intercept(cpu
, S390_CRASH_REASON_EXTINT_LOOP
,
1736 offsetof(LowCore
, external_new_psw
));
1740 /* disabled wait, since enabled wait is handled in kernel */
1741 s390_handle_wait(cpu
);
1745 do_stop_interrupt(&cpu
->env
);
1749 /* check for break points */
1750 r
= handle_sw_breakpoint(cpu
, run
);
1752 /* Then check for potential pgm check loops */
1753 r
= handle_oper_loop(cpu
, run
);
1755 kvm_s390_program_interrupt(cpu
, PGM_OPERATION
);
1759 case ICPT_SOFT_INTERCEPT
:
1760 fprintf(stderr
, "KVM unimplemented icpt SOFT\n");
1764 fprintf(stderr
, "KVM unimplemented icpt IO\n");
1768 fprintf(stderr
, "Unknown intercept code: %d\n", icpt_code
);
1776 static int handle_tsch(S390CPU
*cpu
)
1778 CPUState
*cs
= CPU(cpu
);
1779 struct kvm_run
*run
= cs
->kvm_run
;
1782 ret
= ioinst_handle_tsch(cpu
, cpu
->env
.regs
[1], run
->s390_tsch
.ipb
,
1787 * If an I/O interrupt had been dequeued, we have to reinject it.
1789 if (run
->s390_tsch
.dequeued
) {
1790 s390_io_interrupt(run
->s390_tsch
.subchannel_id
,
1791 run
->s390_tsch
.subchannel_nr
,
1792 run
->s390_tsch
.io_int_parm
,
1793 run
->s390_tsch
.io_int_word
);
1800 static void insert_stsi_3_2_2(S390CPU
*cpu
, __u64 addr
, uint8_t ar
)
1802 const MachineState
*ms
= MACHINE(qdev_get_machine());
1803 uint16_t conf_cpus
= 0, reserved_cpus
= 0;
1808 s390_cpu_pv_mem_read(cpu
, 0, &sysib
, sizeof(sysib
));
1809 } else if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &sysib
, sizeof(sysib
))) {
1812 /* Shift the stack of Extended Names to prepare for our own data */
1813 memmove(&sysib
.ext_names
[1], &sysib
.ext_names
[0],
1814 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- 1));
1815 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1816 * assumed it's not capable of managing Extended Names for lower levels.
1818 for (del
= 1; del
< sysib
.count
; del
++) {
1819 if (!sysib
.vm
[del
].ext_name_encoding
|| !sysib
.ext_names
[del
][0]) {
1823 if (del
< sysib
.count
) {
1824 memset(sysib
.ext_names
[del
], 0,
1825 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- del
));
1828 /* count the cpus and split them into configured and reserved ones */
1829 for (i
= 0; i
< ms
->possible_cpus
->len
; i
++) {
1830 if (ms
->possible_cpus
->cpus
[i
].cpu
) {
1836 sysib
.vm
[0].total_cpus
= conf_cpus
+ reserved_cpus
;
1837 sysib
.vm
[0].conf_cpus
= conf_cpus
;
1838 sysib
.vm
[0].reserved_cpus
= reserved_cpus
;
1840 /* Insert short machine name in EBCDIC, padded with blanks */
1842 memset(sysib
.vm
[0].name
, 0x40, sizeof(sysib
.vm
[0].name
));
1843 ebcdic_put(sysib
.vm
[0].name
, qemu_name
, MIN(sizeof(sysib
.vm
[0].name
),
1844 strlen(qemu_name
)));
1846 sysib
.vm
[0].ext_name_encoding
= 2; /* 2 = UTF-8 */
1847 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1848 * considered by s390 as not capable of providing any Extended Name.
1849 * Therefore if no name was specified on qemu invocation, we go with the
1850 * same "KVMguest" default, which KVM has filled into short name field.
1852 strpadcpy((char *)sysib
.ext_names
[0],
1853 sizeof(sysib
.ext_names
[0]),
1854 qemu_name
?: "KVMguest", '\0');
1857 memcpy(sysib
.vm
[0].uuid
, &qemu_uuid
, sizeof(sysib
.vm
[0].uuid
));
1860 s390_cpu_pv_mem_write(cpu
, 0, &sysib
, sizeof(sysib
));
1862 s390_cpu_virt_mem_write(cpu
, addr
, ar
, &sysib
, sizeof(sysib
));
1866 static int handle_stsi(S390CPU
*cpu
)
1868 CPUState
*cs
= CPU(cpu
);
1869 struct kvm_run
*run
= cs
->kvm_run
;
1871 switch (run
->s390_stsi
.fc
) {
1873 if (run
->s390_stsi
.sel1
!= 2 || run
->s390_stsi
.sel2
!= 2) {
1876 insert_stsi_3_2_2(cpu
, run
->s390_stsi
.addr
, run
->s390_stsi
.ar
);
1879 insert_stsi_15_1_x(cpu
, run
->s390_stsi
.sel2
, run
->s390_stsi
.addr
,
1880 run
->s390_stsi
.ar
, RA_IGNORED
);
1887 static int kvm_arch_handle_debug_exit(S390CPU
*cpu
)
1889 CPUState
*cs
= CPU(cpu
);
1890 struct kvm_run
*run
= cs
->kvm_run
;
1893 struct kvm_debug_exit_arch
*arch_info
= &run
->debug
.arch
;
1895 switch (arch_info
->type
) {
1896 case KVM_HW_WP_WRITE
:
1897 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1898 cs
->watchpoint_hit
= &hw_watchpoint
;
1899 hw_watchpoint
.vaddr
= arch_info
->addr
;
1900 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1905 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1909 case KVM_SINGLESTEP
:
1910 if (cs
->singlestep_enabled
) {
1921 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
1923 S390CPU
*cpu
= S390_CPU(cs
);
1928 kvm_cpu_synchronize_state(cs
);
1930 switch (run
->exit_reason
) {
1931 case KVM_EXIT_S390_SIEIC
:
1932 ret
= handle_intercept(cpu
);
1934 case KVM_EXIT_S390_RESET
:
1935 s390_ipl_reset_request(cs
, S390_RESET_REIPL
);
1937 case KVM_EXIT_S390_TSCH
:
1938 ret
= handle_tsch(cpu
);
1940 case KVM_EXIT_S390_STSI
:
1941 ret
= handle_stsi(cpu
);
1943 case KVM_EXIT_DEBUG
:
1944 ret
= kvm_arch_handle_debug_exit(cpu
);
1947 fprintf(stderr
, "Unknown KVM exit: %d\n", run
->exit_reason
);
1953 ret
= EXCP_INTERRUPT
;
1958 bool kvm_arch_stop_on_emulation_error(CPUState
*cpu
)
1963 void kvm_s390_enable_css_support(S390CPU
*cpu
)
1967 /* Activate host kernel channel subsystem support. */
1968 r
= kvm_vcpu_enable_cap(CPU(cpu
), KVM_CAP_S390_CSS_SUPPORT
, 0);
1972 void kvm_arch_init_irq_routing(KVMState
*s
)
1975 * Note that while irqchip capabilities generally imply that cpustates
1976 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1977 * have to override the common code kvm_halt_in_kernel_allowed setting.
1979 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
1980 kvm_gsi_routing_allowed
= true;
1981 kvm_halt_in_kernel_allowed
= false;
1985 int kvm_s390_assign_subch_ioeventfd(EventNotifier
*notifier
, uint32_t sch
,
1986 int vq
, bool assign
)
1988 struct kvm_ioeventfd kick
= {
1989 .flags
= KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY
|
1990 KVM_IOEVENTFD_FLAG_DATAMATCH
,
1991 .fd
= event_notifier_get_fd(notifier
),
1996 trace_kvm_assign_subch_ioeventfd(kick
.fd
, kick
.addr
, assign
,
1998 if (!kvm_check_extension(kvm_state
, KVM_CAP_IOEVENTFD
)) {
2002 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
2004 return kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
2007 int kvm_s390_get_protected_dump(void)
2009 return cap_protected_dump
;
2012 int kvm_s390_get_ri(void)
2017 int kvm_s390_set_cpu_state(S390CPU
*cpu
, uint8_t cpu_state
)
2019 struct kvm_mp_state mp_state
= {};
2022 /* the kvm part might not have been initialized yet */
2023 if (CPU(cpu
)->kvm_state
== NULL
) {
2027 switch (cpu_state
) {
2028 case S390_CPU_STATE_STOPPED
:
2029 mp_state
.mp_state
= KVM_MP_STATE_STOPPED
;
2031 case S390_CPU_STATE_CHECK_STOP
:
2032 mp_state
.mp_state
= KVM_MP_STATE_CHECK_STOP
;
2034 case S390_CPU_STATE_OPERATING
:
2035 mp_state
.mp_state
= KVM_MP_STATE_OPERATING
;
2037 case S390_CPU_STATE_LOAD
:
2038 mp_state
.mp_state
= KVM_MP_STATE_LOAD
;
2041 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2046 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
2048 trace_kvm_failed_cpu_state_set(CPU(cpu
)->cpu_index
, cpu_state
,
2055 void kvm_s390_vcpu_interrupt_pre_save(S390CPU
*cpu
)
2057 unsigned int max_cpus
= MACHINE(qdev_get_machine())->smp
.max_cpus
;
2058 struct kvm_s390_irq_state irq_state
= {
2059 .buf
= (uint64_t) cpu
->irqstate
,
2060 .len
= VCPU_IRQ_BUF_SIZE(max_cpus
),
2062 CPUState
*cs
= CPU(cpu
);
2065 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_IRQ_STATE
)) {
2069 bytes
= kvm_vcpu_ioctl(cs
, KVM_S390_GET_IRQ_STATE
, &irq_state
);
2071 cpu
->irqstate_saved_size
= 0;
2072 error_report("Migration of interrupt state failed");
2076 cpu
->irqstate_saved_size
= bytes
;
2079 int kvm_s390_vcpu_interrupt_post_load(S390CPU
*cpu
)
2081 CPUState
*cs
= CPU(cpu
);
2082 struct kvm_s390_irq_state irq_state
= {
2083 .buf
= (uint64_t) cpu
->irqstate
,
2084 .len
= cpu
->irqstate_saved_size
,
2088 if (cpu
->irqstate_saved_size
== 0) {
2092 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_IRQ_STATE
)) {
2096 r
= kvm_vcpu_ioctl(cs
, KVM_S390_SET_IRQ_STATE
, &irq_state
);
2098 error_report("Setting interrupt state failed %d", r
);
2103 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
2104 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
2106 S390PCIBusDevice
*pbdev
;
2107 uint32_t vec
= data
& ZPCI_MSI_VEC_MASK
;
2110 trace_kvm_msi_route_fixup("no pci device");
2114 pbdev
= s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev
)->id
);
2116 trace_kvm_msi_route_fixup("no zpci device");
2120 route
->type
= KVM_IRQ_ROUTING_S390_ADAPTER
;
2122 route
->u
.adapter
.summary_addr
= pbdev
->routes
.adapter
.summary_addr
;
2123 route
->u
.adapter
.ind_addr
= pbdev
->routes
.adapter
.ind_addr
;
2124 route
->u
.adapter
.summary_offset
= pbdev
->routes
.adapter
.summary_offset
;
2125 route
->u
.adapter
.ind_offset
= pbdev
->routes
.adapter
.ind_offset
+ vec
;
2126 route
->u
.adapter
.adapter_id
= pbdev
->routes
.adapter
.adapter_id
;
2130 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
2131 int vector
, PCIDevice
*dev
)
2136 int kvm_arch_release_virq_post(int virq
)
2141 int kvm_arch_msi_data_to_gsi(uint32_t data
)
2146 static int query_cpu_subfunc(S390FeatBitmap features
)
2148 struct kvm_s390_vm_cpu_subfunc prop
= {};
2149 struct kvm_device_attr attr
= {
2150 .group
= KVM_S390_VM_CPU_MODEL
,
2151 .attr
= KVM_S390_VM_CPU_MACHINE_SUBFUNC
,
2152 .addr
= (uint64_t) &prop
,
2156 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2162 * We're going to add all subfunctions now, if the corresponding feature
2163 * is available that unlocks the query functions.
2165 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PLO
, prop
.plo
);
2166 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING
, features
)) {
2167 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PTFF
, prop
.ptff
);
2169 if (test_bit(S390_FEAT_MSA
, features
)) {
2170 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMAC
, prop
.kmac
);
2171 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMC
, prop
.kmc
);
2172 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KM
, prop
.km
);
2173 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KIMD
, prop
.kimd
);
2174 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KLMD
, prop
.klmd
);
2176 if (test_bit(S390_FEAT_MSA_EXT_3
, features
)) {
2177 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PCKMO
, prop
.pckmo
);
2179 if (test_bit(S390_FEAT_MSA_EXT_4
, features
)) {
2180 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMCTR
, prop
.kmctr
);
2181 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMF
, prop
.kmf
);
2182 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMO
, prop
.kmo
);
2183 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PCC
, prop
.pcc
);
2185 if (test_bit(S390_FEAT_MSA_EXT_5
, features
)) {
2186 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PPNO
, prop
.ppno
);
2188 if (test_bit(S390_FEAT_MSA_EXT_8
, features
)) {
2189 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMA
, prop
.kma
);
2191 if (test_bit(S390_FEAT_MSA_EXT_9
, features
)) {
2192 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KDSA
, prop
.kdsa
);
2194 if (test_bit(S390_FEAT_ESORT_BASE
, features
)) {
2195 s390_add_from_feat_block(features
, S390_FEAT_TYPE_SORTL
, prop
.sortl
);
2197 if (test_bit(S390_FEAT_DEFLATE_BASE
, features
)) {
2198 s390_add_from_feat_block(features
, S390_FEAT_TYPE_DFLTCC
, prop
.dfltcc
);
2203 static int configure_cpu_subfunc(const S390FeatBitmap features
)
2205 struct kvm_s390_vm_cpu_subfunc prop
= {};
2206 struct kvm_device_attr attr
= {
2207 .group
= KVM_S390_VM_CPU_MODEL
,
2208 .attr
= KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
,
2209 .addr
= (uint64_t) &prop
,
2212 if (!kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2213 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
)) {
2214 /* hardware support might be missing, IBC will handle most of this */
2218 s390_fill_feat_block(features
, S390_FEAT_TYPE_PLO
, prop
.plo
);
2219 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING
, features
)) {
2220 s390_fill_feat_block(features
, S390_FEAT_TYPE_PTFF
, prop
.ptff
);
2222 if (test_bit(S390_FEAT_MSA
, features
)) {
2223 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMAC
, prop
.kmac
);
2224 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMC
, prop
.kmc
);
2225 s390_fill_feat_block(features
, S390_FEAT_TYPE_KM
, prop
.km
);
2226 s390_fill_feat_block(features
, S390_FEAT_TYPE_KIMD
, prop
.kimd
);
2227 s390_fill_feat_block(features
, S390_FEAT_TYPE_KLMD
, prop
.klmd
);
2229 if (test_bit(S390_FEAT_MSA_EXT_3
, features
)) {
2230 s390_fill_feat_block(features
, S390_FEAT_TYPE_PCKMO
, prop
.pckmo
);
2232 if (test_bit(S390_FEAT_MSA_EXT_4
, features
)) {
2233 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMCTR
, prop
.kmctr
);
2234 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMF
, prop
.kmf
);
2235 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMO
, prop
.kmo
);
2236 s390_fill_feat_block(features
, S390_FEAT_TYPE_PCC
, prop
.pcc
);
2238 if (test_bit(S390_FEAT_MSA_EXT_5
, features
)) {
2239 s390_fill_feat_block(features
, S390_FEAT_TYPE_PPNO
, prop
.ppno
);
2241 if (test_bit(S390_FEAT_MSA_EXT_8
, features
)) {
2242 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMA
, prop
.kma
);
2244 if (test_bit(S390_FEAT_MSA_EXT_9
, features
)) {
2245 s390_fill_feat_block(features
, S390_FEAT_TYPE_KDSA
, prop
.kdsa
);
2247 if (test_bit(S390_FEAT_ESORT_BASE
, features
)) {
2248 s390_fill_feat_block(features
, S390_FEAT_TYPE_SORTL
, prop
.sortl
);
2250 if (test_bit(S390_FEAT_DEFLATE_BASE
, features
)) {
2251 s390_fill_feat_block(features
, S390_FEAT_TYPE_DFLTCC
, prop
.dfltcc
);
2253 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2256 static bool ap_available(void)
2258 return kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
,
2259 KVM_S390_VM_CRYPTO_ENABLE_APIE
);
2262 static bool ap_enabled(const S390FeatBitmap features
)
2264 return test_bit(S390_FEAT_AP
, features
);
2267 static bool uv_feat_supported(void)
2269 return kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2270 KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST
);
2273 static int query_uv_feat_guest(S390FeatBitmap features
)
2275 struct kvm_s390_vm_cpu_uv_feat prop
= {};
2276 struct kvm_device_attr attr
= {
2277 .group
= KVM_S390_VM_CPU_MODEL
,
2278 .attr
= KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST
,
2279 .addr
= (uint64_t) &prop
,
2283 /* AP support check is currently the only user of the UV feature test */
2284 if (!(uv_feat_supported() && ap_available())) {
2288 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2294 set_bit(S390_FEAT_UV_FEAT_AP
, features
);
2297 set_bit(S390_FEAT_UV_FEAT_AP_INTR
, features
);
2303 static int kvm_to_feat
[][2] = {
2304 { KVM_S390_VM_CPU_FEAT_ESOP
, S390_FEAT_ESOP
},
2305 { KVM_S390_VM_CPU_FEAT_SIEF2
, S390_FEAT_SIE_F2
},
2306 { KVM_S390_VM_CPU_FEAT_64BSCAO
, S390_FEAT_SIE_64BSCAO
},
2307 { KVM_S390_VM_CPU_FEAT_SIIF
, S390_FEAT_SIE_SIIF
},
2308 { KVM_S390_VM_CPU_FEAT_GPERE
, S390_FEAT_SIE_GPERE
},
2309 { KVM_S390_VM_CPU_FEAT_GSLS
, S390_FEAT_SIE_GSLS
},
2310 { KVM_S390_VM_CPU_FEAT_IB
, S390_FEAT_SIE_IB
},
2311 { KVM_S390_VM_CPU_FEAT_CEI
, S390_FEAT_SIE_CEI
},
2312 { KVM_S390_VM_CPU_FEAT_IBS
, S390_FEAT_SIE_IBS
},
2313 { KVM_S390_VM_CPU_FEAT_SKEY
, S390_FEAT_SIE_SKEY
},
2314 { KVM_S390_VM_CPU_FEAT_CMMA
, S390_FEAT_SIE_CMMA
},
2315 { KVM_S390_VM_CPU_FEAT_PFMFI
, S390_FEAT_SIE_PFMFI
},
2316 { KVM_S390_VM_CPU_FEAT_SIGPIF
, S390_FEAT_SIE_SIGPIF
},
2317 { KVM_S390_VM_CPU_FEAT_KSS
, S390_FEAT_SIE_KSS
},
2320 static int query_cpu_feat(S390FeatBitmap features
)
2322 struct kvm_s390_vm_cpu_feat prop
= {};
2323 struct kvm_device_attr attr
= {
2324 .group
= KVM_S390_VM_CPU_MODEL
,
2325 .attr
= KVM_S390_VM_CPU_MACHINE_FEAT
,
2326 .addr
= (uint64_t) &prop
,
2331 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2336 for (i
= 0; i
< ARRAY_SIZE(kvm_to_feat
); i
++) {
2337 if (test_be_bit(kvm_to_feat
[i
][0], (uint8_t *) prop
.feat
)) {
2338 set_bit(kvm_to_feat
[i
][1], features
);
2344 static int configure_cpu_feat(const S390FeatBitmap features
)
2346 struct kvm_s390_vm_cpu_feat prop
= {};
2347 struct kvm_device_attr attr
= {
2348 .group
= KVM_S390_VM_CPU_MODEL
,
2349 .attr
= KVM_S390_VM_CPU_PROCESSOR_FEAT
,
2350 .addr
= (uint64_t) &prop
,
2354 for (i
= 0; i
< ARRAY_SIZE(kvm_to_feat
); i
++) {
2355 if (test_bit(kvm_to_feat
[i
][1], features
)) {
2356 set_be_bit(kvm_to_feat
[i
][0], (uint8_t *) prop
.feat
);
2359 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2362 bool kvm_s390_cpu_models_supported(void)
2364 if (!cpu_model_allowed()) {
2365 /* compatibility machines interfere with the cpu model */
2368 return kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2369 KVM_S390_VM_CPU_MACHINE
) &&
2370 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2371 KVM_S390_VM_CPU_PROCESSOR
) &&
2372 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2373 KVM_S390_VM_CPU_MACHINE_FEAT
) &&
2374 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2375 KVM_S390_VM_CPU_PROCESSOR_FEAT
) &&
2376 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2377 KVM_S390_VM_CPU_MACHINE_SUBFUNC
);
2380 void kvm_s390_get_host_cpu_model(S390CPUModel
*model
, Error
**errp
)
2382 struct kvm_s390_vm_cpu_machine prop
= {};
2383 struct kvm_device_attr attr
= {
2384 .group
= KVM_S390_VM_CPU_MODEL
,
2385 .attr
= KVM_S390_VM_CPU_MACHINE
,
2386 .addr
= (uint64_t) &prop
,
2388 uint16_t unblocked_ibc
= 0, cpu_type
= 0;
2391 memset(model
, 0, sizeof(*model
));
2393 if (!kvm_s390_cpu_models_supported()) {
2394 error_setg(errp
, "KVM doesn't support CPU models");
2398 /* query the basic cpu model properties */
2399 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2401 error_setg(errp
, "KVM: Error querying host CPU model: %d", rc
);
2405 cpu_type
= cpuid_type(prop
.cpuid
);
2406 if (has_ibc(prop
.ibc
)) {
2407 model
->lowest_ibc
= lowest_ibc(prop
.ibc
);
2408 unblocked_ibc
= unblocked_ibc(prop
.ibc
);
2410 model
->cpu_id
= cpuid_id(prop
.cpuid
);
2411 model
->cpu_id_format
= cpuid_format(prop
.cpuid
);
2412 model
->cpu_ver
= 0xff;
2414 /* get supported cpu features indicated via STFL(E) */
2415 s390_add_from_feat_block(model
->features
, S390_FEAT_TYPE_STFL
,
2416 (uint8_t *) prop
.fac_mask
);
2417 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2418 if (test_bit(S390_FEAT_STFLE
, model
->features
)) {
2419 set_bit(S390_FEAT_DAT_ENH_2
, model
->features
);
2421 /* get supported cpu features indicated e.g. via SCLP */
2422 rc
= query_cpu_feat(model
->features
);
2424 error_setg(errp
, "KVM: Error querying CPU features: %d", rc
);
2427 /* get supported cpu subfunctions indicated via query / test bit */
2428 rc
= query_cpu_subfunc(model
->features
);
2430 error_setg(errp
, "KVM: Error querying CPU subfunctions: %d", rc
);
2434 /* PTFF subfunctions might be indicated although kernel support missing */
2435 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH
, model
->features
)) {
2436 clear_bit(S390_FEAT_PTFF_QSIE
, model
->features
);
2437 clear_bit(S390_FEAT_PTFF_QTOUE
, model
->features
);
2438 clear_bit(S390_FEAT_PTFF_STOE
, model
->features
);
2439 clear_bit(S390_FEAT_PTFF_STOUE
, model
->features
);
2442 /* with cpu model support, CMM is only indicated if really available */
2443 if (kvm_s390_cmma_available()) {
2444 set_bit(S390_FEAT_CMM
, model
->features
);
2446 /* no cmm -> no cmm nt */
2447 clear_bit(S390_FEAT_CMM_NT
, model
->features
);
2450 /* bpb needs kernel support for migration, VSIE and reset */
2451 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_BPB
)) {
2452 clear_bit(S390_FEAT_BPB
, model
->features
);
2456 * If we have support for protected virtualization, indicate
2457 * the protected virtualization IPL unpack facility.
2459 if (cap_protected
) {
2460 set_bit(S390_FEAT_UNPACK
, model
->features
);
2464 * If we have kernel support for CPU Topology indicate the
2465 * configuration-topology facility.
2467 if (kvm_check_extension(kvm_state
, KVM_CAP_S390_CPU_TOPOLOGY
)) {
2468 set_bit(S390_FEAT_CONFIGURATION_TOPOLOGY
, model
->features
);
2471 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2472 set_bit(S390_FEAT_ZPCI
, model
->features
);
2473 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION
, model
->features
);
2475 if (s390_known_cpu_type(cpu_type
)) {
2476 /* we want the exact model, even if some features are missing */
2477 model
->def
= s390_find_cpu_def(cpu_type
, ibc_gen(unblocked_ibc
),
2478 ibc_ec_ga(unblocked_ibc
), NULL
);
2480 /* model unknown, e.g. too new - search using features */
2481 model
->def
= s390_find_cpu_def(0, ibc_gen(unblocked_ibc
),
2482 ibc_ec_ga(unblocked_ibc
),
2486 error_setg(errp
, "KVM: host CPU model could not be identified");
2489 /* for now, we can only provide the AP feature with HW support */
2490 if (ap_available()) {
2491 set_bit(S390_FEAT_AP
, model
->features
);
2495 * Extended-Length SCCB is handled entirely within QEMU.
2496 * For PV guests this is completely fenced by the Ultravisor, as Service
2497 * Call error checking and STFLE interpretation are handled via SIE.
2499 set_bit(S390_FEAT_EXTENDED_LENGTH_SCCB
, model
->features
);
2501 if (kvm_check_extension(kvm_state
, KVM_CAP_S390_DIAG318
)) {
2502 set_bit(S390_FEAT_DIAG_318
, model
->features
);
2505 /* Test for Ultravisor features that influence secure guest behavior */
2506 query_uv_feat_guest(model
->features
);
2508 /* strip of features that are not part of the maximum model */
2509 bitmap_and(model
->features
, model
->features
, model
->def
->full_feat
,
2513 static int configure_uv_feat_guest(const S390FeatBitmap features
)
2515 struct kvm_s390_vm_cpu_uv_feat uv_feat
= {};
2516 struct kvm_device_attr attribute
= {
2517 .group
= KVM_S390_VM_CPU_MODEL
,
2518 .attr
= KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST
,
2519 .addr
= (__u64
) &uv_feat
,
2522 /* AP support check is currently the only user of the UV feature test */
2523 if (!(uv_feat_supported() && ap_enabled(features
))) {
2527 if (test_bit(S390_FEAT_UV_FEAT_AP
, features
)) {
2530 if (test_bit(S390_FEAT_UV_FEAT_AP_INTR
, features
)) {
2531 uv_feat
.ap_intr
= 1;
2534 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attribute
);
2537 static void kvm_s390_configure_apie(bool interpret
)
2539 uint64_t attr
= interpret
? KVM_S390_VM_CRYPTO_ENABLE_APIE
:
2540 KVM_S390_VM_CRYPTO_DISABLE_APIE
;
2542 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
2543 kvm_s390_set_crypto_attr(attr
);
2547 void kvm_s390_apply_cpu_model(const S390CPUModel
*model
, Error
**errp
)
2549 struct kvm_s390_vm_cpu_processor prop
= {
2552 struct kvm_device_attr attr
= {
2553 .group
= KVM_S390_VM_CPU_MODEL
,
2554 .attr
= KVM_S390_VM_CPU_PROCESSOR
,
2555 .addr
= (uint64_t) &prop
,
2560 /* compatibility handling if cpu models are disabled */
2561 if (kvm_s390_cmma_available()) {
2562 kvm_s390_enable_cmma();
2566 if (!kvm_s390_cpu_models_supported()) {
2567 error_setg(errp
, "KVM doesn't support CPU models");
2570 prop
.cpuid
= s390_cpuid_from_cpu_model(model
);
2571 prop
.ibc
= s390_ibc_from_cpu_model(model
);
2572 /* configure cpu features indicated via STFL(e) */
2573 s390_fill_feat_block(model
->features
, S390_FEAT_TYPE_STFL
,
2574 (uint8_t *) prop
.fac_list
);
2575 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2577 error_setg(errp
, "KVM: Error configuring the CPU model: %d", rc
);
2580 /* configure cpu features indicated e.g. via SCLP */
2581 rc
= configure_cpu_feat(model
->features
);
2583 error_setg(errp
, "KVM: Error configuring CPU features: %d", rc
);
2586 /* configure cpu subfunctions indicated via query / test bit */
2587 rc
= configure_cpu_subfunc(model
->features
);
2589 error_setg(errp
, "KVM: Error configuring CPU subfunctions: %d", rc
);
2592 /* enable CMM via CMMA */
2593 if (test_bit(S390_FEAT_CMM
, model
->features
)) {
2594 kvm_s390_enable_cmma();
2597 if (ap_enabled(model
->features
)) {
2598 kvm_s390_configure_apie(true);
2601 /* configure UV-features for the guest indicated via query / test_bit */
2602 rc
= configure_uv_feat_guest(model
->features
);
2604 error_setg(errp
, "KVM: Error configuring CPU UV features %d", rc
);
2609 void kvm_s390_restart_interrupt(S390CPU
*cpu
)
2611 struct kvm_s390_irq irq
= {
2612 .type
= KVM_S390_RESTART
,
2615 kvm_s390_vcpu_interrupt(cpu
, &irq
);
2618 void kvm_s390_stop_interrupt(S390CPU
*cpu
)
2620 struct kvm_s390_irq irq
= {
2621 .type
= KVM_S390_SIGP_STOP
,
2624 kvm_s390_vcpu_interrupt(cpu
, &irq
);
2627 bool kvm_arch_cpu_check_are_resettable(void)
2632 int kvm_s390_get_zpci_op(void)
2637 int kvm_s390_topology_set_mtcr(uint64_t attr
)
2639 struct kvm_device_attr attribute
= {
2640 .group
= KVM_S390_VM_CPU_TOPOLOGY
,
2644 if (!s390_has_feat(S390_FEAT_CONFIGURATION_TOPOLOGY
)) {
2647 if (!kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_TOPOLOGY
, attr
)) {
2651 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attribute
);
2654 void kvm_arch_accel_class_init(ObjectClass
*oc
)