qapi: drop the sentinel in enum array
[qemu/armbru.git] / target / s390x / kvm.c
blobd07763ff2c3cdedee066c42eded7ef559e93a2cb
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"
52 #ifndef DEBUG_KVM
53 #define DEBUG_KVM 0
54 #endif
56 #define DPRINTF(fmt, ...) do { \
57 if (DEBUG_KVM) { \
58 fprintf(stderr, fmt, ## __VA_ARGS__); \
59 } \
60 } while (0);
62 #define kvm_vm_check_mem_attr(s, attr) \
63 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
65 #define IPA0_DIAG 0x8300
66 #define IPA0_SIGP 0xae00
67 #define IPA0_B2 0xb200
68 #define IPA0_B9 0xb900
69 #define IPA0_EB 0xeb00
70 #define IPA0_E3 0xe300
72 #define PRIV_B2_SCLP_CALL 0x20
73 #define PRIV_B2_CSCH 0x30
74 #define PRIV_B2_HSCH 0x31
75 #define PRIV_B2_MSCH 0x32
76 #define PRIV_B2_SSCH 0x33
77 #define PRIV_B2_STSCH 0x34
78 #define PRIV_B2_TSCH 0x35
79 #define PRIV_B2_TPI 0x36
80 #define PRIV_B2_SAL 0x37
81 #define PRIV_B2_RSCH 0x38
82 #define PRIV_B2_STCRW 0x39
83 #define PRIV_B2_STCPS 0x3a
84 #define PRIV_B2_RCHP 0x3b
85 #define PRIV_B2_SCHM 0x3c
86 #define PRIV_B2_CHSC 0x5f
87 #define PRIV_B2_SIGA 0x74
88 #define PRIV_B2_XSCH 0x76
90 #define PRIV_EB_SQBS 0x8a
91 #define PRIV_EB_PCISTB 0xd0
92 #define PRIV_EB_SIC 0xd1
94 #define PRIV_B9_EQBS 0x9c
95 #define PRIV_B9_CLP 0xa0
96 #define PRIV_B9_PCISTG 0xd0
97 #define PRIV_B9_PCILG 0xd2
98 #define PRIV_B9_RPCIT 0xd3
100 #define PRIV_E3_MPCIFC 0xd0
101 #define PRIV_E3_STPCIFC 0xd4
103 #define DIAG_TIMEREVENT 0x288
104 #define DIAG_IPL 0x308
105 #define DIAG_KVM_HYPERCALL 0x500
106 #define DIAG_KVM_BREAKPOINT 0x501
108 #define ICPT_INSTRUCTION 0x04
109 #define ICPT_PROGRAM 0x08
110 #define ICPT_EXT_INT 0x14
111 #define ICPT_WAITPSW 0x1c
112 #define ICPT_SOFT_INTERCEPT 0x24
113 #define ICPT_CPU_STOP 0x28
114 #define ICPT_OPEREXC 0x2c
115 #define ICPT_IO 0x40
117 #define NR_LOCAL_IRQS 32
119 * Needs to be big enough to contain max_cpus emergency signals
120 * and in addition NR_LOCAL_IRQS interrupts
122 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
123 (max_cpus + NR_LOCAL_IRQS))
125 static CPUWatchpoint hw_watchpoint;
127 * We don't use a list because this structure is also used to transmit the
128 * hardware breakpoints to the kernel.
130 static struct kvm_hw_breakpoint *hw_breakpoints;
131 static int nb_hw_breakpoints;
133 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
134 KVM_CAP_LAST_INFO
137 static QemuMutex qemu_sigp_mutex;
139 static int cap_sync_regs;
140 static int cap_async_pf;
141 static int cap_mem_op;
142 static int cap_s390_irq;
143 static int cap_ri;
144 static int cap_gs;
146 static int active_cmma;
148 static void *legacy_s390_alloc(size_t size, uint64_t *align);
150 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
152 struct kvm_device_attr attr = {
153 .group = KVM_S390_VM_MEM_CTRL,
154 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
155 .addr = (uint64_t) memory_limit,
158 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
161 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit)
163 int rc;
165 struct kvm_device_attr attr = {
166 .group = KVM_S390_VM_MEM_CTRL,
167 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
168 .addr = (uint64_t) &new_limit,
171 if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) {
172 return 0;
175 rc = kvm_s390_query_mem_limit(hw_limit);
176 if (rc) {
177 return rc;
178 } else if (*hw_limit < new_limit) {
179 return -E2BIG;
182 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
185 int kvm_s390_cmma_active(void)
187 return active_cmma;
190 static bool kvm_s390_cmma_available(void)
192 static bool initialized, value;
194 if (!initialized) {
195 initialized = true;
196 value = kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_ENABLE_CMMA) &&
197 kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_CLR_CMMA);
199 return value;
202 void kvm_s390_cmma_reset(void)
204 int rc;
205 struct kvm_device_attr attr = {
206 .group = KVM_S390_VM_MEM_CTRL,
207 .attr = KVM_S390_VM_MEM_CLR_CMMA,
210 if (!kvm_s390_cmma_active()) {
211 return;
214 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
215 trace_kvm_clear_cmma(rc);
218 static void kvm_s390_enable_cmma(void)
220 int rc;
221 struct kvm_device_attr attr = {
222 .group = KVM_S390_VM_MEM_CTRL,
223 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
226 if (mem_path) {
227 error_report("Warning: CMM will not be enabled because it is not "
228 "compatible to hugetlbfs.");
229 return;
231 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
232 active_cmma = !rc;
233 trace_kvm_enable_cmma(rc);
236 static void kvm_s390_set_attr(uint64_t attr)
238 struct kvm_device_attr attribute = {
239 .group = KVM_S390_VM_CRYPTO,
240 .attr = attr,
243 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
245 if (ret) {
246 error_report("Failed to set crypto device attribute %lu: %s",
247 attr, strerror(-ret));
251 static void kvm_s390_init_aes_kw(void)
253 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
255 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
256 NULL)) {
257 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
260 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
261 kvm_s390_set_attr(attr);
265 static void kvm_s390_init_dea_kw(void)
267 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
269 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
270 NULL)) {
271 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
274 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
275 kvm_s390_set_attr(attr);
279 void kvm_s390_crypto_reset(void)
281 if (s390_has_feat(S390_FEAT_MSA_EXT_3)) {
282 kvm_s390_init_aes_kw();
283 kvm_s390_init_dea_kw();
287 int kvm_arch_init(MachineState *ms, KVMState *s)
289 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
290 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
291 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
292 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
294 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
295 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
296 phys_mem_set_alloc(legacy_s390_alloc);
299 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
300 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
301 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
302 if (ri_allowed()) {
303 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
304 cap_ri = 1;
307 if (gs_allowed()) {
308 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
309 cap_gs = 1;
313 /* Try to enable AIS facility */
314 kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0);
316 qemu_mutex_init(&qemu_sigp_mutex);
318 return 0;
321 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
323 return 0;
326 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
328 return cpu->cpu_index;
331 int kvm_arch_init_vcpu(CPUState *cs)
333 S390CPU *cpu = S390_CPU(cs);
334 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
335 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
336 return 0;
339 void kvm_s390_reset_vcpu(S390CPU *cpu)
341 CPUState *cs = CPU(cpu);
343 /* The initial reset call is needed here to reset in-kernel
344 * vcpu data that we can't access directly from QEMU
345 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
346 * Before this ioctl cpu_synchronize_state() is called in common kvm
347 * code (kvm-all) */
348 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
349 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
353 static int can_sync_regs(CPUState *cs, int regs)
355 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
358 int kvm_arch_put_registers(CPUState *cs, int level)
360 S390CPU *cpu = S390_CPU(cs);
361 CPUS390XState *env = &cpu->env;
362 struct kvm_sregs sregs;
363 struct kvm_regs regs;
364 struct kvm_fpu fpu = {};
365 int r;
366 int i;
368 /* always save the PSW and the GPRS*/
369 cs->kvm_run->psw_addr = env->psw.addr;
370 cs->kvm_run->psw_mask = env->psw.mask;
372 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
373 for (i = 0; i < 16; i++) {
374 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
375 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
377 } else {
378 for (i = 0; i < 16; i++) {
379 regs.gprs[i] = env->regs[i];
381 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
382 if (r < 0) {
383 return r;
387 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
388 for (i = 0; i < 32; i++) {
389 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
390 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
392 cs->kvm_run->s.regs.fpc = env->fpc;
393 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
394 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
395 for (i = 0; i < 16; i++) {
396 cs->kvm_run->s.regs.fprs[i] = get_freg(env, i)->ll;
398 cs->kvm_run->s.regs.fpc = env->fpc;
399 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
400 } else {
401 /* Floating point */
402 for (i = 0; i < 16; i++) {
403 fpu.fprs[i] = get_freg(env, i)->ll;
405 fpu.fpc = env->fpc;
407 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
408 if (r < 0) {
409 return r;
413 /* Do we need to save more than that? */
414 if (level == KVM_PUT_RUNTIME_STATE) {
415 return 0;
418 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
419 cs->kvm_run->s.regs.cputm = env->cputm;
420 cs->kvm_run->s.regs.ckc = env->ckc;
421 cs->kvm_run->s.regs.todpr = env->todpr;
422 cs->kvm_run->s.regs.gbea = env->gbea;
423 cs->kvm_run->s.regs.pp = env->pp;
424 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
425 } else {
427 * These ONE_REGS are not protected by a capability. As they are only
428 * necessary for migration we just trace a possible error, but don't
429 * return with an error return code.
431 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
432 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
433 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
434 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
435 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
438 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
439 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
440 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
443 /* pfault parameters */
444 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
445 cs->kvm_run->s.regs.pft = env->pfault_token;
446 cs->kvm_run->s.regs.pfs = env->pfault_select;
447 cs->kvm_run->s.regs.pfc = env->pfault_compare;
448 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
449 } else if (cap_async_pf) {
450 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
451 if (r < 0) {
452 return r;
454 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
455 if (r < 0) {
456 return r;
458 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
459 if (r < 0) {
460 return r;
464 /* access registers and control registers*/
465 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
466 for (i = 0; i < 16; i++) {
467 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
468 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
470 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
471 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
472 } else {
473 for (i = 0; i < 16; i++) {
474 sregs.acrs[i] = env->aregs[i];
475 sregs.crs[i] = env->cregs[i];
477 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
478 if (r < 0) {
479 return r;
483 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
484 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
485 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
488 /* Finally the prefix */
489 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
490 cs->kvm_run->s.regs.prefix = env->psa;
491 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
492 } else {
493 /* prefix is only supported via sync regs */
495 return 0;
498 int kvm_arch_get_registers(CPUState *cs)
500 S390CPU *cpu = S390_CPU(cs);
501 CPUS390XState *env = &cpu->env;
502 struct kvm_sregs sregs;
503 struct kvm_regs regs;
504 struct kvm_fpu fpu;
505 int i, r;
507 /* get the PSW */
508 env->psw.addr = cs->kvm_run->psw_addr;
509 env->psw.mask = cs->kvm_run->psw_mask;
511 /* the GPRS */
512 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
513 for (i = 0; i < 16; i++) {
514 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
516 } else {
517 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
518 if (r < 0) {
519 return r;
521 for (i = 0; i < 16; i++) {
522 env->regs[i] = regs.gprs[i];
526 /* The ACRS and CRS */
527 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
528 for (i = 0; i < 16; i++) {
529 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
530 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
532 } else {
533 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
534 if (r < 0) {
535 return r;
537 for (i = 0; i < 16; i++) {
538 env->aregs[i] = sregs.acrs[i];
539 env->cregs[i] = sregs.crs[i];
543 /* Floating point and vector registers */
544 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
545 for (i = 0; i < 32; i++) {
546 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
547 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
549 env->fpc = cs->kvm_run->s.regs.fpc;
550 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
551 for (i = 0; i < 16; i++) {
552 get_freg(env, i)->ll = cs->kvm_run->s.regs.fprs[i];
554 env->fpc = cs->kvm_run->s.regs.fpc;
555 } else {
556 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
557 if (r < 0) {
558 return r;
560 for (i = 0; i < 16; i++) {
561 get_freg(env, i)->ll = fpu.fprs[i];
563 env->fpc = fpu.fpc;
566 /* The prefix */
567 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
568 env->psa = cs->kvm_run->s.regs.prefix;
571 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
572 env->cputm = cs->kvm_run->s.regs.cputm;
573 env->ckc = cs->kvm_run->s.regs.ckc;
574 env->todpr = cs->kvm_run->s.regs.todpr;
575 env->gbea = cs->kvm_run->s.regs.gbea;
576 env->pp = cs->kvm_run->s.regs.pp;
577 } else {
579 * These ONE_REGS are not protected by a capability. As they are only
580 * necessary for migration we just trace a possible error, but don't
581 * return with an error return code.
583 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
584 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
585 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
586 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
587 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
590 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
591 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
594 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
595 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
598 /* pfault parameters */
599 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
600 env->pfault_token = cs->kvm_run->s.regs.pft;
601 env->pfault_select = cs->kvm_run->s.regs.pfs;
602 env->pfault_compare = cs->kvm_run->s.regs.pfc;
603 } else if (cap_async_pf) {
604 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
605 if (r < 0) {
606 return r;
608 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
609 if (r < 0) {
610 return r;
612 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
613 if (r < 0) {
614 return r;
618 return 0;
621 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
623 int r;
624 struct kvm_device_attr attr = {
625 .group = KVM_S390_VM_TOD,
626 .attr = KVM_S390_VM_TOD_LOW,
627 .addr = (uint64_t)tod_low,
630 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
631 if (r) {
632 return r;
635 attr.attr = KVM_S390_VM_TOD_HIGH;
636 attr.addr = (uint64_t)tod_high;
637 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
640 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
642 int r;
644 struct kvm_device_attr attr = {
645 .group = KVM_S390_VM_TOD,
646 .attr = KVM_S390_VM_TOD_LOW,
647 .addr = (uint64_t)tod_low,
650 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
651 if (r) {
652 return r;
655 attr.attr = KVM_S390_VM_TOD_HIGH;
656 attr.addr = (uint64_t)tod_high;
657 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
661 * kvm_s390_mem_op:
662 * @addr: the logical start address in guest memory
663 * @ar: the access register number
664 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
665 * @len: length that should be transferred
666 * @is_write: true = write, false = read
667 * Returns: 0 on success, non-zero if an exception or error occurred
669 * Use KVM ioctl to read/write from/to guest memory. An access exception
670 * is injected into the vCPU in case of translation errors.
672 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
673 int len, bool is_write)
675 struct kvm_s390_mem_op mem_op = {
676 .gaddr = addr,
677 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
678 .size = len,
679 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
680 : KVM_S390_MEMOP_LOGICAL_READ,
681 .buf = (uint64_t)hostbuf,
682 .ar = ar,
684 int ret;
686 if (!cap_mem_op) {
687 return -ENOSYS;
689 if (!hostbuf) {
690 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
693 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
694 if (ret < 0) {
695 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
697 return ret;
701 * Legacy layout for s390:
702 * Older S390 KVM requires the topmost vma of the RAM to be
703 * smaller than an system defined value, which is at least 256GB.
704 * Larger systems have larger values. We put the guest between
705 * the end of data segment (system break) and this value. We
706 * use 32GB as a base to have enough room for the system break
707 * to grow. We also have to use MAP parameters that avoid
708 * read-only mapping of guest pages.
710 static void *legacy_s390_alloc(size_t size, uint64_t *align)
712 void *mem;
714 mem = mmap((void *) 0x800000000ULL, size,
715 PROT_EXEC|PROT_READ|PROT_WRITE,
716 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
717 return mem == MAP_FAILED ? NULL : mem;
720 static uint8_t const *sw_bp_inst;
721 static uint8_t sw_bp_ilen;
723 static void determine_sw_breakpoint_instr(void)
725 /* DIAG 501 is used for sw breakpoints with old kernels */
726 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
727 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
728 static const uint8_t instr_0x0000[] = {0x00, 0x00};
730 if (sw_bp_inst) {
731 return;
733 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
734 sw_bp_inst = diag_501;
735 sw_bp_ilen = sizeof(diag_501);
736 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
737 } else {
738 sw_bp_inst = instr_0x0000;
739 sw_bp_ilen = sizeof(instr_0x0000);
740 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
744 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
746 determine_sw_breakpoint_instr();
748 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
749 sw_bp_ilen, 0) ||
750 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
751 return -EINVAL;
753 return 0;
756 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
758 uint8_t t[MAX_ILEN];
760 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
761 return -EINVAL;
762 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
763 return -EINVAL;
764 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
765 sw_bp_ilen, 1)) {
766 return -EINVAL;
769 return 0;
772 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
773 int len, int type)
775 int n;
777 for (n = 0; n < nb_hw_breakpoints; n++) {
778 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
779 (hw_breakpoints[n].len == len || len == -1)) {
780 return &hw_breakpoints[n];
784 return NULL;
787 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
789 int size;
791 if (find_hw_breakpoint(addr, len, type)) {
792 return -EEXIST;
795 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
797 if (!hw_breakpoints) {
798 nb_hw_breakpoints = 0;
799 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
800 } else {
801 hw_breakpoints =
802 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
805 if (!hw_breakpoints) {
806 nb_hw_breakpoints = 0;
807 return -ENOMEM;
810 hw_breakpoints[nb_hw_breakpoints].addr = addr;
811 hw_breakpoints[nb_hw_breakpoints].len = len;
812 hw_breakpoints[nb_hw_breakpoints].type = type;
814 nb_hw_breakpoints++;
816 return 0;
819 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
820 target_ulong len, int type)
822 switch (type) {
823 case GDB_BREAKPOINT_HW:
824 type = KVM_HW_BP;
825 break;
826 case GDB_WATCHPOINT_WRITE:
827 if (len < 1) {
828 return -EINVAL;
830 type = KVM_HW_WP_WRITE;
831 break;
832 default:
833 return -ENOSYS;
835 return insert_hw_breakpoint(addr, len, type);
838 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
839 target_ulong len, int type)
841 int size;
842 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
844 if (bp == NULL) {
845 return -ENOENT;
848 nb_hw_breakpoints--;
849 if (nb_hw_breakpoints > 0) {
851 * In order to trim the array, move the last element to the position to
852 * be removed - if necessary.
854 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
855 *bp = hw_breakpoints[nb_hw_breakpoints];
857 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
858 hw_breakpoints =
859 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
860 } else {
861 g_free(hw_breakpoints);
862 hw_breakpoints = NULL;
865 return 0;
868 void kvm_arch_remove_all_hw_breakpoints(void)
870 nb_hw_breakpoints = 0;
871 g_free(hw_breakpoints);
872 hw_breakpoints = NULL;
875 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
877 int i;
879 if (nb_hw_breakpoints > 0) {
880 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
881 dbg->arch.hw_bp = hw_breakpoints;
883 for (i = 0; i < nb_hw_breakpoints; ++i) {
884 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
885 hw_breakpoints[i].addr);
887 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
888 } else {
889 dbg->arch.nr_hw_bp = 0;
890 dbg->arch.hw_bp = NULL;
894 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
898 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
900 return MEMTXATTRS_UNSPECIFIED;
903 int kvm_arch_process_async_events(CPUState *cs)
905 return cs->halted;
908 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
909 struct kvm_s390_interrupt *interrupt)
911 int r = 0;
913 interrupt->type = irq->type;
914 switch (irq->type) {
915 case KVM_S390_INT_VIRTIO:
916 interrupt->parm = irq->u.ext.ext_params;
917 /* fall through */
918 case KVM_S390_INT_PFAULT_INIT:
919 case KVM_S390_INT_PFAULT_DONE:
920 interrupt->parm64 = irq->u.ext.ext_params2;
921 break;
922 case KVM_S390_PROGRAM_INT:
923 interrupt->parm = irq->u.pgm.code;
924 break;
925 case KVM_S390_SIGP_SET_PREFIX:
926 interrupt->parm = irq->u.prefix.address;
927 break;
928 case KVM_S390_INT_SERVICE:
929 interrupt->parm = irq->u.ext.ext_params;
930 break;
931 case KVM_S390_MCHK:
932 interrupt->parm = irq->u.mchk.cr14;
933 interrupt->parm64 = irq->u.mchk.mcic;
934 break;
935 case KVM_S390_INT_EXTERNAL_CALL:
936 interrupt->parm = irq->u.extcall.code;
937 break;
938 case KVM_S390_INT_EMERGENCY:
939 interrupt->parm = irq->u.emerg.code;
940 break;
941 case KVM_S390_SIGP_STOP:
942 case KVM_S390_RESTART:
943 break; /* These types have no parameters */
944 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
945 interrupt->parm = irq->u.io.subchannel_id << 16;
946 interrupt->parm |= irq->u.io.subchannel_nr;
947 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
948 interrupt->parm64 |= irq->u.io.io_int_word;
949 break;
950 default:
951 r = -EINVAL;
952 break;
954 return r;
957 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
959 struct kvm_s390_interrupt kvmint = {};
960 int r;
962 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
963 if (r < 0) {
964 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
965 exit(1);
968 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
969 if (r < 0) {
970 fprintf(stderr, "KVM failed to inject interrupt\n");
971 exit(1);
975 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
977 CPUState *cs = CPU(cpu);
978 int r;
980 if (cap_s390_irq) {
981 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
982 if (!r) {
983 return;
985 error_report("KVM failed to inject interrupt %llx", irq->type);
986 exit(1);
989 inject_vcpu_irq_legacy(cs, irq);
992 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
994 struct kvm_s390_interrupt kvmint = {};
995 int r;
997 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
998 if (r < 0) {
999 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1000 exit(1);
1003 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1004 if (r < 0) {
1005 fprintf(stderr, "KVM failed to inject interrupt\n");
1006 exit(1);
1010 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
1012 static bool use_flic = true;
1013 int r;
1015 if (use_flic) {
1016 r = kvm_s390_inject_flic(irq);
1017 if (r == -ENOSYS) {
1018 use_flic = false;
1020 if (!r) {
1021 return;
1024 __kvm_s390_floating_interrupt(irq);
1027 void kvm_s390_service_interrupt(uint32_t parm)
1029 struct kvm_s390_irq irq = {
1030 .type = KVM_S390_INT_SERVICE,
1031 .u.ext.ext_params = parm,
1034 kvm_s390_floating_interrupt(&irq);
1037 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1039 struct kvm_s390_irq irq = {
1040 .type = KVM_S390_PROGRAM_INT,
1041 .u.pgm.code = code,
1044 kvm_s390_vcpu_interrupt(cpu, &irq);
1047 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1049 struct kvm_s390_irq irq = {
1050 .type = KVM_S390_PROGRAM_INT,
1051 .u.pgm.code = code,
1052 .u.pgm.trans_exc_code = te_code,
1053 .u.pgm.exc_access_id = te_code & 3,
1056 kvm_s390_vcpu_interrupt(cpu, &irq);
1059 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1060 uint16_t ipbh0)
1062 CPUS390XState *env = &cpu->env;
1063 uint64_t sccb;
1064 uint32_t code;
1065 int r = 0;
1067 cpu_synchronize_state(CPU(cpu));
1068 sccb = env->regs[ipbh0 & 0xf];
1069 code = env->regs[(ipbh0 & 0xf0) >> 4];
1071 r = sclp_service_call(env, sccb, code);
1072 if (r < 0) {
1073 kvm_s390_program_interrupt(cpu, -r);
1074 } else {
1075 setcc(cpu, r);
1078 return 0;
1081 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1083 CPUS390XState *env = &cpu->env;
1084 int rc = 0;
1085 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1087 cpu_synchronize_state(CPU(cpu));
1089 switch (ipa1) {
1090 case PRIV_B2_XSCH:
1091 ioinst_handle_xsch(cpu, env->regs[1]);
1092 break;
1093 case PRIV_B2_CSCH:
1094 ioinst_handle_csch(cpu, env->regs[1]);
1095 break;
1096 case PRIV_B2_HSCH:
1097 ioinst_handle_hsch(cpu, env->regs[1]);
1098 break;
1099 case PRIV_B2_MSCH:
1100 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1101 break;
1102 case PRIV_B2_SSCH:
1103 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1104 break;
1105 case PRIV_B2_STCRW:
1106 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1107 break;
1108 case PRIV_B2_STSCH:
1109 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1110 break;
1111 case PRIV_B2_TSCH:
1112 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1113 fprintf(stderr, "Spurious tsch intercept\n");
1114 break;
1115 case PRIV_B2_CHSC:
1116 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1117 break;
1118 case PRIV_B2_TPI:
1119 /* This should have been handled by kvm already. */
1120 fprintf(stderr, "Spurious tpi intercept\n");
1121 break;
1122 case PRIV_B2_SCHM:
1123 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1124 run->s390_sieic.ipb);
1125 break;
1126 case PRIV_B2_RSCH:
1127 ioinst_handle_rsch(cpu, env->regs[1]);
1128 break;
1129 case PRIV_B2_RCHP:
1130 ioinst_handle_rchp(cpu, env->regs[1]);
1131 break;
1132 case PRIV_B2_STCPS:
1133 /* We do not provide this instruction, it is suppressed. */
1134 break;
1135 case PRIV_B2_SAL:
1136 ioinst_handle_sal(cpu, env->regs[1]);
1137 break;
1138 case PRIV_B2_SIGA:
1139 /* Not provided, set CC = 3 for subchannel not operational */
1140 setcc(cpu, 3);
1141 break;
1142 case PRIV_B2_SCLP_CALL:
1143 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1144 break;
1145 default:
1146 rc = -1;
1147 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1148 break;
1151 return rc;
1154 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1155 uint8_t *ar)
1157 CPUS390XState *env = &cpu->env;
1158 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1159 uint32_t base2 = run->s390_sieic.ipb >> 28;
1160 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1161 ((run->s390_sieic.ipb & 0xff00) << 4);
1163 if (disp2 & 0x80000) {
1164 disp2 += 0xfff00000;
1166 if (ar) {
1167 *ar = base2;
1170 return (base2 ? env->regs[base2] : 0) +
1171 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1174 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1175 uint8_t *ar)
1177 CPUS390XState *env = &cpu->env;
1178 uint32_t base2 = run->s390_sieic.ipb >> 28;
1179 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1180 ((run->s390_sieic.ipb & 0xff00) << 4);
1182 if (disp2 & 0x80000) {
1183 disp2 += 0xfff00000;
1185 if (ar) {
1186 *ar = base2;
1189 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1192 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1194 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1196 if (s390_has_feat(S390_FEAT_ZPCI)) {
1197 return clp_service_call(cpu, r2);
1198 } else {
1199 return -1;
1203 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1205 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1206 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1208 if (s390_has_feat(S390_FEAT_ZPCI)) {
1209 return pcilg_service_call(cpu, r1, r2);
1210 } else {
1211 return -1;
1215 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1217 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1218 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1220 if (s390_has_feat(S390_FEAT_ZPCI)) {
1221 return pcistg_service_call(cpu, r1, r2);
1222 } else {
1223 return -1;
1227 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1229 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1230 uint64_t fiba;
1231 uint8_t ar;
1233 if (s390_has_feat(S390_FEAT_ZPCI)) {
1234 cpu_synchronize_state(CPU(cpu));
1235 fiba = get_base_disp_rxy(cpu, run, &ar);
1237 return stpcifc_service_call(cpu, r1, fiba, ar);
1238 } else {
1239 return -1;
1243 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1245 CPUS390XState *env = &cpu->env;
1246 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1247 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1248 uint8_t isc;
1249 uint16_t mode;
1250 int r;
1252 cpu_synchronize_state(CPU(cpu));
1253 mode = env->regs[r1] & 0xffff;
1254 isc = (env->regs[r3] >> 27) & 0x7;
1255 r = css_do_sic(env, isc, mode);
1256 if (r) {
1257 kvm_s390_program_interrupt(cpu, -r);
1260 return 0;
1263 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1265 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1266 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1268 if (s390_has_feat(S390_FEAT_ZPCI)) {
1269 return rpcit_service_call(cpu, r1, r2);
1270 } else {
1271 return -1;
1275 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1277 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1278 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1279 uint64_t gaddr;
1280 uint8_t ar;
1282 if (s390_has_feat(S390_FEAT_ZPCI)) {
1283 cpu_synchronize_state(CPU(cpu));
1284 gaddr = get_base_disp_rsy(cpu, run, &ar);
1286 return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1287 } else {
1288 return -1;
1292 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1294 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1295 uint64_t fiba;
1296 uint8_t ar;
1298 if (s390_has_feat(S390_FEAT_ZPCI)) {
1299 cpu_synchronize_state(CPU(cpu));
1300 fiba = get_base_disp_rxy(cpu, run, &ar);
1302 return mpcifc_service_call(cpu, r1, fiba, ar);
1303 } else {
1304 return -1;
1308 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1310 int r = 0;
1312 switch (ipa1) {
1313 case PRIV_B9_CLP:
1314 r = kvm_clp_service_call(cpu, run);
1315 break;
1316 case PRIV_B9_PCISTG:
1317 r = kvm_pcistg_service_call(cpu, run);
1318 break;
1319 case PRIV_B9_PCILG:
1320 r = kvm_pcilg_service_call(cpu, run);
1321 break;
1322 case PRIV_B9_RPCIT:
1323 r = kvm_rpcit_service_call(cpu, run);
1324 break;
1325 case PRIV_B9_EQBS:
1326 /* just inject exception */
1327 r = -1;
1328 break;
1329 default:
1330 r = -1;
1331 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1332 break;
1335 return r;
1338 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1340 int r = 0;
1342 switch (ipbl) {
1343 case PRIV_EB_PCISTB:
1344 r = kvm_pcistb_service_call(cpu, run);
1345 break;
1346 case PRIV_EB_SIC:
1347 r = kvm_sic_service_call(cpu, run);
1348 break;
1349 case PRIV_EB_SQBS:
1350 /* just inject exception */
1351 r = -1;
1352 break;
1353 default:
1354 r = -1;
1355 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1356 break;
1359 return r;
1362 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1364 int r = 0;
1366 switch (ipbl) {
1367 case PRIV_E3_MPCIFC:
1368 r = kvm_mpcifc_service_call(cpu, run);
1369 break;
1370 case PRIV_E3_STPCIFC:
1371 r = kvm_stpcifc_service_call(cpu, run);
1372 break;
1373 default:
1374 r = -1;
1375 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1376 break;
1379 return r;
1382 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1384 CPUS390XState *env = &cpu->env;
1385 int ret;
1387 cpu_synchronize_state(CPU(cpu));
1388 ret = s390_virtio_hypercall(env);
1389 if (ret == -EINVAL) {
1390 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1391 return 0;
1394 return ret;
1397 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1399 uint64_t r1, r3;
1400 int rc;
1402 cpu_synchronize_state(CPU(cpu));
1403 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1404 r3 = run->s390_sieic.ipa & 0x000f;
1405 rc = handle_diag_288(&cpu->env, r1, r3);
1406 if (rc) {
1407 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1411 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1413 uint64_t r1, r3;
1415 cpu_synchronize_state(CPU(cpu));
1416 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1417 r3 = run->s390_sieic.ipa & 0x000f;
1418 handle_diag_308(&cpu->env, r1, r3);
1421 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1423 CPUS390XState *env = &cpu->env;
1424 unsigned long pc;
1426 cpu_synchronize_state(CPU(cpu));
1428 pc = env->psw.addr - sw_bp_ilen;
1429 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1430 env->psw.addr = pc;
1431 return EXCP_DEBUG;
1434 return -ENOENT;
1437 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1439 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1441 int r = 0;
1442 uint16_t func_code;
1445 * For any diagnose call we support, bits 48-63 of the resulting
1446 * address specify the function code; the remainder is ignored.
1448 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1449 switch (func_code) {
1450 case DIAG_TIMEREVENT:
1451 kvm_handle_diag_288(cpu, run);
1452 break;
1453 case DIAG_IPL:
1454 kvm_handle_diag_308(cpu, run);
1455 break;
1456 case DIAG_KVM_HYPERCALL:
1457 r = handle_hypercall(cpu, run);
1458 break;
1459 case DIAG_KVM_BREAKPOINT:
1460 r = handle_sw_breakpoint(cpu, run);
1461 break;
1462 default:
1463 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1464 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1465 break;
1468 return r;
1471 typedef struct SigpInfo {
1472 uint64_t param;
1473 int cc;
1474 uint64_t *status_reg;
1475 } SigpInfo;
1477 static void set_sigp_status(SigpInfo *si, uint64_t status)
1479 *si->status_reg &= 0xffffffff00000000ULL;
1480 *si->status_reg |= status;
1481 si->cc = SIGP_CC_STATUS_STORED;
1484 static void sigp_start(CPUState *cs, run_on_cpu_data arg)
1486 S390CPU *cpu = S390_CPU(cs);
1487 SigpInfo *si = arg.host_ptr;
1489 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1490 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1491 return;
1494 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1495 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1498 static void sigp_stop(CPUState *cs, run_on_cpu_data arg)
1500 S390CPU *cpu = S390_CPU(cs);
1501 SigpInfo *si = arg.host_ptr;
1502 struct kvm_s390_irq irq = {
1503 .type = KVM_S390_SIGP_STOP,
1506 if (s390_cpu_get_state(cpu) != CPU_STATE_OPERATING) {
1507 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1508 return;
1511 /* disabled wait - sleeping in user space */
1512 if (cs->halted) {
1513 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
1514 } else {
1515 /* execute the stop function */
1516 cpu->env.sigp_order = SIGP_STOP;
1517 kvm_s390_vcpu_interrupt(cpu, &irq);
1519 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1522 #define ADTL_GS_OFFSET 1024 /* offset of GS data in adtl save area */
1523 #define ADTL_GS_MIN_SIZE 2048 /* minimal size of adtl save area for GS */
1524 static int do_store_adtl_status(S390CPU *cpu, hwaddr addr, hwaddr len)
1526 hwaddr save = len;
1527 void *mem;
1529 mem = cpu_physical_memory_map(addr, &save, 1);
1530 if (!mem) {
1531 return -EFAULT;
1533 if (save != len) {
1534 cpu_physical_memory_unmap(mem, len, 1, 0);
1535 return -EFAULT;
1538 if (s390_has_feat(S390_FEAT_VECTOR)) {
1539 memcpy(mem, &cpu->env.vregs, 512);
1541 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE) && len >= ADTL_GS_MIN_SIZE) {
1542 memcpy(mem + ADTL_GS_OFFSET, &cpu->env.gscb, 32);
1545 cpu_physical_memory_unmap(mem, len, 1, len);
1547 return 0;
1550 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1551 #define SAVE_AREA_SIZE 512
1552 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1554 static const uint8_t ar_id = 1;
1555 uint64_t ckc = cpu->env.ckc >> 8;
1556 void *mem;
1557 int i;
1558 hwaddr len = SAVE_AREA_SIZE;
1560 mem = cpu_physical_memory_map(addr, &len, 1);
1561 if (!mem) {
1562 return -EFAULT;
1564 if (len != SAVE_AREA_SIZE) {
1565 cpu_physical_memory_unmap(mem, len, 1, 0);
1566 return -EFAULT;
1569 if (store_arch) {
1570 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1572 for (i = 0; i < 16; ++i) {
1573 *((uint64_t *)mem + i) = get_freg(&cpu->env, i)->ll;
1575 memcpy(mem + 128, &cpu->env.regs, 128);
1576 memcpy(mem + 256, &cpu->env.psw, 16);
1577 memcpy(mem + 280, &cpu->env.psa, 4);
1578 memcpy(mem + 284, &cpu->env.fpc, 4);
1579 memcpy(mem + 292, &cpu->env.todpr, 4);
1580 memcpy(mem + 296, &cpu->env.cputm, 8);
1581 memcpy(mem + 304, &ckc, 8);
1582 memcpy(mem + 320, &cpu->env.aregs, 64);
1583 memcpy(mem + 384, &cpu->env.cregs, 128);
1585 cpu_physical_memory_unmap(mem, len, 1, len);
1587 return 0;
1590 static void sigp_stop_and_store_status(CPUState *cs, run_on_cpu_data arg)
1592 S390CPU *cpu = S390_CPU(cs);
1593 SigpInfo *si = arg.host_ptr;
1594 struct kvm_s390_irq irq = {
1595 .type = KVM_S390_SIGP_STOP,
1598 /* disabled wait - sleeping in user space */
1599 if (s390_cpu_get_state(cpu) == CPU_STATE_OPERATING && cs->halted) {
1600 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
1603 switch (s390_cpu_get_state(cpu)) {
1604 case CPU_STATE_OPERATING:
1605 cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1606 kvm_s390_vcpu_interrupt(cpu, &irq);
1607 /* store will be performed when handling the stop intercept */
1608 break;
1609 case CPU_STATE_STOPPED:
1610 /* already stopped, just store the status */
1611 cpu_synchronize_state(cs);
1612 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1613 break;
1615 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1618 static void sigp_store_status_at_address(CPUState *cs, run_on_cpu_data arg)
1620 S390CPU *cpu = S390_CPU(cs);
1621 SigpInfo *si = arg.host_ptr;
1622 uint32_t address = si->param & 0x7ffffe00u;
1624 /* cpu has to be stopped */
1625 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1626 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1627 return;
1630 cpu_synchronize_state(cs);
1632 if (kvm_s390_store_status(cpu, address, false)) {
1633 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1634 return;
1636 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1639 #define ADTL_SAVE_LC_MASK 0xfUL
1640 static void sigp_store_adtl_status(CPUState *cs, run_on_cpu_data arg)
1642 S390CPU *cpu = S390_CPU(cs);
1643 SigpInfo *si = arg.host_ptr;
1644 uint8_t lc = si->param & ADTL_SAVE_LC_MASK;
1645 hwaddr addr = si->param & ~ADTL_SAVE_LC_MASK;
1646 hwaddr len = 1UL << (lc ? lc : 10);
1648 if (!s390_has_feat(S390_FEAT_VECTOR) &&
1649 !s390_has_feat(S390_FEAT_GUARDED_STORAGE)) {
1650 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1651 return;
1654 /* cpu has to be stopped */
1655 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1656 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1657 return;
1660 /* address must be aligned to length */
1661 if (addr & (len - 1)) {
1662 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1663 return;
1666 /* no GS: only lc == 0 is valid */
1667 if (!s390_has_feat(S390_FEAT_GUARDED_STORAGE) &&
1668 lc != 0) {
1669 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1670 return;
1673 /* GS: 0, 10, 11, 12 are valid */
1674 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE) &&
1675 lc != 0 &&
1676 lc != 10 &&
1677 lc != 11 &&
1678 lc != 12) {
1679 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1680 return;
1683 cpu_synchronize_state(cs);
1685 if (do_store_adtl_status(cpu, addr, len)) {
1686 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1687 return;
1689 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1692 static void sigp_restart(CPUState *cs, run_on_cpu_data arg)
1694 S390CPU *cpu = S390_CPU(cs);
1695 SigpInfo *si = arg.host_ptr;
1696 struct kvm_s390_irq irq = {
1697 .type = KVM_S390_RESTART,
1700 switch (s390_cpu_get_state(cpu)) {
1701 case CPU_STATE_STOPPED:
1702 /* the restart irq has to be delivered prior to any other pending irq */
1703 cpu_synchronize_state(cs);
1704 do_restart_interrupt(&cpu->env);
1705 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1706 break;
1707 case CPU_STATE_OPERATING:
1708 kvm_s390_vcpu_interrupt(cpu, &irq);
1709 break;
1711 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1714 int kvm_s390_cpu_restart(S390CPU *cpu)
1716 SigpInfo si = {};
1718 run_on_cpu(CPU(cpu), sigp_restart, RUN_ON_CPU_HOST_PTR(&si));
1719 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1720 return 0;
1723 static void sigp_initial_cpu_reset(CPUState *cs, run_on_cpu_data arg)
1725 S390CPU *cpu = S390_CPU(cs);
1726 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1727 SigpInfo *si = arg.host_ptr;
1729 cpu_synchronize_state(cs);
1730 scc->initial_cpu_reset(cs);
1731 cpu_synchronize_post_reset(cs);
1732 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1735 static void sigp_cpu_reset(CPUState *cs, run_on_cpu_data arg)
1737 S390CPU *cpu = S390_CPU(cs);
1738 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1739 SigpInfo *si = arg.host_ptr;
1741 cpu_synchronize_state(cs);
1742 scc->cpu_reset(cs);
1743 cpu_synchronize_post_reset(cs);
1744 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1747 static void sigp_set_prefix(CPUState *cs, run_on_cpu_data arg)
1749 S390CPU *cpu = S390_CPU(cs);
1750 SigpInfo *si = arg.host_ptr;
1751 uint32_t addr = si->param & 0x7fffe000u;
1753 cpu_synchronize_state(cs);
1755 if (!address_space_access_valid(&address_space_memory, addr,
1756 sizeof(struct LowCore), false)) {
1757 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1758 return;
1761 /* cpu has to be stopped */
1762 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1763 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1764 return;
1767 cpu->env.psa = addr;
1768 cpu_synchronize_post_init(cs);
1769 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1772 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1773 uint64_t param, uint64_t *status_reg)
1775 SigpInfo si = {
1776 .param = param,
1777 .status_reg = status_reg,
1780 /* cpu available? */
1781 if (dst_cpu == NULL) {
1782 return SIGP_CC_NOT_OPERATIONAL;
1785 /* only resets can break pending orders */
1786 if (dst_cpu->env.sigp_order != 0 &&
1787 order != SIGP_CPU_RESET &&
1788 order != SIGP_INITIAL_CPU_RESET) {
1789 return SIGP_CC_BUSY;
1792 switch (order) {
1793 case SIGP_START:
1794 run_on_cpu(CPU(dst_cpu), sigp_start, RUN_ON_CPU_HOST_PTR(&si));
1795 break;
1796 case SIGP_STOP:
1797 run_on_cpu(CPU(dst_cpu), sigp_stop, RUN_ON_CPU_HOST_PTR(&si));
1798 break;
1799 case SIGP_RESTART:
1800 run_on_cpu(CPU(dst_cpu), sigp_restart, RUN_ON_CPU_HOST_PTR(&si));
1801 break;
1802 case SIGP_STOP_STORE_STATUS:
1803 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, RUN_ON_CPU_HOST_PTR(&si));
1804 break;
1805 case SIGP_STORE_STATUS_ADDR:
1806 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, RUN_ON_CPU_HOST_PTR(&si));
1807 break;
1808 case SIGP_STORE_ADTL_STATUS:
1809 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, RUN_ON_CPU_HOST_PTR(&si));
1810 break;
1811 case SIGP_SET_PREFIX:
1812 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, RUN_ON_CPU_HOST_PTR(&si));
1813 break;
1814 case SIGP_INITIAL_CPU_RESET:
1815 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, RUN_ON_CPU_HOST_PTR(&si));
1816 break;
1817 case SIGP_CPU_RESET:
1818 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, RUN_ON_CPU_HOST_PTR(&si));
1819 break;
1820 default:
1821 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1822 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1825 return si.cc;
1828 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1829 uint64_t *status_reg)
1831 CPUState *cur_cs;
1832 S390CPU *cur_cpu;
1833 bool all_stopped = true;
1835 CPU_FOREACH(cur_cs) {
1836 cur_cpu = S390_CPU(cur_cs);
1838 if (cur_cpu == cpu) {
1839 continue;
1841 if (s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1842 all_stopped = false;
1846 *status_reg &= 0xffffffff00000000ULL;
1848 /* Reject set arch order, with czam we're always in z/Arch mode. */
1849 *status_reg |= (all_stopped ? SIGP_STAT_INVALID_PARAMETER :
1850 SIGP_STAT_INCORRECT_STATE);
1851 return SIGP_CC_STATUS_STORED;
1854 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1856 CPUS390XState *env = &cpu->env;
1857 const uint8_t r1 = ipa1 >> 4;
1858 const uint8_t r3 = ipa1 & 0x0f;
1859 int ret;
1860 uint8_t order;
1861 uint64_t *status_reg;
1862 uint64_t param;
1863 S390CPU *dst_cpu = NULL;
1865 cpu_synchronize_state(CPU(cpu));
1867 /* get order code */
1868 order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1869 & SIGP_ORDER_MASK;
1870 status_reg = &env->regs[r1];
1871 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1873 if (qemu_mutex_trylock(&qemu_sigp_mutex)) {
1874 ret = SIGP_CC_BUSY;
1875 goto out;
1878 switch (order) {
1879 case SIGP_SET_ARCH:
1880 ret = sigp_set_architecture(cpu, param, status_reg);
1881 break;
1882 default:
1883 /* all other sigp orders target a single vcpu */
1884 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1885 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1887 qemu_mutex_unlock(&qemu_sigp_mutex);
1889 out:
1890 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1891 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1893 if (ret >= 0) {
1894 setcc(cpu, ret);
1895 return 0;
1898 return ret;
1901 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1903 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1904 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1905 int r = -1;
1907 DPRINTF("handle_instruction 0x%x 0x%x\n",
1908 run->s390_sieic.ipa, run->s390_sieic.ipb);
1909 switch (ipa0) {
1910 case IPA0_B2:
1911 r = handle_b2(cpu, run, ipa1);
1912 break;
1913 case IPA0_B9:
1914 r = handle_b9(cpu, run, ipa1);
1915 break;
1916 case IPA0_EB:
1917 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1918 break;
1919 case IPA0_E3:
1920 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1921 break;
1922 case IPA0_DIAG:
1923 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1924 break;
1925 case IPA0_SIGP:
1926 r = handle_sigp(cpu, run, ipa1);
1927 break;
1930 if (r < 0) {
1931 r = 0;
1932 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1935 return r;
1938 static bool is_special_wait_psw(CPUState *cs)
1940 /* signal quiesce */
1941 return cs->kvm_run->psw_addr == 0xfffUL;
1944 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1946 CPUState *cs = CPU(cpu);
1948 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1949 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1950 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1951 s390_cpu_halt(cpu);
1952 qemu_system_guest_panicked(NULL);
1955 /* try to detect pgm check loops */
1956 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1958 CPUState *cs = CPU(cpu);
1959 PSW oldpsw, newpsw;
1961 cpu_synchronize_state(cs);
1962 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1963 offsetof(LowCore, program_new_psw));
1964 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1965 offsetof(LowCore, program_new_psw) + 8);
1966 oldpsw.mask = run->psw_mask;
1967 oldpsw.addr = run->psw_addr;
1969 * Avoid endless loops of operation exceptions, if the pgm new
1970 * PSW will cause a new operation exception.
1971 * The heuristic checks if the pgm new psw is within 6 bytes before
1972 * the faulting psw address (with same DAT, AS settings) and the
1973 * new psw is not a wait psw and the fault was not triggered by
1974 * problem state. In that case go into crashed state.
1977 if (oldpsw.addr - newpsw.addr <= 6 &&
1978 !(newpsw.mask & PSW_MASK_WAIT) &&
1979 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1980 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1981 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1982 unmanageable_intercept(cpu, "operation exception loop",
1983 offsetof(LowCore, program_new_psw));
1984 return EXCP_HALTED;
1986 return 0;
1989 static int handle_intercept(S390CPU *cpu)
1991 CPUState *cs = CPU(cpu);
1992 struct kvm_run *run = cs->kvm_run;
1993 int icpt_code = run->s390_sieic.icptcode;
1994 int r = 0;
1996 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1997 (long)cs->kvm_run->psw_addr);
1998 switch (icpt_code) {
1999 case ICPT_INSTRUCTION:
2000 r = handle_instruction(cpu, run);
2001 break;
2002 case ICPT_PROGRAM:
2003 unmanageable_intercept(cpu, "program interrupt",
2004 offsetof(LowCore, program_new_psw));
2005 r = EXCP_HALTED;
2006 break;
2007 case ICPT_EXT_INT:
2008 unmanageable_intercept(cpu, "external interrupt",
2009 offsetof(LowCore, external_new_psw));
2010 r = EXCP_HALTED;
2011 break;
2012 case ICPT_WAITPSW:
2013 /* disabled wait, since enabled wait is handled in kernel */
2014 cpu_synchronize_state(cs);
2015 if (s390_cpu_halt(cpu) == 0) {
2016 if (is_special_wait_psw(cs)) {
2017 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
2018 } else {
2019 qemu_system_guest_panicked(NULL);
2022 r = EXCP_HALTED;
2023 break;
2024 case ICPT_CPU_STOP:
2025 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
2026 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
2028 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
2029 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
2030 true);
2032 cpu->env.sigp_order = 0;
2033 r = EXCP_HALTED;
2034 break;
2035 case ICPT_OPEREXC:
2036 /* check for break points */
2037 r = handle_sw_breakpoint(cpu, run);
2038 if (r == -ENOENT) {
2039 /* Then check for potential pgm check loops */
2040 r = handle_oper_loop(cpu, run);
2041 if (r == 0) {
2042 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
2045 break;
2046 case ICPT_SOFT_INTERCEPT:
2047 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
2048 exit(1);
2049 break;
2050 case ICPT_IO:
2051 fprintf(stderr, "KVM unimplemented icpt IO\n");
2052 exit(1);
2053 break;
2054 default:
2055 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
2056 exit(1);
2057 break;
2060 return r;
2063 static int handle_tsch(S390CPU *cpu)
2065 CPUState *cs = CPU(cpu);
2066 struct kvm_run *run = cs->kvm_run;
2067 int ret;
2069 cpu_synchronize_state(cs);
2071 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
2072 if (ret < 0) {
2074 * Failure.
2075 * If an I/O interrupt had been dequeued, we have to reinject it.
2077 if (run->s390_tsch.dequeued) {
2078 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
2079 run->s390_tsch.subchannel_nr,
2080 run->s390_tsch.io_int_parm,
2081 run->s390_tsch.io_int_word);
2083 ret = 0;
2085 return ret;
2088 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
2090 struct sysib_322 sysib;
2091 int del;
2093 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
2094 return;
2096 /* Shift the stack of Extended Names to prepare for our own data */
2097 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
2098 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
2099 /* First virt level, that doesn't provide Ext Names delimits stack. It is
2100 * assumed it's not capable of managing Extended Names for lower levels.
2102 for (del = 1; del < sysib.count; del++) {
2103 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
2104 break;
2107 if (del < sysib.count) {
2108 memset(sysib.ext_names[del], 0,
2109 sizeof(sysib.ext_names[0]) * (sysib.count - del));
2111 /* Insert short machine name in EBCDIC, padded with blanks */
2112 if (qemu_name) {
2113 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
2114 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
2115 strlen(qemu_name)));
2117 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
2118 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
2119 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
2120 * considered by s390 as not capable of providing any Extended Name.
2121 * Therefore if no name was specified on qemu invocation, we go with the
2122 * same "KVMguest" default, which KVM has filled into short name field.
2124 if (qemu_name) {
2125 strncpy((char *)sysib.ext_names[0], qemu_name,
2126 sizeof(sysib.ext_names[0]));
2127 } else {
2128 strcpy((char *)sysib.ext_names[0], "KVMguest");
2130 /* Insert UUID */
2131 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
2133 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
2136 static int handle_stsi(S390CPU *cpu)
2138 CPUState *cs = CPU(cpu);
2139 struct kvm_run *run = cs->kvm_run;
2141 switch (run->s390_stsi.fc) {
2142 case 3:
2143 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
2144 return 0;
2146 /* Only sysib 3.2.2 needs post-handling for now. */
2147 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
2148 return 0;
2149 default:
2150 return 0;
2154 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
2156 CPUState *cs = CPU(cpu);
2157 struct kvm_run *run = cs->kvm_run;
2159 int ret = 0;
2160 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
2162 switch (arch_info->type) {
2163 case KVM_HW_WP_WRITE:
2164 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
2165 cs->watchpoint_hit = &hw_watchpoint;
2166 hw_watchpoint.vaddr = arch_info->addr;
2167 hw_watchpoint.flags = BP_MEM_WRITE;
2168 ret = EXCP_DEBUG;
2170 break;
2171 case KVM_HW_BP:
2172 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
2173 ret = EXCP_DEBUG;
2175 break;
2176 case KVM_SINGLESTEP:
2177 if (cs->singlestep_enabled) {
2178 ret = EXCP_DEBUG;
2180 break;
2181 default:
2182 ret = -ENOSYS;
2185 return ret;
2188 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
2190 S390CPU *cpu = S390_CPU(cs);
2191 int ret = 0;
2193 qemu_mutex_lock_iothread();
2195 switch (run->exit_reason) {
2196 case KVM_EXIT_S390_SIEIC:
2197 ret = handle_intercept(cpu);
2198 break;
2199 case KVM_EXIT_S390_RESET:
2200 s390_reipl_request();
2201 break;
2202 case KVM_EXIT_S390_TSCH:
2203 ret = handle_tsch(cpu);
2204 break;
2205 case KVM_EXIT_S390_STSI:
2206 ret = handle_stsi(cpu);
2207 break;
2208 case KVM_EXIT_DEBUG:
2209 ret = kvm_arch_handle_debug_exit(cpu);
2210 break;
2211 default:
2212 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2213 break;
2215 qemu_mutex_unlock_iothread();
2217 if (ret == 0) {
2218 ret = EXCP_INTERRUPT;
2220 return ret;
2223 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2225 return true;
2228 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2229 uint16_t subchannel_nr, uint32_t io_int_parm,
2230 uint32_t io_int_word)
2232 struct kvm_s390_irq irq = {
2233 .u.io.subchannel_id = subchannel_id,
2234 .u.io.subchannel_nr = subchannel_nr,
2235 .u.io.io_int_parm = io_int_parm,
2236 .u.io.io_int_word = io_int_word,
2239 if (io_int_word & IO_INT_WORD_AI) {
2240 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2241 } else {
2242 irq.type = KVM_S390_INT_IO(0, (subchannel_id & 0xff00) >> 8,
2243 (subchannel_id & 0x0006),
2244 subchannel_nr);
2246 kvm_s390_floating_interrupt(&irq);
2249 static uint64_t build_channel_report_mcic(void)
2251 uint64_t mcic;
2253 /* subclass: indicate channel report pending */
2254 mcic = MCIC_SC_CP |
2255 /* subclass modifiers: none */
2256 /* storage errors: none */
2257 /* validity bits: no damage */
2258 MCIC_VB_WP | MCIC_VB_MS | MCIC_VB_PM | MCIC_VB_IA | MCIC_VB_FP |
2259 MCIC_VB_GR | MCIC_VB_CR | MCIC_VB_ST | MCIC_VB_AR | MCIC_VB_PR |
2260 MCIC_VB_FC | MCIC_VB_CT | MCIC_VB_CC;
2261 if (s390_has_feat(S390_FEAT_VECTOR)) {
2262 mcic |= MCIC_VB_VR;
2264 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE)) {
2265 mcic |= MCIC_VB_GS;
2267 return mcic;
2270 void kvm_s390_crw_mchk(void)
2272 struct kvm_s390_irq irq = {
2273 .type = KVM_S390_MCHK,
2274 .u.mchk.cr14 = 1 << 28,
2275 .u.mchk.mcic = build_channel_report_mcic(),
2277 kvm_s390_floating_interrupt(&irq);
2280 void kvm_s390_enable_css_support(S390CPU *cpu)
2282 int r;
2284 /* Activate host kernel channel subsystem support. */
2285 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2286 assert(r == 0);
2289 void kvm_arch_init_irq_routing(KVMState *s)
2292 * Note that while irqchip capabilities generally imply that cpustates
2293 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2294 * have to override the common code kvm_halt_in_kernel_allowed setting.
2296 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2297 kvm_gsi_routing_allowed = true;
2298 kvm_halt_in_kernel_allowed = false;
2302 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2303 int vq, bool assign)
2305 struct kvm_ioeventfd kick = {
2306 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2307 KVM_IOEVENTFD_FLAG_DATAMATCH,
2308 .fd = event_notifier_get_fd(notifier),
2309 .datamatch = vq,
2310 .addr = sch,
2311 .len = 8,
2313 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2314 return -ENOSYS;
2316 if (!assign) {
2317 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2319 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2322 int kvm_s390_get_memslot_count(void)
2324 return kvm_check_extension(kvm_state, KVM_CAP_NR_MEMSLOTS);
2327 int kvm_s390_get_ri(void)
2329 return cap_ri;
2332 int kvm_s390_get_gs(void)
2334 return cap_gs;
2337 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2339 struct kvm_mp_state mp_state = {};
2340 int ret;
2342 /* the kvm part might not have been initialized yet */
2343 if (CPU(cpu)->kvm_state == NULL) {
2344 return 0;
2347 switch (cpu_state) {
2348 case CPU_STATE_STOPPED:
2349 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2350 break;
2351 case CPU_STATE_CHECK_STOP:
2352 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2353 break;
2354 case CPU_STATE_OPERATING:
2355 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2356 break;
2357 case CPU_STATE_LOAD:
2358 mp_state.mp_state = KVM_MP_STATE_LOAD;
2359 break;
2360 default:
2361 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2362 cpu_state);
2363 exit(1);
2366 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2367 if (ret) {
2368 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2369 strerror(-ret));
2372 return ret;
2375 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2377 struct kvm_s390_irq_state irq_state;
2378 CPUState *cs = CPU(cpu);
2379 int32_t bytes;
2381 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2382 return;
2385 irq_state.buf = (uint64_t) cpu->irqstate;
2386 irq_state.len = VCPU_IRQ_BUF_SIZE;
2388 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2389 if (bytes < 0) {
2390 cpu->irqstate_saved_size = 0;
2391 error_report("Migration of interrupt state failed");
2392 return;
2395 cpu->irqstate_saved_size = bytes;
2398 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2400 CPUState *cs = CPU(cpu);
2401 struct kvm_s390_irq_state irq_state;
2402 int r;
2404 if (cpu->irqstate_saved_size == 0) {
2405 return 0;
2408 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2409 return -ENOSYS;
2412 irq_state.buf = (uint64_t) cpu->irqstate;
2413 irq_state.len = cpu->irqstate_saved_size;
2415 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2416 if (r) {
2417 error_report("Setting interrupt state failed %d", r);
2419 return r;
2422 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2423 uint64_t address, uint32_t data, PCIDevice *dev)
2425 S390PCIBusDevice *pbdev;
2426 uint32_t idx = data >> ZPCI_MSI_VEC_BITS;
2427 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2429 pbdev = s390_pci_find_dev_by_idx(s390_get_phb(), idx);
2430 if (!pbdev) {
2431 DPRINTF("add_msi_route no dev\n");
2432 return -ENODEV;
2435 pbdev->routes.adapter.ind_offset = vec;
2437 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2438 route->flags = 0;
2439 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2440 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2441 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2442 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2443 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2444 return 0;
2447 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2448 int vector, PCIDevice *dev)
2450 return 0;
2453 int kvm_arch_release_virq_post(int virq)
2455 return 0;
2458 int kvm_arch_msi_data_to_gsi(uint32_t data)
2460 abort();
2463 static int query_cpu_subfunc(S390FeatBitmap features)
2465 struct kvm_s390_vm_cpu_subfunc prop;
2466 struct kvm_device_attr attr = {
2467 .group = KVM_S390_VM_CPU_MODEL,
2468 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2469 .addr = (uint64_t) &prop,
2471 int rc;
2473 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2474 if (rc) {
2475 return rc;
2479 * We're going to add all subfunctions now, if the corresponding feature
2480 * is available that unlocks the query functions.
2482 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2483 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2484 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2486 if (test_bit(S390_FEAT_MSA, features)) {
2487 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2488 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2489 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2490 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2491 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2493 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2494 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2496 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2497 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2498 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2499 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2500 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2502 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2503 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2505 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2506 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2508 return 0;
2511 static int configure_cpu_subfunc(const S390FeatBitmap features)
2513 struct kvm_s390_vm_cpu_subfunc prop = {};
2514 struct kvm_device_attr attr = {
2515 .group = KVM_S390_VM_CPU_MODEL,
2516 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2517 .addr = (uint64_t) &prop,
2520 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2521 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2522 /* hardware support might be missing, IBC will handle most of this */
2523 return 0;
2526 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2527 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2528 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2530 if (test_bit(S390_FEAT_MSA, features)) {
2531 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2532 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2533 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2534 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2535 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2537 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2538 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2540 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2541 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2542 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2543 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2544 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2546 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2547 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2549 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2550 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2552 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2555 static int kvm_to_feat[][2] = {
2556 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2557 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2558 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2559 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2560 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2561 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2562 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2563 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2564 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2565 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2566 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2567 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2568 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2569 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2572 static int query_cpu_feat(S390FeatBitmap features)
2574 struct kvm_s390_vm_cpu_feat prop;
2575 struct kvm_device_attr attr = {
2576 .group = KVM_S390_VM_CPU_MODEL,
2577 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2578 .addr = (uint64_t) &prop,
2580 int rc;
2581 int i;
2583 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2584 if (rc) {
2585 return rc;
2588 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2589 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2590 set_bit(kvm_to_feat[i][1], features);
2593 return 0;
2596 static int configure_cpu_feat(const S390FeatBitmap features)
2598 struct kvm_s390_vm_cpu_feat prop = {};
2599 struct kvm_device_attr attr = {
2600 .group = KVM_S390_VM_CPU_MODEL,
2601 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2602 .addr = (uint64_t) &prop,
2604 int i;
2606 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2607 if (test_bit(kvm_to_feat[i][1], features)) {
2608 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2611 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2614 bool kvm_s390_cpu_models_supported(void)
2616 if (!cpu_model_allowed()) {
2617 /* compatibility machines interfere with the cpu model */
2618 return false;
2620 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2621 KVM_S390_VM_CPU_MACHINE) &&
2622 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2623 KVM_S390_VM_CPU_PROCESSOR) &&
2624 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2625 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2626 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2627 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2628 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2629 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2632 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2634 struct kvm_s390_vm_cpu_machine prop = {};
2635 struct kvm_device_attr attr = {
2636 .group = KVM_S390_VM_CPU_MODEL,
2637 .attr = KVM_S390_VM_CPU_MACHINE,
2638 .addr = (uint64_t) &prop,
2640 uint16_t unblocked_ibc = 0, cpu_type = 0;
2641 int rc;
2643 memset(model, 0, sizeof(*model));
2645 if (!kvm_s390_cpu_models_supported()) {
2646 error_setg(errp, "KVM doesn't support CPU models");
2647 return;
2650 /* query the basic cpu model properties */
2651 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2652 if (rc) {
2653 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2654 return;
2657 cpu_type = cpuid_type(prop.cpuid);
2658 if (has_ibc(prop.ibc)) {
2659 model->lowest_ibc = lowest_ibc(prop.ibc);
2660 unblocked_ibc = unblocked_ibc(prop.ibc);
2662 model->cpu_id = cpuid_id(prop.cpuid);
2663 model->cpu_id_format = cpuid_format(prop.cpuid);
2664 model->cpu_ver = 0xff;
2666 /* get supported cpu features indicated via STFL(E) */
2667 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2668 (uint8_t *) prop.fac_mask);
2669 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2670 if (test_bit(S390_FEAT_STFLE, model->features)) {
2671 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2673 /* get supported cpu features indicated e.g. via SCLP */
2674 rc = query_cpu_feat(model->features);
2675 if (rc) {
2676 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2677 return;
2679 /* get supported cpu subfunctions indicated via query / test bit */
2680 rc = query_cpu_subfunc(model->features);
2681 if (rc) {
2682 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2683 return;
2686 /* with cpu model support, CMM is only indicated if really available */
2687 if (kvm_s390_cmma_available()) {
2688 set_bit(S390_FEAT_CMM, model->features);
2689 } else {
2690 /* no cmm -> no cmm nt */
2691 clear_bit(S390_FEAT_CMM_NT, model->features);
2694 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2695 if (pci_available) {
2696 set_bit(S390_FEAT_ZPCI, model->features);
2698 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2700 if (s390_known_cpu_type(cpu_type)) {
2701 /* we want the exact model, even if some features are missing */
2702 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2703 ibc_ec_ga(unblocked_ibc), NULL);
2704 } else {
2705 /* model unknown, e.g. too new - search using features */
2706 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2707 ibc_ec_ga(unblocked_ibc),
2708 model->features);
2710 if (!model->def) {
2711 error_setg(errp, "KVM: host CPU model could not be identified");
2712 return;
2714 /* strip of features that are not part of the maximum model */
2715 bitmap_and(model->features, model->features, model->def->full_feat,
2716 S390_FEAT_MAX);
2719 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2721 struct kvm_s390_vm_cpu_processor prop = {
2722 .fac_list = { 0 },
2724 struct kvm_device_attr attr = {
2725 .group = KVM_S390_VM_CPU_MODEL,
2726 .attr = KVM_S390_VM_CPU_PROCESSOR,
2727 .addr = (uint64_t) &prop,
2729 int rc;
2731 if (!model) {
2732 /* compatibility handling if cpu models are disabled */
2733 if (kvm_s390_cmma_available()) {
2734 kvm_s390_enable_cmma();
2736 return;
2738 if (!kvm_s390_cpu_models_supported()) {
2739 error_setg(errp, "KVM doesn't support CPU models");
2740 return;
2742 prop.cpuid = s390_cpuid_from_cpu_model(model);
2743 prop.ibc = s390_ibc_from_cpu_model(model);
2744 /* configure cpu features indicated via STFL(e) */
2745 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2746 (uint8_t *) prop.fac_list);
2747 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2748 if (rc) {
2749 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2750 return;
2752 /* configure cpu features indicated e.g. via SCLP */
2753 rc = configure_cpu_feat(model->features);
2754 if (rc) {
2755 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2756 return;
2758 /* configure cpu subfunctions indicated via query / test bit */
2759 rc = configure_cpu_subfunc(model->features);
2760 if (rc) {
2761 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2762 return;
2764 /* enable CMM via CMMA */
2765 if (test_bit(S390_FEAT_CMM, model->features)) {
2766 kvm_s390_enable_cmma();