]>
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 | { | |
80 | ARMCPU *cpu = ARM_CPU(cs); | |
81 | struct kvm_vcpu_init init; | |
82 | int ret; | |
83 | ||
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"); | |
87 | return -EINVAL; | |
88 | } | |
89 | ||
90 | init.target = cpu->kvm_target; | |
91 | memset(init.features, 0, sizeof(init.features)); | |
92 | if (cpu->start_powered_off) { | |
93 | init.features[0] = 1 << KVM_ARM_VCPU_POWER_OFF; | |
94 | } | |
95 | ret = kvm_vcpu_ioctl(cs, KVM_ARM_VCPU_INIT, &init); | |
96 | ||
97 | /* TODO : support for save/restore/reset of system regs via tuple list */ | |
98 | ||
99 | return ret; | |
100 | } | |
101 | ||
102 | #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \ | |
103 | KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x)) | |
104 | ||
105 | int kvm_arch_put_registers(CPUState *cs, int level) | |
106 | { | |
107 | struct kvm_one_reg reg; | |
108 | uint64_t val; | |
109 | int i; | |
110 | int ret; | |
111 | ||
112 | ARMCPU *cpu = ARM_CPU(cs); | |
113 | CPUARMState *env = &cpu->env; | |
114 | ||
115 | for (i = 0; i < 31; i++) { | |
116 | reg.id = AARCH64_CORE_REG(regs.regs[i]); | |
117 | reg.addr = (uintptr_t) &env->xregs[i]; | |
118 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
119 | if (ret) { | |
120 | return ret; | |
121 | } | |
122 | } | |
123 | ||
f502cfc2 PM |
124 | /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the |
125 | * QEMU side we keep the current SP in xregs[31] as well. | |
126 | */ | |
127 | if (env->pstate & PSTATE_SP) { | |
128 | env->sp_el[1] = env->xregs[31]; | |
129 | } else { | |
130 | env->sp_el[0] = env->xregs[31]; | |
131 | } | |
132 | ||
26861c7c | 133 | reg.id = AARCH64_CORE_REG(regs.sp); |
f502cfc2 PM |
134 | reg.addr = (uintptr_t) &env->sp_el[0]; |
135 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
136 | if (ret) { | |
137 | return ret; | |
138 | } | |
139 | ||
140 | reg.id = AARCH64_CORE_REG(sp_el1); | |
141 | reg.addr = (uintptr_t) &env->sp_el[1]; | |
26861c7c MH |
142 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); |
143 | if (ret) { | |
144 | return ret; | |
145 | } | |
146 | ||
147 | /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */ | |
148 | val = pstate_read(env); | |
149 | reg.id = AARCH64_CORE_REG(regs.pstate); | |
150 | reg.addr = (uintptr_t) &val; | |
151 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
152 | if (ret) { | |
153 | return ret; | |
154 | } | |
155 | ||
156 | reg.id = AARCH64_CORE_REG(regs.pc); | |
157 | reg.addr = (uintptr_t) &env->pc; | |
158 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
159 | if (ret) { | |
160 | return ret; | |
161 | } | |
162 | ||
a0618a19 | 163 | reg.id = AARCH64_CORE_REG(elr_el1); |
6947f059 | 164 | reg.addr = (uintptr_t) &env->elr_el[1]; |
a0618a19 PM |
165 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); |
166 | if (ret) { | |
167 | return ret; | |
168 | } | |
169 | ||
a65f1de9 PM |
170 | for (i = 0; i < KVM_NR_SPSR; i++) { |
171 | reg.id = AARCH64_CORE_REG(spsr[i]); | |
172 | reg.addr = (uintptr_t) &env->banked_spsr[i - 1]; | |
173 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
174 | if (ret) { | |
175 | return ret; | |
176 | } | |
177 | } | |
178 | ||
26861c7c | 179 | /* TODO: |
26861c7c MH |
180 | * FP state |
181 | * system registers | |
182 | */ | |
183 | return ret; | |
184 | } | |
185 | ||
186 | int kvm_arch_get_registers(CPUState *cs) | |
187 | { | |
188 | struct kvm_one_reg reg; | |
189 | uint64_t val; | |
190 | int i; | |
191 | int ret; | |
192 | ||
193 | ARMCPU *cpu = ARM_CPU(cs); | |
194 | CPUARMState *env = &cpu->env; | |
195 | ||
196 | for (i = 0; i < 31; i++) { | |
197 | reg.id = AARCH64_CORE_REG(regs.regs[i]); | |
198 | reg.addr = (uintptr_t) &env->xregs[i]; | |
199 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
200 | if (ret) { | |
201 | return ret; | |
202 | } | |
203 | } | |
204 | ||
205 | reg.id = AARCH64_CORE_REG(regs.sp); | |
f502cfc2 PM |
206 | reg.addr = (uintptr_t) &env->sp_el[0]; |
207 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
208 | if (ret) { | |
209 | return ret; | |
210 | } | |
211 | ||
212 | reg.id = AARCH64_CORE_REG(sp_el1); | |
213 | reg.addr = (uintptr_t) &env->sp_el[1]; | |
26861c7c MH |
214 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); |
215 | if (ret) { | |
216 | return ret; | |
217 | } | |
218 | ||
219 | reg.id = AARCH64_CORE_REG(regs.pstate); | |
220 | reg.addr = (uintptr_t) &val; | |
221 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
222 | if (ret) { | |
223 | return ret; | |
224 | } | |
225 | pstate_write(env, val); | |
226 | ||
f502cfc2 PM |
227 | /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the |
228 | * QEMU side we keep the current SP in xregs[31] as well. | |
229 | */ | |
230 | if (env->pstate & PSTATE_SP) { | |
231 | env->xregs[31] = env->sp_el[1]; | |
232 | } else { | |
233 | env->xregs[31] = env->sp_el[0]; | |
234 | } | |
235 | ||
26861c7c MH |
236 | reg.id = AARCH64_CORE_REG(regs.pc); |
237 | reg.addr = (uintptr_t) &env->pc; | |
238 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
239 | if (ret) { | |
240 | return ret; | |
241 | } | |
242 | ||
a0618a19 | 243 | reg.id = AARCH64_CORE_REG(elr_el1); |
6947f059 | 244 | reg.addr = (uintptr_t) &env->elr_el[1]; |
a0618a19 PM |
245 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); |
246 | if (ret) { | |
247 | return ret; | |
248 | } | |
249 | ||
a65f1de9 PM |
250 | for (i = 0; i < KVM_NR_SPSR; i++) { |
251 | reg.id = AARCH64_CORE_REG(spsr[i]); | |
252 | reg.addr = (uintptr_t) &env->banked_spsr[i - 1]; | |
253 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
254 | if (ret) { | |
255 | return ret; | |
256 | } | |
257 | } | |
258 | ||
26861c7c MH |
259 | /* TODO: other registers */ |
260 | return ret; | |
261 | } | |
262 | ||
50a2c6e5 | 263 | void kvm_arm_reset_vcpu(ARMCPU *cpu) |
26861c7c MH |
264 | { |
265 | } |