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