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