target-xtensa: Use mul*2 for mul*hi
[qemu/agraf.git] / bsd-user / main.c
blob097fbfe4320ee9e7359dff321852c716da585608
1 /*
2 * qemu user main
4 * Copyright (c) 2003-2008 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, see <http://www.gnu.org/licenses/>.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <machine/trap.h>
26 #include <sys/types.h>
27 #include <sys/mman.h>
29 #include "qemu.h"
30 #include "qemu-common.h"
31 /* For tb_lock */
32 #include "cpu.h"
33 #include "tcg.h"
34 #include "qemu/timer.h"
35 #include "qemu/envlist.h"
37 #define DEBUG_LOGFILE "/tmp/qemu.log"
39 int singlestep;
40 #if defined(CONFIG_USE_GUEST_BASE)
41 unsigned long mmap_min_addr;
42 unsigned long guest_base;
43 int have_guest_base;
44 unsigned long reserved_va;
45 #endif
47 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
48 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
49 extern char **environ;
50 enum BSDType bsd_type;
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 */
55 unsigned long x86_stack_size = 512 * 1024;
57 void gemu_log(const char *fmt, ...)
59 va_list ap;
61 va_start(ap, fmt);
62 vfprintf(stderr, fmt, ap);
63 va_end(ap);
66 #if defined(TARGET_I386)
67 int cpu_get_pic_interrupt(CPUX86State *env)
69 return -1;
71 #endif
73 /* These are no-ops because we are not threadsafe. */
74 static inline void cpu_exec_start(CPUArchState *env)
78 static inline void cpu_exec_end(CPUArchState *env)
82 static inline void start_exclusive(void)
86 static inline void end_exclusive(void)
90 void fork_start(void)
94 void fork_end(int child)
96 if (child) {
97 gdbserver_fork(thread_env);
101 void cpu_list_lock(void)
105 void cpu_list_unlock(void)
109 #ifdef TARGET_I386
110 /***********************************************************/
111 /* CPUX86 core interface */
113 void cpu_smm_update(CPUX86State *env)
117 uint64_t cpu_get_tsc(CPUX86State *env)
119 return cpu_get_real_ticks();
122 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
123 int flags)
125 unsigned int e1, e2;
126 uint32_t *p;
127 e1 = (addr << 16) | (limit & 0xffff);
128 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
129 e2 |= flags;
130 p = ptr;
131 p[0] = tswap32(e1);
132 p[1] = tswap32(e2);
135 static uint64_t *idt_table;
136 #ifdef TARGET_X86_64
137 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
138 uint64_t addr, unsigned int sel)
140 uint32_t *p, e1, e2;
141 e1 = (addr & 0xffff) | (sel << 16);
142 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
143 p = ptr;
144 p[0] = tswap32(e1);
145 p[1] = tswap32(e2);
146 p[2] = tswap32(addr >> 32);
147 p[3] = 0;
149 /* only dpl matters as we do only user space emulation */
150 static void set_idt(int n, unsigned int dpl)
152 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
154 #else
155 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
156 uint32_t addr, unsigned int sel)
158 uint32_t *p, e1, e2;
159 e1 = (addr & 0xffff) | (sel << 16);
160 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
161 p = ptr;
162 p[0] = tswap32(e1);
163 p[1] = tswap32(e2);
166 /* only dpl matters as we do only user space emulation */
167 static void set_idt(int n, unsigned int dpl)
169 set_gate(idt_table + n, 0, dpl, 0, 0);
171 #endif
173 void cpu_loop(CPUX86State *env)
175 int trapnr;
176 abi_ulong pc;
177 //target_siginfo_t info;
179 for(;;) {
180 trapnr = cpu_x86_exec(env);
181 switch(trapnr) {
182 case 0x80:
183 /* syscall from int $0x80 */
184 if (bsd_type == target_freebsd) {
185 abi_ulong params = (abi_ulong) env->regs[R_ESP] +
186 sizeof(int32_t);
187 int32_t syscall_nr = env->regs[R_EAX];
188 int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
190 if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
191 get_user_s32(syscall_nr, params);
192 params += sizeof(int32_t);
193 } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
194 get_user_s32(syscall_nr, params);
195 params += sizeof(int64_t);
197 get_user_s32(arg1, params);
198 params += sizeof(int32_t);
199 get_user_s32(arg2, params);
200 params += sizeof(int32_t);
201 get_user_s32(arg3, params);
202 params += sizeof(int32_t);
203 get_user_s32(arg4, params);
204 params += sizeof(int32_t);
205 get_user_s32(arg5, params);
206 params += sizeof(int32_t);
207 get_user_s32(arg6, params);
208 params += sizeof(int32_t);
209 get_user_s32(arg7, params);
210 params += sizeof(int32_t);
211 get_user_s32(arg8, params);
212 env->regs[R_EAX] = do_freebsd_syscall(env,
213 syscall_nr,
214 arg1,
215 arg2,
216 arg3,
217 arg4,
218 arg5,
219 arg6,
220 arg7,
221 arg8);
222 } else { //if (bsd_type == target_openbsd)
223 env->regs[R_EAX] = do_openbsd_syscall(env,
224 env->regs[R_EAX],
225 env->regs[R_EBX],
226 env->regs[R_ECX],
227 env->regs[R_EDX],
228 env->regs[R_ESI],
229 env->regs[R_EDI],
230 env->regs[R_EBP]);
232 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
233 env->regs[R_EAX] = -env->regs[R_EAX];
234 env->eflags |= CC_C;
235 } else {
236 env->eflags &= ~CC_C;
238 break;
239 #ifndef TARGET_ABI32
240 case EXCP_SYSCALL:
241 /* syscall from syscall instruction */
242 if (bsd_type == target_freebsd)
243 env->regs[R_EAX] = do_freebsd_syscall(env,
244 env->regs[R_EAX],
245 env->regs[R_EDI],
246 env->regs[R_ESI],
247 env->regs[R_EDX],
248 env->regs[R_ECX],
249 env->regs[8],
250 env->regs[9], 0, 0);
251 else { //if (bsd_type == target_openbsd)
252 env->regs[R_EAX] = do_openbsd_syscall(env,
253 env->regs[R_EAX],
254 env->regs[R_EDI],
255 env->regs[R_ESI],
256 env->regs[R_EDX],
257 env->regs[10],
258 env->regs[8],
259 env->regs[9]);
261 env->eip = env->exception_next_eip;
262 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
263 env->regs[R_EAX] = -env->regs[R_EAX];
264 env->eflags |= CC_C;
265 } else {
266 env->eflags &= ~CC_C;
268 break;
269 #endif
270 #if 0
271 case EXCP0B_NOSEG:
272 case EXCP0C_STACK:
273 info.si_signo = SIGBUS;
274 info.si_errno = 0;
275 info.si_code = TARGET_SI_KERNEL;
276 info._sifields._sigfault._addr = 0;
277 queue_signal(env, info.si_signo, &info);
278 break;
279 case EXCP0D_GPF:
280 /* XXX: potential problem if ABI32 */
281 #ifndef TARGET_X86_64
282 if (env->eflags & VM_MASK) {
283 handle_vm86_fault(env);
284 } else
285 #endif
287 info.si_signo = SIGSEGV;
288 info.si_errno = 0;
289 info.si_code = TARGET_SI_KERNEL;
290 info._sifields._sigfault._addr = 0;
291 queue_signal(env, info.si_signo, &info);
293 break;
294 case EXCP0E_PAGE:
295 info.si_signo = SIGSEGV;
296 info.si_errno = 0;
297 if (!(env->error_code & 1))
298 info.si_code = TARGET_SEGV_MAPERR;
299 else
300 info.si_code = TARGET_SEGV_ACCERR;
301 info._sifields._sigfault._addr = env->cr[2];
302 queue_signal(env, info.si_signo, &info);
303 break;
304 case EXCP00_DIVZ:
305 #ifndef TARGET_X86_64
306 if (env->eflags & VM_MASK) {
307 handle_vm86_trap(env, trapnr);
308 } else
309 #endif
311 /* division by zero */
312 info.si_signo = SIGFPE;
313 info.si_errno = 0;
314 info.si_code = TARGET_FPE_INTDIV;
315 info._sifields._sigfault._addr = env->eip;
316 queue_signal(env, info.si_signo, &info);
318 break;
319 case EXCP01_DB:
320 case EXCP03_INT3:
321 #ifndef TARGET_X86_64
322 if (env->eflags & VM_MASK) {
323 handle_vm86_trap(env, trapnr);
324 } else
325 #endif
327 info.si_signo = SIGTRAP;
328 info.si_errno = 0;
329 if (trapnr == EXCP01_DB) {
330 info.si_code = TARGET_TRAP_BRKPT;
331 info._sifields._sigfault._addr = env->eip;
332 } else {
333 info.si_code = TARGET_SI_KERNEL;
334 info._sifields._sigfault._addr = 0;
336 queue_signal(env, info.si_signo, &info);
338 break;
339 case EXCP04_INTO:
340 case EXCP05_BOUND:
341 #ifndef TARGET_X86_64
342 if (env->eflags & VM_MASK) {
343 handle_vm86_trap(env, trapnr);
344 } else
345 #endif
347 info.si_signo = SIGSEGV;
348 info.si_errno = 0;
349 info.si_code = TARGET_SI_KERNEL;
350 info._sifields._sigfault._addr = 0;
351 queue_signal(env, info.si_signo, &info);
353 break;
354 case EXCP06_ILLOP:
355 info.si_signo = SIGILL;
356 info.si_errno = 0;
357 info.si_code = TARGET_ILL_ILLOPN;
358 info._sifields._sigfault._addr = env->eip;
359 queue_signal(env, info.si_signo, &info);
360 break;
361 #endif
362 case EXCP_INTERRUPT:
363 /* just indicate that signals should be handled asap */
364 break;
365 #if 0
366 case EXCP_DEBUG:
368 int sig;
370 sig = gdb_handlesig (env, TARGET_SIGTRAP);
371 if (sig)
373 info.si_signo = sig;
374 info.si_errno = 0;
375 info.si_code = TARGET_TRAP_BRKPT;
376 queue_signal(env, info.si_signo, &info);
379 break;
380 #endif
381 default:
382 pc = env->segs[R_CS].base + env->eip;
383 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
384 (long)pc, trapnr);
385 abort();
387 process_pending_signals(env);
390 #endif
392 #ifdef TARGET_SPARC
393 #define SPARC64_STACK_BIAS 2047
395 //#define DEBUG_WIN
396 /* WARNING: dealing with register windows _is_ complicated. More info
397 can be found at http://www.sics.se/~psm/sparcstack.html */
398 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
400 index = (index + cwp * 16) % (16 * env->nwindows);
401 /* wrap handling : if cwp is on the last window, then we use the
402 registers 'after' the end */
403 if (index < 8 && env->cwp == env->nwindows - 1)
404 index += 16 * env->nwindows;
405 return index;
408 /* save the register window 'cwp1' */
409 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
411 unsigned int i;
412 abi_ulong sp_ptr;
414 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
415 #ifdef TARGET_SPARC64
416 if (sp_ptr & 3)
417 sp_ptr += SPARC64_STACK_BIAS;
418 #endif
419 #if defined(DEBUG_WIN)
420 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
421 sp_ptr, cwp1);
422 #endif
423 for(i = 0; i < 16; i++) {
424 /* FIXME - what to do if put_user() fails? */
425 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
426 sp_ptr += sizeof(abi_ulong);
430 static void save_window(CPUSPARCState *env)
432 #ifndef TARGET_SPARC64
433 unsigned int new_wim;
434 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
435 ((1LL << env->nwindows) - 1);
436 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
437 env->wim = new_wim;
438 #else
439 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
440 env->cansave++;
441 env->canrestore--;
442 #endif
445 static void restore_window(CPUSPARCState *env)
447 #ifndef TARGET_SPARC64
448 unsigned int new_wim;
449 #endif
450 unsigned int i, cwp1;
451 abi_ulong sp_ptr;
453 #ifndef TARGET_SPARC64
454 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
455 ((1LL << env->nwindows) - 1);
456 #endif
458 /* restore the invalid window */
459 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
460 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
461 #ifdef TARGET_SPARC64
462 if (sp_ptr & 3)
463 sp_ptr += SPARC64_STACK_BIAS;
464 #endif
465 #if defined(DEBUG_WIN)
466 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
467 sp_ptr, cwp1);
468 #endif
469 for(i = 0; i < 16; i++) {
470 /* FIXME - what to do if get_user() fails? */
471 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
472 sp_ptr += sizeof(abi_ulong);
474 #ifdef TARGET_SPARC64
475 env->canrestore++;
476 if (env->cleanwin < env->nwindows - 1)
477 env->cleanwin++;
478 env->cansave--;
479 #else
480 env->wim = new_wim;
481 #endif
484 static void flush_windows(CPUSPARCState *env)
486 int offset, cwp1;
488 offset = 1;
489 for(;;) {
490 /* if restore would invoke restore_window(), then we can stop */
491 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
492 #ifndef TARGET_SPARC64
493 if (env->wim & (1 << cwp1))
494 break;
495 #else
496 if (env->canrestore == 0)
497 break;
498 env->cansave++;
499 env->canrestore--;
500 #endif
501 save_window_offset(env, cwp1);
502 offset++;
504 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
505 #ifndef TARGET_SPARC64
506 /* set wim so that restore will reload the registers */
507 env->wim = 1 << cwp1;
508 #endif
509 #if defined(DEBUG_WIN)
510 printf("flush_windows: nb=%d\n", offset - 1);
511 #endif
514 void cpu_loop(CPUSPARCState *env)
516 int trapnr, ret, syscall_nr;
517 //target_siginfo_t info;
519 while (1) {
520 trapnr = cpu_sparc_exec (env);
522 switch (trapnr) {
523 #ifndef TARGET_SPARC64
524 case 0x80:
525 #else
526 /* FreeBSD uses 0x141 for syscalls too */
527 case 0x141:
528 if (bsd_type != target_freebsd)
529 goto badtrap;
530 case 0x100:
531 #endif
532 syscall_nr = env->gregs[1];
533 if (bsd_type == target_freebsd)
534 ret = do_freebsd_syscall(env, syscall_nr,
535 env->regwptr[0], env->regwptr[1],
536 env->regwptr[2], env->regwptr[3],
537 env->regwptr[4], env->regwptr[5], 0, 0);
538 else if (bsd_type == target_netbsd)
539 ret = do_netbsd_syscall(env, syscall_nr,
540 env->regwptr[0], env->regwptr[1],
541 env->regwptr[2], env->regwptr[3],
542 env->regwptr[4], env->regwptr[5]);
543 else { //if (bsd_type == target_openbsd)
544 #if defined(TARGET_SPARC64)
545 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
546 TARGET_OPENBSD_SYSCALL_G2RFLAG);
547 #endif
548 ret = do_openbsd_syscall(env, syscall_nr,
549 env->regwptr[0], env->regwptr[1],
550 env->regwptr[2], env->regwptr[3],
551 env->regwptr[4], env->regwptr[5]);
553 if ((unsigned int)ret >= (unsigned int)(-515)) {
554 ret = -ret;
555 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
556 env->xcc |= PSR_CARRY;
557 #else
558 env->psr |= PSR_CARRY;
559 #endif
560 } else {
561 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
562 env->xcc &= ~PSR_CARRY;
563 #else
564 env->psr &= ~PSR_CARRY;
565 #endif
567 env->regwptr[0] = ret;
568 /* next instruction */
569 #if defined(TARGET_SPARC64)
570 if (bsd_type == target_openbsd &&
571 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
572 env->pc = env->gregs[2];
573 env->npc = env->pc + 4;
574 } else if (bsd_type == target_openbsd &&
575 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
576 env->pc = env->gregs[7];
577 env->npc = env->pc + 4;
578 } else {
579 env->pc = env->npc;
580 env->npc = env->npc + 4;
582 #else
583 env->pc = env->npc;
584 env->npc = env->npc + 4;
585 #endif
586 break;
587 case 0x83: /* flush windows */
588 #ifdef TARGET_ABI32
589 case 0x103:
590 #endif
591 flush_windows(env);
592 /* next instruction */
593 env->pc = env->npc;
594 env->npc = env->npc + 4;
595 break;
596 #ifndef TARGET_SPARC64
597 case TT_WIN_OVF: /* window overflow */
598 save_window(env);
599 break;
600 case TT_WIN_UNF: /* window underflow */
601 restore_window(env);
602 break;
603 case TT_TFAULT:
604 case TT_DFAULT:
605 #if 0
607 info.si_signo = SIGSEGV;
608 info.si_errno = 0;
609 /* XXX: check env->error_code */
610 info.si_code = TARGET_SEGV_MAPERR;
611 info._sifields._sigfault._addr = env->mmuregs[4];
612 queue_signal(env, info.si_signo, &info);
614 #endif
615 break;
616 #else
617 case TT_SPILL: /* window overflow */
618 save_window(env);
619 break;
620 case TT_FILL: /* window underflow */
621 restore_window(env);
622 break;
623 case TT_TFAULT:
624 case TT_DFAULT:
625 #if 0
627 info.si_signo = SIGSEGV;
628 info.si_errno = 0;
629 /* XXX: check env->error_code */
630 info.si_code = TARGET_SEGV_MAPERR;
631 if (trapnr == TT_DFAULT)
632 info._sifields._sigfault._addr = env->dmmuregs[4];
633 else
634 info._sifields._sigfault._addr = env->tsptr->tpc;
635 //queue_signal(env, info.si_signo, &info);
637 #endif
638 break;
639 #endif
640 case EXCP_INTERRUPT:
641 /* just indicate that signals should be handled asap */
642 break;
643 case EXCP_DEBUG:
645 int sig;
647 sig = gdb_handlesig (env, TARGET_SIGTRAP);
648 #if 0
649 if (sig)
651 info.si_signo = sig;
652 info.si_errno = 0;
653 info.si_code = TARGET_TRAP_BRKPT;
654 //queue_signal(env, info.si_signo, &info);
656 #endif
658 break;
659 default:
660 #ifdef TARGET_SPARC64
661 badtrap:
662 #endif
663 printf ("Unhandled trap: 0x%x\n", trapnr);
664 cpu_dump_state(env, stderr, fprintf, 0);
665 exit (1);
667 process_pending_signals (env);
671 #endif
673 static void usage(void)
675 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
676 "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
677 "BSD CPU emulator (compiled for %s emulation)\n"
678 "\n"
679 "Standard options:\n"
680 "-h print this help\n"
681 "-g port wait gdb connection to port\n"
682 "-L path set the elf interpreter prefix (default=%s)\n"
683 "-s size set the stack size in bytes (default=%ld)\n"
684 "-cpu model select CPU (-cpu help for list)\n"
685 "-drop-ld-preload drop LD_PRELOAD for target process\n"
686 "-E var=value sets/modifies targets environment variable(s)\n"
687 "-U var unsets targets environment variable(s)\n"
688 #if defined(CONFIG_USE_GUEST_BASE)
689 "-B address set guest_base address to address\n"
690 #endif
691 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
692 "\n"
693 "Debug options:\n"
694 "-d options activate log (default logfile=%s)\n"
695 "-D logfile override default logfile location\n"
696 "-p pagesize set the host page size to 'pagesize'\n"
697 "-singlestep always run in singlestep mode\n"
698 "-strace log system calls\n"
699 "\n"
700 "Environment variables:\n"
701 "QEMU_STRACE Print system calls and arguments similar to the\n"
702 " 'strace' program. Enable by setting to any value.\n"
703 "You can use -E and -U options to set/unset environment variables\n"
704 "for target process. It is possible to provide several variables\n"
705 "by repeating the option. For example:\n"
706 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
707 "Note that if you provide several changes to single variable\n"
708 "last change will stay in effect.\n"
710 TARGET_ARCH,
711 interp_prefix,
712 x86_stack_size,
713 DEBUG_LOGFILE);
714 exit(1);
717 THREAD CPUArchState *thread_env;
719 /* Assumes contents are already zeroed. */
720 void init_task_state(TaskState *ts)
722 int i;
724 ts->used = 1;
725 ts->first_free = ts->sigqueue_table;
726 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
727 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
729 ts->sigqueue_table[i].next = NULL;
732 int main(int argc, char **argv)
734 const char *filename;
735 const char *cpu_model;
736 const char *log_file = DEBUG_LOGFILE;
737 const char *log_mask = NULL;
738 struct target_pt_regs regs1, *regs = &regs1;
739 struct image_info info1, *info = &info1;
740 TaskState ts1, *ts = &ts1;
741 CPUArchState *env;
742 int optind;
743 const char *r;
744 int gdbstub_port = 0;
745 char **target_environ, **wrk;
746 envlist_t *envlist = NULL;
747 bsd_type = target_openbsd;
749 if (argc <= 1)
750 usage();
752 module_call_init(MODULE_INIT_QOM);
754 if ((envlist = envlist_create()) == NULL) {
755 (void) fprintf(stderr, "Unable to allocate envlist\n");
756 exit(1);
759 /* add current environment into the list */
760 for (wrk = environ; *wrk != NULL; wrk++) {
761 (void) envlist_setenv(envlist, *wrk);
764 cpu_model = NULL;
765 #if defined(cpudef_setup)
766 cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
767 #endif
769 optind = 1;
770 for(;;) {
771 if (optind >= argc)
772 break;
773 r = argv[optind];
774 if (r[0] != '-')
775 break;
776 optind++;
777 r++;
778 if (!strcmp(r, "-")) {
779 break;
780 } else if (!strcmp(r, "d")) {
781 if (optind >= argc) {
782 break;
784 log_mask = argv[optind++];
785 } else if (!strcmp(r, "D")) {
786 if (optind >= argc) {
787 break;
789 log_file = argv[optind++];
790 } else if (!strcmp(r, "E")) {
791 r = argv[optind++];
792 if (envlist_setenv(envlist, r) != 0)
793 usage();
794 } else if (!strcmp(r, "ignore-environment")) {
795 envlist_free(envlist);
796 if ((envlist = envlist_create()) == NULL) {
797 (void) fprintf(stderr, "Unable to allocate envlist\n");
798 exit(1);
800 } else if (!strcmp(r, "U")) {
801 r = argv[optind++];
802 if (envlist_unsetenv(envlist, r) != 0)
803 usage();
804 } else if (!strcmp(r, "s")) {
805 r = argv[optind++];
806 x86_stack_size = strtol(r, (char **)&r, 0);
807 if (x86_stack_size <= 0)
808 usage();
809 if (*r == 'M')
810 x86_stack_size *= 1024 * 1024;
811 else if (*r == 'k' || *r == 'K')
812 x86_stack_size *= 1024;
813 } else if (!strcmp(r, "L")) {
814 interp_prefix = argv[optind++];
815 } else if (!strcmp(r, "p")) {
816 qemu_host_page_size = atoi(argv[optind++]);
817 if (qemu_host_page_size == 0 ||
818 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
819 fprintf(stderr, "page size must be a power of two\n");
820 exit(1);
822 } else if (!strcmp(r, "g")) {
823 gdbstub_port = atoi(argv[optind++]);
824 } else if (!strcmp(r, "r")) {
825 qemu_uname_release = argv[optind++];
826 } else if (!strcmp(r, "cpu")) {
827 cpu_model = argv[optind++];
828 if (is_help_option(cpu_model)) {
829 /* XXX: implement xxx_cpu_list for targets that still miss it */
830 #if defined(cpu_list)
831 cpu_list(stdout, &fprintf);
832 #endif
833 exit(1);
835 #if defined(CONFIG_USE_GUEST_BASE)
836 } else if (!strcmp(r, "B")) {
837 guest_base = strtol(argv[optind++], NULL, 0);
838 have_guest_base = 1;
839 #endif
840 } else if (!strcmp(r, "drop-ld-preload")) {
841 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
842 } else if (!strcmp(r, "bsd")) {
843 if (!strcasecmp(argv[optind], "freebsd")) {
844 bsd_type = target_freebsd;
845 } else if (!strcasecmp(argv[optind], "netbsd")) {
846 bsd_type = target_netbsd;
847 } else if (!strcasecmp(argv[optind], "openbsd")) {
848 bsd_type = target_openbsd;
849 } else {
850 usage();
852 optind++;
853 } else if (!strcmp(r, "singlestep")) {
854 singlestep = 1;
855 } else if (!strcmp(r, "strace")) {
856 do_strace = 1;
857 } else
859 usage();
863 /* init debug */
864 qemu_set_log_filename(log_file);
865 if (log_mask) {
866 int mask;
868 mask = qemu_str_to_log_mask(log_mask);
869 if (!mask) {
870 qemu_print_log_usage(stdout);
871 exit(1);
873 qemu_set_log(mask);
876 if (optind >= argc) {
877 usage();
879 filename = argv[optind];
881 /* Zero out regs */
882 memset(regs, 0, sizeof(struct target_pt_regs));
884 /* Zero out image_info */
885 memset(info, 0, sizeof(struct image_info));
887 /* Scan interp_prefix dir for replacement files. */
888 init_paths(interp_prefix);
890 if (cpu_model == NULL) {
891 #if defined(TARGET_I386)
892 #ifdef TARGET_X86_64
893 cpu_model = "qemu64";
894 #else
895 cpu_model = "qemu32";
896 #endif
897 #elif defined(TARGET_SPARC)
898 #ifdef TARGET_SPARC64
899 cpu_model = "TI UltraSparc II";
900 #else
901 cpu_model = "Fujitsu MB86904";
902 #endif
903 #else
904 cpu_model = "any";
905 #endif
907 tcg_exec_init(0);
908 cpu_exec_init_all();
909 /* NOTE: we need to init the CPU at this stage to get
910 qemu_host_page_size */
911 env = cpu_init(cpu_model);
912 if (!env) {
913 fprintf(stderr, "Unable to find CPU definition\n");
914 exit(1);
916 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
917 cpu_reset(ENV_GET_CPU(env));
918 #endif
919 thread_env = env;
921 if (getenv("QEMU_STRACE")) {
922 do_strace = 1;
925 target_environ = envlist_to_environ(envlist, NULL);
926 envlist_free(envlist);
928 #if defined(CONFIG_USE_GUEST_BASE)
930 * Now that page sizes are configured in cpu_init() we can do
931 * proper page alignment for guest_base.
933 guest_base = HOST_PAGE_ALIGN(guest_base);
936 * Read in mmap_min_addr kernel parameter. This value is used
937 * When loading the ELF image to determine whether guest_base
938 * is needed.
940 * When user has explicitly set the quest base, we skip this
941 * test.
943 if (!have_guest_base) {
944 FILE *fp;
946 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
947 unsigned long tmp;
948 if (fscanf(fp, "%lu", &tmp) == 1) {
949 mmap_min_addr = tmp;
950 qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr);
952 fclose(fp);
955 #endif /* CONFIG_USE_GUEST_BASE */
957 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
958 printf("Error loading %s\n", filename);
959 _exit(1);
962 for (wrk = target_environ; *wrk; wrk++) {
963 free(*wrk);
966 free(target_environ);
968 if (qemu_log_enabled()) {
969 #if defined(CONFIG_USE_GUEST_BASE)
970 qemu_log("guest_base 0x%lx\n", guest_base);
971 #endif
972 log_page_dump();
974 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
975 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
976 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
977 info->start_code);
978 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
979 info->start_data);
980 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
981 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
982 info->start_stack);
983 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
984 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
987 target_set_brk(info->brk);
988 syscall_init();
989 signal_init();
991 #if defined(CONFIG_USE_GUEST_BASE)
992 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
993 generating the prologue until now so that the prologue can take
994 the real value of GUEST_BASE into account. */
995 tcg_prologue_init(&tcg_ctx);
996 #endif
998 /* build Task State */
999 memset(ts, 0, sizeof(TaskState));
1000 init_task_state(ts);
1001 ts->info = info;
1002 env->opaque = ts;
1004 #if defined(TARGET_I386)
1005 cpu_x86_set_cpl(env, 3);
1007 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
1008 env->hflags |= HF_PE_MASK;
1009 if (env->cpuid_features & CPUID_SSE) {
1010 env->cr[4] |= CR4_OSFXSR_MASK;
1011 env->hflags |= HF_OSFXSR_MASK;
1013 #ifndef TARGET_ABI32
1014 /* enable 64 bit mode if possible */
1015 if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
1016 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
1017 exit(1);
1019 env->cr[4] |= CR4_PAE_MASK;
1020 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
1021 env->hflags |= HF_LMA_MASK;
1022 #endif
1024 /* flags setup : we activate the IRQs by default as in user mode */
1025 env->eflags |= IF_MASK;
1027 /* linux register setup */
1028 #ifndef TARGET_ABI32
1029 env->regs[R_EAX] = regs->rax;
1030 env->regs[R_EBX] = regs->rbx;
1031 env->regs[R_ECX] = regs->rcx;
1032 env->regs[R_EDX] = regs->rdx;
1033 env->regs[R_ESI] = regs->rsi;
1034 env->regs[R_EDI] = regs->rdi;
1035 env->regs[R_EBP] = regs->rbp;
1036 env->regs[R_ESP] = regs->rsp;
1037 env->eip = regs->rip;
1038 #else
1039 env->regs[R_EAX] = regs->eax;
1040 env->regs[R_EBX] = regs->ebx;
1041 env->regs[R_ECX] = regs->ecx;
1042 env->regs[R_EDX] = regs->edx;
1043 env->regs[R_ESI] = regs->esi;
1044 env->regs[R_EDI] = regs->edi;
1045 env->regs[R_EBP] = regs->ebp;
1046 env->regs[R_ESP] = regs->esp;
1047 env->eip = regs->eip;
1048 #endif
1050 /* linux interrupt setup */
1051 #ifndef TARGET_ABI32
1052 env->idt.limit = 511;
1053 #else
1054 env->idt.limit = 255;
1055 #endif
1056 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
1057 PROT_READ|PROT_WRITE,
1058 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1059 idt_table = g2h(env->idt.base);
1060 set_idt(0, 0);
1061 set_idt(1, 0);
1062 set_idt(2, 0);
1063 set_idt(3, 3);
1064 set_idt(4, 3);
1065 set_idt(5, 0);
1066 set_idt(6, 0);
1067 set_idt(7, 0);
1068 set_idt(8, 0);
1069 set_idt(9, 0);
1070 set_idt(10, 0);
1071 set_idt(11, 0);
1072 set_idt(12, 0);
1073 set_idt(13, 0);
1074 set_idt(14, 0);
1075 set_idt(15, 0);
1076 set_idt(16, 0);
1077 set_idt(17, 0);
1078 set_idt(18, 0);
1079 set_idt(19, 0);
1080 set_idt(0x80, 3);
1082 /* linux segment setup */
1084 uint64_t *gdt_table;
1085 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
1086 PROT_READ|PROT_WRITE,
1087 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1088 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
1089 gdt_table = g2h(env->gdt.base);
1090 #ifdef TARGET_ABI32
1091 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1092 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1093 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1094 #else
1095 /* 64 bit code segment */
1096 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1097 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1098 DESC_L_MASK |
1099 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1100 #endif
1101 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
1102 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1103 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
1106 cpu_x86_load_seg(env, R_CS, __USER_CS);
1107 cpu_x86_load_seg(env, R_SS, __USER_DS);
1108 #ifdef TARGET_ABI32
1109 cpu_x86_load_seg(env, R_DS, __USER_DS);
1110 cpu_x86_load_seg(env, R_ES, __USER_DS);
1111 cpu_x86_load_seg(env, R_FS, __USER_DS);
1112 cpu_x86_load_seg(env, R_GS, __USER_DS);
1113 /* This hack makes Wine work... */
1114 env->segs[R_FS].selector = 0;
1115 #else
1116 cpu_x86_load_seg(env, R_DS, 0);
1117 cpu_x86_load_seg(env, R_ES, 0);
1118 cpu_x86_load_seg(env, R_FS, 0);
1119 cpu_x86_load_seg(env, R_GS, 0);
1120 #endif
1121 #elif defined(TARGET_SPARC)
1123 int i;
1124 env->pc = regs->pc;
1125 env->npc = regs->npc;
1126 env->y = regs->y;
1127 for(i = 0; i < 8; i++)
1128 env->gregs[i] = regs->u_regs[i];
1129 for(i = 0; i < 8; i++)
1130 env->regwptr[i] = regs->u_regs[i + 8];
1132 #else
1133 #error unsupported target CPU
1134 #endif
1136 if (gdbstub_port) {
1137 gdbserver_start (gdbstub_port);
1138 gdb_handlesig(env, 0);
1140 cpu_loop(env);
1141 /* never exits */
1142 return 0;