2 * QEMU S390x KVM implementation
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/error-report.h"
33 #include "qemu/timer.h"
34 #include "sysemu/sysemu.h"
35 #include "sysemu/kvm.h"
38 #include "sysemu/device_tree.h"
39 #include "qapi/qmp/qjson.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_TIMEREVENT 0x288
102 #define DIAG_IPL 0x308
103 #define DIAG_KVM_HYPERCALL 0x500
104 #define DIAG_KVM_BREAKPOINT 0x501
106 #define ICPT_INSTRUCTION 0x04
107 #define ICPT_PROGRAM 0x08
108 #define ICPT_EXT_INT 0x14
109 #define ICPT_WAITPSW 0x1c
110 #define ICPT_SOFT_INTERCEPT 0x24
111 #define ICPT_CPU_STOP 0x28
114 #define NR_LOCAL_IRQS 32
116 * Needs to be big enough to contain max_cpus emergency signals
117 * and in addition NR_LOCAL_IRQS interrupts
119 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
120 (max_cpus + NR_LOCAL_IRQS))
122 static CPUWatchpoint hw_watchpoint;
124 * We don't use a list because this structure is also used to transmit the
125 * hardware breakpoints to the kernel.
127 static struct kvm_hw_breakpoint *hw_breakpoints;
128 static int nb_hw_breakpoints;
130 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
134 static int cap_sync_regs;
135 static int cap_async_pf;
136 static int cap_mem_op;
137 static int cap_s390_irq;
139 static void *legacy_s390_alloc(size_t size, uint64_t *align);
141 static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
143 struct kvm_device_attr attr = {
144 .group = KVM_S390_VM_MEM_CTRL,
145 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
146 .addr = (uint64_t) memory_limit,
149 return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
152 int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
156 struct kvm_device_attr attr = {
157 .group = KVM_S390_VM_MEM_CTRL,
158 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
159 .addr = (uint64_t) &new_limit,
162 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_LIMIT_SIZE)) {
166 rc = kvm_s390_query_mem_limit(s, hw_limit);
169 } else if (*hw_limit < new_limit) {
173 return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
176 void kvm_s390_clear_cmma_callback(void *opaque)
179 KVMState *s = opaque;
180 struct kvm_device_attr attr = {
181 .group = KVM_S390_VM_MEM_CTRL,
182 .attr = KVM_S390_VM_MEM_CLR_CMMA,
185 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
186 trace_kvm_clear_cmma(rc);
189 static void kvm_s390_enable_cmma(KVMState *s)
192 struct kvm_device_attr attr = {
193 .group = KVM_S390_VM_MEM_CTRL,
194 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
197 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_ENABLE_CMMA) ||
198 !kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_CLR_CMMA)) {
202 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
204 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
206 trace_kvm_enable_cmma(rc);
209 static void kvm_s390_set_attr(uint64_t attr)
211 struct kvm_device_attr attribute = {
212 .group = KVM_S390_VM_CRYPTO,
216 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
219 error_report("Failed to set crypto device attribute %lu: %s",
220 attr, strerror(-ret));
224 static void kvm_s390_init_aes_kw(void)
226 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
228 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
230 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
233 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
234 kvm_s390_set_attr(attr);
238 static void kvm_s390_init_dea_kw(void)
240 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
242 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
244 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
247 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
248 kvm_s390_set_attr(attr);
252 static void kvm_s390_init_crypto(void)
254 kvm_s390_init_aes_kw();
255 kvm_s390_init_dea_kw();
258 int kvm_arch_init(MachineState *ms, KVMState *s)
260 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
261 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
262 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
263 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
265 kvm_s390_enable_cmma(s);
267 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
268 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
269 phys_mem_set_alloc(legacy_s390_alloc);
272 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
273 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
274 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
279 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
281 return cpu->cpu_index;
284 int kvm_arch_init_vcpu(CPUState *cs)
286 S390CPU *cpu = S390_CPU(cs);
287 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
288 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
292 void kvm_s390_reset_vcpu(S390CPU *cpu)
294 CPUState *cs = CPU(cpu);
296 /* The initial reset call is needed here to reset in-kernel
297 * vcpu data that we can't access directly from QEMU
298 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
299 * Before this ioctl cpu_synchronize_state() is called in common kvm
301 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
302 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
305 kvm_s390_init_crypto();
308 static int can_sync_regs(CPUState *cs, int regs)
310 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
313 int kvm_arch_put_registers(CPUState *cs, int level)
315 S390CPU *cpu = S390_CPU(cs);
316 CPUS390XState *env = &cpu->env;
317 struct kvm_sregs sregs;
318 struct kvm_regs regs;
319 struct kvm_fpu fpu = {};
323 /* always save the PSW and the GPRS*/
324 cs->kvm_run->psw_addr = env->psw.addr;
325 cs->kvm_run->psw_mask = env->psw.mask;
327 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
328 for (i = 0; i < 16; i++) {
329 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
330 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
333 for (i = 0; i < 16; i++) {
334 regs.gprs[i] = env->regs[i];
336 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, ®s);
342 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
343 for (i = 0; i < 32; i++) {
344 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
345 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
347 cs->kvm_run->s.regs.fpc = env->fpc;
348 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
351 for (i = 0; i < 16; i++) {
352 fpu.fprs[i] = get_freg(env, i)->ll;
356 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
362 /* Do we need to save more than that? */
363 if (level == KVM_PUT_RUNTIME_STATE) {
367 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
368 cs->kvm_run->s.regs.cputm = env->cputm;
369 cs->kvm_run->s.regs.ckc = env->ckc;
370 cs->kvm_run->s.regs.todpr = env->todpr;
371 cs->kvm_run->s.regs.gbea = env->gbea;
372 cs->kvm_run->s.regs.pp = env->pp;
373 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
376 * These ONE_REGS are not protected by a capability. As they are only
377 * necessary for migration we just trace a possible error, but don't
378 * return with an error return code.
380 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
381 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
382 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
383 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
384 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
387 /* pfault parameters */
388 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
389 cs->kvm_run->s.regs.pft = env->pfault_token;
390 cs->kvm_run->s.regs.pfs = env->pfault_select;
391 cs->kvm_run->s.regs.pfc = env->pfault_compare;
392 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
393 } else if (cap_async_pf) {
394 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
398 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
402 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
408 /* access registers and control registers*/
409 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
410 for (i = 0; i < 16; i++) {
411 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
412 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
414 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
415 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
417 for (i = 0; i < 16; i++) {
418 sregs.acrs[i] = env->aregs[i];
419 sregs.crs[i] = env->cregs[i];
421 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
427 /* Finally the prefix */
428 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
429 cs->kvm_run->s.regs.prefix = env->psa;
430 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
432 /* prefix is only supported via sync regs */
437 int kvm_arch_get_registers(CPUState *cs)
439 S390CPU *cpu = S390_CPU(cs);
440 CPUS390XState *env = &cpu->env;
441 struct kvm_sregs sregs;
442 struct kvm_regs regs;
447 env->psw.addr = cs->kvm_run->psw_addr;
448 env->psw.mask = cs->kvm_run->psw_mask;
451 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
452 for (i = 0; i < 16; i++) {
453 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
456 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, ®s);
460 for (i = 0; i < 16; i++) {
461 env->regs[i] = regs.gprs[i];
465 /* The ACRS and CRS */
466 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
467 for (i = 0; i < 16; i++) {
468 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
469 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
472 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
476 for (i = 0; i < 16; i++) {
477 env->aregs[i] = sregs.acrs[i];
478 env->cregs[i] = sregs.crs[i];
482 /* Floating point and vector registers */
483 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
484 for (i = 0; i < 32; i++) {
485 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
486 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
488 env->fpc = cs->kvm_run->s.regs.fpc;
490 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
494 for (i = 0; i < 16; i++) {
495 get_freg(env, i)->ll = fpu.fprs[i];
501 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
502 env->psa = cs->kvm_run->s.regs.prefix;
505 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
506 env->cputm = cs->kvm_run->s.regs.cputm;
507 env->ckc = cs->kvm_run->s.regs.ckc;
508 env->todpr = cs->kvm_run->s.regs.todpr;
509 env->gbea = cs->kvm_run->s.regs.gbea;
510 env->pp = cs->kvm_run->s.regs.pp;
513 * These ONE_REGS are not protected by a capability. As they are only
514 * necessary for migration we just trace a possible error, but don't
515 * return with an error return code.
517 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
518 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
519 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
520 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
521 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
524 /* pfault parameters */
525 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
526 env->pfault_token = cs->kvm_run->s.regs.pft;
527 env->pfault_select = cs->kvm_run->s.regs.pfs;
528 env->pfault_compare = cs->kvm_run->s.regs.pfc;
529 } else if (cap_async_pf) {
530 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
534 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
538 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
547 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
550 struct kvm_device_attr attr = {
551 .group = KVM_S390_VM_TOD,
552 .attr = KVM_S390_VM_TOD_LOW,
553 .addr = (uint64_t)tod_low,
556 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
561 attr.attr = KVM_S390_VM_TOD_HIGH;
562 attr.addr = (uint64_t)tod_high;
563 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
566 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
570 struct kvm_device_attr attr = {
571 .group = KVM_S390_VM_TOD,
572 .attr = KVM_S390_VM_TOD_LOW,
573 .addr = (uint64_t)tod_low,
576 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
581 attr.attr = KVM_S390_VM_TOD_HIGH;
582 attr.addr = (uint64_t)tod_high;
583 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
588 * @addr: the logical start address in guest memory
589 * @ar: the access register number
590 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
591 * @len: length that should be transferred
592 * @is_write: true = write, false = read
593 * Returns: 0 on success, non-zero if an exception or error occurred
595 * Use KVM ioctl to read/write from/to guest memory. An access exception
596 * is injected into the vCPU in case of translation errors.
598 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
599 int len, bool is_write)
601 struct kvm_s390_mem_op mem_op = {
603 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
605 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
606 : KVM_S390_MEMOP_LOGICAL_READ,
607 .buf = (uint64_t)hostbuf,
616 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
619 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
621 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
627 * Legacy layout for s390:
628 * Older S390 KVM requires the topmost vma of the RAM to be
629 * smaller than an system defined value, which is at least 256GB.
630 * Larger systems have larger values. We put the guest between
631 * the end of data segment (system break) and this value. We
632 * use 32GB as a base to have enough room for the system break
633 * to grow. We also have to use MAP parameters that avoid
634 * read-only mapping of guest pages.
636 static void *legacy_s390_alloc(size_t size, uint64_t *align)
640 mem = mmap((void *) 0x800000000ULL, size,
641 PROT_EXEC|PROT_READ|PROT_WRITE,
642 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
643 return mem == MAP_FAILED ? NULL : mem;
646 /* DIAG 501 is used for sw breakpoints */
647 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
649 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
652 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
653 sizeof(diag_501), 0) ||
654 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
655 sizeof(diag_501), 1)) {
661 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
663 uint8_t t[sizeof(diag_501)];
665 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
667 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
669 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
670 sizeof(diag_501), 1)) {
677 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
682 for (n = 0; n < nb_hw_breakpoints; n++) {
683 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
684 (hw_breakpoints[n].len == len || len == -1)) {
685 return &hw_breakpoints[n];
692 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
696 if (find_hw_breakpoint(addr, len, type)) {
700 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
702 if (!hw_breakpoints) {
703 nb_hw_breakpoints = 0;
704 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
707 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
710 if (!hw_breakpoints) {
711 nb_hw_breakpoints = 0;
715 hw_breakpoints[nb_hw_breakpoints].addr = addr;
716 hw_breakpoints[nb_hw_breakpoints].len = len;
717 hw_breakpoints[nb_hw_breakpoints].type = type;
724 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
725 target_ulong len, int type)
728 case GDB_BREAKPOINT_HW:
731 case GDB_WATCHPOINT_WRITE:
735 type = KVM_HW_WP_WRITE;
740 return insert_hw_breakpoint(addr, len, type);
743 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
744 target_ulong len, int type)
747 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
754 if (nb_hw_breakpoints > 0) {
756 * In order to trim the array, move the last element to the position to
757 * be removed - if necessary.
759 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
760 *bp = hw_breakpoints[nb_hw_breakpoints];
762 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
764 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
766 g_free(hw_breakpoints);
767 hw_breakpoints = NULL;
773 void kvm_arch_remove_all_hw_breakpoints(void)
775 nb_hw_breakpoints = 0;
776 g_free(hw_breakpoints);
777 hw_breakpoints = NULL;
780 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
784 if (nb_hw_breakpoints > 0) {
785 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
786 dbg->arch.hw_bp = hw_breakpoints;
788 for (i = 0; i < nb_hw_breakpoints; ++i) {
789 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
790 hw_breakpoints[i].addr);
792 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
794 dbg->arch.nr_hw_bp = 0;
795 dbg->arch.hw_bp = NULL;
799 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
803 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
805 return MEMTXATTRS_UNSPECIFIED;
808 int kvm_arch_process_async_events(CPUState *cs)
813 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
814 struct kvm_s390_interrupt *interrupt)
818 interrupt->type = irq->type;
820 case KVM_S390_INT_VIRTIO:
821 interrupt->parm = irq->u.ext.ext_params;
823 case KVM_S390_INT_PFAULT_INIT:
824 case KVM_S390_INT_PFAULT_DONE:
825 interrupt->parm64 = irq->u.ext.ext_params2;
827 case KVM_S390_PROGRAM_INT:
828 interrupt->parm = irq->u.pgm.code;
830 case KVM_S390_SIGP_SET_PREFIX:
831 interrupt->parm = irq->u.prefix.address;
833 case KVM_S390_INT_SERVICE:
834 interrupt->parm = irq->u.ext.ext_params;
837 interrupt->parm = irq->u.mchk.cr14;
838 interrupt->parm64 = irq->u.mchk.mcic;
840 case KVM_S390_INT_EXTERNAL_CALL:
841 interrupt->parm = irq->u.extcall.code;
843 case KVM_S390_INT_EMERGENCY:
844 interrupt->parm = irq->u.emerg.code;
846 case KVM_S390_SIGP_STOP:
847 case KVM_S390_RESTART:
848 break; /* These types have no parameters */
849 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
850 interrupt->parm = irq->u.io.subchannel_id << 16;
851 interrupt->parm |= irq->u.io.subchannel_nr;
852 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
853 interrupt->parm64 |= irq->u.io.io_int_word;
862 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
864 struct kvm_s390_interrupt kvmint = {};
867 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
869 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
873 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
875 fprintf(stderr, "KVM failed to inject interrupt\n");
880 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
882 CPUState *cs = CPU(cpu);
886 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
890 error_report("KVM failed to inject interrupt %llx", irq->type);
894 inject_vcpu_irq_legacy(cs, irq);
897 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
899 struct kvm_s390_interrupt kvmint = {};
902 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
904 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
908 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
910 fprintf(stderr, "KVM failed to inject interrupt\n");
915 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
917 static bool use_flic = true;
921 r = kvm_s390_inject_flic(irq);
929 __kvm_s390_floating_interrupt(irq);
932 void kvm_s390_virtio_irq(int config_change, uint64_t token)
934 struct kvm_s390_irq irq = {
935 .type = KVM_S390_INT_VIRTIO,
936 .u.ext.ext_params = config_change,
937 .u.ext.ext_params2 = token,
940 kvm_s390_floating_interrupt(&irq);
943 void kvm_s390_service_interrupt(uint32_t parm)
945 struct kvm_s390_irq irq = {
946 .type = KVM_S390_INT_SERVICE,
947 .u.ext.ext_params = parm,
950 kvm_s390_floating_interrupt(&irq);
953 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
955 struct kvm_s390_irq irq = {
956 .type = KVM_S390_PROGRAM_INT,
960 kvm_s390_vcpu_interrupt(cpu, &irq);
963 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
965 struct kvm_s390_irq irq = {
966 .type = KVM_S390_PROGRAM_INT,
968 .u.pgm.trans_exc_code = te_code,
969 .u.pgm.exc_access_id = te_code & 3,
972 kvm_s390_vcpu_interrupt(cpu, &irq);
975 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
978 CPUS390XState *env = &cpu->env;
983 cpu_synchronize_state(CPU(cpu));
984 sccb = env->regs[ipbh0 & 0xf];
985 code = env->regs[(ipbh0 & 0xf0) >> 4];
987 r = sclp_service_call(env, sccb, code);
989 enter_pgmcheck(cpu, -r);
997 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
999 CPUS390XState *env = &cpu->env;
1001 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1003 cpu_synchronize_state(CPU(cpu));
1007 ioinst_handle_xsch(cpu, env->regs[1]);
1010 ioinst_handle_csch(cpu, env->regs[1]);
1013 ioinst_handle_hsch(cpu, env->regs[1]);
1016 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1019 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1022 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1025 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1028 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1029 fprintf(stderr, "Spurious tsch intercept\n");
1032 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1035 /* This should have been handled by kvm already. */
1036 fprintf(stderr, "Spurious tpi intercept\n");
1039 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1040 run->s390_sieic.ipb);
1043 ioinst_handle_rsch(cpu, env->regs[1]);
1046 ioinst_handle_rchp(cpu, env->regs[1]);
1049 /* We do not provide this instruction, it is suppressed. */
1052 ioinst_handle_sal(cpu, env->regs[1]);
1055 /* Not provided, set CC = 3 for subchannel not operational */
1058 case PRIV_B2_SCLP_CALL:
1059 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1063 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1070 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1073 CPUS390XState *env = &cpu->env;
1074 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1075 uint32_t base2 = run->s390_sieic.ipb >> 28;
1076 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1077 ((run->s390_sieic.ipb & 0xff00) << 4);
1079 if (disp2 & 0x80000) {
1080 disp2 += 0xfff00000;
1086 return (base2 ? env->regs[base2] : 0) +
1087 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1090 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1093 CPUS390XState *env = &cpu->env;
1094 uint32_t base2 = run->s390_sieic.ipb >> 28;
1095 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1096 ((run->s390_sieic.ipb & 0xff00) << 4);
1098 if (disp2 & 0x80000) {
1099 disp2 += 0xfff00000;
1105 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1108 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1110 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1112 return clp_service_call(cpu, r2);
1115 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1117 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1118 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1120 return pcilg_service_call(cpu, r1, r2);
1123 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1125 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1126 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1128 return pcistg_service_call(cpu, r1, r2);
1131 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1133 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1137 cpu_synchronize_state(CPU(cpu));
1138 fiba = get_base_disp_rxy(cpu, run, &ar);
1140 return stpcifc_service_call(cpu, r1, fiba, ar);
1143 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1149 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1151 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1152 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1154 return rpcit_service_call(cpu, r1, r2);
1157 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1159 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1160 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1164 cpu_synchronize_state(CPU(cpu));
1165 gaddr = get_base_disp_rsy(cpu, run, &ar);
1167 return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1170 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1172 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1176 cpu_synchronize_state(CPU(cpu));
1177 fiba = get_base_disp_rxy(cpu, run, &ar);
1179 return mpcifc_service_call(cpu, r1, fiba, ar);
1182 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1188 r = kvm_clp_service_call(cpu, run);
1190 case PRIV_B9_PCISTG:
1191 r = kvm_pcistg_service_call(cpu, run);
1194 r = kvm_pcilg_service_call(cpu, run);
1197 r = kvm_rpcit_service_call(cpu, run);
1200 /* just inject exception */
1205 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1212 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1217 case PRIV_EB_PCISTB:
1218 r = kvm_pcistb_service_call(cpu, run);
1221 r = kvm_sic_service_call(cpu, run);
1224 /* just inject exception */
1229 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1236 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1241 case PRIV_E3_MPCIFC:
1242 r = kvm_mpcifc_service_call(cpu, run);
1244 case PRIV_E3_STPCIFC:
1245 r = kvm_stpcifc_service_call(cpu, run);
1249 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1256 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1258 CPUS390XState *env = &cpu->env;
1261 cpu_synchronize_state(CPU(cpu));
1262 ret = s390_virtio_hypercall(env);
1263 if (ret == -EINVAL) {
1264 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1271 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1276 cpu_synchronize_state(CPU(cpu));
1277 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1278 r3 = run->s390_sieic.ipa & 0x000f;
1279 rc = handle_diag_288(&cpu->env, r1, r3);
1281 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1285 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1289 cpu_synchronize_state(CPU(cpu));
1290 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1291 r3 = run->s390_sieic.ipa & 0x000f;
1292 handle_diag_308(&cpu->env, r1, r3);
1295 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1297 CPUS390XState *env = &cpu->env;
1300 cpu_synchronize_state(CPU(cpu));
1302 pc = env->psw.addr - 4;
1303 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1311 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1313 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1319 * For any diagnose call we support, bits 48-63 of the resulting
1320 * address specify the function code; the remainder is ignored.
1322 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1323 switch (func_code) {
1324 case DIAG_TIMEREVENT:
1325 kvm_handle_diag_288(cpu, run);
1328 kvm_handle_diag_308(cpu, run);
1330 case DIAG_KVM_HYPERCALL:
1331 r = handle_hypercall(cpu, run);
1333 case DIAG_KVM_BREAKPOINT:
1334 r = handle_sw_breakpoint(cpu, run);
1337 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1338 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1345 typedef struct SigpInfo {
1349 uint64_t *status_reg;
1352 static void set_sigp_status(SigpInfo *si, uint64_t status)
1354 *si->status_reg &= 0xffffffff00000000ULL;
1355 *si->status_reg |= status;
1356 si->cc = SIGP_CC_STATUS_STORED;
1359 static void sigp_start(void *arg)
1363 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1364 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1368 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1369 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1372 static void sigp_stop(void *arg)
1375 struct kvm_s390_irq irq = {
1376 .type = KVM_S390_SIGP_STOP,
1379 if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1380 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1384 /* disabled wait - sleeping in user space */
1385 if (CPU(si->cpu)->halted) {
1386 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1388 /* execute the stop function */
1389 si->cpu->env.sigp_order = SIGP_STOP;
1390 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1392 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1395 #define ADTL_SAVE_AREA_SIZE 1024
1396 static int kvm_s390_store_adtl_status(S390CPU *cpu, hwaddr addr)
1399 hwaddr len = ADTL_SAVE_AREA_SIZE;
1401 mem = cpu_physical_memory_map(addr, &len, 1);
1405 if (len != ADTL_SAVE_AREA_SIZE) {
1406 cpu_physical_memory_unmap(mem, len, 1, 0);
1410 memcpy(mem, &cpu->env.vregs, 512);
1412 cpu_physical_memory_unmap(mem, len, 1, len);
1417 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1418 #define SAVE_AREA_SIZE 512
1419 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1421 static const uint8_t ar_id = 1;
1422 uint64_t ckc = cpu->env.ckc >> 8;
1425 hwaddr len = SAVE_AREA_SIZE;
1427 mem = cpu_physical_memory_map(addr, &len, 1);
1431 if (len != SAVE_AREA_SIZE) {
1432 cpu_physical_memory_unmap(mem, len, 1, 0);
1437 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1439 for (i = 0; i < 16; ++i) {
1440 *((uint64 *)mem + i) = get_freg(&cpu->env, i)->ll;
1442 memcpy(mem + 128, &cpu->env.regs, 128);
1443 memcpy(mem + 256, &cpu->env.psw, 16);
1444 memcpy(mem + 280, &cpu->env.psa, 4);
1445 memcpy(mem + 284, &cpu->env.fpc, 4);
1446 memcpy(mem + 292, &cpu->env.todpr, 4);
1447 memcpy(mem + 296, &cpu->env.cputm, 8);
1448 memcpy(mem + 304, &ckc, 8);
1449 memcpy(mem + 320, &cpu->env.aregs, 64);
1450 memcpy(mem + 384, &cpu->env.cregs, 128);
1452 cpu_physical_memory_unmap(mem, len, 1, len);
1457 static void sigp_stop_and_store_status(void *arg)
1460 struct kvm_s390_irq irq = {
1461 .type = KVM_S390_SIGP_STOP,
1464 /* disabled wait - sleeping in user space */
1465 if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1466 CPU(si->cpu)->halted) {
1467 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1470 switch (s390_cpu_get_state(si->cpu)) {
1471 case CPU_STATE_OPERATING:
1472 si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1473 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1474 /* store will be performed when handling the stop intercept */
1476 case CPU_STATE_STOPPED:
1477 /* already stopped, just store the status */
1478 cpu_synchronize_state(CPU(si->cpu));
1479 kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1482 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1485 static void sigp_store_status_at_address(void *arg)
1488 uint32_t address = si->param & 0x7ffffe00u;
1490 /* cpu has to be stopped */
1491 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1492 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1496 cpu_synchronize_state(CPU(si->cpu));
1498 if (kvm_s390_store_status(si->cpu, address, false)) {
1499 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1502 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1505 static void sigp_store_adtl_status(void *arg)
1509 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
1510 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1514 /* cpu has to be stopped */
1515 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1516 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1520 /* parameter must be aligned to 1024-byte boundary */
1521 if (si->param & 0x3ff) {
1522 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1526 cpu_synchronize_state(CPU(si->cpu));
1528 if (kvm_s390_store_adtl_status(si->cpu, si->param)) {
1529 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1532 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1535 static void sigp_restart(void *arg)
1538 struct kvm_s390_irq irq = {
1539 .type = KVM_S390_RESTART,
1542 switch (s390_cpu_get_state(si->cpu)) {
1543 case CPU_STATE_STOPPED:
1544 /* the restart irq has to be delivered prior to any other pending irq */
1545 cpu_synchronize_state(CPU(si->cpu));
1546 do_restart_interrupt(&si->cpu->env);
1547 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1549 case CPU_STATE_OPERATING:
1550 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1553 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1556 int kvm_s390_cpu_restart(S390CPU *cpu)
1562 run_on_cpu(CPU(cpu), sigp_restart, &si);
1563 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1567 static void sigp_initial_cpu_reset(void *arg)
1570 CPUState *cs = CPU(si->cpu);
1571 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1573 cpu_synchronize_state(cs);
1574 scc->initial_cpu_reset(cs);
1575 cpu_synchronize_post_reset(cs);
1576 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1579 static void sigp_cpu_reset(void *arg)
1582 CPUState *cs = CPU(si->cpu);
1583 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1585 cpu_synchronize_state(cs);
1587 cpu_synchronize_post_reset(cs);
1588 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1591 static void sigp_set_prefix(void *arg)
1594 uint32_t addr = si->param & 0x7fffe000u;
1596 cpu_synchronize_state(CPU(si->cpu));
1598 if (!address_space_access_valid(&address_space_memory, addr,
1599 sizeof(struct LowCore), false)) {
1600 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1604 /* cpu has to be stopped */
1605 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1606 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1610 si->cpu->env.psa = addr;
1611 cpu_synchronize_post_init(CPU(si->cpu));
1612 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1615 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1616 uint64_t param, uint64_t *status_reg)
1621 .status_reg = status_reg,
1624 /* cpu available? */
1625 if (dst_cpu == NULL) {
1626 return SIGP_CC_NOT_OPERATIONAL;
1629 /* only resets can break pending orders */
1630 if (dst_cpu->env.sigp_order != 0 &&
1631 order != SIGP_CPU_RESET &&
1632 order != SIGP_INITIAL_CPU_RESET) {
1633 return SIGP_CC_BUSY;
1638 run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1641 run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1644 run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1646 case SIGP_STOP_STORE_STATUS:
1647 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1649 case SIGP_STORE_STATUS_ADDR:
1650 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1652 case SIGP_STORE_ADTL_STATUS:
1653 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, &si);
1655 case SIGP_SET_PREFIX:
1656 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1658 case SIGP_INITIAL_CPU_RESET:
1659 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1661 case SIGP_CPU_RESET:
1662 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1665 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1666 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1672 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1673 uint64_t *status_reg)
1678 /* due to the BQL, we are the only active cpu */
1679 CPU_FOREACH(cur_cs) {
1680 cur_cpu = S390_CPU(cur_cs);
1681 if (cur_cpu->env.sigp_order != 0) {
1682 return SIGP_CC_BUSY;
1684 cpu_synchronize_state(cur_cs);
1685 /* all but the current one have to be stopped */
1686 if (cur_cpu != cpu &&
1687 s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1688 *status_reg &= 0xffffffff00000000ULL;
1689 *status_reg |= SIGP_STAT_INCORRECT_STATE;
1690 return SIGP_CC_STATUS_STORED;
1694 switch (param & 0xff) {
1695 case SIGP_MODE_ESA_S390:
1697 return SIGP_CC_NOT_OPERATIONAL;
1698 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1699 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1700 CPU_FOREACH(cur_cs) {
1701 cur_cpu = S390_CPU(cur_cs);
1702 cur_cpu->env.pfault_token = -1UL;
1706 *status_reg &= 0xffffffff00000000ULL;
1707 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1708 return SIGP_CC_STATUS_STORED;
1711 return SIGP_CC_ORDER_CODE_ACCEPTED;
1714 #define SIGP_ORDER_MASK 0x000000ff
1716 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1718 CPUS390XState *env = &cpu->env;
1719 const uint8_t r1 = ipa1 >> 4;
1720 const uint8_t r3 = ipa1 & 0x0f;
1723 uint64_t *status_reg;
1725 S390CPU *dst_cpu = NULL;
1727 cpu_synchronize_state(CPU(cpu));
1729 /* get order code */
1730 order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1732 status_reg = &env->regs[r1];
1733 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1737 ret = sigp_set_architecture(cpu, param, status_reg);
1740 /* all other sigp orders target a single vcpu */
1741 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1742 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1745 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1746 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1756 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1758 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1759 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1762 DPRINTF("handle_instruction 0x%x 0x%x\n",
1763 run->s390_sieic.ipa, run->s390_sieic.ipb);
1766 r = handle_b2(cpu, run, ipa1);
1769 r = handle_b9(cpu, run, ipa1);
1772 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1775 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1778 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1781 r = handle_sigp(cpu, run, ipa1);
1787 enter_pgmcheck(cpu, 0x0001);
1793 static bool is_special_wait_psw(CPUState *cs)
1795 /* signal quiesce */
1796 return cs->kvm_run->psw_addr == 0xfffUL;
1799 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1801 CPUState *cs = CPU(cpu);
1803 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1804 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1805 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1807 qemu_system_guest_panicked();
1810 static int handle_intercept(S390CPU *cpu)
1812 CPUState *cs = CPU(cpu);
1813 struct kvm_run *run = cs->kvm_run;
1814 int icpt_code = run->s390_sieic.icptcode;
1817 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1818 (long)cs->kvm_run->psw_addr);
1819 switch (icpt_code) {
1820 case ICPT_INSTRUCTION:
1821 r = handle_instruction(cpu, run);
1824 unmanageable_intercept(cpu, "program interrupt",
1825 offsetof(LowCore, program_new_psw));
1829 unmanageable_intercept(cpu, "external interrupt",
1830 offsetof(LowCore, external_new_psw));
1834 /* disabled wait, since enabled wait is handled in kernel */
1835 cpu_synchronize_state(cs);
1836 if (s390_cpu_halt(cpu) == 0) {
1837 if (is_special_wait_psw(cs)) {
1838 qemu_system_shutdown_request();
1840 qemu_system_guest_panicked();
1846 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1847 qemu_system_shutdown_request();
1849 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1850 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1853 cpu->env.sigp_order = 0;
1856 case ICPT_SOFT_INTERCEPT:
1857 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1861 fprintf(stderr, "KVM unimplemented icpt IO\n");
1865 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1873 static int handle_tsch(S390CPU *cpu)
1875 CPUState *cs = CPU(cpu);
1876 struct kvm_run *run = cs->kvm_run;
1879 cpu_synchronize_state(cs);
1881 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1885 * If an I/O interrupt had been dequeued, we have to reinject it.
1887 if (run->s390_tsch.dequeued) {
1888 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1889 run->s390_tsch.subchannel_nr,
1890 run->s390_tsch.io_int_parm,
1891 run->s390_tsch.io_int_word);
1898 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1900 struct sysib_322 sysib;
1903 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1906 /* Shift the stack of Extended Names to prepare for our own data */
1907 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1908 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1909 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1910 * assumed it's not capable of managing Extended Names for lower levels.
1912 for (del = 1; del < sysib.count; del++) {
1913 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1917 if (del < sysib.count) {
1918 memset(sysib.ext_names[del], 0,
1919 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1921 /* Insert short machine name in EBCDIC, padded with blanks */
1923 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1924 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1925 strlen(qemu_name)));
1927 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1928 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1929 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1930 * considered by s390 as not capable of providing any Extended Name.
1931 * Therefore if no name was specified on qemu invocation, we go with the
1932 * same "KVMguest" default, which KVM has filled into short name field.
1935 strncpy((char *)sysib.ext_names[0], qemu_name,
1936 sizeof(sysib.ext_names[0]));
1938 strcpy((char *)sysib.ext_names[0], "KVMguest");
1941 memcpy(sysib.vm[0].uuid, qemu_uuid, sizeof(sysib.vm[0].uuid));
1943 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1946 static int handle_stsi(S390CPU *cpu)
1948 CPUState *cs = CPU(cpu);
1949 struct kvm_run *run = cs->kvm_run;
1951 switch (run->s390_stsi.fc) {
1953 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1956 /* Only sysib 3.2.2 needs post-handling for now. */
1957 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1964 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1966 CPUState *cs = CPU(cpu);
1967 struct kvm_run *run = cs->kvm_run;
1970 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1972 switch (arch_info->type) {
1973 case KVM_HW_WP_WRITE:
1974 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1975 cs->watchpoint_hit = &hw_watchpoint;
1976 hw_watchpoint.vaddr = arch_info->addr;
1977 hw_watchpoint.flags = BP_MEM_WRITE;
1982 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1986 case KVM_SINGLESTEP:
1987 if (cs->singlestep_enabled) {
1998 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
2000 S390CPU *cpu = S390_CPU(cs);
2003 qemu_mutex_lock_iothread();
2005 switch (run->exit_reason) {
2006 case KVM_EXIT_S390_SIEIC:
2007 ret = handle_intercept(cpu);
2009 case KVM_EXIT_S390_RESET:
2010 s390_reipl_request();
2012 case KVM_EXIT_S390_TSCH:
2013 ret = handle_tsch(cpu);
2015 case KVM_EXIT_S390_STSI:
2016 ret = handle_stsi(cpu);
2018 case KVM_EXIT_DEBUG:
2019 ret = kvm_arch_handle_debug_exit(cpu);
2022 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2025 qemu_mutex_unlock_iothread();
2028 ret = EXCP_INTERRUPT;
2033 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2038 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
2043 int kvm_arch_on_sigbus(int code, void *addr)
2048 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2049 uint16_t subchannel_nr, uint32_t io_int_parm,
2050 uint32_t io_int_word)
2052 struct kvm_s390_irq irq = {
2053 .u.io.subchannel_id = subchannel_id,
2054 .u.io.subchannel_nr = subchannel_nr,
2055 .u.io.io_int_parm = io_int_parm,
2056 .u.io.io_int_word = io_int_word,
2059 if (io_int_word & IO_INT_WORD_AI) {
2060 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2062 irq.type = ((subchannel_id & 0xff00) << 24) |
2063 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
2065 kvm_s390_floating_interrupt(&irq);
2068 static uint64_t build_channel_report_mcic(void)
2072 /* subclass: indicate channel report pending */
2074 /* subclass modifiers: none */
2075 /* storage errors: none */
2076 /* validity bits: no damage */
2077 MCIC_VB_WP | MCIC_VB_MS | MCIC_VB_PM | MCIC_VB_IA | MCIC_VB_FP |
2078 MCIC_VB_GR | MCIC_VB_CR | MCIC_VB_ST | MCIC_VB_AR | MCIC_VB_PR |
2079 MCIC_VB_FC | MCIC_VB_CT | MCIC_VB_CC;
2080 if (kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
2086 void kvm_s390_crw_mchk(void)
2088 struct kvm_s390_irq irq = {
2089 .type = KVM_S390_MCHK,
2090 .u.mchk.cr14 = 1 << 28,
2091 .u.mchk.mcic = build_channel_report_mcic(),
2093 kvm_s390_floating_interrupt(&irq);
2096 void kvm_s390_enable_css_support(S390CPU *cpu)
2100 /* Activate host kernel channel subsystem support. */
2101 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2105 void kvm_arch_init_irq_routing(KVMState *s)
2108 * Note that while irqchip capabilities generally imply that cpustates
2109 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2110 * have to override the common code kvm_halt_in_kernel_allowed setting.
2112 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2113 kvm_gsi_routing_allowed = true;
2114 kvm_halt_in_kernel_allowed = false;
2118 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2119 int vq, bool assign)
2121 struct kvm_ioeventfd kick = {
2122 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2123 KVM_IOEVENTFD_FLAG_DATAMATCH,
2124 .fd = event_notifier_get_fd(notifier),
2129 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2133 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2135 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2138 int kvm_s390_get_memslot_count(KVMState *s)
2140 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
2143 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2145 struct kvm_mp_state mp_state = {};
2148 /* the kvm part might not have been initialized yet */
2149 if (CPU(cpu)->kvm_state == NULL) {
2153 switch (cpu_state) {
2154 case CPU_STATE_STOPPED:
2155 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2157 case CPU_STATE_CHECK_STOP:
2158 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2160 case CPU_STATE_OPERATING:
2161 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2163 case CPU_STATE_LOAD:
2164 mp_state.mp_state = KVM_MP_STATE_LOAD;
2167 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2172 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2174 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2181 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2183 struct kvm_s390_irq_state irq_state;
2184 CPUState *cs = CPU(cpu);
2187 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2191 irq_state.buf = (uint64_t) cpu->irqstate;
2192 irq_state.len = VCPU_IRQ_BUF_SIZE;
2194 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2196 cpu->irqstate_saved_size = 0;
2197 error_report("Migration of interrupt state failed");
2201 cpu->irqstate_saved_size = bytes;
2204 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2206 CPUState *cs = CPU(cpu);
2207 struct kvm_s390_irq_state irq_state;
2210 if (cpu->irqstate_saved_size == 0) {
2214 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2218 irq_state.buf = (uint64_t) cpu->irqstate;
2219 irq_state.len = cpu->irqstate_saved_size;
2221 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2223 error_report("Setting interrupt state failed %d", r);
2228 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2229 uint64_t address, uint32_t data, PCIDevice *dev)
2231 S390PCIBusDevice *pbdev;
2232 uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
2233 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2235 pbdev = s390_pci_find_dev_by_fid(fid);
2237 DPRINTF("add_msi_route no dev\n");
2241 pbdev->routes.adapter.ind_offset = vec;
2243 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2245 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2246 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2247 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2248 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2249 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2253 int kvm_arch_msi_data_to_gsi(uint32_t data)