main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. #include "qemu/osdep.h"
  21. #include <sys/resource.h>
  22. #include <sys/sysctl.h>
  23. #include "qemu/help-texts.h"
  24. #include "qemu/units.h"
  25. #include "qemu/accel.h"
  26. #include "qemu-version.h"
  27. #include <machine/trap.h>
  28. #include "qapi/error.h"
  29. #include "qemu.h"
  30. #include "qemu/config-file.h"
  31. #include "qemu/error-report.h"
  32. #include "qemu/path.h"
  33. #include "qemu/help_option.h"
  34. #include "qemu/module.h"
  35. #include "exec/exec-all.h"
  36. #include "tcg/tcg.h"
  37. #include "qemu/timer.h"
  38. #include "qemu/envlist.h"
  39. #include "qemu/cutils.h"
  40. #include "exec/log.h"
  41. #include "trace/control.h"
  42. #include "crypto/init.h"
  43. #include "qemu/guest-random.h"
  44. #include "gdbstub/user.h"
  45. #include "host-os.h"
  46. #include "target_arch_cpu.h"
  47. int singlestep;
  48. uintptr_t guest_base;
  49. bool have_guest_base;
  50. /*
  51. * When running 32-on-64 we should make sure we can fit all of the possible
  52. * guest address space into a contiguous chunk of virtual host memory.
  53. *
  54. * This way we will never overlap with our own libraries or binaries or stack
  55. * or anything else that QEMU maps.
  56. *
  57. * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
  58. * of the address for the kernel. Some cpus rely on this and user space
  59. * uses the high bit(s) for pointer tagging and the like. For them, we
  60. * must preserve the expected address space.
  61. */
  62. #ifndef MAX_RESERVED_VA
  63. # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
  64. # if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
  65. (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
  66. # define MAX_RESERVED_VA 0xfffffffful
  67. # else
  68. # define MAX_RESERVED_VA ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
  69. # endif
  70. # else
  71. # define MAX_RESERVED_VA 0
  72. # endif
  73. #endif
  74. /*
  75. * That said, reserving *too* much vm space via mmap can run into problems
  76. * with rlimits, oom due to page table creation, etc. We will still try it,
  77. * if directed by the command-line option, but not by default.
  78. */
  79. #if HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32
  80. unsigned long reserved_va = MAX_RESERVED_VA;
  81. #else
  82. unsigned long reserved_va;
  83. #endif
  84. static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
  85. const char *qemu_uname_release;
  86. char qemu_proc_pathname[PATH_MAX]; /* full path to exeutable */
  87. unsigned long target_maxtsiz = TARGET_MAXTSIZ; /* max text size */
  88. unsigned long target_dfldsiz = TARGET_DFLDSIZ; /* initial data size limit */
  89. unsigned long target_maxdsiz = TARGET_MAXDSIZ; /* max data size */
  90. unsigned long target_dflssiz = TARGET_DFLSSIZ; /* initial data size limit */
  91. unsigned long target_maxssiz = TARGET_MAXSSIZ; /* max stack size */
  92. unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount to grow stack */
  93. /* Helper routines for implementing atomic operations. */
  94. void fork_start(void)
  95. {
  96. start_exclusive();
  97. cpu_list_lock();
  98. mmap_fork_start();
  99. }
  100. void fork_end(int child)
  101. {
  102. if (child) {
  103. CPUState *cpu, *next_cpu;
  104. /*
  105. * Child processes created by fork() only have a single thread. Discard
  106. * information about the parent threads.
  107. */
  108. CPU_FOREACH_SAFE(cpu, next_cpu) {
  109. if (cpu != thread_cpu) {
  110. QTAILQ_REMOVE_RCU(&cpus, cpu, node);
  111. }
  112. }
  113. mmap_fork_end(child);
  114. /*
  115. * qemu_init_cpu_list() takes care of reinitializing the exclusive
  116. * state, so we don't need to end_exclusive() here.
  117. */
  118. qemu_init_cpu_list();
  119. gdbserver_fork(thread_cpu);
  120. } else {
  121. mmap_fork_end(child);
  122. cpu_list_unlock();
  123. end_exclusive();
  124. }
  125. }
  126. void cpu_loop(CPUArchState *env)
  127. {
  128. target_cpu_loop(env);
  129. }
  130. static void usage(void)
  131. {
  132. printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
  133. "\n" QEMU_COPYRIGHT "\n"
  134. "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
  135. "BSD CPU emulator (compiled for %s emulation)\n"
  136. "\n"
  137. "Standard options:\n"
  138. "-h print this help\n"
  139. "-g port wait gdb connection to port\n"
  140. "-L path set the elf interpreter prefix (default=%s)\n"
  141. "-s size set the stack size in bytes (default=%ld)\n"
  142. "-cpu model select CPU (-cpu help for list)\n"
  143. "-drop-ld-preload drop LD_PRELOAD for target process\n"
  144. "-E var=value sets/modifies targets environment variable(s)\n"
  145. "-U var unsets targets environment variable(s)\n"
  146. "-B address set guest_base address to address\n"
  147. "\n"
  148. "Debug options:\n"
  149. "-d item1[,...] enable logging of specified items\n"
  150. " (use '-d help' for a list of log items)\n"
  151. "-D logfile write logs to 'logfile' (default stderr)\n"
  152. "-singlestep always run in singlestep mode\n"
  153. "-strace log system calls\n"
  154. "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
  155. " specify tracing options\n"
  156. "\n"
  157. "Environment variables:\n"
  158. "QEMU_STRACE Print system calls and arguments similar to the\n"
  159. " 'strace' program. Enable by setting to any value.\n"
  160. "You can use -E and -U options to set/unset environment variables\n"
  161. "for target process. It is possible to provide several variables\n"
  162. "by repeating the option. For example:\n"
  163. " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
  164. "Note that if you provide several changes to single variable\n"
  165. "last change will stay in effect.\n"
  166. "\n"
  167. QEMU_HELP_BOTTOM "\n"
  168. ,
  169. TARGET_NAME,
  170. interp_prefix,
  171. target_dflssiz);
  172. exit(1);
  173. }
  174. __thread CPUState *thread_cpu;
  175. void stop_all_tasks(void)
  176. {
  177. /*
  178. * We trust when using NPTL (pthreads) start_exclusive() handles thread
  179. * stopping correctly.
  180. */
  181. start_exclusive();
  182. }
  183. bool qemu_cpu_is_self(CPUState *cpu)
  184. {
  185. return thread_cpu == cpu;
  186. }
  187. void qemu_cpu_kick(CPUState *cpu)
  188. {
  189. cpu_exit(cpu);
  190. }
  191. /* Assumes contents are already zeroed. */
  192. static void init_task_state(TaskState *ts)
  193. {
  194. ts->sigaltstack_used = (struct target_sigaltstack) {
  195. .ss_sp = 0,
  196. .ss_size = 0,
  197. .ss_flags = TARGET_SS_DISABLE,
  198. };
  199. }
  200. void gemu_log(const char *fmt, ...)
  201. {
  202. va_list ap;
  203. va_start(ap, fmt);
  204. vfprintf(stderr, fmt, ap);
  205. va_end(ap);
  206. }
  207. static void
  208. adjust_ssize(void)
  209. {
  210. struct rlimit rl;
  211. if (getrlimit(RLIMIT_STACK, &rl) != 0) {
  212. return;
  213. }
  214. target_maxssiz = MIN(target_maxssiz, rl.rlim_max);
  215. target_dflssiz = MIN(MAX(target_dflssiz, rl.rlim_cur), target_maxssiz);
  216. rl.rlim_max = target_maxssiz;
  217. rl.rlim_cur = target_dflssiz;
  218. setrlimit(RLIMIT_STACK, &rl);
  219. }
  220. static void save_proc_pathname(char *argv0)
  221. {
  222. int mib[4];
  223. size_t len;
  224. mib[0] = CTL_KERN;
  225. mib[1] = KERN_PROC;
  226. mib[2] = KERN_PROC_PATHNAME;
  227. mib[3] = -1;
  228. len = sizeof(qemu_proc_pathname);
  229. if (sysctl(mib, 4, qemu_proc_pathname, &len, NULL, 0)) {
  230. perror("sysctl");
  231. }
  232. }
  233. int main(int argc, char **argv)
  234. {
  235. const char *filename;
  236. const char *cpu_model;
  237. const char *cpu_type;
  238. const char *log_file = NULL;
  239. const char *log_mask = NULL;
  240. const char *seed_optarg = NULL;
  241. struct target_pt_regs regs1, *regs = &regs1;
  242. struct image_info info1, *info = &info1;
  243. struct bsd_binprm bprm;
  244. TaskState *ts;
  245. CPUArchState *env;
  246. CPUState *cpu;
  247. int optind, rv;
  248. const char *r;
  249. const char *gdbstub = NULL;
  250. char **target_environ, **wrk;
  251. envlist_t *envlist = NULL;
  252. char *argv0 = NULL;
  253. adjust_ssize();
  254. if (argc <= 1) {
  255. usage();
  256. }
  257. save_proc_pathname(argv[0]);
  258. error_init(argv[0]);
  259. module_call_init(MODULE_INIT_TRACE);
  260. qemu_init_cpu_list();
  261. module_call_init(MODULE_INIT_QOM);
  262. envlist = envlist_create();
  263. /* add current environment into the list */
  264. for (wrk = environ; *wrk != NULL; wrk++) {
  265. (void) envlist_setenv(envlist, *wrk);
  266. }
  267. cpu_model = NULL;
  268. qemu_add_opts(&qemu_trace_opts);
  269. optind = 1;
  270. for (;;) {
  271. if (optind >= argc) {
  272. break;
  273. }
  274. r = argv[optind];
  275. if (r[0] != '-') {
  276. break;
  277. }
  278. optind++;
  279. r++;
  280. if (!strcmp(r, "-")) {
  281. break;
  282. } else if (!strcmp(r, "d")) {
  283. if (optind >= argc) {
  284. break;
  285. }
  286. log_mask = argv[optind++];
  287. } else if (!strcmp(r, "D")) {
  288. if (optind >= argc) {
  289. break;
  290. }
  291. log_file = argv[optind++];
  292. } else if (!strcmp(r, "E")) {
  293. r = argv[optind++];
  294. if (envlist_setenv(envlist, r) != 0) {
  295. usage();
  296. }
  297. } else if (!strcmp(r, "ignore-environment")) {
  298. envlist_free(envlist);
  299. envlist = envlist_create();
  300. } else if (!strcmp(r, "U")) {
  301. r = argv[optind++];
  302. if (envlist_unsetenv(envlist, r) != 0) {
  303. usage();
  304. }
  305. } else if (!strcmp(r, "s")) {
  306. r = argv[optind++];
  307. rv = qemu_strtoul(r, &r, 0, &target_dflssiz);
  308. if (rv < 0 || target_dflssiz <= 0) {
  309. usage();
  310. }
  311. if (*r == 'M') {
  312. target_dflssiz *= 1024 * 1024;
  313. } else if (*r == 'k' || *r == 'K') {
  314. target_dflssiz *= 1024;
  315. }
  316. if (target_dflssiz > target_maxssiz) {
  317. usage();
  318. }
  319. } else if (!strcmp(r, "L")) {
  320. interp_prefix = argv[optind++];
  321. } else if (!strcmp(r, "p")) {
  322. qemu_host_page_size = atoi(argv[optind++]);
  323. if (qemu_host_page_size == 0 ||
  324. (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
  325. fprintf(stderr, "page size must be a power of two\n");
  326. exit(1);
  327. }
  328. } else if (!strcmp(r, "g")) {
  329. gdbstub = g_strdup(argv[optind++]);
  330. } else if (!strcmp(r, "r")) {
  331. qemu_uname_release = argv[optind++];
  332. } else if (!strcmp(r, "cpu")) {
  333. cpu_model = argv[optind++];
  334. if (is_help_option(cpu_model)) {
  335. /* XXX: implement xxx_cpu_list for targets that still miss it */
  336. #if defined(cpu_list)
  337. cpu_list();
  338. #endif
  339. exit(1);
  340. }
  341. } else if (!strcmp(r, "B")) {
  342. rv = qemu_strtoul(argv[optind++], NULL, 0, &guest_base);
  343. if (rv < 0) {
  344. usage();
  345. }
  346. have_guest_base = true;
  347. } else if (!strcmp(r, "drop-ld-preload")) {
  348. (void) envlist_unsetenv(envlist, "LD_PRELOAD");
  349. } else if (!strcmp(r, "seed")) {
  350. seed_optarg = optarg;
  351. } else if (!strcmp(r, "singlestep")) {
  352. singlestep = 1;
  353. } else if (!strcmp(r, "strace")) {
  354. do_strace = 1;
  355. } else if (!strcmp(r, "trace")) {
  356. trace_opt_parse(optarg);
  357. } else if (!strcmp(r, "0")) {
  358. argv0 = argv[optind++];
  359. } else {
  360. usage();
  361. }
  362. }
  363. /* init debug */
  364. {
  365. int mask = 0;
  366. if (log_mask) {
  367. mask = qemu_str_to_log_mask(log_mask);
  368. if (!mask) {
  369. qemu_print_log_usage(stdout);
  370. exit(1);
  371. }
  372. }
  373. qemu_set_log_filename_flags(log_file, mask, &error_fatal);
  374. }
  375. if (optind >= argc) {
  376. usage();
  377. }
  378. filename = argv[optind];
  379. if (argv0) {
  380. argv[optind] = argv0;
  381. }
  382. if (!trace_init_backends()) {
  383. exit(1);
  384. }
  385. trace_init_file();
  386. /* Zero out regs */
  387. memset(regs, 0, sizeof(struct target_pt_regs));
  388. /* Zero bsd params */
  389. memset(&bprm, 0, sizeof(bprm));
  390. /* Zero out image_info */
  391. memset(info, 0, sizeof(struct image_info));
  392. /* Scan interp_prefix dir for replacement files. */
  393. init_paths(interp_prefix);
  394. if (cpu_model == NULL) {
  395. cpu_model = TARGET_DEFAULT_CPU_MODEL;
  396. }
  397. cpu_type = parse_cpu_option(cpu_model);
  398. /* init tcg before creating CPUs and to get qemu_host_page_size */
  399. {
  400. AccelClass *ac = ACCEL_GET_CLASS(current_accel());
  401. accel_init_interfaces(ac);
  402. ac->init_machine(NULL);
  403. }
  404. cpu = cpu_create(cpu_type);
  405. env = cpu->env_ptr;
  406. cpu_reset(cpu);
  407. thread_cpu = cpu;
  408. if (getenv("QEMU_STRACE")) {
  409. do_strace = 1;
  410. }
  411. target_environ = envlist_to_environ(envlist, NULL);
  412. envlist_free(envlist);
  413. if (reserved_va) {
  414. mmap_next_start = reserved_va + 1;
  415. }
  416. {
  417. Error *err = NULL;
  418. if (seed_optarg != NULL) {
  419. qemu_guest_random_seed_main(seed_optarg, &err);
  420. } else {
  421. qcrypto_init(&err);
  422. }
  423. if (err) {
  424. error_reportf_err(err, "cannot initialize crypto: ");
  425. exit(1);
  426. }
  427. }
  428. /*
  429. * Now that page sizes are configured we can do
  430. * proper page alignment for guest_base.
  431. */
  432. guest_base = HOST_PAGE_ALIGN(guest_base);
  433. if (loader_exec(filename, argv + optind, target_environ, regs, info,
  434. &bprm) != 0) {
  435. printf("Error loading %s\n", filename);
  436. _exit(1);
  437. }
  438. for (wrk = target_environ; *wrk; wrk++) {
  439. g_free(*wrk);
  440. }
  441. g_free(target_environ);
  442. if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
  443. FILE *f = qemu_log_trylock();
  444. if (f) {
  445. fprintf(f, "guest_base %p\n", (void *)guest_base);
  446. fprintf(f, "page layout changed following binary load\n");
  447. page_dump(f);
  448. fprintf(f, "start_brk 0x" TARGET_ABI_FMT_lx "\n",
  449. info->start_brk);
  450. fprintf(f, "end_code 0x" TARGET_ABI_FMT_lx "\n",
  451. info->end_code);
  452. fprintf(f, "start_code 0x" TARGET_ABI_FMT_lx "\n",
  453. info->start_code);
  454. fprintf(f, "start_data 0x" TARGET_ABI_FMT_lx "\n",
  455. info->start_data);
  456. fprintf(f, "end_data 0x" TARGET_ABI_FMT_lx "\n",
  457. info->end_data);
  458. fprintf(f, "start_stack 0x" TARGET_ABI_FMT_lx "\n",
  459. info->start_stack);
  460. fprintf(f, "brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
  461. fprintf(f, "entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
  462. qemu_log_unlock(f);
  463. }
  464. }
  465. /* build Task State */
  466. ts = g_new0(TaskState, 1);
  467. init_task_state(ts);
  468. ts->info = info;
  469. ts->bprm = &bprm;
  470. cpu->opaque = ts;
  471. target_set_brk(info->brk);
  472. syscall_init();
  473. signal_init();
  474. /*
  475. * Now that we've loaded the binary, GUEST_BASE is fixed. Delay
  476. * generating the prologue until now so that the prologue can take
  477. * the real value of GUEST_BASE into account.
  478. */
  479. tcg_prologue_init(tcg_ctx);
  480. target_cpu_init(env, regs);
  481. if (gdbstub) {
  482. gdbserver_start(gdbstub);
  483. gdb_handlesig(cpu, 0);
  484. }
  485. cpu_loop(env);
  486. /* never exits */
  487. return 0;
  488. }