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