2 * ARM implementation of KVM hooks
4 * Copyright Christoffer Dall 2009-2010
5 * Copyright Mian-M. Hamayun 2013, Virtual Open Systems
6 * Copyright Alex Bennée 2014, Linaro
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include <sys/ioctl.h>
16 #include <linux/kvm.h>
18 #include "qemu/timer.h"
19 #include "qemu/error-report.h"
20 #include "qemu/main-loop.h"
21 #include "qom/object.h"
22 #include "qapi/error.h"
23 #include "sysemu/sysemu.h"
24 #include "sysemu/runstate.h"
25 #include "sysemu/kvm.h"
26 #include "sysemu/kvm_int.h"
30 #include "internals.h"
31 #include "hw/pci/pci.h"
32 #include "exec/memattrs.h"
33 #include "exec/address-spaces.h"
34 #include "gdbstub/enums.h"
35 #include "hw/boards.h"
37 #include "qapi/visitor.h"
39 #include "hw/acpi/acpi.h"
40 #include "hw/acpi/ghes.h"
41 #include "target/arm/gtimer.h"
43 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
47 static bool cap_has_mp_state
;
48 static bool cap_has_inject_serror_esr
;
49 static bool cap_has_inject_ext_dabt
;
52 * ARMHostCPUFeatures: information about the host CPU (identified
53 * by asking the host kernel)
55 typedef struct ARMHostCPUFeatures
{
59 const char *dtb_compatible
;
62 static ARMHostCPUFeatures arm_host_cpu_features
;
68 * Initialize (or reinitialize) the VCPU by invoking the
69 * KVM_ARM_VCPU_INIT ioctl with the CPU type and feature
70 * bitmask specified in the CPUState.
72 * Returns: 0 if success else < 0 error code
74 static int kvm_arm_vcpu_init(ARMCPU
*cpu
)
76 struct kvm_vcpu_init init
;
78 init
.target
= cpu
->kvm_target
;
79 memcpy(init
.features
, cpu
->kvm_init_features
, sizeof(init
.features
));
81 return kvm_vcpu_ioctl(CPU(cpu
), KVM_ARM_VCPU_INIT
, &init
);
85 * kvm_arm_vcpu_finalize:
87 * @feature: feature to finalize
89 * Finalizes the configuration of the specified VCPU feature by
90 * invoking the KVM_ARM_VCPU_FINALIZE ioctl. Features requiring
91 * this are documented in the "KVM_ARM_VCPU_FINALIZE" section of
92 * KVM's API documentation.
94 * Returns: 0 if success else < 0 error code
96 static int kvm_arm_vcpu_finalize(ARMCPU
*cpu
, int feature
)
98 return kvm_vcpu_ioctl(CPU(cpu
), KVM_ARM_VCPU_FINALIZE
, &feature
);
101 bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try
,
103 struct kvm_vcpu_init
*init
)
105 int ret
= 0, kvmfd
= -1, vmfd
= -1, cpufd
= -1;
108 kvmfd
= qemu_open_old("/dev/kvm", O_RDWR
);
112 max_vm_pa_size
= ioctl(kvmfd
, KVM_CHECK_EXTENSION
, KVM_CAP_ARM_VM_IPA_SIZE
);
113 if (max_vm_pa_size
< 0) {
117 vmfd
= ioctl(kvmfd
, KVM_CREATE_VM
, max_vm_pa_size
);
118 } while (vmfd
== -1 && errno
== EINTR
);
122 cpufd
= ioctl(vmfd
, KVM_CREATE_VCPU
, 0);
128 /* Caller doesn't want the VCPU to be initialized, so skip it */
132 if (init
->target
== -1) {
133 struct kvm_vcpu_init preferred
;
135 ret
= ioctl(vmfd
, KVM_ARM_PREFERRED_TARGET
, &preferred
);
137 init
->target
= preferred
.target
;
141 ret
= ioctl(cpufd
, KVM_ARM_VCPU_INIT
, init
);
145 } else if (cpus_to_try
) {
146 /* Old kernel which doesn't know about the
147 * PREFERRED_TARGET ioctl: we know it will only support
148 * creating one kind of guest CPU which is its preferred
151 struct kvm_vcpu_init
try;
153 while (*cpus_to_try
!= QEMU_KVM_ARM_TARGET_NONE
) {
154 try.target
= *cpus_to_try
++;
155 memcpy(try.features
, init
->features
, sizeof(init
->features
));
156 ret
= ioctl(cpufd
, KVM_ARM_VCPU_INIT
, &try);
164 init
->target
= try.target
;
166 /* Treat a NULL cpus_to_try argument the same as an empty
167 * list, which means we will fail the call since this must
168 * be an old kernel which doesn't support PREFERRED_TARGET.
194 void kvm_arm_destroy_scratch_host_vcpu(int *fdarray
)
198 for (i
= 2; i
>= 0; i
--) {
203 static int read_sys_reg32(int fd
, uint32_t *pret
, uint64_t id
)
206 struct kvm_one_reg idreg
= { .id
= id
, .addr
= (uintptr_t)&ret
};
209 assert((id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
);
210 err
= ioctl(fd
, KVM_GET_ONE_REG
, &idreg
);
218 static int read_sys_reg64(int fd
, uint64_t *pret
, uint64_t id
)
220 struct kvm_one_reg idreg
= { .id
= id
, .addr
= (uintptr_t)pret
};
222 assert((id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
);
223 return ioctl(fd
, KVM_GET_ONE_REG
, &idreg
);
226 static bool kvm_arm_pauth_supported(void)
228 return (kvm_check_extension(kvm_state
, KVM_CAP_ARM_PTRAUTH_ADDRESS
) &&
229 kvm_check_extension(kvm_state
, KVM_CAP_ARM_PTRAUTH_GENERIC
));
232 static bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures
*ahcf
)
234 /* Identify the feature bits corresponding to the host CPU, and
235 * fill out the ARMHostCPUClass fields accordingly. To do this
236 * we have to create a scratch VM, create a single CPU inside it,
237 * and then query that CPU for the relevant ID registers.
241 bool pmu_supported
= false;
242 uint64_t features
= 0;
245 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
246 * we know these will only support creating one kind of guest CPU,
247 * which is its preferred CPU type. Fortunately these old kernels
248 * support only a very limited number of CPUs.
250 static const uint32_t cpus_to_try
[] = {
251 KVM_ARM_TARGET_AEM_V8
,
252 KVM_ARM_TARGET_FOUNDATION_V8
,
253 KVM_ARM_TARGET_CORTEX_A57
,
254 QEMU_KVM_ARM_TARGET_NONE
257 * target = -1 informs kvm_arm_create_scratch_host_vcpu()
258 * to use the preferred target
260 struct kvm_vcpu_init init
= { .target
= -1, };
263 * Ask for SVE if supported, so that we can query ID_AA64ZFR0,
264 * which is otherwise RAZ.
266 sve_supported
= kvm_arm_sve_supported();
268 init
.features
[0] |= 1 << KVM_ARM_VCPU_SVE
;
272 * Ask for Pointer Authentication if supported, so that we get
273 * the unsanitized field values for AA64ISAR1_EL1.
275 if (kvm_arm_pauth_supported()) {
276 init
.features
[0] |= (1 << KVM_ARM_VCPU_PTRAUTH_ADDRESS
|
277 1 << KVM_ARM_VCPU_PTRAUTH_GENERIC
);
280 if (kvm_arm_pmu_supported()) {
281 init
.features
[0] |= 1 << KVM_ARM_VCPU_PMU_V3
;
282 pmu_supported
= true;
283 features
|= 1ULL << ARM_FEATURE_PMU
;
286 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try
, fdarray
, &init
)) {
290 ahcf
->target
= init
.target
;
291 ahcf
->dtb_compatible
= "arm,arm-v8";
293 err
= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64pfr0
,
294 ARM64_SYS_REG(3, 0, 0, 4, 0));
295 if (unlikely(err
< 0)) {
297 * Before v4.15, the kernel only exposed a limited number of system
298 * registers, not including any of the interesting AArch64 ID regs.
299 * For the most part we could leave these fields as zero with minimal
300 * effect, since this does not affect the values seen by the guest.
302 * However, it could cause problems down the line for QEMU,
303 * so provide a minimal v8.0 default.
305 * ??? Could read MIDR and use knowledge from cpu64.c.
306 * ??? Could map a page of memory into our temp guest and
307 * run the tiniest of hand-crafted kernels to extract
308 * the values seen by the guest.
309 * ??? Either of these sounds like too much effort just
310 * to work around running a modern host kernel.
312 ahcf
->isar
.id_aa64pfr0
= 0x00000011; /* EL1&0, AArch64 only */
315 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64pfr1
,
316 ARM64_SYS_REG(3, 0, 0, 4, 1));
317 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64smfr0
,
318 ARM64_SYS_REG(3, 0, 0, 4, 5));
319 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64dfr0
,
320 ARM64_SYS_REG(3, 0, 0, 5, 0));
321 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64dfr1
,
322 ARM64_SYS_REG(3, 0, 0, 5, 1));
323 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64isar0
,
324 ARM64_SYS_REG(3, 0, 0, 6, 0));
325 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64isar1
,
326 ARM64_SYS_REG(3, 0, 0, 6, 1));
327 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64isar2
,
328 ARM64_SYS_REG(3, 0, 0, 6, 2));
329 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64mmfr0
,
330 ARM64_SYS_REG(3, 0, 0, 7, 0));
331 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64mmfr1
,
332 ARM64_SYS_REG(3, 0, 0, 7, 1));
333 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64mmfr2
,
334 ARM64_SYS_REG(3, 0, 0, 7, 2));
335 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64mmfr3
,
336 ARM64_SYS_REG(3, 0, 0, 7, 3));
339 * Note that if AArch32 support is not present in the host,
340 * the AArch32 sysregs are present to be read, but will
341 * return UNKNOWN values. This is neither better nor worse
342 * than skipping the reads and leaving 0, as we must avoid
343 * considering the values in every case.
345 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_pfr0
,
346 ARM64_SYS_REG(3, 0, 0, 1, 0));
347 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_pfr1
,
348 ARM64_SYS_REG(3, 0, 0, 1, 1));
349 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_dfr0
,
350 ARM64_SYS_REG(3, 0, 0, 1, 2));
351 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_mmfr0
,
352 ARM64_SYS_REG(3, 0, 0, 1, 4));
353 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_mmfr1
,
354 ARM64_SYS_REG(3, 0, 0, 1, 5));
355 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_mmfr2
,
356 ARM64_SYS_REG(3, 0, 0, 1, 6));
357 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_mmfr3
,
358 ARM64_SYS_REG(3, 0, 0, 1, 7));
359 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar0
,
360 ARM64_SYS_REG(3, 0, 0, 2, 0));
361 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar1
,
362 ARM64_SYS_REG(3, 0, 0, 2, 1));
363 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar2
,
364 ARM64_SYS_REG(3, 0, 0, 2, 2));
365 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar3
,
366 ARM64_SYS_REG(3, 0, 0, 2, 3));
367 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar4
,
368 ARM64_SYS_REG(3, 0, 0, 2, 4));
369 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar5
,
370 ARM64_SYS_REG(3, 0, 0, 2, 5));
371 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_mmfr4
,
372 ARM64_SYS_REG(3, 0, 0, 2, 6));
373 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_isar6
,
374 ARM64_SYS_REG(3, 0, 0, 2, 7));
376 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.mvfr0
,
377 ARM64_SYS_REG(3, 0, 0, 3, 0));
378 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.mvfr1
,
379 ARM64_SYS_REG(3, 0, 0, 3, 1));
380 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.mvfr2
,
381 ARM64_SYS_REG(3, 0, 0, 3, 2));
382 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_pfr2
,
383 ARM64_SYS_REG(3, 0, 0, 3, 4));
384 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_dfr1
,
385 ARM64_SYS_REG(3, 0, 0, 3, 5));
386 err
|= read_sys_reg32(fdarray
[2], &ahcf
->isar
.id_mmfr5
,
387 ARM64_SYS_REG(3, 0, 0, 3, 6));
390 * DBGDIDR is a bit complicated because the kernel doesn't
391 * provide an accessor for it in 64-bit mode, which is what this
392 * scratch VM is in, and there's no architected "64-bit sysreg
393 * which reads the same as the 32-bit register" the way there is
394 * for other ID registers. Instead we synthesize a value from the
395 * AArch64 ID_AA64DFR0, the same way the kernel code in
396 * arch/arm64/kvm/sys_regs.c:trap_dbgidr() does.
397 * We only do this if the CPU supports AArch32 at EL1.
399 if (FIELD_EX32(ahcf
->isar
.id_aa64pfr0
, ID_AA64PFR0
, EL1
) >= 2) {
400 int wrps
= FIELD_EX64(ahcf
->isar
.id_aa64dfr0
, ID_AA64DFR0
, WRPS
);
401 int brps
= FIELD_EX64(ahcf
->isar
.id_aa64dfr0
, ID_AA64DFR0
, BRPS
);
403 FIELD_EX64(ahcf
->isar
.id_aa64dfr0
, ID_AA64DFR0
, CTX_CMPS
);
404 int version
= 6; /* ARMv8 debug architecture */
406 !!FIELD_EX32(ahcf
->isar
.id_aa64pfr0
, ID_AA64PFR0
, EL3
);
407 uint32_t dbgdidr
= 0;
409 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, WRPS
, wrps
);
410 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, BRPS
, brps
);
411 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, CTX_CMPS
, ctx_cmps
);
412 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, VERSION
, version
);
413 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, NSUHD_IMP
, has_el3
);
414 dbgdidr
= FIELD_DP32(dbgdidr
, DBGDIDR
, SE_IMP
, has_el3
);
415 dbgdidr
|= (1 << 15); /* RES1 bit */
416 ahcf
->isar
.dbgdidr
= dbgdidr
;
420 /* PMCR_EL0 is only accessible if the vCPU has feature PMU_V3 */
421 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.reset_pmcr_el0
,
422 ARM64_SYS_REG(3, 3, 9, 12, 0));
427 * There is a range of kernels between kernel commit 73433762fcae
428 * and f81cb2c3ad41 which have a bug where the kernel doesn't
429 * expose SYS_ID_AA64ZFR0_EL1 via the ONE_REG API unless the VM has
430 * enabled SVE support, which resulted in an error rather than RAZ.
431 * So only read the register if we set KVM_ARM_VCPU_SVE above.
433 err
|= read_sys_reg64(fdarray
[2], &ahcf
->isar
.id_aa64zfr0
,
434 ARM64_SYS_REG(3, 0, 0, 4, 4));
438 kvm_arm_destroy_scratch_host_vcpu(fdarray
);
445 * We can assume any KVM supporting CPU is at least a v8
446 * with VFPv4+Neon; this in turn implies most of the other
449 features
|= 1ULL << ARM_FEATURE_V8
;
450 features
|= 1ULL << ARM_FEATURE_NEON
;
451 features
|= 1ULL << ARM_FEATURE_AARCH64
;
452 features
|= 1ULL << ARM_FEATURE_GENERIC_TIMER
;
454 ahcf
->features
= features
;
459 void kvm_arm_set_cpu_features_from_host(ARMCPU
*cpu
)
461 CPUARMState
*env
= &cpu
->env
;
463 if (!arm_host_cpu_features
.dtb_compatible
) {
464 if (!kvm_enabled() ||
465 !kvm_arm_get_host_cpu_features(&arm_host_cpu_features
)) {
466 /* We can't report this error yet, so flag that we need to
467 * in arm_cpu_realizefn().
469 cpu
->kvm_target
= QEMU_KVM_ARM_TARGET_NONE
;
470 cpu
->host_cpu_probe_failed
= true;
475 cpu
->kvm_target
= arm_host_cpu_features
.target
;
476 cpu
->dtb_compatible
= arm_host_cpu_features
.dtb_compatible
;
477 cpu
->isar
= arm_host_cpu_features
.isar
;
478 env
->features
= arm_host_cpu_features
.features
;
481 static bool kvm_no_adjvtime_get(Object
*obj
, Error
**errp
)
483 return !ARM_CPU(obj
)->kvm_adjvtime
;
486 static void kvm_no_adjvtime_set(Object
*obj
, bool value
, Error
**errp
)
488 ARM_CPU(obj
)->kvm_adjvtime
= !value
;
491 static bool kvm_steal_time_get(Object
*obj
, Error
**errp
)
493 return ARM_CPU(obj
)->kvm_steal_time
!= ON_OFF_AUTO_OFF
;
496 static void kvm_steal_time_set(Object
*obj
, bool value
, Error
**errp
)
498 ARM_CPU(obj
)->kvm_steal_time
= value
? ON_OFF_AUTO_ON
: ON_OFF_AUTO_OFF
;
501 /* KVM VCPU properties should be prefixed with "kvm-". */
502 void kvm_arm_add_vcpu_properties(ARMCPU
*cpu
)
504 CPUARMState
*env
= &cpu
->env
;
505 Object
*obj
= OBJECT(cpu
);
507 if (arm_feature(env
, ARM_FEATURE_GENERIC_TIMER
)) {
508 cpu
->kvm_adjvtime
= true;
509 object_property_add_bool(obj
, "kvm-no-adjvtime", kvm_no_adjvtime_get
,
510 kvm_no_adjvtime_set
);
511 object_property_set_description(obj
, "kvm-no-adjvtime",
512 "Set on to disable the adjustment of "
513 "the virtual counter. VM stopped time "
517 cpu
->kvm_steal_time
= ON_OFF_AUTO_AUTO
;
518 object_property_add_bool(obj
, "kvm-steal-time", kvm_steal_time_get
,
520 object_property_set_description(obj
, "kvm-steal-time",
521 "Set off to disable KVM steal time.");
524 bool kvm_arm_pmu_supported(void)
526 return kvm_check_extension(kvm_state
, KVM_CAP_ARM_PMU_V3
);
529 int kvm_arm_get_max_vm_ipa_size(MachineState
*ms
, bool *fixed_ipa
)
531 KVMState
*s
= KVM_STATE(ms
->accelerator
);
534 ret
= kvm_check_extension(s
, KVM_CAP_ARM_VM_IPA_SIZE
);
535 *fixed_ipa
= ret
<= 0;
537 return ret
> 0 ? ret
: 40;
540 int kvm_arch_get_default_type(MachineState
*ms
)
543 int size
= kvm_arm_get_max_vm_ipa_size(ms
, &fixed_ipa
);
544 return fixed_ipa
? 0 : size
;
547 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
550 /* For ARM interrupt delivery is always asynchronous,
551 * whether we are using an in-kernel VGIC or not.
553 kvm_async_interrupts_allowed
= true;
556 * PSCI wakes up secondary cores, so we always need to
557 * have vCPUs waiting in kernel space
559 kvm_halt_in_kernel_allowed
= true;
561 cap_has_mp_state
= kvm_check_extension(s
, KVM_CAP_MP_STATE
);
563 /* Check whether user space can specify guest syndrome value */
564 cap_has_inject_serror_esr
=
565 kvm_check_extension(s
, KVM_CAP_ARM_INJECT_SERROR_ESR
);
567 if (ms
->smp
.cpus
> 256 &&
568 !kvm_check_extension(s
, KVM_CAP_ARM_IRQ_LINE_LAYOUT_2
)) {
569 error_report("Using more than 256 vcpus requires a host kernel "
570 "with KVM_CAP_ARM_IRQ_LINE_LAYOUT_2");
574 if (kvm_check_extension(s
, KVM_CAP_ARM_NISV_TO_USER
)) {
575 if (kvm_vm_enable_cap(s
, KVM_CAP_ARM_NISV_TO_USER
, 0)) {
576 error_report("Failed to enable KVM_CAP_ARM_NISV_TO_USER cap");
578 /* Set status for supporting the external dabt injection */
579 cap_has_inject_ext_dabt
= kvm_check_extension(s
,
580 KVM_CAP_ARM_INJECT_EXT_DABT
);
584 if (s
->kvm_eager_split_size
) {
587 sizes
= kvm_vm_check_extension(s
, KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES
);
589 s
->kvm_eager_split_size
= 0;
590 warn_report("Eager Page Split support not available");
591 } else if (!(s
->kvm_eager_split_size
& sizes
)) {
592 error_report("Eager Page Split requested chunk size not valid");
595 ret
= kvm_vm_enable_cap(s
, KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE
, 0,
596 s
->kvm_eager_split_size
);
598 error_report("Enabling of Eager Page Split failed: %s",
604 max_hw_wps
= kvm_check_extension(s
, KVM_CAP_GUEST_DEBUG_HW_WPS
);
605 hw_watchpoints
= g_array_sized_new(true, true,
606 sizeof(HWWatchpoint
), max_hw_wps
);
608 max_hw_bps
= kvm_check_extension(s
, KVM_CAP_GUEST_DEBUG_HW_BPS
);
609 hw_breakpoints
= g_array_sized_new(true, true,
610 sizeof(HWBreakpoint
), max_hw_bps
);
615 unsigned long kvm_arch_vcpu_id(CPUState
*cpu
)
617 return cpu
->cpu_index
;
620 /* We track all the KVM devices which need their memory addresses
621 * passing to the kernel in a list of these structures.
622 * When board init is complete we run through the list and
623 * tell the kernel the base addresses of the memory regions.
624 * We use a MemoryListener to track mapping and unmapping of
625 * the regions during board creation, so the board models don't
626 * need to do anything special for the KVM case.
628 * Sometimes the address must be OR'ed with some other fields
629 * (for example for KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION).
630 * @kda_addr_ormask aims at storing the value of those fields.
632 typedef struct KVMDevice
{
633 struct kvm_arm_device_addr kda
;
634 struct kvm_device_attr kdattr
;
635 uint64_t kda_addr_ormask
;
637 QSLIST_ENTRY(KVMDevice
) entries
;
641 static QSLIST_HEAD(, KVMDevice
) kvm_devices_head
;
643 static void kvm_arm_devlistener_add(MemoryListener
*listener
,
644 MemoryRegionSection
*section
)
648 QSLIST_FOREACH(kd
, &kvm_devices_head
, entries
) {
649 if (section
->mr
== kd
->mr
) {
650 kd
->kda
.addr
= section
->offset_within_address_space
;
655 static void kvm_arm_devlistener_del(MemoryListener
*listener
,
656 MemoryRegionSection
*section
)
660 QSLIST_FOREACH(kd
, &kvm_devices_head
, entries
) {
661 if (section
->mr
== kd
->mr
) {
667 static MemoryListener devlistener
= {
669 .region_add
= kvm_arm_devlistener_add
,
670 .region_del
= kvm_arm_devlistener_del
,
671 .priority
= MEMORY_LISTENER_PRIORITY_MIN
,
674 static void kvm_arm_set_device_addr(KVMDevice
*kd
)
676 struct kvm_device_attr
*attr
= &kd
->kdattr
;
679 /* If the device control API is available and we have a device fd on the
680 * KVMDevice struct, let's use the newer API
682 if (kd
->dev_fd
>= 0) {
683 uint64_t addr
= kd
->kda
.addr
;
685 addr
|= kd
->kda_addr_ormask
;
686 attr
->addr
= (uintptr_t)&addr
;
687 ret
= kvm_device_ioctl(kd
->dev_fd
, KVM_SET_DEVICE_ATTR
, attr
);
689 ret
= kvm_vm_ioctl(kvm_state
, KVM_ARM_SET_DEVICE_ADDR
, &kd
->kda
);
693 fprintf(stderr
, "Failed to set device address: %s\n",
699 static void kvm_arm_machine_init_done(Notifier
*notifier
, void *data
)
703 QSLIST_FOREACH_SAFE(kd
, &kvm_devices_head
, entries
, tkd
) {
704 if (kd
->kda
.addr
!= -1) {
705 kvm_arm_set_device_addr(kd
);
707 memory_region_unref(kd
->mr
);
708 QSLIST_REMOVE_HEAD(&kvm_devices_head
, entries
);
711 memory_listener_unregister(&devlistener
);
714 static Notifier notify
= {
715 .notify
= kvm_arm_machine_init_done
,
718 void kvm_arm_register_device(MemoryRegion
*mr
, uint64_t devid
, uint64_t group
,
719 uint64_t attr
, int dev_fd
, uint64_t addr_ormask
)
723 if (!kvm_irqchip_in_kernel()) {
727 if (QSLIST_EMPTY(&kvm_devices_head
)) {
728 memory_listener_register(&devlistener
, &address_space_memory
);
729 qemu_add_machine_init_done_notifier(¬ify
);
731 kd
= g_new0(KVMDevice
, 1);
735 kd
->kdattr
.flags
= 0;
736 kd
->kdattr
.group
= group
;
737 kd
->kdattr
.attr
= attr
;
739 kd
->kda_addr_ormask
= addr_ormask
;
740 QSLIST_INSERT_HEAD(&kvm_devices_head
, kd
, entries
);
741 memory_region_ref(kd
->mr
);
744 static int compare_u64(const void *a
, const void *b
)
746 if (*(uint64_t *)a
> *(uint64_t *)b
) {
749 if (*(uint64_t *)a
< *(uint64_t *)b
) {
756 * cpreg_values are sorted in ascending order by KVM register ID
757 * (see kvm_arm_init_cpreg_list). This allows us to cheaply find
758 * the storage for a KVM register by ID with a binary search.
760 static uint64_t *kvm_arm_get_cpreg_ptr(ARMCPU
*cpu
, uint64_t regidx
)
764 res
= bsearch(®idx
, cpu
->cpreg_indexes
, cpu
->cpreg_array_len
,
765 sizeof(uint64_t), compare_u64
);
768 return &cpu
->cpreg_values
[res
- cpu
->cpreg_indexes
];
772 * kvm_arm_reg_syncs_via_cpreg_list:
773 * @regidx: KVM register index
775 * Return true if this KVM register should be synchronized via the
776 * cpreg list of arbitrary system registers, false if it is synchronized
777 * by hand using code in kvm_arch_get/put_registers().
779 static bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx
)
781 switch (regidx
& KVM_REG_ARM_COPROC_MASK
) {
782 case KVM_REG_ARM_CORE
:
783 case KVM_REG_ARM64_SVE
:
791 * kvm_arm_init_cpreg_list:
794 * Initialize the ARMCPU cpreg list according to the kernel's
795 * definition of what CPU registers it knows about (and throw away
796 * the previous TCG-created cpreg list).
798 * Returns: 0 if success, else < 0 error code
800 static int kvm_arm_init_cpreg_list(ARMCPU
*cpu
)
802 struct kvm_reg_list rl
;
803 struct kvm_reg_list
*rlp
;
804 int i
, ret
, arraylen
;
805 CPUState
*cs
= CPU(cpu
);
808 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REG_LIST
, &rl
);
812 rlp
= g_malloc(sizeof(struct kvm_reg_list
) + rl
.n
* sizeof(uint64_t));
814 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REG_LIST
, rlp
);
818 /* Sort the list we get back from the kernel, since cpreg_tuples
819 * must be in strictly ascending order.
821 qsort(&rlp
->reg
, rlp
->n
, sizeof(rlp
->reg
[0]), compare_u64
);
823 for (i
= 0, arraylen
= 0; i
< rlp
->n
; i
++) {
824 if (!kvm_arm_reg_syncs_via_cpreg_list(rlp
->reg
[i
])) {
827 switch (rlp
->reg
[i
] & KVM_REG_SIZE_MASK
) {
828 case KVM_REG_SIZE_U32
:
829 case KVM_REG_SIZE_U64
:
832 fprintf(stderr
, "Can't handle size of register in kernel list\n");
840 cpu
->cpreg_indexes
= g_renew(uint64_t, cpu
->cpreg_indexes
, arraylen
);
841 cpu
->cpreg_values
= g_renew(uint64_t, cpu
->cpreg_values
, arraylen
);
842 cpu
->cpreg_vmstate_indexes
= g_renew(uint64_t, cpu
->cpreg_vmstate_indexes
,
844 cpu
->cpreg_vmstate_values
= g_renew(uint64_t, cpu
->cpreg_vmstate_values
,
846 cpu
->cpreg_array_len
= arraylen
;
847 cpu
->cpreg_vmstate_array_len
= arraylen
;
849 for (i
= 0, arraylen
= 0; i
< rlp
->n
; i
++) {
850 uint64_t regidx
= rlp
->reg
[i
];
851 if (!kvm_arm_reg_syncs_via_cpreg_list(regidx
)) {
854 cpu
->cpreg_indexes
[arraylen
] = regidx
;
857 assert(cpu
->cpreg_array_len
== arraylen
);
859 if (!write_kvmstate_to_list(cpu
)) {
860 /* Shouldn't happen unless kernel is inconsistent about
861 * what registers exist.
863 fprintf(stderr
, "Initial read of kernel register state failed\n");
874 * kvm_arm_cpreg_level:
875 * @regidx: KVM register index
877 * Return the level of this coprocessor/system register. Return value is
878 * either KVM_PUT_RUNTIME_STATE, KVM_PUT_RESET_STATE, or KVM_PUT_FULL_STATE.
880 static int kvm_arm_cpreg_level(uint64_t regidx
)
883 * All system registers are assumed to be level KVM_PUT_RUNTIME_STATE.
884 * If a register should be written less often, you must add it here
885 * with a state of either KVM_PUT_RESET_STATE or KVM_PUT_FULL_STATE.
888 case KVM_REG_ARM_TIMER_CNT
:
889 case KVM_REG_ARM_PTIMER_CNT
:
890 return KVM_PUT_FULL_STATE
;
892 return KVM_PUT_RUNTIME_STATE
;
895 bool write_kvmstate_to_list(ARMCPU
*cpu
)
897 CPUState
*cs
= CPU(cpu
);
901 for (i
= 0; i
< cpu
->cpreg_array_len
; i
++) {
902 uint64_t regidx
= cpu
->cpreg_indexes
[i
];
906 switch (regidx
& KVM_REG_SIZE_MASK
) {
907 case KVM_REG_SIZE_U32
:
908 ret
= kvm_get_one_reg(cs
, regidx
, &v32
);
910 cpu
->cpreg_values
[i
] = v32
;
913 case KVM_REG_SIZE_U64
:
914 ret
= kvm_get_one_reg(cs
, regidx
, cpu
->cpreg_values
+ i
);
917 g_assert_not_reached();
926 bool write_list_to_kvmstate(ARMCPU
*cpu
, int level
)
928 CPUState
*cs
= CPU(cpu
);
932 for (i
= 0; i
< cpu
->cpreg_array_len
; i
++) {
933 uint64_t regidx
= cpu
->cpreg_indexes
[i
];
937 if (kvm_arm_cpreg_level(regidx
) > level
) {
941 switch (regidx
& KVM_REG_SIZE_MASK
) {
942 case KVM_REG_SIZE_U32
:
943 v32
= cpu
->cpreg_values
[i
];
944 ret
= kvm_set_one_reg(cs
, regidx
, &v32
);
946 case KVM_REG_SIZE_U64
:
947 ret
= kvm_set_one_reg(cs
, regidx
, cpu
->cpreg_values
+ i
);
950 g_assert_not_reached();
953 /* We might fail for "unknown register" and also for
954 * "you tried to set a register which is constant with
955 * a different value from what it actually contains".
963 void kvm_arm_cpu_pre_save(ARMCPU
*cpu
)
965 /* KVM virtual time adjustment */
966 if (cpu
->kvm_vtime_dirty
) {
967 *kvm_arm_get_cpreg_ptr(cpu
, KVM_REG_ARM_TIMER_CNT
) = cpu
->kvm_vtime
;
971 void kvm_arm_cpu_post_load(ARMCPU
*cpu
)
973 /* KVM virtual time adjustment */
974 if (cpu
->kvm_adjvtime
) {
975 cpu
->kvm_vtime
= *kvm_arm_get_cpreg_ptr(cpu
, KVM_REG_ARM_TIMER_CNT
);
976 cpu
->kvm_vtime_dirty
= true;
980 void kvm_arm_reset_vcpu(ARMCPU
*cpu
)
984 /* Re-init VCPU so that all registers are set to
985 * their respective reset values.
987 ret
= kvm_arm_vcpu_init(cpu
);
989 fprintf(stderr
, "kvm_arm_vcpu_init failed: %s\n", strerror(-ret
));
992 if (!write_kvmstate_to_list(cpu
)) {
993 fprintf(stderr
, "write_kvmstate_to_list failed\n");
997 * Sync the reset values also into the CPUState. This is necessary
998 * because the next thing we do will be a kvm_arch_put_registers()
999 * which will update the list values from the CPUState before copying
1000 * the list values back to KVM. It's OK to ignore failure returns here
1001 * for the same reason we do so in kvm_arch_get_registers().
1003 write_list_to_cpustate(cpu
);
1007 * Update KVM's MP_STATE based on what QEMU thinks it is
1009 static int kvm_arm_sync_mpstate_to_kvm(ARMCPU
*cpu
)
1011 if (cap_has_mp_state
) {
1012 struct kvm_mp_state mp_state
= {
1013 .mp_state
= (cpu
->power_state
== PSCI_OFF
) ?
1014 KVM_MP_STATE_STOPPED
: KVM_MP_STATE_RUNNABLE
1016 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
1022 * Sync the KVM MP_STATE into QEMU
1024 static int kvm_arm_sync_mpstate_to_qemu(ARMCPU
*cpu
)
1026 if (cap_has_mp_state
) {
1027 struct kvm_mp_state mp_state
;
1028 int ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MP_STATE
, &mp_state
);
1032 cpu
->power_state
= (mp_state
.mp_state
== KVM_MP_STATE_STOPPED
) ?
1039 * kvm_arm_get_virtual_time:
1042 * Gets the VCPU's virtual counter and stores it in the KVM CPU state.
1044 static void kvm_arm_get_virtual_time(ARMCPU
*cpu
)
1048 if (cpu
->kvm_vtime_dirty
) {
1052 ret
= kvm_get_one_reg(CPU(cpu
), KVM_REG_ARM_TIMER_CNT
, &cpu
->kvm_vtime
);
1054 error_report("Failed to get KVM_REG_ARM_TIMER_CNT");
1058 cpu
->kvm_vtime_dirty
= true;
1062 * kvm_arm_put_virtual_time:
1065 * Sets the VCPU's virtual counter to the value stored in the KVM CPU state.
1067 static void kvm_arm_put_virtual_time(ARMCPU
*cpu
)
1071 if (!cpu
->kvm_vtime_dirty
) {
1075 ret
= kvm_set_one_reg(CPU(cpu
), KVM_REG_ARM_TIMER_CNT
, &cpu
->kvm_vtime
);
1077 error_report("Failed to set KVM_REG_ARM_TIMER_CNT");
1081 cpu
->kvm_vtime_dirty
= false;
1085 * kvm_put_vcpu_events:
1088 * Put VCPU related state to kvm.
1090 * Returns: 0 if success else < 0 error code
1092 static int kvm_put_vcpu_events(ARMCPU
*cpu
)
1094 CPUARMState
*env
= &cpu
->env
;
1095 struct kvm_vcpu_events events
;
1098 if (!kvm_has_vcpu_events()) {
1102 memset(&events
, 0, sizeof(events
));
1103 events
.exception
.serror_pending
= env
->serror
.pending
;
1105 /* Inject SError to guest with specified syndrome if host kernel
1106 * supports it, otherwise inject SError without syndrome.
1108 if (cap_has_inject_serror_esr
) {
1109 events
.exception
.serror_has_esr
= env
->serror
.has_esr
;
1110 events
.exception
.serror_esr
= env
->serror
.esr
;
1113 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_VCPU_EVENTS
, &events
);
1115 error_report("failed to put vcpu events");
1122 * kvm_get_vcpu_events:
1125 * Get VCPU related state from kvm.
1127 * Returns: 0 if success else < 0 error code
1129 static int kvm_get_vcpu_events(ARMCPU
*cpu
)
1131 CPUARMState
*env
= &cpu
->env
;
1132 struct kvm_vcpu_events events
;
1135 if (!kvm_has_vcpu_events()) {
1139 memset(&events
, 0, sizeof(events
));
1140 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_VCPU_EVENTS
, &events
);
1142 error_report("failed to get vcpu events");
1146 env
->serror
.pending
= events
.exception
.serror_pending
;
1147 env
->serror
.has_esr
= events
.exception
.serror_has_esr
;
1148 env
->serror
.esr
= events
.exception
.serror_esr
;
1153 #define ARM64_REG_ESR_EL1 ARM64_SYS_REG(3, 0, 5, 2, 0)
1154 #define ARM64_REG_TCR_EL1 ARM64_SYS_REG(3, 0, 2, 0, 2)
1159 * AARCH64: DFSC, bits [5:0]
1163 * FS[3:0] - DFSR[3:0]
1167 #define ESR_DFSC(aarch64, lpae, v) \
1168 ((aarch64 || (lpae)) ? ((v) & 0x3F) \
1169 : (((v) >> 6) | ((v) & 0x1F)))
1171 #define ESR_DFSC_EXTABT(aarch64, lpae) \
1172 ((aarch64) ? 0x10 : (lpae) ? 0x10 : 0x8)
1175 * kvm_arm_verify_ext_dabt_pending:
1178 * Verify the fault status code wrt the Ext DABT injection
1180 * Returns: true if the fault status code is as expected, false otherwise
1182 static bool kvm_arm_verify_ext_dabt_pending(ARMCPU
*cpu
)
1184 CPUState
*cs
= CPU(cpu
);
1187 if (!kvm_get_one_reg(cs
, ARM64_REG_ESR_EL1
, &dfsr_val
)) {
1188 CPUARMState
*env
= &cpu
->env
;
1189 int aarch64_mode
= arm_feature(env
, ARM_FEATURE_AARCH64
);
1192 if (!aarch64_mode
) {
1195 if (!kvm_get_one_reg(cs
, ARM64_REG_TCR_EL1
, &ttbcr
)) {
1196 lpae
= arm_feature(env
, ARM_FEATURE_LPAE
)
1197 && (ttbcr
& TTBCR_EAE
);
1201 * The verification here is based on the DFSC bits
1202 * of the ESR_EL1 reg only
1204 return (ESR_DFSC(aarch64_mode
, lpae
, dfsr_val
) ==
1205 ESR_DFSC_EXTABT(aarch64_mode
, lpae
));
1210 void kvm_arch_pre_run(CPUState
*cs
, struct kvm_run
*run
)
1212 ARMCPU
*cpu
= ARM_CPU(cs
);
1213 CPUARMState
*env
= &cpu
->env
;
1215 if (unlikely(env
->ext_dabt_raised
)) {
1217 * Verifying that the ext DABT has been properly injected,
1218 * otherwise risking indefinitely re-running the faulting instruction
1219 * Covering a very narrow case for kernels 5.5..5.5.4
1220 * when injected abort was misconfigured to be
1221 * an IMPLEMENTATION DEFINED exception (for 32-bit EL1)
1223 if (!arm_feature(env
, ARM_FEATURE_AARCH64
) &&
1224 unlikely(!kvm_arm_verify_ext_dabt_pending(cpu
))) {
1226 error_report("Data abort exception with no valid ISS generated by "
1227 "guest memory access. KVM unable to emulate faulting "
1228 "instruction. Failed to inject an external data abort "
1232 /* Clear the status */
1233 env
->ext_dabt_raised
= 0;
1237 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
1240 uint32_t switched_level
;
1242 if (kvm_irqchip_in_kernel()) {
1244 * We only need to sync timer states with user-space interrupt
1245 * controllers, so return early and save cycles if we don't.
1247 return MEMTXATTRS_UNSPECIFIED
;
1252 /* Synchronize our shadowed in-kernel device irq lines with the kvm ones */
1253 if (run
->s
.regs
.device_irq_level
!= cpu
->device_irq_level
) {
1254 switched_level
= cpu
->device_irq_level
^ run
->s
.regs
.device_irq_level
;
1258 if (switched_level
& KVM_ARM_DEV_EL1_VTIMER
) {
1259 qemu_set_irq(cpu
->gt_timer_outputs
[GTIMER_VIRT
],
1260 !!(run
->s
.regs
.device_irq_level
&
1261 KVM_ARM_DEV_EL1_VTIMER
));
1262 switched_level
&= ~KVM_ARM_DEV_EL1_VTIMER
;
1265 if (switched_level
& KVM_ARM_DEV_EL1_PTIMER
) {
1266 qemu_set_irq(cpu
->gt_timer_outputs
[GTIMER_PHYS
],
1267 !!(run
->s
.regs
.device_irq_level
&
1268 KVM_ARM_DEV_EL1_PTIMER
));
1269 switched_level
&= ~KVM_ARM_DEV_EL1_PTIMER
;
1272 if (switched_level
& KVM_ARM_DEV_PMU
) {
1273 qemu_set_irq(cpu
->pmu_interrupt
,
1274 !!(run
->s
.regs
.device_irq_level
& KVM_ARM_DEV_PMU
));
1275 switched_level
&= ~KVM_ARM_DEV_PMU
;
1278 if (switched_level
) {
1279 qemu_log_mask(LOG_UNIMP
, "%s: unhandled in-kernel device IRQ %x\n",
1280 __func__
, switched_level
);
1283 /* We also mark unknown levels as processed to not waste cycles */
1284 cpu
->device_irq_level
= run
->s
.regs
.device_irq_level
;
1288 return MEMTXATTRS_UNSPECIFIED
;
1291 static void kvm_arm_vm_state_change(void *opaque
, bool running
, RunState state
)
1293 ARMCPU
*cpu
= opaque
;
1296 if (cpu
->kvm_adjvtime
) {
1297 kvm_arm_put_virtual_time(cpu
);
1300 if (cpu
->kvm_adjvtime
) {
1301 kvm_arm_get_virtual_time(cpu
);
1307 * kvm_arm_handle_dabt_nisv:
1309 * @esr_iss: ISS encoding (limited) for the exception from Data Abort
1310 * ISV bit set to '0b0' -> no valid instruction syndrome
1311 * @fault_ipa: faulting address for the synchronous data abort
1313 * Returns: 0 if the exception has been handled, < 0 otherwise
1315 static int kvm_arm_handle_dabt_nisv(ARMCPU
*cpu
, uint64_t esr_iss
,
1318 CPUARMState
*env
= &cpu
->env
;
1320 * Request KVM to inject the external data abort into the guest
1322 if (cap_has_inject_ext_dabt
) {
1323 struct kvm_vcpu_events events
= { };
1325 * The external data abort event will be handled immediately by KVM
1326 * using the address fault that triggered the exit on given VCPU.
1327 * Requesting injection of the external data abort does not rely
1328 * on any other VCPU state. Therefore, in this particular case, the VCPU
1329 * synchronization can be exceptionally skipped.
1331 events
.exception
.ext_dabt_pending
= 1;
1332 /* KVM_CAP_ARM_INJECT_EXT_DABT implies KVM_CAP_VCPU_EVENTS */
1333 if (!kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_VCPU_EVENTS
, &events
)) {
1334 env
->ext_dabt_raised
= 1;
1338 error_report("Data abort exception triggered by guest memory access "
1339 "at physical address: 0x" TARGET_FMT_lx
,
1340 (target_ulong
)fault_ipa
);
1341 error_printf("KVM unable to emulate faulting instruction.\n");
1347 * kvm_arm_handle_debug:
1349 * @debug_exit: debug part of the KVM exit structure
1351 * Returns: TRUE if the debug exception was handled.
1353 * See v8 ARM ARM D7.2.27 ESR_ELx, Exception Syndrome Register
1355 * To minimise translating between kernel and user-space the kernel
1356 * ABI just provides user-space with the full exception syndrome
1357 * register value to be decoded in QEMU.
1359 static bool kvm_arm_handle_debug(ARMCPU
*cpu
,
1360 struct kvm_debug_exit_arch
*debug_exit
)
1362 int hsr_ec
= syn_get_ec(debug_exit
->hsr
);
1363 CPUState
*cs
= CPU(cpu
);
1364 CPUARMState
*env
= &cpu
->env
;
1366 /* Ensure PC is synchronised */
1367 kvm_cpu_synchronize_state(cs
);
1370 case EC_SOFTWARESTEP
:
1371 if (cs
->singlestep_enabled
) {
1375 * The kernel should have suppressed the guest's ability to
1376 * single step at this point so something has gone wrong.
1378 error_report("%s: guest single-step while debugging unsupported"
1379 " (%"PRIx64
", %"PRIx32
")",
1380 __func__
, env
->pc
, debug_exit
->hsr
);
1385 if (kvm_find_sw_breakpoint(cs
, env
->pc
)) {
1390 if (find_hw_breakpoint(cs
, env
->pc
)) {
1396 CPUWatchpoint
*wp
= find_hw_watchpoint(cs
, debug_exit
->far
);
1398 cs
->watchpoint_hit
= wp
;
1404 error_report("%s: unhandled debug exit (%"PRIx32
", %"PRIx64
")",
1405 __func__
, debug_exit
->hsr
, env
->pc
);
1408 /* If we are not handling the debug exception it must belong to
1409 * the guest. Let's re-use the existing TCG interrupt code to set
1410 * everything up properly.
1412 cs
->exception_index
= EXCP_BKPT
;
1413 env
->exception
.syndrome
= debug_exit
->hsr
;
1414 env
->exception
.vaddress
= debug_exit
->far
;
1415 env
->exception
.target_el
= 1;
1417 arm_cpu_do_interrupt(cs
);
1423 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
1425 ARMCPU
*cpu
= ARM_CPU(cs
);
1428 switch (run
->exit_reason
) {
1429 case KVM_EXIT_DEBUG
:
1430 if (kvm_arm_handle_debug(cpu
, &run
->debug
.arch
)) {
1432 } /* otherwise return to guest */
1434 case KVM_EXIT_ARM_NISV
:
1435 /* External DABT with no valid iss to decode */
1436 ret
= kvm_arm_handle_dabt_nisv(cpu
, run
->arm_nisv
.esr_iss
,
1437 run
->arm_nisv
.fault_ipa
);
1440 qemu_log_mask(LOG_UNIMP
, "%s: un-handled exit reason %d\n",
1441 __func__
, run
->exit_reason
);
1447 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
1452 int kvm_arch_process_async_events(CPUState
*cs
)
1458 * kvm_arm_hw_debug_active:
1461 * Return: TRUE if any hardware breakpoints in use.
1463 static bool kvm_arm_hw_debug_active(ARMCPU
*cpu
)
1465 return ((cur_hw_wps
> 0) || (cur_hw_bps
> 0));
1469 * kvm_arm_copy_hw_debug_data:
1470 * @ptr: kvm_guest_debug_arch structure
1472 * Copy the architecture specific debug registers into the
1473 * kvm_guest_debug ioctl structure.
1475 static void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch
*ptr
)
1478 memset(ptr
, 0, sizeof(struct kvm_guest_debug_arch
));
1480 for (i
= 0; i
< max_hw_wps
; i
++) {
1481 HWWatchpoint
*wp
= get_hw_wp(i
);
1482 ptr
->dbg_wcr
[i
] = wp
->wcr
;
1483 ptr
->dbg_wvr
[i
] = wp
->wvr
;
1485 for (i
= 0; i
< max_hw_bps
; i
++) {
1486 HWBreakpoint
*bp
= get_hw_bp(i
);
1487 ptr
->dbg_bcr
[i
] = bp
->bcr
;
1488 ptr
->dbg_bvr
[i
] = bp
->bvr
;
1492 void kvm_arch_update_guest_debug(CPUState
*cs
, struct kvm_guest_debug
*dbg
)
1494 if (kvm_sw_breakpoints_active(cs
)) {
1495 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
1497 if (kvm_arm_hw_debug_active(ARM_CPU(cs
))) {
1498 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW
;
1499 kvm_arm_copy_hw_debug_data(&dbg
->arch
);
1503 void kvm_arch_init_irq_routing(KVMState
*s
)
1507 int kvm_arch_irqchip_create(KVMState
*s
)
1509 if (kvm_kernel_irqchip_split()) {
1510 error_report("-machine kernel_irqchip=split is not supported on ARM.");
1514 /* If we can create the VGIC using the newer device control API, we
1515 * let the device do this when it initializes itself, otherwise we
1516 * fall back to the old API */
1517 return kvm_check_extension(s
, KVM_CAP_DEVICE_CTRL
);
1520 int kvm_arm_vgic_probe(void)
1524 if (kvm_create_device(kvm_state
,
1525 KVM_DEV_TYPE_ARM_VGIC_V3
, true) == 0) {
1526 val
|= KVM_ARM_VGIC_V3
;
1528 if (kvm_create_device(kvm_state
,
1529 KVM_DEV_TYPE_ARM_VGIC_V2
, true) == 0) {
1530 val
|= KVM_ARM_VGIC_V2
;
1535 int kvm_arm_set_irq(int cpu
, int irqtype
, int irq
, int level
)
1537 int kvm_irq
= (irqtype
<< KVM_ARM_IRQ_TYPE_SHIFT
) | irq
;
1538 int cpu_idx1
= cpu
% 256;
1539 int cpu_idx2
= cpu
/ 256;
1541 kvm_irq
|= (cpu_idx1
<< KVM_ARM_IRQ_VCPU_SHIFT
) |
1542 (cpu_idx2
<< KVM_ARM_IRQ_VCPU2_SHIFT
);
1544 return kvm_set_irq(kvm_state
, kvm_irq
, !!level
);
1547 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
1548 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
1550 AddressSpace
*as
= pci_device_iommu_address_space(dev
);
1551 hwaddr xlat
, len
, doorbell_gpa
;
1552 MemoryRegionSection mrs
;
1555 if (as
== &address_space_memory
) {
1559 /* MSI doorbell address is translated by an IOMMU */
1561 RCU_READ_LOCK_GUARD();
1563 mr
= address_space_translate(as
, address
, &xlat
, &len
, true,
1564 MEMTXATTRS_UNSPECIFIED
);
1570 mrs
= memory_region_find(mr
, xlat
, 1);
1576 doorbell_gpa
= mrs
.offset_within_address_space
;
1577 memory_region_unref(mrs
.mr
);
1579 route
->u
.msi
.address_lo
= doorbell_gpa
;
1580 route
->u
.msi
.address_hi
= doorbell_gpa
>> 32;
1582 trace_kvm_arm_fixup_msi_route(address
, doorbell_gpa
);
1587 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
1588 int vector
, PCIDevice
*dev
)
1593 int kvm_arch_release_virq_post(int virq
)
1598 int kvm_arch_msi_data_to_gsi(uint32_t data
)
1600 return (data
- 32) & 0xffff;
1603 static void kvm_arch_get_eager_split_size(Object
*obj
, Visitor
*v
,
1604 const char *name
, void *opaque
,
1607 KVMState
*s
= KVM_STATE(obj
);
1608 uint64_t value
= s
->kvm_eager_split_size
;
1610 visit_type_size(v
, name
, &value
, errp
);
1613 static void kvm_arch_set_eager_split_size(Object
*obj
, Visitor
*v
,
1614 const char *name
, void *opaque
,
1617 KVMState
*s
= KVM_STATE(obj
);
1621 error_setg(errp
, "Unable to set early-split-size after KVM has been initialized");
1625 if (!visit_type_size(v
, name
, &value
, errp
)) {
1629 if (value
&& !is_power_of_2(value
)) {
1630 error_setg(errp
, "early-split-size must be a power of two");
1634 s
->kvm_eager_split_size
= value
;
1637 void kvm_arch_accel_class_init(ObjectClass
*oc
)
1639 object_class_property_add(oc
, "eager-split-size", "size",
1640 kvm_arch_get_eager_split_size
,
1641 kvm_arch_set_eager_split_size
, NULL
, NULL
);
1643 object_class_property_set_description(oc
, "eager-split-size",
1644 "Eager Page Split chunk size for hugepages. (default: 0, disabled)");
1647 int kvm_arch_insert_hw_breakpoint(vaddr addr
, vaddr len
, int type
)
1650 case GDB_BREAKPOINT_HW
:
1651 return insert_hw_breakpoint(addr
);
1653 case GDB_WATCHPOINT_READ
:
1654 case GDB_WATCHPOINT_WRITE
:
1655 case GDB_WATCHPOINT_ACCESS
:
1656 return insert_hw_watchpoint(addr
, len
, type
);
1662 int kvm_arch_remove_hw_breakpoint(vaddr addr
, vaddr len
, int type
)
1665 case GDB_BREAKPOINT_HW
:
1666 return delete_hw_breakpoint(addr
);
1667 case GDB_WATCHPOINT_READ
:
1668 case GDB_WATCHPOINT_WRITE
:
1669 case GDB_WATCHPOINT_ACCESS
:
1670 return delete_hw_watchpoint(addr
, len
, type
);
1676 void kvm_arch_remove_all_hw_breakpoints(void)
1678 if (cur_hw_wps
> 0) {
1679 g_array_remove_range(hw_watchpoints
, 0, cur_hw_wps
);
1681 if (cur_hw_bps
> 0) {
1682 g_array_remove_range(hw_breakpoints
, 0, cur_hw_bps
);
1686 static bool kvm_arm_set_device_attr(ARMCPU
*cpu
, struct kvm_device_attr
*attr
,
1691 err
= kvm_vcpu_ioctl(CPU(cpu
), KVM_HAS_DEVICE_ATTR
, attr
);
1693 error_report("%s: KVM_HAS_DEVICE_ATTR: %s", name
, strerror(-err
));
1697 err
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_DEVICE_ATTR
, attr
);
1699 error_report("%s: KVM_SET_DEVICE_ATTR: %s", name
, strerror(-err
));
1706 void kvm_arm_pmu_init(ARMCPU
*cpu
)
1708 struct kvm_device_attr attr
= {
1709 .group
= KVM_ARM_VCPU_PMU_V3_CTRL
,
1710 .attr
= KVM_ARM_VCPU_PMU_V3_INIT
,
1713 if (!cpu
->has_pmu
) {
1716 if (!kvm_arm_set_device_attr(cpu
, &attr
, "PMU")) {
1717 error_report("failed to init PMU");
1722 void kvm_arm_pmu_set_irq(ARMCPU
*cpu
, int irq
)
1724 struct kvm_device_attr attr
= {
1725 .group
= KVM_ARM_VCPU_PMU_V3_CTRL
,
1726 .addr
= (intptr_t)&irq
,
1727 .attr
= KVM_ARM_VCPU_PMU_V3_IRQ
,
1730 if (!cpu
->has_pmu
) {
1733 if (!kvm_arm_set_device_attr(cpu
, &attr
, "PMU")) {
1734 error_report("failed to set irq for PMU");
1739 void kvm_arm_pvtime_init(ARMCPU
*cpu
, uint64_t ipa
)
1741 struct kvm_device_attr attr
= {
1742 .group
= KVM_ARM_VCPU_PVTIME_CTRL
,
1743 .attr
= KVM_ARM_VCPU_PVTIME_IPA
,
1744 .addr
= (uint64_t)&ipa
,
1747 if (cpu
->kvm_steal_time
== ON_OFF_AUTO_OFF
) {
1750 if (!kvm_arm_set_device_attr(cpu
, &attr
, "PVTIME IPA")) {
1751 error_report("failed to init PVTIME IPA");
1756 void kvm_arm_steal_time_finalize(ARMCPU
*cpu
, Error
**errp
)
1758 bool has_steal_time
= kvm_check_extension(kvm_state
, KVM_CAP_STEAL_TIME
);
1760 if (cpu
->kvm_steal_time
== ON_OFF_AUTO_AUTO
) {
1761 if (!has_steal_time
|| !arm_feature(&cpu
->env
, ARM_FEATURE_AARCH64
)) {
1762 cpu
->kvm_steal_time
= ON_OFF_AUTO_OFF
;
1764 cpu
->kvm_steal_time
= ON_OFF_AUTO_ON
;
1766 } else if (cpu
->kvm_steal_time
== ON_OFF_AUTO_ON
) {
1767 if (!has_steal_time
) {
1768 error_setg(errp
, "'kvm-steal-time' cannot be enabled "
1771 } else if (!arm_feature(&cpu
->env
, ARM_FEATURE_AARCH64
)) {
1773 * DEN0057A chapter 2 says "This specification only covers
1774 * systems in which the Execution state of the hypervisor
1775 * as well as EL1 of virtual machines is AArch64.". And,
1776 * to ensure that, the smc/hvc calls are only specified as
1779 error_setg(errp
, "'kvm-steal-time' cannot be enabled "
1780 "for AArch32 guests");
1786 bool kvm_arm_aarch32_supported(void)
1788 return kvm_check_extension(kvm_state
, KVM_CAP_ARM_EL1_32BIT
);
1791 bool kvm_arm_sve_supported(void)
1793 return kvm_check_extension(kvm_state
, KVM_CAP_ARM_SVE
);
1796 QEMU_BUILD_BUG_ON(KVM_ARM64_SVE_VQ_MIN
!= 1);
1798 uint32_t kvm_arm_sve_get_vls(ARMCPU
*cpu
)
1800 /* Only call this function if kvm_arm_sve_supported() returns true. */
1801 static uint64_t vls
[KVM_ARM64_SVE_VLS_WORDS
];
1807 * KVM ensures all host CPUs support the same set of vector lengths.
1808 * So we only need to create the scratch VCPUs once and then cache
1812 struct kvm_vcpu_init init
= {
1814 .features
[0] = (1 << KVM_ARM_VCPU_SVE
),
1816 struct kvm_one_reg reg
= {
1817 .id
= KVM_REG_ARM64_SVE_VLS
,
1818 .addr
= (uint64_t)&vls
[0],
1820 int fdarray
[3], ret
;
1824 if (!kvm_arm_create_scratch_host_vcpu(NULL
, fdarray
, &init
)) {
1825 error_report("failed to create scratch VCPU with SVE enabled");
1828 ret
= ioctl(fdarray
[2], KVM_GET_ONE_REG
, ®
);
1829 kvm_arm_destroy_scratch_host_vcpu(fdarray
);
1831 error_report("failed to get KVM_REG_ARM64_SVE_VLS: %s",
1836 for (i
= KVM_ARM64_SVE_VLS_WORDS
- 1; i
>= 0; --i
) {
1838 vq
= 64 - clz64(vls
[i
]) + i
* 64;
1842 if (vq
> ARM_MAX_VQ
) {
1843 warn_report("KVM supports vector lengths larger than "
1845 vls
[0] &= MAKE_64BIT_MASK(0, ARM_MAX_VQ
);
1852 static int kvm_arm_sve_set_vls(ARMCPU
*cpu
)
1854 uint64_t vls
[KVM_ARM64_SVE_VLS_WORDS
] = { cpu
->sve_vq
.map
};
1856 assert(cpu
->sve_max_vq
<= KVM_ARM64_SVE_VQ_MAX
);
1858 return kvm_set_one_reg(CPU(cpu
), KVM_REG_ARM64_SVE_VLS
, &vls
[0]);
1861 #define ARM_CPU_ID_MPIDR 3, 0, 0, 0, 5
1863 int kvm_arch_init_vcpu(CPUState
*cs
)
1867 ARMCPU
*cpu
= ARM_CPU(cs
);
1868 CPUARMState
*env
= &cpu
->env
;
1871 if (cpu
->kvm_target
== QEMU_KVM_ARM_TARGET_NONE
||
1872 !object_dynamic_cast(OBJECT(cpu
), TYPE_AARCH64_CPU
)) {
1873 error_report("KVM is not supported for this guest CPU type");
1877 qemu_add_vm_change_state_handler(kvm_arm_vm_state_change
, cpu
);
1879 /* Determine init features for this CPU */
1880 memset(cpu
->kvm_init_features
, 0, sizeof(cpu
->kvm_init_features
));
1881 if (cs
->start_powered_off
) {
1882 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_POWER_OFF
;
1884 if (kvm_check_extension(cs
->kvm_state
, KVM_CAP_ARM_PSCI_0_2
)) {
1885 cpu
->psci_version
= QEMU_PSCI_VERSION_0_2
;
1886 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2
;
1888 if (!arm_feature(env
, ARM_FEATURE_AARCH64
)) {
1889 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT
;
1892 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_PMU_V3
;
1894 if (cpu_isar_feature(aa64_sve
, cpu
)) {
1895 assert(kvm_arm_sve_supported());
1896 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_SVE
;
1898 if (cpu_isar_feature(aa64_pauth
, cpu
)) {
1899 cpu
->kvm_init_features
[0] |= (1 << KVM_ARM_VCPU_PTRAUTH_ADDRESS
|
1900 1 << KVM_ARM_VCPU_PTRAUTH_GENERIC
);
1903 /* Do KVM_ARM_VCPU_INIT ioctl */
1904 ret
= kvm_arm_vcpu_init(cpu
);
1909 if (cpu_isar_feature(aa64_sve
, cpu
)) {
1910 ret
= kvm_arm_sve_set_vls(cpu
);
1914 ret
= kvm_arm_vcpu_finalize(cpu
, KVM_ARM_VCPU_SVE
);
1921 * KVM reports the exact PSCI version it is implementing via a
1922 * special sysreg. If it is present, use its contents to determine
1923 * what to report to the guest in the dtb (it is the PSCI version,
1924 * in the same 15-bits major 16-bits minor format that PSCI_VERSION
1927 if (!kvm_get_one_reg(cs
, KVM_REG_ARM_PSCI_VERSION
, &psciver
)) {
1928 cpu
->psci_version
= psciver
;
1932 * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
1933 * Currently KVM has its own idea about MPIDR assignment, so we
1934 * override our defaults with what we get from KVM.
1936 ret
= kvm_get_one_reg(cs
, ARM64_SYS_REG(ARM_CPU_ID_MPIDR
), &mpidr
);
1940 cpu
->mp_affinity
= mpidr
& ARM64_AFFINITY_MASK
;
1942 return kvm_arm_init_cpreg_list(cpu
);
1945 int kvm_arch_destroy_vcpu(CPUState
*cs
)
1950 /* Callers must hold the iothread mutex lock */
1951 static void kvm_inject_arm_sea(CPUState
*c
)
1953 ARMCPU
*cpu
= ARM_CPU(c
);
1954 CPUARMState
*env
= &cpu
->env
;
1958 c
->exception_index
= EXCP_DATA_ABORT
;
1959 env
->exception
.target_el
= 1;
1962 * Set the DFSC to synchronous external abort and set FnV to not valid,
1963 * this will tell guest the FAR_ELx is UNKNOWN for this abort.
1965 same_el
= arm_current_el(env
) == env
->exception
.target_el
;
1966 esr
= syn_data_abort_no_iss(same_el
, 1, 0, 0, 0, 0, 0x10);
1968 env
->exception
.syndrome
= esr
;
1970 arm_cpu_do_interrupt(c
);
1973 #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
1974 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
1976 #define AARCH64_SIMD_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U128 | \
1977 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
1979 #define AARCH64_SIMD_CTRL_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U32 | \
1980 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
1982 static int kvm_arch_put_fpsimd(CPUState
*cs
)
1984 CPUARMState
*env
= &ARM_CPU(cs
)->env
;
1987 for (i
= 0; i
< 32; i
++) {
1988 uint64_t *q
= aa64_vfp_qreg(env
, i
);
1990 uint64_t fp_val
[2] = { q
[1], q
[0] };
1991 ret
= kvm_set_one_reg(cs
, AARCH64_SIMD_CORE_REG(fp_regs
.vregs
[i
]),
1994 ret
= kvm_set_one_reg(cs
, AARCH64_SIMD_CORE_REG(fp_regs
.vregs
[i
]), q
);
2005 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits
2006 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard
2007 * code the slice index to zero for now as it's unlikely we'll need more than
2008 * one slice for quite some time.
2010 static int kvm_arch_put_sve(CPUState
*cs
)
2012 ARMCPU
*cpu
= ARM_CPU(cs
);
2013 CPUARMState
*env
= &cpu
->env
;
2014 uint64_t tmp
[ARM_MAX_VQ
* 2];
2018 for (n
= 0; n
< KVM_ARM64_SVE_NUM_ZREGS
; ++n
) {
2019 r
= sve_bswap64(tmp
, &env
->vfp
.zregs
[n
].d
[0], cpu
->sve_max_vq
* 2);
2020 ret
= kvm_set_one_reg(cs
, KVM_REG_ARM64_SVE_ZREG(n
, 0), r
);
2026 for (n
= 0; n
< KVM_ARM64_SVE_NUM_PREGS
; ++n
) {
2027 r
= sve_bswap64(tmp
, r
= &env
->vfp
.pregs
[n
].p
[0],
2028 DIV_ROUND_UP(cpu
->sve_max_vq
* 2, 8));
2029 ret
= kvm_set_one_reg(cs
, KVM_REG_ARM64_SVE_PREG(n
, 0), r
);
2035 r
= sve_bswap64(tmp
, &env
->vfp
.pregs
[FFR_PRED_NUM
].p
[0],
2036 DIV_ROUND_UP(cpu
->sve_max_vq
* 2, 8));
2037 ret
= kvm_set_one_reg(cs
, KVM_REG_ARM64_SVE_FFR(0), r
);
2045 int kvm_arch_put_registers(CPUState
*cs
, int level
, Error
**errp
)
2052 ARMCPU
*cpu
= ARM_CPU(cs
);
2053 CPUARMState
*env
= &cpu
->env
;
2055 /* If we are in AArch32 mode then we need to copy the AArch32 regs to the
2056 * AArch64 registers before pushing them out to 64-bit KVM.
2059 aarch64_sync_32_to_64(env
);
2062 for (i
= 0; i
< 31; i
++) {
2063 ret
= kvm_set_one_reg(cs
, AARCH64_CORE_REG(regs
.regs
[i
]),
2070 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
2071 * QEMU side we keep the current SP in xregs[31] as well.
2073 aarch64_save_sp(env
, 1);
2075 ret
= kvm_set_one_reg(cs
, AARCH64_CORE_REG(regs
.sp
), &env
->sp_el
[0]);
2080 ret
= kvm_set_one_reg(cs
, AARCH64_CORE_REG(sp_el1
), &env
->sp_el
[1]);
2085 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
2087 val
= pstate_read(env
);
2089 val
= cpsr_read(env
);
2091 ret
= kvm_set_one_reg(cs
, AARCH64_CORE_REG(regs
.pstate
), &val
);
2096 ret
= kvm_set_one_reg(cs
, AARCH64_CORE_REG(regs
.pc
), &env
->pc
);
2101 ret
= kvm_set_one_reg(cs
, AARCH64_CORE_REG(elr_el1
), &env
->elr_el
[1]);
2106 /* Saved Program State Registers
2108 * Before we restore from the banked_spsr[] array we need to
2109 * ensure that any modifications to env->spsr are correctly
2110 * reflected in the banks.
2112 el
= arm_current_el(env
);
2113 if (el
> 0 && !is_a64(env
)) {
2114 i
= bank_number(env
->uncached_cpsr
& CPSR_M
);
2115 env
->banked_spsr
[i
] = env
->spsr
;
2118 /* KVM 0-4 map to QEMU banks 1-5 */
2119 for (i
= 0; i
< KVM_NR_SPSR
; i
++) {
2120 ret
= kvm_set_one_reg(cs
, AARCH64_CORE_REG(spsr
[i
]),
2121 &env
->banked_spsr
[i
+ 1]);
2127 if (cpu_isar_feature(aa64_sve
, cpu
)) {
2128 ret
= kvm_arch_put_sve(cs
);
2130 ret
= kvm_arch_put_fpsimd(cs
);
2136 fpr
= vfp_get_fpsr(env
);
2137 ret
= kvm_set_one_reg(cs
, AARCH64_SIMD_CTRL_REG(fp_regs
.fpsr
), &fpr
);
2142 fpr
= vfp_get_fpcr(env
);
2143 ret
= kvm_set_one_reg(cs
, AARCH64_SIMD_CTRL_REG(fp_regs
.fpcr
), &fpr
);
2148 write_cpustate_to_list(cpu
, true);
2150 if (!write_list_to_kvmstate(cpu
, level
)) {
2155 * Setting VCPU events should be triggered after syncing the registers
2156 * to avoid overwriting potential changes made by KVM upon calling
2157 * KVM_SET_VCPU_EVENTS ioctl
2159 ret
= kvm_put_vcpu_events(cpu
);
2164 return kvm_arm_sync_mpstate_to_kvm(cpu
);
2167 static int kvm_arch_get_fpsimd(CPUState
*cs
)
2169 CPUARMState
*env
= &ARM_CPU(cs
)->env
;
2172 for (i
= 0; i
< 32; i
++) {
2173 uint64_t *q
= aa64_vfp_qreg(env
, i
);
2174 ret
= kvm_get_one_reg(cs
, AARCH64_SIMD_CORE_REG(fp_regs
.vregs
[i
]), q
);
2180 t
= q
[0], q
[0] = q
[1], q
[1] = t
;
2189 * KVM SVE registers come in slices where ZREGs have a slice size of 2048 bits
2190 * and PREGS and the FFR have a slice size of 256 bits. However we simply hard
2191 * code the slice index to zero for now as it's unlikely we'll need more than
2192 * one slice for quite some time.
2194 static int kvm_arch_get_sve(CPUState
*cs
)
2196 ARMCPU
*cpu
= ARM_CPU(cs
);
2197 CPUARMState
*env
= &cpu
->env
;
2201 for (n
= 0; n
< KVM_ARM64_SVE_NUM_ZREGS
; ++n
) {
2202 r
= &env
->vfp
.zregs
[n
].d
[0];
2203 ret
= kvm_get_one_reg(cs
, KVM_REG_ARM64_SVE_ZREG(n
, 0), r
);
2207 sve_bswap64(r
, r
, cpu
->sve_max_vq
* 2);
2210 for (n
= 0; n
< KVM_ARM64_SVE_NUM_PREGS
; ++n
) {
2211 r
= &env
->vfp
.pregs
[n
].p
[0];
2212 ret
= kvm_get_one_reg(cs
, KVM_REG_ARM64_SVE_PREG(n
, 0), r
);
2216 sve_bswap64(r
, r
, DIV_ROUND_UP(cpu
->sve_max_vq
* 2, 8));
2219 r
= &env
->vfp
.pregs
[FFR_PRED_NUM
].p
[0];
2220 ret
= kvm_get_one_reg(cs
, KVM_REG_ARM64_SVE_FFR(0), r
);
2224 sve_bswap64(r
, r
, DIV_ROUND_UP(cpu
->sve_max_vq
* 2, 8));
2229 int kvm_arch_get_registers(CPUState
*cs
, Error
**errp
)
2236 ARMCPU
*cpu
= ARM_CPU(cs
);
2237 CPUARMState
*env
= &cpu
->env
;
2239 for (i
= 0; i
< 31; i
++) {
2240 ret
= kvm_get_one_reg(cs
, AARCH64_CORE_REG(regs
.regs
[i
]),
2247 ret
= kvm_get_one_reg(cs
, AARCH64_CORE_REG(regs
.sp
), &env
->sp_el
[0]);
2252 ret
= kvm_get_one_reg(cs
, AARCH64_CORE_REG(sp_el1
), &env
->sp_el
[1]);
2257 ret
= kvm_get_one_reg(cs
, AARCH64_CORE_REG(regs
.pstate
), &val
);
2262 env
->aarch64
= ((val
& PSTATE_nRW
) == 0);
2264 pstate_write(env
, val
);
2266 cpsr_write(env
, val
, 0xffffffff, CPSRWriteRaw
);
2269 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
2270 * QEMU side we keep the current SP in xregs[31] as well.
2272 aarch64_restore_sp(env
, 1);
2274 ret
= kvm_get_one_reg(cs
, AARCH64_CORE_REG(regs
.pc
), &env
->pc
);
2279 /* If we are in AArch32 mode then we need to sync the AArch32 regs with the
2280 * incoming AArch64 regs received from 64-bit KVM.
2281 * We must perform this after all of the registers have been acquired from
2285 aarch64_sync_64_to_32(env
);
2288 ret
= kvm_get_one_reg(cs
, AARCH64_CORE_REG(elr_el1
), &env
->elr_el
[1]);
2293 /* Fetch the SPSR registers
2295 * KVM SPSRs 0-4 map to QEMU banks 1-5
2297 for (i
= 0; i
< KVM_NR_SPSR
; i
++) {
2298 ret
= kvm_get_one_reg(cs
, AARCH64_CORE_REG(spsr
[i
]),
2299 &env
->banked_spsr
[i
+ 1]);
2305 el
= arm_current_el(env
);
2306 if (el
> 0 && !is_a64(env
)) {
2307 i
= bank_number(env
->uncached_cpsr
& CPSR_M
);
2308 env
->spsr
= env
->banked_spsr
[i
];
2311 if (cpu_isar_feature(aa64_sve
, cpu
)) {
2312 ret
= kvm_arch_get_sve(cs
);
2314 ret
= kvm_arch_get_fpsimd(cs
);
2320 ret
= kvm_get_one_reg(cs
, AARCH64_SIMD_CTRL_REG(fp_regs
.fpsr
), &fpr
);
2324 vfp_set_fpsr(env
, fpr
);
2326 ret
= kvm_get_one_reg(cs
, AARCH64_SIMD_CTRL_REG(fp_regs
.fpcr
), &fpr
);
2330 vfp_set_fpcr(env
, fpr
);
2332 ret
= kvm_get_vcpu_events(cpu
);
2337 if (!write_kvmstate_to_list(cpu
)) {
2340 /* Note that it's OK to have registers which aren't in CPUState,
2341 * so we can ignore a failure return here.
2343 write_list_to_cpustate(cpu
);
2345 ret
= kvm_arm_sync_mpstate_to_qemu(cpu
);
2347 /* TODO: other registers */
2351 void kvm_arch_on_sigbus_vcpu(CPUState
*c
, int code
, void *addr
)
2353 ram_addr_t ram_addr
;
2356 assert(code
== BUS_MCEERR_AR
|| code
== BUS_MCEERR_AO
);
2358 if (acpi_ghes_present() && addr
) {
2359 ram_addr
= qemu_ram_addr_from_host(addr
);
2360 if (ram_addr
!= RAM_ADDR_INVALID
&&
2361 kvm_physical_memory_addr_from_host(c
->kvm_state
, addr
, &paddr
)) {
2362 kvm_hwpoison_page_add(ram_addr
);
2364 * If this is a BUS_MCEERR_AR, we know we have been called
2365 * synchronously from the vCPU thread, so we can easily
2366 * synchronize the state and inject an error.
2368 * TODO: we currently don't tell the guest at all about
2369 * BUS_MCEERR_AO. In that case we might either be being
2370 * called synchronously from the vCPU thread, or a bit
2371 * later from the main thread, so doing the injection of
2372 * the error would be more complicated.
2374 if (code
== BUS_MCEERR_AR
) {
2375 kvm_cpu_synchronize_state(c
);
2376 if (!acpi_ghes_record_errors(ACPI_HEST_SRC_ID_SEA
, paddr
)) {
2377 kvm_inject_arm_sea(c
);
2379 error_report("failed to record the error");
2385 if (code
== BUS_MCEERR_AO
) {
2386 error_report("Hardware memory error at addr %p for memory used by "
2387 "QEMU itself instead of guest system!", addr
);
2391 if (code
== BUS_MCEERR_AR
) {
2392 error_report("Hardware memory error!");
2397 /* C6.6.29 BRK instruction */
2398 static const uint32_t brk_insn
= 0xd4200000;
2400 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
2402 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 4, 0) ||
2403 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&brk_insn
, 4, 1)) {
2409 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
2411 static uint32_t brk
;
2413 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&brk
, 4, 0) ||
2415 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 4, 1)) {