PPC: Fix rldcl
[qemu/agraf.git] / target-i386 / machine.c
blob3659db9e94431ae5c7aa428a121fb245eb3886ab
1 #include "hw/hw.h"
2 #include "hw/boards.h"
3 #include "hw/i386/pc.h"
4 #include "hw/isa/isa.h"
6 #include "cpu.h"
7 #include "sysemu/kvm.h"
9 static const VMStateDescription vmstate_segment = {
10 .name = "segment",
11 .version_id = 1,
12 .minimum_version_id = 1,
13 .minimum_version_id_old = 1,
14 .fields = (VMStateField []) {
15 VMSTATE_UINT32(selector, SegmentCache),
16 VMSTATE_UINTTL(base, SegmentCache),
17 VMSTATE_UINT32(limit, SegmentCache),
18 VMSTATE_UINT32(flags, SegmentCache),
19 VMSTATE_END_OF_LIST()
23 #define VMSTATE_SEGMENT(_field, _state) { \
24 .name = (stringify(_field)), \
25 .size = sizeof(SegmentCache), \
26 .vmsd = &vmstate_segment, \
27 .flags = VMS_STRUCT, \
28 .offset = offsetof(_state, _field) \
29 + type_check(SegmentCache,typeof_field(_state, _field)) \
32 #define VMSTATE_SEGMENT_ARRAY(_field, _state, _n) \
33 VMSTATE_STRUCT_ARRAY(_field, _state, _n, 0, vmstate_segment, SegmentCache)
35 static const VMStateDescription vmstate_xmm_reg = {
36 .name = "xmm_reg",
37 .version_id = 1,
38 .minimum_version_id = 1,
39 .minimum_version_id_old = 1,
40 .fields = (VMStateField []) {
41 VMSTATE_UINT64(XMM_Q(0), XMMReg),
42 VMSTATE_UINT64(XMM_Q(1), XMMReg),
43 VMSTATE_END_OF_LIST()
47 #define VMSTATE_XMM_REGS(_field, _state, _n) \
48 VMSTATE_STRUCT_ARRAY(_field, _state, _n, 0, vmstate_xmm_reg, XMMReg)
50 /* YMMH format is the same as XMM */
51 static const VMStateDescription vmstate_ymmh_reg = {
52 .name = "ymmh_reg",
53 .version_id = 1,
54 .minimum_version_id = 1,
55 .minimum_version_id_old = 1,
56 .fields = (VMStateField []) {
57 VMSTATE_UINT64(XMM_Q(0), XMMReg),
58 VMSTATE_UINT64(XMM_Q(1), XMMReg),
59 VMSTATE_END_OF_LIST()
63 #define VMSTATE_YMMH_REGS_VARS(_field, _state, _n, _v) \
64 VMSTATE_STRUCT_ARRAY(_field, _state, _n, _v, vmstate_ymmh_reg, XMMReg)
66 static const VMStateDescription vmstate_mtrr_var = {
67 .name = "mtrr_var",
68 .version_id = 1,
69 .minimum_version_id = 1,
70 .minimum_version_id_old = 1,
71 .fields = (VMStateField []) {
72 VMSTATE_UINT64(base, MTRRVar),
73 VMSTATE_UINT64(mask, MTRRVar),
74 VMSTATE_END_OF_LIST()
78 #define VMSTATE_MTRR_VARS(_field, _state, _n, _v) \
79 VMSTATE_STRUCT_ARRAY(_field, _state, _n, _v, vmstate_mtrr_var, MTRRVar)
81 static void put_fpreg_error(QEMUFile *f, void *opaque, size_t size)
83 fprintf(stderr, "call put_fpreg() with invalid arguments\n");
84 exit(0);
87 /* XXX: add that in a FPU generic layer */
88 union x86_longdouble {
89 uint64_t mant;
90 uint16_t exp;
93 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
94 #define EXPBIAS1 1023
95 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
96 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
98 static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
100 int e;
101 /* mantissa */
102 p->mant = (MANTD1(temp) << 11) | (1LL << 63);
103 /* exponent + sign */
104 e = EXPD1(temp) - EXPBIAS1 + 16383;
105 e |= SIGND1(temp) >> 16;
106 p->exp = e;
109 static int get_fpreg(QEMUFile *f, void *opaque, size_t size)
111 FPReg *fp_reg = opaque;
112 uint64_t mant;
113 uint16_t exp;
115 qemu_get_be64s(f, &mant);
116 qemu_get_be16s(f, &exp);
117 fp_reg->d = cpu_set_fp80(mant, exp);
118 return 0;
121 static void put_fpreg(QEMUFile *f, void *opaque, size_t size)
123 FPReg *fp_reg = opaque;
124 uint64_t mant;
125 uint16_t exp;
126 /* we save the real CPU data (in case of MMX usage only 'mant'
127 contains the MMX register */
128 cpu_get_fp80(&mant, &exp, fp_reg->d);
129 qemu_put_be64s(f, &mant);
130 qemu_put_be16s(f, &exp);
133 static const VMStateInfo vmstate_fpreg = {
134 .name = "fpreg",
135 .get = get_fpreg,
136 .put = put_fpreg,
139 static int get_fpreg_1_mmx(QEMUFile *f, void *opaque, size_t size)
141 union x86_longdouble *p = opaque;
142 uint64_t mant;
144 qemu_get_be64s(f, &mant);
145 p->mant = mant;
146 p->exp = 0xffff;
147 return 0;
150 static const VMStateInfo vmstate_fpreg_1_mmx = {
151 .name = "fpreg_1_mmx",
152 .get = get_fpreg_1_mmx,
153 .put = put_fpreg_error,
156 static int get_fpreg_1_no_mmx(QEMUFile *f, void *opaque, size_t size)
158 union x86_longdouble *p = opaque;
159 uint64_t mant;
161 qemu_get_be64s(f, &mant);
162 fp64_to_fp80(p, mant);
163 return 0;
166 static const VMStateInfo vmstate_fpreg_1_no_mmx = {
167 .name = "fpreg_1_no_mmx",
168 .get = get_fpreg_1_no_mmx,
169 .put = put_fpreg_error,
172 static bool fpregs_is_0(void *opaque, int version_id)
174 X86CPU *cpu = opaque;
175 CPUX86State *env = &cpu->env;
177 return (env->fpregs_format_vmstate == 0);
180 static bool fpregs_is_1_mmx(void *opaque, int version_id)
182 X86CPU *cpu = opaque;
183 CPUX86State *env = &cpu->env;
184 int guess_mmx;
186 guess_mmx = ((env->fptag_vmstate == 0xff) &&
187 (env->fpus_vmstate & 0x3800) == 0);
188 return (guess_mmx && (env->fpregs_format_vmstate == 1));
191 static bool fpregs_is_1_no_mmx(void *opaque, int version_id)
193 X86CPU *cpu = opaque;
194 CPUX86State *env = &cpu->env;
195 int guess_mmx;
197 guess_mmx = ((env->fptag_vmstate == 0xff) &&
198 (env->fpus_vmstate & 0x3800) == 0);
199 return (!guess_mmx && (env->fpregs_format_vmstate == 1));
202 #define VMSTATE_FP_REGS(_field, _state, _n) \
203 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_0, vmstate_fpreg, FPReg), \
204 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_1_mmx, vmstate_fpreg_1_mmx, FPReg), \
205 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_1_no_mmx, vmstate_fpreg_1_no_mmx, FPReg)
207 static bool version_is_5(void *opaque, int version_id)
209 return version_id == 5;
212 #ifdef TARGET_X86_64
213 static bool less_than_7(void *opaque, int version_id)
215 return version_id < 7;
218 static int get_uint64_as_uint32(QEMUFile *f, void *pv, size_t size)
220 uint64_t *v = pv;
221 *v = qemu_get_be32(f);
222 return 0;
225 static void put_uint64_as_uint32(QEMUFile *f, void *pv, size_t size)
227 uint64_t *v = pv;
228 qemu_put_be32(f, *v);
231 static const VMStateInfo vmstate_hack_uint64_as_uint32 = {
232 .name = "uint64_as_uint32",
233 .get = get_uint64_as_uint32,
234 .put = put_uint64_as_uint32,
237 #define VMSTATE_HACK_UINT32(_f, _s, _t) \
238 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint64_as_uint32, uint64_t)
239 #endif
241 static void cpu_pre_save(void *opaque)
243 X86CPU *cpu = opaque;
244 CPUX86State *env = &cpu->env;
245 int i;
247 /* FPU */
248 env->fpus_vmstate = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
249 env->fptag_vmstate = 0;
250 for(i = 0; i < 8; i++) {
251 env->fptag_vmstate |= ((!env->fptags[i]) << i);
254 env->fpregs_format_vmstate = 0;
257 static int cpu_post_load(void *opaque, int version_id)
259 X86CPU *cpu = opaque;
260 CPUX86State *env = &cpu->env;
261 int i;
263 /* XXX: restore FPU round state */
264 env->fpstt = (env->fpus_vmstate >> 11) & 7;
265 env->fpus = env->fpus_vmstate & ~0x3800;
266 env->fptag_vmstate ^= 0xff;
267 for(i = 0; i < 8; i++) {
268 env->fptags[i] = (env->fptag_vmstate >> i) & 1;
271 cpu_breakpoint_remove_all(env, BP_CPU);
272 cpu_watchpoint_remove_all(env, BP_CPU);
273 for (i = 0; i < DR7_MAX_BP; i++) {
274 hw_breakpoint_insert(env, i);
276 tlb_flush(env, 1);
278 return 0;
281 static bool async_pf_msr_needed(void *opaque)
283 X86CPU *cpu = opaque;
285 return cpu->env.async_pf_en_msr != 0;
288 static bool pv_eoi_msr_needed(void *opaque)
290 X86CPU *cpu = opaque;
292 return cpu->env.pv_eoi_en_msr != 0;
295 static bool steal_time_msr_needed(void *opaque)
297 CPUX86State *cpu = opaque;
299 return cpu->steal_time_msr != 0;
302 static const VMStateDescription vmstate_steal_time_msr = {
303 .name = "cpu/steal_time_msr",
304 .version_id = 1,
305 .minimum_version_id = 1,
306 .minimum_version_id_old = 1,
307 .fields = (VMStateField []) {
308 VMSTATE_UINT64(steal_time_msr, CPUX86State),
309 VMSTATE_END_OF_LIST()
313 static const VMStateDescription vmstate_async_pf_msr = {
314 .name = "cpu/async_pf_msr",
315 .version_id = 1,
316 .minimum_version_id = 1,
317 .minimum_version_id_old = 1,
318 .fields = (VMStateField []) {
319 VMSTATE_UINT64(env.async_pf_en_msr, X86CPU),
320 VMSTATE_END_OF_LIST()
324 static const VMStateDescription vmstate_pv_eoi_msr = {
325 .name = "cpu/async_pv_eoi_msr",
326 .version_id = 1,
327 .minimum_version_id = 1,
328 .minimum_version_id_old = 1,
329 .fields = (VMStateField []) {
330 VMSTATE_UINT64(env.pv_eoi_en_msr, X86CPU),
331 VMSTATE_END_OF_LIST()
335 static bool fpop_ip_dp_needed(void *opaque)
337 X86CPU *cpu = opaque;
338 CPUX86State *env = &cpu->env;
340 return env->fpop != 0 || env->fpip != 0 || env->fpdp != 0;
343 static const VMStateDescription vmstate_fpop_ip_dp = {
344 .name = "cpu/fpop_ip_dp",
345 .version_id = 1,
346 .minimum_version_id = 1,
347 .minimum_version_id_old = 1,
348 .fields = (VMStateField []) {
349 VMSTATE_UINT16(env.fpop, X86CPU),
350 VMSTATE_UINT64(env.fpip, X86CPU),
351 VMSTATE_UINT64(env.fpdp, X86CPU),
352 VMSTATE_END_OF_LIST()
356 static bool tsc_adjust_needed(void *opaque)
358 X86CPU *cpu = opaque;
359 CPUX86State *env = &cpu->env;
361 return env->tsc_adjust != 0;
364 static const VMStateDescription vmstate_msr_tsc_adjust = {
365 .name = "cpu/msr_tsc_adjust",
366 .version_id = 1,
367 .minimum_version_id = 1,
368 .minimum_version_id_old = 1,
369 .fields = (VMStateField[]) {
370 VMSTATE_UINT64(env.tsc_adjust, X86CPU),
371 VMSTATE_END_OF_LIST()
375 static bool tscdeadline_needed(void *opaque)
377 X86CPU *cpu = opaque;
378 CPUX86State *env = &cpu->env;
380 return env->tsc_deadline != 0;
383 static const VMStateDescription vmstate_msr_tscdeadline = {
384 .name = "cpu/msr_tscdeadline",
385 .version_id = 1,
386 .minimum_version_id = 1,
387 .minimum_version_id_old = 1,
388 .fields = (VMStateField []) {
389 VMSTATE_UINT64(env.tsc_deadline, X86CPU),
390 VMSTATE_END_OF_LIST()
394 static bool misc_enable_needed(void *opaque)
396 X86CPU *cpu = opaque;
397 CPUX86State *env = &cpu->env;
399 return env->msr_ia32_misc_enable != MSR_IA32_MISC_ENABLE_DEFAULT;
402 static const VMStateDescription vmstate_msr_ia32_misc_enable = {
403 .name = "cpu/msr_ia32_misc_enable",
404 .version_id = 1,
405 .minimum_version_id = 1,
406 .minimum_version_id_old = 1,
407 .fields = (VMStateField []) {
408 VMSTATE_UINT64(env.msr_ia32_misc_enable, X86CPU),
409 VMSTATE_END_OF_LIST()
413 const VMStateDescription vmstate_x86_cpu = {
414 .name = "cpu",
415 .version_id = 12,
416 .minimum_version_id = 3,
417 .minimum_version_id_old = 3,
418 .pre_save = cpu_pre_save,
419 .post_load = cpu_post_load,
420 .fields = (VMStateField []) {
421 VMSTATE_UINTTL_ARRAY(env.regs, X86CPU, CPU_NB_REGS),
422 VMSTATE_UINTTL(env.eip, X86CPU),
423 VMSTATE_UINTTL(env.eflags, X86CPU),
424 VMSTATE_UINT32(env.hflags, X86CPU),
425 /* FPU */
426 VMSTATE_UINT16(env.fpuc, X86CPU),
427 VMSTATE_UINT16(env.fpus_vmstate, X86CPU),
428 VMSTATE_UINT16(env.fptag_vmstate, X86CPU),
429 VMSTATE_UINT16(env.fpregs_format_vmstate, X86CPU),
430 VMSTATE_FP_REGS(env.fpregs, X86CPU, 8),
432 VMSTATE_SEGMENT_ARRAY(env.segs, X86CPU, 6),
433 VMSTATE_SEGMENT(env.ldt, X86CPU),
434 VMSTATE_SEGMENT(env.tr, X86CPU),
435 VMSTATE_SEGMENT(env.gdt, X86CPU),
436 VMSTATE_SEGMENT(env.idt, X86CPU),
438 VMSTATE_UINT32(env.sysenter_cs, X86CPU),
439 #ifdef TARGET_X86_64
440 /* Hack: In v7 size changed from 32 to 64 bits on x86_64 */
441 VMSTATE_HACK_UINT32(env.sysenter_esp, X86CPU, less_than_7),
442 VMSTATE_HACK_UINT32(env.sysenter_eip, X86CPU, less_than_7),
443 VMSTATE_UINTTL_V(env.sysenter_esp, X86CPU, 7),
444 VMSTATE_UINTTL_V(env.sysenter_eip, X86CPU, 7),
445 #else
446 VMSTATE_UINTTL(env.sysenter_esp, X86CPU),
447 VMSTATE_UINTTL(env.sysenter_eip, X86CPU),
448 #endif
450 VMSTATE_UINTTL(env.cr[0], X86CPU),
451 VMSTATE_UINTTL(env.cr[2], X86CPU),
452 VMSTATE_UINTTL(env.cr[3], X86CPU),
453 VMSTATE_UINTTL(env.cr[4], X86CPU),
454 VMSTATE_UINTTL_ARRAY(env.dr, X86CPU, 8),
455 /* MMU */
456 VMSTATE_INT32(env.a20_mask, X86CPU),
457 /* XMM */
458 VMSTATE_UINT32(env.mxcsr, X86CPU),
459 VMSTATE_XMM_REGS(env.xmm_regs, X86CPU, CPU_NB_REGS),
461 #ifdef TARGET_X86_64
462 VMSTATE_UINT64(env.efer, X86CPU),
463 VMSTATE_UINT64(env.star, X86CPU),
464 VMSTATE_UINT64(env.lstar, X86CPU),
465 VMSTATE_UINT64(env.cstar, X86CPU),
466 VMSTATE_UINT64(env.fmask, X86CPU),
467 VMSTATE_UINT64(env.kernelgsbase, X86CPU),
468 #endif
469 VMSTATE_UINT32_V(env.smbase, X86CPU, 4),
471 VMSTATE_UINT64_V(env.pat, X86CPU, 5),
472 VMSTATE_UINT32_V(env.hflags2, X86CPU, 5),
474 VMSTATE_UINT32_TEST(parent_obj.halted, X86CPU, version_is_5),
475 VMSTATE_UINT64_V(env.vm_hsave, X86CPU, 5),
476 VMSTATE_UINT64_V(env.vm_vmcb, X86CPU, 5),
477 VMSTATE_UINT64_V(env.tsc_offset, X86CPU, 5),
478 VMSTATE_UINT64_V(env.intercept, X86CPU, 5),
479 VMSTATE_UINT16_V(env.intercept_cr_read, X86CPU, 5),
480 VMSTATE_UINT16_V(env.intercept_cr_write, X86CPU, 5),
481 VMSTATE_UINT16_V(env.intercept_dr_read, X86CPU, 5),
482 VMSTATE_UINT16_V(env.intercept_dr_write, X86CPU, 5),
483 VMSTATE_UINT32_V(env.intercept_exceptions, X86CPU, 5),
484 VMSTATE_UINT8_V(env.v_tpr, X86CPU, 5),
485 /* MTRRs */
486 VMSTATE_UINT64_ARRAY_V(env.mtrr_fixed, X86CPU, 11, 8),
487 VMSTATE_UINT64_V(env.mtrr_deftype, X86CPU, 8),
488 VMSTATE_MTRR_VARS(env.mtrr_var, X86CPU, 8, 8),
489 /* KVM-related states */
490 VMSTATE_INT32_V(env.interrupt_injected, X86CPU, 9),
491 VMSTATE_UINT32_V(env.mp_state, X86CPU, 9),
492 VMSTATE_UINT64_V(env.tsc, X86CPU, 9),
493 VMSTATE_INT32_V(env.exception_injected, X86CPU, 11),
494 VMSTATE_UINT8_V(env.soft_interrupt, X86CPU, 11),
495 VMSTATE_UINT8_V(env.nmi_injected, X86CPU, 11),
496 VMSTATE_UINT8_V(env.nmi_pending, X86CPU, 11),
497 VMSTATE_UINT8_V(env.has_error_code, X86CPU, 11),
498 VMSTATE_UINT32_V(env.sipi_vector, X86CPU, 11),
499 /* MCE */
500 VMSTATE_UINT64_V(env.mcg_cap, X86CPU, 10),
501 VMSTATE_UINT64_V(env.mcg_status, X86CPU, 10),
502 VMSTATE_UINT64_V(env.mcg_ctl, X86CPU, 10),
503 VMSTATE_UINT64_ARRAY_V(env.mce_banks, X86CPU, MCE_BANKS_DEF * 4, 10),
504 /* rdtscp */
505 VMSTATE_UINT64_V(env.tsc_aux, X86CPU, 11),
506 /* KVM pvclock msr */
507 VMSTATE_UINT64_V(env.system_time_msr, X86CPU, 11),
508 VMSTATE_UINT64_V(env.wall_clock_msr, X86CPU, 11),
509 /* XSAVE related fields */
510 VMSTATE_UINT64_V(env.xcr0, X86CPU, 12),
511 VMSTATE_UINT64_V(env.xstate_bv, X86CPU, 12),
512 VMSTATE_YMMH_REGS_VARS(env.ymmh_regs, X86CPU, CPU_NB_REGS, 12),
513 VMSTATE_END_OF_LIST()
514 /* The above list is not sorted /wrt version numbers, watch out! */
516 .subsections = (VMStateSubsection []) {
518 .vmsd = &vmstate_async_pf_msr,
519 .needed = async_pf_msr_needed,
520 } , {
521 .vmsd = &vmstate_pv_eoi_msr,
522 .needed = pv_eoi_msr_needed,
523 } , {
524 .vmsd = &vmstate_steal_time_msr,
525 .needed = steal_time_msr_needed,
526 } , {
527 .vmsd = &vmstate_fpop_ip_dp,
528 .needed = fpop_ip_dp_needed,
529 }, {
530 .vmsd = &vmstate_msr_tsc_adjust,
531 .needed = tsc_adjust_needed,
532 }, {
533 .vmsd = &vmstate_msr_tscdeadline,
534 .needed = tscdeadline_needed,
535 }, {
536 .vmsd = &vmstate_msr_ia32_misc_enable,
537 .needed = misc_enable_needed,
538 } , {
539 /* empty */