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