helper2.c 27.1 KB
Newer Older
B
bellard 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *  i386 helpers (without register variable usage)
 * 
 *  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
 */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <signal.h>
#include <assert.h>

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

//#define DEBUG_MMU

33 34 35
#ifdef USE_CODE_COPY
#include <asm/ldt.h>
#include <linux/unistd.h>
B
bellard 已提交
36
#include <linux/version.h>
37 38

_syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
B
bellard 已提交
39 40 41

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
#define modify_ldt_ldt_s user_desc
42
#endif
B
bellard 已提交
43
#endif /* USE_CODE_COPY */
44

B
bellard 已提交
45 46 47 48 49 50 51 52 53 54 55
CPUX86State *cpu_x86_init(void)
{
    CPUX86State *env;
    static int inited;

    cpu_exec_init();

    env = malloc(sizeof(CPUX86State));
    if (!env)
        return NULL;
    memset(env, 0, sizeof(CPUX86State));
B
bellard 已提交
56 57 58 59 60 61 62 63 64
    /* init various static tables */
    if (!inited) {
        inited = 1;
        optimize_flags_init();
    }
#ifdef USE_CODE_COPY
    /* testing code for code copy case */
    {
        struct modify_ldt_ldt_s ldt;
B
bellard 已提交
65

B
bellard 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79
        ldt.entry_number = 1;
        ldt.base_addr = (unsigned long)env;
        ldt.limit = (sizeof(CPUState) + 0xfff) >> 12;
        ldt.seg_32bit = 1;
        ldt.contents = MODIFY_LDT_CONTENTS_DATA;
        ldt.read_exec_only = 0;
        ldt.limit_in_pages = 1;
        ldt.seg_not_present = 0;
        ldt.useable = 1;
        modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
        
        asm volatile ("movl %0, %%fs" : : "r" ((1 << 3) | 7));
    }
#endif
B
bellard 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    {
        int family, model, stepping;
#ifdef TARGET_X86_64
        env->cpuid_vendor1 = 0x68747541; /* "Auth" */
        env->cpuid_vendor2 = 0x69746e65; /* "enti" */
        env->cpuid_vendor3 = 0x444d4163; /* "cAMD" */
        family = 6;
        model = 2;
        stepping = 3;
#else
        env->cpuid_vendor1 = 0x756e6547; /* "Genu" */
        env->cpuid_vendor2 = 0x49656e69; /* "ineI" */
        env->cpuid_vendor3 = 0x6c65746e; /* "ntel" */
#if 0
        /* pentium 75-200 */
        family = 5;
        model = 2;
        stepping = 11;
#else
        /* pentium pro */
        family = 6;
        model = 1;
        stepping = 3;
#endif
#endif
        env->cpuid_version = (family << 8) | (model << 4) | stepping;
        env->cpuid_features = (CPUID_FP87 | CPUID_DE | CPUID_PSE |
                               CPUID_TSC | CPUID_MSR | CPUID_MCE |
                               CPUID_CX8 | CPUID_PGE | CPUID_CMOV);
#ifdef TARGET_X86_64
        /* currently not enabled for std i386 because not fully tested */
B
bellard 已提交
111 112
        env->cpuid_features |= CPUID_FXSR | CPUID_MMX | CPUID_SSE | CPUID_SSE2;
        env->cpuid_features |= CPUID_APIC | CPUID_PAE;
B
bellard 已提交
113 114
#endif
    }
B
bellard 已提交
115
    cpu_single_env = env;
B
bellard 已提交
116 117 118 119 120 121 122 123 124 125
    cpu_reset(env);
    return env;
}

/* NOTE: must be called outside the CPU execute loop */
void cpu_reset(CPUX86State *env)
{
    int i;

    memset(env, 0, offsetof(CPUX86State, breakpoints));
126 127

    tlb_flush(env, 1);
B
bellard 已提交
128 129 130

    /* init to reset state */

B
bellard 已提交
131 132 133
#ifdef CONFIG_SOFTMMU
    env->hflags |= HF_SOFTMMU_MASK;
#endif
134 135 136 137 138 139 140 141 142 143 144

    cpu_x86_update_cr0(env, 0x60000010);
    env->a20_mask = 0xffffffff;
    
    env->idt.limit = 0xffff;
    env->gdt.limit = 0xffff;
    env->ldt.limit = 0xffff;
    env->ldt.flags = DESC_P_MASK;
    env->tr.limit = 0xffff;
    env->tr.flags = DESC_P_MASK;
    
B
bellard 已提交
145 146 147 148 149 150
    cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff, 0); 
    cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff, 0);
    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff, 0);
    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff, 0);
    cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff, 0);
    cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff, 0);
