Statistics
| Branch: | Revision:

root / target-i386 / exec.h @ 19e6c4b8

History | View | Annotate | Download (13.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 "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
#ifdef USE_FP_CONVERT
99
#define FP_CONVERT  (env->fp_convert)
100
#endif
101

    
102
#include "cpu.h"
103
#include "exec-all.h"
104

    
105
typedef struct CCTable {
106
    int (*compute_all)(void); /* return all the flags */
107
    int (*compute_c)(void);  /* return the C flag */
108
} CCTable;
109

    
110
extern CCTable cc_table[];
111

    
112
void load_seg(int seg_reg, int selector);
113
void helper_ljmp_protected_T0_T1(int next_eip);
114
void helper_lcall_real_T0_T1(int shift, int next_eip);
115
void helper_lcall_protected_T0_T1(int shift, int next_eip);
116
void helper_iret_real(int shift);
117
void helper_iret_protected(int shift, int next_eip);
118
void helper_lret_protected(int shift, int addend);
119
void helper_lldt_T0(void);
120
void helper_ltr_T0(void);
121
void helper_movl_crN_T0(int reg);
122
void helper_movl_drN_T0(int reg);
123
void helper_invlpg(target_ulong addr);
124
void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0);
125
void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3);
126
void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4);
127
void cpu_x86_flush_tlb(CPUX86State *env, target_ulong addr);
128
int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr,
129
                             int is_write, int mmu_idx, int is_softmmu);
130
void tlb_fill(target_ulong addr, int is_write, int mmu_idx,
131
              void *retaddr);
132
void __hidden cpu_lock(void);
133
void __hidden cpu_unlock(void);
134
void do_interrupt(int intno, int is_int, int error_code,
135
                  target_ulong next_eip, int is_hw);
136
void do_interrupt_user(int intno, int is_int, int error_code,
137
                       target_ulong next_eip);
138
void raise_interrupt(int intno, int is_int, int error_code,
139
                     int next_eip_addend);
