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