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