machine.c 30.3 KB
Newer Older
P
Peter Maydell 已提交
1
#include "qemu/osdep.h"
2 3
#include "qemu-common.h"
#include "cpu.h"
4
#include "exec/exec-all.h"
A
aurel32 已提交
5 6
#include "hw/hw.h"
#include "hw/boards.h"
P
Paolo Bonzini 已提交
7 8
#include "hw/i386/pc.h"
#include "hw/isa/isa.h"
9
#include "migration/cpu.h"
A
aurel32 已提交
10

11
#include "sysemu/kvm.h"
A
aurel32 已提交
12

13 14
#include "qemu/error-report.h"

J
Juan Quintela 已提交
15 16 17 18
static const VMStateDescription vmstate_segment = {
    .name = "segment",
    .version_id = 1,
    .minimum_version_id = 1,
19
    .fields = (VMStateField[]) {
J
Juan Quintela 已提交
20 21 22 23 24 25 26 27
        VMSTATE_UINT32(selector, SegmentCache),
        VMSTATE_UINTTL(base, SegmentCache),
        VMSTATE_UINT32(limit, SegmentCache),
        VMSTATE_UINT32(flags, SegmentCache),
        VMSTATE_END_OF_LIST()
    }
};

J
Juan Quintela 已提交
28 29 30 31 32 33 34
#define VMSTATE_SEGMENT(_field, _state) {                            \
    .name       = (stringify(_field)),                               \
    .size       = sizeof(SegmentCache),                              \
    .vmsd       = &vmstate_segment,                                  \
    .flags      = VMS_STRUCT,                                        \
    .offset     = offsetof(_state, _field)                           \
            + type_check(SegmentCache,typeof_field(_state, _field))  \
A
aurel32 已提交
35 36
}

J
Juan Quintela 已提交
37 38
#define VMSTATE_SEGMENT_ARRAY(_field, _state, _n)                    \
    VMSTATE_STRUCT_ARRAY(_field, _state, _n, 0, vmstate_segment, SegmentCache)
A
aurel32 已提交
39

40 41 42 43
static const VMStateDescription vmstate_xmm_reg = {
    .name = "xmm_reg",
    .version_id = 1,
    .minimum_version_id = 1,
44
    .fields = (VMStateField[]) {
45 46
        VMSTATE_UINT64(ZMM_Q(0), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(1), ZMMReg),
47 48 49 50
        VMSTATE_END_OF_LIST()
    }
};

51 52
#define VMSTATE_XMM_REGS(_field, _state, _start)                         \
    VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, CPU_NB_REGS, 0,     \
53
                             vmstate_xmm_reg, ZMMReg)
54

55
/* YMMH format is the same as XMM, but for bits 128-255 */
56 57 58 59
static const VMStateDescription vmstate_ymmh_reg = {
    .name = "ymmh_reg",
    .version_id = 1,
    .minimum_version_id = 1,
60
    .fields = (VMStateField[]) {
61 62
        VMSTATE_UINT64(ZMM_Q(2), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(3), ZMMReg),
63 64 65 66
        VMSTATE_END_OF_LIST()
    }
};

67 68
#define VMSTATE_YMMH_REGS_VARS(_field, _state, _start, _v)               \
    VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, CPU_NB_REGS, _v,    \
69
                             vmstate_ymmh_reg, ZMMReg)
70

C
Chao Peng 已提交
71 72 73 74 75
static const VMStateDescription vmstate_zmmh_reg = {
    .name = "zmmh_reg",
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (VMStateField[]) {
76 77 78 79
        VMSTATE_UINT64(ZMM_Q(4), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(5), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(6), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(7), ZMMReg),
C
Chao Peng 已提交
80 81 82 83
        VMSTATE_END_OF_LIST()
    }
};

84 85
#define VMSTATE_ZMMH_REGS_VARS(_field, _state, _start)                   \
    VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, CPU_NB_REGS, 0,     \
86
                             vmstate_zmmh_reg, ZMMReg)
C
Chao Peng 已提交
87 88 89 90 91 92 93

#ifdef TARGET_X86_64
static const VMStateDescription vmstate_hi16_zmm_reg = {
    .name = "hi16_zmm_reg",
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (VMStateField[]) {
94 95 96 97 98 99 100 101
        VMSTATE_UINT64(ZMM_Q(0), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(1), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(2), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(3), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(4), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(5), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(6), ZMMReg),
        VMSTATE_UINT64(ZMM_Q(7), ZMMReg),
C
Chao Peng 已提交
102 103 104 105
        VMSTATE_END_OF_LIST()
    }
};

106 107
#define VMSTATE_Hi16_ZMM_REGS_VARS(_field, _state, _start)               \
    VMSTATE_STRUCT_SUB_ARRAY(_field, _state, _start, CPU_NB_REGS, 0,     \
108
                             vmstate_hi16_zmm_reg, ZMMReg)
C
Chao Peng 已提交
109 110
#endif

L
Liu Jinsong 已提交
111 112 113 114
static const VMStateDescription vmstate_bnd_regs = {
    .name = "bnd_regs",
    .version_id = 1,
    .minimum_version_id = 1,
115
    .fields = (VMStateField[]) {
L
Liu Jinsong 已提交
116 117 118 119 120 121 122 123 124
        VMSTATE_UINT64(lb, BNDReg),
        VMSTATE_UINT64(ub, BNDReg),
        VMSTATE_END_OF_LIST()
    }
};

#define VMSTATE_BND_REGS(_field, _state, _n)          \
    VMSTATE_STRUCT_ARRAY(_field, _state, _n, 0, vmstate_bnd_regs, BNDReg)

