ARM: KVM: Check KVM_CAP_ARM_IRQ_LINE_LAYOUT_2 for smp_cpus > 256
[qemu/ar7.git] / target / s390x / kvm.c
blobc24c869e770347362eddfed34ad0746402979296
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 int kvm_arch_init(MachineState *ms, KVMState *s)
325 MachineClass *mc = MACHINE_GET_CLASS(ms);
327 mc->default_cpu_type = S390_CPU_TYPE_NAME("host");
329 if (!kvm_check_extension(kvm_state, KVM_CAP_DEVICE_CTRL)) {
330 error_report("KVM is missing capability KVM_CAP_DEVICE_CTRL - "
331 "please use kernel 3.15 or newer");
332 return -1;
335 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
336 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
337 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
338 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
340 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
341 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
342 phys_mem_set_alloc(legacy_s390_alloc);
345 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
346 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
347 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
348 if (ri_allowed()) {
349 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
350 cap_ri = 1;
353 if (cpu_model_allowed()) {
354 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
355 cap_gs = 1;
360 * The migration interface for ais was introduced with kernel 4.13
361 * but the capability itself had been active since 4.12. As migration
362 * support is considered necessary let's disable ais in the 2.10
363 * machine.
365 /* kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0); */
367 kvm_set_max_memslot_size(KVM_SLOT_MAX_BYTES);
368 return 0;
371 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
373 return 0;
376 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
378 return cpu->cpu_index;
381 int kvm_arch_init_vcpu(CPUState *cs)
383 unsigned int max_cpus = MACHINE(qdev_get_machine())->smp.max_cpus;
384 S390CPU *cpu = S390_CPU(cs);
385 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
386 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE(max_cpus));
387 return 0;
390 int kvm_arch_destroy_vcpu(CPUState *cs)
392 S390CPU *cpu = S390_CPU(cs);
394 g_free(cpu->irqstate);
395 cpu->irqstate = NULL;
397 return 0;
400 void kvm_s390_reset_vcpu(S390CPU *cpu)
402 CPUState *cs = CPU(cpu);
404 /* The initial reset call is needed here to reset in-kernel
405 * vcpu data that we can't access directly from QEMU
406 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
407 * Before this ioctl cpu_synchronize_state() is called in common kvm
408 * code (kvm-all) */
409 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
410 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
414 static int can_sync_regs(CPUState *cs, int regs)
416 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
419 int kvm_arch_put_registers(CPUState *cs, int level)
421 S390CPU *cpu = S390_CPU(cs);
422 CPUS390XState *env = &cpu->env;
423 struct kvm_sregs sregs;
424 struct kvm_regs regs;
425 struct kvm_fpu fpu = {};
426 int r;
427 int i;
429 /* always save the PSW and the GPRS*/
430 cs->kvm_run->psw_addr = env->psw.addr;
431 cs->kvm_run->psw_mask = env->psw.mask;
433 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
434 for (i = 0; i < 16; i++) {
435 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
436 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
438 } else {
439 for (i = 0; i < 16; i++) {
440 regs.gprs[i] = env->regs[i];
442 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
443 if (r < 0) {
444 return r;
448 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
449 for (i = 0; i < 32; i++) {
450 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0];
451 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1];
453 cs->kvm_run->s.regs.fpc = env->fpc;
454 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
455 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
456 for (i = 0; i < 16; i++) {
457 cs->kvm_run->s.regs.fprs[i] = *get_freg(env, i);
459 cs->kvm_run->s.regs.fpc = env->fpc;
460 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
461 } else {
462 /* Floating point */
463 for (i = 0; i < 16; i++) {
464 fpu.fprs[i] = *get_freg(env, i);
466 fpu.fpc = env->fpc;
468 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
469 if (r < 0) {
470 return r;
474 /* Do we need to save more than that? */
475 if (level == KVM_PUT_RUNTIME_STATE) {
476 return 0;
479 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
480 cs->kvm_run->s.regs.cputm = env->cputm;
481 cs->kvm_run->s.regs.ckc = env->ckc;
482 cs->kvm_run->s.regs.todpr = env->todpr;
483 cs->kvm_run->s.regs.gbea = env->gbea;
484 cs->kvm_run->s.regs.pp = env->pp;
485 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
486 } else {
488 * These ONE_REGS are not protected by a capability. As they are only
489 * necessary for migration we just trace a possible error, but don't
490 * return with an error return code.
492 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
493 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
494 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
495 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
496 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
499 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
500 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
501 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
504 /* pfault parameters */
505 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
506 cs->kvm_run->s.regs.pft = env->pfault_token;
507 cs->kvm_run->s.regs.pfs = env->pfault_select;
508 cs->kvm_run->s.regs.pfc = env->pfault_compare;
509 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
510 } else if (cap_async_pf) {
511 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
512 if (r < 0) {
513 return r;
515 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
516 if (r < 0) {
517 return r;
519 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
520 if (r < 0) {
521 return r;
525 /* access registers and control registers*/
526 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
527 for (i = 0; i < 16; i++) {
528 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
529 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
531 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
532 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
533 } else {
534 for (i = 0; i < 16; i++) {
535 sregs.acrs[i] = env->aregs[i];
536 sregs.crs[i] = env->cregs[i];
538 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
539 if (r < 0) {
540 return r;
544 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
545 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
546 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
549 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
550 cs->kvm_run->s.regs.bpbc = env->bpbc;
551 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_BPBC;
554 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
555 cs->kvm_run->s.regs.etoken = env->etoken;
556 cs->kvm_run->s.regs.etoken_extension = env->etoken_extension;
557 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ETOKEN;
560 /* Finally the prefix */
561 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
562 cs->kvm_run->s.regs.prefix = env->psa;
563 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
564 } else {
565 /* prefix is only supported via sync regs */
567 return 0;
570 int kvm_arch_get_registers(CPUState *cs)
572 S390CPU *cpu = S390_CPU(cs);
573 CPUS390XState *env = &cpu->env;
574 struct kvm_sregs sregs;
575 struct kvm_regs regs;
576 struct kvm_fpu fpu;
577 int i, r;
579 /* get the PSW */
580 env->psw.addr = cs->kvm_run->psw_addr;
581 env->psw.mask = cs->kvm_run->psw_mask;
583 /* the GPRS */
584 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
585 for (i = 0; i < 16; i++) {
586 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
588 } else {
589 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
590 if (r < 0) {
591 return r;
593 for (i = 0; i < 16; i++) {
594 env->regs[i] = regs.gprs[i];
598 /* The ACRS and CRS */
599 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
600 for (i = 0; i < 16; i++) {
601 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
602 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
604 } else {
605 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
606 if (r < 0) {
607 return r;
609 for (i = 0; i < 16; i++) {
610 env->aregs[i] = sregs.acrs[i];
611 env->cregs[i] = sregs.crs[i];
615 /* Floating point and vector registers */
616 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
617 for (i = 0; i < 32; i++) {
618 env->vregs[i][0] = cs->kvm_run->s.regs.vrs[i][0];
619 env->vregs[i][1] = cs->kvm_run->s.regs.vrs[i][1];
621 env->fpc = cs->kvm_run->s.regs.fpc;
622 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
623 for (i = 0; i < 16; i++) {
624 *get_freg(env, i) = cs->kvm_run->s.regs.fprs[i];
626 env->fpc = cs->kvm_run->s.regs.fpc;
627 } else {
628 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
629 if (r < 0) {
630 return r;
632 for (i = 0; i < 16; i++) {
633 *get_freg(env, i) = fpu.fprs[i];
635 env->fpc = fpu.fpc;
638 /* The prefix */
639 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
640 env->psa = cs->kvm_run->s.regs.prefix;
643 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
644 env->cputm = cs->kvm_run->s.regs.cputm;
645 env->ckc = cs->kvm_run->s.regs.ckc;
646 env->todpr = cs->kvm_run->s.regs.todpr;
647 env->gbea = cs->kvm_run->s.regs.gbea;
648 env->pp = cs->kvm_run->s.regs.pp;
649 } else {
651 * These ONE_REGS are not protected by a capability. As they are only
652 * necessary for migration we just trace a possible error, but don't
653 * return with an error return code.
655 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
656 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
657 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
658 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
659 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
662 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
663 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
666 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
667 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
670 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
671 env->bpbc = cs->kvm_run->s.regs.bpbc;
674 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
675 env->etoken = cs->kvm_run->s.regs.etoken;
676 env->etoken_extension = cs->kvm_run->s.regs.etoken_extension;
679 /* pfault parameters */
680 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
681 env->pfault_token = cs->kvm_run->s.regs.pft;
682 env->pfault_select = cs->kvm_run->s.regs.pfs;
683 env->pfault_compare = cs->kvm_run->s.regs.pfc;
684 } else if (cap_async_pf) {
685 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
686 if (r < 0) {
687 return r;
689 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
690 if (r < 0) {
691 return r;
693 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
694 if (r < 0) {
695 return r;
699 return 0;
702 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
704 int r;
705 struct kvm_device_attr attr = {
706 .group = KVM_S390_VM_TOD,
707 .attr = KVM_S390_VM_TOD_LOW,
708 .addr = (uint64_t)tod_low,
711 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
712 if (r) {
713 return r;
716 attr.attr = KVM_S390_VM_TOD_HIGH;
717 attr.addr = (uint64_t)tod_high;
718 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
721 int kvm_s390_get_clock_ext(uint8_t *tod_high, uint64_t *tod_low)
723 int r;
724 struct kvm_s390_vm_tod_clock gtod;
725 struct kvm_device_attr attr = {
726 .group = KVM_S390_VM_TOD,
727 .attr = KVM_S390_VM_TOD_EXT,
728 .addr = (uint64_t)&gtod,
731 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
732 *tod_high = gtod.epoch_idx;
733 *tod_low = gtod.tod;
735 return r;
738 int kvm_s390_set_clock(uint8_t tod_high, uint64_t tod_low)
740 int r;
741 struct kvm_device_attr attr = {
742 .group = KVM_S390_VM_TOD,
743 .attr = KVM_S390_VM_TOD_LOW,
744 .addr = (uint64_t)&tod_low,
747 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
748 if (r) {
749 return r;
752 attr.attr = KVM_S390_VM_TOD_HIGH;
753 attr.addr = (uint64_t)&tod_high;
754 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
757 int kvm_s390_set_clock_ext(uint8_t tod_high, uint64_t tod_low)
759 struct kvm_s390_vm_tod_clock gtod = {
760 .epoch_idx = tod_high,
761 .tod = tod_low,
763 struct kvm_device_attr attr = {
764 .group = KVM_S390_VM_TOD,
765 .attr = KVM_S390_VM_TOD_EXT,
766 .addr = (uint64_t)&gtod,
769 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
773 * kvm_s390_mem_op:
774 * @addr: the logical start address in guest memory
775 * @ar: the access register number
776 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
777 * @len: length that should be transferred
778 * @is_write: true = write, false = read
779 * Returns: 0 on success, non-zero if an exception or error occurred
781 * Use KVM ioctl to read/write from/to guest memory. An access exception
782 * is injected into the vCPU in case of translation errors.
784 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
785 int len, bool is_write)
787 struct kvm_s390_mem_op mem_op = {
788 .gaddr = addr,
789 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
790 .size = len,
791 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
792 : KVM_S390_MEMOP_LOGICAL_READ,
793 .buf = (uint64_t)hostbuf,
794 .ar = ar,
796 int ret;
798 if (!cap_mem_op) {
799 return -ENOSYS;
801 if (!hostbuf) {
802 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
805 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
806 if (ret < 0) {
807 warn_report("KVM_S390_MEM_OP failed: %s", strerror(-ret));
809 return ret;
813 * Legacy layout for s390:
814 * Older S390 KVM requires the topmost vma of the RAM to be
815 * smaller than an system defined value, which is at least 256GB.
816 * Larger systems have larger values. We put the guest between
817 * the end of data segment (system break) and this value. We
818 * use 32GB as a base to have enough room for the system break
819 * to grow. We also have to use MAP parameters that avoid
820 * read-only mapping of guest pages.
822 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared)
824 static void *mem;
826 if (mem) {
827 /* we only support one allocation, which is enough for initial ram */
828 return NULL;
831 mem = mmap((void *) 0x800000000ULL, size,
832 PROT_EXEC|PROT_READ|PROT_WRITE,
833 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
834 if (mem == MAP_FAILED) {
835 mem = NULL;
837 if (mem && align) {
838 *align = QEMU_VMALLOC_ALIGN;
840 return mem;
843 static uint8_t const *sw_bp_inst;
844 static uint8_t sw_bp_ilen;
846 static void determine_sw_breakpoint_instr(void)
848 /* DIAG 501 is used for sw breakpoints with old kernels */
849 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
850 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
851 static const uint8_t instr_0x0000[] = {0x00, 0x00};
853 if (sw_bp_inst) {
854 return;
856 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
857 sw_bp_inst = diag_501;
858 sw_bp_ilen = sizeof(diag_501);
859 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
860 } else {
861 sw_bp_inst = instr_0x0000;
862 sw_bp_ilen = sizeof(instr_0x0000);
863 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
867 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
869 determine_sw_breakpoint_instr();
871 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
872 sw_bp_ilen, 0) ||
873 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
874 return -EINVAL;
876 return 0;
879 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
881 uint8_t t[MAX_ILEN];
883 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
884 return -EINVAL;
885 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
886 return -EINVAL;
887 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
888 sw_bp_ilen, 1)) {
889 return -EINVAL;
892 return 0;
895 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
896 int len, int type)
898 int n;
900 for (n = 0; n < nb_hw_breakpoints; n++) {
901 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
902 (hw_breakpoints[n].len == len || len == -1)) {
903 return &hw_breakpoints[n];
907 return NULL;
910 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
912 int size;
914 if (find_hw_breakpoint(addr, len, type)) {
915 return -EEXIST;
918 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
920 if (!hw_breakpoints) {
921 nb_hw_breakpoints = 0;
922 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
923 } else {
924 hw_breakpoints =
925 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
928 if (!hw_breakpoints) {
929 nb_hw_breakpoints = 0;
930 return -ENOMEM;
933 hw_breakpoints[nb_hw_breakpoints].addr = addr;
934 hw_breakpoints[nb_hw_breakpoints].len = len;
935 hw_breakpoints[nb_hw_breakpoints].type = type;
937 nb_hw_breakpoints++;
939 return 0;
942 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
943 target_ulong len, int type)
945 switch (type) {
946 case GDB_BREAKPOINT_HW:
947 type = KVM_HW_BP;
948 break;
949 case GDB_WATCHPOINT_WRITE:
950 if (len < 1) {
951 return -EINVAL;
953 type = KVM_HW_WP_WRITE;
954 break;
955 default:
956 return -ENOSYS;
958 return insert_hw_breakpoint(addr, len, type);
961 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
962 target_ulong len, int type)
964 int size;
965 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
967 if (bp == NULL) {
968 return -ENOENT;
971 nb_hw_breakpoints--;
972 if (nb_hw_breakpoints > 0) {
974 * In order to trim the array, move the last element to the position to
975 * be removed - if necessary.
977 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
978 *bp = hw_breakpoints[nb_hw_breakpoints];
980 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
981 hw_breakpoints =
982 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
983 } else {
984 g_free(hw_breakpoints);
985 hw_breakpoints = NULL;
988 return 0;
991 void kvm_arch_remove_all_hw_breakpoints(void)
993 nb_hw_breakpoints = 0;
994 g_free(hw_breakpoints);
995 hw_breakpoints = NULL;
998 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
1000 int i;
1002 if (nb_hw_breakpoints > 0) {
1003 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
1004 dbg->arch.hw_bp = hw_breakpoints;
1006 for (i = 0; i < nb_hw_breakpoints; ++i) {
1007 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
1008 hw_breakpoints[i].addr);
1010 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
1011 } else {
1012 dbg->arch.nr_hw_bp = 0;
1013 dbg->arch.hw_bp = NULL;
1017 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
1021 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1023 return MEMTXATTRS_UNSPECIFIED;
1026 int kvm_arch_process_async_events(CPUState *cs)
1028 return cs->halted;
1031 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
1032 struct kvm_s390_interrupt *interrupt)
1034 int r = 0;
1036 interrupt->type = irq->type;
1037 switch (irq->type) {
1038 case KVM_S390_INT_VIRTIO:
1039 interrupt->parm = irq->u.ext.ext_params;
1040 /* fall through */
1041 case KVM_S390_INT_PFAULT_INIT:
1042 case KVM_S390_INT_PFAULT_DONE:
1043 interrupt->parm64 = irq->u.ext.ext_params2;
1044 break;
1045 case KVM_S390_PROGRAM_INT:
1046 interrupt->parm = irq->u.pgm.code;
1047 break;
1048 case KVM_S390_SIGP_SET_PREFIX:
1049 interrupt->parm = irq->u.prefix.address;
1050 break;
1051 case KVM_S390_INT_SERVICE:
1052 interrupt->parm = irq->u.ext.ext_params;
1053 break;
1054 case KVM_S390_MCHK:
1055 interrupt->parm = irq->u.mchk.cr14;
1056 interrupt->parm64 = irq->u.mchk.mcic;
1057 break;
1058 case KVM_S390_INT_EXTERNAL_CALL:
1059 interrupt->parm = irq->u.extcall.code;
1060 break;
1061 case KVM_S390_INT_EMERGENCY:
1062 interrupt->parm = irq->u.emerg.code;
1063 break;
1064 case KVM_S390_SIGP_STOP:
1065 case KVM_S390_RESTART:
1066 break; /* These types have no parameters */
1067 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1068 interrupt->parm = irq->u.io.subchannel_id << 16;
1069 interrupt->parm |= irq->u.io.subchannel_nr;
1070 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
1071 interrupt->parm64 |= irq->u.io.io_int_word;
1072 break;
1073 default:
1074 r = -EINVAL;
1075 break;
1077 return r;
1080 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
1082 struct kvm_s390_interrupt kvmint = {};
1083 int r;
1085 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1086 if (r < 0) {
1087 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1088 exit(1);
1091 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
1092 if (r < 0) {
1093 fprintf(stderr, "KVM failed to inject interrupt\n");
1094 exit(1);
1098 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
1100 CPUState *cs = CPU(cpu);
1101 int r;
1103 if (cap_s390_irq) {
1104 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
1105 if (!r) {
1106 return;
1108 error_report("KVM failed to inject interrupt %llx", irq->type);
1109 exit(1);
1112 inject_vcpu_irq_legacy(cs, irq);
1115 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq *irq)
1117 struct kvm_s390_interrupt kvmint = {};
1118 int r;
1120 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1121 if (r < 0) {
1122 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1123 exit(1);
1126 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1127 if (r < 0) {
1128 fprintf(stderr, "KVM failed to inject interrupt\n");
1129 exit(1);
1133 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1135 struct kvm_s390_irq irq = {
1136 .type = KVM_S390_PROGRAM_INT,
1137 .u.pgm.code = code,
1139 qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
1140 cpu->env.psw.addr);
1141 kvm_s390_vcpu_interrupt(cpu, &irq);
1144 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1146 struct kvm_s390_irq irq = {
1147 .type = KVM_S390_PROGRAM_INT,
1148 .u.pgm.code = code,
1149 .u.pgm.trans_exc_code = te_code,
1150 .u.pgm.exc_access_id = te_code & 3,
1153 kvm_s390_vcpu_interrupt(cpu, &irq);
1156 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1157 uint16_t ipbh0)
1159 CPUS390XState *env = &cpu->env;
1160 uint64_t sccb;
1161 uint32_t code;
1162 int r = 0;
1164 sccb = env->regs[ipbh0 & 0xf];
1165 code = env->regs[(ipbh0 & 0xf0) >> 4];
1167 r = sclp_service_call(env, sccb, code);
1168 if (r < 0) {
1169 kvm_s390_program_interrupt(cpu, -r);
1170 } else {
1171 setcc(cpu, r);
1174 return 0;
1177 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1179 CPUS390XState *env = &cpu->env;
1180 int rc = 0;
1181 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1183 switch (ipa1) {
1184 case PRIV_B2_XSCH:
1185 ioinst_handle_xsch(cpu, env->regs[1], RA_IGNORED);
1186 break;
1187 case PRIV_B2_CSCH:
1188 ioinst_handle_csch(cpu, env->regs[1], RA_IGNORED);
1189 break;
1190 case PRIV_B2_HSCH:
1191 ioinst_handle_hsch(cpu, env->regs[1], RA_IGNORED);
1192 break;
1193 case PRIV_B2_MSCH:
1194 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1195 break;
1196 case PRIV_B2_SSCH:
1197 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1198 break;
1199 case PRIV_B2_STCRW:
1200 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb, RA_IGNORED);
1201 break;
1202 case PRIV_B2_STSCH:
1203 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1204 break;
1205 case PRIV_B2_TSCH:
1206 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1207 fprintf(stderr, "Spurious tsch intercept\n");
1208 break;
1209 case PRIV_B2_CHSC:
1210 ioinst_handle_chsc(cpu, run->s390_sieic.ipb, RA_IGNORED);
1211 break;
1212 case PRIV_B2_TPI:
1213 /* This should have been handled by kvm already. */
1214 fprintf(stderr, "Spurious tpi intercept\n");
1215 break;
1216 case PRIV_B2_SCHM:
1217 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1218 run->s390_sieic.ipb, RA_IGNORED);
1219 break;
1220 case PRIV_B2_RSCH:
1221 ioinst_handle_rsch(cpu, env->regs[1], RA_IGNORED);
1222 break;
1223 case PRIV_B2_RCHP:
1224 ioinst_handle_rchp(cpu, env->regs[1], RA_IGNORED);
1225 break;
1226 case PRIV_B2_STCPS:
1227 /* We do not provide this instruction, it is suppressed. */
1228 break;
1229 case PRIV_B2_SAL:
1230 ioinst_handle_sal(cpu, env->regs[1], RA_IGNORED);
1231 break;
1232 case PRIV_B2_SIGA:
1233 /* Not provided, set CC = 3 for subchannel not operational */
1234 setcc(cpu, 3);
1235 break;
1236 case PRIV_B2_SCLP_CALL:
1237 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1238 break;
1239 default:
1240 rc = -1;
1241 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1242 break;
1245 return rc;
1248 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1249 uint8_t *ar)
1251 CPUS390XState *env = &cpu->env;
1252 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1253 uint32_t base2 = run->s390_sieic.ipb >> 28;
1254 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1255 ((run->s390_sieic.ipb & 0xff00) << 4);
1257 if (disp2 & 0x80000) {
1258 disp2 += 0xfff00000;
1260 if (ar) {
1261 *ar = base2;
1264 return (base2 ? env->regs[base2] : 0) +
1265 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1268 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1269 uint8_t *ar)
1271 CPUS390XState *env = &cpu->env;
1272 uint32_t base2 = run->s390_sieic.ipb >> 28;
1273 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1274 ((run->s390_sieic.ipb & 0xff00) << 4);
1276 if (disp2 & 0x80000) {
1277 disp2 += 0xfff00000;
1279 if (ar) {
1280 *ar = base2;
1283 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1286 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1288 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1290 if (s390_has_feat(S390_FEAT_ZPCI)) {
1291 return clp_service_call(cpu, r2, RA_IGNORED);
1292 } else {
1293 return -1;
1297 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1299 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1300 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1302 if (s390_has_feat(S390_FEAT_ZPCI)) {
1303 return pcilg_service_call(cpu, r1, r2, RA_IGNORED);
1304 } else {
1305 return -1;
1309 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1311 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1312 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1314 if (s390_has_feat(S390_FEAT_ZPCI)) {
1315 return pcistg_service_call(cpu, r1, r2, RA_IGNORED);
1316 } else {
1317 return -1;
1321 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1323 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1324 uint64_t fiba;
1325 uint8_t ar;
1327 if (s390_has_feat(S390_FEAT_ZPCI)) {
1328 fiba = get_base_disp_rxy(cpu, run, &ar);
1330 return stpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1331 } else {
1332 return -1;
1336 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1338 CPUS390XState *env = &cpu->env;
1339 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1340 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1341 uint8_t isc;
1342 uint16_t mode;
1343 int r;
1345 mode = env->regs[r1] & 0xffff;
1346 isc = (env->regs[r3] >> 27) & 0x7;
1347 r = css_do_sic(env, isc, mode);
1348 if (r) {
1349 kvm_s390_program_interrupt(cpu, -r);
1352 return 0;
1355 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1357 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1358 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1360 if (s390_has_feat(S390_FEAT_ZPCI)) {
1361 return rpcit_service_call(cpu, r1, r2, RA_IGNORED);
1362 } else {
1363 return -1;
1367 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1369 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1370 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1371 uint64_t gaddr;
1372 uint8_t ar;
1374 if (s390_has_feat(S390_FEAT_ZPCI)) {
1375 gaddr = get_base_disp_rsy(cpu, run, &ar);
1377 return pcistb_service_call(cpu, r1, r3, gaddr, ar, RA_IGNORED);
1378 } else {
1379 return -1;
1383 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1385 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1386 uint64_t fiba;
1387 uint8_t ar;
1389 if (s390_has_feat(S390_FEAT_ZPCI)) {
1390 fiba = get_base_disp_rxy(cpu, run, &ar);
1392 return mpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1393 } else {
1394 return -1;
1398 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1400 int r = 0;
1402 switch (ipa1) {
1403 case PRIV_B9_CLP:
1404 r = kvm_clp_service_call(cpu, run);
1405 break;
1406 case PRIV_B9_PCISTG:
1407 r = kvm_pcistg_service_call(cpu, run);
1408 break;
1409 case PRIV_B9_PCILG:
1410 r = kvm_pcilg_service_call(cpu, run);
1411 break;
1412 case PRIV_B9_RPCIT:
1413 r = kvm_rpcit_service_call(cpu, run);
1414 break;
1415 case PRIV_B9_EQBS:
1416 /* just inject exception */
1417 r = -1;
1418 break;
1419 default:
1420 r = -1;
1421 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1422 break;
1425 return r;
1428 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1430 int r = 0;
1432 switch (ipbl) {
1433 case PRIV_EB_PCISTB:
1434 r = kvm_pcistb_service_call(cpu, run);
1435 break;
1436 case PRIV_EB_SIC:
1437 r = kvm_sic_service_call(cpu, run);
1438 break;
1439 case PRIV_EB_SQBS:
1440 /* just inject exception */
1441 r = -1;
1442 break;
1443 default:
1444 r = -1;
1445 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1446 break;
1449 return r;
1452 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1454 int r = 0;
1456 switch (ipbl) {
1457 case PRIV_E3_MPCIFC:
1458 r = kvm_mpcifc_service_call(cpu, run);
1459 break;
1460 case PRIV_E3_STPCIFC:
1461 r = kvm_stpcifc_service_call(cpu, run);
1462 break;
1463 default:
1464 r = -1;
1465 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1466 break;
1469 return r;
1472 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1474 CPUS390XState *env = &cpu->env;
1475 int ret;
1477 ret = s390_virtio_hypercall(env);
1478 if (ret == -EINVAL) {
1479 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1480 return 0;
1483 return ret;
1486 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1488 uint64_t r1, r3;
1489 int rc;
1491 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1492 r3 = run->s390_sieic.ipa & 0x000f;
1493 rc = handle_diag_288(&cpu->env, r1, r3);
1494 if (rc) {
1495 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1499 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1501 uint64_t r1, r3;
1503 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1504 r3 = run->s390_sieic.ipa & 0x000f;
1505 handle_diag_308(&cpu->env, r1, r3, RA_IGNORED);
1508 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1510 CPUS390XState *env = &cpu->env;
1511 unsigned long pc;
1513 pc = env->psw.addr - sw_bp_ilen;
1514 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1515 env->psw.addr = pc;
1516 return EXCP_DEBUG;
1519 return -ENOENT;
1522 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1524 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1526 int r = 0;
1527 uint16_t func_code;
1530 * For any diagnose call we support, bits 48-63 of the resulting
1531 * address specify the function code; the remainder is ignored.
1533 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1534 switch (func_code) {
1535 case DIAG_TIMEREVENT:
1536 kvm_handle_diag_288(cpu, run);
1537 break;
1538 case DIAG_IPL:
1539 kvm_handle_diag_308(cpu, run);
1540 break;
1541 case DIAG_KVM_HYPERCALL:
1542 r = handle_hypercall(cpu, run);
1543 break;
1544 case DIAG_KVM_BREAKPOINT:
1545 r = handle_sw_breakpoint(cpu, run);
1546 break;
1547 default:
1548 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1549 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1550 break;
1553 return r;
1556 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb)
1558 CPUS390XState *env = &cpu->env;
1559 const uint8_t r1 = ipa1 >> 4;
1560 const uint8_t r3 = ipa1 & 0x0f;
1561 int ret;
1562 uint8_t order;
1564 /* get order code */
1565 order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK;
1567 ret = handle_sigp(env, order, r1, r3);
1568 setcc(cpu, ret);
1569 return 0;
1572 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1574 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1575 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1576 int r = -1;
1578 DPRINTF("handle_instruction 0x%x 0x%x\n",
1579 run->s390_sieic.ipa, run->s390_sieic.ipb);
1580 switch (ipa0) {
1581 case IPA0_B2:
1582 r = handle_b2(cpu, run, ipa1);
1583 break;
1584 case IPA0_B9:
1585 r = handle_b9(cpu, run, ipa1);
1586 break;
1587 case IPA0_EB:
1588 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1589 break;
1590 case IPA0_E3:
1591 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1592 break;
1593 case IPA0_DIAG:
1594 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1595 break;
1596 case IPA0_SIGP:
1597 r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb);
1598 break;
1601 if (r < 0) {
1602 r = 0;
1603 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1606 return r;
1609 static void unmanageable_intercept(S390CPU *cpu, S390CrashReason reason,
1610 int pswoffset)
1612 CPUState *cs = CPU(cpu);
1614 s390_cpu_halt(cpu);
1615 cpu->env.crash_reason = reason;
1616 qemu_system_guest_panicked(cpu_get_crash_info(cs));
1619 /* try to detect pgm check loops */
1620 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1622 CPUState *cs = CPU(cpu);
1623 PSW oldpsw, newpsw;
1625 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1626 offsetof(LowCore, program_new_psw));
1627 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1628 offsetof(LowCore, program_new_psw) + 8);
1629 oldpsw.mask = run->psw_mask;
1630 oldpsw.addr = run->psw_addr;
1632 * Avoid endless loops of operation exceptions, if the pgm new
1633 * PSW will cause a new operation exception.
1634 * The heuristic checks if the pgm new psw is within 6 bytes before
1635 * the faulting psw address (with same DAT, AS settings) and the
1636 * new psw is not a wait psw and the fault was not triggered by
1637 * problem state. In that case go into crashed state.
1640 if (oldpsw.addr - newpsw.addr <= 6 &&
1641 !(newpsw.mask & PSW_MASK_WAIT) &&
1642 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1643 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1644 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1645 unmanageable_intercept(cpu, S390_CRASH_REASON_OPINT_LOOP,
1646 offsetof(LowCore, program_new_psw));
1647 return EXCP_HALTED;
1649 return 0;
1652 static int handle_intercept(S390CPU *cpu)
1654 CPUState *cs = CPU(cpu);
1655 struct kvm_run *run = cs->kvm_run;
1656 int icpt_code = run->s390_sieic.icptcode;
1657 int r = 0;
1659 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1660 (long)cs->kvm_run->psw_addr);
1661 switch (icpt_code) {
1662 case ICPT_INSTRUCTION:
1663 r = handle_instruction(cpu, run);
1664 break;
1665 case ICPT_PROGRAM:
1666 unmanageable_intercept(cpu, S390_CRASH_REASON_PGMINT_LOOP,
1667 offsetof(LowCore, program_new_psw));
1668 r = EXCP_HALTED;
1669 break;
1670 case ICPT_EXT_INT:
1671 unmanageable_intercept(cpu, S390_CRASH_REASON_EXTINT_LOOP,
1672 offsetof(LowCore, external_new_psw));
1673 r = EXCP_HALTED;
1674 break;
1675 case ICPT_WAITPSW:
1676 /* disabled wait, since enabled wait is handled in kernel */
1677 s390_handle_wait(cpu);
1678 r = EXCP_HALTED;
1679 break;
1680 case ICPT_CPU_STOP:
1681 do_stop_interrupt(&cpu->env);
1682 r = EXCP_HALTED;
1683 break;
1684 case ICPT_OPEREXC:
1685 /* check for break points */
1686 r = handle_sw_breakpoint(cpu, run);
1687 if (r == -ENOENT) {
1688 /* Then check for potential pgm check loops */
1689 r = handle_oper_loop(cpu, run);
1690 if (r == 0) {
1691 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1694 break;
1695 case ICPT_SOFT_INTERCEPT:
1696 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1697 exit(1);
1698 break;
1699 case ICPT_IO:
1700 fprintf(stderr, "KVM unimplemented icpt IO\n");
1701 exit(1);
1702 break;
1703 default:
1704 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1705 exit(1);
1706 break;
1709 return r;
1712 static int handle_tsch(S390CPU *cpu)
1714 CPUState *cs = CPU(cpu);
1715 struct kvm_run *run = cs->kvm_run;
1716 int ret;
1718 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb,
1719 RA_IGNORED);
1720 if (ret < 0) {
1722 * Failure.
1723 * If an I/O interrupt had been dequeued, we have to reinject it.
1725 if (run->s390_tsch.dequeued) {
1726 s390_io_interrupt(run->s390_tsch.subchannel_id,
1727 run->s390_tsch.subchannel_nr,
1728 run->s390_tsch.io_int_parm,
1729 run->s390_tsch.io_int_word);
1731 ret = 0;
1733 return ret;
1736 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1738 SysIB_322 sysib;
1739 int del;
1741 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1742 return;
1744 /* Shift the stack of Extended Names to prepare for our own data */
1745 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1746 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1747 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1748 * assumed it's not capable of managing Extended Names for lower levels.
1750 for (del = 1; del < sysib.count; del++) {
1751 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1752 break;
1755 if (del < sysib.count) {
1756 memset(sysib.ext_names[del], 0,
1757 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1759 /* Insert short machine name in EBCDIC, padded with blanks */
1760 if (qemu_name) {
1761 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1762 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1763 strlen(qemu_name)));
1765 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1766 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1767 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1768 * considered by s390 as not capable of providing any Extended Name.
1769 * Therefore if no name was specified on qemu invocation, we go with the
1770 * same "KVMguest" default, which KVM has filled into short name field.
1772 if (qemu_name) {
1773 strncpy((char *)sysib.ext_names[0], qemu_name,
1774 sizeof(sysib.ext_names[0]));
1775 } else {
1776 strcpy((char *)sysib.ext_names[0], "KVMguest");
1778 /* Insert UUID */
1779 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
1781 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1784 static int handle_stsi(S390CPU *cpu)
1786 CPUState *cs = CPU(cpu);
1787 struct kvm_run *run = cs->kvm_run;
1789 switch (run->s390_stsi.fc) {
1790 case 3:
1791 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1792 return 0;
1794 /* Only sysib 3.2.2 needs post-handling for now. */
1795 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1796 return 0;
1797 default:
1798 return 0;
1802 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1804 CPUState *cs = CPU(cpu);
1805 struct kvm_run *run = cs->kvm_run;
1807 int ret = 0;
1808 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1810 switch (arch_info->type) {
1811 case KVM_HW_WP_WRITE:
1812 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1813 cs->watchpoint_hit = &hw_watchpoint;
1814 hw_watchpoint.vaddr = arch_info->addr;
1815 hw_watchpoint.flags = BP_MEM_WRITE;
1816 ret = EXCP_DEBUG;
1818 break;
1819 case KVM_HW_BP:
1820 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1821 ret = EXCP_DEBUG;
1823 break;
1824 case KVM_SINGLESTEP:
1825 if (cs->singlestep_enabled) {
1826 ret = EXCP_DEBUG;
1828 break;
1829 default:
1830 ret = -ENOSYS;
1833 return ret;
1836 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1838 S390CPU *cpu = S390_CPU(cs);
1839 int ret = 0;
1841 qemu_mutex_lock_iothread();
1843 kvm_cpu_synchronize_state(cs);
1845 switch (run->exit_reason) {
1846 case KVM_EXIT_S390_SIEIC:
1847 ret = handle_intercept(cpu);
1848 break;
1849 case KVM_EXIT_S390_RESET:
1850 s390_ipl_reset_request(cs, S390_RESET_REIPL);
1851 break;
1852 case KVM_EXIT_S390_TSCH:
1853 ret = handle_tsch(cpu);
1854 break;
1855 case KVM_EXIT_S390_STSI:
1856 ret = handle_stsi(cpu);
1857 break;
1858 case KVM_EXIT_DEBUG:
1859 ret = kvm_arch_handle_debug_exit(cpu);
1860 break;
1861 default:
1862 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1863 break;
1865 qemu_mutex_unlock_iothread();
1867 if (ret == 0) {
1868 ret = EXCP_INTERRUPT;
1870 return ret;
1873 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1875 return true;
1878 void kvm_s390_enable_css_support(S390CPU *cpu)
1880 int r;
1882 /* Activate host kernel channel subsystem support. */
1883 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1884 assert(r == 0);
1887 void kvm_arch_init_irq_routing(KVMState *s)
1890 * Note that while irqchip capabilities generally imply that cpustates
1891 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1892 * have to override the common code kvm_halt_in_kernel_allowed setting.
1894 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1895 kvm_gsi_routing_allowed = true;
1896 kvm_halt_in_kernel_allowed = false;
1900 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1901 int vq, bool assign)
1903 struct kvm_ioeventfd kick = {
1904 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1905 KVM_IOEVENTFD_FLAG_DATAMATCH,
1906 .fd = event_notifier_get_fd(notifier),
1907 .datamatch = vq,
1908 .addr = sch,
1909 .len = 8,
1911 trace_kvm_assign_subch_ioeventfd(kick.fd, kick.addr, assign,
1912 kick.datamatch);
1913 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1914 return -ENOSYS;
1916 if (!assign) {
1917 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1919 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1922 int kvm_s390_get_ri(void)
1924 return cap_ri;
1927 int kvm_s390_get_gs(void)
1929 return cap_gs;
1932 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1934 struct kvm_mp_state mp_state = {};
1935 int ret;
1937 /* the kvm part might not have been initialized yet */
1938 if (CPU(cpu)->kvm_state == NULL) {
1939 return 0;
1942 switch (cpu_state) {
1943 case S390_CPU_STATE_STOPPED:
1944 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1945 break;
1946 case S390_CPU_STATE_CHECK_STOP:
1947 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1948 break;
1949 case S390_CPU_STATE_OPERATING:
1950 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1951 break;
1952 case S390_CPU_STATE_LOAD:
1953 mp_state.mp_state = KVM_MP_STATE_LOAD;
1954 break;
1955 default:
1956 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1957 cpu_state);
1958 exit(1);
1961 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1962 if (ret) {
1963 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1964 strerror(-ret));
1967 return ret;
1970 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
1972 unsigned int max_cpus = MACHINE(qdev_get_machine())->smp.max_cpus;
1973 struct kvm_s390_irq_state irq_state = {
1974 .buf = (uint64_t) cpu->irqstate,
1975 .len = VCPU_IRQ_BUF_SIZE(max_cpus),
1977 CPUState *cs = CPU(cpu);
1978 int32_t bytes;
1980 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1981 return;
1984 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
1985 if (bytes < 0) {
1986 cpu->irqstate_saved_size = 0;
1987 error_report("Migration of interrupt state failed");
1988 return;
1991 cpu->irqstate_saved_size = bytes;
1994 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
1996 CPUState *cs = CPU(cpu);
1997 struct kvm_s390_irq_state irq_state = {
1998 .buf = (uint64_t) cpu->irqstate,
1999 .len = cpu->irqstate_saved_size,
2001 int r;
2003 if (cpu->irqstate_saved_size == 0) {
2004 return 0;
2007 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2008 return -ENOSYS;
2011 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2012 if (r) {
2013 error_report("Setting interrupt state failed %d", r);
2015 return r;
2018 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2019 uint64_t address, uint32_t data, PCIDevice *dev)
2021 S390PCIBusDevice *pbdev;
2022 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2024 if (!dev) {
2025 DPRINTF("add_msi_route no pci device\n");
2026 return -ENODEV;
2029 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
2030 if (!pbdev) {
2031 DPRINTF("add_msi_route no zpci device\n");
2032 return -ENODEV;
2035 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2036 route->flags = 0;
2037 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2038 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2039 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2040 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
2041 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2042 return 0;
2045 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2046 int vector, PCIDevice *dev)
2048 return 0;
2051 int kvm_arch_release_virq_post(int virq)
2053 return 0;
2056 int kvm_arch_msi_data_to_gsi(uint32_t data)
2058 abort();
2061 static int query_cpu_subfunc(S390FeatBitmap features)
2063 struct kvm_s390_vm_cpu_subfunc prop;
2064 struct kvm_device_attr attr = {
2065 .group = KVM_S390_VM_CPU_MODEL,
2066 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2067 .addr = (uint64_t) &prop,
2069 int rc;
2071 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2072 if (rc) {
2073 return rc;
2077 * We're going to add all subfunctions now, if the corresponding feature
2078 * is available that unlocks the query functions.
2080 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2081 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2082 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2084 if (test_bit(S390_FEAT_MSA, features)) {
2085 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2086 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2087 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2088 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2089 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2091 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2092 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2094 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2095 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2096 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2097 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2098 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2100 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2101 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2103 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2104 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2106 if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2107 s390_add_from_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2109 if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2110 s390_add_from_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2112 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2113 s390_add_from_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2115 return 0;
2118 static int configure_cpu_subfunc(const S390FeatBitmap features)
2120 struct kvm_s390_vm_cpu_subfunc prop = {};
2121 struct kvm_device_attr attr = {
2122 .group = KVM_S390_VM_CPU_MODEL,
2123 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2124 .addr = (uint64_t) &prop,
2127 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2128 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2129 /* hardware support might be missing, IBC will handle most of this */
2130 return 0;
2133 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2134 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2135 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2137 if (test_bit(S390_FEAT_MSA, features)) {
2138 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2139 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2140 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2141 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2142 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2144 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2145 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2147 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2148 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2149 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2150 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2151 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2153 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2154 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2156 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2157 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2159 if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2160 s390_fill_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2162 if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2163 s390_fill_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2165 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2166 s390_fill_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2168 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2171 static int kvm_to_feat[][2] = {
2172 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2173 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2174 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2175 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2176 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2177 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2178 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2179 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2180 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2181 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2182 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2183 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2184 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2185 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2188 static int query_cpu_feat(S390FeatBitmap features)
2190 struct kvm_s390_vm_cpu_feat prop;
2191 struct kvm_device_attr attr = {
2192 .group = KVM_S390_VM_CPU_MODEL,
2193 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2194 .addr = (uint64_t) &prop,
2196 int rc;
2197 int i;
2199 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2200 if (rc) {
2201 return rc;
2204 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2205 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2206 set_bit(kvm_to_feat[i][1], features);
2209 return 0;
2212 static int configure_cpu_feat(const S390FeatBitmap features)
2214 struct kvm_s390_vm_cpu_feat prop = {};
2215 struct kvm_device_attr attr = {
2216 .group = KVM_S390_VM_CPU_MODEL,
2217 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2218 .addr = (uint64_t) &prop,
2220 int i;
2222 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2223 if (test_bit(kvm_to_feat[i][1], features)) {
2224 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2227 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2230 bool kvm_s390_cpu_models_supported(void)
2232 if (!cpu_model_allowed()) {
2233 /* compatibility machines interfere with the cpu model */
2234 return false;
2236 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2237 KVM_S390_VM_CPU_MACHINE) &&
2238 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2239 KVM_S390_VM_CPU_PROCESSOR) &&
2240 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2241 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2242 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2243 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2244 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2245 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2248 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2250 struct kvm_s390_vm_cpu_machine prop = {};
2251 struct kvm_device_attr attr = {
2252 .group = KVM_S390_VM_CPU_MODEL,
2253 .attr = KVM_S390_VM_CPU_MACHINE,
2254 .addr = (uint64_t) &prop,
2256 uint16_t unblocked_ibc = 0, cpu_type = 0;
2257 int rc;
2259 memset(model, 0, sizeof(*model));
2261 if (!kvm_s390_cpu_models_supported()) {
2262 error_setg(errp, "KVM doesn't support CPU models");
2263 return;
2266 /* query the basic cpu model properties */
2267 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2268 if (rc) {
2269 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2270 return;
2273 cpu_type = cpuid_type(prop.cpuid);
2274 if (has_ibc(prop.ibc)) {
2275 model->lowest_ibc = lowest_ibc(prop.ibc);
2276 unblocked_ibc = unblocked_ibc(prop.ibc);
2278 model->cpu_id = cpuid_id(prop.cpuid);
2279 model->cpu_id_format = cpuid_format(prop.cpuid);
2280 model->cpu_ver = 0xff;
2282 /* get supported cpu features indicated via STFL(E) */
2283 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2284 (uint8_t *) prop.fac_mask);
2285 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2286 if (test_bit(S390_FEAT_STFLE, model->features)) {
2287 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2289 /* get supported cpu features indicated e.g. via SCLP */
2290 rc = query_cpu_feat(model->features);
2291 if (rc) {
2292 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2293 return;
2295 /* get supported cpu subfunctions indicated via query / test bit */
2296 rc = query_cpu_subfunc(model->features);
2297 if (rc) {
2298 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2299 return;
2302 /* PTFF subfunctions might be indicated although kernel support missing */
2303 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) {
2304 clear_bit(S390_FEAT_PTFF_QSIE, model->features);
2305 clear_bit(S390_FEAT_PTFF_QTOUE, model->features);
2306 clear_bit(S390_FEAT_PTFF_STOE, model->features);
2307 clear_bit(S390_FEAT_PTFF_STOUE, model->features);
2310 /* with cpu model support, CMM is only indicated if really available */
2311 if (kvm_s390_cmma_available()) {
2312 set_bit(S390_FEAT_CMM, model->features);
2313 } else {
2314 /* no cmm -> no cmm nt */
2315 clear_bit(S390_FEAT_CMM_NT, model->features);
2318 /* bpb needs kernel support for migration, VSIE and reset */
2319 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) {
2320 clear_bit(S390_FEAT_BPB, model->features);
2323 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2324 set_bit(S390_FEAT_ZPCI, model->features);
2325 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2327 if (s390_known_cpu_type(cpu_type)) {
2328 /* we want the exact model, even if some features are missing */
2329 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2330 ibc_ec_ga(unblocked_ibc), NULL);
2331 } else {
2332 /* model unknown, e.g. too new - search using features */
2333 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2334 ibc_ec_ga(unblocked_ibc),
2335 model->features);
2337 if (!model->def) {
2338 error_setg(errp, "KVM: host CPU model could not be identified");
2339 return;
2341 /* for now, we can only provide the AP feature with HW support */
2342 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO,
2343 KVM_S390_VM_CRYPTO_ENABLE_APIE)) {
2344 set_bit(S390_FEAT_AP, model->features);
2346 /* strip of features that are not part of the maximum model */
2347 bitmap_and(model->features, model->features, model->def->full_feat,
2348 S390_FEAT_MAX);
2351 static void kvm_s390_configure_apie(bool interpret)
2353 uint64_t attr = interpret ? KVM_S390_VM_CRYPTO_ENABLE_APIE :
2354 KVM_S390_VM_CRYPTO_DISABLE_APIE;
2356 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
2357 kvm_s390_set_attr(attr);
2361 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2363 struct kvm_s390_vm_cpu_processor prop = {
2364 .fac_list = { 0 },
2366 struct kvm_device_attr attr = {
2367 .group = KVM_S390_VM_CPU_MODEL,
2368 .attr = KVM_S390_VM_CPU_PROCESSOR,
2369 .addr = (uint64_t) &prop,
2371 int rc;
2373 if (!model) {
2374 /* compatibility handling if cpu models are disabled */
2375 if (kvm_s390_cmma_available()) {
2376 kvm_s390_enable_cmma();
2378 return;
2380 if (!kvm_s390_cpu_models_supported()) {
2381 error_setg(errp, "KVM doesn't support CPU models");
2382 return;
2384 prop.cpuid = s390_cpuid_from_cpu_model(model);
2385 prop.ibc = s390_ibc_from_cpu_model(model);
2386 /* configure cpu features indicated via STFL(e) */
2387 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2388 (uint8_t *) prop.fac_list);
2389 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2390 if (rc) {
2391 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2392 return;
2394 /* configure cpu features indicated e.g. via SCLP */
2395 rc = configure_cpu_feat(model->features);
2396 if (rc) {
2397 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2398 return;
2400 /* configure cpu subfunctions indicated via query / test bit */
2401 rc = configure_cpu_subfunc(model->features);
2402 if (rc) {
2403 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2404 return;
2406 /* enable CMM via CMMA */
2407 if (test_bit(S390_FEAT_CMM, model->features)) {
2408 kvm_s390_enable_cmma();
2411 if (test_bit(S390_FEAT_AP, model->features)) {
2412 kvm_s390_configure_apie(true);
2416 void kvm_s390_restart_interrupt(S390CPU *cpu)
2418 struct kvm_s390_irq irq = {
2419 .type = KVM_S390_RESTART,
2422 kvm_s390_vcpu_interrupt(cpu, &irq);
2425 void kvm_s390_stop_interrupt(S390CPU *cpu)
2427 struct kvm_s390_irq irq = {
2428 .type = KVM_S390_SIGP_STOP,
2431 kvm_s390_vcpu_interrupt(cpu, &irq);