Add bdrv_aio_multiwrite
[qemu/qemu-dev-zwu.git] / bsd-user / main.c
blob56710ec79ded5c1f4be0044abd64e834538eaa85
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 "exec-all.h"
35 #include "envlist.h"
37 #define DEBUG_LOGFILE "/tmp/qemu.log"
39 int singlestep;
41 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
42 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
43 extern char **environ;
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(CPUState *env)
62 return -1;
64 #endif
66 /* These are no-ops because we are not threadsafe. */
67 static inline void cpu_exec_start(CPUState *env)
71 static inline void cpu_exec_end(CPUState *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_env);
94 void cpu_list_lock(void)
98 void cpu_list_unlock(void)
102 #ifdef TARGET_I386
103 /***********************************************************/
104 /* CPUX86 core interface */
106 void cpu_smm_update(CPUState *env)
110 uint64_t cpu_get_tsc(CPUX86State *env)
112 return cpu_get_real_ticks();
115 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
116 int flags)
118 unsigned int e1, e2;
119 uint32_t *p;
120 e1 = (addr << 16) | (limit & 0xffff);
121 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
122 e2 |= flags;
123 p = ptr;
124 p[0] = tswap32(e1);
125 p[1] = tswap32(e2);
128 static uint64_t *idt_table;
129 #ifdef TARGET_X86_64
130 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
131 uint64_t addr, unsigned int sel)
133 uint32_t *p, e1, e2;
134 e1 = (addr & 0xffff) | (sel << 16);
135 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
136 p = ptr;
137 p[0] = tswap32(e1);
138 p[1] = tswap32(e2);
139 p[2] = tswap32(addr >> 32);
140 p[3] = 0;
142 /* only dpl matters as we do only user space emulation */
143 static void set_idt(int n, unsigned int dpl)
145 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
147 #else
148 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
149 uint32_t addr, unsigned int sel)
151 uint32_t *p, e1, e2;
152 e1 = (addr & 0xffff) | (sel << 16);
153 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
154 p = ptr;
155 p[0] = tswap32(e1);
156 p[1] = tswap32(e2);
159 /* only dpl matters as we do only user space emulation */
160 static void set_idt(int n, unsigned int dpl)
162 set_gate(idt_table + n, 0, dpl, 0, 0);
164 #endif
166 void cpu_loop(CPUX86State *env, enum BSDType bsd_type)
168 int trapnr;
169 abi_ulong pc;
170 //target_siginfo_t info;
172 for(;;) {
173 trapnr = cpu_x86_exec(env);
174 switch(trapnr) {
175 case 0x80:
176 /* syscall from int $0x80 */
177 env->regs[R_EAX] = do_openbsd_syscall(env,
178 env->regs[R_EAX],
179 env->regs[R_EBX],
180 env->regs[R_ECX],
181 env->regs[R_EDX],
182 env->regs[R_ESI],
183 env->regs[R_EDI],
184 env->regs[R_EBP]);
185 break;
186 #ifndef TARGET_ABI32
187 case EXCP_SYSCALL:
188 /* linux syscall from syscall intruction */
189 env->regs[R_EAX] = do_openbsd_syscall(env,
190 env->regs[R_EAX],
191 env->regs[R_EDI],
192 env->regs[R_ESI],
193 env->regs[R_EDX],
194 env->regs[10],
195 env->regs[8],
196 env->regs[9]);
197 env->eip = env->exception_next_eip;
198 break;
199 #endif
200 #if 0
201 case EXCP0B_NOSEG:
202 case EXCP0C_STACK:
203 info.si_signo = SIGBUS;
204 info.si_errno = 0;
205 info.si_code = TARGET_SI_KERNEL;
206 info._sifields._sigfault._addr = 0;
207 queue_signal(env, info.si_signo, &info);
208 break;
209 case EXCP0D_GPF:
210 /* XXX: potential problem if ABI32 */
211 #ifndef TARGET_X86_64
212 if (env->eflags & VM_MASK) {
213 handle_vm86_fault(env);
214 } else
215 #endif
217 info.si_signo = SIGSEGV;
218 info.si_errno = 0;
219 info.si_code = TARGET_SI_KERNEL;
220 info._sifields._sigfault._addr = 0;
221 queue_signal(env, info.si_signo, &info);
223 break;
224 case EXCP0E_PAGE:
225 info.si_signo = SIGSEGV;
226 info.si_errno = 0;
227 if (!(env->error_code & 1))
228 info.si_code = TARGET_SEGV_MAPERR;
229 else
230 info.si_code = TARGET_SEGV_ACCERR;
231 info._sifields._sigfault._addr = env->cr[2];
232 queue_signal(env, info.si_signo, &info);
233 break;
234 case EXCP00_DIVZ:
235 #ifndef TARGET_X86_64
236 if (env->eflags & VM_MASK) {
237 handle_vm86_trap(env, trapnr);
238 } else
239 #endif
241 /* division by zero */
242 info.si_signo = SIGFPE;
243 info.si_errno = 0;
244 info.si_code = TARGET_FPE_INTDIV;
245 info._sifields._sigfault._addr = env->eip;
246 queue_signal(env, info.si_signo, &info);
248 break;
249 case EXCP01_DB:
250 case EXCP03_INT3:
251 #ifndef TARGET_X86_64
252 if (env->eflags & VM_MASK) {
253 handle_vm86_trap(env, trapnr);
254 } else
255 #endif
257 info.si_signo = SIGTRAP;
258 info.si_errno = 0;
259 if (trapnr == EXCP01_DB) {
260 info.si_code = TARGET_TRAP_BRKPT;
261 info._sifields._sigfault._addr = env->eip;
262 } else {
263 info.si_code = TARGET_SI_KERNEL;
264 info._sifields._sigfault._addr = 0;
266 queue_signal(env, info.si_signo, &info);
268 break;
269 case EXCP04_INTO:
270 case EXCP05_BOUND:
271 #ifndef TARGET_X86_64
272 if (env->eflags & VM_MASK) {
273 handle_vm86_trap(env, trapnr);
274 } else
275 #endif
277 info.si_signo = SIGSEGV;
278 info.si_errno = 0;
279 info.si_code = TARGET_SI_KERNEL;
280 info._sifields._sigfault._addr = 0;
281 queue_signal(env, info.si_signo, &info);
283 break;
284 case EXCP06_ILLOP:
285 info.si_signo = SIGILL;
286 info.si_errno = 0;
287 info.si_code = TARGET_ILL_ILLOPN;
288 info._sifields._sigfault._addr = env->eip;
289 queue_signal(env, info.si_signo, &info);
290 break;
291 #endif
292 case EXCP_INTERRUPT:
293 /* just indicate that signals should be handled asap */
294 break;
295 #if 0
296 case EXCP_DEBUG:
298 int sig;
300 sig = gdb_handlesig (env, TARGET_SIGTRAP);
301 if (sig)
303 info.si_signo = sig;
304 info.si_errno = 0;
305 info.si_code = TARGET_TRAP_BRKPT;
306 queue_signal(env, info.si_signo, &info);
309 break;
310 #endif
311 default:
312 pc = env->segs[R_CS].base + env->eip;
313 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
314 (long)pc, trapnr);
315 abort();
317 process_pending_signals(env);
320 #endif
322 #ifdef TARGET_SPARC
323 #define SPARC64_STACK_BIAS 2047
325 //#define DEBUG_WIN
326 /* WARNING: dealing with register windows _is_ complicated. More info
327 can be found at http://www.sics.se/~psm/sparcstack.html */
328 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
330 index = (index + cwp * 16) % (16 * env->nwindows);
331 /* wrap handling : if cwp is on the last window, then we use the
332 registers 'after' the end */
333 if (index < 8 && env->cwp == env->nwindows - 1)
334 index += 16 * env->nwindows;
335 return index;
338 /* save the register window 'cwp1' */
339 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
341 unsigned int i;
342 abi_ulong sp_ptr;
344 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
345 #ifdef TARGET_SPARC64
346 if (sp_ptr & 3)
347 sp_ptr += SPARC64_STACK_BIAS;
348 #endif
349 #if defined(DEBUG_WIN)
350 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
351 sp_ptr, cwp1);
352 #endif
353 for(i = 0; i < 16; i++) {
354 /* FIXME - what to do if put_user() fails? */
355 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
356 sp_ptr += sizeof(abi_ulong);
360 static void save_window(CPUSPARCState *env)
362 #ifndef TARGET_SPARC64
363 unsigned int new_wim;
364 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
365 ((1LL << env->nwindows) - 1);
366 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
367 env->wim = new_wim;
368 #else
369 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
370 env->cansave++;
371 env->canrestore--;
372 #endif
375 static void restore_window(CPUSPARCState *env)
377 #ifndef TARGET_SPARC64
378 unsigned int new_wim;
379 #endif
380 unsigned int i, cwp1;
381 abi_ulong sp_ptr;
383 #ifndef TARGET_SPARC64
384 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
385 ((1LL << env->nwindows) - 1);
386 #endif
388 /* restore the invalid window */
389 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
390 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
391 #ifdef TARGET_SPARC64
392 if (sp_ptr & 3)
393 sp_ptr += SPARC64_STACK_BIAS;
394 #endif
395 #if defined(DEBUG_WIN)
396 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
397 sp_ptr, cwp1);
398 #endif
399 for(i = 0; i < 16; i++) {
400 /* FIXME - what to do if get_user() fails? */
401 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
402 sp_ptr += sizeof(abi_ulong);
404 #ifdef TARGET_SPARC64
405 env->canrestore++;
406 if (env->cleanwin < env->nwindows - 1)
407 env->cleanwin++;
408 env->cansave--;
409 #else
410 env->wim = new_wim;
411 #endif
414 static void flush_windows(CPUSPARCState *env)
416 int offset, cwp1;
418 offset = 1;
419 for(;;) {
420 /* if restore would invoke restore_window(), then we can stop */
421 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
422 #ifndef TARGET_SPARC64
423 if (env->wim & (1 << cwp1))
424 break;
425 #else
426 if (env->canrestore == 0)
427 break;
428 env->cansave++;
429 env->canrestore--;
430 #endif
431 save_window_offset(env, cwp1);
432 offset++;
434 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
435 #ifndef TARGET_SPARC64
436 /* set wim so that restore will reload the registers */
437 env->wim = 1 << cwp1;
438 #endif
439 #if defined(DEBUG_WIN)
440 printf("flush_windows: nb=%d\n", offset - 1);
441 #endif
444 void cpu_loop(CPUSPARCState *env, enum BSDType bsd_type)
446 int trapnr, ret, syscall_nr;
447 //target_siginfo_t info;
449 while (1) {
450 trapnr = cpu_sparc_exec (env);
452 switch (trapnr) {
453 #ifndef TARGET_SPARC64
454 case 0x80:
455 #else
456 case 0x100:
457 #endif
458 syscall_nr = env->gregs[1];
459 if (bsd_type == target_freebsd)
460 ret = do_freebsd_syscall(env, syscall_nr,
461 env->regwptr[0], env->regwptr[1],
462 env->regwptr[2], env->regwptr[3],
463 env->regwptr[4], env->regwptr[5]);
464 else if (bsd_type == target_netbsd)
465 ret = do_netbsd_syscall(env, syscall_nr,
466 env->regwptr[0], env->regwptr[1],
467 env->regwptr[2], env->regwptr[3],
468 env->regwptr[4], env->regwptr[5]);
469 else { //if (bsd_type == target_openbsd)
470 #if defined(TARGET_SPARC64)
471 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
472 TARGET_OPENBSD_SYSCALL_G2RFLAG);
473 #endif
474 ret = do_openbsd_syscall(env, syscall_nr,
475 env->regwptr[0], env->regwptr[1],
476 env->regwptr[2], env->regwptr[3],
477 env->regwptr[4], env->regwptr[5]);
479 if ((unsigned int)ret >= (unsigned int)(-515)) {
480 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
481 env->xcc |= PSR_CARRY;
482 #else
483 env->psr |= PSR_CARRY;
484 #endif
485 } else {
486 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
487 env->xcc &= ~PSR_CARRY;
488 #else
489 env->psr &= ~PSR_CARRY;
490 #endif
492 env->regwptr[0] = ret;
493 /* next instruction */
494 #if defined(TARGET_SPARC64)
495 if (bsd_type == target_openbsd &&
496 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
497 env->pc = env->gregs[2];
498 env->npc = env->pc + 4;
499 } else if (bsd_type == target_openbsd &&
500 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
501 env->pc = env->gregs[7];
502 env->npc = env->pc + 4;
503 } else {
504 env->pc = env->npc;
505 env->npc = env->npc + 4;
507 #else
508 env->pc = env->npc;
509 env->npc = env->npc + 4;
510 #endif
511 break;
512 case 0x83: /* flush windows */
513 #ifdef TARGET_ABI32
514 case 0x103:
515 #endif
516 flush_windows(env);
517 /* next instruction */
518 env->pc = env->npc;
519 env->npc = env->npc + 4;
520 break;
521 #ifndef TARGET_SPARC64
522 case TT_WIN_OVF: /* window overflow */
523 save_window(env);
524 break;
525 case TT_WIN_UNF: /* window underflow */
526 restore_window(env);
527 break;
528 case TT_TFAULT:
529 case TT_DFAULT:
530 #if 0
532 info.si_signo = SIGSEGV;
533 info.si_errno = 0;
534 /* XXX: check env->error_code */
535 info.si_code = TARGET_SEGV_MAPERR;
536 info._sifields._sigfault._addr = env->mmuregs[4];
537 queue_signal(env, info.si_signo, &info);
539 #endif
540 break;
541 #else
542 case TT_SPILL: /* window overflow */
543 save_window(env);
544 break;
545 case TT_FILL: /* window underflow */
546 restore_window(env);
547 break;
548 case TT_TFAULT:
549 case TT_DFAULT:
550 #if 0
552 info.si_signo = SIGSEGV;
553 info.si_errno = 0;
554 /* XXX: check env->error_code */
555 info.si_code = TARGET_SEGV_MAPERR;
556 if (trapnr == TT_DFAULT)
557 info._sifields._sigfault._addr = env->dmmuregs[4];
558 else
559 info._sifields._sigfault._addr = env->tsptr->tpc;
560 //queue_signal(env, info.si_signo, &info);
562 #endif
563 break;
564 #endif
565 case EXCP_INTERRUPT:
566 /* just indicate that signals should be handled asap */
567 break;
568 case EXCP_DEBUG:
570 int sig;
572 sig = gdb_handlesig (env, TARGET_SIGTRAP);
573 #if 0
574 if (sig)
576 info.si_signo = sig;
577 info.si_errno = 0;
578 info.si_code = TARGET_TRAP_BRKPT;
579 //queue_signal(env, info.si_signo, &info);
581 #endif
583 break;
584 default:
585 printf ("Unhandled trap: 0x%x\n", trapnr);
586 cpu_dump_state(env, stderr, fprintf, 0);
587 exit (1);
589 process_pending_signals (env);
593 #endif
595 static void usage(void)
597 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
598 "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
599 "BSD CPU emulator (compiled for %s emulation)\n"
600 "\n"
601 "Standard options:\n"
602 "-h print this help\n"
603 "-g port wait gdb connection to port\n"
604 "-L path set the elf interpreter prefix (default=%s)\n"
605 "-s size set the stack size in bytes (default=%ld)\n"
606 "-cpu model select CPU (-cpu ? for list)\n"
607 "-drop-ld-preload drop LD_PRELOAD for target process\n"
608 "-E var=value sets/modifies targets environment variable(s)\n"
609 "-U var unsets targets environment variable(s)\n"
610 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
611 "\n"
612 "Debug options:\n"
613 "-d options activate log (logfile=%s)\n"
614 "-p pagesize set the host page size to 'pagesize'\n"
615 "-singlestep always run in singlestep mode\n"
616 "-strace log system calls\n"
617 "\n"
618 "Environment variables:\n"
619 "QEMU_STRACE Print system calls and arguments similar to the\n"
620 " 'strace' program. Enable by setting to any value.\n"
621 "You can use -E and -U options to set/unset environment variables\n"
622 "for target process. It is possible to provide several variables\n"
623 "by repeating the option. For example:\n"
624 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
625 "Note that if you provide several changes to single variable\n"
626 "last change will stay in effect.\n"
628 TARGET_ARCH,
629 interp_prefix,
630 x86_stack_size,
631 DEBUG_LOGFILE);
632 exit(1);
635 THREAD CPUState *thread_env;
637 /* Assumes contents are already zeroed. */
638 void init_task_state(TaskState *ts)
640 int i;
642 ts->used = 1;
643 ts->first_free = ts->sigqueue_table;
644 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
645 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
647 ts->sigqueue_table[i].next = NULL;
650 int main(int argc, char **argv)
652 const char *filename;
653 const char *cpu_model;
654 struct target_pt_regs regs1, *regs = &regs1;
655 struct image_info info1, *info = &info1;
656 TaskState ts1, *ts = &ts1;
657 CPUState *env;
658 int optind;
659 const char *r;
660 int gdbstub_port = 0;
661 char **target_environ, **wrk;
662 envlist_t *envlist = NULL;
663 enum BSDType bsd_type = target_openbsd;
665 if (argc <= 1)
666 usage();
668 /* init debug */
669 cpu_set_log_filename(DEBUG_LOGFILE);
671 if ((envlist = envlist_create()) == NULL) {
672 (void) fprintf(stderr, "Unable to allocate envlist\n");
673 exit(1);
676 /* add current environment into the list */
677 for (wrk = environ; *wrk != NULL; wrk++) {
678 (void) envlist_setenv(envlist, *wrk);
681 cpu_model = NULL;
682 optind = 1;
683 for(;;) {
684 if (optind >= argc)
685 break;
686 r = argv[optind];
687 if (r[0] != '-')
688 break;
689 optind++;
690 r++;
691 if (!strcmp(r, "-")) {
692 break;
693 } else if (!strcmp(r, "d")) {
694 int mask;
695 const CPULogItem *item;
697 if (optind >= argc)
698 break;
700 r = argv[optind++];
701 mask = cpu_str_to_log_mask(r);
702 if (!mask) {
703 printf("Log items (comma separated):\n");
704 for(item = cpu_log_items; item->mask != 0; item++) {
705 printf("%-10s %s\n", item->name, item->help);
707 exit(1);
709 cpu_set_log(mask);
710 } else if (!strcmp(r, "E")) {
711 r = argv[optind++];
712 if (envlist_setenv(envlist, r) != 0)
713 usage();
714 } else if (!strcmp(r, "U")) {
715 r = argv[optind++];
716 if (envlist_unsetenv(envlist, r) != 0)
717 usage();
718 } else if (!strcmp(r, "s")) {
719 r = argv[optind++];
720 x86_stack_size = strtol(r, (char **)&r, 0);
721 if (x86_stack_size <= 0)
722 usage();
723 if (*r == 'M')
724 x86_stack_size *= 1024 * 1024;
725 else if (*r == 'k' || *r == 'K')
726 x86_stack_size *= 1024;
727 } else if (!strcmp(r, "L")) {
728 interp_prefix = argv[optind++];
729 } else if (!strcmp(r, "p")) {
730 qemu_host_page_size = atoi(argv[optind++]);
731 if (qemu_host_page_size == 0 ||
732 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
733 fprintf(stderr, "page size must be a power of two\n");
734 exit(1);
736 } else if (!strcmp(r, "g")) {
737 gdbstub_port = atoi(argv[optind++]);
738 } else if (!strcmp(r, "r")) {
739 qemu_uname_release = argv[optind++];
740 } else if (!strcmp(r, "cpu")) {
741 cpu_model = argv[optind++];
742 if (strcmp(cpu_model, "?") == 0) {
743 /* XXX: implement xxx_cpu_list for targets that still miss it */
744 #if defined(cpu_list)
745 cpu_list(stdout, &fprintf);
746 #endif
747 exit(1);
749 } else if (!strcmp(r, "drop-ld-preload")) {
750 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
751 } else if (!strcmp(r, "bsd")) {
752 if (!strcasecmp(argv[optind], "freebsd")) {
753 bsd_type = target_freebsd;
754 } else if (!strcasecmp(argv[optind], "netbsd")) {
755 bsd_type = target_netbsd;
756 } else if (!strcasecmp(argv[optind], "openbsd")) {
757 bsd_type = target_openbsd;
758 } else {
759 usage();
761 optind++;
762 } else if (!strcmp(r, "singlestep")) {
763 singlestep = 1;
764 } else if (!strcmp(r, "strace")) {
765 do_strace = 1;
766 } else
768 usage();
771 if (optind >= argc)
772 usage();
773 filename = argv[optind];
775 /* Zero out regs */
776 memset(regs, 0, sizeof(struct target_pt_regs));
778 /* Zero out image_info */
779 memset(info, 0, sizeof(struct image_info));
781 /* Scan interp_prefix dir for replacement files. */
782 init_paths(interp_prefix);
784 if (cpu_model == NULL) {
785 #if defined(TARGET_I386)
786 #ifdef TARGET_X86_64
787 cpu_model = "qemu64";
788 #else
789 cpu_model = "qemu32";
790 #endif
791 #elif defined(TARGET_SPARC)
792 #ifdef TARGET_SPARC64
793 cpu_model = "TI UltraSparc II";
794 #else
795 cpu_model = "Fujitsu MB86904";
796 #endif
797 #else
798 cpu_model = "any";
799 #endif
801 cpu_exec_init_all(0);
802 /* NOTE: we need to init the CPU at this stage to get
803 qemu_host_page_size */
804 env = cpu_init(cpu_model);
805 if (!env) {
806 fprintf(stderr, "Unable to find CPU definition\n");
807 exit(1);
809 thread_env = env;
811 if (getenv("QEMU_STRACE")) {
812 do_strace = 1;
815 target_environ = envlist_to_environ(envlist, NULL);
816 envlist_free(envlist);
819 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
820 printf("Error loading %s\n", filename);
821 _exit(1);
824 for (wrk = target_environ; *wrk; wrk++) {
825 free(*wrk);
828 free(target_environ);
830 if (qemu_log_enabled()) {
831 log_page_dump();
833 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
834 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
835 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
836 info->start_code);
837 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
838 info->start_data);
839 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
840 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
841 info->start_stack);
842 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
843 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
846 target_set_brk(info->brk);
847 syscall_init();
848 signal_init();
850 /* build Task State */
851 memset(ts, 0, sizeof(TaskState));
852 init_task_state(ts);
853 ts->info = info;
854 env->opaque = ts;
856 #if defined(TARGET_I386)
857 cpu_x86_set_cpl(env, 3);
859 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
860 env->hflags |= HF_PE_MASK;
861 if (env->cpuid_features & CPUID_SSE) {
862 env->cr[4] |= CR4_OSFXSR_MASK;
863 env->hflags |= HF_OSFXSR_MASK;
865 #ifndef TARGET_ABI32
866 /* enable 64 bit mode if possible */
867 if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
868 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
869 exit(1);
871 env->cr[4] |= CR4_PAE_MASK;
872 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
873 env->hflags |= HF_LMA_MASK;
874 #endif
876 /* flags setup : we activate the IRQs by default as in user mode */
877 env->eflags |= IF_MASK;
879 /* linux register setup */
880 #ifndef TARGET_ABI32
881 env->regs[R_EAX] = regs->rax;
882 env->regs[R_EBX] = regs->rbx;
883 env->regs[R_ECX] = regs->rcx;
884 env->regs[R_EDX] = regs->rdx;
885 env->regs[R_ESI] = regs->rsi;
886 env->regs[R_EDI] = regs->rdi;
887 env->regs[R_EBP] = regs->rbp;
888 env->regs[R_ESP] = regs->rsp;
889 env->eip = regs->rip;
890 #else
891 env->regs[R_EAX] = regs->eax;
892 env->regs[R_EBX] = regs->ebx;
893 env->regs[R_ECX] = regs->ecx;
894 env->regs[R_EDX] = regs->edx;
895 env->regs[R_ESI] = regs->esi;
896 env->regs[R_EDI] = regs->edi;
897 env->regs[R_EBP] = regs->ebp;
898 env->regs[R_ESP] = regs->esp;
899 env->eip = regs->eip;
900 #endif
902 /* linux interrupt setup */
903 #ifndef TARGET_ABI32
904 env->idt.limit = 511;
905 #else
906 env->idt.limit = 255;
907 #endif
908 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
909 PROT_READ|PROT_WRITE,
910 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
911 idt_table = g2h(env->idt.base);
912 set_idt(0, 0);
913 set_idt(1, 0);
914 set_idt(2, 0);
915 set_idt(3, 3);
916 set_idt(4, 3);
917 set_idt(5, 0);
918 set_idt(6, 0);
919 set_idt(7, 0);
920 set_idt(8, 0);
921 set_idt(9, 0);
922 set_idt(10, 0);
923 set_idt(11, 0);
924 set_idt(12, 0);
925 set_idt(13, 0);
926 set_idt(14, 0);
927 set_idt(15, 0);
928 set_idt(16, 0);
929 set_idt(17, 0);
930 set_idt(18, 0);
931 set_idt(19, 0);
932 set_idt(0x80, 3);
934 /* linux segment setup */
936 uint64_t *gdt_table;
937 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
938 PROT_READ|PROT_WRITE,
939 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
940 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
941 gdt_table = g2h(env->gdt.base);
942 #ifdef TARGET_ABI32
943 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
944 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
945 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
946 #else
947 /* 64 bit code segment */
948 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
949 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
950 DESC_L_MASK |
951 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
952 #endif
953 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
954 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
955 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
958 cpu_x86_load_seg(env, R_CS, __USER_CS);
959 cpu_x86_load_seg(env, R_SS, __USER_DS);
960 #ifdef TARGET_ABI32
961 cpu_x86_load_seg(env, R_DS, __USER_DS);
962 cpu_x86_load_seg(env, R_ES, __USER_DS);
963 cpu_x86_load_seg(env, R_FS, __USER_DS);
964 cpu_x86_load_seg(env, R_GS, __USER_DS);
965 /* This hack makes Wine work... */
966 env->segs[R_FS].selector = 0;
967 #else
968 cpu_x86_load_seg(env, R_DS, 0);
969 cpu_x86_load_seg(env, R_ES, 0);
970 cpu_x86_load_seg(env, R_FS, 0);
971 cpu_x86_load_seg(env, R_GS, 0);
972 #endif
973 #elif defined(TARGET_SPARC)
975 int i;
976 env->pc = regs->pc;
977 env->npc = regs->npc;
978 env->y = regs->y;
979 for(i = 0; i < 8; i++)
980 env->gregs[i] = regs->u_regs[i];
981 for(i = 0; i < 8; i++)
982 env->regwptr[i] = regs->u_regs[i + 8];
984 #else
985 #error unsupported target CPU
986 #endif
988 if (gdbstub_port) {
989 gdbserver_start (gdbstub_port);
990 gdb_handlesig(env, 0);
992 cpu_loop(env, bsd_type);
993 /* never exits */
994 return 0;