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