1 #include "qemu/osdep.h"
2 #include "qemu-common.h"
4 #include "exec/exec-all.h"
7 #include "hw/i386/pc.h"
8 #include "hw/isa/isa.h"
9 #include "migration/cpu.h"
11 #include "sysemu/kvm.h"
13 #include "qemu/error-report.h"
15 static const VMStateDescription vmstate_segment
= {
18 .minimum_version_id
= 1,
19 .fields
= (VMStateField
[]) {
20 VMSTATE_UINT32(selector
, SegmentCache
),
21 VMSTATE_UINTTL(base
, SegmentCache
),
22 VMSTATE_UINT32(limit
, SegmentCache
),
23 VMSTATE_UINT32(flags
, SegmentCache
),
28 #define VMSTATE_SEGMENT(_field, _state) { \
29 .name = (stringify(_field)), \
30 .size = sizeof(SegmentCache), \
31 .vmsd = &vmstate_segment, \
32 .flags = VMS_STRUCT, \
33 .offset = offsetof(_state, _field) \
34 + type_check(SegmentCache,typeof_field(_state, _field)) \
37 #define VMSTATE_SEGMENT_ARRAY(_field, _state, _n) \
38 VMSTATE_STRUCT_ARRAY(_field, _state, _n, 0, vmstate_segment, SegmentCache)
40 static const VMStateDescription vmstate_xmm_reg
= {
43 .minimum_version_id
= 1,
44 .fields
= (VMStateField
[]) {
45 VMSTATE_UINT64(ZMM_Q(0), ZMMReg
),
46 VMSTATE_UINT64(ZMM_Q(1), ZMMReg
),
51 #define VMSTATE_XMM_REGS(_field, _state, _start) \
52 VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, CPU_NB_REGS, 0, \
53 vmstate_xmm_reg, ZMMReg)
55 /* YMMH format is the same as XMM, but for bits 128-255 */
56 static const VMStateDescription vmstate_ymmh_reg
= {
59 .minimum_version_id
= 1,
60 .fields
= (VMStateField
[]) {
61 VMSTATE_UINT64(ZMM_Q(2), ZMMReg
),
62 VMSTATE_UINT64(ZMM_Q(3), ZMMReg
),
67 #define VMSTATE_YMMH_REGS_VARS(_field, _state, _start, _v) \
68 VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, CPU_NB_REGS, _v, \
69 vmstate_ymmh_reg, ZMMReg)
71 static const VMStateDescription vmstate_zmmh_reg
= {
74 .minimum_version_id
= 1,
75 .fields
= (VMStateField
[]) {
76 VMSTATE_UINT64(ZMM_Q(4), ZMMReg
),
77 VMSTATE_UINT64(ZMM_Q(5), ZMMReg
),
78 VMSTATE_UINT64(ZMM_Q(6), ZMMReg
),
79 VMSTATE_UINT64(ZMM_Q(7), ZMMReg
),
84 #define VMSTATE_ZMMH_REGS_VARS(_field, _state, _start) \
85 VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, CPU_NB_REGS, 0, \
86 vmstate_zmmh_reg, ZMMReg)
89 static const VMStateDescription vmstate_hi16_zmm_reg
= {
90 .name
= "hi16_zmm_reg",
92 .minimum_version_id
= 1,
93 .fields
= (VMStateField
[]) {
94 VMSTATE_UINT64(ZMM_Q(0), ZMMReg
),
95 VMSTATE_UINT64(ZMM_Q(1), ZMMReg
),
96 VMSTATE_UINT64(ZMM_Q(2), ZMMReg
),
97 VMSTATE_UINT64(ZMM_Q(3), ZMMReg
),
98 VMSTATE_UINT64(ZMM_Q(4), ZMMReg
),
99 VMSTATE_UINT64(ZMM_Q(5), ZMMReg
),
100 VMSTATE_UINT64(ZMM_Q(6), ZMMReg
),
101 VMSTATE_UINT64(ZMM_Q(7), ZMMReg
),
102 VMSTATE_END_OF_LIST()
106 #define VMSTATE_Hi16_ZMM_REGS_VARS(_field, _state, _start) \
107 VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, CPU_NB_REGS, 0, \
108 vmstate_hi16_zmm_reg, ZMMReg)
111 static const VMStateDescription vmstate_bnd_regs
= {
114 .minimum_version_id
= 1,
115 .fields
= (VMStateField
[]) {
116 VMSTATE_UINT64(lb
, BNDReg
),
117 VMSTATE_UINT64(ub
, BNDReg
),
118 VMSTATE_END_OF_LIST()
122 #define VMSTATE_BND_REGS(_field, _state, _n) \
123 VMSTATE_STRUCT_ARRAY(_field, _state, _n, 0, vmstate_bnd_regs, BNDReg)
125 static const VMStateDescription vmstate_mtrr_var
= {
128 .minimum_version_id
= 1,
129 .fields
= (VMStateField
[]) {
130 VMSTATE_UINT64(base
, MTRRVar
),
131 VMSTATE_UINT64(mask
, MTRRVar
),
132 VMSTATE_END_OF_LIST()
136 #define VMSTATE_MTRR_VARS(_field, _state, _n, _v) \
137 VMSTATE_STRUCT_ARRAY(_field, _state, _n, _v, vmstate_mtrr_var, MTRRVar)
139 typedef struct x86_FPReg_tmp
{
145 static void cpu_get_fp80(uint64_t *pmant
, uint16_t *pexp
, floatx80 f
)
150 *pmant
= temp
.l
.lower
;
151 *pexp
= temp
.l
.upper
;
154 static floatx80
cpu_set_fp80(uint64_t mant
, uint16_t upper
)
158 temp
.l
.upper
= upper
;
163 static int fpreg_pre_save(void *opaque
)
165 x86_FPReg_tmp
*tmp
= opaque
;
167 /* we save the real CPU data (in case of MMX usage only 'mant'
168 contains the MMX register */
169 cpu_get_fp80(&tmp
->tmp_mant
, &tmp
->tmp_exp
, tmp
->parent
->d
);
174 static int fpreg_post_load(void *opaque
, int version
)
176 x86_FPReg_tmp
*tmp
= opaque
;
178 tmp
->parent
->d
= cpu_set_fp80(tmp
->tmp_mant
, tmp
->tmp_exp
);
182 static const VMStateDescription vmstate_fpreg_tmp
= {
184 .post_load
= fpreg_post_load
,
185 .pre_save
= fpreg_pre_save
,
186 .fields
= (VMStateField
[]) {
187 VMSTATE_UINT64(tmp_mant
, x86_FPReg_tmp
),
188 VMSTATE_UINT16(tmp_exp
, x86_FPReg_tmp
),
189 VMSTATE_END_OF_LIST()
193 static const VMStateDescription vmstate_fpreg
= {
195 .fields
= (VMStateField
[]) {
196 VMSTATE_WITH_TMP(FPReg
, x86_FPReg_tmp
, vmstate_fpreg_tmp
),
197 VMSTATE_END_OF_LIST()
201 static int cpu_pre_save(void *opaque
)
203 X86CPU
*cpu
= opaque
;
204 CPUX86State
*env
= &cpu
->env
;
208 env
->fpus_vmstate
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
209 env
->fptag_vmstate
= 0;
210 for(i
= 0; i
< 8; i
++) {
211 env
->fptag_vmstate
|= ((!env
->fptags
[i
]) << i
);
214 env
->fpregs_format_vmstate
= 0;
217 * Real mode guest segments register DPL should be zero.
218 * Older KVM version were setting it wrongly.
219 * Fixing it will allow live migration to host with unrestricted guest
220 * support (otherwise the migration will fail with invalid guest state
223 if (!(env
->cr
[0] & CR0_PE_MASK
) &&
224 (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
& 3) != 0) {
225 env
->segs
[R_CS
].flags
&= ~(env
->segs
[R_CS
].flags
& DESC_DPL_MASK
);
226 env
->segs
[R_DS
].flags
&= ~(env
->segs
[R_DS
].flags
& DESC_DPL_MASK
);
227 env
->segs
[R_ES
].flags
&= ~(env
->segs
[R_ES
].flags
& DESC_DPL_MASK
);
228 env
->segs
[R_FS
].flags
&= ~(env
->segs
[R_FS
].flags
& DESC_DPL_MASK
);
229 env
->segs
[R_GS
].flags
&= ~(env
->segs
[R_GS
].flags
& DESC_DPL_MASK
);
230 env
->segs
[R_SS
].flags
&= ~(env
->segs
[R_SS
].flags
& DESC_DPL_MASK
);
236 static int cpu_post_load(void *opaque
, int version_id
)
238 X86CPU
*cpu
= opaque
;
239 CPUState
*cs
= CPU(cpu
);
240 CPUX86State
*env
= &cpu
->env
;
243 if (env
->tsc_khz
&& env
->user_tsc_khz
&&
244 env
->tsc_khz
!= env
->user_tsc_khz
) {
245 error_report("Mismatch between user-specified TSC frequency and "
246 "migrated TSC frequency");
250 if (env
->fpregs_format_vmstate
) {
251 error_report("Unsupported old non-softfloat CPU state");
255 * Real mode guest segments register DPL should be zero.
256 * Older KVM version were setting it wrongly.
257 * Fixing it will allow live migration from such host that don't have
258 * restricted guest support to a host with unrestricted guest support
259 * (otherwise the migration will fail with invalid guest state
262 if (!(env
->cr
[0] & CR0_PE_MASK
) &&
263 (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
& 3) != 0) {
264 env
->segs
[R_CS
].flags
&= ~(env
->segs
[R_CS
].flags
& DESC_DPL_MASK
);
265 env
->segs
[R_DS
].flags
&= ~(env
->segs
[R_DS
].flags
& DESC_DPL_MASK
);
266 env
->segs
[R_ES
].flags
&= ~(env
->segs
[R_ES
].flags
& DESC_DPL_MASK
);
267 env
->segs
[R_FS
].flags
&= ~(env
->segs
[R_FS
].flags
& DESC_DPL_MASK
);
268 env
->segs
[R_GS
].flags
&= ~(env
->segs
[R_GS
].flags
& DESC_DPL_MASK
);
269 env
->segs
[R_SS
].flags
&= ~(env
->segs
[R_SS
].flags
& DESC_DPL_MASK
);
272 /* Older versions of QEMU incorrectly used CS.DPL as the CPL when
273 * running under KVM. This is wrong for conforming code segments.
274 * Luckily, in our implementation the CPL field of hflags is redundant
275 * and we can get the right value from the SS descriptor privilege level.
277 env
->hflags
&= ~HF_CPL_MASK
;
278 env
->hflags
|= (env
->segs
[R_SS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
280 env
->fpstt
= (env
->fpus_vmstate
>> 11) & 7;
281 env
->fpus
= env
->fpus_vmstate
& ~0x3800;
282 env
->fptag_vmstate
^= 0xff;
283 for(i
= 0; i
< 8; i
++) {
284 env
->fptags
[i
] = (env
->fptag_vmstate
>> i
) & 1;
288 update_fp_status(env
);
289 update_mxcsr_status(env
);
291 cpu_breakpoint_remove_all(cs
, BP_CPU
);
292 cpu_watchpoint_remove_all(cs
, BP_CPU
);
294 /* Indicate all breakpoints disabled, as they are, then
295 let the helper re-enable them. */
297 env
->dr
[7] = dr7
& ~(DR7_GLOBAL_BP_MASK
| DR7_LOCAL_BP_MASK
);
298 cpu_x86_update_dr7(env
, dr7
);
304 static bool async_pf_msr_needed(void *opaque
)
306 X86CPU
*cpu
= opaque
;
308 return cpu
->env
.async_pf_en_msr
!= 0;
311 static bool pv_eoi_msr_needed(void *opaque
)
313 X86CPU
*cpu
= opaque
;
315 return cpu
->env
.pv_eoi_en_msr
!= 0;
318 static bool steal_time_msr_needed(void *opaque
)
320 X86CPU
*cpu
= opaque
;
322 return cpu
->env
.steal_time_msr
!= 0;
325 static const VMStateDescription vmstate_steal_time_msr
= {
326 .name
= "cpu/steal_time_msr",
328 .minimum_version_id
= 1,
329 .needed
= steal_time_msr_needed
,
330 .fields
= (VMStateField
[]) {
331 VMSTATE_UINT64(env
.steal_time_msr
, X86CPU
),
332 VMSTATE_END_OF_LIST()
336 static const VMStateDescription vmstate_async_pf_msr
= {
337 .name
= "cpu/async_pf_msr",
339 .minimum_version_id
= 1,
340 .needed
= async_pf_msr_needed
,
341 .fields
= (VMStateField
[]) {
342 VMSTATE_UINT64(env
.async_pf_en_msr
, X86CPU
),
343 VMSTATE_END_OF_LIST()
347 static const VMStateDescription vmstate_pv_eoi_msr
= {
348 .name
= "cpu/async_pv_eoi_msr",
350 .minimum_version_id
= 1,
351 .needed
= pv_eoi_msr_needed
,
352 .fields
= (VMStateField
[]) {
353 VMSTATE_UINT64(env
.pv_eoi_en_msr
, X86CPU
),
354 VMSTATE_END_OF_LIST()
358 static bool fpop_ip_dp_needed(void *opaque
)
360 X86CPU
*cpu
= opaque
;
361 CPUX86State
*env
= &cpu
->env
;
363 return env
->fpop
!= 0 || env
->fpip
!= 0 || env
->fpdp
!= 0;
366 static const VMStateDescription vmstate_fpop_ip_dp
= {
367 .name
= "cpu/fpop_ip_dp",
369 .minimum_version_id
= 1,
370 .needed
= fpop_ip_dp_needed
,
371 .fields
= (VMStateField
[]) {
372 VMSTATE_UINT16(env
.fpop
, X86CPU
),
373 VMSTATE_UINT64(env
.fpip
, X86CPU
),
374 VMSTATE_UINT64(env
.fpdp
, X86CPU
),
375 VMSTATE_END_OF_LIST()
379 static bool tsc_adjust_needed(void *opaque
)
381 X86CPU
*cpu
= opaque
;
382 CPUX86State
*env
= &cpu
->env
;
384 return env
->tsc_adjust
!= 0;
387 static const VMStateDescription vmstate_msr_tsc_adjust
= {
388 .name
= "cpu/msr_tsc_adjust",
390 .minimum_version_id
= 1,
391 .needed
= tsc_adjust_needed
,
392 .fields
= (VMStateField
[]) {
393 VMSTATE_UINT64(env
.tsc_adjust
, X86CPU
),
394 VMSTATE_END_OF_LIST()
398 static bool msr_smi_count_needed(void *opaque
)
400 X86CPU
*cpu
= opaque
;
401 CPUX86State
*env
= &cpu
->env
;
403 return env
->msr_smi_count
!= 0;
406 static const VMStateDescription vmstate_msr_smi_count
= {
407 .name
= "cpu/msr_smi_count",
409 .minimum_version_id
= 1,
410 .needed
= msr_smi_count_needed
,
411 .fields
= (VMStateField
[]) {
412 VMSTATE_UINT64(env
.msr_smi_count
, X86CPU
),
413 VMSTATE_END_OF_LIST()
417 static bool tscdeadline_needed(void *opaque
)
419 X86CPU
*cpu
= opaque
;
420 CPUX86State
*env
= &cpu
->env
;
422 return env
->tsc_deadline
!= 0;
425 static const VMStateDescription vmstate_msr_tscdeadline
= {
426 .name
= "cpu/msr_tscdeadline",
428 .minimum_version_id
= 1,
429 .needed
= tscdeadline_needed
,
430 .fields
= (VMStateField
[]) {
431 VMSTATE_UINT64(env
.tsc_deadline
, X86CPU
),
432 VMSTATE_END_OF_LIST()
436 static bool misc_enable_needed(void *opaque
)
438 X86CPU
*cpu
= opaque
;
439 CPUX86State
*env
= &cpu
->env
;
441 return env
->msr_ia32_misc_enable
!= MSR_IA32_MISC_ENABLE_DEFAULT
;
444 static bool feature_control_needed(void *opaque
)
446 X86CPU
*cpu
= opaque
;
447 CPUX86State
*env
= &cpu
->env
;
449 return env
->msr_ia32_feature_control
!= 0;
452 static const VMStateDescription vmstate_msr_ia32_misc_enable
= {
453 .name
= "cpu/msr_ia32_misc_enable",
455 .minimum_version_id
= 1,
456 .needed
= misc_enable_needed
,
457 .fields
= (VMStateField
[]) {
458 VMSTATE_UINT64(env
.msr_ia32_misc_enable
, X86CPU
),
459 VMSTATE_END_OF_LIST()
463 static const VMStateDescription vmstate_msr_ia32_feature_control
= {
464 .name
= "cpu/msr_ia32_feature_control",
466 .minimum_version_id
= 1,
467 .needed
= feature_control_needed
,
468 .fields
= (VMStateField
[]) {
469 VMSTATE_UINT64(env
.msr_ia32_feature_control
, X86CPU
),
470 VMSTATE_END_OF_LIST()
474 static bool pmu_enable_needed(void *opaque
)
476 X86CPU
*cpu
= opaque
;
477 CPUX86State
*env
= &cpu
->env
;
480 if (env
->msr_fixed_ctr_ctrl
|| env
->msr_global_ctrl
||
481 env
->msr_global_status
|| env
->msr_global_ovf_ctrl
) {
484 for (i
= 0; i
< MAX_FIXED_COUNTERS
; i
++) {
485 if (env
->msr_fixed_counters
[i
]) {
489 for (i
= 0; i
< MAX_GP_COUNTERS
; i
++) {
490 if (env
->msr_gp_counters
[i
] || env
->msr_gp_evtsel
[i
]) {
498 static const VMStateDescription vmstate_msr_architectural_pmu
= {
499 .name
= "cpu/msr_architectural_pmu",
501 .minimum_version_id
= 1,
502 .needed
= pmu_enable_needed
,
503 .fields
= (VMStateField
[]) {
504 VMSTATE_UINT64(env
.msr_fixed_ctr_ctrl
, X86CPU
),
505 VMSTATE_UINT64(env
.msr_global_ctrl
, X86CPU
),
506 VMSTATE_UINT64(env
.msr_global_status
, X86CPU
),
507 VMSTATE_UINT64(env
.msr_global_ovf_ctrl
, X86CPU
),
508 VMSTATE_UINT64_ARRAY(env
.msr_fixed_counters
, X86CPU
, MAX_FIXED_COUNTERS
),
509 VMSTATE_UINT64_ARRAY(env
.msr_gp_counters
, X86CPU
, MAX_GP_COUNTERS
),
510 VMSTATE_UINT64_ARRAY(env
.msr_gp_evtsel
, X86CPU
, MAX_GP_COUNTERS
),
511 VMSTATE_END_OF_LIST()
515 static bool mpx_needed(void *opaque
)
517 X86CPU
*cpu
= opaque
;
518 CPUX86State
*env
= &cpu
->env
;
521 for (i
= 0; i
< 4; i
++) {
522 if (env
->bnd_regs
[i
].lb
|| env
->bnd_regs
[i
].ub
) {
527 if (env
->bndcs_regs
.cfgu
|| env
->bndcs_regs
.sts
) {
531 return !!env
->msr_bndcfgs
;
534 static const VMStateDescription vmstate_mpx
= {
537 .minimum_version_id
= 1,
538 .needed
= mpx_needed
,
539 .fields
= (VMStateField
[]) {
540 VMSTATE_BND_REGS(env
.bnd_regs
, X86CPU
, 4),
541 VMSTATE_UINT64(env
.bndcs_regs
.cfgu
, X86CPU
),
542 VMSTATE_UINT64(env
.bndcs_regs
.sts
, X86CPU
),
543 VMSTATE_UINT64(env
.msr_bndcfgs
, X86CPU
),
544 VMSTATE_END_OF_LIST()
548 static bool hyperv_hypercall_enable_needed(void *opaque
)
550 X86CPU
*cpu
= opaque
;
551 CPUX86State
*env
= &cpu
->env
;
553 return env
->msr_hv_hypercall
!= 0 || env
->msr_hv_guest_os_id
!= 0;
556 static const VMStateDescription vmstate_msr_hypercall_hypercall
= {
557 .name
= "cpu/msr_hyperv_hypercall",
559 .minimum_version_id
= 1,
560 .needed
= hyperv_hypercall_enable_needed
,
561 .fields
= (VMStateField
[]) {
562 VMSTATE_UINT64(env
.msr_hv_guest_os_id
, X86CPU
),
563 VMSTATE_UINT64(env
.msr_hv_hypercall
, X86CPU
),
564 VMSTATE_END_OF_LIST()
568 static bool hyperv_vapic_enable_needed(void *opaque
)
570 X86CPU
*cpu
= opaque
;
571 CPUX86State
*env
= &cpu
->env
;
573 return env
->msr_hv_vapic
!= 0;
576 static const VMStateDescription vmstate_msr_hyperv_vapic
= {
577 .name
= "cpu/msr_hyperv_vapic",
579 .minimum_version_id
= 1,
580 .needed
= hyperv_vapic_enable_needed
,
581 .fields
= (VMStateField
[]) {
582 VMSTATE_UINT64(env
.msr_hv_vapic
, X86CPU
),
583 VMSTATE_END_OF_LIST()
587 static bool hyperv_time_enable_needed(void *opaque
)
589 X86CPU
*cpu
= opaque
;
590 CPUX86State
*env
= &cpu
->env
;
592 return env
->msr_hv_tsc
!= 0;
595 static const VMStateDescription vmstate_msr_hyperv_time
= {
596 .name
= "cpu/msr_hyperv_time",
598 .minimum_version_id
= 1,
599 .needed
= hyperv_time_enable_needed
,
600 .fields
= (VMStateField
[]) {
601 VMSTATE_UINT64(env
.msr_hv_tsc
, X86CPU
),
602 VMSTATE_END_OF_LIST()
606 static bool hyperv_crash_enable_needed(void *opaque
)
608 X86CPU
*cpu
= opaque
;
609 CPUX86State
*env
= &cpu
->env
;
612 for (i
= 0; i
< HV_CRASH_PARAMS
; i
++) {
613 if (env
->msr_hv_crash_params
[i
]) {
620 static const VMStateDescription vmstate_msr_hyperv_crash
= {
621 .name
= "cpu/msr_hyperv_crash",
623 .minimum_version_id
= 1,
624 .needed
= hyperv_crash_enable_needed
,
625 .fields
= (VMStateField
[]) {
626 VMSTATE_UINT64_ARRAY(env
.msr_hv_crash_params
, X86CPU
, HV_CRASH_PARAMS
),
627 VMSTATE_END_OF_LIST()
631 static bool hyperv_runtime_enable_needed(void *opaque
)
633 X86CPU
*cpu
= opaque
;
634 CPUX86State
*env
= &cpu
->env
;
636 if (!cpu
->hyperv_runtime
) {
640 return env
->msr_hv_runtime
!= 0;
643 static const VMStateDescription vmstate_msr_hyperv_runtime
= {
644 .name
= "cpu/msr_hyperv_runtime",
646 .minimum_version_id
= 1,
647 .needed
= hyperv_runtime_enable_needed
,
648 .fields
= (VMStateField
[]) {
649 VMSTATE_UINT64(env
.msr_hv_runtime
, X86CPU
),
650 VMSTATE_END_OF_LIST()
654 static bool hyperv_synic_enable_needed(void *opaque
)
656 X86CPU
*cpu
= opaque
;
657 CPUX86State
*env
= &cpu
->env
;
660 if (env
->msr_hv_synic_control
!= 0 ||
661 env
->msr_hv_synic_evt_page
!= 0 ||
662 env
->msr_hv_synic_msg_page
!= 0) {
666 for (i
= 0; i
< ARRAY_SIZE(env
->msr_hv_synic_sint
); i
++) {
667 if (env
->msr_hv_synic_sint
[i
] != 0) {
675 static const VMStateDescription vmstate_msr_hyperv_synic
= {
676 .name
= "cpu/msr_hyperv_synic",
678 .minimum_version_id
= 1,
679 .needed
= hyperv_synic_enable_needed
,
680 .fields
= (VMStateField
[]) {
681 VMSTATE_UINT64(env
.msr_hv_synic_control
, X86CPU
),
682 VMSTATE_UINT64(env
.msr_hv_synic_evt_page
, X86CPU
),
683 VMSTATE_UINT64(env
.msr_hv_synic_msg_page
, X86CPU
),
684 VMSTATE_UINT64_ARRAY(env
.msr_hv_synic_sint
, X86CPU
, HV_SINT_COUNT
),
685 VMSTATE_END_OF_LIST()
689 static bool hyperv_stimer_enable_needed(void *opaque
)
691 X86CPU
*cpu
= opaque
;
692 CPUX86State
*env
= &cpu
->env
;
695 for (i
= 0; i
< ARRAY_SIZE(env
->msr_hv_stimer_config
); i
++) {
696 if (env
->msr_hv_stimer_config
[i
] || env
->msr_hv_stimer_count
[i
]) {
703 static const VMStateDescription vmstate_msr_hyperv_stimer
= {
704 .name
= "cpu/msr_hyperv_stimer",
706 .minimum_version_id
= 1,
707 .needed
= hyperv_stimer_enable_needed
,
708 .fields
= (VMStateField
[]) {
709 VMSTATE_UINT64_ARRAY(env
.msr_hv_stimer_config
, X86CPU
,
711 VMSTATE_UINT64_ARRAY(env
.msr_hv_stimer_count
, X86CPU
, HV_STIMER_COUNT
),
712 VMSTATE_END_OF_LIST()
716 static bool avx512_needed(void *opaque
)
718 X86CPU
*cpu
= opaque
;
719 CPUX86State
*env
= &cpu
->env
;
722 for (i
= 0; i
< NB_OPMASK_REGS
; i
++) {
723 if (env
->opmask_regs
[i
]) {
728 for (i
= 0; i
< CPU_NB_REGS
; i
++) {
729 #define ENV_XMM(reg, field) (env->xmm_regs[reg].ZMM_Q(field))
730 if (ENV_XMM(i
, 4) || ENV_XMM(i
, 6) ||
731 ENV_XMM(i
, 5) || ENV_XMM(i
, 7)) {
735 if (ENV_XMM(i
+16, 0) || ENV_XMM(i
+16, 1) ||
736 ENV_XMM(i
+16, 2) || ENV_XMM(i
+16, 3) ||
737 ENV_XMM(i
+16, 4) || ENV_XMM(i
+16, 5) ||
738 ENV_XMM(i
+16, 6) || ENV_XMM(i
+16, 7)) {
747 static const VMStateDescription vmstate_avx512
= {
748 .name
= "cpu/avx512",
750 .minimum_version_id
= 1,
751 .needed
= avx512_needed
,
752 .fields
= (VMStateField
[]) {
753 VMSTATE_UINT64_ARRAY(env
.opmask_regs
, X86CPU
, NB_OPMASK_REGS
),
754 VMSTATE_ZMMH_REGS_VARS(env
.xmm_regs
, X86CPU
, 0),
756 VMSTATE_Hi16_ZMM_REGS_VARS(env
.xmm_regs
, X86CPU
, 16),
758 VMSTATE_END_OF_LIST()
762 static bool xss_needed(void *opaque
)
764 X86CPU
*cpu
= opaque
;
765 CPUX86State
*env
= &cpu
->env
;
767 return env
->xss
!= 0;
770 static const VMStateDescription vmstate_xss
= {
773 .minimum_version_id
= 1,
774 .needed
= xss_needed
,
775 .fields
= (VMStateField
[]) {
776 VMSTATE_UINT64(env
.xss
, X86CPU
),
777 VMSTATE_END_OF_LIST()
782 static bool pkru_needed(void *opaque
)
784 X86CPU
*cpu
= opaque
;
785 CPUX86State
*env
= &cpu
->env
;
787 return env
->pkru
!= 0;
790 static const VMStateDescription vmstate_pkru
= {
793 .minimum_version_id
= 1,
794 .needed
= pkru_needed
,
795 .fields
= (VMStateField
[]){
796 VMSTATE_UINT32(env
.pkru
, X86CPU
),
797 VMSTATE_END_OF_LIST()
802 static bool tsc_khz_needed(void *opaque
)
804 X86CPU
*cpu
= opaque
;
805 CPUX86State
*env
= &cpu
->env
;
806 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
807 PCMachineClass
*pcmc
= PC_MACHINE_CLASS(mc
);
808 return env
->tsc_khz
&& pcmc
->save_tsc_khz
;
811 static const VMStateDescription vmstate_tsc_khz
= {
812 .name
= "cpu/tsc_khz",
814 .minimum_version_id
= 1,
815 .needed
= tsc_khz_needed
,
816 .fields
= (VMStateField
[]) {
817 VMSTATE_INT64(env
.tsc_khz
, X86CPU
),
818 VMSTATE_END_OF_LIST()
822 static bool mcg_ext_ctl_needed(void *opaque
)
824 X86CPU
*cpu
= opaque
;
825 CPUX86State
*env
= &cpu
->env
;
826 return cpu
->enable_lmce
&& env
->mcg_ext_ctl
;
829 static const VMStateDescription vmstate_mcg_ext_ctl
= {
830 .name
= "cpu/mcg_ext_ctl",
832 .minimum_version_id
= 1,
833 .needed
= mcg_ext_ctl_needed
,
834 .fields
= (VMStateField
[]) {
835 VMSTATE_UINT64(env
.mcg_ext_ctl
, X86CPU
),
836 VMSTATE_END_OF_LIST()
840 static bool spec_ctrl_needed(void *opaque
)
842 X86CPU
*cpu
= opaque
;
843 CPUX86State
*env
= &cpu
->env
;
845 return env
->spec_ctrl
!= 0;
848 static const VMStateDescription vmstate_spec_ctrl
= {
849 .name
= "cpu/spec_ctrl",
851 .minimum_version_id
= 1,
852 .needed
= spec_ctrl_needed
,
853 .fields
= (VMStateField
[]){
854 VMSTATE_UINT64(env
.spec_ctrl
, X86CPU
),
855 VMSTATE_END_OF_LIST()
859 static bool intel_pt_enable_needed(void *opaque
)
861 X86CPU
*cpu
= opaque
;
862 CPUX86State
*env
= &cpu
->env
;
865 if (env
->msr_rtit_ctrl
|| env
->msr_rtit_status
||
866 env
->msr_rtit_output_base
|| env
->msr_rtit_output_mask
||
867 env
->msr_rtit_cr3_match
) {
871 for (i
= 0; i
< MAX_RTIT_ADDRS
; i
++) {
872 if (env
->msr_rtit_addrs
[i
]) {
880 static const VMStateDescription vmstate_msr_intel_pt
= {
881 .name
= "cpu/intel_pt",
883 .minimum_version_id
= 1,
884 .needed
= intel_pt_enable_needed
,
885 .fields
= (VMStateField
[]) {
886 VMSTATE_UINT64(env
.msr_rtit_ctrl
, X86CPU
),
887 VMSTATE_UINT64(env
.msr_rtit_status
, X86CPU
),
888 VMSTATE_UINT64(env
.msr_rtit_output_base
, X86CPU
),
889 VMSTATE_UINT64(env
.msr_rtit_output_mask
, X86CPU
),
890 VMSTATE_UINT64(env
.msr_rtit_cr3_match
, X86CPU
),
891 VMSTATE_UINT64_ARRAY(env
.msr_rtit_addrs
, X86CPU
, MAX_RTIT_ADDRS
),
892 VMSTATE_END_OF_LIST()
896 VMStateDescription vmstate_x86_cpu
= {
899 .minimum_version_id
= 11,
900 .pre_save
= cpu_pre_save
,
901 .post_load
= cpu_post_load
,
902 .fields
= (VMStateField
[]) {
903 VMSTATE_UINTTL_ARRAY(env
.regs
, X86CPU
, CPU_NB_REGS
),
904 VMSTATE_UINTTL(env
.eip
, X86CPU
),
905 VMSTATE_UINTTL(env
.eflags
, X86CPU
),
906 VMSTATE_UINT32(env
.hflags
, X86CPU
),
908 VMSTATE_UINT16(env
.fpuc
, X86CPU
),
909 VMSTATE_UINT16(env
.fpus_vmstate
, X86CPU
),
910 VMSTATE_UINT16(env
.fptag_vmstate
, X86CPU
),
911 VMSTATE_UINT16(env
.fpregs_format_vmstate
, X86CPU
),
913 VMSTATE_STRUCT_ARRAY(env
.fpregs
, X86CPU
, 8, 0, vmstate_fpreg
, FPReg
),
915 VMSTATE_SEGMENT_ARRAY(env
.segs
, X86CPU
, 6),
916 VMSTATE_SEGMENT(env
.ldt
, X86CPU
),
917 VMSTATE_SEGMENT(env
.tr
, X86CPU
),
918 VMSTATE_SEGMENT(env
.gdt
, X86CPU
),
919 VMSTATE_SEGMENT(env
.idt
, X86CPU
),
921 VMSTATE_UINT32(env
.sysenter_cs
, X86CPU
),
922 VMSTATE_UINTTL(env
.sysenter_esp
, X86CPU
),
923 VMSTATE_UINTTL(env
.sysenter_eip
, X86CPU
),
925 VMSTATE_UINTTL(env
.cr
[0], X86CPU
),
926 VMSTATE_UINTTL(env
.cr
[2], X86CPU
),
927 VMSTATE_UINTTL(env
.cr
[3], X86CPU
),
928 VMSTATE_UINTTL(env
.cr
[4], X86CPU
),
929 VMSTATE_UINTTL_ARRAY(env
.dr
, X86CPU
, 8),
931 VMSTATE_INT32(env
.a20_mask
, X86CPU
),
933 VMSTATE_UINT32(env
.mxcsr
, X86CPU
),
934 VMSTATE_XMM_REGS(env
.xmm_regs
, X86CPU
, 0),
937 VMSTATE_UINT64(env
.efer
, X86CPU
),
938 VMSTATE_UINT64(env
.star
, X86CPU
),
939 VMSTATE_UINT64(env
.lstar
, X86CPU
),
940 VMSTATE_UINT64(env
.cstar
, X86CPU
),
941 VMSTATE_UINT64(env
.fmask
, X86CPU
),
942 VMSTATE_UINT64(env
.kernelgsbase
, X86CPU
),
944 VMSTATE_UINT32(env
.smbase
, X86CPU
),
946 VMSTATE_UINT64(env
.pat
, X86CPU
),
947 VMSTATE_UINT32(env
.hflags2
, X86CPU
),
949 VMSTATE_UINT64(env
.vm_hsave
, X86CPU
),
950 VMSTATE_UINT64(env
.vm_vmcb
, X86CPU
),
951 VMSTATE_UINT64(env
.tsc_offset
, X86CPU
),
952 VMSTATE_UINT64(env
.intercept
, X86CPU
),
953 VMSTATE_UINT16(env
.intercept_cr_read
, X86CPU
),
954 VMSTATE_UINT16(env
.intercept_cr_write
, X86CPU
),
955 VMSTATE_UINT16(env
.intercept_dr_read
, X86CPU
),
956 VMSTATE_UINT16(env
.intercept_dr_write
, X86CPU
),
957 VMSTATE_UINT32(env
.intercept_exceptions
, X86CPU
),
958 VMSTATE_UINT8(env
.v_tpr
, X86CPU
),
960 VMSTATE_UINT64_ARRAY(env
.mtrr_fixed
, X86CPU
, 11),
961 VMSTATE_UINT64(env
.mtrr_deftype
, X86CPU
),
962 VMSTATE_MTRR_VARS(env
.mtrr_var
, X86CPU
, MSR_MTRRcap_VCNT
, 8),
963 /* KVM-related states */
964 VMSTATE_INT32(env
.interrupt_injected
, X86CPU
),
965 VMSTATE_UINT32(env
.mp_state
, X86CPU
),
966 VMSTATE_UINT64(env
.tsc
, X86CPU
),
967 VMSTATE_INT32(env
.exception_injected
, X86CPU
),
968 VMSTATE_UINT8(env
.soft_interrupt
, X86CPU
),
969 VMSTATE_UINT8(env
.nmi_injected
, X86CPU
),
970 VMSTATE_UINT8(env
.nmi_pending
, X86CPU
),
971 VMSTATE_UINT8(env
.has_error_code
, X86CPU
),
972 VMSTATE_UINT32(env
.sipi_vector
, X86CPU
),
974 VMSTATE_UINT64(env
.mcg_cap
, X86CPU
),
975 VMSTATE_UINT64(env
.mcg_status
, X86CPU
),
976 VMSTATE_UINT64(env
.mcg_ctl
, X86CPU
),
977 VMSTATE_UINT64_ARRAY(env
.mce_banks
, X86CPU
, MCE_BANKS_DEF
* 4),
979 VMSTATE_UINT64(env
.tsc_aux
, X86CPU
),
980 /* KVM pvclock msr */
981 VMSTATE_UINT64(env
.system_time_msr
, X86CPU
),
982 VMSTATE_UINT64(env
.wall_clock_msr
, X86CPU
),
983 /* XSAVE related fields */
984 VMSTATE_UINT64_V(env
.xcr0
, X86CPU
, 12),
985 VMSTATE_UINT64_V(env
.xstate_bv
, X86CPU
, 12),
986 VMSTATE_YMMH_REGS_VARS(env
.xmm_regs
, X86CPU
, 0, 12),
987 VMSTATE_END_OF_LIST()
988 /* The above list is not sorted /wrt version numbers, watch out! */
990 .subsections
= (const VMStateDescription
*[]) {
991 &vmstate_async_pf_msr
,
993 &vmstate_steal_time_msr
,
995 &vmstate_msr_tsc_adjust
,
996 &vmstate_msr_tscdeadline
,
997 &vmstate_msr_ia32_misc_enable
,
998 &vmstate_msr_ia32_feature_control
,
999 &vmstate_msr_architectural_pmu
,
1001 &vmstate_msr_hypercall_hypercall
,
1002 &vmstate_msr_hyperv_vapic
,
1003 &vmstate_msr_hyperv_time
,
1004 &vmstate_msr_hyperv_crash
,
1005 &vmstate_msr_hyperv_runtime
,
1006 &vmstate_msr_hyperv_synic
,
1007 &vmstate_msr_hyperv_stimer
,
1011 &vmstate_msr_smi_count
,
1012 #ifdef TARGET_X86_64
1016 &vmstate_mcg_ext_ctl
,
1017 &vmstate_msr_intel_pt
,