Statistics
| Branch: | Revision:

root / linux-user / main.c @ cc38b844

History | View | Annotate | Download (15.3 kB)

1
/*
2
 *  qemu user 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
#define DEBUG_LOGFILE "/tmp/qemu.log"
30

    
31
static const char *interp_prefix = CONFIG_QEMU_PREFIX;
32

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

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

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

    
53
void gemu_log(const char *fmt, ...)
54
{
55
    va_list ap;
56

    
57
    va_start(ap, fmt);
58
    vfprintf(stderr, fmt, ap);
59
    va_end(ap);
60
}
61

    
62
#ifdef TARGET_I386
63
/***********************************************************/
64
/* CPUX86 core interface */
65

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

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

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

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

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

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

    
99
int cpu_x86_get_pic_interrupt(CPUX86State *env)
100
{
101
    return -1;
102
}
103

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

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

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

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

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

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

    
245
#ifdef TARGET_ARM
246

    
247
void cpu_loop(CPUARMState *env)
248
{
249
    int trapnr;
250
    unsigned int n, insn;
251
    target_siginfo_t info;
252
    
253
    for(;;) {
254
        trapnr = cpu_arm_exec(env);
255
        switch(trapnr) {
256
        case EXCP_UDEF:
257
            info.si_signo = SIGILL;
258
            info.si_errno = 0;
259
            info.si_code = TARGET_ILL_ILLOPN;
260
            info._sifields._sigfault._addr = env->regs[15];
261
            queue_signal(info.si_signo, &info);
262
            break;
263
        case EXCP_SWI:
264
            {
265
                /* system call */
266
                insn = ldl((void *)(env->regs[15] - 4));
267
                n = insn & 0xffffff;
268
                if (n >= ARM_SYSCALL_BASE) {
269
                    /* linux syscall */
270
                    n -= ARM_SYSCALL_BASE;
271
                    env->regs[0] = do_syscall(env, 
272
                                              n, 
273
                                              env->regs[0],
274
                                              env->regs[1],
275
                                              env->regs[2],
276
                                              env->regs[3],
277
                                              env->regs[4],
278
                                              0);
279
                } else {
280
                    goto error;
281
                }
282
            }
283
            break;
284
        case EXCP_INTERRUPT:
285
            /* just indicate that signals should be handled asap */
286
            break;
287
        default:
288
        error:
289
            fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n", 
290
                    trapnr);
291
            cpu_arm_dump_state(env, stderr, 0);
292
            abort();
293
        }
294
        process_pending_signals(env);
295
    }
296
}
297

    
298
#endif
299

    
300
#ifdef TARGET_SPARC
301

    
302
void cpu_loop (CPUSPARCState *env)
303
{
304
        int trapnr;
305

    
306
        while (1) {
307
                trapnr = cpu_sparc_exec (env);
308

    
309
                switch (trapnr) {
310
                  case 0x8: case 0x10:
311
                        env->regwptr[0] = do_syscall (env, env->gregs[1],
312
                                env->regwptr[0], env->regwptr[1], env->regwptr[2],
313
                                env->regwptr[3], env->regwptr[4], env->regwptr[13]);
314
                        if (env->regwptr[0] >= 0xffffffe0)
315
                                env->psr |= PSR_CARRY;
316
                        break;
317
                  default:
318
                        printf ("Invalid trap: %d\n", trapnr);
319
                        exit (1);
320
                }
321
                process_pending_signals (env);
322
        }
323
}
324

    
325
#endif
326

    
327
void usage(void)
328
{
329
    printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
330
           "usage: qemu-" TARGET_ARCH " [-h] [-d] [-L path] [-s size] program [arguments...]\n"
331
           "Linux CPU emulator (compiled for %s emulation)\n"
332
           "\n"
333
           "-h           print this help\n"
334
           "-L path      set the elf interpreter prefix (default=%s)\n"
335
           "-s size      set the stack size in bytes (default=%ld)\n"
336
           "\n"
337
           "debug options:\n"
338
           "-d           activate log (logfile=%s)\n"
339
           "-p pagesize  set the host page size to 'pagesize'\n",
340
           TARGET_ARCH,
341
           interp_prefix, 
342
           x86_stack_size,
343
           DEBUG_LOGFILE);
344
    _exit(1);
