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