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