exec.h 13.9 KB
Newer Older
B
bellard 已提交
1
/*
2
 *  i386 execution defines
B
bellard 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 *  Copyright (c) 2003 Fabrice Bellard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
B
bellard 已提交
20
#include "config.h"
B
bellard 已提交
21 22
#include "dyngen-exec.h"

B
bellard 已提交
23 24 25 26 27 28 29
/* XXX: factorize this mess */
#ifdef TARGET_X86_64
#define TARGET_LONG_BITS 64
#else
#define TARGET_LONG_BITS 32
#endif

B
bellard 已提交
30 31
#include "cpu-defs.h"

B
bellard 已提交
32
/* at least 4 register variables are defined */
B
bellard 已提交
33
register struct CPUX86State *env asm(AREG0);
B
bellard 已提交
34

35 36
#ifndef CPU_NO_GLOBAL_REGS

B
bellard 已提交
37 38 39 40 41 42
#if TARGET_LONG_BITS > HOST_LONG_BITS

/* no registers can be used */
#define T0 (env->t0)
#define T1 (env->t1)
#define T2 (env->t2)
B
bellard 已提交
43

B
bellard 已提交
44 45 46 47 48 49 50
#else

/* XXX: use unsigned long instead of target_ulong - better code will
   be generated for 64 bit CPUs */
register target_ulong T0 asm(AREG1);
register target_ulong T1 asm(AREG2);
register target_ulong T2 asm(AREG3);
B
bellard 已提交
51

B
bellard 已提交
52
#endif /* ! (TARGET_LONG_BITS > HOST_LONG_BITS) */
B
bellard 已提交
53

54 55
#endif /* ! CPU_NO_GLOBAL_REGS */

B
bellard 已提交
56 57
#define A0 T2

B
bellard 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
extern FILE *logfile;
extern int loglevel;

#ifndef reg_EAX
#define EAX (env->regs[R_EAX])
#endif
#ifndef reg_ECX
#define ECX (env->regs[R_ECX])
#endif
#ifndef reg_EDX
#define EDX (env->regs[R_EDX])
#endif
#ifndef reg_EBX
#define EBX (env->regs[R_EBX])
#endif
#ifndef reg_ESP
#define ESP (env->regs[R_ESP])
#endif
#ifndef reg_EBP
#define EBP (env->regs[R_EBP])
#endif
#ifndef reg_ESI
#define ESI (env->regs[R_ESI])
#endif
#ifndef reg_EDI
#define EDI (env->regs[R_EDI])
#endif
#define EIP  (env->eip)
#define DF  (env->df)

#define CC_SRC (env->cc_src)
#define CC_DST (env->cc_dst)
#define CC_OP  (env->cc_op)

/* float macros */
#define FT0    (env->ft0)
B
bellard 已提交
94 95
#define ST0    (env->fpregs[env->fpstt].d)
#define ST(n)  (env->fpregs[(env->fpstt + (n)) & 7].d)
B
bellard 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#define ST1    ST(1)

#ifdef USE_FP_CONVERT
#define FP_CONVERT  (env->fp_convert)
#endif

#include "cpu.h"
#include "exec-all.h"

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

extern CCTable cc_table[];

112
void load_seg(int seg_reg, int selector);
B
bellard 已提交
113
void helper_ljmp_protected_T0_T1(int next_eip);
B
bellard 已提交
114 115 116
void helper_lcall_real_T0_T1(int shift, int next_eip);
void helper_lcall_protected_T0_T1(int shift, int next_eip);
void helper_iret_real(int shift);
B
bellard 已提交
117
void helper_iret_protected(int shift, int next_eip);
B
bellard 已提交
118 119 120 121 122
void helper_lret_protected(int shift, int addend);
void helper_lldt_T0(void);
void helper_ltr_T0(void);
void helper_movl_crN_T0(int reg);
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);
B
bellard 已提交
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 130
                             int is_write, int mmu_idx, int is_softmmu);
void tlb_fill(target_ulong addr, int is_write, int mmu_idx,
B
bellard 已提交
131
              void *retaddr);
