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