]> git.proxmox.com Git - mirror_qemu.git/blame - target-i386/exec.h
added SMM support
[mirror_qemu.git] / target-i386 / exec.h
CommitLineData
2c0262af
FB
1/*
2 * i386 execution defines
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
7d3505c5 20#include "config.h"
2c0262af
FB
21#include "dyngen-exec.h"
22
14ce26e7 23/* XXX: factorize this mess */
14ce26e7
FB
24#ifdef TARGET_X86_64
25#define TARGET_LONG_BITS 64
26#else
27#define TARGET_LONG_BITS 32
28#endif
29
d785e6be
FB
30#include "cpu-defs.h"
31
0d1a29f9 32/* at least 4 register variables are defined */
2c0262af 33register struct CPUX86State *env asm(AREG0);
14ce26e7 34
d785e6be
FB
35#if TARGET_LONG_BITS > HOST_LONG_BITS
36
37/* no registers can be used */
38#define T0 (env->t0)
39#define T1 (env->t1)
40#define T2 (env->t2)
14ce26e7 41
d785e6be
FB
42#else
43
44/* XXX: use unsigned long instead of target_ulong - better code will
45 be generated for 64 bit CPUs */
46register target_ulong T0 asm(AREG1);
47register target_ulong T1 asm(AREG2);
48register target_ulong T2 asm(AREG3);
2c0262af 49
2c0262af
FB
50/* if more registers are available, we define some registers too */
51#ifdef AREG4
d785e6be 52register target_ulong EAX asm(AREG4);
2c0262af
FB
53#define reg_EAX
54#endif
55
56#ifdef AREG5
d785e6be 57register target_ulong ESP asm(AREG5);
2c0262af
FB
58#define reg_ESP
59#endif
60
61#ifdef AREG6
d785e6be 62register target_ulong EBP asm(AREG6);
2c0262af
FB
63#define reg_EBP
64#endif
65
66#ifdef AREG7
d785e6be 67register target_ulong ECX asm(AREG7);
2c0262af
FB
68#define reg_ECX
69#endif
70
71#ifdef AREG8
d785e6be 72register target_ulong EDX asm(AREG8);
2c0262af
FB
73#define reg_EDX
74#endif
75
76#ifdef AREG9
d785e6be 77register target_ulong EBX asm(AREG9);
2c0262af
FB
78#define reg_EBX
79#endif
80
81#ifdef AREG10
d785e6be 82register target_ulong ESI asm(AREG10);
2c0262af
FB
83#define reg_ESI
84#endif
85
86#ifdef AREG11
d785e6be 87register target_ulong EDI asm(AREG11);
2c0262af
FB
88#define reg_EDI
89#endif
90
d785e6be 91#endif /* ! (TARGET_LONG_BITS > HOST_LONG_BITS) */
14ce26e7
FB
92
93#define A0 T2
94
2c0262af
FB
95extern FILE *logfile;
96extern int loglevel;
97
98#ifndef reg_EAX
99#define EAX (env->regs[R_EAX])
100#endif
101#ifndef reg_ECX
102#define ECX (env->regs[R_ECX])
103#endif
104#ifndef reg_EDX
105#define EDX (env->regs[R_EDX])
106#endif
107#ifndef reg_EBX
108#define EBX (env->regs[R_EBX])
109#endif
110#ifndef reg_ESP
111#define ESP (env->regs[R_ESP])
112#endif
113#ifndef reg_EBP
114#define EBP (env->regs[R_EBP])
115#endif
116#ifndef reg_ESI
117#define ESI (env->regs[R_ESI])
118#endif
119#ifndef reg_EDI
120#define EDI (env->regs[R_EDI])
121#endif
122#define EIP (env->eip)
123#define DF (env->df)
124
125#define CC_SRC (env->cc_src)
126#define CC_DST (env->cc_dst)
127#define CC_OP (env->cc_op)
128
129/* float macros */
130#define FT0 (env->ft0)
664e0f19
FB
131#define ST0 (env->fpregs[env->fpstt].d)
132#define ST(n) (env->fpregs[(env->fpstt + (n)) & 7].d)
2c0262af
FB
133#define ST1 ST(1)
134
135#ifdef USE_FP_CONVERT
136#define FP_CONVERT (env->fp_convert)
137#endif
138
139#include "cpu.h"
140#include "exec-all.h"
141
142typedef struct CCTable {
143 int (*compute_all)(void); /* return all the flags */
144 int (*compute_c)(void); /* return the C flag */
145} CCTable;
146
147extern CCTable cc_table[];
148
8e682019 149void load_seg(int seg_reg, int selector);
08cea4ee 150void helper_ljmp_protected_T0_T1(int next_eip);
2c0262af
FB
151void helper_lcall_real_T0_T1(int shift, int next_eip);
152void helper_lcall_protected_T0_T1(int shift, int next_eip);
153void helper_iret_real(int shift);
08cea4ee 154void helper_iret_protected(int shift, int next_eip);
2c0262af
FB
155void helper_lret_protected(int shift, int addend);
156void helper_lldt_T0(void);
157void helper_ltr_T0(void);
158void helper_movl_crN_T0(int reg);
159void helper_movl_drN_T0(int reg);
8f091a59 160void helper_invlpg(target_ulong addr);
1ac157da 161void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0);
14ce26e7 162void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3);
1ac157da 163void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4);
8f091a59 164void cpu_x86_flush_tlb(CPUX86State *env, target_ulong addr);
14ce26e7 165int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
61382a50 166 int is_write, int is_user, int is_softmmu);
14ce26e7 167void tlb_fill(target_ulong addr, int is_write, int is_user,
61382a50 168 void *retaddr);
2c0262af
FB
169void __hidden cpu_lock(void);
170void __hidden cpu_unlock(void);
171void do_interrupt(int intno, int is_int, int error_code,
14ce26e7 172 target_ulong next_eip, int is_hw);
2c0262af 173void do_interrupt_user(int intno, int is_int, int error_code,
14ce26e7 174 target_ulong next_eip);
2c0262af 175void raise_interrupt(int intno, int is_int, int error_code,
a8ede8ba 176 int next_eip_addend);
2c0262af
FB
177void raise_exception_err(int exception_index, int error_code);
178void raise_exception(int exception_index);
3b21e03e 179void do_smm_enter(void);
2c0262af 180void __hidden cpu_loop_exit(void);
2c0262af
FB
181
182void OPPROTO op_movl_eflags_T0(void);
183void OPPROTO op_movl_T0_eflags(void);
14ce26e7
FB
184void helper_divl_EAX_T0(void);
185void helper_idivl_EAX_T0(void);
186void helper_mulq_EAX_T0(void);
187void helper_imulq_EAX_T0(void);
188void helper_imulq_T0_T1(void);
189void helper_divq_EAX_T0(void);
190void helper_idivq_EAX_T0(void);
68cae3d8 191void helper_bswapq_T0(void);
2c0262af
FB
192void helper_cmpxchg8b(void);
193void helper_cpuid(void);
61a8c4ec 194void helper_enter_level(int level, int data32);
8f091a59 195void helper_enter64_level(int level, int data64);
023fe10d
FB
196void helper_sysenter(void);
197void helper_sysexit(void);
06c2f506 198void helper_syscall(int next_eip_addend);
14ce26e7 199void helper_sysret(int dflag);
2c0262af
FB
200void helper_rdtsc(void);
201void helper_rdmsr(void);
202void helper_wrmsr(void);
203void helper_lsl(void);
204void helper_lar(void);
3ab493de
FB
205void helper_verr(void);
206void helper_verw(void);
3b21e03e 207void helper_rsm(void);
2c0262af 208
3e25f951
FB
209void check_iob_T0(void);
210void check_iow_T0(void);
211void check_iol_T0(void);
212void check_iob_DX(void);
213void check_iow_DX(void);
214void check_iol_DX(void);
215
9951bf39
FB
216#if !defined(CONFIG_USER_ONLY)
217
a9049a07 218#include "softmmu_exec.h"
9951bf39 219
14ce26e7 220static inline double ldfq(target_ulong ptr)
9951bf39
FB
221{
222 union {
223 double d;
224 uint64_t i;
225 } u;
226 u.i = ldq(ptr);
227 return u.d;
228}
229
14ce26e7 230static inline void stfq(target_ulong ptr, double v)
9951bf39
FB
231{
232 union {
233 double d;
234 uint64_t i;
235 } u;
236 u.d = v;
237 stq(ptr, u.i);
238}
239
14ce26e7 240static inline float ldfl(target_ulong ptr)
9951bf39
FB
241{
242 union {
243 float f;
244 uint32_t i;
245 } u;
246 u.i = ldl(ptr);
247 return u.f;
248}
249
14ce26e7 250static inline void stfl(target_ulong ptr, float v)
9951bf39
FB
251{
252 union {
253 float f;
254 uint32_t i;
255 } u;
256 u.f = v;
257 stl(ptr, u.i);
258}
259
260#endif /* !defined(CONFIG_USER_ONLY) */
261
2c0262af
FB
262#ifdef USE_X86LDOUBLE
263/* use long double functions */
7a0e1f41
FB
264#define floatx_to_int32 floatx80_to_int32
265#define floatx_to_int64 floatx80_to_int64
465e9838
FB
266#define floatx_to_int32_round_to_zero floatx80_to_int32_round_to_zero
267#define floatx_to_int64_round_to_zero floatx80_to_int64_round_to_zero
7a0e1f41
FB
268#define floatx_abs floatx80_abs
269#define floatx_chs floatx80_chs
270#define floatx_round_to_int floatx80_round_to_int
8422b113
FB
271#define floatx_compare floatx80_compare
272#define floatx_compare_quiet floatx80_compare_quiet
2c0262af
FB
273#define sin sinl
274#define cos cosl
275#define sqrt sqrtl
276#define pow powl
277#define log logl
278#define tan tanl
279#define atan2 atan2l
280#define floor floorl
281#define ceil ceill
57e4c06e 282#define ldexp ldexpl
7d3505c5 283#else
7a0e1f41
FB
284#define floatx_to_int32 float64_to_int32
285#define floatx_to_int64 float64_to_int64
465e9838
FB
286#define floatx_to_int32_round_to_zero float64_to_int32_round_to_zero
287#define floatx_to_int64_round_to_zero float64_to_int64_round_to_zero
7a0e1f41
FB
288#define floatx_abs float64_abs
289#define floatx_chs float64_chs
290#define floatx_round_to_int float64_round_to_int
8422b113
FB
291#define floatx_compare float64_compare
292#define floatx_compare_quiet float64_compare_quiet
7d3505c5 293#endif
7a0e1f41 294
2c0262af
FB
295extern CPU86_LDouble sin(CPU86_LDouble x);
296extern CPU86_LDouble cos(CPU86_LDouble x);
297extern CPU86_LDouble sqrt(CPU86_LDouble x);
298extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
299extern CPU86_LDouble log(CPU86_LDouble x);
300extern CPU86_LDouble tan(CPU86_LDouble x);
301extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
302extern CPU86_LDouble floor(CPU86_LDouble x);
303extern CPU86_LDouble ceil(CPU86_LDouble x);
2c0262af
FB
304
305#define RC_MASK 0xc00
306#define RC_NEAR 0x000
307#define RC_DOWN 0x400
308#define RC_UP 0x800
309#define RC_CHOP 0xc00
310
311#define MAXTAN 9223372036854775808.0
312
2c0262af
FB
313#ifdef USE_X86LDOUBLE
314
315/* only for x86 */
316typedef union {
317 long double d;
318 struct {
319 unsigned long long lower;
320 unsigned short upper;
321 } l;
322} CPU86_LDoubleU;
323
324/* the following deal with x86 long double-precision numbers */
325#define MAXEXPD 0x7fff
326#define EXPBIAS 16383
327#define EXPD(fp) (fp.l.upper & 0x7fff)
328#define SIGND(fp) ((fp.l.upper) & 0x8000)
329#define MANTD(fp) (fp.l.lower)
330#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
331
332#else
333
334/* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
335typedef union {
336 double d;
337#if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
338 struct {
339 uint32_t lower;
340 int32_t upper;
341 } l;
342#else
343 struct {
344 int32_t upper;
345 uint32_t lower;
346 } l;
347#endif
348#ifndef __arm__
349 int64_t ll;
350#endif
351} CPU86_LDoubleU;
352
353/* the following deal with IEEE double-precision numbers */
354#define MAXEXPD 0x7ff
355#define EXPBIAS 1023
356#define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
357#define SIGND(fp) ((fp.l.upper) & 0x80000000)
358#ifdef __arm__
359#define MANTD(fp) (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
360#else
361#define MANTD(fp) (fp.ll & ((1LL << 52) - 1))
362#endif
363#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
364#endif
365
366static inline void fpush(void)
367{
368 env->fpstt = (env->fpstt - 1) & 7;
369 env->fptags[env->fpstt] = 0; /* validate stack entry */
370}
371
372static inline void fpop(void)
373{
374 env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
375 env->fpstt = (env->fpstt + 1) & 7;
376}
377
378#ifndef USE_X86LDOUBLE
14ce26e7 379static inline CPU86_LDouble helper_fldt(target_ulong ptr)
2c0262af
FB
380{
381 CPU86_LDoubleU temp;
382 int upper, e;
383 uint64_t ll;
384
385 /* mantissa */
386 upper = lduw(ptr + 8);
387 /* XXX: handle overflow ? */
388 e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
389 e |= (upper >> 4) & 0x800; /* sign */
390 ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
391#ifdef __arm__
392 temp.l.upper = (e << 20) | (ll >> 32);
393 temp.l.lower = ll;
394#else
395 temp.ll = ll | ((uint64_t)e << 52);
396#endif
397 return temp.d;
398}
399
664e0f19 400static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
2c0262af
FB
401{
402 CPU86_LDoubleU temp;
403 int e;
404
405 temp.d = f;
406 /* mantissa */
407 stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
408 /* exponent + sign */
409 e = EXPD(temp) - EXPBIAS + 16383;
410 e |= SIGND(temp) >> 16;
411 stw(ptr + 8, e);
412}
9951bf39
FB
413#else
414
415/* XXX: same endianness assumed */
416
417#ifdef CONFIG_USER_ONLY
418
14ce26e7 419static inline CPU86_LDouble helper_fldt(target_ulong ptr)
9951bf39
FB
420{
421 return *(CPU86_LDouble *)ptr;
422}
423
14ce26e7 424static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
9951bf39
FB
425{
426 *(CPU86_LDouble *)ptr = f;
427}
428
429#else
430
431/* we use memory access macros */
432
14ce26e7 433static inline CPU86_LDouble helper_fldt(target_ulong ptr)
9951bf39
FB
434{
435 CPU86_LDoubleU temp;
436
437 temp.l.lower = ldq(ptr);
438 temp.l.upper = lduw(ptr + 8);
439 return temp.d;
440}
441
14ce26e7 442static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
9951bf39
FB
443{
444 CPU86_LDoubleU temp;
445
446 temp.d = f;
447 stq(ptr, temp.l.lower);
448 stw(ptr + 8, temp.l.upper);
449}
450
451#endif /* !CONFIG_USER_ONLY */
452
453#endif /* USE_X86LDOUBLE */
2c0262af 454
2ee73ac3
FB
455#define FPUS_IE (1 << 0)
456#define FPUS_DE (1 << 1)
457#define FPUS_ZE (1 << 2)
458#define FPUS_OE (1 << 3)
459#define FPUS_UE (1 << 4)
460#define FPUS_PE (1 << 5)
461#define FPUS_SF (1 << 6)
462#define FPUS_SE (1 << 7)
463#define FPUS_B (1 << 15)
464
465#define FPUC_EM 0x3f
466
83fb7adf 467extern const CPU86_LDouble f15rk[7];
2c0262af
FB
468
469void helper_fldt_ST0_A0(void);
470void helper_fstt_ST0_A0(void);
2ee73ac3
FB
471void fpu_raise_exception(void);
472CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b);
2c0262af
FB
473void helper_fbld_ST0_A0(void);
474void helper_fbst_ST0_A0(void);
475void helper_f2xm1(void);
476void helper_fyl2x(void);
477void helper_fptan(void);
478void helper_fpatan(void);
479void helper_fxtract(void);
480void helper_fprem1(void);
481void helper_fprem(void);
482void helper_fyl2xp1(void);
483void helper_fsqrt(void);
484void helper_fsincos(void);
485void helper_frndint(void);
486void helper_fscale(void);
487void helper_fsin(void);
488void helper_fcos(void);
489void helper_fxam_ST0(void);
14ce26e7
FB
490void helper_fstenv(target_ulong ptr, int data32);
491void helper_fldenv(target_ulong ptr, int data32);
492void helper_fsave(target_ulong ptr, int data32);
493void helper_frstor(target_ulong ptr, int data32);
494void helper_fxsave(target_ulong ptr, int data64);
495void helper_fxrstor(target_ulong ptr, int data64);
03857e31
FB
496void restore_native_fp_state(CPUState *env);
497void save_native_fp_state(CPUState *env);
664e0f19
FB
498float approx_rsqrt(float a);
499float approx_rcp(float a);
7a0e1f41 500void update_fp_status(void);
3d7374c5
FB
501void helper_hlt(void);
502void helper_monitor(void);
503void helper_mwait(void);
2c0262af 504
83fb7adf
FB
505extern const uint8_t parity_table[256];
506extern const uint8_t rclw_table[32];
507extern const uint8_t rclb_table[32];
2c0262af
FB
508
509static inline uint32_t compute_eflags(void)
510{
511 return env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
512}
513
2c0262af
FB
514/* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
515static inline void load_eflags(int eflags, int update_mask)
516{
517 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
518 DF = 1 - (2 * ((eflags >> 10) & 1));
519 env->eflags = (env->eflags & ~update_mask) |
520 (eflags & update_mask);
521}
522
0d1a29f9
FB
523static inline void env_to_regs(void)
524{
525#ifdef reg_EAX
526 EAX = env->regs[R_EAX];
527#endif
528#ifdef reg_ECX
529 ECX = env->regs[R_ECX];
530#endif
531#ifdef reg_EDX
532 EDX = env->regs[R_EDX];
533#endif
534#ifdef reg_EBX
535 EBX = env->regs[R_EBX];
536#endif
537#ifdef reg_ESP
538 ESP = env->regs[R_ESP];
539#endif
540#ifdef reg_EBP
541 EBP = env->regs[R_EBP];
542#endif
543#ifdef reg_ESI
544 ESI = env->regs[R_ESI];
545#endif
546#ifdef reg_EDI
547 EDI = env->regs[R_EDI];
548#endif
549}
550
551static inline void regs_to_env(void)
552{
553#ifdef reg_EAX
554 env->regs[R_EAX] = EAX;
555#endif
556#ifdef reg_ECX
557 env->regs[R_ECX] = ECX;
558#endif
559#ifdef reg_EDX
560 env->regs[R_EDX] = EDX;
561#endif
562#ifdef reg_EBX
563 env->regs[R_EBX] = EBX;
564#endif
565#ifdef reg_ESP
566 env->regs[R_ESP] = ESP;
567#endif
568#ifdef reg_EBP
569 env->regs[R_EBP] = EBP;
570#endif
571#ifdef reg_ESI
572 env->regs[R_ESI] = ESI;
573#endif
574#ifdef reg_EDI
575 env->regs[R_EDI] = EDI;
576#endif
577}