Merge remote-tracking branch 'remotes/bkoppelmann/tags/pull-tricore-20150330' into...
[qemu.git] / target-s390x / kvm.c
blobb48c643b36a7d18f6cca36018801595103a5cc23
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 kvm_vm_check_mem_attr(s, attr) \
59 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
61 #define IPA0_DIAG 0x8300
62 #define IPA0_SIGP 0xae00
63 #define IPA0_B2 0xb200
64 #define IPA0_B9 0xb900
65 #define IPA0_EB 0xeb00
66 #define IPA0_E3 0xe300
68 #define PRIV_B2_SCLP_CALL 0x20
69 #define PRIV_B2_CSCH 0x30
70 #define PRIV_B2_HSCH 0x31
71 #define PRIV_B2_MSCH 0x32
72 #define PRIV_B2_SSCH 0x33
73 #define PRIV_B2_STSCH 0x34
74 #define PRIV_B2_TSCH 0x35
75 #define PRIV_B2_TPI 0x36
76 #define PRIV_B2_SAL 0x37
77 #define PRIV_B2_RSCH 0x38
78 #define PRIV_B2_STCRW 0x39
79 #define PRIV_B2_STCPS 0x3a
80 #define PRIV_B2_RCHP 0x3b
81 #define PRIV_B2_SCHM 0x3c
82 #define PRIV_B2_CHSC 0x5f
83 #define PRIV_B2_SIGA 0x74
84 #define PRIV_B2_XSCH 0x76
86 #define PRIV_EB_SQBS 0x8a
87 #define PRIV_EB_PCISTB 0xd0
88 #define PRIV_EB_SIC 0xd1
90 #define PRIV_B9_EQBS 0x9c
91 #define PRIV_B9_CLP 0xa0
92 #define PRIV_B9_PCISTG 0xd0
93 #define PRIV_B9_PCILG 0xd2
94 #define PRIV_B9_RPCIT 0xd3
96 #define PRIV_E3_MPCIFC 0xd0
97 #define PRIV_E3_STPCIFC 0xd4
99 #define DIAG_IPL 0x308
100 #define DIAG_KVM_HYPERCALL 0x500
101 #define DIAG_KVM_BREAKPOINT 0x501
103 #define ICPT_INSTRUCTION 0x04
104 #define ICPT_PROGRAM 0x08
105 #define ICPT_EXT_INT 0x14
106 #define ICPT_WAITPSW 0x1c
107 #define ICPT_SOFT_INTERCEPT 0x24
108 #define ICPT_CPU_STOP 0x28
109 #define ICPT_IO 0x40
111 static CPUWatchpoint hw_watchpoint;
113 * We don't use a list because this structure is also used to transmit the
114 * hardware breakpoints to the kernel.
116 static struct kvm_hw_breakpoint *hw_breakpoints;
117 static int nb_hw_breakpoints;
119 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
120 KVM_CAP_LAST_INFO
123 static int cap_sync_regs;
124 static int cap_async_pf;
126 static void *legacy_s390_alloc(size_t size, uint64_t *align);
128 static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
130 struct kvm_device_attr attr = {
131 .group = KVM_S390_VM_MEM_CTRL,
132 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
133 .addr = (uint64_t) memory_limit,
136 return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
139 int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
141 int rc;
143 struct kvm_device_attr attr = {
144 .group = KVM_S390_VM_MEM_CTRL,
145 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
146 .addr = (uint64_t) &new_limit,
149 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_LIMIT_SIZE)) {
150 return 0;
153 rc = kvm_s390_query_mem_limit(s, hw_limit);
154 if (rc) {
155 return rc;
156 } else if (*hw_limit < new_limit) {
157 return -E2BIG;
160 return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
163 void kvm_s390_clear_cmma_callback(void *opaque)
165 int rc;
166 KVMState *s = opaque;
167 struct kvm_device_attr attr = {
168 .group = KVM_S390_VM_MEM_CTRL,
169 .attr = KVM_S390_VM_MEM_CLR_CMMA,
172 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
173 trace_kvm_clear_cmma(rc);
176 static void kvm_s390_enable_cmma(KVMState *s)
178 int rc;
179 struct kvm_device_attr attr = {
180 .group = KVM_S390_VM_MEM_CTRL,
181 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
184 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_ENABLE_CMMA) ||
185 !kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_CLR_CMMA)) {
186 return;
189 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
190 if (!rc) {
191 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
193 trace_kvm_enable_cmma(rc);
196 static void kvm_s390_set_attr(uint64_t attr)
198 struct kvm_device_attr attribute = {
199 .group = KVM_S390_VM_CRYPTO,
200 .attr = attr,
203 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
205 if (ret) {
206 error_report("Failed to set crypto device attribute %lu: %s",
207 attr, strerror(-ret));
211 static void kvm_s390_init_aes_kw(void)
213 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
215 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
216 NULL)) {
217 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
220 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
221 kvm_s390_set_attr(attr);
225 static void kvm_s390_init_dea_kw(void)
227 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
229 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
230 NULL)) {
231 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
234 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
235 kvm_s390_set_attr(attr);
239 static void kvm_s390_init_crypto(void)
241 kvm_s390_init_aes_kw();
242 kvm_s390_init_dea_kw();
245 int kvm_arch_init(MachineState *ms, KVMState *s)
247 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
248 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
250 kvm_s390_enable_cmma(s);
252 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
253 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
254 phys_mem_set_alloc(legacy_s390_alloc);
257 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
259 return 0;
262 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
264 return cpu->cpu_index;
267 int kvm_arch_init_vcpu(CPUState *cs)
269 S390CPU *cpu = S390_CPU(cs);
270 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
271 return 0;
274 void kvm_s390_reset_vcpu(S390CPU *cpu)
276 CPUState *cs = CPU(cpu);
278 /* The initial reset call is needed here to reset in-kernel
279 * vcpu data that we can't access directly from QEMU
280 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
281 * Before this ioctl cpu_synchronize_state() is called in common kvm
282 * code (kvm-all) */
283 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
284 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
287 kvm_s390_init_crypto();
290 static int can_sync_regs(CPUState *cs, int regs)
292 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
295 int kvm_arch_put_registers(CPUState *cs, int level)
297 S390CPU *cpu = S390_CPU(cs);
298 CPUS390XState *env = &cpu->env;
299 struct kvm_sregs sregs;
300 struct kvm_regs regs;
301 struct kvm_fpu fpu = {};
302 int r;
303 int i;
305 /* always save the PSW and the GPRS*/
306 cs->kvm_run->psw_addr = env->psw.addr;
307 cs->kvm_run->psw_mask = env->psw.mask;
309 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
310 for (i = 0; i < 16; i++) {
311 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
312 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
314 } else {
315 for (i = 0; i < 16; i++) {
316 regs.gprs[i] = env->regs[i];
318 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
319 if (r < 0) {
320 return r;
324 /* Floating point */
325 for (i = 0; i < 16; i++) {
326 fpu.fprs[i] = env->fregs[i].ll;
328 fpu.fpc = env->fpc;
330 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
331 if (r < 0) {
332 return r;
335 /* Do we need to save more than that? */
336 if (level == KVM_PUT_RUNTIME_STATE) {
337 return 0;
340 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
341 cs->kvm_run->s.regs.cputm = env->cputm;
342 cs->kvm_run->s.regs.ckc = env->ckc;
343 cs->kvm_run->s.regs.todpr = env->todpr;
344 cs->kvm_run->s.regs.gbea = env->gbea;
345 cs->kvm_run->s.regs.pp = env->pp;
346 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
347 } else {
349 * These ONE_REGS are not protected by a capability. As they are only
350 * necessary for migration we just trace a possible error, but don't
351 * return with an error return code.
353 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
354 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
355 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
356 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
357 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
360 /* pfault parameters */
361 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
362 cs->kvm_run->s.regs.pft = env->pfault_token;
363 cs->kvm_run->s.regs.pfs = env->pfault_select;
364 cs->kvm_run->s.regs.pfc = env->pfault_compare;
365 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
366 } else if (cap_async_pf) {
367 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
368 if (r < 0) {
369 return r;
371 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
372 if (r < 0) {
373 return r;
375 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
376 if (r < 0) {
377 return r;
381 /* access registers and control registers*/
382 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
383 for (i = 0; i < 16; i++) {
384 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
385 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
387 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
388 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
389 } else {
390 for (i = 0; i < 16; i++) {
391 sregs.acrs[i] = env->aregs[i];
392 sregs.crs[i] = env->cregs[i];
394 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
395 if (r < 0) {
396 return r;
400 /* Finally the prefix */
401 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
402 cs->kvm_run->s.regs.prefix = env->psa;
403 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
404 } else {
405 /* prefix is only supported via sync regs */
407 return 0;
410 int kvm_arch_get_registers(CPUState *cs)
412 S390CPU *cpu = S390_CPU(cs);
413 CPUS390XState *env = &cpu->env;
414 struct kvm_sregs sregs;
415 struct kvm_regs regs;
416 struct kvm_fpu fpu;
417 int i, r;
419 /* get the PSW */
420 env->psw.addr = cs->kvm_run->psw_addr;
421 env->psw.mask = cs->kvm_run->psw_mask;
423 /* the GPRS */
424 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
425 for (i = 0; i < 16; i++) {
426 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
428 } else {
429 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
430 if (r < 0) {
431 return r;
433 for (i = 0; i < 16; i++) {
434 env->regs[i] = regs.gprs[i];
438 /* The ACRS and CRS */
439 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
440 for (i = 0; i < 16; i++) {
441 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
442 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
444 } else {
445 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
446 if (r < 0) {
447 return r;
449 for (i = 0; i < 16; i++) {
450 env->aregs[i] = sregs.acrs[i];
451 env->cregs[i] = sregs.crs[i];
455 /* Floating point */
456 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
457 if (r < 0) {
458 return r;
460 for (i = 0; i < 16; i++) {
461 env->fregs[i].ll = fpu.fprs[i];
463 env->fpc = fpu.fpc;
465 /* The prefix */
466 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
467 env->psa = cs->kvm_run->s.regs.prefix;
470 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
471 env->cputm = cs->kvm_run->s.regs.cputm;
472 env->ckc = cs->kvm_run->s.regs.ckc;
473 env->todpr = cs->kvm_run->s.regs.todpr;
474 env->gbea = cs->kvm_run->s.regs.gbea;
475 env->pp = cs->kvm_run->s.regs.pp;
476 } else {
478 * These ONE_REGS are not protected by a capability. As they are only
479 * necessary for migration we just trace a possible error, but don't
480 * return with an error return code.
482 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
483 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
484 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
485 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
486 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
489 /* pfault parameters */
490 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
491 env->pfault_token = cs->kvm_run->s.regs.pft;
492 env->pfault_select = cs->kvm_run->s.regs.pfs;
493 env->pfault_compare = cs->kvm_run->s.regs.pfc;
494 } else if (cap_async_pf) {
495 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
496 if (r < 0) {
497 return r;
499 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
500 if (r < 0) {
501 return r;
503 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
504 if (r < 0) {
505 return r;
509 return 0;
512 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
514 int r;
515 struct kvm_device_attr attr = {
516 .group = KVM_S390_VM_TOD,
517 .attr = KVM_S390_VM_TOD_LOW,
518 .addr = (uint64_t)tod_low,
521 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
522 if (r) {
523 return r;
526 attr.attr = KVM_S390_VM_TOD_HIGH;
527 attr.addr = (uint64_t)tod_high;
528 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
531 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
533 int r;
535 struct kvm_device_attr attr = {
536 .group = KVM_S390_VM_TOD,
537 .attr = KVM_S390_VM_TOD_LOW,
538 .addr = (uint64_t)tod_low,
541 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
542 if (r) {
543 return r;
546 attr.attr = KVM_S390_VM_TOD_HIGH;
547 attr.addr = (uint64_t)tod_high;
548 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
552 * Legacy layout for s390:
553 * Older S390 KVM requires the topmost vma of the RAM to be
554 * smaller than an system defined value, which is at least 256GB.
555 * Larger systems have larger values. We put the guest between
556 * the end of data segment (system break) and this value. We
557 * use 32GB as a base to have enough room for the system break
558 * to grow. We also have to use MAP parameters that avoid
559 * read-only mapping of guest pages.
561 static void *legacy_s390_alloc(size_t size, uint64_t *align)
563 void *mem;
565 mem = mmap((void *) 0x800000000ULL, size,
566 PROT_EXEC|PROT_READ|PROT_WRITE,
567 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
568 return mem == MAP_FAILED ? NULL : mem;
571 /* DIAG 501 is used for sw breakpoints */
572 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
574 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
577 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
578 sizeof(diag_501), 0) ||
579 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
580 sizeof(diag_501), 1)) {
581 return -EINVAL;
583 return 0;
586 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
588 uint8_t t[sizeof(diag_501)];
590 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
591 return -EINVAL;
592 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
593 return -EINVAL;
594 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
595 sizeof(diag_501), 1)) {
596 return -EINVAL;
599 return 0;
602 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
603 int len, int type)
605 int n;
607 for (n = 0; n < nb_hw_breakpoints; n++) {
608 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
609 (hw_breakpoints[n].len == len || len == -1)) {
610 return &hw_breakpoints[n];
614 return NULL;
617 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
619 int size;
621 if (find_hw_breakpoint(addr, len, type)) {
622 return -EEXIST;
625 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
627 if (!hw_breakpoints) {
628 nb_hw_breakpoints = 0;
629 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
630 } else {
631 hw_breakpoints =
632 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
635 if (!hw_breakpoints) {
636 nb_hw_breakpoints = 0;
637 return -ENOMEM;
640 hw_breakpoints[nb_hw_breakpoints].addr = addr;
641 hw_breakpoints[nb_hw_breakpoints].len = len;
642 hw_breakpoints[nb_hw_breakpoints].type = type;
644 nb_hw_breakpoints++;
646 return 0;
649 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
650 target_ulong len, int type)
652 switch (type) {
653 case GDB_BREAKPOINT_HW:
654 type = KVM_HW_BP;
655 break;
656 case GDB_WATCHPOINT_WRITE:
657 if (len < 1) {
658 return -EINVAL;
660 type = KVM_HW_WP_WRITE;
661 break;
662 default:
663 return -ENOSYS;
665 return insert_hw_breakpoint(addr, len, type);
668 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
669 target_ulong len, int type)
671 int size;
672 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
674 if (bp == NULL) {
675 return -ENOENT;
678 nb_hw_breakpoints--;
679 if (nb_hw_breakpoints > 0) {
681 * In order to trim the array, move the last element to the position to
682 * be removed - if necessary.
684 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
685 *bp = hw_breakpoints[nb_hw_breakpoints];
687 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
688 hw_breakpoints =
689 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
690 } else {
691 g_free(hw_breakpoints);
692 hw_breakpoints = NULL;
695 return 0;
698 void kvm_arch_remove_all_hw_breakpoints(void)
700 nb_hw_breakpoints = 0;
701 g_free(hw_breakpoints);
702 hw_breakpoints = NULL;
705 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
707 int i;
709 if (nb_hw_breakpoints > 0) {
710 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
711 dbg->arch.hw_bp = hw_breakpoints;
713 for (i = 0; i < nb_hw_breakpoints; ++i) {
714 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
715 hw_breakpoints[i].addr);
717 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
718 } else {
719 dbg->arch.nr_hw_bp = 0;
720 dbg->arch.hw_bp = NULL;
724 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
728 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
732 int kvm_arch_process_async_events(CPUState *cs)
734 return cs->halted;
737 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
738 struct kvm_s390_interrupt *interrupt)
740 int r = 0;
742 interrupt->type = irq->type;
743 switch (irq->type) {
744 case KVM_S390_INT_VIRTIO:
745 interrupt->parm = irq->u.ext.ext_params;
746 /* fall through */
747 case KVM_S390_INT_PFAULT_INIT:
748 case KVM_S390_INT_PFAULT_DONE:
749 interrupt->parm64 = irq->u.ext.ext_params2;
750 break;
751 case KVM_S390_PROGRAM_INT:
752 interrupt->parm = irq->u.pgm.code;
753 break;
754 case KVM_S390_SIGP_SET_PREFIX:
755 interrupt->parm = irq->u.prefix.address;
756 break;
757 case KVM_S390_INT_SERVICE:
758 interrupt->parm = irq->u.ext.ext_params;
759 break;
760 case KVM_S390_MCHK:
761 interrupt->parm = irq->u.mchk.cr14;
762 interrupt->parm64 = irq->u.mchk.mcic;
763 break;
764 case KVM_S390_INT_EXTERNAL_CALL:
765 interrupt->parm = irq->u.extcall.code;
766 break;
767 case KVM_S390_INT_EMERGENCY:
768 interrupt->parm = irq->u.emerg.code;
769 break;
770 case KVM_S390_SIGP_STOP:
771 case KVM_S390_RESTART:
772 break; /* These types have no parameters */
773 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
774 interrupt->parm = irq->u.io.subchannel_id << 16;
775 interrupt->parm |= irq->u.io.subchannel_nr;
776 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
777 interrupt->parm64 |= irq->u.io.io_int_word;
778 break;
779 default:
780 r = -EINVAL;
781 break;
783 return r;
786 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
788 struct kvm_s390_interrupt kvmint = {};
789 CPUState *cs = CPU(cpu);
790 int r;
792 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
793 if (r < 0) {
794 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
795 exit(1);
798 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
799 if (r < 0) {
800 fprintf(stderr, "KVM failed to inject interrupt\n");
801 exit(1);
805 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
807 struct kvm_s390_interrupt kvmint = {};
808 int r;
810 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
811 if (r < 0) {
812 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
813 exit(1);
816 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
817 if (r < 0) {
818 fprintf(stderr, "KVM failed to inject interrupt\n");
819 exit(1);
823 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
825 static bool use_flic = true;
826 int r;
828 if (use_flic) {
829 r = kvm_s390_inject_flic(irq);
830 if (r == -ENOSYS) {
831 use_flic = false;
833 if (!r) {
834 return;
837 __kvm_s390_floating_interrupt(irq);
840 void kvm_s390_virtio_irq(int config_change, uint64_t token)
842 struct kvm_s390_irq irq = {
843 .type = KVM_S390_INT_VIRTIO,
844 .u.ext.ext_params = config_change,
845 .u.ext.ext_params2 = token,
848 kvm_s390_floating_interrupt(&irq);
851 void kvm_s390_service_interrupt(uint32_t parm)
853 struct kvm_s390_irq irq = {
854 .type = KVM_S390_INT_SERVICE,
855 .u.ext.ext_params = parm,
858 kvm_s390_floating_interrupt(&irq);
861 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
863 struct kvm_s390_irq irq = {
864 .type = KVM_S390_PROGRAM_INT,
865 .u.pgm.code = code,
868 kvm_s390_vcpu_interrupt(cpu, &irq);
871 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
873 struct kvm_s390_irq irq = {
874 .type = KVM_S390_PROGRAM_INT,
875 .u.pgm.code = code,
876 .u.pgm.trans_exc_code = te_code,
877 .u.pgm.exc_access_id = te_code & 3,
880 kvm_s390_vcpu_interrupt(cpu, &irq);
883 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
884 uint16_t ipbh0)
886 CPUS390XState *env = &cpu->env;
887 uint64_t sccb;
888 uint32_t code;
889 int r = 0;
891 cpu_synchronize_state(CPU(cpu));
892 sccb = env->regs[ipbh0 & 0xf];
893 code = env->regs[(ipbh0 & 0xf0) >> 4];
895 r = sclp_service_call(env, sccb, code);
896 if (r < 0) {
897 enter_pgmcheck(cpu, -r);
898 } else {
899 setcc(cpu, r);
902 return 0;
905 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
907 CPUS390XState *env = &cpu->env;
908 int rc = 0;
909 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
911 cpu_synchronize_state(CPU(cpu));
913 switch (ipa1) {
914 case PRIV_B2_XSCH:
915 ioinst_handle_xsch(cpu, env->regs[1]);
916 break;
917 case PRIV_B2_CSCH:
918 ioinst_handle_csch(cpu, env->regs[1]);
919 break;
920 case PRIV_B2_HSCH:
921 ioinst_handle_hsch(cpu, env->regs[1]);
922 break;
923 case PRIV_B2_MSCH:
924 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
925 break;
926 case PRIV_B2_SSCH:
927 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
928 break;
929 case PRIV_B2_STCRW:
930 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
931 break;
932 case PRIV_B2_STSCH:
933 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
934 break;
935 case PRIV_B2_TSCH:
936 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
937 fprintf(stderr, "Spurious tsch intercept\n");
938 break;
939 case PRIV_B2_CHSC:
940 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
941 break;
942 case PRIV_B2_TPI:
943 /* This should have been handled by kvm already. */
944 fprintf(stderr, "Spurious tpi intercept\n");
945 break;
946 case PRIV_B2_SCHM:
947 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
948 run->s390_sieic.ipb);
949 break;
950 case PRIV_B2_RSCH:
951 ioinst_handle_rsch(cpu, env->regs[1]);
952 break;
953 case PRIV_B2_RCHP:
954 ioinst_handle_rchp(cpu, env->regs[1]);
955 break;
956 case PRIV_B2_STCPS:
957 /* We do not provide this instruction, it is suppressed. */
958 break;
959 case PRIV_B2_SAL:
960 ioinst_handle_sal(cpu, env->regs[1]);
961 break;
962 case PRIV_B2_SIGA:
963 /* Not provided, set CC = 3 for subchannel not operational */
964 setcc(cpu, 3);
965 break;
966 case PRIV_B2_SCLP_CALL:
967 rc = kvm_sclp_service_call(cpu, run, ipbh0);
968 break;
969 default:
970 rc = -1;
971 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
972 break;
975 return rc;
978 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run)
980 CPUS390XState *env = &cpu->env;
981 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
982 uint32_t base2 = run->s390_sieic.ipb >> 28;
983 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
984 ((run->s390_sieic.ipb & 0xff00) << 4);
986 if (disp2 & 0x80000) {
987 disp2 += 0xfff00000;
990 return (base2 ? env->regs[base2] : 0) +
991 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
994 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run)
996 CPUS390XState *env = &cpu->env;
997 uint32_t base2 = run->s390_sieic.ipb >> 28;
998 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
999 ((run->s390_sieic.ipb & 0xff00) << 4);
1001 if (disp2 & 0x80000) {
1002 disp2 += 0xfff00000;
1005 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1008 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1010 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1012 return clp_service_call(cpu, r2);
1015 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1017 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1018 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1020 return pcilg_service_call(cpu, r1, r2);
1023 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1025 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1026 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1028 return pcistg_service_call(cpu, r1, r2);
1031 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1033 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1034 uint64_t fiba;
1036 cpu_synchronize_state(CPU(cpu));
1037 fiba = get_base_disp_rxy(cpu, run);
1039 return stpcifc_service_call(cpu, r1, fiba);
1042 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1044 /* NOOP */
1045 return 0;
1048 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1050 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1051 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1053 return rpcit_service_call(cpu, r1, r2);
1056 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1058 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1059 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1060 uint64_t gaddr;
1062 cpu_synchronize_state(CPU(cpu));
1063 gaddr = get_base_disp_rsy(cpu, run);
1065 return pcistb_service_call(cpu, r1, r3, gaddr);
1068 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1070 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1071 uint64_t fiba;
1073 cpu_synchronize_state(CPU(cpu));
1074 fiba = get_base_disp_rxy(cpu, run);
1076 return mpcifc_service_call(cpu, r1, fiba);
1079 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1081 int r = 0;
1083 switch (ipa1) {
1084 case PRIV_B9_CLP:
1085 r = kvm_clp_service_call(cpu, run);
1086 break;
1087 case PRIV_B9_PCISTG:
1088 r = kvm_pcistg_service_call(cpu, run);
1089 break;
1090 case PRIV_B9_PCILG:
1091 r = kvm_pcilg_service_call(cpu, run);
1092 break;
1093 case PRIV_B9_RPCIT:
1094 r = kvm_rpcit_service_call(cpu, run);
1095 break;
1096 case PRIV_B9_EQBS:
1097 /* just inject exception */
1098 r = -1;
1099 break;
1100 default:
1101 r = -1;
1102 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1103 break;
1106 return r;
1109 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1111 int r = 0;
1113 switch (ipbl) {
1114 case PRIV_EB_PCISTB:
1115 r = kvm_pcistb_service_call(cpu, run);
1116 break;
1117 case PRIV_EB_SIC:
1118 r = kvm_sic_service_call(cpu, run);
1119 break;
1120 case PRIV_EB_SQBS:
1121 /* just inject exception */
1122 r = -1;
1123 break;
1124 default:
1125 r = -1;
1126 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1127 break;
1130 return r;
1133 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1135 int r = 0;
1137 switch (ipbl) {
1138 case PRIV_E3_MPCIFC:
1139 r = kvm_mpcifc_service_call(cpu, run);
1140 break;
1141 case PRIV_E3_STPCIFC:
1142 r = kvm_stpcifc_service_call(cpu, run);
1143 break;
1144 default:
1145 r = -1;
1146 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1147 break;
1150 return r;
1153 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1155 CPUS390XState *env = &cpu->env;
1156 int ret;
1158 cpu_synchronize_state(CPU(cpu));
1159 ret = s390_virtio_hypercall(env);
1160 if (ret == -EINVAL) {
1161 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1162 return 0;
1165 return ret;
1168 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1170 uint64_t r1, r3;
1172 cpu_synchronize_state(CPU(cpu));
1173 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1174 r3 = run->s390_sieic.ipa & 0x000f;
1175 handle_diag_308(&cpu->env, r1, r3);
1178 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1180 CPUS390XState *env = &cpu->env;
1181 unsigned long pc;
1183 cpu_synchronize_state(CPU(cpu));
1185 pc = env->psw.addr - 4;
1186 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1187 env->psw.addr = pc;
1188 return EXCP_DEBUG;
1191 return -ENOENT;
1194 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1196 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1198 int r = 0;
1199 uint16_t func_code;
1202 * For any diagnose call we support, bits 48-63 of the resulting
1203 * address specify the function code; the remainder is ignored.
1205 func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
1206 switch (func_code) {
1207 case DIAG_IPL:
1208 kvm_handle_diag_308(cpu, run);
1209 break;
1210 case DIAG_KVM_HYPERCALL:
1211 r = handle_hypercall(cpu, run);
1212 break;
1213 case DIAG_KVM_BREAKPOINT:
1214 r = handle_sw_breakpoint(cpu, run);
1215 break;
1216 default:
1217 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1218 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1219 break;
1222 return r;
1225 typedef struct SigpInfo {
1226 S390CPU *cpu;
1227 uint64_t param;
1228 int cc;
1229 uint64_t *status_reg;
1230 } SigpInfo;
1232 static void set_sigp_status(SigpInfo *si, uint64_t status)
1234 *si->status_reg &= 0xffffffff00000000ULL;
1235 *si->status_reg |= status;
1236 si->cc = SIGP_CC_STATUS_STORED;
1239 static void sigp_start(void *arg)
1241 SigpInfo *si = arg;
1243 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1244 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1245 return;
1248 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1249 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1252 static void sigp_stop(void *arg)
1254 SigpInfo *si = arg;
1255 struct kvm_s390_irq irq = {
1256 .type = KVM_S390_SIGP_STOP,
1259 if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1260 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1261 return;
1264 /* disabled wait - sleeping in user space */
1265 if (CPU(si->cpu)->halted) {
1266 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1267 } else {
1268 /* execute the stop function */
1269 si->cpu->env.sigp_order = SIGP_STOP;
1270 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1272 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1275 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1276 #define SAVE_AREA_SIZE 512
1277 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1279 static const uint8_t ar_id = 1;
1280 uint64_t ckc = cpu->env.ckc >> 8;
1281 void *mem;
1282 hwaddr len = SAVE_AREA_SIZE;
1284 mem = cpu_physical_memory_map(addr, &len, 1);
1285 if (!mem) {
1286 return -EFAULT;
1288 if (len != SAVE_AREA_SIZE) {
1289 cpu_physical_memory_unmap(mem, len, 1, 0);
1290 return -EFAULT;
1293 if (store_arch) {
1294 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1296 memcpy(mem, &cpu->env.fregs, 128);
1297 memcpy(mem + 128, &cpu->env.regs, 128);
1298 memcpy(mem + 256, &cpu->env.psw, 16);
1299 memcpy(mem + 280, &cpu->env.psa, 4);
1300 memcpy(mem + 284, &cpu->env.fpc, 4);
1301 memcpy(mem + 292, &cpu->env.todpr, 4);
1302 memcpy(mem + 296, &cpu->env.cputm, 8);
1303 memcpy(mem + 304, &ckc, 8);
1304 memcpy(mem + 320, &cpu->env.aregs, 64);
1305 memcpy(mem + 384, &cpu->env.cregs, 128);
1307 cpu_physical_memory_unmap(mem, len, 1, len);
1309 return 0;
1312 static void sigp_stop_and_store_status(void *arg)
1314 SigpInfo *si = arg;
1315 struct kvm_s390_irq irq = {
1316 .type = KVM_S390_SIGP_STOP,
1319 /* disabled wait - sleeping in user space */
1320 if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1321 CPU(si->cpu)->halted) {
1322 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1325 switch (s390_cpu_get_state(si->cpu)) {
1326 case CPU_STATE_OPERATING:
1327 si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1328 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1329 /* store will be performed when handling the stop intercept */
1330 break;
1331 case CPU_STATE_STOPPED:
1332 /* already stopped, just store the status */
1333 cpu_synchronize_state(CPU(si->cpu));
1334 kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1335 break;
1337 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1340 static void sigp_store_status_at_address(void *arg)
1342 SigpInfo *si = arg;
1343 uint32_t address = si->param & 0x7ffffe00u;
1345 /* cpu has to be stopped */
1346 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1347 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1348 return;
1351 cpu_synchronize_state(CPU(si->cpu));
1353 if (kvm_s390_store_status(si->cpu, address, false)) {
1354 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1355 return;
1357 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1360 static void sigp_restart(void *arg)
1362 SigpInfo *si = arg;
1363 struct kvm_s390_irq irq = {
1364 .type = KVM_S390_RESTART,
1367 switch (s390_cpu_get_state(si->cpu)) {
1368 case CPU_STATE_STOPPED:
1369 /* the restart irq has to be delivered prior to any other pending irq */
1370 cpu_synchronize_state(CPU(si->cpu));
1371 do_restart_interrupt(&si->cpu->env);
1372 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1373 break;
1374 case CPU_STATE_OPERATING:
1375 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1376 break;
1378 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1381 int kvm_s390_cpu_restart(S390CPU *cpu)
1383 SigpInfo si = {
1384 .cpu = cpu,
1387 run_on_cpu(CPU(cpu), sigp_restart, &si);
1388 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1389 return 0;
1392 static void sigp_initial_cpu_reset(void *arg)
1394 SigpInfo *si = arg;
1395 CPUState *cs = CPU(si->cpu);
1396 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1398 cpu_synchronize_state(cs);
1399 scc->initial_cpu_reset(cs);
1400 cpu_synchronize_post_reset(cs);
1401 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1404 static void sigp_cpu_reset(void *arg)
1406 SigpInfo *si = arg;
1407 CPUState *cs = CPU(si->cpu);
1408 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1410 cpu_synchronize_state(cs);
1411 scc->cpu_reset(cs);
1412 cpu_synchronize_post_reset(cs);
1413 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1416 static void sigp_set_prefix(void *arg)
1418 SigpInfo *si = arg;
1419 uint32_t addr = si->param & 0x7fffe000u;
1421 cpu_synchronize_state(CPU(si->cpu));
1423 if (!address_space_access_valid(&address_space_memory, addr,
1424 sizeof(struct LowCore), false)) {
1425 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1426 return;
1429 /* cpu has to be stopped */
1430 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1431 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1432 return;
1435 si->cpu->env.psa = addr;
1436 cpu_synchronize_post_init(CPU(si->cpu));
1437 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1440 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1441 uint64_t param, uint64_t *status_reg)
1443 SigpInfo si = {
1444 .cpu = dst_cpu,
1445 .param = param,
1446 .status_reg = status_reg,
1449 /* cpu available? */
1450 if (dst_cpu == NULL) {
1451 return SIGP_CC_NOT_OPERATIONAL;
1454 /* only resets can break pending orders */
1455 if (dst_cpu->env.sigp_order != 0 &&
1456 order != SIGP_CPU_RESET &&
1457 order != SIGP_INITIAL_CPU_RESET) {
1458 return SIGP_CC_BUSY;
1461 switch (order) {
1462 case SIGP_START:
1463 run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1464 break;
1465 case SIGP_STOP:
1466 run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1467 break;
1468 case SIGP_RESTART:
1469 run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1470 break;
1471 case SIGP_STOP_STORE_STATUS:
1472 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1473 break;
1474 case SIGP_STORE_STATUS_ADDR:
1475 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1476 break;
1477 case SIGP_SET_PREFIX:
1478 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1479 break;
1480 case SIGP_INITIAL_CPU_RESET:
1481 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1482 break;
1483 case SIGP_CPU_RESET:
1484 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1485 break;
1486 default:
1487 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1488 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1491 return si.cc;
1494 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1495 uint64_t *status_reg)
1497 CPUState *cur_cs;
1498 S390CPU *cur_cpu;
1500 /* due to the BQL, we are the only active cpu */
1501 CPU_FOREACH(cur_cs) {
1502 cur_cpu = S390_CPU(cur_cs);
1503 if (cur_cpu->env.sigp_order != 0) {
1504 return SIGP_CC_BUSY;
1506 cpu_synchronize_state(cur_cs);
1507 /* all but the current one have to be stopped */
1508 if (cur_cpu != cpu &&
1509 s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1510 *status_reg &= 0xffffffff00000000ULL;
1511 *status_reg |= SIGP_STAT_INCORRECT_STATE;
1512 return SIGP_CC_STATUS_STORED;
1516 switch (param & 0xff) {
1517 case SIGP_MODE_ESA_S390:
1518 /* not supported */
1519 return SIGP_CC_NOT_OPERATIONAL;
1520 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1521 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1522 CPU_FOREACH(cur_cs) {
1523 cur_cpu = S390_CPU(cur_cs);
1524 cur_cpu->env.pfault_token = -1UL;
1526 break;
1527 default:
1528 *status_reg &= 0xffffffff00000000ULL;
1529 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1530 return SIGP_CC_STATUS_STORED;
1533 return SIGP_CC_ORDER_CODE_ACCEPTED;
1536 #define SIGP_ORDER_MASK 0x000000ff
1538 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1540 CPUS390XState *env = &cpu->env;
1541 const uint8_t r1 = ipa1 >> 4;
1542 const uint8_t r3 = ipa1 & 0x0f;
1543 int ret;
1544 uint8_t order;
1545 uint64_t *status_reg;
1546 uint64_t param;
1547 S390CPU *dst_cpu = NULL;
1549 cpu_synchronize_state(CPU(cpu));
1551 /* get order code */
1552 order = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
1553 status_reg = &env->regs[r1];
1554 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1556 switch (order) {
1557 case SIGP_SET_ARCH:
1558 ret = sigp_set_architecture(cpu, param, status_reg);
1559 break;
1560 default:
1561 /* all other sigp orders target a single vcpu */
1562 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1563 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1566 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1567 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1569 if (ret >= 0) {
1570 setcc(cpu, ret);
1571 return 0;
1574 return ret;
1577 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1579 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1580 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1581 int r = -1;
1583 DPRINTF("handle_instruction 0x%x 0x%x\n",
1584 run->s390_sieic.ipa, run->s390_sieic.ipb);
1585 switch (ipa0) {
1586 case IPA0_B2:
1587 r = handle_b2(cpu, run, ipa1);
1588 break;
1589 case IPA0_B9:
1590 r = handle_b9(cpu, run, ipa1);
1591 break;
1592 case IPA0_EB:
1593 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1594 break;
1595 case IPA0_E3:
1596 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1597 break;
1598 case IPA0_DIAG:
1599 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1600 break;
1601 case IPA0_SIGP:
1602 r = handle_sigp(cpu, run, ipa1);
1603 break;
1606 if (r < 0) {
1607 r = 0;
1608 enter_pgmcheck(cpu, 0x0001);
1611 return r;
1614 static bool is_special_wait_psw(CPUState *cs)
1616 /* signal quiesce */
1617 return cs->kvm_run->psw_addr == 0xfffUL;
1620 static void guest_panicked(void)
1622 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1623 &error_abort);
1624 vm_stop(RUN_STATE_GUEST_PANICKED);
1627 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1629 CPUState *cs = CPU(cpu);
1631 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1632 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1633 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1634 s390_cpu_halt(cpu);
1635 guest_panicked();
1638 static int handle_intercept(S390CPU *cpu)
1640 CPUState *cs = CPU(cpu);
1641 struct kvm_run *run = cs->kvm_run;
1642 int icpt_code = run->s390_sieic.icptcode;
1643 int r = 0;
1645 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1646 (long)cs->kvm_run->psw_addr);
1647 switch (icpt_code) {
1648 case ICPT_INSTRUCTION:
1649 r = handle_instruction(cpu, run);
1650 break;
1651 case ICPT_PROGRAM:
1652 unmanageable_intercept(cpu, "program interrupt",
1653 offsetof(LowCore, program_new_psw));
1654 r = EXCP_HALTED;
1655 break;
1656 case ICPT_EXT_INT:
1657 unmanageable_intercept(cpu, "external interrupt",
1658 offsetof(LowCore, external_new_psw));
1659 r = EXCP_HALTED;
1660 break;
1661 case ICPT_WAITPSW:
1662 /* disabled wait, since enabled wait is handled in kernel */
1663 cpu_synchronize_state(cs);
1664 if (s390_cpu_halt(cpu) == 0) {
1665 if (is_special_wait_psw(cs)) {
1666 qemu_system_shutdown_request();
1667 } else {
1668 guest_panicked();
1671 r = EXCP_HALTED;
1672 break;
1673 case ICPT_CPU_STOP:
1674 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1675 qemu_system_shutdown_request();
1677 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1678 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1679 true);
1681 cpu->env.sigp_order = 0;
1682 r = EXCP_HALTED;
1683 break;
1684 case ICPT_SOFT_INTERCEPT:
1685 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1686 exit(1);
1687 break;
1688 case ICPT_IO:
1689 fprintf(stderr, "KVM unimplemented icpt IO\n");
1690 exit(1);
1691 break;
1692 default:
1693 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1694 exit(1);
1695 break;
1698 return r;
1701 static int handle_tsch(S390CPU *cpu)
1703 CPUState *cs = CPU(cpu);
1704 struct kvm_run *run = cs->kvm_run;
1705 int ret;
1707 cpu_synchronize_state(cs);
1709 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1710 if (ret < 0) {
1712 * Failure.
1713 * If an I/O interrupt had been dequeued, we have to reinject it.
1715 if (run->s390_tsch.dequeued) {
1716 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1717 run->s390_tsch.subchannel_nr,
1718 run->s390_tsch.io_int_parm,
1719 run->s390_tsch.io_int_word);
1721 ret = 0;
1723 return ret;
1726 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1728 CPUState *cs = CPU(cpu);
1729 struct kvm_run *run = cs->kvm_run;
1731 int ret = 0;
1732 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1734 switch (arch_info->type) {
1735 case KVM_HW_WP_WRITE:
1736 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1737 cs->watchpoint_hit = &hw_watchpoint;
1738 hw_watchpoint.vaddr = arch_info->addr;
1739 hw_watchpoint.flags = BP_MEM_WRITE;
1740 ret = EXCP_DEBUG;
1742 break;
1743 case KVM_HW_BP:
1744 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1745 ret = EXCP_DEBUG;
1747 break;
1748 case KVM_SINGLESTEP:
1749 if (cs->singlestep_enabled) {
1750 ret = EXCP_DEBUG;
1752 break;
1753 default:
1754 ret = -ENOSYS;
1757 return ret;
1760 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1762 S390CPU *cpu = S390_CPU(cs);
1763 int ret = 0;
1765 switch (run->exit_reason) {
1766 case KVM_EXIT_S390_SIEIC:
1767 ret = handle_intercept(cpu);
1768 break;
1769 case KVM_EXIT_S390_RESET:
1770 s390_reipl_request();
1771 break;
1772 case KVM_EXIT_S390_TSCH:
1773 ret = handle_tsch(cpu);
1774 break;
1775 case KVM_EXIT_DEBUG:
1776 ret = kvm_arch_handle_debug_exit(cpu);
1777 break;
1778 default:
1779 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1780 break;
1783 if (ret == 0) {
1784 ret = EXCP_INTERRUPT;
1786 return ret;
1789 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1791 return true;
1794 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
1796 return 1;
1799 int kvm_arch_on_sigbus(int code, void *addr)
1801 return 1;
1804 void kvm_s390_io_interrupt(uint16_t subchannel_id,
1805 uint16_t subchannel_nr, uint32_t io_int_parm,
1806 uint32_t io_int_word)
1808 struct kvm_s390_irq irq = {
1809 .u.io.subchannel_id = subchannel_id,
1810 .u.io.subchannel_nr = subchannel_nr,
1811 .u.io.io_int_parm = io_int_parm,
1812 .u.io.io_int_word = io_int_word,
1815 if (io_int_word & IO_INT_WORD_AI) {
1816 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
1817 } else {
1818 irq.type = ((subchannel_id & 0xff00) << 24) |
1819 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
1821 kvm_s390_floating_interrupt(&irq);
1824 void kvm_s390_crw_mchk(void)
1826 struct kvm_s390_irq irq = {
1827 .type = KVM_S390_MCHK,
1828 .u.mchk.cr14 = 1 << 28,
1829 .u.mchk.mcic = 0x00400f1d40330000ULL,
1831 kvm_s390_floating_interrupt(&irq);
1834 void kvm_s390_enable_css_support(S390CPU *cpu)
1836 int r;
1838 /* Activate host kernel channel subsystem support. */
1839 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1840 assert(r == 0);
1843 void kvm_arch_init_irq_routing(KVMState *s)
1846 * Note that while irqchip capabilities generally imply that cpustates
1847 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1848 * have to override the common code kvm_halt_in_kernel_allowed setting.
1850 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1851 kvm_gsi_routing_allowed = true;
1852 kvm_halt_in_kernel_allowed = false;
1856 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1857 int vq, bool assign)
1859 struct kvm_ioeventfd kick = {
1860 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1861 KVM_IOEVENTFD_FLAG_DATAMATCH,
1862 .fd = event_notifier_get_fd(notifier),
1863 .datamatch = vq,
1864 .addr = sch,
1865 .len = 8,
1867 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1868 return -ENOSYS;
1870 if (!assign) {
1871 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1873 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1876 int kvm_s390_get_memslot_count(KVMState *s)
1878 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
1881 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1883 struct kvm_mp_state mp_state = {};
1884 int ret;
1886 /* the kvm part might not have been initialized yet */
1887 if (CPU(cpu)->kvm_state == NULL) {
1888 return 0;
1891 switch (cpu_state) {
1892 case CPU_STATE_STOPPED:
1893 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1894 break;
1895 case CPU_STATE_CHECK_STOP:
1896 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1897 break;
1898 case CPU_STATE_OPERATING:
1899 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1900 break;
1901 case CPU_STATE_LOAD:
1902 mp_state.mp_state = KVM_MP_STATE_LOAD;
1903 break;
1904 default:
1905 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1906 cpu_state);
1907 exit(1);
1910 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1911 if (ret) {
1912 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1913 strerror(-ret));
1916 return ret;
1919 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1920 uint64_t address, uint32_t data)
1922 S390PCIBusDevice *pbdev;
1923 uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
1924 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
1926 pbdev = s390_pci_find_dev_by_fid(fid);
1927 if (!pbdev) {
1928 DPRINTF("add_msi_route no dev\n");
1929 return -ENODEV;
1932 pbdev->routes.adapter.ind_offset = vec;
1934 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
1935 route->flags = 0;
1936 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
1937 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
1938 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
1939 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
1940 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
1941 return 0;