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