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