125 126 127 128
static const VMStateDescription vmstate_mtrr_var = {
    .name = "mtrr_var",
    .version_id = 1,
    .minimum_version_id = 1,
129
    .fields = (VMStateField[]) {
130 131 132 133 134 135
        VMSTATE_UINT64(base, MTRRVar),
        VMSTATE_UINT64(mask, MTRRVar),
        VMSTATE_END_OF_LIST()
    }
};

J
Juan Quintela 已提交
136 137
#define VMSTATE_MTRR_VARS(_field, _state, _n, _v)                    \
    VMSTATE_STRUCT_ARRAY(_field, _state, _n, _v, vmstate_mtrr_var, MTRRVar)
138

J
Jianjun Duan 已提交
139 140
static int put_fpreg_error(QEMUFile *f, void *opaque, size_t size,
                           VMStateField *field, QJSON *vmdesc)
141
{
J
Juan Quintela 已提交
142 143
    fprintf(stderr, "call put_fpreg() with invalid arguments\n");
    exit(0);
J
Jianjun Duan 已提交
144
    return 0;
145 146
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/* XXX: add that in a FPU generic layer */
union x86_longdouble {
    uint64_t mant;
    uint16_t exp;
};

#define MANTD1(fp)	(fp & ((1LL << 52) - 1))
#define EXPBIAS1 1023
#define EXPD1(fp)	((fp >> 52) & 0x7FF)
#define SIGND1(fp)	((fp >> 32) & 0x80000000)

static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
{
    int e;
    /* mantissa */
    p->mant = (MANTD1(temp) << 11) | (1LL << 63);
    /* exponent + sign */
    e = EXPD1(temp) - EXPBIAS1 + 16383;
    e |= SIGND1(temp) >> 16;
    p->exp = e;
}

J
Jianjun Duan 已提交
169 170
static int get_fpreg(QEMUFile *f, void *opaque, size_t size,
                     VMStateField *field)
171 172 173 174 175 176 177 178 179 180 181
{
    FPReg *fp_reg = opaque;
    uint64_t mant;
    uint16_t exp;

    qemu_get_be64s(f, &mant);
    qemu_get_be16s(f, &exp);
    fp_reg->d = cpu_set_fp80(mant, exp);
    return 0;
}

J
Jianjun Duan 已提交
182 183
static int put_fpreg(QEMUFile *f, void *opaque, size_t size,
                     VMStateField *field, QJSON *vmdesc)
184 185 186 187 188 189 190 191 192
{
    FPReg *fp_reg = opaque;
    uint64_t mant;
    uint16_t exp;
    /* we save the real CPU data (in case of MMX usage only 'mant'
       contains the MMX register */
    cpu_get_fp80(&mant, &exp, fp_reg->d);
    qemu_put_be64s(f, &mant);
    qemu_put_be16s(f, &exp);
J
Jianjun Duan 已提交
193 194

    return 0;
195 196
}

B
Blue Swirl 已提交
197
static const VMStateInfo vmstate_fpreg = {
J
Juan Quintela 已提交
198 199 200 201 202
    .name = "fpreg",
    .get  = get_fpreg,
    .put  = put_fpreg,
};

J
Jianjun Duan 已提交
203 204
static int get_fpreg_1_mmx(QEMUFile *f, void *opaque, size_t size,
                           VMStateField *field)
205 206 207 208 209 210 211 212 213 214
{
    union x86_longdouble *p = opaque;
    uint64_t mant;

    qemu_get_be64s(f, &mant);
    p->mant = mant;
    p->exp = 0xffff;
    return 0;
}

B
Blue Swirl 已提交
215
static const VMStateInfo vmstate_fpreg_1_mmx = {
J
Juan Quintela 已提交
216 217 218 219 220
    .name = "fpreg_1_mmx",
    .get  = get_fpreg_1_mmx,
    .put  = put_fpreg_error,
};

J
Jianjun Duan 已提交
221 222
static int get_fpreg_1_no_mmx(QEMUFile *f, void *opaque, size_t size,
                              VMStateField *field)
223 224 225 226 227 228 229 230 231
{
    union x86_longdouble *p = opaque;
    uint64_t mant;

    qemu_get_be64s(f, &mant);
    fp64_to_fp80(p, mant);
    return 0;
}

B
Blue Swirl 已提交
232
static const VMStateInfo vmstate_fpreg_1_no_mmx = {
J
Juan Quintela 已提交
233 234 235 236 237 238 239
    .name = "fpreg_1_no_mmx",
    .get  = get_fpreg_1_no_mmx,
    .put  = put_fpreg_error,
};

static bool fpregs_is_0(void *opaque, int version_id)
{
240 241
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
J
Juan Quintela 已提交
242 243 244 245 246 247

    return (env->fpregs_format_vmstate == 0);
}

static bool fpregs_is_1_mmx(void *opaque, int version_id)
{
248 249
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
J
Juan Quintela 已提交
250 251 252 253 254 255 256 257 258
    int guess_mmx;

    guess_mmx = ((env->fptag_vmstate == 0xff) &&
                 (env->fpus_vmstate & 0x3800) == 0);
    return (guess_mmx && (env->fpregs_format_vmstate == 1));
}

static bool fpregs_is_1_no_mmx(void *opaque, int version_id)
{
259 260
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
J
Juan Quintela 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    int guess_mmx;

    guess_mmx = ((env->fptag_vmstate == 0xff) &&
                 (env->fpus_vmstate & 0x3800) == 0);
    return (!guess_mmx && (env->fpregs_format_vmstate == 1));
}

#define VMSTATE_FP_REGS(_field, _state, _n)                               \
    VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_0, vmstate_fpreg, FPReg), \
    VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_1_mmx, vmstate_fpreg_1_mmx, FPReg), \
    VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_1_no_mmx, vmstate_fpreg_1_no_mmx, FPReg)

