ppc: move POWER8 Book4 regs in their own routine
[qemu.git] / target-s390x / kvm.c
blobe1859cae0492a4e9d09858c01262a2a6f905ba36
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 "qemu/osdep.h"
25 #include <sys/ioctl.h>
26 #include <sys/mman.h>
28 #include <linux/kvm.h>
29 #include <asm/ptrace.h>
31 #include "qemu-common.h"
32 #include "qemu/error-report.h"
33 #include "qemu/timer.h"
34 #include "sysemu/sysemu.h"
35 #include "sysemu/kvm.h"
36 #include "hw/hw.h"
37 #include "cpu.h"
38 #include "sysemu/device_tree.h"
39 #include "qapi/qmp/qjson.h"
40 #include "exec/gdbstub.h"
41 #include "exec/address-spaces.h"
42 #include "trace.h"
43 #include "qapi-event.h"
44 #include "hw/s390x/s390-pci-inst.h"
45 #include "hw/s390x/s390-pci-bus.h"
46 #include "hw/s390x/ipl.h"
47 #include "hw/s390x/ebcdic.h"
48 #include "exec/memattrs.h"
50 /* #define DEBUG_KVM */
52 #ifdef DEBUG_KVM
53 #define DPRINTF(fmt, ...) \
54 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
55 #else
56 #define DPRINTF(fmt, ...) \
57 do { } while (0)
58 #endif
60 #define kvm_vm_check_mem_attr(s, attr) \
61 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
63 #define IPA0_DIAG 0x8300
64 #define IPA0_SIGP 0xae00
65 #define IPA0_B2 0xb200
66 #define IPA0_B9 0xb900
67 #define IPA0_EB 0xeb00
68 #define IPA0_E3 0xe300
70 #define PRIV_B2_SCLP_CALL 0x20
71 #define PRIV_B2_CSCH 0x30
72 #define PRIV_B2_HSCH 0x31
73 #define PRIV_B2_MSCH 0x32
74 #define PRIV_B2_SSCH 0x33
75 #define PRIV_B2_STSCH 0x34
76 #define PRIV_B2_TSCH 0x35
77 #define PRIV_B2_TPI 0x36
78 #define PRIV_B2_SAL 0x37
79 #define PRIV_B2_RSCH 0x38
80 #define PRIV_B2_STCRW 0x39
81 #define PRIV_B2_STCPS 0x3a
82 #define PRIV_B2_RCHP 0x3b
83 #define PRIV_B2_SCHM 0x3c
84 #define PRIV_B2_CHSC 0x5f
85 #define PRIV_B2_SIGA 0x74
86 #define PRIV_B2_XSCH 0x76
88 #define PRIV_EB_SQBS 0x8a
89 #define PRIV_EB_PCISTB 0xd0
90 #define PRIV_EB_SIC 0xd1
92 #define PRIV_B9_EQBS 0x9c
93 #define PRIV_B9_CLP 0xa0
94 #define PRIV_B9_PCISTG 0xd0
95 #define PRIV_B9_PCILG 0xd2
96 #define PRIV_B9_RPCIT 0xd3
98 #define PRIV_E3_MPCIFC 0xd0
99 #define PRIV_E3_STPCIFC 0xd4
101 #define DIAG_TIMEREVENT 0x288
102 #define DIAG_IPL 0x308
103 #define DIAG_KVM_HYPERCALL 0x500
104 #define DIAG_KVM_BREAKPOINT 0x501
106 #define ICPT_INSTRUCTION 0x04
107 #define ICPT_PROGRAM 0x08
108 #define ICPT_EXT_INT 0x14
109 #define ICPT_WAITPSW 0x1c
110 #define ICPT_SOFT_INTERCEPT 0x24
111 #define ICPT_CPU_STOP 0x28
112 #define ICPT_IO 0x40
114 #define NR_LOCAL_IRQS 32
116 * Needs to be big enough to contain max_cpus emergency signals
117 * and in addition NR_LOCAL_IRQS interrupts
119 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
120 (max_cpus + NR_LOCAL_IRQS))
122 static CPUWatchpoint hw_watchpoint;
124 * We don't use a list because this structure is also used to transmit the
125 * hardware breakpoints to the kernel.
127 static struct kvm_hw_breakpoint *hw_breakpoints;
128 static int nb_hw_breakpoints;
130 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
131 KVM_CAP_LAST_INFO
134 static int cap_sync_regs;
135 static int cap_async_pf;
136 static int cap_mem_op;
137 static int cap_s390_irq;
139 static void *legacy_s390_alloc(size_t size, uint64_t *align);
141 static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
143 struct kvm_device_attr attr = {
144 .group = KVM_S390_VM_MEM_CTRL,
145 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
146 .addr = (uint64_t) memory_limit,
149 return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
152 int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
154 int rc;
156 struct kvm_device_attr attr = {
157 .group = KVM_S390_VM_MEM_CTRL,
158 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
159 .addr = (uint64_t) &new_limit,
162 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_LIMIT_SIZE)) {
163 return 0;
166 rc = kvm_s390_query_mem_limit(s, hw_limit);
167 if (rc) {
168 return rc;
169 } else if (*hw_limit < new_limit) {
170 return -E2BIG;
173 return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
176 void kvm_s390_cmma_reset(void)
178 int rc;
179 struct kvm_device_attr attr = {
180 .group = KVM_S390_VM_MEM_CTRL,
181 .attr = KVM_S390_VM_MEM_CLR_CMMA,
184 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
185 trace_kvm_clear_cmma(rc);
188 static void kvm_s390_enable_cmma(KVMState *s)
190 int rc;
191 struct kvm_device_attr attr = {
192 .group = KVM_S390_VM_MEM_CTRL,
193 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
196 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_ENABLE_CMMA) ||
197 !kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_CLR_CMMA)) {
198 return;
201 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
202 trace_kvm_enable_cmma(rc);
205 static void kvm_s390_set_attr(uint64_t attr)
207 struct kvm_device_attr attribute = {
208 .group = KVM_S390_VM_CRYPTO,
209 .attr = attr,
212 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
214 if (ret) {
215 error_report("Failed to set crypto device attribute %lu: %s",
216 attr, strerror(-ret));
220 static void kvm_s390_init_aes_kw(void)
222 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
224 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
225 NULL)) {
226 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
229 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
230 kvm_s390_set_attr(attr);
234 static void kvm_s390_init_dea_kw(void)
236 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
238 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
239 NULL)) {
240 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
243 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
244 kvm_s390_set_attr(attr);
248 void kvm_s390_crypto_reset(void)
250 kvm_s390_init_aes_kw();
251 kvm_s390_init_dea_kw();
254 int kvm_arch_init(MachineState *ms, KVMState *s)
256 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
257 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
258 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
259 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
261 if (!mem_path) {
262 kvm_s390_enable_cmma(s);
265 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
266 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
267 phys_mem_set_alloc(legacy_s390_alloc);
270 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
271 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 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);
304 static int can_sync_regs(CPUState *cs, int regs)
306 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
309 int kvm_arch_put_registers(CPUState *cs, int level)
311 S390CPU *cpu = S390_CPU(cs);
312 CPUS390XState *env = &cpu->env;
313 struct kvm_sregs sregs;
314 struct kvm_regs regs;
315 struct kvm_fpu fpu = {};
316 int r;
317 int i;
319 /* always save the PSW and the GPRS*/
320 cs->kvm_run->psw_addr = env->psw.addr;
321 cs->kvm_run->psw_mask = env->psw.mask;
323 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
324 for (i = 0; i < 16; i++) {
325 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
326 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
328 } else {
329 for (i = 0; i < 16; i++) {
330 regs.gprs[i] = env->regs[i];
332 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
333 if (r < 0) {
334 return r;
338 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
339 for (i = 0; i < 32; i++) {
340 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
341 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
343 cs->kvm_run->s.regs.fpc = env->fpc;
344 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
345 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
346 for (i = 0; i < 16; i++) {
347 cs->kvm_run->s.regs.fprs[i] = get_freg(env, i)->ll;
349 cs->kvm_run->s.regs.fpc = env->fpc;
350 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
351 } else {
352 /* Floating point */
353 for (i = 0; i < 16; i++) {
354 fpu.fprs[i] = get_freg(env, i)->ll;
356 fpu.fpc = env->fpc;
358 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
359 if (r < 0) {
360 return r;
364 /* Do we need to save more than that? */
365 if (level == KVM_PUT_RUNTIME_STATE) {
366 return 0;
369 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
370 cs->kvm_run->s.regs.cputm = env->cputm;
371 cs->kvm_run->s.regs.ckc = env->ckc;
372 cs->kvm_run->s.regs.todpr = env->todpr;
373 cs->kvm_run->s.regs.gbea = env->gbea;
374 cs->kvm_run->s.regs.pp = env->pp;
375 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
376 } else {
378 * These ONE_REGS are not protected by a capability. As they are only
379 * necessary for migration we just trace a possible error, but don't
380 * return with an error return code.
382 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
383 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
384 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
385 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
386 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
389 /* pfault parameters */
390 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
391 cs->kvm_run->s.regs.pft = env->pfault_token;
392 cs->kvm_run->s.regs.pfs = env->pfault_select;
393 cs->kvm_run->s.regs.pfc = env->pfault_compare;
394 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
395 } else if (cap_async_pf) {
396 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
397 if (r < 0) {
398 return r;
400 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
401 if (r < 0) {
402 return r;
404 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
405 if (r < 0) {
406 return r;
410 /* access registers and control registers*/
411 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
412 for (i = 0; i < 16; i++) {
413 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
414 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
416 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
417 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
418 } else {
419 for (i = 0; i < 16; i++) {
420 sregs.acrs[i] = env->aregs[i];
421 sregs.crs[i] = env->cregs[i];
423 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
424 if (r < 0) {
425 return r;
429 /* Finally the prefix */
430 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
431 cs->kvm_run->s.regs.prefix = env->psa;
432 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
433 } else {
434 /* prefix is only supported via sync regs */
436 return 0;
439 int kvm_arch_get_registers(CPUState *cs)
441 S390CPU *cpu = S390_CPU(cs);
442 CPUS390XState *env = &cpu->env;
443 struct kvm_sregs sregs;
444 struct kvm_regs regs;
445 struct kvm_fpu fpu;
446 int i, r;
448 /* get the PSW */
449 env->psw.addr = cs->kvm_run->psw_addr;
450 env->psw.mask = cs->kvm_run->psw_mask;
452 /* the GPRS */
453 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
454 for (i = 0; i < 16; i++) {
455 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
457 } else {
458 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
459 if (r < 0) {
460 return r;
462 for (i = 0; i < 16; i++) {
463 env->regs[i] = regs.gprs[i];
467 /* The ACRS and CRS */
468 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
469 for (i = 0; i < 16; i++) {
470 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
471 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
473 } else {
474 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
475 if (r < 0) {
476 return r;
478 for (i = 0; i < 16; i++) {
479 env->aregs[i] = sregs.acrs[i];
480 env->cregs[i] = sregs.crs[i];
484 /* Floating point and vector registers */
485 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
486 for (i = 0; i < 32; i++) {
487 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
488 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
490 env->fpc = cs->kvm_run->s.regs.fpc;
491 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
492 for (i = 0; i < 16; i++) {
493 get_freg(env, i)->ll = cs->kvm_run->s.regs.fprs[i];
495 env->fpc = cs->kvm_run->s.regs.fpc;
496 } else {
497 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
498 if (r < 0) {
499 return r;
501 for (i = 0; i < 16; i++) {
502 get_freg(env, i)->ll = fpu.fprs[i];
504 env->fpc = fpu.fpc;
507 /* The prefix */
508 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
509 env->psa = cs->kvm_run->s.regs.prefix;
512 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
513 env->cputm = cs->kvm_run->s.regs.cputm;
514 env->ckc = cs->kvm_run->s.regs.ckc;
515 env->todpr = cs->kvm_run->s.regs.todpr;
516 env->gbea = cs->kvm_run->s.regs.gbea;
517 env->pp = cs->kvm_run->s.regs.pp;
518 } else {
520 * These ONE_REGS are not protected by a capability. As they are only
521 * necessary for migration we just trace a possible error, but don't
522 * return with an error return code.
524 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
525 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
526 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
527 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
528 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
531 /* pfault parameters */
532 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
533 env->pfault_token = cs->kvm_run->s.regs.pft;
534 env->pfault_select = cs->kvm_run->s.regs.pfs;
535 env->pfault_compare = cs->kvm_run->s.regs.pfc;
536 } else if (cap_async_pf) {
537 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
538 if (r < 0) {
539 return r;
541 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
542 if (r < 0) {
543 return r;
545 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
546 if (r < 0) {
547 return r;
551 return 0;
554 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
556 int r;
557 struct kvm_device_attr attr = {
558 .group = KVM_S390_VM_TOD,
559 .attr = KVM_S390_VM_TOD_LOW,
560 .addr = (uint64_t)tod_low,
563 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
564 if (r) {
565 return r;
568 attr.attr = KVM_S390_VM_TOD_HIGH;
569 attr.addr = (uint64_t)tod_high;
570 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
573 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
575 int r;
577 struct kvm_device_attr attr = {
578 .group = KVM_S390_VM_TOD,
579 .attr = KVM_S390_VM_TOD_LOW,
580 .addr = (uint64_t)tod_low,
583 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
584 if (r) {
585 return r;
588 attr.attr = KVM_S390_VM_TOD_HIGH;
589 attr.addr = (uint64_t)tod_high;
590 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
594 * kvm_s390_mem_op:
595 * @addr: the logical start address in guest memory
596 * @ar: the access register number
597 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
598 * @len: length that should be transferred
599 * @is_write: true = write, false = read
600 * Returns: 0 on success, non-zero if an exception or error occurred
602 * Use KVM ioctl to read/write from/to guest memory. An access exception
603 * is injected into the vCPU in case of translation errors.
605 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
606 int len, bool is_write)
608 struct kvm_s390_mem_op mem_op = {
609 .gaddr = addr,
610 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
611 .size = len,
612 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
613 : KVM_S390_MEMOP_LOGICAL_READ,
614 .buf = (uint64_t)hostbuf,
615 .ar = ar,
617 int ret;
619 if (!cap_mem_op) {
620 return -ENOSYS;
622 if (!hostbuf) {
623 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
626 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
627 if (ret < 0) {
628 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
630 return ret;
634 * Legacy layout for s390:
635 * Older S390 KVM requires the topmost vma of the RAM to be
636 * smaller than an system defined value, which is at least 256GB.
637 * Larger systems have larger values. We put the guest between
638 * the end of data segment (system break) and this value. We
639 * use 32GB as a base to have enough room for the system break
640 * to grow. We also have to use MAP parameters that avoid
641 * read-only mapping of guest pages.
643 static void *legacy_s390_alloc(size_t size, uint64_t *align)
645 void *mem;
647 mem = mmap((void *) 0x800000000ULL, size,
648 PROT_EXEC|PROT_READ|PROT_WRITE,
649 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
650 return mem == MAP_FAILED ? NULL : mem;
653 /* DIAG 501 is used for sw breakpoints */
654 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
656 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
659 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
660 sizeof(diag_501), 0) ||
661 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
662 sizeof(diag_501), 1)) {
663 return -EINVAL;
665 return 0;
668 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
670 uint8_t t[sizeof(diag_501)];
672 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
673 return -EINVAL;
674 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
675 return -EINVAL;
676 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
677 sizeof(diag_501), 1)) {
678 return -EINVAL;
681 return 0;
684 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
685 int len, int type)
687 int n;
689 for (n = 0; n < nb_hw_breakpoints; n++) {
690 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
691 (hw_breakpoints[n].len == len || len == -1)) {
692 return &hw_breakpoints[n];
696 return NULL;
699 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
701 int size;
703 if (find_hw_breakpoint(addr, len, type)) {
704 return -EEXIST;
707 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
709 if (!hw_breakpoints) {
710 nb_hw_breakpoints = 0;
711 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
712 } else {
713 hw_breakpoints =
714 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
717 if (!hw_breakpoints) {
718 nb_hw_breakpoints = 0;
719 return -ENOMEM;
722 hw_breakpoints[nb_hw_breakpoints].addr = addr;
723 hw_breakpoints[nb_hw_breakpoints].len = len;
724 hw_breakpoints[nb_hw_breakpoints].type = type;
726 nb_hw_breakpoints++;
728 return 0;
731 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
732 target_ulong len, int type)
734 switch (type) {
735 case GDB_BREAKPOINT_HW:
736 type = KVM_HW_BP;
737 break;
738 case GDB_WATCHPOINT_WRITE:
739 if (len < 1) {
740 return -EINVAL;
742 type = KVM_HW_WP_WRITE;
743 break;
744 default:
745 return -ENOSYS;
747 return insert_hw_breakpoint(addr, len, type);
750 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
751 target_ulong len, int type)
753 int size;
754 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
756 if (bp == NULL) {
757 return -ENOENT;
760 nb_hw_breakpoints--;
761 if (nb_hw_breakpoints > 0) {
763 * In order to trim the array, move the last element to the position to
764 * be removed - if necessary.
766 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
767 *bp = hw_breakpoints[nb_hw_breakpoints];
769 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
770 hw_breakpoints =
771 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
772 } else {
773 g_free(hw_breakpoints);
774 hw_breakpoints = NULL;
777 return 0;
780 void kvm_arch_remove_all_hw_breakpoints(void)
782 nb_hw_breakpoints = 0;
783 g_free(hw_breakpoints);
784 hw_breakpoints = NULL;
787 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
789 int i;
791 if (nb_hw_breakpoints > 0) {
792 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
793 dbg->arch.hw_bp = hw_breakpoints;
795 for (i = 0; i < nb_hw_breakpoints; ++i) {
796 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
797 hw_breakpoints[i].addr);
799 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
800 } else {
801 dbg->arch.nr_hw_bp = 0;
802 dbg->arch.hw_bp = NULL;
806 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
810 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
812 return MEMTXATTRS_UNSPECIFIED;
815 int kvm_arch_process_async_events(CPUState *cs)
817 return cs->halted;
820 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
821 struct kvm_s390_interrupt *interrupt)
823 int r = 0;
825 interrupt->type = irq->type;
826 switch (irq->type) {
827 case KVM_S390_INT_VIRTIO:
828 interrupt->parm = irq->u.ext.ext_params;
829 /* fall through */
830 case KVM_S390_INT_PFAULT_INIT:
831 case KVM_S390_INT_PFAULT_DONE:
832 interrupt->parm64 = irq->u.ext.ext_params2;
833 break;
834 case KVM_S390_PROGRAM_INT:
835 interrupt->parm = irq->u.pgm.code;
836 break;
837 case KVM_S390_SIGP_SET_PREFIX:
838 interrupt->parm = irq->u.prefix.address;
839 break;
840 case KVM_S390_INT_SERVICE:
841 interrupt->parm = irq->u.ext.ext_params;
842 break;
843 case KVM_S390_MCHK:
844 interrupt->parm = irq->u.mchk.cr14;
845 interrupt->parm64 = irq->u.mchk.mcic;
846 break;
847 case KVM_S390_INT_EXTERNAL_CALL:
848 interrupt->parm = irq->u.extcall.code;
849 break;
850 case KVM_S390_INT_EMERGENCY:
851 interrupt->parm = irq->u.emerg.code;
852 break;
853 case KVM_S390_SIGP_STOP:
854 case KVM_S390_RESTART:
855 break; /* These types have no parameters */
856 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
857 interrupt->parm = irq->u.io.subchannel_id << 16;
858 interrupt->parm |= irq->u.io.subchannel_nr;
859 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
860 interrupt->parm64 |= irq->u.io.io_int_word;
861 break;
862 default:
863 r = -EINVAL;
864 break;
866 return r;
869 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
871 struct kvm_s390_interrupt kvmint = {};
872 int r;
874 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
875 if (r < 0) {
876 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
877 exit(1);
880 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
881 if (r < 0) {
882 fprintf(stderr, "KVM failed to inject interrupt\n");
883 exit(1);
887 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
889 CPUState *cs = CPU(cpu);
890 int r;
892 if (cap_s390_irq) {
893 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
894 if (!r) {
895 return;
897 error_report("KVM failed to inject interrupt %llx", irq->type);
898 exit(1);
901 inject_vcpu_irq_legacy(cs, irq);
904 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
906 struct kvm_s390_interrupt kvmint = {};
907 int r;
909 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
910 if (r < 0) {
911 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
912 exit(1);
915 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
916 if (r < 0) {
917 fprintf(stderr, "KVM failed to inject interrupt\n");
918 exit(1);
922 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
924 static bool use_flic = true;
925 int r;
927 if (use_flic) {
928 r = kvm_s390_inject_flic(irq);
929 if (r == -ENOSYS) {
930 use_flic = false;
932 if (!r) {
933 return;
936 __kvm_s390_floating_interrupt(irq);
939 void kvm_s390_service_interrupt(uint32_t parm)
941 struct kvm_s390_irq irq = {
942 .type = KVM_S390_INT_SERVICE,
943 .u.ext.ext_params = parm,
946 kvm_s390_floating_interrupt(&irq);
949 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
951 struct kvm_s390_irq irq = {
952 .type = KVM_S390_PROGRAM_INT,
953 .u.pgm.code = code,
956 kvm_s390_vcpu_interrupt(cpu, &irq);
959 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
961 struct kvm_s390_irq irq = {
962 .type = KVM_S390_PROGRAM_INT,
963 .u.pgm.code = code,
964 .u.pgm.trans_exc_code = te_code,
965 .u.pgm.exc_access_id = te_code & 3,
968 kvm_s390_vcpu_interrupt(cpu, &irq);
971 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
972 uint16_t ipbh0)
974 CPUS390XState *env = &cpu->env;
975 uint64_t sccb;
976 uint32_t code;
977 int r = 0;
979 cpu_synchronize_state(CPU(cpu));
980 sccb = env->regs[ipbh0 & 0xf];
981 code = env->regs[(ipbh0 & 0xf0) >> 4];
983 r = sclp_service_call(env, sccb, code);
984 if (r < 0) {
985 enter_pgmcheck(cpu, -r);
986 } else {
987 setcc(cpu, r);
990 return 0;
993 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
995 CPUS390XState *env = &cpu->env;
996 int rc = 0;
997 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
999 cpu_synchronize_state(CPU(cpu));
1001 switch (ipa1) {
1002 case PRIV_B2_XSCH:
1003 ioinst_handle_xsch(cpu, env->regs[1]);
1004 break;
1005 case PRIV_B2_CSCH:
1006 ioinst_handle_csch(cpu, env->regs[1]);
1007 break;
1008 case PRIV_B2_HSCH:
1009 ioinst_handle_hsch(cpu, env->regs[1]);
1010 break;
1011 case PRIV_B2_MSCH:
1012 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1013 break;
1014 case PRIV_B2_SSCH:
1015 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1016 break;
1017 case PRIV_B2_STCRW:
1018 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1019 break;
1020 case PRIV_B2_STSCH:
1021 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1022 break;
1023 case PRIV_B2_TSCH:
1024 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1025 fprintf(stderr, "Spurious tsch intercept\n");
1026 break;
1027 case PRIV_B2_CHSC:
1028 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1029 break;
1030 case PRIV_B2_TPI:
1031 /* This should have been handled by kvm already. */
1032 fprintf(stderr, "Spurious tpi intercept\n");
1033 break;
1034 case PRIV_B2_SCHM:
1035 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1036 run->s390_sieic.ipb);
1037 break;
1038 case PRIV_B2_RSCH:
1039 ioinst_handle_rsch(cpu, env->regs[1]);
1040 break;
1041 case PRIV_B2_RCHP:
1042 ioinst_handle_rchp(cpu, env->regs[1]);
1043 break;
1044 case PRIV_B2_STCPS:
1045 /* We do not provide this instruction, it is suppressed. */
1046 break;
1047 case PRIV_B2_SAL:
1048 ioinst_handle_sal(cpu, env->regs[1]);
1049 break;
1050 case PRIV_B2_SIGA:
1051 /* Not provided, set CC = 3 for subchannel not operational */
1052 setcc(cpu, 3);
1053 break;
1054 case PRIV_B2_SCLP_CALL:
1055 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1056 break;
1057 default:
1058 rc = -1;
1059 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1060 break;
1063 return rc;
1066 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1067 uint8_t *ar)
1069 CPUS390XState *env = &cpu->env;
1070 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1071 uint32_t base2 = run->s390_sieic.ipb >> 28;
1072 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1073 ((run->s390_sieic.ipb & 0xff00) << 4);
1075 if (disp2 & 0x80000) {
1076 disp2 += 0xfff00000;
1078 if (ar) {
1079 *ar = base2;
1082 return (base2 ? env->regs[base2] : 0) +
1083 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1086 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1087 uint8_t *ar)
1089 CPUS390XState *env = &cpu->env;
1090 uint32_t base2 = run->s390_sieic.ipb >> 28;
1091 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1092 ((run->s390_sieic.ipb & 0xff00) << 4);
1094 if (disp2 & 0x80000) {
1095 disp2 += 0xfff00000;
1097 if (ar) {
1098 *ar = base2;
1101 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1104 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1106 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1108 return clp_service_call(cpu, r2);
1111 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1113 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1114 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1116 return pcilg_service_call(cpu, r1, r2);
1119 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1121 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1122 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1124 return pcistg_service_call(cpu, r1, r2);
1127 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1129 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1130 uint64_t fiba;
1131 uint8_t ar;
1133 cpu_synchronize_state(CPU(cpu));
1134 fiba = get_base_disp_rxy(cpu, run, &ar);
1136 return stpcifc_service_call(cpu, r1, fiba, ar);
1139 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1141 /* NOOP */
1142 return 0;
1145 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1147 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1148 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1150 return rpcit_service_call(cpu, r1, r2);
1153 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1155 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1156 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1157 uint64_t gaddr;
1158 uint8_t ar;
1160 cpu_synchronize_state(CPU(cpu));
1161 gaddr = get_base_disp_rsy(cpu, run, &ar);
1163 return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1166 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1168 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1169 uint64_t fiba;
1170 uint8_t ar;
1172 cpu_synchronize_state(CPU(cpu));
1173 fiba = get_base_disp_rxy(cpu, run, &ar);
1175 return mpcifc_service_call(cpu, r1, fiba, ar);
1178 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1180 int r = 0;
1182 switch (ipa1) {
1183 case PRIV_B9_CLP:
1184 r = kvm_clp_service_call(cpu, run);
1185 break;
1186 case PRIV_B9_PCISTG:
1187 r = kvm_pcistg_service_call(cpu, run);
1188 break;
1189 case PRIV_B9_PCILG:
1190 r = kvm_pcilg_service_call(cpu, run);
1191 break;
1192 case PRIV_B9_RPCIT:
1193 r = kvm_rpcit_service_call(cpu, run);
1194 break;
1195 case PRIV_B9_EQBS:
1196 /* just inject exception */
1197 r = -1;
1198 break;
1199 default:
1200 r = -1;
1201 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1202 break;
1205 return r;
1208 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1210 int r = 0;
1212 switch (ipbl) {
1213 case PRIV_EB_PCISTB:
1214 r = kvm_pcistb_service_call(cpu, run);
1215 break;
1216 case PRIV_EB_SIC:
1217 r = kvm_sic_service_call(cpu, run);
1218 break;
1219 case PRIV_EB_SQBS:
1220 /* just inject exception */
1221 r = -1;
1222 break;
1223 default:
1224 r = -1;
1225 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1226 break;
1229 return r;
1232 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1234 int r = 0;
1236 switch (ipbl) {
1237 case PRIV_E3_MPCIFC:
1238 r = kvm_mpcifc_service_call(cpu, run);
1239 break;
1240 case PRIV_E3_STPCIFC:
1241 r = kvm_stpcifc_service_call(cpu, run);
1242 break;
1243 default:
1244 r = -1;
1245 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1246 break;
1249 return r;
1252 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1254 CPUS390XState *env = &cpu->env;
1255 int ret;
1257 cpu_synchronize_state(CPU(cpu));
1258 ret = s390_virtio_hypercall(env);
1259 if (ret == -EINVAL) {
1260 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1261 return 0;
1264 return ret;
1267 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1269 uint64_t r1, r3;
1270 int rc;
1272 cpu_synchronize_state(CPU(cpu));
1273 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1274 r3 = run->s390_sieic.ipa & 0x000f;
1275 rc = handle_diag_288(&cpu->env, r1, r3);
1276 if (rc) {
1277 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1281 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1283 uint64_t r1, r3;
1285 cpu_synchronize_state(CPU(cpu));
1286 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1287 r3 = run->s390_sieic.ipa & 0x000f;
1288 handle_diag_308(&cpu->env, r1, r3);
1291 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1293 CPUS390XState *env = &cpu->env;
1294 unsigned long pc;
1296 cpu_synchronize_state(CPU(cpu));
1298 pc = env->psw.addr - 4;
1299 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1300 env->psw.addr = pc;
1301 return EXCP_DEBUG;
1304 return -ENOENT;
1307 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1309 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1311 int r = 0;
1312 uint16_t func_code;
1315 * For any diagnose call we support, bits 48-63 of the resulting
1316 * address specify the function code; the remainder is ignored.
1318 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1319 switch (func_code) {
1320 case DIAG_TIMEREVENT:
1321 kvm_handle_diag_288(cpu, run);
1322 break;
1323 case DIAG_IPL:
1324 kvm_handle_diag_308(cpu, run);
1325 break;
1326 case DIAG_KVM_HYPERCALL:
1327 r = handle_hypercall(cpu, run);
1328 break;
1329 case DIAG_KVM_BREAKPOINT:
1330 r = handle_sw_breakpoint(cpu, run);
1331 break;
1332 default:
1333 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1334 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1335 break;
1338 return r;
1341 typedef struct SigpInfo {
1342 S390CPU *cpu;
1343 uint64_t param;
1344 int cc;
1345 uint64_t *status_reg;
1346 } SigpInfo;
1348 static void set_sigp_status(SigpInfo *si, uint64_t status)
1350 *si->status_reg &= 0xffffffff00000000ULL;
1351 *si->status_reg |= status;
1352 si->cc = SIGP_CC_STATUS_STORED;
1355 static void sigp_start(void *arg)
1357 SigpInfo *si = arg;
1359 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1360 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1361 return;
1364 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1365 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1368 static void sigp_stop(void *arg)
1370 SigpInfo *si = arg;
1371 struct kvm_s390_irq irq = {
1372 .type = KVM_S390_SIGP_STOP,
1375 if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1376 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1377 return;
1380 /* disabled wait - sleeping in user space */
1381 if (CPU(si->cpu)->halted) {
1382 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1383 } else {
1384 /* execute the stop function */
1385 si->cpu->env.sigp_order = SIGP_STOP;
1386 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1388 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1391 #define ADTL_SAVE_AREA_SIZE 1024
1392 static int kvm_s390_store_adtl_status(S390CPU *cpu, hwaddr addr)
1394 void *mem;
1395 hwaddr len = ADTL_SAVE_AREA_SIZE;
1397 mem = cpu_physical_memory_map(addr, &len, 1);
1398 if (!mem) {
1399 return -EFAULT;
1401 if (len != ADTL_SAVE_AREA_SIZE) {
1402 cpu_physical_memory_unmap(mem, len, 1, 0);
1403 return -EFAULT;
1406 memcpy(mem, &cpu->env.vregs, 512);
1408 cpu_physical_memory_unmap(mem, len, 1, len);
1410 return 0;
1413 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1414 #define SAVE_AREA_SIZE 512
1415 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1417 static const uint8_t ar_id = 1;
1418 uint64_t ckc = cpu->env.ckc >> 8;
1419 void *mem;
1420 int i;
1421 hwaddr len = SAVE_AREA_SIZE;
1423 mem = cpu_physical_memory_map(addr, &len, 1);
1424 if (!mem) {
1425 return -EFAULT;
1427 if (len != SAVE_AREA_SIZE) {
1428 cpu_physical_memory_unmap(mem, len, 1, 0);
1429 return -EFAULT;
1432 if (store_arch) {
1433 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1435 for (i = 0; i < 16; ++i) {
1436 *((uint64_t *)mem + i) = get_freg(&cpu->env, i)->ll;
1438 memcpy(mem + 128, &cpu->env.regs, 128);
1439 memcpy(mem + 256, &cpu->env.psw, 16);
1440 memcpy(mem + 280, &cpu->env.psa, 4);
1441 memcpy(mem + 284, &cpu->env.fpc, 4);
1442 memcpy(mem + 292, &cpu->env.todpr, 4);
1443 memcpy(mem + 296, &cpu->env.cputm, 8);
1444 memcpy(mem + 304, &ckc, 8);
1445 memcpy(mem + 320, &cpu->env.aregs, 64);
1446 memcpy(mem + 384, &cpu->env.cregs, 128);
1448 cpu_physical_memory_unmap(mem, len, 1, len);
1450 return 0;
1453 static void sigp_stop_and_store_status(void *arg)
1455 SigpInfo *si = arg;
1456 struct kvm_s390_irq irq = {
1457 .type = KVM_S390_SIGP_STOP,
1460 /* disabled wait - sleeping in user space */
1461 if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1462 CPU(si->cpu)->halted) {
1463 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1466 switch (s390_cpu_get_state(si->cpu)) {
1467 case CPU_STATE_OPERATING:
1468 si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1469 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1470 /* store will be performed when handling the stop intercept */
1471 break;
1472 case CPU_STATE_STOPPED:
1473 /* already stopped, just store the status */
1474 cpu_synchronize_state(CPU(si->cpu));
1475 kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1476 break;
1478 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1481 static void sigp_store_status_at_address(void *arg)
1483 SigpInfo *si = arg;
1484 uint32_t address = si->param & 0x7ffffe00u;
1486 /* cpu has to be stopped */
1487 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1488 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1489 return;
1492 cpu_synchronize_state(CPU(si->cpu));
1494 if (kvm_s390_store_status(si->cpu, address, false)) {
1495 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1496 return;
1498 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1501 static void sigp_store_adtl_status(void *arg)
1503 SigpInfo *si = arg;
1505 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
1506 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1507 return;
1510 /* cpu has to be stopped */
1511 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1512 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1513 return;
1516 /* parameter must be aligned to 1024-byte boundary */
1517 if (si->param & 0x3ff) {
1518 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1519 return;
1522 cpu_synchronize_state(CPU(si->cpu));
1524 if (kvm_s390_store_adtl_status(si->cpu, si->param)) {
1525 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1526 return;
1528 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1531 static void sigp_restart(void *arg)
1533 SigpInfo *si = arg;
1534 struct kvm_s390_irq irq = {
1535 .type = KVM_S390_RESTART,
1538 switch (s390_cpu_get_state(si->cpu)) {
1539 case CPU_STATE_STOPPED:
1540 /* the restart irq has to be delivered prior to any other pending irq */
1541 cpu_synchronize_state(CPU(si->cpu));
1542 do_restart_interrupt(&si->cpu->env);
1543 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1544 break;
1545 case CPU_STATE_OPERATING:
1546 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1547 break;
1549 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1552 int kvm_s390_cpu_restart(S390CPU *cpu)
1554 SigpInfo si = {
1555 .cpu = cpu,
1558 run_on_cpu(CPU(cpu), sigp_restart, &si);
1559 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1560 return 0;
1563 static void sigp_initial_cpu_reset(void *arg)
1565 SigpInfo *si = arg;
1566 CPUState *cs = CPU(si->cpu);
1567 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1569 cpu_synchronize_state(cs);
1570 scc->initial_cpu_reset(cs);
1571 cpu_synchronize_post_reset(cs);
1572 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1575 static void sigp_cpu_reset(void *arg)
1577 SigpInfo *si = arg;
1578 CPUState *cs = CPU(si->cpu);
1579 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1581 cpu_synchronize_state(cs);
1582 scc->cpu_reset(cs);
1583 cpu_synchronize_post_reset(cs);
1584 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1587 static void sigp_set_prefix(void *arg)
1589 SigpInfo *si = arg;
1590 uint32_t addr = si->param & 0x7fffe000u;
1592 cpu_synchronize_state(CPU(si->cpu));
1594 if (!address_space_access_valid(&address_space_memory, addr,
1595 sizeof(struct LowCore), false)) {
1596 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1597 return;
1600 /* cpu has to be stopped */
1601 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1602 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1603 return;
1606 si->cpu->env.psa = addr;
1607 cpu_synchronize_post_init(CPU(si->cpu));
1608 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1611 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1612 uint64_t param, uint64_t *status_reg)
1614 SigpInfo si = {
1615 .cpu = dst_cpu,
1616 .param = param,
1617 .status_reg = status_reg,
1620 /* cpu available? */
1621 if (dst_cpu == NULL) {
1622 return SIGP_CC_NOT_OPERATIONAL;
1625 /* only resets can break pending orders */
1626 if (dst_cpu->env.sigp_order != 0 &&
1627 order != SIGP_CPU_RESET &&
1628 order != SIGP_INITIAL_CPU_RESET) {
1629 return SIGP_CC_BUSY;
1632 switch (order) {
1633 case SIGP_START:
1634 run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1635 break;
1636 case SIGP_STOP:
1637 run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1638 break;
1639 case SIGP_RESTART:
1640 run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1641 break;
1642 case SIGP_STOP_STORE_STATUS:
1643 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1644 break;
1645 case SIGP_STORE_STATUS_ADDR:
1646 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1647 break;
1648 case SIGP_STORE_ADTL_STATUS:
1649 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, &si);
1650 break;
1651 case SIGP_SET_PREFIX:
1652 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1653 break;
1654 case SIGP_INITIAL_CPU_RESET:
1655 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1656 break;
1657 case SIGP_CPU_RESET:
1658 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1659 break;
1660 default:
1661 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1662 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1665 return si.cc;
1668 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1669 uint64_t *status_reg)
1671 CPUState *cur_cs;
1672 S390CPU *cur_cpu;
1674 /* due to the BQL, we are the only active cpu */
1675 CPU_FOREACH(cur_cs) {
1676 cur_cpu = S390_CPU(cur_cs);
1677 if (cur_cpu->env.sigp_order != 0) {
1678 return SIGP_CC_BUSY;
1680 cpu_synchronize_state(cur_cs);
1681 /* all but the current one have to be stopped */
1682 if (cur_cpu != cpu &&
1683 s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1684 *status_reg &= 0xffffffff00000000ULL;
1685 *status_reg |= SIGP_STAT_INCORRECT_STATE;
1686 return SIGP_CC_STATUS_STORED;
1690 switch (param & 0xff) {
1691 case SIGP_MODE_ESA_S390:
1692 /* not supported */
1693 return SIGP_CC_NOT_OPERATIONAL;
1694 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1695 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1696 CPU_FOREACH(cur_cs) {
1697 cur_cpu = S390_CPU(cur_cs);
1698 cur_cpu->env.pfault_token = -1UL;
1700 break;
1701 default:
1702 *status_reg &= 0xffffffff00000000ULL;
1703 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1704 return SIGP_CC_STATUS_STORED;
1707 return SIGP_CC_ORDER_CODE_ACCEPTED;
1710 #define SIGP_ORDER_MASK 0x000000ff
1712 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1714 CPUS390XState *env = &cpu->env;
1715 const uint8_t r1 = ipa1 >> 4;
1716 const uint8_t r3 = ipa1 & 0x0f;
1717 int ret;
1718 uint8_t order;
1719 uint64_t *status_reg;
1720 uint64_t param;
1721 S390CPU *dst_cpu = NULL;
1723 cpu_synchronize_state(CPU(cpu));
1725 /* get order code */
1726 order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1727 & SIGP_ORDER_MASK;
1728 status_reg = &env->regs[r1];
1729 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1731 switch (order) {
1732 case SIGP_SET_ARCH:
1733 ret = sigp_set_architecture(cpu, param, status_reg);
1734 break;
1735 default:
1736 /* all other sigp orders target a single vcpu */
1737 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1738 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1741 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1742 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1744 if (ret >= 0) {
1745 setcc(cpu, ret);
1746 return 0;
1749 return ret;
1752 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1754 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1755 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1756 int r = -1;
1758 DPRINTF("handle_instruction 0x%x 0x%x\n",
1759 run->s390_sieic.ipa, run->s390_sieic.ipb);
1760 switch (ipa0) {
1761 case IPA0_B2:
1762 r = handle_b2(cpu, run, ipa1);
1763 break;
1764 case IPA0_B9:
1765 r = handle_b9(cpu, run, ipa1);
1766 break;
1767 case IPA0_EB:
1768 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1769 break;
1770 case IPA0_E3:
1771 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1772 break;
1773 case IPA0_DIAG:
1774 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1775 break;
1776 case IPA0_SIGP:
1777 r = handle_sigp(cpu, run, ipa1);
1778 break;
1781 if (r < 0) {
1782 r = 0;
1783 enter_pgmcheck(cpu, 0x0001);
1786 return r;
1789 static bool is_special_wait_psw(CPUState *cs)
1791 /* signal quiesce */
1792 return cs->kvm_run->psw_addr == 0xfffUL;
1795 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1797 CPUState *cs = CPU(cpu);
1799 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1800 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1801 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1802 s390_cpu_halt(cpu);
1803 qemu_system_guest_panicked();
1806 static int handle_intercept(S390CPU *cpu)
1808 CPUState *cs = CPU(cpu);
1809 struct kvm_run *run = cs->kvm_run;
1810 int icpt_code = run->s390_sieic.icptcode;
1811 int r = 0;
1813 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1814 (long)cs->kvm_run->psw_addr);
1815 switch (icpt_code) {
1816 case ICPT_INSTRUCTION:
1817 r = handle_instruction(cpu, run);
1818 break;
1819 case ICPT_PROGRAM:
1820 unmanageable_intercept(cpu, "program interrupt",
1821 offsetof(LowCore, program_new_psw));
1822 r = EXCP_HALTED;
1823 break;
1824 case ICPT_EXT_INT:
1825 unmanageable_intercept(cpu, "external interrupt",
1826 offsetof(LowCore, external_new_psw));
1827 r = EXCP_HALTED;
1828 break;
1829 case ICPT_WAITPSW:
1830 /* disabled wait, since enabled wait is handled in kernel */
1831 cpu_synchronize_state(cs);
1832 if (s390_cpu_halt(cpu) == 0) {
1833 if (is_special_wait_psw(cs)) {
1834 qemu_system_shutdown_request();
1835 } else {
1836 qemu_system_guest_panicked();
1839 r = EXCP_HALTED;
1840 break;
1841 case ICPT_CPU_STOP:
1842 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1843 qemu_system_shutdown_request();
1845 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1846 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1847 true);
1849 cpu->env.sigp_order = 0;
1850 r = EXCP_HALTED;
1851 break;
1852 case ICPT_SOFT_INTERCEPT:
1853 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1854 exit(1);
1855 break;
1856 case ICPT_IO:
1857 fprintf(stderr, "KVM unimplemented icpt IO\n");
1858 exit(1);
1859 break;
1860 default:
1861 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1862 exit(1);
1863 break;
1866 return r;
1869 static int handle_tsch(S390CPU *cpu)
1871 CPUState *cs = CPU(cpu);
1872 struct kvm_run *run = cs->kvm_run;
1873 int ret;
1875 cpu_synchronize_state(cs);
1877 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1878 if (ret < 0) {
1880 * Failure.
1881 * If an I/O interrupt had been dequeued, we have to reinject it.
1883 if (run->s390_tsch.dequeued) {
1884 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1885 run->s390_tsch.subchannel_nr,
1886 run->s390_tsch.io_int_parm,
1887 run->s390_tsch.io_int_word);
1889 ret = 0;
1891 return ret;
1894 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1896 struct sysib_322 sysib;
1897 int del;
1899 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1900 return;
1902 /* Shift the stack of Extended Names to prepare for our own data */
1903 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1904 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1905 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1906 * assumed it's not capable of managing Extended Names for lower levels.
1908 for (del = 1; del < sysib.count; del++) {
1909 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1910 break;
1913 if (del < sysib.count) {
1914 memset(sysib.ext_names[del], 0,
1915 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1917 /* Insert short machine name in EBCDIC, padded with blanks */
1918 if (qemu_name) {
1919 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1920 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1921 strlen(qemu_name)));
1923 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1924 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1925 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1926 * considered by s390 as not capable of providing any Extended Name.
1927 * Therefore if no name was specified on qemu invocation, we go with the
1928 * same "KVMguest" default, which KVM has filled into short name field.
1930 if (qemu_name) {
1931 strncpy((char *)sysib.ext_names[0], qemu_name,
1932 sizeof(sysib.ext_names[0]));
1933 } else {
1934 strcpy((char *)sysib.ext_names[0], "KVMguest");
1936 /* Insert UUID */
1937 memcpy(sysib.vm[0].uuid, qemu_uuid, sizeof(sysib.vm[0].uuid));
1939 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1942 static int handle_stsi(S390CPU *cpu)
1944 CPUState *cs = CPU(cpu);
1945 struct kvm_run *run = cs->kvm_run;
1947 switch (run->s390_stsi.fc) {
1948 case 3:
1949 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1950 return 0;
1952 /* Only sysib 3.2.2 needs post-handling for now. */
1953 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1954 return 0;
1955 default:
1956 return 0;
1960 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1962 CPUState *cs = CPU(cpu);
1963 struct kvm_run *run = cs->kvm_run;
1965 int ret = 0;
1966 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1968 switch (arch_info->type) {
1969 case KVM_HW_WP_WRITE:
1970 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1971 cs->watchpoint_hit = &hw_watchpoint;
1972 hw_watchpoint.vaddr = arch_info->addr;
1973 hw_watchpoint.flags = BP_MEM_WRITE;
1974 ret = EXCP_DEBUG;
1976 break;
1977 case KVM_HW_BP:
1978 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1979 ret = EXCP_DEBUG;
1981 break;
1982 case KVM_SINGLESTEP:
1983 if (cs->singlestep_enabled) {
1984 ret = EXCP_DEBUG;
1986 break;
1987 default:
1988 ret = -ENOSYS;
1991 return ret;
1994 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1996 S390CPU *cpu = S390_CPU(cs);
1997 int ret = 0;
1999 qemu_mutex_lock_iothread();
2001 switch (run->exit_reason) {
2002 case KVM_EXIT_S390_SIEIC:
2003 ret = handle_intercept(cpu);
2004 break;
2005 case KVM_EXIT_S390_RESET:
2006 s390_reipl_request();
2007 break;
2008 case KVM_EXIT_S390_TSCH:
2009 ret = handle_tsch(cpu);
2010 break;
2011 case KVM_EXIT_S390_STSI:
2012 ret = handle_stsi(cpu);
2013 break;
2014 case KVM_EXIT_DEBUG:
2015 ret = kvm_arch_handle_debug_exit(cpu);
2016 break;
2017 default:
2018 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2019 break;
2021 qemu_mutex_unlock_iothread();
2023 if (ret == 0) {
2024 ret = EXCP_INTERRUPT;
2026 return ret;
2029 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2031 return true;
2034 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
2036 return 1;
2039 int kvm_arch_on_sigbus(int code, void *addr)
2041 return 1;
2044 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2045 uint16_t subchannel_nr, uint32_t io_int_parm,
2046 uint32_t io_int_word)
2048 struct kvm_s390_irq irq = {
2049 .u.io.subchannel_id = subchannel_id,
2050 .u.io.subchannel_nr = subchannel_nr,
2051 .u.io.io_int_parm = io_int_parm,
2052 .u.io.io_int_word = io_int_word,
2055 if (io_int_word & IO_INT_WORD_AI) {
2056 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2057 } else {
2058 irq.type = ((subchannel_id & 0xff00) << 24) |
2059 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
2061 kvm_s390_floating_interrupt(&irq);
2064 static uint64_t build_channel_report_mcic(void)
2066 uint64_t mcic;
2068 /* subclass: indicate channel report pending */
2069 mcic = MCIC_SC_CP |
2070 /* subclass modifiers: none */
2071 /* storage errors: none */
2072 /* validity bits: no damage */
2073 MCIC_VB_WP | MCIC_VB_MS | MCIC_VB_PM | MCIC_VB_IA | MCIC_VB_FP |
2074 MCIC_VB_GR | MCIC_VB_CR | MCIC_VB_ST | MCIC_VB_AR | MCIC_VB_PR |
2075 MCIC_VB_FC | MCIC_VB_CT | MCIC_VB_CC;
2076 if (kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
2077 mcic |= MCIC_VB_VR;
2079 return mcic;
2082 void kvm_s390_crw_mchk(void)
2084 struct kvm_s390_irq irq = {
2085 .type = KVM_S390_MCHK,
2086 .u.mchk.cr14 = 1 << 28,
2087 .u.mchk.mcic = build_channel_report_mcic(),
2089 kvm_s390_floating_interrupt(&irq);
2092 void kvm_s390_enable_css_support(S390CPU *cpu)
2094 int r;
2096 /* Activate host kernel channel subsystem support. */
2097 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2098 assert(r == 0);
2101 void kvm_arch_init_irq_routing(KVMState *s)
2104 * Note that while irqchip capabilities generally imply that cpustates
2105 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2106 * have to override the common code kvm_halt_in_kernel_allowed setting.
2108 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2109 kvm_gsi_routing_allowed = true;
2110 kvm_halt_in_kernel_allowed = false;
2114 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2115 int vq, bool assign)
2117 struct kvm_ioeventfd kick = {
2118 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2119 KVM_IOEVENTFD_FLAG_DATAMATCH,
2120 .fd = event_notifier_get_fd(notifier),
2121 .datamatch = vq,
2122 .addr = sch,
2123 .len = 8,
2125 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2126 return -ENOSYS;
2128 if (!assign) {
2129 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2131 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2134 int kvm_s390_get_memslot_count(KVMState *s)
2136 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
2139 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2141 struct kvm_mp_state mp_state = {};
2142 int ret;
2144 /* the kvm part might not have been initialized yet */
2145 if (CPU(cpu)->kvm_state == NULL) {
2146 return 0;
2149 switch (cpu_state) {
2150 case CPU_STATE_STOPPED:
2151 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2152 break;
2153 case CPU_STATE_CHECK_STOP:
2154 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2155 break;
2156 case CPU_STATE_OPERATING:
2157 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2158 break;
2159 case CPU_STATE_LOAD:
2160 mp_state.mp_state = KVM_MP_STATE_LOAD;
2161 break;
2162 default:
2163 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2164 cpu_state);
2165 exit(1);
2168 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2169 if (ret) {
2170 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2171 strerror(-ret));
2174 return ret;
2177 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2179 struct kvm_s390_irq_state irq_state;
2180 CPUState *cs = CPU(cpu);
2181 int32_t bytes;
2183 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2184 return;
2187 irq_state.buf = (uint64_t) cpu->irqstate;
2188 irq_state.len = VCPU_IRQ_BUF_SIZE;
2190 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2191 if (bytes < 0) {
2192 cpu->irqstate_saved_size = 0;
2193 error_report("Migration of interrupt state failed");
2194 return;
2197 cpu->irqstate_saved_size = bytes;
2200 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2202 CPUState *cs = CPU(cpu);
2203 struct kvm_s390_irq_state irq_state;
2204 int r;
2206 if (cpu->irqstate_saved_size == 0) {
2207 return 0;
2210 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2211 return -ENOSYS;
2214 irq_state.buf = (uint64_t) cpu->irqstate;
2215 irq_state.len = cpu->irqstate_saved_size;
2217 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2218 if (r) {
2219 error_report("Setting interrupt state failed %d", r);
2221 return r;
2224 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2225 uint64_t address, uint32_t data, PCIDevice *dev)
2227 S390PCIBusDevice *pbdev;
2228 uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
2229 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2231 pbdev = s390_pci_find_dev_by_fid(fid);
2232 if (!pbdev) {
2233 DPRINTF("add_msi_route no dev\n");
2234 return -ENODEV;
2237 pbdev->routes.adapter.ind_offset = vec;
2239 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2240 route->flags = 0;
2241 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2242 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2243 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2244 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2245 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2246 return 0;
2249 int kvm_arch_msi_data_to_gsi(uint32_t data)
2251 abort();