140
void raise_exception_err(int exception_index, int error_code);
141
void raise_exception(int exception_index);
142
void do_smm_enter(void);
143
void __hidden cpu_loop_exit(void);
144

    
145
void OPPROTO op_movl_eflags_T0(void);
146
void OPPROTO op_movl_T0_eflags(void);
147

    
148
#include "helper.h"
149

    
150
void helper_mulq_EAX_T0(void);
151
void helper_imulq_EAX_T0(void);
152
void helper_imulq_T0_T1(void);
153
void helper_divq_EAX_T0(void);
154
void helper_idivq_EAX_T0(void);
155
void helper_bswapq_T0(void);
156
void helper_cmpxchg8b(void);
157
void helper_single_step(void);
158
void helper_cpuid(void);
159
void helper_enter_level(int level, int data32);
160
void helper_enter64_level(int level, int data64);
161
void helper_sysenter(void);
162
void helper_sysexit(void);
163
void helper_syscall(int next_eip_addend);
164
void helper_sysret(int dflag);
165
void helper_rdtsc(void);
166
void helper_rdpmc(void);
167
void helper_rdmsr(void);
168
void helper_wrmsr(void);
169
void helper_lsl(void);
170
void helper_lar(void);
171
void helper_verr(void);
172
void helper_verw(void);
173
void helper_rsm(void);
174

    
175
void check_iob_T0(void);
176
void check_iow_T0(void);
177
void check_iol_T0(void);
178
void check_iob_DX(void);
179
void check_iow_DX(void);
180
void check_iol_DX(void);
181

    
182
#if !defined(CONFIG_USER_ONLY)
183

    
184
#include "softmmu_exec.h"
185

    
186
static inline double ldfq(target_ulong ptr)
187
{
188
    union {
189
        double d;
190
        uint64_t i;
191
    } u;
192
    u.i = ldq(ptr);
193
    return u.d;
194
}
195

    
196
static inline void stfq(target_ulong ptr, double v)
197
{
198
    union {
199
        double d;
200
        uint64_t i;
201
    } u;
202
    u.d = v;
203
    stq(ptr, u.i);
204
}
205

    
206
static inline float ldfl(target_ulong ptr)
207
{
208
    union {
209
        float f;
210
        uint32_t i;
211
    } u;
212
    u.i = ldl(ptr);
213
    return u.f;
214
}
215

    
216
static inline void stfl(target_ulong ptr, float v)
217
{
218
    union {
219
        float f;
220
        uint32_t i;
221
    } u;
222
    u.f = v;
223
    stl(ptr, u.i);
224
}
225

    
226
#endif /* !defined(CONFIG_USER_ONLY) */
227

    
228
#ifdef USE_X86LDOUBLE
229
/* use long double functions */
230
#define floatx_to_int32 floatx80_to_int32
231
#define floatx_to_int64 floatx80_to_int64
232
#define floatx_to_int32_round_to_zero floatx80_to_int32_round_to_zero
233
#define floatx_to_int64_round_to_zero floatx80_to_int64_round_to_zero
234
#define int32_to_floatx int32_to_floatx80
235
#define int64_to_floatx int64_to_floatx80
236
#define float32_to_floatx float32_to_floatx80
237
#define float64_to_floatx float64_to_floatx80
238
#define floatx_to_float32 floatx80_to_float32
239
#define floatx_to_float64 floatx80_to_float64
240
#define floatx_abs floatx80_abs
241
#define floatx_chs floatx80_chs
242
#define floatx_round_to_int floatx80_round_to_int
243
#define floatx_compare floatx80_compare
244
#define floatx_compare_quiet floatx80_compare_quiet
245
#define sin sinl
246
#define cos cosl
247
#define sqrt sqrtl
248
#define pow powl
249
#define log logl
250
#define tan tanl
251
#define atan2 atan2l
252
#define floor floorl
253
#define ceil ceill
254
#define ldexp ldexpl
255
#else
256
#define floatx_to_int32 float64_to_int32
257
#define floatx_to_int64 float64_to_int64
258
#define floatx_to_int32_round_to_zero float64_to_int32_round_to_zero
259
#define floatx_to_int64_round_to_zero float64_to_int64_round_to_zero
260
#define int32_to_floatx int32_to_float64
261
#define int64_to_floatx int64_to_float64
262
#define float32_to_floatx float32_to_float64
263
#define float64_to_floatx(x, e) (x)
264
#define floatx_to_float32 float64_to_float32
265
#define floatx_to_float64(x, e) (x)
266
#define floatx_abs float64_abs
267
#define floatx_chs float64_chs
268
#define floatx_round_to_int float64_round_to_int
269
#define floatx_compare float64_compare
270
#define floatx_compare_quiet float64_compare_quiet
271
#endif
272

    
273
extern CPU86_LDouble sin(CPU86_LDouble x);
274
extern CPU86_LDouble cos(CPU86_LDouble x);
275
extern CPU86_LDouble sqrt(CPU86_LDouble x);
276
extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
277
extern CPU86_LDouble log(CPU86_LDouble x);
278
extern CPU86_LDouble tan(CPU86_LDouble x);
279
extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
280
extern CPU86_LDouble floor(CPU86_LDouble x);
281
extern CPU86_LDouble ceil(CPU86_LDouble x);
282

    
283
#define RC_MASK         0xc00
284
#define RC_NEAR                0x000
285
#define RC_DOWN                0x400
286
#define RC_UP                0x800
287
#define RC_CHOP                0xc00
288

    
289
#define MAXTAN 9223372036854775808.0
290

    
291
#ifdef USE_X86LDOUBLE
292

    
293
/* only for x86 */
294
typedef union {
295
    long double d;
296
    struct {
297
        unsigned long long lower;
298
        unsigned short upper;
299
    } l;
300
} CPU86_LDoubleU;
301

    
302
/* the following deal with x86 long double-precision numbers */
303
#define MAXEXPD 0x7fff
304
#define EXPBIAS 16383
305
#define EXPD(fp)        (fp.l.upper & 0x7fff)
306
#define SIGND(fp)        ((fp.l.upper) & 0x8000)
307
#define MANTD(fp)       (fp.l.lower)
308
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
309

    
310
#else
311

    
312
/* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
313
typedef union {
314
    double d;
315
#if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
316
    struct {
317
        uint32_t lower;
318
        int32_t upper;
319
    } l;
320
#else
321
    struct {
322
        int32_t upper;
323
        uint32_t lower;
324
    } l;
325
#endif
326
#ifndef __arm__
327
    int64_t ll;
328
#endif
329
} CPU86_LDoubleU;
330

    
331
/* the following deal with IEEE double-precision numbers */
332
#define MAXEXPD 0x7ff
333
#define EXPBIAS 1023
334
#define EXPD(fp)        (((fp.l.upper) >> 20) & 0x7FF)
335
#define SIGND(fp)        ((fp.l.upper) & 0x80000000)
336
#ifdef __arm__
337
#define MANTD(fp)        (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
338
#else
339
#define MANTD(fp)        (fp.ll & ((1LL << 52) - 1))
340
#endif
341
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
342
#endif
343

    
344
static inline void fpush(void)
345
{
346
    env->fpstt = (env->fpstt - 1) & 7;
347
    env->fptags[env->fpstt] = 0; /* validate stack entry */
348
}
349

    
350
static inline void fpop(void)
351
{
352
    env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
353
    env->fpstt = (env->fpstt + 1) & 7;
354
}
355

    
356
#ifndef USE_X86LDOUBLE
357
static inline CPU86_LDouble helper_fldt(target_ulong ptr)
358
{
359
    CPU86_LDoubleU temp;
360
    int upper, e;
361
    uint64_t ll;
362

    
363
    /* mantissa */
364
    upper = lduw(ptr + 8);
365
    /* XXX: handle overflow ? */
366
    e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
367
    e |= (upper >> 4) & 0x800; /* sign */
368
    ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
369
#ifdef __arm__
370
    temp.l.upper = (e << 20) | (ll >> 32);
371
    temp.l.lower = ll;
372
#else
373
    temp.ll = ll | ((uint64_t)e << 52);
374
#endif
375
    return temp.d;
376
}
377

    
378
static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
379
{
380
    CPU86_LDoubleU temp;
381
    int e;
382

    
383
    temp.d = f;
384
    /* mantissa */
385
    stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
386
    /* exponent + sign */
387
    e = EXPD(temp) - EXPBIAS + 16383;
388
    e |= SIGND(temp) >> 16;
389
    stw(ptr + 8, e);
390
}
391
#else
392

    
393
/* we use memory access macros */
394

    
395
static inline CPU86_LDouble helper_fldt(target_ulong ptr)
396
{
397
    CPU86_LDoubleU temp;
398

    
399
    temp.l.lower = ldq(ptr);
400
    temp.l.upper = lduw(ptr + 8);
401
    return temp.d;
402
}
403

    
404
static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
405
{
406
    CPU86_LDoubleU temp;
407

    
408
    temp.d = f;
409
    stq(ptr, temp.l.lower);
410
    stw(ptr + 8, temp.l.upper);
411
}
412

    
413
#endif /* USE_X86LDOUBLE */
414

    
415
#define FPUS_IE (1 << 0)
416
#define FPUS_DE (1 << 1)
417
#define FPUS_ZE (1 << 2)
418
#define FPUS_OE (1 << 3)
419
#define FPUS_UE (1 << 4)
420
#define FPUS_PE (1 << 5)
421
#define FPUS_SF (1 << 6)
422
#define FPUS_SE (1 << 7)
423
#define FPUS_B  (1 << 15)
424

    
425
#define FPUC_EM 0x3f
426

    
427
extern const CPU86_LDouble f15rk[7];
428

    
429
void fpu_raise_exception(void);
430
void restore_native_fp_state(CPUState *env);
431
void save_native_fp_state(CPUState *env);
432
float approx_rsqrt(float a);
433
float approx_rcp(float a);
434
void update_fp_status(void);
435
void helper_hlt(void);
436
void helper_monitor(void);
437
void helper_mwait(void);
438
void helper_vmrun(target_ulong addr);
439
void helper_vmmcall(void);
440
void helper_vmload(target_ulong addr);
441
void helper_vmsave(target_ulong addr);
442
void helper_stgi(void);
443
void helper_clgi(void);
444
void helper_skinit(void);
445
void helper_invlpga(void);
446
void vmexit(uint64_t exit_code, uint64_t exit_info_1);
447

    
448
extern const uint8_t parity_table[256];
449
extern const uint8_t rclw_table[32];
450
extern const uint8_t rclb_table[32];
451

    
452
static inline uint32_t compute_eflags(void)
453
{
454
    return env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
455
}
456

    
457
/* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
458
static inline void load_eflags(int eflags, int update_mask)
459
{
460
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
461
    DF = 1 - (2 * ((eflags >> 10) & 1));
462
    env->eflags = (env->eflags & ~update_mask) |
463
        (eflags & update_mask);
464
}
465

    
466
static inline void env_to_regs(void)
467
{
468
#ifdef reg_EAX
469
    EAX = env->regs[R_EAX];
470
#endif
471
#ifdef reg_ECX
472
    ECX = env->regs[R_ECX];
473
#endif
474
#ifdef reg_EDX
475
    EDX = env->regs[R_EDX];
476
#endif
477
#ifdef reg_EBX
478
    EBX = env->regs[R_EBX];
479
#endif
480
#ifdef reg_ESP
481
    ESP = env->regs[R_ESP];
482
#endif
483
#ifdef reg_EBP
484
    EBP = env->regs[R_EBP];
485
#endif
486
#ifdef reg_ESI
487
    ESI = env->regs[R_ESI];
488
#endif
489
#ifdef reg_EDI
490
    EDI = env->regs[R_EDI];
491
#endif
492
}
493

    
494
static inline void regs_to_env(void)
495
{
496
#ifdef reg_EAX
497
    env->regs[R_EAX] = EAX;
498
#endif
499
#ifdef reg_ECX
500
    env->regs[R_ECX] = ECX;
501
#endif
502
#ifdef reg_EDX
503
    env->regs[R_EDX] = EDX;
504
#endif
505
#ifdef reg_EBX
506
    env->regs[R_EBX] = EBX;
507
#endif
508
#ifdef reg_ESP
509
    env->regs[R_ESP] = ESP;
510
#endif
511
#ifdef reg_EBP
512
    env->regs[R_EBP] = EBP;
513
#endif
514
#ifdef reg_ESI
515
    env->regs[R_ESI] = ESI;
516
#endif
517
#ifdef reg_EDI
518
    env->regs[R_EDI] = EDI;
519
#endif
520
}
521

    
522
static inline int cpu_halted(CPUState *env) {
523
    /* handle exit of HALTED state */
524
    if (!(env->hflags & HF_HALTED_MASK))
525
        return 0;
526
    /* disable halt condition */
527
    if (((env->interrupt_request & CPU_INTERRUPT_HARD) &&
528
         (env->eflags & IF_MASK)) ||
529
        (env->interrupt_request & CPU_INTERRUPT_NMI)) {
530
        env->hflags &= ~HF_HALTED_MASK;
531
        return 0;
532
    }
533
    return EXCP_HALTED;
534
}
535