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 void kvm_s390_set_max_pagesize(uint64_t pagesize
, Error
**errp
)
288 if (pagesize
== 4 * KiB
) {
292 if (!hpage_1m_allowed()) {
293 error_setg(errp
, "This QEMU machine does not support huge page "
298 if (pagesize
!= 1 * MiB
) {
299 error_setg(errp
, "Memory backing with 2G pages was specified, "
300 "but KVM does not support this memory backing");
304 if (kvm_vm_enable_cap(kvm_state
, KVM_CAP_S390_HPAGE_1M
, 0)) {
305 error_setg(errp
, "Memory backing with 1M pages was specified, "
306 "but KVM does not support this memory backing");
313 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
315 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
317 mc
->default_cpu_type
= S390_CPU_TYPE_NAME("host");
318 cap_sync_regs
= kvm_check_extension(s
, KVM_CAP_SYNC_REGS
);
319 cap_async_pf
= kvm_check_extension(s
, KVM_CAP_ASYNC_PF
);
320 cap_mem_op
= kvm_check_extension(s
, KVM_CAP_S390_MEM_OP
);
321 cap_s390_irq
= kvm_check_extension(s
, KVM_CAP_S390_INJECT_IRQ
);
323 if (!kvm_check_extension(s
, KVM_CAP_S390_GMAP
)
324 || !kvm_check_extension(s
, KVM_CAP_S390_COW
)) {
325 phys_mem_set_alloc(legacy_s390_alloc
);
328 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_SIGP
, 0);
329 kvm_vm_enable_cap(s
, KVM_CAP_S390_VECTOR_REGISTERS
, 0);
330 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_STSI
, 0);
332 if (kvm_vm_enable_cap(s
, KVM_CAP_S390_RI
, 0) == 0) {
336 if (cpu_model_allowed()) {
337 if (kvm_vm_enable_cap(s
, KVM_CAP_S390_GS
, 0) == 0) {
343 * The migration interface for ais was introduced with kernel 4.13
344 * but the capability itself had been active since 4.12. As migration
345 * support is considered necessary let's disable ais in the 2.10
348 /* kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0); */
353 int kvm_arch_irqchip_create(MachineState
*ms
, KVMState
*s
)
358 unsigned long kvm_arch_vcpu_id(CPUState
*cpu
)
360 return cpu
->cpu_index
;
363 int kvm_arch_init_vcpu(CPUState
*cs
)
365 S390CPU
*cpu
= S390_CPU(cs
);
366 kvm_s390_set_cpu_state(cpu
, cpu
->env
.cpu_state
);
367 cpu
->irqstate
= g_malloc0(VCPU_IRQ_BUF_SIZE
);
371 void kvm_s390_reset_vcpu(S390CPU
*cpu
)
373 CPUState
*cs
= CPU(cpu
);
375 /* The initial reset call is needed here to reset in-kernel
376 * vcpu data that we can't access directly from QEMU
377 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
378 * Before this ioctl cpu_synchronize_state() is called in common kvm
380 if (kvm_vcpu_ioctl(cs
, KVM_S390_INITIAL_RESET
, NULL
)) {
381 error_report("Initial CPU reset failed on CPU %i", cs
->cpu_index
);
385 static int can_sync_regs(CPUState
*cs
, int regs
)
387 return cap_sync_regs
&& (cs
->kvm_run
->kvm_valid_regs
& regs
) == regs
;
390 int kvm_arch_put_registers(CPUState
*cs
, int level
)
392 S390CPU
*cpu
= S390_CPU(cs
);
393 CPUS390XState
*env
= &cpu
->env
;
394 struct kvm_sregs sregs
;
395 struct kvm_regs regs
;
396 struct kvm_fpu fpu
= {};
400 /* always save the PSW and the GPRS*/
401 cs
->kvm_run
->psw_addr
= env
->psw
.addr
;
402 cs
->kvm_run
->psw_mask
= env
->psw
.mask
;
404 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
405 for (i
= 0; i
< 16; i
++) {
406 cs
->kvm_run
->s
.regs
.gprs
[i
] = env
->regs
[i
];
407 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_GPRS
;
410 for (i
= 0; i
< 16; i
++) {
411 regs
.gprs
[i
] = env
->regs
[i
];
413 r
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
419 if (can_sync_regs(cs
, KVM_SYNC_VRS
)) {
420 for (i
= 0; i
< 32; i
++) {
421 cs
->kvm_run
->s
.regs
.vrs
[i
][0] = env
->vregs
[i
][0];
422 cs
->kvm_run
->s
.regs
.vrs
[i
][1] = env
->vregs
[i
][1];
424 cs
->kvm_run
->s
.regs
.fpc
= env
->fpc
;
425 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_VRS
;
426 } else if (can_sync_regs(cs
, KVM_SYNC_FPRS
)) {
427 for (i
= 0; i
< 16; i
++) {
428 cs
->kvm_run
->s
.regs
.fprs
[i
] = *get_freg(env
, i
);
430 cs
->kvm_run
->s
.regs
.fpc
= env
->fpc
;
431 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_FPRS
;
434 for (i
= 0; i
< 16; i
++) {
435 fpu
.fprs
[i
] = *get_freg(env
, i
);
439 r
= kvm_vcpu_ioctl(cs
, KVM_SET_FPU
, &fpu
);
445 /* Do we need to save more than that? */
446 if (level
== KVM_PUT_RUNTIME_STATE
) {
450 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
451 cs
->kvm_run
->s
.regs
.cputm
= env
->cputm
;
452 cs
->kvm_run
->s
.regs
.ckc
= env
->ckc
;
453 cs
->kvm_run
->s
.regs
.todpr
= env
->todpr
;
454 cs
->kvm_run
->s
.regs
.gbea
= env
->gbea
;
455 cs
->kvm_run
->s
.regs
.pp
= env
->pp
;
456 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ARCH0
;
459 * These ONE_REGS are not protected by a capability. As they are only
460 * necessary for migration we just trace a possible error, but don't
461 * return with an error return code.
463 kvm_set_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
464 kvm_set_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
465 kvm_set_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
466 kvm_set_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
467 kvm_set_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
470 if (can_sync_regs(cs
, KVM_SYNC_RICCB
)) {
471 memcpy(cs
->kvm_run
->s
.regs
.riccb
, env
->riccb
, 64);
472 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_RICCB
;
475 /* pfault parameters */
476 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
477 cs
->kvm_run
->s
.regs
.pft
= env
->pfault_token
;
478 cs
->kvm_run
->s
.regs
.pfs
= env
->pfault_select
;
479 cs
->kvm_run
->s
.regs
.pfc
= env
->pfault_compare
;
480 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PFAULT
;
481 } else if (cap_async_pf
) {
482 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
486 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
490 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
496 /* access registers and control registers*/
497 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
498 for (i
= 0; i
< 16; i
++) {
499 cs
->kvm_run
->s
.regs
.acrs
[i
] = env
->aregs
[i
];
500 cs
->kvm_run
->s
.regs
.crs
[i
] = env
->cregs
[i
];
502 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ACRS
;
503 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_CRS
;
505 for (i
= 0; i
< 16; i
++) {
506 sregs
.acrs
[i
] = env
->aregs
[i
];
507 sregs
.crs
[i
] = env
->cregs
[i
];
509 r
= kvm_vcpu_ioctl(cs
, KVM_SET_SREGS
, &sregs
);
515 if (can_sync_regs(cs
, KVM_SYNC_GSCB
)) {
516 memcpy(cs
->kvm_run
->s
.regs
.gscb
, env
->gscb
, 32);
517 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_GSCB
;
520 if (can_sync_regs(cs
, KVM_SYNC_BPBC
)) {
521 cs
->kvm_run
->s
.regs
.bpbc
= env
->bpbc
;
522 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_BPBC
;
525 if (can_sync_regs(cs
, KVM_SYNC_ETOKEN
)) {
526 cs
->kvm_run
->s
.regs
.etoken
= env
->etoken
;
527 cs
->kvm_run
->s
.regs
.etoken_extension
= env
->etoken_extension
;
528 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ETOKEN
;
531 /* Finally the prefix */
532 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
533 cs
->kvm_run
->s
.regs
.prefix
= env
->psa
;
534 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PREFIX
;
536 /* prefix is only supported via sync regs */
541 int kvm_arch_get_registers(CPUState
*cs
)
543 S390CPU
*cpu
= S390_CPU(cs
);
544 CPUS390XState
*env
= &cpu
->env
;
545 struct kvm_sregs sregs
;
546 struct kvm_regs regs
;
551 env
->psw
.addr
= cs
->kvm_run
->psw_addr
;
552 env
->psw
.mask
= cs
->kvm_run
->psw_mask
;
555 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
556 for (i
= 0; i
< 16; i
++) {
557 env
->regs
[i
] = cs
->kvm_run
->s
.regs
.gprs
[i
];
560 r
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
564 for (i
= 0; i
< 16; i
++) {
565 env
->regs
[i
] = regs
.gprs
[i
];
569 /* The ACRS and CRS */
570 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
571 for (i
= 0; i
< 16; i
++) {
572 env
->aregs
[i
] = cs
->kvm_run
->s
.regs
.acrs
[i
];
573 env
->cregs
[i
] = cs
->kvm_run
->s
.regs
.crs
[i
];
576 r
= kvm_vcpu_ioctl(cs
, KVM_GET_SREGS
, &sregs
);
580 for (i
= 0; i
< 16; i
++) {
581 env
->aregs
[i
] = sregs
.acrs
[i
];
582 env
->cregs
[i
] = sregs
.crs
[i
];
586 /* Floating point and vector registers */
587 if (can_sync_regs(cs
, KVM_SYNC_VRS
)) {
588 for (i
= 0; i
< 32; i
++) {
589 env
->vregs
[i
][0] = cs
->kvm_run
->s
.regs
.vrs
[i
][0];
590 env
->vregs
[i
][1] = cs
->kvm_run
->s
.regs
.vrs
[i
][1];
592 env
->fpc
= cs
->kvm_run
->s
.regs
.fpc
;
593 } else if (can_sync_regs(cs
, KVM_SYNC_FPRS
)) {
594 for (i
= 0; i
< 16; i
++) {
595 *get_freg(env
, i
) = cs
->kvm_run
->s
.regs
.fprs
[i
];
597 env
->fpc
= cs
->kvm_run
->s
.regs
.fpc
;
599 r
= kvm_vcpu_ioctl(cs
, KVM_GET_FPU
, &fpu
);
603 for (i
= 0; i
< 16; i
++) {
604 *get_freg(env
, i
) = fpu
.fprs
[i
];
610 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
611 env
->psa
= cs
->kvm_run
->s
.regs
.prefix
;
614 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
615 env
->cputm
= cs
->kvm_run
->s
.regs
.cputm
;
616 env
->ckc
= cs
->kvm_run
->s
.regs
.ckc
;
617 env
->todpr
= cs
->kvm_run
->s
.regs
.todpr
;
618 env
->gbea
= cs
->kvm_run
->s
.regs
.gbea
;
619 env
->pp
= cs
->kvm_run
->s
.regs
.pp
;
622 * These ONE_REGS are not protected by a capability. As they are only
623 * necessary for migration we just trace a possible error, but don't
624 * return with an error return code.
626 kvm_get_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
627 kvm_get_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
628 kvm_get_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
629 kvm_get_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
630 kvm_get_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
633 if (can_sync_regs(cs
, KVM_SYNC_RICCB
)) {
634 memcpy(env
->riccb
, cs
->kvm_run
->s
.regs
.riccb
, 64);
637 if (can_sync_regs(cs
, KVM_SYNC_GSCB
)) {
638 memcpy(env
->gscb
, cs
->kvm_run
->s
.regs
.gscb
, 32);
641 if (can_sync_regs(cs
, KVM_SYNC_BPBC
)) {
642 env
->bpbc
= cs
->kvm_run
->s
.regs
.bpbc
;
645 if (can_sync_regs(cs
, KVM_SYNC_ETOKEN
)) {
646 env
->etoken
= cs
->kvm_run
->s
.regs
.etoken
;
647 env
->etoken_extension
= cs
->kvm_run
->s
.regs
.etoken_extension
;
650 /* pfault parameters */
651 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
652 env
->pfault_token
= cs
->kvm_run
->s
.regs
.pft
;
653 env
->pfault_select
= cs
->kvm_run
->s
.regs
.pfs
;
654 env
->pfault_compare
= cs
->kvm_run
->s
.regs
.pfc
;
655 } else if (cap_async_pf
) {
656 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
660 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
664 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
673 int kvm_s390_get_clock(uint8_t *tod_high
, uint64_t *tod_low
)
676 struct kvm_device_attr attr
= {
677 .group
= KVM_S390_VM_TOD
,
678 .attr
= KVM_S390_VM_TOD_LOW
,
679 .addr
= (uint64_t)tod_low
,
682 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
687 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
688 attr
.addr
= (uint64_t)tod_high
;
689 return kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
692 int kvm_s390_get_clock_ext(uint8_t *tod_high
, uint64_t *tod_low
)
695 struct kvm_s390_vm_tod_clock gtod
;
696 struct kvm_device_attr attr
= {
697 .group
= KVM_S390_VM_TOD
,
698 .attr
= KVM_S390_VM_TOD_EXT
,
699 .addr
= (uint64_t)>od
,
702 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
703 *tod_high
= gtod
.epoch_idx
;
709 int kvm_s390_set_clock(uint8_t tod_high
, uint64_t tod_low
)
712 struct kvm_device_attr attr
= {
713 .group
= KVM_S390_VM_TOD
,
714 .attr
= KVM_S390_VM_TOD_LOW
,
715 .addr
= (uint64_t)&tod_low
,
718 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
723 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
724 attr
.addr
= (uint64_t)&tod_high
;
725 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
728 int kvm_s390_set_clock_ext(uint8_t tod_high
, uint64_t tod_low
)
730 struct kvm_s390_vm_tod_clock gtod
= {
731 .epoch_idx
= tod_high
,
734 struct kvm_device_attr attr
= {
735 .group
= KVM_S390_VM_TOD
,
736 .attr
= KVM_S390_VM_TOD_EXT
,
737 .addr
= (uint64_t)>od
,
740 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
745 * @addr: the logical start address in guest memory
746 * @ar: the access register number
747 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
748 * @len: length that should be transferred
749 * @is_write: true = write, false = read
750 * Returns: 0 on success, non-zero if an exception or error occurred
752 * Use KVM ioctl to read/write from/to guest memory. An access exception
753 * is injected into the vCPU in case of translation errors.
755 int kvm_s390_mem_op(S390CPU
*cpu
, vaddr addr
, uint8_t ar
, void *hostbuf
,
756 int len
, bool is_write
)
758 struct kvm_s390_mem_op mem_op
= {
760 .flags
= KVM_S390_MEMOP_F_INJECT_EXCEPTION
,
762 .op
= is_write
? KVM_S390_MEMOP_LOGICAL_WRITE
763 : KVM_S390_MEMOP_LOGICAL_READ
,
764 .buf
= (uint64_t)hostbuf
,
773 mem_op
.flags
|= KVM_S390_MEMOP_F_CHECK_ONLY
;
776 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_S390_MEM_OP
, &mem_op
);
778 warn_report("KVM_S390_MEM_OP failed: %s", strerror(-ret
));
784 * Legacy layout for s390:
785 * Older S390 KVM requires the topmost vma of the RAM to be
786 * smaller than an system defined value, which is at least 256GB.
787 * Larger systems have larger values. We put the guest between
788 * the end of data segment (system break) and this value. We
789 * use 32GB as a base to have enough room for the system break
790 * to grow. We also have to use MAP parameters that avoid
791 * read-only mapping of guest pages.
793 static void *legacy_s390_alloc(size_t size
, uint64_t *align
, bool shared
)
798 /* we only support one allocation, which is enough for initial ram */
802 mem
= mmap((void *) 0x800000000ULL
, size
,
803 PROT_EXEC
|PROT_READ
|PROT_WRITE
,
804 MAP_SHARED
| MAP_ANONYMOUS
| MAP_FIXED
, -1, 0);
805 if (mem
== MAP_FAILED
) {
809 *align
= QEMU_VMALLOC_ALIGN
;
814 static uint8_t const *sw_bp_inst
;
815 static uint8_t sw_bp_ilen
;
817 static void determine_sw_breakpoint_instr(void)
819 /* DIAG 501 is used for sw breakpoints with old kernels */
820 static const uint8_t diag_501
[] = {0x83, 0x24, 0x05, 0x01};
821 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
822 static const uint8_t instr_0x0000
[] = {0x00, 0x00};
827 if (kvm_vm_enable_cap(kvm_state
, KVM_CAP_S390_USER_INSTR0
, 0)) {
828 sw_bp_inst
= diag_501
;
829 sw_bp_ilen
= sizeof(diag_501
);
830 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
832 sw_bp_inst
= instr_0x0000
;
833 sw_bp_ilen
= sizeof(instr_0x0000
);
834 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
838 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
840 determine_sw_breakpoint_instr();
842 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
844 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)sw_bp_inst
, sw_bp_ilen
, 1)) {
850 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
854 if (cpu_memory_rw_debug(cs
, bp
->pc
, t
, sw_bp_ilen
, 0)) {
856 } else if (memcmp(t
, sw_bp_inst
, sw_bp_ilen
)) {
858 } else if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
866 static struct kvm_hw_breakpoint
*find_hw_breakpoint(target_ulong addr
,
871 for (n
= 0; n
< nb_hw_breakpoints
; n
++) {
872 if (hw_breakpoints
[n
].addr
== addr
&& hw_breakpoints
[n
].type
== type
&&
873 (hw_breakpoints
[n
].len
== len
|| len
== -1)) {
874 return &hw_breakpoints
[n
];
881 static int insert_hw_breakpoint(target_ulong addr
, int len
, int type
)
885 if (find_hw_breakpoint(addr
, len
, type
)) {
889 size
= (nb_hw_breakpoints
+ 1) * sizeof(struct kvm_hw_breakpoint
);
891 if (!hw_breakpoints
) {
892 nb_hw_breakpoints
= 0;
893 hw_breakpoints
= (struct kvm_hw_breakpoint
*)g_try_malloc(size
);
896 (struct kvm_hw_breakpoint
*)g_try_realloc(hw_breakpoints
, size
);
899 if (!hw_breakpoints
) {
900 nb_hw_breakpoints
= 0;
904 hw_breakpoints
[nb_hw_breakpoints
].addr
= addr
;
905 hw_breakpoints
[nb_hw_breakpoints
].len
= len
;
906 hw_breakpoints
[nb_hw_breakpoints
].type
= type
;
913 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
914 target_ulong len
, int type
)
917 case GDB_BREAKPOINT_HW
:
920 case GDB_WATCHPOINT_WRITE
:
924 type
= KVM_HW_WP_WRITE
;
929 return insert_hw_breakpoint(addr
, len
, type
);
932 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
933 target_ulong len
, int type
)
936 struct kvm_hw_breakpoint
*bp
= find_hw_breakpoint(addr
, len
, type
);
943 if (nb_hw_breakpoints
> 0) {
945 * In order to trim the array, move the last element to the position to
946 * be removed - if necessary.
948 if (bp
!= &hw_breakpoints
[nb_hw_breakpoints
]) {
949 *bp
= hw_breakpoints
[nb_hw_breakpoints
];
951 size
= nb_hw_breakpoints
* sizeof(struct kvm_hw_breakpoint
);
953 (struct kvm_hw_breakpoint
*)g_realloc(hw_breakpoints
, size
);
955 g_free(hw_breakpoints
);
956 hw_breakpoints
= NULL
;
962 void kvm_arch_remove_all_hw_breakpoints(void)
964 nb_hw_breakpoints
= 0;
965 g_free(hw_breakpoints
);
966 hw_breakpoints
= NULL
;
969 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
973 if (nb_hw_breakpoints
> 0) {
974 dbg
->arch
.nr_hw_bp
= nb_hw_breakpoints
;
975 dbg
->arch
.hw_bp
= hw_breakpoints
;
977 for (i
= 0; i
< nb_hw_breakpoints
; ++i
) {
978 hw_breakpoints
[i
].phys_addr
= s390_cpu_get_phys_addr_debug(cpu
,
979 hw_breakpoints
[i
].addr
);
981 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
983 dbg
->arch
.nr_hw_bp
= 0;
984 dbg
->arch
.hw_bp
= NULL
;
988 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
992 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
994 return MEMTXATTRS_UNSPECIFIED
;
997 int kvm_arch_process_async_events(CPUState
*cs
)
1002 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq
*irq
,
1003 struct kvm_s390_interrupt
*interrupt
)
1007 interrupt
->type
= irq
->type
;
1008 switch (irq
->type
) {
1009 case KVM_S390_INT_VIRTIO
:
1010 interrupt
->parm
= irq
->u
.ext
.ext_params
;
1012 case KVM_S390_INT_PFAULT_INIT
:
1013 case KVM_S390_INT_PFAULT_DONE
:
1014 interrupt
->parm64
= irq
->u
.ext
.ext_params2
;
1016 case KVM_S390_PROGRAM_INT
:
1017 interrupt
->parm
= irq
->u
.pgm
.code
;
1019 case KVM_S390_SIGP_SET_PREFIX
:
1020 interrupt
->parm
= irq
->u
.prefix
.address
;
1022 case KVM_S390_INT_SERVICE
:
1023 interrupt
->parm
= irq
->u
.ext
.ext_params
;
1026 interrupt
->parm
= irq
->u
.mchk
.cr14
;
1027 interrupt
->parm64
= irq
->u
.mchk
.mcic
;
1029 case KVM_S390_INT_EXTERNAL_CALL
:
1030 interrupt
->parm
= irq
->u
.extcall
.code
;
1032 case KVM_S390_INT_EMERGENCY
:
1033 interrupt
->parm
= irq
->u
.emerg
.code
;
1035 case KVM_S390_SIGP_STOP
:
1036 case KVM_S390_RESTART
:
1037 break; /* These types have no parameters */
1038 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1039 interrupt
->parm
= irq
->u
.io
.subchannel_id
<< 16;
1040 interrupt
->parm
|= irq
->u
.io
.subchannel_nr
;
1041 interrupt
->parm64
= (uint64_t)irq
->u
.io
.io_int_parm
<< 32;
1042 interrupt
->parm64
|= irq
->u
.io
.io_int_word
;
1051 static void inject_vcpu_irq_legacy(CPUState
*cs
, struct kvm_s390_irq
*irq
)
1053 struct kvm_s390_interrupt kvmint
= {};
1056 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
1058 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
1062 r
= kvm_vcpu_ioctl(cs
, KVM_S390_INTERRUPT
, &kvmint
);
1064 fprintf(stderr
, "KVM failed to inject interrupt\n");
1069 void kvm_s390_vcpu_interrupt(S390CPU
*cpu
, struct kvm_s390_irq
*irq
)
1071 CPUState
*cs
= CPU(cpu
);
1075 r
= kvm_vcpu_ioctl(cs
, KVM_S390_IRQ
, irq
);
1079 error_report("KVM failed to inject interrupt %llx", irq
->type
);
1083 inject_vcpu_irq_legacy(cs
, irq
);
1086 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq
*irq
)
1088 struct kvm_s390_interrupt kvmint
= {};
1091 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
1093 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
1097 r
= kvm_vm_ioctl(kvm_state
, KVM_S390_INTERRUPT
, &kvmint
);
1099 fprintf(stderr
, "KVM failed to inject interrupt\n");
1104 void kvm_s390_program_interrupt(S390CPU
*cpu
, uint16_t code
)
1106 struct kvm_s390_irq irq
= {
1107 .type
= KVM_S390_PROGRAM_INT
,
1110 qemu_log_mask(CPU_LOG_INT
, "program interrupt at %#" PRIx64
"\n",
1112 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1115 void kvm_s390_access_exception(S390CPU
*cpu
, uint16_t code
, uint64_t te_code
)
1117 struct kvm_s390_irq irq
= {
1118 .type
= KVM_S390_PROGRAM_INT
,
1120 .u
.pgm
.trans_exc_code
= te_code
,
1121 .u
.pgm
.exc_access_id
= te_code
& 3,
1124 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1127 static int kvm_sclp_service_call(S390CPU
*cpu
, struct kvm_run
*run
,
1130 CPUS390XState
*env
= &cpu
->env
;
1135 sccb
= env
->regs
[ipbh0
& 0xf];
1136 code
= env
->regs
[(ipbh0
& 0xf0) >> 4];
1138 r
= sclp_service_call(env
, sccb
, code
);
1140 kvm_s390_program_interrupt(cpu
, -r
);
1148 static int handle_b2(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1150 CPUS390XState
*env
= &cpu
->env
;
1152 uint16_t ipbh0
= (run
->s390_sieic
.ipb
& 0xffff0000) >> 16;
1156 ioinst_handle_xsch(cpu
, env
->regs
[1], RA_IGNORED
);
1159 ioinst_handle_csch(cpu
, env
->regs
[1], RA_IGNORED
);
1162 ioinst_handle_hsch(cpu
, env
->regs
[1], RA_IGNORED
);
1165 ioinst_handle_msch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1168 ioinst_handle_ssch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1171 ioinst_handle_stcrw(cpu
, run
->s390_sieic
.ipb
, RA_IGNORED
);
1174 ioinst_handle_stsch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1177 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1178 fprintf(stderr
, "Spurious tsch intercept\n");
1181 ioinst_handle_chsc(cpu
, run
->s390_sieic
.ipb
, RA_IGNORED
);
1184 /* This should have been handled by kvm already. */
1185 fprintf(stderr
, "Spurious tpi intercept\n");
1188 ioinst_handle_schm(cpu
, env
->regs
[1], env
->regs
[2],
1189 run
->s390_sieic
.ipb
, RA_IGNORED
);
1192 ioinst_handle_rsch(cpu
, env
->regs
[1], RA_IGNORED
);
1195 ioinst_handle_rchp(cpu
, env
->regs
[1], RA_IGNORED
);
1198 /* We do not provide this instruction, it is suppressed. */
1201 ioinst_handle_sal(cpu
, env
->regs
[1], RA_IGNORED
);
1204 /* Not provided, set CC = 3 for subchannel not operational */
1207 case PRIV_B2_SCLP_CALL
:
1208 rc
= kvm_sclp_service_call(cpu
, run
, ipbh0
);
1212 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1
);
1219 static uint64_t get_base_disp_rxy(S390CPU
*cpu
, struct kvm_run
*run
,
1222 CPUS390XState
*env
= &cpu
->env
;
1223 uint32_t x2
= (run
->s390_sieic
.ipa
& 0x000f);
1224 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1225 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1226 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1228 if (disp2
& 0x80000) {
1229 disp2
+= 0xfff00000;
1235 return (base2
? env
->regs
[base2
] : 0) +
1236 (x2
? env
->regs
[x2
] : 0) + (long)(int)disp2
;
1239 static uint64_t get_base_disp_rsy(S390CPU
*cpu
, struct kvm_run
*run
,
1242 CPUS390XState
*env
= &cpu
->env
;
1243 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1244 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1245 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1247 if (disp2
& 0x80000) {
1248 disp2
+= 0xfff00000;
1254 return (base2
? env
->regs
[base2
] : 0) + (long)(int)disp2
;
1257 static int kvm_clp_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1259 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1261 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1262 return clp_service_call(cpu
, r2
, RA_IGNORED
);
1268 static int kvm_pcilg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1270 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1271 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1273 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1274 return pcilg_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1280 static int kvm_pcistg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1282 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1283 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1285 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1286 return pcistg_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1292 static int kvm_stpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1294 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1298 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1299 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1301 return stpcifc_service_call(cpu
, r1
, fiba
, ar
, RA_IGNORED
);
1307 static int kvm_sic_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1309 CPUS390XState
*env
= &cpu
->env
;
1310 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1311 uint8_t r3
= run
->s390_sieic
.ipa
& 0x000f;
1316 mode
= env
->regs
[r1
] & 0xffff;
1317 isc
= (env
->regs
[r3
] >> 27) & 0x7;
1318 r
= css_do_sic(env
, isc
, mode
);
1320 kvm_s390_program_interrupt(cpu
, -r
);
1326 static int kvm_rpcit_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1328 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1329 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1331 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1332 return rpcit_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1338 static int kvm_pcistb_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1340 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1341 uint8_t r3
= run
->s390_sieic
.ipa
& 0x000f;
1345 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1346 gaddr
= get_base_disp_rsy(cpu
, run
, &ar
);
1348 return pcistb_service_call(cpu
, r1
, r3
, gaddr
, ar
, RA_IGNORED
);
1354 static int kvm_mpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1356 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1360 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1361 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1363 return mpcifc_service_call(cpu
, r1
, fiba
, ar
, RA_IGNORED
);
1369 static int handle_b9(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1375 r
= kvm_clp_service_call(cpu
, run
);
1377 case PRIV_B9_PCISTG
:
1378 r
= kvm_pcistg_service_call(cpu
, run
);
1381 r
= kvm_pcilg_service_call(cpu
, run
);
1384 r
= kvm_rpcit_service_call(cpu
, run
);
1387 /* just inject exception */
1392 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1
);
1399 static int handle_eb(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1404 case PRIV_EB_PCISTB
:
1405 r
= kvm_pcistb_service_call(cpu
, run
);
1408 r
= kvm_sic_service_call(cpu
, run
);
1411 /* just inject exception */
1416 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl
);
1423 static int handle_e3(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1428 case PRIV_E3_MPCIFC
:
1429 r
= kvm_mpcifc_service_call(cpu
, run
);
1431 case PRIV_E3_STPCIFC
:
1432 r
= kvm_stpcifc_service_call(cpu
, run
);
1436 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl
);
1443 static int handle_hypercall(S390CPU
*cpu
, struct kvm_run
*run
)
1445 CPUS390XState
*env
= &cpu
->env
;
1448 ret
= s390_virtio_hypercall(env
);
1449 if (ret
== -EINVAL
) {
1450 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1457 static void kvm_handle_diag_288(S390CPU
*cpu
, struct kvm_run
*run
)
1462 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1463 r3
= run
->s390_sieic
.ipa
& 0x000f;
1464 rc
= handle_diag_288(&cpu
->env
, r1
, r3
);
1466 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1470 static void kvm_handle_diag_308(S390CPU
*cpu
, struct kvm_run
*run
)
1474 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1475 r3
= run
->s390_sieic
.ipa
& 0x000f;
1476 handle_diag_308(&cpu
->env
, r1
, r3
, RA_IGNORED
);
1479 static int handle_sw_breakpoint(S390CPU
*cpu
, struct kvm_run
*run
)
1481 CPUS390XState
*env
= &cpu
->env
;
1484 pc
= env
->psw
.addr
- sw_bp_ilen
;
1485 if (kvm_find_sw_breakpoint(CPU(cpu
), pc
)) {
1493 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1495 static int handle_diag(S390CPU
*cpu
, struct kvm_run
*run
, uint32_t ipb
)
1501 * For any diagnose call we support, bits 48-63 of the resulting
1502 * address specify the function code; the remainder is ignored.
1504 func_code
= decode_basedisp_rs(&cpu
->env
, ipb
, NULL
) & DIAG_KVM_CODE_MASK
;
1505 switch (func_code
) {
1506 case DIAG_TIMEREVENT
:
1507 kvm_handle_diag_288(cpu
, run
);
1510 kvm_handle_diag_308(cpu
, run
);
1512 case DIAG_KVM_HYPERCALL
:
1513 r
= handle_hypercall(cpu
, run
);
1515 case DIAG_KVM_BREAKPOINT
:
1516 r
= handle_sw_breakpoint(cpu
, run
);
1519 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code
);
1520 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1527 static int kvm_s390_handle_sigp(S390CPU
*cpu
, uint8_t ipa1
, uint32_t ipb
)
1529 CPUS390XState
*env
= &cpu
->env
;
1530 const uint8_t r1
= ipa1
>> 4;
1531 const uint8_t r3
= ipa1
& 0x0f;
1535 /* get order code */
1536 order
= decode_basedisp_rs(env
, ipb
, NULL
) & SIGP_ORDER_MASK
;
1538 ret
= handle_sigp(env
, order
, r1
, r3
);
1543 static int handle_instruction(S390CPU
*cpu
, struct kvm_run
*run
)
1545 unsigned int ipa0
= (run
->s390_sieic
.ipa
& 0xff00);
1546 uint8_t ipa1
= run
->s390_sieic
.ipa
& 0x00ff;
1549 DPRINTF("handle_instruction 0x%x 0x%x\n",
1550 run
->s390_sieic
.ipa
, run
->s390_sieic
.ipb
);
1553 r
= handle_b2(cpu
, run
, ipa1
);
1556 r
= handle_b9(cpu
, run
, ipa1
);
1559 r
= handle_eb(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1562 r
= handle_e3(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1565 r
= handle_diag(cpu
, run
, run
->s390_sieic
.ipb
);
1568 r
= kvm_s390_handle_sigp(cpu
, ipa1
, run
->s390_sieic
.ipb
);
1574 kvm_s390_program_interrupt(cpu
, PGM_OPERATION
);
1580 static void unmanageable_intercept(S390CPU
*cpu
, S390CrashReason reason
,
1583 CPUState
*cs
= CPU(cpu
);
1586 cpu
->env
.crash_reason
= reason
;
1587 qemu_system_guest_panicked(cpu_get_crash_info(cs
));
1590 /* try to detect pgm check loops */
1591 static int handle_oper_loop(S390CPU
*cpu
, struct kvm_run
*run
)
1593 CPUState
*cs
= CPU(cpu
);
1596 newpsw
.mask
= ldq_phys(cs
->as
, cpu
->env
.psa
+
1597 offsetof(LowCore
, program_new_psw
));
1598 newpsw
.addr
= ldq_phys(cs
->as
, cpu
->env
.psa
+
1599 offsetof(LowCore
, program_new_psw
) + 8);
1600 oldpsw
.mask
= run
->psw_mask
;
1601 oldpsw
.addr
= run
->psw_addr
;
1603 * Avoid endless loops of operation exceptions, if the pgm new
1604 * PSW will cause a new operation exception.
1605 * The heuristic checks if the pgm new psw is within 6 bytes before
1606 * the faulting psw address (with same DAT, AS settings) and the
1607 * new psw is not a wait psw and the fault was not triggered by
1608 * problem state. In that case go into crashed state.
1611 if (oldpsw
.addr
- newpsw
.addr
<= 6 &&
1612 !(newpsw
.mask
& PSW_MASK_WAIT
) &&
1613 !(oldpsw
.mask
& PSW_MASK_PSTATE
) &&
1614 (newpsw
.mask
& PSW_MASK_ASC
) == (oldpsw
.mask
& PSW_MASK_ASC
) &&
1615 (newpsw
.mask
& PSW_MASK_DAT
) == (oldpsw
.mask
& PSW_MASK_DAT
)) {
1616 unmanageable_intercept(cpu
, S390_CRASH_REASON_OPINT_LOOP
,
1617 offsetof(LowCore
, program_new_psw
));
1623 static int handle_intercept(S390CPU
*cpu
)
1625 CPUState
*cs
= CPU(cpu
);
1626 struct kvm_run
*run
= cs
->kvm_run
;
1627 int icpt_code
= run
->s390_sieic
.icptcode
;
1630 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code
,
1631 (long)cs
->kvm_run
->psw_addr
);
1632 switch (icpt_code
) {
1633 case ICPT_INSTRUCTION
:
1634 r
= handle_instruction(cpu
, run
);
1637 unmanageable_intercept(cpu
, S390_CRASH_REASON_PGMINT_LOOP
,
1638 offsetof(LowCore
, program_new_psw
));
1642 unmanageable_intercept(cpu
, S390_CRASH_REASON_EXTINT_LOOP
,
1643 offsetof(LowCore
, external_new_psw
));
1647 /* disabled wait, since enabled wait is handled in kernel */
1648 s390_handle_wait(cpu
);
1652 do_stop_interrupt(&cpu
->env
);
1656 /* check for break points */
1657 r
= handle_sw_breakpoint(cpu
, run
);
1659 /* Then check for potential pgm check loops */
1660 r
= handle_oper_loop(cpu
, run
);
1662 kvm_s390_program_interrupt(cpu
, PGM_OPERATION
);
1666 case ICPT_SOFT_INTERCEPT
:
1667 fprintf(stderr
, "KVM unimplemented icpt SOFT\n");
1671 fprintf(stderr
, "KVM unimplemented icpt IO\n");
1675 fprintf(stderr
, "Unknown intercept code: %d\n", icpt_code
);
1683 static int handle_tsch(S390CPU
*cpu
)
1685 CPUState
*cs
= CPU(cpu
);
1686 struct kvm_run
*run
= cs
->kvm_run
;
1689 ret
= ioinst_handle_tsch(cpu
, cpu
->env
.regs
[1], run
->s390_tsch
.ipb
,
1694 * If an I/O interrupt had been dequeued, we have to reinject it.
1696 if (run
->s390_tsch
.dequeued
) {
1697 s390_io_interrupt(run
->s390_tsch
.subchannel_id
,
1698 run
->s390_tsch
.subchannel_nr
,
1699 run
->s390_tsch
.io_int_parm
,
1700 run
->s390_tsch
.io_int_word
);
1707 static void insert_stsi_3_2_2(S390CPU
*cpu
, __u64 addr
, uint8_t ar
)
1712 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &sysib
, sizeof(sysib
))) {
1715 /* Shift the stack of Extended Names to prepare for our own data */
1716 memmove(&sysib
.ext_names
[1], &sysib
.ext_names
[0],
1717 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- 1));
1718 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1719 * assumed it's not capable of managing Extended Names for lower levels.
1721 for (del
= 1; del
< sysib
.count
; del
++) {
1722 if (!sysib
.vm
[del
].ext_name_encoding
|| !sysib
.ext_names
[del
][0]) {
1726 if (del
< sysib
.count
) {
1727 memset(sysib
.ext_names
[del
], 0,
1728 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- del
));
1730 /* Insert short machine name in EBCDIC, padded with blanks */
1732 memset(sysib
.vm
[0].name
, 0x40, sizeof(sysib
.vm
[0].name
));
1733 ebcdic_put(sysib
.vm
[0].name
, qemu_name
, MIN(sizeof(sysib
.vm
[0].name
),
1734 strlen(qemu_name
)));
1736 sysib
.vm
[0].ext_name_encoding
= 2; /* 2 = UTF-8 */
1737 memset(sysib
.ext_names
[0], 0, sizeof(sysib
.ext_names
[0]));
1738 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1739 * considered by s390 as not capable of providing any Extended Name.
1740 * Therefore if no name was specified on qemu invocation, we go with the
1741 * same "KVMguest" default, which KVM has filled into short name field.
1744 strncpy((char *)sysib
.ext_names
[0], qemu_name
,
1745 sizeof(sysib
.ext_names
[0]));
1747 strcpy((char *)sysib
.ext_names
[0], "KVMguest");
1750 memcpy(sysib
.vm
[0].uuid
, &qemu_uuid
, sizeof(sysib
.vm
[0].uuid
));
1752 s390_cpu_virt_mem_write(cpu
, addr
, ar
, &sysib
, sizeof(sysib
));
1755 static int handle_stsi(S390CPU
*cpu
)
1757 CPUState
*cs
= CPU(cpu
);
1758 struct kvm_run
*run
= cs
->kvm_run
;
1760 switch (run
->s390_stsi
.fc
) {
1762 if (run
->s390_stsi
.sel1
!= 2 || run
->s390_stsi
.sel2
!= 2) {
1765 /* Only sysib 3.2.2 needs post-handling for now. */
1766 insert_stsi_3_2_2(cpu
, run
->s390_stsi
.addr
, run
->s390_stsi
.ar
);
1773 static int kvm_arch_handle_debug_exit(S390CPU
*cpu
)
1775 CPUState
*cs
= CPU(cpu
);
1776 struct kvm_run
*run
= cs
->kvm_run
;
1779 struct kvm_debug_exit_arch
*arch_info
= &run
->debug
.arch
;
1781 switch (arch_info
->type
) {
1782 case KVM_HW_WP_WRITE
:
1783 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1784 cs
->watchpoint_hit
= &hw_watchpoint
;
1785 hw_watchpoint
.vaddr
= arch_info
->addr
;
1786 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1791 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1795 case KVM_SINGLESTEP
:
1796 if (cs
->singlestep_enabled
) {
1807 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
1809 S390CPU
*cpu
= S390_CPU(cs
);
1812 qemu_mutex_lock_iothread();
1814 kvm_cpu_synchronize_state(cs
);
1816 switch (run
->exit_reason
) {
1817 case KVM_EXIT_S390_SIEIC
:
1818 ret
= handle_intercept(cpu
);
1820 case KVM_EXIT_S390_RESET
:
1821 s390_ipl_reset_request(cs
, S390_RESET_REIPL
);
1823 case KVM_EXIT_S390_TSCH
:
1824 ret
= handle_tsch(cpu
);
1826 case KVM_EXIT_S390_STSI
:
1827 ret
= handle_stsi(cpu
);
1829 case KVM_EXIT_DEBUG
:
1830 ret
= kvm_arch_handle_debug_exit(cpu
);
1833 fprintf(stderr
, "Unknown KVM exit: %d\n", run
->exit_reason
);
1836 qemu_mutex_unlock_iothread();
1839 ret
= EXCP_INTERRUPT
;
1844 bool kvm_arch_stop_on_emulation_error(CPUState
*cpu
)
1849 void kvm_s390_enable_css_support(S390CPU
*cpu
)
1853 /* Activate host kernel channel subsystem support. */
1854 r
= kvm_vcpu_enable_cap(CPU(cpu
), KVM_CAP_S390_CSS_SUPPORT
, 0);
1858 void kvm_arch_init_irq_routing(KVMState
*s
)
1861 * Note that while irqchip capabilities generally imply that cpustates
1862 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1863 * have to override the common code kvm_halt_in_kernel_allowed setting.
1865 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
1866 kvm_gsi_routing_allowed
= true;
1867 kvm_halt_in_kernel_allowed
= false;
1871 int kvm_s390_assign_subch_ioeventfd(EventNotifier
*notifier
, uint32_t sch
,
1872 int vq
, bool assign
)
1874 struct kvm_ioeventfd kick
= {
1875 .flags
= KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY
|
1876 KVM_IOEVENTFD_FLAG_DATAMATCH
,
1877 .fd
= event_notifier_get_fd(notifier
),
1882 trace_kvm_assign_subch_ioeventfd(kick
.fd
, kick
.addr
, assign
,
1884 if (!kvm_check_extension(kvm_state
, KVM_CAP_IOEVENTFD
)) {
1888 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1890 return kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
1893 int kvm_s390_get_ri(void)
1898 int kvm_s390_get_gs(void)
1903 int kvm_s390_set_cpu_state(S390CPU
*cpu
, uint8_t cpu_state
)
1905 struct kvm_mp_state mp_state
= {};
1908 /* the kvm part might not have been initialized yet */
1909 if (CPU(cpu
)->kvm_state
== NULL
) {
1913 switch (cpu_state
) {
1914 case S390_CPU_STATE_STOPPED
:
1915 mp_state
.mp_state
= KVM_MP_STATE_STOPPED
;
1917 case S390_CPU_STATE_CHECK_STOP
:
1918 mp_state
.mp_state
= KVM_MP_STATE_CHECK_STOP
;
1920 case S390_CPU_STATE_OPERATING
:
1921 mp_state
.mp_state
= KVM_MP_STATE_OPERATING
;
1923 case S390_CPU_STATE_LOAD
:
1924 mp_state
.mp_state
= KVM_MP_STATE_LOAD
;
1927 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1932 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
1934 trace_kvm_failed_cpu_state_set(CPU(cpu
)->cpu_index
, cpu_state
,
1941 void kvm_s390_vcpu_interrupt_pre_save(S390CPU
*cpu
)
1943 struct kvm_s390_irq_state irq_state
= {
1944 .buf
= (uint64_t) cpu
->irqstate
,
1945 .len
= VCPU_IRQ_BUF_SIZE
,
1947 CPUState
*cs
= CPU(cpu
);
1950 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_IRQ_STATE
)) {
1954 bytes
= kvm_vcpu_ioctl(cs
, KVM_S390_GET_IRQ_STATE
, &irq_state
);
1956 cpu
->irqstate_saved_size
= 0;
1957 error_report("Migration of interrupt state failed");
1961 cpu
->irqstate_saved_size
= bytes
;
1964 int kvm_s390_vcpu_interrupt_post_load(S390CPU
*cpu
)
1966 CPUState
*cs
= CPU(cpu
);
1967 struct kvm_s390_irq_state irq_state
= {
1968 .buf
= (uint64_t) cpu
->irqstate
,
1969 .len
= cpu
->irqstate_saved_size
,
1973 if (cpu
->irqstate_saved_size
== 0) {
1977 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_IRQ_STATE
)) {
1981 r
= kvm_vcpu_ioctl(cs
, KVM_S390_SET_IRQ_STATE
, &irq_state
);
1983 error_report("Setting interrupt state failed %d", r
);
1988 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
1989 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
1991 S390PCIBusDevice
*pbdev
;
1992 uint32_t vec
= data
& ZPCI_MSI_VEC_MASK
;
1995 DPRINTF("add_msi_route no pci device\n");
1999 pbdev
= s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev
)->id
);
2001 DPRINTF("add_msi_route no zpci device\n");
2005 route
->type
= KVM_IRQ_ROUTING_S390_ADAPTER
;
2007 route
->u
.adapter
.summary_addr
= pbdev
->routes
.adapter
.summary_addr
;
2008 route
->u
.adapter
.ind_addr
= pbdev
->routes
.adapter
.ind_addr
;
2009 route
->u
.adapter
.summary_offset
= pbdev
->routes
.adapter
.summary_offset
;
2010 route
->u
.adapter
.ind_offset
= pbdev
->routes
.adapter
.ind_offset
+ vec
;
2011 route
->u
.adapter
.adapter_id
= pbdev
->routes
.adapter
.adapter_id
;
2015 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
2016 int vector
, PCIDevice
*dev
)
2021 int kvm_arch_release_virq_post(int virq
)
2026 int kvm_arch_msi_data_to_gsi(uint32_t data
)
2031 static int query_cpu_subfunc(S390FeatBitmap features
)
2033 struct kvm_s390_vm_cpu_subfunc prop
;
2034 struct kvm_device_attr attr
= {
2035 .group
= KVM_S390_VM_CPU_MODEL
,
2036 .attr
= KVM_S390_VM_CPU_MACHINE_SUBFUNC
,
2037 .addr
= (uint64_t) &prop
,
2041 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2047 * We're going to add all subfunctions now, if the corresponding feature
2048 * is available that unlocks the query functions.
2050 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PLO
, prop
.plo
);
2051 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING
, features
)) {
2052 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PTFF
, prop
.ptff
);
2054 if (test_bit(S390_FEAT_MSA
, features
)) {
2055 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMAC
, prop
.kmac
);
2056 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMC
, prop
.kmc
);
2057 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KM
, prop
.km
);
2058 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KIMD
, prop
.kimd
);
2059 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KLMD
, prop
.klmd
);
2061 if (test_bit(S390_FEAT_MSA_EXT_3
, features
)) {
2062 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PCKMO
, prop
.pckmo
);
2064 if (test_bit(S390_FEAT_MSA_EXT_4
, features
)) {
2065 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMCTR
, prop
.kmctr
);
2066 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMF
, prop
.kmf
);
2067 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMO
, prop
.kmo
);
2068 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PCC
, prop
.pcc
);
2070 if (test_bit(S390_FEAT_MSA_EXT_5
, features
)) {
2071 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PPNO
, prop
.ppno
);
2073 if (test_bit(S390_FEAT_MSA_EXT_8
, features
)) {
2074 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMA
, prop
.kma
);
2076 if (test_bit(S390_FEAT_MSA_EXT_9
, features
)) {
2077 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KDSA
, prop
.kdsa
);
2079 if (test_bit(S390_FEAT_ESORT_BASE
, features
)) {
2080 s390_add_from_feat_block(features
, S390_FEAT_TYPE_SORTL
, prop
.sortl
);
2082 if (test_bit(S390_FEAT_DEFLATE_BASE
, features
)) {
2083 s390_add_from_feat_block(features
, S390_FEAT_TYPE_DFLTCC
, prop
.dfltcc
);
2088 static int configure_cpu_subfunc(const S390FeatBitmap features
)
2090 struct kvm_s390_vm_cpu_subfunc prop
= {};
2091 struct kvm_device_attr attr
= {
2092 .group
= KVM_S390_VM_CPU_MODEL
,
2093 .attr
= KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
,
2094 .addr
= (uint64_t) &prop
,
2097 if (!kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2098 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
)) {
2099 /* hardware support might be missing, IBC will handle most of this */
2103 s390_fill_feat_block(features
, S390_FEAT_TYPE_PLO
, prop
.plo
);
2104 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING
, features
)) {
2105 s390_fill_feat_block(features
, S390_FEAT_TYPE_PTFF
, prop
.ptff
);
2107 if (test_bit(S390_FEAT_MSA
, features
)) {
2108 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMAC
, prop
.kmac
);
2109 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMC
, prop
.kmc
);
2110 s390_fill_feat_block(features
, S390_FEAT_TYPE_KM
, prop
.km
);
2111 s390_fill_feat_block(features
, S390_FEAT_TYPE_KIMD
, prop
.kimd
);
2112 s390_fill_feat_block(features
, S390_FEAT_TYPE_KLMD
, prop
.klmd
);
2114 if (test_bit(S390_FEAT_MSA_EXT_3
, features
)) {
2115 s390_fill_feat_block(features
, S390_FEAT_TYPE_PCKMO
, prop
.pckmo
);
2117 if (test_bit(S390_FEAT_MSA_EXT_4
, features
)) {
2118 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMCTR
, prop
.kmctr
);
2119 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMF
, prop
.kmf
);
2120 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMO
, prop
.kmo
);
2121 s390_fill_feat_block(features
, S390_FEAT_TYPE_PCC
, prop
.pcc
);
2123 if (test_bit(S390_FEAT_MSA_EXT_5
, features
)) {
2124 s390_fill_feat_block(features
, S390_FEAT_TYPE_PPNO
, prop
.ppno
);
2126 if (test_bit(S390_FEAT_MSA_EXT_8
, features
)) {
2127 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMA
, prop
.kma
);
2129 if (test_bit(S390_FEAT_MSA_EXT_9
, features
)) {
2130 s390_fill_feat_block(features
, S390_FEAT_TYPE_KDSA
, prop
.kdsa
);
2132 if (test_bit(S390_FEAT_ESORT_BASE
, features
)) {
2133 s390_fill_feat_block(features
, S390_FEAT_TYPE_SORTL
, prop
.sortl
);
2135 if (test_bit(S390_FEAT_DEFLATE_BASE
, features
)) {
2136 s390_fill_feat_block(features
, S390_FEAT_TYPE_DFLTCC
, prop
.dfltcc
);
2138 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2141 static int kvm_to_feat
[][2] = {
2142 { KVM_S390_VM_CPU_FEAT_ESOP
, S390_FEAT_ESOP
},
2143 { KVM_S390_VM_CPU_FEAT_SIEF2
, S390_FEAT_SIE_F2
},
2144 { KVM_S390_VM_CPU_FEAT_64BSCAO
, S390_FEAT_SIE_64BSCAO
},
2145 { KVM_S390_VM_CPU_FEAT_SIIF
, S390_FEAT_SIE_SIIF
},
2146 { KVM_S390_VM_CPU_FEAT_GPERE
, S390_FEAT_SIE_GPERE
},
2147 { KVM_S390_VM_CPU_FEAT_GSLS
, S390_FEAT_SIE_GSLS
},
2148 { KVM_S390_VM_CPU_FEAT_IB
, S390_FEAT_SIE_IB
},
2149 { KVM_S390_VM_CPU_FEAT_CEI
, S390_FEAT_SIE_CEI
},
2150 { KVM_S390_VM_CPU_FEAT_IBS
, S390_FEAT_SIE_IBS
},
2151 { KVM_S390_VM_CPU_FEAT_SKEY
, S390_FEAT_SIE_SKEY
},
2152 { KVM_S390_VM_CPU_FEAT_CMMA
, S390_FEAT_SIE_CMMA
},
2153 { KVM_S390_VM_CPU_FEAT_PFMFI
, S390_FEAT_SIE_PFMFI
},
2154 { KVM_S390_VM_CPU_FEAT_SIGPIF
, S390_FEAT_SIE_SIGPIF
},
2155 { KVM_S390_VM_CPU_FEAT_KSS
, S390_FEAT_SIE_KSS
},
2158 static int query_cpu_feat(S390FeatBitmap features
)
2160 struct kvm_s390_vm_cpu_feat prop
;
2161 struct kvm_device_attr attr
= {
2162 .group
= KVM_S390_VM_CPU_MODEL
,
2163 .attr
= KVM_S390_VM_CPU_MACHINE_FEAT
,
2164 .addr
= (uint64_t) &prop
,
2169 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2174 for (i
= 0; i
< ARRAY_SIZE(kvm_to_feat
); i
++) {
2175 if (test_be_bit(kvm_to_feat
[i
][0], (uint8_t *) prop
.feat
)) {
2176 set_bit(kvm_to_feat
[i
][1], features
);
2182 static int configure_cpu_feat(const S390FeatBitmap features
)
2184 struct kvm_s390_vm_cpu_feat prop
= {};
2185 struct kvm_device_attr attr
= {
2186 .group
= KVM_S390_VM_CPU_MODEL
,
2187 .attr
= KVM_S390_VM_CPU_PROCESSOR_FEAT
,
2188 .addr
= (uint64_t) &prop
,
2192 for (i
= 0; i
< ARRAY_SIZE(kvm_to_feat
); i
++) {
2193 if (test_bit(kvm_to_feat
[i
][1], features
)) {
2194 set_be_bit(kvm_to_feat
[i
][0], (uint8_t *) prop
.feat
);
2197 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2200 bool kvm_s390_cpu_models_supported(void)
2202 if (!cpu_model_allowed()) {
2203 /* compatibility machines interfere with the cpu model */
2206 return kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2207 KVM_S390_VM_CPU_MACHINE
) &&
2208 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2209 KVM_S390_VM_CPU_PROCESSOR
) &&
2210 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2211 KVM_S390_VM_CPU_MACHINE_FEAT
) &&
2212 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2213 KVM_S390_VM_CPU_PROCESSOR_FEAT
) &&
2214 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2215 KVM_S390_VM_CPU_MACHINE_SUBFUNC
);
2218 void kvm_s390_get_host_cpu_model(S390CPUModel
*model
, Error
**errp
)
2220 struct kvm_s390_vm_cpu_machine prop
= {};
2221 struct kvm_device_attr attr
= {
2222 .group
= KVM_S390_VM_CPU_MODEL
,
2223 .attr
= KVM_S390_VM_CPU_MACHINE
,
2224 .addr
= (uint64_t) &prop
,
2226 uint16_t unblocked_ibc
= 0, cpu_type
= 0;
2229 memset(model
, 0, sizeof(*model
));
2231 if (!kvm_s390_cpu_models_supported()) {
2232 error_setg(errp
, "KVM doesn't support CPU models");
2236 /* query the basic cpu model properties */
2237 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2239 error_setg(errp
, "KVM: Error querying host CPU model: %d", rc
);
2243 cpu_type
= cpuid_type(prop
.cpuid
);
2244 if (has_ibc(prop
.ibc
)) {
2245 model
->lowest_ibc
= lowest_ibc(prop
.ibc
);
2246 unblocked_ibc
= unblocked_ibc(prop
.ibc
);
2248 model
->cpu_id
= cpuid_id(prop
.cpuid
);
2249 model
->cpu_id_format
= cpuid_format(prop
.cpuid
);
2250 model
->cpu_ver
= 0xff;
2252 /* get supported cpu features indicated via STFL(E) */
2253 s390_add_from_feat_block(model
->features
, S390_FEAT_TYPE_STFL
,
2254 (uint8_t *) prop
.fac_mask
);
2255 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2256 if (test_bit(S390_FEAT_STFLE
, model
->features
)) {
2257 set_bit(S390_FEAT_DAT_ENH_2
, model
->features
);
2259 /* get supported cpu features indicated e.g. via SCLP */
2260 rc
= query_cpu_feat(model
->features
);
2262 error_setg(errp
, "KVM: Error querying CPU features: %d", rc
);
2265 /* get supported cpu subfunctions indicated via query / test bit */
2266 rc
= query_cpu_subfunc(model
->features
);
2268 error_setg(errp
, "KVM: Error querying CPU subfunctions: %d", rc
);
2272 /* PTFF subfunctions might be indicated although kernel support missing */
2273 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH
, model
->features
)) {
2274 clear_bit(S390_FEAT_PTFF_QSIE
, model
->features
);
2275 clear_bit(S390_FEAT_PTFF_QTOUE
, model
->features
);
2276 clear_bit(S390_FEAT_PTFF_STOE
, model
->features
);
2277 clear_bit(S390_FEAT_PTFF_STOUE
, model
->features
);
2280 /* with cpu model support, CMM is only indicated if really available */
2281 if (kvm_s390_cmma_available()) {
2282 set_bit(S390_FEAT_CMM
, model
->features
);
2284 /* no cmm -> no cmm nt */
2285 clear_bit(S390_FEAT_CMM_NT
, model
->features
);
2288 /* bpb needs kernel support for migration, VSIE and reset */
2289 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_BPB
)) {
2290 clear_bit(S390_FEAT_BPB
, model
->features
);
2293 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2294 set_bit(S390_FEAT_ZPCI
, model
->features
);
2295 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION
, model
->features
);
2297 if (s390_known_cpu_type(cpu_type
)) {
2298 /* we want the exact model, even if some features are missing */
2299 model
->def
= s390_find_cpu_def(cpu_type
, ibc_gen(unblocked_ibc
),
2300 ibc_ec_ga(unblocked_ibc
), NULL
);
2302 /* model unknown, e.g. too new - search using features */
2303 model
->def
= s390_find_cpu_def(0, ibc_gen(unblocked_ibc
),
2304 ibc_ec_ga(unblocked_ibc
),
2308 error_setg(errp
, "KVM: host CPU model could not be identified");
2311 /* for now, we can only provide the AP feature with HW support */
2312 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
,
2313 KVM_S390_VM_CRYPTO_ENABLE_APIE
)) {
2314 set_bit(S390_FEAT_AP
, model
->features
);
2316 /* strip of features that are not part of the maximum model */
2317 bitmap_and(model
->features
, model
->features
, model
->def
->full_feat
,
2321 static void kvm_s390_configure_apie(bool interpret
)
2323 uint64_t attr
= interpret
? KVM_S390_VM_CRYPTO_ENABLE_APIE
:
2324 KVM_S390_VM_CRYPTO_DISABLE_APIE
;
2326 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
2327 kvm_s390_set_attr(attr
);
2331 void kvm_s390_apply_cpu_model(const S390CPUModel
*model
, Error
**errp
)
2333 struct kvm_s390_vm_cpu_processor prop
= {
2336 struct kvm_device_attr attr
= {
2337 .group
= KVM_S390_VM_CPU_MODEL
,
2338 .attr
= KVM_S390_VM_CPU_PROCESSOR
,
2339 .addr
= (uint64_t) &prop
,
2344 /* compatibility handling if cpu models are disabled */
2345 if (kvm_s390_cmma_available()) {
2346 kvm_s390_enable_cmma();
2350 if (!kvm_s390_cpu_models_supported()) {
2351 error_setg(errp
, "KVM doesn't support CPU models");
2354 prop
.cpuid
= s390_cpuid_from_cpu_model(model
);
2355 prop
.ibc
= s390_ibc_from_cpu_model(model
);
2356 /* configure cpu features indicated via STFL(e) */
2357 s390_fill_feat_block(model
->features
, S390_FEAT_TYPE_STFL
,
2358 (uint8_t *) prop
.fac_list
);
2359 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2361 error_setg(errp
, "KVM: Error configuring the CPU model: %d", rc
);
2364 /* configure cpu features indicated e.g. via SCLP */
2365 rc
= configure_cpu_feat(model
->features
);
2367 error_setg(errp
, "KVM: Error configuring CPU features: %d", rc
);
2370 /* configure cpu subfunctions indicated via query / test bit */
2371 rc
= configure_cpu_subfunc(model
->features
);
2373 error_setg(errp
, "KVM: Error configuring CPU subfunctions: %d", rc
);
2376 /* enable CMM via CMMA */
2377 if (test_bit(S390_FEAT_CMM
, model
->features
)) {
2378 kvm_s390_enable_cmma();
2381 if (test_bit(S390_FEAT_AP
, model
->features
)) {
2382 kvm_s390_configure_apie(true);
2386 void kvm_s390_restart_interrupt(S390CPU
*cpu
)
2388 struct kvm_s390_irq irq
= {
2389 .type
= KVM_S390_RESTART
,
2392 kvm_s390_vcpu_interrupt(cpu
, &irq
);
2395 void kvm_s390_stop_interrupt(S390CPU
*cpu
)
2397 struct kvm_s390_irq irq
= {
2398 .type
= KVM_S390_SIGP_STOP
,
2401 kvm_s390_vcpu_interrupt(cpu
, &irq
);