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