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