update
[qemu] / linux-user / main.c
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 #define DEBUG_LOGFILE "/tmp/qemu.log"
30
31 FILE *logfile = NULL;
32 int loglevel;
33 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
34
35 #ifdef __i386__
36 /* Force usage of an ELF interpreter even if it is an ELF shared
37    object ! */
38 const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
39 #endif
40
41 /* for recent libc, we add these dummies symbol which are not declared
42    when generating a linked object (bug in ld ?) */
43 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
44 long __init_array_start[0];
45 long __init_array_end[0];
46 long __fini_array_start[0];
47 long __fini_array_end[0];
48 #endif
49
50 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
51    we allocate a bigger stack. Need a better solution, for example
52    by remapping the process stack directly at the right place */
53 unsigned long x86_stack_size = 512 * 1024;
54
55 void gemu_log(const char *fmt, ...)
56 {
57     va_list ap;
58
59     va_start(ap, fmt);
60     vfprintf(stderr, fmt, ap);
61     va_end(ap);
62 }
63
64 #ifdef TARGET_I386
65 /***********************************************************/
66 /* CPUX86 core interface */
67
68 void cpu_x86_outb(CPUX86State *env, int addr, int val)
69 {
70     fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
71 }
72
73 void cpu_x86_outw(CPUX86State *env, int addr, int val)
74 {
75     fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
76 }
77
78 void cpu_x86_outl(CPUX86State *env, int addr, int val)
79 {
80     fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
81 }
82
83 int cpu_x86_inb(CPUX86State *env, int addr)
84 {
85     fprintf(stderr, "inb: port=0x%04x\n", addr);
86     return 0;
87 }
88
89 int cpu_x86_inw(CPUX86State *env, int addr)
90 {
91     fprintf(stderr, "inw: port=0x%04x\n", addr);
92     return 0;
93 }
94
95 int cpu_x86_inl(CPUX86State *env, int addr)
96 {
97     fprintf(stderr, "inl: port=0x%04x\n", addr);
98     return 0;
99 }
100
101 int cpu_x86_get_pic_interrupt(CPUX86State *env)
102 {
103     return -1;
104 }
105
106 static void write_dt(void *ptr, unsigned long addr, unsigned long limit, 
107                      int flags)
108 {
109     unsigned int e1, e2;
110     e1 = (addr << 16) | (limit & 0xffff);
111     e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
112     e2 |= flags;
113     stl((uint8_t *)ptr, e1);
114     stl((uint8_t *)ptr + 4, e2);
115 }
116
117 static void set_gate(void *ptr, unsigned int type, unsigned int dpl, 
118                      unsigned long addr, unsigned int sel)
119 {
120     unsigned int e1, e2;
121     e1 = (addr & 0xffff) | (sel << 16);
122     e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
123     stl((uint8_t *)ptr, e1);
124     stl((uint8_t *)ptr + 4, e2);
125 }
126
127 uint64_t gdt_table[6];
128 uint64_t idt_table[256];
129
130 /* only dpl matters as we do only user space emulation */
131 static void set_idt(int n, unsigned int dpl)
132 {
133     set_gate(idt_table + n, 0, dpl, 0, 0);
134 }
135
136 void cpu_loop(CPUX86State *env)
137 {
138     int trapnr;
139     uint8_t *pc;
140     target_siginfo_t info;
141
142     for(;;) {
143         trapnr = cpu_x86_exec(env);
144         switch(trapnr) {
145         case 0x80:
146             /* linux syscall */
147             env->regs[R_EAX] = do_syscall(env, 
148                                           env->regs[R_EAX], 
149                                           env->regs[R_EBX],
150                                           env->regs[R_ECX],
151                                           env->regs[R_EDX],
152                                           env->regs[R_ESI],
153                                           env->regs[R_EDI],
154                                           env->regs[R_EBP]);
155             break;
156         case EXCP0B_NOSEG:
157         case EXCP0C_STACK:
158             info.si_signo = SIGBUS;
159             info.si_errno = 0;
160             info.si_code = TARGET_SI_KERNEL;
161             info._sifields._sigfault._addr = 0;
162             queue_signal(info.si_signo, &info);
163             break;
164         case EXCP0D_GPF:
165             if (env->eflags & VM_MASK) {
166                 handle_vm86_fault(env);
167             } else {
168                 info.si_signo = SIGSEGV;
169                 info.si_errno = 0;
170                 info.si_code = TARGET_SI_KERNEL;
171                 info._sifields._sigfault._addr = 0;
172                 queue_signal(info.si_signo, &info);
173             }
174             break;
175         case EXCP0E_PAGE:
176             info.si_signo = SIGSEGV;
177             info.si_errno = 0;
178             if (!(env->error_code & 1))
179                 info.si_code = TARGET_SEGV_MAPERR;
180             else
181                 info.si_code = TARGET_SEGV_ACCERR;
182             info._sifields._sigfault._addr = env->cr[2];
183             queue_signal(info.si_signo, &info);
184             break;
185         case EXCP00_DIVZ:
186             if (env->eflags & VM_MASK) {
187                 handle_vm86_trap(env, trapnr);
188             } else {
189                 /* division by zero */
190                 info.si_signo = SIGFPE;
191                 info.si_errno = 0;
192                 info.si_code = TARGET_FPE_INTDIV;
193                 info._sifields._sigfault._addr = env->eip;
194                 queue_signal(info.si_signo, &info);
195             }
196             break;
197         case EXCP01_SSTP:
198         case EXCP03_INT3:
199             if (env->eflags & VM_MASK) {
200                 handle_vm86_trap(env, trapnr);
201             } else {
202                 info.si_signo = SIGTRAP;
203                 info.si_errno = 0;
204                 if (trapnr == EXCP01_SSTP) {
205                     info.si_code = TARGET_TRAP_BRKPT;
206                     info._sifields._sigfault._addr = env->eip;
207                 } else {
208                     info.si_code = TARGET_SI_KERNEL;
209                     info._sifields._sigfault._addr = 0;
210                 }
211                 queue_signal(info.si_signo, &info);
212             }
213             break;
214         case EXCP04_INTO:
215         case EXCP05_BOUND:
216             if (env->eflags & VM_MASK) {
217                 handle_vm86_trap(env, trapnr);
218             } else {
219                 info.si_signo = SIGSEGV;
220                 info.si_errno = 0;
221                 info.si_code = TARGET_SI_KERNEL;
222                 info._sifields._sigfault._addr = 0;
223                 queue_signal(info.si_signo, &info);
224             }
225             break;
226         case EXCP06_ILLOP:
227             info.si_signo = SIGILL;
228             info.si_errno = 0;
229             info.si_code = TARGET_ILL_ILLOPN;
230             info._sifields._sigfault._addr = env->eip;
231             queue_signal(info.si_signo, &info);
232             break;
233         case EXCP_INTERRUPT:
234             /* just indicate that signals should be handled asap */
235             break;
236         default:
237             pc = env->segs[R_CS].base + env->eip;
238             fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", 
239                     (long)pc, trapnr);
240             abort();
241         }
242         process_pending_signals(env);
243     }
244 }
245 #endif
246
247 #ifdef TARGET_ARM
248
249 #define ARM_SYSCALL_BASE        0x900000
250
251 void cpu_loop(CPUARMState *env)
252 {
253     int trapnr;
254     unsigned int n, insn;
255     target_siginfo_t info;
256     
257     for(;;) {
258         trapnr = cpu_arm_exec(env);
259         switch(trapnr) {
260         case EXCP_UDEF:
261             info.si_signo = SIGILL;
262             info.si_errno = 0;
263             info.si_code = TARGET_ILL_ILLOPN;
264             info._sifields._sigfault._addr = env->regs[15];
265             queue_signal(info.si_signo, &info);
266             break;
267         case EXCP_SWI:
268             {
269                 /* system call */
270                 insn = ldl((void *)(env->regs[15] - 4));
271                 n = insn & 0xffffff;
272                 if (n >= ARM_SYSCALL_BASE) {
273                     /* linux syscall */
274                     n -= ARM_SYSCALL_BASE;
275                     env->regs[0] = do_syscall(env, 
276                                               n, 
277                                               env->regs[0],
278                                               env->regs[1],
279                                               env->regs[2],
280                                               env->regs[3],
281                                               env->regs[4],
282                                               0);
283                 } else {
284                     goto error;
285                 }
286             }
287             break;
288         default:
289         error:
290             fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n", 
291                     trapnr);
292             cpu_arm_dump_state(env, stderr, 0);
293             abort();
294         }
295         process_pending_signals(env);
296     }
297 }
298
299 #endif
300
301 void usage(void)
302 {
303     printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
304            "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
305            "Linux CPU emulator (compiled for %s emulation)\n"
306            "\n"
307            "-h           print this help\n"
308            "-L path      set the elf interpreter prefix (default=%s)\n"
309            "-s size      set the stack size in bytes (default=%ld)\n"
310            "\n"
311            "debug options:\n"
312            "-d           activate log (logfile=%s)\n"
313            "-p pagesize  set the host page size to 'pagesize'\n",
314            TARGET_ARCH,
315            interp_prefix, 
316            x86_stack_size,
317            DEBUG_LOGFILE);
318     _exit(1);
319 }
320
321 /* XXX: currently only used for async signals (see signal.c) */
322 CPUState *global_env;
323 /* used only if single thread */
324 CPUState *cpu_single_env = NULL;
325
326 /* used to free thread contexts */
327 TaskState *first_task_state;
328
329 int main(int argc, char **argv)
330 {
331     const char *filename;
332     struct target_pt_regs regs1, *regs = &regs1;
333     struct image_info info1, *info = &info1;
334     TaskState ts1, *ts = &ts1;
335     CPUState *env;
336     int optind;
337     const char *r;
338     
339     if (argc <= 1)
340         usage();
341
342     loglevel = 0;
343     optind = 1;
344     for(;;) {
345         if (optind >= argc)
346             break;
347         r = argv[optind];
348         if (r[0] != '-')
349             break;
350         optind++;
351         r++;
352         if (!strcmp(r, "-")) {
353             break;
354         } else if (!strcmp(r, "d")) {
355             loglevel = 1;
356         } else if (!strcmp(r, "s")) {
357             r = argv[optind++];
358             x86_stack_size = strtol(r, (char **)&r, 0);
359             if (x86_stack_size <= 0)
360                 usage();
361             if (*r == 'M')
362                 x86_stack_size *= 1024 * 1024;
363             else if (*r == 'k' || *r == 'K')
364                 x86_stack_size *= 1024;
365         } else if (!strcmp(r, "L")) {
366             interp_prefix = argv[optind++];
367         } else if (!strcmp(r, "p")) {
368             host_page_size = atoi(argv[optind++]);
369             if (host_page_size == 0 ||
370                 (host_page_size & (host_page_size - 1)) != 0) {
371                 fprintf(stderr, "page size must be a power of two\n");
372                 exit(1);
373             }
374         } else {
375             usage();
376         }
377     }
378     if (optind >= argc)
379         usage();
380     filename = argv[optind];
381
382     /* init debug */
383     if (loglevel) {
384         logfile = fopen(DEBUG_LOGFILE, "w");
385         if (!logfile) {
386             perror(DEBUG_LOGFILE);
387             _exit(1);
388         }
389         setvbuf(logfile, NULL, _IOLBF, 0);
390     }
391
392     /* Zero out regs */
393     memset(regs, 0, sizeof(struct target_pt_regs));
394
395     /* Zero out image_info */
396     memset(info, 0, sizeof(struct image_info));
397
398     /* Scan interp_prefix dir for replacement files. */
399     init_paths(interp_prefix);
400
401     /* NOTE: we need to init the CPU at this stage to get the
402        host_page_size */
403     env = cpu_init();
404     
405     if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
406         printf("Error loading %s\n", filename);
407         _exit(1);
408     }
409     
410     if (loglevel) {
411         page_dump(logfile);
412     
413         fprintf(logfile, "start_brk   0x%08lx\n" , info->start_brk);
414         fprintf(logfile, "end_code    0x%08lx\n" , info->end_code);
415         fprintf(logfile, "start_code  0x%08lx\n" , info->start_code);
416         fprintf(logfile, "end_data    0x%08lx\n" , info->end_data);
417         fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
418         fprintf(logfile, "brk         0x%08lx\n" , info->brk);
419         fprintf(logfile, "entry       0x%08lx\n" , info->entry);
420     }
421
422     target_set_brk((char *)info->brk);
423     syscall_init();
424     signal_init();
425
426     global_env = env;
427
428     /* build Task State */
429     memset(ts, 0, sizeof(TaskState));
430     env->opaque = ts;
431     ts->used = 1;
432     env->user_mode_only = 1;
433     
434 #if defined(TARGET_I386)
435     /* linux register setup */
436     env->regs[R_EAX] = regs->eax;
437     env->regs[R_EBX] = regs->ebx;
438     env->regs[R_ECX] = regs->ecx;
439     env->regs[R_EDX] = regs->edx;
440     env->regs[R_ESI] = regs->esi;
441     env->regs[R_EDI] = regs->edi;
442     env->regs[R_EBP] = regs->ebp;
443     env->regs[R_ESP] = regs->esp;
444     env->eip = regs->eip;
445
446     /* linux interrupt setup */
447     env->idt.base = (void *)idt_table;
448     env->idt.limit = sizeof(idt_table) - 1;
449     set_idt(0, 0);
450     set_idt(1, 0);
451     set_idt(2, 0);
452     set_idt(3, 3);
453     set_idt(4, 3);
454     set_idt(5, 3);
455     set_idt(6, 0);
456     set_idt(7, 0);
457     set_idt(8, 0);
458     set_idt(9, 0);
459     set_idt(10, 0);
460     set_idt(11, 0);
461     set_idt(12, 0);
462     set_idt(13, 0);
463     set_idt(14, 0);
464     set_idt(15, 0);
465     set_idt(16, 0);
466     set_idt(17, 0);
467     set_idt(18, 0);
468     set_idt(19, 0);
469     set_idt(0x80, 3);
470
471     /* linux segment setup */
472     env->gdt.base = (void *)gdt_table;
473     env->gdt.limit = sizeof(gdt_table) - 1;
474     write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
475              DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 
476              (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
477     write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
478              DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 
479              (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
480     cpu_x86_load_seg(env, R_CS, __USER_CS);
481     cpu_x86_load_seg(env, R_DS, __USER_DS);
482     cpu_x86_load_seg(env, R_ES, __USER_DS);
483     cpu_x86_load_seg(env, R_SS, __USER_DS);
484     cpu_x86_load_seg(env, R_FS, __USER_DS);
485     cpu_x86_load_seg(env, R_GS, __USER_DS);
486
487 #elif defined(TARGET_ARM)
488     {
489         int i;
490         for(i = 0; i < 16; i++) {
491             env->regs[i] = regs->uregs[i];
492         }
493         env->cpsr = regs->uregs[16];
494     }
495 #else
496 #error unsupported target CPU
497 #endif
498
499     cpu_loop(env);
500     /* never exits */
501     return 0;
502 }