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 "sysemu/kvm_int.h"
32 #include "qemu/cutils.h"
33 #include "qapi/error.h"
34 #include "qemu/error-report.h"
35 #include "qemu/timer.h"
36 #include "qemu/units.h"
37 #include "qemu/main-loop.h"
38 #include "qemu/mmap-alloc.h"
40 #include "sysemu/sysemu.h"
41 #include "sysemu/hw_accel.h"
42 #include "sysemu/runstate.h"
43 #include "sysemu/device_tree.h"
44 #include "exec/gdbstub.h"
45 #include "exec/ram_addr.h"
47 #include "hw/s390x/s390-pci-inst.h"
48 #include "hw/s390x/s390-pci-bus.h"
49 #include "hw/s390x/ipl.h"
50 #include "hw/s390x/ebcdic.h"
51 #include "exec/memattrs.h"
52 #include "hw/s390x/s390-virtio-ccw.h"
53 #include "hw/s390x/s390-virtio-hcall.h"
54 #include "hw/s390x/pv.h"
60 #define DPRINTF(fmt, ...) do { \
62 fprintf(stderr, fmt, ## __VA_ARGS__); \
66 #define kvm_vm_check_mem_attr(s, attr) \
67 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
69 #define IPA0_DIAG 0x8300
70 #define IPA0_SIGP 0xae00
71 #define IPA0_B2 0xb200
72 #define IPA0_B9 0xb900
73 #define IPA0_EB 0xeb00
74 #define IPA0_E3 0xe300
76 #define PRIV_B2_SCLP_CALL 0x20
77 #define PRIV_B2_CSCH 0x30
78 #define PRIV_B2_HSCH 0x31
79 #define PRIV_B2_MSCH 0x32
80 #define PRIV_B2_SSCH 0x33
81 #define PRIV_B2_STSCH 0x34
82 #define PRIV_B2_TSCH 0x35
83 #define PRIV_B2_TPI 0x36
84 #define PRIV_B2_SAL 0x37
85 #define PRIV_B2_RSCH 0x38
86 #define PRIV_B2_STCRW 0x39
87 #define PRIV_B2_STCPS 0x3a
88 #define PRIV_B2_RCHP 0x3b
89 #define PRIV_B2_SCHM 0x3c
90 #define PRIV_B2_CHSC 0x5f
91 #define PRIV_B2_SIGA 0x74
92 #define PRIV_B2_XSCH 0x76
94 #define PRIV_EB_SQBS 0x8a
95 #define PRIV_EB_PCISTB 0xd0
96 #define PRIV_EB_SIC 0xd1
98 #define PRIV_B9_EQBS 0x9c
99 #define PRIV_B9_CLP 0xa0
100 #define PRIV_B9_PCISTG 0xd0
101 #define PRIV_B9_PCILG 0xd2
102 #define PRIV_B9_RPCIT 0xd3
104 #define PRIV_E3_MPCIFC 0xd0
105 #define PRIV_E3_STPCIFC 0xd4
107 #define DIAG_TIMEREVENT 0x288
108 #define DIAG_IPL 0x308
109 #define DIAG_SET_CONTROL_PROGRAM_CODES 0x318
110 #define DIAG_KVM_HYPERCALL 0x500
111 #define DIAG_KVM_BREAKPOINT 0x501
113 #define ICPT_INSTRUCTION 0x04
114 #define ICPT_PROGRAM 0x08
115 #define ICPT_EXT_INT 0x14
116 #define ICPT_WAITPSW 0x1c
117 #define ICPT_SOFT_INTERCEPT 0x24
118 #define ICPT_CPU_STOP 0x28
119 #define ICPT_OPEREXC 0x2c
121 #define ICPT_PV_INSTR 0x68
122 #define ICPT_PV_INSTR_NOTIFICATION 0x6c
124 #define NR_LOCAL_IRQS 32
126 * Needs to be big enough to contain max_cpus emergency signals
127 * and in addition NR_LOCAL_IRQS interrupts
129 #define VCPU_IRQ_BUF_SIZE(max_cpus) (sizeof(struct kvm_s390_irq) * \
130 (max_cpus + NR_LOCAL_IRQS))
132 * KVM does only support memory slots up to KVM_MEM_MAX_NR_PAGES pages
133 * as the dirty bitmap must be managed by bitops that take an int as
134 * position indicator. This would end at an unaligned address
135 * (0x7fffff00000). As future variants might provide larger pages
136 * and to make all addresses properly aligned, let us split at 4TB.
138 #define KVM_SLOT_MAX_BYTES (4UL * TiB)
140 static CPUWatchpoint hw_watchpoint
;
142 * We don't use a list because this structure is also used to transmit the
143 * hardware breakpoints to the kernel.
145 static struct kvm_hw_breakpoint
*hw_breakpoints
;
146 static int nb_hw_breakpoints
;
148 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
152 static int cap_sync_regs
;
153 static int cap_async_pf
;
154 static int cap_mem_op
;
155 static int cap_s390_irq
;
158 static int cap_hpage_1m
;
159 static int cap_vcpu_resets
;
160 static int cap_protected
;
162 static int active_cmma
;
164 static void *legacy_s390_alloc(size_t size
, uint64_t *align
, bool shared
);
166 static int kvm_s390_query_mem_limit(uint64_t *memory_limit
)
168 struct kvm_device_attr attr
= {
169 .group
= KVM_S390_VM_MEM_CTRL
,
170 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
171 .addr
= (uint64_t) memory_limit
,
174 return kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
177 int kvm_s390_set_mem_limit(uint64_t new_limit
, uint64_t *hw_limit
)
181 struct kvm_device_attr attr
= {
182 .group
= KVM_S390_VM_MEM_CTRL
,
183 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
184 .addr
= (uint64_t) &new_limit
,
187 if (!kvm_vm_check_mem_attr(kvm_state
, KVM_S390_VM_MEM_LIMIT_SIZE
)) {
191 rc
= kvm_s390_query_mem_limit(hw_limit
);
194 } else if (*hw_limit
< new_limit
) {
198 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
201 int kvm_s390_cmma_active(void)
206 static bool kvm_s390_cmma_available(void)
208 static bool initialized
, value
;
212 value
= kvm_vm_check_mem_attr(kvm_state
, KVM_S390_VM_MEM_ENABLE_CMMA
) &&
213 kvm_vm_check_mem_attr(kvm_state
, KVM_S390_VM_MEM_CLR_CMMA
);
218 void kvm_s390_cmma_reset(void)
221 struct kvm_device_attr attr
= {
222 .group
= KVM_S390_VM_MEM_CTRL
,
223 .attr
= KVM_S390_VM_MEM_CLR_CMMA
,
226 if (!kvm_s390_cmma_active()) {
230 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
231 trace_kvm_clear_cmma(rc
);
234 static void kvm_s390_enable_cmma(void)
237 struct kvm_device_attr attr
= {
238 .group
= KVM_S390_VM_MEM_CTRL
,
239 .attr
= KVM_S390_VM_MEM_ENABLE_CMMA
,
243 warn_report("CMM will not be enabled because it is not "
244 "compatible with huge memory backings.");
247 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
249 trace_kvm_enable_cmma(rc
);
252 static void kvm_s390_set_attr(uint64_t attr
)
254 struct kvm_device_attr attribute
= {
255 .group
= KVM_S390_VM_CRYPTO
,
259 int ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attribute
);
262 error_report("Failed to set crypto device attribute %lu: %s",
263 attr
, strerror(-ret
));
267 static void kvm_s390_init_aes_kw(void)
269 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_AES_KW
;
271 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
273 attr
= KVM_S390_VM_CRYPTO_ENABLE_AES_KW
;
276 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
277 kvm_s390_set_attr(attr
);
281 static void kvm_s390_init_dea_kw(void)
283 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
;
285 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
287 attr
= KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
;
290 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
291 kvm_s390_set_attr(attr
);
295 void kvm_s390_crypto_reset(void)
297 if (s390_has_feat(S390_FEAT_MSA_EXT_3
)) {
298 kvm_s390_init_aes_kw();
299 kvm_s390_init_dea_kw();
303 void kvm_s390_set_max_pagesize(uint64_t pagesize
, Error
**errp
)
305 if (pagesize
== 4 * KiB
) {
309 if (!hpage_1m_allowed()) {
310 error_setg(errp
, "This QEMU machine does not support huge page "
315 if (pagesize
!= 1 * MiB
) {
316 error_setg(errp
, "Memory backing with 2G pages was specified, "
317 "but KVM does not support this memory backing");
321 if (kvm_vm_enable_cap(kvm_state
, KVM_CAP_S390_HPAGE_1M
, 0)) {
322 error_setg(errp
, "Memory backing with 1M pages was specified, "
323 "but KVM does not support this memory backing");
330 int kvm_s390_get_hpage_1m(void)
335 static void ccw_machine_class_foreach(ObjectClass
*oc
, void *opaque
)
337 MachineClass
*mc
= MACHINE_CLASS(oc
);
339 mc
->default_cpu_type
= S390_CPU_TYPE_NAME("host");
342 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
344 object_class_foreach(ccw_machine_class_foreach
, TYPE_S390_CCW_MACHINE
,
347 if (!kvm_check_extension(kvm_state
, KVM_CAP_DEVICE_CTRL
)) {
348 error_report("KVM is missing capability KVM_CAP_DEVICE_CTRL - "
349 "please use kernel 3.15 or newer");
353 cap_sync_regs
= kvm_check_extension(s
, KVM_CAP_SYNC_REGS
);
354 cap_async_pf
= kvm_check_extension(s
, KVM_CAP_ASYNC_PF
);
355 cap_mem_op
= kvm_check_extension(s
, KVM_CAP_S390_MEM_OP
);
356 cap_s390_irq
= kvm_check_extension(s
, KVM_CAP_S390_INJECT_IRQ
);
357 cap_vcpu_resets
= kvm_check_extension(s
, KVM_CAP_S390_VCPU_RESETS
);
358 cap_protected
= kvm_check_extension(s
, KVM_CAP_S390_PROTECTED
);
360 if (!kvm_check_extension(s
, KVM_CAP_S390_GMAP
)
361 || !kvm_check_extension(s
, KVM_CAP_S390_COW
)) {
362 phys_mem_set_alloc(legacy_s390_alloc
);
365 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_SIGP
, 0);
366 kvm_vm_enable_cap(s
, KVM_CAP_S390_VECTOR_REGISTERS
, 0);
367 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_STSI
, 0);
369 if (kvm_vm_enable_cap(s
, KVM_CAP_S390_RI
, 0) == 0) {
373 if (cpu_model_allowed()) {
374 if (kvm_vm_enable_cap(s
, KVM_CAP_S390_GS
, 0) == 0) {
380 * The migration interface for ais was introduced with kernel 4.13
381 * but the capability itself had been active since 4.12. As migration
382 * support is considered necessary, we only try to enable this for
383 * newer machine types if KVM_CAP_S390_AIS_MIGRATION is available.
385 if (cpu_model_allowed() && kvm_kernel_irqchip_allowed() &&
386 kvm_check_extension(s
, KVM_CAP_S390_AIS_MIGRATION
)) {
387 kvm_vm_enable_cap(s
, KVM_CAP_S390_AIS
, 0);
390 kvm_set_max_memslot_size(KVM_SLOT_MAX_BYTES
);
394 int kvm_arch_irqchip_create(KVMState
*s
)
399 unsigned long kvm_arch_vcpu_id(CPUState
*cpu
)
401 return cpu
->cpu_index
;
404 int kvm_arch_init_vcpu(CPUState
*cs
)
406 unsigned int max_cpus
= MACHINE(qdev_get_machine())->smp
.max_cpus
;
407 S390CPU
*cpu
= S390_CPU(cs
);
408 kvm_s390_set_cpu_state(cpu
, cpu
->env
.cpu_state
);
409 cpu
->irqstate
= g_malloc0(VCPU_IRQ_BUF_SIZE(max_cpus
));
413 int kvm_arch_destroy_vcpu(CPUState
*cs
)
415 S390CPU
*cpu
= S390_CPU(cs
);
417 g_free(cpu
->irqstate
);
418 cpu
->irqstate
= NULL
;
423 static void kvm_s390_reset_vcpu(S390CPU
*cpu
, unsigned long type
)
425 CPUState
*cs
= CPU(cpu
);
428 * The reset call is needed here to reset in-kernel vcpu data that
429 * we can't access directly from QEMU (i.e. with older kernels
430 * which don't support sync_regs/ONE_REG). Before this ioctl
431 * cpu_synchronize_state() is called in common kvm code
434 if (kvm_vcpu_ioctl(cs
, type
)) {
435 error_report("CPU reset failed on CPU %i type %lx",
436 cs
->cpu_index
, type
);
440 void kvm_s390_reset_vcpu_initial(S390CPU
*cpu
)
442 kvm_s390_reset_vcpu(cpu
, KVM_S390_INITIAL_RESET
);
445 void kvm_s390_reset_vcpu_clear(S390CPU
*cpu
)
447 if (cap_vcpu_resets
) {
448 kvm_s390_reset_vcpu(cpu
, KVM_S390_CLEAR_RESET
);
450 kvm_s390_reset_vcpu(cpu
, KVM_S390_INITIAL_RESET
);
454 void kvm_s390_reset_vcpu_normal(S390CPU
*cpu
)
456 if (cap_vcpu_resets
) {
457 kvm_s390_reset_vcpu(cpu
, KVM_S390_NORMAL_RESET
);
461 static int can_sync_regs(CPUState
*cs
, int regs
)
463 return cap_sync_regs
&& (cs
->kvm_run
->kvm_valid_regs
& regs
) == regs
;
466 int kvm_arch_put_registers(CPUState
*cs
, int level
)
468 S390CPU
*cpu
= S390_CPU(cs
);
469 CPUS390XState
*env
= &cpu
->env
;
470 struct kvm_sregs sregs
;
471 struct kvm_regs regs
;
472 struct kvm_fpu fpu
= {};
476 /* always save the PSW and the GPRS*/
477 cs
->kvm_run
->psw_addr
= env
->psw
.addr
;
478 cs
->kvm_run
->psw_mask
= env
->psw
.mask
;
480 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
481 for (i
= 0; i
< 16; i
++) {
482 cs
->kvm_run
->s
.regs
.gprs
[i
] = env
->regs
[i
];
483 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_GPRS
;
486 for (i
= 0; i
< 16; i
++) {
487 regs
.gprs
[i
] = env
->regs
[i
];
489 r
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
495 if (can_sync_regs(cs
, KVM_SYNC_VRS
)) {
496 for (i
= 0; i
< 32; i
++) {
497 cs
->kvm_run
->s
.regs
.vrs
[i
][0] = env
->vregs
[i
][0];
498 cs
->kvm_run
->s
.regs
.vrs
[i
][1] = env
->vregs
[i
][1];
500 cs
->kvm_run
->s
.regs
.fpc
= env
->fpc
;
501 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_VRS
;
502 } else if (can_sync_regs(cs
, KVM_SYNC_FPRS
)) {
503 for (i
= 0; i
< 16; i
++) {
504 cs
->kvm_run
->s
.regs
.fprs
[i
] = *get_freg(env
, i
);
506 cs
->kvm_run
->s
.regs
.fpc
= env
->fpc
;
507 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_FPRS
;
510 for (i
= 0; i
< 16; i
++) {
511 fpu
.fprs
[i
] = *get_freg(env
, i
);
515 r
= kvm_vcpu_ioctl(cs
, KVM_SET_FPU
, &fpu
);
521 /* Do we need to save more than that? */
522 if (level
== KVM_PUT_RUNTIME_STATE
) {
526 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
527 cs
->kvm_run
->s
.regs
.cputm
= env
->cputm
;
528 cs
->kvm_run
->s
.regs
.ckc
= env
->ckc
;
529 cs
->kvm_run
->s
.regs
.todpr
= env
->todpr
;
530 cs
->kvm_run
->s
.regs
.gbea
= env
->gbea
;
531 cs
->kvm_run
->s
.regs
.pp
= env
->pp
;
532 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ARCH0
;
535 * These ONE_REGS are not protected by a capability. As they are only
536 * necessary for migration we just trace a possible error, but don't
537 * return with an error return code.
539 kvm_set_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
540 kvm_set_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
541 kvm_set_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
542 kvm_set_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
543 kvm_set_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
546 if (can_sync_regs(cs
, KVM_SYNC_RICCB
)) {
547 memcpy(cs
->kvm_run
->s
.regs
.riccb
, env
->riccb
, 64);
548 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_RICCB
;
551 /* pfault parameters */
552 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
553 cs
->kvm_run
->s
.regs
.pft
= env
->pfault_token
;
554 cs
->kvm_run
->s
.regs
.pfs
= env
->pfault_select
;
555 cs
->kvm_run
->s
.regs
.pfc
= env
->pfault_compare
;
556 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PFAULT
;
557 } else if (cap_async_pf
) {
558 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
562 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
566 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
572 /* access registers and control registers*/
573 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
574 for (i
= 0; i
< 16; i
++) {
575 cs
->kvm_run
->s
.regs
.acrs
[i
] = env
->aregs
[i
];
576 cs
->kvm_run
->s
.regs
.crs
[i
] = env
->cregs
[i
];
578 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ACRS
;
579 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_CRS
;
581 for (i
= 0; i
< 16; i
++) {
582 sregs
.acrs
[i
] = env
->aregs
[i
];
583 sregs
.crs
[i
] = env
->cregs
[i
];
585 r
= kvm_vcpu_ioctl(cs
, KVM_SET_SREGS
, &sregs
);
591 if (can_sync_regs(cs
, KVM_SYNC_GSCB
)) {
592 memcpy(cs
->kvm_run
->s
.regs
.gscb
, env
->gscb
, 32);
593 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_GSCB
;
596 if (can_sync_regs(cs
, KVM_SYNC_BPBC
)) {
597 cs
->kvm_run
->s
.regs
.bpbc
= env
->bpbc
;
598 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_BPBC
;
601 if (can_sync_regs(cs
, KVM_SYNC_ETOKEN
)) {
602 cs
->kvm_run
->s
.regs
.etoken
= env
->etoken
;
603 cs
->kvm_run
->s
.regs
.etoken_extension
= env
->etoken_extension
;
604 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ETOKEN
;
607 if (can_sync_regs(cs
, KVM_SYNC_DIAG318
)) {
608 cs
->kvm_run
->s
.regs
.diag318
= env
->diag318_info
;
609 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_DIAG318
;
612 /* Finally the prefix */
613 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
614 cs
->kvm_run
->s
.regs
.prefix
= env
->psa
;
615 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PREFIX
;
617 /* prefix is only supported via sync regs */
622 int kvm_arch_get_registers(CPUState
*cs
)
624 S390CPU
*cpu
= S390_CPU(cs
);
625 CPUS390XState
*env
= &cpu
->env
;
626 struct kvm_sregs sregs
;
627 struct kvm_regs regs
;
632 env
->psw
.addr
= cs
->kvm_run
->psw_addr
;
633 env
->psw
.mask
= cs
->kvm_run
->psw_mask
;
636 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
637 for (i
= 0; i
< 16; i
++) {
638 env
->regs
[i
] = cs
->kvm_run
->s
.regs
.gprs
[i
];
641 r
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
645 for (i
= 0; i
< 16; i
++) {
646 env
->regs
[i
] = regs
.gprs
[i
];
650 /* The ACRS and CRS */
651 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
652 for (i
= 0; i
< 16; i
++) {
653 env
->aregs
[i
] = cs
->kvm_run
->s
.regs
.acrs
[i
];
654 env
->cregs
[i
] = cs
->kvm_run
->s
.regs
.crs
[i
];
657 r
= kvm_vcpu_ioctl(cs
, KVM_GET_SREGS
, &sregs
);
661 for (i
= 0; i
< 16; i
++) {
662 env
->aregs
[i
] = sregs
.acrs
[i
];
663 env
->cregs
[i
] = sregs
.crs
[i
];
667 /* Floating point and vector registers */
668 if (can_sync_regs(cs
, KVM_SYNC_VRS
)) {
669 for (i
= 0; i
< 32; i
++) {
670 env
->vregs
[i
][0] = cs
->kvm_run
->s
.regs
.vrs
[i
][0];
671 env
->vregs
[i
][1] = cs
->kvm_run
->s
.regs
.vrs
[i
][1];
673 env
->fpc
= cs
->kvm_run
->s
.regs
.fpc
;
674 } else if (can_sync_regs(cs
, KVM_SYNC_FPRS
)) {
675 for (i
= 0; i
< 16; i
++) {
676 *get_freg(env
, i
) = cs
->kvm_run
->s
.regs
.fprs
[i
];
678 env
->fpc
= cs
->kvm_run
->s
.regs
.fpc
;
680 r
= kvm_vcpu_ioctl(cs
, KVM_GET_FPU
, &fpu
);
684 for (i
= 0; i
< 16; i
++) {
685 *get_freg(env
, i
) = fpu
.fprs
[i
];
691 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
692 env
->psa
= cs
->kvm_run
->s
.regs
.prefix
;
695 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
696 env
->cputm
= cs
->kvm_run
->s
.regs
.cputm
;
697 env
->ckc
= cs
->kvm_run
->s
.regs
.ckc
;
698 env
->todpr
= cs
->kvm_run
->s
.regs
.todpr
;
699 env
->gbea
= cs
->kvm_run
->s
.regs
.gbea
;
700 env
->pp
= cs
->kvm_run
->s
.regs
.pp
;
703 * These ONE_REGS are not protected by a capability. As they are only
704 * necessary for migration we just trace a possible error, but don't
705 * return with an error return code.
707 kvm_get_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
708 kvm_get_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
709 kvm_get_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
710 kvm_get_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
711 kvm_get_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
714 if (can_sync_regs(cs
, KVM_SYNC_RICCB
)) {
715 memcpy(env
->riccb
, cs
->kvm_run
->s
.regs
.riccb
, 64);
718 if (can_sync_regs(cs
, KVM_SYNC_GSCB
)) {
719 memcpy(env
->gscb
, cs
->kvm_run
->s
.regs
.gscb
, 32);
722 if (can_sync_regs(cs
, KVM_SYNC_BPBC
)) {
723 env
->bpbc
= cs
->kvm_run
->s
.regs
.bpbc
;
726 if (can_sync_regs(cs
, KVM_SYNC_ETOKEN
)) {
727 env
->etoken
= cs
->kvm_run
->s
.regs
.etoken
;
728 env
->etoken_extension
= cs
->kvm_run
->s
.regs
.etoken_extension
;
731 /* pfault parameters */
732 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
733 env
->pfault_token
= cs
->kvm_run
->s
.regs
.pft
;
734 env
->pfault_select
= cs
->kvm_run
->s
.regs
.pfs
;
735 env
->pfault_compare
= cs
->kvm_run
->s
.regs
.pfc
;
736 } else if (cap_async_pf
) {
737 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
741 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
745 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
751 if (can_sync_regs(cs
, KVM_SYNC_DIAG318
)) {
752 env
->diag318_info
= cs
->kvm_run
->s
.regs
.diag318
;
758 int kvm_s390_get_clock(uint8_t *tod_high
, uint64_t *tod_low
)
761 struct kvm_device_attr attr
= {
762 .group
= KVM_S390_VM_TOD
,
763 .attr
= KVM_S390_VM_TOD_LOW
,
764 .addr
= (uint64_t)tod_low
,
767 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
772 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
773 attr
.addr
= (uint64_t)tod_high
;
774 return kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
777 int kvm_s390_get_clock_ext(uint8_t *tod_high
, uint64_t *tod_low
)
780 struct kvm_s390_vm_tod_clock gtod
;
781 struct kvm_device_attr attr
= {
782 .group
= KVM_S390_VM_TOD
,
783 .attr
= KVM_S390_VM_TOD_EXT
,
784 .addr
= (uint64_t)>od
,
787 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
788 *tod_high
= gtod
.epoch_idx
;
794 int kvm_s390_set_clock(uint8_t tod_high
, uint64_t tod_low
)
797 struct kvm_device_attr attr
= {
798 .group
= KVM_S390_VM_TOD
,
799 .attr
= KVM_S390_VM_TOD_LOW
,
800 .addr
= (uint64_t)&tod_low
,
803 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
808 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
809 attr
.addr
= (uint64_t)&tod_high
;
810 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
813 int kvm_s390_set_clock_ext(uint8_t tod_high
, uint64_t tod_low
)
815 struct kvm_s390_vm_tod_clock gtod
= {
816 .epoch_idx
= tod_high
,
819 struct kvm_device_attr attr
= {
820 .group
= KVM_S390_VM_TOD
,
821 .attr
= KVM_S390_VM_TOD_EXT
,
822 .addr
= (uint64_t)>od
,
825 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
830 * @addr: the logical start address in guest memory
831 * @ar: the access register number
832 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
833 * @len: length that should be transferred
834 * @is_write: true = write, false = read
835 * Returns: 0 on success, non-zero if an exception or error occurred
837 * Use KVM ioctl to read/write from/to guest memory. An access exception
838 * is injected into the vCPU in case of translation errors.
840 int kvm_s390_mem_op(S390CPU
*cpu
, vaddr addr
, uint8_t ar
, void *hostbuf
,
841 int len
, bool is_write
)
843 struct kvm_s390_mem_op mem_op
= {
845 .flags
= KVM_S390_MEMOP_F_INJECT_EXCEPTION
,
847 .op
= is_write
? KVM_S390_MEMOP_LOGICAL_WRITE
848 : KVM_S390_MEMOP_LOGICAL_READ
,
849 .buf
= (uint64_t)hostbuf
,
858 mem_op
.flags
|= KVM_S390_MEMOP_F_CHECK_ONLY
;
861 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_S390_MEM_OP
, &mem_op
);
863 warn_report("KVM_S390_MEM_OP failed: %s", strerror(-ret
));
868 int kvm_s390_mem_op_pv(S390CPU
*cpu
, uint64_t offset
, void *hostbuf
,
869 int len
, bool is_write
)
871 struct kvm_s390_mem_op mem_op
= {
872 .sida_offset
= offset
,
874 .op
= is_write
? KVM_S390_MEMOP_SIDA_WRITE
875 : KVM_S390_MEMOP_SIDA_READ
,
876 .buf
= (uint64_t)hostbuf
,
880 if (!cap_mem_op
|| !cap_protected
) {
884 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_S390_MEM_OP
, &mem_op
);
886 error_report("KVM_S390_MEM_OP failed: %s", strerror(-ret
));
893 * Legacy layout for s390:
894 * Older S390 KVM requires the topmost vma of the RAM to be
895 * smaller than an system defined value, which is at least 256GB.
896 * Larger systems have larger values. We put the guest between
897 * the end of data segment (system break) and this value. We
898 * use 32GB as a base to have enough room for the system break
899 * to grow. We also have to use MAP parameters that avoid
900 * read-only mapping of guest pages.
902 static void *legacy_s390_alloc(size_t size
, uint64_t *align
, bool shared
)
907 /* we only support one allocation, which is enough for initial ram */
911 mem
= mmap((void *) 0x800000000ULL
, size
,
912 PROT_EXEC
|PROT_READ
|PROT_WRITE
,
913 MAP_SHARED
| MAP_ANONYMOUS
| MAP_FIXED
, -1, 0);
914 if (mem
== MAP_FAILED
) {
918 *align
= QEMU_VMALLOC_ALIGN
;
923 static uint8_t const *sw_bp_inst
;
924 static uint8_t sw_bp_ilen
;
926 static void determine_sw_breakpoint_instr(void)
928 /* DIAG 501 is used for sw breakpoints with old kernels */
929 static const uint8_t diag_501
[] = {0x83, 0x24, 0x05, 0x01};
930 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
931 static const uint8_t instr_0x0000
[] = {0x00, 0x00};
936 if (kvm_vm_enable_cap(kvm_state
, KVM_CAP_S390_USER_INSTR0
, 0)) {
937 sw_bp_inst
= diag_501
;
938 sw_bp_ilen
= sizeof(diag_501
);
939 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
941 sw_bp_inst
= instr_0x0000
;
942 sw_bp_ilen
= sizeof(instr_0x0000
);
943 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
947 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
949 determine_sw_breakpoint_instr();
951 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
953 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)sw_bp_inst
, sw_bp_ilen
, 1)) {
959 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
963 if (cpu_memory_rw_debug(cs
, bp
->pc
, t
, sw_bp_ilen
, 0)) {
965 } else if (memcmp(t
, sw_bp_inst
, sw_bp_ilen
)) {
967 } else if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
975 static struct kvm_hw_breakpoint
*find_hw_breakpoint(target_ulong addr
,
980 for (n
= 0; n
< nb_hw_breakpoints
; n
++) {
981 if (hw_breakpoints
[n
].addr
== addr
&& hw_breakpoints
[n
].type
== type
&&
982 (hw_breakpoints
[n
].len
== len
|| len
== -1)) {
983 return &hw_breakpoints
[n
];
990 static int insert_hw_breakpoint(target_ulong addr
, int len
, int type
)
994 if (find_hw_breakpoint(addr
, len
, type
)) {
998 size
= (nb_hw_breakpoints
+ 1) * sizeof(struct kvm_hw_breakpoint
);
1000 if (!hw_breakpoints
) {
1001 nb_hw_breakpoints
= 0;
1002 hw_breakpoints
= (struct kvm_hw_breakpoint
*)g_try_malloc(size
);
1005 (struct kvm_hw_breakpoint
*)g_try_realloc(hw_breakpoints
, size
);
1008 if (!hw_breakpoints
) {
1009 nb_hw_breakpoints
= 0;
1013 hw_breakpoints
[nb_hw_breakpoints
].addr
= addr
;
1014 hw_breakpoints
[nb_hw_breakpoints
].len
= len
;
1015 hw_breakpoints
[nb_hw_breakpoints
].type
= type
;
1017 nb_hw_breakpoints
++;
1022 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1023 target_ulong len
, int type
)
1026 case GDB_BREAKPOINT_HW
:
1029 case GDB_WATCHPOINT_WRITE
:
1033 type
= KVM_HW_WP_WRITE
;
1038 return insert_hw_breakpoint(addr
, len
, type
);
1041 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
1042 target_ulong len
, int type
)
1045 struct kvm_hw_breakpoint
*bp
= find_hw_breakpoint(addr
, len
, type
);
1051 nb_hw_breakpoints
--;
1052 if (nb_hw_breakpoints
> 0) {
1054 * In order to trim the array, move the last element to the position to
1055 * be removed - if necessary.
1057 if (bp
!= &hw_breakpoints
[nb_hw_breakpoints
]) {
1058 *bp
= hw_breakpoints
[nb_hw_breakpoints
];
1060 size
= nb_hw_breakpoints
* sizeof(struct kvm_hw_breakpoint
);
1062 (struct kvm_hw_breakpoint
*)g_realloc(hw_breakpoints
, size
);
1064 g_free(hw_breakpoints
);
1065 hw_breakpoints
= NULL
;
1071 void kvm_arch_remove_all_hw_breakpoints(void)
1073 nb_hw_breakpoints
= 0;
1074 g_free(hw_breakpoints
);
1075 hw_breakpoints
= NULL
;
1078 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
1082 if (nb_hw_breakpoints
> 0) {
1083 dbg
->arch
.nr_hw_bp
= nb_hw_breakpoints
;
1084 dbg
->arch
.hw_bp
= hw_breakpoints
;
1086 for (i
= 0; i
< nb_hw_breakpoints
; ++i
) {
1087 hw_breakpoints
[i
].phys_addr
= s390_cpu_get_phys_addr_debug(cpu
,
1088 hw_breakpoints
[i
].addr
);
1090 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
1092 dbg
->arch
.nr_hw_bp
= 0;
1093 dbg
->arch
.hw_bp
= NULL
;
1097 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
1101 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
1103 return MEMTXATTRS_UNSPECIFIED
;
1106 int kvm_arch_process_async_events(CPUState
*cs
)
1111 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq
*irq
,
1112 struct kvm_s390_interrupt
*interrupt
)
1116 interrupt
->type
= irq
->type
;
1117 switch (irq
->type
) {
1118 case KVM_S390_INT_VIRTIO
:
1119 interrupt
->parm
= irq
->u
.ext
.ext_params
;
1121 case KVM_S390_INT_PFAULT_INIT
:
1122 case KVM_S390_INT_PFAULT_DONE
:
1123 interrupt
->parm64
= irq
->u
.ext
.ext_params2
;
1125 case KVM_S390_PROGRAM_INT
:
1126 interrupt
->parm
= irq
->u
.pgm
.code
;
1128 case KVM_S390_SIGP_SET_PREFIX
:
1129 interrupt
->parm
= irq
->u
.prefix
.address
;
1131 case KVM_S390_INT_SERVICE
:
1132 interrupt
->parm
= irq
->u
.ext
.ext_params
;
1135 interrupt
->parm
= irq
->u
.mchk
.cr14
;
1136 interrupt
->parm64
= irq
->u
.mchk
.mcic
;
1138 case KVM_S390_INT_EXTERNAL_CALL
:
1139 interrupt
->parm
= irq
->u
.extcall
.code
;
1141 case KVM_S390_INT_EMERGENCY
:
1142 interrupt
->parm
= irq
->u
.emerg
.code
;
1144 case KVM_S390_SIGP_STOP
:
1145 case KVM_S390_RESTART
:
1146 break; /* These types have no parameters */
1147 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1148 interrupt
->parm
= irq
->u
.io
.subchannel_id
<< 16;
1149 interrupt
->parm
|= irq
->u
.io
.subchannel_nr
;
1150 interrupt
->parm64
= (uint64_t)irq
->u
.io
.io_int_parm
<< 32;
1151 interrupt
->parm64
|= irq
->u
.io
.io_int_word
;
1160 static void inject_vcpu_irq_legacy(CPUState
*cs
, struct kvm_s390_irq
*irq
)
1162 struct kvm_s390_interrupt kvmint
= {};
1165 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
1167 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
1171 r
= kvm_vcpu_ioctl(cs
, KVM_S390_INTERRUPT
, &kvmint
);
1173 fprintf(stderr
, "KVM failed to inject interrupt\n");
1178 void kvm_s390_vcpu_interrupt(S390CPU
*cpu
, struct kvm_s390_irq
*irq
)
1180 CPUState
*cs
= CPU(cpu
);
1184 r
= kvm_vcpu_ioctl(cs
, KVM_S390_IRQ
, irq
);
1188 error_report("KVM failed to inject interrupt %llx", irq
->type
);
1192 inject_vcpu_irq_legacy(cs
, irq
);
1195 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq
*irq
)
1197 struct kvm_s390_interrupt kvmint
= {};
1200 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
1202 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
1206 r
= kvm_vm_ioctl(kvm_state
, KVM_S390_INTERRUPT
, &kvmint
);
1208 fprintf(stderr
, "KVM failed to inject interrupt\n");
1213 void kvm_s390_program_interrupt(S390CPU
*cpu
, uint16_t code
)
1215 struct kvm_s390_irq irq
= {
1216 .type
= KVM_S390_PROGRAM_INT
,
1219 qemu_log_mask(CPU_LOG_INT
, "program interrupt at %#" PRIx64
"\n",
1221 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1224 void kvm_s390_access_exception(S390CPU
*cpu
, uint16_t code
, uint64_t te_code
)
1226 struct kvm_s390_irq irq
= {
1227 .type
= KVM_S390_PROGRAM_INT
,
1229 .u
.pgm
.trans_exc_code
= te_code
,
1230 .u
.pgm
.exc_access_id
= te_code
& 3,
1233 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1236 static void kvm_sclp_service_call(S390CPU
*cpu
, struct kvm_run
*run
,
1239 CPUS390XState
*env
= &cpu
->env
;
1244 sccb
= env
->regs
[ipbh0
& 0xf];
1245 code
= env
->regs
[(ipbh0
& 0xf0) >> 4];
1247 switch (run
->s390_sieic
.icptcode
) {
1248 case ICPT_PV_INSTR_NOTIFICATION
:
1249 g_assert(s390_is_pv());
1250 /* The notification intercepts are currently handled by KVM */
1251 error_report("unexpected SCLP PV notification");
1255 g_assert(s390_is_pv());
1256 sclp_service_call_protected(env
, sccb
, code
);
1257 /* Setting the CC is done by the Ultravisor. */
1259 case ICPT_INSTRUCTION
:
1260 g_assert(!s390_is_pv());
1261 r
= sclp_service_call(env
, sccb
, code
);
1263 kvm_s390_program_interrupt(cpu
, -r
);
1270 static int handle_b2(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1272 CPUS390XState
*env
= &cpu
->env
;
1274 uint16_t ipbh0
= (run
->s390_sieic
.ipb
& 0xffff0000) >> 16;
1278 ioinst_handle_xsch(cpu
, env
->regs
[1], RA_IGNORED
);
1281 ioinst_handle_csch(cpu
, env
->regs
[1], RA_IGNORED
);
1284 ioinst_handle_hsch(cpu
, env
->regs
[1], RA_IGNORED
);
1287 ioinst_handle_msch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1290 ioinst_handle_ssch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1293 ioinst_handle_stcrw(cpu
, run
->s390_sieic
.ipb
, RA_IGNORED
);
1296 ioinst_handle_stsch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
, RA_IGNORED
);
1299 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1300 fprintf(stderr
, "Spurious tsch intercept\n");
1303 ioinst_handle_chsc(cpu
, run
->s390_sieic
.ipb
, RA_IGNORED
);
1306 /* This should have been handled by kvm already. */
1307 fprintf(stderr
, "Spurious tpi intercept\n");
1310 ioinst_handle_schm(cpu
, env
->regs
[1], env
->regs
[2],
1311 run
->s390_sieic
.ipb
, RA_IGNORED
);
1314 ioinst_handle_rsch(cpu
, env
->regs
[1], RA_IGNORED
);
1317 ioinst_handle_rchp(cpu
, env
->regs
[1], RA_IGNORED
);
1320 /* We do not provide this instruction, it is suppressed. */
1323 ioinst_handle_sal(cpu
, env
->regs
[1], RA_IGNORED
);
1326 /* Not provided, set CC = 3 for subchannel not operational */
1329 case PRIV_B2_SCLP_CALL
:
1330 kvm_sclp_service_call(cpu
, run
, ipbh0
);
1334 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1
);
1341 static uint64_t get_base_disp_rxy(S390CPU
*cpu
, struct kvm_run
*run
,
1344 CPUS390XState
*env
= &cpu
->env
;
1345 uint32_t x2
= (run
->s390_sieic
.ipa
& 0x000f);
1346 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1347 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1348 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1350 if (disp2
& 0x80000) {
1351 disp2
+= 0xfff00000;
1357 return (base2
? env
->regs
[base2
] : 0) +
1358 (x2
? env
->regs
[x2
] : 0) + (long)(int)disp2
;
1361 static uint64_t get_base_disp_rsy(S390CPU
*cpu
, struct kvm_run
*run
,
1364 CPUS390XState
*env
= &cpu
->env
;
1365 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1366 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1367 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1369 if (disp2
& 0x80000) {
1370 disp2
+= 0xfff00000;
1376 return (base2
? env
->regs
[base2
] : 0) + (long)(int)disp2
;
1379 static int kvm_clp_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1381 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1383 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1384 return clp_service_call(cpu
, r2
, RA_IGNORED
);
1390 static int kvm_pcilg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1392 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1393 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1395 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1396 return pcilg_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1402 static int kvm_pcistg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1404 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1405 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1407 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1408 return pcistg_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1414 static int kvm_stpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1416 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1420 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1421 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1423 return stpcifc_service_call(cpu
, r1
, fiba
, ar
, RA_IGNORED
);
1429 static int kvm_sic_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1431 CPUS390XState
*env
= &cpu
->env
;
1432 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1433 uint8_t r3
= run
->s390_sieic
.ipa
& 0x000f;
1438 mode
= env
->regs
[r1
] & 0xffff;
1439 isc
= (env
->regs
[r3
] >> 27) & 0x7;
1440 r
= css_do_sic(env
, isc
, mode
);
1442 kvm_s390_program_interrupt(cpu
, -r
);
1448 static int kvm_rpcit_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1450 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1451 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1453 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1454 return rpcit_service_call(cpu
, r1
, r2
, RA_IGNORED
);
1460 static int kvm_pcistb_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1462 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1463 uint8_t r3
= run
->s390_sieic
.ipa
& 0x000f;
1467 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1468 gaddr
= get_base_disp_rsy(cpu
, run
, &ar
);
1470 return pcistb_service_call(cpu
, r1
, r3
, gaddr
, ar
, RA_IGNORED
);
1476 static int kvm_mpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1478 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1482 if (s390_has_feat(S390_FEAT_ZPCI
)) {
1483 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1485 return mpcifc_service_call(cpu
, r1
, fiba
, ar
, RA_IGNORED
);
1491 static int handle_b9(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1497 r
= kvm_clp_service_call(cpu
, run
);
1499 case PRIV_B9_PCISTG
:
1500 r
= kvm_pcistg_service_call(cpu
, run
);
1503 r
= kvm_pcilg_service_call(cpu
, run
);
1506 r
= kvm_rpcit_service_call(cpu
, run
);
1509 /* just inject exception */
1514 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1
);
1521 static int handle_eb(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1526 case PRIV_EB_PCISTB
:
1527 r
= kvm_pcistb_service_call(cpu
, run
);
1530 r
= kvm_sic_service_call(cpu
, run
);
1533 /* just inject exception */
1538 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl
);
1545 static int handle_e3(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1550 case PRIV_E3_MPCIFC
:
1551 r
= kvm_mpcifc_service_call(cpu
, run
);
1553 case PRIV_E3_STPCIFC
:
1554 r
= kvm_stpcifc_service_call(cpu
, run
);
1558 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl
);
1565 static int handle_hypercall(S390CPU
*cpu
, struct kvm_run
*run
)
1567 CPUS390XState
*env
= &cpu
->env
;
1570 ret
= s390_virtio_hypercall(env
);
1571 if (ret
== -EINVAL
) {
1572 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1579 static void kvm_handle_diag_288(S390CPU
*cpu
, struct kvm_run
*run
)
1584 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1585 r3
= run
->s390_sieic
.ipa
& 0x000f;
1586 rc
= handle_diag_288(&cpu
->env
, r1
, r3
);
1588 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1592 static void kvm_handle_diag_308(S390CPU
*cpu
, struct kvm_run
*run
)
1596 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1597 r3
= run
->s390_sieic
.ipa
& 0x000f;
1598 handle_diag_308(&cpu
->env
, r1
, r3
, RA_IGNORED
);
1601 static int handle_sw_breakpoint(S390CPU
*cpu
, struct kvm_run
*run
)
1603 CPUS390XState
*env
= &cpu
->env
;
1606 pc
= env
->psw
.addr
- sw_bp_ilen
;
1607 if (kvm_find_sw_breakpoint(CPU(cpu
), pc
)) {
1615 void kvm_s390_set_diag318(CPUState
*cs
, uint64_t diag318_info
)
1617 CPUS390XState
*env
= &S390_CPU(cs
)->env
;
1619 /* Feat bit is set only if KVM supports sync for diag318 */
1620 if (s390_has_feat(S390_FEAT_DIAG_318
)) {
1621 env
->diag318_info
= diag318_info
;
1622 cs
->kvm_run
->s
.regs
.diag318
= diag318_info
;
1623 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_DIAG318
;
1627 static void handle_diag_318(S390CPU
*cpu
, struct kvm_run
*run
)
1629 uint64_t reg
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1630 uint64_t diag318_info
= run
->s
.regs
.gprs
[reg
];
1634 * DIAG 318 can only be enabled with KVM support. As such, let's
1635 * ensure a guest cannot execute this instruction erroneously.
1637 if (!s390_has_feat(S390_FEAT_DIAG_318
)) {
1638 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1643 run_on_cpu(t
, s390_do_cpu_set_diag318
,
1644 RUN_ON_CPU_HOST_ULONG(diag318_info
));
1648 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1650 static int handle_diag(S390CPU
*cpu
, struct kvm_run
*run
, uint32_t ipb
)
1656 * For any diagnose call we support, bits 48-63 of the resulting
1657 * address specify the function code; the remainder is ignored.
1659 func_code
= decode_basedisp_rs(&cpu
->env
, ipb
, NULL
) & DIAG_KVM_CODE_MASK
;
1660 switch (func_code
) {
1661 case DIAG_TIMEREVENT
:
1662 kvm_handle_diag_288(cpu
, run
);
1665 kvm_handle_diag_308(cpu
, run
);
1667 case DIAG_SET_CONTROL_PROGRAM_CODES
:
1668 handle_diag_318(cpu
, run
);
1670 case DIAG_KVM_HYPERCALL
:
1671 r
= handle_hypercall(cpu
, run
);
1673 case DIAG_KVM_BREAKPOINT
:
1674 r
= handle_sw_breakpoint(cpu
, run
);
1677 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code
);
1678 kvm_s390_program_interrupt(cpu
, PGM_SPECIFICATION
);
1685 static int kvm_s390_handle_sigp(S390CPU
*cpu
, uint8_t ipa1
, uint32_t ipb
)
1687 CPUS390XState
*env
= &cpu
->env
;
1688 const uint8_t r1
= ipa1
>> 4;
1689 const uint8_t r3
= ipa1
& 0x0f;
1693 /* get order code */
1694 order
= decode_basedisp_rs(env
, ipb
, NULL
) & SIGP_ORDER_MASK
;
1696 ret
= handle_sigp(env
, order
, r1
, r3
);
1701 static int handle_instruction(S390CPU
*cpu
, struct kvm_run
*run
)
1703 unsigned int ipa0
= (run
->s390_sieic
.ipa
& 0xff00);
1704 uint8_t ipa1
= run
->s390_sieic
.ipa
& 0x00ff;
1707 DPRINTF("handle_instruction 0x%x 0x%x\n",
1708 run
->s390_sieic
.ipa
, run
->s390_sieic
.ipb
);
1711 r
= handle_b2(cpu
, run
, ipa1
);
1714 r
= handle_b9(cpu
, run
, ipa1
);
1717 r
= handle_eb(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1720 r
= handle_e3(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1723 r
= handle_diag(cpu
, run
, run
->s390_sieic
.ipb
);
1726 r
= kvm_s390_handle_sigp(cpu
, ipa1
, run
->s390_sieic
.ipb
);
1732 kvm_s390_program_interrupt(cpu
, PGM_OPERATION
);
1738 static void unmanageable_intercept(S390CPU
*cpu
, S390CrashReason reason
,
1741 CPUState
*cs
= CPU(cpu
);
1744 cpu
->env
.crash_reason
= reason
;
1745 qemu_system_guest_panicked(cpu_get_crash_info(cs
));
1748 /* try to detect pgm check loops */
1749 static int handle_oper_loop(S390CPU
*cpu
, struct kvm_run
*run
)
1751 CPUState
*cs
= CPU(cpu
);
1754 newpsw
.mask
= ldq_phys(cs
->as
, cpu
->env
.psa
+
1755 offsetof(LowCore
, program_new_psw
));
1756 newpsw
.addr
= ldq_phys(cs
->as
, cpu
->env
.psa
+
1757 offsetof(LowCore
, program_new_psw
) + 8);
1758 oldpsw
.mask
= run
->psw_mask
;
1759 oldpsw
.addr
= run
->psw_addr
;
1761 * Avoid endless loops of operation exceptions, if the pgm new
1762 * PSW will cause a new operation exception.
1763 * The heuristic checks if the pgm new psw is within 6 bytes before
1764 * the faulting psw address (with same DAT, AS settings) and the
1765 * new psw is not a wait psw and the fault was not triggered by
1766 * problem state. In that case go into crashed state.
1769 if (oldpsw
.addr
- newpsw
.addr
<= 6 &&
1770 !(newpsw
.mask
& PSW_MASK_WAIT
) &&
1771 !(oldpsw
.mask
& PSW_MASK_PSTATE
) &&
1772 (newpsw
.mask
& PSW_MASK_ASC
) == (oldpsw
.mask
& PSW_MASK_ASC
) &&
1773 (newpsw
.mask
& PSW_MASK_DAT
) == (oldpsw
.mask
& PSW_MASK_DAT
)) {
1774 unmanageable_intercept(cpu
, S390_CRASH_REASON_OPINT_LOOP
,
1775 offsetof(LowCore
, program_new_psw
));
1781 static int handle_intercept(S390CPU
*cpu
)
1783 CPUState
*cs
= CPU(cpu
);
1784 struct kvm_run
*run
= cs
->kvm_run
;
1785 int icpt_code
= run
->s390_sieic
.icptcode
;
1788 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code
,
1789 (long)cs
->kvm_run
->psw_addr
);
1790 switch (icpt_code
) {
1791 case ICPT_INSTRUCTION
:
1793 case ICPT_PV_INSTR_NOTIFICATION
:
1794 r
= handle_instruction(cpu
, run
);
1797 unmanageable_intercept(cpu
, S390_CRASH_REASON_PGMINT_LOOP
,
1798 offsetof(LowCore
, program_new_psw
));
1802 unmanageable_intercept(cpu
, S390_CRASH_REASON_EXTINT_LOOP
,
1803 offsetof(LowCore
, external_new_psw
));
1807 /* disabled wait, since enabled wait is handled in kernel */
1808 s390_handle_wait(cpu
);
1812 do_stop_interrupt(&cpu
->env
);
1816 /* check for break points */
1817 r
= handle_sw_breakpoint(cpu
, run
);
1819 /* Then check for potential pgm check loops */
1820 r
= handle_oper_loop(cpu
, run
);
1822 kvm_s390_program_interrupt(cpu
, PGM_OPERATION
);
1826 case ICPT_SOFT_INTERCEPT
:
1827 fprintf(stderr
, "KVM unimplemented icpt SOFT\n");
1831 fprintf(stderr
, "KVM unimplemented icpt IO\n");
1835 fprintf(stderr
, "Unknown intercept code: %d\n", icpt_code
);
1843 static int handle_tsch(S390CPU
*cpu
)
1845 CPUState
*cs
= CPU(cpu
);
1846 struct kvm_run
*run
= cs
->kvm_run
;
1849 ret
= ioinst_handle_tsch(cpu
, cpu
->env
.regs
[1], run
->s390_tsch
.ipb
,
1854 * If an I/O interrupt had been dequeued, we have to reinject it.
1856 if (run
->s390_tsch
.dequeued
) {
1857 s390_io_interrupt(run
->s390_tsch
.subchannel_id
,
1858 run
->s390_tsch
.subchannel_nr
,
1859 run
->s390_tsch
.io_int_parm
,
1860 run
->s390_tsch
.io_int_word
);
1867 static void insert_stsi_3_2_2(S390CPU
*cpu
, __u64 addr
, uint8_t ar
)
1869 const MachineState
*ms
= MACHINE(qdev_get_machine());
1870 uint16_t conf_cpus
= 0, reserved_cpus
= 0;
1875 s390_cpu_pv_mem_read(cpu
, 0, &sysib
, sizeof(sysib
));
1876 } else if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &sysib
, sizeof(sysib
))) {
1879 /* Shift the stack of Extended Names to prepare for our own data */
1880 memmove(&sysib
.ext_names
[1], &sysib
.ext_names
[0],
1881 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- 1));
1882 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1883 * assumed it's not capable of managing Extended Names for lower levels.
1885 for (del
= 1; del
< sysib
.count
; del
++) {
1886 if (!sysib
.vm
[del
].ext_name_encoding
|| !sysib
.ext_names
[del
][0]) {
1890 if (del
< sysib
.count
) {
1891 memset(sysib
.ext_names
[del
], 0,
1892 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- del
));
1895 /* count the cpus and split them into configured and reserved ones */
1896 for (i
= 0; i
< ms
->possible_cpus
->len
; i
++) {
1897 if (ms
->possible_cpus
->cpus
[i
].cpu
) {
1903 sysib
.vm
[0].total_cpus
= conf_cpus
+ reserved_cpus
;
1904 sysib
.vm
[0].conf_cpus
= conf_cpus
;
1905 sysib
.vm
[0].reserved_cpus
= reserved_cpus
;
1907 /* Insert short machine name in EBCDIC, padded with blanks */
1909 memset(sysib
.vm
[0].name
, 0x40, sizeof(sysib
.vm
[0].name
));
1910 ebcdic_put(sysib
.vm
[0].name
, qemu_name
, MIN(sizeof(sysib
.vm
[0].name
),
1911 strlen(qemu_name
)));
1913 sysib
.vm
[0].ext_name_encoding
= 2; /* 2 = UTF-8 */
1914 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1915 * considered by s390 as not capable of providing any Extended Name.
1916 * Therefore if no name was specified on qemu invocation, we go with the
1917 * same "KVMguest" default, which KVM has filled into short name field.
1919 strpadcpy((char *)sysib
.ext_names
[0],
1920 sizeof(sysib
.ext_names
[0]),
1921 qemu_name
?: "KVMguest", '\0');
1924 memcpy(sysib
.vm
[0].uuid
, &qemu_uuid
, sizeof(sysib
.vm
[0].uuid
));
1927 s390_cpu_pv_mem_write(cpu
, 0, &sysib
, sizeof(sysib
));
1929 s390_cpu_virt_mem_write(cpu
, addr
, ar
, &sysib
, sizeof(sysib
));
1933 static int handle_stsi(S390CPU
*cpu
)
1935 CPUState
*cs
= CPU(cpu
);
1936 struct kvm_run
*run
= cs
->kvm_run
;
1938 switch (run
->s390_stsi
.fc
) {
1940 if (run
->s390_stsi
.sel1
!= 2 || run
->s390_stsi
.sel2
!= 2) {
1943 /* Only sysib 3.2.2 needs post-handling for now. */
1944 insert_stsi_3_2_2(cpu
, run
->s390_stsi
.addr
, run
->s390_stsi
.ar
);
1951 static int kvm_arch_handle_debug_exit(S390CPU
*cpu
)
1953 CPUState
*cs
= CPU(cpu
);
1954 struct kvm_run
*run
= cs
->kvm_run
;
1957 struct kvm_debug_exit_arch
*arch_info
= &run
->debug
.arch
;
1959 switch (arch_info
->type
) {
1960 case KVM_HW_WP_WRITE
:
1961 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1962 cs
->watchpoint_hit
= &hw_watchpoint
;
1963 hw_watchpoint
.vaddr
= arch_info
->addr
;
1964 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1969 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1973 case KVM_SINGLESTEP
:
1974 if (cs
->singlestep_enabled
) {
1985 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
1987 S390CPU
*cpu
= S390_CPU(cs
);
1990 qemu_mutex_lock_iothread();
1992 kvm_cpu_synchronize_state(cs
);
1994 switch (run
->exit_reason
) {
1995 case KVM_EXIT_S390_SIEIC
:
1996 ret
= handle_intercept(cpu
);
1998 case KVM_EXIT_S390_RESET
:
1999 s390_ipl_reset_request(cs
, S390_RESET_REIPL
);
2001 case KVM_EXIT_S390_TSCH
:
2002 ret
= handle_tsch(cpu
);
2004 case KVM_EXIT_S390_STSI
:
2005 ret
= handle_stsi(cpu
);
2007 case KVM_EXIT_DEBUG
:
2008 ret
= kvm_arch_handle_debug_exit(cpu
);
2011 fprintf(stderr
, "Unknown KVM exit: %d\n", run
->exit_reason
);
2014 qemu_mutex_unlock_iothread();
2017 ret
= EXCP_INTERRUPT
;
2022 bool kvm_arch_stop_on_emulation_error(CPUState
*cpu
)
2027 void kvm_s390_enable_css_support(S390CPU
*cpu
)
2031 /* Activate host kernel channel subsystem support. */
2032 r
= kvm_vcpu_enable_cap(CPU(cpu
), KVM_CAP_S390_CSS_SUPPORT
, 0);
2036 void kvm_arch_init_irq_routing(KVMState
*s
)
2039 * Note that while irqchip capabilities generally imply that cpustates
2040 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2041 * have to override the common code kvm_halt_in_kernel_allowed setting.
2043 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
2044 kvm_gsi_routing_allowed
= true;
2045 kvm_halt_in_kernel_allowed
= false;
2049 int kvm_s390_assign_subch_ioeventfd(EventNotifier
*notifier
, uint32_t sch
,
2050 int vq
, bool assign
)
2052 struct kvm_ioeventfd kick
= {
2053 .flags
= KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY
|
2054 KVM_IOEVENTFD_FLAG_DATAMATCH
,
2055 .fd
= event_notifier_get_fd(notifier
),
2060 trace_kvm_assign_subch_ioeventfd(kick
.fd
, kick
.addr
, assign
,
2062 if (!kvm_check_extension(kvm_state
, KVM_CAP_IOEVENTFD
)) {
2066 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
2068 return kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
2071 int kvm_s390_get_ri(void)
2076 int kvm_s390_get_gs(void)
2081 int kvm_s390_set_cpu_state(S390CPU
*cpu
, uint8_t cpu_state
)
2083 struct kvm_mp_state mp_state
= {};
2086 /* the kvm part might not have been initialized yet */
2087 if (CPU(cpu
)->kvm_state
== NULL
) {
2091 switch (cpu_state
) {
2092 case S390_CPU_STATE_STOPPED
:
2093 mp_state
.mp_state
= KVM_MP_STATE_STOPPED
;
2095 case S390_CPU_STATE_CHECK_STOP
:
2096 mp_state
.mp_state
= KVM_MP_STATE_CHECK_STOP
;
2098 case S390_CPU_STATE_OPERATING
:
2099 mp_state
.mp_state
= KVM_MP_STATE_OPERATING
;
2101 case S390_CPU_STATE_LOAD
:
2102 mp_state
.mp_state
= KVM_MP_STATE_LOAD
;
2105 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2110 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
2112 trace_kvm_failed_cpu_state_set(CPU(cpu
)->cpu_index
, cpu_state
,
2119 void kvm_s390_vcpu_interrupt_pre_save(S390CPU
*cpu
)
2121 unsigned int max_cpus
= MACHINE(qdev_get_machine())->smp
.max_cpus
;
2122 struct kvm_s390_irq_state irq_state
= {
2123 .buf
= (uint64_t) cpu
->irqstate
,
2124 .len
= VCPU_IRQ_BUF_SIZE(max_cpus
),
2126 CPUState
*cs
= CPU(cpu
);
2129 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_IRQ_STATE
)) {
2133 bytes
= kvm_vcpu_ioctl(cs
, KVM_S390_GET_IRQ_STATE
, &irq_state
);
2135 cpu
->irqstate_saved_size
= 0;
2136 error_report("Migration of interrupt state failed");
2140 cpu
->irqstate_saved_size
= bytes
;
2143 int kvm_s390_vcpu_interrupt_post_load(S390CPU
*cpu
)
2145 CPUState
*cs
= CPU(cpu
);
2146 struct kvm_s390_irq_state irq_state
= {
2147 .buf
= (uint64_t) cpu
->irqstate
,
2148 .len
= cpu
->irqstate_saved_size
,
2152 if (cpu
->irqstate_saved_size
== 0) {
2156 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_IRQ_STATE
)) {
2160 r
= kvm_vcpu_ioctl(cs
, KVM_S390_SET_IRQ_STATE
, &irq_state
);
2162 error_report("Setting interrupt state failed %d", r
);
2167 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
2168 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
2170 S390PCIBusDevice
*pbdev
;
2171 uint32_t vec
= data
& ZPCI_MSI_VEC_MASK
;
2174 DPRINTF("add_msi_route no pci device\n");
2178 pbdev
= s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev
)->id
);
2180 DPRINTF("add_msi_route no zpci device\n");
2184 route
->type
= KVM_IRQ_ROUTING_S390_ADAPTER
;
2186 route
->u
.adapter
.summary_addr
= pbdev
->routes
.adapter
.summary_addr
;
2187 route
->u
.adapter
.ind_addr
= pbdev
->routes
.adapter
.ind_addr
;
2188 route
->u
.adapter
.summary_offset
= pbdev
->routes
.adapter
.summary_offset
;
2189 route
->u
.adapter
.ind_offset
= pbdev
->routes
.adapter
.ind_offset
+ vec
;
2190 route
->u
.adapter
.adapter_id
= pbdev
->routes
.adapter
.adapter_id
;
2194 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
2195 int vector
, PCIDevice
*dev
)
2200 int kvm_arch_release_virq_post(int virq
)
2205 int kvm_arch_msi_data_to_gsi(uint32_t data
)
2210 static int query_cpu_subfunc(S390FeatBitmap features
)
2212 struct kvm_s390_vm_cpu_subfunc prop
= {};
2213 struct kvm_device_attr attr
= {
2214 .group
= KVM_S390_VM_CPU_MODEL
,
2215 .attr
= KVM_S390_VM_CPU_MACHINE_SUBFUNC
,
2216 .addr
= (uint64_t) &prop
,
2220 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2226 * We're going to add all subfunctions now, if the corresponding feature
2227 * is available that unlocks the query functions.
2229 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PLO
, prop
.plo
);
2230 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING
, features
)) {
2231 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PTFF
, prop
.ptff
);
2233 if (test_bit(S390_FEAT_MSA
, features
)) {
2234 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMAC
, prop
.kmac
);
2235 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMC
, prop
.kmc
);
2236 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KM
, prop
.km
);
2237 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KIMD
, prop
.kimd
);
2238 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KLMD
, prop
.klmd
);
2240 if (test_bit(S390_FEAT_MSA_EXT_3
, features
)) {
2241 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PCKMO
, prop
.pckmo
);
2243 if (test_bit(S390_FEAT_MSA_EXT_4
, features
)) {
2244 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMCTR
, prop
.kmctr
);
2245 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMF
, prop
.kmf
);
2246 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMO
, prop
.kmo
);
2247 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PCC
, prop
.pcc
);
2249 if (test_bit(S390_FEAT_MSA_EXT_5
, features
)) {
2250 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PPNO
, prop
.ppno
);
2252 if (test_bit(S390_FEAT_MSA_EXT_8
, features
)) {
2253 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMA
, prop
.kma
);
2255 if (test_bit(S390_FEAT_MSA_EXT_9
, features
)) {
2256 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KDSA
, prop
.kdsa
);
2258 if (test_bit(S390_FEAT_ESORT_BASE
, features
)) {
2259 s390_add_from_feat_block(features
, S390_FEAT_TYPE_SORTL
, prop
.sortl
);
2261 if (test_bit(S390_FEAT_DEFLATE_BASE
, features
)) {
2262 s390_add_from_feat_block(features
, S390_FEAT_TYPE_DFLTCC
, prop
.dfltcc
);
2267 static int configure_cpu_subfunc(const S390FeatBitmap features
)
2269 struct kvm_s390_vm_cpu_subfunc prop
= {};
2270 struct kvm_device_attr attr
= {
2271 .group
= KVM_S390_VM_CPU_MODEL
,
2272 .attr
= KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
,
2273 .addr
= (uint64_t) &prop
,
2276 if (!kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2277 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
)) {
2278 /* hardware support might be missing, IBC will handle most of this */
2282 s390_fill_feat_block(features
, S390_FEAT_TYPE_PLO
, prop
.plo
);
2283 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING
, features
)) {
2284 s390_fill_feat_block(features
, S390_FEAT_TYPE_PTFF
, prop
.ptff
);
2286 if (test_bit(S390_FEAT_MSA
, features
)) {
2287 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMAC
, prop
.kmac
);
2288 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMC
, prop
.kmc
);
2289 s390_fill_feat_block(features
, S390_FEAT_TYPE_KM
, prop
.km
);
2290 s390_fill_feat_block(features
, S390_FEAT_TYPE_KIMD
, prop
.kimd
);
2291 s390_fill_feat_block(features
, S390_FEAT_TYPE_KLMD
, prop
.klmd
);
2293 if (test_bit(S390_FEAT_MSA_EXT_3
, features
)) {
2294 s390_fill_feat_block(features
, S390_FEAT_TYPE_PCKMO
, prop
.pckmo
);
2296 if (test_bit(S390_FEAT_MSA_EXT_4
, features
)) {
2297 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMCTR
, prop
.kmctr
);
2298 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMF
, prop
.kmf
);
2299 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMO
, prop
.kmo
);
2300 s390_fill_feat_block(features
, S390_FEAT_TYPE_PCC
, prop
.pcc
);
2302 if (test_bit(S390_FEAT_MSA_EXT_5
, features
)) {
2303 s390_fill_feat_block(features
, S390_FEAT_TYPE_PPNO
, prop
.ppno
);
2305 if (test_bit(S390_FEAT_MSA_EXT_8
, features
)) {
2306 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMA
, prop
.kma
);
2308 if (test_bit(S390_FEAT_MSA_EXT_9
, features
)) {
2309 s390_fill_feat_block(features
, S390_FEAT_TYPE_KDSA
, prop
.kdsa
);
2311 if (test_bit(S390_FEAT_ESORT_BASE
, features
)) {
2312 s390_fill_feat_block(features
, S390_FEAT_TYPE_SORTL
, prop
.sortl
);
2314 if (test_bit(S390_FEAT_DEFLATE_BASE
, features
)) {
2315 s390_fill_feat_block(features
, S390_FEAT_TYPE_DFLTCC
, prop
.dfltcc
);
2317 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2320 static int kvm_to_feat
[][2] = {
2321 { KVM_S390_VM_CPU_FEAT_ESOP
, S390_FEAT_ESOP
},
2322 { KVM_S390_VM_CPU_FEAT_SIEF2
, S390_FEAT_SIE_F2
},
2323 { KVM_S390_VM_CPU_FEAT_64BSCAO
, S390_FEAT_SIE_64BSCAO
},
2324 { KVM_S390_VM_CPU_FEAT_SIIF
, S390_FEAT_SIE_SIIF
},
2325 { KVM_S390_VM_CPU_FEAT_GPERE
, S390_FEAT_SIE_GPERE
},
2326 { KVM_S390_VM_CPU_FEAT_GSLS
, S390_FEAT_SIE_GSLS
},
2327 { KVM_S390_VM_CPU_FEAT_IB
, S390_FEAT_SIE_IB
},
2328 { KVM_S390_VM_CPU_FEAT_CEI
, S390_FEAT_SIE_CEI
},
2329 { KVM_S390_VM_CPU_FEAT_IBS
, S390_FEAT_SIE_IBS
},
2330 { KVM_S390_VM_CPU_FEAT_SKEY
, S390_FEAT_SIE_SKEY
},
2331 { KVM_S390_VM_CPU_FEAT_CMMA
, S390_FEAT_SIE_CMMA
},
2332 { KVM_S390_VM_CPU_FEAT_PFMFI
, S390_FEAT_SIE_PFMFI
},
2333 { KVM_S390_VM_CPU_FEAT_SIGPIF
, S390_FEAT_SIE_SIGPIF
},
2334 { KVM_S390_VM_CPU_FEAT_KSS
, S390_FEAT_SIE_KSS
},
2337 static int query_cpu_feat(S390FeatBitmap features
)
2339 struct kvm_s390_vm_cpu_feat prop
= {};
2340 struct kvm_device_attr attr
= {
2341 .group
= KVM_S390_VM_CPU_MODEL
,
2342 .attr
= KVM_S390_VM_CPU_MACHINE_FEAT
,
2343 .addr
= (uint64_t) &prop
,
2348 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2353 for (i
= 0; i
< ARRAY_SIZE(kvm_to_feat
); i
++) {
2354 if (test_be_bit(kvm_to_feat
[i
][0], (uint8_t *) prop
.feat
)) {
2355 set_bit(kvm_to_feat
[i
][1], features
);
2361 static int configure_cpu_feat(const S390FeatBitmap features
)
2363 struct kvm_s390_vm_cpu_feat prop
= {};
2364 struct kvm_device_attr attr
= {
2365 .group
= KVM_S390_VM_CPU_MODEL
,
2366 .attr
= KVM_S390_VM_CPU_PROCESSOR_FEAT
,
2367 .addr
= (uint64_t) &prop
,
2371 for (i
= 0; i
< ARRAY_SIZE(kvm_to_feat
); i
++) {
2372 if (test_bit(kvm_to_feat
[i
][1], features
)) {
2373 set_be_bit(kvm_to_feat
[i
][0], (uint8_t *) prop
.feat
);
2376 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2379 bool kvm_s390_cpu_models_supported(void)
2381 if (!cpu_model_allowed()) {
2382 /* compatibility machines interfere with the cpu model */
2385 return kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2386 KVM_S390_VM_CPU_MACHINE
) &&
2387 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2388 KVM_S390_VM_CPU_PROCESSOR
) &&
2389 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2390 KVM_S390_VM_CPU_MACHINE_FEAT
) &&
2391 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2392 KVM_S390_VM_CPU_PROCESSOR_FEAT
) &&
2393 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2394 KVM_S390_VM_CPU_MACHINE_SUBFUNC
);
2397 void kvm_s390_get_host_cpu_model(S390CPUModel
*model
, Error
**errp
)
2399 struct kvm_s390_vm_cpu_machine prop
= {};
2400 struct kvm_device_attr attr
= {
2401 .group
= KVM_S390_VM_CPU_MODEL
,
2402 .attr
= KVM_S390_VM_CPU_MACHINE
,
2403 .addr
= (uint64_t) &prop
,
2405 uint16_t unblocked_ibc
= 0, cpu_type
= 0;
2408 memset(model
, 0, sizeof(*model
));
2410 if (!kvm_s390_cpu_models_supported()) {
2411 error_setg(errp
, "KVM doesn't support CPU models");
2415 /* query the basic cpu model properties */
2416 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2418 error_setg(errp
, "KVM: Error querying host CPU model: %d", rc
);
2422 cpu_type
= cpuid_type(prop
.cpuid
);
2423 if (has_ibc(prop
.ibc
)) {
2424 model
->lowest_ibc
= lowest_ibc(prop
.ibc
);
2425 unblocked_ibc
= unblocked_ibc(prop
.ibc
);
2427 model
->cpu_id
= cpuid_id(prop
.cpuid
);
2428 model
->cpu_id_format
= cpuid_format(prop
.cpuid
);
2429 model
->cpu_ver
= 0xff;
2431 /* get supported cpu features indicated via STFL(E) */
2432 s390_add_from_feat_block(model
->features
, S390_FEAT_TYPE_STFL
,
2433 (uint8_t *) prop
.fac_mask
);
2434 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2435 if (test_bit(S390_FEAT_STFLE
, model
->features
)) {
2436 set_bit(S390_FEAT_DAT_ENH_2
, model
->features
);
2438 /* get supported cpu features indicated e.g. via SCLP */
2439 rc
= query_cpu_feat(model
->features
);
2441 error_setg(errp
, "KVM: Error querying CPU features: %d", rc
);
2444 /* get supported cpu subfunctions indicated via query / test bit */
2445 rc
= query_cpu_subfunc(model
->features
);
2447 error_setg(errp
, "KVM: Error querying CPU subfunctions: %d", rc
);
2451 /* PTFF subfunctions might be indicated although kernel support missing */
2452 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH
, model
->features
)) {
2453 clear_bit(S390_FEAT_PTFF_QSIE
, model
->features
);
2454 clear_bit(S390_FEAT_PTFF_QTOUE
, model
->features
);
2455 clear_bit(S390_FEAT_PTFF_STOE
, model
->features
);
2456 clear_bit(S390_FEAT_PTFF_STOUE
, model
->features
);
2459 /* with cpu model support, CMM is only indicated if really available */
2460 if (kvm_s390_cmma_available()) {
2461 set_bit(S390_FEAT_CMM
, model
->features
);
2463 /* no cmm -> no cmm nt */
2464 clear_bit(S390_FEAT_CMM_NT
, model
->features
);
2467 /* bpb needs kernel support for migration, VSIE and reset */
2468 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_BPB
)) {
2469 clear_bit(S390_FEAT_BPB
, model
->features
);
2473 * If we have support for protected virtualization, indicate
2474 * the protected virtualization IPL unpack facility.
2476 if (cap_protected
) {
2477 set_bit(S390_FEAT_UNPACK
, model
->features
);
2480 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2481 set_bit(S390_FEAT_ZPCI
, model
->features
);
2482 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION
, model
->features
);
2484 if (s390_known_cpu_type(cpu_type
)) {
2485 /* we want the exact model, even if some features are missing */
2486 model
->def
= s390_find_cpu_def(cpu_type
, ibc_gen(unblocked_ibc
),
2487 ibc_ec_ga(unblocked_ibc
), NULL
);
2489 /* model unknown, e.g. too new - search using features */
2490 model
->def
= s390_find_cpu_def(0, ibc_gen(unblocked_ibc
),
2491 ibc_ec_ga(unblocked_ibc
),
2495 error_setg(errp
, "KVM: host CPU model could not be identified");
2498 /* for now, we can only provide the AP feature with HW support */
2499 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
,
2500 KVM_S390_VM_CRYPTO_ENABLE_APIE
)) {
2501 set_bit(S390_FEAT_AP
, model
->features
);
2505 * Extended-Length SCCB is handled entirely within QEMU.
2506 * For PV guests this is completely fenced by the Ultravisor, as Service
2507 * Call error checking and STFLE interpretation are handled via SIE.
2509 set_bit(S390_FEAT_EXTENDED_LENGTH_SCCB
, model
->features
);
2511 if (kvm_check_extension(kvm_state
, KVM_CAP_S390_DIAG318
)) {
2512 set_bit(S390_FEAT_DIAG_318
, model
->features
);
2515 /* strip of features that are not part of the maximum model */
2516 bitmap_and(model
->features
, model
->features
, model
->def
->full_feat
,
2520 static void kvm_s390_configure_apie(bool interpret
)
2522 uint64_t attr
= interpret
? KVM_S390_VM_CRYPTO_ENABLE_APIE
:
2523 KVM_S390_VM_CRYPTO_DISABLE_APIE
;
2525 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
2526 kvm_s390_set_attr(attr
);
2530 void kvm_s390_apply_cpu_model(const S390CPUModel
*model
, Error
**errp
)
2532 struct kvm_s390_vm_cpu_processor prop
= {
2535 struct kvm_device_attr attr
= {
2536 .group
= KVM_S390_VM_CPU_MODEL
,
2537 .attr
= KVM_S390_VM_CPU_PROCESSOR
,
2538 .addr
= (uint64_t) &prop
,
2543 /* compatibility handling if cpu models are disabled */
2544 if (kvm_s390_cmma_available()) {
2545 kvm_s390_enable_cmma();
2549 if (!kvm_s390_cpu_models_supported()) {
2550 error_setg(errp
, "KVM doesn't support CPU models");
2553 prop
.cpuid
= s390_cpuid_from_cpu_model(model
);
2554 prop
.ibc
= s390_ibc_from_cpu_model(model
);
2555 /* configure cpu features indicated via STFL(e) */
2556 s390_fill_feat_block(model
->features
, S390_FEAT_TYPE_STFL
,
2557 (uint8_t *) prop
.fac_list
);
2558 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2560 error_setg(errp
, "KVM: Error configuring the CPU model: %d", rc
);
2563 /* configure cpu features indicated e.g. via SCLP */
2564 rc
= configure_cpu_feat(model
->features
);
2566 error_setg(errp
, "KVM: Error configuring CPU features: %d", rc
);
2569 /* configure cpu subfunctions indicated via query / test bit */
2570 rc
= configure_cpu_subfunc(model
->features
);
2572 error_setg(errp
, "KVM: Error configuring CPU subfunctions: %d", rc
);
2575 /* enable CMM via CMMA */
2576 if (test_bit(S390_FEAT_CMM
, model
->features
)) {
2577 kvm_s390_enable_cmma();
2580 if (test_bit(S390_FEAT_AP
, model
->features
)) {
2581 kvm_s390_configure_apie(true);
2585 void kvm_s390_restart_interrupt(S390CPU
*cpu
)
2587 struct kvm_s390_irq irq
= {
2588 .type
= KVM_S390_RESTART
,
2591 kvm_s390_vcpu_interrupt(cpu
, &irq
);
2594 void kvm_s390_stop_interrupt(S390CPU
*cpu
)
2596 struct kvm_s390_irq irq
= {
2597 .type
= KVM_S390_SIGP_STOP
,
2600 kvm_s390_vcpu_interrupt(cpu
, &irq
);