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