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