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"
48 #include "exec/memattrs.h"
50 /* #define DEBUG_KVM */
53 #define DPRINTF(fmt, ...) \
54 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
56 #define DPRINTF(fmt, ...) \
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_IPL 0x308
102 #define DIAG_KVM_HYPERCALL 0x500
103 #define DIAG_KVM_BREAKPOINT 0x501
105 #define ICPT_INSTRUCTION 0x04
106 #define ICPT_PROGRAM 0x08
107 #define ICPT_EXT_INT 0x14
108 #define ICPT_WAITPSW 0x1c
109 #define ICPT_SOFT_INTERCEPT 0x24
110 #define ICPT_CPU_STOP 0x28
113 static CPUWatchpoint hw_watchpoint
;
115 * We don't use a list because this structure is also used to transmit the
116 * hardware breakpoints to the kernel.
118 static struct kvm_hw_breakpoint
*hw_breakpoints
;
119 static int nb_hw_breakpoints
;
121 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
125 static int cap_sync_regs
;
126 static int cap_async_pf
;
127 static int cap_mem_op
;
129 static void *legacy_s390_alloc(size_t size
, uint64_t *align
);
131 static int kvm_s390_query_mem_limit(KVMState
*s
, uint64_t *memory_limit
)
133 struct kvm_device_attr attr
= {
134 .group
= KVM_S390_VM_MEM_CTRL
,
135 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
136 .addr
= (uint64_t) memory_limit
,
139 return kvm_vm_ioctl(s
, KVM_GET_DEVICE_ATTR
, &attr
);
142 int kvm_s390_set_mem_limit(KVMState
*s
, uint64_t new_limit
, uint64_t *hw_limit
)
146 struct kvm_device_attr attr
= {
147 .group
= KVM_S390_VM_MEM_CTRL
,
148 .attr
= KVM_S390_VM_MEM_LIMIT_SIZE
,
149 .addr
= (uint64_t) &new_limit
,
152 if (!kvm_vm_check_mem_attr(s
, KVM_S390_VM_MEM_LIMIT_SIZE
)) {
156 rc
= kvm_s390_query_mem_limit(s
, hw_limit
);
159 } else if (*hw_limit
< new_limit
) {
163 return kvm_vm_ioctl(s
, KVM_SET_DEVICE_ATTR
, &attr
);
166 void kvm_s390_clear_cmma_callback(void *opaque
)
169 KVMState
*s
= opaque
;
170 struct kvm_device_attr attr
= {
171 .group
= KVM_S390_VM_MEM_CTRL
,
172 .attr
= KVM_S390_VM_MEM_CLR_CMMA
,
175 rc
= kvm_vm_ioctl(s
, KVM_SET_DEVICE_ATTR
, &attr
);
176 trace_kvm_clear_cmma(rc
);
179 static void kvm_s390_enable_cmma(KVMState
*s
)
182 struct kvm_device_attr attr
= {
183 .group
= KVM_S390_VM_MEM_CTRL
,
184 .attr
= KVM_S390_VM_MEM_ENABLE_CMMA
,
187 if (!kvm_vm_check_mem_attr(s
, KVM_S390_VM_MEM_ENABLE_CMMA
) ||
188 !kvm_vm_check_mem_attr(s
, KVM_S390_VM_MEM_CLR_CMMA
)) {
192 rc
= kvm_vm_ioctl(s
, KVM_SET_DEVICE_ATTR
, &attr
);
194 qemu_register_reset(kvm_s390_clear_cmma_callback
, s
);
196 trace_kvm_enable_cmma(rc
);
199 static void kvm_s390_set_attr(uint64_t attr
)
201 struct kvm_device_attr attribute
= {
202 .group
= KVM_S390_VM_CRYPTO
,
206 int ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attribute
);
209 error_report("Failed to set crypto device attribute %lu: %s",
210 attr
, strerror(-ret
));
214 static void kvm_s390_init_aes_kw(void)
216 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_AES_KW
;
218 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
220 attr
= KVM_S390_VM_CRYPTO_ENABLE_AES_KW
;
223 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
224 kvm_s390_set_attr(attr
);
228 static void kvm_s390_init_dea_kw(void)
230 uint64_t attr
= KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
;
232 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
234 attr
= KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
;
237 if (kvm_vm_check_attr(kvm_state
, KVM_S390_VM_CRYPTO
, attr
)) {
238 kvm_s390_set_attr(attr
);
242 static void kvm_s390_init_crypto(void)
244 kvm_s390_init_aes_kw();
245 kvm_s390_init_dea_kw();
248 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
250 cap_sync_regs
= kvm_check_extension(s
, KVM_CAP_SYNC_REGS
);
251 cap_async_pf
= kvm_check_extension(s
, KVM_CAP_ASYNC_PF
);
252 cap_mem_op
= kvm_check_extension(s
, KVM_CAP_S390_MEM_OP
);
254 kvm_s390_enable_cmma(s
);
256 if (!kvm_check_extension(s
, KVM_CAP_S390_GMAP
)
257 || !kvm_check_extension(s
, KVM_CAP_S390_COW
)) {
258 phys_mem_set_alloc(legacy_s390_alloc
);
261 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_SIGP
, 0);
262 kvm_vm_enable_cap(s
, KVM_CAP_S390_USER_STSI
, 0);
267 unsigned long kvm_arch_vcpu_id(CPUState
*cpu
)
269 return cpu
->cpu_index
;
272 int kvm_arch_init_vcpu(CPUState
*cs
)
274 S390CPU
*cpu
= S390_CPU(cs
);
275 kvm_s390_set_cpu_state(cpu
, cpu
->env
.cpu_state
);
279 void kvm_s390_reset_vcpu(S390CPU
*cpu
)
281 CPUState
*cs
= CPU(cpu
);
283 /* The initial reset call is needed here to reset in-kernel
284 * vcpu data that we can't access directly from QEMU
285 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
286 * Before this ioctl cpu_synchronize_state() is called in common kvm
288 if (kvm_vcpu_ioctl(cs
, KVM_S390_INITIAL_RESET
, NULL
)) {
289 error_report("Initial CPU reset failed on CPU %i", cs
->cpu_index
);
292 kvm_s390_init_crypto();
295 static int can_sync_regs(CPUState
*cs
, int regs
)
297 return cap_sync_regs
&& (cs
->kvm_run
->kvm_valid_regs
& regs
) == regs
;
300 int kvm_arch_put_registers(CPUState
*cs
, int level
)
302 S390CPU
*cpu
= S390_CPU(cs
);
303 CPUS390XState
*env
= &cpu
->env
;
304 struct kvm_sregs sregs
;
305 struct kvm_regs regs
;
306 struct kvm_fpu fpu
= {};
310 /* always save the PSW and the GPRS*/
311 cs
->kvm_run
->psw_addr
= env
->psw
.addr
;
312 cs
->kvm_run
->psw_mask
= env
->psw
.mask
;
314 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
315 for (i
= 0; i
< 16; i
++) {
316 cs
->kvm_run
->s
.regs
.gprs
[i
] = env
->regs
[i
];
317 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_GPRS
;
320 for (i
= 0; i
< 16; i
++) {
321 regs
.gprs
[i
] = env
->regs
[i
];
323 r
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
330 for (i
= 0; i
< 16; i
++) {
331 fpu
.fprs
[i
] = env
->fregs
[i
].ll
;
335 r
= kvm_vcpu_ioctl(cs
, KVM_SET_FPU
, &fpu
);
340 /* Do we need to save more than that? */
341 if (level
== KVM_PUT_RUNTIME_STATE
) {
345 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
346 cs
->kvm_run
->s
.regs
.cputm
= env
->cputm
;
347 cs
->kvm_run
->s
.regs
.ckc
= env
->ckc
;
348 cs
->kvm_run
->s
.regs
.todpr
= env
->todpr
;
349 cs
->kvm_run
->s
.regs
.gbea
= env
->gbea
;
350 cs
->kvm_run
->s
.regs
.pp
= env
->pp
;
351 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ARCH0
;
354 * These ONE_REGS are not protected by a capability. As they are only
355 * necessary for migration we just trace a possible error, but don't
356 * return with an error return code.
358 kvm_set_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
359 kvm_set_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
360 kvm_set_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
361 kvm_set_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
362 kvm_set_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
365 /* pfault parameters */
366 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
367 cs
->kvm_run
->s
.regs
.pft
= env
->pfault_token
;
368 cs
->kvm_run
->s
.regs
.pfs
= env
->pfault_select
;
369 cs
->kvm_run
->s
.regs
.pfc
= env
->pfault_compare
;
370 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PFAULT
;
371 } else if (cap_async_pf
) {
372 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
376 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
380 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
386 /* access registers and control registers*/
387 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
388 for (i
= 0; i
< 16; i
++) {
389 cs
->kvm_run
->s
.regs
.acrs
[i
] = env
->aregs
[i
];
390 cs
->kvm_run
->s
.regs
.crs
[i
] = env
->cregs
[i
];
392 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_ACRS
;
393 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_CRS
;
395 for (i
= 0; i
< 16; i
++) {
396 sregs
.acrs
[i
] = env
->aregs
[i
];
397 sregs
.crs
[i
] = env
->cregs
[i
];
399 r
= kvm_vcpu_ioctl(cs
, KVM_SET_SREGS
, &sregs
);
405 /* Finally the prefix */
406 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
407 cs
->kvm_run
->s
.regs
.prefix
= env
->psa
;
408 cs
->kvm_run
->kvm_dirty_regs
|= KVM_SYNC_PREFIX
;
410 /* prefix is only supported via sync regs */
415 int kvm_arch_get_registers(CPUState
*cs
)
417 S390CPU
*cpu
= S390_CPU(cs
);
418 CPUS390XState
*env
= &cpu
->env
;
419 struct kvm_sregs sregs
;
420 struct kvm_regs regs
;
425 env
->psw
.addr
= cs
->kvm_run
->psw_addr
;
426 env
->psw
.mask
= cs
->kvm_run
->psw_mask
;
429 if (can_sync_regs(cs
, KVM_SYNC_GPRS
)) {
430 for (i
= 0; i
< 16; i
++) {
431 env
->regs
[i
] = cs
->kvm_run
->s
.regs
.gprs
[i
];
434 r
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
438 for (i
= 0; i
< 16; i
++) {
439 env
->regs
[i
] = regs
.gprs
[i
];
443 /* The ACRS and CRS */
444 if (can_sync_regs(cs
, KVM_SYNC_ACRS
| KVM_SYNC_CRS
)) {
445 for (i
= 0; i
< 16; i
++) {
446 env
->aregs
[i
] = cs
->kvm_run
->s
.regs
.acrs
[i
];
447 env
->cregs
[i
] = cs
->kvm_run
->s
.regs
.crs
[i
];
450 r
= kvm_vcpu_ioctl(cs
, KVM_GET_SREGS
, &sregs
);
454 for (i
= 0; i
< 16; i
++) {
455 env
->aregs
[i
] = sregs
.acrs
[i
];
456 env
->cregs
[i
] = sregs
.crs
[i
];
461 r
= kvm_vcpu_ioctl(cs
, KVM_GET_FPU
, &fpu
);
465 for (i
= 0; i
< 16; i
++) {
466 env
->fregs
[i
].ll
= fpu
.fprs
[i
];
471 if (can_sync_regs(cs
, KVM_SYNC_PREFIX
)) {
472 env
->psa
= cs
->kvm_run
->s
.regs
.prefix
;
475 if (can_sync_regs(cs
, KVM_SYNC_ARCH0
)) {
476 env
->cputm
= cs
->kvm_run
->s
.regs
.cputm
;
477 env
->ckc
= cs
->kvm_run
->s
.regs
.ckc
;
478 env
->todpr
= cs
->kvm_run
->s
.regs
.todpr
;
479 env
->gbea
= cs
->kvm_run
->s
.regs
.gbea
;
480 env
->pp
= cs
->kvm_run
->s
.regs
.pp
;
483 * These ONE_REGS are not protected by a capability. As they are only
484 * necessary for migration we just trace a possible error, but don't
485 * return with an error return code.
487 kvm_get_one_reg(cs
, KVM_REG_S390_CPU_TIMER
, &env
->cputm
);
488 kvm_get_one_reg(cs
, KVM_REG_S390_CLOCK_COMP
, &env
->ckc
);
489 kvm_get_one_reg(cs
, KVM_REG_S390_TODPR
, &env
->todpr
);
490 kvm_get_one_reg(cs
, KVM_REG_S390_GBEA
, &env
->gbea
);
491 kvm_get_one_reg(cs
, KVM_REG_S390_PP
, &env
->pp
);
494 /* pfault parameters */
495 if (can_sync_regs(cs
, KVM_SYNC_PFAULT
)) {
496 env
->pfault_token
= cs
->kvm_run
->s
.regs
.pft
;
497 env
->pfault_select
= cs
->kvm_run
->s
.regs
.pfs
;
498 env
->pfault_compare
= cs
->kvm_run
->s
.regs
.pfc
;
499 } else if (cap_async_pf
) {
500 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
504 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
508 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
517 int kvm_s390_get_clock(uint8_t *tod_high
, uint64_t *tod_low
)
520 struct kvm_device_attr attr
= {
521 .group
= KVM_S390_VM_TOD
,
522 .attr
= KVM_S390_VM_TOD_LOW
,
523 .addr
= (uint64_t)tod_low
,
526 r
= kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
531 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
532 attr
.addr
= (uint64_t)tod_high
;
533 return kvm_vm_ioctl(kvm_state
, KVM_GET_DEVICE_ATTR
, &attr
);
536 int kvm_s390_set_clock(uint8_t *tod_high
, uint64_t *tod_low
)
540 struct kvm_device_attr attr
= {
541 .group
= KVM_S390_VM_TOD
,
542 .attr
= KVM_S390_VM_TOD_LOW
,
543 .addr
= (uint64_t)tod_low
,
546 r
= kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
551 attr
.attr
= KVM_S390_VM_TOD_HIGH
;
552 attr
.addr
= (uint64_t)tod_high
;
553 return kvm_vm_ioctl(kvm_state
, KVM_SET_DEVICE_ATTR
, &attr
);
558 * @addr: the logical start address in guest memory
559 * @ar: the access register number
560 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
561 * @len: length that should be transfered
562 * @is_write: true = write, false = read
563 * Returns: 0 on success, non-zero if an exception or error occured
565 * Use KVM ioctl to read/write from/to guest memory. An access exception
566 * is injected into the vCPU in case of translation errors.
568 int kvm_s390_mem_op(S390CPU
*cpu
, vaddr addr
, uint8_t ar
, void *hostbuf
,
569 int len
, bool is_write
)
571 struct kvm_s390_mem_op mem_op
= {
573 .flags
= KVM_S390_MEMOP_F_INJECT_EXCEPTION
,
575 .op
= is_write
? KVM_S390_MEMOP_LOGICAL_WRITE
576 : KVM_S390_MEMOP_LOGICAL_READ
,
577 .buf
= (uint64_t)hostbuf
,
586 mem_op
.flags
|= KVM_S390_MEMOP_F_CHECK_ONLY
;
589 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_S390_MEM_OP
, &mem_op
);
591 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret
));
597 * Legacy layout for s390:
598 * Older S390 KVM requires the topmost vma of the RAM to be
599 * smaller than an system defined value, which is at least 256GB.
600 * Larger systems have larger values. We put the guest between
601 * the end of data segment (system break) and this value. We
602 * use 32GB as a base to have enough room for the system break
603 * to grow. We also have to use MAP parameters that avoid
604 * read-only mapping of guest pages.
606 static void *legacy_s390_alloc(size_t size
, uint64_t *align
)
610 mem
= mmap((void *) 0x800000000ULL
, size
,
611 PROT_EXEC
|PROT_READ
|PROT_WRITE
,
612 MAP_SHARED
| MAP_ANONYMOUS
| MAP_FIXED
, -1, 0);
613 return mem
== MAP_FAILED
? NULL
: mem
;
616 /* DIAG 501 is used for sw breakpoints */
617 static const uint8_t diag_501
[] = {0x83, 0x24, 0x05, 0x01};
619 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
622 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
623 sizeof(diag_501
), 0) ||
624 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)diag_501
,
625 sizeof(diag_501
), 1)) {
631 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
633 uint8_t t
[sizeof(diag_501
)];
635 if (cpu_memory_rw_debug(cs
, bp
->pc
, t
, sizeof(diag_501
), 0)) {
637 } else if (memcmp(t
, diag_501
, sizeof(diag_501
))) {
639 } else if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
640 sizeof(diag_501
), 1)) {
647 static struct kvm_hw_breakpoint
*find_hw_breakpoint(target_ulong addr
,
652 for (n
= 0; n
< nb_hw_breakpoints
; n
++) {
653 if (hw_breakpoints
[n
].addr
== addr
&& hw_breakpoints
[n
].type
== type
&&
654 (hw_breakpoints
[n
].len
== len
|| len
== -1)) {
655 return &hw_breakpoints
[n
];
662 static int insert_hw_breakpoint(target_ulong addr
, int len
, int type
)
666 if (find_hw_breakpoint(addr
, len
, type
)) {
670 size
= (nb_hw_breakpoints
+ 1) * sizeof(struct kvm_hw_breakpoint
);
672 if (!hw_breakpoints
) {
673 nb_hw_breakpoints
= 0;
674 hw_breakpoints
= (struct kvm_hw_breakpoint
*)g_try_malloc(size
);
677 (struct kvm_hw_breakpoint
*)g_try_realloc(hw_breakpoints
, size
);
680 if (!hw_breakpoints
) {
681 nb_hw_breakpoints
= 0;
685 hw_breakpoints
[nb_hw_breakpoints
].addr
= addr
;
686 hw_breakpoints
[nb_hw_breakpoints
].len
= len
;
687 hw_breakpoints
[nb_hw_breakpoints
].type
= type
;
694 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
695 target_ulong len
, int type
)
698 case GDB_BREAKPOINT_HW
:
701 case GDB_WATCHPOINT_WRITE
:
705 type
= KVM_HW_WP_WRITE
;
710 return insert_hw_breakpoint(addr
, len
, type
);
713 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
714 target_ulong len
, int type
)
717 struct kvm_hw_breakpoint
*bp
= find_hw_breakpoint(addr
, len
, type
);
724 if (nb_hw_breakpoints
> 0) {
726 * In order to trim the array, move the last element to the position to
727 * be removed - if necessary.
729 if (bp
!= &hw_breakpoints
[nb_hw_breakpoints
]) {
730 *bp
= hw_breakpoints
[nb_hw_breakpoints
];
732 size
= nb_hw_breakpoints
* sizeof(struct kvm_hw_breakpoint
);
734 (struct kvm_hw_breakpoint
*)g_realloc(hw_breakpoints
, size
);
736 g_free(hw_breakpoints
);
737 hw_breakpoints
= NULL
;
743 void kvm_arch_remove_all_hw_breakpoints(void)
745 nb_hw_breakpoints
= 0;
746 g_free(hw_breakpoints
);
747 hw_breakpoints
= NULL
;
750 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
754 if (nb_hw_breakpoints
> 0) {
755 dbg
->arch
.nr_hw_bp
= nb_hw_breakpoints
;
756 dbg
->arch
.hw_bp
= hw_breakpoints
;
758 for (i
= 0; i
< nb_hw_breakpoints
; ++i
) {
759 hw_breakpoints
[i
].phys_addr
= s390_cpu_get_phys_addr_debug(cpu
,
760 hw_breakpoints
[i
].addr
);
762 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
764 dbg
->arch
.nr_hw_bp
= 0;
765 dbg
->arch
.hw_bp
= NULL
;
769 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
773 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
775 return MEMTXATTRS_UNSPECIFIED
;
778 int kvm_arch_process_async_events(CPUState
*cs
)
783 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq
*irq
,
784 struct kvm_s390_interrupt
*interrupt
)
788 interrupt
->type
= irq
->type
;
790 case KVM_S390_INT_VIRTIO
:
791 interrupt
->parm
= irq
->u
.ext
.ext_params
;
793 case KVM_S390_INT_PFAULT_INIT
:
794 case KVM_S390_INT_PFAULT_DONE
:
795 interrupt
->parm64
= irq
->u
.ext
.ext_params2
;
797 case KVM_S390_PROGRAM_INT
:
798 interrupt
->parm
= irq
->u
.pgm
.code
;
800 case KVM_S390_SIGP_SET_PREFIX
:
801 interrupt
->parm
= irq
->u
.prefix
.address
;
803 case KVM_S390_INT_SERVICE
:
804 interrupt
->parm
= irq
->u
.ext
.ext_params
;
807 interrupt
->parm
= irq
->u
.mchk
.cr14
;
808 interrupt
->parm64
= irq
->u
.mchk
.mcic
;
810 case KVM_S390_INT_EXTERNAL_CALL
:
811 interrupt
->parm
= irq
->u
.extcall
.code
;
813 case KVM_S390_INT_EMERGENCY
:
814 interrupt
->parm
= irq
->u
.emerg
.code
;
816 case KVM_S390_SIGP_STOP
:
817 case KVM_S390_RESTART
:
818 break; /* These types have no parameters */
819 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
820 interrupt
->parm
= irq
->u
.io
.subchannel_id
<< 16;
821 interrupt
->parm
|= irq
->u
.io
.subchannel_nr
;
822 interrupt
->parm64
= (uint64_t)irq
->u
.io
.io_int_parm
<< 32;
823 interrupt
->parm64
|= irq
->u
.io
.io_int_word
;
832 void kvm_s390_vcpu_interrupt(S390CPU
*cpu
, struct kvm_s390_irq
*irq
)
834 struct kvm_s390_interrupt kvmint
= {};
835 CPUState
*cs
= CPU(cpu
);
838 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
840 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
844 r
= kvm_vcpu_ioctl(cs
, KVM_S390_INTERRUPT
, &kvmint
);
846 fprintf(stderr
, "KVM failed to inject interrupt\n");
851 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq
*irq
)
853 struct kvm_s390_interrupt kvmint
= {};
856 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
858 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
862 r
= kvm_vm_ioctl(kvm_state
, KVM_S390_INTERRUPT
, &kvmint
);
864 fprintf(stderr
, "KVM failed to inject interrupt\n");
869 void kvm_s390_floating_interrupt(struct kvm_s390_irq
*irq
)
871 static bool use_flic
= true;
875 r
= kvm_s390_inject_flic(irq
);
883 __kvm_s390_floating_interrupt(irq
);
886 void kvm_s390_virtio_irq(int config_change
, uint64_t token
)
888 struct kvm_s390_irq irq
= {
889 .type
= KVM_S390_INT_VIRTIO
,
890 .u
.ext
.ext_params
= config_change
,
891 .u
.ext
.ext_params2
= token
,
894 kvm_s390_floating_interrupt(&irq
);
897 void kvm_s390_service_interrupt(uint32_t parm
)
899 struct kvm_s390_irq irq
= {
900 .type
= KVM_S390_INT_SERVICE
,
901 .u
.ext
.ext_params
= parm
,
904 kvm_s390_floating_interrupt(&irq
);
907 static void enter_pgmcheck(S390CPU
*cpu
, uint16_t code
)
909 struct kvm_s390_irq irq
= {
910 .type
= KVM_S390_PROGRAM_INT
,
914 kvm_s390_vcpu_interrupt(cpu
, &irq
);
917 void kvm_s390_access_exception(S390CPU
*cpu
, uint16_t code
, uint64_t te_code
)
919 struct kvm_s390_irq irq
= {
920 .type
= KVM_S390_PROGRAM_INT
,
922 .u
.pgm
.trans_exc_code
= te_code
,
923 .u
.pgm
.exc_access_id
= te_code
& 3,
926 kvm_s390_vcpu_interrupt(cpu
, &irq
);
929 static int kvm_sclp_service_call(S390CPU
*cpu
, struct kvm_run
*run
,
932 CPUS390XState
*env
= &cpu
->env
;
937 cpu_synchronize_state(CPU(cpu
));
938 sccb
= env
->regs
[ipbh0
& 0xf];
939 code
= env
->regs
[(ipbh0
& 0xf0) >> 4];
941 r
= sclp_service_call(env
, sccb
, code
);
943 enter_pgmcheck(cpu
, -r
);
951 static int handle_b2(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
953 CPUS390XState
*env
= &cpu
->env
;
955 uint16_t ipbh0
= (run
->s390_sieic
.ipb
& 0xffff0000) >> 16;
957 cpu_synchronize_state(CPU(cpu
));
961 ioinst_handle_xsch(cpu
, env
->regs
[1]);
964 ioinst_handle_csch(cpu
, env
->regs
[1]);
967 ioinst_handle_hsch(cpu
, env
->regs
[1]);
970 ioinst_handle_msch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
973 ioinst_handle_ssch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
976 ioinst_handle_stcrw(cpu
, run
->s390_sieic
.ipb
);
979 ioinst_handle_stsch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
982 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
983 fprintf(stderr
, "Spurious tsch intercept\n");
986 ioinst_handle_chsc(cpu
, run
->s390_sieic
.ipb
);
989 /* This should have been handled by kvm already. */
990 fprintf(stderr
, "Spurious tpi intercept\n");
993 ioinst_handle_schm(cpu
, env
->regs
[1], env
->regs
[2],
994 run
->s390_sieic
.ipb
);
997 ioinst_handle_rsch(cpu
, env
->regs
[1]);
1000 ioinst_handle_rchp(cpu
, env
->regs
[1]);
1003 /* We do not provide this instruction, it is suppressed. */
1006 ioinst_handle_sal(cpu
, env
->regs
[1]);
1009 /* Not provided, set CC = 3 for subchannel not operational */
1012 case PRIV_B2_SCLP_CALL
:
1013 rc
= kvm_sclp_service_call(cpu
, run
, ipbh0
);
1017 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1
);
1024 static uint64_t get_base_disp_rxy(S390CPU
*cpu
, struct kvm_run
*run
,
1027 CPUS390XState
*env
= &cpu
->env
;
1028 uint32_t x2
= (run
->s390_sieic
.ipa
& 0x000f);
1029 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1030 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1031 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1033 if (disp2
& 0x80000) {
1034 disp2
+= 0xfff00000;
1040 return (base2
? env
->regs
[base2
] : 0) +
1041 (x2
? env
->regs
[x2
] : 0) + (long)(int)disp2
;
1044 static uint64_t get_base_disp_rsy(S390CPU
*cpu
, struct kvm_run
*run
,
1047 CPUS390XState
*env
= &cpu
->env
;
1048 uint32_t base2
= run
->s390_sieic
.ipb
>> 28;
1049 uint32_t disp2
= ((run
->s390_sieic
.ipb
& 0x0fff0000) >> 16) +
1050 ((run
->s390_sieic
.ipb
& 0xff00) << 4);
1052 if (disp2
& 0x80000) {
1053 disp2
+= 0xfff00000;
1059 return (base2
? env
->regs
[base2
] : 0) + (long)(int)disp2
;
1062 static int kvm_clp_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1064 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1066 return clp_service_call(cpu
, r2
);
1069 static int kvm_pcilg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1071 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1072 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1074 return pcilg_service_call(cpu
, r1
, r2
);
1077 static int kvm_pcistg_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1079 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1080 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1082 return pcistg_service_call(cpu
, r1
, r2
);
1085 static int kvm_stpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1087 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1091 cpu_synchronize_state(CPU(cpu
));
1092 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1094 return stpcifc_service_call(cpu
, r1
, fiba
, ar
);
1097 static int kvm_sic_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1103 static int kvm_rpcit_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1105 uint8_t r1
= (run
->s390_sieic
.ipb
& 0x00f00000) >> 20;
1106 uint8_t r2
= (run
->s390_sieic
.ipb
& 0x000f0000) >> 16;
1108 return rpcit_service_call(cpu
, r1
, r2
);
1111 static int kvm_pcistb_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1113 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1114 uint8_t r3
= run
->s390_sieic
.ipa
& 0x000f;
1118 cpu_synchronize_state(CPU(cpu
));
1119 gaddr
= get_base_disp_rsy(cpu
, run
, &ar
);
1121 return pcistb_service_call(cpu
, r1
, r3
, gaddr
, ar
);
1124 static int kvm_mpcifc_service_call(S390CPU
*cpu
, struct kvm_run
*run
)
1126 uint8_t r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1130 cpu_synchronize_state(CPU(cpu
));
1131 fiba
= get_base_disp_rxy(cpu
, run
, &ar
);
1133 return mpcifc_service_call(cpu
, r1
, fiba
, ar
);
1136 static int handle_b9(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1142 r
= kvm_clp_service_call(cpu
, run
);
1144 case PRIV_B9_PCISTG
:
1145 r
= kvm_pcistg_service_call(cpu
, run
);
1148 r
= kvm_pcilg_service_call(cpu
, run
);
1151 r
= kvm_rpcit_service_call(cpu
, run
);
1154 /* just inject exception */
1159 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1
);
1166 static int handle_eb(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1171 case PRIV_EB_PCISTB
:
1172 r
= kvm_pcistb_service_call(cpu
, run
);
1175 r
= kvm_sic_service_call(cpu
, run
);
1178 /* just inject exception */
1183 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl
);
1190 static int handle_e3(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipbl
)
1195 case PRIV_E3_MPCIFC
:
1196 r
= kvm_mpcifc_service_call(cpu
, run
);
1198 case PRIV_E3_STPCIFC
:
1199 r
= kvm_stpcifc_service_call(cpu
, run
);
1203 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl
);
1210 static int handle_hypercall(S390CPU
*cpu
, struct kvm_run
*run
)
1212 CPUS390XState
*env
= &cpu
->env
;
1215 cpu_synchronize_state(CPU(cpu
));
1216 ret
= s390_virtio_hypercall(env
);
1217 if (ret
== -EINVAL
) {
1218 enter_pgmcheck(cpu
, PGM_SPECIFICATION
);
1225 static void kvm_handle_diag_308(S390CPU
*cpu
, struct kvm_run
*run
)
1229 cpu_synchronize_state(CPU(cpu
));
1230 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 4;
1231 r3
= run
->s390_sieic
.ipa
& 0x000f;
1232 handle_diag_308(&cpu
->env
, r1
, r3
);
1235 static int handle_sw_breakpoint(S390CPU
*cpu
, struct kvm_run
*run
)
1237 CPUS390XState
*env
= &cpu
->env
;
1240 cpu_synchronize_state(CPU(cpu
));
1242 pc
= env
->psw
.addr
- 4;
1243 if (kvm_find_sw_breakpoint(CPU(cpu
), pc
)) {
1251 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1253 static int handle_diag(S390CPU
*cpu
, struct kvm_run
*run
, uint32_t ipb
)
1259 * For any diagnose call we support, bits 48-63 of the resulting
1260 * address specify the function code; the remainder is ignored.
1262 func_code
= decode_basedisp_rs(&cpu
->env
, ipb
, NULL
) & DIAG_KVM_CODE_MASK
;
1263 switch (func_code
) {
1265 kvm_handle_diag_308(cpu
, run
);
1267 case DIAG_KVM_HYPERCALL
:
1268 r
= handle_hypercall(cpu
, run
);
1270 case DIAG_KVM_BREAKPOINT
:
1271 r
= handle_sw_breakpoint(cpu
, run
);
1274 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code
);
1275 enter_pgmcheck(cpu
, PGM_SPECIFICATION
);
1282 typedef struct SigpInfo
{
1286 uint64_t *status_reg
;
1289 static void set_sigp_status(SigpInfo
*si
, uint64_t status
)
1291 *si
->status_reg
&= 0xffffffff00000000ULL
;
1292 *si
->status_reg
|= status
;
1293 si
->cc
= SIGP_CC_STATUS_STORED
;
1296 static void sigp_start(void *arg
)
1300 if (s390_cpu_get_state(si
->cpu
) != CPU_STATE_STOPPED
) {
1301 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1305 s390_cpu_set_state(CPU_STATE_OPERATING
, si
->cpu
);
1306 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1309 static void sigp_stop(void *arg
)
1312 struct kvm_s390_irq irq
= {
1313 .type
= KVM_S390_SIGP_STOP
,
1316 if (s390_cpu_get_state(si
->cpu
) != CPU_STATE_OPERATING
) {
1317 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1321 /* disabled wait - sleeping in user space */
1322 if (CPU(si
->cpu
)->halted
) {
1323 s390_cpu_set_state(CPU_STATE_STOPPED
, si
->cpu
);
1325 /* execute the stop function */
1326 si
->cpu
->env
.sigp_order
= SIGP_STOP
;
1327 kvm_s390_vcpu_interrupt(si
->cpu
, &irq
);
1329 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1332 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1333 #define SAVE_AREA_SIZE 512
1334 static int kvm_s390_store_status(S390CPU
*cpu
, hwaddr addr
, bool store_arch
)
1336 static const uint8_t ar_id
= 1;
1337 uint64_t ckc
= cpu
->env
.ckc
>> 8;
1339 hwaddr len
= SAVE_AREA_SIZE
;
1341 mem
= cpu_physical_memory_map(addr
, &len
, 1);
1345 if (len
!= SAVE_AREA_SIZE
) {
1346 cpu_physical_memory_unmap(mem
, len
, 1, 0);
1351 cpu_physical_memory_write(offsetof(LowCore
, ar_access_id
), &ar_id
, 1);
1353 memcpy(mem
, &cpu
->env
.fregs
, 128);
1354 memcpy(mem
+ 128, &cpu
->env
.regs
, 128);
1355 memcpy(mem
+ 256, &cpu
->env
.psw
, 16);
1356 memcpy(mem
+ 280, &cpu
->env
.psa
, 4);
1357 memcpy(mem
+ 284, &cpu
->env
.fpc
, 4);
1358 memcpy(mem
+ 292, &cpu
->env
.todpr
, 4);
1359 memcpy(mem
+ 296, &cpu
->env
.cputm
, 8);
1360 memcpy(mem
+ 304, &ckc
, 8);
1361 memcpy(mem
+ 320, &cpu
->env
.aregs
, 64);
1362 memcpy(mem
+ 384, &cpu
->env
.cregs
, 128);
1364 cpu_physical_memory_unmap(mem
, len
, 1, len
);
1369 static void sigp_stop_and_store_status(void *arg
)
1372 struct kvm_s390_irq irq
= {
1373 .type
= KVM_S390_SIGP_STOP
,
1376 /* disabled wait - sleeping in user space */
1377 if (s390_cpu_get_state(si
->cpu
) == CPU_STATE_OPERATING
&&
1378 CPU(si
->cpu
)->halted
) {
1379 s390_cpu_set_state(CPU_STATE_STOPPED
, si
->cpu
);
1382 switch (s390_cpu_get_state(si
->cpu
)) {
1383 case CPU_STATE_OPERATING
:
1384 si
->cpu
->env
.sigp_order
= SIGP_STOP_STORE_STATUS
;
1385 kvm_s390_vcpu_interrupt(si
->cpu
, &irq
);
1386 /* store will be performed when handling the stop intercept */
1388 case CPU_STATE_STOPPED
:
1389 /* already stopped, just store the status */
1390 cpu_synchronize_state(CPU(si
->cpu
));
1391 kvm_s390_store_status(si
->cpu
, KVM_S390_STORE_STATUS_DEF_ADDR
, true);
1394 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1397 static void sigp_store_status_at_address(void *arg
)
1400 uint32_t address
= si
->param
& 0x7ffffe00u
;
1402 /* cpu has to be stopped */
1403 if (s390_cpu_get_state(si
->cpu
) != CPU_STATE_STOPPED
) {
1404 set_sigp_status(si
, SIGP_STAT_INCORRECT_STATE
);
1408 cpu_synchronize_state(CPU(si
->cpu
));
1410 if (kvm_s390_store_status(si
->cpu
, address
, false)) {
1411 set_sigp_status(si
, SIGP_STAT_INVALID_PARAMETER
);
1414 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1417 static void sigp_restart(void *arg
)
1420 struct kvm_s390_irq irq
= {
1421 .type
= KVM_S390_RESTART
,
1424 switch (s390_cpu_get_state(si
->cpu
)) {
1425 case CPU_STATE_STOPPED
:
1426 /* the restart irq has to be delivered prior to any other pending irq */
1427 cpu_synchronize_state(CPU(si
->cpu
));
1428 do_restart_interrupt(&si
->cpu
->env
);
1429 s390_cpu_set_state(CPU_STATE_OPERATING
, si
->cpu
);
1431 case CPU_STATE_OPERATING
:
1432 kvm_s390_vcpu_interrupt(si
->cpu
, &irq
);
1435 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1438 int kvm_s390_cpu_restart(S390CPU
*cpu
)
1444 run_on_cpu(CPU(cpu
), sigp_restart
, &si
);
1445 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu
->env
);
1449 static void sigp_initial_cpu_reset(void *arg
)
1452 CPUState
*cs
= CPU(si
->cpu
);
1453 S390CPUClass
*scc
= S390_CPU_GET_CLASS(si
->cpu
);
1455 cpu_synchronize_state(cs
);
1456 scc
->initial_cpu_reset(cs
);
1457 cpu_synchronize_post_reset(cs
);
1458 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1461 static void sigp_cpu_reset(void *arg
)
1464 CPUState
*cs
= CPU(si
->cpu
);
1465 S390CPUClass
*scc
= S390_CPU_GET_CLASS(si
->cpu
);
1467 cpu_synchronize_state(cs
);
1469 cpu_synchronize_post_reset(cs
);
1470 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1473 static void sigp_set_prefix(void *arg
)
1476 uint32_t addr
= si
->param
& 0x7fffe000u
;
1478 cpu_synchronize_state(CPU(si
->cpu
));
1480 if (!address_space_access_valid(&address_space_memory
, addr
,
1481 sizeof(struct LowCore
), false)) {
1482 set_sigp_status(si
, SIGP_STAT_INVALID_PARAMETER
);
1486 /* cpu has to be stopped */
1487 if (s390_cpu_get_state(si
->cpu
) != CPU_STATE_STOPPED
) {
1488 set_sigp_status(si
, SIGP_STAT_INCORRECT_STATE
);
1492 si
->cpu
->env
.psa
= addr
;
1493 cpu_synchronize_post_init(CPU(si
->cpu
));
1494 si
->cc
= SIGP_CC_ORDER_CODE_ACCEPTED
;
1497 static int handle_sigp_single_dst(S390CPU
*dst_cpu
, uint8_t order
,
1498 uint64_t param
, uint64_t *status_reg
)
1503 .status_reg
= status_reg
,
1506 /* cpu available? */
1507 if (dst_cpu
== NULL
) {
1508 return SIGP_CC_NOT_OPERATIONAL
;
1511 /* only resets can break pending orders */
1512 if (dst_cpu
->env
.sigp_order
!= 0 &&
1513 order
!= SIGP_CPU_RESET
&&
1514 order
!= SIGP_INITIAL_CPU_RESET
) {
1515 return SIGP_CC_BUSY
;
1520 run_on_cpu(CPU(dst_cpu
), sigp_start
, &si
);
1523 run_on_cpu(CPU(dst_cpu
), sigp_stop
, &si
);
1526 run_on_cpu(CPU(dst_cpu
), sigp_restart
, &si
);
1528 case SIGP_STOP_STORE_STATUS
:
1529 run_on_cpu(CPU(dst_cpu
), sigp_stop_and_store_status
, &si
);
1531 case SIGP_STORE_STATUS_ADDR
:
1532 run_on_cpu(CPU(dst_cpu
), sigp_store_status_at_address
, &si
);
1534 case SIGP_SET_PREFIX
:
1535 run_on_cpu(CPU(dst_cpu
), sigp_set_prefix
, &si
);
1537 case SIGP_INITIAL_CPU_RESET
:
1538 run_on_cpu(CPU(dst_cpu
), sigp_initial_cpu_reset
, &si
);
1540 case SIGP_CPU_RESET
:
1541 run_on_cpu(CPU(dst_cpu
), sigp_cpu_reset
, &si
);
1544 DPRINTF("KVM: unknown SIGP: 0x%x\n", order
);
1545 set_sigp_status(&si
, SIGP_STAT_INVALID_ORDER
);
1551 static int sigp_set_architecture(S390CPU
*cpu
, uint32_t param
,
1552 uint64_t *status_reg
)
1557 /* due to the BQL, we are the only active cpu */
1558 CPU_FOREACH(cur_cs
) {
1559 cur_cpu
= S390_CPU(cur_cs
);
1560 if (cur_cpu
->env
.sigp_order
!= 0) {
1561 return SIGP_CC_BUSY
;
1563 cpu_synchronize_state(cur_cs
);
1564 /* all but the current one have to be stopped */
1565 if (cur_cpu
!= cpu
&&
1566 s390_cpu_get_state(cur_cpu
) != CPU_STATE_STOPPED
) {
1567 *status_reg
&= 0xffffffff00000000ULL
;
1568 *status_reg
|= SIGP_STAT_INCORRECT_STATE
;
1569 return SIGP_CC_STATUS_STORED
;
1573 switch (param
& 0xff) {
1574 case SIGP_MODE_ESA_S390
:
1576 return SIGP_CC_NOT_OPERATIONAL
;
1577 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW
:
1578 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW
:
1579 CPU_FOREACH(cur_cs
) {
1580 cur_cpu
= S390_CPU(cur_cs
);
1581 cur_cpu
->env
.pfault_token
= -1UL;
1585 *status_reg
&= 0xffffffff00000000ULL
;
1586 *status_reg
|= SIGP_STAT_INVALID_PARAMETER
;
1587 return SIGP_CC_STATUS_STORED
;
1590 return SIGP_CC_ORDER_CODE_ACCEPTED
;
1593 #define SIGP_ORDER_MASK 0x000000ff
1595 static int handle_sigp(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
1597 CPUS390XState
*env
= &cpu
->env
;
1598 const uint8_t r1
= ipa1
>> 4;
1599 const uint8_t r3
= ipa1
& 0x0f;
1602 uint64_t *status_reg
;
1604 S390CPU
*dst_cpu
= NULL
;
1606 cpu_synchronize_state(CPU(cpu
));
1608 /* get order code */
1609 order
= decode_basedisp_rs(env
, run
->s390_sieic
.ipb
, NULL
)
1611 status_reg
= &env
->regs
[r1
];
1612 param
= (r1
% 2) ? env
->regs
[r1
] : env
->regs
[r1
+ 1];
1616 ret
= sigp_set_architecture(cpu
, param
, status_reg
);
1619 /* all other sigp orders target a single vcpu */
1620 dst_cpu
= s390_cpu_addr2state(env
->regs
[r3
]);
1621 ret
= handle_sigp_single_dst(dst_cpu
, order
, param
, status_reg
);
1624 trace_kvm_sigp_finished(order
, CPU(cpu
)->cpu_index
,
1625 dst_cpu
? CPU(dst_cpu
)->cpu_index
: -1, ret
);
1635 static int handle_instruction(S390CPU
*cpu
, struct kvm_run
*run
)
1637 unsigned int ipa0
= (run
->s390_sieic
.ipa
& 0xff00);
1638 uint8_t ipa1
= run
->s390_sieic
.ipa
& 0x00ff;
1641 DPRINTF("handle_instruction 0x%x 0x%x\n",
1642 run
->s390_sieic
.ipa
, run
->s390_sieic
.ipb
);
1645 r
= handle_b2(cpu
, run
, ipa1
);
1648 r
= handle_b9(cpu
, run
, ipa1
);
1651 r
= handle_eb(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1654 r
= handle_e3(cpu
, run
, run
->s390_sieic
.ipb
& 0xff);
1657 r
= handle_diag(cpu
, run
, run
->s390_sieic
.ipb
);
1660 r
= handle_sigp(cpu
, run
, ipa1
);
1666 enter_pgmcheck(cpu
, 0x0001);
1672 static bool is_special_wait_psw(CPUState
*cs
)
1674 /* signal quiesce */
1675 return cs
->kvm_run
->psw_addr
== 0xfffUL
;
1678 static void guest_panicked(void)
1680 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE
,
1682 vm_stop(RUN_STATE_GUEST_PANICKED
);
1685 static void unmanageable_intercept(S390CPU
*cpu
, const char *str
, int pswoffset
)
1687 CPUState
*cs
= CPU(cpu
);
1689 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1690 str
, cs
->cpu_index
, ldq_phys(cs
->as
, cpu
->env
.psa
+ pswoffset
),
1691 ldq_phys(cs
->as
, cpu
->env
.psa
+ pswoffset
+ 8));
1696 static int handle_intercept(S390CPU
*cpu
)
1698 CPUState
*cs
= CPU(cpu
);
1699 struct kvm_run
*run
= cs
->kvm_run
;
1700 int icpt_code
= run
->s390_sieic
.icptcode
;
1703 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code
,
1704 (long)cs
->kvm_run
->psw_addr
);
1705 switch (icpt_code
) {
1706 case ICPT_INSTRUCTION
:
1707 r
= handle_instruction(cpu
, run
);
1710 unmanageable_intercept(cpu
, "program interrupt",
1711 offsetof(LowCore
, program_new_psw
));
1715 unmanageable_intercept(cpu
, "external interrupt",
1716 offsetof(LowCore
, external_new_psw
));
1720 /* disabled wait, since enabled wait is handled in kernel */
1721 cpu_synchronize_state(cs
);
1722 if (s390_cpu_halt(cpu
) == 0) {
1723 if (is_special_wait_psw(cs
)) {
1724 qemu_system_shutdown_request();
1732 if (s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
) == 0) {
1733 qemu_system_shutdown_request();
1735 if (cpu
->env
.sigp_order
== SIGP_STOP_STORE_STATUS
) {
1736 kvm_s390_store_status(cpu
, KVM_S390_STORE_STATUS_DEF_ADDR
,
1739 cpu
->env
.sigp_order
= 0;
1742 case ICPT_SOFT_INTERCEPT
:
1743 fprintf(stderr
, "KVM unimplemented icpt SOFT\n");
1747 fprintf(stderr
, "KVM unimplemented icpt IO\n");
1751 fprintf(stderr
, "Unknown intercept code: %d\n", icpt_code
);
1759 static int handle_tsch(S390CPU
*cpu
)
1761 CPUState
*cs
= CPU(cpu
);
1762 struct kvm_run
*run
= cs
->kvm_run
;
1765 cpu_synchronize_state(cs
);
1767 ret
= ioinst_handle_tsch(cpu
, cpu
->env
.regs
[1], run
->s390_tsch
.ipb
);
1771 * If an I/O interrupt had been dequeued, we have to reinject it.
1773 if (run
->s390_tsch
.dequeued
) {
1774 kvm_s390_io_interrupt(run
->s390_tsch
.subchannel_id
,
1775 run
->s390_tsch
.subchannel_nr
,
1776 run
->s390_tsch
.io_int_parm
,
1777 run
->s390_tsch
.io_int_word
);
1784 static void insert_stsi_3_2_2(S390CPU
*cpu
, __u64 addr
, uint8_t ar
)
1786 struct sysib_322 sysib
;
1789 if (s390_cpu_virt_mem_read(cpu
, addr
, ar
, &sysib
, sizeof(sysib
))) {
1792 /* Shift the stack of Extended Names to prepare for our own data */
1793 memmove(&sysib
.ext_names
[1], &sysib
.ext_names
[0],
1794 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- 1));
1795 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1796 * assumed it's not capable of managing Extended Names for lower levels.
1798 for (del
= 1; del
< sysib
.count
; del
++) {
1799 if (!sysib
.vm
[del
].ext_name_encoding
|| !sysib
.ext_names
[del
][0]) {
1803 if (del
< sysib
.count
) {
1804 memset(sysib
.ext_names
[del
], 0,
1805 sizeof(sysib
.ext_names
[0]) * (sysib
.count
- del
));
1807 /* Insert short machine name in EBCDIC, padded with blanks */
1809 memset(sysib
.vm
[0].name
, 0x40, sizeof(sysib
.vm
[0].name
));
1810 ebcdic_put(sysib
.vm
[0].name
, qemu_name
, MIN(sizeof(sysib
.vm
[0].name
),
1811 strlen(qemu_name
)));
1813 sysib
.vm
[0].ext_name_encoding
= 2; /* 2 = UTF-8 */
1814 memset(sysib
.ext_names
[0], 0, sizeof(sysib
.ext_names
[0]));
1815 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1816 * considered by s390 as not capable of providing any Extended Name.
1817 * Therefore if no name was specified on qemu invocation, we go with the
1818 * same "KVMguest" default, which KVM has filled into short name field.
1821 strncpy((char *)sysib
.ext_names
[0], qemu_name
,
1822 sizeof(sysib
.ext_names
[0]));
1824 strcpy((char *)sysib
.ext_names
[0], "KVMguest");
1827 memcpy(sysib
.vm
[0].uuid
, qemu_uuid
, sizeof(sysib
.vm
[0].uuid
));
1829 s390_cpu_virt_mem_write(cpu
, addr
, ar
, &sysib
, sizeof(sysib
));
1832 static int handle_stsi(S390CPU
*cpu
)
1834 CPUState
*cs
= CPU(cpu
);
1835 struct kvm_run
*run
= cs
->kvm_run
;
1837 switch (run
->s390_stsi
.fc
) {
1839 if (run
->s390_stsi
.sel1
!= 2 || run
->s390_stsi
.sel2
!= 2) {
1842 /* Only sysib 3.2.2 needs post-handling for now. */
1843 insert_stsi_3_2_2(cpu
, run
->s390_stsi
.addr
, run
->s390_stsi
.ar
);
1850 static int kvm_arch_handle_debug_exit(S390CPU
*cpu
)
1852 CPUState
*cs
= CPU(cpu
);
1853 struct kvm_run
*run
= cs
->kvm_run
;
1856 struct kvm_debug_exit_arch
*arch_info
= &run
->debug
.arch
;
1858 switch (arch_info
->type
) {
1859 case KVM_HW_WP_WRITE
:
1860 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1861 cs
->watchpoint_hit
= &hw_watchpoint
;
1862 hw_watchpoint
.vaddr
= arch_info
->addr
;
1863 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1868 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1872 case KVM_SINGLESTEP
:
1873 if (cs
->singlestep_enabled
) {
1884 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
1886 S390CPU
*cpu
= S390_CPU(cs
);
1889 switch (run
->exit_reason
) {
1890 case KVM_EXIT_S390_SIEIC
:
1891 ret
= handle_intercept(cpu
);
1893 case KVM_EXIT_S390_RESET
:
1894 s390_reipl_request();
1896 case KVM_EXIT_S390_TSCH
:
1897 ret
= handle_tsch(cpu
);
1899 case KVM_EXIT_S390_STSI
:
1900 ret
= handle_stsi(cpu
);
1902 case KVM_EXIT_DEBUG
:
1903 ret
= kvm_arch_handle_debug_exit(cpu
);
1906 fprintf(stderr
, "Unknown KVM exit: %d\n", run
->exit_reason
);
1911 ret
= EXCP_INTERRUPT
;
1916 bool kvm_arch_stop_on_emulation_error(CPUState
*cpu
)
1921 int kvm_arch_on_sigbus_vcpu(CPUState
*cpu
, int code
, void *addr
)
1926 int kvm_arch_on_sigbus(int code
, void *addr
)
1931 void kvm_s390_io_interrupt(uint16_t subchannel_id
,
1932 uint16_t subchannel_nr
, uint32_t io_int_parm
,
1933 uint32_t io_int_word
)
1935 struct kvm_s390_irq irq
= {
1936 .u
.io
.subchannel_id
= subchannel_id
,
1937 .u
.io
.subchannel_nr
= subchannel_nr
,
1938 .u
.io
.io_int_parm
= io_int_parm
,
1939 .u
.io
.io_int_word
= io_int_word
,
1942 if (io_int_word
& IO_INT_WORD_AI
) {
1943 irq
.type
= KVM_S390_INT_IO(1, 0, 0, 0);
1945 irq
.type
= ((subchannel_id
& 0xff00) << 24) |
1946 ((subchannel_id
& 0x00060) << 22) | (subchannel_nr
<< 16);
1948 kvm_s390_floating_interrupt(&irq
);
1951 void kvm_s390_crw_mchk(void)
1953 struct kvm_s390_irq irq
= {
1954 .type
= KVM_S390_MCHK
,
1955 .u
.mchk
.cr14
= 1 << 28,
1956 .u
.mchk
.mcic
= 0x00400f1d40330000ULL
,
1958 kvm_s390_floating_interrupt(&irq
);
1961 void kvm_s390_enable_css_support(S390CPU
*cpu
)
1965 /* Activate host kernel channel subsystem support. */
1966 r
= kvm_vcpu_enable_cap(CPU(cpu
), KVM_CAP_S390_CSS_SUPPORT
, 0);
1970 void kvm_arch_init_irq_routing(KVMState
*s
)
1973 * Note that while irqchip capabilities generally imply that cpustates
1974 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1975 * have to override the common code kvm_halt_in_kernel_allowed setting.
1977 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
1978 kvm_gsi_routing_allowed
= true;
1979 kvm_halt_in_kernel_allowed
= false;
1983 int kvm_s390_assign_subch_ioeventfd(EventNotifier
*notifier
, uint32_t sch
,
1984 int vq
, bool assign
)
1986 struct kvm_ioeventfd kick
= {
1987 .flags
= KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY
|
1988 KVM_IOEVENTFD_FLAG_DATAMATCH
,
1989 .fd
= event_notifier_get_fd(notifier
),
1994 if (!kvm_check_extension(kvm_state
, KVM_CAP_IOEVENTFD
)) {
1998 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
2000 return kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);
2003 int kvm_s390_get_memslot_count(KVMState
*s
)
2005 return kvm_check_extension(s
, KVM_CAP_NR_MEMSLOTS
);
2008 int kvm_s390_set_cpu_state(S390CPU
*cpu
, uint8_t cpu_state
)
2010 struct kvm_mp_state mp_state
= {};
2013 /* the kvm part might not have been initialized yet */
2014 if (CPU(cpu
)->kvm_state
== NULL
) {
2018 switch (cpu_state
) {
2019 case CPU_STATE_STOPPED
:
2020 mp_state
.mp_state
= KVM_MP_STATE_STOPPED
;
2022 case CPU_STATE_CHECK_STOP
:
2023 mp_state
.mp_state
= KVM_MP_STATE_CHECK_STOP
;
2025 case CPU_STATE_OPERATING
:
2026 mp_state
.mp_state
= KVM_MP_STATE_OPERATING
;
2028 case CPU_STATE_LOAD
:
2029 mp_state
.mp_state
= KVM_MP_STATE_LOAD
;
2032 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2037 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
2039 trace_kvm_failed_cpu_state_set(CPU(cpu
)->cpu_index
, cpu_state
,
2046 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
2047 uint64_t address
, uint32_t data
)
2049 S390PCIBusDevice
*pbdev
;
2050 uint32_t fid
= data
>> ZPCI_MSI_VEC_BITS
;
2051 uint32_t vec
= data
& ZPCI_MSI_VEC_MASK
;
2053 pbdev
= s390_pci_find_dev_by_fid(fid
);
2055 DPRINTF("add_msi_route no dev\n");
2059 pbdev
->routes
.adapter
.ind_offset
= vec
;
2061 route
->type
= KVM_IRQ_ROUTING_S390_ADAPTER
;
2063 route
->u
.adapter
.summary_addr
= pbdev
->routes
.adapter
.summary_addr
;
2064 route
->u
.adapter
.ind_addr
= pbdev
->routes
.adapter
.ind_addr
;
2065 route
->u
.adapter
.summary_offset
= pbdev
->routes
.adapter
.summary_offset
;
2066 route
->u
.adapter
.ind_offset
= pbdev
->routes
.adapter
.ind_offset
;
2067 route
->u
.adapter
.adapter_id
= pbdev
->routes
.adapter
.adapter_id
;