linux-headers: update
[qemu.git] / target / s390x / kvm.c
blobd923cf4240a92e6ba6f9096844da1ec2f5da9bcf
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 "qapi/error.h"
35 #include "qemu/error-report.h"
36 #include "qemu/timer.h"
37 #include "sysemu/sysemu.h"
38 #include "sysemu/hw_accel.h"
39 #include "hw/hw.h"
40 #include "sysemu/device_tree.h"
41 #include "exec/gdbstub.h"
42 #include "trace.h"
43 #include "hw/s390x/s390-pci-inst.h"
44 #include "hw/s390x/s390-pci-bus.h"
45 #include "hw/s390x/ipl.h"
46 #include "hw/s390x/ebcdic.h"
47 #include "exec/memattrs.h"
48 #include "hw/s390x/s390-virtio-ccw.h"
49 #include "hw/s390x/s390-virtio-hcall.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 int cap_sync_regs;
137 static int cap_async_pf;
138 static int cap_mem_op;
139 static int cap_s390_irq;
140 static int cap_ri;
141 static int cap_gs;
143 static int active_cmma;
145 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared);
147 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
149 struct kvm_device_attr attr = {
150 .group = KVM_S390_VM_MEM_CTRL,
151 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
152 .addr = (uint64_t) memory_limit,
155 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
158 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit)
160 int rc;
162 struct kvm_device_attr attr = {
163 .group = KVM_S390_VM_MEM_CTRL,
164 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
165 .addr = (uint64_t) &new_limit,
168 if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) {
169 return 0;
172 rc = kvm_s390_query_mem_limit(hw_limit);
173 if (rc) {
174 return rc;
175 } else if (*hw_limit < new_limit) {
176 return -E2BIG;
179 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
182 int kvm_s390_cmma_active(void)
184 return active_cmma;
187 static bool kvm_s390_cmma_available(void)
189 static bool initialized, value;
191 if (!initialized) {
192 initialized = true;
193 value = kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_ENABLE_CMMA) &&
194 kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_CLR_CMMA);
196 return value;
199 void kvm_s390_cmma_reset(void)
201 int rc;
202 struct kvm_device_attr attr = {
203 .group = KVM_S390_VM_MEM_CTRL,
204 .attr = KVM_S390_VM_MEM_CLR_CMMA,
207 if (!kvm_s390_cmma_active()) {
208 return;
211 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
212 trace_kvm_clear_cmma(rc);
215 static void kvm_s390_enable_cmma(void)
217 int rc;
218 struct kvm_device_attr attr = {
219 .group = KVM_S390_VM_MEM_CTRL,
220 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
223 if (mem_path) {
224 warn_report("CMM will not be enabled because it is not "
225 "compatible with hugetlbfs.");
226 return;
228 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
229 active_cmma = !rc;
230 trace_kvm_enable_cmma(rc);
233 static void kvm_s390_set_attr(uint64_t attr)
235 struct kvm_device_attr attribute = {
236 .group = KVM_S390_VM_CRYPTO,
237 .attr = attr,
240 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
242 if (ret) {
243 error_report("Failed to set crypto device attribute %lu: %s",
244 attr, strerror(-ret));
248 static void kvm_s390_init_aes_kw(void)
250 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
252 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
253 NULL)) {
254 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
257 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
258 kvm_s390_set_attr(attr);
262 static void kvm_s390_init_dea_kw(void)
264 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
266 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
267 NULL)) {
268 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
271 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
272 kvm_s390_set_attr(attr);
276 void kvm_s390_crypto_reset(void)
278 if (s390_has_feat(S390_FEAT_MSA_EXT_3)) {
279 kvm_s390_init_aes_kw();
280 kvm_s390_init_dea_kw();
284 int kvm_arch_init(MachineState *ms, KVMState *s)
286 MachineClass *mc = MACHINE_GET_CLASS(ms);
288 mc->default_cpu_type = S390_CPU_TYPE_NAME("host");
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 (cpu_model_allowed()) {
308 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
309 cap_gs = 1;
314 * The migration interface for ais was introduced with kernel 4.13
315 * but the capability itself had been active since 4.12. As migration
316 * support is considered necessary let's disable ais in the 2.10
317 * machine.
319 /* kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0); */
321 return 0;
324 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
326 return 0;
329 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
331 return cpu->cpu_index;
334 int kvm_arch_init_vcpu(CPUState *cs)
336 S390CPU *cpu = S390_CPU(cs);
337 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
338 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
339 return 0;
342 void kvm_s390_reset_vcpu(S390CPU *cpu)
344 CPUState *cs = CPU(cpu);
346 /* The initial reset call is needed here to reset in-kernel
347 * vcpu data that we can't access directly from QEMU
348 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
349 * Before this ioctl cpu_synchronize_state() is called in common kvm
350 * code (kvm-all) */
351 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
352 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
356 static int can_sync_regs(CPUState *cs, int regs)
358 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
361 int kvm_arch_put_registers(CPUState *cs, int level)
363 S390CPU *cpu = S390_CPU(cs);
364 CPUS390XState *env = &cpu->env;
365 struct kvm_sregs sregs;
366 struct kvm_regs regs;
367 struct kvm_fpu fpu = {};
368 int r;
369 int i;
371 /* always save the PSW and the GPRS*/
372 cs->kvm_run->psw_addr = env->psw.addr;
373 cs->kvm_run->psw_mask = env->psw.mask;
375 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
376 for (i = 0; i < 16; i++) {
377 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
378 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
380 } else {
381 for (i = 0; i < 16; i++) {
382 regs.gprs[i] = env->regs[i];
384 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
385 if (r < 0) {
386 return r;
390 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
391 for (i = 0; i < 32; i++) {
392 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
393 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
395 cs->kvm_run->s.regs.fpc = env->fpc;
396 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
397 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
398 for (i = 0; i < 16; i++) {
399 cs->kvm_run->s.regs.fprs[i] = get_freg(env, i)->ll;
401 cs->kvm_run->s.regs.fpc = env->fpc;
402 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
403 } else {
404 /* Floating point */
405 for (i = 0; i < 16; i++) {
406 fpu.fprs[i] = get_freg(env, i)->ll;
408 fpu.fpc = env->fpc;
410 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
411 if (r < 0) {
412 return r;
416 /* Do we need to save more than that? */
417 if (level == KVM_PUT_RUNTIME_STATE) {
418 return 0;
421 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
422 cs->kvm_run->s.regs.cputm = env->cputm;
423 cs->kvm_run->s.regs.ckc = env->ckc;
424 cs->kvm_run->s.regs.todpr = env->todpr;
425 cs->kvm_run->s.regs.gbea = env->gbea;
426 cs->kvm_run->s.regs.pp = env->pp;
427 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
428 } else {
430 * These ONE_REGS are not protected by a capability. As they are only
431 * necessary for migration we just trace a possible error, but don't
432 * return with an error return code.
434 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
435 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
436 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
437 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
438 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
441 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
442 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
443 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
446 /* pfault parameters */
447 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
448 cs->kvm_run->s.regs.pft = env->pfault_token;
449 cs->kvm_run->s.regs.pfs = env->pfault_select;
450 cs->kvm_run->s.regs.pfc = env->pfault_compare;
451 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
452 } else if (cap_async_pf) {
453 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
454 if (r < 0) {
455 return r;
457 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
458 if (r < 0) {
459 return r;
461 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
462 if (r < 0) {
463 return r;
467 /* access registers and control registers*/
468 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
469 for (i = 0; i < 16; i++) {
470 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
471 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
473 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
474 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
475 } else {
476 for (i = 0; i < 16; i++) {
477 sregs.acrs[i] = env->aregs[i];
478 sregs.crs[i] = env->cregs[i];
480 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
481 if (r < 0) {
482 return r;
486 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
487 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
488 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
491 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
492 cs->kvm_run->s.regs.bpbc = env->bpbc;
493 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_BPBC;
496 /* Finally the prefix */
497 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
498 cs->kvm_run->s.regs.prefix = env->psa;
499 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
500 } else {
501 /* prefix is only supported via sync regs */
503 return 0;
506 int kvm_arch_get_registers(CPUState *cs)
508 S390CPU *cpu = S390_CPU(cs);
509 CPUS390XState *env = &cpu->env;
510 struct kvm_sregs sregs;
511 struct kvm_regs regs;
512 struct kvm_fpu fpu;
513 int i, r;
515 /* get the PSW */
516 env->psw.addr = cs->kvm_run->psw_addr;
517 env->psw.mask = cs->kvm_run->psw_mask;
519 /* the GPRS */
520 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
521 for (i = 0; i < 16; i++) {
522 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
524 } else {
525 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
526 if (r < 0) {
527 return r;
529 for (i = 0; i < 16; i++) {
530 env->regs[i] = regs.gprs[i];
534 /* The ACRS and CRS */
535 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
536 for (i = 0; i < 16; i++) {
537 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
538 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
540 } else {
541 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
542 if (r < 0) {
543 return r;
545 for (i = 0; i < 16; i++) {
546 env->aregs[i] = sregs.acrs[i];
547 env->cregs[i] = sregs.crs[i];
551 /* Floating point and vector registers */
552 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
553 for (i = 0; i < 32; i++) {
554 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
555 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
557 env->fpc = cs->kvm_run->s.regs.fpc;
558 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
559 for (i = 0; i < 16; i++) {
560 get_freg(env, i)->ll = cs->kvm_run->s.regs.fprs[i];
562 env->fpc = cs->kvm_run->s.regs.fpc;
563 } else {
564 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
565 if (r < 0) {
566 return r;
568 for (i = 0; i < 16; i++) {
569 get_freg(env, i)->ll = fpu.fprs[i];
571 env->fpc = fpu.fpc;
574 /* The prefix */
575 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
576 env->psa = cs->kvm_run->s.regs.prefix;
579 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
580 env->cputm = cs->kvm_run->s.regs.cputm;
581 env->ckc = cs->kvm_run->s.regs.ckc;
582 env->todpr = cs->kvm_run->s.regs.todpr;
583 env->gbea = cs->kvm_run->s.regs.gbea;
584 env->pp = cs->kvm_run->s.regs.pp;
585 } else {
587 * These ONE_REGS are not protected by a capability. As they are only
588 * necessary for migration we just trace a possible error, but don't
589 * return with an error return code.
591 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
592 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
593 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
594 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
595 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
598 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
599 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
602 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
603 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
606 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
607 env->bpbc = cs->kvm_run->s.regs.bpbc;
610 /* pfault parameters */
611 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
612 env->pfault_token = cs->kvm_run->s.regs.pft;
613 env->pfault_select = cs->kvm_run->s.regs.pfs;
614 env->pfault_compare = cs->kvm_run->s.regs.pfc;
615 } else if (cap_async_pf) {
616 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
617 if (r < 0) {
618 return r;
620 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
621 if (r < 0) {
622 return r;
624 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
625 if (r < 0) {
626 return r;
630 return 0;
633 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
635 int r;
636 struct kvm_device_attr attr = {
637 .group = KVM_S390_VM_TOD,
638 .attr = KVM_S390_VM_TOD_LOW,
639 .addr = (uint64_t)tod_low,
642 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
643 if (r) {
644 return r;
647 attr.attr = KVM_S390_VM_TOD_HIGH;
648 attr.addr = (uint64_t)tod_high;
649 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
652 int kvm_s390_get_clock_ext(uint8_t *tod_high, uint64_t *tod_low)
654 int r;
655 struct kvm_s390_vm_tod_clock gtod;
656 struct kvm_device_attr attr = {
657 .group = KVM_S390_VM_TOD,
658 .attr = KVM_S390_VM_TOD_EXT,
659 .addr = (uint64_t)&gtod,
662 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
663 *tod_high = gtod.epoch_idx;
664 *tod_low = gtod.tod;
666 return r;
669 int kvm_s390_set_clock(uint8_t tod_high, uint64_t tod_low)
671 int r;
672 struct kvm_device_attr attr = {
673 .group = KVM_S390_VM_TOD,
674 .attr = KVM_S390_VM_TOD_LOW,
675 .addr = (uint64_t)&tod_low,
678 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
679 if (r) {
680 return r;
683 attr.attr = KVM_S390_VM_TOD_HIGH;
684 attr.addr = (uint64_t)&tod_high;
685 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
688 int kvm_s390_set_clock_ext(uint8_t tod_high, uint64_t tod_low)
690 struct kvm_s390_vm_tod_clock gtod = {
691 .epoch_idx = tod_high,
692 .tod = tod_low,
694 struct kvm_device_attr attr = {
695 .group = KVM_S390_VM_TOD,
696 .attr = KVM_S390_VM_TOD_EXT,
697 .addr = (uint64_t)&gtod,
700 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
704 * kvm_s390_mem_op:
705 * @addr: the logical start address in guest memory
706 * @ar: the access register number
707 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
708 * @len: length that should be transferred
709 * @is_write: true = write, false = read
710 * Returns: 0 on success, non-zero if an exception or error occurred
712 * Use KVM ioctl to read/write from/to guest memory. An access exception
713 * is injected into the vCPU in case of translation errors.
715 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
716 int len, bool is_write)
718 struct kvm_s390_mem_op mem_op = {
719 .gaddr = addr,
720 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
721 .size = len,
722 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
723 : KVM_S390_MEMOP_LOGICAL_READ,
724 .buf = (uint64_t)hostbuf,
725 .ar = ar,
727 int ret;
729 if (!cap_mem_op) {
730 return -ENOSYS;
732 if (!hostbuf) {
733 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
736 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
737 if (ret < 0) {
738 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
740 return ret;
744 * Legacy layout for s390:
745 * Older S390 KVM requires the topmost vma of the RAM to be
746 * smaller than an system defined value, which is at least 256GB.
747 * Larger systems have larger values. We put the guest between
748 * the end of data segment (system break) and this value. We
749 * use 32GB as a base to have enough room for the system break
750 * to grow. We also have to use MAP parameters that avoid
751 * read-only mapping of guest pages.
753 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared)
755 static void *mem;
757 if (mem) {
758 /* we only support one allocation, which is enough for initial ram */
759 return NULL;
762 mem = mmap((void *) 0x800000000ULL, size,
763 PROT_EXEC|PROT_READ|PROT_WRITE,
764 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
765 if (mem == MAP_FAILED) {
766 mem = NULL;
768 if (mem && align) {
769 *align = QEMU_VMALLOC_ALIGN;
771 return mem;
774 static uint8_t const *sw_bp_inst;
775 static uint8_t sw_bp_ilen;
777 static void determine_sw_breakpoint_instr(void)
779 /* DIAG 501 is used for sw breakpoints with old kernels */
780 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
781 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
782 static const uint8_t instr_0x0000[] = {0x00, 0x00};
784 if (sw_bp_inst) {
785 return;
787 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
788 sw_bp_inst = diag_501;
789 sw_bp_ilen = sizeof(diag_501);
790 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
791 } else {
792 sw_bp_inst = instr_0x0000;
793 sw_bp_ilen = sizeof(instr_0x0000);
794 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
798 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
800 determine_sw_breakpoint_instr();
802 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
803 sw_bp_ilen, 0) ||
804 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
805 return -EINVAL;
807 return 0;
810 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
812 uint8_t t[MAX_ILEN];
814 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
815 return -EINVAL;
816 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
817 return -EINVAL;
818 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
819 sw_bp_ilen, 1)) {
820 return -EINVAL;
823 return 0;
826 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
827 int len, int type)
829 int n;
831 for (n = 0; n < nb_hw_breakpoints; n++) {
832 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
833 (hw_breakpoints[n].len == len || len == -1)) {
834 return &hw_breakpoints[n];
838 return NULL;
841 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
843 int size;
845 if (find_hw_breakpoint(addr, len, type)) {
846 return -EEXIST;
849 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
851 if (!hw_breakpoints) {
852 nb_hw_breakpoints = 0;
853 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
854 } else {
855 hw_breakpoints =
856 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
859 if (!hw_breakpoints) {
860 nb_hw_breakpoints = 0;
861 return -ENOMEM;
864 hw_breakpoints[nb_hw_breakpoints].addr = addr;
865 hw_breakpoints[nb_hw_breakpoints].len = len;
866 hw_breakpoints[nb_hw_breakpoints].type = type;
868 nb_hw_breakpoints++;
870 return 0;
873 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
874 target_ulong len, int type)
876 switch (type) {
877 case GDB_BREAKPOINT_HW:
878 type = KVM_HW_BP;
879 break;
880 case GDB_WATCHPOINT_WRITE:
881 if (len < 1) {
882 return -EINVAL;
884 type = KVM_HW_WP_WRITE;
885 break;
886 default:
887 return -ENOSYS;
889 return insert_hw_breakpoint(addr, len, type);
892 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
893 target_ulong len, int type)
895 int size;
896 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
898 if (bp == NULL) {
899 return -ENOENT;
902 nb_hw_breakpoints--;
903 if (nb_hw_breakpoints > 0) {
905 * In order to trim the array, move the last element to the position to
906 * be removed - if necessary.
908 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
909 *bp = hw_breakpoints[nb_hw_breakpoints];
911 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
912 hw_breakpoints =
913 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
914 } else {
915 g_free(hw_breakpoints);
916 hw_breakpoints = NULL;
919 return 0;
922 void kvm_arch_remove_all_hw_breakpoints(void)
924 nb_hw_breakpoints = 0;
925 g_free(hw_breakpoints);
926 hw_breakpoints = NULL;
929 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
931 int i;
933 if (nb_hw_breakpoints > 0) {
934 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
935 dbg->arch.hw_bp = hw_breakpoints;
937 for (i = 0; i < nb_hw_breakpoints; ++i) {
938 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
939 hw_breakpoints[i].addr);
941 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
942 } else {
943 dbg->arch.nr_hw_bp = 0;
944 dbg->arch.hw_bp = NULL;
948 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
952 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
954 return MEMTXATTRS_UNSPECIFIED;
957 int kvm_arch_process_async_events(CPUState *cs)
959 return cs->halted;
962 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
963 struct kvm_s390_interrupt *interrupt)
965 int r = 0;
967 interrupt->type = irq->type;
968 switch (irq->type) {
969 case KVM_S390_INT_VIRTIO:
970 interrupt->parm = irq->u.ext.ext_params;
971 /* fall through */
972 case KVM_S390_INT_PFAULT_INIT:
973 case KVM_S390_INT_PFAULT_DONE:
974 interrupt->parm64 = irq->u.ext.ext_params2;
975 break;
976 case KVM_S390_PROGRAM_INT:
977 interrupt->parm = irq->u.pgm.code;
978 break;
979 case KVM_S390_SIGP_SET_PREFIX:
980 interrupt->parm = irq->u.prefix.address;
981 break;
982 case KVM_S390_INT_SERVICE:
983 interrupt->parm = irq->u.ext.ext_params;
984 break;
985 case KVM_S390_MCHK:
986 interrupt->parm = irq->u.mchk.cr14;
987 interrupt->parm64 = irq->u.mchk.mcic;
988 break;
989 case KVM_S390_INT_EXTERNAL_CALL:
990 interrupt->parm = irq->u.extcall.code;
991 break;
992 case KVM_S390_INT_EMERGENCY:
993 interrupt->parm = irq->u.emerg.code;
994 break;
995 case KVM_S390_SIGP_STOP:
996 case KVM_S390_RESTART:
997 break; /* These types have no parameters */
998 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
999 interrupt->parm = irq->u.io.subchannel_id << 16;
1000 interrupt->parm |= irq->u.io.subchannel_nr;
1001 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
1002 interrupt->parm64 |= irq->u.io.io_int_word;
1003 break;
1004 default:
1005 r = -EINVAL;
1006 break;
1008 return r;
1011 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
1013 struct kvm_s390_interrupt kvmint = {};
1014 int r;
1016 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1017 if (r < 0) {
1018 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1019 exit(1);
1022 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
1023 if (r < 0) {
1024 fprintf(stderr, "KVM failed to inject interrupt\n");
1025 exit(1);
1029 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
1031 CPUState *cs = CPU(cpu);
1032 int r;
1034 if (cap_s390_irq) {
1035 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
1036 if (!r) {
1037 return;
1039 error_report("KVM failed to inject interrupt %llx", irq->type);
1040 exit(1);
1043 inject_vcpu_irq_legacy(cs, irq);
1046 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq *irq)
1048 struct kvm_s390_interrupt kvmint = {};
1049 int r;
1051 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1052 if (r < 0) {
1053 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1054 exit(1);
1057 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1058 if (r < 0) {
1059 fprintf(stderr, "KVM failed to inject interrupt\n");
1060 exit(1);
1064 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1066 struct kvm_s390_irq irq = {
1067 .type = KVM_S390_PROGRAM_INT,
1068 .u.pgm.code = code,
1071 kvm_s390_vcpu_interrupt(cpu, &irq);
1074 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1076 struct kvm_s390_irq irq = {
1077 .type = KVM_S390_PROGRAM_INT,
1078 .u.pgm.code = code,
1079 .u.pgm.trans_exc_code = te_code,
1080 .u.pgm.exc_access_id = te_code & 3,
1083 kvm_s390_vcpu_interrupt(cpu, &irq);
1086 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1087 uint16_t ipbh0)
1089 CPUS390XState *env = &cpu->env;
1090 uint64_t sccb;
1091 uint32_t code;
1092 int r = 0;
1094 sccb = env->regs[ipbh0 & 0xf];
1095 code = env->regs[(ipbh0 & 0xf0) >> 4];
1097 r = sclp_service_call(env, sccb, code);
1098 if (r < 0) {
1099 kvm_s390_program_interrupt(cpu, -r);
1100 } else {
1101 setcc(cpu, r);
1104 return 0;
1107 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1109 CPUS390XState *env = &cpu->env;
1110 int rc = 0;
1111 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1113 switch (ipa1) {
1114 case PRIV_B2_XSCH:
1115 ioinst_handle_xsch(cpu, env->regs[1], RA_IGNORED);
1116 break;
1117 case PRIV_B2_CSCH:
1118 ioinst_handle_csch(cpu, env->regs[1], RA_IGNORED);
1119 break;
1120 case PRIV_B2_HSCH:
1121 ioinst_handle_hsch(cpu, env->regs[1], RA_IGNORED);
1122 break;
1123 case PRIV_B2_MSCH:
1124 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1125 break;
1126 case PRIV_B2_SSCH:
1127 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1128 break;
1129 case PRIV_B2_STCRW:
1130 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb, RA_IGNORED);
1131 break;
1132 case PRIV_B2_STSCH:
1133 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1134 break;
1135 case PRIV_B2_TSCH:
1136 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1137 fprintf(stderr, "Spurious tsch intercept\n");
1138 break;
1139 case PRIV_B2_CHSC:
1140 ioinst_handle_chsc(cpu, run->s390_sieic.ipb, RA_IGNORED);
1141 break;
1142 case PRIV_B2_TPI:
1143 /* This should have been handled by kvm already. */
1144 fprintf(stderr, "Spurious tpi intercept\n");
1145 break;
1146 case PRIV_B2_SCHM:
1147 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1148 run->s390_sieic.ipb, RA_IGNORED);
1149 break;
1150 case PRIV_B2_RSCH:
1151 ioinst_handle_rsch(cpu, env->regs[1], RA_IGNORED);
1152 break;
1153 case PRIV_B2_RCHP:
1154 ioinst_handle_rchp(cpu, env->regs[1], RA_IGNORED);
1155 break;
1156 case PRIV_B2_STCPS:
1157 /* We do not provide this instruction, it is suppressed. */
1158 break;
1159 case PRIV_B2_SAL:
1160 ioinst_handle_sal(cpu, env->regs[1], RA_IGNORED);
1161 break;
1162 case PRIV_B2_SIGA:
1163 /* Not provided, set CC = 3 for subchannel not operational */
1164 setcc(cpu, 3);
1165 break;
1166 case PRIV_B2_SCLP_CALL:
1167 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1168 break;
1169 default:
1170 rc = -1;
1171 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1172 break;
1175 return rc;
1178 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1179 uint8_t *ar)
1181 CPUS390XState *env = &cpu->env;
1182 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1183 uint32_t base2 = run->s390_sieic.ipb >> 28;
1184 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1185 ((run->s390_sieic.ipb & 0xff00) << 4);
1187 if (disp2 & 0x80000) {
1188 disp2 += 0xfff00000;
1190 if (ar) {
1191 *ar = base2;
1194 return (base2 ? env->regs[base2] : 0) +
1195 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1198 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1199 uint8_t *ar)
1201 CPUS390XState *env = &cpu->env;
1202 uint32_t base2 = run->s390_sieic.ipb >> 28;
1203 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1204 ((run->s390_sieic.ipb & 0xff00) << 4);
1206 if (disp2 & 0x80000) {
1207 disp2 += 0xfff00000;
1209 if (ar) {
1210 *ar = base2;
1213 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1216 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1218 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1220 if (s390_has_feat(S390_FEAT_ZPCI)) {
1221 return clp_service_call(cpu, r2, RA_IGNORED);
1222 } else {
1223 return -1;
1227 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1229 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1230 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1232 if (s390_has_feat(S390_FEAT_ZPCI)) {
1233 return pcilg_service_call(cpu, r1, r2, RA_IGNORED);
1234 } else {
1235 return -1;
1239 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1241 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1242 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1244 if (s390_has_feat(S390_FEAT_ZPCI)) {
1245 return pcistg_service_call(cpu, r1, r2, RA_IGNORED);
1246 } else {
1247 return -1;
1251 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1253 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1254 uint64_t fiba;
1255 uint8_t ar;
1257 if (s390_has_feat(S390_FEAT_ZPCI)) {
1258 fiba = get_base_disp_rxy(cpu, run, &ar);
1260 return stpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1261 } else {
1262 return -1;
1266 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1268 CPUS390XState *env = &cpu->env;
1269 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1270 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1271 uint8_t isc;
1272 uint16_t mode;
1273 int r;
1275 mode = env->regs[r1] & 0xffff;
1276 isc = (env->regs[r3] >> 27) & 0x7;
1277 r = css_do_sic(env, isc, mode);
1278 if (r) {
1279 kvm_s390_program_interrupt(cpu, -r);
1282 return 0;
1285 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1287 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1288 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1290 if (s390_has_feat(S390_FEAT_ZPCI)) {
1291 return rpcit_service_call(cpu, r1, r2, RA_IGNORED);
1292 } else {
1293 return -1;
1297 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1299 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1300 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1301 uint64_t gaddr;
1302 uint8_t ar;
1304 if (s390_has_feat(S390_FEAT_ZPCI)) {
1305 gaddr = get_base_disp_rsy(cpu, run, &ar);
1307 return pcistb_service_call(cpu, r1, r3, gaddr, ar, RA_IGNORED);
1308 } else {
1309 return -1;
1313 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1315 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1316 uint64_t fiba;
1317 uint8_t ar;
1319 if (s390_has_feat(S390_FEAT_ZPCI)) {
1320 fiba = get_base_disp_rxy(cpu, run, &ar);
1322 return mpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1323 } else {
1324 return -1;
1328 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1330 int r = 0;
1332 switch (ipa1) {
1333 case PRIV_B9_CLP:
1334 r = kvm_clp_service_call(cpu, run);
1335 break;
1336 case PRIV_B9_PCISTG:
1337 r = kvm_pcistg_service_call(cpu, run);
1338 break;
1339 case PRIV_B9_PCILG:
1340 r = kvm_pcilg_service_call(cpu, run);
1341 break;
1342 case PRIV_B9_RPCIT:
1343 r = kvm_rpcit_service_call(cpu, run);
1344 break;
1345 case PRIV_B9_EQBS:
1346 /* just inject exception */
1347 r = -1;
1348 break;
1349 default:
1350 r = -1;
1351 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1352 break;
1355 return r;
1358 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1360 int r = 0;
1362 switch (ipbl) {
1363 case PRIV_EB_PCISTB:
1364 r = kvm_pcistb_service_call(cpu, run);
1365 break;
1366 case PRIV_EB_SIC:
1367 r = kvm_sic_service_call(cpu, run);
1368 break;
1369 case PRIV_EB_SQBS:
1370 /* just inject exception */
1371 r = -1;
1372 break;
1373 default:
1374 r = -1;
1375 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1376 break;
1379 return r;
1382 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1384 int r = 0;
1386 switch (ipbl) {
1387 case PRIV_E3_MPCIFC:
1388 r = kvm_mpcifc_service_call(cpu, run);
1389 break;
1390 case PRIV_E3_STPCIFC:
1391 r = kvm_stpcifc_service_call(cpu, run);
1392 break;
1393 default:
1394 r = -1;
1395 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1396 break;
1399 return r;
1402 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1404 CPUS390XState *env = &cpu->env;
1405 int ret;
1407 ret = s390_virtio_hypercall(env);
1408 if (ret == -EINVAL) {
1409 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1410 return 0;
1413 return ret;
1416 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1418 uint64_t r1, r3;
1419 int rc;
1421 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1422 r3 = run->s390_sieic.ipa & 0x000f;
1423 rc = handle_diag_288(&cpu->env, r1, r3);
1424 if (rc) {
1425 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1429 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1431 uint64_t r1, r3;
1433 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1434 r3 = run->s390_sieic.ipa & 0x000f;
1435 handle_diag_308(&cpu->env, r1, r3, RA_IGNORED);
1438 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1440 CPUS390XState *env = &cpu->env;
1441 unsigned long pc;
1443 pc = env->psw.addr - sw_bp_ilen;
1444 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1445 env->psw.addr = pc;
1446 return EXCP_DEBUG;
1449 return -ENOENT;
1452 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1454 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1456 int r = 0;
1457 uint16_t func_code;
1460 * For any diagnose call we support, bits 48-63 of the resulting
1461 * address specify the function code; the remainder is ignored.
1463 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1464 switch (func_code) {
1465 case DIAG_TIMEREVENT:
1466 kvm_handle_diag_288(cpu, run);
1467 break;
1468 case DIAG_IPL:
1469 kvm_handle_diag_308(cpu, run);
1470 break;
1471 case DIAG_KVM_HYPERCALL:
1472 r = handle_hypercall(cpu, run);
1473 break;
1474 case DIAG_KVM_BREAKPOINT:
1475 r = handle_sw_breakpoint(cpu, run);
1476 break;
1477 default:
1478 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1479 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1480 break;
1483 return r;
1486 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb)
1488 CPUS390XState *env = &cpu->env;
1489 const uint8_t r1 = ipa1 >> 4;
1490 const uint8_t r3 = ipa1 & 0x0f;
1491 int ret;
1492 uint8_t order;
1494 /* get order code */
1495 order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK;
1497 ret = handle_sigp(env, order, r1, r3);
1498 setcc(cpu, ret);
1499 return 0;
1502 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1504 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1505 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1506 int r = -1;
1508 DPRINTF("handle_instruction 0x%x 0x%x\n",
1509 run->s390_sieic.ipa, run->s390_sieic.ipb);
1510 switch (ipa0) {
1511 case IPA0_B2:
1512 r = handle_b2(cpu, run, ipa1);
1513 break;
1514 case IPA0_B9:
1515 r = handle_b9(cpu, run, ipa1);
1516 break;
1517 case IPA0_EB:
1518 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1519 break;
1520 case IPA0_E3:
1521 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1522 break;
1523 case IPA0_DIAG:
1524 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1525 break;
1526 case IPA0_SIGP:
1527 r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb);
1528 break;
1531 if (r < 0) {
1532 r = 0;
1533 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1536 return r;
1539 static void unmanageable_intercept(S390CPU *cpu, S390CrashReason reason,
1540 int pswoffset)
1542 CPUState *cs = CPU(cpu);
1544 s390_cpu_halt(cpu);
1545 cpu->env.crash_reason = reason;
1546 qemu_system_guest_panicked(cpu_get_crash_info(cs));
1549 /* try to detect pgm check loops */
1550 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1552 CPUState *cs = CPU(cpu);
1553 PSW oldpsw, newpsw;
1555 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1556 offsetof(LowCore, program_new_psw));
1557 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1558 offsetof(LowCore, program_new_psw) + 8);
1559 oldpsw.mask = run->psw_mask;
1560 oldpsw.addr = run->psw_addr;
1562 * Avoid endless loops of operation exceptions, if the pgm new
1563 * PSW will cause a new operation exception.
1564 * The heuristic checks if the pgm new psw is within 6 bytes before
1565 * the faulting psw address (with same DAT, AS settings) and the
1566 * new psw is not a wait psw and the fault was not triggered by
1567 * problem state. In that case go into crashed state.
1570 if (oldpsw.addr - newpsw.addr <= 6 &&
1571 !(newpsw.mask & PSW_MASK_WAIT) &&
1572 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1573 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1574 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1575 unmanageable_intercept(cpu, S390_CRASH_REASON_OPINT_LOOP,
1576 offsetof(LowCore, program_new_psw));
1577 return EXCP_HALTED;
1579 return 0;
1582 static int handle_intercept(S390CPU *cpu)
1584 CPUState *cs = CPU(cpu);
1585 struct kvm_run *run = cs->kvm_run;
1586 int icpt_code = run->s390_sieic.icptcode;
1587 int r = 0;
1589 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1590 (long)cs->kvm_run->psw_addr);
1591 switch (icpt_code) {
1592 case ICPT_INSTRUCTION:
1593 r = handle_instruction(cpu, run);
1594 break;
1595 case ICPT_PROGRAM:
1596 unmanageable_intercept(cpu, S390_CRASH_REASON_PGMINT_LOOP,
1597 offsetof(LowCore, program_new_psw));
1598 r = EXCP_HALTED;
1599 break;
1600 case ICPT_EXT_INT:
1601 unmanageable_intercept(cpu, S390_CRASH_REASON_EXTINT_LOOP,
1602 offsetof(LowCore, external_new_psw));
1603 r = EXCP_HALTED;
1604 break;
1605 case ICPT_WAITPSW:
1606 /* disabled wait, since enabled wait is handled in kernel */
1607 s390_handle_wait(cpu);
1608 r = EXCP_HALTED;
1609 break;
1610 case ICPT_CPU_STOP:
1611 do_stop_interrupt(&cpu->env);
1612 r = EXCP_HALTED;
1613 break;
1614 case ICPT_OPEREXC:
1615 /* check for break points */
1616 r = handle_sw_breakpoint(cpu, run);
1617 if (r == -ENOENT) {
1618 /* Then check for potential pgm check loops */
1619 r = handle_oper_loop(cpu, run);
1620 if (r == 0) {
1621 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1624 break;
1625 case ICPT_SOFT_INTERCEPT:
1626 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1627 exit(1);
1628 break;
1629 case ICPT_IO:
1630 fprintf(stderr, "KVM unimplemented icpt IO\n");
1631 exit(1);
1632 break;
1633 default:
1634 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1635 exit(1);
1636 break;
1639 return r;
1642 static int handle_tsch(S390CPU *cpu)
1644 CPUState *cs = CPU(cpu);
1645 struct kvm_run *run = cs->kvm_run;
1646 int ret;
1648 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb,
1649 RA_IGNORED);
1650 if (ret < 0) {
1652 * Failure.
1653 * If an I/O interrupt had been dequeued, we have to reinject it.
1655 if (run->s390_tsch.dequeued) {
1656 s390_io_interrupt(run->s390_tsch.subchannel_id,
1657 run->s390_tsch.subchannel_nr,
1658 run->s390_tsch.io_int_parm,
1659 run->s390_tsch.io_int_word);
1661 ret = 0;
1663 return ret;
1666 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1668 SysIB_322 sysib;
1669 int del;
1671 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1672 return;
1674 /* Shift the stack of Extended Names to prepare for our own data */
1675 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1676 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1677 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1678 * assumed it's not capable of managing Extended Names for lower levels.
1680 for (del = 1; del < sysib.count; del++) {
1681 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1682 break;
1685 if (del < sysib.count) {
1686 memset(sysib.ext_names[del], 0,
1687 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1689 /* Insert short machine name in EBCDIC, padded with blanks */
1690 if (qemu_name) {
1691 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1692 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1693 strlen(qemu_name)));
1695 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1696 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1697 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1698 * considered by s390 as not capable of providing any Extended Name.
1699 * Therefore if no name was specified on qemu invocation, we go with the
1700 * same "KVMguest" default, which KVM has filled into short name field.
1702 if (qemu_name) {
1703 strncpy((char *)sysib.ext_names[0], qemu_name,
1704 sizeof(sysib.ext_names[0]));
1705 } else {
1706 strcpy((char *)sysib.ext_names[0], "KVMguest");
1708 /* Insert UUID */
1709 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
1711 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1714 static int handle_stsi(S390CPU *cpu)
1716 CPUState *cs = CPU(cpu);
1717 struct kvm_run *run = cs->kvm_run;
1719 switch (run->s390_stsi.fc) {
1720 case 3:
1721 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1722 return 0;
1724 /* Only sysib 3.2.2 needs post-handling for now. */
1725 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1726 return 0;
1727 default:
1728 return 0;
1732 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1734 CPUState *cs = CPU(cpu);
1735 struct kvm_run *run = cs->kvm_run;
1737 int ret = 0;
1738 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1740 switch (arch_info->type) {
1741 case KVM_HW_WP_WRITE:
1742 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1743 cs->watchpoint_hit = &hw_watchpoint;
1744 hw_watchpoint.vaddr = arch_info->addr;
1745 hw_watchpoint.flags = BP_MEM_WRITE;
1746 ret = EXCP_DEBUG;
1748 break;
1749 case KVM_HW_BP:
1750 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1751 ret = EXCP_DEBUG;
1753 break;
1754 case KVM_SINGLESTEP:
1755 if (cs->singlestep_enabled) {
1756 ret = EXCP_DEBUG;
1758 break;
1759 default:
1760 ret = -ENOSYS;
1763 return ret;
1766 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1768 S390CPU *cpu = S390_CPU(cs);
1769 int ret = 0;
1771 qemu_mutex_lock_iothread();
1773 kvm_cpu_synchronize_state(cs);
1775 switch (run->exit_reason) {
1776 case KVM_EXIT_S390_SIEIC:
1777 ret = handle_intercept(cpu);
1778 break;
1779 case KVM_EXIT_S390_RESET:
1780 s390_ipl_reset_request(cs, S390_RESET_REIPL);
1781 break;
1782 case KVM_EXIT_S390_TSCH:
1783 ret = handle_tsch(cpu);
1784 break;
1785 case KVM_EXIT_S390_STSI:
1786 ret = handle_stsi(cpu);
1787 break;
1788 case KVM_EXIT_DEBUG:
1789 ret = kvm_arch_handle_debug_exit(cpu);
1790 break;
1791 default:
1792 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1793 break;
1795 qemu_mutex_unlock_iothread();
1797 if (ret == 0) {
1798 ret = EXCP_INTERRUPT;
1800 return ret;
1803 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1805 return true;
1808 void kvm_s390_enable_css_support(S390CPU *cpu)
1810 int r;
1812 /* Activate host kernel channel subsystem support. */
1813 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1814 assert(r == 0);
1817 void kvm_arch_init_irq_routing(KVMState *s)
1820 * Note that while irqchip capabilities generally imply that cpustates
1821 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1822 * have to override the common code kvm_halt_in_kernel_allowed setting.
1824 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1825 kvm_gsi_routing_allowed = true;
1826 kvm_halt_in_kernel_allowed = false;
1830 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1831 int vq, bool assign)
1833 struct kvm_ioeventfd kick = {
1834 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1835 KVM_IOEVENTFD_FLAG_DATAMATCH,
1836 .fd = event_notifier_get_fd(notifier),
1837 .datamatch = vq,
1838 .addr = sch,
1839 .len = 8,
1841 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1842 return -ENOSYS;
1844 if (!assign) {
1845 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1847 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1850 int kvm_s390_get_ri(void)
1852 return cap_ri;
1855 int kvm_s390_get_gs(void)
1857 return cap_gs;
1860 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1862 struct kvm_mp_state mp_state = {};
1863 int ret;
1865 /* the kvm part might not have been initialized yet */
1866 if (CPU(cpu)->kvm_state == NULL) {
1867 return 0;
1870 switch (cpu_state) {
1871 case S390_CPU_STATE_STOPPED:
1872 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1873 break;
1874 case S390_CPU_STATE_CHECK_STOP:
1875 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1876 break;
1877 case S390_CPU_STATE_OPERATING:
1878 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1879 break;
1880 case S390_CPU_STATE_LOAD:
1881 mp_state.mp_state = KVM_MP_STATE_LOAD;
1882 break;
1883 default:
1884 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1885 cpu_state);
1886 exit(1);
1889 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1890 if (ret) {
1891 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1892 strerror(-ret));
1895 return ret;
1898 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
1900 struct kvm_s390_irq_state irq_state = {
1901 .buf = (uint64_t) cpu->irqstate,
1902 .len = VCPU_IRQ_BUF_SIZE,
1904 CPUState *cs = CPU(cpu);
1905 int32_t bytes;
1907 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1908 return;
1911 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
1912 if (bytes < 0) {
1913 cpu->irqstate_saved_size = 0;
1914 error_report("Migration of interrupt state failed");
1915 return;
1918 cpu->irqstate_saved_size = bytes;
1921 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
1923 CPUState *cs = CPU(cpu);
1924 struct kvm_s390_irq_state irq_state = {
1925 .buf = (uint64_t) cpu->irqstate,
1926 .len = cpu->irqstate_saved_size,
1928 int r;
1930 if (cpu->irqstate_saved_size == 0) {
1931 return 0;
1934 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1935 return -ENOSYS;
1938 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
1939 if (r) {
1940 error_report("Setting interrupt state failed %d", r);
1942 return r;
1945 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1946 uint64_t address, uint32_t data, PCIDevice *dev)
1948 S390PCIBusDevice *pbdev;
1949 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
1951 if (!dev) {
1952 DPRINTF("add_msi_route no pci device\n");
1953 return -ENODEV;
1956 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
1957 if (!pbdev) {
1958 DPRINTF("add_msi_route no zpci device\n");
1959 return -ENODEV;
1962 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
1963 route->flags = 0;
1964 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
1965 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
1966 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
1967 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
1968 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
1969 return 0;
1972 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
1973 int vector, PCIDevice *dev)
1975 return 0;
1978 int kvm_arch_release_virq_post(int virq)
1980 return 0;
1983 int kvm_arch_msi_data_to_gsi(uint32_t data)
1985 abort();
1988 static int query_cpu_subfunc(S390FeatBitmap features)
1990 struct kvm_s390_vm_cpu_subfunc prop;
1991 struct kvm_device_attr attr = {
1992 .group = KVM_S390_VM_CPU_MODEL,
1993 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
1994 .addr = (uint64_t) &prop,
1996 int rc;
1998 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
1999 if (rc) {
2000 return rc;
2004 * We're going to add all subfunctions now, if the corresponding feature
2005 * is available that unlocks the query functions.
2007 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2008 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2009 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2011 if (test_bit(S390_FEAT_MSA, features)) {
2012 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2013 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2014 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2015 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2016 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2018 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2019 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2021 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2022 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2023 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2024 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2025 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2027 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2028 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2030 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2031 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2033 return 0;
2036 static int configure_cpu_subfunc(const S390FeatBitmap features)
2038 struct kvm_s390_vm_cpu_subfunc prop = {};
2039 struct kvm_device_attr attr = {
2040 .group = KVM_S390_VM_CPU_MODEL,
2041 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2042 .addr = (uint64_t) &prop,
2045 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2046 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2047 /* hardware support might be missing, IBC will handle most of this */
2048 return 0;
2051 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2052 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2053 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2055 if (test_bit(S390_FEAT_MSA, features)) {
2056 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2057 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2058 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2059 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2060 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2062 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2063 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2065 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2066 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2067 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2068 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2069 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2071 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2072 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2074 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2075 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2077 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2080 static int kvm_to_feat[][2] = {
2081 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2082 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2083 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2084 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2085 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2086 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2087 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2088 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2089 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2090 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2091 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2092 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2093 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2094 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2097 static int query_cpu_feat(S390FeatBitmap features)
2099 struct kvm_s390_vm_cpu_feat prop;
2100 struct kvm_device_attr attr = {
2101 .group = KVM_S390_VM_CPU_MODEL,
2102 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2103 .addr = (uint64_t) &prop,
2105 int rc;
2106 int i;
2108 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2109 if (rc) {
2110 return rc;
2113 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2114 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2115 set_bit(kvm_to_feat[i][1], features);
2118 return 0;
2121 static int configure_cpu_feat(const S390FeatBitmap features)
2123 struct kvm_s390_vm_cpu_feat prop = {};
2124 struct kvm_device_attr attr = {
2125 .group = KVM_S390_VM_CPU_MODEL,
2126 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2127 .addr = (uint64_t) &prop,
2129 int i;
2131 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2132 if (test_bit(kvm_to_feat[i][1], features)) {
2133 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2136 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2139 bool kvm_s390_cpu_models_supported(void)
2141 if (!cpu_model_allowed()) {
2142 /* compatibility machines interfere with the cpu model */
2143 return false;
2145 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2146 KVM_S390_VM_CPU_MACHINE) &&
2147 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2148 KVM_S390_VM_CPU_PROCESSOR) &&
2149 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2150 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2151 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2152 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2153 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2154 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2157 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2159 struct kvm_s390_vm_cpu_machine prop = {};
2160 struct kvm_device_attr attr = {
2161 .group = KVM_S390_VM_CPU_MODEL,
2162 .attr = KVM_S390_VM_CPU_MACHINE,
2163 .addr = (uint64_t) &prop,
2165 uint16_t unblocked_ibc = 0, cpu_type = 0;
2166 int rc;
2168 memset(model, 0, sizeof(*model));
2170 if (!kvm_s390_cpu_models_supported()) {
2171 error_setg(errp, "KVM doesn't support CPU models");
2172 return;
2175 /* query the basic cpu model properties */
2176 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2177 if (rc) {
2178 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2179 return;
2182 cpu_type = cpuid_type(prop.cpuid);
2183 if (has_ibc(prop.ibc)) {
2184 model->lowest_ibc = lowest_ibc(prop.ibc);
2185 unblocked_ibc = unblocked_ibc(prop.ibc);
2187 model->cpu_id = cpuid_id(prop.cpuid);
2188 model->cpu_id_format = cpuid_format(prop.cpuid);
2189 model->cpu_ver = 0xff;
2191 /* get supported cpu features indicated via STFL(E) */
2192 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2193 (uint8_t *) prop.fac_mask);
2194 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2195 if (test_bit(S390_FEAT_STFLE, model->features)) {
2196 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2198 /* get supported cpu features indicated e.g. via SCLP */
2199 rc = query_cpu_feat(model->features);
2200 if (rc) {
2201 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2202 return;
2204 /* get supported cpu subfunctions indicated via query / test bit */
2205 rc = query_cpu_subfunc(model->features);
2206 if (rc) {
2207 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2208 return;
2211 /* PTFF subfunctions might be indicated although kernel support missing */
2212 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) {
2213 clear_bit(S390_FEAT_PTFF_QSIE, model->features);
2214 clear_bit(S390_FEAT_PTFF_QTOUE, model->features);
2215 clear_bit(S390_FEAT_PTFF_STOE, model->features);
2216 clear_bit(S390_FEAT_PTFF_STOUE, model->features);
2219 /* with cpu model support, CMM is only indicated if really available */
2220 if (kvm_s390_cmma_available()) {
2221 set_bit(S390_FEAT_CMM, model->features);
2222 } else {
2223 /* no cmm -> no cmm nt */
2224 clear_bit(S390_FEAT_CMM_NT, model->features);
2227 /* bpb needs kernel support for migration, VSIE and reset */
2228 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) {
2229 clear_bit(S390_FEAT_BPB, model->features);
2232 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2233 if (pci_available) {
2234 set_bit(S390_FEAT_ZPCI, model->features);
2236 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2238 if (s390_known_cpu_type(cpu_type)) {
2239 /* we want the exact model, even if some features are missing */
2240 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2241 ibc_ec_ga(unblocked_ibc), NULL);
2242 } else {
2243 /* model unknown, e.g. too new - search using features */
2244 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2245 ibc_ec_ga(unblocked_ibc),
2246 model->features);
2248 if (!model->def) {
2249 error_setg(errp, "KVM: host CPU model could not be identified");
2250 return;
2252 /* strip of features that are not part of the maximum model */
2253 bitmap_and(model->features, model->features, model->def->full_feat,
2254 S390_FEAT_MAX);
2257 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2259 struct kvm_s390_vm_cpu_processor prop = {
2260 .fac_list = { 0 },
2262 struct kvm_device_attr attr = {
2263 .group = KVM_S390_VM_CPU_MODEL,
2264 .attr = KVM_S390_VM_CPU_PROCESSOR,
2265 .addr = (uint64_t) &prop,
2267 int rc;
2269 if (!model) {
2270 /* compatibility handling if cpu models are disabled */
2271 if (kvm_s390_cmma_available()) {
2272 kvm_s390_enable_cmma();
2274 return;
2276 if (!kvm_s390_cpu_models_supported()) {
2277 error_setg(errp, "KVM doesn't support CPU models");
2278 return;
2280 prop.cpuid = s390_cpuid_from_cpu_model(model);
2281 prop.ibc = s390_ibc_from_cpu_model(model);
2282 /* configure cpu features indicated via STFL(e) */
2283 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2284 (uint8_t *) prop.fac_list);
2285 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2286 if (rc) {
2287 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2288 return;
2290 /* configure cpu features indicated e.g. via SCLP */
2291 rc = configure_cpu_feat(model->features);
2292 if (rc) {
2293 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2294 return;
2296 /* configure cpu subfunctions indicated via query / test bit */
2297 rc = configure_cpu_subfunc(model->features);
2298 if (rc) {
2299 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2300 return;
2302 /* enable CMM via CMMA */
2303 if (test_bit(S390_FEAT_CMM, model->features)) {
2304 kvm_s390_enable_cmma();
2308 void kvm_s390_restart_interrupt(S390CPU *cpu)
2310 struct kvm_s390_irq irq = {
2311 .type = KVM_S390_RESTART,
2314 kvm_s390_vcpu_interrupt(cpu, &irq);
2317 void kvm_s390_stop_interrupt(S390CPU *cpu)
2319 struct kvm_s390_irq irq = {
2320 .type = KVM_S390_SIGP_STOP,
2323 kvm_s390_vcpu_interrupt(cpu, &irq);