vfio: Use g_new() & friends where that makes obvious sense
[qemu/kevin.git] / target-s390x / kvm.c
blobc3be180de24898cd5cfdbc85a9298f91275f49f4
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/error-report.h"
33 #include "qemu/timer.h"
34 #include "sysemu/sysemu.h"
35 #include "sysemu/kvm.h"
36 #include "hw/hw.h"
37 #include "cpu.h"
38 #include "sysemu/device_tree.h"
39 #include "qapi/qmp/qjson.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"
47 #include "hw/s390x/ebcdic.h"
48 #include "exec/memattrs.h"
50 /* #define DEBUG_KVM */
52 #ifdef DEBUG_KVM
53 #define DPRINTF(fmt, ...) \
54 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
55 #else
56 #define DPRINTF(fmt, ...) \
57 do { } while (0)
58 #endif
60 #define kvm_vm_check_mem_attr(s, attr) \
61 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
63 #define IPA0_DIAG 0x8300
64 #define IPA0_SIGP 0xae00
65 #define IPA0_B2 0xb200
66 #define IPA0_B9 0xb900
67 #define IPA0_EB 0xeb00
68 #define IPA0_E3 0xe300
70 #define PRIV_B2_SCLP_CALL 0x20
71 #define PRIV_B2_CSCH 0x30
72 #define PRIV_B2_HSCH 0x31
73 #define PRIV_B2_MSCH 0x32
74 #define PRIV_B2_SSCH 0x33
75 #define PRIV_B2_STSCH 0x34
76 #define PRIV_B2_TSCH 0x35
77 #define PRIV_B2_TPI 0x36
78 #define PRIV_B2_SAL 0x37
79 #define PRIV_B2_RSCH 0x38
80 #define PRIV_B2_STCRW 0x39
81 #define PRIV_B2_STCPS 0x3a
82 #define PRIV_B2_RCHP 0x3b
83 #define PRIV_B2_SCHM 0x3c
84 #define PRIV_B2_CHSC 0x5f
85 #define PRIV_B2_SIGA 0x74
86 #define PRIV_B2_XSCH 0x76
88 #define PRIV_EB_SQBS 0x8a
89 #define PRIV_EB_PCISTB 0xd0
90 #define PRIV_EB_SIC 0xd1
92 #define PRIV_B9_EQBS 0x9c
93 #define PRIV_B9_CLP 0xa0
94 #define PRIV_B9_PCISTG 0xd0
95 #define PRIV_B9_PCILG 0xd2
96 #define PRIV_B9_RPCIT 0xd3
98 #define PRIV_E3_MPCIFC 0xd0
99 #define PRIV_E3_STPCIFC 0xd4
101 #define DIAG_TIMEREVENT 0x288
102 #define DIAG_IPL 0x308
103 #define DIAG_KVM_HYPERCALL 0x500
104 #define DIAG_KVM_BREAKPOINT 0x501
106 #define ICPT_INSTRUCTION 0x04
107 #define ICPT_PROGRAM 0x08
108 #define ICPT_EXT_INT 0x14
109 #define ICPT_WAITPSW 0x1c
110 #define ICPT_SOFT_INTERCEPT 0x24
111 #define ICPT_CPU_STOP 0x28
112 #define ICPT_IO 0x40
114 #define NR_LOCAL_IRQS 32
116 * Needs to be big enough to contain max_cpus emergency signals
117 * and in addition NR_LOCAL_IRQS interrupts
119 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
120 (max_cpus + NR_LOCAL_IRQS))
122 static CPUWatchpoint hw_watchpoint;
124 * We don't use a list because this structure is also used to transmit the
125 * hardware breakpoints to the kernel.
127 static struct kvm_hw_breakpoint *hw_breakpoints;
128 static int nb_hw_breakpoints;
130 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
131 KVM_CAP_LAST_INFO
134 static int cap_sync_regs;
135 static int cap_async_pf;
136 static int cap_mem_op;
137 static int cap_s390_irq;
139 static void *legacy_s390_alloc(size_t size, uint64_t *align);
141 static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
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) memory_limit,
149 return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
152 int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
154 int rc;
156 struct kvm_device_attr attr = {
157 .group = KVM_S390_VM_MEM_CTRL,
158 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
159 .addr = (uint64_t) &new_limit,
162 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_LIMIT_SIZE)) {
163 return 0;
166 rc = kvm_s390_query_mem_limit(s, hw_limit);
167 if (rc) {
168 return rc;
169 } else if (*hw_limit < new_limit) {
170 return -E2BIG;
173 return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
176 void kvm_s390_cmma_reset(void)
178 int rc;
179 struct kvm_device_attr attr = {
180 .group = KVM_S390_VM_MEM_CTRL,
181 .attr = KVM_S390_VM_MEM_CLR_CMMA,
184 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
185 trace_kvm_clear_cmma(rc);
188 static void kvm_s390_enable_cmma(KVMState *s)
190 int rc;
191 struct kvm_device_attr attr = {
192 .group = KVM_S390_VM_MEM_CTRL,
193 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
196 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_ENABLE_CMMA) ||
197 !kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_CLR_CMMA)) {
198 return;
201 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
202 trace_kvm_enable_cmma(rc);
205 static void kvm_s390_set_attr(uint64_t attr)
207 struct kvm_device_attr attribute = {
208 .group = KVM_S390_VM_CRYPTO,
209 .attr = attr,
212 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
214 if (ret) {
215 error_report("Failed to set crypto device attribute %lu: %s",
216 attr, strerror(-ret));
220 static void kvm_s390_init_aes_kw(void)
222 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
224 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
225 NULL)) {
226 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
229 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
230 kvm_s390_set_attr(attr);
234 static void kvm_s390_init_dea_kw(void)
236 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
238 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
239 NULL)) {
240 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
243 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
244 kvm_s390_set_attr(attr);
248 void kvm_s390_crypto_reset(void)
250 kvm_s390_init_aes_kw();
251 kvm_s390_init_dea_kw();
254 int kvm_arch_init(MachineState *ms, KVMState *s)
256 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
257 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
258 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
259 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
261 kvm_s390_enable_cmma(s);
263 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
264 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
265 phys_mem_set_alloc(legacy_s390_alloc);
268 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
269 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
270 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
272 return 0;
275 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
277 return cpu->cpu_index;
280 int kvm_arch_init_vcpu(CPUState *cs)
282 S390CPU *cpu = S390_CPU(cs);
283 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
284 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
285 return 0;
288 void kvm_s390_reset_vcpu(S390CPU *cpu)
290 CPUState *cs = CPU(cpu);
292 /* The initial reset call is needed here to reset in-kernel
293 * vcpu data that we can't access directly from QEMU
294 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
295 * Before this ioctl cpu_synchronize_state() is called in common kvm
296 * code (kvm-all) */
297 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
298 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
302 static int can_sync_regs(CPUState *cs, int regs)
304 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
307 int kvm_arch_put_registers(CPUState *cs, int level)
309 S390CPU *cpu = S390_CPU(cs);
310 CPUS390XState *env = &cpu->env;
311 struct kvm_sregs sregs;
312 struct kvm_regs regs;
313 struct kvm_fpu fpu = {};
314 int r;
315 int i;
317 /* always save the PSW and the GPRS*/
318 cs->kvm_run->psw_addr = env->psw.addr;
319 cs->kvm_run->psw_mask = env->psw.mask;
321 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
322 for (i = 0; i < 16; i++) {
323 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
324 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
326 } else {
327 for (i = 0; i < 16; i++) {
328 regs.gprs[i] = env->regs[i];
330 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
331 if (r < 0) {
332 return r;
336 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
337 for (i = 0; i < 32; i++) {
338 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
339 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
341 cs->kvm_run->s.regs.fpc = env->fpc;
342 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
343 } else {
344 /* Floating point */
345 for (i = 0; i < 16; i++) {
346 fpu.fprs[i] = get_freg(env, i)->ll;
348 fpu.fpc = env->fpc;
350 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
351 if (r < 0) {
352 return r;
356 /* Do we need to save more than that? */
357 if (level == KVM_PUT_RUNTIME_STATE) {
358 return 0;
361 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
362 cs->kvm_run->s.regs.cputm = env->cputm;
363 cs->kvm_run->s.regs.ckc = env->ckc;
364 cs->kvm_run->s.regs.todpr = env->todpr;
365 cs->kvm_run->s.regs.gbea = env->gbea;
366 cs->kvm_run->s.regs.pp = env->pp;
367 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
368 } else {
370 * These ONE_REGS are not protected by a capability. As they are only
371 * necessary for migration we just trace a possible error, but don't
372 * return with an error return code.
374 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
375 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
376 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
377 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
378 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
381 /* pfault parameters */
382 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
383 cs->kvm_run->s.regs.pft = env->pfault_token;
384 cs->kvm_run->s.regs.pfs = env->pfault_select;
385 cs->kvm_run->s.regs.pfc = env->pfault_compare;
386 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
387 } else if (cap_async_pf) {
388 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
389 if (r < 0) {
390 return r;
392 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
393 if (r < 0) {
394 return r;
396 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
397 if (r < 0) {
398 return r;
402 /* access registers and control registers*/
403 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
404 for (i = 0; i < 16; i++) {
405 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
406 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
408 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
409 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
410 } else {
411 for (i = 0; i < 16; i++) {
412 sregs.acrs[i] = env->aregs[i];
413 sregs.crs[i] = env->cregs[i];
415 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
416 if (r < 0) {
417 return r;
421 /* Finally the prefix */
422 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
423 cs->kvm_run->s.regs.prefix = env->psa;
424 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
425 } else {
426 /* prefix is only supported via sync regs */
428 return 0;
431 int kvm_arch_get_registers(CPUState *cs)
433 S390CPU *cpu = S390_CPU(cs);
434 CPUS390XState *env = &cpu->env;
435 struct kvm_sregs sregs;
436 struct kvm_regs regs;
437 struct kvm_fpu fpu;
438 int i, r;
440 /* get the PSW */
441 env->psw.addr = cs->kvm_run->psw_addr;
442 env->psw.mask = cs->kvm_run->psw_mask;
444 /* the GPRS */
445 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
446 for (i = 0; i < 16; i++) {
447 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
449 } else {
450 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
451 if (r < 0) {
452 return r;
454 for (i = 0; i < 16; i++) {
455 env->regs[i] = regs.gprs[i];
459 /* The ACRS and CRS */
460 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
461 for (i = 0; i < 16; i++) {
462 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
463 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
465 } else {
466 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
467 if (r < 0) {
468 return r;
470 for (i = 0; i < 16; i++) {
471 env->aregs[i] = sregs.acrs[i];
472 env->cregs[i] = sregs.crs[i];
476 /* Floating point and vector registers */
477 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
478 for (i = 0; i < 32; i++) {
479 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
480 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
482 env->fpc = cs->kvm_run->s.regs.fpc;
483 } else {
484 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
485 if (r < 0) {
486 return r;
488 for (i = 0; i < 16; i++) {
489 get_freg(env, i)->ll = fpu.fprs[i];
491 env->fpc = fpu.fpc;
494 /* The prefix */
495 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
496 env->psa = cs->kvm_run->s.regs.prefix;
499 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
500 env->cputm = cs->kvm_run->s.regs.cputm;
501 env->ckc = cs->kvm_run->s.regs.ckc;
502 env->todpr = cs->kvm_run->s.regs.todpr;
503 env->gbea = cs->kvm_run->s.regs.gbea;
504 env->pp = cs->kvm_run->s.regs.pp;
505 } else {
507 * These ONE_REGS are not protected by a capability. As they are only
508 * necessary for migration we just trace a possible error, but don't
509 * return with an error return code.
511 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
512 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
513 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
514 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
515 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
518 /* pfault parameters */
519 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
520 env->pfault_token = cs->kvm_run->s.regs.pft;
521 env->pfault_select = cs->kvm_run->s.regs.pfs;
522 env->pfault_compare = cs->kvm_run->s.regs.pfc;
523 } else if (cap_async_pf) {
524 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
525 if (r < 0) {
526 return r;
528 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
529 if (r < 0) {
530 return r;
532 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
533 if (r < 0) {
534 return r;
538 return 0;
541 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
543 int r;
544 struct kvm_device_attr attr = {
545 .group = KVM_S390_VM_TOD,
546 .attr = KVM_S390_VM_TOD_LOW,
547 .addr = (uint64_t)tod_low,
550 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
551 if (r) {
552 return r;
555 attr.attr = KVM_S390_VM_TOD_HIGH;
556 attr.addr = (uint64_t)tod_high;
557 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
560 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
562 int r;
564 struct kvm_device_attr attr = {
565 .group = KVM_S390_VM_TOD,
566 .attr = KVM_S390_VM_TOD_LOW,
567 .addr = (uint64_t)tod_low,
570 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
571 if (r) {
572 return r;
575 attr.attr = KVM_S390_VM_TOD_HIGH;
576 attr.addr = (uint64_t)tod_high;
577 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
581 * kvm_s390_mem_op:
582 * @addr: the logical start address in guest memory
583 * @ar: the access register number
584 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
585 * @len: length that should be transferred
586 * @is_write: true = write, false = read
587 * Returns: 0 on success, non-zero if an exception or error occurred
589 * Use KVM ioctl to read/write from/to guest memory. An access exception
590 * is injected into the vCPU in case of translation errors.
592 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
593 int len, bool is_write)
595 struct kvm_s390_mem_op mem_op = {
596 .gaddr = addr,
597 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
598 .size = len,
599 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
600 : KVM_S390_MEMOP_LOGICAL_READ,
601 .buf = (uint64_t)hostbuf,
602 .ar = ar,
604 int ret;
606 if (!cap_mem_op) {
607 return -ENOSYS;
609 if (!hostbuf) {
610 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
613 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
614 if (ret < 0) {
615 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
617 return ret;
621 * Legacy layout for s390:
622 * Older S390 KVM requires the topmost vma of the RAM to be
623 * smaller than an system defined value, which is at least 256GB.
624 * Larger systems have larger values. We put the guest between
625 * the end of data segment (system break) and this value. We
626 * use 32GB as a base to have enough room for the system break
627 * to grow. We also have to use MAP parameters that avoid
628 * read-only mapping of guest pages.
630 static void *legacy_s390_alloc(size_t size, uint64_t *align)
632 void *mem;
634 mem = mmap((void *) 0x800000000ULL, size,
635 PROT_EXEC|PROT_READ|PROT_WRITE,
636 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
637 return mem == MAP_FAILED ? NULL : mem;
640 /* DIAG 501 is used for sw breakpoints */
641 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
643 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
646 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
647 sizeof(diag_501), 0) ||
648 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
649 sizeof(diag_501), 1)) {
650 return -EINVAL;
652 return 0;
655 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
657 uint8_t t[sizeof(diag_501)];
659 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
660 return -EINVAL;
661 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
662 return -EINVAL;
663 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
664 sizeof(diag_501), 1)) {
665 return -EINVAL;
668 return 0;
671 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
672 int len, int type)
674 int n;
676 for (n = 0; n < nb_hw_breakpoints; n++) {
677 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
678 (hw_breakpoints[n].len == len || len == -1)) {
679 return &hw_breakpoints[n];
683 return NULL;
686 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
688 int size;
690 if (find_hw_breakpoint(addr, len, type)) {
691 return -EEXIST;
694 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
696 if (!hw_breakpoints) {
697 nb_hw_breakpoints = 0;
698 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
699 } else {
700 hw_breakpoints =
701 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
704 if (!hw_breakpoints) {
705 nb_hw_breakpoints = 0;
706 return -ENOMEM;
709 hw_breakpoints[nb_hw_breakpoints].addr = addr;
710 hw_breakpoints[nb_hw_breakpoints].len = len;
711 hw_breakpoints[nb_hw_breakpoints].type = type;
713 nb_hw_breakpoints++;
715 return 0;
718 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
719 target_ulong len, int type)
721 switch (type) {
722 case GDB_BREAKPOINT_HW:
723 type = KVM_HW_BP;
724 break;
725 case GDB_WATCHPOINT_WRITE:
726 if (len < 1) {
727 return -EINVAL;
729 type = KVM_HW_WP_WRITE;
730 break;
731 default:
732 return -ENOSYS;
734 return insert_hw_breakpoint(addr, len, type);
737 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
738 target_ulong len, int type)
740 int size;
741 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
743 if (bp == NULL) {
744 return -ENOENT;
747 nb_hw_breakpoints--;
748 if (nb_hw_breakpoints > 0) {
750 * In order to trim the array, move the last element to the position to
751 * be removed - if necessary.
753 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
754 *bp = hw_breakpoints[nb_hw_breakpoints];
756 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
757 hw_breakpoints =
758 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
759 } else {
760 g_free(hw_breakpoints);
761 hw_breakpoints = NULL;
764 return 0;
767 void kvm_arch_remove_all_hw_breakpoints(void)
769 nb_hw_breakpoints = 0;
770 g_free(hw_breakpoints);
771 hw_breakpoints = NULL;
774 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
776 int i;
778 if (nb_hw_breakpoints > 0) {
779 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
780 dbg->arch.hw_bp = hw_breakpoints;
782 for (i = 0; i < nb_hw_breakpoints; ++i) {
783 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
784 hw_breakpoints[i].addr);
786 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
787 } else {
788 dbg->arch.nr_hw_bp = 0;
789 dbg->arch.hw_bp = NULL;
793 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
797 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
799 return MEMTXATTRS_UNSPECIFIED;
802 int kvm_arch_process_async_events(CPUState *cs)
804 return cs->halted;
807 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
808 struct kvm_s390_interrupt *interrupt)
810 int r = 0;
812 interrupt->type = irq->type;
813 switch (irq->type) {
814 case KVM_S390_INT_VIRTIO:
815 interrupt->parm = irq->u.ext.ext_params;
816 /* fall through */
817 case KVM_S390_INT_PFAULT_INIT:
818 case KVM_S390_INT_PFAULT_DONE:
819 interrupt->parm64 = irq->u.ext.ext_params2;
820 break;
821 case KVM_S390_PROGRAM_INT:
822 interrupt->parm = irq->u.pgm.code;
823 break;
824 case KVM_S390_SIGP_SET_PREFIX:
825 interrupt->parm = irq->u.prefix.address;
826 break;
827 case KVM_S390_INT_SERVICE:
828 interrupt->parm = irq->u.ext.ext_params;
829 break;
830 case KVM_S390_MCHK:
831 interrupt->parm = irq->u.mchk.cr14;
832 interrupt->parm64 = irq->u.mchk.mcic;
833 break;
834 case KVM_S390_INT_EXTERNAL_CALL:
835 interrupt->parm = irq->u.extcall.code;
836 break;
837 case KVM_S390_INT_EMERGENCY:
838 interrupt->parm = irq->u.emerg.code;
839 break;
840 case KVM_S390_SIGP_STOP:
841 case KVM_S390_RESTART:
842 break; /* These types have no parameters */
843 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
844 interrupt->parm = irq->u.io.subchannel_id << 16;
845 interrupt->parm |= irq->u.io.subchannel_nr;
846 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
847 interrupt->parm64 |= irq->u.io.io_int_word;
848 break;
849 default:
850 r = -EINVAL;
851 break;
853 return r;
856 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
858 struct kvm_s390_interrupt kvmint = {};
859 int r;
861 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
862 if (r < 0) {
863 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
864 exit(1);
867 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
868 if (r < 0) {
869 fprintf(stderr, "KVM failed to inject interrupt\n");
870 exit(1);
874 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
876 CPUState *cs = CPU(cpu);
877 int r;
879 if (cap_s390_irq) {
880 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
881 if (!r) {
882 return;
884 error_report("KVM failed to inject interrupt %llx", irq->type);
885 exit(1);
888 inject_vcpu_irq_legacy(cs, irq);
891 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
893 struct kvm_s390_interrupt kvmint = {};
894 int r;
896 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
897 if (r < 0) {
898 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
899 exit(1);
902 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
903 if (r < 0) {
904 fprintf(stderr, "KVM failed to inject interrupt\n");
905 exit(1);
909 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
911 static bool use_flic = true;
912 int r;
914 if (use_flic) {
915 r = kvm_s390_inject_flic(irq);
916 if (r == -ENOSYS) {
917 use_flic = false;
919 if (!r) {
920 return;
923 __kvm_s390_floating_interrupt(irq);
926 void kvm_s390_virtio_irq(int config_change, uint64_t token)
928 struct kvm_s390_irq irq = {
929 .type = KVM_S390_INT_VIRTIO,
930 .u.ext.ext_params = config_change,
931 .u.ext.ext_params2 = token,
934 kvm_s390_floating_interrupt(&irq);
937 void kvm_s390_service_interrupt(uint32_t parm)
939 struct kvm_s390_irq irq = {
940 .type = KVM_S390_INT_SERVICE,
941 .u.ext.ext_params = parm,
944 kvm_s390_floating_interrupt(&irq);
947 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
949 struct kvm_s390_irq irq = {
950 .type = KVM_S390_PROGRAM_INT,
951 .u.pgm.code = code,
954 kvm_s390_vcpu_interrupt(cpu, &irq);
957 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
959 struct kvm_s390_irq irq = {
960 .type = KVM_S390_PROGRAM_INT,
961 .u.pgm.code = code,
962 .u.pgm.trans_exc_code = te_code,
963 .u.pgm.exc_access_id = te_code & 3,
966 kvm_s390_vcpu_interrupt(cpu, &irq);
969 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
970 uint16_t ipbh0)
972 CPUS390XState *env = &cpu->env;
973 uint64_t sccb;
974 uint32_t code;
975 int r = 0;
977 cpu_synchronize_state(CPU(cpu));
978 sccb = env->regs[ipbh0 & 0xf];
979 code = env->regs[(ipbh0 & 0xf0) >> 4];
981 r = sclp_service_call(env, sccb, code);
982 if (r < 0) {
983 enter_pgmcheck(cpu, -r);
984 } else {
985 setcc(cpu, r);
988 return 0;
991 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
993 CPUS390XState *env = &cpu->env;
994 int rc = 0;
995 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
997 cpu_synchronize_state(CPU(cpu));
999 switch (ipa1) {
1000 case PRIV_B2_XSCH:
1001 ioinst_handle_xsch(cpu, env->regs[1]);
1002 break;
1003 case PRIV_B2_CSCH:
1004 ioinst_handle_csch(cpu, env->regs[1]);
1005 break;
1006 case PRIV_B2_HSCH:
1007 ioinst_handle_hsch(cpu, env->regs[1]);
1008 break;
1009 case PRIV_B2_MSCH:
1010 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1011 break;
1012 case PRIV_B2_SSCH:
1013 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1014 break;
1015 case PRIV_B2_STCRW:
1016 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1017 break;
1018 case PRIV_B2_STSCH:
1019 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1020 break;
1021 case PRIV_B2_TSCH:
1022 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1023 fprintf(stderr, "Spurious tsch intercept\n");
1024 break;
1025 case PRIV_B2_CHSC:
1026 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1027 break;
1028 case PRIV_B2_TPI:
1029 /* This should have been handled by kvm already. */
1030 fprintf(stderr, "Spurious tpi intercept\n");
1031 break;
1032 case PRIV_B2_SCHM:
1033 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1034 run->s390_sieic.ipb);
1035 break;
1036 case PRIV_B2_RSCH:
1037 ioinst_handle_rsch(cpu, env->regs[1]);
1038 break;
1039 case PRIV_B2_RCHP:
1040 ioinst_handle_rchp(cpu, env->regs[1]);
1041 break;
1042 case PRIV_B2_STCPS:
1043 /* We do not provide this instruction, it is suppressed. */
1044 break;
1045 case PRIV_B2_SAL:
1046 ioinst_handle_sal(cpu, env->regs[1]);
1047 break;
1048 case PRIV_B2_SIGA:
1049 /* Not provided, set CC = 3 for subchannel not operational */
1050 setcc(cpu, 3);
1051 break;
1052 case PRIV_B2_SCLP_CALL:
1053 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1054 break;
1055 default:
1056 rc = -1;
1057 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1058 break;
1061 return rc;
1064 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1065 uint8_t *ar)
1067 CPUS390XState *env = &cpu->env;
1068 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1069 uint32_t base2 = run->s390_sieic.ipb >> 28;
1070 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1071 ((run->s390_sieic.ipb & 0xff00) << 4);
1073 if (disp2 & 0x80000) {
1074 disp2 += 0xfff00000;
1076 if (ar) {
1077 *ar = base2;
1080 return (base2 ? env->regs[base2] : 0) +
1081 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1084 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1085 uint8_t *ar)
1087 CPUS390XState *env = &cpu->env;
1088 uint32_t base2 = run->s390_sieic.ipb >> 28;
1089 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1090 ((run->s390_sieic.ipb & 0xff00) << 4);
1092 if (disp2 & 0x80000) {
1093 disp2 += 0xfff00000;
1095 if (ar) {
1096 *ar = base2;
1099 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1102 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1104 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1106 return clp_service_call(cpu, r2);
1109 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1111 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1112 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1114 return pcilg_service_call(cpu, r1, r2);
1117 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1119 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1120 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1122 return pcistg_service_call(cpu, r1, r2);
1125 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1127 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1128 uint64_t fiba;
1129 uint8_t ar;
1131 cpu_synchronize_state(CPU(cpu));
1132 fiba = get_base_disp_rxy(cpu, run, &ar);
1134 return stpcifc_service_call(cpu, r1, fiba, ar);
1137 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1139 /* NOOP */
1140 return 0;
1143 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1145 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1146 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1148 return rpcit_service_call(cpu, r1, r2);
1151 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1153 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1154 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1155 uint64_t gaddr;
1156 uint8_t ar;
1158 cpu_synchronize_state(CPU(cpu));
1159 gaddr = get_base_disp_rsy(cpu, run, &ar);
1161 return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1164 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1166 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1167 uint64_t fiba;
1168 uint8_t ar;
1170 cpu_synchronize_state(CPU(cpu));
1171 fiba = get_base_disp_rxy(cpu, run, &ar);
1173 return mpcifc_service_call(cpu, r1, fiba, ar);
1176 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1178 int r = 0;
1180 switch (ipa1) {
1181 case PRIV_B9_CLP:
1182 r = kvm_clp_service_call(cpu, run);
1183 break;
1184 case PRIV_B9_PCISTG:
1185 r = kvm_pcistg_service_call(cpu, run);
1186 break;
1187 case PRIV_B9_PCILG:
1188 r = kvm_pcilg_service_call(cpu, run);
1189 break;
1190 case PRIV_B9_RPCIT:
1191 r = kvm_rpcit_service_call(cpu, run);
1192 break;
1193 case PRIV_B9_EQBS:
1194 /* just inject exception */
1195 r = -1;
1196 break;
1197 default:
1198 r = -1;
1199 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1200 break;
1203 return r;
1206 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1208 int r = 0;
1210 switch (ipbl) {
1211 case PRIV_EB_PCISTB:
1212 r = kvm_pcistb_service_call(cpu, run);
1213 break;
1214 case PRIV_EB_SIC:
1215 r = kvm_sic_service_call(cpu, run);
1216 break;
1217 case PRIV_EB_SQBS:
1218 /* just inject exception */
1219 r = -1;
1220 break;
1221 default:
1222 r = -1;
1223 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1224 break;
1227 return r;
1230 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1232 int r = 0;
1234 switch (ipbl) {
1235 case PRIV_E3_MPCIFC:
1236 r = kvm_mpcifc_service_call(cpu, run);
1237 break;
1238 case PRIV_E3_STPCIFC:
1239 r = kvm_stpcifc_service_call(cpu, run);
1240 break;
1241 default:
1242 r = -1;
1243 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1244 break;
1247 return r;
1250 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1252 CPUS390XState *env = &cpu->env;
1253 int ret;
1255 cpu_synchronize_state(CPU(cpu));
1256 ret = s390_virtio_hypercall(env);
1257 if (ret == -EINVAL) {
1258 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1259 return 0;
1262 return ret;
1265 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1267 uint64_t r1, r3;
1268 int rc;
1270 cpu_synchronize_state(CPU(cpu));
1271 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1272 r3 = run->s390_sieic.ipa & 0x000f;
1273 rc = handle_diag_288(&cpu->env, r1, r3);
1274 if (rc) {
1275 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1279 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1281 uint64_t r1, r3;
1283 cpu_synchronize_state(CPU(cpu));
1284 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1285 r3 = run->s390_sieic.ipa & 0x000f;
1286 handle_diag_308(&cpu->env, r1, r3);
1289 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1291 CPUS390XState *env = &cpu->env;
1292 unsigned long pc;
1294 cpu_synchronize_state(CPU(cpu));
1296 pc = env->psw.addr - 4;
1297 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1298 env->psw.addr = pc;
1299 return EXCP_DEBUG;
1302 return -ENOENT;
1305 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1307 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1309 int r = 0;
1310 uint16_t func_code;
1313 * For any diagnose call we support, bits 48-63 of the resulting
1314 * address specify the function code; the remainder is ignored.
1316 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1317 switch (func_code) {
1318 case DIAG_TIMEREVENT:
1319 kvm_handle_diag_288(cpu, run);
1320 break;
1321 case DIAG_IPL:
1322 kvm_handle_diag_308(cpu, run);
1323 break;
1324 case DIAG_KVM_HYPERCALL:
1325 r = handle_hypercall(cpu, run);
1326 break;
1327 case DIAG_KVM_BREAKPOINT:
1328 r = handle_sw_breakpoint(cpu, run);
1329 break;
1330 default:
1331 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1332 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1333 break;
1336 return r;
1339 typedef struct SigpInfo {
1340 S390CPU *cpu;
1341 uint64_t param;
1342 int cc;
1343 uint64_t *status_reg;
1344 } SigpInfo;
1346 static void set_sigp_status(SigpInfo *si, uint64_t status)
1348 *si->status_reg &= 0xffffffff00000000ULL;
1349 *si->status_reg |= status;
1350 si->cc = SIGP_CC_STATUS_STORED;
1353 static void sigp_start(void *arg)
1355 SigpInfo *si = arg;
1357 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1358 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1359 return;
1362 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1363 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1366 static void sigp_stop(void *arg)
1368 SigpInfo *si = arg;
1369 struct kvm_s390_irq irq = {
1370 .type = KVM_S390_SIGP_STOP,
1373 if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1374 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1375 return;
1378 /* disabled wait - sleeping in user space */
1379 if (CPU(si->cpu)->halted) {
1380 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1381 } else {
1382 /* execute the stop function */
1383 si->cpu->env.sigp_order = SIGP_STOP;
1384 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1386 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1389 #define ADTL_SAVE_AREA_SIZE 1024
1390 static int kvm_s390_store_adtl_status(S390CPU *cpu, hwaddr addr)
1392 void *mem;
1393 hwaddr len = ADTL_SAVE_AREA_SIZE;
1395 mem = cpu_physical_memory_map(addr, &len, 1);
1396 if (!mem) {
1397 return -EFAULT;
1399 if (len != ADTL_SAVE_AREA_SIZE) {
1400 cpu_physical_memory_unmap(mem, len, 1, 0);
1401 return -EFAULT;
1404 memcpy(mem, &cpu->env.vregs, 512);
1406 cpu_physical_memory_unmap(mem, len, 1, len);
1408 return 0;
1411 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1412 #define SAVE_AREA_SIZE 512
1413 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1415 static const uint8_t ar_id = 1;
1416 uint64_t ckc = cpu->env.ckc >> 8;
1417 void *mem;
1418 int i;
1419 hwaddr len = SAVE_AREA_SIZE;
1421 mem = cpu_physical_memory_map(addr, &len, 1);
1422 if (!mem) {
1423 return -EFAULT;
1425 if (len != SAVE_AREA_SIZE) {
1426 cpu_physical_memory_unmap(mem, len, 1, 0);
1427 return -EFAULT;
1430 if (store_arch) {
1431 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1433 for (i = 0; i < 16; ++i) {
1434 *((uint64 *)mem + i) = get_freg(&cpu->env, i)->ll;
1436 memcpy(mem + 128, &cpu->env.regs, 128);
1437 memcpy(mem + 256, &cpu->env.psw, 16);
1438 memcpy(mem + 280, &cpu->env.psa, 4);
1439 memcpy(mem + 284, &cpu->env.fpc, 4);
1440 memcpy(mem + 292, &cpu->env.todpr, 4);
1441 memcpy(mem + 296, &cpu->env.cputm, 8);
1442 memcpy(mem + 304, &ckc, 8);
1443 memcpy(mem + 320, &cpu->env.aregs, 64);
1444 memcpy(mem + 384, &cpu->env.cregs, 128);
1446 cpu_physical_memory_unmap(mem, len, 1, len);
1448 return 0;
1451 static void sigp_stop_and_store_status(void *arg)
1453 SigpInfo *si = arg;
1454 struct kvm_s390_irq irq = {
1455 .type = KVM_S390_SIGP_STOP,
1458 /* disabled wait - sleeping in user space */
1459 if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1460 CPU(si->cpu)->halted) {
1461 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1464 switch (s390_cpu_get_state(si->cpu)) {
1465 case CPU_STATE_OPERATING:
1466 si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1467 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1468 /* store will be performed when handling the stop intercept */
1469 break;
1470 case CPU_STATE_STOPPED:
1471 /* already stopped, just store the status */
1472 cpu_synchronize_state(CPU(si->cpu));
1473 kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1474 break;
1476 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1479 static void sigp_store_status_at_address(void *arg)
1481 SigpInfo *si = arg;
1482 uint32_t address = si->param & 0x7ffffe00u;
1484 /* cpu has to be stopped */
1485 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1486 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1487 return;
1490 cpu_synchronize_state(CPU(si->cpu));
1492 if (kvm_s390_store_status(si->cpu, address, false)) {
1493 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1494 return;
1496 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1499 static void sigp_store_adtl_status(void *arg)
1501 SigpInfo *si = arg;
1503 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
1504 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1505 return;
1508 /* cpu has to be stopped */
1509 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1510 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1511 return;
1514 /* parameter must be aligned to 1024-byte boundary */
1515 if (si->param & 0x3ff) {
1516 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1517 return;
1520 cpu_synchronize_state(CPU(si->cpu));
1522 if (kvm_s390_store_adtl_status(si->cpu, si->param)) {
1523 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1524 return;
1526 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1529 static void sigp_restart(void *arg)
1531 SigpInfo *si = arg;
1532 struct kvm_s390_irq irq = {
1533 .type = KVM_S390_RESTART,
1536 switch (s390_cpu_get_state(si->cpu)) {
1537 case CPU_STATE_STOPPED:
1538 /* the restart irq has to be delivered prior to any other pending irq */
1539 cpu_synchronize_state(CPU(si->cpu));
1540 do_restart_interrupt(&si->cpu->env);
1541 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1542 break;
1543 case CPU_STATE_OPERATING:
1544 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1545 break;
1547 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1550 int kvm_s390_cpu_restart(S390CPU *cpu)
1552 SigpInfo si = {
1553 .cpu = cpu,
1556 run_on_cpu(CPU(cpu), sigp_restart, &si);
1557 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1558 return 0;
1561 static void sigp_initial_cpu_reset(void *arg)
1563 SigpInfo *si = arg;
1564 CPUState *cs = CPU(si->cpu);
1565 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1567 cpu_synchronize_state(cs);
1568 scc->initial_cpu_reset(cs);
1569 cpu_synchronize_post_reset(cs);
1570 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1573 static void sigp_cpu_reset(void *arg)
1575 SigpInfo *si = arg;
1576 CPUState *cs = CPU(si->cpu);
1577 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1579 cpu_synchronize_state(cs);
1580 scc->cpu_reset(cs);
1581 cpu_synchronize_post_reset(cs);
1582 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1585 static void sigp_set_prefix(void *arg)
1587 SigpInfo *si = arg;
1588 uint32_t addr = si->param & 0x7fffe000u;
1590 cpu_synchronize_state(CPU(si->cpu));
1592 if (!address_space_access_valid(&address_space_memory, addr,
1593 sizeof(struct LowCore), false)) {
1594 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1595 return;
1598 /* cpu has to be stopped */
1599 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1600 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1601 return;
1604 si->cpu->env.psa = addr;
1605 cpu_synchronize_post_init(CPU(si->cpu));
1606 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1609 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1610 uint64_t param, uint64_t *status_reg)
1612 SigpInfo si = {
1613 .cpu = dst_cpu,
1614 .param = param,
1615 .status_reg = status_reg,
1618 /* cpu available? */
1619 if (dst_cpu == NULL) {
1620 return SIGP_CC_NOT_OPERATIONAL;
1623 /* only resets can break pending orders */
1624 if (dst_cpu->env.sigp_order != 0 &&
1625 order != SIGP_CPU_RESET &&
1626 order != SIGP_INITIAL_CPU_RESET) {
1627 return SIGP_CC_BUSY;
1630 switch (order) {
1631 case SIGP_START:
1632 run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1633 break;
1634 case SIGP_STOP:
1635 run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1636 break;
1637 case SIGP_RESTART:
1638 run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1639 break;
1640 case SIGP_STOP_STORE_STATUS:
1641 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1642 break;
1643 case SIGP_STORE_STATUS_ADDR:
1644 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1645 break;
1646 case SIGP_STORE_ADTL_STATUS:
1647 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, &si);
1648 break;
1649 case SIGP_SET_PREFIX:
1650 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1651 break;
1652 case SIGP_INITIAL_CPU_RESET:
1653 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1654 break;
1655 case SIGP_CPU_RESET:
1656 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1657 break;
1658 default:
1659 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1660 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1663 return si.cc;
1666 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1667 uint64_t *status_reg)
1669 CPUState *cur_cs;
1670 S390CPU *cur_cpu;
1672 /* due to the BQL, we are the only active cpu */
1673 CPU_FOREACH(cur_cs) {
1674 cur_cpu = S390_CPU(cur_cs);
1675 if (cur_cpu->env.sigp_order != 0) {
1676 return SIGP_CC_BUSY;
1678 cpu_synchronize_state(cur_cs);
1679 /* all but the current one have to be stopped */
1680 if (cur_cpu != cpu &&
1681 s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1682 *status_reg &= 0xffffffff00000000ULL;
1683 *status_reg |= SIGP_STAT_INCORRECT_STATE;
1684 return SIGP_CC_STATUS_STORED;
1688 switch (param & 0xff) {
1689 case SIGP_MODE_ESA_S390:
1690 /* not supported */
1691 return SIGP_CC_NOT_OPERATIONAL;
1692 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1693 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1694 CPU_FOREACH(cur_cs) {
1695 cur_cpu = S390_CPU(cur_cs);
1696 cur_cpu->env.pfault_token = -1UL;
1698 break;
1699 default:
1700 *status_reg &= 0xffffffff00000000ULL;
1701 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1702 return SIGP_CC_STATUS_STORED;
1705 return SIGP_CC_ORDER_CODE_ACCEPTED;
1708 #define SIGP_ORDER_MASK 0x000000ff
1710 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1712 CPUS390XState *env = &cpu->env;
1713 const uint8_t r1 = ipa1 >> 4;
1714 const uint8_t r3 = ipa1 & 0x0f;
1715 int ret;
1716 uint8_t order;
1717 uint64_t *status_reg;
1718 uint64_t param;
1719 S390CPU *dst_cpu = NULL;
1721 cpu_synchronize_state(CPU(cpu));
1723 /* get order code */
1724 order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1725 & SIGP_ORDER_MASK;
1726 status_reg = &env->regs[r1];
1727 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1729 switch (order) {
1730 case SIGP_SET_ARCH:
1731 ret = sigp_set_architecture(cpu, param, status_reg);
1732 break;
1733 default:
1734 /* all other sigp orders target a single vcpu */
1735 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1736 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1739 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1740 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1742 if (ret >= 0) {
1743 setcc(cpu, ret);
1744 return 0;
1747 return ret;
1750 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1752 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1753 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1754 int r = -1;
1756 DPRINTF("handle_instruction 0x%x 0x%x\n",
1757 run->s390_sieic.ipa, run->s390_sieic.ipb);
1758 switch (ipa0) {
1759 case IPA0_B2:
1760 r = handle_b2(cpu, run, ipa1);
1761 break;
1762 case IPA0_B9:
1763 r = handle_b9(cpu, run, ipa1);
1764 break;
1765 case IPA0_EB:
1766 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1767 break;
1768 case IPA0_E3:
1769 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1770 break;
1771 case IPA0_DIAG:
1772 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1773 break;
1774 case IPA0_SIGP:
1775 r = handle_sigp(cpu, run, ipa1);
1776 break;
1779 if (r < 0) {
1780 r = 0;
1781 enter_pgmcheck(cpu, 0x0001);
1784 return r;
1787 static bool is_special_wait_psw(CPUState *cs)
1789 /* signal quiesce */
1790 return cs->kvm_run->psw_addr == 0xfffUL;
1793 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1795 CPUState *cs = CPU(cpu);
1797 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1798 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1799 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1800 s390_cpu_halt(cpu);
1801 qemu_system_guest_panicked();
1804 static int handle_intercept(S390CPU *cpu)
1806 CPUState *cs = CPU(cpu);
1807 struct kvm_run *run = cs->kvm_run;
1808 int icpt_code = run->s390_sieic.icptcode;
1809 int r = 0;
1811 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1812 (long)cs->kvm_run->psw_addr);
1813 switch (icpt_code) {
1814 case ICPT_INSTRUCTION:
1815 r = handle_instruction(cpu, run);
1816 break;
1817 case ICPT_PROGRAM:
1818 unmanageable_intercept(cpu, "program interrupt",
1819 offsetof(LowCore, program_new_psw));
1820 r = EXCP_HALTED;
1821 break;
1822 case ICPT_EXT_INT:
1823 unmanageable_intercept(cpu, "external interrupt",
1824 offsetof(LowCore, external_new_psw));
1825 r = EXCP_HALTED;
1826 break;
1827 case ICPT_WAITPSW:
1828 /* disabled wait, since enabled wait is handled in kernel */
1829 cpu_synchronize_state(cs);
1830 if (s390_cpu_halt(cpu) == 0) {
1831 if (is_special_wait_psw(cs)) {
1832 qemu_system_shutdown_request();
1833 } else {
1834 qemu_system_guest_panicked();
1837 r = EXCP_HALTED;
1838 break;
1839 case ICPT_CPU_STOP:
1840 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1841 qemu_system_shutdown_request();
1843 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1844 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1845 true);
1847 cpu->env.sigp_order = 0;
1848 r = EXCP_HALTED;
1849 break;
1850 case ICPT_SOFT_INTERCEPT:
1851 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1852 exit(1);
1853 break;
1854 case ICPT_IO:
1855 fprintf(stderr, "KVM unimplemented icpt IO\n");
1856 exit(1);
1857 break;
1858 default:
1859 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1860 exit(1);
1861 break;
1864 return r;
1867 static int handle_tsch(S390CPU *cpu)
1869 CPUState *cs = CPU(cpu);
1870 struct kvm_run *run = cs->kvm_run;
1871 int ret;
1873 cpu_synchronize_state(cs);
1875 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1876 if (ret < 0) {
1878 * Failure.
1879 * If an I/O interrupt had been dequeued, we have to reinject it.
1881 if (run->s390_tsch.dequeued) {
1882 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1883 run->s390_tsch.subchannel_nr,
1884 run->s390_tsch.io_int_parm,
1885 run->s390_tsch.io_int_word);
1887 ret = 0;
1889 return ret;
1892 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1894 struct sysib_322 sysib;
1895 int del;
1897 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1898 return;
1900 /* Shift the stack of Extended Names to prepare for our own data */
1901 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1902 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1903 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1904 * assumed it's not capable of managing Extended Names for lower levels.
1906 for (del = 1; del < sysib.count; del++) {
1907 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1908 break;
1911 if (del < sysib.count) {
1912 memset(sysib.ext_names[del], 0,
1913 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1915 /* Insert short machine name in EBCDIC, padded with blanks */
1916 if (qemu_name) {
1917 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1918 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1919 strlen(qemu_name)));
1921 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1922 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1923 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1924 * considered by s390 as not capable of providing any Extended Name.
1925 * Therefore if no name was specified on qemu invocation, we go with the
1926 * same "KVMguest" default, which KVM has filled into short name field.
1928 if (qemu_name) {
1929 strncpy((char *)sysib.ext_names[0], qemu_name,
1930 sizeof(sysib.ext_names[0]));
1931 } else {
1932 strcpy((char *)sysib.ext_names[0], "KVMguest");
1934 /* Insert UUID */
1935 memcpy(sysib.vm[0].uuid, qemu_uuid, sizeof(sysib.vm[0].uuid));
1937 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1940 static int handle_stsi(S390CPU *cpu)
1942 CPUState *cs = CPU(cpu);
1943 struct kvm_run *run = cs->kvm_run;
1945 switch (run->s390_stsi.fc) {
1946 case 3:
1947 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1948 return 0;
1950 /* Only sysib 3.2.2 needs post-handling for now. */
1951 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1952 return 0;
1953 default:
1954 return 0;
1958 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1960 CPUState *cs = CPU(cpu);
1961 struct kvm_run *run = cs->kvm_run;
1963 int ret = 0;
1964 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1966 switch (arch_info->type) {
1967 case KVM_HW_WP_WRITE:
1968 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1969 cs->watchpoint_hit = &hw_watchpoint;
1970 hw_watchpoint.vaddr = arch_info->addr;
1971 hw_watchpoint.flags = BP_MEM_WRITE;
1972 ret = EXCP_DEBUG;
1974 break;
1975 case KVM_HW_BP:
1976 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1977 ret = EXCP_DEBUG;
1979 break;
1980 case KVM_SINGLESTEP:
1981 if (cs->singlestep_enabled) {
1982 ret = EXCP_DEBUG;
1984 break;
1985 default:
1986 ret = -ENOSYS;
1989 return ret;
1992 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1994 S390CPU *cpu = S390_CPU(cs);
1995 int ret = 0;
1997 qemu_mutex_lock_iothread();
1999 switch (run->exit_reason) {
2000 case KVM_EXIT_S390_SIEIC:
2001 ret = handle_intercept(cpu);
2002 break;
2003 case KVM_EXIT_S390_RESET:
2004 s390_reipl_request();
2005 break;
2006 case KVM_EXIT_S390_TSCH:
2007 ret = handle_tsch(cpu);
2008 break;
2009 case KVM_EXIT_S390_STSI:
2010 ret = handle_stsi(cpu);
2011 break;
2012 case KVM_EXIT_DEBUG:
2013 ret = kvm_arch_handle_debug_exit(cpu);
2014 break;
2015 default:
2016 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2017 break;
2019 qemu_mutex_unlock_iothread();
2021 if (ret == 0) {
2022 ret = EXCP_INTERRUPT;
2024 return ret;
2027 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2029 return true;
2032 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
2034 return 1;
2037 int kvm_arch_on_sigbus(int code, void *addr)
2039 return 1;
2042 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2043 uint16_t subchannel_nr, uint32_t io_int_parm,
2044 uint32_t io_int_word)
2046 struct kvm_s390_irq irq = {
2047 .u.io.subchannel_id = subchannel_id,
2048 .u.io.subchannel_nr = subchannel_nr,
2049 .u.io.io_int_parm = io_int_parm,
2050 .u.io.io_int_word = io_int_word,
2053 if (io_int_word & IO_INT_WORD_AI) {
2054 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2055 } else {
2056 irq.type = ((subchannel_id & 0xff00) << 24) |
2057 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
2059 kvm_s390_floating_interrupt(&irq);
2062 static uint64_t build_channel_report_mcic(void)
2064 uint64_t mcic;
2066 /* subclass: indicate channel report pending */
2067 mcic = MCIC_SC_CP |
2068 /* subclass modifiers: none */
2069 /* storage errors: none */
2070 /* validity bits: no damage */
2071 MCIC_VB_WP | MCIC_VB_MS | MCIC_VB_PM | MCIC_VB_IA | MCIC_VB_FP |
2072 MCIC_VB_GR | MCIC_VB_CR | MCIC_VB_ST | MCIC_VB_AR | MCIC_VB_PR |
2073 MCIC_VB_FC | MCIC_VB_CT | MCIC_VB_CC;
2074 if (kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
2075 mcic |= MCIC_VB_VR;
2077 return mcic;
2080 void kvm_s390_crw_mchk(void)
2082 struct kvm_s390_irq irq = {
2083 .type = KVM_S390_MCHK,
2084 .u.mchk.cr14 = 1 << 28,
2085 .u.mchk.mcic = build_channel_report_mcic(),
2087 kvm_s390_floating_interrupt(&irq);
2090 void kvm_s390_enable_css_support(S390CPU *cpu)
2092 int r;
2094 /* Activate host kernel channel subsystem support. */
2095 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2096 assert(r == 0);
2099 void kvm_arch_init_irq_routing(KVMState *s)
2102 * Note that while irqchip capabilities generally imply that cpustates
2103 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2104 * have to override the common code kvm_halt_in_kernel_allowed setting.
2106 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2107 kvm_gsi_routing_allowed = true;
2108 kvm_halt_in_kernel_allowed = false;
2112 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2113 int vq, bool assign)
2115 struct kvm_ioeventfd kick = {
2116 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2117 KVM_IOEVENTFD_FLAG_DATAMATCH,
2118 .fd = event_notifier_get_fd(notifier),
2119 .datamatch = vq,
2120 .addr = sch,
2121 .len = 8,
2123 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2124 return -ENOSYS;
2126 if (!assign) {
2127 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2129 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2132 int kvm_s390_get_memslot_count(KVMState *s)
2134 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
2137 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2139 struct kvm_mp_state mp_state = {};
2140 int ret;
2142 /* the kvm part might not have been initialized yet */
2143 if (CPU(cpu)->kvm_state == NULL) {
2144 return 0;
2147 switch (cpu_state) {
2148 case CPU_STATE_STOPPED:
2149 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2150 break;
2151 case CPU_STATE_CHECK_STOP:
2152 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2153 break;
2154 case CPU_STATE_OPERATING:
2155 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2156 break;
2157 case CPU_STATE_LOAD:
2158 mp_state.mp_state = KVM_MP_STATE_LOAD;
2159 break;
2160 default:
2161 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2162 cpu_state);
2163 exit(1);
2166 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2167 if (ret) {
2168 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2169 strerror(-ret));
2172 return ret;
2175 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2177 struct kvm_s390_irq_state irq_state;
2178 CPUState *cs = CPU(cpu);
2179 int32_t bytes;
2181 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2182 return;
2185 irq_state.buf = (uint64_t) cpu->irqstate;
2186 irq_state.len = VCPU_IRQ_BUF_SIZE;
2188 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2189 if (bytes < 0) {
2190 cpu->irqstate_saved_size = 0;
2191 error_report("Migration of interrupt state failed");
2192 return;
2195 cpu->irqstate_saved_size = bytes;
2198 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2200 CPUState *cs = CPU(cpu);
2201 struct kvm_s390_irq_state irq_state;
2202 int r;
2204 if (cpu->irqstate_saved_size == 0) {
2205 return 0;
2208 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2209 return -ENOSYS;
2212 irq_state.buf = (uint64_t) cpu->irqstate;
2213 irq_state.len = cpu->irqstate_saved_size;
2215 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2216 if (r) {
2217 error_report("Setting interrupt state failed %d", r);
2219 return r;
2222 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2223 uint64_t address, uint32_t data, PCIDevice *dev)
2225 S390PCIBusDevice *pbdev;
2226 uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
2227 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2229 pbdev = s390_pci_find_dev_by_fid(fid);
2230 if (!pbdev) {
2231 DPRINTF("add_msi_route no dev\n");
2232 return -ENODEV;
2235 pbdev->routes.adapter.ind_offset = vec;
2237 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2238 route->flags = 0;
2239 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2240 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2241 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2242 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2243 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2244 return 0;
2247 int kvm_arch_msi_data_to_gsi(uint32_t data)
2249 abort();