]> git.proxmox.com Git - mirror_qemu.git/blob - target-s390x/kvm.c
s390x/kvm: Put vm name, extended name and UUID into STSI322 SYSIB
[mirror_qemu.git] / target-s390x / kvm.c
1 /*
2 * QEMU S390x KVM implementation
3 *
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 * Copyright IBM Corp. 2012
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 *
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
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/mman.h>
27
28 #include <linux/kvm.h>
29 #include <asm/ptrace.h>
30
31 #include "qemu-common.h"
32 #include "qemu/timer.h"
33 #include "sysemu/sysemu.h"
34 #include "sysemu/kvm.h"
35 #include "hw/hw.h"
36 #include "cpu.h"
37 #include "sysemu/device_tree.h"
38 #include "qapi/qmp/qjson.h"
39 #include "monitor/monitor.h"
40 #include "exec/gdbstub.h"
41 #include "exec/address-spaces.h"
42 #include "trace.h"
43 #include "qapi-event.h"
44 #include "hw/s390x/s390-pci-inst.h"
45 #include "hw/s390x/s390-pci-bus.h"
46 #include "hw/s390x/ipl.h"
47 #include "hw/s390x/ebcdic.h"
48
49 /* #define DEBUG_KVM */
50
51 #ifdef DEBUG_KVM
52 #define DPRINTF(fmt, ...) \
53 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
54 #else
55 #define DPRINTF(fmt, ...) \
56 do { } while (0)
57 #endif
58
59 #define kvm_vm_check_mem_attr(s, attr) \
60 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr)
61
62 #define IPA0_DIAG 0x8300
63 #define IPA0_SIGP 0xae00
64 #define IPA0_B2 0xb200
65 #define IPA0_B9 0xb900
66 #define IPA0_EB 0xeb00
67 #define IPA0_E3 0xe300
68
69 #define PRIV_B2_SCLP_CALL 0x20
70 #define PRIV_B2_CSCH 0x30
71 #define PRIV_B2_HSCH 0x31
72 #define PRIV_B2_MSCH 0x32
73 #define PRIV_B2_SSCH 0x33
74 #define PRIV_B2_STSCH 0x34
75 #define PRIV_B2_TSCH 0x35
76 #define PRIV_B2_TPI 0x36
77 #define PRIV_B2_SAL 0x37
78 #define PRIV_B2_RSCH 0x38
79 #define PRIV_B2_STCRW 0x39
80 #define PRIV_B2_STCPS 0x3a
81 #define PRIV_B2_RCHP 0x3b
82 #define PRIV_B2_SCHM 0x3c
83 #define PRIV_B2_CHSC 0x5f
84 #define PRIV_B2_SIGA 0x74
85 #define PRIV_B2_XSCH 0x76
86
87 #define PRIV_EB_SQBS 0x8a
88 #define PRIV_EB_PCISTB 0xd0
89 #define PRIV_EB_SIC 0xd1
90
91 #define PRIV_B9_EQBS 0x9c
92 #define PRIV_B9_CLP 0xa0
93 #define PRIV_B9_PCISTG 0xd0
94 #define PRIV_B9_PCILG 0xd2
95 #define PRIV_B9_RPCIT 0xd3
96
97 #define PRIV_E3_MPCIFC 0xd0
98 #define PRIV_E3_STPCIFC 0xd4
99
100 #define DIAG_IPL 0x308
101 #define DIAG_KVM_HYPERCALL 0x500
102 #define DIAG_KVM_BREAKPOINT 0x501
103
104 #define ICPT_INSTRUCTION 0x04
105 #define ICPT_PROGRAM 0x08
106 #define ICPT_EXT_INT 0x14
107 #define ICPT_WAITPSW 0x1c
108 #define ICPT_SOFT_INTERCEPT 0x24
109 #define ICPT_CPU_STOP 0x28
110 #define ICPT_IO 0x40
111
112 static CPUWatchpoint hw_watchpoint;
113 /*
114 * We don't use a list because this structure is also used to transmit the
115 * hardware breakpoints to the kernel.
116 */
117 static struct kvm_hw_breakpoint *hw_breakpoints;
118 static int nb_hw_breakpoints;
119
120 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
121 KVM_CAP_LAST_INFO
122 };
123
124 static int cap_sync_regs;
125 static int cap_async_pf;
126
127 static void *legacy_s390_alloc(size_t size, uint64_t *align);
128
129 static int kvm_s390_query_mem_limit(KVMState *s, uint64_t *memory_limit)
130 {
131 struct kvm_device_attr attr = {
132 .group = KVM_S390_VM_MEM_CTRL,
133 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
134 .addr = (uint64_t) memory_limit,
135 };
136
137 return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &attr);
138 }
139
140 int kvm_s390_set_mem_limit(KVMState *s, uint64_t new_limit, uint64_t *hw_limit)
141 {
142 int rc;
143
144 struct kvm_device_attr attr = {
145 .group = KVM_S390_VM_MEM_CTRL,
146 .attr = KVM_S390_VM_MEM_LIMIT_SIZE,
147 .addr = (uint64_t) &new_limit,
148 };
149
150 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_LIMIT_SIZE)) {
151 return 0;
152 }
153
154 rc = kvm_s390_query_mem_limit(s, hw_limit);
155 if (rc) {
156 return rc;
157 } else if (*hw_limit < new_limit) {
158 return -E2BIG;
159 }
160
161 return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
162 }
163
164 void kvm_s390_clear_cmma_callback(void *opaque)
165 {
166 int rc;
167 KVMState *s = opaque;
168 struct kvm_device_attr attr = {
169 .group = KVM_S390_VM_MEM_CTRL,
170 .attr = KVM_S390_VM_MEM_CLR_CMMA,
171 };
172
173 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
174 trace_kvm_clear_cmma(rc);
175 }
176
177 static void kvm_s390_enable_cmma(KVMState *s)
178 {
179 int rc;
180 struct kvm_device_attr attr = {
181 .group = KVM_S390_VM_MEM_CTRL,
182 .attr = KVM_S390_VM_MEM_ENABLE_CMMA,
183 };
184
185 if (!kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_ENABLE_CMMA) ||
186 !kvm_vm_check_mem_attr(s, KVM_S390_VM_MEM_CLR_CMMA)) {
187 return;
188 }
189
190 rc = kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &attr);
191 if (!rc) {
192 qemu_register_reset(kvm_s390_clear_cmma_callback, s);
193 }
194 trace_kvm_enable_cmma(rc);
195 }
196
197 static void kvm_s390_set_attr(uint64_t attr)
198 {
199 struct kvm_device_attr attribute = {
200 .group = KVM_S390_VM_CRYPTO,
201 .attr = attr,
202 };
203
204 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute);
205
206 if (ret) {
207 error_report("Failed to set crypto device attribute %lu: %s",
208 attr, strerror(-ret));
209 }
210 }
211
212 static void kvm_s390_init_aes_kw(void)
213 {
214 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW;
215
216 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap",
217 NULL)) {
218 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW;
219 }
220
221 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
222 kvm_s390_set_attr(attr);
223 }
224 }
225
226 static void kvm_s390_init_dea_kw(void)
227 {
228 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW;
229
230 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap",
231 NULL)) {
232 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW;
233 }
234
235 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) {
236 kvm_s390_set_attr(attr);
237 }
238 }
239
240 static void kvm_s390_init_crypto(void)
241 {
242 kvm_s390_init_aes_kw();
243 kvm_s390_init_dea_kw();
244 }
245
246 int kvm_arch_init(MachineState *ms, KVMState *s)
247 {
248 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
249 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF);
250
251 kvm_s390_enable_cmma(s);
252
253 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP)
254 || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
255 phys_mem_set_alloc(legacy_s390_alloc);
256 }
257
258 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0);
259 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0);
260
261 return 0;
262 }
263
264 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
265 {
266 return cpu->cpu_index;
267 }
268
269 int kvm_arch_init_vcpu(CPUState *cs)
270 {
271 S390CPU *cpu = S390_CPU(cs);
272 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state);
273 return 0;
274 }
275
276 void kvm_s390_reset_vcpu(S390CPU *cpu)
277 {
278 CPUState *cs = CPU(cpu);
279
280 /* The initial reset call is needed here to reset in-kernel
281 * vcpu data that we can't access directly from QEMU
282 * (i.e. with older kernels which don't support sync_regs/ONE_REG).
283 * Before this ioctl cpu_synchronize_state() is called in common kvm
284 * code (kvm-all) */
285 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) {
286 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index);
287 }
288
289 kvm_s390_init_crypto();
290 }
291
292 static int can_sync_regs(CPUState *cs, int regs)
293 {
294 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs;
295 }
296
297 int kvm_arch_put_registers(CPUState *cs, int level)
298 {
299 S390CPU *cpu = S390_CPU(cs);
300 CPUS390XState *env = &cpu->env;
301 struct kvm_sregs sregs;
302 struct kvm_regs regs;
303 struct kvm_fpu fpu = {};
304 int r;
305 int i;
306
307 /* always save the PSW and the GPRS*/
308 cs->kvm_run->psw_addr = env->psw.addr;
309 cs->kvm_run->psw_mask = env->psw.mask;
310
311 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
312 for (i = 0; i < 16; i++) {
313 cs->kvm_run->s.regs.gprs[i] = env->regs[i];
314 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
315 }
316 } else {
317 for (i = 0; i < 16; i++) {
318 regs.gprs[i] = env->regs[i];
319 }
320 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
321 if (r < 0) {
322 return r;
323 }
324 }
325
326 /* Floating point */
327 for (i = 0; i < 16; i++) {
328 fpu.fprs[i] = env->fregs[i].ll;
329 }
330 fpu.fpc = env->fpc;
331
332 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu);
333 if (r < 0) {
334 return r;
335 }
336
337 /* Do we need to save more than that? */
338 if (level == KVM_PUT_RUNTIME_STATE) {
339 return 0;
340 }
341
342 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
343 cs->kvm_run->s.regs.cputm = env->cputm;
344 cs->kvm_run->s.regs.ckc = env->ckc;
345 cs->kvm_run->s.regs.todpr = env->todpr;
346 cs->kvm_run->s.regs.gbea = env->gbea;
347 cs->kvm_run->s.regs.pp = env->pp;
348 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0;
349 } else {
350 /*
351 * These ONE_REGS are not protected by a capability. As they are only
352 * necessary for migration we just trace a possible error, but don't
353 * return with an error return code.
354 */
355 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
356 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
357 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
358 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
359 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp);
360 }
361
362 /* pfault parameters */
363 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
364 cs->kvm_run->s.regs.pft = env->pfault_token;
365 cs->kvm_run->s.regs.pfs = env->pfault_select;
366 cs->kvm_run->s.regs.pfc = env->pfault_compare;
367 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT;
368 } else if (cap_async_pf) {
369 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
370 if (r < 0) {
371 return r;
372 }
373 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
374 if (r < 0) {
375 return r;
376 }
377 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
378 if (r < 0) {
379 return r;
380 }
381 }
382
383 /* access registers and control registers*/
384 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
385 for (i = 0; i < 16; i++) {
386 cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
387 cs->kvm_run->s.regs.crs[i] = env->cregs[i];
388 }
389 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
390 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
391 } else {
392 for (i = 0; i < 16; i++) {
393 sregs.acrs[i] = env->aregs[i];
394 sregs.crs[i] = env->cregs[i];
395 }
396 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
397 if (r < 0) {
398 return r;
399 }
400 }
401
402 /* Finally the prefix */
403 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
404 cs->kvm_run->s.regs.prefix = env->psa;
405 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
406 } else {
407 /* prefix is only supported via sync regs */
408 }
409 return 0;
410 }
411
412 int kvm_arch_get_registers(CPUState *cs)
413 {
414 S390CPU *cpu = S390_CPU(cs);
415 CPUS390XState *env = &cpu->env;
416 struct kvm_sregs sregs;
417 struct kvm_regs regs;
418 struct kvm_fpu fpu;
419 int i, r;
420
421 /* get the PSW */
422 env->psw.addr = cs->kvm_run->psw_addr;
423 env->psw.mask = cs->kvm_run->psw_mask;
424
425 /* the GPRS */
426 if (can_sync_regs(cs, KVM_SYNC_GPRS)) {
427 for (i = 0; i < 16; i++) {
428 env->regs[i] = cs->kvm_run->s.regs.gprs[i];
429 }
430 } else {
431 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
432 if (r < 0) {
433 return r;
434 }
435 for (i = 0; i < 16; i++) {
436 env->regs[i] = regs.gprs[i];
437 }
438 }
439
440 /* The ACRS and CRS */
441 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) {
442 for (i = 0; i < 16; i++) {
443 env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
444 env->cregs[i] = cs->kvm_run->s.regs.crs[i];
445 }
446 } else {
447 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
448 if (r < 0) {
449 return r;
450 }
451 for (i = 0; i < 16; i++) {
452 env->aregs[i] = sregs.acrs[i];
453 env->cregs[i] = sregs.crs[i];
454 }
455 }
456
457 /* Floating point */
458 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu);
459 if (r < 0) {
460 return r;
461 }
462 for (i = 0; i < 16; i++) {
463 env->fregs[i].ll = fpu.fprs[i];
464 }
465 env->fpc = fpu.fpc;
466
467 /* The prefix */
468 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) {
469 env->psa = cs->kvm_run->s.regs.prefix;
470 }
471
472 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) {
473 env->cputm = cs->kvm_run->s.regs.cputm;
474 env->ckc = cs->kvm_run->s.regs.ckc;
475 env->todpr = cs->kvm_run->s.regs.todpr;
476 env->gbea = cs->kvm_run->s.regs.gbea;
477 env->pp = cs->kvm_run->s.regs.pp;
478 } else {
479 /*
480 * These ONE_REGS are not protected by a capability. As they are only
481 * necessary for migration we just trace a possible error, but don't
482 * return with an error return code.
483 */
484 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm);
485 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc);
486 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr);
487 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea);
488 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp);
489 }
490
491 /* pfault parameters */
492 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) {
493 env->pfault_token = cs->kvm_run->s.regs.pft;
494 env->pfault_select = cs->kvm_run->s.regs.pfs;
495 env->pfault_compare = cs->kvm_run->s.regs.pfc;
496 } else if (cap_async_pf) {
497 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token);
498 if (r < 0) {
499 return r;
500 }
501 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare);
502 if (r < 0) {
503 return r;
504 }
505 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select);
506 if (r < 0) {
507 return r;
508 }
509 }
510
511 return 0;
512 }
513
514 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
515 {
516 int r;
517 struct kvm_device_attr attr = {
518 .group = KVM_S390_VM_TOD,
519 .attr = KVM_S390_VM_TOD_LOW,
520 .addr = (uint64_t)tod_low,
521 };
522
523 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
524 if (r) {
525 return r;
526 }
527
528 attr.attr = KVM_S390_VM_TOD_HIGH;
529 attr.addr = (uint64_t)tod_high;
530 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr);
531 }
532
533 int kvm_s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
534 {
535 int r;
536
537 struct kvm_device_attr attr = {
538 .group = KVM_S390_VM_TOD,
539 .attr = KVM_S390_VM_TOD_LOW,
540 .addr = (uint64_t)tod_low,
541 };
542
543 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
544 if (r) {
545 return r;
546 }
547
548 attr.attr = KVM_S390_VM_TOD_HIGH;
549 attr.addr = (uint64_t)tod_high;
550 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr);
551 }
552
553 /*
554 * Legacy layout for s390:
555 * Older S390 KVM requires the topmost vma of the RAM to be
556 * smaller than an system defined value, which is at least 256GB.
557 * Larger systems have larger values. We put the guest between
558 * the end of data segment (system break) and this value. We
559 * use 32GB as a base to have enough room for the system break
560 * to grow. We also have to use MAP parameters that avoid
561 * read-only mapping of guest pages.
562 */
563 static void *legacy_s390_alloc(size_t size, uint64_t *align)
564 {
565 void *mem;
566
567 mem = mmap((void *) 0x800000000ULL, size,
568 PROT_EXEC|PROT_READ|PROT_WRITE,
569 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
570 return mem == MAP_FAILED ? NULL : mem;
571 }
572
573 /* DIAG 501 is used for sw breakpoints */
574 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
575
576 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
577 {
578
579 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
580 sizeof(diag_501), 0) ||
581 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501,
582 sizeof(diag_501), 1)) {
583 return -EINVAL;
584 }
585 return 0;
586 }
587
588 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
589 {
590 uint8_t t[sizeof(diag_501)];
591
592 if (cpu_memory_rw_debug(cs, bp->pc, t, sizeof(diag_501), 0)) {
593 return -EINVAL;
594 } else if (memcmp(t, diag_501, sizeof(diag_501))) {
595 return -EINVAL;
596 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
597 sizeof(diag_501), 1)) {
598 return -EINVAL;
599 }
600
601 return 0;
602 }
603
604 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr,
605 int len, int type)
606 {
607 int n;
608
609 for (n = 0; n < nb_hw_breakpoints; n++) {
610 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type &&
611 (hw_breakpoints[n].len == len || len == -1)) {
612 return &hw_breakpoints[n];
613 }
614 }
615
616 return NULL;
617 }
618
619 static int insert_hw_breakpoint(target_ulong addr, int len, int type)
620 {
621 int size;
622
623 if (find_hw_breakpoint(addr, len, type)) {
624 return -EEXIST;
625 }
626
627 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint);
628
629 if (!hw_breakpoints) {
630 nb_hw_breakpoints = 0;
631 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size);
632 } else {
633 hw_breakpoints =
634 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size);
635 }
636
637 if (!hw_breakpoints) {
638 nb_hw_breakpoints = 0;
639 return -ENOMEM;
640 }
641
642 hw_breakpoints[nb_hw_breakpoints].addr = addr;
643 hw_breakpoints[nb_hw_breakpoints].len = len;
644 hw_breakpoints[nb_hw_breakpoints].type = type;
645
646 nb_hw_breakpoints++;
647
648 return 0;
649 }
650
651 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
652 target_ulong len, int type)
653 {
654 switch (type) {
655 case GDB_BREAKPOINT_HW:
656 type = KVM_HW_BP;
657 break;
658 case GDB_WATCHPOINT_WRITE:
659 if (len < 1) {
660 return -EINVAL;
661 }
662 type = KVM_HW_WP_WRITE;
663 break;
664 default:
665 return -ENOSYS;
666 }
667 return insert_hw_breakpoint(addr, len, type);
668 }
669
670 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
671 target_ulong len, int type)
672 {
673 int size;
674 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type);
675
676 if (bp == NULL) {
677 return -ENOENT;
678 }
679
680 nb_hw_breakpoints--;
681 if (nb_hw_breakpoints > 0) {
682 /*
683 * In order to trim the array, move the last element to the position to
684 * be removed - if necessary.
685 */
686 if (bp != &hw_breakpoints[nb_hw_breakpoints]) {
687 *bp = hw_breakpoints[nb_hw_breakpoints];
688 }
689 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint);
690 hw_breakpoints =
691 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size);
692 } else {
693 g_free(hw_breakpoints);
694 hw_breakpoints = NULL;
695 }
696
697 return 0;
698 }
699
700 void kvm_arch_remove_all_hw_breakpoints(void)
701 {
702 nb_hw_breakpoints = 0;
703 g_free(hw_breakpoints);
704 hw_breakpoints = NULL;
705 }
706
707 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
708 {
709 int i;
710
711 if (nb_hw_breakpoints > 0) {
712 dbg->arch.nr_hw_bp = nb_hw_breakpoints;
713 dbg->arch.hw_bp = hw_breakpoints;
714
715 for (i = 0; i < nb_hw_breakpoints; ++i) {
716 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu,
717 hw_breakpoints[i].addr);
718 }
719 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
720 } else {
721 dbg->arch.nr_hw_bp = 0;
722 dbg->arch.hw_bp = NULL;
723 }
724 }
725
726 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
727 {
728 }
729
730 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
731 {
732 }
733
734 int kvm_arch_process_async_events(CPUState *cs)
735 {
736 return cs->halted;
737 }
738
739 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq,
740 struct kvm_s390_interrupt *interrupt)
741 {
742 int r = 0;
743
744 interrupt->type = irq->type;
745 switch (irq->type) {
746 case KVM_S390_INT_VIRTIO:
747 interrupt->parm = irq->u.ext.ext_params;
748 /* fall through */
749 case KVM_S390_INT_PFAULT_INIT:
750 case KVM_S390_INT_PFAULT_DONE:
751 interrupt->parm64 = irq->u.ext.ext_params2;
752 break;
753 case KVM_S390_PROGRAM_INT:
754 interrupt->parm = irq->u.pgm.code;
755 break;
756 case KVM_S390_SIGP_SET_PREFIX:
757 interrupt->parm = irq->u.prefix.address;
758 break;
759 case KVM_S390_INT_SERVICE:
760 interrupt->parm = irq->u.ext.ext_params;
761 break;
762 case KVM_S390_MCHK:
763 interrupt->parm = irq->u.mchk.cr14;
764 interrupt->parm64 = irq->u.mchk.mcic;
765 break;
766 case KVM_S390_INT_EXTERNAL_CALL:
767 interrupt->parm = irq->u.extcall.code;
768 break;
769 case KVM_S390_INT_EMERGENCY:
770 interrupt->parm = irq->u.emerg.code;
771 break;
772 case KVM_S390_SIGP_STOP:
773 case KVM_S390_RESTART:
774 break; /* These types have no parameters */
775 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
776 interrupt->parm = irq->u.io.subchannel_id << 16;
777 interrupt->parm |= irq->u.io.subchannel_nr;
778 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32;
779 interrupt->parm64 |= irq->u.io.io_int_word;
780 break;
781 default:
782 r = -EINVAL;
783 break;
784 }
785 return r;
786 }
787
788 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq)
789 {
790 struct kvm_s390_interrupt kvmint = {};
791 CPUState *cs = CPU(cpu);
792 int r;
793
794 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
795 if (r < 0) {
796 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
797 exit(1);
798 }
799
800 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
801 if (r < 0) {
802 fprintf(stderr, "KVM failed to inject interrupt\n");
803 exit(1);
804 }
805 }
806
807 static void __kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
808 {
809 struct kvm_s390_interrupt kvmint = {};
810 int r;
811
812 r = s390_kvm_irq_to_interrupt(irq, &kvmint);
813 if (r < 0) {
814 fprintf(stderr, "%s called with bogus interrupt\n", __func__);
815 exit(1);
816 }
817
818 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint);
819 if (r < 0) {
820 fprintf(stderr, "KVM failed to inject interrupt\n");
821 exit(1);
822 }
823 }
824
825 void kvm_s390_floating_interrupt(struct kvm_s390_irq *irq)
826 {
827 static bool use_flic = true;
828 int r;
829
830 if (use_flic) {
831 r = kvm_s390_inject_flic(irq);
832 if (r == -ENOSYS) {
833 use_flic = false;
834 }
835 if (!r) {
836 return;
837 }
838 }
839 __kvm_s390_floating_interrupt(irq);
840 }
841
842 void kvm_s390_virtio_irq(int config_change, uint64_t token)
843 {
844 struct kvm_s390_irq irq = {
845 .type = KVM_S390_INT_VIRTIO,
846 .u.ext.ext_params = config_change,
847 .u.ext.ext_params2 = token,
848 };
849
850 kvm_s390_floating_interrupt(&irq);
851 }
852
853 void kvm_s390_service_interrupt(uint32_t parm)
854 {
855 struct kvm_s390_irq irq = {
856 .type = KVM_S390_INT_SERVICE,
857 .u.ext.ext_params = parm,
858 };
859
860 kvm_s390_floating_interrupt(&irq);
861 }
862
863 static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
864 {
865 struct kvm_s390_irq irq = {
866 .type = KVM_S390_PROGRAM_INT,
867 .u.pgm.code = code,
868 };
869
870 kvm_s390_vcpu_interrupt(cpu, &irq);
871 }
872
873 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code)
874 {
875 struct kvm_s390_irq irq = {
876 .type = KVM_S390_PROGRAM_INT,
877 .u.pgm.code = code,
878 .u.pgm.trans_exc_code = te_code,
879 .u.pgm.exc_access_id = te_code & 3,
880 };
881
882 kvm_s390_vcpu_interrupt(cpu, &irq);
883 }
884
885 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
886 uint16_t ipbh0)
887 {
888 CPUS390XState *env = &cpu->env;
889 uint64_t sccb;
890 uint32_t code;
891 int r = 0;
892
893 cpu_synchronize_state(CPU(cpu));
894 sccb = env->regs[ipbh0 & 0xf];
895 code = env->regs[(ipbh0 & 0xf0) >> 4];
896
897 r = sclp_service_call(env, sccb, code);
898 if (r < 0) {
899 enter_pgmcheck(cpu, -r);
900 } else {
901 setcc(cpu, r);
902 }
903
904 return 0;
905 }
906
907 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
908 {
909 CPUS390XState *env = &cpu->env;
910 int rc = 0;
911 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
912
913 cpu_synchronize_state(CPU(cpu));
914
915 switch (ipa1) {
916 case PRIV_B2_XSCH:
917 ioinst_handle_xsch(cpu, env->regs[1]);
918 break;
919 case PRIV_B2_CSCH:
920 ioinst_handle_csch(cpu, env->regs[1]);
921 break;
922 case PRIV_B2_HSCH:
923 ioinst_handle_hsch(cpu, env->regs[1]);
924 break;
925 case PRIV_B2_MSCH:
926 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
927 break;
928 case PRIV_B2_SSCH:
929 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
930 break;
931 case PRIV_B2_STCRW:
932 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
933 break;
934 case PRIV_B2_STSCH:
935 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
936 break;
937 case PRIV_B2_TSCH:
938 /* We should only get tsch via KVM_EXIT_S390_TSCH. */
939 fprintf(stderr, "Spurious tsch intercept\n");
940 break;
941 case PRIV_B2_CHSC:
942 ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
943 break;
944 case PRIV_B2_TPI:
945 /* This should have been handled by kvm already. */
946 fprintf(stderr, "Spurious tpi intercept\n");
947 break;
948 case PRIV_B2_SCHM:
949 ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
950 run->s390_sieic.ipb);
951 break;
952 case PRIV_B2_RSCH:
953 ioinst_handle_rsch(cpu, env->regs[1]);
954 break;
955 case PRIV_B2_RCHP:
956 ioinst_handle_rchp(cpu, env->regs[1]);
957 break;
958 case PRIV_B2_STCPS:
959 /* We do not provide this instruction, it is suppressed. */
960 break;
961 case PRIV_B2_SAL:
962 ioinst_handle_sal(cpu, env->regs[1]);
963 break;
964 case PRIV_B2_SIGA:
965 /* Not provided, set CC = 3 for subchannel not operational */
966 setcc(cpu, 3);
967 break;
968 case PRIV_B2_SCLP_CALL:
969 rc = kvm_sclp_service_call(cpu, run, ipbh0);
970 break;
971 default:
972 rc = -1;
973 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1);
974 break;
975 }
976
977 return rc;
978 }
979
980 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run)
981 {
982 CPUS390XState *env = &cpu->env;
983 uint32_t x2 = (run->s390_sieic.ipa & 0x000f);
984 uint32_t base2 = run->s390_sieic.ipb >> 28;
985 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
986 ((run->s390_sieic.ipb & 0xff00) << 4);
987
988 if (disp2 & 0x80000) {
989 disp2 += 0xfff00000;
990 }
991
992 return (base2 ? env->regs[base2] : 0) +
993 (x2 ? env->regs[x2] : 0) + (long)(int)disp2;
994 }
995
996 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run)
997 {
998 CPUS390XState *env = &cpu->env;
999 uint32_t base2 = run->s390_sieic.ipb >> 28;
1000 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) +
1001 ((run->s390_sieic.ipb & 0xff00) << 4);
1002
1003 if (disp2 & 0x80000) {
1004 disp2 += 0xfff00000;
1005 }
1006
1007 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2;
1008 }
1009
1010 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run)
1011 {
1012 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1013
1014 return clp_service_call(cpu, r2);
1015 }
1016
1017 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run)
1018 {
1019 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1020 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1021
1022 return pcilg_service_call(cpu, r1, r2);
1023 }
1024
1025 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run)
1026 {
1027 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1028 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1029
1030 return pcistg_service_call(cpu, r1, r2);
1031 }
1032
1033 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1034 {
1035 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1036 uint64_t fiba;
1037
1038 cpu_synchronize_state(CPU(cpu));
1039 fiba = get_base_disp_rxy(cpu, run);
1040
1041 return stpcifc_service_call(cpu, r1, fiba);
1042 }
1043
1044 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run)
1045 {
1046 /* NOOP */
1047 return 0;
1048 }
1049
1050 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run)
1051 {
1052 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20;
1053 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16;
1054
1055 return rpcit_service_call(cpu, r1, r2);
1056 }
1057
1058 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run)
1059 {
1060 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1061 uint8_t r3 = run->s390_sieic.ipa & 0x000f;
1062 uint64_t gaddr;
1063
1064 cpu_synchronize_state(CPU(cpu));
1065 gaddr = get_base_disp_rsy(cpu, run);
1066
1067 return pcistb_service_call(cpu, r1, r3, gaddr);
1068 }
1069
1070 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run)
1071 {
1072 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1073 uint64_t fiba;
1074
1075 cpu_synchronize_state(CPU(cpu));
1076 fiba = get_base_disp_rxy(cpu, run);
1077
1078 return mpcifc_service_call(cpu, r1, fiba);
1079 }
1080
1081 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1082 {
1083 int r = 0;
1084
1085 switch (ipa1) {
1086 case PRIV_B9_CLP:
1087 r = kvm_clp_service_call(cpu, run);
1088 break;
1089 case PRIV_B9_PCISTG:
1090 r = kvm_pcistg_service_call(cpu, run);
1091 break;
1092 case PRIV_B9_PCILG:
1093 r = kvm_pcilg_service_call(cpu, run);
1094 break;
1095 case PRIV_B9_RPCIT:
1096 r = kvm_rpcit_service_call(cpu, run);
1097 break;
1098 case PRIV_B9_EQBS:
1099 /* just inject exception */
1100 r = -1;
1101 break;
1102 default:
1103 r = -1;
1104 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1);
1105 break;
1106 }
1107
1108 return r;
1109 }
1110
1111 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1112 {
1113 int r = 0;
1114
1115 switch (ipbl) {
1116 case PRIV_EB_PCISTB:
1117 r = kvm_pcistb_service_call(cpu, run);
1118 break;
1119 case PRIV_EB_SIC:
1120 r = kvm_sic_service_call(cpu, run);
1121 break;
1122 case PRIV_EB_SQBS:
1123 /* just inject exception */
1124 r = -1;
1125 break;
1126 default:
1127 r = -1;
1128 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl);
1129 break;
1130 }
1131
1132 return r;
1133 }
1134
1135 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl)
1136 {
1137 int r = 0;
1138
1139 switch (ipbl) {
1140 case PRIV_E3_MPCIFC:
1141 r = kvm_mpcifc_service_call(cpu, run);
1142 break;
1143 case PRIV_E3_STPCIFC:
1144 r = kvm_stpcifc_service_call(cpu, run);
1145 break;
1146 default:
1147 r = -1;
1148 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl);
1149 break;
1150 }
1151
1152 return r;
1153 }
1154
1155 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
1156 {
1157 CPUS390XState *env = &cpu->env;
1158 int ret;
1159
1160 cpu_synchronize_state(CPU(cpu));
1161 ret = s390_virtio_hypercall(env);
1162 if (ret == -EINVAL) {
1163 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1164 return 0;
1165 }
1166
1167 return ret;
1168 }
1169
1170 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
1171 {
1172 uint64_t r1, r3;
1173
1174 cpu_synchronize_state(CPU(cpu));
1175 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4;
1176 r3 = run->s390_sieic.ipa & 0x000f;
1177 handle_diag_308(&cpu->env, r1, r3);
1178 }
1179
1180 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run)
1181 {
1182 CPUS390XState *env = &cpu->env;
1183 unsigned long pc;
1184
1185 cpu_synchronize_state(CPU(cpu));
1186
1187 pc = env->psw.addr - 4;
1188 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) {
1189 env->psw.addr = pc;
1190 return EXCP_DEBUG;
1191 }
1192
1193 return -ENOENT;
1194 }
1195
1196 #define DIAG_KVM_CODE_MASK 0x000000000000ffff
1197
1198 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
1199 {
1200 int r = 0;
1201 uint16_t func_code;
1202
1203 /*
1204 * For any diagnose call we support, bits 48-63 of the resulting
1205 * address specify the function code; the remainder is ignored.
1206 */
1207 func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
1208 switch (func_code) {
1209 case DIAG_IPL:
1210 kvm_handle_diag_308(cpu, run);
1211 break;
1212 case DIAG_KVM_HYPERCALL:
1213 r = handle_hypercall(cpu, run);
1214 break;
1215 case DIAG_KVM_BREAKPOINT:
1216 r = handle_sw_breakpoint(cpu, run);
1217 break;
1218 default:
1219 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
1220 enter_pgmcheck(cpu, PGM_SPECIFICATION);
1221 break;
1222 }
1223
1224 return r;
1225 }
1226
1227 typedef struct SigpInfo {
1228 S390CPU *cpu;
1229 uint64_t param;
1230 int cc;
1231 uint64_t *status_reg;
1232 } SigpInfo;
1233
1234 static void set_sigp_status(SigpInfo *si, uint64_t status)
1235 {
1236 *si->status_reg &= 0xffffffff00000000ULL;
1237 *si->status_reg |= status;
1238 si->cc = SIGP_CC_STATUS_STORED;
1239 }
1240
1241 static void sigp_start(void *arg)
1242 {
1243 SigpInfo *si = arg;
1244
1245 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1246 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1247 return;
1248 }
1249
1250 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1251 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1252 }
1253
1254 static void sigp_stop(void *arg)
1255 {
1256 SigpInfo *si = arg;
1257 struct kvm_s390_irq irq = {
1258 .type = KVM_S390_SIGP_STOP,
1259 };
1260
1261 if (s390_cpu_get_state(si->cpu) != CPU_STATE_OPERATING) {
1262 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1263 return;
1264 }
1265
1266 /* disabled wait - sleeping in user space */
1267 if (CPU(si->cpu)->halted) {
1268 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1269 } else {
1270 /* execute the stop function */
1271 si->cpu->env.sigp_order = SIGP_STOP;
1272 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1273 }
1274 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1275 }
1276
1277 #define KVM_S390_STORE_STATUS_DEF_ADDR offsetof(LowCore, floating_pt_save_area)
1278 #define SAVE_AREA_SIZE 512
1279 static int kvm_s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
1280 {
1281 static const uint8_t ar_id = 1;
1282 uint64_t ckc = cpu->env.ckc >> 8;
1283 void *mem;
1284 hwaddr len = SAVE_AREA_SIZE;
1285
1286 mem = cpu_physical_memory_map(addr, &len, 1);
1287 if (!mem) {
1288 return -EFAULT;
1289 }
1290 if (len != SAVE_AREA_SIZE) {
1291 cpu_physical_memory_unmap(mem, len, 1, 0);
1292 return -EFAULT;
1293 }
1294
1295 if (store_arch) {
1296 cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
1297 }
1298 memcpy(mem, &cpu->env.fregs, 128);
1299 memcpy(mem + 128, &cpu->env.regs, 128);
1300 memcpy(mem + 256, &cpu->env.psw, 16);
1301 memcpy(mem + 280, &cpu->env.psa, 4);
1302 memcpy(mem + 284, &cpu->env.fpc, 4);
1303 memcpy(mem + 292, &cpu->env.todpr, 4);
1304 memcpy(mem + 296, &cpu->env.cputm, 8);
1305 memcpy(mem + 304, &ckc, 8);
1306 memcpy(mem + 320, &cpu->env.aregs, 64);
1307 memcpy(mem + 384, &cpu->env.cregs, 128);
1308
1309 cpu_physical_memory_unmap(mem, len, 1, len);
1310
1311 return 0;
1312 }
1313
1314 static void sigp_stop_and_store_status(void *arg)
1315 {
1316 SigpInfo *si = arg;
1317 struct kvm_s390_irq irq = {
1318 .type = KVM_S390_SIGP_STOP,
1319 };
1320
1321 /* disabled wait - sleeping in user space */
1322 if (s390_cpu_get_state(si->cpu) == CPU_STATE_OPERATING &&
1323 CPU(si->cpu)->halted) {
1324 s390_cpu_set_state(CPU_STATE_STOPPED, si->cpu);
1325 }
1326
1327 switch (s390_cpu_get_state(si->cpu)) {
1328 case CPU_STATE_OPERATING:
1329 si->cpu->env.sigp_order = SIGP_STOP_STORE_STATUS;
1330 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1331 /* store will be performed when handling the stop intercept */
1332 break;
1333 case CPU_STATE_STOPPED:
1334 /* already stopped, just store the status */
1335 cpu_synchronize_state(CPU(si->cpu));
1336 kvm_s390_store_status(si->cpu, KVM_S390_STORE_STATUS_DEF_ADDR, true);
1337 break;
1338 }
1339 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1340 }
1341
1342 static void sigp_store_status_at_address(void *arg)
1343 {
1344 SigpInfo *si = arg;
1345 uint32_t address = si->param & 0x7ffffe00u;
1346
1347 /* cpu has to be stopped */
1348 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1349 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1350 return;
1351 }
1352
1353 cpu_synchronize_state(CPU(si->cpu));
1354
1355 if (kvm_s390_store_status(si->cpu, address, false)) {
1356 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1357 return;
1358 }
1359 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1360 }
1361
1362 static void sigp_restart(void *arg)
1363 {
1364 SigpInfo *si = arg;
1365 struct kvm_s390_irq irq = {
1366 .type = KVM_S390_RESTART,
1367 };
1368
1369 switch (s390_cpu_get_state(si->cpu)) {
1370 case CPU_STATE_STOPPED:
1371 /* the restart irq has to be delivered prior to any other pending irq */
1372 cpu_synchronize_state(CPU(si->cpu));
1373 do_restart_interrupt(&si->cpu->env);
1374 s390_cpu_set_state(CPU_STATE_OPERATING, si->cpu);
1375 break;
1376 case CPU_STATE_OPERATING:
1377 kvm_s390_vcpu_interrupt(si->cpu, &irq);
1378 break;
1379 }
1380 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1381 }
1382
1383 int kvm_s390_cpu_restart(S390CPU *cpu)
1384 {
1385 SigpInfo si = {
1386 .cpu = cpu,
1387 };
1388
1389 run_on_cpu(CPU(cpu), sigp_restart, &si);
1390 DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
1391 return 0;
1392 }
1393
1394 static void sigp_initial_cpu_reset(void *arg)
1395 {
1396 SigpInfo *si = arg;
1397 CPUState *cs = CPU(si->cpu);
1398 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1399
1400 cpu_synchronize_state(cs);
1401 scc->initial_cpu_reset(cs);
1402 cpu_synchronize_post_reset(cs);
1403 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1404 }
1405
1406 static void sigp_cpu_reset(void *arg)
1407 {
1408 SigpInfo *si = arg;
1409 CPUState *cs = CPU(si->cpu);
1410 S390CPUClass *scc = S390_CPU_GET_CLASS(si->cpu);
1411
1412 cpu_synchronize_state(cs);
1413 scc->cpu_reset(cs);
1414 cpu_synchronize_post_reset(cs);
1415 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1416 }
1417
1418 static void sigp_set_prefix(void *arg)
1419 {
1420 SigpInfo *si = arg;
1421 uint32_t addr = si->param & 0x7fffe000u;
1422
1423 cpu_synchronize_state(CPU(si->cpu));
1424
1425 if (!address_space_access_valid(&address_space_memory, addr,
1426 sizeof(struct LowCore), false)) {
1427 set_sigp_status(si, SIGP_STAT_INVALID_PARAMETER);
1428 return;
1429 }
1430
1431 /* cpu has to be stopped */
1432 if (s390_cpu_get_state(si->cpu) != CPU_STATE_STOPPED) {
1433 set_sigp_status(si, SIGP_STAT_INCORRECT_STATE);
1434 return;
1435 }
1436
1437 si->cpu->env.psa = addr;
1438 cpu_synchronize_post_init(CPU(si->cpu));
1439 si->cc = SIGP_CC_ORDER_CODE_ACCEPTED;
1440 }
1441
1442 static int handle_sigp_single_dst(S390CPU *dst_cpu, uint8_t order,
1443 uint64_t param, uint64_t *status_reg)
1444 {
1445 SigpInfo si = {
1446 .cpu = dst_cpu,
1447 .param = param,
1448 .status_reg = status_reg,
1449 };
1450
1451 /* cpu available? */
1452 if (dst_cpu == NULL) {
1453 return SIGP_CC_NOT_OPERATIONAL;
1454 }
1455
1456 /* only resets can break pending orders */
1457 if (dst_cpu->env.sigp_order != 0 &&
1458 order != SIGP_CPU_RESET &&
1459 order != SIGP_INITIAL_CPU_RESET) {
1460 return SIGP_CC_BUSY;
1461 }
1462
1463 switch (order) {
1464 case SIGP_START:
1465 run_on_cpu(CPU(dst_cpu), sigp_start, &si);
1466 break;
1467 case SIGP_STOP:
1468 run_on_cpu(CPU(dst_cpu), sigp_stop, &si);
1469 break;
1470 case SIGP_RESTART:
1471 run_on_cpu(CPU(dst_cpu), sigp_restart, &si);
1472 break;
1473 case SIGP_STOP_STORE_STATUS:
1474 run_on_cpu(CPU(dst_cpu), sigp_stop_and_store_status, &si);
1475 break;
1476 case SIGP_STORE_STATUS_ADDR:
1477 run_on_cpu(CPU(dst_cpu), sigp_store_status_at_address, &si);
1478 break;
1479 case SIGP_SET_PREFIX:
1480 run_on_cpu(CPU(dst_cpu), sigp_set_prefix, &si);
1481 break;
1482 case SIGP_INITIAL_CPU_RESET:
1483 run_on_cpu(CPU(dst_cpu), sigp_initial_cpu_reset, &si);
1484 break;
1485 case SIGP_CPU_RESET:
1486 run_on_cpu(CPU(dst_cpu), sigp_cpu_reset, &si);
1487 break;
1488 default:
1489 DPRINTF("KVM: unknown SIGP: 0x%x\n", order);
1490 set_sigp_status(&si, SIGP_STAT_INVALID_ORDER);
1491 }
1492
1493 return si.cc;
1494 }
1495
1496 static int sigp_set_architecture(S390CPU *cpu, uint32_t param,
1497 uint64_t *status_reg)
1498 {
1499 CPUState *cur_cs;
1500 S390CPU *cur_cpu;
1501
1502 /* due to the BQL, we are the only active cpu */
1503 CPU_FOREACH(cur_cs) {
1504 cur_cpu = S390_CPU(cur_cs);
1505 if (cur_cpu->env.sigp_order != 0) {
1506 return SIGP_CC_BUSY;
1507 }
1508 cpu_synchronize_state(cur_cs);
1509 /* all but the current one have to be stopped */
1510 if (cur_cpu != cpu &&
1511 s390_cpu_get_state(cur_cpu) != CPU_STATE_STOPPED) {
1512 *status_reg &= 0xffffffff00000000ULL;
1513 *status_reg |= SIGP_STAT_INCORRECT_STATE;
1514 return SIGP_CC_STATUS_STORED;
1515 }
1516 }
1517
1518 switch (param & 0xff) {
1519 case SIGP_MODE_ESA_S390:
1520 /* not supported */
1521 return SIGP_CC_NOT_OPERATIONAL;
1522 case SIGP_MODE_Z_ARCH_TRANS_ALL_PSW:
1523 case SIGP_MODE_Z_ARCH_TRANS_CUR_PSW:
1524 CPU_FOREACH(cur_cs) {
1525 cur_cpu = S390_CPU(cur_cs);
1526 cur_cpu->env.pfault_token = -1UL;
1527 }
1528 break;
1529 default:
1530 *status_reg &= 0xffffffff00000000ULL;
1531 *status_reg |= SIGP_STAT_INVALID_PARAMETER;
1532 return SIGP_CC_STATUS_STORED;
1533 }
1534
1535 return SIGP_CC_ORDER_CODE_ACCEPTED;
1536 }
1537
1538 #define SIGP_ORDER_MASK 0x000000ff
1539
1540 static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
1541 {
1542 CPUS390XState *env = &cpu->env;
1543 const uint8_t r1 = ipa1 >> 4;
1544 const uint8_t r3 = ipa1 & 0x0f;
1545 int ret;
1546 uint8_t order;
1547 uint64_t *status_reg;
1548 uint64_t param;
1549 S390CPU *dst_cpu = NULL;
1550
1551 cpu_synchronize_state(CPU(cpu));
1552
1553 /* get order code */
1554 order = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK;
1555 status_reg = &env->regs[r1];
1556 param = (r1 % 2) ? env->regs[r1] : env->regs[r1 + 1];
1557
1558 switch (order) {
1559 case SIGP_SET_ARCH:
1560 ret = sigp_set_architecture(cpu, param, status_reg);
1561 break;
1562 default:
1563 /* all other sigp orders target a single vcpu */
1564 dst_cpu = s390_cpu_addr2state(env->regs[r3]);
1565 ret = handle_sigp_single_dst(dst_cpu, order, param, status_reg);
1566 }
1567
1568 trace_kvm_sigp_finished(order, CPU(cpu)->cpu_index,
1569 dst_cpu ? CPU(dst_cpu)->cpu_index : -1, ret);
1570
1571 if (ret >= 0) {
1572 setcc(cpu, ret);
1573 return 0;
1574 }
1575
1576 return ret;
1577 }
1578
1579 static int handle_instruction(S390CPU *cpu, struct kvm_run *run)
1580 {
1581 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
1582 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
1583 int r = -1;
1584
1585 DPRINTF("handle_instruction 0x%x 0x%x\n",
1586 run->s390_sieic.ipa, run->s390_sieic.ipb);
1587 switch (ipa0) {
1588 case IPA0_B2:
1589 r = handle_b2(cpu, run, ipa1);
1590 break;
1591 case IPA0_B9:
1592 r = handle_b9(cpu, run, ipa1);
1593 break;
1594 case IPA0_EB:
1595 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff);
1596 break;
1597 case IPA0_E3:
1598 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff);
1599 break;
1600 case IPA0_DIAG:
1601 r = handle_diag(cpu, run, run->s390_sieic.ipb);
1602 break;
1603 case IPA0_SIGP:
1604 r = handle_sigp(cpu, run, ipa1);
1605 break;
1606 }
1607
1608 if (r < 0) {
1609 r = 0;
1610 enter_pgmcheck(cpu, 0x0001);
1611 }
1612
1613 return r;
1614 }
1615
1616 static bool is_special_wait_psw(CPUState *cs)
1617 {
1618 /* signal quiesce */
1619 return cs->kvm_run->psw_addr == 0xfffUL;
1620 }
1621
1622 static void guest_panicked(void)
1623 {
1624 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1625 &error_abort);
1626 vm_stop(RUN_STATE_GUEST_PANICKED);
1627 }
1628
1629 static void unmanageable_intercept(S390CPU *cpu, const char *str, int pswoffset)
1630 {
1631 CPUState *cs = CPU(cpu);
1632
1633 error_report("Unmanageable %s! CPU%i new PSW: 0x%016lx:%016lx",
1634 str, cs->cpu_index, ldq_phys(cs->as, cpu->env.psa + pswoffset),
1635 ldq_phys(cs->as, cpu->env.psa + pswoffset + 8));
1636 s390_cpu_halt(cpu);
1637 guest_panicked();
1638 }
1639
1640 static int handle_intercept(S390CPU *cpu)
1641 {
1642 CPUState *cs = CPU(cpu);
1643 struct kvm_run *run = cs->kvm_run;
1644 int icpt_code = run->s390_sieic.icptcode;
1645 int r = 0;
1646
1647 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
1648 (long)cs->kvm_run->psw_addr);
1649 switch (icpt_code) {
1650 case ICPT_INSTRUCTION:
1651 r = handle_instruction(cpu, run);
1652 break;
1653 case ICPT_PROGRAM:
1654 unmanageable_intercept(cpu, "program interrupt",
1655 offsetof(LowCore, program_new_psw));
1656 r = EXCP_HALTED;
1657 break;
1658 case ICPT_EXT_INT:
1659 unmanageable_intercept(cpu, "external interrupt",
1660 offsetof(LowCore, external_new_psw));
1661 r = EXCP_HALTED;
1662 break;
1663 case ICPT_WAITPSW:
1664 /* disabled wait, since enabled wait is handled in kernel */
1665 cpu_synchronize_state(cs);
1666 if (s390_cpu_halt(cpu) == 0) {
1667 if (is_special_wait_psw(cs)) {
1668 qemu_system_shutdown_request();
1669 } else {
1670 guest_panicked();
1671 }
1672 }
1673 r = EXCP_HALTED;
1674 break;
1675 case ICPT_CPU_STOP:
1676 if (s390_cpu_set_state(CPU_STATE_STOPPED, cpu) == 0) {
1677 qemu_system_shutdown_request();
1678 }
1679 if (cpu->env.sigp_order == SIGP_STOP_STORE_STATUS) {
1680 kvm_s390_store_status(cpu, KVM_S390_STORE_STATUS_DEF_ADDR,
1681 true);
1682 }
1683 cpu->env.sigp_order = 0;
1684 r = EXCP_HALTED;
1685 break;
1686 case ICPT_SOFT_INTERCEPT:
1687 fprintf(stderr, "KVM unimplemented icpt SOFT\n");
1688 exit(1);
1689 break;
1690 case ICPT_IO:
1691 fprintf(stderr, "KVM unimplemented icpt IO\n");
1692 exit(1);
1693 break;
1694 default:
1695 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
1696 exit(1);
1697 break;
1698 }
1699
1700 return r;
1701 }
1702
1703 static int handle_tsch(S390CPU *cpu)
1704 {
1705 CPUState *cs = CPU(cpu);
1706 struct kvm_run *run = cs->kvm_run;
1707 int ret;
1708
1709 cpu_synchronize_state(cs);
1710
1711 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb);
1712 if (ret < 0) {
1713 /*
1714 * Failure.
1715 * If an I/O interrupt had been dequeued, we have to reinject it.
1716 */
1717 if (run->s390_tsch.dequeued) {
1718 kvm_s390_io_interrupt(run->s390_tsch.subchannel_id,
1719 run->s390_tsch.subchannel_nr,
1720 run->s390_tsch.io_int_parm,
1721 run->s390_tsch.io_int_word);
1722 }
1723 ret = 0;
1724 }
1725 return ret;
1726 }
1727
1728 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr)
1729 {
1730 struct sysib_322 sysib;
1731 int del;
1732
1733 if (s390_cpu_virt_mem_read(cpu, addr, &sysib, sizeof(sysib))) {
1734 return;
1735 }
1736 /* Shift the stack of Extended Names to prepare for our own data */
1737 memmove(&sysib.ext_names[1], &sysib.ext_names[0],
1738 sizeof(sysib.ext_names[0]) * (sysib.count - 1));
1739 /* First virt level, that doesn't provide Ext Names delimits stack. It is
1740 * assumed it's not capable of managing Extended Names for lower levels.
1741 */
1742 for (del = 1; del < sysib.count; del++) {
1743 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) {
1744 break;
1745 }
1746 }
1747 if (del < sysib.count) {
1748 memset(sysib.ext_names[del], 0,
1749 sizeof(sysib.ext_names[0]) * (sysib.count - del));
1750 }
1751 /* Insert short machine name in EBCDIC, padded with blanks */
1752 if (qemu_name) {
1753 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name));
1754 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name),
1755 strlen(qemu_name)));
1756 }
1757 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */
1758 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0]));
1759 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's
1760 * considered by s390 as not capable of providing any Extended Name.
1761 * Therefore if no name was specified on qemu invocation, we go with the
1762 * same "KVMguest" default, which KVM has filled into short name field.
1763 */
1764 if (qemu_name) {
1765 strncpy((char *)sysib.ext_names[0], qemu_name,
1766 sizeof(sysib.ext_names[0]));
1767 } else {
1768 strcpy((char *)sysib.ext_names[0], "KVMguest");
1769 }
1770 /* Insert UUID */
1771 memcpy(sysib.vm[0].uuid, qemu_uuid, sizeof(sysib.vm[0].uuid));
1772
1773 s390_cpu_virt_mem_write(cpu, addr, &sysib, sizeof(sysib));
1774 }
1775
1776 static int handle_stsi(S390CPU *cpu)
1777 {
1778 CPUState *cs = CPU(cpu);
1779 struct kvm_run *run = cs->kvm_run;
1780
1781 switch (run->s390_stsi.fc) {
1782 case 3:
1783 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) {
1784 return 0;
1785 }
1786 /* Only sysib 3.2.2 needs post-handling for now. */
1787 insert_stsi_3_2_2(cpu, run->s390_stsi.addr);
1788 return 0;
1789 default:
1790 return 0;
1791 }
1792 }
1793
1794 static int kvm_arch_handle_debug_exit(S390CPU *cpu)
1795 {
1796 CPUState *cs = CPU(cpu);
1797 struct kvm_run *run = cs->kvm_run;
1798
1799 int ret = 0;
1800 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1801
1802 switch (arch_info->type) {
1803 case KVM_HW_WP_WRITE:
1804 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1805 cs->watchpoint_hit = &hw_watchpoint;
1806 hw_watchpoint.vaddr = arch_info->addr;
1807 hw_watchpoint.flags = BP_MEM_WRITE;
1808 ret = EXCP_DEBUG;
1809 }
1810 break;
1811 case KVM_HW_BP:
1812 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) {
1813 ret = EXCP_DEBUG;
1814 }
1815 break;
1816 case KVM_SINGLESTEP:
1817 if (cs->singlestep_enabled) {
1818 ret = EXCP_DEBUG;
1819 }
1820 break;
1821 default:
1822 ret = -ENOSYS;
1823 }
1824
1825 return ret;
1826 }
1827
1828 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1829 {
1830 S390CPU *cpu = S390_CPU(cs);
1831 int ret = 0;
1832
1833 switch (run->exit_reason) {
1834 case KVM_EXIT_S390_SIEIC:
1835 ret = handle_intercept(cpu);
1836 break;
1837 case KVM_EXIT_S390_RESET:
1838 s390_reipl_request();
1839 break;
1840 case KVM_EXIT_S390_TSCH:
1841 ret = handle_tsch(cpu);
1842 break;
1843 case KVM_EXIT_S390_STSI:
1844 ret = handle_stsi(cpu);
1845 break;
1846 case KVM_EXIT_DEBUG:
1847 ret = kvm_arch_handle_debug_exit(cpu);
1848 break;
1849 default:
1850 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
1851 break;
1852 }
1853
1854 if (ret == 0) {
1855 ret = EXCP_INTERRUPT;
1856 }
1857 return ret;
1858 }
1859
1860 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1861 {
1862 return true;
1863 }
1864
1865 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
1866 {
1867 return 1;
1868 }
1869
1870 int kvm_arch_on_sigbus(int code, void *addr)
1871 {
1872 return 1;
1873 }
1874
1875 void kvm_s390_io_interrupt(uint16_t subchannel_id,
1876 uint16_t subchannel_nr, uint32_t io_int_parm,
1877 uint32_t io_int_word)
1878 {
1879 struct kvm_s390_irq irq = {
1880 .u.io.subchannel_id = subchannel_id,
1881 .u.io.subchannel_nr = subchannel_nr,
1882 .u.io.io_int_parm = io_int_parm,
1883 .u.io.io_int_word = io_int_word,
1884 };
1885
1886 if (io_int_word & IO_INT_WORD_AI) {
1887 irq.type = KVM_S390_INT_IO(1, 0, 0, 0);
1888 } else {
1889 irq.type = ((subchannel_id & 0xff00) << 24) |
1890 ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
1891 }
1892 kvm_s390_floating_interrupt(&irq);
1893 }
1894
1895 void kvm_s390_crw_mchk(void)
1896 {
1897 struct kvm_s390_irq irq = {
1898 .type = KVM_S390_MCHK,
1899 .u.mchk.cr14 = 1 << 28,
1900 .u.mchk.mcic = 0x00400f1d40330000ULL,
1901 };
1902 kvm_s390_floating_interrupt(&irq);
1903 }
1904
1905 void kvm_s390_enable_css_support(S390CPU *cpu)
1906 {
1907 int r;
1908
1909 /* Activate host kernel channel subsystem support. */
1910 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0);
1911 assert(r == 0);
1912 }
1913
1914 void kvm_arch_init_irq_routing(KVMState *s)
1915 {
1916 /*
1917 * Note that while irqchip capabilities generally imply that cpustates
1918 * are handled in-kernel, it is not true for s390 (yet); therefore, we
1919 * have to override the common code kvm_halt_in_kernel_allowed setting.
1920 */
1921 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
1922 kvm_gsi_routing_allowed = true;
1923 kvm_halt_in_kernel_allowed = false;
1924 }
1925 }
1926
1927 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
1928 int vq, bool assign)
1929 {
1930 struct kvm_ioeventfd kick = {
1931 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
1932 KVM_IOEVENTFD_FLAG_DATAMATCH,
1933 .fd = event_notifier_get_fd(notifier),
1934 .datamatch = vq,
1935 .addr = sch,
1936 .len = 8,
1937 };
1938 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
1939 return -ENOSYS;
1940 }
1941 if (!assign) {
1942 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
1943 }
1944 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
1945 }
1946
1947 int kvm_s390_get_memslot_count(KVMState *s)
1948 {
1949 return kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
1950 }
1951
1952 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state)
1953 {
1954 struct kvm_mp_state mp_state = {};
1955 int ret;
1956
1957 /* the kvm part might not have been initialized yet */
1958 if (CPU(cpu)->kvm_state == NULL) {
1959 return 0;
1960 }
1961
1962 switch (cpu_state) {
1963 case CPU_STATE_STOPPED:
1964 mp_state.mp_state = KVM_MP_STATE_STOPPED;
1965 break;
1966 case CPU_STATE_CHECK_STOP:
1967 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP;
1968 break;
1969 case CPU_STATE_OPERATING:
1970 mp_state.mp_state = KVM_MP_STATE_OPERATING;
1971 break;
1972 case CPU_STATE_LOAD:
1973 mp_state.mp_state = KVM_MP_STATE_LOAD;
1974 break;
1975 default:
1976 error_report("Requested CPU state is not a valid S390 CPU state: %u",
1977 cpu_state);
1978 exit(1);
1979 }
1980
1981 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
1982 if (ret) {
1983 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state,
1984 strerror(-ret));
1985 }
1986
1987 return ret;
1988 }
1989
1990 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1991 uint64_t address, uint32_t data)
1992 {
1993 S390PCIBusDevice *pbdev;
1994 uint32_t fid = data >> ZPCI_MSI_VEC_BITS;
1995 uint32_t vec = data & ZPCI_MSI_VEC_MASK;
1996
1997 pbdev = s390_pci_find_dev_by_fid(fid);
1998 if (!pbdev) {
1999 DPRINTF("add_msi_route no dev\n");
2000 return -ENODEV;
2001 }
2002
2003 pbdev->routes.adapter.ind_offset = vec;
2004
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;
2010 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset;
2011 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
2012 return 0;
2013 }