static bool version_is_5(void *opaque, int version_id)
{
    return version_id == 5;
}

#ifdef TARGET_X86_64
static bool less_than_7(void *opaque, int version_id)
{
    return version_id < 7;
}

J
Jianjun Duan 已提交
284 285
static int get_uint64_as_uint32(QEMUFile *f, void *pv, size_t size,
                                VMStateField *field)
J
Juan Quintela 已提交
286 287 288 289 290 291
{
    uint64_t *v = pv;
    *v = qemu_get_be32(f);
    return 0;
}

J
Jianjun Duan 已提交
292 293
static int put_uint64_as_uint32(QEMUFile *f, void *pv, size_t size,
                                VMStateField *field, QJSON *vmdesc)
J
Juan Quintela 已提交
294 295 296
{
    uint64_t *v = pv;
    qemu_put_be32(f, *v);
J
Jianjun Duan 已提交
297 298

    return 0;
J
Juan Quintela 已提交
299 300
}

B
Blue Swirl 已提交
301
static const VMStateInfo vmstate_hack_uint64_as_uint32 = {
J
Juan Quintela 已提交
302 303 304 305 306 307
    .name = "uint64_as_uint32",
    .get  = get_uint64_as_uint32,
    .put  = put_uint64_as_uint32,
};

#define VMSTATE_HACK_UINT32(_f, _s, _t)                                  \
308
    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint64_as_uint32, uint64_t)
J
Juan Quintela 已提交
309 310
#endif

J
Juan Quintela 已提交
311
static void cpu_pre_save(void *opaque)
A
aurel32 已提交
312
{
313 314
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
315
    int i;
A
aurel32 已提交
316 317

    /* FPU */
318
    env->fpus_vmstate = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
319
    env->fptag_vmstate = 0;
A
aurel32 已提交
320
    for(i = 0; i < 8; i++) {
321
        env->fptag_vmstate |= ((!env->fptags[i]) << i);
A
aurel32 已提交
322 323
    }

J
Juan Quintela 已提交
324
    env->fpregs_format_vmstate = 0;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

    /*
     * Real mode guest segments register DPL should be zero.
     * Older KVM version were setting it wrongly.
     * Fixing it will allow live migration to host with unrestricted guest
     * support (otherwise the migration will fail with invalid guest state
     * error).
     */
    if (!(env->cr[0] & CR0_PE_MASK) &&
        (env->segs[R_CS].flags >> DESC_DPL_SHIFT & 3) != 0) {
        env->segs[R_CS].flags &= ~(env->segs[R_CS].flags & DESC_DPL_MASK);
        env->segs[R_DS].flags &= ~(env->segs[R_DS].flags & DESC_DPL_MASK);
        env->segs[R_ES].flags &= ~(env->segs[R_ES].flags & DESC_DPL_MASK);
        env->segs[R_FS].flags &= ~(env->segs[R_FS].flags & DESC_DPL_MASK);
        env->segs[R_GS].flags &= ~(env->segs[R_GS].flags & DESC_DPL_MASK);
        env->segs[R_SS].flags &= ~(env->segs[R_SS].flags & DESC_DPL_MASK);
    }

J
Juan Quintela 已提交
343 344
}

345 346
static int cpu_post_load(void *opaque, int version_id)
{
347
    X86CPU *cpu = opaque;
348
    CPUState *cs = CPU(cpu);
349
    CPUX86State *env = &cpu->env;
350 351
    int i;

352 353 354 355 356 357 358
    if (env->tsc_khz && env->user_tsc_khz &&
        env->tsc_khz != env->user_tsc_khz) {
        error_report("Mismatch between user-specified TSC frequency and "
                     "migrated TSC frequency");
        return -EINVAL;
    }

O
Orit Wasserman 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    /*
     * Real mode guest segments register DPL should be zero.
     * Older KVM version were setting it wrongly.
     * Fixing it will allow live migration from such host that don't have
     * restricted guest support to a host with unrestricted guest support
     * (otherwise the migration will fail with invalid guest state
     * error).
     */
    if (!(env->cr[0] & CR0_PE_MASK) &&
        (env->segs[R_CS].flags >> DESC_DPL_SHIFT & 3) != 0) {
        env->segs[R_CS].flags &= ~(env->segs[R_CS].flags & DESC_DPL_MASK);
        env->segs[R_DS].flags &= ~(env->segs[R_DS].flags & DESC_DPL_MASK);
        env->segs[R_ES].flags &= ~(env->segs[R_ES].flags & DESC_DPL_MASK);
        env->segs[R_FS].flags &= ~(env->segs[R_FS].flags & DESC_DPL_MASK);
        env->segs[R_GS].flags &= ~(env->segs[R_GS].flags & DESC_DPL_MASK);
        env->segs[R_SS].flags &= ~(env->segs[R_SS].flags & DESC_DPL_MASK);
    }

P
Paolo Bonzini 已提交
377 378 379 380 381 382 383 384
    /* Older versions of QEMU incorrectly used CS.DPL as the CPL when
     * running under KVM.  This is wrong for conforming code segments.
     * Luckily, in our implementation the CPL field of hflags is redundant
     * and we can get the right value from the SS descriptor privilege level.
     */
    env->hflags &= ~HF_CPL_MASK;
    env->hflags |= (env->segs[R_SS].flags >> DESC_DPL_SHIFT) & HF_CPL_MASK;

385 386 387 388 389 390
    env->fpstt = (env->fpus_vmstate >> 11) & 7;
    env->fpus = env->fpus_vmstate & ~0x3800;
    env->fptag_vmstate ^= 0xff;
    for(i = 0; i < 8; i++) {
        env->fptags[i] = (env->fptag_vmstate >> i) & 1;
    }
391
    update_fp_status(env);
392

393
    cpu_breakpoint_remove_all(cs, BP_CPU);
394
    cpu_watchpoint_remove_all(cs, BP_CPU);
395 396 397 398 399 400
    {
        /* Indicate all breakpoints disabled, as they are, then
           let the helper re-enable them.  */
        target_ulong dr7 = env->dr[7];
        env->dr[7] = dr7 & ~(DR7_GLOBAL_BP_MASK | DR7_LOCAL_BP_MASK);
        cpu_x86_update_dr7(env, dr7);
401
    }
402
    tlb_flush(cs);
403

404 405 406
    if (tcg_enabled()) {
        cpu_smm_update(cpu);
    }
J
Juan Quintela 已提交
407
    return 0;
408 409
}