151 152 153 154 155 156 157 158 159 160
    
    env->eip = 0xfff0;
    env->regs[R_EDX] = 0x600; /* indicate P6 processor */
    
    env->eflags = 0x2;
    
    /* FPU init */
    for(i = 0;i < 8; i++)
        env->fptags[i] = 1;
    env->fpuc = 0x37f;
B
bellard 已提交
161 162

    env->mxcsr = 0x1f80;
B
bellard 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175
}

void cpu_x86_close(CPUX86State *env)
{
    free(env);
}

/***********************************************************/
/* x86 debug */

static const char *cc_op_str[] = {
    "DYNAMIC",
    "EFLAGS",
B
bellard 已提交
176

177 178 179
    "MULB",
    "MULW",
    "MULL",
B
bellard 已提交
180 181
    "MULQ",

B
bellard 已提交
182 183 184
    "ADDB",
    "ADDW",
    "ADDL",
B
bellard 已提交
185 186
    "ADDQ",

B
bellard 已提交
187 188 189
    "ADCB",
    "ADCW",
    "ADCL",
B
bellard 已提交
190 191
    "ADCQ",

B
bellard 已提交
192 193 194
    "SUBB",
    "SUBW",
    "SUBL",
B
bellard 已提交
195 196
    "SUBQ",

B
bellard 已提交
197 198 199
    "SBBB",
    "SBBW",
    "SBBL",
B
bellard 已提交
200 201
    "SBBQ",

B
bellard 已提交
202 203 204
    "LOGICB",
    "LOGICW",
    "LOGICL",
B
bellard 已提交
205 206
    "LOGICQ",

B
bellard 已提交
207 208 209
    "INCB",
    "INCW",
    "INCL",
B
bellard 已提交
210 211
    "INCQ",

B
bellard 已提交
212 213 214
    "DECB",
    "DECW",
    "DECL",
B
bellard 已提交
215 216
    "DECQ",

B
bellard 已提交
217 218 219
    "SHLB",
    "SHLW",
    "SHLL",
B
bellard 已提交
220 221
    "SHLQ",

B
bellard 已提交
222 223 224
    "SARB",
    "SARW",
    "SARL",
B
bellard 已提交
225
    "SARQ",
B
bellard 已提交
226 227
};

B
bellard 已提交
228 229 230
void cpu_dump_state(CPUState *env, FILE *f, 
                    int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                    int flags)
