s390x/kvm: Rework SIGP INITIAL CPU RESET handler
[qemu/ar7.git] / target-s390x / kvm.c
blob75e88224e34d4276ef8e76b1bf8674a451e7dbda
1 /*
2 * QEMU S390x KVM implementation
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp. 2012
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * Contributions after 2012-10-29 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
20 * You should have received a copy of the GNU (Lesser) General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/mman.h>
28 #include <linux/kvm.h>
29 #include <asm/ptrace.h>
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 #include "qapi/qmp/qjson.h"
38 #include "monitor/monitor.h"
40 /* #define DEBUG_KVM */
42 #ifdef DEBUG_KVM
43 #define DPRINTF(fmt, ...) \
44 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
45 #else
46 #define DPRINTF(fmt, ...) \
47 do { } while (0)
48 #endif
50 #define IPA0_DIAG 0x8300
51 #define IPA0_SIGP 0xae00
52 #define IPA0_B2 0xb200
53 #define IPA0_B9 0xb900
54 #define IPA0_EB 0xeb00
56 #define PRIV_SCLP_CALL 0x20
57 #define PRIV_CSCH 0x30
58 #define PRIV_HSCH 0x31
59 #define PRIV_MSCH 0x32
60 #define PRIV_SSCH 0x33
61 #define PRIV_STSCH 0x34
62 #define PRIV_TSCH 0x35
63 #define PRIV_TPI 0x36
64 #define PRIV_SAL 0x37
65 #define PRIV_RSCH 0x38
66 #define PRIV_STCRW 0x39
67 #define PRIV_STCPS 0x3a
68 #define PRIV_RCHP 0x3b
69 #define PRIV_SCHM 0x3c
70 #define PRIV_CHSC 0x5f
71 #define PRIV_SIGA 0x74
72 #define PRIV_XSCH 0x76
73 #define PRIV_SQBS 0x8a
74 #define PRIV_EQBS 0x9c
75 #define DIAG_IPL 0x308
76 #define DIAG_KVM_HYPERCALL 0x500
77 #define DIAG_KVM_BREAKPOINT 0x501
79 #define ICPT_INSTRUCTION 0x04
80 #define ICPT_WAITPSW 0x1c
81 #define ICPT_SOFT_INTERCEPT 0x24
82 #define ICPT_CPU_STOP 0x28
83 #define ICPT_IO 0x40
85 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
86 KVM_CAP_LAST_INFO
89 static int cap_sync_regs;
90 static int cap_async_pf;
92 static void *legacy_s390_alloc(size_t size);
94 int kvm_arch_init(KVMState *s)
96 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
97 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
98 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
99 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
100 phys_mem_set_alloc(legacy_s390_alloc);
102 return 0;
105 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
107 return cpu->cpu_index;
110 int kvm_arch_init_vcpu(CPUState *cpu)
112 /* nothing todo yet */
113 return 0;
116 void kvm_arch_reset_vcpu(CPUState *cpu)
118 /* The initial reset call is needed here to reset in-kernel
119 * vcpu data that we can't access directly from QEMU
120 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
121 * Before this ioctl cpu_synchronize_state() is called in common kvm
122 * code (kvm-all) */
123 if (kvm_vcpu_ioctl(cpu, KVM_S390_INITIAL_RESET, NULL)) {
124 perror("Can't reset vcpu\n");
128 int kvm_arch_put_registers(CPUState *cs, int level)
130 S390CPU *cpu = S390_CPU(cs);
131 CPUS390XState *env = &cpu->env;
132 struct kvm_one_reg reg;
133 struct kvm_sregs sregs;
134 struct kvm_regs regs;
135 int ret;
136 int i;
138 /* always save the PSW and the GPRS*/
139 cs->kvm_run->psw_addr = env->psw.addr;
140 cs->kvm_run->psw_mask = env->psw.mask;
142 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
143 for (i = 0; i < 16; i++) {
144 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
145 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
147 } else {
148 for (i = 0; i < 16; i++) {
149 regs.gprs[i] = env->regs[i];
151 ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
152 if (ret < 0) {
153 return ret;
157 /* Do we need to save more than that? */
158 if (level == KVM_PUT_RUNTIME_STATE) {
159 return 0;
162 reg.id = KVM_REG_S390_CPU_TIMER;
163 reg.addr = (__u64)&(env->cputm);
164 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
165 if (ret < 0) {
166 return ret;
169 reg.id = KVM_REG_S390_CLOCK_COMP;
170 reg.addr = (__u64)&(env->ckc);
171 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
172 if (ret < 0) {
173 return ret;
176 reg.id = KVM_REG_S390_TODPR;
177 reg.addr = (__u64)&(env->todpr);
178 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
179 if (ret < 0) {
180 return ret;
183 if (cap_async_pf) {
184 reg.id = KVM_REG_S390_PFTOKEN;
185 reg.addr = (__u64)&(env->pfault_token);
186 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
187 if (ret < 0) {
188 return ret;
191 reg.id = KVM_REG_S390_PFCOMPARE;
192 reg.addr = (__u64)&(env->pfault_compare);
193 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
194 if (ret < 0) {
195 return ret;
198 reg.id = KVM_REG_S390_PFSELECT;
199 reg.addr = (__u64)&(env->pfault_select);
200 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
201 if (ret < 0) {
202 return ret;
206 if (cap_sync_regs &&
207 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
208 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
209 for (i = 0; i < 16; i++) {
210 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
211 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
213 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
214 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
215 } else {
216 for (i = 0; i < 16; i++) {
217 sregs.acrs[i] = env->aregs[i];
218 sregs.crs[i] = env->cregs[i];
220 ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
221 if (ret < 0) {
222 return ret;
226 /* Finally the prefix */
227 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
228 cs->kvm_run->s.regs.prefix = env->psa;
229 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
230 } else {
231 /* prefix is only supported via sync regs */
233 return 0;
236 int kvm_arch_get_registers(CPUState *cs)
238 S390CPU *cpu = S390_CPU(cs);
239 CPUS390XState *env = &cpu->env;
240 struct kvm_one_reg reg;
241 struct kvm_sregs sregs;
242 struct kvm_regs regs;
243 int i, r;
245 /* get the PSW */
246 env->psw.addr = cs->kvm_run->psw_addr;
247 env->psw.mask = cs->kvm_run->psw_mask;
249 /* the GPRS */
250 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
251 for (i = 0; i < 16; i++) {
252 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
254 } else {
255 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
256 if (r < 0) {
257 return r;
259 for (i = 0; i < 16; i++) {
260 env->regs[i] = regs.gprs[i];
264 /* The ACRS and CRS */
265 if (cap_sync_regs &&
266 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
267 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
268 for (i = 0; i < 16; i++) {
269 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
270 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
272 } else {
273 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
274 if (r < 0) {
275 return r;
277 for (i = 0; i < 16; i++) {
278 env->aregs[i] = sregs.acrs[i];
279 env->cregs[i] = sregs.crs[i];
283 /* The prefix */
284 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
285 env->psa = cs->kvm_run->s.regs.prefix;
288 /* One Regs */
289 reg.id = KVM_REG_S390_CPU_TIMER;
290 reg.addr = (__u64)&(env->cputm);
291 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
292 if (r < 0) {
293 return r;
296 reg.id = KVM_REG_S390_CLOCK_COMP;
297 reg.addr = (__u64)&(env->ckc);
298 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
299 if (r < 0) {
300 return r;
303 reg.id = KVM_REG_S390_TODPR;
304 reg.addr = (__u64)&(env->todpr);
305 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
306 if (r < 0) {
307 return r;
310 if (cap_async_pf) {
311 reg.id = KVM_REG_S390_PFTOKEN;
312 reg.addr = (__u64)&(env->pfault_token);
313 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
314 if (r < 0) {
315 return r;
318 reg.id = KVM_REG_S390_PFCOMPARE;
319 reg.addr = (__u64)&(env->pfault_compare);
320 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
321 if (r < 0) {
322 return r;
325 reg.id = KVM_REG_S390_PFSELECT;
326 reg.addr = (__u64)&(env->pfault_select);
327 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
328 if (r < 0) {
329 return r;
333 return 0;
337 * Legacy layout for s390:
338 * Older S390 KVM requires the topmost vma of the RAM to be
339 * smaller than an system defined value, which is at least 256GB.
340 * Larger systems have larger values. We put the guest between
341 * the end of data segment (system break) and this value. We
342 * use 32GB as a base to have enough room for the system break
343 * to grow. We also have to use MAP parameters that avoid
344 * read-only mapping of guest pages.
346 static void *legacy_s390_alloc(size_t size)
348 void *mem;
350 mem = mmap((void *) 0x800000000ULL, size,
351 PROT_EXEC|PROT_READ|PROT_WRITE,
352 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
353 return mem == MAP_FAILED ? NULL : mem;
356 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
358 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
360 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
361 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501, 4, 1)) {
362 return -EINVAL;
364 return 0;
367 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
369 uint8_t t[4];
370 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
372 if (cpu_memory_rw_debug(cs, bp->pc, t, 4, 0)) {
373 return -EINVAL;
374 } else if (memcmp(t, diag_501, 4)) {
375 return -EINVAL;
376 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
377 return -EINVAL;
380 return 0;
383 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
387 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
391 int kvm_arch_process_async_events(CPUState *cs)
393 return cs->halted;
396 void kvm_s390_interrupt_internal(S390CPU *cpu, int type, uint32_t parm,
397 uint64_t parm64, int vm)
399 CPUState *cs = CPU(cpu);
400 struct kvm_s390_interrupt kvmint;
401 int r;
403 if (!cs->kvm_state) {
404 return;
407 kvmint.type = type;
408 kvmint.parm = parm;
409 kvmint.parm64 = parm64;
411 if (vm) {
412 r = kvm_vm_ioctl(cs->kvm_state, KVM_S390_INTERRUPT, &kvmint);
413 } else {
414 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
417 if (r < 0) {
418 fprintf(stderr, "KVM failed to inject interrupt\n");
419 exit(1);
423 void kvm_s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token)
425 kvm_s390_interrupt_internal(cpu, KVM_S390_INT_VIRTIO, config_change,
426 token, 1);
429 void kvm_s390_interrupt(S390CPU *cpu, int type, uint32_t code)
431 kvm_s390_interrupt_internal(cpu, type, code, 0, 0);
434 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
436 kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code);
439 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
440 uint16_t ipbh0)
442 CPUS390XState *env = &cpu->env;
443 uint64_t sccb;
444 uint32_t code;
445 int r = 0;
447 cpu_synchronize_state(CPU(cpu));
448 sccb = env->regs[ipbh0 & 0xf];
449 code = env->regs[(ipbh0 & 0xf0) >> 4];
451 r = sclp_service_call(env, sccb, code);
452 if (r < 0) {
453 enter_pgmcheck(cpu, -r);
454 } else {
455 setcc(cpu, r);
458 return 0;
461 static int kvm_handle_css_inst(S390CPU *cpu, struct kvm_run *run,
462 uint8_t ipa0, uint8_t ipa1, uint8_t ipb)
464 CPUS390XState *env = &cpu->env;
466 if (ipa0 != 0xb2) {
467 /* Not handled for now. */
468 return -1;
471 cpu_synchronize_state(CPU(cpu));
473 switch (ipa1) {
474 case PRIV_XSCH:
475 ioinst_handle_xsch(cpu, env->regs[1]);
476 break;
477 case PRIV_CSCH:
478 ioinst_handle_csch(cpu, env->regs[1]);
479 break;
480 case PRIV_HSCH:
481 ioinst_handle_hsch(cpu, env->regs[1]);
482 break;
483 case PRIV_MSCH:
484 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
485 break;
486 case PRIV_SSCH:
487 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
488 break;
489 case PRIV_STCRW:
490 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
491 break;
492 case PRIV_STSCH:
493 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
494 break;
495 case PRIV_TSCH:
496 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
497 fprintf(stderr, "Spurious tsch intercept\n");
498 break;
499 case PRIV_CHSC:
500 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
501 break;
502 case PRIV_TPI:
503 /* This should have been handled by kvm already. */
504 fprintf(stderr, "Spurious tpi intercept\n");
505 break;
506 case PRIV_SCHM:
507 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
508 run->s390_sieic.ipb);
509 break;
510 case PRIV_RSCH:
511 ioinst_handle_rsch(cpu, env->regs[1]);
512 break;
513 case PRIV_RCHP:
514 ioinst_handle_rchp(cpu, env->regs[1]);
515 break;
516 case PRIV_STCPS:
517 /* We do not provide this instruction, it is suppressed. */
518 break;
519 case PRIV_SAL:
520 ioinst_handle_sal(cpu, env->regs[1]);
521 break;
522 case PRIV_SIGA:
523 /* Not provided, set CC = 3 for subchannel not operational */
524 setcc(cpu, 3);
525 break;
526 default:
527 return -1;
530 return 0;
533 static int handle_priv(S390CPU *cpu, struct kvm_run *run,
534 uint8_t ipa0, uint8_t ipa1)
536 int r = 0;
537 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
538 uint8_t ipb = run->s390_sieic.ipb & 0xff;
540 DPRINTF("KVM: PRIV: %d\n", ipa1);
541 switch (ipa1) {
542 case PRIV_SCLP_CALL:
543 r = kvm_sclp_service_call(cpu, run, ipbh0);
544 break;
545 default:
546 r = kvm_handle_css_inst(cpu, run, ipa0, ipa1, ipb);
547 if (r == -1) {
548 DPRINTF("KVM: unhandled PRIV: 0x%x\n", ipa1);
550 break;
553 return r;
556 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
558 CPUS390XState *env = &cpu->env;
559 int ret;
561 cpu_synchronize_state(CPU(cpu));
562 ret = s390_virtio_hypercall(env);
563 if (ret == -EINVAL) {
564 enter_pgmcheck(cpu, PGM_SPECIFICATION);
565 return 0;
568 return ret;
571 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
573 uint64_t r1, r3;
575 cpu_synchronize_state(CPU(cpu));
576 r1 = (run->s390_sieic.ipa & 0x00f0) >> 8;
577 r3 = run->s390_sieic.ipa & 0x000f;
578 handle_diag_308(&cpu->env, r1, r3);
581 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
583 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
585 int r = 0;
586 uint16_t func_code;
589 * For any diagnose call we support, bits 48-63 of the resulting
590 * address specify the function code; the remainder is ignored.
592 func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
593 switch (func_code) {
594 case DIAG_IPL:
595 kvm_handle_diag_308(cpu, run);
596 break;
597 case DIAG_KVM_HYPERCALL:
598 r = handle_hypercall(cpu, run);
599 break;
600 case DIAG_KVM_BREAKPOINT:
601 sleep(10);
602 break;
603 default:
604 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
605 r = -1;
606 break;
609 return r;
612 static int kvm_s390_cpu_start(S390CPU *cpu)
614 s390_add_running_cpu(cpu);
615 qemu_cpu_kick(CPU(cpu));
616 DPRINTF("DONE: KVM cpu start: %p\n", &cpu->env);
617 return 0;
620 int kvm_s390_cpu_restart(S390CPU *cpu)
622 kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0);
623 s390_add_running_cpu(cpu);
624 qemu_cpu_kick(CPU(cpu));
625 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
626 return 0;
629 static void sigp_initial_cpu_reset(void *arg)
631 CPUState *cpu = arg;
632 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
634 cpu_synchronize_state(cpu);
635 scc->initial_cpu_reset(cpu);
638 #define SIGP_ORDER_MASK 0x000000ff
640 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
642 CPUS390XState *env = &cpu->env;
643 uint8_t order_code;
644 uint16_t cpu_addr;
645 S390CPU *target_cpu;
646 uint64_t *statusreg = &env->regs[ipa1 >> 4];
647 int cc;
649 cpu_synchronize_state(CPU(cpu));
651 /* get order code */
652 order_code = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
654 cpu_addr = env->regs[ipa1 & 0x0f];
655 target_cpu = s390_cpu_addr2state(cpu_addr);
656 if (target_cpu == NULL) {
657 cc = 3; /* not operational */
658 goto out;
661 switch (order_code) {
662 case SIGP_START:
663 cc = kvm_s390_cpu_start(target_cpu);
664 break;
665 case SIGP_RESTART:
666 cc = kvm_s390_cpu_restart(target_cpu);
667 break;
668 case SIGP_SET_ARCH:
669 *statusreg &= 0xffffffff00000000UL;
670 *statusreg |= SIGP_STAT_INVALID_PARAMETER;
671 cc = 1; /* status stored */
672 break;
673 case SIGP_INITIAL_CPU_RESET:
674 run_on_cpu(CPU(target_cpu), sigp_initial_cpu_reset, CPU(target_cpu));
675 cc = 0;
676 break;
677 default:
678 DPRINTF("KVM: unknown SIGP: 0x%x\n", order_code);
679 *statusreg &= 0xffffffff00000000UL;
680 *statusreg |= SIGP_STAT_INVALID_ORDER;
681 cc = 1; /* status stored */
682 break;
685 out:
686 setcc(cpu, cc);
687 return 0;
690 static void handle_instruction(S390CPU *cpu, struct kvm_run *run)
692 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
693 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
694 int r = -1;
696 DPRINTF("handle_instruction 0x%x 0x%x\n",
697 run->s390_sieic.ipa, run->s390_sieic.ipb);
698 switch (ipa0) {
699 case IPA0_B2:
700 case IPA0_B9:
701 case IPA0_EB:
702 r = handle_priv(cpu, run, ipa0 >> 8, ipa1);
703 break;
704 case IPA0_DIAG:
705 r = handle_diag(cpu, run, run->s390_sieic.ipb);
706 break;
707 case IPA0_SIGP:
708 r = handle_sigp(cpu, run, ipa1);
709 break;
712 if (r < 0) {
713 enter_pgmcheck(cpu, 0x0001);
717 static bool is_special_wait_psw(CPUState *cs)
719 /* signal quiesce */
720 return cs->kvm_run->psw_addr == 0xfffUL;
723 static int handle_intercept(S390CPU *cpu)
725 CPUState *cs = CPU(cpu);
726 struct kvm_run *run = cs->kvm_run;
727 int icpt_code = run->s390_sieic.icptcode;
728 int r = 0;
730 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
731 (long)cs->kvm_run->psw_addr);
732 switch (icpt_code) {
733 case ICPT_INSTRUCTION:
734 handle_instruction(cpu, run);
735 break;
736 case ICPT_WAITPSW:
737 /* disabled wait, since enabled wait is handled in kernel */
738 if (s390_del_running_cpu(cpu) == 0) {
739 if (is_special_wait_psw(cs)) {
740 qemu_system_shutdown_request();
741 } else {
742 QObject *data;
744 data = qobject_from_jsonf("{ 'action': %s }", "pause");
745 monitor_protocol_event(QEVENT_GUEST_PANICKED, data);
746 qobject_decref(data);
747 vm_stop(RUN_STATE_GUEST_PANICKED);
750 r = EXCP_HALTED;
751 break;
752 case ICPT_CPU_STOP:
753 if (s390_del_running_cpu(cpu) == 0) {
754 qemu_system_shutdown_request();
756 r = EXCP_HALTED;
757 break;
758 case ICPT_SOFT_INTERCEPT:
759 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
760 exit(1);
761 break;
762 case ICPT_IO:
763 fprintf(stderr, "KVM unimplemented icpt IO\n");
764 exit(1);
765 break;
766 default:
767 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
768 exit(1);
769 break;
772 return r;
775 static int handle_tsch(S390CPU *cpu)
777 CPUS390XState *env = &cpu->env;
778 CPUState *cs = CPU(cpu);
779 struct kvm_run *run = cs->kvm_run;
780 int ret;
782 cpu_synchronize_state(cs);
784 ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
785 if (ret >= 0) {
786 /* Success; set condition code. */
787 setcc(cpu, ret);
788 ret = 0;
789 } else if (ret < -1) {
791 * Failure.
792 * If an I/O interrupt had been dequeued, we have to reinject it.
794 if (run->s390_tsch.dequeued) {
795 uint16_t subchannel_id = run->s390_tsch.subchannel_id;
796 uint16_t subchannel_nr = run->s390_tsch.subchannel_nr;
797 uint32_t io_int_parm = run->s390_tsch.io_int_parm;
798 uint32_t io_int_word = run->s390_tsch.io_int_word;
799 uint32_t type = ((subchannel_id & 0xff00) << 24) |
800 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
802 kvm_s390_interrupt_internal(cpu, type,
803 ((uint32_t)subchannel_id << 16)
804 | subchannel_nr,
805 ((uint64_t)io_int_parm << 32)
806 | io_int_word, 1);
808 ret = 0;
810 return ret;
813 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
815 S390CPU *cpu = S390_CPU(cs);
816 int ret = 0;
818 switch (run->exit_reason) {
819 case KVM_EXIT_S390_SIEIC:
820 ret = handle_intercept(cpu);
821 break;
822 case KVM_EXIT_S390_RESET:
823 qemu_system_reset_request();
824 break;
825 case KVM_EXIT_S390_TSCH:
826 ret = handle_tsch(cpu);
827 break;
828 default:
829 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
830 break;
833 if (ret == 0) {
834 ret = EXCP_INTERRUPT;
836 return ret;
839 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
841 return true;
844 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
846 return 1;
849 int kvm_arch_on_sigbus(int code, void *addr)
851 return 1;
854 void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id,
855 uint16_t subchannel_nr, uint32_t io_int_parm,
856 uint32_t io_int_word)
858 uint32_t type;
860 type = ((subchannel_id & 0xff00) << 24) |
861 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
862 kvm_s390_interrupt_internal(cpu, type,
863 ((uint32_t)subchannel_id << 16) | subchannel_nr,
864 ((uint64_t)io_int_parm << 32) | io_int_word, 1);
867 void kvm_s390_crw_mchk(S390CPU *cpu)
869 kvm_s390_interrupt_internal(cpu, KVM_S390_MCHK, 1 << 28,
870 0x00400f1d40330000, 1);
873 void kvm_s390_enable_css_support(S390CPU *cpu)
875 struct kvm_enable_cap cap = {};
876 int r;
878 /* Activate host kernel channel subsystem support. */
879 cap.cap = KVM_CAP_S390_CSS_SUPPORT;
880 r = kvm_vcpu_ioctl(CPU(cpu), KVM_ENABLE_CAP, &cap);
881 assert(r == 0);
884 void kvm_arch_init_irq_routing(KVMState *s)
888 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
889 int vq, bool assign)
891 struct kvm_ioeventfd kick = {
892 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
893 KVM_IOEVENTFD_FLAG_DATAMATCH,
894 .fd = event_notifier_get_fd(notifier),
895 .datamatch = vq,
896 .addr = sch,
897 .len = 8,
899 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
900 return -ENOSYS;
902 if (!assign) {
903 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
905 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);