]> git.proxmox.com Git - qemu.git/blame - bsd-user/main.c
Fix some compile and linking issues on NetBSD.
[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
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
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>
27
28#include "qemu.h"
29#include "qemu-common.h"
30/* For tb_lock */
31#include "exec-all.h"
32
33#define DEBUG_LOGFILE "/tmp/qemu.log"
34
35static const char *interp_prefix = CONFIG_QEMU_PREFIX;
36const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
37extern char **environ;
38
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 */
42unsigned long x86_stack_size = 512 * 1024;
43
44void gemu_log(const char *fmt, ...)
45{
46 va_list ap;
47
48 va_start(ap, fmt);
49 vfprintf(stderr, fmt, ap);
50 va_end(ap);
51}
52#ifdef TARGET_SPARC
53#define SPARC64_STACK_BIAS 2047
54
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 */
58static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
59{
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;
66}
67
68/* save the register window 'cwp1' */
69static inline void save_window_offset(CPUSPARCState *env, int cwp1)
70{
71 unsigned int i;
72 abi_ulong sp_ptr;
73
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);
87 }
88}
89
90static void save_window(CPUSPARCState *env)
91{
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
103}
104
105static void restore_window(CPUSPARCState *env)
106{
107#ifndef TARGET_SPARC64
108 unsigned int new_wim;
109#endif
110 unsigned int i, cwp1;
111 abi_ulong sp_ptr;
112
113#ifndef TARGET_SPARC64
114 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
115 ((1LL << env->nwindows) - 1);
116#endif
117
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);
133 }
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
142}
143
144static void flush_windows(CPUSPARCState *env)
145{
146 int offset, cwp1;
147
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++;
163 }
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
172}
173
174void cpu_loop(CPUSPARCState *env, enum BSDType bsd_type)
175{
176 int trapnr, ret, syscall_nr;
177 //target_siginfo_t info;
178
179 while (1) {
180 trapnr = cpu_sparc_exec (env);
181
182 switch (trapnr) {
77b9435f
BS
183#ifndef TARGET_SPARC64
184 case 0x80:
185#else
84778508 186 case 0x100:
77b9435f 187#endif
84778508
BS
188 syscall_nr = env->gregs[1];
189#if defined(TARGET_SPARC)
190 syscall_nr &= ~(SYSCALL_G7RFLAG | SYSCALL_G2RFLAG);
191#endif
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;
210#else
211 env->psr |= PSR_CARRY;
212#endif
213 } else {
214#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
215 env->xcc &= ~PSR_CARRY;
216#else
217 env->psr &= ~PSR_CARRY;
218#endif
219 }
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;
229 } else {
230 env->pc = env->npc;
231 env->npc = env->npc + 4;
232 }
233#else
234 env->pc = env->npc;
235 env->npc = env->npc + 4;
236#endif
237 break;
238 case 0x83: /* flush windows */
239#ifdef TARGET_ABI32
240 case 0x103:
241#endif
242 flush_windows(env);
243 /* next instruction */
244 env->pc = env->npc;
245 env->npc = env->npc + 4;
246 break;
247#ifndef TARGET_SPARC64
248 case TT_WIN_OVF: /* window overflow */
249 save_window(env);
250 break;
251 case TT_WIN_UNF: /* window underflow */
252 restore_window(env);
253 break;
254 case TT_TFAULT:
255 case TT_DFAULT:
256#if 0
257 {
258 info.si_signo = SIGSEGV;
259 info.si_errno = 0;
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);
264 }
265#endif
266 break;
267#else
268 case TT_SPILL: /* window overflow */
269 save_window(env);
270 break;
271 case TT_FILL: /* window underflow */
272 restore_window(env);
273 break;
274 case TT_TFAULT:
275 case TT_DFAULT:
276#if 0
277 {
278 info.si_signo = SIGSEGV;
279 info.si_errno = 0;
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];
284 else
285 info._sifields._sigfault._addr = env->tsptr->tpc;
286 //queue_signal(env, info.si_signo, &info);
287 }
288#endif
289 break;
290#endif
291 case EXCP_INTERRUPT:
292 /* just indicate that signals should be handled asap */
293 break;
294 case EXCP_DEBUG:
295 {
296 int sig;
297
298 sig = gdb_handlesig (env, TARGET_SIGTRAP);
299#if 0
300 if (sig)
301 {
302 info.si_signo = sig;
303 info.si_errno = 0;
304 info.si_code = TARGET_TRAP_BRKPT;
305 //queue_signal(env, info.si_signo, &info);
306 }
307#endif
308 }
309 break;
310 default:
311 printf ("Unhandled trap: 0x%x\n", trapnr);
312 cpu_dump_state(env, stderr, fprintf, 0);
313 exit (1);
314 }
315 process_pending_signals (env);
316 }
317}
318
319#endif
320
321static void usage(void)
322{
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"
326 "\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"
335 "\n"
336 "Debug options:\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"
340 "\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"
344 ,
345 TARGET_ARCH,
346 interp_prefix,
347 x86_stack_size,
348 DEBUG_LOGFILE);
349 _exit(1);
350}
351
352THREAD CPUState *thread_env;
353
354/* Assumes contents are already zeroed. */
355void init_task_state(TaskState *ts)
356{
357 int i;
358
359 ts->used = 1;
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];
363 }
364 ts->sigqueue_table[i].next = NULL;
365}
366
367int main(int argc, char **argv)
368{
369 const char *filename;
370 const char *cpu_model;
371 struct target_pt_regs regs1, *regs = &regs1;
372 struct image_info info1, *info = &info1;
373 TaskState ts1, *ts = &ts1;
374 CPUState *env;
375 int optind;
376 const char *r;
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;
381
382 if (argc <= 1)
383 usage();
384
385 /* init debug */
386 cpu_set_log_filename(DEBUG_LOGFILE);
387
388 cpu_model = NULL;
389 optind = 1;
390 for(;;) {
391 if (optind >= argc)
392 break;
393 r = argv[optind];
394 if (r[0] != '-')
395 break;
396 optind++;
397 r++;
398 if (!strcmp(r, "-")) {
399 break;
400 } else if (!strcmp(r, "d")) {
401 int mask;
402 const CPULogItem *item;
403
404 if (optind >= argc)
405 break;
406
407 r = argv[optind++];
408 mask = cpu_str_to_log_mask(r);
409 if (!mask) {
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);
413 }
414 exit(1);
415 }
416 cpu_set_log(mask);
417 } else if (!strcmp(r, "s")) {
418 r = argv[optind++];
419 x86_stack_size = strtol(r, (char **)&r, 0);
420 if (x86_stack_size <= 0)
421 usage();
422 if (*r == 'M')
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");
433 exit(1);
434 }
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);
445#endif
446 _exit(1);
447 }
448 } else if (!strcmp(r, "drop-ld-preload")) {
449 drop_ld_preload = 1;
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;
457 } else {
458 usage();
459 }
460 optind++;
461 } else if (!strcmp(r, "strace")) {
462 do_strace = 1;
463 } else
464 {
465 usage();
466 }
467 }
468 if (optind >= argc)
469 usage();
470 filename = argv[optind];
471
472 /* Zero out regs */
473 memset(regs, 0, sizeof(struct target_pt_regs));
474
475 /* Zero out image_info */
476 memset(info, 0, sizeof(struct image_info));
477
478 /* Scan interp_prefix dir for replacement files. */
479 init_paths(interp_prefix);
480
481 if (cpu_model == NULL) {
482#if defined(TARGET_SPARC)
483#ifdef TARGET_SPARC64
484 cpu_model = "TI UltraSparc II";
485#else
486 cpu_model = "Fujitsu MB86904";
487#endif
488#else
489 cpu_model = "any";
490#endif
491 }
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);
496 if (!env) {
497 fprintf(stderr, "Unable to find CPU definition\n");
498 exit(1);
499 }
500 thread_env = env;
501
502 if (getenv("QEMU_STRACE")) {
503 do_strace = 1;
504 }
505
506 wrk = environ;
507 while (*(wrk++))
508 environ_count++;
509
510 target_environ = malloc((environ_count + 1) * sizeof(char *));
511 if (!target_environ)
512 abort();
513 for (wrk = environ, dst = target_environ; *wrk; wrk++) {
514 if (drop_ld_preload && !strncmp(*wrk, "LD_PRELOAD=", 11))
515 continue;
516 *(dst++) = strdup(*wrk);
517 }
518 *dst = NULL; /* NULL terminate target_environ */
519
520 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
521 printf("Error loading %s\n", filename);
522 _exit(1);
523 }
524
525 for (wrk = target_environ; *wrk; wrk++) {
526 free(*wrk);
527 }
528
529 free(target_environ);
530
531 if (loglevel) {
532 page_dump(logfile);
533
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",
537 info->start_code);
538 fprintf(logfile, "start_data 0x" TARGET_ABI_FMT_lx "\n",
539 info->start_data);
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",
542 info->start_stack);
543 fprintf(logfile, "brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
544 fprintf(logfile, "entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
545 }
546
547 target_set_brk(info->brk);
548 syscall_init();
549 signal_init();
550
551 /* build Task State */
552 memset(ts, 0, sizeof(TaskState));
553 init_task_state(ts);
554 ts->info = info;
555 env->opaque = ts;
556 env->user_mode_only = 1;
557
558#if defined(TARGET_SPARC)
559 {
560 int i;
561 env->pc = regs->pc;
562 env->npc = regs->npc;
563 env->y = regs->y;
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];
568 }
569#else
570#error unsupported target CPU
571#endif
572
573 if (gdbstub_port) {
574 gdbserver_start (gdbstub_port);
575 gdb_handlesig(env, 0);
576 }
577 cpu_loop(env, bsd_type);
578 /* never exits */
579 return 0;
580}