Statistics
| Branch: | Revision:

root / target-i386 / exec.h @ 9951bf39

History | View | Annotate | Download (12.2 kB)

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 "dyngen-exec.h"
21

    
22
/* at least 4 register variables are defines */
23
register struct CPUX86State *env asm(AREG0);
24
register uint32_t T0 asm(AREG1);
25
register uint32_t T1 asm(AREG2);
26
register uint32_t T2 asm(AREG3);
27

    
28
#define A0 T2
29

    
30
/* if more registers are available, we define some registers too */
31
#ifdef AREG4
32
register uint32_t EAX asm(AREG4);
33
#define reg_EAX
34
#endif
35

    
36
#ifdef AREG5
37
register uint32_t ESP asm(AREG5);
38
#define reg_ESP
39
#endif
40

    
41
#ifdef AREG6
42
register uint32_t EBP asm(AREG6);
43
#define reg_EBP
44
#endif
45

    
46
#ifdef AREG7
47
register uint32_t ECX asm(AREG7);
48
#define reg_ECX
49
#endif
50

    
51
#ifdef AREG8
52
register uint32_t EDX asm(AREG8);
53
#define reg_EDX
54
#endif
55

    
56
#ifdef AREG9
57
register uint32_t EBX asm(AREG9);
58
#define reg_EBX
59
#endif
60

    
61
#ifdef AREG10
62
register uint32_t ESI asm(AREG10);
63
#define reg_ESI
64
#endif
65

    
66
#ifdef AREG11
67
register uint32_t EDI asm(AREG11);
68
#define reg_EDI
69
#endif
70

    
71
extern FILE *logfile;
72
extern int loglevel;
73

    
74
#ifndef reg_EAX
75
#define EAX (env->regs[R_EAX])
76
#endif
77
#ifndef reg_ECX
78
#define ECX (env->regs[R_ECX])
79
#endif
80
#ifndef reg_EDX
81
#define EDX (env->regs[R_EDX])
82
#endif
83
#ifndef reg_EBX
84
#define EBX (env->regs[R_EBX])
85
#endif
86
#ifndef reg_ESP
87
#define ESP (env->regs[R_ESP])
88
#endif
89
#ifndef reg_EBP
90
#define EBP (env->regs[R_EBP])
91
#endif
92
#ifndef reg_ESI
93
#define ESI (env->regs[R_ESI])
94
#endif
95
#ifndef reg_EDI
96
#define EDI (env->regs[R_EDI])
97
#endif
98
#define EIP  (env->eip)
99
#define DF  (env->df)
100

    
101
#define CC_SRC (env->cc_src)
102
#define CC_DST (env->cc_dst)
103
#define CC_OP  (env->cc_op)
104

    
105
/* float macros */
106
#define FT0    (env->ft0)
107
#define ST0    (env->fpregs[env->fpstt])
108
#define ST(n)  (env->fpregs[(env->fpstt + (n)) & 7])
109
#define ST1    ST(1)
110

    
111
#ifdef USE_FP_CONVERT
112
#define FP_CONVERT  (env->fp_convert)
113
#endif
114

    
115
#include "cpu.h"
116
#include "exec-all.h"
117

    
118
typedef struct CCTable {
119
    int (*compute_all)(void); /* return all the flags */
120
    int (*compute_c)(void);  /* return the C flag */
121
} CCTable;
122

    
123
extern CCTable cc_table[];
124

    
125
void load_seg(int seg_reg, int selector, unsigned cur_eip);
126
void helper_ljmp_protected_T0_T1(void);
127
void helper_lcall_real_T0_T1(int shift, int next_eip);
128
void helper_lcall_protected_T0_T1(int shift, int next_eip);
129
void helper_iret_real(int shift);
130
void helper_iret_protected(int shift);
131
void helper_lret_protected(int shift, int addend);
132
void helper_lldt_T0(void);
133
void helper_ltr_T0(void);
134
void helper_movl_crN_T0(int reg);
135
void helper_movl_drN_T0(int reg);
136
void helper_invlpg(unsigned int addr);
137
void cpu_x86_update_cr0(CPUX86State *env);
138
void cpu_x86_update_cr3(CPUX86State *env);
139
void cpu_x86_flush_tlb(CPUX86State *env, uint32_t addr);
140
int cpu_x86_handle_mmu_fault(CPUX86State *env, uint32_t addr, 
141
                             int is_write, int is_user, int is_softmmu);
