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