exec: Simplify the guest physical memory allocation hook
[sdk/emulator/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 #include "qapi/qmp/qjson.h"
38 #include "monitor/monitor.h"
39
40 /* #define DEBUG_KVM */
41
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
49
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
55
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
78
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
84
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
89
90 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
91     KVM_CAP_LAST_INFO
92 };
93
94 static int cap_sync_regs;
95
96 static void *legacy_s390_alloc(ram_addr_t size);
97
98 int kvm_arch_init(KVMState *s)
99 {
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);
104     }
105     return 0;
106 }
107
108 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
109 {
110     return cpu->cpu_index;
111 }
112
113 int kvm_arch_init_vcpu(CPUState *cpu)
114 {
115     /* nothing todo yet */
116     return 0;
117 }
118
119 void kvm_arch_reset_vcpu(CPUState *cpu)
120 {
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");
128     }
129 }
130
131 int kvm_arch_put_registers(CPUState *cs, int level)
132 {
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;
140
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;
144
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;
149         }
150     } else {
151         for (i = 0; i < 16; i++) {
152             regs.gprs[i] = env->regs[i];
153         }
154         ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
155         if (ret < 0) {
156             return ret;
157         }
158     }
159
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;
166         }
167
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;
173         }
174
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;
180         }
181     }
182     env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_NONE;
183
184     /* Do we need to save more than that? */
185     if (level == KVM_PUT_RUNTIME_STATE) {
186         return 0;
187     }
188
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];
195         }
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];
202         }
203         ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
204         if (ret < 0) {
205             return ret;
206         }
207     }
208
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 */
215     }
216     return 0;
217 }
218
219 int kvm_arch_get_registers(CPUState *cs)
220 {
221     S390CPU *cpu = S390_CPU(cs);
222     CPUS390XState *env = &cpu->env;
223     struct kvm_one_reg reg;
224     int r;
225
226     r = kvm_s390_get_registers_partial(cs);
227     if (r < 0) {
228         return r;
229     }
230
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;
236     }
237
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;
243     }
244
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;
250     }
251
252     env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_FULL;
253     return 0;
254 }
255
256 int kvm_s390_get_registers_partial(CPUState *cs)
257 {
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;
264
265     if (env->runtime_reg_dirty_mask) {
266         return 0;
267     }
268
269     /* get the PSW */
270     env->psw.addr = cs->kvm_run->psw_addr;
271     env->psw.mask = cs->kvm_run->psw_mask;
272
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];
277         }
278     } else {
279         ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
280         if (ret < 0) {
281             return ret;
282         }
283          for (i = 0; i < 16; i++) {
284             env->regs[i] = regs.gprs[i];
285         }
286     }
287
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];
295         }
296     } else {
297         ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
298         if (ret < 0) {
299             return ret;
300         }
301          for (i = 0; i < 16; i++) {
302             env->aregs[i] = sregs.acrs[i];
303             env->cregs[i] = sregs.crs[i];
304         }
305     }
306
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 */
312     }
313
314     env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_PARTIAL;
315     return 0;
316 }
317
318 /*
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.
327  */
328 static void *legacy_s390_alloc(ram_addr_t size)
329 {
330     void *mem;
331
332     mem = mmap((void *) 0x800000000ULL, size,
333                PROT_EXEC|PROT_READ|PROT_WRITE,
334                MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
335     if (mem == MAP_FAILED) {
336         fprintf(stderr, "Allocating RAM failed\n");
337         abort();
338     }
339     return mem;
340 }
341
342 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
343 {
344     static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
345
346     if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
347         cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501, 4, 1)) {
348         return -EINVAL;
349     }
350     return 0;
351 }
352
353 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
354 {
355     uint8_t t[4];
356     static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
357
358     if (cpu_memory_rw_debug(cs, bp->pc, t, 4, 0)) {
359         return -EINVAL;
360     } else if (memcmp(t, diag_501, 4)) {
361         return -EINVAL;
362     } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
363         return -EINVAL;
364     }
365
366     return 0;
367 }
368
369 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
370 {
371 }
372
373 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
374 {
375 }
376
377 int kvm_arch_process_async_events(CPUState *cs)
378 {
379     return cs->halted;
380 }
381
382 void kvm_s390_interrupt_internal(S390CPU *cpu, int type, uint32_t parm,
383                                  uint64_t parm64, int vm)
384 {
385     CPUState *cs = CPU(cpu);
386     struct kvm_s390_interrupt kvmint;
387     int r;
388
389     if (!cs->kvm_state) {
390         return;
391     }
392
393     kvmint.type = type;
394     kvmint.parm = parm;
395     kvmint.parm64 = parm64;
396
397     if (vm) {
398         r = kvm_vm_ioctl(cs->kvm_state, KVM_S390_INTERRUPT, &kvmint);
399     } else {
400         r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
401     }
402
403     if (r < 0) {
404         fprintf(stderr, "KVM failed to inject interrupt\n");
405         exit(1);
406     }
407 }
408
409 void kvm_s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token)
410 {
411     kvm_s390_interrupt_internal(cpu, KVM_S390_INT_VIRTIO, config_change,
412                                 token, 1);
413 }
414
415 void kvm_s390_interrupt(S390CPU *cpu, int type, uint32_t code)
416 {
417     kvm_s390_interrupt_internal(cpu, type, code, 0, 0);
418 }
419
420 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
421 {
422     kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code);
423 }
424
425 static inline void setcc(S390CPU *cpu, uint64_t cc)
426 {
427     CPUS390XState *env = &cpu->env;
428     CPUState *cs = CPU(cpu);
429
430     cs->kvm_run->psw_mask &= ~(3ull << 44);
431     cs->kvm_run->psw_mask |= (cc & 3) << 44;
432
433     env->psw.mask &= ~(3ul << 44);
434     env->psw.mask |= (cc & 3) << 44;
435 }
436
437 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
438                                  uint16_t ipbh0)
439 {
440     CPUS390XState *env = &cpu->env;
441     uint32_t sccb;
442     uint64_t code;
443     int r = 0;
444
445     cpu_synchronize_state(CPU(cpu));
446     sccb = env->regs[ipbh0 & 0xf];
447     code = env->regs[(ipbh0 & 0xf0) >> 4];
448
449     r = sclp_service_call(sccb, code);
450     if (r < 0) {
451         enter_pgmcheck(cpu, -r);
452     }
453     setcc(cpu, r);
454
455     return 0;
456 }
457
458 static int kvm_handle_css_inst(S390CPU *cpu, struct kvm_run *run,
459                                uint8_t ipa0, uint8_t ipa1, uint8_t ipb)
460 {
461     int r = 0;
462     int no_cc = 0;
463     CPUS390XState *env = &cpu->env;
464     CPUState *cs = CPU(cpu);
465
466     if (ipa0 != 0xb2) {
467         /* Not handled for now. */
468         return -1;
469     }
470
471     kvm_s390_get_registers_partial(cs);
472     cs->kvm_vcpu_dirty = true;
473
474     switch (ipa1) {
475     case PRIV_XSCH:
476         r = ioinst_handle_xsch(env, env->regs[1]);
477         break;
478     case PRIV_CSCH:
479         r = ioinst_handle_csch(env, env->regs[1]);
480         break;
481     case PRIV_HSCH:
482         r = ioinst_handle_hsch(env, env->regs[1]);
483         break;
484     case PRIV_MSCH:
485         r = ioinst_handle_msch(env, env->regs[1], run->s390_sieic.ipb);
486         break;
487     case PRIV_SSCH:
488         r = ioinst_handle_ssch(env, env->regs[1], run->s390_sieic.ipb);
489         break;
490     case PRIV_STCRW:
491         r = ioinst_handle_stcrw(env, run->s390_sieic.ipb);
492         break;
493     case PRIV_STSCH:
494         r = ioinst_handle_stsch(env, env->regs[1], run->s390_sieic.ipb);
495         break;
496     case PRIV_TSCH:
497         /* We should only get tsch via KVM_EXIT_S390_TSCH. */
498         fprintf(stderr, "Spurious tsch intercept\n");
499         break;
500     case PRIV_CHSC:
501         r = ioinst_handle_chsc(env, run->s390_sieic.ipb);
502         break;
503     case PRIV_TPI:
504         /* This should have been handled by kvm already. */
505         fprintf(stderr, "Spurious tpi intercept\n");
506         break;
507     case PRIV_SCHM:
508         no_cc = 1;
509         r = ioinst_handle_schm(env, env->regs[1], env->regs[2],
510                                run->s390_sieic.ipb);
511         break;
512     case PRIV_RSCH:
513         r = ioinst_handle_rsch(env, env->regs[1]);
514         break;
515     case PRIV_RCHP:
516         r = ioinst_handle_rchp(env, env->regs[1]);
517         break;
518     case PRIV_STCPS:
519         /* We do not provide this instruction, it is suppressed. */
520         no_cc = 1;
521         r = 0;
522         break;
523     case PRIV_SAL:
524         no_cc = 1;
525         r = ioinst_handle_sal(env, env->regs[1]);
526         break;
527     case PRIV_SIGA:
528         /* Not provided, set CC = 3 for subchannel not operational */
529         r = 3;
530         break;
531     default:
532         return -1;
533     }
534
535     if (r >= 0 && !no_cc) {
536         setcc(cpu, r);
537     }
538
539     return 0;
540 }
541
542 static int handle_priv(S390CPU *cpu, struct kvm_run *run,
543                        uint8_t ipa0, uint8_t ipa1)
544 {
545     int r = 0;
546     uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
547     uint8_t ipb = run->s390_sieic.ipb & 0xff;
548
549     DPRINTF("KVM: PRIV: %d\n", ipa1);
550     switch (ipa1) {
551         case PRIV_SCLP_CALL:
552             r = kvm_sclp_service_call(cpu, run, ipbh0);
553             break;
554         default:
555             r = kvm_handle_css_inst(cpu, run, ipa0, ipa1, ipb);
556             if (r == -1) {
557                 DPRINTF("KVM: unhandled PRIV: 0x%x\n", ipa1);
558             }
559             break;
560     }
561
562     return r;
563 }
564
565 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
566 {
567     CPUState *cs = CPU(cpu);
568     CPUS390XState *env = &cpu->env;
569
570     kvm_s390_get_registers_partial(cs);
571     cs->kvm_vcpu_dirty = true;
572     env->regs[2] = s390_virtio_hypercall(env);
573
574     return 0;
575 }
576
577 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
578 {
579     uint64_t r1, r3;
580
581     cpu_synchronize_state(CPU(cpu));
582     r1 = (run->s390_sieic.ipa & 0x00f0) >> 8;
583     r3 = run->s390_sieic.ipa & 0x000f;
584     handle_diag_308(&cpu->env, r1, r3);
585 }
586
587 static int handle_diag(S390CPU *cpu, struct kvm_run *run, int ipb_code)
588 {
589     int r = 0;
590
591     switch (ipb_code) {
592     case DIAG_IPL:
593         kvm_handle_diag_308(cpu, run);
594         break;
595     case DIAG_KVM_HYPERCALL:
596         r = handle_hypercall(cpu, run);
597         break;
598     case DIAG_KVM_BREAKPOINT:
599         sleep(10);
600         break;
601     default:
602         DPRINTF("KVM: unknown DIAG: 0x%x\n", ipb_code);
603         r = -1;
604         break;
605     }
606
607     return r;
608 }
609
610 int kvm_s390_cpu_restart(S390CPU *cpu)
611 {
612     kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0);
613     s390_add_running_cpu(cpu);
614     qemu_cpu_kick(CPU(cpu));
615     DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
616     return 0;
617 }
618
619 static int s390_store_status(CPUS390XState *env, uint32_t parameter)
620 {
621     /* XXX */
622     fprintf(stderr, "XXX SIGP store status\n");
623     return -1;
624 }
625
626 static int s390_cpu_initial_reset(S390CPU *cpu)
627 {
628     CPUState *cs = CPU(cpu);
629     CPUS390XState *env = &cpu->env;
630     int i;
631
632     s390_del_running_cpu(cpu);
633     if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL) < 0) {
634         perror("cannot init reset vcpu");
635     }
636
637     /* Manually zero out all registers */
638     cpu_synchronize_state(cs);
639     for (i = 0; i < 16; i++) {
640         env->regs[i] = 0;
641     }
642
643     DPRINTF("DONE: SIGP initial reset: %p\n", env);
644     return 0;
645 }
646
647 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
648 {
649     CPUS390XState *env = &cpu->env;
650     uint8_t order_code;
651     uint32_t parameter;
652     uint16_t cpu_addr;
653     uint8_t t;
654     int r = -1;
655     S390CPU *target_cpu;
656     CPUS390XState *target_env;
657
658     cpu_synchronize_state(CPU(cpu));
659
660     /* get order code */
661     order_code = run->s390_sieic.ipb >> 28;
662     if (order_code > 0) {
663         order_code = env->regs[order_code];
664     }
665     order_code += (run->s390_sieic.ipb & 0x0fff0000) >> 16;
666
667     /* get parameters */
668     t = (ipa1 & 0xf0) >> 4;
669     if (!(t % 2)) {
670         t++;
671     }
672
673     parameter = env->regs[t] & 0x7ffffe00;
674     cpu_addr = env->regs[ipa1 & 0x0f];
675
676     target_cpu = s390_cpu_addr2state(cpu_addr);
677     if (target_cpu == NULL) {
678         goto out;
679     }
680     target_env = &target_cpu->env;
681
682     switch (order_code) {
683         case SIGP_RESTART:
684             r = kvm_s390_cpu_restart(target_cpu);
685             break;
686         case SIGP_STORE_STATUS_ADDR:
687             r = s390_store_status(target_env, parameter);
688             break;
689         case SIGP_SET_ARCH:
690             /* make the caller panic */
691             return -1;
692         case SIGP_INITIAL_CPU_RESET:
693             r = s390_cpu_initial_reset(target_cpu);
694             break;
695         default:
696             fprintf(stderr, "KVM: unknown SIGP: 0x%x\n", order_code);
697             break;
698     }
699
700 out:
701     setcc(cpu, r ? 3 : 0);
702     return 0;
703 }
704
705 static void handle_instruction(S390CPU *cpu, struct kvm_run *run)
706 {
707     unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
708     uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
709     int ipb_code = (run->s390_sieic.ipb & 0x0fff0000) >> 16;
710     int r = -1;
711
712     DPRINTF("handle_instruction 0x%x 0x%x\n",
713             run->s390_sieic.ipa, run->s390_sieic.ipb);
714     switch (ipa0) {
715     case IPA0_B2:
716     case IPA0_B9:
717     case IPA0_EB:
718         r = handle_priv(cpu, run, ipa0 >> 8, ipa1);
719         break;
720     case IPA0_DIAG:
721         r = handle_diag(cpu, run, ipb_code);
722         break;
723     case IPA0_SIGP:
724         r = handle_sigp(cpu, run, ipa1);
725         break;
726     }
727
728     if (r < 0) {
729         enter_pgmcheck(cpu, 0x0001);
730     }
731 }
732
733 static bool is_special_wait_psw(CPUState *cs)
734 {
735     /* signal quiesce */
736     return cs->kvm_run->psw_addr == 0xfffUL;
737 }
738
739 static int handle_intercept(S390CPU *cpu)
740 {
741     CPUState *cs = CPU(cpu);
742     struct kvm_run *run = cs->kvm_run;
743     int icpt_code = run->s390_sieic.icptcode;
744     int r = 0;
745
746     DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
747             (long)cs->kvm_run->psw_addr);
748     switch (icpt_code) {
749         case ICPT_INSTRUCTION:
750             handle_instruction(cpu, run);
751             break;
752         case ICPT_WAITPSW:
753             /* disabled wait, since enabled wait is handled in kernel */
754             if (s390_del_running_cpu(cpu) == 0) {
755                 if (is_special_wait_psw(cs)) {
756                     qemu_system_shutdown_request();
757                 } else {
758                     QObject *data;
759
760                     data = qobject_from_jsonf("{ 'action': %s }", "pause");
761                     monitor_protocol_event(QEVENT_GUEST_PANICKED, data);
762                     qobject_decref(data);
763                     vm_stop(RUN_STATE_GUEST_PANICKED);
764                 }
765             }
766             r = EXCP_HALTED;
767             break;
768         case ICPT_CPU_STOP:
769             if (s390_del_running_cpu(cpu) == 0) {
770                 qemu_system_shutdown_request();
771             }
772             r = EXCP_HALTED;
773             break;
774         case ICPT_SOFT_INTERCEPT:
775             fprintf(stderr, "KVM unimplemented icpt SOFT\n");
776             exit(1);
777             break;
778         case ICPT_IO:
779             fprintf(stderr, "KVM unimplemented icpt IO\n");
780             exit(1);
781             break;
782         default:
783             fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
784             exit(1);
785             break;
786     }
787
788     return r;
789 }
790
791 static int handle_tsch(S390CPU *cpu)
792 {
793     CPUS390XState *env = &cpu->env;
794     CPUState *cs = CPU(cpu);
795     struct kvm_run *run = cs->kvm_run;
796     int ret;
797
798     kvm_s390_get_registers_partial(cs);
799     cs->kvm_vcpu_dirty = true;
800
801     ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
802     if (ret >= 0) {
803         /* Success; set condition code. */
804         setcc(cpu, ret);
805         ret = 0;
806     } else if (ret < -1) {
807         /*
808          * Failure.
809          * If an I/O interrupt had been dequeued, we have to reinject it.
810          */
811         if (run->s390_tsch.dequeued) {
812             uint16_t subchannel_id = run->s390_tsch.subchannel_id;
813             uint16_t subchannel_nr = run->s390_tsch.subchannel_nr;
814             uint32_t io_int_parm = run->s390_tsch.io_int_parm;
815             uint32_t io_int_word = run->s390_tsch.io_int_word;
816             uint32_t type = ((subchannel_id & 0xff00) << 24) |
817                 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
818
819             kvm_s390_interrupt_internal(cpu, type,
820                                         ((uint32_t)subchannel_id << 16)
821                                         | subchannel_nr,
822                                         ((uint64_t)io_int_parm << 32)
823                                         | io_int_word, 1);
824         }
825         ret = 0;
826     }
827     return ret;
828 }
829
830 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
831 {
832     S390CPU *cpu = S390_CPU(cs);
833     int ret = 0;
834
835     switch (run->exit_reason) {
836         case KVM_EXIT_S390_SIEIC:
837             ret = handle_intercept(cpu);
838             break;
839         case KVM_EXIT_S390_RESET:
840             qemu_system_reset_request();
841             break;
842         case KVM_EXIT_S390_TSCH:
843             ret = handle_tsch(cpu);
844             break;
845         default:
846             fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
847             break;
848     }
849
850     if (ret == 0) {
851         ret = EXCP_INTERRUPT;
852     }
853     return ret;
854 }
855
856 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
857 {
858     return true;
859 }
860
861 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
862 {
863     return 1;
864 }
865
866 int kvm_arch_on_sigbus(int code, void *addr)
867 {
868     return 1;
869 }
870
871 void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id,
872                            uint16_t subchannel_nr, uint32_t io_int_parm,
873                            uint32_t io_int_word)
874 {
875     uint32_t type;
876
877     type = ((subchannel_id & 0xff00) << 24) |
878         ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
879     kvm_s390_interrupt_internal(cpu, type,
880                                 ((uint32_t)subchannel_id << 16) | subchannel_nr,
881                                 ((uint64_t)io_int_parm << 32) | io_int_word, 1);
882 }
883
884 void kvm_s390_crw_mchk(S390CPU *cpu)
885 {
886     kvm_s390_interrupt_internal(cpu, KVM_S390_MCHK, 1 << 28,
887                                 0x00400f1d40330000, 1);
888 }
889
890 void kvm_s390_enable_css_support(S390CPU *cpu)
891 {
892     struct kvm_enable_cap cap = {};
893     int r;
894
895     /* Activate host kernel channel subsystem support. */
896     cap.cap = KVM_CAP_S390_CSS_SUPPORT;
897     r = kvm_vcpu_ioctl(CPU(cpu), KVM_ENABLE_CAP, &cap);
898     assert(r == 0);
899 }
900
901 void kvm_arch_init_irq_routing(KVMState *s)
902 {
903 }
904
905 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
906                                     int vq, bool assign)
907 {
908     struct kvm_ioeventfd kick = {
909         .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
910         KVM_IOEVENTFD_FLAG_DATAMATCH,
911         .fd = event_notifier_get_fd(notifier),
912         .datamatch = vq,
913         .addr = sch,
914         .len = 8,
915     };
916     if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
917         return -ENOSYS;
918     }
919     if (!assign) {
920         kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
921     }
922     return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
923 }