Statistics
| Branch: | Revision:

root / linux-user / main.c @ 6dbad63e

History | View | Annotate | Download (6.1 kB)

1
/*
2
 *  gemu main
3
 * 
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program 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
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 */
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include <stdarg.h>
23
#include <errno.h>
24
#include <unistd.h>
25

    
26
#include "gemu.h"
27

    
28
#include "cpu-i386.h"
29

    
30
#define DEBUG_LOGFILE "/tmp/gemu.log"
31

    
32
FILE *logfile = NULL;
33
int loglevel;
34

    
35
unsigned long x86_stack_size;
36
unsigned long stktop;
37

    
38
void gemu_log(const char *fmt, ...)
39
{
40
    va_list ap;
41

    
42
    va_start(ap, fmt);
43
    vfprintf(stderr, fmt, ap);
44
    va_end(ap);
45
}
46

    
47
/***********************************************************/
48
/* CPUX86 core interface */
49

    
50
void cpu_x86_outb(int addr, int val)
51
{
52
    fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
53
}
54

    
55
void cpu_x86_outw(int addr, int val)
56
{
57
    fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
58
}
59

    
60
void cpu_x86_outl(int addr, int val)
61
{
62
    fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
63
}
64

    
65
int cpu_x86_inb(int addr)
66
{
67
    fprintf(stderr, "inb: port=0x%04x\n", addr);
68
    return 0;
69
}
70

    
71
int cpu_x86_inw(int addr)
72
{
73
    fprintf(stderr, "inw: port=0x%04x\n", addr);
74
    return 0;
75
}
76

    
77
int cpu_x86_inl(int addr)
78
{
79
    fprintf(stderr, "inl: port=0x%04x\n", addr);
80
    return 0;
81
}
82

    
83
/* default linux values for the selectors */
84
#define __USER_CS        (0x23)
85
#define __USER_DS        (0x2B)
86

    
87
void write_dt(void *ptr, unsigned long addr, unsigned long limit, 
88
              int seg32_bit)
89
{
90
    unsigned int e1, e2, limit_in_pages;
91
    limit_in_pages = 0;
92
    if (limit > 0xffff) {
93
        limit = limit >> 12;
94
        limit_in_pages = 1;
95
    }
96
    e1 = (addr << 16) | (limit & 0xffff);
97
    e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
98
    e2 |= limit_in_pages << 23; /* byte granularity */
99
    e2 |= seg32_bit << 22; /* 32 bit segment */
100
    stl((uint8_t *)ptr, e1);
101
    stl((uint8_t *)ptr + 4, e2);
102
}
103

    
104
uint64_t gdt_table[6];
105

    
106
void usage(void)
107
{
108
    printf("gemu version " GEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
109
           "usage: gemu [-d] program [arguments...]\n"
110
           "Linux x86 emulator\n"
111
           );
112
    exit(1);
113
}
114

    
115

    
116

    
117
int main(int argc, char **argv)
118
{
119
    const char *filename;
120
    struct target_pt_regs regs1, *regs = &regs1;
121
    struct image_info info1, *info = &info1;
122
    CPUX86State *env;
123
    int optind;
124

    
125
    if (argc <= 1)
126
        usage();
127
    loglevel = 0;
128
    optind = 1;
129
    if (argv[optind] && !strcmp(argv[optind], "-d")) {
130
        loglevel = 1;
131
        optind++;
132
    }
133
    filename = argv[optind];
134

    
135
    /* init debug */
136
    if (loglevel) {
137
        logfile = fopen(DEBUG_LOGFILE, "w");
138
        if (!logfile) {
139
            perror(DEBUG_LOGFILE);
140
            exit(1);
141
        }
142
        setvbuf(logfile, NULL, _IOLBF, 0);
143
    }
144

    
145
    /* Zero out regs */
146
    memset(regs, 0, sizeof(struct target_pt_regs));
147

    
148
    /* Zero out image_info */
149
    memset(info, 0, sizeof(struct image_info));
150

    
151
    if(elf_exec(filename, argv+optind, environ, regs, info) != 0) {
152
        printf("Error loading %s\n", filename);
153
        exit(1);
154
    }
155
    
156
    if (loglevel) {
157
        fprintf(logfile, "start_brk   0x%08lx\n" , info->start_brk);
158
        fprintf(logfile, "end_code    0x%08lx\n" , info->end_code);
159
        fprintf(logfile, "start_code  0x%08lx\n" , info->start_code);
160
        fprintf(logfile, "end_data    0x%08lx\n" , info->end_data);
161
        fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
162
        fprintf(logfile, "brk         0x%08lx\n" , info->brk);
163
        fprintf(logfile, "esp         0x%08lx\n" , regs->esp);
164
        fprintf(logfile, "eip         0x%08lx\n" , regs->eip);
165
    }
166

    
167
    target_set_brk((char *)info->brk);
168
    syscall_init();
169

    
170
    env = cpu_x86_init();
171

    
172
    /* linux register setup */
173
    env->regs[R_EAX] = regs->eax;
174
    env->regs[R_EBX] = regs->ebx;
175
    env->regs[R_ECX] = regs->ecx;
176
    env->regs[R_EDX] = regs->edx;
177
    env->regs[R_ESI] = regs->esi;
178
    env->regs[R_EDI] = regs->edi;
179
    env->regs[R_EBP] = regs->ebp;
180
    env->regs[R_ESP] = regs->esp;
181
    env->pc = regs->eip;
182

    
183
    /* linux segment setup */
184
    env->gdt.base = (void *)gdt_table;
185
    env->gdt.limit = sizeof(gdt_table) - 1;
186
    write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1);
187
    write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1);
188
    cpu_x86_load_seg(env, R_CS, __USER_CS);
189
    cpu_x86_load_seg(env, R_DS, __USER_DS);
190
    cpu_x86_load_seg(env, R_ES, __USER_DS);
191
    cpu_x86_load_seg(env, R_SS, __USER_DS);
192
    cpu_x86_load_seg(env, R_FS, __USER_DS);
193
    cpu_x86_load_seg(env, R_GS, __USER_DS);
194

    
195
    for(;;) {
196
        int err;
197
        uint8_t *pc;
198
        
199
        err = cpu_x86_exec(env);
200
        switch(err) {
201
        case EXCP0D_GPF:
202
            pc = (uint8_t *)env->pc;
203
            if (pc[0] == 0xcd && pc[1] == 0x80) {
204
                /* syscall */
205
                env->pc += 2;
206
                env->regs[R_EAX] = do_syscall(env, 
207
                                              env->regs[R_EAX], 
208
                                              env->regs[R_EBX],
209
                                              env->regs[R_ECX],
210
                                              env->regs[R_EDX],
211
                                              env->regs[R_ESI],
212
                                              env->regs[R_EDI],
213
                                              env->regs[R_EBP]);
214
            } else {
215
                goto trap_error;
216
            }
217
            break;
218
        default:
219
        trap_error:
220
            fprintf(stderr, "0x%08lx: Unknown exception %d, aborting\n", 
221
                    (long)env->pc, err);
222
            abort();
223
        }
224
    }
225
    return 0;
226
}