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