Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu.git] / target-s390x / kvm.c
blob896534bf95a269d0bc3686bd7939906063975c9a
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 "exec/address-spaces.h"
42 #include "trace.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"
48 /* #define DEBUG_KVM */
50 #ifdef DEBUG_KVM
51 #define DPRINTF(fmt, ...) \
52 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
53 #else
54 #define DPRINTF(fmt, ...) \
55 do { } while (0)
56 #endif
58 #define IPA0_DIAG 0x8300
59 #define IPA0_SIGP 0xae00
60 #define IPA0_B2 0xb200
61 #define IPA0_B9 0xb900
62 #define IPA0_EB 0xeb00
63 #define IPA0_E3 0xe300
65 #define PRIV_B2_SCLP_CALL 0x20
66 #define PRIV_B2_CSCH 0x30
67 #define PRIV_B2_HSCH 0x31
68 #define PRIV_B2_MSCH 0x32
69 #define PRIV_B2_SSCH 0x33
70 #define PRIV_B2_STSCH 0x34
71 #define PRIV_B2_TSCH 0x35
72 #define PRIV_B2_TPI 0x36
73 #define PRIV_B2_SAL 0x37
74 #define PRIV_B2_RSCH 0x38
75 #define PRIV_B2_STCRW 0x39
76 #define PRIV_B2_STCPS 0x3a
77 #define PRIV_B2_RCHP 0x3b
78 #define PRIV_B2_SCHM 0x3c
79 #define PRIV_B2_CHSC 0x5f
80 #define PRIV_B2_SIGA 0x74
81 #define PRIV_B2_XSCH 0x76
83 #define PRIV_EB_SQBS 0x8a
84 #define PRIV_EB_PCISTB 0xd0
85 #define PRIV_EB_SIC 0xd1
87 #define PRIV_B9_EQBS 0x9c
88 #define PRIV_B9_CLP 0xa0
89 #define PRIV_B9_PCISTG 0xd0
90 #define PRIV_B9_PCILG 0xd2
91 #define PRIV_B9_RPCIT 0xd3
93 #define PRIV_E3_MPCIFC 0xd0
94 #define PRIV_E3_STPCIFC 0xd4
96 #define DIAG_IPL 0x308
97 #define DIAG_KVM_HYPERCALL 0x500
98 #define DIAG_KVM_BREAKPOINT 0x501
100 #define ICPT_INSTRUCTION 0x04
101 #define ICPT_PROGRAM 0x08
102 #define ICPT_EXT_INT 0x14
103 #define ICPT_WAITPSW 0x1c
104 #define ICPT_SOFT_INTERCEPT 0x24
105 #define ICPT_CPU_STOP 0x28
106 #define ICPT_IO 0x40
108 static CPUWatchpoint hw_watchpoint;
110 * We don't use a list because this structure is also used to transmit the
111 * hardware breakpoints to the kernel.
113 static struct kvm_hw_breakpoint *hw_breakpoints;
114 static int nb_hw_breakpoints;
116 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
117 KVM_CAP_LAST_INFO
120 static int cap_sync_regs;
121 static int cap_async_pf;
123 static void *legacy_s390_alloc(size_t size, uint64_t *align);
125 static int kvm_s390_supports_mem_limit(KVMState *s)
127 struct kvm_device_attr attr = {
128 .group = KVM_S390_VM_MEM_CTRL,
129 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
132 return (kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr) == 0);
135 static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
137 struct kvm_device_attr attr = {
138 .group = KVM_S390_VM_MEM_CTRL,
139 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
140 .addr = (uint64_t) memory_limit,
143 return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
146 int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
148 int rc;
150 struct kvm_device_attr attr = {
151 .group = KVM_S390_VM_MEM_CTRL,
152 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
153 .addr = (uint64_t) &new_limit,
156 if (!kvm_s390_supports_mem_limit(s)) {
157 return 0;
160 rc = kvm_s390_query_mem_limit(s, hw_limit);
161 if (rc) {
162 return rc;
163 } else if (*hw_limit < new_limit) {
164 return -E2BIG;
167 return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
170 static int kvm_s390_check_clear_cmma(KVMState *s)
172 struct kvm_device_attr attr = {
173 .group = KVM_S390_VM_MEM_CTRL,
174 .attr = KVM_S390_VM_MEM_CLR_CMMA,
177 return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
180 static int kvm_s390_check_enable_cmma(KVMState *s)
182 struct kvm_device_attr attr = {
183 .group = KVM_S390_VM_MEM_CTRL,
184 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
187 return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
190 void kvm_s390_clear_cmma_callback(void *opaque)
192 int rc;
193 KVMState *s = opaque;
194 struct kvm_device_attr attr = {
195 .group = KVM_S390_VM_MEM_CTRL,
196 .attr = KVM_S390_VM_MEM_CLR_CMMA,
199 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
200 trace_kvm_clear_cmma(rc);
203 static void kvm_s390_enable_cmma(KVMState *s)
205 int rc;
206 struct kvm_device_attr attr = {
207 .group = KVM_S390_VM_MEM_CTRL,
208 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
211 if (kvm_s390_check_enable_cmma(s) || kvm_s390_check_clear_cmma(s)) {
212 return;
215 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
216 if (!rc) {
217 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
219 trace_kvm_enable_cmma(rc);
222 int kvm_arch_init(MachineState *ms, KVMState *s)
224 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
225 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
227 if (kvm_check_extension(s, KVM_CAP_VM_ATTRIBUTES)) {
228 kvm_s390_enable_cmma(s);
231 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
232 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
233 phys_mem_set_alloc(legacy_s390_alloc);
236 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
238 return 0;
241 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
243 return cpu->cpu_index;
246 int kvm_arch_init_vcpu(CPUState *cs)
248 S390CPU *cpu = S390_CPU(cs);
249 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
250 return 0;
253 void kvm_s390_reset_vcpu(S390CPU *cpu)
255 CPUState *cs = CPU(cpu);
257 /* The initial reset call is needed here to reset in-kernel
258 * vcpu data that we can't access directly from QEMU
259 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
260 * Before this ioctl cpu_synchronize_state() is called in common kvm
261 * code (kvm-all) */
262 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
263 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
267 static int can_sync_regs(CPUState *cs, int regs)
269 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
272 int kvm_arch_put_registers(CPUState *cs, int level)
274 S390CPU *cpu = S390_CPU(cs);
275 CPUS390XState *env = &cpu->env;
276 struct kvm_sregs sregs;
277 struct kvm_regs regs;
278 struct kvm_fpu fpu = {};
279 int r;
280 int i;
282 /* always save the PSW and the GPRS*/
283 cs->kvm_run->psw_addr = env->psw.addr;
284 cs->kvm_run->psw_mask = env->psw.mask;
286 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
287 for (i = 0; i < 16; i++) {
288 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
289 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
291 } else {
292 for (i = 0; i < 16; i++) {
293 regs.gprs[i] = env->regs[i];
295 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
296 if (r < 0) {
297 return r;
301 /* Floating point */
302 for (i = 0; i < 16; i++) {
303 fpu.fprs[i] = env->fregs[i].ll;
305 fpu.fpc = env->fpc;
307 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
308 if (r < 0) {
309 return r;
312 /* Do we need to save more than that? */
313 if (level == KVM_PUT_RUNTIME_STATE) {
314 return 0;
317 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
318 cs->kvm_run->s.regs.cputm = env->cputm;
319 cs->kvm_run->s.regs.ckc = env->ckc;
320 cs->kvm_run->s.regs.todpr = env->todpr;
321 cs->kvm_run->s.regs.gbea = env->gbea;
322 cs->kvm_run->s.regs.pp = env->pp;
323 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
324 } else {
326 * These ONE_REGS are not protected by a capability. As they are only
327 * necessary for migration we just trace a possible error, but don't
328 * return with an error return code.
330 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
331 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
332 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
333 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
334 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
337 /* pfault parameters */
338 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
339 cs->kvm_run->s.regs.pft = env->pfault_token;
340 cs->kvm_run->s.regs.pfs = env->pfault_select;
341 cs->kvm_run->s.regs.pfc = env->pfault_compare;
342 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
343 } else if (cap_async_pf) {
344 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
345 if (r < 0) {
346 return r;
348 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
349 if (r < 0) {
350 return r;
352 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
353 if (r < 0) {
354 return r;
358 /* access registers and control registers*/
359 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
360 for (i = 0; i < 16; i++) {
361 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
362 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
364 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
365 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
366 } else {
367 for (i = 0; i < 16; i++) {
368 sregs.acrs[i] = env->aregs[i];
369 sregs.crs[i] = env->cregs[i];
371 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
372 if (r < 0) {
373 return r;
377 /* Finally the prefix */
378 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
379 cs->kvm_run->s.regs.prefix = env->psa;
380 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
381 } else {
382 /* prefix is only supported via sync regs */
384 return 0;
387 int kvm_arch_get_registers(CPUState *cs)
389 S390CPU *cpu = S390_CPU(cs);
390 CPUS390XState *env = &cpu->env;
391 struct kvm_sregs sregs;
392 struct kvm_regs regs;
393 struct kvm_fpu fpu;
394 int i, r;
396 /* get the PSW */
397 env->psw.addr = cs->kvm_run->psw_addr;
398 env->psw.mask = cs->kvm_run->psw_mask;
400 /* the GPRS */
401 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
402 for (i = 0; i < 16; i++) {
403 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
405 } else {
406 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
407 if (r < 0) {
408 return r;
410 for (i = 0; i < 16; i++) {
411 env->regs[i] = regs.gprs[i];
415 /* The ACRS and CRS */
416 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
417 for (i = 0; i < 16; i++) {
418 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
419 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
421 } else {
422 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
423 if (r < 0) {
424 return r;
426 for (i = 0; i < 16; i++) {
427 env->aregs[i] = sregs.acrs[i];
428 env->cregs[i] = sregs.crs[i];
432 /* Floating point */
433 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
434 if (r < 0) {
435 return r;
437 for (i = 0; i < 16; i++) {
438 env->fregs[i].ll = fpu.fprs[i];
440 env->fpc = fpu.fpc;
442 /* The prefix */
443 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
444 env->psa = cs->kvm_run->s.regs.prefix;
447 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
448 env->cputm = cs->kvm_run->s.regs.cputm;
449 env->ckc = cs->kvm_run->s.regs.ckc;
450 env->todpr = cs->kvm_run->s.regs.todpr;
451 env->gbea = cs->kvm_run->s.regs.gbea;
452 env->pp = cs->kvm_run->s.regs.pp;
453 } else {
455 * These ONE_REGS are not protected by a capability. As they are only
456 * necessary for migration we just trace a possible error, but don't
457 * return with an error return code.
459 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
460 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
461 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
462 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
463 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
466 /* pfault parameters */
467 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
468 env->pfault_token = cs->kvm_run->s.regs.pft;
469 env->pfault_select = cs->kvm_run->s.regs.pfs;
470 env->pfault_compare = cs->kvm_run->s.regs.pfc;
471 } else if (cap_async_pf) {
472 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
473 if (r < 0) {
474 return r;
476 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
477 if (r < 0) {
478 return r;
480 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
481 if (r < 0) {
482 return r;
486 return 0;
490 * Legacy layout for s390:
491 * Older S390 KVM requires the topmost vma of the RAM to be
492 * smaller than an system defined value, which is at least 256GB.
493 * Larger systems have larger values. We put the guest between
494 * the end of data segment (system break) and this value. We
495 * use 32GB as a base to have enough room for the system break
496 * to grow. We also have to use MAP parameters that avoid
497 * read-only mapping of guest pages.
499 static void *legacy_s390_alloc(size_t size, uint64_t *align)
501 void *mem;
503 mem = mmap((void *) 0x800000000ULL, size,
504 PROT_EXEC|PROT_READ|PROT_WRITE,
505 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
506 return mem == MAP_FAILED ? NULL : mem;
509 /* DIAG 501 is used for sw breakpoints */
510 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
512 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
515 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
516 sizeof(diag_501), 0) ||
517 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
518 sizeof(diag_501), 1)) {
519 return -EINVAL;
521 return 0;
524 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
526 uint8_t t[sizeof(diag_501)];
528 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
529 return -EINVAL;
530 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
531 return -EINVAL;
532 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
533 sizeof(diag_501), 1)) {
534 return -EINVAL;
537 return 0;
540 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
541 int len, int type)
543 int n;
545 for (n = 0; n < nb_hw_breakpoints; n++) {
546 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
547 (hw_breakpoints[n].len == len || len == -1)) {
548 return &hw_breakpoints[n];
552 return NULL;
555 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
557 int size;
559 if (find_hw_breakpoint(addr, len, type)) {
560 return -EEXIST;
563 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
565 if (!hw_breakpoints) {
566 nb_hw_breakpoints = 0;
567 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
568 } else {
569 hw_breakpoints =
570 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
573 if (!hw_breakpoints) {
574 nb_hw_breakpoints = 0;
575 return -ENOMEM;
578 hw_breakpoints[nb_hw_breakpoints].addr = addr;
579 hw_breakpoints[nb_hw_breakpoints].len = len;
580 hw_breakpoints[nb_hw_breakpoints].type = type;
582 nb_hw_breakpoints++;
584 return 0;
587 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
588 target_ulong len, int type)
590 switch (type) {
591 case GDB_BREAKPOINT_HW:
592 type = KVM_HW_BP;
593 break;
594 case GDB_WATCHPOINT_WRITE:
595 if (len < 1) {
596 return -EINVAL;
598 type = KVM_HW_WP_WRITE;
599 break;
600 default:
601 return -ENOSYS;
603 return insert_hw_breakpoint(addr, len, type);
606 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
607 target_ulong len, int type)
609 int size;
610 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
612 if (bp == NULL) {
613 return -ENOENT;
616 nb_hw_breakpoints--;
617 if (nb_hw_breakpoints > 0) {
619 * In order to trim the array, move the last element to the position to
620 * be removed - if necessary.
622 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
623 *bp = hw_breakpoints[nb_hw_breakpoints];
625 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
626 hw_breakpoints =
627 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
628 } else {
629 g_free(hw_breakpoints);
630 hw_breakpoints = NULL;
633 return 0;
636 void kvm_arch_remove_all_hw_breakpoints(void)
638 nb_hw_breakpoints = 0;
639 g_free(hw_breakpoints);
640 hw_breakpoints = NULL;
643 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
645 int i;
647 if (nb_hw_breakpoints > 0) {
648 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
649 dbg->arch.hw_bp = hw_breakpoints;
651 for (i = 0; i < nb_hw_breakpoints; ++i) {
652 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
653 hw_breakpoints[i].addr);
655 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
656 } else {
657 dbg->arch.nr_hw_bp = 0;
658 dbg->arch.hw_bp = NULL;
662 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
666 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
670 int kvm_arch_process_async_events(CPUState *cs)
672 return cs->halted;
675 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
676 struct kvm_s390_interrupt *interrupt)
678 int r = 0;
680 interrupt->type = irq->type;
681 switch (irq->type) {
682 case KVM_S390_INT_VIRTIO:
683 interrupt->parm = irq->u.ext.ext_params;
684 /* fall through */
685 case KVM_S390_INT_PFAULT_INIT:
686 case KVM_S390_INT_PFAULT_DONE:
687 interrupt->parm64 = irq->u.ext.ext_params2;
688 break;
689 case KVM_S390_PROGRAM_INT:
690 interrupt->parm = irq->u.pgm.code;
691 break;
692 case KVM_S390_SIGP_SET_PREFIX:
693 interrupt->parm = irq->u.prefix.address;
694 break;
695 case KVM_S390_INT_SERVICE:
696 interrupt->parm = irq->u.ext.ext_params;
697 break;
698 case KVM_S390_MCHK:
699 interrupt->parm = irq->u.mchk.cr14;
700 interrupt->parm64 = irq->u.mchk.mcic;
701 break;
702 case KVM_S390_INT_EXTERNAL_CALL:
703 interrupt->parm = irq->u.extcall.code;
704 break;
705 case KVM_S390_INT_EMERGENCY:
706 interrupt->parm = irq->u.emerg.code;
707 break;
708 case KVM_S390_SIGP_STOP:
709 case KVM_S390_RESTART:
710 break; /* These types have no parameters */
711 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
712 interrupt->parm = irq->u.io.subchannel_id << 16;
713 interrupt->parm |= irq->u.io.subchannel_nr;
714 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
715 interrupt->parm64 |= irq->u.io.io_int_word;
716 break;
717 default:
718 r = -EINVAL;
719 break;
721 return r;
724 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
726 struct kvm_s390_interrupt kvmint = {};
727 CPUState *cs = CPU(cpu);
728 int r;
730 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
731 if (r < 0) {
732 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
733 exit(1);
736 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
737 if (r < 0) {
738 fprintf(stderr, "KVM failed to inject interrupt\n");
739 exit(1);
743 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
745 struct kvm_s390_interrupt kvmint = {};
746 int r;
748 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
749 if (r < 0) {
750 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
751 exit(1);
754 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
755 if (r < 0) {
756 fprintf(stderr, "KVM failed to inject interrupt\n");
757 exit(1);
761 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
763 static bool use_flic = true;
764 int r;
766 if (use_flic) {
767 r = kvm_s390_inject_flic(irq);
768 if (r == -ENOSYS) {
769 use_flic = false;
771 if (!r) {
772 return;
775 __kvm_s390_floating_interrupt(irq);
778 void kvm_s390_virtio_irq(int config_change, uint64_t token)
780 struct kvm_s390_irq irq = {
781 .type = KVM_S390_INT_VIRTIO,
782 .u.ext.ext_params = config_change,
783 .u.ext.ext_params2 = token,
786 kvm_s390_floating_interrupt(&irq);
789 void kvm_s390_service_interrupt(uint32_t parm)
791 struct kvm_s390_irq irq = {
792 .type = KVM_S390_INT_SERVICE,
793 .u.ext.ext_params = parm,
796 kvm_s390_floating_interrupt(&irq);
799 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
801 struct kvm_s390_irq irq = {
802 .type = KVM_S390_PROGRAM_INT,
803 .u.pgm.code = code,
806 kvm_s390_vcpu_interrupt(cpu, &irq);
809 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
811 struct kvm_s390_irq irq = {
812 .type = KVM_S390_PROGRAM_INT,
813 .u.pgm.code = code,
814 .u.pgm.trans_exc_code = te_code,
815 .u.pgm.exc_access_id = te_code & 3,
818 kvm_s390_vcpu_interrupt(cpu, &irq);
821 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
822 uint16_t ipbh0)
824 CPUS390XState *env = &cpu->env;
825 uint64_t sccb;
826 uint32_t code;
827 int r = 0;
829 cpu_synchronize_state(CPU(cpu));
830 sccb = env->regs[ipbh0 & 0xf];
831 code = env->regs[(ipbh0 & 0xf0) >> 4];
833 r = sclp_service_call(env, sccb, code);
834 if (r < 0) {
835 enter_pgmcheck(cpu, -r);
836 } else {
837 setcc(cpu, r);
840 return 0;
843 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
845 CPUS390XState *env = &cpu->env;
846 int rc = 0;
847 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
849 cpu_synchronize_state(CPU(cpu));
851 switch (ipa1) {
852 case PRIV_B2_XSCH:
853 ioinst_handle_xsch(cpu, env->regs[1]);
854 break;
855 case PRIV_B2_CSCH:
856 ioinst_handle_csch(cpu, env->regs[1]);
857 break;
858 case PRIV_B2_HSCH:
859 ioinst_handle_hsch(cpu, env->regs[1]);
860 break;
861 case PRIV_B2_MSCH:
862 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
863 break;
864 case PRIV_B2_SSCH:
865 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
866 break;
867 case PRIV_B2_STCRW:
868 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
869 break;
870 case PRIV_B2_STSCH:
871 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
872 break;
873 case PRIV_B2_TSCH:
874 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
875 fprintf(stderr, "Spurious tsch intercept\n");
876 break;
877 case PRIV_B2_CHSC:
878 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
879 break;
880 case PRIV_B2_TPI:
881 /* This should have been handled by kvm already. */
882 fprintf(stderr, "Spurious tpi intercept\n");
883 break;
884 case PRIV_B2_SCHM:
885 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
886 run->s390_sieic.ipb);
887 break;
888 case PRIV_B2_RSCH:
889 ioinst_handle_rsch(cpu, env->regs[1]);
890 break;
891 case PRIV_B2_RCHP:
892 ioinst_handle_rchp(cpu, env->regs[1]);
893 break;
894 case PRIV_B2_STCPS:
895 /* We do not provide this instruction, it is suppressed. */
896 break;
897 case PRIV_B2_SAL:
898 ioinst_handle_sal(cpu, env->regs[1]);
899 break;
900 case PRIV_B2_SIGA:
901 /* Not provided, set CC = 3 for subchannel not operational */
902 setcc(cpu, 3);
903 break;
904 case PRIV_B2_SCLP_CALL:
905 rc = kvm_sclp_service_call(cpu, run, ipbh0);
906 break;
907 default:
908 rc = -1;
909 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
910 break;
913 return rc;
916 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run)
918 CPUS390XState *env = &cpu->env;
919 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
920 uint32_t base2 = run->s390_sieic.ipb >> 28;
921 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
922 ((run->s390_sieic.ipb & 0xff00) << 4);
924 if (disp2 & 0x80000) {
925 disp2 += 0xfff00000;
928 return (base2 ? env->regs[base2] : 0) +
929 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
932 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run)
934 CPUS390XState *env = &cpu->env;
935 uint32_t base2 = run->s390_sieic.ipb >> 28;
936 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
937 ((run->s390_sieic.ipb & 0xff00) << 4);
939 if (disp2 & 0x80000) {
940 disp2 += 0xfff00000;
943 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
946 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
948 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
950 return clp_service_call(cpu, r2);
953 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
955 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
956 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
958 return pcilg_service_call(cpu, r1, r2);
961 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
963 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
964 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
966 return pcistg_service_call(cpu, r1, r2);
969 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
971 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
972 uint64_t fiba;
974 cpu_synchronize_state(CPU(cpu));
975 fiba = get_base_disp_rxy(cpu, run);
977 return stpcifc_service_call(cpu, r1, fiba);
980 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
982 /* NOOP */
983 return 0;
986 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
988 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
989 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
991 return rpcit_service_call(cpu, r1, r2);
994 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
996 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
997 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
998 uint64_t gaddr;
1000 cpu_synchronize_state(CPU(cpu));
1001 gaddr = get_base_disp_rsy(cpu, run);
1003 return pcistb_service_call(cpu, r1, r3, gaddr);
1006 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1008 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1009 uint64_t fiba;
1011 cpu_synchronize_state(CPU(cpu));
1012 fiba = get_base_disp_rxy(cpu, run);
1014 return mpcifc_service_call(cpu, r1, fiba);
1017 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1019 int r = 0;
1021 switch (ipa1) {
1022 case PRIV_B9_CLP:
1023 r = kvm_clp_service_call(cpu, run);
1024 break;
1025 case PRIV_B9_PCISTG:
1026 r = kvm_pcistg_service_call(cpu, run);
1027 break;
1028 case PRIV_B9_PCILG:
1029 r = kvm_pcilg_service_call(cpu, run);
1030 break;
1031 case PRIV_B9_RPCIT:
1032 r = kvm_rpcit_service_call(cpu, run);
1033 break;
1034 case PRIV_B9_EQBS:
1035 /* just inject exception */
1036 r = -1;
1037 break;
1038 default:
1039 r = -1;
1040 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1041 break;
1044 return r;
1047 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1049 int r = 0;
1051 switch (ipbl) {
1052 case PRIV_EB_PCISTB:
1053 r = kvm_pcistb_service_call(cpu, run);
1054 break;
1055 case PRIV_EB_SIC:
1056 r = kvm_sic_service_call(cpu, run);
1057 break;
1058 case PRIV_EB_SQBS:
1059 /* just inject exception */
1060 r = -1;
1061 break;
1062 default:
1063 r = -1;
1064 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1065 break;
1068 return r;
1071 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1073 int r = 0;
1075 switch (ipbl) {
1076 case PRIV_E3_MPCIFC:
1077 r = kvm_mpcifc_service_call(cpu, run);
1078 break;
1079 case PRIV_E3_STPCIFC:
1080 r = kvm_stpcifc_service_call(cpu, run);
1081 break;
1082 default:
1083 r = -1;
1084 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1085 break;
1088 return r;
1091 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1093 CPUS390XState *env = &cpu->env;
1094 int ret;
1096 cpu_synchronize_state(CPU(cpu));
1097 ret = s390_virtio_hypercall(env);
1098 if (ret == -EINVAL) {
1099 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1100 return 0;
1103 return ret;
1106 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1108 uint64_t r1, r3;
1110 cpu_synchronize_state(CPU(cpu));
1111 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1112 r3 = run->s390_sieic.ipa & 0x000f;
1113 handle_diag_308(&cpu->env, r1, r3);
1116 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1118 CPUS390XState *env = &cpu->env;
1119 unsigned long pc;
1121 cpu_synchronize_state(CPU(cpu));
1123 pc = env->psw.addr - 4;
1124 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1125 env->psw.addr = pc;
1126 return EXCP_DEBUG;
1129 return -ENOENT;
1132 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1134 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1136 int r = 0;
1137 uint16_t func_code;
1140 * For any diagnose call we support, bits 48-63 of the resulting
1141 * address specify the function code; the remainder is ignored.
1143 func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
1144 switch (func_code) {
1145 case DIAG_IPL:
1146 kvm_handle_diag_308(cpu, run);
1147 break;
1148 case DIAG_KVM_HYPERCALL:
1149 r = handle_hypercall(cpu, run);
1150 break;
1151 case DIAG_KVM_BREAKPOINT:
1152 r = handle_sw_breakpoint(cpu, run);
1153 break;
1154 default:
1155 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1156 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1157 break;
1160 return r;
1163 typedef struct SigpInfo {
1164 S390CPU *cpu;
1165 uint64_t param;
1166 int cc;
1167 uint64_t *status_reg;
1168 } SigpInfo;
1170 static void set_sigp_status(SigpInfo *si, uint64_t status)
1172 *si->status_reg &= 0xffffffff00000000ULL;
1173 *si->status_reg |= status;
1174 si->cc = SIGP_CC_STATUS_STORED;
1177 static void sigp_start(void *arg)
1179 SigpInfo *si = arg;
1181 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1182 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1183 return;
1186 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1187 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1190 static void sigp_stop(void *arg)
1192 SigpInfo *si = arg;
1193 struct kvm_s390_irq irq = {
1194 .type = KVM_S390_SIGP_STOP,
1197 if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1198 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1199 return;
1202 /* disabled wait - sleeping in user space */
1203 if (CPU(si->cpu)->halted) {
1204 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1205 } else {
1206 /* execute the stop function */
1207 si->cpu->env.sigp_order = SIGP_STOP;
1208 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1210 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1213 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1214 #define SAVE_AREA_SIZE 512
1215 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1217 static const uint8_t ar_id = 1;
1218 uint64_t ckc = cpu->env.ckc >> 8;
1219 void *mem;
1220 hwaddr len = SAVE_AREA_SIZE;
1222 mem = cpu_physical_memory_map(addr, &len, 1);
1223 if (!mem) {
1224 return -EFAULT;
1226 if (len != SAVE_AREA_SIZE) {
1227 cpu_physical_memory_unmap(mem, len, 1, 0);
1228 return -EFAULT;
1231 if (store_arch) {
1232 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1234 memcpy(mem, &cpu->env.fregs, 128);
1235 memcpy(mem + 128, &cpu->env.regs, 128);
1236 memcpy(mem + 256, &cpu->env.psw, 16);
1237 memcpy(mem + 280, &cpu->env.psa, 4);
1238 memcpy(mem + 284, &cpu->env.fpc, 4);
1239 memcpy(mem + 292, &cpu->env.todpr, 4);
1240 memcpy(mem + 296, &cpu->env.cputm, 8);
1241 memcpy(mem + 304, &ckc, 8);
1242 memcpy(mem + 320, &cpu->env.aregs, 64);
1243 memcpy(mem + 384, &cpu->env.cregs, 128);
1245 cpu_physical_memory_unmap(mem, len, 1, len);
1247 return 0;
1250 static void sigp_stop_and_store_status(void *arg)
1252 SigpInfo *si = arg;
1253 struct kvm_s390_irq irq = {
1254 .type = KVM_S390_SIGP_STOP,
1257 /* disabled wait - sleeping in user space */
1258 if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1259 CPU(si->cpu)->halted) {
1260 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1263 switch (s390_cpu_get_state(si->cpu)) {
1264 case CPU_STATE_OPERATING:
1265 si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1266 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1267 /* store will be performed when handling the stop intercept */
1268 break;
1269 case CPU_STATE_STOPPED:
1270 /* already stopped, just store the status */
1271 cpu_synchronize_state(CPU(si->cpu));
1272 kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1273 break;
1275 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1278 static void sigp_store_status_at_address(void *arg)
1280 SigpInfo *si = arg;
1281 uint32_t address = si->param & 0x7ffffe00u;
1283 /* cpu has to be stopped */
1284 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1285 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1286 return;
1289 cpu_synchronize_state(CPU(si->cpu));
1291 if (kvm_s390_store_status(si->cpu, address, false)) {
1292 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1293 return;
1295 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1298 static void sigp_restart(void *arg)
1300 SigpInfo *si = arg;
1301 struct kvm_s390_irq irq = {
1302 .type = KVM_S390_RESTART,
1305 switch (s390_cpu_get_state(si->cpu)) {
1306 case CPU_STATE_STOPPED:
1307 /* the restart irq has to be delivered prior to any other pending irq */
1308 cpu_synchronize_state(CPU(si->cpu));
1309 do_restart_interrupt(&si->cpu->env);
1310 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1311 break;
1312 case CPU_STATE_OPERATING:
1313 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1314 break;
1316 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1319 int kvm_s390_cpu_restart(S390CPU *cpu)
1321 SigpInfo si = {
1322 .cpu = cpu,
1325 run_on_cpu(CPU(cpu), sigp_restart, &si);
1326 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1327 return 0;
1330 static void sigp_initial_cpu_reset(void *arg)
1332 SigpInfo *si = arg;
1333 CPUState *cs = CPU(si->cpu);
1334 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1336 cpu_synchronize_state(cs);
1337 scc->initial_cpu_reset(cs);
1338 cpu_synchronize_post_reset(cs);
1339 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1342 static void sigp_cpu_reset(void *arg)
1344 SigpInfo *si = arg;
1345 CPUState *cs = CPU(si->cpu);
1346 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1348 cpu_synchronize_state(cs);
1349 scc->cpu_reset(cs);
1350 cpu_synchronize_post_reset(cs);
1351 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1354 static void sigp_set_prefix(void *arg)
1356 SigpInfo *si = arg;
1357 uint32_t addr = si->param & 0x7fffe000u;
1359 cpu_synchronize_state(CPU(si->cpu));
1361 if (!address_space_access_valid(&address_space_memory, addr,
1362 sizeof(struct LowCore), false)) {
1363 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1364 return;
1367 /* cpu has to be stopped */
1368 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1369 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1370 return;
1373 si->cpu->env.psa = addr;
1374 cpu_synchronize_post_init(CPU(si->cpu));
1375 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1378 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1379 uint64_t param, uint64_t *status_reg)
1381 SigpInfo si = {
1382 .cpu = dst_cpu,
1383 .param = param,
1384 .status_reg = status_reg,
1387 /* cpu available? */
1388 if (dst_cpu == NULL) {
1389 return SIGP_CC_NOT_OPERATIONAL;
1392 /* only resets can break pending orders */
1393 if (dst_cpu->env.sigp_order != 0 &&
1394 order != SIGP_CPU_RESET &&
1395 order != SIGP_INITIAL_CPU_RESET) {
1396 return SIGP_CC_BUSY;
1399 switch (order) {
1400 case SIGP_START:
1401 run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1402 break;
1403 case SIGP_STOP:
1404 run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1405 break;
1406 case SIGP_RESTART:
1407 run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1408 break;
1409 case SIGP_STOP_STORE_STATUS:
1410 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1411 break;
1412 case SIGP_STORE_STATUS_ADDR:
1413 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1414 break;
1415 case SIGP_SET_PREFIX:
1416 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1417 break;
1418 case SIGP_INITIAL_CPU_RESET:
1419 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1420 break;
1421 case SIGP_CPU_RESET:
1422 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1423 break;
1424 default:
1425 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1426 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1429 return si.cc;
1432 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1433 uint64_t *status_reg)
1435 CPUState *cur_cs;
1436 S390CPU *cur_cpu;
1438 /* due to the BQL, we are the only active cpu */
1439 CPU_FOREACH(cur_cs) {
1440 cur_cpu = S390_CPU(cur_cs);
1441 if (cur_cpu->env.sigp_order != 0) {
1442 return SIGP_CC_BUSY;
1444 cpu_synchronize_state(cur_cs);
1445 /* all but the current one have to be stopped */
1446 if (cur_cpu != cpu &&
1447 s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1448 *status_reg &= 0xffffffff00000000ULL;
1449 *status_reg |= SIGP_STAT_INCORRECT_STATE;
1450 return SIGP_CC_STATUS_STORED;
1454 switch (param & 0xff) {
1455 case SIGP_MODE_ESA_S390:
1456 /* not supported */
1457 return SIGP_CC_NOT_OPERATIONAL;
1458 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1459 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1460 CPU_FOREACH(cur_cs) {
1461 cur_cpu = S390_CPU(cur_cs);
1462 cur_cpu->env.pfault_token = -1UL;
1464 break;
1465 default:
1466 *status_reg &= 0xffffffff00000000ULL;
1467 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1468 return SIGP_CC_STATUS_STORED;
1471 return SIGP_CC_ORDER_CODE_ACCEPTED;
1474 #define SIGP_ORDER_MASK 0x000000ff
1476 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1478 CPUS390XState *env = &cpu->env;
1479 const uint8_t r1 = ipa1 >> 4;
1480 const uint8_t r3 = ipa1 & 0x0f;
1481 int ret;
1482 uint8_t order;
1483 uint64_t *status_reg;
1484 uint64_t param;
1485 S390CPU *dst_cpu = NULL;
1487 cpu_synchronize_state(CPU(cpu));
1489 /* get order code */
1490 order = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
1491 status_reg = &env->regs[r1];
1492 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1494 switch (order) {
1495 case SIGP_SET_ARCH:
1496 ret = sigp_set_architecture(cpu, param, status_reg);
1497 break;
1498 default:
1499 /* all other sigp orders target a single vcpu */
1500 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1501 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1504 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1505 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1507 if (ret >= 0) {
1508 setcc(cpu, ret);
1509 return 0;
1512 return ret;
1515 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1517 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1518 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1519 int r = -1;
1521 DPRINTF("handle_instruction 0x%x 0x%x\n",
1522 run->s390_sieic.ipa, run->s390_sieic.ipb);
1523 switch (ipa0) {
1524 case IPA0_B2:
1525 r = handle_b2(cpu, run, ipa1);
1526 break;
1527 case IPA0_B9:
1528 r = handle_b9(cpu, run, ipa1);
1529 break;
1530 case IPA0_EB:
1531 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1532 break;
1533 case IPA0_E3:
1534 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1535 break;
1536 case IPA0_DIAG:
1537 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1538 break;
1539 case IPA0_SIGP:
1540 r = handle_sigp(cpu, run, ipa1);
1541 break;
1544 if (r < 0) {
1545 r = 0;
1546 enter_pgmcheck(cpu, 0x0001);
1549 return r;
1552 static bool is_special_wait_psw(CPUState *cs)
1554 /* signal quiesce */
1555 return cs->kvm_run->psw_addr == 0xfffUL;
1558 static void guest_panicked(void)
1560 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1561 &error_abort);
1562 vm_stop(RUN_STATE_GUEST_PANICKED);
1565 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1567 CPUState *cs = CPU(cpu);
1569 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1570 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1571 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1572 s390_cpu_halt(cpu);
1573 guest_panicked();
1576 static int handle_intercept(S390CPU *cpu)
1578 CPUState *cs = CPU(cpu);
1579 struct kvm_run *run = cs->kvm_run;
1580 int icpt_code = run->s390_sieic.icptcode;
1581 int r = 0;
1583 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1584 (long)cs->kvm_run->psw_addr);
1585 switch (icpt_code) {
1586 case ICPT_INSTRUCTION:
1587 r = handle_instruction(cpu, run);
1588 break;
1589 case ICPT_PROGRAM:
1590 unmanageable_intercept(cpu, "program interrupt",
1591 offsetof(LowCore, program_new_psw));
1592 r = EXCP_HALTED;
1593 break;
1594 case ICPT_EXT_INT:
1595 unmanageable_intercept(cpu, "external interrupt",
1596 offsetof(LowCore, external_new_psw));
1597 r = EXCP_HALTED;
1598 break;
1599 case ICPT_WAITPSW:
1600 /* disabled wait, since enabled wait is handled in kernel */
1601 cpu_synchronize_state(cs);
1602 if (s390_cpu_halt(cpu) == 0) {
1603 if (is_special_wait_psw(cs)) {
1604 qemu_system_shutdown_request();
1605 } else {
1606 guest_panicked();
1609 r = EXCP_HALTED;
1610 break;
1611 case ICPT_CPU_STOP:
1612 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1613 qemu_system_shutdown_request();
1615 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1616 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1617 true);
1619 cpu->env.sigp_order = 0;
1620 r = EXCP_HALTED;
1621 break;
1622 case ICPT_SOFT_INTERCEPT:
1623 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1624 exit(1);
1625 break;
1626 case ICPT_IO:
1627 fprintf(stderr, "KVM unimplemented icpt IO\n");
1628 exit(1);
1629 break;
1630 default:
1631 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1632 exit(1);
1633 break;
1636 return r;
1639 static int handle_tsch(S390CPU *cpu)
1641 CPUState *cs = CPU(cpu);
1642 struct kvm_run *run = cs->kvm_run;
1643 int ret;
1645 cpu_synchronize_state(cs);
1647 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1648 if (ret < 0) {
1650 * Failure.
1651 * If an I/O interrupt had been dequeued, we have to reinject it.
1653 if (run->s390_tsch.dequeued) {
1654 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1655 run->s390_tsch.subchannel_nr,
1656 run->s390_tsch.io_int_parm,
1657 run->s390_tsch.io_int_word);
1659 ret = 0;
1661 return ret;
1664 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1666 CPUState *cs = CPU(cpu);
1667 struct kvm_run *run = cs->kvm_run;
1669 int ret = 0;
1670 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1672 switch (arch_info->type) {
1673 case KVM_HW_WP_WRITE:
1674 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1675 cs->watchpoint_hit = &hw_watchpoint;
1676 hw_watchpoint.vaddr = arch_info->addr;
1677 hw_watchpoint.flags = BP_MEM_WRITE;
1678 ret = EXCP_DEBUG;
1680 break;
1681 case KVM_HW_BP:
1682 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1683 ret = EXCP_DEBUG;
1685 break;
1686 case KVM_SINGLESTEP:
1687 if (cs->singlestep_enabled) {
1688 ret = EXCP_DEBUG;
1690 break;
1691 default:
1692 ret = -ENOSYS;
1695 return ret;
1698 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1700 S390CPU *cpu = S390_CPU(cs);
1701 int ret = 0;
1703 switch (run->exit_reason) {
1704 case KVM_EXIT_S390_SIEIC:
1705 ret = handle_intercept(cpu);
1706 break;
1707 case KVM_EXIT_S390_RESET:
1708 s390_reipl_request();
1709 break;
1710 case KVM_EXIT_S390_TSCH:
1711 ret = handle_tsch(cpu);
1712 break;
1713 case KVM_EXIT_DEBUG:
1714 ret = kvm_arch_handle_debug_exit(cpu);
1715 break;
1716 default:
1717 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1718 break;
1721 if (ret == 0) {
1722 ret = EXCP_INTERRUPT;
1724 return ret;
1727 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1729 return true;
1732 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
1734 return 1;
1737 int kvm_arch_on_sigbus(int code, void *addr)
1739 return 1;
1742 void kvm_s390_io_interrupt(uint16_t subchannel_id,
1743 uint16_t subchannel_nr, uint32_t io_int_parm,
1744 uint32_t io_int_word)
1746 struct kvm_s390_irq irq = {
1747 .u.io.subchannel_id = subchannel_id,
1748 .u.io.subchannel_nr = subchannel_nr,
1749 .u.io.io_int_parm = io_int_parm,
1750 .u.io.io_int_word = io_int_word,
1753 if (io_int_word & IO_INT_WORD_AI) {
1754 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
1755 } else {
1756 irq.type = ((subchannel_id & 0xff00) << 24) |
1757 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
1759 kvm_s390_floating_interrupt(&irq);
1762 void kvm_s390_crw_mchk(void)
1764 struct kvm_s390_irq irq = {
1765 .type = KVM_S390_MCHK,
1766 .u.mchk.cr14 = 1 << 28,
1767 .u.mchk.mcic = 0x00400f1d40330000ULL,
1769 kvm_s390_floating_interrupt(&irq);
1772 void kvm_s390_enable_css_support(S390CPU *cpu)
1774 int r;
1776 /* Activate host kernel channel subsystem support. */
1777 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1778 assert(r == 0);
1781 void kvm_arch_init_irq_routing(KVMState *s)
1784 * Note that while irqchip capabilities generally imply that cpustates
1785 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1786 * have to override the common code kvm_halt_in_kernel_allowed setting.
1788 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1789 kvm_gsi_routing_allowed = true;
1790 kvm_halt_in_kernel_allowed = false;
1794 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1795 int vq, bool assign)
1797 struct kvm_ioeventfd kick = {
1798 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1799 KVM_IOEVENTFD_FLAG_DATAMATCH,
1800 .fd = event_notifier_get_fd(notifier),
1801 .datamatch = vq,
1802 .addr = sch,
1803 .len = 8,
1805 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1806 return -ENOSYS;
1808 if (!assign) {
1809 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1811 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1814 int kvm_s390_get_memslot_count(KVMState *s)
1816 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
1819 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1821 struct kvm_mp_state mp_state = {};
1822 int ret;
1824 /* the kvm part might not have been initialized yet */
1825 if (CPU(cpu)->kvm_state == NULL) {
1826 return 0;
1829 switch (cpu_state) {
1830 case CPU_STATE_STOPPED:
1831 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1832 break;
1833 case CPU_STATE_CHECK_STOP:
1834 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1835 break;
1836 case CPU_STATE_OPERATING:
1837 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1838 break;
1839 case CPU_STATE_LOAD:
1840 mp_state.mp_state = KVM_MP_STATE_LOAD;
1841 break;
1842 default:
1843 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1844 cpu_state);
1845 exit(1);
1848 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1849 if (ret) {
1850 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1851 strerror(-ret));
1854 return ret;
1857 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1858 uint64_t address, uint32_t data)
1860 S390PCIBusDevice *pbdev;
1861 uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
1862 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
1864 pbdev = s390_pci_find_dev_by_fid(fid);
1865 if (!pbdev) {
1866 DPRINTF("add_msi_route no dev\n");
1867 return -ENODEV;
1870 pbdev->routes.adapter.ind_offset = vec;
1872 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
1873 route->flags = 0;
1874 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
1875 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
1876 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
1877 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
1878 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
1879 return 0;