s390x: Use uint64_t for vector registers
[qemu/ar7.git] / target / s390x / kvm.c
blobbcec9795ec802887bf53b0e4e4d4b97d64a8dcff
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 (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 S390CPU *cpu = S390_CPU(cs);
366 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
367 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
368 return 0;
371 void kvm_s390_reset_vcpu(S390CPU *cpu)
373 CPUState *cs = CPU(cpu);
375 /* The initial reset call is needed here to reset in-kernel
376 * vcpu data that we can't access directly from QEMU
377 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
378 * Before this ioctl cpu_synchronize_state() is called in common kvm
379 * code (kvm-all) */
380 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
381 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
385 static int can_sync_regs(CPUState *cs, int regs)
387 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
390 int kvm_arch_put_registers(CPUState *cs, int level)
392 S390CPU *cpu = S390_CPU(cs);
393 CPUS390XState *env = &cpu->env;
394 struct kvm_sregs sregs;
395 struct kvm_regs regs;
396 struct kvm_fpu fpu = {};
397 int r;
398 int i;
400 /* always save the PSW and the GPRS*/
401 cs->kvm_run->psw_addr = env->psw.addr;
402 cs->kvm_run->psw_mask = env->psw.mask;
404 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
405 for (i = 0; i < 16; i++) {
406 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
407 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
409 } else {
410 for (i = 0; i < 16; i++) {
411 regs.gprs[i] = env->regs[i];
413 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
414 if (r < 0) {
415 return r;
419 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
420 for (i = 0; i < 32; i++) {
421 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0];
422 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1];
424 cs->kvm_run->s.regs.fpc = env->fpc;
425 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
426 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
427 for (i = 0; i < 16; i++) {
428 cs->kvm_run->s.regs.fprs[i] = *get_freg(env, i);
430 cs->kvm_run->s.regs.fpc = env->fpc;
431 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
432 } else {
433 /* Floating point */
434 for (i = 0; i < 16; i++) {
435 fpu.fprs[i] = *get_freg(env, i);
437 fpu.fpc = env->fpc;
439 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
440 if (r < 0) {
441 return r;
445 /* Do we need to save more than that? */
446 if (level == KVM_PUT_RUNTIME_STATE) {
447 return 0;
450 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
451 cs->kvm_run->s.regs.cputm = env->cputm;
452 cs->kvm_run->s.regs.ckc = env->ckc;
453 cs->kvm_run->s.regs.todpr = env->todpr;
454 cs->kvm_run->s.regs.gbea = env->gbea;
455 cs->kvm_run->s.regs.pp = env->pp;
456 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
457 } else {
459 * These ONE_REGS are not protected by a capability. As they are only
460 * necessary for migration we just trace a possible error, but don't
461 * return with an error return code.
463 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
464 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
465 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
466 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
467 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
470 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
471 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
472 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
475 /* pfault parameters */
476 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
477 cs->kvm_run->s.regs.pft = env->pfault_token;
478 cs->kvm_run->s.regs.pfs = env->pfault_select;
479 cs->kvm_run->s.regs.pfc = env->pfault_compare;
480 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
481 } else if (cap_async_pf) {
482 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
483 if (r < 0) {
484 return r;
486 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
487 if (r < 0) {
488 return r;
490 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
491 if (r < 0) {
492 return r;
496 /* access registers and control registers*/
497 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
498 for (i = 0; i < 16; i++) {
499 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
500 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
502 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
503 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
504 } else {
505 for (i = 0; i < 16; i++) {
506 sregs.acrs[i] = env->aregs[i];
507 sregs.crs[i] = env->cregs[i];
509 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
510 if (r < 0) {
511 return r;
515 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
516 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
517 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
520 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
521 cs->kvm_run->s.regs.bpbc = env->bpbc;
522 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_BPBC;
525 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
526 cs->kvm_run->s.regs.etoken = env->etoken;
527 cs->kvm_run->s.regs.etoken_extension = env->etoken_extension;
528 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ETOKEN;
531 /* Finally the prefix */
532 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
533 cs->kvm_run->s.regs.prefix = env->psa;
534 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
535 } else {
536 /* prefix is only supported via sync regs */
538 return 0;
541 int kvm_arch_get_registers(CPUState *cs)
543 S390CPU *cpu = S390_CPU(cs);
544 CPUS390XState *env = &cpu->env;
545 struct kvm_sregs sregs;
546 struct kvm_regs regs;
547 struct kvm_fpu fpu;
548 int i, r;
550 /* get the PSW */
551 env->psw.addr = cs->kvm_run->psw_addr;
552 env->psw.mask = cs->kvm_run->psw_mask;
554 /* the GPRS */
555 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
556 for (i = 0; i < 16; i++) {
557 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
559 } else {
560 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
561 if (r < 0) {
562 return r;
564 for (i = 0; i < 16; i++) {
565 env->regs[i] = regs.gprs[i];
569 /* The ACRS and CRS */
570 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
571 for (i = 0; i < 16; i++) {
572 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
573 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
575 } else {
576 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
577 if (r < 0) {
578 return r;
580 for (i = 0; i < 16; i++) {
581 env->aregs[i] = sregs.acrs[i];
582 env->cregs[i] = sregs.crs[i];
586 /* Floating point and vector registers */
587 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
588 for (i = 0; i < 32; i++) {
589 env->vregs[i][0] = cs->kvm_run->s.regs.vrs[i][0];
590 env->vregs[i][1] = cs->kvm_run->s.regs.vrs[i][1];
592 env->fpc = cs->kvm_run->s.regs.fpc;
593 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
594 for (i = 0; i < 16; i++) {
595 *get_freg(env, i) = cs->kvm_run->s.regs.fprs[i];
597 env->fpc = cs->kvm_run->s.regs.fpc;
598 } else {
599 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
600 if (r < 0) {
601 return r;
603 for (i = 0; i < 16; i++) {
604 *get_freg(env, i) = fpu.fprs[i];
606 env->fpc = fpu.fpc;
609 /* The prefix */
610 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
611 env->psa = cs->kvm_run->s.regs.prefix;
614 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
615 env->cputm = cs->kvm_run->s.regs.cputm;
616 env->ckc = cs->kvm_run->s.regs.ckc;
617 env->todpr = cs->kvm_run->s.regs.todpr;
618 env->gbea = cs->kvm_run->s.regs.gbea;
619 env->pp = cs->kvm_run->s.regs.pp;
620 } else {
622 * These ONE_REGS are not protected by a capability. As they are only
623 * necessary for migration we just trace a possible error, but don't
624 * return with an error return code.
626 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
627 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
628 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
629 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
630 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
633 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
634 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
637 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
638 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
641 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
642 env->bpbc = cs->kvm_run->s.regs.bpbc;
645 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
646 env->etoken = cs->kvm_run->s.regs.etoken;
647 env->etoken_extension = cs->kvm_run->s.regs.etoken_extension;
650 /* pfault parameters */
651 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
652 env->pfault_token = cs->kvm_run->s.regs.pft;
653 env->pfault_select = cs->kvm_run->s.regs.pfs;
654 env->pfault_compare = cs->kvm_run->s.regs.pfc;
655 } else if (cap_async_pf) {
656 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
657 if (r < 0) {
658 return r;
660 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
661 if (r < 0) {
662 return r;
664 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
665 if (r < 0) {
666 return r;
670 return 0;
673 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
675 int r;
676 struct kvm_device_attr attr = {
677 .group = KVM_S390_VM_TOD,
678 .attr = KVM_S390_VM_TOD_LOW,
679 .addr = (uint64_t)tod_low,
682 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
683 if (r) {
684 return r;
687 attr.attr = KVM_S390_VM_TOD_HIGH;
688 attr.addr = (uint64_t)tod_high;
689 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
692 int kvm_s390_get_clock_ext(uint8_t *tod_high, uint64_t *tod_low)
694 int r;
695 struct kvm_s390_vm_tod_clock gtod;
696 struct kvm_device_attr attr = {
697 .group = KVM_S390_VM_TOD,
698 .attr = KVM_S390_VM_TOD_EXT,
699 .addr = (uint64_t)&gtod,
702 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
703 *tod_high = gtod.epoch_idx;
704 *tod_low = gtod.tod;
706 return r;
709 int kvm_s390_set_clock(uint8_t tod_high, uint64_t tod_low)
711 int r;
712 struct kvm_device_attr attr = {
713 .group = KVM_S390_VM_TOD,
714 .attr = KVM_S390_VM_TOD_LOW,
715 .addr = (uint64_t)&tod_low,
718 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
719 if (r) {
720 return r;
723 attr.attr = KVM_S390_VM_TOD_HIGH;
724 attr.addr = (uint64_t)&tod_high;
725 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
728 int kvm_s390_set_clock_ext(uint8_t tod_high, uint64_t tod_low)
730 struct kvm_s390_vm_tod_clock gtod = {
731 .epoch_idx = tod_high,
732 .tod = tod_low,
734 struct kvm_device_attr attr = {
735 .group = KVM_S390_VM_TOD,
736 .attr = KVM_S390_VM_TOD_EXT,
737 .addr = (uint64_t)&gtod,
740 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
744 * kvm_s390_mem_op:
745 * @addr: the logical start address in guest memory
746 * @ar: the access register number
747 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
748 * @len: length that should be transferred
749 * @is_write: true = write, false = read
750 * Returns: 0 on success, non-zero if an exception or error occurred
752 * Use KVM ioctl to read/write from/to guest memory. An access exception
753 * is injected into the vCPU in case of translation errors.
755 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
756 int len, bool is_write)
758 struct kvm_s390_mem_op mem_op = {
759 .gaddr = addr,
760 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
761 .size = len,
762 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
763 : KVM_S390_MEMOP_LOGICAL_READ,
764 .buf = (uint64_t)hostbuf,
765 .ar = ar,
767 int ret;
769 if (!cap_mem_op) {
770 return -ENOSYS;
772 if (!hostbuf) {
773 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
776 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
777 if (ret < 0) {
778 warn_report("KVM_S390_MEM_OP failed: %s", strerror(-ret));
780 return ret;
784 * Legacy layout for s390:
785 * Older S390 KVM requires the topmost vma of the RAM to be
786 * smaller than an system defined value, which is at least 256GB.
787 * Larger systems have larger values. We put the guest between
788 * the end of data segment (system break) and this value. We
789 * use 32GB as a base to have enough room for the system break
790 * to grow. We also have to use MAP parameters that avoid
791 * read-only mapping of guest pages.
793 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared)
795 static void *mem;
797 if (mem) {
798 /* we only support one allocation, which is enough for initial ram */
799 return NULL;
802 mem = mmap((void *) 0x800000000ULL, size,
803 PROT_EXEC|PROT_READ|PROT_WRITE,
804 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
805 if (mem == MAP_FAILED) {
806 mem = NULL;
808 if (mem && align) {
809 *align = QEMU_VMALLOC_ALIGN;
811 return mem;
814 static uint8_t const *sw_bp_inst;
815 static uint8_t sw_bp_ilen;
817 static void determine_sw_breakpoint_instr(void)
819 /* DIAG 501 is used for sw breakpoints with old kernels */
820 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
821 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
822 static const uint8_t instr_0x0000[] = {0x00, 0x00};
824 if (sw_bp_inst) {
825 return;
827 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
828 sw_bp_inst = diag_501;
829 sw_bp_ilen = sizeof(diag_501);
830 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
831 } else {
832 sw_bp_inst = instr_0x0000;
833 sw_bp_ilen = sizeof(instr_0x0000);
834 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
838 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
840 determine_sw_breakpoint_instr();
842 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
843 sw_bp_ilen, 0) ||
844 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
845 return -EINVAL;
847 return 0;
850 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
852 uint8_t t[MAX_ILEN];
854 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
855 return -EINVAL;
856 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
857 return -EINVAL;
858 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
859 sw_bp_ilen, 1)) {
860 return -EINVAL;
863 return 0;
866 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
867 int len, int type)
869 int n;
871 for (n = 0; n < nb_hw_breakpoints; n++) {
872 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
873 (hw_breakpoints[n].len == len || len == -1)) {
874 return &hw_breakpoints[n];
878 return NULL;
881 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
883 int size;
885 if (find_hw_breakpoint(addr, len, type)) {
886 return -EEXIST;
889 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
891 if (!hw_breakpoints) {
892 nb_hw_breakpoints = 0;
893 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
894 } else {
895 hw_breakpoints =
896 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
899 if (!hw_breakpoints) {
900 nb_hw_breakpoints = 0;
901 return -ENOMEM;
904 hw_breakpoints[nb_hw_breakpoints].addr = addr;
905 hw_breakpoints[nb_hw_breakpoints].len = len;
906 hw_breakpoints[nb_hw_breakpoints].type = type;
908 nb_hw_breakpoints++;
910 return 0;
913 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
914 target_ulong len, int type)
916 switch (type) {
917 case GDB_BREAKPOINT_HW:
918 type = KVM_HW_BP;
919 break;
920 case GDB_WATCHPOINT_WRITE:
921 if (len < 1) {
922 return -EINVAL;
924 type = KVM_HW_WP_WRITE;
925 break;
926 default:
927 return -ENOSYS;
929 return insert_hw_breakpoint(addr, len, type);
932 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
933 target_ulong len, int type)
935 int size;
936 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
938 if (bp == NULL) {
939 return -ENOENT;
942 nb_hw_breakpoints--;
943 if (nb_hw_breakpoints > 0) {
945 * In order to trim the array, move the last element to the position to
946 * be removed - if necessary.
948 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
949 *bp = hw_breakpoints[nb_hw_breakpoints];
951 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
952 hw_breakpoints =
953 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
954 } else {
955 g_free(hw_breakpoints);
956 hw_breakpoints = NULL;
959 return 0;
962 void kvm_arch_remove_all_hw_breakpoints(void)
964 nb_hw_breakpoints = 0;
965 g_free(hw_breakpoints);
966 hw_breakpoints = NULL;
969 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
971 int i;
973 if (nb_hw_breakpoints > 0) {
974 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
975 dbg->arch.hw_bp = hw_breakpoints;
977 for (i = 0; i < nb_hw_breakpoints; ++i) {
978 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
979 hw_breakpoints[i].addr);
981 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
982 } else {
983 dbg->arch.nr_hw_bp = 0;
984 dbg->arch.hw_bp = NULL;
988 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
992 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
994 return MEMTXATTRS_UNSPECIFIED;
997 int kvm_arch_process_async_events(CPUState *cs)
999 return cs->halted;
1002 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
1003 struct kvm_s390_interrupt *interrupt)
1005 int r = 0;
1007 interrupt->type = irq->type;
1008 switch (irq->type) {
1009 case KVM_S390_INT_VIRTIO:
1010 interrupt->parm = irq->u.ext.ext_params;
1011 /* fall through */
1012 case KVM_S390_INT_PFAULT_INIT:
1013 case KVM_S390_INT_PFAULT_DONE:
1014 interrupt->parm64 = irq->u.ext.ext_params2;
1015 break;
1016 case KVM_S390_PROGRAM_INT:
1017 interrupt->parm = irq->u.pgm.code;
1018 break;
1019 case KVM_S390_SIGP_SET_PREFIX:
1020 interrupt->parm = irq->u.prefix.address;
1021 break;
1022 case KVM_S390_INT_SERVICE:
1023 interrupt->parm = irq->u.ext.ext_params;
1024 break;
1025 case KVM_S390_MCHK:
1026 interrupt->parm = irq->u.mchk.cr14;
1027 interrupt->parm64 = irq->u.mchk.mcic;
1028 break;
1029 case KVM_S390_INT_EXTERNAL_CALL:
1030 interrupt->parm = irq->u.extcall.code;
1031 break;
1032 case KVM_S390_INT_EMERGENCY:
1033 interrupt->parm = irq->u.emerg.code;
1034 break;
1035 case KVM_S390_SIGP_STOP:
1036 case KVM_S390_RESTART:
1037 break; /* These types have no parameters */
1038 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1039 interrupt->parm = irq->u.io.subchannel_id << 16;
1040 interrupt->parm |= irq->u.io.subchannel_nr;
1041 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
1042 interrupt->parm64 |= irq->u.io.io_int_word;
1043 break;
1044 default:
1045 r = -EINVAL;
1046 break;
1048 return r;
1051 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
1053 struct kvm_s390_interrupt kvmint = {};
1054 int r;
1056 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1057 if (r < 0) {
1058 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1059 exit(1);
1062 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
1063 if (r < 0) {
1064 fprintf(stderr, "KVM failed to inject interrupt\n");
1065 exit(1);
1069 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
1071 CPUState *cs = CPU(cpu);
1072 int r;
1074 if (cap_s390_irq) {
1075 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
1076 if (!r) {
1077 return;
1079 error_report("KVM failed to inject interrupt %llx", irq->type);
1080 exit(1);
1083 inject_vcpu_irq_legacy(cs, irq);
1086 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq *irq)
1088 struct kvm_s390_interrupt kvmint = {};
1089 int r;
1091 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1092 if (r < 0) {
1093 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1094 exit(1);
1097 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1098 if (r < 0) {
1099 fprintf(stderr, "KVM failed to inject interrupt\n");
1100 exit(1);
1104 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1106 struct kvm_s390_irq irq = {
1107 .type = KVM_S390_PROGRAM_INT,
1108 .u.pgm.code = code,
1110 qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
1111 cpu->env.psw.addr);
1112 kvm_s390_vcpu_interrupt(cpu, &irq);
1115 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1117 struct kvm_s390_irq irq = {
1118 .type = KVM_S390_PROGRAM_INT,
1119 .u.pgm.code = code,
1120 .u.pgm.trans_exc_code = te_code,
1121 .u.pgm.exc_access_id = te_code & 3,
1124 kvm_s390_vcpu_interrupt(cpu, &irq);
1127 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1128 uint16_t ipbh0)
1130 CPUS390XState *env = &cpu->env;
1131 uint64_t sccb;
1132 uint32_t code;
1133 int r = 0;
1135 sccb = env->regs[ipbh0 & 0xf];
1136 code = env->regs[(ipbh0 & 0xf0) >> 4];
1138 r = sclp_service_call(env, sccb, code);
1139 if (r < 0) {
1140 kvm_s390_program_interrupt(cpu, -r);
1141 } else {
1142 setcc(cpu, r);
1145 return 0;
1148 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1150 CPUS390XState *env = &cpu->env;
1151 int rc = 0;
1152 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1154 switch (ipa1) {
1155 case PRIV_B2_XSCH:
1156 ioinst_handle_xsch(cpu, env->regs[1], RA_IGNORED);
1157 break;
1158 case PRIV_B2_CSCH:
1159 ioinst_handle_csch(cpu, env->regs[1], RA_IGNORED);
1160 break;
1161 case PRIV_B2_HSCH:
1162 ioinst_handle_hsch(cpu, env->regs[1], RA_IGNORED);
1163 break;
1164 case PRIV_B2_MSCH:
1165 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1166 break;
1167 case PRIV_B2_SSCH:
1168 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1169 break;
1170 case PRIV_B2_STCRW:
1171 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb, RA_IGNORED);
1172 break;
1173 case PRIV_B2_STSCH:
1174 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1175 break;
1176 case PRIV_B2_TSCH:
1177 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1178 fprintf(stderr, "Spurious tsch intercept\n");
1179 break;
1180 case PRIV_B2_CHSC:
1181 ioinst_handle_chsc(cpu, run->s390_sieic.ipb, RA_IGNORED);
1182 break;
1183 case PRIV_B2_TPI:
1184 /* This should have been handled by kvm already. */
1185 fprintf(stderr, "Spurious tpi intercept\n");
1186 break;
1187 case PRIV_B2_SCHM:
1188 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1189 run->s390_sieic.ipb, RA_IGNORED);
1190 break;
1191 case PRIV_B2_RSCH:
1192 ioinst_handle_rsch(cpu, env->regs[1], RA_IGNORED);
1193 break;
1194 case PRIV_B2_RCHP:
1195 ioinst_handle_rchp(cpu, env->regs[1], RA_IGNORED);
1196 break;
1197 case PRIV_B2_STCPS:
1198 /* We do not provide this instruction, it is suppressed. */
1199 break;
1200 case PRIV_B2_SAL:
1201 ioinst_handle_sal(cpu, env->regs[1], RA_IGNORED);
1202 break;
1203 case PRIV_B2_SIGA:
1204 /* Not provided, set CC = 3 for subchannel not operational */
1205 setcc(cpu, 3);
1206 break;
1207 case PRIV_B2_SCLP_CALL:
1208 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1209 break;
1210 default:
1211 rc = -1;
1212 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1213 break;
1216 return rc;
1219 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1220 uint8_t *ar)
1222 CPUS390XState *env = &cpu->env;
1223 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1224 uint32_t base2 = run->s390_sieic.ipb >> 28;
1225 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1226 ((run->s390_sieic.ipb & 0xff00) << 4);
1228 if (disp2 & 0x80000) {
1229 disp2 += 0xfff00000;
1231 if (ar) {
1232 *ar = base2;
1235 return (base2 ? env->regs[base2] : 0) +
1236 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1239 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1240 uint8_t *ar)
1242 CPUS390XState *env = &cpu->env;
1243 uint32_t base2 = run->s390_sieic.ipb >> 28;
1244 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1245 ((run->s390_sieic.ipb & 0xff00) << 4);
1247 if (disp2 & 0x80000) {
1248 disp2 += 0xfff00000;
1250 if (ar) {
1251 *ar = base2;
1254 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1257 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1259 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1261 if (s390_has_feat(S390_FEAT_ZPCI)) {
1262 return clp_service_call(cpu, r2, RA_IGNORED);
1263 } else {
1264 return -1;
1268 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1270 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1271 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1273 if (s390_has_feat(S390_FEAT_ZPCI)) {
1274 return pcilg_service_call(cpu, r1, r2, RA_IGNORED);
1275 } else {
1276 return -1;
1280 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1282 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1283 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1285 if (s390_has_feat(S390_FEAT_ZPCI)) {
1286 return pcistg_service_call(cpu, r1, r2, RA_IGNORED);
1287 } else {
1288 return -1;
1292 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1294 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1295 uint64_t fiba;
1296 uint8_t ar;
1298 if (s390_has_feat(S390_FEAT_ZPCI)) {
1299 fiba = get_base_disp_rxy(cpu, run, &ar);
1301 return stpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1302 } else {
1303 return -1;
1307 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1309 CPUS390XState *env = &cpu->env;
1310 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1311 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1312 uint8_t isc;
1313 uint16_t mode;
1314 int r;
1316 mode = env->regs[r1] & 0xffff;
1317 isc = (env->regs[r3] >> 27) & 0x7;
1318 r = css_do_sic(env, isc, mode);
1319 if (r) {
1320 kvm_s390_program_interrupt(cpu, -r);
1323 return 0;
1326 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1328 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1329 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1331 if (s390_has_feat(S390_FEAT_ZPCI)) {
1332 return rpcit_service_call(cpu, r1, r2, RA_IGNORED);
1333 } else {
1334 return -1;
1338 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1340 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1341 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1342 uint64_t gaddr;
1343 uint8_t ar;
1345 if (s390_has_feat(S390_FEAT_ZPCI)) {
1346 gaddr = get_base_disp_rsy(cpu, run, &ar);
1348 return pcistb_service_call(cpu, r1, r3, gaddr, ar, RA_IGNORED);
1349 } else {
1350 return -1;
1354 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1356 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1357 uint64_t fiba;
1358 uint8_t ar;
1360 if (s390_has_feat(S390_FEAT_ZPCI)) {
1361 fiba = get_base_disp_rxy(cpu, run, &ar);
1363 return mpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1364 } else {
1365 return -1;
1369 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1371 int r = 0;
1373 switch (ipa1) {
1374 case PRIV_B9_CLP:
1375 r = kvm_clp_service_call(cpu, run);
1376 break;
1377 case PRIV_B9_PCISTG:
1378 r = kvm_pcistg_service_call(cpu, run);
1379 break;
1380 case PRIV_B9_PCILG:
1381 r = kvm_pcilg_service_call(cpu, run);
1382 break;
1383 case PRIV_B9_RPCIT:
1384 r = kvm_rpcit_service_call(cpu, run);
1385 break;
1386 case PRIV_B9_EQBS:
1387 /* just inject exception */
1388 r = -1;
1389 break;
1390 default:
1391 r = -1;
1392 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1393 break;
1396 return r;
1399 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1401 int r = 0;
1403 switch (ipbl) {
1404 case PRIV_EB_PCISTB:
1405 r = kvm_pcistb_service_call(cpu, run);
1406 break;
1407 case PRIV_EB_SIC:
1408 r = kvm_sic_service_call(cpu, run);
1409 break;
1410 case PRIV_EB_SQBS:
1411 /* just inject exception */
1412 r = -1;
1413 break;
1414 default:
1415 r = -1;
1416 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1417 break;
1420 return r;
1423 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1425 int r = 0;
1427 switch (ipbl) {
1428 case PRIV_E3_MPCIFC:
1429 r = kvm_mpcifc_service_call(cpu, run);
1430 break;
1431 case PRIV_E3_STPCIFC:
1432 r = kvm_stpcifc_service_call(cpu, run);
1433 break;
1434 default:
1435 r = -1;
1436 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1437 break;
1440 return r;
1443 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1445 CPUS390XState *env = &cpu->env;
1446 int ret;
1448 ret = s390_virtio_hypercall(env);
1449 if (ret == -EINVAL) {
1450 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1451 return 0;
1454 return ret;
1457 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1459 uint64_t r1, r3;
1460 int rc;
1462 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1463 r3 = run->s390_sieic.ipa & 0x000f;
1464 rc = handle_diag_288(&cpu->env, r1, r3);
1465 if (rc) {
1466 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1470 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1472 uint64_t r1, r3;
1474 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1475 r3 = run->s390_sieic.ipa & 0x000f;
1476 handle_diag_308(&cpu->env, r1, r3, RA_IGNORED);
1479 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1481 CPUS390XState *env = &cpu->env;
1482 unsigned long pc;
1484 pc = env->psw.addr - sw_bp_ilen;
1485 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1486 env->psw.addr = pc;
1487 return EXCP_DEBUG;
1490 return -ENOENT;
1493 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1495 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1497 int r = 0;
1498 uint16_t func_code;
1501 * For any diagnose call we support, bits 48-63 of the resulting
1502 * address specify the function code; the remainder is ignored.
1504 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1505 switch (func_code) {
1506 case DIAG_TIMEREVENT:
1507 kvm_handle_diag_288(cpu, run);
1508 break;
1509 case DIAG_IPL:
1510 kvm_handle_diag_308(cpu, run);
1511 break;
1512 case DIAG_KVM_HYPERCALL:
1513 r = handle_hypercall(cpu, run);
1514 break;
1515 case DIAG_KVM_BREAKPOINT:
1516 r = handle_sw_breakpoint(cpu, run);
1517 break;
1518 default:
1519 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1520 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1521 break;
1524 return r;
1527 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb)
1529 CPUS390XState *env = &cpu->env;
1530 const uint8_t r1 = ipa1 >> 4;
1531 const uint8_t r3 = ipa1 & 0x0f;
1532 int ret;
1533 uint8_t order;
1535 /* get order code */
1536 order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK;
1538 ret = handle_sigp(env, order, r1, r3);
1539 setcc(cpu, ret);
1540 return 0;
1543 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1545 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1546 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1547 int r = -1;
1549 DPRINTF("handle_instruction 0x%x 0x%x\n",
1550 run->s390_sieic.ipa, run->s390_sieic.ipb);
1551 switch (ipa0) {
1552 case IPA0_B2:
1553 r = handle_b2(cpu, run, ipa1);
1554 break;
1555 case IPA0_B9:
1556 r = handle_b9(cpu, run, ipa1);
1557 break;
1558 case IPA0_EB:
1559 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1560 break;
1561 case IPA0_E3:
1562 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1563 break;
1564 case IPA0_DIAG:
1565 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1566 break;
1567 case IPA0_SIGP:
1568 r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb);
1569 break;
1572 if (r < 0) {
1573 r = 0;
1574 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1577 return r;
1580 static void unmanageable_intercept(S390CPU *cpu, S390CrashReason reason,
1581 int pswoffset)
1583 CPUState *cs = CPU(cpu);
1585 s390_cpu_halt(cpu);
1586 cpu->env.crash_reason = reason;
1587 qemu_system_guest_panicked(cpu_get_crash_info(cs));
1590 /* try to detect pgm check loops */
1591 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1593 CPUState *cs = CPU(cpu);
1594 PSW oldpsw, newpsw;
1596 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1597 offsetof(LowCore, program_new_psw));
1598 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1599 offsetof(LowCore, program_new_psw) + 8);
1600 oldpsw.mask = run->psw_mask;
1601 oldpsw.addr = run->psw_addr;
1603 * Avoid endless loops of operation exceptions, if the pgm new
1604 * PSW will cause a new operation exception.
1605 * The heuristic checks if the pgm new psw is within 6 bytes before
1606 * the faulting psw address (with same DAT, AS settings) and the
1607 * new psw is not a wait psw and the fault was not triggered by
1608 * problem state. In that case go into crashed state.
1611 if (oldpsw.addr - newpsw.addr <= 6 &&
1612 !(newpsw.mask & PSW_MASK_WAIT) &&
1613 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1614 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1615 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1616 unmanageable_intercept(cpu, S390_CRASH_REASON_OPINT_LOOP,
1617 offsetof(LowCore, program_new_psw));
1618 return EXCP_HALTED;
1620 return 0;
1623 static int handle_intercept(S390CPU *cpu)
1625 CPUState *cs = CPU(cpu);
1626 struct kvm_run *run = cs->kvm_run;
1627 int icpt_code = run->s390_sieic.icptcode;
1628 int r = 0;
1630 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1631 (long)cs->kvm_run->psw_addr);
1632 switch (icpt_code) {
1633 case ICPT_INSTRUCTION:
1634 r = handle_instruction(cpu, run);
1635 break;
1636 case ICPT_PROGRAM:
1637 unmanageable_intercept(cpu, S390_CRASH_REASON_PGMINT_LOOP,
1638 offsetof(LowCore, program_new_psw));
1639 r = EXCP_HALTED;
1640 break;
1641 case ICPT_EXT_INT:
1642 unmanageable_intercept(cpu, S390_CRASH_REASON_EXTINT_LOOP,
1643 offsetof(LowCore, external_new_psw));
1644 r = EXCP_HALTED;
1645 break;
1646 case ICPT_WAITPSW:
1647 /* disabled wait, since enabled wait is handled in kernel */
1648 s390_handle_wait(cpu);
1649 r = EXCP_HALTED;
1650 break;
1651 case ICPT_CPU_STOP:
1652 do_stop_interrupt(&cpu->env);
1653 r = EXCP_HALTED;
1654 break;
1655 case ICPT_OPEREXC:
1656 /* check for break points */
1657 r = handle_sw_breakpoint(cpu, run);
1658 if (r == -ENOENT) {
1659 /* Then check for potential pgm check loops */
1660 r = handle_oper_loop(cpu, run);
1661 if (r == 0) {
1662 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1665 break;
1666 case ICPT_SOFT_INTERCEPT:
1667 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1668 exit(1);
1669 break;
1670 case ICPT_IO:
1671 fprintf(stderr, "KVM unimplemented icpt IO\n");
1672 exit(1);
1673 break;
1674 default:
1675 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1676 exit(1);
1677 break;
1680 return r;
1683 static int handle_tsch(S390CPU *cpu)
1685 CPUState *cs = CPU(cpu);
1686 struct kvm_run *run = cs->kvm_run;
1687 int ret;
1689 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb,
1690 RA_IGNORED);
1691 if (ret < 0) {
1693 * Failure.
1694 * If an I/O interrupt had been dequeued, we have to reinject it.
1696 if (run->s390_tsch.dequeued) {
1697 s390_io_interrupt(run->s390_tsch.subchannel_id,
1698 run->s390_tsch.subchannel_nr,
1699 run->s390_tsch.io_int_parm,
1700 run->s390_tsch.io_int_word);
1702 ret = 0;
1704 return ret;
1707 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1709 SysIB_322 sysib;
1710 int del;
1712 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1713 return;
1715 /* Shift the stack of Extended Names to prepare for our own data */
1716 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1717 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1718 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1719 * assumed it's not capable of managing Extended Names for lower levels.
1721 for (del = 1; del < sysib.count; del++) {
1722 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1723 break;
1726 if (del < sysib.count) {
1727 memset(sysib.ext_names[del], 0,
1728 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1730 /* Insert short machine name in EBCDIC, padded with blanks */
1731 if (qemu_name) {
1732 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1733 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1734 strlen(qemu_name)));
1736 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1737 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1738 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1739 * considered by s390 as not capable of providing any Extended Name.
1740 * Therefore if no name was specified on qemu invocation, we go with the
1741 * same "KVMguest" default, which KVM has filled into short name field.
1743 if (qemu_name) {
1744 strncpy((char *)sysib.ext_names[0], qemu_name,
1745 sizeof(sysib.ext_names[0]));
1746 } else {
1747 strcpy((char *)sysib.ext_names[0], "KVMguest");
1749 /* Insert UUID */
1750 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
1752 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1755 static int handle_stsi(S390CPU *cpu)
1757 CPUState *cs = CPU(cpu);
1758 struct kvm_run *run = cs->kvm_run;
1760 switch (run->s390_stsi.fc) {
1761 case 3:
1762 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1763 return 0;
1765 /* Only sysib 3.2.2 needs post-handling for now. */
1766 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1767 return 0;
1768 default:
1769 return 0;
1773 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1775 CPUState *cs = CPU(cpu);
1776 struct kvm_run *run = cs->kvm_run;
1778 int ret = 0;
1779 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1781 switch (arch_info->type) {
1782 case KVM_HW_WP_WRITE:
1783 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1784 cs->watchpoint_hit = &hw_watchpoint;
1785 hw_watchpoint.vaddr = arch_info->addr;
1786 hw_watchpoint.flags = BP_MEM_WRITE;
1787 ret = EXCP_DEBUG;
1789 break;
1790 case KVM_HW_BP:
1791 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1792 ret = EXCP_DEBUG;
1794 break;
1795 case KVM_SINGLESTEP:
1796 if (cs->singlestep_enabled) {
1797 ret = EXCP_DEBUG;
1799 break;
1800 default:
1801 ret = -ENOSYS;
1804 return ret;
1807 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1809 S390CPU *cpu = S390_CPU(cs);
1810 int ret = 0;
1812 qemu_mutex_lock_iothread();
1814 kvm_cpu_synchronize_state(cs);
1816 switch (run->exit_reason) {
1817 case KVM_EXIT_S390_SIEIC:
1818 ret = handle_intercept(cpu);
1819 break;
1820 case KVM_EXIT_S390_RESET:
1821 s390_ipl_reset_request(cs, S390_RESET_REIPL);
1822 break;
1823 case KVM_EXIT_S390_TSCH:
1824 ret = handle_tsch(cpu);
1825 break;
1826 case KVM_EXIT_S390_STSI:
1827 ret = handle_stsi(cpu);
1828 break;
1829 case KVM_EXIT_DEBUG:
1830 ret = kvm_arch_handle_debug_exit(cpu);
1831 break;
1832 default:
1833 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1834 break;
1836 qemu_mutex_unlock_iothread();
1838 if (ret == 0) {
1839 ret = EXCP_INTERRUPT;
1841 return ret;
1844 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1846 return true;
1849 void kvm_s390_enable_css_support(S390CPU *cpu)
1851 int r;
1853 /* Activate host kernel channel subsystem support. */
1854 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1855 assert(r == 0);
1858 void kvm_arch_init_irq_routing(KVMState *s)
1861 * Note that while irqchip capabilities generally imply that cpustates
1862 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1863 * have to override the common code kvm_halt_in_kernel_allowed setting.
1865 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1866 kvm_gsi_routing_allowed = true;
1867 kvm_halt_in_kernel_allowed = false;
1871 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1872 int vq, bool assign)
1874 struct kvm_ioeventfd kick = {
1875 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1876 KVM_IOEVENTFD_FLAG_DATAMATCH,
1877 .fd = event_notifier_get_fd(notifier),
1878 .datamatch = vq,
1879 .addr = sch,
1880 .len = 8,
1882 trace_kvm_assign_subch_ioeventfd(kick.fd, kick.addr, assign,
1883 kick.datamatch);
1884 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1885 return -ENOSYS;
1887 if (!assign) {
1888 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1890 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1893 int kvm_s390_get_ri(void)
1895 return cap_ri;
1898 int kvm_s390_get_gs(void)
1900 return cap_gs;
1903 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1905 struct kvm_mp_state mp_state = {};
1906 int ret;
1908 /* the kvm part might not have been initialized yet */
1909 if (CPU(cpu)->kvm_state == NULL) {
1910 return 0;
1913 switch (cpu_state) {
1914 case S390_CPU_STATE_STOPPED:
1915 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1916 break;
1917 case S390_CPU_STATE_CHECK_STOP:
1918 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1919 break;
1920 case S390_CPU_STATE_OPERATING:
1921 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1922 break;
1923 case S390_CPU_STATE_LOAD:
1924 mp_state.mp_state = KVM_MP_STATE_LOAD;
1925 break;
1926 default:
1927 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1928 cpu_state);
1929 exit(1);
1932 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1933 if (ret) {
1934 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1935 strerror(-ret));
1938 return ret;
1941 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
1943 struct kvm_s390_irq_state irq_state = {
1944 .buf = (uint64_t) cpu->irqstate,
1945 .len = VCPU_IRQ_BUF_SIZE,
1947 CPUState *cs = CPU(cpu);
1948 int32_t bytes;
1950 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1951 return;
1954 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
1955 if (bytes < 0) {
1956 cpu->irqstate_saved_size = 0;
1957 error_report("Migration of interrupt state failed");
1958 return;
1961 cpu->irqstate_saved_size = bytes;
1964 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
1966 CPUState *cs = CPU(cpu);
1967 struct kvm_s390_irq_state irq_state = {
1968 .buf = (uint64_t) cpu->irqstate,
1969 .len = cpu->irqstate_saved_size,
1971 int r;
1973 if (cpu->irqstate_saved_size == 0) {
1974 return 0;
1977 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1978 return -ENOSYS;
1981 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
1982 if (r) {
1983 error_report("Setting interrupt state failed %d", r);
1985 return r;
1988 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1989 uint64_t address, uint32_t data, PCIDevice *dev)
1991 S390PCIBusDevice *pbdev;
1992 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
1994 if (!dev) {
1995 DPRINTF("add_msi_route no pci device\n");
1996 return -ENODEV;
1999 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
2000 if (!pbdev) {
2001 DPRINTF("add_msi_route no zpci device\n");
2002 return -ENODEV;
2005 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2006 route->flags = 0;
2007 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2008 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2009 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2010 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
2011 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2012 return 0;
2015 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2016 int vector, PCIDevice *dev)
2018 return 0;
2021 int kvm_arch_release_virq_post(int virq)
2023 return 0;
2026 int kvm_arch_msi_data_to_gsi(uint32_t data)
2028 abort();
2031 static int query_cpu_subfunc(S390FeatBitmap features)
2033 struct kvm_s390_vm_cpu_subfunc prop;
2034 struct kvm_device_attr attr = {
2035 .group = KVM_S390_VM_CPU_MODEL,
2036 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2037 .addr = (uint64_t) &prop,
2039 int rc;
2041 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2042 if (rc) {
2043 return rc;
2047 * We're going to add all subfunctions now, if the corresponding feature
2048 * is available that unlocks the query functions.
2050 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2051 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2052 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2054 if (test_bit(S390_FEAT_MSA, features)) {
2055 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2056 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2057 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2058 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2059 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2061 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2062 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2064 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2065 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2066 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2067 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2068 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2070 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2071 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2073 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2074 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2076 if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2077 s390_add_from_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2079 if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2080 s390_add_from_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2082 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2083 s390_add_from_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2085 return 0;
2088 static int configure_cpu_subfunc(const S390FeatBitmap features)
2090 struct kvm_s390_vm_cpu_subfunc prop = {};
2091 struct kvm_device_attr attr = {
2092 .group = KVM_S390_VM_CPU_MODEL,
2093 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2094 .addr = (uint64_t) &prop,
2097 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2098 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2099 /* hardware support might be missing, IBC will handle most of this */
2100 return 0;
2103 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2104 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2105 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2107 if (test_bit(S390_FEAT_MSA, features)) {
2108 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2109 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2110 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2111 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2112 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2114 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2115 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2117 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2118 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2119 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2120 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2121 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2123 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2124 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2126 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2127 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2129 if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2130 s390_fill_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2132 if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2133 s390_fill_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2135 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2136 s390_fill_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2138 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2141 static int kvm_to_feat[][2] = {
2142 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2143 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2144 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2145 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2146 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2147 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2148 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2149 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2150 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2151 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2152 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2153 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2154 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2155 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2158 static int query_cpu_feat(S390FeatBitmap features)
2160 struct kvm_s390_vm_cpu_feat prop;
2161 struct kvm_device_attr attr = {
2162 .group = KVM_S390_VM_CPU_MODEL,
2163 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2164 .addr = (uint64_t) &prop,
2166 int rc;
2167 int i;
2169 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2170 if (rc) {
2171 return rc;
2174 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2175 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2176 set_bit(kvm_to_feat[i][1], features);
2179 return 0;
2182 static int configure_cpu_feat(const S390FeatBitmap features)
2184 struct kvm_s390_vm_cpu_feat prop = {};
2185 struct kvm_device_attr attr = {
2186 .group = KVM_S390_VM_CPU_MODEL,
2187 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2188 .addr = (uint64_t) &prop,
2190 int i;
2192 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2193 if (test_bit(kvm_to_feat[i][1], features)) {
2194 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2197 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2200 bool kvm_s390_cpu_models_supported(void)
2202 if (!cpu_model_allowed()) {
2203 /* compatibility machines interfere with the cpu model */
2204 return false;
2206 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2207 KVM_S390_VM_CPU_MACHINE) &&
2208 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2209 KVM_S390_VM_CPU_PROCESSOR) &&
2210 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2211 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2212 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2213 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2214 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2215 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2218 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2220 struct kvm_s390_vm_cpu_machine prop = {};
2221 struct kvm_device_attr attr = {
2222 .group = KVM_S390_VM_CPU_MODEL,
2223 .attr = KVM_S390_VM_CPU_MACHINE,
2224 .addr = (uint64_t) &prop,
2226 uint16_t unblocked_ibc = 0, cpu_type = 0;
2227 int rc;
2229 memset(model, 0, sizeof(*model));
2231 if (!kvm_s390_cpu_models_supported()) {
2232 error_setg(errp, "KVM doesn't support CPU models");
2233 return;
2236 /* query the basic cpu model properties */
2237 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2238 if (rc) {
2239 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2240 return;
2243 cpu_type = cpuid_type(prop.cpuid);
2244 if (has_ibc(prop.ibc)) {
2245 model->lowest_ibc = lowest_ibc(prop.ibc);
2246 unblocked_ibc = unblocked_ibc(prop.ibc);
2248 model->cpu_id = cpuid_id(prop.cpuid);
2249 model->cpu_id_format = cpuid_format(prop.cpuid);
2250 model->cpu_ver = 0xff;
2252 /* get supported cpu features indicated via STFL(E) */
2253 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2254 (uint8_t *) prop.fac_mask);
2255 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2256 if (test_bit(S390_FEAT_STFLE, model->features)) {
2257 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2259 /* get supported cpu features indicated e.g. via SCLP */
2260 rc = query_cpu_feat(model->features);
2261 if (rc) {
2262 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2263 return;
2265 /* get supported cpu subfunctions indicated via query / test bit */
2266 rc = query_cpu_subfunc(model->features);
2267 if (rc) {
2268 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2269 return;
2272 /* PTFF subfunctions might be indicated although kernel support missing */
2273 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) {
2274 clear_bit(S390_FEAT_PTFF_QSIE, model->features);
2275 clear_bit(S390_FEAT_PTFF_QTOUE, model->features);
2276 clear_bit(S390_FEAT_PTFF_STOE, model->features);
2277 clear_bit(S390_FEAT_PTFF_STOUE, model->features);
2280 /* with cpu model support, CMM is only indicated if really available */
2281 if (kvm_s390_cmma_available()) {
2282 set_bit(S390_FEAT_CMM, model->features);
2283 } else {
2284 /* no cmm -> no cmm nt */
2285 clear_bit(S390_FEAT_CMM_NT, model->features);
2288 /* bpb needs kernel support for migration, VSIE and reset */
2289 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) {
2290 clear_bit(S390_FEAT_BPB, model->features);
2293 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2294 set_bit(S390_FEAT_ZPCI, model->features);
2295 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2297 if (s390_known_cpu_type(cpu_type)) {
2298 /* we want the exact model, even if some features are missing */
2299 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2300 ibc_ec_ga(unblocked_ibc), NULL);
2301 } else {
2302 /* model unknown, e.g. too new - search using features */
2303 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2304 ibc_ec_ga(unblocked_ibc),
2305 model->features);
2307 if (!model->def) {
2308 error_setg(errp, "KVM: host CPU model could not be identified");
2309 return;
2311 /* for now, we can only provide the AP feature with HW support */
2312 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO,
2313 KVM_S390_VM_CRYPTO_ENABLE_APIE)) {
2314 set_bit(S390_FEAT_AP, model->features);
2316 /* strip of features that are not part of the maximum model */
2317 bitmap_and(model->features, model->features, model->def->full_feat,
2318 S390_FEAT_MAX);
2321 static void kvm_s390_configure_apie(bool interpret)
2323 uint64_t attr = interpret ? KVM_S390_VM_CRYPTO_ENABLE_APIE :
2324 KVM_S390_VM_CRYPTO_DISABLE_APIE;
2326 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
2327 kvm_s390_set_attr(attr);
2331 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2333 struct kvm_s390_vm_cpu_processor prop = {
2334 .fac_list = { 0 },
2336 struct kvm_device_attr attr = {
2337 .group = KVM_S390_VM_CPU_MODEL,
2338 .attr = KVM_S390_VM_CPU_PROCESSOR,
2339 .addr = (uint64_t) &prop,
2341 int rc;
2343 if (!model) {
2344 /* compatibility handling if cpu models are disabled */
2345 if (kvm_s390_cmma_available()) {
2346 kvm_s390_enable_cmma();
2348 return;
2350 if (!kvm_s390_cpu_models_supported()) {
2351 error_setg(errp, "KVM doesn't support CPU models");
2352 return;
2354 prop.cpuid = s390_cpuid_from_cpu_model(model);
2355 prop.ibc = s390_ibc_from_cpu_model(model);
2356 /* configure cpu features indicated via STFL(e) */
2357 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2358 (uint8_t *) prop.fac_list);
2359 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2360 if (rc) {
2361 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2362 return;
2364 /* configure cpu features indicated e.g. via SCLP */
2365 rc = configure_cpu_feat(model->features);
2366 if (rc) {
2367 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2368 return;
2370 /* configure cpu subfunctions indicated via query / test bit */
2371 rc = configure_cpu_subfunc(model->features);
2372 if (rc) {
2373 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2374 return;
2376 /* enable CMM via CMMA */
2377 if (test_bit(S390_FEAT_CMM, model->features)) {
2378 kvm_s390_enable_cmma();
2381 if (test_bit(S390_FEAT_AP, model->features)) {
2382 kvm_s390_configure_apie(true);
2386 void kvm_s390_restart_interrupt(S390CPU *cpu)
2388 struct kvm_s390_irq irq = {
2389 .type = KVM_S390_RESTART,
2392 kvm_s390_vcpu_interrupt(cpu, &irq);
2395 void kvm_s390_stop_interrupt(S390CPU *cpu)
2397 struct kvm_s390_irq irq = {
2398 .type = KVM_S390_SIGP_STOP,
2401 kvm_s390_vcpu_interrupt(cpu, &irq);