]> git.proxmox.com Git - mirror_qemu.git/blame - target-s390x/kvm.c
s390x/kvm: proper use of the cpu states OPERATING and STOPPED
[mirror_qemu.git] / target-s390x / kvm.c
CommitLineData
0e60a699
AG
1/*
2 * QEMU S390x KVM implementation
3 *
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
ccb084d3 5 * Copyright IBM Corp. 2012
0e60a699
AG
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 *
ccb084d3
CB
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
0e60a699
AG
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"
1de7afc9 32#include "qemu/timer.h"
9c17d615
PB
33#include "sysemu/sysemu.h"
34#include "sysemu/kvm.h"
4cb88c3c 35#include "hw/hw.h"
0e60a699 36#include "cpu.h"
9c17d615 37#include "sysemu/device_tree.h"
08eb8c85
CB
38#include "qapi/qmp/qjson.h"
39#include "monitor/monitor.h"
770a6379 40#include "exec/gdbstub.h"
860643bc 41#include "trace.h"
3a449690 42#include "qapi-event.h"
0e60a699
AG
43
44/* #define DEBUG_KVM */
45
46#ifdef DEBUG_KVM
e67137c6 47#define DPRINTF(fmt, ...) \
0e60a699
AG
48 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
49#else
e67137c6 50#define DPRINTF(fmt, ...) \
0e60a699
AG
51 do { } while (0)
52#endif
53
54#define IPA0_DIAG 0x8300
55#define IPA0_SIGP 0xae00
09b99878
CH
56#define IPA0_B2 0xb200
57#define IPA0_B9 0xb900
58#define IPA0_EB 0xeb00
0e60a699 59
1eecf41b
FB
60#define PRIV_B2_SCLP_CALL 0x20
61#define PRIV_B2_CSCH 0x30
62#define PRIV_B2_HSCH 0x31
63#define PRIV_B2_MSCH 0x32
64#define PRIV_B2_SSCH 0x33
65#define PRIV_B2_STSCH 0x34
66#define PRIV_B2_TSCH 0x35
67#define PRIV_B2_TPI 0x36
68#define PRIV_B2_SAL 0x37
69#define PRIV_B2_RSCH 0x38
70#define PRIV_B2_STCRW 0x39
71#define PRIV_B2_STCPS 0x3a
72#define PRIV_B2_RCHP 0x3b
73#define PRIV_B2_SCHM 0x3c
74#define PRIV_B2_CHSC 0x5f
75#define PRIV_B2_SIGA 0x74
76#define PRIV_B2_XSCH 0x76
77
78#define PRIV_EB_SQBS 0x8a
79
80#define PRIV_B9_EQBS 0x9c
81
268846ba 82#define DIAG_IPL 0x308
0e60a699
AG
83#define DIAG_KVM_HYPERCALL 0x500
84#define DIAG_KVM_BREAKPOINT 0x501
85
0e60a699 86#define ICPT_INSTRUCTION 0x04
6449a41a 87#define ICPT_PROGRAM 0x08
a2689242 88#define ICPT_EXT_INT 0x14
0e60a699
AG
89#define ICPT_WAITPSW 0x1c
90#define ICPT_SOFT_INTERCEPT 0x24
91#define ICPT_CPU_STOP 0x28
92#define ICPT_IO 0x40
93
770a6379
DH
94static CPUWatchpoint hw_watchpoint;
95/*
96 * We don't use a list because this structure is also used to transmit the
97 * hardware breakpoints to the kernel.
98 */
99static struct kvm_hw_breakpoint *hw_breakpoints;
100static int nb_hw_breakpoints;
101
94a8d39a
JK
102const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
103 KVM_CAP_LAST_INFO
104};
105
5b08b344 106static int cap_sync_regs;
819bd309 107static int cap_async_pf;
5b08b344 108
575ddeb4 109static void *legacy_s390_alloc(size_t size);
91138037 110
4cb88c3c
DD
111static int kvm_s390_check_clear_cmma(KVMState *s)
112{
113 struct kvm_device_attr attr = {
114 .group = KVM_S390_VM_MEM_CTRL,
115 .attr = KVM_S390_VM_MEM_CLR_CMMA,
116 };
117
118 return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
119}
120
121static int kvm_s390_check_enable_cmma(KVMState *s)
122{
123 struct kvm_device_attr attr = {
124 .group = KVM_S390_VM_MEM_CTRL,
125 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
126 };
127
128 return kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &attr);
129}
130
131void kvm_s390_clear_cmma_callback(void *opaque)
132{
133 int rc;
134 KVMState *s = opaque;
135 struct kvm_device_attr attr = {
136 .group = KVM_S390_VM_MEM_CTRL,
137 .attr = KVM_S390_VM_MEM_CLR_CMMA,
138 };
139
140 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
141 trace_kvm_clear_cmma(rc);
142}
143
144static void kvm_s390_enable_cmma(KVMState *s)
145{
146 int rc;
147 struct kvm_device_attr attr = {
148 .group = KVM_S390_VM_MEM_CTRL,
149 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
150 };
151
152 if (kvm_s390_check_enable_cmma(s) || kvm_s390_check_clear_cmma(s)) {
153 return;
154 }
155
156 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
157 if (!rc) {
158 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
159 }
160 trace_kvm_enable_cmma(rc);
161}
162
cad1e282 163int kvm_arch_init(KVMState *s)
0e60a699 164{
5b08b344 165 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
819bd309 166 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
4cb88c3c
DD
167
168 if (kvm_check_extension(s, KVM_CAP_VM_ATTRIBUTES)) {
169 kvm_s390_enable_cmma(s);
170 }
171
91138037
MA
172 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
173 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
174 phys_mem_set_alloc(legacy_s390_alloc);
175 }
0e60a699
AG
176 return 0;
177}
178
b164e48e
EH
179unsigned long kvm_arch_vcpu_id(CPUState *cpu)
180{
181 return cpu->cpu_index;
182}
183
20d695a9 184int kvm_arch_init_vcpu(CPUState *cpu)
0e60a699 185{
1c9d2a1d
CB
186 /* nothing todo yet */
187 return 0;
0e60a699
AG
188}
189
50a2c6e5 190void kvm_s390_reset_vcpu(S390CPU *cpu)
0e60a699 191{
50a2c6e5
PB
192 CPUState *cs = CPU(cpu);
193
419831d7
AG
194 /* The initial reset call is needed here to reset in-kernel
195 * vcpu data that we can't access directly from QEMU
196 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
197 * Before this ioctl cpu_synchronize_state() is called in common kvm
198 * code (kvm-all) */
50a2c6e5 199 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
70bada03
JF
200 perror("Can't reset vcpu\n");
201 }
0e60a699
AG
202}
203
20d695a9 204int kvm_arch_put_registers(CPUState *cs, int level)
0e60a699 205{
20d695a9
AF
206 S390CPU *cpu = S390_CPU(cs);
207 CPUS390XState *env = &cpu->env;
5b08b344 208 struct kvm_sregs sregs;
0e60a699 209 struct kvm_regs regs;
85ad6230 210 struct kvm_fpu fpu;
860643bc 211 int r;
0e60a699
AG
212 int i;
213
5b08b344 214 /* always save the PSW and the GPRS*/
f7575c96
AF
215 cs->kvm_run->psw_addr = env->psw.addr;
216 cs->kvm_run->psw_mask = env->psw.mask;
0e60a699 217
f7575c96 218 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
5b08b344 219 for (i = 0; i < 16; i++) {
f7575c96
AF
220 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
221 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
5b08b344
CB
222 }
223 } else {
224 for (i = 0; i < 16; i++) {
225 regs.gprs[i] = env->regs[i];
226 }
860643bc
CB
227 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
228 if (r < 0) {
229 return r;
5b08b344 230 }
0e60a699
AG
231 }
232
85ad6230
JH
233 /* Floating point */
234 for (i = 0; i < 16; i++) {
235 fpu.fprs[i] = env->fregs[i].ll;
236 }
237 fpu.fpc = env->fpc;
238
239 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
240 if (r < 0) {
241 return r;
242 }
243
44c68de0
DD
244 /* Do we need to save more than that? */
245 if (level == KVM_PUT_RUNTIME_STATE) {
246 return 0;
247 }
420840e5 248
860643bc
CB
249 /*
250 * These ONE_REGS are not protected by a capability. As they are only
251 * necessary for migration we just trace a possible error, but don't
252 * return with an error return code.
253 */
254 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
255 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
256 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
44b0c0bb
CB
257 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
258 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
0e60a699 259
819bd309 260 if (cap_async_pf) {
860643bc
CB
261 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
262 if (r < 0) {
263 return r;
819bd309 264 }
860643bc
CB
265 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
266 if (r < 0) {
267 return r;
819bd309 268 }
860643bc
CB
269 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
270 if (r < 0) {
271 return r;
819bd309
DD
272 }
273 }
274
5b08b344 275 if (cap_sync_regs &&
f7575c96
AF
276 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
277 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
5b08b344 278 for (i = 0; i < 16; i++) {
f7575c96
AF
279 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
280 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
5b08b344 281 }
f7575c96
AF
282 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
283 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
5b08b344
CB
284 } else {
285 for (i = 0; i < 16; i++) {
286 sregs.acrs[i] = env->aregs[i];
287 sregs.crs[i] = env->cregs[i];
288 }
860643bc
CB
289 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
290 if (r < 0) {
291 return r;
5b08b344
CB
292 }
293 }
0e60a699 294
5b08b344 295 /* Finally the prefix */
f7575c96
AF
296 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
297 cs->kvm_run->s.regs.prefix = env->psa;
298 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
5b08b344
CB
299 } else {
300 /* prefix is only supported via sync regs */
301 }
302 return 0;
0e60a699
AG
303}
304
20d695a9 305int kvm_arch_get_registers(CPUState *cs)
420840e5
JH
306{
307 S390CPU *cpu = S390_CPU(cs);
308 CPUS390XState *env = &cpu->env;
5b08b344 309 struct kvm_sregs sregs;
0e60a699 310 struct kvm_regs regs;
85ad6230 311 struct kvm_fpu fpu;
44c68de0 312 int i, r;
420840e5 313
5b08b344 314 /* get the PSW */
f7575c96
AF
315 env->psw.addr = cs->kvm_run->psw_addr;
316 env->psw.mask = cs->kvm_run->psw_mask;
5b08b344
CB
317
318 /* the GPRS */
f7575c96 319 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
5b08b344 320 for (i = 0; i < 16; i++) {
f7575c96 321 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
5b08b344
CB
322 }
323 } else {
44c68de0
DD
324 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
325 if (r < 0) {
326 return r;
5b08b344
CB
327 }
328 for (i = 0; i < 16; i++) {
329 env->regs[i] = regs.gprs[i];
330 }
0e60a699
AG
331 }
332
5b08b344
CB
333 /* The ACRS and CRS */
334 if (cap_sync_regs &&
f7575c96
AF
335 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
336 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
5b08b344 337 for (i = 0; i < 16; i++) {
f7575c96
AF
338 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
339 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
5b08b344
CB
340 }
341 } else {
44c68de0
DD
342 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
343 if (r < 0) {
344 return r;
5b08b344
CB
345 }
346 for (i = 0; i < 16; i++) {
347 env->aregs[i] = sregs.acrs[i];
348 env->cregs[i] = sregs.crs[i];
349 }
0e60a699
AG
350 }
351
85ad6230
JH
352 /* Floating point */
353 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
354 if (r < 0) {
355 return r;
356 }
357 for (i = 0; i < 16; i++) {
358 env->fregs[i].ll = fpu.fprs[i];
359 }
360 env->fpc = fpu.fpc;
361
44c68de0 362 /* The prefix */
f7575c96
AF
363 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
364 env->psa = cs->kvm_run->s.regs.prefix;
5b08b344 365 }
0e60a699 366
860643bc
CB
367 /*
368 * These ONE_REGS are not protected by a capability. As they are only
369 * necessary for migration we just trace a possible error, but don't
370 * return with an error return code.
371 */
372 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
373 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
374 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
44b0c0bb
CB
375 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
376 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
44c68de0 377
819bd309 378 if (cap_async_pf) {
860643bc 379 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
819bd309
DD
380 if (r < 0) {
381 return r;
382 }
860643bc 383 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
819bd309
DD
384 if (r < 0) {
385 return r;
386 }
860643bc 387 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
819bd309
DD
388 if (r < 0) {
389 return r;
390 }
391 }
392
0e60a699
AG
393 return 0;
394}
395
fdec9918
CB
396/*
397 * Legacy layout for s390:
398 * Older S390 KVM requires the topmost vma of the RAM to be
399 * smaller than an system defined value, which is at least 256GB.
400 * Larger systems have larger values. We put the guest between
401 * the end of data segment (system break) and this value. We
402 * use 32GB as a base to have enough room for the system break
403 * to grow. We also have to use MAP parameters that avoid
404 * read-only mapping of guest pages.
405 */
575ddeb4 406static void *legacy_s390_alloc(size_t size)
fdec9918
CB
407{
408 void *mem;
409
410 mem = mmap((void *) 0x800000000ULL, size,
411 PROT_EXEC|PROT_READ|PROT_WRITE,
412 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
39228250 413 return mem == MAP_FAILED ? NULL : mem;
fdec9918
CB
414}
415
8e4e86af
DH
416/* DIAG 501 is used for sw breakpoints */
417static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
418
20d695a9 419int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
0e60a699 420{
0e60a699 421
8e4e86af
DH
422 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
423 sizeof(diag_501), 0) ||
424 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
425 sizeof(diag_501), 1)) {
0e60a699
AG
426 return -EINVAL;
427 }
428 return 0;
429}
430
20d695a9 431int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
0e60a699 432{
8e4e86af 433 uint8_t t[sizeof(diag_501)];
0e60a699 434
8e4e86af 435 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
0e60a699 436 return -EINVAL;
8e4e86af 437 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
0e60a699 438 return -EINVAL;
8e4e86af
DH
439 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
440 sizeof(diag_501), 1)) {
0e60a699
AG
441 return -EINVAL;
442 }
443
444 return 0;
445}
446
770a6379
DH
447static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
448 int len, int type)
449{
450 int n;
451
452 for (n = 0; n < nb_hw_breakpoints; n++) {
453 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
454 (hw_breakpoints[n].len == len || len == -1)) {
455 return &hw_breakpoints[n];
456 }
457 }
458
459 return NULL;
460}
461
462static int insert_hw_breakpoint(target_ulong addr, int len, int type)
463{
464 int size;
465
466 if (find_hw_breakpoint(addr, len, type)) {
467 return -EEXIST;
468 }
469
470 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
471
472 if (!hw_breakpoints) {
473 nb_hw_breakpoints = 0;
474 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
475 } else {
476 hw_breakpoints =
477 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
478 }
479
480 if (!hw_breakpoints) {
481 nb_hw_breakpoints = 0;
482 return -ENOMEM;
483 }
484
485 hw_breakpoints[nb_hw_breakpoints].addr = addr;
486 hw_breakpoints[nb_hw_breakpoints].len = len;
487 hw_breakpoints[nb_hw_breakpoints].type = type;
488
489 nb_hw_breakpoints++;
490
491 return 0;
492}
493
8c012449
DH
494int kvm_arch_insert_hw_breakpoint(target_ulong addr,
495 target_ulong len, int type)
496{
770a6379
DH
497 switch (type) {
498 case GDB_BREAKPOINT_HW:
499 type = KVM_HW_BP;
500 break;
501 case GDB_WATCHPOINT_WRITE:
502 if (len < 1) {
503 return -EINVAL;
504 }
505 type = KVM_HW_WP_WRITE;
506 break;
507 default:
508 return -ENOSYS;
509 }
510 return insert_hw_breakpoint(addr, len, type);
8c012449
DH
511}
512
513int kvm_arch_remove_hw_breakpoint(target_ulong addr,
514 target_ulong len, int type)
515{
770a6379
DH
516 int size;
517 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
518
519 if (bp == NULL) {
520 return -ENOENT;
521 }
522
523 nb_hw_breakpoints--;
524 if (nb_hw_breakpoints > 0) {
525 /*
526 * In order to trim the array, move the last element to the position to
527 * be removed - if necessary.
528 */
529 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
530 *bp = hw_breakpoints[nb_hw_breakpoints];
531 }
532 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
533 hw_breakpoints =
534 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
535 } else {
536 g_free(hw_breakpoints);
537 hw_breakpoints = NULL;
538 }
539
540 return 0;
8c012449
DH
541}
542
543void kvm_arch_remove_all_hw_breakpoints(void)
544{
770a6379
DH
545 nb_hw_breakpoints = 0;
546 g_free(hw_breakpoints);
547 hw_breakpoints = NULL;
8c012449
DH
548}
549
550void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
551{
770a6379
DH
552 int i;
553
554 if (nb_hw_breakpoints > 0) {
555 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
556 dbg->arch.hw_bp = hw_breakpoints;
557
558 for (i = 0; i < nb_hw_breakpoints; ++i) {
559 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
560 hw_breakpoints[i].addr);
561 }
562 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
563 } else {
564 dbg->arch.nr_hw_bp = 0;
565 dbg->arch.hw_bp = NULL;
566 }
8c012449
DH
567}
568
20d695a9 569void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
0e60a699 570{
0e60a699
AG
571}
572
20d695a9 573void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
0e60a699 574{
0e60a699
AG
575}
576
20d695a9 577int kvm_arch_process_async_events(CPUState *cs)
0af691d7 578{
225dc991 579 return cs->halted;
0af691d7
MT
580}
581
66ad0893
CH
582static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
583 struct kvm_s390_interrupt *interrupt)
584{
585 int r = 0;
586
587 interrupt->type = irq->type;
588 switch (irq->type) {
589 case KVM_S390_INT_VIRTIO:
590 interrupt->parm = irq->u.ext.ext_params;
591 /* fall through */
592 case KVM_S390_INT_PFAULT_INIT:
593 case KVM_S390_INT_PFAULT_DONE:
594 interrupt->parm64 = irq->u.ext.ext_params2;
595 break;
596 case KVM_S390_PROGRAM_INT:
597 interrupt->parm = irq->u.pgm.code;
598 break;
599 case KVM_S390_SIGP_SET_PREFIX:
600 interrupt->parm = irq->u.prefix.address;
601 break;
602 case KVM_S390_INT_SERVICE:
603 interrupt->parm = irq->u.ext.ext_params;
604 break;
605 case KVM_S390_MCHK:
606 interrupt->parm = irq->u.mchk.cr14;
607 interrupt->parm64 = irq->u.mchk.mcic;
608 break;
609 case KVM_S390_INT_EXTERNAL_CALL:
610 interrupt->parm = irq->u.extcall.code;
611 break;
612 case KVM_S390_INT_EMERGENCY:
613 interrupt->parm = irq->u.emerg.code;
614 break;
615 case KVM_S390_SIGP_STOP:
616 case KVM_S390_RESTART:
617 break; /* These types have no parameters */
618 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
619 interrupt->parm = irq->u.io.subchannel_id << 16;
620 interrupt->parm |= irq->u.io.subchannel_nr;
621 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
622 interrupt->parm64 |= irq->u.io.io_int_word;
623 break;
624 default:
625 r = -EINVAL;
626 break;
627 }
628 return r;
629}
630
631void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
632{
633 struct kvm_s390_interrupt kvmint = {};
634 CPUState *cs = CPU(cpu);
635 int r;
636
637 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
638 if (r < 0) {
639 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
640 exit(1);
641 }
642
643 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
644 if (r < 0) {
645 fprintf(stderr, "KVM failed to inject interrupt\n");
646 exit(1);
647 }
648}
649
bbd8bb8e 650static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
66ad0893
CH
651{
652 struct kvm_s390_interrupt kvmint = {};
653 int r;
654
655 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
656 if (r < 0) {
657 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
658 exit(1);
659 }
660
661 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
662 if (r < 0) {
663 fprintf(stderr, "KVM failed to inject interrupt\n");
664 exit(1);
665 }
666}
667
bbd8bb8e
CH
668void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
669{
670 static bool use_flic = true;
671 int r;
672
673 if (use_flic) {
674 r = kvm_s390_inject_flic(irq);
675 if (r == -ENOSYS) {
676 use_flic = false;
677 }
678 if (!r) {
679 return;
680 }
681 }
682 __kvm_s390_floating_interrupt(irq);
683}
684
de13d216 685void kvm_s390_virtio_irq(int config_change, uint64_t token)
0e60a699 686{
de13d216
CH
687 struct kvm_s390_irq irq = {
688 .type = KVM_S390_INT_VIRTIO,
689 .u.ext.ext_params = config_change,
690 .u.ext.ext_params2 = token,
691 };
0e60a699 692
de13d216 693 kvm_s390_floating_interrupt(&irq);
0e60a699
AG
694}
695
de13d216 696void kvm_s390_service_interrupt(uint32_t parm)
0e60a699 697{
de13d216
CH
698 struct kvm_s390_irq irq = {
699 .type = KVM_S390_INT_SERVICE,
700 .u.ext.ext_params = parm,
701 };
0e60a699 702
de13d216 703 kvm_s390_floating_interrupt(&irq);
79afc36d
CH
704}
705
1bc22652 706static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
0e60a699 707{
de13d216
CH
708 struct kvm_s390_irq irq = {
709 .type = KVM_S390_PROGRAM_INT,
710 .u.pgm.code = code,
711 };
712
713 kvm_s390_vcpu_interrupt(cpu, &irq);
0e60a699
AG
714}
715
1bc22652 716static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
bcec36ea 717 uint16_t ipbh0)
0e60a699 718{
1bc22652 719 CPUS390XState *env = &cpu->env;
a0fa2cb8
TH
720 uint64_t sccb;
721 uint32_t code;
0e60a699
AG
722 int r = 0;
723
cb446eca 724 cpu_synchronize_state(CPU(cpu));
0e60a699
AG
725 sccb = env->regs[ipbh0 & 0xf];
726 code = env->regs[(ipbh0 & 0xf0) >> 4];
727
6e252802 728 r = sclp_service_call(env, sccb, code);
9abf567d 729 if (r < 0) {
1bc22652 730 enter_pgmcheck(cpu, -r);
e8803d93
TH
731 } else {
732 setcc(cpu, r);
0e60a699 733 }
81f7c56c 734
0e60a699
AG
735 return 0;
736}
737
1eecf41b 738static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
09b99878 739{
09b99878 740 CPUS390XState *env = &cpu->env;
1eecf41b
FB
741 int rc = 0;
742 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
3474b679 743
44c68de0 744 cpu_synchronize_state(CPU(cpu));
3474b679 745
09b99878 746 switch (ipa1) {
1eecf41b 747 case PRIV_B2_XSCH:
5d9bf1c0 748 ioinst_handle_xsch(cpu, env->regs[1]);
09b99878 749 break;
1eecf41b 750 case PRIV_B2_CSCH:
5d9bf1c0 751 ioinst_handle_csch(cpu, env->regs[1]);
09b99878 752 break;
1eecf41b 753 case PRIV_B2_HSCH:
5d9bf1c0 754 ioinst_handle_hsch(cpu, env->regs[1]);
09b99878 755 break;
1eecf41b 756 case PRIV_B2_MSCH:
5d9bf1c0 757 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 758 break;
1eecf41b 759 case PRIV_B2_SSCH:
5d9bf1c0 760 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 761 break;
1eecf41b 762 case PRIV_B2_STCRW:
5d9bf1c0 763 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
09b99878 764 break;
1eecf41b 765 case PRIV_B2_STSCH:
5d9bf1c0 766 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 767 break;
1eecf41b 768 case PRIV_B2_TSCH:
09b99878
CH
769 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
770 fprintf(stderr, "Spurious tsch intercept\n");
771 break;
1eecf41b 772 case PRIV_B2_CHSC:
5d9bf1c0 773 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
09b99878 774 break;
1eecf41b 775 case PRIV_B2_TPI:
09b99878
CH
776 /* This should have been handled by kvm already. */
777 fprintf(stderr, "Spurious tpi intercept\n");
778 break;
1eecf41b 779 case PRIV_B2_SCHM:
5d9bf1c0
TH
780 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
781 run->s390_sieic.ipb);
09b99878 782 break;
1eecf41b 783 case PRIV_B2_RSCH:
5d9bf1c0 784 ioinst_handle_rsch(cpu, env->regs[1]);
09b99878 785 break;
1eecf41b 786 case PRIV_B2_RCHP:
5d9bf1c0 787 ioinst_handle_rchp(cpu, env->regs[1]);
09b99878 788 break;
1eecf41b 789 case PRIV_B2_STCPS:
09b99878 790 /* We do not provide this instruction, it is suppressed. */
09b99878 791 break;
1eecf41b 792 case PRIV_B2_SAL:
5d9bf1c0 793 ioinst_handle_sal(cpu, env->regs[1]);
09b99878 794 break;
1eecf41b 795 case PRIV_B2_SIGA:
c1e8dfb5 796 /* Not provided, set CC = 3 for subchannel not operational */
5d9bf1c0 797 setcc(cpu, 3);
09b99878 798 break;
1eecf41b
FB
799 case PRIV_B2_SCLP_CALL:
800 rc = kvm_sclp_service_call(cpu, run, ipbh0);
801 break;
c1e8dfb5 802 default:
1eecf41b
FB
803 rc = -1;
804 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
805 break;
09b99878
CH
806 }
807
1eecf41b 808 return rc;
09b99878
CH
809}
810
1eecf41b 811static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
0e60a699
AG
812{
813 int r = 0;
0e60a699 814
0e60a699 815 switch (ipa1) {
1eecf41b
FB
816 case PRIV_B9_EQBS:
817 /* just inject exception */
818 r = -1;
819 break;
820 default:
821 r = -1;
822 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
823 break;
824 }
825
826 return r;
827}
828
829static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
830{
831 int r = 0;
832
833 switch (ipa1) {
834 case PRIV_EB_SQBS:
835 /* just inject exception */
836 r = -1;
837 break;
838 default:
839 r = -1;
840 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipa1);
841 break;
0e60a699
AG
842 }
843
844 return r;
845}
846
4fd6dd06 847static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
0e60a699 848{
4fd6dd06 849 CPUS390XState *env = &cpu->env;
77319f22 850 int ret;
3474b679 851
44c68de0 852 cpu_synchronize_state(CPU(cpu));
77319f22
TH
853 ret = s390_virtio_hypercall(env);
854 if (ret == -EINVAL) {
855 enter_pgmcheck(cpu, PGM_SPECIFICATION);
856 return 0;
857 }
0e60a699 858
77319f22 859 return ret;
0e60a699
AG
860}
861
268846ba
ED
862static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
863{
864 uint64_t r1, r3;
865
866 cpu_synchronize_state(CPU(cpu));
867 r1 = (run->s390_sieic.ipa & 0x00f0) >> 8;
868 r3 = run->s390_sieic.ipa & 0x000f;
869 handle_diag_308(&cpu->env, r1, r3);
870}
871
b30f4dfb
DH
872static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
873{
874 CPUS390XState *env = &cpu->env;
875 unsigned long pc;
876
877 cpu_synchronize_state(CPU(cpu));
878
879 pc = env->psw.addr - 4;
880 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
881 env->psw.addr = pc;
882 return EXCP_DEBUG;
883 }
884
885 return -ENOENT;
886}
887
638129ff
CH
888#define DIAG_KVM_CODE_MASK 0x000000000000ffff
889
890static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
0e60a699
AG
891{
892 int r = 0;
638129ff
CH
893 uint16_t func_code;
894
895 /*
896 * For any diagnose call we support, bits 48-63 of the resulting
897 * address specify the function code; the remainder is ignored.
898 */
899 func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
900 switch (func_code) {
268846ba
ED
901 case DIAG_IPL:
902 kvm_handle_diag_308(cpu, run);
903 break;
39fbc5c6
CB
904 case DIAG_KVM_HYPERCALL:
905 r = handle_hypercall(cpu, run);
906 break;
907 case DIAG_KVM_BREAKPOINT:
b30f4dfb 908 r = handle_sw_breakpoint(cpu, run);
39fbc5c6
CB
909 break;
910 default:
638129ff 911 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
39fbc5c6
CB
912 r = -1;
913 break;
0e60a699
AG
914 }
915
916 return r;
917}
918
6e6ad8db 919static void sigp_cpu_start(void *arg)
b20a461f 920{
6e6ad8db
DH
921 CPUState *cs = arg;
922 S390CPU *cpu = S390_CPU(cs);
923
eb24f7c6 924 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
b20a461f 925 DPRINTF("DONE: KVM cpu start: %p\n", &cpu->env);
b20a461f
TH
926}
927
6e6ad8db 928static void sigp_cpu_restart(void *arg)
0e60a699 929{
6e6ad8db
DH
930 CPUState *cs = arg;
931 S390CPU *cpu = S390_CPU(cs);
de13d216
CH
932 struct kvm_s390_irq irq = {
933 .type = KVM_S390_RESTART,
934 };
935
936 kvm_s390_vcpu_interrupt(cpu, &irq);
eb24f7c6 937 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
6e6ad8db
DH
938}
939
940int kvm_s390_cpu_restart(S390CPU *cpu)
941{
942 run_on_cpu(CPU(cpu), sigp_cpu_restart, CPU(cpu));
7f7f9752 943 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
0e60a699
AG
944 return 0;
945}
946
f7d3e466 947static void sigp_initial_cpu_reset(void *arg)
0e60a699 948{
f7d3e466
TH
949 CPUState *cpu = arg;
950 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
d5900813 951
f7d3e466
TH
952 cpu_synchronize_state(cpu);
953 scc->initial_cpu_reset(cpu);
0e60a699
AG
954}
955
04c2b516
TH
956static void sigp_cpu_reset(void *arg)
957{
958 CPUState *cpu = arg;
959 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
960
961 cpu_synchronize_state(cpu);
962 scc->cpu_reset(cpu);
963}
964
b8031adb
TH
965#define SIGP_ORDER_MASK 0x000000ff
966
f7575c96 967static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
0e60a699 968{
f7575c96 969 CPUS390XState *env = &cpu->env;
0e60a699 970 uint8_t order_code;
0e60a699 971 uint16_t cpu_addr;
45fa769b 972 S390CPU *target_cpu;
3796f0e1
TH
973 uint64_t *statusreg = &env->regs[ipa1 >> 4];
974 int cc;
0e60a699 975
cb446eca 976 cpu_synchronize_state(CPU(cpu));
0e60a699
AG
977
978 /* get order code */
b8031adb 979 order_code = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
0e60a699 980
0e60a699 981 cpu_addr = env->regs[ipa1 & 0x0f];
45fa769b
AF
982 target_cpu = s390_cpu_addr2state(cpu_addr);
983 if (target_cpu == NULL) {
3796f0e1 984 cc = 3; /* not operational */
0e60a699
AG
985 goto out;
986 }
987
988 switch (order_code) {
b20a461f 989 case SIGP_START:
6e6ad8db
DH
990 run_on_cpu(CPU(target_cpu), sigp_cpu_start, CPU(target_cpu));
991 cc = 0;
b20a461f 992 break;
0b9972a2 993 case SIGP_RESTART:
6e6ad8db
DH
994 run_on_cpu(CPU(target_cpu), sigp_cpu_restart, CPU(target_cpu));
995 cc = 0;
0b9972a2
TH
996 break;
997 case SIGP_SET_ARCH:
0788082a
TH
998 *statusreg &= 0xffffffff00000000UL;
999 *statusreg |= SIGP_STAT_INVALID_PARAMETER;
1000 cc = 1; /* status stored */
1001 break;
0b9972a2 1002 case SIGP_INITIAL_CPU_RESET:
f7d3e466
TH
1003 run_on_cpu(CPU(target_cpu), sigp_initial_cpu_reset, CPU(target_cpu));
1004 cc = 0;
0b9972a2 1005 break;
04c2b516
TH
1006 case SIGP_CPU_RESET:
1007 run_on_cpu(CPU(target_cpu), sigp_cpu_reset, CPU(target_cpu));
1008 cc = 0;
1009 break;
0b9972a2 1010 default:
3796f0e1
TH
1011 DPRINTF("KVM: unknown SIGP: 0x%x\n", order_code);
1012 *statusreg &= 0xffffffff00000000UL;
1013 *statusreg |= SIGP_STAT_INVALID_ORDER;
1014 cc = 1; /* status stored */
0b9972a2 1015 break;
0e60a699
AG
1016 }
1017
1018out:
3796f0e1 1019 setcc(cpu, cc);
0e60a699
AG
1020 return 0;
1021}
1022
b30f4dfb 1023static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
0e60a699
AG
1024{
1025 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1026 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
d7963c43 1027 int r = -1;
0e60a699 1028
e67137c6
PM
1029 DPRINTF("handle_instruction 0x%x 0x%x\n",
1030 run->s390_sieic.ipa, run->s390_sieic.ipb);
0e60a699 1031 switch (ipa0) {
09b99878 1032 case IPA0_B2:
1eecf41b
FB
1033 r = handle_b2(cpu, run, ipa1);
1034 break;
09b99878 1035 case IPA0_B9:
1eecf41b
FB
1036 r = handle_b9(cpu, run, ipa1);
1037 break;
09b99878 1038 case IPA0_EB:
1eecf41b 1039 r = handle_eb(cpu, run, ipa1);
09b99878
CH
1040 break;
1041 case IPA0_DIAG:
638129ff 1042 r = handle_diag(cpu, run, run->s390_sieic.ipb);
09b99878
CH
1043 break;
1044 case IPA0_SIGP:
1045 r = handle_sigp(cpu, run, ipa1);
1046 break;
0e60a699
AG
1047 }
1048
1049 if (r < 0) {
b30f4dfb 1050 r = 0;
1bc22652 1051 enter_pgmcheck(cpu, 0x0001);
0e60a699 1052 }
b30f4dfb
DH
1053
1054 return r;
0e60a699
AG
1055}
1056
f7575c96 1057static bool is_special_wait_psw(CPUState *cs)
eca3ed03
CB
1058{
1059 /* signal quiesce */
f7575c96 1060 return cs->kvm_run->psw_addr == 0xfffUL;
eca3ed03
CB
1061}
1062
a2689242
TH
1063static void guest_panicked(void)
1064{
3a449690
WX
1065 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1066 &error_abort);
a2689242
TH
1067 vm_stop(RUN_STATE_GUEST_PANICKED);
1068}
1069
1070static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1071{
1072 CPUState *cs = CPU(cpu);
1073
1074 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1075 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1076 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
eb24f7c6 1077 s390_cpu_halt(cpu);
a2689242
TH
1078 guest_panicked();
1079}
1080
1bc22652 1081static int handle_intercept(S390CPU *cpu)
0e60a699 1082{
f7575c96
AF
1083 CPUState *cs = CPU(cpu);
1084 struct kvm_run *run = cs->kvm_run;
0e60a699
AG
1085 int icpt_code = run->s390_sieic.icptcode;
1086 int r = 0;
1087
e67137c6 1088 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
f7575c96 1089 (long)cs->kvm_run->psw_addr);
0e60a699
AG
1090 switch (icpt_code) {
1091 case ICPT_INSTRUCTION:
b30f4dfb 1092 r = handle_instruction(cpu, run);
0e60a699 1093 break;
6449a41a
TH
1094 case ICPT_PROGRAM:
1095 unmanageable_intercept(cpu, "program interrupt",
1096 offsetof(LowCore, program_new_psw));
1097 r = EXCP_HALTED;
1098 break;
a2689242
TH
1099 case ICPT_EXT_INT:
1100 unmanageable_intercept(cpu, "external interrupt",
1101 offsetof(LowCore, external_new_psw));
1102 r = EXCP_HALTED;
1103 break;
0e60a699 1104 case ICPT_WAITPSW:
08eb8c85 1105 /* disabled wait, since enabled wait is handled in kernel */
eb24f7c6
DH
1106 cpu_synchronize_state(cs);
1107 if (s390_cpu_halt(cpu) == 0) {
08eb8c85
CB
1108 if (is_special_wait_psw(cs)) {
1109 qemu_system_shutdown_request();
1110 } else {
a2689242 1111 guest_panicked();
08eb8c85 1112 }
eca3ed03
CB
1113 }
1114 r = EXCP_HALTED;
1115 break;
854e42f3 1116 case ICPT_CPU_STOP:
eb24f7c6 1117 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
854e42f3
CB
1118 qemu_system_shutdown_request();
1119 }
1120 r = EXCP_HALTED;
0e60a699
AG
1121 break;
1122 case ICPT_SOFT_INTERCEPT:
1123 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1124 exit(1);
1125 break;
0e60a699
AG
1126 case ICPT_IO:
1127 fprintf(stderr, "KVM unimplemented icpt IO\n");
1128 exit(1);
1129 break;
1130 default:
1131 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1132 exit(1);
1133 break;
1134 }
1135
1136 return r;
1137}
1138
09b99878
CH
1139static int handle_tsch(S390CPU *cpu)
1140{
1141 CPUS390XState *env = &cpu->env;
1142 CPUState *cs = CPU(cpu);
1143 struct kvm_run *run = cs->kvm_run;
1144 int ret;
1145
44c68de0 1146 cpu_synchronize_state(cs);
3474b679 1147
09b99878
CH
1148 ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
1149 if (ret >= 0) {
1150 /* Success; set condition code. */
1151 setcc(cpu, ret);
1152 ret = 0;
1153 } else if (ret < -1) {
1154 /*
1155 * Failure.
1156 * If an I/O interrupt had been dequeued, we have to reinject it.
1157 */
1158 if (run->s390_tsch.dequeued) {
de13d216
CH
1159 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1160 run->s390_tsch.subchannel_nr,
1161 run->s390_tsch.io_int_parm,
1162 run->s390_tsch.io_int_word);
09b99878
CH
1163 }
1164 ret = 0;
1165 }
1166 return ret;
1167}
1168
8c012449
DH
1169static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1170{
770a6379
DH
1171 CPUState *cs = CPU(cpu);
1172 struct kvm_run *run = cs->kvm_run;
1173
1174 int ret = 0;
1175 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1176
1177 switch (arch_info->type) {
1178 case KVM_HW_WP_WRITE:
1179 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1180 cs->watchpoint_hit = &hw_watchpoint;
1181 hw_watchpoint.vaddr = arch_info->addr;
1182 hw_watchpoint.flags = BP_MEM_WRITE;
1183 ret = EXCP_DEBUG;
1184 }
1185 break;
1186 case KVM_HW_BP:
1187 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1188 ret = EXCP_DEBUG;
1189 }
1190 break;
1191 case KVM_SINGLESTEP:
1192 if (cs->singlestep_enabled) {
1193 ret = EXCP_DEBUG;
1194 }
1195 break;
1196 default:
1197 ret = -ENOSYS;
1198 }
1199
1200 return ret;
8c012449
DH
1201}
1202
20d695a9 1203int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
0e60a699 1204{
20d695a9 1205 S390CPU *cpu = S390_CPU(cs);
0e60a699
AG
1206 int ret = 0;
1207
1208 switch (run->exit_reason) {
1209 case KVM_EXIT_S390_SIEIC:
1bc22652 1210 ret = handle_intercept(cpu);
0e60a699
AG
1211 break;
1212 case KVM_EXIT_S390_RESET:
add142e0 1213 qemu_system_reset_request();
0e60a699 1214 break;
09b99878
CH
1215 case KVM_EXIT_S390_TSCH:
1216 ret = handle_tsch(cpu);
1217 break;
8c012449
DH
1218 case KVM_EXIT_DEBUG:
1219 ret = kvm_arch_handle_debug_exit(cpu);
1220 break;
0e60a699
AG
1221 default:
1222 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1223 break;
1224 }
1225
bb4ea393
JK
1226 if (ret == 0) {
1227 ret = EXCP_INTERRUPT;
bb4ea393 1228 }
0e60a699
AG
1229 return ret;
1230}
4513d923 1231
20d695a9 1232bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
4513d923
GN
1233{
1234 return true;
1235}
a1b87fe0 1236
20d695a9 1237int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
a1b87fe0
JK
1238{
1239 return 1;
1240}
1241
1242int kvm_arch_on_sigbus(int code, void *addr)
1243{
1244 return 1;
1245}
09b99878 1246
de13d216 1247void kvm_s390_io_interrupt(uint16_t subchannel_id,
09b99878
CH
1248 uint16_t subchannel_nr, uint32_t io_int_parm,
1249 uint32_t io_int_word)
1250{
de13d216
CH
1251 struct kvm_s390_irq irq = {
1252 .u.io.subchannel_id = subchannel_id,
1253 .u.io.subchannel_nr = subchannel_nr,
1254 .u.io.io_int_parm = io_int_parm,
1255 .u.io.io_int_word = io_int_word,
1256 };
09b99878 1257
7e749462 1258 if (io_int_word & IO_INT_WORD_AI) {
de13d216 1259 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
7e749462 1260 } else {
de13d216 1261 irq.type = ((subchannel_id & 0xff00) << 24) |
7e749462
CH
1262 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
1263 }
de13d216 1264 kvm_s390_floating_interrupt(&irq);
09b99878
CH
1265}
1266
de13d216 1267void kvm_s390_crw_mchk(void)
09b99878 1268{
de13d216
CH
1269 struct kvm_s390_irq irq = {
1270 .type = KVM_S390_MCHK,
1271 .u.mchk.cr14 = 1 << 28,
1272 .u.mchk.mcic = 0x00400f1d40330000,
1273 };
1274 kvm_s390_floating_interrupt(&irq);
09b99878
CH
1275}
1276
1277void kvm_s390_enable_css_support(S390CPU *cpu)
1278{
09b99878
CH
1279 int r;
1280
1281 /* Activate host kernel channel subsystem support. */
e080f0fd 1282 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
09b99878
CH
1283 assert(r == 0);
1284}
48475e14
AK
1285
1286void kvm_arch_init_irq_routing(KVMState *s)
1287{
d426d9fb
CH
1288 /*
1289 * Note that while irqchip capabilities generally imply that cpustates
1290 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1291 * have to override the common code kvm_halt_in_kernel_allowed setting.
1292 */
1293 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1294 kvm_irqfds_allowed = true;
1295 kvm_gsi_routing_allowed = true;
1296 kvm_halt_in_kernel_allowed = false;
1297 }
48475e14 1298}
b4436a0b 1299
cc3ac9c4
CH
1300int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1301 int vq, bool assign)
b4436a0b
CH
1302{
1303 struct kvm_ioeventfd kick = {
1304 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1305 KVM_IOEVENTFD_FLAG_DATAMATCH,
cc3ac9c4 1306 .fd = event_notifier_get_fd(notifier),
b4436a0b
CH
1307 .datamatch = vq,
1308 .addr = sch,
1309 .len = 8,
1310 };
1311 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1312 return -ENOSYS;
1313 }
1314 if (!assign) {
1315 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1316 }
1317 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1318}
1def6656
MR
1319
1320int kvm_s390_get_memslot_count(KVMState *s)
1321{
1322 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
1323}