Statistics
| Branch: | Revision:

root / linux-user / main.c @ ae48a073

History | View | Annotate | Download (15.3 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(int addr, int val)
71
{
72
    fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
73
}
74

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

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

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

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

    
97
int cpu_x86_inl(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
//#define DEBUG_VM86
123

    
124
static inline int is_revectored(int nr, struct target_revectored_struct *bitmap)
125
{
126
    return (tswap32(bitmap->__map[nr >> 5]) >> (nr & 0x1f)) & 1;
127
}
128

    
129
static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
130
{
131
    return (uint8_t *)((seg << 4) + (reg & 0xffff));
132
}
133

    
134
static inline void pushw(CPUX86State *env, int val)
135
{
136
    env->regs[R_ESP] = (env->regs[R_ESP] & ~0xffff) | 
137
        ((env->regs[R_ESP] - 2) & 0xffff);
138
    *(uint16_t *)seg_to_linear(env->segs[R_SS], env->regs[R_ESP]) = val;
139
}
140

    
141
static inline unsigned int get_vflags(CPUX86State *env)
142
{
143
    unsigned int eflags;
144
    eflags = env->eflags & ~(VM_MASK | RF_MASK | IF_MASK);
145
    if (eflags & VIF_MASK)
146
        eflags |= IF_MASK;
147
    return eflags;
148
}
149

    
150
void save_v86_state(CPUX86State *env)
151
{
152
    TaskState *ts = env->opaque;
153
#ifdef DEBUG_VM86
154
    printf("save_v86_state\n");
155
#endif
156

    
157
    /* put the VM86 registers in the userspace register structure */
158
    ts->target_v86->regs.eax = tswap32(env->regs[R_EAX]);
159
    ts->target_v86->regs.ebx = tswap32(env->regs[R_EBX]);
160
    ts->target_v86->regs.ecx = tswap32(env->regs[R_ECX]);
161
    ts->target_v86->regs.edx = tswap32(env->regs[R_EDX]);
162
    ts->target_v86->regs.esi = tswap32(env->regs[R_ESI]);
163
    ts->target_v86->regs.edi = tswap32(env->regs[R_EDI]);
164
    ts->target_v86->regs.ebp = tswap32(env->regs[R_EBP]);
165
    ts->target_v86->regs.esp = tswap32(env->regs[R_ESP]);
166
    ts->target_v86->regs.eip = tswap32(env->eip);
167
    ts->target_v86->regs.cs = tswap16(env->segs[R_CS]);
168
    ts->target_v86->regs.ss = tswap16(env->segs[R_SS]);
169
    ts->target_v86->regs.ds = tswap16(env->segs[R_DS]);
170
    ts->target_v86->regs.es = tswap16(env->segs[R_ES]);
171
    ts->target_v86->regs.fs = tswap16(env->segs[R_FS]);
172
    ts->target_v86->regs.gs = tswap16(env->segs[R_GS]);
173
    ts->target_v86->regs.eflags = tswap32(env->eflags);
174

    
175
    /* restore 32 bit registers */
176
    env->regs[R_EAX] = ts->vm86_saved_regs.eax;
177
    env->regs[R_EBX] = ts->vm86_saved_regs.ebx;
178
    env->regs[R_ECX] = ts->vm86_saved_regs.ecx;
179
    env->regs[R_EDX] = ts->vm86_saved_regs.edx;
180
    env->regs[R_ESI] = ts->vm86_saved_regs.esi;
181
    env->regs[R_EDI] = ts->vm86_saved_regs.edi;
182
    env->regs[R_EBP] = ts->vm86_saved_regs.ebp;
183
    env->regs[R_ESP] = ts->vm86_saved_regs.esp;
184
    env->eflags = ts->vm86_saved_regs.eflags;
185
    env->eip = ts->vm86_saved_regs.eip;
186
    
187
    cpu_x86_load_seg(env, R_CS, ts->vm86_saved_regs.cs);
188
    cpu_x86_load_seg(env, R_SS, ts->vm86_saved_regs.ss);
189
    cpu_x86_load_seg(env, R_DS, ts->vm86_saved_regs.ds);
190
    cpu_x86_load_seg(env, R_ES, ts->vm86_saved_regs.es);
191
    cpu_x86_load_seg(env, R_FS, ts->vm86_saved_regs.fs);
192
    cpu_x86_load_seg(env, R_GS, ts->vm86_saved_regs.gs);
193
}
194

    
195
/* return from vm86 mode to 32 bit. The vm86() syscall will return
196
   'retval' */
197
static inline void return_to_32bit(CPUX86State *env, int retval)
198
{
199
#ifdef DEBUG_VM86
200
    printf("return_to_32bit: ret=0x%x\n", retval);
201
#endif
202
    save_v86_state(env);
203
    env->regs[R_EAX] = retval;
204
}
205

    
206
/* handle VM86 interrupt (NOTE: the CPU core currently does not
207
   support TSS interrupt revectoring, so this code is always executed) */
208
static void do_int(CPUX86State *env, int intno)
209
{
210
    TaskState *ts = env->opaque;
211
    uint32_t *int_ptr, segoffs;
212
    
213
    if (env->segs[R_CS] == TARGET_BIOSSEG)
214
        goto cannot_handle; /* XXX: I am not sure this is really useful */
215
    if (is_revectored(intno, &ts->target_v86->int_revectored))
216
        goto cannot_handle;
217
    if (intno == 0x21 && is_revectored((env->regs[R_EAX] >> 8) & 0xff, 
218
                                       &ts->target_v86->int21_revectored))
219
        goto cannot_handle;
220
    int_ptr = (uint32_t *)(intno << 2);
221
    segoffs = tswap32(*int_ptr);
222
    if ((segoffs >> 16) == TARGET_BIOSSEG)
223
        goto cannot_handle;
224
#ifdef DEBUG_VM86
225
    printf("VM86: emulating int 0x%x. CS:IP=%04x:%04x\n", 
226
           intno, segoffs >> 16, segoffs & 0xffff);
227
#endif
228
    /* save old state */
229
    pushw(env, get_vflags(env));
230
    pushw(env, env->segs[R_CS]);
231
    pushw(env, env->eip);
232
    /* goto interrupt handler */
233
    env->eip = segoffs & 0xffff;
234
    cpu_x86_load_seg(env, R_CS, segoffs >> 16);
235
    env->eflags &= ~(VIF_MASK | TF_MASK);
236
    return;
237
 cannot_handle:
238
#ifdef DEBUG_VM86
239
    printf("VM86: return to 32 bits int 0x%x\n", intno);
240
#endif
241
    return_to_32bit(env, TARGET_VM86_INTx | (intno << 8));
242
}
243

    
244
void cpu_loop(struct CPUX86State *env)
245
{
246
    int trapnr;
247
    uint8_t *pc;
248
    target_siginfo_t info;
249

    
250
    for(;;) {
251
        trapnr = cpu_x86_exec(env);
252
        pc = env->seg_cache[R_CS].base + env->eip;
253
        switch(trapnr) {
254
        case EXCP0D_GPF:
255
            if (env->eflags & VM_MASK) {
256
#ifdef DEBUG_VM86
257
                printf("VM86 exception %04x:%08x %02x %02x\n",
258
                       env->segs[R_CS], env->eip, pc[0], pc[1]);
259
#endif
260
                /* VM86 mode */
261
                switch(pc[0]) {
262
                case 0xcd: /* int */
263
                    env->eip += 2;
264
                    do_int(env, pc[1]);
265
                    break;
266
                case 0x66:
267
                    switch(pc[1]) {
268
                    case 0xfb: /* sti */
269
                    case 0x9d: /* popf */
270
                    case 0xcf: /* iret */
271
                        env->eip += 2;
272
                        return_to_32bit(env, TARGET_VM86_STI);
273
                        break;
274
                    default:
275
                        goto vm86_gpf;
276
                    }
277
                    break;
278
                case 0xfb: /* sti */
279
                case 0x9d: /* popf */
280
                case 0xcf: /* iret */
281
                    env->eip++;
282
                    return_to_32bit(env, TARGET_VM86_STI);
283
                    break;
284
                default:
285
                vm86_gpf:
286
                    /* real VM86 GPF exception */
287
                    return_to_32bit(env, TARGET_VM86_UNKNOWN);
288
                    break;
289
                }
290
            } else {
291
                if (pc[0] == 0xcd && pc[1] == 0x80) {
292
                    /* syscall */
293
                    env->eip += 2;
294
                    env->regs[R_EAX] = do_syscall(env, 
295
                                                  env->regs[R_EAX], 
296
                                                  env->regs[R_EBX],
297
                                                  env->regs[R_ECX],
298
                                                  env->regs[R_EDX],
299
                                                  env->regs[R_ESI],
300
                                                  env->regs[R_EDI],
301
                                                  env->regs[R_EBP]);
302
                } else {
303
                    /* XXX: more precise info */
304
                    info.si_signo = SIGSEGV;
305
                    info.si_errno = 0;
306
                    info.si_code = 0;
307
                    info._sifields._sigfault._addr = 0;
308
                    queue_signal(info.si_signo, &info);
309
                }
310
            }
311
            break;
312
        case EXCP00_DIVZ:
313
            if (env->eflags & VM_MASK) {
314
                do_int(env, trapnr);
315
            } else {
316
                /* division by zero */
317
                info.si_signo = SIGFPE;
318
                info.si_errno = 0;
319
                info.si_code = TARGET_FPE_INTDIV;
320
                info._sifields._sigfault._addr = env->eip;
321
                queue_signal(info.si_signo, &info);
322
            }
323
            break;
324
        case EXCP04_INTO:
325
        case EXCP05_BOUND:
326
            if (env->eflags & VM_MASK) {
327
                do_int(env, trapnr);
328
            } else {
329
                info.si_signo = SIGSEGV;
330
                info.si_errno = 0;
331
                info.si_code = 0;
332
                info._sifields._sigfault._addr = 0;
333
                queue_signal(info.si_signo, &info);
334
            }
335
            break;
336
        case EXCP06_ILLOP:
337
            info.si_signo = SIGILL;
338
            info.si_errno = 0;
339
            info.si_code = TARGET_ILL_ILLOPN;
340
            info._sifields._sigfault._addr = env->eip;
341
            queue_signal(info.si_signo, &info);
342
            break;
343
        case EXCP_INTERRUPT:
344
            /* just indicate that signals should be handled asap */
345
            break;
346
        default:
347
            fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", 
348
                    (long)pc, trapnr);
349
            abort();
350
        }
351
        process_pending_signals(env);
352
    }
353
}
354

    
355
void usage(void)
356
{
357
    printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
358
           "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
359
           "Linux x86 emulator\n"
360
           "\n"
361
           "-h        print this help\n"
362
           "-d        activate log (logfile=%s)\n"
363
           "-L path   set the x86 elf interpreter prefix (default=%s)\n"
364
           "-s size   set the x86 stack size in bytes (default=%ld)\n",
365
           DEBUG_LOGFILE,
366
           interp_prefix, 
367
           x86_stack_size);
368
    _exit(1);
369
}
370

    
371
/* XXX: currently only used for async signals (see signal.c) */
372
CPUX86State *global_env;
373
/* used to free thread contexts */
374
TaskState *first_task_state;
375

    
376
int main(int argc, char **argv)
377
{
378
    const char *filename;
379
    struct target_pt_regs regs1, *regs = &regs1;
380
    struct image_info info1, *info = &info1;
381
    TaskState ts1, *ts = &ts1;
382
    CPUX86State *env;
383
    int optind;
384
    const char *r;
385
    
386
    if (argc <= 1)
387
        usage();
388

    
389
    loglevel = 0;
390
    optind = 1;
391
    for(;;) {
392
        if (optind >= argc)
393
            break;
394
        r = argv[optind];
395
        if (r[0] != '-')
396
            break;
397
        optind++;
398
        r++;
399
        if (!strcmp(r, "-")) {
400
            break;
401
        } else if (!strcmp(r, "d")) {
402
            loglevel = 1;
403
        } else if (!strcmp(r, "s")) {
404
            r = argv[optind++];
405
            x86_stack_size = strtol(r, (char **)&r, 0);
406
            if (x86_stack_size <= 0)
407
                usage();
408
            if (*r == 'M')
409
                x86_stack_size *= 1024 * 1024;
410
            else if (*r == 'k' || *r == 'K')
411
                x86_stack_size *= 1024;
412
        } else if (!strcmp(r, "L")) {
413
            interp_prefix = argv[optind++];
414
        } else {
415
            usage();
416
        }
417
    }
418
    if (optind >= argc)
419
        usage();
420
    filename = argv[optind];
421

    
422
    /* init debug */
423
    if (loglevel) {
424
        logfile = fopen(DEBUG_LOGFILE, "w");
425
        if (!logfile) {
426
            perror(DEBUG_LOGFILE);
427
            _exit(1);
428
        }
429
        setvbuf(logfile, NULL, _IOLBF, 0);
430
    }
431

    
432
    /* Zero out regs */
433
    memset(regs, 0, sizeof(struct target_pt_regs));
434

    
435
    /* Zero out image_info */
436
    memset(info, 0, sizeof(struct image_info));
437

    
438
    /* Scan interp_prefix dir for replacement files. */
439
    init_paths(interp_prefix);
440

    
441
    if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
442
        printf("Error loading %s\n", filename);
443
        _exit(1);
444
    }
