s390x/kvm: Log unmanageable program interruptions
[qemu/ar7.git] / target-s390x / kvm.c
blob9adda1b46bfeedaa964fc07516023ad58e0f3fcc
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"
43 /* #define DEBUG_KVM */
45 #ifdef DEBUG_KVM
46 #define DPRINTF(fmt, ...) \
47 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
48 #else
49 #define DPRINTF(fmt, ...) \
50 do { } while (0)
51 #endif
53 #define IPA0_DIAG 0x8300
54 #define IPA0_SIGP 0xae00
55 #define IPA0_B2 0xb200
56 #define IPA0_B9 0xb900
57 #define IPA0_EB 0xeb00
59 #define PRIV_B2_SCLP_CALL 0x20
60 #define PRIV_B2_CSCH 0x30
61 #define PRIV_B2_HSCH 0x31
62 #define PRIV_B2_MSCH 0x32
63 #define PRIV_B2_SSCH 0x33
64 #define PRIV_B2_STSCH 0x34
65 #define PRIV_B2_TSCH 0x35
66 #define PRIV_B2_TPI 0x36
67 #define PRIV_B2_SAL 0x37
68 #define PRIV_B2_RSCH 0x38
69 #define PRIV_B2_STCRW 0x39
70 #define PRIV_B2_STCPS 0x3a
71 #define PRIV_B2_RCHP 0x3b
72 #define PRIV_B2_SCHM 0x3c
73 #define PRIV_B2_CHSC 0x5f
74 #define PRIV_B2_SIGA 0x74
75 #define PRIV_B2_XSCH 0x76
77 #define PRIV_EB_SQBS 0x8a
79 #define PRIV_B9_EQBS 0x9c
81 #define DIAG_IPL 0x308
82 #define DIAG_KVM_HYPERCALL 0x500
83 #define DIAG_KVM_BREAKPOINT 0x501
85 #define ICPT_INSTRUCTION 0x04
86 #define ICPT_PROGRAM 0x08
87 #define ICPT_EXT_INT 0x14
88 #define ICPT_WAITPSW 0x1c
89 #define ICPT_SOFT_INTERCEPT 0x24
90 #define ICPT_CPU_STOP 0x28
91 #define ICPT_IO 0x40
93 static CPUWatchpoint hw_watchpoint;
95 * We don't use a list because this structure is also used to transmit the
96 * hardware breakpoints to the kernel.
98 static struct kvm_hw_breakpoint *hw_breakpoints;
99 static int nb_hw_breakpoints;
101 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
102 KVM_CAP_LAST_INFO
105 static int cap_sync_regs;
106 static int cap_async_pf;
108 static void *legacy_s390_alloc(size_t size);
110 static int kvm_s390_check_clear_cmma(KVMState *s)
112 struct kvm_device_attr attr = {
113 .group = KVM_S390_VM_MEM_CTRL,
114 .attr = KVM_S390_VM_MEM_CLR_CMMA,
117 return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
120 static int kvm_s390_check_enable_cmma(KVMState *s)
122 struct kvm_device_attr attr = {
123 .group = KVM_S390_VM_MEM_CTRL,
124 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
127 return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
130 void kvm_s390_clear_cmma_callback(void *opaque)
132 int rc;
133 KVMState *s = opaque;
134 struct kvm_device_attr attr = {
135 .group = KVM_S390_VM_MEM_CTRL,
136 .attr = KVM_S390_VM_MEM_CLR_CMMA,
139 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
140 trace_kvm_clear_cmma(rc);
143 static void kvm_s390_enable_cmma(KVMState *s)
145 int rc;
146 struct kvm_device_attr attr = {
147 .group = KVM_S390_VM_MEM_CTRL,
148 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
151 if (kvm_s390_check_enable_cmma(s) || kvm_s390_check_clear_cmma(s)) {
152 return;
155 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
156 if (!rc) {
157 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
159 trace_kvm_enable_cmma(rc);
162 int kvm_arch_init(KVMState *s)
164 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
165 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
167 if (kvm_check_extension(s, KVM_CAP_VM_ATTRIBUTES)) {
168 kvm_s390_enable_cmma(s);
171 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
172 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
173 phys_mem_set_alloc(legacy_s390_alloc);
175 return 0;
178 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
180 return cpu->cpu_index;
183 int kvm_arch_init_vcpu(CPUState *cpu)
185 /* nothing todo yet */
186 return 0;
189 void kvm_s390_reset_vcpu(S390CPU *cpu)
191 CPUState *cs = CPU(cpu);
193 /* The initial reset call is needed here to reset in-kernel
194 * vcpu data that we can't access directly from QEMU
195 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
196 * Before this ioctl cpu_synchronize_state() is called in common kvm
197 * code (kvm-all) */
198 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
199 perror("Can't reset vcpu\n");
203 int kvm_arch_put_registers(CPUState *cs, int level)
205 S390CPU *cpu = S390_CPU(cs);
206 CPUS390XState *env = &cpu->env;
207 struct kvm_sregs sregs;
208 struct kvm_regs regs;
209 int r;
210 int i;
212 /* always save the PSW and the GPRS*/
213 cs->kvm_run->psw_addr = env->psw.addr;
214 cs->kvm_run->psw_mask = env->psw.mask;
216 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
217 for (i = 0; i < 16; i++) {
218 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
219 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
221 } else {
222 for (i = 0; i < 16; i++) {
223 regs.gprs[i] = env->regs[i];
225 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
226 if (r < 0) {
227 return r;
231 /* Do we need to save more than that? */
232 if (level == KVM_PUT_RUNTIME_STATE) {
233 return 0;
237 * These ONE_REGS are not protected by a capability. As they are only
238 * necessary for migration we just trace a possible error, but don't
239 * return with an error return code.
241 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
242 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
243 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
244 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
245 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
247 if (cap_async_pf) {
248 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
249 if (r < 0) {
250 return r;
252 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
253 if (r < 0) {
254 return r;
256 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
257 if (r < 0) {
258 return r;
262 if (cap_sync_regs &&
263 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
264 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
265 for (i = 0; i < 16; i++) {
266 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
267 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
269 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
270 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
271 } else {
272 for (i = 0; i < 16; i++) {
273 sregs.acrs[i] = env->aregs[i];
274 sregs.crs[i] = env->cregs[i];
276 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
277 if (r < 0) {
278 return r;
282 /* Finally the prefix */
283 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
284 cs->kvm_run->s.regs.prefix = env->psa;
285 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
286 } else {
287 /* prefix is only supported via sync regs */
289 return 0;
292 int kvm_arch_get_registers(CPUState *cs)
294 S390CPU *cpu = S390_CPU(cs);
295 CPUS390XState *env = &cpu->env;
296 struct kvm_sregs sregs;
297 struct kvm_regs regs;
298 int i, r;
300 /* get the PSW */
301 env->psw.addr = cs->kvm_run->psw_addr;
302 env->psw.mask = cs->kvm_run->psw_mask;
304 /* the GPRS */
305 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
306 for (i = 0; i < 16; i++) {
307 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
309 } else {
310 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
311 if (r < 0) {
312 return r;
314 for (i = 0; i < 16; i++) {
315 env->regs[i] = regs.gprs[i];
319 /* The ACRS and CRS */
320 if (cap_sync_regs &&
321 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
322 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
323 for (i = 0; i < 16; i++) {
324 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
325 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
327 } else {
328 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
329 if (r < 0) {
330 return r;
332 for (i = 0; i < 16; i++) {
333 env->aregs[i] = sregs.acrs[i];
334 env->cregs[i] = sregs.crs[i];
338 /* The prefix */
339 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
340 env->psa = cs->kvm_run->s.regs.prefix;
344 * These ONE_REGS are not protected by a capability. As they are only
345 * necessary for migration we just trace a possible error, but don't
346 * return with an error return code.
348 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
349 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
350 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
351 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
352 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
354 if (cap_async_pf) {
355 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
356 if (r < 0) {
357 return r;
359 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
360 if (r < 0) {
361 return r;
363 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
364 if (r < 0) {
365 return r;
369 return 0;
373 * Legacy layout for s390:
374 * Older S390 KVM requires the topmost vma of the RAM to be
375 * smaller than an system defined value, which is at least 256GB.
376 * Larger systems have larger values. We put the guest between
377 * the end of data segment (system break) and this value. We
378 * use 32GB as a base to have enough room for the system break
379 * to grow. We also have to use MAP parameters that avoid
380 * read-only mapping of guest pages.
382 static void *legacy_s390_alloc(size_t size)
384 void *mem;
386 mem = mmap((void *) 0x800000000ULL, size,
387 PROT_EXEC|PROT_READ|PROT_WRITE,
388 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
389 return mem == MAP_FAILED ? NULL : mem;
392 /* DIAG 501 is used for sw breakpoints */
393 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
395 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
398 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
399 sizeof(diag_501), 0) ||
400 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
401 sizeof(diag_501), 1)) {
402 return -EINVAL;
404 return 0;
407 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
409 uint8_t t[sizeof(diag_501)];
411 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
412 return -EINVAL;
413 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
414 return -EINVAL;
415 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
416 sizeof(diag_501), 1)) {
417 return -EINVAL;
420 return 0;
423 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
424 int len, int type)
426 int n;
428 for (n = 0; n < nb_hw_breakpoints; n++) {
429 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
430 (hw_breakpoints[n].len == len || len == -1)) {
431 return &hw_breakpoints[n];
435 return NULL;
438 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
440 int size;
442 if (find_hw_breakpoint(addr, len, type)) {
443 return -EEXIST;
446 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
448 if (!hw_breakpoints) {
449 nb_hw_breakpoints = 0;
450 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
451 } else {
452 hw_breakpoints =
453 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
456 if (!hw_breakpoints) {
457 nb_hw_breakpoints = 0;
458 return -ENOMEM;
461 hw_breakpoints[nb_hw_breakpoints].addr = addr;
462 hw_breakpoints[nb_hw_breakpoints].len = len;
463 hw_breakpoints[nb_hw_breakpoints].type = type;
465 nb_hw_breakpoints++;
467 return 0;
470 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
471 target_ulong len, int type)
473 switch (type) {
474 case GDB_BREAKPOINT_HW:
475 type = KVM_HW_BP;
476 break;
477 case GDB_WATCHPOINT_WRITE:
478 if (len < 1) {
479 return -EINVAL;
481 type = KVM_HW_WP_WRITE;
482 break;
483 default:
484 return -ENOSYS;
486 return insert_hw_breakpoint(addr, len, type);
489 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
490 target_ulong len, int type)
492 int size;
493 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
495 if (bp == NULL) {
496 return -ENOENT;
499 nb_hw_breakpoints--;
500 if (nb_hw_breakpoints > 0) {
502 * In order to trim the array, move the last element to the position to
503 * be removed - if necessary.
505 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
506 *bp = hw_breakpoints[nb_hw_breakpoints];
508 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
509 hw_breakpoints =
510 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
511 } else {
512 g_free(hw_breakpoints);
513 hw_breakpoints = NULL;
516 return 0;
519 void kvm_arch_remove_all_hw_breakpoints(void)
521 nb_hw_breakpoints = 0;
522 g_free(hw_breakpoints);
523 hw_breakpoints = NULL;
526 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
528 int i;
530 if (nb_hw_breakpoints > 0) {
531 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
532 dbg->arch.hw_bp = hw_breakpoints;
534 for (i = 0; i < nb_hw_breakpoints; ++i) {
535 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
536 hw_breakpoints[i].addr);
538 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
539 } else {
540 dbg->arch.nr_hw_bp = 0;
541 dbg->arch.hw_bp = NULL;
545 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
549 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
553 int kvm_arch_process_async_events(CPUState *cs)
555 return cs->halted;
558 void kvm_s390_interrupt_internal(S390CPU *cpu, int type, uint32_t parm,
559 uint64_t parm64, int vm)
561 CPUState *cs = CPU(cpu);
562 struct kvm_s390_interrupt kvmint;
563 int r;
565 if (!cs->kvm_state) {
566 return;
569 kvmint.type = type;
570 kvmint.parm = parm;
571 kvmint.parm64 = parm64;
573 if (vm) {
574 r = kvm_vm_ioctl(cs->kvm_state, KVM_S390_INTERRUPT, &kvmint);
575 } else {
576 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
579 if (r < 0) {
580 fprintf(stderr, "KVM failed to inject interrupt\n");
581 exit(1);
585 void kvm_s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token)
587 kvm_s390_interrupt_internal(cpu, KVM_S390_INT_VIRTIO, config_change,
588 token, 1);
591 void kvm_s390_interrupt(S390CPU *cpu, int type, uint32_t code)
593 kvm_s390_interrupt_internal(cpu, type, code, 0, 0);
596 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
598 kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code);
601 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
602 uint16_t ipbh0)
604 CPUS390XState *env = &cpu->env;
605 uint64_t sccb;
606 uint32_t code;
607 int r = 0;
609 cpu_synchronize_state(CPU(cpu));
610 sccb = env->regs[ipbh0 & 0xf];
611 code = env->regs[(ipbh0 & 0xf0) >> 4];
613 r = sclp_service_call(env, sccb, code);
614 if (r < 0) {
615 enter_pgmcheck(cpu, -r);
616 } else {
617 setcc(cpu, r);
620 return 0;
623 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
625 CPUS390XState *env = &cpu->env;
626 int rc = 0;
627 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
629 cpu_synchronize_state(CPU(cpu));
631 switch (ipa1) {
632 case PRIV_B2_XSCH:
633 ioinst_handle_xsch(cpu, env->regs[1]);
634 break;
635 case PRIV_B2_CSCH:
636 ioinst_handle_csch(cpu, env->regs[1]);
637 break;
638 case PRIV_B2_HSCH:
639 ioinst_handle_hsch(cpu, env->regs[1]);
640 break;
641 case PRIV_B2_MSCH:
642 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
643 break;
644 case PRIV_B2_SSCH:
645 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
646 break;
647 case PRIV_B2_STCRW:
648 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
649 break;
650 case PRIV_B2_STSCH:
651 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
652 break;
653 case PRIV_B2_TSCH:
654 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
655 fprintf(stderr, "Spurious tsch intercept\n");
656 break;
657 case PRIV_B2_CHSC:
658 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
659 break;
660 case PRIV_B2_TPI:
661 /* This should have been handled by kvm already. */
662 fprintf(stderr, "Spurious tpi intercept\n");
663 break;
664 case PRIV_B2_SCHM:
665 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
666 run->s390_sieic.ipb);
667 break;
668 case PRIV_B2_RSCH:
669 ioinst_handle_rsch(cpu, env->regs[1]);
670 break;
671 case PRIV_B2_RCHP:
672 ioinst_handle_rchp(cpu, env->regs[1]);
673 break;
674 case PRIV_B2_STCPS:
675 /* We do not provide this instruction, it is suppressed. */
676 break;
677 case PRIV_B2_SAL:
678 ioinst_handle_sal(cpu, env->regs[1]);
679 break;
680 case PRIV_B2_SIGA:
681 /* Not provided, set CC = 3 for subchannel not operational */
682 setcc(cpu, 3);
683 break;
684 case PRIV_B2_SCLP_CALL:
685 rc = kvm_sclp_service_call(cpu, run, ipbh0);
686 break;
687 default:
688 rc = -1;
689 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
690 break;
693 return rc;
696 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
698 int r = 0;
700 switch (ipa1) {
701 case PRIV_B9_EQBS:
702 /* just inject exception */
703 r = -1;
704 break;
705 default:
706 r = -1;
707 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
708 break;
711 return r;
714 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
716 int r = 0;
718 switch (ipa1) {
719 case PRIV_EB_SQBS:
720 /* just inject exception */
721 r = -1;
722 break;
723 default:
724 r = -1;
725 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipa1);
726 break;
729 return r;
732 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
734 CPUS390XState *env = &cpu->env;
735 int ret;
737 cpu_synchronize_state(CPU(cpu));
738 ret = s390_virtio_hypercall(env);
739 if (ret == -EINVAL) {
740 enter_pgmcheck(cpu, PGM_SPECIFICATION);
741 return 0;
744 return ret;
747 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
749 uint64_t r1, r3;
751 cpu_synchronize_state(CPU(cpu));
752 r1 = (run->s390_sieic.ipa & 0x00f0) >> 8;
753 r3 = run->s390_sieic.ipa & 0x000f;
754 handle_diag_308(&cpu->env, r1, r3);
757 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
759 CPUS390XState *env = &cpu->env;
760 unsigned long pc;
762 cpu_synchronize_state(CPU(cpu));
764 pc = env->psw.addr - 4;
765 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
766 env->psw.addr = pc;
767 return EXCP_DEBUG;
770 return -ENOENT;
773 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
775 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
777 int r = 0;
778 uint16_t func_code;
781 * For any diagnose call we support, bits 48-63 of the resulting
782 * address specify the function code; the remainder is ignored.
784 func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
785 switch (func_code) {
786 case DIAG_IPL:
787 kvm_handle_diag_308(cpu, run);
788 break;
789 case DIAG_KVM_HYPERCALL:
790 r = handle_hypercall(cpu, run);
791 break;
792 case DIAG_KVM_BREAKPOINT:
793 r = handle_sw_breakpoint(cpu, run);
794 break;
795 default:
796 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
797 r = -1;
798 break;
801 return r;
804 static int kvm_s390_cpu_start(S390CPU *cpu)
806 s390_add_running_cpu(cpu);
807 qemu_cpu_kick(CPU(cpu));
808 DPRINTF("DONE: KVM cpu start: %p\n", &cpu->env);
809 return 0;
812 int kvm_s390_cpu_restart(S390CPU *cpu)
814 kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0);
815 s390_add_running_cpu(cpu);
816 qemu_cpu_kick(CPU(cpu));
817 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
818 return 0;
821 static void sigp_initial_cpu_reset(void *arg)
823 CPUState *cpu = arg;
824 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
826 cpu_synchronize_state(cpu);
827 scc->initial_cpu_reset(cpu);
830 static void sigp_cpu_reset(void *arg)
832 CPUState *cpu = arg;
833 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
835 cpu_synchronize_state(cpu);
836 scc->cpu_reset(cpu);
839 #define SIGP_ORDER_MASK 0x000000ff
841 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
843 CPUS390XState *env = &cpu->env;
844 uint8_t order_code;
845 uint16_t cpu_addr;
846 S390CPU *target_cpu;
847 uint64_t *statusreg = &env->regs[ipa1 >> 4];
848 int cc;
850 cpu_synchronize_state(CPU(cpu));
852 /* get order code */
853 order_code = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
855 cpu_addr = env->regs[ipa1 & 0x0f];
856 target_cpu = s390_cpu_addr2state(cpu_addr);
857 if (target_cpu == NULL) {
858 cc = 3; /* not operational */
859 goto out;
862 switch (order_code) {
863 case SIGP_START:
864 cc = kvm_s390_cpu_start(target_cpu);
865 break;
866 case SIGP_RESTART:
867 cc = kvm_s390_cpu_restart(target_cpu);
868 break;
869 case SIGP_SET_ARCH:
870 *statusreg &= 0xffffffff00000000UL;
871 *statusreg |= SIGP_STAT_INVALID_PARAMETER;
872 cc = 1; /* status stored */
873 break;
874 case SIGP_INITIAL_CPU_RESET:
875 run_on_cpu(CPU(target_cpu), sigp_initial_cpu_reset, CPU(target_cpu));
876 cc = 0;
877 break;
878 case SIGP_CPU_RESET:
879 run_on_cpu(CPU(target_cpu), sigp_cpu_reset, CPU(target_cpu));
880 cc = 0;
881 break;
882 default:
883 DPRINTF("KVM: unknown SIGP: 0x%x\n", order_code);
884 *statusreg &= 0xffffffff00000000UL;
885 *statusreg |= SIGP_STAT_INVALID_ORDER;
886 cc = 1; /* status stored */
887 break;
890 out:
891 setcc(cpu, cc);
892 return 0;
895 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
897 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
898 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
899 int r = -1;
901 DPRINTF("handle_instruction 0x%x 0x%x\n",
902 run->s390_sieic.ipa, run->s390_sieic.ipb);
903 switch (ipa0) {
904 case IPA0_B2:
905 r = handle_b2(cpu, run, ipa1);
906 break;
907 case IPA0_B9:
908 r = handle_b9(cpu, run, ipa1);
909 break;
910 case IPA0_EB:
911 r = handle_eb(cpu, run, ipa1);
912 break;
913 case IPA0_DIAG:
914 r = handle_diag(cpu, run, run->s390_sieic.ipb);
915 break;
916 case IPA0_SIGP:
917 r = handle_sigp(cpu, run, ipa1);
918 break;
921 if (r < 0) {
922 r = 0;
923 enter_pgmcheck(cpu, 0x0001);
926 return r;
929 static bool is_special_wait_psw(CPUState *cs)
931 /* signal quiesce */
932 return cs->kvm_run->psw_addr == 0xfffUL;
935 static void guest_panicked(void)
937 QObject *data;
939 data = qobject_from_jsonf("{ 'action': %s }", "pause");
940 monitor_protocol_event(QEVENT_GUEST_PANICKED, data);
941 qobject_decref(data);
943 vm_stop(RUN_STATE_GUEST_PANICKED);
946 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
948 CPUState *cs = CPU(cpu);
950 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
951 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
952 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
953 s390_del_running_cpu(cpu);
954 guest_panicked();
957 static int handle_intercept(S390CPU *cpu)
959 CPUState *cs = CPU(cpu);
960 struct kvm_run *run = cs->kvm_run;
961 int icpt_code = run->s390_sieic.icptcode;
962 int r = 0;
964 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
965 (long)cs->kvm_run->psw_addr);
966 switch (icpt_code) {
967 case ICPT_INSTRUCTION:
968 r = handle_instruction(cpu, run);
969 break;
970 case ICPT_PROGRAM:
971 unmanageable_intercept(cpu, "program interrupt",
972 offsetof(LowCore, program_new_psw));
973 r = EXCP_HALTED;
974 break;
975 case ICPT_EXT_INT:
976 unmanageable_intercept(cpu, "external interrupt",
977 offsetof(LowCore, external_new_psw));
978 r = EXCP_HALTED;
979 break;
980 case ICPT_WAITPSW:
981 /* disabled wait, since enabled wait is handled in kernel */
982 if (s390_del_running_cpu(cpu) == 0) {
983 if (is_special_wait_psw(cs)) {
984 qemu_system_shutdown_request();
985 } else {
986 guest_panicked();
989 r = EXCP_HALTED;
990 break;
991 case ICPT_CPU_STOP:
992 if (s390_del_running_cpu(cpu) == 0) {
993 qemu_system_shutdown_request();
995 r = EXCP_HALTED;
996 break;
997 case ICPT_SOFT_INTERCEPT:
998 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
999 exit(1);
1000 break;
1001 case ICPT_IO:
1002 fprintf(stderr, "KVM unimplemented icpt IO\n");
1003 exit(1);
1004 break;
1005 default:
1006 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1007 exit(1);
1008 break;
1011 return r;
1014 static int handle_tsch(S390CPU *cpu)
1016 CPUS390XState *env = &cpu->env;
1017 CPUState *cs = CPU(cpu);
1018 struct kvm_run *run = cs->kvm_run;
1019 int ret;
1021 cpu_synchronize_state(cs);
1023 ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
1024 if (ret >= 0) {
1025 /* Success; set condition code. */
1026 setcc(cpu, ret);
1027 ret = 0;
1028 } else if (ret < -1) {
1030 * Failure.
1031 * If an I/O interrupt had been dequeued, we have to reinject it.
1033 if (run->s390_tsch.dequeued) {
1034 uint16_t subchannel_id = run->s390_tsch.subchannel_id;
1035 uint16_t subchannel_nr = run->s390_tsch.subchannel_nr;
1036 uint32_t io_int_parm = run->s390_tsch.io_int_parm;
1037 uint32_t io_int_word = run->s390_tsch.io_int_word;
1038 uint32_t type = ((subchannel_id & 0xff00) << 24) |
1039 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
1041 kvm_s390_interrupt_internal(cpu, type,
1042 ((uint32_t)subchannel_id << 16)
1043 | subchannel_nr,
1044 ((uint64_t)io_int_parm << 32)
1045 | io_int_word, 1);
1047 ret = 0;
1049 return ret;
1052 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1054 CPUState *cs = CPU(cpu);
1055 struct kvm_run *run = cs->kvm_run;
1057 int ret = 0;
1058 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1060 switch (arch_info->type) {
1061 case KVM_HW_WP_WRITE:
1062 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1063 cs->watchpoint_hit = &hw_watchpoint;
1064 hw_watchpoint.vaddr = arch_info->addr;
1065 hw_watchpoint.flags = BP_MEM_WRITE;
1066 ret = EXCP_DEBUG;
1068 break;
1069 case KVM_HW_BP:
1070 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1071 ret = EXCP_DEBUG;
1073 break;
1074 case KVM_SINGLESTEP:
1075 if (cs->singlestep_enabled) {
1076 ret = EXCP_DEBUG;
1078 break;
1079 default:
1080 ret = -ENOSYS;
1083 return ret;
1086 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1088 S390CPU *cpu = S390_CPU(cs);
1089 int ret = 0;
1091 switch (run->exit_reason) {
1092 case KVM_EXIT_S390_SIEIC:
1093 ret = handle_intercept(cpu);
1094 break;
1095 case KVM_EXIT_S390_RESET:
1096 qemu_system_reset_request();
1097 break;
1098 case KVM_EXIT_S390_TSCH:
1099 ret = handle_tsch(cpu);
1100 break;
1101 case KVM_EXIT_DEBUG:
1102 ret = kvm_arch_handle_debug_exit(cpu);
1103 break;
1104 default:
1105 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1106 break;
1109 if (ret == 0) {
1110 ret = EXCP_INTERRUPT;
1112 return ret;
1115 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1117 return true;
1120 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
1122 return 1;
1125 int kvm_arch_on_sigbus(int code, void *addr)
1127 return 1;
1130 void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id,
1131 uint16_t subchannel_nr, uint32_t io_int_parm,
1132 uint32_t io_int_word)
1134 uint32_t type;
1136 if (io_int_word & IO_INT_WORD_AI) {
1137 type = KVM_S390_INT_IO(1, 0, 0, 0);
1138 } else {
1139 type = ((subchannel_id & 0xff00) << 24) |
1140 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
1142 kvm_s390_interrupt_internal(cpu, type,
1143 ((uint32_t)subchannel_id << 16) | subchannel_nr,
1144 ((uint64_t)io_int_parm << 32) | io_int_word, 1);
1147 void kvm_s390_crw_mchk(S390CPU *cpu)
1149 kvm_s390_interrupt_internal(cpu, KVM_S390_MCHK, 1 << 28,
1150 0x00400f1d40330000, 1);
1153 void kvm_s390_enable_css_support(S390CPU *cpu)
1155 int r;
1157 /* Activate host kernel channel subsystem support. */
1158 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1159 assert(r == 0);
1162 void kvm_arch_init_irq_routing(KVMState *s)
1165 * Note that while irqchip capabilities generally imply that cpustates
1166 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1167 * have to override the common code kvm_halt_in_kernel_allowed setting.
1169 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1170 kvm_irqfds_allowed = true;
1171 kvm_gsi_routing_allowed = true;
1172 kvm_halt_in_kernel_allowed = false;
1176 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1177 int vq, bool assign)
1179 struct kvm_ioeventfd kick = {
1180 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1181 KVM_IOEVENTFD_FLAG_DATAMATCH,
1182 .fd = event_notifier_get_fd(notifier),
1183 .datamatch = vq,
1184 .addr = sch,
1185 .len = 8,
1187 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1188 return -ENOSYS;
1190 if (!assign) {
1191 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1193 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);