]>
Commit | Line | Data |
---|---|---|
26861c7c MH |
1 | /* |
2 | * ARM implementation of KVM hooks, 64 bit specific code | |
3 | * | |
4 | * Copyright Mian-M. Hamayun 2013, Virtual Open Systems | |
5 | * | |
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. | |
8 | * | |
9 | */ | |
10 | ||
11 | #include <stdio.h> | |
12 | #include <sys/types.h> | |
13 | #include <sys/ioctl.h> | |
14 | #include <sys/mman.h> | |
15 | ||
16 | #include <linux/kvm.h> | |
17 | ||
18 | #include "qemu-common.h" | |
19 | #include "qemu/timer.h" | |
20 | #include "sysemu/sysemu.h" | |
21 | #include "sysemu/kvm.h" | |
22 | #include "kvm_arm.h" | |
23 | #include "cpu.h" | |
24 | #include "hw/arm/arm.h" | |
25 | ||
26 | static inline void set_feature(uint64_t *features, int feature) | |
27 | { | |
28 | *features |= 1ULL << feature; | |
29 | } | |
30 | ||
31 | bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc) | |
32 | { | |
33 | /* Identify the feature bits corresponding to the host CPU, and | |
34 | * fill out the ARMHostCPUClass fields accordingly. To do this | |
35 | * we have to create a scratch VM, create a single CPU inside it, | |
36 | * and then query that CPU for the relevant ID registers. | |
37 | * For AArch64 we currently don't care about ID registers at | |
38 | * all; we just want to know the CPU type. | |
39 | */ | |
40 | int fdarray[3]; | |
41 | uint64_t features = 0; | |
42 | /* Old kernels may not know about the PREFERRED_TARGET ioctl: however | |
43 | * we know these will only support creating one kind of guest CPU, | |
44 | * which is its preferred CPU type. Fortunately these old kernels | |
45 | * support only a very limited number of CPUs. | |
46 | */ | |
47 | static const uint32_t cpus_to_try[] = { | |
48 | KVM_ARM_TARGET_AEM_V8, | |
49 | KVM_ARM_TARGET_FOUNDATION_V8, | |
50 | KVM_ARM_TARGET_CORTEX_A57, | |
51 | QEMU_KVM_ARM_TARGET_NONE | |
52 | }; | |
53 | struct kvm_vcpu_init init; | |
54 | ||
55 | if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) { | |
56 | return false; | |
57 | } | |
58 | ||
59 | ahcc->target = init.target; | |
60 | ahcc->dtb_compatible = "arm,arm-v8"; | |
61 | ||
62 | kvm_arm_destroy_scratch_host_vcpu(fdarray); | |
63 | ||
64 | /* We can assume any KVM supporting CPU is at least a v8 | |
65 | * with VFPv4+Neon; this in turn implies most of the other | |
66 | * feature bits. | |
67 | */ | |
68 | set_feature(&features, ARM_FEATURE_V8); | |
69 | set_feature(&features, ARM_FEATURE_VFP4); | |
70 | set_feature(&features, ARM_FEATURE_NEON); | |
71 | set_feature(&features, ARM_FEATURE_AARCH64); | |
72 | ||
73 | ahcc->features = features; | |
74 | ||
75 | return true; | |
76 | } | |
77 | ||
78 | int kvm_arch_init_vcpu(CPUState *cs) | |
79 | { | |
26861c7c | 80 | int ret; |
228d5e04 | 81 | ARMCPU *cpu = ARM_CPU(cs); |
26861c7c MH |
82 | |
83 | if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE || | |
84 | !arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { | |
85 | fprintf(stderr, "KVM is not supported for this guest CPU type\n"); | |
86 | return -EINVAL; | |
87 | } | |
88 | ||
228d5e04 PS |
89 | /* Determine init features for this CPU */ |
90 | memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features)); | |
26861c7c | 91 | if (cpu->start_powered_off) { |
228d5e04 PS |
92 | cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF; |
93 | } | |
7cd62e53 | 94 | if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) { |
dd032e34 | 95 | cpu->psci_version = 2; |
7cd62e53 PS |
96 | cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2; |
97 | } | |
228d5e04 PS |
98 | |
99 | /* Do KVM_ARM_VCPU_INIT ioctl */ | |
100 | ret = kvm_arm_vcpu_init(cs); | |
101 | if (ret) { | |
102 | return ret; | |
26861c7c | 103 | } |
26861c7c MH |
104 | |
105 | /* TODO : support for save/restore/reset of system regs via tuple list */ | |
106 | ||
228d5e04 | 107 | return 0; |
26861c7c MH |
108 | } |
109 | ||
110 | #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \ | |
111 | KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x)) | |
112 | ||
113 | int kvm_arch_put_registers(CPUState *cs, int level) | |
114 | { | |
115 | struct kvm_one_reg reg; | |
116 | uint64_t val; | |
117 | int i; | |
118 | int ret; | |
119 | ||
120 | ARMCPU *cpu = ARM_CPU(cs); | |
121 | CPUARMState *env = &cpu->env; | |
122 | ||
123 | for (i = 0; i < 31; i++) { | |
124 | reg.id = AARCH64_CORE_REG(regs.regs[i]); | |
125 | reg.addr = (uintptr_t) &env->xregs[i]; | |
126 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
127 | if (ret) { | |
128 | return ret; | |
129 | } | |
130 | } | |
131 | ||
f502cfc2 PM |
132 | /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the |
133 | * QEMU side we keep the current SP in xregs[31] as well. | |
134 | */ | |
135 | if (env->pstate & PSTATE_SP) { | |
136 | env->sp_el[1] = env->xregs[31]; | |
137 | } else { | |
138 | env->sp_el[0] = env->xregs[31]; | |
139 | } | |
140 | ||
26861c7c | 141 | reg.id = AARCH64_CORE_REG(regs.sp); |
f502cfc2 PM |
142 | reg.addr = (uintptr_t) &env->sp_el[0]; |
143 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
144 | if (ret) { | |
145 | return ret; | |
146 | } | |
147 | ||
148 | reg.id = AARCH64_CORE_REG(sp_el1); | |
149 | reg.addr = (uintptr_t) &env->sp_el[1]; | |
26861c7c MH |
150 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); |
151 | if (ret) { | |
152 | return ret; | |
153 | } | |
154 | ||
155 | /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */ | |
156 | val = pstate_read(env); | |
157 | reg.id = AARCH64_CORE_REG(regs.pstate); | |
158 | reg.addr = (uintptr_t) &val; | |
159 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
160 | if (ret) { | |
161 | return ret; | |
162 | } | |
163 | ||
164 | reg.id = AARCH64_CORE_REG(regs.pc); | |
165 | reg.addr = (uintptr_t) &env->pc; | |
166 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
167 | if (ret) { | |
168 | return ret; | |
169 | } | |
170 | ||
a0618a19 | 171 | reg.id = AARCH64_CORE_REG(elr_el1); |
6947f059 | 172 | reg.addr = (uintptr_t) &env->elr_el[1]; |
a0618a19 PM |
173 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); |
174 | if (ret) { | |
175 | return ret; | |
176 | } | |
177 | ||
a65f1de9 PM |
178 | for (i = 0; i < KVM_NR_SPSR; i++) { |
179 | reg.id = AARCH64_CORE_REG(spsr[i]); | |
180 | reg.addr = (uintptr_t) &env->banked_spsr[i - 1]; | |
181 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
182 | if (ret) { | |
183 | return ret; | |
184 | } | |
185 | } | |
186 | ||
26861c7c | 187 | /* TODO: |
26861c7c MH |
188 | * FP state |
189 | * system registers | |
190 | */ | |
191 | return ret; | |
192 | } | |
193 | ||
194 | int kvm_arch_get_registers(CPUState *cs) | |
195 | { | |
196 | struct kvm_one_reg reg; | |
197 | uint64_t val; | |
198 | int i; | |
199 | int ret; | |
200 | ||
201 | ARMCPU *cpu = ARM_CPU(cs); | |
202 | CPUARMState *env = &cpu->env; | |
203 | ||
204 | for (i = 0; i < 31; i++) { | |
205 | reg.id = AARCH64_CORE_REG(regs.regs[i]); | |
206 | reg.addr = (uintptr_t) &env->xregs[i]; | |
207 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
208 | if (ret) { | |
209 | return ret; | |
210 | } | |
211 | } | |
212 | ||
213 | reg.id = AARCH64_CORE_REG(regs.sp); | |
f502cfc2 PM |
214 | reg.addr = (uintptr_t) &env->sp_el[0]; |
215 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
216 | if (ret) { | |
217 | return ret; | |
218 | } | |
219 | ||
220 | reg.id = AARCH64_CORE_REG(sp_el1); | |
221 | reg.addr = (uintptr_t) &env->sp_el[1]; | |
26861c7c MH |
222 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); |
223 | if (ret) { | |
224 | return ret; | |
225 | } | |
226 | ||
227 | reg.id = AARCH64_CORE_REG(regs.pstate); | |
228 | reg.addr = (uintptr_t) &val; | |
229 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
230 | if (ret) { | |
231 | return ret; | |
232 | } | |
233 | pstate_write(env, val); | |
234 | ||
f502cfc2 PM |
235 | /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the |
236 | * QEMU side we keep the current SP in xregs[31] as well. | |
237 | */ | |
238 | if (env->pstate & PSTATE_SP) { | |
239 | env->xregs[31] = env->sp_el[1]; | |
240 | } else { | |
241 | env->xregs[31] = env->sp_el[0]; | |
242 | } | |
243 | ||
26861c7c MH |
244 | reg.id = AARCH64_CORE_REG(regs.pc); |
245 | reg.addr = (uintptr_t) &env->pc; | |
246 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
247 | if (ret) { | |
248 | return ret; | |
249 | } | |
250 | ||
a0618a19 | 251 | reg.id = AARCH64_CORE_REG(elr_el1); |
6947f059 | 252 | reg.addr = (uintptr_t) &env->elr_el[1]; |
a0618a19 PM |
253 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); |
254 | if (ret) { | |
255 | return ret; | |
256 | } | |
257 | ||
a65f1de9 PM |
258 | for (i = 0; i < KVM_NR_SPSR; i++) { |
259 | reg.id = AARCH64_CORE_REG(spsr[i]); | |
260 | reg.addr = (uintptr_t) &env->banked_spsr[i - 1]; | |
261 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
262 | if (ret) { | |
263 | return ret; | |
264 | } | |
265 | } | |
266 | ||
26861c7c MH |
267 | /* TODO: other registers */ |
268 | return ret; | |
269 | } | |
270 | ||
50a2c6e5 | 271 | void kvm_arm_reset_vcpu(ARMCPU *cpu) |
26861c7c | 272 | { |
73542cf6 PS |
273 | /* Re-init VCPU so that all registers are set to |
274 | * their respective reset values. | |
275 | */ | |
276 | kvm_arm_vcpu_init(CPU(cpu)); | |
26861c7c | 277 | } |