410 411
static bool async_pf_msr_needed(void *opaque)
{
412
    X86CPU *cpu = opaque;
413

414
    return cpu->env.async_pf_en_msr != 0;
415 416
}

M
Michael S. Tsirkin 已提交
417 418
static bool pv_eoi_msr_needed(void *opaque)
{
419
    X86CPU *cpu = opaque;
M
Michael S. Tsirkin 已提交
420

421
    return cpu->env.pv_eoi_en_msr != 0;
M
Michael S. Tsirkin 已提交
422 423
}

424 425
static bool steal_time_msr_needed(void *opaque)
{
426
    X86CPU *cpu = opaque;
427

428
    return cpu->env.steal_time_msr != 0;
429 430 431 432 433 434
}

static const VMStateDescription vmstate_steal_time_msr = {
    .name = "cpu/steal_time_msr",
    .version_id = 1,
    .minimum_version_id = 1,
435
    .needed = steal_time_msr_needed,
436
    .fields = (VMStateField[]) {
437
        VMSTATE_UINT64(env.steal_time_msr, X86CPU),
438 439 440 441
        VMSTATE_END_OF_LIST()
    }
};

442 443 444 445
static const VMStateDescription vmstate_async_pf_msr = {
    .name = "cpu/async_pf_msr",
    .version_id = 1,
    .minimum_version_id = 1,
446
    .needed = async_pf_msr_needed,
447
    .fields = (VMStateField[]) {
448
        VMSTATE_UINT64(env.async_pf_en_msr, X86CPU),
449 450 451 452
        VMSTATE_END_OF_LIST()
    }
};

M
Michael S. Tsirkin 已提交
453 454 455 456
static const VMStateDescription vmstate_pv_eoi_msr = {
    .name = "cpu/async_pv_eoi_msr",
    .version_id = 1,
    .minimum_version_id = 1,
457
    .needed = pv_eoi_msr_needed,
458
    .fields = (VMStateField[]) {
459
        VMSTATE_UINT64(env.pv_eoi_en_msr, X86CPU),
M
Michael S. Tsirkin 已提交
460 461 462 463
        VMSTATE_END_OF_LIST()
    }
};

464 465
static bool fpop_ip_dp_needed(void *opaque)
{
466 467
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
468 469 470 471 472 473 474 475

    return env->fpop != 0 || env->fpip != 0 || env->fpdp != 0;
}

static const VMStateDescription vmstate_fpop_ip_dp = {
    .name = "cpu/fpop_ip_dp",
    .version_id = 1,
    .minimum_version_id = 1,
476
    .needed = fpop_ip_dp_needed,
477
    .fields = (VMStateField[]) {
478 479 480
        VMSTATE_UINT16(env.fpop, X86CPU),
        VMSTATE_UINT64(env.fpip, X86CPU),
        VMSTATE_UINT64(env.fpdp, X86CPU),
481 482 483 484
        VMSTATE_END_OF_LIST()
    }
};

485 486
static bool tsc_adjust_needed(void *opaque)
{
487 488
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
489 490 491 492 493 494 495 496

    return env->tsc_adjust != 0;
}

static const VMStateDescription vmstate_msr_tsc_adjust = {
    .name = "cpu/msr_tsc_adjust",
    .version_id = 1,
    .minimum_version_id = 1,
497
    .needed = tsc_adjust_needed,
498
    .fields = (VMStateField[]) {
499
        VMSTATE_UINT64(env.tsc_adjust, X86CPU),
500 501 502 503
        VMSTATE_END_OF_LIST()
    }
};

504 505
static bool tscdeadline_needed(void *opaque)
{
506 507
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
508 509 510 511 512 513 514 515

    return env->tsc_deadline != 0;
}

static const VMStateDescription vmstate_msr_tscdeadline = {
    .name = "cpu/msr_tscdeadline",
    .version_id = 1,
    .minimum_version_id = 1,
516
    .needed = tscdeadline_needed,
517
    .fields = (VMStateField[]) {
518
        VMSTATE_UINT64(env.tsc_deadline, X86CPU),
519 520 521 522
        VMSTATE_END_OF_LIST()
    }
};

