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