memory: optimize qemu_get_ram_ptr and qemu_ram_ptr_length
[qemu/ar7.git] / bsd-user / main.c
blob287ec1d36953434b7ef9bb461b532b09fe6bef22
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 "qemu/osdep.h"
20 #include <machine/trap.h>
21 #include <sys/mman.h>
23 #include "qemu.h"
24 #include "qemu-common.h"
25 /* For tb_lock */
26 #include "cpu.h"
27 #include "tcg.h"
28 #include "qemu/timer.h"
29 #include "qemu/envlist.h"
30 #include "exec/log.h"
32 int singlestep;
33 unsigned long mmap_min_addr;
34 unsigned long guest_base;
35 int have_guest_base;
36 unsigned long reserved_va;
38 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
39 const char *qemu_uname_release;
40 extern char **environ;
41 enum BSDType bsd_type;
43 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
44 we allocate a bigger stack. Need a better solution, for example
45 by remapping the process stack directly at the right place */
46 unsigned long x86_stack_size = 512 * 1024;
48 void gemu_log(const char *fmt, ...)
50 va_list ap;
52 va_start(ap, fmt);
53 vfprintf(stderr, fmt, ap);
54 va_end(ap);
57 #if defined(TARGET_I386)
58 int cpu_get_pic_interrupt(CPUX86State *env)
60 return -1;
62 #endif
64 /* These are no-ops because we are not threadsafe. */
65 static inline void cpu_exec_start(CPUArchState *env)
69 static inline void cpu_exec_end(CPUArchState *env)
73 static inline void start_exclusive(void)
77 static inline void end_exclusive(void)
81 void fork_start(void)
85 void fork_end(int child)
87 if (child) {
88 gdbserver_fork(thread_cpu);
92 void cpu_list_lock(void)
96 void cpu_list_unlock(void)
100 #ifdef TARGET_I386
101 /***********************************************************/
102 /* CPUX86 core interface */
104 uint64_t cpu_get_tsc(CPUX86State *env)
106 return cpu_get_host_ticks();
109 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
110 int flags)
112 unsigned int e1, e2;
113 uint32_t *p;
114 e1 = (addr << 16) | (limit & 0xffff);
115 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
116 e2 |= flags;
117 p = ptr;
118 p[0] = tswap32(e1);
119 p[1] = tswap32(e2);
122 static uint64_t *idt_table;
123 #ifdef TARGET_X86_64
124 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
125 uint64_t addr, unsigned int sel)
127 uint32_t *p, e1, e2;
128 e1 = (addr & 0xffff) | (sel << 16);
129 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
130 p = ptr;
131 p[0] = tswap32(e1);
132 p[1] = tswap32(e2);
133 p[2] = tswap32(addr >> 32);
134 p[3] = 0;
136 /* only dpl matters as we do only user space emulation */
137 static void set_idt(int n, unsigned int dpl)
139 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
141 #else
142 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
143 uint32_t addr, unsigned int sel)
145 uint32_t *p, e1, e2;
146 e1 = (addr & 0xffff) | (sel << 16);
147 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
148 p = ptr;
149 p[0] = tswap32(e1);
150 p[1] = tswap32(e2);
153 /* only dpl matters as we do only user space emulation */
154 static void set_idt(int n, unsigned int dpl)
156 set_gate(idt_table + n, 0, dpl, 0, 0);
158 #endif
160 void cpu_loop(CPUX86State *env)
162 X86CPU *cpu = x86_env_get_cpu(env);
163 CPUState *cs = CPU(cpu);
164 int trapnr;
165 abi_ulong pc;
166 //target_siginfo_t info;
168 for(;;) {
169 trapnr = cpu_x86_exec(cs);
170 switch(trapnr) {
171 case 0x80:
172 /* syscall from int $0x80 */
173 if (bsd_type == target_freebsd) {
174 abi_ulong params = (abi_ulong) env->regs[R_ESP] +
175 sizeof(int32_t);
176 int32_t syscall_nr = env->regs[R_EAX];
177 int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
179 if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
180 get_user_s32(syscall_nr, params);
181 params += sizeof(int32_t);
182 } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
183 get_user_s32(syscall_nr, params);
184 params += sizeof(int64_t);
186 get_user_s32(arg1, params);
187 params += sizeof(int32_t);
188 get_user_s32(arg2, params);
189 params += sizeof(int32_t);
190 get_user_s32(arg3, params);
191 params += sizeof(int32_t);
192 get_user_s32(arg4, params);
193 params += sizeof(int32_t);
194 get_user_s32(arg5, params);
195 params += sizeof(int32_t);
196 get_user_s32(arg6, params);
197 params += sizeof(int32_t);
198 get_user_s32(arg7, params);
199 params += sizeof(int32_t);
200 get_user_s32(arg8, params);
201 env->regs[R_EAX] = do_freebsd_syscall(env,
202 syscall_nr,
203 arg1,
204 arg2,
205 arg3,
206 arg4,
207 arg5,
208 arg6,
209 arg7,
210 arg8);
211 } else { //if (bsd_type == target_openbsd)
212 env->regs[R_EAX] = do_openbsd_syscall(env,
213 env->regs[R_EAX],
214 env->regs[R_EBX],
215 env->regs[R_ECX],
216 env->regs[R_EDX],
217 env->regs[R_ESI],
218 env->regs[R_EDI],
219 env->regs[R_EBP]);
221 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
222 env->regs[R_EAX] = -env->regs[R_EAX];
223 env->eflags |= CC_C;
224 } else {
225 env->eflags &= ~CC_C;
227 break;
228 #ifndef TARGET_ABI32
229 case EXCP_SYSCALL:
230 /* syscall from syscall instruction */
231 if (bsd_type == target_freebsd)
232 env->regs[R_EAX] = do_freebsd_syscall(env,
233 env->regs[R_EAX],
234 env->regs[R_EDI],
235 env->regs[R_ESI],
236 env->regs[R_EDX],
237 env->regs[R_ECX],
238 env->regs[8],
239 env->regs[9], 0, 0);
240 else { //if (bsd_type == target_openbsd)
241 env->regs[R_EAX] = do_openbsd_syscall(env,
242 env->regs[R_EAX],
243 env->regs[R_EDI],
244 env->regs[R_ESI],
245 env->regs[R_EDX],
246 env->regs[10],
247 env->regs[8],
248 env->regs[9]);
250 env->eip = env->exception_next_eip;
251 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
252 env->regs[R_EAX] = -env->regs[R_EAX];
253 env->eflags |= CC_C;
254 } else {
255 env->eflags &= ~CC_C;
257 break;
258 #endif
259 #if 0
260 case EXCP0B_NOSEG:
261 case EXCP0C_STACK:
262 info.si_signo = SIGBUS;
263 info.si_errno = 0;
264 info.si_code = TARGET_SI_KERNEL;
265 info._sifields._sigfault._addr = 0;
266 queue_signal(env, info.si_signo, &info);
267 break;
268 case EXCP0D_GPF:
269 /* XXX: potential problem if ABI32 */
270 #ifndef TARGET_X86_64
271 if (env->eflags & VM_MASK) {
272 handle_vm86_fault(env);
273 } else
274 #endif
276 info.si_signo = SIGSEGV;
277 info.si_errno = 0;
278 info.si_code = TARGET_SI_KERNEL;
279 info._sifields._sigfault._addr = 0;
280 queue_signal(env, info.si_signo, &info);
282 break;
283 case EXCP0E_PAGE:
284 info.si_signo = SIGSEGV;
285 info.si_errno = 0;
286 if (!(env->error_code & 1))
287 info.si_code = TARGET_SEGV_MAPERR;
288 else
289 info.si_code = TARGET_SEGV_ACCERR;
290 info._sifields._sigfault._addr = env->cr[2];
291 queue_signal(env, info.si_signo, &info);
292 break;
293 case EXCP00_DIVZ:
294 #ifndef TARGET_X86_64
295 if (env->eflags & VM_MASK) {
296 handle_vm86_trap(env, trapnr);
297 } else
298 #endif
300 /* division by zero */
301 info.si_signo = SIGFPE;
302 info.si_errno = 0;
303 info.si_code = TARGET_FPE_INTDIV;
304 info._sifields._sigfault._addr = env->eip;
305 queue_signal(env, info.si_signo, &info);
307 break;
308 case EXCP01_DB:
309 case EXCP03_INT3:
310 #ifndef TARGET_X86_64
311 if (env->eflags & VM_MASK) {
312 handle_vm86_trap(env, trapnr);
313 } else
314 #endif
316 info.si_signo = SIGTRAP;
317 info.si_errno = 0;
318 if (trapnr == EXCP01_DB) {
319 info.si_code = TARGET_TRAP_BRKPT;
320 info._sifields._sigfault._addr = env->eip;
321 } else {
322 info.si_code = TARGET_SI_KERNEL;
323 info._sifields._sigfault._addr = 0;
325 queue_signal(env, info.si_signo, &info);
327 break;
328 case EXCP04_INTO:
329 case EXCP05_BOUND:
330 #ifndef TARGET_X86_64
331 if (env->eflags & VM_MASK) {
332 handle_vm86_trap(env, trapnr);
333 } else
334 #endif
336 info.si_signo = SIGSEGV;
337 info.si_errno = 0;
338 info.si_code = TARGET_SI_KERNEL;
339 info._sifields._sigfault._addr = 0;
340 queue_signal(env, info.si_signo, &info);
342 break;
343 case EXCP06_ILLOP:
344 info.si_signo = SIGILL;
345 info.si_errno = 0;
346 info.si_code = TARGET_ILL_ILLOPN;
347 info._sifields._sigfault._addr = env->eip;
348 queue_signal(env, info.si_signo, &info);
349 break;
350 #endif
351 case EXCP_INTERRUPT:
352 /* just indicate that signals should be handled asap */
353 break;
354 #if 0
355 case EXCP_DEBUG:
357 int sig;
359 sig = gdb_handlesig (env, TARGET_SIGTRAP);
360 if (sig)
362 info.si_signo = sig;
363 info.si_errno = 0;
364 info.si_code = TARGET_TRAP_BRKPT;
365 queue_signal(env, info.si_signo, &info);
368 break;
369 #endif
370 default:
371 pc = env->segs[R_CS].base + env->eip;
372 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
373 (long)pc, trapnr);
374 abort();
376 process_pending_signals(env);
379 #endif
381 #ifdef TARGET_SPARC
382 #define SPARC64_STACK_BIAS 2047
384 //#define DEBUG_WIN
385 /* WARNING: dealing with register windows _is_ complicated. More info
386 can be found at http://www.sics.se/~psm/sparcstack.html */
387 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
389 index = (index + cwp * 16) % (16 * env->nwindows);
390 /* wrap handling : if cwp is on the last window, then we use the
391 registers 'after' the end */
392 if (index < 8 && env->cwp == env->nwindows - 1)
393 index += 16 * env->nwindows;
394 return index;
397 /* save the register window 'cwp1' */
398 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
400 unsigned int i;
401 abi_ulong sp_ptr;
403 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
404 #ifdef TARGET_SPARC64
405 if (sp_ptr & 3)
406 sp_ptr += SPARC64_STACK_BIAS;
407 #endif
408 #if defined(DEBUG_WIN)
409 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
410 sp_ptr, cwp1);
411 #endif
412 for(i = 0; i < 16; i++) {
413 /* FIXME - what to do if put_user() fails? */
414 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
415 sp_ptr += sizeof(abi_ulong);
419 static void save_window(CPUSPARCState *env)
421 #ifndef TARGET_SPARC64
422 unsigned int new_wim;
423 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
424 ((1LL << env->nwindows) - 1);
425 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
426 env->wim = new_wim;
427 #else
428 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
429 env->cansave++;
430 env->canrestore--;
431 #endif
434 static void restore_window(CPUSPARCState *env)
436 #ifndef TARGET_SPARC64
437 unsigned int new_wim;
438 #endif
439 unsigned int i, cwp1;
440 abi_ulong sp_ptr;
442 #ifndef TARGET_SPARC64
443 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
444 ((1LL << env->nwindows) - 1);
445 #endif
447 /* restore the invalid window */
448 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
449 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
450 #ifdef TARGET_SPARC64
451 if (sp_ptr & 3)
452 sp_ptr += SPARC64_STACK_BIAS;
453 #endif
454 #if defined(DEBUG_WIN)
455 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
456 sp_ptr, cwp1);
457 #endif
458 for(i = 0; i < 16; i++) {
459 /* FIXME - what to do if get_user() fails? */
460 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
461 sp_ptr += sizeof(abi_ulong);
463 #ifdef TARGET_SPARC64
464 env->canrestore++;
465 if (env->cleanwin < env->nwindows - 1)
466 env->cleanwin++;
467 env->cansave--;
468 #else
469 env->wim = new_wim;
470 #endif
473 static void flush_windows(CPUSPARCState *env)
475 int offset, cwp1;
477 offset = 1;
478 for(;;) {
479 /* if restore would invoke restore_window(), then we can stop */
480 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
481 #ifndef TARGET_SPARC64
482 if (env->wim & (1 << cwp1))
483 break;
484 #else
485 if (env->canrestore == 0)
486 break;
487 env->cansave++;
488 env->canrestore--;
489 #endif
490 save_window_offset(env, cwp1);
491 offset++;
493 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
494 #ifndef TARGET_SPARC64
495 /* set wim so that restore will reload the registers */
496 env->wim = 1 << cwp1;
497 #endif
498 #if defined(DEBUG_WIN)
499 printf("flush_windows: nb=%d\n", offset - 1);
500 #endif
503 void cpu_loop(CPUSPARCState *env)
505 CPUState *cs = CPU(sparc_env_get_cpu(env));
506 int trapnr, ret, syscall_nr;
507 //target_siginfo_t info;
509 while (1) {
510 trapnr = cpu_sparc_exec(cs);
512 switch (trapnr) {
513 #ifndef TARGET_SPARC64
514 case 0x80:
515 #else
516 /* FreeBSD uses 0x141 for syscalls too */
517 case 0x141:
518 if (bsd_type != target_freebsd)
519 goto badtrap;
520 case 0x100:
521 #endif
522 syscall_nr = env->gregs[1];
523 if (bsd_type == target_freebsd)
524 ret = do_freebsd_syscall(env, syscall_nr,
525 env->regwptr[0], env->regwptr[1],
526 env->regwptr[2], env->regwptr[3],
527 env->regwptr[4], env->regwptr[5], 0, 0);
528 else if (bsd_type == target_netbsd)
529 ret = do_netbsd_syscall(env, syscall_nr,
530 env->regwptr[0], env->regwptr[1],
531 env->regwptr[2], env->regwptr[3],
532 env->regwptr[4], env->regwptr[5]);
533 else { //if (bsd_type == target_openbsd)
534 #if defined(TARGET_SPARC64)
535 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
536 TARGET_OPENBSD_SYSCALL_G2RFLAG);
537 #endif
538 ret = do_openbsd_syscall(env, syscall_nr,
539 env->regwptr[0], env->regwptr[1],
540 env->regwptr[2], env->regwptr[3],
541 env->regwptr[4], env->regwptr[5]);
543 if ((unsigned int)ret >= (unsigned int)(-515)) {
544 ret = -ret;
545 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
546 env->xcc |= PSR_CARRY;
547 #else
548 env->psr |= PSR_CARRY;
549 #endif
550 } else {
551 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
552 env->xcc &= ~PSR_CARRY;
553 #else
554 env->psr &= ~PSR_CARRY;
555 #endif
557 env->regwptr[0] = ret;
558 /* next instruction */
559 #if defined(TARGET_SPARC64)
560 if (bsd_type == target_openbsd &&
561 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
562 env->pc = env->gregs[2];
563 env->npc = env->pc + 4;
564 } else if (bsd_type == target_openbsd &&
565 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
566 env->pc = env->gregs[7];
567 env->npc = env->pc + 4;
568 } else {
569 env->pc = env->npc;
570 env->npc = env->npc + 4;
572 #else
573 env->pc = env->npc;
574 env->npc = env->npc + 4;
575 #endif
576 break;
577 case 0x83: /* flush windows */
578 #ifdef TARGET_ABI32
579 case 0x103:
580 #endif
581 flush_windows(env);
582 /* next instruction */
583 env->pc = env->npc;
584 env->npc = env->npc + 4;
585 break;
586 #ifndef TARGET_SPARC64
587 case TT_WIN_OVF: /* window overflow */
588 save_window(env);
589 break;
590 case TT_WIN_UNF: /* window underflow */
591 restore_window(env);
592 break;
593 case TT_TFAULT:
594 case TT_DFAULT:
595 #if 0
597 info.si_signo = SIGSEGV;
598 info.si_errno = 0;
599 /* XXX: check env->error_code */
600 info.si_code = TARGET_SEGV_MAPERR;
601 info._sifields._sigfault._addr = env->mmuregs[4];
602 queue_signal(env, info.si_signo, &info);
604 #endif
605 break;
606 #else
607 case TT_SPILL: /* window overflow */
608 save_window(env);
609 break;
610 case TT_FILL: /* window underflow */
611 restore_window(env);
612 break;
613 case TT_TFAULT:
614 case TT_DFAULT:
615 #if 0
617 info.si_signo = SIGSEGV;
618 info.si_errno = 0;
619 /* XXX: check env->error_code */
620 info.si_code = TARGET_SEGV_MAPERR;
621 if (trapnr == TT_DFAULT)
622 info._sifields._sigfault._addr = env->dmmuregs[4];
623 else
624 info._sifields._sigfault._addr = env->tsptr->tpc;
625 //queue_signal(env, info.si_signo, &info);
627 #endif
628 break;
629 #endif
630 case EXCP_INTERRUPT:
631 /* just indicate that signals should be handled asap */
632 break;
633 case EXCP_DEBUG:
635 int sig;
637 sig = gdb_handlesig(cs, TARGET_SIGTRAP);
638 #if 0
639 if (sig)
641 info.si_signo = sig;
642 info.si_errno = 0;
643 info.si_code = TARGET_TRAP_BRKPT;
644 //queue_signal(env, info.si_signo, &info);
646 #endif
648 break;
649 default:
650 #ifdef TARGET_SPARC64
651 badtrap:
652 #endif
653 printf ("Unhandled trap: 0x%x\n", trapnr);
654 cpu_dump_state(cs, stderr, fprintf, 0);
655 exit (1);
657 process_pending_signals (env);
661 #endif
663 static void usage(void)
665 printf("qemu-" TARGET_NAME " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
666 "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
667 "BSD CPU emulator (compiled for %s emulation)\n"
668 "\n"
669 "Standard options:\n"
670 "-h print this help\n"
671 "-g port wait gdb connection to port\n"
672 "-L path set the elf interpreter prefix (default=%s)\n"
673 "-s size set the stack size in bytes (default=%ld)\n"
674 "-cpu model select CPU (-cpu help for list)\n"
675 "-drop-ld-preload drop LD_PRELOAD for target process\n"
676 "-E var=value sets/modifies targets environment variable(s)\n"
677 "-U var unsets targets environment variable(s)\n"
678 "-B address set guest_base address to address\n"
679 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
680 "\n"
681 "Debug options:\n"
682 "-d item1[,...] enable logging of specified items\n"
683 " (use '-d help' for a list of log items)\n"
684 "-D logfile write logs to 'logfile' (default stderr)\n"
685 "-p pagesize set the host page size to 'pagesize'\n"
686 "-singlestep always run in singlestep mode\n"
687 "-strace log system calls\n"
688 "\n"
689 "Environment variables:\n"
690 "QEMU_STRACE Print system calls and arguments similar to the\n"
691 " 'strace' program. Enable by setting to any value.\n"
692 "You can use -E and -U options to set/unset environment variables\n"
693 "for target process. It is possible to provide several variables\n"
694 "by repeating the option. For example:\n"
695 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
696 "Note that if you provide several changes to single variable\n"
697 "last change will stay in effect.\n"
699 TARGET_NAME,
700 interp_prefix,
701 x86_stack_size);
702 exit(1);
705 THREAD CPUState *thread_cpu;
707 /* Assumes contents are already zeroed. */
708 void init_task_state(TaskState *ts)
710 int i;
712 ts->used = 1;
713 ts->first_free = ts->sigqueue_table;
714 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
715 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
717 ts->sigqueue_table[i].next = NULL;
720 int main(int argc, char **argv)
722 const char *filename;
723 const char *cpu_model;
724 const char *log_file = NULL;
725 const char *log_mask = NULL;
726 struct target_pt_regs regs1, *regs = &regs1;
727 struct image_info info1, *info = &info1;
728 TaskState ts1, *ts = &ts1;
729 CPUArchState *env;
730 CPUState *cpu;
731 int optind;
732 const char *r;
733 int gdbstub_port = 0;
734 char **target_environ, **wrk;
735 envlist_t *envlist = NULL;
736 bsd_type = target_openbsd;
738 if (argc <= 1)
739 usage();
741 module_call_init(MODULE_INIT_QOM);
743 if ((envlist = envlist_create()) == NULL) {
744 (void) fprintf(stderr, "Unable to allocate envlist\n");
745 exit(1);
748 /* add current environment into the list */
749 for (wrk = environ; *wrk != NULL; wrk++) {
750 (void) envlist_setenv(envlist, *wrk);
753 cpu_model = NULL;
754 #if defined(cpudef_setup)
755 cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
756 #endif
758 optind = 1;
759 for(;;) {
760 if (optind >= argc)
761 break;
762 r = argv[optind];
763 if (r[0] != '-')
764 break;
765 optind++;
766 r++;
767 if (!strcmp(r, "-")) {
768 break;
769 } else if (!strcmp(r, "d")) {
770 if (optind >= argc) {
771 break;
773 log_mask = argv[optind++];
774 } else if (!strcmp(r, "D")) {
775 if (optind >= argc) {
776 break;
778 log_file = argv[optind++];
779 } else if (!strcmp(r, "E")) {
780 r = argv[optind++];
781 if (envlist_setenv(envlist, r) != 0)
782 usage();
783 } else if (!strcmp(r, "ignore-environment")) {
784 envlist_free(envlist);
785 if ((envlist = envlist_create()) == NULL) {
786 (void) fprintf(stderr, "Unable to allocate envlist\n");
787 exit(1);
789 } else if (!strcmp(r, "U")) {
790 r = argv[optind++];
791 if (envlist_unsetenv(envlist, r) != 0)
792 usage();
793 } else if (!strcmp(r, "s")) {
794 r = argv[optind++];
795 x86_stack_size = strtol(r, (char **)&r, 0);
796 if (x86_stack_size <= 0)
797 usage();
798 if (*r == 'M')
799 x86_stack_size *= 1024 * 1024;
800 else if (*r == 'k' || *r == 'K')
801 x86_stack_size *= 1024;
802 } else if (!strcmp(r, "L")) {
803 interp_prefix = argv[optind++];
804 } else if (!strcmp(r, "p")) {
805 qemu_host_page_size = atoi(argv[optind++]);
806 if (qemu_host_page_size == 0 ||
807 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
808 fprintf(stderr, "page size must be a power of two\n");
809 exit(1);
811 } else if (!strcmp(r, "g")) {
812 gdbstub_port = atoi(argv[optind++]);
813 } else if (!strcmp(r, "r")) {
814 qemu_uname_release = argv[optind++];
815 } else if (!strcmp(r, "cpu")) {
816 cpu_model = argv[optind++];
817 if (is_help_option(cpu_model)) {
818 /* XXX: implement xxx_cpu_list for targets that still miss it */
819 #if defined(cpu_list)
820 cpu_list(stdout, &fprintf);
821 #endif
822 exit(1);
824 } else if (!strcmp(r, "B")) {
825 guest_base = strtol(argv[optind++], NULL, 0);
826 have_guest_base = 1;
827 } else if (!strcmp(r, "drop-ld-preload")) {
828 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
829 } else if (!strcmp(r, "bsd")) {
830 if (!strcasecmp(argv[optind], "freebsd")) {
831 bsd_type = target_freebsd;
832 } else if (!strcasecmp(argv[optind], "netbsd")) {
833 bsd_type = target_netbsd;
834 } else if (!strcasecmp(argv[optind], "openbsd")) {
835 bsd_type = target_openbsd;
836 } else {
837 usage();
839 optind++;
840 } else if (!strcmp(r, "singlestep")) {
841 singlestep = 1;
842 } else if (!strcmp(r, "strace")) {
843 do_strace = 1;
844 } else
846 usage();
850 /* init debug */
851 qemu_set_log_filename(log_file);
852 if (log_mask) {
853 int mask;
855 mask = qemu_str_to_log_mask(log_mask);
856 if (!mask) {
857 qemu_print_log_usage(stdout);
858 exit(1);
860 qemu_set_log(mask);
863 if (optind >= argc) {
864 usage();
866 filename = argv[optind];
868 /* Zero out regs */
869 memset(regs, 0, sizeof(struct target_pt_regs));
871 /* Zero out image_info */
872 memset(info, 0, sizeof(struct image_info));
874 /* Scan interp_prefix dir for replacement files. */
875 init_paths(interp_prefix);
877 if (cpu_model == NULL) {
878 #if defined(TARGET_I386)
879 #ifdef TARGET_X86_64
880 cpu_model = "qemu64";
881 #else
882 cpu_model = "qemu32";
883 #endif
884 #elif defined(TARGET_SPARC)
885 #ifdef TARGET_SPARC64
886 cpu_model = "TI UltraSparc II";
887 #else
888 cpu_model = "Fujitsu MB86904";
889 #endif
890 #else
891 cpu_model = "any";
892 #endif
894 tcg_exec_init(0);
895 /* NOTE: we need to init the CPU at this stage to get
896 qemu_host_page_size */
897 cpu = cpu_init(cpu_model);
898 if (!cpu) {
899 fprintf(stderr, "Unable to find CPU definition\n");
900 exit(1);
902 env = cpu->env_ptr;
903 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
904 cpu_reset(cpu);
905 #endif
906 thread_cpu = cpu;
908 if (getenv("QEMU_STRACE")) {
909 do_strace = 1;
912 target_environ = envlist_to_environ(envlist, NULL);
913 envlist_free(envlist);
916 * Now that page sizes are configured in cpu_init() we can do
917 * proper page alignment for guest_base.
919 guest_base = HOST_PAGE_ALIGN(guest_base);
922 * Read in mmap_min_addr kernel parameter. This value is used
923 * When loading the ELF image to determine whether guest_base
924 * is needed.
926 * When user has explicitly set the quest base, we skip this
927 * test.
929 if (!have_guest_base) {
930 FILE *fp;
932 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
933 unsigned long tmp;
934 if (fscanf(fp, "%lu", &tmp) == 1) {
935 mmap_min_addr = tmp;
936 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr);
938 fclose(fp);
942 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
943 printf("Error loading %s\n", filename);
944 _exit(1);
947 for (wrk = target_environ; *wrk; wrk++) {
948 free(*wrk);
951 free(target_environ);
953 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
954 qemu_log("guest_base 0x%lx\n", guest_base);
955 log_page_dump();
957 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
958 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
959 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
960 info->start_code);
961 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
962 info->start_data);
963 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
964 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
965 info->start_stack);
966 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
967 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
970 target_set_brk(info->brk);
971 syscall_init();
972 signal_init();
974 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
975 generating the prologue until now so that the prologue can take
976 the real value of GUEST_BASE into account. */
977 tcg_prologue_init(&tcg_ctx);
979 /* build Task State */
980 memset(ts, 0, sizeof(TaskState));
981 init_task_state(ts);
982 ts->info = info;
983 cpu->opaque = ts;
985 #if defined(TARGET_I386)
986 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
987 env->hflags |= HF_PE_MASK | HF_CPL_MASK;
988 if (env->features[FEAT_1_EDX] & CPUID_SSE) {
989 env->cr[4] |= CR4_OSFXSR_MASK;
990 env->hflags |= HF_OSFXSR_MASK;
992 #ifndef TARGET_ABI32
993 /* enable 64 bit mode if possible */
994 if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
995 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
996 exit(1);
998 env->cr[4] |= CR4_PAE_MASK;
999 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
1000 env->hflags |= HF_LMA_MASK;
1001 #endif
1003 /* flags setup : we activate the IRQs by default as in user mode */
1004 env->eflags |= IF_MASK;
1006 /* linux register setup */
1007 #ifndef TARGET_ABI32
1008 env->regs[R_EAX] = regs->rax;
1009 env->regs[R_EBX] = regs->rbx;
1010 env->regs[R_ECX] = regs->rcx;
1011 env->regs[R_EDX] = regs->rdx;
1012 env->regs[R_ESI] = regs->rsi;
1013 env->regs[R_EDI] = regs->rdi;
1014 env->regs[R_EBP] = regs->rbp;
1015 env->regs[R_ESP] = regs->rsp;
1016 env->eip = regs->rip;
1017 #else
1018 env->regs[R_EAX] = regs->eax;
1019 env->regs[R_EBX] = regs->ebx;
1020 env->regs[R_ECX] = regs->ecx;
1021 env->regs[R_EDX] = regs->edx;
1022 env->regs[R_ESI] = regs->esi;
1023 env->regs[R_EDI] = regs->edi;
1024 env->regs[R_EBP] = regs->ebp;
1025 env->regs[R_ESP] = regs->esp;
1026 env->eip = regs->eip;
1027 #endif
1029 /* linux interrupt setup */
1030 #ifndef TARGET_ABI32
1031 env->idt.limit = 511;
1032 #else
1033 env->idt.limit = 255;
1034 #endif
1035 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
1036 PROT_READ|PROT_WRITE,
1037 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1038 idt_table = g2h(env->idt.base);
1039 set_idt(0, 0);
1040 set_idt(1, 0);
1041 set_idt(2, 0);
1042 set_idt(3, 3);
1043 set_idt(4, 3);
1044 set_idt(5, 0);
1045 set_idt(6, 0);
1046 set_idt(7, 0);
1047 set_idt(8, 0);
1048 set_idt(9, 0);
1049 set_idt(10, 0);
1050 set_idt(11, 0);
1051 set_idt(12, 0);
1052 set_idt(13, 0);
1053 set_idt(14, 0);
1054 set_idt(15, 0);
1055 set_idt(16, 0);
1056 set_idt(17, 0);
1057 set_idt(18, 0);
1058 set_idt(19, 0);
1059 set_idt(0x80, 3);
1061 /* linux segment setup */
1063 uint64_t *gdt_table;
1064 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
1065 PROT_READ|PROT_WRITE,
1066 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1067 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
1068 gdt_table = g2h(env->gdt.base);
1069 #ifdef TARGET_ABI32
1070 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1071 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1072 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1073 #else
1074 /* 64 bit code segment */
1075 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1076 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1077 DESC_L_MASK |
1078 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1079 #endif
1080 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
1081 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1082 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
1085 cpu_x86_load_seg(env, R_CS, __USER_CS);
1086 cpu_x86_load_seg(env, R_SS, __USER_DS);
1087 #ifdef TARGET_ABI32
1088 cpu_x86_load_seg(env, R_DS, __USER_DS);
1089 cpu_x86_load_seg(env, R_ES, __USER_DS);
1090 cpu_x86_load_seg(env, R_FS, __USER_DS);
1091 cpu_x86_load_seg(env, R_GS, __USER_DS);
1092 /* This hack makes Wine work... */
1093 env->segs[R_FS].selector = 0;
1094 #else
1095 cpu_x86_load_seg(env, R_DS, 0);
1096 cpu_x86_load_seg(env, R_ES, 0);
1097 cpu_x86_load_seg(env, R_FS, 0);
1098 cpu_x86_load_seg(env, R_GS, 0);
1099 #endif
1100 #elif defined(TARGET_SPARC)
1102 int i;
1103 env->pc = regs->pc;
1104 env->npc = regs->npc;
1105 env->y = regs->y;
1106 for(i = 0; i < 8; i++)
1107 env->gregs[i] = regs->u_regs[i];
1108 for(i = 0; i < 8; i++)
1109 env->regwptr[i] = regs->u_regs[i + 8];
1111 #else
1112 #error unsupported target CPU
1113 #endif
1115 if (gdbstub_port) {
1116 gdbserver_start (gdbstub_port);
1117 gdb_handlesig(cpu, 0);
1119 cpu_loop(env);
1120 /* never exits */
1121 return 0;