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