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