root / linux-user / main.c @ facc68be
History | View | Annotate | Download (14.8 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 | 3ef693a0 | bellard | #define DEBUG_LOGFILE "/tmp/qemu.log" |
30 | 586314f2 | bellard | |
31 | 586314f2 | bellard | FILE *logfile = NULL;
|
32 | 586314f2 | bellard | int loglevel;
|
33 | 74cd30b8 | bellard | static const char *interp_prefix = CONFIG_QEMU_PREFIX; |
34 | 586314f2 | bellard | |
35 | f801f97e | bellard | #ifdef __i386__
|
36 | f801f97e | bellard | /* Force usage of an ELF interpreter even if it is an ELF shared
|
37 | f801f97e | bellard | object ! */
|
38 | f801f97e | bellard | const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2"; |
39 | 4304763b | bellard | #endif
|
40 | 74cd30b8 | bellard | |
41 | 74cd30b8 | bellard | /* for recent libc, we add these dummies symbol which are not declared
|
42 | 74cd30b8 | bellard | when generating a linked object (bug in ld ?) */
|
43 | 74cd30b8 | bellard | #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) |
44 | 74cd30b8 | bellard | long __init_array_start[0]; |
45 | 74cd30b8 | bellard | long __init_array_end[0]; |
46 | 74cd30b8 | bellard | long __fini_array_start[0]; |
47 | 74cd30b8 | bellard | long __fini_array_end[0]; |
48 | 74cd30b8 | bellard | #endif
|
49 | 74cd30b8 | bellard | |
50 | 9de5e440 | bellard | /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
|
51 | 9de5e440 | bellard | we allocate a bigger stack. Need a better solution, for example
|
52 | 9de5e440 | bellard | by remapping the process stack directly at the right place */
|
53 | 9de5e440 | bellard | unsigned long x86_stack_size = 512 * 1024; |
54 | 31e31b8a | bellard | |
55 | 31e31b8a | bellard | void gemu_log(const char *fmt, ...) |
56 | 31e31b8a | bellard | { |
57 | 31e31b8a | bellard | va_list ap; |
58 | 31e31b8a | bellard | |
59 | 31e31b8a | bellard | va_start(ap, fmt); |
60 | 31e31b8a | bellard | vfprintf(stderr, fmt, ap); |
61 | 31e31b8a | bellard | va_end(ap); |
62 | 31e31b8a | bellard | } |
63 | 31e31b8a | bellard | |
64 | b346ff46 | bellard | #ifdef TARGET_I386
|
65 | 31e31b8a | bellard | /***********************************************************/
|
66 | 0ecfa993 | bellard | /* CPUX86 core interface */
|
67 | 367e86e8 | bellard | |
68 | b689bc57 | bellard | void cpu_x86_outb(CPUX86State *env, int addr, int val) |
69 | 367e86e8 | bellard | { |
70 | 367e86e8 | bellard | fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
|
71 | 367e86e8 | bellard | } |
72 | 367e86e8 | bellard | |
73 | b689bc57 | bellard | void cpu_x86_outw(CPUX86State *env, int addr, int val) |
74 | 367e86e8 | bellard | { |
75 | 367e86e8 | bellard | fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
|
76 | 367e86e8 | bellard | } |
77 | 367e86e8 | bellard | |
78 | b689bc57 | bellard | void cpu_x86_outl(CPUX86State *env, int addr, int val) |
79 | 367e86e8 | bellard | { |
80 | 367e86e8 | bellard | fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
|
81 | 367e86e8 | bellard | } |
82 | 367e86e8 | bellard | |
83 | b689bc57 | bellard | int cpu_x86_inb(CPUX86State *env, int addr) |
84 | 367e86e8 | bellard | { |
85 | 367e86e8 | bellard | fprintf(stderr, "inb: port=0x%04x\n", addr);
|
86 | 367e86e8 | bellard | return 0; |
87 | 367e86e8 | bellard | } |
88 | 367e86e8 | bellard | |
89 | b689bc57 | bellard | int cpu_x86_inw(CPUX86State *env, int addr) |
90 | 367e86e8 | bellard | { |
91 | 367e86e8 | bellard | fprintf(stderr, "inw: port=0x%04x\n", addr);
|
92 | 367e86e8 | bellard | return 0; |
93 | 367e86e8 | bellard | } |
94 | 367e86e8 | bellard | |
95 | b689bc57 | bellard | int cpu_x86_inl(CPUX86State *env, int addr) |
96 | 367e86e8 | bellard | { |
97 | 367e86e8 | bellard | fprintf(stderr, "inl: port=0x%04x\n", addr);
|
98 | 367e86e8 | bellard | return 0; |
99 | 367e86e8 | bellard | } |
100 | 367e86e8 | bellard | |
101 | 92ccca6a | bellard | int cpu_x86_get_pic_interrupt(CPUX86State *env)
|
102 | 92ccca6a | bellard | { |
103 | 92ccca6a | bellard | return -1; |
104 | 92ccca6a | bellard | } |
105 | 92ccca6a | bellard | |
106 | f4beb510 | bellard | static void write_dt(void *ptr, unsigned long addr, unsigned long limit, |
107 | f4beb510 | bellard | int flags)
|
108 | 6dbad63e | bellard | { |
109 | f4beb510 | bellard | unsigned int e1, e2; |
110 | 6dbad63e | bellard | e1 = (addr << 16) | (limit & 0xffff); |
111 | 6dbad63e | bellard | e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); |
112 | f4beb510 | bellard | e2 |= flags; |
113 | f4beb510 | bellard | stl((uint8_t *)ptr, e1); |
114 | f4beb510 | bellard | stl((uint8_t *)ptr + 4, e2);
|
115 | f4beb510 | bellard | } |
116 | f4beb510 | bellard | |
117 | f4beb510 | bellard | static void set_gate(void *ptr, unsigned int type, unsigned int dpl, |
118 | f4beb510 | bellard | unsigned long addr, unsigned int sel) |
119 | f4beb510 | bellard | { |
120 | f4beb510 | bellard | unsigned int e1, e2; |
121 | f4beb510 | bellard | e1 = (addr & 0xffff) | (sel << 16); |
122 | f4beb510 | bellard | e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); |
123 | 6dbad63e | bellard | stl((uint8_t *)ptr, e1); |
124 | 6dbad63e | bellard | stl((uint8_t *)ptr + 4, e2);
|
125 | 6dbad63e | bellard | } |
126 | 6dbad63e | bellard | |
127 | 6dbad63e | bellard | uint64_t gdt_table[6];
|
128 | f4beb510 | bellard | uint64_t idt_table[256];
|
129 | f4beb510 | bellard | |
130 | f4beb510 | bellard | /* only dpl matters as we do only user space emulation */
|
131 | f4beb510 | bellard | static void set_idt(int n, unsigned int dpl) |
132 | f4beb510 | bellard | { |
133 | f4beb510 | bellard | set_gate(idt_table + n, 0, dpl, 0, 0); |
134 | f4beb510 | bellard | } |
135 | 31e31b8a | bellard | |
136 | 89e957e7 | bellard | void cpu_loop(CPUX86State *env)
|
137 | 1b6b029e | bellard | { |
138 | bc8a22cc | bellard | int trapnr;
|
139 | 9de5e440 | bellard | uint8_t *pc; |
140 | 9de5e440 | bellard | target_siginfo_t info; |
141 | 851e67a1 | bellard | |
142 | 1b6b029e | bellard | for(;;) {
|
143 | bc8a22cc | bellard | trapnr = cpu_x86_exec(env); |
144 | bc8a22cc | bellard | switch(trapnr) {
|
145 | f4beb510 | bellard | case 0x80: |
146 | f4beb510 | bellard | /* linux syscall */
|
147 | f4beb510 | bellard | env->regs[R_EAX] = do_syscall(env, |
148 | f4beb510 | bellard | env->regs[R_EAX], |
149 | f4beb510 | bellard | env->regs[R_EBX], |
150 | f4beb510 | bellard | env->regs[R_ECX], |
151 | f4beb510 | bellard | env->regs[R_EDX], |
152 | f4beb510 | bellard | env->regs[R_ESI], |
153 | f4beb510 | bellard | env->regs[R_EDI], |
154 | f4beb510 | bellard | env->regs[R_EBP]); |
155 | f4beb510 | bellard | break;
|
156 | f4beb510 | bellard | case EXCP0B_NOSEG:
|
157 | f4beb510 | bellard | case EXCP0C_STACK:
|
158 | f4beb510 | bellard | info.si_signo = SIGBUS; |
159 | f4beb510 | bellard | info.si_errno = 0;
|
160 | f4beb510 | bellard | info.si_code = TARGET_SI_KERNEL; |
161 | f4beb510 | bellard | info._sifields._sigfault._addr = 0;
|
162 | f4beb510 | bellard | queue_signal(info.si_signo, &info); |
163 | f4beb510 | bellard | break;
|
164 | 1b6b029e | bellard | case EXCP0D_GPF:
|
165 | 851e67a1 | bellard | if (env->eflags & VM_MASK) {
|
166 | 89e957e7 | bellard | handle_vm86_fault(env); |
167 | 1b6b029e | bellard | } else {
|
168 | f4beb510 | bellard | info.si_signo = SIGSEGV; |
169 | f4beb510 | bellard | info.si_errno = 0;
|
170 | f4beb510 | bellard | info.si_code = TARGET_SI_KERNEL; |
171 | f4beb510 | bellard | info._sifields._sigfault._addr = 0;
|
172 | f4beb510 | bellard | queue_signal(info.si_signo, &info); |
173 | 1b6b029e | bellard | } |
174 | 1b6b029e | bellard | break;
|
175 | b689bc57 | bellard | case EXCP0E_PAGE:
|
176 | b689bc57 | bellard | info.si_signo = SIGSEGV; |
177 | b689bc57 | bellard | info.si_errno = 0;
|
178 | b689bc57 | bellard | if (!(env->error_code & 1)) |
179 | b689bc57 | bellard | info.si_code = TARGET_SEGV_MAPERR; |
180 | b689bc57 | bellard | else
|
181 | b689bc57 | bellard | info.si_code = TARGET_SEGV_ACCERR; |
182 | 970a87a6 | bellard | info._sifields._sigfault._addr = env->cr[2];
|
183 | b689bc57 | bellard | queue_signal(info.si_signo, &info); |
184 | b689bc57 | bellard | break;
|
185 | 9de5e440 | bellard | case EXCP00_DIVZ:
|
186 | bc8a22cc | bellard | if (env->eflags & VM_MASK) {
|
187 | 447db213 | bellard | handle_vm86_trap(env, trapnr); |
188 | bc8a22cc | bellard | } else {
|
189 | bc8a22cc | bellard | /* division by zero */
|
190 | bc8a22cc | bellard | info.si_signo = SIGFPE; |
191 | bc8a22cc | bellard | info.si_errno = 0;
|
192 | bc8a22cc | bellard | info.si_code = TARGET_FPE_INTDIV; |
193 | bc8a22cc | bellard | info._sifields._sigfault._addr = env->eip; |
194 | bc8a22cc | bellard | queue_signal(info.si_signo, &info); |
195 | bc8a22cc | bellard | } |
196 | 9de5e440 | bellard | break;
|
197 | 447db213 | bellard | case EXCP01_SSTP:
|
198 | 447db213 | bellard | case EXCP03_INT3:
|
199 | 447db213 | bellard | if (env->eflags & VM_MASK) {
|
200 | 447db213 | bellard | handle_vm86_trap(env, trapnr); |
201 | 447db213 | bellard | } else {
|
202 | 447db213 | bellard | info.si_signo = SIGTRAP; |
203 | 447db213 | bellard | info.si_errno = 0;
|
204 | 447db213 | bellard | if (trapnr == EXCP01_SSTP) {
|
205 | 447db213 | bellard | info.si_code = TARGET_TRAP_BRKPT; |
206 | 447db213 | bellard | info._sifields._sigfault._addr = env->eip; |
207 | 447db213 | bellard | } else {
|
208 | 447db213 | bellard | info.si_code = TARGET_SI_KERNEL; |
209 | 447db213 | bellard | info._sifields._sigfault._addr = 0;
|
210 | 447db213 | bellard | } |
211 | 447db213 | bellard | queue_signal(info.si_signo, &info); |
212 | 447db213 | bellard | } |
213 | 447db213 | bellard | break;
|
214 | 9de5e440 | bellard | case EXCP04_INTO:
|
215 | 9de5e440 | bellard | case EXCP05_BOUND:
|
216 | bc8a22cc | bellard | if (env->eflags & VM_MASK) {
|
217 | 447db213 | bellard | handle_vm86_trap(env, trapnr); |
218 | bc8a22cc | bellard | } else {
|
219 | bc8a22cc | bellard | info.si_signo = SIGSEGV; |
220 | bc8a22cc | bellard | info.si_errno = 0;
|
221 | b689bc57 | bellard | info.si_code = TARGET_SI_KERNEL; |
222 | bc8a22cc | bellard | info._sifields._sigfault._addr = 0;
|
223 | bc8a22cc | bellard | queue_signal(info.si_signo, &info); |
224 | bc8a22cc | bellard | } |
225 | 9de5e440 | bellard | break;
|
226 | 9de5e440 | bellard | case EXCP06_ILLOP:
|
227 | 9de5e440 | bellard | info.si_signo = SIGILL; |
228 | 9de5e440 | bellard | info.si_errno = 0;
|
229 | 9de5e440 | bellard | info.si_code = TARGET_ILL_ILLOPN; |
230 | 9de5e440 | bellard | info._sifields._sigfault._addr = env->eip; |
231 | 9de5e440 | bellard | queue_signal(info.si_signo, &info); |
232 | 9de5e440 | bellard | break;
|
233 | 9de5e440 | bellard | case EXCP_INTERRUPT:
|
234 | 9de5e440 | bellard | /* just indicate that signals should be handled asap */
|
235 | 9de5e440 | bellard | break;
|
236 | 1b6b029e | bellard | default:
|
237 | 970a87a6 | bellard | pc = env->segs[R_CS].base + env->eip; |
238 | bc8a22cc | bellard | fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
|
239 | bc8a22cc | bellard | (long)pc, trapnr);
|
240 | 1b6b029e | bellard | abort(); |
241 | 1b6b029e | bellard | } |
242 | 66fb9763 | bellard | process_pending_signals(env); |
243 | 1b6b029e | bellard | } |
244 | 1b6b029e | bellard | } |
245 | b346ff46 | bellard | #endif
|
246 | b346ff46 | bellard | |
247 | b346ff46 | bellard | #ifdef TARGET_ARM
|
248 | b346ff46 | bellard | |
249 | b346ff46 | bellard | void cpu_loop(CPUARMState *env)
|
250 | b346ff46 | bellard | { |
251 | b346ff46 | bellard | int trapnr;
|
252 | b346ff46 | bellard | unsigned int n, insn; |
253 | b346ff46 | bellard | target_siginfo_t info; |
254 | b346ff46 | bellard | |
255 | b346ff46 | bellard | for(;;) {
|
256 | b346ff46 | bellard | trapnr = cpu_arm_exec(env); |
257 | b346ff46 | bellard | switch(trapnr) {
|
258 | b346ff46 | bellard | case EXCP_UDEF:
|
259 | b346ff46 | bellard | info.si_signo = SIGILL; |
260 | b346ff46 | bellard | info.si_errno = 0;
|
261 | b346ff46 | bellard | info.si_code = TARGET_ILL_ILLOPN; |
262 | b346ff46 | bellard | info._sifields._sigfault._addr = env->regs[15];
|
263 | b346ff46 | bellard | queue_signal(info.si_signo, &info); |
264 | b346ff46 | bellard | break;
|
265 | b346ff46 | bellard | case EXCP_SWI:
|
266 | b346ff46 | bellard | { |
267 | b346ff46 | bellard | /* system call */
|
268 | b346ff46 | bellard | insn = ldl((void *)(env->regs[15] - 4)); |
269 | b346ff46 | bellard | n = insn & 0xffffff;
|
270 | b346ff46 | bellard | if (n >= ARM_SYSCALL_BASE) {
|
271 | b346ff46 | bellard | /* linux syscall */
|
272 | b346ff46 | bellard | n -= ARM_SYSCALL_BASE; |
273 | b346ff46 | bellard | env->regs[0] = do_syscall(env,
|
274 | b346ff46 | bellard | n, |
275 | b346ff46 | bellard | env->regs[0],
|
276 | b346ff46 | bellard | env->regs[1],
|
277 | b346ff46 | bellard | env->regs[2],
|
278 | b346ff46 | bellard | env->regs[3],
|
279 | b346ff46 | bellard | env->regs[4],
|
280 | b346ff46 | bellard | 0);
|
281 | b346ff46 | bellard | } else {
|
282 | b346ff46 | bellard | goto error;
|
283 | b346ff46 | bellard | } |
284 | b346ff46 | bellard | } |
285 | b346ff46 | bellard | break;
|
286 | 43fff238 | bellard | case EXCP_INTERRUPT:
|
287 | 43fff238 | bellard | /* just indicate that signals should be handled asap */
|
288 | 43fff238 | bellard | break;
|
289 | b346ff46 | bellard | default:
|
290 | b346ff46 | bellard | error:
|
291 | b346ff46 | bellard | fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
|
292 | b346ff46 | bellard | trapnr); |
293 | b346ff46 | bellard | cpu_arm_dump_state(env, stderr, 0);
|
294 | b346ff46 | bellard | abort(); |
295 | b346ff46 | bellard | } |
296 | b346ff46 | bellard | process_pending_signals(env); |
297 | b346ff46 | bellard | } |
298 | b346ff46 | bellard | } |
299 | b346ff46 | bellard | |
300 | b346ff46 | bellard | #endif
|
301 | 1b6b029e | bellard | |
302 | 31e31b8a | bellard | void usage(void) |
303 | 31e31b8a | bellard | { |
304 | 3ef693a0 | bellard | printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
305 | d691f669 | bellard | "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
|
306 | b346ff46 | bellard | "Linux CPU emulator (compiled for %s emulation)\n"
|
307 | d691f669 | bellard | "\n"
|
308 | 54936004 | bellard | "-h print this help\n"
|
309 | b346ff46 | bellard | "-L path set the elf interpreter prefix (default=%s)\n"
|
310 | b346ff46 | bellard | "-s size set the stack size in bytes (default=%ld)\n"
|
311 | 54936004 | bellard | "\n"
|
312 | 54936004 | bellard | "debug options:\n"
|
313 | 54936004 | bellard | "-d activate log (logfile=%s)\n"
|
314 | 54936004 | bellard | "-p pagesize set the host page size to 'pagesize'\n",
|
315 | b346ff46 | bellard | TARGET_ARCH, |
316 | d691f669 | bellard | interp_prefix, |
317 | 54936004 | bellard | x86_stack_size, |
318 | 54936004 | bellard | DEBUG_LOGFILE); |
319 | 74cd30b8 | bellard | _exit(1);
|
320 | 31e31b8a | bellard | } |
321 | 31e31b8a | bellard | |
322 | 9de5e440 | bellard | /* XXX: currently only used for async signals (see signal.c) */
|
323 | b346ff46 | bellard | CPUState *global_env; |
324 | 59faf6d6 | bellard | /* used only if single thread */
|
325 | 59faf6d6 | bellard | CPUState *cpu_single_env = NULL;
|
326 | 59faf6d6 | bellard | |
327 | 851e67a1 | bellard | /* used to free thread contexts */
|
328 | 851e67a1 | bellard | TaskState *first_task_state; |
329 | 9de5e440 | bellard | |
330 | 31e31b8a | bellard | int main(int argc, char **argv) |
331 | 31e31b8a | bellard | { |
332 | 31e31b8a | bellard | const char *filename; |
333 | 01ffc75b | bellard | struct target_pt_regs regs1, *regs = ®s1;
|
334 | 31e31b8a | bellard | struct image_info info1, *info = &info1;
|
335 | 851e67a1 | bellard | TaskState ts1, *ts = &ts1; |
336 | b346ff46 | bellard | CPUState *env; |
337 | 586314f2 | bellard | int optind;
|
338 | d691f669 | bellard | const char *r; |
339 | d691f669 | bellard | |
340 | 31e31b8a | bellard | if (argc <= 1) |
341 | 31e31b8a | bellard | usage(); |
342 | f801f97e | bellard | |
343 | 586314f2 | bellard | loglevel = 0;
|
344 | 586314f2 | bellard | optind = 1;
|
345 | d691f669 | bellard | for(;;) {
|
346 | d691f669 | bellard | if (optind >= argc)
|
347 | d691f669 | bellard | break;
|
348 | d691f669 | bellard | r = argv[optind]; |
349 | d691f669 | bellard | if (r[0] != '-') |
350 | d691f669 | bellard | break;
|
351 | 586314f2 | bellard | optind++; |
352 | d691f669 | bellard | r++; |
353 | d691f669 | bellard | if (!strcmp(r, "-")) { |
354 | d691f669 | bellard | break;
|
355 | d691f669 | bellard | } else if (!strcmp(r, "d")) { |
356 | d691f669 | bellard | loglevel = 1;
|
357 | d691f669 | bellard | } else if (!strcmp(r, "s")) { |
358 | d691f669 | bellard | r = argv[optind++]; |
359 | d691f669 | bellard | x86_stack_size = strtol(r, (char **)&r, 0); |
360 | d691f669 | bellard | if (x86_stack_size <= 0) |
361 | d691f669 | bellard | usage(); |
362 | d691f669 | bellard | if (*r == 'M') |
363 | d691f669 | bellard | x86_stack_size *= 1024 * 1024; |
364 | d691f669 | bellard | else if (*r == 'k' || *r == 'K') |
365 | d691f669 | bellard | x86_stack_size *= 1024;
|
366 | d691f669 | bellard | } else if (!strcmp(r, "L")) { |
367 | d691f669 | bellard | interp_prefix = argv[optind++]; |
368 | 54936004 | bellard | } else if (!strcmp(r, "p")) { |
369 | 54936004 | bellard | host_page_size = atoi(argv[optind++]); |
370 | 54936004 | bellard | if (host_page_size == 0 || |
371 | 54936004 | bellard | (host_page_size & (host_page_size - 1)) != 0) { |
372 | 54936004 | bellard | fprintf(stderr, "page size must be a power of two\n");
|
373 | 54936004 | bellard | exit(1);
|
374 | 54936004 | bellard | } |
375 | d691f669 | bellard | } else {
|
376 | d691f669 | bellard | usage(); |
377 | d691f669 | bellard | } |
378 | 586314f2 | bellard | } |
379 | d691f669 | bellard | if (optind >= argc)
|
380 | d691f669 | bellard | usage(); |
381 | 586314f2 | bellard | filename = argv[optind]; |
382 | 586314f2 | bellard | |
383 | 586314f2 | bellard | /* init debug */
|
384 | 586314f2 | bellard | if (loglevel) {
|
385 | 586314f2 | bellard | logfile = fopen(DEBUG_LOGFILE, "w");
|
386 | 586314f2 | bellard | if (!logfile) {
|
387 | 586314f2 | bellard | perror(DEBUG_LOGFILE); |
388 | 74cd30b8 | bellard | _exit(1);
|
389 | 586314f2 | bellard | } |
390 | 586314f2 | bellard | setvbuf(logfile, NULL, _IOLBF, 0); |
391 | 586314f2 | bellard | } |
392 | 31e31b8a | bellard | |
393 | 31e31b8a | bellard | /* Zero out regs */
|
394 | 01ffc75b | bellard | memset(regs, 0, sizeof(struct target_pt_regs)); |
395 | 31e31b8a | bellard | |
396 | 31e31b8a | bellard | /* Zero out image_info */
|
397 | 31e31b8a | bellard | memset(info, 0, sizeof(struct image_info)); |
398 | 31e31b8a | bellard | |
399 | 74cd30b8 | bellard | /* Scan interp_prefix dir for replacement files. */
|
400 | 74cd30b8 | bellard | init_paths(interp_prefix); |
401 | 74cd30b8 | bellard | |
402 | 54936004 | bellard | /* NOTE: we need to init the CPU at this stage to get the
|
403 | 54936004 | bellard | host_page_size */
|
404 | b346ff46 | bellard | env = cpu_init(); |
405 | 92ccca6a | bellard | |
406 | 74cd30b8 | bellard | if (elf_exec(filename, argv+optind, environ, regs, info) != 0) { |
407 | 31e31b8a | bellard | printf("Error loading %s\n", filename);
|
408 | 74cd30b8 | bellard | _exit(1);
|
409 | 31e31b8a | bellard | } |
410 | 31e31b8a | bellard | |
411 | 4b74fe1f | bellard | if (loglevel) {
|
412 | 54936004 | bellard | page_dump(logfile); |
413 | 54936004 | bellard | |
414 | 4b74fe1f | bellard | fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk);
|
415 | 4b74fe1f | bellard | fprintf(logfile, "end_code 0x%08lx\n" , info->end_code);
|
416 | 4b74fe1f | bellard | fprintf(logfile, "start_code 0x%08lx\n" , info->start_code);
|
417 | 4b74fe1f | bellard | fprintf(logfile, "end_data 0x%08lx\n" , info->end_data);
|
418 | 4b74fe1f | bellard | fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
|
419 | 4b74fe1f | bellard | fprintf(logfile, "brk 0x%08lx\n" , info->brk);
|
420 | b346ff46 | bellard | fprintf(logfile, "entry 0x%08lx\n" , info->entry);
|
421 | 4b74fe1f | bellard | } |
422 | 31e31b8a | bellard | |
423 | 31e31b8a | bellard | target_set_brk((char *)info->brk);
|
424 | 31e31b8a | bellard | syscall_init(); |
425 | 66fb9763 | bellard | signal_init(); |
426 | 31e31b8a | bellard | |
427 | 9de5e440 | bellard | global_env = env; |
428 | 0ecfa993 | bellard | |
429 | 851e67a1 | bellard | /* build Task State */
|
430 | 851e67a1 | bellard | memset(ts, 0, sizeof(TaskState)); |
431 | 851e67a1 | bellard | env->opaque = ts; |
432 | 851e67a1 | bellard | ts->used = 1;
|
433 | 59faf6d6 | bellard | env->user_mode_only = 1;
|
434 | 851e67a1 | bellard | |
435 | b346ff46 | bellard | #if defined(TARGET_I386)
|
436 | 2e255c6b | bellard | cpu_x86_set_cpl(env, 3);
|
437 | 2e255c6b | bellard | |
438 | 3802ce26 | bellard | env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
|
439 | 3802ce26 | bellard | |
440 | 6dbad63e | bellard | /* linux register setup */
|
441 | 0ecfa993 | bellard | env->regs[R_EAX] = regs->eax; |
442 | 0ecfa993 | bellard | env->regs[R_EBX] = regs->ebx; |
443 | 0ecfa993 | bellard | env->regs[R_ECX] = regs->ecx; |
444 | 0ecfa993 | bellard | env->regs[R_EDX] = regs->edx; |
445 | 0ecfa993 | bellard | env->regs[R_ESI] = regs->esi; |
446 | 0ecfa993 | bellard | env->regs[R_EDI] = regs->edi; |
447 | 0ecfa993 | bellard | env->regs[R_EBP] = regs->ebp; |
448 | 0ecfa993 | bellard | env->regs[R_ESP] = regs->esp; |
449 | dab2ed99 | bellard | env->eip = regs->eip; |
450 | 31e31b8a | bellard | |
451 | f4beb510 | bellard | /* linux interrupt setup */
|
452 | f4beb510 | bellard | env->idt.base = (void *)idt_table;
|
453 | f4beb510 | bellard | env->idt.limit = sizeof(idt_table) - 1; |
454 | f4beb510 | bellard | set_idt(0, 0); |
455 | f4beb510 | bellard | set_idt(1, 0); |
456 | f4beb510 | bellard | set_idt(2, 0); |
457 | f4beb510 | bellard | set_idt(3, 3); |
458 | f4beb510 | bellard | set_idt(4, 3); |
459 | f4beb510 | bellard | set_idt(5, 3); |
460 | f4beb510 | bellard | set_idt(6, 0); |
461 | f4beb510 | bellard | set_idt(7, 0); |
462 | f4beb510 | bellard | set_idt(8, 0); |
463 | f4beb510 | bellard | set_idt(9, 0); |
464 | f4beb510 | bellard | set_idt(10, 0); |
465 | f4beb510 | bellard | set_idt(11, 0); |
466 | f4beb510 | bellard | set_idt(12, 0); |
467 | f4beb510 | bellard | set_idt(13, 0); |
468 | f4beb510 | bellard | set_idt(14, 0); |
469 | f4beb510 | bellard | set_idt(15, 0); |
470 | f4beb510 | bellard | set_idt(16, 0); |
471 | f4beb510 | bellard | set_idt(17, 0); |
472 | f4beb510 | bellard | set_idt(18, 0); |
473 | f4beb510 | bellard | set_idt(19, 0); |
474 | f4beb510 | bellard | set_idt(0x80, 3); |
475 | f4beb510 | bellard | |
476 | 6dbad63e | bellard | /* linux segment setup */
|
477 | 6dbad63e | bellard | env->gdt.base = (void *)gdt_table;
|
478 | 6dbad63e | bellard | env->gdt.limit = sizeof(gdt_table) - 1; |
479 | f4beb510 | bellard | write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, |
480 | f4beb510 | bellard | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | |
481 | f4beb510 | bellard | (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); |
482 | f4beb510 | bellard | write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff, |
483 | f4beb510 | bellard | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | |
484 | f4beb510 | bellard | (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT)); |
485 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_CS, __USER_CS); |
486 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_DS, __USER_DS); |
487 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_ES, __USER_DS); |
488 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_SS, __USER_DS); |
489 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_FS, __USER_DS); |
490 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_GS, __USER_DS); |
491 | 92ccca6a | bellard | |
492 | b346ff46 | bellard | #elif defined(TARGET_ARM)
|
493 | b346ff46 | bellard | { |
494 | b346ff46 | bellard | int i;
|
495 | b346ff46 | bellard | for(i = 0; i < 16; i++) { |
496 | b346ff46 | bellard | env->regs[i] = regs->uregs[i]; |
497 | b346ff46 | bellard | } |
498 | b346ff46 | bellard | env->cpsr = regs->uregs[16];
|
499 | b346ff46 | bellard | } |
500 | b346ff46 | bellard | #else
|
501 | b346ff46 | bellard | #error unsupported target CPU
|
502 | b346ff46 | bellard | #endif
|
503 | 31e31b8a | bellard | |
504 | 1b6b029e | bellard | cpu_loop(env); |
505 | 1b6b029e | bellard | /* never exits */
|
506 | 31e31b8a | bellard | return 0; |
507 | 31e31b8a | bellard | } |