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