B
bellard 已提交
132 133
void __hidden cpu_lock(void);
void __hidden cpu_unlock(void);
134
void do_interrupt(int intno, int is_int, int error_code,
B
bellard 已提交
135
                  target_ulong next_eip, int is_hw);
136
void do_interrupt_user(int intno, int is_int, int error_code,
B
bellard 已提交
137
                       target_ulong next_eip);
138
void raise_interrupt(int intno, int is_int, int error_code,
139
                     int next_eip_addend);
B
bellard 已提交
140 141
void raise_exception_err(int exception_index, int error_code);
void raise_exception(int exception_index);
B
bellard 已提交
142
void do_smm_enter(void);
B
bellard 已提交
143 144 145 146
void __hidden cpu_loop_exit(void);

void OPPROTO op_movl_eflags_T0(void);
void OPPROTO op_movl_T0_eflags(void);
B
bellard 已提交
147 148 149

#include "helper.h"

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

B
bellard 已提交
175 176 177 178 179 180 181
void check_iob_T0(void);
void check_iow_T0(void);
void check_iol_T0(void);
void check_iob_DX(void);
void check_iow_DX(void);
void check_iol_DX(void);

182 183
#if !defined(CONFIG_USER_ONLY)

184
#include "softmmu_exec.h"
185

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

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

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

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

#endif /* !defined(CONFIG_USER_ONLY) */

B
bellard 已提交
228 229
#ifdef USE_X86LDOUBLE
/* use long double functions */
B
bellard 已提交
230 231
#define floatx_to_int32 floatx80_to_int32
#define floatx_to_int64 floatx80_to_int64
B
bellard 已提交
232 233
#define floatx_to_int32_round_to_zero floatx80_to_int32_round_to_zero
#define floatx_to_int64_round_to_zero floatx80_to_int64_round_to_zero
B
bellard 已提交
234 235 236
#define floatx_abs floatx80_abs
#define floatx_chs floatx80_chs
#define floatx_round_to_int floatx80_round_to_int
B
bellard 已提交
237 238
#define floatx_compare floatx80_compare
#define floatx_compare_quiet floatx80_compare_quiet
B
bellard 已提交
239 240 241 242 243 244 245 246 247
#define sin sinl
#define cos cosl
#define sqrt sqrtl
#define pow powl
#define log logl
#define tan tanl
#define atan2 atan2l
#define floor floorl
#define ceil ceill
248
#define ldexp ldexpl
B
bellard 已提交
249
#else
B
bellard 已提交
250 251
#define floatx_to_int32 float64_to_int32
#define floatx_to_int64 float64_to_int64
B
bellard 已提交
252 253
#define floatx_to_int32_round_to_zero float64_to_int32_round_to_zero
#define floatx_to_int64_round_to_zero float64_to_int64_round_to_zero
B
bellard 已提交
254 255 256
#define floatx_abs float64_abs
#define floatx_chs float64_chs
#define floatx_round_to_int float64_round_to_int
B
bellard 已提交
257 258
#define floatx_compare float64_compare
#define floatx_compare_quiet float64_compare_quiet
B
bellard 已提交
259
#endif
B
bellard 已提交
260

B
bellard 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
extern CPU86_LDouble sin(CPU86_LDouble x);
extern CPU86_LDouble cos(CPU86_LDouble x);
extern CPU86_LDouble sqrt(CPU86_LDouble x);
extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
extern CPU86_LDouble log(CPU86_LDouble x);
extern CPU86_LDouble tan(CPU86_LDouble x);
extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
extern CPU86_LDouble floor(CPU86_LDouble x);
extern CPU86_LDouble ceil(CPU86_LDouble x);

#define RC_MASK         0xc00
#define RC_NEAR		0x000
#define RC_DOWN		0x400
#define RC_UP		0x800
#define RC_CHOP		0xc00

#define MAXTAN 9223372036854775808.0

#ifdef USE_X86LDOUBLE

/* only for x86 */
typedef union {
    long double d;
    struct {
        unsigned long long lower;
        unsigned short upper;
    } l;
} CPU86_LDoubleU;

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

#else

/* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
typedef union {
    double d;
#if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
    struct {
        uint32_t lower;
        int32_t upper;
    } l;
#else
    struct {
        int32_t upper;
        uint32_t lower;
    } l;
#endif
#ifndef __arm__
    int64_t ll;
#endif
} CPU86_LDoubleU;

/* the following deal with IEEE double-precision numbers */
#define MAXEXPD 0x7ff
#define EXPBIAS 1023
#define EXPD(fp)	(((fp.l.upper) >> 20) & 0x7FF)
#define SIGND(fp)	((fp.l.upper) & 0x80000000)
#ifdef __arm__
#define MANTD(fp)	(fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
#else
#define MANTD(fp)	(fp.ll & ((1LL << 52) - 1))
#endif
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
#endif

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

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

#ifndef USE_X86LDOUBLE
B
bellard 已提交
345
static inline CPU86_LDouble helper_fldt(target_ulong ptr)
B
bellard 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
{
    CPU86_LDoubleU temp;
    int upper, e;
    uint64_t ll;

    /* mantissa */
    upper = lduw(ptr + 8);
    /* XXX: handle overflow ? */
    e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
    e |= (upper >> 4) & 0x800; /* sign */
    ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
#ifdef __arm__
    temp.l.upper = (e << 20) | (ll >> 32);
    temp.l.lower = ll;
#else
    temp.ll = ll | ((uint64_t)e << 52);
#endif
    return temp.d;
}

B
bellard 已提交
366
static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
B
bellard 已提交
367 368 369 370 371 372 373 374 375 376 377 378
{
    CPU86_LDoubleU temp;
    int e;

    temp.d = f;
    /* mantissa */
    stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
    /* exponent + sign */
    e = EXPD(temp) - EXPBIAS + 16383;
    e |= SIGND(temp) >> 16;
    stw(ptr + 8, e);
}
379 380 381 382 383 384
#else

/* XXX: same endianness assumed */

#ifdef CONFIG_USER_ONLY

B
bellard 已提交
385
static inline CPU86_LDouble helper_fldt(target_ulong ptr)
386
{
B
bellard 已提交
387
    return *(CPU86_LDouble *)(unsigned long)ptr;
388 389
}

B
bellard 已提交
390
static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
391
{
B
bellard 已提交
392
    *(CPU86_LDouble *)(unsigned long)ptr = f;
393 394 395 396 397 398
}

#else

/* we use memory access macros */

B
bellard 已提交
399
static inline CPU86_LDouble helper_fldt(target_ulong ptr)
400 401 402 403 404 405 406 407
{
    CPU86_LDoubleU temp;

    temp.l.lower = ldq(ptr);
    temp.l.upper = lduw(ptr + 8);
    return temp.d;
}

B
bellard 已提交
408
static inline void helper_fstt(CPU86_LDouble f, target_ulong ptr)
409 410
{
    CPU86_LDoubleU temp;
411

412 413 414 415 416 417 418 419
    temp.d = f;
    stq(ptr, temp.l.lower);
    stw(ptr + 8, temp.l.upper);
}

#endif /* !CONFIG_USER_ONLY */

#endif /* USE_X86LDOUBLE */
B
bellard 已提交
420

B
bellard 已提交
421 422 423 424 425 426 427 428 429 430 431 432
#define FPUS_IE (1 << 0)
#define FPUS_DE (1 << 1)
#define FPUS_ZE (1 << 2)
#define FPUS_OE (1 << 3)
#define FPUS_UE (1 << 4)
#define FPUS_PE (1 << 5)
#define FPUS_SF (1 << 6)
#define FPUS_SE (1 << 7)
#define FPUS_B  (1 << 15)

#define FPUC_EM 0x3f

433
extern const CPU86_LDouble f15rk[7];
B
bellard 已提交
434 435 436

