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