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