void helper_fldt_ST0_A0(void);
void helper_fstt_ST0_A0(void);
B
bellard 已提交
437 438
void fpu_raise_exception(void);
CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b);
B
bellard 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
void helper_fbld_ST0_A0(void);
void helper_fbst_ST0_A0(void);
void helper_f2xm1(void);
void helper_fyl2x(void);
void helper_fptan(void);
void helper_fpatan(void);
void helper_fxtract(void);
void helper_fprem1(void);
void helper_fprem(void);
void helper_fyl2xp1(void);
void helper_fsqrt(void);
void helper_fsincos(void);
void helper_frndint(void);
void helper_fscale(void);
void helper_fsin(void);
void helper_fcos(void);
void helper_fxam_ST0(void);
B
bellard 已提交
456 457 458 459 460 461
void helper_fstenv(target_ulong ptr, int data32);
void helper_fldenv(target_ulong ptr, int data32);
void helper_fsave(target_ulong ptr, int data32);
void helper_frstor(target_ulong ptr, int data32);
void helper_fxsave(target_ulong ptr, int data64);
void helper_fxrstor(target_ulong ptr, int data64);
B
bellard 已提交
462 463
void restore_native_fp_state(CPUState *env);
void save_native_fp_state(CPUState *env);
B
bellard 已提交
464 465
float approx_rsqrt(float a);
float approx_rcp(float a);
B
bellard 已提交
466
void update_fp_status(void);
B
bellard 已提交
467 468 469
void helper_hlt(void);
void helper_monitor(void);
void helper_mwait(void);
T
ths 已提交
470 471 472 473 474 475 476 477 478
void helper_vmrun(target_ulong addr);
void helper_vmmcall(void);
void helper_vmload(target_ulong addr);
void helper_vmsave(target_ulong addr);
void helper_stgi(void);
void helper_clgi(void);
void helper_skinit(void);
void helper_invlpga(void);
void vmexit(uint64_t exit_code, uint64_t exit_info_1);
B
bellard 已提交
479

480 481 482
extern const uint8_t parity_table[256];
extern const uint8_t rclw_table[32];
extern const uint8_t rclb_table[32];
B
bellard 已提交
483 484 485 486 487 488 489 490 491 492 493

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

/* NOTE: CC_OP must be modified manually to CC_OP_EFLAGS */
static inline void load_eflags(int eflags, int update_mask)
{
    CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
    DF = 1 - (2 * ((eflags >> 10) & 1));
494
    env->eflags = (env->eflags & ~update_mask) |
B
bellard 已提交
495 496 497
        (eflags & update_mask);
}

B
bellard 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
static inline void env_to_regs(void)
{
#ifdef reg_EAX
    EAX = env->regs[R_EAX];
#endif
#ifdef reg_ECX
    ECX = env->regs[R_ECX];
#endif
#ifdef reg_EDX
    EDX = env->regs[R_EDX];
#endif
#ifdef reg_EBX
    EBX = env->regs[R_EBX];
#endif
#ifdef reg_ESP
    ESP = env->regs[R_ESP];
#endif
#ifdef reg_EBP
    EBP = env->regs[R_EBP];
#endif
#ifdef reg_ESI
    ESI = env->regs[R_ESI];
#endif
#ifdef reg_EDI
    EDI = env->regs[R_EDI];
#endif
}

static inline void regs_to_env(void)
{
#ifdef reg_EAX
    env->regs[R_EAX] = EAX;
#endif
#ifdef reg_ECX
    env->regs[R_ECX] = ECX;
#endif
#ifdef reg_EDX
    env->regs[R_EDX] = EDX;
#endif
#ifdef reg_EBX
    env->regs[R_EBX] = EBX;
#endif
#ifdef reg_ESP
    env->regs[R_ESP] = ESP;
#endif
#ifdef reg_EBP
    env->regs[R_EBP] = EBP;
#endif
#ifdef reg_ESI
    env->regs[R_ESI] = ESI;
#endif
#ifdef reg_EDI
    env->regs[R_EDI] = EDI;
#endif
}
553 554 555

static inline int cpu_halted(CPUState *env) {
    /* handle exit of HALTED state */
T
ths 已提交
556
    if (!(env->hflags & HF_HALTED_MASK))
557 558
        return 0;
    /* disable halt condition */
A
aurel32 已提交
559 560 561
    if (((env->interrupt_request & CPU_INTERRUPT_HARD) &&
         (env->eflags & IF_MASK)) ||
        (env->interrupt_request & CPU_INTERRUPT_NMI)) {
562 563 564 565 566
        env->hflags &= ~HF_HALTED_MASK;
        return 0;
    }
    return EXCP_HALTED;
}
T
ths 已提交
567