pci core: function pci_host_bus_register() cleanup
[qemu/kevin.git] / target-arm / machine.c
blobed1925ae3eea0ed27845d3749ad993af5dd3dcd1
1 #include "qemu/osdep.h"
2 #include "hw/hw.h"
3 #include "hw/boards.h"
4 #include "qemu/error-report.h"
5 #include "sysemu/kvm.h"
6 #include "kvm_arm.h"
7 #include "internals.h"
9 static bool vfp_needed(void *opaque)
11 ARMCPU *cpu = opaque;
12 CPUARMState *env = &cpu->env;
14 return arm_feature(env, ARM_FEATURE_VFP);
17 static int get_fpscr(QEMUFile *f, void *opaque, size_t size)
19 ARMCPU *cpu = opaque;
20 CPUARMState *env = &cpu->env;
21 uint32_t val = qemu_get_be32(f);
23 vfp_set_fpscr(env, val);
24 return 0;
27 static void put_fpscr(QEMUFile *f, void *opaque, size_t size)
29 ARMCPU *cpu = opaque;
30 CPUARMState *env = &cpu->env;
32 qemu_put_be32(f, vfp_get_fpscr(env));
35 static const VMStateInfo vmstate_fpscr = {
36 .name = "fpscr",
37 .get = get_fpscr,
38 .put = put_fpscr,
41 static const VMStateDescription vmstate_vfp = {
42 .name = "cpu/vfp",
43 .version_id = 3,
44 .minimum_version_id = 3,
45 .needed = vfp_needed,
46 .fields = (VMStateField[]) {
47 VMSTATE_FLOAT64_ARRAY(env.vfp.regs, ARMCPU, 64),
48 /* The xregs array is a little awkward because element 1 (FPSCR)
49 * requires a specific accessor, so we have to split it up in
50 * the vmstate:
52 VMSTATE_UINT32(env.vfp.xregs[0], ARMCPU),
53 VMSTATE_UINT32_SUB_ARRAY(env.vfp.xregs, ARMCPU, 2, 14),
55 .name = "fpscr",
56 .version_id = 0,
57 .size = sizeof(uint32_t),
58 .info = &vmstate_fpscr,
59 .flags = VMS_SINGLE,
60 .offset = 0,
62 VMSTATE_END_OF_LIST()
66 static bool iwmmxt_needed(void *opaque)
68 ARMCPU *cpu = opaque;
69 CPUARMState *env = &cpu->env;
71 return arm_feature(env, ARM_FEATURE_IWMMXT);
74 static const VMStateDescription vmstate_iwmmxt = {
75 .name = "cpu/iwmmxt",
76 .version_id = 1,
77 .minimum_version_id = 1,
78 .needed = iwmmxt_needed,
79 .fields = (VMStateField[]) {
80 VMSTATE_UINT64_ARRAY(env.iwmmxt.regs, ARMCPU, 16),
81 VMSTATE_UINT32_ARRAY(env.iwmmxt.cregs, ARMCPU, 16),
82 VMSTATE_END_OF_LIST()
86 static bool m_needed(void *opaque)
88 ARMCPU *cpu = opaque;
89 CPUARMState *env = &cpu->env;
91 return arm_feature(env, ARM_FEATURE_M);
94 static const VMStateDescription vmstate_m = {
95 .name = "cpu/m",
96 .version_id = 1,
97 .minimum_version_id = 1,
98 .needed = m_needed,
99 .fields = (VMStateField[]) {
100 VMSTATE_UINT32(env.v7m.other_sp, ARMCPU),
101 VMSTATE_UINT32(env.v7m.vecbase, ARMCPU),
102 VMSTATE_UINT32(env.v7m.basepri, ARMCPU),
103 VMSTATE_UINT32(env.v7m.control, ARMCPU),
104 VMSTATE_INT32(env.v7m.current_sp, ARMCPU),
105 VMSTATE_INT32(env.v7m.exception, ARMCPU),
106 VMSTATE_END_OF_LIST()
110 static bool thumb2ee_needed(void *opaque)
112 ARMCPU *cpu = opaque;
113 CPUARMState *env = &cpu->env;
115 return arm_feature(env, ARM_FEATURE_THUMB2EE);
118 static const VMStateDescription vmstate_thumb2ee = {
119 .name = "cpu/thumb2ee",
120 .version_id = 1,
121 .minimum_version_id = 1,
122 .needed = thumb2ee_needed,
123 .fields = (VMStateField[]) {
124 VMSTATE_UINT32(env.teecr, ARMCPU),
125 VMSTATE_UINT32(env.teehbr, ARMCPU),
126 VMSTATE_END_OF_LIST()
130 static bool pmsav7_needed(void *opaque)
132 ARMCPU *cpu = opaque;
133 CPUARMState *env = &cpu->env;
135 return arm_feature(env, ARM_FEATURE_MPU) &&
136 arm_feature(env, ARM_FEATURE_V7);
139 static bool pmsav7_rgnr_vmstate_validate(void *opaque, int version_id)
141 ARMCPU *cpu = opaque;
143 return cpu->env.cp15.c6_rgnr < cpu->pmsav7_dregion;
146 static const VMStateDescription vmstate_pmsav7 = {
147 .name = "cpu/pmsav7",
148 .version_id = 1,
149 .minimum_version_id = 1,
150 .needed = pmsav7_needed,
151 .fields = (VMStateField[]) {
152 VMSTATE_VARRAY_UINT32(env.pmsav7.drbar, ARMCPU, pmsav7_dregion, 0,
153 vmstate_info_uint32, uint32_t),
154 VMSTATE_VARRAY_UINT32(env.pmsav7.drsr, ARMCPU, pmsav7_dregion, 0,
155 vmstate_info_uint32, uint32_t),
156 VMSTATE_VARRAY_UINT32(env.pmsav7.dracr, ARMCPU, pmsav7_dregion, 0,
157 vmstate_info_uint32, uint32_t),
158 VMSTATE_VALIDATE("rgnr is valid", pmsav7_rgnr_vmstate_validate),
159 VMSTATE_END_OF_LIST()
163 static int get_cpsr(QEMUFile *f, void *opaque, size_t size)
165 ARMCPU *cpu = opaque;
166 CPUARMState *env = &cpu->env;
167 uint32_t val = qemu_get_be32(f);
169 env->aarch64 = ((val & PSTATE_nRW) == 0);
171 if (is_a64(env)) {
172 pstate_write(env, val);
173 return 0;
176 /* Avoid mode switch when restoring CPSR */
177 env->uncached_cpsr = val & CPSR_M;
178 cpsr_write(env, val, 0xffffffff);
179 return 0;
182 static void put_cpsr(QEMUFile *f, void *opaque, size_t size)
184 ARMCPU *cpu = opaque;
185 CPUARMState *env = &cpu->env;
186 uint32_t val;
188 if (is_a64(env)) {
189 val = pstate_read(env);
190 } else {
191 val = cpsr_read(env);
194 qemu_put_be32(f, val);
197 static const VMStateInfo vmstate_cpsr = {
198 .name = "cpsr",
199 .get = get_cpsr,
200 .put = put_cpsr,
203 static void cpu_pre_save(void *opaque)
205 ARMCPU *cpu = opaque;
207 if (kvm_enabled()) {
208 if (!write_kvmstate_to_list(cpu)) {
209 /* This should never fail */
210 abort();
212 } else {
213 if (!write_cpustate_to_list(cpu)) {
214 /* This should never fail. */
215 abort();
219 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
220 memcpy(cpu->cpreg_vmstate_indexes, cpu->cpreg_indexes,
221 cpu->cpreg_array_len * sizeof(uint64_t));
222 memcpy(cpu->cpreg_vmstate_values, cpu->cpreg_values,
223 cpu->cpreg_array_len * sizeof(uint64_t));
226 static int cpu_post_load(void *opaque, int version_id)
228 ARMCPU *cpu = opaque;
229 int i, v;
231 /* Update the values list from the incoming migration data.
232 * Anything in the incoming data which we don't know about is
233 * a migration failure; anything we know about but the incoming
234 * data doesn't specify retains its current (reset) value.
235 * The indexes list remains untouched -- we only inspect the
236 * incoming migration index list so we can match the values array
237 * entries with the right slots in our own values array.
240 for (i = 0, v = 0; i < cpu->cpreg_array_len
241 && v < cpu->cpreg_vmstate_array_len; i++) {
242 if (cpu->cpreg_vmstate_indexes[v] > cpu->cpreg_indexes[i]) {
243 /* register in our list but not incoming : skip it */
244 continue;
246 if (cpu->cpreg_vmstate_indexes[v] < cpu->cpreg_indexes[i]) {
247 /* register in their list but not ours: fail migration */
248 return -1;
250 /* matching register, copy the value over */
251 cpu->cpreg_values[i] = cpu->cpreg_vmstate_values[v];
252 v++;
255 if (kvm_enabled()) {
256 if (!write_list_to_kvmstate(cpu, KVM_PUT_FULL_STATE)) {
257 return -1;
259 /* Note that it's OK for the TCG side not to know about
260 * every register in the list; KVM is authoritative if
261 * we're using it.
263 write_list_to_cpustate(cpu);
264 } else {
265 if (!write_list_to_cpustate(cpu)) {
266 return -1;
270 hw_breakpoint_update_all(cpu);
271 hw_watchpoint_update_all(cpu);
273 return 0;
276 const VMStateDescription vmstate_arm_cpu = {
277 .name = "cpu",
278 .version_id = 22,
279 .minimum_version_id = 22,
280 .pre_save = cpu_pre_save,
281 .post_load = cpu_post_load,
282 .fields = (VMStateField[]) {
283 VMSTATE_UINT32_ARRAY(env.regs, ARMCPU, 16),
284 VMSTATE_UINT64_ARRAY(env.xregs, ARMCPU, 32),
285 VMSTATE_UINT64(env.pc, ARMCPU),
287 .name = "cpsr",
288 .version_id = 0,
289 .size = sizeof(uint32_t),
290 .info = &vmstate_cpsr,
291 .flags = VMS_SINGLE,
292 .offset = 0,
294 VMSTATE_UINT32(env.spsr, ARMCPU),
295 VMSTATE_UINT64_ARRAY(env.banked_spsr, ARMCPU, 8),
296 VMSTATE_UINT32_ARRAY(env.banked_r13, ARMCPU, 8),
297 VMSTATE_UINT32_ARRAY(env.banked_r14, ARMCPU, 8),
298 VMSTATE_UINT32_ARRAY(env.usr_regs, ARMCPU, 5),
299 VMSTATE_UINT32_ARRAY(env.fiq_regs, ARMCPU, 5),
300 VMSTATE_UINT64_ARRAY(env.elr_el, ARMCPU, 4),
301 VMSTATE_UINT64_ARRAY(env.sp_el, ARMCPU, 4),
302 /* The length-check must come before the arrays to avoid
303 * incoming data possibly overflowing the array.
305 VMSTATE_INT32_POSITIVE_LE(cpreg_vmstate_array_len, ARMCPU),
306 VMSTATE_VARRAY_INT32(cpreg_vmstate_indexes, ARMCPU,
307 cpreg_vmstate_array_len,
308 0, vmstate_info_uint64, uint64_t),
309 VMSTATE_VARRAY_INT32(cpreg_vmstate_values, ARMCPU,
310 cpreg_vmstate_array_len,
311 0, vmstate_info_uint64, uint64_t),
312 VMSTATE_UINT64(env.exclusive_addr, ARMCPU),
313 VMSTATE_UINT64(env.exclusive_val, ARMCPU),
314 VMSTATE_UINT64(env.exclusive_high, ARMCPU),
315 VMSTATE_UINT64(env.features, ARMCPU),
316 VMSTATE_UINT32(env.exception.syndrome, ARMCPU),
317 VMSTATE_UINT32(env.exception.fsr, ARMCPU),
318 VMSTATE_UINT64(env.exception.vaddress, ARMCPU),
319 VMSTATE_TIMER_PTR(gt_timer[GTIMER_PHYS], ARMCPU),
320 VMSTATE_TIMER_PTR(gt_timer[GTIMER_VIRT], ARMCPU),
321 VMSTATE_BOOL(powered_off, ARMCPU),
322 VMSTATE_END_OF_LIST()
324 .subsections = (const VMStateDescription*[]) {
325 &vmstate_vfp,
326 &vmstate_iwmmxt,
327 &vmstate_m,
328 &vmstate_thumb2ee,
329 &vmstate_pmsav7,
330 NULL
334 const char *gicv3_class_name(void)
336 if (kvm_irqchip_in_kernel()) {
337 #ifdef TARGET_AARCH64
338 return "kvm-arm-gicv3";
339 #else
340 error_report("KVM GICv3 acceleration is not supported on this "
341 "platform");
342 #endif
343 } else {
344 /* TODO: Software emulation is not implemented yet */
345 error_report("KVM is currently required for GICv3 emulation");
348 exit(1);