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