ARM emulation support
[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 #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 #ifdef TARGET_I386
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 #endif
243
244 #ifdef TARGET_ARM
245
246 #define ARM_SYSCALL_BASE        0x900000
247
248 void cpu_loop(CPUARMState *env)
249 {
250     int trapnr;
251     unsigned int n, insn;
252     target_siginfo_t info;
253     
254     for(;;) {
255         trapnr = cpu_arm_exec(env);
256         switch(trapnr) {
257         case EXCP_UDEF:
258             info.si_signo = SIGILL;
259             info.si_errno = 0;
260             info.si_code = TARGET_ILL_ILLOPN;
261             info._sifields._sigfault._addr = env->regs[15];
262             queue_signal(info.si_signo, &info);
263             break;
264         case EXCP_SWI:
265             {
266                 /* system call */
267                 insn = ldl((void *)(env->regs[15] - 4));
268                 n = insn & 0xffffff;
269                 if (n >= ARM_SYSCALL_BASE) {
270                     /* linux syscall */
271                     n -= ARM_SYSCALL_BASE;
272                     env->regs[0] = do_syscall(env, 
273                                               n, 
274                                               env->regs[0],
275                                               env->regs[1],
276                                               env->regs[2],
277                                               env->regs[3],
278                                               env->regs[4],
279                                               0);
280                 } else {
281                     goto error;
282                 }
283             }
284             break;
285         default:
286         error:
287             fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n", 
288                     trapnr);
289             cpu_arm_dump_state(env, stderr, 0);
290             abort();
291         }
292         process_pending_signals(env);
293     }
294 }
295
296 #endif
297
298 void usage(void)
299 {
300     printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
301            "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
302            "Linux CPU emulator (compiled for %s emulation)\n"
303            "\n"
304            "-h           print this help\n"
305            "-L path      set the elf interpreter prefix (default=%s)\n"
306            "-s size      set the stack size in bytes (default=%ld)\n"
307            "\n"
308            "debug options:\n"
309            "-d           activate log (logfile=%s)\n"
310            "-p pagesize  set the host page size to 'pagesize'\n",
311            TARGET_ARCH,
312            interp_prefix, 
313            x86_stack_size,
314            DEBUG_LOGFILE);
315     _exit(1);
316 }
317
318 /* XXX: currently only used for async signals (see signal.c) */
319 CPUState *global_env;
320 /* used to free thread contexts */
321 TaskState *first_task_state;
322
323 int main(int argc, char **argv)
324 {
325     const char *filename;
326     struct target_pt_regs regs1, *regs = &regs1;
327     struct image_info info1, *info = &info1;
328     TaskState ts1, *ts = &ts1;
329     CPUState *env;
330     int optind;
331     const char *r;
332     
333     if (argc <= 1)
334         usage();
335
336     loglevel = 0;
337     optind = 1;
338     for(;;) {
339         if (optind >= argc)
340             break;
341         r = argv[optind];
342         if (r[0] != '-')
343             break;
344         optind++;
345         r++;
346         if (!strcmp(r, "-")) {
347             break;
348         } else if (!strcmp(r, "d")) {
349             loglevel = 1;
350         } else if (!strcmp(r, "s")) {
351             r = argv[optind++];
352             x86_stack_size = strtol(r, (char **)&r, 0);
353             if (x86_stack_size <= 0)
354                 usage();
355             if (*r == 'M')
356                 x86_stack_size *= 1024 * 1024;
357             else if (*r == 'k' || *r == 'K')
358                 x86_stack_size *= 1024;
359         } else if (!strcmp(r, "L")) {
360             interp_prefix = argv[optind++];
361         } else if (!strcmp(r, "p")) {
362             host_page_size = atoi(argv[optind++]);
363             if (host_page_size == 0 ||
364                 (host_page_size & (host_page_size - 1)) != 0) {
365                 fprintf(stderr, "page size must be a power of two\n");
366                 exit(1);
367             }
368         } else {
369             usage();
370         }
371     }
372     if (optind >= argc)
373         usage();
374     filename = argv[optind];
375
376     /* init debug */
377     if (loglevel) {
378         logfile = fopen(DEBUG_LOGFILE, "w");
379         if (!logfile) {
380             perror(DEBUG_LOGFILE);
381             _exit(1);
382         }
383         setvbuf(logfile, NULL, _IOLBF, 0);
384     }
385
386     /* Zero out regs */
387     memset(regs, 0, sizeof(struct target_pt_regs));
388
389     /* Zero out image_info */
390     memset(info, 0, sizeof(struct image_info));
391
392     /* Scan interp_prefix dir for replacement files. */
393     init_paths(interp_prefix);
394
395     /* NOTE: we need to init the CPU at this stage to get the
396        host_page_size */
397     env = cpu_init();
398
399     if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
400         printf("Error loading %s\n", filename);
401         _exit(1);
402     }
403     
404     if (loglevel) {
405         page_dump(logfile);
406     
407         fprintf(logfile, "start_brk   0x%08lx\n" , info->start_brk);
408         fprintf(logfile, "end_code    0x%08lx\n" , info->end_code);
409         fprintf(logfile, "start_code  0x%08lx\n" , info->start_code);
410         fprintf(logfile, "end_data    0x%08lx\n" , info->end_data);
411         fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
412         fprintf(logfile, "brk         0x%08lx\n" , info->brk);
413         fprintf(logfile, "entry       0x%08lx\n" , info->entry);
414     }
415
416     target_set_brk((char *)info->brk);
417     syscall_init();
418     signal_init();
419
420     global_env = env;
421
422     /* build Task State */
423     memset(ts, 0, sizeof(TaskState));
424     env->opaque = ts;
425     ts->used = 1;
426     
427 #if defined(TARGET_I386)
428     /* linux register setup */
429     env->regs[R_EAX] = regs->eax;
430     env->regs[R_EBX] = regs->ebx;
431     env->regs[R_ECX] = regs->ecx;
432     env->regs[R_EDX] = regs->edx;
433     env->regs[R_ESI] = regs->esi;
434     env->regs[R_EDI] = regs->edi;
435     env->regs[R_EBP] = regs->ebp;
436     env->regs[R_ESP] = regs->esp;
437     env->eip = regs->eip;
438
439     /* linux interrupt setup */
440     env->idt.base = (void *)idt_table;
441     env->idt.limit = sizeof(idt_table) - 1;
442     set_idt(0, 0);
443     set_idt(1, 0);
444     set_idt(2, 0);
445     set_idt(3, 3);
446     set_idt(4, 3);
447     set_idt(5, 3);
448     set_idt(6, 0);
449     set_idt(7, 0);
450     set_idt(8, 0);
451     set_idt(9, 0);
452     set_idt(10, 0);
453     set_idt(11, 0);
454     set_idt(12, 0);
455     set_idt(13, 0);
456     set_idt(14, 0);
457     set_idt(15, 0);
458     set_idt(16, 0);
459     set_idt(17, 0);
460     set_idt(18, 0);
461     set_idt(19, 0);
462     set_idt(0x80, 3);
463
464     /* linux segment setup */
465     env->gdt.base = (void *)gdt_table;
466     env->gdt.limit = sizeof(gdt_table) - 1;
467     write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
468              DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 
469              (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
470     write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
471              DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 
472              (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
473     cpu_x86_load_seg(env, R_CS, __USER_CS);
474     cpu_x86_load_seg(env, R_DS, __USER_DS);
475     cpu_x86_load_seg(env, R_ES, __USER_DS);
476     cpu_x86_load_seg(env, R_SS, __USER_DS);
477     cpu_x86_load_seg(env, R_FS, __USER_DS);
478     cpu_x86_load_seg(env, R_GS, __USER_DS);
479 #elif defined(TARGET_ARM)
480     {
481         int i;
482         for(i = 0; i < 16; i++) {
483             env->regs[i] = regs->uregs[i];
484         }
485         env->cpsr = regs->uregs[16];
486     }
487 #else
488 #error unsupported target CPU
489 #endif
490
491     cpu_loop(env);
492     /* never exits */
493     return 0;
494 }