]>
Commit | Line | Data |
---|---|---|
31e31b8a | 1 | /* |
3ef693a0 | 2 | * qemu main |
31e31b8a FB |
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> | |
04369ff2 | 23 | #include <string.h> |
31e31b8a | 24 | #include <errno.h> |
0ecfa993 | 25 | #include <unistd.h> |
31e31b8a | 26 | |
3ef693a0 | 27 | #include "qemu.h" |
31e31b8a | 28 | |
0ecfa993 | 29 | #include "cpu-i386.h" |
31e31b8a | 30 | |
3ef693a0 | 31 | #define DEBUG_LOGFILE "/tmp/qemu.log" |
586314f2 FB |
32 | |
33 | FILE *logfile = NULL; | |
34 | int loglevel; | |
74cd30b8 | 35 | static const char *interp_prefix = CONFIG_QEMU_PREFIX; |
586314f2 | 36 | |
f801f97e FB |
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"; | |
74cd30b8 FB |
41 | |
42 | /* for recent libc, we add these dummies symbol which are not declared | |
43 | when generating a linked object (bug in ld ?) */ | |
44 | #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) | |
45 | long __init_array_start[0]; | |
46 | long __init_array_end[0]; | |
47 | long __fini_array_start[0]; | |
48 | long __fini_array_end[0]; | |
49 | #endif | |
50 | ||
f801f97e FB |
51 | #endif |
52 | ||
9de5e440 FB |
53 | /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so |
54 | we allocate a bigger stack. Need a better solution, for example | |
55 | by remapping the process stack directly at the right place */ | |
56 | unsigned long x86_stack_size = 512 * 1024; | |
31e31b8a FB |
57 | |
58 | void gemu_log(const char *fmt, ...) | |
59 | { | |
60 | va_list ap; | |
61 | ||
62 | va_start(ap, fmt); | |
63 | vfprintf(stderr, fmt, ap); | |
64 | va_end(ap); | |
65 | } | |
66 | ||
31e31b8a | 67 | /***********************************************************/ |
0ecfa993 | 68 | /* CPUX86 core interface */ |
367e86e8 | 69 | |
b689bc57 | 70 | void cpu_x86_outb(CPUX86State *env, int addr, int val) |
367e86e8 FB |
71 | { |
72 | fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val); | |
73 | } | |
74 | ||
b689bc57 | 75 | void cpu_x86_outw(CPUX86State *env, int addr, int val) |
367e86e8 FB |
76 | { |
77 | fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val); | |
78 | } | |
79 | ||
b689bc57 | 80 | void cpu_x86_outl(CPUX86State *env, int addr, int val) |
367e86e8 FB |
81 | { |
82 | fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val); | |
83 | } | |
84 | ||
b689bc57 | 85 | int cpu_x86_inb(CPUX86State *env, int addr) |
367e86e8 FB |
86 | { |
87 | fprintf(stderr, "inb: port=0x%04x\n", addr); | |
88 | return 0; | |
89 | } | |
90 | ||
b689bc57 | 91 | int cpu_x86_inw(CPUX86State *env, int addr) |
367e86e8 FB |
92 | { |
93 | fprintf(stderr, "inw: port=0x%04x\n", addr); | |
94 | return 0; | |
95 | } | |
96 | ||
b689bc57 | 97 | int cpu_x86_inl(CPUX86State *env, int addr) |
367e86e8 FB |
98 | { |
99 | fprintf(stderr, "inl: port=0x%04x\n", addr); | |
100 | return 0; | |
101 | } | |
102 | ||
6dbad63e FB |
103 | void write_dt(void *ptr, unsigned long addr, unsigned long limit, |
104 | int seg32_bit) | |
105 | { | |
106 | unsigned int e1, e2, limit_in_pages; | |
107 | limit_in_pages = 0; | |
108 | if (limit > 0xffff) { | |
109 | limit = limit >> 12; | |
110 | limit_in_pages = 1; | |
111 | } | |
112 | e1 = (addr << 16) | (limit & 0xffff); | |
113 | e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); | |
114 | e2 |= limit_in_pages << 23; /* byte granularity */ | |
115 | e2 |= seg32_bit << 22; /* 32 bit segment */ | |
116 | stl((uint8_t *)ptr, e1); | |
117 | stl((uint8_t *)ptr + 4, e2); | |
118 | } | |
119 | ||
120 | uint64_t gdt_table[6]; | |
31e31b8a | 121 | |
89e957e7 | 122 | void cpu_loop(CPUX86State *env) |
1b6b029e | 123 | { |
bc8a22cc | 124 | int trapnr; |
9de5e440 FB |
125 | uint8_t *pc; |
126 | target_siginfo_t info; | |
851e67a1 | 127 | |
1b6b029e | 128 | for(;;) { |
bc8a22cc | 129 | trapnr = cpu_x86_exec(env); |
bc8a22cc | 130 | switch(trapnr) { |
1b6b029e | 131 | case EXCP0D_GPF: |
851e67a1 | 132 | if (env->eflags & VM_MASK) { |
89e957e7 | 133 | handle_vm86_fault(env); |
1b6b029e | 134 | } else { |
89e957e7 | 135 | pc = env->seg_cache[R_CS].base + env->eip; |
851e67a1 FB |
136 | if (pc[0] == 0xcd && pc[1] == 0x80) { |
137 | /* syscall */ | |
138 | env->eip += 2; | |
139 | env->regs[R_EAX] = do_syscall(env, | |
140 | env->regs[R_EAX], | |
141 | env->regs[R_EBX], | |
142 | env->regs[R_ECX], | |
143 | env->regs[R_EDX], | |
144 | env->regs[R_ESI], | |
145 | env->regs[R_EDI], | |
146 | env->regs[R_EBP]); | |
147 | } else { | |
148 | /* XXX: more precise info */ | |
149 | info.si_signo = SIGSEGV; | |
150 | info.si_errno = 0; | |
b689bc57 | 151 | info.si_code = TARGET_SI_KERNEL; |
851e67a1 FB |
152 | info._sifields._sigfault._addr = 0; |
153 | queue_signal(info.si_signo, &info); | |
154 | } | |
1b6b029e FB |
155 | } |
156 | break; | |
b689bc57 FB |
157 | case EXCP0E_PAGE: |
158 | info.si_signo = SIGSEGV; | |
159 | info.si_errno = 0; | |
160 | if (!(env->error_code & 1)) | |
161 | info.si_code = TARGET_SEGV_MAPERR; | |
162 | else | |
163 | info.si_code = TARGET_SEGV_ACCERR; | |
164 | info._sifields._sigfault._addr = env->cr2; | |
165 | queue_signal(info.si_signo, &info); | |
166 | break; | |
9de5e440 | 167 | case EXCP00_DIVZ: |
bc8a22cc | 168 | if (env->eflags & VM_MASK) { |
447db213 | 169 | handle_vm86_trap(env, trapnr); |
bc8a22cc FB |
170 | } else { |
171 | /* division by zero */ | |
172 | info.si_signo = SIGFPE; | |
173 | info.si_errno = 0; | |
174 | info.si_code = TARGET_FPE_INTDIV; | |
175 | info._sifields._sigfault._addr = env->eip; | |
176 | queue_signal(info.si_signo, &info); | |
177 | } | |
9de5e440 | 178 | break; |
447db213 FB |
179 | case EXCP01_SSTP: |
180 | case EXCP03_INT3: | |
181 | if (env->eflags & VM_MASK) { | |
182 | handle_vm86_trap(env, trapnr); | |
183 | } else { | |
184 | info.si_signo = SIGTRAP; | |
185 | info.si_errno = 0; | |
186 | if (trapnr == EXCP01_SSTP) { | |
187 | info.si_code = TARGET_TRAP_BRKPT; | |
188 | info._sifields._sigfault._addr = env->eip; | |
189 | } else { | |
190 | info.si_code = TARGET_SI_KERNEL; | |
191 | info._sifields._sigfault._addr = 0; | |
192 | } | |
193 | queue_signal(info.si_signo, &info); | |
194 | } | |
195 | break; | |
9de5e440 FB |
196 | case EXCP04_INTO: |
197 | case EXCP05_BOUND: | |
bc8a22cc | 198 | if (env->eflags & VM_MASK) { |
447db213 | 199 | handle_vm86_trap(env, trapnr); |
bc8a22cc FB |
200 | } else { |
201 | info.si_signo = SIGSEGV; | |
202 | info.si_errno = 0; | |
b689bc57 | 203 | info.si_code = TARGET_SI_KERNEL; |
bc8a22cc FB |
204 | info._sifields._sigfault._addr = 0; |
205 | queue_signal(info.si_signo, &info); | |
206 | } | |
9de5e440 FB |
207 | break; |
208 | case EXCP06_ILLOP: | |
209 | info.si_signo = SIGILL; | |
210 | info.si_errno = 0; | |
211 | info.si_code = TARGET_ILL_ILLOPN; | |
212 | info._sifields._sigfault._addr = env->eip; | |
213 | queue_signal(info.si_signo, &info); | |
214 | break; | |
215 | case EXCP_INTERRUPT: | |
216 | /* just indicate that signals should be handled asap */ | |
217 | break; | |
1b6b029e | 218 | default: |
89e957e7 | 219 | pc = env->seg_cache[R_CS].base + env->eip; |
bc8a22cc FB |
220 | fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", |
221 | (long)pc, trapnr); | |
1b6b029e FB |
222 | abort(); |
223 | } | |
66fb9763 | 224 | process_pending_signals(env); |
1b6b029e FB |
225 | } |
226 | } | |
227 | ||
31e31b8a FB |
228 | void usage(void) |
229 | { | |
3ef693a0 | 230 | printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
d691f669 | 231 | "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n" |
31e31b8a | 232 | "Linux x86 emulator\n" |
d691f669 | 233 | "\n" |
54936004 FB |
234 | "-h print this help\n" |
235 | "-L path set the x86 elf interpreter prefix (default=%s)\n" | |
236 | "-s size set the x86 stack size in bytes (default=%ld)\n" | |
237 | "\n" | |
238 | "debug options:\n" | |
239 | "-d activate log (logfile=%s)\n" | |
240 | "-p pagesize set the host page size to 'pagesize'\n", | |
d691f669 | 241 | interp_prefix, |
54936004 FB |
242 | x86_stack_size, |
243 | DEBUG_LOGFILE); | |
74cd30b8 | 244 | _exit(1); |
31e31b8a FB |
245 | } |
246 | ||
9de5e440 FB |
247 | /* XXX: currently only used for async signals (see signal.c) */ |
248 | CPUX86State *global_env; | |
851e67a1 FB |
249 | /* used to free thread contexts */ |
250 | TaskState *first_task_state; | |
9de5e440 | 251 | |
31e31b8a FB |
252 | int main(int argc, char **argv) |
253 | { | |
254 | const char *filename; | |
01ffc75b | 255 | struct target_pt_regs regs1, *regs = ®s1; |
31e31b8a | 256 | struct image_info info1, *info = &info1; |
851e67a1 | 257 | TaskState ts1, *ts = &ts1; |
0ecfa993 | 258 | CPUX86State *env; |
586314f2 | 259 | int optind; |
d691f669 FB |
260 | const char *r; |
261 | ||
31e31b8a FB |
262 | if (argc <= 1) |
263 | usage(); | |
f801f97e | 264 | |
586314f2 FB |
265 | loglevel = 0; |
266 | optind = 1; | |
d691f669 FB |
267 | for(;;) { |
268 | if (optind >= argc) | |
269 | break; | |
270 | r = argv[optind]; | |
271 | if (r[0] != '-') | |
272 | break; | |
586314f2 | 273 | optind++; |
d691f669 FB |
274 | r++; |
275 | if (!strcmp(r, "-")) { | |
276 | break; | |
277 | } else if (!strcmp(r, "d")) { | |
278 | loglevel = 1; | |
279 | } else if (!strcmp(r, "s")) { | |
280 | r = argv[optind++]; | |
281 | x86_stack_size = strtol(r, (char **)&r, 0); | |
282 | if (x86_stack_size <= 0) | |
283 | usage(); | |
284 | if (*r == 'M') | |
285 | x86_stack_size *= 1024 * 1024; | |
286 | else if (*r == 'k' || *r == 'K') | |
287 | x86_stack_size *= 1024; | |
288 | } else if (!strcmp(r, "L")) { | |
289 | interp_prefix = argv[optind++]; | |
54936004 FB |
290 | } else if (!strcmp(r, "p")) { |
291 | host_page_size = atoi(argv[optind++]); | |
292 | if (host_page_size == 0 || | |
293 | (host_page_size & (host_page_size - 1)) != 0) { | |
294 | fprintf(stderr, "page size must be a power of two\n"); | |
295 | exit(1); | |
296 | } | |
d691f669 FB |
297 | } else { |
298 | usage(); | |
299 | } | |
586314f2 | 300 | } |
d691f669 FB |
301 | if (optind >= argc) |
302 | usage(); | |
586314f2 FB |
303 | filename = argv[optind]; |
304 | ||
305 | /* init debug */ | |
306 | if (loglevel) { | |
307 | logfile = fopen(DEBUG_LOGFILE, "w"); | |
308 | if (!logfile) { | |
309 | perror(DEBUG_LOGFILE); | |
74cd30b8 | 310 | _exit(1); |
586314f2 FB |
311 | } |
312 | setvbuf(logfile, NULL, _IOLBF, 0); | |
313 | } | |
31e31b8a FB |
314 | |
315 | /* Zero out regs */ | |
01ffc75b | 316 | memset(regs, 0, sizeof(struct target_pt_regs)); |
31e31b8a FB |
317 | |
318 | /* Zero out image_info */ | |
319 | memset(info, 0, sizeof(struct image_info)); | |
320 | ||
74cd30b8 FB |
321 | /* Scan interp_prefix dir for replacement files. */ |
322 | init_paths(interp_prefix); | |
323 | ||
54936004 FB |
324 | /* NOTE: we need to init the CPU at this stage to get the |
325 | host_page_size */ | |
326 | env = cpu_x86_init(); | |
327 | ||
74cd30b8 | 328 | if (elf_exec(filename, argv+optind, environ, regs, info) != 0) { |
31e31b8a | 329 | printf("Error loading %s\n", filename); |
74cd30b8 | 330 | _exit(1); |
31e31b8a FB |
331 | } |
332 | ||
4b74fe1f | 333 | if (loglevel) { |
54936004 FB |
334 | page_dump(logfile); |
335 | ||
4b74fe1f FB |
336 | fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk); |
337 | fprintf(logfile, "end_code 0x%08lx\n" , info->end_code); | |
338 | fprintf(logfile, "start_code 0x%08lx\n" , info->start_code); | |
339 | fprintf(logfile, "end_data 0x%08lx\n" , info->end_data); | |
340 | fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack); | |
341 | fprintf(logfile, "brk 0x%08lx\n" , info->brk); | |
342 | fprintf(logfile, "esp 0x%08lx\n" , regs->esp); | |
343 | fprintf(logfile, "eip 0x%08lx\n" , regs->eip); | |
344 | } | |
31e31b8a FB |
345 | |
346 | target_set_brk((char *)info->brk); | |
347 | syscall_init(); | |
66fb9763 | 348 | signal_init(); |
31e31b8a | 349 | |
9de5e440 | 350 | global_env = env; |
0ecfa993 | 351 | |
851e67a1 FB |
352 | /* build Task State */ |
353 | memset(ts, 0, sizeof(TaskState)); | |
354 | env->opaque = ts; | |
355 | ts->used = 1; | |
356 | ||
6dbad63e | 357 | /* linux register setup */ |
0ecfa993 FB |
358 | env->regs[R_EAX] = regs->eax; |
359 | env->regs[R_EBX] = regs->ebx; | |
360 | env->regs[R_ECX] = regs->ecx; | |
361 | env->regs[R_EDX] = regs->edx; | |
362 | env->regs[R_ESI] = regs->esi; | |
363 | env->regs[R_EDI] = regs->edi; | |
364 | env->regs[R_EBP] = regs->ebp; | |
365 | env->regs[R_ESP] = regs->esp; | |
dab2ed99 | 366 | env->eip = regs->eip; |
31e31b8a | 367 | |
6dbad63e FB |
368 | /* linux segment setup */ |
369 | env->gdt.base = (void *)gdt_table; | |
370 | env->gdt.limit = sizeof(gdt_table) - 1; | |
371 | write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1); | |
372 | write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1); | |
373 | cpu_x86_load_seg(env, R_CS, __USER_CS); | |
374 | cpu_x86_load_seg(env, R_DS, __USER_DS); | |
375 | cpu_x86_load_seg(env, R_ES, __USER_DS); | |
376 | cpu_x86_load_seg(env, R_SS, __USER_DS); | |
377 | cpu_x86_load_seg(env, R_FS, __USER_DS); | |
378 | cpu_x86_load_seg(env, R_GS, __USER_DS); | |
31e31b8a | 379 | |
1b6b029e FB |
380 | cpu_loop(env); |
381 | /* never exits */ | |
31e31b8a FB |
382 | return 0; |
383 | } |