Statistics
| Branch: | Revision:

root / linux-user / main.c @ 4304763b

History | View | Annotate | Download (12.7 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
#endif
42

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

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

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

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

    
66
/***********************************************************/
67
/* CPUX86 core interface */
68

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

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

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

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

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

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

    
102
static void write_dt(void *ptr, unsigned long addr, unsigned long limit, 
103
                     int flags)
104
{
105
    unsigned int e1, e2;
106
    e1 = (addr << 16) | (limit & 0xffff);
107
    e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
108
    e2 |= flags;
109
    stl((uint8_t *)ptr, e1);
110
    stl((uint8_t *)ptr + 4, e2);
111
}
112

    
113
static void set_gate(void *ptr, unsigned int type, unsigned int dpl, 
114
                     unsigned long addr, unsigned int sel)
115
{
116
    unsigned int e1, e2;
117
    e1 = (addr & 0xffff) | (sel << 16);
118
    e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
119
    stl((uint8_t *)ptr, e1);
120
    stl((uint8_t *)ptr + 4, e2);
121
}
122

    
123
uint64_t gdt_table[6];
124
uint64_t idt_table[256];
125

    
126
/* only dpl matters as we do only user space emulation */
127
static void set_idt(int n, unsigned int dpl)
128
{
129
    set_gate(idt_table + n, 0, dpl, 0, 0);
130
}
131

    
132
void cpu_loop(CPUX86State *env)
133
{
134
    int trapnr;
135
    uint8_t *pc;
136
    target_siginfo_t info;
137

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

    
242
void usage(void)
243
{
244
    printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
245
           "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
246
           "Linux x86 emulator\n"
247
           "\n"
248
           "-h           print this help\n"
249
           "-L path      set the x86 elf interpreter prefix (default=%s)\n"
250
           "-s size      set the x86 stack size in bytes (default=%ld)\n"
251
           "\n"
252
           "debug options:\n"
253
           "-d           activate log (logfile=%s)\n"
254
           "-p pagesize  set the host page size to 'pagesize'\n",
255
           interp_prefix, 
256
           x86_stack_size,
257
           DEBUG_LOGFILE);
258
    _exit(1);
259
}
260

    
261
/* XXX: currently only used for async signals (see signal.c) */
262
CPUX86State *global_env;
263
/* used to free thread contexts */
264
TaskState *first_task_state;
265

    
266
int main(int argc, char **argv)
267
{
268
    const char *filename;
269
    struct target_pt_regs regs1, *regs = &regs1;
270
    struct image_info info1, *info = &info1;
271
    TaskState ts1, *ts = &ts1;
272
    CPUX86State *env;
273
    int optind;
274
    const char *r;
275
    
276
    if (argc <= 1)
277
        usage();
278

    
279
    loglevel = 0;
280
    optind = 1;
281
    for(;;) {
282
        if (optind >= argc)
283
            break;
284
        r = argv[optind];
285
        if (r[0] != '-')
286
            break;
287
        optind++;
288
        r++;
289
        if (!strcmp(r, "-")) {
290
            break;
291
        } else if (!strcmp(r, "d")) {
292
            loglevel = 1;
293
        } else if (!strcmp(r, "s")) {
294
            r = argv[optind++];
295
            x86_stack_size = strtol(r, (char **)&r, 0);
296
            if (x86_stack_size <= 0)
297
                usage();
298
            if (*r == 'M')
299
                x86_stack_size *= 1024 * 1024;
300
            else if (*r == 'k' || *r == 'K')
301
                x86_stack_size *= 1024;
302
        } else if (!strcmp(r, "L")) {
303
            interp_prefix = argv[optind++];
304
        } else if (!strcmp(r, "p")) {
305
            host_page_size = atoi(argv[optind++]);
306
            if (host_page_size == 0 ||
307
                (host_page_size & (host_page_size - 1)) != 0) {
308
                fprintf(stderr, "page size must be a power of two\n");
309
                exit(1);
310
            }
311
        } else {
312
            usage();
313
        }
314
    }
315
    if (optind >= argc)
316
        usage();
317
    filename = argv[optind];
318

    
319
    /* init debug */
320
    if (loglevel) {
321
        logfile = fopen(DEBUG_LOGFILE, "w");
322
        if (!logfile) {
323
            perror(DEBUG_LOGFILE);
324
            _exit(1);
325
        }
326
        setvbuf(logfile, NULL, _IOLBF, 0);
327
    }
328

    
329
    /* Zero out regs */
330
    memset(regs, 0, sizeof(struct target_pt_regs));
331

    
332
    /* Zero out image_info */
333
    memset(info, 0, sizeof(struct image_info));
334

    
335
    /* Scan interp_prefix dir for replacement files. */
336
    init_paths(interp_prefix);
337

    
338
    /* NOTE: we need to init the CPU at this stage to get the
339
       host_page_size */
340
    env = cpu_x86_init();
341

    
342
    if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
343
        printf("Error loading %s\n", filename);
344
        _exit(1);
345
    }
346
    
347
    if (loglevel) {
348
        page_dump(logfile);
349
    
350
        fprintf(logfile, "start_brk   0x%08lx\n" , info->start_brk);
351
        fprintf(logfile, "end_code    0x%08lx\n" , info->end_code);
352
        fprintf(logfile, "start_code  0x%08lx\n" , info->start_code);
353
        fprintf(logfile, "end_data    0x%08lx\n" , info->end_data);
354
        fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
355
        fprintf(logfile, "brk         0x%08lx\n" , info->brk);
356
        fprintf(logfile, "esp         0x%08lx\n" , regs->esp);
357
        fprintf(logfile, "eip         0x%08lx\n" , regs->eip);
358
    }
359

    
360
    target_set_brk((char *)info->brk);
361
    syscall_init();
362
    signal_init();
363

    
364
    global_env = env;
365

    
366
    /* build Task State */
367
    memset(ts, 0, sizeof(TaskState));
368
    env->opaque = ts;
369
    ts->used = 1;
370
    
371
    /* linux register setup */
372
    env->regs[R_EAX] = regs->eax;
373
    env->regs[R_EBX] = regs->ebx;
374
    env->regs[R_ECX] = regs->ecx;
375
    env->regs[R_EDX] = regs->edx;
376
    env->regs[R_ESI] = regs->esi;
377
    env->regs[R_EDI] = regs->edi;
378
    env->regs[R_EBP] = regs->ebp;
379
    env->regs[R_ESP] = regs->esp;
380
    env->eip = regs->eip;
381

    
382
    /* linux interrupt setup */
383
    env->idt.base = (void *)idt_table;
384
    env->idt.limit = sizeof(idt_table) - 1;
385
    set_idt(0, 0);
386
    set_idt(1, 0);
387
    set_idt(2, 0);
388
    set_idt(3, 3);
389
    set_idt(4, 3);
390
    set_idt(5, 3);
391
    set_idt(6, 0);
392
    set_idt(7, 0);
393
    set_idt(8, 0);
394
    set_idt(9, 0);
395
    set_idt(10, 0);
396
    set_idt(11, 0);
397
    set_idt(12, 0);
398
    set_idt(13, 0);
399
    set_idt(14, 0);
400
    set_idt(15, 0);
401
    set_idt(16, 0);
402
    set_idt(17, 0);
403
    set_idt(18, 0);
404
    set_idt(19, 0);
405
    set_idt(0x80, 3);
406

    
407
    /* linux segment setup */
408
    env->gdt.base = (void *)gdt_table;
409
    env->gdt.limit = sizeof(gdt_table) - 1;
410
    write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
411
             DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 
412
             (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
413
    write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
414
             DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 
415
             (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
416
    cpu_x86_load_seg(env, R_CS, __USER_CS);
417
    cpu_x86_load_seg(env, R_DS, __USER_DS);
418
    cpu_x86_load_seg(env, R_ES, __USER_DS);
419
    cpu_x86_load_seg(env, R_SS, __USER_DS);
420
    cpu_x86_load_seg(env, R_FS, __USER_DS);
421
    cpu_x86_load_seg(env, R_GS, __USER_DS);
422

    
423
    cpu_loop(env);
424
    /* never exits */
425
    return 0;
426
}