B
bellard 已提交
231
{
B
bellard 已提交
232
    int eflags, i;
B
bellard 已提交
233
    char cc_op_name[32];
B
bellard 已提交
234
    static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
B
bellard 已提交
235 236

    eflags = env->eflags;
B
bellard 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
#ifdef TARGET_X86_64
    if (env->hflags & HF_CS64_MASK) {
        cpu_fprintf(f, 
                    "RAX=%016llx RBX=%016llx RCX=%016llx RDX=%016llx\n"
                    "RSI=%016llx RDI=%016llx RBP=%016llx RSP=%016llx\n"
                    "R8 =%016llx R9 =%016llx R10=%016llx R11=%016llx\n"
                    "R12=%016llx R13=%016llx R14=%016llx R15=%016llx\n"
                    "RIP=%016llx RFL=%08x [%c%c%c%c%c%c%c]    CPL=%d II=%d A20=%d\n",
                    env->regs[R_EAX], 
                    env->regs[R_EBX], 
                    env->regs[R_ECX], 
                    env->regs[R_EDX], 
                    env->regs[R_ESI], 
                    env->regs[R_EDI], 
                    env->regs[R_EBP], 
                    env->regs[R_ESP], 
                    env->regs[8], 
                    env->regs[9], 
                    env->regs[10], 
                    env->regs[11], 
                    env->regs[12], 
                    env->regs[13], 
                    env->regs[14], 
                    env->regs[15], 
                    env->eip, eflags,
                    eflags & DF_MASK ? 'D' : '-',
                    eflags & CC_O ? 'O' : '-',
                    eflags & CC_S ? 'S' : '-',
                    eflags & CC_Z ? 'Z' : '-',
                    eflags & CC_A ? 'A' : '-',
                    eflags & CC_P ? 'P' : '-',
                    eflags & CC_C ? 'C' : '-',
                    env->hflags & HF_CPL_MASK, 
                    (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
                    (env->a20_mask >> 20) & 1);
    } else 
#endif
    {
        cpu_fprintf(f, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
                    "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
                    "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c]    CPL=%d II=%d A20=%d\n",
                    (uint32_t)env->regs[R_EAX], 
                    (uint32_t)env->regs[R_EBX], 
                    (uint32_t)env->regs[R_ECX], 
                    (uint32_t)env->regs[R_EDX], 
                    (uint32_t)env->regs[R_ESI], 
                    (uint32_t)env->regs[R_EDI], 
                    (uint32_t)env->regs[R_EBP], 
                    (uint32_t)env->regs[R_ESP], 
                    (uint32_t)env->eip, eflags,
                    eflags & DF_MASK ? 'D' : '-',
                    eflags & CC_O ? 'O' : '-',
                    eflags & CC_S ? 'S' : '-',
                    eflags & CC_Z ? 'Z' : '-',
                    eflags & CC_A ? 'A' : '-',
                    eflags & CC_P ? 'P' : '-',
                    eflags & CC_C ? 'C' : '-',
                    env->hflags & HF_CPL_MASK, 
                    (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
                    (env->a20_mask >> 20) & 1);
    }

#ifdef TARGET_X86_64
    if (env->hflags & HF_LMA_MASK) {
        for(i = 0; i < 6; i++) {
            SegmentCache *sc = &env->segs[i];
            cpu_fprintf(f, "%s =%04x %016llx %08x %08x\n",
                        seg_name[i],
                        sc->selector,
                        sc->base,
                        sc->limit,
                        sc->flags);
        }
        cpu_fprintf(f, "LDT=%04x %016llx %08x %08x\n",
                    env->ldt.selector,
                    env->ldt.base,
                    env->ldt.limit,
                    env->ldt.flags);
        cpu_fprintf(f, "TR =%04x %016llx %08x %08x\n",
                    env->tr.selector,
                    env->tr.base,
                    env->tr.limit,
                    env->tr.flags);
        cpu_fprintf(f, "GDT=     %016llx %08x\n",
                    env->gdt.base, env->gdt.limit);
        cpu_fprintf(f, "IDT=     %016llx %08x\n",
                    env->idt.base, env->idt.limit);
        cpu_fprintf(f, "CR0=%08x CR2=%016llx CR3=%016llx CR4=%08x\n",
                    (uint32_t)env->cr[0], 
                    env->cr[2], 
                    env->cr[3], 
                    (uint32_t)env->cr[4]);
    } else
#endif
    {
        for(i = 0; i < 6; i++) {
            SegmentCache *sc = &env->segs[i];
            cpu_fprintf(f, "%s =%04x %08x %08x %08x\n",
                        seg_name[i],
                        sc->selector,
                        (uint32_t)sc->base,
                        sc->limit,
                        sc->flags);
        }
        cpu_fprintf(f, "LDT=%04x %08x %08x %08x\n",
                    env->ldt.selector,
                    (uint32_t)env->ldt.base,
                    env->ldt.limit,
                    env->ldt.flags);
        cpu_fprintf(f, "TR =%04x %08x %08x %08x\n",
                    env->tr.selector,
                    (uint32_t)env->tr.base,
                    env->tr.limit,
                    env->tr.flags);
        cpu_fprintf(f, "GDT=     %08x %08x\n",
                    (uint32_t)env->gdt.base, env->gdt.limit);
        cpu_fprintf(f, "IDT=     %08x %08x\n",
                    (uint32_t)env->idt.base, env->idt.limit);
        cpu_fprintf(f, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
                    (uint32_t)env->cr[0], 
                    (uint32_t)env->cr[2], 
                    (uint32_t)env->cr[3], 
                    (uint32_t)env->cr[4]);
B
bellard 已提交
360
    }
B
bellard 已提交
361 362
    if (flags & X86_DUMP_CCOP) {
        if ((unsigned)env->cc_op < CC_OP_NB)
B
bellard 已提交
363
            snprintf(cc_op_name, sizeof(cc_op_name), "%s", cc_op_str[env->cc_op]);
B
bellard 已提交
364 365
        else
            snprintf(cc_op_name, sizeof(cc_op_name), "[%d]", env->cc_op);
B
bellard 已提交
366 367 368 369 370 371 372 373 374 375 376 377
#ifdef TARGET_X86_64
        if (env->hflags & HF_CS64_MASK) {
            cpu_fprintf(f, "CCS=%016llx CCD=%016llx CCO=%-8s\n",
                        env->cc_src, env->cc_dst, 
                        cc_op_name);
        } else 
#endif
        {
            cpu_fprintf(f, "CCS=%08x CCD=%08x CCO=%-8s\n",
                        (uint32_t)env->cc_src, (uint32_t)env->cc_dst, 
                        cc_op_name);
        }
B
bellard 已提交
378 379
    }
    if (flags & X86_DUMP_FPU) {
B
bellard 已提交
380
        cpu_fprintf(f, "ST0=%f ST1=%f ST2=%f ST3=%f\n", 
B
bellard 已提交
381 382 383 384
                (double)env->fpregs[0].d, 
                (double)env->fpregs[1].d, 
                (double)env->fpregs[2].d, 
                (double)env->fpregs[3].d);
B
bellard 已提交
385
        cpu_fprintf(f, "ST4=%f ST5=%f ST6=%f ST7=%f\n", 
B
bellard 已提交
386 387 388 389
                (double)env->fpregs[4].d, 
                (double)env->fpregs[5].d, 
                (double)env->fpregs[7].d, 
                (double)env->fpregs[8].d);
B
bellard 已提交
390 391 392 393 394 395 396
    }
}

/***********************************************************/
/* x86 mmu */
/* XXX: add PGE support */

B
bellard 已提交
397 398 399
void cpu_x86_set_a20(CPUX86State *env, int a20_state)
{
    a20_state = (a20_state != 0);
400
    if (a20_state != ((env->a20_mask >> 20) & 1)) {
401 402 403
#if defined(DEBUG_MMU)
        printf("A20 update: a20=%d\n", a20_state);
#endif
B
a20 fix  
bellard 已提交
404 405
        /* if the cpu is currently executing code, we must unlink it and
           all the potentially executing TB */
406
        cpu_interrupt(env, CPU_INTERRUPT_EXITTB);
B
a20 fix  
bellard 已提交
407

B
bellard 已提交
408 409
        /* when a20 is changed, all the MMU mappings are invalid, so
           we must flush everything */
410 411
        tlb_flush(env, 1);
        env->a20_mask = 0xffefffff | (a20_state << 20);
B
bellard 已提交
412 413 414
    }
}

415
void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0)
B
bellard 已提交
416
{
417
    int pe_state;
B
bellard 已提交
418

419
#if defined(DEBUG_MMU)
420
    printf("CR0 update: CR0=0x%08x\n", new_cr0);
B
bellard 已提交
421
#endif
422 423 424
    if ((new_cr0 & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK)) !=
        (env->cr[0] & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK))) {
        tlb_flush(env, 1);
B
bellard 已提交
425
    }
