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