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