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