345
}
346

    
347
/* XXX: currently only used for async signals (see signal.c) */
348
CPUState *global_env;
349
/* used only if single thread */
350
CPUState *cpu_single_env = NULL;
351

    
352
/* used to free thread contexts */
353
TaskState *first_task_state;
354

    
355
int main(int argc, char **argv)
356
{
357
    const char *filename;
358
    struct target_pt_regs regs1, *regs = &regs1;
359
    struct image_info info1, *info = &info1;
360
    TaskState ts1, *ts = &ts1;
361
    CPUState *env;
362
    int optind;
363
    const char *r;
364
    
365
    if (argc <= 1)
366
        usage();
367

    
368
    /* init debug */
369
    cpu_set_log_filename(DEBUG_LOGFILE);
370

    
371
    optind = 1;
372
    for(;;) {
373
        if (optind >= argc)
374
            break;
375
        r = argv[optind];
376
        if (r[0] != '-')
377
            break;
378
        optind++;
379
        r++;
380
        if (!strcmp(r, "-")) {
381
            break;
382
        } else if (!strcmp(r, "d")) {
383
            cpu_set_log(CPU_LOG_ALL);
384
        } else if (!strcmp(r, "s")) {
385
            r = argv[optind++];
386
            x86_stack_size = strtol(r, (char **)&r, 0);
387
            if (x86_stack_size <= 0)
388
                usage();
389
            if (*r == 'M')
390
                x86_stack_size *= 1024 * 1024;
391
            else if (*r == 'k' || *r == 'K')
392
                x86_stack_size *= 1024;
393
        } else if (!strcmp(r, "L")) {
394
            interp_prefix = argv[optind++];
395
        } else if (!strcmp(r, "p")) {
396
            host_page_size = atoi(argv[optind++]);
397
            if (host_page_size == 0 ||
398
                (host_page_size & (host_page_size - 1)) != 0) {
399
                fprintf(stderr, "page size must be a power of two\n");
400
                exit(1);
401
            }
402
        } else {
403
            usage();
404
        }
405
    }
406
    if (optind >= argc)
407
        usage();
408
    filename = argv[optind];
409

    
410
    /* Zero out regs */
411
    memset(regs, 0, sizeof(struct target_pt_regs));
412

    
413
    /* Zero out image_info */
414
    memset(info, 0, sizeof(struct image_info));
415

    
416
    /* Scan interp_prefix dir for replacement files. */
417
    init_paths(interp_prefix);
418

    
419
    /* NOTE: we need to init the CPU at this stage to get the
420
       host_page_size */
421
    env = cpu_init();
422
    
423
    if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
424
        printf("Error loading %s\n", filename);
425
        _exit(1);
426
    }
427
    
428
    if (loglevel) {
429
        page_dump(logfile);
430
    
431
        fprintf(logfile, "start_brk   0x%08lx\n" , info->start_brk);
432
        fprintf(logfile, "end_code    0x%08lx\n" , info->end_code);
433
        fprintf(logfile, "start_code  0x%08lx\n" , info->start_code);
434
        fprintf(logfile, "end_data    0x%08lx\n" , info->end_data);
435
        fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
436
        fprintf(logfile, "brk         0x%08lx\n" , info->brk);
437
        fprintf(logfile, "entry       0x%08lx\n" , info->entry);
438
    }
439

    
440
    target_set_brk((char *)info->brk);
441
    syscall_init();
442
    signal_init();
443

    
444
    global_env = env;
445

    
446
    /* build Task State */
447
    memset(ts, 0, sizeof(TaskState));
448
    env->opaque = ts;
449
    ts->used = 1;
450
    env->user_mode_only = 1;
451
    
452
#if defined(TARGET_I386)
453
    cpu_x86_set_cpl(env, 3);
454

    
455
    env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
456

    
457
    /* linux register setup */
458
    env->regs[R_EAX] = regs->eax;
459
    env->regs[R_EBX] = regs->ebx;
460
    env->regs[R_ECX] = regs->ecx;
461
    env->regs[R_EDX] = regs->edx;
462
    env->regs[R_ESI] = regs->esi;
463
    env->regs[R_EDI] = regs->edi;
464
    env->regs[R_EBP] = regs->ebp;
465
    env->regs[R_ESP] = regs->esp;
466
    env->eip = regs->eip;
467

    
468
    /* linux interrupt setup */
469
    env->idt.base = (void *)idt_table;
470
    env->idt.limit = sizeof(idt_table) - 1;
471
    set_idt(0, 0);
472
    set_idt(1, 0);
473
    set_idt(2, 0);
474
    set_idt(3, 3);
475
    set_idt(4, 3);
476
    set_idt(5, 3);
477
    set_idt(6, 0);
478
    set_idt(7, 0);
479
    set_idt(8, 0);
480
    set_idt(9, 0);
481
    set_idt(10, 0);
482
    set_idt(11, 0);
483
    set_idt(12, 0);
484
    set_idt(13, 0);
485
    set_idt(14, 0);
486
    set_idt(15, 0);
487
    set_idt(16, 0);
488
    set_idt(17, 0);
489
    set_idt(18, 0);
490
    set_idt(19, 0);
491
    set_idt(0x80, 3);
492

    
493
    /* linux segment setup */
494
    env->gdt.base = (void *)gdt_table;
495
    env->gdt.limit = sizeof(gdt_table) - 1;
496
    write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
497
             DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 
498
             (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
499
    write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
500
             DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 
501
             (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
502
    cpu_x86_load_seg(env, R_CS, __USER_CS);
503
    cpu_x86_load_seg(env, R_DS, __USER_DS);
504
    cpu_x86_load_seg(env, R_ES, __USER_DS);
505
    cpu_x86_load_seg(env, R_SS, __USER_DS);
506
    cpu_x86_load_seg(env, R_FS, __USER_DS);
507
    cpu_x86_load_seg(env, R_GS, __USER_DS);
508

    
509
#elif defined(TARGET_ARM)
510
    {
511
        int i;
512
        for(i = 0; i < 16; i++) {
513
            env->regs[i] = regs->uregs[i];
514
        }
515
        env->cpsr = regs->uregs[16];
516
    }
517
#elif defined(TARGET_SPARC)
518
        env->pc = regs->u_regs[0];
519
        env->regwptr[6] = regs->u_regs[1]-0x40;
520
#else
521
#error unsupported target CPU
522
#endif
523

    
524
    cpu_loop(env);
525
    /* never exits */
526
    return 0;
527
}