Add qemu 2.4.0
[kvmfornfv.git] / qemu / target-arm / kvm32.c
1 /*
2  * ARM implementation of KVM hooks, 32 bit specific code.
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"
22 #include "kvm_arm.h"
23 #include "cpu.h"
24 #include "internals.h"
25 #include "hw/arm/arm.h"
26
27 static inline void set_feature(uint64_t *features, int feature)
28 {
29     *features |= 1ULL << feature;
30 }
31
32 bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc)
33 {
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      */
39     int i, ret, fdarray[3];
40     uint32_t midr, id_pfr0, id_isar0, mvfr1;
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.
45      */
46     static const uint32_t cpus_to_try[] = {
47         QEMU_KVM_ARM_TARGET_CORTEX_A15,
48         QEMU_KVM_ARM_TARGET_NONE
49     };
50     struct kvm_vcpu_init init;
51     struct kvm_one_reg idregs[] = {
52         {
53             .id = KVM_REG_ARM | KVM_REG_SIZE_U32
54             | ENCODE_CP_REG(15, 0, 0, 0, 0, 0, 0),
55             .addr = (uintptr_t)&midr,
56         },
57         {
58             .id = KVM_REG_ARM | KVM_REG_SIZE_U32
59             | ENCODE_CP_REG(15, 0, 0, 0, 1, 0, 0),
60             .addr = (uintptr_t)&id_pfr0,
61         },
62         {
63             .id = KVM_REG_ARM | KVM_REG_SIZE_U32
64             | ENCODE_CP_REG(15, 0, 0, 0, 2, 0, 0),
65             .addr = (uintptr_t)&id_isar0,
66         },
67         {
68             .id = KVM_REG_ARM | KVM_REG_SIZE_U32
69             | KVM_REG_ARM_VFP | KVM_REG_ARM_VFP_MVFR1,
70             .addr = (uintptr_t)&mvfr1,
71         },
72     };
73
74     if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
75         return false;
76     }
77
78     ahcc->target = init.target;
79
80     /* This is not strictly blessed by the device tree binding docs yet,
81      * but in practice the kernel does not care about this string so
82      * there is no point maintaining an KVM_ARM_TARGET_* -> string table.
83      */
84     ahcc->dtb_compatible = "arm,arm-v7";
85
86     for (i = 0; i < ARRAY_SIZE(idregs); i++) {
87         ret = ioctl(fdarray[2], KVM_GET_ONE_REG, &idregs[i]);
88         if (ret) {
89             break;
90         }
91     }
92
93     kvm_arm_destroy_scratch_host_vcpu(fdarray);
94
95     if (ret) {
96         return false;
97     }
98
99     /* Now we've retrieved all the register information we can
100      * set the feature bits based on the ID register fields.
101      * We can assume any KVM supporting CPU is at least a v7
102      * with VFPv3, LPAE and the generic timers; this in turn implies
103      * most of the other feature bits, but a few must be tested.
104      */
105     set_feature(&features, ARM_FEATURE_V7);
106     set_feature(&features, ARM_FEATURE_VFP3);
107     set_feature(&features, ARM_FEATURE_LPAE);
108     set_feature(&features, ARM_FEATURE_GENERIC_TIMER);
109
110     switch (extract32(id_isar0, 24, 4)) {
111     case 1:
112         set_feature(&features, ARM_FEATURE_THUMB_DIV);
113         break;
114     case 2:
115         set_feature(&features, ARM_FEATURE_ARM_DIV);
116         set_feature(&features, ARM_FEATURE_THUMB_DIV);
117         break;
118     default:
119         break;
120     }
121
122     if (extract32(id_pfr0, 12, 4) == 1) {
123         set_feature(&features, ARM_FEATURE_THUMB2EE);
124     }
125     if (extract32(mvfr1, 20, 4) == 1) {
126         set_feature(&features, ARM_FEATURE_VFP_FP16);
127     }
128     if (extract32(mvfr1, 12, 4) == 1) {
129         set_feature(&features, ARM_FEATURE_NEON);
130     }
131     if (extract32(mvfr1, 28, 4) == 1) {
132         /* FMAC support implies VFPv4 */
133         set_feature(&features, ARM_FEATURE_VFP4);
134     }
135
136     ahcc->features = features;
137
138     return true;
139 }
140
141 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx)
142 {
143     /* Return true if the regidx is a register we should synchronize
144      * via the cpreg_tuples array (ie is not a core reg we sync by
145      * hand in kvm_arch_get/put_registers())
146      */
147     switch (regidx & KVM_REG_ARM_COPROC_MASK) {
148     case KVM_REG_ARM_CORE:
149     case KVM_REG_ARM_VFP:
150         return false;
151     default:
152         return true;
153     }
154 }
155
156 typedef struct CPRegStateLevel {
157     uint64_t regidx;
158     int level;
159 } CPRegStateLevel;
160
161 /* All coprocessor registers not listed in the following table are assumed to
162  * be of the level KVM_PUT_RUNTIME_STATE. If a register should be written less
163  * often, you must add it to this table with a state of either
164  * KVM_PUT_RESET_STATE or KVM_PUT_FULL_STATE.
165  */
166 static const CPRegStateLevel non_runtime_cpregs[] = {
167     { KVM_REG_ARM_TIMER_CNT, KVM_PUT_FULL_STATE },
168 };
169
170 int kvm_arm_cpreg_level(uint64_t regidx)
171 {
172     int i;
173
174     for (i = 0; i < ARRAY_SIZE(non_runtime_cpregs); i++) {
175         const CPRegStateLevel *l = &non_runtime_cpregs[i];
176         if (l->regidx == regidx) {
177             return l->level;
178         }
179     }
180
181     return KVM_PUT_RUNTIME_STATE;
182 }
183
184 #define ARM_MPIDR_HWID_BITMASK 0xFFFFFF
185 #define ARM_CPU_ID_MPIDR       0, 0, 0, 5
186
187 int kvm_arch_init_vcpu(CPUState *cs)
188 {
189     int ret;
190     uint64_t v;
191     uint32_t mpidr;
192     struct kvm_one_reg r;
193     ARMCPU *cpu = ARM_CPU(cs);
194
195     if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE) {
196         fprintf(stderr, "KVM is not supported for this guest CPU type\n");
197         return -EINVAL;
198     }
199
200     /* Determine init features for this CPU */
201     memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
202     if (cpu->start_powered_off) {
203         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF;
204     }
205     if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) {
206         cpu->psci_version = 2;
207         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2;
208     }
209
210     /* Do KVM_ARM_VCPU_INIT ioctl */
211     ret = kvm_arm_vcpu_init(cs);
212     if (ret) {
213         return ret;
214     }
215
216     /* Query the kernel to make sure it supports 32 VFP
217      * registers: QEMU's "cortex-a15" CPU is always a
218      * VFP-D32 core. The simplest way to do this is just
219      * to attempt to read register d31.
220      */
221     r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP | 31;
222     r.addr = (uintptr_t)(&v);
223     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
224     if (ret == -ENOENT) {
225         return -EINVAL;
226     }
227
228     /*
229      * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
230      * Currently KVM has its own idea about MPIDR assignment, so we
231      * override our defaults with what we get from KVM.
232      */
233     ret = kvm_get_one_reg(cs, ARM_CP15_REG32(ARM_CPU_ID_MPIDR), &mpidr);
234     if (ret) {
235         return ret;
236     }
237     cpu->mp_affinity = mpidr & ARM_MPIDR_HWID_BITMASK;
238
239     return kvm_arm_init_cpreg_list(cpu);
240 }
241
242 typedef struct Reg {
243     uint64_t id;
244     int offset;
245 } Reg;
246
247 #define COREREG(KERNELNAME, QEMUFIELD)                       \
248     {                                                        \
249         KVM_REG_ARM | KVM_REG_SIZE_U32 |                     \
250         KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(KERNELNAME), \
251         offsetof(CPUARMState, QEMUFIELD)                     \
252     }
253
254 #define VFPSYSREG(R)                                       \
255     {                                                      \
256         KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP | \
257         KVM_REG_ARM_VFP_##R,                               \
258         offsetof(CPUARMState, vfp.xregs[ARM_VFP_##R])      \
259     }
260
261 /* Like COREREG, but handle fields which are in a uint64_t in CPUARMState. */
262 #define COREREG64(KERNELNAME, QEMUFIELD)                     \
263     {                                                        \
264         KVM_REG_ARM | KVM_REG_SIZE_U32 |                     \
265         KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(KERNELNAME), \
266         offsetoflow32(CPUARMState, QEMUFIELD)                \
267     }
268
269 static const Reg regs[] = {
270     /* R0_usr .. R14_usr */
271     COREREG(usr_regs.uregs[0], regs[0]),
272     COREREG(usr_regs.uregs[1], regs[1]),
273     COREREG(usr_regs.uregs[2], regs[2]),
274     COREREG(usr_regs.uregs[3], regs[3]),
275     COREREG(usr_regs.uregs[4], regs[4]),
276     COREREG(usr_regs.uregs[5], regs[5]),
277     COREREG(usr_regs.uregs[6], regs[6]),
278     COREREG(usr_regs.uregs[7], regs[7]),
279     COREREG(usr_regs.uregs[8], usr_regs[0]),
280     COREREG(usr_regs.uregs[9], usr_regs[1]),
281     COREREG(usr_regs.uregs[10], usr_regs[2]),
282     COREREG(usr_regs.uregs[11], usr_regs[3]),
283     COREREG(usr_regs.uregs[12], usr_regs[4]),
284     COREREG(usr_regs.uregs[13], banked_r13[0]),
285     COREREG(usr_regs.uregs[14], banked_r14[0]),
286     /* R13, R14, SPSR for SVC, ABT, UND, IRQ banks */
287     COREREG(svc_regs[0], banked_r13[1]),
288     COREREG(svc_regs[1], banked_r14[1]),
289     COREREG64(svc_regs[2], banked_spsr[1]),
290     COREREG(abt_regs[0], banked_r13[2]),
291     COREREG(abt_regs[1], banked_r14[2]),
292     COREREG64(abt_regs[2], banked_spsr[2]),
293     COREREG(und_regs[0], banked_r13[3]),
294     COREREG(und_regs[1], banked_r14[3]),
295     COREREG64(und_regs[2], banked_spsr[3]),
296     COREREG(irq_regs[0], banked_r13[4]),
297     COREREG(irq_regs[1], banked_r14[4]),
298     COREREG64(irq_regs[2], banked_spsr[4]),
299     /* R8_fiq .. R14_fiq and SPSR_fiq */
300     COREREG(fiq_regs[0], fiq_regs[0]),
301     COREREG(fiq_regs[1], fiq_regs[1]),
302     COREREG(fiq_regs[2], fiq_regs[2]),
303     COREREG(fiq_regs[3], fiq_regs[3]),
304     COREREG(fiq_regs[4], fiq_regs[4]),
305     COREREG(fiq_regs[5], banked_r13[5]),
306     COREREG(fiq_regs[6], banked_r14[5]),
307     COREREG64(fiq_regs[7], banked_spsr[5]),
308     /* R15 */
309     COREREG(usr_regs.uregs[15], regs[15]),
310     /* VFP system registers */
311     VFPSYSREG(FPSID),
312     VFPSYSREG(MVFR1),
313     VFPSYSREG(MVFR0),
314     VFPSYSREG(FPEXC),
315     VFPSYSREG(FPINST),
316     VFPSYSREG(FPINST2),
317 };
318
319 int kvm_arch_put_registers(CPUState *cs, int level)
320 {
321     ARMCPU *cpu = ARM_CPU(cs);
322     CPUARMState *env = &cpu->env;
323     struct kvm_one_reg r;
324     int mode, bn;
325     int ret, i;
326     uint32_t cpsr, fpscr;
327
328     /* Make sure the banked regs are properly set */
329     mode = env->uncached_cpsr & CPSR_M;
330     bn = bank_number(mode);
331     if (mode == ARM_CPU_MODE_FIQ) {
332         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
333     } else {
334         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
335     }
336     env->banked_r13[bn] = env->regs[13];
337     env->banked_r14[bn] = env->regs[14];
338     env->banked_spsr[bn] = env->spsr;
339
340     /* Now we can safely copy stuff down to the kernel */
341     for (i = 0; i < ARRAY_SIZE(regs); i++) {
342         r.id = regs[i].id;
343         r.addr = (uintptr_t)(env) + regs[i].offset;
344         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
345         if (ret) {
346             return ret;
347         }
348     }
349
350     /* Special cases which aren't a single CPUARMState field */
351     cpsr = cpsr_read(env);
352     r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 |
353         KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr);
354     r.addr = (uintptr_t)(&cpsr);
355     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
356     if (ret) {
357         return ret;
358     }
359
360     /* VFP registers */
361     r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP;
362     for (i = 0; i < 32; i++) {
363         r.addr = (uintptr_t)(&env->vfp.regs[i]);
364         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
365         if (ret) {
366             return ret;
367         }
368         r.id++;
369     }
370
371     r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP |
372         KVM_REG_ARM_VFP_FPSCR;
373     fpscr = vfp_get_fpscr(env);
374     r.addr = (uintptr_t)&fpscr;
375     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
376     if (ret) {
377         return ret;
378     }
379
380     /* Note that we do not call write_cpustate_to_list()
381      * here, so we are only writing the tuple list back to
382      * KVM. This is safe because nothing can change the
383      * CPUARMState cp15 fields (in particular gdb accesses cannot)
384      * and so there are no changes to sync. In fact syncing would
385      * be wrong at this point: for a constant register where TCG and
386      * KVM disagree about its value, the preceding write_list_to_cpustate()
387      * would not have had any effect on the CPUARMState value (since the
388      * register is read-only), and a write_cpustate_to_list() here would
389      * then try to write the TCG value back into KVM -- this would either
390      * fail or incorrectly change the value the guest sees.
391      *
392      * If we ever want to allow the user to modify cp15 registers via
393      * the gdb stub, we would need to be more clever here (for instance
394      * tracking the set of registers kvm_arch_get_registers() successfully
395      * managed to update the CPUARMState with, and only allowing those
396      * to be written back up into the kernel).
397      */
398     if (!write_list_to_kvmstate(cpu, level)) {
399         return EINVAL;
400     }
401
402     kvm_arm_sync_mpstate_to_kvm(cpu);
403
404     return ret;
405 }
406
407 int kvm_arch_get_registers(CPUState *cs)
408 {
409     ARMCPU *cpu = ARM_CPU(cs);
410     CPUARMState *env = &cpu->env;
411     struct kvm_one_reg r;
412     int mode, bn;
413     int ret, i;
414     uint32_t cpsr, fpscr;
415
416     for (i = 0; i < ARRAY_SIZE(regs); i++) {
417         r.id = regs[i].id;
418         r.addr = (uintptr_t)(env) + regs[i].offset;
419         ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
420         if (ret) {
421             return ret;
422         }
423     }
424
425     /* Special cases which aren't a single CPUARMState field */
426     r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 |
427         KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr);
428     r.addr = (uintptr_t)(&cpsr);
429     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
430     if (ret) {
431         return ret;
432     }
433     cpsr_write(env, cpsr, 0xffffffff);
434
435     /* Make sure the current mode regs are properly set */
436     mode = env->uncached_cpsr & CPSR_M;
437     bn = bank_number(mode);
438     if (mode == ARM_CPU_MODE_FIQ) {
439         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
440     } else {
441         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
442     }
443     env->regs[13] = env->banked_r13[bn];
444     env->regs[14] = env->banked_r14[bn];
445     env->spsr = env->banked_spsr[bn];
446
447     /* VFP registers */
448     r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP;
449     for (i = 0; i < 32; i++) {
450         r.addr = (uintptr_t)(&env->vfp.regs[i]);
451         ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
452         if (ret) {
453             return ret;
454         }
455         r.id++;
456     }
457
458     r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP |
459         KVM_REG_ARM_VFP_FPSCR;
460     r.addr = (uintptr_t)&fpscr;
461     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
462     if (ret) {
463         return ret;
464     }
465     vfp_set_fpscr(env, fpscr);
466
467     if (!write_kvmstate_to_list(cpu)) {
468         return EINVAL;
469     }
470     /* Note that it's OK to have registers which aren't in CPUState,
471      * so we can ignore a failure return here.
472      */
473     write_list_to_cpustate(cpu);
474
475     kvm_arm_sync_mpstate_to_qemu(cpu);
476
477     return 0;
478 }