]> git.proxmox.com Git - mirror_qemu.git/blob - target-s390x/kvm.c
s390: implement pci instructions
[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 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.
11 *
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.
16 *
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.
19 *
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/>.
22 */
23
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/mman.h>
27
28 #include <linux/kvm.h>
29 #include <asm/ptrace.h>
30
31 #include "qemu-common.h"
32 #include "qemu/timer.h"
33 #include "sysemu/sysemu.h"
34 #include "sysemu/kvm.h"
35 #include "hw/hw.h"
36 #include "cpu.h"
37 #include "sysemu/device_tree.h"
38 #include "qapi/qmp/qjson.h"
39 #include "monitor/monitor.h"
40 #include "exec/gdbstub.h"
41 #include "trace.h"
42 #include "qapi-event.h"
43 #include "hw/s390x/s390-pci-inst.h"
44
45 /* #define DEBUG_KVM */
46
47 #ifdef DEBUG_KVM
48 #define DPRINTF(fmt, ...) \
49 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
50 #else
51 #define DPRINTF(fmt, ...) \
52 do { } while (0)
53 #endif
54
55 #define IPA0_DIAG 0x8300
56 #define IPA0_SIGP 0xae00
57 #define IPA0_B2 0xb200
58 #define IPA0_B9 0xb900
59 #define IPA0_EB 0xeb00
60 #define IPA0_E3 0xe300
61
62 #define PRIV_B2_SCLP_CALL 0x20
63 #define PRIV_B2_CSCH 0x30
64 #define PRIV_B2_HSCH 0x31
65 #define PRIV_B2_MSCH 0x32
66 #define PRIV_B2_SSCH 0x33
67 #define PRIV_B2_STSCH 0x34
68 #define PRIV_B2_TSCH 0x35
69 #define PRIV_B2_TPI 0x36
70 #define PRIV_B2_SAL 0x37
71 #define PRIV_B2_RSCH 0x38
72 #define PRIV_B2_STCRW 0x39
73 #define PRIV_B2_STCPS 0x3a
74 #define PRIV_B2_RCHP 0x3b
75 #define PRIV_B2_SCHM 0x3c
76 #define PRIV_B2_CHSC 0x5f
77 #define PRIV_B2_SIGA 0x74
78 #define PRIV_B2_XSCH 0x76
79
80 #define PRIV_EB_SQBS 0x8a
81 #define PRIV_EB_PCISTB 0xd0
82 #define PRIV_EB_SIC 0xd1
83
84 #define PRIV_B9_EQBS 0x9c
85 #define PRIV_B9_CLP 0xa0
86 #define PRIV_B9_PCISTG 0xd0
87 #define PRIV_B9_PCILG 0xd2
88 #define PRIV_B9_RPCIT 0xd3
89
90 #define PRIV_E3_MPCIFC 0xd0
91 #define PRIV_E3_STPCIFC 0xd4
92
93 #define DIAG_IPL 0x308
94 #define DIAG_KVM_HYPERCALL 0x500
95 #define DIAG_KVM_BREAKPOINT 0x501
96
97 #define ICPT_INSTRUCTION 0x04
98 #define ICPT_PROGRAM 0x08
99 #define ICPT_EXT_INT 0x14
100 #define ICPT_WAITPSW 0x1c
101 #define ICPT_SOFT_INTERCEPT 0x24
102 #define ICPT_CPU_STOP 0x28
103 #define ICPT_IO 0x40
104
105 static CPUWatchpoint hw_watchpoint;
106 /*
107 * We don't use a list because this structure is also used to transmit the
108 * hardware breakpoints to the kernel.
109 */
110 static struct kvm_hw_breakpoint *hw_breakpoints;
111 static int nb_hw_breakpoints;
112
113 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
114 KVM_CAP_LAST_INFO
115 };
116
117 static int cap_sync_regs;
118 static int cap_async_pf;
119
120 static void *legacy_s390_alloc(size_t size, uint64_t *align);
121
122 static int kvm_s390_check_clear_cmma(KVMState *s)
123 {
124 struct kvm_device_attr attr = {
125 .group = KVM_S390_VM_MEM_CTRL,
126 .attr = KVM_S390_VM_MEM_CLR_CMMA,
127 };
128
129 return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
130 }
131
132 static int kvm_s390_check_enable_cmma(KVMState *s)
133 {
134 struct kvm_device_attr attr = {
135 .group = KVM_S390_VM_MEM_CTRL,
136 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
137 };
138
139 return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
140 }
141
142 void kvm_s390_clear_cmma_callback(void *opaque)
143 {
144 int rc;
145 KVMState *s = opaque;
146 struct kvm_device_attr attr = {
147 .group = KVM_S390_VM_MEM_CTRL,
148 .attr = KVM_S390_VM_MEM_CLR_CMMA,
149 };
150
151 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
152 trace_kvm_clear_cmma(rc);
153 }
154
155 static void kvm_s390_enable_cmma(KVMState *s)
156 {
157 int rc;
158 struct kvm_device_attr attr = {
159 .group = KVM_S390_VM_MEM_CTRL,
160 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
161 };
162
163 if (kvm_s390_check_enable_cmma(s) || kvm_s390_check_clear_cmma(s)) {
164 return;
165 }
166
167 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
168 if (!rc) {
169 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
170 }
171 trace_kvm_enable_cmma(rc);
172 }
173
174 int kvm_arch_init(KVMState *s)
175 {
176 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
177 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
178
179 if (kvm_check_extension(s, KVM_CAP_VM_ATTRIBUTES)) {
180 kvm_s390_enable_cmma(s);
181 }
182
183 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
184 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
185 phys_mem_set_alloc(legacy_s390_alloc);
186 }
187 return 0;
188 }
189
190 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
191 {
192 return cpu->cpu_index;
193 }
194
195 int kvm_arch_init_vcpu(CPUState *cs)
196 {
197 S390CPU *cpu = S390_CPU(cs);
198 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
199 return 0;
200 }
201
202 void kvm_s390_reset_vcpu(S390CPU *cpu)
203 {
204 CPUState *cs = CPU(cpu);
205
206 /* The initial reset call is needed here to reset in-kernel
207 * vcpu data that we can't access directly from QEMU
208 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
209 * Before this ioctl cpu_synchronize_state() is called in common kvm
210 * code (kvm-all) */
211 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
212 error_report("Initial CPU reset failed on CPU %i\n", cs->cpu_index);
213 }
214 }
215
216 static int can_sync_regs(CPUState *cs, int regs)
217 {
218 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
219 }
220
221 int kvm_arch_put_registers(CPUState *cs, int level)
222 {
223 S390CPU *cpu = S390_CPU(cs);
224 CPUS390XState *env = &cpu->env;
225 struct kvm_sregs sregs;
226 struct kvm_regs regs;
227 struct kvm_fpu fpu = {};
228 int r;
229 int i;
230
231 /* always save the PSW and the GPRS*/
232 cs->kvm_run->psw_addr = env->psw.addr;
233 cs->kvm_run->psw_mask = env->psw.mask;
234
235 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
236 for (i = 0; i < 16; i++) {
237 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
238 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
239 }
240 } else {
241 for (i = 0; i < 16; i++) {
242 regs.gprs[i] = env->regs[i];
243 }
244 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
245 if (r < 0) {
246 return r;
247 }
248 }
249
250 /* Floating point */
251 for (i = 0; i < 16; i++) {
252 fpu.fprs[i] = env->fregs[i].ll;
253 }
254 fpu.fpc = env->fpc;
255
256 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
257 if (r < 0) {
258 return r;
259 }
260
261 /* Do we need to save more than that? */
262 if (level == KVM_PUT_RUNTIME_STATE) {
263 return 0;
264 }
265
266 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
267 cs->kvm_run->s.regs.cputm = env->cputm;
268 cs->kvm_run->s.regs.ckc = env->ckc;
269 cs->kvm_run->s.regs.todpr = env->todpr;
270 cs->kvm_run->s.regs.gbea = env->gbea;
271 cs->kvm_run->s.regs.pp = env->pp;
272 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
273 } else {
274 /*
275 * These ONE_REGS are not protected by a capability. As they are only
276 * necessary for migration we just trace a possible error, but don't
277 * return with an error return code.
278 */
279 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
280 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
281 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
282 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
283 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
284 }
285
286 /* pfault parameters */
287 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
288 cs->kvm_run->s.regs.pft = env->pfault_token;
289 cs->kvm_run->s.regs.pfs = env->pfault_select;
290 cs->kvm_run->s.regs.pfc = env->pfault_compare;
291 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
292 } else if (cap_async_pf) {
293 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
294 if (r < 0) {
295 return r;
296 }
297 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
298 if (r < 0) {
299 return r;
300 }
301 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
302 if (r < 0) {
303 return r;
304 }
305 }
306
307 /* access registers and control registers*/
308 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
309 for (i = 0; i < 16; i++) {
310 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
311 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
312 }
313 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
314 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
315 } else {
316 for (i = 0; i < 16; i++) {
317 sregs.acrs[i] = env->aregs[i];
318 sregs.crs[i] = env->cregs[i];
319 }
320 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
321 if (r < 0) {
322 return r;
323 }
324 }
325
326 /* Finally the prefix */
327 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
328 cs->kvm_run->s.regs.prefix = env->psa;
329 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
330 } else {
331 /* prefix is only supported via sync regs */
332 }
333 return 0;
334 }
335
336 int kvm_arch_get_registers(CPUState *cs)
337 {
338 S390CPU *cpu = S390_CPU(cs);
339 CPUS390XState *env = &cpu->env;
340 struct kvm_sregs sregs;
341 struct kvm_regs regs;
342 struct kvm_fpu fpu;
343 int i, r;
344
345 /* get the PSW */
346 env->psw.addr = cs->kvm_run->psw_addr;
347 env->psw.mask = cs->kvm_run->psw_mask;
348
349 /* the GPRS */
350 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
351 for (i = 0; i < 16; i++) {
352 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
353 }
354 } else {
355 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
356 if (r < 0) {
357 return r;
358 }
359 for (i = 0; i < 16; i++) {
360 env->regs[i] = regs.gprs[i];
361 }
362 }
363
364 /* The ACRS and CRS */
365 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
366 for (i = 0; i < 16; i++) {
367 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
368 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
369 }
370 } else {
371 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
372 if (r < 0) {
373 return r;
374 }
375 for (i = 0; i < 16; i++) {
376 env->aregs[i] = sregs.acrs[i];
377 env->cregs[i] = sregs.crs[i];
378 }
379 }
380
381 /* Floating point */
382 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
383 if (r < 0) {
384 return r;
385 }
386 for (i = 0; i < 16; i++) {
387 env->fregs[i].ll = fpu.fprs[i];
388 }
389 env->fpc = fpu.fpc;
390
391 /* The prefix */
392 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
393 env->psa = cs->kvm_run->s.regs.prefix;
394 }
395
396 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
397 env->cputm = cs->kvm_run->s.regs.cputm;
398 env->ckc = cs->kvm_run->s.regs.ckc;
399 env->todpr = cs->kvm_run->s.regs.todpr;
400 env->gbea = cs->kvm_run->s.regs.gbea;
401 env->pp = cs->kvm_run->s.regs.pp;
402 } else {
403 /*
404 * These ONE_REGS are not protected by a capability. As they are only
405 * necessary for migration we just trace a possible error, but don't
406 * return with an error return code.
407 */
408 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
409 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
410 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
411 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
412 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
413 }
414
415 /* pfault parameters */
416 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
417 env->pfault_token = cs->kvm_run->s.regs.pft;
418 env->pfault_select = cs->kvm_run->s.regs.pfs;
419 env->pfault_compare = cs->kvm_run->s.regs.pfc;
420 } else if (cap_async_pf) {
421 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
422 if (r < 0) {
423 return r;
424 }
425 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
426 if (r < 0) {
427 return r;
428 }
429 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
430 if (r < 0) {
431 return r;
432 }
433 }
434
435 return 0;
436 }
437
438 /*
439 * Legacy layout for s390:
440 * Older S390 KVM requires the topmost vma of the RAM to be
441 * smaller than an system defined value, which is at least 256GB.
442 * Larger systems have larger values. We put the guest between
443 * the end of data segment (system break) and this value. We
444 * use 32GB as a base to have enough room for the system break
445 * to grow. We also have to use MAP parameters that avoid
446 * read-only mapping of guest pages.
447 */
448 static void *legacy_s390_alloc(size_t size, uint64_t *align)
449 {
450 void *mem;
451
452 mem = mmap((void *) 0x800000000ULL, size,
453 PROT_EXEC|PROT_READ|PROT_WRITE,
454 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
455 return mem == MAP_FAILED ? NULL : mem;
456 }
457
458 /* DIAG 501 is used for sw breakpoints */
459 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
460
461 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
462 {
463
464 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
465 sizeof(diag_501), 0) ||
466 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
467 sizeof(diag_501), 1)) {
468 return -EINVAL;
469 }
470 return 0;
471 }
472
473 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
474 {
475 uint8_t t[sizeof(diag_501)];
476
477 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
478 return -EINVAL;
479 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
480 return -EINVAL;
481 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
482 sizeof(diag_501), 1)) {
483 return -EINVAL;
484 }
485
486 return 0;
487 }
488
489 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
490 int len, int type)
491 {
492 int n;
493
494 for (n = 0; n < nb_hw_breakpoints; n++) {
495 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
496 (hw_breakpoints[n].len == len || len == -1)) {
497 return &hw_breakpoints[n];
498 }
499 }
500
501 return NULL;
502 }
503
504 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
505 {
506 int size;
507
508 if (find_hw_breakpoint(addr, len, type)) {
509 return -EEXIST;
510 }
511
512 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
513
514 if (!hw_breakpoints) {
515 nb_hw_breakpoints = 0;
516 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
517 } else {
518 hw_breakpoints =
519 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
520 }
521
522 if (!hw_breakpoints) {
523 nb_hw_breakpoints = 0;
524 return -ENOMEM;
525 }
526
527 hw_breakpoints[nb_hw_breakpoints].addr = addr;
528 hw_breakpoints[nb_hw_breakpoints].len = len;
529 hw_breakpoints[nb_hw_breakpoints].type = type;
530
531 nb_hw_breakpoints++;
532
533 return 0;
534 }
535
536 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
537 target_ulong len, int type)
538 {
539 switch (type) {
540 case GDB_BREAKPOINT_HW:
541 type = KVM_HW_BP;
542 break;
543 case GDB_WATCHPOINT_WRITE:
544 if (len < 1) {
545 return -EINVAL;
546 }
547 type = KVM_HW_WP_WRITE;
548 break;
549 default:
550 return -ENOSYS;
551 }
552 return insert_hw_breakpoint(addr, len, type);
553 }
554
555 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
556 target_ulong len, int type)
557 {
558 int size;
559 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
560
561 if (bp == NULL) {
562 return -ENOENT;
563 }
564
565 nb_hw_breakpoints--;
566 if (nb_hw_breakpoints > 0) {
567 /*
568 * In order to trim the array, move the last element to the position to
569 * be removed - if necessary.
570 */
571 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
572 *bp = hw_breakpoints[nb_hw_breakpoints];
573 }
574 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
575 hw_breakpoints =
576 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
577 } else {
578 g_free(hw_breakpoints);
579 hw_breakpoints = NULL;
580 }
581
582 return 0;
583 }
584
585 void kvm_arch_remove_all_hw_breakpoints(void)
586 {
587 nb_hw_breakpoints = 0;
588 g_free(hw_breakpoints);
589 hw_breakpoints = NULL;
590 }
591
592 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
593 {
594 int i;
595
596 if (nb_hw_breakpoints > 0) {
597 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
598 dbg->arch.hw_bp = hw_breakpoints;
599
600 for (i = 0; i < nb_hw_breakpoints; ++i) {
601 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
602 hw_breakpoints[i].addr);
603 }
604 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
605 } else {
606 dbg->arch.nr_hw_bp = 0;
607 dbg->arch.hw_bp = NULL;
608 }
609 }
610
611 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
612 {
613 }
614
615 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
616 {
617 }
618
619 int kvm_arch_process_async_events(CPUState *cs)
620 {
621 return cs->halted;
622 }
623
624 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
625 struct kvm_s390_interrupt *interrupt)
626 {
627 int r = 0;
628
629 interrupt->type = irq->type;
630 switch (irq->type) {
631 case KVM_S390_INT_VIRTIO:
632 interrupt->parm = irq->u.ext.ext_params;
633 /* fall through */
634 case KVM_S390_INT_PFAULT_INIT:
635 case KVM_S390_INT_PFAULT_DONE:
636 interrupt->parm64 = irq->u.ext.ext_params2;
637 break;
638 case KVM_S390_PROGRAM_INT:
639 interrupt->parm = irq->u.pgm.code;
640 break;
641 case KVM_S390_SIGP_SET_PREFIX:
642 interrupt->parm = irq->u.prefix.address;
643 break;
644 case KVM_S390_INT_SERVICE:
645 interrupt->parm = irq->u.ext.ext_params;
646 break;
647 case KVM_S390_MCHK:
648 interrupt->parm = irq->u.mchk.cr14;
649 interrupt->parm64 = irq->u.mchk.mcic;
650 break;
651 case KVM_S390_INT_EXTERNAL_CALL:
652 interrupt->parm = irq->u.extcall.code;
653 break;
654 case KVM_S390_INT_EMERGENCY:
655 interrupt->parm = irq->u.emerg.code;
656 break;
657 case KVM_S390_SIGP_STOP:
658 case KVM_S390_RESTART:
659 break; /* These types have no parameters */
660 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
661 interrupt->parm = irq->u.io.subchannel_id << 16;
662 interrupt->parm |= irq->u.io.subchannel_nr;
663 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
664 interrupt->parm64 |= irq->u.io.io_int_word;
665 break;
666 default:
667 r = -EINVAL;
668 break;
669 }
670 return r;
671 }
672
673 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
674 {
675 struct kvm_s390_interrupt kvmint = {};
676 CPUState *cs = CPU(cpu);
677 int r;
678
679 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
680 if (r < 0) {
681 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
682 exit(1);
683 }
684
685 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
686 if (r < 0) {
687 fprintf(stderr, "KVM failed to inject interrupt\n");
688 exit(1);
689 }
690 }
691
692 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
693 {
694 struct kvm_s390_interrupt kvmint = {};
695 int r;
696
697 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
698 if (r < 0) {
699 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
700 exit(1);
701 }
702
703 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
704 if (r < 0) {
705 fprintf(stderr, "KVM failed to inject interrupt\n");
706 exit(1);
707 }
708 }
709
710 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
711 {
712 static bool use_flic = true;
713 int r;
714
715 if (use_flic) {
716 r = kvm_s390_inject_flic(irq);
717 if (r == -ENOSYS) {
718 use_flic = false;
719 }
720 if (!r) {
721 return;
722 }
723 }
724 __kvm_s390_floating_interrupt(irq);
725 }
726
727 void kvm_s390_virtio_irq(int config_change, uint64_t token)
728 {
729 struct kvm_s390_irq irq = {
730 .type = KVM_S390_INT_VIRTIO,
731 .u.ext.ext_params = config_change,
732 .u.ext.ext_params2 = token,
733 };
734
735 kvm_s390_floating_interrupt(&irq);
736 }
737
738 void kvm_s390_service_interrupt(uint32_t parm)
739 {
740 struct kvm_s390_irq irq = {
741 .type = KVM_S390_INT_SERVICE,
742 .u.ext.ext_params = parm,
743 };
744
745 kvm_s390_floating_interrupt(&irq);
746 }
747
748 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
749 {
750 struct kvm_s390_irq irq = {
751 .type = KVM_S390_PROGRAM_INT,
752 .u.pgm.code = code,
753 };
754
755 kvm_s390_vcpu_interrupt(cpu, &irq);
756 }
757
758 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
759 uint16_t ipbh0)
760 {
761 CPUS390XState *env = &cpu->env;
762 uint64_t sccb;
763 uint32_t code;
764 int r = 0;
765
766 cpu_synchronize_state(CPU(cpu));
767 sccb = env->regs[ipbh0 & 0xf];
768 code = env->regs[(ipbh0 & 0xf0) >> 4];
769
770 r = sclp_service_call(env, sccb, code);
771 if (r < 0) {
772 enter_pgmcheck(cpu, -r);
773 } else {
774 setcc(cpu, r);
775 }
776
777 return 0;
778 }
779
780 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
781 {
782 CPUS390XState *env = &cpu->env;
783 int rc = 0;
784 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
785
786 cpu_synchronize_state(CPU(cpu));
787
788 switch (ipa1) {
789 case PRIV_B2_XSCH:
790 ioinst_handle_xsch(cpu, env->regs[1]);
791 break;
792 case PRIV_B2_CSCH:
793 ioinst_handle_csch(cpu, env->regs[1]);
794 break;
795 case PRIV_B2_HSCH:
796 ioinst_handle_hsch(cpu, env->regs[1]);
797 break;
798 case PRIV_B2_MSCH:
799 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
800 break;
801 case PRIV_B2_SSCH:
802 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
803 break;
804 case PRIV_B2_STCRW:
805 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
806 break;
807 case PRIV_B2_STSCH:
808 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
809 break;
810 case PRIV_B2_TSCH:
811 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
812 fprintf(stderr, "Spurious tsch intercept\n");
813 break;
814 case PRIV_B2_CHSC:
815 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
816 break;
817 case PRIV_B2_TPI:
818 /* This should have been handled by kvm already. */
819 fprintf(stderr, "Spurious tpi intercept\n");
820 break;
821 case PRIV_B2_SCHM:
822 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
823 run->s390_sieic.ipb);
824 break;
825 case PRIV_B2_RSCH:
826 ioinst_handle_rsch(cpu, env->regs[1]);
827 break;
828 case PRIV_B2_RCHP:
829 ioinst_handle_rchp(cpu, env->regs[1]);
830 break;
831 case PRIV_B2_STCPS:
832 /* We do not provide this instruction, it is suppressed. */
833 break;
834 case PRIV_B2_SAL:
835 ioinst_handle_sal(cpu, env->regs[1]);
836 break;
837 case PRIV_B2_SIGA:
838 /* Not provided, set CC = 3 for subchannel not operational */
839 setcc(cpu, 3);
840 break;
841 case PRIV_B2_SCLP_CALL:
842 rc = kvm_sclp_service_call(cpu, run, ipbh0);
843 break;
844 default:
845 rc = -1;
846 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
847 break;
848 }
849
850 return rc;
851 }
852
853 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run)
854 {
855 CPUS390XState *env = &cpu->env;
856 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
857 uint32_t base2 = run->s390_sieic.ipb >> 28;
858 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
859 ((run->s390_sieic.ipb & 0xff00) << 4);
860
861 if (disp2 & 0x80000) {
862 disp2 += 0xfff00000;
863 }
864
865 return (base2 ? env->regs[base2] : 0) +
866 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
867 }
868
869 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run)
870 {
871 CPUS390XState *env = &cpu->env;
872 uint32_t base2 = run->s390_sieic.ipb >> 28;
873 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
874 ((run->s390_sieic.ipb & 0xff00) << 4);
875
876 if (disp2 & 0x80000) {
877 disp2 += 0xfff00000;
878 }
879
880 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
881 }
882
883 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
884 {
885 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
886
887 return clp_service_call(cpu, r2);
888 }
889
890 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
891 {
892 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
893 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
894
895 return pcilg_service_call(cpu, r1, r2);
896 }
897
898 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
899 {
900 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
901 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
902
903 return pcistg_service_call(cpu, r1, r2);
904 }
905
906 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
907 {
908 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
909 uint64_t fiba;
910
911 cpu_synchronize_state(CPU(cpu));
912 fiba = get_base_disp_rxy(cpu, run);
913
914 return stpcifc_service_call(cpu, r1, fiba);
915 }
916
917 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
918 {
919 /* NOOP */
920 return 0;
921 }
922
923 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
924 {
925 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
926 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
927
928 return rpcit_service_call(cpu, r1, r2);
929 }
930
931 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
932 {
933 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
934 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
935 uint64_t gaddr;
936
937 cpu_synchronize_state(CPU(cpu));
938 gaddr = get_base_disp_rsy(cpu, run);
939
940 return pcistb_service_call(cpu, r1, r3, gaddr);
941 }
942
943 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
944 {
945 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
946 uint64_t fiba;
947
948 cpu_synchronize_state(CPU(cpu));
949 fiba = get_base_disp_rxy(cpu, run);
950
951 return mpcifc_service_call(cpu, r1, fiba);
952 }
953
954 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
955 {
956 int r = 0;
957
958 switch (ipa1) {
959 case PRIV_B9_CLP:
960 r = kvm_clp_service_call(cpu, run);
961 break;
962 case PRIV_B9_PCISTG:
963 r = kvm_pcistg_service_call(cpu, run);
964 break;
965 case PRIV_B9_PCILG:
966 r = kvm_pcilg_service_call(cpu, run);
967 break;
968 case PRIV_B9_RPCIT:
969 r = kvm_rpcit_service_call(cpu, run);
970 break;
971 case PRIV_B9_EQBS:
972 /* just inject exception */
973 r = -1;
974 break;
975 default:
976 r = -1;
977 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
978 break;
979 }
980
981 return r;
982 }
983
984 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
985 {
986 int r = 0;
987
988 switch (ipbl) {
989 case PRIV_EB_PCISTB:
990 r = kvm_pcistb_service_call(cpu, run);
991 break;
992 case PRIV_EB_SIC:
993 r = kvm_sic_service_call(cpu, run);
994 break;
995 case PRIV_EB_SQBS:
996 /* just inject exception */
997 r = -1;
998 break;
999 default:
1000 r = -1;
1001 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1002 break;
1003 }
1004
1005 return r;
1006 }
1007
1008 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1009 {
1010 int r = 0;
1011
1012 switch (ipbl) {
1013 case PRIV_E3_MPCIFC:
1014 r = kvm_mpcifc_service_call(cpu, run);
1015 break;
1016 case PRIV_E3_STPCIFC:
1017 r = kvm_stpcifc_service_call(cpu, run);
1018 break;
1019 default:
1020 r = -1;
1021 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1022 break;
1023 }
1024
1025 return r;
1026 }
1027
1028 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1029 {
1030 CPUS390XState *env = &cpu->env;
1031 int ret;
1032
1033 cpu_synchronize_state(CPU(cpu));
1034 ret = s390_virtio_hypercall(env);
1035 if (ret == -EINVAL) {
1036 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1037 return 0;
1038 }
1039
1040 return ret;
1041 }
1042
1043 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1044 {
1045 uint64_t r1, r3;
1046
1047 cpu_synchronize_state(CPU(cpu));
1048 r1 = (run->s390_sieic.ipa & 0x00f0) >> 8;
1049 r3 = run->s390_sieic.ipa & 0x000f;
1050 handle_diag_308(&cpu->env, r1, r3);
1051 }
1052
1053 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1054 {
1055 CPUS390XState *env = &cpu->env;
1056 unsigned long pc;
1057
1058 cpu_synchronize_state(CPU(cpu));
1059
1060 pc = env->psw.addr - 4;
1061 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1062 env->psw.addr = pc;
1063 return EXCP_DEBUG;
1064 }
1065
1066 return -ENOENT;
1067 }
1068
1069 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1070
1071 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1072 {
1073 int r = 0;
1074 uint16_t func_code;
1075
1076 /*
1077 * For any diagnose call we support, bits 48-63 of the resulting
1078 * address specify the function code; the remainder is ignored.
1079 */
1080 func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
1081 switch (func_code) {
1082 case DIAG_IPL:
1083 kvm_handle_diag_308(cpu, run);
1084 break;
1085 case DIAG_KVM_HYPERCALL:
1086 r = handle_hypercall(cpu, run);
1087 break;
1088 case DIAG_KVM_BREAKPOINT:
1089 r = handle_sw_breakpoint(cpu, run);
1090 break;
1091 default:
1092 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1093 r = -1;
1094 break;
1095 }
1096
1097 return r;
1098 }
1099
1100 static void sigp_cpu_start(void *arg)
1101 {
1102 CPUState *cs = arg;
1103 S390CPU *cpu = S390_CPU(cs);
1104
1105 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1106 DPRINTF("DONE: KVM cpu start: %p\n", &cpu->env);
1107 }
1108
1109 static void sigp_cpu_restart(void *arg)
1110 {
1111 CPUState *cs = arg;
1112 S390CPU *cpu = S390_CPU(cs);
1113 struct kvm_s390_irq irq = {
1114 .type = KVM_S390_RESTART,
1115 };
1116
1117 kvm_s390_vcpu_interrupt(cpu, &irq);
1118 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1119 }
1120
1121 int kvm_s390_cpu_restart(S390CPU *cpu)
1122 {
1123 run_on_cpu(CPU(cpu), sigp_cpu_restart, CPU(cpu));
1124 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1125 return 0;
1126 }
1127
1128 static void sigp_initial_cpu_reset(void *arg)
1129 {
1130 CPUState *cpu = arg;
1131 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1132
1133 cpu_synchronize_state(cpu);
1134 scc->initial_cpu_reset(cpu);
1135 cpu_synchronize_post_reset(cpu);
1136 }
1137
1138 static void sigp_cpu_reset(void *arg)
1139 {
1140 CPUState *cpu = arg;
1141 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1142
1143 cpu_synchronize_state(cpu);
1144 scc->cpu_reset(cpu);
1145 cpu_synchronize_post_reset(cpu);
1146 }
1147
1148 #define SIGP_ORDER_MASK 0x000000ff
1149
1150 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1151 {
1152 CPUS390XState *env = &cpu->env;
1153 uint8_t order_code;
1154 uint16_t cpu_addr;
1155 S390CPU *target_cpu;
1156 uint64_t *statusreg = &env->regs[ipa1 >> 4];
1157 int cc;
1158
1159 cpu_synchronize_state(CPU(cpu));
1160
1161 /* get order code */
1162 order_code = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
1163
1164 cpu_addr = env->regs[ipa1 & 0x0f];
1165 target_cpu = s390_cpu_addr2state(cpu_addr);
1166 if (target_cpu == NULL) {
1167 cc = 3; /* not operational */
1168 goto out;
1169 }
1170
1171 switch (order_code) {
1172 case SIGP_START:
1173 run_on_cpu(CPU(target_cpu), sigp_cpu_start, CPU(target_cpu));
1174 cc = 0;
1175 break;
1176 case SIGP_RESTART:
1177 run_on_cpu(CPU(target_cpu), sigp_cpu_restart, CPU(target_cpu));
1178 cc = 0;
1179 break;
1180 case SIGP_SET_ARCH:
1181 *statusreg &= 0xffffffff00000000UL;
1182 *statusreg |= SIGP_STAT_INVALID_PARAMETER;
1183 cc = 1; /* status stored */
1184 break;
1185 case SIGP_INITIAL_CPU_RESET:
1186 run_on_cpu(CPU(target_cpu), sigp_initial_cpu_reset, CPU(target_cpu));
1187 cc = 0;
1188 break;
1189 case SIGP_CPU_RESET:
1190 run_on_cpu(CPU(target_cpu), sigp_cpu_reset, CPU(target_cpu));
1191 cc = 0;
1192 break;
1193 default:
1194 DPRINTF("KVM: unknown SIGP: 0x%x\n", order_code);
1195 *statusreg &= 0xffffffff00000000UL;
1196 *statusreg |= SIGP_STAT_INVALID_ORDER;
1197 cc = 1; /* status stored */
1198 break;
1199 }
1200
1201 out:
1202 setcc(cpu, cc);
1203 return 0;
1204 }
1205
1206 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1207 {
1208 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1209 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1210 int r = -1;
1211
1212 DPRINTF("handle_instruction 0x%x 0x%x\n",
1213 run->s390_sieic.ipa, run->s390_sieic.ipb);
1214 switch (ipa0) {
1215 case IPA0_B2:
1216 r = handle_b2(cpu, run, ipa1);
1217 break;
1218 case IPA0_B9:
1219 r = handle_b9(cpu, run, ipa1);
1220 break;
1221 case IPA0_EB:
1222 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1223 break;
1224 case IPA0_E3:
1225 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1226 break;
1227 case IPA0_DIAG:
1228 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1229 break;
1230 case IPA0_SIGP:
1231 r = handle_sigp(cpu, run, ipa1);
1232 break;
1233 }
1234
1235 if (r < 0) {
1236 r = 0;
1237 enter_pgmcheck(cpu, 0x0001);
1238 }
1239
1240 return r;
1241 }
1242
1243 static bool is_special_wait_psw(CPUState *cs)
1244 {
1245 /* signal quiesce */
1246 return cs->kvm_run->psw_addr == 0xfffUL;
1247 }
1248
1249 static void guest_panicked(void)
1250 {
1251 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1252 &error_abort);
1253 vm_stop(RUN_STATE_GUEST_PANICKED);
1254 }
1255
1256 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1257 {
1258 CPUState *cs = CPU(cpu);
1259
1260 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1261 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1262 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1263 s390_cpu_halt(cpu);
1264 guest_panicked();
1265 }
1266
1267 static int handle_intercept(S390CPU *cpu)
1268 {
1269 CPUState *cs = CPU(cpu);
1270 struct kvm_run *run = cs->kvm_run;
1271 int icpt_code = run->s390_sieic.icptcode;
1272 int r = 0;
1273
1274 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1275 (long)cs->kvm_run->psw_addr);
1276 switch (icpt_code) {
1277 case ICPT_INSTRUCTION:
1278 r = handle_instruction(cpu, run);
1279 break;
1280 case ICPT_PROGRAM:
1281 unmanageable_intercept(cpu, "program interrupt",
1282 offsetof(LowCore, program_new_psw));
1283 r = EXCP_HALTED;
1284 break;
1285 case ICPT_EXT_INT:
1286 unmanageable_intercept(cpu, "external interrupt",
1287 offsetof(LowCore, external_new_psw));
1288 r = EXCP_HALTED;
1289 break;
1290 case ICPT_WAITPSW:
1291 /* disabled wait, since enabled wait is handled in kernel */
1292 cpu_synchronize_state(cs);
1293 if (s390_cpu_halt(cpu) == 0) {
1294 if (is_special_wait_psw(cs)) {
1295 qemu_system_shutdown_request();
1296 } else {
1297 guest_panicked();
1298 }
1299 }
1300 r = EXCP_HALTED;
1301 break;
1302 case ICPT_CPU_STOP:
1303 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1304 qemu_system_shutdown_request();
1305 }
1306 r = EXCP_HALTED;
1307 break;
1308 case ICPT_SOFT_INTERCEPT:
1309 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1310 exit(1);
1311 break;
1312 case ICPT_IO:
1313 fprintf(stderr, "KVM unimplemented icpt IO\n");
1314 exit(1);
1315 break;
1316 default:
1317 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1318 exit(1);
1319 break;
1320 }
1321
1322 return r;
1323 }
1324
1325 static int handle_tsch(S390CPU *cpu)
1326 {
1327 CPUS390XState *env = &cpu->env;
1328 CPUState *cs = CPU(cpu);
1329 struct kvm_run *run = cs->kvm_run;
1330 int ret;
1331
1332 cpu_synchronize_state(cs);
1333
1334 ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
1335 if (ret >= 0) {
1336 /* Success; set condition code. */
1337 setcc(cpu, ret);
1338 ret = 0;
1339 } else if (ret < -1) {
1340 /*
1341 * Failure.
1342 * If an I/O interrupt had been dequeued, we have to reinject it.
1343 */
1344 if (run->s390_tsch.dequeued) {
1345 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1346 run->s390_tsch.subchannel_nr,
1347 run->s390_tsch.io_int_parm,
1348 run->s390_tsch.io_int_word);
1349 }
1350 ret = 0;
1351 }
1352 return ret;
1353 }
1354
1355 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1356 {
1357 CPUState *cs = CPU(cpu);
1358 struct kvm_run *run = cs->kvm_run;
1359
1360 int ret = 0;
1361 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1362
1363 switch (arch_info->type) {
1364 case KVM_HW_WP_WRITE:
1365 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1366 cs->watchpoint_hit = &hw_watchpoint;
1367 hw_watchpoint.vaddr = arch_info->addr;
1368 hw_watchpoint.flags = BP_MEM_WRITE;
1369 ret = EXCP_DEBUG;
1370 }
1371 break;
1372 case KVM_HW_BP:
1373 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1374 ret = EXCP_DEBUG;
1375 }
1376 break;
1377 case KVM_SINGLESTEP:
1378 if (cs->singlestep_enabled) {
1379 ret = EXCP_DEBUG;
1380 }
1381 break;
1382 default:
1383 ret = -ENOSYS;
1384 }
1385
1386 return ret;
1387 }
1388
1389 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1390 {
1391 S390CPU *cpu = S390_CPU(cs);
1392 int ret = 0;
1393
1394 switch (run->exit_reason) {
1395 case KVM_EXIT_S390_SIEIC:
1396 ret = handle_intercept(cpu);
1397 break;
1398 case KVM_EXIT_S390_RESET:
1399 qemu_system_reset_request();
1400 break;
1401 case KVM_EXIT_S390_TSCH:
1402 ret = handle_tsch(cpu);
1403 break;
1404 case KVM_EXIT_DEBUG:
1405 ret = kvm_arch_handle_debug_exit(cpu);
1406 break;
1407 default:
1408 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1409 break;
1410 }
1411
1412 if (ret == 0) {
1413 ret = EXCP_INTERRUPT;
1414 }
1415 return ret;
1416 }
1417
1418 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1419 {
1420 return true;
1421 }
1422
1423 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
1424 {
1425 return 1;
1426 }
1427
1428 int kvm_arch_on_sigbus(int code, void *addr)
1429 {
1430 return 1;
1431 }
1432
1433 void kvm_s390_io_interrupt(uint16_t subchannel_id,
1434 uint16_t subchannel_nr, uint32_t io_int_parm,
1435 uint32_t io_int_word)
1436 {
1437 struct kvm_s390_irq irq = {
1438 .u.io.subchannel_id = subchannel_id,
1439 .u.io.subchannel_nr = subchannel_nr,
1440 .u.io.io_int_parm = io_int_parm,
1441 .u.io.io_int_word = io_int_word,
1442 };
1443
1444 if (io_int_word & IO_INT_WORD_AI) {
1445 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
1446 } else {
1447 irq.type = ((subchannel_id & 0xff00) << 24) |
1448 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
1449 }
1450 kvm_s390_floating_interrupt(&irq);
1451 }
1452
1453 void kvm_s390_crw_mchk(void)
1454 {
1455 struct kvm_s390_irq irq = {
1456 .type = KVM_S390_MCHK,
1457 .u.mchk.cr14 = 1 << 28,
1458 .u.mchk.mcic = 0x00400f1d40330000ULL,
1459 };
1460 kvm_s390_floating_interrupt(&irq);
1461 }
1462
1463 void kvm_s390_enable_css_support(S390CPU *cpu)
1464 {
1465 int r;
1466
1467 /* Activate host kernel channel subsystem support. */
1468 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1469 assert(r == 0);
1470 }
1471
1472 void kvm_arch_init_irq_routing(KVMState *s)
1473 {
1474 /*
1475 * Note that while irqchip capabilities generally imply that cpustates
1476 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1477 * have to override the common code kvm_halt_in_kernel_allowed setting.
1478 */
1479 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1480 kvm_gsi_routing_allowed = true;
1481 kvm_halt_in_kernel_allowed = false;
1482 }
1483 }
1484
1485 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1486 int vq, bool assign)
1487 {
1488 struct kvm_ioeventfd kick = {
1489 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1490 KVM_IOEVENTFD_FLAG_DATAMATCH,
1491 .fd = event_notifier_get_fd(notifier),
1492 .datamatch = vq,
1493 .addr = sch,
1494 .len = 8,
1495 };
1496 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1497 return -ENOSYS;
1498 }
1499 if (!assign) {
1500 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1501 }
1502 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1503 }
1504
1505 int kvm_s390_get_memslot_count(KVMState *s)
1506 {
1507 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
1508 }
1509
1510 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1511 {
1512 struct kvm_mp_state mp_state = {};
1513 int ret;
1514
1515 /* the kvm part might not have been initialized yet */
1516 if (CPU(cpu)->kvm_state == NULL) {
1517 return 0;
1518 }
1519
1520 switch (cpu_state) {
1521 case CPU_STATE_STOPPED:
1522 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1523 break;
1524 case CPU_STATE_CHECK_STOP:
1525 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1526 break;
1527 case CPU_STATE_OPERATING:
1528 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1529 break;
1530 case CPU_STATE_LOAD:
1531 mp_state.mp_state = KVM_MP_STATE_LOAD;
1532 break;
1533 default:
1534 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1535 cpu_state);
1536 exit(1);
1537 }
1538
1539 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1540 if (ret) {
1541 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1542 strerror(-ret));
1543 }
1544
1545 return ret;
1546 }