Fix interrupt exclusion via SSTEP_NOIRQ
[qemu/mini2440.git] / bsd-user / main.c
blobd2129cc0c1e41ab5cfd16b9abe82b842a4421f5a
1 /*
2 * qemu user main
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.
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <machine/trap.h>
28 #include "qemu.h"
29 #include "qemu-common.h"
30 /* For tb_lock */
31 #include "exec-all.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, ...)
46 va_list ap;
48 va_start(ap, fmt);
49 vfprintf(stderr, fmt, ap);
50 va_end(ap);
52 #ifdef TARGET_SPARC
53 #define SPARC64_STACK_BIAS 2047
55 //#define DEBUG_WIN
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;
65 return index;
68 /* save the register window 'cwp1' */
69 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
71 unsigned int i;
72 abi_ulong sp_ptr;
74 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
75 #ifdef TARGET_SPARC64
76 if (sp_ptr & 3)
77 sp_ptr += SPARC64_STACK_BIAS;
78 #endif
79 #if defined(DEBUG_WIN)
80 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
81 sp_ptr, cwp1);
82 #endif
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
93 unsigned int new_wim;
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));
97 env->wim = new_wim;
98 #else
99 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
100 env->cansave++;
101 env->canrestore--;
102 #endif
105 static void restore_window(CPUSPARCState *env)
107 #ifndef TARGET_SPARC64
108 unsigned int new_wim;
109 #endif
110 unsigned int i, cwp1;
111 abi_ulong sp_ptr;
113 #ifndef TARGET_SPARC64
114 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
115 ((1LL << env->nwindows) - 1);
116 #endif
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
122 if (sp_ptr & 3)
123 sp_ptr += SPARC64_STACK_BIAS;
124 #endif
125 #if defined(DEBUG_WIN)
126 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
127 sp_ptr, cwp1);
128 #endif
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
135 env->canrestore++;
136 if (env->cleanwin < env->nwindows - 1)
137 env->cleanwin++;
138 env->cansave--;
139 #else
140 env->wim = new_wim;
141 #endif
144 static void flush_windows(CPUSPARCState *env)
146 int offset, cwp1;
148 offset = 1;
149 for(;;) {
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))
154 break;
155 #else
156 if (env->canrestore == 0)
157 break;
158 env->cansave++;
159 env->canrestore--;
160 #endif
161 save_window_offset(env, cwp1);
162 offset++;
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;
168 #endif
169 #if defined(DEBUG_WIN)
170 printf("flush_windows: nb=%d\n", offset - 1);
171 #endif
174 void cpu_loop(CPUSPARCState *env, enum BSDType bsd_type)
176 int trapnr, ret, syscall_nr;
177 //target_siginfo_t info;
179 while (1) {
180 trapnr = cpu_sparc_exec (env);
182 switch (trapnr) {
183 case 0x100:
184 syscall_nr = env->gregs[1];
185 #if defined(TARGET_SPARC)
186 syscall_nr &= ~(SYSCALL_G7RFLAG | SYSCALL_G2RFLAG);
187 #endif
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;
206 #else
207 env->psr |= PSR_CARRY;
208 #endif
209 } else {
210 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
211 env->xcc &= ~PSR_CARRY;
212 #else
213 env->psr &= ~PSR_CARRY;
214 #endif
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;
225 } else {
226 env->pc = env->npc;
227 env->npc = env->npc + 4;
229 #else
230 env->pc = env->npc;
231 env->npc = env->npc + 4;
232 #endif
233 break;
234 case 0x83: /* flush windows */
235 #ifdef TARGET_ABI32
236 case 0x103:
237 #endif
238 flush_windows(env);
239 /* next instruction */
240 env->pc = env->npc;
241 env->npc = env->npc + 4;
242 break;
243 #ifndef TARGET_SPARC64
244 case TT_WIN_OVF: /* window overflow */
245 save_window(env);
246 break;
247 case TT_WIN_UNF: /* window underflow */
248 restore_window(env);
249 break;
250 case TT_TFAULT:
251 case TT_DFAULT:
252 #if 0
254 info.si_signo = SIGSEGV;
255 info.si_errno = 0;
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);
261 #endif
262 break;
263 #else
264 case TT_SPILL: /* window overflow */
265 save_window(env);
266 break;
267 case TT_FILL: /* window underflow */
268 restore_window(env);
269 break;
270 case TT_TFAULT:
271 case TT_DFAULT:
272 #if 0
274 info.si_signo = SIGSEGV;
275 info.si_errno = 0;
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];
280 else
281 info._sifields._sigfault._addr = env->tsptr->tpc;
282 //queue_signal(env, info.si_signo, &info);
284 #endif
285 break;
286 #endif
287 case EXCP_INTERRUPT:
288 /* just indicate that signals should be handled asap */
289 break;
290 case EXCP_DEBUG:
292 int sig;
294 sig = gdb_handlesig (env, TARGET_SIGTRAP);
295 #if 0
296 if (sig)
298 info.si_signo = sig;
299 info.si_errno = 0;
300 info.si_code = TARGET_TRAP_BRKPT;
301 //queue_signal(env, info.si_signo, &info);
303 #endif
305 break;
306 default:
307 printf ("Unhandled trap: 0x%x\n", trapnr);
308 cpu_dump_state(env, stderr, fprintf, 0);
309 exit (1);
311 process_pending_signals (env);
315 #endif
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"
322 "\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"
331 "\n"
332 "Debug options:\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"
336 "\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"
341 TARGET_ARCH,
342 interp_prefix,
343 x86_stack_size,
344 DEBUG_LOGFILE);
345 _exit(1);
348 THREAD CPUState *thread_env;
350 /* Assumes contents are already zeroed. */
351 void init_task_state(TaskState *ts)
353 int i;
355 ts->used = 1;
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 = &regs1;
368 struct image_info info1, *info = &info1;
369 TaskState ts1, *ts = &ts1;
370 CPUState *env;
371 int optind;
372 const char *r;
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;
378 if (argc <= 1)
379 usage();
381 /* init debug */
382 cpu_set_log_filename(DEBUG_LOGFILE);
384 cpu_model = NULL;
385 optind = 1;
386 for(;;) {
387 if (optind >= argc)
388 break;
389 r = argv[optind];
390 if (r[0] != '-')
391 break;
392 optind++;
393 r++;
394 if (!strcmp(r, "-")) {
395 break;
396 } else if (!strcmp(r, "d")) {
397 int mask;
398 const CPULogItem *item;
400 if (optind >= argc)
401 break;
403 r = argv[optind++];
404 mask = cpu_str_to_log_mask(r);
405 if (!mask) {
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);
410 exit(1);
412 cpu_set_log(mask);
413 } else if (!strcmp(r, "s")) {
414 r = argv[optind++];
415 x86_stack_size = strtol(r, (char **)&r, 0);
416 if (x86_stack_size <= 0)
417 usage();
418 if (*r == 'M')
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");
429 exit(1);
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);
441 #endif
442 _exit(1);
444 } else if (!strcmp(r, "drop-ld-preload")) {
445 drop_ld_preload = 1;
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;
453 } else {
454 usage();
456 optind++;
457 } else if (!strcmp(r, "strace")) {
458 do_strace = 1;
459 } else
461 usage();
464 if (optind >= argc)
465 usage();
466 filename = argv[optind];
468 /* Zero out regs */
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";
481 #else
482 cpu_model = "Fujitsu MB86904";
483 #endif
484 #else
485 cpu_model = "any";
486 #endif
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);
492 if (!env) {
493 fprintf(stderr, "Unable to find CPU definition\n");
494 exit(1);
496 thread_env = env;
498 if (getenv("QEMU_STRACE")) {
499 do_strace = 1;
502 wrk = environ;
503 while (*(wrk++))
504 environ_count++;
506 target_environ = malloc((environ_count + 1) * sizeof(char *));
507 if (!target_environ)
508 abort();
509 for (wrk = environ, dst = target_environ; *wrk; wrk++) {
510 if (drop_ld_preload && !strncmp(*wrk, "LD_PRELOAD=", 11))
511 continue;
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);
518 _exit(1);
521 for (wrk = target_environ; *wrk; wrk++) {
522 free(*wrk);
525 free(target_environ);
527 if (loglevel) {
528 page_dump(logfile);
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",
533 info->start_code);
534 fprintf(logfile, "start_data 0x" TARGET_ABI_FMT_lx "\n",
535 info->start_data);
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",
538 info->start_stack);
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);
544 syscall_init();
545 signal_init();
547 /* build Task State */
548 memset(ts, 0, sizeof(TaskState));
549 init_task_state(ts);
550 ts->info = info;
551 env->opaque = ts;
552 env->user_mode_only = 1;
554 #if defined(TARGET_SPARC)
556 int i;
557 env->pc = regs->pc;
558 env->npc = regs->npc;
559 env->y = regs->y;
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];
565 #else
566 #error unsupported target CPU
567 #endif
569 if (gdbstub_port) {
570 gdbserver_start (gdbstub_port);
571 gdb_handlesig(env, 0);
573 cpu_loop(env, bsd_type);
574 /* never exits */
575 return 0;