linux-user/sparc: Remove target_rt_signal_frame as unused
[qemu/ar7.git] / bsd-user / main.c
blob715129e624fabdf80127fa94fbee0e5a0fbbccfc
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/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qemu/units.h"
23 #include "qemu/accel.h"
24 #include "sysemu/tcg.h"
25 #include "qemu-version.h"
26 #include <machine/trap.h>
28 #include "qapi/error.h"
29 #include "qemu.h"
30 #include "qemu/config-file.h"
31 #include "qemu/error-report.h"
32 #include "qemu/path.h"
33 #include "qemu/help_option.h"
34 #include "qemu/module.h"
35 #include "exec/exec-all.h"
36 #include "tcg/tcg.h"
37 #include "qemu/timer.h"
38 #include "qemu/envlist.h"
39 #include "exec/log.h"
40 #include "trace/control.h"
42 int singlestep;
43 unsigned long mmap_min_addr;
44 uintptr_t guest_base;
45 bool have_guest_base;
46 unsigned long reserved_va;
48 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
49 const char *qemu_uname_release;
50 extern char **environ;
51 enum BSDType bsd_type;
53 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
54 we allocate a bigger stack. Need a better solution, for example
55 by remapping the process stack directly at the right place */
56 unsigned long x86_stack_size = 512 * 1024;
58 void gemu_log(const char *fmt, ...)
60 va_list ap;
62 va_start(ap, fmt);
63 vfprintf(stderr, fmt, ap);
64 va_end(ap);
67 #if defined(TARGET_I386)
68 int cpu_get_pic_interrupt(CPUX86State *env)
70 return -1;
72 #endif
74 void fork_start(void)
78 void fork_end(int child)
80 if (child) {
81 gdbserver_fork(thread_cpu);
85 #ifdef TARGET_I386
86 /***********************************************************/
87 /* CPUX86 core interface */
89 uint64_t cpu_get_tsc(CPUX86State *env)
91 return cpu_get_host_ticks();
94 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
95 int flags)
97 unsigned int e1, e2;
98 uint32_t *p;
99 e1 = (addr << 16) | (limit & 0xffff);
100 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
101 e2 |= flags;
102 p = ptr;
103 p[0] = tswap32(e1);
104 p[1] = tswap32(e2);
107 static uint64_t *idt_table;
108 #ifdef TARGET_X86_64
109 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
110 uint64_t addr, unsigned int sel)
112 uint32_t *p, e1, e2;
113 e1 = (addr & 0xffff) | (sel << 16);
114 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
115 p = ptr;
116 p[0] = tswap32(e1);
117 p[1] = tswap32(e2);
118 p[2] = tswap32(addr >> 32);
119 p[3] = 0;
121 /* only dpl matters as we do only user space emulation */
122 static void set_idt(int n, unsigned int dpl)
124 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
126 #else
127 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
128 uint32_t addr, unsigned int sel)
130 uint32_t *p, e1, e2;
131 e1 = (addr & 0xffff) | (sel << 16);
132 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
133 p = ptr;
134 p[0] = tswap32(e1);
135 p[1] = tswap32(e2);
138 /* only dpl matters as we do only user space emulation */
139 static void set_idt(int n, unsigned int dpl)
141 set_gate(idt_table + n, 0, dpl, 0, 0);
143 #endif
145 void cpu_loop(CPUX86State *env)
147 CPUState *cs = env_cpu(env);
148 int trapnr;
149 abi_ulong pc;
150 //target_siginfo_t info;
152 for(;;) {
153 cpu_exec_start(cs);
154 trapnr = cpu_exec(cs);
155 cpu_exec_end(cs);
156 process_queued_cpu_work(cs);
158 switch(trapnr) {
159 case 0x80:
160 /* syscall from int $0x80 */
161 if (bsd_type == target_freebsd) {
162 abi_ulong params = (abi_ulong) env->regs[R_ESP] +
163 sizeof(int32_t);
164 int32_t syscall_nr = env->regs[R_EAX];
165 int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
167 if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
168 get_user_s32(syscall_nr, params);
169 params += sizeof(int32_t);
170 } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
171 get_user_s32(syscall_nr, params);
172 params += sizeof(int64_t);
174 get_user_s32(arg1, params);
175 params += sizeof(int32_t);
176 get_user_s32(arg2, params);
177 params += sizeof(int32_t);
178 get_user_s32(arg3, params);
179 params += sizeof(int32_t);
180 get_user_s32(arg4, params);
181 params += sizeof(int32_t);
182 get_user_s32(arg5, params);
183 params += sizeof(int32_t);
184 get_user_s32(arg6, params);
185 params += sizeof(int32_t);
186 get_user_s32(arg7, params);
187 params += sizeof(int32_t);
188 get_user_s32(arg8, params);
189 env->regs[R_EAX] = do_freebsd_syscall(env,
190 syscall_nr,
191 arg1,
192 arg2,
193 arg3,
194 arg4,
195 arg5,
196 arg6,
197 arg7,
198 arg8);
199 } else { //if (bsd_type == target_openbsd)
200 env->regs[R_EAX] = do_openbsd_syscall(env,
201 env->regs[R_EAX],
202 env->regs[R_EBX],
203 env->regs[R_ECX],
204 env->regs[R_EDX],
205 env->regs[R_ESI],
206 env->regs[R_EDI],
207 env->regs[R_EBP]);
209 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
210 env->regs[R_EAX] = -env->regs[R_EAX];
211 env->eflags |= CC_C;
212 } else {
213 env->eflags &= ~CC_C;
215 break;
216 #ifndef TARGET_ABI32
217 case EXCP_SYSCALL:
218 /* syscall from syscall instruction */
219 if (bsd_type == target_freebsd)
220 env->regs[R_EAX] = do_freebsd_syscall(env,
221 env->regs[R_EAX],
222 env->regs[R_EDI],
223 env->regs[R_ESI],
224 env->regs[R_EDX],
225 env->regs[R_ECX],
226 env->regs[8],
227 env->regs[9], 0, 0);
228 else { //if (bsd_type == target_openbsd)
229 env->regs[R_EAX] = do_openbsd_syscall(env,
230 env->regs[R_EAX],
231 env->regs[R_EDI],
232 env->regs[R_ESI],
233 env->regs[R_EDX],
234 env->regs[10],
235 env->regs[8],
236 env->regs[9]);
238 env->eip = env->exception_next_eip;
239 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
240 env->regs[R_EAX] = -env->regs[R_EAX];
241 env->eflags |= CC_C;
242 } else {
243 env->eflags &= ~CC_C;
245 break;
246 #endif
247 #if 0
248 case EXCP0B_NOSEG:
249 case EXCP0C_STACK:
250 info.si_signo = SIGBUS;
251 info.si_errno = 0;
252 info.si_code = TARGET_SI_KERNEL;
253 info._sifields._sigfault._addr = 0;
254 queue_signal(env, info.si_signo, &info);
255 break;
256 case EXCP0D_GPF:
257 /* XXX: potential problem if ABI32 */
258 #ifndef TARGET_X86_64
259 if (env->eflags & VM_MASK) {
260 handle_vm86_fault(env);
261 } else
262 #endif
264 info.si_signo = SIGSEGV;
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);
270 break;
271 case EXCP0E_PAGE:
272 info.si_signo = SIGSEGV;
273 info.si_errno = 0;
274 if (!(env->error_code & 1))
275 info.si_code = TARGET_SEGV_MAPERR;
276 else
277 info.si_code = TARGET_SEGV_ACCERR;
278 info._sifields._sigfault._addr = env->cr[2];
279 queue_signal(env, info.si_signo, &info);
280 break;
281 case EXCP00_DIVZ:
282 #ifndef TARGET_X86_64
283 if (env->eflags & VM_MASK) {
284 handle_vm86_trap(env, trapnr);
285 } else
286 #endif
288 /* division by zero */
289 info.si_signo = SIGFPE;
290 info.si_errno = 0;
291 info.si_code = TARGET_FPE_INTDIV;
292 info._sifields._sigfault._addr = env->eip;
293 queue_signal(env, info.si_signo, &info);
295 break;
296 case EXCP01_DB:
297 case EXCP03_INT3:
298 #ifndef TARGET_X86_64
299 if (env->eflags & VM_MASK) {
300 handle_vm86_trap(env, trapnr);
301 } else
302 #endif
304 info.si_signo = SIGTRAP;
305 info.si_errno = 0;
306 if (trapnr == EXCP01_DB) {
307 info.si_code = TARGET_TRAP_BRKPT;
308 info._sifields._sigfault._addr = env->eip;
309 } else {
310 info.si_code = TARGET_SI_KERNEL;
311 info._sifields._sigfault._addr = 0;
313 queue_signal(env, info.si_signo, &info);
315 break;
316 case EXCP04_INTO:
317 case EXCP05_BOUND:
318 #ifndef TARGET_X86_64
319 if (env->eflags & VM_MASK) {
320 handle_vm86_trap(env, trapnr);
321 } else
322 #endif
324 info.si_signo = SIGSEGV;
325 info.si_errno = 0;
326 info.si_code = TARGET_SI_KERNEL;
327 info._sifields._sigfault._addr = 0;
328 queue_signal(env, info.si_signo, &info);
330 break;
331 case EXCP06_ILLOP:
332 info.si_signo = SIGILL;
333 info.si_errno = 0;
334 info.si_code = TARGET_ILL_ILLOPN;
335 info._sifields._sigfault._addr = env->eip;
336 queue_signal(env, info.si_signo, &info);
337 break;
338 #endif
339 case EXCP_INTERRUPT:
340 /* just indicate that signals should be handled asap */
341 break;
342 #if 0
343 case EXCP_DEBUG:
345 int sig;
347 sig = gdb_handlesig (env, TARGET_SIGTRAP);
348 if (sig)
350 info.si_signo = sig;
351 info.si_errno = 0;
352 info.si_code = TARGET_TRAP_BRKPT;
353 queue_signal(env, info.si_signo, &info);
356 break;
357 #endif
358 default:
359 pc = env->segs[R_CS].base + env->eip;
360 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
361 (long)pc, trapnr);
362 abort();
364 process_pending_signals(env);
367 #endif
369 #ifdef TARGET_SPARC
370 #define SPARC64_STACK_BIAS 2047
372 //#define DEBUG_WIN
373 /* WARNING: dealing with register windows _is_ complicated. More info
374 can be found at http://www.sics.se/~psm/sparcstack.html */
375 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
377 index = (index + cwp * 16) % (16 * env->nwindows);
378 /* wrap handling : if cwp is on the last window, then we use the
379 registers 'after' the end */
380 if (index < 8 && env->cwp == env->nwindows - 1)
381 index += 16 * env->nwindows;
382 return index;
385 /* save the register window 'cwp1' */
386 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
388 unsigned int i;
389 abi_ulong sp_ptr;
391 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
392 #ifdef TARGET_SPARC64
393 if (sp_ptr & 3)
394 sp_ptr += SPARC64_STACK_BIAS;
395 #endif
396 #if defined(DEBUG_WIN)
397 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
398 sp_ptr, cwp1);
399 #endif
400 for(i = 0; i < 16; i++) {
401 /* FIXME - what to do if put_user() fails? */
402 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
403 sp_ptr += sizeof(abi_ulong);
407 static void save_window(CPUSPARCState *env)
409 #ifndef TARGET_SPARC64
410 unsigned int new_wim;
411 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
412 ((1LL << env->nwindows) - 1);
413 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
414 env->wim = new_wim;
415 #else
417 * cansave is zero if the spill trap handler is triggered by `save` and
418 * nonzero if triggered by a `flushw`
420 save_window_offset(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2));
421 env->cansave++;
422 env->canrestore--;
423 #endif
426 static void restore_window(CPUSPARCState *env)
428 #ifndef TARGET_SPARC64
429 unsigned int new_wim;
430 #endif
431 unsigned int i, cwp1;
432 abi_ulong sp_ptr;
434 #ifndef TARGET_SPARC64
435 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
436 ((1LL << env->nwindows) - 1);
437 #endif
439 /* restore the invalid window */
440 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
441 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
442 #ifdef TARGET_SPARC64
443 if (sp_ptr & 3)
444 sp_ptr += SPARC64_STACK_BIAS;
445 #endif
446 #if defined(DEBUG_WIN)
447 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
448 sp_ptr, cwp1);
449 #endif
450 for(i = 0; i < 16; i++) {
451 /* FIXME - what to do if get_user() fails? */
452 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
453 sp_ptr += sizeof(abi_ulong);
455 #ifdef TARGET_SPARC64
456 env->canrestore++;
457 if (env->cleanwin < env->nwindows - 1)
458 env->cleanwin++;
459 env->cansave--;
460 #else
461 env->wim = new_wim;
462 #endif
465 static void flush_windows(CPUSPARCState *env)
467 int offset, cwp1;
469 offset = 1;
470 for(;;) {
471 /* if restore would invoke restore_window(), then we can stop */
472 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
473 #ifndef TARGET_SPARC64
474 if (env->wim & (1 << cwp1))
475 break;
476 #else
477 if (env->canrestore == 0)
478 break;
479 env->cansave++;
480 env->canrestore--;
481 #endif
482 save_window_offset(env, cwp1);
483 offset++;
485 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
486 #ifndef TARGET_SPARC64
487 /* set wim so that restore will reload the registers */
488 env->wim = 1 << cwp1;
489 #endif
490 #if defined(DEBUG_WIN)
491 printf("flush_windows: nb=%d\n", offset - 1);
492 #endif
495 void cpu_loop(CPUSPARCState *env)
497 CPUState *cs = env_cpu(env);
498 int trapnr, ret, syscall_nr;
499 //target_siginfo_t info;
501 while (1) {
502 cpu_exec_start(cs);
503 trapnr = cpu_exec(cs);
504 cpu_exec_end(cs);
505 process_queued_cpu_work(cs);
507 switch (trapnr) {
508 #ifndef TARGET_SPARC64
509 case 0x80:
510 #else
511 /* FreeBSD uses 0x141 for syscalls too */
512 case 0x141:
513 if (bsd_type != target_freebsd)
514 goto badtrap;
515 /* fallthrough */
516 case 0x100:
517 #endif
518 syscall_nr = env->gregs[1];
519 if (bsd_type == target_freebsd)
520 ret = do_freebsd_syscall(env, syscall_nr,
521 env->regwptr[0], env->regwptr[1],
522 env->regwptr[2], env->regwptr[3],
523 env->regwptr[4], env->regwptr[5], 0, 0);
524 else if (bsd_type == target_netbsd)
525 ret = do_netbsd_syscall(env, syscall_nr,
526 env->regwptr[0], env->regwptr[1],
527 env->regwptr[2], env->regwptr[3],
528 env->regwptr[4], env->regwptr[5]);
529 else { //if (bsd_type == target_openbsd)
530 #if defined(TARGET_SPARC64)
531 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
532 TARGET_OPENBSD_SYSCALL_G2RFLAG);
533 #endif
534 ret = do_openbsd_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]);
539 if ((unsigned int)ret >= (unsigned int)(-515)) {
540 ret = -ret;
541 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
542 env->xcc |= PSR_CARRY;
543 #else
544 env->psr |= PSR_CARRY;
545 #endif
546 } else {
547 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
548 env->xcc &= ~PSR_CARRY;
549 #else
550 env->psr &= ~PSR_CARRY;
551 #endif
553 env->regwptr[0] = ret;
554 /* next instruction */
555 #if defined(TARGET_SPARC64)
556 if (bsd_type == target_openbsd &&
557 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
558 env->pc = env->gregs[2];
559 env->npc = env->pc + 4;
560 } else if (bsd_type == target_openbsd &&
561 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
562 env->pc = env->gregs[7];
563 env->npc = env->pc + 4;
564 } else {
565 env->pc = env->npc;
566 env->npc = env->npc + 4;
568 #else
569 env->pc = env->npc;
570 env->npc = env->npc + 4;
571 #endif
572 break;
573 case 0x83: /* flush windows */
574 #ifdef TARGET_ABI32
575 case 0x103:
576 #endif
577 flush_windows(env);
578 /* next instruction */
579 env->pc = env->npc;
580 env->npc = env->npc + 4;
581 break;
582 #ifndef TARGET_SPARC64
583 case TT_WIN_OVF: /* window overflow */
584 save_window(env);
585 break;
586 case TT_WIN_UNF: /* window underflow */
587 restore_window(env);
588 break;
589 case TT_TFAULT:
590 case TT_DFAULT:
591 #if 0
593 info.si_signo = SIGSEGV;
594 info.si_errno = 0;
595 /* XXX: check env->error_code */
596 info.si_code = TARGET_SEGV_MAPERR;
597 info._sifields._sigfault._addr = env->mmuregs[4];
598 queue_signal(env, info.si_signo, &info);
600 #endif
601 break;
602 #else
603 case TT_SPILL: /* window overflow */
604 save_window(env);
605 break;
606 case TT_FILL: /* window underflow */
607 restore_window(env);
608 break;
609 case TT_TFAULT:
610 case TT_DFAULT:
611 #if 0
613 info.si_signo = SIGSEGV;
614 info.si_errno = 0;
615 /* XXX: check env->error_code */
616 info.si_code = TARGET_SEGV_MAPERR;
617 if (trapnr == TT_DFAULT)
618 info._sifields._sigfault._addr = env->dmmuregs[4];
619 else
620 info._sifields._sigfault._addr = env->tsptr->tpc;
621 //queue_signal(env, info.si_signo, &info);
623 #endif
624 break;
625 #endif
626 case EXCP_INTERRUPT:
627 /* just indicate that signals should be handled asap */
628 break;
629 case EXCP_DEBUG:
631 #if 0
632 int sig =
633 #endif
634 gdb_handlesig(cs, TARGET_SIGTRAP);
635 #if 0
636 if (sig)
638 info.si_signo = sig;
639 info.si_errno = 0;
640 info.si_code = TARGET_TRAP_BRKPT;
641 //queue_signal(env, info.si_signo, &info);
643 #endif
645 break;
646 default:
647 #ifdef TARGET_SPARC64
648 badtrap:
649 #endif
650 printf ("Unhandled trap: 0x%x\n", trapnr);
651 cpu_dump_state(cs, stderr, 0);
652 exit (1);
654 process_pending_signals (env);
658 #endif
660 static void usage(void)
662 printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
663 "\n" QEMU_COPYRIGHT "\n"
664 "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
665 "BSD CPU emulator (compiled for %s emulation)\n"
666 "\n"
667 "Standard options:\n"
668 "-h print this help\n"
669 "-g port wait gdb connection to port\n"
670 "-L path set the elf interpreter prefix (default=%s)\n"
671 "-s size set the stack size in bytes (default=%ld)\n"
672 "-cpu model select CPU (-cpu help for list)\n"
673 "-drop-ld-preload drop LD_PRELOAD for target process\n"
674 "-E var=value sets/modifies targets environment variable(s)\n"
675 "-U var unsets targets environment variable(s)\n"
676 "-B address set guest_base address to address\n"
677 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
678 "\n"
679 "Debug options:\n"
680 "-d item1[,...] enable logging of specified items\n"
681 " (use '-d help' for a list of log items)\n"
682 "-D logfile write logs to 'logfile' (default stderr)\n"
683 "-p pagesize set the host page size to 'pagesize'\n"
684 "-singlestep always run in singlestep mode\n"
685 "-strace log system calls\n"
686 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
687 " specify tracing options\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"
698 "\n"
699 QEMU_HELP_BOTTOM "\n"
701 TARGET_NAME,
702 interp_prefix,
703 x86_stack_size);
704 exit(1);
707 THREAD CPUState *thread_cpu;
709 bool qemu_cpu_is_self(CPUState *cpu)
711 return thread_cpu == cpu;
714 void qemu_cpu_kick(CPUState *cpu)
716 cpu_exit(cpu);
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 *cpu_type;
737 const char *log_file = NULL;
738 const char *log_mask = NULL;
739 struct target_pt_regs regs1, *regs = &regs1;
740 struct image_info info1, *info = &info1;
741 TaskState ts1, *ts = &ts1;
742 CPUArchState *env;
743 CPUState *cpu;
744 int optind;
745 const char *r;
746 const char *gdbstub = NULL;
747 char **target_environ, **wrk;
748 envlist_t *envlist = NULL;
749 bsd_type = target_openbsd;
751 if (argc <= 1)
752 usage();
754 error_init(argv[0]);
755 module_call_init(MODULE_INIT_TRACE);
756 qemu_init_cpu_list();
757 module_call_init(MODULE_INIT_QOM);
759 envlist = envlist_create();
761 /* add current environment into the list */
762 for (wrk = environ; *wrk != NULL; wrk++) {
763 (void) envlist_setenv(envlist, *wrk);
766 cpu_model = NULL;
768 qemu_add_opts(&qemu_trace_opts);
770 optind = 1;
771 for (;;) {
772 if (optind >= argc)
773 break;
774 r = argv[optind];
775 if (r[0] != '-')
776 break;
777 optind++;
778 r++;
779 if (!strcmp(r, "-")) {
780 break;
781 } else if (!strcmp(r, "d")) {
782 if (optind >= argc) {
783 break;
785 log_mask = argv[optind++];
786 } else if (!strcmp(r, "D")) {
787 if (optind >= argc) {
788 break;
790 log_file = argv[optind++];
791 } else if (!strcmp(r, "E")) {
792 r = argv[optind++];
793 if (envlist_setenv(envlist, r) != 0)
794 usage();
795 } else if (!strcmp(r, "ignore-environment")) {
796 envlist_free(envlist);
797 envlist = envlist_create();
798 } else if (!strcmp(r, "U")) {
799 r = argv[optind++];
800 if (envlist_unsetenv(envlist, r) != 0)
801 usage();
802 } else if (!strcmp(r, "s")) {
803 r = argv[optind++];
804 x86_stack_size = strtol(r, (char **)&r, 0);
805 if (x86_stack_size <= 0)
806 usage();
807 if (*r == 'M')
808 x86_stack_size *= MiB;
809 else if (*r == 'k' || *r == 'K')
810 x86_stack_size *= KiB;
811 } else if (!strcmp(r, "L")) {
812 interp_prefix = argv[optind++];
813 } else if (!strcmp(r, "p")) {
814 qemu_host_page_size = atoi(argv[optind++]);
815 if (qemu_host_page_size == 0 ||
816 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
817 fprintf(stderr, "page size must be a power of two\n");
818 exit(1);
820 } else if (!strcmp(r, "g")) {
821 gdbstub = g_strdup(argv[optind++]);
822 } else if (!strcmp(r, "r")) {
823 qemu_uname_release = argv[optind++];
824 } else if (!strcmp(r, "cpu")) {
825 cpu_model = argv[optind++];
826 if (is_help_option(cpu_model)) {
827 /* XXX: implement xxx_cpu_list for targets that still miss it */
828 #if defined(cpu_list)
829 cpu_list();
830 #endif
831 exit(1);
833 } else if (!strcmp(r, "B")) {
834 guest_base = strtol(argv[optind++], NULL, 0);
835 have_guest_base = true;
836 } else if (!strcmp(r, "drop-ld-preload")) {
837 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
838 } else if (!strcmp(r, "bsd")) {
839 if (!strcasecmp(argv[optind], "freebsd")) {
840 bsd_type = target_freebsd;
841 } else if (!strcasecmp(argv[optind], "netbsd")) {
842 bsd_type = target_netbsd;
843 } else if (!strcasecmp(argv[optind], "openbsd")) {
844 bsd_type = target_openbsd;
845 } else {
846 usage();
848 optind++;
849 } else if (!strcmp(r, "singlestep")) {
850 singlestep = 1;
851 } else if (!strcmp(r, "strace")) {
852 do_strace = 1;
853 } else if (!strcmp(r, "trace")) {
854 trace_opt_parse(optarg);
855 } else {
856 usage();
860 /* init debug */
861 qemu_log_needs_buffers();
862 qemu_set_log_filename(log_file, &error_fatal);
863 if (log_mask) {
864 int mask;
866 mask = qemu_str_to_log_mask(log_mask);
867 if (!mask) {
868 qemu_print_log_usage(stdout);
869 exit(1);
871 qemu_set_log(mask);
874 if (optind >= argc) {
875 usage();
877 filename = argv[optind];
879 if (!trace_init_backends()) {
880 exit(1);
882 trace_init_file();
884 /* Zero out regs */
885 memset(regs, 0, sizeof(struct target_pt_regs));
887 /* Zero out image_info */
888 memset(info, 0, sizeof(struct image_info));
890 /* Scan interp_prefix dir for replacement files. */
891 init_paths(interp_prefix);
893 if (cpu_model == NULL) {
894 #if defined(TARGET_I386)
895 #ifdef TARGET_X86_64
896 cpu_model = "qemu64";
897 #else
898 cpu_model = "qemu32";
899 #endif
900 #elif defined(TARGET_SPARC)
901 #ifdef TARGET_SPARC64
902 cpu_model = "TI UltraSparc II";
903 #else
904 cpu_model = "Fujitsu MB86904";
905 #endif
906 #else
907 cpu_model = "any";
908 #endif
911 cpu_type = parse_cpu_option(cpu_model);
912 /* init tcg before creating CPUs and to get qemu_host_page_size */
914 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
916 accel_init_interfaces(ac);
917 ac->init_machine(NULL);
919 cpu = cpu_create(cpu_type);
920 env = cpu->env_ptr;
921 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
922 cpu_reset(cpu);
923 #endif
924 thread_cpu = cpu;
926 if (getenv("QEMU_STRACE")) {
927 do_strace = 1;
930 target_environ = envlist_to_environ(envlist, NULL);
931 envlist_free(envlist);
934 * Now that page sizes are configured in tcg_exec_init() we can do
935 * proper page alignment for guest_base.
937 guest_base = HOST_PAGE_ALIGN(guest_base);
940 * Read in mmap_min_addr kernel parameter. This value is used
941 * When loading the ELF image to determine whether guest_base
942 * is needed.
944 * When user has explicitly set the quest base, we skip this
945 * test.
947 if (!have_guest_base) {
948 FILE *fp;
950 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
951 unsigned long tmp;
952 if (fscanf(fp, "%lu", &tmp) == 1) {
953 mmap_min_addr = tmp;
954 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr);
956 fclose(fp);
960 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
961 printf("Error loading %s\n", filename);
962 _exit(1);
965 for (wrk = target_environ; *wrk; wrk++) {
966 g_free(*wrk);
969 g_free(target_environ);
971 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
972 qemu_log("guest_base %p\n", (void *)guest_base);
973 log_page_dump("binary load");
975 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
976 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
977 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
978 info->start_code);
979 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
980 info->start_data);
981 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
982 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
983 info->start_stack);
984 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
985 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
988 target_set_brk(info->brk);
989 syscall_init();
990 signal_init();
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 tcg_region_init();
998 /* build Task State */
999 memset(ts, 0, sizeof(TaskState));
1000 init_task_state(ts);
1001 ts->info = info;
1002 cpu->opaque = ts;
1004 #if defined(TARGET_I386)
1005 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
1006 env->hflags |= HF_PE_MASK | HF_CPL_MASK;
1007 if (env->features[FEAT_1_EDX] & CPUID_SSE) {
1008 env->cr[4] |= CR4_OSFXSR_MASK;
1009 env->hflags |= HF_OSFXSR_MASK;
1011 #ifndef TARGET_ABI32
1012 /* enable 64 bit mode if possible */
1013 if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
1014 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
1015 exit(1);
1017 env->cr[4] |= CR4_PAE_MASK;
1018 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
1019 env->hflags |= HF_LMA_MASK;
1020 #endif
1022 /* flags setup : we activate the IRQs by default as in user mode */
1023 env->eflags |= IF_MASK;
1025 /* linux register setup */
1026 #ifndef TARGET_ABI32
1027 env->regs[R_EAX] = regs->rax;
1028 env->regs[R_EBX] = regs->rbx;
1029 env->regs[R_ECX] = regs->rcx;
1030 env->regs[R_EDX] = regs->rdx;
1031 env->regs[R_ESI] = regs->rsi;
1032 env->regs[R_EDI] = regs->rdi;
1033 env->regs[R_EBP] = regs->rbp;
1034 env->regs[R_ESP] = regs->rsp;
1035 env->eip = regs->rip;
1036 #else
1037 env->regs[R_EAX] = regs->eax;
1038 env->regs[R_EBX] = regs->ebx;
1039 env->regs[R_ECX] = regs->ecx;
1040 env->regs[R_EDX] = regs->edx;
1041 env->regs[R_ESI] = regs->esi;
1042 env->regs[R_EDI] = regs->edi;
1043 env->regs[R_EBP] = regs->ebp;
1044 env->regs[R_ESP] = regs->esp;
1045 env->eip = regs->eip;
1046 #endif
1048 /* linux interrupt setup */
1049 #ifndef TARGET_ABI32
1050 env->idt.limit = 511;
1051 #else
1052 env->idt.limit = 255;
1053 #endif
1054 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
1055 PROT_READ|PROT_WRITE,
1056 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1057 idt_table = g2h_untagged(env->idt.base);
1058 set_idt(0, 0);
1059 set_idt(1, 0);
1060 set_idt(2, 0);
1061 set_idt(3, 3);
1062 set_idt(4, 3);
1063 set_idt(5, 0);
1064 set_idt(6, 0);
1065 set_idt(7, 0);
1066 set_idt(8, 0);
1067 set_idt(9, 0);
1068 set_idt(10, 0);
1069 set_idt(11, 0);
1070 set_idt(12, 0);
1071 set_idt(13, 0);
1072 set_idt(14, 0);
1073 set_idt(15, 0);
1074 set_idt(16, 0);
1075 set_idt(17, 0);
1076 set_idt(18, 0);
1077 set_idt(19, 0);
1078 set_idt(0x80, 3);
1080 /* linux segment setup */
1082 uint64_t *gdt_table;
1083 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
1084 PROT_READ|PROT_WRITE,
1085 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1086 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
1087 gdt_table = g2h_untagged(env->gdt.base);
1088 #ifdef TARGET_ABI32
1089 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1090 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1091 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1092 #else
1093 /* 64 bit code segment */
1094 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1095 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1096 DESC_L_MASK |
1097 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1098 #endif
1099 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
1100 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1101 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
1104 cpu_x86_load_seg(env, R_CS, __USER_CS);
1105 cpu_x86_load_seg(env, R_SS, __USER_DS);
1106 #ifdef TARGET_ABI32
1107 cpu_x86_load_seg(env, R_DS, __USER_DS);
1108 cpu_x86_load_seg(env, R_ES, __USER_DS);
1109 cpu_x86_load_seg(env, R_FS, __USER_DS);
1110 cpu_x86_load_seg(env, R_GS, __USER_DS);
1111 /* This hack makes Wine work... */
1112 env->segs[R_FS].selector = 0;
1113 #else
1114 cpu_x86_load_seg(env, R_DS, 0);
1115 cpu_x86_load_seg(env, R_ES, 0);
1116 cpu_x86_load_seg(env, R_FS, 0);
1117 cpu_x86_load_seg(env, R_GS, 0);
1118 #endif
1119 #elif defined(TARGET_SPARC)
1121 int i;
1122 env->pc = regs->pc;
1123 env->npc = regs->npc;
1124 env->y = regs->y;
1125 for(i = 0; i < 8; i++)
1126 env->gregs[i] = regs->u_regs[i];
1127 for(i = 0; i < 8; i++)
1128 env->regwptr[i] = regs->u_regs[i + 8];
1130 #else
1131 #error unsupported target CPU
1132 #endif
1134 if (gdbstub) {
1135 gdbserver_start(gdbstub);
1136 gdb_handlesig(cpu, 0);
1138 cpu_loop(env);
1139 /* never exits */
1140 return 0;