]> git.proxmox.com Git - mirror_qemu.git/blob - target/s390x/kvm.c
s390x: always provide pci support
[mirror_qemu.git] / target / s390x / kvm.c
1 /*
2 * QEMU S390x KVM implementation
3 *
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp. 2012
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21 #include "qemu/osdep.h"
22 #include <sys/ioctl.h>
23
24 #include <linux/kvm.h>
25 #include <asm/ptrace.h>
26
27 #include "qemu-common.h"
28 #include "cpu.h"
29 #include "internal.h"
30 #include "kvm_s390x.h"
31 #include "qapi/error.h"
32 #include "qemu/error-report.h"
33 #include "qemu/timer.h"
34 #include "qemu/units.h"
35 #include "qemu/mmap-alloc.h"
36 #include "qemu/log.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/ram_addr.h"
43 #include "trace.h"
44 #include "hw/s390x/s390-pci-inst.h"
45 #include "hw/s390x/s390-pci-bus.h"
46 #include "hw/s390x/ipl.h"
47 #include "hw/s390x/ebcdic.h"
48 #include "exec/memattrs.h"
49 #include "hw/s390x/s390-virtio-ccw.h"
50 #include "hw/s390x/s390-virtio-hcall.h"
51
52 #ifndef DEBUG_KVM
53 #define DEBUG_KVM 0
54 #endif
55
56 #define DPRINTF(fmt, ...) do { \
57 if (DEBUG_KVM) { \
58 fprintf(stderr, fmt, ## __VA_ARGS__); \
59 } \
60 } while (0)
61
62 #define kvm_vm_check_mem_attr(s, attr) \
63 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
64
65 #define IPA0_DIAG 0x8300
66 #define IPA0_SIGP 0xae00
67 #define IPA0_B2 0xb200
68 #define IPA0_B9 0xb900
69 #define IPA0_EB 0xeb00
70 #define IPA0_E3 0xe300
71
72 #define PRIV_B2_SCLP_CALL 0x20
73 #define PRIV_B2_CSCH 0x30
74 #define PRIV_B2_HSCH 0x31
75 #define PRIV_B2_MSCH 0x32
76 #define PRIV_B2_SSCH 0x33
77 #define PRIV_B2_STSCH 0x34
78 #define PRIV_B2_TSCH 0x35
79 #define PRIV_B2_TPI 0x36
80 #define PRIV_B2_SAL 0x37
81 #define PRIV_B2_RSCH 0x38
82 #define PRIV_B2_STCRW 0x39
83 #define PRIV_B2_STCPS 0x3a
84 #define PRIV_B2_RCHP 0x3b
85 #define PRIV_B2_SCHM 0x3c
86 #define PRIV_B2_CHSC 0x5f
87 #define PRIV_B2_SIGA 0x74
88 #define PRIV_B2_XSCH 0x76
89
90 #define PRIV_EB_SQBS 0x8a
91 #define PRIV_EB_PCISTB 0xd0
92 #define PRIV_EB_SIC 0xd1
93
94 #define PRIV_B9_EQBS 0x9c
95 #define PRIV_B9_CLP 0xa0
96 #define PRIV_B9_PCISTG 0xd0
97 #define PRIV_B9_PCILG 0xd2
98 #define PRIV_B9_RPCIT 0xd3
99
100 #define PRIV_E3_MPCIFC 0xd0
101 #define PRIV_E3_STPCIFC 0xd4
102
103 #define DIAG_TIMEREVENT 0x288
104 #define DIAG_IPL 0x308
105 #define DIAG_KVM_HYPERCALL 0x500
106 #define DIAG_KVM_BREAKPOINT 0x501
107
108 #define ICPT_INSTRUCTION 0x04
109 #define ICPT_PROGRAM 0x08
110 #define ICPT_EXT_INT 0x14
111 #define ICPT_WAITPSW 0x1c
112 #define ICPT_SOFT_INTERCEPT 0x24
113 #define ICPT_CPU_STOP 0x28
114 #define ICPT_OPEREXC 0x2c
115 #define ICPT_IO 0x40
116
117 #define NR_LOCAL_IRQS 32
118 /*
119 * Needs to be big enough to contain max_cpus emergency signals
120 * and in addition NR_LOCAL_IRQS interrupts
121 */
122 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
123 (max_cpus + NR_LOCAL_IRQS))
124
125 static CPUWatchpoint hw_watchpoint;
126 /*
127 * We don't use a list because this structure is also used to transmit the
128 * hardware breakpoints to the kernel.
129 */
130 static struct kvm_hw_breakpoint *hw_breakpoints;
131 static int nb_hw_breakpoints;
132
133 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
134 KVM_CAP_LAST_INFO
135 };
136
137 static int cap_sync_regs;
138 static int cap_async_pf;
139 static int cap_mem_op;
140 static int cap_s390_irq;
141 static int cap_ri;
142 static int cap_gs;
143 static int cap_hpage_1m;
144
145 static int active_cmma;
146
147 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared);
148
149 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
150 {
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,
155 };
156
157 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
158 }
159
160 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit)
161 {
162 int rc;
163
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,
168 };
169
170 if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) {
171 return 0;
172 }
173
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;
179 }
180
181 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
182 }
183
184 int kvm_s390_cmma_active(void)
185 {
186 return active_cmma;
187 }
188
189 static bool kvm_s390_cmma_available(void)
190 {
191 static bool initialized, value;
192
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);
197 }
198 return value;
199 }
200
201 void kvm_s390_cmma_reset(void)
202 {
203 int rc;
204 struct kvm_device_attr attr = {
205 .group = KVM_S390_VM_MEM_CTRL,
206 .attr = KVM_S390_VM_MEM_CLR_CMMA,
207 };
208
209 if (!kvm_s390_cmma_active()) {
210 return;
211 }
212
213 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
214 trace_kvm_clear_cmma(rc);
215 }
216
217 static void kvm_s390_enable_cmma(void)
218 {
219 int rc;
220 struct kvm_device_attr attr = {
221 .group = KVM_S390_VM_MEM_CTRL,
222 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
223 };
224
225 if (cap_hpage_1m) {
226 warn_report("CMM will not be enabled because it is not "
227 "compatible with huge memory backings.");
228 return;
229 }
230 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
231 active_cmma = !rc;
232 trace_kvm_enable_cmma(rc);
233 }
234
235 static void kvm_s390_set_attr(uint64_t attr)
236 {
237 struct kvm_device_attr attribute = {
238 .group = KVM_S390_VM_CRYPTO,
239 .attr = attr,
240 };
241
242 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
243
244 if (ret) {
245 error_report("Failed to set crypto device attribute %lu: %s",
246 attr, strerror(-ret));
247 }
248 }
249
250 static void kvm_s390_init_aes_kw(void)
251 {
252 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
253
254 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
255 NULL)) {
256 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
257 }
258
259 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
260 kvm_s390_set_attr(attr);
261 }
262 }
263
264 static void kvm_s390_init_dea_kw(void)
265 {
266 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
267
268 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
269 NULL)) {
270 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
271 }
272
273 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
274 kvm_s390_set_attr(attr);
275 }
276 }
277
278 void kvm_s390_crypto_reset(void)
279 {
280 if (s390_has_feat(S390_FEAT_MSA_EXT_3)) {
281 kvm_s390_init_aes_kw();
282 kvm_s390_init_dea_kw();
283 }
284 }
285
286 static int kvm_s390_configure_mempath_backing(KVMState *s)
287 {
288 size_t path_psize = qemu_getrampagesize();
289
290 if (path_psize == 4 * KiB) {
291 return 0;
292 }
293
294 if (!hpage_1m_allowed()) {
295 error_report("This QEMU machine does not support huge page "
296 "mappings");
297 return -EINVAL;
298 }
299
300 if (path_psize != 1 * MiB) {
301 error_report("Memory backing with 2G pages was specified, "
302 "but KVM does not support this memory backing");
303 return -EINVAL;
304 }
305
306 if (kvm_vm_enable_cap(s, KVM_CAP_S390_HPAGE_1M, 0)) {
307 error_report("Memory backing with 1M pages was specified, "
308 "but KVM does not support this memory backing");
309 return -EINVAL;
310 }
311
312 cap_hpage_1m = 1;
313 return 0;
314 }
315
316 int kvm_arch_init(MachineState *ms, KVMState *s)
317 {
318 MachineClass *mc = MACHINE_GET_CLASS(ms);
319
320 if (kvm_s390_configure_mempath_backing(s)) {
321 return -EINVAL;
322 }
323
324 mc->default_cpu_type = S390_CPU_TYPE_NAME("host");
325 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
326 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
327 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
328 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
329
330 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
331 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
332 phys_mem_set_alloc(legacy_s390_alloc);
333 }
334
335 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
336 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
337 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
338 if (ri_allowed()) {
339 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
340 cap_ri = 1;
341 }
342 }
343 if (cpu_model_allowed()) {
344 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
345 cap_gs = 1;
346 }
347 }
348
349 /*
350 * The migration interface for ais was introduced with kernel 4.13
351 * but the capability itself had been active since 4.12. As migration
352 * support is considered necessary let's disable ais in the 2.10
353 * machine.
354 */
355 /* kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0); */
356
357 return 0;
358 }
359
360 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
361 {
362 return 0;
363 }
364
365 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
366 {
367 return cpu->cpu_index;
368 }
369
370 int kvm_arch_init_vcpu(CPUState *cs)
371 {
372 S390CPU *cpu = S390_CPU(cs);
373 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
374 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
375 return 0;
376 }
377
378 void kvm_s390_reset_vcpu(S390CPU *cpu)
379 {
380 CPUState *cs = CPU(cpu);
381
382 /* The initial reset call is needed here to reset in-kernel
383 * vcpu data that we can't access directly from QEMU
384 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
385 * Before this ioctl cpu_synchronize_state() is called in common kvm
386 * code (kvm-all) */
387 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
388 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
389 }
390 }
391
392 static int can_sync_regs(CPUState *cs, int regs)
393 {
394 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
395 }
396
397 int kvm_arch_put_registers(CPUState *cs, int level)
398 {
399 S390CPU *cpu = S390_CPU(cs);
400 CPUS390XState *env = &cpu->env;
401 struct kvm_sregs sregs;
402 struct kvm_regs regs;
403 struct kvm_fpu fpu = {};
404 int r;
405 int i;
406
407 /* always save the PSW and the GPRS*/
408 cs->kvm_run->psw_addr = env->psw.addr;
409 cs->kvm_run->psw_mask = env->psw.mask;
410
411 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
412 for (i = 0; i < 16; i++) {
413 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
414 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
415 }
416 } else {
417 for (i = 0; i < 16; i++) {
418 regs.gprs[i] = env->regs[i];
419 }
420 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
421 if (r < 0) {
422 return r;
423 }
424 }
425
426 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
427 for (i = 0; i < 32; i++) {
428 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
429 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
430 }
431 cs->kvm_run->s.regs.fpc = env->fpc;
432 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
433 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
434 for (i = 0; i < 16; i++) {
435 cs->kvm_run->s.regs.fprs[i] = get_freg(env, i)->ll;
436 }
437 cs->kvm_run->s.regs.fpc = env->fpc;
438 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
439 } else {
440 /* Floating point */
441 for (i = 0; i < 16; i++) {
442 fpu.fprs[i] = get_freg(env, i)->ll;
443 }
444 fpu.fpc = env->fpc;
445
446 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
447 if (r < 0) {
448 return r;
449 }
450 }
451
452 /* Do we need to save more than that? */
453 if (level == KVM_PUT_RUNTIME_STATE) {
454 return 0;
455 }
456
457 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
458 cs->kvm_run->s.regs.cputm = env->cputm;
459 cs->kvm_run->s.regs.ckc = env->ckc;
460 cs->kvm_run->s.regs.todpr = env->todpr;
461 cs->kvm_run->s.regs.gbea = env->gbea;
462 cs->kvm_run->s.regs.pp = env->pp;
463 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
464 } else {
465 /*
466 * These ONE_REGS are not protected by a capability. As they are only
467 * necessary for migration we just trace a possible error, but don't
468 * return with an error return code.
469 */
470 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
471 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
472 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
473 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
474 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
475 }
476
477 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
478 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
479 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
480 }
481
482 /* pfault parameters */
483 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
484 cs->kvm_run->s.regs.pft = env->pfault_token;
485 cs->kvm_run->s.regs.pfs = env->pfault_select;
486 cs->kvm_run->s.regs.pfc = env->pfault_compare;
487 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
488 } else if (cap_async_pf) {
489 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
490 if (r < 0) {
491 return r;
492 }
493 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
494 if (r < 0) {
495 return r;
496 }
497 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
498 if (r < 0) {
499 return r;
500 }
501 }
502
503 /* access registers and control registers*/
504 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
505 for (i = 0; i < 16; i++) {
506 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
507 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
508 }
509 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
510 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
511 } else {
512 for (i = 0; i < 16; i++) {
513 sregs.acrs[i] = env->aregs[i];
514 sregs.crs[i] = env->cregs[i];
515 }
516 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
517 if (r < 0) {
518 return r;
519 }
520 }
521
522 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
523 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
524 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
525 }
526
527 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
528 cs->kvm_run->s.regs.bpbc = env->bpbc;
529 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_BPBC;
530 }
531
532 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
533 cs->kvm_run->s.regs.etoken = env->etoken;
534 cs->kvm_run->s.regs.etoken_extension = env->etoken_extension;
535 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ETOKEN;
536 }
537
538 /* Finally the prefix */
539 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
540 cs->kvm_run->s.regs.prefix = env->psa;
541 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
542 } else {
543 /* prefix is only supported via sync regs */
544 }
545 return 0;
546 }
547
548 int kvm_arch_get_registers(CPUState *cs)
549 {
550 S390CPU *cpu = S390_CPU(cs);
551 CPUS390XState *env = &cpu->env;
552 struct kvm_sregs sregs;
553 struct kvm_regs regs;
554 struct kvm_fpu fpu;
555 int i, r;
556
557 /* get the PSW */
558 env->psw.addr = cs->kvm_run->psw_addr;
559 env->psw.mask = cs->kvm_run->psw_mask;
560
561 /* the GPRS */
562 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
563 for (i = 0; i < 16; i++) {
564 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
565 }
566 } else {
567 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
568 if (r < 0) {
569 return r;
570 }
571 for (i = 0; i < 16; i++) {
572 env->regs[i] = regs.gprs[i];
573 }
574 }
575
576 /* The ACRS and CRS */
577 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
578 for (i = 0; i < 16; i++) {
579 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
580 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
581 }
582 } else {
583 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
584 if (r < 0) {
585 return r;
586 }
587 for (i = 0; i < 16; i++) {
588 env->aregs[i] = sregs.acrs[i];
589 env->cregs[i] = sregs.crs[i];
590 }
591 }
592
593 /* Floating point and vector registers */
594 if (can_sync_regs(cs, KVM_SYNC_VRS)) {
595 for (i = 0; i < 32; i++) {
596 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
597 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
598 }
599 env->fpc = cs->kvm_run->s.regs.fpc;
600 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
601 for (i = 0; i < 16; i++) {
602 get_freg(env, i)->ll = cs->kvm_run->s.regs.fprs[i];
603 }
604 env->fpc = cs->kvm_run->s.regs.fpc;
605 } else {
606 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
607 if (r < 0) {
608 return r;
609 }
610 for (i = 0; i < 16; i++) {
611 get_freg(env, i)->ll = fpu.fprs[i];
612 }
613 env->fpc = fpu.fpc;
614 }
615
616 /* The prefix */
617 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
618 env->psa = cs->kvm_run->s.regs.prefix;
619 }
620
621 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
622 env->cputm = cs->kvm_run->s.regs.cputm;
623 env->ckc = cs->kvm_run->s.regs.ckc;
624 env->todpr = cs->kvm_run->s.regs.todpr;
625 env->gbea = cs->kvm_run->s.regs.gbea;
626 env->pp = cs->kvm_run->s.regs.pp;
627 } else {
628 /*
629 * These ONE_REGS are not protected by a capability. As they are only
630 * necessary for migration we just trace a possible error, but don't
631 * return with an error return code.
632 */
633 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
634 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
635 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
636 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
637 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
638 }
639
640 if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
641 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
642 }
643
644 if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
645 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
646 }
647
648 if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
649 env->bpbc = cs->kvm_run->s.regs.bpbc;
650 }
651
652 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
653 env->etoken = cs->kvm_run->s.regs.etoken;
654 env->etoken_extension = cs->kvm_run->s.regs.etoken_extension;
655 }
656
657 /* pfault parameters */
658 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
659 env->pfault_token = cs->kvm_run->s.regs.pft;
660 env->pfault_select = cs->kvm_run->s.regs.pfs;
661 env->pfault_compare = cs->kvm_run->s.regs.pfc;
662 } else if (cap_async_pf) {
663 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
664 if (r < 0) {
665 return r;
666 }
667 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
668 if (r < 0) {
669 return r;
670 }
671 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
672 if (r < 0) {
673 return r;
674 }
675 }
676
677 return 0;
678 }
679
680 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
681 {
682 int r;
683 struct kvm_device_attr attr = {
684 .group = KVM_S390_VM_TOD,
685 .attr = KVM_S390_VM_TOD_LOW,
686 .addr = (uint64_t)tod_low,
687 };
688
689 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
690 if (r) {
691 return r;
692 }
693
694 attr.attr = KVM_S390_VM_TOD_HIGH;
695 attr.addr = (uint64_t)tod_high;
696 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
697 }
698
699 int kvm_s390_get_clock_ext(uint8_t *tod_high, uint64_t *tod_low)
700 {
701 int r;
702 struct kvm_s390_vm_tod_clock gtod;
703 struct kvm_device_attr attr = {
704 .group = KVM_S390_VM_TOD,
705 .attr = KVM_S390_VM_TOD_EXT,
706 .addr = (uint64_t)&gtod,
707 };
708
709 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
710 *tod_high = gtod.epoch_idx;
711 *tod_low = gtod.tod;
712
713 return r;
714 }
715
716 int kvm_s390_set_clock(uint8_t tod_high, uint64_t tod_low)
717 {
718 int r;
719 struct kvm_device_attr attr = {
720 .group = KVM_S390_VM_TOD,
721 .attr = KVM_S390_VM_TOD_LOW,
722 .addr = (uint64_t)&tod_low,
723 };
724
725 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
726 if (r) {
727 return r;
728 }
729
730 attr.attr = KVM_S390_VM_TOD_HIGH;
731 attr.addr = (uint64_t)&tod_high;
732 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
733 }
734
735 int kvm_s390_set_clock_ext(uint8_t tod_high, uint64_t tod_low)
736 {
737 struct kvm_s390_vm_tod_clock gtod = {
738 .epoch_idx = tod_high,
739 .tod = tod_low,
740 };
741 struct kvm_device_attr attr = {
742 .group = KVM_S390_VM_TOD,
743 .attr = KVM_S390_VM_TOD_EXT,
744 .addr = (uint64_t)&gtod,
745 };
746
747 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
748 }
749
750 /**
751 * kvm_s390_mem_op:
752 * @addr: the logical start address in guest memory
753 * @ar: the access register number
754 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
755 * @len: length that should be transferred
756 * @is_write: true = write, false = read
757 * Returns: 0 on success, non-zero if an exception or error occurred
758 *
759 * Use KVM ioctl to read/write from/to guest memory. An access exception
760 * is injected into the vCPU in case of translation errors.
761 */
762 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
763 int len, bool is_write)
764 {
765 struct kvm_s390_mem_op mem_op = {
766 .gaddr = addr,
767 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
768 .size = len,
769 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
770 : KVM_S390_MEMOP_LOGICAL_READ,
771 .buf = (uint64_t)hostbuf,
772 .ar = ar,
773 };
774 int ret;
775
776 if (!cap_mem_op) {
777 return -ENOSYS;
778 }
779 if (!hostbuf) {
780 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
781 }
782
783 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
784 if (ret < 0) {
785 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
786 }
787 return ret;
788 }
789
790 /*
791 * Legacy layout for s390:
792 * Older S390 KVM requires the topmost vma of the RAM to be
793 * smaller than an system defined value, which is at least 256GB.
794 * Larger systems have larger values. We put the guest between
795 * the end of data segment (system break) and this value. We
796 * use 32GB as a base to have enough room for the system break
797 * to grow. We also have to use MAP parameters that avoid
798 * read-only mapping of guest pages.
799 */
800 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared)
801 {
802 static void *mem;
803
804 if (mem) {
805 /* we only support one allocation, which is enough for initial ram */
806 return NULL;
807 }
808
809 mem = mmap((void *) 0x800000000ULL, size,
810 PROT_EXEC|PROT_READ|PROT_WRITE,
811 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
812 if (mem == MAP_FAILED) {
813 mem = NULL;
814 }
815 if (mem && align) {
816 *align = QEMU_VMALLOC_ALIGN;
817 }
818 return mem;
819 }
820
821 static uint8_t const *sw_bp_inst;
822 static uint8_t sw_bp_ilen;
823
824 static void determine_sw_breakpoint_instr(void)
825 {
826 /* DIAG 501 is used for sw breakpoints with old kernels */
827 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
828 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
829 static const uint8_t instr_0x0000[] = {0x00, 0x00};
830
831 if (sw_bp_inst) {
832 return;
833 }
834 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
835 sw_bp_inst = diag_501;
836 sw_bp_ilen = sizeof(diag_501);
837 DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
838 } else {
839 sw_bp_inst = instr_0x0000;
840 sw_bp_ilen = sizeof(instr_0x0000);
841 DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
842 }
843 }
844
845 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
846 {
847 determine_sw_breakpoint_instr();
848
849 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
850 sw_bp_ilen, 0) ||
851 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
852 return -EINVAL;
853 }
854 return 0;
855 }
856
857 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
858 {
859 uint8_t t[MAX_ILEN];
860
861 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
862 return -EINVAL;
863 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
864 return -EINVAL;
865 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
866 sw_bp_ilen, 1)) {
867 return -EINVAL;
868 }
869
870 return 0;
871 }
872
873 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
874 int len, int type)
875 {
876 int n;
877
878 for (n = 0; n < nb_hw_breakpoints; n++) {
879 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
880 (hw_breakpoints[n].len == len || len == -1)) {
881 return &hw_breakpoints[n];
882 }
883 }
884
885 return NULL;
886 }
887
888 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
889 {
890 int size;
891
892 if (find_hw_breakpoint(addr, len, type)) {
893 return -EEXIST;
894 }
895
896 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
897
898 if (!hw_breakpoints) {
899 nb_hw_breakpoints = 0;
900 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
901 } else {
902 hw_breakpoints =
903 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
904 }
905
906 if (!hw_breakpoints) {
907 nb_hw_breakpoints = 0;
908 return -ENOMEM;
909 }
910
911 hw_breakpoints[nb_hw_breakpoints].addr = addr;
912 hw_breakpoints[nb_hw_breakpoints].len = len;
913 hw_breakpoints[nb_hw_breakpoints].type = type;
914
915 nb_hw_breakpoints++;
916
917 return 0;
918 }
919
920 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
921 target_ulong len, int type)
922 {
923 switch (type) {
924 case GDB_BREAKPOINT_HW:
925 type = KVM_HW_BP;
926 break;
927 case GDB_WATCHPOINT_WRITE:
928 if (len < 1) {
929 return -EINVAL;
930 }
931 type = KVM_HW_WP_WRITE;
932 break;
933 default:
934 return -ENOSYS;
935 }
936 return insert_hw_breakpoint(addr, len, type);
937 }
938
939 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
940 target_ulong len, int type)
941 {
942 int size;
943 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
944
945 if (bp == NULL) {
946 return -ENOENT;
947 }
948
949 nb_hw_breakpoints--;
950 if (nb_hw_breakpoints > 0) {
951 /*
952 * In order to trim the array, move the last element to the position to
953 * be removed - if necessary.
954 */
955 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
956 *bp = hw_breakpoints[nb_hw_breakpoints];
957 }
958 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
959 hw_breakpoints =
960 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
961 } else {
962 g_free(hw_breakpoints);
963 hw_breakpoints = NULL;
964 }
965
966 return 0;
967 }
968
969 void kvm_arch_remove_all_hw_breakpoints(void)
970 {
971 nb_hw_breakpoints = 0;
972 g_free(hw_breakpoints);
973 hw_breakpoints = NULL;
974 }
975
976 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
977 {
978 int i;
979
980 if (nb_hw_breakpoints > 0) {
981 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
982 dbg->arch.hw_bp = hw_breakpoints;
983
984 for (i = 0; i < nb_hw_breakpoints; ++i) {
985 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
986 hw_breakpoints[i].addr);
987 }
988 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
989 } else {
990 dbg->arch.nr_hw_bp = 0;
991 dbg->arch.hw_bp = NULL;
992 }
993 }
994
995 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
996 {
997 }
998
999 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1000 {
1001 return MEMTXATTRS_UNSPECIFIED;
1002 }
1003
1004 int kvm_arch_process_async_events(CPUState *cs)
1005 {
1006 return cs->halted;
1007 }
1008
1009 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
1010 struct kvm_s390_interrupt *interrupt)
1011 {
1012 int r = 0;
1013
1014 interrupt->type = irq->type;
1015 switch (irq->type) {
1016 case KVM_S390_INT_VIRTIO:
1017 interrupt->parm = irq->u.ext.ext_params;
1018 /* fall through */
1019 case KVM_S390_INT_PFAULT_INIT:
1020 case KVM_S390_INT_PFAULT_DONE:
1021 interrupt->parm64 = irq->u.ext.ext_params2;
1022 break;
1023 case KVM_S390_PROGRAM_INT:
1024 interrupt->parm = irq->u.pgm.code;
1025 break;
1026 case KVM_S390_SIGP_SET_PREFIX:
1027 interrupt->parm = irq->u.prefix.address;
1028 break;
1029 case KVM_S390_INT_SERVICE:
1030 interrupt->parm = irq->u.ext.ext_params;
1031 break;
1032 case KVM_S390_MCHK:
1033 interrupt->parm = irq->u.mchk.cr14;
1034 interrupt->parm64 = irq->u.mchk.mcic;
1035 break;
1036 case KVM_S390_INT_EXTERNAL_CALL:
1037 interrupt->parm = irq->u.extcall.code;
1038 break;
1039 case KVM_S390_INT_EMERGENCY:
1040 interrupt->parm = irq->u.emerg.code;
1041 break;
1042 case KVM_S390_SIGP_STOP:
1043 case KVM_S390_RESTART:
1044 break; /* These types have no parameters */
1045 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1046 interrupt->parm = irq->u.io.subchannel_id << 16;
1047 interrupt->parm |= irq->u.io.subchannel_nr;
1048 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
1049 interrupt->parm64 |= irq->u.io.io_int_word;
1050 break;
1051 default:
1052 r = -EINVAL;
1053 break;
1054 }
1055 return r;
1056 }
1057
1058 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
1059 {
1060 struct kvm_s390_interrupt kvmint = {};
1061 int r;
1062
1063 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1064 if (r < 0) {
1065 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1066 exit(1);
1067 }
1068
1069 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
1070 if (r < 0) {
1071 fprintf(stderr, "KVM failed to inject interrupt\n");
1072 exit(1);
1073 }
1074 }
1075
1076 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
1077 {
1078 CPUState *cs = CPU(cpu);
1079 int r;
1080
1081 if (cap_s390_irq) {
1082 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
1083 if (!r) {
1084 return;
1085 }
1086 error_report("KVM failed to inject interrupt %llx", irq->type);
1087 exit(1);
1088 }
1089
1090 inject_vcpu_irq_legacy(cs, irq);
1091 }
1092
1093 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq *irq)
1094 {
1095 struct kvm_s390_interrupt kvmint = {};
1096 int r;
1097
1098 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1099 if (r < 0) {
1100 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1101 exit(1);
1102 }
1103
1104 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1105 if (r < 0) {
1106 fprintf(stderr, "KVM failed to inject interrupt\n");
1107 exit(1);
1108 }
1109 }
1110
1111 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1112 {
1113 struct kvm_s390_irq irq = {
1114 .type = KVM_S390_PROGRAM_INT,
1115 .u.pgm.code = code,
1116 };
1117 qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
1118 cpu->env.psw.addr);
1119 kvm_s390_vcpu_interrupt(cpu, &irq);
1120 }
1121
1122 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1123 {
1124 struct kvm_s390_irq irq = {
1125 .type = KVM_S390_PROGRAM_INT,
1126 .u.pgm.code = code,
1127 .u.pgm.trans_exc_code = te_code,
1128 .u.pgm.exc_access_id = te_code & 3,
1129 };
1130
1131 kvm_s390_vcpu_interrupt(cpu, &irq);
1132 }
1133
1134 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1135 uint16_t ipbh0)
1136 {
1137 CPUS390XState *env = &cpu->env;
1138 uint64_t sccb;
1139 uint32_t code;
1140 int r = 0;
1141
1142 sccb = env->regs[ipbh0 & 0xf];
1143 code = env->regs[(ipbh0 & 0xf0) >> 4];
1144
1145 r = sclp_service_call(env, sccb, code);
1146 if (r < 0) {
1147 kvm_s390_program_interrupt(cpu, -r);
1148 } else {
1149 setcc(cpu, r);
1150 }
1151
1152 return 0;
1153 }
1154
1155 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1156 {
1157 CPUS390XState *env = &cpu->env;
1158 int rc = 0;
1159 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1160
1161 switch (ipa1) {
1162 case PRIV_B2_XSCH:
1163 ioinst_handle_xsch(cpu, env->regs[1], RA_IGNORED);
1164 break;
1165 case PRIV_B2_CSCH:
1166 ioinst_handle_csch(cpu, env->regs[1], RA_IGNORED);
1167 break;
1168 case PRIV_B2_HSCH:
1169 ioinst_handle_hsch(cpu, env->regs[1], RA_IGNORED);
1170 break;
1171 case PRIV_B2_MSCH:
1172 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1173 break;
1174 case PRIV_B2_SSCH:
1175 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1176 break;
1177 case PRIV_B2_STCRW:
1178 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb, RA_IGNORED);
1179 break;
1180 case PRIV_B2_STSCH:
1181 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1182 break;
1183 case PRIV_B2_TSCH:
1184 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1185 fprintf(stderr, "Spurious tsch intercept\n");
1186 break;
1187 case PRIV_B2_CHSC:
1188 ioinst_handle_chsc(cpu, run->s390_sieic.ipb, RA_IGNORED);
1189 break;
1190 case PRIV_B2_TPI:
1191 /* This should have been handled by kvm already. */
1192 fprintf(stderr, "Spurious tpi intercept\n");
1193 break;
1194 case PRIV_B2_SCHM:
1195 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1196 run->s390_sieic.ipb, RA_IGNORED);
1197 break;
1198 case PRIV_B2_RSCH:
1199 ioinst_handle_rsch(cpu, env->regs[1], RA_IGNORED);
1200 break;
1201 case PRIV_B2_RCHP:
1202 ioinst_handle_rchp(cpu, env->regs[1], RA_IGNORED);
1203 break;
1204 case PRIV_B2_STCPS:
1205 /* We do not provide this instruction, it is suppressed. */
1206 break;
1207 case PRIV_B2_SAL:
1208 ioinst_handle_sal(cpu, env->regs[1], RA_IGNORED);
1209 break;
1210 case PRIV_B2_SIGA:
1211 /* Not provided, set CC = 3 for subchannel not operational */
1212 setcc(cpu, 3);
1213 break;
1214 case PRIV_B2_SCLP_CALL:
1215 rc = kvm_sclp_service_call(cpu, run, ipbh0);
1216 break;
1217 default:
1218 rc = -1;
1219 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1220 break;
1221 }
1222
1223 return rc;
1224 }
1225
1226 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1227 uint8_t *ar)
1228 {
1229 CPUS390XState *env = &cpu->env;
1230 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1231 uint32_t base2 = run->s390_sieic.ipb >> 28;
1232 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1233 ((run->s390_sieic.ipb & 0xff00) << 4);
1234
1235 if (disp2 & 0x80000) {
1236 disp2 += 0xfff00000;
1237 }
1238 if (ar) {
1239 *ar = base2;
1240 }
1241
1242 return (base2 ? env->regs[base2] : 0) +
1243 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1244 }
1245
1246 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1247 uint8_t *ar)
1248 {
1249 CPUS390XState *env = &cpu->env;
1250 uint32_t base2 = run->s390_sieic.ipb >> 28;
1251 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1252 ((run->s390_sieic.ipb & 0xff00) << 4);
1253
1254 if (disp2 & 0x80000) {
1255 disp2 += 0xfff00000;
1256 }
1257 if (ar) {
1258 *ar = base2;
1259 }
1260
1261 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1262 }
1263
1264 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1265 {
1266 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1267
1268 if (s390_has_feat(S390_FEAT_ZPCI)) {
1269 return clp_service_call(cpu, r2, RA_IGNORED);
1270 } else {
1271 return -1;
1272 }
1273 }
1274
1275 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1276 {
1277 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1278 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1279
1280 if (s390_has_feat(S390_FEAT_ZPCI)) {
1281 return pcilg_service_call(cpu, r1, r2, RA_IGNORED);
1282 } else {
1283 return -1;
1284 }
1285 }
1286
1287 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1288 {
1289 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1290 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1291
1292 if (s390_has_feat(S390_FEAT_ZPCI)) {
1293 return pcistg_service_call(cpu, r1, r2, RA_IGNORED);
1294 } else {
1295 return -1;
1296 }
1297 }
1298
1299 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1300 {
1301 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1302 uint64_t fiba;
1303 uint8_t ar;
1304
1305 if (s390_has_feat(S390_FEAT_ZPCI)) {
1306 fiba = get_base_disp_rxy(cpu, run, &ar);
1307
1308 return stpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1309 } else {
1310 return -1;
1311 }
1312 }
1313
1314 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1315 {
1316 CPUS390XState *env = &cpu->env;
1317 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1318 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1319 uint8_t isc;
1320 uint16_t mode;
1321 int r;
1322
1323 mode = env->regs[r1] & 0xffff;
1324 isc = (env->regs[r3] >> 27) & 0x7;
1325 r = css_do_sic(env, isc, mode);
1326 if (r) {
1327 kvm_s390_program_interrupt(cpu, -r);
1328 }
1329
1330 return 0;
1331 }
1332
1333 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1334 {
1335 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1336 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1337
1338 if (s390_has_feat(S390_FEAT_ZPCI)) {
1339 return rpcit_service_call(cpu, r1, r2, RA_IGNORED);
1340 } else {
1341 return -1;
1342 }
1343 }
1344
1345 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1346 {
1347 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1348 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1349 uint64_t gaddr;
1350 uint8_t ar;
1351
1352 if (s390_has_feat(S390_FEAT_ZPCI)) {
1353 gaddr = get_base_disp_rsy(cpu, run, &ar);
1354
1355 return pcistb_service_call(cpu, r1, r3, gaddr, ar, RA_IGNORED);
1356 } else {
1357 return -1;
1358 }
1359 }
1360
1361 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1362 {
1363 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1364 uint64_t fiba;
1365 uint8_t ar;
1366
1367 if (s390_has_feat(S390_FEAT_ZPCI)) {
1368 fiba = get_base_disp_rxy(cpu, run, &ar);
1369
1370 return mpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1371 } else {
1372 return -1;
1373 }
1374 }
1375
1376 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1377 {
1378 int r = 0;
1379
1380 switch (ipa1) {
1381 case PRIV_B9_CLP:
1382 r = kvm_clp_service_call(cpu, run);
1383 break;
1384 case PRIV_B9_PCISTG:
1385 r = kvm_pcistg_service_call(cpu, run);
1386 break;
1387 case PRIV_B9_PCILG:
1388 r = kvm_pcilg_service_call(cpu, run);
1389 break;
1390 case PRIV_B9_RPCIT:
1391 r = kvm_rpcit_service_call(cpu, run);
1392 break;
1393 case PRIV_B9_EQBS:
1394 /* just inject exception */
1395 r = -1;
1396 break;
1397 default:
1398 r = -1;
1399 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1400 break;
1401 }
1402
1403 return r;
1404 }
1405
1406 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1407 {
1408 int r = 0;
1409
1410 switch (ipbl) {
1411 case PRIV_EB_PCISTB:
1412 r = kvm_pcistb_service_call(cpu, run);
1413 break;
1414 case PRIV_EB_SIC:
1415 r = kvm_sic_service_call(cpu, run);
1416 break;
1417 case PRIV_EB_SQBS:
1418 /* just inject exception */
1419 r = -1;
1420 break;
1421 default:
1422 r = -1;
1423 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1424 break;
1425 }
1426
1427 return r;
1428 }
1429
1430 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1431 {
1432 int r = 0;
1433
1434 switch (ipbl) {
1435 case PRIV_E3_MPCIFC:
1436 r = kvm_mpcifc_service_call(cpu, run);
1437 break;
1438 case PRIV_E3_STPCIFC:
1439 r = kvm_stpcifc_service_call(cpu, run);
1440 break;
1441 default:
1442 r = -1;
1443 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1444 break;
1445 }
1446
1447 return r;
1448 }
1449
1450 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1451 {
1452 CPUS390XState *env = &cpu->env;
1453 int ret;
1454
1455 ret = s390_virtio_hypercall(env);
1456 if (ret == -EINVAL) {
1457 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1458 return 0;
1459 }
1460
1461 return ret;
1462 }
1463
1464 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1465 {
1466 uint64_t r1, r3;
1467 int rc;
1468
1469 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1470 r3 = run->s390_sieic.ipa & 0x000f;
1471 rc = handle_diag_288(&cpu->env, r1, r3);
1472 if (rc) {
1473 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1474 }
1475 }
1476
1477 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1478 {
1479 uint64_t r1, r3;
1480
1481 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1482 r3 = run->s390_sieic.ipa & 0x000f;
1483 handle_diag_308(&cpu->env, r1, r3, RA_IGNORED);
1484 }
1485
1486 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1487 {
1488 CPUS390XState *env = &cpu->env;
1489 unsigned long pc;
1490
1491 pc = env->psw.addr - sw_bp_ilen;
1492 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1493 env->psw.addr = pc;
1494 return EXCP_DEBUG;
1495 }
1496
1497 return -ENOENT;
1498 }
1499
1500 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1501
1502 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1503 {
1504 int r = 0;
1505 uint16_t func_code;
1506
1507 /*
1508 * For any diagnose call we support, bits 48-63 of the resulting
1509 * address specify the function code; the remainder is ignored.
1510 */
1511 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1512 switch (func_code) {
1513 case DIAG_TIMEREVENT:
1514 kvm_handle_diag_288(cpu, run);
1515 break;
1516 case DIAG_IPL:
1517 kvm_handle_diag_308(cpu, run);
1518 break;
1519 case DIAG_KVM_HYPERCALL:
1520 r = handle_hypercall(cpu, run);
1521 break;
1522 case DIAG_KVM_BREAKPOINT:
1523 r = handle_sw_breakpoint(cpu, run);
1524 break;
1525 default:
1526 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1527 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1528 break;
1529 }
1530
1531 return r;
1532 }
1533
1534 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb)
1535 {
1536 CPUS390XState *env = &cpu->env;
1537 const uint8_t r1 = ipa1 >> 4;
1538 const uint8_t r3 = ipa1 & 0x0f;
1539 int ret;
1540 uint8_t order;
1541
1542 /* get order code */
1543 order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK;
1544
1545 ret = handle_sigp(env, order, r1, r3);
1546 setcc(cpu, ret);
1547 return 0;
1548 }
1549
1550 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1551 {
1552 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1553 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1554 int r = -1;
1555
1556 DPRINTF("handle_instruction 0x%x 0x%x\n",
1557 run->s390_sieic.ipa, run->s390_sieic.ipb);
1558 switch (ipa0) {
1559 case IPA0_B2:
1560 r = handle_b2(cpu, run, ipa1);
1561 break;
1562 case IPA0_B9:
1563 r = handle_b9(cpu, run, ipa1);
1564 break;
1565 case IPA0_EB:
1566 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1567 break;
1568 case IPA0_E3:
1569 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1570 break;
1571 case IPA0_DIAG:
1572 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1573 break;
1574 case IPA0_SIGP:
1575 r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb);
1576 break;
1577 }
1578
1579 if (r < 0) {
1580 r = 0;
1581 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1582 }
1583
1584 return r;
1585 }
1586
1587 static void unmanageable_intercept(S390CPU *cpu, S390CrashReason reason,
1588 int pswoffset)
1589 {
1590 CPUState *cs = CPU(cpu);
1591
1592 s390_cpu_halt(cpu);
1593 cpu->env.crash_reason = reason;
1594 qemu_system_guest_panicked(cpu_get_crash_info(cs));
1595 }
1596
1597 /* try to detect pgm check loops */
1598 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1599 {
1600 CPUState *cs = CPU(cpu);
1601 PSW oldpsw, newpsw;
1602
1603 newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1604 offsetof(LowCore, program_new_psw));
1605 newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1606 offsetof(LowCore, program_new_psw) + 8);
1607 oldpsw.mask = run->psw_mask;
1608 oldpsw.addr = run->psw_addr;
1609 /*
1610 * Avoid endless loops of operation exceptions, if the pgm new
1611 * PSW will cause a new operation exception.
1612 * The heuristic checks if the pgm new psw is within 6 bytes before
1613 * the faulting psw address (with same DAT, AS settings) and the
1614 * new psw is not a wait psw and the fault was not triggered by
1615 * problem state. In that case go into crashed state.
1616 */
1617
1618 if (oldpsw.addr - newpsw.addr <= 6 &&
1619 !(newpsw.mask & PSW_MASK_WAIT) &&
1620 !(oldpsw.mask & PSW_MASK_PSTATE) &&
1621 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1622 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1623 unmanageable_intercept(cpu, S390_CRASH_REASON_OPINT_LOOP,
1624 offsetof(LowCore, program_new_psw));
1625 return EXCP_HALTED;
1626 }
1627 return 0;
1628 }
1629
1630 static int handle_intercept(S390CPU *cpu)
1631 {
1632 CPUState *cs = CPU(cpu);
1633 struct kvm_run *run = cs->kvm_run;
1634 int icpt_code = run->s390_sieic.icptcode;
1635 int r = 0;
1636
1637 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1638 (long)cs->kvm_run->psw_addr);
1639 switch (icpt_code) {
1640 case ICPT_INSTRUCTION:
1641 r = handle_instruction(cpu, run);
1642 break;
1643 case ICPT_PROGRAM:
1644 unmanageable_intercept(cpu, S390_CRASH_REASON_PGMINT_LOOP,
1645 offsetof(LowCore, program_new_psw));
1646 r = EXCP_HALTED;
1647 break;
1648 case ICPT_EXT_INT:
1649 unmanageable_intercept(cpu, S390_CRASH_REASON_EXTINT_LOOP,
1650 offsetof(LowCore, external_new_psw));
1651 r = EXCP_HALTED;
1652 break;
1653 case ICPT_WAITPSW:
1654 /* disabled wait, since enabled wait is handled in kernel */
1655 s390_handle_wait(cpu);
1656 r = EXCP_HALTED;
1657 break;
1658 case ICPT_CPU_STOP:
1659 do_stop_interrupt(&cpu->env);
1660 r = EXCP_HALTED;
1661 break;
1662 case ICPT_OPEREXC:
1663 /* check for break points */
1664 r = handle_sw_breakpoint(cpu, run);
1665 if (r == -ENOENT) {
1666 /* Then check for potential pgm check loops */
1667 r = handle_oper_loop(cpu, run);
1668 if (r == 0) {
1669 kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1670 }
1671 }
1672 break;
1673 case ICPT_SOFT_INTERCEPT:
1674 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1675 exit(1);
1676 break;
1677 case ICPT_IO:
1678 fprintf(stderr, "KVM unimplemented icpt IO\n");
1679 exit(1);
1680 break;
1681 default:
1682 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1683 exit(1);
1684 break;
1685 }
1686
1687 return r;
1688 }
1689
1690 static int handle_tsch(S390CPU *cpu)
1691 {
1692 CPUState *cs = CPU(cpu);
1693 struct kvm_run *run = cs->kvm_run;
1694 int ret;
1695
1696 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb,
1697 RA_IGNORED);
1698 if (ret < 0) {
1699 /*
1700 * Failure.
1701 * If an I/O interrupt had been dequeued, we have to reinject it.
1702 */
1703 if (run->s390_tsch.dequeued) {
1704 s390_io_interrupt(run->s390_tsch.subchannel_id,
1705 run->s390_tsch.subchannel_nr,
1706 run->s390_tsch.io_int_parm,
1707 run->s390_tsch.io_int_word);
1708 }
1709 ret = 0;
1710 }
1711 return ret;
1712 }
1713
1714 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1715 {
1716 SysIB_322 sysib;
1717 int del;
1718
1719 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1720 return;
1721 }
1722 /* Shift the stack of Extended Names to prepare for our own data */
1723 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1724 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1725 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1726 * assumed it's not capable of managing Extended Names for lower levels.
1727 */
1728 for (del = 1; del < sysib.count; del++) {
1729 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1730 break;
1731 }
1732 }
1733 if (del < sysib.count) {
1734 memset(sysib.ext_names[del], 0,
1735 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1736 }
1737 /* Insert short machine name in EBCDIC, padded with blanks */
1738 if (qemu_name) {
1739 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1740 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1741 strlen(qemu_name)));
1742 }
1743 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1744 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1745 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1746 * considered by s390 as not capable of providing any Extended Name.
1747 * Therefore if no name was specified on qemu invocation, we go with the
1748 * same "KVMguest" default, which KVM has filled into short name field.
1749 */
1750 if (qemu_name) {
1751 strncpy((char *)sysib.ext_names[0], qemu_name,
1752 sizeof(sysib.ext_names[0]));
1753 } else {
1754 strcpy((char *)sysib.ext_names[0], "KVMguest");
1755 }
1756 /* Insert UUID */
1757 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
1758
1759 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1760 }
1761
1762 static int handle_stsi(S390CPU *cpu)
1763 {
1764 CPUState *cs = CPU(cpu);
1765 struct kvm_run *run = cs->kvm_run;
1766
1767 switch (run->s390_stsi.fc) {
1768 case 3:
1769 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1770 return 0;
1771 }
1772 /* Only sysib 3.2.2 needs post-handling for now. */
1773 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1774 return 0;
1775 default:
1776 return 0;
1777 }
1778 }
1779
1780 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1781 {
1782 CPUState *cs = CPU(cpu);
1783 struct kvm_run *run = cs->kvm_run;
1784
1785 int ret = 0;
1786 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1787
1788 switch (arch_info->type) {
1789 case KVM_HW_WP_WRITE:
1790 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1791 cs->watchpoint_hit = &hw_watchpoint;
1792 hw_watchpoint.vaddr = arch_info->addr;
1793 hw_watchpoint.flags = BP_MEM_WRITE;
1794 ret = EXCP_DEBUG;
1795 }
1796 break;
1797 case KVM_HW_BP:
1798 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1799 ret = EXCP_DEBUG;
1800 }
1801 break;
1802 case KVM_SINGLESTEP:
1803 if (cs->singlestep_enabled) {
1804 ret = EXCP_DEBUG;
1805 }
1806 break;
1807 default:
1808 ret = -ENOSYS;
1809 }
1810
1811 return ret;
1812 }
1813
1814 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1815 {
1816 S390CPU *cpu = S390_CPU(cs);
1817 int ret = 0;
1818
1819 qemu_mutex_lock_iothread();
1820
1821 kvm_cpu_synchronize_state(cs);
1822
1823 switch (run->exit_reason) {
1824 case KVM_EXIT_S390_SIEIC:
1825 ret = handle_intercept(cpu);
1826 break;
1827 case KVM_EXIT_S390_RESET:
1828 s390_ipl_reset_request(cs, S390_RESET_REIPL);
1829 break;
1830 case KVM_EXIT_S390_TSCH:
1831 ret = handle_tsch(cpu);
1832 break;
1833 case KVM_EXIT_S390_STSI:
1834 ret = handle_stsi(cpu);
1835 break;
1836 case KVM_EXIT_DEBUG:
1837 ret = kvm_arch_handle_debug_exit(cpu);
1838 break;
1839 default:
1840 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1841 break;
1842 }
1843 qemu_mutex_unlock_iothread();
1844
1845 if (ret == 0) {
1846 ret = EXCP_INTERRUPT;
1847 }
1848 return ret;
1849 }
1850
1851 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1852 {
1853 return true;
1854 }
1855
1856 void kvm_s390_enable_css_support(S390CPU *cpu)
1857 {
1858 int r;
1859
1860 /* Activate host kernel channel subsystem support. */
1861 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1862 assert(r == 0);
1863 }
1864
1865 void kvm_arch_init_irq_routing(KVMState *s)
1866 {
1867 /*
1868 * Note that while irqchip capabilities generally imply that cpustates
1869 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1870 * have to override the common code kvm_halt_in_kernel_allowed setting.
1871 */
1872 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1873 kvm_gsi_routing_allowed = true;
1874 kvm_halt_in_kernel_allowed = false;
1875 }
1876 }
1877
1878 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1879 int vq, bool assign)
1880 {
1881 struct kvm_ioeventfd kick = {
1882 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1883 KVM_IOEVENTFD_FLAG_DATAMATCH,
1884 .fd = event_notifier_get_fd(notifier),
1885 .datamatch = vq,
1886 .addr = sch,
1887 .len = 8,
1888 };
1889 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1890 return -ENOSYS;
1891 }
1892 if (!assign) {
1893 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1894 }
1895 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1896 }
1897
1898 int kvm_s390_get_ri(void)
1899 {
1900 return cap_ri;
1901 }
1902
1903 int kvm_s390_get_gs(void)
1904 {
1905 return cap_gs;
1906 }
1907
1908 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1909 {
1910 struct kvm_mp_state mp_state = {};
1911 int ret;
1912
1913 /* the kvm part might not have been initialized yet */
1914 if (CPU(cpu)->kvm_state == NULL) {
1915 return 0;
1916 }
1917
1918 switch (cpu_state) {
1919 case S390_CPU_STATE_STOPPED:
1920 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1921 break;
1922 case S390_CPU_STATE_CHECK_STOP:
1923 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1924 break;
1925 case S390_CPU_STATE_OPERATING:
1926 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1927 break;
1928 case S390_CPU_STATE_LOAD:
1929 mp_state.mp_state = KVM_MP_STATE_LOAD;
1930 break;
1931 default:
1932 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1933 cpu_state);
1934 exit(1);
1935 }
1936
1937 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1938 if (ret) {
1939 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1940 strerror(-ret));
1941 }
1942
1943 return ret;
1944 }
1945
1946 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
1947 {
1948 struct kvm_s390_irq_state irq_state = {
1949 .buf = (uint64_t) cpu->irqstate,
1950 .len = VCPU_IRQ_BUF_SIZE,
1951 };
1952 CPUState *cs = CPU(cpu);
1953 int32_t bytes;
1954
1955 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1956 return;
1957 }
1958
1959 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
1960 if (bytes < 0) {
1961 cpu->irqstate_saved_size = 0;
1962 error_report("Migration of interrupt state failed");
1963 return;
1964 }
1965
1966 cpu->irqstate_saved_size = bytes;
1967 }
1968
1969 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
1970 {
1971 CPUState *cs = CPU(cpu);
1972 struct kvm_s390_irq_state irq_state = {
1973 .buf = (uint64_t) cpu->irqstate,
1974 .len = cpu->irqstate_saved_size,
1975 };
1976 int r;
1977
1978 if (cpu->irqstate_saved_size == 0) {
1979 return 0;
1980 }
1981
1982 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1983 return -ENOSYS;
1984 }
1985
1986 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
1987 if (r) {
1988 error_report("Setting interrupt state failed %d", r);
1989 }
1990 return r;
1991 }
1992
1993 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1994 uint64_t address, uint32_t data, PCIDevice *dev)
1995 {
1996 S390PCIBusDevice *pbdev;
1997 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
1998
1999 if (!dev) {
2000 DPRINTF("add_msi_route no pci device\n");
2001 return -ENODEV;
2002 }
2003
2004 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
2005 if (!pbdev) {
2006 DPRINTF("add_msi_route no zpci device\n");
2007 return -ENODEV;
2008 }
2009
2010 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2011 route->flags = 0;
2012 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2013 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2014 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2015 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
2016 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2017 return 0;
2018 }
2019
2020 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2021 int vector, PCIDevice *dev)
2022 {
2023 return 0;
2024 }
2025
2026 int kvm_arch_release_virq_post(int virq)
2027 {
2028 return 0;
2029 }
2030
2031 int kvm_arch_msi_data_to_gsi(uint32_t data)
2032 {
2033 abort();
2034 }
2035
2036 static int query_cpu_subfunc(S390FeatBitmap features)
2037 {
2038 struct kvm_s390_vm_cpu_subfunc prop;
2039 struct kvm_device_attr attr = {
2040 .group = KVM_S390_VM_CPU_MODEL,
2041 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2042 .addr = (uint64_t) &prop,
2043 };
2044 int rc;
2045
2046 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2047 if (rc) {
2048 return rc;
2049 }
2050
2051 /*
2052 * We're going to add all subfunctions now, if the corresponding feature
2053 * is available that unlocks the query functions.
2054 */
2055 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2056 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2057 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2058 }
2059 if (test_bit(S390_FEAT_MSA, features)) {
2060 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2061 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2062 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2063 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2064 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2065 }
2066 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2067 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2068 }
2069 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2070 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2071 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2072 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2073 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2074 }
2075 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2076 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2077 }
2078 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2079 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2080 }
2081 return 0;
2082 }
2083
2084 static int configure_cpu_subfunc(const S390FeatBitmap features)
2085 {
2086 struct kvm_s390_vm_cpu_subfunc prop = {};
2087 struct kvm_device_attr attr = {
2088 .group = KVM_S390_VM_CPU_MODEL,
2089 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2090 .addr = (uint64_t) &prop,
2091 };
2092
2093 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2094 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2095 /* hardware support might be missing, IBC will handle most of this */
2096 return 0;
2097 }
2098
2099 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2100 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2101 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2102 }
2103 if (test_bit(S390_FEAT_MSA, features)) {
2104 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2105 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2106 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2107 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2108 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2109 }
2110 if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2111 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2112 }
2113 if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2114 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2115 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2116 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2117 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2118 }
2119 if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2120 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2121 }
2122 if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2123 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2124 }
2125 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2126 }
2127
2128 static int kvm_to_feat[][2] = {
2129 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2130 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2131 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2132 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2133 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2134 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2135 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2136 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2137 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2138 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2139 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2140 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2141 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2142 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2143 };
2144
2145 static int query_cpu_feat(S390FeatBitmap features)
2146 {
2147 struct kvm_s390_vm_cpu_feat prop;
2148 struct kvm_device_attr attr = {
2149 .group = KVM_S390_VM_CPU_MODEL,
2150 .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2151 .addr = (uint64_t) &prop,
2152 };
2153 int rc;
2154 int i;
2155
2156 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2157 if (rc) {
2158 return rc;
2159 }
2160
2161 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2162 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2163 set_bit(kvm_to_feat[i][1], features);
2164 }
2165 }
2166 return 0;
2167 }
2168
2169 static int configure_cpu_feat(const S390FeatBitmap features)
2170 {
2171 struct kvm_s390_vm_cpu_feat prop = {};
2172 struct kvm_device_attr attr = {
2173 .group = KVM_S390_VM_CPU_MODEL,
2174 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2175 .addr = (uint64_t) &prop,
2176 };
2177 int i;
2178
2179 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2180 if (test_bit(kvm_to_feat[i][1], features)) {
2181 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2182 }
2183 }
2184 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2185 }
2186
2187 bool kvm_s390_cpu_models_supported(void)
2188 {
2189 if (!cpu_model_allowed()) {
2190 /* compatibility machines interfere with the cpu model */
2191 return false;
2192 }
2193 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2194 KVM_S390_VM_CPU_MACHINE) &&
2195 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2196 KVM_S390_VM_CPU_PROCESSOR) &&
2197 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2198 KVM_S390_VM_CPU_MACHINE_FEAT) &&
2199 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2200 KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2201 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2202 KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2203 }
2204
2205 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2206 {
2207 struct kvm_s390_vm_cpu_machine prop = {};
2208 struct kvm_device_attr attr = {
2209 .group = KVM_S390_VM_CPU_MODEL,
2210 .attr = KVM_S390_VM_CPU_MACHINE,
2211 .addr = (uint64_t) &prop,
2212 };
2213 uint16_t unblocked_ibc = 0, cpu_type = 0;
2214 int rc;
2215
2216 memset(model, 0, sizeof(*model));
2217
2218 if (!kvm_s390_cpu_models_supported()) {
2219 error_setg(errp, "KVM doesn't support CPU models");
2220 return;
2221 }
2222
2223 /* query the basic cpu model properties */
2224 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2225 if (rc) {
2226 error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2227 return;
2228 }
2229
2230 cpu_type = cpuid_type(prop.cpuid);
2231 if (has_ibc(prop.ibc)) {
2232 model->lowest_ibc = lowest_ibc(prop.ibc);
2233 unblocked_ibc = unblocked_ibc(prop.ibc);
2234 }
2235 model->cpu_id = cpuid_id(prop.cpuid);
2236 model->cpu_id_format = cpuid_format(prop.cpuid);
2237 model->cpu_ver = 0xff;
2238
2239 /* get supported cpu features indicated via STFL(E) */
2240 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2241 (uint8_t *) prop.fac_mask);
2242 /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2243 if (test_bit(S390_FEAT_STFLE, model->features)) {
2244 set_bit(S390_FEAT_DAT_ENH_2, model->features);
2245 }
2246 /* get supported cpu features indicated e.g. via SCLP */
2247 rc = query_cpu_feat(model->features);
2248 if (rc) {
2249 error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2250 return;
2251 }
2252 /* get supported cpu subfunctions indicated via query / test bit */
2253 rc = query_cpu_subfunc(model->features);
2254 if (rc) {
2255 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2256 return;
2257 }
2258
2259 /* PTFF subfunctions might be indicated although kernel support missing */
2260 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) {
2261 clear_bit(S390_FEAT_PTFF_QSIE, model->features);
2262 clear_bit(S390_FEAT_PTFF_QTOUE, model->features);
2263 clear_bit(S390_FEAT_PTFF_STOE, model->features);
2264 clear_bit(S390_FEAT_PTFF_STOUE, model->features);
2265 }
2266
2267 /* with cpu model support, CMM is only indicated if really available */
2268 if (kvm_s390_cmma_available()) {
2269 set_bit(S390_FEAT_CMM, model->features);
2270 } else {
2271 /* no cmm -> no cmm nt */
2272 clear_bit(S390_FEAT_CMM_NT, model->features);
2273 }
2274
2275 /* bpb needs kernel support for migration, VSIE and reset */
2276 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) {
2277 clear_bit(S390_FEAT_BPB, model->features);
2278 }
2279
2280 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2281 set_bit(S390_FEAT_ZPCI, model->features);
2282 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2283
2284 if (s390_known_cpu_type(cpu_type)) {
2285 /* we want the exact model, even if some features are missing */
2286 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2287 ibc_ec_ga(unblocked_ibc), NULL);
2288 } else {
2289 /* model unknown, e.g. too new - search using features */
2290 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2291 ibc_ec_ga(unblocked_ibc),
2292 model->features);
2293 }
2294 if (!model->def) {
2295 error_setg(errp, "KVM: host CPU model could not be identified");
2296 return;
2297 }
2298 /* for now, we can only provide the AP feature with HW support */
2299 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO,
2300 KVM_S390_VM_CRYPTO_ENABLE_APIE)) {
2301 set_bit(S390_FEAT_AP, model->features);
2302 }
2303 /* strip of features that are not part of the maximum model */
2304 bitmap_and(model->features, model->features, model->def->full_feat,
2305 S390_FEAT_MAX);
2306 }
2307
2308 static void kvm_s390_configure_apie(bool interpret)
2309 {
2310 uint64_t attr = interpret ? KVM_S390_VM_CRYPTO_ENABLE_APIE :
2311 KVM_S390_VM_CRYPTO_DISABLE_APIE;
2312
2313 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
2314 kvm_s390_set_attr(attr);
2315 }
2316 }
2317
2318 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2319 {
2320 struct kvm_s390_vm_cpu_processor prop = {
2321 .fac_list = { 0 },
2322 };
2323 struct kvm_device_attr attr = {
2324 .group = KVM_S390_VM_CPU_MODEL,
2325 .attr = KVM_S390_VM_CPU_PROCESSOR,
2326 .addr = (uint64_t) &prop,
2327 };
2328 int rc;
2329
2330 if (!model) {
2331 /* compatibility handling if cpu models are disabled */
2332 if (kvm_s390_cmma_available()) {
2333 kvm_s390_enable_cmma();
2334 }
2335 return;
2336 }
2337 if (!kvm_s390_cpu_models_supported()) {
2338 error_setg(errp, "KVM doesn't support CPU models");
2339 return;
2340 }
2341 prop.cpuid = s390_cpuid_from_cpu_model(model);
2342 prop.ibc = s390_ibc_from_cpu_model(model);
2343 /* configure cpu features indicated via STFL(e) */
2344 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2345 (uint8_t *) prop.fac_list);
2346 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2347 if (rc) {
2348 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2349 return;
2350 }
2351 /* configure cpu features indicated e.g. via SCLP */
2352 rc = configure_cpu_feat(model->features);
2353 if (rc) {
2354 error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2355 return;
2356 }
2357 /* configure cpu subfunctions indicated via query / test bit */
2358 rc = configure_cpu_subfunc(model->features);
2359 if (rc) {
2360 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2361 return;
2362 }
2363 /* enable CMM via CMMA */
2364 if (test_bit(S390_FEAT_CMM, model->features)) {
2365 kvm_s390_enable_cmma();
2366 }
2367
2368 if (test_bit(S390_FEAT_AP, model->features)) {
2369 kvm_s390_configure_apie(true);
2370 }
2371 }
2372
2373 void kvm_s390_restart_interrupt(S390CPU *cpu)
2374 {
2375 struct kvm_s390_irq irq = {
2376 .type = KVM_S390_RESTART,
2377 };
2378
2379 kvm_s390_vcpu_interrupt(cpu, &irq);
2380 }
2381
2382 void kvm_s390_stop_interrupt(S390CPU *cpu)
2383 {
2384 struct kvm_s390_irq irq = {
2385 .type = KVM_S390_SIGP_STOP,
2386 };
2387
2388 kvm_s390_vcpu_interrupt(cpu, &irq);
2389 }