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