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