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