142
void tlb_fill(unsigned long addr, int is_write, int is_user, 
143
              void *retaddr);
144
void __hidden cpu_lock(void);
145
void __hidden cpu_unlock(void);
146
void do_interrupt(int intno, int is_int, int error_code, 
147
                  unsigned int next_eip, int is_hw);
148
void do_interrupt_user(int intno, int is_int, int error_code, 
149
                       unsigned int next_eip);
150
void raise_interrupt(int intno, int is_int, int error_code, 
151
                     unsigned int next_eip);
152
void raise_exception_err(int exception_index, int error_code);
153
void raise_exception(int exception_index);
154
void __hidden cpu_loop_exit(void);
155
void helper_fsave(uint8_t *ptr, int data32);
156
void helper_frstor(uint8_t *ptr, int data32);
157

    
158
void OPPROTO op_movl_eflags_T0(void);
159
void OPPROTO op_movl_T0_eflags(void);
160
void raise_interrupt(int intno, int is_int, int error_code, 
161
                     unsigned int next_eip);
162
void raise_exception_err(int exception_index, int error_code);
163
void raise_exception(int exception_index);
164
void helper_divl_EAX_T0(uint32_t eip);
165
void helper_idivl_EAX_T0(uint32_t eip);
166
void helper_cmpxchg8b(void);
167
void helper_cpuid(void);
168
void helper_rdtsc(void);
169
void helper_rdmsr(void);
170
void helper_wrmsr(void);
171
void helper_lsl(void);
172
void helper_lar(void);
173

    
174
/* XXX: move that to a generic header */
175
#if !defined(CONFIG_USER_ONLY)
176

    
177
#define ldul_user ldl_user
178
#define ldul_kernel ldl_kernel
179

    
180
#define ACCESS_TYPE 0
181
#define MEMSUFFIX _kernel
182
#define DATA_SIZE 1
183
#include "softmmu_header.h"
184

    
185
#define DATA_SIZE 2
186
#include "softmmu_header.h"
187

    
188
#define DATA_SIZE 4
189
#include "softmmu_header.h"
190

    
191
#define DATA_SIZE 8
192
#include "softmmu_header.h"
193
#undef ACCESS_TYPE
194
#undef MEMSUFFIX
195

    
196
#define ACCESS_TYPE 1
197
#define MEMSUFFIX _user
198
#define DATA_SIZE 1
199
#include "softmmu_header.h"
200

    
201
#define DATA_SIZE 2
202
#include "softmmu_header.h"
203

    
204
#define DATA_SIZE 4
205
#include "softmmu_header.h"
206

    
207
#define DATA_SIZE 8
208
#include "softmmu_header.h"
209
#undef ACCESS_TYPE
210
#undef MEMSUFFIX
211

    
212
/* these access are slower, they must be as rare as possible */
213
#define ACCESS_TYPE 2
214
#define MEMSUFFIX _data
215
#define DATA_SIZE 1
216
#include "softmmu_header.h"
217

    
218
#define DATA_SIZE 2
219
#include "softmmu_header.h"
220

    
221
#define DATA_SIZE 4
222
#include "softmmu_header.h"
223

    
224
#define DATA_SIZE 8
225
#include "softmmu_header.h"
226
#undef ACCESS_TYPE
227
#undef MEMSUFFIX
228

    
229
#define ldub(p) ldub_data(p)
230
#define ldsb(p) ldsb_data(p)
231
#define lduw(p) lduw_data(p)
232
#define ldsw(p) ldsw_data(p)
233
#define ldl(p) ldl_data(p)
234
#define ldq(p) ldq_data(p)
235

    
236
#define stb(p, v) stb_data(p, v)
237
#define stw(p, v) stw_data(p, v)
238
#define stl(p, v) stl_data(p, v)
239
#define stq(p, v) stq_data(p, v)
240

    
241
static inline double ldfq(void *ptr)
242
{
243
    union {
244
        double d;
245
        uint64_t i;
246
    } u;
247
    u.i = ldq(ptr);
248
    return u.d;
249
}
250

    
251
static inline void stfq(void *ptr, double v)
252
{
253
    union {
254
        double d;
255
        uint64_t i;
256
    } u;
257
    u.d = v;
258
    stq(ptr, u.i);
259
}
260

    
261
static inline float ldfl(void *ptr)
262
{
263
    union {
264
        float f;
265
        uint32_t i;
266
    } u;
267
    u.i = ldl(ptr);
268
    return u.f;
269
}
270

    
271
static inline void stfl(void *ptr, float v)
272
{
273
    union {
274
        float f;
275
        uint32_t i;
276
    } u;
277
    u.f = v;
278
    stl(ptr, u.i);
279
}
280

    
281
#endif /* !defined(CONFIG_USER_ONLY) */
282

    
283
#ifdef USE_X86LDOUBLE
284
/* use long double functions */
285
#define lrint lrintl
286
#define llrint llrintl
287
#define fabs fabsl
288
#define sin sinl
289
#define cos cosl
290
#define sqrt sqrtl
291
#define pow powl
292
#define log logl
293
#define tan tanl
294
#define atan2 atan2l
295
#define floor floorl
296
#define ceil ceill
297
#define rint rintl
298
#endif
299

    
300
extern int lrint(CPU86_LDouble x);
301
extern int64_t llrint(CPU86_LDouble x);
302
extern CPU86_LDouble fabs(CPU86_LDouble x);
303
extern CPU86_LDouble sin(CPU86_LDouble x);
304
extern CPU86_LDouble cos(CPU86_LDouble x);
305
extern CPU86_LDouble sqrt(CPU86_LDouble x);
306
extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
307
extern CPU86_LDouble log(CPU86_LDouble x);
308
extern CPU86_LDouble tan(CPU86_LDouble x);
309
extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
310
extern CPU86_LDouble floor(CPU86_LDouble x);
311
extern CPU86_LDouble ceil(CPU86_LDouble x);
312
extern CPU86_LDouble rint(CPU86_LDouble x);
313

    
314
#define RC_MASK         0xc00
315
#define RC_NEAR                0x000
316
#define RC_DOWN                0x400
317
#define RC_UP                0x800
318
#define RC_CHOP                0xc00
319

    
320
#define MAXTAN 9223372036854775808.0
321

    
322
#ifdef __arm__
323
/* we have no way to do correct rounding - a FPU emulator is needed */
324
#define FE_DOWNWARD   FE_TONEAREST
325
#define FE_UPWARD     FE_TONEAREST
326
#define FE_TOWARDZERO FE_TONEAREST
327
#endif
328

    
329
#ifdef USE_X86LDOUBLE
330

    
331
/* only for x86 */
332
typedef union {
333
    long double d;
334
    struct {
335
        unsigned long long lower;
336
        unsigned short upper;
337
    } l;
338
} CPU86_LDoubleU;
339

    
340
/* the following deal with x86 long double-precision numbers */
341
#define MAXEXPD 0x7fff
342
#define EXPBIAS 16383
343
#define EXPD(fp)        (fp.l.upper & 0x7fff)
344
#define SIGND(fp)        ((fp.l.upper) & 0x8000)
345
#define MANTD(fp)       (fp.l.lower)
346
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
347

    
348
#else
349

    
350
/* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
351
typedef union {
352
    double d;
353
#if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
354
    struct {
355
        uint32_t lower;
356
        int32_t upper;
357
    } l;
358
#else
359
    struct {
360
        int32_t upper;
361
        uint32_t lower;
362
    } l;
363
#endif
364
#ifndef __arm__
365
    int64_t ll;
366
#endif
367
} CPU86_LDoubleU;
368

    
369
/* the following deal with IEEE double-precision numbers */
370
#define MAXEXPD 0x7ff
371
#define EXPBIAS 1023
372
#define EXPD(fp)        (((fp.l.upper) >> 20) & 0x7FF)
373
#define SIGND(fp)        ((fp.l.upper) & 0x80000000)
374
#ifdef __arm__
375
#define MANTD(fp)        (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
376
#else
377
#define MANTD(fp)        (fp.ll & ((1LL << 52) - 1))
378
#endif
379
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
380
#endif
381

    
382
static inline void fpush(void)
383
{
384
    env->fpstt = (env->fpstt - 1) & 7;
385
    env->fptags[env->fpstt] = 0; /* validate stack entry */
386
}
387

    
388
static inline void fpop(void)
389
{
390
    env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
391
    env->fpstt = (env->fpstt + 1) & 7;
392
}
393

    
394
#ifndef USE_X86LDOUBLE
395
static inline CPU86_LDouble helper_fldt(uint8_t *ptr)
396
{
397
    CPU86_LDoubleU temp;
398
    int upper, e;
399
    uint64_t ll;
400

    
401
    /* mantissa */
402
    upper = lduw(ptr + 8);
403
    /* XXX: handle overflow ? */
404
    e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
405
    e |= (upper >> 4) & 0x800; /* sign */
406
    ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
407
#ifdef __arm__
408
    temp.l.upper = (e << 20) | (ll >> 32);
409
    temp.l.lower = ll;
410
#else
411
    temp.ll = ll | ((uint64_t)e << 52);
412
#endif
413
    return temp.d;
414
}
415

    
416
static inline void helper_fstt(CPU86_LDouble f, uint8_t *ptr)
417
{
418
    CPU86_LDoubleU temp;
419
    int e;
420

    
421
    temp.d = f;
422
    /* mantissa */
423
    stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
424
    /* exponent + sign */
425
    e = EXPD(temp) - EXPBIAS + 16383;
426
    e |= SIGND(temp) >> 16;
427
    stw(ptr + 8, e);
428
}
429
#else
430

    
431
/* XXX: same endianness assumed */
432

    
433
#ifdef CONFIG_USER_ONLY
434

    
435
static inline CPU86_LDouble helper_fldt(uint8_t *ptr)
436
{
437
    return *(CPU86_LDouble *)ptr;
438
}
439

    
440
static inline void helper_fstt(CPU86_LDouble f, uint8_t *ptr)
441
{
442
    *(CPU86_LDouble *)ptr = f;
443
}
444

    
445
#else
446

    
447
/* we use memory access macros */
448

    
449
static inline CPU86_LDouble helper_fldt(uint8_t *ptr)
450
{
451
    CPU86_LDoubleU temp;
452

    
453
    temp.l.lower = ldq(ptr);
454
    temp.l.upper = lduw(ptr + 8);
455
    return temp.d;
456
}
457

    
458
static inline void helper_fstt(CPU86_LDouble f, uint8_t *ptr)
459
{
460
    CPU86_LDoubleU temp;
461
    
462
    temp.d = f;
463
    stq(ptr, temp.l.lower);
464
    stw(ptr + 8, temp.l.upper);
465
}
466

    
467
#endif /* !CONFIG_USER_ONLY */
468

    
469
#endif /* USE_X86LDOUBLE */
470

    
471
const CPU86_LDouble f15rk[7];
472

    
473
void helper_fldt_ST0_A0(void);
474
void helper_fstt_ST0_A0(void);
475
void helper_fbld_ST0_A0(void);
476
void helper_fbst_ST0_A0(void);
477
void helper_f2xm1(void);
478
void helper_fyl2x(void);
479
void helper_fptan(void);
480
void helper_fpatan(void);
481
void helper_fxtract(void);
482
void helper_fprem1(void);
483
void helper_fprem(void);
484
void helper_fyl2xp1(void);
485
void helper_fsqrt(void);
486
void helper_fsincos(void);
487
void helper_frndint(void);
488
void helper_fscale(void);
489
void helper_fsin(void);
490
void helper_fcos(void);
491
void helper_fxam_ST0(void);
492
void helper_fstenv(uint8_t *ptr, int data32);
493
void helper_fldenv(uint8_t *ptr, int data32);
494
void helper_fsave(uint8_t *ptr, int data32);
495
void helper_frstor(uint8_t *ptr, int data32);
496

    
497
const uint8_t parity_table[256];
498
const uint8_t rclw_table[32];
499
const uint8_t rclb_table[32];
500

    
501
static inline uint32_t compute_eflags(void)
502
{
503
    return env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
504
}
505

    
506
#define FL_UPDATE_MASK32 (TF_MASK | AC_MASK | ID_MASK)
507

    
508
#define FL_UPDATE_CPL0_MASK (TF_MASK | IF_MASK | IOPL_MASK | NT_MASK | \
509
                             RF_MASK | AC_MASK | ID_MASK)
510

    
511
/* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
512
static inline void load_eflags(int eflags, int update_mask)
513
{
514
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
515
    DF = 1 - (2 * ((eflags >> 10) & 1));
516
    env->eflags = (env->eflags & ~update_mask) | 
517
        (eflags & update_mask);
518
}
519