mips: MIPSCPU model subclasses
[qemu.git] / target / s390x / kvm.c
blob720cb1d83385cef8f7f32b17ae564ea7353e4098
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;
314 /* Try to enable AIS facility */
315 kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0);
317 qemu_mutex_init(&qemu_sigp_mutex);
319 return 0;
322 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
324 return 0;
327 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
329 return cpu->cpu_index;
332 int kvm_arch_init_vcpu(CPUState *cs)
334 S390CPU *cpu = S390_CPU(cs);
335 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
336 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
337 return 0;
340 void kvm_s390_reset_vcpu(S390CPU *cpu)
342 CPUState *cs = CPU(cpu);
344 /* The initial reset call is needed here to reset in-kernel
345 * vcpu data that we can't access directly from QEMU
346 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
347 * Before this ioctl cpu_synchronize_state() is called in common kvm
348 * code (kvm-all) */
349 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
350 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
354 static int can_sync_regs(CPUState *cs, int regs)
356 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
359 int kvm_arch_put_registers(CPUState *cs, int level)
361 S390CPU *cpu = S390_CPU(cs);
362 CPUS390XState *env = &cpu->env;
363 struct kvm_sregs sregs;
364 struct kvm_regs regs;
365 struct kvm_fpu fpu = {};
366 int r;
367 int i;
369 /* always save the PSW and the GPRS*/
370 cs->kvm_run->psw_addr = env->psw.addr;
371 cs->kvm_run->psw_mask = env->psw.mask;
373 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
374 for (i = 0; i < 16; i++) {
375 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
376 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
378 } else {
379 for (i = 0; i < 16; i++) {
380 regs.gprs[i] = env->regs[i];
382 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
383 if (r < 0) {
384 return r;
388 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
389 for (i = 0; i < 32; i++) {
390 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
391 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
393 cs->kvm_run->s.regs.fpc = env->fpc;
394 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
395 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
396 for (i = 0; i < 16; i++) {
397 cs->kvm_run->s.regs.fprs[i] = get_freg(env, i)->ll;
399 cs->kvm_run->s.regs.fpc = env->fpc;
400 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
401 } else {
402 /* Floating point */
403 for (i = 0; i < 16; i++) {
404 fpu.fprs[i] = get_freg(env, i)->ll;
406 fpu.fpc = env->fpc;
408 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
409 if (r < 0) {
410 return r;
414 /* Do we need to save more than that? */
415 if (level == KVM_PUT_RUNTIME_STATE) {
416 return 0;
419 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
420 cs->kvm_run->s.regs.cputm = env->cputm;
421 cs->kvm_run->s.regs.ckc = env->ckc;
422 cs->kvm_run->s.regs.todpr = env->todpr;
423 cs->kvm_run->s.regs.gbea = env->gbea;
424 cs->kvm_run->s.regs.pp = env->pp;
425 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
426 } else {
428 * These ONE_REGS are not protected by a capability. As they are only
429 * necessary for migration we just trace a possible error, but don't
430 * return with an error return code.
432 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
433 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
434 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
435 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
436 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
439 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
440 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
441 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
444 /* pfault parameters */
445 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
446 cs->kvm_run->s.regs.pft = env->pfault_token;
447 cs->kvm_run->s.regs.pfs = env->pfault_select;
448 cs->kvm_run->s.regs.pfc = env->pfault_compare;
449 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
450 } else if (cap_async_pf) {
451 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
452 if (r < 0) {
453 return r;
455 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
456 if (r < 0) {
457 return r;
459 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
460 if (r < 0) {
461 return r;
465 /* access registers and control registers*/
466 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
467 for (i = 0; i < 16; i++) {
468 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
469 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
471 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
472 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
473 } else {
474 for (i = 0; i < 16; i++) {
475 sregs.acrs[i] = env->aregs[i];
476 sregs.crs[i] = env->cregs[i];
478 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
479 if (r < 0) {
480 return r;
484 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
485 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
486 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
489 /* Finally the prefix */
490 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
491 cs->kvm_run->s.regs.prefix = env->psa;
492 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
493 } else {
494 /* prefix is only supported via sync regs */
496 return 0;
499 int kvm_arch_get_registers(CPUState *cs)
501 S390CPU *cpu = S390_CPU(cs);
502 CPUS390XState *env = &cpu->env;
503 struct kvm_sregs sregs;
504 struct kvm_regs regs;
505 struct kvm_fpu fpu;
506 int i, r;
508 /* get the PSW */
509 env->psw.addr = cs->kvm_run->psw_addr;
510 env->psw.mask = cs->kvm_run->psw_mask;
512 /* the GPRS */
513 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
514 for (i = 0; i < 16; i++) {
515 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
517 } else {
518 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
519 if (r < 0) {
520 return r;
522 for (i = 0; i < 16; i++) {
523 env->regs[i] = regs.gprs[i];
527 /* The ACRS and CRS */
528 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
529 for (i = 0; i < 16; i++) {
530 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
531 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
533 } else {
534 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
535 if (r < 0) {
536 return r;
538 for (i = 0; i < 16; i++) {
539 env->aregs[i] = sregs.acrs[i];
540 env->cregs[i] = sregs.crs[i];
544 /* Floating point and vector registers */
545 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
546 for (i = 0; i < 32; i++) {
547 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
548 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
550 env->fpc = cs->kvm_run->s.regs.fpc;
551 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
552 for (i = 0; i < 16; i++) {
553 get_freg(env, i)->ll = cs->kvm_run->s.regs.fprs[i];
555 env->fpc = cs->kvm_run->s.regs.fpc;
556 } else {
557 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
558 if (r < 0) {
559 return r;
561 for (i = 0; i < 16; i++) {
562 get_freg(env, i)->ll = fpu.fprs[i];
564 env->fpc = fpu.fpc;
567 /* The prefix */
568 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
569 env->psa = cs->kvm_run->s.regs.prefix;
572 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
573 env->cputm = cs->kvm_run->s.regs.cputm;
574 env->ckc = cs->kvm_run->s.regs.ckc;
575 env->todpr = cs->kvm_run->s.regs.todpr;
576 env->gbea = cs->kvm_run->s.regs.gbea;
577 env->pp = cs->kvm_run->s.regs.pp;
578 } else {
580 * These ONE_REGS are not protected by a capability. As they are only
581 * necessary for migration we just trace a possible error, but don't
582 * return with an error return code.
584 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
585 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
586 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
587 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
588 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
591 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
592 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
595 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
596 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
599 /* pfault parameters */
600 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
601 env->pfault_token = cs->kvm_run->s.regs.pft;
602 env->pfault_select = cs->kvm_run->s.regs.pfs;
603 env->pfault_compare = cs->kvm_run->s.regs.pfc;
604 } else if (cap_async_pf) {
605 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
606 if (r < 0) {
607 return r;
609 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
610 if (r < 0) {
611 return r;
613 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
614 if (r < 0) {
615 return r;
619 return 0;
622 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
624 int r;
625 struct kvm_device_attr attr = {
626 .group = KVM_S390_VM_TOD,
627 .attr = KVM_S390_VM_TOD_LOW,
628 .addr = (uint64_t)tod_low,
631 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
632 if (r) {
633 return r;
636 attr.attr = KVM_S390_VM_TOD_HIGH;
637 attr.addr = (uint64_t)tod_high;
638 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
641 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
643 int r;
645 struct kvm_device_attr attr = {
646 .group = KVM_S390_VM_TOD,
647 .attr = KVM_S390_VM_TOD_LOW,
648 .addr = (uint64_t)tod_low,
651 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
652 if (r) {
653 return r;
656 attr.attr = KVM_S390_VM_TOD_HIGH;
657 attr.addr = (uint64_t)tod_high;
658 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
662 * kvm_s390_mem_op:
663 * @addr: the logical start address in guest memory
664 * @ar: the access register number
665 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
666 * @len: length that should be transferred
667 * @is_write: true = write, false = read
668 * Returns: 0 on success, non-zero if an exception or error occurred
670 * Use KVM ioctl to read/write from/to guest memory. An access exception
671 * is injected into the vCPU in case of translation errors.
673 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
674 int len, bool is_write)
676 struct kvm_s390_mem_op mem_op = {
677 .gaddr = addr,
678 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
679 .size = len,
680 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
681 : KVM_S390_MEMOP_LOGICAL_READ,
682 .buf = (uint64_t)hostbuf,
683 .ar = ar,
685 int ret;
687 if (!cap_mem_op) {
688 return -ENOSYS;
690 if (!hostbuf) {
691 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
694 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
695 if (ret < 0) {
696 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
698 return ret;
702 * Legacy layout for s390:
703 * Older S390 KVM requires the topmost vma of the RAM to be
704 * smaller than an system defined value, which is at least 256GB.
705 * Larger systems have larger values. We put the guest between
706 * the end of data segment (system break) and this value. We
707 * use 32GB as a base to have enough room for the system break
708 * to grow. We also have to use MAP parameters that avoid
709 * read-only mapping of guest pages.
711 static void *legacy_s390_alloc(size_t size, uint64_t *align)
713 void *mem;
715 mem = mmap((void *) 0x800000000ULL, size,
716 PROT_EXEC|PROT_READ|PROT_WRITE,
717 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
718 return mem == MAP_FAILED ? NULL : mem;
721 static uint8_t const *sw_bp_inst;
722 static uint8_t sw_bp_ilen;
724 static void determine_sw_breakpoint_instr(void)
726 /* DIAG 501 is used for sw breakpoints with old kernels */
727 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
728 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
729 static const uint8_t instr_0x0000[] = {0x00, 0x00};
731 if (sw_bp_inst) {
732 return;
734 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
735 sw_bp_inst = diag_501;
736 sw_bp_ilen = sizeof(diag_501);
737 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
738 } else {
739 sw_bp_inst = instr_0x0000;
740 sw_bp_ilen = sizeof(instr_0x0000);
741 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
745 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
747 determine_sw_breakpoint_instr();
749 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
750 sw_bp_ilen, 0) ||
751 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
752 return -EINVAL;
754 return 0;
757 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
759 uint8_t t[MAX_ILEN];
761 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
762 return -EINVAL;
763 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
764 return -EINVAL;
765 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
766 sw_bp_ilen, 1)) {
767 return -EINVAL;
770 return 0;
773 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
774 int len, int type)
776 int n;
778 for (n = 0; n < nb_hw_breakpoints; n++) {
779 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
780 (hw_breakpoints[n].len == len || len == -1)) {
781 return &hw_breakpoints[n];
785 return NULL;
788 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
790 int size;
792 if (find_hw_breakpoint(addr, len, type)) {
793 return -EEXIST;
796 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
798 if (!hw_breakpoints) {
799 nb_hw_breakpoints = 0;
800 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
801 } else {
802 hw_breakpoints =
803 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
806 if (!hw_breakpoints) {
807 nb_hw_breakpoints = 0;
808 return -ENOMEM;
811 hw_breakpoints[nb_hw_breakpoints].addr = addr;
812 hw_breakpoints[nb_hw_breakpoints].len = len;
813 hw_breakpoints[nb_hw_breakpoints].type = type;
815 nb_hw_breakpoints++;
817 return 0;
820 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
821 target_ulong len, int type)
823 switch (type) {
824 case GDB_BREAKPOINT_HW:
825 type = KVM_HW_BP;
826 break;
827 case GDB_WATCHPOINT_WRITE:
828 if (len < 1) {
829 return -EINVAL;
831 type = KVM_HW_WP_WRITE;
832 break;
833 default:
834 return -ENOSYS;
836 return insert_hw_breakpoint(addr, len, type);
839 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
840 target_ulong len, int type)
842 int size;
843 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
845 if (bp == NULL) {
846 return -ENOENT;
849 nb_hw_breakpoints--;
850 if (nb_hw_breakpoints > 0) {
852 * In order to trim the array, move the last element to the position to
853 * be removed - if necessary.
855 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
856 *bp = hw_breakpoints[nb_hw_breakpoints];
858 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
859 hw_breakpoints =
860 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
861 } else {
862 g_free(hw_breakpoints);
863 hw_breakpoints = NULL;
866 return 0;
869 void kvm_arch_remove_all_hw_breakpoints(void)
871 nb_hw_breakpoints = 0;
872 g_free(hw_breakpoints);
873 hw_breakpoints = NULL;
876 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
878 int i;
880 if (nb_hw_breakpoints > 0) {
881 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
882 dbg->arch.hw_bp = hw_breakpoints;
884 for (i = 0; i < nb_hw_breakpoints; ++i) {
885 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
886 hw_breakpoints[i].addr);
888 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
889 } else {
890 dbg->arch.nr_hw_bp = 0;
891 dbg->arch.hw_bp = NULL;
895 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
899 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
901 return MEMTXATTRS_UNSPECIFIED;
904 int kvm_arch_process_async_events(CPUState *cs)
906 return cs->halted;
909 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
910 struct kvm_s390_interrupt *interrupt)
912 int r = 0;
914 interrupt->type = irq->type;
915 switch (irq->type) {
916 case KVM_S390_INT_VIRTIO:
917 interrupt->parm = irq->u.ext.ext_params;
918 /* fall through */
919 case KVM_S390_INT_PFAULT_INIT:
920 case KVM_S390_INT_PFAULT_DONE:
921 interrupt->parm64 = irq->u.ext.ext_params2;
922 break;
923 case KVM_S390_PROGRAM_INT:
924 interrupt->parm = irq->u.pgm.code;
925 break;
926 case KVM_S390_SIGP_SET_PREFIX:
927 interrupt->parm = irq->u.prefix.address;
928 break;
929 case KVM_S390_INT_SERVICE:
930 interrupt->parm = irq->u.ext.ext_params;
931 break;
932 case KVM_S390_MCHK:
933 interrupt->parm = irq->u.mchk.cr14;
934 interrupt->parm64 = irq->u.mchk.mcic;
935 break;
936 case KVM_S390_INT_EXTERNAL_CALL:
937 interrupt->parm = irq->u.extcall.code;
938 break;
939 case KVM_S390_INT_EMERGENCY:
940 interrupt->parm = irq->u.emerg.code;
941 break;
942 case KVM_S390_SIGP_STOP:
943 case KVM_S390_RESTART:
944 break; /* These types have no parameters */
945 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
946 interrupt->parm = irq->u.io.subchannel_id << 16;
947 interrupt->parm |= irq->u.io.subchannel_nr;
948 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
949 interrupt->parm64 |= irq->u.io.io_int_word;
950 break;
951 default:
952 r = -EINVAL;
953 break;
955 return r;
958 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
960 struct kvm_s390_interrupt kvmint = {};
961 int r;
963 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
964 if (r < 0) {
965 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
966 exit(1);
969 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
970 if (r < 0) {
971 fprintf(stderr, "KVM failed to inject interrupt\n");
972 exit(1);
976 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
978 CPUState *cs = CPU(cpu);
979 int r;
981 if (cap_s390_irq) {
982 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
983 if (!r) {
984 return;
986 error_report("KVM failed to inject interrupt %llx", irq->type);
987 exit(1);
990 inject_vcpu_irq_legacy(cs, irq);
993 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
995 struct kvm_s390_interrupt kvmint = {};
996 int r;
998 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
999 if (r < 0) {
1000 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1001 exit(1);
1004 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1005 if (r < 0) {
1006 fprintf(stderr, "KVM failed to inject interrupt\n");
1007 exit(1);
1011 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
1013 static bool use_flic = true;
1014 int r;
1016 if (use_flic) {
1017 r = kvm_s390_inject_flic(irq);
1018 if (r == -ENOSYS) {
1019 use_flic = false;
1021 if (!r) {
1022 return;
1025 __kvm_s390_floating_interrupt(irq);
1028 void kvm_s390_service_interrupt(uint32_t parm)
1030 struct kvm_s390_irq irq = {
1031 .type = KVM_S390_INT_SERVICE,
1032 .u.ext.ext_params = parm,
1035 kvm_s390_floating_interrupt(&irq);
1038 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1040 struct kvm_s390_irq irq = {
1041 .type = KVM_S390_PROGRAM_INT,
1042 .u.pgm.code = code,
1045 kvm_s390_vcpu_interrupt(cpu, &irq);
1048 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1050 struct kvm_s390_irq irq = {
1051 .type = KVM_S390_PROGRAM_INT,
1052 .u.pgm.code = code,
1053 .u.pgm.trans_exc_code = te_code,
1054 .u.pgm.exc_access_id = te_code & 3,
1057 kvm_s390_vcpu_interrupt(cpu, &irq);
1060 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1061 uint16_t ipbh0)
1063 CPUS390XState *env = &cpu->env;
1064 uint64_t sccb;
1065 uint32_t code;
1066 int r = 0;
1068 cpu_synchronize_state(CPU(cpu));
1069 sccb = env->regs[ipbh0 & 0xf];
1070 code = env->regs[(ipbh0 & 0xf0) >> 4];
1072 r = sclp_service_call(env, sccb, code);
1073 if (r < 0) {
1074 kvm_s390_program_interrupt(cpu, -r);
1075 } else {
1076 setcc(cpu, r);
1079 return 0;
1082 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1084 CPUS390XState *env = &cpu->env;
1085 int rc = 0;
1086 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1088 cpu_synchronize_state(CPU(cpu));
1090 switch (ipa1) {
1091 case PRIV_B2_XSCH:
1092 ioinst_handle_xsch(cpu, env->regs[1]);
1093 break;
1094 case PRIV_B2_CSCH:
1095 ioinst_handle_csch(cpu, env->regs[1]);
1096 break;
1097 case PRIV_B2_HSCH:
1098 ioinst_handle_hsch(cpu, env->regs[1]);
1099 break;
1100 case PRIV_B2_MSCH:
1101 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1102 break;
1103 case PRIV_B2_SSCH:
1104 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1105 break;
1106 case PRIV_B2_STCRW:
1107 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1108 break;
1109 case PRIV_B2_STSCH:
1110 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1111 break;
1112 case PRIV_B2_TSCH:
1113 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1114 fprintf(stderr, "Spurious tsch intercept\n");
1115 break;
1116 case PRIV_B2_CHSC:
1117 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1118 break;
1119 case PRIV_B2_TPI:
1120 /* This should have been handled by kvm already. */
1121 fprintf(stderr, "Spurious tpi intercept\n");
1122 break;
1123 case PRIV_B2_SCHM:
1124 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1125 run->s390_sieic.ipb);
1126 break;
1127 case PRIV_B2_RSCH:
1128 ioinst_handle_rsch(cpu, env->regs[1]);
1129 break;
1130 case PRIV_B2_RCHP:
1131 ioinst_handle_rchp(cpu, env->regs[1]);
1132 break;
1133 case PRIV_B2_STCPS:
1134 /* We do not provide this instruction, it is suppressed. */
1135 break;
1136 case PRIV_B2_SAL:
1137 ioinst_handle_sal(cpu, env->regs[1]);
1138 break;
1139 case PRIV_B2_SIGA:
1140 /* Not provided, set CC = 3 for subchannel not operational */
1141 setcc(cpu, 3);
1142 break;
1143 case PRIV_B2_SCLP_CALL:
1144 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1145 break;
1146 default:
1147 rc = -1;
1148 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1149 break;
1152 return rc;
1155 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1156 uint8_t *ar)
1158 CPUS390XState *env = &cpu->env;
1159 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1160 uint32_t base2 = run->s390_sieic.ipb >> 28;
1161 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1162 ((run->s390_sieic.ipb & 0xff00) << 4);
1164 if (disp2 & 0x80000) {
1165 disp2 += 0xfff00000;
1167 if (ar) {
1168 *ar = base2;
1171 return (base2 ? env->regs[base2] : 0) +
1172 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1175 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1176 uint8_t *ar)
1178 CPUS390XState *env = &cpu->env;
1179 uint32_t base2 = run->s390_sieic.ipb >> 28;
1180 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1181 ((run->s390_sieic.ipb & 0xff00) << 4);
1183 if (disp2 & 0x80000) {
1184 disp2 += 0xfff00000;
1186 if (ar) {
1187 *ar = base2;
1190 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1193 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1195 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1197 if (s390_has_feat(S390_FEAT_ZPCI)) {
1198 return clp_service_call(cpu, r2);
1199 } else {
1200 return -1;
1204 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1206 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1207 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1209 if (s390_has_feat(S390_FEAT_ZPCI)) {
1210 return pcilg_service_call(cpu, r1, r2);
1211 } else {
1212 return -1;
1216 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1218 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1219 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1221 if (s390_has_feat(S390_FEAT_ZPCI)) {
1222 return pcistg_service_call(cpu, r1, r2);
1223 } else {
1224 return -1;
1228 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1230 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1231 uint64_t fiba;
1232 uint8_t ar;
1234 if (s390_has_feat(S390_FEAT_ZPCI)) {
1235 cpu_synchronize_state(CPU(cpu));
1236 fiba = get_base_disp_rxy(cpu, run, &ar);
1238 return stpcifc_service_call(cpu, r1, fiba, ar);
1239 } else {
1240 return -1;
1244 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1246 CPUS390XState *env = &cpu->env;
1247 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1248 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1249 uint8_t isc;
1250 uint16_t mode;
1251 int r;
1253 cpu_synchronize_state(CPU(cpu));
1254 mode = env->regs[r1] & 0xffff;
1255 isc = (env->regs[r3] >> 27) & 0x7;
1256 r = css_do_sic(env, isc, mode);
1257 if (r) {
1258 kvm_s390_program_interrupt(cpu, -r);
1261 return 0;
1264 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1266 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1267 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1269 if (s390_has_feat(S390_FEAT_ZPCI)) {
1270 return rpcit_service_call(cpu, r1, r2);
1271 } else {
1272 return -1;
1276 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1278 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1279 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1280 uint64_t gaddr;
1281 uint8_t ar;
1283 if (s390_has_feat(S390_FEAT_ZPCI)) {
1284 cpu_synchronize_state(CPU(cpu));
1285 gaddr = get_base_disp_rsy(cpu, run, &ar);
1287 return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1288 } else {
1289 return -1;
1293 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1295 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1296 uint64_t fiba;
1297 uint8_t ar;
1299 if (s390_has_feat(S390_FEAT_ZPCI)) {
1300 cpu_synchronize_state(CPU(cpu));
1301 fiba = get_base_disp_rxy(cpu, run, &ar);
1303 return mpcifc_service_call(cpu, r1, fiba, ar);
1304 } else {
1305 return -1;
1309 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1311 int r = 0;
1313 switch (ipa1) {
1314 case PRIV_B9_CLP:
1315 r = kvm_clp_service_call(cpu, run);
1316 break;
1317 case PRIV_B9_PCISTG:
1318 r = kvm_pcistg_service_call(cpu, run);
1319 break;
1320 case PRIV_B9_PCILG:
1321 r = kvm_pcilg_service_call(cpu, run);
1322 break;
1323 case PRIV_B9_RPCIT:
1324 r = kvm_rpcit_service_call(cpu, run);
1325 break;
1326 case PRIV_B9_EQBS:
1327 /* just inject exception */
1328 r = -1;
1329 break;
1330 default:
1331 r = -1;
1332 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1333 break;
1336 return r;
1339 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1341 int r = 0;
1343 switch (ipbl) {
1344 case PRIV_EB_PCISTB:
1345 r = kvm_pcistb_service_call(cpu, run);
1346 break;
1347 case PRIV_EB_SIC:
1348 r = kvm_sic_service_call(cpu, run);
1349 break;
1350 case PRIV_EB_SQBS:
1351 /* just inject exception */
1352 r = -1;
1353 break;
1354 default:
1355 r = -1;
1356 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1357 break;
1360 return r;
1363 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1365 int r = 0;
1367 switch (ipbl) {
1368 case PRIV_E3_MPCIFC:
1369 r = kvm_mpcifc_service_call(cpu, run);
1370 break;
1371 case PRIV_E3_STPCIFC:
1372 r = kvm_stpcifc_service_call(cpu, run);
1373 break;
1374 default:
1375 r = -1;
1376 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1377 break;
1380 return r;
1383 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1385 CPUS390XState *env = &cpu->env;
1386 int ret;
1388 cpu_synchronize_state(CPU(cpu));
1389 ret = s390_virtio_hypercall(env);
1390 if (ret == -EINVAL) {
1391 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1392 return 0;
1395 return ret;
1398 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1400 uint64_t r1, r3;
1401 int rc;
1403 cpu_synchronize_state(CPU(cpu));
1404 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1405 r3 = run->s390_sieic.ipa & 0x000f;
1406 rc = handle_diag_288(&cpu->env, r1, r3);
1407 if (rc) {
1408 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1412 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1414 uint64_t r1, r3;
1416 cpu_synchronize_state(CPU(cpu));
1417 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1418 r3 = run->s390_sieic.ipa & 0x000f;
1419 handle_diag_308(&cpu->env, r1, r3);
1422 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1424 CPUS390XState *env = &cpu->env;
1425 unsigned long pc;
1427 cpu_synchronize_state(CPU(cpu));
1429 pc = env->psw.addr - sw_bp_ilen;
1430 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1431 env->psw.addr = pc;
1432 return EXCP_DEBUG;
1435 return -ENOENT;
1438 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1440 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1442 int r = 0;
1443 uint16_t func_code;
1446 * For any diagnose call we support, bits 48-63 of the resulting
1447 * address specify the function code; the remainder is ignored.
1449 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1450 switch (func_code) {
1451 case DIAG_TIMEREVENT:
1452 kvm_handle_diag_288(cpu, run);
1453 break;
1454 case DIAG_IPL:
1455 kvm_handle_diag_308(cpu, run);
1456 break;
1457 case DIAG_KVM_HYPERCALL:
1458 r = handle_hypercall(cpu, run);
1459 break;
1460 case DIAG_KVM_BREAKPOINT:
1461 r = handle_sw_breakpoint(cpu, run);
1462 break;
1463 default:
1464 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1465 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1466 break;
1469 return r;
1472 typedef struct SigpInfo {
1473 uint64_t param;
1474 int cc;
1475 uint64_t *status_reg;
1476 } SigpInfo;
1478 static void set_sigp_status(SigpInfo *si, uint64_t status)
1480 *si->status_reg &= 0xffffffff00000000ULL;
1481 *si->status_reg |= status;
1482 si->cc = SIGP_CC_STATUS_STORED;
1485 static void sigp_start(CPUState *cs, run_on_cpu_data arg)
1487 S390CPU *cpu = S390_CPU(cs);
1488 SigpInfo *si = arg.host_ptr;
1490 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1491 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1492 return;
1495 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1496 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1499 static void sigp_stop(CPUState *cs, run_on_cpu_data arg)
1501 S390CPU *cpu = S390_CPU(cs);
1502 SigpInfo *si = arg.host_ptr;
1503 struct kvm_s390_irq irq = {
1504 .type = KVM_S390_SIGP_STOP,
1507 if (s390_cpu_get_state(cpu) != CPU_STATE_OPERATING) {
1508 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1509 return;
1512 /* disabled wait - sleeping in user space */
1513 if (cs->halted) {
1514 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
1515 } else {
1516 /* execute the stop function */
1517 cpu->env.sigp_order = SIGP_STOP;
1518 kvm_s390_vcpu_interrupt(cpu, &irq);
1520 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1523 #define ADTL_GS_OFFSET 1024 /* offset of GS data in adtl save area */
1524 #define ADTL_GS_MIN_SIZE 2048 /* minimal size of adtl save area for GS */
1525 static int do_store_adtl_status(S390CPU *cpu, hwaddr addr, hwaddr len)
1527 hwaddr save = len;
1528 void *mem;
1530 mem = cpu_physical_memory_map(addr, &save, 1);
1531 if (!mem) {
1532 return -EFAULT;
1534 if (save != len) {
1535 cpu_physical_memory_unmap(mem, len, 1, 0);
1536 return -EFAULT;
1539 if (s390_has_feat(S390_FEAT_VECTOR)) {
1540 memcpy(mem, &cpu->env.vregs, 512);
1542 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE) && len >= ADTL_GS_MIN_SIZE) {
1543 memcpy(mem + ADTL_GS_OFFSET, &cpu->env.gscb, 32);
1546 cpu_physical_memory_unmap(mem, len, 1, len);
1548 return 0;
1551 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1552 #define SAVE_AREA_SIZE 512
1553 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1555 static const uint8_t ar_id = 1;
1556 uint64_t ckc = cpu->env.ckc >> 8;
1557 void *mem;
1558 int i;
1559 hwaddr len = SAVE_AREA_SIZE;
1561 mem = cpu_physical_memory_map(addr, &len, 1);
1562 if (!mem) {
1563 return -EFAULT;
1565 if (len != SAVE_AREA_SIZE) {
1566 cpu_physical_memory_unmap(mem, len, 1, 0);
1567 return -EFAULT;
1570 if (store_arch) {
1571 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1573 for (i = 0; i < 16; ++i) {
1574 *((uint64_t *)mem + i) = get_freg(&cpu->env, i)->ll;
1576 memcpy(mem + 128, &cpu->env.regs, 128);
1577 memcpy(mem + 256, &cpu->env.psw, 16);
1578 memcpy(mem + 280, &cpu->env.psa, 4);
1579 memcpy(mem + 284, &cpu->env.fpc, 4);
1580 memcpy(mem + 292, &cpu->env.todpr, 4);
1581 memcpy(mem + 296, &cpu->env.cputm, 8);
1582 memcpy(mem + 304, &ckc, 8);
1583 memcpy(mem + 320, &cpu->env.aregs, 64);
1584 memcpy(mem + 384, &cpu->env.cregs, 128);
1586 cpu_physical_memory_unmap(mem, len, 1, len);
1588 return 0;
1591 static void sigp_stop_and_store_status(CPUState *cs, run_on_cpu_data arg)
1593 S390CPU *cpu = S390_CPU(cs);
1594 SigpInfo *si = arg.host_ptr;
1595 struct kvm_s390_irq irq = {
1596 .type = KVM_S390_SIGP_STOP,
1599 /* disabled wait - sleeping in user space */
1600 if (s390_cpu_get_state(cpu) == CPU_STATE_OPERATING && cs->halted) {
1601 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
1604 switch (s390_cpu_get_state(cpu)) {
1605 case CPU_STATE_OPERATING:
1606 cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1607 kvm_s390_vcpu_interrupt(cpu, &irq);
1608 /* store will be performed when handling the stop intercept */
1609 break;
1610 case CPU_STATE_STOPPED:
1611 /* already stopped, just store the status */
1612 cpu_synchronize_state(cs);
1613 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1614 break;
1616 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1619 static void sigp_store_status_at_address(CPUState *cs, run_on_cpu_data arg)
1621 S390CPU *cpu = S390_CPU(cs);
1622 SigpInfo *si = arg.host_ptr;
1623 uint32_t address = si->param & 0x7ffffe00u;
1625 /* cpu has to be stopped */
1626 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1627 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1628 return;
1631 cpu_synchronize_state(cs);
1633 if (kvm_s390_store_status(cpu, address, false)) {
1634 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1635 return;
1637 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1640 #define ADTL_SAVE_LC_MASK 0xfUL
1641 static void sigp_store_adtl_status(CPUState *cs, run_on_cpu_data arg)
1643 S390CPU *cpu = S390_CPU(cs);
1644 SigpInfo *si = arg.host_ptr;
1645 uint8_t lc = si->param & ADTL_SAVE_LC_MASK;
1646 hwaddr addr = si->param & ~ADTL_SAVE_LC_MASK;
1647 hwaddr len = 1UL << (lc ? lc : 10);
1649 if (!s390_has_feat(S390_FEAT_VECTOR) &&
1650 !s390_has_feat(S390_FEAT_GUARDED_STORAGE)) {
1651 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1652 return;
1655 /* cpu has to be stopped */
1656 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1657 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1658 return;
1661 /* address must be aligned to length */
1662 if (addr & (len - 1)) {
1663 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1664 return;
1667 /* no GS: only lc == 0 is valid */
1668 if (!s390_has_feat(S390_FEAT_GUARDED_STORAGE) &&
1669 lc != 0) {
1670 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1671 return;
1674 /* GS: 0, 10, 11, 12 are valid */
1675 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE) &&
1676 lc != 0 &&
1677 lc != 10 &&
1678 lc != 11 &&
1679 lc != 12) {
1680 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1681 return;
1684 cpu_synchronize_state(cs);
1686 if (do_store_adtl_status(cpu, addr, len)) {
1687 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1688 return;
1690 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1693 static void sigp_restart(CPUState *cs, run_on_cpu_data arg)
1695 S390CPU *cpu = S390_CPU(cs);
1696 SigpInfo *si = arg.host_ptr;
1697 struct kvm_s390_irq irq = {
1698 .type = KVM_S390_RESTART,
1701 switch (s390_cpu_get_state(cpu)) {
1702 case CPU_STATE_STOPPED:
1703 /* the restart irq has to be delivered prior to any other pending irq */
1704 cpu_synchronize_state(cs);
1705 do_restart_interrupt(&cpu->env);
1706 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1707 break;
1708 case CPU_STATE_OPERATING:
1709 kvm_s390_vcpu_interrupt(cpu, &irq);
1710 break;
1712 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1715 int kvm_s390_cpu_restart(S390CPU *cpu)
1717 SigpInfo si = {};
1719 run_on_cpu(CPU(cpu), sigp_restart, RUN_ON_CPU_HOST_PTR(&si));
1720 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1721 return 0;
1724 static void sigp_initial_cpu_reset(CPUState *cs, run_on_cpu_data arg)
1726 S390CPU *cpu = S390_CPU(cs);
1727 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1728 SigpInfo *si = arg.host_ptr;
1730 cpu_synchronize_state(cs);
1731 scc->initial_cpu_reset(cs);
1732 cpu_synchronize_post_reset(cs);
1733 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1736 static void sigp_cpu_reset(CPUState *cs, run_on_cpu_data arg)
1738 S390CPU *cpu = S390_CPU(cs);
1739 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1740 SigpInfo *si = arg.host_ptr;
1742 cpu_synchronize_state(cs);
1743 scc->cpu_reset(cs);
1744 cpu_synchronize_post_reset(cs);
1745 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1748 static void sigp_set_prefix(CPUState *cs, run_on_cpu_data arg)
1750 S390CPU *cpu = S390_CPU(cs);
1751 SigpInfo *si = arg.host_ptr;
1752 uint32_t addr = si->param & 0x7fffe000u;
1754 cpu_synchronize_state(cs);
1756 if (!address_space_access_valid(&address_space_memory, addr,
1757 sizeof(struct LowCore), false)) {
1758 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1759 return;
1762 /* cpu has to be stopped */
1763 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1764 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1765 return;
1768 cpu->env.psa = addr;
1769 cpu_synchronize_post_init(cs);
1770 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1773 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1774 uint64_t param, uint64_t *status_reg)
1776 SigpInfo si = {
1777 .param = param,
1778 .status_reg = status_reg,
1781 /* cpu available? */
1782 if (dst_cpu == NULL) {
1783 return SIGP_CC_NOT_OPERATIONAL;
1786 /* only resets can break pending orders */
1787 if (dst_cpu->env.sigp_order != 0 &&
1788 order != SIGP_CPU_RESET &&
1789 order != SIGP_INITIAL_CPU_RESET) {
1790 return SIGP_CC_BUSY;
1793 switch (order) {
1794 case SIGP_START:
1795 run_on_cpu(CPU(dst_cpu), sigp_start, RUN_ON_CPU_HOST_PTR(&si));
1796 break;
1797 case SIGP_STOP:
1798 run_on_cpu(CPU(dst_cpu), sigp_stop, RUN_ON_CPU_HOST_PTR(&si));
1799 break;
1800 case SIGP_RESTART:
1801 run_on_cpu(CPU(dst_cpu), sigp_restart, RUN_ON_CPU_HOST_PTR(&si));
1802 break;
1803 case SIGP_STOP_STORE_STATUS:
1804 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, RUN_ON_CPU_HOST_PTR(&si));
1805 break;
1806 case SIGP_STORE_STATUS_ADDR:
1807 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, RUN_ON_CPU_HOST_PTR(&si));
1808 break;
1809 case SIGP_STORE_ADTL_STATUS:
1810 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, RUN_ON_CPU_HOST_PTR(&si));
1811 break;
1812 case SIGP_SET_PREFIX:
1813 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, RUN_ON_CPU_HOST_PTR(&si));
1814 break;
1815 case SIGP_INITIAL_CPU_RESET:
1816 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, RUN_ON_CPU_HOST_PTR(&si));
1817 break;
1818 case SIGP_CPU_RESET:
1819 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, RUN_ON_CPU_HOST_PTR(&si));
1820 break;
1821 default:
1822 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1823 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1826 return si.cc;
1829 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1830 uint64_t *status_reg)
1832 CPUState *cur_cs;
1833 S390CPU *cur_cpu;
1834 bool all_stopped = true;
1836 CPU_FOREACH(cur_cs) {
1837 cur_cpu = S390_CPU(cur_cs);
1839 if (cur_cpu == cpu) {
1840 continue;
1842 if (s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1843 all_stopped = false;
1847 *status_reg &= 0xffffffff00000000ULL;
1849 /* Reject set arch order, with czam we're always in z/Arch mode. */
1850 *status_reg |= (all_stopped ? SIGP_STAT_INVALID_PARAMETER :
1851 SIGP_STAT_INCORRECT_STATE);
1852 return SIGP_CC_STATUS_STORED;
1855 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1857 CPUS390XState *env = &cpu->env;
1858 const uint8_t r1 = ipa1 >> 4;
1859 const uint8_t r3 = ipa1 & 0x0f;
1860 int ret;
1861 uint8_t order;
1862 uint64_t *status_reg;
1863 uint64_t param;
1864 S390CPU *dst_cpu = NULL;
1866 cpu_synchronize_state(CPU(cpu));
1868 /* get order code */
1869 order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1870 & SIGP_ORDER_MASK;
1871 status_reg = &env->regs[r1];
1872 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1874 if (qemu_mutex_trylock(&qemu_sigp_mutex)) {
1875 ret = SIGP_CC_BUSY;
1876 goto out;
1879 switch (order) {
1880 case SIGP_SET_ARCH:
1881 ret = sigp_set_architecture(cpu, param, status_reg);
1882 break;
1883 default:
1884 /* all other sigp orders target a single vcpu */
1885 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1886 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1888 qemu_mutex_unlock(&qemu_sigp_mutex);
1890 out:
1891 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1892 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1894 if (ret >= 0) {
1895 setcc(cpu, ret);
1896 return 0;
1899 return ret;
1902 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1904 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1905 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1906 int r = -1;
1908 DPRINTF("handle_instruction 0x%x 0x%x\n",
1909 run->s390_sieic.ipa, run->s390_sieic.ipb);
1910 switch (ipa0) {
1911 case IPA0_B2:
1912 r = handle_b2(cpu, run, ipa1);
1913 break;
1914 case IPA0_B9:
1915 r = handle_b9(cpu, run, ipa1);
1916 break;
1917 case IPA0_EB:
1918 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1919 break;
1920 case IPA0_E3:
1921 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1922 break;
1923 case IPA0_DIAG:
1924 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1925 break;
1926 case IPA0_SIGP:
1927 r = handle_sigp(cpu, run, ipa1);
1928 break;
1931 if (r < 0) {
1932 r = 0;
1933 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1936 return r;
1939 static bool is_special_wait_psw(CPUState *cs)
1941 /* signal quiesce */
1942 return cs->kvm_run->psw_addr == 0xfffUL;
1945 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1947 CPUState *cs = CPU(cpu);
1949 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1950 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1951 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1952 s390_cpu_halt(cpu);
1953 qemu_system_guest_panicked(NULL);
1956 /* try to detect pgm check loops */
1957 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1959 CPUState *cs = CPU(cpu);
1960 PSW oldpsw, newpsw;
1962 cpu_synchronize_state(cs);
1963 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1964 offsetof(LowCore, program_new_psw));
1965 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1966 offsetof(LowCore, program_new_psw) + 8);
1967 oldpsw.mask = run->psw_mask;
1968 oldpsw.addr = run->psw_addr;
1970 * Avoid endless loops of operation exceptions, if the pgm new
1971 * PSW will cause a new operation exception.
1972 * The heuristic checks if the pgm new psw is within 6 bytes before
1973 * the faulting psw address (with same DAT, AS settings) and the
1974 * new psw is not a wait psw and the fault was not triggered by
1975 * problem state. In that case go into crashed state.
1978 if (oldpsw.addr - newpsw.addr <= 6 &&
1979 !(newpsw.mask & PSW_MASK_WAIT) &&
1980 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1981 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1982 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1983 unmanageable_intercept(cpu, "operation exception loop",
1984 offsetof(LowCore, program_new_psw));
1985 return EXCP_HALTED;
1987 return 0;
1990 static int handle_intercept(S390CPU *cpu)
1992 CPUState *cs = CPU(cpu);
1993 struct kvm_run *run = cs->kvm_run;
1994 int icpt_code = run->s390_sieic.icptcode;
1995 int r = 0;
1997 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1998 (long)cs->kvm_run->psw_addr);
1999 switch (icpt_code) {
2000 case ICPT_INSTRUCTION:
2001 r = handle_instruction(cpu, run);
2002 break;
2003 case ICPT_PROGRAM:
2004 unmanageable_intercept(cpu, "program interrupt",
2005 offsetof(LowCore, program_new_psw));
2006 r = EXCP_HALTED;
2007 break;
2008 case ICPT_EXT_INT:
2009 unmanageable_intercept(cpu, "external interrupt",
2010 offsetof(LowCore, external_new_psw));
2011 r = EXCP_HALTED;
2012 break;
2013 case ICPT_WAITPSW:
2014 /* disabled wait, since enabled wait is handled in kernel */
2015 cpu_synchronize_state(cs);
2016 if (s390_cpu_halt(cpu) == 0) {
2017 if (is_special_wait_psw(cs)) {
2018 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
2019 } else {
2020 qemu_system_guest_panicked(NULL);
2023 r = EXCP_HALTED;
2024 break;
2025 case ICPT_CPU_STOP:
2026 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
2027 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
2029 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
2030 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
2031 true);
2033 cpu->env.sigp_order = 0;
2034 r = EXCP_HALTED;
2035 break;
2036 case ICPT_OPEREXC:
2037 /* check for break points */
2038 r = handle_sw_breakpoint(cpu, run);
2039 if (r == -ENOENT) {
2040 /* Then check for potential pgm check loops */
2041 r = handle_oper_loop(cpu, run);
2042 if (r == 0) {
2043 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
2046 break;
2047 case ICPT_SOFT_INTERCEPT:
2048 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
2049 exit(1);
2050 break;
2051 case ICPT_IO:
2052 fprintf(stderr, "KVM unimplemented icpt IO\n");
2053 exit(1);
2054 break;
2055 default:
2056 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
2057 exit(1);
2058 break;
2061 return r;
2064 static int handle_tsch(S390CPU *cpu)
2066 CPUState *cs = CPU(cpu);
2067 struct kvm_run *run = cs->kvm_run;
2068 int ret;
2070 cpu_synchronize_state(cs);
2072 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
2073 if (ret < 0) {
2075 * Failure.
2076 * If an I/O interrupt had been dequeued, we have to reinject it.
2078 if (run->s390_tsch.dequeued) {
2079 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
2080 run->s390_tsch.subchannel_nr,
2081 run->s390_tsch.io_int_parm,
2082 run->s390_tsch.io_int_word);
2084 ret = 0;
2086 return ret;
2089 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
2091 struct sysib_322 sysib;
2092 int del;
2094 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
2095 return;
2097 /* Shift the stack of Extended Names to prepare for our own data */
2098 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
2099 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
2100 /* First virt level, that doesn't provide Ext Names delimits stack. It is
2101 * assumed it's not capable of managing Extended Names for lower levels.
2103 for (del = 1; del < sysib.count; del++) {
2104 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
2105 break;
2108 if (del < sysib.count) {
2109 memset(sysib.ext_names[del], 0,
2110 sizeof(sysib.ext_names[0]) * (sysib.count - del));
2112 /* Insert short machine name in EBCDIC, padded with blanks */
2113 if (qemu_name) {
2114 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
2115 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
2116 strlen(qemu_name)));
2118 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
2119 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
2120 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
2121 * considered by s390 as not capable of providing any Extended Name.
2122 * Therefore if no name was specified on qemu invocation, we go with the
2123 * same "KVMguest" default, which KVM has filled into short name field.
2125 if (qemu_name) {
2126 strncpy((char *)sysib.ext_names[0], qemu_name,
2127 sizeof(sysib.ext_names[0]));
2128 } else {
2129 strcpy((char *)sysib.ext_names[0], "KVMguest");
2131 /* Insert UUID */
2132 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
2134 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
2137 static int handle_stsi(S390CPU *cpu)
2139 CPUState *cs = CPU(cpu);
2140 struct kvm_run *run = cs->kvm_run;
2142 switch (run->s390_stsi.fc) {
2143 case 3:
2144 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
2145 return 0;
2147 /* Only sysib 3.2.2 needs post-handling for now. */
2148 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
2149 return 0;
2150 default:
2151 return 0;
2155 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
2157 CPUState *cs = CPU(cpu);
2158 struct kvm_run *run = cs->kvm_run;
2160 int ret = 0;
2161 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
2163 switch (arch_info->type) {
2164 case KVM_HW_WP_WRITE:
2165 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
2166 cs->watchpoint_hit = &hw_watchpoint;
2167 hw_watchpoint.vaddr = arch_info->addr;
2168 hw_watchpoint.flags = BP_MEM_WRITE;
2169 ret = EXCP_DEBUG;
2171 break;
2172 case KVM_HW_BP:
2173 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
2174 ret = EXCP_DEBUG;
2176 break;
2177 case KVM_SINGLESTEP:
2178 if (cs->singlestep_enabled) {
2179 ret = EXCP_DEBUG;
2181 break;
2182 default:
2183 ret = -ENOSYS;
2186 return ret;
2189 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
2191 S390CPU *cpu = S390_CPU(cs);
2192 int ret = 0;
2194 qemu_mutex_lock_iothread();
2196 switch (run->exit_reason) {
2197 case KVM_EXIT_S390_SIEIC:
2198 ret = handle_intercept(cpu);
2199 break;
2200 case KVM_EXIT_S390_RESET:
2201 s390_reipl_request();
2202 break;
2203 case KVM_EXIT_S390_TSCH:
2204 ret = handle_tsch(cpu);
2205 break;
2206 case KVM_EXIT_S390_STSI:
2207 ret = handle_stsi(cpu);
2208 break;
2209 case KVM_EXIT_DEBUG:
2210 ret = kvm_arch_handle_debug_exit(cpu);
2211 break;
2212 default:
2213 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2214 break;
2216 qemu_mutex_unlock_iothread();
2218 if (ret == 0) {
2219 ret = EXCP_INTERRUPT;
2221 return ret;
2224 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2226 return true;
2229 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2230 uint16_t subchannel_nr, uint32_t io_int_parm,
2231 uint32_t io_int_word)
2233 struct kvm_s390_irq irq = {
2234 .u.io.subchannel_id = subchannel_id,
2235 .u.io.subchannel_nr = subchannel_nr,
2236 .u.io.io_int_parm = io_int_parm,
2237 .u.io.io_int_word = io_int_word,
2240 if (io_int_word & IO_INT_WORD_AI) {
2241 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2242 } else {
2243 irq.type = KVM_S390_INT_IO(0, (subchannel_id & 0xff00) >> 8,
2244 (subchannel_id & 0x0006),
2245 subchannel_nr);
2247 kvm_s390_floating_interrupt(&irq);
2250 static uint64_t build_channel_report_mcic(void)
2252 uint64_t mcic;
2254 /* subclass: indicate channel report pending */
2255 mcic = MCIC_SC_CP |
2256 /* subclass modifiers: none */
2257 /* storage errors: none */
2258 /* validity bits: no damage */
2259 MCIC_VB_WP | MCIC_VB_MS | MCIC_VB_PM | MCIC_VB_IA | MCIC_VB_FP |
2260 MCIC_VB_GR | MCIC_VB_CR | MCIC_VB_ST | MCIC_VB_AR | MCIC_VB_PR |
2261 MCIC_VB_FC | MCIC_VB_CT | MCIC_VB_CC;
2262 if (s390_has_feat(S390_FEAT_VECTOR)) {
2263 mcic |= MCIC_VB_VR;
2265 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE)) {
2266 mcic |= MCIC_VB_GS;
2268 return mcic;
2271 void kvm_s390_crw_mchk(void)
2273 struct kvm_s390_irq irq = {
2274 .type = KVM_S390_MCHK,
2275 .u.mchk.cr14 = 1 << 28,
2276 .u.mchk.mcic = build_channel_report_mcic(),
2278 kvm_s390_floating_interrupt(&irq);
2281 void kvm_s390_enable_css_support(S390CPU *cpu)
2283 int r;
2285 /* Activate host kernel channel subsystem support. */
2286 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2287 assert(r == 0);
2290 void kvm_arch_init_irq_routing(KVMState *s)
2293 * Note that while irqchip capabilities generally imply that cpustates
2294 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2295 * have to override the common code kvm_halt_in_kernel_allowed setting.
2297 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2298 kvm_gsi_routing_allowed = true;
2299 kvm_halt_in_kernel_allowed = false;
2303 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2304 int vq, bool assign)
2306 struct kvm_ioeventfd kick = {
2307 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2308 KVM_IOEVENTFD_FLAG_DATAMATCH,
2309 .fd = event_notifier_get_fd(notifier),
2310 .datamatch = vq,
2311 .addr = sch,
2312 .len = 8,
2314 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2315 return -ENOSYS;
2317 if (!assign) {
2318 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2320 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2323 int kvm_s390_get_memslot_count(void)
2325 return kvm_check_extension(kvm_state, KVM_CAP_NR_MEMSLOTS);
2328 int kvm_s390_get_ri(void)
2330 return cap_ri;
2333 int kvm_s390_get_gs(void)
2335 return cap_gs;
2338 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2340 struct kvm_mp_state mp_state = {};
2341 int ret;
2343 /* the kvm part might not have been initialized yet */
2344 if (CPU(cpu)->kvm_state == NULL) {
2345 return 0;
2348 switch (cpu_state) {
2349 case CPU_STATE_STOPPED:
2350 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2351 break;
2352 case CPU_STATE_CHECK_STOP:
2353 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2354 break;
2355 case CPU_STATE_OPERATING:
2356 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2357 break;
2358 case CPU_STATE_LOAD:
2359 mp_state.mp_state = KVM_MP_STATE_LOAD;
2360 break;
2361 default:
2362 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2363 cpu_state);
2364 exit(1);
2367 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2368 if (ret) {
2369 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2370 strerror(-ret));
2373 return ret;
2376 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2378 struct kvm_s390_irq_state irq_state;
2379 CPUState *cs = CPU(cpu);
2380 int32_t bytes;
2382 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2383 return;
2386 irq_state.buf = (uint64_t) cpu->irqstate;
2387 irq_state.len = VCPU_IRQ_BUF_SIZE;
2389 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2390 if (bytes < 0) {
2391 cpu->irqstate_saved_size = 0;
2392 error_report("Migration of interrupt state failed");
2393 return;
2396 cpu->irqstate_saved_size = bytes;
2399 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2401 CPUState *cs = CPU(cpu);
2402 struct kvm_s390_irq_state irq_state;
2403 int r;
2405 if (cpu->irqstate_saved_size == 0) {
2406 return 0;
2409 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2410 return -ENOSYS;
2413 irq_state.buf = (uint64_t) cpu->irqstate;
2414 irq_state.len = cpu->irqstate_saved_size;
2416 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2417 if (r) {
2418 error_report("Setting interrupt state failed %d", r);
2420 return r;
2423 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2424 uint64_t address, uint32_t data, PCIDevice *dev)
2426 S390PCIBusDevice *pbdev;
2427 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2429 if (!dev) {
2430 DPRINTF("add_msi_route no pci device\n");
2431 return -ENODEV;
2434 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
2435 if (!pbdev) {
2436 DPRINTF("add_msi_route no zpci device\n");
2437 return -ENODEV;
2440 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2441 route->flags = 0;
2442 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2443 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2444 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2445 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
2446 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2447 return 0;
2450 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2451 int vector, PCIDevice *dev)
2453 return 0;
2456 int kvm_arch_release_virq_post(int virq)
2458 return 0;
2461 int kvm_arch_msi_data_to_gsi(uint32_t data)
2463 abort();
2466 static int query_cpu_subfunc(S390FeatBitmap features)
2468 struct kvm_s390_vm_cpu_subfunc prop;
2469 struct kvm_device_attr attr = {
2470 .group = KVM_S390_VM_CPU_MODEL,
2471 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2472 .addr = (uint64_t) &prop,
2474 int rc;
2476 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2477 if (rc) {
2478 return rc;
2482 * We're going to add all subfunctions now, if the corresponding feature
2483 * is available that unlocks the query functions.
2485 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2486 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2487 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2489 if (test_bit(S390_FEAT_MSA, features)) {
2490 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2491 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2492 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2493 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2494 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2496 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2497 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2499 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2500 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2501 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2502 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2503 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2505 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2506 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2508 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2509 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2511 return 0;
2514 static int configure_cpu_subfunc(const S390FeatBitmap features)
2516 struct kvm_s390_vm_cpu_subfunc prop = {};
2517 struct kvm_device_attr attr = {
2518 .group = KVM_S390_VM_CPU_MODEL,
2519 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2520 .addr = (uint64_t) &prop,
2523 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2524 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2525 /* hardware support might be missing, IBC will handle most of this */
2526 return 0;
2529 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2530 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2531 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2533 if (test_bit(S390_FEAT_MSA, features)) {
2534 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2535 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2536 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2537 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2538 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2540 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2541 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2543 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2544 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2545 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2546 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2547 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2549 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2550 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2552 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2553 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2555 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2558 static int kvm_to_feat[][2] = {
2559 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2560 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2561 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2562 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2563 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2564 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2565 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2566 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2567 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2568 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2569 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2570 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2571 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2572 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2575 static int query_cpu_feat(S390FeatBitmap features)
2577 struct kvm_s390_vm_cpu_feat prop;
2578 struct kvm_device_attr attr = {
2579 .group = KVM_S390_VM_CPU_MODEL,
2580 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2581 .addr = (uint64_t) &prop,
2583 int rc;
2584 int i;
2586 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2587 if (rc) {
2588 return rc;
2591 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2592 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2593 set_bit(kvm_to_feat[i][1], features);
2596 return 0;
2599 static int configure_cpu_feat(const S390FeatBitmap features)
2601 struct kvm_s390_vm_cpu_feat prop = {};
2602 struct kvm_device_attr attr = {
2603 .group = KVM_S390_VM_CPU_MODEL,
2604 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2605 .addr = (uint64_t) &prop,
2607 int i;
2609 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2610 if (test_bit(kvm_to_feat[i][1], features)) {
2611 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2614 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2617 bool kvm_s390_cpu_models_supported(void)
2619 if (!cpu_model_allowed()) {
2620 /* compatibility machines interfere with the cpu model */
2621 return false;
2623 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2624 KVM_S390_VM_CPU_MACHINE) &&
2625 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2626 KVM_S390_VM_CPU_PROCESSOR) &&
2627 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2628 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2629 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2630 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2631 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2632 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2635 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2637 struct kvm_s390_vm_cpu_machine prop = {};
2638 struct kvm_device_attr attr = {
2639 .group = KVM_S390_VM_CPU_MODEL,
2640 .attr = KVM_S390_VM_CPU_MACHINE,
2641 .addr = (uint64_t) &prop,
2643 uint16_t unblocked_ibc = 0, cpu_type = 0;
2644 int rc;
2646 memset(model, 0, sizeof(*model));
2648 if (!kvm_s390_cpu_models_supported()) {
2649 error_setg(errp, "KVM doesn't support CPU models");
2650 return;
2653 /* query the basic cpu model properties */
2654 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2655 if (rc) {
2656 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2657 return;
2660 cpu_type = cpuid_type(prop.cpuid);
2661 if (has_ibc(prop.ibc)) {
2662 model->lowest_ibc = lowest_ibc(prop.ibc);
2663 unblocked_ibc = unblocked_ibc(prop.ibc);
2665 model->cpu_id = cpuid_id(prop.cpuid);
2666 model->cpu_id_format = cpuid_format(prop.cpuid);
2667 model->cpu_ver = 0xff;
2669 /* get supported cpu features indicated via STFL(E) */
2670 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2671 (uint8_t *) prop.fac_mask);
2672 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2673 if (test_bit(S390_FEAT_STFLE, model->features)) {
2674 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2676 /* get supported cpu features indicated e.g. via SCLP */
2677 rc = query_cpu_feat(model->features);
2678 if (rc) {
2679 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2680 return;
2682 /* get supported cpu subfunctions indicated via query / test bit */
2683 rc = query_cpu_subfunc(model->features);
2684 if (rc) {
2685 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2686 return;
2689 /* with cpu model support, CMM is only indicated if really available */
2690 if (kvm_s390_cmma_available()) {
2691 set_bit(S390_FEAT_CMM, model->features);
2692 } else {
2693 /* no cmm -> no cmm nt */
2694 clear_bit(S390_FEAT_CMM_NT, model->features);
2697 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2698 if (pci_available) {
2699 set_bit(S390_FEAT_ZPCI, model->features);
2701 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2703 if (s390_known_cpu_type(cpu_type)) {
2704 /* we want the exact model, even if some features are missing */
2705 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2706 ibc_ec_ga(unblocked_ibc), NULL);
2707 } else {
2708 /* model unknown, e.g. too new - search using features */
2709 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2710 ibc_ec_ga(unblocked_ibc),
2711 model->features);
2713 if (!model->def) {
2714 error_setg(errp, "KVM: host CPU model could not be identified");
2715 return;
2717 /* strip of features that are not part of the maximum model */
2718 bitmap_and(model->features, model->features, model->def->full_feat,
2719 S390_FEAT_MAX);
2722 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2724 struct kvm_s390_vm_cpu_processor prop = {
2725 .fac_list = { 0 },
2727 struct kvm_device_attr attr = {
2728 .group = KVM_S390_VM_CPU_MODEL,
2729 .attr = KVM_S390_VM_CPU_PROCESSOR,
2730 .addr = (uint64_t) &prop,
2732 int rc;
2734 if (!model) {
2735 /* compatibility handling if cpu models are disabled */
2736 if (kvm_s390_cmma_available()) {
2737 kvm_s390_enable_cmma();
2739 return;
2741 if (!kvm_s390_cpu_models_supported()) {
2742 error_setg(errp, "KVM doesn't support CPU models");
2743 return;
2745 prop.cpuid = s390_cpuid_from_cpu_model(model);
2746 prop.ibc = s390_ibc_from_cpu_model(model);
2747 /* configure cpu features indicated via STFL(e) */
2748 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2749 (uint8_t *) prop.fac_list);
2750 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2751 if (rc) {
2752 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2753 return;
2755 /* configure cpu features indicated e.g. via SCLP */
2756 rc = configure_cpu_feat(model->features);
2757 if (rc) {
2758 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2759 return;
2761 /* configure cpu subfunctions indicated via query / test bit */
2762 rc = configure_cpu_subfunc(model->features);
2763 if (rc) {
2764 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2765 return;
2767 /* enable CMM via CMMA */
2768 if (test_bit(S390_FEAT_CMM, model->features)) {
2769 kvm_s390_enable_cmma();