xref: /qemu/target/s390x/kvm/kvm.c (revision 650d103d3ea959212f826acb9d3fe80cf30e347b)
1 /*
2  * QEMU S390x KVM implementation
3  *
4  * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5  * Copyright IBM Corp. 2012
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "qemu/osdep.h"
22 #include <sys/ioctl.h>
23 
24 #include <linux/kvm.h>
25 #include <asm/ptrace.h>
26 
27 #include "qemu-common.h"
28 #include "cpu.h"
29 #include "internal.h"
30 #include "kvm_s390x.h"
31 #include "qapi/error.h"
32 #include "qemu/error-report.h"
33 #include "qemu/timer.h"
34 #include "qemu/units.h"
35 #include "qemu/mmap-alloc.h"
36 #include "qemu/log.h"
37 #include "sysemu/sysemu.h"
38 #include "sysemu/hw_accel.h"
39 #include "sysemu/device_tree.h"
40 #include "exec/gdbstub.h"
41 #include "exec/ram_addr.h"
42 #include "trace.h"
43 #include "hw/s390x/s390-pci-inst.h"
44 #include "hw/s390x/s390-pci-bus.h"
45 #include "hw/s390x/ipl.h"
46 #include "hw/s390x/ebcdic.h"
47 #include "exec/memattrs.h"
48 #include "hw/s390x/s390-virtio-ccw.h"
49 #include "hw/s390x/s390-virtio-hcall.h"
50 
51 #ifndef DEBUG_KVM
52 #define DEBUG_KVM  0
53 #endif
54 
55 #define DPRINTF(fmt, ...) do {                \
56     if (DEBUG_KVM) {                          \
57         fprintf(stderr, fmt, ## __VA_ARGS__); \
58     }                                         \
59 } while (0)
60 
61 #define kvm_vm_check_mem_attr(s, attr) \
62     kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
63 
64 #define IPA0_DIAG                       0x8300
65 #define IPA0_SIGP                       0xae00
66 #define IPA0_B2                         0xb200
67 #define IPA0_B9                         0xb900
68 #define IPA0_EB                         0xeb00
69 #define IPA0_E3                         0xe300
70 
71 #define PRIV_B2_SCLP_CALL               0x20
72 #define PRIV_B2_CSCH                    0x30
73 #define PRIV_B2_HSCH                    0x31
74 #define PRIV_B2_MSCH                    0x32
75 #define PRIV_B2_SSCH                    0x33
76 #define PRIV_B2_STSCH                   0x34
77 #define PRIV_B2_TSCH                    0x35
78 #define PRIV_B2_TPI                     0x36
79 #define PRIV_B2_SAL                     0x37
80 #define PRIV_B2_RSCH                    0x38
81 #define PRIV_B2_STCRW                   0x39
82 #define PRIV_B2_STCPS                   0x3a
83 #define PRIV_B2_RCHP                    0x3b
84 #define PRIV_B2_SCHM                    0x3c
85 #define PRIV_B2_CHSC                    0x5f
86 #define PRIV_B2_SIGA                    0x74
87 #define PRIV_B2_XSCH                    0x76
88 
89 #define PRIV_EB_SQBS                    0x8a
90 #define PRIV_EB_PCISTB                  0xd0
91 #define PRIV_EB_SIC                     0xd1
92 
93 #define PRIV_B9_EQBS                    0x9c
94 #define PRIV_B9_CLP                     0xa0
95 #define PRIV_B9_PCISTG                  0xd0
96 #define PRIV_B9_PCILG                   0xd2
97 #define PRIV_B9_RPCIT                   0xd3
98 
99 #define PRIV_E3_MPCIFC                  0xd0
100 #define PRIV_E3_STPCIFC                 0xd4
101 
102 #define DIAG_TIMEREVENT                 0x288
103 #define DIAG_IPL                        0x308
104 #define DIAG_KVM_HYPERCALL              0x500
105 #define DIAG_KVM_BREAKPOINT             0x501
106 
107 #define ICPT_INSTRUCTION                0x04
108 #define ICPT_PROGRAM                    0x08
109 #define ICPT_EXT_INT                    0x14
110 #define ICPT_WAITPSW                    0x1c
111 #define ICPT_SOFT_INTERCEPT             0x24
112 #define ICPT_CPU_STOP                   0x28
113 #define ICPT_OPEREXC                    0x2c
114 #define ICPT_IO                         0x40
115 
116 #define NR_LOCAL_IRQS 32
117 /*
118  * Needs to be big enough to contain max_cpus emergency signals
119  * and in addition NR_LOCAL_IRQS interrupts
120  */
121 #define VCPU_IRQ_BUF_SIZE(max_cpus) (sizeof(struct kvm_s390_irq) * \
122                                      (max_cpus + NR_LOCAL_IRQS))
123 
124 static CPUWatchpoint hw_watchpoint;
125 /*
126  * We don't use a list because this structure is also used to transmit the
127  * hardware breakpoints to the kernel.
128  */
129 static struct kvm_hw_breakpoint *hw_breakpoints;
130 static int nb_hw_breakpoints;
131 
132 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
133     KVM_CAP_LAST_INFO
134 };
135 
136 static int cap_sync_regs;
137 static int cap_async_pf;
138 static int cap_mem_op;
139 static int cap_s390_irq;
140 static int cap_ri;
141 static int cap_gs;
142 static int cap_hpage_1m;
143 
144 static int active_cmma;
145 
146 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared);
147 
148 static int kvm_s390_query_mem_limit(uint64_t *memory_limit)
149 {
150     struct kvm_device_attr attr = {
151         .group = KVM_S390_VM_MEM_CTRL,
152         .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
153         .addr = (uint64_t) memory_limit,
154     };
155 
156     return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
157 }
158 
159 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit)
160 {
161     int rc;
162 
163     struct kvm_device_attr attr = {
164         .group = KVM_S390_VM_MEM_CTRL,
165         .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
166         .addr = (uint64_t) &new_limit,
167     };
168 
169     if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) {
170         return 0;
171     }
172 
173     rc = kvm_s390_query_mem_limit(hw_limit);
174     if (rc) {
175         return rc;
176     } else if (*hw_limit < new_limit) {
177         return -E2BIG;
178     }
179 
180     return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
181 }
182 
183 int kvm_s390_cmma_active(void)
184 {
185     return active_cmma;
186 }
187 
188 static bool kvm_s390_cmma_available(void)
189 {
190     static bool initialized, value;
191 
192     if (!initialized) {
193         initialized = true;
194         value = kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_ENABLE_CMMA) &&
195                 kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_CLR_CMMA);
196     }
197     return value;
198 }
199 
200 void kvm_s390_cmma_reset(void)
201 {
202     int rc;
203     struct kvm_device_attr attr = {
204         .group = KVM_S390_VM_MEM_CTRL,
205         .attr = KVM_S390_VM_MEM_CLR_CMMA,
206     };
207 
208     if (!kvm_s390_cmma_active()) {
209         return;
210     }
211 
212     rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
213     trace_kvm_clear_cmma(rc);
214 }
215 
216 static void kvm_s390_enable_cmma(void)
217 {
218     int rc;
219     struct kvm_device_attr attr = {
220         .group = KVM_S390_VM_MEM_CTRL,
221         .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
222     };
223 
224     if (cap_hpage_1m) {
225         warn_report("CMM will not be enabled because it is not "
226                     "compatible with huge memory backings.");
227         return;
228     }
229     rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
230     active_cmma = !rc;
231     trace_kvm_enable_cmma(rc);
232 }
233 
234 static void kvm_s390_set_attr(uint64_t attr)
235 {
236     struct kvm_device_attr attribute = {
237         .group = KVM_S390_VM_CRYPTO,
238         .attr  = attr,
239     };
240 
241     int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
242 
243     if (ret) {
244         error_report("Failed to set crypto device attribute %lu: %s",
245                      attr, strerror(-ret));
246     }
247 }
248 
249 static void kvm_s390_init_aes_kw(void)
250 {
251     uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
252 
253     if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
254                                  NULL)) {
255             attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
256     }
257 
258     if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
259             kvm_s390_set_attr(attr);
260     }
261 }
262 
263 static void kvm_s390_init_dea_kw(void)
264 {
265     uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
266 
267     if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
268                                  NULL)) {
269             attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
270     }
271 
272     if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
273             kvm_s390_set_attr(attr);
274     }
275 }
276 
277 void kvm_s390_crypto_reset(void)
278 {
279     if (s390_has_feat(S390_FEAT_MSA_EXT_3)) {
280         kvm_s390_init_aes_kw();
281         kvm_s390_init_dea_kw();
282     }
283 }
284 
285 void kvm_s390_set_max_pagesize(uint64_t pagesize, Error **errp)
286 {
287     if (pagesize == 4 * KiB) {
288         return;
289     }
290 
291     if (!hpage_1m_allowed()) {
292         error_setg(errp, "This QEMU machine does not support huge page "
293                    "mappings");
294         return;
295     }
296 
297     if (pagesize != 1 * MiB) {
298         error_setg(errp, "Memory backing with 2G pages was specified, "
299                    "but KVM does not support this memory backing");
300         return;
301     }
302 
303     if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_HPAGE_1M, 0)) {
304         error_setg(errp, "Memory backing with 1M pages was specified, "
305                    "but KVM does not support this memory backing");
306         return;
307     }
308 
309     cap_hpage_1m = 1;
310 }
311 
312 int kvm_arch_init(MachineState *ms, KVMState *s)
313 {
314     MachineClass *mc = MACHINE_GET_CLASS(ms);
315 
316     mc->default_cpu_type = S390_CPU_TYPE_NAME("host");
317     cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
318     cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
319     cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
320     cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
321 
322     if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
323         || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
324         phys_mem_set_alloc(legacy_s390_alloc);
325     }
326 
327     kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
328     kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
329     kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
330     if (ri_allowed()) {
331         if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) {
332             cap_ri = 1;
333         }
334     }
335     if (cpu_model_allowed()) {
336         if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) {
337             cap_gs = 1;
338         }
339     }
340 
341     /*
342      * The migration interface for ais was introduced with kernel 4.13
343      * but the capability itself had been active since 4.12. As migration
344      * support is considered necessary let's disable ais in the 2.10
345      * machine.
346      */
347     /* kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0); */
348 
349     return 0;
350 }
351 
352 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
353 {
354     return 0;
355 }
356 
357 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
358 {
359     return cpu->cpu_index;
360 }
361 
362 int kvm_arch_init_vcpu(CPUState *cs)
363 {
364     unsigned int max_cpus = MACHINE(qdev_get_machine())->smp.max_cpus;
365     S390CPU *cpu = S390_CPU(cs);
366     kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
367     cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE(max_cpus));
368     return 0;
369 }
370 
371 int kvm_arch_destroy_vcpu(CPUState *cs)
372 {
373     S390CPU *cpu = S390_CPU(cs);
374 
375     g_free(cpu->irqstate);
376     cpu->irqstate = NULL;
377 
378     return 0;
379 }
380 
381 void kvm_s390_reset_vcpu(S390CPU *cpu)
382 {
383     CPUState *cs = CPU(cpu);
384 
385     /* The initial reset call is needed here to reset in-kernel
386      * vcpu data that we can't access directly from QEMU
387      * (i.e. with older kernels which don't support sync_regs/ONE_REG).
388      * Before this ioctl cpu_synchronize_state() is called in common kvm
389      * code (kvm-all) */
390     if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
391         error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
392     }
393 }
394 
395 static int can_sync_regs(CPUState *cs, int regs)
396 {
397     return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
398 }
399 
400 int kvm_arch_put_registers(CPUState *cs, int level)
401 {
402     S390CPU *cpu = S390_CPU(cs);
403     CPUS390XState *env = &cpu->env;
404     struct kvm_sregs sregs;
405     struct kvm_regs regs;
406     struct kvm_fpu fpu = {};
407     int r;
408     int i;
409 
410     /* always save the PSW  and the GPRS*/
411     cs->kvm_run->psw_addr = env->psw.addr;
412     cs->kvm_run->psw_mask = env->psw.mask;
413 
414     if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
415         for (i = 0; i < 16; i++) {
416             cs->kvm_run->s.regs.gprs[i] = env->regs[i];
417             cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
418         }
419     } else {
420         for (i = 0; i < 16; i++) {
421             regs.gprs[i] = env->regs[i];
422         }
423         r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
424         if (r < 0) {
425             return r;
426         }
427     }
428 
429     if (can_sync_regs(cs, KVM_SYNC_VRS)) {
430         for (i = 0; i < 32; i++) {
431             cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0];
432             cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1];
433         }
434         cs->kvm_run->s.regs.fpc = env->fpc;
435         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
436     } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
437         for (i = 0; i < 16; i++) {
438             cs->kvm_run->s.regs.fprs[i] = *get_freg(env, i);
439         }
440         cs->kvm_run->s.regs.fpc = env->fpc;
441         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS;
442     } else {
443         /* Floating point */
444         for (i = 0; i < 16; i++) {
445             fpu.fprs[i] = *get_freg(env, i);
446         }
447         fpu.fpc = env->fpc;
448 
449         r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
450         if (r < 0) {
451             return r;
452         }
453     }
454 
455     /* Do we need to save more than that? */
456     if (level == KVM_PUT_RUNTIME_STATE) {
457         return 0;
458     }
459 
460     if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
461         cs->kvm_run->s.regs.cputm = env->cputm;
462         cs->kvm_run->s.regs.ckc = env->ckc;
463         cs->kvm_run->s.regs.todpr = env->todpr;
464         cs->kvm_run->s.regs.gbea = env->gbea;
465         cs->kvm_run->s.regs.pp = env->pp;
466         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
467     } else {
468         /*
469          * These ONE_REGS are not protected by a capability. As they are only
470          * necessary for migration we just trace a possible error, but don't
471          * return with an error return code.
472          */
473         kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
474         kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
475         kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
476         kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
477         kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
478     }
479 
480     if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
481         memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64);
482         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB;
483     }
484 
485     /* pfault parameters */
486     if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
487         cs->kvm_run->s.regs.pft = env->pfault_token;
488         cs->kvm_run->s.regs.pfs = env->pfault_select;
489         cs->kvm_run->s.regs.pfc = env->pfault_compare;
490         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
491     } else if (cap_async_pf) {
492         r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
493         if (r < 0) {
494             return r;
495         }
496         r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
497         if (r < 0) {
498             return r;
499         }
500         r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
501         if (r < 0) {
502             return r;
503         }
504     }
505 
506     /* access registers and control registers*/
507     if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
508         for (i = 0; i < 16; i++) {
509             cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
510             cs->kvm_run->s.regs.crs[i] = env->cregs[i];
511         }
512         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
513         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
514     } else {
515         for (i = 0; i < 16; i++) {
516             sregs.acrs[i] = env->aregs[i];
517             sregs.crs[i] = env->cregs[i];
518         }
519         r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
520         if (r < 0) {
521             return r;
522         }
523     }
524 
525     if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
526         memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32);
527         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB;
528     }
529 
530     if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
531         cs->kvm_run->s.regs.bpbc = env->bpbc;
532         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_BPBC;
533     }
534 
535     if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
536         cs->kvm_run->s.regs.etoken = env->etoken;
537         cs->kvm_run->s.regs.etoken_extension  = env->etoken_extension;
538         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ETOKEN;
539     }
540 
541     /* Finally the prefix */
542     if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
543         cs->kvm_run->s.regs.prefix = env->psa;
544         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
545     } else {
546         /* prefix is only supported via sync regs */
547     }
548     return 0;
549 }
550 
551 int kvm_arch_get_registers(CPUState *cs)
552 {
553     S390CPU *cpu = S390_CPU(cs);
554     CPUS390XState *env = &cpu->env;
555     struct kvm_sregs sregs;
556     struct kvm_regs regs;
557     struct kvm_fpu fpu;
558     int i, r;
559 
560     /* get the PSW */
561     env->psw.addr = cs->kvm_run->psw_addr;
562     env->psw.mask = cs->kvm_run->psw_mask;
563 
564     /* the GPRS */
565     if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
566         for (i = 0; i < 16; i++) {
567             env->regs[i] = cs->kvm_run->s.regs.gprs[i];
568         }
569     } else {
570         r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
571         if (r < 0) {
572             return r;
573         }
574          for (i = 0; i < 16; i++) {
575             env->regs[i] = regs.gprs[i];
576         }
577     }
578 
579     /* The ACRS and CRS */
580     if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
581         for (i = 0; i < 16; i++) {
582             env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
583             env->cregs[i] = cs->kvm_run->s.regs.crs[i];
584         }
585     } else {
586         r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
587         if (r < 0) {
588             return r;
589         }
590          for (i = 0; i < 16; i++) {
591             env->aregs[i] = sregs.acrs[i];
592             env->cregs[i] = sregs.crs[i];
593         }
594     }
595 
596     /* Floating point and vector registers */
597     if (can_sync_regs(cs, KVM_SYNC_VRS)) {
598         for (i = 0; i < 32; i++) {
599             env->vregs[i][0] = cs->kvm_run->s.regs.vrs[i][0];
600             env->vregs[i][1] = cs->kvm_run->s.regs.vrs[i][1];
601         }
602         env->fpc = cs->kvm_run->s.regs.fpc;
603     } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) {
604         for (i = 0; i < 16; i++) {
605             *get_freg(env, i) = cs->kvm_run->s.regs.fprs[i];
606         }
607         env->fpc = cs->kvm_run->s.regs.fpc;
608     } else {
609         r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
610         if (r < 0) {
611             return r;
612         }
613         for (i = 0; i < 16; i++) {
614             *get_freg(env, i) = fpu.fprs[i];
615         }
616         env->fpc = fpu.fpc;
617     }
618 
619     /* The prefix */
620     if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
621         env->psa = cs->kvm_run->s.regs.prefix;
622     }
623 
624     if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
625         env->cputm = cs->kvm_run->s.regs.cputm;
626         env->ckc = cs->kvm_run->s.regs.ckc;
627         env->todpr = cs->kvm_run->s.regs.todpr;
628         env->gbea = cs->kvm_run->s.regs.gbea;
629         env->pp = cs->kvm_run->s.regs.pp;
630     } else {
631         /*
632          * These ONE_REGS are not protected by a capability. As they are only
633          * necessary for migration we just trace a possible error, but don't
634          * return with an error return code.
635          */
636         kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
637         kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
638         kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
639         kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
640         kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
641     }
642 
643     if (can_sync_regs(cs, KVM_SYNC_RICCB)) {
644         memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64);
645     }
646 
647     if (can_sync_regs(cs, KVM_SYNC_GSCB)) {
648         memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32);
649     }
650 
651     if (can_sync_regs(cs, KVM_SYNC_BPBC)) {
652         env->bpbc = cs->kvm_run->s.regs.bpbc;
653     }
654 
655     if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) {
656         env->etoken = cs->kvm_run->s.regs.etoken;
657         env->etoken_extension = cs->kvm_run->s.regs.etoken_extension;
658     }
659 
660     /* pfault parameters */
661     if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
662         env->pfault_token = cs->kvm_run->s.regs.pft;
663         env->pfault_select = cs->kvm_run->s.regs.pfs;
664         env->pfault_compare = cs->kvm_run->s.regs.pfc;
665     } else if (cap_async_pf) {
666         r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
667         if (r < 0) {
668             return r;
669         }
670         r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
671         if (r < 0) {
672             return r;
673         }
674         r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
675         if (r < 0) {
676             return r;
677         }
678     }
679 
680     return 0;
681 }
682 
683 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
684 {
685     int r;
686     struct kvm_device_attr attr = {
687         .group = KVM_S390_VM_TOD,
688         .attr = KVM_S390_VM_TOD_LOW,
689         .addr = (uint64_t)tod_low,
690     };
691 
692     r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
693     if (r) {
694         return r;
695     }
696 
697     attr.attr = KVM_S390_VM_TOD_HIGH;
698     attr.addr = (uint64_t)tod_high;
699     return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
700 }
701 
702 int kvm_s390_get_clock_ext(uint8_t *tod_high, uint64_t *tod_low)
703 {
704     int r;
705     struct kvm_s390_vm_tod_clock gtod;
706     struct kvm_device_attr attr = {
707         .group = KVM_S390_VM_TOD,
708         .attr = KVM_S390_VM_TOD_EXT,
709         .addr = (uint64_t)&gtod,
710     };
711 
712     r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
713     *tod_high = gtod.epoch_idx;
714     *tod_low  = gtod.tod;
715 
716     return r;
717 }
718 
719 int kvm_s390_set_clock(uint8_t tod_high, uint64_t tod_low)
720 {
721     int r;
722     struct kvm_device_attr attr = {
723         .group = KVM_S390_VM_TOD,
724         .attr = KVM_S390_VM_TOD_LOW,
725         .addr = (uint64_t)&tod_low,
726     };
727 
728     r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
729     if (r) {
730         return r;
731     }
732 
733     attr.attr = KVM_S390_VM_TOD_HIGH;
734     attr.addr = (uint64_t)&tod_high;
735     return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
736 }
737 
738 int kvm_s390_set_clock_ext(uint8_t tod_high, uint64_t tod_low)
739 {
740     struct kvm_s390_vm_tod_clock gtod = {
741         .epoch_idx = tod_high,
742         .tod  = tod_low,
743     };
744     struct kvm_device_attr attr = {
745         .group = KVM_S390_VM_TOD,
746         .attr = KVM_S390_VM_TOD_EXT,
747         .addr = (uint64_t)&gtod,
748     };
749 
750     return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
751 }
752 
753 /**
754  * kvm_s390_mem_op:
755  * @addr:      the logical start address in guest memory
756  * @ar:        the access register number
757  * @hostbuf:   buffer in host memory. NULL = do only checks w/o copying
758  * @len:       length that should be transferred
759  * @is_write:  true = write, false = read
760  * Returns:    0 on success, non-zero if an exception or error occurred
761  *
762  * Use KVM ioctl to read/write from/to guest memory. An access exception
763  * is injected into the vCPU in case of translation errors.
764  */
765 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
766                     int len, bool is_write)
767 {
768     struct kvm_s390_mem_op mem_op = {
769         .gaddr = addr,
770         .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
771         .size = len,
772         .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
773                        : KVM_S390_MEMOP_LOGICAL_READ,
774         .buf = (uint64_t)hostbuf,
775         .ar = ar,
776     };
777     int ret;
778 
779     if (!cap_mem_op) {
780         return -ENOSYS;
781     }
782     if (!hostbuf) {
783         mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
784     }
785 
786     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
787     if (ret < 0) {
788         warn_report("KVM_S390_MEM_OP failed: %s", strerror(-ret));
789     }
790     return ret;
791 }
792 
793 /*
794  * Legacy layout for s390:
795  * Older S390 KVM requires the topmost vma of the RAM to be
796  * smaller than an system defined value, which is at least 256GB.
797  * Larger systems have larger values. We put the guest between
798  * the end of data segment (system break) and this value. We
799  * use 32GB as a base to have enough room for the system break
800  * to grow. We also have to use MAP parameters that avoid
801  * read-only mapping of guest pages.
802  */
803 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared)
804 {
805     static void *mem;
806 
807     if (mem) {
808         /* we only support one allocation, which is enough for initial ram */
809         return NULL;
810     }
811 
812     mem = mmap((void *) 0x800000000ULL, size,
813                PROT_EXEC|PROT_READ|PROT_WRITE,
814                MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
815     if (mem == MAP_FAILED) {
816         mem = NULL;
817     }
818     if (mem && align) {
819         *align = QEMU_VMALLOC_ALIGN;
820     }
821     return mem;
822 }
823 
824 static uint8_t const *sw_bp_inst;
825 static uint8_t sw_bp_ilen;
826 
827 static void determine_sw_breakpoint_instr(void)
828 {
829         /* DIAG 501 is used for sw breakpoints with old kernels */
830         static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
831         /* Instruction 0x0000 is used for sw breakpoints with recent kernels */
832         static const uint8_t instr_0x0000[] = {0x00, 0x00};
833 
834         if (sw_bp_inst) {
835             return;
836         }
837         if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) {
838             sw_bp_inst = diag_501;
839             sw_bp_ilen = sizeof(diag_501);
840             DPRINTF("KVM: will use 4-byte sw breakpoints.\n");
841         } else {
842             sw_bp_inst = instr_0x0000;
843             sw_bp_ilen = sizeof(instr_0x0000);
844             DPRINTF("KVM: will use 2-byte sw breakpoints.\n");
845         }
846 }
847 
848 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
849 {
850     determine_sw_breakpoint_instr();
851 
852     if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
853                             sw_bp_ilen, 0) ||
854         cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) {
855         return -EINVAL;
856     }
857     return 0;
858 }
859 
860 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
861 {
862     uint8_t t[MAX_ILEN];
863 
864     if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) {
865         return -EINVAL;
866     } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) {
867         return -EINVAL;
868     } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
869                                    sw_bp_ilen, 1)) {
870         return -EINVAL;
871     }
872 
873     return 0;
874 }
875 
876 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
877                                                     int len, int type)
878 {
879     int n;
880 
881     for (n = 0; n < nb_hw_breakpoints; n++) {
882         if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
883             (hw_breakpoints[n].len == len || len == -1)) {
884             return &hw_breakpoints[n];
885         }
886     }
887 
888     return NULL;
889 }
890 
891 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
892 {
893     int size;
894 
895     if (find_hw_breakpoint(addr, len, type)) {
896         return -EEXIST;
897     }
898 
899     size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
900 
901     if (!hw_breakpoints) {
902         nb_hw_breakpoints = 0;
903         hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
904     } else {
905         hw_breakpoints =
906             (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
907     }
908 
909     if (!hw_breakpoints) {
910         nb_hw_breakpoints = 0;
911         return -ENOMEM;
912     }
913 
914     hw_breakpoints[nb_hw_breakpoints].addr = addr;
915     hw_breakpoints[nb_hw_breakpoints].len = len;
916     hw_breakpoints[nb_hw_breakpoints].type = type;
917 
918     nb_hw_breakpoints++;
919 
920     return 0;
921 }
922 
923 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
924                                   target_ulong len, int type)
925 {
926     switch (type) {
927     case GDB_BREAKPOINT_HW:
928         type = KVM_HW_BP;
929         break;
930     case GDB_WATCHPOINT_WRITE:
931         if (len < 1) {
932             return -EINVAL;
933         }
934         type = KVM_HW_WP_WRITE;
935         break;
936     default:
937         return -ENOSYS;
938     }
939     return insert_hw_breakpoint(addr, len, type);
940 }
941 
942 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
943                                   target_ulong len, int type)
944 {
945     int size;
946     struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
947 
948     if (bp == NULL) {
949         return -ENOENT;
950     }
951 
952     nb_hw_breakpoints--;
953     if (nb_hw_breakpoints > 0) {
954         /*
955          * In order to trim the array, move the last element to the position to
956          * be removed - if necessary.
957          */
958         if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
959             *bp = hw_breakpoints[nb_hw_breakpoints];
960         }
961         size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
962         hw_breakpoints =
963              (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
964     } else {
965         g_free(hw_breakpoints);
966         hw_breakpoints = NULL;
967     }
968 
969     return 0;
970 }
971 
972 void kvm_arch_remove_all_hw_breakpoints(void)
973 {
974     nb_hw_breakpoints = 0;
975     g_free(hw_breakpoints);
976     hw_breakpoints = NULL;
977 }
978 
979 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
980 {
981     int i;
982 
983     if (nb_hw_breakpoints > 0) {
984         dbg->arch.nr_hw_bp = nb_hw_breakpoints;
985         dbg->arch.hw_bp = hw_breakpoints;
986 
987         for (i = 0; i < nb_hw_breakpoints; ++i) {
988             hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
989                                                        hw_breakpoints[i].addr);
990         }
991         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
992     } else {
993         dbg->arch.nr_hw_bp = 0;
994         dbg->arch.hw_bp = NULL;
995     }
996 }
997 
998 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
999 {
1000 }
1001 
1002 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1003 {
1004     return MEMTXATTRS_UNSPECIFIED;
1005 }
1006 
1007 int kvm_arch_process_async_events(CPUState *cs)
1008 {
1009     return cs->halted;
1010 }
1011 
1012 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
1013                                      struct kvm_s390_interrupt *interrupt)
1014 {
1015     int r = 0;
1016 
1017     interrupt->type = irq->type;
1018     switch (irq->type) {
1019     case KVM_S390_INT_VIRTIO:
1020         interrupt->parm = irq->u.ext.ext_params;
1021         /* fall through */
1022     case KVM_S390_INT_PFAULT_INIT:
1023     case KVM_S390_INT_PFAULT_DONE:
1024         interrupt->parm64 = irq->u.ext.ext_params2;
1025         break;
1026     case KVM_S390_PROGRAM_INT:
1027         interrupt->parm = irq->u.pgm.code;
1028         break;
1029     case KVM_S390_SIGP_SET_PREFIX:
1030         interrupt->parm = irq->u.prefix.address;
1031         break;
1032     case KVM_S390_INT_SERVICE:
1033         interrupt->parm = irq->u.ext.ext_params;
1034         break;
1035     case KVM_S390_MCHK:
1036         interrupt->parm = irq->u.mchk.cr14;
1037         interrupt->parm64 = irq->u.mchk.mcic;
1038         break;
1039     case KVM_S390_INT_EXTERNAL_CALL:
1040         interrupt->parm = irq->u.extcall.code;
1041         break;
1042     case KVM_S390_INT_EMERGENCY:
1043         interrupt->parm = irq->u.emerg.code;
1044         break;
1045     case KVM_S390_SIGP_STOP:
1046     case KVM_S390_RESTART:
1047         break; /* These types have no parameters */
1048     case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1049         interrupt->parm = irq->u.io.subchannel_id << 16;
1050         interrupt->parm |= irq->u.io.subchannel_nr;
1051         interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
1052         interrupt->parm64 |= irq->u.io.io_int_word;
1053         break;
1054     default:
1055         r = -EINVAL;
1056         break;
1057     }
1058     return r;
1059 }
1060 
1061 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
1062 {
1063     struct kvm_s390_interrupt kvmint = {};
1064     int r;
1065 
1066     r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1067     if (r < 0) {
1068         fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1069         exit(1);
1070     }
1071 
1072     r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
1073     if (r < 0) {
1074         fprintf(stderr, "KVM failed to inject interrupt\n");
1075         exit(1);
1076     }
1077 }
1078 
1079 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
1080 {
1081     CPUState *cs = CPU(cpu);
1082     int r;
1083 
1084     if (cap_s390_irq) {
1085         r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
1086         if (!r) {
1087             return;
1088         }
1089         error_report("KVM failed to inject interrupt %llx", irq->type);
1090         exit(1);
1091     }
1092 
1093     inject_vcpu_irq_legacy(cs, irq);
1094 }
1095 
1096 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq *irq)
1097 {
1098     struct kvm_s390_interrupt kvmint = {};
1099     int r;
1100 
1101     r = s390_kvm_irq_to_interrupt(irq, &kvmint);
1102     if (r < 0) {
1103         fprintf(stderr, "%s called with bogus interrupt\n", __func__);
1104         exit(1);
1105     }
1106 
1107     r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
1108     if (r < 0) {
1109         fprintf(stderr, "KVM failed to inject interrupt\n");
1110         exit(1);
1111     }
1112 }
1113 
1114 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code)
1115 {
1116     struct kvm_s390_irq irq = {
1117         .type = KVM_S390_PROGRAM_INT,
1118         .u.pgm.code = code,
1119     };
1120     qemu_log_mask(CPU_LOG_INT, "program interrupt at %#" PRIx64 "\n",
1121                   cpu->env.psw.addr);
1122     kvm_s390_vcpu_interrupt(cpu, &irq);
1123 }
1124 
1125 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
1126 {
1127     struct kvm_s390_irq irq = {
1128         .type = KVM_S390_PROGRAM_INT,
1129         .u.pgm.code = code,
1130         .u.pgm.trans_exc_code = te_code,
1131         .u.pgm.exc_access_id = te_code & 3,
1132     };
1133 
1134     kvm_s390_vcpu_interrupt(cpu, &irq);
1135 }
1136 
1137 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
1138                                  uint16_t ipbh0)
1139 {
1140     CPUS390XState *env = &cpu->env;
1141     uint64_t sccb;
1142     uint32_t code;
1143     int r = 0;
1144 
1145     sccb = env->regs[ipbh0 & 0xf];
1146     code = env->regs[(ipbh0 & 0xf0) >> 4];
1147 
1148     r = sclp_service_call(env, sccb, code);
1149     if (r < 0) {
1150         kvm_s390_program_interrupt(cpu, -r);
1151     } else {
1152         setcc(cpu, r);
1153     }
1154 
1155     return 0;
1156 }
1157 
1158 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1159 {
1160     CPUS390XState *env = &cpu->env;
1161     int rc = 0;
1162     uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1163 
1164     switch (ipa1) {
1165     case PRIV_B2_XSCH:
1166         ioinst_handle_xsch(cpu, env->regs[1], RA_IGNORED);
1167         break;
1168     case PRIV_B2_CSCH:
1169         ioinst_handle_csch(cpu, env->regs[1], RA_IGNORED);
1170         break;
1171     case PRIV_B2_HSCH:
1172         ioinst_handle_hsch(cpu, env->regs[1], RA_IGNORED);
1173         break;
1174     case PRIV_B2_MSCH:
1175         ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1176         break;
1177     case PRIV_B2_SSCH:
1178         ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1179         break;
1180     case PRIV_B2_STCRW:
1181         ioinst_handle_stcrw(cpu, run->s390_sieic.ipb, RA_IGNORED);
1182         break;
1183     case PRIV_B2_STSCH:
1184         ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED);
1185         break;
1186     case PRIV_B2_TSCH:
1187         /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1188         fprintf(stderr, "Spurious tsch intercept\n");
1189         break;
1190     case PRIV_B2_CHSC:
1191         ioinst_handle_chsc(cpu, run->s390_sieic.ipb, RA_IGNORED);
1192         break;
1193     case PRIV_B2_TPI:
1194         /* This should have been handled by kvm already. */
1195         fprintf(stderr, "Spurious tpi intercept\n");
1196         break;
1197     case PRIV_B2_SCHM:
1198         ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1199                            run->s390_sieic.ipb, RA_IGNORED);
1200         break;
1201     case PRIV_B2_RSCH:
1202         ioinst_handle_rsch(cpu, env->regs[1], RA_IGNORED);
1203         break;
1204     case PRIV_B2_RCHP:
1205         ioinst_handle_rchp(cpu, env->regs[1], RA_IGNORED);
1206         break;
1207     case PRIV_B2_STCPS:
1208         /* We do not provide this instruction, it is suppressed. */
1209         break;
1210     case PRIV_B2_SAL:
1211         ioinst_handle_sal(cpu, env->regs[1], RA_IGNORED);
1212         break;
1213     case PRIV_B2_SIGA:
1214         /* Not provided, set CC = 3 for subchannel not operational */
1215         setcc(cpu, 3);
1216         break;
1217     case PRIV_B2_SCLP_CALL:
1218         rc = kvm_sclp_service_call(cpu, run, ipbh0);
1219         break;
1220     default:
1221         rc = -1;
1222         DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1223         break;
1224     }
1225 
1226     return rc;
1227 }
1228 
1229 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1230                                   uint8_t *ar)
1231 {
1232     CPUS390XState *env = &cpu->env;
1233     uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1234     uint32_t base2 = run->s390_sieic.ipb >> 28;
1235     uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1236                      ((run->s390_sieic.ipb & 0xff00) << 4);
1237 
1238     if (disp2 & 0x80000) {
1239         disp2 += 0xfff00000;
1240     }
1241     if (ar) {
1242         *ar = base2;
1243     }
1244 
1245     return (base2 ? env->regs[base2] : 0) +
1246            (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1247 }
1248 
1249 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1250                                   uint8_t *ar)
1251 {
1252     CPUS390XState *env = &cpu->env;
1253     uint32_t base2 = run->s390_sieic.ipb >> 28;
1254     uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1255                      ((run->s390_sieic.ipb & 0xff00) << 4);
1256 
1257     if (disp2 & 0x80000) {
1258         disp2 += 0xfff00000;
1259     }
1260     if (ar) {
1261         *ar = base2;
1262     }
1263 
1264     return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1265 }
1266 
1267 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1268 {
1269     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1270 
1271     if (s390_has_feat(S390_FEAT_ZPCI)) {
1272         return clp_service_call(cpu, r2, RA_IGNORED);
1273     } else {
1274         return -1;
1275     }
1276 }
1277 
1278 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1279 {
1280     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1281     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1282 
1283     if (s390_has_feat(S390_FEAT_ZPCI)) {
1284         return pcilg_service_call(cpu, r1, r2, RA_IGNORED);
1285     } else {
1286         return -1;
1287     }
1288 }
1289 
1290 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1291 {
1292     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1293     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1294 
1295     if (s390_has_feat(S390_FEAT_ZPCI)) {
1296         return pcistg_service_call(cpu, r1, r2, RA_IGNORED);
1297     } else {
1298         return -1;
1299     }
1300 }
1301 
1302 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1303 {
1304     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1305     uint64_t fiba;
1306     uint8_t ar;
1307 
1308     if (s390_has_feat(S390_FEAT_ZPCI)) {
1309         fiba = get_base_disp_rxy(cpu, run, &ar);
1310 
1311         return stpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1312     } else {
1313         return -1;
1314     }
1315 }
1316 
1317 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1318 {
1319     CPUS390XState *env = &cpu->env;
1320     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1321     uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1322     uint8_t isc;
1323     uint16_t mode;
1324     int r;
1325 
1326     mode = env->regs[r1] & 0xffff;
1327     isc = (env->regs[r3] >> 27) & 0x7;
1328     r = css_do_sic(env, isc, mode);
1329     if (r) {
1330         kvm_s390_program_interrupt(cpu, -r);
1331     }
1332 
1333     return 0;
1334 }
1335 
1336 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1337 {
1338     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1339     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1340 
1341     if (s390_has_feat(S390_FEAT_ZPCI)) {
1342         return rpcit_service_call(cpu, r1, r2, RA_IGNORED);
1343     } else {
1344         return -1;
1345     }
1346 }
1347 
1348 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1349 {
1350     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1351     uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1352     uint64_t gaddr;
1353     uint8_t ar;
1354 
1355     if (s390_has_feat(S390_FEAT_ZPCI)) {
1356         gaddr = get_base_disp_rsy(cpu, run, &ar);
1357 
1358         return pcistb_service_call(cpu, r1, r3, gaddr, ar, RA_IGNORED);
1359     } else {
1360         return -1;
1361     }
1362 }
1363 
1364 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1365 {
1366     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1367     uint64_t fiba;
1368     uint8_t ar;
1369 
1370     if (s390_has_feat(S390_FEAT_ZPCI)) {
1371         fiba = get_base_disp_rxy(cpu, run, &ar);
1372 
1373         return mpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED);
1374     } else {
1375         return -1;
1376     }
1377 }
1378 
1379 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1380 {
1381     int r = 0;
1382 
1383     switch (ipa1) {
1384     case PRIV_B9_CLP:
1385         r = kvm_clp_service_call(cpu, run);
1386         break;
1387     case PRIV_B9_PCISTG:
1388         r = kvm_pcistg_service_call(cpu, run);
1389         break;
1390     case PRIV_B9_PCILG:
1391         r = kvm_pcilg_service_call(cpu, run);
1392         break;
1393     case PRIV_B9_RPCIT:
1394         r = kvm_rpcit_service_call(cpu, run);
1395         break;
1396     case PRIV_B9_EQBS:
1397         /* just inject exception */
1398         r = -1;
1399         break;
1400     default:
1401         r = -1;
1402         DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1403         break;
1404     }
1405 
1406     return r;
1407 }
1408 
1409 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1410 {
1411     int r = 0;
1412 
1413     switch (ipbl) {
1414     case PRIV_EB_PCISTB:
1415         r = kvm_pcistb_service_call(cpu, run);
1416         break;
1417     case PRIV_EB_SIC:
1418         r = kvm_sic_service_call(cpu, run);
1419         break;
1420     case PRIV_EB_SQBS:
1421         /* just inject exception */
1422         r = -1;
1423         break;
1424     default:
1425         r = -1;
1426         DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1427         break;
1428     }
1429 
1430     return r;
1431 }
1432 
1433 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1434 {
1435     int r = 0;
1436 
1437     switch (ipbl) {
1438     case PRIV_E3_MPCIFC:
1439         r = kvm_mpcifc_service_call(cpu, run);
1440         break;
1441     case PRIV_E3_STPCIFC:
1442         r = kvm_stpcifc_service_call(cpu, run);
1443         break;
1444     default:
1445         r = -1;
1446         DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1447         break;
1448     }
1449 
1450     return r;
1451 }
1452 
1453 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1454 {
1455     CPUS390XState *env = &cpu->env;
1456     int ret;
1457 
1458     ret = s390_virtio_hypercall(env);
1459     if (ret == -EINVAL) {
1460         kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1461         return 0;
1462     }
1463 
1464     return ret;
1465 }
1466 
1467 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run)
1468 {
1469     uint64_t r1, r3;
1470     int rc;
1471 
1472     r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1473     r3 = run->s390_sieic.ipa & 0x000f;
1474     rc = handle_diag_288(&cpu->env, r1, r3);
1475     if (rc) {
1476         kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1477     }
1478 }
1479 
1480 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1481 {
1482     uint64_t r1, r3;
1483 
1484     r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1485     r3 = run->s390_sieic.ipa & 0x000f;
1486     handle_diag_308(&cpu->env, r1, r3, RA_IGNORED);
1487 }
1488 
1489 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1490 {
1491     CPUS390XState *env = &cpu->env;
1492     unsigned long pc;
1493 
1494     pc = env->psw.addr - sw_bp_ilen;
1495     if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1496         env->psw.addr = pc;
1497         return EXCP_DEBUG;
1498     }
1499 
1500     return -ENOENT;
1501 }
1502 
1503 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1504 
1505 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1506 {
1507     int r = 0;
1508     uint16_t func_code;
1509 
1510     /*
1511      * For any diagnose call we support, bits 48-63 of the resulting
1512      * address specify the function code; the remainder is ignored.
1513      */
1514     func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1515     switch (func_code) {
1516     case DIAG_TIMEREVENT:
1517         kvm_handle_diag_288(cpu, run);
1518         break;
1519     case DIAG_IPL:
1520         kvm_handle_diag_308(cpu, run);
1521         break;
1522     case DIAG_KVM_HYPERCALL:
1523         r = handle_hypercall(cpu, run);
1524         break;
1525     case DIAG_KVM_BREAKPOINT:
1526         r = handle_sw_breakpoint(cpu, run);
1527         break;
1528     default:
1529         DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1530         kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION);
1531         break;
1532     }
1533 
1534     return r;
1535 }
1536 
1537 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb)
1538 {
1539     CPUS390XState *env = &cpu->env;
1540     const uint8_t r1 = ipa1 >> 4;
1541     const uint8_t r3 = ipa1 & 0x0f;
1542     int ret;
1543     uint8_t order;
1544 
1545     /* get order code */
1546     order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK;
1547 
1548     ret = handle_sigp(env, order, r1, r3);
1549     setcc(cpu, ret);
1550     return 0;
1551 }
1552 
1553 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1554 {
1555     unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1556     uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1557     int r = -1;
1558 
1559     DPRINTF("handle_instruction 0x%x 0x%x\n",
1560             run->s390_sieic.ipa, run->s390_sieic.ipb);
1561     switch (ipa0) {
1562     case IPA0_B2:
1563         r = handle_b2(cpu, run, ipa1);
1564         break;
1565     case IPA0_B9:
1566         r = handle_b9(cpu, run, ipa1);
1567         break;
1568     case IPA0_EB:
1569         r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1570         break;
1571     case IPA0_E3:
1572         r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1573         break;
1574     case IPA0_DIAG:
1575         r = handle_diag(cpu, run, run->s390_sieic.ipb);
1576         break;
1577     case IPA0_SIGP:
1578         r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb);
1579         break;
1580     }
1581 
1582     if (r < 0) {
1583         r = 0;
1584         kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1585     }
1586 
1587     return r;
1588 }
1589 
1590 static void unmanageable_intercept(S390CPU *cpu, S390CrashReason reason,
1591                                    int pswoffset)
1592 {
1593     CPUState *cs = CPU(cpu);
1594 
1595     s390_cpu_halt(cpu);
1596     cpu->env.crash_reason = reason;
1597     qemu_system_guest_panicked(cpu_get_crash_info(cs));
1598 }
1599 
1600 /* try to detect pgm check loops */
1601 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run)
1602 {
1603     CPUState *cs = CPU(cpu);
1604     PSW oldpsw, newpsw;
1605 
1606     newpsw.mask = ldq_phys(cs->as, cpu->env.psa +
1607                            offsetof(LowCore, program_new_psw));
1608     newpsw.addr = ldq_phys(cs->as, cpu->env.psa +
1609                            offsetof(LowCore, program_new_psw) + 8);
1610     oldpsw.mask  = run->psw_mask;
1611     oldpsw.addr  = run->psw_addr;
1612     /*
1613      * Avoid endless loops of operation exceptions, if the pgm new
1614      * PSW will cause a new operation exception.
1615      * The heuristic checks if the pgm new psw is within 6 bytes before
1616      * the faulting psw address (with same DAT, AS settings) and the
1617      * new psw is not a wait psw and the fault was not triggered by
1618      * problem state. In that case go into crashed state.
1619      */
1620 
1621     if (oldpsw.addr - newpsw.addr <= 6 &&
1622         !(newpsw.mask & PSW_MASK_WAIT) &&
1623         !(oldpsw.mask & PSW_MASK_PSTATE) &&
1624         (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
1625         (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) {
1626         unmanageable_intercept(cpu, S390_CRASH_REASON_OPINT_LOOP,
1627                                offsetof(LowCore, program_new_psw));
1628         return EXCP_HALTED;
1629     }
1630     return 0;
1631 }
1632 
1633 static int handle_intercept(S390CPU *cpu)
1634 {
1635     CPUState *cs = CPU(cpu);
1636     struct kvm_run *run = cs->kvm_run;
1637     int icpt_code = run->s390_sieic.icptcode;
1638     int r = 0;
1639 
1640     DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1641             (long)cs->kvm_run->psw_addr);
1642     switch (icpt_code) {
1643         case ICPT_INSTRUCTION:
1644             r = handle_instruction(cpu, run);
1645             break;
1646         case ICPT_PROGRAM:
1647             unmanageable_intercept(cpu, S390_CRASH_REASON_PGMINT_LOOP,
1648                                    offsetof(LowCore, program_new_psw));
1649             r = EXCP_HALTED;
1650             break;
1651         case ICPT_EXT_INT:
1652             unmanageable_intercept(cpu, S390_CRASH_REASON_EXTINT_LOOP,
1653                                    offsetof(LowCore, external_new_psw));
1654             r = EXCP_HALTED;
1655             break;
1656         case ICPT_WAITPSW:
1657             /* disabled wait, since enabled wait is handled in kernel */
1658             s390_handle_wait(cpu);
1659             r = EXCP_HALTED;
1660             break;
1661         case ICPT_CPU_STOP:
1662             do_stop_interrupt(&cpu->env);
1663             r = EXCP_HALTED;
1664             break;
1665         case ICPT_OPEREXC:
1666             /* check for break points */
1667             r = handle_sw_breakpoint(cpu, run);
1668             if (r == -ENOENT) {
1669                 /* Then check for potential pgm check loops */
1670                 r = handle_oper_loop(cpu, run);
1671                 if (r == 0) {
1672                     kvm_s390_program_interrupt(cpu, PGM_OPERATION);
1673                 }
1674             }
1675             break;
1676         case ICPT_SOFT_INTERCEPT:
1677             fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1678             exit(1);
1679             break;
1680         case ICPT_IO:
1681             fprintf(stderr, "KVM unimplemented icpt IO\n");
1682             exit(1);
1683             break;
1684         default:
1685             fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1686             exit(1);
1687             break;
1688     }
1689 
1690     return r;
1691 }
1692 
1693 static int handle_tsch(S390CPU *cpu)
1694 {
1695     CPUState *cs = CPU(cpu);
1696     struct kvm_run *run = cs->kvm_run;
1697     int ret;
1698 
1699     ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb,
1700                              RA_IGNORED);
1701     if (ret < 0) {
1702         /*
1703          * Failure.
1704          * If an I/O interrupt had been dequeued, we have to reinject it.
1705          */
1706         if (run->s390_tsch.dequeued) {
1707             s390_io_interrupt(run->s390_tsch.subchannel_id,
1708                               run->s390_tsch.subchannel_nr,
1709                               run->s390_tsch.io_int_parm,
1710                               run->s390_tsch.io_int_word);
1711         }
1712         ret = 0;
1713     }
1714     return ret;
1715 }
1716 
1717 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1718 {
1719     SysIB_322 sysib;
1720     int del;
1721 
1722     if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1723         return;
1724     }
1725     /* Shift the stack of Extended Names to prepare for our own data */
1726     memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1727             sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1728     /* First virt level, that doesn't provide Ext Names delimits stack. It is
1729      * assumed it's not capable of managing Extended Names for lower levels.
1730      */
1731     for (del = 1; del < sysib.count; del++) {
1732         if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1733             break;
1734         }
1735     }
1736     if (del < sysib.count) {
1737         memset(sysib.ext_names[del], 0,
1738                sizeof(sysib.ext_names[0]) * (sysib.count - del));
1739     }
1740     /* Insert short machine name in EBCDIC, padded with blanks */
1741     if (qemu_name) {
1742         memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1743         ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1744                                                     strlen(qemu_name)));
1745     }
1746     sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1747     memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1748     /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1749      * considered by s390 as not capable of providing any Extended Name.
1750      * Therefore if no name was specified on qemu invocation, we go with the
1751      * same "KVMguest" default, which KVM has filled into short name field.
1752      */
1753     if (qemu_name) {
1754         strncpy((char *)sysib.ext_names[0], qemu_name,
1755                 sizeof(sysib.ext_names[0]));
1756     } else {
1757         strcpy((char *)sysib.ext_names[0], "KVMguest");
1758     }
1759     /* Insert UUID */
1760     memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid));
1761 
1762     s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1763 }
1764 
1765 static int handle_stsi(S390CPU *cpu)
1766 {
1767     CPUState *cs = CPU(cpu);
1768     struct kvm_run *run = cs->kvm_run;
1769 
1770     switch (run->s390_stsi.fc) {
1771     case 3:
1772         if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1773             return 0;
1774         }
1775         /* Only sysib 3.2.2 needs post-handling for now. */
1776         insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1777         return 0;
1778     default:
1779         return 0;
1780     }
1781 }
1782 
1783 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1784 {
1785     CPUState *cs = CPU(cpu);
1786     struct kvm_run *run = cs->kvm_run;
1787 
1788     int ret = 0;
1789     struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1790 
1791     switch (arch_info->type) {
1792     case KVM_HW_WP_WRITE:
1793         if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1794             cs->watchpoint_hit = &hw_watchpoint;
1795             hw_watchpoint.vaddr = arch_info->addr;
1796             hw_watchpoint.flags = BP_MEM_WRITE;
1797             ret = EXCP_DEBUG;
1798         }
1799         break;
1800     case KVM_HW_BP:
1801         if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1802             ret = EXCP_DEBUG;
1803         }
1804         break;
1805     case KVM_SINGLESTEP:
1806         if (cs->singlestep_enabled) {
1807             ret = EXCP_DEBUG;
1808         }
1809         break;
1810     default:
1811         ret = -ENOSYS;
1812     }
1813 
1814     return ret;
1815 }
1816 
1817 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1818 {
1819     S390CPU *cpu = S390_CPU(cs);
1820     int ret = 0;
1821 
1822     qemu_mutex_lock_iothread();
1823 
1824     kvm_cpu_synchronize_state(cs);
1825 
1826     switch (run->exit_reason) {
1827         case KVM_EXIT_S390_SIEIC:
1828             ret = handle_intercept(cpu);
1829             break;
1830         case KVM_EXIT_S390_RESET:
1831             s390_ipl_reset_request(cs, S390_RESET_REIPL);
1832             break;
1833         case KVM_EXIT_S390_TSCH:
1834             ret = handle_tsch(cpu);
1835             break;
1836         case KVM_EXIT_S390_STSI:
1837             ret = handle_stsi(cpu);
1838             break;
1839         case KVM_EXIT_DEBUG:
1840             ret = kvm_arch_handle_debug_exit(cpu);
1841             break;
1842         default:
1843             fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1844             break;
1845     }
1846     qemu_mutex_unlock_iothread();
1847 
1848     if (ret == 0) {
1849         ret = EXCP_INTERRUPT;
1850     }
1851     return ret;
1852 }
1853 
1854 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1855 {
1856     return true;
1857 }
1858 
1859 void kvm_s390_enable_css_support(S390CPU *cpu)
1860 {
1861     int r;
1862 
1863     /* Activate host kernel channel subsystem support. */
1864     r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1865     assert(r == 0);
1866 }
1867 
1868 void kvm_arch_init_irq_routing(KVMState *s)
1869 {
1870     /*
1871      * Note that while irqchip capabilities generally imply that cpustates
1872      * are handled in-kernel, it is not true for s390 (yet); therefore, we
1873      * have to override the common code kvm_halt_in_kernel_allowed setting.
1874      */
1875     if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1876         kvm_gsi_routing_allowed = true;
1877         kvm_halt_in_kernel_allowed = false;
1878     }
1879 }
1880 
1881 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1882                                     int vq, bool assign)
1883 {
1884     struct kvm_ioeventfd kick = {
1885         .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1886         KVM_IOEVENTFD_FLAG_DATAMATCH,
1887         .fd = event_notifier_get_fd(notifier),
1888         .datamatch = vq,
1889         .addr = sch,
1890         .len = 8,
1891     };
1892     trace_kvm_assign_subch_ioeventfd(kick.fd, kick.addr, assign,
1893                                      kick.datamatch);
1894     if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1895         return -ENOSYS;
1896     }
1897     if (!assign) {
1898         kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1899     }
1900     return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1901 }
1902 
1903 int kvm_s390_get_ri(void)
1904 {
1905     return cap_ri;
1906 }
1907 
1908 int kvm_s390_get_gs(void)
1909 {
1910     return cap_gs;
1911 }
1912 
1913 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1914 {
1915     struct kvm_mp_state mp_state = {};
1916     int ret;
1917 
1918     /* the kvm part might not have been initialized yet */
1919     if (CPU(cpu)->kvm_state == NULL) {
1920         return 0;
1921     }
1922 
1923     switch (cpu_state) {
1924     case S390_CPU_STATE_STOPPED:
1925         mp_state.mp_state = KVM_MP_STATE_STOPPED;
1926         break;
1927     case S390_CPU_STATE_CHECK_STOP:
1928         mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1929         break;
1930     case S390_CPU_STATE_OPERATING:
1931         mp_state.mp_state = KVM_MP_STATE_OPERATING;
1932         break;
1933     case S390_CPU_STATE_LOAD:
1934         mp_state.mp_state = KVM_MP_STATE_LOAD;
1935         break;
1936     default:
1937         error_report("Requested CPU state is not a valid S390 CPU state: %u",
1938                      cpu_state);
1939         exit(1);
1940     }
1941 
1942     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1943     if (ret) {
1944         trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1945                                        strerror(-ret));
1946     }
1947 
1948     return ret;
1949 }
1950 
1951 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
1952 {
1953     unsigned int max_cpus = MACHINE(qdev_get_machine())->smp.max_cpus;
1954     struct kvm_s390_irq_state irq_state = {
1955         .buf = (uint64_t) cpu->irqstate,
1956         .len = VCPU_IRQ_BUF_SIZE(max_cpus),
1957     };
1958     CPUState *cs = CPU(cpu);
1959     int32_t bytes;
1960 
1961     if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1962         return;
1963     }
1964 
1965     bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
1966     if (bytes < 0) {
1967         cpu->irqstate_saved_size = 0;
1968         error_report("Migration of interrupt state failed");
1969         return;
1970     }
1971 
1972     cpu->irqstate_saved_size = bytes;
1973 }
1974 
1975 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
1976 {
1977     CPUState *cs = CPU(cpu);
1978     struct kvm_s390_irq_state irq_state = {
1979         .buf = (uint64_t) cpu->irqstate,
1980         .len = cpu->irqstate_saved_size,
1981     };
1982     int r;
1983 
1984     if (cpu->irqstate_saved_size == 0) {
1985         return 0;
1986     }
1987 
1988     if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
1989         return -ENOSYS;
1990     }
1991 
1992     r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
1993     if (r) {
1994         error_report("Setting interrupt state failed %d", r);
1995     }
1996     return r;
1997 }
1998 
1999 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2000                              uint64_t address, uint32_t data, PCIDevice *dev)
2001 {
2002     S390PCIBusDevice *pbdev;
2003     uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2004 
2005     if (!dev) {
2006         DPRINTF("add_msi_route no pci device\n");
2007         return -ENODEV;
2008     }
2009 
2010     pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id);
2011     if (!pbdev) {
2012         DPRINTF("add_msi_route no zpci device\n");
2013         return -ENODEV;
2014     }
2015 
2016     route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2017     route->flags = 0;
2018     route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2019     route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2020     route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2021     route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec;
2022     route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2023     return 0;
2024 }
2025 
2026 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2027                                 int vector, PCIDevice *dev)
2028 {
2029     return 0;
2030 }
2031 
2032 int kvm_arch_release_virq_post(int virq)
2033 {
2034     return 0;
2035 }
2036 
2037 int kvm_arch_msi_data_to_gsi(uint32_t data)
2038 {
2039     abort();
2040 }
2041 
2042 static int query_cpu_subfunc(S390FeatBitmap features)
2043 {
2044     struct kvm_s390_vm_cpu_subfunc prop;
2045     struct kvm_device_attr attr = {
2046         .group = KVM_S390_VM_CPU_MODEL,
2047         .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC,
2048         .addr = (uint64_t) &prop,
2049     };
2050     int rc;
2051 
2052     rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2053     if (rc) {
2054         return  rc;
2055     }
2056 
2057     /*
2058      * We're going to add all subfunctions now, if the corresponding feature
2059      * is available that unlocks the query functions.
2060      */
2061     s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2062     if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2063         s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2064     }
2065     if (test_bit(S390_FEAT_MSA, features)) {
2066         s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2067         s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2068         s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2069         s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2070         s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2071     }
2072     if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2073         s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2074     }
2075     if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2076         s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2077         s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2078         s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2079         s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2080     }
2081     if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2082         s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2083     }
2084     if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2085         s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2086     }
2087     if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2088         s390_add_from_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2089     }
2090     if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2091         s390_add_from_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2092     }
2093     if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2094         s390_add_from_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2095     }
2096     return 0;
2097 }
2098 
2099 static int configure_cpu_subfunc(const S390FeatBitmap features)
2100 {
2101     struct kvm_s390_vm_cpu_subfunc prop = {};
2102     struct kvm_device_attr attr = {
2103         .group = KVM_S390_VM_CPU_MODEL,
2104         .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC,
2105         .addr = (uint64_t) &prop,
2106     };
2107 
2108     if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2109                            KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) {
2110         /* hardware support might be missing, IBC will handle most of this */
2111         return 0;
2112     }
2113 
2114     s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo);
2115     if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) {
2116         s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff);
2117     }
2118     if (test_bit(S390_FEAT_MSA, features)) {
2119         s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac);
2120         s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc);
2121         s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km);
2122         s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd);
2123         s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd);
2124     }
2125     if (test_bit(S390_FEAT_MSA_EXT_3, features)) {
2126         s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo);
2127     }
2128     if (test_bit(S390_FEAT_MSA_EXT_4, features)) {
2129         s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr);
2130         s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf);
2131         s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo);
2132         s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc);
2133     }
2134     if (test_bit(S390_FEAT_MSA_EXT_5, features)) {
2135         s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno);
2136     }
2137     if (test_bit(S390_FEAT_MSA_EXT_8, features)) {
2138         s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma);
2139     }
2140     if (test_bit(S390_FEAT_MSA_EXT_9, features)) {
2141         s390_fill_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa);
2142     }
2143     if (test_bit(S390_FEAT_ESORT_BASE, features)) {
2144         s390_fill_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl);
2145     }
2146     if (test_bit(S390_FEAT_DEFLATE_BASE, features)) {
2147         s390_fill_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc);
2148     }
2149     return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2150 }
2151 
2152 static int kvm_to_feat[][2] = {
2153     { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP },
2154     { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 },
2155     { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO },
2156     { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF },
2157     { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE },
2158     { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS },
2159     { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB },
2160     { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI },
2161     { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS },
2162     { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY },
2163     { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA },
2164     { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI},
2165     { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF},
2166     { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS},
2167 };
2168 
2169 static int query_cpu_feat(S390FeatBitmap features)
2170 {
2171     struct kvm_s390_vm_cpu_feat prop;
2172     struct kvm_device_attr attr = {
2173         .group = KVM_S390_VM_CPU_MODEL,
2174         .attr = KVM_S390_VM_CPU_MACHINE_FEAT,
2175         .addr = (uint64_t) &prop,
2176     };
2177     int rc;
2178     int i;
2179 
2180     rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2181     if (rc) {
2182         return  rc;
2183     }
2184 
2185     for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2186         if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) {
2187             set_bit(kvm_to_feat[i][1], features);
2188         }
2189     }
2190     return 0;
2191 }
2192 
2193 static int configure_cpu_feat(const S390FeatBitmap features)
2194 {
2195     struct kvm_s390_vm_cpu_feat prop = {};
2196     struct kvm_device_attr attr = {
2197         .group = KVM_S390_VM_CPU_MODEL,
2198         .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT,
2199         .addr = (uint64_t) &prop,
2200     };
2201     int i;
2202 
2203     for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) {
2204         if (test_bit(kvm_to_feat[i][1], features)) {
2205             set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat);
2206         }
2207     }
2208     return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2209 }
2210 
2211 bool kvm_s390_cpu_models_supported(void)
2212 {
2213     if (!cpu_model_allowed()) {
2214         /* compatibility machines interfere with the cpu model */
2215         return false;
2216     }
2217     return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2218                              KVM_S390_VM_CPU_MACHINE) &&
2219            kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2220                              KVM_S390_VM_CPU_PROCESSOR) &&
2221            kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2222                              KVM_S390_VM_CPU_MACHINE_FEAT) &&
2223            kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2224                              KVM_S390_VM_CPU_PROCESSOR_FEAT) &&
2225            kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL,
2226                              KVM_S390_VM_CPU_MACHINE_SUBFUNC);
2227 }
2228 
2229 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)
2230 {
2231     struct kvm_s390_vm_cpu_machine prop = {};
2232     struct kvm_device_attr attr = {
2233         .group = KVM_S390_VM_CPU_MODEL,
2234         .attr = KVM_S390_VM_CPU_MACHINE,
2235         .addr = (uint64_t) &prop,
2236     };
2237     uint16_t unblocked_ibc = 0, cpu_type = 0;
2238     int rc;
2239 
2240     memset(model, 0, sizeof(*model));
2241 
2242     if (!kvm_s390_cpu_models_supported()) {
2243         error_setg(errp, "KVM doesn't support CPU models");
2244         return;
2245     }
2246 
2247     /* query the basic cpu model properties */
2248     rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
2249     if (rc) {
2250         error_setg(errp, "KVM: Error querying host CPU model: %d", rc);
2251         return;
2252     }
2253 
2254     cpu_type = cpuid_type(prop.cpuid);
2255     if (has_ibc(prop.ibc)) {
2256         model->lowest_ibc = lowest_ibc(prop.ibc);
2257         unblocked_ibc = unblocked_ibc(prop.ibc);
2258     }
2259     model->cpu_id = cpuid_id(prop.cpuid);
2260     model->cpu_id_format = cpuid_format(prop.cpuid);
2261     model->cpu_ver = 0xff;
2262 
2263     /* get supported cpu features indicated via STFL(E) */
2264     s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL,
2265                              (uint8_t *) prop.fac_mask);
2266     /* dat-enhancement facility 2 has no bit but was introduced with stfle */
2267     if (test_bit(S390_FEAT_STFLE, model->features)) {
2268         set_bit(S390_FEAT_DAT_ENH_2, model->features);
2269     }
2270     /* get supported cpu features indicated e.g. via SCLP */
2271     rc = query_cpu_feat(model->features);
2272     if (rc) {
2273         error_setg(errp, "KVM: Error querying CPU features: %d", rc);
2274         return;
2275     }
2276     /* get supported cpu subfunctions indicated via query / test bit */
2277     rc = query_cpu_subfunc(model->features);
2278     if (rc) {
2279         error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc);
2280         return;
2281     }
2282 
2283     /* PTFF subfunctions might be indicated although kernel support missing */
2284     if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) {
2285         clear_bit(S390_FEAT_PTFF_QSIE, model->features);
2286         clear_bit(S390_FEAT_PTFF_QTOUE, model->features);
2287         clear_bit(S390_FEAT_PTFF_STOE, model->features);
2288         clear_bit(S390_FEAT_PTFF_STOUE, model->features);
2289     }
2290 
2291     /* with cpu model support, CMM is only indicated if really available */
2292     if (kvm_s390_cmma_available()) {
2293         set_bit(S390_FEAT_CMM, model->features);
2294     } else {
2295         /* no cmm -> no cmm nt */
2296         clear_bit(S390_FEAT_CMM_NT, model->features);
2297     }
2298 
2299     /* bpb needs kernel support for migration, VSIE and reset */
2300     if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) {
2301         clear_bit(S390_FEAT_BPB, model->features);
2302     }
2303 
2304     /* We emulate a zPCI bus and AEN, therefore we don't need HW support */
2305     set_bit(S390_FEAT_ZPCI, model->features);
2306     set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features);
2307 
2308     if (s390_known_cpu_type(cpu_type)) {
2309         /* we want the exact model, even if some features are missing */
2310         model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc),
2311                                        ibc_ec_ga(unblocked_ibc), NULL);
2312     } else {
2313         /* model unknown, e.g. too new - search using features */
2314         model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc),
2315                                        ibc_ec_ga(unblocked_ibc),
2316                                        model->features);
2317     }
2318     if (!model->def) {
2319         error_setg(errp, "KVM: host CPU model could not be identified");
2320         return;
2321     }
2322     /* for now, we can only provide the AP feature with HW support */
2323     if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO,
2324         KVM_S390_VM_CRYPTO_ENABLE_APIE)) {
2325         set_bit(S390_FEAT_AP, model->features);
2326     }
2327     /* strip of features that are not part of the maximum model */
2328     bitmap_and(model->features, model->features, model->def->full_feat,
2329                S390_FEAT_MAX);
2330 }
2331 
2332 static void kvm_s390_configure_apie(bool interpret)
2333 {
2334     uint64_t attr = interpret ? KVM_S390_VM_CRYPTO_ENABLE_APIE :
2335                                 KVM_S390_VM_CRYPTO_DISABLE_APIE;
2336 
2337     if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
2338         kvm_s390_set_attr(attr);
2339     }
2340 }
2341 
2342 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp)
2343 {
2344     struct kvm_s390_vm_cpu_processor prop  = {
2345         .fac_list = { 0 },
2346     };
2347     struct kvm_device_attr attr = {
2348         .group = KVM_S390_VM_CPU_MODEL,
2349         .attr = KVM_S390_VM_CPU_PROCESSOR,
2350         .addr = (uint64_t) &prop,
2351     };
2352     int rc;
2353 
2354     if (!model) {
2355         /* compatibility handling if cpu models are disabled */
2356         if (kvm_s390_cmma_available()) {
2357             kvm_s390_enable_cmma();
2358         }
2359         return;
2360     }
2361     if (!kvm_s390_cpu_models_supported()) {
2362         error_setg(errp, "KVM doesn't support CPU models");
2363         return;
2364     }
2365     prop.cpuid = s390_cpuid_from_cpu_model(model);
2366     prop.ibc = s390_ibc_from_cpu_model(model);
2367     /* configure cpu features indicated via STFL(e) */
2368     s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL,
2369                          (uint8_t *) prop.fac_list);
2370     rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
2371     if (rc) {
2372         error_setg(errp, "KVM: Error configuring the CPU model: %d", rc);
2373         return;
2374     }
2375     /* configure cpu features indicated e.g. via SCLP */
2376     rc = configure_cpu_feat(model->features);
2377     if (rc) {
2378         error_setg(errp, "KVM: Error configuring CPU features: %d", rc);
2379         return;
2380     }
2381     /* configure cpu subfunctions indicated via query / test bit */
2382     rc = configure_cpu_subfunc(model->features);
2383     if (rc) {
2384         error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc);
2385         return;
2386     }
2387     /* enable CMM via CMMA */
2388     if (test_bit(S390_FEAT_CMM, model->features)) {
2389         kvm_s390_enable_cmma();
2390     }
2391 
2392     if (test_bit(S390_FEAT_AP, model->features)) {
2393         kvm_s390_configure_apie(true);
2394     }
2395 }
2396 
2397 void kvm_s390_restart_interrupt(S390CPU *cpu)
2398 {
2399     struct kvm_s390_irq irq = {
2400         .type = KVM_S390_RESTART,
2401     };
2402 
2403     kvm_s390_vcpu_interrupt(cpu, &irq);
2404 }
2405 
2406 void kvm_s390_stop_interrupt(S390CPU *cpu)
2407 {
2408     struct kvm_s390_irq irq = {
2409         .type = KVM_S390_SIGP_STOP,
2410     };
2411 
2412     kvm_s390_vcpu_interrupt(cpu, &irq);
2413 }
2414