A
Avi Kivity 已提交
523 524
static bool misc_enable_needed(void *opaque)
{
525 526
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
A
Avi Kivity 已提交
527 528 529 530

    return env->msr_ia32_misc_enable != MSR_IA32_MISC_ENABLE_DEFAULT;
}

531 532 533 534 535 536 537 538
static bool feature_control_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;

    return env->msr_ia32_feature_control != 0;
}

A
Avi Kivity 已提交
539 540 541 542
static const VMStateDescription vmstate_msr_ia32_misc_enable = {
    .name = "cpu/msr_ia32_misc_enable",
    .version_id = 1,
    .minimum_version_id = 1,
543
    .needed = misc_enable_needed,
544
    .fields = (VMStateField[]) {
545
        VMSTATE_UINT64(env.msr_ia32_misc_enable, X86CPU),
A
Avi Kivity 已提交
546 547 548 549
        VMSTATE_END_OF_LIST()
    }
};

550 551 552 553
static const VMStateDescription vmstate_msr_ia32_feature_control = {
    .name = "cpu/msr_ia32_feature_control",
    .version_id = 1,
    .minimum_version_id = 1,
554
    .needed = feature_control_needed,
555
    .fields = (VMStateField[]) {
556 557 558 559 560
        VMSTATE_UINT64(env.msr_ia32_feature_control, X86CPU),
        VMSTATE_END_OF_LIST()
    }
};

P
Paolo Bonzini 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
static bool pmu_enable_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
    int i;

    if (env->msr_fixed_ctr_ctrl || env->msr_global_ctrl ||
        env->msr_global_status || env->msr_global_ovf_ctrl) {
        return true;
    }
    for (i = 0; i < MAX_FIXED_COUNTERS; i++) {
        if (env->msr_fixed_counters[i]) {
            return true;
        }
    }
    for (i = 0; i < MAX_GP_COUNTERS; i++) {
        if (env->msr_gp_counters[i] || env->msr_gp_evtsel[i]) {
            return true;
        }
    }

    return false;
}

static const VMStateDescription vmstate_msr_architectural_pmu = {
    .name = "cpu/msr_architectural_pmu",
    .version_id = 1,
    .minimum_version_id = 1,
589
    .needed = pmu_enable_needed,
590
    .fields = (VMStateField[]) {
P
Paolo Bonzini 已提交
591 592 593 594 595 596 597 598 599 600 601
        VMSTATE_UINT64(env.msr_fixed_ctr_ctrl, X86CPU),
        VMSTATE_UINT64(env.msr_global_ctrl, X86CPU),
        VMSTATE_UINT64(env.msr_global_status, X86CPU),
        VMSTATE_UINT64(env.msr_global_ovf_ctrl, X86CPU),
        VMSTATE_UINT64_ARRAY(env.msr_fixed_counters, X86CPU, MAX_FIXED_COUNTERS),
        VMSTATE_UINT64_ARRAY(env.msr_gp_counters, X86CPU, MAX_GP_COUNTERS),
        VMSTATE_UINT64_ARRAY(env.msr_gp_evtsel, X86CPU, MAX_GP_COUNTERS),
        VMSTATE_END_OF_LIST()
    }
};

L
Liu Jinsong 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
static bool mpx_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
    unsigned int i;

    for (i = 0; i < 4; i++) {
        if (env->bnd_regs[i].lb || env->bnd_regs[i].ub) {
            return true;
        }
    }

    if (env->bndcs_regs.cfgu || env->bndcs_regs.sts) {
        return true;
    }

    return !!env->msr_bndcfgs;
}

static const VMStateDescription vmstate_mpx = {
    .name = "cpu/mpx",
    .version_id = 1,
    .minimum_version_id = 1,
625
    .needed = mpx_needed,
626
    .fields = (VMStateField[]) {
L
Liu Jinsong 已提交
627 628 629 630 631 632 633 634
        VMSTATE_BND_REGS(env.bnd_regs, X86CPU, 4),
        VMSTATE_UINT64(env.bndcs_regs.cfgu, X86CPU),
        VMSTATE_UINT64(env.bndcs_regs.sts, X86CPU),
        VMSTATE_UINT64(env.msr_bndcfgs, X86CPU),
        VMSTATE_END_OF_LIST()
    }
};

635 636 637 638 639 640 641 642 643 644 645 646
static bool hyperv_hypercall_enable_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;

    return env->msr_hv_hypercall != 0 || env->msr_hv_guest_os_id != 0;
}

static const VMStateDescription vmstate_msr_hypercall_hypercall = {
    .name = "cpu/msr_hyperv_hypercall",
    .version_id = 1,
    .minimum_version_id = 1,
647
    .needed = hyperv_hypercall_enable_needed,
648
    .fields = (VMStateField[]) {
649
        VMSTATE_UINT64(env.msr_hv_guest_os_id, X86CPU),
650
        VMSTATE_UINT64(env.msr_hv_hypercall, X86CPU),
651 652 653 654
        VMSTATE_END_OF_LIST()
    }
};

655 656 657 658 659 660 661 662 663 664 665 666
static bool hyperv_vapic_enable_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;

    return env->msr_hv_vapic != 0;
}

static const VMStateDescription vmstate_msr_hyperv_vapic = {
    .name = "cpu/msr_hyperv_vapic",
    .version_id = 1,
    .minimum_version_id = 1,
667
    .needed = hyperv_vapic_enable_needed,
668
    .fields = (VMStateField[]) {
669 670 671 672 673
        VMSTATE_UINT64(env.msr_hv_vapic, X86CPU),
        VMSTATE_END_OF_LIST()
    }
};

