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