]> Git Repo - qemu.git/blob - target-s390x/kvm.c
s390x/kvm: Support access register mode for KVM_S390_MEM_OP ioctl
[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 "exec/address-spaces.h"
42 #include "trace.h"
43 #include "qapi-event.h"
44 #include "hw/s390x/s390-pci-inst.h"
45 #include "hw/s390x/s390-pci-bus.h"
46 #include "hw/s390x/ipl.h"
47 #include "hw/s390x/ebcdic.h"
48
49 /* #define DEBUG_KVM */
50
51 #ifdef DEBUG_KVM
52 #define DPRINTF(fmt, ...) \
53     do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
54 #else
55 #define DPRINTF(fmt, ...) \
56     do { } while (0)
57 #endif
58
59 #define kvm_vm_check_mem_attr(s, attr) \
60     kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
61
62 #define IPA0_DIAG                       0x8300
63 #define IPA0_SIGP                       0xae00
64 #define IPA0_B2                         0xb200
65 #define IPA0_B9                         0xb900
66 #define IPA0_EB                         0xeb00
67 #define IPA0_E3                         0xe300
68
69 #define PRIV_B2_SCLP_CALL               0x20
70 #define PRIV_B2_CSCH                    0x30
71 #define PRIV_B2_HSCH                    0x31
72 #define PRIV_B2_MSCH                    0x32
73 #define PRIV_B2_SSCH                    0x33
74 #define PRIV_B2_STSCH                   0x34
75 #define PRIV_B2_TSCH                    0x35
76 #define PRIV_B2_TPI                     0x36
77 #define PRIV_B2_SAL                     0x37
78 #define PRIV_B2_RSCH                    0x38
79 #define PRIV_B2_STCRW                   0x39
80 #define PRIV_B2_STCPS                   0x3a
81 #define PRIV_B2_RCHP                    0x3b
82 #define PRIV_B2_SCHM                    0x3c
83 #define PRIV_B2_CHSC                    0x5f
84 #define PRIV_B2_SIGA                    0x74
85 #define PRIV_B2_XSCH                    0x76
86
87 #define PRIV_EB_SQBS                    0x8a
88 #define PRIV_EB_PCISTB                  0xd0
89 #define PRIV_EB_SIC                     0xd1
90
91 #define PRIV_B9_EQBS                    0x9c
92 #define PRIV_B9_CLP                     0xa0
93 #define PRIV_B9_PCISTG                  0xd0
94 #define PRIV_B9_PCILG                   0xd2
95 #define PRIV_B9_RPCIT                   0xd3
96
97 #define PRIV_E3_MPCIFC                  0xd0
98 #define PRIV_E3_STPCIFC                 0xd4
99
100 #define DIAG_IPL                        0x308
101 #define DIAG_KVM_HYPERCALL              0x500
102 #define DIAG_KVM_BREAKPOINT             0x501
103
104 #define ICPT_INSTRUCTION                0x04
105 #define ICPT_PROGRAM                    0x08
106 #define ICPT_EXT_INT                    0x14
107 #define ICPT_WAITPSW                    0x1c
108 #define ICPT_SOFT_INTERCEPT             0x24
109 #define ICPT_CPU_STOP                   0x28
110 #define ICPT_IO                         0x40
111
112 static CPUWatchpoint hw_watchpoint;
113 /*
114  * We don't use a list because this structure is also used to transmit the
115  * hardware breakpoints to the kernel.
116  */
117 static struct kvm_hw_breakpoint *hw_breakpoints;
118 static int nb_hw_breakpoints;
119
120 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
121     KVM_CAP_LAST_INFO
122 };
123
124 static int cap_sync_regs;
125 static int cap_async_pf;
126 static int cap_mem_op;
127
128 static void *legacy_s390_alloc(size_t size, uint64_t *align);
129
130 static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
131 {
132     struct kvm_device_attr attr = {
133         .group = KVM_S390_VM_MEM_CTRL,
134         .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
135         .addr = (uint64_t) memory_limit,
136     };
137
138     return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
139 }
140
141 int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
142 {
143     int rc;
144
145     struct kvm_device_attr attr = {
146         .group = KVM_S390_VM_MEM_CTRL,
147         .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
148         .addr = (uint64_t) &new_limit,
149     };
150
151     if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_LIMIT_SIZE)) {
152         return 0;
153     }
154
155     rc = kvm_s390_query_mem_limit(s, hw_limit);
156     if (rc) {
157         return rc;
158     } else if (*hw_limit < new_limit) {
159         return -E2BIG;
160     }
161
162     return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
163 }
164
165 void kvm_s390_clear_cmma_callback(void *opaque)
166 {
167     int rc;
168     KVMState *s = opaque;
169     struct kvm_device_attr attr = {
170         .group = KVM_S390_VM_MEM_CTRL,
171         .attr = KVM_S390_VM_MEM_CLR_CMMA,
172     };
173
174     rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
175     trace_kvm_clear_cmma(rc);
176 }
177
178 static void kvm_s390_enable_cmma(KVMState *s)
179 {
180     int rc;
181     struct kvm_device_attr attr = {
182         .group = KVM_S390_VM_MEM_CTRL,
183         .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
184     };
185
186     if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_ENABLE_CMMA) ||
187         !kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_CLR_CMMA)) {
188         return;
189     }
190
191     rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
192     if (!rc) {
193         qemu_register_reset(kvm_s390_clear_cmma_callback, s);
194     }
195     trace_kvm_enable_cmma(rc);
196 }
197
198 static void kvm_s390_set_attr(uint64_t attr)
199 {
200     struct kvm_device_attr attribute = {
201         .group = KVM_S390_VM_CRYPTO,
202         .attr  = attr,
203     };
204
205     int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
206
207     if (ret) {
208         error_report("Failed to set crypto device attribute %lu: %s",
209                      attr, strerror(-ret));
210     }
211 }
212
213 static void kvm_s390_init_aes_kw(void)
214 {
215     uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
216
217     if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
218                                  NULL)) {
219             attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
220     }
221
222     if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
223             kvm_s390_set_attr(attr);
224     }
225 }
226
227 static void kvm_s390_init_dea_kw(void)
228 {
229     uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
230
231     if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
232                                  NULL)) {
233             attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
234     }
235
236     if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
237             kvm_s390_set_attr(attr);
238     }
239 }
240
241 static void kvm_s390_init_crypto(void)
242 {
243     kvm_s390_init_aes_kw();
244     kvm_s390_init_dea_kw();
245 }
246
247 int kvm_arch_init(MachineState *ms, KVMState *s)
248 {
249     cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
250     cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
251     cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
252
253     kvm_s390_enable_cmma(s);
254
255     if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
256         || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
257         phys_mem_set_alloc(legacy_s390_alloc);
258     }
259
260     kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
261     kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
262
263     return 0;
264 }
265
266 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
267 {
268     return cpu->cpu_index;
269 }
270
271 int kvm_arch_init_vcpu(CPUState *cs)
272 {
273     S390CPU *cpu = S390_CPU(cs);
274     kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
275     return 0;
276 }
277
278 void kvm_s390_reset_vcpu(S390CPU *cpu)
279 {
280     CPUState *cs = CPU(cpu);
281
282     /* The initial reset call is needed here to reset in-kernel
283      * vcpu data that we can't access directly from QEMU
284      * (i.e. with older kernels which don't support sync_regs/ONE_REG).
285      * Before this ioctl cpu_synchronize_state() is called in common kvm
286      * code (kvm-all) */
287     if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
288         error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
289     }
290
291     kvm_s390_init_crypto();
292 }
293
294 static int can_sync_regs(CPUState *cs, int regs)
295 {
296     return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
297 }
298
299 int kvm_arch_put_registers(CPUState *cs, int level)
300 {
301     S390CPU *cpu = S390_CPU(cs);
302     CPUS390XState *env = &cpu->env;
303     struct kvm_sregs sregs;
304     struct kvm_regs regs;
305     struct kvm_fpu fpu = {};
306     int r;
307     int i;
308
309     /* always save the PSW  and the GPRS*/
310     cs->kvm_run->psw_addr = env->psw.addr;
311     cs->kvm_run->psw_mask = env->psw.mask;
312
313     if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
314         for (i = 0; i < 16; i++) {
315             cs->kvm_run->s.regs.gprs[i] = env->regs[i];
316             cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
317         }
318     } else {
319         for (i = 0; i < 16; i++) {
320             regs.gprs[i] = env->regs[i];
321         }
322         r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
323         if (r < 0) {
324             return r;
325         }
326     }
327
328     /* Floating point */
329     for (i = 0; i < 16; i++) {
330         fpu.fprs[i] = env->fregs[i].ll;
331     }
332     fpu.fpc = env->fpc;
333
334     r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
335     if (r < 0) {
336         return r;
337     }
338
339     /* Do we need to save more than that? */
340     if (level == KVM_PUT_RUNTIME_STATE) {
341         return 0;
342     }
343
344     if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
345         cs->kvm_run->s.regs.cputm = env->cputm;
346         cs->kvm_run->s.regs.ckc = env->ckc;
347         cs->kvm_run->s.regs.todpr = env->todpr;
348         cs->kvm_run->s.regs.gbea = env->gbea;
349         cs->kvm_run->s.regs.pp = env->pp;
350         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
351     } else {
352         /*
353          * These ONE_REGS are not protected by a capability. As they are only
354          * necessary for migration we just trace a possible error, but don't
355          * return with an error return code.
356          */
357         kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
358         kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
359         kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
360         kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
361         kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
362     }
363
364     /* pfault parameters */
365     if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
366         cs->kvm_run->s.regs.pft = env->pfault_token;
367         cs->kvm_run->s.regs.pfs = env->pfault_select;
368         cs->kvm_run->s.regs.pfc = env->pfault_compare;
369         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
370     } else if (cap_async_pf) {
371         r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
372         if (r < 0) {
373             return r;
374         }
375         r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
376         if (r < 0) {
377             return r;
378         }
379         r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
380         if (r < 0) {
381             return r;
382         }
383     }
384
385     /* access registers and control registers*/
386     if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
387         for (i = 0; i < 16; i++) {
388             cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
389             cs->kvm_run->s.regs.crs[i] = env->cregs[i];
390         }
391         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
392         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
393     } else {
394         for (i = 0; i < 16; i++) {
395             sregs.acrs[i] = env->aregs[i];
396             sregs.crs[i] = env->cregs[i];
397         }
398         r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
399         if (r < 0) {
400             return r;
401         }
402     }
403
404     /* Finally the prefix */
405     if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
406         cs->kvm_run->s.regs.prefix = env->psa;
407         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
408     } else {
409         /* prefix is only supported via sync regs */
410     }
411     return 0;
412 }
413
414 int kvm_arch_get_registers(CPUState *cs)
415 {
416     S390CPU *cpu = S390_CPU(cs);
417     CPUS390XState *env = &cpu->env;
418     struct kvm_sregs sregs;
419     struct kvm_regs regs;
420     struct kvm_fpu fpu;
421     int i, r;
422
423     /* get the PSW */
424     env->psw.addr = cs->kvm_run->psw_addr;
425     env->psw.mask = cs->kvm_run->psw_mask;
426
427     /* the GPRS */
428     if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
429         for (i = 0; i < 16; i++) {
430             env->regs[i] = cs->kvm_run->s.regs.gprs[i];
431         }
432     } else {
433         r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
434         if (r < 0) {
435             return r;
436         }
437          for (i = 0; i < 16; i++) {
438             env->regs[i] = regs.gprs[i];
439         }
440     }
441
442     /* The ACRS and CRS */
443     if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
444         for (i = 0; i < 16; i++) {
445             env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
446             env->cregs[i] = cs->kvm_run->s.regs.crs[i];
447         }
448     } else {
449         r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
450         if (r < 0) {
451             return r;
452         }
453          for (i = 0; i < 16; i++) {
454             env->aregs[i] = sregs.acrs[i];
455             env->cregs[i] = sregs.crs[i];
456         }
457     }
458
459     /* Floating point */
460     r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
461     if (r < 0) {
462         return r;
463     }
464     for (i = 0; i < 16; i++) {
465         env->fregs[i].ll = fpu.fprs[i];
466     }
467     env->fpc = fpu.fpc;
468
469     /* The prefix */
470     if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
471         env->psa = cs->kvm_run->s.regs.prefix;
472     }
473
474     if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
475         env->cputm = cs->kvm_run->s.regs.cputm;
476         env->ckc = cs->kvm_run->s.regs.ckc;
477         env->todpr = cs->kvm_run->s.regs.todpr;
478         env->gbea = cs->kvm_run->s.regs.gbea;
479         env->pp = cs->kvm_run->s.regs.pp;
480     } else {
481         /*
482          * These ONE_REGS are not protected by a capability. As they are only
483          * necessary for migration we just trace a possible error, but don't
484          * return with an error return code.
485          */
486         kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
487         kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
488         kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
489         kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
490         kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
491     }
492
493     /* pfault parameters */
494     if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
495         env->pfault_token = cs->kvm_run->s.regs.pft;
496         env->pfault_select = cs->kvm_run->s.regs.pfs;
497         env->pfault_compare = cs->kvm_run->s.regs.pfc;
498     } else if (cap_async_pf) {
499         r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
500         if (r < 0) {
501             return r;
502         }
503         r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
504         if (r < 0) {
505             return r;
506         }
507         r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
508         if (r < 0) {
509             return r;
510         }
511     }
512
513     return 0;
514 }
515
516 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
517 {
518     int r;
519     struct kvm_device_attr attr = {
520         .group = KVM_S390_VM_TOD,
521         .attr = KVM_S390_VM_TOD_LOW,
522         .addr = (uint64_t)tod_low,
523     };
524
525     r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
526     if (r) {
527         return r;
528     }
529
530     attr.attr = KVM_S390_VM_TOD_HIGH;
531     attr.addr = (uint64_t)tod_high;
532     return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
533 }
534
535 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
536 {
537     int r;
538
539     struct kvm_device_attr attr = {
540         .group = KVM_S390_VM_TOD,
541         .attr = KVM_S390_VM_TOD_LOW,
542         .addr = (uint64_t)tod_low,
543     };
544
545     r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
546     if (r) {
547         return r;
548     }
549
550     attr.attr = KVM_S390_VM_TOD_HIGH;
551     attr.addr = (uint64_t)tod_high;
552     return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
553 }
554
555 /**
556  * kvm_s390_mem_op:
557  * @addr:      the logical start address in guest memory
558  * @ar:        the access register number
559  * @hostbuf:   buffer in host memory. NULL = do only checks w/o copying
560  * @len:       length that should be transfered
561  * @is_write:  true = write, false = read
562  * Returns:    0 on success, non-zero if an exception or error occured
563  *
564  * Use KVM ioctl to read/write from/to guest memory. An access exception
565  * is injected into the vCPU in case of translation errors.
566  */
567 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
568                     int len, bool is_write)
569 {
570     struct kvm_s390_mem_op mem_op = {
571         .gaddr = addr,
572         .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
573         .size = len,
574         .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
575                        : KVM_S390_MEMOP_LOGICAL_READ,
576         .buf = (uint64_t)hostbuf,
577         .ar = ar,
578     };
579     int ret;
580
581     if (!cap_mem_op) {
582         return -ENOSYS;
583     }
584     if (!hostbuf) {
585         mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
586     }
587
588     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
589     if (ret < 0) {
590         error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
591     }
592     return ret;
593 }
594
595 /*
596  * Legacy layout for s390:
597  * Older S390 KVM requires the topmost vma of the RAM to be
598  * smaller than an system defined value, which is at least 256GB.
599  * Larger systems have larger values. We put the guest between
600  * the end of data segment (system break) and this value. We
601  * use 32GB as a base to have enough room for the system break
602  * to grow. We also have to use MAP parameters that avoid
603  * read-only mapping of guest pages.
604  */
605 static void *legacy_s390_alloc(size_t size, uint64_t *align)
606 {
607     void *mem;
608
609     mem = mmap((void *) 0x800000000ULL, size,
610                PROT_EXEC|PROT_READ|PROT_WRITE,
611                MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
612     return mem == MAP_FAILED ? NULL : mem;
613 }
614
615 /* DIAG 501 is used for sw breakpoints */
616 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
617
618 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
619 {
620
621     if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
622                             sizeof(diag_501), 0) ||
623         cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
624                             sizeof(diag_501), 1)) {
625         return -EINVAL;
626     }
627     return 0;
628 }
629
630 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
631 {
632     uint8_t t[sizeof(diag_501)];
633
634     if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
635         return -EINVAL;
636     } else if (memcmp(t, diag_501, sizeof(diag_501))) {
637         return -EINVAL;
638     } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
639                                    sizeof(diag_501), 1)) {
640         return -EINVAL;
641     }
642
643     return 0;
644 }
645
646 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
647                                                     int len, int type)
648 {
649     int n;
650
651     for (n = 0; n < nb_hw_breakpoints; n++) {
652         if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
653             (hw_breakpoints[n].len == len || len == -1)) {
654             return &hw_breakpoints[n];
655         }
656     }
657
658     return NULL;
659 }
660
661 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
662 {
663     int size;
664
665     if (find_hw_breakpoint(addr, len, type)) {
666         return -EEXIST;
667     }
668
669     size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
670
671     if (!hw_breakpoints) {
672         nb_hw_breakpoints = 0;
673         hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
674     } else {
675         hw_breakpoints =
676             (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
677     }
678
679     if (!hw_breakpoints) {
680         nb_hw_breakpoints = 0;
681         return -ENOMEM;
682     }
683
684     hw_breakpoints[nb_hw_breakpoints].addr = addr;
685     hw_breakpoints[nb_hw_breakpoints].len = len;
686     hw_breakpoints[nb_hw_breakpoints].type = type;
687
688     nb_hw_breakpoints++;
689
690     return 0;
691 }
692
693 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
694                                   target_ulong len, int type)
695 {
696     switch (type) {
697     case GDB_BREAKPOINT_HW:
698         type = KVM_HW_BP;
699         break;
700     case GDB_WATCHPOINT_WRITE:
701         if (len < 1) {
702             return -EINVAL;
703         }
704         type = KVM_HW_WP_WRITE;
705         break;
706     default:
707         return -ENOSYS;
708     }
709     return insert_hw_breakpoint(addr, len, type);
710 }
711
712 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
713                                   target_ulong len, int type)
714 {
715     int size;
716     struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
717
718     if (bp == NULL) {
719         return -ENOENT;
720     }
721
722     nb_hw_breakpoints--;
723     if (nb_hw_breakpoints > 0) {
724         /*
725          * In order to trim the array, move the last element to the position to
726          * be removed - if necessary.
727          */
728         if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
729             *bp = hw_breakpoints[nb_hw_breakpoints];
730         }
731         size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
732         hw_breakpoints =
733              (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
734     } else {
735         g_free(hw_breakpoints);
736         hw_breakpoints = NULL;
737     }
738
739     return 0;
740 }
741
742 void kvm_arch_remove_all_hw_breakpoints(void)
743 {
744     nb_hw_breakpoints = 0;
745     g_free(hw_breakpoints);
746     hw_breakpoints = NULL;
747 }
748
749 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
750 {
751     int i;
752
753     if (nb_hw_breakpoints > 0) {
754         dbg->arch.nr_hw_bp = nb_hw_breakpoints;
755         dbg->arch.hw_bp = hw_breakpoints;
756
757         for (i = 0; i < nb_hw_breakpoints; ++i) {
758             hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
759                                                        hw_breakpoints[i].addr);
760         }
761         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
762     } else {
763         dbg->arch.nr_hw_bp = 0;
764         dbg->arch.hw_bp = NULL;
765     }
766 }
767
768 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
769 {
770 }
771
772 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
773 {
774 }
775
776 int kvm_arch_process_async_events(CPUState *cs)
777 {
778     return cs->halted;
779 }
780
781 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
782                                      struct kvm_s390_interrupt *interrupt)
783 {
784     int r = 0;
785
786     interrupt->type = irq->type;
787     switch (irq->type) {
788     case KVM_S390_INT_VIRTIO:
789         interrupt->parm = irq->u.ext.ext_params;
790         /* fall through */
791     case KVM_S390_INT_PFAULT_INIT:
792     case KVM_S390_INT_PFAULT_DONE:
793         interrupt->parm64 = irq->u.ext.ext_params2;
794         break;
795     case KVM_S390_PROGRAM_INT:
796         interrupt->parm = irq->u.pgm.code;
797         break;
798     case KVM_S390_SIGP_SET_PREFIX:
799         interrupt->parm = irq->u.prefix.address;
800         break;
801     case KVM_S390_INT_SERVICE:
802         interrupt->parm = irq->u.ext.ext_params;
803         break;
804     case KVM_S390_MCHK:
805         interrupt->parm = irq->u.mchk.cr14;
806         interrupt->parm64 = irq->u.mchk.mcic;
807         break;
808     case KVM_S390_INT_EXTERNAL_CALL:
809         interrupt->parm = irq->u.extcall.code;
810         break;
811     case KVM_S390_INT_EMERGENCY:
812         interrupt->parm = irq->u.emerg.code;
813         break;
814     case KVM_S390_SIGP_STOP:
815     case KVM_S390_RESTART:
816         break; /* These types have no parameters */
817     case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
818         interrupt->parm = irq->u.io.subchannel_id << 16;
819         interrupt->parm |= irq->u.io.subchannel_nr;
820         interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
821         interrupt->parm64 |= irq->u.io.io_int_word;
822         break;
823     default:
824         r = -EINVAL;
825         break;
826     }
827     return r;
828 }
829
830 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
831 {
832     struct kvm_s390_interrupt kvmint = {};
833     CPUState *cs = CPU(cpu);
834     int r;
835
836     r = s390_kvm_irq_to_interrupt(irq, &kvmint);
837     if (r < 0) {
838         fprintf(stderr, "%s called with bogus interrupt\n", __func__);
839         exit(1);
840     }
841
842     r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
843     if (r < 0) {
844         fprintf(stderr, "KVM failed to inject interrupt\n");
845         exit(1);
846     }
847 }
848
849 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
850 {
851     struct kvm_s390_interrupt kvmint = {};
852     int r;
853
854     r = s390_kvm_irq_to_interrupt(irq, &kvmint);
855     if (r < 0) {
856         fprintf(stderr, "%s called with bogus interrupt\n", __func__);
857         exit(1);
858     }
859
860     r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
861     if (r < 0) {
862         fprintf(stderr, "KVM failed to inject interrupt\n");
863         exit(1);
864     }
865 }
866
867 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
868 {
869     static bool use_flic = true;
870     int r;
871
872     if (use_flic) {
873         r = kvm_s390_inject_flic(irq);
874         if (r == -ENOSYS) {
875             use_flic = false;
876         }
877         if (!r) {
878             return;
879         }
880     }
881     __kvm_s390_floating_interrupt(irq);
882 }
883
884 void kvm_s390_virtio_irq(int config_change, uint64_t token)
885 {
886     struct kvm_s390_irq irq = {
887         .type = KVM_S390_INT_VIRTIO,
888         .u.ext.ext_params = config_change,
889         .u.ext.ext_params2 = token,
890     };
891
892     kvm_s390_floating_interrupt(&irq);
893 }
894
895 void kvm_s390_service_interrupt(uint32_t parm)
896 {
897     struct kvm_s390_irq irq = {
898         .type = KVM_S390_INT_SERVICE,
899         .u.ext.ext_params = parm,
900     };
901
902     kvm_s390_floating_interrupt(&irq);
903 }
904
905 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
906 {
907     struct kvm_s390_irq irq = {
908         .type = KVM_S390_PROGRAM_INT,
909         .u.pgm.code = code,
910     };
911
912     kvm_s390_vcpu_interrupt(cpu, &irq);
913 }
914
915 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
916 {
917     struct kvm_s390_irq irq = {
918         .type = KVM_S390_PROGRAM_INT,
919         .u.pgm.code = code,
920         .u.pgm.trans_exc_code = te_code,
921         .u.pgm.exc_access_id = te_code & 3,
922     };
923
924     kvm_s390_vcpu_interrupt(cpu, &irq);
925 }
926
927 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
928                                  uint16_t ipbh0)
929 {
930     CPUS390XState *env = &cpu->env;
931     uint64_t sccb;
932     uint32_t code;
933     int r = 0;
934
935     cpu_synchronize_state(CPU(cpu));
936     sccb = env->regs[ipbh0 & 0xf];
937     code = env->regs[(ipbh0 & 0xf0) >> 4];
938
939     r = sclp_service_call(env, sccb, code);
940     if (r < 0) {
941         enter_pgmcheck(cpu, -r);
942     } else {
943         setcc(cpu, r);
944     }
945
946     return 0;
947 }
948
949 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
950 {
951     CPUS390XState *env = &cpu->env;
952     int rc = 0;
953     uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
954
955     cpu_synchronize_state(CPU(cpu));
956
957     switch (ipa1) {
958     case PRIV_B2_XSCH:
959         ioinst_handle_xsch(cpu, env->regs[1]);
960         break;
961     case PRIV_B2_CSCH:
962         ioinst_handle_csch(cpu, env->regs[1]);
963         break;
964     case PRIV_B2_HSCH:
965         ioinst_handle_hsch(cpu, env->regs[1]);
966         break;
967     case PRIV_B2_MSCH:
968         ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
969         break;
970     case PRIV_B2_SSCH:
971         ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
972         break;
973     case PRIV_B2_STCRW:
974         ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
975         break;
976     case PRIV_B2_STSCH:
977         ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
978         break;
979     case PRIV_B2_TSCH:
980         /* We should only get tsch via KVM_EXIT_S390_TSCH. */
981         fprintf(stderr, "Spurious tsch intercept\n");
982         break;
983     case PRIV_B2_CHSC:
984         ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
985         break;
986     case PRIV_B2_TPI:
987         /* This should have been handled by kvm already. */
988         fprintf(stderr, "Spurious tpi intercept\n");
989         break;
990     case PRIV_B2_SCHM:
991         ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
992                            run->s390_sieic.ipb);
993         break;
994     case PRIV_B2_RSCH:
995         ioinst_handle_rsch(cpu, env->regs[1]);
996         break;
997     case PRIV_B2_RCHP:
998         ioinst_handle_rchp(cpu, env->regs[1]);
999         break;
1000     case PRIV_B2_STCPS:
1001         /* We do not provide this instruction, it is suppressed. */
1002         break;
1003     case PRIV_B2_SAL:
1004         ioinst_handle_sal(cpu, env->regs[1]);
1005         break;
1006     case PRIV_B2_SIGA:
1007         /* Not provided, set CC = 3 for subchannel not operational */
1008         setcc(cpu, 3);
1009         break;
1010     case PRIV_B2_SCLP_CALL:
1011         rc = kvm_sclp_service_call(cpu, run, ipbh0);
1012         break;
1013     default:
1014         rc = -1;
1015         DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1016         break;
1017     }
1018
1019     return rc;
1020 }
1021
1022 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1023                                   uint8_t *ar)
1024 {
1025     CPUS390XState *env = &cpu->env;
1026     uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1027     uint32_t base2 = run->s390_sieic.ipb >> 28;
1028     uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1029                      ((run->s390_sieic.ipb & 0xff00) << 4);
1030
1031     if (disp2 & 0x80000) {
1032         disp2 += 0xfff00000;
1033     }
1034     if (ar) {
1035         *ar = base2;
1036     }
1037
1038     return (base2 ? env->regs[base2] : 0) +
1039            (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1040 }
1041
1042 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1043                                   uint8_t *ar)
1044 {
1045     CPUS390XState *env = &cpu->env;
1046     uint32_t base2 = run->s390_sieic.ipb >> 28;
1047     uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1048                      ((run->s390_sieic.ipb & 0xff00) << 4);
1049
1050     if (disp2 & 0x80000) {
1051         disp2 += 0xfff00000;
1052     }
1053     if (ar) {
1054         *ar = base2;
1055     }
1056
1057     return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1058 }
1059
1060 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1061 {
1062     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1063
1064     return clp_service_call(cpu, r2);
1065 }
1066
1067 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1068 {
1069     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1070     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1071
1072     return pcilg_service_call(cpu, r1, r2);
1073 }
1074
1075 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1076 {
1077     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1078     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1079
1080     return pcistg_service_call(cpu, r1, r2);
1081 }
1082
1083 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1084 {
1085     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1086     uint64_t fiba;
1087     uint8_t ar;
1088
1089     cpu_synchronize_state(CPU(cpu));
1090     fiba = get_base_disp_rxy(cpu, run, &ar);
1091
1092     return stpcifc_service_call(cpu, r1, fiba, ar);
1093 }
1094
1095 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1096 {
1097     /* NOOP */
1098     return 0;
1099 }
1100
1101 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1102 {
1103     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1104     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1105
1106     return rpcit_service_call(cpu, r1, r2);
1107 }
1108
1109 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1110 {
1111     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1112     uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1113     uint64_t gaddr;
1114     uint8_t ar;
1115
1116     cpu_synchronize_state(CPU(cpu));
1117     gaddr = get_base_disp_rsy(cpu, run, &ar);
1118
1119     return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1120 }
1121
1122 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1123 {
1124     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1125     uint64_t fiba;
1126     uint8_t ar;
1127
1128     cpu_synchronize_state(CPU(cpu));
1129     fiba = get_base_disp_rxy(cpu, run, &ar);
1130
1131     return mpcifc_service_call(cpu, r1, fiba, ar);
1132 }
1133
1134 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1135 {
1136     int r = 0;
1137
1138     switch (ipa1) {
1139     case PRIV_B9_CLP:
1140         r = kvm_clp_service_call(cpu, run);
1141         break;
1142     case PRIV_B9_PCISTG:
1143         r = kvm_pcistg_service_call(cpu, run);
1144         break;
1145     case PRIV_B9_PCILG:
1146         r = kvm_pcilg_service_call(cpu, run);
1147         break;
1148     case PRIV_B9_RPCIT:
1149         r = kvm_rpcit_service_call(cpu, run);
1150         break;
1151     case PRIV_B9_EQBS:
1152         /* just inject exception */
1153         r = -1;
1154         break;
1155     default:
1156         r = -1;
1157         DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1158         break;
1159     }
1160
1161     return r;
1162 }
1163
1164 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1165 {
1166     int r = 0;
1167
1168     switch (ipbl) {
1169     case PRIV_EB_PCISTB:
1170         r = kvm_pcistb_service_call(cpu, run);
1171         break;
1172     case PRIV_EB_SIC:
1173         r = kvm_sic_service_call(cpu, run);
1174         break;
1175     case PRIV_EB_SQBS:
1176         /* just inject exception */
1177         r = -1;
1178         break;
1179     default:
1180         r = -1;
1181         DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1182         break;
1183     }
1184
1185     return r;
1186 }
1187
1188 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1189 {
1190     int r = 0;
1191
1192     switch (ipbl) {
1193     case PRIV_E3_MPCIFC:
1194         r = kvm_mpcifc_service_call(cpu, run);
1195         break;
1196     case PRIV_E3_STPCIFC:
1197         r = kvm_stpcifc_service_call(cpu, run);
1198         break;
1199     default:
1200         r = -1;
1201         DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1202         break;
1203     }
1204
1205     return r;
1206 }
1207
1208 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1209 {
1210     CPUS390XState *env = &cpu->env;
1211     int ret;
1212
1213     cpu_synchronize_state(CPU(cpu));
1214     ret = s390_virtio_hypercall(env);
1215     if (ret == -EINVAL) {
1216         enter_pgmcheck(cpu, PGM_SPECIFICATION);
1217         return 0;
1218     }
1219
1220     return ret;
1221 }
1222
1223 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1224 {
1225     uint64_t r1, r3;
1226
1227     cpu_synchronize_state(CPU(cpu));
1228     r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1229     r3 = run->s390_sieic.ipa & 0x000f;
1230     handle_diag_308(&cpu->env, r1, r3);
1231 }
1232
1233 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1234 {
1235     CPUS390XState *env = &cpu->env;
1236     unsigned long pc;
1237
1238     cpu_synchronize_state(CPU(cpu));
1239
1240     pc = env->psw.addr - 4;
1241     if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1242         env->psw.addr = pc;
1243         return EXCP_DEBUG;
1244     }
1245
1246     return -ENOENT;
1247 }
1248
1249 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1250
1251 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1252 {
1253     int r = 0;
1254     uint16_t func_code;
1255
1256     /*
1257      * For any diagnose call we support, bits 48-63 of the resulting
1258      * address specify the function code; the remainder is ignored.
1259      */
1260     func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1261     switch (func_code) {
1262     case DIAG_IPL:
1263         kvm_handle_diag_308(cpu, run);
1264         break;
1265     case DIAG_KVM_HYPERCALL:
1266         r = handle_hypercall(cpu, run);
1267         break;
1268     case DIAG_KVM_BREAKPOINT:
1269         r = handle_sw_breakpoint(cpu, run);
1270         break;
1271     default:
1272         DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1273         enter_pgmcheck(cpu, PGM_SPECIFICATION);
1274         break;
1275     }
1276
1277     return r;
1278 }
1279
1280 typedef struct SigpInfo {
1281     S390CPU *cpu;
1282     uint64_t param;
1283     int cc;
1284     uint64_t *status_reg;
1285 } SigpInfo;
1286
1287 static void set_sigp_status(SigpInfo *si, uint64_t status)
1288 {
1289     *si->status_reg &= 0xffffffff00000000ULL;
1290     *si->status_reg |= status;
1291     si->cc = SIGP_CC_STATUS_STORED;
1292 }
1293
1294 static void sigp_start(void *arg)
1295 {
1296     SigpInfo *si = arg;
1297
1298     if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1299         si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1300         return;
1301     }
1302
1303     s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1304     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1305 }
1306
1307 static void sigp_stop(void *arg)
1308 {
1309     SigpInfo *si = arg;
1310     struct kvm_s390_irq irq = {
1311         .type = KVM_S390_SIGP_STOP,
1312     };
1313
1314     if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1315         si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1316         return;
1317     }
1318
1319     /* disabled wait - sleeping in user space */
1320     if (CPU(si->cpu)->halted) {
1321         s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1322     } else {
1323         /* execute the stop function */
1324         si->cpu->env.sigp_order = SIGP_STOP;
1325         kvm_s390_vcpu_interrupt(si->cpu, &irq);
1326     }
1327     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1328 }
1329
1330 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1331 #define SAVE_AREA_SIZE 512
1332 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1333 {
1334     static const uint8_t ar_id = 1;
1335     uint64_t ckc = cpu->env.ckc >> 8;
1336     void *mem;
1337     hwaddr len = SAVE_AREA_SIZE;
1338
1339     mem = cpu_physical_memory_map(addr, &len, 1);
1340     if (!mem) {
1341         return -EFAULT;
1342     }
1343     if (len != SAVE_AREA_SIZE) {
1344         cpu_physical_memory_unmap(mem, len, 1, 0);
1345         return -EFAULT;
1346     }
1347
1348     if (store_arch) {
1349         cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1350     }
1351     memcpy(mem, &cpu->env.fregs, 128);
1352     memcpy(mem + 128, &cpu->env.regs, 128);
1353     memcpy(mem + 256, &cpu->env.psw, 16);
1354     memcpy(mem + 280, &cpu->env.psa, 4);
1355     memcpy(mem + 284, &cpu->env.fpc, 4);
1356     memcpy(mem + 292, &cpu->env.todpr, 4);
1357     memcpy(mem + 296, &cpu->env.cputm, 8);
1358     memcpy(mem + 304, &ckc, 8);
1359     memcpy(mem + 320, &cpu->env.aregs, 64);
1360     memcpy(mem + 384, &cpu->env.cregs, 128);
1361
1362     cpu_physical_memory_unmap(mem, len, 1, len);
1363
1364     return 0;
1365 }
1366
1367 static void sigp_stop_and_store_status(void *arg)
1368 {
1369     SigpInfo *si = arg;
1370     struct kvm_s390_irq irq = {
1371         .type = KVM_S390_SIGP_STOP,
1372     };
1373
1374     /* disabled wait - sleeping in user space */
1375     if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1376         CPU(si->cpu)->halted) {
1377         s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1378     }
1379
1380     switch (s390_cpu_get_state(si->cpu)) {
1381     case CPU_STATE_OPERATING:
1382         si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1383         kvm_s390_vcpu_interrupt(si->cpu, &irq);
1384         /* store will be performed when handling the stop intercept */
1385         break;
1386     case CPU_STATE_STOPPED:
1387         /* already stopped, just store the status */
1388         cpu_synchronize_state(CPU(si->cpu));
1389         kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1390         break;
1391     }
1392     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1393 }
1394
1395 static void sigp_store_status_at_address(void *arg)
1396 {
1397     SigpInfo *si = arg;
1398     uint32_t address = si->param & 0x7ffffe00u;
1399
1400     /* cpu has to be stopped */
1401     if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1402         set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1403         return;
1404     }
1405
1406     cpu_synchronize_state(CPU(si->cpu));
1407
1408     if (kvm_s390_store_status(si->cpu, address, false)) {
1409         set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1410         return;
1411     }
1412     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1413 }
1414
1415 static void sigp_restart(void *arg)
1416 {
1417     SigpInfo *si = arg;
1418     struct kvm_s390_irq irq = {
1419         .type = KVM_S390_RESTART,
1420     };
1421
1422     switch (s390_cpu_get_state(si->cpu)) {
1423     case CPU_STATE_STOPPED:
1424         /* the restart irq has to be delivered prior to any other pending irq */
1425         cpu_synchronize_state(CPU(si->cpu));
1426         do_restart_interrupt(&si->cpu->env);
1427         s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1428         break;
1429     case CPU_STATE_OPERATING:
1430         kvm_s390_vcpu_interrupt(si->cpu, &irq);
1431         break;
1432     }
1433     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1434 }
1435
1436 int kvm_s390_cpu_restart(S390CPU *cpu)
1437 {
1438     SigpInfo si = {
1439         .cpu = cpu,
1440     };
1441
1442     run_on_cpu(CPU(cpu), sigp_restart, &si);
1443     DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1444     return 0;
1445 }
1446
1447 static void sigp_initial_cpu_reset(void *arg)
1448 {
1449     SigpInfo *si = arg;
1450     CPUState *cs = CPU(si->cpu);
1451     S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1452
1453     cpu_synchronize_state(cs);
1454     scc->initial_cpu_reset(cs);
1455     cpu_synchronize_post_reset(cs);
1456     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1457 }
1458
1459 static void sigp_cpu_reset(void *arg)
1460 {
1461     SigpInfo *si = arg;
1462     CPUState *cs = CPU(si->cpu);
1463     S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1464
1465     cpu_synchronize_state(cs);
1466     scc->cpu_reset(cs);
1467     cpu_synchronize_post_reset(cs);
1468     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1469 }
1470
1471 static void sigp_set_prefix(void *arg)
1472 {
1473     SigpInfo *si = arg;
1474     uint32_t addr = si->param & 0x7fffe000u;
1475
1476     cpu_synchronize_state(CPU(si->cpu));
1477
1478     if (!address_space_access_valid(&address_space_memory, addr,
1479                                     sizeof(struct LowCore), false)) {
1480         set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1481         return;
1482     }
1483
1484     /* cpu has to be stopped */
1485     if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1486         set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1487         return;
1488     }
1489
1490     si->cpu->env.psa = addr;
1491     cpu_synchronize_post_init(CPU(si->cpu));
1492     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1493 }
1494
1495 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1496                                   uint64_t param, uint64_t *status_reg)
1497 {
1498     SigpInfo si = {
1499         .cpu = dst_cpu,
1500         .param = param,
1501         .status_reg = status_reg,
1502     };
1503
1504     /* cpu available? */
1505     if (dst_cpu == NULL) {
1506         return SIGP_CC_NOT_OPERATIONAL;
1507     }
1508
1509     /* only resets can break pending orders */
1510     if (dst_cpu->env.sigp_order != 0 &&
1511         order != SIGP_CPU_RESET &&
1512         order != SIGP_INITIAL_CPU_RESET) {
1513         return SIGP_CC_BUSY;
1514     }
1515
1516     switch (order) {
1517     case SIGP_START:
1518         run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1519         break;
1520     case SIGP_STOP:
1521         run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1522         break;
1523     case SIGP_RESTART:
1524         run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1525         break;
1526     case SIGP_STOP_STORE_STATUS:
1527         run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1528         break;
1529     case SIGP_STORE_STATUS_ADDR:
1530         run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1531         break;
1532     case SIGP_SET_PREFIX:
1533         run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1534         break;
1535     case SIGP_INITIAL_CPU_RESET:
1536         run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1537         break;
1538     case SIGP_CPU_RESET:
1539         run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1540         break;
1541     default:
1542         DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1543         set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1544     }
1545
1546     return si.cc;
1547 }
1548
1549 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1550                                  uint64_t *status_reg)
1551 {
1552     CPUState *cur_cs;
1553     S390CPU *cur_cpu;
1554
1555     /* due to the BQL, we are the only active cpu */
1556     CPU_FOREACH(cur_cs) {
1557         cur_cpu = S390_CPU(cur_cs);
1558         if (cur_cpu->env.sigp_order != 0) {
1559             return SIGP_CC_BUSY;
1560         }
1561         cpu_synchronize_state(cur_cs);
1562         /* all but the current one have to be stopped */
1563         if (cur_cpu != cpu &&
1564             s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1565             *status_reg &= 0xffffffff00000000ULL;
1566             *status_reg |= SIGP_STAT_INCORRECT_STATE;
1567             return SIGP_CC_STATUS_STORED;
1568         }
1569     }
1570
1571     switch (param & 0xff) {
1572     case SIGP_MODE_ESA_S390:
1573         /* not supported */
1574         return SIGP_CC_NOT_OPERATIONAL;
1575     case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1576     case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1577         CPU_FOREACH(cur_cs) {
1578             cur_cpu = S390_CPU(cur_cs);
1579             cur_cpu->env.pfault_token = -1UL;
1580         }
1581         break;
1582     default:
1583         *status_reg &= 0xffffffff00000000ULL;
1584         *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1585         return SIGP_CC_STATUS_STORED;
1586     }
1587
1588     return SIGP_CC_ORDER_CODE_ACCEPTED;
1589 }
1590
1591 #define SIGP_ORDER_MASK 0x000000ff
1592
1593 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1594 {
1595     CPUS390XState *env = &cpu->env;
1596     const uint8_t r1 = ipa1 >> 4;
1597     const uint8_t r3 = ipa1 & 0x0f;
1598     int ret;
1599     uint8_t order;
1600     uint64_t *status_reg;
1601     uint64_t param;
1602     S390CPU *dst_cpu = NULL;
1603
1604     cpu_synchronize_state(CPU(cpu));
1605
1606     /* get order code */
1607     order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1608         & SIGP_ORDER_MASK;
1609     status_reg = &env->regs[r1];
1610     param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1611
1612     switch (order) {
1613     case SIGP_SET_ARCH:
1614         ret = sigp_set_architecture(cpu, param, status_reg);
1615         break;
1616     default:
1617         /* all other sigp orders target a single vcpu */
1618         dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1619         ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1620     }
1621
1622     trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1623                             dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1624
1625     if (ret >= 0) {
1626         setcc(cpu, ret);
1627         return 0;
1628     }
1629
1630     return ret;
1631 }
1632
1633 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1634 {
1635     unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1636     uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1637     int r = -1;
1638
1639     DPRINTF("handle_instruction 0x%x 0x%x\n",
1640             run->s390_sieic.ipa, run->s390_sieic.ipb);
1641     switch (ipa0) {
1642     case IPA0_B2:
1643         r = handle_b2(cpu, run, ipa1);
1644         break;
1645     case IPA0_B9:
1646         r = handle_b9(cpu, run, ipa1);
1647         break;
1648     case IPA0_EB:
1649         r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1650         break;
1651     case IPA0_E3:
1652         r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1653         break;
1654     case IPA0_DIAG:
1655         r = handle_diag(cpu, run, run->s390_sieic.ipb);
1656         break;
1657     case IPA0_SIGP:
1658         r = handle_sigp(cpu, run, ipa1);
1659         break;
1660     }
1661
1662     if (r < 0) {
1663         r = 0;
1664         enter_pgmcheck(cpu, 0x0001);
1665     }
1666
1667     return r;
1668 }
1669
1670 static bool is_special_wait_psw(CPUState *cs)
1671 {
1672     /* signal quiesce */
1673     return cs->kvm_run->psw_addr == 0xfffUL;
1674 }
1675
1676 static void guest_panicked(void)
1677 {
1678     qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1679                                    &error_abort);
1680     vm_stop(RUN_STATE_GUEST_PANICKED);
1681 }
1682
1683 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1684 {
1685     CPUState *cs = CPU(cpu);
1686
1687     error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1688                  str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1689                  ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1690     s390_cpu_halt(cpu);
1691     guest_panicked();
1692 }
1693
1694 static int handle_intercept(S390CPU *cpu)
1695 {
1696     CPUState *cs = CPU(cpu);
1697     struct kvm_run *run = cs->kvm_run;
1698     int icpt_code = run->s390_sieic.icptcode;
1699     int r = 0;
1700
1701     DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1702             (long)cs->kvm_run->psw_addr);
1703     switch (icpt_code) {
1704         case ICPT_INSTRUCTION:
1705             r = handle_instruction(cpu, run);
1706             break;
1707         case ICPT_PROGRAM:
1708             unmanageable_intercept(cpu, "program interrupt",
1709                                    offsetof(LowCore, program_new_psw));
1710             r = EXCP_HALTED;
1711             break;
1712         case ICPT_EXT_INT:
1713             unmanageable_intercept(cpu, "external interrupt",
1714                                    offsetof(LowCore, external_new_psw));
1715             r = EXCP_HALTED;
1716             break;
1717         case ICPT_WAITPSW:
1718             /* disabled wait, since enabled wait is handled in kernel */
1719             cpu_synchronize_state(cs);
1720             if (s390_cpu_halt(cpu) == 0) {
1721                 if (is_special_wait_psw(cs)) {
1722                     qemu_system_shutdown_request();
1723                 } else {
1724                     guest_panicked();
1725                 }
1726             }
1727             r = EXCP_HALTED;
1728             break;
1729         case ICPT_CPU_STOP:
1730             if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1731                 qemu_system_shutdown_request();
1732             }
1733             if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1734                 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1735                                       true);
1736             }
1737             cpu->env.sigp_order = 0;
1738             r = EXCP_HALTED;
1739             break;
1740         case ICPT_SOFT_INTERCEPT:
1741             fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1742             exit(1);
1743             break;
1744         case ICPT_IO:
1745             fprintf(stderr, "KVM unimplemented icpt IO\n");
1746             exit(1);
1747             break;
1748         default:
1749             fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1750             exit(1);
1751             break;
1752     }
1753
1754     return r;
1755 }
1756
1757 static int handle_tsch(S390CPU *cpu)
1758 {
1759     CPUState *cs = CPU(cpu);
1760     struct kvm_run *run = cs->kvm_run;
1761     int ret;
1762
1763     cpu_synchronize_state(cs);
1764
1765     ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1766     if (ret < 0) {
1767         /*
1768          * Failure.
1769          * If an I/O interrupt had been dequeued, we have to reinject it.
1770          */
1771         if (run->s390_tsch.dequeued) {
1772             kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1773                                   run->s390_tsch.subchannel_nr,
1774                                   run->s390_tsch.io_int_parm,
1775                                   run->s390_tsch.io_int_word);
1776         }
1777         ret = 0;
1778     }
1779     return ret;
1780 }
1781
1782 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1783 {
1784     struct sysib_322 sysib;
1785     int del;
1786
1787     if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1788         return;
1789     }
1790     /* Shift the stack of Extended Names to prepare for our own data */
1791     memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1792             sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1793     /* First virt level, that doesn't provide Ext Names delimits stack. It is
1794      * assumed it's not capable of managing Extended Names for lower levels.
1795      */
1796     for (del = 1; del < sysib.count; del++) {
1797         if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1798             break;
1799         }
1800     }
1801     if (del < sysib.count) {
1802         memset(sysib.ext_names[del], 0,
1803                sizeof(sysib.ext_names[0]) * (sysib.count - del));
1804     }
1805     /* Insert short machine name in EBCDIC, padded with blanks */
1806     if (qemu_name) {
1807         memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1808         ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1809                                                     strlen(qemu_name)));
1810     }
1811     sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1812     memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1813     /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1814      * considered by s390 as not capable of providing any Extended Name.
1815      * Therefore if no name was specified on qemu invocation, we go with the
1816      * same "KVMguest" default, which KVM has filled into short name field.
1817      */
1818     if (qemu_name) {
1819         strncpy((char *)sysib.ext_names[0], qemu_name,
1820                 sizeof(sysib.ext_names[0]));
1821     } else {
1822         strcpy((char *)sysib.ext_names[0], "KVMguest");
1823     }
1824     /* Insert UUID */
1825     memcpy(sysib.vm[0].uuid, qemu_uuid, sizeof(sysib.vm[0].uuid));
1826
1827     s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1828 }
1829
1830 static int handle_stsi(S390CPU *cpu)
1831 {
1832     CPUState *cs = CPU(cpu);
1833     struct kvm_run *run = cs->kvm_run;
1834
1835     switch (run->s390_stsi.fc) {
1836     case 3:
1837         if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1838             return 0;
1839         }
1840         /* Only sysib 3.2.2 needs post-handling for now. */
1841         insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1842         return 0;
1843     default:
1844         return 0;
1845     }
1846 }
1847
1848 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1849 {
1850     CPUState *cs = CPU(cpu);
1851     struct kvm_run *run = cs->kvm_run;
1852
1853     int ret = 0;
1854     struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1855
1856     switch (arch_info->type) {
1857     case KVM_HW_WP_WRITE:
1858         if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1859             cs->watchpoint_hit = &hw_watchpoint;
1860             hw_watchpoint.vaddr = arch_info->addr;
1861             hw_watchpoint.flags = BP_MEM_WRITE;
1862             ret = EXCP_DEBUG;
1863         }
1864         break;
1865     case KVM_HW_BP:
1866         if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1867             ret = EXCP_DEBUG;
1868         }
1869         break;
1870     case KVM_SINGLESTEP:
1871         if (cs->singlestep_enabled) {
1872             ret = EXCP_DEBUG;
1873         }
1874         break;
1875     default:
1876         ret = -ENOSYS;
1877     }
1878
1879     return ret;
1880 }
1881
1882 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1883 {
1884     S390CPU *cpu = S390_CPU(cs);
1885     int ret = 0;
1886
1887     switch (run->exit_reason) {
1888         case KVM_EXIT_S390_SIEIC:
1889             ret = handle_intercept(cpu);
1890             break;
1891         case KVM_EXIT_S390_RESET:
1892             s390_reipl_request();
1893             break;
1894         case KVM_EXIT_S390_TSCH:
1895             ret = handle_tsch(cpu);
1896             break;
1897         case KVM_EXIT_S390_STSI:
1898             ret = handle_stsi(cpu);
1899             break;
1900         case KVM_EXIT_DEBUG:
1901             ret = kvm_arch_handle_debug_exit(cpu);
1902             break;
1903         default:
1904             fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1905             break;
1906     }
1907
1908     if (ret == 0) {
1909         ret = EXCP_INTERRUPT;
1910     }
1911     return ret;
1912 }
1913
1914 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1915 {
1916     return true;
1917 }
1918
1919 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
1920 {
1921     return 1;
1922 }
1923
1924 int kvm_arch_on_sigbus(int code, void *addr)
1925 {
1926     return 1;
1927 }
1928
1929 void kvm_s390_io_interrupt(uint16_t subchannel_id,
1930                            uint16_t subchannel_nr, uint32_t io_int_parm,
1931                            uint32_t io_int_word)
1932 {
1933     struct kvm_s390_irq irq = {
1934         .u.io.subchannel_id = subchannel_id,
1935         .u.io.subchannel_nr = subchannel_nr,
1936         .u.io.io_int_parm = io_int_parm,
1937         .u.io.io_int_word = io_int_word,
1938     };
1939
1940     if (io_int_word & IO_INT_WORD_AI) {
1941         irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
1942     } else {
1943         irq.type = ((subchannel_id & 0xff00) << 24) |
1944             ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
1945     }
1946     kvm_s390_floating_interrupt(&irq);
1947 }
1948
1949 void kvm_s390_crw_mchk(void)
1950 {
1951     struct kvm_s390_irq irq = {
1952         .type = KVM_S390_MCHK,
1953         .u.mchk.cr14 = 1 << 28,
1954         .u.mchk.mcic = 0x00400f1d40330000ULL,
1955     };
1956     kvm_s390_floating_interrupt(&irq);
1957 }
1958
1959 void kvm_s390_enable_css_support(S390CPU *cpu)
1960 {
1961     int r;
1962
1963     /* Activate host kernel channel subsystem support. */
1964     r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1965     assert(r == 0);
1966 }
1967
1968 void kvm_arch_init_irq_routing(KVMState *s)
1969 {
1970     /*
1971      * Note that while irqchip capabilities generally imply that cpustates
1972      * are handled in-kernel, it is not true for s390 (yet); therefore, we
1973      * have to override the common code kvm_halt_in_kernel_allowed setting.
1974      */
1975     if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1976         kvm_gsi_routing_allowed = true;
1977         kvm_halt_in_kernel_allowed = false;
1978     }
1979 }
1980
1981 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1982                                     int vq, bool assign)
1983 {
1984     struct kvm_ioeventfd kick = {
1985         .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1986         KVM_IOEVENTFD_FLAG_DATAMATCH,
1987         .fd = event_notifier_get_fd(notifier),
1988         .datamatch = vq,
1989         .addr = sch,
1990         .len = 8,
1991     };
1992     if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1993         return -ENOSYS;
1994     }
1995     if (!assign) {
1996         kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1997     }
1998     return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1999 }
2000
2001 int kvm_s390_get_memslot_count(KVMState *s)
2002 {
2003     return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
2004 }
2005
2006 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2007 {
2008     struct kvm_mp_state mp_state = {};
2009     int ret;
2010
2011     /* the kvm part might not have been initialized yet */
2012     if (CPU(cpu)->kvm_state == NULL) {
2013         return 0;
2014     }
2015
2016     switch (cpu_state) {
2017     case CPU_STATE_STOPPED:
2018         mp_state.mp_state = KVM_MP_STATE_STOPPED;
2019         break;
2020     case CPU_STATE_CHECK_STOP:
2021         mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2022         break;
2023     case CPU_STATE_OPERATING:
2024         mp_state.mp_state = KVM_MP_STATE_OPERATING;
2025         break;
2026     case CPU_STATE_LOAD:
2027         mp_state.mp_state = KVM_MP_STATE_LOAD;
2028         break;
2029     default:
2030         error_report("Requested CPU state is not a valid S390 CPU state: %u",
2031                      cpu_state);
2032         exit(1);
2033     }
2034
2035     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2036     if (ret) {
2037         trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2038                                        strerror(-ret));
2039     }
2040
2041     return ret;
2042 }
2043
2044 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2045                               uint64_t address, uint32_t data)
2046 {
2047     S390PCIBusDevice *pbdev;
2048     uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
2049     uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2050
2051     pbdev = s390_pci_find_dev_by_fid(fid);
2052     if (!pbdev) {
2053         DPRINTF("add_msi_route no dev\n");
2054         return -ENODEV;
2055     }
2056
2057     pbdev->routes.adapter.ind_offset = vec;
2058
2059     route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2060     route->flags = 0;
2061     route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2062     route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2063     route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2064     route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2065     route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2066     return 0;
2067 }
This page took 0.134156 seconds and 4 git commands to generate.