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/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 #define NR_LOCAL_IRQS 32
115 * Needs to be big enough to contain max_cpus emergency signals
116 * and in addition NR_LOCAL_IRQS interrupts
118 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
119 (max_cpus + NR_LOCAL_IRQS))
121 static CPUWatchpoint hw_watchpoint;
123 * We don't use a list because this structure is also used to transmit the
124 * hardware breakpoints to the kernel.
126 static struct kvm_hw_breakpoint *hw_breakpoints;
127 static int nb_hw_breakpoints;
129 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
133 static int cap_sync_regs;
134 static int cap_async_pf;
135 static int cap_mem_op;
136 static int cap_s390_irq;
138 static void *legacy_s390_alloc(size_t size, uint64_t *align);
140 static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
142 struct kvm_device_attr attr = {
143 .group = KVM_S390_VM_MEM_CTRL,
144 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
145 .addr = (uint64_t) memory_limit,
148 return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
151 int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
155 struct kvm_device_attr attr = {
156 .group = KVM_S390_VM_MEM_CTRL,
157 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
158 .addr = (uint64_t) &new_limit,
161 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_LIMIT_SIZE)) {
165 rc = kvm_s390_query_mem_limit(s, hw_limit);
168 } else if (*hw_limit < new_limit) {
172 return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
175 void kvm_s390_clear_cmma_callback(void *opaque)
178 KVMState *s = opaque;
179 struct kvm_device_attr attr = {
180 .group = KVM_S390_VM_MEM_CTRL,
181 .attr = KVM_S390_VM_MEM_CLR_CMMA,
184 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
185 trace_kvm_clear_cmma(rc);
188 static void kvm_s390_enable_cmma(KVMState *s)
191 struct kvm_device_attr attr = {
192 .group = KVM_S390_VM_MEM_CTRL,
193 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
196 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_ENABLE_CMMA) ||
197 !kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_CLR_CMMA)) {
201 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
203 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
205 trace_kvm_enable_cmma(rc);
208 static void kvm_s390_set_attr(uint64_t attr)
210 struct kvm_device_attr attribute = {
211 .group = KVM_S390_VM_CRYPTO,
215 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
218 error_report("Failed to set crypto device attribute %lu: %s",
219 attr, strerror(-ret));
223 static void kvm_s390_init_aes_kw(void)
225 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
227 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
229 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
232 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
233 kvm_s390_set_attr(attr);
237 static void kvm_s390_init_dea_kw(void)
239 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
241 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
243 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
246 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
247 kvm_s390_set_attr(attr);
251 static void kvm_s390_init_crypto(void)
253 kvm_s390_init_aes_kw();
254 kvm_s390_init_dea_kw();
257 int kvm_arch_init(MachineState *ms, KVMState *s)
259 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
260 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
261 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
262 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
264 kvm_s390_enable_cmma(s);
266 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
267 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
268 phys_mem_set_alloc(legacy_s390_alloc);
271 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
272 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
273 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
278 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
280 return cpu->cpu_index;
283 int kvm_arch_init_vcpu(CPUState *cs)
285 S390CPU *cpu = S390_CPU(cs);
286 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
287 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
291 void kvm_s390_reset_vcpu(S390CPU *cpu)
293 CPUState *cs = CPU(cpu);
295 /* The initial reset call is needed here to reset in-kernel
296 * vcpu data that we can't access directly from QEMU
297 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
298 * Before this ioctl cpu_synchronize_state() is called in common kvm
300 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
301 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
304 kvm_s390_init_crypto();
307 static int can_sync_regs(CPUState *cs, int regs)
309 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
312 int kvm_arch_put_registers(CPUState *cs, int level)
314 S390CPU *cpu = S390_CPU(cs);
315 CPUS390XState *env = &cpu->env;
316 struct kvm_sregs sregs;
317 struct kvm_regs regs;
318 struct kvm_fpu fpu = {};
322 /* always save the PSW and the GPRS*/
323 cs->kvm_run->psw_addr = env->psw.addr;
324 cs->kvm_run->psw_mask = env->psw.mask;
326 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
327 for (i = 0; i < 16; i++) {
328 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
329 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
332 for (i = 0; i < 16; i++) {
333 regs.gprs[i] = env->regs[i];
335 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, ®s);
341 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
342 for (i = 0; i < 32; i++) {
343 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
344 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
346 cs->kvm_run->s.regs.fpc = env->fpc;
347 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
350 for (i = 0; i < 16; i++) {
351 fpu.fprs[i] = get_freg(env, i)->ll;
355 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
361 /* Do we need to save more than that? */
362 if (level == KVM_PUT_RUNTIME_STATE) {
366 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
367 cs->kvm_run->s.regs.cputm = env->cputm;
368 cs->kvm_run->s.regs.ckc = env->ckc;
369 cs->kvm_run->s.regs.todpr = env->todpr;
370 cs->kvm_run->s.regs.gbea = env->gbea;
371 cs->kvm_run->s.regs.pp = env->pp;
372 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
375 * These ONE_REGS are not protected by a capability. As they are only
376 * necessary for migration we just trace a possible error, but don't
377 * return with an error return code.
379 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
380 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
381 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
382 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
383 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
386 /* pfault parameters */
387 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
388 cs->kvm_run->s.regs.pft = env->pfault_token;
389 cs->kvm_run->s.regs.pfs = env->pfault_select;
390 cs->kvm_run->s.regs.pfc = env->pfault_compare;
391 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
392 } else if (cap_async_pf) {
393 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
397 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
401 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
407 /* access registers and control registers*/
408 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
409 for (i = 0; i < 16; i++) {
410 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
411 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
413 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
414 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
416 for (i = 0; i < 16; i++) {
417 sregs.acrs[i] = env->aregs[i];
418 sregs.crs[i] = env->cregs[i];
420 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
426 /* Finally the prefix */
427 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
428 cs->kvm_run->s.regs.prefix = env->psa;
429 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
431 /* prefix is only supported via sync regs */
436 int kvm_arch_get_registers(CPUState *cs)
438 S390CPU *cpu = S390_CPU(cs);
439 CPUS390XState *env = &cpu->env;
440 struct kvm_sregs sregs;
441 struct kvm_regs regs;
446 env->psw.addr = cs->kvm_run->psw_addr;
447 env->psw.mask = cs->kvm_run->psw_mask;
450 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
451 for (i = 0; i < 16; i++) {
452 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
455 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, ®s);
459 for (i = 0; i < 16; i++) {
460 env->regs[i] = regs.gprs[i];
464 /* The ACRS and CRS */
465 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
466 for (i = 0; i < 16; i++) {
467 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
468 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
471 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
475 for (i = 0; i < 16; i++) {
476 env->aregs[i] = sregs.acrs[i];
477 env->cregs[i] = sregs.crs[i];
481 /* Floating point and vector registers */
482 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
483 for (i = 0; i < 32; i++) {
484 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
485 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
487 env->fpc = cs->kvm_run->s.regs.fpc;
489 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
493 for (i = 0; i < 16; i++) {
494 get_freg(env, i)->ll = fpu.fprs[i];
500 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
501 env->psa = cs->kvm_run->s.regs.prefix;
504 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
505 env->cputm = cs->kvm_run->s.regs.cputm;
506 env->ckc = cs->kvm_run->s.regs.ckc;
507 env->todpr = cs->kvm_run->s.regs.todpr;
508 env->gbea = cs->kvm_run->s.regs.gbea;
509 env->pp = cs->kvm_run->s.regs.pp;
512 * These ONE_REGS are not protected by a capability. As they are only
513 * necessary for migration we just trace a possible error, but don't
514 * return with an error return code.
516 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
517 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
518 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
519 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
520 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
523 /* pfault parameters */
524 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
525 env->pfault_token = cs->kvm_run->s.regs.pft;
526 env->pfault_select = cs->kvm_run->s.regs.pfs;
527 env->pfault_compare = cs->kvm_run->s.regs.pfc;
528 } else if (cap_async_pf) {
529 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
533 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
537 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
546 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
549 struct kvm_device_attr attr = {
550 .group = KVM_S390_VM_TOD,
551 .attr = KVM_S390_VM_TOD_LOW,
552 .addr = (uint64_t)tod_low,
555 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
560 attr.attr = KVM_S390_VM_TOD_HIGH;
561 attr.addr = (uint64_t)tod_high;
562 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
565 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
569 struct kvm_device_attr attr = {
570 .group = KVM_S390_VM_TOD,
571 .attr = KVM_S390_VM_TOD_LOW,
572 .addr = (uint64_t)tod_low,
575 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
580 attr.attr = KVM_S390_VM_TOD_HIGH;
581 attr.addr = (uint64_t)tod_high;
582 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
587 * @addr: the logical start address in guest memory
588 * @ar: the access register number
589 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
590 * @len: length that should be transfered
591 * @is_write: true = write, false = read
592 * Returns: 0 on success, non-zero if an exception or error occured
594 * Use KVM ioctl to read/write from/to guest memory. An access exception
595 * is injected into the vCPU in case of translation errors.
597 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
598 int len, bool is_write)
600 struct kvm_s390_mem_op mem_op = {
602 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
604 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
605 : KVM_S390_MEMOP_LOGICAL_READ,
606 .buf = (uint64_t)hostbuf,
615 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
618 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
620 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
626 * Legacy layout for s390:
627 * Older S390 KVM requires the topmost vma of the RAM to be
628 * smaller than an system defined value, which is at least 256GB.
629 * Larger systems have larger values. We put the guest between
630 * the end of data segment (system break) and this value. We
631 * use 32GB as a base to have enough room for the system break
632 * to grow. We also have to use MAP parameters that avoid
633 * read-only mapping of guest pages.
635 static void *legacy_s390_alloc(size_t size, uint64_t *align)
639 mem = mmap((void *) 0x800000000ULL, size,
640 PROT_EXEC|PROT_READ|PROT_WRITE,
641 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
642 return mem == MAP_FAILED ? NULL : mem;
645 /* DIAG 501 is used for sw breakpoints */
646 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
648 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
651 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
652 sizeof(diag_501), 0) ||
653 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
654 sizeof(diag_501), 1)) {
660 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
662 uint8_t t[sizeof(diag_501)];
664 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
666 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
668 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
669 sizeof(diag_501), 1)) {
676 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
681 for (n = 0; n < nb_hw_breakpoints; n++) {
682 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
683 (hw_breakpoints[n].len == len || len == -1)) {
684 return &hw_breakpoints[n];
691 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
695 if (find_hw_breakpoint(addr, len, type)) {
699 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
701 if (!hw_breakpoints) {
702 nb_hw_breakpoints = 0;
703 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
706 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
709 if (!hw_breakpoints) {
710 nb_hw_breakpoints = 0;
714 hw_breakpoints[nb_hw_breakpoints].addr = addr;
715 hw_breakpoints[nb_hw_breakpoints].len = len;
716 hw_breakpoints[nb_hw_breakpoints].type = type;
723 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
724 target_ulong len, int type)
727 case GDB_BREAKPOINT_HW:
730 case GDB_WATCHPOINT_WRITE:
734 type = KVM_HW_WP_WRITE;
739 return insert_hw_breakpoint(addr, len, type);
742 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
743 target_ulong len, int type)
746 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
753 if (nb_hw_breakpoints > 0) {
755 * In order to trim the array, move the last element to the position to
756 * be removed - if necessary.
758 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
759 *bp = hw_breakpoints[nb_hw_breakpoints];
761 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
763 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
765 g_free(hw_breakpoints);
766 hw_breakpoints = NULL;
772 void kvm_arch_remove_all_hw_breakpoints(void)
774 nb_hw_breakpoints = 0;
775 g_free(hw_breakpoints);
776 hw_breakpoints = NULL;
779 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
783 if (nb_hw_breakpoints > 0) {
784 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
785 dbg->arch.hw_bp = hw_breakpoints;
787 for (i = 0; i < nb_hw_breakpoints; ++i) {
788 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
789 hw_breakpoints[i].addr);
791 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
793 dbg->arch.nr_hw_bp = 0;
794 dbg->arch.hw_bp = NULL;
798 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
802 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
804 return MEMTXATTRS_UNSPECIFIED;
807 int kvm_arch_process_async_events(CPUState *cs)
812 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
813 struct kvm_s390_interrupt *interrupt)
817 interrupt->type = irq->type;
819 case KVM_S390_INT_VIRTIO:
820 interrupt->parm = irq->u.ext.ext_params;
822 case KVM_S390_INT_PFAULT_INIT:
823 case KVM_S390_INT_PFAULT_DONE:
824 interrupt->parm64 = irq->u.ext.ext_params2;
826 case KVM_S390_PROGRAM_INT:
827 interrupt->parm = irq->u.pgm.code;
829 case KVM_S390_SIGP_SET_PREFIX:
830 interrupt->parm = irq->u.prefix.address;
832 case KVM_S390_INT_SERVICE:
833 interrupt->parm = irq->u.ext.ext_params;
836 interrupt->parm = irq->u.mchk.cr14;
837 interrupt->parm64 = irq->u.mchk.mcic;
839 case KVM_S390_INT_EXTERNAL_CALL:
840 interrupt->parm = irq->u.extcall.code;
842 case KVM_S390_INT_EMERGENCY:
843 interrupt->parm = irq->u.emerg.code;
845 case KVM_S390_SIGP_STOP:
846 case KVM_S390_RESTART:
847 break; /* These types have no parameters */
848 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
849 interrupt->parm = irq->u.io.subchannel_id << 16;
850 interrupt->parm |= irq->u.io.subchannel_nr;
851 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
852 interrupt->parm64 |= irq->u.io.io_int_word;
861 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
863 struct kvm_s390_interrupt kvmint = {};
866 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
868 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
872 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
874 fprintf(stderr, "KVM failed to inject interrupt\n");
879 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
881 CPUState *cs = CPU(cpu);
885 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
889 error_report("KVM failed to inject interrupt %llx", irq->type);
893 inject_vcpu_irq_legacy(cs, irq);
896 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
898 struct kvm_s390_interrupt kvmint = {};
901 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
903 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
907 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
909 fprintf(stderr, "KVM failed to inject interrupt\n");
914 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
916 static bool use_flic = true;
920 r = kvm_s390_inject_flic(irq);
928 __kvm_s390_floating_interrupt(irq);
931 void kvm_s390_virtio_irq(int config_change, uint64_t token)
933 struct kvm_s390_irq irq = {
934 .type = KVM_S390_INT_VIRTIO,
935 .u.ext.ext_params = config_change,
936 .u.ext.ext_params2 = token,
939 kvm_s390_floating_interrupt(&irq);
942 void kvm_s390_service_interrupt(uint32_t parm)
944 struct kvm_s390_irq irq = {
945 .type = KVM_S390_INT_SERVICE,
946 .u.ext.ext_params = parm,
949 kvm_s390_floating_interrupt(&irq);
952 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
954 struct kvm_s390_irq irq = {
955 .type = KVM_S390_PROGRAM_INT,
959 kvm_s390_vcpu_interrupt(cpu, &irq);
962 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
964 struct kvm_s390_irq irq = {
965 .type = KVM_S390_PROGRAM_INT,
967 .u.pgm.trans_exc_code = te_code,
968 .u.pgm.exc_access_id = te_code & 3,
971 kvm_s390_vcpu_interrupt(cpu, &irq);
974 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
977 CPUS390XState *env = &cpu->env;
982 cpu_synchronize_state(CPU(cpu));
983 sccb = env->regs[ipbh0 & 0xf];
984 code = env->regs[(ipbh0 & 0xf0) >> 4];
986 r = sclp_service_call(env, sccb, code);
988 enter_pgmcheck(cpu, -r);
996 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
998 CPUS390XState *env = &cpu->env;
1000 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1002 cpu_synchronize_state(CPU(cpu));
1006 ioinst_handle_xsch(cpu, env->regs[1]);
1009 ioinst_handle_csch(cpu, env->regs[1]);
1012 ioinst_handle_hsch(cpu, env->regs[1]);
1015 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1018 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1021 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1024 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1027 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1028 fprintf(stderr, "Spurious tsch intercept\n");
1031 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1034 /* This should have been handled by kvm already. */
1035 fprintf(stderr, "Spurious tpi intercept\n");
1038 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1039 run->s390_sieic.ipb);
1042 ioinst_handle_rsch(cpu, env->regs[1]);
1045 ioinst_handle_rchp(cpu, env->regs[1]);
1048 /* We do not provide this instruction, it is suppressed. */
1051 ioinst_handle_sal(cpu, env->regs[1]);
1054 /* Not provided, set CC = 3 for subchannel not operational */
1057 case PRIV_B2_SCLP_CALL:
1058 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1062 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1069 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1072 CPUS390XState *env = &cpu->env;
1073 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1074 uint32_t base2 = run->s390_sieic.ipb >> 28;
1075 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1076 ((run->s390_sieic.ipb & 0xff00) << 4);
1078 if (disp2 & 0x80000) {
1079 disp2 += 0xfff00000;
1085 return (base2 ? env->regs[base2] : 0) +
1086 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1089 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1092 CPUS390XState *env = &cpu->env;
1093 uint32_t base2 = run->s390_sieic.ipb >> 28;
1094 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1095 ((run->s390_sieic.ipb & 0xff00) << 4);
1097 if (disp2 & 0x80000) {
1098 disp2 += 0xfff00000;
1104 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1107 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1109 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1111 return clp_service_call(cpu, r2);
1114 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1116 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1117 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1119 return pcilg_service_call(cpu, r1, r2);
1122 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1124 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1125 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1127 return pcistg_service_call(cpu, r1, r2);
1130 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1132 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1136 cpu_synchronize_state(CPU(cpu));
1137 fiba = get_base_disp_rxy(cpu, run, &ar);
1139 return stpcifc_service_call(cpu, r1, fiba, ar);
1142 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1148 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1150 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1151 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1153 return rpcit_service_call(cpu, r1, r2);
1156 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1158 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1159 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1163 cpu_synchronize_state(CPU(cpu));
1164 gaddr = get_base_disp_rsy(cpu, run, &ar);
1166 return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1169 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1171 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1175 cpu_synchronize_state(CPU(cpu));
1176 fiba = get_base_disp_rxy(cpu, run, &ar);
1178 return mpcifc_service_call(cpu, r1, fiba, ar);
1181 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1187 r = kvm_clp_service_call(cpu, run);
1189 case PRIV_B9_PCISTG:
1190 r = kvm_pcistg_service_call(cpu, run);
1193 r = kvm_pcilg_service_call(cpu, run);
1196 r = kvm_rpcit_service_call(cpu, run);
1199 /* just inject exception */
1204 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1211 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1216 case PRIV_EB_PCISTB:
1217 r = kvm_pcistb_service_call(cpu, run);
1220 r = kvm_sic_service_call(cpu, run);
1223 /* just inject exception */
1228 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1235 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1240 case PRIV_E3_MPCIFC:
1241 r = kvm_mpcifc_service_call(cpu, run);
1243 case PRIV_E3_STPCIFC:
1244 r = kvm_stpcifc_service_call(cpu, run);
1248 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1255 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1257 CPUS390XState *env = &cpu->env;
1260 cpu_synchronize_state(CPU(cpu));
1261 ret = s390_virtio_hypercall(env);
1262 if (ret == -EINVAL) {
1263 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1270 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1274 cpu_synchronize_state(CPU(cpu));
1275 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1276 r3 = run->s390_sieic.ipa & 0x000f;
1277 handle_diag_308(&cpu->env, r1, r3);
1280 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1282 CPUS390XState *env = &cpu->env;
1285 cpu_synchronize_state(CPU(cpu));
1287 pc = env->psw.addr - 4;
1288 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1296 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1298 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1304 * For any diagnose call we support, bits 48-63 of the resulting
1305 * address specify the function code; the remainder is ignored.
1307 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1308 switch (func_code) {
1310 kvm_handle_diag_308(cpu, run);
1312 case DIAG_KVM_HYPERCALL:
1313 r = handle_hypercall(cpu, run);
1315 case DIAG_KVM_BREAKPOINT:
1316 r = handle_sw_breakpoint(cpu, run);
1319 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1320 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1327 typedef struct SigpInfo {
1331 uint64_t *status_reg;
1334 static void set_sigp_status(SigpInfo *si, uint64_t status)
1336 *si->status_reg &= 0xffffffff00000000ULL;
1337 *si->status_reg |= status;
1338 si->cc = SIGP_CC_STATUS_STORED;
1341 static void sigp_start(void *arg)
1345 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1346 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1350 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1351 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1354 static void sigp_stop(void *arg)
1357 struct kvm_s390_irq irq = {
1358 .type = KVM_S390_SIGP_STOP,
1361 if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1362 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1366 /* disabled wait - sleeping in user space */
1367 if (CPU(si->cpu)->halted) {
1368 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1370 /* execute the stop function */
1371 si->cpu->env.sigp_order = SIGP_STOP;
1372 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1374 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1377 #define ADTL_SAVE_AREA_SIZE 1024
1378 static int kvm_s390_store_adtl_status(S390CPU *cpu, hwaddr addr)
1381 hwaddr len = ADTL_SAVE_AREA_SIZE;
1383 mem = cpu_physical_memory_map(addr, &len, 1);
1387 if (len != ADTL_SAVE_AREA_SIZE) {
1388 cpu_physical_memory_unmap(mem, len, 1, 0);
1392 memcpy(mem, &cpu->env.vregs, 512);
1394 cpu_physical_memory_unmap(mem, len, 1, len);
1399 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1400 #define SAVE_AREA_SIZE 512
1401 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1403 static const uint8_t ar_id = 1;
1404 uint64_t ckc = cpu->env.ckc >> 8;
1407 hwaddr len = SAVE_AREA_SIZE;
1409 mem = cpu_physical_memory_map(addr, &len, 1);
1413 if (len != SAVE_AREA_SIZE) {
1414 cpu_physical_memory_unmap(mem, len, 1, 0);
1419 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1421 for (i = 0; i < 16; ++i) {
1422 *((uint64 *)mem + i) = get_freg(&cpu->env, i)->ll;
1424 memcpy(mem + 128, &cpu->env.regs, 128);
1425 memcpy(mem + 256, &cpu->env.psw, 16);
1426 memcpy(mem + 280, &cpu->env.psa, 4);
1427 memcpy(mem + 284, &cpu->env.fpc, 4);
1428 memcpy(mem + 292, &cpu->env.todpr, 4);
1429 memcpy(mem + 296, &cpu->env.cputm, 8);
1430 memcpy(mem + 304, &ckc, 8);
1431 memcpy(mem + 320, &cpu->env.aregs, 64);
1432 memcpy(mem + 384, &cpu->env.cregs, 128);
1434 cpu_physical_memory_unmap(mem, len, 1, len);
1439 static void sigp_stop_and_store_status(void *arg)
1442 struct kvm_s390_irq irq = {
1443 .type = KVM_S390_SIGP_STOP,
1446 /* disabled wait - sleeping in user space */
1447 if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1448 CPU(si->cpu)->halted) {
1449 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1452 switch (s390_cpu_get_state(si->cpu)) {
1453 case CPU_STATE_OPERATING:
1454 si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1455 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1456 /* store will be performed when handling the stop intercept */
1458 case CPU_STATE_STOPPED:
1459 /* already stopped, just store the status */
1460 cpu_synchronize_state(CPU(si->cpu));
1461 kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1464 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1467 static void sigp_store_status_at_address(void *arg)
1470 uint32_t address = si->param & 0x7ffffe00u;
1472 /* cpu has to be stopped */
1473 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1474 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1478 cpu_synchronize_state(CPU(si->cpu));
1480 if (kvm_s390_store_status(si->cpu, address, false)) {
1481 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1484 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1487 static void sigp_store_adtl_status(void *arg)
1491 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
1492 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1496 /* cpu has to be stopped */
1497 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1498 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1502 /* parameter must be aligned to 1024-byte boundary */
1503 if (si->param & 0x3ff) {
1504 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1508 cpu_synchronize_state(CPU(si->cpu));
1510 if (kvm_s390_store_adtl_status(si->cpu, si->param)) {
1511 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1514 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1517 static void sigp_restart(void *arg)
1520 struct kvm_s390_irq irq = {
1521 .type = KVM_S390_RESTART,
1524 switch (s390_cpu_get_state(si->cpu)) {
1525 case CPU_STATE_STOPPED:
1526 /* the restart irq has to be delivered prior to any other pending irq */
1527 cpu_synchronize_state(CPU(si->cpu));
1528 do_restart_interrupt(&si->cpu->env);
1529 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1531 case CPU_STATE_OPERATING:
1532 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1535 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1538 int kvm_s390_cpu_restart(S390CPU *cpu)
1544 run_on_cpu(CPU(cpu), sigp_restart, &si);
1545 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1549 static void sigp_initial_cpu_reset(void *arg)
1552 CPUState *cs = CPU(si->cpu);
1553 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1555 cpu_synchronize_state(cs);
1556 scc->initial_cpu_reset(cs);
1557 cpu_synchronize_post_reset(cs);
1558 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1561 static void sigp_cpu_reset(void *arg)
1564 CPUState *cs = CPU(si->cpu);
1565 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1567 cpu_synchronize_state(cs);
1569 cpu_synchronize_post_reset(cs);
1570 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1573 static void sigp_set_prefix(void *arg)
1576 uint32_t addr = si->param & 0x7fffe000u;
1578 cpu_synchronize_state(CPU(si->cpu));
1580 if (!address_space_access_valid(&address_space_memory, addr,
1581 sizeof(struct LowCore), false)) {
1582 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1586 /* cpu has to be stopped */
1587 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1588 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1592 si->cpu->env.psa = addr;
1593 cpu_synchronize_post_init(CPU(si->cpu));
1594 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1597 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1598 uint64_t param, uint64_t *status_reg)
1603 .status_reg = status_reg,
1606 /* cpu available? */
1607 if (dst_cpu == NULL) {
1608 return SIGP_CC_NOT_OPERATIONAL;
1611 /* only resets can break pending orders */
1612 if (dst_cpu->env.sigp_order != 0 &&
1613 order != SIGP_CPU_RESET &&
1614 order != SIGP_INITIAL_CPU_RESET) {
1615 return SIGP_CC_BUSY;
1620 run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1623 run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1626 run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1628 case SIGP_STOP_STORE_STATUS:
1629 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1631 case SIGP_STORE_STATUS_ADDR:
1632 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1634 case SIGP_STORE_ADTL_STATUS:
1635 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, &si);
1637 case SIGP_SET_PREFIX:
1638 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1640 case SIGP_INITIAL_CPU_RESET:
1641 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1643 case SIGP_CPU_RESET:
1644 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1647 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1648 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1654 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1655 uint64_t *status_reg)
1660 /* due to the BQL, we are the only active cpu */
1661 CPU_FOREACH(cur_cs) {
1662 cur_cpu = S390_CPU(cur_cs);
1663 if (cur_cpu->env.sigp_order != 0) {
1664 return SIGP_CC_BUSY;
1666 cpu_synchronize_state(cur_cs);
1667 /* all but the current one have to be stopped */
1668 if (cur_cpu != cpu &&
1669 s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1670 *status_reg &= 0xffffffff00000000ULL;
1671 *status_reg |= SIGP_STAT_INCORRECT_STATE;
1672 return SIGP_CC_STATUS_STORED;
1676 switch (param & 0xff) {
1677 case SIGP_MODE_ESA_S390:
1679 return SIGP_CC_NOT_OPERATIONAL;
1680 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1681 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1682 CPU_FOREACH(cur_cs) {
1683 cur_cpu = S390_CPU(cur_cs);
1684 cur_cpu->env.pfault_token = -1UL;
1688 *status_reg &= 0xffffffff00000000ULL;
1689 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1690 return SIGP_CC_STATUS_STORED;
1693 return SIGP_CC_ORDER_CODE_ACCEPTED;
1696 #define SIGP_ORDER_MASK 0x000000ff
1698 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1700 CPUS390XState *env = &cpu->env;
1701 const uint8_t r1 = ipa1 >> 4;
1702 const uint8_t r3 = ipa1 & 0x0f;
1705 uint64_t *status_reg;
1707 S390CPU *dst_cpu = NULL;
1709 cpu_synchronize_state(CPU(cpu));
1711 /* get order code */
1712 order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1714 status_reg = &env->regs[r1];
1715 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1719 ret = sigp_set_architecture(cpu, param, status_reg);
1722 /* all other sigp orders target a single vcpu */
1723 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1724 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1727 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1728 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1738 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1740 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1741 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1744 DPRINTF("handle_instruction 0x%x 0x%x\n",
1745 run->s390_sieic.ipa, run->s390_sieic.ipb);
1748 r = handle_b2(cpu, run, ipa1);
1751 r = handle_b9(cpu, run, ipa1);
1754 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1757 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1760 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1763 r = handle_sigp(cpu, run, ipa1);
1769 enter_pgmcheck(cpu, 0x0001);
1775 static bool is_special_wait_psw(CPUState *cs)
1777 /* signal quiesce */
1778 return cs->kvm_run->psw_addr == 0xfffUL;
1781 static void guest_panicked(void)
1783 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1785 vm_stop(RUN_STATE_GUEST_PANICKED);
1788 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1790 CPUState *cs = CPU(cpu);
1792 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1793 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1794 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1799 static int handle_intercept(S390CPU *cpu)
1801 CPUState *cs = CPU(cpu);
1802 struct kvm_run *run = cs->kvm_run;
1803 int icpt_code = run->s390_sieic.icptcode;
1806 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1807 (long)cs->kvm_run->psw_addr);
1808 switch (icpt_code) {
1809 case ICPT_INSTRUCTION:
1810 r = handle_instruction(cpu, run);
1813 unmanageable_intercept(cpu, "program interrupt",
1814 offsetof(LowCore, program_new_psw));
1818 unmanageable_intercept(cpu, "external interrupt",
1819 offsetof(LowCore, external_new_psw));
1823 /* disabled wait, since enabled wait is handled in kernel */
1824 cpu_synchronize_state(cs);
1825 if (s390_cpu_halt(cpu) == 0) {
1826 if (is_special_wait_psw(cs)) {
1827 qemu_system_shutdown_request();
1835 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1836 qemu_system_shutdown_request();
1838 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1839 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1842 cpu->env.sigp_order = 0;
1845 case ICPT_SOFT_INTERCEPT:
1846 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1850 fprintf(stderr, "KVM unimplemented icpt IO\n");
1854 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1862 static int handle_tsch(S390CPU *cpu)
1864 CPUState *cs = CPU(cpu);
1865 struct kvm_run *run = cs->kvm_run;
1868 cpu_synchronize_state(cs);
1870 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1874 * If an I/O interrupt had been dequeued, we have to reinject it.
1876 if (run->s390_tsch.dequeued) {
1877 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1878 run->s390_tsch.subchannel_nr,
1879 run->s390_tsch.io_int_parm,
1880 run->s390_tsch.io_int_word);
1887 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1889 struct sysib_322 sysib;
1892 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1895 /* Shift the stack of Extended Names to prepare for our own data */
1896 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1897 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1898 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1899 * assumed it's not capable of managing Extended Names for lower levels.
1901 for (del = 1; del < sysib.count; del++) {
1902 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1906 if (del < sysib.count) {
1907 memset(sysib.ext_names[del], 0,
1908 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1910 /* Insert short machine name in EBCDIC, padded with blanks */
1912 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1913 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1914 strlen(qemu_name)));
1916 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1917 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1918 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1919 * considered by s390 as not capable of providing any Extended Name.
1920 * Therefore if no name was specified on qemu invocation, we go with the
1921 * same "KVMguest" default, which KVM has filled into short name field.
1924 strncpy((char *)sysib.ext_names[0], qemu_name,
1925 sizeof(sysib.ext_names[0]));
1927 strcpy((char *)sysib.ext_names[0], "KVMguest");
1930 memcpy(sysib.vm[0].uuid, qemu_uuid, sizeof(sysib.vm[0].uuid));
1932 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1935 static int handle_stsi(S390CPU *cpu)
1937 CPUState *cs = CPU(cpu);
1938 struct kvm_run *run = cs->kvm_run;
1940 switch (run->s390_stsi.fc) {
1942 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1945 /* Only sysib 3.2.2 needs post-handling for now. */
1946 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1953 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1955 CPUState *cs = CPU(cpu);
1956 struct kvm_run *run = cs->kvm_run;
1959 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1961 switch (arch_info->type) {
1962 case KVM_HW_WP_WRITE:
1963 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1964 cs->watchpoint_hit = &hw_watchpoint;
1965 hw_watchpoint.vaddr = arch_info->addr;
1966 hw_watchpoint.flags = BP_MEM_WRITE;
1971 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1975 case KVM_SINGLESTEP:
1976 if (cs->singlestep_enabled) {
1987 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1989 S390CPU *cpu = S390_CPU(cs);
1992 switch (run->exit_reason) {
1993 case KVM_EXIT_S390_SIEIC:
1994 ret = handle_intercept(cpu);
1996 case KVM_EXIT_S390_RESET:
1997 s390_reipl_request();
1999 case KVM_EXIT_S390_TSCH:
2000 ret = handle_tsch(cpu);
2002 case KVM_EXIT_S390_STSI:
2003 ret = handle_stsi(cpu);
2005 case KVM_EXIT_DEBUG:
2006 ret = kvm_arch_handle_debug_exit(cpu);
2009 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2014 ret = EXCP_INTERRUPT;
2019 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2024 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
2029 int kvm_arch_on_sigbus(int code, void *addr)
2034 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2035 uint16_t subchannel_nr, uint32_t io_int_parm,
2036 uint32_t io_int_word)
2038 struct kvm_s390_irq irq = {
2039 .u.io.subchannel_id = subchannel_id,
2040 .u.io.subchannel_nr = subchannel_nr,
2041 .u.io.io_int_parm = io_int_parm,
2042 .u.io.io_int_word = io_int_word,
2045 if (io_int_word & IO_INT_WORD_AI) {
2046 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2048 irq.type = ((subchannel_id & 0xff00) << 24) |
2049 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
2051 kvm_s390_floating_interrupt(&irq);
2054 void kvm_s390_crw_mchk(void)
2056 struct kvm_s390_irq irq = {
2057 .type = KVM_S390_MCHK,
2058 .u.mchk.cr14 = 1 << 28,
2059 .u.mchk.mcic = 0x00400f1d40330000ULL,
2061 kvm_s390_floating_interrupt(&irq);
2064 void kvm_s390_enable_css_support(S390CPU *cpu)
2068 /* Activate host kernel channel subsystem support. */
2069 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2073 void kvm_arch_init_irq_routing(KVMState *s)
2076 * Note that while irqchip capabilities generally imply that cpustates
2077 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2078 * have to override the common code kvm_halt_in_kernel_allowed setting.
2080 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2081 kvm_gsi_routing_allowed = true;
2082 kvm_halt_in_kernel_allowed = false;
2086 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2087 int vq, bool assign)
2089 struct kvm_ioeventfd kick = {
2090 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2091 KVM_IOEVENTFD_FLAG_DATAMATCH,
2092 .fd = event_notifier_get_fd(notifier),
2097 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2101 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2103 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2106 int kvm_s390_get_memslot_count(KVMState *s)
2108 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
2111 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2113 struct kvm_mp_state mp_state = {};
2116 /* the kvm part might not have been initialized yet */
2117 if (CPU(cpu)->kvm_state == NULL) {
2121 switch (cpu_state) {
2122 case CPU_STATE_STOPPED:
2123 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2125 case CPU_STATE_CHECK_STOP:
2126 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2128 case CPU_STATE_OPERATING:
2129 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2131 case CPU_STATE_LOAD:
2132 mp_state.mp_state = KVM_MP_STATE_LOAD;
2135 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2140 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2142 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2149 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2151 struct kvm_s390_irq_state irq_state;
2152 CPUState *cs = CPU(cpu);
2155 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2159 irq_state.buf = (uint64_t) cpu->irqstate;
2160 irq_state.len = VCPU_IRQ_BUF_SIZE;
2162 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2164 cpu->irqstate_saved_size = 0;
2165 error_report("Migration of interrupt state failed");
2169 cpu->irqstate_saved_size = bytes;
2172 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2174 CPUState *cs = CPU(cpu);
2175 struct kvm_s390_irq_state irq_state;
2178 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2182 if (cpu->irqstate_saved_size == 0) {
2185 irq_state.buf = (uint64_t) cpu->irqstate;
2186 irq_state.len = cpu->irqstate_saved_size;
2188 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2190 error_report("Setting interrupt state failed %d", r);
2195 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2196 uint64_t address, uint32_t data)
2198 S390PCIBusDevice *pbdev;
2199 uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
2200 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2202 pbdev = s390_pci_find_dev_by_fid(fid);
2204 DPRINTF("add_msi_route no dev\n");
2208 pbdev->routes.adapter.ind_offset = vec;
2210 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2212 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2213 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2214 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2215 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2216 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2220 int kvm_arch_msi_data_to_gsi(uint32_t data)