674 675 676 677 678 679 680 681 682 683 684 685
static bool hyperv_time_enable_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;

    return env->msr_hv_tsc != 0;
}

static const VMStateDescription vmstate_msr_hyperv_time = {
    .name = "cpu/msr_hyperv_time",
    .version_id = 1,
    .minimum_version_id = 1,
686
    .needed = hyperv_time_enable_needed,
687
    .fields = (VMStateField[]) {
688 689 690 691 692
        VMSTATE_UINT64(env.msr_hv_tsc, X86CPU),
        VMSTATE_END_OF_LIST()
    }
};

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
static bool hyperv_crash_enable_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
    int i;

    for (i = 0; i < HV_X64_MSR_CRASH_PARAMS; i++) {
        if (env->msr_hv_crash_params[i]) {
            return true;
        }
    }
    return false;
}

static const VMStateDescription vmstate_msr_hyperv_crash = {
    .name = "cpu/msr_hyperv_crash",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = hyperv_crash_enable_needed,
    .fields = (VMStateField[]) {
        VMSTATE_UINT64_ARRAY(env.msr_hv_crash_params,
                             X86CPU, HV_X64_MSR_CRASH_PARAMS),
        VMSTATE_END_OF_LIST()
    }
};

719 720 721 722 723
static bool hyperv_runtime_enable_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;

724 725 726 727
    if (!cpu->hyperv_runtime) {
        return false;
    }

728 729 730 731 732 733 734 735 736 737 738 739 740 741
    return env->msr_hv_runtime != 0;
}

static const VMStateDescription vmstate_msr_hyperv_runtime = {
    .name = "cpu/msr_hyperv_runtime",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = hyperv_runtime_enable_needed,
    .fields = (VMStateField[]) {
        VMSTATE_UINT64(env.msr_hv_runtime, X86CPU),
        VMSTATE_END_OF_LIST()
    }
};

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
static bool hyperv_synic_enable_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
    int i;

    if (env->msr_hv_synic_control != 0 ||
        env->msr_hv_synic_evt_page != 0 ||
        env->msr_hv_synic_msg_page != 0) {
        return true;
    }

    for (i = 0; i < ARRAY_SIZE(env->msr_hv_synic_sint); i++) {
        if (env->msr_hv_synic_sint[i] != 0) {
            return true;
        }
    }

    return false;
}

static const VMStateDescription vmstate_msr_hyperv_synic = {
    .name = "cpu/msr_hyperv_synic",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = hyperv_synic_enable_needed,
    .fields = (VMStateField[]) {
        VMSTATE_UINT64(env.msr_hv_synic_control, X86CPU),
        VMSTATE_UINT64(env.msr_hv_synic_evt_page, X86CPU),
        VMSTATE_UINT64(env.msr_hv_synic_msg_page, X86CPU),
        VMSTATE_UINT64_ARRAY(env.msr_hv_synic_sint, X86CPU,
                             HV_SYNIC_SINT_COUNT),
        VMSTATE_END_OF_LIST()
    }
};

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
static bool hyperv_stimer_enable_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
    int i;

    for (i = 0; i < ARRAY_SIZE(env->msr_hv_stimer_config); i++) {
        if (env->msr_hv_stimer_config[i] || env->msr_hv_stimer_count[i]) {
            return true;
        }
    }
    return false;
}

static const VMStateDescription vmstate_msr_hyperv_stimer = {
    .name = "cpu/msr_hyperv_stimer",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = hyperv_stimer_enable_needed,
    .fields = (VMStateField[]) {
        VMSTATE_UINT64_ARRAY(env.msr_hv_stimer_config,
                             X86CPU, HV_SYNIC_STIMER_COUNT),
        VMSTATE_UINT64_ARRAY(env.msr_hv_stimer_count,
                             X86CPU, HV_SYNIC_STIMER_COUNT),
        VMSTATE_END_OF_LIST()
    }
};

C
Chao Peng 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818
static bool avx512_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
    unsigned int i;

    for (i = 0; i < NB_OPMASK_REGS; i++) {
        if (env->opmask_regs[i]) {
            return true;
        }
    }

    for (i = 0; i < CPU_NB_REGS; i++) {
819
#define ENV_XMM(reg, field) (env->xmm_regs[reg].ZMM_Q(field))
820 821
        if (ENV_XMM(i, 4) || ENV_XMM(i, 6) ||
            ENV_XMM(i, 5) || ENV_XMM(i, 7)) {
C
Chao Peng 已提交
822 823 824
            return true;
        }
#ifdef TARGET_X86_64
825 826 827 828
        if (ENV_XMM(i+16, 0) || ENV_XMM(i+16, 1) ||
            ENV_XMM(i+16, 2) || ENV_XMM(i+16, 3) ||
            ENV_XMM(i+16, 4) || ENV_XMM(i+16, 5) ||
            ENV_XMM(i+16, 6) || ENV_XMM(i+16, 7)) {
C
Chao Peng 已提交
829 830 831 832 833 834 835 836 837 838 839 840
            return true;
        }
#endif
    }

    return false;
}

static const VMStateDescription vmstate_avx512 = {
    .name = "cpu/avx512",
    .version_id = 1,
    .minimum_version_id = 1,
841
    .needed = avx512_needed,
C
Chao Peng 已提交
842 843
    .fields = (VMStateField[]) {
        VMSTATE_UINT64_ARRAY(env.opmask_regs, X86CPU, NB_OPMASK_REGS),
844
        VMSTATE_ZMMH_REGS_VARS(env.xmm_regs, X86CPU, 0),
C
Chao Peng 已提交
845
#ifdef TARGET_X86_64
846
        VMSTATE_Hi16_ZMM_REGS_VARS(env.xmm_regs, X86CPU, 16),
C
Chao Peng 已提交
847 848 849 850 851
#endif
        VMSTATE_END_OF_LIST()
    }
};

