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