Statistics
| Branch: | Revision:

root / linux-user / main.c @ 5a91de8c

History | View | Annotate | Download (11.6 kB)

1
/*
2
 *  qemu 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 <string.h>
24
#include <errno.h>
25
#include <unistd.h>
26

    
27
#include "qemu.h"
28

    
29
#include "cpu-i386.h"
30

    
31
#define DEBUG_LOGFILE "/tmp/qemu.log"
32

    
33
FILE *logfile = NULL;
34
int loglevel;
35
static const char *interp_prefix = CONFIG_QEMU_PREFIX;
36

    
37
#ifdef __i386__
38
/* Force usage of an ELF interpreter even if it is an ELF shared
39
   object ! */
40
const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
41

    
42
/* for recent libc, we add these dummies symbol which are not declared
43
   when generating a linked object (bug in ld ?) */
44
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
45
long __init_array_start[0];
46
long __init_array_end[0];
47
long __fini_array_start[0];
48
long __fini_array_end[0];
49
#endif
50

    
51
#endif
52

    
53
/* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
54
   we allocate a bigger stack. Need a better solution, for example
55
   by remapping the process stack directly at the right place */
56
unsigned long x86_stack_size = 512 * 1024;
57

    
58
void gemu_log(const char *fmt, ...)
59
{
60
    va_list ap;
61

    
62
    va_start(ap, fmt);
63
    vfprintf(stderr, fmt, ap);
64
    va_end(ap);
65
}
66

    
67
/***********************************************************/
68
/* CPUX86 core interface */
69

    
70
void cpu_x86_outb(CPUX86State *env, int addr, int val)
71
{
72
    fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
73
}
74

    
75
void cpu_x86_outw(CPUX86State *env, int addr, int val)
76
{
77
    fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
78
}
79

    
80
void cpu_x86_outl(CPUX86State *env, int addr, int val)
81
{
82
    fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
83
}
84

    
85
int cpu_x86_inb(CPUX86State *env, int addr)
86
{
87
    fprintf(stderr, "inb: port=0x%04x\n", addr);
88
    return 0;
89
}
90

    
91
int cpu_x86_inw(CPUX86State *env, int addr)
92
{
93
    fprintf(stderr, "inw: port=0x%04x\n", addr);
94
    return 0;
95
}
96

    
97
int cpu_x86_inl(CPUX86State *env, int addr)
98
{
99
    fprintf(stderr, "inl: port=0x%04x\n", addr);
100
    return 0;
101
}
102

    
103
void write_dt(void *ptr, unsigned long addr, unsigned long limit, 
104
              int seg32_bit)
