Statistics
| Branch: | Revision:

root / target-i386 / exec.h @ 8f091a59

History | View | Annotate | Download (14.6 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 "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
#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)
41

    
42
#else
43

    
44
/* XXX: use unsigned long instead of target_ulong - better code will
45
   be generated for 64 bit CPUs */
46
register target_ulong T0 asm(AREG1);
47
register target_ulong T1 asm(AREG2);
48
register target_ulong T2 asm(AREG3);
49

    
50
/* if more registers are available, we define some registers too */
51
#ifdef AREG4
52
register target_ulong EAX asm(AREG4);
53
#define reg_EAX
54
#endif
55

    
56
#ifdef AREG5
57
register target_ulong ESP asm(AREG5);
58
#define reg_ESP
59
#endif
60

    
61
#ifdef AREG6
62
register target_ulong EBP asm(AREG6);
63
#define reg_EBP
64
#endif
65

    
66
#ifdef AREG7
67
register target_ulong ECX asm(AREG7);
68
#define reg_ECX
69
#endif
70

    
71
#ifdef AREG8
72
register target_ulong EDX asm(AREG8);
73
#define reg_EDX
74
#endif
75

    
76
#ifdef AREG9
77
register target_ulong EBX asm(AREG9);
78
#define reg_EBX
79
#endif
80

    
81
#ifdef AREG10
82
register target_ulong ESI asm(AREG10);
83
#define reg_ESI
84
#endif
85

    
86
#ifdef AREG11
87
register target_ulong EDI asm(AREG11);
88
#define reg_EDI
89
#endif
90

    
91
#endif /* ! (TARGET_LONG_BITS > HOST_LONG_BITS) */
92

    
93
#define A0 T2
94

    
95
extern FILE *logfile;
96
extern 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)
131
#define ST0    (env->fpregs[env->fpstt].d)
132
#define ST(n)  (env->fpregs[(env->fpstt + (n)) & 7].d)
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

    
142
typedef struct CCTable {
143
    int (*compute_all)(void); /* return all the flags */
144
    int (*compute_c)(void);  /* return the C flag */
145
} CCTable;
146

    
147
extern CCTable cc_table[];
148

    
149
void load_seg(int seg_reg, int selector);
150
void helper_ljmp_protected_T0_T1(int next_eip);
151
void helper_lcall_real_T0_T1(int shift, int next_eip);
152
void helper_lcall_protected_T0_T1(int shift, int next_eip);
153
void helper_iret_real(int shift);
154
void helper_iret_protected(int shift, int next_eip);
155
void helper_lret_protected(int shift, int addend);
156
void helper_lldt_T0(void);
157
void helper_ltr_T0(void);
158
void helper_movl_crN_T0(int reg);
159
void helper_movl_drN_T0(int reg);
160
void helper_invlpg(target_ulong addr);
161
void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0);
162
void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3);
163
void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4);
164
void cpu_x86_flush_tlb(CPUX86State *env, target_ulong addr);
165
int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr, 
166
                             int is_write, int is_user, int is_softmmu);
167
void tlb_fill(target_ulong addr, int is_write, int is_user, 
168
              void *retaddr);
169
void __hidden cpu_lock(void);
170
void __hidden cpu_unlock(void);
171
void do_interrupt(int intno, int is_int, int error_code, 
172
                  target_ulong next_eip, int is_hw);
173
void do_interrupt_user(int intno, int is_int, int error_code, 
174
                       target_ulong next_eip);
175
void raise_interrupt(int intno, int is_int, int error_code, 
176
                     int next_eip_addend);
