]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/main.c
Compile target independent files only once
[mirror_qemu.git] / bsd-user / main.c
CommitLineData
84778508
BS
1/*
2 * qemu user main
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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.
10 *
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.
15 *
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
530e7615
BS
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 * MA 02110-1301, USA.
84778508
BS
20 */
21#include <stdlib.h>
22#include <stdio.h>
23#include <stdarg.h>
24#include <string.h>
25#include <errno.h>
26#include <unistd.h>
27#include <machine/trap.h>
28
29#include "qemu.h"
30#include "qemu-common.h"
31/* For tb_lock */
32#include "exec-all.h"
33
34#define DEBUG_LOGFILE "/tmp/qemu.log"
35
1b530a6d
AJ
36int singlestep;
37
84778508
BS
38static const char *interp_prefix = CONFIG_QEMU_PREFIX;
39const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
40extern char **environ;
41
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 */
45unsigned long x86_stack_size = 512 * 1024;
46
47void gemu_log(const char *fmt, ...)
48{
49 va_list ap;
50
51 va_start(ap, fmt);
52 vfprintf(stderr, fmt, ap);
53 va_end(ap);
54}
9399f095
BS
55
56/* These are no-ops because we are not threadsafe. */
57static inline void cpu_exec_start(CPUState *env)
58{
59}
60
61static inline void cpu_exec_end(CPUState *env)
62{
63}
64
65static inline void start_exclusive(void)
66{
67}
68
69static inline void end_exclusive(void)
70{
71}
72
73void fork_start(void)
74{
75}
76
77void fork_end(int child)
78{
79 if (child) {
80 gdbserver_fork(thread_env);
81 }
82}
83
84void cpu_list_lock(void)
85{
86}
87
88void cpu_list_unlock(void)
89{
90}
91
84778508
BS
92#ifdef TARGET_SPARC
93#define SPARC64_STACK_BIAS 2047
94
95//#define DEBUG_WIN
96/* WARNING: dealing with register windows _is_ complicated. More info
97 can be found at http://www.sics.se/~psm/sparcstack.html */
98static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
99{
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;
105 return index;
106}
107
108/* save the register window 'cwp1' */
109static inline void save_window_offset(CPUSPARCState *env, int cwp1)
110{
111 unsigned int i;
112 abi_ulong sp_ptr;
113
114 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
115#ifdef TARGET_SPARC64
116 if (sp_ptr & 3)
117 sp_ptr += SPARC64_STACK_BIAS;
118#endif
119#if defined(DEBUG_WIN)
120 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
121 sp_ptr, cwp1);
122#endif
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);
127 }
128}
129
130static void save_window(CPUSPARCState *env)
131{
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));
137 env->wim = new_wim;
138#else
139 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
140 env->cansave++;
141 env->canrestore--;
142#endif
143}
144
145static void restore_window(CPUSPARCState *env)
146{
147#ifndef TARGET_SPARC64
148 unsigned int new_wim;
149#endif
150 unsigned int i, cwp1;
151 abi_ulong sp_ptr;
152
153#ifndef TARGET_SPARC64
154 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
155 ((1LL << env->nwindows) - 1);
156#endif
157
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
162 if (sp_ptr & 3)
163 sp_ptr += SPARC64_STACK_BIAS;
164#endif
165#if defined(DEBUG_WIN)
166 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
167 sp_ptr, cwp1);
168#endif
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);
173 }
174#ifdef TARGET_SPARC64
175 env->canrestore++;
176 if (env->cleanwin < env->nwindows - 1)
177 env->cleanwin++;
178 env->cansave--;
179#else
180 env->wim = new_wim;
181#endif
182}
183
184static void flush_windows(CPUSPARCState *env)
185{
186 int offset, cwp1;
187
188 offset = 1;
189 for(;;) {
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))
194 break;
195#else
196 if (env->canrestore == 0)
197 break;
198 env->cansave++;
199 env->canrestore--;
200#endif
201 save_window_offset(env, cwp1);
202 offset++;
203 }
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;
208#endif
209#if defined(DEBUG_WIN)
210 printf("flush_windows: nb=%d\n", offset - 1);
211#endif
212}
213
214void cpu_loop(CPUSPARCState *env, enum BSDType bsd_type)
215{
216 int trapnr, ret, syscall_nr;
217 //target_siginfo_t info;
218
219 while (1) {
220 trapnr = cpu_sparc_exec (env);
221
222 switch (trapnr) {
77b9435f
BS
223#ifndef TARGET_SPARC64
224 case 0x80:
225#else
84778508 226 case 0x100:
77b9435f 227#endif
84778508 228 syscall_nr = env->gregs[1];
84778508
BS
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]);
cdba95bd
BS
239 else { //if (bsd_type == target_openbsd)
240#if defined(TARGET_SPARC64)
241 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
242 TARGET_OPENBSD_SYSCALL_G2RFLAG);
243#endif
84778508
BS
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]);
cdba95bd 248 }
84778508
BS
249 if ((unsigned int)ret >= (unsigned int)(-515)) {
250#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
251 env->xcc |= PSR_CARRY;
252#else
253 env->psr |= PSR_CARRY;
254#endif
255 } else {
256#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
257 env->xcc &= ~PSR_CARRY;
258#else
259 env->psr &= ~PSR_CARRY;
260#endif
261 }
262 env->regwptr[0] = ret;
263 /* next instruction */
cdba95bd
BS
264#if defined(TARGET_SPARC64)
265 if (bsd_type == target_openbsd &&
266 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
84778508
BS
267 env->pc = env->gregs[2];
268 env->npc = env->pc + 4;
cdba95bd
BS
269 } else if (bsd_type == target_openbsd &&
270 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
84778508
BS
271 env->pc = env->gregs[7];
272 env->npc = env->pc + 4;
273 } else {
274 env->pc = env->npc;
275 env->npc = env->npc + 4;
276 }
277#else
278 env->pc = env->npc;
279 env->npc = env->npc + 4;
280#endif
281 break;
282 case 0x83: /* flush windows */
283#ifdef TARGET_ABI32
284 case 0x103:
285#endif
286 flush_windows(env);
287 /* next instruction */
288 env->pc = env->npc;
289 env->npc = env->npc + 4;
290 break;
291#ifndef TARGET_SPARC64
292 case TT_WIN_OVF: /* window overflow */
293 save_window(env);
294 break;
295 case TT_WIN_UNF: /* window underflow */
296 restore_window(env);
297 break;
298 case TT_TFAULT:
299 case TT_DFAULT:
300#if 0
301 {
302 info.si_signo = SIGSEGV;
303 info.si_errno = 0;
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);
308 }
309#endif
310 break;
311#else
312 case TT_SPILL: /* window overflow */
313 save_window(env);
314 break;
315 case TT_FILL: /* window underflow */
316 restore_window(env);
317 break;
318 case TT_TFAULT:
319 case TT_DFAULT:
320#if 0
321 {
322 info.si_signo = SIGSEGV;
323 info.si_errno = 0;
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];
328 else
329 info._sifields._sigfault._addr = env->tsptr->tpc;
330 //queue_signal(env, info.si_signo, &info);
331 }
332#endif
333 break;
334#endif
335 case EXCP_INTERRUPT:
336 /* just indicate that signals should be handled asap */
337 break;
338 case EXCP_DEBUG:
339 {
340 int sig;
341
342 sig = gdb_handlesig (env, TARGET_SIGTRAP);
343#if 0
344 if (sig)
345 {
346 info.si_signo = sig;
347 info.si_errno = 0;
348 info.si_code = TARGET_TRAP_BRKPT;
349 //queue_signal(env, info.si_signo, &info);
350 }
351#endif
352 }
353 break;
354 default:
355 printf ("Unhandled trap: 0x%x\n", trapnr);
356 cpu_dump_state(env, stderr, fprintf, 0);
357 exit (1);
358 }
359 process_pending_signals (env);
360 }
361}
362
363#endif
364
365static void usage(void)
366{
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"
370 "\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"
379 "\n"
380 "Debug options:\n"
381 "-d options activate log (logfile=%s)\n"
382 "-p pagesize set the host page size to 'pagesize'\n"
1b530a6d 383 "-singlestep always run in singlestep mode\n"
84778508
BS
384 "-strace log system calls\n"
385 "\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"
389 ,
390 TARGET_ARCH,
391 interp_prefix,
392 x86_stack_size,
393 DEBUG_LOGFILE);
2d18e637 394 exit(1);
84778508
BS
395}
396
397THREAD CPUState *thread_env;
398
399/* Assumes contents are already zeroed. */
400void init_task_state(TaskState *ts)
401{
402 int i;
403
404 ts->used = 1;
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];
408 }
409 ts->sigqueue_table[i].next = NULL;
410}
411
412int main(int argc, char **argv)
413{
414 const char *filename;
415 const char *cpu_model;
416 struct target_pt_regs regs1, *regs = &regs1;
417 struct image_info info1, *info = &info1;
418 TaskState ts1, *ts = &ts1;
419 CPUState *env;
420 int optind;
421 const char *r;
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;
426
427 if (argc <= 1)
428 usage();
429
430 /* init debug */
431 cpu_set_log_filename(DEBUG_LOGFILE);
432
433 cpu_model = NULL;
434 optind = 1;
435 for(;;) {
436 if (optind >= argc)
437 break;
438 r = argv[optind];
439 if (r[0] != '-')
440 break;
441 optind++;
442 r++;
443 if (!strcmp(r, "-")) {
444 break;
445 } else if (!strcmp(r, "d")) {
446 int mask;
447 const CPULogItem *item;
448
449 if (optind >= argc)
450 break;
451
452 r = argv[optind++];
453 mask = cpu_str_to_log_mask(r);
454 if (!mask) {
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);
458 }
459 exit(1);
460 }
461 cpu_set_log(mask);
462 } else if (!strcmp(r, "s")) {
463 r = argv[optind++];
464 x86_stack_size = strtol(r, (char **)&r, 0);
465 if (x86_stack_size <= 0)
466 usage();
467 if (*r == 'M')
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");
478 exit(1);
479 }
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);
490#endif
2d18e637 491 exit(1);
84778508
BS
492 }
493 } else if (!strcmp(r, "drop-ld-preload")) {
494 drop_ld_preload = 1;
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;
502 } else {
503 usage();
504 }
505 optind++;
1b530a6d
AJ
506 } else if (!strcmp(r, "singlestep")) {
507 singlestep = 1;
84778508
BS
508 } else if (!strcmp(r, "strace")) {
509 do_strace = 1;
510 } else
511 {
512 usage();
513 }
514 }
515 if (optind >= argc)
516 usage();
517 filename = argv[optind];
518
519 /* Zero out regs */
520 memset(regs, 0, sizeof(struct target_pt_regs));
521
522 /* Zero out image_info */
523 memset(info, 0, sizeof(struct image_info));
524
525 /* Scan interp_prefix dir for replacement files. */
526 init_paths(interp_prefix);
527
528 if (cpu_model == NULL) {
529#if defined(TARGET_SPARC)
530#ifdef TARGET_SPARC64
531 cpu_model = "TI UltraSparc II";
532#else
533 cpu_model = "Fujitsu MB86904";
534#endif
535#else
536 cpu_model = "any";
537#endif
538 }
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);
543 if (!env) {
544 fprintf(stderr, "Unable to find CPU definition\n");
545 exit(1);
546 }
547 thread_env = env;
548
549 if (getenv("QEMU_STRACE")) {
550 do_strace = 1;
551 }
552
553 wrk = environ;
554 while (*(wrk++))
555 environ_count++;
556
557 target_environ = malloc((environ_count + 1) * sizeof(char *));
558 if (!target_environ)
559 abort();
560 for (wrk = environ, dst = target_environ; *wrk; wrk++) {
561 if (drop_ld_preload && !strncmp(*wrk, "LD_PRELOAD=", 11))
562 continue;
563 *(dst++) = strdup(*wrk);
564 }
565 *dst = NULL; /* NULL terminate target_environ */
566
567 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
568 printf("Error loading %s\n", filename);
569 _exit(1);
570 }
571
572 for (wrk = target_environ; *wrk; wrk++) {
573 free(*wrk);
574 }
575
576 free(target_environ);
577
2e77eac6
BS
578 if (qemu_log_enabled()) {
579 log_page_dump();
580
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",
584 info->start_code);
585 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
586 info->start_data);
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",
589 info->start_stack);
590 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
591 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
592 }
84778508
BS
593
594 target_set_brk(info->brk);
595 syscall_init();
596 signal_init();
597
598 /* build Task State */
599 memset(ts, 0, sizeof(TaskState));
600 init_task_state(ts);
601 ts->info = info;
602 env->opaque = ts;
84778508
BS
603
604#if defined(TARGET_SPARC)
605 {
606 int i;
607 env->pc = regs->pc;
608 env->npc = regs->npc;
609 env->y = regs->y;
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];
614 }
615#else
616#error unsupported target CPU
617#endif
618
619 if (gdbstub_port) {
620 gdbserver_start (gdbstub_port);
621 gdb_handlesig(env, 0);
622 }
623 cpu_loop(env, bsd_type);
624 /* never exits */
625 return 0;
626}