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