root / linux-user / main.c @ 982b4315
History | View | Annotate | Download (15.8 kB)
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 |
|
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 |
|
51 |
#endif
|
52 |
|
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; |
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 |
|
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 |
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];
|
121 |
|
122 |
//#define DEBUG_VM86
|
123 |
|
124 |
static inline int is_revectored(int nr, struct target_revectored_struct *bitmap) |
125 |
{ |
126 |
return (tswap32(bitmap->__map[nr >> 5]) >> (nr & 0x1f)) & 1; |
127 |
} |
128 |
|
129 |
static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg) |
130 |
{ |
131 |
return (uint8_t *)((seg << 4) + (reg & 0xffff)); |
132 |
} |
133 |
|
134 |
static inline void pushw(CPUX86State *env, int val) |
135 |
{ |
136 |
env->regs[R_ESP] = (env->regs[R_ESP] & ~0xffff) |
|
137 |
((env->regs[R_ESP] - 2) & 0xffff); |
138 |
*(uint16_t *)seg_to_linear(env->segs[R_SS], env->regs[R_ESP]) = val; |
139 |
} |
140 |
|
141 |
static inline unsigned int get_vflags(CPUX86State *env) |
142 |
{ |
143 |
unsigned int eflags; |
144 |
eflags = env->eflags & ~(VM_MASK | RF_MASK | IF_MASK); |
145 |
if (eflags & VIF_MASK)
|
146 |
eflags |= IF_MASK; |
147 |
return eflags;
|
148 |
} |
149 |
|
150 |
void save_v86_state(CPUX86State *env)
|
151 |
{ |
152 |
TaskState *ts = env->opaque; |
153 |
#ifdef DEBUG_VM86
|
154 |
printf("save_v86_state\n");
|
155 |
#endif
|
156 |
|
157 |
/* put the VM86 registers in the userspace register structure */
|
158 |
ts->target_v86->regs.eax = tswap32(env->regs[R_EAX]); |
159 |
ts->target_v86->regs.ebx = tswap32(env->regs[R_EBX]); |
160 |
ts->target_v86->regs.ecx = tswap32(env->regs[R_ECX]); |
161 |
ts->target_v86->regs.edx = tswap32(env->regs[R_EDX]); |
162 |
ts->target_v86->regs.esi = tswap32(env->regs[R_ESI]); |
163 |
ts->target_v86->regs.edi = tswap32(env->regs[R_EDI]); |
164 |
ts->target_v86->regs.ebp = tswap32(env->regs[R_EBP]); |
165 |
ts->target_v86->regs.esp = tswap32(env->regs[R_ESP]); |
166 |
ts->target_v86->regs.eip = tswap32(env->eip); |
167 |
ts->target_v86->regs.cs = tswap16(env->segs[R_CS]); |
168 |
ts->target_v86->regs.ss = tswap16(env->segs[R_SS]); |
169 |
ts->target_v86->regs.ds = tswap16(env->segs[R_DS]); |
170 |
ts->target_v86->regs.es = tswap16(env->segs[R_ES]); |
171 |
ts->target_v86->regs.fs = tswap16(env->segs[R_FS]); |
172 |
ts->target_v86->regs.gs = tswap16(env->segs[R_GS]); |
173 |
ts->target_v86->regs.eflags = tswap32(env->eflags); |
174 |
|
175 |
/* restore 32 bit registers */
|
176 |
env->regs[R_EAX] = ts->vm86_saved_regs.eax; |
177 |
env->regs[R_EBX] = ts->vm86_saved_regs.ebx; |
178 |
env->regs[R_ECX] = ts->vm86_saved_regs.ecx; |
179 |
env->regs[R_EDX] = ts->vm86_saved_regs.edx; |
180 |
env->regs[R_ESI] = ts->vm86_saved_regs.esi; |
181 |
env->regs[R_EDI] = ts->vm86_saved_regs.edi; |
182 |
env->regs[R_EBP] = ts->vm86_saved_regs.ebp; |
183 |
env->regs[R_ESP] = ts->vm86_saved_regs.esp; |
184 |
env->eflags = ts->vm86_saved_regs.eflags; |
185 |
env->eip = ts->vm86_saved_regs.eip; |
186 |
|
187 |
cpu_x86_load_seg(env, R_CS, ts->vm86_saved_regs.cs); |
188 |
cpu_x86_load_seg(env, R_SS, ts->vm86_saved_regs.ss); |
189 |
cpu_x86_load_seg(env, R_DS, ts->vm86_saved_regs.ds); |
190 |
cpu_x86_load_seg(env, R_ES, ts->vm86_saved_regs.es); |
191 |
cpu_x86_load_seg(env, R_FS, ts->vm86_saved_regs.fs); |
192 |
cpu_x86_load_seg(env, R_GS, ts->vm86_saved_regs.gs); |
193 |
} |
194 |
|
195 |
/* return from vm86 mode to 32 bit. The vm86() syscall will return
|
196 |
'retval' */
|
197 |
static inline void return_to_32bit(CPUX86State *env, int retval) |
198 |
{ |
199 |
#ifdef DEBUG_VM86
|
200 |
printf("return_to_32bit: ret=0x%x\n", retval);
|
201 |
#endif
|
202 |
save_v86_state(env); |
203 |
env->regs[R_EAX] = retval; |
204 |
} |
205 |
|
206 |
/* handle VM86 interrupt (NOTE: the CPU core currently does not
|
207 |
support TSS interrupt revectoring, so this code is always executed) */
|
208 |
static void do_int(CPUX86State *env, int intno) |
209 |
{ |
210 |
TaskState *ts = env->opaque; |
211 |
uint32_t *int_ptr, segoffs; |
212 |
|
213 |
if (env->segs[R_CS] == TARGET_BIOSSEG)
|
214 |
goto cannot_handle; /* XXX: I am not sure this is really useful */ |
215 |
if (is_revectored(intno, &ts->target_v86->int_revectored))
|
216 |
goto cannot_handle;
|
217 |
if (intno == 0x21 && is_revectored((env->regs[R_EAX] >> 8) & 0xff, |
218 |
&ts->target_v86->int21_revectored)) |
219 |
goto cannot_handle;
|
220 |
int_ptr = (uint32_t *)(intno << 2);
|
221 |
segoffs = tswap32(*int_ptr); |
222 |
if ((segoffs >> 16) == TARGET_BIOSSEG) |
223 |
goto cannot_handle;
|
224 |
#ifdef DEBUG_VM86
|
225 |
printf("VM86: emulating int 0x%x. CS:IP=%04x:%04x\n",
|
226 |
intno, segoffs >> 16, segoffs & 0xffff); |
227 |
#endif
|
228 |
/* save old state */
|
229 |
pushw(env, get_vflags(env)); |
230 |
pushw(env, env->segs[R_CS]); |
231 |
pushw(env, env->eip); |
232 |
/* goto interrupt handler */
|
233 |
env->eip = segoffs & 0xffff;
|
234 |
cpu_x86_load_seg(env, R_CS, segoffs >> 16);
|
235 |
env->eflags &= ~(VIF_MASK | TF_MASK); |
236 |
return;
|
237 |
cannot_handle:
|
238 |
#ifdef DEBUG_VM86
|
239 |
printf("VM86: return to 32 bits int 0x%x\n", intno);
|
240 |
#endif
|
241 |
return_to_32bit(env, TARGET_VM86_INTx | (intno << 8));
|
242 |
} |
243 |
|
244 |
void cpu_loop(struct CPUX86State *env) |
245 |
{ |
246 |
int trapnr;
|
247 |
uint8_t *pc; |
248 |
target_siginfo_t info; |
249 |
|
250 |
for(;;) {
|
251 |
trapnr = cpu_x86_exec(env); |
252 |
pc = env->seg_cache[R_CS].base + env->eip; |
253 |
switch(trapnr) {
|
254 |
case EXCP0D_GPF:
|
255 |
if (env->eflags & VM_MASK) {
|
256 |
#ifdef DEBUG_VM86
|
257 |
printf("VM86 exception %04x:%08x %02x %02x\n",
|
258 |
env->segs[R_CS], env->eip, pc[0], pc[1]); |
259 |
#endif
|
260 |
/* VM86 mode */
|
261 |
switch(pc[0]) { |
262 |
case 0xcd: /* int */ |
263 |
env->eip += 2;
|
264 |
do_int(env, pc[1]);
|
265 |
break;
|
266 |
case 0x66: |
267 |
switch(pc[1]) { |
268 |
case 0xfb: /* sti */ |
269 |
case 0x9d: /* popf */ |
270 |
case 0xcf: /* iret */ |
271 |
env->eip += 2;
|
272 |
return_to_32bit(env, TARGET_VM86_STI); |
273 |
break;
|
274 |
default:
|
275 |
goto vm86_gpf;
|
276 |
} |
277 |
break;
|
278 |
case 0xfb: /* sti */ |
279 |
case 0x9d: /* popf */ |
280 |
case 0xcf: /* iret */ |
281 |
env->eip++; |
282 |
return_to_32bit(env, TARGET_VM86_STI); |
283 |
break;
|
284 |
default:
|
285 |
vm86_gpf:
|
286 |
/* real VM86 GPF exception */
|
287 |
return_to_32bit(env, TARGET_VM86_UNKNOWN); |
288 |
break;
|
289 |
} |
290 |
} else {
|
291 |
if (pc[0] == 0xcd && pc[1] == 0x80) { |
292 |
/* syscall */
|
293 |
env->eip += 2;
|
294 |
env->regs[R_EAX] = do_syscall(env, |
295 |
env->regs[R_EAX], |
296 |
env->regs[R_EBX], |
297 |
env->regs[R_ECX], |
298 |
env->regs[R_EDX], |
299 |
env->regs[R_ESI], |
300 |
env->regs[R_EDI], |
301 |
env->regs[R_EBP]); |
302 |
} else {
|
303 |
/* XXX: more precise info */
|
304 |
info.si_signo = SIGSEGV; |
305 |
info.si_errno = 0;
|
306 |
info.si_code = TARGET_SI_KERNEL; |
307 |
info._sifields._sigfault._addr = 0;
|
308 |
queue_signal(info.si_signo, &info); |
309 |
} |
310 |
} |
311 |
break;
|
312 |
case EXCP0E_PAGE:
|
313 |
info.si_signo = SIGSEGV; |
314 |
info.si_errno = 0;
|
315 |
if (!(env->error_code & 1)) |
316 |
info.si_code = TARGET_SEGV_MAPERR; |
317 |
else
|
318 |
info.si_code = TARGET_SEGV_ACCERR; |
319 |
info._sifields._sigfault._addr = env->cr2; |
320 |
queue_signal(info.si_signo, &info); |
321 |
break;
|
322 |
case EXCP00_DIVZ:
|
323 |
if (env->eflags & VM_MASK) {
|
324 |
do_int(env, trapnr); |
325 |
} else {
|
326 |
/* division by zero */
|
327 |
info.si_signo = SIGFPE; |
328 |
info.si_errno = 0;
|
329 |
info.si_code = TARGET_FPE_INTDIV; |
330 |
info._sifields._sigfault._addr = env->eip; |
331 |
queue_signal(info.si_signo, &info); |
332 |
} |
333 |
break;
|
334 |
case EXCP04_INTO:
|
335 |
case EXCP05_BOUND:
|
336 |
if (env->eflags & VM_MASK) {
|
337 |
do_int(env, trapnr); |
338 |
} else {
|
339 |
info.si_signo = SIGSEGV; |
340 |
info.si_errno = 0;
|
341 |
info.si_code = TARGET_SI_KERNEL; |
342 |
info._sifields._sigfault._addr = 0;
|
343 |
queue_signal(info.si_signo, &info); |
344 |
} |
345 |
break;
|
346 |
case EXCP06_ILLOP:
|
347 |
info.si_signo = SIGILL; |
348 |
info.si_errno = 0;
|
349 |
info.si_code = TARGET_ILL_ILLOPN; |
350 |
info._sifields._sigfault._addr = env->eip; |
351 |
queue_signal(info.si_signo, &info); |
352 |
break;
|
353 |
case EXCP_INTERRUPT:
|
354 |
/* just indicate that signals should be handled asap */
|
355 |
break;
|
356 |
default:
|
357 |
fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
|
358 |
(long)pc, trapnr);
|
359 |
abort(); |
360 |
} |
361 |
process_pending_signals(env); |
362 |
} |
363 |
} |
364 |
|
365 |
void usage(void) |
366 |
{ |
367 |
printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
368 |
"usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
|
369 |
"Linux x86 emulator\n"
|
370 |
"\n"
|
371 |
"-h print this help\n"
|
372 |
"-d activate log (logfile=%s)\n"
|
373 |
"-L path set the x86 elf interpreter prefix (default=%s)\n"
|
374 |
"-s size set the x86 stack size in bytes (default=%ld)\n",
|
375 |
DEBUG_LOGFILE, |
376 |
interp_prefix, |
377 |
x86_stack_size); |
378 |
_exit(1);
|
379 |
} |
380 |
|
381 |
/* XXX: currently only used for async signals (see signal.c) */
|
382 |
CPUX86State *global_env; |
383 |
/* used to free thread contexts */
|
384 |
TaskState *first_task_state; |
385 |
|
386 |
int main(int argc, char **argv) |
387 |
{ |
388 |
const char *filename; |
389 |
struct target_pt_regs regs1, *regs = ®s1;
|
390 |
struct image_info info1, *info = &info1;
|
391 |
TaskState ts1, *ts = &ts1; |
392 |
CPUX86State *env; |
393 |
int optind;
|
394 |
const char *r; |
395 |
|
396 |
if (argc <= 1) |
397 |
usage(); |
398 |
|
399 |
loglevel = 0;
|
400 |
optind = 1;
|
401 |
for(;;) {
|
402 |
if (optind >= argc)
|
403 |
break;
|
404 |
r = argv[optind]; |
405 |
if (r[0] != '-') |
406 |
break;
|
407 |
optind++; |
408 |
r++; |
409 |
if (!strcmp(r, "-")) { |
410 |
break;
|
411 |
} else if (!strcmp(r, "d")) { |
412 |
loglevel = 1;
|
413 |
} else if (!strcmp(r, "s")) { |
414 |
r = argv[optind++]; |
415 |
x86_stack_size = strtol(r, (char **)&r, 0); |
416 |
if (x86_stack_size <= 0) |
417 |
usage(); |
418 |
if (*r == 'M') |
419 |
x86_stack_size *= 1024 * 1024; |
420 |
else if (*r == 'k' || *r == 'K') |
421 |
x86_stack_size *= 1024;
|
422 |
} else if (!strcmp(r, "L")) { |
423 |
interp_prefix = argv[optind++]; |
424 |
} else {
|
425 |
usage(); |
426 |
} |
427 |
} |
428 |
if (optind >= argc)
|
429 |
usage(); |
430 |
filename = argv[optind]; |
431 |
|
432 |
/* init debug */
|
433 |
if (loglevel) {
|
434 |
logfile = fopen(DEBUG_LOGFILE, "w");
|
435 |
if (!logfile) {
|
436 |
perror(DEBUG_LOGFILE); |
437 |
_exit(1);
|
438 |
} |
439 |
setvbuf(logfile, NULL, _IOLBF, 0); |
440 |
} |
441 |
|
442 |
/* Zero out regs */
|
443 |
memset(regs, 0, sizeof(struct target_pt_regs)); |
444 |
|
445 |
/* Zero out image_info */
|
446 |
memset(info, 0, sizeof(struct image_info)); |
447 |
|
448 |
/* Scan interp_prefix dir for replacement files. */
|
449 |
init_paths(interp_prefix); |
450 |
|
451 |
if (elf_exec(filename, argv+optind, environ, regs, info) != 0) { |
452 |
printf("Error loading %s\n", filename);
|
453 |
_exit(1);
|
454 |
} |
455 |
|
456 |
if (loglevel) {
|
457 |
fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk);
|
458 |
fprintf(logfile, "end_code 0x%08lx\n" , info->end_code);
|
459 |
fprintf(logfile, "start_code 0x%08lx\n" , info->start_code);
|
460 |
fprintf(logfile, "end_data 0x%08lx\n" , info->end_data);
|
461 |
fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
|
462 |
fprintf(logfile, "brk 0x%08lx\n" , info->brk);
|
463 |
fprintf(logfile, "esp 0x%08lx\n" , regs->esp);
|
464 |
fprintf(logfile, "eip 0x%08lx\n" , regs->eip);
|
465 |
} |
466 |
|
467 |
target_set_brk((char *)info->brk);
|
468 |
syscall_init(); |
469 |
signal_init(); |
470 |
|
471 |
env = cpu_x86_init(); |
472 |
global_env = env; |
473 |
|
474 |
/* build Task State */
|
475 |
memset(ts, 0, sizeof(TaskState)); |
476 |
env->opaque = ts; |
477 |
ts->used = 1;
|
478 |
|
479 |
/* linux register setup */
|
480 |
env->regs[R_EAX] = regs->eax; |
481 |
env->regs[R_EBX] = regs->ebx; |
482 |
env->regs[R_ECX] = regs->ecx; |
483 |
env->regs[R_EDX] = regs->edx; |
484 |
env->regs[R_ESI] = regs->esi; |
485 |
env->regs[R_EDI] = regs->edi; |
486 |
env->regs[R_EBP] = regs->ebp; |
487 |
env->regs[R_ESP] = regs->esp; |
488 |
env->eip = regs->eip; |
489 |
|
490 |
/* linux segment setup */
|
491 |
env->gdt.base = (void *)gdt_table;
|
492 |
env->gdt.limit = sizeof(gdt_table) - 1; |
493 |
write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1); |
494 |
write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1); |
495 |
cpu_x86_load_seg(env, R_CS, __USER_CS); |
496 |
cpu_x86_load_seg(env, R_DS, __USER_DS); |
497 |
cpu_x86_load_seg(env, R_ES, __USER_DS); |
498 |
cpu_x86_load_seg(env, R_SS, __USER_DS); |
499 |
cpu_x86_load_seg(env, R_FS, __USER_DS); |
500 |
cpu_x86_load_seg(env, R_GS, __USER_DS); |
501 |
|
502 |
cpu_loop(env); |
503 |
/* never exits */
|
504 |
return 0; |
505 |
} |