You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4608 lines
135KB

  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/units.h"
  26. #include "qapi/error.h"
  27. #include "qemu-version.h"
  28. #include "qemu/cutils.h"
  29. #include "qemu/help_option.h"
  30. #include "qemu/uuid.h"
  31. #include "sysemu/seccomp.h"
  32. #ifdef CONFIG_SDL
  33. #if defined(__APPLE__) || defined(main)
  34. #include <SDL.h>
  35. int qemu_main(int argc, char **argv, char **envp);
  36. int main(int argc, char **argv)
  37. {
  38. return qemu_main(argc, argv, NULL);
  39. }
  40. #undef main
  41. #define main qemu_main
  42. #endif
  43. #endif /* CONFIG_SDL */
  44. #ifdef CONFIG_COCOA
  45. #undef main
  46. #define main qemu_main
  47. #endif /* CONFIG_COCOA */
  48. #include "qemu/error-report.h"
  49. #include "qemu/sockets.h"
  50. #include "hw/hw.h"
  51. #include "hw/boards.h"
  52. #include "sysemu/accel.h"
  53. #include "hw/usb.h"
  54. #include "hw/isa/isa.h"
  55. #include "hw/scsi/scsi.h"
  56. #include "hw/display/vga.h"
  57. #include "hw/bt.h"
  58. #include "sysemu/watchdog.h"
  59. #include "hw/firmware/smbios.h"
  60. #include "hw/acpi/acpi.h"
  61. #include "hw/xen/xen.h"
  62. #include "hw/qdev.h"
  63. #include "hw/loader.h"
  64. #include "monitor/qdev.h"
  65. #include "sysemu/bt.h"
  66. #include "net/net.h"
  67. #include "net/slirp.h"
  68. #include "monitor/monitor.h"
  69. #include "ui/console.h"
  70. #include "ui/input.h"
  71. #include "sysemu/sysemu.h"
  72. #include "sysemu/numa.h"
  73. #include "exec/gdbstub.h"
  74. #include "qemu/timer.h"
  75. #include "chardev/char.h"
  76. #include "qemu/bitmap.h"
  77. #include "qemu/log.h"
  78. #include "sysemu/blockdev.h"
  79. #include "hw/block/block.h"
  80. #include "migration/misc.h"
  81. #include "migration/snapshot.h"
  82. #include "migration/global_state.h"
  83. #include "sysemu/tpm.h"
  84. #include "sysemu/dma.h"
  85. #include "hw/audio/soundhw.h"
  86. #include "audio/audio.h"
  87. #include "sysemu/cpus.h"
  88. #include "migration/colo.h"
  89. #include "migration/postcopy-ram.h"
  90. #include "sysemu/kvm.h"
  91. #include "sysemu/hax.h"
  92. #include "qapi/qobject-input-visitor.h"
  93. #include "qemu/option.h"
  94. #include "qemu/config-file.h"
  95. #include "qemu-options.h"
  96. #include "qemu/main-loop.h"
  97. #ifdef CONFIG_VIRTFS
  98. #include "fsdev/qemu-fsdev.h"
  99. #endif
  100. #include "sysemu/qtest.h"
  101. #include "disas/disas.h"
  102. #include "slirp/libslirp.h"
  103. #include "trace-root.h"
  104. #include "trace/control.h"
  105. #include "qemu/queue.h"
  106. #include "sysemu/arch_init.h"
  107. #include "ui/qemu-spice.h"
  108. #include "qapi/string-input-visitor.h"
  109. #include "qapi/opts-visitor.h"
  110. #include "qapi/clone-visitor.h"
  111. #include "qom/object_interfaces.h"
  112. #include "exec/semihost.h"
  113. #include "crypto/init.h"
  114. #include "sysemu/replay.h"
  115. #include "qapi/qapi-events-run-state.h"
  116. #include "qapi/qapi-visit-block-core.h"
  117. #include "qapi/qapi-visit-ui.h"
  118. #include "qapi/qapi-commands-block-core.h"
  119. #include "qapi/qapi-commands-misc.h"
  120. #include "qapi/qapi-commands-run-state.h"
  121. #include "qapi/qapi-commands-ui.h"
  122. #include "qapi/qmp/qerror.h"
  123. #include "sysemu/iothread.h"
  124. #define MAX_VIRTIO_CONSOLES 1
  125. static const char *data_dir[16];
  126. static int data_dir_idx;
  127. const char *bios_name = NULL;
  128. enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
  129. int display_opengl;
  130. const char* keyboard_layout = NULL;
  131. ram_addr_t ram_size;
  132. const char *mem_path = NULL;
  133. int mem_prealloc = 0; /* force preallocation of physical target memory */
  134. bool enable_mlock = false;
  135. bool enable_cpu_pm = false;
  136. int nb_nics;
  137. NICInfo nd_table[MAX_NICS];
  138. int autostart;
  139. static enum {
  140. RTC_BASE_UTC,
  141. RTC_BASE_LOCALTIME,
  142. RTC_BASE_DATETIME,
  143. } rtc_base_type = RTC_BASE_UTC;
  144. static time_t rtc_ref_start_datetime;
  145. static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
  146. static int rtc_host_datetime_offset = -1; /* valid & used only with
  147. RTC_BASE_DATETIME */
  148. QEMUClockType rtc_clock;
  149. int vga_interface_type = VGA_NONE;
  150. static DisplayOptions dpy;
  151. static int num_serial_hds;
  152. static Chardev **serial_hds;
  153. Chardev *parallel_hds[MAX_PARALLEL_PORTS];
  154. int win2k_install_hack = 0;
  155. int singlestep = 0;
  156. int smp_cpus;
  157. unsigned int max_cpus;
  158. int smp_cores = 1;
  159. int smp_threads = 1;
  160. int acpi_enabled = 1;
  161. int no_hpet = 0;
  162. int fd_bootchk = 1;
  163. static int no_reboot;
  164. int no_shutdown = 0;
  165. int cursor_hide = 1;
  166. int graphic_rotate = 0;
  167. const char *watchdog;
  168. QEMUOptionRom option_rom[MAX_OPTION_ROMS];
  169. int nb_option_roms;
  170. int old_param = 0;
  171. const char *qemu_name;
  172. int alt_grab = 0;
  173. int ctrl_grab = 0;
  174. unsigned int nb_prom_envs = 0;
  175. const char *prom_envs[MAX_PROM_ENVS];
  176. int boot_menu;
  177. bool boot_strict;
  178. uint8_t *boot_splash_filedata;
  179. size_t boot_splash_filedata_size;
  180. bool wakeup_suspend_enabled;
  181. int icount_align_option;
  182. /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
  183. * little-endian "wire format" described in the SMBIOS 2.6 specification.
  184. */
  185. QemuUUID qemu_uuid;
  186. bool qemu_uuid_set;
  187. static NotifierList exit_notifiers =
  188. NOTIFIER_LIST_INITIALIZER(exit_notifiers);
  189. static NotifierList machine_init_done_notifiers =
  190. NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
  191. bool xen_allowed;
  192. uint32_t xen_domid;
  193. enum xen_mode xen_mode = XEN_EMULATE;
  194. bool xen_domid_restrict;
  195. static int has_defaults = 1;
  196. static int default_serial = 1;
  197. static int default_parallel = 1;
  198. static int default_monitor = 1;
  199. static int default_floppy = 1;
  200. static int default_cdrom = 1;
  201. static int default_sdcard = 1;
  202. static int default_vga = 1;
  203. static int default_net = 1;
  204. static struct {
  205. const char *driver;
  206. int *flag;
  207. } default_list[] = {
  208. { .driver = "isa-serial", .flag = &default_serial },
  209. { .driver = "isa-parallel", .flag = &default_parallel },
  210. { .driver = "isa-fdc", .flag = &default_floppy },
  211. { .driver = "floppy", .flag = &default_floppy },
  212. { .driver = "ide-cd", .flag = &default_cdrom },
  213. { .driver = "ide-hd", .flag = &default_cdrom },
  214. { .driver = "ide-drive", .flag = &default_cdrom },
  215. { .driver = "scsi-cd", .flag = &default_cdrom },
  216. { .driver = "scsi-hd", .flag = &default_cdrom },
  217. { .driver = "VGA", .flag = &default_vga },
  218. { .driver = "isa-vga", .flag = &default_vga },
  219. { .driver = "cirrus-vga", .flag = &default_vga },
  220. { .driver = "isa-cirrus-vga", .flag = &default_vga },
  221. { .driver = "vmware-svga", .flag = &default_vga },
  222. { .driver = "qxl-vga", .flag = &default_vga },
  223. { .driver = "virtio-vga", .flag = &default_vga },
  224. };
  225. static QemuOptsList qemu_rtc_opts = {
  226. .name = "rtc",
  227. .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
  228. .merge_lists = true,
  229. .desc = {
  230. {
  231. .name = "base",
  232. .type = QEMU_OPT_STRING,
  233. },{
  234. .name = "clock",
  235. .type = QEMU_OPT_STRING,
  236. },{
  237. .name = "driftfix",
  238. .type = QEMU_OPT_STRING,
  239. },
  240. { /* end of list */ }
  241. },
  242. };
  243. static QemuOptsList qemu_option_rom_opts = {
  244. .name = "option-rom",
  245. .implied_opt_name = "romfile",
  246. .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
  247. .desc = {
  248. {
  249. .name = "bootindex",
  250. .type = QEMU_OPT_NUMBER,
  251. }, {
  252. .name = "romfile",
  253. .type = QEMU_OPT_STRING,
  254. },
  255. { /* end of list */ }
  256. },
  257. };
  258. static QemuOptsList qemu_machine_opts = {
  259. .name = "machine",
  260. .implied_opt_name = "type",
  261. .merge_lists = true,
  262. .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
  263. .desc = {
  264. /*
  265. * no elements => accept any
  266. * sanity checking will happen later
  267. * when setting machine properties
  268. */
  269. { }
  270. },
  271. };
  272. static QemuOptsList qemu_accel_opts = {
  273. .name = "accel",
  274. .implied_opt_name = "accel",
  275. .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
  276. .merge_lists = true,
  277. .desc = {
  278. {
  279. .name = "accel",
  280. .type = QEMU_OPT_STRING,
  281. .help = "Select the type of accelerator",
  282. },
  283. {
  284. .name = "thread",
  285. .type = QEMU_OPT_STRING,
  286. .help = "Enable/disable multi-threaded TCG",
  287. },
  288. { /* end of list */ }
  289. },
  290. };
  291. static QemuOptsList qemu_boot_opts = {
  292. .name = "boot-opts",
  293. .implied_opt_name = "order",
  294. .merge_lists = true,
  295. .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
  296. .desc = {
  297. {
  298. .name = "order",
  299. .type = QEMU_OPT_STRING,
  300. }, {
  301. .name = "once",
  302. .type = QEMU_OPT_STRING,
  303. }, {
  304. .name = "menu",
  305. .type = QEMU_OPT_BOOL,
  306. }, {
  307. .name = "splash",
  308. .type = QEMU_OPT_STRING,
  309. }, {
  310. .name = "splash-time",
  311. .type = QEMU_OPT_NUMBER,
  312. }, {
  313. .name = "reboot-timeout",
  314. .type = QEMU_OPT_NUMBER,
  315. }, {
  316. .name = "strict",
  317. .type = QEMU_OPT_BOOL,
  318. },
  319. { /*End of list */ }
  320. },
  321. };
  322. static QemuOptsList qemu_add_fd_opts = {
  323. .name = "add-fd",
  324. .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
  325. .desc = {
  326. {
  327. .name = "fd",
  328. .type = QEMU_OPT_NUMBER,
  329. .help = "file descriptor of which a duplicate is added to fd set",
  330. },{
  331. .name = "set",
  332. .type = QEMU_OPT_NUMBER,
  333. .help = "ID of the fd set to add fd to",
  334. },{
  335. .name = "opaque",
  336. .type = QEMU_OPT_STRING,
  337. .help = "free-form string used to describe fd",
  338. },
  339. { /* end of list */ }
  340. },
  341. };
  342. static QemuOptsList qemu_object_opts = {
  343. .name = "object",
  344. .implied_opt_name = "qom-type",
  345. .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
  346. .desc = {
  347. { }
  348. },
  349. };
  350. static QemuOptsList qemu_tpmdev_opts = {
  351. .name = "tpmdev",
  352. .implied_opt_name = "type",
  353. .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
  354. .desc = {
  355. /* options are defined in the TPM backends */
  356. { /* end of list */ }
  357. },
  358. };
  359. static QemuOptsList qemu_realtime_opts = {
  360. .name = "realtime",
  361. .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
  362. .desc = {
  363. {
  364. .name = "mlock",
  365. .type = QEMU_OPT_BOOL,
  366. },
  367. { /* end of list */ }
  368. },
  369. };
  370. static QemuOptsList qemu_overcommit_opts = {
  371. .name = "overcommit",
  372. .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
  373. .desc = {
  374. {
  375. .name = "mem-lock",
  376. .type = QEMU_OPT_BOOL,
  377. },
  378. {
  379. .name = "cpu-pm",
  380. .type = QEMU_OPT_BOOL,
  381. },
  382. { /* end of list */ }
  383. },
  384. };
  385. static QemuOptsList qemu_msg_opts = {
  386. .name = "msg",
  387. .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
  388. .desc = {
  389. {
  390. .name = "timestamp",
  391. .type = QEMU_OPT_BOOL,
  392. },
  393. { /* end of list */ }
  394. },
  395. };
  396. static QemuOptsList qemu_name_opts = {
  397. .name = "name",
  398. .implied_opt_name = "guest",
  399. .merge_lists = true,
  400. .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
  401. .desc = {
  402. {
  403. .name = "guest",
  404. .type = QEMU_OPT_STRING,
  405. .help = "Sets the name of the guest.\n"
  406. "This name will be displayed in the SDL window caption.\n"
  407. "The name will also be used for the VNC server",
  408. }, {
  409. .name = "process",
  410. .type = QEMU_OPT_STRING,
  411. .help = "Sets the name of the QEMU process, as shown in top etc",
  412. }, {
  413. .name = "debug-threads",
  414. .type = QEMU_OPT_BOOL,
  415. .help = "When enabled, name the individual threads; defaults off.\n"
  416. "NOTE: The thread names are for debugging and not a\n"
  417. "stable API.",
  418. },
  419. { /* End of list */ }
  420. },
  421. };
  422. static QemuOptsList qemu_mem_opts = {
  423. .name = "memory",
  424. .implied_opt_name = "size",
  425. .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
  426. .merge_lists = true,
  427. .desc = {
  428. {
  429. .name = "size",
  430. .type = QEMU_OPT_SIZE,
  431. },
  432. {
  433. .name = "slots",
  434. .type = QEMU_OPT_NUMBER,
  435. },
  436. {
  437. .name = "maxmem",
  438. .type = QEMU_OPT_SIZE,
  439. },
  440. { /* end of list */ }
  441. },
  442. };
  443. static QemuOptsList qemu_icount_opts = {
  444. .name = "icount",
  445. .implied_opt_name = "shift",
  446. .merge_lists = true,
  447. .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
  448. .desc = {
  449. {
  450. .name = "shift",
  451. .type = QEMU_OPT_STRING,
  452. }, {
  453. .name = "align",
  454. .type = QEMU_OPT_BOOL,
  455. }, {
  456. .name = "sleep",
  457. .type = QEMU_OPT_BOOL,
  458. }, {
  459. .name = "rr",
  460. .type = QEMU_OPT_STRING,
  461. }, {
  462. .name = "rrfile",
  463. .type = QEMU_OPT_STRING,
  464. }, {
  465. .name = "rrsnapshot",
  466. .type = QEMU_OPT_STRING,
  467. },
  468. { /* end of list */ }
  469. },
  470. };
  471. static QemuOptsList qemu_semihosting_config_opts = {
  472. .name = "semihosting-config",
  473. .implied_opt_name = "enable",
  474. .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
  475. .desc = {
  476. {
  477. .name = "enable",
  478. .type = QEMU_OPT_BOOL,
  479. }, {
  480. .name = "target",
  481. .type = QEMU_OPT_STRING,
  482. }, {
  483. .name = "arg",
  484. .type = QEMU_OPT_STRING,
  485. },
  486. { /* end of list */ }
  487. },
  488. };
  489. static QemuOptsList qemu_fw_cfg_opts = {
  490. .name = "fw_cfg",
  491. .implied_opt_name = "name",
  492. .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
  493. .desc = {
  494. {
  495. .name = "name",
  496. .type = QEMU_OPT_STRING,
  497. .help = "Sets the fw_cfg name of the blob to be inserted",
  498. }, {
  499. .name = "file",
  500. .type = QEMU_OPT_STRING,
  501. .help = "Sets the name of the file from which "
  502. "the fw_cfg blob will be loaded",
  503. }, {
  504. .name = "string",
  505. .type = QEMU_OPT_STRING,
  506. .help = "Sets content of the blob to be inserted from a string",
  507. },
  508. { /* end of list */ }
  509. },
  510. };
  511. /**
  512. * Get machine options
  513. *
  514. * Returns: machine options (never null).
  515. */
  516. QemuOpts *qemu_get_machine_opts(void)
  517. {
  518. return qemu_find_opts_singleton("machine");
  519. }
  520. const char *qemu_get_vm_name(void)
  521. {
  522. return qemu_name;
  523. }
  524. static void res_free(void)
  525. {
  526. g_free(boot_splash_filedata);
  527. boot_splash_filedata = NULL;
  528. }
  529. static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
  530. {
  531. const char *driver = qemu_opt_get(opts, "driver");
  532. int i;
  533. if (!driver)
  534. return 0;
  535. for (i = 0; i < ARRAY_SIZE(default_list); i++) {
  536. if (strcmp(default_list[i].driver, driver) != 0)
  537. continue;
  538. *(default_list[i].flag) = 0;
  539. }
  540. return 0;
  541. }
  542. /***********************************************************/
  543. /* QEMU state */
  544. static RunState current_run_state = RUN_STATE_PRECONFIG;
  545. /* We use RUN_STATE__MAX but any invalid value will do */
  546. static RunState vmstop_requested = RUN_STATE__MAX;
  547. static QemuMutex vmstop_lock;
  548. typedef struct {
  549. RunState from;
  550. RunState to;
  551. } RunStateTransition;
  552. static const RunStateTransition runstate_transitions_def[] = {
  553. /* from -> to */
  554. { RUN_STATE_PRECONFIG, RUN_STATE_PRELAUNCH },
  555. /* Early switch to inmigrate state to allow -incoming CLI option work
  556. * as it used to. TODO: delay actual switching to inmigrate state to
  557. * the point after machine is built and remove this hack.
  558. */
  559. { RUN_STATE_PRECONFIG, RUN_STATE_INMIGRATE },
  560. { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
  561. { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
  562. { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
  563. { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
  564. { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
  565. { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
  566. { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
  567. { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
  568. { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
  569. { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
  570. { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
  571. { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
  572. { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
  573. { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
  574. { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
  575. { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
  576. { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
  577. { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
  578. { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
  579. { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
  580. { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
  581. { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
  582. { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
  583. { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
  584. { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
  585. { RUN_STATE_PAUSED, RUN_STATE_COLO},
  586. { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
  587. { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
  588. { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
  589. { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
  590. { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
  591. { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
  592. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
  593. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
  594. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
  595. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
  596. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
  597. { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
  598. { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
  599. { RUN_STATE_COLO, RUN_STATE_RUNNING },
  600. { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
  601. { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
  602. { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
  603. { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
  604. { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
  605. { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
  606. { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
  607. { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
  608. { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
  609. { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
  610. { RUN_STATE_RUNNING, RUN_STATE_COLO},
  611. { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
  612. { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
  613. { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
  614. { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
  615. { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
  616. { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
  617. { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
  618. { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
  619. { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
  620. { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
  621. { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
  622. { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
  623. { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
  624. { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
  625. { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
  626. { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
  627. { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
  628. { RUN_STATE__MAX, RUN_STATE__MAX },
  629. };
  630. static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
  631. bool runstate_check(RunState state)
  632. {
  633. return current_run_state == state;
  634. }
  635. bool runstate_store(char *str, size_t size)
  636. {
  637. const char *state = RunState_str(current_run_state);
  638. size_t len = strlen(state) + 1;
  639. if (len > size) {
  640. return false;
  641. }
  642. memcpy(str, state, len);
  643. return true;
  644. }
  645. static void runstate_init(void)
  646. {
  647. const RunStateTransition *p;
  648. memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
  649. for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
  650. runstate_valid_transitions[p->from][p->to] = true;
  651. }
  652. qemu_mutex_init(&vmstop_lock);
  653. }
  654. /* This function will abort() on invalid state transitions */
  655. void runstate_set(RunState new_state)
  656. {
  657. assert(new_state < RUN_STATE__MAX);
  658. trace_runstate_set(current_run_state, RunState_str(current_run_state),
  659. new_state, RunState_str(current_run_state));
  660. if (current_run_state == new_state) {
  661. return;
  662. }
  663. if (!runstate_valid_transitions[current_run_state][new_state]) {
  664. error_report("invalid runstate transition: '%s' -> '%s'",
  665. RunState_str(current_run_state),
  666. RunState_str(new_state));
  667. abort();
  668. }
  669. current_run_state = new_state;
  670. }
  671. int runstate_is_running(void)
  672. {
  673. return runstate_check(RUN_STATE_RUNNING);
  674. }
  675. bool runstate_needs_reset(void)
  676. {
  677. return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
  678. runstate_check(RUN_STATE_SHUTDOWN);
  679. }
  680. StatusInfo *qmp_query_status(Error **errp)
  681. {
  682. StatusInfo *info = g_malloc0(sizeof(*info));
  683. info->running = runstate_is_running();
  684. info->singlestep = singlestep;
  685. info->status = current_run_state;
  686. return info;
  687. }
  688. bool qemu_vmstop_requested(RunState *r)
  689. {
  690. qemu_mutex_lock(&vmstop_lock);
  691. *r = vmstop_requested;
  692. vmstop_requested = RUN_STATE__MAX;
  693. qemu_mutex_unlock(&vmstop_lock);
  694. return *r < RUN_STATE__MAX;
  695. }
  696. void qemu_system_vmstop_request_prepare(void)
  697. {
  698. qemu_mutex_lock(&vmstop_lock);
  699. }
  700. void qemu_system_vmstop_request(RunState state)
  701. {
  702. vmstop_requested = state;
  703. qemu_mutex_unlock(&vmstop_lock);
  704. qemu_notify_event();
  705. }
  706. /***********************************************************/
  707. /* RTC reference time/date access */
  708. static time_t qemu_ref_timedate(QEMUClockType clock)
  709. {
  710. time_t value = qemu_clock_get_ms(clock) / 1000;
  711. switch (clock) {
  712. case QEMU_CLOCK_REALTIME:
  713. value -= rtc_realtime_clock_offset;
  714. /* no break */
  715. case QEMU_CLOCK_VIRTUAL:
  716. value += rtc_ref_start_datetime;
  717. break;
  718. case QEMU_CLOCK_HOST:
  719. if (rtc_base_type == RTC_BASE_DATETIME) {
  720. value -= rtc_host_datetime_offset;
  721. }
  722. break;
  723. default:
  724. assert(0);
  725. }
  726. return value;
  727. }
  728. void qemu_get_timedate(struct tm *tm, int offset)
  729. {
  730. time_t ti = qemu_ref_timedate(rtc_clock);
  731. ti += offset;
  732. switch (rtc_base_type) {
  733. case RTC_BASE_DATETIME:
  734. case RTC_BASE_UTC:
  735. gmtime_r(&ti, tm);
  736. break;
  737. case RTC_BASE_LOCALTIME:
  738. localtime_r(&ti, tm);
  739. break;
  740. }
  741. }
  742. int qemu_timedate_diff(struct tm *tm)
  743. {
  744. time_t seconds;
  745. switch (rtc_base_type) {
  746. case RTC_BASE_DATETIME:
  747. case RTC_BASE_UTC:
  748. seconds = mktimegm(tm);
  749. break;
  750. case RTC_BASE_LOCALTIME:
  751. {
  752. struct tm tmp = *tm;
  753. tmp.tm_isdst = -1; /* use timezone to figure it out */
  754. seconds = mktime(&tmp);
  755. break;
  756. }
  757. default:
  758. abort();
  759. }
  760. return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
  761. }
  762. static void configure_rtc_base_datetime(const char *startdate)
  763. {
  764. time_t rtc_start_datetime;
  765. struct tm tm;
  766. if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
  767. &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
  768. /* OK */
  769. } else if (sscanf(startdate, "%d-%d-%d",
  770. &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
  771. tm.tm_hour = 0;
  772. tm.tm_min = 0;
  773. tm.tm_sec = 0;
  774. } else {
  775. goto date_fail;
  776. }
  777. tm.tm_year -= 1900;
  778. tm.tm_mon--;
  779. rtc_start_datetime = mktimegm(&tm);
  780. if (rtc_start_datetime == -1) {
  781. date_fail:
  782. error_report("invalid datetime format");
  783. error_printf("valid formats: "
  784. "'2006-06-17T16:01:21' or '2006-06-17'\n");
  785. exit(1);
  786. }
  787. rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
  788. rtc_ref_start_datetime = rtc_start_datetime;
  789. }
  790. static void configure_rtc(QemuOpts *opts)
  791. {
  792. const char *value;
  793. /* Set defaults */
  794. rtc_clock = QEMU_CLOCK_HOST;
  795. rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
  796. rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
  797. value = qemu_opt_get(opts, "base");
  798. if (value) {
  799. if (!strcmp(value, "utc")) {
  800. rtc_base_type = RTC_BASE_UTC;
  801. } else if (!strcmp(value, "localtime")) {
  802. Error *blocker = NULL;
  803. rtc_base_type = RTC_BASE_LOCALTIME;
  804. error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
  805. "-rtc base=localtime");
  806. replay_add_blocker(blocker);
  807. } else {
  808. rtc_base_type = RTC_BASE_DATETIME;
  809. configure_rtc_base_datetime(value);
  810. }
  811. }
  812. value = qemu_opt_get(opts, "clock");
  813. if (value) {
  814. if (!strcmp(value, "host")) {
  815. rtc_clock = QEMU_CLOCK_HOST;
  816. } else if (!strcmp(value, "rt")) {
  817. rtc_clock = QEMU_CLOCK_REALTIME;
  818. } else if (!strcmp(value, "vm")) {
  819. rtc_clock = QEMU_CLOCK_VIRTUAL;
  820. } else {
  821. error_report("invalid option value '%s'", value);
  822. exit(1);
  823. }
  824. }
  825. value = qemu_opt_get(opts, "driftfix");
  826. if (value) {
  827. if (!strcmp(value, "slew")) {
  828. static GlobalProperty slew_lost_ticks = {
  829. .driver = "mc146818rtc",
  830. .property = "lost_tick_policy",
  831. .value = "slew",
  832. };
  833. qdev_prop_register_global(&slew_lost_ticks);
  834. } else if (!strcmp(value, "none")) {
  835. /* discard is default */
  836. } else {
  837. error_report("invalid option value '%s'", value);
  838. exit(1);
  839. }
  840. }
  841. }
  842. /***********************************************************/
  843. /* Bluetooth support */
  844. static int nb_hcis;
  845. static int cur_hci;
  846. static struct HCIInfo *hci_table[MAX_NICS];
  847. struct HCIInfo *qemu_next_hci(void)
  848. {
  849. if (cur_hci == nb_hcis)
  850. return &null_hci;
  851. return hci_table[cur_hci++];
  852. }
  853. static int bt_hci_parse(const char *str)
  854. {
  855. struct HCIInfo *hci;
  856. bdaddr_t bdaddr;
  857. if (nb_hcis >= MAX_NICS) {
  858. error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
  859. return -1;
  860. }
  861. hci = hci_init(str);
  862. if (!hci)
  863. return -1;
  864. bdaddr.b[0] = 0x52;
  865. bdaddr.b[1] = 0x54;
  866. bdaddr.b[2] = 0x00;
  867. bdaddr.b[3] = 0x12;
  868. bdaddr.b[4] = 0x34;
  869. bdaddr.b[5] = 0x56 + nb_hcis;
  870. hci->bdaddr_set(hci, bdaddr.b);
  871. hci_table[nb_hcis++] = hci;
  872. return 0;
  873. }
  874. static void bt_vhci_add(int vlan_id)
  875. {
  876. struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
  877. if (!vlan->slave)
  878. warn_report("adding a VHCI to an empty scatternet %i",
  879. vlan_id);
  880. bt_vhci_init(bt_new_hci(vlan));
  881. }
  882. static struct bt_device_s *bt_device_add(const char *opt)
  883. {
  884. struct bt_scatternet_s *vlan;
  885. int vlan_id = 0;
  886. char *endp = strstr(opt, ",vlan=");
  887. int len = (endp ? endp - opt : strlen(opt)) + 1;
  888. char devname[10];
  889. pstrcpy(devname, MIN(sizeof(devname), len), opt);
  890. if (endp) {
  891. vlan_id = strtol(endp + 6, &endp, 0);
  892. if (*endp) {
  893. error_report("unrecognised bluetooth vlan Id");
  894. return 0;
  895. }
  896. }
  897. vlan = qemu_find_bt_vlan(vlan_id);
  898. if (!vlan->slave)
  899. warn_report("adding a slave device to an empty scatternet %i",
  900. vlan_id);
  901. if (!strcmp(devname, "keyboard"))
  902. return bt_keyboard_init(vlan);
  903. error_report("unsupported bluetooth device '%s'", devname);
  904. return 0;
  905. }
  906. static int bt_parse(const char *opt)
  907. {
  908. const char *endp, *p;
  909. int vlan;
  910. if (strstart(opt, "hci", &endp)) {
  911. if (!*endp || *endp == ',') {
  912. if (*endp)
  913. if (!strstart(endp, ",vlan=", 0))
  914. opt = endp + 1;
  915. return bt_hci_parse(opt);
  916. }
  917. } else if (strstart(opt, "vhci", &endp)) {
  918. if (!*endp || *endp == ',') {
  919. if (*endp) {
  920. if (strstart(endp, ",vlan=", &p)) {
  921. vlan = strtol(p, (char **) &endp, 0);
  922. if (*endp) {
  923. error_report("bad scatternet '%s'", p);
  924. return 1;
  925. }
  926. } else {
  927. error_report("bad parameter '%s'", endp + 1);
  928. return 1;
  929. }
  930. } else
  931. vlan = 0;
  932. bt_vhci_add(vlan);
  933. return 0;
  934. }
  935. } else if (strstart(opt, "device:", &endp))
  936. return !bt_device_add(endp);
  937. error_report("bad bluetooth parameter '%s'", opt);
  938. return 1;
  939. }
  940. static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
  941. {
  942. const char *proc_name;
  943. if (qemu_opt_get(opts, "debug-threads")) {
  944. qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
  945. }
  946. qemu_name = qemu_opt_get(opts, "guest");
  947. proc_name = qemu_opt_get(opts, "process");
  948. if (proc_name) {
  949. os_set_proc_name(proc_name);
  950. }
  951. return 0;
  952. }
  953. bool defaults_enabled(void)
  954. {
  955. return has_defaults;
  956. }
  957. #ifndef _WIN32
  958. static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
  959. {
  960. int fd, dupfd, flags;
  961. int64_t fdset_id;
  962. const char *fd_opaque = NULL;
  963. AddfdInfo *fdinfo;
  964. fd = qemu_opt_get_number(opts, "fd", -1);
  965. fdset_id = qemu_opt_get_number(opts, "set", -1);
  966. fd_opaque = qemu_opt_get(opts, "opaque");
  967. if (fd < 0) {
  968. error_setg(errp, "fd option is required and must be non-negative");
  969. return -1;
  970. }
  971. if (fd <= STDERR_FILENO) {
  972. error_setg(errp, "fd cannot be a standard I/O stream");
  973. return -1;
  974. }
  975. /*
  976. * All fds inherited across exec() necessarily have FD_CLOEXEC
  977. * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
  978. */
  979. flags = fcntl(fd, F_GETFD);
  980. if (flags == -1 || (flags & FD_CLOEXEC)) {
  981. error_setg(errp, "fd is not valid or already in use");
  982. return -1;
  983. }
  984. if (fdset_id < 0) {
  985. error_setg(errp, "set option is required and must be non-negative");
  986. return -1;
  987. }
  988. #ifdef F_DUPFD_CLOEXEC
  989. dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
  990. #else
  991. dupfd = dup(fd);
  992. if (dupfd != -1) {
  993. qemu_set_cloexec(dupfd);
  994. }
  995. #endif
  996. if (dupfd == -1) {
  997. error_setg(errp, "error duplicating fd: %s", strerror(errno));
  998. return -1;
  999. }
  1000. /* add the duplicate fd, and optionally the opaque string, to the fd set */
  1001. fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
  1002. &error_abort);
  1003. g_free(fdinfo);
  1004. return 0;
  1005. }
  1006. static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
  1007. {
  1008. int fd;
  1009. fd = qemu_opt_get_number(opts, "fd", -1);
  1010. close(fd);
  1011. return 0;
  1012. }
  1013. #endif
  1014. /***********************************************************/
  1015. /* QEMU Block devices */
  1016. #define HD_OPTS "media=disk"
  1017. #define CDROM_OPTS "media=cdrom"
  1018. #define FD_OPTS ""
  1019. #define PFLASH_OPTS ""
  1020. #define MTD_OPTS ""
  1021. #define SD_OPTS ""
  1022. static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
  1023. {
  1024. BlockInterfaceType *block_default_type = opaque;
  1025. return drive_new(opts, *block_default_type, errp) == NULL;
  1026. }
  1027. static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
  1028. {
  1029. if (qemu_opt_get(opts, "snapshot") == NULL) {
  1030. qemu_opt_set(opts, "snapshot", "on", &error_abort);
  1031. }
  1032. return 0;
  1033. }
  1034. static void default_drive(int enable, int snapshot, BlockInterfaceType type,
  1035. int index, const char *optstr)
  1036. {
  1037. QemuOpts *opts;
  1038. DriveInfo *dinfo;
  1039. if (!enable || drive_get_by_index(type, index)) {
  1040. return;
  1041. }
  1042. opts = drive_add(type, index, NULL, optstr);
  1043. if (snapshot) {
  1044. drive_enable_snapshot(NULL, opts, NULL);
  1045. }
  1046. dinfo = drive_new(opts, type, &error_abort);
  1047. dinfo->is_default = true;
  1048. }
  1049. static QemuOptsList qemu_smp_opts = {
  1050. .name = "smp-opts",
  1051. .implied_opt_name = "cpus",
  1052. .merge_lists = true,
  1053. .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
  1054. .desc = {
  1055. {
  1056. .name = "cpus",
  1057. .type = QEMU_OPT_NUMBER,
  1058. }, {
  1059. .name = "sockets",
  1060. .type = QEMU_OPT_NUMBER,
  1061. }, {
  1062. .name = "cores",
  1063. .type = QEMU_OPT_NUMBER,
  1064. }, {
  1065. .name = "threads",
  1066. .type = QEMU_OPT_NUMBER,
  1067. }, {
  1068. .name = "maxcpus",
  1069. .type = QEMU_OPT_NUMBER,
  1070. },
  1071. { /*End of list */ }
  1072. },
  1073. };
  1074. static void smp_parse(QemuOpts *opts)
  1075. {
  1076. if (opts) {
  1077. unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
  1078. unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
  1079. unsigned cores = qemu_opt_get_number(opts, "cores", 0);
  1080. unsigned threads = qemu_opt_get_number(opts, "threads", 0);
  1081. /* compute missing values, prefer sockets over cores over threads */
  1082. if (cpus == 0 || sockets == 0) {
  1083. cores = cores > 0 ? cores : 1;
  1084. threads = threads > 0 ? threads : 1;
  1085. if (cpus == 0) {
  1086. sockets = sockets > 0 ? sockets : 1;
  1087. cpus = cores * threads * sockets;
  1088. } else {
  1089. max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
  1090. sockets = max_cpus / (cores * threads);
  1091. }
  1092. } else if (cores == 0) {
  1093. threads = threads > 0 ? threads : 1;
  1094. cores = cpus / (sockets * threads);
  1095. cores = cores > 0 ? cores : 1;
  1096. } else if (threads == 0) {
  1097. threads = cpus / (cores * sockets);
  1098. threads = threads > 0 ? threads : 1;
  1099. } else if (sockets * cores * threads < cpus) {
  1100. error_report("cpu topology: "
  1101. "sockets (%u) * cores (%u) * threads (%u) < "
  1102. "smp_cpus (%u)",
  1103. sockets, cores, threads, cpus);
  1104. exit(1);
  1105. }
  1106. max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
  1107. if (max_cpus < cpus) {
  1108. error_report("maxcpus must be equal to or greater than smp");
  1109. exit(1);
  1110. }
  1111. if (sockets * cores * threads > max_cpus) {
  1112. error_report("cpu topology: "
  1113. "sockets (%u) * cores (%u) * threads (%u) > "
  1114. "maxcpus (%u)",
  1115. sockets, cores, threads, max_cpus);
  1116. exit(1);
  1117. }
  1118. if (sockets * cores * threads != max_cpus) {
  1119. warn_report("Invalid CPU topology deprecated: "
  1120. "sockets (%u) * cores (%u) * threads (%u) "
  1121. "!= maxcpus (%u)",
  1122. sockets, cores, threads, max_cpus);
  1123. }
  1124. smp_cpus = cpus;
  1125. smp_cores = cores;
  1126. smp_threads = threads;
  1127. }
  1128. if (smp_cpus > 1) {
  1129. Error *blocker = NULL;
  1130. error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
  1131. replay_add_blocker(blocker);
  1132. }
  1133. }
  1134. static void realtime_init(void)
  1135. {
  1136. if (enable_mlock) {
  1137. if (os_mlock() < 0) {
  1138. error_report("locking memory failed");
  1139. exit(1);
  1140. }
  1141. }
  1142. }
  1143. static void configure_msg(QemuOpts *opts)
  1144. {
  1145. enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
  1146. }
  1147. /***********************************************************/
  1148. /* Semihosting */
  1149. typedef struct SemihostingConfig {
  1150. bool enabled;
  1151. SemihostingTarget target;
  1152. const char **argv;
  1153. int argc;
  1154. const char *cmdline; /* concatenated argv */
  1155. } SemihostingConfig;
  1156. static SemihostingConfig semihosting;
  1157. bool semihosting_enabled(void)
  1158. {
  1159. return semihosting.enabled;
  1160. }
  1161. SemihostingTarget semihosting_get_target(void)
  1162. {
  1163. return semihosting.target;
  1164. }
  1165. const char *semihosting_get_arg(int i)
  1166. {
  1167. if (i >= semihosting.argc) {
  1168. return NULL;
  1169. }
  1170. return semihosting.argv[i];
  1171. }
  1172. int semihosting_get_argc(void)
  1173. {
  1174. return semihosting.argc;
  1175. }
  1176. const char *semihosting_get_cmdline(void)
  1177. {
  1178. if (semihosting.cmdline == NULL && semihosting.argc > 0) {
  1179. semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
  1180. }
  1181. return semihosting.cmdline;
  1182. }
  1183. static int add_semihosting_arg(void *opaque,
  1184. const char *name, const char *val,
  1185. Error **errp)
  1186. {
  1187. SemihostingConfig *s = opaque;
  1188. if (strcmp(name, "arg") == 0) {
  1189. s->argc++;
  1190. /* one extra element as g_strjoinv() expects NULL-terminated array */
  1191. s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
  1192. s->argv[s->argc - 1] = val;
  1193. s->argv[s->argc] = NULL;
  1194. }
  1195. return 0;
  1196. }
  1197. /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
  1198. static inline void semihosting_arg_fallback(const char *file, const char *cmd)
  1199. {
  1200. char *cmd_token;
  1201. /* argv[0] */
  1202. add_semihosting_arg(&semihosting, "arg", file, NULL);
  1203. /* split -append and initialize argv[1..n] */
  1204. cmd_token = strtok(g_strdup(cmd), " ");
  1205. while (cmd_token) {
  1206. add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
  1207. cmd_token = strtok(NULL, " ");
  1208. }
  1209. }
  1210. /* Now we still need this for compatibility with XEN. */
  1211. bool has_igd_gfx_passthru;
  1212. static void igd_gfx_passthru(void)
  1213. {
  1214. has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
  1215. }
  1216. /***********************************************************/
  1217. /* USB devices */
  1218. static int usb_device_add(const char *devname)
  1219. {
  1220. USBDevice *dev = NULL;
  1221. if (!machine_usb(current_machine)) {
  1222. return -1;
  1223. }
  1224. dev = usbdevice_create(devname);
  1225. if (!dev)
  1226. return -1;
  1227. return 0;
  1228. }
  1229. static int usb_parse(const char *cmdline)
  1230. {
  1231. int r;
  1232. r = usb_device_add(cmdline);
  1233. if (r < 0) {
  1234. error_report("could not add USB device '%s'", cmdline);
  1235. }
  1236. return r;
  1237. }
  1238. /***********************************************************/
  1239. /* machine registration */
  1240. MachineState *current_machine;
  1241. static MachineClass *find_machine(const char *name)
  1242. {
  1243. GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
  1244. MachineClass *mc = NULL;
  1245. for (el = machines; el; el = el->next) {
  1246. MachineClass *temp = el->data;
  1247. if (!strcmp(temp->name, name)) {
  1248. mc = temp;
  1249. break;
  1250. }
  1251. if (temp->alias &&
  1252. !strcmp(temp->alias, name)) {
  1253. mc = temp;
  1254. break;
  1255. }
  1256. }
  1257. g_slist_free(machines);
  1258. return mc;
  1259. }
  1260. MachineClass *find_default_machine(void)
  1261. {
  1262. GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
  1263. MachineClass *mc = NULL;
  1264. for (el = machines; el; el = el->next) {
  1265. MachineClass *temp = el->data;
  1266. if (temp->is_default) {
  1267. mc = temp;
  1268. break;
  1269. }
  1270. }
  1271. g_slist_free(machines);
  1272. return mc;
  1273. }
  1274. MachineInfoList *qmp_query_machines(Error **errp)
  1275. {
  1276. GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
  1277. MachineInfoList *mach_list = NULL;
  1278. for (el = machines; el; el = el->next) {
  1279. MachineClass *mc = el->data;
  1280. MachineInfoList *entry;
  1281. MachineInfo *info;
  1282. info = g_malloc0(sizeof(*info));
  1283. if (mc->is_default) {
  1284. info->has_is_default = true;
  1285. info->is_default = true;
  1286. }
  1287. if (mc->alias) {
  1288. info->has_alias = true;
  1289. info->alias = g_strdup(mc->alias);
  1290. }
  1291. info->name = g_strdup(mc->name);
  1292. info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
  1293. info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
  1294. entry = g_malloc0(sizeof(*entry));
  1295. entry->value = info;
  1296. entry->next = mach_list;
  1297. mach_list = entry;
  1298. }
  1299. g_slist_free(machines);
  1300. return mach_list;
  1301. }
  1302. static int machine_help_func(QemuOpts *opts, MachineState *machine)
  1303. {
  1304. ObjectProperty *prop;
  1305. ObjectPropertyIterator iter;
  1306. if (!qemu_opt_has_help_opt(opts)) {
  1307. return 0;
  1308. }
  1309. object_property_iter_init(&iter, OBJECT(machine));
  1310. while ((prop = object_property_iter_next(&iter))) {
  1311. if (!prop->set) {
  1312. continue;
  1313. }
  1314. error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
  1315. prop->name, prop->type);
  1316. if (prop->description) {
  1317. error_printf(" (%s)\n", prop->description);
  1318. } else {
  1319. error_printf("\n");
  1320. }
  1321. }
  1322. return 1;
  1323. }
  1324. struct vm_change_state_entry {
  1325. VMChangeStateHandler *cb;
  1326. void *opaque;
  1327. QLIST_ENTRY (vm_change_state_entry) entries;
  1328. };
  1329. static QLIST_HEAD(, vm_change_state_entry) vm_change_state_head;
  1330. VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
  1331. void *opaque)
  1332. {
  1333. VMChangeStateEntry *e;
  1334. e = g_malloc0(sizeof (*e));
  1335. e->cb = cb;
  1336. e->opaque = opaque;
  1337. QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
  1338. return e;
  1339. }
  1340. void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
  1341. {
  1342. QLIST_REMOVE (e, entries);
  1343. g_free (e);
  1344. }
  1345. void vm_state_notify(int running, RunState state)
  1346. {
  1347. VMChangeStateEntry *e, *next;
  1348. trace_vm_state_notify(running, state, RunState_str(state));
  1349. QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
  1350. e->cb(e->opaque, running, state);
  1351. }
  1352. }
  1353. static ShutdownCause reset_requested;
  1354. static ShutdownCause shutdown_requested;
  1355. static int shutdown_signal;
  1356. static pid_t shutdown_pid;
  1357. static int powerdown_requested;
  1358. static int debug_requested;
  1359. static int suspend_requested;
  1360. static bool preconfig_exit_requested = true;
  1361. static WakeupReason wakeup_reason;
  1362. static NotifierList powerdown_notifiers =
  1363. NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
  1364. static NotifierList suspend_notifiers =
  1365. NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
  1366. static NotifierList wakeup_notifiers =
  1367. NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
  1368. static NotifierList shutdown_notifiers =
  1369. NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
  1370. static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
  1371. ShutdownCause qemu_shutdown_requested_get(void)
  1372. {
  1373. return shutdown_requested;
  1374. }
  1375. ShutdownCause qemu_reset_requested_get(void)
  1376. {
  1377. return reset_requested;
  1378. }
  1379. static int qemu_shutdown_requested(void)
  1380. {
  1381. return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
  1382. }
  1383. static void qemu_kill_report(void)
  1384. {
  1385. if (!qtest_driver() && shutdown_signal) {
  1386. if (shutdown_pid == 0) {
  1387. /* This happens for eg ^C at the terminal, so it's worth
  1388. * avoiding printing an odd message in that case.
  1389. */
  1390. error_report("terminating on signal %d", shutdown_signal);
  1391. } else {
  1392. char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
  1393. error_report("terminating on signal %d from pid " FMT_pid " (%s)",
  1394. shutdown_signal, shutdown_pid,
  1395. shutdown_cmd ? shutdown_cmd : "<unknown process>");
  1396. g_free(shutdown_cmd);
  1397. }
  1398. shutdown_signal = 0;
  1399. }
  1400. }
  1401. static ShutdownCause qemu_reset_requested(void)
  1402. {
  1403. ShutdownCause r = reset_requested;
  1404. if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
  1405. reset_requested = SHUTDOWN_CAUSE_NONE;
  1406. return r;
  1407. }
  1408. return SHUTDOWN_CAUSE_NONE;
  1409. }
  1410. static int qemu_suspend_requested(void)
  1411. {
  1412. int r = suspend_requested;
  1413. if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
  1414. suspend_requested = 0;
  1415. return r;
  1416. }
  1417. return false;
  1418. }
  1419. static WakeupReason qemu_wakeup_requested(void)
  1420. {
  1421. return wakeup_reason;
  1422. }
  1423. static int qemu_powerdown_requested(void)
  1424. {
  1425. int r = powerdown_requested;
  1426. powerdown_requested = 0;
  1427. return r;
  1428. }
  1429. static int qemu_debug_requested(void)
  1430. {
  1431. int r = debug_requested;
  1432. debug_requested = 0;
  1433. return r;
  1434. }
  1435. void qemu_exit_preconfig_request(void)
  1436. {
  1437. preconfig_exit_requested = true;
  1438. }
  1439. /*
  1440. * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
  1441. */
  1442. void qemu_system_reset(ShutdownCause reason)
  1443. {
  1444. MachineClass *mc;
  1445. mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
  1446. cpu_synchronize_all_states();
  1447. if (mc && mc->reset) {
  1448. mc->reset();
  1449. } else {
  1450. qemu_devices_reset();
  1451. }
  1452. if (reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
  1453. qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
  1454. }
  1455. cpu_synchronize_all_post_reset();
  1456. }
  1457. void qemu_system_guest_panicked(GuestPanicInformation *info)
  1458. {
  1459. qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
  1460. if (current_cpu) {
  1461. current_cpu->crash_occurred = true;
  1462. }
  1463. qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
  1464. !!info, info);
  1465. vm_stop(RUN_STATE_GUEST_PANICKED);
  1466. if (!no_shutdown) {
  1467. qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
  1468. !!info, info);
  1469. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
  1470. }
  1471. if (info) {
  1472. if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
  1473. qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
  1474. " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
  1475. info->u.hyper_v.arg1,
  1476. info->u.hyper_v.arg2,
  1477. info->u.hyper_v.arg3,
  1478. info->u.hyper_v.arg4,
  1479. info->u.hyper_v.arg5);
  1480. } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
  1481. qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
  1482. "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
  1483. info->u.s390.core,
  1484. S390CrashReason_str(info->u.s390.reason),
  1485. info->u.s390.psw_mask,
  1486. info->u.s390.psw_addr);
  1487. }
  1488. qapi_free_GuestPanicInformation(info);
  1489. }
  1490. }
  1491. void qemu_system_reset_request(ShutdownCause reason)
  1492. {
  1493. if (no_reboot && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
  1494. shutdown_requested = reason;
  1495. } else {
  1496. reset_requested = reason;
  1497. }
  1498. cpu_stop_current();
  1499. qemu_notify_event();
  1500. }
  1501. static void qemu_system_suspend(void)
  1502. {
  1503. pause_all_vcpus();
  1504. notifier_list_notify(&suspend_notifiers, NULL);
  1505. runstate_set(RUN_STATE_SUSPENDED);
  1506. qapi_event_send_suspend();
  1507. }
  1508. void qemu_system_suspend_request(void)
  1509. {
  1510. if (runstate_check(RUN_STATE_SUSPENDED)) {
  1511. return;
  1512. }
  1513. suspend_requested = 1;
  1514. cpu_stop_current();
  1515. qemu_notify_event();
  1516. }
  1517. void qemu_register_suspend_notifier(Notifier *notifier)
  1518. {
  1519. notifier_list_add(&suspend_notifiers, notifier);
  1520. }
  1521. void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
  1522. {
  1523. trace_system_wakeup_request(reason);
  1524. if (!runstate_check(RUN_STATE_SUSPENDED)) {
  1525. error_setg(errp,
  1526. "Unable to wake up: guest is not in suspended state");
  1527. return;
  1528. }
  1529. if (!(wakeup_reason_mask & (1 << reason))) {
  1530. return;
  1531. }
  1532. runstate_set(RUN_STATE_RUNNING);
  1533. wakeup_reason = reason;
  1534. qemu_notify_event();
  1535. }
  1536. void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
  1537. {
  1538. if (enabled) {
  1539. wakeup_reason_mask |= (1 << reason);
  1540. } else {
  1541. wakeup_reason_mask &= ~(1 << reason);
  1542. }
  1543. }
  1544. void qemu_register_wakeup_notifier(Notifier *notifier)
  1545. {
  1546. notifier_list_add(&wakeup_notifiers, notifier);
  1547. }
  1548. void qemu_register_wakeup_support(void)
  1549. {
  1550. wakeup_suspend_enabled = true;
  1551. }
  1552. bool qemu_wakeup_suspend_enabled(void)
  1553. {
  1554. return wakeup_suspend_enabled;
  1555. }
  1556. CurrentMachineParams *qmp_query_current_machine(Error **errp)
  1557. {
  1558. CurrentMachineParams *params = g_malloc0(sizeof(*params));
  1559. params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
  1560. return params;
  1561. }
  1562. void qemu_system_killed(int signal, pid_t pid)
  1563. {
  1564. shutdown_signal = signal;
  1565. shutdown_pid = pid;
  1566. no_shutdown = 0;
  1567. /* Cannot call qemu_system_shutdown_request directly because
  1568. * we are in a signal handler.
  1569. */
  1570. shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
  1571. qemu_notify_event();
  1572. }
  1573. void qemu_system_shutdown_request(ShutdownCause reason)
  1574. {
  1575. trace_qemu_system_shutdown_request(reason);
  1576. replay_shutdown_request(reason);
  1577. shutdown_requested = reason;
  1578. qemu_notify_event();
  1579. }
  1580. static void qemu_system_powerdown(void)
  1581. {
  1582. qapi_event_send_powerdown();
  1583. notifier_list_notify(&powerdown_notifiers, NULL);
  1584. }
  1585. static void qemu_system_shutdown(ShutdownCause cause)
  1586. {
  1587. qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
  1588. notifier_list_notify(&shutdown_notifiers, &cause);
  1589. }
  1590. void qemu_system_powerdown_request(void)
  1591. {
  1592. trace_qemu_system_powerdown_request();
  1593. powerdown_requested = 1;
  1594. qemu_notify_event();
  1595. }
  1596. void qemu_register_powerdown_notifier(Notifier *notifier)
  1597. {
  1598. notifier_list_add(&powerdown_notifiers, notifier);
  1599. }
  1600. void qemu_register_shutdown_notifier(Notifier *notifier)
  1601. {
  1602. notifier_list_add(&shutdown_notifiers, notifier);
  1603. }
  1604. void qemu_system_debug_request(void)
  1605. {
  1606. debug_requested = 1;
  1607. qemu_notify_event();
  1608. }
  1609. static bool main_loop_should_exit(void)
  1610. {
  1611. RunState r;
  1612. ShutdownCause request;
  1613. if (preconfig_exit_requested) {
  1614. if (runstate_check(RUN_STATE_PRECONFIG)) {
  1615. runstate_set(RUN_STATE_PRELAUNCH);
  1616. }
  1617. preconfig_exit_requested = false;
  1618. return true;
  1619. }
  1620. if (qemu_debug_requested()) {
  1621. vm_stop(RUN_STATE_DEBUG);
  1622. }
  1623. if (qemu_suspend_requested()) {
  1624. qemu_system_suspend();
  1625. }
  1626. request = qemu_shutdown_requested();
  1627. if (request) {
  1628. qemu_kill_report();
  1629. qemu_system_shutdown(request);
  1630. if (no_shutdown) {
  1631. vm_stop(RUN_STATE_SHUTDOWN);
  1632. } else {
  1633. return true;
  1634. }
  1635. }
  1636. request = qemu_reset_requested();
  1637. if (request) {
  1638. pause_all_vcpus();
  1639. qemu_system_reset(request);
  1640. resume_all_vcpus();
  1641. if (!runstate_check(RUN_STATE_RUNNING) &&
  1642. !runstate_check(RUN_STATE_INMIGRATE)) {
  1643. runstate_set(RUN_STATE_PRELAUNCH);
  1644. }
  1645. }
  1646. if (qemu_wakeup_requested()) {
  1647. pause_all_vcpus();
  1648. qemu_system_reset(SHUTDOWN_CAUSE_NONE);
  1649. notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
  1650. wakeup_reason = QEMU_WAKEUP_REASON_NONE;
  1651. resume_all_vcpus();
  1652. qapi_event_send_wakeup();
  1653. }
  1654. if (qemu_powerdown_requested()) {
  1655. qemu_system_powerdown();
  1656. }
  1657. if (qemu_vmstop_requested(&r)) {
  1658. vm_stop(r);
  1659. }
  1660. return false;
  1661. }
  1662. static void main_loop(void)
  1663. {
  1664. #ifdef CONFIG_PROFILER
  1665. int64_t ti;
  1666. #endif
  1667. while (!main_loop_should_exit()) {
  1668. #ifdef CONFIG_PROFILER
  1669. ti = profile_getclock();
  1670. #endif
  1671. main_loop_wait(false);
  1672. #ifdef CONFIG_PROFILER
  1673. dev_time += profile_getclock() - ti;
  1674. #endif
  1675. }
  1676. }
  1677. static void version(void)
  1678. {
  1679. printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
  1680. QEMU_COPYRIGHT "\n");
  1681. }
  1682. static void help(int exitcode)
  1683. {
  1684. version();
  1685. printf("usage: %s [options] [disk_image]\n\n"
  1686. "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
  1687. error_get_progname());
  1688. #define QEMU_OPTIONS_GENERATE_HELP
  1689. #include "qemu-options-wrapper.h"
  1690. printf("\nDuring emulation, the following keys are useful:\n"
  1691. "ctrl-alt-f toggle full screen\n"
  1692. "ctrl-alt-n switch to virtual console 'n'\n"
  1693. "ctrl-alt toggle mouse and keyboard grab\n"
  1694. "\n"
  1695. "When using -nographic, press 'ctrl-a h' to get some help.\n"
  1696. "\n"
  1697. QEMU_HELP_BOTTOM "\n");
  1698. exit(exitcode);
  1699. }
  1700. #define HAS_ARG 0x0001
  1701. typedef struct QEMUOption {
  1702. const char *name;
  1703. int flags;
  1704. int index;
  1705. uint32_t arch_mask;
  1706. } QEMUOption;
  1707. static const QEMUOption qemu_options[] = {
  1708. { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
  1709. #define QEMU_OPTIONS_GENERATE_OPTIONS
  1710. #include "qemu-options-wrapper.h"
  1711. { NULL },
  1712. };
  1713. typedef struct VGAInterfaceInfo {
  1714. const char *opt_name; /* option name */
  1715. const char *name; /* human-readable name */
  1716. /* Class names indicating that support is available.
  1717. * If no class is specified, the interface is always available */
  1718. const char *class_names[2];
  1719. } VGAInterfaceInfo;
  1720. static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
  1721. [VGA_NONE] = {
  1722. .opt_name = "none",
  1723. },
  1724. [VGA_STD] = {
  1725. .opt_name = "std",
  1726. .name = "standard VGA",
  1727. .class_names = { "VGA", "isa-vga" },
  1728. },
  1729. [VGA_CIRRUS] = {
  1730. .opt_name = "cirrus",
  1731. .name = "Cirrus VGA",
  1732. .class_names = { "cirrus-vga", "isa-cirrus-vga" },
  1733. },
  1734. [VGA_VMWARE] = {
  1735. .opt_name = "vmware",
  1736. .name = "VMWare SVGA",
  1737. .class_names = { "vmware-svga" },
  1738. },
  1739. [VGA_VIRTIO] = {
  1740. .opt_name = "virtio",
  1741. .name = "Virtio VGA",
  1742. .class_names = { "virtio-vga" },
  1743. },
  1744. [VGA_QXL] = {
  1745. .opt_name = "qxl",
  1746. .name = "QXL VGA",
  1747. .class_names = { "qxl-vga" },
  1748. },
  1749. [VGA_TCX] = {
  1750. .opt_name = "tcx",
  1751. .name = "TCX framebuffer",
  1752. .class_names = { "SUNW,tcx" },
  1753. },
  1754. [VGA_CG3] = {
  1755. .opt_name = "cg3",
  1756. .name = "CG3 framebuffer",
  1757. .class_names = { "cgthree" },
  1758. },
  1759. [VGA_XENFB] = {
  1760. .opt_name = "xenfb",
  1761. },
  1762. };
  1763. static bool vga_interface_available(VGAInterfaceType t)
  1764. {
  1765. VGAInterfaceInfo *ti = &vga_interfaces[t];
  1766. assert(t < VGA_TYPE_MAX);
  1767. return !ti->class_names[0] ||
  1768. object_class_by_name(ti->class_names[0]) ||
  1769. object_class_by_name(ti->class_names[1]);
  1770. }
  1771. static void select_vgahw(const char *p)
  1772. {
  1773. const char *opts;
  1774. int t;
  1775. assert(vga_interface_type == VGA_NONE);
  1776. for (t = 0; t < VGA_TYPE_MAX; t++) {
  1777. VGAInterfaceInfo *ti = &vga_interfaces[t];
  1778. if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
  1779. if (!vga_interface_available(t)) {
  1780. error_report("%s not available", ti->name);
  1781. exit(1);
  1782. }
  1783. vga_interface_type = t;
  1784. break;
  1785. }
  1786. }
  1787. if (t == VGA_TYPE_MAX) {
  1788. invalid_vga:
  1789. error_report("unknown vga type: %s", p);
  1790. exit(1);
  1791. }
  1792. while (*opts) {
  1793. const char *nextopt;
  1794. if (strstart(opts, ",retrace=", &nextopt)) {
  1795. opts = nextopt;
  1796. if (strstart(opts, "dumb", &nextopt))
  1797. vga_retrace_method = VGA_RETRACE_DUMB;
  1798. else if (strstart(opts, "precise", &nextopt))
  1799. vga_retrace_method = VGA_RETRACE_PRECISE;
  1800. else goto invalid_vga;
  1801. } else goto invalid_vga;
  1802. opts = nextopt;
  1803. }
  1804. }
  1805. static void parse_display_qapi(const char *optarg)
  1806. {
  1807. DisplayOptions *opts;
  1808. Visitor *v;
  1809. v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
  1810. visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
  1811. QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
  1812. qapi_free_DisplayOptions(opts);
  1813. visit_free(v);
  1814. }
  1815. DisplayOptions *qmp_query_display_options(Error **errp)
  1816. {
  1817. return QAPI_CLONE(DisplayOptions, &dpy);
  1818. }
  1819. static void parse_display(const char *p)
  1820. {
  1821. const char *opts;
  1822. if (strstart(p, "sdl", &opts)) {
  1823. /*
  1824. * sdl DisplayType needs hand-crafted parser instead of
  1825. * parse_display_qapi() due to some options not in
  1826. * DisplayOptions, specifically:
  1827. * - frame
  1828. * Already deprecated.
  1829. * - ctrl_grab + alt_grab
  1830. * Not clear yet what happens to them long-term. Should
  1831. * replaced by something better or deprecated and dropped.
  1832. */
  1833. dpy.type = DISPLAY_TYPE_SDL;
  1834. while (*opts) {
  1835. const char *nextopt;
  1836. if (strstart(opts, ",alt_grab=", &nextopt)) {
  1837. opts = nextopt;
  1838. if (strstart(opts, "on", &nextopt)) {
  1839. alt_grab = 1;
  1840. } else if (strstart(opts, "off", &nextopt)) {
  1841. alt_grab = 0;
  1842. } else {
  1843. goto invalid_sdl_args;
  1844. }
  1845. } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
  1846. opts = nextopt;
  1847. if (strstart(opts, "on", &nextopt)) {
  1848. ctrl_grab = 1;
  1849. } else if (strstart(opts, "off", &nextopt)) {
  1850. ctrl_grab = 0;
  1851. } else {
  1852. goto invalid_sdl_args;
  1853. }
  1854. } else if (strstart(opts, ",window_close=", &nextopt)) {
  1855. opts = nextopt;
  1856. dpy.has_window_close = true;
  1857. if (strstart(opts, "on", &nextopt)) {
  1858. dpy.window_close = true;
  1859. } else if (strstart(opts, "off", &nextopt)) {
  1860. dpy.window_close = false;
  1861. } else {
  1862. goto invalid_sdl_args;
  1863. }
  1864. } else if (strstart(opts, ",gl=", &nextopt)) {
  1865. opts = nextopt;
  1866. dpy.has_gl = true;
  1867. if (strstart(opts, "on", &nextopt)) {
  1868. dpy.gl = DISPLAYGL_MODE_ON;
  1869. } else if (strstart(opts, "core", &nextopt)) {
  1870. dpy.gl = DISPLAYGL_MODE_CORE;
  1871. } else if (strstart(opts, "es", &nextopt)) {
  1872. dpy.gl = DISPLAYGL_MODE_ES;
  1873. } else if (strstart(opts, "off", &nextopt)) {
  1874. dpy.gl = DISPLAYGL_MODE_OFF;
  1875. } else {
  1876. goto invalid_sdl_args;
  1877. }
  1878. } else {
  1879. invalid_sdl_args:
  1880. error_report("invalid SDL option string");
  1881. exit(1);
  1882. }
  1883. opts = nextopt;
  1884. }
  1885. } else if (strstart(p, "vnc", &opts)) {
  1886. /*
  1887. * vnc isn't a (local) DisplayType but a protocol for remote
  1888. * display access.
  1889. */
  1890. if (*opts == '=') {
  1891. vnc_parse(opts + 1, &error_fatal);
  1892. } else {
  1893. error_report("VNC requires a display argument vnc=<display>");
  1894. exit(1);
  1895. }
  1896. } else {
  1897. parse_display_qapi(p);
  1898. }
  1899. }
  1900. char *qemu_find_file(int type, const char *name)
  1901. {
  1902. int i;
  1903. const char *subdir;
  1904. char *buf;
  1905. /* Try the name as a straight path first */
  1906. if (access(name, R_OK) == 0) {
  1907. trace_load_file(name, name);
  1908. return g_strdup(name);
  1909. }
  1910. switch (type) {
  1911. case QEMU_FILE_TYPE_BIOS:
  1912. subdir = "";
  1913. break;
  1914. case QEMU_FILE_TYPE_KEYMAP:
  1915. subdir = "keymaps/";
  1916. break;
  1917. default:
  1918. abort();
  1919. }
  1920. for (i = 0; i < data_dir_idx; i++) {
  1921. buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
  1922. if (access(buf, R_OK) == 0) {
  1923. trace_load_file(name, buf);
  1924. return buf;
  1925. }
  1926. g_free(buf);
  1927. }
  1928. return NULL;
  1929. }
  1930. static void qemu_add_data_dir(const char *path)
  1931. {
  1932. int i;
  1933. if (path == NULL) {
  1934. return;
  1935. }
  1936. if (data_dir_idx == ARRAY_SIZE(data_dir)) {
  1937. return;
  1938. }
  1939. for (i = 0; i < data_dir_idx; i++) {
  1940. if (strcmp(data_dir[i], path) == 0) {
  1941. return; /* duplicate */
  1942. }
  1943. }
  1944. data_dir[data_dir_idx++] = g_strdup(path);
  1945. }
  1946. static inline bool nonempty_str(const char *str)
  1947. {
  1948. return str && *str;
  1949. }
  1950. static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
  1951. {
  1952. gchar *buf;
  1953. size_t size;
  1954. const char *name, *file, *str;
  1955. FWCfgState *fw_cfg = (FWCfgState *) opaque;
  1956. if (fw_cfg == NULL) {
  1957. error_setg(errp, "fw_cfg device not available");
  1958. return -1;
  1959. }
  1960. name = qemu_opt_get(opts, "name");
  1961. file = qemu_opt_get(opts, "file");
  1962. str = qemu_opt_get(opts, "string");
  1963. /* we need name and either a file or the content string */
  1964. if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
  1965. error_setg(errp, "invalid argument(s)");
  1966. return -1;
  1967. }
  1968. if (nonempty_str(file) && nonempty_str(str)) {
  1969. error_setg(errp, "file and string are mutually exclusive");
  1970. return -1;
  1971. }
  1972. if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
  1973. error_setg(errp, "name too long (max. %d char)",
  1974. FW_CFG_MAX_FILE_PATH - 1);
  1975. return -1;
  1976. }
  1977. if (strncmp(name, "opt/", 4) != 0) {
  1978. warn_report("externally provided fw_cfg item names "
  1979. "should be prefixed with \"opt/\"");
  1980. }
  1981. if (nonempty_str(str)) {
  1982. size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
  1983. buf = g_memdup(str, size);
  1984. } else {
  1985. GError *err = NULL;
  1986. if (!g_file_get_contents(file, &buf, &size, &err)) {
  1987. error_setg(errp, "can't load %s: %s", file, err->message);
  1988. g_error_free(err);
  1989. return -1;
  1990. }
  1991. }
  1992. /* For legacy, keep user files in a specific global order. */
  1993. fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
  1994. fw_cfg_add_file(fw_cfg, name, buf, size);
  1995. fw_cfg_reset_order_override(fw_cfg);
  1996. return 0;
  1997. }
  1998. static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
  1999. {
  2000. return qdev_device_help(opts);
  2001. }
  2002. static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
  2003. {
  2004. DeviceState *dev;
  2005. dev = qdev_device_add(opts, errp);
  2006. if (!dev) {
  2007. return -1;
  2008. }
  2009. object_unref(OBJECT(dev));
  2010. return 0;
  2011. }
  2012. static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
  2013. {
  2014. Error *local_err = NULL;
  2015. if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
  2016. if (local_err) {
  2017. error_propagate(errp, local_err);
  2018. return -1;
  2019. }
  2020. exit(0);
  2021. }
  2022. return 0;
  2023. }
  2024. #ifdef CONFIG_VIRTFS
  2025. static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
  2026. {
  2027. return qemu_fsdev_add(opts, errp);
  2028. }
  2029. #endif
  2030. static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
  2031. {
  2032. Chardev *chr;
  2033. const char *chardev;
  2034. const char *mode;
  2035. int flags;
  2036. mode = qemu_opt_get(opts, "mode");
  2037. if (mode == NULL) {
  2038. mode = "readline";
  2039. }
  2040. if (strcmp(mode, "readline") == 0) {
  2041. flags = MONITOR_USE_READLINE;
  2042. } else if (strcmp(mode, "control") == 0) {
  2043. flags = MONITOR_USE_CONTROL;
  2044. } else {
  2045. error_setg(errp, "unknown monitor mode \"%s\"", mode);
  2046. return -1;
  2047. }
  2048. if (qemu_opt_get_bool(opts, "pretty", 0))
  2049. flags |= MONITOR_USE_PRETTY;
  2050. chardev = qemu_opt_get(opts, "chardev");
  2051. if (!chardev) {
  2052. error_report("chardev is required");
  2053. exit(1);
  2054. }
  2055. chr = qemu_chr_find(chardev);
  2056. if (chr == NULL) {
  2057. error_setg(errp, "chardev \"%s\" not found", chardev);
  2058. return -1;
  2059. }
  2060. monitor_init(chr, flags);
  2061. return 0;
  2062. }
  2063. static void monitor_parse(const char *optarg, const char *mode, bool pretty)
  2064. {
  2065. static int monitor_device_index = 0;
  2066. QemuOpts *opts;
  2067. const char *p;
  2068. char label[32];
  2069. if (strstart(optarg, "chardev:", &p)) {
  2070. snprintf(label, sizeof(label), "%s", p);
  2071. } else {
  2072. snprintf(label, sizeof(label), "compat_monitor%d",
  2073. monitor_device_index);
  2074. opts = qemu_chr_parse_compat(label, optarg, true);
  2075. if (!opts) {
  2076. error_report("parse error: %s", optarg);
  2077. exit(1);
  2078. }
  2079. }
  2080. opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
  2081. qemu_opt_set(opts, "mode", mode, &error_abort);
  2082. qemu_opt_set(opts, "chardev", label, &error_abort);
  2083. qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
  2084. monitor_device_index++;
  2085. }
  2086. struct device_config {
  2087. enum {
  2088. DEV_USB, /* -usbdevice */
  2089. DEV_BT, /* -bt */
  2090. DEV_SERIAL, /* -serial */
  2091. DEV_PARALLEL, /* -parallel */
  2092. DEV_DEBUGCON, /* -debugcon */
  2093. DEV_GDB, /* -gdb, -s */
  2094. DEV_SCLP, /* s390 sclp */
  2095. } type;
  2096. const char *cmdline;
  2097. Location loc;
  2098. QTAILQ_ENTRY(device_config) next;
  2099. };
  2100. static QTAILQ_HEAD(, device_config) device_configs =
  2101. QTAILQ_HEAD_INITIALIZER(device_configs);
  2102. static void add_device_config(int type, const char *cmdline)
  2103. {
  2104. struct device_config *conf;
  2105. conf = g_malloc0(sizeof(*conf));
  2106. conf->type = type;
  2107. conf->cmdline = cmdline;
  2108. loc_save(&conf->loc);
  2109. QTAILQ_INSERT_TAIL(&device_configs, conf, next);
  2110. }
  2111. static int foreach_device_config(int type, int (*func)(const char *cmdline))
  2112. {
  2113. struct device_config *conf;
  2114. int rc;
  2115. QTAILQ_FOREACH(conf, &device_configs, next) {
  2116. if (conf->type != type)
  2117. continue;
  2118. loc_push_restore(&conf->loc);
  2119. rc = func(conf->cmdline);
  2120. loc_pop(&conf->loc);
  2121. if (rc) {
  2122. return rc;
  2123. }
  2124. }
  2125. return 0;
  2126. }
  2127. static int serial_parse(const char *devname)
  2128. {
  2129. int index = num_serial_hds;
  2130. char label[32];
  2131. if (strcmp(devname, "none") == 0)
  2132. return 0;
  2133. snprintf(label, sizeof(label), "serial%d", index);
  2134. serial_hds = g_renew(Chardev *, serial_hds, index + 1);
  2135. serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
  2136. if (!serial_hds[index]) {
  2137. error_report("could not connect serial device"
  2138. " to character backend '%s'", devname);
  2139. return -1;
  2140. }
  2141. num_serial_hds++;
  2142. return 0;
  2143. }
  2144. Chardev *serial_hd(int i)
  2145. {
  2146. assert(i >= 0);
  2147. if (i < num_serial_hds) {
  2148. return serial_hds[i];
  2149. }
  2150. return NULL;
  2151. }
  2152. int serial_max_hds(void)
  2153. {
  2154. return num_serial_hds;
  2155. }
  2156. static int parallel_parse(const char *devname)
  2157. {
  2158. static int index = 0;
  2159. char label[32];
  2160. if (strcmp(devname, "none") == 0)
  2161. return 0;
  2162. if (index == MAX_PARALLEL_PORTS) {
  2163. error_report("too many parallel ports");
  2164. exit(1);
  2165. }
  2166. snprintf(label, sizeof(label), "parallel%d", index);
  2167. parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
  2168. if (!parallel_hds[index]) {
  2169. error_report("could not connect parallel device"
  2170. " to character backend '%s'", devname);
  2171. return -1;
  2172. }
  2173. index++;
  2174. return 0;
  2175. }
  2176. static int debugcon_parse(const char *devname)
  2177. {
  2178. QemuOpts *opts;
  2179. if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
  2180. error_report("invalid character backend '%s'", devname);
  2181. exit(1);
  2182. }
  2183. opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
  2184. if (!opts) {
  2185. error_report("already have a debugcon device");
  2186. exit(1);
  2187. }
  2188. qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
  2189. qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
  2190. return 0;
  2191. }
  2192. static gint machine_class_cmp(gconstpointer a, gconstpointer b)
  2193. {
  2194. const MachineClass *mc1 = a, *mc2 = b;
  2195. int res;
  2196. if (mc1->family == NULL) {
  2197. if (mc2->family == NULL) {
  2198. /* Compare standalone machine types against each other; they sort
  2199. * in increasing order.
  2200. */
  2201. return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
  2202. object_class_get_name(OBJECT_CLASS(mc2)));
  2203. }
  2204. /* Standalone machine types sort after families. */
  2205. return 1;
  2206. }
  2207. if (mc2->family == NULL) {
  2208. /* Families sort before standalone machine types. */
  2209. return -1;
  2210. }
  2211. /* Families sort between each other alphabetically increasingly. */
  2212. res = strcmp(mc1->family, mc2->family);
  2213. if (res != 0) {
  2214. return res;
  2215. }
  2216. /* Within the same family, machine types sort in decreasing order. */
  2217. return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
  2218. object_class_get_name(OBJECT_CLASS(mc1)));
  2219. }
  2220. static MachineClass *machine_parse(const char *name)
  2221. {
  2222. MachineClass *mc = NULL;
  2223. GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
  2224. if (name) {
  2225. mc = find_machine(name);
  2226. }
  2227. if (mc) {
  2228. g_slist_free(machines);
  2229. return mc;
  2230. }
  2231. if (name && !is_help_option(name)) {
  2232. error_report("unsupported machine type");
  2233. error_printf("Use -machine help to list supported machines\n");
  2234. } else {
  2235. printf("Supported machines are:\n");
  2236. machines = g_slist_sort(machines, machine_class_cmp);
  2237. for (el = machines; el; el = el->next) {
  2238. MachineClass *mc = el->data;
  2239. if (mc->alias) {
  2240. printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
  2241. }
  2242. printf("%-20s %s%s%s\n", mc->name, mc->desc,
  2243. mc->is_default ? " (default)" : "",
  2244. mc->deprecation_reason ? " (deprecated)" : "");
  2245. }
  2246. }
  2247. g_slist_free(machines);
  2248. exit(!name || !is_help_option(name));
  2249. }
  2250. void qemu_add_exit_notifier(Notifier *notify)
  2251. {
  2252. notifier_list_add(&exit_notifiers, notify);
  2253. }
  2254. void qemu_remove_exit_notifier(Notifier *notify)
  2255. {
  2256. notifier_remove(notify);
  2257. }
  2258. static void qemu_run_exit_notifiers(void)
  2259. {
  2260. notifier_list_notify(&exit_notifiers, NULL);
  2261. }
  2262. static const char *pid_file;
  2263. static Notifier qemu_unlink_pidfile_notifier;
  2264. static void qemu_unlink_pidfile(Notifier *n, void *data)
  2265. {
  2266. if (pid_file) {
  2267. unlink(pid_file);
  2268. }
  2269. }
  2270. bool machine_init_done;
  2271. void qemu_add_machine_init_done_notifier(Notifier *notify)
  2272. {
  2273. notifier_list_add(&machine_init_done_notifiers, notify);
  2274. if (machine_init_done) {
  2275. notify->notify(notify, NULL);
  2276. }
  2277. }
  2278. void qemu_remove_machine_init_done_notifier(Notifier *notify)
  2279. {
  2280. notifier_remove(notify);
  2281. }
  2282. static void qemu_run_machine_init_done_notifiers(void)
  2283. {
  2284. machine_init_done = true;
  2285. notifier_list_notify(&machine_init_done_notifiers, NULL);
  2286. }
  2287. static const QEMUOption *lookup_opt(int argc, char **argv,
  2288. const char **poptarg, int *poptind)
  2289. {
  2290. const QEMUOption *popt;
  2291. int optind = *poptind;
  2292. char *r = argv[optind];
  2293. const char *optarg;
  2294. loc_set_cmdline(argv, optind, 1);
  2295. optind++;
  2296. /* Treat --foo the same as -foo. */
  2297. if (r[1] == '-')
  2298. r++;
  2299. popt = qemu_options;
  2300. for(;;) {
  2301. if (!popt->name) {
  2302. error_report("invalid option");
  2303. exit(1);
  2304. }
  2305. if (!strcmp(popt->name, r + 1))
  2306. break;
  2307. popt++;
  2308. }
  2309. if (popt->flags & HAS_ARG) {
  2310. if (optind >= argc) {
  2311. error_report("requires an argument");
  2312. exit(1);
  2313. }
  2314. optarg = argv[optind++];
  2315. loc_set_cmdline(argv, optind - 2, 2);
  2316. } else {
  2317. optarg = NULL;
  2318. }
  2319. *poptarg = optarg;
  2320. *poptind = optind;
  2321. return popt;
  2322. }
  2323. static MachineClass *select_machine(void)
  2324. {
  2325. MachineClass *machine_class = find_default_machine();
  2326. const char *optarg;
  2327. QemuOpts *opts;
  2328. Location loc;
  2329. loc_push_none(&loc);
  2330. opts = qemu_get_machine_opts();
  2331. qemu_opts_loc_restore(opts);
  2332. optarg = qemu_opt_get(opts, "type");
  2333. if (optarg) {
  2334. machine_class = machine_parse(optarg);
  2335. }
  2336. if (!machine_class) {
  2337. error_report("No machine specified, and there is no default");
  2338. error_printf("Use -machine help to list supported machines\n");
  2339. exit(1);
  2340. }
  2341. loc_pop(&loc);
  2342. return machine_class;
  2343. }
  2344. static int machine_set_property(void *opaque,
  2345. const char *name, const char *value,
  2346. Error **errp)
  2347. {
  2348. Object *obj = OBJECT(opaque);
  2349. Error *local_err = NULL;
  2350. char *p, *qom_name;
  2351. if (strcmp(name, "type") == 0) {
  2352. return 0;
  2353. }
  2354. qom_name = g_strdup(name);
  2355. for (p = qom_name; *p; p++) {
  2356. if (*p == '_') {
  2357. *p = '-';
  2358. }
  2359. }
  2360. object_property_parse(obj, value, qom_name, &local_err);
  2361. g_free(qom_name);
  2362. if (local_err) {
  2363. error_propagate(errp, local_err);
  2364. return -1;
  2365. }
  2366. return 0;
  2367. }
  2368. /*
  2369. * Initial object creation happens before all other
  2370. * QEMU data types are created. The majority of objects
  2371. * can be created at this point. The rng-egd object
  2372. * cannot be created here, as it depends on the chardev
  2373. * already existing.
  2374. */
  2375. static bool object_create_initial(const char *type, QemuOpts *opts)
  2376. {
  2377. ObjectClass *klass;
  2378. if (is_help_option(type)) {
  2379. GSList *l, *list;
  2380. printf("List of user creatable objects:\n");
  2381. list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
  2382. for (l = list; l != NULL; l = l->next) {
  2383. ObjectClass *oc = OBJECT_CLASS(l->data);
  2384. printf(" %s\n", object_class_get_name(oc));
  2385. }
  2386. g_slist_free(list);
  2387. exit(0);
  2388. }
  2389. klass = object_class_by_name(type);
  2390. if (klass && qemu_opt_has_help_opt(opts)) {
  2391. ObjectPropertyIterator iter;
  2392. ObjectProperty *prop;
  2393. GPtrArray *array = g_ptr_array_new();
  2394. int i;
  2395. object_class_property_iter_init(&iter, klass);
  2396. while ((prop = object_property_iter_next(&iter))) {
  2397. GString *str;
  2398. if (!prop->set) {
  2399. continue;
  2400. }
  2401. str = g_string_new(NULL);
  2402. g_string_append_printf(str, " %s=<%s>", prop->name, prop->type);
  2403. if (prop->description) {
  2404. if (str->len < 24) {
  2405. g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
  2406. }
  2407. g_string_append_printf(str, " - %s", prop->description);
  2408. }
  2409. g_ptr_array_add(array, g_string_free(str, false));
  2410. }
  2411. g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
  2412. if (array->len > 0) {
  2413. printf("%s options:\n", type);
  2414. } else {
  2415. printf("There are no options for %s.\n", type);
  2416. }
  2417. for (i = 0; i < array->len; i++) {
  2418. printf("%s\n", (char *)array->pdata[i]);
  2419. }
  2420. g_ptr_array_set_free_func(array, g_free);
  2421. g_ptr_array_free(array, true);
  2422. exit(0);
  2423. }
  2424. if (g_str_equal(type, "rng-egd") ||
  2425. g_str_has_prefix(type, "pr-manager-")) {
  2426. return false;
  2427. }
  2428. #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
  2429. if (g_str_equal(type, "cryptodev-vhost-user")) {
  2430. return false;
  2431. }
  2432. #endif
  2433. /*
  2434. * return false for concrete netfilters since
  2435. * they depend on netdevs already existing
  2436. */
  2437. if (g_str_equal(type, "filter-buffer") ||
  2438. g_str_equal(type, "filter-dump") ||
  2439. g_str_equal(type, "filter-mirror") ||
  2440. g_str_equal(type, "filter-redirector") ||
  2441. g_str_equal(type, "colo-compare") ||
  2442. g_str_equal(type, "filter-rewriter") ||
  2443. g_str_equal(type, "filter-replay")) {
  2444. return false;
  2445. }
  2446. /* Memory allocation by backends needs to be done
  2447. * after configure_accelerator() (due to the tcg_enabled()
  2448. * checks at memory_region_init_*()).
  2449. *
  2450. * Also, allocation of large amounts of memory may delay
  2451. * chardev initialization for too long, and trigger timeouts
  2452. * on software that waits for a monitor socket to be created
  2453. * (e.g. libvirt).
  2454. */
  2455. if (g_str_has_prefix(type, "memory-backend-")) {
  2456. return false;
  2457. }
  2458. return true;
  2459. }
  2460. /*
  2461. * The remainder of object creation happens after the
  2462. * creation of chardev, fsdev, net clients and device data types.
  2463. */
  2464. static bool object_create_delayed(const char *type, QemuOpts *opts)
  2465. {
  2466. return !object_create_initial(type, opts);
  2467. }
  2468. static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
  2469. MachineClass *mc)
  2470. {
  2471. uint64_t sz;
  2472. const char *mem_str;
  2473. const ram_addr_t default_ram_size = mc->default_ram_size;
  2474. QemuOpts *opts = qemu_find_opts_singleton("memory");
  2475. Location loc;
  2476. loc_push_none(&loc);
  2477. qemu_opts_loc_restore(opts);
  2478. sz = 0;
  2479. mem_str = qemu_opt_get(opts, "size");
  2480. if (mem_str) {
  2481. if (!*mem_str) {
  2482. error_report("missing 'size' option value");
  2483. exit(EXIT_FAILURE);
  2484. }
  2485. sz = qemu_opt_get_size(opts, "size", ram_size);
  2486. /* Fix up legacy suffix-less format */
  2487. if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
  2488. uint64_t overflow_check = sz;
  2489. sz *= MiB;
  2490. if (sz / MiB != overflow_check) {
  2491. error_report("too large 'size' option value");
  2492. exit(EXIT_FAILURE);
  2493. }
  2494. }
  2495. }
  2496. /* backward compatibility behaviour for case "-m 0" */
  2497. if (sz == 0) {
  2498. sz = default_ram_size;
  2499. }
  2500. sz = QEMU_ALIGN_UP(sz, 8192);
  2501. ram_size = sz;
  2502. if (ram_size != sz) {
  2503. error_report("ram size too large");
  2504. exit(EXIT_FAILURE);
  2505. }
  2506. /* store value for the future use */
  2507. qemu_opt_set_number(opts, "size", ram_size, &error_abort);
  2508. *maxram_size = ram_size;
  2509. if (qemu_opt_get(opts, "maxmem")) {
  2510. uint64_t slots;
  2511. sz = qemu_opt_get_size(opts, "maxmem", 0);
  2512. slots = qemu_opt_get_number(opts, "slots", 0);
  2513. if (sz < ram_size) {
  2514. error_report("invalid value of -m option maxmem: "
  2515. "maximum memory size (0x%" PRIx64 ") must be at least "
  2516. "the initial memory size (0x" RAM_ADDR_FMT ")",
  2517. sz, ram_size);
  2518. exit(EXIT_FAILURE);
  2519. } else if (slots && sz == ram_size) {
  2520. error_report("invalid value of -m option maxmem: "
  2521. "memory slots were specified but maximum memory size "
  2522. "(0x%" PRIx64 ") is equal to the initial memory size "
  2523. "(0x" RAM_ADDR_FMT ")", sz, ram_size);
  2524. exit(EXIT_FAILURE);
  2525. }
  2526. *maxram_size = sz;
  2527. *ram_slots = slots;
  2528. } else if (qemu_opt_get(opts, "slots")) {
  2529. error_report("invalid -m option value: missing 'maxmem' option");
  2530. exit(EXIT_FAILURE);
  2531. }
  2532. loc_pop(&loc);
  2533. }
  2534. static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
  2535. {
  2536. GlobalProperty *g;
  2537. g = g_malloc0(sizeof(*g));
  2538. g->driver = qemu_opt_get(opts, "driver");
  2539. g->property = qemu_opt_get(opts, "property");
  2540. g->value = qemu_opt_get(opts, "value");
  2541. qdev_prop_register_global(g);
  2542. return 0;
  2543. }
  2544. static int qemu_read_default_config_file(void)
  2545. {
  2546. int ret;
  2547. ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
  2548. if (ret < 0 && ret != -ENOENT) {
  2549. return ret;
  2550. }
  2551. return 0;
  2552. }
  2553. static void user_register_global_props(void)
  2554. {
  2555. qemu_opts_foreach(qemu_find_opts("global"),
  2556. global_init_func, NULL, NULL);
  2557. }
  2558. /*
  2559. * Note: we should see that these properties are actually having a
  2560. * priority: accel < machine < user. This means e.g. when user
  2561. * specifies something in "-global", it'll always be used with highest
  2562. * priority than either machine/accelerator compat properties.
  2563. */
  2564. static void register_global_properties(MachineState *ms)
  2565. {
  2566. user_register_global_props();
  2567. }
  2568. int main(int argc, char **argv, char **envp)
  2569. {
  2570. int i;
  2571. int snapshot, linux_boot;
  2572. const char *initrd_filename;
  2573. const char *kernel_filename, *kernel_cmdline;
  2574. const char *boot_order = NULL;
  2575. const char *boot_once = NULL;
  2576. DisplayState *ds;
  2577. QemuOpts *opts, *machine_opts;
  2578. QemuOpts *icount_opts = NULL, *accel_opts = NULL;
  2579. QemuOptsList *olist;
  2580. int optind;
  2581. const char *optarg;
  2582. const char *loadvm = NULL;
  2583. MachineClass *machine_class;
  2584. const char *cpu_model;
  2585. const char *vga_model = NULL;
  2586. const char *qtest_chrdev = NULL;
  2587. const char *qtest_log = NULL;
  2588. const char *incoming = NULL;
  2589. bool userconfig = true;
  2590. bool nographic = false;
  2591. int display_remote = 0;
  2592. const char *log_mask = NULL;
  2593. const char *log_file = NULL;
  2594. char *trace_file = NULL;
  2595. ram_addr_t maxram_size;
  2596. uint64_t ram_slots = 0;
  2597. FILE *vmstate_dump_file = NULL;
  2598. Error *main_loop_err = NULL;
  2599. Error *err = NULL;
  2600. bool list_data_dirs = false;
  2601. char *dir, **dirs;
  2602. typedef struct BlockdevOptions_queue {
  2603. BlockdevOptions *bdo;
  2604. Location loc;
  2605. QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
  2606. } BlockdevOptions_queue;
  2607. QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
  2608. = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
  2609. module_call_init(MODULE_INIT_TRACE);
  2610. qemu_init_cpu_list();
  2611. qemu_init_cpu_loop();
  2612. qemu_mutex_lock_iothread();
  2613. atexit(qemu_run_exit_notifiers);
  2614. error_set_progname(argv[0]);
  2615. qemu_init_exec_dir(argv[0]);
  2616. module_call_init(MODULE_INIT_QOM);
  2617. qemu_add_opts(&qemu_drive_opts);
  2618. qemu_add_drive_opts(&qemu_legacy_drive_opts);
  2619. qemu_add_drive_opts(&qemu_common_drive_opts);
  2620. qemu_add_drive_opts(&qemu_drive_opts);
  2621. qemu_add_drive_opts(&bdrv_runtime_opts);
  2622. qemu_add_opts(&qemu_chardev_opts);
  2623. qemu_add_opts(&qemu_device_opts);
  2624. qemu_add_opts(&qemu_netdev_opts);
  2625. qemu_add_opts(&qemu_nic_opts);
  2626. qemu_add_opts(&qemu_net_opts);
  2627. qemu_add_opts(&qemu_rtc_opts);
  2628. qemu_add_opts(&qemu_global_opts);
  2629. qemu_add_opts(&qemu_mon_opts);
  2630. qemu_add_opts(&qemu_trace_opts);
  2631. qemu_add_opts(&qemu_option_rom_opts);
  2632. qemu_add_opts(&qemu_machine_opts);
  2633. qemu_add_opts(&qemu_accel_opts);
  2634. qemu_add_opts(&qemu_mem_opts);
  2635. qemu_add_opts(&qemu_smp_opts);
  2636. qemu_add_opts(&qemu_boot_opts);
  2637. qemu_add_opts(&qemu_add_fd_opts);
  2638. qemu_add_opts(&qemu_object_opts);
  2639. qemu_add_opts(&qemu_tpmdev_opts);
  2640. qemu_add_opts(&qemu_realtime_opts);
  2641. qemu_add_opts(&qemu_overcommit_opts);
  2642. qemu_add_opts(&qemu_msg_opts);
  2643. qemu_add_opts(&qemu_name_opts);
  2644. qemu_add_opts(&qemu_numa_opts);
  2645. qemu_add_opts(&qemu_icount_opts);
  2646. qemu_add_opts(&qemu_semihosting_config_opts);
  2647. qemu_add_opts(&qemu_fw_cfg_opts);
  2648. module_call_init(MODULE_INIT_OPTS);
  2649. runstate_init();
  2650. precopy_infrastructure_init();
  2651. postcopy_infrastructure_init();
  2652. monitor_init_globals();
  2653. if (qcrypto_init(&err) < 0) {
  2654. error_reportf_err(err, "cannot initialize crypto: ");
  2655. exit(1);
  2656. }
  2657. QLIST_INIT (&vm_change_state_head);
  2658. os_setup_early_signal_handling();
  2659. cpu_model = NULL;
  2660. snapshot = 0;
  2661. nb_nics = 0;
  2662. bdrv_init_with_whitelist();
  2663. autostart = 1;
  2664. /* first pass of option parsing */
  2665. optind = 1;
  2666. while (optind < argc) {
  2667. if (argv[optind][0] != '-') {
  2668. /* disk image */
  2669. optind++;
  2670. } else {
  2671. const QEMUOption *popt;
  2672. popt = lookup_opt(argc, argv, &optarg, &optind);
  2673. switch (popt->index) {
  2674. case QEMU_OPTION_nouserconfig:
  2675. userconfig = false;
  2676. break;
  2677. }
  2678. }
  2679. }
  2680. if (userconfig) {
  2681. if (qemu_read_default_config_file() < 0) {
  2682. exit(1);
  2683. }
  2684. }
  2685. /* second pass of option parsing */
  2686. optind = 1;
  2687. for(;;) {
  2688. if (optind >= argc)
  2689. break;
  2690. if (argv[optind][0] != '-') {
  2691. drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
  2692. } else {
  2693. const QEMUOption *popt;
  2694. popt = lookup_opt(argc, argv, &optarg, &optind);
  2695. if (!(popt->arch_mask & arch_type)) {
  2696. error_report("Option not supported for this target");
  2697. exit(1);
  2698. }
  2699. switch(popt->index) {
  2700. case QEMU_OPTION_cpu:
  2701. /* hw initialization will check this */
  2702. cpu_model = optarg;
  2703. break;
  2704. case QEMU_OPTION_hda:
  2705. case QEMU_OPTION_hdb:
  2706. case QEMU_OPTION_hdc:
  2707. case QEMU_OPTION_hdd:
  2708. drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
  2709. HD_OPTS);
  2710. break;
  2711. case QEMU_OPTION_blockdev:
  2712. {
  2713. Visitor *v;
  2714. BlockdevOptions_queue *bdo;
  2715. v = qobject_input_visitor_new_str(optarg, "driver",
  2716. &error_fatal);
  2717. bdo = g_new(BlockdevOptions_queue, 1);
  2718. visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
  2719. &error_fatal);
  2720. visit_free(v);
  2721. loc_save(&bdo->loc);
  2722. QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
  2723. break;
  2724. }
  2725. case QEMU_OPTION_drive:
  2726. if (drive_def(optarg) == NULL) {
  2727. exit(1);
  2728. }
  2729. break;
  2730. case QEMU_OPTION_set:
  2731. if (qemu_set_option(optarg) != 0)
  2732. exit(1);
  2733. break;
  2734. case QEMU_OPTION_global:
  2735. if (qemu_global_option(optarg) != 0)
  2736. exit(1);
  2737. break;
  2738. case QEMU_OPTION_mtdblock:
  2739. drive_add(IF_MTD, -1, optarg, MTD_OPTS);
  2740. break;
  2741. case QEMU_OPTION_sd:
  2742. drive_add(IF_SD, -1, optarg, SD_OPTS);
  2743. break;
  2744. case QEMU_OPTION_pflash:
  2745. drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
  2746. break;
  2747. case QEMU_OPTION_snapshot:
  2748. snapshot = 1;
  2749. break;
  2750. case QEMU_OPTION_numa:
  2751. opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
  2752. optarg, true);
  2753. if (!opts) {
  2754. exit(1);
  2755. }
  2756. break;
  2757. case QEMU_OPTION_display:
  2758. parse_display(optarg);
  2759. break;
  2760. case QEMU_OPTION_nographic:
  2761. olist = qemu_find_opts("machine");
  2762. qemu_opts_parse_noisily(olist, "graphics=off", false);
  2763. nographic = true;
  2764. dpy.type = DISPLAY_TYPE_NONE;
  2765. break;
  2766. case QEMU_OPTION_curses:
  2767. #ifdef CONFIG_CURSES
  2768. dpy.type = DISPLAY_TYPE_CURSES;
  2769. #else
  2770. error_report("curses support is disabled");
  2771. exit(1);
  2772. #endif
  2773. break;
  2774. case QEMU_OPTION_portrait:
  2775. graphic_rotate = 90;
  2776. break;
  2777. case QEMU_OPTION_rotate:
  2778. graphic_rotate = strtol(optarg, (char **) &optarg, 10);
  2779. if (graphic_rotate != 0 && graphic_rotate != 90 &&
  2780. graphic_rotate != 180 && graphic_rotate != 270) {
  2781. error_report("only 90, 180, 270 deg rotation is available");
  2782. exit(1);
  2783. }
  2784. break;
  2785. case QEMU_OPTION_kernel:
  2786. qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
  2787. &error_abort);
  2788. break;
  2789. case QEMU_OPTION_initrd:
  2790. qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
  2791. &error_abort);
  2792. break;
  2793. case QEMU_OPTION_append:
  2794. qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
  2795. &error_abort);
  2796. break;
  2797. case QEMU_OPTION_dtb:
  2798. qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
  2799. &error_abort);
  2800. break;
  2801. case QEMU_OPTION_cdrom:
  2802. drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
  2803. break;
  2804. case QEMU_OPTION_boot:
  2805. opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
  2806. optarg, true);
  2807. if (!opts) {
  2808. exit(1);
  2809. }
  2810. break;
  2811. case QEMU_OPTION_fda:
  2812. case QEMU_OPTION_fdb:
  2813. drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
  2814. optarg, FD_OPTS);
  2815. break;
  2816. case QEMU_OPTION_no_fd_bootchk:
  2817. fd_bootchk = 0;
  2818. break;
  2819. case QEMU_OPTION_netdev:
  2820. default_net = 0;
  2821. if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
  2822. exit(1);
  2823. }
  2824. break;
  2825. case QEMU_OPTION_nic:
  2826. default_net = 0;
  2827. if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
  2828. exit(1);
  2829. }
  2830. break;
  2831. case QEMU_OPTION_net:
  2832. default_net = 0;
  2833. if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
  2834. exit(1);
  2835. }
  2836. break;
  2837. #ifdef CONFIG_LIBISCSI
  2838. case QEMU_OPTION_iscsi:
  2839. opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
  2840. optarg, false);
  2841. if (!opts) {
  2842. exit(1);
  2843. }
  2844. break;
  2845. #endif
  2846. case QEMU_OPTION_bt:
  2847. warn_report("The bluetooth subsystem is deprecated and will "
  2848. "be removed soon. If the bluetooth subsystem is "
  2849. "still useful for you, please send a mail to "
  2850. "qemu-devel@nongnu.org with your usecase.");
  2851. add_device_config(DEV_BT, optarg);
  2852. break;
  2853. case QEMU_OPTION_audio_help:
  2854. AUD_help ();
  2855. exit (0);
  2856. break;
  2857. case QEMU_OPTION_soundhw:
  2858. select_soundhw (optarg);
  2859. break;
  2860. case QEMU_OPTION_h:
  2861. help(0);
  2862. break;
  2863. case QEMU_OPTION_version:
  2864. version();
  2865. exit(0);
  2866. break;
  2867. case QEMU_OPTION_m:
  2868. opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
  2869. optarg, true);
  2870. if (!opts) {
  2871. exit(EXIT_FAILURE);
  2872. }
  2873. break;
  2874. #ifdef CONFIG_TPM
  2875. case QEMU_OPTION_tpmdev:
  2876. if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
  2877. exit(1);
  2878. }
  2879. break;
  2880. #endif
  2881. case QEMU_OPTION_mempath:
  2882. mem_path = optarg;
  2883. break;
  2884. case QEMU_OPTION_mem_prealloc:
  2885. mem_prealloc = 1;
  2886. break;
  2887. case QEMU_OPTION_d:
  2888. log_mask = optarg;
  2889. break;
  2890. case QEMU_OPTION_D:
  2891. log_file = optarg;
  2892. break;
  2893. case QEMU_OPTION_DFILTER:
  2894. qemu_set_dfilter_ranges(optarg, &error_fatal);
  2895. break;
  2896. case QEMU_OPTION_s:
  2897. add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
  2898. break;
  2899. case QEMU_OPTION_gdb:
  2900. add_device_config(DEV_GDB, optarg);
  2901. break;
  2902. case QEMU_OPTION_L:
  2903. if (is_help_option(optarg)) {
  2904. list_data_dirs = true;
  2905. } else {
  2906. qemu_add_data_dir(optarg);
  2907. }
  2908. break;
  2909. case QEMU_OPTION_bios:
  2910. qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
  2911. &error_abort);
  2912. break;
  2913. case QEMU_OPTION_singlestep:
  2914. singlestep = 1;
  2915. break;
  2916. case QEMU_OPTION_S:
  2917. autostart = 0;
  2918. break;
  2919. case QEMU_OPTION_k:
  2920. keyboard_layout = optarg;
  2921. break;
  2922. case QEMU_OPTION_vga:
  2923. vga_model = optarg;
  2924. default_vga = 0;
  2925. break;
  2926. case QEMU_OPTION_g:
  2927. {
  2928. const char *p;
  2929. int w, h, depth;
  2930. p = optarg;
  2931. w = strtol(p, (char **)&p, 10);
  2932. if (w <= 0) {
  2933. graphic_error:
  2934. error_report("invalid resolution or depth");
  2935. exit(1);
  2936. }
  2937. if (*p != 'x')
  2938. goto graphic_error;
  2939. p++;
  2940. h = strtol(p, (char **)&p, 10);
  2941. if (h <= 0)
  2942. goto graphic_error;
  2943. if (*p == 'x') {
  2944. p++;
  2945. depth = strtol(p, (char **)&p, 10);
  2946. if (depth != 8 && depth != 15 && depth != 16 &&
  2947. depth != 24 && depth != 32)
  2948. goto graphic_error;
  2949. } else if (*p == '\0') {
  2950. depth = graphic_depth;
  2951. } else {
  2952. goto graphic_error;
  2953. }
  2954. graphic_width = w;
  2955. graphic_height = h;
  2956. graphic_depth = depth;
  2957. }
  2958. break;
  2959. case QEMU_OPTION_echr:
  2960. {
  2961. char *r;
  2962. term_escape_char = strtol(optarg, &r, 0);
  2963. if (r == optarg)
  2964. printf("Bad argument to echr\n");
  2965. break;
  2966. }
  2967. case QEMU_OPTION_monitor:
  2968. default_monitor = 0;
  2969. if (strncmp(optarg, "none", 4)) {
  2970. monitor_parse(optarg, "readline", false);
  2971. }
  2972. break;
  2973. case QEMU_OPTION_qmp:
  2974. monitor_parse(optarg, "control", false);
  2975. default_monitor = 0;
  2976. break;
  2977. case QEMU_OPTION_qmp_pretty:
  2978. monitor_parse(optarg, "control", true);
  2979. default_monitor = 0;
  2980. break;
  2981. case QEMU_OPTION_mon:
  2982. opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
  2983. true);
  2984. if (!opts) {
  2985. exit(1);
  2986. }
  2987. default_monitor = 0;
  2988. break;
  2989. case QEMU_OPTION_chardev:
  2990. opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
  2991. optarg, true);
  2992. if (!opts) {
  2993. exit(1);
  2994. }
  2995. break;
  2996. case QEMU_OPTION_fsdev:
  2997. olist = qemu_find_opts("fsdev");
  2998. if (!olist) {
  2999. error_report("fsdev support is disabled");
  3000. exit(1);
  3001. }
  3002. opts = qemu_opts_parse_noisily(olist, optarg, true);
  3003. if (!opts) {
  3004. exit(1);
  3005. }
  3006. break;
  3007. case QEMU_OPTION_virtfs: {
  3008. QemuOpts *fsdev;
  3009. QemuOpts *device;
  3010. const char *writeout, *sock_fd, *socket, *path, *security_model;
  3011. olist = qemu_find_opts("virtfs");
  3012. if (!olist) {
  3013. error_report("virtfs support is disabled");
  3014. exit(1);
  3015. }
  3016. opts = qemu_opts_parse_noisily(olist, optarg, true);
  3017. if (!opts) {
  3018. exit(1);
  3019. }
  3020. if (qemu_opt_get(opts, "fsdriver") == NULL ||
  3021. qemu_opt_get(opts, "mount_tag") == NULL) {
  3022. error_report("Usage: -virtfs fsdriver,mount_tag=tag");
  3023. exit(1);
  3024. }
  3025. fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
  3026. qemu_opts_id(opts) ?:
  3027. qemu_opt_get(opts, "mount_tag"),
  3028. 1, NULL);
  3029. if (!fsdev) {
  3030. error_report("duplicate or invalid fsdev id: %s",
  3031. qemu_opt_get(opts, "mount_tag"));
  3032. exit(1);
  3033. }
  3034. writeout = qemu_opt_get(opts, "writeout");
  3035. if (writeout) {
  3036. #ifdef CONFIG_SYNC_FILE_RANGE
  3037. qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
  3038. #else
  3039. error_report("writeout=immediate not supported "
  3040. "on this platform");
  3041. exit(1);
  3042. #endif
  3043. }
  3044. qemu_opt_set(fsdev, "fsdriver",
  3045. qemu_opt_get(opts, "fsdriver"), &error_abort);
  3046. path = qemu_opt_get(opts, "path");
  3047. if (path) {
  3048. qemu_opt_set(fsdev, "path", path, &error_abort);
  3049. }
  3050. security_model = qemu_opt_get(opts, "security_model");
  3051. if (security_model) {
  3052. qemu_opt_set(fsdev, "security_model", security_model,
  3053. &error_abort);
  3054. }
  3055. socket = qemu_opt_get(opts, "socket");
  3056. if (socket) {
  3057. qemu_opt_set(fsdev, "socket", socket, &error_abort);
  3058. }
  3059. sock_fd = qemu_opt_get(opts, "sock_fd");
  3060. if (sock_fd) {
  3061. qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
  3062. }
  3063. qemu_opt_set_bool(fsdev, "readonly",
  3064. qemu_opt_get_bool(opts, "readonly", 0),
  3065. &error_abort);
  3066. device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
  3067. &error_abort);
  3068. qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
  3069. qemu_opt_set(device, "fsdev",
  3070. qemu_opts_id(fsdev), &error_abort);
  3071. qemu_opt_set(device, "mount_tag",
  3072. qemu_opt_get(opts, "mount_tag"), &error_abort);
  3073. break;
  3074. }
  3075. case QEMU_OPTION_virtfs_synth: {
  3076. QemuOpts *fsdev;
  3077. QemuOpts *device;
  3078. fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
  3079. 1, NULL);
  3080. if (!fsdev) {
  3081. error_report("duplicate option: %s", "virtfs_synth");
  3082. exit(1);
  3083. }
  3084. qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
  3085. device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
  3086. &error_abort);
  3087. qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
  3088. qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
  3089. qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
  3090. break;
  3091. }
  3092. case QEMU_OPTION_serial:
  3093. add_device_config(DEV_SERIAL, optarg);
  3094. default_serial = 0;
  3095. if (strncmp(optarg, "mon:", 4) == 0) {
  3096. default_monitor = 0;
  3097. }
  3098. break;
  3099. case QEMU_OPTION_watchdog:
  3100. if (watchdog) {
  3101. error_report("only one watchdog option may be given");
  3102. return 1;
  3103. }
  3104. watchdog = optarg;
  3105. break;
  3106. case QEMU_OPTION_watchdog_action:
  3107. if (select_watchdog_action(optarg) == -1) {
  3108. error_report("unknown -watchdog-action parameter");
  3109. exit(1);
  3110. }
  3111. break;
  3112. case QEMU_OPTION_parallel:
  3113. add_device_config(DEV_PARALLEL, optarg);
  3114. default_parallel = 0;
  3115. if (strncmp(optarg, "mon:", 4) == 0) {
  3116. default_monitor = 0;
  3117. }
  3118. break;
  3119. case QEMU_OPTION_debugcon:
  3120. add_device_config(DEV_DEBUGCON, optarg);
  3121. break;
  3122. case QEMU_OPTION_loadvm:
  3123. loadvm = optarg;
  3124. break;
  3125. case QEMU_OPTION_full_screen:
  3126. dpy.has_full_screen = true;
  3127. dpy.full_screen = true;
  3128. break;
  3129. case QEMU_OPTION_alt_grab:
  3130. alt_grab = 1;
  3131. break;
  3132. case QEMU_OPTION_ctrl_grab:
  3133. ctrl_grab = 1;
  3134. break;
  3135. case QEMU_OPTION_no_quit:
  3136. dpy.has_window_close = true;
  3137. dpy.window_close = false;
  3138. break;
  3139. case QEMU_OPTION_sdl:
  3140. #ifdef CONFIG_SDL
  3141. dpy.type = DISPLAY_TYPE_SDL;
  3142. break;
  3143. #else
  3144. error_report("SDL support is disabled");
  3145. exit(1);
  3146. #endif
  3147. case QEMU_OPTION_pidfile:
  3148. pid_file = optarg;
  3149. break;
  3150. case QEMU_OPTION_win2k_hack:
  3151. win2k_install_hack = 1;
  3152. break;
  3153. case QEMU_OPTION_acpitable:
  3154. opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
  3155. optarg, true);
  3156. if (!opts) {
  3157. exit(1);
  3158. }
  3159. acpi_table_add(opts, &error_fatal);
  3160. break;
  3161. case QEMU_OPTION_smbios:
  3162. opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
  3163. optarg, false);
  3164. if (!opts) {
  3165. exit(1);
  3166. }
  3167. smbios_entry_add(opts, &error_fatal);
  3168. break;
  3169. case QEMU_OPTION_fwcfg:
  3170. opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
  3171. optarg, true);
  3172. if (opts == NULL) {
  3173. exit(1);
  3174. }
  3175. break;
  3176. case QEMU_OPTION_preconfig:
  3177. preconfig_exit_requested = false;
  3178. break;
  3179. case QEMU_OPTION_enable_kvm:
  3180. olist = qemu_find_opts("machine");
  3181. qemu_opts_parse_noisily(olist, "accel=kvm", false);
  3182. break;
  3183. case QEMU_OPTION_M:
  3184. case QEMU_OPTION_machine:
  3185. olist = qemu_find_opts("machine");
  3186. opts = qemu_opts_parse_noisily(olist, optarg, true);
  3187. if (!opts) {
  3188. exit(1);
  3189. }
  3190. break;
  3191. case QEMU_OPTION_no_kvm:
  3192. olist = qemu_find_opts("machine");
  3193. qemu_opts_parse_noisily(olist, "accel=tcg", false);
  3194. break;
  3195. case QEMU_OPTION_accel:
  3196. accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
  3197. optarg, true);
  3198. optarg = qemu_opt_get(accel_opts, "accel");
  3199. if (!optarg || is_help_option(optarg)) {
  3200. error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
  3201. exit(0);
  3202. }
  3203. opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
  3204. false, &error_abort);
  3205. qemu_opt_set(opts, "accel", optarg, &error_abort);
  3206. break;
  3207. case QEMU_OPTION_usb:
  3208. olist = qemu_find_opts("machine");
  3209. qemu_opts_parse_noisily(olist, "usb=on", false);
  3210. break;
  3211. case QEMU_OPTION_usbdevice:
  3212. error_report("'-usbdevice' is deprecated, please use "
  3213. "'-device usb-...' instead");
  3214. olist = qemu_find_opts("machine");
  3215. qemu_opts_parse_noisily(olist, "usb=on", false);
  3216. add_device_config(DEV_USB, optarg);
  3217. break;
  3218. case QEMU_OPTION_device:
  3219. if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
  3220. optarg, true)) {
  3221. exit(1);
  3222. }
  3223. break;
  3224. case QEMU_OPTION_smp:
  3225. if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
  3226. optarg, true)) {
  3227. exit(1);
  3228. }
  3229. break;
  3230. case QEMU_OPTION_vnc:
  3231. vnc_parse(optarg, &error_fatal);
  3232. break;
  3233. case QEMU_OPTION_no_acpi:
  3234. acpi_enabled = 0;
  3235. break;
  3236. case QEMU_OPTION_no_hpet:
  3237. no_hpet = 1;
  3238. break;
  3239. case QEMU_OPTION_no_reboot:
  3240. no_reboot = 1;
  3241. break;
  3242. case QEMU_OPTION_no_shutdown:
  3243. no_shutdown = 1;
  3244. break;
  3245. case QEMU_OPTION_show_cursor:
  3246. cursor_hide = 0;
  3247. break;
  3248. case QEMU_OPTION_uuid:
  3249. if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
  3250. error_report("failed to parse UUID string: wrong format");
  3251. exit(1);
  3252. }
  3253. qemu_uuid_set = true;
  3254. break;
  3255. case QEMU_OPTION_option_rom:
  3256. if (nb_option_roms >= MAX_OPTION_ROMS) {
  3257. error_report("too many option ROMs");
  3258. exit(1);
  3259. }
  3260. opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
  3261. optarg, true);
  3262. if (!opts) {
  3263. exit(1);
  3264. }
  3265. option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
  3266. option_rom[nb_option_roms].bootindex =
  3267. qemu_opt_get_number(opts, "bootindex", -1);
  3268. if (!option_rom[nb_option_roms].name) {
  3269. error_report("Option ROM file is not specified");
  3270. exit(1);
  3271. }
  3272. nb_option_roms++;
  3273. break;
  3274. case QEMU_OPTION_semihosting:
  3275. semihosting.enabled = true;
  3276. semihosting.target = SEMIHOSTING_TARGET_AUTO;
  3277. break;
  3278. case QEMU_OPTION_semihosting_config:
  3279. semihosting.enabled = true;
  3280. opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
  3281. optarg, false);
  3282. if (opts != NULL) {
  3283. semihosting.enabled = qemu_opt_get_bool(opts, "enable",
  3284. true);
  3285. const char *target = qemu_opt_get(opts, "target");
  3286. if (target != NULL) {
  3287. if (strcmp("native", target) == 0) {
  3288. semihosting.target = SEMIHOSTING_TARGET_NATIVE;
  3289. } else if (strcmp("gdb", target) == 0) {
  3290. semihosting.target = SEMIHOSTING_TARGET_GDB;
  3291. } else if (strcmp("auto", target) == 0) {
  3292. semihosting.target = SEMIHOSTING_TARGET_AUTO;
  3293. } else {
  3294. error_report("unsupported semihosting-config %s",
  3295. optarg);
  3296. exit(1);
  3297. }
  3298. } else {
  3299. semihosting.target = SEMIHOSTING_TARGET_AUTO;
  3300. }
  3301. /* Set semihosting argument count and vector */
  3302. qemu_opt_foreach(opts, add_semihosting_arg,
  3303. &semihosting, NULL);
  3304. } else {
  3305. error_report("unsupported semihosting-config %s", optarg);
  3306. exit(1);
  3307. }
  3308. break;
  3309. case QEMU_OPTION_name:
  3310. opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
  3311. optarg, true);
  3312. if (!opts) {
  3313. exit(1);
  3314. }
  3315. break;
  3316. case QEMU_OPTION_prom_env:
  3317. if (nb_prom_envs >= MAX_PROM_ENVS) {
  3318. error_report("too many prom variables");
  3319. exit(1);
  3320. }
  3321. prom_envs[nb_prom_envs] = optarg;
  3322. nb_prom_envs++;
  3323. break;
  3324. case QEMU_OPTION_old_param:
  3325. old_param = 1;
  3326. break;
  3327. case QEMU_OPTION_rtc:
  3328. opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
  3329. false);
  3330. if (!opts) {
  3331. exit(1);
  3332. }
  3333. break;
  3334. case QEMU_OPTION_tb_size:
  3335. #ifndef CONFIG_TCG
  3336. error_report("TCG is disabled");
  3337. exit(1);
  3338. #endif
  3339. if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
  3340. error_report("Invalid argument to -tb-size");
  3341. exit(1);
  3342. }
  3343. break;
  3344. case QEMU_OPTION_icount:
  3345. icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
  3346. optarg, true);
  3347. if (!icount_opts) {
  3348. exit(1);
  3349. }
  3350. break;
  3351. case QEMU_OPTION_incoming:
  3352. if (!incoming) {
  3353. runstate_set(RUN_STATE_INMIGRATE);
  3354. }
  3355. incoming = optarg;
  3356. break;
  3357. case QEMU_OPTION_only_migratable:
  3358. /*
  3359. * TODO: we can remove this option one day, and we
  3360. * should all use:
  3361. *
  3362. * "-global migration.only-migratable=true"
  3363. */
  3364. qemu_global_option("migration.only-migratable=true");
  3365. break;
  3366. case QEMU_OPTION_nodefaults:
  3367. has_defaults = 0;
  3368. break;
  3369. case QEMU_OPTION_xen_domid:
  3370. if (!(xen_available())) {
  3371. error_report("Option not supported for this target");
  3372. exit(1);
  3373. }
  3374. xen_domid = atoi(optarg);
  3375. break;
  3376. case QEMU_OPTION_xen_attach:
  3377. if (!(xen_available())) {
  3378. error_report("Option not supported for this target");
  3379. exit(1);
  3380. }
  3381. xen_mode = XEN_ATTACH;
  3382. break;
  3383. case QEMU_OPTION_xen_domid_restrict:
  3384. if (!(xen_available())) {
  3385. error_report("Option not supported for this target");
  3386. exit(1);
  3387. }
  3388. xen_domid_restrict = true;
  3389. break;
  3390. case QEMU_OPTION_trace:
  3391. g_free(trace_file);
  3392. trace_file = trace_opt_parse(optarg);
  3393. break;
  3394. case QEMU_OPTION_readconfig:
  3395. {
  3396. int ret = qemu_read_config_file(optarg);
  3397. if (ret < 0) {
  3398. error_report("read config %s: %s", optarg,
  3399. strerror(-ret));
  3400. exit(1);
  3401. }
  3402. break;
  3403. }
  3404. case QEMU_OPTION_spice:
  3405. olist = qemu_find_opts("spice");
  3406. if (!olist) {
  3407. error_report("spice support is disabled");
  3408. exit(1);
  3409. }
  3410. opts = qemu_opts_parse_noisily(olist, optarg, false);
  3411. if (!opts) {
  3412. exit(1);
  3413. }
  3414. display_remote++;
  3415. break;
  3416. case QEMU_OPTION_writeconfig:
  3417. {
  3418. FILE *fp;
  3419. if (strcmp(optarg, "-") == 0) {
  3420. fp = stdout;
  3421. } else {
  3422. fp = fopen(optarg, "w");
  3423. if (fp == NULL) {
  3424. error_report("open %s: %s", optarg,
  3425. strerror(errno));
  3426. exit(1);
  3427. }
  3428. }
  3429. qemu_config_write(fp);
  3430. if (fp != stdout) {
  3431. fclose(fp);
  3432. }
  3433. break;
  3434. }
  3435. case QEMU_OPTION_qtest:
  3436. qtest_chrdev = optarg;
  3437. break;
  3438. case QEMU_OPTION_qtest_log:
  3439. qtest_log = optarg;
  3440. break;
  3441. case QEMU_OPTION_sandbox:
  3442. #ifdef CONFIG_SECCOMP
  3443. opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
  3444. optarg, true);
  3445. if (!opts) {
  3446. exit(1);
  3447. }
  3448. #else
  3449. error_report("-sandbox support is not enabled "
  3450. "in this QEMU binary");
  3451. exit(1);
  3452. #endif
  3453. break;
  3454. case QEMU_OPTION_add_fd:
  3455. #ifndef _WIN32
  3456. opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
  3457. optarg, false);
  3458. if (!opts) {
  3459. exit(1);
  3460. }
  3461. #else
  3462. error_report("File descriptor passing is disabled on this "
  3463. "platform");
  3464. exit(1);
  3465. #endif
  3466. break;
  3467. case QEMU_OPTION_object:
  3468. opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
  3469. optarg, true);
  3470. if (!opts) {
  3471. exit(1);
  3472. }
  3473. break;
  3474. case QEMU_OPTION_realtime:
  3475. opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
  3476. optarg, false);
  3477. if (!opts) {
  3478. exit(1);
  3479. }
  3480. /* Don't override the -overcommit option if set */
  3481. enable_mlock = enable_mlock ||
  3482. qemu_opt_get_bool(opts, "mlock", true);
  3483. break;
  3484. case QEMU_OPTION_overcommit:
  3485. opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
  3486. optarg, false);
  3487. if (!opts) {
  3488. exit(1);
  3489. }
  3490. /* Don't override the -realtime option if set */
  3491. enable_mlock = enable_mlock ||
  3492. qemu_opt_get_bool(opts, "mem-lock", false);
  3493. enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
  3494. break;
  3495. case QEMU_OPTION_msg:
  3496. opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
  3497. false);
  3498. if (!opts) {
  3499. exit(1);
  3500. }
  3501. configure_msg(opts);
  3502. break;
  3503. case QEMU_OPTION_dump_vmstate:
  3504. if (vmstate_dump_file) {
  3505. error_report("only one '-dump-vmstate' "
  3506. "option may be given");
  3507. exit(1);
  3508. }
  3509. vmstate_dump_file = fopen(optarg, "w");
  3510. if (vmstate_dump_file == NULL) {
  3511. error_report("open %s: %s", optarg, strerror(errno));
  3512. exit(1);
  3513. }
  3514. break;
  3515. case QEMU_OPTION_enable_sync_profile:
  3516. qsp_enable();
  3517. break;
  3518. case QEMU_OPTION_nouserconfig:
  3519. /* Nothing to be parsed here. Especially, do not error out below. */
  3520. break;
  3521. default:
  3522. if (os_parse_cmd_args(popt->index, optarg)) {
  3523. error_report("Option not supported in this build");
  3524. exit(1);
  3525. }
  3526. }
  3527. }
  3528. }
  3529. /*
  3530. * Clear error location left behind by the loop.
  3531. * Best done right after the loop. Do not insert code here!
  3532. */
  3533. loc_set_none();
  3534. replay_configure(icount_opts);
  3535. if (incoming && !preconfig_exit_requested) {
  3536. error_report("'preconfig' and 'incoming' options are "
  3537. "mutually exclusive");
  3538. exit(EXIT_FAILURE);
  3539. }
  3540. configure_rtc(qemu_find_opts_singleton("rtc"));
  3541. machine_class = select_machine();
  3542. set_memory_options(&ram_slots, &maxram_size, machine_class);
  3543. os_daemonize();
  3544. rcu_disable_atfork();
  3545. if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
  3546. error_reportf_err(err, "cannot create PID file: ");
  3547. exit(1);
  3548. }
  3549. qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
  3550. qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
  3551. if (qemu_init_main_loop(&main_loop_err)) {
  3552. error_report_err(main_loop_err);
  3553. exit(1);
  3554. }
  3555. #ifdef CONFIG_SECCOMP
  3556. olist = qemu_find_opts_err("sandbox", NULL);
  3557. if (olist) {
  3558. qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
  3559. }
  3560. #endif
  3561. qemu_opts_foreach(qemu_find_opts("name"),
  3562. parse_name, NULL, &error_fatal);
  3563. #ifndef _WIN32
  3564. qemu_opts_foreach(qemu_find_opts("add-fd"),
  3565. parse_add_fd, NULL, &error_fatal);
  3566. qemu_opts_foreach(qemu_find_opts("add-fd"),
  3567. cleanup_add_fd, NULL, &error_fatal);
  3568. #endif
  3569. current_machine = MACHINE(object_new(object_class_get_name(
  3570. OBJECT_CLASS(machine_class))));
  3571. if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
  3572. exit(0);
  3573. }
  3574. object_property_add_child(object_get_root(), "machine",
  3575. OBJECT(current_machine), &error_abort);
  3576. if (machine_class->minimum_page_bits) {
  3577. if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
  3578. /* This would be a board error: specifying a minimum smaller than
  3579. * a target's compile-time fixed setting.
  3580. */
  3581. g_assert_not_reached();
  3582. }
  3583. }
  3584. cpu_exec_init_all();
  3585. if (machine_class->hw_version) {
  3586. qemu_set_hw_version(machine_class->hw_version);
  3587. }
  3588. if (cpu_model && is_help_option(cpu_model)) {
  3589. list_cpus(stdout, &fprintf, cpu_model);
  3590. exit(0);
  3591. }
  3592. if (!trace_init_backends()) {
  3593. exit(1);
  3594. }
  3595. trace_init_file(trace_file);
  3596. /* Open the logfile at this point and set the log mask if necessary.
  3597. */
  3598. if (log_file) {
  3599. qemu_set_log_filename(log_file, &error_fatal);
  3600. }
  3601. if (log_mask) {
  3602. int mask;
  3603. mask = qemu_str_to_log_mask(log_mask);
  3604. if (!mask) {
  3605. qemu_print_log_usage(stdout);
  3606. exit(1);
  3607. }
  3608. qemu_set_log(mask);
  3609. } else {
  3610. qemu_set_log(0);
  3611. }
  3612. /* add configured firmware directories */
  3613. dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
  3614. for (i = 0; dirs[i] != NULL; i++) {
  3615. qemu_add_data_dir(dirs[i]);
  3616. }
  3617. g_strfreev(dirs);
  3618. /* try to find datadir relative to the executable path */
  3619. dir = os_find_datadir();
  3620. qemu_add_data_dir(dir);
  3621. g_free(dir);
  3622. /* add the datadir specified when building */
  3623. qemu_add_data_dir(CONFIG_QEMU_DATADIR);
  3624. /* -L help lists the data directories and exits. */
  3625. if (list_data_dirs) {
  3626. for (i = 0; i < data_dir_idx; i++) {
  3627. printf("%s\n", data_dir[i]);
  3628. }
  3629. exit(0);
  3630. }
  3631. /* machine_class: default to UP */
  3632. machine_class->max_cpus = machine_class->max_cpus ?: 1;
  3633. machine_class->min_cpus = machine_class->min_cpus ?: 1;
  3634. machine_class->default_cpus = machine_class->default_cpus ?: 1;
  3635. /* default to machine_class->default_cpus */
  3636. smp_cpus = machine_class->default_cpus;
  3637. max_cpus = machine_class->default_cpus;
  3638. smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
  3639. /* sanity-check smp_cpus and max_cpus against machine_class */
  3640. if (smp_cpus < machine_class->min_cpus) {
  3641. error_report("Invalid SMP CPUs %d. The min CPUs "
  3642. "supported by machine '%s' is %d", smp_cpus,
  3643. machine_class->name, machine_class->min_cpus);
  3644. exit(1);
  3645. }
  3646. if (max_cpus > machine_class->max_cpus) {
  3647. error_report("Invalid SMP CPUs %d. The max CPUs "
  3648. "supported by machine '%s' is %d", max_cpus,
  3649. machine_class->name, machine_class->max_cpus);
  3650. exit(1);
  3651. }
  3652. /*
  3653. * Get the default machine options from the machine if it is not already
  3654. * specified either by the configuration file or by the command line.
  3655. */
  3656. if (machine_class->default_machine_opts) {
  3657. qemu_opts_set_defaults(qemu_find_opts("machine"),
  3658. machine_class->default_machine_opts, 0);
  3659. }
  3660. qemu_opts_foreach(qemu_find_opts("device"),
  3661. default_driver_check, NULL, NULL);
  3662. qemu_opts_foreach(qemu_find_opts("global"),
  3663. default_driver_check, NULL, NULL);
  3664. if (!vga_model && !default_vga) {
  3665. vga_interface_type = VGA_DEVICE;
  3666. }
  3667. if (!has_defaults || machine_class->no_serial) {
  3668. default_serial = 0;
  3669. }
  3670. if (!has_defaults || machine_class->no_parallel) {
  3671. default_parallel = 0;
  3672. }
  3673. if (!has_defaults || machine_class->no_floppy) {
  3674. default_floppy = 0;
  3675. }
  3676. if (!has_defaults || machine_class->no_cdrom) {
  3677. default_cdrom = 0;
  3678. }
  3679. if (!has_defaults || machine_class->no_sdcard) {
  3680. default_sdcard = 0;
  3681. }
  3682. if (!has_defaults) {
  3683. default_monitor = 0;
  3684. default_net = 0;
  3685. default_vga = 0;
  3686. }
  3687. if (is_daemonized()) {
  3688. if (!preconfig_exit_requested) {
  3689. error_report("'preconfig' and 'daemonize' options are "
  3690. "mutually exclusive");
  3691. exit(EXIT_FAILURE);
  3692. }
  3693. /* According to documentation and historically, -nographic redirects
  3694. * serial port, parallel port and monitor to stdio, which does not work
  3695. * with -daemonize. We can redirect these to null instead, but since
  3696. * -nographic is legacy, let's just error out.
  3697. * We disallow -nographic only if all other ports are not redirected
  3698. * explicitly, to not break existing legacy setups which uses
  3699. * -nographic _and_ redirects all ports explicitly - this is valid
  3700. * usage, -nographic is just a no-op in this case.
  3701. */
  3702. if (nographic
  3703. && (default_parallel || default_serial || default_monitor)) {
  3704. error_report("-nographic cannot be used with -daemonize");
  3705. exit(1);
  3706. }
  3707. #ifdef CONFIG_CURSES
  3708. if (dpy.type == DISPLAY_TYPE_CURSES) {
  3709. error_report("curses display cannot be used with -daemonize");
  3710. exit(1);
  3711. }
  3712. #endif
  3713. }
  3714. if (nographic) {
  3715. if (default_parallel)
  3716. add_device_config(DEV_PARALLEL, "null");
  3717. if (default_serial && default_monitor) {
  3718. add_device_config(DEV_SERIAL, "mon:stdio");
  3719. } else {
  3720. if (default_serial)
  3721. add_device_config(DEV_SERIAL, "stdio");
  3722. if (default_monitor)
  3723. monitor_parse("stdio", "readline", false);
  3724. }
  3725. } else {
  3726. if (default_serial)
  3727. add_device_config(DEV_SERIAL, "vc:80Cx24C");
  3728. if (default_parallel)
  3729. add_device_config(DEV_PARALLEL, "vc:80Cx24C");
  3730. if (default_monitor)
  3731. monitor_parse("vc:80Cx24C", "readline", false);
  3732. }
  3733. #if defined(CONFIG_VNC)
  3734. if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
  3735. display_remote++;
  3736. }
  3737. #endif
  3738. if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
  3739. if (!qemu_display_find_default(&dpy)) {
  3740. dpy.type = DISPLAY_TYPE_NONE;
  3741. #if defined(CONFIG_VNC)
  3742. vnc_parse("localhost:0,to=99,id=default", &error_abort);
  3743. #endif
  3744. }
  3745. }
  3746. if (dpy.type == DISPLAY_TYPE_DEFAULT) {
  3747. dpy.type = DISPLAY_TYPE_NONE;
  3748. }
  3749. if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
  3750. error_report("-alt-grab and -ctrl-grab are only valid "
  3751. "for SDL, ignoring option");
  3752. }
  3753. if (dpy.has_window_close &&
  3754. (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
  3755. error_report("-no-quit is only valid for GTK and SDL, "
  3756. "ignoring option");
  3757. }
  3758. qemu_display_early_init(&dpy);
  3759. qemu_console_early_init();
  3760. if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
  3761. #if defined(CONFIG_OPENGL)
  3762. error_report("OpenGL is not supported by the display");
  3763. #else
  3764. error_report("OpenGL support is disabled");
  3765. #endif
  3766. exit(1);
  3767. }
  3768. page_size_init();
  3769. socket_init();
  3770. qemu_opts_foreach(qemu_find_opts("object"),
  3771. user_creatable_add_opts_foreach,
  3772. object_create_initial, &error_fatal);
  3773. qemu_opts_foreach(qemu_find_opts("chardev"),
  3774. chardev_init_func, NULL, &error_fatal);
  3775. #ifdef CONFIG_VIRTFS
  3776. qemu_opts_foreach(qemu_find_opts("fsdev"),
  3777. fsdev_init_func, NULL, &error_fatal);
  3778. #endif
  3779. if (qemu_opts_foreach(qemu_find_opts("device"),
  3780. device_help_func, NULL, NULL)) {
  3781. exit(0);
  3782. }
  3783. machine_opts = qemu_get_machine_opts();
  3784. qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
  3785. &error_fatal);
  3786. current_machine->ram_size = ram_size;
  3787. current_machine->maxram_size = maxram_size;
  3788. current_machine->ram_slots = ram_slots;
  3789. configure_accelerator(current_machine, argv[0]);
  3790. if (!qtest_enabled() && machine_class->deprecation_reason) {
  3791. error_report("Machine type '%s' is deprecated: %s",
  3792. machine_class->name, machine_class->deprecation_reason);
  3793. }
  3794. /*
  3795. * Register all the global properties, including accel properties,
  3796. * machine properties, and user-specified ones.
  3797. */
  3798. register_global_properties(current_machine);
  3799. /*
  3800. * Migration object can only be created after global properties
  3801. * are applied correctly.
  3802. */
  3803. migration_object_init();
  3804. if (qtest_chrdev) {
  3805. qtest_init(qtest_chrdev, qtest_log, &error_fatal);
  3806. }
  3807. machine_opts = qemu_get_machine_opts();
  3808. kernel_filename = qemu_opt_get(machine_opts, "kernel");
  3809. initrd_filename = qemu_opt_get(machine_opts, "initrd");
  3810. kernel_cmdline = qemu_opt_get(machine_opts, "append");
  3811. bios_name = qemu_opt_get(machine_opts, "firmware");
  3812. opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
  3813. if (opts) {
  3814. boot_order = qemu_opt_get(opts, "order");
  3815. if (boot_order) {
  3816. validate_bootdevices(boot_order, &error_fatal);
  3817. }
  3818. boot_once = qemu_opt_get(opts, "once");
  3819. if (boot_once) {
  3820. validate_bootdevices(boot_once, &error_fatal);
  3821. }
  3822. boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
  3823. boot_strict = qemu_opt_get_bool(opts, "strict", false);
  3824. }
  3825. if (!boot_order) {
  3826. boot_order = machine_class->default_boot_order;
  3827. }
  3828. if (!kernel_cmdline) {
  3829. kernel_cmdline = "";
  3830. current_machine->kernel_cmdline = (char *)kernel_cmdline;
  3831. }
  3832. linux_boot = (kernel_filename != NULL);
  3833. if (!linux_boot && *kernel_cmdline != '\0') {
  3834. error_report("-append only allowed with -kernel option");
  3835. exit(1);
  3836. }
  3837. if (!linux_boot && initrd_filename != NULL) {
  3838. error_report("-initrd only allowed with -kernel option");
  3839. exit(1);
  3840. }
  3841. if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
  3842. /* fall back to the -kernel/-append */
  3843. semihosting_arg_fallback(kernel_filename, kernel_cmdline);
  3844. }
  3845. os_set_line_buffering();
  3846. /* spice needs the timers to be initialized by this point */
  3847. qemu_spice_init();
  3848. cpu_ticks_init();
  3849. if (icount_opts) {
  3850. if (!tcg_enabled()) {
  3851. error_report("-icount is not allowed with hardware virtualization");
  3852. exit(1);
  3853. }
  3854. configure_icount(icount_opts, &error_abort);
  3855. qemu_opts_del(icount_opts);
  3856. }
  3857. if (tcg_enabled()) {
  3858. qemu_tcg_configure(accel_opts, &error_fatal);
  3859. }
  3860. if (default_net) {
  3861. QemuOptsList *net = qemu_find_opts("net");
  3862. qemu_opts_set(net, NULL, "type", "nic", &error_abort);
  3863. #ifdef CONFIG_SLIRP
  3864. qemu_opts_set(net, NULL, "type", "user", &error_abort);
  3865. #endif
  3866. }
  3867. if (net_init_clients(&err) < 0) {
  3868. error_report_err(err);
  3869. exit(1);
  3870. }
  3871. qemu_opts_foreach(qemu_find_opts("object"),
  3872. user_creatable_add_opts_foreach,
  3873. object_create_delayed, &error_fatal);
  3874. tpm_init();
  3875. /* init the bluetooth world */
  3876. if (foreach_device_config(DEV_BT, bt_parse))
  3877. exit(1);
  3878. if (!xen_enabled()) {
  3879. /* On 32-bit hosts, QEMU is limited by virtual address space */
  3880. if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
  3881. error_report("at most 2047 MB RAM can be simulated");
  3882. exit(1);
  3883. }
  3884. }
  3885. blk_mig_init();
  3886. ram_mig_init();
  3887. dirty_bitmap_mig_init();
  3888. /* If the currently selected machine wishes to override the units-per-bus
  3889. * property of its default HBA interface type, do so now. */
  3890. if (machine_class->units_per_default_bus) {
  3891. override_max_devs(machine_class->block_default_type,
  3892. machine_class->units_per_default_bus);
  3893. }
  3894. /* open the virtual block devices */
  3895. while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
  3896. BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
  3897. QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
  3898. loc_push_restore(&bdo->loc);
  3899. qmp_blockdev_add(bdo->bdo, &error_fatal);
  3900. loc_pop(&bdo->loc);
  3901. qapi_free_BlockdevOptions(bdo->bdo);
  3902. g_free(bdo);
  3903. }
  3904. if (snapshot || replay_mode != REPLAY_MODE_NONE) {
  3905. qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
  3906. NULL, NULL);
  3907. }
  3908. if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
  3909. &machine_class->block_default_type, &error_fatal)) {
  3910. /* We printed help */
  3911. exit(0);
  3912. }
  3913. default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
  3914. CDROM_OPTS);
  3915. default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
  3916. default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
  3917. qemu_opts_foreach(qemu_find_opts("mon"),
  3918. mon_init_func, NULL, &error_fatal);
  3919. if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
  3920. exit(1);
  3921. if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
  3922. exit(1);
  3923. if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
  3924. exit(1);
  3925. /* If no default VGA is requested, the default is "none". */
  3926. if (default_vga) {
  3927. if (machine_class->default_display) {
  3928. vga_model = machine_class->default_display;
  3929. } else if (vga_interface_available(VGA_CIRRUS)) {
  3930. vga_model = "cirrus";
  3931. } else if (vga_interface_available(VGA_STD)) {
  3932. vga_model = "std";
  3933. }
  3934. }
  3935. if (vga_model) {
  3936. select_vgahw(vga_model);
  3937. }
  3938. if (watchdog) {
  3939. i = select_watchdog(watchdog);
  3940. if (i > 0)
  3941. exit (i == 1 ? 1 : 0);
  3942. }
  3943. /* This checkpoint is required by replay to separate prior clock
  3944. reading from the other reads, because timer polling functions query
  3945. clock values from the log. */
  3946. replay_checkpoint(CHECKPOINT_INIT);
  3947. qdev_machine_init();
  3948. current_machine->boot_order = boot_order;
  3949. /* parse features once if machine provides default cpu_type */
  3950. current_machine->cpu_type = machine_class->default_cpu_type;
  3951. if (cpu_model) {
  3952. current_machine->cpu_type = parse_cpu_model(cpu_model);
  3953. }
  3954. parse_numa_opts(current_machine);
  3955. /* do monitor/qmp handling at preconfig state if requested */
  3956. main_loop();
  3957. /* from here on runstate is RUN_STATE_PRELAUNCH */
  3958. machine_run_board_init(current_machine);
  3959. realtime_init();
  3960. soundhw_init();
  3961. if (hax_enabled()) {
  3962. hax_sync_vcpus();
  3963. }
  3964. qemu_opts_foreach(qemu_find_opts("fw_cfg"),
  3965. parse_fw_cfg, fw_cfg_find(), &error_fatal);
  3966. /* init USB devices */
  3967. if (machine_usb(current_machine)) {
  3968. if (foreach_device_config(DEV_USB, usb_parse) < 0)
  3969. exit(1);
  3970. }
  3971. /* Check if IGD GFX passthrough. */
  3972. igd_gfx_passthru();
  3973. /* init generic devices */
  3974. rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
  3975. qemu_opts_foreach(qemu_find_opts("device"),
  3976. device_init_func, NULL, &error_fatal);
  3977. cpu_synchronize_all_post_init();
  3978. rom_reset_order_override();
  3979. /* Did we create any drives that we failed to create a device for? */
  3980. drive_check_orphaned();
  3981. /* Don't warn about the default network setup that you get if
  3982. * no command line -net or -netdev options are specified. There
  3983. * are two cases that we would otherwise complain about:
  3984. * (1) board doesn't support a NIC but the implicit "-net nic"
  3985. * requested one
  3986. * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
  3987. * sets up a nic that isn't connected to anything.
  3988. */
  3989. if (!default_net && (!qtest_enabled() || has_defaults)) {
  3990. net_check_clients();
  3991. }
  3992. if (boot_once) {
  3993. qemu_boot_set(boot_once, &error_fatal);
  3994. qemu_register_reset(restore_boot_order, g_strdup(boot_order));
  3995. }
  3996. /* init local displays */
  3997. ds = init_displaystate();
  3998. qemu_display_init(ds, &dpy);
  3999. /* must be after terminal init, SDL library changes signal handlers */
  4000. os_setup_signal_handling();
  4001. /* init remote displays */
  4002. #ifdef CONFIG_VNC
  4003. qemu_opts_foreach(qemu_find_opts("vnc"),
  4004. vnc_init_func, NULL, &error_fatal);
  4005. #endif
  4006. if (using_spice) {
  4007. qemu_spice_display_init();
  4008. }
  4009. if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
  4010. exit(1);
  4011. }
  4012. qdev_machine_creation_done();
  4013. /* TODO: once all bus devices are qdevified, this should be done
  4014. * when bus is created by qdev.c */
  4015. qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
  4016. qemu_run_machine_init_done_notifiers();
  4017. if (rom_check_and_register_reset() != 0) {
  4018. error_report("rom check and register reset failed");
  4019. exit(1);
  4020. }
  4021. replay_start();
  4022. /* This checkpoint is required by replay to separate prior clock
  4023. reading from the other reads, because timer polling functions query
  4024. clock values from the log. */
  4025. replay_checkpoint(CHECKPOINT_RESET);
  4026. qemu_system_reset(SHUTDOWN_CAUSE_NONE);
  4027. register_global_state();
  4028. if (loadvm) {
  4029. Error *local_err = NULL;
  4030. if (load_snapshot(loadvm, &local_err) < 0) {
  4031. error_report_err(local_err);
  4032. autostart = 0;
  4033. exit(1);
  4034. }
  4035. }
  4036. if (replay_mode != REPLAY_MODE_NONE) {
  4037. replay_vmstate_init();
  4038. }
  4039. qdev_prop_check_globals();
  4040. if (vmstate_dump_file) {
  4041. /* dump and exit */
  4042. dump_vmstate_json_to_file(vmstate_dump_file);
  4043. return 0;
  4044. }
  4045. if (incoming) {
  4046. Error *local_err = NULL;
  4047. qemu_start_incoming_migration(incoming, &local_err);
  4048. if (local_err) {
  4049. error_reportf_err(local_err, "-incoming %s: ", incoming);
  4050. exit(1);
  4051. }
  4052. } else if (autostart) {
  4053. vm_start();
  4054. }
  4055. accel_setup_post(current_machine);
  4056. os_setup_post();
  4057. main_loop();
  4058. gdbserver_cleanup();
  4059. /*
  4060. * cleaning up the migration object cancels any existing migration
  4061. * try to do this early so that it also stops using devices.
  4062. */
  4063. migration_shutdown();
  4064. /* No more vcpu or device emulation activity beyond this point */
  4065. vm_shutdown();
  4066. job_cancel_sync_all();
  4067. bdrv_close_all();
  4068. res_free();
  4069. /* vhost-user must be cleaned up before chardevs. */
  4070. tpm_cleanup();
  4071. net_cleanup();
  4072. audio_cleanup();
  4073. monitor_cleanup();
  4074. qemu_chr_cleanup();
  4075. user_creatable_cleanup();
  4076. /* TODO: unref root container, check all devices are ok */
  4077. return 0;
  4078. }