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