target-arm: Implement missing AMAIR registers
[qemu/ar7.git] / target-s390x / kvm.c
blobae3a0affec9c4a98cd944dcf4f1e9e08d02124e9
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_clear_cmma_callback(void *opaque)
178 int rc;
179 KVMState *s = opaque;
180 struct kvm_device_attr attr = {
181 .group = KVM_S390_VM_MEM_CTRL,
182 .attr = KVM_S390_VM_MEM_CLR_CMMA,
185 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
186 trace_kvm_clear_cmma(rc);
189 static void kvm_s390_enable_cmma(KVMState *s)
191 int rc;
192 struct kvm_device_attr attr = {
193 .group = KVM_S390_VM_MEM_CTRL,
194 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
197 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_ENABLE_CMMA) ||
198 !kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_CLR_CMMA)) {
199 return;
202 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
203 if (!rc) {
204 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
206 trace_kvm_enable_cmma(rc);
209 static void kvm_s390_set_attr(uint64_t attr)
211 struct kvm_device_attr attribute = {
212 .group = KVM_S390_VM_CRYPTO,
213 .attr = attr,
216 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
218 if (ret) {
219 error_report("Failed to set crypto device attribute %lu: %s",
220 attr, strerror(-ret));
224 static void kvm_s390_init_aes_kw(void)
226 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
228 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
229 NULL)) {
230 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
233 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
234 kvm_s390_set_attr(attr);
238 static void kvm_s390_init_dea_kw(void)
240 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
242 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
243 NULL)) {
244 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
247 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
248 kvm_s390_set_attr(attr);
252 static void kvm_s390_init_crypto(void)
254 kvm_s390_init_aes_kw();
255 kvm_s390_init_dea_kw();
258 int kvm_arch_init(MachineState *ms, KVMState *s)
260 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
261 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
262 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
263 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
265 kvm_s390_enable_cmma(s);
267 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
268 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
269 phys_mem_set_alloc(legacy_s390_alloc);
272 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
273 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
274 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
276 return 0;
279 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
281 return cpu->cpu_index;
284 int kvm_arch_init_vcpu(CPUState *cs)
286 S390CPU *cpu = S390_CPU(cs);
287 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
288 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
289 return 0;
292 void kvm_s390_reset_vcpu(S390CPU *cpu)
294 CPUState *cs = CPU(cpu);
296 /* The initial reset call is needed here to reset in-kernel
297 * vcpu data that we can't access directly from QEMU
298 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
299 * Before this ioctl cpu_synchronize_state() is called in common kvm
300 * code (kvm-all) */
301 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
302 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
305 kvm_s390_init_crypto();
308 static int can_sync_regs(CPUState *cs, int regs)
310 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
313 int kvm_arch_put_registers(CPUState *cs, int level)
315 S390CPU *cpu = S390_CPU(cs);
316 CPUS390XState *env = &cpu->env;
317 struct kvm_sregs sregs;
318 struct kvm_regs regs;
319 struct kvm_fpu fpu = {};
320 int r;
321 int i;
323 /* always save the PSW and the GPRS*/
324 cs->kvm_run->psw_addr = env->psw.addr;
325 cs->kvm_run->psw_mask = env->psw.mask;
327 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
328 for (i = 0; i < 16; i++) {
329 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
330 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
332 } else {
333 for (i = 0; i < 16; i++) {
334 regs.gprs[i] = env->regs[i];
336 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
337 if (r < 0) {
338 return r;
342 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
343 for (i = 0; i < 32; i++) {
344 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
345 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
347 cs->kvm_run->s.regs.fpc = env->fpc;
348 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
349 } else {
350 /* Floating point */
351 for (i = 0; i < 16; i++) {
352 fpu.fprs[i] = get_freg(env, i)->ll;
354 fpu.fpc = env->fpc;
356 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
357 if (r < 0) {
358 return r;
362 /* Do we need to save more than that? */
363 if (level == KVM_PUT_RUNTIME_STATE) {
364 return 0;
367 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
368 cs->kvm_run->s.regs.cputm = env->cputm;
369 cs->kvm_run->s.regs.ckc = env->ckc;
370 cs->kvm_run->s.regs.todpr = env->todpr;
371 cs->kvm_run->s.regs.gbea = env->gbea;
372 cs->kvm_run->s.regs.pp = env->pp;
373 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
374 } else {
376 * These ONE_REGS are not protected by a capability. As they are only
377 * necessary for migration we just trace a possible error, but don't
378 * return with an error return code.
380 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
381 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
382 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
383 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
384 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
387 /* pfault parameters */
388 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
389 cs->kvm_run->s.regs.pft = env->pfault_token;
390 cs->kvm_run->s.regs.pfs = env->pfault_select;
391 cs->kvm_run->s.regs.pfc = env->pfault_compare;
392 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
393 } else if (cap_async_pf) {
394 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
395 if (r < 0) {
396 return r;
398 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
399 if (r < 0) {
400 return r;
402 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
403 if (r < 0) {
404 return r;
408 /* access registers and control registers*/
409 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
410 for (i = 0; i < 16; i++) {
411 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
412 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
414 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
415 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
416 } else {
417 for (i = 0; i < 16; i++) {
418 sregs.acrs[i] = env->aregs[i];
419 sregs.crs[i] = env->cregs[i];
421 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
422 if (r < 0) {
423 return r;
427 /* Finally the prefix */
428 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
429 cs->kvm_run->s.regs.prefix = env->psa;
430 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
431 } else {
432 /* prefix is only supported via sync regs */
434 return 0;
437 int kvm_arch_get_registers(CPUState *cs)
439 S390CPU *cpu = S390_CPU(cs);
440 CPUS390XState *env = &cpu->env;
441 struct kvm_sregs sregs;
442 struct kvm_regs regs;
443 struct kvm_fpu fpu;
444 int i, r;
446 /* get the PSW */
447 env->psw.addr = cs->kvm_run->psw_addr;
448 env->psw.mask = cs->kvm_run->psw_mask;
450 /* the GPRS */
451 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
452 for (i = 0; i < 16; i++) {
453 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
455 } else {
456 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
457 if (r < 0) {
458 return r;
460 for (i = 0; i < 16; i++) {
461 env->regs[i] = regs.gprs[i];
465 /* The ACRS and CRS */
466 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
467 for (i = 0; i < 16; i++) {
468 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
469 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
471 } else {
472 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
473 if (r < 0) {
474 return r;
476 for (i = 0; i < 16; i++) {
477 env->aregs[i] = sregs.acrs[i];
478 env->cregs[i] = sregs.crs[i];
482 /* Floating point and vector registers */
483 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
484 for (i = 0; i < 32; i++) {
485 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
486 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
488 env->fpc = cs->kvm_run->s.regs.fpc;
489 } else {
490 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
491 if (r < 0) {
492 return r;
494 for (i = 0; i < 16; i++) {
495 get_freg(env, i)->ll = fpu.fprs[i];
497 env->fpc = fpu.fpc;
500 /* The prefix */
501 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
502 env->psa = cs->kvm_run->s.regs.prefix;
505 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
506 env->cputm = cs->kvm_run->s.regs.cputm;
507 env->ckc = cs->kvm_run->s.regs.ckc;
508 env->todpr = cs->kvm_run->s.regs.todpr;
509 env->gbea = cs->kvm_run->s.regs.gbea;
510 env->pp = cs->kvm_run->s.regs.pp;
511 } else {
513 * These ONE_REGS are not protected by a capability. As they are only
514 * necessary for migration we just trace a possible error, but don't
515 * return with an error return code.
517 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
518 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
519 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
520 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
521 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
524 /* pfault parameters */
525 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
526 env->pfault_token = cs->kvm_run->s.regs.pft;
527 env->pfault_select = cs->kvm_run->s.regs.pfs;
528 env->pfault_compare = cs->kvm_run->s.regs.pfc;
529 } else if (cap_async_pf) {
530 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
531 if (r < 0) {
532 return r;
534 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
535 if (r < 0) {
536 return r;
538 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
539 if (r < 0) {
540 return r;
544 return 0;
547 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
549 int r;
550 struct kvm_device_attr attr = {
551 .group = KVM_S390_VM_TOD,
552 .attr = KVM_S390_VM_TOD_LOW,
553 .addr = (uint64_t)tod_low,
556 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
557 if (r) {
558 return r;
561 attr.attr = KVM_S390_VM_TOD_HIGH;
562 attr.addr = (uint64_t)tod_high;
563 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
566 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
568 int r;
570 struct kvm_device_attr attr = {
571 .group = KVM_S390_VM_TOD,
572 .attr = KVM_S390_VM_TOD_LOW,
573 .addr = (uint64_t)tod_low,
576 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
577 if (r) {
578 return r;
581 attr.attr = KVM_S390_VM_TOD_HIGH;
582 attr.addr = (uint64_t)tod_high;
583 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
587 * kvm_s390_mem_op:
588 * @addr: the logical start address in guest memory
589 * @ar: the access register number
590 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
591 * @len: length that should be transfered
592 * @is_write: true = write, false = read
593 * Returns: 0 on success, non-zero if an exception or error occured
595 * Use KVM ioctl to read/write from/to guest memory. An access exception
596 * is injected into the vCPU in case of translation errors.
598 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
599 int len, bool is_write)
601 struct kvm_s390_mem_op mem_op = {
602 .gaddr = addr,
603 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
604 .size = len,
605 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
606 : KVM_S390_MEMOP_LOGICAL_READ,
607 .buf = (uint64_t)hostbuf,
608 .ar = ar,
610 int ret;
612 if (!cap_mem_op) {
613 return -ENOSYS;
615 if (!hostbuf) {
616 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
619 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
620 if (ret < 0) {
621 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
623 return ret;
627 * Legacy layout for s390:
628 * Older S390 KVM requires the topmost vma of the RAM to be
629 * smaller than an system defined value, which is at least 256GB.
630 * Larger systems have larger values. We put the guest between
631 * the end of data segment (system break) and this value. We
632 * use 32GB as a base to have enough room for the system break
633 * to grow. We also have to use MAP parameters that avoid
634 * read-only mapping of guest pages.
636 static void *legacy_s390_alloc(size_t size, uint64_t *align)
638 void *mem;
640 mem = mmap((void *) 0x800000000ULL, size,
641 PROT_EXEC|PROT_READ|PROT_WRITE,
642 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
643 return mem == MAP_FAILED ? NULL : mem;
646 /* DIAG 501 is used for sw breakpoints */
647 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
649 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
652 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
653 sizeof(diag_501), 0) ||
654 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
655 sizeof(diag_501), 1)) {
656 return -EINVAL;
658 return 0;
661 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
663 uint8_t t[sizeof(diag_501)];
665 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
666 return -EINVAL;
667 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
668 return -EINVAL;
669 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
670 sizeof(diag_501), 1)) {
671 return -EINVAL;
674 return 0;
677 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
678 int len, int type)
680 int n;
682 for (n = 0; n < nb_hw_breakpoints; n++) {
683 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
684 (hw_breakpoints[n].len == len || len == -1)) {
685 return &hw_breakpoints[n];
689 return NULL;
692 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
694 int size;
696 if (find_hw_breakpoint(addr, len, type)) {
697 return -EEXIST;
700 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
702 if (!hw_breakpoints) {
703 nb_hw_breakpoints = 0;
704 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
705 } else {
706 hw_breakpoints =
707 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
710 if (!hw_breakpoints) {
711 nb_hw_breakpoints = 0;
712 return -ENOMEM;
715 hw_breakpoints[nb_hw_breakpoints].addr = addr;
716 hw_breakpoints[nb_hw_breakpoints].len = len;
717 hw_breakpoints[nb_hw_breakpoints].type = type;
719 nb_hw_breakpoints++;
721 return 0;
724 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
725 target_ulong len, int type)
727 switch (type) {
728 case GDB_BREAKPOINT_HW:
729 type = KVM_HW_BP;
730 break;
731 case GDB_WATCHPOINT_WRITE:
732 if (len < 1) {
733 return -EINVAL;
735 type = KVM_HW_WP_WRITE;
736 break;
737 default:
738 return -ENOSYS;
740 return insert_hw_breakpoint(addr, len, type);
743 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
744 target_ulong len, int type)
746 int size;
747 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
749 if (bp == NULL) {
750 return -ENOENT;
753 nb_hw_breakpoints--;
754 if (nb_hw_breakpoints > 0) {
756 * In order to trim the array, move the last element to the position to
757 * be removed - if necessary.
759 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
760 *bp = hw_breakpoints[nb_hw_breakpoints];
762 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
763 hw_breakpoints =
764 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
765 } else {
766 g_free(hw_breakpoints);
767 hw_breakpoints = NULL;
770 return 0;
773 void kvm_arch_remove_all_hw_breakpoints(void)
775 nb_hw_breakpoints = 0;
776 g_free(hw_breakpoints);
777 hw_breakpoints = NULL;
780 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
782 int i;
784 if (nb_hw_breakpoints > 0) {
785 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
786 dbg->arch.hw_bp = hw_breakpoints;
788 for (i = 0; i < nb_hw_breakpoints; ++i) {
789 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
790 hw_breakpoints[i].addr);
792 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
793 } else {
794 dbg->arch.nr_hw_bp = 0;
795 dbg->arch.hw_bp = NULL;
799 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
803 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
805 return MEMTXATTRS_UNSPECIFIED;
808 int kvm_arch_process_async_events(CPUState *cs)
810 return cs->halted;
813 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
814 struct kvm_s390_interrupt *interrupt)
816 int r = 0;
818 interrupt->type = irq->type;
819 switch (irq->type) {
820 case KVM_S390_INT_VIRTIO:
821 interrupt->parm = irq->u.ext.ext_params;
822 /* fall through */
823 case KVM_S390_INT_PFAULT_INIT:
824 case KVM_S390_INT_PFAULT_DONE:
825 interrupt->parm64 = irq->u.ext.ext_params2;
826 break;
827 case KVM_S390_PROGRAM_INT:
828 interrupt->parm = irq->u.pgm.code;
829 break;
830 case KVM_S390_SIGP_SET_PREFIX:
831 interrupt->parm = irq->u.prefix.address;
832 break;
833 case KVM_S390_INT_SERVICE:
834 interrupt->parm = irq->u.ext.ext_params;
835 break;
836 case KVM_S390_MCHK:
837 interrupt->parm = irq->u.mchk.cr14;
838 interrupt->parm64 = irq->u.mchk.mcic;
839 break;
840 case KVM_S390_INT_EXTERNAL_CALL:
841 interrupt->parm = irq->u.extcall.code;
842 break;
843 case KVM_S390_INT_EMERGENCY:
844 interrupt->parm = irq->u.emerg.code;
845 break;
846 case KVM_S390_SIGP_STOP:
847 case KVM_S390_RESTART:
848 break; /* These types have no parameters */
849 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
850 interrupt->parm = irq->u.io.subchannel_id << 16;
851 interrupt->parm |= irq->u.io.subchannel_nr;
852 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
853 interrupt->parm64 |= irq->u.io.io_int_word;
854 break;
855 default:
856 r = -EINVAL;
857 break;
859 return r;
862 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
864 struct kvm_s390_interrupt kvmint = {};
865 int r;
867 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
868 if (r < 0) {
869 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
870 exit(1);
873 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
874 if (r < 0) {
875 fprintf(stderr, "KVM failed to inject interrupt\n");
876 exit(1);
880 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
882 CPUState *cs = CPU(cpu);
883 int r;
885 if (cap_s390_irq) {
886 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
887 if (!r) {
888 return;
890 error_report("KVM failed to inject interrupt %llx", irq->type);
891 exit(1);
894 inject_vcpu_irq_legacy(cs, irq);
897 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
899 struct kvm_s390_interrupt kvmint = {};
900 int r;
902 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
903 if (r < 0) {
904 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
905 exit(1);
908 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
909 if (r < 0) {
910 fprintf(stderr, "KVM failed to inject interrupt\n");
911 exit(1);
915 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
917 static bool use_flic = true;
918 int r;
920 if (use_flic) {
921 r = kvm_s390_inject_flic(irq);
922 if (r == -ENOSYS) {
923 use_flic = false;
925 if (!r) {
926 return;
929 __kvm_s390_floating_interrupt(irq);
932 void kvm_s390_virtio_irq(int config_change, uint64_t token)
934 struct kvm_s390_irq irq = {
935 .type = KVM_S390_INT_VIRTIO,
936 .u.ext.ext_params = config_change,
937 .u.ext.ext_params2 = token,
940 kvm_s390_floating_interrupt(&irq);
943 void kvm_s390_service_interrupt(uint32_t parm)
945 struct kvm_s390_irq irq = {
946 .type = KVM_S390_INT_SERVICE,
947 .u.ext.ext_params = parm,
950 kvm_s390_floating_interrupt(&irq);
953 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
955 struct kvm_s390_irq irq = {
956 .type = KVM_S390_PROGRAM_INT,
957 .u.pgm.code = code,
960 kvm_s390_vcpu_interrupt(cpu, &irq);
963 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
965 struct kvm_s390_irq irq = {
966 .type = KVM_S390_PROGRAM_INT,
967 .u.pgm.code = code,
968 .u.pgm.trans_exc_code = te_code,
969 .u.pgm.exc_access_id = te_code & 3,
972 kvm_s390_vcpu_interrupt(cpu, &irq);
975 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
976 uint16_t ipbh0)
978 CPUS390XState *env = &cpu->env;
979 uint64_t sccb;
980 uint32_t code;
981 int r = 0;
983 cpu_synchronize_state(CPU(cpu));
984 sccb = env->regs[ipbh0 & 0xf];
985 code = env->regs[(ipbh0 & 0xf0) >> 4];
987 r = sclp_service_call(env, sccb, code);
988 if (r < 0) {
989 enter_pgmcheck(cpu, -r);
990 } else {
991 setcc(cpu, r);
994 return 0;
997 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
999 CPUS390XState *env = &cpu->env;
1000 int rc = 0;
1001 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1003 cpu_synchronize_state(CPU(cpu));
1005 switch (ipa1) {
1006 case PRIV_B2_XSCH:
1007 ioinst_handle_xsch(cpu, env->regs[1]);
1008 break;
1009 case PRIV_B2_CSCH:
1010 ioinst_handle_csch(cpu, env->regs[1]);
1011 break;
1012 case PRIV_B2_HSCH:
1013 ioinst_handle_hsch(cpu, env->regs[1]);
1014 break;
1015 case PRIV_B2_MSCH:
1016 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1017 break;
1018 case PRIV_B2_SSCH:
1019 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1020 break;
1021 case PRIV_B2_STCRW:
1022 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1023 break;
1024 case PRIV_B2_STSCH:
1025 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1026 break;
1027 case PRIV_B2_TSCH:
1028 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1029 fprintf(stderr, "Spurious tsch intercept\n");
1030 break;
1031 case PRIV_B2_CHSC:
1032 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1033 break;
1034 case PRIV_B2_TPI:
1035 /* This should have been handled by kvm already. */
1036 fprintf(stderr, "Spurious tpi intercept\n");
1037 break;
1038 case PRIV_B2_SCHM:
1039 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1040 run->s390_sieic.ipb);
1041 break;
1042 case PRIV_B2_RSCH:
1043 ioinst_handle_rsch(cpu, env->regs[1]);
1044 break;
1045 case PRIV_B2_RCHP:
1046 ioinst_handle_rchp(cpu, env->regs[1]);
1047 break;
1048 case PRIV_B2_STCPS:
1049 /* We do not provide this instruction, it is suppressed. */
1050 break;
1051 case PRIV_B2_SAL:
1052 ioinst_handle_sal(cpu, env->regs[1]);
1053 break;
1054 case PRIV_B2_SIGA:
1055 /* Not provided, set CC = 3 for subchannel not operational */
1056 setcc(cpu, 3);
1057 break;
1058 case PRIV_B2_SCLP_CALL:
1059 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1060 break;
1061 default:
1062 rc = -1;
1063 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1064 break;
1067 return rc;
1070 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1071 uint8_t *ar)
1073 CPUS390XState *env = &cpu->env;
1074 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1075 uint32_t base2 = run->s390_sieic.ipb >> 28;
1076 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1077 ((run->s390_sieic.ipb & 0xff00) << 4);
1079 if (disp2 & 0x80000) {
1080 disp2 += 0xfff00000;
1082 if (ar) {
1083 *ar = base2;
1086 return (base2 ? env->regs[base2] : 0) +
1087 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1090 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1091 uint8_t *ar)
1093 CPUS390XState *env = &cpu->env;
1094 uint32_t base2 = run->s390_sieic.ipb >> 28;
1095 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1096 ((run->s390_sieic.ipb & 0xff00) << 4);
1098 if (disp2 & 0x80000) {
1099 disp2 += 0xfff00000;
1101 if (ar) {
1102 *ar = base2;
1105 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1108 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1110 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1112 return clp_service_call(cpu, r2);
1115 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1117 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1118 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1120 return pcilg_service_call(cpu, r1, r2);
1123 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1125 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1126 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1128 return pcistg_service_call(cpu, r1, r2);
1131 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1133 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1134 uint64_t fiba;
1135 uint8_t ar;
1137 cpu_synchronize_state(CPU(cpu));
1138 fiba = get_base_disp_rxy(cpu, run, &ar);
1140 return stpcifc_service_call(cpu, r1, fiba, ar);
1143 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1145 /* NOOP */
1146 return 0;
1149 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1151 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1152 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1154 return rpcit_service_call(cpu, r1, r2);
1157 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1159 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1160 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1161 uint64_t gaddr;
1162 uint8_t ar;
1164 cpu_synchronize_state(CPU(cpu));
1165 gaddr = get_base_disp_rsy(cpu, run, &ar);
1167 return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1170 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1172 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1173 uint64_t fiba;
1174 uint8_t ar;
1176 cpu_synchronize_state(CPU(cpu));
1177 fiba = get_base_disp_rxy(cpu, run, &ar);
1179 return mpcifc_service_call(cpu, r1, fiba, ar);
1182 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1184 int r = 0;
1186 switch (ipa1) {
1187 case PRIV_B9_CLP:
1188 r = kvm_clp_service_call(cpu, run);
1189 break;
1190 case PRIV_B9_PCISTG:
1191 r = kvm_pcistg_service_call(cpu, run);
1192 break;
1193 case PRIV_B9_PCILG:
1194 r = kvm_pcilg_service_call(cpu, run);
1195 break;
1196 case PRIV_B9_RPCIT:
1197 r = kvm_rpcit_service_call(cpu, run);
1198 break;
1199 case PRIV_B9_EQBS:
1200 /* just inject exception */
1201 r = -1;
1202 break;
1203 default:
1204 r = -1;
1205 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1206 break;
1209 return r;
1212 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1214 int r = 0;
1216 switch (ipbl) {
1217 case PRIV_EB_PCISTB:
1218 r = kvm_pcistb_service_call(cpu, run);
1219 break;
1220 case PRIV_EB_SIC:
1221 r = kvm_sic_service_call(cpu, run);
1222 break;
1223 case PRIV_EB_SQBS:
1224 /* just inject exception */
1225 r = -1;
1226 break;
1227 default:
1228 r = -1;
1229 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1230 break;
1233 return r;
1236 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1238 int r = 0;
1240 switch (ipbl) {
1241 case PRIV_E3_MPCIFC:
1242 r = kvm_mpcifc_service_call(cpu, run);
1243 break;
1244 case PRIV_E3_STPCIFC:
1245 r = kvm_stpcifc_service_call(cpu, run);
1246 break;
1247 default:
1248 r = -1;
1249 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1250 break;
1253 return r;
1256 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1258 CPUS390XState *env = &cpu->env;
1259 int ret;
1261 cpu_synchronize_state(CPU(cpu));
1262 ret = s390_virtio_hypercall(env);
1263 if (ret == -EINVAL) {
1264 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1265 return 0;
1268 return ret;
1271 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1273 uint64_t r1, r3;
1274 int rc;
1276 cpu_synchronize_state(CPU(cpu));
1277 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1278 r3 = run->s390_sieic.ipa & 0x000f;
1279 rc = handle_diag_288(&cpu->env, r1, r3);
1280 if (rc) {
1281 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1285 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1287 uint64_t r1, r3;
1289 cpu_synchronize_state(CPU(cpu));
1290 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1291 r3 = run->s390_sieic.ipa & 0x000f;
1292 handle_diag_308(&cpu->env, r1, r3);
1295 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1297 CPUS390XState *env = &cpu->env;
1298 unsigned long pc;
1300 cpu_synchronize_state(CPU(cpu));
1302 pc = env->psw.addr - 4;
1303 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1304 env->psw.addr = pc;
1305 return EXCP_DEBUG;
1308 return -ENOENT;
1311 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1313 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1315 int r = 0;
1316 uint16_t func_code;
1319 * For any diagnose call we support, bits 48-63 of the resulting
1320 * address specify the function code; the remainder is ignored.
1322 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1323 switch (func_code) {
1324 case DIAG_TIMEREVENT:
1325 kvm_handle_diag_288(cpu, run);
1326 break;
1327 case DIAG_IPL:
1328 kvm_handle_diag_308(cpu, run);
1329 break;
1330 case DIAG_KVM_HYPERCALL:
1331 r = handle_hypercall(cpu, run);
1332 break;
1333 case DIAG_KVM_BREAKPOINT:
1334 r = handle_sw_breakpoint(cpu, run);
1335 break;
1336 default:
1337 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1338 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1339 break;
1342 return r;
1345 typedef struct SigpInfo {
1346 S390CPU *cpu;
1347 uint64_t param;
1348 int cc;
1349 uint64_t *status_reg;
1350 } SigpInfo;
1352 static void set_sigp_status(SigpInfo *si, uint64_t status)
1354 *si->status_reg &= 0xffffffff00000000ULL;
1355 *si->status_reg |= status;
1356 si->cc = SIGP_CC_STATUS_STORED;
1359 static void sigp_start(void *arg)
1361 SigpInfo *si = arg;
1363 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1364 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1365 return;
1368 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1369 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1372 static void sigp_stop(void *arg)
1374 SigpInfo *si = arg;
1375 struct kvm_s390_irq irq = {
1376 .type = KVM_S390_SIGP_STOP,
1379 if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1380 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1381 return;
1384 /* disabled wait - sleeping in user space */
1385 if (CPU(si->cpu)->halted) {
1386 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1387 } else {
1388 /* execute the stop function */
1389 si->cpu->env.sigp_order = SIGP_STOP;
1390 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1392 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1395 #define ADTL_SAVE_AREA_SIZE 1024
1396 static int kvm_s390_store_adtl_status(S390CPU *cpu, hwaddr addr)
1398 void *mem;
1399 hwaddr len = ADTL_SAVE_AREA_SIZE;
1401 mem = cpu_physical_memory_map(addr, &len, 1);
1402 if (!mem) {
1403 return -EFAULT;
1405 if (len != ADTL_SAVE_AREA_SIZE) {
1406 cpu_physical_memory_unmap(mem, len, 1, 0);
1407 return -EFAULT;
1410 memcpy(mem, &cpu->env.vregs, 512);
1412 cpu_physical_memory_unmap(mem, len, 1, len);
1414 return 0;
1417 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1418 #define SAVE_AREA_SIZE 512
1419 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1421 static const uint8_t ar_id = 1;
1422 uint64_t ckc = cpu->env.ckc >> 8;
1423 void *mem;
1424 int i;
1425 hwaddr len = SAVE_AREA_SIZE;
1427 mem = cpu_physical_memory_map(addr, &len, 1);
1428 if (!mem) {
1429 return -EFAULT;
1431 if (len != SAVE_AREA_SIZE) {
1432 cpu_physical_memory_unmap(mem, len, 1, 0);
1433 return -EFAULT;
1436 if (store_arch) {
1437 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1439 for (i = 0; i < 16; ++i) {
1440 *((uint64 *)mem + i) = get_freg(&cpu->env, i)->ll;
1442 memcpy(mem + 128, &cpu->env.regs, 128);
1443 memcpy(mem + 256, &cpu->env.psw, 16);
1444 memcpy(mem + 280, &cpu->env.psa, 4);
1445 memcpy(mem + 284, &cpu->env.fpc, 4);
1446 memcpy(mem + 292, &cpu->env.todpr, 4);
1447 memcpy(mem + 296, &cpu->env.cputm, 8);
1448 memcpy(mem + 304, &ckc, 8);
1449 memcpy(mem + 320, &cpu->env.aregs, 64);
1450 memcpy(mem + 384, &cpu->env.cregs, 128);
1452 cpu_physical_memory_unmap(mem, len, 1, len);
1454 return 0;
1457 static void sigp_stop_and_store_status(void *arg)
1459 SigpInfo *si = arg;
1460 struct kvm_s390_irq irq = {
1461 .type = KVM_S390_SIGP_STOP,
1464 /* disabled wait - sleeping in user space */
1465 if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1466 CPU(si->cpu)->halted) {
1467 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1470 switch (s390_cpu_get_state(si->cpu)) {
1471 case CPU_STATE_OPERATING:
1472 si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1473 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1474 /* store will be performed when handling the stop intercept */
1475 break;
1476 case CPU_STATE_STOPPED:
1477 /* already stopped, just store the status */
1478 cpu_synchronize_state(CPU(si->cpu));
1479 kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1480 break;
1482 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1485 static void sigp_store_status_at_address(void *arg)
1487 SigpInfo *si = arg;
1488 uint32_t address = si->param & 0x7ffffe00u;
1490 /* cpu has to be stopped */
1491 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1492 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1493 return;
1496 cpu_synchronize_state(CPU(si->cpu));
1498 if (kvm_s390_store_status(si->cpu, address, false)) {
1499 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1500 return;
1502 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1505 static void sigp_store_adtl_status(void *arg)
1507 SigpInfo *si = arg;
1509 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
1510 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1511 return;
1514 /* cpu has to be stopped */
1515 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1516 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1517 return;
1520 /* parameter must be aligned to 1024-byte boundary */
1521 if (si->param & 0x3ff) {
1522 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1523 return;
1526 cpu_synchronize_state(CPU(si->cpu));
1528 if (kvm_s390_store_adtl_status(si->cpu, si->param)) {
1529 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1530 return;
1532 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1535 static void sigp_restart(void *arg)
1537 SigpInfo *si = arg;
1538 struct kvm_s390_irq irq = {
1539 .type = KVM_S390_RESTART,
1542 switch (s390_cpu_get_state(si->cpu)) {
1543 case CPU_STATE_STOPPED:
1544 /* the restart irq has to be delivered prior to any other pending irq */
1545 cpu_synchronize_state(CPU(si->cpu));
1546 do_restart_interrupt(&si->cpu->env);
1547 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1548 break;
1549 case CPU_STATE_OPERATING:
1550 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1551 break;
1553 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1556 int kvm_s390_cpu_restart(S390CPU *cpu)
1558 SigpInfo si = {
1559 .cpu = cpu,
1562 run_on_cpu(CPU(cpu), sigp_restart, &si);
1563 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1564 return 0;
1567 static void sigp_initial_cpu_reset(void *arg)
1569 SigpInfo *si = arg;
1570 CPUState *cs = CPU(si->cpu);
1571 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1573 cpu_synchronize_state(cs);
1574 scc->initial_cpu_reset(cs);
1575 cpu_synchronize_post_reset(cs);
1576 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1579 static void sigp_cpu_reset(void *arg)
1581 SigpInfo *si = arg;
1582 CPUState *cs = CPU(si->cpu);
1583 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1585 cpu_synchronize_state(cs);
1586 scc->cpu_reset(cs);
1587 cpu_synchronize_post_reset(cs);
1588 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1591 static void sigp_set_prefix(void *arg)
1593 SigpInfo *si = arg;
1594 uint32_t addr = si->param & 0x7fffe000u;
1596 cpu_synchronize_state(CPU(si->cpu));
1598 if (!address_space_access_valid(&address_space_memory, addr,
1599 sizeof(struct LowCore), false)) {
1600 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1601 return;
1604 /* cpu has to be stopped */
1605 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1606 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1607 return;
1610 si->cpu->env.psa = addr;
1611 cpu_synchronize_post_init(CPU(si->cpu));
1612 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1615 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1616 uint64_t param, uint64_t *status_reg)
1618 SigpInfo si = {
1619 .cpu = dst_cpu,
1620 .param = param,
1621 .status_reg = status_reg,
1624 /* cpu available? */
1625 if (dst_cpu == NULL) {
1626 return SIGP_CC_NOT_OPERATIONAL;
1629 /* only resets can break pending orders */
1630 if (dst_cpu->env.sigp_order != 0 &&
1631 order != SIGP_CPU_RESET &&
1632 order != SIGP_INITIAL_CPU_RESET) {
1633 return SIGP_CC_BUSY;
1636 switch (order) {
1637 case SIGP_START:
1638 run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1639 break;
1640 case SIGP_STOP:
1641 run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1642 break;
1643 case SIGP_RESTART:
1644 run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1645 break;
1646 case SIGP_STOP_STORE_STATUS:
1647 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1648 break;
1649 case SIGP_STORE_STATUS_ADDR:
1650 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1651 break;
1652 case SIGP_STORE_ADTL_STATUS:
1653 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, &si);
1654 break;
1655 case SIGP_SET_PREFIX:
1656 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1657 break;
1658 case SIGP_INITIAL_CPU_RESET:
1659 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1660 break;
1661 case SIGP_CPU_RESET:
1662 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1663 break;
1664 default:
1665 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1666 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1669 return si.cc;
1672 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1673 uint64_t *status_reg)
1675 CPUState *cur_cs;
1676 S390CPU *cur_cpu;
1678 /* due to the BQL, we are the only active cpu */
1679 CPU_FOREACH(cur_cs) {
1680 cur_cpu = S390_CPU(cur_cs);
1681 if (cur_cpu->env.sigp_order != 0) {
1682 return SIGP_CC_BUSY;
1684 cpu_synchronize_state(cur_cs);
1685 /* all but the current one have to be stopped */
1686 if (cur_cpu != cpu &&
1687 s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1688 *status_reg &= 0xffffffff00000000ULL;
1689 *status_reg |= SIGP_STAT_INCORRECT_STATE;
1690 return SIGP_CC_STATUS_STORED;
1694 switch (param & 0xff) {
1695 case SIGP_MODE_ESA_S390:
1696 /* not supported */
1697 return SIGP_CC_NOT_OPERATIONAL;
1698 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1699 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1700 CPU_FOREACH(cur_cs) {
1701 cur_cpu = S390_CPU(cur_cs);
1702 cur_cpu->env.pfault_token = -1UL;
1704 break;
1705 default:
1706 *status_reg &= 0xffffffff00000000ULL;
1707 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1708 return SIGP_CC_STATUS_STORED;
1711 return SIGP_CC_ORDER_CODE_ACCEPTED;
1714 #define SIGP_ORDER_MASK 0x000000ff
1716 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1718 CPUS390XState *env = &cpu->env;
1719 const uint8_t r1 = ipa1 >> 4;
1720 const uint8_t r3 = ipa1 & 0x0f;
1721 int ret;
1722 uint8_t order;
1723 uint64_t *status_reg;
1724 uint64_t param;
1725 S390CPU *dst_cpu = NULL;
1727 cpu_synchronize_state(CPU(cpu));
1729 /* get order code */
1730 order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1731 & SIGP_ORDER_MASK;
1732 status_reg = &env->regs[r1];
1733 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1735 switch (order) {
1736 case SIGP_SET_ARCH:
1737 ret = sigp_set_architecture(cpu, param, status_reg);
1738 break;
1739 default:
1740 /* all other sigp orders target a single vcpu */
1741 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1742 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1745 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1746 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1748 if (ret >= 0) {
1749 setcc(cpu, ret);
1750 return 0;
1753 return ret;
1756 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1758 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1759 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1760 int r = -1;
1762 DPRINTF("handle_instruction 0x%x 0x%x\n",
1763 run->s390_sieic.ipa, run->s390_sieic.ipb);
1764 switch (ipa0) {
1765 case IPA0_B2:
1766 r = handle_b2(cpu, run, ipa1);
1767 break;
1768 case IPA0_B9:
1769 r = handle_b9(cpu, run, ipa1);
1770 break;
1771 case IPA0_EB:
1772 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1773 break;
1774 case IPA0_E3:
1775 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1776 break;
1777 case IPA0_DIAG:
1778 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1779 break;
1780 case IPA0_SIGP:
1781 r = handle_sigp(cpu, run, ipa1);
1782 break;
1785 if (r < 0) {
1786 r = 0;
1787 enter_pgmcheck(cpu, 0x0001);
1790 return r;
1793 static bool is_special_wait_psw(CPUState *cs)
1795 /* signal quiesce */
1796 return cs->kvm_run->psw_addr == 0xfffUL;
1799 static void guest_panicked(void)
1801 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1802 &error_abort);
1803 vm_stop(RUN_STATE_GUEST_PANICKED);
1806 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1808 CPUState *cs = CPU(cpu);
1810 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1811 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1812 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1813 s390_cpu_halt(cpu);
1814 guest_panicked();
1817 static int handle_intercept(S390CPU *cpu)
1819 CPUState *cs = CPU(cpu);
1820 struct kvm_run *run = cs->kvm_run;
1821 int icpt_code = run->s390_sieic.icptcode;
1822 int r = 0;
1824 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1825 (long)cs->kvm_run->psw_addr);
1826 switch (icpt_code) {
1827 case ICPT_INSTRUCTION:
1828 r = handle_instruction(cpu, run);
1829 break;
1830 case ICPT_PROGRAM:
1831 unmanageable_intercept(cpu, "program interrupt",
1832 offsetof(LowCore, program_new_psw));
1833 r = EXCP_HALTED;
1834 break;
1835 case ICPT_EXT_INT:
1836 unmanageable_intercept(cpu, "external interrupt",
1837 offsetof(LowCore, external_new_psw));
1838 r = EXCP_HALTED;
1839 break;
1840 case ICPT_WAITPSW:
1841 /* disabled wait, since enabled wait is handled in kernel */
1842 cpu_synchronize_state(cs);
1843 if (s390_cpu_halt(cpu) == 0) {
1844 if (is_special_wait_psw(cs)) {
1845 qemu_system_shutdown_request();
1846 } else {
1847 guest_panicked();
1850 r = EXCP_HALTED;
1851 break;
1852 case ICPT_CPU_STOP:
1853 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1854 qemu_system_shutdown_request();
1856 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1857 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1858 true);
1860 cpu->env.sigp_order = 0;
1861 r = EXCP_HALTED;
1862 break;
1863 case ICPT_SOFT_INTERCEPT:
1864 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1865 exit(1);
1866 break;
1867 case ICPT_IO:
1868 fprintf(stderr, "KVM unimplemented icpt IO\n");
1869 exit(1);
1870 break;
1871 default:
1872 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1873 exit(1);
1874 break;
1877 return r;
1880 static int handle_tsch(S390CPU *cpu)
1882 CPUState *cs = CPU(cpu);
1883 struct kvm_run *run = cs->kvm_run;
1884 int ret;
1886 cpu_synchronize_state(cs);
1888 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1889 if (ret < 0) {
1891 * Failure.
1892 * If an I/O interrupt had been dequeued, we have to reinject it.
1894 if (run->s390_tsch.dequeued) {
1895 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1896 run->s390_tsch.subchannel_nr,
1897 run->s390_tsch.io_int_parm,
1898 run->s390_tsch.io_int_word);
1900 ret = 0;
1902 return ret;
1905 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1907 struct sysib_322 sysib;
1908 int del;
1910 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1911 return;
1913 /* Shift the stack of Extended Names to prepare for our own data */
1914 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1915 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1916 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1917 * assumed it's not capable of managing Extended Names for lower levels.
1919 for (del = 1; del < sysib.count; del++) {
1920 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1921 break;
1924 if (del < sysib.count) {
1925 memset(sysib.ext_names[del], 0,
1926 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1928 /* Insert short machine name in EBCDIC, padded with blanks */
1929 if (qemu_name) {
1930 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1931 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1932 strlen(qemu_name)));
1934 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1935 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1936 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1937 * considered by s390 as not capable of providing any Extended Name.
1938 * Therefore if no name was specified on qemu invocation, we go with the
1939 * same "KVMguest" default, which KVM has filled into short name field.
1941 if (qemu_name) {
1942 strncpy((char *)sysib.ext_names[0], qemu_name,
1943 sizeof(sysib.ext_names[0]));
1944 } else {
1945 strcpy((char *)sysib.ext_names[0], "KVMguest");
1947 /* Insert UUID */
1948 memcpy(sysib.vm[0].uuid, qemu_uuid, sizeof(sysib.vm[0].uuid));
1950 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1953 static int handle_stsi(S390CPU *cpu)
1955 CPUState *cs = CPU(cpu);
1956 struct kvm_run *run = cs->kvm_run;
1958 switch (run->s390_stsi.fc) {
1959 case 3:
1960 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1961 return 0;
1963 /* Only sysib 3.2.2 needs post-handling for now. */
1964 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1965 return 0;
1966 default:
1967 return 0;
1971 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1973 CPUState *cs = CPU(cpu);
1974 struct kvm_run *run = cs->kvm_run;
1976 int ret = 0;
1977 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1979 switch (arch_info->type) {
1980 case KVM_HW_WP_WRITE:
1981 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1982 cs->watchpoint_hit = &hw_watchpoint;
1983 hw_watchpoint.vaddr = arch_info->addr;
1984 hw_watchpoint.flags = BP_MEM_WRITE;
1985 ret = EXCP_DEBUG;
1987 break;
1988 case KVM_HW_BP:
1989 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1990 ret = EXCP_DEBUG;
1992 break;
1993 case KVM_SINGLESTEP:
1994 if (cs->singlestep_enabled) {
1995 ret = EXCP_DEBUG;
1997 break;
1998 default:
1999 ret = -ENOSYS;
2002 return ret;
2005 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
2007 S390CPU *cpu = S390_CPU(cs);
2008 int ret = 0;
2010 qemu_mutex_lock_iothread();
2012 switch (run->exit_reason) {
2013 case KVM_EXIT_S390_SIEIC:
2014 ret = handle_intercept(cpu);
2015 break;
2016 case KVM_EXIT_S390_RESET:
2017 s390_reipl_request();
2018 break;
2019 case KVM_EXIT_S390_TSCH:
2020 ret = handle_tsch(cpu);
2021 break;
2022 case KVM_EXIT_S390_STSI:
2023 ret = handle_stsi(cpu);
2024 break;
2025 case KVM_EXIT_DEBUG:
2026 ret = kvm_arch_handle_debug_exit(cpu);
2027 break;
2028 default:
2029 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2030 break;
2032 qemu_mutex_unlock_iothread();
2034 if (ret == 0) {
2035 ret = EXCP_INTERRUPT;
2037 return ret;
2040 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2042 return true;
2045 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
2047 return 1;
2050 int kvm_arch_on_sigbus(int code, void *addr)
2052 return 1;
2055 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2056 uint16_t subchannel_nr, uint32_t io_int_parm,
2057 uint32_t io_int_word)
2059 struct kvm_s390_irq irq = {
2060 .u.io.subchannel_id = subchannel_id,
2061 .u.io.subchannel_nr = subchannel_nr,
2062 .u.io.io_int_parm = io_int_parm,
2063 .u.io.io_int_word = io_int_word,
2066 if (io_int_word & IO_INT_WORD_AI) {
2067 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2068 } else {
2069 irq.type = ((subchannel_id & 0xff00) << 24) |
2070 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
2072 kvm_s390_floating_interrupt(&irq);
2075 void kvm_s390_crw_mchk(void)
2077 struct kvm_s390_irq irq = {
2078 .type = KVM_S390_MCHK,
2079 .u.mchk.cr14 = 1 << 28,
2080 .u.mchk.mcic = 0x00400f1d40330000ULL,
2082 kvm_s390_floating_interrupt(&irq);
2085 void kvm_s390_enable_css_support(S390CPU *cpu)
2087 int r;
2089 /* Activate host kernel channel subsystem support. */
2090 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2091 assert(r == 0);
2094 void kvm_arch_init_irq_routing(KVMState *s)
2097 * Note that while irqchip capabilities generally imply that cpustates
2098 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2099 * have to override the common code kvm_halt_in_kernel_allowed setting.
2101 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2102 kvm_gsi_routing_allowed = true;
2103 kvm_halt_in_kernel_allowed = false;
2107 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2108 int vq, bool assign)
2110 struct kvm_ioeventfd kick = {
2111 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2112 KVM_IOEVENTFD_FLAG_DATAMATCH,
2113 .fd = event_notifier_get_fd(notifier),
2114 .datamatch = vq,
2115 .addr = sch,
2116 .len = 8,
2118 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2119 return -ENOSYS;
2121 if (!assign) {
2122 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2124 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2127 int kvm_s390_get_memslot_count(KVMState *s)
2129 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
2132 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2134 struct kvm_mp_state mp_state = {};
2135 int ret;
2137 /* the kvm part might not have been initialized yet */
2138 if (CPU(cpu)->kvm_state == NULL) {
2139 return 0;
2142 switch (cpu_state) {
2143 case CPU_STATE_STOPPED:
2144 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2145 break;
2146 case CPU_STATE_CHECK_STOP:
2147 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2148 break;
2149 case CPU_STATE_OPERATING:
2150 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2151 break;
2152 case CPU_STATE_LOAD:
2153 mp_state.mp_state = KVM_MP_STATE_LOAD;
2154 break;
2155 default:
2156 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2157 cpu_state);
2158 exit(1);
2161 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2162 if (ret) {
2163 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2164 strerror(-ret));
2167 return ret;
2170 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2172 struct kvm_s390_irq_state irq_state;
2173 CPUState *cs = CPU(cpu);
2174 int32_t bytes;
2176 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2177 return;
2180 irq_state.buf = (uint64_t) cpu->irqstate;
2181 irq_state.len = VCPU_IRQ_BUF_SIZE;
2183 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2184 if (bytes < 0) {
2185 cpu->irqstate_saved_size = 0;
2186 error_report("Migration of interrupt state failed");
2187 return;
2190 cpu->irqstate_saved_size = bytes;
2193 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2195 CPUState *cs = CPU(cpu);
2196 struct kvm_s390_irq_state irq_state;
2197 int r;
2199 if (cpu->irqstate_saved_size == 0) {
2200 return 0;
2203 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2204 return -ENOSYS;
2207 irq_state.buf = (uint64_t) cpu->irqstate;
2208 irq_state.len = cpu->irqstate_saved_size;
2210 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2211 if (r) {
2212 error_report("Setting interrupt state failed %d", r);
2214 return r;
2217 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2218 uint64_t address, uint32_t data)
2220 S390PCIBusDevice *pbdev;
2221 uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
2222 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2224 pbdev = s390_pci_find_dev_by_fid(fid);
2225 if (!pbdev) {
2226 DPRINTF("add_msi_route no dev\n");
2227 return -ENODEV;
2230 pbdev->routes.adapter.ind_offset = vec;
2232 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2233 route->flags = 0;
2234 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2235 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2236 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2237 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2238 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2239 return 0;
2242 int kvm_arch_msi_data_to_gsi(uint32_t data)
2244 abort();