target/s390x: introduce internal.h
[qemu.git] / target / s390x / kvm.c
blobe271f958a4bb6a8511f2643562ba5527e4acbe08
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 "qemu/error-report.h"
34 #include "qemu/timer.h"
35 #include "sysemu/sysemu.h"
36 #include "sysemu/hw_accel.h"
37 #include "hw/hw.h"
38 #include "sysemu/device_tree.h"
39 #include "qapi/qmp/qjson.h"
40 #include "exec/gdbstub.h"
41 #include "exec/address-spaces.h"
42 #include "trace.h"
43 #include "qapi-event.h"
44 #include "hw/s390x/s390-pci-inst.h"
45 #include "hw/s390x/s390-pci-bus.h"
46 #include "hw/s390x/ipl.h"
47 #include "hw/s390x/ebcdic.h"
48 #include "exec/memattrs.h"
49 #include "hw/s390x/s390-virtio-ccw.h"
51 #ifndef DEBUG_KVM
52 #define DEBUG_KVM 0
53 #endif
55 #define DPRINTF(fmt, ...) do { \
56 if (DEBUG_KVM) { \
57 fprintf(stderr, fmt, ## __VA_ARGS__); \
58 } \
59 } while (0);
61 #define kvm_vm_check_mem_attr(s, attr) \
62 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
64 #define IPA0_DIAG 0x8300
65 #define IPA0_SIGP 0xae00
66 #define IPA0_B2 0xb200
67 #define IPA0_B9 0xb900
68 #define IPA0_EB 0xeb00
69 #define IPA0_E3 0xe300
71 #define PRIV_B2_SCLP_CALL 0x20
72 #define PRIV_B2_CSCH 0x30
73 #define PRIV_B2_HSCH 0x31
74 #define PRIV_B2_MSCH 0x32
75 #define PRIV_B2_SSCH 0x33
76 #define PRIV_B2_STSCH 0x34
77 #define PRIV_B2_TSCH 0x35
78 #define PRIV_B2_TPI 0x36
79 #define PRIV_B2_SAL 0x37
80 #define PRIV_B2_RSCH 0x38
81 #define PRIV_B2_STCRW 0x39
82 #define PRIV_B2_STCPS 0x3a
83 #define PRIV_B2_RCHP 0x3b
84 #define PRIV_B2_SCHM 0x3c
85 #define PRIV_B2_CHSC 0x5f
86 #define PRIV_B2_SIGA 0x74
87 #define PRIV_B2_XSCH 0x76
89 #define PRIV_EB_SQBS 0x8a
90 #define PRIV_EB_PCISTB 0xd0
91 #define PRIV_EB_SIC 0xd1
93 #define PRIV_B9_EQBS 0x9c
94 #define PRIV_B9_CLP 0xa0
95 #define PRIV_B9_PCISTG 0xd0
96 #define PRIV_B9_PCILG 0xd2
97 #define PRIV_B9_RPCIT 0xd3
99 #define PRIV_E3_MPCIFC 0xd0
100 #define PRIV_E3_STPCIFC 0xd4
102 #define DIAG_TIMEREVENT 0x288
103 #define DIAG_IPL 0x308
104 #define DIAG_KVM_HYPERCALL 0x500
105 #define DIAG_KVM_BREAKPOINT 0x501
107 #define ICPT_INSTRUCTION 0x04
108 #define ICPT_PROGRAM 0x08
109 #define ICPT_EXT_INT 0x14
110 #define ICPT_WAITPSW 0x1c
111 #define ICPT_SOFT_INTERCEPT 0x24
112 #define ICPT_CPU_STOP 0x28
113 #define ICPT_OPEREXC 0x2c
114 #define ICPT_IO 0x40
116 #define NR_LOCAL_IRQS 32
118 * Needs to be big enough to contain max_cpus emergency signals
119 * and in addition NR_LOCAL_IRQS interrupts
121 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
122 (max_cpus + NR_LOCAL_IRQS))
124 static CPUWatchpoint hw_watchpoint;
126 * We don't use a list because this structure is also used to transmit the
127 * hardware breakpoints to the kernel.
129 static struct kvm_hw_breakpoint *hw_breakpoints;
130 static int nb_hw_breakpoints;
132 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
133 KVM_CAP_LAST_INFO
136 static QemuMutex qemu_sigp_mutex;
138 static int cap_sync_regs;
139 static int cap_async_pf;
140 static int cap_mem_op;
141 static int cap_s390_irq;
142 static int cap_ri;
143 static int cap_gs;
145 static int active_cmma;
147 static void *legacy_s390_alloc(size_t size, uint64_t *align);
149 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
151 struct kvm_device_attr attr = {
152 .group = KVM_S390_VM_MEM_CTRL,
153 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
154 .addr = (uint64_t) memory_limit,
157 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
160 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit)
162 int rc;
164 struct kvm_device_attr attr = {
165 .group = KVM_S390_VM_MEM_CTRL,
166 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
167 .addr = (uint64_t) &new_limit,
170 if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) {
171 return 0;
174 rc = kvm_s390_query_mem_limit(hw_limit);
175 if (rc) {
176 return rc;
177 } else if (*hw_limit < new_limit) {
178 return -E2BIG;
181 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
184 int kvm_s390_cmma_active(void)
186 return active_cmma;
189 static bool kvm_s390_cmma_available(void)
191 static bool initialized, value;
193 if (!initialized) {
194 initialized = true;
195 value = kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_ENABLE_CMMA) &&
196 kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_CLR_CMMA);
198 return value;
201 void kvm_s390_cmma_reset(void)
203 int rc;
204 struct kvm_device_attr attr = {
205 .group = KVM_S390_VM_MEM_CTRL,
206 .attr = KVM_S390_VM_MEM_CLR_CMMA,
209 if (!kvm_s390_cmma_active()) {
210 return;
213 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
214 trace_kvm_clear_cmma(rc);
217 static void kvm_s390_enable_cmma(void)
219 int rc;
220 struct kvm_device_attr attr = {
221 .group = KVM_S390_VM_MEM_CTRL,
222 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
225 if (mem_path) {
226 error_report("Warning: CMM will not be enabled because it is not "
227 "compatible to hugetlbfs.");
228 return;
230 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
231 active_cmma = !rc;
232 trace_kvm_enable_cmma(rc);
235 static void kvm_s390_set_attr(uint64_t attr)
237 struct kvm_device_attr attribute = {
238 .group = KVM_S390_VM_CRYPTO,
239 .attr = attr,
242 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
244 if (ret) {
245 error_report("Failed to set crypto device attribute %lu: %s",
246 attr, strerror(-ret));
250 static void kvm_s390_init_aes_kw(void)
252 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
254 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
255 NULL)) {
256 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
259 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
260 kvm_s390_set_attr(attr);
264 static void kvm_s390_init_dea_kw(void)
266 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
268 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
269 NULL)) {
270 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
273 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
274 kvm_s390_set_attr(attr);
278 void kvm_s390_crypto_reset(void)
280 if (s390_has_feat(S390_FEAT_MSA_EXT_3)) {
281 kvm_s390_init_aes_kw();
282 kvm_s390_init_dea_kw();
286 int kvm_arch_init(MachineState *ms, KVMState *s)
288 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
289 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
290 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
291 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
293 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
294 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
295 phys_mem_set_alloc(legacy_s390_alloc);
298 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
299 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
300 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
301 if (ri_allowed()) {
302 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
303 cap_ri = 1;
306 if (gs_allowed()) {
307 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
308 cap_gs = 1;
312 /* Try to enable AIS facility */
313 kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0);
315 qemu_mutex_init(&qemu_sigp_mutex);
317 return 0;
320 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
322 return 0;
325 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
327 return cpu->cpu_index;
330 int kvm_arch_init_vcpu(CPUState *cs)
332 S390CPU *cpu = S390_CPU(cs);
333 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
334 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
335 return 0;
338 void kvm_s390_reset_vcpu(S390CPU *cpu)
340 CPUState *cs = CPU(cpu);
342 /* The initial reset call is needed here to reset in-kernel
343 * vcpu data that we can't access directly from QEMU
344 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
345 * Before this ioctl cpu_synchronize_state() is called in common kvm
346 * code (kvm-all) */
347 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
348 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
352 static int can_sync_regs(CPUState *cs, int regs)
354 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
357 int kvm_arch_put_registers(CPUState *cs, int level)
359 S390CPU *cpu = S390_CPU(cs);
360 CPUS390XState *env = &cpu->env;
361 struct kvm_sregs sregs;
362 struct kvm_regs regs;
363 struct kvm_fpu fpu = {};
364 int r;
365 int i;
367 /* always save the PSW and the GPRS*/
368 cs->kvm_run->psw_addr = env->psw.addr;
369 cs->kvm_run->psw_mask = env->psw.mask;
371 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
372 for (i = 0; i < 16; i++) {
373 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
374 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
376 } else {
377 for (i = 0; i < 16; i++) {
378 regs.gprs[i] = env->regs[i];
380 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
381 if (r < 0) {
382 return r;
386 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
387 for (i = 0; i < 32; i++) {
388 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
389 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
391 cs->kvm_run->s.regs.fpc = env->fpc;
392 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
393 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
394 for (i = 0; i < 16; i++) {
395 cs->kvm_run->s.regs.fprs[i] = get_freg(env, i)->ll;
397 cs->kvm_run->s.regs.fpc = env->fpc;
398 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
399 } else {
400 /* Floating point */
401 for (i = 0; i < 16; i++) {
402 fpu.fprs[i] = get_freg(env, i)->ll;
404 fpu.fpc = env->fpc;
406 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
407 if (r < 0) {
408 return r;
412 /* Do we need to save more than that? */
413 if (level == KVM_PUT_RUNTIME_STATE) {
414 return 0;
417 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
418 cs->kvm_run->s.regs.cputm = env->cputm;
419 cs->kvm_run->s.regs.ckc = env->ckc;
420 cs->kvm_run->s.regs.todpr = env->todpr;
421 cs->kvm_run->s.regs.gbea = env->gbea;
422 cs->kvm_run->s.regs.pp = env->pp;
423 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
424 } else {
426 * These ONE_REGS are not protected by a capability. As they are only
427 * necessary for migration we just trace a possible error, but don't
428 * return with an error return code.
430 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
431 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
432 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
433 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
434 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
437 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
438 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
439 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
442 /* pfault parameters */
443 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
444 cs->kvm_run->s.regs.pft = env->pfault_token;
445 cs->kvm_run->s.regs.pfs = env->pfault_select;
446 cs->kvm_run->s.regs.pfc = env->pfault_compare;
447 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
448 } else if (cap_async_pf) {
449 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
450 if (r < 0) {
451 return r;
453 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
454 if (r < 0) {
455 return r;
457 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
458 if (r < 0) {
459 return r;
463 /* access registers and control registers*/
464 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
465 for (i = 0; i < 16; i++) {
466 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
467 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
469 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
470 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
471 } else {
472 for (i = 0; i < 16; i++) {
473 sregs.acrs[i] = env->aregs[i];
474 sregs.crs[i] = env->cregs[i];
476 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
477 if (r < 0) {
478 return r;
482 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
483 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
484 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
487 /* Finally the prefix */
488 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
489 cs->kvm_run->s.regs.prefix = env->psa;
490 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
491 } else {
492 /* prefix is only supported via sync regs */
494 return 0;
497 int kvm_arch_get_registers(CPUState *cs)
499 S390CPU *cpu = S390_CPU(cs);
500 CPUS390XState *env = &cpu->env;
501 struct kvm_sregs sregs;
502 struct kvm_regs regs;
503 struct kvm_fpu fpu;
504 int i, r;
506 /* get the PSW */
507 env->psw.addr = cs->kvm_run->psw_addr;
508 env->psw.mask = cs->kvm_run->psw_mask;
510 /* the GPRS */
511 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
512 for (i = 0; i < 16; i++) {
513 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
515 } else {
516 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
517 if (r < 0) {
518 return r;
520 for (i = 0; i < 16; i++) {
521 env->regs[i] = regs.gprs[i];
525 /* The ACRS and CRS */
526 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
527 for (i = 0; i < 16; i++) {
528 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
529 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
531 } else {
532 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
533 if (r < 0) {
534 return r;
536 for (i = 0; i < 16; i++) {
537 env->aregs[i] = sregs.acrs[i];
538 env->cregs[i] = sregs.crs[i];
542 /* Floating point and vector registers */
543 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
544 for (i = 0; i < 32; i++) {
545 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
546 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
548 env->fpc = cs->kvm_run->s.regs.fpc;
549 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
550 for (i = 0; i < 16; i++) {
551 get_freg(env, i)->ll = cs->kvm_run->s.regs.fprs[i];
553 env->fpc = cs->kvm_run->s.regs.fpc;
554 } else {
555 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
556 if (r < 0) {
557 return r;
559 for (i = 0; i < 16; i++) {
560 get_freg(env, i)->ll = fpu.fprs[i];
562 env->fpc = fpu.fpc;
565 /* The prefix */
566 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
567 env->psa = cs->kvm_run->s.regs.prefix;
570 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
571 env->cputm = cs->kvm_run->s.regs.cputm;
572 env->ckc = cs->kvm_run->s.regs.ckc;
573 env->todpr = cs->kvm_run->s.regs.todpr;
574 env->gbea = cs->kvm_run->s.regs.gbea;
575 env->pp = cs->kvm_run->s.regs.pp;
576 } else {
578 * These ONE_REGS are not protected by a capability. As they are only
579 * necessary for migration we just trace a possible error, but don't
580 * return with an error return code.
582 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
583 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
584 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
585 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
586 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
589 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
590 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
593 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
594 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
597 /* pfault parameters */
598 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
599 env->pfault_token = cs->kvm_run->s.regs.pft;
600 env->pfault_select = cs->kvm_run->s.regs.pfs;
601 env->pfault_compare = cs->kvm_run->s.regs.pfc;
602 } else if (cap_async_pf) {
603 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
604 if (r < 0) {
605 return r;
607 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
608 if (r < 0) {
609 return r;
611 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
612 if (r < 0) {
613 return r;
617 return 0;
620 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
622 int r;
623 struct kvm_device_attr attr = {
624 .group = KVM_S390_VM_TOD,
625 .attr = KVM_S390_VM_TOD_LOW,
626 .addr = (uint64_t)tod_low,
629 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
630 if (r) {
631 return r;
634 attr.attr = KVM_S390_VM_TOD_HIGH;
635 attr.addr = (uint64_t)tod_high;
636 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
639 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
641 int r;
643 struct kvm_device_attr attr = {
644 .group = KVM_S390_VM_TOD,
645 .attr = KVM_S390_VM_TOD_LOW,
646 .addr = (uint64_t)tod_low,
649 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
650 if (r) {
651 return r;
654 attr.attr = KVM_S390_VM_TOD_HIGH;
655 attr.addr = (uint64_t)tod_high;
656 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
660 * kvm_s390_mem_op:
661 * @addr: the logical start address in guest memory
662 * @ar: the access register number
663 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
664 * @len: length that should be transferred
665 * @is_write: true = write, false = read
666 * Returns: 0 on success, non-zero if an exception or error occurred
668 * Use KVM ioctl to read/write from/to guest memory. An access exception
669 * is injected into the vCPU in case of translation errors.
671 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
672 int len, bool is_write)
674 struct kvm_s390_mem_op mem_op = {
675 .gaddr = addr,
676 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
677 .size = len,
678 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
679 : KVM_S390_MEMOP_LOGICAL_READ,
680 .buf = (uint64_t)hostbuf,
681 .ar = ar,
683 int ret;
685 if (!cap_mem_op) {
686 return -ENOSYS;
688 if (!hostbuf) {
689 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
692 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
693 if (ret < 0) {
694 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
696 return ret;
700 * Legacy layout for s390:
701 * Older S390 KVM requires the topmost vma of the RAM to be
702 * smaller than an system defined value, which is at least 256GB.
703 * Larger systems have larger values. We put the guest between
704 * the end of data segment (system break) and this value. We
705 * use 32GB as a base to have enough room for the system break
706 * to grow. We also have to use MAP parameters that avoid
707 * read-only mapping of guest pages.
709 static void *legacy_s390_alloc(size_t size, uint64_t *align)
711 void *mem;
713 mem = mmap((void *) 0x800000000ULL, size,
714 PROT_EXEC|PROT_READ|PROT_WRITE,
715 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
716 return mem == MAP_FAILED ? NULL : mem;
719 static uint8_t const *sw_bp_inst;
720 static uint8_t sw_bp_ilen;
722 static void determine_sw_breakpoint_instr(void)
724 /* DIAG 501 is used for sw breakpoints with old kernels */
725 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
726 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
727 static const uint8_t instr_0x0000[] = {0x00, 0x00};
729 if (sw_bp_inst) {
730 return;
732 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
733 sw_bp_inst = diag_501;
734 sw_bp_ilen = sizeof(diag_501);
735 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
736 } else {
737 sw_bp_inst = instr_0x0000;
738 sw_bp_ilen = sizeof(instr_0x0000);
739 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
743 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
745 determine_sw_breakpoint_instr();
747 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
748 sw_bp_ilen, 0) ||
749 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
750 return -EINVAL;
752 return 0;
755 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
757 uint8_t t[MAX_ILEN];
759 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
760 return -EINVAL;
761 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
762 return -EINVAL;
763 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
764 sw_bp_ilen, 1)) {
765 return -EINVAL;
768 return 0;
771 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
772 int len, int type)
774 int n;
776 for (n = 0; n < nb_hw_breakpoints; n++) {
777 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
778 (hw_breakpoints[n].len == len || len == -1)) {
779 return &hw_breakpoints[n];
783 return NULL;
786 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
788 int size;
790 if (find_hw_breakpoint(addr, len, type)) {
791 return -EEXIST;
794 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
796 if (!hw_breakpoints) {
797 nb_hw_breakpoints = 0;
798 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
799 } else {
800 hw_breakpoints =
801 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
804 if (!hw_breakpoints) {
805 nb_hw_breakpoints = 0;
806 return -ENOMEM;
809 hw_breakpoints[nb_hw_breakpoints].addr = addr;
810 hw_breakpoints[nb_hw_breakpoints].len = len;
811 hw_breakpoints[nb_hw_breakpoints].type = type;
813 nb_hw_breakpoints++;
815 return 0;
818 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
819 target_ulong len, int type)
821 switch (type) {
822 case GDB_BREAKPOINT_HW:
823 type = KVM_HW_BP;
824 break;
825 case GDB_WATCHPOINT_WRITE:
826 if (len < 1) {
827 return -EINVAL;
829 type = KVM_HW_WP_WRITE;
830 break;
831 default:
832 return -ENOSYS;
834 return insert_hw_breakpoint(addr, len, type);
837 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
838 target_ulong len, int type)
840 int size;
841 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
843 if (bp == NULL) {
844 return -ENOENT;
847 nb_hw_breakpoints--;
848 if (nb_hw_breakpoints > 0) {
850 * In order to trim the array, move the last element to the position to
851 * be removed - if necessary.
853 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
854 *bp = hw_breakpoints[nb_hw_breakpoints];
856 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
857 hw_breakpoints =
858 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
859 } else {
860 g_free(hw_breakpoints);
861 hw_breakpoints = NULL;
864 return 0;
867 void kvm_arch_remove_all_hw_breakpoints(void)
869 nb_hw_breakpoints = 0;
870 g_free(hw_breakpoints);
871 hw_breakpoints = NULL;
874 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
876 int i;
878 if (nb_hw_breakpoints > 0) {
879 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
880 dbg->arch.hw_bp = hw_breakpoints;
882 for (i = 0; i < nb_hw_breakpoints; ++i) {
883 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
884 hw_breakpoints[i].addr);
886 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
887 } else {
888 dbg->arch.nr_hw_bp = 0;
889 dbg->arch.hw_bp = NULL;
893 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
897 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
899 return MEMTXATTRS_UNSPECIFIED;
902 int kvm_arch_process_async_events(CPUState *cs)
904 return cs->halted;
907 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
908 struct kvm_s390_interrupt *interrupt)
910 int r = 0;
912 interrupt->type = irq->type;
913 switch (irq->type) {
914 case KVM_S390_INT_VIRTIO:
915 interrupt->parm = irq->u.ext.ext_params;
916 /* fall through */
917 case KVM_S390_INT_PFAULT_INIT:
918 case KVM_S390_INT_PFAULT_DONE:
919 interrupt->parm64 = irq->u.ext.ext_params2;
920 break;
921 case KVM_S390_PROGRAM_INT:
922 interrupt->parm = irq->u.pgm.code;
923 break;
924 case KVM_S390_SIGP_SET_PREFIX:
925 interrupt->parm = irq->u.prefix.address;
926 break;
927 case KVM_S390_INT_SERVICE:
928 interrupt->parm = irq->u.ext.ext_params;
929 break;
930 case KVM_S390_MCHK:
931 interrupt->parm = irq->u.mchk.cr14;
932 interrupt->parm64 = irq->u.mchk.mcic;
933 break;
934 case KVM_S390_INT_EXTERNAL_CALL:
935 interrupt->parm = irq->u.extcall.code;
936 break;
937 case KVM_S390_INT_EMERGENCY:
938 interrupt->parm = irq->u.emerg.code;
939 break;
940 case KVM_S390_SIGP_STOP:
941 case KVM_S390_RESTART:
942 break; /* These types have no parameters */
943 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
944 interrupt->parm = irq->u.io.subchannel_id << 16;
945 interrupt->parm |= irq->u.io.subchannel_nr;
946 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
947 interrupt->parm64 |= irq->u.io.io_int_word;
948 break;
949 default:
950 r = -EINVAL;
951 break;
953 return r;
956 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
958 struct kvm_s390_interrupt kvmint = {};
959 int r;
961 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
962 if (r < 0) {
963 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
964 exit(1);
967 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
968 if (r < 0) {
969 fprintf(stderr, "KVM failed to inject interrupt\n");
970 exit(1);
974 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
976 CPUState *cs = CPU(cpu);
977 int r;
979 if (cap_s390_irq) {
980 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
981 if (!r) {
982 return;
984 error_report("KVM failed to inject interrupt %llx", irq->type);
985 exit(1);
988 inject_vcpu_irq_legacy(cs, irq);
991 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
993 struct kvm_s390_interrupt kvmint = {};
994 int r;
996 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
997 if (r < 0) {
998 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
999 exit(1);
1002 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1003 if (r < 0) {
1004 fprintf(stderr, "KVM failed to inject interrupt\n");
1005 exit(1);
1009 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
1011 static bool use_flic = true;
1012 int r;
1014 if (use_flic) {
1015 r = kvm_s390_inject_flic(irq);
1016 if (r == -ENOSYS) {
1017 use_flic = false;
1019 if (!r) {
1020 return;
1023 __kvm_s390_floating_interrupt(irq);
1026 void kvm_s390_service_interrupt(uint32_t parm)
1028 struct kvm_s390_irq irq = {
1029 .type = KVM_S390_INT_SERVICE,
1030 .u.ext.ext_params = parm,
1033 kvm_s390_floating_interrupt(&irq);
1036 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1038 struct kvm_s390_irq irq = {
1039 .type = KVM_S390_PROGRAM_INT,
1040 .u.pgm.code = code,
1043 kvm_s390_vcpu_interrupt(cpu, &irq);
1046 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1048 struct kvm_s390_irq irq = {
1049 .type = KVM_S390_PROGRAM_INT,
1050 .u.pgm.code = code,
1051 .u.pgm.trans_exc_code = te_code,
1052 .u.pgm.exc_access_id = te_code & 3,
1055 kvm_s390_vcpu_interrupt(cpu, &irq);
1058 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1059 uint16_t ipbh0)
1061 CPUS390XState *env = &cpu->env;
1062 uint64_t sccb;
1063 uint32_t code;
1064 int r = 0;
1066 cpu_synchronize_state(CPU(cpu));
1067 sccb = env->regs[ipbh0 & 0xf];
1068 code = env->regs[(ipbh0 & 0xf0) >> 4];
1070 r = sclp_service_call(env, sccb, code);
1071 if (r < 0) {
1072 kvm_s390_program_interrupt(cpu, -r);
1073 } else {
1074 setcc(cpu, r);
1077 return 0;
1080 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1082 CPUS390XState *env = &cpu->env;
1083 int rc = 0;
1084 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1086 cpu_synchronize_state(CPU(cpu));
1088 switch (ipa1) {
1089 case PRIV_B2_XSCH:
1090 ioinst_handle_xsch(cpu, env->regs[1]);
1091 break;
1092 case PRIV_B2_CSCH:
1093 ioinst_handle_csch(cpu, env->regs[1]);
1094 break;
1095 case PRIV_B2_HSCH:
1096 ioinst_handle_hsch(cpu, env->regs[1]);
1097 break;
1098 case PRIV_B2_MSCH:
1099 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1100 break;
1101 case PRIV_B2_SSCH:
1102 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1103 break;
1104 case PRIV_B2_STCRW:
1105 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1106 break;
1107 case PRIV_B2_STSCH:
1108 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1109 break;
1110 case PRIV_B2_TSCH:
1111 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1112 fprintf(stderr, "Spurious tsch intercept\n");
1113 break;
1114 case PRIV_B2_CHSC:
1115 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1116 break;
1117 case PRIV_B2_TPI:
1118 /* This should have been handled by kvm already. */
1119 fprintf(stderr, "Spurious tpi intercept\n");
1120 break;
1121 case PRIV_B2_SCHM:
1122 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1123 run->s390_sieic.ipb);
1124 break;
1125 case PRIV_B2_RSCH:
1126 ioinst_handle_rsch(cpu, env->regs[1]);
1127 break;
1128 case PRIV_B2_RCHP:
1129 ioinst_handle_rchp(cpu, env->regs[1]);
1130 break;
1131 case PRIV_B2_STCPS:
1132 /* We do not provide this instruction, it is suppressed. */
1133 break;
1134 case PRIV_B2_SAL:
1135 ioinst_handle_sal(cpu, env->regs[1]);
1136 break;
1137 case PRIV_B2_SIGA:
1138 /* Not provided, set CC = 3 for subchannel not operational */
1139 setcc(cpu, 3);
1140 break;
1141 case PRIV_B2_SCLP_CALL:
1142 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1143 break;
1144 default:
1145 rc = -1;
1146 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1147 break;
1150 return rc;
1153 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1154 uint8_t *ar)
1156 CPUS390XState *env = &cpu->env;
1157 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1158 uint32_t base2 = run->s390_sieic.ipb >> 28;
1159 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1160 ((run->s390_sieic.ipb & 0xff00) << 4);
1162 if (disp2 & 0x80000) {
1163 disp2 += 0xfff00000;
1165 if (ar) {
1166 *ar = base2;
1169 return (base2 ? env->regs[base2] : 0) +
1170 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1173 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1174 uint8_t *ar)
1176 CPUS390XState *env = &cpu->env;
1177 uint32_t base2 = run->s390_sieic.ipb >> 28;
1178 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1179 ((run->s390_sieic.ipb & 0xff00) << 4);
1181 if (disp2 & 0x80000) {
1182 disp2 += 0xfff00000;
1184 if (ar) {
1185 *ar = base2;
1188 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1191 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1193 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1195 if (s390_has_feat(S390_FEAT_ZPCI)) {
1196 return clp_service_call(cpu, r2);
1197 } else {
1198 return -1;
1202 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1204 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1205 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1207 if (s390_has_feat(S390_FEAT_ZPCI)) {
1208 return pcilg_service_call(cpu, r1, r2);
1209 } else {
1210 return -1;
1214 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1216 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1217 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1219 if (s390_has_feat(S390_FEAT_ZPCI)) {
1220 return pcistg_service_call(cpu, r1, r2);
1221 } else {
1222 return -1;
1226 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1228 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1229 uint64_t fiba;
1230 uint8_t ar;
1232 if (s390_has_feat(S390_FEAT_ZPCI)) {
1233 cpu_synchronize_state(CPU(cpu));
1234 fiba = get_base_disp_rxy(cpu, run, &ar);
1236 return stpcifc_service_call(cpu, r1, fiba, ar);
1237 } else {
1238 return -1;
1242 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1244 CPUS390XState *env = &cpu->env;
1245 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1246 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1247 uint8_t isc;
1248 uint16_t mode;
1249 int r;
1251 cpu_synchronize_state(CPU(cpu));
1252 mode = env->regs[r1] & 0xffff;
1253 isc = (env->regs[r3] >> 27) & 0x7;
1254 r = css_do_sic(env, isc, mode);
1255 if (r) {
1256 kvm_s390_program_interrupt(cpu, -r);
1259 return 0;
1262 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1264 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1265 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1267 if (s390_has_feat(S390_FEAT_ZPCI)) {
1268 return rpcit_service_call(cpu, r1, r2);
1269 } else {
1270 return -1;
1274 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1276 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1277 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1278 uint64_t gaddr;
1279 uint8_t ar;
1281 if (s390_has_feat(S390_FEAT_ZPCI)) {
1282 cpu_synchronize_state(CPU(cpu));
1283 gaddr = get_base_disp_rsy(cpu, run, &ar);
1285 return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1286 } else {
1287 return -1;
1291 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1293 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1294 uint64_t fiba;
1295 uint8_t ar;
1297 if (s390_has_feat(S390_FEAT_ZPCI)) {
1298 cpu_synchronize_state(CPU(cpu));
1299 fiba = get_base_disp_rxy(cpu, run, &ar);
1301 return mpcifc_service_call(cpu, r1, fiba, ar);
1302 } else {
1303 return -1;
1307 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1309 int r = 0;
1311 switch (ipa1) {
1312 case PRIV_B9_CLP:
1313 r = kvm_clp_service_call(cpu, run);
1314 break;
1315 case PRIV_B9_PCISTG:
1316 r = kvm_pcistg_service_call(cpu, run);
1317 break;
1318 case PRIV_B9_PCILG:
1319 r = kvm_pcilg_service_call(cpu, run);
1320 break;
1321 case PRIV_B9_RPCIT:
1322 r = kvm_rpcit_service_call(cpu, run);
1323 break;
1324 case PRIV_B9_EQBS:
1325 /* just inject exception */
1326 r = -1;
1327 break;
1328 default:
1329 r = -1;
1330 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1331 break;
1334 return r;
1337 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1339 int r = 0;
1341 switch (ipbl) {
1342 case PRIV_EB_PCISTB:
1343 r = kvm_pcistb_service_call(cpu, run);
1344 break;
1345 case PRIV_EB_SIC:
1346 r = kvm_sic_service_call(cpu, run);
1347 break;
1348 case PRIV_EB_SQBS:
1349 /* just inject exception */
1350 r = -1;
1351 break;
1352 default:
1353 r = -1;
1354 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1355 break;
1358 return r;
1361 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1363 int r = 0;
1365 switch (ipbl) {
1366 case PRIV_E3_MPCIFC:
1367 r = kvm_mpcifc_service_call(cpu, run);
1368 break;
1369 case PRIV_E3_STPCIFC:
1370 r = kvm_stpcifc_service_call(cpu, run);
1371 break;
1372 default:
1373 r = -1;
1374 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1375 break;
1378 return r;
1381 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1383 CPUS390XState *env = &cpu->env;
1384 int ret;
1386 cpu_synchronize_state(CPU(cpu));
1387 ret = s390_virtio_hypercall(env);
1388 if (ret == -EINVAL) {
1389 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1390 return 0;
1393 return ret;
1396 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1398 uint64_t r1, r3;
1399 int rc;
1401 cpu_synchronize_state(CPU(cpu));
1402 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1403 r3 = run->s390_sieic.ipa & 0x000f;
1404 rc = handle_diag_288(&cpu->env, r1, r3);
1405 if (rc) {
1406 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1410 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1412 uint64_t r1, r3;
1414 cpu_synchronize_state(CPU(cpu));
1415 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1416 r3 = run->s390_sieic.ipa & 0x000f;
1417 handle_diag_308(&cpu->env, r1, r3);
1420 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1422 CPUS390XState *env = &cpu->env;
1423 unsigned long pc;
1425 cpu_synchronize_state(CPU(cpu));
1427 pc = env->psw.addr - sw_bp_ilen;
1428 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1429 env->psw.addr = pc;
1430 return EXCP_DEBUG;
1433 return -ENOENT;
1436 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1438 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1440 int r = 0;
1441 uint16_t func_code;
1444 * For any diagnose call we support, bits 48-63 of the resulting
1445 * address specify the function code; the remainder is ignored.
1447 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1448 switch (func_code) {
1449 case DIAG_TIMEREVENT:
1450 kvm_handle_diag_288(cpu, run);
1451 break;
1452 case DIAG_IPL:
1453 kvm_handle_diag_308(cpu, run);
1454 break;
1455 case DIAG_KVM_HYPERCALL:
1456 r = handle_hypercall(cpu, run);
1457 break;
1458 case DIAG_KVM_BREAKPOINT:
1459 r = handle_sw_breakpoint(cpu, run);
1460 break;
1461 default:
1462 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1463 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1464 break;
1467 return r;
1470 typedef struct SigpInfo {
1471 uint64_t param;
1472 int cc;
1473 uint64_t *status_reg;
1474 } SigpInfo;
1476 static void set_sigp_status(SigpInfo *si, uint64_t status)
1478 *si->status_reg &= 0xffffffff00000000ULL;
1479 *si->status_reg |= status;
1480 si->cc = SIGP_CC_STATUS_STORED;
1483 static void sigp_start(CPUState *cs, run_on_cpu_data arg)
1485 S390CPU *cpu = S390_CPU(cs);
1486 SigpInfo *si = arg.host_ptr;
1488 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1489 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1490 return;
1493 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1494 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1497 static void sigp_stop(CPUState *cs, run_on_cpu_data arg)
1499 S390CPU *cpu = S390_CPU(cs);
1500 SigpInfo *si = arg.host_ptr;
1501 struct kvm_s390_irq irq = {
1502 .type = KVM_S390_SIGP_STOP,
1505 if (s390_cpu_get_state(cpu) != CPU_STATE_OPERATING) {
1506 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1507 return;
1510 /* disabled wait - sleeping in user space */
1511 if (cs->halted) {
1512 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
1513 } else {
1514 /* execute the stop function */
1515 cpu->env.sigp_order = SIGP_STOP;
1516 kvm_s390_vcpu_interrupt(cpu, &irq);
1518 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1521 #define ADTL_GS_OFFSET 1024 /* offset of GS data in adtl save area */
1522 #define ADTL_GS_MIN_SIZE 2048 /* minimal size of adtl save area for GS */
1523 static int do_store_adtl_status(S390CPU *cpu, hwaddr addr, hwaddr len)
1525 hwaddr save = len;
1526 void *mem;
1528 mem = cpu_physical_memory_map(addr, &save, 1);
1529 if (!mem) {
1530 return -EFAULT;
1532 if (save != len) {
1533 cpu_physical_memory_unmap(mem, len, 1, 0);
1534 return -EFAULT;
1537 if (s390_has_feat(S390_FEAT_VECTOR)) {
1538 memcpy(mem, &cpu->env.vregs, 512);
1540 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE) && len >= ADTL_GS_MIN_SIZE) {
1541 memcpy(mem + ADTL_GS_OFFSET, &cpu->env.gscb, 32);
1544 cpu_physical_memory_unmap(mem, len, 1, len);
1546 return 0;
1549 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1550 #define SAVE_AREA_SIZE 512
1551 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1553 static const uint8_t ar_id = 1;
1554 uint64_t ckc = cpu->env.ckc >> 8;
1555 void *mem;
1556 int i;
1557 hwaddr len = SAVE_AREA_SIZE;
1559 mem = cpu_physical_memory_map(addr, &len, 1);
1560 if (!mem) {
1561 return -EFAULT;
1563 if (len != SAVE_AREA_SIZE) {
1564 cpu_physical_memory_unmap(mem, len, 1, 0);
1565 return -EFAULT;
1568 if (store_arch) {
1569 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1571 for (i = 0; i < 16; ++i) {
1572 *((uint64_t *)mem + i) = get_freg(&cpu->env, i)->ll;
1574 memcpy(mem + 128, &cpu->env.regs, 128);
1575 memcpy(mem + 256, &cpu->env.psw, 16);
1576 memcpy(mem + 280, &cpu->env.psa, 4);
1577 memcpy(mem + 284, &cpu->env.fpc, 4);
1578 memcpy(mem + 292, &cpu->env.todpr, 4);
1579 memcpy(mem + 296, &cpu->env.cputm, 8);
1580 memcpy(mem + 304, &ckc, 8);
1581 memcpy(mem + 320, &cpu->env.aregs, 64);
1582 memcpy(mem + 384, &cpu->env.cregs, 128);
1584 cpu_physical_memory_unmap(mem, len, 1, len);
1586 return 0;
1589 static void sigp_stop_and_store_status(CPUState *cs, run_on_cpu_data arg)
1591 S390CPU *cpu = S390_CPU(cs);
1592 SigpInfo *si = arg.host_ptr;
1593 struct kvm_s390_irq irq = {
1594 .type = KVM_S390_SIGP_STOP,
1597 /* disabled wait - sleeping in user space */
1598 if (s390_cpu_get_state(cpu) == CPU_STATE_OPERATING && cs->halted) {
1599 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
1602 switch (s390_cpu_get_state(cpu)) {
1603 case CPU_STATE_OPERATING:
1604 cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1605 kvm_s390_vcpu_interrupt(cpu, &irq);
1606 /* store will be performed when handling the stop intercept */
1607 break;
1608 case CPU_STATE_STOPPED:
1609 /* already stopped, just store the status */
1610 cpu_synchronize_state(cs);
1611 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1612 break;
1614 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1617 static void sigp_store_status_at_address(CPUState *cs, run_on_cpu_data arg)
1619 S390CPU *cpu = S390_CPU(cs);
1620 SigpInfo *si = arg.host_ptr;
1621 uint32_t address = si->param & 0x7ffffe00u;
1623 /* cpu has to be stopped */
1624 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1625 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1626 return;
1629 cpu_synchronize_state(cs);
1631 if (kvm_s390_store_status(cpu, address, false)) {
1632 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1633 return;
1635 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1638 #define ADTL_SAVE_LC_MASK 0xfUL
1639 static void sigp_store_adtl_status(CPUState *cs, run_on_cpu_data arg)
1641 S390CPU *cpu = S390_CPU(cs);
1642 SigpInfo *si = arg.host_ptr;
1643 uint8_t lc = si->param & ADTL_SAVE_LC_MASK;
1644 hwaddr addr = si->param & ~ADTL_SAVE_LC_MASK;
1645 hwaddr len = 1UL << (lc ? lc : 10);
1647 if (!s390_has_feat(S390_FEAT_VECTOR) &&
1648 !s390_has_feat(S390_FEAT_GUARDED_STORAGE)) {
1649 set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1650 return;
1653 /* cpu has to be stopped */
1654 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1655 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1656 return;
1659 /* address must be aligned to length */
1660 if (addr & (len - 1)) {
1661 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1662 return;
1665 /* no GS: only lc == 0 is valid */
1666 if (!s390_has_feat(S390_FEAT_GUARDED_STORAGE) &&
1667 lc != 0) {
1668 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1669 return;
1672 /* GS: 0, 10, 11, 12 are valid */
1673 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE) &&
1674 lc != 0 &&
1675 lc != 10 &&
1676 lc != 11 &&
1677 lc != 12) {
1678 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1679 return;
1682 cpu_synchronize_state(cs);
1684 if (do_store_adtl_status(cpu, addr, len)) {
1685 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1686 return;
1688 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1691 static void sigp_restart(CPUState *cs, run_on_cpu_data arg)
1693 S390CPU *cpu = S390_CPU(cs);
1694 SigpInfo *si = arg.host_ptr;
1695 struct kvm_s390_irq irq = {
1696 .type = KVM_S390_RESTART,
1699 switch (s390_cpu_get_state(cpu)) {
1700 case CPU_STATE_STOPPED:
1701 /* the restart irq has to be delivered prior to any other pending irq */
1702 cpu_synchronize_state(cs);
1703 do_restart_interrupt(&cpu->env);
1704 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1705 break;
1706 case CPU_STATE_OPERATING:
1707 kvm_s390_vcpu_interrupt(cpu, &irq);
1708 break;
1710 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1713 int kvm_s390_cpu_restart(S390CPU *cpu)
1715 SigpInfo si = {};
1717 run_on_cpu(CPU(cpu), sigp_restart, RUN_ON_CPU_HOST_PTR(&si));
1718 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1719 return 0;
1722 static void sigp_initial_cpu_reset(CPUState *cs, run_on_cpu_data arg)
1724 S390CPU *cpu = S390_CPU(cs);
1725 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1726 SigpInfo *si = arg.host_ptr;
1728 cpu_synchronize_state(cs);
1729 scc->initial_cpu_reset(cs);
1730 cpu_synchronize_post_reset(cs);
1731 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1734 static void sigp_cpu_reset(CPUState *cs, run_on_cpu_data arg)
1736 S390CPU *cpu = S390_CPU(cs);
1737 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1738 SigpInfo *si = arg.host_ptr;
1740 cpu_synchronize_state(cs);
1741 scc->cpu_reset(cs);
1742 cpu_synchronize_post_reset(cs);
1743 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1746 static void sigp_set_prefix(CPUState *cs, run_on_cpu_data arg)
1748 S390CPU *cpu = S390_CPU(cs);
1749 SigpInfo *si = arg.host_ptr;
1750 uint32_t addr = si->param & 0x7fffe000u;
1752 cpu_synchronize_state(cs);
1754 if (!address_space_access_valid(&address_space_memory, addr,
1755 sizeof(struct LowCore), false)) {
1756 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1757 return;
1760 /* cpu has to be stopped */
1761 if (s390_cpu_get_state(cpu) != CPU_STATE_STOPPED) {
1762 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1763 return;
1766 cpu->env.psa = addr;
1767 cpu_synchronize_post_init(cs);
1768 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1771 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1772 uint64_t param, uint64_t *status_reg)
1774 SigpInfo si = {
1775 .param = param,
1776 .status_reg = status_reg,
1779 /* cpu available? */
1780 if (dst_cpu == NULL) {
1781 return SIGP_CC_NOT_OPERATIONAL;
1784 /* only resets can break pending orders */
1785 if (dst_cpu->env.sigp_order != 0 &&
1786 order != SIGP_CPU_RESET &&
1787 order != SIGP_INITIAL_CPU_RESET) {
1788 return SIGP_CC_BUSY;
1791 switch (order) {
1792 case SIGP_START:
1793 run_on_cpu(CPU(dst_cpu), sigp_start, RUN_ON_CPU_HOST_PTR(&si));
1794 break;
1795 case SIGP_STOP:
1796 run_on_cpu(CPU(dst_cpu), sigp_stop, RUN_ON_CPU_HOST_PTR(&si));
1797 break;
1798 case SIGP_RESTART:
1799 run_on_cpu(CPU(dst_cpu), sigp_restart, RUN_ON_CPU_HOST_PTR(&si));
1800 break;
1801 case SIGP_STOP_STORE_STATUS:
1802 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, RUN_ON_CPU_HOST_PTR(&si));
1803 break;
1804 case SIGP_STORE_STATUS_ADDR:
1805 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, RUN_ON_CPU_HOST_PTR(&si));
1806 break;
1807 case SIGP_STORE_ADTL_STATUS:
1808 run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, RUN_ON_CPU_HOST_PTR(&si));
1809 break;
1810 case SIGP_SET_PREFIX:
1811 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, RUN_ON_CPU_HOST_PTR(&si));
1812 break;
1813 case SIGP_INITIAL_CPU_RESET:
1814 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, RUN_ON_CPU_HOST_PTR(&si));
1815 break;
1816 case SIGP_CPU_RESET:
1817 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, RUN_ON_CPU_HOST_PTR(&si));
1818 break;
1819 default:
1820 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1821 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1824 return si.cc;
1827 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1828 uint64_t *status_reg)
1830 CPUState *cur_cs;
1831 S390CPU *cur_cpu;
1832 bool all_stopped = true;
1834 CPU_FOREACH(cur_cs) {
1835 cur_cpu = S390_CPU(cur_cs);
1837 if (cur_cpu == cpu) {
1838 continue;
1840 if (s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1841 all_stopped = false;
1845 *status_reg &= 0xffffffff00000000ULL;
1847 /* Reject set arch order, with czam we're always in z/Arch mode. */
1848 *status_reg |= (all_stopped ? SIGP_STAT_INVALID_PARAMETER :
1849 SIGP_STAT_INCORRECT_STATE);
1850 return SIGP_CC_STATUS_STORED;
1853 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1855 CPUS390XState *env = &cpu->env;
1856 const uint8_t r1 = ipa1 >> 4;
1857 const uint8_t r3 = ipa1 & 0x0f;
1858 int ret;
1859 uint8_t order;
1860 uint64_t *status_reg;
1861 uint64_t param;
1862 S390CPU *dst_cpu = NULL;
1864 cpu_synchronize_state(CPU(cpu));
1866 /* get order code */
1867 order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1868 & SIGP_ORDER_MASK;
1869 status_reg = &env->regs[r1];
1870 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1872 if (qemu_mutex_trylock(&qemu_sigp_mutex)) {
1873 ret = SIGP_CC_BUSY;
1874 goto out;
1877 switch (order) {
1878 case SIGP_SET_ARCH:
1879 ret = sigp_set_architecture(cpu, param, status_reg);
1880 break;
1881 default:
1882 /* all other sigp orders target a single vcpu */
1883 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1884 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1886 qemu_mutex_unlock(&qemu_sigp_mutex);
1888 out:
1889 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1890 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1892 if (ret >= 0) {
1893 setcc(cpu, ret);
1894 return 0;
1897 return ret;
1900 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1902 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1903 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1904 int r = -1;
1906 DPRINTF("handle_instruction 0x%x 0x%x\n",
1907 run->s390_sieic.ipa, run->s390_sieic.ipb);
1908 switch (ipa0) {
1909 case IPA0_B2:
1910 r = handle_b2(cpu, run, ipa1);
1911 break;
1912 case IPA0_B9:
1913 r = handle_b9(cpu, run, ipa1);
1914 break;
1915 case IPA0_EB:
1916 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1917 break;
1918 case IPA0_E3:
1919 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1920 break;
1921 case IPA0_DIAG:
1922 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1923 break;
1924 case IPA0_SIGP:
1925 r = handle_sigp(cpu, run, ipa1);
1926 break;
1929 if (r < 0) {
1930 r = 0;
1931 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1934 return r;
1937 static bool is_special_wait_psw(CPUState *cs)
1939 /* signal quiesce */
1940 return cs->kvm_run->psw_addr == 0xfffUL;
1943 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1945 CPUState *cs = CPU(cpu);
1947 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1948 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1949 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1950 s390_cpu_halt(cpu);
1951 qemu_system_guest_panicked(NULL);
1954 /* try to detect pgm check loops */
1955 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1957 CPUState *cs = CPU(cpu);
1958 PSW oldpsw, newpsw;
1960 cpu_synchronize_state(cs);
1961 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1962 offsetof(LowCore, program_new_psw));
1963 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1964 offsetof(LowCore, program_new_psw) + 8);
1965 oldpsw.mask = run->psw_mask;
1966 oldpsw.addr = run->psw_addr;
1968 * Avoid endless loops of operation exceptions, if the pgm new
1969 * PSW will cause a new operation exception.
1970 * The heuristic checks if the pgm new psw is within 6 bytes before
1971 * the faulting psw address (with same DAT, AS settings) and the
1972 * new psw is not a wait psw and the fault was not triggered by
1973 * problem state. In that case go into crashed state.
1976 if (oldpsw.addr - newpsw.addr <= 6 &&
1977 !(newpsw.mask & PSW_MASK_WAIT) &&
1978 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1979 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1980 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1981 unmanageable_intercept(cpu, "operation exception loop",
1982 offsetof(LowCore, program_new_psw));
1983 return EXCP_HALTED;
1985 return 0;
1988 static int handle_intercept(S390CPU *cpu)
1990 CPUState *cs = CPU(cpu);
1991 struct kvm_run *run = cs->kvm_run;
1992 int icpt_code = run->s390_sieic.icptcode;
1993 int r = 0;
1995 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1996 (long)cs->kvm_run->psw_addr);
1997 switch (icpt_code) {
1998 case ICPT_INSTRUCTION:
1999 r = handle_instruction(cpu, run);
2000 break;
2001 case ICPT_PROGRAM:
2002 unmanageable_intercept(cpu, "program interrupt",
2003 offsetof(LowCore, program_new_psw));
2004 r = EXCP_HALTED;
2005 break;
2006 case ICPT_EXT_INT:
2007 unmanageable_intercept(cpu, "external interrupt",
2008 offsetof(LowCore, external_new_psw));
2009 r = EXCP_HALTED;
2010 break;
2011 case ICPT_WAITPSW:
2012 /* disabled wait, since enabled wait is handled in kernel */
2013 cpu_synchronize_state(cs);
2014 if (s390_cpu_halt(cpu) == 0) {
2015 if (is_special_wait_psw(cs)) {
2016 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
2017 } else {
2018 qemu_system_guest_panicked(NULL);
2021 r = EXCP_HALTED;
2022 break;
2023 case ICPT_CPU_STOP:
2024 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
2025 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
2027 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
2028 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
2029 true);
2031 cpu->env.sigp_order = 0;
2032 r = EXCP_HALTED;
2033 break;
2034 case ICPT_OPEREXC:
2035 /* check for break points */
2036 r = handle_sw_breakpoint(cpu, run);
2037 if (r == -ENOENT) {
2038 /* Then check for potential pgm check loops */
2039 r = handle_oper_loop(cpu, run);
2040 if (r == 0) {
2041 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
2044 break;
2045 case ICPT_SOFT_INTERCEPT:
2046 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
2047 exit(1);
2048 break;
2049 case ICPT_IO:
2050 fprintf(stderr, "KVM unimplemented icpt IO\n");
2051 exit(1);
2052 break;
2053 default:
2054 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
2055 exit(1);
2056 break;
2059 return r;
2062 static int handle_tsch(S390CPU *cpu)
2064 CPUState *cs = CPU(cpu);
2065 struct kvm_run *run = cs->kvm_run;
2066 int ret;
2068 cpu_synchronize_state(cs);
2070 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
2071 if (ret < 0) {
2073 * Failure.
2074 * If an I/O interrupt had been dequeued, we have to reinject it.
2076 if (run->s390_tsch.dequeued) {
2077 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
2078 run->s390_tsch.subchannel_nr,
2079 run->s390_tsch.io_int_parm,
2080 run->s390_tsch.io_int_word);
2082 ret = 0;
2084 return ret;
2087 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
2089 struct sysib_322 sysib;
2090 int del;
2092 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
2093 return;
2095 /* Shift the stack of Extended Names to prepare for our own data */
2096 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
2097 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
2098 /* First virt level, that doesn't provide Ext Names delimits stack. It is
2099 * assumed it's not capable of managing Extended Names for lower levels.
2101 for (del = 1; del < sysib.count; del++) {
2102 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
2103 break;
2106 if (del < sysib.count) {
2107 memset(sysib.ext_names[del], 0,
2108 sizeof(sysib.ext_names[0]) * (sysib.count - del));
2110 /* Insert short machine name in EBCDIC, padded with blanks */
2111 if (qemu_name) {
2112 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
2113 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
2114 strlen(qemu_name)));
2116 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
2117 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
2118 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
2119 * considered by s390 as not capable of providing any Extended Name.
2120 * Therefore if no name was specified on qemu invocation, we go with the
2121 * same "KVMguest" default, which KVM has filled into short name field.
2123 if (qemu_name) {
2124 strncpy((char *)sysib.ext_names[0], qemu_name,
2125 sizeof(sysib.ext_names[0]));
2126 } else {
2127 strcpy((char *)sysib.ext_names[0], "KVMguest");
2129 /* Insert UUID */
2130 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
2132 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
2135 static int handle_stsi(S390CPU *cpu)
2137 CPUState *cs = CPU(cpu);
2138 struct kvm_run *run = cs->kvm_run;
2140 switch (run->s390_stsi.fc) {
2141 case 3:
2142 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
2143 return 0;
2145 /* Only sysib 3.2.2 needs post-handling for now. */
2146 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
2147 return 0;
2148 default:
2149 return 0;
2153 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
2155 CPUState *cs = CPU(cpu);
2156 struct kvm_run *run = cs->kvm_run;
2158 int ret = 0;
2159 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
2161 switch (arch_info->type) {
2162 case KVM_HW_WP_WRITE:
2163 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
2164 cs->watchpoint_hit = &hw_watchpoint;
2165 hw_watchpoint.vaddr = arch_info->addr;
2166 hw_watchpoint.flags = BP_MEM_WRITE;
2167 ret = EXCP_DEBUG;
2169 break;
2170 case KVM_HW_BP:
2171 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
2172 ret = EXCP_DEBUG;
2174 break;
2175 case KVM_SINGLESTEP:
2176 if (cs->singlestep_enabled) {
2177 ret = EXCP_DEBUG;
2179 break;
2180 default:
2181 ret = -ENOSYS;
2184 return ret;
2187 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
2189 S390CPU *cpu = S390_CPU(cs);
2190 int ret = 0;
2192 qemu_mutex_lock_iothread();
2194 switch (run->exit_reason) {
2195 case KVM_EXIT_S390_SIEIC:
2196 ret = handle_intercept(cpu);
2197 break;
2198 case KVM_EXIT_S390_RESET:
2199 s390_reipl_request();
2200 break;
2201 case KVM_EXIT_S390_TSCH:
2202 ret = handle_tsch(cpu);
2203 break;
2204 case KVM_EXIT_S390_STSI:
2205 ret = handle_stsi(cpu);
2206 break;
2207 case KVM_EXIT_DEBUG:
2208 ret = kvm_arch_handle_debug_exit(cpu);
2209 break;
2210 default:
2211 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2212 break;
2214 qemu_mutex_unlock_iothread();
2216 if (ret == 0) {
2217 ret = EXCP_INTERRUPT;
2219 return ret;
2222 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2224 return true;
2227 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2228 uint16_t subchannel_nr, uint32_t io_int_parm,
2229 uint32_t io_int_word)
2231 struct kvm_s390_irq irq = {
2232 .u.io.subchannel_id = subchannel_id,
2233 .u.io.subchannel_nr = subchannel_nr,
2234 .u.io.io_int_parm = io_int_parm,
2235 .u.io.io_int_word = io_int_word,
2238 if (io_int_word & IO_INT_WORD_AI) {
2239 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2240 } else {
2241 irq.type = KVM_S390_INT_IO(0, (subchannel_id & 0xff00) >> 8,
2242 (subchannel_id & 0x0006),
2243 subchannel_nr);
2245 kvm_s390_floating_interrupt(&irq);
2248 static uint64_t build_channel_report_mcic(void)
2250 uint64_t mcic;
2252 /* subclass: indicate channel report pending */
2253 mcic = MCIC_SC_CP |
2254 /* subclass modifiers: none */
2255 /* storage errors: none */
2256 /* validity bits: no damage */
2257 MCIC_VB_WP | MCIC_VB_MS | MCIC_VB_PM | MCIC_VB_IA | MCIC_VB_FP |
2258 MCIC_VB_GR | MCIC_VB_CR | MCIC_VB_ST | MCIC_VB_AR | MCIC_VB_PR |
2259 MCIC_VB_FC | MCIC_VB_CT | MCIC_VB_CC;
2260 if (s390_has_feat(S390_FEAT_VECTOR)) {
2261 mcic |= MCIC_VB_VR;
2263 if (s390_has_feat(S390_FEAT_GUARDED_STORAGE)) {
2264 mcic |= MCIC_VB_GS;
2266 return mcic;
2269 void kvm_s390_crw_mchk(void)
2271 struct kvm_s390_irq irq = {
2272 .type = KVM_S390_MCHK,
2273 .u.mchk.cr14 = 1 << 28,
2274 .u.mchk.mcic = build_channel_report_mcic(),
2276 kvm_s390_floating_interrupt(&irq);
2279 void kvm_s390_enable_css_support(S390CPU *cpu)
2281 int r;
2283 /* Activate host kernel channel subsystem support. */
2284 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2285 assert(r == 0);
2288 void kvm_arch_init_irq_routing(KVMState *s)
2291 * Note that while irqchip capabilities generally imply that cpustates
2292 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2293 * have to override the common code kvm_halt_in_kernel_allowed setting.
2295 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2296 kvm_gsi_routing_allowed = true;
2297 kvm_halt_in_kernel_allowed = false;
2301 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2302 int vq, bool assign)
2304 struct kvm_ioeventfd kick = {
2305 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2306 KVM_IOEVENTFD_FLAG_DATAMATCH,
2307 .fd = event_notifier_get_fd(notifier),
2308 .datamatch = vq,
2309 .addr = sch,
2310 .len = 8,
2312 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2313 return -ENOSYS;
2315 if (!assign) {
2316 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2318 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2321 int kvm_s390_get_memslot_count(void)
2323 return kvm_check_extension(kvm_state, KVM_CAP_NR_MEMSLOTS);
2326 int kvm_s390_get_ri(void)
2328 return cap_ri;
2331 int kvm_s390_get_gs(void)
2333 return cap_gs;
2336 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2338 struct kvm_mp_state mp_state = {};
2339 int ret;
2341 /* the kvm part might not have been initialized yet */
2342 if (CPU(cpu)->kvm_state == NULL) {
2343 return 0;
2346 switch (cpu_state) {
2347 case CPU_STATE_STOPPED:
2348 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2349 break;
2350 case CPU_STATE_CHECK_STOP:
2351 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2352 break;
2353 case CPU_STATE_OPERATING:
2354 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2355 break;
2356 case CPU_STATE_LOAD:
2357 mp_state.mp_state = KVM_MP_STATE_LOAD;
2358 break;
2359 default:
2360 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2361 cpu_state);
2362 exit(1);
2365 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2366 if (ret) {
2367 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2368 strerror(-ret));
2371 return ret;
2374 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2376 struct kvm_s390_irq_state irq_state;
2377 CPUState *cs = CPU(cpu);
2378 int32_t bytes;
2380 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2381 return;
2384 irq_state.buf = (uint64_t) cpu->irqstate;
2385 irq_state.len = VCPU_IRQ_BUF_SIZE;
2387 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2388 if (bytes < 0) {
2389 cpu->irqstate_saved_size = 0;
2390 error_report("Migration of interrupt state failed");
2391 return;
2394 cpu->irqstate_saved_size = bytes;
2397 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2399 CPUState *cs = CPU(cpu);
2400 struct kvm_s390_irq_state irq_state;
2401 int r;
2403 if (cpu->irqstate_saved_size == 0) {
2404 return 0;
2407 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2408 return -ENOSYS;
2411 irq_state.buf = (uint64_t) cpu->irqstate;
2412 irq_state.len = cpu->irqstate_saved_size;
2414 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2415 if (r) {
2416 error_report("Setting interrupt state failed %d", r);
2418 return r;
2421 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2422 uint64_t address, uint32_t data, PCIDevice *dev)
2424 S390PCIBusDevice *pbdev;
2425 uint32_t idx = data >> ZPCI_MSI_VEC_BITS;
2426 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2428 pbdev = s390_pci_find_dev_by_idx(s390_get_phb(), idx);
2429 if (!pbdev) {
2430 DPRINTF("add_msi_route no dev\n");
2431 return -ENODEV;
2434 pbdev->routes.adapter.ind_offset = vec;
2436 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2437 route->flags = 0;
2438 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2439 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2440 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2441 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2442 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2443 return 0;
2446 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2447 int vector, PCIDevice *dev)
2449 return 0;
2452 int kvm_arch_release_virq_post(int virq)
2454 return 0;
2457 int kvm_arch_msi_data_to_gsi(uint32_t data)
2459 abort();
2462 static int query_cpu_subfunc(S390FeatBitmap features)
2464 struct kvm_s390_vm_cpu_subfunc prop;
2465 struct kvm_device_attr attr = {
2466 .group = KVM_S390_VM_CPU_MODEL,
2467 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2468 .addr = (uint64_t) &prop,
2470 int rc;
2472 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2473 if (rc) {
2474 return rc;
2478 * We're going to add all subfunctions now, if the corresponding feature
2479 * is available that unlocks the query functions.
2481 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2482 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2483 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2485 if (test_bit(S390_FEAT_MSA, features)) {
2486 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2487 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2488 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2489 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2490 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2492 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2493 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2495 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2496 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2497 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2498 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2499 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2501 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2502 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2504 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2505 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2507 return 0;
2510 static int configure_cpu_subfunc(const S390FeatBitmap features)
2512 struct kvm_s390_vm_cpu_subfunc prop = {};
2513 struct kvm_device_attr attr = {
2514 .group = KVM_S390_VM_CPU_MODEL,
2515 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2516 .addr = (uint64_t) &prop,
2519 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2520 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2521 /* hardware support might be missing, IBC will handle most of this */
2522 return 0;
2525 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2526 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2527 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2529 if (test_bit(S390_FEAT_MSA, features)) {
2530 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2531 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2532 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2533 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2534 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2536 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2537 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2539 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2540 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2541 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2542 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2543 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2545 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2546 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2548 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2549 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2551 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2554 static int kvm_to_feat[][2] = {
2555 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2556 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2557 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2558 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2559 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2560 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2561 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2562 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2563 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2564 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2565 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2566 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2567 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2568 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2571 static int query_cpu_feat(S390FeatBitmap features)
2573 struct kvm_s390_vm_cpu_feat prop;
2574 struct kvm_device_attr attr = {
2575 .group = KVM_S390_VM_CPU_MODEL,
2576 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2577 .addr = (uint64_t) &prop,
2579 int rc;
2580 int i;
2582 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2583 if (rc) {
2584 return rc;
2587 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2588 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2589 set_bit(kvm_to_feat[i][1], features);
2592 return 0;
2595 static int configure_cpu_feat(const S390FeatBitmap features)
2597 struct kvm_s390_vm_cpu_feat prop = {};
2598 struct kvm_device_attr attr = {
2599 .group = KVM_S390_VM_CPU_MODEL,
2600 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2601 .addr = (uint64_t) &prop,
2603 int i;
2605 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2606 if (test_bit(kvm_to_feat[i][1], features)) {
2607 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2610 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2613 bool kvm_s390_cpu_models_supported(void)
2615 if (!cpu_model_allowed()) {
2616 /* compatibility machines interfere with the cpu model */
2617 return false;
2619 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2620 KVM_S390_VM_CPU_MACHINE) &&
2621 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2622 KVM_S390_VM_CPU_PROCESSOR) &&
2623 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2624 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2625 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2626 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2627 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2628 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2631 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2633 struct kvm_s390_vm_cpu_machine prop = {};
2634 struct kvm_device_attr attr = {
2635 .group = KVM_S390_VM_CPU_MODEL,
2636 .attr = KVM_S390_VM_CPU_MACHINE,
2637 .addr = (uint64_t) &prop,
2639 uint16_t unblocked_ibc = 0, cpu_type = 0;
2640 int rc;
2642 memset(model, 0, sizeof(*model));
2644 if (!kvm_s390_cpu_models_supported()) {
2645 error_setg(errp, "KVM doesn't support CPU models");
2646 return;
2649 /* query the basic cpu model properties */
2650 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2651 if (rc) {
2652 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2653 return;
2656 cpu_type = cpuid_type(prop.cpuid);
2657 if (has_ibc(prop.ibc)) {
2658 model->lowest_ibc = lowest_ibc(prop.ibc);
2659 unblocked_ibc = unblocked_ibc(prop.ibc);
2661 model->cpu_id = cpuid_id(prop.cpuid);
2662 model->cpu_id_format = cpuid_format(prop.cpuid);
2663 model->cpu_ver = 0xff;
2665 /* get supported cpu features indicated via STFL(E) */
2666 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2667 (uint8_t *) prop.fac_mask);
2668 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2669 if (test_bit(S390_FEAT_STFLE, model->features)) {
2670 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2672 /* get supported cpu features indicated e.g. via SCLP */
2673 rc = query_cpu_feat(model->features);
2674 if (rc) {
2675 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2676 return;
2678 /* get supported cpu subfunctions indicated via query / test bit */
2679 rc = query_cpu_subfunc(model->features);
2680 if (rc) {
2681 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2682 return;
2685 /* with cpu model support, CMM is only indicated if really available */
2686 if (kvm_s390_cmma_available()) {
2687 set_bit(S390_FEAT_CMM, model->features);
2688 } else {
2689 /* no cmm -> no cmm nt */
2690 clear_bit(S390_FEAT_CMM_NT, model->features);
2693 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2694 if (pci_available) {
2695 set_bit(S390_FEAT_ZPCI, model->features);
2697 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2699 if (s390_known_cpu_type(cpu_type)) {
2700 /* we want the exact model, even if some features are missing */
2701 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2702 ibc_ec_ga(unblocked_ibc), NULL);
2703 } else {
2704 /* model unknown, e.g. too new - search using features */
2705 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2706 ibc_ec_ga(unblocked_ibc),
2707 model->features);
2709 if (!model->def) {
2710 error_setg(errp, "KVM: host CPU model could not be identified");
2711 return;
2713 /* strip of features that are not part of the maximum model */
2714 bitmap_and(model->features, model->features, model->def->full_feat,
2715 S390_FEAT_MAX);
2718 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2720 struct kvm_s390_vm_cpu_processor prop = {
2721 .fac_list = { 0 },
2723 struct kvm_device_attr attr = {
2724 .group = KVM_S390_VM_CPU_MODEL,
2725 .attr = KVM_S390_VM_CPU_PROCESSOR,
2726 .addr = (uint64_t) &prop,
2728 int rc;
2730 if (!model) {
2731 /* compatibility handling if cpu models are disabled */
2732 if (kvm_s390_cmma_available()) {
2733 kvm_s390_enable_cmma();
2735 return;
2737 if (!kvm_s390_cpu_models_supported()) {
2738 error_setg(errp, "KVM doesn't support CPU models");
2739 return;
2741 prop.cpuid = s390_cpuid_from_cpu_model(model);
2742 prop.ibc = s390_ibc_from_cpu_model(model);
2743 /* configure cpu features indicated via STFL(e) */
2744 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2745 (uint8_t *) prop.fac_list);
2746 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2747 if (rc) {
2748 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2749 return;
2751 /* configure cpu features indicated e.g. via SCLP */
2752 rc = configure_cpu_feat(model->features);
2753 if (rc) {
2754 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2755 return;
2757 /* configure cpu subfunctions indicated via query / test bit */
2758 rc = configure_cpu_subfunc(model->features);
2759 if (rc) {
2760 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2761 return;
2763 /* enable CMM via CMMA */
2764 if (test_bit(S390_FEAT_CMM, model->features)) {
2765 kvm_s390_enable_cmma();