s390x/kvm: 2 byte software breakpoint support
[qemu.git] / target-s390x / kvm.c
blob4341d5496957793b5fffea0628e6bd88a4624166
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>
27 #include <linux/kvm.h>
28 #include <asm/ptrace.h>
30 #include "qemu-common.h"
31 #include "cpu.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 "sysemu/device_tree.h"
38 #include "qapi/qmp/qjson.h"
39 #include "exec/gdbstub.h"
40 #include "exec/address-spaces.h"
41 #include "trace.h"
42 #include "qapi-event.h"
43 #include "hw/s390x/s390-pci-inst.h"
44 #include "hw/s390x/s390-pci-bus.h"
45 #include "hw/s390x/ipl.h"
46 #include "hw/s390x/ebcdic.h"
47 #include "exec/memattrs.h"
48 #include "hw/s390x/s390-virtio-ccw.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_OPEREXC 0x2c
113 #define ICPT_IO 0x40
115 #define NR_LOCAL_IRQS 32
117 * Needs to be big enough to contain max_cpus emergency signals
118 * and in addition NR_LOCAL_IRQS interrupts
120 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
121 (max_cpus + NR_LOCAL_IRQS))
123 static CPUWatchpoint hw_watchpoint;
125 * We don't use a list because this structure is also used to transmit the
126 * hardware breakpoints to the kernel.
128 static struct kvm_hw_breakpoint *hw_breakpoints;
129 static int nb_hw_breakpoints;
131 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
132 KVM_CAP_LAST_INFO
135 static int cap_sync_regs;
136 static int cap_async_pf;
137 static int cap_mem_op;
138 static int cap_s390_irq;
139 static int cap_ri;
141 static void *legacy_s390_alloc(size_t size, uint64_t *align);
143 static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
145 struct kvm_device_attr attr = {
146 .group = KVM_S390_VM_MEM_CTRL,
147 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
148 .addr = (uint64_t) memory_limit,
151 return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
154 int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
156 int rc;
158 struct kvm_device_attr attr = {
159 .group = KVM_S390_VM_MEM_CTRL,
160 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
161 .addr = (uint64_t) &new_limit,
164 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_LIMIT_SIZE)) {
165 return 0;
168 rc = kvm_s390_query_mem_limit(s, hw_limit);
169 if (rc) {
170 return rc;
171 } else if (*hw_limit < new_limit) {
172 return -E2BIG;
175 return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
178 void kvm_s390_cmma_reset(void)
180 int rc;
181 struct kvm_device_attr attr = {
182 .group = KVM_S390_VM_MEM_CTRL,
183 .attr = KVM_S390_VM_MEM_CLR_CMMA,
186 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
187 trace_kvm_clear_cmma(rc);
190 static void kvm_s390_enable_cmma(KVMState *s)
192 int rc;
193 struct kvm_device_attr attr = {
194 .group = KVM_S390_VM_MEM_CTRL,
195 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
198 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_ENABLE_CMMA) ||
199 !kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_CLR_CMMA)) {
200 return;
203 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
204 trace_kvm_enable_cmma(rc);
207 static void kvm_s390_set_attr(uint64_t attr)
209 struct kvm_device_attr attribute = {
210 .group = KVM_S390_VM_CRYPTO,
211 .attr = attr,
214 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
216 if (ret) {
217 error_report("Failed to set crypto device attribute %lu: %s",
218 attr, strerror(-ret));
222 static void kvm_s390_init_aes_kw(void)
224 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
226 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
227 NULL)) {
228 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
231 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
232 kvm_s390_set_attr(attr);
236 static void kvm_s390_init_dea_kw(void)
238 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
240 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
241 NULL)) {
242 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
245 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
246 kvm_s390_set_attr(attr);
250 void kvm_s390_crypto_reset(void)
252 kvm_s390_init_aes_kw();
253 kvm_s390_init_dea_kw();
256 int kvm_arch_init(MachineState *ms, KVMState *s)
258 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
259 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
260 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
261 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
263 if (!mem_path) {
264 kvm_s390_enable_cmma(s);
267 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
268 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
269 phys_mem_set_alloc(legacy_s390_alloc);
272 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
273 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
274 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
275 if (ri_allowed()) {
276 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
277 cap_ri = 1;
281 return 0;
284 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
286 return cpu->cpu_index;
289 int kvm_arch_init_vcpu(CPUState *cs)
291 S390CPU *cpu = S390_CPU(cs);
292 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
293 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
294 return 0;
297 void kvm_s390_reset_vcpu(S390CPU *cpu)
299 CPUState *cs = CPU(cpu);
301 /* The initial reset call is needed here to reset in-kernel
302 * vcpu data that we can't access directly from QEMU
303 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
304 * Before this ioctl cpu_synchronize_state() is called in common kvm
305 * code (kvm-all) */
306 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
307 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
311 static int can_sync_regs(CPUState *cs, int regs)
313 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
316 int kvm_arch_put_registers(CPUState *cs, int level)
318 S390CPU *cpu = S390_CPU(cs);
319 CPUS390XState *env = &cpu->env;
320 struct kvm_sregs sregs;
321 struct kvm_regs regs;
322 struct kvm_fpu fpu = {};
323 int r;
324 int i;
326 /* always save the PSW and the GPRS*/
327 cs->kvm_run->psw_addr = env->psw.addr;
328 cs->kvm_run->psw_mask = env->psw.mask;
330 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
331 for (i = 0; i < 16; i++) {
332 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
333 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
335 } else {
336 for (i = 0; i < 16; i++) {
337 regs.gprs[i] = env->regs[i];
339 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
340 if (r < 0) {
341 return r;
345 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
346 for (i = 0; i < 32; i++) {
347 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
348 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
350 cs->kvm_run->s.regs.fpc = env->fpc;
351 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
352 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
353 for (i = 0; i < 16; i++) {
354 cs->kvm_run->s.regs.fprs[i] = get_freg(env, i)->ll;
356 cs->kvm_run->s.regs.fpc = env->fpc;
357 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
358 } else {
359 /* Floating point */
360 for (i = 0; i < 16; i++) {
361 fpu.fprs[i] = get_freg(env, i)->ll;
363 fpu.fpc = env->fpc;
365 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
366 if (r < 0) {
367 return r;
371 /* Do we need to save more than that? */
372 if (level == KVM_PUT_RUNTIME_STATE) {
373 return 0;
376 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
377 cs->kvm_run->s.regs.cputm = env->cputm;
378 cs->kvm_run->s.regs.ckc = env->ckc;
379 cs->kvm_run->s.regs.todpr = env->todpr;
380 cs->kvm_run->s.regs.gbea = env->gbea;
381 cs->kvm_run->s.regs.pp = env->pp;
382 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
383 } else {
385 * These ONE_REGS are not protected by a capability. As they are only
386 * necessary for migration we just trace a possible error, but don't
387 * return with an error return code.
389 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
390 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
391 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
392 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
393 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
396 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
397 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
398 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
401 /* pfault parameters */
402 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
403 cs->kvm_run->s.regs.pft = env->pfault_token;
404 cs->kvm_run->s.regs.pfs = env->pfault_select;
405 cs->kvm_run->s.regs.pfc = env->pfault_compare;
406 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
407 } else if (cap_async_pf) {
408 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
409 if (r < 0) {
410 return r;
412 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
413 if (r < 0) {
414 return r;
416 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
417 if (r < 0) {
418 return r;
422 /* access registers and control registers*/
423 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
424 for (i = 0; i < 16; i++) {
425 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
426 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
428 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
429 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
430 } else {
431 for (i = 0; i < 16; i++) {
432 sregs.acrs[i] = env->aregs[i];
433 sregs.crs[i] = env->cregs[i];
435 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
436 if (r < 0) {
437 return r;
441 /* Finally the prefix */
442 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
443 cs->kvm_run->s.regs.prefix = env->psa;
444 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
445 } else {
446 /* prefix is only supported via sync regs */
448 return 0;
451 int kvm_arch_get_registers(CPUState *cs)
453 S390CPU *cpu = S390_CPU(cs);
454 CPUS390XState *env = &cpu->env;
455 struct kvm_sregs sregs;
456 struct kvm_regs regs;
457 struct kvm_fpu fpu;
458 int i, r;
460 /* get the PSW */
461 env->psw.addr = cs->kvm_run->psw_addr;
462 env->psw.mask = cs->kvm_run->psw_mask;
464 /* the GPRS */
465 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
466 for (i = 0; i < 16; i++) {
467 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
469 } else {
470 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
471 if (r < 0) {
472 return r;
474 for (i = 0; i < 16; i++) {
475 env->regs[i] = regs.gprs[i];
479 /* The ACRS and CRS */
480 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
481 for (i = 0; i < 16; i++) {
482 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
483 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
485 } else {
486 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
487 if (r < 0) {
488 return r;
490 for (i = 0; i < 16; i++) {
491 env->aregs[i] = sregs.acrs[i];
492 env->cregs[i] = sregs.crs[i];
496 /* Floating point and vector registers */
497 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
498 for (i = 0; i < 32; i++) {
499 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
500 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
502 env->fpc = cs->kvm_run->s.regs.fpc;
503 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
504 for (i = 0; i < 16; i++) {
505 get_freg(env, i)->ll = cs->kvm_run->s.regs.fprs[i];
507 env->fpc = cs->kvm_run->s.regs.fpc;
508 } else {
509 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
510 if (r < 0) {
511 return r;
513 for (i = 0; i < 16; i++) {
514 get_freg(env, i)->ll = fpu.fprs[i];
516 env->fpc = fpu.fpc;
519 /* The prefix */
520 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
521 env->psa = cs->kvm_run->s.regs.prefix;
524 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
525 env->cputm = cs->kvm_run->s.regs.cputm;
526 env->ckc = cs->kvm_run->s.regs.ckc;
527 env->todpr = cs->kvm_run->s.regs.todpr;
528 env->gbea = cs->kvm_run->s.regs.gbea;
529 env->pp = cs->kvm_run->s.regs.pp;
530 } else {
532 * These ONE_REGS are not protected by a capability. As they are only
533 * necessary for migration we just trace a possible error, but don't
534 * return with an error return code.
536 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
537 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
538 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
539 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
540 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
543 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
544 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
547 /* pfault parameters */
548 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
549 env->pfault_token = cs->kvm_run->s.regs.pft;
550 env->pfault_select = cs->kvm_run->s.regs.pfs;
551 env->pfault_compare = cs->kvm_run->s.regs.pfc;
552 } else if (cap_async_pf) {
553 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
554 if (r < 0) {
555 return r;
557 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
558 if (r < 0) {
559 return r;
561 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
562 if (r < 0) {
563 return r;
567 return 0;
570 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
572 int r;
573 struct kvm_device_attr attr = {
574 .group = KVM_S390_VM_TOD,
575 .attr = KVM_S390_VM_TOD_LOW,
576 .addr = (uint64_t)tod_low,
579 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
580 if (r) {
581 return r;
584 attr.attr = KVM_S390_VM_TOD_HIGH;
585 attr.addr = (uint64_t)tod_high;
586 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
589 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
591 int r;
593 struct kvm_device_attr attr = {
594 .group = KVM_S390_VM_TOD,
595 .attr = KVM_S390_VM_TOD_LOW,
596 .addr = (uint64_t)tod_low,
599 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
600 if (r) {
601 return r;
604 attr.attr = KVM_S390_VM_TOD_HIGH;
605 attr.addr = (uint64_t)tod_high;
606 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
610 * kvm_s390_mem_op:
611 * @addr: the logical start address in guest memory
612 * @ar: the access register number
613 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
614 * @len: length that should be transferred
615 * @is_write: true = write, false = read
616 * Returns: 0 on success, non-zero if an exception or error occurred
618 * Use KVM ioctl to read/write from/to guest memory. An access exception
619 * is injected into the vCPU in case of translation errors.
621 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
622 int len, bool is_write)
624 struct kvm_s390_mem_op mem_op = {
625 .gaddr = addr,
626 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
627 .size = len,
628 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
629 : KVM_S390_MEMOP_LOGICAL_READ,
630 .buf = (uint64_t)hostbuf,
631 .ar = ar,
633 int ret;
635 if (!cap_mem_op) {
636 return -ENOSYS;
638 if (!hostbuf) {
639 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
642 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
643 if (ret < 0) {
644 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
646 return ret;
650 * Legacy layout for s390:
651 * Older S390 KVM requires the topmost vma of the RAM to be
652 * smaller than an system defined value, which is at least 256GB.
653 * Larger systems have larger values. We put the guest between
654 * the end of data segment (system break) and this value. We
655 * use 32GB as a base to have enough room for the system break
656 * to grow. We also have to use MAP parameters that avoid
657 * read-only mapping of guest pages.
659 static void *legacy_s390_alloc(size_t size, uint64_t *align)
661 void *mem;
663 mem = mmap((void *) 0x800000000ULL, size,
664 PROT_EXEC|PROT_READ|PROT_WRITE,
665 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
666 return mem == MAP_FAILED ? NULL : mem;
669 static uint8_t const *sw_bp_inst;
670 static uint8_t sw_bp_ilen;
672 static void determine_sw_breakpoint_instr(void)
674 /* DIAG 501 is used for sw breakpoints with old kernels */
675 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
676 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
677 static const uint8_t instr_0x0000[] = {0x00, 0x00};
679 if (sw_bp_inst) {
680 return;
682 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
683 sw_bp_inst = diag_501;
684 sw_bp_ilen = sizeof(diag_501);
685 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
686 } else {
687 sw_bp_inst = instr_0x0000;
688 sw_bp_ilen = sizeof(instr_0x0000);
689 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
693 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
695 determine_sw_breakpoint_instr();
697 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
698 sw_bp_ilen, 0) ||
699 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
700 return -EINVAL;
702 return 0;
705 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
707 uint8_t t[MAX_ILEN];
709 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
710 return -EINVAL;
711 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
712 return -EINVAL;
713 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
714 sw_bp_ilen, 1)) {
715 return -EINVAL;
718 return 0;
721 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
722 int len, int type)
724 int n;
726 for (n = 0; n < nb_hw_breakpoints; n++) {
727 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
728 (hw_breakpoints[n].len == len || len == -1)) {
729 return &hw_breakpoints[n];
733 return NULL;
736 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
738 int size;
740 if (find_hw_breakpoint(addr, len, type)) {
741 return -EEXIST;
744 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
746 if (!hw_breakpoints) {
747 nb_hw_breakpoints = 0;
748 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
749 } else {
750 hw_breakpoints =
751 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
754 if (!hw_breakpoints) {
755 nb_hw_breakpoints = 0;
756 return -ENOMEM;
759 hw_breakpoints[nb_hw_breakpoints].addr = addr;
760 hw_breakpoints[nb_hw_breakpoints].len = len;
761 hw_breakpoints[nb_hw_breakpoints].type = type;
763 nb_hw_breakpoints++;
765 return 0;
768 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
769 target_ulong len, int type)
771 switch (type) {
772 case GDB_BREAKPOINT_HW:
773 type = KVM_HW_BP;
774 break;
775 case GDB_WATCHPOINT_WRITE:
776 if (len < 1) {
777 return -EINVAL;
779 type = KVM_HW_WP_WRITE;
780 break;
781 default:
782 return -ENOSYS;
784 return insert_hw_breakpoint(addr, len, type);
787 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
788 target_ulong len, int type)
790 int size;
791 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
793 if (bp == NULL) {
794 return -ENOENT;
797 nb_hw_breakpoints--;
798 if (nb_hw_breakpoints > 0) {
800 * In order to trim the array, move the last element to the position to
801 * be removed - if necessary.
803 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
804 *bp = hw_breakpoints[nb_hw_breakpoints];
806 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
807 hw_breakpoints =
808 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
809 } else {
810 g_free(hw_breakpoints);
811 hw_breakpoints = NULL;
814 return 0;
817 void kvm_arch_remove_all_hw_breakpoints(void)
819 nb_hw_breakpoints = 0;
820 g_free(hw_breakpoints);
821 hw_breakpoints = NULL;
824 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
826 int i;
828 if (nb_hw_breakpoints > 0) {
829 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
830 dbg->arch.hw_bp = hw_breakpoints;
832 for (i = 0; i < nb_hw_breakpoints; ++i) {
833 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
834 hw_breakpoints[i].addr);
836 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
837 } else {
838 dbg->arch.nr_hw_bp = 0;
839 dbg->arch.hw_bp = NULL;
843 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
847 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
849 return MEMTXATTRS_UNSPECIFIED;
852 int kvm_arch_process_async_events(CPUState *cs)
854 return cs->halted;
857 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
858 struct kvm_s390_interrupt *interrupt)
860 int r = 0;
862 interrupt->type = irq->type;
863 switch (irq->type) {
864 case KVM_S390_INT_VIRTIO:
865 interrupt->parm = irq->u.ext.ext_params;
866 /* fall through */
867 case KVM_S390_INT_PFAULT_INIT:
868 case KVM_S390_INT_PFAULT_DONE:
869 interrupt->parm64 = irq->u.ext.ext_params2;
870 break;
871 case KVM_S390_PROGRAM_INT:
872 interrupt->parm = irq->u.pgm.code;
873 break;
874 case KVM_S390_SIGP_SET_PREFIX:
875 interrupt->parm = irq->u.prefix.address;
876 break;
877 case KVM_S390_INT_SERVICE:
878 interrupt->parm = irq->u.ext.ext_params;
879 break;
880 case KVM_S390_MCHK:
881 interrupt->parm = irq->u.mchk.cr14;
882 interrupt->parm64 = irq->u.mchk.mcic;
883 break;
884 case KVM_S390_INT_EXTERNAL_CALL:
885 interrupt->parm = irq->u.extcall.code;
886 break;
887 case KVM_S390_INT_EMERGENCY:
888 interrupt->parm = irq->u.emerg.code;
889 break;
890 case KVM_S390_SIGP_STOP:
891 case KVM_S390_RESTART:
892 break; /* These types have no parameters */
893 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
894 interrupt->parm = irq->u.io.subchannel_id << 16;
895 interrupt->parm |= irq->u.io.subchannel_nr;
896 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
897 interrupt->parm64 |= irq->u.io.io_int_word;
898 break;
899 default:
900 r = -EINVAL;
901 break;
903 return r;
906 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
908 struct kvm_s390_interrupt kvmint = {};
909 int r;
911 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
912 if (r < 0) {
913 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
914 exit(1);
917 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
918 if (r < 0) {
919 fprintf(stderr, "KVM failed to inject interrupt\n");
920 exit(1);
924 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
926 CPUState *cs = CPU(cpu);
927 int r;
929 if (cap_s390_irq) {
930 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
931 if (!r) {
932 return;
934 error_report("KVM failed to inject interrupt %llx", irq->type);
935 exit(1);
938 inject_vcpu_irq_legacy(cs, irq);
941 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
943 struct kvm_s390_interrupt kvmint = {};
944 int r;
946 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
947 if (r < 0) {
948 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
949 exit(1);
952 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
953 if (r < 0) {
954 fprintf(stderr, "KVM failed to inject interrupt\n");
955 exit(1);
959 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
961 static bool use_flic = true;
962 int r;
964 if (use_flic) {
965 r = kvm_s390_inject_flic(irq);
966 if (r == -ENOSYS) {
967 use_flic = false;
969 if (!r) {
970 return;
973 __kvm_s390_floating_interrupt(irq);
976 void kvm_s390_service_interrupt(uint32_t parm)
978 struct kvm_s390_irq irq = {
979 .type = KVM_S390_INT_SERVICE,
980 .u.ext.ext_params = parm,
983 kvm_s390_floating_interrupt(&irq);
986 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
988 struct kvm_s390_irq irq = {
989 .type = KVM_S390_PROGRAM_INT,
990 .u.pgm.code = code,
993 kvm_s390_vcpu_interrupt(cpu, &irq);
996 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
998 struct kvm_s390_irq irq = {
999 .type = KVM_S390_PROGRAM_INT,
1000 .u.pgm.code = code,
1001 .u.pgm.trans_exc_code = te_code,
1002 .u.pgm.exc_access_id = te_code & 3,
1005 kvm_s390_vcpu_interrupt(cpu, &irq);
1008 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1009 uint16_t ipbh0)
1011 CPUS390XState *env = &cpu->env;
1012 uint64_t sccb;
1013 uint32_t code;
1014 int r = 0;
1016 cpu_synchronize_state(CPU(cpu));
1017 sccb = env->regs[ipbh0 & 0xf];
1018 code = env->regs[(ipbh0 & 0xf0) >> 4];
1020 r = sclp_service_call(env, sccb, code);
1021 if (r < 0) {
1022 enter_pgmcheck(cpu, -r);
1023 } else {
1024 setcc(cpu, r);
1027 return 0;
1030 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1032 CPUS390XState *env = &cpu->env;
1033 int rc = 0;
1034 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1036 cpu_synchronize_state(CPU(cpu));
1038 switch (ipa1) {
1039 case PRIV_B2_XSCH:
1040 ioinst_handle_xsch(cpu, env->regs[1]);
1041 break;
1042 case PRIV_B2_CSCH:
1043 ioinst_handle_csch(cpu, env->regs[1]);
1044 break;
1045 case PRIV_B2_HSCH:
1046 ioinst_handle_hsch(cpu, env->regs[1]);
1047 break;
1048 case PRIV_B2_MSCH:
1049 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1050 break;
1051 case PRIV_B2_SSCH:
1052 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1053 break;
1054 case PRIV_B2_STCRW:
1055 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1056 break;
1057 case PRIV_B2_STSCH:
1058 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1059 break;
1060 case PRIV_B2_TSCH:
1061 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1062 fprintf(stderr, "Spurious tsch intercept\n");
1063 break;
1064 case PRIV_B2_CHSC:
1065 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1066 break;
1067 case PRIV_B2_TPI:
1068 /* This should have been handled by kvm already. */
1069 fprintf(stderr, "Spurious tpi intercept\n");
1070 break;
1071 case PRIV_B2_SCHM:
1072 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1073 run->s390_sieic.ipb);
1074 break;
1075 case PRIV_B2_RSCH:
1076 ioinst_handle_rsch(cpu, env->regs[1]);
1077 break;
1078 case PRIV_B2_RCHP:
1079 ioinst_handle_rchp(cpu, env->regs[1]);
1080 break;
1081 case PRIV_B2_STCPS:
1082 /* We do not provide this instruction, it is suppressed. */
1083 break;
1084 case PRIV_B2_SAL:
1085 ioinst_handle_sal(cpu, env->regs[1]);
1086 break;
1087 case PRIV_B2_SIGA:
1088 /* Not provided, set CC = 3 for subchannel not operational */
1089 setcc(cpu, 3);
1090 break;
1091 case PRIV_B2_SCLP_CALL:
1092 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1093 break;
1094 default:
1095 rc = -1;
1096 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1097 break;
1100 return rc;
1103 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1104 uint8_t *ar)
1106 CPUS390XState *env = &cpu->env;
1107 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1108 uint32_t base2 = run->s390_sieic.ipb >> 28;
1109 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1110 ((run->s390_sieic.ipb & 0xff00) << 4);
1112 if (disp2 & 0x80000) {
1113 disp2 += 0xfff00000;
1115 if (ar) {
1116 *ar = base2;
1119 return (base2 ? env->regs[base2] : 0) +
1120 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1123 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1124 uint8_t *ar)
1126 CPUS390XState *env = &cpu->env;
1127 uint32_t base2 = run->s390_sieic.ipb >> 28;
1128 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1129 ((run->s390_sieic.ipb & 0xff00) << 4);
1131 if (disp2 & 0x80000) {
1132 disp2 += 0xfff00000;
1134 if (ar) {
1135 *ar = base2;
1138 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1141 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1143 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1145 return clp_service_call(cpu, r2);
1148 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1150 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1151 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1153 return pcilg_service_call(cpu, r1, r2);
1156 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1158 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1159 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1161 return pcistg_service_call(cpu, r1, r2);
1164 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1166 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1167 uint64_t fiba;
1168 uint8_t ar;
1170 cpu_synchronize_state(CPU(cpu));
1171 fiba = get_base_disp_rxy(cpu, run, &ar);
1173 return stpcifc_service_call(cpu, r1, fiba, ar);
1176 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1178 /* NOOP */
1179 return 0;
1182 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1184 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1185 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1187 return rpcit_service_call(cpu, r1, r2);
1190 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1192 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1193 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1194 uint64_t gaddr;
1195 uint8_t ar;
1197 cpu_synchronize_state(CPU(cpu));
1198 gaddr = get_base_disp_rsy(cpu, run, &ar);
1200 return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1203 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1205 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1206 uint64_t fiba;
1207 uint8_t ar;
1209 cpu_synchronize_state(CPU(cpu));
1210 fiba = get_base_disp_rxy(cpu, run, &ar);
1212 return mpcifc_service_call(cpu, r1, fiba, ar);
1215 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1217 int r = 0;
1219 switch (ipa1) {
1220 case PRIV_B9_CLP:
1221 r = kvm_clp_service_call(cpu, run);
1222 break;
1223 case PRIV_B9_PCISTG:
1224 r = kvm_pcistg_service_call(cpu, run);
1225 break;
1226 case PRIV_B9_PCILG:
1227 r = kvm_pcilg_service_call(cpu, run);
1228 break;
1229 case PRIV_B9_RPCIT:
1230 r = kvm_rpcit_service_call(cpu, run);
1231 break;
1232 case PRIV_B9_EQBS:
1233 /* just inject exception */
1234 r = -1;
1235 break;
1236 default:
1237 r = -1;
1238 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1239 break;
1242 return r;
1245 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1247 int r = 0;
1249 switch (ipbl) {
1250 case PRIV_EB_PCISTB:
1251 r = kvm_pcistb_service_call(cpu, run);
1252 break;
1253 case PRIV_EB_SIC:
1254 r = kvm_sic_service_call(cpu, run);
1255 break;
1256 case PRIV_EB_SQBS:
1257 /* just inject exception */
1258 r = -1;
1259 break;
1260 default:
1261 r = -1;
1262 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1263 break;
1266 return r;
1269 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1271 int r = 0;
1273 switch (ipbl) {
1274 case PRIV_E3_MPCIFC:
1275 r = kvm_mpcifc_service_call(cpu, run);
1276 break;
1277 case PRIV_E3_STPCIFC:
1278 r = kvm_stpcifc_service_call(cpu, run);
1279 break;
1280 default:
1281 r = -1;
1282 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1283 break;
1286 return r;
1289 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1291 CPUS390XState *env = &cpu->env;
1292 int ret;
1294 cpu_synchronize_state(CPU(cpu));
1295 ret = s390_virtio_hypercall(env);
1296 if (ret == -EINVAL) {
1297 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1298 return 0;
1301 return ret;
1304 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1306 uint64_t r1, r3;
1307 int rc;
1309 cpu_synchronize_state(CPU(cpu));
1310 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1311 r3 = run->s390_sieic.ipa & 0x000f;
1312 rc = handle_diag_288(&cpu->env, r1, r3);
1313 if (rc) {
1314 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1318 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1320 uint64_t r1, r3;
1322 cpu_synchronize_state(CPU(cpu));
1323 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1324 r3 = run->s390_sieic.ipa & 0x000f;
1325 handle_diag_308(&cpu->env, r1, r3);
1328 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1330 CPUS390XState *env = &cpu->env;
1331 unsigned long pc;
1333 cpu_synchronize_state(CPU(cpu));
1335 pc = env->psw.addr - sw_bp_ilen;
1336 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1337 env->psw.addr = pc;
1338 return EXCP_DEBUG;
1341 return -ENOENT;
1344 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1346 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1348 int r = 0;
1349 uint16_t func_code;
1352 * For any diagnose call we support, bits 48-63 of the resulting
1353 * address specify the function code; the remainder is ignored.
1355 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1356 switch (func_code) {
1357 case DIAG_TIMEREVENT:
1358 kvm_handle_diag_288(cpu, run);
1359 break;
1360 case DIAG_IPL:
1361 kvm_handle_diag_308(cpu, run);
1362 break;
1363 case DIAG_KVM_HYPERCALL:
1364 r = handle_hypercall(cpu, run);
1365 break;
1366 case DIAG_KVM_BREAKPOINT:
1367 r = handle_sw_breakpoint(cpu, run);
1368 break;
1369 default:
1370 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1371 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1372 break;
1375 return r;
1378 typedef struct SigpInfo {
1379 S390CPU *cpu;
1380 uint64_t param;
1381 int cc;
1382 uint64_t *status_reg;
1383 } SigpInfo;
1385 static void set_sigp_status(SigpInfo *si, uint64_t status)
1387 *si->status_reg &= 0xffffffff00000000ULL;
1388 *si->status_reg |= status;
1389 si->cc = SIGP_CC_STATUS_STORED;
1392 static void sigp_start(void *arg)
1394 SigpInfo *si = arg;
1396 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1397 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1398 return;
1401 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1402 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1405 static void sigp_stop(void *arg)
1407 SigpInfo *si = arg;
1408 struct kvm_s390_irq irq = {
1409 .type = KVM_S390_SIGP_STOP,
1412 if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1413 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1414 return;
1417 /* disabled wait - sleeping in user space */
1418 if (CPU(si->cpu)->halted) {
1419 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1420 } else {
1421 /* execute the stop function */
1422 si->cpu->env.sigp_order = SIGP_STOP;
1423 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1425 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1428 #define ADTL_SAVE_AREA_SIZE 1024
1429 static int kvm_s390_store_adtl_status(S390CPU *cpu, hwaddr addr)
1431 void *mem;
1432 hwaddr len = ADTL_SAVE_AREA_SIZE;
1434 mem = cpu_physical_memory_map(addr, &len, 1);
1435 if (!mem) {
1436 return -EFAULT;
1438 if (len != ADTL_SAVE_AREA_SIZE) {
1439 cpu_physical_memory_unmap(mem, len, 1, 0);
1440 return -EFAULT;
1443 memcpy(mem, &cpu->env.vregs, 512);
1445 cpu_physical_memory_unmap(mem, len, 1, len);
1447 return 0;
1450 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1451 #define SAVE_AREA_SIZE 512
1452 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1454 static const uint8_t ar_id = 1;
1455 uint64_t ckc = cpu->env.ckc >> 8;
1456 void *mem;
1457 int i;
1458 hwaddr len = SAVE_AREA_SIZE;
1460 mem = cpu_physical_memory_map(addr, &len, 1);
1461 if (!mem) {
1462 return -EFAULT;
1464 if (len != SAVE_AREA_SIZE) {
1465 cpu_physical_memory_unmap(mem, len, 1, 0);
1466 return -EFAULT;
1469 if (store_arch) {
1470 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1472 for (i = 0; i < 16; ++i) {
1473 *((uint64_t *)mem + i) = get_freg(&cpu->env, i)->ll;
1475 memcpy(mem + 128, &cpu->env.regs, 128);
1476 memcpy(mem + 256, &cpu->env.psw, 16);
1477 memcpy(mem + 280, &cpu->env.psa, 4);
1478 memcpy(mem + 284, &cpu->env.fpc, 4);
1479 memcpy(mem + 292, &cpu->env.todpr, 4);
1480 memcpy(mem + 296, &cpu->env.cputm, 8);
1481 memcpy(mem + 304, &ckc, 8);
1482 memcpy(mem + 320, &cpu->env.aregs, 64);
1483 memcpy(mem + 384, &cpu->env.cregs, 128);
1485 cpu_physical_memory_unmap(mem, len, 1, len);
1487 return 0;
1490 static void sigp_stop_and_store_status(void *arg)
1492 SigpInfo *si = arg;
1493 struct kvm_s390_irq irq = {
1494 .type = KVM_S390_SIGP_STOP,
1497 /* disabled wait - sleeping in user space */
1498 if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1499 CPU(si->cpu)->halted) {
1500 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1503 switch (s390_cpu_get_state(si->cpu)) {
1504 case CPU_STATE_OPERATING:
1505 si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1506 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1507 /* store will be performed when handling the stop intercept */
1508 break;
1509 case CPU_STATE_STOPPED:
1510 /* already stopped, just store the status */
1511 cpu_synchronize_state(CPU(si->cpu));
1512 kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1513 break;
1515 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1518 static void sigp_store_status_at_address(void *arg)
1520 SigpInfo *si = arg;
1521 uint32_t address = si->param & 0x7ffffe00u;
1523 /* cpu has to be stopped */
1524 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1525 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1526 return;
1529 cpu_synchronize_state(CPU(si->cpu));
1531 if (kvm_s390_store_status(si->cpu, address, false)) {
1532 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1533 return;
1535 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1538 static void sigp_store_adtl_status(void *arg)
1540 SigpInfo *si = arg;
1542 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
1543 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1544 return;
1547 /* cpu has to be stopped */
1548 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1549 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1550 return;
1553 /* parameter must be aligned to 1024-byte boundary */
1554 if (si->param & 0x3ff) {
1555 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1556 return;
1559 cpu_synchronize_state(CPU(si->cpu));
1561 if (kvm_s390_store_adtl_status(si->cpu, si->param)) {
1562 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1563 return;
1565 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1568 static void sigp_restart(void *arg)
1570 SigpInfo *si = arg;
1571 struct kvm_s390_irq irq = {
1572 .type = KVM_S390_RESTART,
1575 switch (s390_cpu_get_state(si->cpu)) {
1576 case CPU_STATE_STOPPED:
1577 /* the restart irq has to be delivered prior to any other pending irq */
1578 cpu_synchronize_state(CPU(si->cpu));
1579 do_restart_interrupt(&si->cpu->env);
1580 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1581 break;
1582 case CPU_STATE_OPERATING:
1583 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1584 break;
1586 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1589 int kvm_s390_cpu_restart(S390CPU *cpu)
1591 SigpInfo si = {
1592 .cpu = cpu,
1595 run_on_cpu(CPU(cpu), sigp_restart, &si);
1596 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1597 return 0;
1600 static void sigp_initial_cpu_reset(void *arg)
1602 SigpInfo *si = arg;
1603 CPUState *cs = CPU(si->cpu);
1604 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1606 cpu_synchronize_state(cs);
1607 scc->initial_cpu_reset(cs);
1608 cpu_synchronize_post_reset(cs);
1609 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1612 static void sigp_cpu_reset(void *arg)
1614 SigpInfo *si = arg;
1615 CPUState *cs = CPU(si->cpu);
1616 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1618 cpu_synchronize_state(cs);
1619 scc->cpu_reset(cs);
1620 cpu_synchronize_post_reset(cs);
1621 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1624 static void sigp_set_prefix(void *arg)
1626 SigpInfo *si = arg;
1627 uint32_t addr = si->param & 0x7fffe000u;
1629 cpu_synchronize_state(CPU(si->cpu));
1631 if (!address_space_access_valid(&address_space_memory, addr,
1632 sizeof(struct LowCore), false)) {
1633 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1634 return;
1637 /* cpu has to be stopped */
1638 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1639 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1640 return;
1643 si->cpu->env.psa = addr;
1644 cpu_synchronize_post_init(CPU(si->cpu));
1645 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1648 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1649 uint64_t param, uint64_t *status_reg)
1651 SigpInfo si = {
1652 .cpu = dst_cpu,
1653 .param = param,
1654 .status_reg = status_reg,
1657 /* cpu available? */
1658 if (dst_cpu == NULL) {
1659 return SIGP_CC_NOT_OPERATIONAL;
1662 /* only resets can break pending orders */
1663 if (dst_cpu->env.sigp_order != 0 &&
1664 order != SIGP_CPU_RESET &&
1665 order != SIGP_INITIAL_CPU_RESET) {
1666 return SIGP_CC_BUSY;
1669 switch (order) {
1670 case SIGP_START:
1671 run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1672 break;
1673 case SIGP_STOP:
1674 run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1675 break;
1676 case SIGP_RESTART:
1677 run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1678 break;
1679 case SIGP_STOP_STORE_STATUS:
1680 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1681 break;
1682 case SIGP_STORE_STATUS_ADDR:
1683 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1684 break;
1685 case SIGP_STORE_ADTL_STATUS:
1686 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, &si);
1687 break;
1688 case SIGP_SET_PREFIX:
1689 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1690 break;
1691 case SIGP_INITIAL_CPU_RESET:
1692 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1693 break;
1694 case SIGP_CPU_RESET:
1695 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1696 break;
1697 default:
1698 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1699 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1702 return si.cc;
1705 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1706 uint64_t *status_reg)
1708 CPUState *cur_cs;
1709 S390CPU *cur_cpu;
1711 /* due to the BQL, we are the only active cpu */
1712 CPU_FOREACH(cur_cs) {
1713 cur_cpu = S390_CPU(cur_cs);
1714 if (cur_cpu->env.sigp_order != 0) {
1715 return SIGP_CC_BUSY;
1717 cpu_synchronize_state(cur_cs);
1718 /* all but the current one have to be stopped */
1719 if (cur_cpu != cpu &&
1720 s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1721 *status_reg &= 0xffffffff00000000ULL;
1722 *status_reg |= SIGP_STAT_INCORRECT_STATE;
1723 return SIGP_CC_STATUS_STORED;
1727 switch (param & 0xff) {
1728 case SIGP_MODE_ESA_S390:
1729 /* not supported */
1730 return SIGP_CC_NOT_OPERATIONAL;
1731 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1732 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1733 CPU_FOREACH(cur_cs) {
1734 cur_cpu = S390_CPU(cur_cs);
1735 cur_cpu->env.pfault_token = -1UL;
1737 break;
1738 default:
1739 *status_reg &= 0xffffffff00000000ULL;
1740 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1741 return SIGP_CC_STATUS_STORED;
1744 return SIGP_CC_ORDER_CODE_ACCEPTED;
1747 #define SIGP_ORDER_MASK 0x000000ff
1749 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1751 CPUS390XState *env = &cpu->env;
1752 const uint8_t r1 = ipa1 >> 4;
1753 const uint8_t r3 = ipa1 & 0x0f;
1754 int ret;
1755 uint8_t order;
1756 uint64_t *status_reg;
1757 uint64_t param;
1758 S390CPU *dst_cpu = NULL;
1760 cpu_synchronize_state(CPU(cpu));
1762 /* get order code */
1763 order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1764 & SIGP_ORDER_MASK;
1765 status_reg = &env->regs[r1];
1766 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1768 switch (order) {
1769 case SIGP_SET_ARCH:
1770 ret = sigp_set_architecture(cpu, param, status_reg);
1771 break;
1772 default:
1773 /* all other sigp orders target a single vcpu */
1774 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1775 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1778 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1779 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1781 if (ret >= 0) {
1782 setcc(cpu, ret);
1783 return 0;
1786 return ret;
1789 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1791 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1792 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1793 int r = -1;
1795 DPRINTF("handle_instruction 0x%x 0x%x\n",
1796 run->s390_sieic.ipa, run->s390_sieic.ipb);
1797 switch (ipa0) {
1798 case IPA0_B2:
1799 r = handle_b2(cpu, run, ipa1);
1800 break;
1801 case IPA0_B9:
1802 r = handle_b9(cpu, run, ipa1);
1803 break;
1804 case IPA0_EB:
1805 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1806 break;
1807 case IPA0_E3:
1808 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1809 break;
1810 case IPA0_DIAG:
1811 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1812 break;
1813 case IPA0_SIGP:
1814 r = handle_sigp(cpu, run, ipa1);
1815 break;
1818 if (r < 0) {
1819 r = 0;
1820 enter_pgmcheck(cpu, 0x0001);
1823 return r;
1826 static bool is_special_wait_psw(CPUState *cs)
1828 /* signal quiesce */
1829 return cs->kvm_run->psw_addr == 0xfffUL;
1832 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1834 CPUState *cs = CPU(cpu);
1836 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1837 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1838 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1839 s390_cpu_halt(cpu);
1840 qemu_system_guest_panicked();
1843 static int handle_intercept(S390CPU *cpu)
1845 CPUState *cs = CPU(cpu);
1846 struct kvm_run *run = cs->kvm_run;
1847 int icpt_code = run->s390_sieic.icptcode;
1848 int r = 0;
1850 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1851 (long)cs->kvm_run->psw_addr);
1852 switch (icpt_code) {
1853 case ICPT_INSTRUCTION:
1854 r = handle_instruction(cpu, run);
1855 break;
1856 case ICPT_PROGRAM:
1857 unmanageable_intercept(cpu, "program interrupt",
1858 offsetof(LowCore, program_new_psw));
1859 r = EXCP_HALTED;
1860 break;
1861 case ICPT_EXT_INT:
1862 unmanageable_intercept(cpu, "external interrupt",
1863 offsetof(LowCore, external_new_psw));
1864 r = EXCP_HALTED;
1865 break;
1866 case ICPT_WAITPSW:
1867 /* disabled wait, since enabled wait is handled in kernel */
1868 cpu_synchronize_state(cs);
1869 if (s390_cpu_halt(cpu) == 0) {
1870 if (is_special_wait_psw(cs)) {
1871 qemu_system_shutdown_request();
1872 } else {
1873 qemu_system_guest_panicked();
1876 r = EXCP_HALTED;
1877 break;
1878 case ICPT_CPU_STOP:
1879 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1880 qemu_system_shutdown_request();
1882 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1883 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1884 true);
1886 cpu->env.sigp_order = 0;
1887 r = EXCP_HALTED;
1888 break;
1889 case ICPT_OPEREXC:
1890 /* currently only instr 0x0000 after enabled via capability */
1891 r = handle_sw_breakpoint(cpu, run);
1892 if (r == -ENOENT) {
1893 enter_pgmcheck(cpu, PGM_OPERATION);
1894 r = 0;
1896 break;
1897 case ICPT_SOFT_INTERCEPT:
1898 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1899 exit(1);
1900 break;
1901 case ICPT_IO:
1902 fprintf(stderr, "KVM unimplemented icpt IO\n");
1903 exit(1);
1904 break;
1905 default:
1906 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1907 exit(1);
1908 break;
1911 return r;
1914 static int handle_tsch(S390CPU *cpu)
1916 CPUState *cs = CPU(cpu);
1917 struct kvm_run *run = cs->kvm_run;
1918 int ret;
1920 cpu_synchronize_state(cs);
1922 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1923 if (ret < 0) {
1925 * Failure.
1926 * If an I/O interrupt had been dequeued, we have to reinject it.
1928 if (run->s390_tsch.dequeued) {
1929 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1930 run->s390_tsch.subchannel_nr,
1931 run->s390_tsch.io_int_parm,
1932 run->s390_tsch.io_int_word);
1934 ret = 0;
1936 return ret;
1939 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1941 struct sysib_322 sysib;
1942 int del;
1944 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1945 return;
1947 /* Shift the stack of Extended Names to prepare for our own data */
1948 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1949 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1950 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1951 * assumed it's not capable of managing Extended Names for lower levels.
1953 for (del = 1; del < sysib.count; del++) {
1954 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1955 break;
1958 if (del < sysib.count) {
1959 memset(sysib.ext_names[del], 0,
1960 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1962 /* Insert short machine name in EBCDIC, padded with blanks */
1963 if (qemu_name) {
1964 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1965 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1966 strlen(qemu_name)));
1968 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1969 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1970 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1971 * considered by s390 as not capable of providing any Extended Name.
1972 * Therefore if no name was specified on qemu invocation, we go with the
1973 * same "KVMguest" default, which KVM has filled into short name field.
1975 if (qemu_name) {
1976 strncpy((char *)sysib.ext_names[0], qemu_name,
1977 sizeof(sysib.ext_names[0]));
1978 } else {
1979 strcpy((char *)sysib.ext_names[0], "KVMguest");
1981 /* Insert UUID */
1982 memcpy(sysib.vm[0].uuid, qemu_uuid, sizeof(sysib.vm[0].uuid));
1984 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1987 static int handle_stsi(S390CPU *cpu)
1989 CPUState *cs = CPU(cpu);
1990 struct kvm_run *run = cs->kvm_run;
1992 switch (run->s390_stsi.fc) {
1993 case 3:
1994 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1995 return 0;
1997 /* Only sysib 3.2.2 needs post-handling for now. */
1998 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1999 return 0;
2000 default:
2001 return 0;
2005 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
2007 CPUState *cs = CPU(cpu);
2008 struct kvm_run *run = cs->kvm_run;
2010 int ret = 0;
2011 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
2013 switch (arch_info->type) {
2014 case KVM_HW_WP_WRITE:
2015 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
2016 cs->watchpoint_hit = &hw_watchpoint;
2017 hw_watchpoint.vaddr = arch_info->addr;
2018 hw_watchpoint.flags = BP_MEM_WRITE;
2019 ret = EXCP_DEBUG;
2021 break;
2022 case KVM_HW_BP:
2023 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
2024 ret = EXCP_DEBUG;
2026 break;
2027 case KVM_SINGLESTEP:
2028 if (cs->singlestep_enabled) {
2029 ret = EXCP_DEBUG;
2031 break;
2032 default:
2033 ret = -ENOSYS;
2036 return ret;
2039 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
2041 S390CPU *cpu = S390_CPU(cs);
2042 int ret = 0;
2044 qemu_mutex_lock_iothread();
2046 switch (run->exit_reason) {
2047 case KVM_EXIT_S390_SIEIC:
2048 ret = handle_intercept(cpu);
2049 break;
2050 case KVM_EXIT_S390_RESET:
2051 s390_reipl_request();
2052 break;
2053 case KVM_EXIT_S390_TSCH:
2054 ret = handle_tsch(cpu);
2055 break;
2056 case KVM_EXIT_S390_STSI:
2057 ret = handle_stsi(cpu);
2058 break;
2059 case KVM_EXIT_DEBUG:
2060 ret = kvm_arch_handle_debug_exit(cpu);
2061 break;
2062 default:
2063 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2064 break;
2066 qemu_mutex_unlock_iothread();
2068 if (ret == 0) {
2069 ret = EXCP_INTERRUPT;
2071 return ret;
2074 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2076 return true;
2079 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
2081 return 1;
2084 int kvm_arch_on_sigbus(int code, void *addr)
2086 return 1;
2089 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2090 uint16_t subchannel_nr, uint32_t io_int_parm,
2091 uint32_t io_int_word)
2093 struct kvm_s390_irq irq = {
2094 .u.io.subchannel_id = subchannel_id,
2095 .u.io.subchannel_nr = subchannel_nr,
2096 .u.io.io_int_parm = io_int_parm,
2097 .u.io.io_int_word = io_int_word,
2100 if (io_int_word & IO_INT_WORD_AI) {
2101 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2102 } else {
2103 irq.type = KVM_S390_INT_IO(0, (subchannel_id & 0xff00) >> 8,
2104 (subchannel_id & 0x0006),
2105 subchannel_nr);
2107 kvm_s390_floating_interrupt(&irq);
2110 static uint64_t build_channel_report_mcic(void)
2112 uint64_t mcic;
2114 /* subclass: indicate channel report pending */
2115 mcic = MCIC_SC_CP |
2116 /* subclass modifiers: none */
2117 /* storage errors: none */
2118 /* validity bits: no damage */
2119 MCIC_VB_WP | MCIC_VB_MS | MCIC_VB_PM | MCIC_VB_IA | MCIC_VB_FP |
2120 MCIC_VB_GR | MCIC_VB_CR | MCIC_VB_ST | MCIC_VB_AR | MCIC_VB_PR |
2121 MCIC_VB_FC | MCIC_VB_CT | MCIC_VB_CC;
2122 if (kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
2123 mcic |= MCIC_VB_VR;
2125 return mcic;
2128 void kvm_s390_crw_mchk(void)
2130 struct kvm_s390_irq irq = {
2131 .type = KVM_S390_MCHK,
2132 .u.mchk.cr14 = 1 << 28,
2133 .u.mchk.mcic = build_channel_report_mcic(),
2135 kvm_s390_floating_interrupt(&irq);
2138 void kvm_s390_enable_css_support(S390CPU *cpu)
2140 int r;
2142 /* Activate host kernel channel subsystem support. */
2143 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2144 assert(r == 0);
2147 void kvm_arch_init_irq_routing(KVMState *s)
2150 * Note that while irqchip capabilities generally imply that cpustates
2151 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2152 * have to override the common code kvm_halt_in_kernel_allowed setting.
2154 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2155 kvm_gsi_routing_allowed = true;
2156 kvm_halt_in_kernel_allowed = false;
2160 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2161 int vq, bool assign)
2163 struct kvm_ioeventfd kick = {
2164 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2165 KVM_IOEVENTFD_FLAG_DATAMATCH,
2166 .fd = event_notifier_get_fd(notifier),
2167 .datamatch = vq,
2168 .addr = sch,
2169 .len = 8,
2171 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2172 return -ENOSYS;
2174 if (!assign) {
2175 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2177 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2180 int kvm_s390_get_memslot_count(KVMState *s)
2182 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
2185 int kvm_s390_get_ri(void)
2187 return cap_ri;
2190 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2192 struct kvm_mp_state mp_state = {};
2193 int ret;
2195 /* the kvm part might not have been initialized yet */
2196 if (CPU(cpu)->kvm_state == NULL) {
2197 return 0;
2200 switch (cpu_state) {
2201 case CPU_STATE_STOPPED:
2202 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2203 break;
2204 case CPU_STATE_CHECK_STOP:
2205 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2206 break;
2207 case CPU_STATE_OPERATING:
2208 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2209 break;
2210 case CPU_STATE_LOAD:
2211 mp_state.mp_state = KVM_MP_STATE_LOAD;
2212 break;
2213 default:
2214 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2215 cpu_state);
2216 exit(1);
2219 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2220 if (ret) {
2221 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2222 strerror(-ret));
2225 return ret;
2228 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2230 struct kvm_s390_irq_state irq_state;
2231 CPUState *cs = CPU(cpu);
2232 int32_t bytes;
2234 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2235 return;
2238 irq_state.buf = (uint64_t) cpu->irqstate;
2239 irq_state.len = VCPU_IRQ_BUF_SIZE;
2241 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2242 if (bytes < 0) {
2243 cpu->irqstate_saved_size = 0;
2244 error_report("Migration of interrupt state failed");
2245 return;
2248 cpu->irqstate_saved_size = bytes;
2251 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2253 CPUState *cs = CPU(cpu);
2254 struct kvm_s390_irq_state irq_state;
2255 int r;
2257 if (cpu->irqstate_saved_size == 0) {
2258 return 0;
2261 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2262 return -ENOSYS;
2265 irq_state.buf = (uint64_t) cpu->irqstate;
2266 irq_state.len = cpu->irqstate_saved_size;
2268 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2269 if (r) {
2270 error_report("Setting interrupt state failed %d", r);
2272 return r;
2275 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2276 uint64_t address, uint32_t data, PCIDevice *dev)
2278 S390PCIBusDevice *pbdev;
2279 uint32_t idx = data >> ZPCI_MSI_VEC_BITS;
2280 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2282 pbdev = s390_pci_find_dev_by_idx(idx);
2283 if (!pbdev) {
2284 DPRINTF("add_msi_route no dev\n");
2285 return -ENODEV;
2288 pbdev->routes.adapter.ind_offset = vec;
2290 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2291 route->flags = 0;
2292 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2293 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2294 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2295 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2296 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2297 return 0;
2300 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2301 int vector, PCIDevice *dev)
2303 return 0;
2306 int kvm_arch_release_virq_post(int virq)
2308 return 0;
2311 int kvm_arch_msi_data_to_gsi(uint32_t data)
2313 abort();