852 853 854 855 856 857 858 859 860 861 862 863
static bool xss_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;

    return env->xss != 0;
}

static const VMStateDescription vmstate_xss = {
    .name = "cpu/xss",
    .version_id = 1,
    .minimum_version_id = 1,
864
    .needed = xss_needed,
865 866 867 868 869 870
    .fields = (VMStateField[]) {
        VMSTATE_UINT64(env.xss, X86CPU),
        VMSTATE_END_OF_LIST()
    }
};

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
#ifdef TARGET_X86_64
static bool pkru_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;

    return env->pkru != 0;
}

static const VMStateDescription vmstate_pkru = {
    .name = "cpu/pkru",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = pkru_needed,
    .fields = (VMStateField[]){
        VMSTATE_UINT32(env.pkru, X86CPU),
        VMSTATE_END_OF_LIST()
    }
};
#endif

892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
static bool tsc_khz_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
    MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
    PCMachineClass *pcmc = PC_MACHINE_CLASS(mc);
    return env->tsc_khz && pcmc->save_tsc_khz;
}

static const VMStateDescription vmstate_tsc_khz = {
    .name = "cpu/tsc_khz",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = tsc_khz_needed,
    .fields = (VMStateField[]) {
        VMSTATE_INT64(env.tsc_khz, X86CPU),
        VMSTATE_END_OF_LIST()
    }
};

912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
static bool mcg_ext_ctl_needed(void *opaque)
{
    X86CPU *cpu = opaque;
    CPUX86State *env = &cpu->env;
    return cpu->enable_lmce && env->mcg_ext_ctl;
}

static const VMStateDescription vmstate_mcg_ext_ctl = {
    .name = "cpu/mcg_ext_ctl",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = mcg_ext_ctl_needed,
    .fields = (VMStateField[]) {
        VMSTATE_UINT64(env.mcg_ext_ctl, X86CPU),
        VMSTATE_END_OF_LIST()
    }
};

