Statistics
| Branch: | Revision:

root / linux-user / main.c @ f4beb510

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
365
    global_env = env;
366

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

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

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

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