177
void raise_exception_err(int exception_index, int error_code);
178
void raise_exception(int exception_index);
179
void __hidden cpu_loop_exit(void);
180

    
181
void OPPROTO op_movl_eflags_T0(void);
182
void OPPROTO op_movl_T0_eflags(void);
183
void helper_divl_EAX_T0(void);
184
void helper_idivl_EAX_T0(void);
185
void helper_mulq_EAX_T0(void);
186
void helper_imulq_EAX_T0(void);
187
void helper_imulq_T0_T1(void);
188
void helper_divq_EAX_T0(void);
189
void helper_idivq_EAX_T0(void);
190
void helper_cmpxchg8b(void);
191
void helper_cpuid(void);
192
void helper_enter_level(int level, int data32);
193
void helper_enter64_level(int level, int data64);
194
void helper_sysenter(void);
195
void helper_sysexit(void);
196
void helper_syscall(int next_eip_addend);
197
void helper_sysret(int dflag);
198
void helper_rdtsc(void);
199
void helper_rdmsr(void);
200
void helper_wrmsr(void);
201
void helper_lsl(void);
202
void helper_lar(void);
203
void helper_verr(void);
204
void helper_verw(void);
205

    
206
void check_iob_T0(void);
207
void check_iow_T0(void);
208
void check_iol_T0(void);
209
void check_iob_DX(void);
210
void check_iow_DX(void);
211
void check_iol_DX(void);
212

    
213
/* XXX: move that to a generic header */
214
#if !defined(CONFIG_USER_ONLY)
215

    
216
#define ldul_user ldl_user
217
#define ldul_kernel ldl_kernel
218

    
219
#define ACCESS_TYPE 0
220
#define MEMSUFFIX _kernel
221
#define DATA_SIZE 1
222
#include "softmmu_header.h"
223

    
224
#define DATA_SIZE 2
225
#include "softmmu_header.h"
226

    
227
#define DATA_SIZE 4
228
#include "softmmu_header.h"
229

    
230
#define DATA_SIZE 8
231
#include "softmmu_header.h"
232
#undef ACCESS_TYPE
233
#undef MEMSUFFIX
234

    
235
#define ACCESS_TYPE 1
236
#define MEMSUFFIX _user
237
#define DATA_SIZE 1
238
#include "softmmu_header.h"
239

    
240
#define DATA_SIZE 2
241
#include "softmmu_header.h"
242

    
243
#define DATA_SIZE 4
244
#include "softmmu_header.h"
245

    
246
#define DATA_SIZE 8
247
#include "softmmu_header.h"
248
#undef ACCESS_TYPE
249
#undef MEMSUFFIX
250

    
251
/* these access are slower, they must be as rare as possible */
252
#define ACCESS_TYPE 2
253
#define MEMSUFFIX _data
254
#define DATA_SIZE 1
255
#include "softmmu_header.h"
256

    
257
#define DATA_SIZE 2
258
#include "softmmu_header.h"
259

    
260
#define DATA_SIZE 4
261
#include "softmmu_header.h"
262

    
263
#define DATA_SIZE 8
264
#include "softmmu_header.h"
265
#undef ACCESS_TYPE
266
#undef MEMSUFFIX
267

    
268
#define ldub(p) ldub_data(p)
269
#define ldsb(p) ldsb_data(p)
270
#define lduw(p) lduw_data(p)
271
#define ldsw(p) ldsw_data(p)
272
#define ldl(p) ldl_data(p)
273
#define ldq(p) ldq_data(p)
274

    
275
#define stb(p, v) stb_data(p, v)
276
#define stw(p, v) stw_data(p, v)
277
#define stl(p, v) stl_data(p, v)
278
#define stq(p, v) stq_data(p, v)
279

    
280
static inline double ldfq(target_ulong ptr)
281
{
282
    union {
283
        double d;
284
        uint64_t i;
285
    } u;
286
    u.i = ldq(ptr);
287
    return u.d;
288
}
289

    
290
static inline void stfq(target_ulong ptr, double v)
291
{
292
    union {
293
        double d;
294
        uint64_t i;
295
    } u;
296
    u.d = v;
297
    stq(ptr, u.i);
298
}
299

    
300
static inline float ldfl(target_ulong ptr)
301
{
302
    union {
303
        float f;
304
        uint32_t i;
305
    } u;
306
    u.i = ldl(ptr);
307
    return u.f;
308
}
309

    
310
static inline void stfl(target_ulong ptr, float v)
311
{
312
    union {
313
        float f;
314
        uint32_t i;
315
    } u;
316
    u.f = v;
317
    stl(ptr, u.i);
318
}
319

    
320
#endif /* !defined(CONFIG_USER_ONLY) */
321

    
322
#ifdef USE_X86LDOUBLE
323
/* use long double functions */
324
#define floatx_to_int32 floatx80_to_int32
325
#define floatx_to_int64 floatx80_to_int64
326
#define floatx_abs floatx80_abs
327
#define floatx_chs floatx80_chs
328
#define floatx_round_to_int floatx80_round_to_int
329
#define floatx_compare floatx80_compare
330
#define floatx_compare_quiet floatx80_compare_quiet
331
#define sin sinl
332
#define cos cosl
333
#define sqrt sqrtl
334
#define pow powl
335
#define log logl
336
#define tan tanl
337
#define atan2 atan2l
338
#define floor floorl
339
#define ceil ceill
340
#else
341
#define floatx_to_int32 float64_to_int32
342
#define floatx_to_int64 float64_to_int64
343
#define floatx_abs float64_abs
344
#define floatx_chs float64_chs
345
#define floatx_round_to_int float64_round_to_int
346
#define floatx_compare float64_compare
347
#define floatx_compare_quiet float64_compare_quiet
348
#endif
349

    
350
extern CPU86_LDouble sin(CPU86_LDouble x);
351
extern CPU86_LDouble cos(CPU86_LDouble x);
352
extern CPU86_LDouble sqrt(CPU86_LDouble x);
353
extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
354
extern CPU86_LDouble log(CPU86_LDouble x);
355
extern CPU86_LDouble tan(CPU86_LDouble x);
356
extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
357
extern CPU86_LDouble floor(CPU86_LDouble x);
358
extern CPU86_LDouble ceil(CPU86_LDouble x);
359

    
360
#define RC_MASK         0xc00
361
#define RC_NEAR                0x000
362
#define RC_DOWN                0x400
363
#define RC_UP                0x800
364
#define RC_CHOP                0xc00
365

    
366
#define MAXTAN 9223372036854775808.0
367

    
368
#ifdef USE_X86LDOUBLE
369

    
370
/* only for x86 */
371
typedef union {
372
    long double d;
373
    struct {
374
        unsigned long long lower;
375
        unsigned short upper;
376
    } l;
377
} CPU86_LDoubleU;
378

    
379
/* the following deal with x86 long double-precision numbers */
380
#define MAXEXPD 0x7fff
381
#define EXPBIAS 16383
382
#define EXPD(fp)        (fp.l.upper & 0x7fff)
383
#define SIGND(fp)        ((fp.l.upper) & 0x8000)
384
#define MANTD(fp)       (fp.l.lower)
385
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
386

    
387
#else
388

    
389
/* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
390
typedef union {
391
    double d;
392
#if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
393
    struct {
394
        uint32_t lower;
395
        int32_t upper;
396
    } l;
397
#else
398
    struct {
399
        int32_t upper;
400
        uint32_t lower;
401
    } l;
402
#endif
403
#ifndef __arm__
404
    int64_t ll;
405
#endif
406
} CPU86_LDoubleU;
407

    
408
/* the following deal with IEEE double-precision numbers */
409
#define MAXEXPD 0x7ff
410
#define EXPBIAS 1023
411
#define EXPD(fp)        (((fp.l.upper) >> 20) & 0x7FF)
412
#define SIGND(fp)        ((fp.l.upper) & 0x80000000)
413
#ifdef __arm__
414
#define MANTD(fp)        (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
415
#else
416
#define MANTD(fp)        (fp.ll & ((1LL << 52) - 1))
417
#endif
418
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
419
#endif
420

    
421
static inline void fpush(void)
422
{
423
    env->fpstt = (env->fpstt - 1) & 7;
424
    env->fptags[env->fpstt] = 0; /* validate stack entry */
425
}
426

    
427
static inline void fpop(void)
428
{
429
    env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
430
    env->fpstt = (env->fpstt + 1) & 7;
431
}
432

    
433
#ifndef USE_X86LDOUBLE
434
static inline CPU86_LDouble helper_fldt(target_ulong ptr)
435
{
436
    CPU86_LDoubleU temp;
437
    int upper, e;
438
    uint64_t ll;
439

    
440
    /* mantissa */
441
    upper = lduw(ptr + 8);
442
    /* XXX: handle overflow ? */
443
    e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
444
    e |= (upper >> 4) & 0x800; /* sign */
445
    ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
446
#ifdef __arm__
447
    temp.l.upper = (e << 20) | (ll >> 32);
448
    temp.l.lower = ll;
449
#else
450
    temp.ll = ll | ((uint64_t)e << 52);
451
#endif
452
    return temp.d;
453
}
454

    
455
static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
456
{
457
    CPU86_LDoubleU temp;
458
    int e;
459

    
460
    temp.d = f;
461
    /* mantissa */
462
    stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
463
    /* exponent + sign */
464
    e = EXPD(temp) - EXPBIAS + 16383;
465
    e |= SIGND(temp) >> 16;
466
    stw(ptr + 8, e);
467
}
468
#else
469

    
470
/* XXX: same endianness assumed */
471

    
472
#ifdef CONFIG_USER_ONLY
473

    
474
static inline CPU86_LDouble helper_fldt(target_ulong ptr)
475
{
476
    return *(CPU86_LDouble *)ptr;
477
}
478

    
479
static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
480
{
481
    *(CPU86_LDouble *)ptr = f;
482
}
483

    
484
#else
485

    
486
/* we use memory access macros */
487

    
488
static inline CPU86_LDouble helper_fldt(target_ulong ptr)
489
{
490
    CPU86_LDoubleU temp;
491

    
492
    temp.l.lower = ldq(ptr);
493
    temp.l.upper = lduw(ptr + 8);
494
    return temp.d;
495
}
496

    
497
static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
498
{
499
    CPU86_LDoubleU temp;
500
    
501
    temp.d = f;
502
    stq(ptr, temp.l.lower);
503
    stw(ptr + 8, temp.l.upper);
504
}
505

    
506
#endif /* !CONFIG_USER_ONLY */
507

    
508
#endif /* USE_X86LDOUBLE */
509

    
510
#define FPUS_IE (1 << 0)
511
#define FPUS_DE (1 << 1)
512
#define FPUS_ZE (1 << 2)
513
#define FPUS_OE (1 << 3)
514
#define FPUS_UE (1 << 4)
515
#define FPUS_PE (1 << 5)
516
#define FPUS_SF (1 << 6)
517
#define FPUS_SE (1 << 7)
518
#define FPUS_B  (1 << 15)
519

    
520
#define FPUC_EM 0x3f
521

    
522
extern const CPU86_LDouble f15rk[7];
523

    
524
void helper_fldt_ST0_A0(void);
525
void helper_fstt_ST0_A0(void);
526
void fpu_raise_exception(void);
527
CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b);
528
void helper_fbld_ST0_A0(void);
529
void helper_fbst_ST0_A0(void);
530
void helper_f2xm1(void);
531
void helper_fyl2x(void);
532
void helper_fptan(void);
533
void helper_fpatan(void);
534
void helper_fxtract(void);
535
void helper_fprem1(void);
536
void helper_fprem(void);
537
void helper_fyl2xp1(void);
538
void helper_fsqrt(void);
539
void helper_fsincos(void);
540
void helper_frndint(void);
541
void helper_fscale(void);
542
void helper_fsin(void);
543
void helper_fcos(void);
544
void helper_fxam_ST0(void);
545
void helper_fstenv(target_ulong ptr, int data32);
546
void helper_fldenv(target_ulong ptr, int data32);
547
void helper_fsave(target_ulong ptr, int data32);
548
void helper_frstor(target_ulong ptr, int data32);
549
void helper_fxsave(target_ulong ptr, int data64);
550
void helper_fxrstor(target_ulong ptr, int data64);
551
void restore_native_fp_state(CPUState *env);
552
void save_native_fp_state(CPUState *env);
553
float approx_rsqrt(float a);
554
float approx_rcp(float a);
555
void update_fp_status(void);
556

    
557
extern const uint8_t parity_table[256];
558
extern const uint8_t rclw_table[32];
559
extern const uint8_t rclb_table[32];
560

    
561
static inline uint32_t compute_eflags(void)
562
{
563
    return env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
564
}
565

    
566
/* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
567
static inline void load_eflags(int eflags, int update_mask)
568
{
569
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
570
    DF = 1 - (2 * ((eflags >> 10) & 1));
571
    env->eflags = (env->eflags & ~update_mask) | 
572
        (eflags & update_mask);
573
}
574

    
575
static inline void env_to_regs(void)
576
{
577
#ifdef reg_EAX
578
    EAX = env->regs[R_EAX];
579
#endif
580
#ifdef reg_ECX
581
    ECX = env->regs[R_ECX];
582
#endif
583
#ifdef reg_EDX
584
    EDX = env->regs[R_EDX];
585
#endif
586
#ifdef reg_EBX
587
    EBX = env->regs[R_EBX];
588
#endif
589
#ifdef reg_ESP
590
    ESP = env->regs[R_ESP];
591
#endif
592
#ifdef reg_EBP
593
    EBP = env->regs[R_EBP];
594
#endif
595
#ifdef reg_ESI
596
    ESI = env->regs[R_ESI];
597
#endif
598
#ifdef reg_EDI
599
    EDI = env->regs[R_EDI];
600
#endif
601
}
602

    
603
static inline void regs_to_env(void)
604
{
605
#ifdef reg_EAX
606
    env->regs[R_EAX] = EAX;
607
#endif
608
#ifdef reg_ECX
609
    env->regs[R_ECX] = ECX;
610
#endif
611
#ifdef reg_EDX
612
    env->regs[R_EDX] = EDX;
613
#endif
614
#ifdef reg_EBX
615
    env->regs[R_EBX] = EBX;
616
#endif
617
#ifdef reg_ESP
618
    env->regs[R_ESP] = ESP;
619
#endif
620
#ifdef reg_EBP
621
    env->regs[R_EBP] = EBP;
622
#endif
623
#ifdef reg_ESI
624
    env->regs[R_ESI] = ESI;
625
#endif
626
#ifdef reg_EDI
627
    env->regs[R_EDI] = EDI;
628
#endif
629
}