target/s390x: Move DisasFields into DisasContext
[qemu/ar7.git] / target / s390x / kvm.c
blob30112e529c2ebea6afefc3bb8bb6fd65930f9c64
1 /*
2 * QEMU S390x KVM implementation
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp. 2012
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program 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 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include <sys/ioctl.h>
24 #include <linux/kvm.h>
25 #include <asm/ptrace.h>
27 #include "qemu-common.h"
28 #include "cpu.h"
29 #include "internal.h"
30 #include "kvm_s390x.h"
31 #include "sysemu/kvm_int.h"
32 #include "qapi/error.h"
33 #include "qemu/error-report.h"
34 #include "qemu/timer.h"
35 #include "qemu/units.h"
36 #include "qemu/main-loop.h"
37 #include "qemu/mmap-alloc.h"
38 #include "qemu/log.h"
39 #include "sysemu/sysemu.h"
40 #include "sysemu/hw_accel.h"
41 #include "sysemu/runstate.h"
42 #include "sysemu/device_tree.h"
43 #include "exec/gdbstub.h"
44 #include "exec/ram_addr.h"
45 #include "trace.h"
46 #include "hw/s390x/s390-pci-inst.h"
47 #include "hw/s390x/s390-pci-bus.h"
48 #include "hw/s390x/ipl.h"
49 #include "hw/s390x/ebcdic.h"
50 #include "exec/memattrs.h"
51 #include "hw/s390x/s390-virtio-ccw.h"
52 #include "hw/s390x/s390-virtio-hcall.h"
54 #ifndef DEBUG_KVM
55 #define DEBUG_KVM 0
56 #endif
58 #define DPRINTF(fmt, ...) do { \
59 if (DEBUG_KVM) { \
60 fprintf(stderr, fmt, ## __VA_ARGS__); \
61 } \
62 } while (0)
64 #define kvm_vm_check_mem_attr(s, attr) \
65 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
67 #define IPA0_DIAG 0x8300
68 #define IPA0_SIGP 0xae00
69 #define IPA0_B2 0xb200
70 #define IPA0_B9 0xb900
71 #define IPA0_EB 0xeb00
72 #define IPA0_E3 0xe300
74 #define PRIV_B2_SCLP_CALL 0x20
75 #define PRIV_B2_CSCH 0x30
76 #define PRIV_B2_HSCH 0x31
77 #define PRIV_B2_MSCH 0x32
78 #define PRIV_B2_SSCH 0x33
79 #define PRIV_B2_STSCH 0x34
80 #define PRIV_B2_TSCH 0x35
81 #define PRIV_B2_TPI 0x36
82 #define PRIV_B2_SAL 0x37
83 #define PRIV_B2_RSCH 0x38
84 #define PRIV_B2_STCRW 0x39
85 #define PRIV_B2_STCPS 0x3a
86 #define PRIV_B2_RCHP 0x3b
87 #define PRIV_B2_SCHM 0x3c
88 #define PRIV_B2_CHSC 0x5f
89 #define PRIV_B2_SIGA 0x74
90 #define PRIV_B2_XSCH 0x76
92 #define PRIV_EB_SQBS 0x8a
93 #define PRIV_EB_PCISTB 0xd0
94 #define PRIV_EB_SIC 0xd1
96 #define PRIV_B9_EQBS 0x9c
97 #define PRIV_B9_CLP 0xa0
98 #define PRIV_B9_PCISTG 0xd0
99 #define PRIV_B9_PCILG 0xd2
100 #define PRIV_B9_RPCIT 0xd3
102 #define PRIV_E3_MPCIFC 0xd0
103 #define PRIV_E3_STPCIFC 0xd4
105 #define DIAG_TIMEREVENT 0x288
106 #define DIAG_IPL 0x308
107 #define DIAG_KVM_HYPERCALL 0x500
108 #define DIAG_KVM_BREAKPOINT 0x501
110 #define ICPT_INSTRUCTION 0x04
111 #define ICPT_PROGRAM 0x08
112 #define ICPT_EXT_INT 0x14
113 #define ICPT_WAITPSW 0x1c
114 #define ICPT_SOFT_INTERCEPT 0x24
115 #define ICPT_CPU_STOP 0x28
116 #define ICPT_OPEREXC 0x2c
117 #define ICPT_IO 0x40
119 #define NR_LOCAL_IRQS 32
121 * Needs to be big enough to contain max_cpus emergency signals
122 * and in addition NR_LOCAL_IRQS interrupts
124 #define VCPU_IRQ_BUF_SIZE(max_cpus) (sizeof(struct kvm_s390_irq) * \
125 (max_cpus + NR_LOCAL_IRQS))
127 * KVM does only support memory slots up to KVM_MEM_MAX_NR_PAGES pages
128 * as the dirty bitmap must be managed by bitops that take an int as
129 * position indicator. This would end at an unaligned address
130 * (0x7fffff00000). As future variants might provide larger pages
131 * and to make all addresses properly aligned, let us split at 4TB.
133 #define KVM_SLOT_MAX_BYTES (4UL * TiB)
135 static CPUWatchpoint hw_watchpoint;
137 * We don't use a list because this structure is also used to transmit the
138 * hardware breakpoints to the kernel.
140 static struct kvm_hw_breakpoint *hw_breakpoints;
141 static int nb_hw_breakpoints;
143 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
144 KVM_CAP_LAST_INFO
147 static int cap_sync_regs;
148 static int cap_async_pf;
149 static int cap_mem_op;
150 static int cap_s390_irq;
151 static int cap_ri;
152 static int cap_gs;
153 static int cap_hpage_1m;
155 static int active_cmma;
157 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared);
159 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
161 struct kvm_device_attr attr = {
162 .group = KVM_S390_VM_MEM_CTRL,
163 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
164 .addr = (uint64_t) memory_limit,
167 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
170 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit)
172 int rc;
174 struct kvm_device_attr attr = {
175 .group = KVM_S390_VM_MEM_CTRL,
176 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
177 .addr = (uint64_t) &new_limit,
180 if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) {
181 return 0;
184 rc = kvm_s390_query_mem_limit(hw_limit);
185 if (rc) {
186 return rc;
187 } else if (*hw_limit < new_limit) {
188 return -E2BIG;
191 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
194 int kvm_s390_cmma_active(void)
196 return active_cmma;
199 static bool kvm_s390_cmma_available(void)
201 static bool initialized, value;
203 if (!initialized) {
204 initialized = true;
205 value = kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_ENABLE_CMMA) &&
206 kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_CLR_CMMA);
208 return value;
211 void kvm_s390_cmma_reset(void)
213 int rc;
214 struct kvm_device_attr attr = {
215 .group = KVM_S390_VM_MEM_CTRL,
216 .attr = KVM_S390_VM_MEM_CLR_CMMA,
219 if (!kvm_s390_cmma_active()) {
220 return;
223 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
224 trace_kvm_clear_cmma(rc);
227 static void kvm_s390_enable_cmma(void)
229 int rc;
230 struct kvm_device_attr attr = {
231 .group = KVM_S390_VM_MEM_CTRL,
232 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
235 if (cap_hpage_1m) {
236 warn_report("CMM will not be enabled because it is not "
237 "compatible with huge memory backings.");
238 return;
240 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
241 active_cmma = !rc;
242 trace_kvm_enable_cmma(rc);
245 static void kvm_s390_set_attr(uint64_t attr)
247 struct kvm_device_attr attribute = {
248 .group = KVM_S390_VM_CRYPTO,
249 .attr = attr,
252 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
254 if (ret) {
255 error_report("Failed to set crypto device attribute %lu: %s",
256 attr, strerror(-ret));
260 static void kvm_s390_init_aes_kw(void)
262 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
264 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
265 NULL)) {
266 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
269 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
270 kvm_s390_set_attr(attr);
274 static void kvm_s390_init_dea_kw(void)
276 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
278 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
279 NULL)) {
280 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
283 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
284 kvm_s390_set_attr(attr);
288 void kvm_s390_crypto_reset(void)
290 if (s390_has_feat(S390_FEAT_MSA_EXT_3)) {
291 kvm_s390_init_aes_kw();
292 kvm_s390_init_dea_kw();
296 void kvm_s390_set_max_pagesize(uint64_t pagesize, Error **errp)
298 if (pagesize == 4 * KiB) {
299 return;
302 if (!hpage_1m_allowed()) {
303 error_setg(errp, "This QEMU machine does not support huge page "
304 "mappings");
305 return;
308 if (pagesize != 1 * MiB) {
309 error_setg(errp, "Memory backing with 2G pages was specified, "
310 "but KVM does not support this memory backing");
311 return;
314 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_HPAGE_1M, 0)) {
315 error_setg(errp, "Memory backing with 1M pages was specified, "
316 "but KVM does not support this memory backing");
317 return;
320 cap_hpage_1m = 1;
323 static void ccw_machine_class_foreach(ObjectClass *oc, void *opaque)
325 MachineClass *mc = MACHINE_CLASS(oc);
327 mc->default_cpu_type = S390_CPU_TYPE_NAME("host");
330 int kvm_arch_init(MachineState *ms, KVMState *s)
332 object_class_foreach(ccw_machine_class_foreach, TYPE_S390_CCW_MACHINE,
333 false, NULL);
335 if (!kvm_check_extension(kvm_state, KVM_CAP_DEVICE_CTRL)) {
336 error_report("KVM is missing capability KVM_CAP_DEVICE_CTRL - "
337 "please use kernel 3.15 or newer");
338 return -1;
341 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
342 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
343 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
344 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
346 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
347 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
348 phys_mem_set_alloc(legacy_s390_alloc);
351 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
352 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
353 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
354 if (ri_allowed()) {
355 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
356 cap_ri = 1;
359 if (cpu_model_allowed()) {
360 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
361 cap_gs = 1;
366 * The migration interface for ais was introduced with kernel 4.13
367 * but the capability itself had been active since 4.12. As migration
368 * support is considered necessary, we only try to enable this for
369 * newer machine types if KVM_CAP_S390_AIS_MIGRATION is available.
371 if (cpu_model_allowed() && kvm_kernel_irqchip_allowed() &&
372 kvm_check_extension(s, KVM_CAP_S390_AIS_MIGRATION)) {
373 kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0);
376 kvm_set_max_memslot_size(KVM_SLOT_MAX_BYTES);
377 return 0;
380 int kvm_arch_irqchip_create(KVMState *s)
382 return 0;
385 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
387 return cpu->cpu_index;
390 int kvm_arch_init_vcpu(CPUState *cs)
392 unsigned int max_cpus = MACHINE(qdev_get_machine())->smp.max_cpus;
393 S390CPU *cpu = S390_CPU(cs);
394 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
395 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE(max_cpus));
396 return 0;
399 int kvm_arch_destroy_vcpu(CPUState *cs)
401 S390CPU *cpu = S390_CPU(cs);
403 g_free(cpu->irqstate);
404 cpu->irqstate = NULL;
406 return 0;
409 void kvm_s390_reset_vcpu(S390CPU *cpu)
411 CPUState *cs = CPU(cpu);
413 /* The initial reset call is needed here to reset in-kernel
414 * vcpu data that we can't access directly from QEMU
415 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
416 * Before this ioctl cpu_synchronize_state() is called in common kvm
417 * code (kvm-all) */
418 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
419 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
423 static int can_sync_regs(CPUState *cs, int regs)
425 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
428 int kvm_arch_put_registers(CPUState *cs, int level)
430 S390CPU *cpu = S390_CPU(cs);
431 CPUS390XState *env = &cpu->env;
432 struct kvm_sregs sregs;
433 struct kvm_regs regs;
434 struct kvm_fpu fpu = {};
435 int r;
436 int i;
438 /* always save the PSW and the GPRS*/
439 cs->kvm_run->psw_addr = env->psw.addr;
440 cs->kvm_run->psw_mask = env->psw.mask;
442 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
443 for (i = 0; i < 16; i++) {
444 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
445 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
447 } else {
448 for (i = 0; i < 16; i++) {
449 regs.gprs[i] = env->regs[i];
451 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
452 if (r < 0) {
453 return r;
457 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
458 for (i = 0; i < 32; i++) {
459 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0];
460 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1];
462 cs->kvm_run->s.regs.fpc = env->fpc;
463 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
464 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
465 for (i = 0; i < 16; i++) {
466 cs->kvm_run->s.regs.fprs[i] = *get_freg(env, i);
468 cs->kvm_run->s.regs.fpc = env->fpc;
469 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
470 } else {
471 /* Floating point */
472 for (i = 0; i < 16; i++) {
473 fpu.fprs[i] = *get_freg(env, i);
475 fpu.fpc = env->fpc;
477 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
478 if (r < 0) {
479 return r;
483 /* Do we need to save more than that? */
484 if (level == KVM_PUT_RUNTIME_STATE) {
485 return 0;
488 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
489 cs->kvm_run->s.regs.cputm = env->cputm;
490 cs->kvm_run->s.regs.ckc = env->ckc;
491 cs->kvm_run->s.regs.todpr = env->todpr;
492 cs->kvm_run->s.regs.gbea = env->gbea;
493 cs->kvm_run->s.regs.pp = env->pp;
494 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
495 } else {
497 * These ONE_REGS are not protected by a capability. As they are only
498 * necessary for migration we just trace a possible error, but don't
499 * return with an error return code.
501 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
502 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
503 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
504 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
505 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
508 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
509 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
510 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
513 /* pfault parameters */
514 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
515 cs->kvm_run->s.regs.pft = env->pfault_token;
516 cs->kvm_run->s.regs.pfs = env->pfault_select;
517 cs->kvm_run->s.regs.pfc = env->pfault_compare;
518 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
519 } else if (cap_async_pf) {
520 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
521 if (r < 0) {
522 return r;
524 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
525 if (r < 0) {
526 return r;
528 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
529 if (r < 0) {
530 return r;
534 /* access registers and control registers*/
535 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
536 for (i = 0; i < 16; i++) {
537 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
538 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
540 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
541 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
542 } else {
543 for (i = 0; i < 16; i++) {
544 sregs.acrs[i] = env->aregs[i];
545 sregs.crs[i] = env->cregs[i];
547 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
548 if (r < 0) {
549 return r;
553 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
554 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
555 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
558 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
559 cs->kvm_run->s.regs.bpbc = env->bpbc;
560 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_BPBC;
563 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
564 cs->kvm_run->s.regs.etoken = env->etoken;
565 cs->kvm_run->s.regs.etoken_extension = env->etoken_extension;
566 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ETOKEN;
569 /* Finally the prefix */
570 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
571 cs->kvm_run->s.regs.prefix = env->psa;
572 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
573 } else {
574 /* prefix is only supported via sync regs */
576 return 0;
579 int kvm_arch_get_registers(CPUState *cs)
581 S390CPU *cpu = S390_CPU(cs);
582 CPUS390XState *env = &cpu->env;
583 struct kvm_sregs sregs;
584 struct kvm_regs regs;
585 struct kvm_fpu fpu;
586 int i, r;
588 /* get the PSW */
589 env->psw.addr = cs->kvm_run->psw_addr;
590 env->psw.mask = cs->kvm_run->psw_mask;
592 /* the GPRS */
593 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
594 for (i = 0; i < 16; i++) {
595 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
597 } else {
598 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
599 if (r < 0) {
600 return r;
602 for (i = 0; i < 16; i++) {
603 env->regs[i] = regs.gprs[i];
607 /* The ACRS and CRS */
608 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
609 for (i = 0; i < 16; i++) {
610 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
611 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
613 } else {
614 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
615 if (r < 0) {
616 return r;
618 for (i = 0; i < 16; i++) {
619 env->aregs[i] = sregs.acrs[i];
620 env->cregs[i] = sregs.crs[i];
624 /* Floating point and vector registers */
625 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
626 for (i = 0; i < 32; i++) {
627 env->vregs[i][0] = cs->kvm_run->s.regs.vrs[i][0];
628 env->vregs[i][1] = cs->kvm_run->s.regs.vrs[i][1];
630 env->fpc = cs->kvm_run->s.regs.fpc;
631 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
632 for (i = 0; i < 16; i++) {
633 *get_freg(env, i) = cs->kvm_run->s.regs.fprs[i];
635 env->fpc = cs->kvm_run->s.regs.fpc;
636 } else {
637 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
638 if (r < 0) {
639 return r;
641 for (i = 0; i < 16; i++) {
642 *get_freg(env, i) = fpu.fprs[i];
644 env->fpc = fpu.fpc;
647 /* The prefix */
648 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
649 env->psa = cs->kvm_run->s.regs.prefix;
652 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
653 env->cputm = cs->kvm_run->s.regs.cputm;
654 env->ckc = cs->kvm_run->s.regs.ckc;
655 env->todpr = cs->kvm_run->s.regs.todpr;
656 env->gbea = cs->kvm_run->s.regs.gbea;
657 env->pp = cs->kvm_run->s.regs.pp;
658 } else {
660 * These ONE_REGS are not protected by a capability. As they are only
661 * necessary for migration we just trace a possible error, but don't
662 * return with an error return code.
664 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
665 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
666 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
667 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
668 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
671 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
672 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
675 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
676 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
679 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
680 env->bpbc = cs->kvm_run->s.regs.bpbc;
683 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
684 env->etoken = cs->kvm_run->s.regs.etoken;
685 env->etoken_extension = cs->kvm_run->s.regs.etoken_extension;
688 /* pfault parameters */
689 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
690 env->pfault_token = cs->kvm_run->s.regs.pft;
691 env->pfault_select = cs->kvm_run->s.regs.pfs;
692 env->pfault_compare = cs->kvm_run->s.regs.pfc;
693 } else if (cap_async_pf) {
694 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
695 if (r < 0) {
696 return r;
698 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
699 if (r < 0) {
700 return r;
702 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
703 if (r < 0) {
704 return r;
708 return 0;
711 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
713 int r;
714 struct kvm_device_attr attr = {
715 .group = KVM_S390_VM_TOD,
716 .attr = KVM_S390_VM_TOD_LOW,
717 .addr = (uint64_t)tod_low,
720 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
721 if (r) {
722 return r;
725 attr.attr = KVM_S390_VM_TOD_HIGH;
726 attr.addr = (uint64_t)tod_high;
727 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
730 int kvm_s390_get_clock_ext(uint8_t *tod_high, uint64_t *tod_low)
732 int r;
733 struct kvm_s390_vm_tod_clock gtod;
734 struct kvm_device_attr attr = {
735 .group = KVM_S390_VM_TOD,
736 .attr = KVM_S390_VM_TOD_EXT,
737 .addr = (uint64_t)&gtod,
740 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
741 *tod_high = gtod.epoch_idx;
742 *tod_low = gtod.tod;
744 return r;
747 int kvm_s390_set_clock(uint8_t tod_high, uint64_t tod_low)
749 int r;
750 struct kvm_device_attr attr = {
751 .group = KVM_S390_VM_TOD,
752 .attr = KVM_S390_VM_TOD_LOW,
753 .addr = (uint64_t)&tod_low,
756 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
757 if (r) {
758 return r;
761 attr.attr = KVM_S390_VM_TOD_HIGH;
762 attr.addr = (uint64_t)&tod_high;
763 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
766 int kvm_s390_set_clock_ext(uint8_t tod_high, uint64_t tod_low)
768 struct kvm_s390_vm_tod_clock gtod = {
769 .epoch_idx = tod_high,
770 .tod = tod_low,
772 struct kvm_device_attr attr = {
773 .group = KVM_S390_VM_TOD,
774 .attr = KVM_S390_VM_TOD_EXT,
775 .addr = (uint64_t)&gtod,
778 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
782 * kvm_s390_mem_op:
783 * @addr: the logical start address in guest memory
784 * @ar: the access register number
785 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
786 * @len: length that should be transferred
787 * @is_write: true = write, false = read
788 * Returns: 0 on success, non-zero if an exception or error occurred
790 * Use KVM ioctl to read/write from/to guest memory. An access exception
791 * is injected into the vCPU in case of translation errors.
793 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
794 int len, bool is_write)
796 struct kvm_s390_mem_op mem_op = {
797 .gaddr = addr,
798 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
799 .size = len,
800 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
801 : KVM_S390_MEMOP_LOGICAL_READ,
802 .buf = (uint64_t)hostbuf,
803 .ar = ar,
805 int ret;
807 if (!cap_mem_op) {
808 return -ENOSYS;
810 if (!hostbuf) {
811 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
814 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
815 if (ret < 0) {
816 warn_report("KVM_S390_MEM_OP failed: %s", strerror(-ret));
818 return ret;
822 * Legacy layout for s390:
823 * Older S390 KVM requires the topmost vma of the RAM to be
824 * smaller than an system defined value, which is at least 256GB.
825 * Larger systems have larger values. We put the guest between
826 * the end of data segment (system break) and this value. We
827 * use 32GB as a base to have enough room for the system break
828 * to grow. We also have to use MAP parameters that avoid
829 * read-only mapping of guest pages.
831 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared)
833 static void *mem;
835 if (mem) {
836 /* we only support one allocation, which is enough for initial ram */
837 return NULL;
840 mem = mmap((void *) 0x800000000ULL, size,
841 PROT_EXEC|PROT_READ|PROT_WRITE,
842 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
843 if (mem == MAP_FAILED) {
844 mem = NULL;
846 if (mem && align) {
847 *align = QEMU_VMALLOC_ALIGN;
849 return mem;
852 static uint8_t const *sw_bp_inst;
853 static uint8_t sw_bp_ilen;
855 static void determine_sw_breakpoint_instr(void)
857 /* DIAG 501 is used for sw breakpoints with old kernels */
858 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
859 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
860 static const uint8_t instr_0x0000[] = {0x00, 0x00};
862 if (sw_bp_inst) {
863 return;
865 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
866 sw_bp_inst = diag_501;
867 sw_bp_ilen = sizeof(diag_501);
868 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
869 } else {
870 sw_bp_inst = instr_0x0000;
871 sw_bp_ilen = sizeof(instr_0x0000);
872 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
876 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
878 determine_sw_breakpoint_instr();
880 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
881 sw_bp_ilen, 0) ||
882 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
883 return -EINVAL;
885 return 0;
888 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
890 uint8_t t[MAX_ILEN];
892 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
893 return -EINVAL;
894 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
895 return -EINVAL;
896 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
897 sw_bp_ilen, 1)) {
898 return -EINVAL;
901 return 0;
904 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
905 int len, int type)
907 int n;
909 for (n = 0; n < nb_hw_breakpoints; n++) {
910 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
911 (hw_breakpoints[n].len == len || len == -1)) {
912 return &hw_breakpoints[n];
916 return NULL;
919 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
921 int size;
923 if (find_hw_breakpoint(addr, len, type)) {
924 return -EEXIST;
927 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
929 if (!hw_breakpoints) {
930 nb_hw_breakpoints = 0;
931 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
932 } else {
933 hw_breakpoints =
934 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
937 if (!hw_breakpoints) {
938 nb_hw_breakpoints = 0;
939 return -ENOMEM;
942 hw_breakpoints[nb_hw_breakpoints].addr = addr;
943 hw_breakpoints[nb_hw_breakpoints].len = len;
944 hw_breakpoints[nb_hw_breakpoints].type = type;
946 nb_hw_breakpoints++;
948 return 0;
951 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
952 target_ulong len, int type)
954 switch (type) {
955 case GDB_BREAKPOINT_HW:
956 type = KVM_HW_BP;
957 break;
958 case GDB_WATCHPOINT_WRITE:
959 if (len < 1) {
960 return -EINVAL;
962 type = KVM_HW_WP_WRITE;
963 break;
964 default:
965 return -ENOSYS;
967 return insert_hw_breakpoint(addr, len, type);
970 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
971 target_ulong len, int type)
973 int size;
974 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
976 if (bp == NULL) {
977 return -ENOENT;
980 nb_hw_breakpoints--;
981 if (nb_hw_breakpoints > 0) {
983 * In order to trim the array, move the last element to the position to
984 * be removed - if necessary.
986 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
987 *bp = hw_breakpoints[nb_hw_breakpoints];
989 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
990 hw_breakpoints =
991 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
992 } else {
993 g_free(hw_breakpoints);
994 hw_breakpoints = NULL;
997 return 0;
1000 void kvm_arch_remove_all_hw_breakpoints(void)
1002 nb_hw_breakpoints = 0;
1003 g_free(hw_breakpoints);
1004 hw_breakpoints = NULL;
1007 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
1009 int i;
1011 if (nb_hw_breakpoints > 0) {
1012 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
1013 dbg->arch.hw_bp = hw_breakpoints;
1015 for (i = 0; i < nb_hw_breakpoints; ++i) {
1016 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
1017 hw_breakpoints[i].addr);
1019 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
1020 } else {
1021 dbg->arch.nr_hw_bp = 0;
1022 dbg->arch.hw_bp = NULL;
1026 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
1030 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1032 return MEMTXATTRS_UNSPECIFIED;
1035 int kvm_arch_process_async_events(CPUState *cs)
1037 return cs->halted;
1040 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
1041 struct kvm_s390_interrupt *interrupt)
1043 int r = 0;
1045 interrupt->type = irq->type;
1046 switch (irq->type) {
1047 case KVM_S390_INT_VIRTIO:
1048 interrupt->parm = irq->u.ext.ext_params;
1049 /* fall through */
1050 case KVM_S390_INT_PFAULT_INIT:
1051 case KVM_S390_INT_PFAULT_DONE:
1052 interrupt->parm64 = irq->u.ext.ext_params2;
1053 break;
1054 case KVM_S390_PROGRAM_INT:
1055 interrupt->parm = irq->u.pgm.code;
1056 break;
1057 case KVM_S390_SIGP_SET_PREFIX:
1058 interrupt->parm = irq->u.prefix.address;
1059 break;
1060 case KVM_S390_INT_SERVICE:
1061 interrupt->parm = irq->u.ext.ext_params;
1062 break;
1063 case KVM_S390_MCHK:
1064 interrupt->parm = irq->u.mchk.cr14;
1065 interrupt->parm64 = irq->u.mchk.mcic;
1066 break;
1067 case KVM_S390_INT_EXTERNAL_CALL:
1068 interrupt->parm = irq->u.extcall.code;
1069 break;
1070 case KVM_S390_INT_EMERGENCY:
1071 interrupt->parm = irq->u.emerg.code;
1072 break;
1073 case KVM_S390_SIGP_STOP:
1074 case KVM_S390_RESTART:
1075 break; /* These types have no parameters */
1076 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1077 interrupt->parm = irq->u.io.subchannel_id << 16;
1078 interrupt->parm |= irq->u.io.subchannel_nr;
1079 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
1080 interrupt->parm64 |= irq->u.io.io_int_word;
1081 break;
1082 default:
1083 r = -EINVAL;
1084 break;
1086 return r;
1089 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
1091 struct kvm_s390_interrupt kvmint = {};
1092 int r;
1094 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1095 if (r < 0) {
1096 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1097 exit(1);
1100 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
1101 if (r < 0) {
1102 fprintf(stderr, "KVM failed to inject interrupt\n");
1103 exit(1);
1107 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
1109 CPUState *cs = CPU(cpu);
1110 int r;
1112 if (cap_s390_irq) {
1113 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
1114 if (!r) {
1115 return;
1117 error_report("KVM failed to inject interrupt %llx", irq->type);
1118 exit(1);
1121 inject_vcpu_irq_legacy(cs, irq);
1124 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq *irq)
1126 struct kvm_s390_interrupt kvmint = {};
1127 int r;
1129 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1130 if (r < 0) {
1131 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1132 exit(1);
1135 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1136 if (r < 0) {
1137 fprintf(stderr, "KVM failed to inject interrupt\n");
1138 exit(1);
1142 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1144 struct kvm_s390_irq irq = {
1145 .type = KVM_S390_PROGRAM_INT,
1146 .u.pgm.code = code,
1148 qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
1149 cpu->env.psw.addr);
1150 kvm_s390_vcpu_interrupt(cpu, &irq);
1153 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1155 struct kvm_s390_irq irq = {
1156 .type = KVM_S390_PROGRAM_INT,
1157 .u.pgm.code = code,
1158 .u.pgm.trans_exc_code = te_code,
1159 .u.pgm.exc_access_id = te_code & 3,
1162 kvm_s390_vcpu_interrupt(cpu, &irq);
1165 static void kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1166 uint16_t ipbh0)
1168 CPUS390XState *env = &cpu->env;
1169 uint64_t sccb;
1170 uint32_t code;
1171 int r;
1173 sccb = env->regs[ipbh0 & 0xf];
1174 code = env->regs[(ipbh0 & 0xf0) >> 4];
1176 r = sclp_service_call(env, sccb, code);
1177 if (r < 0) {
1178 kvm_s390_program_interrupt(cpu, -r);
1179 return;
1181 setcc(cpu, r);
1184 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1186 CPUS390XState *env = &cpu->env;
1187 int rc = 0;
1188 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1190 switch (ipa1) {
1191 case PRIV_B2_XSCH:
1192 ioinst_handle_xsch(cpu, env->regs[1], RA_IGNORED);
1193 break;
1194 case PRIV_B2_CSCH:
1195 ioinst_handle_csch(cpu, env->regs[1], RA_IGNORED);
1196 break;
1197 case PRIV_B2_HSCH:
1198 ioinst_handle_hsch(cpu, env->regs[1], RA_IGNORED);
1199 break;
1200 case PRIV_B2_MSCH:
1201 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1202 break;
1203 case PRIV_B2_SSCH:
1204 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1205 break;
1206 case PRIV_B2_STCRW:
1207 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb, RA_IGNORED);
1208 break;
1209 case PRIV_B2_STSCH:
1210 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1211 break;
1212 case PRIV_B2_TSCH:
1213 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1214 fprintf(stderr, "Spurious tsch intercept\n");
1215 break;
1216 case PRIV_B2_CHSC:
1217 ioinst_handle_chsc(cpu, run->s390_sieic.ipb, RA_IGNORED);
1218 break;
1219 case PRIV_B2_TPI:
1220 /* This should have been handled by kvm already. */
1221 fprintf(stderr, "Spurious tpi intercept\n");
1222 break;
1223 case PRIV_B2_SCHM:
1224 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1225 run->s390_sieic.ipb, RA_IGNORED);
1226 break;
1227 case PRIV_B2_RSCH:
1228 ioinst_handle_rsch(cpu, env->regs[1], RA_IGNORED);
1229 break;
1230 case PRIV_B2_RCHP:
1231 ioinst_handle_rchp(cpu, env->regs[1], RA_IGNORED);
1232 break;
1233 case PRIV_B2_STCPS:
1234 /* We do not provide this instruction, it is suppressed. */
1235 break;
1236 case PRIV_B2_SAL:
1237 ioinst_handle_sal(cpu, env->regs[1], RA_IGNORED);
1238 break;
1239 case PRIV_B2_SIGA:
1240 /* Not provided, set CC = 3 for subchannel not operational */
1241 setcc(cpu, 3);
1242 break;
1243 case PRIV_B2_SCLP_CALL:
1244 kvm_sclp_service_call(cpu, run, ipbh0);
1245 break;
1246 default:
1247 rc = -1;
1248 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1249 break;
1252 return rc;
1255 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1256 uint8_t *ar)
1258 CPUS390XState *env = &cpu->env;
1259 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1260 uint32_t base2 = run->s390_sieic.ipb >> 28;
1261 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1262 ((run->s390_sieic.ipb & 0xff00) << 4);
1264 if (disp2 & 0x80000) {
1265 disp2 += 0xfff00000;
1267 if (ar) {
1268 *ar = base2;
1271 return (base2 ? env->regs[base2] : 0) +
1272 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1275 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1276 uint8_t *ar)
1278 CPUS390XState *env = &cpu->env;
1279 uint32_t base2 = run->s390_sieic.ipb >> 28;
1280 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1281 ((run->s390_sieic.ipb & 0xff00) << 4);
1283 if (disp2 & 0x80000) {
1284 disp2 += 0xfff00000;
1286 if (ar) {
1287 *ar = base2;
1290 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1293 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1295 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1297 if (s390_has_feat(S390_FEAT_ZPCI)) {
1298 return clp_service_call(cpu, r2, RA_IGNORED);
1299 } else {
1300 return -1;
1304 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1306 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1307 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1309 if (s390_has_feat(S390_FEAT_ZPCI)) {
1310 return pcilg_service_call(cpu, r1, r2, RA_IGNORED);
1311 } else {
1312 return -1;
1316 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1318 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1319 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1321 if (s390_has_feat(S390_FEAT_ZPCI)) {
1322 return pcistg_service_call(cpu, r1, r2, RA_IGNORED);
1323 } else {
1324 return -1;
1328 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1330 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1331 uint64_t fiba;
1332 uint8_t ar;
1334 if (s390_has_feat(S390_FEAT_ZPCI)) {
1335 fiba = get_base_disp_rxy(cpu, run, &ar);
1337 return stpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1338 } else {
1339 return -1;
1343 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1345 CPUS390XState *env = &cpu->env;
1346 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1347 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1348 uint8_t isc;
1349 uint16_t mode;
1350 int r;
1352 mode = env->regs[r1] & 0xffff;
1353 isc = (env->regs[r3] >> 27) & 0x7;
1354 r = css_do_sic(env, isc, mode);
1355 if (r) {
1356 kvm_s390_program_interrupt(cpu, -r);
1359 return 0;
1362 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1364 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1365 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1367 if (s390_has_feat(S390_FEAT_ZPCI)) {
1368 return rpcit_service_call(cpu, r1, r2, RA_IGNORED);
1369 } else {
1370 return -1;
1374 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1376 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1377 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1378 uint64_t gaddr;
1379 uint8_t ar;
1381 if (s390_has_feat(S390_FEAT_ZPCI)) {
1382 gaddr = get_base_disp_rsy(cpu, run, &ar);
1384 return pcistb_service_call(cpu, r1, r3, gaddr, ar, RA_IGNORED);
1385 } else {
1386 return -1;
1390 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1392 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1393 uint64_t fiba;
1394 uint8_t ar;
1396 if (s390_has_feat(S390_FEAT_ZPCI)) {
1397 fiba = get_base_disp_rxy(cpu, run, &ar);
1399 return mpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1400 } else {
1401 return -1;
1405 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1407 int r = 0;
1409 switch (ipa1) {
1410 case PRIV_B9_CLP:
1411 r = kvm_clp_service_call(cpu, run);
1412 break;
1413 case PRIV_B9_PCISTG:
1414 r = kvm_pcistg_service_call(cpu, run);
1415 break;
1416 case PRIV_B9_PCILG:
1417 r = kvm_pcilg_service_call(cpu, run);
1418 break;
1419 case PRIV_B9_RPCIT:
1420 r = kvm_rpcit_service_call(cpu, run);
1421 break;
1422 case PRIV_B9_EQBS:
1423 /* just inject exception */
1424 r = -1;
1425 break;
1426 default:
1427 r = -1;
1428 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1429 break;
1432 return r;
1435 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1437 int r = 0;
1439 switch (ipbl) {
1440 case PRIV_EB_PCISTB:
1441 r = kvm_pcistb_service_call(cpu, run);
1442 break;
1443 case PRIV_EB_SIC:
1444 r = kvm_sic_service_call(cpu, run);
1445 break;
1446 case PRIV_EB_SQBS:
1447 /* just inject exception */
1448 r = -1;
1449 break;
1450 default:
1451 r = -1;
1452 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1453 break;
1456 return r;
1459 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1461 int r = 0;
1463 switch (ipbl) {
1464 case PRIV_E3_MPCIFC:
1465 r = kvm_mpcifc_service_call(cpu, run);
1466 break;
1467 case PRIV_E3_STPCIFC:
1468 r = kvm_stpcifc_service_call(cpu, run);
1469 break;
1470 default:
1471 r = -1;
1472 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1473 break;
1476 return r;
1479 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1481 CPUS390XState *env = &cpu->env;
1482 int ret;
1484 ret = s390_virtio_hypercall(env);
1485 if (ret == -EINVAL) {
1486 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1487 return 0;
1490 return ret;
1493 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1495 uint64_t r1, r3;
1496 int rc;
1498 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1499 r3 = run->s390_sieic.ipa & 0x000f;
1500 rc = handle_diag_288(&cpu->env, r1, r3);
1501 if (rc) {
1502 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1506 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1508 uint64_t r1, r3;
1510 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1511 r3 = run->s390_sieic.ipa & 0x000f;
1512 handle_diag_308(&cpu->env, r1, r3, RA_IGNORED);
1515 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1517 CPUS390XState *env = &cpu->env;
1518 unsigned long pc;
1520 pc = env->psw.addr - sw_bp_ilen;
1521 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1522 env->psw.addr = pc;
1523 return EXCP_DEBUG;
1526 return -ENOENT;
1529 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1531 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1533 int r = 0;
1534 uint16_t func_code;
1537 * For any diagnose call we support, bits 48-63 of the resulting
1538 * address specify the function code; the remainder is ignored.
1540 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1541 switch (func_code) {
1542 case DIAG_TIMEREVENT:
1543 kvm_handle_diag_288(cpu, run);
1544 break;
1545 case DIAG_IPL:
1546 kvm_handle_diag_308(cpu, run);
1547 break;
1548 case DIAG_KVM_HYPERCALL:
1549 r = handle_hypercall(cpu, run);
1550 break;
1551 case DIAG_KVM_BREAKPOINT:
1552 r = handle_sw_breakpoint(cpu, run);
1553 break;
1554 default:
1555 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1556 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1557 break;
1560 return r;
1563 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb)
1565 CPUS390XState *env = &cpu->env;
1566 const uint8_t r1 = ipa1 >> 4;
1567 const uint8_t r3 = ipa1 & 0x0f;
1568 int ret;
1569 uint8_t order;
1571 /* get order code */
1572 order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK;
1574 ret = handle_sigp(env, order, r1, r3);
1575 setcc(cpu, ret);
1576 return 0;
1579 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1581 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1582 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1583 int r = -1;
1585 DPRINTF("handle_instruction 0x%x 0x%x\n",
1586 run->s390_sieic.ipa, run->s390_sieic.ipb);
1587 switch (ipa0) {
1588 case IPA0_B2:
1589 r = handle_b2(cpu, run, ipa1);
1590 break;
1591 case IPA0_B9:
1592 r = handle_b9(cpu, run, ipa1);
1593 break;
1594 case IPA0_EB:
1595 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1596 break;
1597 case IPA0_E3:
1598 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1599 break;
1600 case IPA0_DIAG:
1601 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1602 break;
1603 case IPA0_SIGP:
1604 r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb);
1605 break;
1608 if (r < 0) {
1609 r = 0;
1610 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1613 return r;
1616 static void unmanageable_intercept(S390CPU *cpu, S390CrashReason reason,
1617 int pswoffset)
1619 CPUState *cs = CPU(cpu);
1621 s390_cpu_halt(cpu);
1622 cpu->env.crash_reason = reason;
1623 qemu_system_guest_panicked(cpu_get_crash_info(cs));
1626 /* try to detect pgm check loops */
1627 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1629 CPUState *cs = CPU(cpu);
1630 PSW oldpsw, newpsw;
1632 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1633 offsetof(LowCore, program_new_psw));
1634 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1635 offsetof(LowCore, program_new_psw) + 8);
1636 oldpsw.mask = run->psw_mask;
1637 oldpsw.addr = run->psw_addr;
1639 * Avoid endless loops of operation exceptions, if the pgm new
1640 * PSW will cause a new operation exception.
1641 * The heuristic checks if the pgm new psw is within 6 bytes before
1642 * the faulting psw address (with same DAT, AS settings) and the
1643 * new psw is not a wait psw and the fault was not triggered by
1644 * problem state. In that case go into crashed state.
1647 if (oldpsw.addr - newpsw.addr <= 6 &&
1648 !(newpsw.mask & PSW_MASK_WAIT) &&
1649 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1650 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1651 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1652 unmanageable_intercept(cpu, S390_CRASH_REASON_OPINT_LOOP,
1653 offsetof(LowCore, program_new_psw));
1654 return EXCP_HALTED;
1656 return 0;
1659 static int handle_intercept(S390CPU *cpu)
1661 CPUState *cs = CPU(cpu);
1662 struct kvm_run *run = cs->kvm_run;
1663 int icpt_code = run->s390_sieic.icptcode;
1664 int r = 0;
1666 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1667 (long)cs->kvm_run->psw_addr);
1668 switch (icpt_code) {
1669 case ICPT_INSTRUCTION:
1670 r = handle_instruction(cpu, run);
1671 break;
1672 case ICPT_PROGRAM:
1673 unmanageable_intercept(cpu, S390_CRASH_REASON_PGMINT_LOOP,
1674 offsetof(LowCore, program_new_psw));
1675 r = EXCP_HALTED;
1676 break;
1677 case ICPT_EXT_INT:
1678 unmanageable_intercept(cpu, S390_CRASH_REASON_EXTINT_LOOP,
1679 offsetof(LowCore, external_new_psw));
1680 r = EXCP_HALTED;
1681 break;
1682 case ICPT_WAITPSW:
1683 /* disabled wait, since enabled wait is handled in kernel */
1684 s390_handle_wait(cpu);
1685 r = EXCP_HALTED;
1686 break;
1687 case ICPT_CPU_STOP:
1688 do_stop_interrupt(&cpu->env);
1689 r = EXCP_HALTED;
1690 break;
1691 case ICPT_OPEREXC:
1692 /* check for break points */
1693 r = handle_sw_breakpoint(cpu, run);
1694 if (r == -ENOENT) {
1695 /* Then check for potential pgm check loops */
1696 r = handle_oper_loop(cpu, run);
1697 if (r == 0) {
1698 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1701 break;
1702 case ICPT_SOFT_INTERCEPT:
1703 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1704 exit(1);
1705 break;
1706 case ICPT_IO:
1707 fprintf(stderr, "KVM unimplemented icpt IO\n");
1708 exit(1);
1709 break;
1710 default:
1711 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1712 exit(1);
1713 break;
1716 return r;
1719 static int handle_tsch(S390CPU *cpu)
1721 CPUState *cs = CPU(cpu);
1722 struct kvm_run *run = cs->kvm_run;
1723 int ret;
1725 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb,
1726 RA_IGNORED);
1727 if (ret < 0) {
1729 * Failure.
1730 * If an I/O interrupt had been dequeued, we have to reinject it.
1732 if (run->s390_tsch.dequeued) {
1733 s390_io_interrupt(run->s390_tsch.subchannel_id,
1734 run->s390_tsch.subchannel_nr,
1735 run->s390_tsch.io_int_parm,
1736 run->s390_tsch.io_int_word);
1738 ret = 0;
1740 return ret;
1743 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1745 SysIB_322 sysib;
1746 int del;
1748 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1749 return;
1751 /* Shift the stack of Extended Names to prepare for our own data */
1752 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1753 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1754 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1755 * assumed it's not capable of managing Extended Names for lower levels.
1757 for (del = 1; del < sysib.count; del++) {
1758 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1759 break;
1762 if (del < sysib.count) {
1763 memset(sysib.ext_names[del], 0,
1764 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1766 /* Insert short machine name in EBCDIC, padded with blanks */
1767 if (qemu_name) {
1768 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1769 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1770 strlen(qemu_name)));
1772 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1773 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1774 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1775 * considered by s390 as not capable of providing any Extended Name.
1776 * Therefore if no name was specified on qemu invocation, we go with the
1777 * same "KVMguest" default, which KVM has filled into short name field.
1779 if (qemu_name) {
1780 strncpy((char *)sysib.ext_names[0], qemu_name,
1781 sizeof(sysib.ext_names[0]));
1782 } else {
1783 strcpy((char *)sysib.ext_names[0], "KVMguest");
1785 /* Insert UUID */
1786 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
1788 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1791 static int handle_stsi(S390CPU *cpu)
1793 CPUState *cs = CPU(cpu);
1794 struct kvm_run *run = cs->kvm_run;
1796 switch (run->s390_stsi.fc) {
1797 case 3:
1798 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1799 return 0;
1801 /* Only sysib 3.2.2 needs post-handling for now. */
1802 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1803 return 0;
1804 default:
1805 return 0;
1809 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1811 CPUState *cs = CPU(cpu);
1812 struct kvm_run *run = cs->kvm_run;
1814 int ret = 0;
1815 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1817 switch (arch_info->type) {
1818 case KVM_HW_WP_WRITE:
1819 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1820 cs->watchpoint_hit = &hw_watchpoint;
1821 hw_watchpoint.vaddr = arch_info->addr;
1822 hw_watchpoint.flags = BP_MEM_WRITE;
1823 ret = EXCP_DEBUG;
1825 break;
1826 case KVM_HW_BP:
1827 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1828 ret = EXCP_DEBUG;
1830 break;
1831 case KVM_SINGLESTEP:
1832 if (cs->singlestep_enabled) {
1833 ret = EXCP_DEBUG;
1835 break;
1836 default:
1837 ret = -ENOSYS;
1840 return ret;
1843 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1845 S390CPU *cpu = S390_CPU(cs);
1846 int ret = 0;
1848 qemu_mutex_lock_iothread();
1850 kvm_cpu_synchronize_state(cs);
1852 switch (run->exit_reason) {
1853 case KVM_EXIT_S390_SIEIC:
1854 ret = handle_intercept(cpu);
1855 break;
1856 case KVM_EXIT_S390_RESET:
1857 s390_ipl_reset_request(cs, S390_RESET_REIPL);
1858 break;
1859 case KVM_EXIT_S390_TSCH:
1860 ret = handle_tsch(cpu);
1861 break;
1862 case KVM_EXIT_S390_STSI:
1863 ret = handle_stsi(cpu);
1864 break;
1865 case KVM_EXIT_DEBUG:
1866 ret = kvm_arch_handle_debug_exit(cpu);
1867 break;
1868 default:
1869 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1870 break;
1872 qemu_mutex_unlock_iothread();
1874 if (ret == 0) {
1875 ret = EXCP_INTERRUPT;
1877 return ret;
1880 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1882 return true;
1885 void kvm_s390_enable_css_support(S390CPU *cpu)
1887 int r;
1889 /* Activate host kernel channel subsystem support. */
1890 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1891 assert(r == 0);
1894 void kvm_arch_init_irq_routing(KVMState *s)
1897 * Note that while irqchip capabilities generally imply that cpustates
1898 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1899 * have to override the common code kvm_halt_in_kernel_allowed setting.
1901 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1902 kvm_gsi_routing_allowed = true;
1903 kvm_halt_in_kernel_allowed = false;
1907 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1908 int vq, bool assign)
1910 struct kvm_ioeventfd kick = {
1911 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1912 KVM_IOEVENTFD_FLAG_DATAMATCH,
1913 .fd = event_notifier_get_fd(notifier),
1914 .datamatch = vq,
1915 .addr = sch,
1916 .len = 8,
1918 trace_kvm_assign_subch_ioeventfd(kick.fd, kick.addr, assign,
1919 kick.datamatch);
1920 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1921 return -ENOSYS;
1923 if (!assign) {
1924 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1926 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1929 int kvm_s390_get_ri(void)
1931 return cap_ri;
1934 int kvm_s390_get_gs(void)
1936 return cap_gs;
1939 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1941 struct kvm_mp_state mp_state = {};
1942 int ret;
1944 /* the kvm part might not have been initialized yet */
1945 if (CPU(cpu)->kvm_state == NULL) {
1946 return 0;
1949 switch (cpu_state) {
1950 case S390_CPU_STATE_STOPPED:
1951 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1952 break;
1953 case S390_CPU_STATE_CHECK_STOP:
1954 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1955 break;
1956 case S390_CPU_STATE_OPERATING:
1957 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1958 break;
1959 case S390_CPU_STATE_LOAD:
1960 mp_state.mp_state = KVM_MP_STATE_LOAD;
1961 break;
1962 default:
1963 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1964 cpu_state);
1965 exit(1);
1968 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1969 if (ret) {
1970 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1971 strerror(-ret));
1974 return ret;
1977 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
1979 unsigned int max_cpus = MACHINE(qdev_get_machine())->smp.max_cpus;
1980 struct kvm_s390_irq_state irq_state = {
1981 .buf = (uint64_t) cpu->irqstate,
1982 .len = VCPU_IRQ_BUF_SIZE(max_cpus),
1984 CPUState *cs = CPU(cpu);
1985 int32_t bytes;
1987 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1988 return;
1991 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
1992 if (bytes < 0) {
1993 cpu->irqstate_saved_size = 0;
1994 error_report("Migration of interrupt state failed");
1995 return;
1998 cpu->irqstate_saved_size = bytes;
2001 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2003 CPUState *cs = CPU(cpu);
2004 struct kvm_s390_irq_state irq_state = {
2005 .buf = (uint64_t) cpu->irqstate,
2006 .len = cpu->irqstate_saved_size,
2008 int r;
2010 if (cpu->irqstate_saved_size == 0) {
2011 return 0;
2014 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2015 return -ENOSYS;
2018 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2019 if (r) {
2020 error_report("Setting interrupt state failed %d", r);
2022 return r;
2025 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2026 uint64_t address, uint32_t data, PCIDevice *dev)
2028 S390PCIBusDevice *pbdev;
2029 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2031 if (!dev) {
2032 DPRINTF("add_msi_route no pci device\n");
2033 return -ENODEV;
2036 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
2037 if (!pbdev) {
2038 DPRINTF("add_msi_route no zpci device\n");
2039 return -ENODEV;
2042 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2043 route->flags = 0;
2044 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2045 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2046 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2047 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
2048 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2049 return 0;
2052 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2053 int vector, PCIDevice *dev)
2055 return 0;
2058 int kvm_arch_release_virq_post(int virq)
2060 return 0;
2063 int kvm_arch_msi_data_to_gsi(uint32_t data)
2065 abort();
2068 static int query_cpu_subfunc(S390FeatBitmap features)
2070 struct kvm_s390_vm_cpu_subfunc prop;
2071 struct kvm_device_attr attr = {
2072 .group = KVM_S390_VM_CPU_MODEL,
2073 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2074 .addr = (uint64_t) &prop,
2076 int rc;
2078 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2079 if (rc) {
2080 return rc;
2084 * We're going to add all subfunctions now, if the corresponding feature
2085 * is available that unlocks the query functions.
2087 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2088 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2089 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2091 if (test_bit(S390_FEAT_MSA, features)) {
2092 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2093 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2094 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2095 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2096 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2098 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2099 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2101 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2102 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2103 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2104 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2105 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2107 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2108 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2110 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2111 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2113 if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2114 s390_add_from_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2116 if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2117 s390_add_from_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2119 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2120 s390_add_from_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2122 return 0;
2125 static int configure_cpu_subfunc(const S390FeatBitmap features)
2127 struct kvm_s390_vm_cpu_subfunc prop = {};
2128 struct kvm_device_attr attr = {
2129 .group = KVM_S390_VM_CPU_MODEL,
2130 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2131 .addr = (uint64_t) &prop,
2134 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2135 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2136 /* hardware support might be missing, IBC will handle most of this */
2137 return 0;
2140 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2141 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2142 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2144 if (test_bit(S390_FEAT_MSA, features)) {
2145 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2146 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2147 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2148 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2149 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2151 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2152 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2154 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2155 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2156 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2157 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2158 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2160 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2161 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2163 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2164 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2166 if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2167 s390_fill_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2169 if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2170 s390_fill_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2172 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2173 s390_fill_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2175 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2178 static int kvm_to_feat[][2] = {
2179 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2180 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2181 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2182 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2183 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2184 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2185 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2186 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2187 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2188 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2189 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2190 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2191 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2192 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2195 static int query_cpu_feat(S390FeatBitmap features)
2197 struct kvm_s390_vm_cpu_feat prop;
2198 struct kvm_device_attr attr = {
2199 .group = KVM_S390_VM_CPU_MODEL,
2200 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2201 .addr = (uint64_t) &prop,
2203 int rc;
2204 int i;
2206 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2207 if (rc) {
2208 return rc;
2211 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2212 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2213 set_bit(kvm_to_feat[i][1], features);
2216 return 0;
2219 static int configure_cpu_feat(const S390FeatBitmap features)
2221 struct kvm_s390_vm_cpu_feat prop = {};
2222 struct kvm_device_attr attr = {
2223 .group = KVM_S390_VM_CPU_MODEL,
2224 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2225 .addr = (uint64_t) &prop,
2227 int i;
2229 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2230 if (test_bit(kvm_to_feat[i][1], features)) {
2231 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2234 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2237 bool kvm_s390_cpu_models_supported(void)
2239 if (!cpu_model_allowed()) {
2240 /* compatibility machines interfere with the cpu model */
2241 return false;
2243 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2244 KVM_S390_VM_CPU_MACHINE) &&
2245 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2246 KVM_S390_VM_CPU_PROCESSOR) &&
2247 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2248 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2249 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2250 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2251 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2252 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2255 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2257 struct kvm_s390_vm_cpu_machine prop = {};
2258 struct kvm_device_attr attr = {
2259 .group = KVM_S390_VM_CPU_MODEL,
2260 .attr = KVM_S390_VM_CPU_MACHINE,
2261 .addr = (uint64_t) &prop,
2263 uint16_t unblocked_ibc = 0, cpu_type = 0;
2264 int rc;
2266 memset(model, 0, sizeof(*model));
2268 if (!kvm_s390_cpu_models_supported()) {
2269 error_setg(errp, "KVM doesn't support CPU models");
2270 return;
2273 /* query the basic cpu model properties */
2274 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2275 if (rc) {
2276 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2277 return;
2280 cpu_type = cpuid_type(prop.cpuid);
2281 if (has_ibc(prop.ibc)) {
2282 model->lowest_ibc = lowest_ibc(prop.ibc);
2283 unblocked_ibc = unblocked_ibc(prop.ibc);
2285 model->cpu_id = cpuid_id(prop.cpuid);
2286 model->cpu_id_format = cpuid_format(prop.cpuid);
2287 model->cpu_ver = 0xff;
2289 /* get supported cpu features indicated via STFL(E) */
2290 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2291 (uint8_t *) prop.fac_mask);
2292 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2293 if (test_bit(S390_FEAT_STFLE, model->features)) {
2294 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2296 /* get supported cpu features indicated e.g. via SCLP */
2297 rc = query_cpu_feat(model->features);
2298 if (rc) {
2299 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2300 return;
2302 /* get supported cpu subfunctions indicated via query / test bit */
2303 rc = query_cpu_subfunc(model->features);
2304 if (rc) {
2305 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2306 return;
2309 /* PTFF subfunctions might be indicated although kernel support missing */
2310 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) {
2311 clear_bit(S390_FEAT_PTFF_QSIE, model->features);
2312 clear_bit(S390_FEAT_PTFF_QTOUE, model->features);
2313 clear_bit(S390_FEAT_PTFF_STOE, model->features);
2314 clear_bit(S390_FEAT_PTFF_STOUE, model->features);
2317 /* with cpu model support, CMM is only indicated if really available */
2318 if (kvm_s390_cmma_available()) {
2319 set_bit(S390_FEAT_CMM, model->features);
2320 } else {
2321 /* no cmm -> no cmm nt */
2322 clear_bit(S390_FEAT_CMM_NT, model->features);
2325 /* bpb needs kernel support for migration, VSIE and reset */
2326 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) {
2327 clear_bit(S390_FEAT_BPB, model->features);
2330 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2331 set_bit(S390_FEAT_ZPCI, model->features);
2332 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2334 if (s390_known_cpu_type(cpu_type)) {
2335 /* we want the exact model, even if some features are missing */
2336 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2337 ibc_ec_ga(unblocked_ibc), NULL);
2338 } else {
2339 /* model unknown, e.g. too new - search using features */
2340 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2341 ibc_ec_ga(unblocked_ibc),
2342 model->features);
2344 if (!model->def) {
2345 error_setg(errp, "KVM: host CPU model could not be identified");
2346 return;
2348 /* for now, we can only provide the AP feature with HW support */
2349 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO,
2350 KVM_S390_VM_CRYPTO_ENABLE_APIE)) {
2351 set_bit(S390_FEAT_AP, model->features);
2353 /* strip of features that are not part of the maximum model */
2354 bitmap_and(model->features, model->features, model->def->full_feat,
2355 S390_FEAT_MAX);
2358 static void kvm_s390_configure_apie(bool interpret)
2360 uint64_t attr = interpret ? KVM_S390_VM_CRYPTO_ENABLE_APIE :
2361 KVM_S390_VM_CRYPTO_DISABLE_APIE;
2363 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
2364 kvm_s390_set_attr(attr);
2368 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2370 struct kvm_s390_vm_cpu_processor prop = {
2371 .fac_list = { 0 },
2373 struct kvm_device_attr attr = {
2374 .group = KVM_S390_VM_CPU_MODEL,
2375 .attr = KVM_S390_VM_CPU_PROCESSOR,
2376 .addr = (uint64_t) &prop,
2378 int rc;
2380 if (!model) {
2381 /* compatibility handling if cpu models are disabled */
2382 if (kvm_s390_cmma_available()) {
2383 kvm_s390_enable_cmma();
2385 return;
2387 if (!kvm_s390_cpu_models_supported()) {
2388 error_setg(errp, "KVM doesn't support CPU models");
2389 return;
2391 prop.cpuid = s390_cpuid_from_cpu_model(model);
2392 prop.ibc = s390_ibc_from_cpu_model(model);
2393 /* configure cpu features indicated via STFL(e) */
2394 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2395 (uint8_t *) prop.fac_list);
2396 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2397 if (rc) {
2398 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2399 return;
2401 /* configure cpu features indicated e.g. via SCLP */
2402 rc = configure_cpu_feat(model->features);
2403 if (rc) {
2404 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2405 return;
2407 /* configure cpu subfunctions indicated via query / test bit */
2408 rc = configure_cpu_subfunc(model->features);
2409 if (rc) {
2410 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2411 return;
2413 /* enable CMM via CMMA */
2414 if (test_bit(S390_FEAT_CMM, model->features)) {
2415 kvm_s390_enable_cmma();
2418 if (test_bit(S390_FEAT_AP, model->features)) {
2419 kvm_s390_configure_apie(true);
2423 void kvm_s390_restart_interrupt(S390CPU *cpu)
2425 struct kvm_s390_irq irq = {
2426 .type = KVM_S390_RESTART,
2429 kvm_s390_vcpu_interrupt(cpu, &irq);
2432 void kvm_s390_stop_interrupt(S390CPU *cpu)
2434 struct kvm_s390_irq irq = {
2435 .type = KVM_S390_SIGP_STOP,
2438 kvm_s390_vcpu_interrupt(cpu, &irq);