]> Git Repo - qemu.git/blob - target-s390x/kvm.c
Merge remote-tracking branch 'remotes/mst/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 <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/mman.h>
27
28 #include <linux/kvm.h>
29 #include <asm/ptrace.h>
30
31 #include "qemu-common.h"
32 #include "qemu/timer.h"
33 #include "sysemu/sysemu.h"
34 #include "sysemu/kvm.h"
35 #include "hw/hw.h"
36 #include "cpu.h"
37 #include "sysemu/device_tree.h"
38 #include "qapi/qmp/qjson.h"
39 #include "monitor/monitor.h"
40 #include "exec/gdbstub.h"
41 #include "exec/address-spaces.h"
42 #include "trace.h"
43 #include "qapi-event.h"
44 #include "hw/s390x/s390-pci-inst.h"
45 #include "hw/s390x/s390-pci-bus.h"
46 #include "hw/s390x/ipl.h"
47 #include "hw/s390x/ebcdic.h"
48 #include "exec/memattrs.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_IPL                        0x308
102 #define DIAG_KVM_HYPERCALL              0x500
103 #define DIAG_KVM_BREAKPOINT             0x501
104
105 #define ICPT_INSTRUCTION                0x04
106 #define ICPT_PROGRAM                    0x08
107 #define ICPT_EXT_INT                    0x14
108 #define ICPT_WAITPSW                    0x1c
109 #define ICPT_SOFT_INTERCEPT             0x24
110 #define ICPT_CPU_STOP                   0x28
111 #define ICPT_IO                         0x40
112
113 #define NR_LOCAL_IRQS 32
114 /*
115  * Needs to be big enough to contain max_cpus emergency signals
116  * and in addition NR_LOCAL_IRQS interrupts
117  */
118 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \
119                            (max_cpus + NR_LOCAL_IRQS))
120
121 static CPUWatchpoint hw_watchpoint;
122 /*
123  * We don't use a list because this structure is also used to transmit the
124  * hardware breakpoints to the kernel.
125  */
126 static struct kvm_hw_breakpoint *hw_breakpoints;
127 static int nb_hw_breakpoints;
128
129 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
130     KVM_CAP_LAST_INFO
131 };
132
133 static int cap_sync_regs;
134 static int cap_async_pf;
135 static int cap_mem_op;
136 static int cap_s390_irq;
137
138 static void *legacy_s390_alloc(size_t size, uint64_t *align);
139
140 static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
141 {
142     struct kvm_device_attr attr = {
143         .group = KVM_S390_VM_MEM_CTRL,
144         .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
145         .addr = (uint64_t) memory_limit,
146     };
147
148     return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
149 }
150
151 int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
152 {
153     int rc;
154
155     struct kvm_device_attr attr = {
156         .group = KVM_S390_VM_MEM_CTRL,
157         .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
158         .addr = (uint64_t) &new_limit,
159     };
160
161     if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_LIMIT_SIZE)) {
162         return 0;
163     }
164
165     rc = kvm_s390_query_mem_limit(s, hw_limit);
166     if (rc) {
167         return rc;
168     } else if (*hw_limit < new_limit) {
169         return -E2BIG;
170     }
171
172     return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
173 }
174
175 void kvm_s390_clear_cmma_callback(void *opaque)
176 {
177     int rc;
178     KVMState *s = opaque;
179     struct kvm_device_attr attr = {
180         .group = KVM_S390_VM_MEM_CTRL,
181         .attr = KVM_S390_VM_MEM_CLR_CMMA,
182     };
183
184     rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
185     trace_kvm_clear_cmma(rc);
186 }
187
188 static void kvm_s390_enable_cmma(KVMState *s)
189 {
190     int rc;
191     struct kvm_device_attr attr = {
192         .group = KVM_S390_VM_MEM_CTRL,
193         .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
194     };
195
196     if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_ENABLE_CMMA) ||
197         !kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_CLR_CMMA)) {
198         return;
199     }
200
201     rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
202     if (!rc) {
203         qemu_register_reset(kvm_s390_clear_cmma_callback, s);
204     }
205     trace_kvm_enable_cmma(rc);
206 }
207
208 static void kvm_s390_set_attr(uint64_t attr)
209 {
210     struct kvm_device_attr attribute = {
211         .group = KVM_S390_VM_CRYPTO,
212         .attr  = attr,
213     };
214
215     int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
216
217     if (ret) {
218         error_report("Failed to set crypto device attribute %lu: %s",
219                      attr, strerror(-ret));
220     }
221 }
222
223 static void kvm_s390_init_aes_kw(void)
224 {
225     uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
226
227     if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
228                                  NULL)) {
229             attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
230     }
231
232     if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
233             kvm_s390_set_attr(attr);
234     }
235 }
236
237 static void kvm_s390_init_dea_kw(void)
238 {
239     uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
240
241     if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
242                                  NULL)) {
243             attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
244     }
245
246     if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
247             kvm_s390_set_attr(attr);
248     }
249 }
250
251 static void kvm_s390_init_crypto(void)
252 {
253     kvm_s390_init_aes_kw();
254     kvm_s390_init_dea_kw();
255 }
256
257 int kvm_arch_init(MachineState *ms, KVMState *s)
258 {
259     cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
260     cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
261     cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP);
262     cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ);
263
264     kvm_s390_enable_cmma(s);
265
266     if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
267         || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
268         phys_mem_set_alloc(legacy_s390_alloc);
269     }
270
271     kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
272     kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0);
273     kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
274
275     return 0;
276 }
277
278 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
279 {
280     return cpu->cpu_index;
281 }
282
283 int kvm_arch_init_vcpu(CPUState *cs)
284 {
285     S390CPU *cpu = S390_CPU(cs);
286     kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
287     cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE);
288     return 0;
289 }
290
291 void kvm_s390_reset_vcpu(S390CPU *cpu)
292 {
293     CPUState *cs = CPU(cpu);
294
295     /* The initial reset call is needed here to reset in-kernel
296      * vcpu data that we can't access directly from QEMU
297      * (i.e. with older kernels which don't support sync_regs/ONE_REG).
298      * Before this ioctl cpu_synchronize_state() is called in common kvm
299      * code (kvm-all) */
300     if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
301         error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
302     }
303
304     kvm_s390_init_crypto();
305 }
306
307 static int can_sync_regs(CPUState *cs, int regs)
308 {
309     return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
310 }
311
312 int kvm_arch_put_registers(CPUState *cs, int level)
313 {
314     S390CPU *cpu = S390_CPU(cs);
315     CPUS390XState *env = &cpu->env;
316     struct kvm_sregs sregs;
317     struct kvm_regs regs;
318     struct kvm_fpu fpu = {};
319     int r;
320     int i;
321
322     /* always save the PSW  and the GPRS*/
323     cs->kvm_run->psw_addr = env->psw.addr;
324     cs->kvm_run->psw_mask = env->psw.mask;
325
326     if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
327         for (i = 0; i < 16; i++) {
328             cs->kvm_run->s.regs.gprs[i] = env->regs[i];
329             cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
330         }
331     } else {
332         for (i = 0; i < 16; i++) {
333             regs.gprs[i] = env->regs[i];
334         }
335         r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
336         if (r < 0) {
337             return r;
338         }
339     }
340
341     if (can_sync_regs(cs, KVM_SYNC_VRS)) {
342         for (i = 0; i < 32; i++) {
343             cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll;
344             cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll;
345         }
346         cs->kvm_run->s.regs.fpc = env->fpc;
347         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS;
348     } else {
349         /* Floating point */
350         for (i = 0; i < 16; i++) {
351             fpu.fprs[i] = get_freg(env, i)->ll;
352         }
353         fpu.fpc = env->fpc;
354
355         r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
356         if (r < 0) {
357             return r;
358         }
359     }
360
361     /* Do we need to save more than that? */
362     if (level == KVM_PUT_RUNTIME_STATE) {
363         return 0;
364     }
365
366     if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
367         cs->kvm_run->s.regs.cputm = env->cputm;
368         cs->kvm_run->s.regs.ckc = env->ckc;
369         cs->kvm_run->s.regs.todpr = env->todpr;
370         cs->kvm_run->s.regs.gbea = env->gbea;
371         cs->kvm_run->s.regs.pp = env->pp;
372         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
373     } else {
374         /*
375          * These ONE_REGS are not protected by a capability. As they are only
376          * necessary for migration we just trace a possible error, but don't
377          * return with an error return code.
378          */
379         kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
380         kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
381         kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
382         kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
383         kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
384     }
385
386     /* pfault parameters */
387     if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
388         cs->kvm_run->s.regs.pft = env->pfault_token;
389         cs->kvm_run->s.regs.pfs = env->pfault_select;
390         cs->kvm_run->s.regs.pfc = env->pfault_compare;
391         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
392     } else if (cap_async_pf) {
393         r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
394         if (r < 0) {
395             return r;
396         }
397         r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
398         if (r < 0) {
399             return r;
400         }
401         r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
402         if (r < 0) {
403             return r;
404         }
405     }
406
407     /* access registers and control registers*/
408     if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
409         for (i = 0; i < 16; i++) {
410             cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
411             cs->kvm_run->s.regs.crs[i] = env->cregs[i];
412         }
413         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
414         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
415     } else {
416         for (i = 0; i < 16; i++) {
417             sregs.acrs[i] = env->aregs[i];
418             sregs.crs[i] = env->cregs[i];
419         }
420         r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
421         if (r < 0) {
422             return r;
423         }
424     }
425
426     /* Finally the prefix */
427     if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
428         cs->kvm_run->s.regs.prefix = env->psa;
429         cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
430     } else {
431         /* prefix is only supported via sync regs */
432     }
433     return 0;
434 }
435
436 int kvm_arch_get_registers(CPUState *cs)
437 {
438     S390CPU *cpu = S390_CPU(cs);
439     CPUS390XState *env = &cpu->env;
440     struct kvm_sregs sregs;
441     struct kvm_regs regs;
442     struct kvm_fpu fpu;
443     int i, r;
444
445     /* get the PSW */
446     env->psw.addr = cs->kvm_run->psw_addr;
447     env->psw.mask = cs->kvm_run->psw_mask;
448
449     /* the GPRS */
450     if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
451         for (i = 0; i < 16; i++) {
452             env->regs[i] = cs->kvm_run->s.regs.gprs[i];
453         }
454     } else {
455         r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
456         if (r < 0) {
457             return r;
458         }
459          for (i = 0; i < 16; i++) {
460             env->regs[i] = regs.gprs[i];
461         }
462     }
463
464     /* The ACRS and CRS */
465     if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
466         for (i = 0; i < 16; i++) {
467             env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
468             env->cregs[i] = cs->kvm_run->s.regs.crs[i];
469         }
470     } else {
471         r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
472         if (r < 0) {
473             return r;
474         }
475          for (i = 0; i < 16; i++) {
476             env->aregs[i] = sregs.acrs[i];
477             env->cregs[i] = sregs.crs[i];
478         }
479     }
480
481     /* Floating point and vector registers */
482     if (can_sync_regs(cs, KVM_SYNC_VRS)) {
483         for (i = 0; i < 32; i++) {
484             env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0];
485             env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1];
486         }
487         env->fpc = cs->kvm_run->s.regs.fpc;
488     } else {
489         r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
490         if (r < 0) {
491             return r;
492         }
493         for (i = 0; i < 16; i++) {
494             get_freg(env, i)->ll = fpu.fprs[i];
495         }
496         env->fpc = fpu.fpc;
497     }
498
499     /* The prefix */
500     if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
501         env->psa = cs->kvm_run->s.regs.prefix;
502     }
503
504     if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
505         env->cputm = cs->kvm_run->s.regs.cputm;
506         env->ckc = cs->kvm_run->s.regs.ckc;
507         env->todpr = cs->kvm_run->s.regs.todpr;
508         env->gbea = cs->kvm_run->s.regs.gbea;
509         env->pp = cs->kvm_run->s.regs.pp;
510     } else {
511         /*
512          * These ONE_REGS are not protected by a capability. As they are only
513          * necessary for migration we just trace a possible error, but don't
514          * return with an error return code.
515          */
516         kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
517         kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
518         kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
519         kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
520         kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
521     }
522
523     /* pfault parameters */
524     if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
525         env->pfault_token = cs->kvm_run->s.regs.pft;
526         env->pfault_select = cs->kvm_run->s.regs.pfs;
527         env->pfault_compare = cs->kvm_run->s.regs.pfc;
528     } else if (cap_async_pf) {
529         r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
530         if (r < 0) {
531             return r;
532         }
533         r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
534         if (r < 0) {
535             return r;
536         }
537         r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
538         if (r < 0) {
539             return r;
540         }
541     }
542
543     return 0;
544 }
545
546 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
547 {
548     int r;
549     struct kvm_device_attr attr = {
550         .group = KVM_S390_VM_TOD,
551         .attr = KVM_S390_VM_TOD_LOW,
552         .addr = (uint64_t)tod_low,
553     };
554
555     r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
556     if (r) {
557         return r;
558     }
559
560     attr.attr = KVM_S390_VM_TOD_HIGH;
561     attr.addr = (uint64_t)tod_high;
562     return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
563 }
564
565 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
566 {
567     int r;
568
569     struct kvm_device_attr attr = {
570         .group = KVM_S390_VM_TOD,
571         .attr = KVM_S390_VM_TOD_LOW,
572         .addr = (uint64_t)tod_low,
573     };
574
575     r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
576     if (r) {
577         return r;
578     }
579
580     attr.attr = KVM_S390_VM_TOD_HIGH;
581     attr.addr = (uint64_t)tod_high;
582     return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
583 }
584
585 /**
586  * kvm_s390_mem_op:
587  * @addr:      the logical start address in guest memory
588  * @ar:        the access register number
589  * @hostbuf:   buffer in host memory. NULL = do only checks w/o copying
590  * @len:       length that should be transfered
591  * @is_write:  true = write, false = read
592  * Returns:    0 on success, non-zero if an exception or error occured
593  *
594  * Use KVM ioctl to read/write from/to guest memory. An access exception
595  * is injected into the vCPU in case of translation errors.
596  */
597 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf,
598                     int len, bool is_write)
599 {
600     struct kvm_s390_mem_op mem_op = {
601         .gaddr = addr,
602         .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION,
603         .size = len,
604         .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE
605                        : KVM_S390_MEMOP_LOGICAL_READ,
606         .buf = (uint64_t)hostbuf,
607         .ar = ar,
608     };
609     int ret;
610
611     if (!cap_mem_op) {
612         return -ENOSYS;
613     }
614     if (!hostbuf) {
615         mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY;
616     }
617
618     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op);
619     if (ret < 0) {
620         error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret));
621     }
622     return ret;
623 }
624
625 /*
626  * Legacy layout for s390:
627  * Older S390 KVM requires the topmost vma of the RAM to be
628  * smaller than an system defined value, which is at least 256GB.
629  * Larger systems have larger values. We put the guest between
630  * the end of data segment (system break) and this value. We
631  * use 32GB as a base to have enough room for the system break
632  * to grow. We also have to use MAP parameters that avoid
633  * read-only mapping of guest pages.
634  */
635 static void *legacy_s390_alloc(size_t size, uint64_t *align)
636 {
637     void *mem;
638
639     mem = mmap((void *) 0x800000000ULL, size,
640                PROT_EXEC|PROT_READ|PROT_WRITE,
641                MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
642     return mem == MAP_FAILED ? NULL : mem;
643 }
644
645 /* DIAG 501 is used for sw breakpoints */
646 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
647
648 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
649 {
650
651     if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
652                             sizeof(diag_501), 0) ||
653         cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
654                             sizeof(diag_501), 1)) {
655         return -EINVAL;
656     }
657     return 0;
658 }
659
660 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
661 {
662     uint8_t t[sizeof(diag_501)];
663
664     if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
665         return -EINVAL;
666     } else if (memcmp(t, diag_501, sizeof(diag_501))) {
667         return -EINVAL;
668     } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
669                                    sizeof(diag_501), 1)) {
670         return -EINVAL;
671     }
672
673     return 0;
674 }
675
676 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
677                                                     int len, int type)
678 {
679     int n;
680
681     for (n = 0; n < nb_hw_breakpoints; n++) {
682         if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
683             (hw_breakpoints[n].len == len || len == -1)) {
684             return &hw_breakpoints[n];
685         }
686     }
687
688     return NULL;
689 }
690
691 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
692 {
693     int size;
694
695     if (find_hw_breakpoint(addr, len, type)) {
696         return -EEXIST;
697     }
698
699     size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
700
701     if (!hw_breakpoints) {
702         nb_hw_breakpoints = 0;
703         hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
704     } else {
705         hw_breakpoints =
706             (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
707     }
708
709     if (!hw_breakpoints) {
710         nb_hw_breakpoints = 0;
711         return -ENOMEM;
712     }
713
714     hw_breakpoints[nb_hw_breakpoints].addr = addr;
715     hw_breakpoints[nb_hw_breakpoints].len = len;
716     hw_breakpoints[nb_hw_breakpoints].type = type;
717
718     nb_hw_breakpoints++;
719
720     return 0;
721 }
722
723 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
724                                   target_ulong len, int type)
725 {
726     switch (type) {
727     case GDB_BREAKPOINT_HW:
728         type = KVM_HW_BP;
729         break;
730     case GDB_WATCHPOINT_WRITE:
731         if (len < 1) {
732             return -EINVAL;
733         }
734         type = KVM_HW_WP_WRITE;
735         break;
736     default:
737         return -ENOSYS;
738     }
739     return insert_hw_breakpoint(addr, len, type);
740 }
741
742 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
743                                   target_ulong len, int type)
744 {
745     int size;
746     struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
747
748     if (bp == NULL) {
749         return -ENOENT;
750     }
751
752     nb_hw_breakpoints--;
753     if (nb_hw_breakpoints > 0) {
754         /*
755          * In order to trim the array, move the last element to the position to
756          * be removed - if necessary.
757          */
758         if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
759             *bp = hw_breakpoints[nb_hw_breakpoints];
760         }
761         size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
762         hw_breakpoints =
763              (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
764     } else {
765         g_free(hw_breakpoints);
766         hw_breakpoints = NULL;
767     }
768
769     return 0;
770 }
771
772 void kvm_arch_remove_all_hw_breakpoints(void)
773 {
774     nb_hw_breakpoints = 0;
775     g_free(hw_breakpoints);
776     hw_breakpoints = NULL;
777 }
778
779 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
780 {
781     int i;
782
783     if (nb_hw_breakpoints > 0) {
784         dbg->arch.nr_hw_bp = nb_hw_breakpoints;
785         dbg->arch.hw_bp = hw_breakpoints;
786
787         for (i = 0; i < nb_hw_breakpoints; ++i) {
788             hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
789                                                        hw_breakpoints[i].addr);
790         }
791         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
792     } else {
793         dbg->arch.nr_hw_bp = 0;
794         dbg->arch.hw_bp = NULL;
795     }
796 }
797
798 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
799 {
800 }
801
802 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
803 {
804     return MEMTXATTRS_UNSPECIFIED;
805 }
806
807 int kvm_arch_process_async_events(CPUState *cs)
808 {
809     return cs->halted;
810 }
811
812 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
813                                      struct kvm_s390_interrupt *interrupt)
814 {
815     int r = 0;
816
817     interrupt->type = irq->type;
818     switch (irq->type) {
819     case KVM_S390_INT_VIRTIO:
820         interrupt->parm = irq->u.ext.ext_params;
821         /* fall through */
822     case KVM_S390_INT_PFAULT_INIT:
823     case KVM_S390_INT_PFAULT_DONE:
824         interrupt->parm64 = irq->u.ext.ext_params2;
825         break;
826     case KVM_S390_PROGRAM_INT:
827         interrupt->parm = irq->u.pgm.code;
828         break;
829     case KVM_S390_SIGP_SET_PREFIX:
830         interrupt->parm = irq->u.prefix.address;
831         break;
832     case KVM_S390_INT_SERVICE:
833         interrupt->parm = irq->u.ext.ext_params;
834         break;
835     case KVM_S390_MCHK:
836         interrupt->parm = irq->u.mchk.cr14;
837         interrupt->parm64 = irq->u.mchk.mcic;
838         break;
839     case KVM_S390_INT_EXTERNAL_CALL:
840         interrupt->parm = irq->u.extcall.code;
841         break;
842     case KVM_S390_INT_EMERGENCY:
843         interrupt->parm = irq->u.emerg.code;
844         break;
845     case KVM_S390_SIGP_STOP:
846     case KVM_S390_RESTART:
847         break; /* These types have no parameters */
848     case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
849         interrupt->parm = irq->u.io.subchannel_id << 16;
850         interrupt->parm |= irq->u.io.subchannel_nr;
851         interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
852         interrupt->parm64 |= irq->u.io.io_int_word;
853         break;
854     default:
855         r = -EINVAL;
856         break;
857     }
858     return r;
859 }
860
861 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq)
862 {
863     struct kvm_s390_interrupt kvmint = {};
864     int r;
865
866     r = s390_kvm_irq_to_interrupt(irq, &kvmint);
867     if (r < 0) {
868         fprintf(stderr, "%s called with bogus interrupt\n", __func__);
869         exit(1);
870     }
871
872     r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
873     if (r < 0) {
874         fprintf(stderr, "KVM failed to inject interrupt\n");
875         exit(1);
876     }
877 }
878
879 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
880 {
881     CPUState *cs = CPU(cpu);
882     int r;
883
884     if (cap_s390_irq) {
885         r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq);
886         if (!r) {
887             return;
888         }
889         error_report("KVM failed to inject interrupt %llx", irq->type);
890         exit(1);
891     }
892
893     inject_vcpu_irq_legacy(cs, irq);
894 }
895
896 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
897 {
898     struct kvm_s390_interrupt kvmint = {};
899     int r;
900
901     r = s390_kvm_irq_to_interrupt(irq, &kvmint);
902     if (r < 0) {
903         fprintf(stderr, "%s called with bogus interrupt\n", __func__);
904         exit(1);
905     }
906
907     r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
908     if (r < 0) {
909         fprintf(stderr, "KVM failed to inject interrupt\n");
910         exit(1);
911     }
912 }
913
914 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
915 {
916     static bool use_flic = true;
917     int r;
918
919     if (use_flic) {
920         r = kvm_s390_inject_flic(irq);
921         if (r == -ENOSYS) {
922             use_flic = false;
923         }
924         if (!r) {
925             return;
926         }
927     }
928     __kvm_s390_floating_interrupt(irq);
929 }
930
931 void kvm_s390_virtio_irq(int config_change, uint64_t token)
932 {
933     struct kvm_s390_irq irq = {
934         .type = KVM_S390_INT_VIRTIO,
935         .u.ext.ext_params = config_change,
936         .u.ext.ext_params2 = token,
937     };
938
939     kvm_s390_floating_interrupt(&irq);
940 }
941
942 void kvm_s390_service_interrupt(uint32_t parm)
943 {
944     struct kvm_s390_irq irq = {
945         .type = KVM_S390_INT_SERVICE,
946         .u.ext.ext_params = parm,
947     };
948
949     kvm_s390_floating_interrupt(&irq);
950 }
951
952 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
953 {
954     struct kvm_s390_irq irq = {
955         .type = KVM_S390_PROGRAM_INT,
956         .u.pgm.code = code,
957     };
958
959     kvm_s390_vcpu_interrupt(cpu, &irq);
960 }
961
962 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
963 {
964     struct kvm_s390_irq irq = {
965         .type = KVM_S390_PROGRAM_INT,
966         .u.pgm.code = code,
967         .u.pgm.trans_exc_code = te_code,
968         .u.pgm.exc_access_id = te_code & 3,
969     };
970
971     kvm_s390_vcpu_interrupt(cpu, &irq);
972 }
973
974 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
975                                  uint16_t ipbh0)
976 {
977     CPUS390XState *env = &cpu->env;
978     uint64_t sccb;
979     uint32_t code;
980     int r = 0;
981
982     cpu_synchronize_state(CPU(cpu));
983     sccb = env->regs[ipbh0 & 0xf];
984     code = env->regs[(ipbh0 & 0xf0) >> 4];
985
986     r = sclp_service_call(env, sccb, code);
987     if (r < 0) {
988         enter_pgmcheck(cpu, -r);
989     } else {
990         setcc(cpu, r);
991     }
992
993     return 0;
994 }
995
996 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
997 {
998     CPUS390XState *env = &cpu->env;
999     int rc = 0;
1000     uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
1001
1002     cpu_synchronize_state(CPU(cpu));
1003
1004     switch (ipa1) {
1005     case PRIV_B2_XSCH:
1006         ioinst_handle_xsch(cpu, env->regs[1]);
1007         break;
1008     case PRIV_B2_CSCH:
1009         ioinst_handle_csch(cpu, env->regs[1]);
1010         break;
1011     case PRIV_B2_HSCH:
1012         ioinst_handle_hsch(cpu, env->regs[1]);
1013         break;
1014     case PRIV_B2_MSCH:
1015         ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
1016         break;
1017     case PRIV_B2_SSCH:
1018         ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
1019         break;
1020     case PRIV_B2_STCRW:
1021         ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
1022         break;
1023     case PRIV_B2_STSCH:
1024         ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
1025         break;
1026     case PRIV_B2_TSCH:
1027         /* We should only get tsch via KVM_EXIT_S390_TSCH. */
1028         fprintf(stderr, "Spurious tsch intercept\n");
1029         break;
1030     case PRIV_B2_CHSC:
1031         ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
1032         break;
1033     case PRIV_B2_TPI:
1034         /* This should have been handled by kvm already. */
1035         fprintf(stderr, "Spurious tpi intercept\n");
1036         break;
1037     case PRIV_B2_SCHM:
1038         ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
1039                            run->s390_sieic.ipb);
1040         break;
1041     case PRIV_B2_RSCH:
1042         ioinst_handle_rsch(cpu, env->regs[1]);
1043         break;
1044     case PRIV_B2_RCHP:
1045         ioinst_handle_rchp(cpu, env->regs[1]);
1046         break;
1047     case PRIV_B2_STCPS:
1048         /* We do not provide this instruction, it is suppressed. */
1049         break;
1050     case PRIV_B2_SAL:
1051         ioinst_handle_sal(cpu, env->regs[1]);
1052         break;
1053     case PRIV_B2_SIGA:
1054         /* Not provided, set CC = 3 for subchannel not operational */
1055         setcc(cpu, 3);
1056         break;
1057     case PRIV_B2_SCLP_CALL:
1058         rc = kvm_sclp_service_call(cpu, run, ipbh0);
1059         break;
1060     default:
1061         rc = -1;
1062         DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
1063         break;
1064     }
1065
1066     return rc;
1067 }
1068
1069 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run,
1070                                   uint8_t *ar)
1071 {
1072     CPUS390XState *env = &cpu->env;
1073     uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
1074     uint32_t base2 = run->s390_sieic.ipb >> 28;
1075     uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1076                      ((run->s390_sieic.ipb & 0xff00) << 4);
1077
1078     if (disp2 & 0x80000) {
1079         disp2 += 0xfff00000;
1080     }
1081     if (ar) {
1082         *ar = base2;
1083     }
1084
1085     return (base2 ? env->regs[base2] : 0) +
1086            (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
1087 }
1088
1089 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run,
1090                                   uint8_t *ar)
1091 {
1092     CPUS390XState *env = &cpu->env;
1093     uint32_t base2 = run->s390_sieic.ipb >> 28;
1094     uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1095                      ((run->s390_sieic.ipb & 0xff00) << 4);
1096
1097     if (disp2 & 0x80000) {
1098         disp2 += 0xfff00000;
1099     }
1100     if (ar) {
1101         *ar = base2;
1102     }
1103
1104     return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1105 }
1106
1107 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1108 {
1109     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1110
1111     return clp_service_call(cpu, r2);
1112 }
1113
1114 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1115 {
1116     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1117     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1118
1119     return pcilg_service_call(cpu, r1, r2);
1120 }
1121
1122 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1123 {
1124     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1125     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1126
1127     return pcistg_service_call(cpu, r1, r2);
1128 }
1129
1130 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1131 {
1132     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1133     uint64_t fiba;
1134     uint8_t ar;
1135
1136     cpu_synchronize_state(CPU(cpu));
1137     fiba = get_base_disp_rxy(cpu, run, &ar);
1138
1139     return stpcifc_service_call(cpu, r1, fiba, ar);
1140 }
1141
1142 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1143 {
1144     /* NOOP */
1145     return 0;
1146 }
1147
1148 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1149 {
1150     uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1151     uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1152
1153     return rpcit_service_call(cpu, r1, r2);
1154 }
1155
1156 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1157 {
1158     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1159     uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1160     uint64_t gaddr;
1161     uint8_t ar;
1162
1163     cpu_synchronize_state(CPU(cpu));
1164     gaddr = get_base_disp_rsy(cpu, run, &ar);
1165
1166     return pcistb_service_call(cpu, r1, r3, gaddr, ar);
1167 }
1168
1169 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1170 {
1171     uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1172     uint64_t fiba;
1173     uint8_t ar;
1174
1175     cpu_synchronize_state(CPU(cpu));
1176     fiba = get_base_disp_rxy(cpu, run, &ar);
1177
1178     return mpcifc_service_call(cpu, r1, fiba, ar);
1179 }
1180
1181 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1182 {
1183     int r = 0;
1184
1185     switch (ipa1) {
1186     case PRIV_B9_CLP:
1187         r = kvm_clp_service_call(cpu, run);
1188         break;
1189     case PRIV_B9_PCISTG:
1190         r = kvm_pcistg_service_call(cpu, run);
1191         break;
1192     case PRIV_B9_PCILG:
1193         r = kvm_pcilg_service_call(cpu, run);
1194         break;
1195     case PRIV_B9_RPCIT:
1196         r = kvm_rpcit_service_call(cpu, run);
1197         break;
1198     case PRIV_B9_EQBS:
1199         /* just inject exception */
1200         r = -1;
1201         break;
1202     default:
1203         r = -1;
1204         DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1205         break;
1206     }
1207
1208     return r;
1209 }
1210
1211 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1212 {
1213     int r = 0;
1214
1215     switch (ipbl) {
1216     case PRIV_EB_PCISTB:
1217         r = kvm_pcistb_service_call(cpu, run);
1218         break;
1219     case PRIV_EB_SIC:
1220         r = kvm_sic_service_call(cpu, run);
1221         break;
1222     case PRIV_EB_SQBS:
1223         /* just inject exception */
1224         r = -1;
1225         break;
1226     default:
1227         r = -1;
1228         DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1229         break;
1230     }
1231
1232     return r;
1233 }
1234
1235 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1236 {
1237     int r = 0;
1238
1239     switch (ipbl) {
1240     case PRIV_E3_MPCIFC:
1241         r = kvm_mpcifc_service_call(cpu, run);
1242         break;
1243     case PRIV_E3_STPCIFC:
1244         r = kvm_stpcifc_service_call(cpu, run);
1245         break;
1246     default:
1247         r = -1;
1248         DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1249         break;
1250     }
1251
1252     return r;
1253 }
1254
1255 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1256 {
1257     CPUS390XState *env = &cpu->env;
1258     int ret;
1259
1260     cpu_synchronize_state(CPU(cpu));
1261     ret = s390_virtio_hypercall(env);
1262     if (ret == -EINVAL) {
1263         enter_pgmcheck(cpu, PGM_SPECIFICATION);
1264         return 0;
1265     }
1266
1267     return ret;
1268 }
1269
1270 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1271 {
1272     uint64_t r1, r3;
1273
1274     cpu_synchronize_state(CPU(cpu));
1275     r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1276     r3 = run->s390_sieic.ipa & 0x000f;
1277     handle_diag_308(&cpu->env, r1, r3);
1278 }
1279
1280 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1281 {
1282     CPUS390XState *env = &cpu->env;
1283     unsigned long pc;
1284
1285     cpu_synchronize_state(CPU(cpu));
1286
1287     pc = env->psw.addr - 4;
1288     if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1289         env->psw.addr = pc;
1290         return EXCP_DEBUG;
1291     }
1292
1293     return -ENOENT;
1294 }
1295
1296 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1297
1298 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1299 {
1300     int r = 0;
1301     uint16_t func_code;
1302
1303     /*
1304      * For any diagnose call we support, bits 48-63 of the resulting
1305      * address specify the function code; the remainder is ignored.
1306      */
1307     func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK;
1308     switch (func_code) {
1309     case DIAG_IPL:
1310         kvm_handle_diag_308(cpu, run);
1311         break;
1312     case DIAG_KVM_HYPERCALL:
1313         r = handle_hypercall(cpu, run);
1314         break;
1315     case DIAG_KVM_BREAKPOINT:
1316         r = handle_sw_breakpoint(cpu, run);
1317         break;
1318     default:
1319         DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1320         enter_pgmcheck(cpu, PGM_SPECIFICATION);
1321         break;
1322     }
1323
1324     return r;
1325 }
1326
1327 typedef struct SigpInfo {
1328     S390CPU *cpu;
1329     uint64_t param;
1330     int cc;
1331     uint64_t *status_reg;
1332 } SigpInfo;
1333
1334 static void set_sigp_status(SigpInfo *si, uint64_t status)
1335 {
1336     *si->status_reg &= 0xffffffff00000000ULL;
1337     *si->status_reg |= status;
1338     si->cc = SIGP_CC_STATUS_STORED;
1339 }
1340
1341 static void sigp_start(void *arg)
1342 {
1343     SigpInfo *si = arg;
1344
1345     if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1346         si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1347         return;
1348     }
1349
1350     s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1351     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1352 }
1353
1354 static void sigp_stop(void *arg)
1355 {
1356     SigpInfo *si = arg;
1357     struct kvm_s390_irq irq = {
1358         .type = KVM_S390_SIGP_STOP,
1359     };
1360
1361     if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1362         si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1363         return;
1364     }
1365
1366     /* disabled wait - sleeping in user space */
1367     if (CPU(si->cpu)->halted) {
1368         s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1369     } else {
1370         /* execute the stop function */
1371         si->cpu->env.sigp_order = SIGP_STOP;
1372         kvm_s390_vcpu_interrupt(si->cpu, &irq);
1373     }
1374     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1375 }
1376
1377 #define ADTL_SAVE_AREA_SIZE 1024
1378 static int kvm_s390_store_adtl_status(S390CPU *cpu, hwaddr addr)
1379 {
1380     void *mem;
1381     hwaddr len = ADTL_SAVE_AREA_SIZE;
1382
1383     mem = cpu_physical_memory_map(addr, &len, 1);
1384     if (!mem) {
1385         return -EFAULT;
1386     }
1387     if (len != ADTL_SAVE_AREA_SIZE) {
1388         cpu_physical_memory_unmap(mem, len, 1, 0);
1389         return -EFAULT;
1390     }
1391
1392     memcpy(mem, &cpu->env.vregs, 512);
1393
1394     cpu_physical_memory_unmap(mem, len, 1, len);
1395
1396     return 0;
1397 }
1398
1399 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1400 #define SAVE_AREA_SIZE 512
1401 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1402 {
1403     static const uint8_t ar_id = 1;
1404     uint64_t ckc = cpu->env.ckc >> 8;
1405     void *mem;
1406     int i;
1407     hwaddr len = SAVE_AREA_SIZE;
1408
1409     mem = cpu_physical_memory_map(addr, &len, 1);
1410     if (!mem) {
1411         return -EFAULT;
1412     }
1413     if (len != SAVE_AREA_SIZE) {
1414         cpu_physical_memory_unmap(mem, len, 1, 0);
1415         return -EFAULT;
1416     }
1417
1418     if (store_arch) {
1419         cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1420     }
1421     for (i = 0; i < 16; ++i) {
1422         *((uint64 *)mem + i) = get_freg(&cpu->env, i)->ll;
1423     }
1424     memcpy(mem + 128, &cpu->env.regs, 128);
1425     memcpy(mem + 256, &cpu->env.psw, 16);
1426     memcpy(mem + 280, &cpu->env.psa, 4);
1427     memcpy(mem + 284, &cpu->env.fpc, 4);
1428     memcpy(mem + 292, &cpu->env.todpr, 4);
1429     memcpy(mem + 296, &cpu->env.cputm, 8);
1430     memcpy(mem + 304, &ckc, 8);
1431     memcpy(mem + 320, &cpu->env.aregs, 64);
1432     memcpy(mem + 384, &cpu->env.cregs, 128);
1433
1434     cpu_physical_memory_unmap(mem, len, 1, len);
1435
1436     return 0;
1437 }
1438
1439 static void sigp_stop_and_store_status(void *arg)
1440 {
1441     SigpInfo *si = arg;
1442     struct kvm_s390_irq irq = {
1443         .type = KVM_S390_SIGP_STOP,
1444     };
1445
1446     /* disabled wait - sleeping in user space */
1447     if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1448         CPU(si->cpu)->halted) {
1449         s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1450     }
1451
1452     switch (s390_cpu_get_state(si->cpu)) {
1453     case CPU_STATE_OPERATING:
1454         si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1455         kvm_s390_vcpu_interrupt(si->cpu, &irq);
1456         /* store will be performed when handling the stop intercept */
1457         break;
1458     case CPU_STATE_STOPPED:
1459         /* already stopped, just store the status */
1460         cpu_synchronize_state(CPU(si->cpu));
1461         kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1462         break;
1463     }
1464     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1465 }
1466
1467 static void sigp_store_status_at_address(void *arg)
1468 {
1469     SigpInfo *si = arg;
1470     uint32_t address = si->param & 0x7ffffe00u;
1471
1472     /* cpu has to be stopped */
1473     if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1474         set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1475         return;
1476     }
1477
1478     cpu_synchronize_state(CPU(si->cpu));
1479
1480     if (kvm_s390_store_status(si->cpu, address, false)) {
1481         set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1482         return;
1483     }
1484     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1485 }
1486
1487 static void sigp_store_adtl_status(void *arg)
1488 {
1489     SigpInfo *si = arg;
1490
1491     if (!kvm_check_extension(kvm_state, KVM_CAP_S390_VECTOR_REGISTERS)) {
1492         set_sigp_status(si, SIGP_STAT_INVALID_ORDER);
1493         return;
1494     }
1495
1496     /* cpu has to be stopped */
1497     if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1498         set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1499         return;
1500     }
1501
1502     /* parameter must be aligned to 1024-byte boundary */
1503     if (si->param & 0x3ff) {
1504         set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1505         return;
1506     }
1507
1508     cpu_synchronize_state(CPU(si->cpu));
1509
1510     if (kvm_s390_store_adtl_status(si->cpu, si->param)) {
1511         set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1512         return;
1513     }
1514     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1515 }
1516
1517 static void sigp_restart(void *arg)
1518 {
1519     SigpInfo *si = arg;
1520     struct kvm_s390_irq irq = {
1521         .type = KVM_S390_RESTART,
1522     };
1523
1524     switch (s390_cpu_get_state(si->cpu)) {
1525     case CPU_STATE_STOPPED:
1526         /* the restart irq has to be delivered prior to any other pending irq */
1527         cpu_synchronize_state(CPU(si->cpu));
1528         do_restart_interrupt(&si->cpu->env);
1529         s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1530         break;
1531     case CPU_STATE_OPERATING:
1532         kvm_s390_vcpu_interrupt(si->cpu, &irq);
1533         break;
1534     }
1535     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1536 }
1537
1538 int kvm_s390_cpu_restart(S390CPU *cpu)
1539 {
1540     SigpInfo si = {
1541         .cpu = cpu,
1542     };
1543
1544     run_on_cpu(CPU(cpu), sigp_restart, &si);
1545     DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1546     return 0;
1547 }
1548
1549 static void sigp_initial_cpu_reset(void *arg)
1550 {
1551     SigpInfo *si = arg;
1552     CPUState *cs = CPU(si->cpu);
1553     S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1554
1555     cpu_synchronize_state(cs);
1556     scc->initial_cpu_reset(cs);
1557     cpu_synchronize_post_reset(cs);
1558     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1559 }
1560
1561 static void sigp_cpu_reset(void *arg)
1562 {
1563     SigpInfo *si = arg;
1564     CPUState *cs = CPU(si->cpu);
1565     S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1566
1567     cpu_synchronize_state(cs);
1568     scc->cpu_reset(cs);
1569     cpu_synchronize_post_reset(cs);
1570     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1571 }
1572
1573 static void sigp_set_prefix(void *arg)
1574 {
1575     SigpInfo *si = arg;
1576     uint32_t addr = si->param & 0x7fffe000u;
1577
1578     cpu_synchronize_state(CPU(si->cpu));
1579
1580     if (!address_space_access_valid(&address_space_memory, addr,
1581                                     sizeof(struct LowCore), false)) {
1582         set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1583         return;
1584     }
1585
1586     /* cpu has to be stopped */
1587     if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1588         set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1589         return;
1590     }
1591
1592     si->cpu->env.psa = addr;
1593     cpu_synchronize_post_init(CPU(si->cpu));
1594     si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1595 }
1596
1597 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1598                                   uint64_t param, uint64_t *status_reg)
1599 {
1600     SigpInfo si = {
1601         .cpu = dst_cpu,
1602         .param = param,
1603         .status_reg = status_reg,
1604     };
1605
1606     /* cpu available? */
1607     if (dst_cpu == NULL) {
1608         return SIGP_CC_NOT_OPERATIONAL;
1609     }
1610
1611     /* only resets can break pending orders */
1612     if (dst_cpu->env.sigp_order != 0 &&
1613         order != SIGP_CPU_RESET &&
1614         order != SIGP_INITIAL_CPU_RESET) {
1615         return SIGP_CC_BUSY;
1616     }
1617
1618     switch (order) {
1619     case SIGP_START:
1620         run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1621         break;
1622     case SIGP_STOP:
1623         run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1624         break;
1625     case SIGP_RESTART:
1626         run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1627         break;
1628     case SIGP_STOP_STORE_STATUS:
1629         run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1630         break;
1631     case SIGP_STORE_STATUS_ADDR:
1632         run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1633         break;
1634     case SIGP_STORE_ADTL_STATUS:
1635         run_on_cpu(CPU(dst_cpu), sigp_store_adtl_status, &si);
1636         break;
1637     case SIGP_SET_PREFIX:
1638         run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1639         break;
1640     case SIGP_INITIAL_CPU_RESET:
1641         run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1642         break;
1643     case SIGP_CPU_RESET:
1644         run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1645         break;
1646     default:
1647         DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1648         set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1649     }
1650
1651     return si.cc;
1652 }
1653
1654 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1655                                  uint64_t *status_reg)
1656 {
1657     CPUState *cur_cs;
1658     S390CPU *cur_cpu;
1659
1660     /* due to the BQL, we are the only active cpu */
1661     CPU_FOREACH(cur_cs) {
1662         cur_cpu = S390_CPU(cur_cs);
1663         if (cur_cpu->env.sigp_order != 0) {
1664             return SIGP_CC_BUSY;
1665         }
1666         cpu_synchronize_state(cur_cs);
1667         /* all but the current one have to be stopped */
1668         if (cur_cpu != cpu &&
1669             s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1670             *status_reg &= 0xffffffff00000000ULL;
1671             *status_reg |= SIGP_STAT_INCORRECT_STATE;
1672             return SIGP_CC_STATUS_STORED;
1673         }
1674     }
1675
1676     switch (param & 0xff) {
1677     case SIGP_MODE_ESA_S390:
1678         /* not supported */
1679         return SIGP_CC_NOT_OPERATIONAL;
1680     case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1681     case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1682         CPU_FOREACH(cur_cs) {
1683             cur_cpu = S390_CPU(cur_cs);
1684             cur_cpu->env.pfault_token = -1UL;
1685         }
1686         break;
1687     default:
1688         *status_reg &= 0xffffffff00000000ULL;
1689         *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1690         return SIGP_CC_STATUS_STORED;
1691     }
1692
1693     return SIGP_CC_ORDER_CODE_ACCEPTED;
1694 }
1695
1696 #define SIGP_ORDER_MASK 0x000000ff
1697
1698 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1699 {
1700     CPUS390XState *env = &cpu->env;
1701     const uint8_t r1 = ipa1 >> 4;
1702     const uint8_t r3 = ipa1 & 0x0f;
1703     int ret;
1704     uint8_t order;
1705     uint64_t *status_reg;
1706     uint64_t param;
1707     S390CPU *dst_cpu = NULL;
1708
1709     cpu_synchronize_state(CPU(cpu));
1710
1711     /* get order code */
1712     order = decode_basedisp_rs(env, run->s390_sieic.ipb, NULL)
1713         & SIGP_ORDER_MASK;
1714     status_reg = &env->regs[r1];
1715     param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1716
1717     switch (order) {
1718     case SIGP_SET_ARCH:
1719         ret = sigp_set_architecture(cpu, param, status_reg);
1720         break;
1721     default:
1722         /* all other sigp orders target a single vcpu */
1723         dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1724         ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1725     }
1726
1727     trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1728                             dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1729
1730     if (ret >= 0) {
1731         setcc(cpu, ret);
1732         return 0;
1733     }
1734
1735     return ret;
1736 }
1737
1738 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1739 {
1740     unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1741     uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1742     int r = -1;
1743
1744     DPRINTF("handle_instruction 0x%x 0x%x\n",
1745             run->s390_sieic.ipa, run->s390_sieic.ipb);
1746     switch (ipa0) {
1747     case IPA0_B2:
1748         r = handle_b2(cpu, run, ipa1);
1749         break;
1750     case IPA0_B9:
1751         r = handle_b9(cpu, run, ipa1);
1752         break;
1753     case IPA0_EB:
1754         r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1755         break;
1756     case IPA0_E3:
1757         r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1758         break;
1759     case IPA0_DIAG:
1760         r = handle_diag(cpu, run, run->s390_sieic.ipb);
1761         break;
1762     case IPA0_SIGP:
1763         r = handle_sigp(cpu, run, ipa1);
1764         break;
1765     }
1766
1767     if (r < 0) {
1768         r = 0;
1769         enter_pgmcheck(cpu, 0x0001);
1770     }
1771
1772     return r;
1773 }
1774
1775 static bool is_special_wait_psw(CPUState *cs)
1776 {
1777     /* signal quiesce */
1778     return cs->kvm_run->psw_addr == 0xfffUL;
1779 }
1780
1781 static void guest_panicked(void)
1782 {
1783     qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1784                                    &error_abort);
1785     vm_stop(RUN_STATE_GUEST_PANICKED);
1786 }
1787
1788 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1789 {
1790     CPUState *cs = CPU(cpu);
1791
1792     error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1793                  str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1794                  ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1795     s390_cpu_halt(cpu);
1796     guest_panicked();
1797 }
1798
1799 static int handle_intercept(S390CPU *cpu)
1800 {
1801     CPUState *cs = CPU(cpu);
1802     struct kvm_run *run = cs->kvm_run;
1803     int icpt_code = run->s390_sieic.icptcode;
1804     int r = 0;
1805
1806     DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1807             (long)cs->kvm_run->psw_addr);
1808     switch (icpt_code) {
1809         case ICPT_INSTRUCTION:
1810             r = handle_instruction(cpu, run);
1811             break;
1812         case ICPT_PROGRAM:
1813             unmanageable_intercept(cpu, "program interrupt",
1814                                    offsetof(LowCore, program_new_psw));
1815             r = EXCP_HALTED;
1816             break;
1817         case ICPT_EXT_INT:
1818             unmanageable_intercept(cpu, "external interrupt",
1819                                    offsetof(LowCore, external_new_psw));
1820             r = EXCP_HALTED;
1821             break;
1822         case ICPT_WAITPSW:
1823             /* disabled wait, since enabled wait is handled in kernel */
1824             cpu_synchronize_state(cs);
1825             if (s390_cpu_halt(cpu) == 0) {
1826                 if (is_special_wait_psw(cs)) {
1827                     qemu_system_shutdown_request();
1828                 } else {
1829                     guest_panicked();
1830                 }
1831             }
1832             r = EXCP_HALTED;
1833             break;
1834         case ICPT_CPU_STOP:
1835             if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1836                 qemu_system_shutdown_request();
1837             }
1838             if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1839                 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1840                                       true);
1841             }
1842             cpu->env.sigp_order = 0;
1843             r = EXCP_HALTED;
1844             break;
1845         case ICPT_SOFT_INTERCEPT:
1846             fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1847             exit(1);
1848             break;
1849         case ICPT_IO:
1850             fprintf(stderr, "KVM unimplemented icpt IO\n");
1851             exit(1);
1852             break;
1853         default:
1854             fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1855             exit(1);
1856             break;
1857     }
1858
1859     return r;
1860 }
1861
1862 static int handle_tsch(S390CPU *cpu)
1863 {
1864     CPUState *cs = CPU(cpu);
1865     struct kvm_run *run = cs->kvm_run;
1866     int ret;
1867
1868     cpu_synchronize_state(cs);
1869
1870     ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1871     if (ret < 0) {
1872         /*
1873          * Failure.
1874          * If an I/O interrupt had been dequeued, we have to reinject it.
1875          */
1876         if (run->s390_tsch.dequeued) {
1877             kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1878                                   run->s390_tsch.subchannel_nr,
1879                                   run->s390_tsch.io_int_parm,
1880                                   run->s390_tsch.io_int_word);
1881         }
1882         ret = 0;
1883     }
1884     return ret;
1885 }
1886
1887 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar)
1888 {
1889     struct sysib_322 sysib;
1890     int del;
1891
1892     if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) {
1893         return;
1894     }
1895     /* Shift the stack of Extended Names to prepare for our own data */
1896     memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1897             sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1898     /* First virt level, that doesn't provide Ext Names delimits stack. It is
1899      * assumed it's not capable of managing Extended Names for lower levels.
1900      */
1901     for (del = 1; del < sysib.count; del++) {
1902         if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1903             break;
1904         }
1905     }
1906     if (del < sysib.count) {
1907         memset(sysib.ext_names[del], 0,
1908                sizeof(sysib.ext_names[0]) * (sysib.count - del));
1909     }
1910     /* Insert short machine name in EBCDIC, padded with blanks */
1911     if (qemu_name) {
1912         memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1913         ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1914                                                     strlen(qemu_name)));
1915     }
1916     sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1917     memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1918     /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1919      * considered by s390 as not capable of providing any Extended Name.
1920      * Therefore if no name was specified on qemu invocation, we go with the
1921      * same "KVMguest" default, which KVM has filled into short name field.
1922      */
1923     if (qemu_name) {
1924         strncpy((char *)sysib.ext_names[0], qemu_name,
1925                 sizeof(sysib.ext_names[0]));
1926     } else {
1927         strcpy((char *)sysib.ext_names[0], "KVMguest");
1928     }
1929     /* Insert UUID */
1930     memcpy(sysib.vm[0].uuid, qemu_uuid, sizeof(sysib.vm[0].uuid));
1931
1932     s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib));
1933 }
1934
1935 static int handle_stsi(S390CPU *cpu)
1936 {
1937     CPUState *cs = CPU(cpu);
1938     struct kvm_run *run = cs->kvm_run;
1939
1940     switch (run->s390_stsi.fc) {
1941     case 3:
1942         if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1943             return 0;
1944         }
1945         /* Only sysib 3.2.2 needs post-handling for now. */
1946         insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar);
1947         return 0;
1948     default:
1949         return 0;
1950     }
1951 }
1952
1953 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1954 {
1955     CPUState *cs = CPU(cpu);
1956     struct kvm_run *run = cs->kvm_run;
1957
1958     int ret = 0;
1959     struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1960
1961     switch (arch_info->type) {
1962     case KVM_HW_WP_WRITE:
1963         if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1964             cs->watchpoint_hit = &hw_watchpoint;
1965             hw_watchpoint.vaddr = arch_info->addr;
1966             hw_watchpoint.flags = BP_MEM_WRITE;
1967             ret = EXCP_DEBUG;
1968         }
1969         break;
1970     case KVM_HW_BP:
1971         if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1972             ret = EXCP_DEBUG;
1973         }
1974         break;
1975     case KVM_SINGLESTEP:
1976         if (cs->singlestep_enabled) {
1977             ret = EXCP_DEBUG;
1978         }
1979         break;
1980     default:
1981         ret = -ENOSYS;
1982     }
1983
1984     return ret;
1985 }
1986
1987 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1988 {
1989     S390CPU *cpu = S390_CPU(cs);
1990     int ret = 0;
1991
1992     switch (run->exit_reason) {
1993         case KVM_EXIT_S390_SIEIC:
1994             ret = handle_intercept(cpu);
1995             break;
1996         case KVM_EXIT_S390_RESET:
1997             s390_reipl_request();
1998             break;
1999         case KVM_EXIT_S390_TSCH:
2000             ret = handle_tsch(cpu);
2001             break;
2002         case KVM_EXIT_S390_STSI:
2003             ret = handle_stsi(cpu);
2004             break;
2005         case KVM_EXIT_DEBUG:
2006             ret = kvm_arch_handle_debug_exit(cpu);
2007             break;
2008         default:
2009             fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
2010             break;
2011     }
2012
2013     if (ret == 0) {
2014         ret = EXCP_INTERRUPT;
2015     }
2016     return ret;
2017 }
2018
2019 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2020 {
2021     return true;
2022 }
2023
2024 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
2025 {
2026     return 1;
2027 }
2028
2029 int kvm_arch_on_sigbus(int code, void *addr)
2030 {
2031     return 1;
2032 }
2033
2034 void kvm_s390_io_interrupt(uint16_t subchannel_id,
2035                            uint16_t subchannel_nr, uint32_t io_int_parm,
2036                            uint32_t io_int_word)
2037 {
2038     struct kvm_s390_irq irq = {
2039         .u.io.subchannel_id = subchannel_id,
2040         .u.io.subchannel_nr = subchannel_nr,
2041         .u.io.io_int_parm = io_int_parm,
2042         .u.io.io_int_word = io_int_word,
2043     };
2044
2045     if (io_int_word & IO_INT_WORD_AI) {
2046         irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
2047     } else {
2048         irq.type = ((subchannel_id & 0xff00) << 24) |
2049             ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
2050     }
2051     kvm_s390_floating_interrupt(&irq);
2052 }
2053
2054 void kvm_s390_crw_mchk(void)
2055 {
2056     struct kvm_s390_irq irq = {
2057         .type = KVM_S390_MCHK,
2058         .u.mchk.cr14 = 1 << 28,
2059         .u.mchk.mcic = 0x00400f1d40330000ULL,
2060     };
2061     kvm_s390_floating_interrupt(&irq);
2062 }
2063
2064 void kvm_s390_enable_css_support(S390CPU *cpu)
2065 {
2066     int r;
2067
2068     /* Activate host kernel channel subsystem support. */
2069     r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
2070     assert(r == 0);
2071 }
2072
2073 void kvm_arch_init_irq_routing(KVMState *s)
2074 {
2075     /*
2076      * Note that while irqchip capabilities generally imply that cpustates
2077      * are handled in-kernel, it is not true for s390 (yet); therefore, we
2078      * have to override the common code kvm_halt_in_kernel_allowed setting.
2079      */
2080     if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
2081         kvm_gsi_routing_allowed = true;
2082         kvm_halt_in_kernel_allowed = false;
2083     }
2084 }
2085
2086 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
2087                                     int vq, bool assign)
2088 {
2089     struct kvm_ioeventfd kick = {
2090         .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
2091         KVM_IOEVENTFD_FLAG_DATAMATCH,
2092         .fd = event_notifier_get_fd(notifier),
2093         .datamatch = vq,
2094         .addr = sch,
2095         .len = 8,
2096     };
2097     if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
2098         return -ENOSYS;
2099     }
2100     if (!assign) {
2101         kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
2102     }
2103     return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
2104 }
2105
2106 int kvm_s390_get_memslot_count(KVMState *s)
2107 {
2108     return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
2109 }
2110
2111 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
2112 {
2113     struct kvm_mp_state mp_state = {};
2114     int ret;
2115
2116     /* the kvm part might not have been initialized yet */
2117     if (CPU(cpu)->kvm_state == NULL) {
2118         return 0;
2119     }
2120
2121     switch (cpu_state) {
2122     case CPU_STATE_STOPPED:
2123         mp_state.mp_state = KVM_MP_STATE_STOPPED;
2124         break;
2125     case CPU_STATE_CHECK_STOP:
2126         mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
2127         break;
2128     case CPU_STATE_OPERATING:
2129         mp_state.mp_state = KVM_MP_STATE_OPERATING;
2130         break;
2131     case CPU_STATE_LOAD:
2132         mp_state.mp_state = KVM_MP_STATE_LOAD;
2133         break;
2134     default:
2135         error_report("Requested CPU state is not a valid S390 CPU state: %u",
2136                      cpu_state);
2137         exit(1);
2138     }
2139
2140     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2141     if (ret) {
2142         trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
2143                                        strerror(-ret));
2144     }
2145
2146     return ret;
2147 }
2148
2149 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu)
2150 {
2151     struct kvm_s390_irq_state irq_state;
2152     CPUState *cs = CPU(cpu);
2153     int32_t bytes;
2154
2155     if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2156         return;
2157     }
2158
2159     irq_state.buf = (uint64_t) cpu->irqstate;
2160     irq_state.len = VCPU_IRQ_BUF_SIZE;
2161
2162     bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state);
2163     if (bytes < 0) {
2164         cpu->irqstate_saved_size = 0;
2165         error_report("Migration of interrupt state failed");
2166         return;
2167     }
2168
2169     cpu->irqstate_saved_size = bytes;
2170 }
2171
2172 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu)
2173 {
2174     CPUState *cs = CPU(cpu);
2175     struct kvm_s390_irq_state irq_state;
2176     int r;
2177
2178     if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) {
2179         return -ENOSYS;
2180     }
2181
2182     if (cpu->irqstate_saved_size == 0) {
2183         return 0;
2184     }
2185     irq_state.buf = (uint64_t) cpu->irqstate;
2186     irq_state.len = cpu->irqstate_saved_size;
2187
2188     r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state);
2189     if (r) {
2190         error_report("Setting interrupt state failed %d", r);
2191     }
2192     return r;
2193 }
2194
2195 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2196                               uint64_t address, uint32_t data)
2197 {
2198     S390PCIBusDevice *pbdev;
2199     uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
2200     uint32_t vec = data & ZPCI_MSI_VEC_MASK;
2201
2202     pbdev = s390_pci_find_dev_by_fid(fid);
2203     if (!pbdev) {
2204         DPRINTF("add_msi_route no dev\n");
2205         return -ENODEV;
2206     }
2207
2208     pbdev->routes.adapter.ind_offset = vec;
2209
2210     route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2211     route->flags = 0;
2212     route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2213     route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2214     route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2215     route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2216     route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2217     return 0;
2218 }
2219
2220 int kvm_arch_msi_data_to_gsi(uint32_t data)
2221 {
2222     abort();
2223 }
This page took 0.148194 seconds and 4 git commands to generate.