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