s390/cpu: Make setcc() function available to other files
[qemu/ar7.git] / target-s390x / kvm.c
bloba5d5584fc3bd886566acb65dac058944d73e5b63
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 #define SIGP_RESTART 0x06
86 #define SIGP_INITIAL_CPU_RESET 0x0b
87 #define SIGP_STORE_STATUS_ADDR 0x0e
88 #define SIGP_SET_ARCH 0x12
90 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
91 KVM_CAP_LAST_INFO
94 static int cap_sync_regs;
96 static void *legacy_s390_alloc(ram_addr_t size);
98 int kvm_arch_init(KVMState *s)
100 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
101 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
102 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
103 phys_mem_set_alloc(legacy_s390_alloc);
105 return 0;
108 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
110 return cpu->cpu_index;
113 int kvm_arch_init_vcpu(CPUState *cpu)
115 /* nothing todo yet */
116 return 0;
119 void kvm_arch_reset_vcpu(CPUState *cpu)
121 /* The initial reset call is needed here to reset in-kernel
122 * vcpu data that we can't access directly from QEMU
123 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
124 * Before this ioctl cpu_synchronize_state() is called in common kvm
125 * code (kvm-all) */
126 if (kvm_vcpu_ioctl(cpu, KVM_S390_INITIAL_RESET, NULL)) {
127 perror("Can't reset vcpu\n");
131 int kvm_arch_put_registers(CPUState *cs, int level)
133 S390CPU *cpu = S390_CPU(cs);
134 CPUS390XState *env = &cpu->env;
135 struct kvm_one_reg reg;
136 struct kvm_sregs sregs;
137 struct kvm_regs regs;
138 int ret;
139 int i;
141 /* always save the PSW and the GPRS*/
142 cs->kvm_run->psw_addr = env->psw.addr;
143 cs->kvm_run->psw_mask = env->psw.mask;
145 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
146 for (i = 0; i < 16; i++) {
147 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
148 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
150 } else {
151 for (i = 0; i < 16; i++) {
152 regs.gprs[i] = env->regs[i];
154 ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
155 if (ret < 0) {
156 return ret;
160 if (env->runtime_reg_dirty_mask == KVM_S390_RUNTIME_DIRTY_FULL) {
161 reg.id = KVM_REG_S390_CPU_TIMER;
162 reg.addr = (__u64)&(env->cputm);
163 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
164 if (ret < 0) {
165 return ret;
168 reg.id = KVM_REG_S390_CLOCK_COMP;
169 reg.addr = (__u64)&(env->ckc);
170 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
171 if (ret < 0) {
172 return ret;
175 reg.id = KVM_REG_S390_TODPR;
176 reg.addr = (__u64)&(env->todpr);
177 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
178 if (ret < 0) {
179 return ret;
182 env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_NONE;
184 /* Do we need to save more than that? */
185 if (level == KVM_PUT_RUNTIME_STATE) {
186 return 0;
189 if (cap_sync_regs &&
190 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
191 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
192 for (i = 0; i < 16; i++) {
193 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
194 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
196 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
197 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
198 } else {
199 for (i = 0; i < 16; i++) {
200 sregs.acrs[i] = env->aregs[i];
201 sregs.crs[i] = env->cregs[i];
203 ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
204 if (ret < 0) {
205 return ret;
209 /* Finally the prefix */
210 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
211 cs->kvm_run->s.regs.prefix = env->psa;
212 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
213 } else {
214 /* prefix is only supported via sync regs */
216 return 0;
219 int kvm_arch_get_registers(CPUState *cs)
221 S390CPU *cpu = S390_CPU(cs);
222 CPUS390XState *env = &cpu->env;
223 struct kvm_one_reg reg;
224 int r;
226 r = kvm_s390_get_registers_partial(cs);
227 if (r < 0) {
228 return r;
231 reg.id = KVM_REG_S390_CPU_TIMER;
232 reg.addr = (__u64)&(env->cputm);
233 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
234 if (r < 0) {
235 return r;
238 reg.id = KVM_REG_S390_CLOCK_COMP;
239 reg.addr = (__u64)&(env->ckc);
240 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
241 if (r < 0) {
242 return r;
245 reg.id = KVM_REG_S390_TODPR;
246 reg.addr = (__u64)&(env->todpr);
247 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
248 if (r < 0) {
249 return r;
252 env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_FULL;
253 return 0;
256 int kvm_s390_get_registers_partial(CPUState *cs)
258 S390CPU *cpu = S390_CPU(cs);
259 CPUS390XState *env = &cpu->env;
260 struct kvm_sregs sregs;
261 struct kvm_regs regs;
262 int ret;
263 int i;
265 if (env->runtime_reg_dirty_mask) {
266 return 0;
269 /* get the PSW */
270 env->psw.addr = cs->kvm_run->psw_addr;
271 env->psw.mask = cs->kvm_run->psw_mask;
273 /* the GPRS */
274 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
275 for (i = 0; i < 16; i++) {
276 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
278 } else {
279 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
280 if (ret < 0) {
281 return ret;
283 for (i = 0; i < 16; i++) {
284 env->regs[i] = regs.gprs[i];
288 /* The ACRS and CRS */
289 if (cap_sync_regs &&
290 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
291 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
292 for (i = 0; i < 16; i++) {
293 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
294 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
296 } else {
297 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
298 if (ret < 0) {
299 return ret;
301 for (i = 0; i < 16; i++) {
302 env->aregs[i] = sregs.acrs[i];
303 env->cregs[i] = sregs.crs[i];
307 /* Finally the prefix */
308 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
309 env->psa = cs->kvm_run->s.regs.prefix;
310 } else {
311 /* no prefix without sync regs */
314 env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_PARTIAL;
315 return 0;
319 * Legacy layout for s390:
320 * Older S390 KVM requires the topmost vma of the RAM to be
321 * smaller than an system defined value, which is at least 256GB.
322 * Larger systems have larger values. We put the guest between
323 * the end of data segment (system break) and this value. We
324 * use 32GB as a base to have enough room for the system break
325 * to grow. We also have to use MAP parameters that avoid
326 * read-only mapping of guest pages.
328 static void *legacy_s390_alloc(ram_addr_t size)
330 void *mem;
332 mem = mmap((void *) 0x800000000ULL, size,
333 PROT_EXEC|PROT_READ|PROT_WRITE,
334 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
335 return mem == MAP_FAILED ? NULL : mem;
338 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
340 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
342 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
343 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501, 4, 1)) {
344 return -EINVAL;
346 return 0;
349 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
351 uint8_t t[4];
352 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
354 if (cpu_memory_rw_debug(cs, bp->pc, t, 4, 0)) {
355 return -EINVAL;
356 } else if (memcmp(t, diag_501, 4)) {
357 return -EINVAL;
358 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
359 return -EINVAL;
362 return 0;
365 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
369 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
373 int kvm_arch_process_async_events(CPUState *cs)
375 return cs->halted;
378 void kvm_s390_interrupt_internal(S390CPU *cpu, int type, uint32_t parm,
379 uint64_t parm64, int vm)
381 CPUState *cs = CPU(cpu);
382 struct kvm_s390_interrupt kvmint;
383 int r;
385 if (!cs->kvm_state) {
386 return;
389 kvmint.type = type;
390 kvmint.parm = parm;
391 kvmint.parm64 = parm64;
393 if (vm) {
394 r = kvm_vm_ioctl(cs->kvm_state, KVM_S390_INTERRUPT, &kvmint);
395 } else {
396 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
399 if (r < 0) {
400 fprintf(stderr, "KVM failed to inject interrupt\n");
401 exit(1);
405 void kvm_s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token)
407 kvm_s390_interrupt_internal(cpu, KVM_S390_INT_VIRTIO, config_change,
408 token, 1);
411 void kvm_s390_interrupt(S390CPU *cpu, int type, uint32_t code)
413 kvm_s390_interrupt_internal(cpu, type, code, 0, 0);
416 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
418 kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code);
421 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
422 uint16_t ipbh0)
424 CPUS390XState *env = &cpu->env;
425 uint32_t sccb;
426 uint64_t code;
427 int r = 0;
429 cpu_synchronize_state(CPU(cpu));
430 if (env->psw.mask & PSW_MASK_PSTATE) {
431 enter_pgmcheck(cpu, PGM_PRIVILEGED);
432 return 0;
434 sccb = env->regs[ipbh0 & 0xf];
435 code = env->regs[(ipbh0 & 0xf0) >> 4];
437 r = sclp_service_call(sccb, code);
438 if (r < 0) {
439 enter_pgmcheck(cpu, -r);
441 setcc(cpu, r);
443 return 0;
446 static int kvm_handle_css_inst(S390CPU *cpu, struct kvm_run *run,
447 uint8_t ipa0, uint8_t ipa1, uint8_t ipb)
449 int r = 0;
450 int no_cc = 0;
451 CPUS390XState *env = &cpu->env;
452 CPUState *cs = CPU(cpu);
454 if (ipa0 != 0xb2) {
455 /* Not handled for now. */
456 return -1;
459 kvm_s390_get_registers_partial(cs);
460 cs->kvm_vcpu_dirty = true;
462 switch (ipa1) {
463 case PRIV_XSCH:
464 r = ioinst_handle_xsch(env, env->regs[1]);
465 break;
466 case PRIV_CSCH:
467 r = ioinst_handle_csch(env, env->regs[1]);
468 break;
469 case PRIV_HSCH:
470 r = ioinst_handle_hsch(env, env->regs[1]);
471 break;
472 case PRIV_MSCH:
473 r = ioinst_handle_msch(env, env->regs[1], run->s390_sieic.ipb);
474 break;
475 case PRIV_SSCH:
476 r = ioinst_handle_ssch(env, env->regs[1], run->s390_sieic.ipb);
477 break;
478 case PRIV_STCRW:
479 r = ioinst_handle_stcrw(env, run->s390_sieic.ipb);
480 break;
481 case PRIV_STSCH:
482 r = ioinst_handle_stsch(env, env->regs[1], run->s390_sieic.ipb);
483 break;
484 case PRIV_TSCH:
485 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
486 fprintf(stderr, "Spurious tsch intercept\n");
487 break;
488 case PRIV_CHSC:
489 r = ioinst_handle_chsc(env, run->s390_sieic.ipb);
490 break;
491 case PRIV_TPI:
492 /* This should have been handled by kvm already. */
493 fprintf(stderr, "Spurious tpi intercept\n");
494 break;
495 case PRIV_SCHM:
496 no_cc = 1;
497 r = ioinst_handle_schm(env, env->regs[1], env->regs[2],
498 run->s390_sieic.ipb);
499 break;
500 case PRIV_RSCH:
501 r = ioinst_handle_rsch(env, env->regs[1]);
502 break;
503 case PRIV_RCHP:
504 r = ioinst_handle_rchp(env, env->regs[1]);
505 break;
506 case PRIV_STCPS:
507 /* We do not provide this instruction, it is suppressed. */
508 no_cc = 1;
509 r = 0;
510 break;
511 case PRIV_SAL:
512 no_cc = 1;
513 r = ioinst_handle_sal(env, env->regs[1]);
514 break;
515 case PRIV_SIGA:
516 /* Not provided, set CC = 3 for subchannel not operational */
517 r = 3;
518 break;
519 default:
520 return -1;
523 if (r >= 0 && !no_cc) {
524 setcc(cpu, r);
527 return 0;
530 static int handle_priv(S390CPU *cpu, struct kvm_run *run,
531 uint8_t ipa0, uint8_t ipa1)
533 int r = 0;
534 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
535 uint8_t ipb = run->s390_sieic.ipb & 0xff;
537 DPRINTF("KVM: PRIV: %d\n", ipa1);
538 switch (ipa1) {
539 case PRIV_SCLP_CALL:
540 r = kvm_sclp_service_call(cpu, run, ipbh0);
541 break;
542 default:
543 r = kvm_handle_css_inst(cpu, run, ipa0, ipa1, ipb);
544 if (r == -1) {
545 DPRINTF("KVM: unhandled PRIV: 0x%x\n", ipa1);
547 break;
550 return r;
553 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
555 CPUState *cs = CPU(cpu);
556 CPUS390XState *env = &cpu->env;
558 kvm_s390_get_registers_partial(cs);
559 cs->kvm_vcpu_dirty = true;
560 env->regs[2] = s390_virtio_hypercall(env);
562 return 0;
565 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
567 uint64_t r1, r3;
569 cpu_synchronize_state(CPU(cpu));
570 r1 = (run->s390_sieic.ipa & 0x00f0) >> 8;
571 r3 = run->s390_sieic.ipa & 0x000f;
572 handle_diag_308(&cpu->env, r1, r3);
575 static int handle_diag(S390CPU *cpu, struct kvm_run *run, int ipb_code)
577 int r = 0;
579 switch (ipb_code) {
580 case DIAG_IPL:
581 kvm_handle_diag_308(cpu, run);
582 break;
583 case DIAG_KVM_HYPERCALL:
584 r = handle_hypercall(cpu, run);
585 break;
586 case DIAG_KVM_BREAKPOINT:
587 sleep(10);
588 break;
589 default:
590 DPRINTF("KVM: unknown DIAG: 0x%x\n", ipb_code);
591 r = -1;
592 break;
595 return r;
598 int kvm_s390_cpu_restart(S390CPU *cpu)
600 kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0);
601 s390_add_running_cpu(cpu);
602 qemu_cpu_kick(CPU(cpu));
603 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
604 return 0;
607 static int s390_store_status(CPUS390XState *env, uint32_t parameter)
609 /* XXX */
610 fprintf(stderr, "XXX SIGP store status\n");
611 return -1;
614 static int s390_cpu_initial_reset(S390CPU *cpu)
616 CPUState *cs = CPU(cpu);
617 CPUS390XState *env = &cpu->env;
618 int i;
620 s390_del_running_cpu(cpu);
621 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL) < 0) {
622 perror("cannot init reset vcpu");
625 /* Manually zero out all registers */
626 cpu_synchronize_state(cs);
627 for (i = 0; i < 16; i++) {
628 env->regs[i] = 0;
631 DPRINTF("DONE: SIGP initial reset: %p\n", env);
632 return 0;
635 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
637 CPUS390XState *env = &cpu->env;
638 uint8_t order_code;
639 uint32_t parameter;
640 uint16_t cpu_addr;
641 uint8_t t;
642 int r = -1;
643 S390CPU *target_cpu;
644 CPUS390XState *target_env;
646 cpu_synchronize_state(CPU(cpu));
648 /* get order code */
649 order_code = run->s390_sieic.ipb >> 28;
650 if (order_code > 0) {
651 order_code = env->regs[order_code];
653 order_code += (run->s390_sieic.ipb & 0x0fff0000) >> 16;
655 /* get parameters */
656 t = (ipa1 & 0xf0) >> 4;
657 if (!(t % 2)) {
658 t++;
661 parameter = env->regs[t] & 0x7ffffe00;
662 cpu_addr = env->regs[ipa1 & 0x0f];
664 target_cpu = s390_cpu_addr2state(cpu_addr);
665 if (target_cpu == NULL) {
666 goto out;
668 target_env = &target_cpu->env;
670 switch (order_code) {
671 case SIGP_RESTART:
672 r = kvm_s390_cpu_restart(target_cpu);
673 break;
674 case SIGP_STORE_STATUS_ADDR:
675 r = s390_store_status(target_env, parameter);
676 break;
677 case SIGP_SET_ARCH:
678 /* make the caller panic */
679 return -1;
680 case SIGP_INITIAL_CPU_RESET:
681 r = s390_cpu_initial_reset(target_cpu);
682 break;
683 default:
684 fprintf(stderr, "KVM: unknown SIGP: 0x%x\n", order_code);
685 break;
688 out:
689 setcc(cpu, r ? 3 : 0);
690 return 0;
693 static void handle_instruction(S390CPU *cpu, struct kvm_run *run)
695 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
696 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
697 int ipb_code = (run->s390_sieic.ipb & 0x0fff0000) >> 16;
698 int r = -1;
700 DPRINTF("handle_instruction 0x%x 0x%x\n",
701 run->s390_sieic.ipa, run->s390_sieic.ipb);
702 switch (ipa0) {
703 case IPA0_B2:
704 case IPA0_B9:
705 case IPA0_EB:
706 r = handle_priv(cpu, run, ipa0 >> 8, ipa1);
707 break;
708 case IPA0_DIAG:
709 r = handle_diag(cpu, run, ipb_code);
710 break;
711 case IPA0_SIGP:
712 r = handle_sigp(cpu, run, ipa1);
713 break;
716 if (r < 0) {
717 enter_pgmcheck(cpu, 0x0001);
721 static bool is_special_wait_psw(CPUState *cs)
723 /* signal quiesce */
724 return cs->kvm_run->psw_addr == 0xfffUL;
727 static int handle_intercept(S390CPU *cpu)
729 CPUState *cs = CPU(cpu);
730 struct kvm_run *run = cs->kvm_run;
731 int icpt_code = run->s390_sieic.icptcode;
732 int r = 0;
734 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
735 (long)cs->kvm_run->psw_addr);
736 switch (icpt_code) {
737 case ICPT_INSTRUCTION:
738 handle_instruction(cpu, run);
739 break;
740 case ICPT_WAITPSW:
741 /* disabled wait, since enabled wait is handled in kernel */
742 if (s390_del_running_cpu(cpu) == 0) {
743 if (is_special_wait_psw(cs)) {
744 qemu_system_shutdown_request();
745 } else {
746 QObject *data;
748 data = qobject_from_jsonf("{ 'action': %s }", "pause");
749 monitor_protocol_event(QEVENT_GUEST_PANICKED, data);
750 qobject_decref(data);
751 vm_stop(RUN_STATE_GUEST_PANICKED);
754 r = EXCP_HALTED;
755 break;
756 case ICPT_CPU_STOP:
757 if (s390_del_running_cpu(cpu) == 0) {
758 qemu_system_shutdown_request();
760 r = EXCP_HALTED;
761 break;
762 case ICPT_SOFT_INTERCEPT:
763 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
764 exit(1);
765 break;
766 case ICPT_IO:
767 fprintf(stderr, "KVM unimplemented icpt IO\n");
768 exit(1);
769 break;
770 default:
771 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
772 exit(1);
773 break;
776 return r;
779 static int handle_tsch(S390CPU *cpu)
781 CPUS390XState *env = &cpu->env;
782 CPUState *cs = CPU(cpu);
783 struct kvm_run *run = cs->kvm_run;
784 int ret;
786 kvm_s390_get_registers_partial(cs);
787 cs->kvm_vcpu_dirty = true;
789 ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
790 if (ret >= 0) {
791 /* Success; set condition code. */
792 setcc(cpu, ret);
793 ret = 0;
794 } else if (ret < -1) {
796 * Failure.
797 * If an I/O interrupt had been dequeued, we have to reinject it.
799 if (run->s390_tsch.dequeued) {
800 uint16_t subchannel_id = run->s390_tsch.subchannel_id;
801 uint16_t subchannel_nr = run->s390_tsch.subchannel_nr;
802 uint32_t io_int_parm = run->s390_tsch.io_int_parm;
803 uint32_t io_int_word = run->s390_tsch.io_int_word;
804 uint32_t type = ((subchannel_id & 0xff00) << 24) |
805 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
807 kvm_s390_interrupt_internal(cpu, type,
808 ((uint32_t)subchannel_id << 16)
809 | subchannel_nr,
810 ((uint64_t)io_int_parm << 32)
811 | io_int_word, 1);
813 ret = 0;
815 return ret;
818 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
820 S390CPU *cpu = S390_CPU(cs);
821 int ret = 0;
823 switch (run->exit_reason) {
824 case KVM_EXIT_S390_SIEIC:
825 ret = handle_intercept(cpu);
826 break;
827 case KVM_EXIT_S390_RESET:
828 qemu_system_reset_request();
829 break;
830 case KVM_EXIT_S390_TSCH:
831 ret = handle_tsch(cpu);
832 break;
833 default:
834 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
835 break;
838 if (ret == 0) {
839 ret = EXCP_INTERRUPT;
841 return ret;
844 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
846 return true;
849 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
851 return 1;
854 int kvm_arch_on_sigbus(int code, void *addr)
856 return 1;
859 void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id,
860 uint16_t subchannel_nr, uint32_t io_int_parm,
861 uint32_t io_int_word)
863 uint32_t type;
865 type = ((subchannel_id & 0xff00) << 24) |
866 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
867 kvm_s390_interrupt_internal(cpu, type,
868 ((uint32_t)subchannel_id << 16) | subchannel_nr,
869 ((uint64_t)io_int_parm << 32) | io_int_word, 1);
872 void kvm_s390_crw_mchk(S390CPU *cpu)
874 kvm_s390_interrupt_internal(cpu, KVM_S390_MCHK, 1 << 28,
875 0x00400f1d40330000, 1);
878 void kvm_s390_enable_css_support(S390CPU *cpu)
880 struct kvm_enable_cap cap = {};
881 int r;
883 /* Activate host kernel channel subsystem support. */
884 cap.cap = KVM_CAP_S390_CSS_SUPPORT;
885 r = kvm_vcpu_ioctl(CPU(cpu), KVM_ENABLE_CAP, &cap);
886 assert(r == 0);
889 void kvm_arch_init_irq_routing(KVMState *s)
893 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
894 int vq, bool assign)
896 struct kvm_ioeventfd kick = {
897 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
898 KVM_IOEVENTFD_FLAG_DATAMATCH,
899 .fd = event_notifier_get_fd(notifier),
900 .datamatch = vq,
901 .addr = sch,
902 .len = 8,
904 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
905 return -ENOSYS;
907 if (!assign) {
908 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
910 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);