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