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