]> git.proxmox.com Git - mirror_qemu.git/blob - bsd-user/main.c
bsd-user: style tweak: Use preferred block comments
[mirror_qemu.git] / bsd-user / main.c
1 /*
2 * qemu user main
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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>
27
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"
41
42 int singlestep;
43 unsigned long mmap_min_addr;
44 uintptr_t guest_base;
45 bool have_guest_base;
46 unsigned long reserved_va;
47
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;
52
53 /*
54 * XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
55 * we allocate a bigger stack. Need a better solution, for example
56 * by remapping the process stack directly at the right place
57 */
58 unsigned long x86_stack_size = 512 * 1024;
59
60 void gemu_log(const char *fmt, ...)
61 {
62 va_list ap;
63
64 va_start(ap, fmt);
65 vfprintf(stderr, fmt, ap);
66 va_end(ap);
67 }
68
69 #if defined(TARGET_I386)
70 int cpu_get_pic_interrupt(CPUX86State *env)
71 {
72 return -1;
73 }
74 #endif
75
76 void fork_start(void)
77 {
78 }
79
80 void fork_end(int child)
81 {
82 if (child) {
83 gdbserver_fork(thread_cpu);
84 }
85 }
86
87 #ifdef TARGET_I386
88 /***********************************************************/
89 /* CPUX86 core interface */
90
91 uint64_t cpu_get_tsc(CPUX86State *env)
92 {
93 return cpu_get_host_ticks();
94 }
95
96 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
97 int flags)
98 {
99 unsigned int e1, e2;
100 uint32_t *p;
101 e1 = (addr << 16) | (limit & 0xffff);
102 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
103 e2 |= flags;
104 p = ptr;
105 p[0] = tswap32(e1);
106 p[1] = tswap32(e2);
107 }
108
109 static uint64_t *idt_table;
110 #ifdef TARGET_X86_64
111 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
112 uint64_t addr, unsigned int sel)
113 {
114 uint32_t *p, e1, e2;
115 e1 = (addr & 0xffff) | (sel << 16);
116 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
117 p = ptr;
118 p[0] = tswap32(e1);
119 p[1] = tswap32(e2);
120 p[2] = tswap32(addr >> 32);
121 p[3] = 0;
122 }
123 /* only dpl matters as we do only user space emulation */
124 static void set_idt(int n, unsigned int dpl)
125 {
126 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
127 }
128 #else
129 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
130 uint32_t addr, unsigned int sel)
131 {
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 }
139
140 /* only dpl matters as we do only user space emulation */
141 static void set_idt(int n, unsigned int dpl)
142 {
143 set_gate(idt_table + n, 0, dpl, 0, 0);
144 }
145 #endif
146
147 void cpu_loop(CPUX86State *env)
148 {
149 CPUState *cs = env_cpu(env);
150 int trapnr;
151 abi_ulong pc;
152 /* target_siginfo_t info; */
153
154 for (;;) {
155 cpu_exec_start(cs);
156 trapnr = cpu_exec(cs);
157 cpu_exec_end(cs);
158 process_queued_cpu_work(cs);
159
160 switch (trapnr) {
161 case 0x80:
162 /* syscall from int $0x80 */
163 if (bsd_type == target_freebsd) {
164 abi_ulong params = (abi_ulong) env->regs[R_ESP] +
165 sizeof(int32_t);
166 int32_t syscall_nr = env->regs[R_EAX];
167 int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
168
169 if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
170 get_user_s32(syscall_nr, params);
171 params += sizeof(int32_t);
172 } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
173 get_user_s32(syscall_nr, params);
174 params += sizeof(int64_t);
175 }
176 get_user_s32(arg1, params);
177 params += sizeof(int32_t);
178 get_user_s32(arg2, params);
179 params += sizeof(int32_t);
180 get_user_s32(arg3, params);
181 params += sizeof(int32_t);
182 get_user_s32(arg4, params);
183 params += sizeof(int32_t);
184 get_user_s32(arg5, params);
185 params += sizeof(int32_t);
186 get_user_s32(arg6, params);
187 params += sizeof(int32_t);
188 get_user_s32(arg7, params);
189 params += sizeof(int32_t);
190 get_user_s32(arg8, params);
191 env->regs[R_EAX] = do_freebsd_syscall(env,
192 syscall_nr,
193 arg1,
194 arg2,
195 arg3,
196 arg4,
197 arg5,
198 arg6,
199 arg7,
200 arg8);
201 } else { /* if (bsd_type == target_openbsd) */
202 env->regs[R_EAX] = do_openbsd_syscall(env,
203 env->regs[R_EAX],
204 env->regs[R_EBX],
205 env->regs[R_ECX],
206 env->regs[R_EDX],
207 env->regs[R_ESI],
208 env->regs[R_EDI],
209 env->regs[R_EBP]);
210 }
211 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
212 env->regs[R_EAX] = -env->regs[R_EAX];
213 env->eflags |= CC_C;
214 } else {
215 env->eflags &= ~CC_C;
216 }
217 break;
218 #ifndef TARGET_ABI32
219 case EXCP_SYSCALL:
220 /* syscall from syscall instruction */
221 if (bsd_type == target_freebsd)
222 env->regs[R_EAX] = do_freebsd_syscall(env,
223 env->regs[R_EAX],
224 env->regs[R_EDI],
225 env->regs[R_ESI],
226 env->regs[R_EDX],
227 env->regs[R_ECX],
228 env->regs[8],
229 env->regs[9], 0, 0);
230 else { /* if (bsd_type == target_openbsd) */
231 env->regs[R_EAX] = do_openbsd_syscall(env,
232 env->regs[R_EAX],
233 env->regs[R_EDI],
234 env->regs[R_ESI],
235 env->regs[R_EDX],
236 env->regs[10],
237 env->regs[8],
238 env->regs[9]);
239 }
240 env->eip = env->exception_next_eip;
241 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
242 env->regs[R_EAX] = -env->regs[R_EAX];
243 env->eflags |= CC_C;
244 } else {
245 env->eflags &= ~CC_C;
246 }
247 break;
248 #endif
249 case EXCP_INTERRUPT:
250 /* just indicate that signals should be handled asap */
251 break;
252 default:
253 pc = env->segs[R_CS].base + env->eip;
254 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
255 (long)pc, trapnr);
256 abort();
257 }
258 process_pending_signals(env);
259 }
260 }
261 #endif
262
263 #ifdef TARGET_SPARC
264 #define SPARC64_STACK_BIAS 2047
265
266 /* #define DEBUG_WIN */
267 /*
268 * WARNING: dealing with register windows _is_ complicated. More info
269 * can be found at http://www.sics.se/~psm/sparcstack.html
270 */
271 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
272 {
273 index = (index + cwp * 16) % (16 * env->nwindows);
274 /*
275 * wrap handling : if cwp is on the last window, then we use the
276 * registers 'after' the end
277 */
278 if (index < 8 && env->cwp == env->nwindows - 1)
279 index += 16 * env->nwindows;
280 return index;
281 }
282
283 /* save the register window 'cwp1' */
284 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
285 {
286 unsigned int i;
287 abi_ulong sp_ptr;
288
289 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
290 #ifdef TARGET_SPARC64
291 if (sp_ptr & 3)
292 sp_ptr += SPARC64_STACK_BIAS;
293 #endif
294 #if defined(DEBUG_WIN)
295 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
296 sp_ptr, cwp1);
297 #endif
298 for (i = 0; i < 16; i++) {
299 /* FIXME - what to do if put_user() fails? */
300 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
301 sp_ptr += sizeof(abi_ulong);
302 }
303 }
304
305 static void save_window(CPUSPARCState *env)
306 {
307 #ifndef TARGET_SPARC64
308 unsigned int new_wim;
309 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
310 ((1LL << env->nwindows) - 1);
311 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
312 env->wim = new_wim;
313 #else
314 /*
315 * cansave is zero if the spill trap handler is triggered by `save` and
316 * nonzero if triggered by a `flushw`
317 */
318 save_window_offset(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2));
319 env->cansave++;
320 env->canrestore--;
321 #endif
322 }
323
324 static void restore_window(CPUSPARCState *env)
325 {
326 #ifndef TARGET_SPARC64
327 unsigned int new_wim;
328 #endif
329 unsigned int i, cwp1;
330 abi_ulong sp_ptr;
331
332 #ifndef TARGET_SPARC64
333 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
334 ((1LL << env->nwindows) - 1);
335 #endif
336
337 /* restore the invalid window */
338 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
339 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
340 #ifdef TARGET_SPARC64
341 if (sp_ptr & 3)
342 sp_ptr += SPARC64_STACK_BIAS;
343 #endif
344 #if defined(DEBUG_WIN)
345 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
346 sp_ptr, cwp1);
347 #endif
348 for (i = 0; i < 16; i++) {
349 /* FIXME - what to do if get_user() fails? */
350 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
351 sp_ptr += sizeof(abi_ulong);
352 }
353 #ifdef TARGET_SPARC64
354 env->canrestore++;
355 if (env->cleanwin < env->nwindows - 1)
356 env->cleanwin++;
357 env->cansave--;
358 #else
359 env->wim = new_wim;
360 #endif
361 }
362
363 static void flush_windows(CPUSPARCState *env)
364 {
365 int offset, cwp1;
366
367 offset = 1;
368 for (;;) {
369 /* if restore would invoke restore_window(), then we can stop */
370 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
371 #ifndef TARGET_SPARC64
372 if (env->wim & (1 << cwp1))
373 break;
374 #else
375 if (env->canrestore == 0)
376 break;
377 env->cansave++;
378 env->canrestore--;
379 #endif
380 save_window_offset(env, cwp1);
381 offset++;
382 }
383 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
384 #ifndef TARGET_SPARC64
385 /* set wim so that restore will reload the registers */
386 env->wim = 1 << cwp1;
387 #endif
388 #if defined(DEBUG_WIN)
389 printf("flush_windows: nb=%d\n", offset - 1);
390 #endif
391 }
392
393 void cpu_loop(CPUSPARCState *env)
394 {
395 CPUState *cs = env_cpu(env);
396 int trapnr, ret, syscall_nr;
397 /* target_siginfo_t info; */
398
399 while (1) {
400 cpu_exec_start(cs);
401 trapnr = cpu_exec(cs);
402 cpu_exec_end(cs);
403 process_queued_cpu_work(cs);
404
405 switch (trapnr) {
406 #ifndef TARGET_SPARC64
407 case 0x80:
408 #else
409 /* FreeBSD uses 0x141 for syscalls too */
410 case 0x141:
411 if (bsd_type != target_freebsd)
412 goto badtrap;
413 /* fallthrough */
414 case 0x100:
415 #endif
416 syscall_nr = env->gregs[1];
417 if (bsd_type == target_freebsd)
418 ret = do_freebsd_syscall(env, syscall_nr,
419 env->regwptr[0], env->regwptr[1],
420 env->regwptr[2], env->regwptr[3],
421 env->regwptr[4], env->regwptr[5], 0, 0);
422 else if (bsd_type == target_netbsd)
423 ret = do_netbsd_syscall(env, syscall_nr,
424 env->regwptr[0], env->regwptr[1],
425 env->regwptr[2], env->regwptr[3],
426 env->regwptr[4], env->regwptr[5]);
427 else { /* if (bsd_type == target_openbsd) */
428 #if defined(TARGET_SPARC64)
429 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
430 TARGET_OPENBSD_SYSCALL_G2RFLAG);
431 #endif
432 ret = do_openbsd_syscall(env, syscall_nr,
433 env->regwptr[0], env->regwptr[1],
434 env->regwptr[2], env->regwptr[3],
435 env->regwptr[4], env->regwptr[5]);
436 }
437 if ((unsigned int)ret >= (unsigned int)(-515)) {
438 ret = -ret;
439 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
440 env->xcc |= PSR_CARRY;
441 #else
442 env->psr |= PSR_CARRY;
443 #endif
444 } else {
445 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
446 env->xcc &= ~PSR_CARRY;
447 #else
448 env->psr &= ~PSR_CARRY;
449 #endif
450 }
451 env->regwptr[0] = ret;
452 /* next instruction */
453 #if defined(TARGET_SPARC64)
454 if (bsd_type == target_openbsd &&
455 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
456 env->pc = env->gregs[2];
457 env->npc = env->pc + 4;
458 } else if (bsd_type == target_openbsd &&
459 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
460 env->pc = env->gregs[7];
461 env->npc = env->pc + 4;
462 } else {
463 env->pc = env->npc;
464 env->npc = env->npc + 4;
465 }
466 #else
467 env->pc = env->npc;
468 env->npc = env->npc + 4;
469 #endif
470 break;
471 case 0x83: /* flush windows */
472 #ifdef TARGET_ABI32
473 case 0x103:
474 #endif
475 flush_windows(env);
476 /* next instruction */
477 env->pc = env->npc;
478 env->npc = env->npc + 4;
479 break;
480 #ifndef TARGET_SPARC64
481 case TT_WIN_OVF: /* window overflow */
482 save_window(env);
483 break;
484 case TT_WIN_UNF: /* window underflow */
485 restore_window(env);
486 break;
487 case TT_TFAULT:
488 case TT_DFAULT:
489 break;
490 #else
491 case TT_SPILL: /* window overflow */
492 save_window(env);
493 break;
494 case TT_FILL: /* window underflow */
495 restore_window(env);
496 break;
497 case TT_TFAULT:
498 case TT_DFAULT:
499 break;
500 #endif
501 case EXCP_INTERRUPT:
502 /* just indicate that signals should be handled asap */
503 break;
504 case EXCP_DEBUG:
505 {
506 gdb_handlesig(cs, TARGET_SIGTRAP);
507 }
508 break;
509 default:
510 #ifdef TARGET_SPARC64
511 badtrap:
512 #endif
513 printf("Unhandled trap: 0x%x\n", trapnr);
514 cpu_dump_state(cs, stderr, 0);
515 exit(1);
516 }
517 process_pending_signals(env);
518 }
519 }
520
521 #endif
522
523 static void usage(void)
524 {
525 printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
526 "\n" QEMU_COPYRIGHT "\n"
527 "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
528 "BSD CPU emulator (compiled for %s emulation)\n"
529 "\n"
530 "Standard options:\n"
531 "-h print this help\n"
532 "-g port wait gdb connection to port\n"
533 "-L path set the elf interpreter prefix (default=%s)\n"
534 "-s size set the stack size in bytes (default=%ld)\n"
535 "-cpu model select CPU (-cpu help for list)\n"
536 "-drop-ld-preload drop LD_PRELOAD for target process\n"
537 "-E var=value sets/modifies targets environment variable(s)\n"
538 "-U var unsets targets environment variable(s)\n"
539 "-B address set guest_base address to address\n"
540 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
541 "\n"
542 "Debug options:\n"
543 "-d item1[,...] enable logging of specified items\n"
544 " (use '-d help' for a list of log items)\n"
545 "-D logfile write logs to 'logfile' (default stderr)\n"
546 "-p pagesize set the host page size to 'pagesize'\n"
547 "-singlestep always run in singlestep mode\n"
548 "-strace log system calls\n"
549 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
550 " specify tracing options\n"
551 "\n"
552 "Environment variables:\n"
553 "QEMU_STRACE Print system calls and arguments similar to the\n"
554 " 'strace' program. Enable by setting to any value.\n"
555 "You can use -E and -U options to set/unset environment variables\n"
556 "for target process. It is possible to provide several variables\n"
557 "by repeating the option. For example:\n"
558 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
559 "Note that if you provide several changes to single variable\n"
560 "last change will stay in effect.\n"
561 "\n"
562 QEMU_HELP_BOTTOM "\n"
563 ,
564 TARGET_NAME,
565 interp_prefix,
566 x86_stack_size);
567 exit(1);
568 }
569
570 THREAD CPUState *thread_cpu;
571
572 bool qemu_cpu_is_self(CPUState *cpu)
573 {
574 return thread_cpu == cpu;
575 }
576
577 void qemu_cpu_kick(CPUState *cpu)
578 {
579 cpu_exit(cpu);
580 }
581
582 /* Assumes contents are already zeroed. */
583 void init_task_state(TaskState *ts)
584 {
585 int i;
586
587 ts->used = 1;
588 ts->first_free = ts->sigqueue_table;
589 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
590 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
591 }
592 ts->sigqueue_table[i].next = NULL;
593 }
594
595 int main(int argc, char **argv)
596 {
597 const char *filename;
598 const char *cpu_model;
599 const char *cpu_type;
600 const char *log_file = NULL;
601 const char *log_mask = NULL;
602 struct target_pt_regs regs1, *regs = &regs1;
603 struct image_info info1, *info = &info1;
604 TaskState ts1, *ts = &ts1;
605 CPUArchState *env;
606 CPUState *cpu;
607 int optind;
608 const char *r;
609 const char *gdbstub = NULL;
610 char **target_environ, **wrk;
611 envlist_t *envlist = NULL;
612 bsd_type = target_openbsd;
613
614 if (argc <= 1)
615 usage();
616
617 error_init(argv[0]);
618 module_call_init(MODULE_INIT_TRACE);
619 qemu_init_cpu_list();
620 module_call_init(MODULE_INIT_QOM);
621
622 envlist = envlist_create();
623
624 /* add current environment into the list */
625 for (wrk = environ; *wrk != NULL; wrk++) {
626 (void) envlist_setenv(envlist, *wrk);
627 }
628
629 cpu_model = NULL;
630
631 qemu_add_opts(&qemu_trace_opts);
632
633 optind = 1;
634 for (;;) {
635 if (optind >= argc)
636 break;
637 r = argv[optind];
638 if (r[0] != '-')
639 break;
640 optind++;
641 r++;
642 if (!strcmp(r, "-")) {
643 break;
644 } else if (!strcmp(r, "d")) {
645 if (optind >= argc) {
646 break;
647 }
648 log_mask = argv[optind++];
649 } else if (!strcmp(r, "D")) {
650 if (optind >= argc) {
651 break;
652 }
653 log_file = argv[optind++];
654 } else if (!strcmp(r, "E")) {
655 r = argv[optind++];
656 if (envlist_setenv(envlist, r) != 0)
657 usage();
658 } else if (!strcmp(r, "ignore-environment")) {
659 envlist_free(envlist);
660 envlist = envlist_create();
661 } else if (!strcmp(r, "U")) {
662 r = argv[optind++];
663 if (envlist_unsetenv(envlist, r) != 0)
664 usage();
665 } else if (!strcmp(r, "s")) {
666 r = argv[optind++];
667 x86_stack_size = strtol(r, (char **)&r, 0);
668 if (x86_stack_size <= 0)
669 usage();
670 if (*r == 'M')
671 x86_stack_size *= MiB;
672 else if (*r == 'k' || *r == 'K')
673 x86_stack_size *= KiB;
674 } else if (!strcmp(r, "L")) {
675 interp_prefix = argv[optind++];
676 } else if (!strcmp(r, "p")) {
677 qemu_host_page_size = atoi(argv[optind++]);
678 if (qemu_host_page_size == 0 ||
679 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
680 fprintf(stderr, "page size must be a power of two\n");
681 exit(1);
682 }
683 } else if (!strcmp(r, "g")) {
684 gdbstub = g_strdup(argv[optind++]);
685 } else if (!strcmp(r, "r")) {
686 qemu_uname_release = argv[optind++];
687 } else if (!strcmp(r, "cpu")) {
688 cpu_model = argv[optind++];
689 if (is_help_option(cpu_model)) {
690 /* XXX: implement xxx_cpu_list for targets that still miss it */
691 #if defined(cpu_list)
692 cpu_list();
693 #endif
694 exit(1);
695 }
696 } else if (!strcmp(r, "B")) {
697 guest_base = strtol(argv[optind++], NULL, 0);
698 have_guest_base = true;
699 } else if (!strcmp(r, "drop-ld-preload")) {
700 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
701 } else if (!strcmp(r, "bsd")) {
702 if (!strcasecmp(argv[optind], "freebsd")) {
703 bsd_type = target_freebsd;
704 } else if (!strcasecmp(argv[optind], "netbsd")) {
705 bsd_type = target_netbsd;
706 } else if (!strcasecmp(argv[optind], "openbsd")) {
707 bsd_type = target_openbsd;
708 } else {
709 usage();
710 }
711 optind++;
712 } else if (!strcmp(r, "singlestep")) {
713 singlestep = 1;
714 } else if (!strcmp(r, "strace")) {
715 do_strace = 1;
716 } else if (!strcmp(r, "trace")) {
717 trace_opt_parse(optarg);
718 } else {
719 usage();
720 }
721 }
722
723 /* init debug */
724 qemu_log_needs_buffers();
725 qemu_set_log_filename(log_file, &error_fatal);
726 if (log_mask) {
727 int mask;
728
729 mask = qemu_str_to_log_mask(log_mask);
730 if (!mask) {
731 qemu_print_log_usage(stdout);
732 exit(1);
733 }
734 qemu_set_log(mask);
735 }
736
737 if (optind >= argc) {
738 usage();
739 }
740 filename = argv[optind];
741
742 if (!trace_init_backends()) {
743 exit(1);
744 }
745 trace_init_file();
746
747 /* Zero out regs */
748 memset(regs, 0, sizeof(struct target_pt_regs));
749
750 /* Zero out image_info */
751 memset(info, 0, sizeof(struct image_info));
752
753 /* Scan interp_prefix dir for replacement files. */
754 init_paths(interp_prefix);
755
756 if (cpu_model == NULL) {
757 #if defined(TARGET_I386)
758 #ifdef TARGET_X86_64
759 cpu_model = "qemu64";
760 #else
761 cpu_model = "qemu32";
762 #endif
763 #elif defined(TARGET_SPARC)
764 #ifdef TARGET_SPARC64
765 cpu_model = "TI UltraSparc II";
766 #else
767 cpu_model = "Fujitsu MB86904";
768 #endif
769 #else
770 cpu_model = "any";
771 #endif
772 }
773
774 cpu_type = parse_cpu_option(cpu_model);
775 /* init tcg before creating CPUs and to get qemu_host_page_size */
776 {
777 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
778
779 ac->init_machine(NULL);
780 accel_init_interfaces(ac);
781 }
782 cpu = cpu_create(cpu_type);
783 env = cpu->env_ptr;
784 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
785 cpu_reset(cpu);
786 #endif
787 thread_cpu = cpu;
788
789 if (getenv("QEMU_STRACE")) {
790 do_strace = 1;
791 }
792
793 target_environ = envlist_to_environ(envlist, NULL);
794 envlist_free(envlist);
795
796 /*
797 * Now that page sizes are configured in tcg_exec_init() we can do
798 * proper page alignment for guest_base.
799 */
800 guest_base = HOST_PAGE_ALIGN(guest_base);
801
802 /*
803 * Read in mmap_min_addr kernel parameter. This value is used
804 * When loading the ELF image to determine whether guest_base
805 * is needed.
806 *
807 * When user has explicitly set the quest base, we skip this
808 * test.
809 */
810 if (!have_guest_base) {
811 FILE *fp;
812
813 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
814 unsigned long tmp;
815 if (fscanf(fp, "%lu", &tmp) == 1) {
816 mmap_min_addr = tmp;
817 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr);
818 }
819 fclose(fp);
820 }
821 }
822
823 if (loader_exec(filename, argv + optind, target_environ, regs, info) != 0) {
824 printf("Error loading %s\n", filename);
825 _exit(1);
826 }
827
828 for (wrk = target_environ; *wrk; wrk++) {
829 g_free(*wrk);
830 }
831
832 g_free(target_environ);
833
834 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
835 qemu_log("guest_base %p\n", (void *)guest_base);
836 log_page_dump("binary load");
837
838 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
839 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
840 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
841 info->start_code);
842 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
843 info->start_data);
844 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
845 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
846 info->start_stack);
847 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
848 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
849 }
850
851 target_set_brk(info->brk);
852 syscall_init();
853 signal_init();
854
855 /*
856 * Now that we've loaded the binary, GUEST_BASE is fixed. Delay
857 * generating the prologue until now so that the prologue can take
858 * the real value of GUEST_BASE into account.
859 */
860 tcg_prologue_init(tcg_ctx);
861 tcg_region_init();
862
863 /* build Task State */
864 memset(ts, 0, sizeof(TaskState));
865 init_task_state(ts);
866 ts->info = info;
867 cpu->opaque = ts;
868
869 #if defined(TARGET_I386)
870 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
871 env->hflags |= HF_PE_MASK | HF_CPL_MASK;
872 if (env->features[FEAT_1_EDX] & CPUID_SSE) {
873 env->cr[4] |= CR4_OSFXSR_MASK;
874 env->hflags |= HF_OSFXSR_MASK;
875 }
876 #ifndef TARGET_ABI32
877 /* enable 64 bit mode if possible */
878 if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
879 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
880 exit(1);
881 }
882 env->cr[4] |= CR4_PAE_MASK;
883 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
884 env->hflags |= HF_LMA_MASK;
885 #endif
886
887 /* flags setup : we activate the IRQs by default as in user mode */
888 env->eflags |= IF_MASK;
889
890 /* linux register setup */
891 #ifndef TARGET_ABI32
892 env->regs[R_EAX] = regs->rax;
893 env->regs[R_EBX] = regs->rbx;
894 env->regs[R_ECX] = regs->rcx;
895 env->regs[R_EDX] = regs->rdx;
896 env->regs[R_ESI] = regs->rsi;
897 env->regs[R_EDI] = regs->rdi;
898 env->regs[R_EBP] = regs->rbp;
899 env->regs[R_ESP] = regs->rsp;
900 env->eip = regs->rip;
901 #else
902 env->regs[R_EAX] = regs->eax;
903 env->regs[R_EBX] = regs->ebx;
904 env->regs[R_ECX] = regs->ecx;
905 env->regs[R_EDX] = regs->edx;
906 env->regs[R_ESI] = regs->esi;
907 env->regs[R_EDI] = regs->edi;
908 env->regs[R_EBP] = regs->ebp;
909 env->regs[R_ESP] = regs->esp;
910 env->eip = regs->eip;
911 #endif
912
913 /* linux interrupt setup */
914 #ifndef TARGET_ABI32
915 env->idt.limit = 511;
916 #else
917 env->idt.limit = 255;
918 #endif
919 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
920 PROT_READ | PROT_WRITE,
921 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
922 idt_table = g2h_untagged(env->idt.base);
923 set_idt(0, 0);
924 set_idt(1, 0);
925 set_idt(2, 0);
926 set_idt(3, 3);
927 set_idt(4, 3);
928 set_idt(5, 0);
929 set_idt(6, 0);
930 set_idt(7, 0);
931 set_idt(8, 0);
932 set_idt(9, 0);
933 set_idt(10, 0);
934 set_idt(11, 0);
935 set_idt(12, 0);
936 set_idt(13, 0);
937 set_idt(14, 0);
938 set_idt(15, 0);
939 set_idt(16, 0);
940 set_idt(17, 0);
941 set_idt(18, 0);
942 set_idt(19, 0);
943 set_idt(0x80, 3);
944
945 /* linux segment setup */
946 {
947 uint64_t *gdt_table;
948 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
949 PROT_READ | PROT_WRITE,
950 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
951 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
952 gdt_table = g2h_untagged(env->gdt.base);
953 #ifdef TARGET_ABI32
954 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
955 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
956 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
957 #else
958 /* 64 bit code segment */
959 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
960 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
961 DESC_L_MASK |
962 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
963 #endif
964 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
965 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
966 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
967 }
968
969 cpu_x86_load_seg(env, R_CS, __USER_CS);
970 cpu_x86_load_seg(env, R_SS, __USER_DS);
971 #ifdef TARGET_ABI32
972 cpu_x86_load_seg(env, R_DS, __USER_DS);
973 cpu_x86_load_seg(env, R_ES, __USER_DS);
974 cpu_x86_load_seg(env, R_FS, __USER_DS);
975 cpu_x86_load_seg(env, R_GS, __USER_DS);
976 /* This hack makes Wine work... */
977 env->segs[R_FS].selector = 0;
978 #else
979 cpu_x86_load_seg(env, R_DS, 0);
980 cpu_x86_load_seg(env, R_ES, 0);
981 cpu_x86_load_seg(env, R_FS, 0);
982 cpu_x86_load_seg(env, R_GS, 0);
983 #endif
984 #elif defined(TARGET_SPARC)
985 {
986 int i;
987 env->pc = regs->pc;
988 env->npc = regs->npc;
989 env->y = regs->y;
990 for (i = 0; i < 8; i++)
991 env->gregs[i] = regs->u_regs[i];
992 for (i = 0; i < 8; i++)
993 env->regwptr[i] = regs->u_regs[i + 8];
994 }
995 #else
996 #error unsupported target CPU
997 #endif
998
999 if (gdbstub) {
1000 gdbserver_start(gdbstub);
1001 gdb_handlesig(cpu, 0);
1002 }
1003 cpu_loop(env);
1004 /* never exits */
1005 return 0;
1006 }