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