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