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