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