]> git.proxmox.com Git - mirror_qemu.git/blame - target-s390x/kvm.c
s390x/kvm: software breakpoint support
[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"
0e60a699 35#include "cpu.h"
9c17d615 36#include "sysemu/device_tree.h"
08eb8c85
CB
37#include "qapi/qmp/qjson.h"
38#include "monitor/monitor.h"
860643bc 39#include "trace.h"
0e60a699
AG
40
41/* #define DEBUG_KVM */
42
43#ifdef DEBUG_KVM
e67137c6 44#define DPRINTF(fmt, ...) \
0e60a699
AG
45 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
46#else
e67137c6 47#define DPRINTF(fmt, ...) \
0e60a699
AG
48 do { } while (0)
49#endif
50
51#define IPA0_DIAG 0x8300
52#define IPA0_SIGP 0xae00
09b99878
CH
53#define IPA0_B2 0xb200
54#define IPA0_B9 0xb900
55#define IPA0_EB 0xeb00
0e60a699 56
1eecf41b
FB
57#define PRIV_B2_SCLP_CALL 0x20
58#define PRIV_B2_CSCH 0x30
59#define PRIV_B2_HSCH 0x31
60#define PRIV_B2_MSCH 0x32
61#define PRIV_B2_SSCH 0x33
62#define PRIV_B2_STSCH 0x34
63#define PRIV_B2_TSCH 0x35
64#define PRIV_B2_TPI 0x36
65#define PRIV_B2_SAL 0x37
66#define PRIV_B2_RSCH 0x38
67#define PRIV_B2_STCRW 0x39
68#define PRIV_B2_STCPS 0x3a
69#define PRIV_B2_RCHP 0x3b
70#define PRIV_B2_SCHM 0x3c
71#define PRIV_B2_CHSC 0x5f
72#define PRIV_B2_SIGA 0x74
73#define PRIV_B2_XSCH 0x76
74
75#define PRIV_EB_SQBS 0x8a
76
77#define PRIV_B9_EQBS 0x9c
78
268846ba 79#define DIAG_IPL 0x308
0e60a699
AG
80#define DIAG_KVM_HYPERCALL 0x500
81#define DIAG_KVM_BREAKPOINT 0x501
82
0e60a699
AG
83#define ICPT_INSTRUCTION 0x04
84#define ICPT_WAITPSW 0x1c
85#define ICPT_SOFT_INTERCEPT 0x24
86#define ICPT_CPU_STOP 0x28
87#define ICPT_IO 0x40
88
94a8d39a
JK
89const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
90 KVM_CAP_LAST_INFO
91};
92
5b08b344 93static int cap_sync_regs;
819bd309 94static int cap_async_pf;
5b08b344 95
575ddeb4 96static void *legacy_s390_alloc(size_t size);
91138037 97
cad1e282 98int kvm_arch_init(KVMState *s)
0e60a699 99{
5b08b344 100 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
819bd309 101 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
91138037
MA
102 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
103 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
104 phys_mem_set_alloc(legacy_s390_alloc);
105 }
0e60a699
AG
106 return 0;
107}
108
b164e48e
EH
109unsigned long kvm_arch_vcpu_id(CPUState *cpu)
110{
111 return cpu->cpu_index;
112}
113
20d695a9 114int kvm_arch_init_vcpu(CPUState *cpu)
0e60a699 115{
1c9d2a1d
CB
116 /* nothing todo yet */
117 return 0;
0e60a699
AG
118}
119
50a2c6e5 120void kvm_s390_reset_vcpu(S390CPU *cpu)
0e60a699 121{
50a2c6e5
PB
122 CPUState *cs = CPU(cpu);
123
419831d7
AG
124 /* The initial reset call is needed here to reset in-kernel
125 * vcpu data that we can't access directly from QEMU
126 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
127 * Before this ioctl cpu_synchronize_state() is called in common kvm
128 * code (kvm-all) */
50a2c6e5 129 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
70bada03
JF
130 perror("Can't reset vcpu\n");
131 }
0e60a699
AG
132}
133
20d695a9 134int kvm_arch_put_registers(CPUState *cs, int level)
0e60a699 135{
20d695a9
AF
136 S390CPU *cpu = S390_CPU(cs);
137 CPUS390XState *env = &cpu->env;
5b08b344 138 struct kvm_sregs sregs;
0e60a699 139 struct kvm_regs regs;
860643bc 140 int r;
0e60a699
AG
141 int i;
142
5b08b344 143 /* always save the PSW and the GPRS*/
f7575c96
AF
144 cs->kvm_run->psw_addr = env->psw.addr;
145 cs->kvm_run->psw_mask = env->psw.mask;
0e60a699 146
f7575c96 147 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
5b08b344 148 for (i = 0; i < 16; i++) {
f7575c96
AF
149 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
150 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
5b08b344
CB
151 }
152 } else {
153 for (i = 0; i < 16; i++) {
154 regs.gprs[i] = env->regs[i];
155 }
860643bc
CB
156 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
157 if (r < 0) {
158 return r;
5b08b344 159 }
0e60a699
AG
160 }
161
44c68de0
DD
162 /* Do we need to save more than that? */
163 if (level == KVM_PUT_RUNTIME_STATE) {
164 return 0;
165 }
420840e5 166
860643bc
CB
167 /*
168 * These ONE_REGS are not protected by a capability. As they are only
169 * necessary for migration we just trace a possible error, but don't
170 * return with an error return code.
171 */
172 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
173 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
174 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
44b0c0bb
CB
175 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
176 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
0e60a699 177
819bd309 178 if (cap_async_pf) {
860643bc
CB
179 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
180 if (r < 0) {
181 return r;
819bd309 182 }
860643bc
CB
183 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
184 if (r < 0) {
185 return r;
819bd309 186 }
860643bc
CB
187 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
188 if (r < 0) {
189 return r;
819bd309
DD
190 }
191 }
192
5b08b344 193 if (cap_sync_regs &&
f7575c96
AF
194 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
195 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
5b08b344 196 for (i = 0; i < 16; i++) {
f7575c96
AF
197 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
198 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
5b08b344 199 }
f7575c96
AF
200 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
201 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
5b08b344
CB
202 } else {
203 for (i = 0; i < 16; i++) {
204 sregs.acrs[i] = env->aregs[i];
205 sregs.crs[i] = env->cregs[i];
206 }
860643bc
CB
207 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
208 if (r < 0) {
209 return r;
5b08b344
CB
210 }
211 }
0e60a699 212
5b08b344 213 /* Finally the prefix */
f7575c96
AF
214 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
215 cs->kvm_run->s.regs.prefix = env->psa;
216 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
5b08b344
CB
217 } else {
218 /* prefix is only supported via sync regs */
219 }
220 return 0;
0e60a699
AG
221}
222
20d695a9 223int kvm_arch_get_registers(CPUState *cs)
420840e5
JH
224{
225 S390CPU *cpu = S390_CPU(cs);
226 CPUS390XState *env = &cpu->env;
5b08b344 227 struct kvm_sregs sregs;
0e60a699 228 struct kvm_regs regs;
44c68de0 229 int i, r;
420840e5 230
5b08b344 231 /* get the PSW */
f7575c96
AF
232 env->psw.addr = cs->kvm_run->psw_addr;
233 env->psw.mask = cs->kvm_run->psw_mask;
5b08b344
CB
234
235 /* the GPRS */
f7575c96 236 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
5b08b344 237 for (i = 0; i < 16; i++) {
f7575c96 238 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
5b08b344
CB
239 }
240 } else {
44c68de0
DD
241 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
242 if (r < 0) {
243 return r;
5b08b344
CB
244 }
245 for (i = 0; i < 16; i++) {
246 env->regs[i] = regs.gprs[i];
247 }
0e60a699
AG
248 }
249
5b08b344
CB
250 /* The ACRS and CRS */
251 if (cap_sync_regs &&
f7575c96
AF
252 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
253 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
5b08b344 254 for (i = 0; i < 16; i++) {
f7575c96
AF
255 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
256 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
5b08b344
CB
257 }
258 } else {
44c68de0
DD
259 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
260 if (r < 0) {
261 return r;
5b08b344
CB
262 }
263 for (i = 0; i < 16; i++) {
264 env->aregs[i] = sregs.acrs[i];
265 env->cregs[i] = sregs.crs[i];
266 }
0e60a699
AG
267 }
268
44c68de0 269 /* The prefix */
f7575c96
AF
270 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
271 env->psa = cs->kvm_run->s.regs.prefix;
5b08b344 272 }
0e60a699 273
860643bc
CB
274 /*
275 * These ONE_REGS are not protected by a capability. As they are only
276 * necessary for migration we just trace a possible error, but don't
277 * return with an error return code.
278 */
279 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
280 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
281 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
44b0c0bb
CB
282 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
283 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
44c68de0 284
819bd309 285 if (cap_async_pf) {
860643bc 286 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
819bd309
DD
287 if (r < 0) {
288 return r;
289 }
860643bc 290 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
819bd309
DD
291 if (r < 0) {
292 return r;
293 }
860643bc 294 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
819bd309
DD
295 if (r < 0) {
296 return r;
297 }
298 }
299
0e60a699
AG
300 return 0;
301}
302
fdec9918
CB
303/*
304 * Legacy layout for s390:
305 * Older S390 KVM requires the topmost vma of the RAM to be
306 * smaller than an system defined value, which is at least 256GB.
307 * Larger systems have larger values. We put the guest between
308 * the end of data segment (system break) and this value. We
309 * use 32GB as a base to have enough room for the system break
310 * to grow. We also have to use MAP parameters that avoid
311 * read-only mapping of guest pages.
312 */
575ddeb4 313static void *legacy_s390_alloc(size_t size)
fdec9918
CB
314{
315 void *mem;
316
317 mem = mmap((void *) 0x800000000ULL, size,
318 PROT_EXEC|PROT_READ|PROT_WRITE,
319 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
39228250 320 return mem == MAP_FAILED ? NULL : mem;
fdec9918
CB
321}
322
8e4e86af
DH
323/* DIAG 501 is used for sw breakpoints */
324static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
325
20d695a9 326int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
0e60a699 327{
0e60a699 328
8e4e86af
DH
329 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
330 sizeof(diag_501), 0) ||
331 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
332 sizeof(diag_501), 1)) {
0e60a699
AG
333 return -EINVAL;
334 }
335 return 0;
336}
337
20d695a9 338int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
0e60a699 339{
8e4e86af 340 uint8_t t[sizeof(diag_501)];
0e60a699 341
8e4e86af 342 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
0e60a699 343 return -EINVAL;
8e4e86af 344 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
0e60a699 345 return -EINVAL;
8e4e86af
DH
346 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
347 sizeof(diag_501), 1)) {
0e60a699
AG
348 return -EINVAL;
349 }
350
351 return 0;
352}
353
8c012449
DH
354int kvm_arch_insert_hw_breakpoint(target_ulong addr,
355 target_ulong len, int type)
356{
357 return -ENOSYS;
358}
359
360int kvm_arch_remove_hw_breakpoint(target_ulong addr,
361 target_ulong len, int type)
362{
363 return -ENOSYS;
364}
365
366void kvm_arch_remove_all_hw_breakpoints(void)
367{
368}
369
370void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
371{
372}
373
20d695a9 374void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
0e60a699 375{
0e60a699
AG
376}
377
20d695a9 378void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
0e60a699 379{
0e60a699
AG
380}
381
20d695a9 382int kvm_arch_process_async_events(CPUState *cs)
0af691d7 383{
225dc991 384 return cs->halted;
0af691d7
MT
385}
386
1bc22652 387void kvm_s390_interrupt_internal(S390CPU *cpu, int type, uint32_t parm,
bcec36ea 388 uint64_t parm64, int vm)
0e60a699 389{
1bc22652 390 CPUState *cs = CPU(cpu);
0e60a699
AG
391 struct kvm_s390_interrupt kvmint;
392 int r;
393
a60f24b5 394 if (!cs->kvm_state) {
0e60a699
AG
395 return;
396 }
397
0e60a699
AG
398 kvmint.type = type;
399 kvmint.parm = parm;
400 kvmint.parm64 = parm64;
401
402 if (vm) {
a60f24b5 403 r = kvm_vm_ioctl(cs->kvm_state, KVM_S390_INTERRUPT, &kvmint);
0e60a699 404 } else {
1bc22652 405 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
0e60a699
AG
406 }
407
408 if (r < 0) {
409 fprintf(stderr, "KVM failed to inject interrupt\n");
410 exit(1);
411 }
412}
413
1bc22652 414void kvm_s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token)
0e60a699 415{
1bc22652 416 kvm_s390_interrupt_internal(cpu, KVM_S390_INT_VIRTIO, config_change,
0e60a699
AG
417 token, 1);
418}
419
1bc22652 420void kvm_s390_interrupt(S390CPU *cpu, int type, uint32_t code)
0e60a699 421{
1bc22652 422 kvm_s390_interrupt_internal(cpu, type, code, 0, 0);
0e60a699
AG
423}
424
1bc22652 425static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
0e60a699 426{
1bc22652 427 kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code);
0e60a699
AG
428}
429
1bc22652 430static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
bcec36ea 431 uint16_t ipbh0)
0e60a699 432{
1bc22652 433 CPUS390XState *env = &cpu->env;
a0fa2cb8
TH
434 uint64_t sccb;
435 uint32_t code;
0e60a699
AG
436 int r = 0;
437
cb446eca 438 cpu_synchronize_state(CPU(cpu));
0e60a699
AG
439 sccb = env->regs[ipbh0 & 0xf];
440 code = env->regs[(ipbh0 & 0xf0) >> 4];
441
6e252802 442 r = sclp_service_call(env, sccb, code);
9abf567d 443 if (r < 0) {
1bc22652 444 enter_pgmcheck(cpu, -r);
e8803d93
TH
445 } else {
446 setcc(cpu, r);
0e60a699 447 }
81f7c56c 448
0e60a699
AG
449 return 0;
450}
451
1eecf41b 452static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
09b99878 453{
09b99878 454 CPUS390XState *env = &cpu->env;
1eecf41b
FB
455 int rc = 0;
456 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
3474b679 457
44c68de0 458 cpu_synchronize_state(CPU(cpu));
3474b679 459
09b99878 460 switch (ipa1) {
1eecf41b 461 case PRIV_B2_XSCH:
5d9bf1c0 462 ioinst_handle_xsch(cpu, env->regs[1]);
09b99878 463 break;
1eecf41b 464 case PRIV_B2_CSCH:
5d9bf1c0 465 ioinst_handle_csch(cpu, env->regs[1]);
09b99878 466 break;
1eecf41b 467 case PRIV_B2_HSCH:
5d9bf1c0 468 ioinst_handle_hsch(cpu, env->regs[1]);
09b99878 469 break;
1eecf41b 470 case PRIV_B2_MSCH:
5d9bf1c0 471 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 472 break;
1eecf41b 473 case PRIV_B2_SSCH:
5d9bf1c0 474 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 475 break;
1eecf41b 476 case PRIV_B2_STCRW:
5d9bf1c0 477 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
09b99878 478 break;
1eecf41b 479 case PRIV_B2_STSCH:
5d9bf1c0 480 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 481 break;
1eecf41b 482 case PRIV_B2_TSCH:
09b99878
CH
483 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
484 fprintf(stderr, "Spurious tsch intercept\n");
485 break;
1eecf41b 486 case PRIV_B2_CHSC:
5d9bf1c0 487 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
09b99878 488 break;
1eecf41b 489 case PRIV_B2_TPI:
09b99878
CH
490 /* This should have been handled by kvm already. */
491 fprintf(stderr, "Spurious tpi intercept\n");
492 break;
1eecf41b 493 case PRIV_B2_SCHM:
5d9bf1c0
TH
494 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
495 run->s390_sieic.ipb);
09b99878 496 break;
1eecf41b 497 case PRIV_B2_RSCH:
5d9bf1c0 498 ioinst_handle_rsch(cpu, env->regs[1]);
09b99878 499 break;
1eecf41b 500 case PRIV_B2_RCHP:
5d9bf1c0 501 ioinst_handle_rchp(cpu, env->regs[1]);
09b99878 502 break;
1eecf41b 503 case PRIV_B2_STCPS:
09b99878 504 /* We do not provide this instruction, it is suppressed. */
09b99878 505 break;
1eecf41b 506 case PRIV_B2_SAL:
5d9bf1c0 507 ioinst_handle_sal(cpu, env->regs[1]);
09b99878 508 break;
1eecf41b 509 case PRIV_B2_SIGA:
c1e8dfb5 510 /* Not provided, set CC = 3 for subchannel not operational */
5d9bf1c0 511 setcc(cpu, 3);
09b99878 512 break;
1eecf41b
FB
513 case PRIV_B2_SCLP_CALL:
514 rc = kvm_sclp_service_call(cpu, run, ipbh0);
515 break;
c1e8dfb5 516 default:
1eecf41b
FB
517 rc = -1;
518 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
519 break;
09b99878
CH
520 }
521
1eecf41b 522 return rc;
09b99878
CH
523}
524
1eecf41b 525static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
0e60a699
AG
526{
527 int r = 0;
0e60a699 528
0e60a699 529 switch (ipa1) {
1eecf41b
FB
530 case PRIV_B9_EQBS:
531 /* just inject exception */
532 r = -1;
533 break;
534 default:
535 r = -1;
536 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
537 break;
538 }
539
540 return r;
541}
542
543static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
544{
545 int r = 0;
546
547 switch (ipa1) {
548 case PRIV_EB_SQBS:
549 /* just inject exception */
550 r = -1;
551 break;
552 default:
553 r = -1;
554 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipa1);
555 break;
0e60a699
AG
556 }
557
558 return r;
559}
560
4fd6dd06 561static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
0e60a699 562{
4fd6dd06 563 CPUS390XState *env = &cpu->env;
77319f22 564 int ret;
3474b679 565
44c68de0 566 cpu_synchronize_state(CPU(cpu));
77319f22
TH
567 ret = s390_virtio_hypercall(env);
568 if (ret == -EINVAL) {
569 enter_pgmcheck(cpu, PGM_SPECIFICATION);
570 return 0;
571 }
0e60a699 572
77319f22 573 return ret;
0e60a699
AG
574}
575
268846ba
ED
576static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
577{
578 uint64_t r1, r3;
579
580 cpu_synchronize_state(CPU(cpu));
581 r1 = (run->s390_sieic.ipa & 0x00f0) >> 8;
582 r3 = run->s390_sieic.ipa & 0x000f;
583 handle_diag_308(&cpu->env, r1, r3);
584}
585
b30f4dfb
DH
586static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
587{
588 CPUS390XState *env = &cpu->env;
589 unsigned long pc;
590
591 cpu_synchronize_state(CPU(cpu));
592
593 pc = env->psw.addr - 4;
594 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
595 env->psw.addr = pc;
596 return EXCP_DEBUG;
597 }
598
599 return -ENOENT;
600}
601
638129ff
CH
602#define DIAG_KVM_CODE_MASK 0x000000000000ffff
603
604static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
0e60a699
AG
605{
606 int r = 0;
638129ff
CH
607 uint16_t func_code;
608
609 /*
610 * For any diagnose call we support, bits 48-63 of the resulting
611 * address specify the function code; the remainder is ignored.
612 */
613 func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
614 switch (func_code) {
268846ba
ED
615 case DIAG_IPL:
616 kvm_handle_diag_308(cpu, run);
617 break;
39fbc5c6
CB
618 case DIAG_KVM_HYPERCALL:
619 r = handle_hypercall(cpu, run);
620 break;
621 case DIAG_KVM_BREAKPOINT:
b30f4dfb 622 r = handle_sw_breakpoint(cpu, run);
39fbc5c6
CB
623 break;
624 default:
638129ff 625 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
39fbc5c6
CB
626 r = -1;
627 break;
0e60a699
AG
628 }
629
630 return r;
631}
632
b20a461f
TH
633static int kvm_s390_cpu_start(S390CPU *cpu)
634{
635 s390_add_running_cpu(cpu);
636 qemu_cpu_kick(CPU(cpu));
637 DPRINTF("DONE: KVM cpu start: %p\n", &cpu->env);
638 return 0;
639}
640
7f7f9752 641int kvm_s390_cpu_restart(S390CPU *cpu)
0e60a699 642{
1bc22652 643 kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0);
49e15878 644 s390_add_running_cpu(cpu);
c08d7424 645 qemu_cpu_kick(CPU(cpu));
7f7f9752 646 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
0e60a699
AG
647 return 0;
648}
649
f7d3e466 650static void sigp_initial_cpu_reset(void *arg)
0e60a699 651{
f7d3e466
TH
652 CPUState *cpu = arg;
653 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
d5900813 654
f7d3e466
TH
655 cpu_synchronize_state(cpu);
656 scc->initial_cpu_reset(cpu);
0e60a699
AG
657}
658
04c2b516
TH
659static void sigp_cpu_reset(void *arg)
660{
661 CPUState *cpu = arg;
662 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
663
664 cpu_synchronize_state(cpu);
665 scc->cpu_reset(cpu);
666}
667
b8031adb
TH
668#define SIGP_ORDER_MASK 0x000000ff
669
f7575c96 670static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
0e60a699 671{
f7575c96 672 CPUS390XState *env = &cpu->env;
0e60a699 673 uint8_t order_code;
0e60a699 674 uint16_t cpu_addr;
45fa769b 675 S390CPU *target_cpu;
3796f0e1
TH
676 uint64_t *statusreg = &env->regs[ipa1 >> 4];
677 int cc;
0e60a699 678
cb446eca 679 cpu_synchronize_state(CPU(cpu));
0e60a699
AG
680
681 /* get order code */
b8031adb 682 order_code = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
0e60a699 683
0e60a699 684 cpu_addr = env->regs[ipa1 & 0x0f];
45fa769b
AF
685 target_cpu = s390_cpu_addr2state(cpu_addr);
686 if (target_cpu == NULL) {
3796f0e1 687 cc = 3; /* not operational */
0e60a699
AG
688 goto out;
689 }
690
691 switch (order_code) {
b20a461f 692 case SIGP_START:
3796f0e1 693 cc = kvm_s390_cpu_start(target_cpu);
b20a461f 694 break;
0b9972a2 695 case SIGP_RESTART:
3796f0e1 696 cc = kvm_s390_cpu_restart(target_cpu);
0b9972a2
TH
697 break;
698 case SIGP_SET_ARCH:
0788082a
TH
699 *statusreg &= 0xffffffff00000000UL;
700 *statusreg |= SIGP_STAT_INVALID_PARAMETER;
701 cc = 1; /* status stored */
702 break;
0b9972a2 703 case SIGP_INITIAL_CPU_RESET:
f7d3e466
TH
704 run_on_cpu(CPU(target_cpu), sigp_initial_cpu_reset, CPU(target_cpu));
705 cc = 0;
0b9972a2 706 break;
04c2b516
TH
707 case SIGP_CPU_RESET:
708 run_on_cpu(CPU(target_cpu), sigp_cpu_reset, CPU(target_cpu));
709 cc = 0;
710 break;
0b9972a2 711 default:
3796f0e1
TH
712 DPRINTF("KVM: unknown SIGP: 0x%x\n", order_code);
713 *statusreg &= 0xffffffff00000000UL;
714 *statusreg |= SIGP_STAT_INVALID_ORDER;
715 cc = 1; /* status stored */
0b9972a2 716 break;
0e60a699
AG
717 }
718
719out:
3796f0e1 720 setcc(cpu, cc);
0e60a699
AG
721 return 0;
722}
723
b30f4dfb 724static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
0e60a699
AG
725{
726 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
727 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
d7963c43 728 int r = -1;
0e60a699 729
e67137c6
PM
730 DPRINTF("handle_instruction 0x%x 0x%x\n",
731 run->s390_sieic.ipa, run->s390_sieic.ipb);
0e60a699 732 switch (ipa0) {
09b99878 733 case IPA0_B2:
1eecf41b
FB
734 r = handle_b2(cpu, run, ipa1);
735 break;
09b99878 736 case IPA0_B9:
1eecf41b
FB
737 r = handle_b9(cpu, run, ipa1);
738 break;
09b99878 739 case IPA0_EB:
1eecf41b 740 r = handle_eb(cpu, run, ipa1);
09b99878
CH
741 break;
742 case IPA0_DIAG:
638129ff 743 r = handle_diag(cpu, run, run->s390_sieic.ipb);
09b99878
CH
744 break;
745 case IPA0_SIGP:
746 r = handle_sigp(cpu, run, ipa1);
747 break;
0e60a699
AG
748 }
749
750 if (r < 0) {
b30f4dfb 751 r = 0;
1bc22652 752 enter_pgmcheck(cpu, 0x0001);
0e60a699 753 }
b30f4dfb
DH
754
755 return r;
0e60a699
AG
756}
757
f7575c96 758static bool is_special_wait_psw(CPUState *cs)
eca3ed03
CB
759{
760 /* signal quiesce */
f7575c96 761 return cs->kvm_run->psw_addr == 0xfffUL;
eca3ed03
CB
762}
763
1bc22652 764static int handle_intercept(S390CPU *cpu)
0e60a699 765{
f7575c96
AF
766 CPUState *cs = CPU(cpu);
767 struct kvm_run *run = cs->kvm_run;
0e60a699
AG
768 int icpt_code = run->s390_sieic.icptcode;
769 int r = 0;
770
e67137c6 771 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
f7575c96 772 (long)cs->kvm_run->psw_addr);
0e60a699
AG
773 switch (icpt_code) {
774 case ICPT_INSTRUCTION:
b30f4dfb 775 r = handle_instruction(cpu, run);
0e60a699
AG
776 break;
777 case ICPT_WAITPSW:
08eb8c85
CB
778 /* disabled wait, since enabled wait is handled in kernel */
779 if (s390_del_running_cpu(cpu) == 0) {
780 if (is_special_wait_psw(cs)) {
781 qemu_system_shutdown_request();
782 } else {
783 QObject *data;
784
785 data = qobject_from_jsonf("{ 'action': %s }", "pause");
786 monitor_protocol_event(QEVENT_GUEST_PANICKED, data);
787 qobject_decref(data);
788 vm_stop(RUN_STATE_GUEST_PANICKED);
789 }
eca3ed03
CB
790 }
791 r = EXCP_HALTED;
792 break;
854e42f3 793 case ICPT_CPU_STOP:
49e15878 794 if (s390_del_running_cpu(cpu) == 0) {
854e42f3
CB
795 qemu_system_shutdown_request();
796 }
797 r = EXCP_HALTED;
0e60a699
AG
798 break;
799 case ICPT_SOFT_INTERCEPT:
800 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
801 exit(1);
802 break;
0e60a699
AG
803 case ICPT_IO:
804 fprintf(stderr, "KVM unimplemented icpt IO\n");
805 exit(1);
806 break;
807 default:
808 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
809 exit(1);
810 break;
811 }
812
813 return r;
814}
815
09b99878
CH
816static int handle_tsch(S390CPU *cpu)
817{
818 CPUS390XState *env = &cpu->env;
819 CPUState *cs = CPU(cpu);
820 struct kvm_run *run = cs->kvm_run;
821 int ret;
822
44c68de0 823 cpu_synchronize_state(cs);
3474b679 824
09b99878
CH
825 ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
826 if (ret >= 0) {
827 /* Success; set condition code. */
828 setcc(cpu, ret);
829 ret = 0;
830 } else if (ret < -1) {
831 /*
832 * Failure.
833 * If an I/O interrupt had been dequeued, we have to reinject it.
834 */
835 if (run->s390_tsch.dequeued) {
836 uint16_t subchannel_id = run->s390_tsch.subchannel_id;
837 uint16_t subchannel_nr = run->s390_tsch.subchannel_nr;
838 uint32_t io_int_parm = run->s390_tsch.io_int_parm;
839 uint32_t io_int_word = run->s390_tsch.io_int_word;
840 uint32_t type = ((subchannel_id & 0xff00) << 24) |
841 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
842
843 kvm_s390_interrupt_internal(cpu, type,
844 ((uint32_t)subchannel_id << 16)
845 | subchannel_nr,
846 ((uint64_t)io_int_parm << 32)
847 | io_int_word, 1);
848 }
849 ret = 0;
850 }
851 return ret;
852}
853
8c012449
DH
854static int kvm_arch_handle_debug_exit(S390CPU *cpu)
855{
856 return -ENOSYS;
857}
858
20d695a9 859int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
0e60a699 860{
20d695a9 861 S390CPU *cpu = S390_CPU(cs);
0e60a699
AG
862 int ret = 0;
863
864 switch (run->exit_reason) {
865 case KVM_EXIT_S390_SIEIC:
1bc22652 866 ret = handle_intercept(cpu);
0e60a699
AG
867 break;
868 case KVM_EXIT_S390_RESET:
add142e0 869 qemu_system_reset_request();
0e60a699 870 break;
09b99878
CH
871 case KVM_EXIT_S390_TSCH:
872 ret = handle_tsch(cpu);
873 break;
8c012449
DH
874 case KVM_EXIT_DEBUG:
875 ret = kvm_arch_handle_debug_exit(cpu);
876 break;
0e60a699
AG
877 default:
878 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
879 break;
880 }
881
bb4ea393
JK
882 if (ret == 0) {
883 ret = EXCP_INTERRUPT;
bb4ea393 884 }
0e60a699
AG
885 return ret;
886}
4513d923 887
20d695a9 888bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
4513d923
GN
889{
890 return true;
891}
a1b87fe0 892
20d695a9 893int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
a1b87fe0
JK
894{
895 return 1;
896}
897
898int kvm_arch_on_sigbus(int code, void *addr)
899{
900 return 1;
901}
09b99878
CH
902
903void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id,
904 uint16_t subchannel_nr, uint32_t io_int_parm,
905 uint32_t io_int_word)
906{
907 uint32_t type;
908
7e749462
CH
909 if (io_int_word & IO_INT_WORD_AI) {
910 type = KVM_S390_INT_IO(1, 0, 0, 0);
911 } else {
912 type = ((subchannel_id & 0xff00) << 24) |
913 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
914 }
09b99878
CH
915 kvm_s390_interrupt_internal(cpu, type,
916 ((uint32_t)subchannel_id << 16) | subchannel_nr,
917 ((uint64_t)io_int_parm << 32) | io_int_word, 1);
918}
919
920void kvm_s390_crw_mchk(S390CPU *cpu)
921{
922 kvm_s390_interrupt_internal(cpu, KVM_S390_MCHK, 1 << 28,
923 0x00400f1d40330000, 1);
924}
925
926void kvm_s390_enable_css_support(S390CPU *cpu)
927{
09b99878
CH
928 int r;
929
930 /* Activate host kernel channel subsystem support. */
e080f0fd 931 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
09b99878
CH
932 assert(r == 0);
933}
48475e14
AK
934
935void kvm_arch_init_irq_routing(KVMState *s)
936{
d426d9fb
CH
937 /*
938 * Note that while irqchip capabilities generally imply that cpustates
939 * are handled in-kernel, it is not true for s390 (yet); therefore, we
940 * have to override the common code kvm_halt_in_kernel_allowed setting.
941 */
942 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
943 kvm_irqfds_allowed = true;
944 kvm_gsi_routing_allowed = true;
945 kvm_halt_in_kernel_allowed = false;
946 }
48475e14 947}
b4436a0b 948
cc3ac9c4
CH
949int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
950 int vq, bool assign)
b4436a0b
CH
951{
952 struct kvm_ioeventfd kick = {
953 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
954 KVM_IOEVENTFD_FLAG_DATAMATCH,
cc3ac9c4 955 .fd = event_notifier_get_fd(notifier),
b4436a0b
CH
956 .datamatch = vq,
957 .addr = sch,
958 .len = 8,
959 };
960 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
961 return -ENOSYS;
962 }
963 if (!assign) {
964 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
965 }
966 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
967}