s390x/ais: for 2.10 stable: disable ais facility
[qemu.git] / target / s390x / kvm.c
blobebb75cafaaea6b8a19aeadb5590e94540d8f93a8
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 "qemu/error-report.h"
35 #include "qemu/timer.h"
36 #include "sysemu/sysemu.h"
37 #include "sysemu/hw_accel.h"
38 #include "hw/hw.h"
39 #include "sysemu/device_tree.h"
40 #include "qapi/qmp/qjson.h"
41 #include "exec/gdbstub.h"
42 #include "exec/address-spaces.h"
43 #include "trace.h"
44 #include "qapi-event.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 QemuMutex qemu_sigp_mutex;
140 static int cap_sync_regs;
141 static int cap_async_pf;
142 static int cap_mem_op;
143 static int cap_s390_irq;
144 static int cap_ri;
145 static int cap_gs;
147 static int active_cmma;
149 static void *legacy_s390_alloc(size_t size, uint64_t *align);
151 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
153 struct kvm_device_attr attr = {
154 .group = KVM_S390_VM_MEM_CTRL,
155 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
156 .addr = (uint64_t) memory_limit,
159 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
162 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit)
164 int rc;
166 struct kvm_device_attr attr = {
167 .group = KVM_S390_VM_MEM_CTRL,
168 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
169 .addr = (uint64_t) &new_limit,
172 if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) {
173 return 0;
176 rc = kvm_s390_query_mem_limit(hw_limit);
177 if (rc) {
178 return rc;
179 } else if (*hw_limit < new_limit) {
180 return -E2BIG;
183 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
186 int kvm_s390_cmma_active(void)
188 return active_cmma;
191 static bool kvm_s390_cmma_available(void)
193 static bool initialized, value;
195 if (!initialized) {
196 initialized = true;
197 value = kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_ENABLE_CMMA) &&
198 kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_CLR_CMMA);
200 return value;
203 void kvm_s390_cmma_reset(void)
205 int rc;
206 struct kvm_device_attr attr = {
207 .group = KVM_S390_VM_MEM_CTRL,
208 .attr = KVM_S390_VM_MEM_CLR_CMMA,
211 if (!kvm_s390_cmma_active()) {
212 return;
215 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
216 trace_kvm_clear_cmma(rc);
219 static void kvm_s390_enable_cmma(void)
221 int rc;
222 struct kvm_device_attr attr = {
223 .group = KVM_S390_VM_MEM_CTRL,
224 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
227 if (mem_path) {
228 warn_report("CMM will not be enabled because it is not "
229 "compatible with hugetlbfs.");
230 return;
232 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
233 active_cmma = !rc;
234 trace_kvm_enable_cmma(rc);
237 static void kvm_s390_set_attr(uint64_t attr)
239 struct kvm_device_attr attribute = {
240 .group = KVM_S390_VM_CRYPTO,
241 .attr = attr,
244 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
246 if (ret) {
247 error_report("Failed to set crypto device attribute %lu: %s",
248 attr, strerror(-ret));
252 static void kvm_s390_init_aes_kw(void)
254 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
256 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
257 NULL)) {
258 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
261 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
262 kvm_s390_set_attr(attr);
266 static void kvm_s390_init_dea_kw(void)
268 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
270 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
271 NULL)) {
272 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
275 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
276 kvm_s390_set_attr(attr);
280 void kvm_s390_crypto_reset(void)
282 if (s390_has_feat(S390_FEAT_MSA_EXT_3)) {
283 kvm_s390_init_aes_kw();
284 kvm_s390_init_dea_kw();
288 int kvm_arch_init(MachineState *ms, KVMState *s)
290 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
291 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
292 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
293 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
295 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
296 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
297 phys_mem_set_alloc(legacy_s390_alloc);
300 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
301 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
302 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
303 if (ri_allowed()) {
304 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
305 cap_ri = 1;
308 if (gs_allowed()) {
309 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
310 cap_gs = 1;
315 * The migration interface for ais was introduced with kernel 4.13
316 * but the capability itself had been active since 4.12. As migration
317 * support is considered necessary let's disable ais in the 2.10
318 * machine.
320 /* kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0); */
322 qemu_mutex_init(&qemu_sigp_mutex);
324 return 0;
327 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
329 return 0;
332 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
334 return cpu->cpu_index;
337 int kvm_arch_init_vcpu(CPUState *cs)
339 S390CPU *cpu = S390_CPU(cs);
340 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
341 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
342 return 0;
345 void kvm_s390_reset_vcpu(S390CPU *cpu)
347 CPUState *cs = CPU(cpu);
349 /* The initial reset call is needed here to reset in-kernel
350 * vcpu data that we can't access directly from QEMU
351 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
352 * Before this ioctl cpu_synchronize_state() is called in common kvm
353 * code (kvm-all) */
354 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
355 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
359 static int can_sync_regs(CPUState *cs, int regs)
361 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
364 int kvm_arch_put_registers(CPUState *cs, int level)
366 S390CPU *cpu = S390_CPU(cs);
367 CPUS390XState *env = &cpu->env;
368 struct kvm_sregs sregs;
369 struct kvm_regs regs;
370 struct kvm_fpu fpu = {};
371 int r;
372 int i;
374 /* always save the PSW and the GPRS*/
375 cs->kvm_run->psw_addr = env->psw.addr;
376 cs->kvm_run->psw_mask = env->psw.mask;
378 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
379 for (i = 0; i < 16; i++) {
380 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
381 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
383 } else {
384 for (i = 0; i < 16; i++) {
385 regs.gprs[i] = env->regs[i];
387 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
388 if (r < 0) {
389 return r;
393 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
394 for (i = 0; i < 32; i++) {
395 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
396 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
398 cs->kvm_run->s.regs.fpc = env->fpc;
399 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
400 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
401 for (i = 0; i < 16; i++) {
402 cs->kvm_run->s.regs.fprs[i] = get_freg(env, i)->ll;
404 cs->kvm_run->s.regs.fpc = env->fpc;
405 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
406 } else {
407 /* Floating point */
408 for (i = 0; i < 16; i++) {
409 fpu.fprs[i] = get_freg(env, i)->ll;
411 fpu.fpc = env->fpc;
413 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
414 if (r < 0) {
415 return r;
419 /* Do we need to save more than that? */
420 if (level == KVM_PUT_RUNTIME_STATE) {
421 return 0;
424 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
425 cs->kvm_run->s.regs.cputm = env->cputm;
426 cs->kvm_run->s.regs.ckc = env->ckc;
427 cs->kvm_run->s.regs.todpr = env->todpr;
428 cs->kvm_run->s.regs.gbea = env->gbea;
429 cs->kvm_run->s.regs.pp = env->pp;
430 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
431 } else {
433 * These ONE_REGS are not protected by a capability. As they are only
434 * necessary for migration we just trace a possible error, but don't
435 * return with an error return code.
437 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
438 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
439 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
440 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
441 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
444 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
445 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
446 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
449 /* pfault parameters */
450 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
451 cs->kvm_run->s.regs.pft = env->pfault_token;
452 cs->kvm_run->s.regs.pfs = env->pfault_select;
453 cs->kvm_run->s.regs.pfc = env->pfault_compare;
454 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
455 } else if (cap_async_pf) {
456 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
457 if (r < 0) {
458 return r;
460 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
461 if (r < 0) {
462 return r;
464 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
465 if (r < 0) {
466 return r;
470 /* access registers and control registers*/
471 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
472 for (i = 0; i < 16; i++) {
473 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
474 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
476 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
477 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
478 } else {
479 for (i = 0; i < 16; i++) {
480 sregs.acrs[i] = env->aregs[i];
481 sregs.crs[i] = env->cregs[i];
483 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
484 if (r < 0) {
485 return r;
489 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
490 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
491 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
494 /* Finally the prefix */
495 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
496 cs->kvm_run->s.regs.prefix = env->psa;
497 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
498 } else {
499 /* prefix is only supported via sync regs */
501 return 0;
504 int kvm_arch_get_registers(CPUState *cs)
506 S390CPU *cpu = S390_CPU(cs);
507 CPUS390XState *env = &cpu->env;
508 struct kvm_sregs sregs;
509 struct kvm_regs regs;
510 struct kvm_fpu fpu;
511 int i, r;
513 /* get the PSW */
514 env->psw.addr = cs->kvm_run->psw_addr;
515 env->psw.mask = cs->kvm_run->psw_mask;
517 /* the GPRS */
518 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
519 for (i = 0; i < 16; i++) {
520 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
522 } else {
523 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
524 if (r < 0) {
525 return r;
527 for (i = 0; i < 16; i++) {
528 env->regs[i] = regs.gprs[i];
532 /* The ACRS and CRS */
533 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
534 for (i = 0; i < 16; i++) {
535 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
536 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
538 } else {
539 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
540 if (r < 0) {
541 return r;
543 for (i = 0; i < 16; i++) {
544 env->aregs[i] = sregs.acrs[i];
545 env->cregs[i] = sregs.crs[i];
549 /* Floating point and vector registers */
550 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
551 for (i = 0; i < 32; i++) {
552 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
553 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
555 env->fpc = cs->kvm_run->s.regs.fpc;
556 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
557 for (i = 0; i < 16; i++) {
558 get_freg(env, i)->ll = cs->kvm_run->s.regs.fprs[i];
560 env->fpc = cs->kvm_run->s.regs.fpc;
561 } else {
562 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
563 if (r < 0) {
564 return r;
566 for (i = 0; i < 16; i++) {
567 get_freg(env, i)->ll = fpu.fprs[i];
569 env->fpc = fpu.fpc;
572 /* The prefix */
573 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
574 env->psa = cs->kvm_run->s.regs.prefix;
577 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
578 env->cputm = cs->kvm_run->s.regs.cputm;
579 env->ckc = cs->kvm_run->s.regs.ckc;
580 env->todpr = cs->kvm_run->s.regs.todpr;
581 env->gbea = cs->kvm_run->s.regs.gbea;
582 env->pp = cs->kvm_run->s.regs.pp;
583 } else {
585 * These ONE_REGS are not protected by a capability. As they are only
586 * necessary for migration we just trace a possible error, but don't
587 * return with an error return code.
589 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
590 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
591 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
592 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
593 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
596 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
597 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
600 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
601 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
604 /* pfault parameters */
605 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
606 env->pfault_token = cs->kvm_run->s.regs.pft;
607 env->pfault_select = cs->kvm_run->s.regs.pfs;
608 env->pfault_compare = cs->kvm_run->s.regs.pfc;
609 } else if (cap_async_pf) {
610 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
611 if (r < 0) {
612 return r;
614 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
615 if (r < 0) {
616 return r;
618 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
619 if (r < 0) {
620 return r;
624 return 0;
627 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
629 int r;
630 struct kvm_device_attr attr = {
631 .group = KVM_S390_VM_TOD,
632 .attr = KVM_S390_VM_TOD_LOW,
633 .addr = (uint64_t)tod_low,
636 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
637 if (r) {
638 return r;
641 attr.attr = KVM_S390_VM_TOD_HIGH;
642 attr.addr = (uint64_t)tod_high;
643 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
646 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
648 int r;
650 struct kvm_device_attr attr = {
651 .group = KVM_S390_VM_TOD,
652 .attr = KVM_S390_VM_TOD_LOW,
653 .addr = (uint64_t)tod_low,
656 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
657 if (r) {
658 return r;
661 attr.attr = KVM_S390_VM_TOD_HIGH;
662 attr.addr = (uint64_t)tod_high;
663 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
667 * kvm_s390_mem_op:
668 * @addr: the logical start address in guest memory
669 * @ar: the access register number
670 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
671 * @len: length that should be transferred
672 * @is_write: true = write, false = read
673 * Returns: 0 on success, non-zero if an exception or error occurred
675 * Use KVM ioctl to read/write from/to guest memory. An access exception
676 * is injected into the vCPU in case of translation errors.
678 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
679 int len, bool is_write)
681 struct kvm_s390_mem_op mem_op = {
682 .gaddr = addr,
683 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
684 .size = len,
685 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
686 : KVM_S390_MEMOP_LOGICAL_READ,
687 .buf = (uint64_t)hostbuf,
688 .ar = ar,
690 int ret;
692 if (!cap_mem_op) {
693 return -ENOSYS;
695 if (!hostbuf) {
696 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
699 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
700 if (ret < 0) {
701 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
703 return ret;
707 * Legacy layout for s390:
708 * Older S390 KVM requires the topmost vma of the RAM to be
709 * smaller than an system defined value, which is at least 256GB.
710 * Larger systems have larger values. We put the guest between
711 * the end of data segment (system break) and this value. We
712 * use 32GB as a base to have enough room for the system break
713 * to grow. We also have to use MAP parameters that avoid
714 * read-only mapping of guest pages.
716 static void *legacy_s390_alloc(size_t size, uint64_t *align)
718 void *mem;
720 mem = mmap((void *) 0x800000000ULL, size,
721 PROT_EXEC|PROT_READ|PROT_WRITE,
722 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
723 return mem == MAP_FAILED ? NULL : mem;
726 static uint8_t const *sw_bp_inst;
727 static uint8_t sw_bp_ilen;
729 static void determine_sw_breakpoint_instr(void)
731 /* DIAG 501 is used for sw breakpoints with old kernels */
732 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
733 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
734 static const uint8_t instr_0x0000[] = {0x00, 0x00};
736 if (sw_bp_inst) {
737 return;
739 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
740 sw_bp_inst = diag_501;
741 sw_bp_ilen = sizeof(diag_501);
742 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
743 } else {
744 sw_bp_inst = instr_0x0000;
745 sw_bp_ilen = sizeof(instr_0x0000);
746 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
750 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
752 determine_sw_breakpoint_instr();
754 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
755 sw_bp_ilen, 0) ||
756 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
757 return -EINVAL;
759 return 0;
762 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
764 uint8_t t[MAX_ILEN];
766 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
767 return -EINVAL;
768 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
769 return -EINVAL;
770 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
771 sw_bp_ilen, 1)) {
772 return -EINVAL;
775 return 0;
778 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
779 int len, int type)
781 int n;
783 for (n = 0; n < nb_hw_breakpoints; n++) {
784 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
785 (hw_breakpoints[n].len == len || len == -1)) {
786 return &hw_breakpoints[n];
790 return NULL;
793 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
795 int size;
797 if (find_hw_breakpoint(addr, len, type)) {
798 return -EEXIST;
801 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
803 if (!hw_breakpoints) {
804 nb_hw_breakpoints = 0;
805 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
806 } else {
807 hw_breakpoints =
808 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
811 if (!hw_breakpoints) {
812 nb_hw_breakpoints = 0;
813 return -ENOMEM;
816 hw_breakpoints[nb_hw_breakpoints].addr = addr;
817 hw_breakpoints[nb_hw_breakpoints].len = len;
818 hw_breakpoints[nb_hw_breakpoints].type = type;
820 nb_hw_breakpoints++;
822 return 0;
825 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
826 target_ulong len, int type)
828 switch (type) {
829 case GDB_BREAKPOINT_HW:
830 type = KVM_HW_BP;
831 break;
832 case GDB_WATCHPOINT_WRITE:
833 if (len < 1) {
834 return -EINVAL;
836 type = KVM_HW_WP_WRITE;
837 break;
838 default:
839 return -ENOSYS;
841 return insert_hw_breakpoint(addr, len, type);
844 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
845 target_ulong len, int type)
847 int size;
848 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
850 if (bp == NULL) {
851 return -ENOENT;
854 nb_hw_breakpoints--;
855 if (nb_hw_breakpoints > 0) {
857 * In order to trim the array, move the last element to the position to
858 * be removed - if necessary.
860 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
861 *bp = hw_breakpoints[nb_hw_breakpoints];
863 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
864 hw_breakpoints =
865 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
866 } else {
867 g_free(hw_breakpoints);
868 hw_breakpoints = NULL;
871 return 0;
874 void kvm_arch_remove_all_hw_breakpoints(void)
876 nb_hw_breakpoints = 0;
877 g_free(hw_breakpoints);
878 hw_breakpoints = NULL;
881 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
883 int i;
885 if (nb_hw_breakpoints > 0) {
886 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
887 dbg->arch.hw_bp = hw_breakpoints;
889 for (i = 0; i < nb_hw_breakpoints; ++i) {
890 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
891 hw_breakpoints[i].addr);
893 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
894 } else {
895 dbg->arch.nr_hw_bp = 0;
896 dbg->arch.hw_bp = NULL;
900 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
904 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
906 return MEMTXATTRS_UNSPECIFIED;
909 int kvm_arch_process_async_events(CPUState *cs)
911 return cs->halted;
914 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
915 struct kvm_s390_interrupt *interrupt)
917 int r = 0;
919 interrupt->type = irq->type;
920 switch (irq->type) {
921 case KVM_S390_INT_VIRTIO:
922 interrupt->parm = irq->u.ext.ext_params;
923 /* fall through */
924 case KVM_S390_INT_PFAULT_INIT:
925 case KVM_S390_INT_PFAULT_DONE:
926 interrupt->parm64 = irq->u.ext.ext_params2;
927 break;
928 case KVM_S390_PROGRAM_INT:
929 interrupt->parm = irq->u.pgm.code;
930 break;
931 case KVM_S390_SIGP_SET_PREFIX:
932 interrupt->parm = irq->u.prefix.address;
933 break;
934 case KVM_S390_INT_SERVICE:
935 interrupt->parm = irq->u.ext.ext_params;
936 break;
937 case KVM_S390_MCHK:
938 interrupt->parm = irq->u.mchk.cr14;
939 interrupt->parm64 = irq->u.mchk.mcic;
940 break;
941 case KVM_S390_INT_EXTERNAL_CALL:
942 interrupt->parm = irq->u.extcall.code;
943 break;
944 case KVM_S390_INT_EMERGENCY:
945 interrupt->parm = irq->u.emerg.code;
946 break;
947 case KVM_S390_SIGP_STOP:
948 case KVM_S390_RESTART:
949 break; /* These types have no parameters */
950 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
951 interrupt->parm = irq->u.io.subchannel_id << 16;
952 interrupt->parm |= irq->u.io.subchannel_nr;
953 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
954 interrupt->parm64 |= irq->u.io.io_int_word;
955 break;
956 default:
957 r = -EINVAL;
958 break;
960 return r;
963 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
965 struct kvm_s390_interrupt kvmint = {};
966 int r;
968 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
969 if (r < 0) {
970 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
971 exit(1);
974 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
975 if (r < 0) {
976 fprintf(stderr, "KVM failed to inject interrupt\n");
977 exit(1);
981 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
983 CPUState *cs = CPU(cpu);
984 int r;
986 if (cap_s390_irq) {
987 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
988 if (!r) {
989 return;
991 error_report("KVM failed to inject interrupt %llx", irq->type);
992 exit(1);
995 inject_vcpu_irq_legacy(cs, irq);
998 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
1000 struct kvm_s390_interrupt kvmint = {};
1001 int r;
1003 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1004 if (r < 0) {
1005 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1006 exit(1);
1009 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1010 if (r < 0) {
1011 fprintf(stderr, "KVM failed to inject interrupt\n");
1012 exit(1);
1016 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
1018 static bool use_flic = true;
1019 int r;
1021 if (use_flic) {
1022 r = kvm_s390_inject_flic(irq);
1023 if (r == -ENOSYS) {
1024 use_flic = false;
1026 if (!r) {
1027 return;
1030 __kvm_s390_floating_interrupt(irq);
1033 void kvm_s390_service_interrupt(uint32_t parm)
1035 struct kvm_s390_irq irq = {
1036 .type = KVM_S390_INT_SERVICE,
1037 .u.ext.ext_params = parm,
1040 kvm_s390_floating_interrupt(&irq);
1043 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1045 struct kvm_s390_irq irq = {
1046 .type = KVM_S390_PROGRAM_INT,
1047 .u.pgm.code = code,
1050 kvm_s390_vcpu_interrupt(cpu, &irq);
1053 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1055 struct kvm_s390_irq irq = {
1056 .type = KVM_S390_PROGRAM_INT,
1057 .u.pgm.code = code,
1058 .u.pgm.trans_exc_code = te_code,
1059 .u.pgm.exc_access_id = te_code & 3,
1062 kvm_s390_vcpu_interrupt(cpu, &irq);
1065 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1066 uint16_t ipbh0)
1068 CPUS390XState *env = &cpu->env;
1069 uint64_t sccb;
1070 uint32_t code;
1071 int r = 0;
1073 cpu_synchronize_state(CPU(cpu));
1074 sccb = env->regs[ipbh0 & 0xf];
1075 code = env->regs[(ipbh0 & 0xf0) >> 4];
1077 r = sclp_service_call(env, sccb, code);
1078 if (r < 0) {
1079 kvm_s390_program_interrupt(cpu, -r);
1080 } else {
1081 setcc(cpu, r);
1084 return 0;
1087 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1089 CPUS390XState *env = &cpu->env;
1090 int rc = 0;
1091 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1093 cpu_synchronize_state(CPU(cpu));
1095 switch (ipa1) {
1096 case PRIV_B2_XSCH:
1097 ioinst_handle_xsch(cpu, env->regs[1]);
1098 break;
1099 case PRIV_B2_CSCH:
1100 ioinst_handle_csch(cpu, env->regs[1]);
1101 break;
1102 case PRIV_B2_HSCH:
1103 ioinst_handle_hsch(cpu, env->regs[1]);
1104 break;
1105 case PRIV_B2_MSCH:
1106 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1107 break;
1108 case PRIV_B2_SSCH:
1109 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1110 break;
1111 case PRIV_B2_STCRW:
1112 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1113 break;
1114 case PRIV_B2_STSCH:
1115 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1116 break;
1117 case PRIV_B2_TSCH:
1118 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1119 fprintf(stderr, "Spurious tsch intercept\n");
1120 break;
1121 case PRIV_B2_CHSC:
1122 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1123 break;
1124 case PRIV_B2_TPI:
1125 /* This should have been handled by kvm already. */
1126 fprintf(stderr, "Spurious tpi intercept\n");
1127 break;
1128 case PRIV_B2_SCHM:
1129 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1130 run->s390_sieic.ipb);
1131 break;
1132 case PRIV_B2_RSCH:
1133 ioinst_handle_rsch(cpu, env->regs[1]);
1134 break;
1135 case PRIV_B2_RCHP:
1136 ioinst_handle_rchp(cpu, env->regs[1]);
1137 break;
1138 case PRIV_B2_STCPS:
1139 /* We do not provide this instruction, it is suppressed. */
1140 break;
1141 case PRIV_B2_SAL:
1142 ioinst_handle_sal(cpu, env->regs[1]);
1143 break;
1144 case PRIV_B2_SIGA:
1145 /* Not provided, set CC = 3 for subchannel not operational */
1146 setcc(cpu, 3);
1147 break;
1148 case PRIV_B2_SCLP_CALL:
1149 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1150 break;
1151 default:
1152 rc = -1;
1153 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1154 break;
1157 return rc;
1160 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1161 uint8_t *ar)
1163 CPUS390XState *env = &cpu->env;
1164 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1165 uint32_t base2 = run->s390_sieic.ipb >> 28;
1166 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1167 ((run->s390_sieic.ipb & 0xff00) << 4);
1169 if (disp2 & 0x80000) {
1170 disp2 += 0xfff00000;
1172 if (ar) {
1173 *ar = base2;
1176 return (base2 ? env->regs[base2] : 0) +
1177 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1180 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1181 uint8_t *ar)
1183 CPUS390XState *env = &cpu->env;
1184 uint32_t base2 = run->s390_sieic.ipb >> 28;
1185 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1186 ((run->s390_sieic.ipb & 0xff00) << 4);
1188 if (disp2 & 0x80000) {
1189 disp2 += 0xfff00000;
1191 if (ar) {
1192 *ar = base2;
1195 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1198 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1200 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1202 if (s390_has_feat(S390_FEAT_ZPCI)) {
1203 return clp_service_call(cpu, r2);
1204 } else {
1205 return -1;
1209 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1211 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1212 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1214 if (s390_has_feat(S390_FEAT_ZPCI)) {
1215 return pcilg_service_call(cpu, r1, r2);
1216 } else {
1217 return -1;
1221 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1223 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1224 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1226 if (s390_has_feat(S390_FEAT_ZPCI)) {
1227 return pcistg_service_call(cpu, r1, r2);
1228 } else {
1229 return -1;
1233 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1235 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1236 uint64_t fiba;
1237 uint8_t ar;
1239 if (s390_has_feat(S390_FEAT_ZPCI)) {
1240 cpu_synchronize_state(CPU(cpu));
1241 fiba = get_base_disp_rxy(cpu, run, &ar);
1243 return stpcifc_service_call(cpu, r1, fiba, ar);
1244 } else {
1245 return -1;
1249 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1251 CPUS390XState *env = &cpu->env;
1252 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1253 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1254 uint8_t isc;
1255 uint16_t mode;
1256 int r;
1258 cpu_synchronize_state(CPU(cpu));
1259 mode = env->regs[r1] & 0xffff;
1260 isc = (env->regs[r3] >> 27) & 0x7;
1261 r = css_do_sic(env, isc, mode);
1262 if (r) {
1263 kvm_s390_program_interrupt(cpu, -r);
1266 return 0;
1269 static int kvm_rpcit_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 rpcit_service_call(cpu, r1, r2);
1276 } else {
1277 return -1;
1281 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1283 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1284 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1285 uint64_t gaddr;
1286 uint8_t ar;
1288 if (s390_has_feat(S390_FEAT_ZPCI)) {
1289 cpu_synchronize_state(CPU(cpu));
1290 gaddr = get_base_disp_rsy(cpu, run, &ar);
1292 return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1293 } else {
1294 return -1;
1298 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1300 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1301 uint64_t fiba;
1302 uint8_t ar;
1304 if (s390_has_feat(S390_FEAT_ZPCI)) {
1305 cpu_synchronize_state(CPU(cpu));
1306 fiba = get_base_disp_rxy(cpu, run, &ar);
1308 return mpcifc_service_call(cpu, r1, fiba, ar);
1309 } else {
1310 return -1;
1314 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1316 int r = 0;
1318 switch (ipa1) {
1319 case PRIV_B9_CLP:
1320 r = kvm_clp_service_call(cpu, run);
1321 break;
1322 case PRIV_B9_PCISTG:
1323 r = kvm_pcistg_service_call(cpu, run);
1324 break;
1325 case PRIV_B9_PCILG:
1326 r = kvm_pcilg_service_call(cpu, run);
1327 break;
1328 case PRIV_B9_RPCIT:
1329 r = kvm_rpcit_service_call(cpu, run);
1330 break;
1331 case PRIV_B9_EQBS:
1332 /* just inject exception */
1333 r = -1;
1334 break;
1335 default:
1336 r = -1;
1337 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1338 break;
1341 return r;
1344 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1346 int r = 0;
1348 switch (ipbl) {
1349 case PRIV_EB_PCISTB:
1350 r = kvm_pcistb_service_call(cpu, run);
1351 break;
1352 case PRIV_EB_SIC:
1353 r = kvm_sic_service_call(cpu, run);
1354 break;
1355 case PRIV_EB_SQBS:
1356 /* just inject exception */
1357 r = -1;
1358 break;
1359 default:
1360 r = -1;
1361 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1362 break;
1365 return r;
1368 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1370 int r = 0;
1372 switch (ipbl) {
1373 case PRIV_E3_MPCIFC:
1374 r = kvm_mpcifc_service_call(cpu, run);
1375 break;
1376 case PRIV_E3_STPCIFC:
1377 r = kvm_stpcifc_service_call(cpu, run);
1378 break;
1379 default:
1380 r = -1;
1381 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1382 break;
1385 return r;
1388 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1390 CPUS390XState *env = &cpu->env;
1391 int ret;
1393 cpu_synchronize_state(CPU(cpu));
1394 ret = s390_virtio_hypercall(env);
1395 if (ret == -EINVAL) {
1396 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1397 return 0;
1400 return ret;
1403 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1405 uint64_t r1, r3;
1406 int rc;
1408 cpu_synchronize_state(CPU(cpu));
1409 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1410 r3 = run->s390_sieic.ipa & 0x000f;
1411 rc = handle_diag_288(&cpu->env, r1, r3);
1412 if (rc) {
1413 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1417 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1419 uint64_t r1, r3;
1421 cpu_synchronize_state(CPU(cpu));
1422 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1423 r3 = run->s390_sieic.ipa & 0x000f;
1424 handle_diag_308(&cpu->env, r1, r3);
1427 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1429 CPUS390XState *env = &cpu->env;
1430 unsigned long pc;
1432 cpu_synchronize_state(CPU(cpu));
1434 pc = env->psw.addr - sw_bp_ilen;
1435 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1436 env->psw.addr = pc;
1437 return EXCP_DEBUG;
1440 return -ENOENT;
1443 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1445 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1447 int r = 0;
1448 uint16_t func_code;
1451 * For any diagnose call we support, bits 48-63 of the resulting
1452 * address specify the function code; the remainder is ignored.
1454 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1455 switch (func_code) {
1456 case DIAG_TIMEREVENT:
1457 kvm_handle_diag_288(cpu, run);
1458 break;
1459 case DIAG_IPL:
1460 kvm_handle_diag_308(cpu, run);
1461 break;
1462 case DIAG_KVM_HYPERCALL:
1463 r = handle_hypercall(cpu, run);
1464 break;
1465 case DIAG_KVM_BREAKPOINT:
1466 r = handle_sw_breakpoint(cpu, run);
1467 break;
1468 default:
1469 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1470 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1471 break;
1474 return r;
1477 typedef struct SigpInfo {
1478 uint64_t param;
1479 int cc;
1480 uint64_t *status_reg;
1481 } SigpInfo;
1483 static void set_sigp_status(SigpInfo *si, uint64_t status)
1485 *si->status_reg &= 0xffffffff00000000ULL;
1486 *si->status_reg |= status;
1487 si->cc = SIGP_CC_STATUS_STORED;
1490 static void sigp_start(CPUState *cs, run_on_cpu_data arg)
1492 S390CPU *cpu = S390_CPU(cs);
1493 SigpInfo *si = arg.host_ptr;
1495 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1496 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1497 return;
1500 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1501 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1504 static void sigp_stop(CPUState *cs, run_on_cpu_data arg)
1506 S390CPU *cpu = S390_CPU(cs);
1507 SigpInfo *si = arg.host_ptr;
1508 struct kvm_s390_irq irq = {
1509 .type = KVM_S390_SIGP_STOP,
1512 if (s390_cpu_get_state(cpu) != CPU_STATE_OPERATING) {
1513 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1514 return;
1517 /* disabled wait - sleeping in user space */
1518 if (cs->halted) {
1519 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
1520 } else {
1521 /* execute the stop function */
1522 cpu->env.sigp_order = SIGP_STOP;
1523 kvm_s390_vcpu_interrupt(cpu, &irq);
1525 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1528 #define ADTL_GS_OFFSET 1024 /* offset of GS data in adtl save area */
1529 #define ADTL_GS_MIN_SIZE 2048 /* minimal size of adtl save area for GS */
1530 static int do_store_adtl_status(S390CPU *cpu, hwaddr addr, hwaddr len)
1532 hwaddr save = len;
1533 void *mem;
1535 mem = cpu_physical_memory_map(addr, &save, 1);
1536 if (!mem) {
1537 return -EFAULT;
1539 if (save != len) {
1540 cpu_physical_memory_unmap(mem, len, 1, 0);
1541 return -EFAULT;
1544 if (s390_has_feat(S390_FEAT_VECTOR)) {
1545 memcpy(mem, &cpu->env.vregs, 512);
1547 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE) && len >= ADTL_GS_MIN_SIZE) {
1548 memcpy(mem + ADTL_GS_OFFSET, &cpu->env.gscb, 32);
1551 cpu_physical_memory_unmap(mem, len, 1, len);
1553 return 0;
1556 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1557 #define SAVE_AREA_SIZE 512
1558 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1560 static const uint8_t ar_id = 1;
1561 uint64_t ckc = cpu->env.ckc >> 8;
1562 void *mem;
1563 int i;
1564 hwaddr len = SAVE_AREA_SIZE;
1566 mem = cpu_physical_memory_map(addr, &len, 1);
1567 if (!mem) {
1568 return -EFAULT;
1570 if (len != SAVE_AREA_SIZE) {
1571 cpu_physical_memory_unmap(mem, len, 1, 0);
1572 return -EFAULT;
1575 if (store_arch) {
1576 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1578 for (i = 0; i < 16; ++i) {
1579 *((uint64_t *)mem + i) = get_freg(&cpu->env, i)->ll;
1581 memcpy(mem + 128, &cpu->env.regs, 128);
1582 memcpy(mem + 256, &cpu->env.psw, 16);
1583 memcpy(mem + 280, &cpu->env.psa, 4);
1584 memcpy(mem + 284, &cpu->env.fpc, 4);
1585 memcpy(mem + 292, &cpu->env.todpr, 4);
1586 memcpy(mem + 296, &cpu->env.cputm, 8);
1587 memcpy(mem + 304, &ckc, 8);
1588 memcpy(mem + 320, &cpu->env.aregs, 64);
1589 memcpy(mem + 384, &cpu->env.cregs, 128);
1591 cpu_physical_memory_unmap(mem, len, 1, len);
1593 return 0;
1596 static void sigp_stop_and_store_status(CPUState *cs, run_on_cpu_data arg)
1598 S390CPU *cpu = S390_CPU(cs);
1599 SigpInfo *si = arg.host_ptr;
1600 struct kvm_s390_irq irq = {
1601 .type = KVM_S390_SIGP_STOP,
1604 /* disabled wait - sleeping in user space */
1605 if (s390_cpu_get_state(cpu) == CPU_STATE_OPERATING && cs->halted) {
1606 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
1609 switch (s390_cpu_get_state(cpu)) {
1610 case CPU_STATE_OPERATING:
1611 cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1612 kvm_s390_vcpu_interrupt(cpu, &irq);
1613 /* store will be performed when handling the stop intercept */
1614 break;
1615 case CPU_STATE_STOPPED:
1616 /* already stopped, just store the status */
1617 cpu_synchronize_state(cs);
1618 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1619 break;
1621 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1624 static void sigp_store_status_at_address(CPUState *cs, run_on_cpu_data arg)
1626 S390CPU *cpu = S390_CPU(cs);
1627 SigpInfo *si = arg.host_ptr;
1628 uint32_t address = si->param & 0x7ffffe00u;
1630 /* cpu has to be stopped */
1631 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1632 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1633 return;
1636 cpu_synchronize_state(cs);
1638 if (kvm_s390_store_status(cpu, address, false)) {
1639 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1640 return;
1642 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1645 #define ADTL_SAVE_LC_MASK 0xfUL
1646 static void sigp_store_adtl_status(CPUState *cs, run_on_cpu_data arg)
1648 S390CPU *cpu = S390_CPU(cs);
1649 SigpInfo *si = arg.host_ptr;
1650 uint8_t lc = si->param & ADTL_SAVE_LC_MASK;
1651 hwaddr addr = si->param & ~ADTL_SAVE_LC_MASK;
1652 hwaddr len = 1UL << (lc ? lc : 10);
1654 if (!s390_has_feat(S390_FEAT_VECTOR) &&
1655 !s390_has_feat(S390_FEAT_GUARDED_STORAGE)) {
1656 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1657 return;
1660 /* cpu has to be stopped */
1661 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1662 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1663 return;
1666 /* address must be aligned to length */
1667 if (addr & (len - 1)) {
1668 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1669 return;
1672 /* no GS: only lc == 0 is valid */
1673 if (!s390_has_feat(S390_FEAT_GUARDED_STORAGE) &&
1674 lc != 0) {
1675 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1676 return;
1679 /* GS: 0, 10, 11, 12 are valid */
1680 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE) &&
1681 lc != 0 &&
1682 lc != 10 &&
1683 lc != 11 &&
1684 lc != 12) {
1685 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1686 return;
1689 cpu_synchronize_state(cs);
1691 if (do_store_adtl_status(cpu, addr, len)) {
1692 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1693 return;
1695 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1698 static void sigp_restart(CPUState *cs, run_on_cpu_data arg)
1700 S390CPU *cpu = S390_CPU(cs);
1701 SigpInfo *si = arg.host_ptr;
1702 struct kvm_s390_irq irq = {
1703 .type = KVM_S390_RESTART,
1706 switch (s390_cpu_get_state(cpu)) {
1707 case CPU_STATE_STOPPED:
1708 /* the restart irq has to be delivered prior to any other pending irq */
1709 cpu_synchronize_state(cs);
1710 do_restart_interrupt(&cpu->env);
1711 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1712 break;
1713 case CPU_STATE_OPERATING:
1714 kvm_s390_vcpu_interrupt(cpu, &irq);
1715 break;
1717 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1720 int kvm_s390_cpu_restart(S390CPU *cpu)
1722 SigpInfo si = {};
1724 run_on_cpu(CPU(cpu), sigp_restart, RUN_ON_CPU_HOST_PTR(&si));
1725 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1726 return 0;
1729 static void sigp_initial_cpu_reset(CPUState *cs, run_on_cpu_data arg)
1731 S390CPU *cpu = S390_CPU(cs);
1732 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1733 SigpInfo *si = arg.host_ptr;
1735 cpu_synchronize_state(cs);
1736 scc->initial_cpu_reset(cs);
1737 cpu_synchronize_post_reset(cs);
1738 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1741 static void sigp_cpu_reset(CPUState *cs, run_on_cpu_data arg)
1743 S390CPU *cpu = S390_CPU(cs);
1744 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1745 SigpInfo *si = arg.host_ptr;
1747 cpu_synchronize_state(cs);
1748 scc->cpu_reset(cs);
1749 cpu_synchronize_post_reset(cs);
1750 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1753 static void sigp_set_prefix(CPUState *cs, run_on_cpu_data arg)
1755 S390CPU *cpu = S390_CPU(cs);
1756 SigpInfo *si = arg.host_ptr;
1757 uint32_t addr = si->param & 0x7fffe000u;
1759 cpu_synchronize_state(cs);
1761 if (!address_space_access_valid(&address_space_memory, addr,
1762 sizeof(struct LowCore), false)) {
1763 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1764 return;
1767 /* cpu has to be stopped */
1768 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1769 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1770 return;
1773 cpu->env.psa = addr;
1774 cpu_synchronize_post_init(cs);
1775 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1778 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1779 uint64_t param, uint64_t *status_reg)
1781 SigpInfo si = {
1782 .param = param,
1783 .status_reg = status_reg,
1786 /* cpu available? */
1787 if (dst_cpu == NULL) {
1788 return SIGP_CC_NOT_OPERATIONAL;
1791 /* only resets can break pending orders */
1792 if (dst_cpu->env.sigp_order != 0 &&
1793 order != SIGP_CPU_RESET &&
1794 order != SIGP_INITIAL_CPU_RESET) {
1795 return SIGP_CC_BUSY;
1798 switch (order) {
1799 case SIGP_START:
1800 run_on_cpu(CPU(dst_cpu), sigp_start, RUN_ON_CPU_HOST_PTR(&si));
1801 break;
1802 case SIGP_STOP:
1803 run_on_cpu(CPU(dst_cpu), sigp_stop, RUN_ON_CPU_HOST_PTR(&si));
1804 break;
1805 case SIGP_RESTART:
1806 run_on_cpu(CPU(dst_cpu), sigp_restart, RUN_ON_CPU_HOST_PTR(&si));
1807 break;
1808 case SIGP_STOP_STORE_STATUS:
1809 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, RUN_ON_CPU_HOST_PTR(&si));
1810 break;
1811 case SIGP_STORE_STATUS_ADDR:
1812 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, RUN_ON_CPU_HOST_PTR(&si));
1813 break;
1814 case SIGP_STORE_ADTL_STATUS:
1815 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, RUN_ON_CPU_HOST_PTR(&si));
1816 break;
1817 case SIGP_SET_PREFIX:
1818 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, RUN_ON_CPU_HOST_PTR(&si));
1819 break;
1820 case SIGP_INITIAL_CPU_RESET:
1821 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, RUN_ON_CPU_HOST_PTR(&si));
1822 break;
1823 case SIGP_CPU_RESET:
1824 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, RUN_ON_CPU_HOST_PTR(&si));
1825 break;
1826 default:
1827 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1828 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1831 return si.cc;
1834 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1835 uint64_t *status_reg)
1837 CPUState *cur_cs;
1838 S390CPU *cur_cpu;
1839 bool all_stopped = true;
1841 CPU_FOREACH(cur_cs) {
1842 cur_cpu = S390_CPU(cur_cs);
1844 if (cur_cpu == cpu) {
1845 continue;
1847 if (s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1848 all_stopped = false;
1852 *status_reg &= 0xffffffff00000000ULL;
1854 /* Reject set arch order, with czam we're always in z/Arch mode. */
1855 *status_reg |= (all_stopped ? SIGP_STAT_INVALID_PARAMETER :
1856 SIGP_STAT_INCORRECT_STATE);
1857 return SIGP_CC_STATUS_STORED;
1860 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1862 CPUS390XState *env = &cpu->env;
1863 const uint8_t r1 = ipa1 >> 4;
1864 const uint8_t r3 = ipa1 & 0x0f;
1865 int ret;
1866 uint8_t order;
1867 uint64_t *status_reg;
1868 uint64_t param;
1869 S390CPU *dst_cpu = NULL;
1871 cpu_synchronize_state(CPU(cpu));
1873 /* get order code */
1874 order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1875 & SIGP_ORDER_MASK;
1876 status_reg = &env->regs[r1];
1877 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1879 if (qemu_mutex_trylock(&qemu_sigp_mutex)) {
1880 ret = SIGP_CC_BUSY;
1881 goto out;
1884 switch (order) {
1885 case SIGP_SET_ARCH:
1886 ret = sigp_set_architecture(cpu, param, status_reg);
1887 break;
1888 default:
1889 /* all other sigp orders target a single vcpu */
1890 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1891 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1893 qemu_mutex_unlock(&qemu_sigp_mutex);
1895 out:
1896 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1897 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1899 if (ret >= 0) {
1900 setcc(cpu, ret);
1901 return 0;
1904 return ret;
1907 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1909 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1910 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1911 int r = -1;
1913 DPRINTF("handle_instruction 0x%x 0x%x\n",
1914 run->s390_sieic.ipa, run->s390_sieic.ipb);
1915 switch (ipa0) {
1916 case IPA0_B2:
1917 r = handle_b2(cpu, run, ipa1);
1918 break;
1919 case IPA0_B9:
1920 r = handle_b9(cpu, run, ipa1);
1921 break;
1922 case IPA0_EB:
1923 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1924 break;
1925 case IPA0_E3:
1926 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1927 break;
1928 case IPA0_DIAG:
1929 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1930 break;
1931 case IPA0_SIGP:
1932 r = handle_sigp(cpu, run, ipa1);
1933 break;
1936 if (r < 0) {
1937 r = 0;
1938 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1941 return r;
1944 static bool is_special_wait_psw(CPUState *cs)
1946 /* signal quiesce */
1947 return cs->kvm_run->psw_addr == 0xfffUL;
1950 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1952 CPUState *cs = CPU(cpu);
1954 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1955 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1956 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1957 s390_cpu_halt(cpu);
1958 qemu_system_guest_panicked(NULL);
1961 /* try to detect pgm check loops */
1962 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1964 CPUState *cs = CPU(cpu);
1965 PSW oldpsw, newpsw;
1967 cpu_synchronize_state(cs);
1968 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1969 offsetof(LowCore, program_new_psw));
1970 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1971 offsetof(LowCore, program_new_psw) + 8);
1972 oldpsw.mask = run->psw_mask;
1973 oldpsw.addr = run->psw_addr;
1975 * Avoid endless loops of operation exceptions, if the pgm new
1976 * PSW will cause a new operation exception.
1977 * The heuristic checks if the pgm new psw is within 6 bytes before
1978 * the faulting psw address (with same DAT, AS settings) and the
1979 * new psw is not a wait psw and the fault was not triggered by
1980 * problem state. In that case go into crashed state.
1983 if (oldpsw.addr - newpsw.addr <= 6 &&
1984 !(newpsw.mask & PSW_MASK_WAIT) &&
1985 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1986 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1987 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1988 unmanageable_intercept(cpu, "operation exception loop",
1989 offsetof(LowCore, program_new_psw));
1990 return EXCP_HALTED;
1992 return 0;
1995 static int handle_intercept(S390CPU *cpu)
1997 CPUState *cs = CPU(cpu);
1998 struct kvm_run *run = cs->kvm_run;
1999 int icpt_code = run->s390_sieic.icptcode;
2000 int r = 0;
2002 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
2003 (long)cs->kvm_run->psw_addr);
2004 switch (icpt_code) {
2005 case ICPT_INSTRUCTION:
2006 r = handle_instruction(cpu, run);
2007 break;
2008 case ICPT_PROGRAM:
2009 unmanageable_intercept(cpu, "program interrupt",
2010 offsetof(LowCore, program_new_psw));
2011 r = EXCP_HALTED;
2012 break;
2013 case ICPT_EXT_INT:
2014 unmanageable_intercept(cpu, "external interrupt",
2015 offsetof(LowCore, external_new_psw));
2016 r = EXCP_HALTED;
2017 break;
2018 case ICPT_WAITPSW:
2019 /* disabled wait, since enabled wait is handled in kernel */
2020 cpu_synchronize_state(cs);
2021 if (s390_cpu_halt(cpu) == 0) {
2022 if (is_special_wait_psw(cs)) {
2023 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
2024 } else {
2025 qemu_system_guest_panicked(NULL);
2028 r = EXCP_HALTED;
2029 break;
2030 case ICPT_CPU_STOP:
2031 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
2032 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
2034 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
2035 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
2036 true);
2038 cpu->env.sigp_order = 0;
2039 r = EXCP_HALTED;
2040 break;
2041 case ICPT_OPEREXC:
2042 /* check for break points */
2043 r = handle_sw_breakpoint(cpu, run);
2044 if (r == -ENOENT) {
2045 /* Then check for potential pgm check loops */
2046 r = handle_oper_loop(cpu, run);
2047 if (r == 0) {
2048 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
2051 break;
2052 case ICPT_SOFT_INTERCEPT:
2053 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
2054 exit(1);
2055 break;
2056 case ICPT_IO:
2057 fprintf(stderr, "KVM unimplemented icpt IO\n");
2058 exit(1);
2059 break;
2060 default:
2061 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
2062 exit(1);
2063 break;
2066 return r;
2069 static int handle_tsch(S390CPU *cpu)
2071 CPUState *cs = CPU(cpu);
2072 struct kvm_run *run = cs->kvm_run;
2073 int ret;
2075 cpu_synchronize_state(cs);
2077 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
2078 if (ret < 0) {
2080 * Failure.
2081 * If an I/O interrupt had been dequeued, we have to reinject it.
2083 if (run->s390_tsch.dequeued) {
2084 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
2085 run->s390_tsch.subchannel_nr,
2086 run->s390_tsch.io_int_parm,
2087 run->s390_tsch.io_int_word);
2089 ret = 0;
2091 return ret;
2094 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
2096 struct sysib_322 sysib;
2097 int del;
2099 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
2100 return;
2102 /* Shift the stack of Extended Names to prepare for our own data */
2103 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
2104 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
2105 /* First virt level, that doesn't provide Ext Names delimits stack. It is
2106 * assumed it's not capable of managing Extended Names for lower levels.
2108 for (del = 1; del < sysib.count; del++) {
2109 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
2110 break;
2113 if (del < sysib.count) {
2114 memset(sysib.ext_names[del], 0,
2115 sizeof(sysib.ext_names[0]) * (sysib.count - del));
2117 /* Insert short machine name in EBCDIC, padded with blanks */
2118 if (qemu_name) {
2119 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
2120 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
2121 strlen(qemu_name)));
2123 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
2124 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
2125 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
2126 * considered by s390 as not capable of providing any Extended Name.
2127 * Therefore if no name was specified on qemu invocation, we go with the
2128 * same "KVMguest" default, which KVM has filled into short name field.
2130 if (qemu_name) {
2131 strncpy((char *)sysib.ext_names[0], qemu_name,
2132 sizeof(sysib.ext_names[0]));
2133 } else {
2134 strcpy((char *)sysib.ext_names[0], "KVMguest");
2136 /* Insert UUID */
2137 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
2139 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
2142 static int handle_stsi(S390CPU *cpu)
2144 CPUState *cs = CPU(cpu);
2145 struct kvm_run *run = cs->kvm_run;
2147 switch (run->s390_stsi.fc) {
2148 case 3:
2149 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
2150 return 0;
2152 /* Only sysib 3.2.2 needs post-handling for now. */
2153 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
2154 return 0;
2155 default:
2156 return 0;
2160 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
2162 CPUState *cs = CPU(cpu);
2163 struct kvm_run *run = cs->kvm_run;
2165 int ret = 0;
2166 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
2168 switch (arch_info->type) {
2169 case KVM_HW_WP_WRITE:
2170 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
2171 cs->watchpoint_hit = &hw_watchpoint;
2172 hw_watchpoint.vaddr = arch_info->addr;
2173 hw_watchpoint.flags = BP_MEM_WRITE;
2174 ret = EXCP_DEBUG;
2176 break;
2177 case KVM_HW_BP:
2178 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
2179 ret = EXCP_DEBUG;
2181 break;
2182 case KVM_SINGLESTEP:
2183 if (cs->singlestep_enabled) {
2184 ret = EXCP_DEBUG;
2186 break;
2187 default:
2188 ret = -ENOSYS;
2191 return ret;
2194 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
2196 S390CPU *cpu = S390_CPU(cs);
2197 int ret = 0;
2199 qemu_mutex_lock_iothread();
2201 switch (run->exit_reason) {
2202 case KVM_EXIT_S390_SIEIC:
2203 ret = handle_intercept(cpu);
2204 break;
2205 case KVM_EXIT_S390_RESET:
2206 s390_reipl_request();
2207 break;
2208 case KVM_EXIT_S390_TSCH:
2209 ret = handle_tsch(cpu);
2210 break;
2211 case KVM_EXIT_S390_STSI:
2212 ret = handle_stsi(cpu);
2213 break;
2214 case KVM_EXIT_DEBUG:
2215 ret = kvm_arch_handle_debug_exit(cpu);
2216 break;
2217 default:
2218 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2219 break;
2221 qemu_mutex_unlock_iothread();
2223 if (ret == 0) {
2224 ret = EXCP_INTERRUPT;
2226 return ret;
2229 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2231 return true;
2234 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2235 uint16_t subchannel_nr, uint32_t io_int_parm,
2236 uint32_t io_int_word)
2238 struct kvm_s390_irq irq = {
2239 .u.io.subchannel_id = subchannel_id,
2240 .u.io.subchannel_nr = subchannel_nr,
2241 .u.io.io_int_parm = io_int_parm,
2242 .u.io.io_int_word = io_int_word,
2245 if (io_int_word & IO_INT_WORD_AI) {
2246 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2247 } else {
2248 irq.type = KVM_S390_INT_IO(0, (subchannel_id & 0xff00) >> 8,
2249 (subchannel_id & 0x0006),
2250 subchannel_nr);
2252 kvm_s390_floating_interrupt(&irq);
2255 static uint64_t build_channel_report_mcic(void)
2257 uint64_t mcic;
2259 /* subclass: indicate channel report pending */
2260 mcic = MCIC_SC_CP |
2261 /* subclass modifiers: none */
2262 /* storage errors: none */
2263 /* validity bits: no damage */
2264 MCIC_VB_WP | MCIC_VB_MS | MCIC_VB_PM | MCIC_VB_IA | MCIC_VB_FP |
2265 MCIC_VB_GR | MCIC_VB_CR | MCIC_VB_ST | MCIC_VB_AR | MCIC_VB_PR |
2266 MCIC_VB_FC | MCIC_VB_CT | MCIC_VB_CC;
2267 if (s390_has_feat(S390_FEAT_VECTOR)) {
2268 mcic |= MCIC_VB_VR;
2270 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE)) {
2271 mcic |= MCIC_VB_GS;
2273 return mcic;
2276 void kvm_s390_crw_mchk(void)
2278 struct kvm_s390_irq irq = {
2279 .type = KVM_S390_MCHK,
2280 .u.mchk.cr14 = 1 << 28,
2281 .u.mchk.mcic = build_channel_report_mcic(),
2283 kvm_s390_floating_interrupt(&irq);
2286 void kvm_s390_enable_css_support(S390CPU *cpu)
2288 int r;
2290 /* Activate host kernel channel subsystem support. */
2291 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2292 assert(r == 0);
2295 void kvm_arch_init_irq_routing(KVMState *s)
2298 * Note that while irqchip capabilities generally imply that cpustates
2299 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2300 * have to override the common code kvm_halt_in_kernel_allowed setting.
2302 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2303 kvm_gsi_routing_allowed = true;
2304 kvm_halt_in_kernel_allowed = false;
2308 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2309 int vq, bool assign)
2311 struct kvm_ioeventfd kick = {
2312 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2313 KVM_IOEVENTFD_FLAG_DATAMATCH,
2314 .fd = event_notifier_get_fd(notifier),
2315 .datamatch = vq,
2316 .addr = sch,
2317 .len = 8,
2319 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2320 return -ENOSYS;
2322 if (!assign) {
2323 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2325 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2328 int kvm_s390_get_memslot_count(void)
2330 return kvm_check_extension(kvm_state, KVM_CAP_NR_MEMSLOTS);
2333 int kvm_s390_get_ri(void)
2335 return cap_ri;
2338 int kvm_s390_get_gs(void)
2340 return cap_gs;
2343 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2345 struct kvm_mp_state mp_state = {};
2346 int ret;
2348 /* the kvm part might not have been initialized yet */
2349 if (CPU(cpu)->kvm_state == NULL) {
2350 return 0;
2353 switch (cpu_state) {
2354 case CPU_STATE_STOPPED:
2355 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2356 break;
2357 case CPU_STATE_CHECK_STOP:
2358 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2359 break;
2360 case CPU_STATE_OPERATING:
2361 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2362 break;
2363 case CPU_STATE_LOAD:
2364 mp_state.mp_state = KVM_MP_STATE_LOAD;
2365 break;
2366 default:
2367 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2368 cpu_state);
2369 exit(1);
2372 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2373 if (ret) {
2374 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2375 strerror(-ret));
2378 return ret;
2381 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2383 struct kvm_s390_irq_state irq_state;
2384 CPUState *cs = CPU(cpu);
2385 int32_t bytes;
2387 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2388 return;
2391 irq_state.buf = (uint64_t) cpu->irqstate;
2392 irq_state.len = VCPU_IRQ_BUF_SIZE;
2394 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2395 if (bytes < 0) {
2396 cpu->irqstate_saved_size = 0;
2397 error_report("Migration of interrupt state failed");
2398 return;
2401 cpu->irqstate_saved_size = bytes;
2404 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2406 CPUState *cs = CPU(cpu);
2407 struct kvm_s390_irq_state irq_state;
2408 int r;
2410 if (cpu->irqstate_saved_size == 0) {
2411 return 0;
2414 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2415 return -ENOSYS;
2418 irq_state.buf = (uint64_t) cpu->irqstate;
2419 irq_state.len = cpu->irqstate_saved_size;
2421 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2422 if (r) {
2423 error_report("Setting interrupt state failed %d", r);
2425 return r;
2428 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2429 uint64_t address, uint32_t data, PCIDevice *dev)
2431 S390PCIBusDevice *pbdev;
2432 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2434 if (!dev) {
2435 DPRINTF("add_msi_route no pci device\n");
2436 return -ENODEV;
2439 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
2440 if (!pbdev) {
2441 DPRINTF("add_msi_route no zpci device\n");
2442 return -ENODEV;
2445 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2446 route->flags = 0;
2447 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2448 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2449 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2450 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
2451 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2452 return 0;
2455 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2456 int vector, PCIDevice *dev)
2458 return 0;
2461 int kvm_arch_release_virq_post(int virq)
2463 return 0;
2466 int kvm_arch_msi_data_to_gsi(uint32_t data)
2468 abort();
2471 static int query_cpu_subfunc(S390FeatBitmap features)
2473 struct kvm_s390_vm_cpu_subfunc prop;
2474 struct kvm_device_attr attr = {
2475 .group = KVM_S390_VM_CPU_MODEL,
2476 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2477 .addr = (uint64_t) &prop,
2479 int rc;
2481 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2482 if (rc) {
2483 return rc;
2487 * We're going to add all subfunctions now, if the corresponding feature
2488 * is available that unlocks the query functions.
2490 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2491 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2492 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2494 if (test_bit(S390_FEAT_MSA, features)) {
2495 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2496 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2497 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2498 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2499 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2501 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2502 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2504 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2505 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2506 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2507 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2508 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2510 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2511 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2513 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2514 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2516 return 0;
2519 static int configure_cpu_subfunc(const S390FeatBitmap features)
2521 struct kvm_s390_vm_cpu_subfunc prop = {};
2522 struct kvm_device_attr attr = {
2523 .group = KVM_S390_VM_CPU_MODEL,
2524 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2525 .addr = (uint64_t) &prop,
2528 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2529 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2530 /* hardware support might be missing, IBC will handle most of this */
2531 return 0;
2534 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2535 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2536 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2538 if (test_bit(S390_FEAT_MSA, features)) {
2539 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2540 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2541 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2542 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2543 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2545 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2546 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2548 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2549 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2550 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2551 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2552 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2554 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2555 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2557 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2558 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2560 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2563 static int kvm_to_feat[][2] = {
2564 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2565 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2566 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2567 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2568 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2569 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2570 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2571 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2572 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2573 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2574 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2575 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2576 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2577 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2580 static int query_cpu_feat(S390FeatBitmap features)
2582 struct kvm_s390_vm_cpu_feat prop;
2583 struct kvm_device_attr attr = {
2584 .group = KVM_S390_VM_CPU_MODEL,
2585 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2586 .addr = (uint64_t) &prop,
2588 int rc;
2589 int i;
2591 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2592 if (rc) {
2593 return rc;
2596 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2597 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2598 set_bit(kvm_to_feat[i][1], features);
2601 return 0;
2604 static int configure_cpu_feat(const S390FeatBitmap features)
2606 struct kvm_s390_vm_cpu_feat prop = {};
2607 struct kvm_device_attr attr = {
2608 .group = KVM_S390_VM_CPU_MODEL,
2609 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2610 .addr = (uint64_t) &prop,
2612 int i;
2614 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2615 if (test_bit(kvm_to_feat[i][1], features)) {
2616 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2619 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2622 bool kvm_s390_cpu_models_supported(void)
2624 if (!cpu_model_allowed()) {
2625 /* compatibility machines interfere with the cpu model */
2626 return false;
2628 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2629 KVM_S390_VM_CPU_MACHINE) &&
2630 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2631 KVM_S390_VM_CPU_PROCESSOR) &&
2632 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2633 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2634 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2635 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2636 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2637 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2640 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2642 struct kvm_s390_vm_cpu_machine prop = {};
2643 struct kvm_device_attr attr = {
2644 .group = KVM_S390_VM_CPU_MODEL,
2645 .attr = KVM_S390_VM_CPU_MACHINE,
2646 .addr = (uint64_t) &prop,
2648 uint16_t unblocked_ibc = 0, cpu_type = 0;
2649 int rc;
2651 memset(model, 0, sizeof(*model));
2653 if (!kvm_s390_cpu_models_supported()) {
2654 error_setg(errp, "KVM doesn't support CPU models");
2655 return;
2658 /* query the basic cpu model properties */
2659 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2660 if (rc) {
2661 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2662 return;
2665 cpu_type = cpuid_type(prop.cpuid);
2666 if (has_ibc(prop.ibc)) {
2667 model->lowest_ibc = lowest_ibc(prop.ibc);
2668 unblocked_ibc = unblocked_ibc(prop.ibc);
2670 model->cpu_id = cpuid_id(prop.cpuid);
2671 model->cpu_id_format = cpuid_format(prop.cpuid);
2672 model->cpu_ver = 0xff;
2674 /* get supported cpu features indicated via STFL(E) */
2675 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2676 (uint8_t *) prop.fac_mask);
2677 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2678 if (test_bit(S390_FEAT_STFLE, model->features)) {
2679 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2681 /* get supported cpu features indicated e.g. via SCLP */
2682 rc = query_cpu_feat(model->features);
2683 if (rc) {
2684 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2685 return;
2687 /* get supported cpu subfunctions indicated via query / test bit */
2688 rc = query_cpu_subfunc(model->features);
2689 if (rc) {
2690 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2691 return;
2694 /* with cpu model support, CMM is only indicated if really available */
2695 if (kvm_s390_cmma_available()) {
2696 set_bit(S390_FEAT_CMM, model->features);
2697 } else {
2698 /* no cmm -> no cmm nt */
2699 clear_bit(S390_FEAT_CMM_NT, model->features);
2702 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2703 if (pci_available) {
2704 set_bit(S390_FEAT_ZPCI, model->features);
2706 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2708 if (s390_known_cpu_type(cpu_type)) {
2709 /* we want the exact model, even if some features are missing */
2710 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2711 ibc_ec_ga(unblocked_ibc), NULL);
2712 } else {
2713 /* model unknown, e.g. too new - search using features */
2714 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2715 ibc_ec_ga(unblocked_ibc),
2716 model->features);
2718 if (!model->def) {
2719 error_setg(errp, "KVM: host CPU model could not be identified");
2720 return;
2722 /* strip of features that are not part of the maximum model */
2723 bitmap_and(model->features, model->features, model->def->full_feat,
2724 S390_FEAT_MAX);
2727 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2729 struct kvm_s390_vm_cpu_processor prop = {
2730 .fac_list = { 0 },
2732 struct kvm_device_attr attr = {
2733 .group = KVM_S390_VM_CPU_MODEL,
2734 .attr = KVM_S390_VM_CPU_PROCESSOR,
2735 .addr = (uint64_t) &prop,
2737 int rc;
2739 if (!model) {
2740 /* compatibility handling if cpu models are disabled */
2741 if (kvm_s390_cmma_available()) {
2742 kvm_s390_enable_cmma();
2744 return;
2746 if (!kvm_s390_cpu_models_supported()) {
2747 error_setg(errp, "KVM doesn't support CPU models");
2748 return;
2750 prop.cpuid = s390_cpuid_from_cpu_model(model);
2751 prop.ibc = s390_ibc_from_cpu_model(model);
2752 /* configure cpu features indicated via STFL(e) */
2753 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2754 (uint8_t *) prop.fac_list);
2755 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2756 if (rc) {
2757 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2758 return;
2760 /* configure cpu features indicated e.g. via SCLP */
2761 rc = configure_cpu_feat(model->features);
2762 if (rc) {
2763 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2764 return;
2766 /* configure cpu subfunctions indicated via query / test bit */
2767 rc = configure_cpu_subfunc(model->features);
2768 if (rc) {
2769 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2770 return;
2772 /* enable CMM via CMMA */
2773 if (test_bit(S390_FEAT_CMM, model->features)) {
2774 kvm_s390_enable_cmma();