Statistics
| Branch: | Revision:

root / target-i386 / exec.h @ 57fec1fe

History | View | Annotate | Download (14.4 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 mmu_idx, int is_softmmu);
167
void tlb_fill(target_ulong addr, int is_write, int mmu_idx,
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 do_smm_enter(void);
180
void __hidden cpu_loop_exit(void);
181

    
182
void OPPROTO op_movl_eflags_T0(void);
183
void OPPROTO op_movl_T0_eflags(void);
184

    
185
#include "helper.h"
186

    
187
void helper_mulq_EAX_T0(void);
188
void helper_imulq_EAX_T0(void);
189
void helper_imulq_T0_T1(void);
190
void helper_divq_EAX_T0(void);
191
void helper_idivq_EAX_T0(void);
192
void helper_bswapq_T0(void);
193
void helper_cmpxchg8b(void);
194
void helper_single_step(void);
195
void helper_cpuid(void);
196
void helper_enter_level(int level, int data32);
197
void helper_enter64_level(int level, int data64);
198
void helper_sysenter(void);
199
void helper_sysexit(void);
200
void helper_syscall(int next_eip_addend);
201
void helper_sysret(int dflag);
202
void helper_rdtsc(void);
203
void helper_rdpmc(void);
204
void helper_rdmsr(void);
205
void helper_wrmsr(void);
206
void helper_lsl(void);
207
void helper_lar(void);
208
void helper_verr(void);
209
void helper_verw(void);
210
void helper_rsm(void);
211

    
212
void check_iob_T0(void);
213
void check_iow_T0(void);
214
void check_iol_T0(void);
215
void check_iob_DX(void);
216
void check_iow_DX(void);
217
void check_iol_DX(void);
218

    
219
#if !defined(CONFIG_USER_ONLY)
220

    
221
#include "softmmu_exec.h"
222

    
223
static inline double ldfq(target_ulong ptr)
224
{
225
    union {
226
        double d;
227
        uint64_t i;
228
    } u;
229
    u.i = ldq(ptr);
230
    return u.d;
231
}
232

    
233
static inline void stfq(target_ulong ptr, double v)
234
{
235
    union {
236
        double d;
237
        uint64_t i;
238
    } u;
239
    u.d = v;
240
    stq(ptr, u.i);
241
}
242

    
243
static inline float ldfl(target_ulong ptr)
244
{
245
    union {
246
        float f;
247
        uint32_t i;
248
    } u;
249
    u.i = ldl(ptr);
250
    return u.f;
251
}
252

    
253
static inline void stfl(target_ulong ptr, float v)
254
{
255
    union {
256
        float f;
257
        uint32_t i;
258
    } u;
259
    u.f = v;
260
    stl(ptr, u.i);
261
}
262

    
263
#endif /* !defined(CONFIG_USER_ONLY) */
264

    
265
#ifdef USE_X86LDOUBLE
266
/* use long double functions */
267
#define floatx_to_int32 floatx80_to_int32
268
#define floatx_to_int64 floatx80_to_int64
269
#define floatx_to_int32_round_to_zero floatx80_to_int32_round_to_zero
270
#define floatx_to_int64_round_to_zero floatx80_to_int64_round_to_zero
271
#define floatx_abs floatx80_abs
272
#define floatx_chs floatx80_chs
273
#define floatx_round_to_int floatx80_round_to_int
274
#define floatx_compare floatx80_compare
275
#define floatx_compare_quiet floatx80_compare_quiet
276
#define sin sinl
277
#define cos cosl
278
#define sqrt sqrtl
279
#define pow powl
280
#define log logl
281
#define tan tanl
282
#define atan2 atan2l
283
#define floor floorl
284
#define ceil ceill
285
#define ldexp ldexpl
286
#else
287
#define floatx_to_int32 float64_to_int32
288
#define floatx_to_int64 float64_to_int64
289
#define floatx_to_int32_round_to_zero float64_to_int32_round_to_zero
290
#define floatx_to_int64_round_to_zero float64_to_int64_round_to_zero
291
#define floatx_abs float64_abs
292
#define floatx_chs float64_chs
293
#define floatx_round_to_int float64_round_to_int
294
#define floatx_compare float64_compare
295
#define floatx_compare_quiet float64_compare_quiet
296
#endif
297

    
298
extern CPU86_LDouble sin(CPU86_LDouble x);
299
extern CPU86_LDouble cos(CPU86_LDouble x);
300
extern CPU86_LDouble sqrt(CPU86_LDouble x);
301
extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
302
extern CPU86_LDouble log(CPU86_LDouble x);
303
extern CPU86_LDouble tan(CPU86_LDouble x);
304
extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
305
extern CPU86_LDouble floor(CPU86_LDouble x);
306
extern CPU86_LDouble ceil(CPU86_LDouble x);
307

    
308
#define RC_MASK         0xc00
309
#define RC_NEAR                0x000
310
#define RC_DOWN                0x400
311
#define RC_UP                0x800
312
#define RC_CHOP                0xc00
313

    
314
#define MAXTAN 9223372036854775808.0
315

    
316
#ifdef USE_X86LDOUBLE
317

    
318
/* only for x86 */
319
typedef union {
320
    long double d;
321
    struct {
322
        unsigned long long lower;
323
        unsigned short upper;
324
    } l;
325
} CPU86_LDoubleU;
326

    
327
/* the following deal with x86 long double-precision numbers */
328
#define MAXEXPD 0x7fff
329
#define EXPBIAS 16383
330
#define EXPD(fp)        (fp.l.upper & 0x7fff)
331
#define SIGND(fp)        ((fp.l.upper) & 0x8000)
332
#define MANTD(fp)       (fp.l.lower)
333
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
334

    
335
#else
336

    
337
/* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
338
typedef union {
339
    double d;
340
#if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
341
    struct {
342
        uint32_t lower;
343
        int32_t upper;
344
    } l;
345
#else
346
    struct {
347
        int32_t upper;
348
        uint32_t lower;
349
    } l;
350
#endif
351
#ifndef __arm__
352
    int64_t ll;
353
#endif
354
} CPU86_LDoubleU;
355

    
356
/* the following deal with IEEE double-precision numbers */
357
#define MAXEXPD 0x7ff
358
#define EXPBIAS 1023
359
#define EXPD(fp)        (((fp.l.upper) >> 20) & 0x7FF)
360
#define SIGND(fp)        ((fp.l.upper) & 0x80000000)
361
#ifdef __arm__
362
#define MANTD(fp)        (fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
363
#else
364
#define MANTD(fp)        (fp.ll & ((1LL << 52) - 1))
365
#endif
366
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
367
#endif
368

    
369
static inline void fpush(void)
370
{
371
    env->fpstt = (env->fpstt - 1) & 7;
372
    env->fptags[env->fpstt] = 0; /* validate stack entry */
373
}
374

    
375
static inline void fpop(void)
376
{
377
    env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
378
    env->fpstt = (env->fpstt + 1) & 7;
379
}
380

    
381
#ifndef USE_X86LDOUBLE
382
static inline CPU86_LDouble helper_fldt(target_ulong ptr)
383
{
384
    CPU86_LDoubleU temp;
385
    int upper, e;
386
    uint64_t ll;
387

    
388
    /* mantissa */
389
    upper = lduw(ptr + 8);
390
    /* XXX: handle overflow ? */
391
    e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
392
    e |= (upper >> 4) & 0x800; /* sign */
393
    ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
394
#ifdef __arm__
395
    temp.l.upper = (e << 20) | (ll >> 32);
396
    temp.l.lower = ll;
397
#else
398
    temp.ll = ll | ((uint64_t)e << 52);
399
#endif
400
    return temp.d;
401
}
402

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

    
408
    temp.d = f;
409
    /* mantissa */
410
    stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
411
    /* exponent + sign */
412
    e = EXPD(temp) - EXPBIAS + 16383;
413
    e |= SIGND(temp) >> 16;
414
    stw(ptr + 8, e);
415
}
416
#else
417

    
418
/* XXX: same endianness assumed */
419

    
420
#ifdef CONFIG_USER_ONLY
421

    
422
static inline CPU86_LDouble helper_fldt(target_ulong ptr)
423
{
424
    return *(CPU86_LDouble *)(unsigned long)ptr;
425
}
426

    
427
static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
428
{
429
    *(CPU86_LDouble *)(unsigned long)ptr = f;
430
}
431

    
432
#else
433

    
434
/* we use memory access macros */
435

    
436
static inline CPU86_LDouble helper_fldt(target_ulong ptr)
437
{
438
    CPU86_LDoubleU temp;
439

    
440
    temp.l.lower = ldq(ptr);
441
    temp.l.upper = lduw(ptr + 8);
442
    return temp.d;
443
}
444

    
445
static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
446
{
447
    CPU86_LDoubleU temp;
448

    
449
    temp.d = f;
450
    stq(ptr, temp.l.lower);
451
    stw(ptr + 8, temp.l.upper);
452
}
453

    
454
#endif /* !CONFIG_USER_ONLY */
455

    
456
#endif /* USE_X86LDOUBLE */
457

    
458
#define FPUS_IE (1 << 0)
459
#define FPUS_DE (1 << 1)
460
#define FPUS_ZE (1 << 2)
461
#define FPUS_OE (1 << 3)
462
#define FPUS_UE (1 << 4)
463
#define FPUS_PE (1 << 5)
464
#define FPUS_SF (1 << 6)
465
#define FPUS_SE (1 << 7)
466
#define FPUS_B  (1 << 15)
467

    
468
#define FPUC_EM 0x3f
469

    
470
extern const CPU86_LDouble f15rk[7];
471

    
472
void helper_fldt_ST0_A0(void);
473
void helper_fstt_ST0_A0(void);
474
void fpu_raise_exception(void);
475
CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b);
476
void helper_fbld_ST0_A0(void);
477
void helper_fbst_ST0_A0(void);
478
void helper_f2xm1(void);
479
void helper_fyl2x(void);
480
void helper_fptan(void);
481
void helper_fpatan(void);
482
void helper_fxtract(void);
483
void helper_fprem1(void);
484
void helper_fprem(void);
485
void helper_fyl2xp1(void);
486
void helper_fsqrt(void);
487
void helper_fsincos(void);
488
void helper_frndint(void);
489
void helper_fscale(void);
490
void helper_fsin(void);
491
void helper_fcos(void);
492
void helper_fxam_ST0(void);
493
void helper_fstenv(target_ulong ptr, int data32);
494
void helper_fldenv(target_ulong ptr, int data32);
495
void helper_fsave(target_ulong ptr, int data32);
496
void helper_frstor(target_ulong ptr, int data32);
497
void helper_fxsave(target_ulong ptr, int data64);
498
void helper_fxrstor(target_ulong ptr, int data64);
499
void restore_native_fp_state(CPUState *env);
500
void save_native_fp_state(CPUState *env);
501
float approx_rsqrt(float a);
502
float approx_rcp(float a);
503
void update_fp_status(void);
504
void helper_hlt(void);
505
void helper_monitor(void);
506
void helper_mwait(void);
507
void helper_vmrun(target_ulong addr);
508
void helper_vmmcall(void);
509
void helper_vmload(target_ulong addr);
510
void helper_vmsave(target_ulong addr);
511
void helper_stgi(void);
512
void helper_clgi(void);
513
void helper_skinit(void);
514
void helper_invlpga(void);
515
void vmexit(uint64_t exit_code, uint64_t exit_info_1);
516

    
517
extern const uint8_t parity_table[256];
518
extern const uint8_t rclw_table[32];
519
extern const uint8_t rclb_table[32];
520

    
521
static inline uint32_t compute_eflags(void)
522
{
523
    return env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
524
}
525

    
526
/* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
527
static inline void load_eflags(int eflags, int update_mask)
528
{
529
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
530
    DF = 1 - (2 * ((eflags >> 10) & 1));
531
    env->eflags = (env->eflags & ~update_mask) |
532
        (eflags & update_mask);
533
}
534

    
535
static inline void env_to_regs(void)
536
{
537
#ifdef reg_EAX
538
    EAX = env->regs[R_EAX];
539
#endif
540
#ifdef reg_ECX
541
    ECX = env->regs[R_ECX];
542
#endif
543
#ifdef reg_EDX
544
    EDX = env->regs[R_EDX];
545
#endif
546
#ifdef reg_EBX
547
    EBX = env->regs[R_EBX];
548
#endif
549
#ifdef reg_ESP
550
    ESP = env->regs[R_ESP];
551
#endif
552
#ifdef reg_EBP
553
    EBP = env->regs[R_EBP];
554
#endif
555
#ifdef reg_ESI
556
    ESI = env->regs[R_ESI];
557
#endif
558
#ifdef reg_EDI
559
    EDI = env->regs[R_EDI];
560
#endif
561
}
562

    
563
static inline void regs_to_env(void)
564
{
565
#ifdef reg_EAX
566
    env->regs[R_EAX] = EAX;
567
#endif
568
#ifdef reg_ECX
569
    env->regs[R_ECX] = ECX;
570
#endif
571
#ifdef reg_EDX
572
    env->regs[R_EDX] = EDX;
573
#endif
574
#ifdef reg_EBX
575
    env->regs[R_EBX] = EBX;
576
#endif
577
#ifdef reg_ESP
578
    env->regs[R_ESP] = ESP;
579
#endif
580
#ifdef reg_EBP
581
    env->regs[R_EBP] = EBP;
582
#endif
583
#ifdef reg_ESI
584
    env->regs[R_ESI] = ESI;
585
#endif
586
#ifdef reg_EDI
587
    env->regs[R_EDI] = EDI;
588
#endif
589
}
590

    
591
static inline int cpu_halted(CPUState *env) {
592
    /* handle exit of HALTED state */
593
    if (!(env->hflags & HF_HALTED_MASK))
594
        return 0;
595
    /* disable halt condition */
596
    if ((env->interrupt_request & CPU_INTERRUPT_HARD) &&
597
        (env->eflags & IF_MASK)) {
598
        env->hflags &= ~HF_HALTED_MASK;
599
        return 0;
600
    }
601
    return EXCP_HALTED;
602
}
603