B
bellard 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

#ifdef TARGET_X86_64
    if (!(env->cr[0] & CR0_PG_MASK) && (new_cr0 & CR0_PG_MASK) &&
        (env->efer & MSR_EFER_LME)) {
        /* enter in long mode */
        /* XXX: generate an exception */
        if (!(env->cr[4] & CR4_PAE_MASK))
            return;
        env->efer |= MSR_EFER_LMA;
        env->hflags |= HF_LMA_MASK;
    } else if ((env->cr[0] & CR0_PG_MASK) && !(new_cr0 & CR0_PG_MASK) &&
               (env->efer & MSR_EFER_LMA)) {
        /* exit long mode */
        env->efer &= ~MSR_EFER_LMA;
        env->hflags &= ~(HF_LMA_MASK | HF_CS64_MASK);
        env->eip &= 0xffffffff;
    }
#endif
B
bellard 已提交
444
    env->cr[0] = new_cr0 | CR0_ET_MASK;
445
    
446 447 448 449 450
    /* update PE flag in hidden flags */
    pe_state = (env->cr[0] & CR0_PE_MASK);
    env->hflags = (env->hflags & ~HF_PE_MASK) | (pe_state << HF_PE_SHIFT);
    /* ensure that ADDSEG is always set in real mode */
    env->hflags |= ((pe_state ^ 1) << HF_ADDSEG_SHIFT);
