]> git.proxmox.com Git - mirror_qemu.git/blob - bsd-user/main.c
bsd-user: Move per-cpu code into target_arch_cpu.h
[mirror_qemu.git] / bsd-user / main.c
1 /*
2 * qemu bsd user main
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2013-14 Stacey Son
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "qemu/osdep.h"
22 #include "qemu-common.h"
23 #include "qemu/units.h"
24 #include "qemu/accel.h"
25 #include "sysemu/tcg.h"
26 #include "qemu-version.h"
27 #include <machine/trap.h>
28
29 #include "qapi/error.h"
30 #include "qemu.h"
31 #include "qemu/config-file.h"
32 #include "qemu/error-report.h"
33 #include "qemu/path.h"
34 #include "qemu/help_option.h"
35 #include "qemu/module.h"
36 #include "exec/exec-all.h"
37 #include "tcg/tcg.h"
38 #include "qemu/timer.h"
39 #include "qemu/envlist.h"
40 #include "qemu/cutils.h"
41 #include "exec/log.h"
42 #include "trace/control.h"
43
44 #include "host-os.h"
45 #include "target_arch_cpu.h"
46
47 #include <sys/sysctl.h>
48
49 int singlestep;
50 unsigned long mmap_min_addr;
51 uintptr_t guest_base;
52 bool have_guest_base;
53 unsigned long reserved_va;
54
55 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
56 const char *qemu_uname_release;
57 enum BSDType bsd_type;
58 char qemu_proc_pathname[PATH_MAX]; /* full path to exeutable */
59
60 /*
61 * XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
62 * we allocate a bigger stack. Need a better solution, for example
63 * by remapping the process stack directly at the right place
64 */
65 unsigned long x86_stack_size = 512 * 1024;
66
67 void gemu_log(const char *fmt, ...)
68 {
69 va_list ap;
70
71 va_start(ap, fmt);
72 vfprintf(stderr, fmt, ap);
73 va_end(ap);
74 }
75
76 void fork_start(void)
77 {
78 }
79
80 void fork_end(int child)
81 {
82 if (child) {
83 gdbserver_fork(thread_cpu);
84 }
85 }
86
87 void cpu_loop(CPUArchState *env)
88 {
89 target_cpu_loop(env);
90 }
91
92 static void usage(void)
93 {
94 printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
95 "\n" QEMU_COPYRIGHT "\n"
96 "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
97 "BSD CPU emulator (compiled for %s emulation)\n"
98 "\n"
99 "Standard options:\n"
100 "-h print this help\n"
101 "-g port wait gdb connection to port\n"
102 "-L path set the elf interpreter prefix (default=%s)\n"
103 "-s size set the stack size in bytes (default=%ld)\n"
104 "-cpu model select CPU (-cpu help for list)\n"
105 "-drop-ld-preload drop LD_PRELOAD for target process\n"
106 "-E var=value sets/modifies targets environment variable(s)\n"
107 "-U var unsets targets environment variable(s)\n"
108 "-B address set guest_base address to address\n"
109 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
110 "\n"
111 "Debug options:\n"
112 "-d item1[,...] enable logging of specified items\n"
113 " (use '-d help' for a list of log items)\n"
114 "-D logfile write logs to 'logfile' (default stderr)\n"
115 "-p pagesize set the host page size to 'pagesize'\n"
116 "-singlestep always run in singlestep mode\n"
117 "-strace log system calls\n"
118 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
119 " specify tracing options\n"
120 "\n"
121 "Environment variables:\n"
122 "QEMU_STRACE Print system calls and arguments similar to the\n"
123 " 'strace' program. Enable by setting to any value.\n"
124 "You can use -E and -U options to set/unset environment variables\n"
125 "for target process. It is possible to provide several variables\n"
126 "by repeating the option. For example:\n"
127 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
128 "Note that if you provide several changes to single variable\n"
129 "last change will stay in effect.\n"
130 "\n"
131 QEMU_HELP_BOTTOM "\n"
132 ,
133 TARGET_NAME,
134 interp_prefix,
135 x86_stack_size);
136 exit(1);
137 }
138
139 __thread CPUState *thread_cpu;
140
141 bool qemu_cpu_is_self(CPUState *cpu)
142 {
143 return thread_cpu == cpu;
144 }
145
146 void qemu_cpu_kick(CPUState *cpu)
147 {
148 cpu_exit(cpu);
149 }
150
151 /* Assumes contents are already zeroed. */
152 void init_task_state(TaskState *ts)
153 {
154 int i;
155
156 ts->used = 1;
157 ts->first_free = ts->sigqueue_table;
158 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
159 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
160 }
161 ts->sigqueue_table[i].next = NULL;
162 }
163
164 static void save_proc_pathname(char *argv0)
165 {
166 int mib[4];
167 size_t len;
168
169 mib[0] = CTL_KERN;
170 mib[1] = KERN_PROC;
171 mib[2] = KERN_PROC_PATHNAME;
172 mib[3] = -1;
173
174 len = sizeof(qemu_proc_pathname);
175 if (sysctl(mib, 4, qemu_proc_pathname, &len, NULL, 0)) {
176 perror("sysctl");
177 }
178 }
179
180 int main(int argc, char **argv)
181 {
182 const char *filename;
183 const char *cpu_model;
184 const char *cpu_type;
185 const char *log_file = NULL;
186 const char *log_mask = NULL;
187 struct target_pt_regs regs1, *regs = &regs1;
188 struct image_info info1, *info = &info1;
189 struct bsd_binprm bprm;
190 TaskState *ts;
191 CPUArchState *env;
192 CPUState *cpu;
193 int optind, rv;
194 const char *r;
195 const char *gdbstub = NULL;
196 char **target_environ, **wrk;
197 envlist_t *envlist = NULL;
198 bsd_type = HOST_DEFAULT_BSD_TYPE;
199
200 if (argc <= 1) {
201 usage();
202 }
203
204 save_proc_pathname(argv[0]);
205
206 error_init(argv[0]);
207 module_call_init(MODULE_INIT_TRACE);
208 qemu_init_cpu_list();
209 module_call_init(MODULE_INIT_QOM);
210
211 envlist = envlist_create();
212
213 /* add current environment into the list */
214 for (wrk = environ; *wrk != NULL; wrk++) {
215 (void) envlist_setenv(envlist, *wrk);
216 }
217
218 cpu_model = NULL;
219
220 qemu_add_opts(&qemu_trace_opts);
221
222 optind = 1;
223 for (;;) {
224 if (optind >= argc) {
225 break;
226 }
227 r = argv[optind];
228 if (r[0] != '-') {
229 break;
230 }
231 optind++;
232 r++;
233 if (!strcmp(r, "-")) {
234 break;
235 } else if (!strcmp(r, "d")) {
236 if (optind >= argc) {
237 break;
238 }
239 log_mask = argv[optind++];
240 } else if (!strcmp(r, "D")) {
241 if (optind >= argc) {
242 break;
243 }
244 log_file = argv[optind++];
245 } else if (!strcmp(r, "E")) {
246 r = argv[optind++];
247 if (envlist_setenv(envlist, r) != 0) {
248 usage();
249 }
250 } else if (!strcmp(r, "ignore-environment")) {
251 envlist_free(envlist);
252 envlist = envlist_create();
253 } else if (!strcmp(r, "U")) {
254 r = argv[optind++];
255 if (envlist_unsetenv(envlist, r) != 0) {
256 usage();
257 }
258 } else if (!strcmp(r, "s")) {
259 r = argv[optind++];
260 rv = qemu_strtoul(r, &r, 0, &x86_stack_size);
261 if (rv < 0 || x86_stack_size <= 0) {
262 usage();
263 }
264 if (*r == 'M') {
265 x86_stack_size *= MiB;
266 } else if (*r == 'k' || *r == 'K') {
267 x86_stack_size *= KiB;
268 }
269 } else if (!strcmp(r, "L")) {
270 interp_prefix = argv[optind++];
271 } else if (!strcmp(r, "p")) {
272 qemu_host_page_size = atoi(argv[optind++]);
273 if (qemu_host_page_size == 0 ||
274 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
275 fprintf(stderr, "page size must be a power of two\n");
276 exit(1);
277 }
278 } else if (!strcmp(r, "g")) {
279 gdbstub = g_strdup(argv[optind++]);
280 } else if (!strcmp(r, "r")) {
281 qemu_uname_release = argv[optind++];
282 } else if (!strcmp(r, "cpu")) {
283 cpu_model = argv[optind++];
284 if (is_help_option(cpu_model)) {
285 /* XXX: implement xxx_cpu_list for targets that still miss it */
286 #if defined(cpu_list)
287 cpu_list();
288 #endif
289 exit(1);
290 }
291 } else if (!strcmp(r, "B")) {
292 rv = qemu_strtoul(argv[optind++], NULL, 0, &guest_base);
293 if (rv < 0) {
294 usage();
295 }
296 have_guest_base = true;
297 } else if (!strcmp(r, "drop-ld-preload")) {
298 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
299 } else if (!strcmp(r, "bsd")) {
300 if (!strcasecmp(argv[optind], "freebsd")) {
301 bsd_type = target_freebsd;
302 } else if (!strcasecmp(argv[optind], "netbsd")) {
303 bsd_type = target_netbsd;
304 } else if (!strcasecmp(argv[optind], "openbsd")) {
305 bsd_type = target_openbsd;
306 } else {
307 usage();
308 }
309 optind++;
310 } else if (!strcmp(r, "singlestep")) {
311 singlestep = 1;
312 } else if (!strcmp(r, "strace")) {
313 do_strace = 1;
314 } else if (!strcmp(r, "trace")) {
315 trace_opt_parse(optarg);
316 } else {
317 usage();
318 }
319 }
320
321 /* init debug */
322 qemu_log_needs_buffers();
323 qemu_set_log_filename(log_file, &error_fatal);
324 if (log_mask) {
325 int mask;
326
327 mask = qemu_str_to_log_mask(log_mask);
328 if (!mask) {
329 qemu_print_log_usage(stdout);
330 exit(1);
331 }
332 qemu_set_log(mask);
333 }
334
335 if (optind >= argc) {
336 usage();
337 }
338 filename = argv[optind];
339
340 if (!trace_init_backends()) {
341 exit(1);
342 }
343 trace_init_file();
344
345 /* Zero out regs */
346 memset(regs, 0, sizeof(struct target_pt_regs));
347
348 /* Zero bsd params */
349 memset(&bprm, 0, sizeof(bprm));
350
351 /* Zero out image_info */
352 memset(info, 0, sizeof(struct image_info));
353
354 /* Scan interp_prefix dir for replacement files. */
355 init_paths(interp_prefix);
356
357 if (cpu_model == NULL) {
358 cpu_model = TARGET_DEFAULT_CPU_MODEL;
359 }
360
361 cpu_type = parse_cpu_option(cpu_model);
362
363 /* init tcg before creating CPUs and to get qemu_host_page_size */
364 {
365 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
366
367 accel_init_interfaces(ac);
368 ac->init_machine(NULL);
369 }
370 cpu = cpu_create(cpu_type);
371 env = cpu->env_ptr;
372 cpu_reset(cpu);
373 thread_cpu = cpu;
374
375 if (getenv("QEMU_STRACE")) {
376 do_strace = 1;
377 }
378
379 target_environ = envlist_to_environ(envlist, NULL);
380 envlist_free(envlist);
381
382 /*
383 * Now that page sizes are configured we can do
384 * proper page alignment for guest_base.
385 */
386 guest_base = HOST_PAGE_ALIGN(guest_base);
387
388 if (loader_exec(filename, argv + optind, target_environ, regs, info,
389 &bprm) != 0) {
390 printf("Error loading %s\n", filename);
391 _exit(1);
392 }
393
394 for (wrk = target_environ; *wrk; wrk++) {
395 g_free(*wrk);
396 }
397
398 g_free(target_environ);
399
400 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
401 qemu_log("guest_base %p\n", (void *)guest_base);
402 log_page_dump("binary load");
403
404 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
405 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
406 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
407 info->start_code);
408 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
409 info->start_data);
410 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
411 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
412 info->start_stack);
413 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
414 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
415 }
416
417 /* build Task State */
418 ts = g_new0(TaskState, 1);
419 init_task_state(ts);
420 ts->info = info;
421 ts->bprm = &bprm;
422 cpu->opaque = ts;
423
424 target_set_brk(info->brk);
425 syscall_init();
426 signal_init();
427
428 /*
429 * Now that we've loaded the binary, GUEST_BASE is fixed. Delay
430 * generating the prologue until now so that the prologue can take
431 * the real value of GUEST_BASE into account.
432 */
433 tcg_prologue_init(tcg_ctx);
434
435 target_cpu_init(env, regs);
436
437 if (gdbstub) {
438 gdbserver_start(gdbstub);
439 gdb_handlesig(cpu, 0);
440 }
441 cpu_loop(env);
442 /* never exits */
443 return 0;
444 }