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