]>
Commit | Line | Data |
---|---|---|
494b00c7 CD |
1 | /* |
2 | * ARM implementation of KVM hooks | |
3 | * | |
4 | * Copyright Christoffer Dall 2009-2010 | |
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" | |
eb035b48 | 22 | #include "kvm_arm.h" |
494b00c7 | 23 | #include "cpu.h" |
bd2be150 | 24 | #include "hw/arm/arm.h" |
494b00c7 | 25 | |
721fae12 PM |
26 | /* Check that cpu.h's idea of coprocessor fields matches KVM's */ |
27 | #if (CP_REG_SIZE_SHIFT != KVM_REG_SIZE_SHIFT) || \ | |
28 | (CP_REG_SIZE_MASK != KVM_REG_SIZE_MASK) || \ | |
29 | (CP_REG_SIZE_U32 != KVM_REG_SIZE_U32) || \ | |
30 | (CP_REG_SIZE_U64 != KVM_REG_SIZE_U64) || \ | |
31 | (CP_REG_ARM != KVM_REG_ARM) | |
32 | #error mismatch between cpu.h and KVM header definitions | |
33 | #endif | |
34 | ||
494b00c7 CD |
35 | const KVMCapabilityInfo kvm_arch_required_capabilities[] = { |
36 | KVM_CAP_LAST_INFO | |
37 | }; | |
38 | ||
39 | int kvm_arch_init(KVMState *s) | |
40 | { | |
41 | /* For ARM interrupt delivery is always asynchronous, | |
42 | * whether we are using an in-kernel VGIC or not. | |
43 | */ | |
44 | kvm_async_interrupts_allowed = true; | |
45 | return 0; | |
46 | } | |
47 | ||
48 | unsigned long kvm_arch_vcpu_id(CPUState *cpu) | |
49 | { | |
50 | return cpu->cpu_index; | |
51 | } | |
52 | ||
ff047453 PM |
53 | static bool reg_syncs_via_tuple_list(uint64_t regidx) |
54 | { | |
55 | /* Return true if the regidx is a register we should synchronize | |
56 | * via the cpreg_tuples array (ie is not a core reg we sync by | |
57 | * hand in kvm_arch_get/put_registers()) | |
58 | */ | |
59 | switch (regidx & KVM_REG_ARM_COPROC_MASK) { | |
60 | case KVM_REG_ARM_CORE: | |
61 | case KVM_REG_ARM_VFP: | |
62 | return false; | |
63 | default: | |
64 | return true; | |
65 | } | |
66 | } | |
67 | ||
68 | static int compare_u64(const void *a, const void *b) | |
69 | { | |
70 | return *(uint64_t *)a - *(uint64_t *)b; | |
71 | } | |
72 | ||
494b00c7 CD |
73 | int kvm_arch_init_vcpu(CPUState *cs) |
74 | { | |
75 | struct kvm_vcpu_init init; | |
ff047453 | 76 | int i, ret, arraylen; |
81635574 PM |
77 | uint64_t v; |
78 | struct kvm_one_reg r; | |
ff047453 PM |
79 | struct kvm_reg_list rl; |
80 | struct kvm_reg_list *rlp; | |
81 | ARMCPU *cpu = ARM_CPU(cs); | |
494b00c7 CD |
82 | |
83 | init.target = KVM_ARM_TARGET_CORTEX_A15; | |
84 | memset(init.features, 0, sizeof(init.features)); | |
81635574 PM |
85 | ret = kvm_vcpu_ioctl(cs, KVM_ARM_VCPU_INIT, &init); |
86 | if (ret) { | |
87 | return ret; | |
88 | } | |
89 | /* Query the kernel to make sure it supports 32 VFP | |
90 | * registers: QEMU's "cortex-a15" CPU is always a | |
91 | * VFP-D32 core. The simplest way to do this is just | |
92 | * to attempt to read register d31. | |
93 | */ | |
94 | r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP | 31; | |
95 | r.addr = (uintptr_t)(&v); | |
96 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r); | |
536f25e4 PM |
97 | if (ret == -ENOENT) { |
98 | return -EINVAL; | |
81635574 | 99 | } |
ff047453 PM |
100 | |
101 | /* Populate the cpreg list based on the kernel's idea | |
102 | * of what registers exist (and throw away the TCG-created list). | |
103 | */ | |
104 | rl.n = 0; | |
105 | ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, &rl); | |
106 | if (ret != -E2BIG) { | |
107 | return ret; | |
108 | } | |
109 | rlp = g_malloc(sizeof(struct kvm_reg_list) + rl.n * sizeof(uint64_t)); | |
110 | rlp->n = rl.n; | |
111 | ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, rlp); | |
112 | if (ret) { | |
113 | goto out; | |
114 | } | |
115 | /* Sort the list we get back from the kernel, since cpreg_tuples | |
116 | * must be in strictly ascending order. | |
117 | */ | |
118 | qsort(&rlp->reg, rlp->n, sizeof(rlp->reg[0]), compare_u64); | |
119 | ||
120 | for (i = 0, arraylen = 0; i < rlp->n; i++) { | |
121 | if (!reg_syncs_via_tuple_list(rlp->reg[i])) { | |
122 | continue; | |
123 | } | |
124 | switch (rlp->reg[i] & KVM_REG_SIZE_MASK) { | |
125 | case KVM_REG_SIZE_U32: | |
126 | case KVM_REG_SIZE_U64: | |
127 | break; | |
128 | default: | |
129 | fprintf(stderr, "Can't handle size of register in kernel list\n"); | |
130 | ret = -EINVAL; | |
131 | goto out; | |
132 | } | |
133 | ||
134 | arraylen++; | |
135 | } | |
136 | ||
137 | cpu->cpreg_indexes = g_renew(uint64_t, cpu->cpreg_indexes, arraylen); | |
138 | cpu->cpreg_values = g_renew(uint64_t, cpu->cpreg_values, arraylen); | |
139 | cpu->cpreg_vmstate_indexes = g_renew(uint64_t, cpu->cpreg_vmstate_indexes, | |
140 | arraylen); | |
141 | cpu->cpreg_vmstate_values = g_renew(uint64_t, cpu->cpreg_vmstate_values, | |
142 | arraylen); | |
143 | cpu->cpreg_array_len = arraylen; | |
144 | cpu->cpreg_vmstate_array_len = arraylen; | |
145 | ||
146 | for (i = 0, arraylen = 0; i < rlp->n; i++) { | |
147 | uint64_t regidx = rlp->reg[i]; | |
148 | if (!reg_syncs_via_tuple_list(regidx)) { | |
149 | continue; | |
150 | } | |
151 | cpu->cpreg_indexes[arraylen] = regidx; | |
152 | arraylen++; | |
153 | } | |
154 | assert(cpu->cpreg_array_len == arraylen); | |
155 | ||
156 | if (!write_kvmstate_to_list(cpu)) { | |
157 | /* Shouldn't happen unless kernel is inconsistent about | |
158 | * what registers exist. | |
159 | */ | |
160 | fprintf(stderr, "Initial read of kernel register state failed\n"); | |
161 | ret = -EINVAL; | |
162 | goto out; | |
163 | } | |
164 | ||
165 | out: | |
166 | g_free(rlp); | |
81635574 | 167 | return ret; |
494b00c7 CD |
168 | } |
169 | ||
eb035b48 PM |
170 | /* We track all the KVM devices which need their memory addresses |
171 | * passing to the kernel in a list of these structures. | |
172 | * When board init is complete we run through the list and | |
173 | * tell the kernel the base addresses of the memory regions. | |
174 | * We use a MemoryListener to track mapping and unmapping of | |
175 | * the regions during board creation, so the board models don't | |
176 | * need to do anything special for the KVM case. | |
177 | */ | |
178 | typedef struct KVMDevice { | |
179 | struct kvm_arm_device_addr kda; | |
180 | MemoryRegion *mr; | |
181 | QSLIST_ENTRY(KVMDevice) entries; | |
182 | } KVMDevice; | |
183 | ||
184 | static QSLIST_HEAD(kvm_devices_head, KVMDevice) kvm_devices_head; | |
185 | ||
186 | static void kvm_arm_devlistener_add(MemoryListener *listener, | |
187 | MemoryRegionSection *section) | |
188 | { | |
189 | KVMDevice *kd; | |
190 | ||
191 | QSLIST_FOREACH(kd, &kvm_devices_head, entries) { | |
192 | if (section->mr == kd->mr) { | |
193 | kd->kda.addr = section->offset_within_address_space; | |
194 | } | |
195 | } | |
196 | } | |
197 | ||
198 | static void kvm_arm_devlistener_del(MemoryListener *listener, | |
199 | MemoryRegionSection *section) | |
200 | { | |
201 | KVMDevice *kd; | |
202 | ||
203 | QSLIST_FOREACH(kd, &kvm_devices_head, entries) { | |
204 | if (section->mr == kd->mr) { | |
205 | kd->kda.addr = -1; | |
206 | } | |
207 | } | |
208 | } | |
209 | ||
210 | static MemoryListener devlistener = { | |
211 | .region_add = kvm_arm_devlistener_add, | |
212 | .region_del = kvm_arm_devlistener_del, | |
213 | }; | |
214 | ||
215 | static void kvm_arm_machine_init_done(Notifier *notifier, void *data) | |
216 | { | |
217 | KVMDevice *kd, *tkd; | |
218 | ||
219 | memory_listener_unregister(&devlistener); | |
220 | QSLIST_FOREACH_SAFE(kd, &kvm_devices_head, entries, tkd) { | |
221 | if (kd->kda.addr != -1) { | |
222 | if (kvm_vm_ioctl(kvm_state, KVM_ARM_SET_DEVICE_ADDR, | |
223 | &kd->kda) < 0) { | |
224 | fprintf(stderr, "KVM_ARM_SET_DEVICE_ADDRESS failed: %s\n", | |
225 | strerror(errno)); | |
226 | abort(); | |
227 | } | |
228 | } | |
229 | g_free(kd); | |
230 | } | |
231 | } | |
232 | ||
233 | static Notifier notify = { | |
234 | .notify = kvm_arm_machine_init_done, | |
235 | }; | |
236 | ||
237 | void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid) | |
238 | { | |
239 | KVMDevice *kd; | |
240 | ||
241 | if (!kvm_irqchip_in_kernel()) { | |
242 | return; | |
243 | } | |
244 | ||
245 | if (QSLIST_EMPTY(&kvm_devices_head)) { | |
246 | memory_listener_register(&devlistener, NULL); | |
247 | qemu_add_machine_init_done_notifier(¬ify); | |
248 | } | |
249 | kd = g_new0(KVMDevice, 1); | |
250 | kd->mr = mr; | |
251 | kd->kda.id = devid; | |
252 | kd->kda.addr = -1; | |
253 | QSLIST_INSERT_HEAD(&kvm_devices_head, kd, entries); | |
254 | } | |
255 | ||
ff047453 PM |
256 | bool write_kvmstate_to_list(ARMCPU *cpu) |
257 | { | |
258 | CPUState *cs = CPU(cpu); | |
259 | int i; | |
260 | bool ok = true; | |
261 | ||
262 | for (i = 0; i < cpu->cpreg_array_len; i++) { | |
263 | struct kvm_one_reg r; | |
264 | uint64_t regidx = cpu->cpreg_indexes[i]; | |
265 | uint32_t v32; | |
266 | int ret; | |
267 | ||
268 | r.id = regidx; | |
269 | ||
270 | switch (regidx & KVM_REG_SIZE_MASK) { | |
271 | case KVM_REG_SIZE_U32: | |
272 | r.addr = (uintptr_t)&v32; | |
273 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r); | |
274 | if (!ret) { | |
275 | cpu->cpreg_values[i] = v32; | |
276 | } | |
277 | break; | |
278 | case KVM_REG_SIZE_U64: | |
279 | r.addr = (uintptr_t)(cpu->cpreg_values + i); | |
280 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r); | |
281 | break; | |
282 | default: | |
283 | abort(); | |
284 | } | |
285 | if (ret) { | |
286 | ok = false; | |
287 | } | |
288 | } | |
289 | return ok; | |
290 | } | |
291 | ||
292 | bool write_list_to_kvmstate(ARMCPU *cpu) | |
293 | { | |
294 | CPUState *cs = CPU(cpu); | |
295 | int i; | |
296 | bool ok = true; | |
297 | ||
298 | for (i = 0; i < cpu->cpreg_array_len; i++) { | |
299 | struct kvm_one_reg r; | |
300 | uint64_t regidx = cpu->cpreg_indexes[i]; | |
301 | uint32_t v32; | |
302 | int ret; | |
303 | ||
304 | r.id = regidx; | |
305 | switch (regidx & KVM_REG_SIZE_MASK) { | |
306 | case KVM_REG_SIZE_U32: | |
307 | v32 = cpu->cpreg_values[i]; | |
308 | r.addr = (uintptr_t)&v32; | |
309 | break; | |
310 | case KVM_REG_SIZE_U64: | |
311 | r.addr = (uintptr_t)(cpu->cpreg_values + i); | |
312 | break; | |
313 | default: | |
314 | abort(); | |
315 | } | |
316 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r); | |
317 | if (ret) { | |
318 | /* We might fail for "unknown register" and also for | |
319 | * "you tried to set a register which is constant with | |
320 | * a different value from what it actually contains". | |
321 | */ | |
322 | ok = false; | |
323 | } | |
324 | } | |
325 | return ok; | |
326 | } | |
327 | ||
494b00c7 CD |
328 | typedef struct Reg { |
329 | uint64_t id; | |
330 | int offset; | |
331 | } Reg; | |
332 | ||
333 | #define COREREG(KERNELNAME, QEMUFIELD) \ | |
334 | { \ | |
335 | KVM_REG_ARM | KVM_REG_SIZE_U32 | \ | |
336 | KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(KERNELNAME), \ | |
337 | offsetof(CPUARMState, QEMUFIELD) \ | |
338 | } | |
339 | ||
340 | #define CP15REG(CRN, CRM, OPC1, OPC2, QEMUFIELD) \ | |
341 | { \ | |
342 | KVM_REG_ARM | KVM_REG_SIZE_U32 | \ | |
343 | (15 << KVM_REG_ARM_COPROC_SHIFT) | \ | |
344 | ((CRN) << KVM_REG_ARM_32_CRN_SHIFT) | \ | |
345 | ((CRM) << KVM_REG_ARM_CRM_SHIFT) | \ | |
346 | ((OPC1) << KVM_REG_ARM_OPC1_SHIFT) | \ | |
347 | ((OPC2) << KVM_REG_ARM_32_OPC2_SHIFT), \ | |
348 | offsetof(CPUARMState, QEMUFIELD) \ | |
349 | } | |
350 | ||
81635574 PM |
351 | #define VFPSYSREG(R) \ |
352 | { \ | |
353 | KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP | \ | |
354 | KVM_REG_ARM_VFP_##R, \ | |
355 | offsetof(CPUARMState, vfp.xregs[ARM_VFP_##R]) \ | |
356 | } | |
357 | ||
494b00c7 CD |
358 | static const Reg regs[] = { |
359 | /* R0_usr .. R14_usr */ | |
360 | COREREG(usr_regs.uregs[0], regs[0]), | |
361 | COREREG(usr_regs.uregs[1], regs[1]), | |
362 | COREREG(usr_regs.uregs[2], regs[2]), | |
363 | COREREG(usr_regs.uregs[3], regs[3]), | |
364 | COREREG(usr_regs.uregs[4], regs[4]), | |
365 | COREREG(usr_regs.uregs[5], regs[5]), | |
366 | COREREG(usr_regs.uregs[6], regs[6]), | |
367 | COREREG(usr_regs.uregs[7], regs[7]), | |
368 | COREREG(usr_regs.uregs[8], usr_regs[0]), | |
369 | COREREG(usr_regs.uregs[9], usr_regs[1]), | |
370 | COREREG(usr_regs.uregs[10], usr_regs[2]), | |
371 | COREREG(usr_regs.uregs[11], usr_regs[3]), | |
372 | COREREG(usr_regs.uregs[12], usr_regs[4]), | |
373 | COREREG(usr_regs.uregs[13], banked_r13[0]), | |
374 | COREREG(usr_regs.uregs[14], banked_r14[0]), | |
375 | /* R13, R14, SPSR for SVC, ABT, UND, IRQ banks */ | |
376 | COREREG(svc_regs[0], banked_r13[1]), | |
377 | COREREG(svc_regs[1], banked_r14[1]), | |
378 | COREREG(svc_regs[2], banked_spsr[1]), | |
379 | COREREG(abt_regs[0], banked_r13[2]), | |
380 | COREREG(abt_regs[1], banked_r14[2]), | |
381 | COREREG(abt_regs[2], banked_spsr[2]), | |
382 | COREREG(und_regs[0], banked_r13[3]), | |
383 | COREREG(und_regs[1], banked_r14[3]), | |
384 | COREREG(und_regs[2], banked_spsr[3]), | |
385 | COREREG(irq_regs[0], banked_r13[4]), | |
386 | COREREG(irq_regs[1], banked_r14[4]), | |
387 | COREREG(irq_regs[2], banked_spsr[4]), | |
388 | /* R8_fiq .. R14_fiq and SPSR_fiq */ | |
389 | COREREG(fiq_regs[0], fiq_regs[0]), | |
390 | COREREG(fiq_regs[1], fiq_regs[1]), | |
391 | COREREG(fiq_regs[2], fiq_regs[2]), | |
392 | COREREG(fiq_regs[3], fiq_regs[3]), | |
393 | COREREG(fiq_regs[4], fiq_regs[4]), | |
394 | COREREG(fiq_regs[5], banked_r13[5]), | |
395 | COREREG(fiq_regs[6], banked_r14[5]), | |
396 | COREREG(fiq_regs[7], banked_spsr[5]), | |
397 | /* R15 */ | |
398 | COREREG(usr_regs.uregs[15], regs[15]), | |
399 | /* A non-comprehensive set of cp15 registers. | |
400 | * TODO: drive this from the cp_regs hashtable instead. | |
401 | */ | |
402 | CP15REG(1, 0, 0, 0, cp15.c1_sys), /* SCTLR */ | |
403 | CP15REG(2, 0, 0, 2, cp15.c2_control), /* TTBCR */ | |
404 | CP15REG(3, 0, 0, 0, cp15.c3), /* DACR */ | |
81635574 PM |
405 | /* VFP system registers */ |
406 | VFPSYSREG(FPSID), | |
407 | VFPSYSREG(MVFR1), | |
408 | VFPSYSREG(MVFR0), | |
409 | VFPSYSREG(FPEXC), | |
410 | VFPSYSREG(FPINST), | |
411 | VFPSYSREG(FPINST2), | |
494b00c7 CD |
412 | }; |
413 | ||
414 | int kvm_arch_put_registers(CPUState *cs, int level) | |
415 | { | |
416 | ARMCPU *cpu = ARM_CPU(cs); | |
417 | CPUARMState *env = &cpu->env; | |
418 | struct kvm_one_reg r; | |
419 | int mode, bn; | |
420 | int ret, i; | |
81635574 | 421 | uint32_t cpsr, fpscr; |
494b00c7 CD |
422 | uint64_t ttbr; |
423 | ||
424 | /* Make sure the banked regs are properly set */ | |
425 | mode = env->uncached_cpsr & CPSR_M; | |
426 | bn = bank_number(mode); | |
427 | if (mode == ARM_CPU_MODE_FIQ) { | |
428 | memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); | |
429 | } else { | |
430 | memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); | |
431 | } | |
432 | env->banked_r13[bn] = env->regs[13]; | |
433 | env->banked_r14[bn] = env->regs[14]; | |
434 | env->banked_spsr[bn] = env->spsr; | |
435 | ||
436 | /* Now we can safely copy stuff down to the kernel */ | |
437 | for (i = 0; i < ARRAY_SIZE(regs); i++) { | |
438 | r.id = regs[i].id; | |
439 | r.addr = (uintptr_t)(env) + regs[i].offset; | |
440 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r); | |
441 | if (ret) { | |
442 | return ret; | |
443 | } | |
444 | } | |
445 | ||
446 | /* Special cases which aren't a single CPUARMState field */ | |
447 | cpsr = cpsr_read(env); | |
448 | r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | | |
449 | KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr); | |
450 | r.addr = (uintptr_t)(&cpsr); | |
451 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r); | |
452 | if (ret) { | |
453 | return ret; | |
454 | } | |
455 | ||
456 | /* TTBR0: cp15 crm=2 opc1=0 */ | |
457 | ttbr = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0; | |
458 | r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | (15 << KVM_REG_ARM_COPROC_SHIFT) | | |
459 | (2 << KVM_REG_ARM_CRM_SHIFT) | (0 << KVM_REG_ARM_OPC1_SHIFT); | |
460 | r.addr = (uintptr_t)(&ttbr); | |
461 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r); | |
462 | if (ret) { | |
463 | return ret; | |
464 | } | |
465 | ||
466 | /* TTBR1: cp15 crm=2 opc1=1 */ | |
467 | ttbr = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1; | |
468 | r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | (15 << KVM_REG_ARM_COPROC_SHIFT) | | |
469 | (2 << KVM_REG_ARM_CRM_SHIFT) | (1 << KVM_REG_ARM_OPC1_SHIFT); | |
470 | r.addr = (uintptr_t)(&ttbr); | |
471 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r); | |
81635574 PM |
472 | if (ret) { |
473 | return ret; | |
474 | } | |
475 | ||
476 | /* VFP registers */ | |
477 | r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP; | |
478 | for (i = 0; i < 32; i++) { | |
479 | r.addr = (uintptr_t)(&env->vfp.regs[i]); | |
480 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r); | |
481 | if (ret) { | |
482 | return ret; | |
483 | } | |
484 | r.id++; | |
485 | } | |
486 | ||
487 | r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP | | |
488 | KVM_REG_ARM_VFP_FPSCR; | |
489 | fpscr = vfp_get_fpscr(env); | |
490 | r.addr = (uintptr_t)&fpscr; | |
491 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r); | |
494b00c7 CD |
492 | |
493 | return ret; | |
494 | } | |
495 | ||
496 | int kvm_arch_get_registers(CPUState *cs) | |
497 | { | |
498 | ARMCPU *cpu = ARM_CPU(cs); | |
499 | CPUARMState *env = &cpu->env; | |
500 | struct kvm_one_reg r; | |
501 | int mode, bn; | |
502 | int ret, i; | |
81635574 | 503 | uint32_t cpsr, fpscr; |
494b00c7 CD |
504 | uint64_t ttbr; |
505 | ||
506 | for (i = 0; i < ARRAY_SIZE(regs); i++) { | |
507 | r.id = regs[i].id; | |
508 | r.addr = (uintptr_t)(env) + regs[i].offset; | |
509 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r); | |
510 | if (ret) { | |
511 | return ret; | |
512 | } | |
513 | } | |
514 | ||
515 | /* Special cases which aren't a single CPUARMState field */ | |
516 | r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | | |
517 | KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr); | |
518 | r.addr = (uintptr_t)(&cpsr); | |
519 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r); | |
520 | if (ret) { | |
521 | return ret; | |
522 | } | |
523 | cpsr_write(env, cpsr, 0xffffffff); | |
524 | ||
525 | /* TTBR0: cp15 crm=2 opc1=0 */ | |
526 | r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | (15 << KVM_REG_ARM_COPROC_SHIFT) | | |
527 | (2 << KVM_REG_ARM_CRM_SHIFT) | (0 << KVM_REG_ARM_OPC1_SHIFT); | |
528 | r.addr = (uintptr_t)(&ttbr); | |
529 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r); | |
530 | if (ret) { | |
531 | return ret; | |
532 | } | |
533 | env->cp15.c2_base0_hi = ttbr >> 32; | |
534 | env->cp15.c2_base0 = ttbr; | |
535 | ||
536 | /* TTBR1: cp15 crm=2 opc1=1 */ | |
537 | r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | (15 << KVM_REG_ARM_COPROC_SHIFT) | | |
538 | (2 << KVM_REG_ARM_CRM_SHIFT) | (1 << KVM_REG_ARM_OPC1_SHIFT); | |
539 | r.addr = (uintptr_t)(&ttbr); | |
540 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r); | |
541 | if (ret) { | |
542 | return ret; | |
543 | } | |
544 | env->cp15.c2_base1_hi = ttbr >> 32; | |
545 | env->cp15.c2_base1 = ttbr; | |
546 | ||
547 | /* Make sure the current mode regs are properly set */ | |
548 | mode = env->uncached_cpsr & CPSR_M; | |
549 | bn = bank_number(mode); | |
550 | if (mode == ARM_CPU_MODE_FIQ) { | |
551 | memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); | |
552 | } else { | |
553 | memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); | |
554 | } | |
555 | env->regs[13] = env->banked_r13[bn]; | |
556 | env->regs[14] = env->banked_r14[bn]; | |
557 | env->spsr = env->banked_spsr[bn]; | |
558 | ||
559 | /* The main GET_ONE_REG loop above set c2_control, but we need to | |
560 | * update some extra cached precomputed values too. | |
561 | * When this is driven from the cp_regs hashtable then this ugliness | |
562 | * can disappear because we'll use the access function which sets | |
563 | * these values automatically. | |
564 | */ | |
565 | env->cp15.c2_mask = ~(0xffffffffu >> env->cp15.c2_control); | |
566 | env->cp15.c2_base_mask = ~(0x3fffu >> env->cp15.c2_control); | |
567 | ||
81635574 PM |
568 | /* VFP registers */ |
569 | r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP; | |
570 | for (i = 0; i < 32; i++) { | |
571 | r.addr = (uintptr_t)(&env->vfp.regs[i]); | |
572 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r); | |
573 | if (ret) { | |
574 | return ret; | |
575 | } | |
576 | r.id++; | |
577 | } | |
578 | ||
579 | r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP | | |
580 | KVM_REG_ARM_VFP_FPSCR; | |
581 | r.addr = (uintptr_t)&fpscr; | |
582 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r); | |
583 | if (ret) { | |
584 | return ret; | |
585 | } | |
586 | vfp_set_fpscr(env, fpscr); | |
587 | ||
494b00c7 CD |
588 | return 0; |
589 | } | |
590 | ||
591 | void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) | |
592 | { | |
593 | } | |
594 | ||
595 | void kvm_arch_post_run(CPUState *cs, struct kvm_run *run) | |
596 | { | |
597 | } | |
598 | ||
599 | int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) | |
600 | { | |
601 | return 0; | |
602 | } | |
603 | ||
604 | void kvm_arch_reset_vcpu(CPUState *cs) | |
605 | { | |
606 | } | |
607 | ||
608 | bool kvm_arch_stop_on_emulation_error(CPUState *cs) | |
609 | { | |
610 | return true; | |
611 | } | |
612 | ||
613 | int kvm_arch_process_async_events(CPUState *cs) | |
614 | { | |
615 | return 0; | |
616 | } | |
617 | ||
618 | int kvm_arch_on_sigbus_vcpu(CPUState *cs, int code, void *addr) | |
619 | { | |
620 | return 1; | |
621 | } | |
622 | ||
623 | int kvm_arch_on_sigbus(int code, void *addr) | |
624 | { | |
625 | return 1; | |
626 | } | |
627 | ||
628 | void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg) | |
629 | { | |
630 | qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__); | |
631 | } | |
632 | ||
633 | int kvm_arch_insert_sw_breakpoint(CPUState *cs, | |
634 | struct kvm_sw_breakpoint *bp) | |
635 | { | |
636 | qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__); | |
637 | return -EINVAL; | |
638 | } | |
639 | ||
640 | int kvm_arch_insert_hw_breakpoint(target_ulong addr, | |
641 | target_ulong len, int type) | |
642 | { | |
643 | qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__); | |
644 | return -EINVAL; | |
645 | } | |
646 | ||
647 | int kvm_arch_remove_hw_breakpoint(target_ulong addr, | |
648 | target_ulong len, int type) | |
649 | { | |
650 | qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__); | |
651 | return -EINVAL; | |
652 | } | |
653 | ||
654 | int kvm_arch_remove_sw_breakpoint(CPUState *cs, | |
655 | struct kvm_sw_breakpoint *bp) | |
656 | { | |
657 | qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__); | |
658 | return -EINVAL; | |
659 | } | |
660 | ||
661 | void kvm_arch_remove_all_hw_breakpoints(void) | |
662 | { | |
663 | qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__); | |
664 | } | |
b3a1c626 AK |
665 | |
666 | void kvm_arch_init_irq_routing(KVMState *s) | |
667 | { | |
668 | } |