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