converted more helpers to TCG - fixed some SVM issues
[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 cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0);
109 void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3);
110 void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4);
111 void cpu_x86_flush_tlb(CPUX86State *env, target_ulong addr);
112 int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
113                              int is_write, int mmu_idx, int is_softmmu);
114 void tlb_fill(target_ulong addr, int is_write, int mmu_idx,
115               void *retaddr);
116 void __hidden cpu_lock(void);
117 void __hidden cpu_unlock(void);
118 void do_interrupt(int intno, int is_int, int error_code,
119                   target_ulong next_eip, int is_hw);
120 void do_interrupt_user(int intno, int is_int, int error_code,
121                        target_ulong next_eip);
122 void raise_interrupt(int intno, int is_int, int error_code,
123                      int next_eip_addend);
124 void raise_exception_err(int exception_index, int error_code);
125 void raise_exception(int exception_index);
126 void do_smm_enter(void);
127 void __hidden cpu_loop_exit(void);
128
129 void OPPROTO op_movl_eflags_T0(void);
130 void OPPROTO op_movl_T0_eflags(void);
131
132 #include "helper.h"
133
134 static inline void svm_check_intercept(uint32_t type)
135 {
136     helper_svm_check_intercept_param(type, 0);
137 }
138
139 #if !defined(CONFIG_USER_ONLY)
140
141 #include "softmmu_exec.h"
142
143 #endif /* !defined(CONFIG_USER_ONLY) */
144
145 #ifdef USE_X86LDOUBLE
146 /* use long double functions */
147 #define floatx_to_int32 floatx80_to_int32
148 #define floatx_to_int64 floatx80_to_int64
149 #define floatx_to_int32_round_to_zero floatx80_to_int32_round_to_zero
150 #define floatx_to_int64_round_to_zero floatx80_to_int64_round_to_zero
151 #define int32_to_floatx int32_to_floatx80
152 #define int64_to_floatx int64_to_floatx80
153 #define float32_to_floatx float32_to_floatx80
154 #define float64_to_floatx float64_to_floatx80
155 #define floatx_to_float32 floatx80_to_float32
156 #define floatx_to_float64 floatx80_to_float64
157 #define floatx_abs floatx80_abs
158 #define floatx_chs floatx80_chs
159 #define floatx_round_to_int floatx80_round_to_int
160 #define floatx_compare floatx80_compare
161 #define floatx_compare_quiet floatx80_compare_quiet
162 #define sin sinl
163 #define cos cosl
164 #define sqrt sqrtl
165 #define pow powl
166 #define log logl
167 #define tan tanl
168 #define atan2 atan2l
169 #define floor floorl
170 #define ceil ceill
171 #define ldexp ldexpl
172 #else
173 #define floatx_to_int32 float64_to_int32
174 #define floatx_to_int64 float64_to_int64
175 #define floatx_to_int32_round_to_zero float64_to_int32_round_to_zero
176 #define floatx_to_int64_round_to_zero float64_to_int64_round_to_zero
177 #define int32_to_floatx int32_to_float64
178 #define int64_to_floatx int64_to_float64
179 #define float32_to_floatx float32_to_float64
180 #define float64_to_floatx(x, e) (x)
181 #define floatx_to_float32 float64_to_float32
182 #define floatx_to_float64(x, e) (x)
183 #define floatx_abs float64_abs
184 #define floatx_chs float64_chs
185 #define floatx_round_to_int float64_round_to_int
186 #define floatx_compare float64_compare
187 #define floatx_compare_quiet float64_compare_quiet
188 #endif
189
190 extern CPU86_LDouble sin(CPU86_LDouble x);
191 extern CPU86_LDouble cos(CPU86_LDouble x);
192 extern CPU86_LDouble sqrt(CPU86_LDouble x);
193 extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
194 extern CPU86_LDouble log(CPU86_LDouble x);
195 extern CPU86_LDouble tan(CPU86_LDouble x);
196 extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
197 extern CPU86_LDouble floor(CPU86_LDouble x);
198 extern CPU86_LDouble ceil(CPU86_LDouble x);
199
200 #define RC_MASK         0xc00
201 #define RC_NEAR         0x000
202 #define RC_DOWN         0x400
203 #define RC_UP           0x800
204 #define RC_CHOP         0xc00
205
206 #define MAXTAN 9223372036854775808.0
207
208 #ifdef USE_X86LDOUBLE
209
210 /* only for x86 */
211 typedef union {
212     long double d;
213     struct {
214         unsigned long long lower;
215         unsigned short upper;
216     } l;
217 } CPU86_LDoubleU;
218
219 /* the following deal with x86 long double-precision numbers */
220 #define MAXEXPD 0x7fff
221 #define EXPBIAS 16383
222 #define EXPD(fp)        (fp.l.upper & 0x7fff)
223 #define SIGND(fp)       ((fp.l.upper) & 0x8000)
224 #define MANTD(fp)       (fp.l.lower)
225 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
226
227 #else
228
229 /* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
230 typedef union {
231     double d;
232 #if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
233     struct {
234         uint32_t lower;
235         int32_t upper;
236     } l;
237 #else
238     struct {
239         int32_t upper;
240         uint32_t lower;
241     } l;
242 #endif
243 #ifndef __arm__
244     int64_t ll;
245 #endif
246 } CPU86_LDoubleU;
247
248 /* the following deal with IEEE double-precision numbers */
249 #define MAXEXPD 0x7ff
250 #define EXPBIAS 1023
251 #define EXPD(fp)        (((fp.l.upper) >> 20) & 0x7FF)
252 #define SIGND(fp)       ((fp.l.upper) & 0x80000000)
253 #ifdef __arm__
254 #define MANTD(fp)       (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
255 #else
256 #define MANTD(fp)       (fp.ll & ((1LL << 52) - 1))
257 #endif
258 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
259 #endif
260
261 static inline void fpush(void)
262 {
263     env->fpstt = (env->fpstt - 1) & 7;
264     env->fptags[env->fpstt] = 0; /* validate stack entry */
265 }
266
267 static inline void fpop(void)
268 {
269     env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
270     env->fpstt = (env->fpstt + 1) & 7;
271 }
272
273 #ifndef USE_X86LDOUBLE
274 static inline CPU86_LDouble helper_fldt(target_ulong ptr)
275 {
276     CPU86_LDoubleU temp;
277     int upper, e;
278     uint64_t ll;
279
280     /* mantissa */
281     upper = lduw(ptr + 8);
282     /* XXX: handle overflow ? */
283     e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
284     e |= (upper >> 4) & 0x800; /* sign */
285     ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
286 #ifdef __arm__
287     temp.l.upper = (e << 20) | (ll >> 32);
288     temp.l.lower = ll;
289 #else
290     temp.ll = ll | ((uint64_t)e << 52);
291 #endif
292     return temp.d;
293 }
294
295 static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
296 {
297     CPU86_LDoubleU temp;
298     int e;
299
300     temp.d = f;
301     /* mantissa */
302     stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
303     /* exponent + sign */
304     e = EXPD(temp) - EXPBIAS + 16383;
305     e |= SIGND(temp) >> 16;
306     stw(ptr + 8, e);
307 }
308 #else
309
310 /* we use memory access macros */
311
312 static inline CPU86_LDouble helper_fldt(target_ulong ptr)
313 {
314     CPU86_LDoubleU temp;
315
316     temp.l.lower = ldq(ptr);
317     temp.l.upper = lduw(ptr + 8);
318     return temp.d;
319 }
320
321 static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
322 {
323     CPU86_LDoubleU temp;
324
325     temp.d = f;
326     stq(ptr, temp.l.lower);
327     stw(ptr + 8, temp.l.upper);
328 }
329
330 #endif /* USE_X86LDOUBLE */
331
332 #define FPUS_IE (1 << 0)
333 #define FPUS_DE (1 << 1)
334 #define FPUS_ZE (1 << 2)
335 #define FPUS_OE (1 << 3)
336 #define FPUS_UE (1 << 4)
337 #define FPUS_PE (1 << 5)
338 #define FPUS_SF (1 << 6)
339 #define FPUS_SE (1 << 7)
340 #define FPUS_B  (1 << 15)
341
342 #define FPUC_EM 0x3f
343
344 extern const CPU86_LDouble f15rk[7];
345
346 void fpu_raise_exception(void);
347 void restore_native_fp_state(CPUState *env);
348 void save_native_fp_state(CPUState *env);
349
350 extern const uint8_t parity_table[256];
351 extern const uint8_t rclw_table[32];
352 extern const uint8_t rclb_table[32];
353
354 static inline uint32_t compute_eflags(void)
355 {
356     return env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
357 }
358
359 /* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
360 static inline void load_eflags(int eflags, int update_mask)
361 {
362     CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
363     DF = 1 - (2 * ((eflags >> 10) & 1));
364     env->eflags = (env->eflags & ~update_mask) |
365         (eflags & update_mask);
366 }
367
368 static inline void env_to_regs(void)
369 {
370 #ifdef reg_EAX
371     EAX = env->regs[R_EAX];
372 #endif
373 #ifdef reg_ECX
374     ECX = env->regs[R_ECX];
375 #endif
376 #ifdef reg_EDX
377     EDX = env->regs[R_EDX];
378 #endif
379 #ifdef reg_EBX
380     EBX = env->regs[R_EBX];
381 #endif
382 #ifdef reg_ESP
383     ESP = env->regs[R_ESP];
384 #endif
385 #ifdef reg_EBP
386     EBP = env->regs[R_EBP];
387 #endif
388 #ifdef reg_ESI
389     ESI = env->regs[R_ESI];
390 #endif
391 #ifdef reg_EDI
392     EDI = env->regs[R_EDI];
393 #endif
394 }
395
396 static inline void regs_to_env(void)
397 {
398 #ifdef reg_EAX
399     env->regs[R_EAX] = EAX;
400 #endif
401 #ifdef reg_ECX
402     env->regs[R_ECX] = ECX;
403 #endif
404 #ifdef reg_EDX
405     env->regs[R_EDX] = EDX;
406 #endif
407 #ifdef reg_EBX
408     env->regs[R_EBX] = EBX;
409 #endif
410 #ifdef reg_ESP
411     env->regs[R_ESP] = ESP;
412 #endif
413 #ifdef reg_EBP
414     env->regs[R_EBP] = EBP;
415 #endif
416 #ifdef reg_ESI
417     env->regs[R_ESI] = ESI;
418 #endif
419 #ifdef reg_EDI
420     env->regs[R_EDI] = EDI;
421 #endif
422 }
423
424 static inline int cpu_halted(CPUState *env) {
425     /* handle exit of HALTED state */
426     if (!(env->hflags & HF_HALTED_MASK))
427         return 0;
428     /* disable halt condition */
429     if (((env->interrupt_request & CPU_INTERRUPT_HARD) &&
430          (env->eflags & IF_MASK)) ||
431         (env->interrupt_request & CPU_INTERRUPT_NMI)) {
432         env->hflags &= ~HF_HALTED_MASK;
433         return 0;
434     }
435     return EXCP_HALTED;
436 }
437