hw/arm/boot: Rename elf_{low, high}_addr to image_{low, high}_addr
[qemu/ar7.git] / target / s390x / kvm.c
blob6e814c230bb4db44feca98e5277929282726b894
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 "qapi/error.h"
32 #include "qemu/error-report.h"
33 #include "qemu/timer.h"
34 #include "qemu/units.h"
35 #include "qemu/mmap-alloc.h"
36 #include "qemu/log.h"
37 #include "sysemu/sysemu.h"
38 #include "sysemu/hw_accel.h"
39 #include "hw/hw.h"
40 #include "sysemu/device_tree.h"
41 #include "exec/gdbstub.h"
42 #include "exec/ram_addr.h"
43 #include "trace.h"
44 #include "hw/s390x/s390-pci-inst.h"
45 #include "hw/s390x/s390-pci-bus.h"
46 #include "hw/s390x/ipl.h"
47 #include "hw/s390x/ebcdic.h"
48 #include "exec/memattrs.h"
49 #include "hw/s390x/s390-virtio-ccw.h"
50 #include "hw/s390x/s390-virtio-hcall.h"
52 #ifndef DEBUG_KVM
53 #define DEBUG_KVM 0
54 #endif
56 #define DPRINTF(fmt, ...) do { \
57 if (DEBUG_KVM) { \
58 fprintf(stderr, fmt, ## __VA_ARGS__); \
59 } \
60 } while (0)
62 #define kvm_vm_check_mem_attr(s, attr) \
63 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
65 #define IPA0_DIAG 0x8300
66 #define IPA0_SIGP 0xae00
67 #define IPA0_B2 0xb200
68 #define IPA0_B9 0xb900
69 #define IPA0_EB 0xeb00
70 #define IPA0_E3 0xe300
72 #define PRIV_B2_SCLP_CALL 0x20
73 #define PRIV_B2_CSCH 0x30
74 #define PRIV_B2_HSCH 0x31
75 #define PRIV_B2_MSCH 0x32
76 #define PRIV_B2_SSCH 0x33
77 #define PRIV_B2_STSCH 0x34
78 #define PRIV_B2_TSCH 0x35
79 #define PRIV_B2_TPI 0x36
80 #define PRIV_B2_SAL 0x37
81 #define PRIV_B2_RSCH 0x38
82 #define PRIV_B2_STCRW 0x39
83 #define PRIV_B2_STCPS 0x3a
84 #define PRIV_B2_RCHP 0x3b
85 #define PRIV_B2_SCHM 0x3c
86 #define PRIV_B2_CHSC 0x5f
87 #define PRIV_B2_SIGA 0x74
88 #define PRIV_B2_XSCH 0x76
90 #define PRIV_EB_SQBS 0x8a
91 #define PRIV_EB_PCISTB 0xd0
92 #define PRIV_EB_SIC 0xd1
94 #define PRIV_B9_EQBS 0x9c
95 #define PRIV_B9_CLP 0xa0
96 #define PRIV_B9_PCISTG 0xd0
97 #define PRIV_B9_PCILG 0xd2
98 #define PRIV_B9_RPCIT 0xd3
100 #define PRIV_E3_MPCIFC 0xd0
101 #define PRIV_E3_STPCIFC 0xd4
103 #define DIAG_TIMEREVENT 0x288
104 #define DIAG_IPL 0x308
105 #define DIAG_KVM_HYPERCALL 0x500
106 #define DIAG_KVM_BREAKPOINT 0x501
108 #define ICPT_INSTRUCTION 0x04
109 #define ICPT_PROGRAM 0x08
110 #define ICPT_EXT_INT 0x14
111 #define ICPT_WAITPSW 0x1c
112 #define ICPT_SOFT_INTERCEPT 0x24
113 #define ICPT_CPU_STOP 0x28
114 #define ICPT_OPEREXC 0x2c
115 #define ICPT_IO 0x40
117 #define NR_LOCAL_IRQS 32
119 * Needs to be big enough to contain max_cpus emergency signals
120 * and in addition NR_LOCAL_IRQS interrupts
122 #define VCPU_IRQ_BUF_SIZE(max_cpus) (sizeof(struct kvm_s390_irq) * \
123 (max_cpus + NR_LOCAL_IRQS))
125 static CPUWatchpoint hw_watchpoint;
127 * We don't use a list because this structure is also used to transmit the
128 * hardware breakpoints to the kernel.
130 static struct kvm_hw_breakpoint *hw_breakpoints;
131 static int nb_hw_breakpoints;
133 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
134 KVM_CAP_LAST_INFO
137 static int cap_sync_regs;
138 static int cap_async_pf;
139 static int cap_mem_op;
140 static int cap_s390_irq;
141 static int cap_ri;
142 static int cap_gs;
143 static int cap_hpage_1m;
145 static int active_cmma;
147 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared);
149 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
151 struct kvm_device_attr attr = {
152 .group = KVM_S390_VM_MEM_CTRL,
153 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
154 .addr = (uint64_t) memory_limit,
157 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
160 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit)
162 int rc;
164 struct kvm_device_attr attr = {
165 .group = KVM_S390_VM_MEM_CTRL,
166 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
167 .addr = (uint64_t) &new_limit,
170 if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) {
171 return 0;
174 rc = kvm_s390_query_mem_limit(hw_limit);
175 if (rc) {
176 return rc;
177 } else if (*hw_limit < new_limit) {
178 return -E2BIG;
181 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
184 int kvm_s390_cmma_active(void)
186 return active_cmma;
189 static bool kvm_s390_cmma_available(void)
191 static bool initialized, value;
193 if (!initialized) {
194 initialized = true;
195 value = kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_ENABLE_CMMA) &&
196 kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_CLR_CMMA);
198 return value;
201 void kvm_s390_cmma_reset(void)
203 int rc;
204 struct kvm_device_attr attr = {
205 .group = KVM_S390_VM_MEM_CTRL,
206 .attr = KVM_S390_VM_MEM_CLR_CMMA,
209 if (!kvm_s390_cmma_active()) {
210 return;
213 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
214 trace_kvm_clear_cmma(rc);
217 static void kvm_s390_enable_cmma(void)
219 int rc;
220 struct kvm_device_attr attr = {
221 .group = KVM_S390_VM_MEM_CTRL,
222 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
225 if (cap_hpage_1m) {
226 warn_report("CMM will not be enabled because it is not "
227 "compatible with huge memory backings.");
228 return;
230 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
231 active_cmma = !rc;
232 trace_kvm_enable_cmma(rc);
235 static void kvm_s390_set_attr(uint64_t attr)
237 struct kvm_device_attr attribute = {
238 .group = KVM_S390_VM_CRYPTO,
239 .attr = attr,
242 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
244 if (ret) {
245 error_report("Failed to set crypto device attribute %lu: %s",
246 attr, strerror(-ret));
250 static void kvm_s390_init_aes_kw(void)
252 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
254 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
255 NULL)) {
256 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
259 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
260 kvm_s390_set_attr(attr);
264 static void kvm_s390_init_dea_kw(void)
266 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
268 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
269 NULL)) {
270 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
273 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
274 kvm_s390_set_attr(attr);
278 void kvm_s390_crypto_reset(void)
280 if (s390_has_feat(S390_FEAT_MSA_EXT_3)) {
281 kvm_s390_init_aes_kw();
282 kvm_s390_init_dea_kw();
286 void kvm_s390_set_max_pagesize(uint64_t pagesize, Error **errp)
288 if (pagesize == 4 * KiB) {
289 return;
292 if (!hpage_1m_allowed()) {
293 error_setg(errp, "This QEMU machine does not support huge page "
294 "mappings");
295 return;
298 if (pagesize != 1 * MiB) {
299 error_setg(errp, "Memory backing with 2G pages was specified, "
300 "but KVM does not support this memory backing");
301 return;
304 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_HPAGE_1M, 0)) {
305 error_setg(errp, "Memory backing with 1M pages was specified, "
306 "but KVM does not support this memory backing");
307 return;
310 cap_hpage_1m = 1;
313 int kvm_arch_init(MachineState *ms, KVMState *s)
315 MachineClass *mc = MACHINE_GET_CLASS(ms);
317 mc->default_cpu_type = S390_CPU_TYPE_NAME("host");
318 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
319 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
320 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
321 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
323 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
324 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
325 phys_mem_set_alloc(legacy_s390_alloc);
328 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
329 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
330 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
331 if (ri_allowed()) {
332 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
333 cap_ri = 1;
336 if (cpu_model_allowed()) {
337 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
338 cap_gs = 1;
343 * The migration interface for ais was introduced with kernel 4.13
344 * but the capability itself had been active since 4.12. As migration
345 * support is considered necessary let's disable ais in the 2.10
346 * machine.
348 /* kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0); */
350 return 0;
353 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
355 return 0;
358 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
360 return cpu->cpu_index;
363 int kvm_arch_init_vcpu(CPUState *cs)
365 unsigned int max_cpus = MACHINE(qdev_get_machine())->smp.max_cpus;
366 S390CPU *cpu = S390_CPU(cs);
367 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
368 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE(max_cpus));
369 return 0;
372 int kvm_arch_destroy_vcpu(CPUState *cs)
374 S390CPU *cpu = S390_CPU(cs);
376 g_free(cpu->irqstate);
377 cpu->irqstate = NULL;
379 return 0;
382 void kvm_s390_reset_vcpu(S390CPU *cpu)
384 CPUState *cs = CPU(cpu);
386 /* The initial reset call is needed here to reset in-kernel
387 * vcpu data that we can't access directly from QEMU
388 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
389 * Before this ioctl cpu_synchronize_state() is called in common kvm
390 * code (kvm-all) */
391 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
392 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
396 static int can_sync_regs(CPUState *cs, int regs)
398 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
401 int kvm_arch_put_registers(CPUState *cs, int level)
403 S390CPU *cpu = S390_CPU(cs);
404 CPUS390XState *env = &cpu->env;
405 struct kvm_sregs sregs;
406 struct kvm_regs regs;
407 struct kvm_fpu fpu = {};
408 int r;
409 int i;
411 /* always save the PSW and the GPRS*/
412 cs->kvm_run->psw_addr = env->psw.addr;
413 cs->kvm_run->psw_mask = env->psw.mask;
415 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
416 for (i = 0; i < 16; i++) {
417 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
418 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
420 } else {
421 for (i = 0; i < 16; i++) {
422 regs.gprs[i] = env->regs[i];
424 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
425 if (r < 0) {
426 return r;
430 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
431 for (i = 0; i < 32; i++) {
432 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0];
433 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1];
435 cs->kvm_run->s.regs.fpc = env->fpc;
436 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
437 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
438 for (i = 0; i < 16; i++) {
439 cs->kvm_run->s.regs.fprs[i] = *get_freg(env, i);
441 cs->kvm_run->s.regs.fpc = env->fpc;
442 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
443 } else {
444 /* Floating point */
445 for (i = 0; i < 16; i++) {
446 fpu.fprs[i] = *get_freg(env, i);
448 fpu.fpc = env->fpc;
450 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
451 if (r < 0) {
452 return r;
456 /* Do we need to save more than that? */
457 if (level == KVM_PUT_RUNTIME_STATE) {
458 return 0;
461 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
462 cs->kvm_run->s.regs.cputm = env->cputm;
463 cs->kvm_run->s.regs.ckc = env->ckc;
464 cs->kvm_run->s.regs.todpr = env->todpr;
465 cs->kvm_run->s.regs.gbea = env->gbea;
466 cs->kvm_run->s.regs.pp = env->pp;
467 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
468 } else {
470 * These ONE_REGS are not protected by a capability. As they are only
471 * necessary for migration we just trace a possible error, but don't
472 * return with an error return code.
474 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
475 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
476 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
477 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
478 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
481 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
482 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
483 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
486 /* pfault parameters */
487 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
488 cs->kvm_run->s.regs.pft = env->pfault_token;
489 cs->kvm_run->s.regs.pfs = env->pfault_select;
490 cs->kvm_run->s.regs.pfc = env->pfault_compare;
491 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
492 } else if (cap_async_pf) {
493 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
494 if (r < 0) {
495 return r;
497 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
498 if (r < 0) {
499 return r;
501 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
502 if (r < 0) {
503 return r;
507 /* access registers and control registers*/
508 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
509 for (i = 0; i < 16; i++) {
510 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
511 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
513 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
514 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
515 } else {
516 for (i = 0; i < 16; i++) {
517 sregs.acrs[i] = env->aregs[i];
518 sregs.crs[i] = env->cregs[i];
520 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
521 if (r < 0) {
522 return r;
526 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
527 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
528 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
531 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
532 cs->kvm_run->s.regs.bpbc = env->bpbc;
533 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_BPBC;
536 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
537 cs->kvm_run->s.regs.etoken = env->etoken;
538 cs->kvm_run->s.regs.etoken_extension = env->etoken_extension;
539 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ETOKEN;
542 /* Finally the prefix */
543 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
544 cs->kvm_run->s.regs.prefix = env->psa;
545 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
546 } else {
547 /* prefix is only supported via sync regs */
549 return 0;
552 int kvm_arch_get_registers(CPUState *cs)
554 S390CPU *cpu = S390_CPU(cs);
555 CPUS390XState *env = &cpu->env;
556 struct kvm_sregs sregs;
557 struct kvm_regs regs;
558 struct kvm_fpu fpu;
559 int i, r;
561 /* get the PSW */
562 env->psw.addr = cs->kvm_run->psw_addr;
563 env->psw.mask = cs->kvm_run->psw_mask;
565 /* the GPRS */
566 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
567 for (i = 0; i < 16; i++) {
568 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
570 } else {
571 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
572 if (r < 0) {
573 return r;
575 for (i = 0; i < 16; i++) {
576 env->regs[i] = regs.gprs[i];
580 /* The ACRS and CRS */
581 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
582 for (i = 0; i < 16; i++) {
583 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
584 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
586 } else {
587 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
588 if (r < 0) {
589 return r;
591 for (i = 0; i < 16; i++) {
592 env->aregs[i] = sregs.acrs[i];
593 env->cregs[i] = sregs.crs[i];
597 /* Floating point and vector registers */
598 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
599 for (i = 0; i < 32; i++) {
600 env->vregs[i][0] = cs->kvm_run->s.regs.vrs[i][0];
601 env->vregs[i][1] = cs->kvm_run->s.regs.vrs[i][1];
603 env->fpc = cs->kvm_run->s.regs.fpc;
604 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
605 for (i = 0; i < 16; i++) {
606 *get_freg(env, i) = cs->kvm_run->s.regs.fprs[i];
608 env->fpc = cs->kvm_run->s.regs.fpc;
609 } else {
610 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
611 if (r < 0) {
612 return r;
614 for (i = 0; i < 16; i++) {
615 *get_freg(env, i) = fpu.fprs[i];
617 env->fpc = fpu.fpc;
620 /* The prefix */
621 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
622 env->psa = cs->kvm_run->s.regs.prefix;
625 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
626 env->cputm = cs->kvm_run->s.regs.cputm;
627 env->ckc = cs->kvm_run->s.regs.ckc;
628 env->todpr = cs->kvm_run->s.regs.todpr;
629 env->gbea = cs->kvm_run->s.regs.gbea;
630 env->pp = cs->kvm_run->s.regs.pp;
631 } else {
633 * These ONE_REGS are not protected by a capability. As they are only
634 * necessary for migration we just trace a possible error, but don't
635 * return with an error return code.
637 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
638 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
639 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
640 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
641 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
644 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
645 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
648 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
649 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
652 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
653 env->bpbc = cs->kvm_run->s.regs.bpbc;
656 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
657 env->etoken = cs->kvm_run->s.regs.etoken;
658 env->etoken_extension = cs->kvm_run->s.regs.etoken_extension;
661 /* pfault parameters */
662 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
663 env->pfault_token = cs->kvm_run->s.regs.pft;
664 env->pfault_select = cs->kvm_run->s.regs.pfs;
665 env->pfault_compare = cs->kvm_run->s.regs.pfc;
666 } else if (cap_async_pf) {
667 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
668 if (r < 0) {
669 return r;
671 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
672 if (r < 0) {
673 return r;
675 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
676 if (r < 0) {
677 return r;
681 return 0;
684 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
686 int r;
687 struct kvm_device_attr attr = {
688 .group = KVM_S390_VM_TOD,
689 .attr = KVM_S390_VM_TOD_LOW,
690 .addr = (uint64_t)tod_low,
693 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
694 if (r) {
695 return r;
698 attr.attr = KVM_S390_VM_TOD_HIGH;
699 attr.addr = (uint64_t)tod_high;
700 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
703 int kvm_s390_get_clock_ext(uint8_t *tod_high, uint64_t *tod_low)
705 int r;
706 struct kvm_s390_vm_tod_clock gtod;
707 struct kvm_device_attr attr = {
708 .group = KVM_S390_VM_TOD,
709 .attr = KVM_S390_VM_TOD_EXT,
710 .addr = (uint64_t)&gtod,
713 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
714 *tod_high = gtod.epoch_idx;
715 *tod_low = gtod.tod;
717 return r;
720 int kvm_s390_set_clock(uint8_t tod_high, uint64_t tod_low)
722 int r;
723 struct kvm_device_attr attr = {
724 .group = KVM_S390_VM_TOD,
725 .attr = KVM_S390_VM_TOD_LOW,
726 .addr = (uint64_t)&tod_low,
729 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
730 if (r) {
731 return r;
734 attr.attr = KVM_S390_VM_TOD_HIGH;
735 attr.addr = (uint64_t)&tod_high;
736 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
739 int kvm_s390_set_clock_ext(uint8_t tod_high, uint64_t tod_low)
741 struct kvm_s390_vm_tod_clock gtod = {
742 .epoch_idx = tod_high,
743 .tod = tod_low,
745 struct kvm_device_attr attr = {
746 .group = KVM_S390_VM_TOD,
747 .attr = KVM_S390_VM_TOD_EXT,
748 .addr = (uint64_t)&gtod,
751 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
755 * kvm_s390_mem_op:
756 * @addr: the logical start address in guest memory
757 * @ar: the access register number
758 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
759 * @len: length that should be transferred
760 * @is_write: true = write, false = read
761 * Returns: 0 on success, non-zero if an exception or error occurred
763 * Use KVM ioctl to read/write from/to guest memory. An access exception
764 * is injected into the vCPU in case of translation errors.
766 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
767 int len, bool is_write)
769 struct kvm_s390_mem_op mem_op = {
770 .gaddr = addr,
771 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
772 .size = len,
773 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
774 : KVM_S390_MEMOP_LOGICAL_READ,
775 .buf = (uint64_t)hostbuf,
776 .ar = ar,
778 int ret;
780 if (!cap_mem_op) {
781 return -ENOSYS;
783 if (!hostbuf) {
784 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
787 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
788 if (ret < 0) {
789 warn_report("KVM_S390_MEM_OP failed: %s", strerror(-ret));
791 return ret;
795 * Legacy layout for s390:
796 * Older S390 KVM requires the topmost vma of the RAM to be
797 * smaller than an system defined value, which is at least 256GB.
798 * Larger systems have larger values. We put the guest between
799 * the end of data segment (system break) and this value. We
800 * use 32GB as a base to have enough room for the system break
801 * to grow. We also have to use MAP parameters that avoid
802 * read-only mapping of guest pages.
804 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared)
806 static void *mem;
808 if (mem) {
809 /* we only support one allocation, which is enough for initial ram */
810 return NULL;
813 mem = mmap((void *) 0x800000000ULL, size,
814 PROT_EXEC|PROT_READ|PROT_WRITE,
815 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
816 if (mem == MAP_FAILED) {
817 mem = NULL;
819 if (mem && align) {
820 *align = QEMU_VMALLOC_ALIGN;
822 return mem;
825 static uint8_t const *sw_bp_inst;
826 static uint8_t sw_bp_ilen;
828 static void determine_sw_breakpoint_instr(void)
830 /* DIAG 501 is used for sw breakpoints with old kernels */
831 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
832 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
833 static const uint8_t instr_0x0000[] = {0x00, 0x00};
835 if (sw_bp_inst) {
836 return;
838 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
839 sw_bp_inst = diag_501;
840 sw_bp_ilen = sizeof(diag_501);
841 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
842 } else {
843 sw_bp_inst = instr_0x0000;
844 sw_bp_ilen = sizeof(instr_0x0000);
845 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
849 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
851 determine_sw_breakpoint_instr();
853 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
854 sw_bp_ilen, 0) ||
855 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
856 return -EINVAL;
858 return 0;
861 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
863 uint8_t t[MAX_ILEN];
865 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
866 return -EINVAL;
867 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
868 return -EINVAL;
869 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
870 sw_bp_ilen, 1)) {
871 return -EINVAL;
874 return 0;
877 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
878 int len, int type)
880 int n;
882 for (n = 0; n < nb_hw_breakpoints; n++) {
883 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
884 (hw_breakpoints[n].len == len || len == -1)) {
885 return &hw_breakpoints[n];
889 return NULL;
892 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
894 int size;
896 if (find_hw_breakpoint(addr, len, type)) {
897 return -EEXIST;
900 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
902 if (!hw_breakpoints) {
903 nb_hw_breakpoints = 0;
904 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
905 } else {
906 hw_breakpoints =
907 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
910 if (!hw_breakpoints) {
911 nb_hw_breakpoints = 0;
912 return -ENOMEM;
915 hw_breakpoints[nb_hw_breakpoints].addr = addr;
916 hw_breakpoints[nb_hw_breakpoints].len = len;
917 hw_breakpoints[nb_hw_breakpoints].type = type;
919 nb_hw_breakpoints++;
921 return 0;
924 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
925 target_ulong len, int type)
927 switch (type) {
928 case GDB_BREAKPOINT_HW:
929 type = KVM_HW_BP;
930 break;
931 case GDB_WATCHPOINT_WRITE:
932 if (len < 1) {
933 return -EINVAL;
935 type = KVM_HW_WP_WRITE;
936 break;
937 default:
938 return -ENOSYS;
940 return insert_hw_breakpoint(addr, len, type);
943 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
944 target_ulong len, int type)
946 int size;
947 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
949 if (bp == NULL) {
950 return -ENOENT;
953 nb_hw_breakpoints--;
954 if (nb_hw_breakpoints > 0) {
956 * In order to trim the array, move the last element to the position to
957 * be removed - if necessary.
959 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
960 *bp = hw_breakpoints[nb_hw_breakpoints];
962 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
963 hw_breakpoints =
964 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
965 } else {
966 g_free(hw_breakpoints);
967 hw_breakpoints = NULL;
970 return 0;
973 void kvm_arch_remove_all_hw_breakpoints(void)
975 nb_hw_breakpoints = 0;
976 g_free(hw_breakpoints);
977 hw_breakpoints = NULL;
980 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
982 int i;
984 if (nb_hw_breakpoints > 0) {
985 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
986 dbg->arch.hw_bp = hw_breakpoints;
988 for (i = 0; i < nb_hw_breakpoints; ++i) {
989 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
990 hw_breakpoints[i].addr);
992 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
993 } else {
994 dbg->arch.nr_hw_bp = 0;
995 dbg->arch.hw_bp = NULL;
999 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
1003 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1005 return MEMTXATTRS_UNSPECIFIED;
1008 int kvm_arch_process_async_events(CPUState *cs)
1010 return cs->halted;
1013 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
1014 struct kvm_s390_interrupt *interrupt)
1016 int r = 0;
1018 interrupt->type = irq->type;
1019 switch (irq->type) {
1020 case KVM_S390_INT_VIRTIO:
1021 interrupt->parm = irq->u.ext.ext_params;
1022 /* fall through */
1023 case KVM_S390_INT_PFAULT_INIT:
1024 case KVM_S390_INT_PFAULT_DONE:
1025 interrupt->parm64 = irq->u.ext.ext_params2;
1026 break;
1027 case KVM_S390_PROGRAM_INT:
1028 interrupt->parm = irq->u.pgm.code;
1029 break;
1030 case KVM_S390_SIGP_SET_PREFIX:
1031 interrupt->parm = irq->u.prefix.address;
1032 break;
1033 case KVM_S390_INT_SERVICE:
1034 interrupt->parm = irq->u.ext.ext_params;
1035 break;
1036 case KVM_S390_MCHK:
1037 interrupt->parm = irq->u.mchk.cr14;
1038 interrupt->parm64 = irq->u.mchk.mcic;
1039 break;
1040 case KVM_S390_INT_EXTERNAL_CALL:
1041 interrupt->parm = irq->u.extcall.code;
1042 break;
1043 case KVM_S390_INT_EMERGENCY:
1044 interrupt->parm = irq->u.emerg.code;
1045 break;
1046 case KVM_S390_SIGP_STOP:
1047 case KVM_S390_RESTART:
1048 break; /* These types have no parameters */
1049 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1050 interrupt->parm = irq->u.io.subchannel_id << 16;
1051 interrupt->parm |= irq->u.io.subchannel_nr;
1052 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
1053 interrupt->parm64 |= irq->u.io.io_int_word;
1054 break;
1055 default:
1056 r = -EINVAL;
1057 break;
1059 return r;
1062 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
1064 struct kvm_s390_interrupt kvmint = {};
1065 int r;
1067 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1068 if (r < 0) {
1069 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1070 exit(1);
1073 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
1074 if (r < 0) {
1075 fprintf(stderr, "KVM failed to inject interrupt\n");
1076 exit(1);
1080 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
1082 CPUState *cs = CPU(cpu);
1083 int r;
1085 if (cap_s390_irq) {
1086 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
1087 if (!r) {
1088 return;
1090 error_report("KVM failed to inject interrupt %llx", irq->type);
1091 exit(1);
1094 inject_vcpu_irq_legacy(cs, irq);
1097 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq *irq)
1099 struct kvm_s390_interrupt kvmint = {};
1100 int r;
1102 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1103 if (r < 0) {
1104 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1105 exit(1);
1108 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1109 if (r < 0) {
1110 fprintf(stderr, "KVM failed to inject interrupt\n");
1111 exit(1);
1115 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1117 struct kvm_s390_irq irq = {
1118 .type = KVM_S390_PROGRAM_INT,
1119 .u.pgm.code = code,
1121 qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
1122 cpu->env.psw.addr);
1123 kvm_s390_vcpu_interrupt(cpu, &irq);
1126 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1128 struct kvm_s390_irq irq = {
1129 .type = KVM_S390_PROGRAM_INT,
1130 .u.pgm.code = code,
1131 .u.pgm.trans_exc_code = te_code,
1132 .u.pgm.exc_access_id = te_code & 3,
1135 kvm_s390_vcpu_interrupt(cpu, &irq);
1138 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1139 uint16_t ipbh0)
1141 CPUS390XState *env = &cpu->env;
1142 uint64_t sccb;
1143 uint32_t code;
1144 int r = 0;
1146 sccb = env->regs[ipbh0 & 0xf];
1147 code = env->regs[(ipbh0 & 0xf0) >> 4];
1149 r = sclp_service_call(env, sccb, code);
1150 if (r < 0) {
1151 kvm_s390_program_interrupt(cpu, -r);
1152 } else {
1153 setcc(cpu, r);
1156 return 0;
1159 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1161 CPUS390XState *env = &cpu->env;
1162 int rc = 0;
1163 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1165 switch (ipa1) {
1166 case PRIV_B2_XSCH:
1167 ioinst_handle_xsch(cpu, env->regs[1], RA_IGNORED);
1168 break;
1169 case PRIV_B2_CSCH:
1170 ioinst_handle_csch(cpu, env->regs[1], RA_IGNORED);
1171 break;
1172 case PRIV_B2_HSCH:
1173 ioinst_handle_hsch(cpu, env->regs[1], RA_IGNORED);
1174 break;
1175 case PRIV_B2_MSCH:
1176 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1177 break;
1178 case PRIV_B2_SSCH:
1179 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1180 break;
1181 case PRIV_B2_STCRW:
1182 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb, RA_IGNORED);
1183 break;
1184 case PRIV_B2_STSCH:
1185 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1186 break;
1187 case PRIV_B2_TSCH:
1188 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1189 fprintf(stderr, "Spurious tsch intercept\n");
1190 break;
1191 case PRIV_B2_CHSC:
1192 ioinst_handle_chsc(cpu, run->s390_sieic.ipb, RA_IGNORED);
1193 break;
1194 case PRIV_B2_TPI:
1195 /* This should have been handled by kvm already. */
1196 fprintf(stderr, "Spurious tpi intercept\n");
1197 break;
1198 case PRIV_B2_SCHM:
1199 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1200 run->s390_sieic.ipb, RA_IGNORED);
1201 break;
1202 case PRIV_B2_RSCH:
1203 ioinst_handle_rsch(cpu, env->regs[1], RA_IGNORED);
1204 break;
1205 case PRIV_B2_RCHP:
1206 ioinst_handle_rchp(cpu, env->regs[1], RA_IGNORED);
1207 break;
1208 case PRIV_B2_STCPS:
1209 /* We do not provide this instruction, it is suppressed. */
1210 break;
1211 case PRIV_B2_SAL:
1212 ioinst_handle_sal(cpu, env->regs[1], RA_IGNORED);
1213 break;
1214 case PRIV_B2_SIGA:
1215 /* Not provided, set CC = 3 for subchannel not operational */
1216 setcc(cpu, 3);
1217 break;
1218 case PRIV_B2_SCLP_CALL:
1219 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1220 break;
1221 default:
1222 rc = -1;
1223 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1224 break;
1227 return rc;
1230 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1231 uint8_t *ar)
1233 CPUS390XState *env = &cpu->env;
1234 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1235 uint32_t base2 = run->s390_sieic.ipb >> 28;
1236 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1237 ((run->s390_sieic.ipb & 0xff00) << 4);
1239 if (disp2 & 0x80000) {
1240 disp2 += 0xfff00000;
1242 if (ar) {
1243 *ar = base2;
1246 return (base2 ? env->regs[base2] : 0) +
1247 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1250 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1251 uint8_t *ar)
1253 CPUS390XState *env = &cpu->env;
1254 uint32_t base2 = run->s390_sieic.ipb >> 28;
1255 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1256 ((run->s390_sieic.ipb & 0xff00) << 4);
1258 if (disp2 & 0x80000) {
1259 disp2 += 0xfff00000;
1261 if (ar) {
1262 *ar = base2;
1265 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1268 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1270 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1272 if (s390_has_feat(S390_FEAT_ZPCI)) {
1273 return clp_service_call(cpu, r2, RA_IGNORED);
1274 } else {
1275 return -1;
1279 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1281 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1282 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1284 if (s390_has_feat(S390_FEAT_ZPCI)) {
1285 return pcilg_service_call(cpu, r1, r2, RA_IGNORED);
1286 } else {
1287 return -1;
1291 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1293 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1294 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1296 if (s390_has_feat(S390_FEAT_ZPCI)) {
1297 return pcistg_service_call(cpu, r1, r2, RA_IGNORED);
1298 } else {
1299 return -1;
1303 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1305 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1306 uint64_t fiba;
1307 uint8_t ar;
1309 if (s390_has_feat(S390_FEAT_ZPCI)) {
1310 fiba = get_base_disp_rxy(cpu, run, &ar);
1312 return stpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1313 } else {
1314 return -1;
1318 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1320 CPUS390XState *env = &cpu->env;
1321 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1322 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1323 uint8_t isc;
1324 uint16_t mode;
1325 int r;
1327 mode = env->regs[r1] & 0xffff;
1328 isc = (env->regs[r3] >> 27) & 0x7;
1329 r = css_do_sic(env, isc, mode);
1330 if (r) {
1331 kvm_s390_program_interrupt(cpu, -r);
1334 return 0;
1337 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1339 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1340 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1342 if (s390_has_feat(S390_FEAT_ZPCI)) {
1343 return rpcit_service_call(cpu, r1, r2, RA_IGNORED);
1344 } else {
1345 return -1;
1349 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1351 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1352 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1353 uint64_t gaddr;
1354 uint8_t ar;
1356 if (s390_has_feat(S390_FEAT_ZPCI)) {
1357 gaddr = get_base_disp_rsy(cpu, run, &ar);
1359 return pcistb_service_call(cpu, r1, r3, gaddr, ar, RA_IGNORED);
1360 } else {
1361 return -1;
1365 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1367 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1368 uint64_t fiba;
1369 uint8_t ar;
1371 if (s390_has_feat(S390_FEAT_ZPCI)) {
1372 fiba = get_base_disp_rxy(cpu, run, &ar);
1374 return mpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1375 } else {
1376 return -1;
1380 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1382 int r = 0;
1384 switch (ipa1) {
1385 case PRIV_B9_CLP:
1386 r = kvm_clp_service_call(cpu, run);
1387 break;
1388 case PRIV_B9_PCISTG:
1389 r = kvm_pcistg_service_call(cpu, run);
1390 break;
1391 case PRIV_B9_PCILG:
1392 r = kvm_pcilg_service_call(cpu, run);
1393 break;
1394 case PRIV_B9_RPCIT:
1395 r = kvm_rpcit_service_call(cpu, run);
1396 break;
1397 case PRIV_B9_EQBS:
1398 /* just inject exception */
1399 r = -1;
1400 break;
1401 default:
1402 r = -1;
1403 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1404 break;
1407 return r;
1410 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1412 int r = 0;
1414 switch (ipbl) {
1415 case PRIV_EB_PCISTB:
1416 r = kvm_pcistb_service_call(cpu, run);
1417 break;
1418 case PRIV_EB_SIC:
1419 r = kvm_sic_service_call(cpu, run);
1420 break;
1421 case PRIV_EB_SQBS:
1422 /* just inject exception */
1423 r = -1;
1424 break;
1425 default:
1426 r = -1;
1427 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1428 break;
1431 return r;
1434 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1436 int r = 0;
1438 switch (ipbl) {
1439 case PRIV_E3_MPCIFC:
1440 r = kvm_mpcifc_service_call(cpu, run);
1441 break;
1442 case PRIV_E3_STPCIFC:
1443 r = kvm_stpcifc_service_call(cpu, run);
1444 break;
1445 default:
1446 r = -1;
1447 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1448 break;
1451 return r;
1454 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1456 CPUS390XState *env = &cpu->env;
1457 int ret;
1459 ret = s390_virtio_hypercall(env);
1460 if (ret == -EINVAL) {
1461 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1462 return 0;
1465 return ret;
1468 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1470 uint64_t r1, r3;
1471 int rc;
1473 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1474 r3 = run->s390_sieic.ipa & 0x000f;
1475 rc = handle_diag_288(&cpu->env, r1, r3);
1476 if (rc) {
1477 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1481 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1483 uint64_t r1, r3;
1485 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1486 r3 = run->s390_sieic.ipa & 0x000f;
1487 handle_diag_308(&cpu->env, r1, r3, RA_IGNORED);
1490 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1492 CPUS390XState *env = &cpu->env;
1493 unsigned long pc;
1495 pc = env->psw.addr - sw_bp_ilen;
1496 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1497 env->psw.addr = pc;
1498 return EXCP_DEBUG;
1501 return -ENOENT;
1504 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1506 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1508 int r = 0;
1509 uint16_t func_code;
1512 * For any diagnose call we support, bits 48-63 of the resulting
1513 * address specify the function code; the remainder is ignored.
1515 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1516 switch (func_code) {
1517 case DIAG_TIMEREVENT:
1518 kvm_handle_diag_288(cpu, run);
1519 break;
1520 case DIAG_IPL:
1521 kvm_handle_diag_308(cpu, run);
1522 break;
1523 case DIAG_KVM_HYPERCALL:
1524 r = handle_hypercall(cpu, run);
1525 break;
1526 case DIAG_KVM_BREAKPOINT:
1527 r = handle_sw_breakpoint(cpu, run);
1528 break;
1529 default:
1530 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1531 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1532 break;
1535 return r;
1538 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb)
1540 CPUS390XState *env = &cpu->env;
1541 const uint8_t r1 = ipa1 >> 4;
1542 const uint8_t r3 = ipa1 & 0x0f;
1543 int ret;
1544 uint8_t order;
1546 /* get order code */
1547 order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK;
1549 ret = handle_sigp(env, order, r1, r3);
1550 setcc(cpu, ret);
1551 return 0;
1554 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1556 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1557 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1558 int r = -1;
1560 DPRINTF("handle_instruction 0x%x 0x%x\n",
1561 run->s390_sieic.ipa, run->s390_sieic.ipb);
1562 switch (ipa0) {
1563 case IPA0_B2:
1564 r = handle_b2(cpu, run, ipa1);
1565 break;
1566 case IPA0_B9:
1567 r = handle_b9(cpu, run, ipa1);
1568 break;
1569 case IPA0_EB:
1570 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1571 break;
1572 case IPA0_E3:
1573 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1574 break;
1575 case IPA0_DIAG:
1576 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1577 break;
1578 case IPA0_SIGP:
1579 r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb);
1580 break;
1583 if (r < 0) {
1584 r = 0;
1585 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1588 return r;
1591 static void unmanageable_intercept(S390CPU *cpu, S390CrashReason reason,
1592 int pswoffset)
1594 CPUState *cs = CPU(cpu);
1596 s390_cpu_halt(cpu);
1597 cpu->env.crash_reason = reason;
1598 qemu_system_guest_panicked(cpu_get_crash_info(cs));
1601 /* try to detect pgm check loops */
1602 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1604 CPUState *cs = CPU(cpu);
1605 PSW oldpsw, newpsw;
1607 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1608 offsetof(LowCore, program_new_psw));
1609 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1610 offsetof(LowCore, program_new_psw) + 8);
1611 oldpsw.mask = run->psw_mask;
1612 oldpsw.addr = run->psw_addr;
1614 * Avoid endless loops of operation exceptions, if the pgm new
1615 * PSW will cause a new operation exception.
1616 * The heuristic checks if the pgm new psw is within 6 bytes before
1617 * the faulting psw address (with same DAT, AS settings) and the
1618 * new psw is not a wait psw and the fault was not triggered by
1619 * problem state. In that case go into crashed state.
1622 if (oldpsw.addr - newpsw.addr <= 6 &&
1623 !(newpsw.mask & PSW_MASK_WAIT) &&
1624 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1625 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1626 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1627 unmanageable_intercept(cpu, S390_CRASH_REASON_OPINT_LOOP,
1628 offsetof(LowCore, program_new_psw));
1629 return EXCP_HALTED;
1631 return 0;
1634 static int handle_intercept(S390CPU *cpu)
1636 CPUState *cs = CPU(cpu);
1637 struct kvm_run *run = cs->kvm_run;
1638 int icpt_code = run->s390_sieic.icptcode;
1639 int r = 0;
1641 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1642 (long)cs->kvm_run->psw_addr);
1643 switch (icpt_code) {
1644 case ICPT_INSTRUCTION:
1645 r = handle_instruction(cpu, run);
1646 break;
1647 case ICPT_PROGRAM:
1648 unmanageable_intercept(cpu, S390_CRASH_REASON_PGMINT_LOOP,
1649 offsetof(LowCore, program_new_psw));
1650 r = EXCP_HALTED;
1651 break;
1652 case ICPT_EXT_INT:
1653 unmanageable_intercept(cpu, S390_CRASH_REASON_EXTINT_LOOP,
1654 offsetof(LowCore, external_new_psw));
1655 r = EXCP_HALTED;
1656 break;
1657 case ICPT_WAITPSW:
1658 /* disabled wait, since enabled wait is handled in kernel */
1659 s390_handle_wait(cpu);
1660 r = EXCP_HALTED;
1661 break;
1662 case ICPT_CPU_STOP:
1663 do_stop_interrupt(&cpu->env);
1664 r = EXCP_HALTED;
1665 break;
1666 case ICPT_OPEREXC:
1667 /* check for break points */
1668 r = handle_sw_breakpoint(cpu, run);
1669 if (r == -ENOENT) {
1670 /* Then check for potential pgm check loops */
1671 r = handle_oper_loop(cpu, run);
1672 if (r == 0) {
1673 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1676 break;
1677 case ICPT_SOFT_INTERCEPT:
1678 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1679 exit(1);
1680 break;
1681 case ICPT_IO:
1682 fprintf(stderr, "KVM unimplemented icpt IO\n");
1683 exit(1);
1684 break;
1685 default:
1686 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1687 exit(1);
1688 break;
1691 return r;
1694 static int handle_tsch(S390CPU *cpu)
1696 CPUState *cs = CPU(cpu);
1697 struct kvm_run *run = cs->kvm_run;
1698 int ret;
1700 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb,
1701 RA_IGNORED);
1702 if (ret < 0) {
1704 * Failure.
1705 * If an I/O interrupt had been dequeued, we have to reinject it.
1707 if (run->s390_tsch.dequeued) {
1708 s390_io_interrupt(run->s390_tsch.subchannel_id,
1709 run->s390_tsch.subchannel_nr,
1710 run->s390_tsch.io_int_parm,
1711 run->s390_tsch.io_int_word);
1713 ret = 0;
1715 return ret;
1718 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1720 SysIB_322 sysib;
1721 int del;
1723 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1724 return;
1726 /* Shift the stack of Extended Names to prepare for our own data */
1727 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1728 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1729 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1730 * assumed it's not capable of managing Extended Names for lower levels.
1732 for (del = 1; del < sysib.count; del++) {
1733 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1734 break;
1737 if (del < sysib.count) {
1738 memset(sysib.ext_names[del], 0,
1739 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1741 /* Insert short machine name in EBCDIC, padded with blanks */
1742 if (qemu_name) {
1743 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1744 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1745 strlen(qemu_name)));
1747 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1748 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1749 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1750 * considered by s390 as not capable of providing any Extended Name.
1751 * Therefore if no name was specified on qemu invocation, we go with the
1752 * same "KVMguest" default, which KVM has filled into short name field.
1754 if (qemu_name) {
1755 strncpy((char *)sysib.ext_names[0], qemu_name,
1756 sizeof(sysib.ext_names[0]));
1757 } else {
1758 strcpy((char *)sysib.ext_names[0], "KVMguest");
1760 /* Insert UUID */
1761 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
1763 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1766 static int handle_stsi(S390CPU *cpu)
1768 CPUState *cs = CPU(cpu);
1769 struct kvm_run *run = cs->kvm_run;
1771 switch (run->s390_stsi.fc) {
1772 case 3:
1773 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1774 return 0;
1776 /* Only sysib 3.2.2 needs post-handling for now. */
1777 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1778 return 0;
1779 default:
1780 return 0;
1784 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1786 CPUState *cs = CPU(cpu);
1787 struct kvm_run *run = cs->kvm_run;
1789 int ret = 0;
1790 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1792 switch (arch_info->type) {
1793 case KVM_HW_WP_WRITE:
1794 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1795 cs->watchpoint_hit = &hw_watchpoint;
1796 hw_watchpoint.vaddr = arch_info->addr;
1797 hw_watchpoint.flags = BP_MEM_WRITE;
1798 ret = EXCP_DEBUG;
1800 break;
1801 case KVM_HW_BP:
1802 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1803 ret = EXCP_DEBUG;
1805 break;
1806 case KVM_SINGLESTEP:
1807 if (cs->singlestep_enabled) {
1808 ret = EXCP_DEBUG;
1810 break;
1811 default:
1812 ret = -ENOSYS;
1815 return ret;
1818 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1820 S390CPU *cpu = S390_CPU(cs);
1821 int ret = 0;
1823 qemu_mutex_lock_iothread();
1825 kvm_cpu_synchronize_state(cs);
1827 switch (run->exit_reason) {
1828 case KVM_EXIT_S390_SIEIC:
1829 ret = handle_intercept(cpu);
1830 break;
1831 case KVM_EXIT_S390_RESET:
1832 s390_ipl_reset_request(cs, S390_RESET_REIPL);
1833 break;
1834 case KVM_EXIT_S390_TSCH:
1835 ret = handle_tsch(cpu);
1836 break;
1837 case KVM_EXIT_S390_STSI:
1838 ret = handle_stsi(cpu);
1839 break;
1840 case KVM_EXIT_DEBUG:
1841 ret = kvm_arch_handle_debug_exit(cpu);
1842 break;
1843 default:
1844 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1845 break;
1847 qemu_mutex_unlock_iothread();
1849 if (ret == 0) {
1850 ret = EXCP_INTERRUPT;
1852 return ret;
1855 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1857 return true;
1860 void kvm_s390_enable_css_support(S390CPU *cpu)
1862 int r;
1864 /* Activate host kernel channel subsystem support. */
1865 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1866 assert(r == 0);
1869 void kvm_arch_init_irq_routing(KVMState *s)
1872 * Note that while irqchip capabilities generally imply that cpustates
1873 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1874 * have to override the common code kvm_halt_in_kernel_allowed setting.
1876 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1877 kvm_gsi_routing_allowed = true;
1878 kvm_halt_in_kernel_allowed = false;
1882 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1883 int vq, bool assign)
1885 struct kvm_ioeventfd kick = {
1886 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1887 KVM_IOEVENTFD_FLAG_DATAMATCH,
1888 .fd = event_notifier_get_fd(notifier),
1889 .datamatch = vq,
1890 .addr = sch,
1891 .len = 8,
1893 trace_kvm_assign_subch_ioeventfd(kick.fd, kick.addr, assign,
1894 kick.datamatch);
1895 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1896 return -ENOSYS;
1898 if (!assign) {
1899 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1901 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1904 int kvm_s390_get_ri(void)
1906 return cap_ri;
1909 int kvm_s390_get_gs(void)
1911 return cap_gs;
1914 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1916 struct kvm_mp_state mp_state = {};
1917 int ret;
1919 /* the kvm part might not have been initialized yet */
1920 if (CPU(cpu)->kvm_state == NULL) {
1921 return 0;
1924 switch (cpu_state) {
1925 case S390_CPU_STATE_STOPPED:
1926 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1927 break;
1928 case S390_CPU_STATE_CHECK_STOP:
1929 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1930 break;
1931 case S390_CPU_STATE_OPERATING:
1932 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1933 break;
1934 case S390_CPU_STATE_LOAD:
1935 mp_state.mp_state = KVM_MP_STATE_LOAD;
1936 break;
1937 default:
1938 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1939 cpu_state);
1940 exit(1);
1943 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1944 if (ret) {
1945 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1946 strerror(-ret));
1949 return ret;
1952 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
1954 unsigned int max_cpus = MACHINE(qdev_get_machine())->smp.max_cpus;
1955 struct kvm_s390_irq_state irq_state = {
1956 .buf = (uint64_t) cpu->irqstate,
1957 .len = VCPU_IRQ_BUF_SIZE(max_cpus),
1959 CPUState *cs = CPU(cpu);
1960 int32_t bytes;
1962 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1963 return;
1966 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
1967 if (bytes < 0) {
1968 cpu->irqstate_saved_size = 0;
1969 error_report("Migration of interrupt state failed");
1970 return;
1973 cpu->irqstate_saved_size = bytes;
1976 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
1978 CPUState *cs = CPU(cpu);
1979 struct kvm_s390_irq_state irq_state = {
1980 .buf = (uint64_t) cpu->irqstate,
1981 .len = cpu->irqstate_saved_size,
1983 int r;
1985 if (cpu->irqstate_saved_size == 0) {
1986 return 0;
1989 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1990 return -ENOSYS;
1993 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
1994 if (r) {
1995 error_report("Setting interrupt state failed %d", r);
1997 return r;
2000 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2001 uint64_t address, uint32_t data, PCIDevice *dev)
2003 S390PCIBusDevice *pbdev;
2004 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2006 if (!dev) {
2007 DPRINTF("add_msi_route no pci device\n");
2008 return -ENODEV;
2011 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
2012 if (!pbdev) {
2013 DPRINTF("add_msi_route no zpci device\n");
2014 return -ENODEV;
2017 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2018 route->flags = 0;
2019 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2020 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2021 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2022 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
2023 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2024 return 0;
2027 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2028 int vector, PCIDevice *dev)
2030 return 0;
2033 int kvm_arch_release_virq_post(int virq)
2035 return 0;
2038 int kvm_arch_msi_data_to_gsi(uint32_t data)
2040 abort();
2043 static int query_cpu_subfunc(S390FeatBitmap features)
2045 struct kvm_s390_vm_cpu_subfunc prop;
2046 struct kvm_device_attr attr = {
2047 .group = KVM_S390_VM_CPU_MODEL,
2048 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2049 .addr = (uint64_t) &prop,
2051 int rc;
2053 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2054 if (rc) {
2055 return rc;
2059 * We're going to add all subfunctions now, if the corresponding feature
2060 * is available that unlocks the query functions.
2062 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2063 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2064 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2066 if (test_bit(S390_FEAT_MSA, features)) {
2067 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2068 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2069 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2070 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2071 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2073 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2074 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2076 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2077 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2078 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2079 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2080 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2082 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2083 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2085 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2086 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2088 if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2089 s390_add_from_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2091 if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2092 s390_add_from_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2094 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2095 s390_add_from_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2097 return 0;
2100 static int configure_cpu_subfunc(const S390FeatBitmap features)
2102 struct kvm_s390_vm_cpu_subfunc prop = {};
2103 struct kvm_device_attr attr = {
2104 .group = KVM_S390_VM_CPU_MODEL,
2105 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2106 .addr = (uint64_t) &prop,
2109 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2110 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2111 /* hardware support might be missing, IBC will handle most of this */
2112 return 0;
2115 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2116 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2117 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2119 if (test_bit(S390_FEAT_MSA, features)) {
2120 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2121 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2122 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2123 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2124 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2126 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2127 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2129 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2130 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2131 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2132 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2133 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2135 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2136 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2138 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2139 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2141 if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2142 s390_fill_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2144 if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2145 s390_fill_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2147 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2148 s390_fill_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2150 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2153 static int kvm_to_feat[][2] = {
2154 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2155 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2156 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2157 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2158 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2159 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2160 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2161 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2162 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2163 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2164 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2165 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2166 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2167 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2170 static int query_cpu_feat(S390FeatBitmap features)
2172 struct kvm_s390_vm_cpu_feat prop;
2173 struct kvm_device_attr attr = {
2174 .group = KVM_S390_VM_CPU_MODEL,
2175 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2176 .addr = (uint64_t) &prop,
2178 int rc;
2179 int i;
2181 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2182 if (rc) {
2183 return rc;
2186 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2187 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2188 set_bit(kvm_to_feat[i][1], features);
2191 return 0;
2194 static int configure_cpu_feat(const S390FeatBitmap features)
2196 struct kvm_s390_vm_cpu_feat prop = {};
2197 struct kvm_device_attr attr = {
2198 .group = KVM_S390_VM_CPU_MODEL,
2199 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2200 .addr = (uint64_t) &prop,
2202 int i;
2204 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2205 if (test_bit(kvm_to_feat[i][1], features)) {
2206 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2209 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2212 bool kvm_s390_cpu_models_supported(void)
2214 if (!cpu_model_allowed()) {
2215 /* compatibility machines interfere with the cpu model */
2216 return false;
2218 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2219 KVM_S390_VM_CPU_MACHINE) &&
2220 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2221 KVM_S390_VM_CPU_PROCESSOR) &&
2222 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2223 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2224 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2225 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2226 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2227 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2230 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2232 struct kvm_s390_vm_cpu_machine prop = {};
2233 struct kvm_device_attr attr = {
2234 .group = KVM_S390_VM_CPU_MODEL,
2235 .attr = KVM_S390_VM_CPU_MACHINE,
2236 .addr = (uint64_t) &prop,
2238 uint16_t unblocked_ibc = 0, cpu_type = 0;
2239 int rc;
2241 memset(model, 0, sizeof(*model));
2243 if (!kvm_s390_cpu_models_supported()) {
2244 error_setg(errp, "KVM doesn't support CPU models");
2245 return;
2248 /* query the basic cpu model properties */
2249 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2250 if (rc) {
2251 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2252 return;
2255 cpu_type = cpuid_type(prop.cpuid);
2256 if (has_ibc(prop.ibc)) {
2257 model->lowest_ibc = lowest_ibc(prop.ibc);
2258 unblocked_ibc = unblocked_ibc(prop.ibc);
2260 model->cpu_id = cpuid_id(prop.cpuid);
2261 model->cpu_id_format = cpuid_format(prop.cpuid);
2262 model->cpu_ver = 0xff;
2264 /* get supported cpu features indicated via STFL(E) */
2265 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2266 (uint8_t *) prop.fac_mask);
2267 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2268 if (test_bit(S390_FEAT_STFLE, model->features)) {
2269 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2271 /* get supported cpu features indicated e.g. via SCLP */
2272 rc = query_cpu_feat(model->features);
2273 if (rc) {
2274 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2275 return;
2277 /* get supported cpu subfunctions indicated via query / test bit */
2278 rc = query_cpu_subfunc(model->features);
2279 if (rc) {
2280 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2281 return;
2284 /* PTFF subfunctions might be indicated although kernel support missing */
2285 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) {
2286 clear_bit(S390_FEAT_PTFF_QSIE, model->features);
2287 clear_bit(S390_FEAT_PTFF_QTOUE, model->features);
2288 clear_bit(S390_FEAT_PTFF_STOE, model->features);
2289 clear_bit(S390_FEAT_PTFF_STOUE, model->features);
2292 /* with cpu model support, CMM is only indicated if really available */
2293 if (kvm_s390_cmma_available()) {
2294 set_bit(S390_FEAT_CMM, model->features);
2295 } else {
2296 /* no cmm -> no cmm nt */
2297 clear_bit(S390_FEAT_CMM_NT, model->features);
2300 /* bpb needs kernel support for migration, VSIE and reset */
2301 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) {
2302 clear_bit(S390_FEAT_BPB, model->features);
2305 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2306 set_bit(S390_FEAT_ZPCI, model->features);
2307 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2309 if (s390_known_cpu_type(cpu_type)) {
2310 /* we want the exact model, even if some features are missing */
2311 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2312 ibc_ec_ga(unblocked_ibc), NULL);
2313 } else {
2314 /* model unknown, e.g. too new - search using features */
2315 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2316 ibc_ec_ga(unblocked_ibc),
2317 model->features);
2319 if (!model->def) {
2320 error_setg(errp, "KVM: host CPU model could not be identified");
2321 return;
2323 /* for now, we can only provide the AP feature with HW support */
2324 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO,
2325 KVM_S390_VM_CRYPTO_ENABLE_APIE)) {
2326 set_bit(S390_FEAT_AP, model->features);
2328 /* strip of features that are not part of the maximum model */
2329 bitmap_and(model->features, model->features, model->def->full_feat,
2330 S390_FEAT_MAX);
2333 static void kvm_s390_configure_apie(bool interpret)
2335 uint64_t attr = interpret ? KVM_S390_VM_CRYPTO_ENABLE_APIE :
2336 KVM_S390_VM_CRYPTO_DISABLE_APIE;
2338 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
2339 kvm_s390_set_attr(attr);
2343 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2345 struct kvm_s390_vm_cpu_processor prop = {
2346 .fac_list = { 0 },
2348 struct kvm_device_attr attr = {
2349 .group = KVM_S390_VM_CPU_MODEL,
2350 .attr = KVM_S390_VM_CPU_PROCESSOR,
2351 .addr = (uint64_t) &prop,
2353 int rc;
2355 if (!model) {
2356 /* compatibility handling if cpu models are disabled */
2357 if (kvm_s390_cmma_available()) {
2358 kvm_s390_enable_cmma();
2360 return;
2362 if (!kvm_s390_cpu_models_supported()) {
2363 error_setg(errp, "KVM doesn't support CPU models");
2364 return;
2366 prop.cpuid = s390_cpuid_from_cpu_model(model);
2367 prop.ibc = s390_ibc_from_cpu_model(model);
2368 /* configure cpu features indicated via STFL(e) */
2369 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2370 (uint8_t *) prop.fac_list);
2371 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2372 if (rc) {
2373 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2374 return;
2376 /* configure cpu features indicated e.g. via SCLP */
2377 rc = configure_cpu_feat(model->features);
2378 if (rc) {
2379 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2380 return;
2382 /* configure cpu subfunctions indicated via query / test bit */
2383 rc = configure_cpu_subfunc(model->features);
2384 if (rc) {
2385 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2386 return;
2388 /* enable CMM via CMMA */
2389 if (test_bit(S390_FEAT_CMM, model->features)) {
2390 kvm_s390_enable_cmma();
2393 if (test_bit(S390_FEAT_AP, model->features)) {
2394 kvm_s390_configure_apie(true);
2398 void kvm_s390_restart_interrupt(S390CPU *cpu)
2400 struct kvm_s390_irq irq = {
2401 .type = KVM_S390_RESTART,
2404 kvm_s390_vcpu_interrupt(cpu, &irq);
2407 void kvm_s390_stop_interrupt(S390CPU *cpu)
2409 struct kvm_s390_irq irq = {
2410 .type = KVM_S390_SIGP_STOP,
2413 kvm_s390_vcpu_interrupt(cpu, &irq);