2 * ARM implementation of KVM hooks, 64 bit specific code
4 * Copyright Mian-M. Hamayun 2013, Virtual Open Systems
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
16 #include <linux/kvm.h>
18 #include "config-host.h"
19 #include "qemu-common.h"
20 #include "qemu/timer.h"
21 #include "sysemu/sysemu.h"
22 #include "sysemu/kvm.h"
25 #include "internals.h"
26 #include "hw/arm/arm.h"
28 static inline void set_feature(uint64_t *features
, int feature
)
30 *features
|= 1ULL << feature
;
33 bool kvm_arm_get_host_cpu_features(ARMHostCPUClass
*ahcc
)
35 /* Identify the feature bits corresponding to the host CPU, and
36 * fill out the ARMHostCPUClass fields accordingly. To do this
37 * we have to create a scratch VM, create a single CPU inside it,
38 * and then query that CPU for the relevant ID registers.
39 * For AArch64 we currently don't care about ID registers at
40 * all; we just want to know the CPU type.
43 uint64_t features
= 0;
44 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
45 * we know these will only support creating one kind of guest CPU,
46 * which is its preferred CPU type. Fortunately these old kernels
47 * support only a very limited number of CPUs.
49 static const uint32_t cpus_to_try
[] = {
50 KVM_ARM_TARGET_AEM_V8
,
51 KVM_ARM_TARGET_FOUNDATION_V8
,
52 KVM_ARM_TARGET_CORTEX_A57
,
53 QEMU_KVM_ARM_TARGET_NONE
55 struct kvm_vcpu_init init
;
57 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try
, fdarray
, &init
)) {
61 ahcc
->target
= init
.target
;
62 ahcc
->dtb_compatible
= "arm,arm-v8";
64 kvm_arm_destroy_scratch_host_vcpu(fdarray
);
66 /* We can assume any KVM supporting CPU is at least a v8
67 * with VFPv4+Neon; this in turn implies most of the other
70 set_feature(&features
, ARM_FEATURE_V8
);
71 set_feature(&features
, ARM_FEATURE_VFP4
);
72 set_feature(&features
, ARM_FEATURE_NEON
);
73 set_feature(&features
, ARM_FEATURE_AARCH64
);
75 ahcc
->features
= features
;
80 int kvm_arch_init_vcpu(CPUState
*cs
)
83 ARMCPU
*cpu
= ARM_CPU(cs
);
85 if (cpu
->kvm_target
== QEMU_KVM_ARM_TARGET_NONE
||
86 !object_dynamic_cast(OBJECT(cpu
), TYPE_AARCH64_CPU
)) {
87 fprintf(stderr
, "KVM is not supported for this guest CPU type\n");
91 /* Determine init features for this CPU */
92 memset(cpu
->kvm_init_features
, 0, sizeof(cpu
->kvm_init_features
));
93 if (cpu
->start_powered_off
) {
94 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_POWER_OFF
;
96 if (kvm_check_extension(cs
->kvm_state
, KVM_CAP_ARM_PSCI_0_2
)) {
97 cpu
->psci_version
= 2;
98 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2
;
100 if (!arm_feature(&cpu
->env
, ARM_FEATURE_AARCH64
)) {
101 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT
;
104 /* Do KVM_ARM_VCPU_INIT ioctl */
105 ret
= kvm_arm_vcpu_init(cs
);
110 return kvm_arm_init_cpreg_list(cpu
);
113 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx
)
115 /* Return true if the regidx is a register we should synchronize
116 * via the cpreg_tuples array (ie is not a core reg we sync by
117 * hand in kvm_arch_get/put_registers())
119 switch (regidx
& KVM_REG_ARM_COPROC_MASK
) {
120 case KVM_REG_ARM_CORE
:
127 #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
128 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
130 #define AARCH64_SIMD_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U128 | \
131 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
133 #define AARCH64_SIMD_CTRL_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U32 | \
134 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
136 int kvm_arch_put_registers(CPUState
*cs
, int level
)
138 struct kvm_one_reg reg
;
145 ARMCPU
*cpu
= ARM_CPU(cs
);
146 CPUARMState
*env
= &cpu
->env
;
148 /* If we are in AArch32 mode then we need to copy the AArch32 regs to the
149 * AArch64 registers before pushing them out to 64-bit KVM.
152 aarch64_sync_32_to_64(env
);
155 for (i
= 0; i
< 31; i
++) {
156 reg
.id
= AARCH64_CORE_REG(regs
.regs
[i
]);
157 reg
.addr
= (uintptr_t) &env
->xregs
[i
];
158 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
164 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
165 * QEMU side we keep the current SP in xregs[31] as well.
167 aarch64_save_sp(env
, 1);
169 reg
.id
= AARCH64_CORE_REG(regs
.sp
);
170 reg
.addr
= (uintptr_t) &env
->sp_el
[0];
171 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
176 reg
.id
= AARCH64_CORE_REG(sp_el1
);
177 reg
.addr
= (uintptr_t) &env
->sp_el
[1];
178 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
183 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
185 val
= pstate_read(env
);
187 val
= cpsr_read(env
);
189 reg
.id
= AARCH64_CORE_REG(regs
.pstate
);
190 reg
.addr
= (uintptr_t) &val
;
191 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
196 reg
.id
= AARCH64_CORE_REG(regs
.pc
);
197 reg
.addr
= (uintptr_t) &env
->pc
;
198 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
203 reg
.id
= AARCH64_CORE_REG(elr_el1
);
204 reg
.addr
= (uintptr_t) &env
->elr_el
[1];
205 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
210 /* Saved Program State Registers
212 * Before we restore from the banked_spsr[] array we need to
213 * ensure that any modifications to env->spsr are correctly
214 * reflected in the banks.
216 el
= arm_current_el(env
);
217 if (el
> 0 && !is_a64(env
)) {
218 i
= bank_number(env
->uncached_cpsr
& CPSR_M
);
219 env
->banked_spsr
[i
] = env
->spsr
;
222 /* KVM 0-4 map to QEMU banks 1-5 */
223 for (i
= 0; i
< KVM_NR_SPSR
; i
++) {
224 reg
.id
= AARCH64_CORE_REG(spsr
[i
]);
225 reg
.addr
= (uintptr_t) &env
->banked_spsr
[i
+ 1];
226 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
232 /* Advanced SIMD and FP registers
233 * We map Qn = regs[2n+1]:regs[2n]
235 for (i
= 0; i
< 32; i
++) {
238 #ifdef HOST_WORDS_BIGENDIAN
239 fp_val
[0] = env
->vfp
.regs
[rd
+ 1];
240 fp_val
[1] = env
->vfp
.regs
[rd
];
242 fp_val
[1] = env
->vfp
.regs
[rd
+ 1];
243 fp_val
[0] = env
->vfp
.regs
[rd
];
245 reg
.id
= AARCH64_SIMD_CORE_REG(fp_regs
.vregs
[i
]);
246 reg
.addr
= (uintptr_t)(&fp_val
);
247 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
253 reg
.addr
= (uintptr_t)(&fpr
);
254 fpr
= vfp_get_fpsr(env
);
255 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpsr
);
256 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
261 fpr
= vfp_get_fpcr(env
);
262 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpcr
);
263 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
268 if (!write_list_to_kvmstate(cpu
)) {
272 kvm_arm_sync_mpstate_to_kvm(cpu
);
277 int kvm_arch_get_registers(CPUState
*cs
)
279 struct kvm_one_reg reg
;
286 ARMCPU
*cpu
= ARM_CPU(cs
);
287 CPUARMState
*env
= &cpu
->env
;
289 for (i
= 0; i
< 31; i
++) {
290 reg
.id
= AARCH64_CORE_REG(regs
.regs
[i
]);
291 reg
.addr
= (uintptr_t) &env
->xregs
[i
];
292 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
298 reg
.id
= AARCH64_CORE_REG(regs
.sp
);
299 reg
.addr
= (uintptr_t) &env
->sp_el
[0];
300 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
305 reg
.id
= AARCH64_CORE_REG(sp_el1
);
306 reg
.addr
= (uintptr_t) &env
->sp_el
[1];
307 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
312 reg
.id
= AARCH64_CORE_REG(regs
.pstate
);
313 reg
.addr
= (uintptr_t) &val
;
314 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
319 env
->aarch64
= ((val
& PSTATE_nRW
) == 0);
321 pstate_write(env
, val
);
323 env
->uncached_cpsr
= val
& CPSR_M
;
324 cpsr_write(env
, val
, 0xffffffff);
327 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
328 * QEMU side we keep the current SP in xregs[31] as well.
330 aarch64_restore_sp(env
, 1);
332 reg
.id
= AARCH64_CORE_REG(regs
.pc
);
333 reg
.addr
= (uintptr_t) &env
->pc
;
334 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
339 /* If we are in AArch32 mode then we need to sync the AArch32 regs with the
340 * incoming AArch64 regs received from 64-bit KVM.
341 * We must perform this after all of the registers have been acquired from
345 aarch64_sync_64_to_32(env
);
348 reg
.id
= AARCH64_CORE_REG(elr_el1
);
349 reg
.addr
= (uintptr_t) &env
->elr_el
[1];
350 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
355 /* Fetch the SPSR registers
357 * KVM SPSRs 0-4 map to QEMU banks 1-5
359 for (i
= 0; i
< KVM_NR_SPSR
; i
++) {
360 reg
.id
= AARCH64_CORE_REG(spsr
[i
]);
361 reg
.addr
= (uintptr_t) &env
->banked_spsr
[i
+ 1];
362 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
368 el
= arm_current_el(env
);
369 if (el
> 0 && !is_a64(env
)) {
370 i
= bank_number(env
->uncached_cpsr
& CPSR_M
);
371 env
->spsr
= env
->banked_spsr
[i
];
374 /* Advanced SIMD and FP registers
375 * We map Qn = regs[2n+1]:regs[2n]
377 for (i
= 0; i
< 32; i
++) {
379 reg
.id
= AARCH64_SIMD_CORE_REG(fp_regs
.vregs
[i
]);
380 reg
.addr
= (uintptr_t)(&fp_val
);
381 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
386 #ifdef HOST_WORDS_BIGENDIAN
387 env
->vfp
.regs
[rd
+ 1] = fp_val
[0];
388 env
->vfp
.regs
[rd
] = fp_val
[1];
390 env
->vfp
.regs
[rd
+ 1] = fp_val
[1];
391 env
->vfp
.regs
[rd
] = fp_val
[0];
396 reg
.addr
= (uintptr_t)(&fpr
);
397 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpsr
);
398 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
402 vfp_set_fpsr(env
, fpr
);
404 reg
.id
= AARCH64_SIMD_CTRL_REG(fp_regs
.fpcr
);
405 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
409 vfp_set_fpcr(env
, fpr
);
411 if (!write_kvmstate_to_list(cpu
)) {
414 /* Note that it's OK to have registers which aren't in CPUState,
415 * so we can ignore a failure return here.
417 write_list_to_cpustate(cpu
);
419 kvm_arm_sync_mpstate_to_qemu(cpu
);
421 /* TODO: other registers */