930
VMStateDescription vmstate_x86_cpu = {
J
Juan Quintela 已提交
931
    .name = "cpu",
932
    .version_id = 12,
J
Juan Quintela 已提交
933 934 935
    .minimum_version_id = 3,
    .pre_save = cpu_pre_save,
    .post_load = cpu_post_load,
936
    .fields = (VMStateField[]) {
937 938 939 940
        VMSTATE_UINTTL_ARRAY(env.regs, X86CPU, CPU_NB_REGS),
        VMSTATE_UINTTL(env.eip, X86CPU),
        VMSTATE_UINTTL(env.eflags, X86CPU),
        VMSTATE_UINT32(env.hflags, X86CPU),
J
Juan Quintela 已提交
941
        /* FPU */
942 943 944 945 946 947 948 949 950 951 952 953 954
        VMSTATE_UINT16(env.fpuc, X86CPU),
        VMSTATE_UINT16(env.fpus_vmstate, X86CPU),
        VMSTATE_UINT16(env.fptag_vmstate, X86CPU),
        VMSTATE_UINT16(env.fpregs_format_vmstate, X86CPU),
        VMSTATE_FP_REGS(env.fpregs, X86CPU, 8),

        VMSTATE_SEGMENT_ARRAY(env.segs, X86CPU, 6),
        VMSTATE_SEGMENT(env.ldt, X86CPU),
        VMSTATE_SEGMENT(env.tr, X86CPU),
        VMSTATE_SEGMENT(env.gdt, X86CPU),
        VMSTATE_SEGMENT(env.idt, X86CPU),

        VMSTATE_UINT32(env.sysenter_cs, X86CPU),
J
Juan Quintela 已提交
955 956
#ifdef TARGET_X86_64
        /* Hack: In v7 size changed from 32 to 64 bits on x86_64 */
957 958 959 960
        VMSTATE_HACK_UINT32(env.sysenter_esp, X86CPU, less_than_7),
        VMSTATE_HACK_UINT32(env.sysenter_eip, X86CPU, less_than_7),
        VMSTATE_UINTTL_V(env.sysenter_esp, X86CPU, 7),
        VMSTATE_UINTTL_V(env.sysenter_eip, X86CPU, 7),
A
aurel32 已提交
961
#else
962 963
        VMSTATE_UINTTL(env.sysenter_esp, X86CPU),
        VMSTATE_UINTTL(env.sysenter_eip, X86CPU),
964
#endif
A
aurel32 已提交
965

966 967 968 969 970
        VMSTATE_UINTTL(env.cr[0], X86CPU),
        VMSTATE_UINTTL(env.cr[2], X86CPU),
        VMSTATE_UINTTL(env.cr[3], X86CPU),
        VMSTATE_UINTTL(env.cr[4], X86CPU),
        VMSTATE_UINTTL_ARRAY(env.dr, X86CPU, 8),
J
Juan Quintela 已提交
971
        /* MMU */
972
        VMSTATE_INT32(env.a20_mask, X86CPU),
J
Juan Quintela 已提交
973
        /* XMM */
974
        VMSTATE_UINT32(env.mxcsr, X86CPU),
975
        VMSTATE_XMM_REGS(env.xmm_regs, X86CPU, 0),
A
aurel32 已提交
976 977

#ifdef TARGET_X86_64
978 979 980 981 982 983
        VMSTATE_UINT64(env.efer, X86CPU),
        VMSTATE_UINT64(env.star, X86CPU),
        VMSTATE_UINT64(env.lstar, X86CPU),
        VMSTATE_UINT64(env.cstar, X86CPU),
        VMSTATE_UINT64(env.fmask, X86CPU),
        VMSTATE_UINT64(env.kernelgsbase, X86CPU),
A
aurel32 已提交
984
#endif
985 986 987 988 989
        VMSTATE_UINT32_V(env.smbase, X86CPU, 4),

        VMSTATE_UINT64_V(env.pat, X86CPU, 5),
        VMSTATE_UINT32_V(env.hflags2, X86CPU, 5),

990
        VMSTATE_UINT32_TEST(parent_obj.halted, X86CPU, version_is_5),
991 992 993 994 995 996 997 998 999 1000
        VMSTATE_UINT64_V(env.vm_hsave, X86CPU, 5),
        VMSTATE_UINT64_V(env.vm_vmcb, X86CPU, 5),
        VMSTATE_UINT64_V(env.tsc_offset, X86CPU, 5),
        VMSTATE_UINT64_V(env.intercept, X86CPU, 5),
        VMSTATE_UINT16_V(env.intercept_cr_read, X86CPU, 5),
        VMSTATE_UINT16_V(env.intercept_cr_write, X86CPU, 5),
        VMSTATE_UINT16_V(env.intercept_dr_read, X86CPU, 5),
        VMSTATE_UINT16_V(env.intercept_dr_write, X86CPU, 5),
        VMSTATE_UINT32_V(env.intercept_exceptions, X86CPU, 5),
        VMSTATE_UINT8_V(env.v_tpr, X86CPU, 5),
1001
        /* MTRRs */
1002 1003
        VMSTATE_UINT64_ARRAY_V(env.mtrr_fixed, X86CPU, 11, 8),
        VMSTATE_UINT64_V(env.mtrr_deftype, X86CPU, 8),
1004
        VMSTATE_MTRR_VARS(env.mtrr_var, X86CPU, MSR_MTRRcap_VCNT, 8),
J
Juan Quintela 已提交
1005
        /* KVM-related states */
1006 1007 1008 1009 1010 1011 1012 1013 1014
        VMSTATE_INT32_V(env.interrupt_injected, X86CPU, 9),
        VMSTATE_UINT32_V(env.mp_state, X86CPU, 9),
        VMSTATE_UINT64_V(env.tsc, X86CPU, 9),
        VMSTATE_INT32_V(env.exception_injected, X86CPU, 11),
        VMSTATE_UINT8_V(env.soft_interrupt, X86CPU, 11),
        VMSTATE_UINT8_V(env.nmi_injected, X86CPU, 11),
        VMSTATE_UINT8_V(env.nmi_pending, X86CPU, 11),
        VMSTATE_UINT8_V(env.has_error_code, X86CPU, 11),
        VMSTATE_UINT32_V(env.sipi_vector, X86CPU, 11),
J
Juan Quintela 已提交
1015
        /* MCE */
1016 1017 1018 1019
        VMSTATE_UINT64_V(env.mcg_cap, X86CPU, 10),
        VMSTATE_UINT64_V(env.mcg_status, X86CPU, 10),
        VMSTATE_UINT64_V(env.mcg_ctl, X86CPU, 10),
        VMSTATE_UINT64_ARRAY_V(env.mce_banks, X86CPU, MCE_BANKS_DEF * 4, 10),
J
Juan Quintela 已提交
1020
        /* rdtscp */
1021
        VMSTATE_UINT64_V(env.tsc_aux, X86CPU, 11),
1022
        /* KVM pvclock msr */
1023 1024
        VMSTATE_UINT64_V(env.system_time_msr, X86CPU, 11),
        VMSTATE_UINT64_V(env.wall_clock_msr, X86CPU, 11),
1025
        /* XSAVE related fields */
1026 1027
        VMSTATE_UINT64_V(env.xcr0, X86CPU, 12),
        VMSTATE_UINT64_V(env.xstate_bv, X86CPU, 12),
1028
        VMSTATE_YMMH_REGS_VARS(env.xmm_regs, X86CPU, 0, 12),
J
Juan Quintela 已提交
1029
        VMSTATE_END_OF_LIST()
1030
        /* The above list is not sorted /wrt version numbers, watch out! */
1031
    },
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
    .subsections = (const VMStateDescription*[]) {
        &vmstate_async_pf_msr,
        &vmstate_pv_eoi_msr,
        &vmstate_steal_time_msr,
        &vmstate_fpop_ip_dp,
        &vmstate_msr_tsc_adjust,
        &vmstate_msr_tscdeadline,
        &vmstate_msr_ia32_misc_enable,
        &vmstate_msr_ia32_feature_control,
        &vmstate_msr_architectural_pmu,
        &vmstate_mpx,
        &vmstate_msr_hypercall_hypercall,
        &vmstate_msr_hyperv_vapic,
        &vmstate_msr_hyperv_time,
1046
        &vmstate_msr_hyperv_crash,
1047
        &vmstate_msr_hyperv_runtime,
1048
        &vmstate_msr_hyperv_synic,
1049
        &vmstate_msr_hyperv_stimer,
1050 1051
        &vmstate_avx512,
        &vmstate_xss,
1052
        &vmstate_tsc_khz,
1053 1054 1055
#ifdef TARGET_X86_64
        &vmstate_pkru,
#endif
1056
        &vmstate_mcg_ext_ctl,
1057
        NULL
1058
    }
J
Juan Quintela 已提交
1059
};