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