ppc: Fix 64K pages support in full emulation
[qemu/kevin.git] / bsd-user / main.c
blob4819b9ec63339cb19cd4a4b2f6c9e0f218b1deca
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>
22 #include "qapi/error.h"
23 #include "qemu.h"
24 #include "qemu/path.h"
25 #include "qemu/help_option.h"
26 /* For tb_lock */
27 #include "cpu.h"
28 #include "exec/exec-all.h"
29 #include "tcg.h"
30 #include "qemu/timer.h"
31 #include "qemu/envlist.h"
32 #include "exec/log.h"
34 int singlestep;
35 unsigned long mmap_min_addr;
36 unsigned long guest_base;
37 int have_guest_base;
38 unsigned long reserved_va;
40 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
41 const char *qemu_uname_release;
42 extern char **environ;
43 enum BSDType bsd_type;
45 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
46 we allocate a bigger stack. Need a better solution, for example
47 by remapping the process stack directly at the right place */
48 unsigned long x86_stack_size = 512 * 1024;
50 void gemu_log(const char *fmt, ...)
52 va_list ap;
54 va_start(ap, fmt);
55 vfprintf(stderr, fmt, ap);
56 va_end(ap);
59 #if defined(TARGET_I386)
60 int cpu_get_pic_interrupt(CPUX86State *env)
62 return -1;
64 #endif
66 /* These are no-ops because we are not threadsafe. */
67 static inline void cpu_exec_start(CPUArchState *env)
71 static inline void cpu_exec_end(CPUArchState *env)
75 static inline void start_exclusive(void)
79 static inline void end_exclusive(void)
83 void fork_start(void)
87 void fork_end(int child)
89 if (child) {
90 gdbserver_fork(thread_cpu);
94 void cpu_list_lock(void)
98 void cpu_list_unlock(void)
102 #ifdef TARGET_I386
103 /***********************************************************/
104 /* CPUX86 core interface */
106 uint64_t cpu_get_tsc(CPUX86State *env)
108 return cpu_get_host_ticks();
111 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
112 int flags)
114 unsigned int e1, e2;
115 uint32_t *p;
116 e1 = (addr << 16) | (limit & 0xffff);
117 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
118 e2 |= flags;
119 p = ptr;
120 p[0] = tswap32(e1);
121 p[1] = tswap32(e2);
124 static uint64_t *idt_table;
125 #ifdef TARGET_X86_64
126 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
127 uint64_t addr, unsigned int sel)
129 uint32_t *p, e1, e2;
130 e1 = (addr & 0xffff) | (sel << 16);
131 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
132 p = ptr;
133 p[0] = tswap32(e1);
134 p[1] = tswap32(e2);
135 p[2] = tswap32(addr >> 32);
136 p[3] = 0;
138 /* only dpl matters as we do only user space emulation */
139 static void set_idt(int n, unsigned int dpl)
141 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
143 #else
144 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
145 uint32_t addr, unsigned int sel)
147 uint32_t *p, e1, e2;
148 e1 = (addr & 0xffff) | (sel << 16);
149 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
150 p = ptr;
151 p[0] = tswap32(e1);
152 p[1] = tswap32(e2);
155 /* only dpl matters as we do only user space emulation */
156 static void set_idt(int n, unsigned int dpl)
158 set_gate(idt_table + n, 0, dpl, 0, 0);
160 #endif
162 void cpu_loop(CPUX86State *env)
164 X86CPU *cpu = x86_env_get_cpu(env);
165 CPUState *cs = CPU(cpu);
166 int trapnr;
167 abi_ulong pc;
168 //target_siginfo_t info;
170 for(;;) {
171 trapnr = cpu_x86_exec(cs);
172 switch(trapnr) {
173 case 0x80:
174 /* syscall from int $0x80 */
175 if (bsd_type == target_freebsd) {
176 abi_ulong params = (abi_ulong) env->regs[R_ESP] +
177 sizeof(int32_t);
178 int32_t syscall_nr = env->regs[R_EAX];
179 int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
181 if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
182 get_user_s32(syscall_nr, params);
183 params += sizeof(int32_t);
184 } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
185 get_user_s32(syscall_nr, params);
186 params += sizeof(int64_t);
188 get_user_s32(arg1, params);
189 params += sizeof(int32_t);
190 get_user_s32(arg2, params);
191 params += sizeof(int32_t);
192 get_user_s32(arg3, params);
193 params += sizeof(int32_t);
194 get_user_s32(arg4, params);
195 params += sizeof(int32_t);
196 get_user_s32(arg5, params);
197 params += sizeof(int32_t);
198 get_user_s32(arg6, params);
199 params += sizeof(int32_t);
200 get_user_s32(arg7, params);
201 params += sizeof(int32_t);
202 get_user_s32(arg8, params);
203 env->regs[R_EAX] = do_freebsd_syscall(env,
204 syscall_nr,
205 arg1,
206 arg2,
207 arg3,
208 arg4,
209 arg5,
210 arg6,
211 arg7,
212 arg8);
213 } else { //if (bsd_type == target_openbsd)
214 env->regs[R_EAX] = do_openbsd_syscall(env,
215 env->regs[R_EAX],
216 env->regs[R_EBX],
217 env->regs[R_ECX],
218 env->regs[R_EDX],
219 env->regs[R_ESI],
220 env->regs[R_EDI],
221 env->regs[R_EBP]);
223 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
224 env->regs[R_EAX] = -env->regs[R_EAX];
225 env->eflags |= CC_C;
226 } else {
227 env->eflags &= ~CC_C;
229 break;
230 #ifndef TARGET_ABI32
231 case EXCP_SYSCALL:
232 /* syscall from syscall instruction */
233 if (bsd_type == target_freebsd)
234 env->regs[R_EAX] = do_freebsd_syscall(env,
235 env->regs[R_EAX],
236 env->regs[R_EDI],
237 env->regs[R_ESI],
238 env->regs[R_EDX],
239 env->regs[R_ECX],
240 env->regs[8],
241 env->regs[9], 0, 0);
242 else { //if (bsd_type == target_openbsd)
243 env->regs[R_EAX] = do_openbsd_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[10],
249 env->regs[8],
250 env->regs[9]);
252 env->eip = env->exception_next_eip;
253 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
254 env->regs[R_EAX] = -env->regs[R_EAX];
255 env->eflags |= CC_C;
256 } else {
257 env->eflags &= ~CC_C;
259 break;
260 #endif
261 #if 0
262 case EXCP0B_NOSEG:
263 case EXCP0C_STACK:
264 info.si_signo = SIGBUS;
265 info.si_errno = 0;
266 info.si_code = TARGET_SI_KERNEL;
267 info._sifields._sigfault._addr = 0;
268 queue_signal(env, info.si_signo, &info);
269 break;
270 case EXCP0D_GPF:
271 /* XXX: potential problem if ABI32 */
272 #ifndef TARGET_X86_64
273 if (env->eflags & VM_MASK) {
274 handle_vm86_fault(env);
275 } else
276 #endif
278 info.si_signo = SIGSEGV;
279 info.si_errno = 0;
280 info.si_code = TARGET_SI_KERNEL;
281 info._sifields._sigfault._addr = 0;
282 queue_signal(env, info.si_signo, &info);
284 break;
285 case EXCP0E_PAGE:
286 info.si_signo = SIGSEGV;
287 info.si_errno = 0;
288 if (!(env->error_code & 1))
289 info.si_code = TARGET_SEGV_MAPERR;
290 else
291 info.si_code = TARGET_SEGV_ACCERR;
292 info._sifields._sigfault._addr = env->cr[2];
293 queue_signal(env, info.si_signo, &info);
294 break;
295 case EXCP00_DIVZ:
296 #ifndef TARGET_X86_64
297 if (env->eflags & VM_MASK) {
298 handle_vm86_trap(env, trapnr);
299 } else
300 #endif
302 /* division by zero */
303 info.si_signo = SIGFPE;
304 info.si_errno = 0;
305 info.si_code = TARGET_FPE_INTDIV;
306 info._sifields._sigfault._addr = env->eip;
307 queue_signal(env, info.si_signo, &info);
309 break;
310 case EXCP01_DB:
311 case EXCP03_INT3:
312 #ifndef TARGET_X86_64
313 if (env->eflags & VM_MASK) {
314 handle_vm86_trap(env, trapnr);
315 } else
316 #endif
318 info.si_signo = SIGTRAP;
319 info.si_errno = 0;
320 if (trapnr == EXCP01_DB) {
321 info.si_code = TARGET_TRAP_BRKPT;
322 info._sifields._sigfault._addr = env->eip;
323 } else {
324 info.si_code = TARGET_SI_KERNEL;
325 info._sifields._sigfault._addr = 0;
327 queue_signal(env, info.si_signo, &info);
329 break;
330 case EXCP04_INTO:
331 case EXCP05_BOUND:
332 #ifndef TARGET_X86_64
333 if (env->eflags & VM_MASK) {
334 handle_vm86_trap(env, trapnr);
335 } else
336 #endif
338 info.si_signo = SIGSEGV;
339 info.si_errno = 0;
340 info.si_code = TARGET_SI_KERNEL;
341 info._sifields._sigfault._addr = 0;
342 queue_signal(env, info.si_signo, &info);
344 break;
345 case EXCP06_ILLOP:
346 info.si_signo = SIGILL;
347 info.si_errno = 0;
348 info.si_code = TARGET_ILL_ILLOPN;
349 info._sifields._sigfault._addr = env->eip;
350 queue_signal(env, info.si_signo, &info);
351 break;
352 #endif
353 case EXCP_INTERRUPT:
354 /* just indicate that signals should be handled asap */
355 break;
356 #if 0
357 case EXCP_DEBUG:
359 int sig;
361 sig = gdb_handlesig (env, TARGET_SIGTRAP);
362 if (sig)
364 info.si_signo = sig;
365 info.si_errno = 0;
366 info.si_code = TARGET_TRAP_BRKPT;
367 queue_signal(env, info.si_signo, &info);
370 break;
371 #endif
372 default:
373 pc = env->segs[R_CS].base + env->eip;
374 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
375 (long)pc, trapnr);
376 abort();
378 process_pending_signals(env);
381 #endif
383 #ifdef TARGET_SPARC
384 #define SPARC64_STACK_BIAS 2047
386 //#define DEBUG_WIN
387 /* WARNING: dealing with register windows _is_ complicated. More info
388 can be found at http://www.sics.se/~psm/sparcstack.html */
389 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
391 index = (index + cwp * 16) % (16 * env->nwindows);
392 /* wrap handling : if cwp is on the last window, then we use the
393 registers 'after' the end */
394 if (index < 8 && env->cwp == env->nwindows - 1)
395 index += 16 * env->nwindows;
396 return index;
399 /* save the register window 'cwp1' */
400 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
402 unsigned int i;
403 abi_ulong sp_ptr;
405 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
406 #ifdef TARGET_SPARC64
407 if (sp_ptr & 3)
408 sp_ptr += SPARC64_STACK_BIAS;
409 #endif
410 #if defined(DEBUG_WIN)
411 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
412 sp_ptr, cwp1);
413 #endif
414 for(i = 0; i < 16; i++) {
415 /* FIXME - what to do if put_user() fails? */
416 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
417 sp_ptr += sizeof(abi_ulong);
421 static void save_window(CPUSPARCState *env)
423 #ifndef TARGET_SPARC64
424 unsigned int new_wim;
425 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
426 ((1LL << env->nwindows) - 1);
427 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
428 env->wim = new_wim;
429 #else
430 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
431 env->cansave++;
432 env->canrestore--;
433 #endif
436 static void restore_window(CPUSPARCState *env)
438 #ifndef TARGET_SPARC64
439 unsigned int new_wim;
440 #endif
441 unsigned int i, cwp1;
442 abi_ulong sp_ptr;
444 #ifndef TARGET_SPARC64
445 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
446 ((1LL << env->nwindows) - 1);
447 #endif
449 /* restore the invalid window */
450 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
451 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
452 #ifdef TARGET_SPARC64
453 if (sp_ptr & 3)
454 sp_ptr += SPARC64_STACK_BIAS;
455 #endif
456 #if defined(DEBUG_WIN)
457 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
458 sp_ptr, cwp1);
459 #endif
460 for(i = 0; i < 16; i++) {
461 /* FIXME - what to do if get_user() fails? */
462 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
463 sp_ptr += sizeof(abi_ulong);
465 #ifdef TARGET_SPARC64
466 env->canrestore++;
467 if (env->cleanwin < env->nwindows - 1)
468 env->cleanwin++;
469 env->cansave--;
470 #else
471 env->wim = new_wim;
472 #endif
475 static void flush_windows(CPUSPARCState *env)
477 int offset, cwp1;
479 offset = 1;
480 for(;;) {
481 /* if restore would invoke restore_window(), then we can stop */
482 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
483 #ifndef TARGET_SPARC64
484 if (env->wim & (1 << cwp1))
485 break;
486 #else
487 if (env->canrestore == 0)
488 break;
489 env->cansave++;
490 env->canrestore--;
491 #endif
492 save_window_offset(env, cwp1);
493 offset++;
495 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
496 #ifndef TARGET_SPARC64
497 /* set wim so that restore will reload the registers */
498 env->wim = 1 << cwp1;
499 #endif
500 #if defined(DEBUG_WIN)
501 printf("flush_windows: nb=%d\n", offset - 1);
502 #endif
505 void cpu_loop(CPUSPARCState *env)
507 CPUState *cs = CPU(sparc_env_get_cpu(env));
508 int trapnr, ret, syscall_nr;
509 //target_siginfo_t info;
511 while (1) {
512 trapnr = cpu_sparc_exec(cs);
514 switch (trapnr) {
515 #ifndef TARGET_SPARC64
516 case 0x80:
517 #else
518 /* FreeBSD uses 0x141 for syscalls too */
519 case 0x141:
520 if (bsd_type != target_freebsd)
521 goto badtrap;
522 case 0x100:
523 #endif
524 syscall_nr = env->gregs[1];
525 if (bsd_type == target_freebsd)
526 ret = do_freebsd_syscall(env, syscall_nr,
527 env->regwptr[0], env->regwptr[1],
528 env->regwptr[2], env->regwptr[3],
529 env->regwptr[4], env->regwptr[5], 0, 0);
530 else if (bsd_type == target_netbsd)
531 ret = do_netbsd_syscall(env, syscall_nr,
532 env->regwptr[0], env->regwptr[1],
533 env->regwptr[2], env->regwptr[3],
534 env->regwptr[4], env->regwptr[5]);
535 else { //if (bsd_type == target_openbsd)
536 #if defined(TARGET_SPARC64)
537 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
538 TARGET_OPENBSD_SYSCALL_G2RFLAG);
539 #endif
540 ret = do_openbsd_syscall(env, syscall_nr,
541 env->regwptr[0], env->regwptr[1],
542 env->regwptr[2], env->regwptr[3],
543 env->regwptr[4], env->regwptr[5]);
545 if ((unsigned int)ret >= (unsigned int)(-515)) {
546 ret = -ret;
547 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
548 env->xcc |= PSR_CARRY;
549 #else
550 env->psr |= PSR_CARRY;
551 #endif
552 } else {
553 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
554 env->xcc &= ~PSR_CARRY;
555 #else
556 env->psr &= ~PSR_CARRY;
557 #endif
559 env->regwptr[0] = ret;
560 /* next instruction */
561 #if defined(TARGET_SPARC64)
562 if (bsd_type == target_openbsd &&
563 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
564 env->pc = env->gregs[2];
565 env->npc = env->pc + 4;
566 } else if (bsd_type == target_openbsd &&
567 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
568 env->pc = env->gregs[7];
569 env->npc = env->pc + 4;
570 } else {
571 env->pc = env->npc;
572 env->npc = env->npc + 4;
574 #else
575 env->pc = env->npc;
576 env->npc = env->npc + 4;
577 #endif
578 break;
579 case 0x83: /* flush windows */
580 #ifdef TARGET_ABI32
581 case 0x103:
582 #endif
583 flush_windows(env);
584 /* next instruction */
585 env->pc = env->npc;
586 env->npc = env->npc + 4;
587 break;
588 #ifndef TARGET_SPARC64
589 case TT_WIN_OVF: /* window overflow */
590 save_window(env);
591 break;
592 case TT_WIN_UNF: /* window underflow */
593 restore_window(env);
594 break;
595 case TT_TFAULT:
596 case TT_DFAULT:
597 #if 0
599 info.si_signo = SIGSEGV;
600 info.si_errno = 0;
601 /* XXX: check env->error_code */
602 info.si_code = TARGET_SEGV_MAPERR;
603 info._sifields._sigfault._addr = env->mmuregs[4];
604 queue_signal(env, info.si_signo, &info);
606 #endif
607 break;
608 #else
609 case TT_SPILL: /* window overflow */
610 save_window(env);
611 break;
612 case TT_FILL: /* window underflow */
613 restore_window(env);
614 break;
615 case TT_TFAULT:
616 case TT_DFAULT:
617 #if 0
619 info.si_signo = SIGSEGV;
620 info.si_errno = 0;
621 /* XXX: check env->error_code */
622 info.si_code = TARGET_SEGV_MAPERR;
623 if (trapnr == TT_DFAULT)
624 info._sifields._sigfault._addr = env->dmmuregs[4];
625 else
626 info._sifields._sigfault._addr = env->tsptr->tpc;
627 //queue_signal(env, info.si_signo, &info);
629 #endif
630 break;
631 #endif
632 case EXCP_INTERRUPT:
633 /* just indicate that signals should be handled asap */
634 break;
635 case EXCP_DEBUG:
637 int sig;
639 sig = gdb_handlesig(cs, TARGET_SIGTRAP);
640 #if 0
641 if (sig)
643 info.si_signo = sig;
644 info.si_errno = 0;
645 info.si_code = TARGET_TRAP_BRKPT;
646 //queue_signal(env, info.si_signo, &info);
648 #endif
650 break;
651 default:
652 #ifdef TARGET_SPARC64
653 badtrap:
654 #endif
655 printf ("Unhandled trap: 0x%x\n", trapnr);
656 cpu_dump_state(cs, stderr, fprintf, 0);
657 exit (1);
659 process_pending_signals (env);
663 #endif
665 static void usage(void)
667 printf("qemu-" TARGET_NAME " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
668 "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
669 "BSD CPU emulator (compiled for %s emulation)\n"
670 "\n"
671 "Standard options:\n"
672 "-h print this help\n"
673 "-g port wait gdb connection to port\n"
674 "-L path set the elf interpreter prefix (default=%s)\n"
675 "-s size set the stack size in bytes (default=%ld)\n"
676 "-cpu model select CPU (-cpu help for list)\n"
677 "-drop-ld-preload drop LD_PRELOAD for target process\n"
678 "-E var=value sets/modifies targets environment variable(s)\n"
679 "-U var unsets targets environment variable(s)\n"
680 "-B address set guest_base address to address\n"
681 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
682 "\n"
683 "Debug options:\n"
684 "-d item1[,...] enable logging of specified items\n"
685 " (use '-d help' for a list of log items)\n"
686 "-D logfile write logs to 'logfile' (default stderr)\n"
687 "-p pagesize set the host page size to 'pagesize'\n"
688 "-singlestep always run in singlestep mode\n"
689 "-strace log system calls\n"
690 "\n"
691 "Environment variables:\n"
692 "QEMU_STRACE Print system calls and arguments similar to the\n"
693 " 'strace' program. Enable by setting to any value.\n"
694 "You can use -E and -U options to set/unset environment variables\n"
695 "for target process. It is possible to provide several variables\n"
696 "by repeating the option. For example:\n"
697 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
698 "Note that if you provide several changes to single variable\n"
699 "last change will stay in effect.\n"
701 TARGET_NAME,
702 interp_prefix,
703 x86_stack_size);
704 exit(1);
707 THREAD CPUState *thread_cpu;
709 /* Assumes contents are already zeroed. */
710 void init_task_state(TaskState *ts)
712 int i;
714 ts->used = 1;
715 ts->first_free = ts->sigqueue_table;
716 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
717 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
719 ts->sigqueue_table[i].next = NULL;
722 int main(int argc, char **argv)
724 const char *filename;
725 const char *cpu_model;
726 const char *log_file = NULL;
727 const char *log_mask = NULL;
728 struct target_pt_regs regs1, *regs = &regs1;
729 struct image_info info1, *info = &info1;
730 TaskState ts1, *ts = &ts1;
731 CPUArchState *env;
732 CPUState *cpu;
733 int optind;
734 const char *r;
735 int gdbstub_port = 0;
736 char **target_environ, **wrk;
737 envlist_t *envlist = NULL;
738 bsd_type = target_openbsd;
740 if (argc <= 1)
741 usage();
743 module_call_init(MODULE_INIT_QOM);
745 if ((envlist = envlist_create()) == NULL) {
746 (void) fprintf(stderr, "Unable to allocate envlist\n");
747 exit(1);
750 /* add current environment into the list */
751 for (wrk = environ; *wrk != NULL; wrk++) {
752 (void) envlist_setenv(envlist, *wrk);
755 cpu_model = NULL;
757 optind = 1;
758 for(;;) {
759 if (optind >= argc)
760 break;
761 r = argv[optind];
762 if (r[0] != '-')
763 break;
764 optind++;
765 r++;
766 if (!strcmp(r, "-")) {
767 break;
768 } else if (!strcmp(r, "d")) {
769 if (optind >= argc) {
770 break;
772 log_mask = argv[optind++];
773 } else if (!strcmp(r, "D")) {
774 if (optind >= argc) {
775 break;
777 log_file = argv[optind++];
778 } else if (!strcmp(r, "E")) {
779 r = argv[optind++];
780 if (envlist_setenv(envlist, r) != 0)
781 usage();
782 } else if (!strcmp(r, "ignore-environment")) {
783 envlist_free(envlist);
784 if ((envlist = envlist_create()) == NULL) {
785 (void) fprintf(stderr, "Unable to allocate envlist\n");
786 exit(1);
788 } else if (!strcmp(r, "U")) {
789 r = argv[optind++];
790 if (envlist_unsetenv(envlist, r) != 0)
791 usage();
792 } else if (!strcmp(r, "s")) {
793 r = argv[optind++];
794 x86_stack_size = strtol(r, (char **)&r, 0);
795 if (x86_stack_size <= 0)
796 usage();
797 if (*r == 'M')
798 x86_stack_size *= 1024 * 1024;
799 else if (*r == 'k' || *r == 'K')
800 x86_stack_size *= 1024;
801 } else if (!strcmp(r, "L")) {
802 interp_prefix = argv[optind++];
803 } else if (!strcmp(r, "p")) {
804 qemu_host_page_size = atoi(argv[optind++]);
805 if (qemu_host_page_size == 0 ||
806 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
807 fprintf(stderr, "page size must be a power of two\n");
808 exit(1);
810 } else if (!strcmp(r, "g")) {
811 gdbstub_port = atoi(argv[optind++]);
812 } else if (!strcmp(r, "r")) {
813 qemu_uname_release = argv[optind++];
814 } else if (!strcmp(r, "cpu")) {
815 cpu_model = argv[optind++];
816 if (is_help_option(cpu_model)) {
817 /* XXX: implement xxx_cpu_list for targets that still miss it */
818 #if defined(cpu_list)
819 cpu_list(stdout, &fprintf);
820 #endif
821 exit(1);
823 } else if (!strcmp(r, "B")) {
824 guest_base = strtol(argv[optind++], NULL, 0);
825 have_guest_base = 1;
826 } else if (!strcmp(r, "drop-ld-preload")) {
827 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
828 } else if (!strcmp(r, "bsd")) {
829 if (!strcasecmp(argv[optind], "freebsd")) {
830 bsd_type = target_freebsd;
831 } else if (!strcasecmp(argv[optind], "netbsd")) {
832 bsd_type = target_netbsd;
833 } else if (!strcasecmp(argv[optind], "openbsd")) {
834 bsd_type = target_openbsd;
835 } else {
836 usage();
838 optind++;
839 } else if (!strcmp(r, "singlestep")) {
840 singlestep = 1;
841 } else if (!strcmp(r, "strace")) {
842 do_strace = 1;
843 } else
845 usage();
849 /* init debug */
850 qemu_log_needs_buffers();
851 qemu_set_log_filename(log_file, &error_fatal);
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;