]> git.proxmox.com Git - qemu.git/blame - target-s390x/kvm.c
s390: wire up nmi command to raise a RESTART interrupt on S390
[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"
0e60a699
AG
39
40/* #define DEBUG_KVM */
41
42#ifdef DEBUG_KVM
e67137c6 43#define DPRINTF(fmt, ...) \
0e60a699
AG
44 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
45#else
e67137c6 46#define DPRINTF(fmt, ...) \
0e60a699
AG
47 do { } while (0)
48#endif
49
50#define IPA0_DIAG 0x8300
51#define IPA0_SIGP 0xae00
09b99878
CH
52#define IPA0_B2 0xb200
53#define IPA0_B9 0xb900
54#define IPA0_EB 0xeb00
0e60a699
AG
55
56#define PRIV_SCLP_CALL 0x20
09b99878
CH
57#define PRIV_CSCH 0x30
58#define PRIV_HSCH 0x31
59#define PRIV_MSCH 0x32
60#define PRIV_SSCH 0x33
61#define PRIV_STSCH 0x34
62#define PRIV_TSCH 0x35
63#define PRIV_TPI 0x36
64#define PRIV_SAL 0x37
65#define PRIV_RSCH 0x38
66#define PRIV_STCRW 0x39
67#define PRIV_STCPS 0x3a
68#define PRIV_RCHP 0x3b
69#define PRIV_SCHM 0x3c
70#define PRIV_CHSC 0x5f
71#define PRIV_SIGA 0x74
72#define PRIV_XSCH 0x76
73#define PRIV_SQBS 0x8a
74#define PRIV_EQBS 0x9c
268846ba 75#define DIAG_IPL 0x308
0e60a699
AG
76#define DIAG_KVM_HYPERCALL 0x500
77#define DIAG_KVM_BREAKPOINT 0x501
78
0e60a699
AG
79#define ICPT_INSTRUCTION 0x04
80#define ICPT_WAITPSW 0x1c
81#define ICPT_SOFT_INTERCEPT 0x24
82#define ICPT_CPU_STOP 0x28
83#define ICPT_IO 0x40
84
85#define SIGP_RESTART 0x06
86#define SIGP_INITIAL_CPU_RESET 0x0b
87#define SIGP_STORE_STATUS_ADDR 0x0e
88#define SIGP_SET_ARCH 0x12
89
94a8d39a
JK
90const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
91 KVM_CAP_LAST_INFO
92};
93
5b08b344
CB
94static int cap_sync_regs;
95
cad1e282 96int kvm_arch_init(KVMState *s)
0e60a699 97{
5b08b344 98 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
0e60a699
AG
99 return 0;
100}
101
b164e48e
EH
102unsigned long kvm_arch_vcpu_id(CPUState *cpu)
103{
104 return cpu->cpu_index;
105}
106
20d695a9 107int kvm_arch_init_vcpu(CPUState *cpu)
0e60a699 108{
1c9d2a1d
CB
109 /* nothing todo yet */
110 return 0;
0e60a699
AG
111}
112
20d695a9 113void kvm_arch_reset_vcpu(CPUState *cpu)
0e60a699 114{
419831d7
AG
115 /* The initial reset call is needed here to reset in-kernel
116 * vcpu data that we can't access directly from QEMU
117 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
118 * Before this ioctl cpu_synchronize_state() is called in common kvm
119 * code (kvm-all) */
70bada03
JF
120 if (kvm_vcpu_ioctl(cpu, KVM_S390_INITIAL_RESET, NULL)) {
121 perror("Can't reset vcpu\n");
122 }
0e60a699
AG
123}
124
20d695a9 125int kvm_arch_put_registers(CPUState *cs, int level)
0e60a699 126{
20d695a9
AF
127 S390CPU *cpu = S390_CPU(cs);
128 CPUS390XState *env = &cpu->env;
420840e5 129 struct kvm_one_reg reg;
5b08b344 130 struct kvm_sregs sregs;
0e60a699
AG
131 struct kvm_regs regs;
132 int ret;
133 int i;
134
5b08b344 135 /* always save the PSW and the GPRS*/
f7575c96
AF
136 cs->kvm_run->psw_addr = env->psw.addr;
137 cs->kvm_run->psw_mask = env->psw.mask;
0e60a699 138
f7575c96 139 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
5b08b344 140 for (i = 0; i < 16; i++) {
f7575c96
AF
141 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
142 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
5b08b344
CB
143 }
144 } else {
145 for (i = 0; i < 16; i++) {
146 regs.gprs[i] = env->regs[i];
147 }
1bc22652 148 ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
5b08b344
CB
149 if (ret < 0) {
150 return ret;
151 }
0e60a699
AG
152 }
153
420840e5
JH
154 if (env->runtime_reg_dirty_mask == KVM_S390_RUNTIME_DIRTY_FULL) {
155 reg.id = KVM_REG_S390_CPU_TIMER;
156 reg.addr = (__u64)&(env->cputm);
157 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
158 if (ret < 0) {
159 return ret;
160 }
161
162 reg.id = KVM_REG_S390_CLOCK_COMP;
163 reg.addr = (__u64)&(env->ckc);
164 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
165 if (ret < 0) {
166 return ret;
167 }
168
169 reg.id = KVM_REG_S390_TODPR;
170 reg.addr = (__u64)&(env->todpr);
171 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
172 if (ret < 0) {
173 return ret;
174 }
175 }
176 env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_NONE;
177
5b08b344
CB
178 /* Do we need to save more than that? */
179 if (level == KVM_PUT_RUNTIME_STATE) {
180 return 0;
0e60a699
AG
181 }
182
5b08b344 183 if (cap_sync_regs &&
f7575c96
AF
184 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
185 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
5b08b344 186 for (i = 0; i < 16; i++) {
f7575c96
AF
187 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
188 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
5b08b344 189 }
f7575c96
AF
190 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
191 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
5b08b344
CB
192 } else {
193 for (i = 0; i < 16; i++) {
194 sregs.acrs[i] = env->aregs[i];
195 sregs.crs[i] = env->cregs[i];
196 }
1bc22652 197 ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
5b08b344
CB
198 if (ret < 0) {
199 return ret;
200 }
201 }
0e60a699 202
5b08b344 203 /* Finally the prefix */
f7575c96
AF
204 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
205 cs->kvm_run->s.regs.prefix = env->psa;
206 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
5b08b344
CB
207 } else {
208 /* prefix is only supported via sync regs */
209 }
210 return 0;
0e60a699
AG
211}
212
20d695a9 213int kvm_arch_get_registers(CPUState *cs)
420840e5
JH
214{
215 S390CPU *cpu = S390_CPU(cs);
216 CPUS390XState *env = &cpu->env;
217 struct kvm_one_reg reg;
218 int r;
219
220 r = kvm_s390_get_registers_partial(cs);
221 if (r < 0) {
222 return r;
223 }
224
225 reg.id = KVM_REG_S390_CPU_TIMER;
226 reg.addr = (__u64)&(env->cputm);
227 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
228 if (r < 0) {
229 return r;
230 }
231
232 reg.id = KVM_REG_S390_CLOCK_COMP;
233 reg.addr = (__u64)&(env->ckc);
234 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
235 if (r < 0) {
236 return r;
237 }
238
239 reg.id = KVM_REG_S390_TODPR;
240 reg.addr = (__u64)&(env->todpr);
241 r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
242 if (r < 0) {
243 return r;
244 }
245
246 env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_FULL;
247 return 0;
248}
249
250int kvm_s390_get_registers_partial(CPUState *cs)
0e60a699 251{
20d695a9
AF
252 S390CPU *cpu = S390_CPU(cs);
253 CPUS390XState *env = &cpu->env;
5b08b344 254 struct kvm_sregs sregs;
0e60a699 255 struct kvm_regs regs;
5b08b344 256 int ret;
0e60a699
AG
257 int i;
258
420840e5
JH
259 if (env->runtime_reg_dirty_mask) {
260 return 0;
261 }
262
5b08b344 263 /* get the PSW */
f7575c96
AF
264 env->psw.addr = cs->kvm_run->psw_addr;
265 env->psw.mask = cs->kvm_run->psw_mask;
5b08b344
CB
266
267 /* the GPRS */
f7575c96 268 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
5b08b344 269 for (i = 0; i < 16; i++) {
f7575c96 270 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
5b08b344
CB
271 }
272 } else {
1bc22652 273 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
5b08b344
CB
274 if (ret < 0) {
275 return ret;
276 }
277 for (i = 0; i < 16; i++) {
278 env->regs[i] = regs.gprs[i];
279 }
0e60a699
AG
280 }
281
5b08b344
CB
282 /* The ACRS and CRS */
283 if (cap_sync_regs &&
f7575c96
AF
284 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
285 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
5b08b344 286 for (i = 0; i < 16; i++) {
f7575c96
AF
287 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
288 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
5b08b344
CB
289 }
290 } else {
1bc22652 291 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
5b08b344
CB
292 if (ret < 0) {
293 return ret;
294 }
295 for (i = 0; i < 16; i++) {
296 env->aregs[i] = sregs.acrs[i];
297 env->cregs[i] = sregs.crs[i];
298 }
0e60a699
AG
299 }
300
5b08b344 301 /* Finally the prefix */
f7575c96
AF
302 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
303 env->psa = cs->kvm_run->s.regs.prefix;
5b08b344
CB
304 } else {
305 /* no prefix without sync regs */
306 }
0e60a699 307
420840e5 308 env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_PARTIAL;
0e60a699
AG
309 return 0;
310}
311
fdec9918
CB
312/*
313 * Legacy layout for s390:
314 * Older S390 KVM requires the topmost vma of the RAM to be
315 * smaller than an system defined value, which is at least 256GB.
316 * Larger systems have larger values. We put the guest between
317 * the end of data segment (system break) and this value. We
318 * use 32GB as a base to have enough room for the system break
319 * to grow. We also have to use MAP parameters that avoid
320 * read-only mapping of guest pages.
321 */
322static void *legacy_s390_alloc(ram_addr_t size)
323{
324 void *mem;
325
326 mem = mmap((void *) 0x800000000ULL, size,
327 PROT_EXEC|PROT_READ|PROT_WRITE,
328 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
329 if (mem == MAP_FAILED) {
330 fprintf(stderr, "Allocating RAM failed\n");
331 abort();
332 }
333 return mem;
334}
335
6eebf958 336void *kvm_arch_ram_alloc(ram_addr_t size)
fdec9918
CB
337{
338 /* Can we use the standard allocation ? */
339 if (kvm_check_extension(kvm_state, KVM_CAP_S390_GMAP) &&
340 kvm_check_extension(kvm_state, KVM_CAP_S390_COW)) {
341 return NULL;
342 } else {
343 return legacy_s390_alloc(size);
344 }
345}
346
20d695a9 347int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
0e60a699
AG
348{
349 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
350
9282b73a
CB
351 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
352 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501, 4, 1)) {
0e60a699
AG
353 return -EINVAL;
354 }
355 return 0;
356}
357
20d695a9 358int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
0e60a699
AG
359{
360 uint8_t t[4];
361 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
362
9282b73a 363 if (cpu_memory_rw_debug(cs, bp->pc, t, 4, 0)) {
0e60a699
AG
364 return -EINVAL;
365 } else if (memcmp(t, diag_501, 4)) {
366 return -EINVAL;
9282b73a 367 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
0e60a699
AG
368 return -EINVAL;
369 }
370
371 return 0;
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
f7575c96 430static inline void setcc(S390CPU *cpu, uint64_t cc)
0e60a699 431{
f7575c96
AF
432 CPUS390XState *env = &cpu->env;
433 CPUState *cs = CPU(cpu);
434
435 cs->kvm_run->psw_mask &= ~(3ull << 44);
436 cs->kvm_run->psw_mask |= (cc & 3) << 44;
0e60a699
AG
437
438 env->psw.mask &= ~(3ul << 44);
439 env->psw.mask |= (cc & 3) << 44;
440}
441
1bc22652 442static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
bcec36ea 443 uint16_t ipbh0)
0e60a699 444{
1bc22652 445 CPUS390XState *env = &cpu->env;
0e60a699
AG
446 uint32_t sccb;
447 uint64_t code;
448 int r = 0;
449
cb446eca 450 cpu_synchronize_state(CPU(cpu));
0e60a699
AG
451 sccb = env->regs[ipbh0 & 0xf];
452 code = env->regs[(ipbh0 & 0xf0) >> 4];
453
f6c98f92 454 r = sclp_service_call(sccb, code);
9abf567d 455 if (r < 0) {
1bc22652 456 enter_pgmcheck(cpu, -r);
0e60a699 457 }
f7575c96 458 setcc(cpu, r);
81f7c56c 459
0e60a699
AG
460 return 0;
461}
462
09b99878
CH
463static int kvm_handle_css_inst(S390CPU *cpu, struct kvm_run *run,
464 uint8_t ipa0, uint8_t ipa1, uint8_t ipb)
465{
466 int r = 0;
467 int no_cc = 0;
468 CPUS390XState *env = &cpu->env;
19079e46 469 CPUState *cs = CPU(cpu);
09b99878
CH
470
471 if (ipa0 != 0xb2) {
472 /* Not handled for now. */
473 return -1;
474 }
3474b679
JH
475
476 kvm_s390_get_registers_partial(cs);
477 cs->kvm_vcpu_dirty = true;
478
09b99878
CH
479 switch (ipa1) {
480 case PRIV_XSCH:
481 r = ioinst_handle_xsch(env, env->regs[1]);
482 break;
483 case PRIV_CSCH:
484 r = ioinst_handle_csch(env, env->regs[1]);
485 break;
486 case PRIV_HSCH:
487 r = ioinst_handle_hsch(env, env->regs[1]);
488 break;
489 case PRIV_MSCH:
490 r = ioinst_handle_msch(env, env->regs[1], run->s390_sieic.ipb);
491 break;
492 case PRIV_SSCH:
493 r = ioinst_handle_ssch(env, env->regs[1], run->s390_sieic.ipb);
494 break;
495 case PRIV_STCRW:
496 r = ioinst_handle_stcrw(env, run->s390_sieic.ipb);
497 break;
498 case PRIV_STSCH:
499 r = ioinst_handle_stsch(env, env->regs[1], run->s390_sieic.ipb);
500 break;
501 case PRIV_TSCH:
502 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
503 fprintf(stderr, "Spurious tsch intercept\n");
504 break;
505 case PRIV_CHSC:
506 r = ioinst_handle_chsc(env, run->s390_sieic.ipb);
507 break;
508 case PRIV_TPI:
509 /* This should have been handled by kvm already. */
510 fprintf(stderr, "Spurious tpi intercept\n");
511 break;
512 case PRIV_SCHM:
513 no_cc = 1;
514 r = ioinst_handle_schm(env, env->regs[1], env->regs[2],
515 run->s390_sieic.ipb);
516 break;
517 case PRIV_RSCH:
518 r = ioinst_handle_rsch(env, env->regs[1]);
519 break;
520 case PRIV_RCHP:
521 r = ioinst_handle_rchp(env, env->regs[1]);
522 break;
523 case PRIV_STCPS:
524 /* We do not provide this instruction, it is suppressed. */
525 no_cc = 1;
526 r = 0;
527 break;
528 case PRIV_SAL:
529 no_cc = 1;
530 r = ioinst_handle_sal(env, env->regs[1]);
531 break;
c1e8dfb5
TH
532 case PRIV_SIGA:
533 /* Not provided, set CC = 3 for subchannel not operational */
534 r = 3;
09b99878 535 break;
c1e8dfb5
TH
536 default:
537 return -1;
09b99878
CH
538 }
539
c1e8dfb5
TH
540 if (r >= 0 && !no_cc) {
541 setcc(cpu, r);
09b99878
CH
542 }
543
c1e8dfb5 544 return 0;
09b99878
CH
545}
546
547static int handle_priv(S390CPU *cpu, struct kvm_run *run,
548 uint8_t ipa0, uint8_t ipa1)
0e60a699
AG
549{
550 int r = 0;
551 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
09b99878 552 uint8_t ipb = run->s390_sieic.ipb & 0xff;
0e60a699 553
e67137c6 554 DPRINTF("KVM: PRIV: %d\n", ipa1);
0e60a699
AG
555 switch (ipa1) {
556 case PRIV_SCLP_CALL:
1bc22652 557 r = kvm_sclp_service_call(cpu, run, ipbh0);
0e60a699
AG
558 break;
559 default:
c1e8dfb5
TH
560 r = kvm_handle_css_inst(cpu, run, ipa0, ipa1, ipb);
561 if (r == -1) {
562 DPRINTF("KVM: unhandled PRIV: 0x%x\n", ipa1);
09b99878 563 }
0e60a699
AG
564 break;
565 }
566
567 return r;
568}
569
4fd6dd06 570static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
0e60a699 571{
4fd6dd06
AF
572 CPUState *cs = CPU(cpu);
573 CPUS390XState *env = &cpu->env;
3474b679
JH
574
575 kvm_s390_get_registers_partial(cs);
576 cs->kvm_vcpu_dirty = true;
28e942f8 577 env->regs[2] = s390_virtio_hypercall(env);
0e60a699 578
bcec36ea 579 return 0;
0e60a699
AG
580}
581
268846ba
ED
582static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
583{
584 uint64_t r1, r3;
585
586 cpu_synchronize_state(CPU(cpu));
587 r1 = (run->s390_sieic.ipa & 0x00f0) >> 8;
588 r3 = run->s390_sieic.ipa & 0x000f;
589 handle_diag_308(&cpu->env, r1, r3);
590}
591
4fd6dd06 592static int handle_diag(S390CPU *cpu, struct kvm_run *run, int ipb_code)
0e60a699
AG
593{
594 int r = 0;
595
596 switch (ipb_code) {
268846ba
ED
597 case DIAG_IPL:
598 kvm_handle_diag_308(cpu, run);
599 break;
39fbc5c6
CB
600 case DIAG_KVM_HYPERCALL:
601 r = handle_hypercall(cpu, run);
602 break;
603 case DIAG_KVM_BREAKPOINT:
604 sleep(10);
605 break;
606 default:
607 DPRINTF("KVM: unknown DIAG: 0x%x\n", ipb_code);
608 r = -1;
609 break;
0e60a699
AG
610 }
611
612 return r;
613}
614
7f7f9752 615int kvm_s390_cpu_restart(S390CPU *cpu)
0e60a699 616{
1bc22652 617 kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0);
49e15878 618 s390_add_running_cpu(cpu);
c08d7424 619 qemu_cpu_kick(CPU(cpu));
7f7f9752 620 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
0e60a699
AG
621 return 0;
622}
623
a4e3ad19 624static int s390_store_status(CPUS390XState *env, uint32_t parameter)
0e60a699
AG
625{
626 /* XXX */
627 fprintf(stderr, "XXX SIGP store status\n");
628 return -1;
629}
630
1bc22652 631static int s390_cpu_initial_reset(S390CPU *cpu)
0e60a699 632{
cb446eca 633 CPUState *cs = CPU(cpu);
1bc22652 634 CPUS390XState *env = &cpu->env;
d5900813
AG
635 int i;
636
49e15878 637 s390_del_running_cpu(cpu);
cb446eca 638 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL) < 0) {
d5900813
AG
639 perror("cannot init reset vcpu");
640 }
641
642 /* Manually zero out all registers */
cb446eca 643 cpu_synchronize_state(cs);
d5900813
AG
644 for (i = 0; i < 16; i++) {
645 env->regs[i] = 0;
646 }
647
e67137c6 648 DPRINTF("DONE: SIGP initial reset: %p\n", env);
d5900813 649 return 0;
0e60a699
AG
650}
651
f7575c96 652static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
0e60a699 653{
f7575c96 654 CPUS390XState *env = &cpu->env;
0e60a699
AG
655 uint8_t order_code;
656 uint32_t parameter;
657 uint16_t cpu_addr;
658 uint8_t t;
659 int r = -1;
45fa769b 660 S390CPU *target_cpu;
a4e3ad19 661 CPUS390XState *target_env;
0e60a699 662
cb446eca 663 cpu_synchronize_state(CPU(cpu));
0e60a699
AG
664
665 /* get order code */
666 order_code = run->s390_sieic.ipb >> 28;
667 if (order_code > 0) {
668 order_code = env->regs[order_code];
669 }
670 order_code += (run->s390_sieic.ipb & 0x0fff0000) >> 16;
671
672 /* get parameters */
673 t = (ipa1 & 0xf0) >> 4;
674 if (!(t % 2)) {
675 t++;
676 }
677
678 parameter = env->regs[t] & 0x7ffffe00;
679 cpu_addr = env->regs[ipa1 & 0x0f];
680
45fa769b
AF
681 target_cpu = s390_cpu_addr2state(cpu_addr);
682 if (target_cpu == NULL) {
0e60a699
AG
683 goto out;
684 }
45fa769b 685 target_env = &target_cpu->env;
0e60a699
AG
686
687 switch (order_code) {
688 case SIGP_RESTART:
7f7f9752 689 r = kvm_s390_cpu_restart(target_cpu);
0e60a699
AG
690 break;
691 case SIGP_STORE_STATUS_ADDR:
692 r = s390_store_status(target_env, parameter);
693 break;
694 case SIGP_SET_ARCH:
695 /* make the caller panic */
696 return -1;
697 case SIGP_INITIAL_CPU_RESET:
1bc22652 698 r = s390_cpu_initial_reset(target_cpu);
0e60a699
AG
699 break;
700 default:
a74cdab4 701 fprintf(stderr, "KVM: unknown SIGP: 0x%x\n", order_code);
0e60a699
AG
702 break;
703 }
704
705out:
f7575c96 706 setcc(cpu, r ? 3 : 0);
0e60a699
AG
707 return 0;
708}
709
d2ee7746 710static void handle_instruction(S390CPU *cpu, struct kvm_run *run)
0e60a699
AG
711{
712 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
713 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
714 int ipb_code = (run->s390_sieic.ipb & 0x0fff0000) >> 16;
d7963c43 715 int r = -1;
0e60a699 716
e67137c6
PM
717 DPRINTF("handle_instruction 0x%x 0x%x\n",
718 run->s390_sieic.ipa, run->s390_sieic.ipb);
0e60a699 719 switch (ipa0) {
09b99878
CH
720 case IPA0_B2:
721 case IPA0_B9:
722 case IPA0_EB:
723 r = handle_priv(cpu, run, ipa0 >> 8, ipa1);
724 break;
725 case IPA0_DIAG:
4fd6dd06 726 r = handle_diag(cpu, run, ipb_code);
09b99878
CH
727 break;
728 case IPA0_SIGP:
729 r = handle_sigp(cpu, run, ipa1);
730 break;
0e60a699
AG
731 }
732
733 if (r < 0) {
1bc22652 734 enter_pgmcheck(cpu, 0x0001);
0e60a699 735 }
0e60a699
AG
736}
737
f7575c96 738static bool is_special_wait_psw(CPUState *cs)
eca3ed03
CB
739{
740 /* signal quiesce */
f7575c96 741 return cs->kvm_run->psw_addr == 0xfffUL;
eca3ed03
CB
742}
743
1bc22652 744static int handle_intercept(S390CPU *cpu)
0e60a699 745{
f7575c96
AF
746 CPUState *cs = CPU(cpu);
747 struct kvm_run *run = cs->kvm_run;
0e60a699
AG
748 int icpt_code = run->s390_sieic.icptcode;
749 int r = 0;
750
e67137c6 751 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
f7575c96 752 (long)cs->kvm_run->psw_addr);
0e60a699
AG
753 switch (icpt_code) {
754 case ICPT_INSTRUCTION:
d2ee7746 755 handle_instruction(cpu, run);
0e60a699
AG
756 break;
757 case ICPT_WAITPSW:
08eb8c85
CB
758 /* disabled wait, since enabled wait is handled in kernel */
759 if (s390_del_running_cpu(cpu) == 0) {
760 if (is_special_wait_psw(cs)) {
761 qemu_system_shutdown_request();
762 } else {
763 QObject *data;
764
765 data = qobject_from_jsonf("{ 'action': %s }", "pause");
766 monitor_protocol_event(QEVENT_GUEST_PANICKED, data);
767 qobject_decref(data);
768 vm_stop(RUN_STATE_GUEST_PANICKED);
769 }
eca3ed03
CB
770 }
771 r = EXCP_HALTED;
772 break;
854e42f3 773 case ICPT_CPU_STOP:
49e15878 774 if (s390_del_running_cpu(cpu) == 0) {
854e42f3
CB
775 qemu_system_shutdown_request();
776 }
777 r = EXCP_HALTED;
0e60a699
AG
778 break;
779 case ICPT_SOFT_INTERCEPT:
780 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
781 exit(1);
782 break;
0e60a699
AG
783 case ICPT_IO:
784 fprintf(stderr, "KVM unimplemented icpt IO\n");
785 exit(1);
786 break;
787 default:
788 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
789 exit(1);
790 break;
791 }
792
793 return r;
794}
795
09b99878
CH
796static int handle_tsch(S390CPU *cpu)
797{
798 CPUS390XState *env = &cpu->env;
799 CPUState *cs = CPU(cpu);
800 struct kvm_run *run = cs->kvm_run;
801 int ret;
802
3474b679
JH
803 kvm_s390_get_registers_partial(cs);
804 cs->kvm_vcpu_dirty = true;
805
09b99878
CH
806 ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
807 if (ret >= 0) {
808 /* Success; set condition code. */
809 setcc(cpu, ret);
810 ret = 0;
811 } else if (ret < -1) {
812 /*
813 * Failure.
814 * If an I/O interrupt had been dequeued, we have to reinject it.
815 */
816 if (run->s390_tsch.dequeued) {
817 uint16_t subchannel_id = run->s390_tsch.subchannel_id;
818 uint16_t subchannel_nr = run->s390_tsch.subchannel_nr;
819 uint32_t io_int_parm = run->s390_tsch.io_int_parm;
820 uint32_t io_int_word = run->s390_tsch.io_int_word;
821 uint32_t type = ((subchannel_id & 0xff00) << 24) |
822 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
823
824 kvm_s390_interrupt_internal(cpu, type,
825 ((uint32_t)subchannel_id << 16)
826 | subchannel_nr,
827 ((uint64_t)io_int_parm << 32)
828 | io_int_word, 1);
829 }
830 ret = 0;
831 }
832 return ret;
833}
834
20d695a9 835int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
0e60a699 836{
20d695a9 837 S390CPU *cpu = S390_CPU(cs);
0e60a699
AG
838 int ret = 0;
839
840 switch (run->exit_reason) {
841 case KVM_EXIT_S390_SIEIC:
1bc22652 842 ret = handle_intercept(cpu);
0e60a699
AG
843 break;
844 case KVM_EXIT_S390_RESET:
add142e0 845 qemu_system_reset_request();
0e60a699 846 break;
09b99878
CH
847 case KVM_EXIT_S390_TSCH:
848 ret = handle_tsch(cpu);
849 break;
0e60a699
AG
850 default:
851 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
852 break;
853 }
854
bb4ea393
JK
855 if (ret == 0) {
856 ret = EXCP_INTERRUPT;
bb4ea393 857 }
0e60a699
AG
858 return ret;
859}
4513d923 860
20d695a9 861bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
4513d923
GN
862{
863 return true;
864}
a1b87fe0 865
20d695a9 866int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
a1b87fe0
JK
867{
868 return 1;
869}
870
871int kvm_arch_on_sigbus(int code, void *addr)
872{
873 return 1;
874}
09b99878
CH
875
876void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id,
877 uint16_t subchannel_nr, uint32_t io_int_parm,
878 uint32_t io_int_word)
879{
880 uint32_t type;
881
882 type = ((subchannel_id & 0xff00) << 24) |
883 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
884 kvm_s390_interrupt_internal(cpu, type,
885 ((uint32_t)subchannel_id << 16) | subchannel_nr,
886 ((uint64_t)io_int_parm << 32) | io_int_word, 1);
887}
888
889void kvm_s390_crw_mchk(S390CPU *cpu)
890{
891 kvm_s390_interrupt_internal(cpu, KVM_S390_MCHK, 1 << 28,
892 0x00400f1d40330000, 1);
893}
894
895void kvm_s390_enable_css_support(S390CPU *cpu)
896{
897 struct kvm_enable_cap cap = {};
898 int r;
899
900 /* Activate host kernel channel subsystem support. */
901 cap.cap = KVM_CAP_S390_CSS_SUPPORT;
902 r = kvm_vcpu_ioctl(CPU(cpu), KVM_ENABLE_CAP, &cap);
903 assert(r == 0);
904}
48475e14
AK
905
906void kvm_arch_init_irq_routing(KVMState *s)
907{
908}
b4436a0b 909
cc3ac9c4
CH
910int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
911 int vq, bool assign)
b4436a0b
CH
912{
913 struct kvm_ioeventfd kick = {
914 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
915 KVM_IOEVENTFD_FLAG_DATAMATCH,
cc3ac9c4 916 .fd = event_notifier_get_fd(notifier),
b4436a0b
CH
917 .datamatch = vq,
918 .addr = sch,
919 .len = 8,
920 };
921 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
922 return -ENOSYS;
923 }
924 if (!assign) {
925 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
926 }
927 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
928}