s390x: Migrate vector registers
[qemu/qmp-unstable.git] / target-s390x / kvm.c
blob61e1321a4bfdce6479d64d689c4c369501443911
1 /*
2 * QEMU S390x KVM implementation
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp. 2012
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * Contributions after 2012-10-29 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
20 * You should have received a copy of the GNU (Lesser) General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/mman.h>
28 #include <linux/kvm.h>
29 #include <asm/ptrace.h>
31 #include "qemu-common.h"
32 #include "qemu/timer.h"
33 #include "sysemu/sysemu.h"
34 #include "sysemu/kvm.h"
35 #include "hw/hw.h"
36 #include "cpu.h"
37 #include "sysemu/device_tree.h"
38 #include "qapi/qmp/qjson.h"
39 #include "monitor/monitor.h"
40 #include "exec/gdbstub.h"
41 #include "exec/address-spaces.h"
42 #include "trace.h"
43 #include "qapi-event.h"
44 #include "hw/s390x/s390-pci-inst.h"
45 #include "hw/s390x/s390-pci-bus.h"
46 #include "hw/s390x/ipl.h"
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_IPL 0x308
102 #define DIAG_KVM_HYPERCALL 0x500
103 #define DIAG_KVM_BREAKPOINT 0x501
105 #define ICPT_INSTRUCTION 0x04
106 #define ICPT_PROGRAM 0x08
107 #define ICPT_EXT_INT 0x14
108 #define ICPT_WAITPSW 0x1c
109 #define ICPT_SOFT_INTERCEPT 0x24
110 #define ICPT_CPU_STOP 0x28
111 #define ICPT_IO 0x40
113 #define NR_LOCAL_IRQS 32
115 * Needs to be big enough to contain max_cpus emergency signals
116 * and in addition NR_LOCAL_IRQS interrupts
118 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
119 (max_cpus + NR_LOCAL_IRQS))
121 static CPUWatchpoint hw_watchpoint;
123 * We don't use a list because this structure is also used to transmit the
124 * hardware breakpoints to the kernel.
126 static struct kvm_hw_breakpoint *hw_breakpoints;
127 static int nb_hw_breakpoints;
129 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
130 KVM_CAP_LAST_INFO
133 static int cap_sync_regs;
134 static int cap_async_pf;
135 static int cap_mem_op;
136 static int cap_s390_irq;
138 static void *legacy_s390_alloc(size_t size, uint64_t *align);
140 static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
142 struct kvm_device_attr attr = {
143 .group = KVM_S390_VM_MEM_CTRL,
144 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
145 .addr = (uint64_t) memory_limit,
148 return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
151 int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
153 int rc;
155 struct kvm_device_attr attr = {
156 .group = KVM_S390_VM_MEM_CTRL,
157 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
158 .addr = (uint64_t) &new_limit,
161 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_LIMIT_SIZE)) {
162 return 0;
165 rc = kvm_s390_query_mem_limit(s, hw_limit);
166 if (rc) {
167 return rc;
168 } else if (*hw_limit < new_limit) {
169 return -E2BIG;
172 return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
175 void kvm_s390_clear_cmma_callback(void *opaque)
177 int rc;
178 KVMState *s = opaque;
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(s, 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 if (!rc) {
203 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
205 trace_kvm_enable_cmma(rc);
208 static void kvm_s390_set_attr(uint64_t attr)
210 struct kvm_device_attr attribute = {
211 .group = KVM_S390_VM_CRYPTO,
212 .attr = attr,
215 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
217 if (ret) {
218 error_report("Failed to set crypto device attribute %lu: %s",
219 attr, strerror(-ret));
223 static void kvm_s390_init_aes_kw(void)
225 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
227 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
228 NULL)) {
229 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
232 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
233 kvm_s390_set_attr(attr);
237 static void kvm_s390_init_dea_kw(void)
239 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
241 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
242 NULL)) {
243 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
246 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
247 kvm_s390_set_attr(attr);
251 static void kvm_s390_init_crypto(void)
253 kvm_s390_init_aes_kw();
254 kvm_s390_init_dea_kw();
257 int kvm_arch_init(MachineState *ms, KVMState *s)
259 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
260 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
261 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
262 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
264 kvm_s390_enable_cmma(s);
266 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
267 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
268 phys_mem_set_alloc(legacy_s390_alloc);
271 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
272 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
274 return 0;
277 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
279 return cpu->cpu_index;
282 int kvm_arch_init_vcpu(CPUState *cs)
284 S390CPU *cpu = S390_CPU(cs);
285 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
286 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
287 return 0;
290 void kvm_s390_reset_vcpu(S390CPU *cpu)
292 CPUState *cs = CPU(cpu);
294 /* The initial reset call is needed here to reset in-kernel
295 * vcpu data that we can't access directly from QEMU
296 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
297 * Before this ioctl cpu_synchronize_state() is called in common kvm
298 * code (kvm-all) */
299 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
300 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
303 kvm_s390_init_crypto();
306 static int can_sync_regs(CPUState *cs, int regs)
308 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
311 int kvm_arch_put_registers(CPUState *cs, int level)
313 S390CPU *cpu = S390_CPU(cs);
314 CPUS390XState *env = &cpu->env;
315 struct kvm_sregs sregs;
316 struct kvm_regs regs;
317 struct kvm_fpu fpu = {};
318 int r;
319 int i;
321 /* always save the PSW and the GPRS*/
322 cs->kvm_run->psw_addr = env->psw.addr;
323 cs->kvm_run->psw_mask = env->psw.mask;
325 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
326 for (i = 0; i < 16; i++) {
327 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
328 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
330 } else {
331 for (i = 0; i < 16; i++) {
332 regs.gprs[i] = env->regs[i];
334 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
335 if (r < 0) {
336 return r;
340 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
341 for (i = 0; i < 32; i++) {
342 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
343 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
345 cs->kvm_run->s.regs.fpc = env->fpc;
346 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
347 } else {
348 /* Floating point */
349 for (i = 0; i < 16; i++) {
350 fpu.fprs[i] = get_freg(env, i)->ll;
352 fpu.fpc = env->fpc;
354 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
355 if (r < 0) {
356 return r;
360 /* Do we need to save more than that? */
361 if (level == KVM_PUT_RUNTIME_STATE) {
362 return 0;
365 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
366 cs->kvm_run->s.regs.cputm = env->cputm;
367 cs->kvm_run->s.regs.ckc = env->ckc;
368 cs->kvm_run->s.regs.todpr = env->todpr;
369 cs->kvm_run->s.regs.gbea = env->gbea;
370 cs->kvm_run->s.regs.pp = env->pp;
371 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
372 } else {
374 * These ONE_REGS are not protected by a capability. As they are only
375 * necessary for migration we just trace a possible error, but don't
376 * return with an error return code.
378 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
379 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
380 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
381 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
382 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
385 /* pfault parameters */
386 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
387 cs->kvm_run->s.regs.pft = env->pfault_token;
388 cs->kvm_run->s.regs.pfs = env->pfault_select;
389 cs->kvm_run->s.regs.pfc = env->pfault_compare;
390 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
391 } else if (cap_async_pf) {
392 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
393 if (r < 0) {
394 return r;
396 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
397 if (r < 0) {
398 return r;
400 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
401 if (r < 0) {
402 return r;
406 /* access registers and control registers*/
407 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
408 for (i = 0; i < 16; i++) {
409 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
410 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
412 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
413 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
414 } else {
415 for (i = 0; i < 16; i++) {
416 sregs.acrs[i] = env->aregs[i];
417 sregs.crs[i] = env->cregs[i];
419 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
420 if (r < 0) {
421 return r;
425 /* Finally the prefix */
426 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
427 cs->kvm_run->s.regs.prefix = env->psa;
428 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
429 } else {
430 /* prefix is only supported via sync regs */
432 return 0;
435 int kvm_arch_get_registers(CPUState *cs)
437 S390CPU *cpu = S390_CPU(cs);
438 CPUS390XState *env = &cpu->env;
439 struct kvm_sregs sregs;
440 struct kvm_regs regs;
441 struct kvm_fpu fpu;
442 int i, r;
444 /* get the PSW */
445 env->psw.addr = cs->kvm_run->psw_addr;
446 env->psw.mask = cs->kvm_run->psw_mask;
448 /* the GPRS */
449 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
450 for (i = 0; i < 16; i++) {
451 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
453 } else {
454 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
455 if (r < 0) {
456 return r;
458 for (i = 0; i < 16; i++) {
459 env->regs[i] = regs.gprs[i];
463 /* The ACRS and CRS */
464 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
465 for (i = 0; i < 16; i++) {
466 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
467 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
469 } else {
470 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
471 if (r < 0) {
472 return r;
474 for (i = 0; i < 16; i++) {
475 env->aregs[i] = sregs.acrs[i];
476 env->cregs[i] = sregs.crs[i];
480 /* Floating point and vector registers */
481 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
482 for (i = 0; i < 32; i++) {
483 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
484 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
486 env->fpc = cs->kvm_run->s.regs.fpc;
487 } else {
488 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
489 if (r < 0) {
490 return r;
492 for (i = 0; i < 16; i++) {
493 get_freg(env, i)->ll = fpu.fprs[i];
495 env->fpc = fpu.fpc;
498 /* The prefix */
499 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
500 env->psa = cs->kvm_run->s.regs.prefix;
503 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
504 env->cputm = cs->kvm_run->s.regs.cputm;
505 env->ckc = cs->kvm_run->s.regs.ckc;
506 env->todpr = cs->kvm_run->s.regs.todpr;
507 env->gbea = cs->kvm_run->s.regs.gbea;
508 env->pp = cs->kvm_run->s.regs.pp;
509 } else {
511 * These ONE_REGS are not protected by a capability. As they are only
512 * necessary for migration we just trace a possible error, but don't
513 * return with an error return code.
515 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
516 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
517 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
518 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
519 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
522 /* pfault parameters */
523 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
524 env->pfault_token = cs->kvm_run->s.regs.pft;
525 env->pfault_select = cs->kvm_run->s.regs.pfs;
526 env->pfault_compare = cs->kvm_run->s.regs.pfc;
527 } else if (cap_async_pf) {
528 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
529 if (r < 0) {
530 return r;
532 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
533 if (r < 0) {
534 return r;
536 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
537 if (r < 0) {
538 return r;
542 return 0;
545 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
547 int r;
548 struct kvm_device_attr attr = {
549 .group = KVM_S390_VM_TOD,
550 .attr = KVM_S390_VM_TOD_LOW,
551 .addr = (uint64_t)tod_low,
554 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
555 if (r) {
556 return r;
559 attr.attr = KVM_S390_VM_TOD_HIGH;
560 attr.addr = (uint64_t)tod_high;
561 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
564 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
566 int r;
568 struct kvm_device_attr attr = {
569 .group = KVM_S390_VM_TOD,
570 .attr = KVM_S390_VM_TOD_LOW,
571 .addr = (uint64_t)tod_low,
574 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
575 if (r) {
576 return r;
579 attr.attr = KVM_S390_VM_TOD_HIGH;
580 attr.addr = (uint64_t)tod_high;
581 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
585 * kvm_s390_mem_op:
586 * @addr: the logical start address in guest memory
587 * @ar: the access register number
588 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
589 * @len: length that should be transfered
590 * @is_write: true = write, false = read
591 * Returns: 0 on success, non-zero if an exception or error occured
593 * Use KVM ioctl to read/write from/to guest memory. An access exception
594 * is injected into the vCPU in case of translation errors.
596 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
597 int len, bool is_write)
599 struct kvm_s390_mem_op mem_op = {
600 .gaddr = addr,
601 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
602 .size = len,
603 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
604 : KVM_S390_MEMOP_LOGICAL_READ,
605 .buf = (uint64_t)hostbuf,
606 .ar = ar,
608 int ret;
610 if (!cap_mem_op) {
611 return -ENOSYS;
613 if (!hostbuf) {
614 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
617 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
618 if (ret < 0) {
619 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
621 return ret;
625 * Legacy layout for s390:
626 * Older S390 KVM requires the topmost vma of the RAM to be
627 * smaller than an system defined value, which is at least 256GB.
628 * Larger systems have larger values. We put the guest between
629 * the end of data segment (system break) and this value. We
630 * use 32GB as a base to have enough room for the system break
631 * to grow. We also have to use MAP parameters that avoid
632 * read-only mapping of guest pages.
634 static void *legacy_s390_alloc(size_t size, uint64_t *align)
636 void *mem;
638 mem = mmap((void *) 0x800000000ULL, size,
639 PROT_EXEC|PROT_READ|PROT_WRITE,
640 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
641 return mem == MAP_FAILED ? NULL : mem;
644 /* DIAG 501 is used for sw breakpoints */
645 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
647 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
650 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
651 sizeof(diag_501), 0) ||
652 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
653 sizeof(diag_501), 1)) {
654 return -EINVAL;
656 return 0;
659 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
661 uint8_t t[sizeof(diag_501)];
663 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
664 return -EINVAL;
665 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
666 return -EINVAL;
667 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
668 sizeof(diag_501), 1)) {
669 return -EINVAL;
672 return 0;
675 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
676 int len, int type)
678 int n;
680 for (n = 0; n < nb_hw_breakpoints; n++) {
681 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
682 (hw_breakpoints[n].len == len || len == -1)) {
683 return &hw_breakpoints[n];
687 return NULL;
690 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
692 int size;
694 if (find_hw_breakpoint(addr, len, type)) {
695 return -EEXIST;
698 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
700 if (!hw_breakpoints) {
701 nb_hw_breakpoints = 0;
702 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
703 } else {
704 hw_breakpoints =
705 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
708 if (!hw_breakpoints) {
709 nb_hw_breakpoints = 0;
710 return -ENOMEM;
713 hw_breakpoints[nb_hw_breakpoints].addr = addr;
714 hw_breakpoints[nb_hw_breakpoints].len = len;
715 hw_breakpoints[nb_hw_breakpoints].type = type;
717 nb_hw_breakpoints++;
719 return 0;
722 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
723 target_ulong len, int type)
725 switch (type) {
726 case GDB_BREAKPOINT_HW:
727 type = KVM_HW_BP;
728 break;
729 case GDB_WATCHPOINT_WRITE:
730 if (len < 1) {
731 return -EINVAL;
733 type = KVM_HW_WP_WRITE;
734 break;
735 default:
736 return -ENOSYS;
738 return insert_hw_breakpoint(addr, len, type);
741 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
742 target_ulong len, int type)
744 int size;
745 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
747 if (bp == NULL) {
748 return -ENOENT;
751 nb_hw_breakpoints--;
752 if (nb_hw_breakpoints > 0) {
754 * In order to trim the array, move the last element to the position to
755 * be removed - if necessary.
757 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
758 *bp = hw_breakpoints[nb_hw_breakpoints];
760 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
761 hw_breakpoints =
762 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
763 } else {
764 g_free(hw_breakpoints);
765 hw_breakpoints = NULL;
768 return 0;
771 void kvm_arch_remove_all_hw_breakpoints(void)
773 nb_hw_breakpoints = 0;
774 g_free(hw_breakpoints);
775 hw_breakpoints = NULL;
778 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
780 int i;
782 if (nb_hw_breakpoints > 0) {
783 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
784 dbg->arch.hw_bp = hw_breakpoints;
786 for (i = 0; i < nb_hw_breakpoints; ++i) {
787 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
788 hw_breakpoints[i].addr);
790 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
791 } else {
792 dbg->arch.nr_hw_bp = 0;
793 dbg->arch.hw_bp = NULL;
797 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
801 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
803 return MEMTXATTRS_UNSPECIFIED;
806 int kvm_arch_process_async_events(CPUState *cs)
808 return cs->halted;
811 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
812 struct kvm_s390_interrupt *interrupt)
814 int r = 0;
816 interrupt->type = irq->type;
817 switch (irq->type) {
818 case KVM_S390_INT_VIRTIO:
819 interrupt->parm = irq->u.ext.ext_params;
820 /* fall through */
821 case KVM_S390_INT_PFAULT_INIT:
822 case KVM_S390_INT_PFAULT_DONE:
823 interrupt->parm64 = irq->u.ext.ext_params2;
824 break;
825 case KVM_S390_PROGRAM_INT:
826 interrupt->parm = irq->u.pgm.code;
827 break;
828 case KVM_S390_SIGP_SET_PREFIX:
829 interrupt->parm = irq->u.prefix.address;
830 break;
831 case KVM_S390_INT_SERVICE:
832 interrupt->parm = irq->u.ext.ext_params;
833 break;
834 case KVM_S390_MCHK:
835 interrupt->parm = irq->u.mchk.cr14;
836 interrupt->parm64 = irq->u.mchk.mcic;
837 break;
838 case KVM_S390_INT_EXTERNAL_CALL:
839 interrupt->parm = irq->u.extcall.code;
840 break;
841 case KVM_S390_INT_EMERGENCY:
842 interrupt->parm = irq->u.emerg.code;
843 break;
844 case KVM_S390_SIGP_STOP:
845 case KVM_S390_RESTART:
846 break; /* These types have no parameters */
847 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
848 interrupt->parm = irq->u.io.subchannel_id << 16;
849 interrupt->parm |= irq->u.io.subchannel_nr;
850 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
851 interrupt->parm64 |= irq->u.io.io_int_word;
852 break;
853 default:
854 r = -EINVAL;
855 break;
857 return r;
860 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
862 struct kvm_s390_interrupt kvmint = {};
863 int r;
865 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
866 if (r < 0) {
867 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
868 exit(1);
871 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
872 if (r < 0) {
873 fprintf(stderr, "KVM failed to inject interrupt\n");
874 exit(1);
878 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
880 CPUState *cs = CPU(cpu);
881 int r;
883 if (cap_s390_irq) {
884 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
885 if (!r) {
886 return;
888 error_report("KVM failed to inject interrupt %llx", irq->type);
889 exit(1);
892 inject_vcpu_irq_legacy(cs, irq);
895 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
897 struct kvm_s390_interrupt kvmint = {};
898 int r;
900 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
901 if (r < 0) {
902 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
903 exit(1);
906 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
907 if (r < 0) {
908 fprintf(stderr, "KVM failed to inject interrupt\n");
909 exit(1);
913 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
915 static bool use_flic = true;
916 int r;
918 if (use_flic) {
919 r = kvm_s390_inject_flic(irq);
920 if (r == -ENOSYS) {
921 use_flic = false;
923 if (!r) {
924 return;
927 __kvm_s390_floating_interrupt(irq);
930 void kvm_s390_virtio_irq(int config_change, uint64_t token)
932 struct kvm_s390_irq irq = {
933 .type = KVM_S390_INT_VIRTIO,
934 .u.ext.ext_params = config_change,
935 .u.ext.ext_params2 = token,
938 kvm_s390_floating_interrupt(&irq);
941 void kvm_s390_service_interrupt(uint32_t parm)
943 struct kvm_s390_irq irq = {
944 .type = KVM_S390_INT_SERVICE,
945 .u.ext.ext_params = parm,
948 kvm_s390_floating_interrupt(&irq);
951 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
953 struct kvm_s390_irq irq = {
954 .type = KVM_S390_PROGRAM_INT,
955 .u.pgm.code = code,
958 kvm_s390_vcpu_interrupt(cpu, &irq);
961 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
963 struct kvm_s390_irq irq = {
964 .type = KVM_S390_PROGRAM_INT,
965 .u.pgm.code = code,
966 .u.pgm.trans_exc_code = te_code,
967 .u.pgm.exc_access_id = te_code & 3,
970 kvm_s390_vcpu_interrupt(cpu, &irq);
973 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
974 uint16_t ipbh0)
976 CPUS390XState *env = &cpu->env;
977 uint64_t sccb;
978 uint32_t code;
979 int r = 0;
981 cpu_synchronize_state(CPU(cpu));
982 sccb = env->regs[ipbh0 & 0xf];
983 code = env->regs[(ipbh0 & 0xf0) >> 4];
985 r = sclp_service_call(env, sccb, code);
986 if (r < 0) {
987 enter_pgmcheck(cpu, -r);
988 } else {
989 setcc(cpu, r);
992 return 0;
995 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
997 CPUS390XState *env = &cpu->env;
998 int rc = 0;
999 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1001 cpu_synchronize_state(CPU(cpu));
1003 switch (ipa1) {
1004 case PRIV_B2_XSCH:
1005 ioinst_handle_xsch(cpu, env->regs[1]);
1006 break;
1007 case PRIV_B2_CSCH:
1008 ioinst_handle_csch(cpu, env->regs[1]);
1009 break;
1010 case PRIV_B2_HSCH:
1011 ioinst_handle_hsch(cpu, env->regs[1]);
1012 break;
1013 case PRIV_B2_MSCH:
1014 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1015 break;
1016 case PRIV_B2_SSCH:
1017 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1018 break;
1019 case PRIV_B2_STCRW:
1020 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1021 break;
1022 case PRIV_B2_STSCH:
1023 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1024 break;
1025 case PRIV_B2_TSCH:
1026 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1027 fprintf(stderr, "Spurious tsch intercept\n");
1028 break;
1029 case PRIV_B2_CHSC:
1030 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1031 break;
1032 case PRIV_B2_TPI:
1033 /* This should have been handled by kvm already. */
1034 fprintf(stderr, "Spurious tpi intercept\n");
1035 break;
1036 case PRIV_B2_SCHM:
1037 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1038 run->s390_sieic.ipb);
1039 break;
1040 case PRIV_B2_RSCH:
1041 ioinst_handle_rsch(cpu, env->regs[1]);
1042 break;
1043 case PRIV_B2_RCHP:
1044 ioinst_handle_rchp(cpu, env->regs[1]);
1045 break;
1046 case PRIV_B2_STCPS:
1047 /* We do not provide this instruction, it is suppressed. */
1048 break;
1049 case PRIV_B2_SAL:
1050 ioinst_handle_sal(cpu, env->regs[1]);
1051 break;
1052 case PRIV_B2_SIGA:
1053 /* Not provided, set CC = 3 for subchannel not operational */
1054 setcc(cpu, 3);
1055 break;
1056 case PRIV_B2_SCLP_CALL:
1057 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1058 break;
1059 default:
1060 rc = -1;
1061 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1062 break;
1065 return rc;
1068 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1069 uint8_t *ar)
1071 CPUS390XState *env = &cpu->env;
1072 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1073 uint32_t base2 = run->s390_sieic.ipb >> 28;
1074 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1075 ((run->s390_sieic.ipb & 0xff00) << 4);
1077 if (disp2 & 0x80000) {
1078 disp2 += 0xfff00000;
1080 if (ar) {
1081 *ar = base2;
1084 return (base2 ? env->regs[base2] : 0) +
1085 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1088 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1089 uint8_t *ar)
1091 CPUS390XState *env = &cpu->env;
1092 uint32_t base2 = run->s390_sieic.ipb >> 28;
1093 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1094 ((run->s390_sieic.ipb & 0xff00) << 4);
1096 if (disp2 & 0x80000) {
1097 disp2 += 0xfff00000;
1099 if (ar) {
1100 *ar = base2;
1103 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1106 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1108 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1110 return clp_service_call(cpu, r2);
1113 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1115 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1116 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1118 return pcilg_service_call(cpu, r1, r2);
1121 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1123 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1124 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1126 return pcistg_service_call(cpu, r1, r2);
1129 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1131 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1132 uint64_t fiba;
1133 uint8_t ar;
1135 cpu_synchronize_state(CPU(cpu));
1136 fiba = get_base_disp_rxy(cpu, run, &ar);
1138 return stpcifc_service_call(cpu, r1, fiba, ar);
1141 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1143 /* NOOP */
1144 return 0;
1147 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1149 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1150 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1152 return rpcit_service_call(cpu, r1, r2);
1155 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1157 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1158 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1159 uint64_t gaddr;
1160 uint8_t ar;
1162 cpu_synchronize_state(CPU(cpu));
1163 gaddr = get_base_disp_rsy(cpu, run, &ar);
1165 return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1168 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1170 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1171 uint64_t fiba;
1172 uint8_t ar;
1174 cpu_synchronize_state(CPU(cpu));
1175 fiba = get_base_disp_rxy(cpu, run, &ar);
1177 return mpcifc_service_call(cpu, r1, fiba, ar);
1180 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1182 int r = 0;
1184 switch (ipa1) {
1185 case PRIV_B9_CLP:
1186 r = kvm_clp_service_call(cpu, run);
1187 break;
1188 case PRIV_B9_PCISTG:
1189 r = kvm_pcistg_service_call(cpu, run);
1190 break;
1191 case PRIV_B9_PCILG:
1192 r = kvm_pcilg_service_call(cpu, run);
1193 break;
1194 case PRIV_B9_RPCIT:
1195 r = kvm_rpcit_service_call(cpu, run);
1196 break;
1197 case PRIV_B9_EQBS:
1198 /* just inject exception */
1199 r = -1;
1200 break;
1201 default:
1202 r = -1;
1203 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1204 break;
1207 return r;
1210 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1212 int r = 0;
1214 switch (ipbl) {
1215 case PRIV_EB_PCISTB:
1216 r = kvm_pcistb_service_call(cpu, run);
1217 break;
1218 case PRIV_EB_SIC:
1219 r = kvm_sic_service_call(cpu, run);
1220 break;
1221 case PRIV_EB_SQBS:
1222 /* just inject exception */
1223 r = -1;
1224 break;
1225 default:
1226 r = -1;
1227 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1228 break;
1231 return r;
1234 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1236 int r = 0;
1238 switch (ipbl) {
1239 case PRIV_E3_MPCIFC:
1240 r = kvm_mpcifc_service_call(cpu, run);
1241 break;
1242 case PRIV_E3_STPCIFC:
1243 r = kvm_stpcifc_service_call(cpu, run);
1244 break;
1245 default:
1246 r = -1;
1247 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1248 break;
1251 return r;
1254 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1256 CPUS390XState *env = &cpu->env;
1257 int ret;
1259 cpu_synchronize_state(CPU(cpu));
1260 ret = s390_virtio_hypercall(env);
1261 if (ret == -EINVAL) {
1262 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1263 return 0;
1266 return ret;
1269 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1271 uint64_t r1, r3;
1273 cpu_synchronize_state(CPU(cpu));
1274 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1275 r3 = run->s390_sieic.ipa & 0x000f;
1276 handle_diag_308(&cpu->env, r1, r3);
1279 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1281 CPUS390XState *env = &cpu->env;
1282 unsigned long pc;
1284 cpu_synchronize_state(CPU(cpu));
1286 pc = env->psw.addr - 4;
1287 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1288 env->psw.addr = pc;
1289 return EXCP_DEBUG;
1292 return -ENOENT;
1295 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1297 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1299 int r = 0;
1300 uint16_t func_code;
1303 * For any diagnose call we support, bits 48-63 of the resulting
1304 * address specify the function code; the remainder is ignored.
1306 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1307 switch (func_code) {
1308 case DIAG_IPL:
1309 kvm_handle_diag_308(cpu, run);
1310 break;
1311 case DIAG_KVM_HYPERCALL:
1312 r = handle_hypercall(cpu, run);
1313 break;
1314 case DIAG_KVM_BREAKPOINT:
1315 r = handle_sw_breakpoint(cpu, run);
1316 break;
1317 default:
1318 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1319 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1320 break;
1323 return r;
1326 typedef struct SigpInfo {
1327 S390CPU *cpu;
1328 uint64_t param;
1329 int cc;
1330 uint64_t *status_reg;
1331 } SigpInfo;
1333 static void set_sigp_status(SigpInfo *si, uint64_t status)
1335 *si->status_reg &= 0xffffffff00000000ULL;
1336 *si->status_reg |= status;
1337 si->cc = SIGP_CC_STATUS_STORED;
1340 static void sigp_start(void *arg)
1342 SigpInfo *si = arg;
1344 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1345 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1346 return;
1349 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1350 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1353 static void sigp_stop(void *arg)
1355 SigpInfo *si = arg;
1356 struct kvm_s390_irq irq = {
1357 .type = KVM_S390_SIGP_STOP,
1360 if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1361 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1362 return;
1365 /* disabled wait - sleeping in user space */
1366 if (CPU(si->cpu)->halted) {
1367 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1368 } else {
1369 /* execute the stop function */
1370 si->cpu->env.sigp_order = SIGP_STOP;
1371 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1373 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1376 #define ADTL_SAVE_AREA_SIZE 1024
1377 static int kvm_s390_store_adtl_status(S390CPU *cpu, hwaddr addr)
1379 void *mem;
1380 hwaddr len = ADTL_SAVE_AREA_SIZE;
1382 mem = cpu_physical_memory_map(addr, &len, 1);
1383 if (!mem) {
1384 return -EFAULT;
1386 if (len != ADTL_SAVE_AREA_SIZE) {
1387 cpu_physical_memory_unmap(mem, len, 1, 0);
1388 return -EFAULT;
1391 memcpy(mem, &cpu->env.vregs, 512);
1393 cpu_physical_memory_unmap(mem, len, 1, len);
1395 return 0;
1398 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1399 #define SAVE_AREA_SIZE 512
1400 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1402 static const uint8_t ar_id = 1;
1403 uint64_t ckc = cpu->env.ckc >> 8;
1404 void *mem;
1405 int i;
1406 hwaddr len = SAVE_AREA_SIZE;
1408 mem = cpu_physical_memory_map(addr, &len, 1);
1409 if (!mem) {
1410 return -EFAULT;
1412 if (len != SAVE_AREA_SIZE) {
1413 cpu_physical_memory_unmap(mem, len, 1, 0);
1414 return -EFAULT;
1417 if (store_arch) {
1418 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1420 for (i = 0; i < 16; ++i) {
1421 *((uint64 *)mem + i) = get_freg(&cpu->env, i)->ll;
1423 memcpy(mem + 128, &cpu->env.regs, 128);
1424 memcpy(mem + 256, &cpu->env.psw, 16);
1425 memcpy(mem + 280, &cpu->env.psa, 4);
1426 memcpy(mem + 284, &cpu->env.fpc, 4);
1427 memcpy(mem + 292, &cpu->env.todpr, 4);
1428 memcpy(mem + 296, &cpu->env.cputm, 8);
1429 memcpy(mem + 304, &ckc, 8);
1430 memcpy(mem + 320, &cpu->env.aregs, 64);
1431 memcpy(mem + 384, &cpu->env.cregs, 128);
1433 cpu_physical_memory_unmap(mem, len, 1, len);
1435 return 0;
1438 static void sigp_stop_and_store_status(void *arg)
1440 SigpInfo *si = arg;
1441 struct kvm_s390_irq irq = {
1442 .type = KVM_S390_SIGP_STOP,
1445 /* disabled wait - sleeping in user space */
1446 if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1447 CPU(si->cpu)->halted) {
1448 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1451 switch (s390_cpu_get_state(si->cpu)) {
1452 case CPU_STATE_OPERATING:
1453 si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1454 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1455 /* store will be performed when handling the stop intercept */
1456 break;
1457 case CPU_STATE_STOPPED:
1458 /* already stopped, just store the status */
1459 cpu_synchronize_state(CPU(si->cpu));
1460 kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1461 break;
1463 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1466 static void sigp_store_status_at_address(void *arg)
1468 SigpInfo *si = arg;
1469 uint32_t address = si->param & 0x7ffffe00u;
1471 /* cpu has to be stopped */
1472 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1473 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1474 return;
1477 cpu_synchronize_state(CPU(si->cpu));
1479 if (kvm_s390_store_status(si->cpu, address, false)) {
1480 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1481 return;
1483 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1486 static void sigp_store_adtl_status(void *arg)
1488 SigpInfo *si = arg;
1490 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
1491 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1492 return;
1495 /* cpu has to be stopped */
1496 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1497 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1498 return;
1501 /* parameter must be aligned to 1024-byte boundary */
1502 if (si->param & 0x3ff) {
1503 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1504 return;
1507 cpu_synchronize_state(CPU(si->cpu));
1509 if (kvm_s390_store_adtl_status(si->cpu, si->param)) {
1510 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1511 return;
1513 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1516 static void sigp_restart(void *arg)
1518 SigpInfo *si = arg;
1519 struct kvm_s390_irq irq = {
1520 .type = KVM_S390_RESTART,
1523 switch (s390_cpu_get_state(si->cpu)) {
1524 case CPU_STATE_STOPPED:
1525 /* the restart irq has to be delivered prior to any other pending irq */
1526 cpu_synchronize_state(CPU(si->cpu));
1527 do_restart_interrupt(&si->cpu->env);
1528 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1529 break;
1530 case CPU_STATE_OPERATING:
1531 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1532 break;
1534 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1537 int kvm_s390_cpu_restart(S390CPU *cpu)
1539 SigpInfo si = {
1540 .cpu = cpu,
1543 run_on_cpu(CPU(cpu), sigp_restart, &si);
1544 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1545 return 0;
1548 static void sigp_initial_cpu_reset(void *arg)
1550 SigpInfo *si = arg;
1551 CPUState *cs = CPU(si->cpu);
1552 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1554 cpu_synchronize_state(cs);
1555 scc->initial_cpu_reset(cs);
1556 cpu_synchronize_post_reset(cs);
1557 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1560 static void sigp_cpu_reset(void *arg)
1562 SigpInfo *si = arg;
1563 CPUState *cs = CPU(si->cpu);
1564 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1566 cpu_synchronize_state(cs);
1567 scc->cpu_reset(cs);
1568 cpu_synchronize_post_reset(cs);
1569 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1572 static void sigp_set_prefix(void *arg)
1574 SigpInfo *si = arg;
1575 uint32_t addr = si->param & 0x7fffe000u;
1577 cpu_synchronize_state(CPU(si->cpu));
1579 if (!address_space_access_valid(&address_space_memory, addr,
1580 sizeof(struct LowCore), false)) {
1581 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1582 return;
1585 /* cpu has to be stopped */
1586 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1587 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1588 return;
1591 si->cpu->env.psa = addr;
1592 cpu_synchronize_post_init(CPU(si->cpu));
1593 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1596 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1597 uint64_t param, uint64_t *status_reg)
1599 SigpInfo si = {
1600 .cpu = dst_cpu,
1601 .param = param,
1602 .status_reg = status_reg,
1605 /* cpu available? */
1606 if (dst_cpu == NULL) {
1607 return SIGP_CC_NOT_OPERATIONAL;
1610 /* only resets can break pending orders */
1611 if (dst_cpu->env.sigp_order != 0 &&
1612 order != SIGP_CPU_RESET &&
1613 order != SIGP_INITIAL_CPU_RESET) {
1614 return SIGP_CC_BUSY;
1617 switch (order) {
1618 case SIGP_START:
1619 run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1620 break;
1621 case SIGP_STOP:
1622 run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1623 break;
1624 case SIGP_RESTART:
1625 run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1626 break;
1627 case SIGP_STOP_STORE_STATUS:
1628 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1629 break;
1630 case SIGP_STORE_STATUS_ADDR:
1631 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1632 break;
1633 case SIGP_STORE_ADTL_STATUS:
1634 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, &si);
1635 break;
1636 case SIGP_SET_PREFIX:
1637 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1638 break;
1639 case SIGP_INITIAL_CPU_RESET:
1640 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1641 break;
1642 case SIGP_CPU_RESET:
1643 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1644 break;
1645 default:
1646 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1647 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1650 return si.cc;
1653 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1654 uint64_t *status_reg)
1656 CPUState *cur_cs;
1657 S390CPU *cur_cpu;
1659 /* due to the BQL, we are the only active cpu */
1660 CPU_FOREACH(cur_cs) {
1661 cur_cpu = S390_CPU(cur_cs);
1662 if (cur_cpu->env.sigp_order != 0) {
1663 return SIGP_CC_BUSY;
1665 cpu_synchronize_state(cur_cs);
1666 /* all but the current one have to be stopped */
1667 if (cur_cpu != cpu &&
1668 s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1669 *status_reg &= 0xffffffff00000000ULL;
1670 *status_reg |= SIGP_STAT_INCORRECT_STATE;
1671 return SIGP_CC_STATUS_STORED;
1675 switch (param & 0xff) {
1676 case SIGP_MODE_ESA_S390:
1677 /* not supported */
1678 return SIGP_CC_NOT_OPERATIONAL;
1679 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1680 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1681 CPU_FOREACH(cur_cs) {
1682 cur_cpu = S390_CPU(cur_cs);
1683 cur_cpu->env.pfault_token = -1UL;
1685 break;
1686 default:
1687 *status_reg &= 0xffffffff00000000ULL;
1688 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1689 return SIGP_CC_STATUS_STORED;
1692 return SIGP_CC_ORDER_CODE_ACCEPTED;
1695 #define SIGP_ORDER_MASK 0x000000ff
1697 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1699 CPUS390XState *env = &cpu->env;
1700 const uint8_t r1 = ipa1 >> 4;
1701 const uint8_t r3 = ipa1 & 0x0f;
1702 int ret;
1703 uint8_t order;
1704 uint64_t *status_reg;
1705 uint64_t param;
1706 S390CPU *dst_cpu = NULL;
1708 cpu_synchronize_state(CPU(cpu));
1710 /* get order code */
1711 order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1712 & SIGP_ORDER_MASK;
1713 status_reg = &env->regs[r1];
1714 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1716 switch (order) {
1717 case SIGP_SET_ARCH:
1718 ret = sigp_set_architecture(cpu, param, status_reg);
1719 break;
1720 default:
1721 /* all other sigp orders target a single vcpu */
1722 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1723 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1726 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1727 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1729 if (ret >= 0) {
1730 setcc(cpu, ret);
1731 return 0;
1734 return ret;
1737 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1739 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1740 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1741 int r = -1;
1743 DPRINTF("handle_instruction 0x%x 0x%x\n",
1744 run->s390_sieic.ipa, run->s390_sieic.ipb);
1745 switch (ipa0) {
1746 case IPA0_B2:
1747 r = handle_b2(cpu, run, ipa1);
1748 break;
1749 case IPA0_B9:
1750 r = handle_b9(cpu, run, ipa1);
1751 break;
1752 case IPA0_EB:
1753 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1754 break;
1755 case IPA0_E3:
1756 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1757 break;
1758 case IPA0_DIAG:
1759 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1760 break;
1761 case IPA0_SIGP:
1762 r = handle_sigp(cpu, run, ipa1);
1763 break;
1766 if (r < 0) {
1767 r = 0;
1768 enter_pgmcheck(cpu, 0x0001);
1771 return r;
1774 static bool is_special_wait_psw(CPUState *cs)
1776 /* signal quiesce */
1777 return cs->kvm_run->psw_addr == 0xfffUL;
1780 static void guest_panicked(void)
1782 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1783 &error_abort);
1784 vm_stop(RUN_STATE_GUEST_PANICKED);
1787 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1789 CPUState *cs = CPU(cpu);
1791 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1792 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1793 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1794 s390_cpu_halt(cpu);
1795 guest_panicked();
1798 static int handle_intercept(S390CPU *cpu)
1800 CPUState *cs = CPU(cpu);
1801 struct kvm_run *run = cs->kvm_run;
1802 int icpt_code = run->s390_sieic.icptcode;
1803 int r = 0;
1805 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1806 (long)cs->kvm_run->psw_addr);
1807 switch (icpt_code) {
1808 case ICPT_INSTRUCTION:
1809 r = handle_instruction(cpu, run);
1810 break;
1811 case ICPT_PROGRAM:
1812 unmanageable_intercept(cpu, "program interrupt",
1813 offsetof(LowCore, program_new_psw));
1814 r = EXCP_HALTED;
1815 break;
1816 case ICPT_EXT_INT:
1817 unmanageable_intercept(cpu, "external interrupt",
1818 offsetof(LowCore, external_new_psw));
1819 r = EXCP_HALTED;
1820 break;
1821 case ICPT_WAITPSW:
1822 /* disabled wait, since enabled wait is handled in kernel */
1823 cpu_synchronize_state(cs);
1824 if (s390_cpu_halt(cpu) == 0) {
1825 if (is_special_wait_psw(cs)) {
1826 qemu_system_shutdown_request();
1827 } else {
1828 guest_panicked();
1831 r = EXCP_HALTED;
1832 break;
1833 case ICPT_CPU_STOP:
1834 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1835 qemu_system_shutdown_request();
1837 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1838 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1839 true);
1841 cpu->env.sigp_order = 0;
1842 r = EXCP_HALTED;
1843 break;
1844 case ICPT_SOFT_INTERCEPT:
1845 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1846 exit(1);
1847 break;
1848 case ICPT_IO:
1849 fprintf(stderr, "KVM unimplemented icpt IO\n");
1850 exit(1);
1851 break;
1852 default:
1853 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1854 exit(1);
1855 break;
1858 return r;
1861 static int handle_tsch(S390CPU *cpu)
1863 CPUState *cs = CPU(cpu);
1864 struct kvm_run *run = cs->kvm_run;
1865 int ret;
1867 cpu_synchronize_state(cs);
1869 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1870 if (ret < 0) {
1872 * Failure.
1873 * If an I/O interrupt had been dequeued, we have to reinject it.
1875 if (run->s390_tsch.dequeued) {
1876 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1877 run->s390_tsch.subchannel_nr,
1878 run->s390_tsch.io_int_parm,
1879 run->s390_tsch.io_int_word);
1881 ret = 0;
1883 return ret;
1886 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1888 struct sysib_322 sysib;
1889 int del;
1891 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1892 return;
1894 /* Shift the stack of Extended Names to prepare for our own data */
1895 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1896 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1897 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1898 * assumed it's not capable of managing Extended Names for lower levels.
1900 for (del = 1; del < sysib.count; del++) {
1901 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1902 break;
1905 if (del < sysib.count) {
1906 memset(sysib.ext_names[del], 0,
1907 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1909 /* Insert short machine name in EBCDIC, padded with blanks */
1910 if (qemu_name) {
1911 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1912 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1913 strlen(qemu_name)));
1915 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1916 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1917 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1918 * considered by s390 as not capable of providing any Extended Name.
1919 * Therefore if no name was specified on qemu invocation, we go with the
1920 * same "KVMguest" default, which KVM has filled into short name field.
1922 if (qemu_name) {
1923 strncpy((char *)sysib.ext_names[0], qemu_name,
1924 sizeof(sysib.ext_names[0]));
1925 } else {
1926 strcpy((char *)sysib.ext_names[0], "KVMguest");
1928 /* Insert UUID */
1929 memcpy(sysib.vm[0].uuid, qemu_uuid, sizeof(sysib.vm[0].uuid));
1931 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1934 static int handle_stsi(S390CPU *cpu)
1936 CPUState *cs = CPU(cpu);
1937 struct kvm_run *run = cs->kvm_run;
1939 switch (run->s390_stsi.fc) {
1940 case 3:
1941 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1942 return 0;
1944 /* Only sysib 3.2.2 needs post-handling for now. */
1945 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1946 return 0;
1947 default:
1948 return 0;
1952 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1954 CPUState *cs = CPU(cpu);
1955 struct kvm_run *run = cs->kvm_run;
1957 int ret = 0;
1958 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1960 switch (arch_info->type) {
1961 case KVM_HW_WP_WRITE:
1962 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1963 cs->watchpoint_hit = &hw_watchpoint;
1964 hw_watchpoint.vaddr = arch_info->addr;
1965 hw_watchpoint.flags = BP_MEM_WRITE;
1966 ret = EXCP_DEBUG;
1968 break;
1969 case KVM_HW_BP:
1970 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1971 ret = EXCP_DEBUG;
1973 break;
1974 case KVM_SINGLESTEP:
1975 if (cs->singlestep_enabled) {
1976 ret = EXCP_DEBUG;
1978 break;
1979 default:
1980 ret = -ENOSYS;
1983 return ret;
1986 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1988 S390CPU *cpu = S390_CPU(cs);
1989 int ret = 0;
1991 switch (run->exit_reason) {
1992 case KVM_EXIT_S390_SIEIC:
1993 ret = handle_intercept(cpu);
1994 break;
1995 case KVM_EXIT_S390_RESET:
1996 s390_reipl_request();
1997 break;
1998 case KVM_EXIT_S390_TSCH:
1999 ret = handle_tsch(cpu);
2000 break;
2001 case KVM_EXIT_S390_STSI:
2002 ret = handle_stsi(cpu);
2003 break;
2004 case KVM_EXIT_DEBUG:
2005 ret = kvm_arch_handle_debug_exit(cpu);
2006 break;
2007 default:
2008 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2009 break;
2012 if (ret == 0) {
2013 ret = EXCP_INTERRUPT;
2015 return ret;
2018 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2020 return true;
2023 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
2025 return 1;
2028 int kvm_arch_on_sigbus(int code, void *addr)
2030 return 1;
2033 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2034 uint16_t subchannel_nr, uint32_t io_int_parm,
2035 uint32_t io_int_word)
2037 struct kvm_s390_irq irq = {
2038 .u.io.subchannel_id = subchannel_id,
2039 .u.io.subchannel_nr = subchannel_nr,
2040 .u.io.io_int_parm = io_int_parm,
2041 .u.io.io_int_word = io_int_word,
2044 if (io_int_word & IO_INT_WORD_AI) {
2045 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2046 } else {
2047 irq.type = ((subchannel_id & 0xff00) << 24) |
2048 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
2050 kvm_s390_floating_interrupt(&irq);
2053 void kvm_s390_crw_mchk(void)
2055 struct kvm_s390_irq irq = {
2056 .type = KVM_S390_MCHK,
2057 .u.mchk.cr14 = 1 << 28,
2058 .u.mchk.mcic = 0x00400f1d40330000ULL,
2060 kvm_s390_floating_interrupt(&irq);
2063 void kvm_s390_enable_css_support(S390CPU *cpu)
2065 int r;
2067 /* Activate host kernel channel subsystem support. */
2068 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2069 assert(r == 0);
2072 void kvm_arch_init_irq_routing(KVMState *s)
2075 * Note that while irqchip capabilities generally imply that cpustates
2076 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2077 * have to override the common code kvm_halt_in_kernel_allowed setting.
2079 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2080 kvm_gsi_routing_allowed = true;
2081 kvm_halt_in_kernel_allowed = false;
2085 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2086 int vq, bool assign)
2088 struct kvm_ioeventfd kick = {
2089 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2090 KVM_IOEVENTFD_FLAG_DATAMATCH,
2091 .fd = event_notifier_get_fd(notifier),
2092 .datamatch = vq,
2093 .addr = sch,
2094 .len = 8,
2096 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2097 return -ENOSYS;
2099 if (!assign) {
2100 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2102 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2105 int kvm_s390_get_memslot_count(KVMState *s)
2107 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
2110 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2112 struct kvm_mp_state mp_state = {};
2113 int ret;
2115 /* the kvm part might not have been initialized yet */
2116 if (CPU(cpu)->kvm_state == NULL) {
2117 return 0;
2120 switch (cpu_state) {
2121 case CPU_STATE_STOPPED:
2122 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2123 break;
2124 case CPU_STATE_CHECK_STOP:
2125 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2126 break;
2127 case CPU_STATE_OPERATING:
2128 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2129 break;
2130 case CPU_STATE_LOAD:
2131 mp_state.mp_state = KVM_MP_STATE_LOAD;
2132 break;
2133 default:
2134 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2135 cpu_state);
2136 exit(1);
2139 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2140 if (ret) {
2141 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2142 strerror(-ret));
2145 return ret;
2148 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2150 struct kvm_s390_irq_state irq_state;
2151 CPUState *cs = CPU(cpu);
2152 int32_t bytes;
2154 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2155 return;
2158 irq_state.buf = (uint64_t) cpu->irqstate;
2159 irq_state.len = VCPU_IRQ_BUF_SIZE;
2161 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2162 if (bytes < 0) {
2163 cpu->irqstate_saved_size = 0;
2164 error_report("Migration of interrupt state failed");
2165 return;
2168 cpu->irqstate_saved_size = bytes;
2171 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2173 CPUState *cs = CPU(cpu);
2174 struct kvm_s390_irq_state irq_state;
2175 int r;
2177 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2178 return -ENOSYS;
2181 if (cpu->irqstate_saved_size == 0) {
2182 return 0;
2184 irq_state.buf = (uint64_t) cpu->irqstate;
2185 irq_state.len = cpu->irqstate_saved_size;
2187 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2188 if (r) {
2189 error_report("Setting interrupt state failed %d", r);
2191 return r;
2194 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2195 uint64_t address, uint32_t data)
2197 S390PCIBusDevice *pbdev;
2198 uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
2199 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2201 pbdev = s390_pci_find_dev_by_fid(fid);
2202 if (!pbdev) {
2203 DPRINTF("add_msi_route no dev\n");
2204 return -ENODEV;
2207 pbdev->routes.adapter.ind_offset = vec;
2209 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2210 route->flags = 0;
2211 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2212 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2213 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2214 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2215 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2216 return 0;