Statistics
| Branch: | Revision:

root / target-i386 / helper2.c @ b7f0f463

History | View | Annotate | Download (12.6 kB)

1
/*
2
 *  i386 helpers (without register variable usage)
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 <stdarg.h>
21
#include <stdlib.h>
22
#include <stdio.h>
23
#include <string.h>
24
#include <inttypes.h>
25
#include <signal.h>
26
#include <assert.h>
27
#include <sys/mman.h>
28

    
29
#include "cpu.h"
30
#include "exec-all.h"
31

    
32
//#define DEBUG_MMU
33

    
34
CPUX86State *cpu_x86_init(void)
35
{
36
    CPUX86State *env;
37
    int i;
38
    static int inited;
39

    
40
    cpu_exec_init();
41

    
42
    env = malloc(sizeof(CPUX86State));
43
    if (!env)
44
        return NULL;
45
    memset(env, 0, sizeof(CPUX86State));
46
    /* basic FPU init */
47
    for(i = 0;i < 8; i++)
48
        env->fptags[i] = 1;
49
    env->fpuc = 0x37f;
50
    /* flags setup : we activate the IRQs by default as in user mode */
51
    env->eflags = 0x2 | IF_MASK;
52

    
53
    tlb_flush(env);
54
#ifdef CONFIG_SOFTMMU
55
    env->hflags |= HF_SOFTMMU_MASK;
56
#endif
57
    /* init various static tables */
58
    if (!inited) {
59
        inited = 1;
60
        optimize_flags_init();
61
    }
62
    return env;
