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 "qemu-common.h"
19 #include "qemu/timer.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/kvm.h"
24 #include "internals.h"
25 #include "hw/arm/arm.h"
27 static inline void set_feature(uint64_t *features
, int feature
)
29 *features
|= 1ULL << feature
;
32 bool kvm_arm_get_host_cpu_features(ARMHostCPUClass
*ahcc
)
34 /* Identify the feature bits corresponding to the host CPU, and
35 * fill out the ARMHostCPUClass fields accordingly. To do this
36 * we have to create a scratch VM, create a single CPU inside it,
37 * and then query that CPU for the relevant ID registers.
38 * For AArch64 we currently don't care about ID registers at
39 * all; we just want to know the CPU type.
42 uint64_t features
= 0;
43 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
44 * we know these will only support creating one kind of guest CPU,
45 * which is its preferred CPU type. Fortunately these old kernels
46 * support only a very limited number of CPUs.
48 static const uint32_t cpus_to_try
[] = {
49 KVM_ARM_TARGET_AEM_V8
,
50 KVM_ARM_TARGET_FOUNDATION_V8
,
51 KVM_ARM_TARGET_CORTEX_A57
,
52 QEMU_KVM_ARM_TARGET_NONE
54 struct kvm_vcpu_init init
;
56 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try
, fdarray
, &init
)) {
60 ahcc
->target
= init
.target
;
61 ahcc
->dtb_compatible
= "arm,arm-v8";
63 kvm_arm_destroy_scratch_host_vcpu(fdarray
);
65 /* We can assume any KVM supporting CPU is at least a v8
66 * with VFPv4+Neon; this in turn implies most of the other
69 set_feature(&features
, ARM_FEATURE_V8
);
70 set_feature(&features
, ARM_FEATURE_VFP4
);
71 set_feature(&features
, ARM_FEATURE_NEON
);
72 set_feature(&features
, ARM_FEATURE_AARCH64
);
74 ahcc
->features
= features
;
79 int kvm_arch_init_vcpu(CPUState
*cs
)
82 ARMCPU
*cpu
= ARM_CPU(cs
);
84 if (cpu
->kvm_target
== QEMU_KVM_ARM_TARGET_NONE
||
85 !arm_feature(&cpu
->env
, ARM_FEATURE_AARCH64
)) {
86 fprintf(stderr
, "KVM is not supported for this guest CPU type\n");
90 /* Determine init features for this CPU */
91 memset(cpu
->kvm_init_features
, 0, sizeof(cpu
->kvm_init_features
));
92 if (cpu
->start_powered_off
) {
93 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_POWER_OFF
;
95 if (kvm_check_extension(cs
->kvm_state
, KVM_CAP_ARM_PSCI_0_2
)) {
96 cpu
->psci_version
= 2;
97 cpu
->kvm_init_features
[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2
;
100 /* Do KVM_ARM_VCPU_INIT ioctl */
101 ret
= kvm_arm_vcpu_init(cs
);
106 return kvm_arm_init_cpreg_list(cpu
);
109 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx
)
111 /* Return true if the regidx is a register we should synchronize
112 * via the cpreg_tuples array (ie is not a core reg we sync by
113 * hand in kvm_arch_get/put_registers())
115 switch (regidx
& KVM_REG_ARM_COPROC_MASK
) {
116 case KVM_REG_ARM_CORE
:
123 #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
124 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
126 int kvm_arch_put_registers(CPUState
*cs
, int level
)
128 struct kvm_one_reg reg
;
133 ARMCPU
*cpu
= ARM_CPU(cs
);
134 CPUARMState
*env
= &cpu
->env
;
136 for (i
= 0; i
< 31; i
++) {
137 reg
.id
= AARCH64_CORE_REG(regs
.regs
[i
]);
138 reg
.addr
= (uintptr_t) &env
->xregs
[i
];
139 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
145 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
146 * QEMU side we keep the current SP in xregs[31] as well.
148 aarch64_save_sp(env
, 1);
150 reg
.id
= AARCH64_CORE_REG(regs
.sp
);
151 reg
.addr
= (uintptr_t) &env
->sp_el
[0];
152 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
157 reg
.id
= AARCH64_CORE_REG(sp_el1
);
158 reg
.addr
= (uintptr_t) &env
->sp_el
[1];
159 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
164 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
165 val
= pstate_read(env
);
166 reg
.id
= AARCH64_CORE_REG(regs
.pstate
);
167 reg
.addr
= (uintptr_t) &val
;
168 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
173 reg
.id
= AARCH64_CORE_REG(regs
.pc
);
174 reg
.addr
= (uintptr_t) &env
->pc
;
175 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
180 reg
.id
= AARCH64_CORE_REG(elr_el1
);
181 reg
.addr
= (uintptr_t) &env
->elr_el
[1];
182 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
187 for (i
= 0; i
< KVM_NR_SPSR
; i
++) {
188 reg
.id
= AARCH64_CORE_REG(spsr
[i
]);
189 reg
.addr
= (uintptr_t) &env
->banked_spsr
[i
- 1];
190 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
203 int kvm_arch_get_registers(CPUState
*cs
)
205 struct kvm_one_reg reg
;
210 ARMCPU
*cpu
= ARM_CPU(cs
);
211 CPUARMState
*env
= &cpu
->env
;
213 for (i
= 0; i
< 31; i
++) {
214 reg
.id
= AARCH64_CORE_REG(regs
.regs
[i
]);
215 reg
.addr
= (uintptr_t) &env
->xregs
[i
];
216 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
222 reg
.id
= AARCH64_CORE_REG(regs
.sp
);
223 reg
.addr
= (uintptr_t) &env
->sp_el
[0];
224 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
229 reg
.id
= AARCH64_CORE_REG(sp_el1
);
230 reg
.addr
= (uintptr_t) &env
->sp_el
[1];
231 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
236 reg
.id
= AARCH64_CORE_REG(regs
.pstate
);
237 reg
.addr
= (uintptr_t) &val
;
238 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
242 pstate_write(env
, val
);
244 /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
245 * QEMU side we keep the current SP in xregs[31] as well.
247 aarch64_restore_sp(env
, 1);
249 reg
.id
= AARCH64_CORE_REG(regs
.pc
);
250 reg
.addr
= (uintptr_t) &env
->pc
;
251 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
256 reg
.id
= AARCH64_CORE_REG(elr_el1
);
257 reg
.addr
= (uintptr_t) &env
->elr_el
[1];
258 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
263 for (i
= 0; i
< KVM_NR_SPSR
; i
++) {
264 reg
.id
= AARCH64_CORE_REG(spsr
[i
]);
265 reg
.addr
= (uintptr_t) &env
->banked_spsr
[i
- 1];
266 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
272 /* TODO: other registers */