main.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/help-texts.h"
  21. #include "qemu/units.h"
  22. #include "qemu/accel.h"
  23. #include "qemu-version.h"
  24. #include <sys/syscall.h>
  25. #include <sys/resource.h>
  26. #include <sys/shm.h>
  27. #include <linux/binfmts.h>
  28. #include "qapi/error.h"
  29. #include "qemu.h"
  30. #include "user-internals.h"
  31. #include "qemu/path.h"
  32. #include "qemu/queue.h"
  33. #include "qemu/config-file.h"
  34. #include "qemu/cutils.h"
  35. #include "qemu/error-report.h"
  36. #include "qemu/help_option.h"
  37. #include "qemu/module.h"
  38. #include "qemu/plugin.h"
  39. #include "user/guest-base.h"
  40. #include "user/page-protection.h"
  41. #include "exec/exec-all.h"
  42. #include "exec/gdbstub.h"
  43. #include "gdbstub/user.h"
  44. #include "tcg/startup.h"
  45. #include "qemu/timer.h"
  46. #include "qemu/envlist.h"
  47. #include "qemu/guest-random.h"
  48. #include "elf.h"
  49. #include "trace/control.h"
  50. #include "target_elf.h"
  51. #include "user/cpu_loop.h"
  52. #include "crypto/init.h"
  53. #include "fd-trans.h"
  54. #include "signal-common.h"
  55. #include "loader.h"
  56. #include "user-mmap.h"
  57. #include "tcg/perf.h"
  58. #include "exec/page-vary.h"
  59. #ifdef CONFIG_SEMIHOSTING
  60. #include "semihosting/semihost.h"
  61. #endif
  62. #ifndef AT_FLAGS_PRESERVE_ARGV0
  63. #define AT_FLAGS_PRESERVE_ARGV0_BIT 0
  64. #define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT)
  65. #endif
  66. char *exec_path;
  67. char real_exec_path[PATH_MAX];
  68. static bool opt_one_insn_per_tb;
  69. static unsigned long opt_tb_size;
  70. static const char *argv0;
  71. static const char *gdbstub;
  72. static envlist_t *envlist;
  73. static const char *cpu_model;
  74. static const char *cpu_type;
  75. static const char *seed_optarg;
  76. unsigned long mmap_min_addr;
  77. uintptr_t guest_base;
  78. bool have_guest_base;
  79. /*
  80. * Used to implement backwards-compatibility for the `-strace`, and
  81. * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
  82. * -strace, or vice versa.
  83. */
  84. static bool enable_strace;
  85. /*
  86. * The last log mask given by the user in an environment variable or argument.
  87. * Used to support command line arguments overriding environment variables.
  88. */
  89. static int last_log_mask;
  90. static const char *last_log_filename;
  91. /*
  92. * When running 32-on-64 we should make sure we can fit all of the possible
  93. * guest address space into a contiguous chunk of virtual host memory.
  94. *
  95. * This way we will never overlap with our own libraries or binaries or stack
  96. * or anything else that QEMU maps.
  97. *
  98. * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
  99. * of the address for the kernel. Some cpus rely on this and user space
  100. * uses the high bit(s) for pointer tagging and the like. For them, we
  101. * must preserve the expected address space.
  102. */
  103. #ifndef MAX_RESERVED_VA
  104. # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
  105. # if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
  106. (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
  107. # define MAX_RESERVED_VA(CPU) 0xfffffffful
  108. # else
  109. # define MAX_RESERVED_VA(CPU) ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
  110. # endif
  111. # else
  112. # define MAX_RESERVED_VA(CPU) 0
  113. # endif
  114. #endif
  115. unsigned long reserved_va;
  116. static void usage(int exitcode);
  117. static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
  118. const char *qemu_uname_release;
  119. #if !defined(TARGET_DEFAULT_STACK_SIZE)
  120. /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
  121. we allocate a bigger stack. Need a better solution, for example
  122. by remapping the process stack directly at the right place */
  123. #define TARGET_DEFAULT_STACK_SIZE 8 * 1024 * 1024UL
  124. #endif
  125. unsigned long guest_stack_size = TARGET_DEFAULT_STACK_SIZE;
  126. /***********************************************************/
  127. /* Helper routines for implementing atomic operations. */
  128. /* Make sure everything is in a consistent state for calling fork(). */
  129. void fork_start(void)
  130. {
  131. start_exclusive();
  132. mmap_fork_start();
  133. cpu_list_lock();
  134. qemu_plugin_user_prefork_lock();
  135. gdbserver_fork_start();
  136. }
  137. void fork_end(pid_t pid)
  138. {
  139. bool child = pid == 0;
  140. qemu_plugin_user_postfork(child);
  141. mmap_fork_end(child);
  142. if (child) {
  143. CPUState *cpu, *next_cpu;
  144. /* Child processes created by fork() only have a single thread.
  145. Discard information about the parent threads. */
  146. CPU_FOREACH_SAFE(cpu, next_cpu) {
  147. if (cpu != thread_cpu) {
  148. QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node);
  149. }
  150. }
  151. qemu_init_cpu_list();
  152. get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id();
  153. } else {
  154. cpu_list_unlock();
  155. }
  156. gdbserver_fork_end(thread_cpu, pid);
  157. /*
  158. * qemu_init_cpu_list() reinitialized the child exclusive state, but we
  159. * also need to keep current_cpu consistent, so call end_exclusive() for
  160. * both child and parent.
  161. */
  162. end_exclusive();
  163. }
  164. __thread CPUState *thread_cpu;
  165. bool qemu_cpu_is_self(CPUState *cpu)
  166. {
  167. return thread_cpu == cpu;
  168. }
  169. void qemu_cpu_kick(CPUState *cpu)
  170. {
  171. cpu_exit(cpu);
  172. }
  173. void task_settid(TaskState *ts)
  174. {
  175. if (ts->ts_tid == 0) {
  176. ts->ts_tid = (pid_t)syscall(SYS_gettid);
  177. }
  178. }
  179. void stop_all_tasks(void)
  180. {
  181. /*
  182. * We trust that when using NPTL, start_exclusive()
  183. * handles thread stopping correctly.
  184. */
  185. start_exclusive();
  186. }
  187. /* Assumes contents are already zeroed. */
  188. void init_task_state(TaskState *ts)
  189. {
  190. long ticks_per_sec;
  191. struct timespec bt;
  192. ts->used = 1;
  193. ts->sigaltstack_used = (struct target_sigaltstack) {
  194. .ss_sp = 0,
  195. .ss_size = 0,
  196. .ss_flags = TARGET_SS_DISABLE,
  197. };
  198. /* Capture task start time relative to system boot */
  199. ticks_per_sec = sysconf(_SC_CLK_TCK);
  200. if ((ticks_per_sec > 0) && !clock_gettime(CLOCK_BOOTTIME, &bt)) {
  201. /* start_boottime is expressed in clock ticks */
  202. ts->start_boottime = bt.tv_sec * (uint64_t) ticks_per_sec;
  203. ts->start_boottime += bt.tv_nsec * (uint64_t) ticks_per_sec /
  204. NANOSECONDS_PER_SECOND;
  205. }
  206. }
  207. CPUArchState *cpu_copy(CPUArchState *env)
  208. {
  209. CPUState *cpu = env_cpu(env);
  210. CPUState *new_cpu = cpu_create(cpu_type);
  211. CPUArchState *new_env = cpu_env(new_cpu);
  212. CPUBreakpoint *bp;
  213. /* Reset non arch specific state */
  214. cpu_reset(new_cpu);
  215. new_cpu->tcg_cflags = cpu->tcg_cflags;
  216. memcpy(new_env, env, sizeof(CPUArchState));
  217. #if defined(TARGET_I386) || defined(TARGET_X86_64)
  218. new_env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
  219. PROT_READ | PROT_WRITE,
  220. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  221. memcpy(g2h_untagged(new_env->gdt.base), g2h_untagged(env->gdt.base),
  222. sizeof(uint64_t) * TARGET_GDT_ENTRIES);
  223. OBJECT(new_cpu)->free = OBJECT(cpu)->free;
  224. #endif
  225. /* Clone all break/watchpoints.
  226. Note: Once we support ptrace with hw-debug register access, make sure
  227. BP_CPU break/watchpoints are handled correctly on clone. */
  228. QTAILQ_INIT(&new_cpu->breakpoints);
  229. QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
  230. cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL);
  231. }
  232. return new_env;
  233. }
  234. static void handle_arg_help(const char *arg)
  235. {
  236. usage(EXIT_SUCCESS);
  237. }
  238. static void handle_arg_log(const char *arg)
  239. {
  240. last_log_mask = qemu_str_to_log_mask(arg);
  241. if (!last_log_mask) {
  242. qemu_print_log_usage(stdout);
  243. exit(EXIT_FAILURE);
  244. }
  245. }
  246. static void handle_arg_dfilter(const char *arg)
  247. {
  248. qemu_set_dfilter_ranges(arg, &error_fatal);
  249. }
  250. static void handle_arg_log_filename(const char *arg)
  251. {
  252. last_log_filename = arg;
  253. }
  254. static void handle_arg_set_env(const char *arg)
  255. {
  256. char *r, *p, *token;
  257. r = p = strdup(arg);
  258. while ((token = strsep(&p, ",")) != NULL) {
  259. if (envlist_setenv(envlist, token) != 0) {
  260. usage(EXIT_FAILURE);
  261. }
  262. }
  263. free(r);
  264. }
  265. static void handle_arg_unset_env(const char *arg)
  266. {
  267. char *r, *p, *token;
  268. r = p = strdup(arg);
  269. while ((token = strsep(&p, ",")) != NULL) {
  270. if (envlist_unsetenv(envlist, token) != 0) {
  271. usage(EXIT_FAILURE);
  272. }
  273. }
  274. free(r);
  275. }
  276. static void handle_arg_argv0(const char *arg)
  277. {
  278. argv0 = strdup(arg);
  279. }
  280. static void handle_arg_stack_size(const char *arg)
  281. {
  282. char *p;
  283. guest_stack_size = strtoul(arg, &p, 0);
  284. if (guest_stack_size == 0) {
  285. usage(EXIT_FAILURE);
  286. }
  287. if (*p == 'M') {
  288. guest_stack_size *= MiB;
  289. } else if (*p == 'k' || *p == 'K') {
  290. guest_stack_size *= KiB;
  291. }
  292. }
  293. static void handle_arg_ld_prefix(const char *arg)
  294. {
  295. interp_prefix = strdup(arg);
  296. }
  297. static void handle_arg_pagesize(const char *arg)
  298. {
  299. unsigned size, want = qemu_real_host_page_size();
  300. if (qemu_strtoui(arg, NULL, 10, &size) || size != want) {
  301. warn_report("Deprecated page size option cannot "
  302. "change host page size (%u)", want);
  303. }
  304. }
  305. static void handle_arg_seed(const char *arg)
  306. {
  307. seed_optarg = arg;
  308. }
  309. static void handle_arg_gdb(const char *arg)
  310. {
  311. gdbstub = g_strdup(arg);
  312. }
  313. static void handle_arg_uname(const char *arg)
  314. {
  315. qemu_uname_release = strdup(arg);
  316. }
  317. static void handle_arg_cpu(const char *arg)
  318. {
  319. cpu_model = strdup(arg);
  320. if (cpu_model == NULL || is_help_option(cpu_model)) {
  321. list_cpus();
  322. exit(EXIT_FAILURE);
  323. }
  324. }
  325. static void handle_arg_guest_base(const char *arg)
  326. {
  327. guest_base = strtol(arg, NULL, 0);
  328. have_guest_base = true;
  329. }
  330. static void handle_arg_reserved_va(const char *arg)
  331. {
  332. char *p;
  333. int shift = 0;
  334. unsigned long val;
  335. val = strtoul(arg, &p, 0);
  336. switch (*p) {
  337. case 'k':
  338. case 'K':
  339. shift = 10;
  340. break;
  341. case 'M':
  342. shift = 20;
  343. break;
  344. case 'G':
  345. shift = 30;
  346. break;
  347. }
  348. if (shift) {
  349. unsigned long unshifted = val;
  350. p++;
  351. val <<= shift;
  352. if (val >> shift != unshifted) {
  353. fprintf(stderr, "Reserved virtual address too big\n");
  354. exit(EXIT_FAILURE);
  355. }
  356. }
  357. if (*p) {
  358. fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p);
  359. exit(EXIT_FAILURE);
  360. }
  361. /* The representation is size - 1, with 0 remaining "default". */
  362. reserved_va = val ? val - 1 : 0;
  363. }
  364. static const char *rtsig_map = CONFIG_QEMU_RTSIG_MAP;
  365. static void handle_arg_rtsig_map(const char *arg)
  366. {
  367. rtsig_map = arg;
  368. }
  369. static void handle_arg_one_insn_per_tb(const char *arg)
  370. {
  371. opt_one_insn_per_tb = true;
  372. }
  373. static void handle_arg_tb_size(const char *arg)
  374. {
  375. if (qemu_strtoul(arg, NULL, 0, &opt_tb_size)) {
  376. usage(EXIT_FAILURE);
  377. }
  378. }
  379. static void handle_arg_strace(const char *arg)
  380. {
  381. enable_strace = true;
  382. }
  383. static void handle_arg_version(const char *arg)
  384. {
  385. printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
  386. "\n" QEMU_COPYRIGHT "\n");
  387. exit(EXIT_SUCCESS);
  388. }
  389. static void handle_arg_trace(const char *arg)
  390. {
  391. trace_opt_parse(arg);
  392. }
  393. #if defined(TARGET_XTENSA)
  394. static void handle_arg_abi_call0(const char *arg)
  395. {
  396. xtensa_set_abi_call0();
  397. }
  398. #endif
  399. static void handle_arg_perfmap(const char *arg)
  400. {
  401. perf_enable_perfmap();
  402. }
  403. static void handle_arg_jitdump(const char *arg)
  404. {
  405. perf_enable_jitdump();
  406. }
  407. static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
  408. #ifdef CONFIG_PLUGIN
  409. static void handle_arg_plugin(const char *arg)
  410. {
  411. qemu_plugin_opt_parse(arg, &plugins);
  412. }
  413. #endif
  414. struct qemu_argument {
  415. const char *argv;
  416. const char *env;
  417. bool has_arg;
  418. void (*handle_opt)(const char *arg);
  419. const char *example;
  420. const char *help;
  421. };
  422. static const struct qemu_argument arg_table[] = {
  423. {"h", "", false, handle_arg_help,
  424. "", "print this help"},
  425. {"help", "", false, handle_arg_help,
  426. "", ""},
  427. {"g", "QEMU_GDB", true, handle_arg_gdb,
  428. "port", "wait gdb connection to 'port'"},
  429. {"L", "QEMU_LD_PREFIX", true, handle_arg_ld_prefix,
  430. "path", "set the elf interpreter prefix to 'path'"},
  431. {"s", "QEMU_STACK_SIZE", true, handle_arg_stack_size,
  432. "size", "set the stack size to 'size' bytes"},
  433. {"cpu", "QEMU_CPU", true, handle_arg_cpu,
  434. "model", "select CPU (-cpu help for list)"},
  435. {"E", "QEMU_SET_ENV", true, handle_arg_set_env,
  436. "var=value", "sets targets environment variable (see below)"},
  437. {"U", "QEMU_UNSET_ENV", true, handle_arg_unset_env,
  438. "var", "unsets targets environment variable (see below)"},
  439. {"0", "QEMU_ARGV0", true, handle_arg_argv0,
  440. "argv0", "forces target process argv[0] to be 'argv0'"},
  441. {"r", "QEMU_UNAME", true, handle_arg_uname,
  442. "uname", "set qemu uname release string to 'uname'"},
  443. {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base,
  444. "address", "set guest_base address to 'address'"},
  445. {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va,
  446. "size", "reserve 'size' bytes for guest virtual address space"},
  447. {"t", "QEMU_RTSIG_MAP", true, handle_arg_rtsig_map,
  448. "tsig hsig n[,...]",
  449. "map target rt signals [tsig,tsig+n) to [hsig,hsig+n]"},
  450. {"d", "QEMU_LOG", true, handle_arg_log,
  451. "item[,...]", "enable logging of specified items "
  452. "(use '-d help' for a list of items)"},
  453. {"dfilter", "QEMU_DFILTER", true, handle_arg_dfilter,
  454. "range[,...]","filter logging based on address range"},
  455. {"D", "QEMU_LOG_FILENAME", true, handle_arg_log_filename,
  456. "logfile", "write logs to 'logfile' (default stderr)"},
  457. {"p", "QEMU_PAGESIZE", true, handle_arg_pagesize,
  458. "pagesize", "deprecated change to host page size"},
  459. {"one-insn-per-tb",
  460. "QEMU_ONE_INSN_PER_TB", false, handle_arg_one_insn_per_tb,
  461. "", "run with one guest instruction per emulated TB"},
  462. {"tb-size", "QEMU_TB_SIZE", true, handle_arg_tb_size,
  463. "size", "TCG translation block cache size"},
  464. {"strace", "QEMU_STRACE", false, handle_arg_strace,
  465. "", "log system calls"},
  466. {"seed", "QEMU_RAND_SEED", true, handle_arg_seed,
  467. "", "Seed for pseudo-random number generator"},
  468. {"trace", "QEMU_TRACE", true, handle_arg_trace,
  469. "", "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
  470. #ifdef CONFIG_PLUGIN
  471. {"plugin", "QEMU_PLUGIN", true, handle_arg_plugin,
  472. "", "[file=]<file>[,<argname>=<argvalue>]"},
  473. #endif
  474. {"version", "QEMU_VERSION", false, handle_arg_version,
  475. "", "display version information and exit"},
  476. #if defined(TARGET_XTENSA)
  477. {"xtensa-abi-call0", "QEMU_XTENSA_ABI_CALL0", false, handle_arg_abi_call0,
  478. "", "assume CALL0 Xtensa ABI"},
  479. #endif
  480. {"perfmap", "QEMU_PERFMAP", false, handle_arg_perfmap,
  481. "", "Generate a /tmp/perf-${pid}.map file for perf"},
  482. {"jitdump", "QEMU_JITDUMP", false, handle_arg_jitdump,
  483. "", "Generate a jit-${pid}.dump file for perf"},
  484. {NULL, NULL, false, NULL, NULL, NULL}
  485. };
  486. static void usage(int exitcode)
  487. {
  488. const struct qemu_argument *arginfo;
  489. int maxarglen;
  490. int maxenvlen;
  491. printf("usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
  492. "Linux CPU emulator (compiled for " TARGET_NAME " emulation)\n"
  493. "\n"
  494. "Options and associated environment variables:\n"
  495. "\n");
  496. /* Calculate column widths. We must always have at least enough space
  497. * for the column header.
  498. */
  499. maxarglen = strlen("Argument");
  500. maxenvlen = strlen("Env-variable");
  501. for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
  502. int arglen = strlen(arginfo->argv);
  503. if (arginfo->has_arg) {
  504. arglen += strlen(arginfo->example) + 1;
  505. }
  506. if (strlen(arginfo->env) > maxenvlen) {
  507. maxenvlen = strlen(arginfo->env);
  508. }
  509. if (arglen > maxarglen) {
  510. maxarglen = arglen;
  511. }
  512. }
  513. printf("%-*s %-*s Description\n", maxarglen+1, "Argument",
  514. maxenvlen, "Env-variable");
  515. for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
  516. if (arginfo->has_arg) {
  517. printf("-%s %-*s %-*s %s\n", arginfo->argv,
  518. (int)(maxarglen - strlen(arginfo->argv) - 1),
  519. arginfo->example, maxenvlen, arginfo->env, arginfo->help);
  520. } else {
  521. printf("-%-*s %-*s %s\n", maxarglen, arginfo->argv,
  522. maxenvlen, arginfo->env,
  523. arginfo->help);
  524. }
  525. }
  526. printf("\n"
  527. "Defaults:\n"
  528. "QEMU_LD_PREFIX = %s\n"
  529. "QEMU_STACK_SIZE = %ld byte\n",
  530. interp_prefix,
  531. guest_stack_size);
  532. printf("\n"
  533. "You can use -E and -U options or the QEMU_SET_ENV and\n"
  534. "QEMU_UNSET_ENV environment variables to set and unset\n"
  535. "environment variables for the target process.\n"
  536. "It is possible to provide several variables by separating them\n"
  537. "by commas in getsubopt(3) style. Additionally it is possible to\n"
  538. "provide the -E and -U options multiple times.\n"
  539. "The following lines are equivalent:\n"
  540. " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
  541. " -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
  542. " QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
  543. "Note that if you provide several changes to a single variable\n"
  544. "the last change will stay in effect.\n"
  545. "\n"
  546. QEMU_HELP_BOTTOM "\n");
  547. exit(exitcode);
  548. }
  549. static int parse_args(int argc, char **argv)
  550. {
  551. const char *r;
  552. int optind;
  553. const struct qemu_argument *arginfo;
  554. for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
  555. if (arginfo->env == NULL) {
  556. continue;
  557. }
  558. r = getenv(arginfo->env);
  559. if (r != NULL) {
  560. arginfo->handle_opt(r);
  561. }
  562. }
  563. optind = 1;
  564. for (;;) {
  565. if (optind >= argc) {
  566. break;
  567. }
  568. r = argv[optind];
  569. if (r[0] != '-') {
  570. break;
  571. }
  572. optind++;
  573. r++;
  574. if (!strcmp(r, "-")) {
  575. break;
  576. }
  577. /* Treat --foo the same as -foo. */
  578. if (r[0] == '-') {
  579. r++;
  580. }
  581. for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
  582. if (!strcmp(r, arginfo->argv)) {
  583. if (arginfo->has_arg) {
  584. if (optind >= argc) {
  585. (void) fprintf(stderr,
  586. "qemu: missing argument for option '%s'\n", r);
  587. exit(EXIT_FAILURE);
  588. }
  589. arginfo->handle_opt(argv[optind]);
  590. optind++;
  591. } else {
  592. arginfo->handle_opt(NULL);
  593. }
  594. break;
  595. }
  596. }
  597. /* no option matched the current argv */
  598. if (arginfo->handle_opt == NULL) {
  599. (void) fprintf(stderr, "qemu: unknown option '%s'\n", r);
  600. exit(EXIT_FAILURE);
  601. }
  602. }
  603. if (optind >= argc) {
  604. (void) fprintf(stderr, "qemu: no user program specified\n");
  605. exit(EXIT_FAILURE);
  606. }
  607. exec_path = argv[optind];
  608. return optind;
  609. }
  610. int main(int argc, char **argv, char **envp)
  611. {
  612. struct target_pt_regs regs1, *regs = &regs1;
  613. struct image_info info1, *info = &info1;
  614. struct linux_binprm bprm;
  615. TaskState *ts;
  616. CPUArchState *env;
  617. CPUState *cpu;
  618. int optind;
  619. char **target_environ, **wrk;
  620. char **target_argv;
  621. int target_argc;
  622. int i;
  623. int ret;
  624. int execfd;
  625. int host_page_size;
  626. unsigned long max_reserved_va;
  627. bool preserve_argv0;
  628. error_init(argv[0]);
  629. module_call_init(MODULE_INIT_TRACE);
  630. qemu_init_cpu_list();
  631. module_call_init(MODULE_INIT_QOM);
  632. envlist = envlist_create();
  633. /*
  634. * add current environment into the list
  635. * envlist_setenv adds to the front of the list; to preserve environ
  636. * order add from back to front
  637. */
  638. for (wrk = environ; *wrk != NULL; wrk++) {
  639. continue;
  640. }
  641. while (wrk != environ) {
  642. wrk--;
  643. (void) envlist_setenv(envlist, *wrk);
  644. }
  645. /* Read the stack limit from the kernel. If it's "unlimited",
  646. then we can do little else besides use the default. */
  647. {
  648. struct rlimit lim;
  649. if (getrlimit(RLIMIT_STACK, &lim) == 0
  650. && lim.rlim_cur != RLIM_INFINITY
  651. && lim.rlim_cur == (target_long)lim.rlim_cur
  652. && lim.rlim_cur > guest_stack_size) {
  653. guest_stack_size = lim.rlim_cur;
  654. }
  655. }
  656. cpu_model = NULL;
  657. qemu_add_opts(&qemu_trace_opts);
  658. qemu_plugin_add_opts();
  659. optind = parse_args(argc, argv);
  660. qemu_set_log_filename_flags(last_log_filename,
  661. last_log_mask | (enable_strace * LOG_STRACE),
  662. &error_fatal);
  663. if (!trace_init_backends()) {
  664. exit(1);
  665. }
  666. trace_init_file();
  667. qemu_plugin_load_list(&plugins, &error_fatal);
  668. /* Zero out regs */
  669. memset(regs, 0, sizeof(struct target_pt_regs));
  670. /* Zero out image_info */
  671. memset(info, 0, sizeof(struct image_info));
  672. memset(&bprm, 0, sizeof (bprm));
  673. /* Scan interp_prefix dir for replacement files. */
  674. init_paths(interp_prefix);
  675. init_qemu_uname_release();
  676. /*
  677. * Manage binfmt-misc open-binary flag
  678. */
  679. errno = 0;
  680. execfd = qemu_getauxval(AT_EXECFD);
  681. if (errno != 0) {
  682. execfd = open(exec_path, O_RDONLY);
  683. if (execfd < 0) {
  684. printf("Error while loading %s: %s\n", exec_path, strerror(errno));
  685. _exit(EXIT_FAILURE);
  686. }
  687. }
  688. /* Resolve executable file name to full path name */
  689. if (realpath(exec_path, real_exec_path)) {
  690. exec_path = real_exec_path;
  691. }
  692. /*
  693. * get binfmt_misc flags
  694. */
  695. preserve_argv0 = !!(qemu_getauxval(AT_FLAGS) & AT_FLAGS_PRESERVE_ARGV0);
  696. /*
  697. * Manage binfmt-misc preserve-arg[0] flag
  698. * argv[optind] full path to the binary
  699. * argv[optind + 1] original argv[0]
  700. */
  701. if (optind + 1 < argc && preserve_argv0) {
  702. optind++;
  703. }
  704. if (cpu_model == NULL) {
  705. cpu_model = cpu_get_model(get_elf_eflags(execfd));
  706. }
  707. cpu_type = parse_cpu_option(cpu_model);
  708. /* init tcg before creating CPUs */
  709. {
  710. AccelState *accel = current_accel();
  711. AccelClass *ac = ACCEL_GET_CLASS(accel);
  712. accel_init_interfaces(ac);
  713. object_property_set_bool(OBJECT(accel), "one-insn-per-tb",
  714. opt_one_insn_per_tb, &error_abort);
  715. object_property_set_int(OBJECT(accel), "tb-size",
  716. opt_tb_size, &error_abort);
  717. ac->init_machine(NULL);
  718. }
  719. /*
  720. * Finalize page size before creating CPUs.
  721. * This will do nothing if !TARGET_PAGE_BITS_VARY.
  722. * The most efficient setting is to match the host.
  723. */
  724. host_page_size = qemu_real_host_page_size();
  725. set_preferred_target_page_bits(ctz32(host_page_size));
  726. finalize_target_page_bits();
  727. cpu = cpu_create(cpu_type);
  728. env = cpu_env(cpu);
  729. cpu_reset(cpu);
  730. thread_cpu = cpu;
  731. /*
  732. * Reserving too much vm space via mmap can run into problems with rlimits,
  733. * oom due to page table creation, etc. We will still try it, if directed
  734. * by the command-line option, but not by default. Unless we're running a
  735. * target address space of 32 or fewer bits on a host with 64 bits.
  736. */
  737. max_reserved_va = MAX_RESERVED_VA(cpu);
  738. if (reserved_va != 0) {
  739. if ((reserved_va + 1) % host_page_size) {
  740. char *s = size_to_str(host_page_size);
  741. fprintf(stderr, "Reserved virtual address not aligned mod %s\n", s);
  742. g_free(s);
  743. exit(EXIT_FAILURE);
  744. }
  745. if (max_reserved_va && reserved_va > max_reserved_va) {
  746. fprintf(stderr, "Reserved virtual address too big\n");
  747. exit(EXIT_FAILURE);
  748. }
  749. } else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) {
  750. /* MAX_RESERVED_VA + 1 is a large power of 2, so is aligned. */
  751. reserved_va = max_reserved_va;
  752. }
  753. /*
  754. * Temporarily disable
  755. * "comparison is always false due to limited range of data type"
  756. * due to comparison between (possible) uint64_t and uintptr_t.
  757. */
  758. #pragma GCC diagnostic push
  759. #pragma GCC diagnostic ignored "-Wtype-limits"
  760. #pragma GCC diagnostic ignored "-Wtautological-compare"
  761. /*
  762. * Select an initial value for task_unmapped_base that is in range.
  763. */
  764. if (reserved_va) {
  765. if (TASK_UNMAPPED_BASE < reserved_va) {
  766. task_unmapped_base = TASK_UNMAPPED_BASE;
  767. } else {
  768. /* The most common default formula is TASK_SIZE / 3. */
  769. task_unmapped_base = TARGET_PAGE_ALIGN(reserved_va / 3);
  770. }
  771. } else if (TASK_UNMAPPED_BASE < UINTPTR_MAX) {
  772. task_unmapped_base = TASK_UNMAPPED_BASE;
  773. } else {
  774. /* 32-bit host: pick something medium size. */
  775. task_unmapped_base = 0x10000000;
  776. }
  777. mmap_next_start = task_unmapped_base;
  778. /* Similarly for elf_et_dyn_base. */
  779. if (reserved_va) {
  780. if (ELF_ET_DYN_BASE < reserved_va) {
  781. elf_et_dyn_base = ELF_ET_DYN_BASE;
  782. } else {
  783. /* The most common default formula is TASK_SIZE / 3 * 2. */
  784. elf_et_dyn_base = TARGET_PAGE_ALIGN(reserved_va / 3) * 2;
  785. }
  786. } else if (ELF_ET_DYN_BASE < UINTPTR_MAX) {
  787. elf_et_dyn_base = ELF_ET_DYN_BASE;
  788. } else {
  789. /* 32-bit host: pick something medium size. */
  790. elf_et_dyn_base = 0x18000000;
  791. }
  792. #pragma GCC diagnostic pop
  793. {
  794. Error *err = NULL;
  795. if (seed_optarg != NULL) {
  796. qemu_guest_random_seed_main(seed_optarg, &err);
  797. } else {
  798. qcrypto_init(&err);
  799. }
  800. if (err) {
  801. error_reportf_err(err, "cannot initialize crypto: ");
  802. exit(1);
  803. }
  804. }
  805. target_environ = envlist_to_environ(envlist, NULL);
  806. envlist_free(envlist);
  807. /*
  808. * Read in mmap_min_addr kernel parameter. This value is used
  809. * When loading the ELF image to determine whether guest_base
  810. * is needed. It is also used in mmap_find_vma.
  811. */
  812. {
  813. FILE *fp;
  814. if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
  815. unsigned long tmp;
  816. if (fscanf(fp, "%lu", &tmp) == 1 && tmp != 0) {
  817. mmap_min_addr = MAX(tmp, host_page_size);
  818. qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n",
  819. mmap_min_addr);
  820. }
  821. fclose(fp);
  822. }
  823. }
  824. /*
  825. * We prefer to not make NULL pointers accessible to QEMU.
  826. * If we're in a chroot with no /proc, fall back to 1 page.
  827. */
  828. if (mmap_min_addr == 0) {
  829. mmap_min_addr = host_page_size;
  830. qemu_log_mask(CPU_LOG_PAGE,
  831. "host mmap_min_addr=0x%lx (fallback)\n",
  832. mmap_min_addr);
  833. }
  834. /*
  835. * Prepare copy of argv vector for target.
  836. */
  837. target_argc = argc - optind;
  838. target_argv = g_new0(char *, target_argc + 1);
  839. /*
  840. * If argv0 is specified (using '-0' switch) we replace
  841. * argv[0] pointer with the given one.
  842. */
  843. i = 0;
  844. if (argv0 != NULL) {
  845. target_argv[i++] = strdup(argv0);
  846. }
  847. for (; i < target_argc; i++) {
  848. target_argv[i] = strdup(argv[optind + i]);
  849. }
  850. target_argv[target_argc] = NULL;
  851. ts = g_new0(TaskState, 1);
  852. init_task_state(ts);
  853. /* build Task State */
  854. ts->info = info;
  855. ts->bprm = &bprm;
  856. cpu->opaque = ts;
  857. task_settid(ts);
  858. fd_trans_init();
  859. ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
  860. info, &bprm);
  861. if (ret != 0) {
  862. printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
  863. _exit(EXIT_FAILURE);
  864. }
  865. for (wrk = target_environ; *wrk; wrk++) {
  866. g_free(*wrk);
  867. }
  868. g_free(target_environ);
  869. if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
  870. FILE *f = qemu_log_trylock();
  871. if (f) {
  872. fprintf(f, "guest_base %p\n", (void *)guest_base);
  873. fprintf(f, "page layout changed following binary load\n");
  874. page_dump(f);
  875. fprintf(f, "end_code 0x" TARGET_ABI_FMT_lx "\n",
  876. info->end_code);
  877. fprintf(f, "start_code 0x" TARGET_ABI_FMT_lx "\n",
  878. info->start_code);
  879. fprintf(f, "start_data 0x" TARGET_ABI_FMT_lx "\n",
  880. info->start_data);
  881. fprintf(f, "end_data 0x" TARGET_ABI_FMT_lx "\n",
  882. info->end_data);
  883. fprintf(f, "start_stack 0x" TARGET_ABI_FMT_lx "\n",
  884. info->start_stack);
  885. fprintf(f, "brk 0x" TARGET_ABI_FMT_lx "\n",
  886. info->brk);
  887. fprintf(f, "entry 0x" TARGET_ABI_FMT_lx "\n",
  888. info->entry);
  889. fprintf(f, "argv_start 0x" TARGET_ABI_FMT_lx "\n",
  890. info->argv);
  891. fprintf(f, "env_start 0x" TARGET_ABI_FMT_lx "\n",
  892. info->envp);
  893. fprintf(f, "auxv_start 0x" TARGET_ABI_FMT_lx "\n",
  894. info->saved_auxv);
  895. qemu_log_unlock(f);
  896. }
  897. }
  898. target_set_brk(info->brk);
  899. syscall_init();
  900. signal_init(rtsig_map);
  901. /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
  902. generating the prologue until now so that the prologue can take
  903. the real value of GUEST_BASE into account. */
  904. tcg_prologue_init();
  905. target_cpu_copy_regs(env, regs);
  906. if (gdbstub) {
  907. gdbserver_start(gdbstub, &error_fatal);
  908. }
  909. #ifdef CONFIG_SEMIHOSTING
  910. qemu_semihosting_guestfd_init();
  911. #endif
  912. cpu_loop(env);
  913. /* never exits */
  914. return 0;
  915. }