target-i386: Enabling IA32_TSC_ADJUST for QEMU KVM guest VMs
[qemu/rayw.git] / target-i386 / machine.c
blob4229ddeac89bdfc520d9909d1872666c4ec5684d
1 #include "hw/hw.h"
2 #include "hw/boards.h"
3 #include "hw/pc.h"
4 #include "hw/isa.h"
6 #include "cpu.h"
7 #include "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 CPUX86State *env = opaque;
176 return (env->fpregs_format_vmstate == 0);
179 static bool fpregs_is_1_mmx(void *opaque, int version_id)
181 CPUX86State *env = opaque;
182 int guess_mmx;
184 guess_mmx = ((env->fptag_vmstate == 0xff) &&
185 (env->fpus_vmstate & 0x3800) == 0);
186 return (guess_mmx && (env->fpregs_format_vmstate == 1));
189 static bool fpregs_is_1_no_mmx(void *opaque, int version_id)
191 CPUX86State *env = opaque;
192 int guess_mmx;
194 guess_mmx = ((env->fptag_vmstate == 0xff) &&
195 (env->fpus_vmstate & 0x3800) == 0);
196 return (!guess_mmx && (env->fpregs_format_vmstate == 1));
199 #define VMSTATE_FP_REGS(_field, _state, _n) \
200 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_0, vmstate_fpreg, FPReg), \
201 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_1_mmx, vmstate_fpreg_1_mmx, FPReg), \
202 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_1_no_mmx, vmstate_fpreg_1_no_mmx, FPReg)
204 static bool version_is_5(void *opaque, int version_id)
206 return version_id == 5;
209 #ifdef TARGET_X86_64
210 static bool less_than_7(void *opaque, int version_id)
212 return version_id < 7;
215 static int get_uint64_as_uint32(QEMUFile *f, void *pv, size_t size)
217 uint64_t *v = pv;
218 *v = qemu_get_be32(f);
219 return 0;
222 static void put_uint64_as_uint32(QEMUFile *f, void *pv, size_t size)
224 uint64_t *v = pv;
225 qemu_put_be32(f, *v);
228 static const VMStateInfo vmstate_hack_uint64_as_uint32 = {
229 .name = "uint64_as_uint32",
230 .get = get_uint64_as_uint32,
231 .put = put_uint64_as_uint32,
234 #define VMSTATE_HACK_UINT32(_f, _s, _t) \
235 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint64_as_uint32, uint64_t)
236 #endif
238 static void cpu_pre_save(void *opaque)
240 CPUX86State *env = opaque;
241 int i;
243 /* FPU */
244 env->fpus_vmstate = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
245 env->fptag_vmstate = 0;
246 for(i = 0; i < 8; i++) {
247 env->fptag_vmstate |= ((!env->fptags[i]) << i);
250 env->fpregs_format_vmstate = 0;
253 static int cpu_post_load(void *opaque, int version_id)
255 CPUX86State *env = opaque;
256 int i;
258 /* XXX: restore FPU round state */
259 env->fpstt = (env->fpus_vmstate >> 11) & 7;
260 env->fpus = env->fpus_vmstate & ~0x3800;
261 env->fptag_vmstate ^= 0xff;
262 for(i = 0; i < 8; i++) {
263 env->fptags[i] = (env->fptag_vmstate >> i) & 1;
266 cpu_breakpoint_remove_all(env, BP_CPU);
267 cpu_watchpoint_remove_all(env, BP_CPU);
268 for (i = 0; i < 4; i++)
269 hw_breakpoint_insert(env, i);
271 tlb_flush(env, 1);
272 return 0;
275 static bool async_pf_msr_needed(void *opaque)
277 CPUX86State *cpu = opaque;
279 return cpu->async_pf_en_msr != 0;
282 static bool pv_eoi_msr_needed(void *opaque)
284 CPUX86State *cpu = opaque;
286 return cpu->pv_eoi_en_msr != 0;
289 static const VMStateDescription vmstate_async_pf_msr = {
290 .name = "cpu/async_pf_msr",
291 .version_id = 1,
292 .minimum_version_id = 1,
293 .minimum_version_id_old = 1,
294 .fields = (VMStateField []) {
295 VMSTATE_UINT64(async_pf_en_msr, CPUX86State),
296 VMSTATE_END_OF_LIST()
300 static const VMStateDescription vmstate_pv_eoi_msr = {
301 .name = "cpu/async_pv_eoi_msr",
302 .version_id = 1,
303 .minimum_version_id = 1,
304 .minimum_version_id_old = 1,
305 .fields = (VMStateField []) {
306 VMSTATE_UINT64(pv_eoi_en_msr, CPUX86State),
307 VMSTATE_END_OF_LIST()
311 static bool fpop_ip_dp_needed(void *opaque)
313 CPUX86State *env = opaque;
315 return env->fpop != 0 || env->fpip != 0 || env->fpdp != 0;
318 static const VMStateDescription vmstate_fpop_ip_dp = {
319 .name = "cpu/fpop_ip_dp",
320 .version_id = 1,
321 .minimum_version_id = 1,
322 .minimum_version_id_old = 1,
323 .fields = (VMStateField []) {
324 VMSTATE_UINT16(fpop, CPUX86State),
325 VMSTATE_UINT64(fpip, CPUX86State),
326 VMSTATE_UINT64(fpdp, CPUX86State),
327 VMSTATE_END_OF_LIST()
331 static bool tsc_adjust_needed(void *opaque)
333 CPUX86State *env = opaque;
335 return env->tsc_adjust != 0;
338 static const VMStateDescription vmstate_msr_tsc_adjust = {
339 .name = "cpu/msr_tsc_adjust",
340 .version_id = 1,
341 .minimum_version_id = 1,
342 .minimum_version_id_old = 1,
343 .fields = (VMStateField[]) {
344 VMSTATE_UINT64(tsc_adjust, CPUX86State),
345 VMSTATE_END_OF_LIST()
349 static bool tscdeadline_needed(void *opaque)
351 CPUX86State *env = opaque;
353 return env->tsc_deadline != 0;
356 static const VMStateDescription vmstate_msr_tscdeadline = {
357 .name = "cpu/msr_tscdeadline",
358 .version_id = 1,
359 .minimum_version_id = 1,
360 .minimum_version_id_old = 1,
361 .fields = (VMStateField []) {
362 VMSTATE_UINT64(tsc_deadline, CPUX86State),
363 VMSTATE_END_OF_LIST()
367 static bool misc_enable_needed(void *opaque)
369 CPUX86State *env = opaque;
371 return env->msr_ia32_misc_enable != MSR_IA32_MISC_ENABLE_DEFAULT;
374 static const VMStateDescription vmstate_msr_ia32_misc_enable = {
375 .name = "cpu/msr_ia32_misc_enable",
376 .version_id = 1,
377 .minimum_version_id = 1,
378 .minimum_version_id_old = 1,
379 .fields = (VMStateField []) {
380 VMSTATE_UINT64(msr_ia32_misc_enable, CPUX86State),
381 VMSTATE_END_OF_LIST()
385 static const VMStateDescription vmstate_cpu = {
386 .name = "cpu",
387 .version_id = CPU_SAVE_VERSION,
388 .minimum_version_id = 3,
389 .minimum_version_id_old = 3,
390 .pre_save = cpu_pre_save,
391 .post_load = cpu_post_load,
392 .fields = (VMStateField []) {
393 VMSTATE_UINTTL_ARRAY(regs, CPUX86State, CPU_NB_REGS),
394 VMSTATE_UINTTL(eip, CPUX86State),
395 VMSTATE_UINTTL(eflags, CPUX86State),
396 VMSTATE_UINT32(hflags, CPUX86State),
397 /* FPU */
398 VMSTATE_UINT16(fpuc, CPUX86State),
399 VMSTATE_UINT16(fpus_vmstate, CPUX86State),
400 VMSTATE_UINT16(fptag_vmstate, CPUX86State),
401 VMSTATE_UINT16(fpregs_format_vmstate, CPUX86State),
402 VMSTATE_FP_REGS(fpregs, CPUX86State, 8),
404 VMSTATE_SEGMENT_ARRAY(segs, CPUX86State, 6),
405 VMSTATE_SEGMENT(ldt, CPUX86State),
406 VMSTATE_SEGMENT(tr, CPUX86State),
407 VMSTATE_SEGMENT(gdt, CPUX86State),
408 VMSTATE_SEGMENT(idt, CPUX86State),
410 VMSTATE_UINT32(sysenter_cs, CPUX86State),
411 #ifdef TARGET_X86_64
412 /* Hack: In v7 size changed from 32 to 64 bits on x86_64 */
413 VMSTATE_HACK_UINT32(sysenter_esp, CPUX86State, less_than_7),
414 VMSTATE_HACK_UINT32(sysenter_eip, CPUX86State, less_than_7),
415 VMSTATE_UINTTL_V(sysenter_esp, CPUX86State, 7),
416 VMSTATE_UINTTL_V(sysenter_eip, CPUX86State, 7),
417 #else
418 VMSTATE_UINTTL(sysenter_esp, CPUX86State),
419 VMSTATE_UINTTL(sysenter_eip, CPUX86State),
420 #endif
422 VMSTATE_UINTTL(cr[0], CPUX86State),
423 VMSTATE_UINTTL(cr[2], CPUX86State),
424 VMSTATE_UINTTL(cr[3], CPUX86State),
425 VMSTATE_UINTTL(cr[4], CPUX86State),
426 VMSTATE_UINTTL_ARRAY(dr, CPUX86State, 8),
427 /* MMU */
428 VMSTATE_INT32(a20_mask, CPUX86State),
429 /* XMM */
430 VMSTATE_UINT32(mxcsr, CPUX86State),
431 VMSTATE_XMM_REGS(xmm_regs, CPUX86State, CPU_NB_REGS),
433 #ifdef TARGET_X86_64
434 VMSTATE_UINT64(efer, CPUX86State),
435 VMSTATE_UINT64(star, CPUX86State),
436 VMSTATE_UINT64(lstar, CPUX86State),
437 VMSTATE_UINT64(cstar, CPUX86State),
438 VMSTATE_UINT64(fmask, CPUX86State),
439 VMSTATE_UINT64(kernelgsbase, CPUX86State),
440 #endif
441 VMSTATE_UINT32_V(smbase, CPUX86State, 4),
443 VMSTATE_UINT64_V(pat, CPUX86State, 5),
444 VMSTATE_UINT32_V(hflags2, CPUX86State, 5),
446 VMSTATE_UINT32_TEST(halted, CPUX86State, version_is_5),
447 VMSTATE_UINT64_V(vm_hsave, CPUX86State, 5),
448 VMSTATE_UINT64_V(vm_vmcb, CPUX86State, 5),
449 VMSTATE_UINT64_V(tsc_offset, CPUX86State, 5),
450 VMSTATE_UINT64_V(intercept, CPUX86State, 5),
451 VMSTATE_UINT16_V(intercept_cr_read, CPUX86State, 5),
452 VMSTATE_UINT16_V(intercept_cr_write, CPUX86State, 5),
453 VMSTATE_UINT16_V(intercept_dr_read, CPUX86State, 5),
454 VMSTATE_UINT16_V(intercept_dr_write, CPUX86State, 5),
455 VMSTATE_UINT32_V(intercept_exceptions, CPUX86State, 5),
456 VMSTATE_UINT8_V(v_tpr, CPUX86State, 5),
457 /* MTRRs */
458 VMSTATE_UINT64_ARRAY_V(mtrr_fixed, CPUX86State, 11, 8),
459 VMSTATE_UINT64_V(mtrr_deftype, CPUX86State, 8),
460 VMSTATE_MTRR_VARS(mtrr_var, CPUX86State, 8, 8),
461 /* KVM-related states */
462 VMSTATE_INT32_V(interrupt_injected, CPUX86State, 9),
463 VMSTATE_UINT32_V(mp_state, CPUX86State, 9),
464 VMSTATE_UINT64_V(tsc, CPUX86State, 9),
465 VMSTATE_INT32_V(exception_injected, CPUX86State, 11),
466 VMSTATE_UINT8_V(soft_interrupt, CPUX86State, 11),
467 VMSTATE_UINT8_V(nmi_injected, CPUX86State, 11),
468 VMSTATE_UINT8_V(nmi_pending, CPUX86State, 11),
469 VMSTATE_UINT8_V(has_error_code, CPUX86State, 11),
470 VMSTATE_UINT32_V(sipi_vector, CPUX86State, 11),
471 /* MCE */
472 VMSTATE_UINT64_V(mcg_cap, CPUX86State, 10),
473 VMSTATE_UINT64_V(mcg_status, CPUX86State, 10),
474 VMSTATE_UINT64_V(mcg_ctl, CPUX86State, 10),
475 VMSTATE_UINT64_ARRAY_V(mce_banks, CPUX86State, MCE_BANKS_DEF *4, 10),
476 /* rdtscp */
477 VMSTATE_UINT64_V(tsc_aux, CPUX86State, 11),
478 /* KVM pvclock msr */
479 VMSTATE_UINT64_V(system_time_msr, CPUX86State, 11),
480 VMSTATE_UINT64_V(wall_clock_msr, CPUX86State, 11),
481 /* XSAVE related fields */
482 VMSTATE_UINT64_V(xcr0, CPUX86State, 12),
483 VMSTATE_UINT64_V(xstate_bv, CPUX86State, 12),
484 VMSTATE_YMMH_REGS_VARS(ymmh_regs, CPUX86State, CPU_NB_REGS, 12),
485 VMSTATE_END_OF_LIST()
486 /* The above list is not sorted /wrt version numbers, watch out! */
488 .subsections = (VMStateSubsection []) {
490 .vmsd = &vmstate_async_pf_msr,
491 .needed = async_pf_msr_needed,
492 } , {
493 .vmsd = &vmstate_pv_eoi_msr,
494 .needed = pv_eoi_msr_needed,
495 } , {
496 .vmsd = &vmstate_fpop_ip_dp,
497 .needed = fpop_ip_dp_needed,
498 }, {
499 .vmsd = &vmstate_msr_tsc_adjust,
500 .needed = tsc_adjust_needed,
501 }, {
502 .vmsd = &vmstate_msr_tscdeadline,
503 .needed = tscdeadline_needed,
504 }, {
505 .vmsd = &vmstate_msr_ia32_misc_enable,
506 .needed = misc_enable_needed,
507 } , {
508 /* empty */
513 void cpu_save(QEMUFile *f, void *opaque)
515 vmstate_save_state(f, &vmstate_cpu, opaque);
518 int cpu_load(QEMUFile *f, void *opaque, int version_id)
520 return vmstate_load_state(f, &vmstate_cpu, opaque, version_id);