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