sparc emulation target (thanx to Thomas M. Ogrisegg)
[qemu] / linux-user / main.c
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 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 dummy symbols 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 void cpu_loop(CPUARMState *env)
250 {
251     int trapnr;
252     unsigned int n, insn;
253     target_siginfo_t info;
254     
255     for(;;) {
256         trapnr = cpu_arm_exec(env);
257         switch(trapnr) {
258         case EXCP_UDEF:
259             info.si_signo = SIGILL;
260             info.si_errno = 0;
261             info.si_code = TARGET_ILL_ILLOPN;
262             info._sifields._sigfault._addr = env->regs[15];
263             queue_signal(info.si_signo, &info);
264             break;
265         case EXCP_SWI:
266             {
267                 /* system call */
268                 insn = ldl((void *)(env->regs[15] - 4));
269                 n = insn & 0xffffff;
270                 if (n >= ARM_SYSCALL_BASE) {
271                     /* linux syscall */
272                     n -= ARM_SYSCALL_BASE;
273                     env->regs[0] = do_syscall(env, 
274                                               n, 
275                                               env->regs[0],
276                                               env->regs[1],
277                                               env->regs[2],
278                                               env->regs[3],
279                                               env->regs[4],
280                                               0);
281                 } else {
282                     goto error;
283                 }
284             }
285             break;
286         case EXCP_INTERRUPT:
287             /* just indicate that signals should be handled asap */
288             break;
289         default:
290         error:
291             fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n", 
292                     trapnr);
293             cpu_arm_dump_state(env, stderr, 0);
294             abort();
295         }
296         process_pending_signals(env);
297     }
298 }
299
300 #endif
301
302 #ifdef TARGET_SPARC
303
304 void cpu_loop (CPUSPARCState *env)
305 {
306         int trapnr;
307
308         while (1) {
309                 trapnr = cpu_sparc_exec (env);
310
311                 switch (trapnr) {
312                   case 0x8: case 0x10:
313                         env->regwptr[0] = do_syscall (env, env->gregs[1],
314                                 env->regwptr[0], env->regwptr[1], env->regwptr[2],
315                                 env->regwptr[3], env->regwptr[4], env->regwptr[13]);
316                         if (env->regwptr[0] >= 0xffffffe0)
317                                 env->psr |= PSR_CARRY;
318                         break;
319                   default:
320                         printf ("Invalid trap: %d\n", trapnr);
321                         exit (1);
322                 }
323                 process_pending_signals (env);
324         }
325 }
326
327 #endif
328
329 void usage(void)
330 {
331     printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
332            "usage: qemu-" TARGET_ARCH " [-h] [-d] [-L path] [-s size] program [arguments...]\n"
333            "Linux CPU emulator (compiled for %s emulation)\n"
334            "\n"
335            "-h           print this help\n"
336            "-L path      set the elf interpreter prefix (default=%s)\n"
337            "-s size      set the stack size in bytes (default=%ld)\n"
338            "\n"
339            "debug options:\n"
340            "-d           activate log (logfile=%s)\n"
341            "-p pagesize  set the host page size to 'pagesize'\n",
342            TARGET_ARCH,
343            interp_prefix, 
344            x86_stack_size,
345            DEBUG_LOGFILE);
346     _exit(1);
347 }
348
349 /* XXX: currently only used for async signals (see signal.c) */
350 CPUState *global_env;
351 /* used only if single thread */
352 CPUState *cpu_single_env = NULL;
353
354 /* used to free thread contexts */
355 TaskState *first_task_state;
356
357 int main(int argc, char **argv)
358 {
359     const char *filename;
360     struct target_pt_regs regs1, *regs = &regs1;
361     struct image_info info1, *info = &info1;
362     TaskState ts1, *ts = &ts1;
363     CPUState *env;
364     int optind;
365     const char *r;
366     
367     if (argc <= 1)
368         usage();
369
370     loglevel = 0;
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             loglevel = 1;
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     /* init debug */
411     if (loglevel) {
412         logfile = fopen(DEBUG_LOGFILE, "w");
413         if (!logfile) {
414             perror(DEBUG_LOGFILE);
415             _exit(1);
416         }
417         setvbuf(logfile, NULL, _IOLBF, 0);
418     }
419
420     /* Zero out regs */
421     memset(regs, 0, sizeof(struct target_pt_regs));
422
423     /* Zero out image_info */
424     memset(info, 0, sizeof(struct image_info));
425
426     /* Scan interp_prefix dir for replacement files. */
427     init_paths(interp_prefix);
428
429     /* NOTE: we need to init the CPU at this stage to get the
430        host_page_size */
431     env = cpu_init();
432     
433     if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
434         printf("Error loading %s\n", filename);
435         _exit(1);
436     }
437     
438     if (loglevel) {
439         page_dump(logfile);
440     
441         fprintf(logfile, "start_brk   0x%08lx\n" , info->start_brk);
442         fprintf(logfile, "end_code    0x%08lx\n" , info->end_code);
443         fprintf(logfile, "start_code  0x%08lx\n" , info->start_code);
444         fprintf(logfile, "end_data    0x%08lx\n" , info->end_data);
445         fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
446         fprintf(logfile, "brk         0x%08lx\n" , info->brk);
447         fprintf(logfile, "entry       0x%08lx\n" , info->entry);
448     }
449
450     target_set_brk((char *)info->brk);
451     syscall_init();
452     signal_init();
453
454     global_env = env;
455
456     /* build Task State */
457     memset(ts, 0, sizeof(TaskState));
458     env->opaque = ts;
459     ts->used = 1;
460     env->user_mode_only = 1;
461     
462 #if defined(TARGET_I386)
463     cpu_x86_set_cpl(env, 3);
464
465     env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
466
467     /* linux register setup */
468     env->regs[R_EAX] = regs->eax;
469     env->regs[R_EBX] = regs->ebx;
470     env->regs[R_ECX] = regs->ecx;
471     env->regs[R_EDX] = regs->edx;
472     env->regs[R_ESI] = regs->esi;
473     env->regs[R_EDI] = regs->edi;
474     env->regs[R_EBP] = regs->ebp;
475     env->regs[R_ESP] = regs->esp;
476     env->eip = regs->eip;
477
478     /* linux interrupt setup */
479     env->idt.base = (void *)idt_table;
480     env->idt.limit = sizeof(idt_table) - 1;
481     set_idt(0, 0);
482     set_idt(1, 0);
483     set_idt(2, 0);
484     set_idt(3, 3);
485     set_idt(4, 3);
486     set_idt(5, 3);
487     set_idt(6, 0);
488     set_idt(7, 0);
489     set_idt(8, 0);
490     set_idt(9, 0);
491     set_idt(10, 0);
492     set_idt(11, 0);
493     set_idt(12, 0);
494     set_idt(13, 0);
495     set_idt(14, 0);
496     set_idt(15, 0);
497     set_idt(16, 0);
498     set_idt(17, 0);
499     set_idt(18, 0);
500     set_idt(19, 0);
501     set_idt(0x80, 3);
502
503     /* linux segment setup */
504     env->gdt.base = (void *)gdt_table;
505     env->gdt.limit = sizeof(gdt_table) - 1;
506     write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
507              DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 
508              (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
509     write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
510              DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | 
511              (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
512     cpu_x86_load_seg(env, R_CS, __USER_CS);
513     cpu_x86_load_seg(env, R_DS, __USER_DS);
514     cpu_x86_load_seg(env, R_ES, __USER_DS);
515     cpu_x86_load_seg(env, R_SS, __USER_DS);
516     cpu_x86_load_seg(env, R_FS, __USER_DS);
517     cpu_x86_load_seg(env, R_GS, __USER_DS);
518
519 #elif defined(TARGET_ARM)
520     {
521         int i;
522         for(i = 0; i < 16; i++) {
523             env->regs[i] = regs->uregs[i];
524         }
525         env->cpsr = regs->uregs[16];
526     }
527 #elif defined(TARGET_SPARC)
528         env->pc = regs->u_regs[0];
529         env->regwptr[6] = regs->u_regs[1]-0x40;
530 #else
531 #error unsupported target CPU
532 #endif
533
534     cpu_loop(env);
535     /* never exits */
536     return 0;
537 }