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