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