63
}
64

    
65
void cpu_x86_close(CPUX86State *env)
66
{
67
    free(env);
68
}
69

    
70
/***********************************************************/
71
/* x86 debug */
72

    
73
static const char *cc_op_str[] = {
74
    "DYNAMIC",
75
    "EFLAGS",
76
    "MULB",
77
    "MULW",
78
    "MULL",
79
    "ADDB",
80
    "ADDW",
81
    "ADDL",
82
    "ADCB",
83
    "ADCW",
84
    "ADCL",
85
    "SUBB",
86
    "SUBW",
87
    "SUBL",
88
    "SBBB",
89
    "SBBW",
90
    "SBBL",
91
    "LOGICB",
92
    "LOGICW",
93
    "LOGICL",
94
    "INCB",
95
    "INCW",
96
    "INCL",
97
    "DECB",
98
    "DECW",
99
    "DECL",
100
    "SHLB",
101
    "SHLW",
102
    "SHLL",
103
    "SARB",
104
    "SARW",
105
    "SARL",
106
};
107

    
108
void cpu_x86_dump_state(CPUX86State *env, FILE *f, int flags)
109
{
110
    int eflags, i;
111
    char cc_op_name[32];
112
    static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
113

    
114
    eflags = env->eflags;
115
    fprintf(f, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
116
            "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
117
            "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c]    CPL=%d II=%d\n",
118
            env->regs[R_EAX], env->regs[R_EBX], env->regs[R_ECX], env->regs[R_EDX], 
119
            env->regs[R_ESI], env->regs[R_EDI], env->regs[R_EBP], env->regs[R_ESP], 
120
            env->eip, eflags,
121
            eflags & DF_MASK ? 'D' : '-',
122
            eflags & CC_O ? 'O' : '-',
123
            eflags & CC_S ? 'S' : '-',
124
            eflags & CC_Z ? 'Z' : '-',
125
            eflags & CC_A ? 'A' : '-',
126
            eflags & CC_P ? 'P' : '-',
127
            eflags & CC_C ? 'C' : '-',
128
            env->hflags & HF_CPL_MASK, 
129
            (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1);
130
    for(i = 0; i < 6; i++) {
131
        SegmentCache *sc = &env->segs[i];
132
        fprintf(f, "%s =%04x %08x %08x %08x\n",
133
                seg_name[i],
134
                sc->selector,
135
                (int)sc->base,
136
                sc->limit,
137
                sc->flags);
138
    }
139
    fprintf(f, "LDT=%04x %08x %08x %08x\n",
140
            env->ldt.selector,
141
            (int)env->ldt.base,
142
            env->ldt.limit,
143
            env->ldt.flags);
144
    fprintf(f, "TR =%04x %08x %08x %08x\n",
145
            env->tr.selector,
146
            (int)env->tr.base,
147
            env->tr.limit,
148
            env->tr.flags);
149
    fprintf(f, "GDT=     %08x %08x\n",
150
            (int)env->gdt.base, env->gdt.limit);
151
    fprintf(f, "IDT=     %08x %08x\n",
152
            (int)env->idt.base, env->idt.limit);
153
    fprintf(f, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
154
            env->cr[0], env->cr[2], env->cr[3], env->cr[4]);
155
    
156
    if (flags & X86_DUMP_CCOP) {
157
        if ((unsigned)env->cc_op < CC_OP_NB)
158
            strcpy(cc_op_name, cc_op_str[env->cc_op]);
159
        else
160
            snprintf(cc_op_name, sizeof(cc_op_name), "[%d]", env->cc_op);
161
        fprintf(f, "CCS=%08x CCD=%08x CCO=%-8s\n",
162
                env->cc_src, env->cc_dst, cc_op_name);
163
    }
164
    if (flags & X86_DUMP_FPU) {
165
        fprintf(f, "ST0=%f ST1=%f ST2=%f ST3=%f\n", 
166
                (double)env->fpregs[0], 
167
                (double)env->fpregs[1], 
168
                (double)env->fpregs[2], 
169
                (double)env->fpregs[3]);
170
        fprintf(f, "ST4=%f ST5=%f ST6=%f ST7=%f\n", 
171
                (double)env->fpregs[4], 
172
                (double)env->fpregs[5], 
173
                (double)env->fpregs[7], 
174
                (double)env->fpregs[8]);
175
    }
176
}
177

    
178
/***********************************************************/
179
/* x86 mmu */
180
/* XXX: add PGE support */
181

    
182
/* called when cr3 or PG bit are modified */
183
static int last_pg_state = -1;
184
static int last_pe_state = 0;
185
static uint32_t a20_mask;
186
int a20_enabled;
187

    
188
int phys_ram_size;
189
int phys_ram_fd;
190
uint8_t *phys_ram_base;
191

    
192
void cpu_x86_set_a20(CPUX86State *env, int a20_state)
193
{
194
    a20_state = (a20_state != 0);
195
    if (a20_state != a20_enabled) {
196
#if defined(DEBUG_MMU)
197
        printf("A20 update: a20=%d\n", a20_state);
198
#endif
199
        /* if the cpu is currently executing code, we must unlink it and
200
           all the potentially executing TB */
201
        cpu_interrupt(env, 0);
202

    
203
        /* when a20 is changed, all the MMU mappings are invalid, so
204
           we must flush everything */
205
        tlb_flush(env);
206
        a20_enabled = a20_state;
207
        if (a20_enabled)
208
            a20_mask = 0xffffffff;
209
        else
210
            a20_mask = 0xffefffff;
211
    }
212
}
213

    
214
void cpu_x86_update_cr0(CPUX86State *env)
215
{
216
    int pg_state, pe_state;
217

    
218
#if defined(DEBUG_MMU)
219
    printf("CR0 update: CR0=0x%08x\n", env->cr[0]);
220
#endif
221
    pg_state = env->cr[0] & CR0_PG_MASK;
222
    if (pg_state != last_pg_state) {
223
        tlb_flush(env);
224
        last_pg_state = pg_state;
225
    }
226
    pe_state = env->cr[0] & CR0_PE_MASK;
227
    if (last_pe_state != pe_state) {
228
        tb_flush(env);
229
        last_pe_state = pe_state;
230
    }
231
}
232

    
233
void cpu_x86_update_cr3(CPUX86State *env)
234
{
235
    if (env->cr[0] & CR0_PG_MASK) {
236
#if defined(DEBUG_MMU)
237
        printf("CR3 update: CR3=%08x\n", env->cr[3]);
238
#endif
239
        tlb_flush(env);
240
    }
241
}
242

    
243
void cpu_x86_init_mmu(CPUX86State *env)
244
{
245
    a20_enabled = 1;
246
    a20_mask = 0xffffffff;
247

    
248
    last_pg_state = -1;
249
    cpu_x86_update_cr0(env);
250
}
251

    
252
/* XXX: also flush 4MB pages */
253
void cpu_x86_flush_tlb(CPUX86State *env, uint32_t addr)
254
{
255
    tlb_flush_page(env, addr);
256
}
257

    
258
/* return value:
259
   -1 = cannot handle fault 
260
   0  = nothing more to do 
261
   1  = generate PF fault
262
   2  = soft MMU activation required for this block
263
*/
264
int cpu_x86_handle_mmu_fault(CPUX86State *env, uint32_t addr, 
265
                             int is_write, int is_user, int is_softmmu)
266
{
267
    uint8_t *pde_ptr, *pte_ptr;
268
    uint32_t pde, pte, virt_addr;
269
    int error_code, is_dirty, prot, page_size, ret;
270
    unsigned long pd;
271
    
272
#ifdef DEBUG_MMU
273
    printf("MMU fault: addr=0x%08x w=%d u=%d eip=%08x\n", 
274
           addr, is_write, is_user, env->eip);
275
#endif
276

    
277
    if (env->user_mode_only) {
278
        /* user mode only emulation */
279
        error_code = 0;
280
        goto do_fault;
281
    }
282

    
283
    if (!(env->cr[0] & CR0_PG_MASK)) {
284
        pte = addr;
285
        virt_addr = addr & TARGET_PAGE_MASK;
286
        prot = PROT_READ | PROT_WRITE;
287
        page_size = 4096;
288
        goto do_mapping;
289
    }
290

    
291
    /* page directory entry */
292
    pde_ptr = phys_ram_base + 
293
        (((env->cr[3] & ~0xfff) + ((addr >> 20) & ~3)) & a20_mask);
294
    pde = ldl_raw(pde_ptr);
295
    if (!(pde & PG_PRESENT_MASK)) {
296
        error_code = 0;
297
        goto do_fault;
298
    }
299
    if (is_user) {
300
        if (!(pde & PG_USER_MASK))
301
            goto do_fault_protect;
302
        if (is_write && !(pde & PG_RW_MASK))
303
            goto do_fault_protect;
304
    } else {
305
        if ((env->cr[0] & CR0_WP_MASK) && (pde & PG_USER_MASK) &&
306
            is_write && !(pde & PG_RW_MASK)) 
307
            goto do_fault_protect;
308
    }
309
    /* if PSE bit is set, then we use a 4MB page */
310
    if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
311
        is_dirty = is_write && !(pde & PG_DIRTY_MASK);
312
        if (!(pde & PG_ACCESSED_MASK)) {
313
            pde |= PG_ACCESSED_MASK;
314
            if (is_dirty)
315
                pde |= PG_DIRTY_MASK;
316
            stl_raw(pde_ptr, pde);
317
        }
318
        
319
        pte = pde & ~0x003ff000; /* align to 4MB */
320
        page_size = 4096 * 1024;
321
        virt_addr = addr & ~0x003fffff;
322
    } else {
323
        if (!(pde & PG_ACCESSED_MASK)) {
324
            pde |= PG_ACCESSED_MASK;
325
            stl_raw(pde_ptr, pde);
326
        }
327

    
328
        /* page directory entry */
329
        pte_ptr = phys_ram_base + 
330
            (((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & a20_mask);
331
        pte = ldl_raw(pte_ptr);
332
        if (!(pte & PG_PRESENT_MASK)) {
333
            error_code = 0;
334
            goto do_fault;
335
        }
336
        if (is_user) {
337
            if (!(pte & PG_USER_MASK))
338
                goto do_fault_protect;
339
            if (is_write && !(pte & PG_RW_MASK))
340
                goto do_fault_protect;
341
        } else {
342
            if ((env->cr[0] & CR0_WP_MASK) && (pte & PG_USER_MASK) &&
343
                is_write && !(pte & PG_RW_MASK)) 
344
                goto do_fault_protect;
345
        }
346
        is_dirty = is_write && !(pte & PG_DIRTY_MASK);
347
        if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
348
            pte |= PG_ACCESSED_MASK;
349
            if (is_dirty)
350
                pte |= PG_DIRTY_MASK;
351
            stl_raw(pte_ptr, pte);
352
        }
353
        page_size = 4096;
354
        virt_addr = addr & ~0xfff;
355
    }
356
    /* the page can be put in the TLB */
357
    prot = PROT_READ;
358
    if (is_user) {
359
        if (pte & PG_RW_MASK)
360
            prot |= PROT_WRITE;
361
    } else {
362
        if (!(env->cr[0] & CR0_WP_MASK) || !(pte & PG_USER_MASK) ||
363
            (pte & PG_RW_MASK))
364
            prot |= PROT_WRITE;
365
    }
366
    
367
 do_mapping:
368
    pte = pte & a20_mask;
369
#if !defined(CONFIG_SOFTMMU)
370
    if (is_softmmu) 
371
#endif
372
    {
373
        unsigned long paddr, vaddr, address, addend, page_offset;
374
        int index;
375

    
376
        /* software MMU case. Even if 4MB pages, we map only one 4KB
377
           page in the cache to avoid filling it too fast */
378
        page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
379
        paddr = (pte & TARGET_PAGE_MASK) + page_offset;
380
        vaddr = virt_addr + page_offset;
381
        index = (addr >> 12) & (CPU_TLB_SIZE - 1);
382
        pd = physpage_find(paddr);
383
        if (pd & 0xfff) {
384
            /* IO memory case */
385
            address = vaddr | pd;
386
            addend = paddr;
387
        } else {
388
            /* standard memory */
389
            address = vaddr;
390
            addend = (unsigned long)phys_ram_base + pd;
391
        }
392
        addend -= vaddr;
393
        env->tlb_read[is_user][index].address = address;
394
        env->tlb_read[is_user][index].addend = addend;
395
        if (prot & PROT_WRITE) {
396
            env->tlb_write[is_user][index].address = address;
397
            env->tlb_write[is_user][index].addend = addend;
398
        } else {
399
            env->tlb_write[is_user][index].address = -1;
400
            env->tlb_write[is_user][index].addend = -1;
401
        }
402
        page_set_flags(vaddr, vaddr + TARGET_PAGE_SIZE, 
403
                       PAGE_VALID | PAGE_EXEC | prot);
404
        ret = 0;
405
    }
406
#if !defined(CONFIG_SOFTMMU)
407
    else {
408
        ret = 0;
409
        /* XXX: incorrect for 4MB pages */
410
        pd = physpage_find(pte & ~0xfff);
411
        if ((pd & 0xfff) != 0) {
412
            /* IO access: no mapping is done as it will be handled by the
413
               soft MMU */
414
            if (!(env->hflags & HF_SOFTMMU_MASK))
415
                ret = 2;
416
        } else {
417
            void *map_addr;
418
            map_addr = mmap((void *)virt_addr, page_size, prot, 
419
                            MAP_SHARED | MAP_FIXED, phys_ram_fd, pd);
420
            if (map_addr == MAP_FAILED) {
421
                fprintf(stderr, 
422
                        "mmap failed when mapped physical address 0x%08x to virtual address 0x%08x\n",
423
                        pte & ~0xfff, virt_addr);
424
                exit(1);
425
            }
426
#ifdef DEBUG_MMU
427
            printf("mmaping 0x%08x to virt 0x%08x pse=%d\n", 
428
                   pte & ~0xfff, virt_addr, (page_size != 4096));
429
#endif
430
            page_set_flags(virt_addr, virt_addr + page_size, 
431
                           PAGE_VALID | PAGE_EXEC | prot);
432
        }
433
    }
434
#endif
435
    return ret;
436
 do_fault_protect:
437
    error_code = PG_ERROR_P_MASK;
438
 do_fault:
439
    env->cr[2] = addr;
440
    env->error_code = (is_write << PG_ERROR_W_BIT) | error_code;
441
    if (is_user)
442
        env->error_code |= PG_ERROR_U_MASK;
443
    return 1;
444
}