mem: add share parameter to memory-backend-ram
[qemu/kevin.git] / target / s390x / kvm.c
blobe13c8907dfa74614c8aec07332a7a2a5483143a2
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 "exec/address-spaces.h"
43 #include "trace.h"
44 #include "qapi-event.h"
45 #include "hw/s390x/s390-pci-inst.h"
46 #include "hw/s390x/s390-pci-bus.h"
47 #include "hw/s390x/ipl.h"
48 #include "hw/s390x/ebcdic.h"
49 #include "exec/memattrs.h"
50 #include "hw/s390x/s390-virtio-ccw.h"
51 #include "hw/s390x/s390-virtio-hcall.h"
53 #ifndef DEBUG_KVM
54 #define DEBUG_KVM 0
55 #endif
57 #define DPRINTF(fmt, ...) do { \
58 if (DEBUG_KVM) { \
59 fprintf(stderr, fmt, ## __VA_ARGS__); \
60 } \
61 } while (0)
63 #define kvm_vm_check_mem_attr(s, attr) \
64 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
66 #define IPA0_DIAG 0x8300
67 #define IPA0_SIGP 0xae00
68 #define IPA0_B2 0xb200
69 #define IPA0_B9 0xb900
70 #define IPA0_EB 0xeb00
71 #define IPA0_E3 0xe300
73 #define PRIV_B2_SCLP_CALL 0x20
74 #define PRIV_B2_CSCH 0x30
75 #define PRIV_B2_HSCH 0x31
76 #define PRIV_B2_MSCH 0x32
77 #define PRIV_B2_SSCH 0x33
78 #define PRIV_B2_STSCH 0x34
79 #define PRIV_B2_TSCH 0x35
80 #define PRIV_B2_TPI 0x36
81 #define PRIV_B2_SAL 0x37
82 #define PRIV_B2_RSCH 0x38
83 #define PRIV_B2_STCRW 0x39
84 #define PRIV_B2_STCPS 0x3a
85 #define PRIV_B2_RCHP 0x3b
86 #define PRIV_B2_SCHM 0x3c
87 #define PRIV_B2_CHSC 0x5f
88 #define PRIV_B2_SIGA 0x74
89 #define PRIV_B2_XSCH 0x76
91 #define PRIV_EB_SQBS 0x8a
92 #define PRIV_EB_PCISTB 0xd0
93 #define PRIV_EB_SIC 0xd1
95 #define PRIV_B9_EQBS 0x9c
96 #define PRIV_B9_CLP 0xa0
97 #define PRIV_B9_PCISTG 0xd0
98 #define PRIV_B9_PCILG 0xd2
99 #define PRIV_B9_RPCIT 0xd3
101 #define PRIV_E3_MPCIFC 0xd0
102 #define PRIV_E3_STPCIFC 0xd4
104 #define DIAG_TIMEREVENT 0x288
105 #define DIAG_IPL 0x308
106 #define DIAG_KVM_HYPERCALL 0x500
107 #define DIAG_KVM_BREAKPOINT 0x501
109 #define ICPT_INSTRUCTION 0x04
110 #define ICPT_PROGRAM 0x08
111 #define ICPT_EXT_INT 0x14
112 #define ICPT_WAITPSW 0x1c
113 #define ICPT_SOFT_INTERCEPT 0x24
114 #define ICPT_CPU_STOP 0x28
115 #define ICPT_OPEREXC 0x2c
116 #define ICPT_IO 0x40
118 #define NR_LOCAL_IRQS 32
120 * Needs to be big enough to contain max_cpus emergency signals
121 * and in addition NR_LOCAL_IRQS interrupts
123 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
124 (max_cpus + NR_LOCAL_IRQS))
126 static CPUWatchpoint hw_watchpoint;
128 * We don't use a list because this structure is also used to transmit the
129 * hardware breakpoints to the kernel.
131 static struct kvm_hw_breakpoint *hw_breakpoints;
132 static int nb_hw_breakpoints;
134 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
135 KVM_CAP_LAST_INFO
138 static int cap_sync_regs;
139 static int cap_async_pf;
140 static int cap_mem_op;
141 static int cap_s390_irq;
142 static int cap_ri;
143 static int cap_gs;
145 static int active_cmma;
147 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared);
149 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
151 struct kvm_device_attr attr = {
152 .group = KVM_S390_VM_MEM_CTRL,
153 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
154 .addr = (uint64_t) memory_limit,
157 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
160 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit)
162 int rc;
164 struct kvm_device_attr attr = {
165 .group = KVM_S390_VM_MEM_CTRL,
166 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
167 .addr = (uint64_t) &new_limit,
170 if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) {
171 return 0;
174 rc = kvm_s390_query_mem_limit(hw_limit);
175 if (rc) {
176 return rc;
177 } else if (*hw_limit < new_limit) {
178 return -E2BIG;
181 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
184 int kvm_s390_cmma_active(void)
186 return active_cmma;
189 static bool kvm_s390_cmma_available(void)
191 static bool initialized, value;
193 if (!initialized) {
194 initialized = true;
195 value = kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_ENABLE_CMMA) &&
196 kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_CLR_CMMA);
198 return value;
201 void kvm_s390_cmma_reset(void)
203 int rc;
204 struct kvm_device_attr attr = {
205 .group = KVM_S390_VM_MEM_CTRL,
206 .attr = KVM_S390_VM_MEM_CLR_CMMA,
209 if (!kvm_s390_cmma_active()) {
210 return;
213 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
214 trace_kvm_clear_cmma(rc);
217 static void kvm_s390_enable_cmma(void)
219 int rc;
220 struct kvm_device_attr attr = {
221 .group = KVM_S390_VM_MEM_CTRL,
222 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
225 if (mem_path) {
226 warn_report("CMM will not be enabled because it is not "
227 "compatible with hugetlbfs.");
228 return;
230 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
231 active_cmma = !rc;
232 trace_kvm_enable_cmma(rc);
235 static void kvm_s390_set_attr(uint64_t attr)
237 struct kvm_device_attr attribute = {
238 .group = KVM_S390_VM_CRYPTO,
239 .attr = attr,
242 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
244 if (ret) {
245 error_report("Failed to set crypto device attribute %lu: %s",
246 attr, strerror(-ret));
250 static void kvm_s390_init_aes_kw(void)
252 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
254 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
255 NULL)) {
256 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
259 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
260 kvm_s390_set_attr(attr);
264 static void kvm_s390_init_dea_kw(void)
266 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
268 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
269 NULL)) {
270 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
273 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
274 kvm_s390_set_attr(attr);
278 void kvm_s390_crypto_reset(void)
280 if (s390_has_feat(S390_FEAT_MSA_EXT_3)) {
281 kvm_s390_init_aes_kw();
282 kvm_s390_init_dea_kw();
286 int kvm_arch_init(MachineState *ms, KVMState *s)
288 MachineClass *mc = MACHINE_GET_CLASS(ms);
290 mc->default_cpu_type = S390_CPU_TYPE_NAME("host");
291 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
292 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
293 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
294 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
296 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
297 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
298 phys_mem_set_alloc(legacy_s390_alloc);
301 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
302 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
303 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
304 if (ri_allowed()) {
305 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
306 cap_ri = 1;
309 if (cpu_model_allowed()) {
310 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
311 cap_gs = 1;
316 * The migration interface for ais was introduced with kernel 4.13
317 * but the capability itself had been active since 4.12. As migration
318 * support is considered necessary let's disable ais in the 2.10
319 * machine.
321 /* kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0); */
323 return 0;
326 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
328 return 0;
331 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
333 return cpu->cpu_index;
336 int kvm_arch_init_vcpu(CPUState *cs)
338 S390CPU *cpu = S390_CPU(cs);
339 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
340 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
341 return 0;
344 void kvm_s390_reset_vcpu(S390CPU *cpu)
346 CPUState *cs = CPU(cpu);
348 /* The initial reset call is needed here to reset in-kernel
349 * vcpu data that we can't access directly from QEMU
350 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
351 * Before this ioctl cpu_synchronize_state() is called in common kvm
352 * code (kvm-all) */
353 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
354 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
358 static int can_sync_regs(CPUState *cs, int regs)
360 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
363 int kvm_arch_put_registers(CPUState *cs, int level)
365 S390CPU *cpu = S390_CPU(cs);
366 CPUS390XState *env = &cpu->env;
367 struct kvm_sregs sregs;
368 struct kvm_regs regs;
369 struct kvm_fpu fpu = {};
370 int r;
371 int i;
373 /* always save the PSW and the GPRS*/
374 cs->kvm_run->psw_addr = env->psw.addr;
375 cs->kvm_run->psw_mask = env->psw.mask;
377 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
378 for (i = 0; i < 16; i++) {
379 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
380 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
382 } else {
383 for (i = 0; i < 16; i++) {
384 regs.gprs[i] = env->regs[i];
386 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
387 if (r < 0) {
388 return r;
392 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
393 for (i = 0; i < 32; i++) {
394 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
395 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
397 cs->kvm_run->s.regs.fpc = env->fpc;
398 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
399 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
400 for (i = 0; i < 16; i++) {
401 cs->kvm_run->s.regs.fprs[i] = get_freg(env, i)->ll;
403 cs->kvm_run->s.regs.fpc = env->fpc;
404 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
405 } else {
406 /* Floating point */
407 for (i = 0; i < 16; i++) {
408 fpu.fprs[i] = get_freg(env, i)->ll;
410 fpu.fpc = env->fpc;
412 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
413 if (r < 0) {
414 return r;
418 /* Do we need to save more than that? */
419 if (level == KVM_PUT_RUNTIME_STATE) {
420 return 0;
423 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
424 cs->kvm_run->s.regs.cputm = env->cputm;
425 cs->kvm_run->s.regs.ckc = env->ckc;
426 cs->kvm_run->s.regs.todpr = env->todpr;
427 cs->kvm_run->s.regs.gbea = env->gbea;
428 cs->kvm_run->s.regs.pp = env->pp;
429 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
430 } else {
432 * These ONE_REGS are not protected by a capability. As they are only
433 * necessary for migration we just trace a possible error, but don't
434 * return with an error return code.
436 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
437 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
438 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
439 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
440 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
443 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
444 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
445 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
448 /* pfault parameters */
449 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
450 cs->kvm_run->s.regs.pft = env->pfault_token;
451 cs->kvm_run->s.regs.pfs = env->pfault_select;
452 cs->kvm_run->s.regs.pfc = env->pfault_compare;
453 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
454 } else if (cap_async_pf) {
455 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
456 if (r < 0) {
457 return r;
459 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
460 if (r < 0) {
461 return r;
463 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
464 if (r < 0) {
465 return r;
469 /* access registers and control registers*/
470 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
471 for (i = 0; i < 16; i++) {
472 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
473 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
475 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
476 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
477 } else {
478 for (i = 0; i < 16; i++) {
479 sregs.acrs[i] = env->aregs[i];
480 sregs.crs[i] = env->cregs[i];
482 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
483 if (r < 0) {
484 return r;
488 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
489 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
490 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
493 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
494 cs->kvm_run->s.regs.bpbc = env->bpbc;
495 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_BPBC;
498 /* Finally the prefix */
499 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
500 cs->kvm_run->s.regs.prefix = env->psa;
501 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
502 } else {
503 /* prefix is only supported via sync regs */
505 return 0;
508 int kvm_arch_get_registers(CPUState *cs)
510 S390CPU *cpu = S390_CPU(cs);
511 CPUS390XState *env = &cpu->env;
512 struct kvm_sregs sregs;
513 struct kvm_regs regs;
514 struct kvm_fpu fpu;
515 int i, r;
517 /* get the PSW */
518 env->psw.addr = cs->kvm_run->psw_addr;
519 env->psw.mask = cs->kvm_run->psw_mask;
521 /* the GPRS */
522 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
523 for (i = 0; i < 16; i++) {
524 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
526 } else {
527 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
528 if (r < 0) {
529 return r;
531 for (i = 0; i < 16; i++) {
532 env->regs[i] = regs.gprs[i];
536 /* The ACRS and CRS */
537 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
538 for (i = 0; i < 16; i++) {
539 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
540 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
542 } else {
543 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
544 if (r < 0) {
545 return r;
547 for (i = 0; i < 16; i++) {
548 env->aregs[i] = sregs.acrs[i];
549 env->cregs[i] = sregs.crs[i];
553 /* Floating point and vector registers */
554 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
555 for (i = 0; i < 32; i++) {
556 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
557 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
559 env->fpc = cs->kvm_run->s.regs.fpc;
560 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
561 for (i = 0; i < 16; i++) {
562 get_freg(env, i)->ll = cs->kvm_run->s.regs.fprs[i];
564 env->fpc = cs->kvm_run->s.regs.fpc;
565 } else {
566 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
567 if (r < 0) {
568 return r;
570 for (i = 0; i < 16; i++) {
571 get_freg(env, i)->ll = fpu.fprs[i];
573 env->fpc = fpu.fpc;
576 /* The prefix */
577 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
578 env->psa = cs->kvm_run->s.regs.prefix;
581 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
582 env->cputm = cs->kvm_run->s.regs.cputm;
583 env->ckc = cs->kvm_run->s.regs.ckc;
584 env->todpr = cs->kvm_run->s.regs.todpr;
585 env->gbea = cs->kvm_run->s.regs.gbea;
586 env->pp = cs->kvm_run->s.regs.pp;
587 } else {
589 * These ONE_REGS are not protected by a capability. As they are only
590 * necessary for migration we just trace a possible error, but don't
591 * return with an error return code.
593 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
594 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
595 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
596 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
597 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
600 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
601 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
604 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
605 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
608 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
609 env->bpbc = cs->kvm_run->s.regs.bpbc;
612 /* pfault parameters */
613 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
614 env->pfault_token = cs->kvm_run->s.regs.pft;
615 env->pfault_select = cs->kvm_run->s.regs.pfs;
616 env->pfault_compare = cs->kvm_run->s.regs.pfc;
617 } else if (cap_async_pf) {
618 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
619 if (r < 0) {
620 return r;
622 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
623 if (r < 0) {
624 return r;
626 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
627 if (r < 0) {
628 return r;
632 return 0;
635 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
637 int r;
638 struct kvm_device_attr attr = {
639 .group = KVM_S390_VM_TOD,
640 .attr = KVM_S390_VM_TOD_LOW,
641 .addr = (uint64_t)tod_low,
644 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
645 if (r) {
646 return r;
649 attr.attr = KVM_S390_VM_TOD_HIGH;
650 attr.addr = (uint64_t)tod_high;
651 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
654 int kvm_s390_get_clock_ext(uint8_t *tod_high, uint64_t *tod_low)
656 int r;
657 struct kvm_s390_vm_tod_clock gtod;
658 struct kvm_device_attr attr = {
659 .group = KVM_S390_VM_TOD,
660 .attr = KVM_S390_VM_TOD_EXT,
661 .addr = (uint64_t)&gtod,
664 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
665 *tod_high = gtod.epoch_idx;
666 *tod_low = gtod.tod;
668 return r;
671 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
673 int r;
674 struct kvm_device_attr attr = {
675 .group = KVM_S390_VM_TOD,
676 .attr = KVM_S390_VM_TOD_LOW,
677 .addr = (uint64_t)tod_low,
680 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
681 if (r) {
682 return r;
685 attr.attr = KVM_S390_VM_TOD_HIGH;
686 attr.addr = (uint64_t)tod_high;
687 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
690 int kvm_s390_set_clock_ext(uint8_t *tod_high, uint64_t *tod_low)
692 struct kvm_s390_vm_tod_clock gtod = {
693 .epoch_idx = *tod_high,
694 .tod = *tod_low,
696 struct kvm_device_attr attr = {
697 .group = KVM_S390_VM_TOD,
698 .attr = KVM_S390_VM_TOD_EXT,
699 .addr = (uint64_t)&gtod,
702 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
706 * kvm_s390_mem_op:
707 * @addr: the logical start address in guest memory
708 * @ar: the access register number
709 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
710 * @len: length that should be transferred
711 * @is_write: true = write, false = read
712 * Returns: 0 on success, non-zero if an exception or error occurred
714 * Use KVM ioctl to read/write from/to guest memory. An access exception
715 * is injected into the vCPU in case of translation errors.
717 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
718 int len, bool is_write)
720 struct kvm_s390_mem_op mem_op = {
721 .gaddr = addr,
722 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
723 .size = len,
724 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
725 : KVM_S390_MEMOP_LOGICAL_READ,
726 .buf = (uint64_t)hostbuf,
727 .ar = ar,
729 int ret;
731 if (!cap_mem_op) {
732 return -ENOSYS;
734 if (!hostbuf) {
735 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
738 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
739 if (ret < 0) {
740 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
742 return ret;
746 * Legacy layout for s390:
747 * Older S390 KVM requires the topmost vma of the RAM to be
748 * smaller than an system defined value, which is at least 256GB.
749 * Larger systems have larger values. We put the guest between
750 * the end of data segment (system break) and this value. We
751 * use 32GB as a base to have enough room for the system break
752 * to grow. We also have to use MAP parameters that avoid
753 * read-only mapping of guest pages.
755 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared)
757 void *mem;
759 mem = mmap((void *) 0x800000000ULL, size,
760 PROT_EXEC|PROT_READ|PROT_WRITE,
761 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
762 return mem == MAP_FAILED ? NULL : mem;
765 static uint8_t const *sw_bp_inst;
766 static uint8_t sw_bp_ilen;
768 static void determine_sw_breakpoint_instr(void)
770 /* DIAG 501 is used for sw breakpoints with old kernels */
771 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
772 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
773 static const uint8_t instr_0x0000[] = {0x00, 0x00};
775 if (sw_bp_inst) {
776 return;
778 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
779 sw_bp_inst = diag_501;
780 sw_bp_ilen = sizeof(diag_501);
781 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
782 } else {
783 sw_bp_inst = instr_0x0000;
784 sw_bp_ilen = sizeof(instr_0x0000);
785 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
789 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
791 determine_sw_breakpoint_instr();
793 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
794 sw_bp_ilen, 0) ||
795 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
796 return -EINVAL;
798 return 0;
801 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
803 uint8_t t[MAX_ILEN];
805 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
806 return -EINVAL;
807 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
808 return -EINVAL;
809 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
810 sw_bp_ilen, 1)) {
811 return -EINVAL;
814 return 0;
817 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
818 int len, int type)
820 int n;
822 for (n = 0; n < nb_hw_breakpoints; n++) {
823 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
824 (hw_breakpoints[n].len == len || len == -1)) {
825 return &hw_breakpoints[n];
829 return NULL;
832 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
834 int size;
836 if (find_hw_breakpoint(addr, len, type)) {
837 return -EEXIST;
840 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
842 if (!hw_breakpoints) {
843 nb_hw_breakpoints = 0;
844 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
845 } else {
846 hw_breakpoints =
847 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
850 if (!hw_breakpoints) {
851 nb_hw_breakpoints = 0;
852 return -ENOMEM;
855 hw_breakpoints[nb_hw_breakpoints].addr = addr;
856 hw_breakpoints[nb_hw_breakpoints].len = len;
857 hw_breakpoints[nb_hw_breakpoints].type = type;
859 nb_hw_breakpoints++;
861 return 0;
864 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
865 target_ulong len, int type)
867 switch (type) {
868 case GDB_BREAKPOINT_HW:
869 type = KVM_HW_BP;
870 break;
871 case GDB_WATCHPOINT_WRITE:
872 if (len < 1) {
873 return -EINVAL;
875 type = KVM_HW_WP_WRITE;
876 break;
877 default:
878 return -ENOSYS;
880 return insert_hw_breakpoint(addr, len, type);
883 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
884 target_ulong len, int type)
886 int size;
887 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
889 if (bp == NULL) {
890 return -ENOENT;
893 nb_hw_breakpoints--;
894 if (nb_hw_breakpoints > 0) {
896 * In order to trim the array, move the last element to the position to
897 * be removed - if necessary.
899 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
900 *bp = hw_breakpoints[nb_hw_breakpoints];
902 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
903 hw_breakpoints =
904 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
905 } else {
906 g_free(hw_breakpoints);
907 hw_breakpoints = NULL;
910 return 0;
913 void kvm_arch_remove_all_hw_breakpoints(void)
915 nb_hw_breakpoints = 0;
916 g_free(hw_breakpoints);
917 hw_breakpoints = NULL;
920 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
922 int i;
924 if (nb_hw_breakpoints > 0) {
925 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
926 dbg->arch.hw_bp = hw_breakpoints;
928 for (i = 0; i < nb_hw_breakpoints; ++i) {
929 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
930 hw_breakpoints[i].addr);
932 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
933 } else {
934 dbg->arch.nr_hw_bp = 0;
935 dbg->arch.hw_bp = NULL;
939 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
943 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
945 return MEMTXATTRS_UNSPECIFIED;
948 int kvm_arch_process_async_events(CPUState *cs)
950 return cs->halted;
953 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
954 struct kvm_s390_interrupt *interrupt)
956 int r = 0;
958 interrupt->type = irq->type;
959 switch (irq->type) {
960 case KVM_S390_INT_VIRTIO:
961 interrupt->parm = irq->u.ext.ext_params;
962 /* fall through */
963 case KVM_S390_INT_PFAULT_INIT:
964 case KVM_S390_INT_PFAULT_DONE:
965 interrupt->parm64 = irq->u.ext.ext_params2;
966 break;
967 case KVM_S390_PROGRAM_INT:
968 interrupt->parm = irq->u.pgm.code;
969 break;
970 case KVM_S390_SIGP_SET_PREFIX:
971 interrupt->parm = irq->u.prefix.address;
972 break;
973 case KVM_S390_INT_SERVICE:
974 interrupt->parm = irq->u.ext.ext_params;
975 break;
976 case KVM_S390_MCHK:
977 interrupt->parm = irq->u.mchk.cr14;
978 interrupt->parm64 = irq->u.mchk.mcic;
979 break;
980 case KVM_S390_INT_EXTERNAL_CALL:
981 interrupt->parm = irq->u.extcall.code;
982 break;
983 case KVM_S390_INT_EMERGENCY:
984 interrupt->parm = irq->u.emerg.code;
985 break;
986 case KVM_S390_SIGP_STOP:
987 case KVM_S390_RESTART:
988 break; /* These types have no parameters */
989 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
990 interrupt->parm = irq->u.io.subchannel_id << 16;
991 interrupt->parm |= irq->u.io.subchannel_nr;
992 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
993 interrupt->parm64 |= irq->u.io.io_int_word;
994 break;
995 default:
996 r = -EINVAL;
997 break;
999 return r;
1002 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
1004 struct kvm_s390_interrupt kvmint = {};
1005 int r;
1007 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1008 if (r < 0) {
1009 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1010 exit(1);
1013 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
1014 if (r < 0) {
1015 fprintf(stderr, "KVM failed to inject interrupt\n");
1016 exit(1);
1020 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
1022 CPUState *cs = CPU(cpu);
1023 int r;
1025 if (cap_s390_irq) {
1026 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
1027 if (!r) {
1028 return;
1030 error_report("KVM failed to inject interrupt %llx", irq->type);
1031 exit(1);
1034 inject_vcpu_irq_legacy(cs, irq);
1037 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq *irq)
1039 struct kvm_s390_interrupt kvmint = {};
1040 int r;
1042 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1043 if (r < 0) {
1044 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1045 exit(1);
1048 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1049 if (r < 0) {
1050 fprintf(stderr, "KVM failed to inject interrupt\n");
1051 exit(1);
1055 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1057 struct kvm_s390_irq irq = {
1058 .type = KVM_S390_PROGRAM_INT,
1059 .u.pgm.code = code,
1062 kvm_s390_vcpu_interrupt(cpu, &irq);
1065 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1067 struct kvm_s390_irq irq = {
1068 .type = KVM_S390_PROGRAM_INT,
1069 .u.pgm.code = code,
1070 .u.pgm.trans_exc_code = te_code,
1071 .u.pgm.exc_access_id = te_code & 3,
1074 kvm_s390_vcpu_interrupt(cpu, &irq);
1077 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1078 uint16_t ipbh0)
1080 CPUS390XState *env = &cpu->env;
1081 uint64_t sccb;
1082 uint32_t code;
1083 int r = 0;
1085 cpu_synchronize_state(CPU(cpu));
1086 sccb = env->regs[ipbh0 & 0xf];
1087 code = env->regs[(ipbh0 & 0xf0) >> 4];
1089 r = sclp_service_call(env, sccb, code);
1090 if (r < 0) {
1091 kvm_s390_program_interrupt(cpu, -r);
1092 } else {
1093 setcc(cpu, r);
1096 return 0;
1099 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1101 CPUS390XState *env = &cpu->env;
1102 int rc = 0;
1103 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1105 cpu_synchronize_state(CPU(cpu));
1107 switch (ipa1) {
1108 case PRIV_B2_XSCH:
1109 ioinst_handle_xsch(cpu, env->regs[1], RA_IGNORED);
1110 break;
1111 case PRIV_B2_CSCH:
1112 ioinst_handle_csch(cpu, env->regs[1], RA_IGNORED);
1113 break;
1114 case PRIV_B2_HSCH:
1115 ioinst_handle_hsch(cpu, env->regs[1], RA_IGNORED);
1116 break;
1117 case PRIV_B2_MSCH:
1118 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1119 break;
1120 case PRIV_B2_SSCH:
1121 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1122 break;
1123 case PRIV_B2_STCRW:
1124 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb, RA_IGNORED);
1125 break;
1126 case PRIV_B2_STSCH:
1127 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1128 break;
1129 case PRIV_B2_TSCH:
1130 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1131 fprintf(stderr, "Spurious tsch intercept\n");
1132 break;
1133 case PRIV_B2_CHSC:
1134 ioinst_handle_chsc(cpu, run->s390_sieic.ipb, RA_IGNORED);
1135 break;
1136 case PRIV_B2_TPI:
1137 /* This should have been handled by kvm already. */
1138 fprintf(stderr, "Spurious tpi intercept\n");
1139 break;
1140 case PRIV_B2_SCHM:
1141 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1142 run->s390_sieic.ipb, RA_IGNORED);
1143 break;
1144 case PRIV_B2_RSCH:
1145 ioinst_handle_rsch(cpu, env->regs[1], RA_IGNORED);
1146 break;
1147 case PRIV_B2_RCHP:
1148 ioinst_handle_rchp(cpu, env->regs[1], RA_IGNORED);
1149 break;
1150 case PRIV_B2_STCPS:
1151 /* We do not provide this instruction, it is suppressed. */
1152 break;
1153 case PRIV_B2_SAL:
1154 ioinst_handle_sal(cpu, env->regs[1], RA_IGNORED);
1155 break;
1156 case PRIV_B2_SIGA:
1157 /* Not provided, set CC = 3 for subchannel not operational */
1158 setcc(cpu, 3);
1159 break;
1160 case PRIV_B2_SCLP_CALL:
1161 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1162 break;
1163 default:
1164 rc = -1;
1165 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1166 break;
1169 return rc;
1172 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1173 uint8_t *ar)
1175 CPUS390XState *env = &cpu->env;
1176 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1177 uint32_t base2 = run->s390_sieic.ipb >> 28;
1178 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1179 ((run->s390_sieic.ipb & 0xff00) << 4);
1181 if (disp2 & 0x80000) {
1182 disp2 += 0xfff00000;
1184 if (ar) {
1185 *ar = base2;
1188 return (base2 ? env->regs[base2] : 0) +
1189 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1192 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1193 uint8_t *ar)
1195 CPUS390XState *env = &cpu->env;
1196 uint32_t base2 = run->s390_sieic.ipb >> 28;
1197 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1198 ((run->s390_sieic.ipb & 0xff00) << 4);
1200 if (disp2 & 0x80000) {
1201 disp2 += 0xfff00000;
1203 if (ar) {
1204 *ar = base2;
1207 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1210 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1212 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1214 if (s390_has_feat(S390_FEAT_ZPCI)) {
1215 return clp_service_call(cpu, r2, RA_IGNORED);
1216 } else {
1217 return -1;
1221 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1223 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1224 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1226 if (s390_has_feat(S390_FEAT_ZPCI)) {
1227 return pcilg_service_call(cpu, r1, r2, RA_IGNORED);
1228 } else {
1229 return -1;
1233 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1235 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1236 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1238 if (s390_has_feat(S390_FEAT_ZPCI)) {
1239 return pcistg_service_call(cpu, r1, r2, RA_IGNORED);
1240 } else {
1241 return -1;
1245 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1247 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1248 uint64_t fiba;
1249 uint8_t ar;
1251 if (s390_has_feat(S390_FEAT_ZPCI)) {
1252 cpu_synchronize_state(CPU(cpu));
1253 fiba = get_base_disp_rxy(cpu, run, &ar);
1255 return stpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1256 } else {
1257 return -1;
1261 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1263 CPUS390XState *env = &cpu->env;
1264 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1265 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1266 uint8_t isc;
1267 uint16_t mode;
1268 int r;
1270 cpu_synchronize_state(CPU(cpu));
1271 mode = env->regs[r1] & 0xffff;
1272 isc = (env->regs[r3] >> 27) & 0x7;
1273 r = css_do_sic(env, isc, mode);
1274 if (r) {
1275 kvm_s390_program_interrupt(cpu, -r);
1278 return 0;
1281 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1283 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1284 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1286 if (s390_has_feat(S390_FEAT_ZPCI)) {
1287 return rpcit_service_call(cpu, r1, r2, RA_IGNORED);
1288 } else {
1289 return -1;
1293 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1295 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1296 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1297 uint64_t gaddr;
1298 uint8_t ar;
1300 if (s390_has_feat(S390_FEAT_ZPCI)) {
1301 cpu_synchronize_state(CPU(cpu));
1302 gaddr = get_base_disp_rsy(cpu, run, &ar);
1304 return pcistb_service_call(cpu, r1, r3, gaddr, ar, RA_IGNORED);
1305 } else {
1306 return -1;
1310 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1312 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1313 uint64_t fiba;
1314 uint8_t ar;
1316 if (s390_has_feat(S390_FEAT_ZPCI)) {
1317 cpu_synchronize_state(CPU(cpu));
1318 fiba = get_base_disp_rxy(cpu, run, &ar);
1320 return mpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1321 } else {
1322 return -1;
1326 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1328 int r = 0;
1330 switch (ipa1) {
1331 case PRIV_B9_CLP:
1332 r = kvm_clp_service_call(cpu, run);
1333 break;
1334 case PRIV_B9_PCISTG:
1335 r = kvm_pcistg_service_call(cpu, run);
1336 break;
1337 case PRIV_B9_PCILG:
1338 r = kvm_pcilg_service_call(cpu, run);
1339 break;
1340 case PRIV_B9_RPCIT:
1341 r = kvm_rpcit_service_call(cpu, run);
1342 break;
1343 case PRIV_B9_EQBS:
1344 /* just inject exception */
1345 r = -1;
1346 break;
1347 default:
1348 r = -1;
1349 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1350 break;
1353 return r;
1356 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1358 int r = 0;
1360 switch (ipbl) {
1361 case PRIV_EB_PCISTB:
1362 r = kvm_pcistb_service_call(cpu, run);
1363 break;
1364 case PRIV_EB_SIC:
1365 r = kvm_sic_service_call(cpu, run);
1366 break;
1367 case PRIV_EB_SQBS:
1368 /* just inject exception */
1369 r = -1;
1370 break;
1371 default:
1372 r = -1;
1373 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1374 break;
1377 return r;
1380 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1382 int r = 0;
1384 switch (ipbl) {
1385 case PRIV_E3_MPCIFC:
1386 r = kvm_mpcifc_service_call(cpu, run);
1387 break;
1388 case PRIV_E3_STPCIFC:
1389 r = kvm_stpcifc_service_call(cpu, run);
1390 break;
1391 default:
1392 r = -1;
1393 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1394 break;
1397 return r;
1400 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1402 CPUS390XState *env = &cpu->env;
1403 int ret;
1405 cpu_synchronize_state(CPU(cpu));
1406 ret = s390_virtio_hypercall(env);
1407 if (ret == -EINVAL) {
1408 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1409 return 0;
1412 return ret;
1415 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1417 uint64_t r1, r3;
1418 int rc;
1420 cpu_synchronize_state(CPU(cpu));
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 cpu_synchronize_state(CPU(cpu));
1434 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1435 r3 = run->s390_sieic.ipa & 0x000f;
1436 handle_diag_308(&cpu->env, r1, r3, RA_IGNORED);
1439 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1441 CPUS390XState *env = &cpu->env;
1442 unsigned long pc;
1444 cpu_synchronize_state(CPU(cpu));
1446 pc = env->psw.addr - sw_bp_ilen;
1447 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1448 env->psw.addr = pc;
1449 return EXCP_DEBUG;
1452 return -ENOENT;
1455 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1457 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1459 int r = 0;
1460 uint16_t func_code;
1463 * For any diagnose call we support, bits 48-63 of the resulting
1464 * address specify the function code; the remainder is ignored.
1466 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1467 switch (func_code) {
1468 case DIAG_TIMEREVENT:
1469 kvm_handle_diag_288(cpu, run);
1470 break;
1471 case DIAG_IPL:
1472 kvm_handle_diag_308(cpu, run);
1473 break;
1474 case DIAG_KVM_HYPERCALL:
1475 r = handle_hypercall(cpu, run);
1476 break;
1477 case DIAG_KVM_BREAKPOINT:
1478 r = handle_sw_breakpoint(cpu, run);
1479 break;
1480 default:
1481 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1482 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1483 break;
1486 return r;
1489 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb)
1491 CPUS390XState *env = &cpu->env;
1492 const uint8_t r1 = ipa1 >> 4;
1493 const uint8_t r3 = ipa1 & 0x0f;
1494 int ret;
1495 uint8_t order;
1497 cpu_synchronize_state(CPU(cpu));
1499 /* get order code */
1500 order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK;
1502 ret = handle_sigp(env, order, r1, r3);
1503 setcc(cpu, ret);
1504 return 0;
1507 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1509 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1510 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1511 int r = -1;
1513 DPRINTF("handle_instruction 0x%x 0x%x\n",
1514 run->s390_sieic.ipa, run->s390_sieic.ipb);
1515 switch (ipa0) {
1516 case IPA0_B2:
1517 r = handle_b2(cpu, run, ipa1);
1518 break;
1519 case IPA0_B9:
1520 r = handle_b9(cpu, run, ipa1);
1521 break;
1522 case IPA0_EB:
1523 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1524 break;
1525 case IPA0_E3:
1526 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1527 break;
1528 case IPA0_DIAG:
1529 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1530 break;
1531 case IPA0_SIGP:
1532 r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb);
1533 break;
1536 if (r < 0) {
1537 r = 0;
1538 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1541 return r;
1544 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1546 CPUState *cs = CPU(cpu);
1548 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1549 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1550 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1551 s390_cpu_halt(cpu);
1552 qemu_system_guest_panicked(NULL);
1555 /* try to detect pgm check loops */
1556 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1558 CPUState *cs = CPU(cpu);
1559 PSW oldpsw, newpsw;
1561 cpu_synchronize_state(cs);
1562 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1563 offsetof(LowCore, program_new_psw));
1564 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1565 offsetof(LowCore, program_new_psw) + 8);
1566 oldpsw.mask = run->psw_mask;
1567 oldpsw.addr = run->psw_addr;
1569 * Avoid endless loops of operation exceptions, if the pgm new
1570 * PSW will cause a new operation exception.
1571 * The heuristic checks if the pgm new psw is within 6 bytes before
1572 * the faulting psw address (with same DAT, AS settings) and the
1573 * new psw is not a wait psw and the fault was not triggered by
1574 * problem state. In that case go into crashed state.
1577 if (oldpsw.addr - newpsw.addr <= 6 &&
1578 !(newpsw.mask & PSW_MASK_WAIT) &&
1579 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1580 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1581 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1582 unmanageable_intercept(cpu, "operation exception loop",
1583 offsetof(LowCore, program_new_psw));
1584 return EXCP_HALTED;
1586 return 0;
1589 static int handle_intercept(S390CPU *cpu)
1591 CPUState *cs = CPU(cpu);
1592 struct kvm_run *run = cs->kvm_run;
1593 int icpt_code = run->s390_sieic.icptcode;
1594 int r = 0;
1596 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1597 (long)cs->kvm_run->psw_addr);
1598 switch (icpt_code) {
1599 case ICPT_INSTRUCTION:
1600 r = handle_instruction(cpu, run);
1601 break;
1602 case ICPT_PROGRAM:
1603 unmanageable_intercept(cpu, "program interrupt",
1604 offsetof(LowCore, program_new_psw));
1605 r = EXCP_HALTED;
1606 break;
1607 case ICPT_EXT_INT:
1608 unmanageable_intercept(cpu, "external interrupt",
1609 offsetof(LowCore, external_new_psw));
1610 r = EXCP_HALTED;
1611 break;
1612 case ICPT_WAITPSW:
1613 /* disabled wait, since enabled wait is handled in kernel */
1614 cpu_synchronize_state(cs);
1615 s390_handle_wait(cpu);
1616 r = EXCP_HALTED;
1617 break;
1618 case ICPT_CPU_STOP:
1619 do_stop_interrupt(&cpu->env);
1620 r = EXCP_HALTED;
1621 break;
1622 case ICPT_OPEREXC:
1623 /* check for break points */
1624 r = handle_sw_breakpoint(cpu, run);
1625 if (r == -ENOENT) {
1626 /* Then check for potential pgm check loops */
1627 r = handle_oper_loop(cpu, run);
1628 if (r == 0) {
1629 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1632 break;
1633 case ICPT_SOFT_INTERCEPT:
1634 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1635 exit(1);
1636 break;
1637 case ICPT_IO:
1638 fprintf(stderr, "KVM unimplemented icpt IO\n");
1639 exit(1);
1640 break;
1641 default:
1642 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1643 exit(1);
1644 break;
1647 return r;
1650 static int handle_tsch(S390CPU *cpu)
1652 CPUState *cs = CPU(cpu);
1653 struct kvm_run *run = cs->kvm_run;
1654 int ret;
1656 cpu_synchronize_state(cs);
1658 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb,
1659 RA_IGNORED);
1660 if (ret < 0) {
1662 * Failure.
1663 * If an I/O interrupt had been dequeued, we have to reinject it.
1665 if (run->s390_tsch.dequeued) {
1666 s390_io_interrupt(run->s390_tsch.subchannel_id,
1667 run->s390_tsch.subchannel_nr,
1668 run->s390_tsch.io_int_parm,
1669 run->s390_tsch.io_int_word);
1671 ret = 0;
1673 return ret;
1676 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1678 SysIB_322 sysib;
1679 int del;
1681 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1682 return;
1684 /* Shift the stack of Extended Names to prepare for our own data */
1685 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1686 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1687 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1688 * assumed it's not capable of managing Extended Names for lower levels.
1690 for (del = 1; del < sysib.count; del++) {
1691 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1692 break;
1695 if (del < sysib.count) {
1696 memset(sysib.ext_names[del], 0,
1697 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1699 /* Insert short machine name in EBCDIC, padded with blanks */
1700 if (qemu_name) {
1701 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1702 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1703 strlen(qemu_name)));
1705 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1706 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1707 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1708 * considered by s390 as not capable of providing any Extended Name.
1709 * Therefore if no name was specified on qemu invocation, we go with the
1710 * same "KVMguest" default, which KVM has filled into short name field.
1712 if (qemu_name) {
1713 strncpy((char *)sysib.ext_names[0], qemu_name,
1714 sizeof(sysib.ext_names[0]));
1715 } else {
1716 strcpy((char *)sysib.ext_names[0], "KVMguest");
1718 /* Insert UUID */
1719 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
1721 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1724 static int handle_stsi(S390CPU *cpu)
1726 CPUState *cs = CPU(cpu);
1727 struct kvm_run *run = cs->kvm_run;
1729 switch (run->s390_stsi.fc) {
1730 case 3:
1731 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1732 return 0;
1734 /* Only sysib 3.2.2 needs post-handling for now. */
1735 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1736 return 0;
1737 default:
1738 return 0;
1742 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1744 CPUState *cs = CPU(cpu);
1745 struct kvm_run *run = cs->kvm_run;
1747 int ret = 0;
1748 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1750 switch (arch_info->type) {
1751 case KVM_HW_WP_WRITE:
1752 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1753 cs->watchpoint_hit = &hw_watchpoint;
1754 hw_watchpoint.vaddr = arch_info->addr;
1755 hw_watchpoint.flags = BP_MEM_WRITE;
1756 ret = EXCP_DEBUG;
1758 break;
1759 case KVM_HW_BP:
1760 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1761 ret = EXCP_DEBUG;
1763 break;
1764 case KVM_SINGLESTEP:
1765 if (cs->singlestep_enabled) {
1766 ret = EXCP_DEBUG;
1768 break;
1769 default:
1770 ret = -ENOSYS;
1773 return ret;
1776 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1778 S390CPU *cpu = S390_CPU(cs);
1779 int ret = 0;
1781 qemu_mutex_lock_iothread();
1783 switch (run->exit_reason) {
1784 case KVM_EXIT_S390_SIEIC:
1785 ret = handle_intercept(cpu);
1786 break;
1787 case KVM_EXIT_S390_RESET:
1788 s390_reipl_request();
1789 break;
1790 case KVM_EXIT_S390_TSCH:
1791 ret = handle_tsch(cpu);
1792 break;
1793 case KVM_EXIT_S390_STSI:
1794 ret = handle_stsi(cpu);
1795 break;
1796 case KVM_EXIT_DEBUG:
1797 ret = kvm_arch_handle_debug_exit(cpu);
1798 break;
1799 default:
1800 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1801 break;
1803 qemu_mutex_unlock_iothread();
1805 if (ret == 0) {
1806 ret = EXCP_INTERRUPT;
1808 return ret;
1811 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1813 return true;
1816 void kvm_s390_enable_css_support(S390CPU *cpu)
1818 int r;
1820 /* Activate host kernel channel subsystem support. */
1821 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1822 assert(r == 0);
1825 void kvm_arch_init_irq_routing(KVMState *s)
1828 * Note that while irqchip capabilities generally imply that cpustates
1829 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1830 * have to override the common code kvm_halt_in_kernel_allowed setting.
1832 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1833 kvm_gsi_routing_allowed = true;
1834 kvm_halt_in_kernel_allowed = false;
1838 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1839 int vq, bool assign)
1841 struct kvm_ioeventfd kick = {
1842 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1843 KVM_IOEVENTFD_FLAG_DATAMATCH,
1844 .fd = event_notifier_get_fd(notifier),
1845 .datamatch = vq,
1846 .addr = sch,
1847 .len = 8,
1849 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1850 return -ENOSYS;
1852 if (!assign) {
1853 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1855 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1858 int kvm_s390_get_memslot_count(void)
1860 return kvm_check_extension(kvm_state, KVM_CAP_NR_MEMSLOTS);
1863 int kvm_s390_get_ri(void)
1865 return cap_ri;
1868 int kvm_s390_get_gs(void)
1870 return cap_gs;
1873 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1875 struct kvm_mp_state mp_state = {};
1876 int ret;
1878 /* the kvm part might not have been initialized yet */
1879 if (CPU(cpu)->kvm_state == NULL) {
1880 return 0;
1883 switch (cpu_state) {
1884 case CPU_STATE_STOPPED:
1885 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1886 break;
1887 case CPU_STATE_CHECK_STOP:
1888 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1889 break;
1890 case CPU_STATE_OPERATING:
1891 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1892 break;
1893 case CPU_STATE_LOAD:
1894 mp_state.mp_state = KVM_MP_STATE_LOAD;
1895 break;
1896 default:
1897 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1898 cpu_state);
1899 exit(1);
1902 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1903 if (ret) {
1904 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1905 strerror(-ret));
1908 return ret;
1911 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
1913 struct kvm_s390_irq_state irq_state = {
1914 .buf = (uint64_t) cpu->irqstate,
1915 .len = VCPU_IRQ_BUF_SIZE,
1917 CPUState *cs = CPU(cpu);
1918 int32_t bytes;
1920 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1921 return;
1924 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
1925 if (bytes < 0) {
1926 cpu->irqstate_saved_size = 0;
1927 error_report("Migration of interrupt state failed");
1928 return;
1931 cpu->irqstate_saved_size = bytes;
1934 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
1936 CPUState *cs = CPU(cpu);
1937 struct kvm_s390_irq_state irq_state = {
1938 .buf = (uint64_t) cpu->irqstate,
1939 .len = cpu->irqstate_saved_size,
1941 int r;
1943 if (cpu->irqstate_saved_size == 0) {
1944 return 0;
1947 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1948 return -ENOSYS;
1951 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
1952 if (r) {
1953 error_report("Setting interrupt state failed %d", r);
1955 return r;
1958 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1959 uint64_t address, uint32_t data, PCIDevice *dev)
1961 S390PCIBusDevice *pbdev;
1962 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
1964 if (!dev) {
1965 DPRINTF("add_msi_route no pci device\n");
1966 return -ENODEV;
1969 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
1970 if (!pbdev) {
1971 DPRINTF("add_msi_route no zpci device\n");
1972 return -ENODEV;
1975 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
1976 route->flags = 0;
1977 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
1978 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
1979 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
1980 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
1981 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
1982 return 0;
1985 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
1986 int vector, PCIDevice *dev)
1988 return 0;
1991 int kvm_arch_release_virq_post(int virq)
1993 return 0;
1996 int kvm_arch_msi_data_to_gsi(uint32_t data)
1998 abort();
2001 static int query_cpu_subfunc(S390FeatBitmap features)
2003 struct kvm_s390_vm_cpu_subfunc prop;
2004 struct kvm_device_attr attr = {
2005 .group = KVM_S390_VM_CPU_MODEL,
2006 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2007 .addr = (uint64_t) &prop,
2009 int rc;
2011 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2012 if (rc) {
2013 return rc;
2017 * We're going to add all subfunctions now, if the corresponding feature
2018 * is available that unlocks the query functions.
2020 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2021 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2022 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2024 if (test_bit(S390_FEAT_MSA, features)) {
2025 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2026 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2027 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2028 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2029 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2031 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2032 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2034 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2035 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2036 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2037 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2038 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2040 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2041 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2043 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2044 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2046 return 0;
2049 static int configure_cpu_subfunc(const S390FeatBitmap features)
2051 struct kvm_s390_vm_cpu_subfunc prop = {};
2052 struct kvm_device_attr attr = {
2053 .group = KVM_S390_VM_CPU_MODEL,
2054 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2055 .addr = (uint64_t) &prop,
2058 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2059 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2060 /* hardware support might be missing, IBC will handle most of this */
2061 return 0;
2064 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2065 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2066 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2068 if (test_bit(S390_FEAT_MSA, features)) {
2069 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2070 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2071 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2072 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2073 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2075 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2076 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2078 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2079 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2080 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2081 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2082 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2084 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2085 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2087 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2088 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2090 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2093 static int kvm_to_feat[][2] = {
2094 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2095 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2096 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2097 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2098 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2099 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2100 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2101 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2102 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2103 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2104 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2105 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2106 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2107 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2110 static int query_cpu_feat(S390FeatBitmap features)
2112 struct kvm_s390_vm_cpu_feat prop;
2113 struct kvm_device_attr attr = {
2114 .group = KVM_S390_VM_CPU_MODEL,
2115 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2116 .addr = (uint64_t) &prop,
2118 int rc;
2119 int i;
2121 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2122 if (rc) {
2123 return rc;
2126 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2127 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2128 set_bit(kvm_to_feat[i][1], features);
2131 return 0;
2134 static int configure_cpu_feat(const S390FeatBitmap features)
2136 struct kvm_s390_vm_cpu_feat prop = {};
2137 struct kvm_device_attr attr = {
2138 .group = KVM_S390_VM_CPU_MODEL,
2139 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2140 .addr = (uint64_t) &prop,
2142 int i;
2144 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2145 if (test_bit(kvm_to_feat[i][1], features)) {
2146 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2149 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2152 bool kvm_s390_cpu_models_supported(void)
2154 if (!cpu_model_allowed()) {
2155 /* compatibility machines interfere with the cpu model */
2156 return false;
2158 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2159 KVM_S390_VM_CPU_MACHINE) &&
2160 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2161 KVM_S390_VM_CPU_PROCESSOR) &&
2162 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2163 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2164 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2165 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2166 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2167 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2170 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2172 struct kvm_s390_vm_cpu_machine prop = {};
2173 struct kvm_device_attr attr = {
2174 .group = KVM_S390_VM_CPU_MODEL,
2175 .attr = KVM_S390_VM_CPU_MACHINE,
2176 .addr = (uint64_t) &prop,
2178 uint16_t unblocked_ibc = 0, cpu_type = 0;
2179 int rc;
2181 memset(model, 0, sizeof(*model));
2183 if (!kvm_s390_cpu_models_supported()) {
2184 error_setg(errp, "KVM doesn't support CPU models");
2185 return;
2188 /* query the basic cpu model properties */
2189 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2190 if (rc) {
2191 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2192 return;
2195 cpu_type = cpuid_type(prop.cpuid);
2196 if (has_ibc(prop.ibc)) {
2197 model->lowest_ibc = lowest_ibc(prop.ibc);
2198 unblocked_ibc = unblocked_ibc(prop.ibc);
2200 model->cpu_id = cpuid_id(prop.cpuid);
2201 model->cpu_id_format = cpuid_format(prop.cpuid);
2202 model->cpu_ver = 0xff;
2204 /* get supported cpu features indicated via STFL(E) */
2205 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2206 (uint8_t *) prop.fac_mask);
2207 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2208 if (test_bit(S390_FEAT_STFLE, model->features)) {
2209 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2211 /* get supported cpu features indicated e.g. via SCLP */
2212 rc = query_cpu_feat(model->features);
2213 if (rc) {
2214 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2215 return;
2217 /* get supported cpu subfunctions indicated via query / test bit */
2218 rc = query_cpu_subfunc(model->features);
2219 if (rc) {
2220 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2221 return;
2224 /* PTFF subfunctions might be indicated although kernel support missing */
2225 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) {
2226 clear_bit(S390_FEAT_PTFF_QSIE, model->features);
2227 clear_bit(S390_FEAT_PTFF_QTOUE, model->features);
2228 clear_bit(S390_FEAT_PTFF_STOE, model->features);
2229 clear_bit(S390_FEAT_PTFF_STOUE, model->features);
2232 /* with cpu model support, CMM is only indicated if really available */
2233 if (kvm_s390_cmma_available()) {
2234 set_bit(S390_FEAT_CMM, model->features);
2235 } else {
2236 /* no cmm -> no cmm nt */
2237 clear_bit(S390_FEAT_CMM_NT, model->features);
2240 /* bpb needs kernel support for migration, VSIE and reset */
2241 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) {
2242 clear_bit(S390_FEAT_BPB, model->features);
2245 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2246 if (pci_available) {
2247 set_bit(S390_FEAT_ZPCI, model->features);
2249 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2251 if (s390_known_cpu_type(cpu_type)) {
2252 /* we want the exact model, even if some features are missing */
2253 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2254 ibc_ec_ga(unblocked_ibc), NULL);
2255 } else {
2256 /* model unknown, e.g. too new - search using features */
2257 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2258 ibc_ec_ga(unblocked_ibc),
2259 model->features);
2261 if (!model->def) {
2262 error_setg(errp, "KVM: host CPU model could not be identified");
2263 return;
2265 /* strip of features that are not part of the maximum model */
2266 bitmap_and(model->features, model->features, model->def->full_feat,
2267 S390_FEAT_MAX);
2270 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2272 struct kvm_s390_vm_cpu_processor prop = {
2273 .fac_list = { 0 },
2275 struct kvm_device_attr attr = {
2276 .group = KVM_S390_VM_CPU_MODEL,
2277 .attr = KVM_S390_VM_CPU_PROCESSOR,
2278 .addr = (uint64_t) &prop,
2280 int rc;
2282 if (!model) {
2283 /* compatibility handling if cpu models are disabled */
2284 if (kvm_s390_cmma_available()) {
2285 kvm_s390_enable_cmma();
2287 return;
2289 if (!kvm_s390_cpu_models_supported()) {
2290 error_setg(errp, "KVM doesn't support CPU models");
2291 return;
2293 prop.cpuid = s390_cpuid_from_cpu_model(model);
2294 prop.ibc = s390_ibc_from_cpu_model(model);
2295 /* configure cpu features indicated via STFL(e) */
2296 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2297 (uint8_t *) prop.fac_list);
2298 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2299 if (rc) {
2300 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2301 return;
2303 /* configure cpu features indicated e.g. via SCLP */
2304 rc = configure_cpu_feat(model->features);
2305 if (rc) {
2306 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2307 return;
2309 /* configure cpu subfunctions indicated via query / test bit */
2310 rc = configure_cpu_subfunc(model->features);
2311 if (rc) {
2312 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2313 return;
2315 /* enable CMM via CMMA */
2316 if (test_bit(S390_FEAT_CMM, model->features)) {
2317 kvm_s390_enable_cmma();
2321 void kvm_s390_restart_interrupt(S390CPU *cpu)
2323 struct kvm_s390_irq irq = {
2324 .type = KVM_S390_RESTART,
2327 kvm_s390_vcpu_interrupt(cpu, &irq);
2330 void kvm_s390_stop_interrupt(S390CPU *cpu)
2332 struct kvm_s390_irq irq = {
2333 .type = KVM_S390_SIGP_STOP,
2336 kvm_s390_vcpu_interrupt(cpu, &irq);