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 <sys/types.h>
25 #include <sys/ioctl.h>
28 #include <linux/kvm.h>
29 #include <asm/ptrace.h>
31 #include "qemu-common.h"
32 #include "qemu/timer.h"
33 #include "sysemu/sysemu.h"
34 #include "sysemu/kvm.h"
37 #include "sysemu/device_tree.h"
38 #include "qapi/qmp/qjson.h"
39 #include "monitor/monitor.h"
40 #include "exec/gdbstub.h"
41 #include "exec/address-spaces.h"
43 #include "qapi-event.h"
44 #include "hw/s390x/s390-pci-inst.h"
45 #include "hw/s390x/s390-pci-bus.h"
46 #include "hw/s390x/ipl.h"
47 #include "hw/s390x/ebcdic.h"
49 /* #define DEBUG_KVM */
52 #define DPRINTF(fmt, ...) \
53 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
55 #define DPRINTF(fmt, ...) \
59 #define kvm_vm_check_mem_attr(s, attr) \
60 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
62 #define IPA0_DIAG 0x8300
63 #define IPA0_SIGP 0xae00
64 #define IPA0_B2 0xb200
65 #define IPA0_B9 0xb900
66 #define IPA0_EB 0xeb00
67 #define IPA0_E3 0xe300
69 #define PRIV_B2_SCLP_CALL 0x20
70 #define PRIV_B2_CSCH 0x30
71 #define PRIV_B2_HSCH 0x31
72 #define PRIV_B2_MSCH 0x32
73 #define PRIV_B2_SSCH 0x33
74 #define PRIV_B2_STSCH 0x34
75 #define PRIV_B2_TSCH 0x35
76 #define PRIV_B2_TPI 0x36
77 #define PRIV_B2_SAL 0x37
78 #define PRIV_B2_RSCH 0x38
79 #define PRIV_B2_STCRW 0x39
80 #define PRIV_B2_STCPS 0x3a
81 #define PRIV_B2_RCHP 0x3b
82 #define PRIV_B2_SCHM 0x3c
83 #define PRIV_B2_CHSC 0x5f
84 #define PRIV_B2_SIGA 0x74
85 #define PRIV_B2_XSCH 0x76
87 #define PRIV_EB_SQBS 0x8a
88 #define PRIV_EB_PCISTB 0xd0
89 #define PRIV_EB_SIC 0xd1
91 #define PRIV_B9_EQBS 0x9c
92 #define PRIV_B9_CLP 0xa0
93 #define PRIV_B9_PCISTG 0xd0
94 #define PRIV_B9_PCILG 0xd2
95 #define PRIV_B9_RPCIT 0xd3
97 #define PRIV_E3_MPCIFC 0xd0
98 #define PRIV_E3_STPCIFC 0xd4
100 #define DIAG_IPL 0x308
101 #define DIAG_KVM_HYPERCALL 0x500
102 #define DIAG_KVM_BREAKPOINT 0x501
104 #define ICPT_INSTRUCTION 0x04
105 #define ICPT_PROGRAM 0x08
106 #define ICPT_EXT_INT 0x14
107 #define ICPT_WAITPSW 0x1c
108 #define ICPT_SOFT_INTERCEPT 0x24
109 #define ICPT_CPU_STOP 0x28
112 static CPUWatchpoint hw_watchpoint
;
114 * We don't use a list because this structure is also used to transmit the
115 * hardware breakpoints to the kernel.
117 static struct kvm_hw_breakpoint
*hw_breakpoints
;
118 static int nb_hw_breakpoints
;
120 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
124 static int cap_sync_regs
;
125 static int cap_async_pf
;
127 static void *legacy_s390_alloc(size_t size
, uint64_t *align
);
129 static int kvm_s390_query_mem_limit(KVMState
*s
, uint64_t *memory_limit
)
131 struct kvm_device_attr attr
= {
132 .group
= KVM_S390_VM_MEM_CTRL
,
133 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
134 .addr
= (uint64_t) memory_limit
,
137 return kvm_vm_ioctl(s
, KVM_GET_DEVICE_ATTR
, &attr
);
140 int kvm_s390_set_mem_limit(KVMState
*s
, uint64_t new_limit
, uint64_t *hw_limit
)
144 struct kvm_device_attr attr
= {
145 .group
= KVM_S390_VM_MEM_CTRL
,
146 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
147 .addr
= (uint64_t) &new_limit
,
150 if (!kvm_vm_check_mem_attr(s
, KVM_S390_VM_MEM_LIMIT_SIZE
)) {
154 rc
= kvm_s390_query_mem_limit(s
, hw_limit
);
157 } else if (*hw_limit
< new_limit
) {
161 return kvm_vm_ioctl(s
, KVM_SET_DEVICE_ATTR
, &attr
);
164 void kvm_s390_clear_cmma_callback(void *opaque
)
167 KVMState
*s
= opaque
;
168 struct kvm_device_attr attr
= {
169 .group
= KVM_S390_VM_MEM_CTRL
,
170 .attr
= KVM_S390_VM_MEM_CLR_CMMA
,
173 rc
= kvm_vm_ioctl(s
, KVM_SET_DEVICE_ATTR
, &attr
);
174 trace_kvm_clear_cmma(rc
);
177 static void kvm_s390_enable_cmma(KVMState
*s
)
180 struct kvm_device_attr attr
= {
181 .group
= KVM_S390_VM_MEM_CTRL
,
182 .attr
= KVM_S390_VM_MEM_ENABLE_CMMA
,
185 if (!kvm_vm_check_mem_attr(s
, KVM_S390_VM_MEM_ENABLE_CMMA
) ||
186 !kvm_vm_check_mem_attr(s
, KVM_S390_VM_MEM_CLR_CMMA
)) {
190 rc
= kvm_vm_ioctl(s
, KVM_SET_DEVICE_ATTR
, &attr
);
192 qemu_register_reset(kvm_s390_clear_cmma_callback
, s
);
194 trace_kvm_enable_cmma(rc
);
197 static void kvm_s390_set_attr(uint64_t attr
)
199 struct kvm_device_attr attribute
= {
200 .group
= KVM_S390_VM_CRYPTO
,
204 int ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attribute
);
207 error_report("Failed to set crypto device attribute %lu: %s",
208 attr
, strerror(-ret
));
212 static void kvm_s390_init_aes_kw(void)
214 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_AES_KW
;
216 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
218 attr
= KVM_S390_VM_CRYPTO_ENABLE_AES_KW
;
221 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
222 kvm_s390_set_attr(attr
);
226 static void kvm_s390_init_dea_kw(void)
228 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
;
230 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
232 attr
= KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
;
235 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
236 kvm_s390_set_attr(attr
);
240 static void kvm_s390_init_crypto(void)
242 kvm_s390_init_aes_kw();
243 kvm_s390_init_dea_kw();
246 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
248 cap_sync_regs
= kvm_check_extension(s
, KVM_CAP_SYNC_REGS
);
249 cap_async_pf
= kvm_check_extension(s
, KVM_CAP_ASYNC_PF
);
251 kvm_s390_enable_cmma(s
);
253 if (!kvm_check_extension(s
, KVM_CAP_S390_GMAP
)
254 || !kvm_check_extension(s
, KVM_CAP_S390_COW
)) {
255 phys_mem_set_alloc(legacy_s390_alloc
);
258 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_SIGP
, 0);
259 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_STSI
, 0);
264 unsigned long kvm_arch_vcpu_id(CPUState
*cpu
)
266 return cpu
->cpu_index
;
269 int kvm_arch_init_vcpu(CPUState
*cs
)
271 S390CPU
*cpu
= S390_CPU(cs
);
272 kvm_s390_set_cpu_state(cpu
, cpu
->env
.cpu_state
);
276 void kvm_s390_reset_vcpu(S390CPU
*cpu
)
278 CPUState
*cs
= CPU(cpu
);
280 /* The initial reset call is needed here to reset in-kernel
281 * vcpu data that we can't access directly from QEMU
282 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
283 * Before this ioctl cpu_synchronize_state() is called in common kvm
285 if (kvm_vcpu_ioctl(cs
, KVM_S390_INITIAL_RESET
, NULL
)) {
286 error_report("Initial CPU reset failed on CPU %i", cs
->cpu_index
);
289 kvm_s390_init_crypto();
292 static int can_sync_regs(CPUState
*cs
, int regs
)
294 return cap_sync_regs
&& (cs
->kvm_run
->kvm_valid_regs
& regs
) == regs
;
297 int kvm_arch_put_registers(CPUState
*cs
, int level
)
299 S390CPU
*cpu
= S390_CPU(cs
);
300 CPUS390XState
*env
= &cpu
->env
;
301 struct kvm_sregs sregs
;
302 struct kvm_regs regs
;
303 struct kvm_fpu fpu
= {};
307 /* always save the PSW and the GPRS*/
308 cs
->kvm_run
->psw_addr
= env
->psw
.addr
;
309 cs
->kvm_run
->psw_mask
= env
->psw
.mask
;
311 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
312 for (i
= 0; i
< 16; i
++) {
313 cs
->kvm_run
->s
.regs
.gprs
[i
] = env
->regs
[i
];
314 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_GPRS
;
317 for (i
= 0; i
< 16; i
++) {
318 regs
.gprs
[i
] = env
->regs
[i
];
320 r
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
327 for (i
= 0; i
< 16; i
++) {
328 fpu
.fprs
[i
] = env
->fregs
[i
].ll
;
332 r
= kvm_vcpu_ioctl(cs
, KVM_SET_FPU
, &fpu
);
337 /* Do we need to save more than that? */
338 if (level
== KVM_PUT_RUNTIME_STATE
) {
342 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
343 cs
->kvm_run
->s
.regs
.cputm
= env
->cputm
;
344 cs
->kvm_run
->s
.regs
.ckc
= env
->ckc
;
345 cs
->kvm_run
->s
.regs
.todpr
= env
->todpr
;
346 cs
->kvm_run
->s
.regs
.gbea
= env
->gbea
;
347 cs
->kvm_run
->s
.regs
.pp
= env
->pp
;
348 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ARCH0
;
351 * These ONE_REGS are not protected by a capability. As they are only
352 * necessary for migration we just trace a possible error, but don't
353 * return with an error return code.
355 kvm_set_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
356 kvm_set_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
357 kvm_set_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
358 kvm_set_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
359 kvm_set_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
362 /* pfault parameters */
363 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
364 cs
->kvm_run
->s
.regs
.pft
= env
->pfault_token
;
365 cs
->kvm_run
->s
.regs
.pfs
= env
->pfault_select
;
366 cs
->kvm_run
->s
.regs
.pfc
= env
->pfault_compare
;
367 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PFAULT
;
368 } else if (cap_async_pf
) {
369 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
373 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
377 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
383 /* access registers and control registers*/
384 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
385 for (i
= 0; i
< 16; i
++) {
386 cs
->kvm_run
->s
.regs
.acrs
[i
] = env
->aregs
[i
];
387 cs
->kvm_run
->s
.regs
.crs
[i
] = env
->cregs
[i
];
389 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ACRS
;
390 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_CRS
;
392 for (i
= 0; i
< 16; i
++) {
393 sregs
.acrs
[i
] = env
->aregs
[i
];
394 sregs
.crs
[i
] = env
->cregs
[i
];
396 r
= kvm_vcpu_ioctl(cs
, KVM_SET_SREGS
, &sregs
);
402 /* Finally the prefix */
403 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
404 cs
->kvm_run
->s
.regs
.prefix
= env
->psa
;
405 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PREFIX
;
407 /* prefix is only supported via sync regs */
412 int kvm_arch_get_registers(CPUState
*cs
)
414 S390CPU
*cpu
= S390_CPU(cs
);
415 CPUS390XState
*env
= &cpu
->env
;
416 struct kvm_sregs sregs
;
417 struct kvm_regs regs
;
422 env
->psw
.addr
= cs
->kvm_run
->psw_addr
;
423 env
->psw
.mask
= cs
->kvm_run
->psw_mask
;
426 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
427 for (i
= 0; i
< 16; i
++) {
428 env
->regs
[i
] = cs
->kvm_run
->s
.regs
.gprs
[i
];
431 r
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
435 for (i
= 0; i
< 16; i
++) {
436 env
->regs
[i
] = regs
.gprs
[i
];
440 /* The ACRS and CRS */
441 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
442 for (i
= 0; i
< 16; i
++) {
443 env
->aregs
[i
] = cs
->kvm_run
->s
.regs
.acrs
[i
];
444 env
->cregs
[i
] = cs
->kvm_run
->s
.regs
.crs
[i
];
447 r
= kvm_vcpu_ioctl(cs
, KVM_GET_SREGS
, &sregs
);
451 for (i
= 0; i
< 16; i
++) {
452 env
->aregs
[i
] = sregs
.acrs
[i
];
453 env
->cregs
[i
] = sregs
.crs
[i
];
458 r
= kvm_vcpu_ioctl(cs
, KVM_GET_FPU
, &fpu
);
462 for (i
= 0; i
< 16; i
++) {
463 env
->fregs
[i
].ll
= fpu
.fprs
[i
];
468 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
469 env
->psa
= cs
->kvm_run
->s
.regs
.prefix
;
472 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
473 env
->cputm
= cs
->kvm_run
->s
.regs
.cputm
;
474 env
->ckc
= cs
->kvm_run
->s
.regs
.ckc
;
475 env
->todpr
= cs
->kvm_run
->s
.regs
.todpr
;
476 env
->gbea
= cs
->kvm_run
->s
.regs
.gbea
;
477 env
->pp
= cs
->kvm_run
->s
.regs
.pp
;
480 * These ONE_REGS are not protected by a capability. As they are only
481 * necessary for migration we just trace a possible error, but don't
482 * return with an error return code.
484 kvm_get_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
485 kvm_get_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
486 kvm_get_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
487 kvm_get_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
488 kvm_get_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
491 /* pfault parameters */
492 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
493 env
->pfault_token
= cs
->kvm_run
->s
.regs
.pft
;
494 env
->pfault_select
= cs
->kvm_run
->s
.regs
.pfs
;
495 env
->pfault_compare
= cs
->kvm_run
->s
.regs
.pfc
;
496 } else if (cap_async_pf
) {
497 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
501 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
505 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
514 int kvm_s390_get_clock(uint8_t *tod_high
, uint64_t *tod_low
)
517 struct kvm_device_attr attr
= {
518 .group
= KVM_S390_VM_TOD
,
519 .attr
= KVM_S390_VM_TOD_LOW
,
520 .addr
= (uint64_t)tod_low
,
523 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
528 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
529 attr
.addr
= (uint64_t)tod_high
;
530 return kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
533 int kvm_s390_set_clock(uint8_t *tod_high
, uint64_t *tod_low
)
537 struct kvm_device_attr attr
= {
538 .group
= KVM_S390_VM_TOD
,
539 .attr
= KVM_S390_VM_TOD_LOW
,
540 .addr
= (uint64_t)tod_low
,
543 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
548 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
549 attr
.addr
= (uint64_t)tod_high
;
550 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
554 * Legacy layout for s390:
555 * Older S390 KVM requires the topmost vma of the RAM to be
556 * smaller than an system defined value, which is at least 256GB.
557 * Larger systems have larger values. We put the guest between
558 * the end of data segment (system break) and this value. We
559 * use 32GB as a base to have enough room for the system break
560 * to grow. We also have to use MAP parameters that avoid
561 * read-only mapping of guest pages.
563 static void *legacy_s390_alloc(size_t size
, uint64_t *align
)
567 mem
= mmap((void *) 0x800000000ULL
, size
,
568 PROT_EXEC
|PROT_READ
|PROT_WRITE
,
569 MAP_SHARED
| MAP_ANONYMOUS
| MAP_FIXED
, -1, 0);
570 return mem
== MAP_FAILED
? NULL
: mem
;
573 /* DIAG 501 is used for sw breakpoints */
574 static const uint8_t diag_501
[] = {0x83, 0x24, 0x05, 0x01};
576 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
579 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
580 sizeof(diag_501
), 0) ||
581 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)diag_501
,
582 sizeof(diag_501
), 1)) {
588 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
590 uint8_t t
[sizeof(diag_501
)];
592 if (cpu_memory_rw_debug(cs
, bp
->pc
, t
, sizeof(diag_501
), 0)) {
594 } else if (memcmp(t
, diag_501
, sizeof(diag_501
))) {
596 } else if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
597 sizeof(diag_501
), 1)) {
604 static struct kvm_hw_breakpoint
*find_hw_breakpoint(target_ulong addr
,
609 for (n
= 0; n
< nb_hw_breakpoints
; n
++) {
610 if (hw_breakpoints
[n
].addr
== addr
&& hw_breakpoints
[n
].type
== type
&&
611 (hw_breakpoints
[n
].len
== len
|| len
== -1)) {
612 return &hw_breakpoints
[n
];
619 static int insert_hw_breakpoint(target_ulong addr
, int len
, int type
)
623 if (find_hw_breakpoint(addr
, len
, type
)) {
627 size
= (nb_hw_breakpoints
+ 1) * sizeof(struct kvm_hw_breakpoint
);
629 if (!hw_breakpoints
) {
630 nb_hw_breakpoints
= 0;
631 hw_breakpoints
= (struct kvm_hw_breakpoint
*)g_try_malloc(size
);
634 (struct kvm_hw_breakpoint
*)g_try_realloc(hw_breakpoints
, size
);
637 if (!hw_breakpoints
) {
638 nb_hw_breakpoints
= 0;
642 hw_breakpoints
[nb_hw_breakpoints
].addr
= addr
;
643 hw_breakpoints
[nb_hw_breakpoints
].len
= len
;
644 hw_breakpoints
[nb_hw_breakpoints
].type
= type
;
651 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
652 target_ulong len
, int type
)
655 case GDB_BREAKPOINT_HW
:
658 case GDB_WATCHPOINT_WRITE
:
662 type
= KVM_HW_WP_WRITE
;
667 return insert_hw_breakpoint(addr
, len
, type
);
670 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
671 target_ulong len
, int type
)
674 struct kvm_hw_breakpoint
*bp
= find_hw_breakpoint(addr
, len
, type
);
681 if (nb_hw_breakpoints
> 0) {
683 * In order to trim the array, move the last element to the position to
684 * be removed - if necessary.
686 if (bp
!= &hw_breakpoints
[nb_hw_breakpoints
]) {
687 *bp
= hw_breakpoints
[nb_hw_breakpoints
];
689 size
= nb_hw_breakpoints
* sizeof(struct kvm_hw_breakpoint
);
691 (struct kvm_hw_breakpoint
*)g_realloc(hw_breakpoints
, size
);
693 g_free(hw_breakpoints
);
694 hw_breakpoints
= NULL
;
700 void kvm_arch_remove_all_hw_breakpoints(void)
702 nb_hw_breakpoints
= 0;
703 g_free(hw_breakpoints
);
704 hw_breakpoints
= NULL
;
707 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
711 if (nb_hw_breakpoints
> 0) {
712 dbg
->arch
.nr_hw_bp
= nb_hw_breakpoints
;
713 dbg
->arch
.hw_bp
= hw_breakpoints
;
715 for (i
= 0; i
< nb_hw_breakpoints
; ++i
) {
716 hw_breakpoints
[i
].phys_addr
= s390_cpu_get_phys_addr_debug(cpu
,
717 hw_breakpoints
[i
].addr
);
719 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
721 dbg
->arch
.nr_hw_bp
= 0;
722 dbg
->arch
.hw_bp
= NULL
;
726 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
730 void kvm_arch_post_run(CPUState
*cpu
, struct kvm_run
*run
)
734 int kvm_arch_process_async_events(CPUState
*cs
)
739 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq
*irq
,
740 struct kvm_s390_interrupt
*interrupt
)
744 interrupt
->type
= irq
->type
;
746 case KVM_S390_INT_VIRTIO
:
747 interrupt
->parm
= irq
->u
.ext
.ext_params
;
749 case KVM_S390_INT_PFAULT_INIT
:
750 case KVM_S390_INT_PFAULT_DONE
:
751 interrupt
->parm64
= irq
->u
.ext
.ext_params2
;
753 case KVM_S390_PROGRAM_INT
:
754 interrupt
->parm
= irq
->u
.pgm
.code
;
756 case KVM_S390_SIGP_SET_PREFIX
:
757 interrupt
->parm
= irq
->u
.prefix
.address
;
759 case KVM_S390_INT_SERVICE
:
760 interrupt
->parm
= irq
->u
.ext
.ext_params
;
763 interrupt
->parm
= irq
->u
.mchk
.cr14
;
764 interrupt
->parm64
= irq
->u
.mchk
.mcic
;
766 case KVM_S390_INT_EXTERNAL_CALL
:
767 interrupt
->parm
= irq
->u
.extcall
.code
;
769 case KVM_S390_INT_EMERGENCY
:
770 interrupt
->parm
= irq
->u
.emerg
.code
;
772 case KVM_S390_SIGP_STOP
:
773 case KVM_S390_RESTART
:
774 break; /* These types have no parameters */
775 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
776 interrupt
->parm
= irq
->u
.io
.subchannel_id
<< 16;
777 interrupt
->parm
|= irq
->u
.io
.subchannel_nr
;
778 interrupt
->parm64
= (uint64_t)irq
->u
.io
.io_int_parm
<< 32;
779 interrupt
->parm64
|= irq
->u
.io
.io_int_word
;
788 void kvm_s390_vcpu_interrupt(S390CPU
*cpu
, struct kvm_s390_irq
*irq
)
790 struct kvm_s390_interrupt kvmint
= {};
791 CPUState
*cs
= CPU(cpu
);
794 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
796 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
800 r
= kvm_vcpu_ioctl(cs
, KVM_S390_INTERRUPT
, &kvmint
);
802 fprintf(stderr
, "KVM failed to inject interrupt\n");
807 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq
*irq
)
809 struct kvm_s390_interrupt kvmint
= {};
812 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
814 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
818 r
= kvm_vm_ioctl(kvm_state
, KVM_S390_INTERRUPT
, &kvmint
);
820 fprintf(stderr
, "KVM failed to inject interrupt\n");
825 void kvm_s390_floating_interrupt(struct kvm_s390_irq
*irq
)
827 static bool use_flic
= true;
831 r
= kvm_s390_inject_flic(irq
);
839 __kvm_s390_floating_interrupt(irq
);
842 void kvm_s390_virtio_irq(int config_change
, uint64_t token
)
844 struct kvm_s390_irq irq
= {
845 .type
= KVM_S390_INT_VIRTIO
,
846 .u
.ext
.ext_params
= config_change
,
847 .u
.ext
.ext_params2
= token
,
850 kvm_s390_floating_interrupt(&irq
);
853 void kvm_s390_service_interrupt(uint32_t parm
)
855 struct kvm_s390_irq irq
= {
856 .type
= KVM_S390_INT_SERVICE
,
857 .u
.ext
.ext_params
= parm
,
860 kvm_s390_floating_interrupt(&irq
);
863 static void enter_pgmcheck(S390CPU
*cpu
, uint16_t code
)
865 struct kvm_s390_irq irq
= {
866 .type
= KVM_S390_PROGRAM_INT
,
870 kvm_s390_vcpu_interrupt(cpu
, &irq
);
873 void kvm_s390_access_exception(S390CPU
*cpu
, uint16_t code
, uint64_t te_code
)
875 struct kvm_s390_irq irq
= {
876 .type
= KVM_S390_PROGRAM_INT
,
878 .u
.pgm
.trans_exc_code
= te_code
,
879 .u
.pgm
.exc_access_id
= te_code
& 3,
882 kvm_s390_vcpu_interrupt(cpu
, &irq
);
885 static int kvm_sclp_service_call(S390CPU
*cpu
, struct kvm_run
*run
,
888 CPUS390XState
*env
= &cpu
->env
;
893 cpu_synchronize_state(CPU(cpu
));
894 sccb
= env
->regs
[ipbh0
& 0xf];
895 code
= env
->regs
[(ipbh0
& 0xf0) >> 4];
897 r
= sclp_service_call(env
, sccb
, code
);
899 enter_pgmcheck(cpu
, -r
);
907 static int handle_b2(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
909 CPUS390XState
*env
= &cpu
->env
;
911 uint16_t ipbh0
= (run
->s390_sieic
.ipb
& 0xffff0000) >> 16;
913 cpu_synchronize_state(CPU(cpu
));
917 ioinst_handle_xsch(cpu
, env
->regs
[1]);
920 ioinst_handle_csch(cpu
, env
->regs
[1]);
923 ioinst_handle_hsch(cpu
, env
->regs
[1]);
926 ioinst_handle_msch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
929 ioinst_handle_ssch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
932 ioinst_handle_stcrw(cpu
, run
->s390_sieic
.ipb
);
935 ioinst_handle_stsch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
938 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
939 fprintf(stderr
, "Spurious tsch intercept\n");
942 ioinst_handle_chsc(cpu
, run
->s390_sieic
.ipb
);
945 /* This should have been handled by kvm already. */
946 fprintf(stderr
, "Spurious tpi intercept\n");
949 ioinst_handle_schm(cpu
, env
->regs
[1], env
->regs
[2],
950 run
->s390_sieic
.ipb
);
953 ioinst_handle_rsch(cpu
, env
->regs
[1]);
956 ioinst_handle_rchp(cpu
, env
->regs
[1]);
959 /* We do not provide this instruction, it is suppressed. */
962 ioinst_handle_sal(cpu
, env
->regs
[1]);
965 /* Not provided, set CC = 3 for subchannel not operational */
968 case PRIV_B2_SCLP_CALL
:
969 rc
= kvm_sclp_service_call(cpu
, run
, ipbh0
);
973 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1
);
980 static uint64_t get_base_disp_rxy(S390CPU
*cpu
, struct kvm_run
*run
)
982 CPUS390XState
*env
= &cpu
->env
;
983 uint32_t x2
= (run
->s390_sieic
.ipa
& 0x000f);
984 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
985 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
986 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
988 if (disp2
& 0x80000) {
992 return (base2
? env
->regs
[base2
] : 0) +
993 (x2
? env
->regs
[x2
] : 0) + (long)(int)disp2
;
996 static uint64_t get_base_disp_rsy(S390CPU
*cpu
, struct kvm_run
*run
)
998 CPUS390XState
*env
= &cpu
->env
;
999 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1000 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1001 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1003 if (disp2
& 0x80000) {
1004 disp2
+= 0xfff00000;
1007 return (base2
? env
->regs
[base2
] : 0) + (long)(int)disp2
;
1010 static int kvm_clp_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1012 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1014 return clp_service_call(cpu
, r2
);
1017 static int kvm_pcilg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1019 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1020 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1022 return pcilg_service_call(cpu
, r1
, r2
);
1025 static int kvm_pcistg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1027 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1028 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1030 return pcistg_service_call(cpu
, r1
, r2
);
1033 static int kvm_stpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1035 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1038 cpu_synchronize_state(CPU(cpu
));
1039 fiba
= get_base_disp_rxy(cpu
, run
);
1041 return stpcifc_service_call(cpu
, r1
, fiba
);
1044 static int kvm_sic_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1050 static int kvm_rpcit_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1052 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1053 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1055 return rpcit_service_call(cpu
, r1
, r2
);
1058 static int kvm_pcistb_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1060 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1061 uint8_t r3
= run
->s390_sieic
.ipa
& 0x000f;
1064 cpu_synchronize_state(CPU(cpu
));
1065 gaddr
= get_base_disp_rsy(cpu
, run
);
1067 return pcistb_service_call(cpu
, r1
, r3
, gaddr
);
1070 static int kvm_mpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1072 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1075 cpu_synchronize_state(CPU(cpu
));
1076 fiba
= get_base_disp_rxy(cpu
, run
);
1078 return mpcifc_service_call(cpu
, r1
, fiba
);
1081 static int handle_b9(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1087 r
= kvm_clp_service_call(cpu
, run
);
1089 case PRIV_B9_PCISTG
:
1090 r
= kvm_pcistg_service_call(cpu
, run
);
1093 r
= kvm_pcilg_service_call(cpu
, run
);
1096 r
= kvm_rpcit_service_call(cpu
, run
);
1099 /* just inject exception */
1104 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1
);
1111 static int handle_eb(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1116 case PRIV_EB_PCISTB
:
1117 r
= kvm_pcistb_service_call(cpu
, run
);
1120 r
= kvm_sic_service_call(cpu
, run
);
1123 /* just inject exception */
1128 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl
);
1135 static int handle_e3(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1140 case PRIV_E3_MPCIFC
:
1141 r
= kvm_mpcifc_service_call(cpu
, run
);
1143 case PRIV_E3_STPCIFC
:
1144 r
= kvm_stpcifc_service_call(cpu
, run
);
1148 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl
);
1155 static int handle_hypercall(S390CPU
*cpu
, struct kvm_run
*run
)
1157 CPUS390XState
*env
= &cpu
->env
;
1160 cpu_synchronize_state(CPU(cpu
));
1161 ret
= s390_virtio_hypercall(env
);
1162 if (ret
== -EINVAL
) {
1163 enter_pgmcheck(cpu
, PGM_SPECIFICATION
);
1170 static void kvm_handle_diag_308(S390CPU
*cpu
, struct kvm_run
*run
)
1174 cpu_synchronize_state(CPU(cpu
));
1175 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1176 r3
= run
->s390_sieic
.ipa
& 0x000f;
1177 handle_diag_308(&cpu
->env
, r1
, r3
);
1180 static int handle_sw_breakpoint(S390CPU
*cpu
, struct kvm_run
*run
)
1182 CPUS390XState
*env
= &cpu
->env
;
1185 cpu_synchronize_state(CPU(cpu
));
1187 pc
= env
->psw
.addr
- 4;
1188 if (kvm_find_sw_breakpoint(CPU(cpu
), pc
)) {
1196 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1198 static int handle_diag(S390CPU
*cpu
, struct kvm_run
*run
, uint32_t ipb
)
1204 * For any diagnose call we support, bits 48-63 of the resulting
1205 * address specify the function code; the remainder is ignored.
1207 func_code
= decode_basedisp_rs(&cpu
->env
, ipb
) & DIAG_KVM_CODE_MASK
;
1208 switch (func_code
) {
1210 kvm_handle_diag_308(cpu
, run
);
1212 case DIAG_KVM_HYPERCALL
:
1213 r
= handle_hypercall(cpu
, run
);
1215 case DIAG_KVM_BREAKPOINT
:
1216 r
= handle_sw_breakpoint(cpu
, run
);
1219 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code
);
1220 enter_pgmcheck(cpu
, PGM_SPECIFICATION
);
1227 typedef struct SigpInfo
{
1231 uint64_t *status_reg
;
1234 static void set_sigp_status(SigpInfo
*si
, uint64_t status
)
1236 *si
->status_reg
&= 0xffffffff00000000ULL
;
1237 *si
->status_reg
|= status
;
1238 si
->cc
= SIGP_CC_STATUS_STORED
;
1241 static void sigp_start(void *arg
)
1245 if (s390_cpu_get_state(si
->cpu
) != CPU_STATE_STOPPED
) {
1246 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1250 s390_cpu_set_state(CPU_STATE_OPERATING
, si
->cpu
);
1251 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1254 static void sigp_stop(void *arg
)
1257 struct kvm_s390_irq irq
= {
1258 .type
= KVM_S390_SIGP_STOP
,
1261 if (s390_cpu_get_state(si
->cpu
) != CPU_STATE_OPERATING
) {
1262 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1266 /* disabled wait - sleeping in user space */
1267 if (CPU(si
->cpu
)->halted
) {
1268 s390_cpu_set_state(CPU_STATE_STOPPED
, si
->cpu
);
1270 /* execute the stop function */
1271 si
->cpu
->env
.sigp_order
= SIGP_STOP
;
1272 kvm_s390_vcpu_interrupt(si
->cpu
, &irq
);
1274 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1277 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1278 #define SAVE_AREA_SIZE 512
1279 static int kvm_s390_store_status(S390CPU
*cpu
, hwaddr addr
, bool store_arch
)
1281 static const uint8_t ar_id
= 1;
1282 uint64_t ckc
= cpu
->env
.ckc
>> 8;
1284 hwaddr len
= SAVE_AREA_SIZE
;
1286 mem
= cpu_physical_memory_map(addr
, &len
, 1);
1290 if (len
!= SAVE_AREA_SIZE
) {
1291 cpu_physical_memory_unmap(mem
, len
, 1, 0);
1296 cpu_physical_memory_write(offsetof(LowCore
, ar_access_id
), &ar_id
, 1);
1298 memcpy(mem
, &cpu
->env
.fregs
, 128);
1299 memcpy(mem
+ 128, &cpu
->env
.regs
, 128);
1300 memcpy(mem
+ 256, &cpu
->env
.psw
, 16);
1301 memcpy(mem
+ 280, &cpu
->env
.psa
, 4);
1302 memcpy(mem
+ 284, &cpu
->env
.fpc
, 4);
1303 memcpy(mem
+ 292, &cpu
->env
.todpr
, 4);
1304 memcpy(mem
+ 296, &cpu
->env
.cputm
, 8);
1305 memcpy(mem
+ 304, &ckc
, 8);
1306 memcpy(mem
+ 320, &cpu
->env
.aregs
, 64);
1307 memcpy(mem
+ 384, &cpu
->env
.cregs
, 128);
1309 cpu_physical_memory_unmap(mem
, len
, 1, len
);
1314 static void sigp_stop_and_store_status(void *arg
)
1317 struct kvm_s390_irq irq
= {
1318 .type
= KVM_S390_SIGP_STOP
,
1321 /* disabled wait - sleeping in user space */
1322 if (s390_cpu_get_state(si
->cpu
) == CPU_STATE_OPERATING
&&
1323 CPU(si
->cpu
)->halted
) {
1324 s390_cpu_set_state(CPU_STATE_STOPPED
, si
->cpu
);
1327 switch (s390_cpu_get_state(si
->cpu
)) {
1328 case CPU_STATE_OPERATING
:
1329 si
->cpu
->env
.sigp_order
= SIGP_STOP_STORE_STATUS
;
1330 kvm_s390_vcpu_interrupt(si
->cpu
, &irq
);
1331 /* store will be performed when handling the stop intercept */
1333 case CPU_STATE_STOPPED
:
1334 /* already stopped, just store the status */
1335 cpu_synchronize_state(CPU(si
->cpu
));
1336 kvm_s390_store_status(si
->cpu
, KVM_S390_STORE_STATUS_DEF_ADDR
, true);
1339 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1342 static void sigp_store_status_at_address(void *arg
)
1345 uint32_t address
= si
->param
& 0x7ffffe00u
;
1347 /* cpu has to be stopped */
1348 if (s390_cpu_get_state(si
->cpu
) != CPU_STATE_STOPPED
) {
1349 set_sigp_status(si
, SIGP_STAT_INCORRECT_STATE
);
1353 cpu_synchronize_state(CPU(si
->cpu
));
1355 if (kvm_s390_store_status(si
->cpu
, address
, false)) {
1356 set_sigp_status(si
, SIGP_STAT_INVALID_PARAMETER
);
1359 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1362 static void sigp_restart(void *arg
)
1365 struct kvm_s390_irq irq
= {
1366 .type
= KVM_S390_RESTART
,
1369 switch (s390_cpu_get_state(si
->cpu
)) {
1370 case CPU_STATE_STOPPED
:
1371 /* the restart irq has to be delivered prior to any other pending irq */
1372 cpu_synchronize_state(CPU(si
->cpu
));
1373 do_restart_interrupt(&si
->cpu
->env
);
1374 s390_cpu_set_state(CPU_STATE_OPERATING
, si
->cpu
);
1376 case CPU_STATE_OPERATING
:
1377 kvm_s390_vcpu_interrupt(si
->cpu
, &irq
);
1380 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1383 int kvm_s390_cpu_restart(S390CPU
*cpu
)
1389 run_on_cpu(CPU(cpu
), sigp_restart
, &si
);
1390 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu
->env
);
1394 static void sigp_initial_cpu_reset(void *arg
)
1397 CPUState
*cs
= CPU(si
->cpu
);
1398 S390CPUClass
*scc
= S390_CPU_GET_CLASS(si
->cpu
);
1400 cpu_synchronize_state(cs
);
1401 scc
->initial_cpu_reset(cs
);
1402 cpu_synchronize_post_reset(cs
);
1403 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1406 static void sigp_cpu_reset(void *arg
)
1409 CPUState
*cs
= CPU(si
->cpu
);
1410 S390CPUClass
*scc
= S390_CPU_GET_CLASS(si
->cpu
);
1412 cpu_synchronize_state(cs
);
1414 cpu_synchronize_post_reset(cs
);
1415 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1418 static void sigp_set_prefix(void *arg
)
1421 uint32_t addr
= si
->param
& 0x7fffe000u
;
1423 cpu_synchronize_state(CPU(si
->cpu
));
1425 if (!address_space_access_valid(&address_space_memory
, addr
,
1426 sizeof(struct LowCore
), false)) {
1427 set_sigp_status(si
, SIGP_STAT_INVALID_PARAMETER
);
1431 /* cpu has to be stopped */
1432 if (s390_cpu_get_state(si
->cpu
) != CPU_STATE_STOPPED
) {
1433 set_sigp_status(si
, SIGP_STAT_INCORRECT_STATE
);
1437 si
->cpu
->env
.psa
= addr
;
1438 cpu_synchronize_post_init(CPU(si
->cpu
));
1439 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1442 static int handle_sigp_single_dst(S390CPU
*dst_cpu
, uint8_t order
,
1443 uint64_t param
, uint64_t *status_reg
)
1448 .status_reg
= status_reg
,
1451 /* cpu available? */
1452 if (dst_cpu
== NULL
) {
1453 return SIGP_CC_NOT_OPERATIONAL
;
1456 /* only resets can break pending orders */
1457 if (dst_cpu
->env
.sigp_order
!= 0 &&
1458 order
!= SIGP_CPU_RESET
&&
1459 order
!= SIGP_INITIAL_CPU_RESET
) {
1460 return SIGP_CC_BUSY
;
1465 run_on_cpu(CPU(dst_cpu
), sigp_start
, &si
);
1468 run_on_cpu(CPU(dst_cpu
), sigp_stop
, &si
);
1471 run_on_cpu(CPU(dst_cpu
), sigp_restart
, &si
);
1473 case SIGP_STOP_STORE_STATUS
:
1474 run_on_cpu(CPU(dst_cpu
), sigp_stop_and_store_status
, &si
);
1476 case SIGP_STORE_STATUS_ADDR
:
1477 run_on_cpu(CPU(dst_cpu
), sigp_store_status_at_address
, &si
);
1479 case SIGP_SET_PREFIX
:
1480 run_on_cpu(CPU(dst_cpu
), sigp_set_prefix
, &si
);
1482 case SIGP_INITIAL_CPU_RESET
:
1483 run_on_cpu(CPU(dst_cpu
), sigp_initial_cpu_reset
, &si
);
1485 case SIGP_CPU_RESET
:
1486 run_on_cpu(CPU(dst_cpu
), sigp_cpu_reset
, &si
);
1489 DPRINTF("KVM: unknown SIGP: 0x%x\n", order
);
1490 set_sigp_status(&si
, SIGP_STAT_INVALID_ORDER
);
1496 static int sigp_set_architecture(S390CPU
*cpu
, uint32_t param
,
1497 uint64_t *status_reg
)
1502 /* due to the BQL, we are the only active cpu */
1503 CPU_FOREACH(cur_cs
) {
1504 cur_cpu
= S390_CPU(cur_cs
);
1505 if (cur_cpu
->env
.sigp_order
!= 0) {
1506 return SIGP_CC_BUSY
;
1508 cpu_synchronize_state(cur_cs
);
1509 /* all but the current one have to be stopped */
1510 if (cur_cpu
!= cpu
&&
1511 s390_cpu_get_state(cur_cpu
) != CPU_STATE_STOPPED
) {
1512 *status_reg
&= 0xffffffff00000000ULL
;
1513 *status_reg
|= SIGP_STAT_INCORRECT_STATE
;
1514 return SIGP_CC_STATUS_STORED
;
1518 switch (param
& 0xff) {
1519 case SIGP_MODE_ESA_S390
:
1521 return SIGP_CC_NOT_OPERATIONAL
;
1522 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW
:
1523 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW
:
1524 CPU_FOREACH(cur_cs
) {
1525 cur_cpu
= S390_CPU(cur_cs
);
1526 cur_cpu
->env
.pfault_token
= -1UL;
1530 *status_reg
&= 0xffffffff00000000ULL
;
1531 *status_reg
|= SIGP_STAT_INVALID_PARAMETER
;
1532 return SIGP_CC_STATUS_STORED
;
1535 return SIGP_CC_ORDER_CODE_ACCEPTED
;
1538 #define SIGP_ORDER_MASK 0x000000ff
1540 static int handle_sigp(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1542 CPUS390XState
*env
= &cpu
->env
;
1543 const uint8_t r1
= ipa1
>> 4;
1544 const uint8_t r3
= ipa1
& 0x0f;
1547 uint64_t *status_reg
;
1549 S390CPU
*dst_cpu
= NULL
;
1551 cpu_synchronize_state(CPU(cpu
));
1553 /* get order code */
1554 order
= decode_basedisp_rs(env
, run
->s390_sieic
.ipb
) & SIGP_ORDER_MASK
;
1555 status_reg
= &env
->regs
[r1
];
1556 param
= (r1
% 2) ? env
->regs
[r1
] : env
->regs
[r1
+ 1];
1560 ret
= sigp_set_architecture(cpu
, param
, status_reg
);
1563 /* all other sigp orders target a single vcpu */
1564 dst_cpu
= s390_cpu_addr2state(env
->regs
[r3
]);
1565 ret
= handle_sigp_single_dst(dst_cpu
, order
, param
, status_reg
);
1568 trace_kvm_sigp_finished(order
, CPU(cpu
)->cpu_index
,
1569 dst_cpu
? CPU(dst_cpu
)->cpu_index
: -1, ret
);
1579 static int handle_instruction(S390CPU
*cpu
, struct kvm_run
*run
)
1581 unsigned int ipa0
= (run
->s390_sieic
.ipa
& 0xff00);
1582 uint8_t ipa1
= run
->s390_sieic
.ipa
& 0x00ff;
1585 DPRINTF("handle_instruction 0x%x 0x%x\n",
1586 run
->s390_sieic
.ipa
, run
->s390_sieic
.ipb
);
1589 r
= handle_b2(cpu
, run
, ipa1
);
1592 r
= handle_b9(cpu
, run
, ipa1
);
1595 r
= handle_eb(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1598 r
= handle_e3(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1601 r
= handle_diag(cpu
, run
, run
->s390_sieic
.ipb
);
1604 r
= handle_sigp(cpu
, run
, ipa1
);
1610 enter_pgmcheck(cpu
, 0x0001);
1616 static bool is_special_wait_psw(CPUState
*cs
)
1618 /* signal quiesce */
1619 return cs
->kvm_run
->psw_addr
== 0xfffUL
;
1622 static void guest_panicked(void)
1624 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE
,
1626 vm_stop(RUN_STATE_GUEST_PANICKED
);
1629 static void unmanageable_intercept(S390CPU
*cpu
, const char *str
, int pswoffset
)
1631 CPUState
*cs
= CPU(cpu
);
1633 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1634 str
, cs
->cpu_index
, ldq_phys(cs
->as
, cpu
->env
.psa
+ pswoffset
),
1635 ldq_phys(cs
->as
, cpu
->env
.psa
+ pswoffset
+ 8));
1640 static int handle_intercept(S390CPU
*cpu
)
1642 CPUState
*cs
= CPU(cpu
);
1643 struct kvm_run
*run
= cs
->kvm_run
;
1644 int icpt_code
= run
->s390_sieic
.icptcode
;
1647 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code
,
1648 (long)cs
->kvm_run
->psw_addr
);
1649 switch (icpt_code
) {
1650 case ICPT_INSTRUCTION
:
1651 r
= handle_instruction(cpu
, run
);
1654 unmanageable_intercept(cpu
, "program interrupt",
1655 offsetof(LowCore
, program_new_psw
));
1659 unmanageable_intercept(cpu
, "external interrupt",
1660 offsetof(LowCore
, external_new_psw
));
1664 /* disabled wait, since enabled wait is handled in kernel */
1665 cpu_synchronize_state(cs
);
1666 if (s390_cpu_halt(cpu
) == 0) {
1667 if (is_special_wait_psw(cs
)) {
1668 qemu_system_shutdown_request();
1676 if (s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
) == 0) {
1677 qemu_system_shutdown_request();
1679 if (cpu
->env
.sigp_order
== SIGP_STOP_STORE_STATUS
) {
1680 kvm_s390_store_status(cpu
, KVM_S390_STORE_STATUS_DEF_ADDR
,
1683 cpu
->env
.sigp_order
= 0;
1686 case ICPT_SOFT_INTERCEPT
:
1687 fprintf(stderr
, "KVM unimplemented icpt SOFT\n");
1691 fprintf(stderr
, "KVM unimplemented icpt IO\n");
1695 fprintf(stderr
, "Unknown intercept code: %d\n", icpt_code
);
1703 static int handle_tsch(S390CPU
*cpu
)
1705 CPUState
*cs
= CPU(cpu
);
1706 struct kvm_run
*run
= cs
->kvm_run
;
1709 cpu_synchronize_state(cs
);
1711 ret
= ioinst_handle_tsch(cpu
, cpu
->env
.regs
[1], run
->s390_tsch
.ipb
);
1715 * If an I/O interrupt had been dequeued, we have to reinject it.
1717 if (run
->s390_tsch
.dequeued
) {
1718 kvm_s390_io_interrupt(run
->s390_tsch
.subchannel_id
,
1719 run
->s390_tsch
.subchannel_nr
,
1720 run
->s390_tsch
.io_int_parm
,
1721 run
->s390_tsch
.io_int_word
);
1728 static void insert_stsi_3_2_2(S390CPU
*cpu
, __u64 addr
)
1730 struct sysib_322 sysib
;
1733 if (s390_cpu_virt_mem_read(cpu
, addr
, &sysib
, sizeof(sysib
))) {
1736 /* Shift the stack of Extended Names to prepare for our own data */
1737 memmove(&sysib
.ext_names
[1], &sysib
.ext_names
[0],
1738 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- 1));
1739 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1740 * assumed it's not capable of managing Extended Names for lower levels.
1742 for (del
= 1; del
< sysib
.count
; del
++) {
1743 if (!sysib
.vm
[del
].ext_name_encoding
|| !sysib
.ext_names
[del
][0]) {
1747 if (del
< sysib
.count
) {
1748 memset(sysib
.ext_names
[del
], 0,
1749 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- del
));
1751 /* Insert short machine name in EBCDIC, padded with blanks */
1753 memset(sysib
.vm
[0].name
, 0x40, sizeof(sysib
.vm
[0].name
));
1754 ebcdic_put(sysib
.vm
[0].name
, qemu_name
, MIN(sizeof(sysib
.vm
[0].name
),
1755 strlen(qemu_name
)));
1757 sysib
.vm
[0].ext_name_encoding
= 2; /* 2 = UTF-8 */
1758 memset(sysib
.ext_names
[0], 0, sizeof(sysib
.ext_names
[0]));
1759 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1760 * considered by s390 as not capable of providing any Extended Name.
1761 * Therefore if no name was specified on qemu invocation, we go with the
1762 * same "KVMguest" default, which KVM has filled into short name field.
1765 strncpy((char *)sysib
.ext_names
[0], qemu_name
,
1766 sizeof(sysib
.ext_names
[0]));
1768 strcpy((char *)sysib
.ext_names
[0], "KVMguest");
1771 memcpy(sysib
.vm
[0].uuid
, qemu_uuid
, sizeof(sysib
.vm
[0].uuid
));
1773 s390_cpu_virt_mem_write(cpu
, addr
, &sysib
, sizeof(sysib
));
1776 static int handle_stsi(S390CPU
*cpu
)
1778 CPUState
*cs
= CPU(cpu
);
1779 struct kvm_run
*run
= cs
->kvm_run
;
1781 switch (run
->s390_stsi
.fc
) {
1783 if (run
->s390_stsi
.sel1
!= 2 || run
->s390_stsi
.sel2
!= 2) {
1786 /* Only sysib 3.2.2 needs post-handling for now. */
1787 insert_stsi_3_2_2(cpu
, run
->s390_stsi
.addr
);
1794 static int kvm_arch_handle_debug_exit(S390CPU
*cpu
)
1796 CPUState
*cs
= CPU(cpu
);
1797 struct kvm_run
*run
= cs
->kvm_run
;
1800 struct kvm_debug_exit_arch
*arch_info
= &run
->debug
.arch
;
1802 switch (arch_info
->type
) {
1803 case KVM_HW_WP_WRITE
:
1804 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1805 cs
->watchpoint_hit
= &hw_watchpoint
;
1806 hw_watchpoint
.vaddr
= arch_info
->addr
;
1807 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1812 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1816 case KVM_SINGLESTEP
:
1817 if (cs
->singlestep_enabled
) {
1828 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
1830 S390CPU
*cpu
= S390_CPU(cs
);
1833 switch (run
->exit_reason
) {
1834 case KVM_EXIT_S390_SIEIC
:
1835 ret
= handle_intercept(cpu
);
1837 case KVM_EXIT_S390_RESET
:
1838 s390_reipl_request();
1840 case KVM_EXIT_S390_TSCH
:
1841 ret
= handle_tsch(cpu
);
1843 case KVM_EXIT_S390_STSI
:
1844 ret
= handle_stsi(cpu
);
1846 case KVM_EXIT_DEBUG
:
1847 ret
= kvm_arch_handle_debug_exit(cpu
);
1850 fprintf(stderr
, "Unknown KVM exit: %d\n", run
->exit_reason
);
1855 ret
= EXCP_INTERRUPT
;
1860 bool kvm_arch_stop_on_emulation_error(CPUState
*cpu
)
1865 int kvm_arch_on_sigbus_vcpu(CPUState
*cpu
, int code
, void *addr
)
1870 int kvm_arch_on_sigbus(int code
, void *addr
)
1875 void kvm_s390_io_interrupt(uint16_t subchannel_id
,
1876 uint16_t subchannel_nr
, uint32_t io_int_parm
,
1877 uint32_t io_int_word
)
1879 struct kvm_s390_irq irq
= {
1880 .u
.io
.subchannel_id
= subchannel_id
,
1881 .u
.io
.subchannel_nr
= subchannel_nr
,
1882 .u
.io
.io_int_parm
= io_int_parm
,
1883 .u
.io
.io_int_word
= io_int_word
,
1886 if (io_int_word
& IO_INT_WORD_AI
) {
1887 irq
.type
= KVM_S390_INT_IO(1, 0, 0, 0);
1889 irq
.type
= ((subchannel_id
& 0xff00) << 24) |
1890 ((subchannel_id
& 0x00060) << 22) | (subchannel_nr
<< 16);
1892 kvm_s390_floating_interrupt(&irq
);
1895 void kvm_s390_crw_mchk(void)
1897 struct kvm_s390_irq irq
= {
1898 .type
= KVM_S390_MCHK
,
1899 .u
.mchk
.cr14
= 1 << 28,
1900 .u
.mchk
.mcic
= 0x00400f1d40330000ULL
,
1902 kvm_s390_floating_interrupt(&irq
);
1905 void kvm_s390_enable_css_support(S390CPU
*cpu
)
1909 /* Activate host kernel channel subsystem support. */
1910 r
= kvm_vcpu_enable_cap(CPU(cpu
), KVM_CAP_S390_CSS_SUPPORT
, 0);
1914 void kvm_arch_init_irq_routing(KVMState
*s
)
1917 * Note that while irqchip capabilities generally imply that cpustates
1918 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1919 * have to override the common code kvm_halt_in_kernel_allowed setting.
1921 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
1922 kvm_gsi_routing_allowed
= true;
1923 kvm_halt_in_kernel_allowed
= false;
1927 int kvm_s390_assign_subch_ioeventfd(EventNotifier
*notifier
, uint32_t sch
,
1928 int vq
, bool assign
)
1930 struct kvm_ioeventfd kick
= {
1931 .flags
= KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY
|
1932 KVM_IOEVENTFD_FLAG_DATAMATCH
,
1933 .fd
= event_notifier_get_fd(notifier
),
1938 if (!kvm_check_extension(kvm_state
, KVM_CAP_IOEVENTFD
)) {
1942 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1944 return kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
1947 int kvm_s390_get_memslot_count(KVMState
*s
)
1949 return kvm_check_extension(s
, KVM_CAP_NR_MEMSLOTS
);
1952 int kvm_s390_set_cpu_state(S390CPU
*cpu
, uint8_t cpu_state
)
1954 struct kvm_mp_state mp_state
= {};
1957 /* the kvm part might not have been initialized yet */
1958 if (CPU(cpu
)->kvm_state
== NULL
) {
1962 switch (cpu_state
) {
1963 case CPU_STATE_STOPPED
:
1964 mp_state
.mp_state
= KVM_MP_STATE_STOPPED
;
1966 case CPU_STATE_CHECK_STOP
:
1967 mp_state
.mp_state
= KVM_MP_STATE_CHECK_STOP
;
1969 case CPU_STATE_OPERATING
:
1970 mp_state
.mp_state
= KVM_MP_STATE_OPERATING
;
1972 case CPU_STATE_LOAD
:
1973 mp_state
.mp_state
= KVM_MP_STATE_LOAD
;
1976 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1981 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
1983 trace_kvm_failed_cpu_state_set(CPU(cpu
)->cpu_index
, cpu_state
,
1990 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
1991 uint64_t address
, uint32_t data
)
1993 S390PCIBusDevice
*pbdev
;
1994 uint32_t fid
= data
>> ZPCI_MSI_VEC_BITS
;
1995 uint32_t vec
= data
& ZPCI_MSI_VEC_MASK
;
1997 pbdev
= s390_pci_find_dev_by_fid(fid
);
1999 DPRINTF("add_msi_route no dev\n");
2003 pbdev
->routes
.adapter
.ind_offset
= vec
;
2005 route
->type
= KVM_IRQ_ROUTING_S390_ADAPTER
;
2007 route
->u
.adapter
.summary_addr
= pbdev
->routes
.adapter
.summary_addr
;
2008 route
->u
.adapter
.ind_addr
= pbdev
->routes
.adapter
.ind_addr
;
2009 route
->u
.adapter
.summary_offset
= pbdev
->routes
.adapter
.summary_offset
;
2010 route
->u
.adapter
.ind_offset
= pbdev
->routes
.adapter
.ind_offset
;
2011 route
->u
.adapter
.adapter_id
= pbdev
->routes
.adapter
.adapter_id
;