451 452 453
    /* update FPU flags */
    env->hflags = (env->hflags & ~(HF_MP_MASK | HF_EM_MASK | HF_TS_MASK)) |
        ((new_cr0 << (HF_MP_SHIFT - 1)) & (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK));
B
bellard 已提交
454 455
}

B
bellard 已提交
456
void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3)
B
bellard 已提交
457
{
458
    env->cr[3] = new_cr3;
B
bellard 已提交
459 460
    if (env->cr[0] & CR0_PG_MASK) {
#if defined(DEBUG_MMU)
B
bellard 已提交
461
        printf("CR3 update: CR3=" TARGET_FMT_lx "\n", new_cr3);
B
bellard 已提交
462
#endif
463
        tlb_flush(env, 0);
B
bellard 已提交
464 465 466
    }
}

467
void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
B
bellard 已提交
468
{
469
#if defined(DEBUG_MMU)
B
bellard 已提交
470
    printf("CR4 update: CR4=%08x\n", (uint32_t)env->cr[4]);
471 472 473 474 475
#endif
    if ((new_cr4 & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK)) !=
        (env->cr[4] & (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK))) {
        tlb_flush(env, 1);
    }
B
bellard 已提交
476 477 478 479 480 481 482 483
    /* SSE handling */
    if (!(env->cpuid_features & CPUID_SSE))
        new_cr4 &= ~CR4_OSFXSR_MASK;
    if (new_cr4 & CR4_OSFXSR_MASK)
        env->hflags |= HF_OSFXSR_MASK;
    else
        env->hflags &= ~HF_OSFXSR_MASK;

484
    env->cr[4] = new_cr4;
B
bellard 已提交
485 486 487 488 489 490 491 492
}

/* XXX: also flush 4MB pages */
void cpu_x86_flush_tlb(CPUX86State *env, uint32_t addr)
{
    tlb_flush_page(env, addr);
}

B
bellard 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
static inline uint8_t *get_phys_mem_ptr(target_phys_addr_t addr)
{
    /* XXX: incorrect */
    return phys_ram_base + addr;
}

/* WARNING: addr must be aligned */
uint32_t ldl_phys_aligned(target_phys_addr_t addr)
{
    uint8_t *ptr;
    uint32_t val;
    ptr = get_phys_mem_ptr(addr);
    if (!ptr)
        val = 0;
    else
        val = ldl_raw(ptr);
    return val;
}

void stl_phys_aligned(target_phys_addr_t addr, uint32_t val)
{
    uint8_t *ptr;
    ptr = get_phys_mem_ptr(addr);
    if (!ptr)
        return;
    stl_raw(ptr, val);
}

B
bellard 已提交
521 522 523 524 525 526
/* return value:
   -1 = cannot handle fault 
   0  = nothing more to do 
   1  = generate PF fault
   2  = soft MMU activation required for this block
*/
B
bellard 已提交
527
int cpu_x86_handle_mmu_fault(CPUX86State *env, target_ulong addr, 
B
bellard 已提交
528
                             int is_write, int is_user, int is_softmmu)
