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"
36 static const char *interp_prefix
= CONFIG_QEMU_PREFIX
;
37 const char *qemu_uname_release
= CONFIG_UNAME_RELEASE
;
38 extern char **environ
;
40 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
41 we allocate a bigger stack. Need a better solution, for example
42 by remapping the process stack directly at the right place */
43 unsigned long x86_stack_size
= 512 * 1024;
45 void gemu_log(const char *fmt
, ...)
50 vfprintf(stderr
, fmt
, ap
);
54 #define SPARC64_STACK_BIAS 2047
57 /* WARNING: dealing with register windows _is_ complicated. More info
58 can be found at http://www.sics.se/~psm/sparcstack.html */
59 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
61 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
62 /* wrap handling : if cwp is on the last window, then we use the
63 registers 'after' the end */
64 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
65 index
+= 16 * env
->nwindows
;
69 /* save the register window 'cwp1' */
70 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
75 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
78 sp_ptr
+= SPARC64_STACK_BIAS
;
80 #if defined(DEBUG_WIN)
81 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
84 for(i
= 0; i
< 16; i
++) {
85 /* FIXME - what to do if put_user() fails? */
86 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
87 sp_ptr
+= sizeof(abi_ulong
);
91 static void save_window(CPUSPARCState
*env
)
93 #ifndef TARGET_SPARC64
95 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
96 ((1LL << env
->nwindows
) - 1);
97 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
100 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
106 static void restore_window(CPUSPARCState
*env
)
108 #ifndef TARGET_SPARC64
109 unsigned int new_wim
;
111 unsigned int i
, cwp1
;
114 #ifndef TARGET_SPARC64
115 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
116 ((1LL << env
->nwindows
) - 1);
119 /* restore the invalid window */
120 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
121 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
122 #ifdef TARGET_SPARC64
124 sp_ptr
+= SPARC64_STACK_BIAS
;
126 #if defined(DEBUG_WIN)
127 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
130 for(i
= 0; i
< 16; i
++) {
131 /* FIXME - what to do if get_user() fails? */
132 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
133 sp_ptr
+= sizeof(abi_ulong
);
135 #ifdef TARGET_SPARC64
137 if (env
->cleanwin
< env
->nwindows
- 1)
145 static void flush_windows(CPUSPARCState
*env
)
151 /* if restore would invoke restore_window(), then we can stop */
152 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
153 #ifndef TARGET_SPARC64
154 if (env
->wim
& (1 << cwp1
))
157 if (env
->canrestore
== 0)
162 save_window_offset(env
, cwp1
);
165 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
166 #ifndef TARGET_SPARC64
167 /* set wim so that restore will reload the registers */
168 env
->wim
= 1 << cwp1
;
170 #if defined(DEBUG_WIN)
171 printf("flush_windows: nb=%d\n", offset
- 1);
175 void cpu_loop(CPUSPARCState
*env
, enum BSDType bsd_type
)
177 int trapnr
, ret
, syscall_nr
;
178 //target_siginfo_t info;
181 trapnr
= cpu_sparc_exec (env
);
184 #ifndef TARGET_SPARC64
189 syscall_nr
= env
->gregs
[1];
190 if (bsd_type
== target_freebsd
)
191 ret
= do_freebsd_syscall(env
, syscall_nr
,
192 env
->regwptr
[0], env
->regwptr
[1],
193 env
->regwptr
[2], env
->regwptr
[3],
194 env
->regwptr
[4], env
->regwptr
[5]);
195 else if (bsd_type
== target_netbsd
)
196 ret
= do_netbsd_syscall(env
, syscall_nr
,
197 env
->regwptr
[0], env
->regwptr
[1],
198 env
->regwptr
[2], env
->regwptr
[3],
199 env
->regwptr
[4], env
->regwptr
[5]);
200 else { //if (bsd_type == target_openbsd)
201 #if defined(TARGET_SPARC64)
202 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
203 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
205 ret
= do_openbsd_syscall(env
, syscall_nr
,
206 env
->regwptr
[0], env
->regwptr
[1],
207 env
->regwptr
[2], env
->regwptr
[3],
208 env
->regwptr
[4], env
->regwptr
[5]);
210 if ((unsigned int)ret
>= (unsigned int)(-515)) {
211 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
212 env
->xcc
|= PSR_CARRY
;
214 env
->psr
|= PSR_CARRY
;
217 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
218 env
->xcc
&= ~PSR_CARRY
;
220 env
->psr
&= ~PSR_CARRY
;
223 env
->regwptr
[0] = ret
;
224 /* next instruction */
225 #if defined(TARGET_SPARC64)
226 if (bsd_type
== target_openbsd
&&
227 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
228 env
->pc
= env
->gregs
[2];
229 env
->npc
= env
->pc
+ 4;
230 } else if (bsd_type
== target_openbsd
&&
231 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
232 env
->pc
= env
->gregs
[7];
233 env
->npc
= env
->pc
+ 4;
236 env
->npc
= env
->npc
+ 4;
240 env
->npc
= env
->npc
+ 4;
243 case 0x83: /* flush windows */
248 /* next instruction */
250 env
->npc
= env
->npc
+ 4;
252 #ifndef TARGET_SPARC64
253 case TT_WIN_OVF
: /* window overflow */
256 case TT_WIN_UNF
: /* window underflow */
263 info
.si_signo
= SIGSEGV
;
265 /* XXX: check env->error_code */
266 info
.si_code
= TARGET_SEGV_MAPERR
;
267 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
268 queue_signal(env
, info
.si_signo
, &info
);
273 case TT_SPILL
: /* window overflow */
276 case TT_FILL
: /* window underflow */
283 info
.si_signo
= SIGSEGV
;
285 /* XXX: check env->error_code */
286 info
.si_code
= TARGET_SEGV_MAPERR
;
287 if (trapnr
== TT_DFAULT
)
288 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
290 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
291 //queue_signal(env, info.si_signo, &info);
297 /* just indicate that signals should be handled asap */
303 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
309 info
.si_code
= TARGET_TRAP_BRKPT
;
310 //queue_signal(env, info.si_signo, &info);
316 printf ("Unhandled trap: 0x%x\n", trapnr
);
317 cpu_dump_state(env
, stderr
, fprintf
, 0);
320 process_pending_signals (env
);
326 static void usage(void)
328 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
329 "usage: qemu-" TARGET_ARCH
" [options] program [arguments...]\n"
330 "BSD CPU emulator (compiled for %s emulation)\n"
332 "Standard options:\n"
333 "-h print this help\n"
334 "-g port wait gdb connection to port\n"
335 "-L path set the elf interpreter prefix (default=%s)\n"
336 "-s size set the stack size in bytes (default=%ld)\n"
337 "-cpu model select CPU (-cpu ? for list)\n"
338 "-drop-ld-preload drop LD_PRELOAD for target process\n"
339 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
342 "-d options activate log (logfile=%s)\n"
343 "-p pagesize set the host page size to 'pagesize'\n"
344 "-strace log system calls\n"
346 "Environment variables:\n"
347 "QEMU_STRACE Print system calls and arguments similar to the\n"
348 " 'strace' program. Enable by setting to any value.\n"
357 THREAD CPUState
*thread_env
;
359 /* Assumes contents are already zeroed. */
360 void init_task_state(TaskState
*ts
)
365 ts
->first_free
= ts
->sigqueue_table
;
366 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
367 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
369 ts
->sigqueue_table
[i
].next
= NULL
;
372 int main(int argc
, char **argv
)
374 const char *filename
;
375 const char *cpu_model
;
376 struct target_pt_regs regs1
, *regs
= ®s1
;
377 struct image_info info1
, *info
= &info1
;
378 TaskState ts1
, *ts
= &ts1
;
382 int gdbstub_port
= 0;
383 int drop_ld_preload
= 0, environ_count
= 0;
384 char **target_environ
, **wrk
, **dst
;
385 enum BSDType bsd_type
= target_openbsd
;
391 cpu_set_log_filename(DEBUG_LOGFILE
);
403 if (!strcmp(r
, "-")) {
405 } else if (!strcmp(r
, "d")) {
407 const CPULogItem
*item
;
413 mask
= cpu_str_to_log_mask(r
);
415 printf("Log items (comma separated):\n");
416 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
417 printf("%-10s %s\n", item
->name
, item
->help
);
422 } else if (!strcmp(r
, "s")) {
424 x86_stack_size
= strtol(r
, (char **)&r
, 0);
425 if (x86_stack_size
<= 0)
428 x86_stack_size
*= 1024 * 1024;
429 else if (*r
== 'k' || *r
== 'K')
430 x86_stack_size
*= 1024;
431 } else if (!strcmp(r
, "L")) {
432 interp_prefix
= argv
[optind
++];
433 } else if (!strcmp(r
, "p")) {
434 qemu_host_page_size
= atoi(argv
[optind
++]);
435 if (qemu_host_page_size
== 0 ||
436 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
437 fprintf(stderr
, "page size must be a power of two\n");
440 } else if (!strcmp(r
, "g")) {
441 gdbstub_port
= atoi(argv
[optind
++]);
442 } else if (!strcmp(r
, "r")) {
443 qemu_uname_release
= argv
[optind
++];
444 } else if (!strcmp(r
, "cpu")) {
445 cpu_model
= argv
[optind
++];
446 if (strcmp(cpu_model
, "?") == 0) {
447 /* XXX: implement xxx_cpu_list for targets that still miss it */
448 #if defined(cpu_list)
449 cpu_list(stdout
, &fprintf
);
453 } else if (!strcmp(r
, "drop-ld-preload")) {
455 } else if (!strcmp(r
, "bsd")) {
456 if (!strcasecmp(argv
[optind
], "freebsd")) {
457 bsd_type
= target_freebsd
;
458 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
459 bsd_type
= target_netbsd
;
460 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
461 bsd_type
= target_openbsd
;
466 } else if (!strcmp(r
, "strace")) {
475 filename
= argv
[optind
];
478 memset(regs
, 0, sizeof(struct target_pt_regs
));
480 /* Zero out image_info */
481 memset(info
, 0, sizeof(struct image_info
));
483 /* Scan interp_prefix dir for replacement files. */
484 init_paths(interp_prefix
);
486 if (cpu_model
== NULL
) {
487 #if defined(TARGET_SPARC)
488 #ifdef TARGET_SPARC64
489 cpu_model
= "TI UltraSparc II";
491 cpu_model
= "Fujitsu MB86904";
497 cpu_exec_init_all(0);
498 /* NOTE: we need to init the CPU at this stage to get
499 qemu_host_page_size */
500 env
= cpu_init(cpu_model
);
502 fprintf(stderr
, "Unable to find CPU definition\n");
507 if (getenv("QEMU_STRACE")) {
515 target_environ
= malloc((environ_count
+ 1) * sizeof(char *));
518 for (wrk
= environ
, dst
= target_environ
; *wrk
; wrk
++) {
519 if (drop_ld_preload
&& !strncmp(*wrk
, "LD_PRELOAD=", 11))
521 *(dst
++) = strdup(*wrk
);
523 *dst
= NULL
; /* NULL terminate target_environ */
525 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
526 printf("Error loading %s\n", filename
);
530 for (wrk
= target_environ
; *wrk
; wrk
++) {
534 free(target_environ
);
536 if (qemu_log_enabled()) {
539 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
540 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
541 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
543 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
545 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
546 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
548 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
549 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
552 target_set_brk(info
->brk
);
556 /* build Task State */
557 memset(ts
, 0, sizeof(TaskState
));
562 #if defined(TARGET_SPARC)
566 env
->npc
= regs
->npc
;
568 for(i
= 0; i
< 8; i
++)
569 env
->gregs
[i
] = regs
->u_regs
[i
];
570 for(i
= 0; i
< 8; i
++)
571 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
574 #error unsupported target CPU
578 gdbserver_start (gdbstub_port
);
579 gdb_handlesig(env
, 0);
581 cpu_loop(env
, bsd_type
);