28999342668b9c39ff55fccf5966fc70e09ae33a
[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
36 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
37    we allocate a bigger stack. Need a better solution, for example
38    by remapping the process stack directly at the right place */
39 unsigned long x86_stack_size = 512 * 1024;
40 unsigned long stktop;
41
42 void gemu_log(const char *fmt, ...)
43 {
44     va_list ap;
45
46     va_start(ap, fmt);
47     vfprintf(stderr, fmt, ap);
48     va_end(ap);
49 }
50
51 /***********************************************************/
52 /* CPUX86 core interface */
53
54 void cpu_x86_outb(int addr, int val)
55 {
56     fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
57 }
58
59 void cpu_x86_outw(int addr, int val)
60 {
61     fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
62 }
63
64 void cpu_x86_outl(int addr, int val)
65 {
66     fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
67 }
68
69 int cpu_x86_inb(int addr)
70 {
71     fprintf(stderr, "inb: port=0x%04x\n", addr);
72     return 0;
73 }
74
75 int cpu_x86_inw(int addr)
76 {
77     fprintf(stderr, "inw: port=0x%04x\n", addr);
78     return 0;
79 }
80
81 int cpu_x86_inl(int addr)
82 {
83     fprintf(stderr, "inl: port=0x%04x\n", addr);
84     return 0;
85 }
86
87 void write_dt(void *ptr, unsigned long addr, unsigned long limit, 
88               int seg32_bit)
89 {
90     unsigned int e1, e2, limit_in_pages;
91     limit_in_pages = 0;
92     if (limit > 0xffff) {
93         limit = limit >> 12;
94         limit_in_pages = 1;
95     }
96     e1 = (addr << 16) | (limit & 0xffff);
97     e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
98     e2 |= limit_in_pages << 23; /* byte granularity */
99     e2 |= seg32_bit << 22; /* 32 bit segment */
100     stl((uint8_t *)ptr, e1);
101     stl((uint8_t *)ptr + 4, e2);
102 }
103
104 uint64_t gdt_table[6];
105
106 void cpu_loop(struct CPUX86State *env)
107 {
108     int err;
109     uint8_t *pc;
110     target_siginfo_t info;
111     
112     for(;;) {
113         err = cpu_x86_exec(env);
114         pc = env->seg_cache[R_CS].base + env->eip;
115         switch(err) {
116         case EXCP0D_GPF:
117             if (pc[0] == 0xcd && pc[1] == 0x80) {
118                 /* syscall */
119                 env->eip += 2;
120                 env->regs[R_EAX] = do_syscall(env, 
121                                               env->regs[R_EAX], 
122                                               env->regs[R_EBX],
123                                               env->regs[R_ECX],
124                                               env->regs[R_EDX],
125                                               env->regs[R_ESI],
126                                               env->regs[R_EDI],
127                                               env->regs[R_EBP]);
128             } else {
129                 /* XXX: more precise info */
130                 info.si_signo = SIGSEGV;
131                 info.si_errno = 0;
132                 info.si_code = 0;
133                 info._sifields._sigfault._addr = 0;
134                 queue_signal(info.si_signo, &info);
135             }
136             break;
137         case EXCP00_DIVZ:
138             /* division by zero */
139             info.si_signo = SIGFPE;
140             info.si_errno = 0;
141             info.si_code = TARGET_FPE_INTDIV;
142             info._sifields._sigfault._addr = env->eip;
143             queue_signal(info.si_signo, &info);
144             break;
145         case EXCP04_INTO:
146         case EXCP05_BOUND:
147             info.si_signo = SIGSEGV;
148             info.si_errno = 0;
149             info.si_code = 0;
150             info._sifields._sigfault._addr = 0;
151             queue_signal(info.si_signo, &info);
152             break;
153         case EXCP06_ILLOP:
154             info.si_signo = SIGILL;
155             info.si_errno = 0;
156             info.si_code = TARGET_ILL_ILLOPN;
157             info._sifields._sigfault._addr = env->eip;
158             queue_signal(info.si_signo, &info);
159             break;
160         case EXCP_INTERRUPT:
161             /* just indicate that signals should be handled asap */
162             break;
163         default:
164             fprintf(stderr, "0x%08lx: Unknown exception CPU %d, aborting\n", 
165                     (long)pc, err);
166             abort();
167         }
168         process_pending_signals(env);
169     }
170 }
171
172 void usage(void)
173 {
174     printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
175            "usage: qemu [-d] program [arguments...]\n"
176            "Linux x86 emulator\n"
177            );
178     exit(1);
179 }
180
181 /* XXX: currently only used for async signals (see signal.c) */
182 CPUX86State *global_env;
183
184 int main(int argc, char **argv)
185 {
186     const char *filename;
187     struct target_pt_regs regs1, *regs = &regs1;
188     struct image_info info1, *info = &info1;
189     CPUX86State *env;
190     int optind;
191
192     if (argc <= 1)
193         usage();
194     loglevel = 0;
195     optind = 1;
196     if (argv[optind] && !strcmp(argv[optind], "-d")) {
197         loglevel = 1;
198         optind++;
199     }
200     filename = argv[optind];
201
202     /* init debug */
203     if (loglevel) {
204         logfile = fopen(DEBUG_LOGFILE, "w");
205         if (!logfile) {
206             perror(DEBUG_LOGFILE);
207             exit(1);
208         }
209         setvbuf(logfile, NULL, _IOLBF, 0);
210     }
211
212     /* Zero out regs */
213     memset(regs, 0, sizeof(struct target_pt_regs));
214
215     /* Zero out image_info */
216     memset(info, 0, sizeof(struct image_info));
217
218     if(elf_exec(filename, argv+optind, environ, regs, info) != 0) {
219         printf("Error loading %s\n", filename);
220         exit(1);
221     }
222     
223     if (loglevel) {
224         fprintf(logfile, "start_brk   0x%08lx\n" , info->start_brk);
225         fprintf(logfile, "end_code    0x%08lx\n" , info->end_code);
226         fprintf(logfile, "start_code  0x%08lx\n" , info->start_code);
227         fprintf(logfile, "end_data    0x%08lx\n" , info->end_data);
228         fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
229         fprintf(logfile, "brk         0x%08lx\n" , info->brk);
230         fprintf(logfile, "esp         0x%08lx\n" , regs->esp);
231         fprintf(logfile, "eip         0x%08lx\n" , regs->eip);
232     }
233
234     target_set_brk((char *)info->brk);
235     syscall_init();
236     signal_init();
237
238     env = cpu_x86_init();
239     global_env = env;
240
241     /* linux register setup */
242     env->regs[R_EAX] = regs->eax;
243     env->regs[R_EBX] = regs->ebx;
244     env->regs[R_ECX] = regs->ecx;
245     env->regs[R_EDX] = regs->edx;
246     env->regs[R_ESI] = regs->esi;
247     env->regs[R_EDI] = regs->edi;
248     env->regs[R_EBP] = regs->ebp;
249     env->regs[R_ESP] = regs->esp;
250     env->eip = regs->eip;
251
252     /* linux segment setup */
253     env->gdt.base = (void *)gdt_table;
254     env->gdt.limit = sizeof(gdt_table) - 1;
255     write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1);
256     write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1);
257     cpu_x86_load_seg(env, R_CS, __USER_CS);
258     cpu_x86_load_seg(env, R_DS, __USER_DS);
259     cpu_x86_load_seg(env, R_ES, __USER_DS);
260     cpu_x86_load_seg(env, R_SS, __USER_DS);
261     cpu_x86_load_seg(env, R_FS, __USER_DS);
262     cpu_x86_load_seg(env, R_GS, __USER_DS);
263
264     cpu_loop(env);
265     /* never exits */
266     return 0;
267 }