target-s390x: Fix CPUState rework fallout
[qemu.git] / target-s390x / kvm.c
blob85f01125de443f0510f6d3950a560c30ef180045
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_KVM_HYPERCALL 0x500
76 #define DIAG_KVM_BREAKPOINT 0x501
78 #define ICPT_INSTRUCTION 0x04
79 #define ICPT_WAITPSW 0x1c
80 #define ICPT_SOFT_INTERCEPT 0x24
81 #define ICPT_CPU_STOP 0x28
82 #define ICPT_IO 0x40
84 #define SIGP_RESTART 0x06
85 #define SIGP_INITIAL_CPU_RESET 0x0b
86 #define SIGP_STORE_STATUS_ADDR 0x0e
87 #define SIGP_SET_ARCH 0x12
89 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
90 KVM_CAP_LAST_INFO
93 static int cap_sync_regs;
95 int kvm_arch_init(KVMState *s)
97 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
98 return 0;
101 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
103 return cpu->cpu_index;
106 int kvm_arch_init_vcpu(CPUState *cpu)
108 /* nothing todo yet */
109 return 0;
112 void kvm_arch_reset_vcpu(CPUState *cpu)
114 /* The initial reset call is needed here to reset in-kernel
115 * vcpu data that we can't access directly from QEMU
116 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
117 * Before this ioctl cpu_synchronize_state() is called in common kvm
118 * code (kvm-all) */
119 if (kvm_vcpu_ioctl(cpu, KVM_S390_INITIAL_RESET, NULL)) {
120 perror("Can't reset vcpu\n");
124 int kvm_arch_put_registers(CPUState *cs, int level)
126 S390CPU *cpu = S390_CPU(cs);
127 CPUS390XState *env = &cpu->env;
128 struct kvm_one_reg reg;
129 struct kvm_sregs sregs;
130 struct kvm_regs regs;
131 int ret;
132 int i;
134 /* always save the PSW and the GPRS*/
135 cs->kvm_run->psw_addr = env->psw.addr;
136 cs->kvm_run->psw_mask = env->psw.mask;
138 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
139 for (i = 0; i < 16; i++) {
140 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
141 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
143 } else {
144 for (i = 0; i < 16; i++) {
145 regs.gprs[i] = env->regs[i];
147 ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
148 if (ret < 0) {
149 return ret;
153 if (env->runtime_reg_dirty_mask == KVM_S390_RUNTIME_DIRTY_FULL) {
154 reg.id = KVM_REG_S390_CPU_TIMER;
155 reg.addr = (__u64)&(env->cputm);
156 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
157 if (ret < 0) {
158 return ret;
161 reg.id = KVM_REG_S390_CLOCK_COMP;
162 reg.addr = (__u64)&(env->ckc);
163 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
164 if (ret < 0) {
165 return ret;
168 reg.id = KVM_REG_S390_TODPR;
169 reg.addr = (__u64)&(env->todpr);
170 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
171 if (ret < 0) {
172 return ret;
175 env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_NONE;
177 /* Do we need to save more than that? */
178 if (level == KVM_PUT_RUNTIME_STATE) {
179 return 0;
182 if (cap_sync_regs &&
183 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
184 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
185 for (i = 0; i < 16; i++) {
186 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
187 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
189 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
190 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
191 } else {
192 for (i = 0; i < 16; i++) {
193 sregs.acrs[i] = env->aregs[i];
194 sregs.crs[i] = env->cregs[i];
196 ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
197 if (ret < 0) {
198 return ret;
202 /* Finally the prefix */
203 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
204 cs->kvm_run->s.regs.prefix = env->psa;
205 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
206 } else {
207 /* prefix is only supported via sync regs */
209 return 0;
212 int kvm_arch_get_registers(CPUState *cs)
214 S390CPU *cpu = S390_CPU(cs);
215 CPUS390XState *env = &cpu->env;
216 struct kvm_one_reg reg;
217 int r;
219 r = kvm_s390_get_registers_partial(cs);
220 if (r < 0) {
221 return r;
224 reg.id = KVM_REG_S390_CPU_TIMER;
225 reg.addr = (__u64)&(env->cputm);
226 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
227 if (r < 0) {
228 return r;
231 reg.id = KVM_REG_S390_CLOCK_COMP;
232 reg.addr = (__u64)&(env->ckc);
233 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
234 if (r < 0) {
235 return r;
238 reg.id = KVM_REG_S390_TODPR;
239 reg.addr = (__u64)&(env->todpr);
240 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
241 if (r < 0) {
242 return r;
245 env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_FULL;
246 return 0;
249 int kvm_s390_get_registers_partial(CPUState *cs)
251 S390CPU *cpu = S390_CPU(cs);
252 CPUS390XState *env = &cpu->env;
253 struct kvm_sregs sregs;
254 struct kvm_regs regs;
255 int ret;
256 int i;
258 if (env->runtime_reg_dirty_mask) {
259 return 0;
262 /* get the PSW */
263 env->psw.addr = cs->kvm_run->psw_addr;
264 env->psw.mask = cs->kvm_run->psw_mask;
266 /* the GPRS */
267 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
268 for (i = 0; i < 16; i++) {
269 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
271 } else {
272 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
273 if (ret < 0) {
274 return ret;
276 for (i = 0; i < 16; i++) {
277 env->regs[i] = regs.gprs[i];
281 /* The ACRS and CRS */
282 if (cap_sync_regs &&
283 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
284 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
285 for (i = 0; i < 16; i++) {
286 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
287 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
289 } else {
290 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
291 if (ret < 0) {
292 return ret;
294 for (i = 0; i < 16; i++) {
295 env->aregs[i] = sregs.acrs[i];
296 env->cregs[i] = sregs.crs[i];
300 /* Finally the prefix */
301 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
302 env->psa = cs->kvm_run->s.regs.prefix;
303 } else {
304 /* no prefix without sync regs */
307 env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_PARTIAL;
308 return 0;
312 * Legacy layout for s390:
313 * Older S390 KVM requires the topmost vma of the RAM to be
314 * smaller than an system defined value, which is at least 256GB.
315 * Larger systems have larger values. We put the guest between
316 * the end of data segment (system break) and this value. We
317 * use 32GB as a base to have enough room for the system break
318 * to grow. We also have to use MAP parameters that avoid
319 * read-only mapping of guest pages.
321 static void *legacy_s390_alloc(ram_addr_t size)
323 void *mem;
325 mem = mmap((void *) 0x800000000ULL, size,
326 PROT_EXEC|PROT_READ|PROT_WRITE,
327 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
328 if (mem == MAP_FAILED) {
329 fprintf(stderr, "Allocating RAM failed\n");
330 abort();
332 return mem;
335 void *kvm_arch_ram_alloc(ram_addr_t size)
337 /* Can we use the standard allocation ? */
338 if (kvm_check_extension(kvm_state, KVM_CAP_S390_GMAP) &&
339 kvm_check_extension(kvm_state, KVM_CAP_S390_COW)) {
340 return NULL;
341 } else {
342 return legacy_s390_alloc(size);
346 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
348 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
350 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
351 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501, 4, 1)) {
352 return -EINVAL;
354 return 0;
357 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
359 uint8_t t[4];
360 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
362 if (cpu_memory_rw_debug(cs, bp->pc, t, 4, 0)) {
363 return -EINVAL;
364 } else if (memcmp(t, diag_501, 4)) {
365 return -EINVAL;
366 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
367 return -EINVAL;
370 return 0;
373 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
377 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
381 int kvm_arch_process_async_events(CPUState *cs)
383 return cs->halted;
386 void kvm_s390_interrupt_internal(S390CPU *cpu, int type, uint32_t parm,
387 uint64_t parm64, int vm)
389 CPUState *cs = CPU(cpu);
390 struct kvm_s390_interrupt kvmint;
391 int r;
393 if (!cs->kvm_state) {
394 return;
397 kvmint.type = type;
398 kvmint.parm = parm;
399 kvmint.parm64 = parm64;
401 if (vm) {
402 r = kvm_vm_ioctl(cs->kvm_state, KVM_S390_INTERRUPT, &kvmint);
403 } else {
404 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
407 if (r < 0) {
408 fprintf(stderr, "KVM failed to inject interrupt\n");
409 exit(1);
413 void kvm_s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token)
415 kvm_s390_interrupt_internal(cpu, KVM_S390_INT_VIRTIO, config_change,
416 token, 1);
419 void kvm_s390_interrupt(S390CPU *cpu, int type, uint32_t code)
421 kvm_s390_interrupt_internal(cpu, type, code, 0, 0);
424 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
426 kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code);
429 static inline void setcc(S390CPU *cpu, uint64_t cc)
431 CPUS390XState *env = &cpu->env;
432 CPUState *cs = CPU(cpu);
434 cs->kvm_run->psw_mask &= ~(3ull << 44);
435 cs->kvm_run->psw_mask |= (cc & 3) << 44;
437 env->psw.mask &= ~(3ul << 44);
438 env->psw.mask |= (cc & 3) << 44;
441 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
442 uint16_t ipbh0)
444 CPUS390XState *env = &cpu->env;
445 uint32_t sccb;
446 uint64_t code;
447 int r = 0;
449 cpu_synchronize_state(CPU(cpu));
450 sccb = env->regs[ipbh0 & 0xf];
451 code = env->regs[(ipbh0 & 0xf0) >> 4];
453 r = sclp_service_call(sccb, code);
454 if (r < 0) {
455 enter_pgmcheck(cpu, -r);
457 setcc(cpu, r);
459 return 0;
462 static int kvm_handle_css_inst(S390CPU *cpu, struct kvm_run *run,
463 uint8_t ipa0, uint8_t ipa1, uint8_t ipb)
465 int r = 0;
466 int no_cc = 0;
467 CPUS390XState *env = &cpu->env;
468 CPUState *cs = CPU(cpu);
470 if (ipa0 != 0xb2) {
471 /* Not handled for now. */
472 return -1;
475 kvm_s390_get_registers_partial(cs);
476 cs->kvm_vcpu_dirty = true;
478 switch (ipa1) {
479 case PRIV_XSCH:
480 r = ioinst_handle_xsch(env, env->regs[1]);
481 break;
482 case PRIV_CSCH:
483 r = ioinst_handle_csch(env, env->regs[1]);
484 break;
485 case PRIV_HSCH:
486 r = ioinst_handle_hsch(env, env->regs[1]);
487 break;
488 case PRIV_MSCH:
489 r = ioinst_handle_msch(env, env->regs[1], run->s390_sieic.ipb);
490 break;
491 case PRIV_SSCH:
492 r = ioinst_handle_ssch(env, env->regs[1], run->s390_sieic.ipb);
493 break;
494 case PRIV_STCRW:
495 r = ioinst_handle_stcrw(env, run->s390_sieic.ipb);
496 break;
497 case PRIV_STSCH:
498 r = ioinst_handle_stsch(env, env->regs[1], run->s390_sieic.ipb);
499 break;
500 case PRIV_TSCH:
501 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
502 fprintf(stderr, "Spurious tsch intercept\n");
503 break;
504 case PRIV_CHSC:
505 r = ioinst_handle_chsc(env, run->s390_sieic.ipb);
506 break;
507 case PRIV_TPI:
508 /* This should have been handled by kvm already. */
509 fprintf(stderr, "Spurious tpi intercept\n");
510 break;
511 case PRIV_SCHM:
512 no_cc = 1;
513 r = ioinst_handle_schm(env, env->regs[1], env->regs[2],
514 run->s390_sieic.ipb);
515 break;
516 case PRIV_RSCH:
517 r = ioinst_handle_rsch(env, env->regs[1]);
518 break;
519 case PRIV_RCHP:
520 r = ioinst_handle_rchp(env, env->regs[1]);
521 break;
522 case PRIV_STCPS:
523 /* We do not provide this instruction, it is suppressed. */
524 no_cc = 1;
525 r = 0;
526 break;
527 case PRIV_SAL:
528 no_cc = 1;
529 r = ioinst_handle_sal(env, env->regs[1]);
530 break;
531 default:
532 r = -1;
533 break;
536 if (r >= 0) {
537 if (!no_cc) {
538 setcc(cpu, r);
540 r = 0;
541 } else if (r < -1) {
542 r = 0;
544 return r;
547 static int is_ioinst(uint8_t ipa0, uint8_t ipa1, uint8_t ipb)
549 int ret = 0;
550 uint16_t ipa = (ipa0 << 8) | ipa1;
552 switch (ipa) {
553 case IPA0_B2 | PRIV_CSCH:
554 case IPA0_B2 | PRIV_HSCH:
555 case IPA0_B2 | PRIV_MSCH:
556 case IPA0_B2 | PRIV_SSCH:
557 case IPA0_B2 | PRIV_STSCH:
558 case IPA0_B2 | PRIV_TPI:
559 case IPA0_B2 | PRIV_SAL:
560 case IPA0_B2 | PRIV_RSCH:
561 case IPA0_B2 | PRIV_STCRW:
562 case IPA0_B2 | PRIV_STCPS:
563 case IPA0_B2 | PRIV_RCHP:
564 case IPA0_B2 | PRIV_SCHM:
565 case IPA0_B2 | PRIV_CHSC:
566 case IPA0_B2 | PRIV_SIGA:
567 case IPA0_B2 | PRIV_XSCH:
568 case IPA0_B9 | PRIV_EQBS:
569 case IPA0_EB | PRIV_SQBS:
570 ret = 1;
571 break;
574 return ret;
577 static int handle_priv(S390CPU *cpu, struct kvm_run *run,
578 uint8_t ipa0, uint8_t ipa1)
580 int r = 0;
581 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
582 uint8_t ipb = run->s390_sieic.ipb & 0xff;
584 dprintf("KVM: PRIV: %d\n", ipa1);
585 switch (ipa1) {
586 case PRIV_SCLP_CALL:
587 r = kvm_sclp_service_call(cpu, run, ipbh0);
588 break;
589 default:
590 if (is_ioinst(ipa0, ipa1, ipb)) {
591 r = kvm_handle_css_inst(cpu, run, ipa0, ipa1, ipb);
592 if (r == -1) {
593 setcc(cpu, 3);
594 r = 0;
596 } else {
597 dprintf("KVM: unknown PRIV: 0x%x\n", ipa1);
598 r = -1;
600 break;
603 return r;
606 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
608 CPUState *cs = CPU(cpu);
609 CPUS390XState *env = &cpu->env;
611 kvm_s390_get_registers_partial(cs);
612 cs->kvm_vcpu_dirty = true;
613 env->regs[2] = s390_virtio_hypercall(env);
615 return 0;
618 static int handle_diag(S390CPU *cpu, struct kvm_run *run, int ipb_code)
620 int r = 0;
622 switch (ipb_code) {
623 case DIAG_KVM_HYPERCALL:
624 r = handle_hypercall(cpu, run);
625 break;
626 case DIAG_KVM_BREAKPOINT:
627 sleep(10);
628 break;
629 default:
630 dprintf("KVM: unknown DIAG: 0x%x\n", ipb_code);
631 r = -1;
632 break;
635 return r;
638 static int s390_cpu_restart(S390CPU *cpu)
640 kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0);
641 s390_add_running_cpu(cpu);
642 qemu_cpu_kick(CPU(cpu));
643 dprintf("DONE: SIGP cpu restart: %p\n", &cpu->env);
644 return 0;
647 static int s390_store_status(CPUS390XState *env, uint32_t parameter)
649 /* XXX */
650 fprintf(stderr, "XXX SIGP store status\n");
651 return -1;
654 static int s390_cpu_initial_reset(S390CPU *cpu)
656 CPUState *cs = CPU(cpu);
657 CPUS390XState *env = &cpu->env;
658 int i;
660 s390_del_running_cpu(cpu);
661 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL) < 0) {
662 perror("cannot init reset vcpu");
665 /* Manually zero out all registers */
666 cpu_synchronize_state(cs);
667 for (i = 0; i < 16; i++) {
668 env->regs[i] = 0;
671 dprintf("DONE: SIGP initial reset: %p\n", env);
672 return 0;
675 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
677 CPUS390XState *env = &cpu->env;
678 uint8_t order_code;
679 uint32_t parameter;
680 uint16_t cpu_addr;
681 uint8_t t;
682 int r = -1;
683 S390CPU *target_cpu;
684 CPUS390XState *target_env;
686 cpu_synchronize_state(CPU(cpu));
688 /* get order code */
689 order_code = run->s390_sieic.ipb >> 28;
690 if (order_code > 0) {
691 order_code = env->regs[order_code];
693 order_code += (run->s390_sieic.ipb & 0x0fff0000) >> 16;
695 /* get parameters */
696 t = (ipa1 & 0xf0) >> 4;
697 if (!(t % 2)) {
698 t++;
701 parameter = env->regs[t] & 0x7ffffe00;
702 cpu_addr = env->regs[ipa1 & 0x0f];
704 target_cpu = s390_cpu_addr2state(cpu_addr);
705 if (target_cpu == NULL) {
706 goto out;
708 target_env = &target_cpu->env;
710 switch (order_code) {
711 case SIGP_RESTART:
712 r = s390_cpu_restart(target_cpu);
713 break;
714 case SIGP_STORE_STATUS_ADDR:
715 r = s390_store_status(target_env, parameter);
716 break;
717 case SIGP_SET_ARCH:
718 /* make the caller panic */
719 return -1;
720 case SIGP_INITIAL_CPU_RESET:
721 r = s390_cpu_initial_reset(target_cpu);
722 break;
723 default:
724 fprintf(stderr, "KVM: unknown SIGP: 0x%x\n", order_code);
725 break;
728 out:
729 setcc(cpu, r ? 3 : 0);
730 return 0;
733 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
735 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
736 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
737 int ipb_code = (run->s390_sieic.ipb & 0x0fff0000) >> 16;
738 int r = -1;
740 dprintf("handle_instruction 0x%x 0x%x\n", run->s390_sieic.ipa, run->s390_sieic.ipb);
741 switch (ipa0) {
742 case IPA0_B2:
743 case IPA0_B9:
744 case IPA0_EB:
745 r = handle_priv(cpu, run, ipa0 >> 8, ipa1);
746 break;
747 case IPA0_DIAG:
748 r = handle_diag(cpu, run, ipb_code);
749 break;
750 case IPA0_SIGP:
751 r = handle_sigp(cpu, run, ipa1);
752 break;
755 if (r < 0) {
756 enter_pgmcheck(cpu, 0x0001);
758 return 0;
761 static bool is_special_wait_psw(CPUState *cs)
763 /* signal quiesce */
764 return cs->kvm_run->psw_addr == 0xfffUL;
767 static int handle_intercept(S390CPU *cpu)
769 CPUState *cs = CPU(cpu);
770 struct kvm_run *run = cs->kvm_run;
771 int icpt_code = run->s390_sieic.icptcode;
772 int r = 0;
774 dprintf("intercept: 0x%x (at 0x%lx)\n", icpt_code,
775 (long)cs->kvm_run->psw_addr);
776 switch (icpt_code) {
777 case ICPT_INSTRUCTION:
778 r = handle_instruction(cpu, run);
779 break;
780 case ICPT_WAITPSW:
781 /* disabled wait, since enabled wait is handled in kernel */
782 if (s390_del_running_cpu(cpu) == 0) {
783 if (is_special_wait_psw(cs)) {
784 qemu_system_shutdown_request();
785 } else {
786 QObject *data;
788 data = qobject_from_jsonf("{ 'action': %s }", "pause");
789 monitor_protocol_event(QEVENT_GUEST_PANICKED, data);
790 qobject_decref(data);
791 vm_stop(RUN_STATE_GUEST_PANICKED);
794 r = EXCP_HALTED;
795 break;
796 case ICPT_CPU_STOP:
797 if (s390_del_running_cpu(cpu) == 0) {
798 qemu_system_shutdown_request();
800 r = EXCP_HALTED;
801 break;
802 case ICPT_SOFT_INTERCEPT:
803 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
804 exit(1);
805 break;
806 case ICPT_IO:
807 fprintf(stderr, "KVM unimplemented icpt IO\n");
808 exit(1);
809 break;
810 default:
811 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
812 exit(1);
813 break;
816 return r;
819 static int handle_tsch(S390CPU *cpu)
821 CPUS390XState *env = &cpu->env;
822 CPUState *cs = CPU(cpu);
823 struct kvm_run *run = cs->kvm_run;
824 int ret;
826 kvm_s390_get_registers_partial(cs);
827 cs->kvm_vcpu_dirty = true;
829 ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
830 if (ret >= 0) {
831 /* Success; set condition code. */
832 setcc(cpu, ret);
833 ret = 0;
834 } else if (ret < -1) {
836 * Failure.
837 * If an I/O interrupt had been dequeued, we have to reinject it.
839 if (run->s390_tsch.dequeued) {
840 uint16_t subchannel_id = run->s390_tsch.subchannel_id;
841 uint16_t subchannel_nr = run->s390_tsch.subchannel_nr;
842 uint32_t io_int_parm = run->s390_tsch.io_int_parm;
843 uint32_t io_int_word = run->s390_tsch.io_int_word;
844 uint32_t type = ((subchannel_id & 0xff00) << 24) |
845 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
847 kvm_s390_interrupt_internal(cpu, type,
848 ((uint32_t)subchannel_id << 16)
849 | subchannel_nr,
850 ((uint64_t)io_int_parm << 32)
851 | io_int_word, 1);
853 ret = 0;
855 return ret;
858 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
860 S390CPU *cpu = S390_CPU(cs);
861 int ret = 0;
863 switch (run->exit_reason) {
864 case KVM_EXIT_S390_SIEIC:
865 ret = handle_intercept(cpu);
866 break;
867 case KVM_EXIT_S390_RESET:
868 qemu_system_reset_request();
869 break;
870 case KVM_EXIT_S390_TSCH:
871 ret = handle_tsch(cpu);
872 break;
873 default:
874 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
875 break;
878 if (ret == 0) {
879 ret = EXCP_INTERRUPT;
881 return ret;
884 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
886 return true;
889 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
891 return 1;
894 int kvm_arch_on_sigbus(int code, void *addr)
896 return 1;
899 void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id,
900 uint16_t subchannel_nr, uint32_t io_int_parm,
901 uint32_t io_int_word)
903 uint32_t type;
905 type = ((subchannel_id & 0xff00) << 24) |
906 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
907 kvm_s390_interrupt_internal(cpu, type,
908 ((uint32_t)subchannel_id << 16) | subchannel_nr,
909 ((uint64_t)io_int_parm << 32) | io_int_word, 1);
912 void kvm_s390_crw_mchk(S390CPU *cpu)
914 kvm_s390_interrupt_internal(cpu, KVM_S390_MCHK, 1 << 28,
915 0x00400f1d40330000, 1);
918 void kvm_s390_enable_css_support(S390CPU *cpu)
920 struct kvm_enable_cap cap = {};
921 int r;
923 /* Activate host kernel channel subsystem support. */
924 cap.cap = KVM_CAP_S390_CSS_SUPPORT;
925 r = kvm_vcpu_ioctl(CPU(cpu), KVM_ENABLE_CAP, &cap);
926 assert(r == 0);
929 void kvm_arch_init_irq_routing(KVMState *s)
933 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
934 int vq, bool assign)
936 struct kvm_ioeventfd kick = {
937 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
938 KVM_IOEVENTFD_FLAG_DATAMATCH,
939 .fd = event_notifier_get_fd(notifier),
940 .datamatch = vq,
941 .addr = sch,
942 .len = 8,
944 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
945 return -ENOSYS;
947 if (!assign) {
948 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
950 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);