converted more helpers to TCG
[qemu] / target-i386 / exec.h
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  */
20 #include "config.h"
21 #include "dyngen-exec.h"
22
23 /* XXX: factorize this mess */
24 #ifdef TARGET_X86_64
25 #define TARGET_LONG_BITS 64
26 #else
27 #define TARGET_LONG_BITS 32
28 #endif
29
30 #include "cpu-defs.h"
31
32 /* at least 4 register variables are defined */
33 register struct CPUX86State *env asm(AREG0);
34
35 #ifndef CPU_NO_GLOBAL_REGS
36
37 #if TARGET_LONG_BITS > HOST_LONG_BITS
38
39 /* no registers can be used */
40 #define T0 (env->t0)
41 #define T1 (env->t1)
42 #define T2 (env->t2)
43
44 #else
45
46 /* XXX: use unsigned long instead of target_ulong - better code will
47    be generated for 64 bit CPUs */
48 register target_ulong T0 asm(AREG1);
49 register target_ulong T1 asm(AREG2);
50 register target_ulong T2 asm(AREG3);
51
52 #endif /* ! (TARGET_LONG_BITS > HOST_LONG_BITS) */
53
54 #endif /* ! CPU_NO_GLOBAL_REGS */
55
56 #define A0 T2
57
58 extern FILE *logfile;
59 extern int loglevel;
60
61 #ifndef reg_EAX
62 #define EAX (env->regs[R_EAX])
63 #endif
64 #ifndef reg_ECX
65 #define ECX (env->regs[R_ECX])
66 #endif
67 #ifndef reg_EDX
68 #define EDX (env->regs[R_EDX])
69 #endif
70 #ifndef reg_EBX
71 #define EBX (env->regs[R_EBX])
72 #endif
73 #ifndef reg_ESP
74 #define ESP (env->regs[R_ESP])
75 #endif
76 #ifndef reg_EBP
77 #define EBP (env->regs[R_EBP])
78 #endif
79 #ifndef reg_ESI
80 #define ESI (env->regs[R_ESI])
81 #endif
82 #ifndef reg_EDI
83 #define EDI (env->regs[R_EDI])
84 #endif
85 #define EIP  (env->eip)
86 #define DF  (env->df)
87
88 #define CC_SRC (env->cc_src)
89 #define CC_DST (env->cc_dst)
90 #define CC_OP  (env->cc_op)
91
92 /* float macros */
93 #define FT0    (env->ft0)
94 #define ST0    (env->fpregs[env->fpstt].d)
95 #define ST(n)  (env->fpregs[(env->fpstt + (n)) & 7].d)
96 #define ST1    ST(1)
97
98 #include "cpu.h"
99 #include "exec-all.h"
100
101 typedef struct CCTable {
102     int (*compute_all)(void); /* return all the flags */
103     int (*compute_c)(void);  /* return the C flag */
104 } CCTable;
105
106 extern CCTable cc_table[];
107
108 void helper_load_seg(int seg_reg, int selector);
109 void helper_ljmp_protected_T0_T1(int next_eip);
110 void helper_lcall_real_T0_T1(int shift, int next_eip);
111 void helper_lcall_protected_T0_T1(int shift, int next_eip);
112 void helper_iret_real(int shift);
113 void helper_iret_protected(int shift, int next_eip);
114 void helper_lret_protected(int shift, int addend);
115 void helper_movl_crN_T0(int reg);
116 void helper_movl_drN_T0(int reg);
117 void helper_invlpg(target_ulong addr);
118 void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0);
119 void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3);
120 void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4);
121 void cpu_x86_flush_tlb(CPUX86State *env, target_ulong addr);
122 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
123                              int is_write, int mmu_idx, int is_softmmu);
124 void tlb_fill(target_ulong addr, int is_write, int mmu_idx,
125               void *retaddr);
126 void __hidden cpu_lock(void);
127 void __hidden cpu_unlock(void);
128 void do_interrupt(int intno, int is_int, int error_code,
129                   target_ulong next_eip, int is_hw);
130 void do_interrupt_user(int intno, int is_int, int error_code,
131                        target_ulong next_eip);
132 void raise_interrupt(int intno, int is_int, int error_code,
133                      int next_eip_addend);
134 void raise_exception_err(int exception_index, int error_code);
135 void raise_exception(int exception_index);
136 void do_smm_enter(void);
137 void __hidden cpu_loop_exit(void);
138
139 void OPPROTO op_movl_eflags_T0(void);
140 void OPPROTO op_movl_T0_eflags(void);
141
142 #include "helper.h"
143
144 void helper_mulq_EAX_T0(void);
145 void helper_imulq_EAX_T0(void);
146 void helper_imulq_T0_T1(void);
147 void helper_cmpxchg8b(void);
148
149 void check_iob_T0(void);
150 void check_iow_T0(void);
151 void check_iol_T0(void);
152 void check_iob_DX(void);
153 void check_iow_DX(void);
154 void check_iol_DX(void);
155
156 #if !defined(CONFIG_USER_ONLY)
157
158 #include "softmmu_exec.h"
159
160 #endif /* !defined(CONFIG_USER_ONLY) */
161
162 #ifdef USE_X86LDOUBLE
163 /* use long double functions */
164 #define floatx_to_int32 floatx80_to_int32
165 #define floatx_to_int64 floatx80_to_int64
166 #define floatx_to_int32_round_to_zero floatx80_to_int32_round_to_zero
167 #define floatx_to_int64_round_to_zero floatx80_to_int64_round_to_zero
168 #define int32_to_floatx int32_to_floatx80
169 #define int64_to_floatx int64_to_floatx80
170 #define float32_to_floatx float32_to_floatx80
171 #define float64_to_floatx float64_to_floatx80
172 #define floatx_to_float32 floatx80_to_float32
173 #define floatx_to_float64 floatx80_to_float64
174 #define floatx_abs floatx80_abs
175 #define floatx_chs floatx80_chs
176 #define floatx_round_to_int floatx80_round_to_int
177 #define floatx_compare floatx80_compare
178 #define floatx_compare_quiet floatx80_compare_quiet
179 #define sin sinl
180 #define cos cosl
181 #define sqrt sqrtl
182 #define pow powl
183 #define log logl
184 #define tan tanl
185 #define atan2 atan2l
186 #define floor floorl
187 #define ceil ceill
188 #define ldexp ldexpl
189 #else
190 #define floatx_to_int32 float64_to_int32
191 #define floatx_to_int64 float64_to_int64
192 #define floatx_to_int32_round_to_zero float64_to_int32_round_to_zero
193 #define floatx_to_int64_round_to_zero float64_to_int64_round_to_zero
194 #define int32_to_floatx int32_to_float64
195 #define int64_to_floatx int64_to_float64
196 #define float32_to_floatx float32_to_float64
197 #define float64_to_floatx(x, e) (x)
198 #define floatx_to_float32 float64_to_float32
199 #define floatx_to_float64(x, e) (x)
200 #define floatx_abs float64_abs
201 #define floatx_chs float64_chs
202 #define floatx_round_to_int float64_round_to_int
203 #define floatx_compare float64_compare
204 #define floatx_compare_quiet float64_compare_quiet
205 #endif
206
207 extern CPU86_LDouble sin(CPU86_LDouble x);
208 extern CPU86_LDouble cos(CPU86_LDouble x);
209 extern CPU86_LDouble sqrt(CPU86_LDouble x);
210 extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
211 extern CPU86_LDouble log(CPU86_LDouble x);
212 extern CPU86_LDouble tan(CPU86_LDouble x);
213 extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
214 extern CPU86_LDouble floor(CPU86_LDouble x);
215 extern CPU86_LDouble ceil(CPU86_LDouble x);
216
217 #define RC_MASK         0xc00
218 #define RC_NEAR         0x000
219 #define RC_DOWN         0x400
220 #define RC_UP           0x800
221 #define RC_CHOP         0xc00
222
223 #define MAXTAN 9223372036854775808.0
224
225 #ifdef USE_X86LDOUBLE
226
227 /* only for x86 */
228 typedef union {
229     long double d;
230     struct {
231         unsigned long long lower;
232         unsigned short upper;
233     } l;
234 } CPU86_LDoubleU;
235
236 /* the following deal with x86 long double-precision numbers */
237 #define MAXEXPD 0x7fff
238 #define EXPBIAS 16383
239 #define EXPD(fp)        (fp.l.upper & 0x7fff)
240 #define SIGND(fp)       ((fp.l.upper) & 0x8000)
241 #define MANTD(fp)       (fp.l.lower)
242 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
243
244 #else
245
246 /* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
247 typedef union {
248     double d;
249 #if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
250     struct {
251         uint32_t lower;
252         int32_t upper;
253     } l;
254 #else
255     struct {
256         int32_t upper;
257         uint32_t lower;
258     } l;
259 #endif
260 #ifndef __arm__
261     int64_t ll;
262 #endif
263 } CPU86_LDoubleU;
264
265 /* the following deal with IEEE double-precision numbers */
266 #define MAXEXPD 0x7ff
267 #define EXPBIAS 1023
268 #define EXPD(fp)        (((fp.l.upper) >> 20) & 0x7FF)
269 #define SIGND(fp)       ((fp.l.upper) & 0x80000000)
270 #ifdef __arm__
271 #define MANTD(fp)       (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
272 #else
273 #define MANTD(fp)       (fp.ll & ((1LL << 52) - 1))
274 #endif
275 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
276 #endif
277
278 static inline void fpush(void)
279 {
280     env->fpstt = (env->fpstt - 1) & 7;
281     env->fptags[env->fpstt] = 0; /* validate stack entry */
282 }
283
284 static inline void fpop(void)
285 {
286     env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
287     env->fpstt = (env->fpstt + 1) & 7;
288 }
289
290 #ifndef USE_X86LDOUBLE
291 static inline CPU86_LDouble helper_fldt(target_ulong ptr)
292 {
293     CPU86_LDoubleU temp;
294     int upper, e;
295     uint64_t ll;
296
297     /* mantissa */
298     upper = lduw(ptr + 8);
299     /* XXX: handle overflow ? */
300     e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
301     e |= (upper >> 4) & 0x800; /* sign */
302     ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
303 #ifdef __arm__
304     temp.l.upper = (e << 20) | (ll >> 32);
305     temp.l.lower = ll;
306 #else
307     temp.ll = ll | ((uint64_t)e << 52);
308 #endif
309     return temp.d;
310 }
311
312 static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
313 {
314     CPU86_LDoubleU temp;
315     int e;
316
317     temp.d = f;
318     /* mantissa */
319     stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
320     /* exponent + sign */
321     e = EXPD(temp) - EXPBIAS + 16383;
322     e |= SIGND(temp) >> 16;
323     stw(ptr + 8, e);
324 }
325 #else
326
327 /* we use memory access macros */
328
329 static inline CPU86_LDouble helper_fldt(target_ulong ptr)
330 {
331     CPU86_LDoubleU temp;
332
333     temp.l.lower = ldq(ptr);
334     temp.l.upper = lduw(ptr + 8);
335     return temp.d;
336 }
337
338 static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
339 {
340     CPU86_LDoubleU temp;
341
342     temp.d = f;
343     stq(ptr, temp.l.lower);
344     stw(ptr + 8, temp.l.upper);
345 }
346
347 #endif /* USE_X86LDOUBLE */
348
349 #define FPUS_IE (1 << 0)
350 #define FPUS_DE (1 << 1)
351 #define FPUS_ZE (1 << 2)
352 #define FPUS_OE (1 << 3)
353 #define FPUS_UE (1 << 4)
354 #define FPUS_PE (1 << 5)
355 #define FPUS_SF (1 << 6)
356 #define FPUS_SE (1 << 7)
357 #define FPUS_B  (1 << 15)
358
359 #define FPUC_EM 0x3f
360
361 extern const CPU86_LDouble f15rk[7];
362
363 void fpu_raise_exception(void);
364 void restore_native_fp_state(CPUState *env);
365 void save_native_fp_state(CPUState *env);
366 void vmexit(uint64_t exit_code, uint64_t exit_info_1);
367
368 extern const uint8_t parity_table[256];
369 extern const uint8_t rclw_table[32];
370 extern const uint8_t rclb_table[32];
371
372 static inline uint32_t compute_eflags(void)
373 {
374     return env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
375 }
376
377 /* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
378 static inline void load_eflags(int eflags, int update_mask)
379 {
380     CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
381     DF = 1 - (2 * ((eflags >> 10) & 1));
382     env->eflags = (env->eflags & ~update_mask) |
383         (eflags & update_mask);
384 }
385
386 static inline void env_to_regs(void)
387 {
388 #ifdef reg_EAX
389     EAX = env->regs[R_EAX];
390 #endif
391 #ifdef reg_ECX
392     ECX = env->regs[R_ECX];
393 #endif
394 #ifdef reg_EDX
395     EDX = env->regs[R_EDX];
396 #endif
397 #ifdef reg_EBX
398     EBX = env->regs[R_EBX];
399 #endif
400 #ifdef reg_ESP
401     ESP = env->regs[R_ESP];
402 #endif
403 #ifdef reg_EBP
404     EBP = env->regs[R_EBP];
405 #endif
406 #ifdef reg_ESI
407     ESI = env->regs[R_ESI];
408 #endif
409 #ifdef reg_EDI
410     EDI = env->regs[R_EDI];
411 #endif
412 }
413
414 static inline void regs_to_env(void)
415 {
416 #ifdef reg_EAX
417     env->regs[R_EAX] = EAX;
418 #endif
419 #ifdef reg_ECX
420     env->regs[R_ECX] = ECX;
421 #endif
422 #ifdef reg_EDX
423     env->regs[R_EDX] = EDX;
424 #endif
425 #ifdef reg_EBX
426     env->regs[R_EBX] = EBX;
427 #endif
428 #ifdef reg_ESP
429     env->regs[R_ESP] = ESP;
430 #endif
431 #ifdef reg_EBP
432     env->regs[R_EBP] = EBP;
433 #endif
434 #ifdef reg_ESI
435     env->regs[R_ESI] = ESI;
436 #endif
437 #ifdef reg_EDI
438     env->regs[R_EDI] = EDI;
439 #endif
440 }
441
442 static inline int cpu_halted(CPUState *env) {
443     /* handle exit of HALTED state */
444     if (!(env->hflags & HF_HALTED_MASK))
445         return 0;
446     /* disable halt condition */
447     if (((env->interrupt_request & CPU_INTERRUPT_HARD) &&
448          (env->eflags & IF_MASK)) ||
449         (env->interrupt_request & CPU_INTERRUPT_NMI)) {
450         env->hflags &= ~HF_HALTED_MASK;
451         return 0;
452     }
453     return EXCP_HALTED;
454 }
455