]> Git Repo - qemu.git/blame - target-s390x/kvm.c
target-i386: the x86 CPL is stored in CS.selector - auto update hflags accordingly.
[qemu.git] / target-s390x / kvm.c
CommitLineData
0e60a699
AG
1/*
2 * QEMU S390x KVM implementation
3 *
4 * Copyright (c) 2009 Alexander Graf <[email protected]>
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
20d695a9 132int kvm_arch_put_registers(CPUState *cs, int level)
0e60a699 133{
20d695a9
AF
134 S390CPU *cpu = S390_CPU(cs);
135 CPUS390XState *env = &cpu->env;
5b08b344 136 struct kvm_sregs sregs;
0e60a699 137 struct kvm_regs regs;
860643bc 138 int r;
0e60a699
AG
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 }
860643bc
CB
154 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
155 if (r < 0) {
156 return r;
5b08b344 157 }
0e60a699
AG
158 }
159
44c68de0
DD
160 /* Do we need to save more than that? */
161 if (level == KVM_PUT_RUNTIME_STATE) {
162 return 0;
163 }
420840e5 164
860643bc
CB
165 /*
166 * These ONE_REGS are not protected by a capability. As they are only
167 * necessary for migration we just trace a possible error, but don't
168 * return with an error return code.
169 */
170 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
171 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
172 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
44b0c0bb
CB
173 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
174 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
0e60a699 175
819bd309 176 if (cap_async_pf) {
860643bc
CB
177 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
178 if (r < 0) {
179 return r;
819bd309 180 }
860643bc
CB
181 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
182 if (r < 0) {
183 return r;
819bd309 184 }
860643bc
CB
185 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
186 if (r < 0) {
187 return r;
819bd309
DD
188 }
189 }
190
5b08b344 191 if (cap_sync_regs &&
f7575c96
AF
192 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
193 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
5b08b344 194 for (i = 0; i < 16; i++) {
f7575c96
AF
195 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
196 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
5b08b344 197 }
f7575c96
AF
198 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
199 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
5b08b344
CB
200 } else {
201 for (i = 0; i < 16; i++) {
202 sregs.acrs[i] = env->aregs[i];
203 sregs.crs[i] = env->cregs[i];
204 }
860643bc
CB
205 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
206 if (r < 0) {
207 return r;
5b08b344
CB
208 }
209 }
0e60a699 210
5b08b344 211 /* Finally the prefix */
f7575c96
AF
212 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
213 cs->kvm_run->s.regs.prefix = env->psa;
214 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
5b08b344
CB
215 } else {
216 /* prefix is only supported via sync regs */
217 }
218 return 0;
0e60a699
AG
219}
220
20d695a9 221int kvm_arch_get_registers(CPUState *cs)
420840e5
JH
222{
223 S390CPU *cpu = S390_CPU(cs);
224 CPUS390XState *env = &cpu->env;
5b08b344 225 struct kvm_sregs sregs;
0e60a699 226 struct kvm_regs regs;
44c68de0 227 int i, r;
420840e5 228
5b08b344 229 /* get the PSW */
f7575c96
AF
230 env->psw.addr = cs->kvm_run->psw_addr;
231 env->psw.mask = cs->kvm_run->psw_mask;
5b08b344
CB
232
233 /* the GPRS */
f7575c96 234 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
5b08b344 235 for (i = 0; i < 16; i++) {
f7575c96 236 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
5b08b344
CB
237 }
238 } else {
44c68de0
DD
239 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
240 if (r < 0) {
241 return r;
5b08b344
CB
242 }
243 for (i = 0; i < 16; i++) {
244 env->regs[i] = regs.gprs[i];
245 }
0e60a699
AG
246 }
247
5b08b344
CB
248 /* The ACRS and CRS */
249 if (cap_sync_regs &&
f7575c96
AF
250 cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
251 cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
5b08b344 252 for (i = 0; i < 16; i++) {
f7575c96
AF
253 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
254 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
5b08b344
CB
255 }
256 } else {
44c68de0
DD
257 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
258 if (r < 0) {
259 return r;
5b08b344
CB
260 }
261 for (i = 0; i < 16; i++) {
262 env->aregs[i] = sregs.acrs[i];
263 env->cregs[i] = sregs.crs[i];
264 }
0e60a699
AG
265 }
266
44c68de0 267 /* The prefix */
f7575c96
AF
268 if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
269 env->psa = cs->kvm_run->s.regs.prefix;
5b08b344 270 }
0e60a699 271
860643bc
CB
272 /*
273 * These ONE_REGS are not protected by a capability. As they are only
274 * necessary for migration we just trace a possible error, but don't
275 * return with an error return code.
276 */
277 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
278 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
279 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
44b0c0bb
CB
280 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
281 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
44c68de0 282
819bd309 283 if (cap_async_pf) {
860643bc 284 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
819bd309
DD
285 if (r < 0) {
286 return r;
287 }
860643bc 288 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
819bd309
DD
289 if (r < 0) {
290 return r;
291 }
860643bc 292 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
819bd309
DD
293 if (r < 0) {
294 return r;
295 }
296 }
297
0e60a699
AG
298 return 0;
299}
300
fdec9918
CB
301/*
302 * Legacy layout for s390:
303 * Older S390 KVM requires the topmost vma of the RAM to be
304 * smaller than an system defined value, which is at least 256GB.
305 * Larger systems have larger values. We put the guest between
306 * the end of data segment (system break) and this value. We
307 * use 32GB as a base to have enough room for the system break
308 * to grow. We also have to use MAP parameters that avoid
309 * read-only mapping of guest pages.
310 */
575ddeb4 311static void *legacy_s390_alloc(size_t size)
fdec9918
CB
312{
313 void *mem;
314
315 mem = mmap((void *) 0x800000000ULL, size,
316 PROT_EXEC|PROT_READ|PROT_WRITE,
317 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
39228250 318 return mem == MAP_FAILED ? NULL : mem;
fdec9918
CB
319}
320
20d695a9 321int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
0e60a699
AG
322{
323 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
324
9282b73a
CB
325 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
326 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501, 4, 1)) {
0e60a699
AG
327 return -EINVAL;
328 }
329 return 0;
330}
331
20d695a9 332int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
0e60a699
AG
333{
334 uint8_t t[4];
335 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
336
9282b73a 337 if (cpu_memory_rw_debug(cs, bp->pc, t, 4, 0)) {
0e60a699
AG
338 return -EINVAL;
339 } else if (memcmp(t, diag_501, 4)) {
340 return -EINVAL;
9282b73a 341 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
0e60a699
AG
342 return -EINVAL;
343 }
344
345 return 0;
346}
347
8c012449
DH
348int kvm_arch_insert_hw_breakpoint(target_ulong addr,
349 target_ulong len, int type)
350{
351 return -ENOSYS;
352}
353
354int kvm_arch_remove_hw_breakpoint(target_ulong addr,
355 target_ulong len, int type)
356{
357 return -ENOSYS;
358}
359
360void kvm_arch_remove_all_hw_breakpoints(void)
361{
362}
363
364void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
365{
366}
367
20d695a9 368void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
0e60a699 369{
0e60a699
AG
370}
371
20d695a9 372void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
0e60a699 373{
0e60a699
AG
374}
375
20d695a9 376int kvm_arch_process_async_events(CPUState *cs)
0af691d7 377{
225dc991 378 return cs->halted;
0af691d7
MT
379}
380
1bc22652 381void kvm_s390_interrupt_internal(S390CPU *cpu, int type, uint32_t parm,
bcec36ea 382 uint64_t parm64, int vm)
0e60a699 383{
1bc22652 384 CPUState *cs = CPU(cpu);
0e60a699
AG
385 struct kvm_s390_interrupt kvmint;
386 int r;
387
a60f24b5 388 if (!cs->kvm_state) {
0e60a699
AG
389 return;
390 }
391
0e60a699
AG
392 kvmint.type = type;
393 kvmint.parm = parm;
394 kvmint.parm64 = parm64;
395
396 if (vm) {
a60f24b5 397 r = kvm_vm_ioctl(cs->kvm_state, KVM_S390_INTERRUPT, &kvmint);
0e60a699 398 } else {
1bc22652 399 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
0e60a699
AG
400 }
401
402 if (r < 0) {
403 fprintf(stderr, "KVM failed to inject interrupt\n");
404 exit(1);
405 }
406}
407
1bc22652 408void kvm_s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token)
0e60a699 409{
1bc22652 410 kvm_s390_interrupt_internal(cpu, KVM_S390_INT_VIRTIO, config_change,
0e60a699
AG
411 token, 1);
412}
413
1bc22652 414void kvm_s390_interrupt(S390CPU *cpu, int type, uint32_t code)
0e60a699 415{
1bc22652 416 kvm_s390_interrupt_internal(cpu, type, code, 0, 0);
0e60a699
AG
417}
418
1bc22652 419static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
0e60a699 420{
1bc22652 421 kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code);
0e60a699
AG
422}
423
1bc22652 424static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
bcec36ea 425 uint16_t ipbh0)
0e60a699 426{
1bc22652 427 CPUS390XState *env = &cpu->env;
a0fa2cb8
TH
428 uint64_t sccb;
429 uint32_t code;
0e60a699
AG
430 int r = 0;
431
cb446eca 432 cpu_synchronize_state(CPU(cpu));
0e60a699
AG
433 sccb = env->regs[ipbh0 & 0xf];
434 code = env->regs[(ipbh0 & 0xf0) >> 4];
435
6e252802 436 r = sclp_service_call(env, sccb, code);
9abf567d 437 if (r < 0) {
1bc22652 438 enter_pgmcheck(cpu, -r);
e8803d93
TH
439 } else {
440 setcc(cpu, r);
0e60a699 441 }
81f7c56c 442
0e60a699
AG
443 return 0;
444}
445
1eecf41b 446static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
09b99878 447{
09b99878 448 CPUS390XState *env = &cpu->env;
1eecf41b
FB
449 int rc = 0;
450 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
3474b679 451
44c68de0 452 cpu_synchronize_state(CPU(cpu));
3474b679 453
09b99878 454 switch (ipa1) {
1eecf41b 455 case PRIV_B2_XSCH:
5d9bf1c0 456 ioinst_handle_xsch(cpu, env->regs[1]);
09b99878 457 break;
1eecf41b 458 case PRIV_B2_CSCH:
5d9bf1c0 459 ioinst_handle_csch(cpu, env->regs[1]);
09b99878 460 break;
1eecf41b 461 case PRIV_B2_HSCH:
5d9bf1c0 462 ioinst_handle_hsch(cpu, env->regs[1]);
09b99878 463 break;
1eecf41b 464 case PRIV_B2_MSCH:
5d9bf1c0 465 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 466 break;
1eecf41b 467 case PRIV_B2_SSCH:
5d9bf1c0 468 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 469 break;
1eecf41b 470 case PRIV_B2_STCRW:
5d9bf1c0 471 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
09b99878 472 break;
1eecf41b 473 case PRIV_B2_STSCH:
5d9bf1c0 474 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 475 break;
1eecf41b 476 case PRIV_B2_TSCH:
09b99878
CH
477 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
478 fprintf(stderr, "Spurious tsch intercept\n");
479 break;
1eecf41b 480 case PRIV_B2_CHSC:
5d9bf1c0 481 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
09b99878 482 break;
1eecf41b 483 case PRIV_B2_TPI:
09b99878
CH
484 /* This should have been handled by kvm already. */
485 fprintf(stderr, "Spurious tpi intercept\n");
486 break;
1eecf41b 487 case PRIV_B2_SCHM:
5d9bf1c0
TH
488 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
489 run->s390_sieic.ipb);
09b99878 490 break;
1eecf41b 491 case PRIV_B2_RSCH:
5d9bf1c0 492 ioinst_handle_rsch(cpu, env->regs[1]);
09b99878 493 break;
1eecf41b 494 case PRIV_B2_RCHP:
5d9bf1c0 495 ioinst_handle_rchp(cpu, env->regs[1]);
09b99878 496 break;
1eecf41b 497 case PRIV_B2_STCPS:
09b99878 498 /* We do not provide this instruction, it is suppressed. */
09b99878 499 break;
1eecf41b 500 case PRIV_B2_SAL:
5d9bf1c0 501 ioinst_handle_sal(cpu, env->regs[1]);
09b99878 502 break;
1eecf41b 503 case PRIV_B2_SIGA:
c1e8dfb5 504 /* Not provided, set CC = 3 for subchannel not operational */
5d9bf1c0 505 setcc(cpu, 3);
09b99878 506 break;
1eecf41b
FB
507 case PRIV_B2_SCLP_CALL:
508 rc = kvm_sclp_service_call(cpu, run, ipbh0);
509 break;
c1e8dfb5 510 default:
1eecf41b
FB
511 rc = -1;
512 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
513 break;
09b99878
CH
514 }
515
1eecf41b 516 return rc;
09b99878
CH
517}
518
1eecf41b 519static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
0e60a699
AG
520{
521 int r = 0;
0e60a699 522
0e60a699 523 switch (ipa1) {
1eecf41b
FB
524 case PRIV_B9_EQBS:
525 /* just inject exception */
526 r = -1;
527 break;
528 default:
529 r = -1;
530 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
531 break;
532 }
533
534 return r;
535}
536
537static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
538{
539 int r = 0;
540
541 switch (ipa1) {
542 case PRIV_EB_SQBS:
543 /* just inject exception */
544 r = -1;
545 break;
546 default:
547 r = -1;
548 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipa1);
549 break;
0e60a699
AG
550 }
551
552 return r;
553}
554
4fd6dd06 555static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
0e60a699 556{
4fd6dd06 557 CPUS390XState *env = &cpu->env;
77319f22 558 int ret;
3474b679 559
44c68de0 560 cpu_synchronize_state(CPU(cpu));
77319f22
TH
561 ret = s390_virtio_hypercall(env);
562 if (ret == -EINVAL) {
563 enter_pgmcheck(cpu, PGM_SPECIFICATION);
564 return 0;
565 }
0e60a699 566
77319f22 567 return ret;
0e60a699
AG
568}
569
268846ba
ED
570static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
571{
572 uint64_t r1, r3;
573
574 cpu_synchronize_state(CPU(cpu));
575 r1 = (run->s390_sieic.ipa & 0x00f0) >> 8;
576 r3 = run->s390_sieic.ipa & 0x000f;
577 handle_diag_308(&cpu->env, r1, r3);
578}
579
638129ff
CH
580#define DIAG_KVM_CODE_MASK 0x000000000000ffff
581
582static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
0e60a699
AG
583{
584 int r = 0;
638129ff
CH
585 uint16_t func_code;
586
587 /*
588 * For any diagnose call we support, bits 48-63 of the resulting
589 * address specify the function code; the remainder is ignored.
590 */
591 func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
592 switch (func_code) {
268846ba
ED
593 case DIAG_IPL:
594 kvm_handle_diag_308(cpu, run);
595 break;
39fbc5c6
CB
596 case DIAG_KVM_HYPERCALL:
597 r = handle_hypercall(cpu, run);
598 break;
599 case DIAG_KVM_BREAKPOINT:
600 sleep(10);
601 break;
602 default:
638129ff 603 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
39fbc5c6
CB
604 r = -1;
605 break;
0e60a699
AG
606 }
607
608 return r;
609}
610
b20a461f
TH
611static int kvm_s390_cpu_start(S390CPU *cpu)
612{
613 s390_add_running_cpu(cpu);
614 qemu_cpu_kick(CPU(cpu));
615 DPRINTF("DONE: KVM cpu start: %p\n", &cpu->env);
616 return 0;
617}
618
7f7f9752 619int kvm_s390_cpu_restart(S390CPU *cpu)
0e60a699 620{
1bc22652 621 kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0);
49e15878 622 s390_add_running_cpu(cpu);
c08d7424 623 qemu_cpu_kick(CPU(cpu));
7f7f9752 624 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
0e60a699
AG
625 return 0;
626}
627
f7d3e466 628static void sigp_initial_cpu_reset(void *arg)
0e60a699 629{
f7d3e466
TH
630 CPUState *cpu = arg;
631 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
d5900813 632
f7d3e466
TH
633 cpu_synchronize_state(cpu);
634 scc->initial_cpu_reset(cpu);
0e60a699
AG
635}
636
04c2b516
TH
637static void sigp_cpu_reset(void *arg)
638{
639 CPUState *cpu = arg;
640 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
641
642 cpu_synchronize_state(cpu);
643 scc->cpu_reset(cpu);
644}
645
b8031adb
TH
646#define SIGP_ORDER_MASK 0x000000ff
647
f7575c96 648static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
0e60a699 649{
f7575c96 650 CPUS390XState *env = &cpu->env;
0e60a699 651 uint8_t order_code;
0e60a699 652 uint16_t cpu_addr;
45fa769b 653 S390CPU *target_cpu;
3796f0e1
TH
654 uint64_t *statusreg = &env->regs[ipa1 >> 4];
655 int cc;
0e60a699 656
cb446eca 657 cpu_synchronize_state(CPU(cpu));
0e60a699
AG
658
659 /* get order code */
b8031adb 660 order_code = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
0e60a699 661
0e60a699 662 cpu_addr = env->regs[ipa1 & 0x0f];
45fa769b
AF
663 target_cpu = s390_cpu_addr2state(cpu_addr);
664 if (target_cpu == NULL) {
3796f0e1 665 cc = 3; /* not operational */
0e60a699
AG
666 goto out;
667 }
668
669 switch (order_code) {
b20a461f 670 case SIGP_START:
3796f0e1 671 cc = kvm_s390_cpu_start(target_cpu);
b20a461f 672 break;
0b9972a2 673 case SIGP_RESTART:
3796f0e1 674 cc = kvm_s390_cpu_restart(target_cpu);
0b9972a2
TH
675 break;
676 case SIGP_SET_ARCH:
0788082a
TH
677 *statusreg &= 0xffffffff00000000UL;
678 *statusreg |= SIGP_STAT_INVALID_PARAMETER;
679 cc = 1; /* status stored */
680 break;
0b9972a2 681 case SIGP_INITIAL_CPU_RESET:
f7d3e466
TH
682 run_on_cpu(CPU(target_cpu), sigp_initial_cpu_reset, CPU(target_cpu));
683 cc = 0;
0b9972a2 684 break;
04c2b516
TH
685 case SIGP_CPU_RESET:
686 run_on_cpu(CPU(target_cpu), sigp_cpu_reset, CPU(target_cpu));
687 cc = 0;
688 break;
0b9972a2 689 default:
3796f0e1
TH
690 DPRINTF("KVM: unknown SIGP: 0x%x\n", order_code);
691 *statusreg &= 0xffffffff00000000UL;
692 *statusreg |= SIGP_STAT_INVALID_ORDER;
693 cc = 1; /* status stored */
0b9972a2 694 break;
0e60a699
AG
695 }
696
697out:
3796f0e1 698 setcc(cpu, cc);
0e60a699
AG
699 return 0;
700}
701
d2ee7746 702static void handle_instruction(S390CPU *cpu, struct kvm_run *run)
0e60a699
AG
703{
704 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
705 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
d7963c43 706 int r = -1;
0e60a699 707
e67137c6
PM
708 DPRINTF("handle_instruction 0x%x 0x%x\n",
709 run->s390_sieic.ipa, run->s390_sieic.ipb);
0e60a699 710 switch (ipa0) {
09b99878 711 case IPA0_B2:
1eecf41b
FB
712 r = handle_b2(cpu, run, ipa1);
713 break;
09b99878 714 case IPA0_B9:
1eecf41b
FB
715 r = handle_b9(cpu, run, ipa1);
716 break;
09b99878 717 case IPA0_EB:
1eecf41b 718 r = handle_eb(cpu, run, ipa1);
09b99878
CH
719 break;
720 case IPA0_DIAG:
638129ff 721 r = handle_diag(cpu, run, run->s390_sieic.ipb);
09b99878
CH
722 break;
723 case IPA0_SIGP:
724 r = handle_sigp(cpu, run, ipa1);
725 break;
0e60a699
AG
726 }
727
728 if (r < 0) {
1bc22652 729 enter_pgmcheck(cpu, 0x0001);
0e60a699 730 }
0e60a699
AG
731}
732
f7575c96 733static bool is_special_wait_psw(CPUState *cs)
eca3ed03
CB
734{
735 /* signal quiesce */
f7575c96 736 return cs->kvm_run->psw_addr == 0xfffUL;
eca3ed03
CB
737}
738
1bc22652 739static int handle_intercept(S390CPU *cpu)
0e60a699 740{
f7575c96
AF
741 CPUState *cs = CPU(cpu);
742 struct kvm_run *run = cs->kvm_run;
0e60a699
AG
743 int icpt_code = run->s390_sieic.icptcode;
744 int r = 0;
745
e67137c6 746 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
f7575c96 747 (long)cs->kvm_run->psw_addr);
0e60a699
AG
748 switch (icpt_code) {
749 case ICPT_INSTRUCTION:
d2ee7746 750 handle_instruction(cpu, run);
0e60a699
AG
751 break;
752 case ICPT_WAITPSW:
08eb8c85
CB
753 /* disabled wait, since enabled wait is handled in kernel */
754 if (s390_del_running_cpu(cpu) == 0) {
755 if (is_special_wait_psw(cs)) {
756 qemu_system_shutdown_request();
757 } else {
758 QObject *data;
759
760 data = qobject_from_jsonf("{ 'action': %s }", "pause");
761 monitor_protocol_event(QEVENT_GUEST_PANICKED, data);
762 qobject_decref(data);
763 vm_stop(RUN_STATE_GUEST_PANICKED);
764 }
eca3ed03
CB
765 }
766 r = EXCP_HALTED;
767 break;
854e42f3 768 case ICPT_CPU_STOP:
49e15878 769 if (s390_del_running_cpu(cpu) == 0) {
854e42f3
CB
770 qemu_system_shutdown_request();
771 }
772 r = EXCP_HALTED;
0e60a699
AG
773 break;
774 case ICPT_SOFT_INTERCEPT:
775 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
776 exit(1);
777 break;
0e60a699
AG
778 case ICPT_IO:
779 fprintf(stderr, "KVM unimplemented icpt IO\n");
780 exit(1);
781 break;
782 default:
783 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
784 exit(1);
785 break;
786 }
787
788 return r;
789}
790
09b99878
CH
791static int handle_tsch(S390CPU *cpu)
792{
793 CPUS390XState *env = &cpu->env;
794 CPUState *cs = CPU(cpu);
795 struct kvm_run *run = cs->kvm_run;
796 int ret;
797
44c68de0 798 cpu_synchronize_state(cs);
3474b679 799
09b99878
CH
800 ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
801 if (ret >= 0) {
802 /* Success; set condition code. */
803 setcc(cpu, ret);
804 ret = 0;
805 } else if (ret < -1) {
806 /*
807 * Failure.
808 * If an I/O interrupt had been dequeued, we have to reinject it.
809 */
810 if (run->s390_tsch.dequeued) {
811 uint16_t subchannel_id = run->s390_tsch.subchannel_id;
812 uint16_t subchannel_nr = run->s390_tsch.subchannel_nr;
813 uint32_t io_int_parm = run->s390_tsch.io_int_parm;
814 uint32_t io_int_word = run->s390_tsch.io_int_word;
815 uint32_t type = ((subchannel_id & 0xff00) << 24) |
816 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
817
818 kvm_s390_interrupt_internal(cpu, type,
819 ((uint32_t)subchannel_id << 16)
820 | subchannel_nr,
821 ((uint64_t)io_int_parm << 32)
822 | io_int_word, 1);
823 }
824 ret = 0;
825 }
826 return ret;
827}
828
8c012449
DH
829static int kvm_arch_handle_debug_exit(S390CPU *cpu)
830{
831 return -ENOSYS;
832}
833
20d695a9 834int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
0e60a699 835{
20d695a9 836 S390CPU *cpu = S390_CPU(cs);
0e60a699
AG
837 int ret = 0;
838
839 switch (run->exit_reason) {
840 case KVM_EXIT_S390_SIEIC:
1bc22652 841 ret = handle_intercept(cpu);
0e60a699
AG
842 break;
843 case KVM_EXIT_S390_RESET:
add142e0 844 qemu_system_reset_request();
0e60a699 845 break;
09b99878
CH
846 case KVM_EXIT_S390_TSCH:
847 ret = handle_tsch(cpu);
848 break;
8c012449
DH
849 case KVM_EXIT_DEBUG:
850 ret = kvm_arch_handle_debug_exit(cpu);
851 break;
0e60a699
AG
852 default:
853 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
854 break;
855 }
856
bb4ea393
JK
857 if (ret == 0) {
858 ret = EXCP_INTERRUPT;
bb4ea393 859 }
0e60a699
AG
860 return ret;
861}
4513d923 862
20d695a9 863bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
4513d923
GN
864{
865 return true;
866}
a1b87fe0 867
20d695a9 868int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
a1b87fe0
JK
869{
870 return 1;
871}
872
873int kvm_arch_on_sigbus(int code, void *addr)
874{
875 return 1;
876}
09b99878
CH
877
878void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id,
879 uint16_t subchannel_nr, uint32_t io_int_parm,
880 uint32_t io_int_word)
881{
882 uint32_t type;
883
7e749462
CH
884 if (io_int_word & IO_INT_WORD_AI) {
885 type = KVM_S390_INT_IO(1, 0, 0, 0);
886 } else {
887 type = ((subchannel_id & 0xff00) << 24) |
888 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
889 }
09b99878
CH
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{
09b99878
CH
903 int r;
904
905 /* Activate host kernel channel subsystem support. */
e080f0fd 906 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
09b99878
CH
907 assert(r == 0);
908}
48475e14
AK
909
910void kvm_arch_init_irq_routing(KVMState *s)
911{
912}
b4436a0b 913
cc3ac9c4
CH
914int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
915 int vq, bool assign)
b4436a0b
CH
916{
917 struct kvm_ioeventfd kick = {
918 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
919 KVM_IOEVENTFD_FLAG_DATAMATCH,
cc3ac9c4 920 .fd = event_notifier_get_fd(notifier),
b4436a0b
CH
921 .datamatch = vq,
922 .addr = sch,
923 .len = 8,
924 };
925 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
926 return -ENOSYS;
927 }
928 if (!assign) {
929 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
930 }
931 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
932}
This page took 0.626082 seconds and 4 git commands to generate.