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