Merge remote-tracking branch 'remotes/bonzini-gitlab/tags/for-upstream' into staging
[qemu/ar7.git] / target / s390x / kvm.c
blob4fb3bbfef506302b817ea43ed8744361deaf3851
1 /*
2 * QEMU S390x KVM implementation
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp. 2012
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program 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 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include <sys/ioctl.h>
24 #include <linux/kvm.h>
25 #include <asm/ptrace.h>
27 #include "qemu-common.h"
28 #include "cpu.h"
29 #include "internal.h"
30 #include "kvm_s390x.h"
31 #include "sysemu/kvm_int.h"
32 #include "qemu/cutils.h"
33 #include "qapi/error.h"
34 #include "qemu/error-report.h"
35 #include "qemu/timer.h"
36 #include "qemu/units.h"
37 #include "qemu/main-loop.h"
38 #include "qemu/mmap-alloc.h"
39 #include "qemu/log.h"
40 #include "sysemu/sysemu.h"
41 #include "sysemu/hw_accel.h"
42 #include "sysemu/runstate.h"
43 #include "sysemu/device_tree.h"
44 #include "exec/gdbstub.h"
45 #include "exec/ram_addr.h"
46 #include "trace.h"
47 #include "hw/s390x/s390-pci-inst.h"
48 #include "hw/s390x/s390-pci-bus.h"
49 #include "hw/s390x/ipl.h"
50 #include "hw/s390x/ebcdic.h"
51 #include "exec/memattrs.h"
52 #include "hw/s390x/s390-virtio-ccw.h"
53 #include "hw/s390x/s390-virtio-hcall.h"
54 #include "hw/s390x/pv.h"
56 #ifndef DEBUG_KVM
57 #define DEBUG_KVM 0
58 #endif
60 #define DPRINTF(fmt, ...) do { \
61 if (DEBUG_KVM) { \
62 fprintf(stderr, fmt, ## __VA_ARGS__); \
63 } \
64 } while (0)
66 #define kvm_vm_check_mem_attr(s, attr) \
67 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
69 #define IPA0_DIAG 0x8300
70 #define IPA0_SIGP 0xae00
71 #define IPA0_B2 0xb200
72 #define IPA0_B9 0xb900
73 #define IPA0_EB 0xeb00
74 #define IPA0_E3 0xe300
76 #define PRIV_B2_SCLP_CALL 0x20
77 #define PRIV_B2_CSCH 0x30
78 #define PRIV_B2_HSCH 0x31
79 #define PRIV_B2_MSCH 0x32
80 #define PRIV_B2_SSCH 0x33
81 #define PRIV_B2_STSCH 0x34
82 #define PRIV_B2_TSCH 0x35
83 #define PRIV_B2_TPI 0x36
84 #define PRIV_B2_SAL 0x37
85 #define PRIV_B2_RSCH 0x38
86 #define PRIV_B2_STCRW 0x39
87 #define PRIV_B2_STCPS 0x3a
88 #define PRIV_B2_RCHP 0x3b
89 #define PRIV_B2_SCHM 0x3c
90 #define PRIV_B2_CHSC 0x5f
91 #define PRIV_B2_SIGA 0x74
92 #define PRIV_B2_XSCH 0x76
94 #define PRIV_EB_SQBS 0x8a
95 #define PRIV_EB_PCISTB 0xd0
96 #define PRIV_EB_SIC 0xd1
98 #define PRIV_B9_EQBS 0x9c
99 #define PRIV_B9_CLP 0xa0
100 #define PRIV_B9_PCISTG 0xd0
101 #define PRIV_B9_PCILG 0xd2
102 #define PRIV_B9_RPCIT 0xd3
104 #define PRIV_E3_MPCIFC 0xd0
105 #define PRIV_E3_STPCIFC 0xd4
107 #define DIAG_TIMEREVENT 0x288
108 #define DIAG_IPL 0x308
109 #define DIAG_SET_CONTROL_PROGRAM_CODES 0x318
110 #define DIAG_KVM_HYPERCALL 0x500
111 #define DIAG_KVM_BREAKPOINT 0x501
113 #define ICPT_INSTRUCTION 0x04
114 #define ICPT_PROGRAM 0x08
115 #define ICPT_EXT_INT 0x14
116 #define ICPT_WAITPSW 0x1c
117 #define ICPT_SOFT_INTERCEPT 0x24
118 #define ICPT_CPU_STOP 0x28
119 #define ICPT_OPEREXC 0x2c
120 #define ICPT_IO 0x40
121 #define ICPT_PV_INSTR 0x68
122 #define ICPT_PV_INSTR_NOTIFICATION 0x6c
124 #define NR_LOCAL_IRQS 32
126 * Needs to be big enough to contain max_cpus emergency signals
127 * and in addition NR_LOCAL_IRQS interrupts
129 #define VCPU_IRQ_BUF_SIZE(max_cpus) (sizeof(struct kvm_s390_irq) * \
130 (max_cpus + NR_LOCAL_IRQS))
132 * KVM does only support memory slots up to KVM_MEM_MAX_NR_PAGES pages
133 * as the dirty bitmap must be managed by bitops that take an int as
134 * position indicator. This would end at an unaligned address
135 * (0x7fffff00000). As future variants might provide larger pages
136 * and to make all addresses properly aligned, let us split at 4TB.
138 #define KVM_SLOT_MAX_BYTES (4UL * TiB)
140 static CPUWatchpoint hw_watchpoint;
142 * We don't use a list because this structure is also used to transmit the
143 * hardware breakpoints to the kernel.
145 static struct kvm_hw_breakpoint *hw_breakpoints;
146 static int nb_hw_breakpoints;
148 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
149 KVM_CAP_LAST_INFO
152 static int cap_sync_regs;
153 static int cap_async_pf;
154 static int cap_mem_op;
155 static int cap_s390_irq;
156 static int cap_ri;
157 static int cap_gs;
158 static int cap_hpage_1m;
159 static int cap_vcpu_resets;
160 static int cap_protected;
162 static int active_cmma;
164 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
166 struct kvm_device_attr attr = {
167 .group = KVM_S390_VM_MEM_CTRL,
168 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
169 .addr = (uint64_t) memory_limit,
172 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
175 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit)
177 int rc;
179 struct kvm_device_attr attr = {
180 .group = KVM_S390_VM_MEM_CTRL,
181 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
182 .addr = (uint64_t) &new_limit,
185 if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) {
186 return 0;
189 rc = kvm_s390_query_mem_limit(hw_limit);
190 if (rc) {
191 return rc;
192 } else if (*hw_limit < new_limit) {
193 return -E2BIG;
196 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
199 int kvm_s390_cmma_active(void)
201 return active_cmma;
204 static bool kvm_s390_cmma_available(void)
206 static bool initialized, value;
208 if (!initialized) {
209 initialized = true;
210 value = kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_ENABLE_CMMA) &&
211 kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_CLR_CMMA);
213 return value;
216 void kvm_s390_cmma_reset(void)
218 int rc;
219 struct kvm_device_attr attr = {
220 .group = KVM_S390_VM_MEM_CTRL,
221 .attr = KVM_S390_VM_MEM_CLR_CMMA,
224 if (!kvm_s390_cmma_active()) {
225 return;
228 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
229 trace_kvm_clear_cmma(rc);
232 static void kvm_s390_enable_cmma(void)
234 int rc;
235 struct kvm_device_attr attr = {
236 .group = KVM_S390_VM_MEM_CTRL,
237 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
240 if (cap_hpage_1m) {
241 warn_report("CMM will not be enabled because it is not "
242 "compatible with huge memory backings.");
243 return;
245 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
246 active_cmma = !rc;
247 trace_kvm_enable_cmma(rc);
250 static void kvm_s390_set_attr(uint64_t attr)
252 struct kvm_device_attr attribute = {
253 .group = KVM_S390_VM_CRYPTO,
254 .attr = attr,
257 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
259 if (ret) {
260 error_report("Failed to set crypto device attribute %lu: %s",
261 attr, strerror(-ret));
265 static void kvm_s390_init_aes_kw(void)
267 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
269 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
270 NULL)) {
271 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
274 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
275 kvm_s390_set_attr(attr);
279 static void kvm_s390_init_dea_kw(void)
281 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
283 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
284 NULL)) {
285 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
288 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
289 kvm_s390_set_attr(attr);
293 void kvm_s390_crypto_reset(void)
295 if (s390_has_feat(S390_FEAT_MSA_EXT_3)) {
296 kvm_s390_init_aes_kw();
297 kvm_s390_init_dea_kw();
301 void kvm_s390_set_max_pagesize(uint64_t pagesize, Error **errp)
303 if (pagesize == 4 * KiB) {
304 return;
307 if (!hpage_1m_allowed()) {
308 error_setg(errp, "This QEMU machine does not support huge page "
309 "mappings");
310 return;
313 if (pagesize != 1 * MiB) {
314 error_setg(errp, "Memory backing with 2G pages was specified, "
315 "but KVM does not support this memory backing");
316 return;
319 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_HPAGE_1M, 0)) {
320 error_setg(errp, "Memory backing with 1M pages was specified, "
321 "but KVM does not support this memory backing");
322 return;
325 cap_hpage_1m = 1;
328 int kvm_s390_get_hpage_1m(void)
330 return cap_hpage_1m;
333 static void ccw_machine_class_foreach(ObjectClass *oc, void *opaque)
335 MachineClass *mc = MACHINE_CLASS(oc);
337 mc->default_cpu_type = S390_CPU_TYPE_NAME("host");
340 int kvm_arch_init(MachineState *ms, KVMState *s)
342 object_class_foreach(ccw_machine_class_foreach, TYPE_S390_CCW_MACHINE,
343 false, NULL);
345 if (!kvm_check_extension(kvm_state, KVM_CAP_DEVICE_CTRL)) {
346 error_report("KVM is missing capability KVM_CAP_DEVICE_CTRL - "
347 "please use kernel 3.15 or newer");
348 return -1;
350 if (!kvm_check_extension(s, KVM_CAP_S390_COW)) {
351 error_report("KVM is missing capability KVM_CAP_S390_COW - "
352 "unsupported environment");
353 return -1;
356 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
357 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
358 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
359 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
360 cap_vcpu_resets = kvm_check_extension(s, KVM_CAP_S390_VCPU_RESETS);
361 cap_protected = kvm_check_extension(s, KVM_CAP_S390_PROTECTED);
363 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
364 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
365 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
366 if (ri_allowed()) {
367 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
368 cap_ri = 1;
371 if (cpu_model_allowed()) {
372 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
373 cap_gs = 1;
378 * The migration interface for ais was introduced with kernel 4.13
379 * but the capability itself had been active since 4.12. As migration
380 * support is considered necessary, we only try to enable this for
381 * newer machine types if KVM_CAP_S390_AIS_MIGRATION is available.
383 if (cpu_model_allowed() && kvm_kernel_irqchip_allowed() &&
384 kvm_check_extension(s, KVM_CAP_S390_AIS_MIGRATION)) {
385 kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0);
388 kvm_set_max_memslot_size(KVM_SLOT_MAX_BYTES);
389 return 0;
392 int kvm_arch_irqchip_create(KVMState *s)
394 return 0;
397 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
399 return cpu->cpu_index;
402 int kvm_arch_init_vcpu(CPUState *cs)
404 unsigned int max_cpus = MACHINE(qdev_get_machine())->smp.max_cpus;
405 S390CPU *cpu = S390_CPU(cs);
406 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
407 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE(max_cpus));
408 return 0;
411 int kvm_arch_destroy_vcpu(CPUState *cs)
413 S390CPU *cpu = S390_CPU(cs);
415 g_free(cpu->irqstate);
416 cpu->irqstate = NULL;
418 return 0;
421 static void kvm_s390_reset_vcpu(S390CPU *cpu, unsigned long type)
423 CPUState *cs = CPU(cpu);
426 * The reset call is needed here to reset in-kernel vcpu data that
427 * we can't access directly from QEMU (i.e. with older kernels
428 * which don't support sync_regs/ONE_REG). Before this ioctl
429 * cpu_synchronize_state() is called in common kvm code
430 * (kvm-all).
432 if (kvm_vcpu_ioctl(cs, type)) {
433 error_report("CPU reset failed on CPU %i type %lx",
434 cs->cpu_index, type);
438 void kvm_s390_reset_vcpu_initial(S390CPU *cpu)
440 kvm_s390_reset_vcpu(cpu, KVM_S390_INITIAL_RESET);
443 void kvm_s390_reset_vcpu_clear(S390CPU *cpu)
445 if (cap_vcpu_resets) {
446 kvm_s390_reset_vcpu(cpu, KVM_S390_CLEAR_RESET);
447 } else {
448 kvm_s390_reset_vcpu(cpu, KVM_S390_INITIAL_RESET);
452 void kvm_s390_reset_vcpu_normal(S390CPU *cpu)
454 if (cap_vcpu_resets) {
455 kvm_s390_reset_vcpu(cpu, KVM_S390_NORMAL_RESET);
459 static int can_sync_regs(CPUState *cs, int regs)
461 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
464 int kvm_arch_put_registers(CPUState *cs, int level)
466 S390CPU *cpu = S390_CPU(cs);
467 CPUS390XState *env = &cpu->env;
468 struct kvm_sregs sregs;
469 struct kvm_regs regs;
470 struct kvm_fpu fpu = {};
471 int r;
472 int i;
474 /* always save the PSW and the GPRS*/
475 cs->kvm_run->psw_addr = env->psw.addr;
476 cs->kvm_run->psw_mask = env->psw.mask;
478 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
479 for (i = 0; i < 16; i++) {
480 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
481 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
483 } else {
484 for (i = 0; i < 16; i++) {
485 regs.gprs[i] = env->regs[i];
487 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
488 if (r < 0) {
489 return r;
493 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
494 for (i = 0; i < 32; i++) {
495 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0];
496 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1];
498 cs->kvm_run->s.regs.fpc = env->fpc;
499 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
500 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
501 for (i = 0; i < 16; i++) {
502 cs->kvm_run->s.regs.fprs[i] = *get_freg(env, i);
504 cs->kvm_run->s.regs.fpc = env->fpc;
505 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
506 } else {
507 /* Floating point */
508 for (i = 0; i < 16; i++) {
509 fpu.fprs[i] = *get_freg(env, i);
511 fpu.fpc = env->fpc;
513 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
514 if (r < 0) {
515 return r;
519 /* Do we need to save more than that? */
520 if (level == KVM_PUT_RUNTIME_STATE) {
521 return 0;
524 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
525 cs->kvm_run->s.regs.cputm = env->cputm;
526 cs->kvm_run->s.regs.ckc = env->ckc;
527 cs->kvm_run->s.regs.todpr = env->todpr;
528 cs->kvm_run->s.regs.gbea = env->gbea;
529 cs->kvm_run->s.regs.pp = env->pp;
530 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
531 } else {
533 * These ONE_REGS are not protected by a capability. As they are only
534 * necessary for migration we just trace a possible error, but don't
535 * return with an error return code.
537 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
538 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
539 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
540 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
541 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
544 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
545 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
546 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
549 /* pfault parameters */
550 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
551 cs->kvm_run->s.regs.pft = env->pfault_token;
552 cs->kvm_run->s.regs.pfs = env->pfault_select;
553 cs->kvm_run->s.regs.pfc = env->pfault_compare;
554 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
555 } else if (cap_async_pf) {
556 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
557 if (r < 0) {
558 return r;
560 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
561 if (r < 0) {
562 return r;
564 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
565 if (r < 0) {
566 return r;
570 /* access registers and control registers*/
571 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
572 for (i = 0; i < 16; i++) {
573 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
574 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
576 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
577 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
578 } else {
579 for (i = 0; i < 16; i++) {
580 sregs.acrs[i] = env->aregs[i];
581 sregs.crs[i] = env->cregs[i];
583 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
584 if (r < 0) {
585 return r;
589 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
590 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
591 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
594 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
595 cs->kvm_run->s.regs.bpbc = env->bpbc;
596 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_BPBC;
599 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
600 cs->kvm_run->s.regs.etoken = env->etoken;
601 cs->kvm_run->s.regs.etoken_extension = env->etoken_extension;
602 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ETOKEN;
605 if (can_sync_regs(cs, KVM_SYNC_DIAG318)) {
606 cs->kvm_run->s.regs.diag318 = env->diag318_info;
607 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_DIAG318;
610 /* Finally the prefix */
611 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
612 cs->kvm_run->s.regs.prefix = env->psa;
613 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
614 } else {
615 /* prefix is only supported via sync regs */
617 return 0;
620 int kvm_arch_get_registers(CPUState *cs)
622 S390CPU *cpu = S390_CPU(cs);
623 CPUS390XState *env = &cpu->env;
624 struct kvm_sregs sregs;
625 struct kvm_regs regs;
626 struct kvm_fpu fpu;
627 int i, r;
629 /* get the PSW */
630 env->psw.addr = cs->kvm_run->psw_addr;
631 env->psw.mask = cs->kvm_run->psw_mask;
633 /* the GPRS */
634 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
635 for (i = 0; i < 16; i++) {
636 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
638 } else {
639 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
640 if (r < 0) {
641 return r;
643 for (i = 0; i < 16; i++) {
644 env->regs[i] = regs.gprs[i];
648 /* The ACRS and CRS */
649 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
650 for (i = 0; i < 16; i++) {
651 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
652 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
654 } else {
655 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
656 if (r < 0) {
657 return r;
659 for (i = 0; i < 16; i++) {
660 env->aregs[i] = sregs.acrs[i];
661 env->cregs[i] = sregs.crs[i];
665 /* Floating point and vector registers */
666 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
667 for (i = 0; i < 32; i++) {
668 env->vregs[i][0] = cs->kvm_run->s.regs.vrs[i][0];
669 env->vregs[i][1] = cs->kvm_run->s.regs.vrs[i][1];
671 env->fpc = cs->kvm_run->s.regs.fpc;
672 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
673 for (i = 0; i < 16; i++) {
674 *get_freg(env, i) = cs->kvm_run->s.regs.fprs[i];
676 env->fpc = cs->kvm_run->s.regs.fpc;
677 } else {
678 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
679 if (r < 0) {
680 return r;
682 for (i = 0; i < 16; i++) {
683 *get_freg(env, i) = fpu.fprs[i];
685 env->fpc = fpu.fpc;
688 /* The prefix */
689 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
690 env->psa = cs->kvm_run->s.regs.prefix;
693 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
694 env->cputm = cs->kvm_run->s.regs.cputm;
695 env->ckc = cs->kvm_run->s.regs.ckc;
696 env->todpr = cs->kvm_run->s.regs.todpr;
697 env->gbea = cs->kvm_run->s.regs.gbea;
698 env->pp = cs->kvm_run->s.regs.pp;
699 } else {
701 * These ONE_REGS are not protected by a capability. As they are only
702 * necessary for migration we just trace a possible error, but don't
703 * return with an error return code.
705 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
706 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
707 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
708 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
709 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
712 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
713 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
716 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
717 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
720 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
721 env->bpbc = cs->kvm_run->s.regs.bpbc;
724 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
725 env->etoken = cs->kvm_run->s.regs.etoken;
726 env->etoken_extension = cs->kvm_run->s.regs.etoken_extension;
729 /* pfault parameters */
730 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
731 env->pfault_token = cs->kvm_run->s.regs.pft;
732 env->pfault_select = cs->kvm_run->s.regs.pfs;
733 env->pfault_compare = cs->kvm_run->s.regs.pfc;
734 } else if (cap_async_pf) {
735 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
736 if (r < 0) {
737 return r;
739 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
740 if (r < 0) {
741 return r;
743 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
744 if (r < 0) {
745 return r;
749 if (can_sync_regs(cs, KVM_SYNC_DIAG318)) {
750 env->diag318_info = cs->kvm_run->s.regs.diag318;
753 return 0;
756 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
758 int r;
759 struct kvm_device_attr attr = {
760 .group = KVM_S390_VM_TOD,
761 .attr = KVM_S390_VM_TOD_LOW,
762 .addr = (uint64_t)tod_low,
765 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
766 if (r) {
767 return r;
770 attr.attr = KVM_S390_VM_TOD_HIGH;
771 attr.addr = (uint64_t)tod_high;
772 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
775 int kvm_s390_get_clock_ext(uint8_t *tod_high, uint64_t *tod_low)
777 int r;
778 struct kvm_s390_vm_tod_clock gtod;
779 struct kvm_device_attr attr = {
780 .group = KVM_S390_VM_TOD,
781 .attr = KVM_S390_VM_TOD_EXT,
782 .addr = (uint64_t)&gtod,
785 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
786 *tod_high = gtod.epoch_idx;
787 *tod_low = gtod.tod;
789 return r;
792 int kvm_s390_set_clock(uint8_t tod_high, uint64_t tod_low)
794 int r;
795 struct kvm_device_attr attr = {
796 .group = KVM_S390_VM_TOD,
797 .attr = KVM_S390_VM_TOD_LOW,
798 .addr = (uint64_t)&tod_low,
801 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
802 if (r) {
803 return r;
806 attr.attr = KVM_S390_VM_TOD_HIGH;
807 attr.addr = (uint64_t)&tod_high;
808 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
811 int kvm_s390_set_clock_ext(uint8_t tod_high, uint64_t tod_low)
813 struct kvm_s390_vm_tod_clock gtod = {
814 .epoch_idx = tod_high,
815 .tod = tod_low,
817 struct kvm_device_attr attr = {
818 .group = KVM_S390_VM_TOD,
819 .attr = KVM_S390_VM_TOD_EXT,
820 .addr = (uint64_t)&gtod,
823 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
827 * kvm_s390_mem_op:
828 * @addr: the logical start address in guest memory
829 * @ar: the access register number
830 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
831 * @len: length that should be transferred
832 * @is_write: true = write, false = read
833 * Returns: 0 on success, non-zero if an exception or error occurred
835 * Use KVM ioctl to read/write from/to guest memory. An access exception
836 * is injected into the vCPU in case of translation errors.
838 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
839 int len, bool is_write)
841 struct kvm_s390_mem_op mem_op = {
842 .gaddr = addr,
843 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
844 .size = len,
845 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
846 : KVM_S390_MEMOP_LOGICAL_READ,
847 .buf = (uint64_t)hostbuf,
848 .ar = ar,
850 int ret;
852 if (!cap_mem_op) {
853 return -ENOSYS;
855 if (!hostbuf) {
856 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
859 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
860 if (ret < 0) {
861 warn_report("KVM_S390_MEM_OP failed: %s", strerror(-ret));
863 return ret;
866 int kvm_s390_mem_op_pv(S390CPU *cpu, uint64_t offset, void *hostbuf,
867 int len, bool is_write)
869 struct kvm_s390_mem_op mem_op = {
870 .sida_offset = offset,
871 .size = len,
872 .op = is_write ? KVM_S390_MEMOP_SIDA_WRITE
873 : KVM_S390_MEMOP_SIDA_READ,
874 .buf = (uint64_t)hostbuf,
876 int ret;
878 if (!cap_mem_op || !cap_protected) {
879 return -ENOSYS;
882 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
883 if (ret < 0) {
884 error_report("KVM_S390_MEM_OP failed: %s", strerror(-ret));
885 abort();
887 return ret;
890 static uint8_t const *sw_bp_inst;
891 static uint8_t sw_bp_ilen;
893 static void determine_sw_breakpoint_instr(void)
895 /* DIAG 501 is used for sw breakpoints with old kernels */
896 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
897 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
898 static const uint8_t instr_0x0000[] = {0x00, 0x00};
900 if (sw_bp_inst) {
901 return;
903 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
904 sw_bp_inst = diag_501;
905 sw_bp_ilen = sizeof(diag_501);
906 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
907 } else {
908 sw_bp_inst = instr_0x0000;
909 sw_bp_ilen = sizeof(instr_0x0000);
910 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
914 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
916 determine_sw_breakpoint_instr();
918 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
919 sw_bp_ilen, 0) ||
920 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
921 return -EINVAL;
923 return 0;
926 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
928 uint8_t t[MAX_ILEN];
930 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
931 return -EINVAL;
932 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
933 return -EINVAL;
934 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
935 sw_bp_ilen, 1)) {
936 return -EINVAL;
939 return 0;
942 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
943 int len, int type)
945 int n;
947 for (n = 0; n < nb_hw_breakpoints; n++) {
948 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
949 (hw_breakpoints[n].len == len || len == -1)) {
950 return &hw_breakpoints[n];
954 return NULL;
957 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
959 int size;
961 if (find_hw_breakpoint(addr, len, type)) {
962 return -EEXIST;
965 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
967 if (!hw_breakpoints) {
968 nb_hw_breakpoints = 0;
969 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
970 } else {
971 hw_breakpoints =
972 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
975 if (!hw_breakpoints) {
976 nb_hw_breakpoints = 0;
977 return -ENOMEM;
980 hw_breakpoints[nb_hw_breakpoints].addr = addr;
981 hw_breakpoints[nb_hw_breakpoints].len = len;
982 hw_breakpoints[nb_hw_breakpoints].type = type;
984 nb_hw_breakpoints++;
986 return 0;
989 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
990 target_ulong len, int type)
992 switch (type) {
993 case GDB_BREAKPOINT_HW:
994 type = KVM_HW_BP;
995 break;
996 case GDB_WATCHPOINT_WRITE:
997 if (len < 1) {
998 return -EINVAL;
1000 type = KVM_HW_WP_WRITE;
1001 break;
1002 default:
1003 return -ENOSYS;
1005 return insert_hw_breakpoint(addr, len, type);
1008 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
1009 target_ulong len, int type)
1011 int size;
1012 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
1014 if (bp == NULL) {
1015 return -ENOENT;
1018 nb_hw_breakpoints--;
1019 if (nb_hw_breakpoints > 0) {
1021 * In order to trim the array, move the last element to the position to
1022 * be removed - if necessary.
1024 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
1025 *bp = hw_breakpoints[nb_hw_breakpoints];
1027 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
1028 hw_breakpoints =
1029 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
1030 } else {
1031 g_free(hw_breakpoints);
1032 hw_breakpoints = NULL;
1035 return 0;
1038 void kvm_arch_remove_all_hw_breakpoints(void)
1040 nb_hw_breakpoints = 0;
1041 g_free(hw_breakpoints);
1042 hw_breakpoints = NULL;
1045 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
1047 int i;
1049 if (nb_hw_breakpoints > 0) {
1050 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
1051 dbg->arch.hw_bp = hw_breakpoints;
1053 for (i = 0; i < nb_hw_breakpoints; ++i) {
1054 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
1055 hw_breakpoints[i].addr);
1057 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
1058 } else {
1059 dbg->arch.nr_hw_bp = 0;
1060 dbg->arch.hw_bp = NULL;
1064 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
1068 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1070 return MEMTXATTRS_UNSPECIFIED;
1073 int kvm_arch_process_async_events(CPUState *cs)
1075 return cs->halted;
1078 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
1079 struct kvm_s390_interrupt *interrupt)
1081 int r = 0;
1083 interrupt->type = irq->type;
1084 switch (irq->type) {
1085 case KVM_S390_INT_VIRTIO:
1086 interrupt->parm = irq->u.ext.ext_params;
1087 /* fall through */
1088 case KVM_S390_INT_PFAULT_INIT:
1089 case KVM_S390_INT_PFAULT_DONE:
1090 interrupt->parm64 = irq->u.ext.ext_params2;
1091 break;
1092 case KVM_S390_PROGRAM_INT:
1093 interrupt->parm = irq->u.pgm.code;
1094 break;
1095 case KVM_S390_SIGP_SET_PREFIX:
1096 interrupt->parm = irq->u.prefix.address;
1097 break;
1098 case KVM_S390_INT_SERVICE:
1099 interrupt->parm = irq->u.ext.ext_params;
1100 break;
1101 case KVM_S390_MCHK:
1102 interrupt->parm = irq->u.mchk.cr14;
1103 interrupt->parm64 = irq->u.mchk.mcic;
1104 break;
1105 case KVM_S390_INT_EXTERNAL_CALL:
1106 interrupt->parm = irq->u.extcall.code;
1107 break;
1108 case KVM_S390_INT_EMERGENCY:
1109 interrupt->parm = irq->u.emerg.code;
1110 break;
1111 case KVM_S390_SIGP_STOP:
1112 case KVM_S390_RESTART:
1113 break; /* These types have no parameters */
1114 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1115 interrupt->parm = irq->u.io.subchannel_id << 16;
1116 interrupt->parm |= irq->u.io.subchannel_nr;
1117 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
1118 interrupt->parm64 |= irq->u.io.io_int_word;
1119 break;
1120 default:
1121 r = -EINVAL;
1122 break;
1124 return r;
1127 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
1129 struct kvm_s390_interrupt kvmint = {};
1130 int r;
1132 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1133 if (r < 0) {
1134 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1135 exit(1);
1138 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
1139 if (r < 0) {
1140 fprintf(stderr, "KVM failed to inject interrupt\n");
1141 exit(1);
1145 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
1147 CPUState *cs = CPU(cpu);
1148 int r;
1150 if (cap_s390_irq) {
1151 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
1152 if (!r) {
1153 return;
1155 error_report("KVM failed to inject interrupt %llx", irq->type);
1156 exit(1);
1159 inject_vcpu_irq_legacy(cs, irq);
1162 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq *irq)
1164 struct kvm_s390_interrupt kvmint = {};
1165 int r;
1167 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1168 if (r < 0) {
1169 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1170 exit(1);
1173 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1174 if (r < 0) {
1175 fprintf(stderr, "KVM failed to inject interrupt\n");
1176 exit(1);
1180 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1182 struct kvm_s390_irq irq = {
1183 .type = KVM_S390_PROGRAM_INT,
1184 .u.pgm.code = code,
1186 qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
1187 cpu->env.psw.addr);
1188 kvm_s390_vcpu_interrupt(cpu, &irq);
1191 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1193 struct kvm_s390_irq irq = {
1194 .type = KVM_S390_PROGRAM_INT,
1195 .u.pgm.code = code,
1196 .u.pgm.trans_exc_code = te_code,
1197 .u.pgm.exc_access_id = te_code & 3,
1200 kvm_s390_vcpu_interrupt(cpu, &irq);
1203 static void kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1204 uint16_t ipbh0)
1206 CPUS390XState *env = &cpu->env;
1207 uint64_t sccb;
1208 uint32_t code;
1209 int r;
1211 sccb = env->regs[ipbh0 & 0xf];
1212 code = env->regs[(ipbh0 & 0xf0) >> 4];
1214 switch (run->s390_sieic.icptcode) {
1215 case ICPT_PV_INSTR_NOTIFICATION:
1216 g_assert(s390_is_pv());
1217 /* The notification intercepts are currently handled by KVM */
1218 error_report("unexpected SCLP PV notification");
1219 exit(1);
1220 break;
1221 case ICPT_PV_INSTR:
1222 g_assert(s390_is_pv());
1223 sclp_service_call_protected(env, sccb, code);
1224 /* Setting the CC is done by the Ultravisor. */
1225 break;
1226 case ICPT_INSTRUCTION:
1227 g_assert(!s390_is_pv());
1228 r = sclp_service_call(env, sccb, code);
1229 if (r < 0) {
1230 kvm_s390_program_interrupt(cpu, -r);
1231 return;
1233 setcc(cpu, r);
1237 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1239 CPUS390XState *env = &cpu->env;
1240 int rc = 0;
1241 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1243 switch (ipa1) {
1244 case PRIV_B2_XSCH:
1245 ioinst_handle_xsch(cpu, env->regs[1], RA_IGNORED);
1246 break;
1247 case PRIV_B2_CSCH:
1248 ioinst_handle_csch(cpu, env->regs[1], RA_IGNORED);
1249 break;
1250 case PRIV_B2_HSCH:
1251 ioinst_handle_hsch(cpu, env->regs[1], RA_IGNORED);
1252 break;
1253 case PRIV_B2_MSCH:
1254 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1255 break;
1256 case PRIV_B2_SSCH:
1257 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1258 break;
1259 case PRIV_B2_STCRW:
1260 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb, RA_IGNORED);
1261 break;
1262 case PRIV_B2_STSCH:
1263 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1264 break;
1265 case PRIV_B2_TSCH:
1266 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1267 fprintf(stderr, "Spurious tsch intercept\n");
1268 break;
1269 case PRIV_B2_CHSC:
1270 ioinst_handle_chsc(cpu, run->s390_sieic.ipb, RA_IGNORED);
1271 break;
1272 case PRIV_B2_TPI:
1273 /* This should have been handled by kvm already. */
1274 fprintf(stderr, "Spurious tpi intercept\n");
1275 break;
1276 case PRIV_B2_SCHM:
1277 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1278 run->s390_sieic.ipb, RA_IGNORED);
1279 break;
1280 case PRIV_B2_RSCH:
1281 ioinst_handle_rsch(cpu, env->regs[1], RA_IGNORED);
1282 break;
1283 case PRIV_B2_RCHP:
1284 ioinst_handle_rchp(cpu, env->regs[1], RA_IGNORED);
1285 break;
1286 case PRIV_B2_STCPS:
1287 /* We do not provide this instruction, it is suppressed. */
1288 break;
1289 case PRIV_B2_SAL:
1290 ioinst_handle_sal(cpu, env->regs[1], RA_IGNORED);
1291 break;
1292 case PRIV_B2_SIGA:
1293 /* Not provided, set CC = 3 for subchannel not operational */
1294 setcc(cpu, 3);
1295 break;
1296 case PRIV_B2_SCLP_CALL:
1297 kvm_sclp_service_call(cpu, run, ipbh0);
1298 break;
1299 default:
1300 rc = -1;
1301 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1302 break;
1305 return rc;
1308 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1309 uint8_t *ar)
1311 CPUS390XState *env = &cpu->env;
1312 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1313 uint32_t base2 = run->s390_sieic.ipb >> 28;
1314 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1315 ((run->s390_sieic.ipb & 0xff00) << 4);
1317 if (disp2 & 0x80000) {
1318 disp2 += 0xfff00000;
1320 if (ar) {
1321 *ar = base2;
1324 return (base2 ? env->regs[base2] : 0) +
1325 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1328 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1329 uint8_t *ar)
1331 CPUS390XState *env = &cpu->env;
1332 uint32_t base2 = run->s390_sieic.ipb >> 28;
1333 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1334 ((run->s390_sieic.ipb & 0xff00) << 4);
1336 if (disp2 & 0x80000) {
1337 disp2 += 0xfff00000;
1339 if (ar) {
1340 *ar = base2;
1343 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1346 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1348 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1350 if (s390_has_feat(S390_FEAT_ZPCI)) {
1351 return clp_service_call(cpu, r2, RA_IGNORED);
1352 } else {
1353 return -1;
1357 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1359 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1360 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1362 if (s390_has_feat(S390_FEAT_ZPCI)) {
1363 return pcilg_service_call(cpu, r1, r2, RA_IGNORED);
1364 } else {
1365 return -1;
1369 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1371 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1372 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1374 if (s390_has_feat(S390_FEAT_ZPCI)) {
1375 return pcistg_service_call(cpu, r1, r2, RA_IGNORED);
1376 } else {
1377 return -1;
1381 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1383 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1384 uint64_t fiba;
1385 uint8_t ar;
1387 if (s390_has_feat(S390_FEAT_ZPCI)) {
1388 fiba = get_base_disp_rxy(cpu, run, &ar);
1390 return stpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1391 } else {
1392 return -1;
1396 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1398 CPUS390XState *env = &cpu->env;
1399 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1400 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1401 uint8_t isc;
1402 uint16_t mode;
1403 int r;
1405 mode = env->regs[r1] & 0xffff;
1406 isc = (env->regs[r3] >> 27) & 0x7;
1407 r = css_do_sic(env, isc, mode);
1408 if (r) {
1409 kvm_s390_program_interrupt(cpu, -r);
1412 return 0;
1415 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1417 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1418 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1420 if (s390_has_feat(S390_FEAT_ZPCI)) {
1421 return rpcit_service_call(cpu, r1, r2, RA_IGNORED);
1422 } else {
1423 return -1;
1427 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1429 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1430 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1431 uint64_t gaddr;
1432 uint8_t ar;
1434 if (s390_has_feat(S390_FEAT_ZPCI)) {
1435 gaddr = get_base_disp_rsy(cpu, run, &ar);
1437 return pcistb_service_call(cpu, r1, r3, gaddr, ar, RA_IGNORED);
1438 } else {
1439 return -1;
1443 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1445 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1446 uint64_t fiba;
1447 uint8_t ar;
1449 if (s390_has_feat(S390_FEAT_ZPCI)) {
1450 fiba = get_base_disp_rxy(cpu, run, &ar);
1452 return mpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1453 } else {
1454 return -1;
1458 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1460 int r = 0;
1462 switch (ipa1) {
1463 case PRIV_B9_CLP:
1464 r = kvm_clp_service_call(cpu, run);
1465 break;
1466 case PRIV_B9_PCISTG:
1467 r = kvm_pcistg_service_call(cpu, run);
1468 break;
1469 case PRIV_B9_PCILG:
1470 r = kvm_pcilg_service_call(cpu, run);
1471 break;
1472 case PRIV_B9_RPCIT:
1473 r = kvm_rpcit_service_call(cpu, run);
1474 break;
1475 case PRIV_B9_EQBS:
1476 /* just inject exception */
1477 r = -1;
1478 break;
1479 default:
1480 r = -1;
1481 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1482 break;
1485 return r;
1488 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1490 int r = 0;
1492 switch (ipbl) {
1493 case PRIV_EB_PCISTB:
1494 r = kvm_pcistb_service_call(cpu, run);
1495 break;
1496 case PRIV_EB_SIC:
1497 r = kvm_sic_service_call(cpu, run);
1498 break;
1499 case PRIV_EB_SQBS:
1500 /* just inject exception */
1501 r = -1;
1502 break;
1503 default:
1504 r = -1;
1505 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1506 break;
1509 return r;
1512 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1514 int r = 0;
1516 switch (ipbl) {
1517 case PRIV_E3_MPCIFC:
1518 r = kvm_mpcifc_service_call(cpu, run);
1519 break;
1520 case PRIV_E3_STPCIFC:
1521 r = kvm_stpcifc_service_call(cpu, run);
1522 break;
1523 default:
1524 r = -1;
1525 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1526 break;
1529 return r;
1532 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1534 CPUS390XState *env = &cpu->env;
1535 int ret;
1537 ret = s390_virtio_hypercall(env);
1538 if (ret == -EINVAL) {
1539 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1540 return 0;
1543 return ret;
1546 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1548 uint64_t r1, r3;
1549 int rc;
1551 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1552 r3 = run->s390_sieic.ipa & 0x000f;
1553 rc = handle_diag_288(&cpu->env, r1, r3);
1554 if (rc) {
1555 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1559 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1561 uint64_t r1, r3;
1563 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1564 r3 = run->s390_sieic.ipa & 0x000f;
1565 handle_diag_308(&cpu->env, r1, r3, RA_IGNORED);
1568 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1570 CPUS390XState *env = &cpu->env;
1571 unsigned long pc;
1573 pc = env->psw.addr - sw_bp_ilen;
1574 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1575 env->psw.addr = pc;
1576 return EXCP_DEBUG;
1579 return -ENOENT;
1582 void kvm_s390_set_diag318(CPUState *cs, uint64_t diag318_info)
1584 CPUS390XState *env = &S390_CPU(cs)->env;
1586 /* Feat bit is set only if KVM supports sync for diag318 */
1587 if (s390_has_feat(S390_FEAT_DIAG_318)) {
1588 env->diag318_info = diag318_info;
1589 cs->kvm_run->s.regs.diag318 = diag318_info;
1590 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_DIAG318;
1594 static void handle_diag_318(S390CPU *cpu, struct kvm_run *run)
1596 uint64_t reg = (run->s390_sieic.ipa & 0x00f0) >> 4;
1597 uint64_t diag318_info = run->s.regs.gprs[reg];
1598 CPUState *t;
1601 * DIAG 318 can only be enabled with KVM support. As such, let's
1602 * ensure a guest cannot execute this instruction erroneously.
1604 if (!s390_has_feat(S390_FEAT_DIAG_318)) {
1605 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1606 return;
1609 CPU_FOREACH(t) {
1610 run_on_cpu(t, s390_do_cpu_set_diag318,
1611 RUN_ON_CPU_HOST_ULONG(diag318_info));
1615 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1617 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1619 int r = 0;
1620 uint16_t func_code;
1623 * For any diagnose call we support, bits 48-63 of the resulting
1624 * address specify the function code; the remainder is ignored.
1626 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1627 switch (func_code) {
1628 case DIAG_TIMEREVENT:
1629 kvm_handle_diag_288(cpu, run);
1630 break;
1631 case DIAG_IPL:
1632 kvm_handle_diag_308(cpu, run);
1633 break;
1634 case DIAG_SET_CONTROL_PROGRAM_CODES:
1635 handle_diag_318(cpu, run);
1636 break;
1637 case DIAG_KVM_HYPERCALL:
1638 r = handle_hypercall(cpu, run);
1639 break;
1640 case DIAG_KVM_BREAKPOINT:
1641 r = handle_sw_breakpoint(cpu, run);
1642 break;
1643 default:
1644 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1645 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1646 break;
1649 return r;
1652 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb)
1654 CPUS390XState *env = &cpu->env;
1655 const uint8_t r1 = ipa1 >> 4;
1656 const uint8_t r3 = ipa1 & 0x0f;
1657 int ret;
1658 uint8_t order;
1660 /* get order code */
1661 order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK;
1663 ret = handle_sigp(env, order, r1, r3);
1664 setcc(cpu, ret);
1665 return 0;
1668 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1670 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1671 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1672 int r = -1;
1674 DPRINTF("handle_instruction 0x%x 0x%x\n",
1675 run->s390_sieic.ipa, run->s390_sieic.ipb);
1676 switch (ipa0) {
1677 case IPA0_B2:
1678 r = handle_b2(cpu, run, ipa1);
1679 break;
1680 case IPA0_B9:
1681 r = handle_b9(cpu, run, ipa1);
1682 break;
1683 case IPA0_EB:
1684 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1685 break;
1686 case IPA0_E3:
1687 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1688 break;
1689 case IPA0_DIAG:
1690 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1691 break;
1692 case IPA0_SIGP:
1693 r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb);
1694 break;
1697 if (r < 0) {
1698 r = 0;
1699 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1702 return r;
1705 static void unmanageable_intercept(S390CPU *cpu, S390CrashReason reason,
1706 int pswoffset)
1708 CPUState *cs = CPU(cpu);
1710 s390_cpu_halt(cpu);
1711 cpu->env.crash_reason = reason;
1712 qemu_system_guest_panicked(cpu_get_crash_info(cs));
1715 /* try to detect pgm check loops */
1716 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1718 CPUState *cs = CPU(cpu);
1719 PSW oldpsw, newpsw;
1721 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1722 offsetof(LowCore, program_new_psw));
1723 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1724 offsetof(LowCore, program_new_psw) + 8);
1725 oldpsw.mask = run->psw_mask;
1726 oldpsw.addr = run->psw_addr;
1728 * Avoid endless loops of operation exceptions, if the pgm new
1729 * PSW will cause a new operation exception.
1730 * The heuristic checks if the pgm new psw is within 6 bytes before
1731 * the faulting psw address (with same DAT, AS settings) and the
1732 * new psw is not a wait psw and the fault was not triggered by
1733 * problem state. In that case go into crashed state.
1736 if (oldpsw.addr - newpsw.addr <= 6 &&
1737 !(newpsw.mask & PSW_MASK_WAIT) &&
1738 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1739 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1740 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1741 unmanageable_intercept(cpu, S390_CRASH_REASON_OPINT_LOOP,
1742 offsetof(LowCore, program_new_psw));
1743 return EXCP_HALTED;
1745 return 0;
1748 static int handle_intercept(S390CPU *cpu)
1750 CPUState *cs = CPU(cpu);
1751 struct kvm_run *run = cs->kvm_run;
1752 int icpt_code = run->s390_sieic.icptcode;
1753 int r = 0;
1755 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code, (long)run->psw_addr);
1756 switch (icpt_code) {
1757 case ICPT_INSTRUCTION:
1758 case ICPT_PV_INSTR:
1759 case ICPT_PV_INSTR_NOTIFICATION:
1760 r = handle_instruction(cpu, run);
1761 break;
1762 case ICPT_PROGRAM:
1763 unmanageable_intercept(cpu, S390_CRASH_REASON_PGMINT_LOOP,
1764 offsetof(LowCore, program_new_psw));
1765 r = EXCP_HALTED;
1766 break;
1767 case ICPT_EXT_INT:
1768 unmanageable_intercept(cpu, S390_CRASH_REASON_EXTINT_LOOP,
1769 offsetof(LowCore, external_new_psw));
1770 r = EXCP_HALTED;
1771 break;
1772 case ICPT_WAITPSW:
1773 /* disabled wait, since enabled wait is handled in kernel */
1774 s390_handle_wait(cpu);
1775 r = EXCP_HALTED;
1776 break;
1777 case ICPT_CPU_STOP:
1778 do_stop_interrupt(&cpu->env);
1779 r = EXCP_HALTED;
1780 break;
1781 case ICPT_OPEREXC:
1782 /* check for break points */
1783 r = handle_sw_breakpoint(cpu, run);
1784 if (r == -ENOENT) {
1785 /* Then check for potential pgm check loops */
1786 r = handle_oper_loop(cpu, run);
1787 if (r == 0) {
1788 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1791 break;
1792 case ICPT_SOFT_INTERCEPT:
1793 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1794 exit(1);
1795 break;
1796 case ICPT_IO:
1797 fprintf(stderr, "KVM unimplemented icpt IO\n");
1798 exit(1);
1799 break;
1800 default:
1801 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1802 exit(1);
1803 break;
1806 return r;
1809 static int handle_tsch(S390CPU *cpu)
1811 CPUState *cs = CPU(cpu);
1812 struct kvm_run *run = cs->kvm_run;
1813 int ret;
1815 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb,
1816 RA_IGNORED);
1817 if (ret < 0) {
1819 * Failure.
1820 * If an I/O interrupt had been dequeued, we have to reinject it.
1822 if (run->s390_tsch.dequeued) {
1823 s390_io_interrupt(run->s390_tsch.subchannel_id,
1824 run->s390_tsch.subchannel_nr,
1825 run->s390_tsch.io_int_parm,
1826 run->s390_tsch.io_int_word);
1828 ret = 0;
1830 return ret;
1833 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1835 const MachineState *ms = MACHINE(qdev_get_machine());
1836 uint16_t conf_cpus = 0, reserved_cpus = 0;
1837 SysIB_322 sysib;
1838 int del, i;
1840 if (s390_is_pv()) {
1841 s390_cpu_pv_mem_read(cpu, 0, &sysib, sizeof(sysib));
1842 } else if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1843 return;
1845 /* Shift the stack of Extended Names to prepare for our own data */
1846 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1847 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1848 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1849 * assumed it's not capable of managing Extended Names for lower levels.
1851 for (del = 1; del < sysib.count; del++) {
1852 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1853 break;
1856 if (del < sysib.count) {
1857 memset(sysib.ext_names[del], 0,
1858 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1861 /* count the cpus and split them into configured and reserved ones */
1862 for (i = 0; i < ms->possible_cpus->len; i++) {
1863 if (ms->possible_cpus->cpus[i].cpu) {
1864 conf_cpus++;
1865 } else {
1866 reserved_cpus++;
1869 sysib.vm[0].total_cpus = conf_cpus + reserved_cpus;
1870 sysib.vm[0].conf_cpus = conf_cpus;
1871 sysib.vm[0].reserved_cpus = reserved_cpus;
1873 /* Insert short machine name in EBCDIC, padded with blanks */
1874 if (qemu_name) {
1875 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1876 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1877 strlen(qemu_name)));
1879 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1880 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1881 * considered by s390 as not capable of providing any Extended Name.
1882 * Therefore if no name was specified on qemu invocation, we go with the
1883 * same "KVMguest" default, which KVM has filled into short name field.
1885 strpadcpy((char *)sysib.ext_names[0],
1886 sizeof(sysib.ext_names[0]),
1887 qemu_name ?: "KVMguest", '\0');
1889 /* Insert UUID */
1890 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
1892 if (s390_is_pv()) {
1893 s390_cpu_pv_mem_write(cpu, 0, &sysib, sizeof(sysib));
1894 } else {
1895 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1899 static int handle_stsi(S390CPU *cpu)
1901 CPUState *cs = CPU(cpu);
1902 struct kvm_run *run = cs->kvm_run;
1904 switch (run->s390_stsi.fc) {
1905 case 3:
1906 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1907 return 0;
1909 /* Only sysib 3.2.2 needs post-handling for now. */
1910 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1911 return 0;
1912 default:
1913 return 0;
1917 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1919 CPUState *cs = CPU(cpu);
1920 struct kvm_run *run = cs->kvm_run;
1922 int ret = 0;
1923 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1925 switch (arch_info->type) {
1926 case KVM_HW_WP_WRITE:
1927 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1928 cs->watchpoint_hit = &hw_watchpoint;
1929 hw_watchpoint.vaddr = arch_info->addr;
1930 hw_watchpoint.flags = BP_MEM_WRITE;
1931 ret = EXCP_DEBUG;
1933 break;
1934 case KVM_HW_BP:
1935 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1936 ret = EXCP_DEBUG;
1938 break;
1939 case KVM_SINGLESTEP:
1940 if (cs->singlestep_enabled) {
1941 ret = EXCP_DEBUG;
1943 break;
1944 default:
1945 ret = -ENOSYS;
1948 return ret;
1951 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1953 S390CPU *cpu = S390_CPU(cs);
1954 int ret = 0;
1956 qemu_mutex_lock_iothread();
1958 kvm_cpu_synchronize_state(cs);
1960 switch (run->exit_reason) {
1961 case KVM_EXIT_S390_SIEIC:
1962 ret = handle_intercept(cpu);
1963 break;
1964 case KVM_EXIT_S390_RESET:
1965 s390_ipl_reset_request(cs, S390_RESET_REIPL);
1966 break;
1967 case KVM_EXIT_S390_TSCH:
1968 ret = handle_tsch(cpu);
1969 break;
1970 case KVM_EXIT_S390_STSI:
1971 ret = handle_stsi(cpu);
1972 break;
1973 case KVM_EXIT_DEBUG:
1974 ret = kvm_arch_handle_debug_exit(cpu);
1975 break;
1976 default:
1977 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1978 break;
1980 qemu_mutex_unlock_iothread();
1982 if (ret == 0) {
1983 ret = EXCP_INTERRUPT;
1985 return ret;
1988 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1990 return true;
1993 void kvm_s390_enable_css_support(S390CPU *cpu)
1995 int r;
1997 /* Activate host kernel channel subsystem support. */
1998 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1999 assert(r == 0);
2002 void kvm_arch_init_irq_routing(KVMState *s)
2005 * Note that while irqchip capabilities generally imply that cpustates
2006 * are handled in-kernel, it is not true for s390 (yet); therefore, we
2007 * have to override the common code kvm_halt_in_kernel_allowed setting.
2009 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2010 kvm_gsi_routing_allowed = true;
2011 kvm_halt_in_kernel_allowed = false;
2015 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2016 int vq, bool assign)
2018 struct kvm_ioeventfd kick = {
2019 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2020 KVM_IOEVENTFD_FLAG_DATAMATCH,
2021 .fd = event_notifier_get_fd(notifier),
2022 .datamatch = vq,
2023 .addr = sch,
2024 .len = 8,
2026 trace_kvm_assign_subch_ioeventfd(kick.fd, kick.addr, assign,
2027 kick.datamatch);
2028 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2029 return -ENOSYS;
2031 if (!assign) {
2032 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2034 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2037 int kvm_s390_get_ri(void)
2039 return cap_ri;
2042 int kvm_s390_get_gs(void)
2044 return cap_gs;
2047 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2049 struct kvm_mp_state mp_state = {};
2050 int ret;
2052 /* the kvm part might not have been initialized yet */
2053 if (CPU(cpu)->kvm_state == NULL) {
2054 return 0;
2057 switch (cpu_state) {
2058 case S390_CPU_STATE_STOPPED:
2059 mp_state.mp_state = KVM_MP_STATE_STOPPED;
2060 break;
2061 case S390_CPU_STATE_CHECK_STOP:
2062 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2063 break;
2064 case S390_CPU_STATE_OPERATING:
2065 mp_state.mp_state = KVM_MP_STATE_OPERATING;
2066 break;
2067 case S390_CPU_STATE_LOAD:
2068 mp_state.mp_state = KVM_MP_STATE_LOAD;
2069 break;
2070 default:
2071 error_report("Requested CPU state is not a valid S390 CPU state: %u",
2072 cpu_state);
2073 exit(1);
2076 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2077 if (ret) {
2078 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2079 strerror(-ret));
2082 return ret;
2085 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2087 unsigned int max_cpus = MACHINE(qdev_get_machine())->smp.max_cpus;
2088 struct kvm_s390_irq_state irq_state = {
2089 .buf = (uint64_t) cpu->irqstate,
2090 .len = VCPU_IRQ_BUF_SIZE(max_cpus),
2092 CPUState *cs = CPU(cpu);
2093 int32_t bytes;
2095 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2096 return;
2099 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2100 if (bytes < 0) {
2101 cpu->irqstate_saved_size = 0;
2102 error_report("Migration of interrupt state failed");
2103 return;
2106 cpu->irqstate_saved_size = bytes;
2109 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2111 CPUState *cs = CPU(cpu);
2112 struct kvm_s390_irq_state irq_state = {
2113 .buf = (uint64_t) cpu->irqstate,
2114 .len = cpu->irqstate_saved_size,
2116 int r;
2118 if (cpu->irqstate_saved_size == 0) {
2119 return 0;
2122 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2123 return -ENOSYS;
2126 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2127 if (r) {
2128 error_report("Setting interrupt state failed %d", r);
2130 return r;
2133 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2134 uint64_t address, uint32_t data, PCIDevice *dev)
2136 S390PCIBusDevice *pbdev;
2137 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2139 if (!dev) {
2140 DPRINTF("add_msi_route no pci device\n");
2141 return -ENODEV;
2144 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
2145 if (!pbdev) {
2146 DPRINTF("add_msi_route no zpci device\n");
2147 return -ENODEV;
2150 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2151 route->flags = 0;
2152 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2153 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2154 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2155 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
2156 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2157 return 0;
2160 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2161 int vector, PCIDevice *dev)
2163 return 0;
2166 int kvm_arch_release_virq_post(int virq)
2168 return 0;
2171 int kvm_arch_msi_data_to_gsi(uint32_t data)
2173 abort();
2176 static int query_cpu_subfunc(S390FeatBitmap features)
2178 struct kvm_s390_vm_cpu_subfunc prop = {};
2179 struct kvm_device_attr attr = {
2180 .group = KVM_S390_VM_CPU_MODEL,
2181 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2182 .addr = (uint64_t) &prop,
2184 int rc;
2186 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2187 if (rc) {
2188 return rc;
2192 * We're going to add all subfunctions now, if the corresponding feature
2193 * is available that unlocks the query functions.
2195 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2196 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2197 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2199 if (test_bit(S390_FEAT_MSA, features)) {
2200 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2201 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2202 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2203 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2204 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2206 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2207 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2209 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2210 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2211 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2212 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2213 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2215 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2216 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2218 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2219 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2221 if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2222 s390_add_from_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2224 if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2225 s390_add_from_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2227 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2228 s390_add_from_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2230 return 0;
2233 static int configure_cpu_subfunc(const S390FeatBitmap features)
2235 struct kvm_s390_vm_cpu_subfunc prop = {};
2236 struct kvm_device_attr attr = {
2237 .group = KVM_S390_VM_CPU_MODEL,
2238 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2239 .addr = (uint64_t) &prop,
2242 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2243 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2244 /* hardware support might be missing, IBC will handle most of this */
2245 return 0;
2248 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2249 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2250 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2252 if (test_bit(S390_FEAT_MSA, features)) {
2253 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2254 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2255 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2256 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2257 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2259 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2260 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2262 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2263 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2264 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2265 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2266 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2268 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2269 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2271 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2272 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2274 if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2275 s390_fill_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2277 if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2278 s390_fill_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2280 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2281 s390_fill_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2283 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2286 static int kvm_to_feat[][2] = {
2287 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2288 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2289 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2290 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2291 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2292 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2293 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2294 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2295 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2296 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2297 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2298 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2299 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2300 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2303 static int query_cpu_feat(S390FeatBitmap features)
2305 struct kvm_s390_vm_cpu_feat prop = {};
2306 struct kvm_device_attr attr = {
2307 .group = KVM_S390_VM_CPU_MODEL,
2308 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2309 .addr = (uint64_t) &prop,
2311 int rc;
2312 int i;
2314 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2315 if (rc) {
2316 return rc;
2319 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2320 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2321 set_bit(kvm_to_feat[i][1], features);
2324 return 0;
2327 static int configure_cpu_feat(const S390FeatBitmap features)
2329 struct kvm_s390_vm_cpu_feat prop = {};
2330 struct kvm_device_attr attr = {
2331 .group = KVM_S390_VM_CPU_MODEL,
2332 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2333 .addr = (uint64_t) &prop,
2335 int i;
2337 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2338 if (test_bit(kvm_to_feat[i][1], features)) {
2339 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2342 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2345 bool kvm_s390_cpu_models_supported(void)
2347 if (!cpu_model_allowed()) {
2348 /* compatibility machines interfere with the cpu model */
2349 return false;
2351 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2352 KVM_S390_VM_CPU_MACHINE) &&
2353 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2354 KVM_S390_VM_CPU_PROCESSOR) &&
2355 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2356 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2357 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2358 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2359 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2360 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2363 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2365 struct kvm_s390_vm_cpu_machine prop = {};
2366 struct kvm_device_attr attr = {
2367 .group = KVM_S390_VM_CPU_MODEL,
2368 .attr = KVM_S390_VM_CPU_MACHINE,
2369 .addr = (uint64_t) &prop,
2371 uint16_t unblocked_ibc = 0, cpu_type = 0;
2372 int rc;
2374 memset(model, 0, sizeof(*model));
2376 if (!kvm_s390_cpu_models_supported()) {
2377 error_setg(errp, "KVM doesn't support CPU models");
2378 return;
2381 /* query the basic cpu model properties */
2382 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2383 if (rc) {
2384 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2385 return;
2388 cpu_type = cpuid_type(prop.cpuid);
2389 if (has_ibc(prop.ibc)) {
2390 model->lowest_ibc = lowest_ibc(prop.ibc);
2391 unblocked_ibc = unblocked_ibc(prop.ibc);
2393 model->cpu_id = cpuid_id(prop.cpuid);
2394 model->cpu_id_format = cpuid_format(prop.cpuid);
2395 model->cpu_ver = 0xff;
2397 /* get supported cpu features indicated via STFL(E) */
2398 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2399 (uint8_t *) prop.fac_mask);
2400 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2401 if (test_bit(S390_FEAT_STFLE, model->features)) {
2402 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2404 /* get supported cpu features indicated e.g. via SCLP */
2405 rc = query_cpu_feat(model->features);
2406 if (rc) {
2407 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2408 return;
2410 /* get supported cpu subfunctions indicated via query / test bit */
2411 rc = query_cpu_subfunc(model->features);
2412 if (rc) {
2413 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2414 return;
2417 /* PTFF subfunctions might be indicated although kernel support missing */
2418 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) {
2419 clear_bit(S390_FEAT_PTFF_QSIE, model->features);
2420 clear_bit(S390_FEAT_PTFF_QTOUE, model->features);
2421 clear_bit(S390_FEAT_PTFF_STOE, model->features);
2422 clear_bit(S390_FEAT_PTFF_STOUE, model->features);
2425 /* with cpu model support, CMM is only indicated if really available */
2426 if (kvm_s390_cmma_available()) {
2427 set_bit(S390_FEAT_CMM, model->features);
2428 } else {
2429 /* no cmm -> no cmm nt */
2430 clear_bit(S390_FEAT_CMM_NT, model->features);
2433 /* bpb needs kernel support for migration, VSIE and reset */
2434 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) {
2435 clear_bit(S390_FEAT_BPB, model->features);
2439 * If we have support for protected virtualization, indicate
2440 * the protected virtualization IPL unpack facility.
2442 if (cap_protected) {
2443 set_bit(S390_FEAT_UNPACK, model->features);
2446 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2447 set_bit(S390_FEAT_ZPCI, model->features);
2448 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2450 if (s390_known_cpu_type(cpu_type)) {
2451 /* we want the exact model, even if some features are missing */
2452 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2453 ibc_ec_ga(unblocked_ibc), NULL);
2454 } else {
2455 /* model unknown, e.g. too new - search using features */
2456 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2457 ibc_ec_ga(unblocked_ibc),
2458 model->features);
2460 if (!model->def) {
2461 error_setg(errp, "KVM: host CPU model could not be identified");
2462 return;
2464 /* for now, we can only provide the AP feature with HW support */
2465 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO,
2466 KVM_S390_VM_CRYPTO_ENABLE_APIE)) {
2467 set_bit(S390_FEAT_AP, model->features);
2471 * Extended-Length SCCB is handled entirely within QEMU.
2472 * For PV guests this is completely fenced by the Ultravisor, as Service
2473 * Call error checking and STFLE interpretation are handled via SIE.
2475 set_bit(S390_FEAT_EXTENDED_LENGTH_SCCB, model->features);
2477 if (kvm_check_extension(kvm_state, KVM_CAP_S390_DIAG318)) {
2478 set_bit(S390_FEAT_DIAG_318, model->features);
2481 /* strip of features that are not part of the maximum model */
2482 bitmap_and(model->features, model->features, model->def->full_feat,
2483 S390_FEAT_MAX);
2486 static void kvm_s390_configure_apie(bool interpret)
2488 uint64_t attr = interpret ? KVM_S390_VM_CRYPTO_ENABLE_APIE :
2489 KVM_S390_VM_CRYPTO_DISABLE_APIE;
2491 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
2492 kvm_s390_set_attr(attr);
2496 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2498 struct kvm_s390_vm_cpu_processor prop = {
2499 .fac_list = { 0 },
2501 struct kvm_device_attr attr = {
2502 .group = KVM_S390_VM_CPU_MODEL,
2503 .attr = KVM_S390_VM_CPU_PROCESSOR,
2504 .addr = (uint64_t) &prop,
2506 int rc;
2508 if (!model) {
2509 /* compatibility handling if cpu models are disabled */
2510 if (kvm_s390_cmma_available()) {
2511 kvm_s390_enable_cmma();
2513 return;
2515 if (!kvm_s390_cpu_models_supported()) {
2516 error_setg(errp, "KVM doesn't support CPU models");
2517 return;
2519 prop.cpuid = s390_cpuid_from_cpu_model(model);
2520 prop.ibc = s390_ibc_from_cpu_model(model);
2521 /* configure cpu features indicated via STFL(e) */
2522 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2523 (uint8_t *) prop.fac_list);
2524 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2525 if (rc) {
2526 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2527 return;
2529 /* configure cpu features indicated e.g. via SCLP */
2530 rc = configure_cpu_feat(model->features);
2531 if (rc) {
2532 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2533 return;
2535 /* configure cpu subfunctions indicated via query / test bit */
2536 rc = configure_cpu_subfunc(model->features);
2537 if (rc) {
2538 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2539 return;
2541 /* enable CMM via CMMA */
2542 if (test_bit(S390_FEAT_CMM, model->features)) {
2543 kvm_s390_enable_cmma();
2546 if (test_bit(S390_FEAT_AP, model->features)) {
2547 kvm_s390_configure_apie(true);
2551 void kvm_s390_restart_interrupt(S390CPU *cpu)
2553 struct kvm_s390_irq irq = {
2554 .type = KVM_S390_RESTART,
2557 kvm_s390_vcpu_interrupt(cpu, &irq);
2560 void kvm_s390_stop_interrupt(S390CPU *cpu)
2562 struct kvm_s390_irq irq = {
2563 .type = KVM_S390_SIGP_STOP,
2566 kvm_s390_vcpu_interrupt(cpu, &irq);
2569 bool kvm_arch_cpu_check_are_resettable(void)
2571 return true;