B
bellard 已提交
529
{
B
bellard 已提交
530 531
    uint32_t pdpe_addr, pde_addr, pte_addr;
    uint32_t pde, pte, ptep, pdpe;
B
bellard 已提交
532
    int error_code, is_dirty, prot, page_size, ret;
B
bellard 已提交
533 534
    unsigned long paddr, page_offset;
    target_ulong vaddr, virt_addr;
B
bellard 已提交
535
    
536
#if defined(DEBUG_MMU)
B
bellard 已提交
537
    printf("MMU fault: addr=" TARGET_FMT_lx " w=%d u=%d eip=" TARGET_FMT_lx "\n", 
B
bellard 已提交
538 539
           addr, is_write, is_user, env->eip);
#endif
B
bellard 已提交
540 541
    is_write &= 1;
    
B
bellard 已提交
542 543 544 545 546 547 548 549
    if (env->user_mode_only) {
        /* user mode only emulation */
        error_code = 0;
        goto do_fault;
    }

    if (!(env->cr[0] & CR0_PG_MASK)) {
        pte = addr;
B
bellard 已提交
550
        virt_addr = addr & TARGET_PAGE_MASK;
B
bellard 已提交
551
        prot = PAGE_READ | PAGE_WRITE;
B
bellard 已提交
552 553 554 555
        page_size = 4096;
        goto do_mapping;
    }

B
bellard 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

    if (env->cr[4] & CR4_PAE_MASK) {
        /* XXX: we only use 32 bit physical addresses */
#ifdef TARGET_X86_64
        if (env->hflags & HF_LMA_MASK) {
            uint32_t pml4e_addr, pml4e;
            int32_t sext;

            /* XXX: handle user + rw rights */
            /* XXX: handle NX flag */
            /* test virtual address sign extension */
            sext = (int64_t)addr >> 47;
            if (sext != 0 && sext != -1) {
                error_code = 0;
                goto do_fault;
            }
            
            pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) & 
                env->a20_mask;
            pml4e = ldl_phys_aligned(pml4e_addr);
            if (!(pml4e & PG_PRESENT_MASK)) {
                error_code = 0;
                goto do_fault;
            }
            if (!(pml4e & PG_ACCESSED_MASK)) {
                pml4e |= PG_ACCESSED_MASK;
                stl_phys_aligned(pml4e_addr, pml4e);
            }
            
            pdpe_addr = ((pml4e & ~0xfff) + (((addr >> 30) & 0x1ff) << 3)) & 
                env->a20_mask;
            pdpe = ldl_phys_aligned(pdpe_addr);
            if (!(pdpe & PG_PRESENT_MASK)) {
                error_code = 0;
                goto do_fault;
            }
            if (!(pdpe & PG_ACCESSED_MASK)) {
                pdpe |= PG_ACCESSED_MASK;
                stl_phys_aligned(pdpe_addr, pdpe);
            }
        } else 
#endif
        {
            pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 30) << 3)) & 
                env->a20_mask;
            pdpe = ldl_phys_aligned(pdpe_addr);
            if (!(pdpe & PG_PRESENT_MASK)) {
                error_code = 0;
                goto do_fault;
            }
606
        }
B
bellard 已提交
607 608 609 610 611 612 613

        pde_addr = ((pdpe & ~0xfff) + (((addr >> 21) & 0x1ff) << 3)) &
            env->a20_mask;
        pde = ldl_phys_aligned(pde_addr);
        if (!(pde & PG_PRESENT_MASK)) {
            error_code = 0;
            goto do_fault;
B
bellard 已提交
614
        }
B
bellard 已提交
615 616 617 618 619 620 621 622 623 624 625 626 627
        if (pde & PG_PSE_MASK) {
            /* 2 MB page */
            page_size = 2048 * 1024;
            goto handle_big_page;
        } else {
            /* 4 KB page */
            if (!(pde & PG_ACCESSED_MASK)) {
                pde |= PG_ACCESSED_MASK;
                stl_phys_aligned(pde_addr, pde);
            }
            pte_addr = ((pde & ~0xfff) + (((addr >> 12) & 0x1ff) << 3)) &
                env->a20_mask;
            goto handle_4k_page;
B
bellard 已提交
628
        }
B
bellard 已提交
629
    } else {
B
bellard 已提交
630
        /* page directory entry */
B
bellard 已提交
631 632 633 634
        pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & ~3)) & 
            env->a20_mask;
        pde = ldl_phys_aligned(pde_addr);
        if (!(pde & PG_PRESENT_MASK)) {
B
bellard 已提交
635 636 637
            error_code = 0;
            goto do_fault;
        }
B
bellard 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
        /* if PSE bit is set, then we use a 4MB page */
        if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
            page_size = 4096 * 1024;
        handle_big_page:
            if (is_user) {
                if (!(pde & PG_USER_MASK))
                    goto do_fault_protect;
                if (is_write && !(pde & PG_RW_MASK))
                    goto do_fault_protect;
            } else {
                if ((env->cr[0] & CR0_WP_MASK) && 
                    is_write && !(pde & PG_RW_MASK)) 
                    goto do_fault_protect;
            }
            is_dirty = is_write && !(pde & PG_DIRTY_MASK);
            if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
                pde |= PG_ACCESSED_MASK;
                if (is_dirty)
                    pde |= PG_DIRTY_MASK;
                stl_phys_aligned(pde_addr, pde);
            }
        
            pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
            ptep = pte;
            virt_addr = addr & ~(page_size - 1);
