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
);
183 #ifndef TARGET_SPARC64
188 syscall_nr
= env
->gregs
[1];
189 #if defined(TARGET_SPARC)
190 syscall_nr
&= ~(SYSCALL_G7RFLAG
| SYSCALL_G2RFLAG
);
192 if (bsd_type
== target_freebsd
)
193 ret
= do_freebsd_syscall(env
, syscall_nr
,
194 env
->regwptr
[0], env
->regwptr
[1],
195 env
->regwptr
[2], env
->regwptr
[3],
196 env
->regwptr
[4], env
->regwptr
[5]);
197 else if (bsd_type
== target_netbsd
)
198 ret
= do_netbsd_syscall(env
, syscall_nr
,
199 env
->regwptr
[0], env
->regwptr
[1],
200 env
->regwptr
[2], env
->regwptr
[3],
201 env
->regwptr
[4], env
->regwptr
[5]);
202 else //if (bsd_type == target_openbsd)
203 ret
= do_openbsd_syscall(env
, syscall_nr
,
204 env
->regwptr
[0], env
->regwptr
[1],
205 env
->regwptr
[2], env
->regwptr
[3],
206 env
->regwptr
[4], env
->regwptr
[5]);
207 if ((unsigned int)ret
>= (unsigned int)(-515)) {
208 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
209 env
->xcc
|= PSR_CARRY
;
211 env
->psr
|= PSR_CARRY
;
214 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
215 env
->xcc
&= ~PSR_CARRY
;
217 env
->psr
&= ~PSR_CARRY
;
220 env
->regwptr
[0] = ret
;
221 /* next instruction */
222 #if defined(TARGET_SPARC)
223 if (env
->gregs
[1] & SYSCALL_G2RFLAG
) {
224 env
->pc
= env
->gregs
[2];
225 env
->npc
= env
->pc
+ 4;
226 } else if (env
->gregs
[1] & SYSCALL_G7RFLAG
) {
227 env
->pc
= env
->gregs
[7];
228 env
->npc
= env
->pc
+ 4;
231 env
->npc
= env
->npc
+ 4;
235 env
->npc
= env
->npc
+ 4;
238 case 0x83: /* flush windows */
243 /* next instruction */
245 env
->npc
= env
->npc
+ 4;
247 #ifndef TARGET_SPARC64
248 case TT_WIN_OVF
: /* window overflow */
251 case TT_WIN_UNF
: /* window underflow */
258 info
.si_signo
= SIGSEGV
;
260 /* XXX: check env->error_code */
261 info
.si_code
= TARGET_SEGV_MAPERR
;
262 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
263 queue_signal(env
, info
.si_signo
, &info
);
268 case TT_SPILL
: /* window overflow */
271 case TT_FILL
: /* window underflow */
278 info
.si_signo
= SIGSEGV
;
280 /* XXX: check env->error_code */
281 info
.si_code
= TARGET_SEGV_MAPERR
;
282 if (trapnr
== TT_DFAULT
)
283 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
285 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
286 //queue_signal(env, info.si_signo, &info);
292 /* just indicate that signals should be handled asap */
298 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
304 info
.si_code
= TARGET_TRAP_BRKPT
;
305 //queue_signal(env, info.si_signo, &info);
311 printf ("Unhandled trap: 0x%x\n", trapnr
);
312 cpu_dump_state(env
, stderr
, fprintf
, 0);
315 process_pending_signals (env
);
321 static void usage(void)
323 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
324 "usage: qemu-" TARGET_ARCH
" [options] program [arguments...]\n"
325 "BSD CPU emulator (compiled for %s emulation)\n"
327 "Standard options:\n"
328 "-h print this help\n"
329 "-g port wait gdb connection to port\n"
330 "-L path set the elf interpreter prefix (default=%s)\n"
331 "-s size set the stack size in bytes (default=%ld)\n"
332 "-cpu model select CPU (-cpu ? for list)\n"
333 "-drop-ld-preload drop LD_PRELOAD for target process\n"
334 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
337 "-d options activate log (logfile=%s)\n"
338 "-p pagesize set the host page size to 'pagesize'\n"
339 "-strace log system calls\n"
341 "Environment variables:\n"
342 "QEMU_STRACE Print system calls and arguments similar to the\n"
343 " 'strace' program. Enable by setting to any value.\n"
352 THREAD CPUState
*thread_env
;
354 /* Assumes contents are already zeroed. */
355 void init_task_state(TaskState
*ts
)
360 ts
->first_free
= ts
->sigqueue_table
;
361 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
362 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
364 ts
->sigqueue_table
[i
].next
= NULL
;
367 int main(int argc
, char **argv
)
369 const char *filename
;
370 const char *cpu_model
;
371 struct target_pt_regs regs1
, *regs
= ®s1
;
372 struct image_info info1
, *info
= &info1
;
373 TaskState ts1
, *ts
= &ts1
;
377 int gdbstub_port
= 0;
378 int drop_ld_preload
= 0, environ_count
= 0;
379 char **target_environ
, **wrk
, **dst
;
380 enum BSDType bsd_type
= target_openbsd
;
386 cpu_set_log_filename(DEBUG_LOGFILE
);
398 if (!strcmp(r
, "-")) {
400 } else if (!strcmp(r
, "d")) {
402 const CPULogItem
*item
;
408 mask
= cpu_str_to_log_mask(r
);
410 printf("Log items (comma separated):\n");
411 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
412 printf("%-10s %s\n", item
->name
, item
->help
);
417 } else if (!strcmp(r
, "s")) {
419 x86_stack_size
= strtol(r
, (char **)&r
, 0);
420 if (x86_stack_size
<= 0)
423 x86_stack_size
*= 1024 * 1024;
424 else if (*r
== 'k' || *r
== 'K')
425 x86_stack_size
*= 1024;
426 } else if (!strcmp(r
, "L")) {
427 interp_prefix
= argv
[optind
++];
428 } else if (!strcmp(r
, "p")) {
429 qemu_host_page_size
= atoi(argv
[optind
++]);
430 if (qemu_host_page_size
== 0 ||
431 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
432 fprintf(stderr
, "page size must be a power of two\n");
435 } else if (!strcmp(r
, "g")) {
436 gdbstub_port
= atoi(argv
[optind
++]);
437 } else if (!strcmp(r
, "r")) {
438 qemu_uname_release
= argv
[optind
++];
439 } else if (!strcmp(r
, "cpu")) {
440 cpu_model
= argv
[optind
++];
441 if (strcmp(cpu_model
, "?") == 0) {
442 /* XXX: implement xxx_cpu_list for targets that still miss it */
443 #if defined(cpu_list)
444 cpu_list(stdout
, &fprintf
);
448 } else if (!strcmp(r
, "drop-ld-preload")) {
450 } else if (!strcmp(r
, "bsd")) {
451 if (!strcasecmp(argv
[optind
], "freebsd")) {
452 bsd_type
= target_freebsd
;
453 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
454 bsd_type
= target_netbsd
;
455 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
456 bsd_type
= target_openbsd
;
461 } else if (!strcmp(r
, "strace")) {
470 filename
= argv
[optind
];
473 memset(regs
, 0, sizeof(struct target_pt_regs
));
475 /* Zero out image_info */
476 memset(info
, 0, sizeof(struct image_info
));
478 /* Scan interp_prefix dir for replacement files. */
479 init_paths(interp_prefix
);
481 if (cpu_model
== NULL
) {
482 #if defined(TARGET_SPARC)
483 #ifdef TARGET_SPARC64
484 cpu_model
= "TI UltraSparc II";
486 cpu_model
= "Fujitsu MB86904";
492 cpu_exec_init_all(0);
493 /* NOTE: we need to init the CPU at this stage to get
494 qemu_host_page_size */
495 env
= cpu_init(cpu_model
);
497 fprintf(stderr
, "Unable to find CPU definition\n");
502 if (getenv("QEMU_STRACE")) {
510 target_environ
= malloc((environ_count
+ 1) * sizeof(char *));
513 for (wrk
= environ
, dst
= target_environ
; *wrk
; wrk
++) {
514 if (drop_ld_preload
&& !strncmp(*wrk
, "LD_PRELOAD=", 11))
516 *(dst
++) = strdup(*wrk
);
518 *dst
= NULL
; /* NULL terminate target_environ */
520 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
521 printf("Error loading %s\n", filename
);
525 for (wrk
= target_environ
; *wrk
; wrk
++) {
529 free(target_environ
);
534 fprintf(logfile
, "start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
535 fprintf(logfile
, "end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
536 fprintf(logfile
, "start_code 0x" TARGET_ABI_FMT_lx
"\n",
538 fprintf(logfile
, "start_data 0x" TARGET_ABI_FMT_lx
"\n",
540 fprintf(logfile
, "end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
541 fprintf(logfile
, "start_stack 0x" TARGET_ABI_FMT_lx
"\n",
543 fprintf(logfile
, "brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
544 fprintf(logfile
, "entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
547 target_set_brk(info
->brk
);
551 /* build Task State */
552 memset(ts
, 0, sizeof(TaskState
));
556 env
->user_mode_only
= 1;
558 #if defined(TARGET_SPARC)
562 env
->npc
= regs
->npc
;
564 for(i
= 0; i
< 8; i
++)
565 env
->gregs
[i
] = regs
->u_regs
[i
];
566 for(i
= 0; i
< 8; i
++)
567 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
570 #error unsupported target CPU
574 gdbserver_start (gdbstub_port
);
575 gdb_handlesig(env
, 0);
577 cpu_loop(env
, bsd_type
);