445
    
446
    if (loglevel) {
447
        fprintf(logfile, "start_brk   0x%08lx\n" , info->start_brk);
448
        fprintf(logfile, "end_code    0x%08lx\n" , info->end_code);
449
        fprintf(logfile, "start_code  0x%08lx\n" , info->start_code);
450
        fprintf(logfile, "end_data    0x%08lx\n" , info->end_data);
451
        fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
452
        fprintf(logfile, "brk         0x%08lx\n" , info->brk);
453
        fprintf(logfile, "esp         0x%08lx\n" , regs->esp);
454
        fprintf(logfile, "eip         0x%08lx\n" , regs->eip);
455
    }
456

    
457
    target_set_brk((char *)info->brk);
458
    syscall_init();
459
    signal_init();
460

    
461
    env = cpu_x86_init();
462
    global_env = env;
463

    
464
    /* build Task State */
465
    memset(ts, 0, sizeof(TaskState));
466
    env->opaque = ts;
467
    ts->used = 1;
468
    
469
    /* linux register setup */
470
    env->regs[R_EAX] = regs->eax;
471
    env->regs[R_EBX] = regs->ebx;
472
    env->regs[R_ECX] = regs->ecx;
473
    env->regs[R_EDX] = regs->edx;
474
    env->regs[R_ESI] = regs->esi;
475
    env->regs[R_EDI] = regs->edi;
476
    env->regs[R_EBP] = regs->ebp;
477
    env->regs[R_ESP] = regs->esp;
478
    env->eip = regs->eip;
479

    
480
    /* linux segment setup */
481
    env->gdt.base = (void *)gdt_table;
482
    env->gdt.limit = sizeof(gdt_table) - 1;
483
    write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1);
484
    write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1);
485
    cpu_x86_load_seg(env, R_CS, __USER_CS);
486
    cpu_x86_load_seg(env, R_DS, __USER_DS);
487
    cpu_x86_load_seg(env, R_ES, __USER_DS);
488
    cpu_x86_load_seg(env, R_SS, __USER_DS);
489
    cpu_x86_load_seg(env, R_FS, __USER_DS);
490
    cpu_x86_load_seg(env, R_GS, __USER_DS);
491

    
492
    cpu_loop(env);
493
    /* never exits */
494
    return 0;
495
}