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>
27 #include "qemu-common.h"
30 #include "kvm_s390x.h"
31 #include "qapi/error.h"
32 #include "qemu/error-report.h"
33 #include "qemu/timer.h"
34 #include "qemu/units.h"
35 #include "qemu/mmap-alloc.h"
37 #include "sysemu/sysemu.h"
38 #include "sysemu/hw_accel.h"
40 #include "sysemu/device_tree.h"
41 #include "exec/gdbstub.h"
42 #include "exec/ram_addr.h"
44 #include "hw/s390x/s390-pci-inst.h"
45 #include "hw/s390x/s390-pci-bus.h"
46 #include "hw/s390x/ipl.h"
47 #include "hw/s390x/ebcdic.h"
48 #include "exec/memattrs.h"
49 #include "hw/s390x/s390-virtio-ccw.h"
50 #include "hw/s390x/s390-virtio-hcall.h"
56 #define DPRINTF(fmt, ...) do { \
58 fprintf(stderr, fmt, ## __VA_ARGS__); \
62 #define kvm_vm_check_mem_attr(s, attr) \
63 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
65 #define IPA0_DIAG 0x8300
66 #define IPA0_SIGP 0xae00
67 #define IPA0_B2 0xb200
68 #define IPA0_B9 0xb900
69 #define IPA0_EB 0xeb00
70 #define IPA0_E3 0xe300
72 #define PRIV_B2_SCLP_CALL 0x20
73 #define PRIV_B2_CSCH 0x30
74 #define PRIV_B2_HSCH 0x31
75 #define PRIV_B2_MSCH 0x32
76 #define PRIV_B2_SSCH 0x33
77 #define PRIV_B2_STSCH 0x34
78 #define PRIV_B2_TSCH 0x35
79 #define PRIV_B2_TPI 0x36
80 #define PRIV_B2_SAL 0x37
81 #define PRIV_B2_RSCH 0x38
82 #define PRIV_B2_STCRW 0x39
83 #define PRIV_B2_STCPS 0x3a
84 #define PRIV_B2_RCHP 0x3b
85 #define PRIV_B2_SCHM 0x3c
86 #define PRIV_B2_CHSC 0x5f
87 #define PRIV_B2_SIGA 0x74
88 #define PRIV_B2_XSCH 0x76
90 #define PRIV_EB_SQBS 0x8a
91 #define PRIV_EB_PCISTB 0xd0
92 #define PRIV_EB_SIC 0xd1
94 #define PRIV_B9_EQBS 0x9c
95 #define PRIV_B9_CLP 0xa0
96 #define PRIV_B9_PCISTG 0xd0
97 #define PRIV_B9_PCILG 0xd2
98 #define PRIV_B9_RPCIT 0xd3
100 #define PRIV_E3_MPCIFC 0xd0
101 #define PRIV_E3_STPCIFC 0xd4
103 #define DIAG_TIMEREVENT 0x288
104 #define DIAG_IPL 0x308
105 #define DIAG_KVM_HYPERCALL 0x500
106 #define DIAG_KVM_BREAKPOINT 0x501
108 #define ICPT_INSTRUCTION 0x04
109 #define ICPT_PROGRAM 0x08
110 #define ICPT_EXT_INT 0x14
111 #define ICPT_WAITPSW 0x1c
112 #define ICPT_SOFT_INTERCEPT 0x24
113 #define ICPT_CPU_STOP 0x28
114 #define ICPT_OPEREXC 0x2c
117 #define NR_LOCAL_IRQS 32
119 * Needs to be big enough to contain max_cpus emergency signals
120 * and in addition NR_LOCAL_IRQS interrupts
122 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
123 (max_cpus + NR_LOCAL_IRQS))
125 static CPUWatchpoint hw_watchpoint
;
127 * We don't use a list because this structure is also used to transmit the
128 * hardware breakpoints to the kernel.
130 static struct kvm_hw_breakpoint
*hw_breakpoints
;
131 static int nb_hw_breakpoints
;
133 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
137 static int cap_sync_regs
;
138 static int cap_async_pf
;
139 static int cap_mem_op
;
140 static int cap_s390_irq
;
143 static int cap_hpage_1m
;
145 static int active_cmma
;
147 static void *legacy_s390_alloc(size_t size
, uint64_t *align
, bool shared
);
149 static int kvm_s390_query_mem_limit(uint64_t *memory_limit
)
151 struct kvm_device_attr attr
= {
152 .group
= KVM_S390_VM_MEM_CTRL
,
153 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
154 .addr
= (uint64_t) memory_limit
,
157 return kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
160 int kvm_s390_set_mem_limit(uint64_t new_limit
, uint64_t *hw_limit
)
164 struct kvm_device_attr attr
= {
165 .group
= KVM_S390_VM_MEM_CTRL
,
166 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
167 .addr
= (uint64_t) &new_limit
,
170 if (!kvm_vm_check_mem_attr(kvm_state
, KVM_S390_VM_MEM_LIMIT_SIZE
)) {
174 rc
= kvm_s390_query_mem_limit(hw_limit
);
177 } else if (*hw_limit
< new_limit
) {
181 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
184 int kvm_s390_cmma_active(void)
189 static bool kvm_s390_cmma_available(void)
191 static bool initialized
, value
;
195 value
= kvm_vm_check_mem_attr(kvm_state
, KVM_S390_VM_MEM_ENABLE_CMMA
) &&
196 kvm_vm_check_mem_attr(kvm_state
, KVM_S390_VM_MEM_CLR_CMMA
);
201 void kvm_s390_cmma_reset(void)
204 struct kvm_device_attr attr
= {
205 .group
= KVM_S390_VM_MEM_CTRL
,
206 .attr
= KVM_S390_VM_MEM_CLR_CMMA
,
209 if (!kvm_s390_cmma_active()) {
213 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
214 trace_kvm_clear_cmma(rc
);
217 static void kvm_s390_enable_cmma(void)
220 struct kvm_device_attr attr
= {
221 .group
= KVM_S390_VM_MEM_CTRL
,
222 .attr
= KVM_S390_VM_MEM_ENABLE_CMMA
,
226 warn_report("CMM will not be enabled because it is not "
227 "compatible with huge memory backings.");
230 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
232 trace_kvm_enable_cmma(rc
);
235 static void kvm_s390_set_attr(uint64_t attr
)
237 struct kvm_device_attr attribute
= {
238 .group
= KVM_S390_VM_CRYPTO
,
242 int ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attribute
);
245 error_report("Failed to set crypto device attribute %lu: %s",
246 attr
, strerror(-ret
));
250 static void kvm_s390_init_aes_kw(void)
252 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_AES_KW
;
254 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
256 attr
= KVM_S390_VM_CRYPTO_ENABLE_AES_KW
;
259 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
260 kvm_s390_set_attr(attr
);
264 static void kvm_s390_init_dea_kw(void)
266 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
;
268 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
270 attr
= KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
;
273 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
274 kvm_s390_set_attr(attr
);
278 void kvm_s390_crypto_reset(void)
280 if (s390_has_feat(S390_FEAT_MSA_EXT_3
)) {
281 kvm_s390_init_aes_kw();
282 kvm_s390_init_dea_kw();
286 static int kvm_s390_configure_mempath_backing(KVMState
*s
)
288 size_t path_psize
= qemu_getrampagesize();
290 if (path_psize
== 4 * KiB
) {
294 if (!hpage_1m_allowed()) {
295 error_report("This QEMU machine does not support huge page "
300 if (path_psize
!= 1 * MiB
) {
301 error_report("Memory backing with 2G pages was specified, "
302 "but KVM does not support this memory backing");
306 if (kvm_vm_enable_cap(s
, KVM_CAP_S390_HPAGE_1M
, 0)) {
307 error_report("Memory backing with 1M pages was specified, "
308 "but KVM does not support this memory backing");
316 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
318 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
320 if (kvm_s390_configure_mempath_backing(s
)) {
324 mc
->default_cpu_type
= S390_CPU_TYPE_NAME("host");
325 cap_sync_regs
= kvm_check_extension(s
, KVM_CAP_SYNC_REGS
);
326 cap_async_pf
= kvm_check_extension(s
, KVM_CAP_ASYNC_PF
);
327 cap_mem_op
= kvm_check_extension(s
, KVM_CAP_S390_MEM_OP
);
328 cap_s390_irq
= kvm_check_extension(s
, KVM_CAP_S390_INJECT_IRQ
);
330 if (!kvm_check_extension(s
, KVM_CAP_S390_GMAP
)
331 || !kvm_check_extension(s
, KVM_CAP_S390_COW
)) {
332 phys_mem_set_alloc(legacy_s390_alloc
);
335 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_SIGP
, 0);
336 kvm_vm_enable_cap(s
, KVM_CAP_S390_VECTOR_REGISTERS
, 0);
337 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_STSI
, 0);
339 if (kvm_vm_enable_cap(s
, KVM_CAP_S390_RI
, 0) == 0) {
343 if (cpu_model_allowed()) {
344 if (kvm_vm_enable_cap(s
, KVM_CAP_S390_GS
, 0) == 0) {
350 * The migration interface for ais was introduced with kernel 4.13
351 * but the capability itself had been active since 4.12. As migration
352 * support is considered necessary let's disable ais in the 2.10
355 /* kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0); */
360 int kvm_arch_irqchip_create(MachineState
*ms
, KVMState
*s
)
365 unsigned long kvm_arch_vcpu_id(CPUState
*cpu
)
367 return cpu
->cpu_index
;
370 int kvm_arch_init_vcpu(CPUState
*cs
)
372 S390CPU
*cpu
= S390_CPU(cs
);
373 kvm_s390_set_cpu_state(cpu
, cpu
->env
.cpu_state
);
374 cpu
->irqstate
= g_malloc0(VCPU_IRQ_BUF_SIZE
);
378 void kvm_s390_reset_vcpu(S390CPU
*cpu
)
380 CPUState
*cs
= CPU(cpu
);
382 /* The initial reset call is needed here to reset in-kernel
383 * vcpu data that we can't access directly from QEMU
384 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
385 * Before this ioctl cpu_synchronize_state() is called in common kvm
387 if (kvm_vcpu_ioctl(cs
, KVM_S390_INITIAL_RESET
, NULL
)) {
388 error_report("Initial CPU reset failed on CPU %i", cs
->cpu_index
);
392 static int can_sync_regs(CPUState
*cs
, int regs
)
394 return cap_sync_regs
&& (cs
->kvm_run
->kvm_valid_regs
& regs
) == regs
;
397 int kvm_arch_put_registers(CPUState
*cs
, int level
)
399 S390CPU
*cpu
= S390_CPU(cs
);
400 CPUS390XState
*env
= &cpu
->env
;
401 struct kvm_sregs sregs
;
402 struct kvm_regs regs
;
403 struct kvm_fpu fpu
= {};
407 /* always save the PSW and the GPRS*/
408 cs
->kvm_run
->psw_addr
= env
->psw
.addr
;
409 cs
->kvm_run
->psw_mask
= env
->psw
.mask
;
411 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
412 for (i
= 0; i
< 16; i
++) {
413 cs
->kvm_run
->s
.regs
.gprs
[i
] = env
->regs
[i
];
414 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_GPRS
;
417 for (i
= 0; i
< 16; i
++) {
418 regs
.gprs
[i
] = env
->regs
[i
];
420 r
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
426 if (can_sync_regs(cs
, KVM_SYNC_VRS
)) {
427 for (i
= 0; i
< 32; i
++) {
428 cs
->kvm_run
->s
.regs
.vrs
[i
][0] = env
->vregs
[i
][0].ll
;
429 cs
->kvm_run
->s
.regs
.vrs
[i
][1] = env
->vregs
[i
][1].ll
;
431 cs
->kvm_run
->s
.regs
.fpc
= env
->fpc
;
432 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_VRS
;
433 } else if (can_sync_regs(cs
, KVM_SYNC_FPRS
)) {
434 for (i
= 0; i
< 16; i
++) {
435 cs
->kvm_run
->s
.regs
.fprs
[i
] = get_freg(env
, i
)->ll
;
437 cs
->kvm_run
->s
.regs
.fpc
= env
->fpc
;
438 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_FPRS
;
441 for (i
= 0; i
< 16; i
++) {
442 fpu
.fprs
[i
] = get_freg(env
, i
)->ll
;
446 r
= kvm_vcpu_ioctl(cs
, KVM_SET_FPU
, &fpu
);
452 /* Do we need to save more than that? */
453 if (level
== KVM_PUT_RUNTIME_STATE
) {
457 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
458 cs
->kvm_run
->s
.regs
.cputm
= env
->cputm
;
459 cs
->kvm_run
->s
.regs
.ckc
= env
->ckc
;
460 cs
->kvm_run
->s
.regs
.todpr
= env
->todpr
;
461 cs
->kvm_run
->s
.regs
.gbea
= env
->gbea
;
462 cs
->kvm_run
->s
.regs
.pp
= env
->pp
;
463 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ARCH0
;
466 * These ONE_REGS are not protected by a capability. As they are only
467 * necessary for migration we just trace a possible error, but don't
468 * return with an error return code.
470 kvm_set_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
471 kvm_set_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
472 kvm_set_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
473 kvm_set_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
474 kvm_set_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
477 if (can_sync_regs(cs
, KVM_SYNC_RICCB
)) {
478 memcpy(cs
->kvm_run
->s
.regs
.riccb
, env
->riccb
, 64);
479 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_RICCB
;
482 /* pfault parameters */
483 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
484 cs
->kvm_run
->s
.regs
.pft
= env
->pfault_token
;
485 cs
->kvm_run
->s
.regs
.pfs
= env
->pfault_select
;
486 cs
->kvm_run
->s
.regs
.pfc
= env
->pfault_compare
;
487 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PFAULT
;
488 } else if (cap_async_pf
) {
489 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
493 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
497 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
503 /* access registers and control registers*/
504 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
505 for (i
= 0; i
< 16; i
++) {
506 cs
->kvm_run
->s
.regs
.acrs
[i
] = env
->aregs
[i
];
507 cs
->kvm_run
->s
.regs
.crs
[i
] = env
->cregs
[i
];
509 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ACRS
;
510 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_CRS
;
512 for (i
= 0; i
< 16; i
++) {
513 sregs
.acrs
[i
] = env
->aregs
[i
];
514 sregs
.crs
[i
] = env
->cregs
[i
];
516 r
= kvm_vcpu_ioctl(cs
, KVM_SET_SREGS
, &sregs
);
522 if (can_sync_regs(cs
, KVM_SYNC_GSCB
)) {
523 memcpy(cs
->kvm_run
->s
.regs
.gscb
, env
->gscb
, 32);
524 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_GSCB
;
527 if (can_sync_regs(cs
, KVM_SYNC_BPBC
)) {
528 cs
->kvm_run
->s
.regs
.bpbc
= env
->bpbc
;
529 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_BPBC
;
532 if (can_sync_regs(cs
, KVM_SYNC_ETOKEN
)) {
533 cs
->kvm_run
->s
.regs
.etoken
= env
->etoken
;
534 cs
->kvm_run
->s
.regs
.etoken_extension
= env
->etoken_extension
;
535 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ETOKEN
;
538 /* Finally the prefix */
539 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
540 cs
->kvm_run
->s
.regs
.prefix
= env
->psa
;
541 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PREFIX
;
543 /* prefix is only supported via sync regs */
548 int kvm_arch_get_registers(CPUState
*cs
)
550 S390CPU
*cpu
= S390_CPU(cs
);
551 CPUS390XState
*env
= &cpu
->env
;
552 struct kvm_sregs sregs
;
553 struct kvm_regs regs
;
558 env
->psw
.addr
= cs
->kvm_run
->psw_addr
;
559 env
->psw
.mask
= cs
->kvm_run
->psw_mask
;
562 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
563 for (i
= 0; i
< 16; i
++) {
564 env
->regs
[i
] = cs
->kvm_run
->s
.regs
.gprs
[i
];
567 r
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
571 for (i
= 0; i
< 16; i
++) {
572 env
->regs
[i
] = regs
.gprs
[i
];
576 /* The ACRS and CRS */
577 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
578 for (i
= 0; i
< 16; i
++) {
579 env
->aregs
[i
] = cs
->kvm_run
->s
.regs
.acrs
[i
];
580 env
->cregs
[i
] = cs
->kvm_run
->s
.regs
.crs
[i
];
583 r
= kvm_vcpu_ioctl(cs
, KVM_GET_SREGS
, &sregs
);
587 for (i
= 0; i
< 16; i
++) {
588 env
->aregs
[i
] = sregs
.acrs
[i
];
589 env
->cregs
[i
] = sregs
.crs
[i
];
593 /* Floating point and vector registers */
594 if (can_sync_regs(cs
, KVM_SYNC_VRS
)) {
595 for (i
= 0; i
< 32; i
++) {
596 env
->vregs
[i
][0].ll
= cs
->kvm_run
->s
.regs
.vrs
[i
][0];
597 env
->vregs
[i
][1].ll
= cs
->kvm_run
->s
.regs
.vrs
[i
][1];
599 env
->fpc
= cs
->kvm_run
->s
.regs
.fpc
;
600 } else if (can_sync_regs(cs
, KVM_SYNC_FPRS
)) {
601 for (i
= 0; i
< 16; i
++) {
602 get_freg(env
, i
)->ll
= cs
->kvm_run
->s
.regs
.fprs
[i
];
604 env
->fpc
= cs
->kvm_run
->s
.regs
.fpc
;
606 r
= kvm_vcpu_ioctl(cs
, KVM_GET_FPU
, &fpu
);
610 for (i
= 0; i
< 16; i
++) {
611 get_freg(env
, i
)->ll
= fpu
.fprs
[i
];
617 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
618 env
->psa
= cs
->kvm_run
->s
.regs
.prefix
;
621 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
622 env
->cputm
= cs
->kvm_run
->s
.regs
.cputm
;
623 env
->ckc
= cs
->kvm_run
->s
.regs
.ckc
;
624 env
->todpr
= cs
->kvm_run
->s
.regs
.todpr
;
625 env
->gbea
= cs
->kvm_run
->s
.regs
.gbea
;
626 env
->pp
= cs
->kvm_run
->s
.regs
.pp
;
629 * These ONE_REGS are not protected by a capability. As they are only
630 * necessary for migration we just trace a possible error, but don't
631 * return with an error return code.
633 kvm_get_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
634 kvm_get_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
635 kvm_get_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
636 kvm_get_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
637 kvm_get_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
640 if (can_sync_regs(cs
, KVM_SYNC_RICCB
)) {
641 memcpy(env
->riccb
, cs
->kvm_run
->s
.regs
.riccb
, 64);
644 if (can_sync_regs(cs
, KVM_SYNC_GSCB
)) {
645 memcpy(env
->gscb
, cs
->kvm_run
->s
.regs
.gscb
, 32);
648 if (can_sync_regs(cs
, KVM_SYNC_BPBC
)) {
649 env
->bpbc
= cs
->kvm_run
->s
.regs
.bpbc
;
652 if (can_sync_regs(cs
, KVM_SYNC_ETOKEN
)) {
653 env
->etoken
= cs
->kvm_run
->s
.regs
.etoken
;
654 env
->etoken_extension
= cs
->kvm_run
->s
.regs
.etoken_extension
;
657 /* pfault parameters */
658 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
659 env
->pfault_token
= cs
->kvm_run
->s
.regs
.pft
;
660 env
->pfault_select
= cs
->kvm_run
->s
.regs
.pfs
;
661 env
->pfault_compare
= cs
->kvm_run
->s
.regs
.pfc
;
662 } else if (cap_async_pf
) {
663 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
667 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
671 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
680 int kvm_s390_get_clock(uint8_t *tod_high
, uint64_t *tod_low
)
683 struct kvm_device_attr attr
= {
684 .group
= KVM_S390_VM_TOD
,
685 .attr
= KVM_S390_VM_TOD_LOW
,
686 .addr
= (uint64_t)tod_low
,
689 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
694 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
695 attr
.addr
= (uint64_t)tod_high
;
696 return kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
699 int kvm_s390_get_clock_ext(uint8_t *tod_high
, uint64_t *tod_low
)
702 struct kvm_s390_vm_tod_clock gtod
;
703 struct kvm_device_attr attr
= {
704 .group
= KVM_S390_VM_TOD
,
705 .attr
= KVM_S390_VM_TOD_EXT
,
706 .addr
= (uint64_t)>od
,
709 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
710 *tod_high
= gtod
.epoch_idx
;
716 int kvm_s390_set_clock(uint8_t tod_high
, uint64_t tod_low
)
719 struct kvm_device_attr attr
= {
720 .group
= KVM_S390_VM_TOD
,
721 .attr
= KVM_S390_VM_TOD_LOW
,
722 .addr
= (uint64_t)&tod_low
,
725 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
730 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
731 attr
.addr
= (uint64_t)&tod_high
;
732 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
735 int kvm_s390_set_clock_ext(uint8_t tod_high
, uint64_t tod_low
)
737 struct kvm_s390_vm_tod_clock gtod
= {
738 .epoch_idx
= tod_high
,
741 struct kvm_device_attr attr
= {
742 .group
= KVM_S390_VM_TOD
,
743 .attr
= KVM_S390_VM_TOD_EXT
,
744 .addr
= (uint64_t)>od
,
747 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
752 * @addr: the logical start address in guest memory
753 * @ar: the access register number
754 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
755 * @len: length that should be transferred
756 * @is_write: true = write, false = read
757 * Returns: 0 on success, non-zero if an exception or error occurred
759 * Use KVM ioctl to read/write from/to guest memory. An access exception
760 * is injected into the vCPU in case of translation errors.
762 int kvm_s390_mem_op(S390CPU
*cpu
, vaddr addr
, uint8_t ar
, void *hostbuf
,
763 int len
, bool is_write
)
765 struct kvm_s390_mem_op mem_op
= {
767 .flags
= KVM_S390_MEMOP_F_INJECT_EXCEPTION
,
769 .op
= is_write
? KVM_S390_MEMOP_LOGICAL_WRITE
770 : KVM_S390_MEMOP_LOGICAL_READ
,
771 .buf
= (uint64_t)hostbuf
,
780 mem_op
.flags
|= KVM_S390_MEMOP_F_CHECK_ONLY
;
783 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_S390_MEM_OP
, &mem_op
);
785 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret
));
791 * Legacy layout for s390:
792 * Older S390 KVM requires the topmost vma of the RAM to be
793 * smaller than an system defined value, which is at least 256GB.
794 * Larger systems have larger values. We put the guest between
795 * the end of data segment (system break) and this value. We
796 * use 32GB as a base to have enough room for the system break
797 * to grow. We also have to use MAP parameters that avoid
798 * read-only mapping of guest pages.
800 static void *legacy_s390_alloc(size_t size
, uint64_t *align
, bool shared
)
805 /* we only support one allocation, which is enough for initial ram */
809 mem
= mmap((void *) 0x800000000ULL
, size
,
810 PROT_EXEC
|PROT_READ
|PROT_WRITE
,
811 MAP_SHARED
| MAP_ANONYMOUS
| MAP_FIXED
, -1, 0);
812 if (mem
== MAP_FAILED
) {
816 *align
= QEMU_VMALLOC_ALIGN
;
821 static uint8_t const *sw_bp_inst
;
822 static uint8_t sw_bp_ilen
;
824 static void determine_sw_breakpoint_instr(void)
826 /* DIAG 501 is used for sw breakpoints with old kernels */
827 static const uint8_t diag_501
[] = {0x83, 0x24, 0x05, 0x01};
828 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
829 static const uint8_t instr_0x0000
[] = {0x00, 0x00};
834 if (kvm_vm_enable_cap(kvm_state
, KVM_CAP_S390_USER_INSTR0
, 0)) {
835 sw_bp_inst
= diag_501
;
836 sw_bp_ilen
= sizeof(diag_501
);
837 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
839 sw_bp_inst
= instr_0x0000
;
840 sw_bp_ilen
= sizeof(instr_0x0000
);
841 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
845 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
847 determine_sw_breakpoint_instr();
849 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
851 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)sw_bp_inst
, sw_bp_ilen
, 1)) {
857 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
861 if (cpu_memory_rw_debug(cs
, bp
->pc
, t
, sw_bp_ilen
, 0)) {
863 } else if (memcmp(t
, sw_bp_inst
, sw_bp_ilen
)) {
865 } else if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
873 static struct kvm_hw_breakpoint
*find_hw_breakpoint(target_ulong addr
,
878 for (n
= 0; n
< nb_hw_breakpoints
; n
++) {
879 if (hw_breakpoints
[n
].addr
== addr
&& hw_breakpoints
[n
].type
== type
&&
880 (hw_breakpoints
[n
].len
== len
|| len
== -1)) {
881 return &hw_breakpoints
[n
];
888 static int insert_hw_breakpoint(target_ulong addr
, int len
, int type
)
892 if (find_hw_breakpoint(addr
, len
, type
)) {
896 size
= (nb_hw_breakpoints
+ 1) * sizeof(struct kvm_hw_breakpoint
);
898 if (!hw_breakpoints
) {
899 nb_hw_breakpoints
= 0;
900 hw_breakpoints
= (struct kvm_hw_breakpoint
*)g_try_malloc(size
);
903 (struct kvm_hw_breakpoint
*)g_try_realloc(hw_breakpoints
, size
);
906 if (!hw_breakpoints
) {
907 nb_hw_breakpoints
= 0;
911 hw_breakpoints
[nb_hw_breakpoints
].addr
= addr
;
912 hw_breakpoints
[nb_hw_breakpoints
].len
= len
;
913 hw_breakpoints
[nb_hw_breakpoints
].type
= type
;
920 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
921 target_ulong len
, int type
)
924 case GDB_BREAKPOINT_HW
:
927 case GDB_WATCHPOINT_WRITE
:
931 type
= KVM_HW_WP_WRITE
;
936 return insert_hw_breakpoint(addr
, len
, type
);
939 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
940 target_ulong len
, int type
)
943 struct kvm_hw_breakpoint
*bp
= find_hw_breakpoint(addr
, len
, type
);
950 if (nb_hw_breakpoints
> 0) {
952 * In order to trim the array, move the last element to the position to
953 * be removed - if necessary.
955 if (bp
!= &hw_breakpoints
[nb_hw_breakpoints
]) {
956 *bp
= hw_breakpoints
[nb_hw_breakpoints
];
958 size
= nb_hw_breakpoints
* sizeof(struct kvm_hw_breakpoint
);
960 (struct kvm_hw_breakpoint
*)g_realloc(hw_breakpoints
, size
);
962 g_free(hw_breakpoints
);
963 hw_breakpoints
= NULL
;
969 void kvm_arch_remove_all_hw_breakpoints(void)
971 nb_hw_breakpoints
= 0;
972 g_free(hw_breakpoints
);
973 hw_breakpoints
= NULL
;
976 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
980 if (nb_hw_breakpoints
> 0) {
981 dbg
->arch
.nr_hw_bp
= nb_hw_breakpoints
;
982 dbg
->arch
.hw_bp
= hw_breakpoints
;
984 for (i
= 0; i
< nb_hw_breakpoints
; ++i
) {
985 hw_breakpoints
[i
].phys_addr
= s390_cpu_get_phys_addr_debug(cpu
,
986 hw_breakpoints
[i
].addr
);
988 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
990 dbg
->arch
.nr_hw_bp
= 0;
991 dbg
->arch
.hw_bp
= NULL
;
995 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
999 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
1001 return MEMTXATTRS_UNSPECIFIED
;
1004 int kvm_arch_process_async_events(CPUState
*cs
)
1009 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq
*irq
,
1010 struct kvm_s390_interrupt
*interrupt
)
1014 interrupt
->type
= irq
->type
;
1015 switch (irq
->type
) {
1016 case KVM_S390_INT_VIRTIO
:
1017 interrupt
->parm
= irq
->u
.ext
.ext_params
;
1019 case KVM_S390_INT_PFAULT_INIT
:
1020 case KVM_S390_INT_PFAULT_DONE
:
1021 interrupt
->parm64
= irq
->u
.ext
.ext_params2
;
1023 case KVM_S390_PROGRAM_INT
:
1024 interrupt
->parm
= irq
->u
.pgm
.code
;
1026 case KVM_S390_SIGP_SET_PREFIX
:
1027 interrupt
->parm
= irq
->u
.prefix
.address
;
1029 case KVM_S390_INT_SERVICE
:
1030 interrupt
->parm
= irq
->u
.ext
.ext_params
;
1033 interrupt
->parm
= irq
->u
.mchk
.cr14
;
1034 interrupt
->parm64
= irq
->u
.mchk
.mcic
;
1036 case KVM_S390_INT_EXTERNAL_CALL
:
1037 interrupt
->parm
= irq
->u
.extcall
.code
;
1039 case KVM_S390_INT_EMERGENCY
:
1040 interrupt
->parm
= irq
->u
.emerg
.code
;
1042 case KVM_S390_SIGP_STOP
:
1043 case KVM_S390_RESTART
:
1044 break; /* These types have no parameters */
1045 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1046 interrupt
->parm
= irq
->u
.io
.subchannel_id
<< 16;
1047 interrupt
->parm
|= irq
->u
.io
.subchannel_nr
;
1048 interrupt
->parm64
= (uint64_t)irq
->u
.io
.io_int_parm
<< 32;
1049 interrupt
->parm64
|= irq
->u
.io
.io_int_word
;
1058 static void inject_vcpu_irq_legacy(CPUState
*cs
, struct kvm_s390_irq
*irq
)
1060 struct kvm_s390_interrupt kvmint
= {};
1063 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
1065 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
1069 r
= kvm_vcpu_ioctl(cs
, KVM_S390_INTERRUPT
, &kvmint
);
1071 fprintf(stderr
, "KVM failed to inject interrupt\n");
1076 void kvm_s390_vcpu_interrupt(S390CPU
*cpu
, struct kvm_s390_irq
*irq
)
1078 CPUState
*cs
= CPU(cpu
);
1082 r
= kvm_vcpu_ioctl(cs
, KVM_S390_IRQ
, irq
);
1086 error_report("KVM failed to inject interrupt %llx", irq
->type
);
1090 inject_vcpu_irq_legacy(cs
, irq
);
1093 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq
*irq
)
1095 struct kvm_s390_interrupt kvmint
= {};
1098 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
1100 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
1104 r
= kvm_vm_ioctl(kvm_state
, KVM_S390_INTERRUPT
, &kvmint
);
1106 fprintf(stderr
, "KVM failed to inject interrupt\n");
1111 void kvm_s390_program_interrupt(S390CPU
*cpu
, uint16_t code
)
1113 struct kvm_s390_irq irq
= {
1114 .type
= KVM_S390_PROGRAM_INT
,
1117 qemu_log_mask(CPU_LOG_INT
, "program interrupt at %#" PRIx64
"\n",
1119 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1122 void kvm_s390_access_exception(S390CPU
*cpu
, uint16_t code
, uint64_t te_code
)
1124 struct kvm_s390_irq irq
= {
1125 .type
= KVM_S390_PROGRAM_INT
,
1127 .u
.pgm
.trans_exc_code
= te_code
,
1128 .u
.pgm
.exc_access_id
= te_code
& 3,
1131 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1134 static int kvm_sclp_service_call(S390CPU
*cpu
, struct kvm_run
*run
,
1137 CPUS390XState
*env
= &cpu
->env
;
1142 sccb
= env
->regs
[ipbh0
& 0xf];
1143 code
= env
->regs
[(ipbh0
& 0xf0) >> 4];
1145 r
= sclp_service_call(env
, sccb
, code
);
1147 kvm_s390_program_interrupt(cpu
, -r
);
1155 static int handle_b2(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1157 CPUS390XState
*env
= &cpu
->env
;
1159 uint16_t ipbh0
= (run
->s390_sieic
.ipb
& 0xffff0000) >> 16;
1163 ioinst_handle_xsch(cpu
, env
->regs
[1], RA_IGNORED
);
1166 ioinst_handle_csch(cpu
, env
->regs
[1], RA_IGNORED
);
1169 ioinst_handle_hsch(cpu
, env
->regs
[1], RA_IGNORED
);
1172 ioinst_handle_msch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1175 ioinst_handle_ssch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1178 ioinst_handle_stcrw(cpu
, run
->s390_sieic
.ipb
, RA_IGNORED
);
1181 ioinst_handle_stsch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1184 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1185 fprintf(stderr
, "Spurious tsch intercept\n");
1188 ioinst_handle_chsc(cpu
, run
->s390_sieic
.ipb
, RA_IGNORED
);
1191 /* This should have been handled by kvm already. */
1192 fprintf(stderr
, "Spurious tpi intercept\n");
1195 ioinst_handle_schm(cpu
, env
->regs
[1], env
->regs
[2],
1196 run
->s390_sieic
.ipb
, RA_IGNORED
);
1199 ioinst_handle_rsch(cpu
, env
->regs
[1], RA_IGNORED
);
1202 ioinst_handle_rchp(cpu
, env
->regs
[1], RA_IGNORED
);
1205 /* We do not provide this instruction, it is suppressed. */
1208 ioinst_handle_sal(cpu
, env
->regs
[1], RA_IGNORED
);
1211 /* Not provided, set CC = 3 for subchannel not operational */
1214 case PRIV_B2_SCLP_CALL
:
1215 rc
= kvm_sclp_service_call(cpu
, run
, ipbh0
);
1219 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1
);
1226 static uint64_t get_base_disp_rxy(S390CPU
*cpu
, struct kvm_run
*run
,
1229 CPUS390XState
*env
= &cpu
->env
;
1230 uint32_t x2
= (run
->s390_sieic
.ipa
& 0x000f);
1231 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1232 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1233 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1235 if (disp2
& 0x80000) {
1236 disp2
+= 0xfff00000;
1242 return (base2
? env
->regs
[base2
] : 0) +
1243 (x2
? env
->regs
[x2
] : 0) + (long)(int)disp2
;
1246 static uint64_t get_base_disp_rsy(S390CPU
*cpu
, struct kvm_run
*run
,
1249 CPUS390XState
*env
= &cpu
->env
;
1250 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1251 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1252 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1254 if (disp2
& 0x80000) {
1255 disp2
+= 0xfff00000;
1261 return (base2
? env
->regs
[base2
] : 0) + (long)(int)disp2
;
1264 static int kvm_clp_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1266 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1268 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1269 return clp_service_call(cpu
, r2
, RA_IGNORED
);
1275 static int kvm_pcilg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1277 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1278 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1280 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1281 return pcilg_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1287 static int kvm_pcistg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1289 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1290 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1292 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1293 return pcistg_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1299 static int kvm_stpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1301 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1305 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1306 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1308 return stpcifc_service_call(cpu
, r1
, fiba
, ar
, RA_IGNORED
);
1314 static int kvm_sic_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1316 CPUS390XState
*env
= &cpu
->env
;
1317 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1318 uint8_t r3
= run
->s390_sieic
.ipa
& 0x000f;
1323 mode
= env
->regs
[r1
] & 0xffff;
1324 isc
= (env
->regs
[r3
] >> 27) & 0x7;
1325 r
= css_do_sic(env
, isc
, mode
);
1327 kvm_s390_program_interrupt(cpu
, -r
);
1333 static int kvm_rpcit_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1335 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1336 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1338 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1339 return rpcit_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1345 static int kvm_pcistb_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1347 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1348 uint8_t r3
= run
->s390_sieic
.ipa
& 0x000f;
1352 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1353 gaddr
= get_base_disp_rsy(cpu
, run
, &ar
);
1355 return pcistb_service_call(cpu
, r1
, r3
, gaddr
, ar
, RA_IGNORED
);
1361 static int kvm_mpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1363 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1367 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1368 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1370 return mpcifc_service_call(cpu
, r1
, fiba
, ar
, RA_IGNORED
);
1376 static int handle_b9(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1382 r
= kvm_clp_service_call(cpu
, run
);
1384 case PRIV_B9_PCISTG
:
1385 r
= kvm_pcistg_service_call(cpu
, run
);
1388 r
= kvm_pcilg_service_call(cpu
, run
);
1391 r
= kvm_rpcit_service_call(cpu
, run
);
1394 /* just inject exception */
1399 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1
);
1406 static int handle_eb(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1411 case PRIV_EB_PCISTB
:
1412 r
= kvm_pcistb_service_call(cpu
, run
);
1415 r
= kvm_sic_service_call(cpu
, run
);
1418 /* just inject exception */
1423 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl
);
1430 static int handle_e3(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1435 case PRIV_E3_MPCIFC
:
1436 r
= kvm_mpcifc_service_call(cpu
, run
);
1438 case PRIV_E3_STPCIFC
:
1439 r
= kvm_stpcifc_service_call(cpu
, run
);
1443 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl
);
1450 static int handle_hypercall(S390CPU
*cpu
, struct kvm_run
*run
)
1452 CPUS390XState
*env
= &cpu
->env
;
1455 ret
= s390_virtio_hypercall(env
);
1456 if (ret
== -EINVAL
) {
1457 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1464 static void kvm_handle_diag_288(S390CPU
*cpu
, struct kvm_run
*run
)
1469 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1470 r3
= run
->s390_sieic
.ipa
& 0x000f;
1471 rc
= handle_diag_288(&cpu
->env
, r1
, r3
);
1473 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1477 static void kvm_handle_diag_308(S390CPU
*cpu
, struct kvm_run
*run
)
1481 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1482 r3
= run
->s390_sieic
.ipa
& 0x000f;
1483 handle_diag_308(&cpu
->env
, r1
, r3
, RA_IGNORED
);
1486 static int handle_sw_breakpoint(S390CPU
*cpu
, struct kvm_run
*run
)
1488 CPUS390XState
*env
= &cpu
->env
;
1491 pc
= env
->psw
.addr
- sw_bp_ilen
;
1492 if (kvm_find_sw_breakpoint(CPU(cpu
), pc
)) {
1500 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1502 static int handle_diag(S390CPU
*cpu
, struct kvm_run
*run
, uint32_t ipb
)
1508 * For any diagnose call we support, bits 48-63 of the resulting
1509 * address specify the function code; the remainder is ignored.
1511 func_code
= decode_basedisp_rs(&cpu
->env
, ipb
, NULL
) & DIAG_KVM_CODE_MASK
;
1512 switch (func_code
) {
1513 case DIAG_TIMEREVENT
:
1514 kvm_handle_diag_288(cpu
, run
);
1517 kvm_handle_diag_308(cpu
, run
);
1519 case DIAG_KVM_HYPERCALL
:
1520 r
= handle_hypercall(cpu
, run
);
1522 case DIAG_KVM_BREAKPOINT
:
1523 r
= handle_sw_breakpoint(cpu
, run
);
1526 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code
);
1527 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1534 static int kvm_s390_handle_sigp(S390CPU
*cpu
, uint8_t ipa1
, uint32_t ipb
)
1536 CPUS390XState
*env
= &cpu
->env
;
1537 const uint8_t r1
= ipa1
>> 4;
1538 const uint8_t r3
= ipa1
& 0x0f;
1542 /* get order code */
1543 order
= decode_basedisp_rs(env
, ipb
, NULL
) & SIGP_ORDER_MASK
;
1545 ret
= handle_sigp(env
, order
, r1
, r3
);
1550 static int handle_instruction(S390CPU
*cpu
, struct kvm_run
*run
)
1552 unsigned int ipa0
= (run
->s390_sieic
.ipa
& 0xff00);
1553 uint8_t ipa1
= run
->s390_sieic
.ipa
& 0x00ff;
1556 DPRINTF("handle_instruction 0x%x 0x%x\n",
1557 run
->s390_sieic
.ipa
, run
->s390_sieic
.ipb
);
1560 r
= handle_b2(cpu
, run
, ipa1
);
1563 r
= handle_b9(cpu
, run
, ipa1
);
1566 r
= handle_eb(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1569 r
= handle_e3(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1572 r
= handle_diag(cpu
, run
, run
->s390_sieic
.ipb
);
1575 r
= kvm_s390_handle_sigp(cpu
, ipa1
, run
->s390_sieic
.ipb
);
1581 kvm_s390_program_interrupt(cpu
, PGM_OPERATION
);
1587 static void unmanageable_intercept(S390CPU
*cpu
, S390CrashReason reason
,
1590 CPUState
*cs
= CPU(cpu
);
1593 cpu
->env
.crash_reason
= reason
;
1594 qemu_system_guest_panicked(cpu_get_crash_info(cs
));
1597 /* try to detect pgm check loops */
1598 static int handle_oper_loop(S390CPU
*cpu
, struct kvm_run
*run
)
1600 CPUState
*cs
= CPU(cpu
);
1603 newpsw
.mask
= ldq_phys(cs
->as
, cpu
->env
.psa
+
1604 offsetof(LowCore
, program_new_psw
));
1605 newpsw
.addr
= ldq_phys(cs
->as
, cpu
->env
.psa
+
1606 offsetof(LowCore
, program_new_psw
) + 8);
1607 oldpsw
.mask
= run
->psw_mask
;
1608 oldpsw
.addr
= run
->psw_addr
;
1610 * Avoid endless loops of operation exceptions, if the pgm new
1611 * PSW will cause a new operation exception.
1612 * The heuristic checks if the pgm new psw is within 6 bytes before
1613 * the faulting psw address (with same DAT, AS settings) and the
1614 * new psw is not a wait psw and the fault was not triggered by
1615 * problem state. In that case go into crashed state.
1618 if (oldpsw
.addr
- newpsw
.addr
<= 6 &&
1619 !(newpsw
.mask
& PSW_MASK_WAIT
) &&
1620 !(oldpsw
.mask
& PSW_MASK_PSTATE
) &&
1621 (newpsw
.mask
& PSW_MASK_ASC
) == (oldpsw
.mask
& PSW_MASK_ASC
) &&
1622 (newpsw
.mask
& PSW_MASK_DAT
) == (oldpsw
.mask
& PSW_MASK_DAT
)) {
1623 unmanageable_intercept(cpu
, S390_CRASH_REASON_OPINT_LOOP
,
1624 offsetof(LowCore
, program_new_psw
));
1630 static int handle_intercept(S390CPU
*cpu
)
1632 CPUState
*cs
= CPU(cpu
);
1633 struct kvm_run
*run
= cs
->kvm_run
;
1634 int icpt_code
= run
->s390_sieic
.icptcode
;
1637 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code
,
1638 (long)cs
->kvm_run
->psw_addr
);
1639 switch (icpt_code
) {
1640 case ICPT_INSTRUCTION
:
1641 r
= handle_instruction(cpu
, run
);
1644 unmanageable_intercept(cpu
, S390_CRASH_REASON_PGMINT_LOOP
,
1645 offsetof(LowCore
, program_new_psw
));
1649 unmanageable_intercept(cpu
, S390_CRASH_REASON_EXTINT_LOOP
,
1650 offsetof(LowCore
, external_new_psw
));
1654 /* disabled wait, since enabled wait is handled in kernel */
1655 s390_handle_wait(cpu
);
1659 do_stop_interrupt(&cpu
->env
);
1663 /* check for break points */
1664 r
= handle_sw_breakpoint(cpu
, run
);
1666 /* Then check for potential pgm check loops */
1667 r
= handle_oper_loop(cpu
, run
);
1669 kvm_s390_program_interrupt(cpu
, PGM_OPERATION
);
1673 case ICPT_SOFT_INTERCEPT
:
1674 fprintf(stderr
, "KVM unimplemented icpt SOFT\n");
1678 fprintf(stderr
, "KVM unimplemented icpt IO\n");
1682 fprintf(stderr
, "Unknown intercept code: %d\n", icpt_code
);
1690 static int handle_tsch(S390CPU
*cpu
)
1692 CPUState
*cs
= CPU(cpu
);
1693 struct kvm_run
*run
= cs
->kvm_run
;
1696 ret
= ioinst_handle_tsch(cpu
, cpu
->env
.regs
[1], run
->s390_tsch
.ipb
,
1701 * If an I/O interrupt had been dequeued, we have to reinject it.
1703 if (run
->s390_tsch
.dequeued
) {
1704 s390_io_interrupt(run
->s390_tsch
.subchannel_id
,
1705 run
->s390_tsch
.subchannel_nr
,
1706 run
->s390_tsch
.io_int_parm
,
1707 run
->s390_tsch
.io_int_word
);
1714 static void insert_stsi_3_2_2(S390CPU
*cpu
, __u64 addr
, uint8_t ar
)
1719 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &sysib
, sizeof(sysib
))) {
1722 /* Shift the stack of Extended Names to prepare for our own data */
1723 memmove(&sysib
.ext_names
[1], &sysib
.ext_names
[0],
1724 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- 1));
1725 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1726 * assumed it's not capable of managing Extended Names for lower levels.
1728 for (del
= 1; del
< sysib
.count
; del
++) {
1729 if (!sysib
.vm
[del
].ext_name_encoding
|| !sysib
.ext_names
[del
][0]) {
1733 if (del
< sysib
.count
) {
1734 memset(sysib
.ext_names
[del
], 0,
1735 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- del
));
1737 /* Insert short machine name in EBCDIC, padded with blanks */
1739 memset(sysib
.vm
[0].name
, 0x40, sizeof(sysib
.vm
[0].name
));
1740 ebcdic_put(sysib
.vm
[0].name
, qemu_name
, MIN(sizeof(sysib
.vm
[0].name
),
1741 strlen(qemu_name
)));
1743 sysib
.vm
[0].ext_name_encoding
= 2; /* 2 = UTF-8 */
1744 memset(sysib
.ext_names
[0], 0, sizeof(sysib
.ext_names
[0]));
1745 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1746 * considered by s390 as not capable of providing any Extended Name.
1747 * Therefore if no name was specified on qemu invocation, we go with the
1748 * same "KVMguest" default, which KVM has filled into short name field.
1751 strncpy((char *)sysib
.ext_names
[0], qemu_name
,
1752 sizeof(sysib
.ext_names
[0]));
1754 strcpy((char *)sysib
.ext_names
[0], "KVMguest");
1757 memcpy(sysib
.vm
[0].uuid
, &qemu_uuid
, sizeof(sysib
.vm
[0].uuid
));
1759 s390_cpu_virt_mem_write(cpu
, addr
, ar
, &sysib
, sizeof(sysib
));
1762 static int handle_stsi(S390CPU
*cpu
)
1764 CPUState
*cs
= CPU(cpu
);
1765 struct kvm_run
*run
= cs
->kvm_run
;
1767 switch (run
->s390_stsi
.fc
) {
1769 if (run
->s390_stsi
.sel1
!= 2 || run
->s390_stsi
.sel2
!= 2) {
1772 /* Only sysib 3.2.2 needs post-handling for now. */
1773 insert_stsi_3_2_2(cpu
, run
->s390_stsi
.addr
, run
->s390_stsi
.ar
);
1780 static int kvm_arch_handle_debug_exit(S390CPU
*cpu
)
1782 CPUState
*cs
= CPU(cpu
);
1783 struct kvm_run
*run
= cs
->kvm_run
;
1786 struct kvm_debug_exit_arch
*arch_info
= &run
->debug
.arch
;
1788 switch (arch_info
->type
) {
1789 case KVM_HW_WP_WRITE
:
1790 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1791 cs
->watchpoint_hit
= &hw_watchpoint
;
1792 hw_watchpoint
.vaddr
= arch_info
->addr
;
1793 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1798 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1802 case KVM_SINGLESTEP
:
1803 if (cs
->singlestep_enabled
) {
1814 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
1816 S390CPU
*cpu
= S390_CPU(cs
);
1819 qemu_mutex_lock_iothread();
1821 kvm_cpu_synchronize_state(cs
);
1823 switch (run
->exit_reason
) {
1824 case KVM_EXIT_S390_SIEIC
:
1825 ret
= handle_intercept(cpu
);
1827 case KVM_EXIT_S390_RESET
:
1828 s390_ipl_reset_request(cs
, S390_RESET_REIPL
);
1830 case KVM_EXIT_S390_TSCH
:
1831 ret
= handle_tsch(cpu
);
1833 case KVM_EXIT_S390_STSI
:
1834 ret
= handle_stsi(cpu
);
1836 case KVM_EXIT_DEBUG
:
1837 ret
= kvm_arch_handle_debug_exit(cpu
);
1840 fprintf(stderr
, "Unknown KVM exit: %d\n", run
->exit_reason
);
1843 qemu_mutex_unlock_iothread();
1846 ret
= EXCP_INTERRUPT
;
1851 bool kvm_arch_stop_on_emulation_error(CPUState
*cpu
)
1856 void kvm_s390_enable_css_support(S390CPU
*cpu
)
1860 /* Activate host kernel channel subsystem support. */
1861 r
= kvm_vcpu_enable_cap(CPU(cpu
), KVM_CAP_S390_CSS_SUPPORT
, 0);
1865 void kvm_arch_init_irq_routing(KVMState
*s
)
1868 * Note that while irqchip capabilities generally imply that cpustates
1869 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1870 * have to override the common code kvm_halt_in_kernel_allowed setting.
1872 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
1873 kvm_gsi_routing_allowed
= true;
1874 kvm_halt_in_kernel_allowed
= false;
1878 int kvm_s390_assign_subch_ioeventfd(EventNotifier
*notifier
, uint32_t sch
,
1879 int vq
, bool assign
)
1881 struct kvm_ioeventfd kick
= {
1882 .flags
= KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY
|
1883 KVM_IOEVENTFD_FLAG_DATAMATCH
,
1884 .fd
= event_notifier_get_fd(notifier
),
1889 trace_kvm_assign_subch_ioeventfd(kick
.fd
, kick
.addr
, assign
,
1891 if (!kvm_check_extension(kvm_state
, KVM_CAP_IOEVENTFD
)) {
1895 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1897 return kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
1900 int kvm_s390_get_ri(void)
1905 int kvm_s390_get_gs(void)
1910 int kvm_s390_set_cpu_state(S390CPU
*cpu
, uint8_t cpu_state
)
1912 struct kvm_mp_state mp_state
= {};
1915 /* the kvm part might not have been initialized yet */
1916 if (CPU(cpu
)->kvm_state
== NULL
) {
1920 switch (cpu_state
) {
1921 case S390_CPU_STATE_STOPPED
:
1922 mp_state
.mp_state
= KVM_MP_STATE_STOPPED
;
1924 case S390_CPU_STATE_CHECK_STOP
:
1925 mp_state
.mp_state
= KVM_MP_STATE_CHECK_STOP
;
1927 case S390_CPU_STATE_OPERATING
:
1928 mp_state
.mp_state
= KVM_MP_STATE_OPERATING
;
1930 case S390_CPU_STATE_LOAD
:
1931 mp_state
.mp_state
= KVM_MP_STATE_LOAD
;
1934 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1939 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
1941 trace_kvm_failed_cpu_state_set(CPU(cpu
)->cpu_index
, cpu_state
,
1948 void kvm_s390_vcpu_interrupt_pre_save(S390CPU
*cpu
)
1950 struct kvm_s390_irq_state irq_state
= {
1951 .buf
= (uint64_t) cpu
->irqstate
,
1952 .len
= VCPU_IRQ_BUF_SIZE
,
1954 CPUState
*cs
= CPU(cpu
);
1957 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_IRQ_STATE
)) {
1961 bytes
= kvm_vcpu_ioctl(cs
, KVM_S390_GET_IRQ_STATE
, &irq_state
);
1963 cpu
->irqstate_saved_size
= 0;
1964 error_report("Migration of interrupt state failed");
1968 cpu
->irqstate_saved_size
= bytes
;
1971 int kvm_s390_vcpu_interrupt_post_load(S390CPU
*cpu
)
1973 CPUState
*cs
= CPU(cpu
);
1974 struct kvm_s390_irq_state irq_state
= {
1975 .buf
= (uint64_t) cpu
->irqstate
,
1976 .len
= cpu
->irqstate_saved_size
,
1980 if (cpu
->irqstate_saved_size
== 0) {
1984 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_IRQ_STATE
)) {
1988 r
= kvm_vcpu_ioctl(cs
, KVM_S390_SET_IRQ_STATE
, &irq_state
);
1990 error_report("Setting interrupt state failed %d", r
);
1995 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
1996 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
1998 S390PCIBusDevice
*pbdev
;
1999 uint32_t vec
= data
& ZPCI_MSI_VEC_MASK
;
2002 DPRINTF("add_msi_route no pci device\n");
2006 pbdev
= s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev
)->id
);
2008 DPRINTF("add_msi_route no zpci device\n");
2012 route
->type
= KVM_IRQ_ROUTING_S390_ADAPTER
;
2014 route
->u
.adapter
.summary_addr
= pbdev
->routes
.adapter
.summary_addr
;
2015 route
->u
.adapter
.ind_addr
= pbdev
->routes
.adapter
.ind_addr
;
2016 route
->u
.adapter
.summary_offset
= pbdev
->routes
.adapter
.summary_offset
;
2017 route
->u
.adapter
.ind_offset
= pbdev
->routes
.adapter
.ind_offset
+ vec
;
2018 route
->u
.adapter
.adapter_id
= pbdev
->routes
.adapter
.adapter_id
;
2022 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
2023 int vector
, PCIDevice
*dev
)
2028 int kvm_arch_release_virq_post(int virq
)
2033 int kvm_arch_msi_data_to_gsi(uint32_t data
)
2038 static int query_cpu_subfunc(S390FeatBitmap features
)
2040 struct kvm_s390_vm_cpu_subfunc prop
;
2041 struct kvm_device_attr attr
= {
2042 .group
= KVM_S390_VM_CPU_MODEL
,
2043 .attr
= KVM_S390_VM_CPU_MACHINE_SUBFUNC
,
2044 .addr
= (uint64_t) &prop
,
2048 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2054 * We're going to add all subfunctions now, if the corresponding feature
2055 * is available that unlocks the query functions.
2057 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PLO
, prop
.plo
);
2058 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING
, features
)) {
2059 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PTFF
, prop
.ptff
);
2061 if (test_bit(S390_FEAT_MSA
, features
)) {
2062 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMAC
, prop
.kmac
);
2063 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMC
, prop
.kmc
);
2064 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KM
, prop
.km
);
2065 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KIMD
, prop
.kimd
);
2066 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KLMD
, prop
.klmd
);
2068 if (test_bit(S390_FEAT_MSA_EXT_3
, features
)) {
2069 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PCKMO
, prop
.pckmo
);
2071 if (test_bit(S390_FEAT_MSA_EXT_4
, features
)) {
2072 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMCTR
, prop
.kmctr
);
2073 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMF
, prop
.kmf
);
2074 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMO
, prop
.kmo
);
2075 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PCC
, prop
.pcc
);
2077 if (test_bit(S390_FEAT_MSA_EXT_5
, features
)) {
2078 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PPNO
, prop
.ppno
);
2080 if (test_bit(S390_FEAT_MSA_EXT_8
, features
)) {
2081 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMA
, prop
.kma
);
2086 static int configure_cpu_subfunc(const S390FeatBitmap features
)
2088 struct kvm_s390_vm_cpu_subfunc prop
= {};
2089 struct kvm_device_attr attr
= {
2090 .group
= KVM_S390_VM_CPU_MODEL
,
2091 .attr
= KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
,
2092 .addr
= (uint64_t) &prop
,
2095 if (!kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2096 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
)) {
2097 /* hardware support might be missing, IBC will handle most of this */
2101 s390_fill_feat_block(features
, S390_FEAT_TYPE_PLO
, prop
.plo
);
2102 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING
, features
)) {
2103 s390_fill_feat_block(features
, S390_FEAT_TYPE_PTFF
, prop
.ptff
);
2105 if (test_bit(S390_FEAT_MSA
, features
)) {
2106 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMAC
, prop
.kmac
);
2107 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMC
, prop
.kmc
);
2108 s390_fill_feat_block(features
, S390_FEAT_TYPE_KM
, prop
.km
);
2109 s390_fill_feat_block(features
, S390_FEAT_TYPE_KIMD
, prop
.kimd
);
2110 s390_fill_feat_block(features
, S390_FEAT_TYPE_KLMD
, prop
.klmd
);
2112 if (test_bit(S390_FEAT_MSA_EXT_3
, features
)) {
2113 s390_fill_feat_block(features
, S390_FEAT_TYPE_PCKMO
, prop
.pckmo
);
2115 if (test_bit(S390_FEAT_MSA_EXT_4
, features
)) {
2116 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMCTR
, prop
.kmctr
);
2117 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMF
, prop
.kmf
);
2118 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMO
, prop
.kmo
);
2119 s390_fill_feat_block(features
, S390_FEAT_TYPE_PCC
, prop
.pcc
);
2121 if (test_bit(S390_FEAT_MSA_EXT_5
, features
)) {
2122 s390_fill_feat_block(features
, S390_FEAT_TYPE_PPNO
, prop
.ppno
);
2124 if (test_bit(S390_FEAT_MSA_EXT_8
, features
)) {
2125 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMA
, prop
.kma
);
2127 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2130 static int kvm_to_feat
[][2] = {
2131 { KVM_S390_VM_CPU_FEAT_ESOP
, S390_FEAT_ESOP
},
2132 { KVM_S390_VM_CPU_FEAT_SIEF2
, S390_FEAT_SIE_F2
},
2133 { KVM_S390_VM_CPU_FEAT_64BSCAO
, S390_FEAT_SIE_64BSCAO
},
2134 { KVM_S390_VM_CPU_FEAT_SIIF
, S390_FEAT_SIE_SIIF
},
2135 { KVM_S390_VM_CPU_FEAT_GPERE
, S390_FEAT_SIE_GPERE
},
2136 { KVM_S390_VM_CPU_FEAT_GSLS
, S390_FEAT_SIE_GSLS
},
2137 { KVM_S390_VM_CPU_FEAT_IB
, S390_FEAT_SIE_IB
},
2138 { KVM_S390_VM_CPU_FEAT_CEI
, S390_FEAT_SIE_CEI
},
2139 { KVM_S390_VM_CPU_FEAT_IBS
, S390_FEAT_SIE_IBS
},
2140 { KVM_S390_VM_CPU_FEAT_SKEY
, S390_FEAT_SIE_SKEY
},
2141 { KVM_S390_VM_CPU_FEAT_CMMA
, S390_FEAT_SIE_CMMA
},
2142 { KVM_S390_VM_CPU_FEAT_PFMFI
, S390_FEAT_SIE_PFMFI
},
2143 { KVM_S390_VM_CPU_FEAT_SIGPIF
, S390_FEAT_SIE_SIGPIF
},
2144 { KVM_S390_VM_CPU_FEAT_KSS
, S390_FEAT_SIE_KSS
},
2147 static int query_cpu_feat(S390FeatBitmap features
)
2149 struct kvm_s390_vm_cpu_feat prop
;
2150 struct kvm_device_attr attr
= {
2151 .group
= KVM_S390_VM_CPU_MODEL
,
2152 .attr
= KVM_S390_VM_CPU_MACHINE_FEAT
,
2153 .addr
= (uint64_t) &prop
,
2158 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2163 for (i
= 0; i
< ARRAY_SIZE(kvm_to_feat
); i
++) {
2164 if (test_be_bit(kvm_to_feat
[i
][0], (uint8_t *) prop
.feat
)) {
2165 set_bit(kvm_to_feat
[i
][1], features
);
2171 static int configure_cpu_feat(const S390FeatBitmap features
)
2173 struct kvm_s390_vm_cpu_feat prop
= {};
2174 struct kvm_device_attr attr
= {
2175 .group
= KVM_S390_VM_CPU_MODEL
,
2176 .attr
= KVM_S390_VM_CPU_PROCESSOR_FEAT
,
2177 .addr
= (uint64_t) &prop
,
2181 for (i
= 0; i
< ARRAY_SIZE(kvm_to_feat
); i
++) {
2182 if (test_bit(kvm_to_feat
[i
][1], features
)) {
2183 set_be_bit(kvm_to_feat
[i
][0], (uint8_t *) prop
.feat
);
2186 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2189 bool kvm_s390_cpu_models_supported(void)
2191 if (!cpu_model_allowed()) {
2192 /* compatibility machines interfere with the cpu model */
2195 return kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2196 KVM_S390_VM_CPU_MACHINE
) &&
2197 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2198 KVM_S390_VM_CPU_PROCESSOR
) &&
2199 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2200 KVM_S390_VM_CPU_MACHINE_FEAT
) &&
2201 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2202 KVM_S390_VM_CPU_PROCESSOR_FEAT
) &&
2203 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2204 KVM_S390_VM_CPU_MACHINE_SUBFUNC
);
2207 void kvm_s390_get_host_cpu_model(S390CPUModel
*model
, Error
**errp
)
2209 struct kvm_s390_vm_cpu_machine prop
= {};
2210 struct kvm_device_attr attr
= {
2211 .group
= KVM_S390_VM_CPU_MODEL
,
2212 .attr
= KVM_S390_VM_CPU_MACHINE
,
2213 .addr
= (uint64_t) &prop
,
2215 uint16_t unblocked_ibc
= 0, cpu_type
= 0;
2218 memset(model
, 0, sizeof(*model
));
2220 if (!kvm_s390_cpu_models_supported()) {
2221 error_setg(errp
, "KVM doesn't support CPU models");
2225 /* query the basic cpu model properties */
2226 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2228 error_setg(errp
, "KVM: Error querying host CPU model: %d", rc
);
2232 cpu_type
= cpuid_type(prop
.cpuid
);
2233 if (has_ibc(prop
.ibc
)) {
2234 model
->lowest_ibc
= lowest_ibc(prop
.ibc
);
2235 unblocked_ibc
= unblocked_ibc(prop
.ibc
);
2237 model
->cpu_id
= cpuid_id(prop
.cpuid
);
2238 model
->cpu_id_format
= cpuid_format(prop
.cpuid
);
2239 model
->cpu_ver
= 0xff;
2241 /* get supported cpu features indicated via STFL(E) */
2242 s390_add_from_feat_block(model
->features
, S390_FEAT_TYPE_STFL
,
2243 (uint8_t *) prop
.fac_mask
);
2244 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2245 if (test_bit(S390_FEAT_STFLE
, model
->features
)) {
2246 set_bit(S390_FEAT_DAT_ENH_2
, model
->features
);
2248 /* get supported cpu features indicated e.g. via SCLP */
2249 rc
= query_cpu_feat(model
->features
);
2251 error_setg(errp
, "KVM: Error querying CPU features: %d", rc
);
2254 /* get supported cpu subfunctions indicated via query / test bit */
2255 rc
= query_cpu_subfunc(model
->features
);
2257 error_setg(errp
, "KVM: Error querying CPU subfunctions: %d", rc
);
2261 /* PTFF subfunctions might be indicated although kernel support missing */
2262 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH
, model
->features
)) {
2263 clear_bit(S390_FEAT_PTFF_QSIE
, model
->features
);
2264 clear_bit(S390_FEAT_PTFF_QTOUE
, model
->features
);
2265 clear_bit(S390_FEAT_PTFF_STOE
, model
->features
);
2266 clear_bit(S390_FEAT_PTFF_STOUE
, model
->features
);
2269 /* with cpu model support, CMM is only indicated if really available */
2270 if (kvm_s390_cmma_available()) {
2271 set_bit(S390_FEAT_CMM
, model
->features
);
2273 /* no cmm -> no cmm nt */
2274 clear_bit(S390_FEAT_CMM_NT
, model
->features
);
2277 /* bpb needs kernel support for migration, VSIE and reset */
2278 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_BPB
)) {
2279 clear_bit(S390_FEAT_BPB
, model
->features
);
2282 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2283 set_bit(S390_FEAT_ZPCI
, model
->features
);
2284 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION
, model
->features
);
2286 if (s390_known_cpu_type(cpu_type
)) {
2287 /* we want the exact model, even if some features are missing */
2288 model
->def
= s390_find_cpu_def(cpu_type
, ibc_gen(unblocked_ibc
),
2289 ibc_ec_ga(unblocked_ibc
), NULL
);
2291 /* model unknown, e.g. too new - search using features */
2292 model
->def
= s390_find_cpu_def(0, ibc_gen(unblocked_ibc
),
2293 ibc_ec_ga(unblocked_ibc
),
2297 error_setg(errp
, "KVM: host CPU model could not be identified");
2300 /* for now, we can only provide the AP feature with HW support */
2301 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
,
2302 KVM_S390_VM_CRYPTO_ENABLE_APIE
)) {
2303 set_bit(S390_FEAT_AP
, model
->features
);
2305 /* strip of features that are not part of the maximum model */
2306 bitmap_and(model
->features
, model
->features
, model
->def
->full_feat
,
2310 static void kvm_s390_configure_apie(bool interpret
)
2312 uint64_t attr
= interpret
? KVM_S390_VM_CRYPTO_ENABLE_APIE
:
2313 KVM_S390_VM_CRYPTO_DISABLE_APIE
;
2315 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
2316 kvm_s390_set_attr(attr
);
2320 void kvm_s390_apply_cpu_model(const S390CPUModel
*model
, Error
**errp
)
2322 struct kvm_s390_vm_cpu_processor prop
= {
2325 struct kvm_device_attr attr
= {
2326 .group
= KVM_S390_VM_CPU_MODEL
,
2327 .attr
= KVM_S390_VM_CPU_PROCESSOR
,
2328 .addr
= (uint64_t) &prop
,
2333 /* compatibility handling if cpu models are disabled */
2334 if (kvm_s390_cmma_available()) {
2335 kvm_s390_enable_cmma();
2339 if (!kvm_s390_cpu_models_supported()) {
2340 error_setg(errp
, "KVM doesn't support CPU models");
2343 prop
.cpuid
= s390_cpuid_from_cpu_model(model
);
2344 prop
.ibc
= s390_ibc_from_cpu_model(model
);
2345 /* configure cpu features indicated via STFL(e) */
2346 s390_fill_feat_block(model
->features
, S390_FEAT_TYPE_STFL
,
2347 (uint8_t *) prop
.fac_list
);
2348 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2350 error_setg(errp
, "KVM: Error configuring the CPU model: %d", rc
);
2353 /* configure cpu features indicated e.g. via SCLP */
2354 rc
= configure_cpu_feat(model
->features
);
2356 error_setg(errp
, "KVM: Error configuring CPU features: %d", rc
);
2359 /* configure cpu subfunctions indicated via query / test bit */
2360 rc
= configure_cpu_subfunc(model
->features
);
2362 error_setg(errp
, "KVM: Error configuring CPU subfunctions: %d", rc
);
2365 /* enable CMM via CMMA */
2366 if (test_bit(S390_FEAT_CMM
, model
->features
)) {
2367 kvm_s390_enable_cmma();
2370 if (test_bit(S390_FEAT_AP
, model
->features
)) {
2371 kvm_s390_configure_apie(true);
2375 void kvm_s390_restart_interrupt(S390CPU
*cpu
)
2377 struct kvm_s390_irq irq
= {
2378 .type
= KVM_S390_RESTART
,
2381 kvm_s390_vcpu_interrupt(cpu
, &irq
);
2384 void kvm_s390_stop_interrupt(S390CPU
*cpu
)
2386 struct kvm_s390_irq irq
= {
2387 .type
= KVM_S390_SIGP_STOP
,
2390 kvm_s390_vcpu_interrupt(cpu
, &irq
);