]> Git Repo - qemu.git/blob - target-s390x/kvm.c
kvm: extend kvm_irqchip_add_msi_route to work on s390
[qemu.git] / target-s390x / kvm.c
1 /*
2  * QEMU S390x KVM implementation
3  *
4  * Copyright (c) 2009 Alexander Graf <[email protected]>
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 "hw/hw.h"
36 #include "cpu.h"
37 #include "sysemu/device_tree.h"
38 #include "qapi/qmp/qjson.h"
39 #include "monitor/monitor.h"
40 #include "exec/gdbstub.h"
41 #include "trace.h"
42 #include "qapi-event.h"
43 #include "hw/s390x/s390-pci-inst.h"
44 #include "hw/s390x/s390-pci-bus.h"
45
46 /* #define DEBUG_KVM */
47
48 #ifdef DEBUG_KVM
49 #define DPRINTF(fmt, ...) \
50     do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
51 #else
52 #define DPRINTF(fmt, ...) \
53     do { } while (0)
54 #endif
55
56 #define IPA0_DIAG                       0x8300
57 #define IPA0_SIGP                       0xae00
58 #define IPA0_B2                         0xb200
59 #define IPA0_B9                         0xb900
60 #define IPA0_EB                         0xeb00
61 #define IPA0_E3                         0xe300
62
63 #define PRIV_B2_SCLP_CALL               0x20
64 #define PRIV_B2_CSCH                    0x30
65 #define PRIV_B2_HSCH                    0x31
66 #define PRIV_B2_MSCH                    0x32
67 #define PRIV_B2_SSCH                    0x33
68 #define PRIV_B2_STSCH                   0x34
69 #define PRIV_B2_TSCH                    0x35
70 #define PRIV_B2_TPI                     0x36
71 #define PRIV_B2_SAL                     0x37
72 #define PRIV_B2_RSCH                    0x38
73 #define PRIV_B2_STCRW                   0x39
74 #define PRIV_B2_STCPS                   0x3a
75 #define PRIV_B2_RCHP                    0x3b
76 #define PRIV_B2_SCHM                    0x3c
77 #define PRIV_B2_CHSC                    0x5f
78 #define PRIV_B2_SIGA                    0x74
79 #define PRIV_B2_XSCH                    0x76
80
81 #define PRIV_EB_SQBS                    0x8a
82 #define PRIV_EB_PCISTB                  0xd0
83 #define PRIV_EB_SIC                     0xd1
84
85 #define PRIV_B9_EQBS                    0x9c
86 #define PRIV_B9_CLP                     0xa0
87 #define PRIV_B9_PCISTG                  0xd0
88 #define PRIV_B9_PCILG                   0xd2
89 #define PRIV_B9_RPCIT                   0xd3
90
91 #define PRIV_E3_MPCIFC                  0xd0
92 #define PRIV_E3_STPCIFC                 0xd4
93
94 #define DIAG_IPL                        0x308
95 #define DIAG_KVM_HYPERCALL              0x500
96 #define DIAG_KVM_BREAKPOINT             0x501
97
98 #define ICPT_INSTRUCTION                0x04
99 #define ICPT_PROGRAM                    0x08
100 #define ICPT_EXT_INT                    0x14
101 #define ICPT_WAITPSW                    0x1c
102 #define ICPT_SOFT_INTERCEPT             0x24
103 #define ICPT_CPU_STOP                   0x28
104 #define ICPT_IO                         0x40
105
106 static CPUWatchpoint hw_watchpoint;
107 /*
108  * We don't use a list because this structure is also used to transmit the
109  * hardware breakpoints to the kernel.
110  */
111 static struct kvm_hw_breakpoint *hw_breakpoints;
112 static int nb_hw_breakpoints;
113
114 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
115     KVM_CAP_LAST_INFO
116 };
117
118 static int cap_sync_regs;
119 static int cap_async_pf;
120
121 static void *legacy_s390_alloc(size_t size, uint64_t *align);
122
123 static int kvm_s390_check_clear_cmma(KVMState *s)
124 {
125     struct kvm_device_attr attr = {
126         .group = KVM_S390_VM_MEM_CTRL,
127         .attr = KVM_S390_VM_MEM_CLR_CMMA,
128     };
129
130     return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
131 }
132
133 static int kvm_s390_check_enable_cmma(KVMState *s)
134 {
135     struct kvm_device_attr attr = {
136         .group = KVM_S390_VM_MEM_CTRL,
137         .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
138     };
139
140     return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
141 }
142
143 void kvm_s390_clear_cmma_callback(void *opaque)
144 {
145     int rc;
146     KVMState *s = opaque;
147     struct kvm_device_attr attr = {
148         .group = KVM_S390_VM_MEM_CTRL,
149         .attr = KVM_S390_VM_MEM_CLR_CMMA,
150     };
151
152     rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
153     trace_kvm_clear_cmma(rc);
154 }
155
156 static void kvm_s390_enable_cmma(KVMState *s)
157 {
158     int rc;
159     struct kvm_device_attr attr = {
160         .group = KVM_S390_VM_MEM_CTRL,
161         .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
162     };
163
164     if (kvm_s390_check_enable_cmma(s) || kvm_s390_check_clear_cmma(s)) {
165         return;
166     }
167
168     rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
169     if (!rc) {
170         qemu_register_reset(kvm_s390_clear_cmma_callback, s);
171     }
172     trace_kvm_enable_cmma(rc);
173 }
174
175 int kvm_arch_init(KVMState *s)
176 {
177     cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
178     cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
179
180     if (kvm_check_extension(s, KVM_CAP_VM_ATTRIBUTES)) {
181         kvm_s390_enable_cmma(s);
182     }
183
184     if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
185         || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
186         phys_mem_set_alloc(legacy_s390_alloc);
187     }
188     return 0;
189 }
190
191 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
192 {
193     return cpu->cpu_index;
194 }
195
196 int kvm_arch_init_vcpu(CPUState *cs)
197 {
198     S390CPU *cpu = S390_CPU(cs);
199     kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
200     return 0;
201 }
202
203 void kvm_s390_reset_vcpu(S390CPU *cpu)
204 {
205     CPUState *cs = CPU(cpu);
206
207     /* The initial reset call is needed here to reset in-kernel
208      * vcpu data that we can't access directly from QEMU
209      * (i.e. with older kernels which don't support sync_regs/ONE_REG).
210      * Before this ioctl cpu_synchronize_state() is called in common kvm
211      * code (kvm-all) */
212     if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
213         error_report("Initial CPU reset failed on CPU %i\n", cs->cpu_index);
214     }
215 }
216
217 static int can_sync_regs(CPUState *cs, int regs)
218 {
219     return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
220 }
221
222 int kvm_arch_put_registers(CPUState *cs, int level)
223 {
224     S390CPU *cpu = S390_CPU(cs);
225     CPUS390XState *env = &cpu->env;
226     struct kvm_sregs sregs;
227     struct kvm_regs regs;
228     struct kvm_fpu fpu = {};
229     int r;
230     int i;
231
232     /* always save the PSW  and the GPRS*/
233     cs->kvm_run->psw_addr = env->psw.addr;
234     cs->kvm_run->psw_mask = env->psw.mask;
235
236     if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
237         for (i = 0; i < 16; i++) {
238             cs->kvm_run->s.regs.gprs[i] = env->regs[i];
239             cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
240         }
241     } else {
242         for (i = 0; i < 16; i++) {
243             regs.gprs[i] = env->regs[i];
244         }
245         r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
246         if (r < 0) {
247             return r;
248         }
249     }
250
251     /* Floating point */
252     for (i = 0; i < 16; i++) {
253         fpu.fprs[i] = env->fregs[i].ll;
254     }
255     fpu.fpc = env->fpc;
256
257     r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
258     if (r < 0) {
259         return r;
260     }
261
262     /* Do we need to save more than that? */
263     if (level == KVM_PUT_RUNTIME_STATE) {
264         return 0;
265     }
266
267     if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
268         cs->kvm_run->s.regs.cputm = env->cputm;
269         cs->kvm_run->s.regs.ckc = env->ckc;
270         cs->kvm_run->s.regs.todpr = env->todpr;
271         cs->kvm_run->s.regs.gbea = env->gbea;
272         cs->kvm_run->s.regs.pp = env->pp;
273         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
274     } else {
275         /*
276          * These ONE_REGS are not protected by a capability. As they are only
277          * necessary for migration we just trace a possible error, but don't
278          * return with an error return code.
279          */
280         kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
281         kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
282         kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
283         kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
284         kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
285     }
286
287     /* pfault parameters */
288     if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
289         cs->kvm_run->s.regs.pft = env->pfault_token;
290         cs->kvm_run->s.regs.pfs = env->pfault_select;
291         cs->kvm_run->s.regs.pfc = env->pfault_compare;
292         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
293     } else if (cap_async_pf) {
294         r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
295         if (r < 0) {
296             return r;
297         }
298         r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
299         if (r < 0) {
300             return r;
301         }
302         r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
303         if (r < 0) {
304             return r;
305         }
306     }
307
308     /* access registers and control registers*/
309     if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
310         for (i = 0; i < 16; i++) {
311             cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
312             cs->kvm_run->s.regs.crs[i] = env->cregs[i];
313         }
314         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
315         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
316     } else {
317         for (i = 0; i < 16; i++) {
318             sregs.acrs[i] = env->aregs[i];
319             sregs.crs[i] = env->cregs[i];
320         }
321         r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
322         if (r < 0) {
323             return r;
324         }
325     }
326
327     /* Finally the prefix */
328     if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
329         cs->kvm_run->s.regs.prefix = env->psa;
330         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
331     } else {
332         /* prefix is only supported via sync regs */
333     }
334     return 0;
335 }
336
337 int kvm_arch_get_registers(CPUState *cs)
338 {
339     S390CPU *cpu = S390_CPU(cs);
340     CPUS390XState *env = &cpu->env;
341     struct kvm_sregs sregs;
342     struct kvm_regs regs;
343     struct kvm_fpu fpu;
344     int i, r;
345
346     /* get the PSW */
347     env->psw.addr = cs->kvm_run->psw_addr;
348     env->psw.mask = cs->kvm_run->psw_mask;
349
350     /* the GPRS */
351     if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
352         for (i = 0; i < 16; i++) {
353             env->regs[i] = cs->kvm_run->s.regs.gprs[i];
354         }
355     } else {
356         r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
357         if (r < 0) {
358             return r;
359         }
360          for (i = 0; i < 16; i++) {
361             env->regs[i] = regs.gprs[i];
362         }
363     }
364
365     /* The ACRS and CRS */
366     if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
367         for (i = 0; i < 16; i++) {
368             env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
369             env->cregs[i] = cs->kvm_run->s.regs.crs[i];
370         }
371     } else {
372         r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
373         if (r < 0) {
374             return r;
375         }
376          for (i = 0; i < 16; i++) {
377             env->aregs[i] = sregs.acrs[i];
378             env->cregs[i] = sregs.crs[i];
379         }
380     }
381
382     /* Floating point */
383     r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
384     if (r < 0) {
385         return r;
386     }
387     for (i = 0; i < 16; i++) {
388         env->fregs[i].ll = fpu.fprs[i];
389     }
390     env->fpc = fpu.fpc;
391
392     /* The prefix */
393     if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
394         env->psa = cs->kvm_run->s.regs.prefix;
395     }
396
397     if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
398         env->cputm = cs->kvm_run->s.regs.cputm;
399         env->ckc = cs->kvm_run->s.regs.ckc;
400         env->todpr = cs->kvm_run->s.regs.todpr;
401         env->gbea = cs->kvm_run->s.regs.gbea;
402         env->pp = cs->kvm_run->s.regs.pp;
403     } else {
404         /*
405          * These ONE_REGS are not protected by a capability. As they are only
406          * necessary for migration we just trace a possible error, but don't
407          * return with an error return code.
408          */
409         kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
410         kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
411         kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
412         kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
413         kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
414     }
415
416     /* pfault parameters */
417     if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
418         env->pfault_token = cs->kvm_run->s.regs.pft;
419         env->pfault_select = cs->kvm_run->s.regs.pfs;
420         env->pfault_compare = cs->kvm_run->s.regs.pfc;
421     } else if (cap_async_pf) {
422         r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
423         if (r < 0) {
424             return r;
425         }
426         r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
427         if (r < 0) {
428             return r;
429         }
430         r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
431         if (r < 0) {
432             return r;
433         }
434     }
435
436     return 0;
437 }
438
439 /*
440  * Legacy layout for s390:
441  * Older S390 KVM requires the topmost vma of the RAM to be
442  * smaller than an system defined value, which is at least 256GB.
443  * Larger systems have larger values. We put the guest between
444  * the end of data segment (system break) and this value. We
445  * use 32GB as a base to have enough room for the system break
446  * to grow. We also have to use MAP parameters that avoid
447  * read-only mapping of guest pages.
448  */
449 static void *legacy_s390_alloc(size_t size, uint64_t *align)
450 {
451     void *mem;
452
453     mem = mmap((void *) 0x800000000ULL, size,
454                PROT_EXEC|PROT_READ|PROT_WRITE,
455                MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
456     return mem == MAP_FAILED ? NULL : mem;
457 }
458
459 /* DIAG 501 is used for sw breakpoints */
460 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
461
462 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
463 {
464
465     if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
466                             sizeof(diag_501), 0) ||
467         cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
468                             sizeof(diag_501), 1)) {
469         return -EINVAL;
470     }
471     return 0;
472 }
473
474 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
475 {
476     uint8_t t[sizeof(diag_501)];
477
478     if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
479         return -EINVAL;
480     } else if (memcmp(t, diag_501, sizeof(diag_501))) {
481         return -EINVAL;
482     } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
483                                    sizeof(diag_501), 1)) {
484         return -EINVAL;
485     }
486
487     return 0;
488 }
489
490 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
491                                                     int len, int type)
492 {
493     int n;
494
495     for (n = 0; n < nb_hw_breakpoints; n++) {
496         if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
497             (hw_breakpoints[n].len == len || len == -1)) {
498             return &hw_breakpoints[n];
499         }
500     }
501
502     return NULL;
503 }
504
505 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
506 {
507     int size;
508
509     if (find_hw_breakpoint(addr, len, type)) {
510         return -EEXIST;
511     }
512
513     size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
514
515     if (!hw_breakpoints) {
516         nb_hw_breakpoints = 0;
517         hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
518     } else {
519         hw_breakpoints =
520             (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
521     }
522
523     if (!hw_breakpoints) {
524         nb_hw_breakpoints = 0;
525         return -ENOMEM;
526     }
527
528     hw_breakpoints[nb_hw_breakpoints].addr = addr;
529     hw_breakpoints[nb_hw_breakpoints].len = len;
530     hw_breakpoints[nb_hw_breakpoints].type = type;
531
532     nb_hw_breakpoints++;
533
534     return 0;
535 }
536
537 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
538                                   target_ulong len, int type)
539 {
540     switch (type) {
541     case GDB_BREAKPOINT_HW:
542         type = KVM_HW_BP;
543         break;
544     case GDB_WATCHPOINT_WRITE:
545         if (len < 1) {
546             return -EINVAL;
547         }
548         type = KVM_HW_WP_WRITE;
549         break;
550     default:
551         return -ENOSYS;
552     }
553     return insert_hw_breakpoint(addr, len, type);
554 }
555
556 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
557                                   target_ulong len, int type)
558 {
559     int size;
560     struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
561
562     if (bp == NULL) {
563         return -ENOENT;
564     }
565
566     nb_hw_breakpoints--;
567     if (nb_hw_breakpoints > 0) {
568         /*
569          * In order to trim the array, move the last element to the position to
570          * be removed - if necessary.
571          */
572         if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
573             *bp = hw_breakpoints[nb_hw_breakpoints];
574         }
575         size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
576         hw_breakpoints =
577              (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
578     } else {
579         g_free(hw_breakpoints);
580         hw_breakpoints = NULL;
581     }
582
583     return 0;
584 }
585
586 void kvm_arch_remove_all_hw_breakpoints(void)
587 {
588     nb_hw_breakpoints = 0;
589     g_free(hw_breakpoints);
590     hw_breakpoints = NULL;
591 }
592
593 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
594 {
595     int i;
596
597     if (nb_hw_breakpoints > 0) {
598         dbg->arch.nr_hw_bp = nb_hw_breakpoints;
599         dbg->arch.hw_bp = hw_breakpoints;
600
601         for (i = 0; i < nb_hw_breakpoints; ++i) {
602             hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
603                                                        hw_breakpoints[i].addr);
604         }
605         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
606     } else {
607         dbg->arch.nr_hw_bp = 0;
608         dbg->arch.hw_bp = NULL;
609     }
610 }
611
612 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
613 {
614 }
615
616 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
617 {
618 }
619
620 int kvm_arch_process_async_events(CPUState *cs)
621 {
622     return cs->halted;
623 }
624
625 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
626                                      struct kvm_s390_interrupt *interrupt)
627 {
628     int r = 0;
629
630     interrupt->type = irq->type;
631     switch (irq->type) {
632     case KVM_S390_INT_VIRTIO:
633         interrupt->parm = irq->u.ext.ext_params;
634         /* fall through */
635     case KVM_S390_INT_PFAULT_INIT:
636     case KVM_S390_INT_PFAULT_DONE:
637         interrupt->parm64 = irq->u.ext.ext_params2;
638         break;
639     case KVM_S390_PROGRAM_INT:
640         interrupt->parm = irq->u.pgm.code;
641         break;
642     case KVM_S390_SIGP_SET_PREFIX:
643         interrupt->parm = irq->u.prefix.address;
644         break;
645     case KVM_S390_INT_SERVICE:
646         interrupt->parm = irq->u.ext.ext_params;
647         break;
648     case KVM_S390_MCHK:
649         interrupt->parm = irq->u.mchk.cr14;
650         interrupt->parm64 = irq->u.mchk.mcic;
651         break;
652     case KVM_S390_INT_EXTERNAL_CALL:
653         interrupt->parm = irq->u.extcall.code;
654         break;
655     case KVM_S390_INT_EMERGENCY:
656         interrupt->parm = irq->u.emerg.code;
657         break;
658     case KVM_S390_SIGP_STOP:
659     case KVM_S390_RESTART:
660         break; /* These types have no parameters */
661     case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
662         interrupt->parm = irq->u.io.subchannel_id << 16;
663         interrupt->parm |= irq->u.io.subchannel_nr;
664         interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
665         interrupt->parm64 |= irq->u.io.io_int_word;
666         break;
667     default:
668         r = -EINVAL;
669         break;
670     }
671     return r;
672 }
673
674 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
675 {
676     struct kvm_s390_interrupt kvmint = {};
677     CPUState *cs = CPU(cpu);
678     int r;
679
680     r = s390_kvm_irq_to_interrupt(irq, &kvmint);
681     if (r < 0) {
682         fprintf(stderr, "%s called with bogus interrupt\n", __func__);
683         exit(1);
684     }
685
686     r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
687     if (r < 0) {
688         fprintf(stderr, "KVM failed to inject interrupt\n");
689         exit(1);
690     }
691 }
692
693 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
694 {
695     struct kvm_s390_interrupt kvmint = {};
696     int r;
697
698     r = s390_kvm_irq_to_interrupt(irq, &kvmint);
699     if (r < 0) {
700         fprintf(stderr, "%s called with bogus interrupt\n", __func__);
701         exit(1);
702     }
703
704     r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
705     if (r < 0) {
706         fprintf(stderr, "KVM failed to inject interrupt\n");
707         exit(1);
708     }
709 }
710
711 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
712 {
713     static bool use_flic = true;
714     int r;
715
716     if (use_flic) {
717         r = kvm_s390_inject_flic(irq);
718         if (r == -ENOSYS) {
719             use_flic = false;
720         }
721         if (!r) {
722             return;
723         }
724     }
725     __kvm_s390_floating_interrupt(irq);
726 }
727
728 void kvm_s390_virtio_irq(int config_change, uint64_t token)
729 {
730     struct kvm_s390_irq irq = {
731         .type = KVM_S390_INT_VIRTIO,
732         .u.ext.ext_params = config_change,
733         .u.ext.ext_params2 = token,
734     };
735
736     kvm_s390_floating_interrupt(&irq);
737 }
738
739 void kvm_s390_service_interrupt(uint32_t parm)
740 {
741     struct kvm_s390_irq irq = {
742         .type = KVM_S390_INT_SERVICE,
743         .u.ext.ext_params = parm,
744     };
745
746     kvm_s390_floating_interrupt(&irq);
747 }
748
749 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
750 {
751     struct kvm_s390_irq irq = {
752         .type = KVM_S390_PROGRAM_INT,
753         .u.pgm.code = code,
754     };
755
756     kvm_s390_vcpu_interrupt(cpu, &irq);
757 }
758
759 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
760                                  uint16_t ipbh0)
761 {
762     CPUS390XState *env = &cpu->env;
763     uint64_t sccb;
764     uint32_t code;
765     int r = 0;
766
767     cpu_synchronize_state(CPU(cpu));
768     sccb = env->regs[ipbh0 & 0xf];
769     code = env->regs[(ipbh0 & 0xf0) >> 4];
770
771     r = sclp_service_call(env, sccb, code);
772     if (r < 0) {
773         enter_pgmcheck(cpu, -r);
774     } else {
775         setcc(cpu, r);
776     }
777
778     return 0;
779 }
780
781 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
782 {
783     CPUS390XState *env = &cpu->env;
784     int rc = 0;
785     uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
786
787     cpu_synchronize_state(CPU(cpu));
788
789     switch (ipa1) {
790     case PRIV_B2_XSCH:
791         ioinst_handle_xsch(cpu, env->regs[1]);
792         break;
793     case PRIV_B2_CSCH:
794         ioinst_handle_csch(cpu, env->regs[1]);
795         break;
796     case PRIV_B2_HSCH:
797         ioinst_handle_hsch(cpu, env->regs[1]);
798         break;
799     case PRIV_B2_MSCH:
800         ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
801         break;
802     case PRIV_B2_SSCH:
803         ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
804         break;
805     case PRIV_B2_STCRW:
806         ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
807         break;
808     case PRIV_B2_STSCH:
809         ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
810         break;
811     case PRIV_B2_TSCH:
812         /* We should only get tsch via KVM_EXIT_S390_TSCH. */
813         fprintf(stderr, "Spurious tsch intercept\n");
814         break;
815     case PRIV_B2_CHSC:
816         ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
817         break;
818     case PRIV_B2_TPI:
819         /* This should have been handled by kvm already. */
820         fprintf(stderr, "Spurious tpi intercept\n");
821         break;
822     case PRIV_B2_SCHM:
823         ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
824                            run->s390_sieic.ipb);
825         break;
826     case PRIV_B2_RSCH:
827         ioinst_handle_rsch(cpu, env->regs[1]);
828         break;
829     case PRIV_B2_RCHP:
830         ioinst_handle_rchp(cpu, env->regs[1]);
831         break;
832     case PRIV_B2_STCPS:
833         /* We do not provide this instruction, it is suppressed. */
834         break;
835     case PRIV_B2_SAL:
836         ioinst_handle_sal(cpu, env->regs[1]);
837         break;
838     case PRIV_B2_SIGA:
839         /* Not provided, set CC = 3 for subchannel not operational */
840         setcc(cpu, 3);
841         break;
842     case PRIV_B2_SCLP_CALL:
843         rc = kvm_sclp_service_call(cpu, run, ipbh0);
844         break;
845     default:
846         rc = -1;
847         DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
848         break;
849     }
850
851     return rc;
852 }
853
854 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run)
855 {
856     CPUS390XState *env = &cpu->env;
857     uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
858     uint32_t base2 = run->s390_sieic.ipb >> 28;
859     uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
860                      ((run->s390_sieic.ipb & 0xff00) << 4);
861
862     if (disp2 & 0x80000) {
863         disp2 += 0xfff00000;
864     }
865
866     return (base2 ? env->regs[base2] : 0) +
867            (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
868 }
869
870 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run)
871 {
872     CPUS390XState *env = &cpu->env;
873     uint32_t base2 = run->s390_sieic.ipb >> 28;
874     uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
875                      ((run->s390_sieic.ipb & 0xff00) << 4);
876
877     if (disp2 & 0x80000) {
878         disp2 += 0xfff00000;
879     }
880
881     return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
882 }
883
884 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
885 {
886     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
887
888     return clp_service_call(cpu, r2);
889 }
890
891 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
892 {
893     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
894     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
895
896     return pcilg_service_call(cpu, r1, r2);
897 }
898
899 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
900 {
901     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
902     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
903
904     return pcistg_service_call(cpu, r1, r2);
905 }
906
907 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
908 {
909     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
910     uint64_t fiba;
911
912     cpu_synchronize_state(CPU(cpu));
913     fiba = get_base_disp_rxy(cpu, run);
914
915     return stpcifc_service_call(cpu, r1, fiba);
916 }
917
918 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
919 {
920     /* NOOP */
921     return 0;
922 }
923
924 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
925 {
926     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
927     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
928
929     return rpcit_service_call(cpu, r1, r2);
930 }
931
932 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
933 {
934     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
935     uint8_t r3 = run->s390_sieic.ipa & 0x000f;
936     uint64_t gaddr;
937
938     cpu_synchronize_state(CPU(cpu));
939     gaddr = get_base_disp_rsy(cpu, run);
940
941     return pcistb_service_call(cpu, r1, r3, gaddr);
942 }
943
944 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
945 {
946     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
947     uint64_t fiba;
948
949     cpu_synchronize_state(CPU(cpu));
950     fiba = get_base_disp_rxy(cpu, run);
951
952     return mpcifc_service_call(cpu, r1, fiba);
953 }
954
955 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
956 {
957     int r = 0;
958
959     switch (ipa1) {
960     case PRIV_B9_CLP:
961         r = kvm_clp_service_call(cpu, run);
962         break;
963     case PRIV_B9_PCISTG:
964         r = kvm_pcistg_service_call(cpu, run);
965         break;
966     case PRIV_B9_PCILG:
967         r = kvm_pcilg_service_call(cpu, run);
968         break;
969     case PRIV_B9_RPCIT:
970         r = kvm_rpcit_service_call(cpu, run);
971         break;
972     case PRIV_B9_EQBS:
973         /* just inject exception */
974         r = -1;
975         break;
976     default:
977         r = -1;
978         DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
979         break;
980     }
981
982     return r;
983 }
984
985 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
986 {
987     int r = 0;
988
989     switch (ipbl) {
990     case PRIV_EB_PCISTB:
991         r = kvm_pcistb_service_call(cpu, run);
992         break;
993     case PRIV_EB_SIC:
994         r = kvm_sic_service_call(cpu, run);
995         break;
996     case PRIV_EB_SQBS:
997         /* just inject exception */
998         r = -1;
999         break;
1000     default:
1001         r = -1;
1002         DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1003         break;
1004     }
1005
1006     return r;
1007 }
1008
1009 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1010 {
1011     int r = 0;
1012
1013     switch (ipbl) {
1014     case PRIV_E3_MPCIFC:
1015         r = kvm_mpcifc_service_call(cpu, run);
1016         break;
1017     case PRIV_E3_STPCIFC:
1018         r = kvm_stpcifc_service_call(cpu, run);
1019         break;
1020     default:
1021         r = -1;
1022         DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1023         break;
1024     }
1025
1026     return r;
1027 }
1028
1029 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1030 {
1031     CPUS390XState *env = &cpu->env;
1032     int ret;
1033
1034     cpu_synchronize_state(CPU(cpu));
1035     ret = s390_virtio_hypercall(env);
1036     if (ret == -EINVAL) {
1037         enter_pgmcheck(cpu, PGM_SPECIFICATION);
1038         return 0;
1039     }
1040
1041     return ret;
1042 }
1043
1044 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1045 {
1046     uint64_t r1, r3;
1047
1048     cpu_synchronize_state(CPU(cpu));
1049     r1 = (run->s390_sieic.ipa & 0x00f0) >> 8;
1050     r3 = run->s390_sieic.ipa & 0x000f;
1051     handle_diag_308(&cpu->env, r1, r3);
1052 }
1053
1054 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1055 {
1056     CPUS390XState *env = &cpu->env;
1057     unsigned long pc;
1058
1059     cpu_synchronize_state(CPU(cpu));
1060
1061     pc = env->psw.addr - 4;
1062     if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1063         env->psw.addr = pc;
1064         return EXCP_DEBUG;
1065     }
1066
1067     return -ENOENT;
1068 }
1069
1070 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1071
1072 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1073 {
1074     int r = 0;
1075     uint16_t func_code;
1076
1077     /*
1078      * For any diagnose call we support, bits 48-63 of the resulting
1079      * address specify the function code; the remainder is ignored.
1080      */
1081     func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
1082     switch (func_code) {
1083     case DIAG_IPL:
1084         kvm_handle_diag_308(cpu, run);
1085         break;
1086     case DIAG_KVM_HYPERCALL:
1087         r = handle_hypercall(cpu, run);
1088         break;
1089     case DIAG_KVM_BREAKPOINT:
1090         r = handle_sw_breakpoint(cpu, run);
1091         break;
1092     default:
1093         DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1094         r = -1;
1095         break;
1096     }
1097
1098     return r;
1099 }
1100
1101 static void sigp_cpu_start(void *arg)
1102 {
1103     CPUState *cs = arg;
1104     S390CPU *cpu = S390_CPU(cs);
1105
1106     s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1107     DPRINTF("DONE: KVM cpu start: %p\n", &cpu->env);
1108 }
1109
1110 static void sigp_cpu_restart(void *arg)
1111 {
1112     CPUState *cs = arg;
1113     S390CPU *cpu = S390_CPU(cs);
1114     struct kvm_s390_irq irq = {
1115         .type = KVM_S390_RESTART,
1116     };
1117
1118     kvm_s390_vcpu_interrupt(cpu, &irq);
1119     s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
1120 }
1121
1122 int kvm_s390_cpu_restart(S390CPU *cpu)
1123 {
1124     run_on_cpu(CPU(cpu), sigp_cpu_restart, CPU(cpu));
1125     DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1126     return 0;
1127 }
1128
1129 static void sigp_initial_cpu_reset(void *arg)
1130 {
1131     CPUState *cpu = arg;
1132     S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1133
1134     cpu_synchronize_state(cpu);
1135     scc->initial_cpu_reset(cpu);
1136     cpu_synchronize_post_reset(cpu);
1137 }
1138
1139 static void sigp_cpu_reset(void *arg)
1140 {
1141     CPUState *cpu = arg;
1142     S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
1143
1144     cpu_synchronize_state(cpu);
1145     scc->cpu_reset(cpu);
1146     cpu_synchronize_post_reset(cpu);
1147 }
1148
1149 #define SIGP_ORDER_MASK 0x000000ff
1150
1151 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1152 {
1153     CPUS390XState *env = &cpu->env;
1154     uint8_t order_code;
1155     uint16_t cpu_addr;
1156     S390CPU *target_cpu;
1157     uint64_t *statusreg = &env->regs[ipa1 >> 4];
1158     int cc;
1159
1160     cpu_synchronize_state(CPU(cpu));
1161
1162     /* get order code */
1163     order_code = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
1164
1165     cpu_addr = env->regs[ipa1 & 0x0f];
1166     target_cpu = s390_cpu_addr2state(cpu_addr);
1167     if (target_cpu == NULL) {
1168         cc = 3;    /* not operational */
1169         goto out;
1170     }
1171
1172     switch (order_code) {
1173     case SIGP_START:
1174         run_on_cpu(CPU(target_cpu), sigp_cpu_start, CPU(target_cpu));
1175         cc = 0;
1176         break;
1177     case SIGP_RESTART:
1178         run_on_cpu(CPU(target_cpu), sigp_cpu_restart, CPU(target_cpu));
1179         cc = 0;
1180         break;
1181     case SIGP_SET_ARCH:
1182         *statusreg &= 0xffffffff00000000UL;
1183         *statusreg |= SIGP_STAT_INVALID_PARAMETER;
1184         cc = 1;   /* status stored */
1185         break;
1186     case SIGP_INITIAL_CPU_RESET:
1187         run_on_cpu(CPU(target_cpu), sigp_initial_cpu_reset, CPU(target_cpu));
1188         cc = 0;
1189         break;
1190     case SIGP_CPU_RESET:
1191         run_on_cpu(CPU(target_cpu), sigp_cpu_reset, CPU(target_cpu));
1192         cc = 0;
1193         break;
1194     default:
1195         DPRINTF("KVM: unknown SIGP: 0x%x\n", order_code);
1196         *statusreg &= 0xffffffff00000000UL;
1197         *statusreg |= SIGP_STAT_INVALID_ORDER;
1198         cc = 1;   /* status stored */
1199         break;
1200     }
1201
1202 out:
1203     setcc(cpu, cc);
1204     return 0;
1205 }
1206
1207 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1208 {
1209     unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1210     uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1211     int r = -1;
1212
1213     DPRINTF("handle_instruction 0x%x 0x%x\n",
1214             run->s390_sieic.ipa, run->s390_sieic.ipb);
1215     switch (ipa0) {
1216     case IPA0_B2:
1217         r = handle_b2(cpu, run, ipa1);
1218         break;
1219     case IPA0_B9:
1220         r = handle_b9(cpu, run, ipa1);
1221         break;
1222     case IPA0_EB:
1223         r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1224         break;
1225     case IPA0_E3:
1226         r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1227         break;
1228     case IPA0_DIAG:
1229         r = handle_diag(cpu, run, run->s390_sieic.ipb);
1230         break;
1231     case IPA0_SIGP:
1232         r = handle_sigp(cpu, run, ipa1);
1233         break;
1234     }
1235
1236     if (r < 0) {
1237         r = 0;
1238         enter_pgmcheck(cpu, 0x0001);
1239     }
1240
1241     return r;
1242 }
1243
1244 static bool is_special_wait_psw(CPUState *cs)
1245 {
1246     /* signal quiesce */
1247     return cs->kvm_run->psw_addr == 0xfffUL;
1248 }
1249
1250 static void guest_panicked(void)
1251 {
1252     qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1253                                    &error_abort);
1254     vm_stop(RUN_STATE_GUEST_PANICKED);
1255 }
1256
1257 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1258 {
1259     CPUState *cs = CPU(cpu);
1260
1261     error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1262                  str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1263                  ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1264     s390_cpu_halt(cpu);
1265     guest_panicked();
1266 }
1267
1268 static int handle_intercept(S390CPU *cpu)
1269 {
1270     CPUState *cs = CPU(cpu);
1271     struct kvm_run *run = cs->kvm_run;
1272     int icpt_code = run->s390_sieic.icptcode;
1273     int r = 0;
1274
1275     DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1276             (long)cs->kvm_run->psw_addr);
1277     switch (icpt_code) {
1278         case ICPT_INSTRUCTION:
1279             r = handle_instruction(cpu, run);
1280             break;
1281         case ICPT_PROGRAM:
1282             unmanageable_intercept(cpu, "program interrupt",
1283                                    offsetof(LowCore, program_new_psw));
1284             r = EXCP_HALTED;
1285             break;
1286         case ICPT_EXT_INT:
1287             unmanageable_intercept(cpu, "external interrupt",
1288                                    offsetof(LowCore, external_new_psw));
1289             r = EXCP_HALTED;
1290             break;
1291         case ICPT_WAITPSW:
1292             /* disabled wait, since enabled wait is handled in kernel */
1293             cpu_synchronize_state(cs);
1294             if (s390_cpu_halt(cpu) == 0) {
1295                 if (is_special_wait_psw(cs)) {
1296                     qemu_system_shutdown_request();
1297                 } else {
1298                     guest_panicked();
1299                 }
1300             }
1301             r = EXCP_HALTED;
1302             break;
1303         case ICPT_CPU_STOP:
1304             if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1305                 qemu_system_shutdown_request();
1306             }
1307             r = EXCP_HALTED;
1308             break;
1309         case ICPT_SOFT_INTERCEPT:
1310             fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1311             exit(1);
1312             break;
1313         case ICPT_IO:
1314             fprintf(stderr, "KVM unimplemented icpt IO\n");
1315             exit(1);
1316             break;
1317         default:
1318             fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1319             exit(1);
1320             break;
1321     }
1322
1323     return r;
1324 }
1325
1326 static int handle_tsch(S390CPU *cpu)
1327 {
1328     CPUS390XState *env = &cpu->env;
1329     CPUState *cs = CPU(cpu);
1330     struct kvm_run *run = cs->kvm_run;
1331     int ret;
1332
1333     cpu_synchronize_state(cs);
1334
1335     ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
1336     if (ret >= 0) {
1337         /* Success; set condition code. */
1338         setcc(cpu, ret);
1339         ret = 0;
1340     } else if (ret < -1) {
1341         /*
1342          * Failure.
1343          * If an I/O interrupt had been dequeued, we have to reinject it.
1344          */
1345         if (run->s390_tsch.dequeued) {
1346             kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1347                                   run->s390_tsch.subchannel_nr,
1348                                   run->s390_tsch.io_int_parm,
1349                                   run->s390_tsch.io_int_word);
1350         }
1351         ret = 0;
1352     }
1353     return ret;
1354 }
1355
1356 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1357 {
1358     CPUState *cs = CPU(cpu);
1359     struct kvm_run *run = cs->kvm_run;
1360
1361     int ret = 0;
1362     struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1363
1364     switch (arch_info->type) {
1365     case KVM_HW_WP_WRITE:
1366         if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1367             cs->watchpoint_hit = &hw_watchpoint;
1368             hw_watchpoint.vaddr = arch_info->addr;
1369             hw_watchpoint.flags = BP_MEM_WRITE;
1370             ret = EXCP_DEBUG;
1371         }
1372         break;
1373     case KVM_HW_BP:
1374         if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1375             ret = EXCP_DEBUG;
1376         }
1377         break;
1378     case KVM_SINGLESTEP:
1379         if (cs->singlestep_enabled) {
1380             ret = EXCP_DEBUG;
1381         }
1382         break;
1383     default:
1384         ret = -ENOSYS;
1385     }
1386
1387     return ret;
1388 }
1389
1390 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1391 {
1392     S390CPU *cpu = S390_CPU(cs);
1393     int ret = 0;
1394
1395     switch (run->exit_reason) {
1396         case KVM_EXIT_S390_SIEIC:
1397             ret = handle_intercept(cpu);
1398             break;
1399         case KVM_EXIT_S390_RESET:
1400             qemu_system_reset_request();
1401             break;
1402         case KVM_EXIT_S390_TSCH:
1403             ret = handle_tsch(cpu);
1404             break;
1405         case KVM_EXIT_DEBUG:
1406             ret = kvm_arch_handle_debug_exit(cpu);
1407             break;
1408         default:
1409             fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1410             break;
1411     }
1412
1413     if (ret == 0) {
1414         ret = EXCP_INTERRUPT;
1415     }
1416     return ret;
1417 }
1418
1419 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1420 {
1421     return true;
1422 }
1423
1424 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
1425 {
1426     return 1;
1427 }
1428
1429 int kvm_arch_on_sigbus(int code, void *addr)
1430 {
1431     return 1;
1432 }
1433
1434 void kvm_s390_io_interrupt(uint16_t subchannel_id,
1435                            uint16_t subchannel_nr, uint32_t io_int_parm,
1436                            uint32_t io_int_word)
1437 {
1438     struct kvm_s390_irq irq = {
1439         .u.io.subchannel_id = subchannel_id,
1440         .u.io.subchannel_nr = subchannel_nr,
1441         .u.io.io_int_parm = io_int_parm,
1442         .u.io.io_int_word = io_int_word,
1443     };
1444
1445     if (io_int_word & IO_INT_WORD_AI) {
1446         irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
1447     } else {
1448         irq.type = ((subchannel_id & 0xff00) << 24) |
1449             ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
1450     }
1451     kvm_s390_floating_interrupt(&irq);
1452 }
1453
1454 void kvm_s390_crw_mchk(void)
1455 {
1456     struct kvm_s390_irq irq = {
1457         .type = KVM_S390_MCHK,
1458         .u.mchk.cr14 = 1 << 28,
1459         .u.mchk.mcic = 0x00400f1d40330000ULL,
1460     };
1461     kvm_s390_floating_interrupt(&irq);
1462 }
1463
1464 void kvm_s390_enable_css_support(S390CPU *cpu)
1465 {
1466     int r;
1467
1468     /* Activate host kernel channel subsystem support. */
1469     r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1470     assert(r == 0);
1471 }
1472
1473 void kvm_arch_init_irq_routing(KVMState *s)
1474 {
1475     /*
1476      * Note that while irqchip capabilities generally imply that cpustates
1477      * are handled in-kernel, it is not true for s390 (yet); therefore, we
1478      * have to override the common code kvm_halt_in_kernel_allowed setting.
1479      */
1480     if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1481         kvm_gsi_routing_allowed = true;
1482         kvm_halt_in_kernel_allowed = false;
1483     }
1484 }
1485
1486 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1487                                     int vq, bool assign)
1488 {
1489     struct kvm_ioeventfd kick = {
1490         .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1491         KVM_IOEVENTFD_FLAG_DATAMATCH,
1492         .fd = event_notifier_get_fd(notifier),
1493         .datamatch = vq,
1494         .addr = sch,
1495         .len = 8,
1496     };
1497     if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1498         return -ENOSYS;
1499     }
1500     if (!assign) {
1501         kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1502     }
1503     return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1504 }
1505
1506 int kvm_s390_get_memslot_count(KVMState *s)
1507 {
1508     return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
1509 }
1510
1511 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1512 {
1513     struct kvm_mp_state mp_state = {};
1514     int ret;
1515
1516     /* the kvm part might not have been initialized yet */
1517     if (CPU(cpu)->kvm_state == NULL) {
1518         return 0;
1519     }
1520
1521     switch (cpu_state) {
1522     case CPU_STATE_STOPPED:
1523         mp_state.mp_state = KVM_MP_STATE_STOPPED;
1524         break;
1525     case CPU_STATE_CHECK_STOP:
1526         mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1527         break;
1528     case CPU_STATE_OPERATING:
1529         mp_state.mp_state = KVM_MP_STATE_OPERATING;
1530         break;
1531     case CPU_STATE_LOAD:
1532         mp_state.mp_state = KVM_MP_STATE_LOAD;
1533         break;
1534     default:
1535         error_report("Requested CPU state is not a valid S390 CPU state: %u",
1536                      cpu_state);
1537         exit(1);
1538     }
1539
1540     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1541     if (ret) {
1542         trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1543                                        strerror(-ret));
1544     }
1545
1546     return ret;
1547 }
1548
1549 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1550                               uint64_t address, uint32_t data)
1551 {
1552     S390PCIBusDevice *pbdev;
1553     uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
1554     uint32_t vec = data & ZPCI_MSI_VEC_MASK;
1555
1556     pbdev = s390_pci_find_dev_by_fid(fid);
1557     if (!pbdev) {
1558         DPRINTF("add_msi_route no dev\n");
1559         return -ENODEV;
1560     }
1561
1562     pbdev->routes.adapter.ind_offset = vec;
1563
1564     route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
1565     route->flags = 0;
1566     route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
1567     route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
1568     route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
1569     route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
1570     route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
1571     return 0;
1572 }
This page took 0.10872 seconds and 4 git commands to generate.