105
{
106
    unsigned int e1, e2, limit_in_pages;
107
    limit_in_pages = 0;
108
    if (limit > 0xffff) {
109
        limit = limit >> 12;
110
        limit_in_pages = 1;
111
    }
112
    e1 = (addr << 16) | (limit & 0xffff);
113
    e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
114
    e2 |= limit_in_pages << 23; /* byte granularity */
115
    e2 |= seg32_bit << 22; /* 32 bit segment */
116
    stl((uint8_t *)ptr, e1);
117
    stl((uint8_t *)ptr + 4, e2);
118
}
119

    
120
uint64_t gdt_table[6];
121

    
122
void cpu_loop(CPUX86State *env)
123
{
124
    int trapnr;
125
    uint8_t *pc;
126
    target_siginfo_t info;
127

    
128
    for(;;) {
129
        trapnr = cpu_x86_exec(env);
130
        switch(trapnr) {
131
        case EXCP0D_GPF:
132
            if (env->eflags & VM_MASK) {
133
                handle_vm86_fault(env);
134
            } else {
135
                pc = env->seg_cache[R_CS].base + env->eip;
136
                if (pc[0] == 0xcd && pc[1] == 0x80) {
137
                    /* syscall */
138
                    env->eip += 2;
139
                    env->regs[R_EAX] = do_syscall(env, 
140
                                                  env->regs[R_EAX], 
141
                                                  env->regs[R_EBX],
142
                                                  env->regs[R_ECX],
143
                                                  env->regs[R_EDX],
144
                                                  env->regs[R_ESI],
145
                                                  env->regs[R_EDI],
146
                                                  env->regs[R_EBP]);
147
                } else {
148
                    /* XXX: more precise info */
149
                    info.si_signo = SIGSEGV;
150
                    info.si_errno = 0;
151
                    info.si_code = TARGET_SI_KERNEL;
152
                    info._sifields._sigfault._addr = 0;
153
                    queue_signal(info.si_signo, &info);
154
                }
155
            }
156
            break;
157
        case EXCP0E_PAGE:
158
            info.si_signo = SIGSEGV;
159
            info.si_errno = 0;
160
            if (!(env->error_code & 1))
161
                info.si_code = TARGET_SEGV_MAPERR;
162
            else
163
                info.si_code = TARGET_SEGV_ACCERR;
164
            info._sifields._sigfault._addr = env->cr2;
165
            queue_signal(info.si_signo, &info);
166
            break;
167
        case EXCP00_DIVZ:
168
            if (env->eflags & VM_MASK) {
169
                handle_vm86_trap(env, trapnr);
170
            } else {
171
                /* division by zero */
172
                info.si_signo = SIGFPE;
173
                info.si_errno = 0;
174
                info.si_code = TARGET_FPE_INTDIV;
175
                info._sifields._sigfault._addr = env->eip;
176
                queue_signal(info.si_signo, &info);
177
            }
178
            break;
179
        case EXCP01_SSTP:
180
        case EXCP03_INT3:
181
            if (env->eflags & VM_MASK) {
182
                handle_vm86_trap(env, trapnr);
183
            } else {
184
                info.si_signo = SIGTRAP;
185
                info.si_errno = 0;
186
                if (trapnr == EXCP01_SSTP) {
187
                    info.si_code = TARGET_TRAP_BRKPT;
188
                    info._sifields._sigfault._addr = env->eip;
189
                } else {
190
                    info.si_code = TARGET_SI_KERNEL;
191
                    info._sifields._sigfault._addr = 0;
192
                }
193
                queue_signal(info.si_signo, &info);
194
            }
195
            break;
196
        case EXCP04_INTO:
197
        case EXCP05_BOUND:
198
            if (env->eflags & VM_MASK) {
199
                handle_vm86_trap(env, trapnr);
200
            } else {
201
                info.si_signo = SIGSEGV;
202
                info.si_errno = 0;
203
                info.si_code = TARGET_SI_KERNEL;
204
                info._sifields._sigfault._addr = 0;
205
                queue_signal(info.si_signo, &info);
206
            }
207
            break;
208
        case EXCP06_ILLOP:
209
            info.si_signo = SIGILL;
210
            info.si_errno = 0;
211
            info.si_code = TARGET_ILL_ILLOPN;
212
            info._sifields._sigfault._addr = env->eip;
213
            queue_signal(info.si_signo, &info);
214
            break;
215
        case EXCP_INTERRUPT:
216
            /* just indicate that signals should be handled asap */
217
            break;
218
        default:
219
            pc = env->seg_cache[R_CS].base + env->eip;
220
            fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", 
221
                    (long)pc, trapnr);
222
            abort();
223
        }
224
        process_pending_signals(env);
225
    }
226
}
227

    
228
void usage(void)
229
{
230
    printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
231
           "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
232
           "Linux x86 emulator\n"
233
           "\n"
234
           "-h           print this help\n"
235
           "-L path      set the x86 elf interpreter prefix (default=%s)\n"
236
           "-s size      set the x86 stack size in bytes (default=%ld)\n"
237
           "\n"
238
           "debug options:\n"
239
           "-d           activate log (logfile=%s)\n"
240
           "-p pagesize  set the host page size to 'pagesize'\n",
241
           interp_prefix, 
242
           x86_stack_size,
243
           DEBUG_LOGFILE);
244
    _exit(1);
