target-i386: Simplify listflags() function
[qemu/ar7.git] / target-s390x / kvm.c
blob6f2d5b492412768c7f7e48335476b6c2bce73826
1 /*
2 * QEMU S390x KVM implementation
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp. 2012
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * Contributions after 2012-10-29 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
20 * You should have received a copy of the GNU (Lesser) General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/mman.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"
35 #include "hw/hw.h"
36 #include "cpu.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 "trace.h"
42 #include "qapi-event.h"
43 #include "hw/s390x/s390-pci-inst.h"
44 #include "hw/s390x/s390-pci-bus.h"
46 /* #define DEBUG_KVM */
48 #ifdef DEBUG_KVM
49 #define DPRINTF(fmt, ...) \
50 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
51 #else
52 #define DPRINTF(fmt, ...) \
53 do { } while (0)
54 #endif
56 #define IPA0_DIAG 0x8300
57 #define IPA0_SIGP 0xae00
58 #define IPA0_B2 0xb200
59 #define IPA0_B9 0xb900
60 #define IPA0_EB 0xeb00
61 #define IPA0_E3 0xe300
63 #define PRIV_B2_SCLP_CALL 0x20
64 #define PRIV_B2_CSCH 0x30
65 #define PRIV_B2_HSCH 0x31
66 #define PRIV_B2_MSCH 0x32
67 #define PRIV_B2_SSCH 0x33
68 #define PRIV_B2_STSCH 0x34
69 #define PRIV_B2_TSCH 0x35
70 #define PRIV_B2_TPI 0x36
71 #define PRIV_B2_SAL 0x37
72 #define PRIV_B2_RSCH 0x38
73 #define PRIV_B2_STCRW 0x39
74 #define PRIV_B2_STCPS 0x3a
75 #define PRIV_B2_RCHP 0x3b
76 #define PRIV_B2_SCHM 0x3c
77 #define PRIV_B2_CHSC 0x5f
78 #define PRIV_B2_SIGA 0x74
79 #define PRIV_B2_XSCH 0x76
81 #define PRIV_EB_SQBS 0x8a
82 #define PRIV_EB_PCISTB 0xd0
83 #define PRIV_EB_SIC 0xd1
85 #define PRIV_B9_EQBS 0x9c
86 #define PRIV_B9_CLP 0xa0
87 #define PRIV_B9_PCISTG 0xd0
88 #define PRIV_B9_PCILG 0xd2
89 #define PRIV_B9_RPCIT 0xd3
91 #define PRIV_E3_MPCIFC 0xd0
92 #define PRIV_E3_STPCIFC 0xd4
94 #define DIAG_IPL 0x308
95 #define DIAG_KVM_HYPERCALL 0x500
96 #define DIAG_KVM_BREAKPOINT 0x501
98 #define ICPT_INSTRUCTION 0x04
99 #define ICPT_PROGRAM 0x08
100 #define ICPT_EXT_INT 0x14
101 #define ICPT_WAITPSW 0x1c
102 #define ICPT_SOFT_INTERCEPT 0x24
103 #define ICPT_CPU_STOP 0x28
104 #define ICPT_IO 0x40
106 static CPUWatchpoint hw_watchpoint;
108 * We don't use a list because this structure is also used to transmit the
109 * hardware breakpoints to the kernel.
111 static struct kvm_hw_breakpoint *hw_breakpoints;
112 static int nb_hw_breakpoints;
114 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
115 KVM_CAP_LAST_INFO
118 static int cap_sync_regs;
119 static int cap_async_pf;
121 static void *legacy_s390_alloc(size_t size, uint64_t *align);
123 static int kvm_s390_check_clear_cmma(KVMState *s)
125 struct kvm_device_attr attr = {
126 .group = KVM_S390_VM_MEM_CTRL,
127 .attr = KVM_S390_VM_MEM_CLR_CMMA,
130 return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
133 static int kvm_s390_check_enable_cmma(KVMState *s)
135 struct kvm_device_attr attr = {
136 .group = KVM_S390_VM_MEM_CTRL,
137 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
140 return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
143 void kvm_s390_clear_cmma_callback(void *opaque)
145 int rc;
146 KVMState *s = opaque;
147 struct kvm_device_attr attr = {
148 .group = KVM_S390_VM_MEM_CTRL,
149 .attr = KVM_S390_VM_MEM_CLR_CMMA,
152 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
153 trace_kvm_clear_cmma(rc);
156 static void kvm_s390_enable_cmma(KVMState *s)
158 int rc;
159 struct kvm_device_attr attr = {
160 .group = KVM_S390_VM_MEM_CTRL,
161 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
164 if (kvm_s390_check_enable_cmma(s) || kvm_s390_check_clear_cmma(s)) {
165 return;
168 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
169 if (!rc) {
170 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
172 trace_kvm_enable_cmma(rc);
175 int kvm_arch_init(KVMState *s)
177 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
178 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
180 if (kvm_check_extension(s, KVM_CAP_VM_ATTRIBUTES)) {
181 kvm_s390_enable_cmma(s);
184 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
185 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
186 phys_mem_set_alloc(legacy_s390_alloc);
188 return 0;
191 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
193 return cpu->cpu_index;
196 int kvm_arch_init_vcpu(CPUState *cs)
198 S390CPU *cpu = S390_CPU(cs);
199 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
200 return 0;
203 void kvm_s390_reset_vcpu(S390CPU *cpu)
205 CPUState *cs = CPU(cpu);
207 /* The initial reset call is needed here to reset in-kernel
208 * vcpu data that we can't access directly from QEMU
209 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
210 * Before this ioctl cpu_synchronize_state() is called in common kvm
211 * code (kvm-all) */
212 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
213 error_report("Initial CPU reset failed on CPU %i\n", cs->cpu_index);
217 static int can_sync_regs(CPUState *cs, int regs)
219 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
222 int kvm_arch_put_registers(CPUState *cs, int level)
224 S390CPU *cpu = S390_CPU(cs);
225 CPUS390XState *env = &cpu->env;
226 struct kvm_sregs sregs;
227 struct kvm_regs regs;
228 struct kvm_fpu fpu = {};
229 int r;
230 int i;
232 /* always save the PSW and the GPRS*/
233 cs->kvm_run->psw_addr = env->psw.addr;
234 cs->kvm_run->psw_mask = env->psw.mask;
236 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
237 for (i = 0; i < 16; i++) {
238 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
239 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
241 } else {
242 for (i = 0; i < 16; i++) {
243 regs.gprs[i] = env->regs[i];
245 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
246 if (r < 0) {
247 return r;
251 /* Floating point */
252 for (i = 0; i < 16; i++) {
253 fpu.fprs[i] = env->fregs[i].ll;
255 fpu.fpc = env->fpc;
257 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
258 if (r < 0) {
259 return r;
262 /* Do we need to save more than that? */
263 if (level == KVM_PUT_RUNTIME_STATE) {
264 return 0;
267 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
268 cs->kvm_run->s.regs.cputm = env->cputm;
269 cs->kvm_run->s.regs.ckc = env->ckc;
270 cs->kvm_run->s.regs.todpr = env->todpr;
271 cs->kvm_run->s.regs.gbea = env->gbea;
272 cs->kvm_run->s.regs.pp = env->pp;
273 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
274 } else {
276 * These ONE_REGS are not protected by a capability. As they are only
277 * necessary for migration we just trace a possible error, but don't
278 * return with an error return code.
280 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
281 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
282 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
283 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
284 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
287 /* pfault parameters */
288 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
289 cs->kvm_run->s.regs.pft = env->pfault_token;
290 cs->kvm_run->s.regs.pfs = env->pfault_select;
291 cs->kvm_run->s.regs.pfc = env->pfault_compare;
292 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
293 } else if (cap_async_pf) {
294 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
295 if (r < 0) {
296 return r;
298 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
299 if (r < 0) {
300 return r;
302 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
303 if (r < 0) {
304 return r;
308 /* access registers and control registers*/
309 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
310 for (i = 0; i < 16; i++) {
311 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
312 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
314 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
315 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
316 } else {
317 for (i = 0; i < 16; i++) {
318 sregs.acrs[i] = env->aregs[i];
319 sregs.crs[i] = env->cregs[i];
321 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
322 if (r < 0) {
323 return r;
327 /* Finally the prefix */
328 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
329 cs->kvm_run->s.regs.prefix = env->psa;
330 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
331 } else {
332 /* prefix is only supported via sync regs */
334 return 0;
337 int kvm_arch_get_registers(CPUState *cs)
339 S390CPU *cpu = S390_CPU(cs);
340 CPUS390XState *env = &cpu->env;
341 struct kvm_sregs sregs;
342 struct kvm_regs regs;
343 struct kvm_fpu fpu;
344 int i, r;
346 /* get the PSW */
347 env->psw.addr = cs->kvm_run->psw_addr;
348 env->psw.mask = cs->kvm_run->psw_mask;
350 /* the GPRS */
351 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
352 for (i = 0; i < 16; i++) {
353 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
355 } else {
356 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
357 if (r < 0) {
358 return r;
360 for (i = 0; i < 16; i++) {
361 env->regs[i] = regs.gprs[i];
365 /* The ACRS and CRS */
366 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
367 for (i = 0; i < 16; i++) {
368 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
369 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
371 } else {
372 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
373 if (r < 0) {
374 return r;
376 for (i = 0; i < 16; i++) {
377 env->aregs[i] = sregs.acrs[i];
378 env->cregs[i] = sregs.crs[i];
382 /* Floating point */
383 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
384 if (r < 0) {
385 return r;
387 for (i = 0; i < 16; i++) {
388 env->fregs[i].ll = fpu.fprs[i];
390 env->fpc = fpu.fpc;
392 /* The prefix */
393 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
394 env->psa = cs->kvm_run->s.regs.prefix;
397 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
398 env->cputm = cs->kvm_run->s.regs.cputm;
399 env->ckc = cs->kvm_run->s.regs.ckc;
400 env->todpr = cs->kvm_run->s.regs.todpr;
401 env->gbea = cs->kvm_run->s.regs.gbea;
402 env->pp = cs->kvm_run->s.regs.pp;
403 } else {
405 * These ONE_REGS are not protected by a capability. As they are only
406 * necessary for migration we just trace a possible error, but don't
407 * return with an error return code.
409 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
410 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
411 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
412 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
413 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
416 /* pfault parameters */
417 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
418 env->pfault_token = cs->kvm_run->s.regs.pft;
419 env->pfault_select = cs->kvm_run->s.regs.pfs;
420 env->pfault_compare = cs->kvm_run->s.regs.pfc;
421 } else if (cap_async_pf) {
422 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
423 if (r < 0) {
424 return r;
426 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
427 if (r < 0) {
428 return r;
430 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
431 if (r < 0) {
432 return r;
436 return 0;
440 * Legacy layout for s390:
441 * Older S390 KVM requires the topmost vma of the RAM to be
442 * smaller than an system defined value, which is at least 256GB.
443 * Larger systems have larger values. We put the guest between
444 * the end of data segment (system break) and this value. We
445 * use 32GB as a base to have enough room for the system break
446 * to grow. We also have to use MAP parameters that avoid
447 * read-only mapping of guest pages.
449 static void *legacy_s390_alloc(size_t size, uint64_t *align)
451 void *mem;
453 mem = mmap((void *) 0x800000000ULL, size,
454 PROT_EXEC|PROT_READ|PROT_WRITE,
455 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
456 return mem == MAP_FAILED ? NULL : mem;
459 /* DIAG 501 is used for sw breakpoints */
460 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
462 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
465 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
466 sizeof(diag_501), 0) ||
467 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
468 sizeof(diag_501), 1)) {
469 return -EINVAL;
471 return 0;
474 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
476 uint8_t t[sizeof(diag_501)];
478 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
479 return -EINVAL;
480 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
481 return -EINVAL;
482 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
483 sizeof(diag_501), 1)) {
484 return -EINVAL;
487 return 0;
490 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
491 int len, int type)
493 int n;
495 for (n = 0; n < nb_hw_breakpoints; n++) {
496 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
497 (hw_breakpoints[n].len == len || len == -1)) {
498 return &hw_breakpoints[n];
502 return NULL;
505 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
507 int size;
509 if (find_hw_breakpoint(addr, len, type)) {
510 return -EEXIST;
513 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
515 if (!hw_breakpoints) {
516 nb_hw_breakpoints = 0;
517 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
518 } else {
519 hw_breakpoints =
520 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
523 if (!hw_breakpoints) {
524 nb_hw_breakpoints = 0;
525 return -ENOMEM;
528 hw_breakpoints[nb_hw_breakpoints].addr = addr;
529 hw_breakpoints[nb_hw_breakpoints].len = len;
530 hw_breakpoints[nb_hw_breakpoints].type = type;
532 nb_hw_breakpoints++;
534 return 0;
537 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
538 target_ulong len, int type)
540 switch (type) {
541 case GDB_BREAKPOINT_HW:
542 type = KVM_HW_BP;
543 break;
544 case GDB_WATCHPOINT_WRITE:
545 if (len < 1) {
546 return -EINVAL;
548 type = KVM_HW_WP_WRITE;
549 break;
550 default:
551 return -ENOSYS;
553 return insert_hw_breakpoint(addr, len, type);
556 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
557 target_ulong len, int type)
559 int size;
560 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
562 if (bp == NULL) {
563 return -ENOENT;
566 nb_hw_breakpoints--;
567 if (nb_hw_breakpoints > 0) {
569 * In order to trim the array, move the last element to the position to
570 * be removed - if necessary.
572 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
573 *bp = hw_breakpoints[nb_hw_breakpoints];
575 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
576 hw_breakpoints =
577 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
578 } else {
579 g_free(hw_breakpoints);
580 hw_breakpoints = NULL;
583 return 0;
586 void kvm_arch_remove_all_hw_breakpoints(void)
588 nb_hw_breakpoints = 0;
589 g_free(hw_breakpoints);
590 hw_breakpoints = NULL;
593 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
595 int i;
597 if (nb_hw_breakpoints > 0) {
598 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
599 dbg->arch.hw_bp = hw_breakpoints;
601 for (i = 0; i < nb_hw_breakpoints; ++i) {
602 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
603 hw_breakpoints[i].addr);
605 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
606 } else {
607 dbg->arch.nr_hw_bp = 0;
608 dbg->arch.hw_bp = NULL;
612 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
616 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
620 int kvm_arch_process_async_events(CPUState *cs)
622 return cs->halted;
625 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
626 struct kvm_s390_interrupt *interrupt)
628 int r = 0;
630 interrupt->type = irq->type;
631 switch (irq->type) {
632 case KVM_S390_INT_VIRTIO:
633 interrupt->parm = irq->u.ext.ext_params;
634 /* fall through */
635 case KVM_S390_INT_PFAULT_INIT:
636 case KVM_S390_INT_PFAULT_DONE:
637 interrupt->parm64 = irq->u.ext.ext_params2;
638 break;
639 case KVM_S390_PROGRAM_INT:
640 interrupt->parm = irq->u.pgm.code;
641 break;
642 case KVM_S390_SIGP_SET_PREFIX:
643 interrupt->parm = irq->u.prefix.address;
644 break;
645 case KVM_S390_INT_SERVICE:
646 interrupt->parm = irq->u.ext.ext_params;
647 break;
648 case KVM_S390_MCHK:
649 interrupt->parm = irq->u.mchk.cr14;
650 interrupt->parm64 = irq->u.mchk.mcic;
651 break;
652 case KVM_S390_INT_EXTERNAL_CALL:
653 interrupt->parm = irq->u.extcall.code;
654 break;
655 case KVM_S390_INT_EMERGENCY:
656 interrupt->parm = irq->u.emerg.code;
657 break;
658 case KVM_S390_SIGP_STOP:
659 case KVM_S390_RESTART:
660 break; /* These types have no parameters */
661 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
662 interrupt->parm = irq->u.io.subchannel_id << 16;
663 interrupt->parm |= irq->u.io.subchannel_nr;
664 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
665 interrupt->parm64 |= irq->u.io.io_int_word;
666 break;
667 default:
668 r = -EINVAL;
669 break;
671 return r;
674 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
676 struct kvm_s390_interrupt kvmint = {};
677 CPUState *cs = CPU(cpu);
678 int r;
680 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
681 if (r < 0) {
682 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
683 exit(1);
686 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
687 if (r < 0) {
688 fprintf(stderr, "KVM failed to inject interrupt\n");
689 exit(1);
693 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
695 struct kvm_s390_interrupt kvmint = {};
696 int r;
698 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
699 if (r < 0) {
700 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
701 exit(1);
704 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
705 if (r < 0) {
706 fprintf(stderr, "KVM failed to inject interrupt\n");
707 exit(1);
711 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
713 static bool use_flic = true;
714 int r;
716 if (use_flic) {
717 r = kvm_s390_inject_flic(irq);
718 if (r == -ENOSYS) {
719 use_flic = false;
721 if (!r) {
722 return;
725 __kvm_s390_floating_interrupt(irq);
728 void kvm_s390_virtio_irq(int config_change, uint64_t token)
730 struct kvm_s390_irq irq = {
731 .type = KVM_S390_INT_VIRTIO,
732 .u.ext.ext_params = config_change,
733 .u.ext.ext_params2 = token,
736 kvm_s390_floating_interrupt(&irq);
739 void kvm_s390_service_interrupt(uint32_t parm)
741 struct kvm_s390_irq irq = {
742 .type = KVM_S390_INT_SERVICE,
743 .u.ext.ext_params = parm,
746 kvm_s390_floating_interrupt(&irq);
749 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
751 struct kvm_s390_irq irq = {
752 .type = KVM_S390_PROGRAM_INT,
753 .u.pgm.code = code,
756 kvm_s390_vcpu_interrupt(cpu, &irq);
759 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
760 uint16_t ipbh0)
762 CPUS390XState *env = &cpu->env;
763 uint64_t sccb;
764 uint32_t code;
765 int r = 0;
767 cpu_synchronize_state(CPU(cpu));
768 sccb = env->regs[ipbh0 & 0xf];
769 code = env->regs[(ipbh0 & 0xf0) >> 4];
771 r = sclp_service_call(env, sccb, code);
772 if (r < 0) {
773 enter_pgmcheck(cpu, -r);
774 } else {
775 setcc(cpu, r);
778 return 0;
781 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
783 CPUS390XState *env = &cpu->env;
784 int rc = 0;
785 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
787 cpu_synchronize_state(CPU(cpu));
789 switch (ipa1) {
790 case PRIV_B2_XSCH:
791 ioinst_handle_xsch(cpu, env->regs[1]);
792 break;
793 case PRIV_B2_CSCH:
794 ioinst_handle_csch(cpu, env->regs[1]);
795 break;
796 case PRIV_B2_HSCH:
797 ioinst_handle_hsch(cpu, env->regs[1]);
798 break;
799 case PRIV_B2_MSCH:
800 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
801 break;
802 case PRIV_B2_SSCH:
803 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
804 break;
805 case PRIV_B2_STCRW:
806 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
807 break;
808 case PRIV_B2_STSCH:
809 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
810 break;
811 case PRIV_B2_TSCH:
812 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
813 fprintf(stderr, "Spurious tsch intercept\n");
814 break;
815 case PRIV_B2_CHSC:
816 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
817 break;
818 case PRIV_B2_TPI:
819 /* This should have been handled by kvm already. */
820 fprintf(stderr, "Spurious tpi intercept\n");
821 break;
822 case PRIV_B2_SCHM:
823 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
824 run->s390_sieic.ipb);
825 break;
826 case PRIV_B2_RSCH:
827 ioinst_handle_rsch(cpu, env->regs[1]);
828 break;
829 case PRIV_B2_RCHP:
830 ioinst_handle_rchp(cpu, env->regs[1]);
831 break;
832 case PRIV_B2_STCPS:
833 /* We do not provide this instruction, it is suppressed. */
834 break;
835 case PRIV_B2_SAL:
836 ioinst_handle_sal(cpu, env->regs[1]);
837 break;
838 case PRIV_B2_SIGA:
839 /* Not provided, set CC = 3 for subchannel not operational */
840 setcc(cpu, 3);
841 break;
842 case PRIV_B2_SCLP_CALL:
843 rc = kvm_sclp_service_call(cpu, run, ipbh0);
844 break;
845 default:
846 rc = -1;
847 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
848 break;
851 return rc;
854 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run)
856 CPUS390XState *env = &cpu->env;
857 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
858 uint32_t base2 = run->s390_sieic.ipb >> 28;
859 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
860 ((run->s390_sieic.ipb & 0xff00) << 4);
862 if (disp2 & 0x80000) {
863 disp2 += 0xfff00000;
866 return (base2 ? env->regs[base2] : 0) +
867 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
870 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run)
872 CPUS390XState *env = &cpu->env;
873 uint32_t base2 = run->s390_sieic.ipb >> 28;
874 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
875 ((run->s390_sieic.ipb & 0xff00) << 4);
877 if (disp2 & 0x80000) {
878 disp2 += 0xfff00000;
881 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
884 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
886 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
888 return clp_service_call(cpu, r2);
891 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
893 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
894 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
896 return pcilg_service_call(cpu, r1, r2);
899 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
901 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
902 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
904 return pcistg_service_call(cpu, r1, r2);
907 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
909 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
910 uint64_t fiba;
912 cpu_synchronize_state(CPU(cpu));
913 fiba = get_base_disp_rxy(cpu, run);
915 return stpcifc_service_call(cpu, r1, fiba);
918 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
920 /* NOOP */
921 return 0;
924 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
926 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
927 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
929 return rpcit_service_call(cpu, r1, r2);
932 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
934 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
935 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
936 uint64_t gaddr;
938 cpu_synchronize_state(CPU(cpu));
939 gaddr = get_base_disp_rsy(cpu, run);
941 return pcistb_service_call(cpu, r1, r3, gaddr);
944 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
946 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
947 uint64_t fiba;
949 cpu_synchronize_state(CPU(cpu));
950 fiba = get_base_disp_rxy(cpu, run);
952 return mpcifc_service_call(cpu, r1, fiba);
955 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
957 int r = 0;
959 switch (ipa1) {
960 case PRIV_B9_CLP:
961 r = kvm_clp_service_call(cpu, run);
962 break;
963 case PRIV_B9_PCISTG:
964 r = kvm_pcistg_service_call(cpu, run);
965 break;
966 case PRIV_B9_PCILG:
967 r = kvm_pcilg_service_call(cpu, run);
968 break;
969 case PRIV_B9_RPCIT:
970 r = kvm_rpcit_service_call(cpu, run);
971 break;
972 case PRIV_B9_EQBS:
973 /* just inject exception */
974 r = -1;
975 break;
976 default:
977 r = -1;
978 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
979 break;
982 return r;
985 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
987 int r = 0;
989 switch (ipbl) {
990 case PRIV_EB_PCISTB:
991 r = kvm_pcistb_service_call(cpu, run);
992 break;
993 case PRIV_EB_SIC:
994 r = kvm_sic_service_call(cpu, run);
995 break;
996 case PRIV_EB_SQBS:
997 /* just inject exception */
998 r = -1;
999 break;
1000 default:
1001 r = -1;
1002 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1003 break;
1006 return r;
1009 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1011 int r = 0;
1013 switch (ipbl) {
1014 case PRIV_E3_MPCIFC:
1015 r = kvm_mpcifc_service_call(cpu, run);
1016 break;
1017 case PRIV_E3_STPCIFC:
1018 r = kvm_stpcifc_service_call(cpu, run);
1019 break;
1020 default:
1021 r = -1;
1022 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1023 break;
1026 return r;
1029 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1031 CPUS390XState *env = &cpu->env;
1032 int ret;
1034 cpu_synchronize_state(CPU(cpu));
1035 ret = s390_virtio_hypercall(env);
1036 if (ret == -EINVAL) {
1037 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1038 return 0;
1041 return ret;
1044 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1046 uint64_t r1, r3;
1048 cpu_synchronize_state(CPU(cpu));
1049 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1050 r3 = run->s390_sieic.ipa & 0x000f;
1051 handle_diag_308(&cpu->env, r1, r3);
1054 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1056 CPUS390XState *env = &cpu->env;
1057 unsigned long pc;
1059 cpu_synchronize_state(CPU(cpu));
1061 pc = env->psw.addr - 4;
1062 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1063 env->psw.addr = pc;
1064 return EXCP_DEBUG;
1067 return -ENOENT;
1070 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1072 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1074 int r = 0;
1075 uint16_t func_code;
1078 * For any diagnose call we support, bits 48-63 of the resulting
1079 * address specify the function code; the remainder is ignored.
1081 func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
1082 switch (func_code) {
1083 case DIAG_IPL:
1084 kvm_handle_diag_308(cpu, run);
1085 break;
1086 case DIAG_KVM_HYPERCALL:
1087 r = handle_hypercall(cpu, run);
1088 break;
1089 case DIAG_KVM_BREAKPOINT:
1090 r = handle_sw_breakpoint(cpu, run);
1091 break;
1092 default:
1093 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1094 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1095 break;
1098 return r;
1101 static void sigp_cpu_start(void *arg)
1103 CPUState *cs = arg;
1104 S390CPU *cpu = S390_CPU(cs);
1106 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1107 DPRINTF("DONE: KVM cpu start: %p\n", &cpu->env);
1110 static void sigp_cpu_restart(void *arg)
1112 CPUState *cs = arg;
1113 S390CPU *cpu = S390_CPU(cs);
1114 struct kvm_s390_irq irq = {
1115 .type = KVM_S390_RESTART,
1118 kvm_s390_vcpu_interrupt(cpu, &irq);
1119 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1122 int kvm_s390_cpu_restart(S390CPU *cpu)
1124 run_on_cpu(CPU(cpu), sigp_cpu_restart, CPU(cpu));
1125 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1126 return 0;
1129 static void sigp_initial_cpu_reset(void *arg)
1131 CPUState *cpu = arg;
1132 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1134 cpu_synchronize_state(cpu);
1135 scc->initial_cpu_reset(cpu);
1136 cpu_synchronize_post_reset(cpu);
1139 static void sigp_cpu_reset(void *arg)
1141 CPUState *cpu = arg;
1142 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1144 cpu_synchronize_state(cpu);
1145 scc->cpu_reset(cpu);
1146 cpu_synchronize_post_reset(cpu);
1149 #define SIGP_ORDER_MASK 0x000000ff
1151 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1153 CPUS390XState *env = &cpu->env;
1154 uint8_t order_code;
1155 uint16_t cpu_addr;
1156 S390CPU *target_cpu;
1157 uint64_t *statusreg = &env->regs[ipa1 >> 4];
1158 int cc;
1160 cpu_synchronize_state(CPU(cpu));
1162 /* get order code */
1163 order_code = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
1165 cpu_addr = env->regs[ipa1 & 0x0f];
1166 target_cpu = s390_cpu_addr2state(cpu_addr);
1167 if (target_cpu == NULL) {
1168 cc = 3; /* not operational */
1169 goto out;
1172 switch (order_code) {
1173 case SIGP_START:
1174 run_on_cpu(CPU(target_cpu), sigp_cpu_start, CPU(target_cpu));
1175 cc = 0;
1176 break;
1177 case SIGP_RESTART:
1178 run_on_cpu(CPU(target_cpu), sigp_cpu_restart, CPU(target_cpu));
1179 cc = 0;
1180 break;
1181 case SIGP_SET_ARCH:
1182 *statusreg &= 0xffffffff00000000UL;
1183 *statusreg |= SIGP_STAT_INVALID_PARAMETER;
1184 cc = 1; /* status stored */
1185 break;
1186 case SIGP_INITIAL_CPU_RESET:
1187 run_on_cpu(CPU(target_cpu), sigp_initial_cpu_reset, CPU(target_cpu));
1188 cc = 0;
1189 break;
1190 case SIGP_CPU_RESET:
1191 run_on_cpu(CPU(target_cpu), sigp_cpu_reset, CPU(target_cpu));
1192 cc = 0;
1193 break;
1194 default:
1195 DPRINTF("KVM: unknown SIGP: 0x%x\n", order_code);
1196 *statusreg &= 0xffffffff00000000UL;
1197 *statusreg |= SIGP_STAT_INVALID_ORDER;
1198 cc = 1; /* status stored */
1199 break;
1202 out:
1203 setcc(cpu, cc);
1204 return 0;
1207 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1209 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1210 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1211 int r = -1;
1213 DPRINTF("handle_instruction 0x%x 0x%x\n",
1214 run->s390_sieic.ipa, run->s390_sieic.ipb);
1215 switch (ipa0) {
1216 case IPA0_B2:
1217 r = handle_b2(cpu, run, ipa1);
1218 break;
1219 case IPA0_B9:
1220 r = handle_b9(cpu, run, ipa1);
1221 break;
1222 case IPA0_EB:
1223 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1224 break;
1225 case IPA0_E3:
1226 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1227 break;
1228 case IPA0_DIAG:
1229 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1230 break;
1231 case IPA0_SIGP:
1232 r = handle_sigp(cpu, run, ipa1);
1233 break;
1236 if (r < 0) {
1237 r = 0;
1238 enter_pgmcheck(cpu, 0x0001);
1241 return r;
1244 static bool is_special_wait_psw(CPUState *cs)
1246 /* signal quiesce */
1247 return cs->kvm_run->psw_addr == 0xfffUL;
1250 static void guest_panicked(void)
1252 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1253 &error_abort);
1254 vm_stop(RUN_STATE_GUEST_PANICKED);
1257 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1259 CPUState *cs = CPU(cpu);
1261 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1262 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1263 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1264 s390_cpu_halt(cpu);
1265 guest_panicked();
1268 static int handle_intercept(S390CPU *cpu)
1270 CPUState *cs = CPU(cpu);
1271 struct kvm_run *run = cs->kvm_run;
1272 int icpt_code = run->s390_sieic.icptcode;
1273 int r = 0;
1275 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1276 (long)cs->kvm_run->psw_addr);
1277 switch (icpt_code) {
1278 case ICPT_INSTRUCTION:
1279 r = handle_instruction(cpu, run);
1280 break;
1281 case ICPT_PROGRAM:
1282 unmanageable_intercept(cpu, "program interrupt",
1283 offsetof(LowCore, program_new_psw));
1284 r = EXCP_HALTED;
1285 break;
1286 case ICPT_EXT_INT:
1287 unmanageable_intercept(cpu, "external interrupt",
1288 offsetof(LowCore, external_new_psw));
1289 r = EXCP_HALTED;
1290 break;
1291 case ICPT_WAITPSW:
1292 /* disabled wait, since enabled wait is handled in kernel */
1293 cpu_synchronize_state(cs);
1294 if (s390_cpu_halt(cpu) == 0) {
1295 if (is_special_wait_psw(cs)) {
1296 qemu_system_shutdown_request();
1297 } else {
1298 guest_panicked();
1301 r = EXCP_HALTED;
1302 break;
1303 case ICPT_CPU_STOP:
1304 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1305 qemu_system_shutdown_request();
1307 r = EXCP_HALTED;
1308 break;
1309 case ICPT_SOFT_INTERCEPT:
1310 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1311 exit(1);
1312 break;
1313 case ICPT_IO:
1314 fprintf(stderr, "KVM unimplemented icpt IO\n");
1315 exit(1);
1316 break;
1317 default:
1318 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1319 exit(1);
1320 break;
1323 return r;
1326 static int handle_tsch(S390CPU *cpu)
1328 CPUS390XState *env = &cpu->env;
1329 CPUState *cs = CPU(cpu);
1330 struct kvm_run *run = cs->kvm_run;
1331 int ret;
1333 cpu_synchronize_state(cs);
1335 ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
1336 if (ret >= 0) {
1337 /* Success; set condition code. */
1338 setcc(cpu, ret);
1339 ret = 0;
1340 } else if (ret < -1) {
1342 * Failure.
1343 * If an I/O interrupt had been dequeued, we have to reinject it.
1345 if (run->s390_tsch.dequeued) {
1346 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1347 run->s390_tsch.subchannel_nr,
1348 run->s390_tsch.io_int_parm,
1349 run->s390_tsch.io_int_word);
1351 ret = 0;
1353 return ret;
1356 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1358 CPUState *cs = CPU(cpu);
1359 struct kvm_run *run = cs->kvm_run;
1361 int ret = 0;
1362 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1364 switch (arch_info->type) {
1365 case KVM_HW_WP_WRITE:
1366 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1367 cs->watchpoint_hit = &hw_watchpoint;
1368 hw_watchpoint.vaddr = arch_info->addr;
1369 hw_watchpoint.flags = BP_MEM_WRITE;
1370 ret = EXCP_DEBUG;
1372 break;
1373 case KVM_HW_BP:
1374 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1375 ret = EXCP_DEBUG;
1377 break;
1378 case KVM_SINGLESTEP:
1379 if (cs->singlestep_enabled) {
1380 ret = EXCP_DEBUG;
1382 break;
1383 default:
1384 ret = -ENOSYS;
1387 return ret;
1390 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1392 S390CPU *cpu = S390_CPU(cs);
1393 int ret = 0;
1395 switch (run->exit_reason) {
1396 case KVM_EXIT_S390_SIEIC:
1397 ret = handle_intercept(cpu);
1398 break;
1399 case KVM_EXIT_S390_RESET:
1400 qemu_system_reset_request();
1401 break;
1402 case KVM_EXIT_S390_TSCH:
1403 ret = handle_tsch(cpu);
1404 break;
1405 case KVM_EXIT_DEBUG:
1406 ret = kvm_arch_handle_debug_exit(cpu);
1407 break;
1408 default:
1409 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1410 break;
1413 if (ret == 0) {
1414 ret = EXCP_INTERRUPT;
1416 return ret;
1419 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1421 return true;
1424 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
1426 return 1;
1429 int kvm_arch_on_sigbus(int code, void *addr)
1431 return 1;
1434 void kvm_s390_io_interrupt(uint16_t subchannel_id,
1435 uint16_t subchannel_nr, uint32_t io_int_parm,
1436 uint32_t io_int_word)
1438 struct kvm_s390_irq irq = {
1439 .u.io.subchannel_id = subchannel_id,
1440 .u.io.subchannel_nr = subchannel_nr,
1441 .u.io.io_int_parm = io_int_parm,
1442 .u.io.io_int_word = io_int_word,
1445 if (io_int_word & IO_INT_WORD_AI) {
1446 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
1447 } else {
1448 irq.type = ((subchannel_id & 0xff00) << 24) |
1449 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
1451 kvm_s390_floating_interrupt(&irq);
1454 void kvm_s390_crw_mchk(void)
1456 struct kvm_s390_irq irq = {
1457 .type = KVM_S390_MCHK,
1458 .u.mchk.cr14 = 1 << 28,
1459 .u.mchk.mcic = 0x00400f1d40330000ULL,
1461 kvm_s390_floating_interrupt(&irq);
1464 void kvm_s390_enable_css_support(S390CPU *cpu)
1466 int r;
1468 /* Activate host kernel channel subsystem support. */
1469 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1470 assert(r == 0);
1473 void kvm_arch_init_irq_routing(KVMState *s)
1476 * Note that while irqchip capabilities generally imply that cpustates
1477 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1478 * have to override the common code kvm_halt_in_kernel_allowed setting.
1480 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1481 kvm_gsi_routing_allowed = true;
1482 kvm_halt_in_kernel_allowed = false;
1486 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1487 int vq, bool assign)
1489 struct kvm_ioeventfd kick = {
1490 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1491 KVM_IOEVENTFD_FLAG_DATAMATCH,
1492 .fd = event_notifier_get_fd(notifier),
1493 .datamatch = vq,
1494 .addr = sch,
1495 .len = 8,
1497 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1498 return -ENOSYS;
1500 if (!assign) {
1501 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1503 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1506 int kvm_s390_get_memslot_count(KVMState *s)
1508 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
1511 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1513 struct kvm_mp_state mp_state = {};
1514 int ret;
1516 /* the kvm part might not have been initialized yet */
1517 if (CPU(cpu)->kvm_state == NULL) {
1518 return 0;
1521 switch (cpu_state) {
1522 case CPU_STATE_STOPPED:
1523 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1524 break;
1525 case CPU_STATE_CHECK_STOP:
1526 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1527 break;
1528 case CPU_STATE_OPERATING:
1529 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1530 break;
1531 case CPU_STATE_LOAD:
1532 mp_state.mp_state = KVM_MP_STATE_LOAD;
1533 break;
1534 default:
1535 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1536 cpu_state);
1537 exit(1);
1540 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1541 if (ret) {
1542 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1543 strerror(-ret));
1546 return ret;
1549 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1550 uint64_t address, uint32_t data)
1552 S390PCIBusDevice *pbdev;
1553 uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
1554 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
1556 pbdev = s390_pci_find_dev_by_fid(fid);
1557 if (!pbdev) {
1558 DPRINTF("add_msi_route no dev\n");
1559 return -ENODEV;
1562 pbdev->routes.adapter.ind_offset = vec;
1564 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
1565 route->flags = 0;
1566 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
1567 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
1568 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
1569 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
1570 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
1571 return 0;