qdev: move name+size into DeviceInfo (v2)
[qemu.git] / bsd-user / main.c
blob827c9c348d795d4b4e1e55270a99e6a8e2167dae
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 * MA 02110-1301, USA.
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <machine/trap.h>
28 #include <sys/types.h>
29 #include <sys/mman.h>
31 #include "qemu.h"
32 #include "qemu-common.h"
33 /* For tb_lock */
34 #include "exec-all.h"
36 #define DEBUG_LOGFILE "/tmp/qemu.log"
38 int singlestep;
40 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
41 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
42 extern char **environ;
44 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
45 we allocate a bigger stack. Need a better solution, for example
46 by remapping the process stack directly at the right place */
47 unsigned long x86_stack_size = 512 * 1024;
49 void gemu_log(const char *fmt, ...)
51 va_list ap;
53 va_start(ap, fmt);
54 vfprintf(stderr, fmt, ap);
55 va_end(ap);
58 void cpu_outb(CPUState *env, int addr, int val)
60 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
63 void cpu_outw(CPUState *env, int addr, int val)
65 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
68 void cpu_outl(CPUState *env, int addr, int val)
70 fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
73 int cpu_inb(CPUState *env, int addr)
75 fprintf(stderr, "inb: port=0x%04x\n", addr);
76 return 0;
79 int cpu_inw(CPUState *env, int addr)
81 fprintf(stderr, "inw: port=0x%04x\n", addr);
82 return 0;
85 int cpu_inl(CPUState *env, int addr)
87 fprintf(stderr, "inl: port=0x%04x\n", addr);
88 return 0;
91 #if defined(TARGET_I386)
92 int cpu_get_pic_interrupt(CPUState *env)
94 return -1;
96 #endif
98 /* These are no-ops because we are not threadsafe. */
99 static inline void cpu_exec_start(CPUState *env)
103 static inline void cpu_exec_end(CPUState *env)
107 static inline void start_exclusive(void)
111 static inline void end_exclusive(void)
115 void fork_start(void)
119 void fork_end(int child)
121 if (child) {
122 gdbserver_fork(thread_env);
126 void cpu_list_lock(void)
130 void cpu_list_unlock(void)
134 #ifdef TARGET_I386
135 /***********************************************************/
136 /* CPUX86 core interface */
138 void cpu_smm_update(CPUState *env)
142 uint64_t cpu_get_tsc(CPUX86State *env)
144 return cpu_get_real_ticks();
147 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
148 int flags)
150 unsigned int e1, e2;
151 uint32_t *p;
152 e1 = (addr << 16) | (limit & 0xffff);
153 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
154 e2 |= flags;
155 p = ptr;
156 p[0] = tswap32(e1);
157 p[1] = tswap32(e2);
160 static uint64_t *idt_table;
161 #ifdef TARGET_X86_64
162 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
163 uint64_t addr, unsigned int sel)
165 uint32_t *p, e1, e2;
166 e1 = (addr & 0xffff) | (sel << 16);
167 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
168 p = ptr;
169 p[0] = tswap32(e1);
170 p[1] = tswap32(e2);
171 p[2] = tswap32(addr >> 32);
172 p[3] = 0;
174 /* only dpl matters as we do only user space emulation */
175 static void set_idt(int n, unsigned int dpl)
177 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
179 #else
180 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
181 uint32_t addr, unsigned int sel)
183 uint32_t *p, e1, e2;
184 e1 = (addr & 0xffff) | (sel << 16);
185 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
186 p = ptr;
187 p[0] = tswap32(e1);
188 p[1] = tswap32(e2);
191 /* only dpl matters as we do only user space emulation */
192 static void set_idt(int n, unsigned int dpl)
194 set_gate(idt_table + n, 0, dpl, 0, 0);
196 #endif
198 void cpu_loop(CPUX86State *env, enum BSDType bsd_type)
200 int trapnr;
201 abi_ulong pc;
202 //target_siginfo_t info;
204 for(;;) {
205 trapnr = cpu_x86_exec(env);
206 switch(trapnr) {
207 case 0x80:
208 /* syscall from int $0x80 */
209 env->regs[R_EAX] = do_openbsd_syscall(env,
210 env->regs[R_EAX],
211 env->regs[R_EBX],
212 env->regs[R_ECX],
213 env->regs[R_EDX],
214 env->regs[R_ESI],
215 env->regs[R_EDI],
216 env->regs[R_EBP]);
217 break;
218 #ifndef TARGET_ABI32
219 case EXCP_SYSCALL:
220 /* linux syscall from syscall intruction */
221 env->regs[R_EAX] = do_openbsd_syscall(env,
222 env->regs[R_EAX],
223 env->regs[R_EDI],
224 env->regs[R_ESI],
225 env->regs[R_EDX],
226 env->regs[10],
227 env->regs[8],
228 env->regs[9]);
229 env->eip = env->exception_next_eip;
230 break;
231 #endif
232 #if 0
233 case EXCP0B_NOSEG:
234 case EXCP0C_STACK:
235 info.si_signo = SIGBUS;
236 info.si_errno = 0;
237 info.si_code = TARGET_SI_KERNEL;
238 info._sifields._sigfault._addr = 0;
239 queue_signal(env, info.si_signo, &info);
240 break;
241 case EXCP0D_GPF:
242 /* XXX: potential problem if ABI32 */
243 #ifndef TARGET_X86_64
244 if (env->eflags & VM_MASK) {
245 handle_vm86_fault(env);
246 } else
247 #endif
249 info.si_signo = SIGSEGV;
250 info.si_errno = 0;
251 info.si_code = TARGET_SI_KERNEL;
252 info._sifields._sigfault._addr = 0;
253 queue_signal(env, info.si_signo, &info);
255 break;
256 case EXCP0E_PAGE:
257 info.si_signo = SIGSEGV;
258 info.si_errno = 0;
259 if (!(env->error_code & 1))
260 info.si_code = TARGET_SEGV_MAPERR;
261 else
262 info.si_code = TARGET_SEGV_ACCERR;
263 info._sifields._sigfault._addr = env->cr[2];
264 queue_signal(env, info.si_signo, &info);
265 break;
266 case EXCP00_DIVZ:
267 #ifndef TARGET_X86_64
268 if (env->eflags & VM_MASK) {
269 handle_vm86_trap(env, trapnr);
270 } else
271 #endif
273 /* division by zero */
274 info.si_signo = SIGFPE;
275 info.si_errno = 0;
276 info.si_code = TARGET_FPE_INTDIV;
277 info._sifields._sigfault._addr = env->eip;
278 queue_signal(env, info.si_signo, &info);
280 break;
281 case EXCP01_DB:
282 case EXCP03_INT3:
283 #ifndef TARGET_X86_64
284 if (env->eflags & VM_MASK) {
285 handle_vm86_trap(env, trapnr);
286 } else
287 #endif
289 info.si_signo = SIGTRAP;
290 info.si_errno = 0;
291 if (trapnr == EXCP01_DB) {
292 info.si_code = TARGET_TRAP_BRKPT;
293 info._sifields._sigfault._addr = env->eip;
294 } else {
295 info.si_code = TARGET_SI_KERNEL;
296 info._sifields._sigfault._addr = 0;
298 queue_signal(env, info.si_signo, &info);
300 break;
301 case EXCP04_INTO:
302 case EXCP05_BOUND:
303 #ifndef TARGET_X86_64
304 if (env->eflags & VM_MASK) {
305 handle_vm86_trap(env, trapnr);
306 } else
307 #endif
309 info.si_signo = SIGSEGV;
310 info.si_errno = 0;
311 info.si_code = TARGET_SI_KERNEL;
312 info._sifields._sigfault._addr = 0;
313 queue_signal(env, info.si_signo, &info);
315 break;
316 case EXCP06_ILLOP:
317 info.si_signo = SIGILL;
318 info.si_errno = 0;
319 info.si_code = TARGET_ILL_ILLOPN;
320 info._sifields._sigfault._addr = env->eip;
321 queue_signal(env, info.si_signo, &info);
322 break;
323 #endif
324 case EXCP_INTERRUPT:
325 /* just indicate that signals should be handled asap */
326 break;
327 #if 0
328 case EXCP_DEBUG:
330 int sig;
332 sig = gdb_handlesig (env, TARGET_SIGTRAP);
333 if (sig)
335 info.si_signo = sig;
336 info.si_errno = 0;
337 info.si_code = TARGET_TRAP_BRKPT;
338 queue_signal(env, info.si_signo, &info);
341 break;
342 #endif
343 default:
344 pc = env->segs[R_CS].base + env->eip;
345 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
346 (long)pc, trapnr);
347 abort();
349 process_pending_signals(env);
352 #endif
354 #ifdef TARGET_SPARC
355 #define SPARC64_STACK_BIAS 2047
357 //#define DEBUG_WIN
358 /* WARNING: dealing with register windows _is_ complicated. More info
359 can be found at http://www.sics.se/~psm/sparcstack.html */
360 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
362 index = (index + cwp * 16) % (16 * env->nwindows);
363 /* wrap handling : if cwp is on the last window, then we use the
364 registers 'after' the end */
365 if (index < 8 && env->cwp == env->nwindows - 1)
366 index += 16 * env->nwindows;
367 return index;
370 /* save the register window 'cwp1' */
371 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
373 unsigned int i;
374 abi_ulong sp_ptr;
376 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
377 #ifdef TARGET_SPARC64
378 if (sp_ptr & 3)
379 sp_ptr += SPARC64_STACK_BIAS;
380 #endif
381 #if defined(DEBUG_WIN)
382 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
383 sp_ptr, cwp1);
384 #endif
385 for(i = 0; i < 16; i++) {
386 /* FIXME - what to do if put_user() fails? */
387 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
388 sp_ptr += sizeof(abi_ulong);
392 static void save_window(CPUSPARCState *env)
394 #ifndef TARGET_SPARC64
395 unsigned int new_wim;
396 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
397 ((1LL << env->nwindows) - 1);
398 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
399 env->wim = new_wim;
400 #else
401 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
402 env->cansave++;
403 env->canrestore--;
404 #endif
407 static void restore_window(CPUSPARCState *env)
409 #ifndef TARGET_SPARC64
410 unsigned int new_wim;
411 #endif
412 unsigned int i, cwp1;
413 abi_ulong sp_ptr;
415 #ifndef TARGET_SPARC64
416 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
417 ((1LL << env->nwindows) - 1);
418 #endif
420 /* restore the invalid window */
421 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
422 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
423 #ifdef TARGET_SPARC64
424 if (sp_ptr & 3)
425 sp_ptr += SPARC64_STACK_BIAS;
426 #endif
427 #if defined(DEBUG_WIN)
428 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
429 sp_ptr, cwp1);
430 #endif
431 for(i = 0; i < 16; i++) {
432 /* FIXME - what to do if get_user() fails? */
433 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
434 sp_ptr += sizeof(abi_ulong);
436 #ifdef TARGET_SPARC64
437 env->canrestore++;
438 if (env->cleanwin < env->nwindows - 1)
439 env->cleanwin++;
440 env->cansave--;
441 #else
442 env->wim = new_wim;
443 #endif
446 static void flush_windows(CPUSPARCState *env)
448 int offset, cwp1;
450 offset = 1;
451 for(;;) {
452 /* if restore would invoke restore_window(), then we can stop */
453 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
454 #ifndef TARGET_SPARC64
455 if (env->wim & (1 << cwp1))
456 break;
457 #else
458 if (env->canrestore == 0)
459 break;
460 env->cansave++;
461 env->canrestore--;
462 #endif
463 save_window_offset(env, cwp1);
464 offset++;
466 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
467 #ifndef TARGET_SPARC64
468 /* set wim so that restore will reload the registers */
469 env->wim = 1 << cwp1;
470 #endif
471 #if defined(DEBUG_WIN)
472 printf("flush_windows: nb=%d\n", offset - 1);
473 #endif
476 void cpu_loop(CPUSPARCState *env, enum BSDType bsd_type)
478 int trapnr, ret, syscall_nr;
479 //target_siginfo_t info;
481 while (1) {
482 trapnr = cpu_sparc_exec (env);
484 switch (trapnr) {
485 #ifndef TARGET_SPARC64
486 case 0x80:
487 #else
488 case 0x100:
489 #endif
490 syscall_nr = env->gregs[1];
491 if (bsd_type == target_freebsd)
492 ret = do_freebsd_syscall(env, syscall_nr,
493 env->regwptr[0], env->regwptr[1],
494 env->regwptr[2], env->regwptr[3],
495 env->regwptr[4], env->regwptr[5]);
496 else if (bsd_type == target_netbsd)
497 ret = do_netbsd_syscall(env, syscall_nr,
498 env->regwptr[0], env->regwptr[1],
499 env->regwptr[2], env->regwptr[3],
500 env->regwptr[4], env->regwptr[5]);
501 else { //if (bsd_type == target_openbsd)
502 #if defined(TARGET_SPARC64)
503 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
504 TARGET_OPENBSD_SYSCALL_G2RFLAG);
505 #endif
506 ret = do_openbsd_syscall(env, syscall_nr,
507 env->regwptr[0], env->regwptr[1],
508 env->regwptr[2], env->regwptr[3],
509 env->regwptr[4], env->regwptr[5]);
511 if ((unsigned int)ret >= (unsigned int)(-515)) {
512 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
513 env->xcc |= PSR_CARRY;
514 #else
515 env->psr |= PSR_CARRY;
516 #endif
517 } else {
518 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
519 env->xcc &= ~PSR_CARRY;
520 #else
521 env->psr &= ~PSR_CARRY;
522 #endif
524 env->regwptr[0] = ret;
525 /* next instruction */
526 #if defined(TARGET_SPARC64)
527 if (bsd_type == target_openbsd &&
528 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
529 env->pc = env->gregs[2];
530 env->npc = env->pc + 4;
531 } else if (bsd_type == target_openbsd &&
532 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
533 env->pc = env->gregs[7];
534 env->npc = env->pc + 4;
535 } else {
536 env->pc = env->npc;
537 env->npc = env->npc + 4;
539 #else
540 env->pc = env->npc;
541 env->npc = env->npc + 4;
542 #endif
543 break;
544 case 0x83: /* flush windows */
545 #ifdef TARGET_ABI32
546 case 0x103:
547 #endif
548 flush_windows(env);
549 /* next instruction */
550 env->pc = env->npc;
551 env->npc = env->npc + 4;
552 break;
553 #ifndef TARGET_SPARC64
554 case TT_WIN_OVF: /* window overflow */
555 save_window(env);
556 break;
557 case TT_WIN_UNF: /* window underflow */
558 restore_window(env);
559 break;
560 case TT_TFAULT:
561 case TT_DFAULT:
562 #if 0
564 info.si_signo = SIGSEGV;
565 info.si_errno = 0;
566 /* XXX: check env->error_code */
567 info.si_code = TARGET_SEGV_MAPERR;
568 info._sifields._sigfault._addr = env->mmuregs[4];
569 queue_signal(env, info.si_signo, &info);
571 #endif
572 break;
573 #else
574 case TT_SPILL: /* window overflow */
575 save_window(env);
576 break;
577 case TT_FILL: /* window underflow */
578 restore_window(env);
579 break;
580 case TT_TFAULT:
581 case TT_DFAULT:
582 #if 0
584 info.si_signo = SIGSEGV;
585 info.si_errno = 0;
586 /* XXX: check env->error_code */
587 info.si_code = TARGET_SEGV_MAPERR;
588 if (trapnr == TT_DFAULT)
589 info._sifields._sigfault._addr = env->dmmuregs[4];
590 else
591 info._sifields._sigfault._addr = env->tsptr->tpc;
592 //queue_signal(env, info.si_signo, &info);
594 #endif
595 break;
596 #endif
597 case EXCP_INTERRUPT:
598 /* just indicate that signals should be handled asap */
599 break;
600 case EXCP_DEBUG:
602 int sig;
604 sig = gdb_handlesig (env, TARGET_SIGTRAP);
605 #if 0
606 if (sig)
608 info.si_signo = sig;
609 info.si_errno = 0;
610 info.si_code = TARGET_TRAP_BRKPT;
611 //queue_signal(env, info.si_signo, &info);
613 #endif
615 break;
616 default:
617 printf ("Unhandled trap: 0x%x\n", trapnr);
618 cpu_dump_state(env, stderr, fprintf, 0);
619 exit (1);
621 process_pending_signals (env);
625 #endif
627 static void usage(void)
629 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
630 "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
631 "BSD CPU emulator (compiled for %s emulation)\n"
632 "\n"
633 "Standard options:\n"
634 "-h print this help\n"
635 "-g port wait gdb connection to port\n"
636 "-L path set the elf interpreter prefix (default=%s)\n"
637 "-s size set the stack size in bytes (default=%ld)\n"
638 "-cpu model select CPU (-cpu ? for list)\n"
639 "-drop-ld-preload drop LD_PRELOAD for target process\n"
640 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
641 "\n"
642 "Debug options:\n"
643 "-d options activate log (logfile=%s)\n"
644 "-p pagesize set the host page size to 'pagesize'\n"
645 "-singlestep always run in singlestep mode\n"
646 "-strace log system calls\n"
647 "\n"
648 "Environment variables:\n"
649 "QEMU_STRACE Print system calls and arguments similar to the\n"
650 " 'strace' program. Enable by setting to any value.\n"
652 TARGET_ARCH,
653 interp_prefix,
654 x86_stack_size,
655 DEBUG_LOGFILE);
656 exit(1);
659 THREAD CPUState *thread_env;
661 /* Assumes contents are already zeroed. */
662 void init_task_state(TaskState *ts)
664 int i;
666 ts->used = 1;
667 ts->first_free = ts->sigqueue_table;
668 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
669 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
671 ts->sigqueue_table[i].next = NULL;
674 int main(int argc, char **argv)
676 const char *filename;
677 const char *cpu_model;
678 struct target_pt_regs regs1, *regs = &regs1;
679 struct image_info info1, *info = &info1;
680 TaskState ts1, *ts = &ts1;
681 CPUState *env;
682 int optind;
683 const char *r;
684 int gdbstub_port = 0;
685 int drop_ld_preload = 0, environ_count = 0;
686 char **target_environ, **wrk, **dst;
687 enum BSDType bsd_type = target_openbsd;
689 if (argc <= 1)
690 usage();
692 /* init debug */
693 cpu_set_log_filename(DEBUG_LOGFILE);
695 cpu_model = NULL;
696 optind = 1;
697 for(;;) {
698 if (optind >= argc)
699 break;
700 r = argv[optind];
701 if (r[0] != '-')
702 break;
703 optind++;
704 r++;
705 if (!strcmp(r, "-")) {
706 break;
707 } else if (!strcmp(r, "d")) {
708 int mask;
709 const CPULogItem *item;
711 if (optind >= argc)
712 break;
714 r = argv[optind++];
715 mask = cpu_str_to_log_mask(r);
716 if (!mask) {
717 printf("Log items (comma separated):\n");
718 for(item = cpu_log_items; item->mask != 0; item++) {
719 printf("%-10s %s\n", item->name, item->help);
721 exit(1);
723 cpu_set_log(mask);
724 } else if (!strcmp(r, "s")) {
725 r = argv[optind++];
726 x86_stack_size = strtol(r, (char **)&r, 0);
727 if (x86_stack_size <= 0)
728 usage();
729 if (*r == 'M')
730 x86_stack_size *= 1024 * 1024;
731 else if (*r == 'k' || *r == 'K')
732 x86_stack_size *= 1024;
733 } else if (!strcmp(r, "L")) {
734 interp_prefix = argv[optind++];
735 } else if (!strcmp(r, "p")) {
736 qemu_host_page_size = atoi(argv[optind++]);
737 if (qemu_host_page_size == 0 ||
738 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
739 fprintf(stderr, "page size must be a power of two\n");
740 exit(1);
742 } else if (!strcmp(r, "g")) {
743 gdbstub_port = atoi(argv[optind++]);
744 } else if (!strcmp(r, "r")) {
745 qemu_uname_release = argv[optind++];
746 } else if (!strcmp(r, "cpu")) {
747 cpu_model = argv[optind++];
748 if (strcmp(cpu_model, "?") == 0) {
749 /* XXX: implement xxx_cpu_list for targets that still miss it */
750 #if defined(cpu_list)
751 cpu_list(stdout, &fprintf);
752 #endif
753 exit(1);
755 } else if (!strcmp(r, "drop-ld-preload")) {
756 drop_ld_preload = 1;
757 } else if (!strcmp(r, "bsd")) {
758 if (!strcasecmp(argv[optind], "freebsd")) {
759 bsd_type = target_freebsd;
760 } else if (!strcasecmp(argv[optind], "netbsd")) {
761 bsd_type = target_netbsd;
762 } else if (!strcasecmp(argv[optind], "openbsd")) {
763 bsd_type = target_openbsd;
764 } else {
765 usage();
767 optind++;
768 } else if (!strcmp(r, "singlestep")) {
769 singlestep = 1;
770 } else if (!strcmp(r, "strace")) {
771 do_strace = 1;
772 } else
774 usage();
777 if (optind >= argc)
778 usage();
779 filename = argv[optind];
781 /* Zero out regs */
782 memset(regs, 0, sizeof(struct target_pt_regs));
784 /* Zero out image_info */
785 memset(info, 0, sizeof(struct image_info));
787 /* Scan interp_prefix dir for replacement files. */
788 init_paths(interp_prefix);
790 if (cpu_model == NULL) {
791 #if defined(TARGET_I386)
792 #ifdef TARGET_X86_64
793 cpu_model = "qemu64";
794 #else
795 cpu_model = "qemu32";
796 #endif
797 #elif defined(TARGET_SPARC)
798 #ifdef TARGET_SPARC64
799 cpu_model = "TI UltraSparc II";
800 #else
801 cpu_model = "Fujitsu MB86904";
802 #endif
803 #else
804 cpu_model = "any";
805 #endif
807 cpu_exec_init_all(0);
808 /* NOTE: we need to init the CPU at this stage to get
809 qemu_host_page_size */
810 env = cpu_init(cpu_model);
811 if (!env) {
812 fprintf(stderr, "Unable to find CPU definition\n");
813 exit(1);
815 thread_env = env;
817 if (getenv("QEMU_STRACE")) {
818 do_strace = 1;
821 wrk = environ;
822 while (*(wrk++))
823 environ_count++;
825 target_environ = malloc((environ_count + 1) * sizeof(char *));
826 if (!target_environ)
827 abort();
828 for (wrk = environ, dst = target_environ; *wrk; wrk++) {
829 if (drop_ld_preload && !strncmp(*wrk, "LD_PRELOAD=", 11))
830 continue;
831 *(dst++) = strdup(*wrk);
833 *dst = NULL; /* NULL terminate target_environ */
835 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
836 printf("Error loading %s\n", filename);
837 _exit(1);
840 for (wrk = target_environ; *wrk; wrk++) {
841 free(*wrk);
844 free(target_environ);
846 if (qemu_log_enabled()) {
847 log_page_dump();
849 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
850 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
851 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
852 info->start_code);
853 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
854 info->start_data);
855 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
856 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
857 info->start_stack);
858 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
859 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
862 target_set_brk(info->brk);
863 syscall_init();
864 signal_init();
866 /* build Task State */
867 memset(ts, 0, sizeof(TaskState));
868 init_task_state(ts);
869 ts->info = info;
870 env->opaque = ts;
872 #if defined(TARGET_I386)
873 cpu_x86_set_cpl(env, 3);
875 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
876 env->hflags |= HF_PE_MASK;
877 if (env->cpuid_features & CPUID_SSE) {
878 env->cr[4] |= CR4_OSFXSR_MASK;
879 env->hflags |= HF_OSFXSR_MASK;
881 #ifndef TARGET_ABI32
882 /* enable 64 bit mode if possible */
883 if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
884 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
885 exit(1);
887 env->cr[4] |= CR4_PAE_MASK;
888 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
889 env->hflags |= HF_LMA_MASK;
890 #endif
892 /* flags setup : we activate the IRQs by default as in user mode */
893 env->eflags |= IF_MASK;
895 /* linux register setup */
896 #ifndef TARGET_ABI32
897 env->regs[R_EAX] = regs->rax;
898 env->regs[R_EBX] = regs->rbx;
899 env->regs[R_ECX] = regs->rcx;
900 env->regs[R_EDX] = regs->rdx;
901 env->regs[R_ESI] = regs->rsi;
902 env->regs[R_EDI] = regs->rdi;
903 env->regs[R_EBP] = regs->rbp;
904 env->regs[R_ESP] = regs->rsp;
905 env->eip = regs->rip;
906 #else
907 env->regs[R_EAX] = regs->eax;
908 env->regs[R_EBX] = regs->ebx;
909 env->regs[R_ECX] = regs->ecx;
910 env->regs[R_EDX] = regs->edx;
911 env->regs[R_ESI] = regs->esi;
912 env->regs[R_EDI] = regs->edi;
913 env->regs[R_EBP] = regs->ebp;
914 env->regs[R_ESP] = regs->esp;
915 env->eip = regs->eip;
916 #endif
918 /* linux interrupt setup */
919 #ifndef TARGET_ABI32
920 env->idt.limit = 511;
921 #else
922 env->idt.limit = 255;
923 #endif
924 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
925 PROT_READ|PROT_WRITE,
926 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
927 idt_table = g2h(env->idt.base);
928 set_idt(0, 0);
929 set_idt(1, 0);
930 set_idt(2, 0);
931 set_idt(3, 3);
932 set_idt(4, 3);
933 set_idt(5, 0);
934 set_idt(6, 0);
935 set_idt(7, 0);
936 set_idt(8, 0);
937 set_idt(9, 0);
938 set_idt(10, 0);
939 set_idt(11, 0);
940 set_idt(12, 0);
941 set_idt(13, 0);
942 set_idt(14, 0);
943 set_idt(15, 0);
944 set_idt(16, 0);
945 set_idt(17, 0);
946 set_idt(18, 0);
947 set_idt(19, 0);
948 set_idt(0x80, 3);
950 /* linux segment setup */
952 uint64_t *gdt_table;
953 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
954 PROT_READ|PROT_WRITE,
955 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
956 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
957 gdt_table = g2h(env->gdt.base);
958 #ifdef TARGET_ABI32
959 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
960 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
961 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
962 #else
963 /* 64 bit code segment */
964 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
965 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
966 DESC_L_MASK |
967 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
968 #endif
969 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
970 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
971 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
974 cpu_x86_load_seg(env, R_CS, __USER_CS);
975 cpu_x86_load_seg(env, R_SS, __USER_DS);
976 #ifdef TARGET_ABI32
977 cpu_x86_load_seg(env, R_DS, __USER_DS);
978 cpu_x86_load_seg(env, R_ES, __USER_DS);
979 cpu_x86_load_seg(env, R_FS, __USER_DS);
980 cpu_x86_load_seg(env, R_GS, __USER_DS);
981 /* This hack makes Wine work... */
982 env->segs[R_FS].selector = 0;
983 #else
984 cpu_x86_load_seg(env, R_DS, 0);
985 cpu_x86_load_seg(env, R_ES, 0);
986 cpu_x86_load_seg(env, R_FS, 0);
987 cpu_x86_load_seg(env, R_GS, 0);
988 #endif
989 #elif defined(TARGET_SPARC)
991 int i;
992 env->pc = regs->pc;
993 env->npc = regs->npc;
994 env->y = regs->y;
995 for(i = 0; i < 8; i++)
996 env->gregs[i] = regs->u_regs[i];
997 for(i = 0; i < 8; i++)
998 env->regwptr[i] = regs->u_regs[i + 8];
1000 #else
1001 #error unsupported target CPU
1002 #endif
1004 if (gdbstub_port) {
1005 gdbserver_start (gdbstub_port);
1006 gdb_handlesig(env, 0);
1008 cpu_loop(env, bsd_type);
1009 /* never exits */
1010 return 0;