245
}
246

    
247
/* XXX: currently only used for async signals (see signal.c) */
248
CPUX86State *global_env;
249
/* used to free thread contexts */
250
TaskState *first_task_state;
251

    
252
int main(int argc, char **argv)
253
{
254
    const char *filename;
255
    struct target_pt_regs regs1, *regs = &regs1;
256
    struct image_info info1, *info = &info1;
257
    TaskState ts1, *ts = &ts1;
258
    CPUX86State *env;
259
    int optind;
260
    const char *r;
261
    
262
    if (argc <= 1)
263
        usage();
264

    
265
    loglevel = 0;
266
    optind = 1;
267
    for(;;) {
268
        if (optind >= argc)
269
            break;
270
        r = argv[optind];
271
        if (r[0] != '-')
272
            break;
273
        optind++;
274
        r++;
275
        if (!strcmp(r, "-")) {
276
            break;
277
        } else if (!strcmp(r, "d")) {
278
            loglevel = 1;
279
        } else if (!strcmp(r, "s")) {
280
            r = argv[optind++];
281
            x86_stack_size = strtol(r, (char **)&r, 0);
282
            if (x86_stack_size <= 0)
283
                usage();
284
            if (*r == 'M')
285
                x86_stack_size *= 1024 * 1024;
286
            else if (*r == 'k' || *r == 'K')
287
                x86_stack_size *= 1024;
288
        } else if (!strcmp(r, "L")) {
289
            interp_prefix = argv[optind++];
290
        } else if (!strcmp(r, "p")) {
291
            host_page_size = atoi(argv[optind++]);
292
            if (host_page_size == 0 ||
293
                (host_page_size & (host_page_size - 1)) != 0) {
294
                fprintf(stderr, "page size must be a power of two\n");
295
                exit(1);
296
            }
297
        } else {
298
            usage();
299
        }
300
    }
301
    if (optind >= argc)
302
        usage();
303
    filename = argv[optind];
304

    
305
    /* init debug */
306
    if (loglevel) {
307
        logfile = fopen(DEBUG_LOGFILE, "w");
308
        if (!logfile) {
309
            perror(DEBUG_LOGFILE);
310
            _exit(1);
311
        }
312
        setvbuf(logfile, NULL, _IOLBF, 0);
313
    }
314

    
315
    /* Zero out regs */
316
    memset(regs, 0, sizeof(struct target_pt_regs));
317

    
318
    /* Zero out image_info */
319
    memset(info, 0, sizeof(struct image_info));
320

    
321
    /* Scan interp_prefix dir for replacement files. */
322
    init_paths(interp_prefix);
323

    
324
    /* NOTE: we need to init the CPU at this stage to get the
325
       host_page_size */
326
    env = cpu_x86_init();
327

    
328
    if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
329
        printf("Error loading %s\n", filename);
330
        _exit(1);
331
    }
332
    
333
    if (loglevel) {
334
        page_dump(logfile);
335
    
336
        fprintf(logfile, "start_brk   0x%08lx\n" , info->start_brk);
337
        fprintf(logfile, "end_code    0x%08lx\n" , info->end_code);
338
        fprintf(logfile, "start_code  0x%08lx\n" , info->start_code);
339
        fprintf(logfile, "end_data    0x%08lx\n" , info->end_data);
340
        fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
341
        fprintf(logfile, "brk         0x%08lx\n" , info->brk);
342
        fprintf(logfile, "esp         0x%08lx\n" , regs->esp);
343
        fprintf(logfile, "eip         0x%08lx\n" , regs->eip);
344
    }
345

    
346
    target_set_brk((char *)info->brk);
347
    syscall_init();
348
    signal_init();
349

    
350
    global_env = env;
351

    
352
    /* build Task State */
353
    memset(ts, 0, sizeof(TaskState));
354
    env->opaque = ts;
355
    ts->used = 1;
356
    
357
    /* linux register setup */
358
    env->regs[R_EAX] = regs->eax;
359
    env->regs[R_EBX] = regs->ebx;
360
    env->regs[R_ECX] = regs->ecx;
361
    env->regs[R_EDX] = regs->edx;
362
    env->regs[R_ESI] = regs->esi;
363
    env->regs[R_EDI] = regs->edi;
364
    env->regs[R_EBP] = regs->ebp;
365
    env->regs[R_ESP] = regs->esp;
366
    env->eip = regs->eip;
367

    
368
    /* linux segment setup */
369
    env->gdt.base = (void *)gdt_table;
370
    env->gdt.limit = sizeof(gdt_table) - 1;
371
    write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1);
372
    write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1);
373
    cpu_x86_load_seg(env, R_CS, __USER_CS);
374
    cpu_x86_load_seg(env, R_DS, __USER_DS);
375
    cpu_x86_load_seg(env, R_ES, __USER_DS);
376
    cpu_x86_load_seg(env, R_SS, __USER_DS);
377
    cpu_x86_load_seg(env, R_FS, __USER_DS);
378
    cpu_x86_load_seg(env, R_GS, __USER_DS);
379

    
380
    cpu_loop(env);
381
    /* never exits */
382
    return 0;
383
}