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>
28 #include <linux/kvm.h>
29 #include <asm/ptrace.h>
31 #include "qemu-common.h"
32 #include "qemu/timer.h"
33 #include "sysemu/sysemu.h"
34 #include "sysemu/kvm.h"
37 #include "sysemu/device_tree.h"
38 #include "qapi/qmp/qjson.h"
39 #include "monitor/monitor.h"
40 #include "exec/gdbstub.h"
43 /* #define DEBUG_KVM */
46 #define DPRINTF(fmt, ...) \
47 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
49 #define DPRINTF(fmt, ...) \
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
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
[] = {
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
)
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
)
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
)) {
155 rc
= kvm_vm_ioctl(s
, KVM_SET_DEVICE_ATTR
, &attr
);
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
);
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 */
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
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
;
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
;
222 for (i
= 0; i
< 16; i
++) {
223 regs
.gprs
[i
] = env
->regs
[i
];
225 r
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
231 /* Do we need to save more than that? */
232 if (level
== KVM_PUT_RUNTIME_STATE
) {
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
);
248 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
252 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
256 r
= kvm_set_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
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
;
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
);
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
;
287 /* prefix is only supported via sync regs */
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
;
301 env
->psw
.addr
= cs
->kvm_run
->psw_addr
;
302 env
->psw
.mask
= cs
->kvm_run
->psw_mask
;
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
];
310 r
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
314 for (i
= 0; i
< 16; i
++) {
315 env
->regs
[i
] = regs
.gprs
[i
];
319 /* The ACRS and CRS */
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
];
328 r
= kvm_vcpu_ioctl(cs
, KVM_GET_SREGS
, &sregs
);
332 for (i
= 0; i
< 16; i
++) {
333 env
->aregs
[i
] = sregs
.acrs
[i
];
334 env
->cregs
[i
] = sregs
.crs
[i
];
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
);
355 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFTOKEN
, &env
->pfault_token
);
359 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFCOMPARE
, &env
->pfault_compare
);
363 r
= kvm_get_one_reg(cs
, KVM_REG_S390_PFSELECT
, &env
->pfault_select
);
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
)
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)) {
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)) {
413 } else if (memcmp(t
, diag_501
, sizeof(diag_501
))) {
415 } else if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
416 sizeof(diag_501
), 1)) {
423 static struct kvm_hw_breakpoint
*find_hw_breakpoint(target_ulong addr
,
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
];
438 static int insert_hw_breakpoint(target_ulong addr
, int len
, int type
)
442 if (find_hw_breakpoint(addr
, len
, type
)) {
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
);
453 (struct kvm_hw_breakpoint
*)g_try_realloc(hw_breakpoints
, size
);
456 if (!hw_breakpoints
) {
457 nb_hw_breakpoints
= 0;
461 hw_breakpoints
[nb_hw_breakpoints
].addr
= addr
;
462 hw_breakpoints
[nb_hw_breakpoints
].len
= len
;
463 hw_breakpoints
[nb_hw_breakpoints
].type
= type
;
470 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
471 target_ulong len
, int type
)
474 case GDB_BREAKPOINT_HW
:
477 case GDB_WATCHPOINT_WRITE
:
481 type
= KVM_HW_WP_WRITE
;
486 return insert_hw_breakpoint(addr
, len
, type
);
489 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
490 target_ulong len
, int type
)
493 struct kvm_hw_breakpoint
*bp
= find_hw_breakpoint(addr
, len
, type
);
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
);
510 (struct kvm_hw_breakpoint
*)g_realloc(hw_breakpoints
, size
);
512 g_free(hw_breakpoints
);
513 hw_breakpoints
= NULL
;
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
)
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
;
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
)
558 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq
*irq
,
559 struct kvm_s390_interrupt
*interrupt
)
563 interrupt
->type
= irq
->type
;
565 case KVM_S390_INT_VIRTIO
:
566 interrupt
->parm
= irq
->u
.ext
.ext_params
;
568 case KVM_S390_INT_PFAULT_INIT
:
569 case KVM_S390_INT_PFAULT_DONE
:
570 interrupt
->parm64
= irq
->u
.ext
.ext_params2
;
572 case KVM_S390_PROGRAM_INT
:
573 interrupt
->parm
= irq
->u
.pgm
.code
;
575 case KVM_S390_SIGP_SET_PREFIX
:
576 interrupt
->parm
= irq
->u
.prefix
.address
;
578 case KVM_S390_INT_SERVICE
:
579 interrupt
->parm
= irq
->u
.ext
.ext_params
;
582 interrupt
->parm
= irq
->u
.mchk
.cr14
;
583 interrupt
->parm64
= irq
->u
.mchk
.mcic
;
585 case KVM_S390_INT_EXTERNAL_CALL
:
586 interrupt
->parm
= irq
->u
.extcall
.code
;
588 case KVM_S390_INT_EMERGENCY
:
589 interrupt
->parm
= irq
->u
.emerg
.code
;
591 case KVM_S390_SIGP_STOP
:
592 case KVM_S390_RESTART
:
593 break; /* These types have no parameters */
594 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
595 interrupt
->parm
= irq
->u
.io
.subchannel_id
<< 16;
596 interrupt
->parm
|= irq
->u
.io
.subchannel_nr
;
597 interrupt
->parm64
= (uint64_t)irq
->u
.io
.io_int_parm
<< 32;
598 interrupt
->parm64
|= irq
->u
.io
.io_int_word
;
607 void kvm_s390_vcpu_interrupt(S390CPU
*cpu
, struct kvm_s390_irq
*irq
)
609 struct kvm_s390_interrupt kvmint
= {};
610 CPUState
*cs
= CPU(cpu
);
613 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
615 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
619 r
= kvm_vcpu_ioctl(cs
, KVM_S390_INTERRUPT
, &kvmint
);
621 fprintf(stderr
, "KVM failed to inject interrupt\n");
626 void kvm_s390_floating_interrupt(struct kvm_s390_irq
*irq
)
628 struct kvm_s390_interrupt kvmint
= {};
631 r
= s390_kvm_irq_to_interrupt(irq
, &kvmint
);
633 fprintf(stderr
, "%s called with bogus interrupt\n", __func__
);
637 r
= kvm_vm_ioctl(kvm_state
, KVM_S390_INTERRUPT
, &kvmint
);
639 fprintf(stderr
, "KVM failed to inject interrupt\n");
644 void kvm_s390_virtio_irq(int config_change
, uint64_t token
)
646 struct kvm_s390_irq irq
= {
647 .type
= KVM_S390_INT_VIRTIO
,
648 .u
.ext
.ext_params
= config_change
,
649 .u
.ext
.ext_params2
= token
,
652 kvm_s390_floating_interrupt(&irq
);
655 void kvm_s390_service_interrupt(uint32_t parm
)
657 struct kvm_s390_irq irq
= {
658 .type
= KVM_S390_INT_SERVICE
,
659 .u
.ext
.ext_params
= parm
,
662 kvm_s390_floating_interrupt(&irq
);
665 static void enter_pgmcheck(S390CPU
*cpu
, uint16_t code
)
667 struct kvm_s390_irq irq
= {
668 .type
= KVM_S390_PROGRAM_INT
,
672 kvm_s390_vcpu_interrupt(cpu
, &irq
);
675 static int kvm_sclp_service_call(S390CPU
*cpu
, struct kvm_run
*run
,
678 CPUS390XState
*env
= &cpu
->env
;
683 cpu_synchronize_state(CPU(cpu
));
684 sccb
= env
->regs
[ipbh0
& 0xf];
685 code
= env
->regs
[(ipbh0
& 0xf0) >> 4];
687 r
= sclp_service_call(env
, sccb
, code
);
689 enter_pgmcheck(cpu
, -r
);
697 static int handle_b2(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
699 CPUS390XState
*env
= &cpu
->env
;
701 uint16_t ipbh0
= (run
->s390_sieic
.ipb
& 0xffff0000) >> 16;
703 cpu_synchronize_state(CPU(cpu
));
707 ioinst_handle_xsch(cpu
, env
->regs
[1]);
710 ioinst_handle_csch(cpu
, env
->regs
[1]);
713 ioinst_handle_hsch(cpu
, env
->regs
[1]);
716 ioinst_handle_msch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
719 ioinst_handle_ssch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
722 ioinst_handle_stcrw(cpu
, run
->s390_sieic
.ipb
);
725 ioinst_handle_stsch(cpu
, env
->regs
[1], run
->s390_sieic
.ipb
);
728 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
729 fprintf(stderr
, "Spurious tsch intercept\n");
732 ioinst_handle_chsc(cpu
, run
->s390_sieic
.ipb
);
735 /* This should have been handled by kvm already. */
736 fprintf(stderr
, "Spurious tpi intercept\n");
739 ioinst_handle_schm(cpu
, env
->regs
[1], env
->regs
[2],
740 run
->s390_sieic
.ipb
);
743 ioinst_handle_rsch(cpu
, env
->regs
[1]);
746 ioinst_handle_rchp(cpu
, env
->regs
[1]);
749 /* We do not provide this instruction, it is suppressed. */
752 ioinst_handle_sal(cpu
, env
->regs
[1]);
755 /* Not provided, set CC = 3 for subchannel not operational */
758 case PRIV_B2_SCLP_CALL
:
759 rc
= kvm_sclp_service_call(cpu
, run
, ipbh0
);
763 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1
);
770 static int handle_b9(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
776 /* just inject exception */
781 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1
);
788 static int handle_eb(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
794 /* just inject exception */
799 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipa1
);
806 static int handle_hypercall(S390CPU
*cpu
, struct kvm_run
*run
)
808 CPUS390XState
*env
= &cpu
->env
;
811 cpu_synchronize_state(CPU(cpu
));
812 ret
= s390_virtio_hypercall(env
);
813 if (ret
== -EINVAL
) {
814 enter_pgmcheck(cpu
, PGM_SPECIFICATION
);
821 static void kvm_handle_diag_308(S390CPU
*cpu
, struct kvm_run
*run
)
825 cpu_synchronize_state(CPU(cpu
));
826 r1
= (run
->s390_sieic
.ipa
& 0x00f0) >> 8;
827 r3
= run
->s390_sieic
.ipa
& 0x000f;
828 handle_diag_308(&cpu
->env
, r1
, r3
);
831 static int handle_sw_breakpoint(S390CPU
*cpu
, struct kvm_run
*run
)
833 CPUS390XState
*env
= &cpu
->env
;
836 cpu_synchronize_state(CPU(cpu
));
838 pc
= env
->psw
.addr
- 4;
839 if (kvm_find_sw_breakpoint(CPU(cpu
), pc
)) {
847 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
849 static int handle_diag(S390CPU
*cpu
, struct kvm_run
*run
, uint32_t ipb
)
855 * For any diagnose call we support, bits 48-63 of the resulting
856 * address specify the function code; the remainder is ignored.
858 func_code
= decode_basedisp_rs(&cpu
->env
, ipb
) & DIAG_KVM_CODE_MASK
;
861 kvm_handle_diag_308(cpu
, run
);
863 case DIAG_KVM_HYPERCALL
:
864 r
= handle_hypercall(cpu
, run
);
866 case DIAG_KVM_BREAKPOINT
:
867 r
= handle_sw_breakpoint(cpu
, run
);
870 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code
);
878 static int kvm_s390_cpu_start(S390CPU
*cpu
)
880 s390_add_running_cpu(cpu
);
881 qemu_cpu_kick(CPU(cpu
));
882 DPRINTF("DONE: KVM cpu start: %p\n", &cpu
->env
);
886 int kvm_s390_cpu_restart(S390CPU
*cpu
)
888 struct kvm_s390_irq irq
= {
889 .type
= KVM_S390_RESTART
,
892 kvm_s390_vcpu_interrupt(cpu
, &irq
);
893 s390_add_running_cpu(cpu
);
894 qemu_cpu_kick(CPU(cpu
));
895 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu
->env
);
899 static void sigp_initial_cpu_reset(void *arg
)
902 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
904 cpu_synchronize_state(cpu
);
905 scc
->initial_cpu_reset(cpu
);
908 static void sigp_cpu_reset(void *arg
)
911 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
913 cpu_synchronize_state(cpu
);
917 #define SIGP_ORDER_MASK 0x000000ff
919 static int handle_sigp(S390CPU
*cpu
, struct kvm_run
*run
, uint8_t ipa1
)
921 CPUS390XState
*env
= &cpu
->env
;
925 uint64_t *statusreg
= &env
->regs
[ipa1
>> 4];
928 cpu_synchronize_state(CPU(cpu
));
931 order_code
= decode_basedisp_rs(env
, run
->s390_sieic
.ipb
) & SIGP_ORDER_MASK
;
933 cpu_addr
= env
->regs
[ipa1
& 0x0f];
934 target_cpu
= s390_cpu_addr2state(cpu_addr
);
935 if (target_cpu
== NULL
) {
936 cc
= 3; /* not operational */
940 switch (order_code
) {
942 cc
= kvm_s390_cpu_start(target_cpu
);
945 cc
= kvm_s390_cpu_restart(target_cpu
);
948 *statusreg
&= 0xffffffff00000000UL
;
949 *statusreg
|= SIGP_STAT_INVALID_PARAMETER
;
950 cc
= 1; /* status stored */
952 case SIGP_INITIAL_CPU_RESET
:
953 run_on_cpu(CPU(target_cpu
), sigp_initial_cpu_reset
, CPU(target_cpu
));
957 run_on_cpu(CPU(target_cpu
), sigp_cpu_reset
, CPU(target_cpu
));
961 DPRINTF("KVM: unknown SIGP: 0x%x\n", order_code
);
962 *statusreg
&= 0xffffffff00000000UL
;
963 *statusreg
|= SIGP_STAT_INVALID_ORDER
;
964 cc
= 1; /* status stored */
973 static int handle_instruction(S390CPU
*cpu
, struct kvm_run
*run
)
975 unsigned int ipa0
= (run
->s390_sieic
.ipa
& 0xff00);
976 uint8_t ipa1
= run
->s390_sieic
.ipa
& 0x00ff;
979 DPRINTF("handle_instruction 0x%x 0x%x\n",
980 run
->s390_sieic
.ipa
, run
->s390_sieic
.ipb
);
983 r
= handle_b2(cpu
, run
, ipa1
);
986 r
= handle_b9(cpu
, run
, ipa1
);
989 r
= handle_eb(cpu
, run
, ipa1
);
992 r
= handle_diag(cpu
, run
, run
->s390_sieic
.ipb
);
995 r
= handle_sigp(cpu
, run
, ipa1
);
1001 enter_pgmcheck(cpu
, 0x0001);
1007 static bool is_special_wait_psw(CPUState
*cs
)
1009 /* signal quiesce */
1010 return cs
->kvm_run
->psw_addr
== 0xfffUL
;
1013 static void guest_panicked(void)
1017 data
= qobject_from_jsonf("{ 'action': %s }", "pause");
1018 monitor_protocol_event(QEVENT_GUEST_PANICKED
, data
);
1019 qobject_decref(data
);
1021 vm_stop(RUN_STATE_GUEST_PANICKED
);
1024 static void unmanageable_intercept(S390CPU
*cpu
, const char *str
, int pswoffset
)
1026 CPUState
*cs
= CPU(cpu
);
1028 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1029 str
, cs
->cpu_index
, ldq_phys(cs
->as
, cpu
->env
.psa
+ pswoffset
),
1030 ldq_phys(cs
->as
, cpu
->env
.psa
+ pswoffset
+ 8));
1031 s390_del_running_cpu(cpu
);
1035 static int handle_intercept(S390CPU
*cpu
)
1037 CPUState
*cs
= CPU(cpu
);
1038 struct kvm_run
*run
= cs
->kvm_run
;
1039 int icpt_code
= run
->s390_sieic
.icptcode
;
1042 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code
,
1043 (long)cs
->kvm_run
->psw_addr
);
1044 switch (icpt_code
) {
1045 case ICPT_INSTRUCTION
:
1046 r
= handle_instruction(cpu
, run
);
1049 unmanageable_intercept(cpu
, "program interrupt",
1050 offsetof(LowCore
, program_new_psw
));
1054 unmanageable_intercept(cpu
, "external interrupt",
1055 offsetof(LowCore
, external_new_psw
));
1059 /* disabled wait, since enabled wait is handled in kernel */
1060 if (s390_del_running_cpu(cpu
) == 0) {
1061 if (is_special_wait_psw(cs
)) {
1062 qemu_system_shutdown_request();
1070 if (s390_del_running_cpu(cpu
) == 0) {
1071 qemu_system_shutdown_request();
1075 case ICPT_SOFT_INTERCEPT
:
1076 fprintf(stderr
, "KVM unimplemented icpt SOFT\n");
1080 fprintf(stderr
, "KVM unimplemented icpt IO\n");
1084 fprintf(stderr
, "Unknown intercept code: %d\n", icpt_code
);
1092 static int handle_tsch(S390CPU
*cpu
)
1094 CPUS390XState
*env
= &cpu
->env
;
1095 CPUState
*cs
= CPU(cpu
);
1096 struct kvm_run
*run
= cs
->kvm_run
;
1099 cpu_synchronize_state(cs
);
1101 ret
= ioinst_handle_tsch(env
, env
->regs
[1], run
->s390_tsch
.ipb
);
1103 /* Success; set condition code. */
1106 } else if (ret
< -1) {
1109 * If an I/O interrupt had been dequeued, we have to reinject it.
1111 if (run
->s390_tsch
.dequeued
) {
1112 kvm_s390_io_interrupt(run
->s390_tsch
.subchannel_id
,
1113 run
->s390_tsch
.subchannel_nr
,
1114 run
->s390_tsch
.io_int_parm
,
1115 run
->s390_tsch
.io_int_word
);
1122 static int kvm_arch_handle_debug_exit(S390CPU
*cpu
)
1124 CPUState
*cs
= CPU(cpu
);
1125 struct kvm_run
*run
= cs
->kvm_run
;
1128 struct kvm_debug_exit_arch
*arch_info
= &run
->debug
.arch
;
1130 switch (arch_info
->type
) {
1131 case KVM_HW_WP_WRITE
:
1132 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1133 cs
->watchpoint_hit
= &hw_watchpoint
;
1134 hw_watchpoint
.vaddr
= arch_info
->addr
;
1135 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1140 if (find_hw_breakpoint(arch_info
->addr
, -1, arch_info
->type
)) {
1144 case KVM_SINGLESTEP
:
1145 if (cs
->singlestep_enabled
) {
1156 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
1158 S390CPU
*cpu
= S390_CPU(cs
);
1161 switch (run
->exit_reason
) {
1162 case KVM_EXIT_S390_SIEIC
:
1163 ret
= handle_intercept(cpu
);
1165 case KVM_EXIT_S390_RESET
:
1166 qemu_system_reset_request();
1168 case KVM_EXIT_S390_TSCH
:
1169 ret
= handle_tsch(cpu
);
1171 case KVM_EXIT_DEBUG
:
1172 ret
= kvm_arch_handle_debug_exit(cpu
);
1175 fprintf(stderr
, "Unknown KVM exit: %d\n", run
->exit_reason
);
1180 ret
= EXCP_INTERRUPT
;
1185 bool kvm_arch_stop_on_emulation_error(CPUState
*cpu
)
1190 int kvm_arch_on_sigbus_vcpu(CPUState
*cpu
, int code
, void *addr
)
1195 int kvm_arch_on_sigbus(int code
, void *addr
)
1200 void kvm_s390_io_interrupt(uint16_t subchannel_id
,
1201 uint16_t subchannel_nr
, uint32_t io_int_parm
,
1202 uint32_t io_int_word
)
1204 struct kvm_s390_irq irq
= {
1205 .u
.io
.subchannel_id
= subchannel_id
,
1206 .u
.io
.subchannel_nr
= subchannel_nr
,
1207 .u
.io
.io_int_parm
= io_int_parm
,
1208 .u
.io
.io_int_word
= io_int_word
,
1211 if (io_int_word
& IO_INT_WORD_AI
) {
1212 irq
.type
= KVM_S390_INT_IO(1, 0, 0, 0);
1214 irq
.type
= ((subchannel_id
& 0xff00) << 24) |
1215 ((subchannel_id
& 0x00060) << 22) | (subchannel_nr
<< 16);
1217 kvm_s390_floating_interrupt(&irq
);
1220 void kvm_s390_crw_mchk(void)
1222 struct kvm_s390_irq irq
= {
1223 .type
= KVM_S390_MCHK
,
1224 .u
.mchk
.cr14
= 1 << 28,
1225 .u
.mchk
.mcic
= 0x00400f1d40330000,
1227 kvm_s390_floating_interrupt(&irq
);
1230 void kvm_s390_enable_css_support(S390CPU
*cpu
)
1234 /* Activate host kernel channel subsystem support. */
1235 r
= kvm_vcpu_enable_cap(CPU(cpu
), KVM_CAP_S390_CSS_SUPPORT
, 0);
1239 void kvm_arch_init_irq_routing(KVMState
*s
)
1242 * Note that while irqchip capabilities generally imply that cpustates
1243 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1244 * have to override the common code kvm_halt_in_kernel_allowed setting.
1246 if (kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
1247 kvm_irqfds_allowed
= true;
1248 kvm_gsi_routing_allowed
= true;
1249 kvm_halt_in_kernel_allowed
= false;
1253 int kvm_s390_assign_subch_ioeventfd(EventNotifier
*notifier
, uint32_t sch
,
1254 int vq
, bool assign
)
1256 struct kvm_ioeventfd kick
= {
1257 .flags
= KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY
|
1258 KVM_IOEVENTFD_FLAG_DATAMATCH
,
1259 .fd
= event_notifier_get_fd(notifier
),
1264 if (!kvm_check_extension(kvm_state
, KVM_CAP_IOEVENTFD
)) {
1268 kick
.flags
|= KVM_IOEVENTFD_FLAG_DEASSIGN
;
1270 return kvm_vm_ioctl(kvm_state
, KVM_IOEVENTFD
, &kick
);