tcg/arm: fix condition in zero/sign extension functions
[qemu.git] / target-i386 / machine.c
blobb547e2ac7ac4fa97444634f317ec516caba3c1c2
1 #include "hw/hw.h"
2 #include "hw/boards.h"
3 #include "hw/pc.h"
4 #include "hw/isa.h"
6 #include "exec-all.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 static const VMStateDescription vmstate_mtrr_var = {
51 .name = "mtrr_var",
52 .version_id = 1,
53 .minimum_version_id = 1,
54 .minimum_version_id_old = 1,
55 .fields = (VMStateField []) {
56 VMSTATE_UINT64(base, MTRRVar),
57 VMSTATE_UINT64(mask, MTRRVar),
58 VMSTATE_END_OF_LIST()
62 #define VMSTATE_MTRR_VARS(_field, _state, _n, _v) \
63 VMSTATE_STRUCT_ARRAY(_field, _state, _n, _v, vmstate_mtrr_var, MTRRVar)
65 static void put_fpreg_error(QEMUFile *f, void *opaque, size_t size)
67 fprintf(stderr, "call put_fpreg() with invalid arguments\n");
68 exit(0);
71 #ifdef USE_X86LDOUBLE
72 /* XXX: add that in a FPU generic layer */
73 union x86_longdouble {
74 uint64_t mant;
75 uint16_t exp;
78 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
79 #define EXPBIAS1 1023
80 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
81 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
83 static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
85 int e;
86 /* mantissa */
87 p->mant = (MANTD1(temp) << 11) | (1LL << 63);
88 /* exponent + sign */
89 e = EXPD1(temp) - EXPBIAS1 + 16383;
90 e |= SIGND1(temp) >> 16;
91 p->exp = e;
94 static int get_fpreg(QEMUFile *f, void *opaque, size_t size)
96 FPReg *fp_reg = opaque;
97 uint64_t mant;
98 uint16_t exp;
100 qemu_get_be64s(f, &mant);
101 qemu_get_be16s(f, &exp);
102 fp_reg->d = cpu_set_fp80(mant, exp);
103 return 0;
106 static void put_fpreg(QEMUFile *f, void *opaque, size_t size)
108 FPReg *fp_reg = opaque;
109 uint64_t mant;
110 uint16_t exp;
111 /* we save the real CPU data (in case of MMX usage only 'mant'
112 contains the MMX register */
113 cpu_get_fp80(&mant, &exp, fp_reg->d);
114 qemu_put_be64s(f, &mant);
115 qemu_put_be16s(f, &exp);
118 static const VMStateInfo vmstate_fpreg = {
119 .name = "fpreg",
120 .get = get_fpreg,
121 .put = put_fpreg,
124 static int get_fpreg_1_mmx(QEMUFile *f, void *opaque, size_t size)
126 union x86_longdouble *p = opaque;
127 uint64_t mant;
129 qemu_get_be64s(f, &mant);
130 p->mant = mant;
131 p->exp = 0xffff;
132 return 0;
135 static const VMStateInfo vmstate_fpreg_1_mmx = {
136 .name = "fpreg_1_mmx",
137 .get = get_fpreg_1_mmx,
138 .put = put_fpreg_error,
141 static int get_fpreg_1_no_mmx(QEMUFile *f, void *opaque, size_t size)
143 union x86_longdouble *p = opaque;
144 uint64_t mant;
146 qemu_get_be64s(f, &mant);
147 fp64_to_fp80(p, mant);
148 return 0;
151 static const VMStateInfo vmstate_fpreg_1_no_mmx = {
152 .name = "fpreg_1_no_mmx",
153 .get = get_fpreg_1_no_mmx,
154 .put = put_fpreg_error,
157 static bool fpregs_is_0(void *opaque, int version_id)
159 CPUState *env = opaque;
161 return (env->fpregs_format_vmstate == 0);
164 static bool fpregs_is_1_mmx(void *opaque, int version_id)
166 CPUState *env = opaque;
167 int guess_mmx;
169 guess_mmx = ((env->fptag_vmstate == 0xff) &&
170 (env->fpus_vmstate & 0x3800) == 0);
171 return (guess_mmx && (env->fpregs_format_vmstate == 1));
174 static bool fpregs_is_1_no_mmx(void *opaque, int version_id)
176 CPUState *env = opaque;
177 int guess_mmx;
179 guess_mmx = ((env->fptag_vmstate == 0xff) &&
180 (env->fpus_vmstate & 0x3800) == 0);
181 return (!guess_mmx && (env->fpregs_format_vmstate == 1));
184 #define VMSTATE_FP_REGS(_field, _state, _n) \
185 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_0, vmstate_fpreg, FPReg), \
186 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_1_mmx, vmstate_fpreg_1_mmx, FPReg), \
187 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_1_no_mmx, vmstate_fpreg_1_no_mmx, FPReg)
189 #else
190 static int get_fpreg(QEMUFile *f, void *opaque, size_t size)
192 FPReg *fp_reg = opaque;
194 qemu_get_be64s(f, &fp_reg->mmx.MMX_Q(0));
195 return 0;
198 static void put_fpreg(QEMUFile *f, void *opaque, size_t size)
200 FPReg *fp_reg = opaque;
201 /* if we use doubles for float emulation, we save the doubles to
202 avoid losing information in case of MMX usage. It can give
203 problems if the image is restored on a CPU where long
204 doubles are used instead. */
205 qemu_put_be64s(f, &fp_reg->mmx.MMX_Q(0));
208 const VMStateInfo vmstate_fpreg = {
209 .name = "fpreg",
210 .get = get_fpreg,
211 .put = put_fpreg,
214 static int get_fpreg_0_mmx(QEMUFile *f, void *opaque, size_t size)
216 FPReg *fp_reg = opaque;
217 uint64_t mant;
218 uint16_t exp;
220 qemu_get_be64s(f, &mant);
221 qemu_get_be16s(f, &exp);
222 fp_reg->mmx.MMX_Q(0) = mant;
223 return 0;
226 const VMStateInfo vmstate_fpreg_0_mmx = {
227 .name = "fpreg_0_mmx",
228 .get = get_fpreg_0_mmx,
229 .put = put_fpreg_error,
232 static int get_fpreg_0_no_mmx(QEMUFile *f, void *opaque, size_t size)
234 FPReg *fp_reg = opaque;
235 uint64_t mant;
236 uint16_t exp;
238 qemu_get_be64s(f, &mant);
239 qemu_get_be16s(f, &exp);
241 fp_reg->d = cpu_set_fp80(mant, exp);
242 return 0;
245 const VMStateInfo vmstate_fpreg_0_no_mmx = {
246 .name = "fpreg_0_no_mmx",
247 .get = get_fpreg_0_no_mmx,
248 .put = put_fpreg_error,
251 static bool fpregs_is_1(void *opaque, int version_id)
253 CPUState *env = opaque;
255 return env->fpregs_format_vmstate == 1;
258 static bool fpregs_is_0_mmx(void *opaque, int version_id)
260 CPUState *env = opaque;
261 int guess_mmx;
263 guess_mmx = ((env->fptag_vmstate == 0xff) &&
264 (env->fpus_vmstate & 0x3800) == 0);
265 return guess_mmx && env->fpregs_format_vmstate == 0;
268 static bool fpregs_is_0_no_mmx(void *opaque, int version_id)
270 CPUState *env = opaque;
271 int guess_mmx;
273 guess_mmx = ((env->fptag_vmstate == 0xff) &&
274 (env->fpus_vmstate & 0x3800) == 0);
275 return !guess_mmx && env->fpregs_format_vmstate == 0;
278 #define VMSTATE_FP_REGS(_field, _state, _n) \
279 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_1, vmstate_fpreg, FPReg), \
280 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_0_mmx, vmstate_fpreg_0_mmx, FPReg), \
281 VMSTATE_ARRAY_TEST(_field, _state, _n, fpregs_is_0_no_mmx, vmstate_fpreg_0_no_mmx, FPReg)
283 #endif /* USE_X86LDOUBLE */
285 static bool version_is_5(void *opaque, int version_id)
287 return version_id == 5;
290 #ifdef TARGET_X86_64
291 static bool less_than_7(void *opaque, int version_id)
293 return version_id < 7;
296 static int get_uint64_as_uint32(QEMUFile *f, void *pv, size_t size)
298 uint64_t *v = pv;
299 *v = qemu_get_be32(f);
300 return 0;
303 static void put_uint64_as_uint32(QEMUFile *f, void *pv, size_t size)
305 uint64_t *v = pv;
306 qemu_put_be32(f, *v);
309 static const VMStateInfo vmstate_hack_uint64_as_uint32 = {
310 .name = "uint64_as_uint32",
311 .get = get_uint64_as_uint32,
312 .put = put_uint64_as_uint32,
315 #define VMSTATE_HACK_UINT32(_f, _s, _t) \
316 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint64_as_uint32, uint64_t)
317 #endif
319 static void cpu_pre_save(void *opaque)
321 CPUState *env = opaque;
322 int i;
324 /* FPU */
325 env->fpus_vmstate = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
326 env->fptag_vmstate = 0;
327 for(i = 0; i < 8; i++) {
328 env->fptag_vmstate |= ((!env->fptags[i]) << i);
331 #ifdef USE_X86LDOUBLE
332 env->fpregs_format_vmstate = 0;
333 #else
334 env->fpregs_format_vmstate = 1;
335 #endif
338 static int cpu_post_load(void *opaque, int version_id)
340 CPUState *env = opaque;
341 int i;
343 /* XXX: restore FPU round state */
344 env->fpstt = (env->fpus_vmstate >> 11) & 7;
345 env->fpus = env->fpus_vmstate & ~0x3800;
346 env->fptag_vmstate ^= 0xff;
347 for(i = 0; i < 8; i++) {
348 env->fptags[i] = (env->fptag_vmstate >> i) & 1;
351 cpu_breakpoint_remove_all(env, BP_CPU);
352 cpu_watchpoint_remove_all(env, BP_CPU);
353 for (i = 0; i < 4; i++)
354 hw_breakpoint_insert(env, i);
356 tlb_flush(env, 1);
357 return 0;
360 static const VMStateDescription vmstate_cpu = {
361 .name = "cpu",
362 .version_id = CPU_SAVE_VERSION,
363 .minimum_version_id = 3,
364 .minimum_version_id_old = 3,
365 .pre_save = cpu_pre_save,
366 .post_load = cpu_post_load,
367 .fields = (VMStateField []) {
368 VMSTATE_UINTTL_ARRAY(regs, CPUState, CPU_NB_REGS),
369 VMSTATE_UINTTL(eip, CPUState),
370 VMSTATE_UINTTL(eflags, CPUState),
371 VMSTATE_UINT32(hflags, CPUState),
372 /* FPU */
373 VMSTATE_UINT16(fpuc, CPUState),
374 VMSTATE_UINT16(fpus_vmstate, CPUState),
375 VMSTATE_UINT16(fptag_vmstate, CPUState),
376 VMSTATE_UINT16(fpregs_format_vmstate, CPUState),
377 VMSTATE_FP_REGS(fpregs, CPUState, 8),
379 VMSTATE_SEGMENT_ARRAY(segs, CPUState, 6),
380 VMSTATE_SEGMENT(ldt, CPUState),
381 VMSTATE_SEGMENT(tr, CPUState),
382 VMSTATE_SEGMENT(gdt, CPUState),
383 VMSTATE_SEGMENT(idt, CPUState),
385 VMSTATE_UINT32(sysenter_cs, CPUState),
386 #ifdef TARGET_X86_64
387 /* Hack: In v7 size changed from 32 to 64 bits on x86_64 */
388 VMSTATE_HACK_UINT32(sysenter_esp, CPUState, less_than_7),
389 VMSTATE_HACK_UINT32(sysenter_eip, CPUState, less_than_7),
390 VMSTATE_UINTTL_V(sysenter_esp, CPUState, 7),
391 VMSTATE_UINTTL_V(sysenter_eip, CPUState, 7),
392 #else
393 VMSTATE_UINTTL(sysenter_esp, CPUState),
394 VMSTATE_UINTTL(sysenter_eip, CPUState),
395 #endif
397 VMSTATE_UINTTL(cr[0], CPUState),
398 VMSTATE_UINTTL(cr[2], CPUState),
399 VMSTATE_UINTTL(cr[3], CPUState),
400 VMSTATE_UINTTL(cr[4], CPUState),
401 VMSTATE_UINTTL_ARRAY(dr, CPUState, 8),
402 /* MMU */
403 VMSTATE_INT32(a20_mask, CPUState),
404 /* XMM */
405 VMSTATE_UINT32(mxcsr, CPUState),
406 VMSTATE_XMM_REGS(xmm_regs, CPUState, CPU_NB_REGS),
408 #ifdef TARGET_X86_64
409 VMSTATE_UINT64(efer, CPUState),
410 VMSTATE_UINT64(star, CPUState),
411 VMSTATE_UINT64(lstar, CPUState),
412 VMSTATE_UINT64(cstar, CPUState),
413 VMSTATE_UINT64(fmask, CPUState),
414 VMSTATE_UINT64(kernelgsbase, CPUState),
415 #endif
416 VMSTATE_UINT32_V(smbase, CPUState, 4),
418 VMSTATE_UINT64_V(pat, CPUState, 5),
419 VMSTATE_UINT32_V(hflags2, CPUState, 5),
421 VMSTATE_UINT32_TEST(halted, CPUState, version_is_5),
422 VMSTATE_UINT64_V(vm_hsave, CPUState, 5),
423 VMSTATE_UINT64_V(vm_vmcb, CPUState, 5),
424 VMSTATE_UINT64_V(tsc_offset, CPUState, 5),
425 VMSTATE_UINT64_V(intercept, CPUState, 5),
426 VMSTATE_UINT16_V(intercept_cr_read, CPUState, 5),
427 VMSTATE_UINT16_V(intercept_cr_write, CPUState, 5),
428 VMSTATE_UINT16_V(intercept_dr_read, CPUState, 5),
429 VMSTATE_UINT16_V(intercept_dr_write, CPUState, 5),
430 VMSTATE_UINT32_V(intercept_exceptions, CPUState, 5),
431 VMSTATE_UINT8_V(v_tpr, CPUState, 5),
432 /* MTRRs */
433 VMSTATE_UINT64_ARRAY_V(mtrr_fixed, CPUState, 11, 8),
434 VMSTATE_UINT64_V(mtrr_deftype, CPUState, 8),
435 VMSTATE_MTRR_VARS(mtrr_var, CPUState, 8, 8),
436 /* KVM-related states */
437 VMSTATE_INT32_V(interrupt_injected, CPUState, 9),
438 VMSTATE_UINT32_V(mp_state, CPUState, 9),
439 VMSTATE_UINT64_V(tsc, CPUState, 9),
440 VMSTATE_INT32_V(exception_injected, CPUState, 11),
441 VMSTATE_UINT8_V(soft_interrupt, CPUState, 11),
442 VMSTATE_UINT8_V(nmi_injected, CPUState, 11),
443 VMSTATE_UINT8_V(nmi_pending, CPUState, 11),
444 VMSTATE_UINT8_V(has_error_code, CPUState, 11),
445 VMSTATE_UINT32_V(sipi_vector, CPUState, 11),
446 /* MCE */
447 VMSTATE_UINT64_V(mcg_cap, CPUState, 10),
448 VMSTATE_UINT64_V(mcg_status, CPUState, 10),
449 VMSTATE_UINT64_V(mcg_ctl, CPUState, 10),
450 VMSTATE_UINT64_ARRAY_V(mce_banks, CPUState, MCE_BANKS_DEF *4, 10),
451 /* rdtscp */
452 VMSTATE_UINT64_V(tsc_aux, CPUState, 11),
453 /* KVM pvclock msr */
454 VMSTATE_UINT64_V(system_time_msr, CPUState, 11),
455 VMSTATE_UINT64_V(wall_clock_msr, CPUState, 11),
456 VMSTATE_END_OF_LIST()
457 /* The above list is not sorted /wrt version numbers, watch out! */
461 void cpu_save(QEMUFile *f, void *opaque)
463 vmstate_save_state(f, &vmstate_cpu, opaque);
466 int cpu_load(QEMUFile *f, void *opaque, int version_id)
468 return vmstate_load_state(f, &vmstate_cpu, opaque, version_id);