]> Git Repo - qemu.git/blame - target-s390x/kvm.c
s390x/kvm: Put vm name, extended name and UUID into STSI322 SYSIB
[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"
4cb88c3c 35#include "hw/hw.h"
0e60a699 36#include "cpu.h"
9c17d615 37#include "sysemu/device_tree.h"
08eb8c85
CB
38#include "qapi/qmp/qjson.h"
39#include "monitor/monitor.h"
770a6379 40#include "exec/gdbstub.h"
18ff9494 41#include "exec/address-spaces.h"
860643bc 42#include "trace.h"
3a449690 43#include "qapi-event.h"
863f6f52 44#include "hw/s390x/s390-pci-inst.h"
9e03a040 45#include "hw/s390x/s390-pci-bus.h"
e91e972c 46#include "hw/s390x/ipl.h"
f07177a5 47#include "hw/s390x/ebcdic.h"
0e60a699
AG
48
49/* #define DEBUG_KVM */
50
51#ifdef DEBUG_KVM
e67137c6 52#define DPRINTF(fmt, ...) \
0e60a699
AG
53 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
54#else
e67137c6 55#define DPRINTF(fmt, ...) \
0e60a699
AG
56 do { } while (0)
57#endif
58
2b147555
DD
59#define kvm_vm_check_mem_attr(s, attr) \
60 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
61
0e60a699
AG
62#define IPA0_DIAG 0x8300
63#define IPA0_SIGP 0xae00
09b99878
CH
64#define IPA0_B2 0xb200
65#define IPA0_B9 0xb900
66#define IPA0_EB 0xeb00
863f6f52 67#define IPA0_E3 0xe300
0e60a699 68
1eecf41b
FB
69#define PRIV_B2_SCLP_CALL 0x20
70#define PRIV_B2_CSCH 0x30
71#define PRIV_B2_HSCH 0x31
72#define PRIV_B2_MSCH 0x32
73#define PRIV_B2_SSCH 0x33
74#define PRIV_B2_STSCH 0x34
75#define PRIV_B2_TSCH 0x35
76#define PRIV_B2_TPI 0x36
77#define PRIV_B2_SAL 0x37
78#define PRIV_B2_RSCH 0x38
79#define PRIV_B2_STCRW 0x39
80#define PRIV_B2_STCPS 0x3a
81#define PRIV_B2_RCHP 0x3b
82#define PRIV_B2_SCHM 0x3c
83#define PRIV_B2_CHSC 0x5f
84#define PRIV_B2_SIGA 0x74
85#define PRIV_B2_XSCH 0x76
86
87#define PRIV_EB_SQBS 0x8a
863f6f52
FB
88#define PRIV_EB_PCISTB 0xd0
89#define PRIV_EB_SIC 0xd1
1eecf41b
FB
90
91#define PRIV_B9_EQBS 0x9c
863f6f52
FB
92#define PRIV_B9_CLP 0xa0
93#define PRIV_B9_PCISTG 0xd0
94#define PRIV_B9_PCILG 0xd2
95#define PRIV_B9_RPCIT 0xd3
96
97#define PRIV_E3_MPCIFC 0xd0
98#define PRIV_E3_STPCIFC 0xd4
1eecf41b 99
268846ba 100#define DIAG_IPL 0x308
0e60a699
AG
101#define DIAG_KVM_HYPERCALL 0x500
102#define DIAG_KVM_BREAKPOINT 0x501
103
0e60a699 104#define ICPT_INSTRUCTION 0x04
6449a41a 105#define ICPT_PROGRAM 0x08
a2689242 106#define ICPT_EXT_INT 0x14
0e60a699
AG
107#define ICPT_WAITPSW 0x1c
108#define ICPT_SOFT_INTERCEPT 0x24
109#define ICPT_CPU_STOP 0x28
110#define ICPT_IO 0x40
111
770a6379
DH
112static CPUWatchpoint hw_watchpoint;
113/*
114 * We don't use a list because this structure is also used to transmit the
115 * hardware breakpoints to the kernel.
116 */
117static struct kvm_hw_breakpoint *hw_breakpoints;
118static int nb_hw_breakpoints;
119
94a8d39a
JK
120const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
121 KVM_CAP_LAST_INFO
122};
123
5b08b344 124static int cap_sync_regs;
819bd309 125static int cap_async_pf;
5b08b344 126
dc622deb 127static void *legacy_s390_alloc(size_t size, uint64_t *align);
91138037 128
a310b283
DD
129static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
130{
131 struct kvm_device_attr attr = {
132 .group = KVM_S390_VM_MEM_CTRL,
133 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
134 .addr = (uint64_t) memory_limit,
135 };
136
137 return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
138}
139
140int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
141{
142 int rc;
143
144 struct kvm_device_attr attr = {
145 .group = KVM_S390_VM_MEM_CTRL,
146 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
147 .addr = (uint64_t) &new_limit,
148 };
149
2b147555 150 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_LIMIT_SIZE)) {
a310b283
DD
151 return 0;
152 }
153
154 rc = kvm_s390_query_mem_limit(s, hw_limit);
155 if (rc) {
156 return rc;
157 } else if (*hw_limit < new_limit) {
158 return -E2BIG;
159 }
160
161 return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
162}
163
4cb88c3c
DD
164void kvm_s390_clear_cmma_callback(void *opaque)
165{
166 int rc;
167 KVMState *s = opaque;
168 struct kvm_device_attr attr = {
169 .group = KVM_S390_VM_MEM_CTRL,
170 .attr = KVM_S390_VM_MEM_CLR_CMMA,
171 };
172
173 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
174 trace_kvm_clear_cmma(rc);
175}
176
177static void kvm_s390_enable_cmma(KVMState *s)
178{
179 int rc;
180 struct kvm_device_attr attr = {
181 .group = KVM_S390_VM_MEM_CTRL,
182 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
183 };
184
2b147555
DD
185 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_ENABLE_CMMA) ||
186 !kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_CLR_CMMA)) {
4cb88c3c
DD
187 return;
188 }
189
190 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
191 if (!rc) {
192 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
193 }
194 trace_kvm_enable_cmma(rc);
195}
196
2eb1cd07
TK
197static void kvm_s390_set_attr(uint64_t attr)
198{
199 struct kvm_device_attr attribute = {
200 .group = KVM_S390_VM_CRYPTO,
201 .attr = attr,
202 };
203
204 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
205
206 if (ret) {
207 error_report("Failed to set crypto device attribute %lu: %s",
208 attr, strerror(-ret));
209 }
210}
211
212static void kvm_s390_init_aes_kw(void)
213{
214 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
215
216 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
217 NULL)) {
218 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
219 }
220
221 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
222 kvm_s390_set_attr(attr);
223 }
224}
225
226static void kvm_s390_init_dea_kw(void)
227{
228 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
229
230 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
231 NULL)) {
232 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
233 }
234
235 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
236 kvm_s390_set_attr(attr);
237 }
238}
239
240static void kvm_s390_init_crypto(void)
241{
242 kvm_s390_init_aes_kw();
243 kvm_s390_init_dea_kw();
244}
245
b16565b3 246int kvm_arch_init(MachineState *ms, KVMState *s)
0e60a699 247{
5b08b344 248 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
819bd309 249 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
4cb88c3c 250
2b147555 251 kvm_s390_enable_cmma(s);
4cb88c3c 252
91138037
MA
253 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
254 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
255 phys_mem_set_alloc(legacy_s390_alloc);
256 }
f16d3f58
DH
257
258 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
f07177a5 259 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
f16d3f58 260
0e60a699
AG
261 return 0;
262}
263
b164e48e
EH
264unsigned long kvm_arch_vcpu_id(CPUState *cpu)
265{
266 return cpu->cpu_index;
267}
268
c9e659c9 269int kvm_arch_init_vcpu(CPUState *cs)
0e60a699 270{
c9e659c9
DH
271 S390CPU *cpu = S390_CPU(cs);
272 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
1c9d2a1d 273 return 0;
0e60a699
AG
274}
275
50a2c6e5 276void kvm_s390_reset_vcpu(S390CPU *cpu)
0e60a699 277{
50a2c6e5
PB
278 CPUState *cs = CPU(cpu);
279
419831d7
AG
280 /* The initial reset call is needed here to reset in-kernel
281 * vcpu data that we can't access directly from QEMU
282 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
283 * Before this ioctl cpu_synchronize_state() is called in common kvm
284 * code (kvm-all) */
50a2c6e5 285 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
81b07353 286 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
70bada03 287 }
2eb1cd07
TK
288
289 kvm_s390_init_crypto();
0e60a699
AG
290}
291
fdb78ec0
DH
292static int can_sync_regs(CPUState *cs, int regs)
293{
294 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
295}
296
20d695a9 297int kvm_arch_put_registers(CPUState *cs, int level)
0e60a699 298{
20d695a9
AF
299 S390CPU *cpu = S390_CPU(cs);
300 CPUS390XState *env = &cpu->env;
5b08b344 301 struct kvm_sregs sregs;
0e60a699 302 struct kvm_regs regs;
e6eef7c2 303 struct kvm_fpu fpu = {};
860643bc 304 int r;
0e60a699
AG
305 int i;
306
5b08b344 307 /* always save the PSW and the GPRS*/
f7575c96
AF
308 cs->kvm_run->psw_addr = env->psw.addr;
309 cs->kvm_run->psw_mask = env->psw.mask;
0e60a699 310
fdb78ec0 311 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
5b08b344 312 for (i = 0; i < 16; i++) {
f7575c96
AF
313 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
314 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
5b08b344
CB
315 }
316 } else {
317 for (i = 0; i < 16; i++) {
318 regs.gprs[i] = env->regs[i];
319 }
860643bc
CB
320 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
321 if (r < 0) {
322 return r;
5b08b344 323 }
0e60a699
AG
324 }
325
85ad6230
JH
326 /* Floating point */
327 for (i = 0; i < 16; i++) {
328 fpu.fprs[i] = env->fregs[i].ll;
329 }
330 fpu.fpc = env->fpc;
331
332 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
333 if (r < 0) {
334 return r;
335 }
336
44c68de0
DD
337 /* Do we need to save more than that? */
338 if (level == KVM_PUT_RUNTIME_STATE) {
339 return 0;
340 }
420840e5 341
59ac1532
DH
342 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
343 cs->kvm_run->s.regs.cputm = env->cputm;
344 cs->kvm_run->s.regs.ckc = env->ckc;
345 cs->kvm_run->s.regs.todpr = env->todpr;
346 cs->kvm_run->s.regs.gbea = env->gbea;
347 cs->kvm_run->s.regs.pp = env->pp;
348 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
349 } else {
350 /*
351 * These ONE_REGS are not protected by a capability. As they are only
352 * necessary for migration we just trace a possible error, but don't
353 * return with an error return code.
354 */
355 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
356 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
357 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
358 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
359 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
360 }
361
362 /* pfault parameters */
363 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
364 cs->kvm_run->s.regs.pft = env->pfault_token;
365 cs->kvm_run->s.regs.pfs = env->pfault_select;
366 cs->kvm_run->s.regs.pfc = env->pfault_compare;
367 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
368 } else if (cap_async_pf) {
860643bc
CB
369 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
370 if (r < 0) {
371 return r;
819bd309 372 }
860643bc
CB
373 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
374 if (r < 0) {
375 return r;
819bd309 376 }
860643bc
CB
377 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
378 if (r < 0) {
379 return r;
819bd309
DD
380 }
381 }
382
fdb78ec0
DH
383 /* access registers and control registers*/
384 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
5b08b344 385 for (i = 0; i < 16; i++) {
f7575c96
AF
386 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
387 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
5b08b344 388 }
f7575c96
AF
389 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
390 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
5b08b344
CB
391 } else {
392 for (i = 0; i < 16; i++) {
393 sregs.acrs[i] = env->aregs[i];
394 sregs.crs[i] = env->cregs[i];
395 }
860643bc
CB
396 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
397 if (r < 0) {
398 return r;
5b08b344
CB
399 }
400 }
0e60a699 401
5b08b344 402 /* Finally the prefix */
fdb78ec0 403 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
f7575c96
AF
404 cs->kvm_run->s.regs.prefix = env->psa;
405 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
5b08b344
CB
406 } else {
407 /* prefix is only supported via sync regs */
408 }
409 return 0;
0e60a699
AG
410}
411
20d695a9 412int kvm_arch_get_registers(CPUState *cs)
420840e5
JH
413{
414 S390CPU *cpu = S390_CPU(cs);
415 CPUS390XState *env = &cpu->env;
5b08b344 416 struct kvm_sregs sregs;
0e60a699 417 struct kvm_regs regs;
85ad6230 418 struct kvm_fpu fpu;
44c68de0 419 int i, r;
420840e5 420
5b08b344 421 /* get the PSW */
f7575c96
AF
422 env->psw.addr = cs->kvm_run->psw_addr;
423 env->psw.mask = cs->kvm_run->psw_mask;
5b08b344
CB
424
425 /* the GPRS */
fdb78ec0 426 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
5b08b344 427 for (i = 0; i < 16; i++) {
f7575c96 428 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
5b08b344
CB
429 }
430 } else {
44c68de0
DD
431 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
432 if (r < 0) {
433 return r;
5b08b344
CB
434 }
435 for (i = 0; i < 16; i++) {
436 env->regs[i] = regs.gprs[i];
437 }
0e60a699
AG
438 }
439
5b08b344 440 /* The ACRS and CRS */
fdb78ec0 441 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
5b08b344 442 for (i = 0; i < 16; i++) {
f7575c96
AF
443 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
444 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
5b08b344
CB
445 }
446 } else {
44c68de0
DD
447 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
448 if (r < 0) {
449 return r;
5b08b344
CB
450 }
451 for (i = 0; i < 16; i++) {
452 env->aregs[i] = sregs.acrs[i];
453 env->cregs[i] = sregs.crs[i];
454 }
0e60a699
AG
455 }
456
85ad6230
JH
457 /* Floating point */
458 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
459 if (r < 0) {
460 return r;
461 }
462 for (i = 0; i < 16; i++) {
463 env->fregs[i].ll = fpu.fprs[i];
464 }
465 env->fpc = fpu.fpc;
466
44c68de0 467 /* The prefix */
fdb78ec0 468 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
f7575c96 469 env->psa = cs->kvm_run->s.regs.prefix;
5b08b344 470 }
0e60a699 471
59ac1532
DH
472 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
473 env->cputm = cs->kvm_run->s.regs.cputm;
474 env->ckc = cs->kvm_run->s.regs.ckc;
475 env->todpr = cs->kvm_run->s.regs.todpr;
476 env->gbea = cs->kvm_run->s.regs.gbea;
477 env->pp = cs->kvm_run->s.regs.pp;
478 } else {
479 /*
480 * These ONE_REGS are not protected by a capability. As they are only
481 * necessary for migration we just trace a possible error, but don't
482 * return with an error return code.
483 */
484 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
485 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
486 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
487 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
488 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
489 }
490
491 /* pfault parameters */
492 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
493 env->pfault_token = cs->kvm_run->s.regs.pft;
494 env->pfault_select = cs->kvm_run->s.regs.pfs;
495 env->pfault_compare = cs->kvm_run->s.regs.pfc;
496 } else if (cap_async_pf) {
860643bc 497 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
819bd309
DD
498 if (r < 0) {
499 return r;
500 }
860643bc 501 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
819bd309
DD
502 if (r < 0) {
503 return r;
504 }
860643bc 505 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
819bd309
DD
506 if (r < 0) {
507 return r;
508 }
509 }
510
0e60a699
AG
511 return 0;
512}
513
3f9e59bb
JH
514int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
515{
516 int r;
517 struct kvm_device_attr attr = {
518 .group = KVM_S390_VM_TOD,
519 .attr = KVM_S390_VM_TOD_LOW,
520 .addr = (uint64_t)tod_low,
521 };
522
523 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
524 if (r) {
525 return r;
526 }
527
528 attr.attr = KVM_S390_VM_TOD_HIGH;
529 attr.addr = (uint64_t)tod_high;
530 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
531}
532
533int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
534{
535 int r;
536
537 struct kvm_device_attr attr = {
538 .group = KVM_S390_VM_TOD,
539 .attr = KVM_S390_VM_TOD_LOW,
540 .addr = (uint64_t)tod_low,
541 };
542
543 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
544 if (r) {
545 return r;
546 }
547
548 attr.attr = KVM_S390_VM_TOD_HIGH;
549 attr.addr = (uint64_t)tod_high;
550 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
551}
552
fdec9918
CB
553/*
554 * Legacy layout for s390:
555 * Older S390 KVM requires the topmost vma of the RAM to be
556 * smaller than an system defined value, which is at least 256GB.
557 * Larger systems have larger values. We put the guest between
558 * the end of data segment (system break) and this value. We
559 * use 32GB as a base to have enough room for the system break
560 * to grow. We also have to use MAP parameters that avoid
561 * read-only mapping of guest pages.
562 */
dc622deb 563static void *legacy_s390_alloc(size_t size, uint64_t *align)
fdec9918
CB
564{
565 void *mem;
566
567 mem = mmap((void *) 0x800000000ULL, size,
568 PROT_EXEC|PROT_READ|PROT_WRITE,
569 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
39228250 570 return mem == MAP_FAILED ? NULL : mem;
fdec9918
CB
571}
572
8e4e86af
DH
573/* DIAG 501 is used for sw breakpoints */
574static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
575
20d695a9 576int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
0e60a699 577{
0e60a699 578
8e4e86af
DH
579 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
580 sizeof(diag_501), 0) ||
581 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
582 sizeof(diag_501), 1)) {
0e60a699
AG
583 return -EINVAL;
584 }
585 return 0;
586}
587
20d695a9 588int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
0e60a699 589{
8e4e86af 590 uint8_t t[sizeof(diag_501)];
0e60a699 591
8e4e86af 592 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
0e60a699 593 return -EINVAL;
8e4e86af 594 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
0e60a699 595 return -EINVAL;
8e4e86af
DH
596 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
597 sizeof(diag_501), 1)) {
0e60a699
AG
598 return -EINVAL;
599 }
600
601 return 0;
602}
603
770a6379
DH
604static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
605 int len, int type)
606{
607 int n;
608
609 for (n = 0; n < nb_hw_breakpoints; n++) {
610 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
611 (hw_breakpoints[n].len == len || len == -1)) {
612 return &hw_breakpoints[n];
613 }
614 }
615
616 return NULL;
617}
618
619static int insert_hw_breakpoint(target_ulong addr, int len, int type)
620{
621 int size;
622
623 if (find_hw_breakpoint(addr, len, type)) {
624 return -EEXIST;
625 }
626
627 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
628
629 if (!hw_breakpoints) {
630 nb_hw_breakpoints = 0;
631 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
632 } else {
633 hw_breakpoints =
634 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
635 }
636
637 if (!hw_breakpoints) {
638 nb_hw_breakpoints = 0;
639 return -ENOMEM;
640 }
641
642 hw_breakpoints[nb_hw_breakpoints].addr = addr;
643 hw_breakpoints[nb_hw_breakpoints].len = len;
644 hw_breakpoints[nb_hw_breakpoints].type = type;
645
646 nb_hw_breakpoints++;
647
648 return 0;
649}
650
8c012449
DH
651int kvm_arch_insert_hw_breakpoint(target_ulong addr,
652 target_ulong len, int type)
653{
770a6379
DH
654 switch (type) {
655 case GDB_BREAKPOINT_HW:
656 type = KVM_HW_BP;
657 break;
658 case GDB_WATCHPOINT_WRITE:
659 if (len < 1) {
660 return -EINVAL;
661 }
662 type = KVM_HW_WP_WRITE;
663 break;
664 default:
665 return -ENOSYS;
666 }
667 return insert_hw_breakpoint(addr, len, type);
8c012449
DH
668}
669
670int kvm_arch_remove_hw_breakpoint(target_ulong addr,
671 target_ulong len, int type)
672{
770a6379
DH
673 int size;
674 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
675
676 if (bp == NULL) {
677 return -ENOENT;
678 }
679
680 nb_hw_breakpoints--;
681 if (nb_hw_breakpoints > 0) {
682 /*
683 * In order to trim the array, move the last element to the position to
684 * be removed - if necessary.
685 */
686 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
687 *bp = hw_breakpoints[nb_hw_breakpoints];
688 }
689 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
690 hw_breakpoints =
691 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
692 } else {
693 g_free(hw_breakpoints);
694 hw_breakpoints = NULL;
695 }
696
697 return 0;
8c012449
DH
698}
699
700void kvm_arch_remove_all_hw_breakpoints(void)
701{
770a6379
DH
702 nb_hw_breakpoints = 0;
703 g_free(hw_breakpoints);
704 hw_breakpoints = NULL;
8c012449
DH
705}
706
707void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
708{
770a6379
DH
709 int i;
710
711 if (nb_hw_breakpoints > 0) {
712 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
713 dbg->arch.hw_bp = hw_breakpoints;
714
715 for (i = 0; i < nb_hw_breakpoints; ++i) {
716 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
717 hw_breakpoints[i].addr);
718 }
719 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
720 } else {
721 dbg->arch.nr_hw_bp = 0;
722 dbg->arch.hw_bp = NULL;
723 }
8c012449
DH
724}
725
20d695a9 726void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
0e60a699 727{
0e60a699
AG
728}
729
20d695a9 730void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
0e60a699 731{
0e60a699
AG
732}
733
20d695a9 734int kvm_arch_process_async_events(CPUState *cs)
0af691d7 735{
225dc991 736 return cs->halted;
0af691d7
MT
737}
738
66ad0893
CH
739static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
740 struct kvm_s390_interrupt *interrupt)
741{
742 int r = 0;
743
744 interrupt->type = irq->type;
745 switch (irq->type) {
746 case KVM_S390_INT_VIRTIO:
747 interrupt->parm = irq->u.ext.ext_params;
748 /* fall through */
749 case KVM_S390_INT_PFAULT_INIT:
750 case KVM_S390_INT_PFAULT_DONE:
751 interrupt->parm64 = irq->u.ext.ext_params2;
752 break;
753 case KVM_S390_PROGRAM_INT:
754 interrupt->parm = irq->u.pgm.code;
755 break;
756 case KVM_S390_SIGP_SET_PREFIX:
757 interrupt->parm = irq->u.prefix.address;
758 break;
759 case KVM_S390_INT_SERVICE:
760 interrupt->parm = irq->u.ext.ext_params;
761 break;
762 case KVM_S390_MCHK:
763 interrupt->parm = irq->u.mchk.cr14;
764 interrupt->parm64 = irq->u.mchk.mcic;
765 break;
766 case KVM_S390_INT_EXTERNAL_CALL:
767 interrupt->parm = irq->u.extcall.code;
768 break;
769 case KVM_S390_INT_EMERGENCY:
770 interrupt->parm = irq->u.emerg.code;
771 break;
772 case KVM_S390_SIGP_STOP:
773 case KVM_S390_RESTART:
774 break; /* These types have no parameters */
775 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
776 interrupt->parm = irq->u.io.subchannel_id << 16;
777 interrupt->parm |= irq->u.io.subchannel_nr;
778 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
779 interrupt->parm64 |= irq->u.io.io_int_word;
780 break;
781 default:
782 r = -EINVAL;
783 break;
784 }
785 return r;
786}
787
788void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
789{
790 struct kvm_s390_interrupt kvmint = {};
791 CPUState *cs = CPU(cpu);
792 int r;
793
794 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
795 if (r < 0) {
796 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
797 exit(1);
798 }
799
800 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
801 if (r < 0) {
802 fprintf(stderr, "KVM failed to inject interrupt\n");
803 exit(1);
804 }
805}
806
bbd8bb8e 807static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
66ad0893
CH
808{
809 struct kvm_s390_interrupt kvmint = {};
810 int r;
811
812 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
813 if (r < 0) {
814 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
815 exit(1);
816 }
817
818 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
819 if (r < 0) {
820 fprintf(stderr, "KVM failed to inject interrupt\n");
821 exit(1);
822 }
823}
824
bbd8bb8e
CH
825void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
826{
827 static bool use_flic = true;
828 int r;
829
830 if (use_flic) {
831 r = kvm_s390_inject_flic(irq);
832 if (r == -ENOSYS) {
833 use_flic = false;
834 }
835 if (!r) {
836 return;
837 }
838 }
839 __kvm_s390_floating_interrupt(irq);
840}
841
de13d216 842void kvm_s390_virtio_irq(int config_change, uint64_t token)
0e60a699 843{
de13d216
CH
844 struct kvm_s390_irq irq = {
845 .type = KVM_S390_INT_VIRTIO,
846 .u.ext.ext_params = config_change,
847 .u.ext.ext_params2 = token,
848 };
0e60a699 849
de13d216 850 kvm_s390_floating_interrupt(&irq);
0e60a699
AG
851}
852
de13d216 853void kvm_s390_service_interrupt(uint32_t parm)
0e60a699 854{
de13d216
CH
855 struct kvm_s390_irq irq = {
856 .type = KVM_S390_INT_SERVICE,
857 .u.ext.ext_params = parm,
858 };
0e60a699 859
de13d216 860 kvm_s390_floating_interrupt(&irq);
79afc36d
CH
861}
862
1bc22652 863static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
0e60a699 864{
de13d216
CH
865 struct kvm_s390_irq irq = {
866 .type = KVM_S390_PROGRAM_INT,
867 .u.pgm.code = code,
868 };
869
870 kvm_s390_vcpu_interrupt(cpu, &irq);
0e60a699
AG
871}
872
801cdd35
TH
873void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
874{
875 struct kvm_s390_irq irq = {
876 .type = KVM_S390_PROGRAM_INT,
877 .u.pgm.code = code,
878 .u.pgm.trans_exc_code = te_code,
879 .u.pgm.exc_access_id = te_code & 3,
880 };
881
882 kvm_s390_vcpu_interrupt(cpu, &irq);
883}
884
1bc22652 885static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
bcec36ea 886 uint16_t ipbh0)
0e60a699 887{
1bc22652 888 CPUS390XState *env = &cpu->env;
a0fa2cb8
TH
889 uint64_t sccb;
890 uint32_t code;
0e60a699
AG
891 int r = 0;
892
cb446eca 893 cpu_synchronize_state(CPU(cpu));
0e60a699
AG
894 sccb = env->regs[ipbh0 & 0xf];
895 code = env->regs[(ipbh0 & 0xf0) >> 4];
896
6e252802 897 r = sclp_service_call(env, sccb, code);
9abf567d 898 if (r < 0) {
1bc22652 899 enter_pgmcheck(cpu, -r);
e8803d93
TH
900 } else {
901 setcc(cpu, r);
0e60a699 902 }
81f7c56c 903
0e60a699
AG
904 return 0;
905}
906
1eecf41b 907static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
09b99878 908{
09b99878 909 CPUS390XState *env = &cpu->env;
1eecf41b
FB
910 int rc = 0;
911 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
3474b679 912
44c68de0 913 cpu_synchronize_state(CPU(cpu));
3474b679 914
09b99878 915 switch (ipa1) {
1eecf41b 916 case PRIV_B2_XSCH:
5d9bf1c0 917 ioinst_handle_xsch(cpu, env->regs[1]);
09b99878 918 break;
1eecf41b 919 case PRIV_B2_CSCH:
5d9bf1c0 920 ioinst_handle_csch(cpu, env->regs[1]);
09b99878 921 break;
1eecf41b 922 case PRIV_B2_HSCH:
5d9bf1c0 923 ioinst_handle_hsch(cpu, env->regs[1]);
09b99878 924 break;
1eecf41b 925 case PRIV_B2_MSCH:
5d9bf1c0 926 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 927 break;
1eecf41b 928 case PRIV_B2_SSCH:
5d9bf1c0 929 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 930 break;
1eecf41b 931 case PRIV_B2_STCRW:
5d9bf1c0 932 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
09b99878 933 break;
1eecf41b 934 case PRIV_B2_STSCH:
5d9bf1c0 935 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
09b99878 936 break;
1eecf41b 937 case PRIV_B2_TSCH:
09b99878
CH
938 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
939 fprintf(stderr, "Spurious tsch intercept\n");
940 break;
1eecf41b 941 case PRIV_B2_CHSC:
5d9bf1c0 942 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
09b99878 943 break;
1eecf41b 944 case PRIV_B2_TPI:
09b99878
CH
945 /* This should have been handled by kvm already. */
946 fprintf(stderr, "Spurious tpi intercept\n");
947 break;
1eecf41b 948 case PRIV_B2_SCHM:
5d9bf1c0
TH
949 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
950 run->s390_sieic.ipb);
09b99878 951 break;
1eecf41b 952 case PRIV_B2_RSCH:
5d9bf1c0 953 ioinst_handle_rsch(cpu, env->regs[1]);
09b99878 954 break;
1eecf41b 955 case PRIV_B2_RCHP:
5d9bf1c0 956 ioinst_handle_rchp(cpu, env->regs[1]);
09b99878 957 break;
1eecf41b 958 case PRIV_B2_STCPS:
09b99878 959 /* We do not provide this instruction, it is suppressed. */
09b99878 960 break;
1eecf41b 961 case PRIV_B2_SAL:
5d9bf1c0 962 ioinst_handle_sal(cpu, env->regs[1]);
09b99878 963 break;
1eecf41b 964 case PRIV_B2_SIGA:
c1e8dfb5 965 /* Not provided, set CC = 3 for subchannel not operational */
5d9bf1c0 966 setcc(cpu, 3);
09b99878 967 break;
1eecf41b
FB
968 case PRIV_B2_SCLP_CALL:
969 rc = kvm_sclp_service_call(cpu, run, ipbh0);
970 break;
c1e8dfb5 971 default:
1eecf41b
FB
972 rc = -1;
973 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
974 break;
09b99878
CH
975 }
976
1eecf41b 977 return rc;
09b99878
CH
978}
979
863f6f52
FB
980static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run)
981{
982 CPUS390XState *env = &cpu->env;
983 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
984 uint32_t base2 = run->s390_sieic.ipb >> 28;
985 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
986 ((run->s390_sieic.ipb & 0xff00) << 4);
987
988 if (disp2 & 0x80000) {
989 disp2 += 0xfff00000;
990 }
991
992 return (base2 ? env->regs[base2] : 0) +
993 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
994}
995
996static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run)
997{
998 CPUS390XState *env = &cpu->env;
999 uint32_t base2 = run->s390_sieic.ipb >> 28;
1000 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1001 ((run->s390_sieic.ipb & 0xff00) << 4);
1002
1003 if (disp2 & 0x80000) {
1004 disp2 += 0xfff00000;
1005 }
1006
1007 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1008}
1009
1010static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1011{
1012 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1013
1014 return clp_service_call(cpu, r2);
1015}
1016
1017static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1018{
1019 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1020 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1021
1022 return pcilg_service_call(cpu, r1, r2);
1023}
1024
1025static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1026{
1027 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1028 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1029
1030 return pcistg_service_call(cpu, r1, r2);
1031}
1032
1033static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1034{
1035 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1036 uint64_t fiba;
1037
1038 cpu_synchronize_state(CPU(cpu));
1039 fiba = get_base_disp_rxy(cpu, run);
1040
1041 return stpcifc_service_call(cpu, r1, fiba);
1042}
1043
1044static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1045{
1046 /* NOOP */
1047 return 0;
1048}
1049
1050static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1051{
1052 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1053 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1054
1055 return rpcit_service_call(cpu, r1, r2);
1056}
1057
1058static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1059{
1060 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1061 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1062 uint64_t gaddr;
1063
1064 cpu_synchronize_state(CPU(cpu));
1065 gaddr = get_base_disp_rsy(cpu, run);
1066
1067 return pcistb_service_call(cpu, r1, r3, gaddr);
1068}
1069
1070static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1071{
1072 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1073 uint64_t fiba;
1074
1075 cpu_synchronize_state(CPU(cpu));
1076 fiba = get_base_disp_rxy(cpu, run);
1077
1078 return mpcifc_service_call(cpu, r1, fiba);
1079}
1080
1eecf41b 1081static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
0e60a699
AG
1082{
1083 int r = 0;
0e60a699 1084
0e60a699 1085 switch (ipa1) {
863f6f52
FB
1086 case PRIV_B9_CLP:
1087 r = kvm_clp_service_call(cpu, run);
1088 break;
1089 case PRIV_B9_PCISTG:
1090 r = kvm_pcistg_service_call(cpu, run);
1091 break;
1092 case PRIV_B9_PCILG:
1093 r = kvm_pcilg_service_call(cpu, run);
1094 break;
1095 case PRIV_B9_RPCIT:
1096 r = kvm_rpcit_service_call(cpu, run);
1097 break;
1eecf41b
FB
1098 case PRIV_B9_EQBS:
1099 /* just inject exception */
1100 r = -1;
1101 break;
1102 default:
1103 r = -1;
1104 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1105 break;
1106 }
1107
1108 return r;
1109}
1110
80765f07 1111static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1eecf41b
FB
1112{
1113 int r = 0;
1114
80765f07 1115 switch (ipbl) {
863f6f52
FB
1116 case PRIV_EB_PCISTB:
1117 r = kvm_pcistb_service_call(cpu, run);
1118 break;
1119 case PRIV_EB_SIC:
1120 r = kvm_sic_service_call(cpu, run);
1121 break;
1eecf41b
FB
1122 case PRIV_EB_SQBS:
1123 /* just inject exception */
1124 r = -1;
1125 break;
1126 default:
1127 r = -1;
80765f07 1128 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1eecf41b 1129 break;
0e60a699
AG
1130 }
1131
1132 return r;
1133}
1134
863f6f52
FB
1135static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1136{
1137 int r = 0;
1138
1139 switch (ipbl) {
1140 case PRIV_E3_MPCIFC:
1141 r = kvm_mpcifc_service_call(cpu, run);
1142 break;
1143 case PRIV_E3_STPCIFC:
1144 r = kvm_stpcifc_service_call(cpu, run);
1145 break;
1146 default:
1147 r = -1;
1148 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1149 break;
1150 }
1151
1152 return r;
1153}
1154
4fd6dd06 1155static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
0e60a699 1156{
4fd6dd06 1157 CPUS390XState *env = &cpu->env;
77319f22 1158 int ret;
3474b679 1159
44c68de0 1160 cpu_synchronize_state(CPU(cpu));
77319f22
TH
1161 ret = s390_virtio_hypercall(env);
1162 if (ret == -EINVAL) {
1163 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1164 return 0;
1165 }
0e60a699 1166
77319f22 1167 return ret;
0e60a699
AG
1168}
1169
268846ba
ED
1170static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1171{
1172 uint64_t r1, r3;
1173
1174 cpu_synchronize_state(CPU(cpu));
20dd25bb 1175 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
268846ba
ED
1176 r3 = run->s390_sieic.ipa & 0x000f;
1177 handle_diag_308(&cpu->env, r1, r3);
1178}
1179
b30f4dfb
DH
1180static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1181{
1182 CPUS390XState *env = &cpu->env;
1183 unsigned long pc;
1184
1185 cpu_synchronize_state(CPU(cpu));
1186
1187 pc = env->psw.addr - 4;
1188 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1189 env->psw.addr = pc;
1190 return EXCP_DEBUG;
1191 }
1192
1193 return -ENOENT;
1194}
1195
638129ff
CH
1196#define DIAG_KVM_CODE_MASK 0x000000000000ffff
1197
1198static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
0e60a699
AG
1199{
1200 int r = 0;
638129ff
CH
1201 uint16_t func_code;
1202
1203 /*
1204 * For any diagnose call we support, bits 48-63 of the resulting
1205 * address specify the function code; the remainder is ignored.
1206 */
1207 func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
1208 switch (func_code) {
268846ba
ED
1209 case DIAG_IPL:
1210 kvm_handle_diag_308(cpu, run);
1211 break;
39fbc5c6
CB
1212 case DIAG_KVM_HYPERCALL:
1213 r = handle_hypercall(cpu, run);
1214 break;
1215 case DIAG_KVM_BREAKPOINT:
b30f4dfb 1216 r = handle_sw_breakpoint(cpu, run);
39fbc5c6
CB
1217 break;
1218 default:
638129ff 1219 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
68540b1a 1220 enter_pgmcheck(cpu, PGM_SPECIFICATION);
39fbc5c6 1221 break;
0e60a699
AG
1222 }
1223
1224 return r;
1225}
1226
6eb8f212
DH
1227typedef struct SigpInfo {
1228 S390CPU *cpu;
22740e3f 1229 uint64_t param;
6eb8f212
DH
1230 int cc;
1231 uint64_t *status_reg;
1232} SigpInfo;
1233
36b5c845 1234static void set_sigp_status(SigpInfo *si, uint64_t status)
b20a461f 1235{
36b5c845
DH
1236 *si->status_reg &= 0xffffffff00000000ULL;
1237 *si->status_reg |= status;
1238 si->cc = SIGP_CC_STATUS_STORED;
1239}
6e6ad8db 1240
6eb8f212 1241static void sigp_start(void *arg)
b20a461f 1242{
6eb8f212 1243 SigpInfo *si = arg;
6e6ad8db 1244
4f2b55d1
DH
1245 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1246 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1247 return;
1248 }
1249
6eb8f212
DH
1250 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1251 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
b20a461f
TH
1252}
1253
18ff9494 1254static void sigp_stop(void *arg)
0e60a699 1255{
18ff9494
DH
1256 SigpInfo *si = arg;
1257 struct kvm_s390_irq irq = {
1258 .type = KVM_S390_SIGP_STOP,
1259 };
1260
1261 if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1262 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1263 return;
1264 }
1265
1266 /* disabled wait - sleeping in user space */
1267 if (CPU(si->cpu)->halted) {
1268 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1269 } else {
1270 /* execute the stop function */
1271 si->cpu->env.sigp_order = SIGP_STOP;
1272 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1273 }
1274 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1275}
1276
1277#define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1278#define SAVE_AREA_SIZE 512
1279static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1280{
1281 static const uint8_t ar_id = 1;
1282 uint64_t ckc = cpu->env.ckc >> 8;
1283 void *mem;
1284 hwaddr len = SAVE_AREA_SIZE;
1285
1286 mem = cpu_physical_memory_map(addr, &len, 1);
1287 if (!mem) {
1288 return -EFAULT;
1289 }
1290 if (len != SAVE_AREA_SIZE) {
1291 cpu_physical_memory_unmap(mem, len, 1, 0);
1292 return -EFAULT;
1293 }
1294
1295 if (store_arch) {
1296 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1297 }
1298 memcpy(mem, &cpu->env.fregs, 128);
1299 memcpy(mem + 128, &cpu->env.regs, 128);
1300 memcpy(mem + 256, &cpu->env.psw, 16);
1301 memcpy(mem + 280, &cpu->env.psa, 4);
1302 memcpy(mem + 284, &cpu->env.fpc, 4);
1303 memcpy(mem + 292, &cpu->env.todpr, 4);
1304 memcpy(mem + 296, &cpu->env.cputm, 8);
1305 memcpy(mem + 304, &ckc, 8);
1306 memcpy(mem + 320, &cpu->env.aregs, 64);
1307 memcpy(mem + 384, &cpu->env.cregs, 128);
1308
1309 cpu_physical_memory_unmap(mem, len, 1, len);
1310
1311 return 0;
1312}
1313
1314static void sigp_stop_and_store_status(void *arg)
1315{
1316 SigpInfo *si = arg;
1317 struct kvm_s390_irq irq = {
1318 .type = KVM_S390_SIGP_STOP,
1319 };
1320
1321 /* disabled wait - sleeping in user space */
1322 if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1323 CPU(si->cpu)->halted) {
1324 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1325 }
1326
1327 switch (s390_cpu_get_state(si->cpu)) {
1328 case CPU_STATE_OPERATING:
1329 si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1330 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1331 /* store will be performed when handling the stop intercept */
1332 break;
1333 case CPU_STATE_STOPPED:
1334 /* already stopped, just store the status */
1335 cpu_synchronize_state(CPU(si->cpu));
1336 kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1337 break;
1338 }
1339 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1340}
1341
1342static void sigp_store_status_at_address(void *arg)
1343{
1344 SigpInfo *si = arg;
1345 uint32_t address = si->param & 0x7ffffe00u;
1346
1347 /* cpu has to be stopped */
1348 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1349 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1350 return;
1351 }
1352
1353 cpu_synchronize_state(CPU(si->cpu));
1354
1355 if (kvm_s390_store_status(si->cpu, address, false)) {
1356 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1357 return;
1358 }
1359 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1360}
1361
6eb8f212 1362static void sigp_restart(void *arg)
0e60a699 1363{
6eb8f212 1364 SigpInfo *si = arg;
de13d216
CH
1365 struct kvm_s390_irq irq = {
1366 .type = KVM_S390_RESTART,
1367 };
1368
e3b7b578
DH
1369 switch (s390_cpu_get_state(si->cpu)) {
1370 case CPU_STATE_STOPPED:
1371 /* the restart irq has to be delivered prior to any other pending irq */
1372 cpu_synchronize_state(CPU(si->cpu));
1373 do_restart_interrupt(&si->cpu->env);
1374 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1375 break;
1376 case CPU_STATE_OPERATING:
1377 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1378 break;
1379 }
6eb8f212 1380 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
6e6ad8db
DH
1381}
1382
1383int kvm_s390_cpu_restart(S390CPU *cpu)
1384{
6eb8f212
DH
1385 SigpInfo si = {
1386 .cpu = cpu,
1387 };
1388
1389 run_on_cpu(CPU(cpu), sigp_restart, &si);
7f7f9752 1390 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
0e60a699
AG
1391 return 0;
1392}
1393
f7d3e466 1394static void sigp_initial_cpu_reset(void *arg)
0e60a699 1395{
6eb8f212
DH
1396 SigpInfo *si = arg;
1397 CPUState *cs = CPU(si->cpu);
1398 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
d5900813 1399
6eb8f212
DH
1400 cpu_synchronize_state(cs);
1401 scc->initial_cpu_reset(cs);
1402 cpu_synchronize_post_reset(cs);
1403 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
0e60a699
AG
1404}
1405
04c2b516
TH
1406static void sigp_cpu_reset(void *arg)
1407{
6eb8f212
DH
1408 SigpInfo *si = arg;
1409 CPUState *cs = CPU(si->cpu);
1410 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
04c2b516 1411
6eb8f212
DH
1412 cpu_synchronize_state(cs);
1413 scc->cpu_reset(cs);
1414 cpu_synchronize_post_reset(cs);
1415 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
04c2b516
TH
1416}
1417
18ff9494 1418static void sigp_set_prefix(void *arg)
0e60a699 1419{
18ff9494
DH
1420 SigpInfo *si = arg;
1421 uint32_t addr = si->param & 0x7fffe000u;
0e60a699 1422
18ff9494 1423 cpu_synchronize_state(CPU(si->cpu));
0e60a699 1424
18ff9494
DH
1425 if (!address_space_access_valid(&address_space_memory, addr,
1426 sizeof(struct LowCore), false)) {
1427 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1428 return;
1429 }
0e60a699 1430
18ff9494
DH
1431 /* cpu has to be stopped */
1432 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1433 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1434 return;
0e60a699
AG
1435 }
1436
18ff9494
DH
1437 si->cpu->env.psa = addr;
1438 cpu_synchronize_post_init(CPU(si->cpu));
1439 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1440}
1441
6eb8f212 1442static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
22740e3f 1443 uint64_t param, uint64_t *status_reg)
6eb8f212
DH
1444{
1445 SigpInfo si = {
1446 .cpu = dst_cpu,
22740e3f 1447 .param = param,
6eb8f212
DH
1448 .status_reg = status_reg,
1449 };
1450
1451 /* cpu available? */
1452 if (dst_cpu == NULL) {
1453 return SIGP_CC_NOT_OPERATIONAL;
1454 }
1455
18ff9494
DH
1456 /* only resets can break pending orders */
1457 if (dst_cpu->env.sigp_order != 0 &&
1458 order != SIGP_CPU_RESET &&
1459 order != SIGP_INITIAL_CPU_RESET) {
1460 return SIGP_CC_BUSY;
1461 }
1462
6eb8f212 1463 switch (order) {
b20a461f 1464 case SIGP_START:
6eb8f212
DH
1465 run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1466 break;
18ff9494
DH
1467 case SIGP_STOP:
1468 run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
b20a461f 1469 break;
0b9972a2 1470 case SIGP_RESTART:
6eb8f212 1471 run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
0b9972a2 1472 break;
18ff9494
DH
1473 case SIGP_STOP_STORE_STATUS:
1474 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1475 break;
1476 case SIGP_STORE_STATUS_ADDR:
1477 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1478 break;
1479 case SIGP_SET_PREFIX:
1480 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
0788082a 1481 break;
0b9972a2 1482 case SIGP_INITIAL_CPU_RESET:
6eb8f212 1483 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
0b9972a2 1484 break;
04c2b516 1485 case SIGP_CPU_RESET:
6eb8f212 1486 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
04c2b516 1487 break;
0b9972a2 1488 default:
6eb8f212 1489 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
36b5c845 1490 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
6eb8f212 1491 }
04c2b516 1492
6eb8f212 1493 return si.cc;
04c2b516
TH
1494}
1495
18ff9494
DH
1496static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1497 uint64_t *status_reg)
1498{
1499 CPUState *cur_cs;
1500 S390CPU *cur_cpu;
1501
1502 /* due to the BQL, we are the only active cpu */
1503 CPU_FOREACH(cur_cs) {
1504 cur_cpu = S390_CPU(cur_cs);
1505 if (cur_cpu->env.sigp_order != 0) {
1506 return SIGP_CC_BUSY;
1507 }
1508 cpu_synchronize_state(cur_cs);
1509 /* all but the current one have to be stopped */
1510 if (cur_cpu != cpu &&
1511 s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1512 *status_reg &= 0xffffffff00000000ULL;
1513 *status_reg |= SIGP_STAT_INCORRECT_STATE;
1514 return SIGP_CC_STATUS_STORED;
1515 }
1516 }
1517
1518 switch (param & 0xff) {
1519 case SIGP_MODE_ESA_S390:
1520 /* not supported */
1521 return SIGP_CC_NOT_OPERATIONAL;
1522 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1523 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1524 CPU_FOREACH(cur_cs) {
1525 cur_cpu = S390_CPU(cur_cs);
1526 cur_cpu->env.pfault_token = -1UL;
1527 }
0b9972a2 1528 break;
18ff9494
DH
1529 default:
1530 *status_reg &= 0xffffffff00000000ULL;
1531 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1532 return SIGP_CC_STATUS_STORED;
0e60a699
AG
1533 }
1534
18ff9494
DH
1535 return SIGP_CC_ORDER_CODE_ACCEPTED;
1536}
1537
b8031adb
TH
1538#define SIGP_ORDER_MASK 0x000000ff
1539
f7575c96 1540static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
0e60a699 1541{
f7575c96 1542 CPUS390XState *env = &cpu->env;
6eb8f212
DH
1543 const uint8_t r1 = ipa1 >> 4;
1544 const uint8_t r3 = ipa1 & 0x0f;
1545 int ret;
1546 uint8_t order;
1547 uint64_t *status_reg;
22740e3f 1548 uint64_t param;
6eb8f212 1549 S390CPU *dst_cpu = NULL;
0e60a699 1550
cb446eca 1551 cpu_synchronize_state(CPU(cpu));
0e60a699
AG
1552
1553 /* get order code */
6eb8f212
DH
1554 order = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
1555 status_reg = &env->regs[r1];
22740e3f 1556 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
0e60a699 1557
6eb8f212 1558 switch (order) {
0b9972a2 1559 case SIGP_SET_ARCH:
18ff9494 1560 ret = sigp_set_architecture(cpu, param, status_reg);
04c2b516 1561 break;
0b9972a2 1562 default:
6eb8f212
DH
1563 /* all other sigp orders target a single vcpu */
1564 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
22740e3f 1565 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
0e60a699
AG
1566 }
1567
56dba22b
DH
1568 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1569 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1570
6eb8f212
DH
1571 if (ret >= 0) {
1572 setcc(cpu, ret);
1573 return 0;
1574 }
1575
1576 return ret;
0e60a699
AG
1577}
1578
b30f4dfb 1579static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
0e60a699
AG
1580{
1581 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1582 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
d7963c43 1583 int r = -1;
0e60a699 1584
e67137c6
PM
1585 DPRINTF("handle_instruction 0x%x 0x%x\n",
1586 run->s390_sieic.ipa, run->s390_sieic.ipb);
0e60a699 1587 switch (ipa0) {
09b99878 1588 case IPA0_B2:
1eecf41b
FB
1589 r = handle_b2(cpu, run, ipa1);
1590 break;
09b99878 1591 case IPA0_B9:
1eecf41b
FB
1592 r = handle_b9(cpu, run, ipa1);
1593 break;
09b99878 1594 case IPA0_EB:
80765f07 1595 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
09b99878 1596 break;
863f6f52
FB
1597 case IPA0_E3:
1598 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1599 break;
09b99878 1600 case IPA0_DIAG:
638129ff 1601 r = handle_diag(cpu, run, run->s390_sieic.ipb);
09b99878
CH
1602 break;
1603 case IPA0_SIGP:
1604 r = handle_sigp(cpu, run, ipa1);
1605 break;
0e60a699
AG
1606 }
1607
1608 if (r < 0) {
b30f4dfb 1609 r = 0;
1bc22652 1610 enter_pgmcheck(cpu, 0x0001);
0e60a699 1611 }
b30f4dfb
DH
1612
1613 return r;
0e60a699
AG
1614}
1615
f7575c96 1616static bool is_special_wait_psw(CPUState *cs)
eca3ed03
CB
1617{
1618 /* signal quiesce */
f7575c96 1619 return cs->kvm_run->psw_addr == 0xfffUL;
eca3ed03
CB
1620}
1621
a2689242
TH
1622static void guest_panicked(void)
1623{
3a449690
WX
1624 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1625 &error_abort);
a2689242
TH
1626 vm_stop(RUN_STATE_GUEST_PANICKED);
1627}
1628
1629static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1630{
1631 CPUState *cs = CPU(cpu);
1632
1633 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1634 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1635 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
eb24f7c6 1636 s390_cpu_halt(cpu);
a2689242
TH
1637 guest_panicked();
1638}
1639
1bc22652 1640static int handle_intercept(S390CPU *cpu)
0e60a699 1641{
f7575c96
AF
1642 CPUState *cs = CPU(cpu);
1643 struct kvm_run *run = cs->kvm_run;
0e60a699
AG
1644 int icpt_code = run->s390_sieic.icptcode;
1645 int r = 0;
1646
e67137c6 1647 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
f7575c96 1648 (long)cs->kvm_run->psw_addr);
0e60a699
AG
1649 switch (icpt_code) {
1650 case ICPT_INSTRUCTION:
b30f4dfb 1651 r = handle_instruction(cpu, run);
0e60a699 1652 break;
6449a41a
TH
1653 case ICPT_PROGRAM:
1654 unmanageable_intercept(cpu, "program interrupt",
1655 offsetof(LowCore, program_new_psw));
1656 r = EXCP_HALTED;
1657 break;
a2689242
TH
1658 case ICPT_EXT_INT:
1659 unmanageable_intercept(cpu, "external interrupt",
1660 offsetof(LowCore, external_new_psw));
1661 r = EXCP_HALTED;
1662 break;
0e60a699 1663 case ICPT_WAITPSW:
08eb8c85 1664 /* disabled wait, since enabled wait is handled in kernel */
eb24f7c6
DH
1665 cpu_synchronize_state(cs);
1666 if (s390_cpu_halt(cpu) == 0) {
08eb8c85
CB
1667 if (is_special_wait_psw(cs)) {
1668 qemu_system_shutdown_request();
1669 } else {
a2689242 1670 guest_panicked();
08eb8c85 1671 }
eca3ed03
CB
1672 }
1673 r = EXCP_HALTED;
1674 break;
854e42f3 1675 case ICPT_CPU_STOP:
eb24f7c6 1676 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
854e42f3
CB
1677 qemu_system_shutdown_request();
1678 }
18ff9494
DH
1679 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1680 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1681 true);
1682 }
1683 cpu->env.sigp_order = 0;
854e42f3 1684 r = EXCP_HALTED;
0e60a699
AG
1685 break;
1686 case ICPT_SOFT_INTERCEPT:
1687 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1688 exit(1);
1689 break;
0e60a699
AG
1690 case ICPT_IO:
1691 fprintf(stderr, "KVM unimplemented icpt IO\n");
1692 exit(1);
1693 break;
1694 default:
1695 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1696 exit(1);
1697 break;
1698 }
1699
1700 return r;
1701}
1702
09b99878
CH
1703static int handle_tsch(S390CPU *cpu)
1704{
09b99878
CH
1705 CPUState *cs = CPU(cpu);
1706 struct kvm_run *run = cs->kvm_run;
1707 int ret;
1708
44c68de0 1709 cpu_synchronize_state(cs);
3474b679 1710
653b0809
TH
1711 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1712 if (ret < 0) {
09b99878
CH
1713 /*
1714 * Failure.
1715 * If an I/O interrupt had been dequeued, we have to reinject it.
1716 */
1717 if (run->s390_tsch.dequeued) {
de13d216
CH
1718 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1719 run->s390_tsch.subchannel_nr,
1720 run->s390_tsch.io_int_parm,
1721 run->s390_tsch.io_int_word);
09b99878
CH
1722 }
1723 ret = 0;
1724 }
1725 return ret;
1726}
1727
f07177a5
ET
1728static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr)
1729{
1730 struct sysib_322 sysib;
1731 int del;
1732
1733 if (s390_cpu_virt_mem_read(cpu, addr, &sysib, sizeof(sysib))) {
1734 return;
1735 }
1736 /* Shift the stack of Extended Names to prepare for our own data */
1737 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1738 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1739 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1740 * assumed it's not capable of managing Extended Names for lower levels.
1741 */
1742 for (del = 1; del < sysib.count; del++) {
1743 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1744 break;
1745 }
1746 }
1747 if (del < sysib.count) {
1748 memset(sysib.ext_names[del], 0,
1749 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1750 }
1751 /* Insert short machine name in EBCDIC, padded with blanks */
1752 if (qemu_name) {
1753 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1754 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1755 strlen(qemu_name)));
1756 }
1757 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1758 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1759 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1760 * considered by s390 as not capable of providing any Extended Name.
1761 * Therefore if no name was specified on qemu invocation, we go with the
1762 * same "KVMguest" default, which KVM has filled into short name field.
1763 */
1764 if (qemu_name) {
1765 strncpy((char *)sysib.ext_names[0], qemu_name,
1766 sizeof(sysib.ext_names[0]));
1767 } else {
1768 strcpy((char *)sysib.ext_names[0], "KVMguest");
1769 }
1770 /* Insert UUID */
1771 memcpy(sysib.vm[0].uuid, qemu_uuid, sizeof(sysib.vm[0].uuid));
1772
1773 s390_cpu_virt_mem_write(cpu, addr, &sysib, sizeof(sysib));
1774}
1775
1776static int handle_stsi(S390CPU *cpu)
1777{
1778 CPUState *cs = CPU(cpu);
1779 struct kvm_run *run = cs->kvm_run;
1780
1781 switch (run->s390_stsi.fc) {
1782 case 3:
1783 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1784 return 0;
1785 }
1786 /* Only sysib 3.2.2 needs post-handling for now. */
1787 insert_stsi_3_2_2(cpu, run->s390_stsi.addr);
1788 return 0;
1789 default:
1790 return 0;
1791 }
1792}
1793
8c012449
DH
1794static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1795{
770a6379
DH
1796 CPUState *cs = CPU(cpu);
1797 struct kvm_run *run = cs->kvm_run;
1798
1799 int ret = 0;
1800 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1801
1802 switch (arch_info->type) {
1803 case KVM_HW_WP_WRITE:
1804 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1805 cs->watchpoint_hit = &hw_watchpoint;
1806 hw_watchpoint.vaddr = arch_info->addr;
1807 hw_watchpoint.flags = BP_MEM_WRITE;
1808 ret = EXCP_DEBUG;
1809 }
1810 break;
1811 case KVM_HW_BP:
1812 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1813 ret = EXCP_DEBUG;
1814 }
1815 break;
1816 case KVM_SINGLESTEP:
1817 if (cs->singlestep_enabled) {
1818 ret = EXCP_DEBUG;
1819 }
1820 break;
1821 default:
1822 ret = -ENOSYS;
1823 }
1824
1825 return ret;
8c012449
DH
1826}
1827
20d695a9 1828int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
0e60a699 1829{
20d695a9 1830 S390CPU *cpu = S390_CPU(cs);
0e60a699
AG
1831 int ret = 0;
1832
1833 switch (run->exit_reason) {
1834 case KVM_EXIT_S390_SIEIC:
1bc22652 1835 ret = handle_intercept(cpu);
0e60a699
AG
1836 break;
1837 case KVM_EXIT_S390_RESET:
e91e972c 1838 s390_reipl_request();
0e60a699 1839 break;
09b99878
CH
1840 case KVM_EXIT_S390_TSCH:
1841 ret = handle_tsch(cpu);
1842 break;
f07177a5
ET
1843 case KVM_EXIT_S390_STSI:
1844 ret = handle_stsi(cpu);
1845 break;
8c012449
DH
1846 case KVM_EXIT_DEBUG:
1847 ret = kvm_arch_handle_debug_exit(cpu);
1848 break;
0e60a699
AG
1849 default:
1850 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1851 break;
1852 }
1853
bb4ea393
JK
1854 if (ret == 0) {
1855 ret = EXCP_INTERRUPT;
bb4ea393 1856 }
0e60a699
AG
1857 return ret;
1858}
4513d923 1859
20d695a9 1860bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
4513d923
GN
1861{
1862 return true;
1863}
a1b87fe0 1864
20d695a9 1865int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
a1b87fe0
JK
1866{
1867 return 1;
1868}
1869
1870int kvm_arch_on_sigbus(int code, void *addr)
1871{
1872 return 1;
1873}
09b99878 1874
de13d216 1875void kvm_s390_io_interrupt(uint16_t subchannel_id,
09b99878
CH
1876 uint16_t subchannel_nr, uint32_t io_int_parm,
1877 uint32_t io_int_word)
1878{
de13d216
CH
1879 struct kvm_s390_irq irq = {
1880 .u.io.subchannel_id = subchannel_id,
1881 .u.io.subchannel_nr = subchannel_nr,
1882 .u.io.io_int_parm = io_int_parm,
1883 .u.io.io_int_word = io_int_word,
1884 };
09b99878 1885
7e749462 1886 if (io_int_word & IO_INT_WORD_AI) {
de13d216 1887 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
7e749462 1888 } else {
de13d216 1889 irq.type = ((subchannel_id & 0xff00) << 24) |
7e749462
CH
1890 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
1891 }
de13d216 1892 kvm_s390_floating_interrupt(&irq);
09b99878
CH
1893}
1894
de13d216 1895void kvm_s390_crw_mchk(void)
09b99878 1896{
de13d216
CH
1897 struct kvm_s390_irq irq = {
1898 .type = KVM_S390_MCHK,
1899 .u.mchk.cr14 = 1 << 28,
f0d4dc18 1900 .u.mchk.mcic = 0x00400f1d40330000ULL,
de13d216
CH
1901 };
1902 kvm_s390_floating_interrupt(&irq);
09b99878
CH
1903}
1904
1905void kvm_s390_enable_css_support(S390CPU *cpu)
1906{
09b99878
CH
1907 int r;
1908
1909 /* Activate host kernel channel subsystem support. */
e080f0fd 1910 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
09b99878
CH
1911 assert(r == 0);
1912}
48475e14
AK
1913
1914void kvm_arch_init_irq_routing(KVMState *s)
1915{
d426d9fb
CH
1916 /*
1917 * Note that while irqchip capabilities generally imply that cpustates
1918 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1919 * have to override the common code kvm_halt_in_kernel_allowed setting.
1920 */
1921 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
d426d9fb
CH
1922 kvm_gsi_routing_allowed = true;
1923 kvm_halt_in_kernel_allowed = false;
1924 }
48475e14 1925}
b4436a0b 1926
cc3ac9c4
CH
1927int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1928 int vq, bool assign)
b4436a0b
CH
1929{
1930 struct kvm_ioeventfd kick = {
1931 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1932 KVM_IOEVENTFD_FLAG_DATAMATCH,
cc3ac9c4 1933 .fd = event_notifier_get_fd(notifier),
b4436a0b
CH
1934 .datamatch = vq,
1935 .addr = sch,
1936 .len = 8,
1937 };
1938 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1939 return -ENOSYS;
1940 }
1941 if (!assign) {
1942 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1943 }
1944 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1945}
1def6656
MR
1946
1947int kvm_s390_get_memslot_count(KVMState *s)
1948{
1949 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
1950}
c9e659c9
DH
1951
1952int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1953{
1954 struct kvm_mp_state mp_state = {};
1955 int ret;
1956
1957 /* the kvm part might not have been initialized yet */
1958 if (CPU(cpu)->kvm_state == NULL) {
1959 return 0;
1960 }
1961
1962 switch (cpu_state) {
1963 case CPU_STATE_STOPPED:
1964 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1965 break;
1966 case CPU_STATE_CHECK_STOP:
1967 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1968 break;
1969 case CPU_STATE_OPERATING:
1970 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1971 break;
1972 case CPU_STATE_LOAD:
1973 mp_state.mp_state = KVM_MP_STATE_LOAD;
1974 break;
1975 default:
1976 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1977 cpu_state);
1978 exit(1);
1979 }
1980
1981 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1982 if (ret) {
1983 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1984 strerror(-ret));
1985 }
1986
1987 return ret;
1988}
9e03a040
FB
1989
1990int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1991 uint64_t address, uint32_t data)
1992{
1993 S390PCIBusDevice *pbdev;
1994 uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
1995 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
1996
1997 pbdev = s390_pci_find_dev_by_fid(fid);
1998 if (!pbdev) {
1999 DPRINTF("add_msi_route no dev\n");
2000 return -ENODEV;
2001 }
2002
2003 pbdev->routes.adapter.ind_offset = vec;
2004
2005 route->type = KVM_IRQ_ROUTING_S390_ADAPTER;
2006 route->flags = 0;
2007 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr;
2008 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr;
2009 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset;
2010 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2011 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2012 return 0;
2013}
This page took 0.785683 seconds and 4 git commands to generate.