Regenerate BIOS and add patches for -boot option
[qemu/aliguori-queue.git] / bsd-user / main.c
blobe4a62554f7caf8da05aa8eb916f2e0df3141ab63
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 #if defined(TARGET_I386)
59 int cpu_get_pic_interrupt(CPUState *env)
61 return -1;
63 #endif
65 /* These are no-ops because we are not threadsafe. */
66 static inline void cpu_exec_start(CPUState *env)
70 static inline void cpu_exec_end(CPUState *env)
74 static inline void start_exclusive(void)
78 static inline void end_exclusive(void)
82 void fork_start(void)
86 void fork_end(int child)
88 if (child) {
89 gdbserver_fork(thread_env);
93 void cpu_list_lock(void)
97 void cpu_list_unlock(void)
101 #ifdef TARGET_I386
102 /***********************************************************/
103 /* CPUX86 core interface */
105 void cpu_smm_update(CPUState *env)
109 uint64_t cpu_get_tsc(CPUX86State *env)
111 return cpu_get_real_ticks();
114 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
115 int flags)
117 unsigned int e1, e2;
118 uint32_t *p;
119 e1 = (addr << 16) | (limit & 0xffff);
120 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
121 e2 |= flags;
122 p = ptr;
123 p[0] = tswap32(e1);
124 p[1] = tswap32(e2);
127 static uint64_t *idt_table;
128 #ifdef TARGET_X86_64
129 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
130 uint64_t addr, unsigned int sel)
132 uint32_t *p, e1, e2;
133 e1 = (addr & 0xffff) | (sel << 16);
134 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
135 p = ptr;
136 p[0] = tswap32(e1);
137 p[1] = tswap32(e2);
138 p[2] = tswap32(addr >> 32);
139 p[3] = 0;
141 /* only dpl matters as we do only user space emulation */
142 static void set_idt(int n, unsigned int dpl)
144 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
146 #else
147 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
148 uint32_t addr, unsigned int sel)
150 uint32_t *p, e1, e2;
151 e1 = (addr & 0xffff) | (sel << 16);
152 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
153 p = ptr;
154 p[0] = tswap32(e1);
155 p[1] = tswap32(e2);
158 /* only dpl matters as we do only user space emulation */
159 static void set_idt(int n, unsigned int dpl)
161 set_gate(idt_table + n, 0, dpl, 0, 0);
163 #endif
165 void cpu_loop(CPUX86State *env, enum BSDType bsd_type)
167 int trapnr;
168 abi_ulong pc;
169 //target_siginfo_t info;
171 for(;;) {
172 trapnr = cpu_x86_exec(env);
173 switch(trapnr) {
174 case 0x80:
175 /* syscall from int $0x80 */
176 env->regs[R_EAX] = do_openbsd_syscall(env,
177 env->regs[R_EAX],
178 env->regs[R_EBX],
179 env->regs[R_ECX],
180 env->regs[R_EDX],
181 env->regs[R_ESI],
182 env->regs[R_EDI],
183 env->regs[R_EBP]);
184 break;
185 #ifndef TARGET_ABI32
186 case EXCP_SYSCALL:
187 /* linux syscall from syscall intruction */
188 env->regs[R_EAX] = do_openbsd_syscall(env,
189 env->regs[R_EAX],
190 env->regs[R_EDI],
191 env->regs[R_ESI],
192 env->regs[R_EDX],
193 env->regs[10],
194 env->regs[8],
195 env->regs[9]);
196 env->eip = env->exception_next_eip;
197 break;
198 #endif
199 #if 0
200 case EXCP0B_NOSEG:
201 case EXCP0C_STACK:
202 info.si_signo = SIGBUS;
203 info.si_errno = 0;
204 info.si_code = TARGET_SI_KERNEL;
205 info._sifields._sigfault._addr = 0;
206 queue_signal(env, info.si_signo, &info);
207 break;
208 case EXCP0D_GPF:
209 /* XXX: potential problem if ABI32 */
210 #ifndef TARGET_X86_64
211 if (env->eflags & VM_MASK) {
212 handle_vm86_fault(env);
213 } else
214 #endif
216 info.si_signo = SIGSEGV;
217 info.si_errno = 0;
218 info.si_code = TARGET_SI_KERNEL;
219 info._sifields._sigfault._addr = 0;
220 queue_signal(env, info.si_signo, &info);
222 break;
223 case EXCP0E_PAGE:
224 info.si_signo = SIGSEGV;
225 info.si_errno = 0;
226 if (!(env->error_code & 1))
227 info.si_code = TARGET_SEGV_MAPERR;
228 else
229 info.si_code = TARGET_SEGV_ACCERR;
230 info._sifields._sigfault._addr = env->cr[2];
231 queue_signal(env, info.si_signo, &info);
232 break;
233 case EXCP00_DIVZ:
234 #ifndef TARGET_X86_64
235 if (env->eflags & VM_MASK) {
236 handle_vm86_trap(env, trapnr);
237 } else
238 #endif
240 /* division by zero */
241 info.si_signo = SIGFPE;
242 info.si_errno = 0;
243 info.si_code = TARGET_FPE_INTDIV;
244 info._sifields._sigfault._addr = env->eip;
245 queue_signal(env, info.si_signo, &info);
247 break;
248 case EXCP01_DB:
249 case EXCP03_INT3:
250 #ifndef TARGET_X86_64
251 if (env->eflags & VM_MASK) {
252 handle_vm86_trap(env, trapnr);
253 } else
254 #endif
256 info.si_signo = SIGTRAP;
257 info.si_errno = 0;
258 if (trapnr == EXCP01_DB) {
259 info.si_code = TARGET_TRAP_BRKPT;
260 info._sifields._sigfault._addr = env->eip;
261 } else {
262 info.si_code = TARGET_SI_KERNEL;
263 info._sifields._sigfault._addr = 0;
265 queue_signal(env, info.si_signo, &info);
267 break;
268 case EXCP04_INTO:
269 case EXCP05_BOUND:
270 #ifndef TARGET_X86_64
271 if (env->eflags & VM_MASK) {
272 handle_vm86_trap(env, trapnr);
273 } else
274 #endif
276 info.si_signo = SIGSEGV;
277 info.si_errno = 0;
278 info.si_code = TARGET_SI_KERNEL;
279 info._sifields._sigfault._addr = 0;
280 queue_signal(env, info.si_signo, &info);
282 break;
283 case EXCP06_ILLOP:
284 info.si_signo = SIGILL;
285 info.si_errno = 0;
286 info.si_code = TARGET_ILL_ILLOPN;
287 info._sifields._sigfault._addr = env->eip;
288 queue_signal(env, info.si_signo, &info);
289 break;
290 #endif
291 case EXCP_INTERRUPT:
292 /* just indicate that signals should be handled asap */
293 break;
294 #if 0
295 case EXCP_DEBUG:
297 int sig;
299 sig = gdb_handlesig (env, TARGET_SIGTRAP);
300 if (sig)
302 info.si_signo = sig;
303 info.si_errno = 0;
304 info.si_code = TARGET_TRAP_BRKPT;
305 queue_signal(env, info.si_signo, &info);
308 break;
309 #endif
310 default:
311 pc = env->segs[R_CS].base + env->eip;
312 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
313 (long)pc, trapnr);
314 abort();
316 process_pending_signals(env);
319 #endif
321 #ifdef TARGET_SPARC
322 #define SPARC64_STACK_BIAS 2047
324 //#define DEBUG_WIN
325 /* WARNING: dealing with register windows _is_ complicated. More info
326 can be found at http://www.sics.se/~psm/sparcstack.html */
327 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
329 index = (index + cwp * 16) % (16 * env->nwindows);
330 /* wrap handling : if cwp is on the last window, then we use the
331 registers 'after' the end */
332 if (index < 8 && env->cwp == env->nwindows - 1)
333 index += 16 * env->nwindows;
334 return index;
337 /* save the register window 'cwp1' */
338 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
340 unsigned int i;
341 abi_ulong sp_ptr;
343 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
344 #ifdef TARGET_SPARC64
345 if (sp_ptr & 3)
346 sp_ptr += SPARC64_STACK_BIAS;
347 #endif
348 #if defined(DEBUG_WIN)
349 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
350 sp_ptr, cwp1);
351 #endif
352 for(i = 0; i < 16; i++) {
353 /* FIXME - what to do if put_user() fails? */
354 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
355 sp_ptr += sizeof(abi_ulong);
359 static void save_window(CPUSPARCState *env)
361 #ifndef TARGET_SPARC64
362 unsigned int new_wim;
363 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
364 ((1LL << env->nwindows) - 1);
365 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
366 env->wim = new_wim;
367 #else
368 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
369 env->cansave++;
370 env->canrestore--;
371 #endif
374 static void restore_window(CPUSPARCState *env)
376 #ifndef TARGET_SPARC64
377 unsigned int new_wim;
378 #endif
379 unsigned int i, cwp1;
380 abi_ulong sp_ptr;
382 #ifndef TARGET_SPARC64
383 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
384 ((1LL << env->nwindows) - 1);
385 #endif
387 /* restore the invalid window */
388 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
389 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
390 #ifdef TARGET_SPARC64
391 if (sp_ptr & 3)
392 sp_ptr += SPARC64_STACK_BIAS;
393 #endif
394 #if defined(DEBUG_WIN)
395 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
396 sp_ptr, cwp1);
397 #endif
398 for(i = 0; i < 16; i++) {
399 /* FIXME - what to do if get_user() fails? */
400 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
401 sp_ptr += sizeof(abi_ulong);
403 #ifdef TARGET_SPARC64
404 env->canrestore++;
405 if (env->cleanwin < env->nwindows - 1)
406 env->cleanwin++;
407 env->cansave--;
408 #else
409 env->wim = new_wim;
410 #endif
413 static void flush_windows(CPUSPARCState *env)
415 int offset, cwp1;
417 offset = 1;
418 for(;;) {
419 /* if restore would invoke restore_window(), then we can stop */
420 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
421 #ifndef TARGET_SPARC64
422 if (env->wim & (1 << cwp1))
423 break;
424 #else
425 if (env->canrestore == 0)
426 break;
427 env->cansave++;
428 env->canrestore--;
429 #endif
430 save_window_offset(env, cwp1);
431 offset++;
433 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
434 #ifndef TARGET_SPARC64
435 /* set wim so that restore will reload the registers */
436 env->wim = 1 << cwp1;
437 #endif
438 #if defined(DEBUG_WIN)
439 printf("flush_windows: nb=%d\n", offset - 1);
440 #endif
443 void cpu_loop(CPUSPARCState *env, enum BSDType bsd_type)
445 int trapnr, ret, syscall_nr;
446 //target_siginfo_t info;
448 while (1) {
449 trapnr = cpu_sparc_exec (env);
451 switch (trapnr) {
452 #ifndef TARGET_SPARC64
453 case 0x80:
454 #else
455 case 0x100:
456 #endif
457 syscall_nr = env->gregs[1];
458 if (bsd_type == target_freebsd)
459 ret = do_freebsd_syscall(env, syscall_nr,
460 env->regwptr[0], env->regwptr[1],
461 env->regwptr[2], env->regwptr[3],
462 env->regwptr[4], env->regwptr[5]);
463 else if (bsd_type == target_netbsd)
464 ret = do_netbsd_syscall(env, syscall_nr,
465 env->regwptr[0], env->regwptr[1],
466 env->regwptr[2], env->regwptr[3],
467 env->regwptr[4], env->regwptr[5]);
468 else { //if (bsd_type == target_openbsd)
469 #if defined(TARGET_SPARC64)
470 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
471 TARGET_OPENBSD_SYSCALL_G2RFLAG);
472 #endif
473 ret = do_openbsd_syscall(env, syscall_nr,
474 env->regwptr[0], env->regwptr[1],
475 env->regwptr[2], env->regwptr[3],
476 env->regwptr[4], env->regwptr[5]);
478 if ((unsigned int)ret >= (unsigned int)(-515)) {
479 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
480 env->xcc |= PSR_CARRY;
481 #else
482 env->psr |= PSR_CARRY;
483 #endif
484 } else {
485 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
486 env->xcc &= ~PSR_CARRY;
487 #else
488 env->psr &= ~PSR_CARRY;
489 #endif
491 env->regwptr[0] = ret;
492 /* next instruction */
493 #if defined(TARGET_SPARC64)
494 if (bsd_type == target_openbsd &&
495 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
496 env->pc = env->gregs[2];
497 env->npc = env->pc + 4;
498 } else if (bsd_type == target_openbsd &&
499 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
500 env->pc = env->gregs[7];
501 env->npc = env->pc + 4;
502 } else {
503 env->pc = env->npc;
504 env->npc = env->npc + 4;
506 #else
507 env->pc = env->npc;
508 env->npc = env->npc + 4;
509 #endif
510 break;
511 case 0x83: /* flush windows */
512 #ifdef TARGET_ABI32
513 case 0x103:
514 #endif
515 flush_windows(env);
516 /* next instruction */
517 env->pc = env->npc;
518 env->npc = env->npc + 4;
519 break;
520 #ifndef TARGET_SPARC64
521 case TT_WIN_OVF: /* window overflow */
522 save_window(env);
523 break;
524 case TT_WIN_UNF: /* window underflow */
525 restore_window(env);
526 break;
527 case TT_TFAULT:
528 case TT_DFAULT:
529 #if 0
531 info.si_signo = SIGSEGV;
532 info.si_errno = 0;
533 /* XXX: check env->error_code */
534 info.si_code = TARGET_SEGV_MAPERR;
535 info._sifields._sigfault._addr = env->mmuregs[4];
536 queue_signal(env, info.si_signo, &info);
538 #endif
539 break;
540 #else
541 case TT_SPILL: /* window overflow */
542 save_window(env);
543 break;
544 case TT_FILL: /* window underflow */
545 restore_window(env);
546 break;
547 case TT_TFAULT:
548 case TT_DFAULT:
549 #if 0
551 info.si_signo = SIGSEGV;
552 info.si_errno = 0;
553 /* XXX: check env->error_code */
554 info.si_code = TARGET_SEGV_MAPERR;
555 if (trapnr == TT_DFAULT)
556 info._sifields._sigfault._addr = env->dmmuregs[4];
557 else
558 info._sifields._sigfault._addr = env->tsptr->tpc;
559 //queue_signal(env, info.si_signo, &info);
561 #endif
562 break;
563 #endif
564 case EXCP_INTERRUPT:
565 /* just indicate that signals should be handled asap */
566 break;
567 case EXCP_DEBUG:
569 int sig;
571 sig = gdb_handlesig (env, TARGET_SIGTRAP);
572 #if 0
573 if (sig)
575 info.si_signo = sig;
576 info.si_errno = 0;
577 info.si_code = TARGET_TRAP_BRKPT;
578 //queue_signal(env, info.si_signo, &info);
580 #endif
582 break;
583 default:
584 printf ("Unhandled trap: 0x%x\n", trapnr);
585 cpu_dump_state(env, stderr, fprintf, 0);
586 exit (1);
588 process_pending_signals (env);
592 #endif
594 static void usage(void)
596 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
597 "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
598 "BSD CPU emulator (compiled for %s emulation)\n"
599 "\n"
600 "Standard options:\n"
601 "-h print this help\n"
602 "-g port wait gdb connection to port\n"
603 "-L path set the elf interpreter prefix (default=%s)\n"
604 "-s size set the stack size in bytes (default=%ld)\n"
605 "-cpu model select CPU (-cpu ? for list)\n"
606 "-drop-ld-preload drop LD_PRELOAD for target process\n"
607 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
608 "\n"
609 "Debug options:\n"
610 "-d options activate log (logfile=%s)\n"
611 "-p pagesize set the host page size to 'pagesize'\n"
612 "-singlestep always run in singlestep mode\n"
613 "-strace log system calls\n"
614 "\n"
615 "Environment variables:\n"
616 "QEMU_STRACE Print system calls and arguments similar to the\n"
617 " 'strace' program. Enable by setting to any value.\n"
619 TARGET_ARCH,
620 interp_prefix,
621 x86_stack_size,
622 DEBUG_LOGFILE);
623 exit(1);
626 THREAD CPUState *thread_env;
628 /* Assumes contents are already zeroed. */
629 void init_task_state(TaskState *ts)
631 int i;
633 ts->used = 1;
634 ts->first_free = ts->sigqueue_table;
635 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
636 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
638 ts->sigqueue_table[i].next = NULL;
641 int main(int argc, char **argv)
643 const char *filename;
644 const char *cpu_model;
645 struct target_pt_regs regs1, *regs = &regs1;
646 struct image_info info1, *info = &info1;
647 TaskState ts1, *ts = &ts1;
648 CPUState *env;
649 int optind;
650 const char *r;
651 int gdbstub_port = 0;
652 int drop_ld_preload = 0, environ_count = 0;
653 char **target_environ, **wrk, **dst;
654 enum BSDType bsd_type = target_openbsd;
656 if (argc <= 1)
657 usage();
659 /* init debug */
660 cpu_set_log_filename(DEBUG_LOGFILE);
662 cpu_model = NULL;
663 optind = 1;
664 for(;;) {
665 if (optind >= argc)
666 break;
667 r = argv[optind];
668 if (r[0] != '-')
669 break;
670 optind++;
671 r++;
672 if (!strcmp(r, "-")) {
673 break;
674 } else if (!strcmp(r, "d")) {
675 int mask;
676 const CPULogItem *item;
678 if (optind >= argc)
679 break;
681 r = argv[optind++];
682 mask = cpu_str_to_log_mask(r);
683 if (!mask) {
684 printf("Log items (comma separated):\n");
685 for(item = cpu_log_items; item->mask != 0; item++) {
686 printf("%-10s %s\n", item->name, item->help);
688 exit(1);
690 cpu_set_log(mask);
691 } else if (!strcmp(r, "s")) {
692 r = argv[optind++];
693 x86_stack_size = strtol(r, (char **)&r, 0);
694 if (x86_stack_size <= 0)
695 usage();
696 if (*r == 'M')
697 x86_stack_size *= 1024 * 1024;
698 else if (*r == 'k' || *r == 'K')
699 x86_stack_size *= 1024;
700 } else if (!strcmp(r, "L")) {
701 interp_prefix = argv[optind++];
702 } else if (!strcmp(r, "p")) {
703 qemu_host_page_size = atoi(argv[optind++]);
704 if (qemu_host_page_size == 0 ||
705 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
706 fprintf(stderr, "page size must be a power of two\n");
707 exit(1);
709 } else if (!strcmp(r, "g")) {
710 gdbstub_port = atoi(argv[optind++]);
711 } else if (!strcmp(r, "r")) {
712 qemu_uname_release = argv[optind++];
713 } else if (!strcmp(r, "cpu")) {
714 cpu_model = argv[optind++];
715 if (strcmp(cpu_model, "?") == 0) {
716 /* XXX: implement xxx_cpu_list for targets that still miss it */
717 #if defined(cpu_list)
718 cpu_list(stdout, &fprintf);
719 #endif
720 exit(1);
722 } else if (!strcmp(r, "drop-ld-preload")) {
723 drop_ld_preload = 1;
724 } else if (!strcmp(r, "bsd")) {
725 if (!strcasecmp(argv[optind], "freebsd")) {
726 bsd_type = target_freebsd;
727 } else if (!strcasecmp(argv[optind], "netbsd")) {
728 bsd_type = target_netbsd;
729 } else if (!strcasecmp(argv[optind], "openbsd")) {
730 bsd_type = target_openbsd;
731 } else {
732 usage();
734 optind++;
735 } else if (!strcmp(r, "singlestep")) {
736 singlestep = 1;
737 } else if (!strcmp(r, "strace")) {
738 do_strace = 1;
739 } else
741 usage();
744 if (optind >= argc)
745 usage();
746 filename = argv[optind];
748 /* Zero out regs */
749 memset(regs, 0, sizeof(struct target_pt_regs));
751 /* Zero out image_info */
752 memset(info, 0, sizeof(struct image_info));
754 /* Scan interp_prefix dir for replacement files. */
755 init_paths(interp_prefix);
757 if (cpu_model == NULL) {
758 #if defined(TARGET_I386)
759 #ifdef TARGET_X86_64
760 cpu_model = "qemu64";
761 #else
762 cpu_model = "qemu32";
763 #endif
764 #elif defined(TARGET_SPARC)
765 #ifdef TARGET_SPARC64
766 cpu_model = "TI UltraSparc II";
767 #else
768 cpu_model = "Fujitsu MB86904";
769 #endif
770 #else
771 cpu_model = "any";
772 #endif
774 cpu_exec_init_all(0);
775 /* NOTE: we need to init the CPU at this stage to get
776 qemu_host_page_size */
777 env = cpu_init(cpu_model);
778 if (!env) {
779 fprintf(stderr, "Unable to find CPU definition\n");
780 exit(1);
782 thread_env = env;
784 if (getenv("QEMU_STRACE")) {
785 do_strace = 1;
788 wrk = environ;
789 while (*(wrk++))
790 environ_count++;
792 target_environ = malloc((environ_count + 1) * sizeof(char *));
793 if (!target_environ)
794 abort();
795 for (wrk = environ, dst = target_environ; *wrk; wrk++) {
796 if (drop_ld_preload && !strncmp(*wrk, "LD_PRELOAD=", 11))
797 continue;
798 *(dst++) = strdup(*wrk);
800 *dst = NULL; /* NULL terminate target_environ */
802 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
803 printf("Error loading %s\n", filename);
804 _exit(1);
807 for (wrk = target_environ; *wrk; wrk++) {
808 free(*wrk);
811 free(target_environ);
813 if (qemu_log_enabled()) {
814 log_page_dump();
816 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
817 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
818 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
819 info->start_code);
820 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
821 info->start_data);
822 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
823 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
824 info->start_stack);
825 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
826 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
829 target_set_brk(info->brk);
830 syscall_init();
831 signal_init();
833 /* build Task State */
834 memset(ts, 0, sizeof(TaskState));
835 init_task_state(ts);
836 ts->info = info;
837 env->opaque = ts;
839 #if defined(TARGET_I386)
840 cpu_x86_set_cpl(env, 3);
842 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
843 env->hflags |= HF_PE_MASK;
844 if (env->cpuid_features & CPUID_SSE) {
845 env->cr[4] |= CR4_OSFXSR_MASK;
846 env->hflags |= HF_OSFXSR_MASK;
848 #ifndef TARGET_ABI32
849 /* enable 64 bit mode if possible */
850 if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
851 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
852 exit(1);
854 env->cr[4] |= CR4_PAE_MASK;
855 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
856 env->hflags |= HF_LMA_MASK;
857 #endif
859 /* flags setup : we activate the IRQs by default as in user mode */
860 env->eflags |= IF_MASK;
862 /* linux register setup */
863 #ifndef TARGET_ABI32
864 env->regs[R_EAX] = regs->rax;
865 env->regs[R_EBX] = regs->rbx;
866 env->regs[R_ECX] = regs->rcx;
867 env->regs[R_EDX] = regs->rdx;
868 env->regs[R_ESI] = regs->rsi;
869 env->regs[R_EDI] = regs->rdi;
870 env->regs[R_EBP] = regs->rbp;
871 env->regs[R_ESP] = regs->rsp;
872 env->eip = regs->rip;
873 #else
874 env->regs[R_EAX] = regs->eax;
875 env->regs[R_EBX] = regs->ebx;
876 env->regs[R_ECX] = regs->ecx;
877 env->regs[R_EDX] = regs->edx;
878 env->regs[R_ESI] = regs->esi;
879 env->regs[R_EDI] = regs->edi;
880 env->regs[R_EBP] = regs->ebp;
881 env->regs[R_ESP] = regs->esp;
882 env->eip = regs->eip;
883 #endif
885 /* linux interrupt setup */
886 #ifndef TARGET_ABI32
887 env->idt.limit = 511;
888 #else
889 env->idt.limit = 255;
890 #endif
891 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
892 PROT_READ|PROT_WRITE,
893 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
894 idt_table = g2h(env->idt.base);
895 set_idt(0, 0);
896 set_idt(1, 0);
897 set_idt(2, 0);
898 set_idt(3, 3);
899 set_idt(4, 3);
900 set_idt(5, 0);
901 set_idt(6, 0);
902 set_idt(7, 0);
903 set_idt(8, 0);
904 set_idt(9, 0);
905 set_idt(10, 0);
906 set_idt(11, 0);
907 set_idt(12, 0);
908 set_idt(13, 0);
909 set_idt(14, 0);
910 set_idt(15, 0);
911 set_idt(16, 0);
912 set_idt(17, 0);
913 set_idt(18, 0);
914 set_idt(19, 0);
915 set_idt(0x80, 3);
917 /* linux segment setup */
919 uint64_t *gdt_table;
920 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
921 PROT_READ|PROT_WRITE,
922 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
923 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
924 gdt_table = g2h(env->gdt.base);
925 #ifdef TARGET_ABI32
926 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
927 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
928 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
929 #else
930 /* 64 bit code segment */
931 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
932 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
933 DESC_L_MASK |
934 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
935 #endif
936 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
937 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
938 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
941 cpu_x86_load_seg(env, R_CS, __USER_CS);
942 cpu_x86_load_seg(env, R_SS, __USER_DS);
943 #ifdef TARGET_ABI32
944 cpu_x86_load_seg(env, R_DS, __USER_DS);
945 cpu_x86_load_seg(env, R_ES, __USER_DS);
946 cpu_x86_load_seg(env, R_FS, __USER_DS);
947 cpu_x86_load_seg(env, R_GS, __USER_DS);
948 /* This hack makes Wine work... */
949 env->segs[R_FS].selector = 0;
950 #else
951 cpu_x86_load_seg(env, R_DS, 0);
952 cpu_x86_load_seg(env, R_ES, 0);
953 cpu_x86_load_seg(env, R_FS, 0);
954 cpu_x86_load_seg(env, R_GS, 0);
955 #endif
956 #elif defined(TARGET_SPARC)
958 int i;
959 env->pc = regs->pc;
960 env->npc = regs->npc;
961 env->y = regs->y;
962 for(i = 0; i < 8; i++)
963 env->gregs[i] = regs->u_regs[i];
964 for(i = 0; i < 8; i++)
965 env->regwptr[i] = regs->u_regs[i + 8];
967 #else
968 #error unsupported target CPU
969 #endif
971 if (gdbstub_port) {
972 gdbserver_start (gdbstub_port);
973 gdb_handlesig(env, 0);
975 cpu_loop(env, bsd_type);
976 /* never exits */
977 return 0;