tests: add test-list-simpleq
[qemu/ar7.git] / target / s390x / kvm.c
blob348e8cc5467a3dbc1cdc4c5c109b60210da7b9b4
1 /*
2 * QEMU S390x KVM implementation
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp. 2012
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * Contributions after 2012-10-29 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
20 * You should have received a copy of the GNU (Lesser) General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "qemu/osdep.h"
25 #include <sys/ioctl.h>
27 #include <linux/kvm.h>
28 #include <asm/ptrace.h>
30 #include "qemu-common.h"
31 #include "cpu.h"
32 #include "internal.h"
33 #include "kvm_s390x.h"
34 #include "qapi/error.h"
35 #include "qemu/error-report.h"
36 #include "qemu/timer.h"
37 #include "qemu/units.h"
38 #include "qemu/mmap-alloc.h"
39 #include "sysemu/sysemu.h"
40 #include "sysemu/hw_accel.h"
41 #include "hw/hw.h"
42 #include "sysemu/device_tree.h"
43 #include "exec/gdbstub.h"
44 #include "trace.h"
45 #include "hw/s390x/s390-pci-inst.h"
46 #include "hw/s390x/s390-pci-bus.h"
47 #include "hw/s390x/ipl.h"
48 #include "hw/s390x/ebcdic.h"
49 #include "exec/memattrs.h"
50 #include "hw/s390x/s390-virtio-ccw.h"
51 #include "hw/s390x/s390-virtio-hcall.h"
53 #ifndef DEBUG_KVM
54 #define DEBUG_KVM 0
55 #endif
57 #define DPRINTF(fmt, ...) do { \
58 if (DEBUG_KVM) { \
59 fprintf(stderr, fmt, ## __VA_ARGS__); \
60 } \
61 } while (0)
63 #define kvm_vm_check_mem_attr(s, attr) \
64 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
66 #define IPA0_DIAG 0x8300
67 #define IPA0_SIGP 0xae00
68 #define IPA0_B2 0xb200
69 #define IPA0_B9 0xb900
70 #define IPA0_EB 0xeb00
71 #define IPA0_E3 0xe300
73 #define PRIV_B2_SCLP_CALL 0x20
74 #define PRIV_B2_CSCH 0x30
75 #define PRIV_B2_HSCH 0x31
76 #define PRIV_B2_MSCH 0x32
77 #define PRIV_B2_SSCH 0x33
78 #define PRIV_B2_STSCH 0x34
79 #define PRIV_B2_TSCH 0x35
80 #define PRIV_B2_TPI 0x36
81 #define PRIV_B2_SAL 0x37
82 #define PRIV_B2_RSCH 0x38
83 #define PRIV_B2_STCRW 0x39
84 #define PRIV_B2_STCPS 0x3a
85 #define PRIV_B2_RCHP 0x3b
86 #define PRIV_B2_SCHM 0x3c
87 #define PRIV_B2_CHSC 0x5f
88 #define PRIV_B2_SIGA 0x74
89 #define PRIV_B2_XSCH 0x76
91 #define PRIV_EB_SQBS 0x8a
92 #define PRIV_EB_PCISTB 0xd0
93 #define PRIV_EB_SIC 0xd1
95 #define PRIV_B9_EQBS 0x9c
96 #define PRIV_B9_CLP 0xa0
97 #define PRIV_B9_PCISTG 0xd0
98 #define PRIV_B9_PCILG 0xd2
99 #define PRIV_B9_RPCIT 0xd3
101 #define PRIV_E3_MPCIFC 0xd0
102 #define PRIV_E3_STPCIFC 0xd4
104 #define DIAG_TIMEREVENT 0x288
105 #define DIAG_IPL 0x308
106 #define DIAG_KVM_HYPERCALL 0x500
107 #define DIAG_KVM_BREAKPOINT 0x501
109 #define ICPT_INSTRUCTION 0x04
110 #define ICPT_PROGRAM 0x08
111 #define ICPT_EXT_INT 0x14
112 #define ICPT_WAITPSW 0x1c
113 #define ICPT_SOFT_INTERCEPT 0x24
114 #define ICPT_CPU_STOP 0x28
115 #define ICPT_OPEREXC 0x2c
116 #define ICPT_IO 0x40
118 #define NR_LOCAL_IRQS 32
120 * Needs to be big enough to contain max_cpus emergency signals
121 * and in addition NR_LOCAL_IRQS interrupts
123 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
124 (max_cpus + NR_LOCAL_IRQS))
126 static CPUWatchpoint hw_watchpoint;
128 * We don't use a list because this structure is also used to transmit the
129 * hardware breakpoints to the kernel.
131 static struct kvm_hw_breakpoint *hw_breakpoints;
132 static int nb_hw_breakpoints;
134 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
135 KVM_CAP_LAST_INFO
138 static int cap_sync_regs;
139 static int cap_async_pf;
140 static int cap_mem_op;
141 static int cap_s390_irq;
142 static int cap_ri;
143 static int cap_gs;
144 static int cap_hpage_1m;
146 static int active_cmma;
148 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared);
150 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
152 struct kvm_device_attr attr = {
153 .group = KVM_S390_VM_MEM_CTRL,
154 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
155 .addr = (uint64_t) memory_limit,
158 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
161 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit)
163 int rc;
165 struct kvm_device_attr attr = {
166 .group = KVM_S390_VM_MEM_CTRL,
167 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
168 .addr = (uint64_t) &new_limit,
171 if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) {
172 return 0;
175 rc = kvm_s390_query_mem_limit(hw_limit);
176 if (rc) {
177 return rc;
178 } else if (*hw_limit < new_limit) {
179 return -E2BIG;
182 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
185 int kvm_s390_cmma_active(void)
187 return active_cmma;
190 static bool kvm_s390_cmma_available(void)
192 static bool initialized, value;
194 if (!initialized) {
195 initialized = true;
196 value = kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_ENABLE_CMMA) &&
197 kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_CLR_CMMA);
199 return value;
202 void kvm_s390_cmma_reset(void)
204 int rc;
205 struct kvm_device_attr attr = {
206 .group = KVM_S390_VM_MEM_CTRL,
207 .attr = KVM_S390_VM_MEM_CLR_CMMA,
210 if (!kvm_s390_cmma_active()) {
211 return;
214 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
215 trace_kvm_clear_cmma(rc);
218 static void kvm_s390_enable_cmma(void)
220 int rc;
221 struct kvm_device_attr attr = {
222 .group = KVM_S390_VM_MEM_CTRL,
223 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
226 if (cap_hpage_1m) {
227 warn_report("CMM will not be enabled because it is not "
228 "compatible with huge memory backings.");
229 return;
231 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
232 active_cmma = !rc;
233 trace_kvm_enable_cmma(rc);
236 static void kvm_s390_set_attr(uint64_t attr)
238 struct kvm_device_attr attribute = {
239 .group = KVM_S390_VM_CRYPTO,
240 .attr = attr,
243 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
245 if (ret) {
246 error_report("Failed to set crypto device attribute %lu: %s",
247 attr, strerror(-ret));
251 static void kvm_s390_init_aes_kw(void)
253 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
255 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
256 NULL)) {
257 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
260 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
261 kvm_s390_set_attr(attr);
265 static void kvm_s390_init_dea_kw(void)
267 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
269 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
270 NULL)) {
271 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
274 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
275 kvm_s390_set_attr(attr);
279 void kvm_s390_crypto_reset(void)
281 if (s390_has_feat(S390_FEAT_MSA_EXT_3)) {
282 kvm_s390_init_aes_kw();
283 kvm_s390_init_dea_kw();
287 static int kvm_s390_configure_mempath_backing(KVMState *s)
289 size_t path_psize = qemu_mempath_getpagesize(mem_path);
291 if (path_psize == 4 * KiB) {
292 return 0;
295 if (path_psize != 1 * MiB) {
296 error_report("Memory backing with 2G pages was specified, "
297 "but KVM does not support this memory backing");
298 return -EINVAL;
301 if (kvm_vm_enable_cap(s, KVM_CAP_S390_HPAGE_1M, 0)) {
302 error_report("Memory backing with 1M pages was specified, "
303 "but KVM does not support this memory backing");
304 return -EINVAL;
307 cap_hpage_1m = 1;
308 return 0;
311 int kvm_arch_init(MachineState *ms, KVMState *s)
313 MachineClass *mc = MACHINE_GET_CLASS(ms);
315 if (mem_path && kvm_s390_configure_mempath_backing(s)) {
316 return -EINVAL;
319 mc->default_cpu_type = S390_CPU_TYPE_NAME("host");
320 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
321 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
322 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
323 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
325 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
326 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
327 phys_mem_set_alloc(legacy_s390_alloc);
330 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
331 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
332 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
333 if (ri_allowed()) {
334 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
335 cap_ri = 1;
338 if (cpu_model_allowed()) {
339 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
340 cap_gs = 1;
345 * The migration interface for ais was introduced with kernel 4.13
346 * but the capability itself had been active since 4.12. As migration
347 * support is considered necessary let's disable ais in the 2.10
348 * machine.
350 /* kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0); */
352 return 0;
355 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
357 return 0;
360 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
362 return cpu->cpu_index;
365 int kvm_arch_init_vcpu(CPUState *cs)
367 S390CPU *cpu = S390_CPU(cs);
368 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
369 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
370 return 0;
373 void kvm_s390_reset_vcpu(S390CPU *cpu)
375 CPUState *cs = CPU(cpu);
377 /* The initial reset call is needed here to reset in-kernel
378 * vcpu data that we can't access directly from QEMU
379 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
380 * Before this ioctl cpu_synchronize_state() is called in common kvm
381 * code (kvm-all) */
382 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
383 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
387 static int can_sync_regs(CPUState *cs, int regs)
389 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
392 int kvm_arch_put_registers(CPUState *cs, int level)
394 S390CPU *cpu = S390_CPU(cs);
395 CPUS390XState *env = &cpu->env;
396 struct kvm_sregs sregs;
397 struct kvm_regs regs;
398 struct kvm_fpu fpu = {};
399 int r;
400 int i;
402 /* always save the PSW and the GPRS*/
403 cs->kvm_run->psw_addr = env->psw.addr;
404 cs->kvm_run->psw_mask = env->psw.mask;
406 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
407 for (i = 0; i < 16; i++) {
408 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
409 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
411 } else {
412 for (i = 0; i < 16; i++) {
413 regs.gprs[i] = env->regs[i];
415 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
416 if (r < 0) {
417 return r;
421 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
422 for (i = 0; i < 32; i++) {
423 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
424 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
426 cs->kvm_run->s.regs.fpc = env->fpc;
427 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
428 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
429 for (i = 0; i < 16; i++) {
430 cs->kvm_run->s.regs.fprs[i] = get_freg(env, i)->ll;
432 cs->kvm_run->s.regs.fpc = env->fpc;
433 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
434 } else {
435 /* Floating point */
436 for (i = 0; i < 16; i++) {
437 fpu.fprs[i] = get_freg(env, i)->ll;
439 fpu.fpc = env->fpc;
441 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
442 if (r < 0) {
443 return r;
447 /* Do we need to save more than that? */
448 if (level == KVM_PUT_RUNTIME_STATE) {
449 return 0;
452 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
453 cs->kvm_run->s.regs.cputm = env->cputm;
454 cs->kvm_run->s.regs.ckc = env->ckc;
455 cs->kvm_run->s.regs.todpr = env->todpr;
456 cs->kvm_run->s.regs.gbea = env->gbea;
457 cs->kvm_run->s.regs.pp = env->pp;
458 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
459 } else {
461 * These ONE_REGS are not protected by a capability. As they are only
462 * necessary for migration we just trace a possible error, but don't
463 * return with an error return code.
465 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
466 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
467 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
468 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
469 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
472 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
473 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
474 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
477 /* pfault parameters */
478 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
479 cs->kvm_run->s.regs.pft = env->pfault_token;
480 cs->kvm_run->s.regs.pfs = env->pfault_select;
481 cs->kvm_run->s.regs.pfc = env->pfault_compare;
482 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
483 } else if (cap_async_pf) {
484 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
485 if (r < 0) {
486 return r;
488 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
489 if (r < 0) {
490 return r;
492 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
493 if (r < 0) {
494 return r;
498 /* access registers and control registers*/
499 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
500 for (i = 0; i < 16; i++) {
501 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
502 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
504 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
505 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
506 } else {
507 for (i = 0; i < 16; i++) {
508 sregs.acrs[i] = env->aregs[i];
509 sregs.crs[i] = env->cregs[i];
511 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
512 if (r < 0) {
513 return r;
517 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
518 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
519 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
522 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
523 cs->kvm_run->s.regs.bpbc = env->bpbc;
524 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_BPBC;
527 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
528 cs->kvm_run->s.regs.etoken = env->etoken;
529 cs->kvm_run->s.regs.etoken_extension = env->etoken_extension;
530 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ETOKEN;
533 /* Finally the prefix */
534 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
535 cs->kvm_run->s.regs.prefix = env->psa;
536 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
537 } else {
538 /* prefix is only supported via sync regs */
540 return 0;
543 int kvm_arch_get_registers(CPUState *cs)
545 S390CPU *cpu = S390_CPU(cs);
546 CPUS390XState *env = &cpu->env;
547 struct kvm_sregs sregs;
548 struct kvm_regs regs;
549 struct kvm_fpu fpu;
550 int i, r;
552 /* get the PSW */
553 env->psw.addr = cs->kvm_run->psw_addr;
554 env->psw.mask = cs->kvm_run->psw_mask;
556 /* the GPRS */
557 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
558 for (i = 0; i < 16; i++) {
559 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
561 } else {
562 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
563 if (r < 0) {
564 return r;
566 for (i = 0; i < 16; i++) {
567 env->regs[i] = regs.gprs[i];
571 /* The ACRS and CRS */
572 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
573 for (i = 0; i < 16; i++) {
574 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
575 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
577 } else {
578 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
579 if (r < 0) {
580 return r;
582 for (i = 0; i < 16; i++) {
583 env->aregs[i] = sregs.acrs[i];
584 env->cregs[i] = sregs.crs[i];
588 /* Floating point and vector registers */
589 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
590 for (i = 0; i < 32; i++) {
591 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
592 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
594 env->fpc = cs->kvm_run->s.regs.fpc;
595 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
596 for (i = 0; i < 16; i++) {
597 get_freg(env, i)->ll = cs->kvm_run->s.regs.fprs[i];
599 env->fpc = cs->kvm_run->s.regs.fpc;
600 } else {
601 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
602 if (r < 0) {
603 return r;
605 for (i = 0; i < 16; i++) {
606 get_freg(env, i)->ll = fpu.fprs[i];
608 env->fpc = fpu.fpc;
611 /* The prefix */
612 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
613 env->psa = cs->kvm_run->s.regs.prefix;
616 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
617 env->cputm = cs->kvm_run->s.regs.cputm;
618 env->ckc = cs->kvm_run->s.regs.ckc;
619 env->todpr = cs->kvm_run->s.regs.todpr;
620 env->gbea = cs->kvm_run->s.regs.gbea;
621 env->pp = cs->kvm_run->s.regs.pp;
622 } else {
624 * These ONE_REGS are not protected by a capability. As they are only
625 * necessary for migration we just trace a possible error, but don't
626 * return with an error return code.
628 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
629 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
630 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
631 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
632 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
635 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
636 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
639 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
640 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
643 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
644 env->bpbc = cs->kvm_run->s.regs.bpbc;
647 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
648 env->etoken = cs->kvm_run->s.regs.etoken;
649 env->etoken_extension = cs->kvm_run->s.regs.etoken_extension;
652 /* pfault parameters */
653 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
654 env->pfault_token = cs->kvm_run->s.regs.pft;
655 env->pfault_select = cs->kvm_run->s.regs.pfs;
656 env->pfault_compare = cs->kvm_run->s.regs.pfc;
657 } else if (cap_async_pf) {
658 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
659 if (r < 0) {
660 return r;
662 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
663 if (r < 0) {
664 return r;
666 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
667 if (r < 0) {
668 return r;
672 return 0;
675 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
677 int r;
678 struct kvm_device_attr attr = {
679 .group = KVM_S390_VM_TOD,
680 .attr = KVM_S390_VM_TOD_LOW,
681 .addr = (uint64_t)tod_low,
684 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
685 if (r) {
686 return r;
689 attr.attr = KVM_S390_VM_TOD_HIGH;
690 attr.addr = (uint64_t)tod_high;
691 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
694 int kvm_s390_get_clock_ext(uint8_t *tod_high, uint64_t *tod_low)
696 int r;
697 struct kvm_s390_vm_tod_clock gtod;
698 struct kvm_device_attr attr = {
699 .group = KVM_S390_VM_TOD,
700 .attr = KVM_S390_VM_TOD_EXT,
701 .addr = (uint64_t)&gtod,
704 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
705 *tod_high = gtod.epoch_idx;
706 *tod_low = gtod.tod;
708 return r;
711 int kvm_s390_set_clock(uint8_t tod_high, uint64_t tod_low)
713 int r;
714 struct kvm_device_attr attr = {
715 .group = KVM_S390_VM_TOD,
716 .attr = KVM_S390_VM_TOD_LOW,
717 .addr = (uint64_t)&tod_low,
720 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
721 if (r) {
722 return r;
725 attr.attr = KVM_S390_VM_TOD_HIGH;
726 attr.addr = (uint64_t)&tod_high;
727 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
730 int kvm_s390_set_clock_ext(uint8_t tod_high, uint64_t tod_low)
732 struct kvm_s390_vm_tod_clock gtod = {
733 .epoch_idx = tod_high,
734 .tod = tod_low,
736 struct kvm_device_attr attr = {
737 .group = KVM_S390_VM_TOD,
738 .attr = KVM_S390_VM_TOD_EXT,
739 .addr = (uint64_t)&gtod,
742 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
746 * kvm_s390_mem_op:
747 * @addr: the logical start address in guest memory
748 * @ar: the access register number
749 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
750 * @len: length that should be transferred
751 * @is_write: true = write, false = read
752 * Returns: 0 on success, non-zero if an exception or error occurred
754 * Use KVM ioctl to read/write from/to guest memory. An access exception
755 * is injected into the vCPU in case of translation errors.
757 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
758 int len, bool is_write)
760 struct kvm_s390_mem_op mem_op = {
761 .gaddr = addr,
762 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
763 .size = len,
764 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
765 : KVM_S390_MEMOP_LOGICAL_READ,
766 .buf = (uint64_t)hostbuf,
767 .ar = ar,
769 int ret;
771 if (!cap_mem_op) {
772 return -ENOSYS;
774 if (!hostbuf) {
775 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
778 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
779 if (ret < 0) {
780 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
782 return ret;
786 * Legacy layout for s390:
787 * Older S390 KVM requires the topmost vma of the RAM to be
788 * smaller than an system defined value, which is at least 256GB.
789 * Larger systems have larger values. We put the guest between
790 * the end of data segment (system break) and this value. We
791 * use 32GB as a base to have enough room for the system break
792 * to grow. We also have to use MAP parameters that avoid
793 * read-only mapping of guest pages.
795 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared)
797 static void *mem;
799 if (mem) {
800 /* we only support one allocation, which is enough for initial ram */
801 return NULL;
804 mem = mmap((void *) 0x800000000ULL, size,
805 PROT_EXEC|PROT_READ|PROT_WRITE,
806 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
807 if (mem == MAP_FAILED) {
808 mem = NULL;
810 if (mem && align) {
811 *align = QEMU_VMALLOC_ALIGN;
813 return mem;
816 static uint8_t const *sw_bp_inst;
817 static uint8_t sw_bp_ilen;
819 static void determine_sw_breakpoint_instr(void)
821 /* DIAG 501 is used for sw breakpoints with old kernels */
822 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
823 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
824 static const uint8_t instr_0x0000[] = {0x00, 0x00};
826 if (sw_bp_inst) {
827 return;
829 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
830 sw_bp_inst = diag_501;
831 sw_bp_ilen = sizeof(diag_501);
832 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
833 } else {
834 sw_bp_inst = instr_0x0000;
835 sw_bp_ilen = sizeof(instr_0x0000);
836 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
840 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
842 determine_sw_breakpoint_instr();
844 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
845 sw_bp_ilen, 0) ||
846 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
847 return -EINVAL;
849 return 0;
852 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
854 uint8_t t[MAX_ILEN];
856 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
857 return -EINVAL;
858 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
859 return -EINVAL;
860 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
861 sw_bp_ilen, 1)) {
862 return -EINVAL;
865 return 0;
868 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
869 int len, int type)
871 int n;
873 for (n = 0; n < nb_hw_breakpoints; n++) {
874 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
875 (hw_breakpoints[n].len == len || len == -1)) {
876 return &hw_breakpoints[n];
880 return NULL;
883 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
885 int size;
887 if (find_hw_breakpoint(addr, len, type)) {
888 return -EEXIST;
891 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
893 if (!hw_breakpoints) {
894 nb_hw_breakpoints = 0;
895 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
896 } else {
897 hw_breakpoints =
898 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
901 if (!hw_breakpoints) {
902 nb_hw_breakpoints = 0;
903 return -ENOMEM;
906 hw_breakpoints[nb_hw_breakpoints].addr = addr;
907 hw_breakpoints[nb_hw_breakpoints].len = len;
908 hw_breakpoints[nb_hw_breakpoints].type = type;
910 nb_hw_breakpoints++;
912 return 0;
915 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
916 target_ulong len, int type)
918 switch (type) {
919 case GDB_BREAKPOINT_HW:
920 type = KVM_HW_BP;
921 break;
922 case GDB_WATCHPOINT_WRITE:
923 if (len < 1) {
924 return -EINVAL;
926 type = KVM_HW_WP_WRITE;
927 break;
928 default:
929 return -ENOSYS;
931 return insert_hw_breakpoint(addr, len, type);
934 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
935 target_ulong len, int type)
937 int size;
938 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
940 if (bp == NULL) {
941 return -ENOENT;
944 nb_hw_breakpoints--;
945 if (nb_hw_breakpoints > 0) {
947 * In order to trim the array, move the last element to the position to
948 * be removed - if necessary.
950 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
951 *bp = hw_breakpoints[nb_hw_breakpoints];
953 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
954 hw_breakpoints =
955 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
956 } else {
957 g_free(hw_breakpoints);
958 hw_breakpoints = NULL;
961 return 0;
964 void kvm_arch_remove_all_hw_breakpoints(void)
966 nb_hw_breakpoints = 0;
967 g_free(hw_breakpoints);
968 hw_breakpoints = NULL;
971 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
973 int i;
975 if (nb_hw_breakpoints > 0) {
976 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
977 dbg->arch.hw_bp = hw_breakpoints;
979 for (i = 0; i < nb_hw_breakpoints; ++i) {
980 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
981 hw_breakpoints[i].addr);
983 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
984 } else {
985 dbg->arch.nr_hw_bp = 0;
986 dbg->arch.hw_bp = NULL;
990 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
994 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
996 return MEMTXATTRS_UNSPECIFIED;
999 int kvm_arch_process_async_events(CPUState *cs)
1001 return cs->halted;
1004 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
1005 struct kvm_s390_interrupt *interrupt)
1007 int r = 0;
1009 interrupt->type = irq->type;
1010 switch (irq->type) {
1011 case KVM_S390_INT_VIRTIO:
1012 interrupt->parm = irq->u.ext.ext_params;
1013 /* fall through */
1014 case KVM_S390_INT_PFAULT_INIT:
1015 case KVM_S390_INT_PFAULT_DONE:
1016 interrupt->parm64 = irq->u.ext.ext_params2;
1017 break;
1018 case KVM_S390_PROGRAM_INT:
1019 interrupt->parm = irq->u.pgm.code;
1020 break;
1021 case KVM_S390_SIGP_SET_PREFIX:
1022 interrupt->parm = irq->u.prefix.address;
1023 break;
1024 case KVM_S390_INT_SERVICE:
1025 interrupt->parm = irq->u.ext.ext_params;
1026 break;
1027 case KVM_S390_MCHK:
1028 interrupt->parm = irq->u.mchk.cr14;
1029 interrupt->parm64 = irq->u.mchk.mcic;
1030 break;
1031 case KVM_S390_INT_EXTERNAL_CALL:
1032 interrupt->parm = irq->u.extcall.code;
1033 break;
1034 case KVM_S390_INT_EMERGENCY:
1035 interrupt->parm = irq->u.emerg.code;
1036 break;
1037 case KVM_S390_SIGP_STOP:
1038 case KVM_S390_RESTART:
1039 break; /* These types have no parameters */
1040 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1041 interrupt->parm = irq->u.io.subchannel_id << 16;
1042 interrupt->parm |= irq->u.io.subchannel_nr;
1043 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
1044 interrupt->parm64 |= irq->u.io.io_int_word;
1045 break;
1046 default:
1047 r = -EINVAL;
1048 break;
1050 return r;
1053 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
1055 struct kvm_s390_interrupt kvmint = {};
1056 int r;
1058 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1059 if (r < 0) {
1060 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1061 exit(1);
1064 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
1065 if (r < 0) {
1066 fprintf(stderr, "KVM failed to inject interrupt\n");
1067 exit(1);
1071 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
1073 CPUState *cs = CPU(cpu);
1074 int r;
1076 if (cap_s390_irq) {
1077 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
1078 if (!r) {
1079 return;
1081 error_report("KVM failed to inject interrupt %llx", irq->type);
1082 exit(1);
1085 inject_vcpu_irq_legacy(cs, irq);
1088 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq *irq)
1090 struct kvm_s390_interrupt kvmint = {};
1091 int r;
1093 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1094 if (r < 0) {
1095 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1096 exit(1);
1099 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1100 if (r < 0) {
1101 fprintf(stderr, "KVM failed to inject interrupt\n");
1102 exit(1);
1106 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1108 struct kvm_s390_irq irq = {
1109 .type = KVM_S390_PROGRAM_INT,
1110 .u.pgm.code = code,
1113 kvm_s390_vcpu_interrupt(cpu, &irq);
1116 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1118 struct kvm_s390_irq irq = {
1119 .type = KVM_S390_PROGRAM_INT,
1120 .u.pgm.code = code,
1121 .u.pgm.trans_exc_code = te_code,
1122 .u.pgm.exc_access_id = te_code & 3,
1125 kvm_s390_vcpu_interrupt(cpu, &irq);
1128 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1129 uint16_t ipbh0)
1131 CPUS390XState *env = &cpu->env;
1132 uint64_t sccb;
1133 uint32_t code;
1134 int r = 0;
1136 sccb = env->regs[ipbh0 & 0xf];
1137 code = env->regs[(ipbh0 & 0xf0) >> 4];
1139 r = sclp_service_call(env, sccb, code);
1140 if (r < 0) {
1141 kvm_s390_program_interrupt(cpu, -r);
1142 } else {
1143 setcc(cpu, r);
1146 return 0;
1149 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1151 CPUS390XState *env = &cpu->env;
1152 int rc = 0;
1153 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1155 switch (ipa1) {
1156 case PRIV_B2_XSCH:
1157 ioinst_handle_xsch(cpu, env->regs[1], RA_IGNORED);
1158 break;
1159 case PRIV_B2_CSCH:
1160 ioinst_handle_csch(cpu, env->regs[1], RA_IGNORED);
1161 break;
1162 case PRIV_B2_HSCH:
1163 ioinst_handle_hsch(cpu, env->regs[1], RA_IGNORED);
1164 break;
1165 case PRIV_B2_MSCH:
1166 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1167 break;
1168 case PRIV_B2_SSCH:
1169 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1170 break;
1171 case PRIV_B2_STCRW:
1172 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb, RA_IGNORED);
1173 break;
1174 case PRIV_B2_STSCH:
1175 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1176 break;
1177 case PRIV_B2_TSCH:
1178 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1179 fprintf(stderr, "Spurious tsch intercept\n");
1180 break;
1181 case PRIV_B2_CHSC:
1182 ioinst_handle_chsc(cpu, run->s390_sieic.ipb, RA_IGNORED);
1183 break;
1184 case PRIV_B2_TPI:
1185 /* This should have been handled by kvm already. */
1186 fprintf(stderr, "Spurious tpi intercept\n");
1187 break;
1188 case PRIV_B2_SCHM:
1189 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1190 run->s390_sieic.ipb, RA_IGNORED);
1191 break;
1192 case PRIV_B2_RSCH:
1193 ioinst_handle_rsch(cpu, env->regs[1], RA_IGNORED);
1194 break;
1195 case PRIV_B2_RCHP:
1196 ioinst_handle_rchp(cpu, env->regs[1], RA_IGNORED);
1197 break;
1198 case PRIV_B2_STCPS:
1199 /* We do not provide this instruction, it is suppressed. */
1200 break;
1201 case PRIV_B2_SAL:
1202 ioinst_handle_sal(cpu, env->regs[1], RA_IGNORED);
1203 break;
1204 case PRIV_B2_SIGA:
1205 /* Not provided, set CC = 3 for subchannel not operational */
1206 setcc(cpu, 3);
1207 break;
1208 case PRIV_B2_SCLP_CALL:
1209 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1210 break;
1211 default:
1212 rc = -1;
1213 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1214 break;
1217 return rc;
1220 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1221 uint8_t *ar)
1223 CPUS390XState *env = &cpu->env;
1224 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1225 uint32_t base2 = run->s390_sieic.ipb >> 28;
1226 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1227 ((run->s390_sieic.ipb & 0xff00) << 4);
1229 if (disp2 & 0x80000) {
1230 disp2 += 0xfff00000;
1232 if (ar) {
1233 *ar = base2;
1236 return (base2 ? env->regs[base2] : 0) +
1237 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1240 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1241 uint8_t *ar)
1243 CPUS390XState *env = &cpu->env;
1244 uint32_t base2 = run->s390_sieic.ipb >> 28;
1245 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1246 ((run->s390_sieic.ipb & 0xff00) << 4);
1248 if (disp2 & 0x80000) {
1249 disp2 += 0xfff00000;
1251 if (ar) {
1252 *ar = base2;
1255 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1258 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1260 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1262 if (s390_has_feat(S390_FEAT_ZPCI)) {
1263 return clp_service_call(cpu, r2, RA_IGNORED);
1264 } else {
1265 return -1;
1269 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1271 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1272 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1274 if (s390_has_feat(S390_FEAT_ZPCI)) {
1275 return pcilg_service_call(cpu, r1, r2, RA_IGNORED);
1276 } else {
1277 return -1;
1281 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1283 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1284 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1286 if (s390_has_feat(S390_FEAT_ZPCI)) {
1287 return pcistg_service_call(cpu, r1, r2, RA_IGNORED);
1288 } else {
1289 return -1;
1293 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1295 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1296 uint64_t fiba;
1297 uint8_t ar;
1299 if (s390_has_feat(S390_FEAT_ZPCI)) {
1300 fiba = get_base_disp_rxy(cpu, run, &ar);
1302 return stpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1303 } else {
1304 return -1;
1308 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1310 CPUS390XState *env = &cpu->env;
1311 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1312 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1313 uint8_t isc;
1314 uint16_t mode;
1315 int r;
1317 mode = env->regs[r1] & 0xffff;
1318 isc = (env->regs[r3] >> 27) & 0x7;
1319 r = css_do_sic(env, isc, mode);
1320 if (r) {
1321 kvm_s390_program_interrupt(cpu, -r);
1324 return 0;
1327 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1329 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1330 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1332 if (s390_has_feat(S390_FEAT_ZPCI)) {
1333 return rpcit_service_call(cpu, r1, r2, RA_IGNORED);
1334 } else {
1335 return -1;
1339 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1341 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1342 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1343 uint64_t gaddr;
1344 uint8_t ar;
1346 if (s390_has_feat(S390_FEAT_ZPCI)) {
1347 gaddr = get_base_disp_rsy(cpu, run, &ar);
1349 return pcistb_service_call(cpu, r1, r3, gaddr, ar, RA_IGNORED);
1350 } else {
1351 return -1;
1355 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1357 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1358 uint64_t fiba;
1359 uint8_t ar;
1361 if (s390_has_feat(S390_FEAT_ZPCI)) {
1362 fiba = get_base_disp_rxy(cpu, run, &ar);
1364 return mpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1365 } else {
1366 return -1;
1370 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1372 int r = 0;
1374 switch (ipa1) {
1375 case PRIV_B9_CLP:
1376 r = kvm_clp_service_call(cpu, run);
1377 break;
1378 case PRIV_B9_PCISTG:
1379 r = kvm_pcistg_service_call(cpu, run);
1380 break;
1381 case PRIV_B9_PCILG:
1382 r = kvm_pcilg_service_call(cpu, run);
1383 break;
1384 case PRIV_B9_RPCIT:
1385 r = kvm_rpcit_service_call(cpu, run);
1386 break;
1387 case PRIV_B9_EQBS:
1388 /* just inject exception */
1389 r = -1;
1390 break;
1391 default:
1392 r = -1;
1393 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1394 break;
1397 return r;
1400 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1402 int r = 0;
1404 switch (ipbl) {
1405 case PRIV_EB_PCISTB:
1406 r = kvm_pcistb_service_call(cpu, run);
1407 break;
1408 case PRIV_EB_SIC:
1409 r = kvm_sic_service_call(cpu, run);
1410 break;
1411 case PRIV_EB_SQBS:
1412 /* just inject exception */
1413 r = -1;
1414 break;
1415 default:
1416 r = -1;
1417 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1418 break;
1421 return r;
1424 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1426 int r = 0;
1428 switch (ipbl) {
1429 case PRIV_E3_MPCIFC:
1430 r = kvm_mpcifc_service_call(cpu, run);
1431 break;
1432 case PRIV_E3_STPCIFC:
1433 r = kvm_stpcifc_service_call(cpu, run);
1434 break;
1435 default:
1436 r = -1;
1437 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1438 break;
1441 return r;
1444 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1446 CPUS390XState *env = &cpu->env;
1447 int ret;
1449 ret = s390_virtio_hypercall(env);
1450 if (ret == -EINVAL) {
1451 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1452 return 0;
1455 return ret;
1458 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1460 uint64_t r1, r3;
1461 int rc;
1463 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1464 r3 = run->s390_sieic.ipa & 0x000f;
1465 rc = handle_diag_288(&cpu->env, r1, r3);
1466 if (rc) {
1467 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1471 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1473 uint64_t r1, r3;
1475 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1476 r3 = run->s390_sieic.ipa & 0x000f;
1477 handle_diag_308(&cpu->env, r1, r3, RA_IGNORED);
1480 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1482 CPUS390XState *env = &cpu->env;
1483 unsigned long pc;
1485 pc = env->psw.addr - sw_bp_ilen;
1486 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1487 env->psw.addr = pc;
1488 return EXCP_DEBUG;
1491 return -ENOENT;
1494 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1496 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1498 int r = 0;
1499 uint16_t func_code;
1502 * For any diagnose call we support, bits 48-63 of the resulting
1503 * address specify the function code; the remainder is ignored.
1505 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1506 switch (func_code) {
1507 case DIAG_TIMEREVENT:
1508 kvm_handle_diag_288(cpu, run);
1509 break;
1510 case DIAG_IPL:
1511 kvm_handle_diag_308(cpu, run);
1512 break;
1513 case DIAG_KVM_HYPERCALL:
1514 r = handle_hypercall(cpu, run);
1515 break;
1516 case DIAG_KVM_BREAKPOINT:
1517 r = handle_sw_breakpoint(cpu, run);
1518 break;
1519 default:
1520 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1521 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1522 break;
1525 return r;
1528 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb)
1530 CPUS390XState *env = &cpu->env;
1531 const uint8_t r1 = ipa1 >> 4;
1532 const uint8_t r3 = ipa1 & 0x0f;
1533 int ret;
1534 uint8_t order;
1536 /* get order code */
1537 order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK;
1539 ret = handle_sigp(env, order, r1, r3);
1540 setcc(cpu, ret);
1541 return 0;
1544 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1546 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1547 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1548 int r = -1;
1550 DPRINTF("handle_instruction 0x%x 0x%x\n",
1551 run->s390_sieic.ipa, run->s390_sieic.ipb);
1552 switch (ipa0) {
1553 case IPA0_B2:
1554 r = handle_b2(cpu, run, ipa1);
1555 break;
1556 case IPA0_B9:
1557 r = handle_b9(cpu, run, ipa1);
1558 break;
1559 case IPA0_EB:
1560 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1561 break;
1562 case IPA0_E3:
1563 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1564 break;
1565 case IPA0_DIAG:
1566 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1567 break;
1568 case IPA0_SIGP:
1569 r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb);
1570 break;
1573 if (r < 0) {
1574 r = 0;
1575 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1578 return r;
1581 static void unmanageable_intercept(S390CPU *cpu, S390CrashReason reason,
1582 int pswoffset)
1584 CPUState *cs = CPU(cpu);
1586 s390_cpu_halt(cpu);
1587 cpu->env.crash_reason = reason;
1588 qemu_system_guest_panicked(cpu_get_crash_info(cs));
1591 /* try to detect pgm check loops */
1592 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1594 CPUState *cs = CPU(cpu);
1595 PSW oldpsw, newpsw;
1597 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1598 offsetof(LowCore, program_new_psw));
1599 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1600 offsetof(LowCore, program_new_psw) + 8);
1601 oldpsw.mask = run->psw_mask;
1602 oldpsw.addr = run->psw_addr;
1604 * Avoid endless loops of operation exceptions, if the pgm new
1605 * PSW will cause a new operation exception.
1606 * The heuristic checks if the pgm new psw is within 6 bytes before
1607 * the faulting psw address (with same DAT, AS settings) and the
1608 * new psw is not a wait psw and the fault was not triggered by
1609 * problem state. In that case go into crashed state.
1612 if (oldpsw.addr - newpsw.addr <= 6 &&
1613 !(newpsw.mask & PSW_MASK_WAIT) &&
1614 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1615 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1616 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1617 unmanageable_intercept(cpu, S390_CRASH_REASON_OPINT_LOOP,
1618 offsetof(LowCore, program_new_psw));
1619 return EXCP_HALTED;
1621 return 0;
1624 static int handle_intercept(S390CPU *cpu)
1626 CPUState *cs = CPU(cpu);
1627 struct kvm_run *run = cs->kvm_run;
1628 int icpt_code = run->s390_sieic.icptcode;
1629 int r = 0;
1631 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1632 (long)cs->kvm_run->psw_addr);
1633 switch (icpt_code) {
1634 case ICPT_INSTRUCTION:
1635 r = handle_instruction(cpu, run);
1636 break;
1637 case ICPT_PROGRAM:
1638 unmanageable_intercept(cpu, S390_CRASH_REASON_PGMINT_LOOP,
1639 offsetof(LowCore, program_new_psw));
1640 r = EXCP_HALTED;
1641 break;
1642 case ICPT_EXT_INT:
1643 unmanageable_intercept(cpu, S390_CRASH_REASON_EXTINT_LOOP,
1644 offsetof(LowCore, external_new_psw));
1645 r = EXCP_HALTED;
1646 break;
1647 case ICPT_WAITPSW:
1648 /* disabled wait, since enabled wait is handled in kernel */
1649 s390_handle_wait(cpu);
1650 r = EXCP_HALTED;
1651 break;
1652 case ICPT_CPU_STOP:
1653 do_stop_interrupt(&cpu->env);
1654 r = EXCP_HALTED;
1655 break;
1656 case ICPT_OPEREXC:
1657 /* check for break points */
1658 r = handle_sw_breakpoint(cpu, run);
1659 if (r == -ENOENT) {
1660 /* Then check for potential pgm check loops */
1661 r = handle_oper_loop(cpu, run);
1662 if (r == 0) {
1663 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1666 break;
1667 case ICPT_SOFT_INTERCEPT:
1668 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1669 exit(1);
1670 break;
1671 case ICPT_IO:
1672 fprintf(stderr, "KVM unimplemented icpt IO\n");
1673 exit(1);
1674 break;
1675 default:
1676 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1677 exit(1);
1678 break;
1681 return r;
1684 static int handle_tsch(S390CPU *cpu)
1686 CPUState *cs = CPU(cpu);
1687 struct kvm_run *run = cs->kvm_run;
1688 int ret;
1690 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb,
1691 RA_IGNORED);
1692 if (ret < 0) {
1694 * Failure.
1695 * If an I/O interrupt had been dequeued, we have to reinject it.
1697 if (run->s390_tsch.dequeued) {
1698 s390_io_interrupt(run->s390_tsch.subchannel_id,
1699 run->s390_tsch.subchannel_nr,
1700 run->s390_tsch.io_int_parm,
1701 run->s390_tsch.io_int_word);
1703 ret = 0;
1705 return ret;
1708 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1710 SysIB_322 sysib;
1711 int del;
1713 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1714 return;
1716 /* Shift the stack of Extended Names to prepare for our own data */
1717 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1718 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1719 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1720 * assumed it's not capable of managing Extended Names for lower levels.
1722 for (del = 1; del < sysib.count; del++) {
1723 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1724 break;
1727 if (del < sysib.count) {
1728 memset(sysib.ext_names[del], 0,
1729 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1731 /* Insert short machine name in EBCDIC, padded with blanks */
1732 if (qemu_name) {
1733 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1734 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1735 strlen(qemu_name)));
1737 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1738 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1739 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1740 * considered by s390 as not capable of providing any Extended Name.
1741 * Therefore if no name was specified on qemu invocation, we go with the
1742 * same "KVMguest" default, which KVM has filled into short name field.
1744 if (qemu_name) {
1745 strncpy((char *)sysib.ext_names[0], qemu_name,
1746 sizeof(sysib.ext_names[0]));
1747 } else {
1748 strcpy((char *)sysib.ext_names[0], "KVMguest");
1750 /* Insert UUID */
1751 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
1753 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1756 static int handle_stsi(S390CPU *cpu)
1758 CPUState *cs = CPU(cpu);
1759 struct kvm_run *run = cs->kvm_run;
1761 switch (run->s390_stsi.fc) {
1762 case 3:
1763 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1764 return 0;
1766 /* Only sysib 3.2.2 needs post-handling for now. */
1767 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1768 return 0;
1769 default:
1770 return 0;
1774 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1776 CPUState *cs = CPU(cpu);
1777 struct kvm_run *run = cs->kvm_run;
1779 int ret = 0;
1780 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1782 switch (arch_info->type) {
1783 case KVM_HW_WP_WRITE:
1784 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1785 cs->watchpoint_hit = &hw_watchpoint;
1786 hw_watchpoint.vaddr = arch_info->addr;
1787 hw_watchpoint.flags = BP_MEM_WRITE;
1788 ret = EXCP_DEBUG;
1790 break;
1791 case KVM_HW_BP:
1792 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1793 ret = EXCP_DEBUG;
1795 break;
1796 case KVM_SINGLESTEP:
1797 if (cs->singlestep_enabled) {
1798 ret = EXCP_DEBUG;
1800 break;
1801 default:
1802 ret = -ENOSYS;
1805 return ret;
1808 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1810 S390CPU *cpu = S390_CPU(cs);
1811 int ret = 0;
1813 qemu_mutex_lock_iothread();
1815 kvm_cpu_synchronize_state(cs);
1817 switch (run->exit_reason) {
1818 case KVM_EXIT_S390_SIEIC:
1819 ret = handle_intercept(cpu);
1820 break;
1821 case KVM_EXIT_S390_RESET:
1822 s390_ipl_reset_request(cs, S390_RESET_REIPL);
1823 break;
1824 case KVM_EXIT_S390_TSCH:
1825 ret = handle_tsch(cpu);
1826 break;
1827 case KVM_EXIT_S390_STSI:
1828 ret = handle_stsi(cpu);
1829 break;
1830 case KVM_EXIT_DEBUG:
1831 ret = kvm_arch_handle_debug_exit(cpu);
1832 break;
1833 default:
1834 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1835 break;
1837 qemu_mutex_unlock_iothread();
1839 if (ret == 0) {
1840 ret = EXCP_INTERRUPT;
1842 return ret;
1845 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1847 return true;
1850 void kvm_s390_enable_css_support(S390CPU *cpu)
1852 int r;
1854 /* Activate host kernel channel subsystem support. */
1855 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1856 assert(r == 0);
1859 void kvm_arch_init_irq_routing(KVMState *s)
1862 * Note that while irqchip capabilities generally imply that cpustates
1863 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1864 * have to override the common code kvm_halt_in_kernel_allowed setting.
1866 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1867 kvm_gsi_routing_allowed = true;
1868 kvm_halt_in_kernel_allowed = false;
1872 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1873 int vq, bool assign)
1875 struct kvm_ioeventfd kick = {
1876 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1877 KVM_IOEVENTFD_FLAG_DATAMATCH,
1878 .fd = event_notifier_get_fd(notifier),
1879 .datamatch = vq,
1880 .addr = sch,
1881 .len = 8,
1883 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1884 return -ENOSYS;
1886 if (!assign) {
1887 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1889 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1892 int kvm_s390_get_ri(void)
1894 return cap_ri;
1897 int kvm_s390_get_gs(void)
1899 return cap_gs;
1902 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1904 struct kvm_mp_state mp_state = {};
1905 int ret;
1907 /* the kvm part might not have been initialized yet */
1908 if (CPU(cpu)->kvm_state == NULL) {
1909 return 0;
1912 switch (cpu_state) {
1913 case S390_CPU_STATE_STOPPED:
1914 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1915 break;
1916 case S390_CPU_STATE_CHECK_STOP:
1917 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1918 break;
1919 case S390_CPU_STATE_OPERATING:
1920 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1921 break;
1922 case S390_CPU_STATE_LOAD:
1923 mp_state.mp_state = KVM_MP_STATE_LOAD;
1924 break;
1925 default:
1926 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1927 cpu_state);
1928 exit(1);
1931 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1932 if (ret) {
1933 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1934 strerror(-ret));
1937 return ret;
1940 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
1942 struct kvm_s390_irq_state irq_state = {
1943 .buf = (uint64_t) cpu->irqstate,
1944 .len = VCPU_IRQ_BUF_SIZE,
1946 CPUState *cs = CPU(cpu);
1947 int32_t bytes;
1949 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1950 return;
1953 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
1954 if (bytes < 0) {
1955 cpu->irqstate_saved_size = 0;
1956 error_report("Migration of interrupt state failed");
1957 return;
1960 cpu->irqstate_saved_size = bytes;
1963 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
1965 CPUState *cs = CPU(cpu);
1966 struct kvm_s390_irq_state irq_state = {
1967 .buf = (uint64_t) cpu->irqstate,
1968 .len = cpu->irqstate_saved_size,
1970 int r;
1972 if (cpu->irqstate_saved_size == 0) {
1973 return 0;
1976 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1977 return -ENOSYS;
1980 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
1981 if (r) {
1982 error_report("Setting interrupt state failed %d", r);
1984 return r;
1987 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1988 uint64_t address, uint32_t data, PCIDevice *dev)
1990 S390PCIBusDevice *pbdev;
1991 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
1993 if (!dev) {
1994 DPRINTF("add_msi_route no pci device\n");
1995 return -ENODEV;
1998 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
1999 if (!pbdev) {
2000 DPRINTF("add_msi_route no zpci device\n");
2001 return -ENODEV;
2004 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2005 route->flags = 0;
2006 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2007 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2008 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2009 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
2010 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2011 return 0;
2014 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2015 int vector, PCIDevice *dev)
2017 return 0;
2020 int kvm_arch_release_virq_post(int virq)
2022 return 0;
2025 int kvm_arch_msi_data_to_gsi(uint32_t data)
2027 abort();
2030 static int query_cpu_subfunc(S390FeatBitmap features)
2032 struct kvm_s390_vm_cpu_subfunc prop;
2033 struct kvm_device_attr attr = {
2034 .group = KVM_S390_VM_CPU_MODEL,
2035 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2036 .addr = (uint64_t) &prop,
2038 int rc;
2040 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2041 if (rc) {
2042 return rc;
2046 * We're going to add all subfunctions now, if the corresponding feature
2047 * is available that unlocks the query functions.
2049 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2050 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2051 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2053 if (test_bit(S390_FEAT_MSA, features)) {
2054 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2055 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2056 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2057 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2058 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2060 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2061 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2063 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2064 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2065 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2066 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2067 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2069 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2070 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2072 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2073 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2075 return 0;
2078 static int configure_cpu_subfunc(const S390FeatBitmap features)
2080 struct kvm_s390_vm_cpu_subfunc prop = {};
2081 struct kvm_device_attr attr = {
2082 .group = KVM_S390_VM_CPU_MODEL,
2083 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2084 .addr = (uint64_t) &prop,
2087 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2088 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2089 /* hardware support might be missing, IBC will handle most of this */
2090 return 0;
2093 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2094 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2095 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2097 if (test_bit(S390_FEAT_MSA, features)) {
2098 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2099 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2100 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2101 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2102 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2104 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2105 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2107 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2108 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2109 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2110 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2111 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2113 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2114 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2116 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2117 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2119 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2122 static int kvm_to_feat[][2] = {
2123 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2124 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2125 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2126 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2127 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2128 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2129 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2130 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2131 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2132 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2133 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2134 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2135 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2136 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2139 static int query_cpu_feat(S390FeatBitmap features)
2141 struct kvm_s390_vm_cpu_feat prop;
2142 struct kvm_device_attr attr = {
2143 .group = KVM_S390_VM_CPU_MODEL,
2144 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2145 .addr = (uint64_t) &prop,
2147 int rc;
2148 int i;
2150 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2151 if (rc) {
2152 return rc;
2155 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2156 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2157 set_bit(kvm_to_feat[i][1], features);
2160 return 0;
2163 static int configure_cpu_feat(const S390FeatBitmap features)
2165 struct kvm_s390_vm_cpu_feat prop = {};
2166 struct kvm_device_attr attr = {
2167 .group = KVM_S390_VM_CPU_MODEL,
2168 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2169 .addr = (uint64_t) &prop,
2171 int i;
2173 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2174 if (test_bit(kvm_to_feat[i][1], features)) {
2175 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2178 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2181 bool kvm_s390_cpu_models_supported(void)
2183 if (!cpu_model_allowed()) {
2184 /* compatibility machines interfere with the cpu model */
2185 return false;
2187 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2188 KVM_S390_VM_CPU_MACHINE) &&
2189 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2190 KVM_S390_VM_CPU_PROCESSOR) &&
2191 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2192 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2193 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2194 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2195 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2196 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2199 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2201 struct kvm_s390_vm_cpu_machine prop = {};
2202 struct kvm_device_attr attr = {
2203 .group = KVM_S390_VM_CPU_MODEL,
2204 .attr = KVM_S390_VM_CPU_MACHINE,
2205 .addr = (uint64_t) &prop,
2207 uint16_t unblocked_ibc = 0, cpu_type = 0;
2208 int rc;
2210 memset(model, 0, sizeof(*model));
2212 if (!kvm_s390_cpu_models_supported()) {
2213 error_setg(errp, "KVM doesn't support CPU models");
2214 return;
2217 /* query the basic cpu model properties */
2218 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2219 if (rc) {
2220 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2221 return;
2224 cpu_type = cpuid_type(prop.cpuid);
2225 if (has_ibc(prop.ibc)) {
2226 model->lowest_ibc = lowest_ibc(prop.ibc);
2227 unblocked_ibc = unblocked_ibc(prop.ibc);
2229 model->cpu_id = cpuid_id(prop.cpuid);
2230 model->cpu_id_format = cpuid_format(prop.cpuid);
2231 model->cpu_ver = 0xff;
2233 /* get supported cpu features indicated via STFL(E) */
2234 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2235 (uint8_t *) prop.fac_mask);
2236 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2237 if (test_bit(S390_FEAT_STFLE, model->features)) {
2238 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2240 /* get supported cpu features indicated e.g. via SCLP */
2241 rc = query_cpu_feat(model->features);
2242 if (rc) {
2243 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2244 return;
2246 /* get supported cpu subfunctions indicated via query / test bit */
2247 rc = query_cpu_subfunc(model->features);
2248 if (rc) {
2249 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2250 return;
2253 /* PTFF subfunctions might be indicated although kernel support missing */
2254 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) {
2255 clear_bit(S390_FEAT_PTFF_QSIE, model->features);
2256 clear_bit(S390_FEAT_PTFF_QTOUE, model->features);
2257 clear_bit(S390_FEAT_PTFF_STOE, model->features);
2258 clear_bit(S390_FEAT_PTFF_STOUE, model->features);
2261 /* with cpu model support, CMM is only indicated if really available */
2262 if (kvm_s390_cmma_available()) {
2263 set_bit(S390_FEAT_CMM, model->features);
2264 } else {
2265 /* no cmm -> no cmm nt */
2266 clear_bit(S390_FEAT_CMM_NT, model->features);
2269 /* bpb needs kernel support for migration, VSIE and reset */
2270 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) {
2271 clear_bit(S390_FEAT_BPB, model->features);
2274 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2275 if (pci_available) {
2276 set_bit(S390_FEAT_ZPCI, model->features);
2278 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2280 if (s390_known_cpu_type(cpu_type)) {
2281 /* we want the exact model, even if some features are missing */
2282 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2283 ibc_ec_ga(unblocked_ibc), NULL);
2284 } else {
2285 /* model unknown, e.g. too new - search using features */
2286 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2287 ibc_ec_ga(unblocked_ibc),
2288 model->features);
2290 if (!model->def) {
2291 error_setg(errp, "KVM: host CPU model could not be identified");
2292 return;
2294 /* strip of features that are not part of the maximum model */
2295 bitmap_and(model->features, model->features, model->def->full_feat,
2296 S390_FEAT_MAX);
2299 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2301 struct kvm_s390_vm_cpu_processor prop = {
2302 .fac_list = { 0 },
2304 struct kvm_device_attr attr = {
2305 .group = KVM_S390_VM_CPU_MODEL,
2306 .attr = KVM_S390_VM_CPU_PROCESSOR,
2307 .addr = (uint64_t) &prop,
2309 int rc;
2311 if (!model) {
2312 /* compatibility handling if cpu models are disabled */
2313 if (kvm_s390_cmma_available()) {
2314 kvm_s390_enable_cmma();
2316 return;
2318 if (!kvm_s390_cpu_models_supported()) {
2319 error_setg(errp, "KVM doesn't support CPU models");
2320 return;
2322 prop.cpuid = s390_cpuid_from_cpu_model(model);
2323 prop.ibc = s390_ibc_from_cpu_model(model);
2324 /* configure cpu features indicated via STFL(e) */
2325 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2326 (uint8_t *) prop.fac_list);
2327 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2328 if (rc) {
2329 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2330 return;
2332 /* configure cpu features indicated e.g. via SCLP */
2333 rc = configure_cpu_feat(model->features);
2334 if (rc) {
2335 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2336 return;
2338 /* configure cpu subfunctions indicated via query / test bit */
2339 rc = configure_cpu_subfunc(model->features);
2340 if (rc) {
2341 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2342 return;
2344 /* enable CMM via CMMA */
2345 if (test_bit(S390_FEAT_CMM, model->features)) {
2346 kvm_s390_enable_cmma();
2350 void kvm_s390_restart_interrupt(S390CPU *cpu)
2352 struct kvm_s390_irq irq = {
2353 .type = KVM_S390_RESTART,
2356 kvm_s390_vcpu_interrupt(cpu, &irq);
2359 void kvm_s390_stop_interrupt(S390CPU *cpu)
2361 struct kvm_s390_irq irq = {
2362 .type = KVM_S390_SIGP_STOP,
2365 kvm_s390_vcpu_interrupt(cpu, &irq);