B
bellard 已提交
663
        } else {
B
bellard 已提交
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
            if (!(pde & PG_ACCESSED_MASK)) {
                pde |= PG_ACCESSED_MASK;
                stl_phys_aligned(pde_addr, pde);
            }

            /* page directory entry */
            pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & 
                env->a20_mask;
        handle_4k_page:
            pte = ldl_phys_aligned(pte_addr);
            if (!(pte & PG_PRESENT_MASK)) {
                error_code = 0;
                goto do_fault;
            }
            /* combine pde and pte user and rw protections */
            ptep = pte & pde;
            if (is_user) {
                if (!(ptep & PG_USER_MASK))
                    goto do_fault_protect;
                if (is_write && !(ptep & PG_RW_MASK))
                    goto do_fault_protect;
            } else {
                if ((env->cr[0] & CR0_WP_MASK) &&
                    is_write && !(ptep & PG_RW_MASK)) 
                    goto do_fault_protect;
            }
            is_dirty = is_write && !(pte & PG_DIRTY_MASK);
            if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
                pte |= PG_ACCESSED_MASK;
                if (is_dirty)
                    pte |= PG_DIRTY_MASK;
                stl_phys_aligned(pte_addr, pte);
            }
            page_size = 4096;
            virt_addr = addr & ~0xfff;
B
bellard 已提交
699
        }
700

B
bellard 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713
        /* the page can be put in the TLB */
        prot = PAGE_READ;
        if (pte & PG_DIRTY_MASK) {
            /* only set write access if already dirty... otherwise wait
               for dirty access */
            if (is_user) {
                if (ptep & PG_RW_MASK)
                    prot |= PAGE_WRITE;
            } else {
                if (!(env->cr[0] & CR0_WP_MASK) ||
                    (ptep & PG_RW_MASK))
                    prot |= PAGE_WRITE;
            }
714
        }
B
bellard 已提交
715 716
    }
 do_mapping:
717
    pte = pte & env->a20_mask;
B
bellard 已提交
718

719 720 721 722 723 724 725
    /* Even if 4MB pages, we map only one 4KB page in the cache to
       avoid filling it too fast */
    page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
    paddr = (pte & TARGET_PAGE_MASK) + page_offset;
    vaddr = virt_addr + page_offset;
    
    ret = tlb_set_page(env, vaddr, paddr, prot, is_user, is_softmmu);
B
bellard 已提交
726 727 728 729 730 731 732 733 734 735
    return ret;
 do_fault_protect:
    error_code = PG_ERROR_P_MASK;
 do_fault:
    env->cr[2] = addr;
    env->error_code = (is_write << PG_ERROR_W_BIT) | error_code;
    if (is_user)
        env->error_code |= PG_ERROR_U_MASK;
    return 1;
}
736 737 738 739 740 741 742 743 744

