]> git.proxmox.com Git - qemu.git/blob - target-s390x/kvm.c
s390: Wire up channel I/O in kvm.
[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 "cpu.h"
36 #include "sysemu/device_tree.h"
37
38 /* #define DEBUG_KVM */
39
40 #ifdef DEBUG_KVM
41 #define dprintf(fmt, ...) \
42 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
43 #else
44 #define dprintf(fmt, ...) \
45 do { } while (0)
46 #endif
47
48 #define IPA0_DIAG 0x8300
49 #define IPA0_SIGP 0xae00
50 #define IPA0_B2 0xb200
51 #define IPA0_B9 0xb900
52 #define IPA0_EB 0xeb00
53
54 #define PRIV_SCLP_CALL 0x20
55 #define PRIV_CSCH 0x30
56 #define PRIV_HSCH 0x31
57 #define PRIV_MSCH 0x32
58 #define PRIV_SSCH 0x33
59 #define PRIV_STSCH 0x34
60 #define PRIV_TSCH 0x35
61 #define PRIV_TPI 0x36
62 #define PRIV_SAL 0x37
63 #define PRIV_RSCH 0x38
64 #define PRIV_STCRW 0x39
65 #define PRIV_STCPS 0x3a
66 #define PRIV_RCHP 0x3b
67 #define PRIV_SCHM 0x3c
68 #define PRIV_CHSC 0x5f
69 #define PRIV_SIGA 0x74
70 #define PRIV_XSCH 0x76
71 #define PRIV_SQBS 0x8a
72 #define PRIV_EQBS 0x9c
73 #define DIAG_KVM_HYPERCALL 0x500
74 #define DIAG_KVM_BREAKPOINT 0x501
75
76 #define ICPT_INSTRUCTION 0x04
77 #define ICPT_WAITPSW 0x1c
78 #define ICPT_SOFT_INTERCEPT 0x24
79 #define ICPT_CPU_STOP 0x28
80 #define ICPT_IO 0x40
81
82 #define SIGP_RESTART 0x06
83 #define SIGP_INITIAL_CPU_RESET 0x0b
84 #define SIGP_STORE_STATUS_ADDR 0x0e
85 #define SIGP_SET_ARCH 0x12
86
87 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
88 KVM_CAP_LAST_INFO
89 };
90
91 static int cap_sync_regs;
92
93 int kvm_arch_init(KVMState *s)
94 {
95 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
96 return 0;
97 }
98
99 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
100 {
101 return cpu->cpu_index;
102 }
103
104 int kvm_arch_init_vcpu(CPUState *cpu)
105 {
106 int ret = 0;
107
108 if (kvm_vcpu_ioctl(cpu, KVM_S390_INITIAL_RESET, NULL) < 0) {
109 perror("cannot init reset vcpu");
110 }
111
112 return ret;
113 }
114
115 void kvm_arch_reset_vcpu(CPUState *cpu)
116 {
117 /* The initial reset call is needed here to reset in-kernel
118 * vcpu data that we can't access directly from QEMU
119 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
120 * Before this ioctl cpu_synchronize_state() is called in common kvm
121 * code (kvm-all) */
122 if (kvm_vcpu_ioctl(cpu, KVM_S390_INITIAL_RESET, NULL)) {
123 perror("Can't reset vcpu\n");
124 }
125 }
126
127 int kvm_arch_put_registers(CPUState *cs, int level)
128 {
129 S390CPU *cpu = S390_CPU(cs);
130 CPUS390XState *env = &cpu->env;
131 struct kvm_sregs sregs;
132 struct kvm_regs regs;
133 int ret;
134 int i;
135
136 /* always save the PSW and the GPRS*/
137 cs->kvm_run->psw_addr = env->psw.addr;
138 cs->kvm_run->psw_mask = env->psw.mask;
139
140 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
141 for (i = 0; i < 16; i++) {
142 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
143 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
144 }
145 } else {
146 for (i = 0; i < 16; i++) {
147 regs.gprs[i] = env->regs[i];
148 }
149 ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
150 if (ret < 0) {
151 return ret;
152 }
153 }
154
155 /* Do we need to save more than that? */
156 if (level == KVM_PUT_RUNTIME_STATE) {
157 return 0;
158 }
159
160 if (cap_sync_regs &&
161 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
162 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
163 for (i = 0; i < 16; i++) {
164 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
165 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
166 }
167 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
168 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
169 } else {
170 for (i = 0; i < 16; i++) {
171 sregs.acrs[i] = env->aregs[i];
172 sregs.crs[i] = env->cregs[i];
173 }
174 ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
175 if (ret < 0) {
176 return ret;
177 }
178 }
179
180 /* Finally the prefix */
181 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
182 cs->kvm_run->s.regs.prefix = env->psa;
183 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
184 } else {
185 /* prefix is only supported via sync regs */
186 }
187 return 0;
188 }
189
190 int kvm_arch_get_registers(CPUState *cs)
191 {
192 S390CPU *cpu = S390_CPU(cs);
193 CPUS390XState *env = &cpu->env;
194 struct kvm_sregs sregs;
195 struct kvm_regs regs;
196 int ret;
197 int i;
198
199 /* get the PSW */
200 env->psw.addr = cs->kvm_run->psw_addr;
201 env->psw.mask = cs->kvm_run->psw_mask;
202
203 /* the GPRS */
204 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
205 for (i = 0; i < 16; i++) {
206 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
207 }
208 } else {
209 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
210 if (ret < 0) {
211 return ret;
212 }
213 for (i = 0; i < 16; i++) {
214 env->regs[i] = regs.gprs[i];
215 }
216 }
217
218 /* The ACRS and CRS */
219 if (cap_sync_regs &&
220 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
221 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
222 for (i = 0; i < 16; i++) {
223 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
224 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
225 }
226 } else {
227 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
228 if (ret < 0) {
229 return ret;
230 }
231 for (i = 0; i < 16; i++) {
232 env->aregs[i] = sregs.acrs[i];
233 env->cregs[i] = sregs.crs[i];
234 }
235 }
236
237 /* Finally the prefix */
238 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
239 env->psa = cs->kvm_run->s.regs.prefix;
240 } else {
241 /* no prefix without sync regs */
242 }
243
244 return 0;
245 }
246
247 /*
248 * Legacy layout for s390:
249 * Older S390 KVM requires the topmost vma of the RAM to be
250 * smaller than an system defined value, which is at least 256GB.
251 * Larger systems have larger values. We put the guest between
252 * the end of data segment (system break) and this value. We
253 * use 32GB as a base to have enough room for the system break
254 * to grow. We also have to use MAP parameters that avoid
255 * read-only mapping of guest pages.
256 */
257 static void *legacy_s390_alloc(ram_addr_t size)
258 {
259 void *mem;
260
261 mem = mmap((void *) 0x800000000ULL, size,
262 PROT_EXEC|PROT_READ|PROT_WRITE,
263 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
264 if (mem == MAP_FAILED) {
265 fprintf(stderr, "Allocating RAM failed\n");
266 abort();
267 }
268 return mem;
269 }
270
271 void *kvm_arch_vmalloc(ram_addr_t size)
272 {
273 /* Can we use the standard allocation ? */
274 if (kvm_check_extension(kvm_state, KVM_CAP_S390_GMAP) &&
275 kvm_check_extension(kvm_state, KVM_CAP_S390_COW)) {
276 return NULL;
277 } else {
278 return legacy_s390_alloc(size);
279 }
280 }
281
282 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
283 {
284 S390CPU *cpu = S390_CPU(cs);
285 CPUS390XState *env = &cpu->env;
286 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
287
288 if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
289 cpu_memory_rw_debug(env, bp->pc, (uint8_t *)diag_501, 4, 1)) {
290 return -EINVAL;
291 }
292 return 0;
293 }
294
295 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
296 {
297 S390CPU *cpu = S390_CPU(cs);
298 CPUS390XState *env = &cpu->env;
299 uint8_t t[4];
300 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
301
302 if (cpu_memory_rw_debug(env, bp->pc, t, 4, 0)) {
303 return -EINVAL;
304 } else if (memcmp(t, diag_501, 4)) {
305 return -EINVAL;
306 } else if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
307 return -EINVAL;
308 }
309
310 return 0;
311 }
312
313 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
314 {
315 }
316
317 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
318 {
319 }
320
321 int kvm_arch_process_async_events(CPUState *cs)
322 {
323 S390CPU *cpu = S390_CPU(cs);
324 return cpu->env.halted;
325 }
326
327 void kvm_s390_interrupt_internal(S390CPU *cpu, int type, uint32_t parm,
328 uint64_t parm64, int vm)
329 {
330 CPUState *cs = CPU(cpu);
331 struct kvm_s390_interrupt kvmint;
332 int r;
333
334 if (!cs->kvm_state) {
335 return;
336 }
337
338 kvmint.type = type;
339 kvmint.parm = parm;
340 kvmint.parm64 = parm64;
341
342 if (vm) {
343 r = kvm_vm_ioctl(cs->kvm_state, KVM_S390_INTERRUPT, &kvmint);
344 } else {
345 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
346 }
347
348 if (r < 0) {
349 fprintf(stderr, "KVM failed to inject interrupt\n");
350 exit(1);
351 }
352 }
353
354 void kvm_s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token)
355 {
356 kvm_s390_interrupt_internal(cpu, KVM_S390_INT_VIRTIO, config_change,
357 token, 1);
358 }
359
360 void kvm_s390_interrupt(S390CPU *cpu, int type, uint32_t code)
361 {
362 kvm_s390_interrupt_internal(cpu, type, code, 0, 0);
363 }
364
365 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
366 {
367 kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code);
368 }
369
370 static inline void setcc(S390CPU *cpu, uint64_t cc)
371 {
372 CPUS390XState *env = &cpu->env;
373 CPUState *cs = CPU(cpu);
374
375 cs->kvm_run->psw_mask &= ~(3ull << 44);
376 cs->kvm_run->psw_mask |= (cc & 3) << 44;
377
378 env->psw.mask &= ~(3ul << 44);
379 env->psw.mask |= (cc & 3) << 44;
380 }
381
382 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
383 uint16_t ipbh0)
384 {
385 CPUS390XState *env = &cpu->env;
386 uint32_t sccb;
387 uint64_t code;
388 int r = 0;
389
390 cpu_synchronize_state(env);
391 sccb = env->regs[ipbh0 & 0xf];
392 code = env->regs[(ipbh0 & 0xf0) >> 4];
393
394 r = sclp_service_call(sccb, code);
395 if (r < 0) {
396 enter_pgmcheck(cpu, -r);
397 }
398 setcc(cpu, r);
399
400 return 0;
401 }
402
403 static int kvm_handle_css_inst(S390CPU *cpu, struct kvm_run *run,
404 uint8_t ipa0, uint8_t ipa1, uint8_t ipb)
405 {
406 int r = 0;
407 int no_cc = 0;
408 CPUS390XState *env = &cpu->env;
409
410 if (ipa0 != 0xb2) {
411 /* Not handled for now. */
412 return -1;
413 }
414 cpu_synchronize_state(env);
415 switch (ipa1) {
416 case PRIV_XSCH:
417 r = ioinst_handle_xsch(env, env->regs[1]);
418 break;
419 case PRIV_CSCH:
420 r = ioinst_handle_csch(env, env->regs[1]);
421 break;
422 case PRIV_HSCH:
423 r = ioinst_handle_hsch(env, env->regs[1]);
424 break;
425 case PRIV_MSCH:
426 r = ioinst_handle_msch(env, env->regs[1], run->s390_sieic.ipb);
427 break;
428 case PRIV_SSCH:
429 r = ioinst_handle_ssch(env, env->regs[1], run->s390_sieic.ipb);
430 break;
431 case PRIV_STCRW:
432 r = ioinst_handle_stcrw(env, run->s390_sieic.ipb);
433 break;
434 case PRIV_STSCH:
435 r = ioinst_handle_stsch(env, env->regs[1], run->s390_sieic.ipb);
436 break;
437 case PRIV_TSCH:
438 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
439 fprintf(stderr, "Spurious tsch intercept\n");
440 break;
441 case PRIV_CHSC:
442 r = ioinst_handle_chsc(env, run->s390_sieic.ipb);
443 break;
444 case PRIV_TPI:
445 /* This should have been handled by kvm already. */
446 fprintf(stderr, "Spurious tpi intercept\n");
447 break;
448 case PRIV_SCHM:
449 no_cc = 1;
450 r = ioinst_handle_schm(env, env->regs[1], env->regs[2],
451 run->s390_sieic.ipb);
452 break;
453 case PRIV_RSCH:
454 r = ioinst_handle_rsch(env, env->regs[1]);
455 break;
456 case PRIV_RCHP:
457 r = ioinst_handle_rchp(env, env->regs[1]);
458 break;
459 case PRIV_STCPS:
460 /* We do not provide this instruction, it is suppressed. */
461 no_cc = 1;
462 r = 0;
463 break;
464 case PRIV_SAL:
465 no_cc = 1;
466 r = ioinst_handle_sal(env, env->regs[1]);
467 break;
468 default:
469 r = -1;
470 break;
471 }
472
473 if (r >= 0) {
474 if (!no_cc) {
475 setcc(cpu, r);
476 }
477 r = 0;
478 } else if (r < -1) {
479 r = 0;
480 }
481 return r;
482 }
483
484 static int is_ioinst(uint8_t ipa0, uint8_t ipa1, uint8_t ipb)
485 {
486 int ret = 0;
487 uint16_t ipa = (ipa0 << 8) | ipa1;
488
489 switch (ipa) {
490 case IPA0_B2 | PRIV_CSCH:
491 case IPA0_B2 | PRIV_HSCH:
492 case IPA0_B2 | PRIV_MSCH:
493 case IPA0_B2 | PRIV_SSCH:
494 case IPA0_B2 | PRIV_STSCH:
495 case IPA0_B2 | PRIV_TPI:
496 case IPA0_B2 | PRIV_SAL:
497 case IPA0_B2 | PRIV_RSCH:
498 case IPA0_B2 | PRIV_STCRW:
499 case IPA0_B2 | PRIV_STCPS:
500 case IPA0_B2 | PRIV_RCHP:
501 case IPA0_B2 | PRIV_SCHM:
502 case IPA0_B2 | PRIV_CHSC:
503 case IPA0_B2 | PRIV_SIGA:
504 case IPA0_B2 | PRIV_XSCH:
505 case IPA0_B9 | PRIV_EQBS:
506 case IPA0_EB | PRIV_SQBS:
507 ret = 1;
508 break;
509 }
510
511 return ret;
512 }
513
514 static int handle_priv(S390CPU *cpu, struct kvm_run *run,
515 uint8_t ipa0, uint8_t ipa1)
516 {
517 int r = 0;
518 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
519 uint8_t ipb = run->s390_sieic.ipb & 0xff;
520
521 dprintf("KVM: PRIV: %d\n", ipa1);
522 switch (ipa1) {
523 case PRIV_SCLP_CALL:
524 r = kvm_sclp_service_call(cpu, run, ipbh0);
525 break;
526 default:
527 if (is_ioinst(ipa0, ipa1, ipb)) {
528 r = kvm_handle_css_inst(cpu, run, ipa0, ipa1, ipb);
529 if (r == -1) {
530 setcc(cpu, 3);
531 r = 0;
532 }
533 } else {
534 dprintf("KVM: unknown PRIV: 0x%x\n", ipa1);
535 r = -1;
536 }
537 break;
538 }
539
540 return r;
541 }
542
543 static int handle_hypercall(CPUS390XState *env, struct kvm_run *run)
544 {
545 cpu_synchronize_state(env);
546 env->regs[2] = s390_virtio_hypercall(env);
547
548 return 0;
549 }
550
551 static int handle_diag(CPUS390XState *env, struct kvm_run *run, int ipb_code)
552 {
553 int r = 0;
554
555 switch (ipb_code) {
556 case DIAG_KVM_HYPERCALL:
557 r = handle_hypercall(env, run);
558 break;
559 case DIAG_KVM_BREAKPOINT:
560 sleep(10);
561 break;
562 default:
563 dprintf("KVM: unknown DIAG: 0x%x\n", ipb_code);
564 r = -1;
565 break;
566 }
567
568 return r;
569 }
570
571 static int s390_cpu_restart(S390CPU *cpu)
572 {
573 CPUS390XState *env = &cpu->env;
574
575 kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0);
576 s390_add_running_cpu(env);
577 qemu_cpu_kick(CPU(cpu));
578 dprintf("DONE: SIGP cpu restart: %p\n", env);
579 return 0;
580 }
581
582 static int s390_store_status(CPUS390XState *env, uint32_t parameter)
583 {
584 /* XXX */
585 fprintf(stderr, "XXX SIGP store status\n");
586 return -1;
587 }
588
589 static int s390_cpu_initial_reset(S390CPU *cpu)
590 {
591 CPUS390XState *env = &cpu->env;
592 int i;
593
594 s390_del_running_cpu(env);
595 if (kvm_vcpu_ioctl(CPU(cpu), KVM_S390_INITIAL_RESET, NULL) < 0) {
596 perror("cannot init reset vcpu");
597 }
598
599 /* Manually zero out all registers */
600 cpu_synchronize_state(env);
601 for (i = 0; i < 16; i++) {
602 env->regs[i] = 0;
603 }
604
605 dprintf("DONE: SIGP initial reset: %p\n", env);
606 return 0;
607 }
608
609 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
610 {
611 CPUS390XState *env = &cpu->env;
612 uint8_t order_code;
613 uint32_t parameter;
614 uint16_t cpu_addr;
615 uint8_t t;
616 int r = -1;
617 S390CPU *target_cpu;
618 CPUS390XState *target_env;
619
620 cpu_synchronize_state(env);
621
622 /* get order code */
623 order_code = run->s390_sieic.ipb >> 28;
624 if (order_code > 0) {
625 order_code = env->regs[order_code];
626 }
627 order_code += (run->s390_sieic.ipb & 0x0fff0000) >> 16;
628
629 /* get parameters */
630 t = (ipa1 & 0xf0) >> 4;
631 if (!(t % 2)) {
632 t++;
633 }
634
635 parameter = env->regs[t] & 0x7ffffe00;
636 cpu_addr = env->regs[ipa1 & 0x0f];
637
638 target_cpu = s390_cpu_addr2state(cpu_addr);
639 if (target_cpu == NULL) {
640 goto out;
641 }
642 target_env = &target_cpu->env;
643
644 switch (order_code) {
645 case SIGP_RESTART:
646 r = s390_cpu_restart(target_cpu);
647 break;
648 case SIGP_STORE_STATUS_ADDR:
649 r = s390_store_status(target_env, parameter);
650 break;
651 case SIGP_SET_ARCH:
652 /* make the caller panic */
653 return -1;
654 case SIGP_INITIAL_CPU_RESET:
655 r = s390_cpu_initial_reset(target_cpu);
656 break;
657 default:
658 fprintf(stderr, "KVM: unknown SIGP: 0x%x\n", order_code);
659 break;
660 }
661
662 out:
663 setcc(cpu, r ? 3 : 0);
664 return 0;
665 }
666
667 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
668 {
669 CPUS390XState *env = &cpu->env;
670 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
671 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
672 int ipb_code = (run->s390_sieic.ipb & 0x0fff0000) >> 16;
673 int r = -1;
674
675 dprintf("handle_instruction 0x%x 0x%x\n", run->s390_sieic.ipa, run->s390_sieic.ipb);
676 switch (ipa0) {
677 case IPA0_B2:
678 case IPA0_B9:
679 case IPA0_EB:
680 r = handle_priv(cpu, run, ipa0 >> 8, ipa1);
681 break;
682 case IPA0_DIAG:
683 r = handle_diag(env, run, ipb_code);
684 break;
685 case IPA0_SIGP:
686 r = handle_sigp(cpu, run, ipa1);
687 break;
688 }
689
690 if (r < 0) {
691 enter_pgmcheck(cpu, 0x0001);
692 }
693 return 0;
694 }
695
696 static bool is_special_wait_psw(CPUState *cs)
697 {
698 /* signal quiesce */
699 return cs->kvm_run->psw_addr == 0xfffUL;
700 }
701
702 static int handle_intercept(S390CPU *cpu)
703 {
704 CPUS390XState *env = &cpu->env;
705 CPUState *cs = CPU(cpu);
706 struct kvm_run *run = cs->kvm_run;
707 int icpt_code = run->s390_sieic.icptcode;
708 int r = 0;
709
710 dprintf("intercept: 0x%x (at 0x%lx)\n", icpt_code,
711 (long)cs->kvm_run->psw_addr);
712 switch (icpt_code) {
713 case ICPT_INSTRUCTION:
714 r = handle_instruction(cpu, run);
715 break;
716 case ICPT_WAITPSW:
717 if (s390_del_running_cpu(env) == 0 &&
718 is_special_wait_psw(cs)) {
719 qemu_system_shutdown_request();
720 }
721 r = EXCP_HALTED;
722 break;
723 case ICPT_CPU_STOP:
724 if (s390_del_running_cpu(env) == 0) {
725 qemu_system_shutdown_request();
726 }
727 r = EXCP_HALTED;
728 break;
729 case ICPT_SOFT_INTERCEPT:
730 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
731 exit(1);
732 break;
733 case ICPT_IO:
734 fprintf(stderr, "KVM unimplemented icpt IO\n");
735 exit(1);
736 break;
737 default:
738 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
739 exit(1);
740 break;
741 }
742
743 return r;
744 }
745
746 static int handle_tsch(S390CPU *cpu)
747 {
748 CPUS390XState *env = &cpu->env;
749 CPUState *cs = CPU(cpu);
750 struct kvm_run *run = cs->kvm_run;
751 int ret;
752
753 cpu_synchronize_state(env);
754 ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
755 if (ret >= 0) {
756 /* Success; set condition code. */
757 setcc(cpu, ret);
758 ret = 0;
759 } else if (ret < -1) {
760 /*
761 * Failure.
762 * If an I/O interrupt had been dequeued, we have to reinject it.
763 */
764 if (run->s390_tsch.dequeued) {
765 uint16_t subchannel_id = run->s390_tsch.subchannel_id;
766 uint16_t subchannel_nr = run->s390_tsch.subchannel_nr;
767 uint32_t io_int_parm = run->s390_tsch.io_int_parm;
768 uint32_t io_int_word = run->s390_tsch.io_int_word;
769 uint32_t type = ((subchannel_id & 0xff00) << 24) |
770 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
771
772 kvm_s390_interrupt_internal(cpu, type,
773 ((uint32_t)subchannel_id << 16)
774 | subchannel_nr,
775 ((uint64_t)io_int_parm << 32)
776 | io_int_word, 1);
777 }
778 ret = 0;
779 }
780 return ret;
781 }
782
783 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
784 {
785 S390CPU *cpu = S390_CPU(cs);
786 int ret = 0;
787
788 switch (run->exit_reason) {
789 case KVM_EXIT_S390_SIEIC:
790 ret = handle_intercept(cpu);
791 break;
792 case KVM_EXIT_S390_RESET:
793 qemu_system_reset_request();
794 break;
795 case KVM_EXIT_S390_TSCH:
796 ret = handle_tsch(cpu);
797 break;
798 default:
799 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
800 break;
801 }
802
803 if (ret == 0) {
804 ret = EXCP_INTERRUPT;
805 }
806 return ret;
807 }
808
809 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
810 {
811 return true;
812 }
813
814 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
815 {
816 return 1;
817 }
818
819 int kvm_arch_on_sigbus(int code, void *addr)
820 {
821 return 1;
822 }
823
824 void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id,
825 uint16_t subchannel_nr, uint32_t io_int_parm,
826 uint32_t io_int_word)
827 {
828 uint32_t type;
829
830 type = ((subchannel_id & 0xff00) << 24) |
831 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
832 kvm_s390_interrupt_internal(cpu, type,
833 ((uint32_t)subchannel_id << 16) | subchannel_nr,
834 ((uint64_t)io_int_parm << 32) | io_int_word, 1);
835 }
836
837 void kvm_s390_crw_mchk(S390CPU *cpu)
838 {
839 kvm_s390_interrupt_internal(cpu, KVM_S390_MCHK, 1 << 28,
840 0x00400f1d40330000, 1);
841 }
842
843 void kvm_s390_enable_css_support(S390CPU *cpu)
844 {
845 struct kvm_enable_cap cap = {};
846 int r;
847
848 /* Activate host kernel channel subsystem support. */
849 cap.cap = KVM_CAP_S390_CSS_SUPPORT;
850 r = kvm_vcpu_ioctl(CPU(cpu), KVM_ENABLE_CAP, &cap);
851 assert(r == 0);
852 }