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