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