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