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 (bsd_type
== target_freebsd
)
190 ret
= do_freebsd_syscall(env
, syscall_nr
,
191 env
->regwptr
[0], env
->regwptr
[1],
192 env
->regwptr
[2], env
->regwptr
[3],
193 env
->regwptr
[4], env
->regwptr
[5]);
194 else if (bsd_type
== target_netbsd
)
195 ret
= do_netbsd_syscall(env
, syscall_nr
,
196 env
->regwptr
[0], env
->regwptr
[1],
197 env
->regwptr
[2], env
->regwptr
[3],
198 env
->regwptr
[4], env
->regwptr
[5]);
199 else { //if (bsd_type == target_openbsd)
200 #if defined(TARGET_SPARC64)
201 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
202 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
204 ret
= do_openbsd_syscall(env
, syscall_nr
,
205 env
->regwptr
[0], env
->regwptr
[1],
206 env
->regwptr
[2], env
->regwptr
[3],
207 env
->regwptr
[4], env
->regwptr
[5]);
209 if ((unsigned int)ret
>= (unsigned int)(-515)) {
210 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
211 env
->xcc
|= PSR_CARRY
;
213 env
->psr
|= PSR_CARRY
;
216 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
217 env
->xcc
&= ~PSR_CARRY
;
219 env
->psr
&= ~PSR_CARRY
;
222 env
->regwptr
[0] = ret
;
223 /* next instruction */
224 #if defined(TARGET_SPARC64)
225 if (bsd_type
== target_openbsd
&&
226 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
227 env
->pc
= env
->gregs
[2];
228 env
->npc
= env
->pc
+ 4;
229 } else if (bsd_type
== target_openbsd
&&
230 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
231 env
->pc
= env
->gregs
[7];
232 env
->npc
= env
->pc
+ 4;
235 env
->npc
= env
->npc
+ 4;
239 env
->npc
= env
->npc
+ 4;
242 case 0x83: /* flush windows */
247 /* next instruction */
249 env
->npc
= env
->npc
+ 4;
251 #ifndef TARGET_SPARC64
252 case TT_WIN_OVF
: /* window overflow */
255 case TT_WIN_UNF
: /* window underflow */
262 info
.si_signo
= SIGSEGV
;
264 /* XXX: check env->error_code */
265 info
.si_code
= TARGET_SEGV_MAPERR
;
266 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
267 queue_signal(env
, info
.si_signo
, &info
);
272 case TT_SPILL
: /* window overflow */
275 case TT_FILL
: /* window underflow */
282 info
.si_signo
= SIGSEGV
;
284 /* XXX: check env->error_code */
285 info
.si_code
= TARGET_SEGV_MAPERR
;
286 if (trapnr
== TT_DFAULT
)
287 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
289 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
290 //queue_signal(env, info.si_signo, &info);
296 /* just indicate that signals should be handled asap */
302 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
308 info
.si_code
= TARGET_TRAP_BRKPT
;
309 //queue_signal(env, info.si_signo, &info);
315 printf ("Unhandled trap: 0x%x\n", trapnr
);
316 cpu_dump_state(env
, stderr
, fprintf
, 0);
319 process_pending_signals (env
);
325 static void usage(void)
327 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
328 "usage: qemu-" TARGET_ARCH
" [options] program [arguments...]\n"
329 "BSD CPU emulator (compiled for %s emulation)\n"
331 "Standard options:\n"
332 "-h print this help\n"
333 "-g port wait gdb connection to port\n"
334 "-L path set the elf interpreter prefix (default=%s)\n"
335 "-s size set the stack size in bytes (default=%ld)\n"
336 "-cpu model select CPU (-cpu ? for list)\n"
337 "-drop-ld-preload drop LD_PRELOAD for target process\n"
338 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
341 "-d options activate log (logfile=%s)\n"
342 "-p pagesize set the host page size to 'pagesize'\n"
343 "-strace log system calls\n"
345 "Environment variables:\n"
346 "QEMU_STRACE Print system calls and arguments similar to the\n"
347 " 'strace' program. Enable by setting to any value.\n"
356 THREAD CPUState
*thread_env
;
358 /* Assumes contents are already zeroed. */
359 void init_task_state(TaskState
*ts
)
364 ts
->first_free
= ts
->sigqueue_table
;
365 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
366 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
368 ts
->sigqueue_table
[i
].next
= NULL
;
371 int main(int argc
, char **argv
)
373 const char *filename
;
374 const char *cpu_model
;
375 struct target_pt_regs regs1
, *regs
= ®s1
;
376 struct image_info info1
, *info
= &info1
;
377 TaskState ts1
, *ts
= &ts1
;
381 int gdbstub_port
= 0;
382 int drop_ld_preload
= 0, environ_count
= 0;
383 char **target_environ
, **wrk
, **dst
;
384 enum BSDType bsd_type
= target_openbsd
;
390 cpu_set_log_filename(DEBUG_LOGFILE
);
402 if (!strcmp(r
, "-")) {
404 } else if (!strcmp(r
, "d")) {
406 const CPULogItem
*item
;
412 mask
= cpu_str_to_log_mask(r
);
414 printf("Log items (comma separated):\n");
415 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
416 printf("%-10s %s\n", item
->name
, item
->help
);
421 } else if (!strcmp(r
, "s")) {
423 x86_stack_size
= strtol(r
, (char **)&r
, 0);
424 if (x86_stack_size
<= 0)
427 x86_stack_size
*= 1024 * 1024;
428 else if (*r
== 'k' || *r
== 'K')
429 x86_stack_size
*= 1024;
430 } else if (!strcmp(r
, "L")) {
431 interp_prefix
= argv
[optind
++];
432 } else if (!strcmp(r
, "p")) {
433 qemu_host_page_size
= atoi(argv
[optind
++]);
434 if (qemu_host_page_size
== 0 ||
435 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
436 fprintf(stderr
, "page size must be a power of two\n");
439 } else if (!strcmp(r
, "g")) {
440 gdbstub_port
= atoi(argv
[optind
++]);
441 } else if (!strcmp(r
, "r")) {
442 qemu_uname_release
= argv
[optind
++];
443 } else if (!strcmp(r
, "cpu")) {
444 cpu_model
= argv
[optind
++];
445 if (strcmp(cpu_model
, "?") == 0) {
446 /* XXX: implement xxx_cpu_list for targets that still miss it */
447 #if defined(cpu_list)
448 cpu_list(stdout
, &fprintf
);
452 } else if (!strcmp(r
, "drop-ld-preload")) {
454 } else if (!strcmp(r
, "bsd")) {
455 if (!strcasecmp(argv
[optind
], "freebsd")) {
456 bsd_type
= target_freebsd
;
457 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
458 bsd_type
= target_netbsd
;
459 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
460 bsd_type
= target_openbsd
;
465 } else if (!strcmp(r
, "strace")) {
474 filename
= argv
[optind
];
477 memset(regs
, 0, sizeof(struct target_pt_regs
));
479 /* Zero out image_info */
480 memset(info
, 0, sizeof(struct image_info
));
482 /* Scan interp_prefix dir for replacement files. */
483 init_paths(interp_prefix
);
485 if (cpu_model
== NULL
) {
486 #if defined(TARGET_SPARC)
487 #ifdef TARGET_SPARC64
488 cpu_model
= "TI UltraSparc II";
490 cpu_model
= "Fujitsu MB86904";
496 cpu_exec_init_all(0);
497 /* NOTE: we need to init the CPU at this stage to get
498 qemu_host_page_size */
499 env
= cpu_init(cpu_model
);
501 fprintf(stderr
, "Unable to find CPU definition\n");
506 if (getenv("QEMU_STRACE")) {
514 target_environ
= malloc((environ_count
+ 1) * sizeof(char *));
517 for (wrk
= environ
, dst
= target_environ
; *wrk
; wrk
++) {
518 if (drop_ld_preload
&& !strncmp(*wrk
, "LD_PRELOAD=", 11))
520 *(dst
++) = strdup(*wrk
);
522 *dst
= NULL
; /* NULL terminate target_environ */
524 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
525 printf("Error loading %s\n", filename
);
529 for (wrk
= target_environ
; *wrk
; wrk
++) {
533 free(target_environ
);
538 fprintf(logfile
, "start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
539 fprintf(logfile
, "end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
540 fprintf(logfile
, "start_code 0x" TARGET_ABI_FMT_lx
"\n",
542 fprintf(logfile
, "start_data 0x" TARGET_ABI_FMT_lx
"\n",
544 fprintf(logfile
, "end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
545 fprintf(logfile
, "start_stack 0x" TARGET_ABI_FMT_lx
"\n",
547 fprintf(logfile
, "brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
548 fprintf(logfile
, "entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
551 target_set_brk(info
->brk
);
555 /* build Task State */
556 memset(ts
, 0, sizeof(TaskState
));
560 env
->user_mode_only
= 1;
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
);