root / linux-user / main.c @ c50c0c3f
History | View | Annotate | Download (10.5 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 |
void cpu_loop(CPUX86State *env)
|
123 |
{ |
124 |
int trapnr;
|
125 |
uint8_t *pc; |
126 |
target_siginfo_t info; |
127 |
|
128 |
for(;;) {
|
129 |
trapnr = cpu_x86_exec(env); |
130 |
switch(trapnr) {
|
131 |
case EXCP0D_GPF:
|
132 |
if (env->eflags & VM_MASK) {
|
133 |
handle_vm86_fault(env); |
134 |
} else {
|
135 |
pc = env->seg_cache[R_CS].base + env->eip; |
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;
|
151 |
info.si_code = TARGET_SI_KERNEL; |
152 |
info._sifields._sigfault._addr = 0;
|
153 |
queue_signal(info.si_signo, &info); |
154 |
} |
155 |
} |
156 |
break;
|
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;
|
167 |
case EXCP00_DIVZ:
|
168 |
if (env->eflags & VM_MASK) {
|
169 |
do_int(env, trapnr); |
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 |
} |
178 |
break;
|
179 |
case EXCP04_INTO:
|
180 |
case EXCP05_BOUND:
|
181 |
if (env->eflags & VM_MASK) {
|
182 |
do_int(env, trapnr); |
183 |
} else {
|
184 |
info.si_signo = SIGSEGV; |
185 |
info.si_errno = 0;
|
186 |
info.si_code = TARGET_SI_KERNEL; |
187 |
info._sifields._sigfault._addr = 0;
|
188 |
queue_signal(info.si_signo, &info); |
189 |
} |
190 |
break;
|
191 |
case EXCP06_ILLOP:
|
192 |
info.si_signo = SIGILL; |
193 |
info.si_errno = 0;
|
194 |
info.si_code = TARGET_ILL_ILLOPN; |
195 |
info._sifields._sigfault._addr = env->eip; |
196 |
queue_signal(info.si_signo, &info); |
197 |
break;
|
198 |
case EXCP_INTERRUPT:
|
199 |
/* just indicate that signals should be handled asap */
|
200 |
break;
|
201 |
default:
|
202 |
pc = env->seg_cache[R_CS].base + env->eip; |
203 |
fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
|
204 |
(long)pc, trapnr);
|
205 |
abort(); |
206 |
} |
207 |
process_pending_signals(env); |
208 |
} |
209 |
} |
210 |
|
211 |
void usage(void) |
212 |
{ |
213 |
printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
214 |
"usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
|
215 |
"Linux x86 emulator\n"
|
216 |
"\n"
|
217 |
"-h print this help\n"
|
218 |
"-d activate log (logfile=%s)\n"
|
219 |
"-L path set the x86 elf interpreter prefix (default=%s)\n"
|
220 |
"-s size set the x86 stack size in bytes (default=%ld)\n",
|
221 |
DEBUG_LOGFILE, |
222 |
interp_prefix, |
223 |
x86_stack_size); |
224 |
_exit(1);
|
225 |
} |
226 |
|
227 |
/* XXX: currently only used for async signals (see signal.c) */
|
228 |
CPUX86State *global_env; |
229 |
/* used to free thread contexts */
|
230 |
TaskState *first_task_state; |
231 |
|
232 |
int main(int argc, char **argv) |
233 |
{ |
234 |
const char *filename; |
235 |
struct target_pt_regs regs1, *regs = ®s1;
|
236 |
struct image_info info1, *info = &info1;
|
237 |
TaskState ts1, *ts = &ts1; |
238 |
CPUX86State *env; |
239 |
int optind;
|
240 |
const char *r; |
241 |
|
242 |
if (argc <= 1) |
243 |
usage(); |
244 |
|
245 |
loglevel = 0;
|
246 |
optind = 1;
|
247 |
for(;;) {
|
248 |
if (optind >= argc)
|
249 |
break;
|
250 |
r = argv[optind]; |
251 |
if (r[0] != '-') |
252 |
break;
|
253 |
optind++; |
254 |
r++; |
255 |
if (!strcmp(r, "-")) { |
256 |
break;
|
257 |
} else if (!strcmp(r, "d")) { |
258 |
loglevel = 1;
|
259 |
} else if (!strcmp(r, "s")) { |
260 |
r = argv[optind++]; |
261 |
x86_stack_size = strtol(r, (char **)&r, 0); |
262 |
if (x86_stack_size <= 0) |
263 |
usage(); |
264 |
if (*r == 'M') |
265 |
x86_stack_size *= 1024 * 1024; |
266 |
else if (*r == 'k' || *r == 'K') |
267 |
x86_stack_size *= 1024;
|
268 |
} else if (!strcmp(r, "L")) { |
269 |
interp_prefix = argv[optind++]; |
270 |
} else {
|
271 |
usage(); |
272 |
} |
273 |
} |
274 |
if (optind >= argc)
|
275 |
usage(); |
276 |
filename = argv[optind]; |
277 |
|
278 |
/* init debug */
|
279 |
if (loglevel) {
|
280 |
logfile = fopen(DEBUG_LOGFILE, "w");
|
281 |
if (!logfile) {
|
282 |
perror(DEBUG_LOGFILE); |
283 |
_exit(1);
|
284 |
} |
285 |
setvbuf(logfile, NULL, _IOLBF, 0); |
286 |
} |
287 |
|
288 |
/* Zero out regs */
|
289 |
memset(regs, 0, sizeof(struct target_pt_regs)); |
290 |
|
291 |
/* Zero out image_info */
|
292 |
memset(info, 0, sizeof(struct image_info)); |
293 |
|
294 |
/* Scan interp_prefix dir for replacement files. */
|
295 |
init_paths(interp_prefix); |
296 |
|
297 |
if (elf_exec(filename, argv+optind, environ, regs, info) != 0) { |
298 |
printf("Error loading %s\n", filename);
|
299 |
_exit(1);
|
300 |
} |
301 |
|
302 |
if (loglevel) {
|
303 |
fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk);
|
304 |
fprintf(logfile, "end_code 0x%08lx\n" , info->end_code);
|
305 |
fprintf(logfile, "start_code 0x%08lx\n" , info->start_code);
|
306 |
fprintf(logfile, "end_data 0x%08lx\n" , info->end_data);
|
307 |
fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
|
308 |
fprintf(logfile, "brk 0x%08lx\n" , info->brk);
|
309 |
fprintf(logfile, "esp 0x%08lx\n" , regs->esp);
|
310 |
fprintf(logfile, "eip 0x%08lx\n" , regs->eip);
|
311 |
} |
312 |
|
313 |
target_set_brk((char *)info->brk);
|
314 |
syscall_init(); |
315 |
signal_init(); |
316 |
|
317 |
env = cpu_x86_init(); |
318 |
global_env = env; |
319 |
|
320 |
/* build Task State */
|
321 |
memset(ts, 0, sizeof(TaskState)); |
322 |
env->opaque = ts; |
323 |
ts->used = 1;
|
324 |
|
325 |
/* linux register setup */
|
326 |
env->regs[R_EAX] = regs->eax; |
327 |
env->regs[R_EBX] = regs->ebx; |
328 |
env->regs[R_ECX] = regs->ecx; |
329 |
env->regs[R_EDX] = regs->edx; |
330 |
env->regs[R_ESI] = regs->esi; |
331 |
env->regs[R_EDI] = regs->edi; |
332 |
env->regs[R_EBP] = regs->ebp; |
333 |
env->regs[R_ESP] = regs->esp; |
334 |
env->eip = regs->eip; |
335 |
|
336 |
/* linux segment setup */
|
337 |
env->gdt.base = (void *)gdt_table;
|
338 |
env->gdt.limit = sizeof(gdt_table) - 1; |
339 |
write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1); |
340 |
write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1); |
341 |
cpu_x86_load_seg(env, R_CS, __USER_CS); |
342 |
cpu_x86_load_seg(env, R_DS, __USER_DS); |
343 |
cpu_x86_load_seg(env, R_ES, __USER_DS); |
344 |
cpu_x86_load_seg(env, R_SS, __USER_DS); |
345 |
cpu_x86_load_seg(env, R_FS, __USER_DS); |
346 |
cpu_x86_load_seg(env, R_GS, __USER_DS); |
347 |
|
348 |
cpu_loop(env); |
349 |
/* never exits */
|
350 |
return 0; |
351 |
} |