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., 51 Franklin Street - Fifth Floor, Boston,
27 #include <machine/trap.h>
30 #include "qemu-common.h"
34 #define DEBUG_LOGFILE "/tmp/qemu.log"
38 static const char *interp_prefix
= CONFIG_QEMU_PREFIX
;
39 const char *qemu_uname_release
= CONFIG_UNAME_RELEASE
;
40 extern char **environ
;
42 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
43 we allocate a bigger stack. Need a better solution, for example
44 by remapping the process stack directly at the right place */
45 unsigned long x86_stack_size
= 512 * 1024;
47 void gemu_log(const char *fmt
, ...)
52 vfprintf(stderr
, fmt
, ap
);
56 /* These are no-ops because we are not threadsafe. */
57 static inline void cpu_exec_start(CPUState
*env
)
61 static inline void cpu_exec_end(CPUState
*env
)
65 static inline void start_exclusive(void)
69 static inline void end_exclusive(void)
77 void fork_end(int child
)
80 gdbserver_fork(thread_env
);
84 void cpu_list_lock(void)
88 void cpu_list_unlock(void)
93 #define SPARC64_STACK_BIAS 2047
96 /* WARNING: dealing with register windows _is_ complicated. More info
97 can be found at http://www.sics.se/~psm/sparcstack.html */
98 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
100 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
101 /* wrap handling : if cwp is on the last window, then we use the
102 registers 'after' the end */
103 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
104 index
+= 16 * env
->nwindows
;
108 /* save the register window 'cwp1' */
109 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
114 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
115 #ifdef TARGET_SPARC64
117 sp_ptr
+= SPARC64_STACK_BIAS
;
119 #if defined(DEBUG_WIN)
120 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
123 for(i
= 0; i
< 16; i
++) {
124 /* FIXME - what to do if put_user() fails? */
125 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
126 sp_ptr
+= sizeof(abi_ulong
);
130 static void save_window(CPUSPARCState
*env
)
132 #ifndef TARGET_SPARC64
133 unsigned int new_wim
;
134 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
135 ((1LL << env
->nwindows
) - 1);
136 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
139 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
145 static void restore_window(CPUSPARCState
*env
)
147 #ifndef TARGET_SPARC64
148 unsigned int new_wim
;
150 unsigned int i
, cwp1
;
153 #ifndef TARGET_SPARC64
154 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
155 ((1LL << env
->nwindows
) - 1);
158 /* restore the invalid window */
159 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
160 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
161 #ifdef TARGET_SPARC64
163 sp_ptr
+= SPARC64_STACK_BIAS
;
165 #if defined(DEBUG_WIN)
166 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
169 for(i
= 0; i
< 16; i
++) {
170 /* FIXME - what to do if get_user() fails? */
171 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
172 sp_ptr
+= sizeof(abi_ulong
);
174 #ifdef TARGET_SPARC64
176 if (env
->cleanwin
< env
->nwindows
- 1)
184 static void flush_windows(CPUSPARCState
*env
)
190 /* if restore would invoke restore_window(), then we can stop */
191 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
192 #ifndef TARGET_SPARC64
193 if (env
->wim
& (1 << cwp1
))
196 if (env
->canrestore
== 0)
201 save_window_offset(env
, cwp1
);
204 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
205 #ifndef TARGET_SPARC64
206 /* set wim so that restore will reload the registers */
207 env
->wim
= 1 << cwp1
;
209 #if defined(DEBUG_WIN)
210 printf("flush_windows: nb=%d\n", offset
- 1);
214 void cpu_loop(CPUSPARCState
*env
, enum BSDType bsd_type
)
216 int trapnr
, ret
, syscall_nr
;
217 //target_siginfo_t info;
220 trapnr
= cpu_sparc_exec (env
);
223 #ifndef TARGET_SPARC64
228 syscall_nr
= env
->gregs
[1];
229 if (bsd_type
== target_freebsd
)
230 ret
= do_freebsd_syscall(env
, syscall_nr
,
231 env
->regwptr
[0], env
->regwptr
[1],
232 env
->regwptr
[2], env
->regwptr
[3],
233 env
->regwptr
[4], env
->regwptr
[5]);
234 else if (bsd_type
== target_netbsd
)
235 ret
= do_netbsd_syscall(env
, syscall_nr
,
236 env
->regwptr
[0], env
->regwptr
[1],
237 env
->regwptr
[2], env
->regwptr
[3],
238 env
->regwptr
[4], env
->regwptr
[5]);
239 else { //if (bsd_type == target_openbsd)
240 #if defined(TARGET_SPARC64)
241 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
242 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
244 ret
= do_openbsd_syscall(env
, syscall_nr
,
245 env
->regwptr
[0], env
->regwptr
[1],
246 env
->regwptr
[2], env
->regwptr
[3],
247 env
->regwptr
[4], env
->regwptr
[5]);
249 if ((unsigned int)ret
>= (unsigned int)(-515)) {
250 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
251 env
->xcc
|= PSR_CARRY
;
253 env
->psr
|= PSR_CARRY
;
256 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
257 env
->xcc
&= ~PSR_CARRY
;
259 env
->psr
&= ~PSR_CARRY
;
262 env
->regwptr
[0] = ret
;
263 /* next instruction */
264 #if defined(TARGET_SPARC64)
265 if (bsd_type
== target_openbsd
&&
266 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
267 env
->pc
= env
->gregs
[2];
268 env
->npc
= env
->pc
+ 4;
269 } else if (bsd_type
== target_openbsd
&&
270 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
271 env
->pc
= env
->gregs
[7];
272 env
->npc
= env
->pc
+ 4;
275 env
->npc
= env
->npc
+ 4;
279 env
->npc
= env
->npc
+ 4;
282 case 0x83: /* flush windows */
287 /* next instruction */
289 env
->npc
= env
->npc
+ 4;
291 #ifndef TARGET_SPARC64
292 case TT_WIN_OVF
: /* window overflow */
295 case TT_WIN_UNF
: /* window underflow */
302 info
.si_signo
= SIGSEGV
;
304 /* XXX: check env->error_code */
305 info
.si_code
= TARGET_SEGV_MAPERR
;
306 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
307 queue_signal(env
, info
.si_signo
, &info
);
312 case TT_SPILL
: /* window overflow */
315 case TT_FILL
: /* window underflow */
322 info
.si_signo
= SIGSEGV
;
324 /* XXX: check env->error_code */
325 info
.si_code
= TARGET_SEGV_MAPERR
;
326 if (trapnr
== TT_DFAULT
)
327 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
329 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
330 //queue_signal(env, info.si_signo, &info);
336 /* just indicate that signals should be handled asap */
342 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
348 info
.si_code
= TARGET_TRAP_BRKPT
;
349 //queue_signal(env, info.si_signo, &info);
355 printf ("Unhandled trap: 0x%x\n", trapnr
);
356 cpu_dump_state(env
, stderr
, fprintf
, 0);
359 process_pending_signals (env
);
365 static void usage(void)
367 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
368 "usage: qemu-" TARGET_ARCH
" [options] program [arguments...]\n"
369 "BSD CPU emulator (compiled for %s emulation)\n"
371 "Standard options:\n"
372 "-h print this help\n"
373 "-g port wait gdb connection to port\n"
374 "-L path set the elf interpreter prefix (default=%s)\n"
375 "-s size set the stack size in bytes (default=%ld)\n"
376 "-cpu model select CPU (-cpu ? for list)\n"
377 "-drop-ld-preload drop LD_PRELOAD for target process\n"
378 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
381 "-d options activate log (logfile=%s)\n"
382 "-p pagesize set the host page size to 'pagesize'\n"
383 "-singlestep always run in singlestep mode\n"
384 "-strace log system calls\n"
386 "Environment variables:\n"
387 "QEMU_STRACE Print system calls and arguments similar to the\n"
388 " 'strace' program. Enable by setting to any value.\n"
397 THREAD CPUState
*thread_env
;
399 /* Assumes contents are already zeroed. */
400 void init_task_state(TaskState
*ts
)
405 ts
->first_free
= ts
->sigqueue_table
;
406 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
407 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
409 ts
->sigqueue_table
[i
].next
= NULL
;
412 int main(int argc
, char **argv
)
414 const char *filename
;
415 const char *cpu_model
;
416 struct target_pt_regs regs1
, *regs
= ®s1
;
417 struct image_info info1
, *info
= &info1
;
418 TaskState ts1
, *ts
= &ts1
;
422 int gdbstub_port
= 0;
423 int drop_ld_preload
= 0, environ_count
= 0;
424 char **target_environ
, **wrk
, **dst
;
425 enum BSDType bsd_type
= target_openbsd
;
431 cpu_set_log_filename(DEBUG_LOGFILE
);
443 if (!strcmp(r
, "-")) {
445 } else if (!strcmp(r
, "d")) {
447 const CPULogItem
*item
;
453 mask
= cpu_str_to_log_mask(r
);
455 printf("Log items (comma separated):\n");
456 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
457 printf("%-10s %s\n", item
->name
, item
->help
);
462 } else if (!strcmp(r
, "s")) {
464 x86_stack_size
= strtol(r
, (char **)&r
, 0);
465 if (x86_stack_size
<= 0)
468 x86_stack_size
*= 1024 * 1024;
469 else if (*r
== 'k' || *r
== 'K')
470 x86_stack_size
*= 1024;
471 } else if (!strcmp(r
, "L")) {
472 interp_prefix
= argv
[optind
++];
473 } else if (!strcmp(r
, "p")) {
474 qemu_host_page_size
= atoi(argv
[optind
++]);
475 if (qemu_host_page_size
== 0 ||
476 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
477 fprintf(stderr
, "page size must be a power of two\n");
480 } else if (!strcmp(r
, "g")) {
481 gdbstub_port
= atoi(argv
[optind
++]);
482 } else if (!strcmp(r
, "r")) {
483 qemu_uname_release
= argv
[optind
++];
484 } else if (!strcmp(r
, "cpu")) {
485 cpu_model
= argv
[optind
++];
486 if (strcmp(cpu_model
, "?") == 0) {
487 /* XXX: implement xxx_cpu_list for targets that still miss it */
488 #if defined(cpu_list)
489 cpu_list(stdout
, &fprintf
);
493 } else if (!strcmp(r
, "drop-ld-preload")) {
495 } else if (!strcmp(r
, "bsd")) {
496 if (!strcasecmp(argv
[optind
], "freebsd")) {
497 bsd_type
= target_freebsd
;
498 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
499 bsd_type
= target_netbsd
;
500 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
501 bsd_type
= target_openbsd
;
506 } else if (!strcmp(r
, "singlestep")) {
508 } else if (!strcmp(r
, "strace")) {
517 filename
= argv
[optind
];
520 memset(regs
, 0, sizeof(struct target_pt_regs
));
522 /* Zero out image_info */
523 memset(info
, 0, sizeof(struct image_info
));
525 /* Scan interp_prefix dir for replacement files. */
526 init_paths(interp_prefix
);
528 if (cpu_model
== NULL
) {
529 #if defined(TARGET_SPARC)
530 #ifdef TARGET_SPARC64
531 cpu_model
= "TI UltraSparc II";
533 cpu_model
= "Fujitsu MB86904";
539 cpu_exec_init_all(0);
540 /* NOTE: we need to init the CPU at this stage to get
541 qemu_host_page_size */
542 env
= cpu_init(cpu_model
);
544 fprintf(stderr
, "Unable to find CPU definition\n");
549 if (getenv("QEMU_STRACE")) {
557 target_environ
= malloc((environ_count
+ 1) * sizeof(char *));
560 for (wrk
= environ
, dst
= target_environ
; *wrk
; wrk
++) {
561 if (drop_ld_preload
&& !strncmp(*wrk
, "LD_PRELOAD=", 11))
563 *(dst
++) = strdup(*wrk
);
565 *dst
= NULL
; /* NULL terminate target_environ */
567 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
568 printf("Error loading %s\n", filename
);
572 for (wrk
= target_environ
; *wrk
; wrk
++) {
576 free(target_environ
);
578 if (qemu_log_enabled()) {
581 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
582 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
583 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
585 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
587 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
588 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
590 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
591 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
594 target_set_brk(info
->brk
);
598 /* build Task State */
599 memset(ts
, 0, sizeof(TaskState
));
604 #if defined(TARGET_SPARC)
608 env
->npc
= regs
->npc
;
610 for(i
= 0; i
< 8; i
++)
611 env
->gregs
[i
] = regs
->u_regs
[i
];
612 for(i
= 0; i
< 8; i
++)
613 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
616 #error unsupported target CPU
620 gdbserver_start (gdbstub_port
);
621 gdb_handlesig(env
, 0);
623 cpu_loop(env
, bsd_type
);