4 * Copyright (c) 2003-2008 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <machine/trap.h>
29 #include "qemu-common.h"
33 #define DEBUG_LOGFILE "/tmp/qemu.log"
35 static const char *interp_prefix
= CONFIG_QEMU_PREFIX
;
36 const char *qemu_uname_release
= CONFIG_UNAME_RELEASE
;
37 extern char **environ
;
39 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
40 we allocate a bigger stack. Need a better solution, for example
41 by remapping the process stack directly at the right place */
42 unsigned long x86_stack_size
= 512 * 1024;
44 void gemu_log(const char *fmt
, ...)
49 vfprintf(stderr
, fmt
, ap
);
53 #define SPARC64_STACK_BIAS 2047
56 /* WARNING: dealing with register windows _is_ complicated. More info
57 can be found at http://www.sics.se/~psm/sparcstack.html */
58 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
60 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
61 /* wrap handling : if cwp is on the last window, then we use the
62 registers 'after' the end */
63 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
64 index
+= 16 * env
->nwindows
;
68 /* save the register window 'cwp1' */
69 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
74 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
77 sp_ptr
+= SPARC64_STACK_BIAS
;
79 #if defined(DEBUG_WIN)
80 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
83 for(i
= 0; i
< 16; i
++) {
84 /* FIXME - what to do if put_user() fails? */
85 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
86 sp_ptr
+= sizeof(abi_ulong
);
90 static void save_window(CPUSPARCState
*env
)
92 #ifndef TARGET_SPARC64
94 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
95 ((1LL << env
->nwindows
) - 1);
96 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
99 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
105 static void restore_window(CPUSPARCState
*env
)
107 #ifndef TARGET_SPARC64
108 unsigned int new_wim
;
110 unsigned int i
, cwp1
;
113 #ifndef TARGET_SPARC64
114 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
115 ((1LL << env
->nwindows
) - 1);
118 /* restore the invalid window */
119 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
120 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
121 #ifdef TARGET_SPARC64
123 sp_ptr
+= SPARC64_STACK_BIAS
;
125 #if defined(DEBUG_WIN)
126 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
129 for(i
= 0; i
< 16; i
++) {
130 /* FIXME - what to do if get_user() fails? */
131 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
132 sp_ptr
+= sizeof(abi_ulong
);
134 #ifdef TARGET_SPARC64
136 if (env
->cleanwin
< env
->nwindows
- 1)
144 static void flush_windows(CPUSPARCState
*env
)
150 /* if restore would invoke restore_window(), then we can stop */
151 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
152 #ifndef TARGET_SPARC64
153 if (env
->wim
& (1 << cwp1
))
156 if (env
->canrestore
== 0)
161 save_window_offset(env
, cwp1
);
164 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
165 #ifndef TARGET_SPARC64
166 /* set wim so that restore will reload the registers */
167 env
->wim
= 1 << cwp1
;
169 #if defined(DEBUG_WIN)
170 printf("flush_windows: nb=%d\n", offset
- 1);
174 void cpu_loop(CPUSPARCState
*env
, enum BSDType bsd_type
)
176 int trapnr
, ret
, syscall_nr
;
177 //target_siginfo_t info;
180 trapnr
= cpu_sparc_exec (env
);
184 syscall_nr
= env
->gregs
[1];
185 #if defined(TARGET_SPARC)
186 syscall_nr
&= ~(SYSCALL_G7RFLAG
| SYSCALL_G2RFLAG
);
188 if (bsd_type
== target_freebsd
)
189 ret
= do_freebsd_syscall(env
, syscall_nr
,
190 env
->regwptr
[0], env
->regwptr
[1],
191 env
->regwptr
[2], env
->regwptr
[3],
192 env
->regwptr
[4], env
->regwptr
[5]);
193 else if (bsd_type
== target_netbsd
)
194 ret
= do_netbsd_syscall(env
, syscall_nr
,
195 env
->regwptr
[0], env
->regwptr
[1],
196 env
->regwptr
[2], env
->regwptr
[3],
197 env
->regwptr
[4], env
->regwptr
[5]);
198 else //if (bsd_type == target_openbsd)
199 ret
= do_openbsd_syscall(env
, syscall_nr
,
200 env
->regwptr
[0], env
->regwptr
[1],
201 env
->regwptr
[2], env
->regwptr
[3],
202 env
->regwptr
[4], env
->regwptr
[5]);
203 if ((unsigned int)ret
>= (unsigned int)(-515)) {
204 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
205 env
->xcc
|= PSR_CARRY
;
207 env
->psr
|= PSR_CARRY
;
210 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
211 env
->xcc
&= ~PSR_CARRY
;
213 env
->psr
&= ~PSR_CARRY
;
216 env
->regwptr
[0] = ret
;
217 /* next instruction */
218 #if defined(TARGET_SPARC)
219 if (env
->gregs
[1] & SYSCALL_G2RFLAG
) {
220 env
->pc
= env
->gregs
[2];
221 env
->npc
= env
->pc
+ 4;
222 } else if (env
->gregs
[1] & SYSCALL_G7RFLAG
) {
223 env
->pc
= env
->gregs
[7];
224 env
->npc
= env
->pc
+ 4;
227 env
->npc
= env
->npc
+ 4;
231 env
->npc
= env
->npc
+ 4;
234 case 0x83: /* flush windows */
239 /* next instruction */
241 env
->npc
= env
->npc
+ 4;
243 #ifndef TARGET_SPARC64
244 case TT_WIN_OVF
: /* window overflow */
247 case TT_WIN_UNF
: /* window underflow */
254 info
.si_signo
= SIGSEGV
;
256 /* XXX: check env->error_code */
257 info
.si_code
= TARGET_SEGV_MAPERR
;
258 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
259 queue_signal(env
, info
.si_signo
, &info
);
264 case TT_SPILL
: /* window overflow */
267 case TT_FILL
: /* window underflow */
274 info
.si_signo
= SIGSEGV
;
276 /* XXX: check env->error_code */
277 info
.si_code
= TARGET_SEGV_MAPERR
;
278 if (trapnr
== TT_DFAULT
)
279 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
281 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
282 //queue_signal(env, info.si_signo, &info);
288 /* just indicate that signals should be handled asap */
294 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
300 info
.si_code
= TARGET_TRAP_BRKPT
;
301 //queue_signal(env, info.si_signo, &info);
307 printf ("Unhandled trap: 0x%x\n", trapnr
);
308 cpu_dump_state(env
, stderr
, fprintf
, 0);
311 process_pending_signals (env
);
317 static void usage(void)
319 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
320 "usage: qemu-" TARGET_ARCH
" [options] program [arguments...]\n"
321 "BSD CPU emulator (compiled for %s emulation)\n"
323 "Standard options:\n"
324 "-h print this help\n"
325 "-g port wait gdb connection to port\n"
326 "-L path set the elf interpreter prefix (default=%s)\n"
327 "-s size set the stack size in bytes (default=%ld)\n"
328 "-cpu model select CPU (-cpu ? for list)\n"
329 "-drop-ld-preload drop LD_PRELOAD for target process\n"
330 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
333 "-d options activate log (logfile=%s)\n"
334 "-p pagesize set the host page size to 'pagesize'\n"
335 "-strace log system calls\n"
337 "Environment variables:\n"
338 "QEMU_STRACE Print system calls and arguments similar to the\n"
339 " 'strace' program. Enable by setting to any value.\n"
348 THREAD CPUState
*thread_env
;
350 /* Assumes contents are already zeroed. */
351 void init_task_state(TaskState
*ts
)
356 ts
->first_free
= ts
->sigqueue_table
;
357 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
358 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
360 ts
->sigqueue_table
[i
].next
= NULL
;
363 int main(int argc
, char **argv
)
365 const char *filename
;
366 const char *cpu_model
;
367 struct target_pt_regs regs1
, *regs
= ®s1
;
368 struct image_info info1
, *info
= &info1
;
369 TaskState ts1
, *ts
= &ts1
;
373 int gdbstub_port
= 0;
374 int drop_ld_preload
= 0, environ_count
= 0;
375 char **target_environ
, **wrk
, **dst
;
376 enum BSDType bsd_type
= target_openbsd
;
382 cpu_set_log_filename(DEBUG_LOGFILE
);
394 if (!strcmp(r
, "-")) {
396 } else if (!strcmp(r
, "d")) {
398 const CPULogItem
*item
;
404 mask
= cpu_str_to_log_mask(r
);
406 printf("Log items (comma separated):\n");
407 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
408 printf("%-10s %s\n", item
->name
, item
->help
);
413 } else if (!strcmp(r
, "s")) {
415 x86_stack_size
= strtol(r
, (char **)&r
, 0);
416 if (x86_stack_size
<= 0)
419 x86_stack_size
*= 1024 * 1024;
420 else if (*r
== 'k' || *r
== 'K')
421 x86_stack_size
*= 1024;
422 } else if (!strcmp(r
, "L")) {
423 interp_prefix
= argv
[optind
++];
424 } else if (!strcmp(r
, "p")) {
425 qemu_host_page_size
= atoi(argv
[optind
++]);
426 if (qemu_host_page_size
== 0 ||
427 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
428 fprintf(stderr
, "page size must be a power of two\n");
431 } else if (!strcmp(r
, "g")) {
432 gdbstub_port
= atoi(argv
[optind
++]);
433 } else if (!strcmp(r
, "r")) {
434 qemu_uname_release
= argv
[optind
++];
435 } else if (!strcmp(r
, "cpu")) {
436 cpu_model
= argv
[optind
++];
437 if (strcmp(cpu_model
, "?") == 0) {
438 /* XXX: implement xxx_cpu_list for targets that still miss it */
439 #if defined(cpu_list)
440 cpu_list(stdout
, &fprintf
);
444 } else if (!strcmp(r
, "drop-ld-preload")) {
446 } else if (!strcmp(r
, "bsd")) {
447 if (!strcasecmp(argv
[optind
], "freebsd")) {
448 bsd_type
= target_freebsd
;
449 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
450 bsd_type
= target_netbsd
;
451 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
452 bsd_type
= target_openbsd
;
457 } else if (!strcmp(r
, "strace")) {
466 filename
= argv
[optind
];
469 memset(regs
, 0, sizeof(struct target_pt_regs
));
471 /* Zero out image_info */
472 memset(info
, 0, sizeof(struct image_info
));
474 /* Scan interp_prefix dir for replacement files. */
475 init_paths(interp_prefix
);
477 if (cpu_model
== NULL
) {
478 #if defined(TARGET_SPARC)
479 #ifdef TARGET_SPARC64
480 cpu_model
= "TI UltraSparc II";
482 cpu_model
= "Fujitsu MB86904";
488 cpu_exec_init_all(0);
489 /* NOTE: we need to init the CPU at this stage to get
490 qemu_host_page_size */
491 env
= cpu_init(cpu_model
);
493 fprintf(stderr
, "Unable to find CPU definition\n");
498 if (getenv("QEMU_STRACE")) {
506 target_environ
= malloc((environ_count
+ 1) * sizeof(char *));
509 for (wrk
= environ
, dst
= target_environ
; *wrk
; wrk
++) {
510 if (drop_ld_preload
&& !strncmp(*wrk
, "LD_PRELOAD=", 11))
512 *(dst
++) = strdup(*wrk
);
514 *dst
= NULL
; /* NULL terminate target_environ */
516 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
517 printf("Error loading %s\n", filename
);
521 for (wrk
= target_environ
; *wrk
; wrk
++) {
525 free(target_environ
);
530 fprintf(logfile
, "start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
531 fprintf(logfile
, "end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
532 fprintf(logfile
, "start_code 0x" TARGET_ABI_FMT_lx
"\n",
534 fprintf(logfile
, "start_data 0x" TARGET_ABI_FMT_lx
"\n",
536 fprintf(logfile
, "end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
537 fprintf(logfile
, "start_stack 0x" TARGET_ABI_FMT_lx
"\n",
539 fprintf(logfile
, "brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
540 fprintf(logfile
, "entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
543 target_set_brk(info
->brk
);
547 /* build Task State */
548 memset(ts
, 0, sizeof(TaskState
));
552 env
->user_mode_only
= 1;
554 #if defined(TARGET_SPARC)
558 env
->npc
= regs
->npc
;
560 for(i
= 0; i
< 8; i
++)
561 env
->gregs
[i
] = regs
->u_regs
[i
];
562 for(i
= 0; i
< 8; i
++)
563 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
566 #error unsupported target CPU
570 gdbserver_start (gdbstub_port
);
571 gdb_handlesig(env
, 0);
573 cpu_loop(env
, bsd_type
);