2 * QEMU S390x KVM implementation
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp. 2012
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library 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 * Lesser General Public License for more details.
17 * Contributions after 2012-10-29 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
20 * You should have received a copy of the GNU (Lesser) General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "qemu/osdep.h"
25 #include <sys/ioctl.h>
27 #include <linux/kvm.h>
28 #include <asm/ptrace.h>
30 #include "qemu-common.h"
32 #include "qemu/error-report.h"
33 #include "qemu/timer.h"
34 #include "sysemu/sysemu.h"
35 #include "sysemu/hw_accel.h"
37 #include "sysemu/device_tree.h"
38 #include "qapi/qmp/qjson.h"
39 #include "exec/gdbstub.h"
40 #include "exec/address-spaces.h"
42 #include "qapi-event.h"
43 #include "hw/s390x/s390-pci-inst.h"
44 #include "hw/s390x/s390-pci-bus.h"
45 #include "hw/s390x/ipl.h"
46 #include "hw/s390x/ebcdic.h"
47 #include "exec/memattrs.h"
48 #include "hw/s390x/s390-virtio-ccw.h"
54 #define DPRINTF(fmt, ...) do { \
56 fprintf(stderr, fmt, ## __VA_ARGS__); \
60 #define kvm_vm_check_mem_attr(s, attr) \
61 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
63 #define IPA0_DIAG 0x8300
64 #define IPA0_SIGP 0xae00
65 #define IPA0_B2 0xb200
66 #define IPA0_B9 0xb900
67 #define IPA0_EB 0xeb00
68 #define IPA0_E3 0xe300
70 #define PRIV_B2_SCLP_CALL 0x20
71 #define PRIV_B2_CSCH 0x30
72 #define PRIV_B2_HSCH 0x31
73 #define PRIV_B2_MSCH 0x32
74 #define PRIV_B2_SSCH 0x33
75 #define PRIV_B2_STSCH 0x34
76 #define PRIV_B2_TSCH 0x35
77 #define PRIV_B2_TPI 0x36
78 #define PRIV_B2_SAL 0x37
79 #define PRIV_B2_RSCH 0x38
80 #define PRIV_B2_STCRW 0x39
81 #define PRIV_B2_STCPS 0x3a
82 #define PRIV_B2_RCHP 0x3b
83 #define PRIV_B2_SCHM 0x3c
84 #define PRIV_B2_CHSC 0x5f
85 #define PRIV_B2_SIGA 0x74
86 #define PRIV_B2_XSCH 0x76
88 #define PRIV_EB_SQBS 0x8a
89 #define PRIV_EB_PCISTB 0xd0
90 #define PRIV_EB_SIC 0xd1
92 #define PRIV_B9_EQBS 0x9c
93 #define PRIV_B9_CLP 0xa0
94 #define PRIV_B9_PCISTG 0xd0
95 #define PRIV_B9_PCILG 0xd2
96 #define PRIV_B9_RPCIT 0xd3
98 #define PRIV_E3_MPCIFC 0xd0
99 #define PRIV_E3_STPCIFC 0xd4
101 #define DIAG_TIMEREVENT 0x288
102 #define DIAG_IPL 0x308
103 #define DIAG_KVM_HYPERCALL 0x500
104 #define DIAG_KVM_BREAKPOINT 0x501
106 #define ICPT_INSTRUCTION 0x04
107 #define ICPT_PROGRAM 0x08
108 #define ICPT_EXT_INT 0x14
109 #define ICPT_WAITPSW 0x1c
110 #define ICPT_SOFT_INTERCEPT 0x24
111 #define ICPT_CPU_STOP 0x28
112 #define ICPT_OPEREXC 0x2c
115 #define NR_LOCAL_IRQS 32
117 * Needs to be big enough to contain max_cpus emergency signals
118 * and in addition NR_LOCAL_IRQS interrupts
120 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
121 (max_cpus + NR_LOCAL_IRQS))
123 static CPUWatchpoint hw_watchpoint
;
125 * We don't use a list because this structure is also used to transmit the
126 * hardware breakpoints to the kernel.
128 static struct kvm_hw_breakpoint
*hw_breakpoints
;
129 static int nb_hw_breakpoints
;
131 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
135 static QemuMutex qemu_sigp_mutex
;
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 void *legacy_s390_alloc(size_t size
, uint64_t *align
);
145 static int kvm_s390_query_mem_limit(KVMState
*s
, uint64_t *memory_limit
)
147 struct kvm_device_attr attr
= {
148 .group
= KVM_S390_VM_MEM_CTRL
,
149 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
150 .addr
= (uint64_t) memory_limit
,
153 return kvm_vm_ioctl(s
, KVM_GET_DEVICE_ATTR
, &attr
);
156 int kvm_s390_set_mem_limit(KVMState
*s
, uint64_t new_limit
, uint64_t *hw_limit
)
160 struct kvm_device_attr attr
= {
161 .group
= KVM_S390_VM_MEM_CTRL
,
162 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
163 .addr
= (uint64_t) &new_limit
,
166 if (!kvm_vm_check_mem_attr(s
, KVM_S390_VM_MEM_LIMIT_SIZE
)) {
170 rc
= kvm_s390_query_mem_limit(s
, hw_limit
);
173 } else if (*hw_limit
< new_limit
) {
177 return kvm_vm_ioctl(s
, KVM_SET_DEVICE_ATTR
, &attr
);
180 static bool kvm_s390_cmma_available(void)
182 static bool initialized
, value
;
186 value
= kvm_vm_check_mem_attr(kvm_state
, KVM_S390_VM_MEM_ENABLE_CMMA
) &&
187 kvm_vm_check_mem_attr(kvm_state
, KVM_S390_VM_MEM_CLR_CMMA
);
192 void kvm_s390_cmma_reset(void)
195 struct kvm_device_attr attr
= {
196 .group
= KVM_S390_VM_MEM_CTRL
,
197 .attr
= KVM_S390_VM_MEM_CLR_CMMA
,
200 if (mem_path
|| !kvm_s390_cmma_available()) {
204 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
205 trace_kvm_clear_cmma(rc
);
208 static void kvm_s390_enable_cmma(void)
211 struct kvm_device_attr attr
= {
212 .group
= KVM_S390_VM_MEM_CTRL
,
213 .attr
= KVM_S390_VM_MEM_ENABLE_CMMA
,
216 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
217 trace_kvm_enable_cmma(rc
);
220 static void kvm_s390_set_attr(uint64_t attr
)
222 struct kvm_device_attr attribute
= {
223 .group
= KVM_S390_VM_CRYPTO
,
227 int ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attribute
);
230 error_report("Failed to set crypto device attribute %lu: %s",
231 attr
, strerror(-ret
));
235 static void kvm_s390_init_aes_kw(void)
237 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_AES_KW
;
239 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
241 attr
= KVM_S390_VM_CRYPTO_ENABLE_AES_KW
;
244 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
245 kvm_s390_set_attr(attr
);
249 static void kvm_s390_init_dea_kw(void)
251 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
;
253 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
255 attr
= KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
;
258 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
259 kvm_s390_set_attr(attr
);
263 void kvm_s390_crypto_reset(void)
265 if (s390_has_feat(S390_FEAT_MSA_EXT_3
)) {
266 kvm_s390_init_aes_kw();
267 kvm_s390_init_dea_kw();
271 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
273 cap_sync_regs
= kvm_check_extension(s
, KVM_CAP_SYNC_REGS
);
274 cap_async_pf
= kvm_check_extension(s
, KVM_CAP_ASYNC_PF
);
275 cap_mem_op
= kvm_check_extension(s
, KVM_CAP_S390_MEM_OP
);
276 cap_s390_irq
= kvm_check_extension(s
, KVM_CAP_S390_INJECT_IRQ
);
278 if (!kvm_check_extension(s
, KVM_CAP_S390_GMAP
)
279 || !kvm_check_extension(s
, KVM_CAP_S390_COW
)) {
280 phys_mem_set_alloc(legacy_s390_alloc
);
283 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_SIGP
, 0);
284 kvm_vm_enable_cap(s
, KVM_CAP_S390_VECTOR_REGISTERS
, 0);
285 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_STSI
, 0);
287 if (kvm_vm_enable_cap(s
, KVM_CAP_S390_RI
, 0) == 0) {
292 qemu_mutex_init(&qemu_sigp_mutex
);
297 int kvm_arch_irqchip_create(MachineState
*ms
, KVMState
*s
)
302 unsigned long kvm_arch_vcpu_id(CPUState
*cpu
)
304 return cpu
->cpu_index
;
307 int kvm_arch_init_vcpu(CPUState
*cs
)
309 S390CPU
*cpu
= S390_CPU(cs
);
310 kvm_s390_set_cpu_state(cpu
, cpu
->env
.cpu_state
);
311 cpu
->irqstate
= g_malloc0(VCPU_IRQ_BUF_SIZE
);
315 void kvm_s390_reset_vcpu(S390CPU
*cpu
)
317 CPUState
*cs
= CPU(cpu
);
319 /* The initial reset call is needed here to reset in-kernel
320 * vcpu data that we can't access directly from QEMU
321 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
322 * Before this ioctl cpu_synchronize_state() is called in common kvm
324 if (kvm_vcpu_ioctl(cs
, KVM_S390_INITIAL_RESET
, NULL
)) {
325 error_report("Initial CPU reset failed on CPU %i", cs
->cpu_index
);
329 static int can_sync_regs(CPUState
*cs
, int regs
)
331 return cap_sync_regs
&& (cs
->kvm_run
->kvm_valid_regs
& regs
) == regs
;
334 int kvm_arch_put_registers(CPUState
*cs
, int level
)
336 S390CPU
*cpu
= S390_CPU(cs
);
337 CPUS390XState
*env
= &cpu
->env
;
338 struct kvm_sregs sregs
;
339 struct kvm_regs regs
;
340 struct kvm_fpu fpu
= {};
344 /* always save the PSW and the GPRS*/
345 cs
->kvm_run
->psw_addr
= env
->psw
.addr
;
346 cs
->kvm_run
->psw_mask
= env
->psw
.mask
;
348 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
349 for (i
= 0; i
< 16; i
++) {
350 cs
->kvm_run
->s
.regs
.gprs
[i
] = env
->regs
[i
];
351 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_GPRS
;
354 for (i
= 0; i
< 16; i
++) {
355 regs
.gprs
[i
] = env
->regs
[i
];
357 r
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
363 if (can_sync_regs(cs
, KVM_SYNC_VRS
)) {
364 for (i
= 0; i
< 32; i
++) {
365 cs
->kvm_run
->s
.regs
.vrs
[i
][0] = env
->vregs
[i
][0].ll
;
366 cs
->kvm_run
->s
.regs
.vrs
[i
][1] = env
->vregs
[i
][1].ll
;
368 cs
->kvm_run
->s
.regs
.fpc
= env
->fpc
;
369 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_VRS
;
370 } else if (can_sync_regs(cs
, KVM_SYNC_FPRS
)) {
371 for (i
= 0; i
< 16; i
++) {
372 cs
->kvm_run
->s
.regs
.fprs
[i
] = get_freg(env
, i
)->ll
;
374 cs
->kvm_run
->s
.regs
.fpc
= env
->fpc
;
375 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_FPRS
;
378 for (i
= 0; i
< 16; i
++) {
379 fpu
.fprs
[i
] = get_freg(env
, i
)->ll
;
383 r
= kvm_vcpu_ioctl(cs
, KVM_SET_FPU
, &fpu
);
389 /* Do we need to save more than that? */
390 if (level
== KVM_PUT_RUNTIME_STATE
) {
394 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
395 cs
->kvm_run
->s
.regs
.cputm
= env
->cputm
;
396 cs
->kvm_run
->s
.regs
.ckc
= env
->ckc
;
397 cs
->kvm_run
->s
.regs
.todpr
= env
->todpr
;
398 cs
->kvm_run
->s
.regs
.gbea
= env
->gbea
;
399 cs
->kvm_run
->s
.regs
.pp
= env
->pp
;
400 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ARCH0
;
403 * These ONE_REGS are not protected by a capability. As they are only
404 * necessary for migration we just trace a possible error, but don't
405 * return with an error return code.
407 kvm_set_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
408 kvm_set_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
409 kvm_set_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
410 kvm_set_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
411 kvm_set_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
414 if (can_sync_regs(cs
, KVM_SYNC_RICCB
)) {
415 memcpy(cs
->kvm_run
->s
.regs
.riccb
, env
->riccb
, 64);
416 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_RICCB
;
419 /* pfault parameters */
420 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
421 cs
->kvm_run
->s
.regs
.pft
= env
->pfault_token
;
422 cs
->kvm_run
->s
.regs
.pfs
= env
->pfault_select
;
423 cs
->kvm_run
->s
.regs
.pfc
= env
->pfault_compare
;
424 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PFAULT
;
425 } else if (cap_async_pf
) {
426 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
430 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
434 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
440 /* access registers and control registers*/
441 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
442 for (i
= 0; i
< 16; i
++) {
443 cs
->kvm_run
->s
.regs
.acrs
[i
] = env
->aregs
[i
];
444 cs
->kvm_run
->s
.regs
.crs
[i
] = env
->cregs
[i
];
446 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ACRS
;
447 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_CRS
;
449 for (i
= 0; i
< 16; i
++) {
450 sregs
.acrs
[i
] = env
->aregs
[i
];
451 sregs
.crs
[i
] = env
->cregs
[i
];
453 r
= kvm_vcpu_ioctl(cs
, KVM_SET_SREGS
, &sregs
);
459 /* Finally the prefix */
460 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
461 cs
->kvm_run
->s
.regs
.prefix
= env
->psa
;
462 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PREFIX
;
464 /* prefix is only supported via sync regs */
469 int kvm_arch_get_registers(CPUState
*cs
)
471 S390CPU
*cpu
= S390_CPU(cs
);
472 CPUS390XState
*env
= &cpu
->env
;
473 struct kvm_sregs sregs
;
474 struct kvm_regs regs
;
479 env
->psw
.addr
= cs
->kvm_run
->psw_addr
;
480 env
->psw
.mask
= cs
->kvm_run
->psw_mask
;
483 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
484 for (i
= 0; i
< 16; i
++) {
485 env
->regs
[i
] = cs
->kvm_run
->s
.regs
.gprs
[i
];
488 r
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
492 for (i
= 0; i
< 16; i
++) {
493 env
->regs
[i
] = regs
.gprs
[i
];
497 /* The ACRS and CRS */
498 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
499 for (i
= 0; i
< 16; i
++) {
500 env
->aregs
[i
] = cs
->kvm_run
->s
.regs
.acrs
[i
];
501 env
->cregs
[i
] = cs
->kvm_run
->s
.regs
.crs
[i
];
504 r
= kvm_vcpu_ioctl(cs
, KVM_GET_SREGS
, &sregs
);
508 for (i
= 0; i
< 16; i
++) {
509 env
->aregs
[i
] = sregs
.acrs
[i
];
510 env
->cregs
[i
] = sregs
.crs
[i
];
514 /* Floating point and vector registers */
515 if (can_sync_regs(cs
, KVM_SYNC_VRS
)) {
516 for (i
= 0; i
< 32; i
++) {
517 env
->vregs
[i
][0].ll
= cs
->kvm_run
->s
.regs
.vrs
[i
][0];
518 env
->vregs
[i
][1].ll
= cs
->kvm_run
->s
.regs
.vrs
[i
][1];
520 env
->fpc
= cs
->kvm_run
->s
.regs
.fpc
;
521 } else if (can_sync_regs(cs
, KVM_SYNC_FPRS
)) {
522 for (i
= 0; i
< 16; i
++) {
523 get_freg(env
, i
)->ll
= cs
->kvm_run
->s
.regs
.fprs
[i
];
525 env
->fpc
= cs
->kvm_run
->s
.regs
.fpc
;
527 r
= kvm_vcpu_ioctl(cs
, KVM_GET_FPU
, &fpu
);
531 for (i
= 0; i
< 16; i
++) {
532 get_freg(env
, i
)->ll
= fpu
.fprs
[i
];
538 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
539 env
->psa
= cs
->kvm_run
->s
.regs
.prefix
;
542 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
543 env
->cputm
= cs
->kvm_run
->s
.regs
.cputm
;
544 env
->ckc
= cs
->kvm_run
->s
.regs
.ckc
;
545 env
->todpr
= cs
->kvm_run
->s
.regs
.todpr
;
546 env
->gbea
= cs
->kvm_run
->s
.regs
.gbea
;
547 env
->pp
= cs
->kvm_run
->s
.regs
.pp
;
550 * These ONE_REGS are not protected by a capability. As they are only
551 * necessary for migration we just trace a possible error, but don't
552 * return with an error return code.
554 kvm_get_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
555 kvm_get_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
556 kvm_get_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
557 kvm_get_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
558 kvm_get_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
561 if (can_sync_regs(cs
, KVM_SYNC_RICCB
)) {
562 memcpy(env
->riccb
, cs
->kvm_run
->s
.regs
.riccb
, 64);
565 /* pfault parameters */
566 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
567 env
->pfault_token
= cs
->kvm_run
->s
.regs
.pft
;
568 env
->pfault_select
= cs
->kvm_run
->s
.regs
.pfs
;
569 env
->pfault_compare
= cs
->kvm_run
->s
.regs
.pfc
;
570 } else if (cap_async_pf
) {
571 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
575 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
579 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
588 int kvm_s390_get_clock(uint8_t *tod_high
, uint64_t *tod_low
)
591 struct kvm_device_attr attr
= {
592 .group
= KVM_S390_VM_TOD
,
593 .attr
= KVM_S390_VM_TOD_LOW
,
594 .addr
= (uint64_t)tod_low
,
597 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
602 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
603 attr
.addr
= (uint64_t)tod_high
;
604 return kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
607 int kvm_s390_set_clock(uint8_t *tod_high
, uint64_t *tod_low
)
611 struct kvm_device_attr attr
= {
612 .group
= KVM_S390_VM_TOD
,
613 .attr
= KVM_S390_VM_TOD_LOW
,
614 .addr
= (uint64_t)tod_low
,
617 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
622 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
623 attr
.addr
= (uint64_t)tod_high
;
624 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
629 * @addr: the logical start address in guest memory
630 * @ar: the access register number
631 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
632 * @len: length that should be transferred
633 * @is_write: true = write, false = read
634 * Returns: 0 on success, non-zero if an exception or error occurred
636 * Use KVM ioctl to read/write from/to guest memory. An access exception
637 * is injected into the vCPU in case of translation errors.
639 int kvm_s390_mem_op(S390CPU
*cpu
, vaddr addr
, uint8_t ar
, void *hostbuf
,
640 int len
, bool is_write
)
642 struct kvm_s390_mem_op mem_op
= {
644 .flags
= KVM_S390_MEMOP_F_INJECT_EXCEPTION
,
646 .op
= is_write
? KVM_S390_MEMOP_LOGICAL_WRITE
647 : KVM_S390_MEMOP_LOGICAL_READ
,
648 .buf
= (uint64_t)hostbuf
,
657 mem_op
.flags
|= KVM_S390_MEMOP_F_CHECK_ONLY
;
660 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_S390_MEM_OP
, &mem_op
);
662 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret
));
668 * Legacy layout for s390:
669 * Older S390 KVM requires the topmost vma of the RAM to be
670 * smaller than an system defined value, which is at least 256GB.
671 * Larger systems have larger values. We put the guest between
672 * the end of data segment (system break) and this value. We
673 * use 32GB as a base to have enough room for the system break
674 * to grow. We also have to use MAP parameters that avoid
675 * read-only mapping of guest pages.
677 static void *legacy_s390_alloc(size_t size
, uint64_t *align
)
681 mem
= mmap((void *) 0x800000000ULL
, size
,
682 PROT_EXEC
|PROT_READ
|PROT_WRITE
,
683 MAP_SHARED
| MAP_ANONYMOUS
| MAP_FIXED
, -1, 0);
684 return mem
== MAP_FAILED
? NULL
: mem
;
687 static uint8_t const *sw_bp_inst
;
688 static uint8_t sw_bp_ilen
;
690 static void determine_sw_breakpoint_instr(void)
692 /* DIAG 501 is used for sw breakpoints with old kernels */
693 static const uint8_t diag_501
[] = {0x83, 0x24, 0x05, 0x01};
694 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
695 static const uint8_t instr_0x0000
[] = {0x00, 0x00};
700 if (kvm_vm_enable_cap(kvm_state
, KVM_CAP_S390_USER_INSTR0
, 0)) {
701 sw_bp_inst
= diag_501
;
702 sw_bp_ilen
= sizeof(diag_501
);
703 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
705 sw_bp_inst
= instr_0x0000
;
706 sw_bp_ilen
= sizeof(instr_0x0000
);
707 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
711 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
713 determine_sw_breakpoint_instr();
715 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
717 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)sw_bp_inst
, sw_bp_ilen
, 1)) {
723 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
727 if (cpu_memory_rw_debug(cs
, bp
->pc
, t
, sw_bp_ilen
, 0)) {
729 } else if (memcmp(t
, sw_bp_inst
, sw_bp_ilen
)) {
731 } else if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
739 static struct kvm_hw_breakpoint
*find_hw_breakpoint(target_ulong addr
,
744 for (n
= 0; n
< nb_hw_breakpoints
; n
++) {
745 if (hw_breakpoints
[n
].addr
== addr
&& hw_breakpoints
[n
].type
== type
&&
746 (hw_breakpoints
[n
].len
== len
|| len
== -1)) {
747 return &hw_breakpoints
[n
];
754 static int insert_hw_breakpoint(target_ulong addr
, int len
, int type
)
758 if (find_hw_breakpoint(addr
, len
, type
)) {
762 size
= (nb_hw_breakpoints
+ 1) * sizeof(struct kvm_hw_breakpoint
);
764 if (!hw_breakpoints
) {
765 nb_hw_breakpoints
= 0;
766 hw_breakpoints
= (struct kvm_hw_breakpoint
*)g_try_malloc(size
);
769 (struct kvm_hw_breakpoint
*)g_try_realloc(hw_breakpoints
, size
);
772 if (!hw_breakpoints
) {
773 nb_hw_breakpoints
= 0;
777 hw_breakpoints
[nb_hw_breakpoints
].addr
= addr
;
778 hw_breakpoints
[nb_hw_breakpoints
].len
= len
;
779 hw_breakpoints
[nb_hw_breakpoints
].type
= type
;
786 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
787 target_ulong len
, int type
)
790 case GDB_BREAKPOINT_HW
:
793 case GDB_WATCHPOINT_WRITE
:
797 type
= KVM_HW_WP_WRITE
;
802 return insert_hw_breakpoint(addr
, len
, type
);
805 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
806 target_ulong len
, int type
)
809 struct kvm_hw_breakpoint
*bp
= find_hw_breakpoint(addr
, len
, type
);
816 if (nb_hw_breakpoints
> 0) {
818 * In order to trim the array, move the last element to the position to
819 * be removed - if necessary.
821 if (bp
!= &hw_breakpoints
[nb_hw_breakpoints
]) {
822 *bp
= hw_breakpoints
[nb_hw_breakpoints
];
824 size
= nb_hw_breakpoints
* sizeof(struct kvm_hw_breakpoint
);
826 (struct kvm_hw_breakpoint
*)g_realloc(hw_breakpoints
, size
);
828 g_free(hw_breakpoints
);
829 hw_breakpoints
= NULL
;
835 void kvm_arch_remove_all_hw_breakpoints(void)
837 nb_hw_breakpoints
= 0;
838 g_free(hw_breakpoints
);
839 hw_breakpoints
= NULL
;
842 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
846 if (nb_hw_breakpoints
> 0) {
847 dbg
->arch
.nr_hw_bp
= nb_hw_breakpoints
;
848 dbg
->arch
.hw_bp
= hw_breakpoints
;
850 for (i
= 0; i
< nb_hw_breakpoints
; ++i
) {
851 hw_breakpoints
[i
].phys_addr
= s390_cpu_get_phys_addr_debug(cpu
,
852 hw_breakpoints
[i
].addr
);
854 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
856 dbg
->arch
.nr_hw_bp
= 0;
857 dbg
->arch
.hw_bp
= NULL
;
861 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
865 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
867 return MEMTXATTRS_UNSPECIFIED
;
870 int kvm_arch_process_async_events(CPUState
*cs
)
875 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq
*irq
,
876 struct kvm_s390_interrupt
*interrupt
)
880 interrupt
->type
= irq
->type
;
882 case KVM_S390_INT_VIRTIO
:
883 interrupt
->parm
= irq
->u
.ext
.ext_params
;
885 case KVM_S390_INT_PFAULT_INIT
:
886 case KVM_S390_INT_PFAULT_DONE
:
887 interrupt
->parm64
= irq
->u
.ext
.ext_params2
;
889 case KVM_S390_PROGRAM_INT
:
890 interrupt
->parm
= irq
->u
.pgm
.code
;
892 case KVM_S390_SIGP_SET_PREFIX
:
893 interrupt
->parm
= irq
->u
.prefix
.address
;
895 case KVM_S390_INT_SERVICE
:
896 interrupt
->parm
= irq
->u
.ext
.ext_params
;
899 interrupt
->parm
= irq
->u
.mchk
.cr14
;
900 interrupt
->parm64
= irq
->u
.mchk
.mcic
;
902 case KVM_S390_INT_EXTERNAL_CALL
:
903 interrupt
->parm
= irq
->u
.extcall
.code
;
905 case KVM_S390_INT_EMERGENCY
:
906 interrupt
->parm
= irq
->u
.emerg
.code
;
908 case KVM_S390_SIGP_STOP
:
909 case KVM_S390_RESTART
:
910 break; /* These types have no parameters */
911 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
912 interrupt
->parm
= irq
->u
.io
.subchannel_id
<< 16;
913 interrupt
->parm
|= irq
->u
.io
.subchannel_nr
;
914 interrupt
->parm64
= (uint64_t)irq
->u
.io
.io_int_parm
<< 32;
915 interrupt
->parm64
|= irq
->u
.io
.io_int_word
;
924 static void inject_vcpu_irq_legacy(CPUState
*cs
, struct kvm_s390_irq
*irq
)
926 struct kvm_s390_interrupt kvmint
= {};
929 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
931 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
935 r
= kvm_vcpu_ioctl(cs
, KVM_S390_INTERRUPT
, &kvmint
);
937 fprintf(stderr
, "KVM failed to inject interrupt\n");
942 void kvm_s390_vcpu_interrupt(S390CPU
*cpu
, struct kvm_s390_irq
*irq
)
944 CPUState
*cs
= CPU(cpu
);
948 r
= kvm_vcpu_ioctl(cs
, KVM_S390_IRQ
, irq
);
952 error_report("KVM failed to inject interrupt %llx", irq
->type
);
956 inject_vcpu_irq_legacy(cs
, irq
);
959 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq
*irq
)
961 struct kvm_s390_interrupt kvmint
= {};
964 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
966 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
970 r
= kvm_vm_ioctl(kvm_state
, KVM_S390_INTERRUPT
, &kvmint
);
972 fprintf(stderr
, "KVM failed to inject interrupt\n");
977 void kvm_s390_floating_interrupt(struct kvm_s390_irq
*irq
)
979 static bool use_flic
= true;
983 r
= kvm_s390_inject_flic(irq
);
991 __kvm_s390_floating_interrupt(irq
);
994 void kvm_s390_service_interrupt(uint32_t parm
)
996 struct kvm_s390_irq irq
= {
997 .type
= KVM_S390_INT_SERVICE
,
998 .u
.ext
.ext_params
= parm
,
1001 kvm_s390_floating_interrupt(&irq
);
1004 static void enter_pgmcheck(S390CPU
*cpu
, uint16_t code
)
1006 struct kvm_s390_irq irq
= {
1007 .type
= KVM_S390_PROGRAM_INT
,
1011 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1014 void kvm_s390_access_exception(S390CPU
*cpu
, uint16_t code
, uint64_t te_code
)
1016 struct kvm_s390_irq irq
= {
1017 .type
= KVM_S390_PROGRAM_INT
,
1019 .u
.pgm
.trans_exc_code
= te_code
,
1020 .u
.pgm
.exc_access_id
= te_code
& 3,
1023 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1026 static int kvm_sclp_service_call(S390CPU
*cpu
, struct kvm_run
*run
,
1029 CPUS390XState
*env
= &cpu
->env
;
1034 cpu_synchronize_state(CPU(cpu
));
1035 sccb
= env
->regs
[ipbh0
& 0xf];
1036 code
= env
->regs
[(ipbh0
& 0xf0) >> 4];
1038 r
= sclp_service_call(env
, sccb
, code
);
1040 enter_pgmcheck(cpu
, -r
);
1048 static int handle_b2(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1050 CPUS390XState
*env
= &cpu
->env
;
1052 uint16_t ipbh0
= (run
->s390_sieic
.ipb
& 0xffff0000) >> 16;
1054 cpu_synchronize_state(CPU(cpu
));
1058 ioinst_handle_xsch(cpu
, env
->regs
[1]);
1061 ioinst_handle_csch(cpu
, env
->regs
[1]);
1064 ioinst_handle_hsch(cpu
, env
->regs
[1]);
1067 ioinst_handle_msch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
1070 ioinst_handle_ssch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
1073 ioinst_handle_stcrw(cpu
, run
->s390_sieic
.ipb
);
1076 ioinst_handle_stsch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
1079 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1080 fprintf(stderr
, "Spurious tsch intercept\n");
1083 ioinst_handle_chsc(cpu
, run
->s390_sieic
.ipb
);
1086 /* This should have been handled by kvm already. */
1087 fprintf(stderr
, "Spurious tpi intercept\n");
1090 ioinst_handle_schm(cpu
, env
->regs
[1], env
->regs
[2],
1091 run
->s390_sieic
.ipb
);
1094 ioinst_handle_rsch(cpu
, env
->regs
[1]);
1097 ioinst_handle_rchp(cpu
, env
->regs
[1]);
1100 /* We do not provide this instruction, it is suppressed. */
1103 ioinst_handle_sal(cpu
, env
->regs
[1]);
1106 /* Not provided, set CC = 3 for subchannel not operational */
1109 case PRIV_B2_SCLP_CALL
:
1110 rc
= kvm_sclp_service_call(cpu
, run
, ipbh0
);
1114 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1
);
1121 static uint64_t get_base_disp_rxy(S390CPU
*cpu
, struct kvm_run
*run
,
1124 CPUS390XState
*env
= &cpu
->env
;
1125 uint32_t x2
= (run
->s390_sieic
.ipa
& 0x000f);
1126 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1127 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1128 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1130 if (disp2
& 0x80000) {
1131 disp2
+= 0xfff00000;
1137 return (base2
? env
->regs
[base2
] : 0) +
1138 (x2
? env
->regs
[x2
] : 0) + (long)(int)disp2
;
1141 static uint64_t get_base_disp_rsy(S390CPU
*cpu
, struct kvm_run
*run
,
1144 CPUS390XState
*env
= &cpu
->env
;
1145 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1146 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1147 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1149 if (disp2
& 0x80000) {
1150 disp2
+= 0xfff00000;
1156 return (base2
? env
->regs
[base2
] : 0) + (long)(int)disp2
;
1159 static int kvm_clp_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1161 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1163 return clp_service_call(cpu
, r2
);
1166 static int kvm_pcilg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1168 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1169 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1171 return pcilg_service_call(cpu
, r1
, r2
);
1174 static int kvm_pcistg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1176 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1177 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1179 return pcistg_service_call(cpu
, r1
, r2
);
1182 static int kvm_stpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1184 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1188 cpu_synchronize_state(CPU(cpu
));
1189 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1191 return stpcifc_service_call(cpu
, r1
, fiba
, ar
);
1194 static int kvm_sic_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1200 static int kvm_rpcit_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1202 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1203 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1205 return rpcit_service_call(cpu
, r1
, r2
);
1208 static int kvm_pcistb_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1210 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1211 uint8_t r3
= run
->s390_sieic
.ipa
& 0x000f;
1215 cpu_synchronize_state(CPU(cpu
));
1216 gaddr
= get_base_disp_rsy(cpu
, run
, &ar
);
1218 return pcistb_service_call(cpu
, r1
, r3
, gaddr
, ar
);
1221 static int kvm_mpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1223 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1227 cpu_synchronize_state(CPU(cpu
));
1228 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1230 return mpcifc_service_call(cpu
, r1
, fiba
, ar
);
1233 static int handle_b9(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1239 r
= kvm_clp_service_call(cpu
, run
);
1241 case PRIV_B9_PCISTG
:
1242 r
= kvm_pcistg_service_call(cpu
, run
);
1245 r
= kvm_pcilg_service_call(cpu
, run
);
1248 r
= kvm_rpcit_service_call(cpu
, run
);
1251 /* just inject exception */
1256 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1
);
1263 static int handle_eb(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1268 case PRIV_EB_PCISTB
:
1269 r
= kvm_pcistb_service_call(cpu
, run
);
1272 r
= kvm_sic_service_call(cpu
, run
);
1275 /* just inject exception */
1280 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl
);
1287 static int handle_e3(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1292 case PRIV_E3_MPCIFC
:
1293 r
= kvm_mpcifc_service_call(cpu
, run
);
1295 case PRIV_E3_STPCIFC
:
1296 r
= kvm_stpcifc_service_call(cpu
, run
);
1300 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl
);
1307 static int handle_hypercall(S390CPU
*cpu
, struct kvm_run
*run
)
1309 CPUS390XState
*env
= &cpu
->env
;
1312 cpu_synchronize_state(CPU(cpu
));
1313 ret
= s390_virtio_hypercall(env
);
1314 if (ret
== -EINVAL
) {
1315 enter_pgmcheck(cpu
, PGM_SPECIFICATION
);
1322 static void kvm_handle_diag_288(S390CPU
*cpu
, struct kvm_run
*run
)
1327 cpu_synchronize_state(CPU(cpu
));
1328 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1329 r3
= run
->s390_sieic
.ipa
& 0x000f;
1330 rc
= handle_diag_288(&cpu
->env
, r1
, r3
);
1332 enter_pgmcheck(cpu
, PGM_SPECIFICATION
);
1336 static void kvm_handle_diag_308(S390CPU
*cpu
, struct kvm_run
*run
)
1340 cpu_synchronize_state(CPU(cpu
));
1341 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1342 r3
= run
->s390_sieic
.ipa
& 0x000f;
1343 handle_diag_308(&cpu
->env
, r1
, r3
);
1346 static int handle_sw_breakpoint(S390CPU
*cpu
, struct kvm_run
*run
)
1348 CPUS390XState
*env
= &cpu
->env
;
1351 cpu_synchronize_state(CPU(cpu
));
1353 pc
= env
->psw
.addr
- sw_bp_ilen
;
1354 if (kvm_find_sw_breakpoint(CPU(cpu
), pc
)) {
1362 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1364 static int handle_diag(S390CPU
*cpu
, struct kvm_run
*run
, uint32_t ipb
)
1370 * For any diagnose call we support, bits 48-63 of the resulting
1371 * address specify the function code; the remainder is ignored.
1373 func_code
= decode_basedisp_rs(&cpu
->env
, ipb
, NULL
) & DIAG_KVM_CODE_MASK
;
1374 switch (func_code
) {
1375 case DIAG_TIMEREVENT
:
1376 kvm_handle_diag_288(cpu
, run
);
1379 kvm_handle_diag_308(cpu
, run
);
1381 case DIAG_KVM_HYPERCALL
:
1382 r
= handle_hypercall(cpu
, run
);
1384 case DIAG_KVM_BREAKPOINT
:
1385 r
= handle_sw_breakpoint(cpu
, run
);
1388 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code
);
1389 enter_pgmcheck(cpu
, PGM_SPECIFICATION
);
1396 typedef struct SigpInfo
{
1399 uint64_t *status_reg
;
1402 static void set_sigp_status(SigpInfo
*si
, uint64_t status
)
1404 *si
->status_reg
&= 0xffffffff00000000ULL
;
1405 *si
->status_reg
|= status
;
1406 si
->cc
= SIGP_CC_STATUS_STORED
;
1409 static void sigp_start(CPUState
*cs
, run_on_cpu_data arg
)
1411 S390CPU
*cpu
= S390_CPU(cs
);
1412 SigpInfo
*si
= arg
.host_ptr
;
1414 if (s390_cpu_get_state(cpu
) != CPU_STATE_STOPPED
) {
1415 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1419 s390_cpu_set_state(CPU_STATE_OPERATING
, cpu
);
1420 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1423 static void sigp_stop(CPUState
*cs
, run_on_cpu_data arg
)
1425 S390CPU
*cpu
= S390_CPU(cs
);
1426 SigpInfo
*si
= arg
.host_ptr
;
1427 struct kvm_s390_irq irq
= {
1428 .type
= KVM_S390_SIGP_STOP
,
1431 if (s390_cpu_get_state(cpu
) != CPU_STATE_OPERATING
) {
1432 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1436 /* disabled wait - sleeping in user space */
1438 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
1440 /* execute the stop function */
1441 cpu
->env
.sigp_order
= SIGP_STOP
;
1442 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1444 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1447 #define ADTL_SAVE_AREA_SIZE 1024
1448 static int kvm_s390_store_adtl_status(S390CPU
*cpu
, hwaddr addr
)
1451 hwaddr len
= ADTL_SAVE_AREA_SIZE
;
1453 mem
= cpu_physical_memory_map(addr
, &len
, 1);
1457 if (len
!= ADTL_SAVE_AREA_SIZE
) {
1458 cpu_physical_memory_unmap(mem
, len
, 1, 0);
1462 memcpy(mem
, &cpu
->env
.vregs
, 512);
1464 cpu_physical_memory_unmap(mem
, len
, 1, len
);
1469 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1470 #define SAVE_AREA_SIZE 512
1471 static int kvm_s390_store_status(S390CPU
*cpu
, hwaddr addr
, bool store_arch
)
1473 static const uint8_t ar_id
= 1;
1474 uint64_t ckc
= cpu
->env
.ckc
>> 8;
1477 hwaddr len
= SAVE_AREA_SIZE
;
1479 mem
= cpu_physical_memory_map(addr
, &len
, 1);
1483 if (len
!= SAVE_AREA_SIZE
) {
1484 cpu_physical_memory_unmap(mem
, len
, 1, 0);
1489 cpu_physical_memory_write(offsetof(LowCore
, ar_access_id
), &ar_id
, 1);
1491 for (i
= 0; i
< 16; ++i
) {
1492 *((uint64_t *)mem
+ i
) = get_freg(&cpu
->env
, i
)->ll
;
1494 memcpy(mem
+ 128, &cpu
->env
.regs
, 128);
1495 memcpy(mem
+ 256, &cpu
->env
.psw
, 16);
1496 memcpy(mem
+ 280, &cpu
->env
.psa
, 4);
1497 memcpy(mem
+ 284, &cpu
->env
.fpc
, 4);
1498 memcpy(mem
+ 292, &cpu
->env
.todpr
, 4);
1499 memcpy(mem
+ 296, &cpu
->env
.cputm
, 8);
1500 memcpy(mem
+ 304, &ckc
, 8);
1501 memcpy(mem
+ 320, &cpu
->env
.aregs
, 64);
1502 memcpy(mem
+ 384, &cpu
->env
.cregs
, 128);
1504 cpu_physical_memory_unmap(mem
, len
, 1, len
);
1509 static void sigp_stop_and_store_status(CPUState
*cs
, run_on_cpu_data arg
)
1511 S390CPU
*cpu
= S390_CPU(cs
);
1512 SigpInfo
*si
= arg
.host_ptr
;
1513 struct kvm_s390_irq irq
= {
1514 .type
= KVM_S390_SIGP_STOP
,
1517 /* disabled wait - sleeping in user space */
1518 if (s390_cpu_get_state(cpu
) == CPU_STATE_OPERATING
&& cs
->halted
) {
1519 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
1522 switch (s390_cpu_get_state(cpu
)) {
1523 case CPU_STATE_OPERATING
:
1524 cpu
->env
.sigp_order
= SIGP_STOP_STORE_STATUS
;
1525 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1526 /* store will be performed when handling the stop intercept */
1528 case CPU_STATE_STOPPED
:
1529 /* already stopped, just store the status */
1530 cpu_synchronize_state(cs
);
1531 kvm_s390_store_status(cpu
, KVM_S390_STORE_STATUS_DEF_ADDR
, true);
1534 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1537 static void sigp_store_status_at_address(CPUState
*cs
, run_on_cpu_data arg
)
1539 S390CPU
*cpu
= S390_CPU(cs
);
1540 SigpInfo
*si
= arg
.host_ptr
;
1541 uint32_t address
= si
->param
& 0x7ffffe00u
;
1543 /* cpu has to be stopped */
1544 if (s390_cpu_get_state(cpu
) != CPU_STATE_STOPPED
) {
1545 set_sigp_status(si
, SIGP_STAT_INCORRECT_STATE
);
1549 cpu_synchronize_state(cs
);
1551 if (kvm_s390_store_status(cpu
, address
, false)) {
1552 set_sigp_status(si
, SIGP_STAT_INVALID_PARAMETER
);
1555 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1558 static void sigp_store_adtl_status(CPUState
*cs
, run_on_cpu_data arg
)
1560 S390CPU
*cpu
= S390_CPU(cs
);
1561 SigpInfo
*si
= arg
.host_ptr
;
1563 if (!s390_has_feat(S390_FEAT_VECTOR
)) {
1564 set_sigp_status(si
, SIGP_STAT_INVALID_ORDER
);
1568 /* cpu has to be stopped */
1569 if (s390_cpu_get_state(cpu
) != CPU_STATE_STOPPED
) {
1570 set_sigp_status(si
, SIGP_STAT_INCORRECT_STATE
);
1574 /* parameter must be aligned to 1024-byte boundary */
1575 if (si
->param
& 0x3ff) {
1576 set_sigp_status(si
, SIGP_STAT_INVALID_PARAMETER
);
1580 cpu_synchronize_state(cs
);
1582 if (kvm_s390_store_adtl_status(cpu
, si
->param
)) {
1583 set_sigp_status(si
, SIGP_STAT_INVALID_PARAMETER
);
1586 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1589 static void sigp_restart(CPUState
*cs
, run_on_cpu_data arg
)
1591 S390CPU
*cpu
= S390_CPU(cs
);
1592 SigpInfo
*si
= arg
.host_ptr
;
1593 struct kvm_s390_irq irq
= {
1594 .type
= KVM_S390_RESTART
,
1597 switch (s390_cpu_get_state(cpu
)) {
1598 case CPU_STATE_STOPPED
:
1599 /* the restart irq has to be delivered prior to any other pending irq */
1600 cpu_synchronize_state(cs
);
1601 do_restart_interrupt(&cpu
->env
);
1602 s390_cpu_set_state(CPU_STATE_OPERATING
, cpu
);
1604 case CPU_STATE_OPERATING
:
1605 kvm_s390_vcpu_interrupt(cpu
, &irq
);
1608 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1611 int kvm_s390_cpu_restart(S390CPU
*cpu
)
1615 run_on_cpu(CPU(cpu
), sigp_restart
, RUN_ON_CPU_HOST_PTR(&si
));
1616 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu
->env
);
1620 static void sigp_initial_cpu_reset(CPUState
*cs
, run_on_cpu_data arg
)
1622 S390CPU
*cpu
= S390_CPU(cs
);
1623 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
1624 SigpInfo
*si
= arg
.host_ptr
;
1626 cpu_synchronize_state(cs
);
1627 scc
->initial_cpu_reset(cs
);
1628 cpu_synchronize_post_reset(cs
);
1629 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1632 static void sigp_cpu_reset(CPUState
*cs
, run_on_cpu_data arg
)
1634 S390CPU
*cpu
= S390_CPU(cs
);
1635 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
1636 SigpInfo
*si
= arg
.host_ptr
;
1638 cpu_synchronize_state(cs
);
1640 cpu_synchronize_post_reset(cs
);
1641 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1644 static void sigp_set_prefix(CPUState
*cs
, run_on_cpu_data arg
)
1646 S390CPU
*cpu
= S390_CPU(cs
);
1647 SigpInfo
*si
= arg
.host_ptr
;
1648 uint32_t addr
= si
->param
& 0x7fffe000u
;
1650 cpu_synchronize_state(cs
);
1652 if (!address_space_access_valid(&address_space_memory
, addr
,
1653 sizeof(struct LowCore
), false)) {
1654 set_sigp_status(si
, SIGP_STAT_INVALID_PARAMETER
);
1658 /* cpu has to be stopped */
1659 if (s390_cpu_get_state(cpu
) != CPU_STATE_STOPPED
) {
1660 set_sigp_status(si
, SIGP_STAT_INCORRECT_STATE
);
1664 cpu
->env
.psa
= addr
;
1665 cpu_synchronize_post_init(cs
);
1666 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1669 static int handle_sigp_single_dst(S390CPU
*dst_cpu
, uint8_t order
,
1670 uint64_t param
, uint64_t *status_reg
)
1674 .status_reg
= status_reg
,
1677 /* cpu available? */
1678 if (dst_cpu
== NULL
) {
1679 return SIGP_CC_NOT_OPERATIONAL
;
1682 /* only resets can break pending orders */
1683 if (dst_cpu
->env
.sigp_order
!= 0 &&
1684 order
!= SIGP_CPU_RESET
&&
1685 order
!= SIGP_INITIAL_CPU_RESET
) {
1686 return SIGP_CC_BUSY
;
1691 run_on_cpu(CPU(dst_cpu
), sigp_start
, RUN_ON_CPU_HOST_PTR(&si
));
1694 run_on_cpu(CPU(dst_cpu
), sigp_stop
, RUN_ON_CPU_HOST_PTR(&si
));
1697 run_on_cpu(CPU(dst_cpu
), sigp_restart
, RUN_ON_CPU_HOST_PTR(&si
));
1699 case SIGP_STOP_STORE_STATUS
:
1700 run_on_cpu(CPU(dst_cpu
), sigp_stop_and_store_status
, RUN_ON_CPU_HOST_PTR(&si
));
1702 case SIGP_STORE_STATUS_ADDR
:
1703 run_on_cpu(CPU(dst_cpu
), sigp_store_status_at_address
, RUN_ON_CPU_HOST_PTR(&si
));
1705 case SIGP_STORE_ADTL_STATUS
:
1706 run_on_cpu(CPU(dst_cpu
), sigp_store_adtl_status
, RUN_ON_CPU_HOST_PTR(&si
));
1708 case SIGP_SET_PREFIX
:
1709 run_on_cpu(CPU(dst_cpu
), sigp_set_prefix
, RUN_ON_CPU_HOST_PTR(&si
));
1711 case SIGP_INITIAL_CPU_RESET
:
1712 run_on_cpu(CPU(dst_cpu
), sigp_initial_cpu_reset
, RUN_ON_CPU_HOST_PTR(&si
));
1714 case SIGP_CPU_RESET
:
1715 run_on_cpu(CPU(dst_cpu
), sigp_cpu_reset
, RUN_ON_CPU_HOST_PTR(&si
));
1718 DPRINTF("KVM: unknown SIGP: 0x%x\n", order
);
1719 set_sigp_status(&si
, SIGP_STAT_INVALID_ORDER
);
1725 static int sigp_set_architecture(S390CPU
*cpu
, uint32_t param
,
1726 uint64_t *status_reg
)
1731 /* due to the BQL, we are the only active cpu */
1732 CPU_FOREACH(cur_cs
) {
1733 cur_cpu
= S390_CPU(cur_cs
);
1734 if (cur_cpu
->env
.sigp_order
!= 0) {
1735 return SIGP_CC_BUSY
;
1737 cpu_synchronize_state(cur_cs
);
1738 /* all but the current one have to be stopped */
1739 if (cur_cpu
!= cpu
&&
1740 s390_cpu_get_state(cur_cpu
) != CPU_STATE_STOPPED
) {
1741 *status_reg
&= 0xffffffff00000000ULL
;
1742 *status_reg
|= SIGP_STAT_INCORRECT_STATE
;
1743 return SIGP_CC_STATUS_STORED
;
1747 switch (param
& 0xff) {
1748 case SIGP_MODE_ESA_S390
:
1750 return SIGP_CC_NOT_OPERATIONAL
;
1751 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW
:
1752 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW
:
1753 CPU_FOREACH(cur_cs
) {
1754 cur_cpu
= S390_CPU(cur_cs
);
1755 cur_cpu
->env
.pfault_token
= -1UL;
1759 *status_reg
&= 0xffffffff00000000ULL
;
1760 *status_reg
|= SIGP_STAT_INVALID_PARAMETER
;
1761 return SIGP_CC_STATUS_STORED
;
1764 return SIGP_CC_ORDER_CODE_ACCEPTED
;
1767 #define SIGP_ORDER_MASK 0x000000ff
1769 static int handle_sigp(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1771 CPUS390XState
*env
= &cpu
->env
;
1772 const uint8_t r1
= ipa1
>> 4;
1773 const uint8_t r3
= ipa1
& 0x0f;
1776 uint64_t *status_reg
;
1778 S390CPU
*dst_cpu
= NULL
;
1780 cpu_synchronize_state(CPU(cpu
));
1782 /* get order code */
1783 order
= decode_basedisp_rs(env
, run
->s390_sieic
.ipb
, NULL
)
1785 status_reg
= &env
->regs
[r1
];
1786 param
= (r1
% 2) ? env
->regs
[r1
] : env
->regs
[r1
+ 1];
1788 if (qemu_mutex_trylock(&qemu_sigp_mutex
)) {
1795 ret
= sigp_set_architecture(cpu
, param
, status_reg
);
1798 /* all other sigp orders target a single vcpu */
1799 dst_cpu
= s390_cpu_addr2state(env
->regs
[r3
]);
1800 ret
= handle_sigp_single_dst(dst_cpu
, order
, param
, status_reg
);
1802 qemu_mutex_unlock(&qemu_sigp_mutex
);
1805 trace_kvm_sigp_finished(order
, CPU(cpu
)->cpu_index
,
1806 dst_cpu
? CPU(dst_cpu
)->cpu_index
: -1, ret
);
1816 static int handle_instruction(S390CPU
*cpu
, struct kvm_run
*run
)
1818 unsigned int ipa0
= (run
->s390_sieic
.ipa
& 0xff00);
1819 uint8_t ipa1
= run
->s390_sieic
.ipa
& 0x00ff;
1822 DPRINTF("handle_instruction 0x%x 0x%x\n",
1823 run
->s390_sieic
.ipa
, run
->s390_sieic
.ipb
);
1826 r
= handle_b2(cpu
, run
, ipa1
);
1829 r
= handle_b9(cpu
, run
, ipa1
);
1832 r
= handle_eb(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1835 r
= handle_e3(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1838 r
= handle_diag(cpu
, run
, run
->s390_sieic
.ipb
);
1841 r
= handle_sigp(cpu
, run
, ipa1
);
1847 enter_pgmcheck(cpu
, 0x0001);
1853 static bool is_special_wait_psw(CPUState
*cs
)
1855 /* signal quiesce */
1856 return cs
->kvm_run
->psw_addr
== 0xfffUL
;
1859 static void unmanageable_intercept(S390CPU
*cpu
, const char *str
, int pswoffset
)
1861 CPUState
*cs
= CPU(cpu
);
1863 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1864 str
, cs
->cpu_index
, ldq_phys(cs
->as
, cpu
->env
.psa
+ pswoffset
),
1865 ldq_phys(cs
->as
, cpu
->env
.psa
+ pswoffset
+ 8));
1867 qemu_system_guest_panicked(NULL
);
1870 /* try to detect pgm check loops */
1871 static int handle_oper_loop(S390CPU
*cpu
, struct kvm_run
*run
)
1873 CPUState
*cs
= CPU(cpu
);
1876 cpu_synchronize_state(cs
);
1877 newpsw
.mask
= ldq_phys(cs
->as
, cpu
->env
.psa
+
1878 offsetof(LowCore
, program_new_psw
));
1879 newpsw
.addr
= ldq_phys(cs
->as
, cpu
->env
.psa
+
1880 offsetof(LowCore
, program_new_psw
) + 8);
1881 oldpsw
.mask
= run
->psw_mask
;
1882 oldpsw
.addr
= run
->psw_addr
;
1884 * Avoid endless loops of operation exceptions, if the pgm new
1885 * PSW will cause a new operation exception.
1886 * The heuristic checks if the pgm new psw is within 6 bytes before
1887 * the faulting psw address (with same DAT, AS settings) and the
1888 * new psw is not a wait psw and the fault was not triggered by
1889 * problem state. In that case go into crashed state.
1892 if (oldpsw
.addr
- newpsw
.addr
<= 6 &&
1893 !(newpsw
.mask
& PSW_MASK_WAIT
) &&
1894 !(oldpsw
.mask
& PSW_MASK_PSTATE
) &&
1895 (newpsw
.mask
& PSW_MASK_ASC
) == (oldpsw
.mask
& PSW_MASK_ASC
) &&
1896 (newpsw
.mask
& PSW_MASK_DAT
) == (oldpsw
.mask
& PSW_MASK_DAT
)) {
1897 unmanageable_intercept(cpu
, "operation exception loop",
1898 offsetof(LowCore
, program_new_psw
));
1904 static int handle_intercept(S390CPU
*cpu
)
1906 CPUState
*cs
= CPU(cpu
);
1907 struct kvm_run
*run
= cs
->kvm_run
;
1908 int icpt_code
= run
->s390_sieic
.icptcode
;
1911 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code
,
1912 (long)cs
->kvm_run
->psw_addr
);
1913 switch (icpt_code
) {
1914 case ICPT_INSTRUCTION
:
1915 r
= handle_instruction(cpu
, run
);
1918 unmanageable_intercept(cpu
, "program interrupt",
1919 offsetof(LowCore
, program_new_psw
));
1923 unmanageable_intercept(cpu
, "external interrupt",
1924 offsetof(LowCore
, external_new_psw
));
1928 /* disabled wait, since enabled wait is handled in kernel */
1929 cpu_synchronize_state(cs
);
1930 if (s390_cpu_halt(cpu
) == 0) {
1931 if (is_special_wait_psw(cs
)) {
1932 qemu_system_shutdown_request();
1934 qemu_system_guest_panicked(NULL
);
1940 if (s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
) == 0) {
1941 qemu_system_shutdown_request();
1943 if (cpu
->env
.sigp_order
== SIGP_STOP_STORE_STATUS
) {
1944 kvm_s390_store_status(cpu
, KVM_S390_STORE_STATUS_DEF_ADDR
,
1947 cpu
->env
.sigp_order
= 0;
1951 /* check for break points */
1952 r
= handle_sw_breakpoint(cpu
, run
);
1954 /* Then check for potential pgm check loops */
1955 r
= handle_oper_loop(cpu
, run
);
1957 enter_pgmcheck(cpu
, PGM_OPERATION
);
1961 case ICPT_SOFT_INTERCEPT
:
1962 fprintf(stderr
, "KVM unimplemented icpt SOFT\n");
1966 fprintf(stderr
, "KVM unimplemented icpt IO\n");
1970 fprintf(stderr
, "Unknown intercept code: %d\n", icpt_code
);
1978 static int handle_tsch(S390CPU
*cpu
)
1980 CPUState
*cs
= CPU(cpu
);
1981 struct kvm_run
*run
= cs
->kvm_run
;
1984 cpu_synchronize_state(cs
);
1986 ret
= ioinst_handle_tsch(cpu
, cpu
->env
.regs
[1], run
->s390_tsch
.ipb
);
1990 * If an I/O interrupt had been dequeued, we have to reinject it.
1992 if (run
->s390_tsch
.dequeued
) {
1993 kvm_s390_io_interrupt(run
->s390_tsch
.subchannel_id
,
1994 run
->s390_tsch
.subchannel_nr
,
1995 run
->s390_tsch
.io_int_parm
,
1996 run
->s390_tsch
.io_int_word
);
2003 static void insert_stsi_3_2_2(S390CPU
*cpu
, __u64 addr
, uint8_t ar
)
2005 struct sysib_322 sysib
;
2008 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &sysib
, sizeof(sysib
))) {
2011 /* Shift the stack of Extended Names to prepare for our own data */
2012 memmove(&sysib
.ext_names
[1], &sysib
.ext_names
[0],
2013 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- 1));
2014 /* First virt level, that doesn't provide Ext Names delimits stack. It is
2015 * assumed it's not capable of managing Extended Names for lower levels.
2017 for (del
= 1; del
< sysib
.count
; del
++) {
2018 if (!sysib
.vm
[del
].ext_name_encoding
|| !sysib
.ext_names
[del
][0]) {
2022 if (del
< sysib
.count
) {
2023 memset(sysib
.ext_names
[del
], 0,
2024 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- del
));
2026 /* Insert short machine name in EBCDIC, padded with blanks */
2028 memset(sysib
.vm
[0].name
, 0x40, sizeof(sysib
.vm
[0].name
));
2029 ebcdic_put(sysib
.vm
[0].name
, qemu_name
, MIN(sizeof(sysib
.vm
[0].name
),
2030 strlen(qemu_name
)));
2032 sysib
.vm
[0].ext_name_encoding
= 2; /* 2 = UTF-8 */
2033 memset(sysib
.ext_names
[0], 0, sizeof(sysib
.ext_names
[0]));
2034 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
2035 * considered by s390 as not capable of providing any Extended Name.
2036 * Therefore if no name was specified on qemu invocation, we go with the
2037 * same "KVMguest" default, which KVM has filled into short name field.
2040 strncpy((char *)sysib
.ext_names
[0], qemu_name
,
2041 sizeof(sysib
.ext_names
[0]));
2043 strcpy((char *)sysib
.ext_names
[0], "KVMguest");
2046 memcpy(sysib
.vm
[0].uuid
, &qemu_uuid
, sizeof(sysib
.vm
[0].uuid
));
2048 s390_cpu_virt_mem_write(cpu
, addr
, ar
, &sysib
, sizeof(sysib
));
2051 static int handle_stsi(S390CPU
*cpu
)
2053 CPUState
*cs
= CPU(cpu
);
2054 struct kvm_run
*run
= cs
->kvm_run
;
2056 switch (run
->s390_stsi
.fc
) {
2058 if (run
->s390_stsi
.sel1
!= 2 || run
->s390_stsi
.sel2
!= 2) {
2061 /* Only sysib 3.2.2 needs post-handling for now. */
2062 insert_stsi_3_2_2(cpu
, run
->s390_stsi
.addr
, run
->s390_stsi
.ar
);
2069 static int kvm_arch_handle_debug_exit(S390CPU
*cpu
)
2071 CPUState
*cs
= CPU(cpu
);
2072 struct kvm_run
*run
= cs
->kvm_run
;
2075 struct kvm_debug_exit_arch
*arch_info
= &run
->debug
.arch
;
2077 switch (arch_info
->type
) {
2078 case KVM_HW_WP_WRITE
:
2079 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
2080 cs
->watchpoint_hit
= &hw_watchpoint
;
2081 hw_watchpoint
.vaddr
= arch_info
->addr
;
2082 hw_watchpoint
.flags
= BP_MEM_WRITE
;
2087 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
2091 case KVM_SINGLESTEP
:
2092 if (cs
->singlestep_enabled
) {
2103 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
2105 S390CPU
*cpu
= S390_CPU(cs
);
2108 qemu_mutex_lock_iothread();
2110 switch (run
->exit_reason
) {
2111 case KVM_EXIT_S390_SIEIC
:
2112 ret
= handle_intercept(cpu
);
2114 case KVM_EXIT_S390_RESET
:
2115 s390_reipl_request();
2117 case KVM_EXIT_S390_TSCH
:
2118 ret
= handle_tsch(cpu
);
2120 case KVM_EXIT_S390_STSI
:
2121 ret
= handle_stsi(cpu
);
2123 case KVM_EXIT_DEBUG
:
2124 ret
= kvm_arch_handle_debug_exit(cpu
);
2127 fprintf(stderr
, "Unknown KVM exit: %d\n", run
->exit_reason
);
2130 qemu_mutex_unlock_iothread();
2133 ret
= EXCP_INTERRUPT
;
2138 bool kvm_arch_stop_on_emulation_error(CPUState
*cpu
)
2143 void kvm_s390_io_interrupt(uint16_t subchannel_id
,
2144 uint16_t subchannel_nr
, uint32_t io_int_parm
,
2145 uint32_t io_int_word
)
2147 struct kvm_s390_irq irq
= {
2148 .u
.io
.subchannel_id
= subchannel_id
,
2149 .u
.io
.subchannel_nr
= subchannel_nr
,
2150 .u
.io
.io_int_parm
= io_int_parm
,
2151 .u
.io
.io_int_word
= io_int_word
,
2154 if (io_int_word
& IO_INT_WORD_AI
) {
2155 irq
.type
= KVM_S390_INT_IO(1, 0, 0, 0);
2157 irq
.type
= KVM_S390_INT_IO(0, (subchannel_id
& 0xff00) >> 8,
2158 (subchannel_id
& 0x0006),
2161 kvm_s390_floating_interrupt(&irq
);
2164 static uint64_t build_channel_report_mcic(void)
2168 /* subclass: indicate channel report pending */
2170 /* subclass modifiers: none */
2171 /* storage errors: none */
2172 /* validity bits: no damage */
2173 MCIC_VB_WP
| MCIC_VB_MS
| MCIC_VB_PM
| MCIC_VB_IA
| MCIC_VB_FP
|
2174 MCIC_VB_GR
| MCIC_VB_CR
| MCIC_VB_ST
| MCIC_VB_AR
| MCIC_VB_PR
|
2175 MCIC_VB_FC
| MCIC_VB_CT
| MCIC_VB_CC
;
2176 if (s390_has_feat(S390_FEAT_VECTOR
)) {
2182 void kvm_s390_crw_mchk(void)
2184 struct kvm_s390_irq irq
= {
2185 .type
= KVM_S390_MCHK
,
2186 .u
.mchk
.cr14
= 1 << 28,
2187 .u
.mchk
.mcic
= build_channel_report_mcic(),
2189 kvm_s390_floating_interrupt(&irq
);
2192 void kvm_s390_enable_css_support(S390CPU
*cpu
)
2196 /* Activate host kernel channel subsystem support. */
2197 r
= kvm_vcpu_enable_cap(CPU(cpu
), KVM_CAP_S390_CSS_SUPPORT
, 0);
2201 void kvm_arch_init_irq_routing(KVMState
*s
)
2204 * Note that while irqchip capabilities generally imply that cpustates
2205 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2206 * have to override the common code kvm_halt_in_kernel_allowed setting.
2208 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
2209 kvm_gsi_routing_allowed
= true;
2210 kvm_halt_in_kernel_allowed
= false;
2214 int kvm_s390_assign_subch_ioeventfd(EventNotifier
*notifier
, uint32_t sch
,
2215 int vq
, bool assign
)
2217 struct kvm_ioeventfd kick
= {
2218 .flags
= KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY
|
2219 KVM_IOEVENTFD_FLAG_DATAMATCH
,
2220 .fd
= event_notifier_get_fd(notifier
),
2225 if (!kvm_check_extension(kvm_state
, KVM_CAP_IOEVENTFD
)) {
2229 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
2231 return kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
2234 int kvm_s390_get_memslot_count(KVMState
*s
)
2236 return kvm_check_extension(s
, KVM_CAP_NR_MEMSLOTS
);
2239 int kvm_s390_get_ri(void)
2244 int kvm_s390_set_cpu_state(S390CPU
*cpu
, uint8_t cpu_state
)
2246 struct kvm_mp_state mp_state
= {};
2249 /* the kvm part might not have been initialized yet */
2250 if (CPU(cpu
)->kvm_state
== NULL
) {
2254 switch (cpu_state
) {
2255 case CPU_STATE_STOPPED
:
2256 mp_state
.mp_state
= KVM_MP_STATE_STOPPED
;
2258 case CPU_STATE_CHECK_STOP
:
2259 mp_state
.mp_state
= KVM_MP_STATE_CHECK_STOP
;
2261 case CPU_STATE_OPERATING
:
2262 mp_state
.mp_state
= KVM_MP_STATE_OPERATING
;
2264 case CPU_STATE_LOAD
:
2265 mp_state
.mp_state
= KVM_MP_STATE_LOAD
;
2268 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2273 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
2275 trace_kvm_failed_cpu_state_set(CPU(cpu
)->cpu_index
, cpu_state
,
2282 void kvm_s390_vcpu_interrupt_pre_save(S390CPU
*cpu
)
2284 struct kvm_s390_irq_state irq_state
;
2285 CPUState
*cs
= CPU(cpu
);
2288 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_IRQ_STATE
)) {
2292 irq_state
.buf
= (uint64_t) cpu
->irqstate
;
2293 irq_state
.len
= VCPU_IRQ_BUF_SIZE
;
2295 bytes
= kvm_vcpu_ioctl(cs
, KVM_S390_GET_IRQ_STATE
, &irq_state
);
2297 cpu
->irqstate_saved_size
= 0;
2298 error_report("Migration of interrupt state failed");
2302 cpu
->irqstate_saved_size
= bytes
;
2305 int kvm_s390_vcpu_interrupt_post_load(S390CPU
*cpu
)
2307 CPUState
*cs
= CPU(cpu
);
2308 struct kvm_s390_irq_state irq_state
;
2311 if (cpu
->irqstate_saved_size
== 0) {
2315 if (!kvm_check_extension(kvm_state
, KVM_CAP_S390_IRQ_STATE
)) {
2319 irq_state
.buf
= (uint64_t) cpu
->irqstate
;
2320 irq_state
.len
= cpu
->irqstate_saved_size
;
2322 r
= kvm_vcpu_ioctl(cs
, KVM_S390_SET_IRQ_STATE
, &irq_state
);
2324 error_report("Setting interrupt state failed %d", r
);
2329 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
2330 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
2332 S390PCIBusDevice
*pbdev
;
2333 uint32_t idx
= data
>> ZPCI_MSI_VEC_BITS
;
2334 uint32_t vec
= data
& ZPCI_MSI_VEC_MASK
;
2336 pbdev
= s390_pci_find_dev_by_idx(s390_get_phb(), idx
);
2338 DPRINTF("add_msi_route no dev\n");
2342 pbdev
->routes
.adapter
.ind_offset
= vec
;
2344 route
->type
= KVM_IRQ_ROUTING_S390_ADAPTER
;
2346 route
->u
.adapter
.summary_addr
= pbdev
->routes
.adapter
.summary_addr
;
2347 route
->u
.adapter
.ind_addr
= pbdev
->routes
.adapter
.ind_addr
;
2348 route
->u
.adapter
.summary_offset
= pbdev
->routes
.adapter
.summary_offset
;
2349 route
->u
.adapter
.ind_offset
= pbdev
->routes
.adapter
.ind_offset
;
2350 route
->u
.adapter
.adapter_id
= pbdev
->routes
.adapter
.adapter_id
;
2354 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
2355 int vector
, PCIDevice
*dev
)
2360 int kvm_arch_release_virq_post(int virq
)
2365 int kvm_arch_msi_data_to_gsi(uint32_t data
)
2370 static inline int test_bit_inv(long nr
, const unsigned long *addr
)
2372 return test_bit(BE_BIT_NR(nr
), addr
);
2375 static inline void set_bit_inv(long nr
, unsigned long *addr
)
2377 set_bit(BE_BIT_NR(nr
), addr
);
2380 static int query_cpu_subfunc(S390FeatBitmap features
)
2382 struct kvm_s390_vm_cpu_subfunc prop
;
2383 struct kvm_device_attr attr
= {
2384 .group
= KVM_S390_VM_CPU_MODEL
,
2385 .attr
= KVM_S390_VM_CPU_MACHINE_SUBFUNC
,
2386 .addr
= (uint64_t) &prop
,
2390 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2396 * We're going to add all subfunctions now, if the corresponding feature
2397 * is available that unlocks the query functions.
2399 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PLO
, prop
.plo
);
2400 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING
, features
)) {
2401 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PTFF
, prop
.ptff
);
2403 if (test_bit(S390_FEAT_MSA
, features
)) {
2404 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMAC
, prop
.kmac
);
2405 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMC
, prop
.kmc
);
2406 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KM
, prop
.km
);
2407 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KIMD
, prop
.kimd
);
2408 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KLMD
, prop
.klmd
);
2410 if (test_bit(S390_FEAT_MSA_EXT_3
, features
)) {
2411 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PCKMO
, prop
.pckmo
);
2413 if (test_bit(S390_FEAT_MSA_EXT_4
, features
)) {
2414 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMCTR
, prop
.kmctr
);
2415 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMF
, prop
.kmf
);
2416 s390_add_from_feat_block(features
, S390_FEAT_TYPE_KMO
, prop
.kmo
);
2417 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PCC
, prop
.pcc
);
2419 if (test_bit(S390_FEAT_MSA_EXT_5
, features
)) {
2420 s390_add_from_feat_block(features
, S390_FEAT_TYPE_PPNO
, prop
.ppno
);
2425 static int configure_cpu_subfunc(const S390FeatBitmap features
)
2427 struct kvm_s390_vm_cpu_subfunc prop
= {};
2428 struct kvm_device_attr attr
= {
2429 .group
= KVM_S390_VM_CPU_MODEL
,
2430 .attr
= KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
,
2431 .addr
= (uint64_t) &prop
,
2434 if (!kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2435 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
)) {
2436 /* hardware support might be missing, IBC will handle most of this */
2440 s390_fill_feat_block(features
, S390_FEAT_TYPE_PLO
, prop
.plo
);
2441 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING
, features
)) {
2442 s390_fill_feat_block(features
, S390_FEAT_TYPE_PTFF
, prop
.ptff
);
2443 prop
.ptff
[0] |= 0x80; /* query is always available */
2445 if (test_bit(S390_FEAT_MSA
, features
)) {
2446 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMAC
, prop
.kmac
);
2447 prop
.kmac
[0] |= 0x80; /* query is always available */
2448 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMC
, prop
.kmc
);
2449 prop
.kmc
[0] |= 0x80; /* query is always available */
2450 s390_fill_feat_block(features
, S390_FEAT_TYPE_KM
, prop
.km
);
2451 prop
.km
[0] |= 0x80; /* query is always available */
2452 s390_fill_feat_block(features
, S390_FEAT_TYPE_KIMD
, prop
.kimd
);
2453 prop
.kimd
[0] |= 0x80; /* query is always available */
2454 s390_fill_feat_block(features
, S390_FEAT_TYPE_KLMD
, prop
.klmd
);
2455 prop
.klmd
[0] |= 0x80; /* query is always available */
2457 if (test_bit(S390_FEAT_MSA_EXT_3
, features
)) {
2458 s390_fill_feat_block(features
, S390_FEAT_TYPE_PCKMO
, prop
.pckmo
);
2459 prop
.pckmo
[0] |= 0x80; /* query is always available */
2461 if (test_bit(S390_FEAT_MSA_EXT_4
, features
)) {
2462 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMCTR
, prop
.kmctr
);
2463 prop
.kmctr
[0] |= 0x80; /* query is always available */
2464 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMF
, prop
.kmf
);
2465 prop
.kmf
[0] |= 0x80; /* query is always available */
2466 s390_fill_feat_block(features
, S390_FEAT_TYPE_KMO
, prop
.kmo
);
2467 prop
.kmo
[0] |= 0x80; /* query is always available */
2468 s390_fill_feat_block(features
, S390_FEAT_TYPE_PCC
, prop
.pcc
);
2469 prop
.pcc
[0] |= 0x80; /* query is always available */
2471 if (test_bit(S390_FEAT_MSA_EXT_5
, features
)) {
2472 s390_fill_feat_block(features
, S390_FEAT_TYPE_PPNO
, prop
.ppno
);
2473 prop
.ppno
[0] |= 0x80; /* query is always available */
2475 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2478 static int kvm_to_feat
[][2] = {
2479 { KVM_S390_VM_CPU_FEAT_ESOP
, S390_FEAT_ESOP
},
2480 { KVM_S390_VM_CPU_FEAT_SIEF2
, S390_FEAT_SIE_F2
},
2481 { KVM_S390_VM_CPU_FEAT_64BSCAO
, S390_FEAT_SIE_64BSCAO
},
2482 { KVM_S390_VM_CPU_FEAT_SIIF
, S390_FEAT_SIE_SIIF
},
2483 { KVM_S390_VM_CPU_FEAT_GPERE
, S390_FEAT_SIE_GPERE
},
2484 { KVM_S390_VM_CPU_FEAT_GSLS
, S390_FEAT_SIE_GSLS
},
2485 { KVM_S390_VM_CPU_FEAT_IB
, S390_FEAT_SIE_IB
},
2486 { KVM_S390_VM_CPU_FEAT_CEI
, S390_FEAT_SIE_CEI
},
2487 { KVM_S390_VM_CPU_FEAT_IBS
, S390_FEAT_SIE_IBS
},
2488 { KVM_S390_VM_CPU_FEAT_SKEY
, S390_FEAT_SIE_SKEY
},
2489 { KVM_S390_VM_CPU_FEAT_CMMA
, S390_FEAT_SIE_CMMA
},
2490 { KVM_S390_VM_CPU_FEAT_PFMFI
, S390_FEAT_SIE_PFMFI
},
2491 { KVM_S390_VM_CPU_FEAT_SIGPIF
, S390_FEAT_SIE_SIGPIF
},
2494 static int query_cpu_feat(S390FeatBitmap features
)
2496 struct kvm_s390_vm_cpu_feat prop
;
2497 struct kvm_device_attr attr
= {
2498 .group
= KVM_S390_VM_CPU_MODEL
,
2499 .attr
= KVM_S390_VM_CPU_MACHINE_FEAT
,
2500 .addr
= (uint64_t) &prop
,
2505 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2510 for (i
= 0; i
< ARRAY_SIZE(kvm_to_feat
); i
++) {
2511 if (test_bit_inv(kvm_to_feat
[i
][0], (unsigned long *)prop
.feat
)) {
2512 set_bit(kvm_to_feat
[i
][1], features
);
2518 static int configure_cpu_feat(const S390FeatBitmap features
)
2520 struct kvm_s390_vm_cpu_feat prop
= {};
2521 struct kvm_device_attr attr
= {
2522 .group
= KVM_S390_VM_CPU_MODEL
,
2523 .attr
= KVM_S390_VM_CPU_PROCESSOR_FEAT
,
2524 .addr
= (uint64_t) &prop
,
2528 for (i
= 0; i
< ARRAY_SIZE(kvm_to_feat
); i
++) {
2529 if (test_bit(kvm_to_feat
[i
][1], features
)) {
2530 set_bit_inv(kvm_to_feat
[i
][0], (unsigned long *)prop
.feat
);
2533 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2536 bool kvm_s390_cpu_models_supported(void)
2538 if (!cpu_model_allowed()) {
2539 /* compatibility machines interfere with the cpu model */
2542 return kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2543 KVM_S390_VM_CPU_MACHINE
) &&
2544 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2545 KVM_S390_VM_CPU_PROCESSOR
) &&
2546 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2547 KVM_S390_VM_CPU_MACHINE_FEAT
) &&
2548 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2549 KVM_S390_VM_CPU_PROCESSOR_FEAT
) &&
2550 kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CPU_MODEL
,
2551 KVM_S390_VM_CPU_MACHINE_SUBFUNC
);
2554 void kvm_s390_get_host_cpu_model(S390CPUModel
*model
, Error
**errp
)
2556 struct kvm_s390_vm_cpu_machine prop
= {};
2557 struct kvm_device_attr attr
= {
2558 .group
= KVM_S390_VM_CPU_MODEL
,
2559 .attr
= KVM_S390_VM_CPU_MACHINE
,
2560 .addr
= (uint64_t) &prop
,
2562 uint16_t unblocked_ibc
= 0, cpu_type
= 0;
2565 memset(model
, 0, sizeof(*model
));
2567 if (!kvm_s390_cpu_models_supported()) {
2568 error_setg(errp
, "KVM doesn't support CPU models");
2572 /* query the basic cpu model properties */
2573 rc
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
2575 error_setg(errp
, "KVM: Error querying host CPU model: %d", rc
);
2579 cpu_type
= cpuid_type(prop
.cpuid
);
2580 if (has_ibc(prop
.ibc
)) {
2581 model
->lowest_ibc
= lowest_ibc(prop
.ibc
);
2582 unblocked_ibc
= unblocked_ibc(prop
.ibc
);
2584 model
->cpu_id
= cpuid_id(prop
.cpuid
);
2585 model
->cpu_ver
= 0xff;
2587 /* get supported cpu features indicated via STFL(E) */
2588 s390_add_from_feat_block(model
->features
, S390_FEAT_TYPE_STFL
,
2589 (uint8_t *) prop
.fac_mask
);
2590 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2591 if (test_bit(S390_FEAT_STFLE
, model
->features
)) {
2592 set_bit(S390_FEAT_DAT_ENH_2
, model
->features
);
2594 /* get supported cpu features indicated e.g. via SCLP */
2595 rc
= query_cpu_feat(model
->features
);
2597 error_setg(errp
, "KVM: Error querying CPU features: %d", rc
);
2600 /* get supported cpu subfunctions indicated via query / test bit */
2601 rc
= query_cpu_subfunc(model
->features
);
2603 error_setg(errp
, "KVM: Error querying CPU subfunctions: %d", rc
);
2607 /* with cpu model support, CMM is only indicated if really available */
2608 if (kvm_s390_cmma_available()) {
2609 set_bit(S390_FEAT_CMM
, model
->features
);
2612 if (s390_known_cpu_type(cpu_type
)) {
2613 /* we want the exact model, even if some features are missing */
2614 model
->def
= s390_find_cpu_def(cpu_type
, ibc_gen(unblocked_ibc
),
2615 ibc_ec_ga(unblocked_ibc
), NULL
);
2617 /* model unknown, e.g. too new - search using features */
2618 model
->def
= s390_find_cpu_def(0, ibc_gen(unblocked_ibc
),
2619 ibc_ec_ga(unblocked_ibc
),
2623 error_setg(errp
, "KVM: host CPU model could not be identified");
2626 /* strip of features that are not part of the maximum model */
2627 bitmap_and(model
->features
, model
->features
, model
->def
->full_feat
,
2631 void kvm_s390_apply_cpu_model(const S390CPUModel
*model
, Error
**errp
)
2633 struct kvm_s390_vm_cpu_processor prop
= {
2636 struct kvm_device_attr attr
= {
2637 .group
= KVM_S390_VM_CPU_MODEL
,
2638 .attr
= KVM_S390_VM_CPU_PROCESSOR
,
2639 .addr
= (uint64_t) &prop
,
2644 /* compatibility handling if cpu models are disabled */
2645 if (kvm_s390_cmma_available() && !mem_path
) {
2646 kvm_s390_enable_cmma();
2650 if (!kvm_s390_cpu_models_supported()) {
2651 error_setg(errp
, "KVM doesn't support CPU models");
2654 prop
.cpuid
= s390_cpuid_from_cpu_model(model
);
2655 prop
.ibc
= s390_ibc_from_cpu_model(model
);
2656 /* configure cpu features indicated via STFL(e) */
2657 s390_fill_feat_block(model
->features
, S390_FEAT_TYPE_STFL
,
2658 (uint8_t *) prop
.fac_list
);
2659 rc
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
2661 error_setg(errp
, "KVM: Error configuring the CPU model: %d", rc
);
2664 /* configure cpu features indicated e.g. via SCLP */
2665 rc
= configure_cpu_feat(model
->features
);
2667 error_setg(errp
, "KVM: Error configuring CPU features: %d", rc
);
2670 /* configure cpu subfunctions indicated via query / test bit */
2671 rc
= configure_cpu_subfunc(model
->features
);
2673 error_setg(errp
, "KVM: Error configuring CPU subfunctions: %d", rc
);
2676 /* enable CMM via CMMA - disable on hugetlbfs */
2677 if (test_bit(S390_FEAT_CMM
, model
->features
)) {
2679 error_report("Warning: CMM will not be enabled because it is not "
2680 "compatible to hugetlbfs.");
2682 kvm_s390_enable_cmma();