]>
git.proxmox.com Git - mirror_qemu.git/blob - linux-user/main.c
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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.
31 #define DEBUG_LOGFILE "/tmp/qemu.log"
35 static const char *interp_prefix
= CONFIG_QEMU_PREFIX
;
38 /* Force usage of an ELF interpreter even if it is an ELF shared
40 const char interp
[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
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];
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;
58 void gemu_log(const char *fmt
, ...)
63 vfprintf(stderr
, fmt
, ap
);
67 /***********************************************************/
68 /* CPUX86 core interface */
70 void cpu_x86_outb(CPUX86State
*env
, int addr
, int val
)
72 fprintf(stderr
, "outb: port=0x%04x, data=%02x\n", addr
, val
);
75 void cpu_x86_outw(CPUX86State
*env
, int addr
, int val
)
77 fprintf(stderr
, "outw: port=0x%04x, data=%04x\n", addr
, val
);
80 void cpu_x86_outl(CPUX86State
*env
, int addr
, int val
)
82 fprintf(stderr
, "outl: port=0x%04x, data=%08x\n", addr
, val
);
85 int cpu_x86_inb(CPUX86State
*env
, int addr
)
87 fprintf(stderr
, "inb: port=0x%04x\n", addr
);
91 int cpu_x86_inw(CPUX86State
*env
, int addr
)
93 fprintf(stderr
, "inw: port=0x%04x\n", addr
);
97 int cpu_x86_inl(CPUX86State
*env
, int addr
)
99 fprintf(stderr
, "inl: port=0x%04x\n", addr
);
103 void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
106 unsigned int e1
, e2
, limit_in_pages
;
108 if (limit
> 0xffff) {
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
);
120 uint64_t gdt_table
[6];
124 static inline int is_revectored(int nr
, struct target_revectored_struct
*bitmap
)
126 return (tswap32(bitmap
->__map
[nr
>> 5]) >> (nr
& 0x1f)) & 1;
129 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
131 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
134 static inline void pushw(CPUX86State
*env
, int val
)
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
;
141 static inline unsigned int get_vflags(CPUX86State
*env
)
144 eflags
= env
->eflags
& ~(VM_MASK
| RF_MASK
| IF_MASK
);
145 if (eflags
& VIF_MASK
)
150 void save_v86_state(CPUX86State
*env
)
152 TaskState
*ts
= env
->opaque
;
154 printf("save_v86_state\n");
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
);
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
;
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
);
195 /* return from vm86 mode to 32 bit. The vm86() syscall will return
197 static inline void return_to_32bit(CPUX86State
*env
, int retval
)
200 printf("return_to_32bit: ret=0x%x\n", retval
);
203 env
->regs
[R_EAX
] = retval
;
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
)
210 TaskState
*ts
= env
->opaque
;
211 uint32_t *int_ptr
, segoffs
;
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
))
217 if (intno
== 0x21 && is_revectored((env
->regs
[R_EAX
] >> 8) & 0xff,
218 &ts
->target_v86
->int21_revectored
))
220 int_ptr
= (uint32_t *)(intno
<< 2);
221 segoffs
= tswap32(*int_ptr
);
222 if ((segoffs
>> 16) == TARGET_BIOSSEG
)
225 printf("VM86: emulating int 0x%x. CS:IP=%04x:%04x\n",
226 intno
, segoffs
>> 16, segoffs
& 0xffff);
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
);
239 printf("VM86: return to 32 bits int 0x%x\n", intno
);
241 return_to_32bit(env
, TARGET_VM86_INTx
| (intno
<< 8));
244 void cpu_loop(struct CPUX86State
*env
)
248 target_siginfo_t info
;
251 trapnr
= cpu_x86_exec(env
);
252 pc
= env
->seg_cache
[R_CS
].base
+ env
->eip
;
255 if (env
->eflags
& VM_MASK
) {
257 printf("VM86 exception %04x:%08x %02x %02x\n",
258 env
->segs
[R_CS
], env
->eip
, pc
[0], pc
[1]);
269 case 0x9d: /* popf */
270 case 0xcf: /* iret */
272 return_to_32bit(env
, TARGET_VM86_STI
);
279 case 0x9d: /* popf */
280 case 0xcf: /* iret */
282 return_to_32bit(env
, TARGET_VM86_STI
);
286 /* real VM86 GPF exception */
287 return_to_32bit(env
, TARGET_VM86_UNKNOWN
);
291 if (pc
[0] == 0xcd && pc
[1] == 0x80) {
294 env
->regs
[R_EAX
] = do_syscall(env
,
303 /* XXX: more precise info */
304 info
.si_signo
= SIGSEGV
;
306 info
.si_code
= TARGET_SI_KERNEL
;
307 info
._sifields
._sigfault
._addr
= 0;
308 queue_signal(info
.si_signo
, &info
);
313 info
.si_signo
= SIGSEGV
;
315 if (!(env
->error_code
& 1))
316 info
.si_code
= TARGET_SEGV_MAPERR
;
318 info
.si_code
= TARGET_SEGV_ACCERR
;
319 info
._sifields
._sigfault
._addr
= env
->cr2
;
320 queue_signal(info
.si_signo
, &info
);
323 if (env
->eflags
& VM_MASK
) {
326 /* division by zero */
327 info
.si_signo
= SIGFPE
;
329 info
.si_code
= TARGET_FPE_INTDIV
;
330 info
._sifields
._sigfault
._addr
= env
->eip
;
331 queue_signal(info
.si_signo
, &info
);
336 if (env
->eflags
& VM_MASK
) {
339 info
.si_signo
= SIGSEGV
;
341 info
.si_code
= TARGET_SI_KERNEL
;
342 info
._sifields
._sigfault
._addr
= 0;
343 queue_signal(info
.si_signo
, &info
);
347 info
.si_signo
= SIGILL
;
349 info
.si_code
= TARGET_ILL_ILLOPN
;
350 info
._sifields
._sigfault
._addr
= env
->eip
;
351 queue_signal(info
.si_signo
, &info
);
354 /* just indicate that signals should be handled asap */
357 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
361 process_pending_signals(env
);
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"
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",
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
;
386 int main(int argc
, char **argv
)
388 const char *filename
;
389 struct target_pt_regs regs1
, *regs
= ®s1
;
390 struct image_info info1
, *info
= &info1
;
391 TaskState ts1
, *ts
= &ts1
;
409 if (!strcmp(r
, "-")) {
411 } else if (!strcmp(r
, "d")) {
413 } else if (!strcmp(r
, "s")) {
415 x86_stack_size
= strtol(r
, (char **)&r
, 0);
416 if (x86_stack_size
<= 0)
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
++];
430 filename
= argv
[optind
];
434 logfile
= fopen(DEBUG_LOGFILE
, "w");
436 perror(DEBUG_LOGFILE
);
439 setvbuf(logfile
, NULL
, _IOLBF
, 0);
443 memset(regs
, 0, sizeof(struct target_pt_regs
));
445 /* Zero out image_info */
446 memset(info
, 0, sizeof(struct image_info
));
448 /* Scan interp_prefix dir for replacement files. */
449 init_paths(interp_prefix
);
451 if (elf_exec(filename
, argv
+optind
, environ
, regs
, info
) != 0) {
452 printf("Error loading %s\n", filename
);
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
);
467 target_set_brk((char *)info
->brk
);
471 env
= cpu_x86_init();
474 /* build Task State */
475 memset(ts
, 0, sizeof(TaskState
));
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
;
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
);