#if defined(CONFIG_USER_ONLY) 
target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
{
    return addr;
}
#else
target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
{
745
    uint32_t pde_addr, pte_addr;
746 747
    uint32_t pde, pte, paddr, page_offset, page_size;

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
    if (env->cr[4] & CR4_PAE_MASK) {
        uint32_t pdpe_addr, pde_addr, pte_addr;
        uint32_t pdpe;

        /* XXX: we only use 32 bit physical addresses */
#ifdef TARGET_X86_64
        if (env->hflags & HF_LMA_MASK) {
            uint32_t pml4e_addr, pml4e;
            int32_t sext;

            /* test virtual address sign extension */
            sext = (int64_t)addr >> 47;
            if (sext != 0 && sext != -1)
                return -1;
            
            pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) & 
                env->a20_mask;
            pml4e = ldl_phys_aligned(pml4e_addr);
            if (!(pml4e & PG_PRESENT_MASK))
                return -1;
            
            pdpe_addr = ((pml4e & ~0xfff) + (((addr >> 30) & 0x1ff) << 3)) & 
                env->a20_mask;
            pdpe = ldl_phys_aligned(pdpe_addr);
            if (!(pdpe & PG_PRESENT_MASK))
                return -1;
        } else 
#endif
        {
            pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 30) << 3)) & 
                env->a20_mask;
            pdpe = ldl_phys_aligned(pdpe_addr);
            if (!(pdpe & PG_PRESENT_MASK))
                return -1;
        }

        pde_addr = ((pdpe & ~0xfff) + (((addr >> 21) & 0x1ff) << 3)) &
            env->a20_mask;
        pde = ldl_phys_aligned(pde_addr);
        if (!(pde & PG_PRESENT_MASK)) {
788
            return -1;
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
        }
        if (pde & PG_PSE_MASK) {
            /* 2 MB page */
            page_size = 2048 * 1024;
            pte = pde & ~( (page_size - 1) & ~0xfff); /* align to page_size */
        } else {
            /* 4 KB page */
            pte_addr = ((pde & ~0xfff) + (((addr >> 12) & 0x1ff) << 3)) &
                env->a20_mask;
            page_size = 4096;
            pte = ldl_phys_aligned(pte_addr);
        }
    } else {
        if (!(env->cr[0] & CR0_PG_MASK)) {
            pte = addr;
            page_size = 4096;
805 806
        } else {
            /* page directory entry */
807 808 809
            pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & ~3)) & env->a20_mask;
            pde = ldl_phys_aligned(pde_addr);
            if (!(pde & PG_PRESENT_MASK)) 
810
                return -1;
811 812 813 814 815 816 817 818 819 820 821
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
                pte = pde & ~0x003ff000; /* align to 4MB */
                page_size = 4096 * 1024;
            } else {
                /* page directory entry */
                pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & env->a20_mask;
                pte = ldl_phys_aligned(pte_addr);
                if (!(pte & PG_PRESENT_MASK))
                    return -1;
                page_size = 4096;
            }
822
        }
823
        pte = pte & env->a20_mask;
824
    }
825

826 827 828 829 830
    page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
    paddr = (pte & TARGET_PAGE_MASK) + page_offset;
    return paddr;
}
#endif
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866

#if defined(USE_CODE_COPY)
struct fpstate {
    uint16_t fpuc;
    uint16_t dummy1;
    uint16_t fpus;
    uint16_t dummy2;
    uint16_t fptag;
    uint16_t dummy3;

    uint32_t fpip;
    uint32_t fpcs;
    uint32_t fpoo;
    uint32_t fpos;
    uint8_t fpregs1[8 * 10];
};

void restore_native_fp_state(CPUState *env)
{
    int fptag, i, j;
    struct fpstate fp1, *fp = &fp1;
    
    fp->fpuc = env->fpuc;
    fp->fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
    fptag = 0;
    for (i=7; i>=0; i--) {
	fptag <<= 2;
	if (env->fptags[i]) {
            fptag |= 3;
        } else {
            /* the FPU automatically computes it */
        }
    }
    fp->fptag = fptag;
    j = env->fpstt;
    for(i = 0;i < 8; i++) {
B
bellard 已提交
867
        memcpy(&fp->fpregs1[i * 10], &env->fpregs[j].d, 10);
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
        j = (j + 1) & 7;
    }
    asm volatile ("frstor %0" : "=m" (*fp));
    env->native_fp_regs = 1;
}
 
void save_native_fp_state(CPUState *env)
{
    int fptag, i, j;
    uint16_t fpuc;
    struct fpstate fp1, *fp = &fp1;

    asm volatile ("fsave %0" : : "m" (*fp));
    env->fpuc = fp->fpuc;
    env->fpstt = (fp->fpus >> 11) & 7;
    env->fpus = fp->fpus & ~0x3800;
    fptag = fp->fptag;
    for(i = 0;i < 8; i++) {
        env->fptags[i] = ((fptag & 3) == 3);
        fptag >>= 2;
    }
    j = env->fpstt;
    for(i = 0;i < 8; i++) {
B
bellard 已提交
891
        memcpy(&env->fpregs[j].d, &fp->fpregs1[i * 10], 10);
892 893 894 895 896 897 898 899 900
        j = (j + 1) & 7;
    }
    /* we must restore the default rounding state */
    /* XXX: we do not restore the exception state */
    fpuc = 0x037f | (env->fpuc & (3 << 10));
    asm volatile("fldcw %0" : : "m" (fpuc));
    env->native_fp_regs = 0;
}
#endif