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