]> git.ipfire.org Git - thirdparty/qemu.git/blame - softmmu/vl.c
vl: extract various command line validation snippets to a new function
[thirdparty/qemu.git] / softmmu / vl.c
CommitLineData
0824d6fc 1/*
80cabfad 2 * QEMU System Emulator
5fafdf24 3 *
68d0f70e 4 * Copyright (c) 2003-2008 Fabrice Bellard
5fafdf24 5 *
1df912cf
FB
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.
0824d6fc 23 */
e688df6b 24
d38ea87a 25#include "qemu/osdep.h"
a8d25326 26#include "qemu-common.h"
3dc54b0e 27#include "qemu/units.h"
fe48442f 28#include "hw/boards.h"
a27bd6c7 29#include "hw/qdev-properties.h"
e688df6b 30#include "qapi/error.h"
67a1de0d 31#include "qemu-version.h"
f348b6d1
VB
32#include "qemu/cutils.h"
33#include "qemu/help_option.h"
cea25275 34#include "qemu/uuid.h"
71e8a915 35#include "sysemu/reset.h"
54d31236 36#include "sysemu/runstate.h"
9c17d615 37#include "sysemu/seccomp.h"
14a48c1d 38#include "sysemu/tcg.h"
da278d58 39#include "sysemu/xen.h"
452dfbef 40
d49b6836 41#include "qemu/error-report.h"
c8897e8e 42#include "qemu/sockets.h"
a1a9cb0c 43#include "sysemu/accel.h"
511d2b14 44#include "hw/usb.h"
0d09e41a 45#include "hw/isa/isa.h"
fb8b660e 46#include "hw/scsi/scsi.h"
866e2b37 47#include "hw/display/vga.h"
0d09e41a 48#include "sysemu/watchdog.h"
a2eb5c0c 49#include "hw/firmware/smbios.h"
9f57061c 50#include "hw/acpi/acpi.h"
0d09e41a 51#include "hw/xen/xen.h"
45a50b16 52#include "hw/loader.h"
b4a42f81 53#include "monitor/qdev.h"
1422e32d 54#include "net/net.h"
68ac40d2 55#include "net/slirp.h"
83c9089e 56#include "monitor/monitor.h"
28ecbaee 57#include "ui/console.h"
e0d2bd51 58#include "ui/input.h"
9c17d615 59#include "sysemu/sysemu.h"
e35704ba 60#include "sysemu/numa.h"
900c0ba3 61#include "sysemu/hostmem.h"
022c62cb 62#include "exec/gdbstub.h"
1de7afc9 63#include "qemu/timer.h"
8228e353 64#include "chardev/char.h"
b33276a7 65#include "qemu/bitmap.h"
03dd024f 66#include "qemu/log.h"
9c17d615 67#include "sysemu/blockdev.h"
0d09e41a 68#include "hw/block/block.h"
7b1e1a22 69#include "migration/misc.h"
5e22479a 70#include "migration/snapshot.h"
84a899de 71#include "migration/global_state.h"
bdee56f5 72#include "sysemu/tpm.h"
9c17d615 73#include "sysemu/dma.h"
8a824e4d 74#include "hw/audio/soundhw.h"
511d2b14 75#include "audio/audio.h"
4b4629d9 76#include "sysemu/cpus.h"
740b1759 77#include "sysemu/cpu-timers.h"
5821ebf9 78#include "migration/colo.h"
1693c64c 79#include "migration/postcopy-ram.h"
9c17d615 80#include "sysemu/kvm.h"
b0cb0a66 81#include "sysemu/hax.h"
42e5f393 82#include "qapi/qobject-input-visitor.h"
1de7afc9
PB
83#include "qemu/option.h"
84#include "qemu/config-file.h"
59a5264b 85#include "qemu-options.h"
1de7afc9 86#include "qemu/main-loop.h"
758e8e38 87#ifdef CONFIG_VIRTFS
74db920c
GS
88#include "fsdev/qemu-fsdev.h"
89#endif
9c17d615 90#include "sysemu/qtest.h"
511d2b14 91
76cad711 92#include "disas/disas.h"
fc01f7e7 93
8b7a5507 94#include "trace.h"
e4858974 95#include "trace/control.h"
42229a75 96#include "qemu/plugin.h"
1de7afc9 97#include "qemu/queue.h"
9c17d615 98#include "sysemu/arch_init.h"
72cf2d4f 99
29b0040b 100#include "ui/qemu-spice.h"
68d98d3e 101#include "qapi/string-input-visitor.h"
c4090f8e 102#include "qapi/opts-visitor.h"
776d1344 103#include "qapi/clone-visitor.h"
84321831 104#include "qom/object_interfaces.h"
f1672e6f 105#include "hw/semihosting/semihost.h"
ddbb0d09 106#include "crypto/init.h"
b60c48a7 107#include "sysemu/replay.h"
9af23989
MA
108#include "qapi/qapi-events-run-state.h"
109#include "qapi/qapi-visit-block-core.h"
776d1344 110#include "qapi/qapi-visit-ui.h"
112ed241 111#include "qapi/qapi-commands-block-core.h"
112ed241 112#include "qapi/qapi-commands-run-state.h"
e1ca8f7e 113#include "qapi/qapi-commands-ui.h"
0194749a 114#include "qapi/qmp/qerror.h"
dce8921b 115#include "sysemu/iothread.h"
9c09a251 116#include "qemu/guest-random.h"
29b0040b 117
98b19252
AS
118#define MAX_VIRTIO_CONSOLES 1
119
0546c060 120static const char *cpu_option;
4524051c
GH
121static const char *data_dir[16];
122static int data_dir_idx;
d8e4de41
PB
123static const char *mem_path;
124static const char *boot_order;
125static const char *boot_once;
58c91595 126static const char *incoming;
cb5a7aa8 127enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
da076ffe 128int display_opengl;
3d11d0eb 129const char* keyboard_layout = NULL;
b326b6ea 130static ram_addr_t ram_size;
28d16f38 131bool enable_mlock = false;
6f131f13 132bool enable_cpu_pm = false;
c4b1fcc0 133int nb_nics;
7c9d8e07 134NICInfo nd_table[MAX_NICS];
efd7ab22 135int autostart = 1;
7e166ebd
AP
136static enum {
137 RTC_BASE_UTC,
138 RTC_BASE_LOCALTIME,
139 RTC_BASE_DATETIME,
140} rtc_base_type = RTC_BASE_UTC;
eb6a5209
AP
141static time_t rtc_ref_start_datetime;
142static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
143static int rtc_host_datetime_offset = -1; /* valid & used only with
144 RTC_BASE_DATETIME */
884f17c2 145QEMUClockType rtc_clock;
64465297 146int vga_interface_type = VGA_NONE;
0c8d7065 147static DisplayOptions dpy;
b8d880ba
PM
148static int num_serial_hds;
149static Chardev **serial_hds;
0546c060
PB
150static const char *log_mask;
151static const char *log_file;
152static bool list_data_dirs;
0ec7b3e7 153Chardev *parallel_hds[MAX_PARALLEL_PORTS];
a09db21f 154int win2k_install_hack = 0;
1b530a6d 155int singlestep = 0;
52ca8d6a 156int fd_bootchk = 1;
4fdcac0e 157static int no_reboot;
b2f76161 158int no_shutdown = 0;
a171fe39 159int graphic_rotate = 0;
09aaa160 160const char *watchdog;
2e55e842 161QEMUOptionRom option_rom[MAX_OPTION_ROMS];
9ae02555 162int nb_option_roms;
2b8f2d41 163int old_param = 0;
c35734b2 164const char *qemu_name;
3780e197 165int alt_grab = 0;
0ca9f8a4 166int ctrl_grab = 0;
66508601
BS
167unsigned int nb_prom_envs = 0;
168const char *prom_envs[MAX_PROM_ENVS];
95387491 169int boot_menu;
bc74112f 170bool boot_strict;
3d3b8303 171uint8_t *boot_splash_filedata;
811f8652 172int only_migratable; /* turn it off unless user states otherwise */
46ea94ca 173bool wakeup_suspend_enabled;
a8bfac37 174int icount_align_option;
eaf65114
TH
175static const char *qtest_chrdev;
176static const char *qtest_log;
1ca4d09a 177
9c5ce8db 178/* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
caad057b
EH
179 * little-endian "wire format" described in the SMBIOS 2.6 specification.
180 */
9c5ce8db 181QemuUUID qemu_uuid;
fc3b3295 182bool qemu_uuid_set;
8fcb1b90 183
fd42deeb
GH
184static NotifierList exit_notifiers =
185 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
186
4cab946a
GN
187static NotifierList machine_init_done_notifiers =
188 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
189
d745bef8
BS
190uint32_t xen_domid;
191enum xen_mode xen_mode = XEN_EMULATE;
1c599472 192bool xen_domid_restrict;
d745bef8 193
d44229c5 194static int has_defaults = 1;
998bbd74 195static int default_serial = 1;
6a5e8b0e 196static int default_parallel = 1;
abdeed06 197static int default_monitor = 1;
ac33f8fa
GH
198static int default_floppy = 1;
199static int default_cdrom = 1;
200static int default_sdcard = 1;
7f1b17f2 201static int default_vga = 1;
d30300f7 202static int default_net = 1;
998bbd74
GH
203
204static struct {
205 const char *driver;
206 int *flag;
207} default_list[] = {
6a5e8b0e
GH
208 { .driver = "isa-serial", .flag = &default_serial },
209 { .driver = "isa-parallel", .flag = &default_parallel },
d8bcbabf 210 { .driver = "isa-fdc", .flag = &default_floppy },
a92bd191 211 { .driver = "floppy", .flag = &default_floppy },
af6bf132
MA
212 { .driver = "ide-cd", .flag = &default_cdrom },
213 { .driver = "ide-hd", .flag = &default_cdrom },
d8bcbabf 214 { .driver = "ide-drive", .flag = &default_cdrom },
af6bf132 215 { .driver = "scsi-cd", .flag = &default_cdrom },
f6f99b48 216 { .driver = "scsi-hd", .flag = &default_cdrom },
7f1b17f2
PB
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 },
a94f0c5c 223 { .driver = "virtio-vga", .flag = &default_vga },
862b4a29 224 { .driver = "ati-vga", .flag = &default_vga },
267f6646 225 { .driver = "vhost-user-vga", .flag = &default_vga },
998bbd74
GH
226};
227
4d454574
PB
228static QemuOptsList qemu_rtc_opts = {
229 .name = "rtc",
230 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
eb6a5209 231 .merge_lists = true,
4d454574
PB
232 .desc = {
233 {
234 .name = "base",
235 .type = QEMU_OPT_STRING,
236 },{
237 .name = "clock",
238 .type = QEMU_OPT_STRING,
239 },{
240 .name = "driftfix",
241 .type = QEMU_OPT_STRING,
242 },
243 { /* end of list */ }
244 },
245};
246
4d454574
PB
247static QemuOptsList qemu_option_rom_opts = {
248 .name = "option-rom",
249 .implied_opt_name = "romfile",
250 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
251 .desc = {
252 {
253 .name = "bootindex",
254 .type = QEMU_OPT_NUMBER,
255 }, {
256 .name = "romfile",
257 .type = QEMU_OPT_STRING,
258 },
259 { /* end of list */ }
260 },
261};
262
263static QemuOptsList qemu_machine_opts = {
264 .name = "machine",
265 .implied_opt_name = "type",
266 .merge_lists = true,
267 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
268 .desc = {
49d2e648
MA
269 /*
270 * no elements => accept any
271 * sanity checking will happen later
272 * when setting machine properties
273 */
274 { }
4d454574
PB
275 },
276};
277
8d4e9146
FK
278static QemuOptsList qemu_accel_opts = {
279 .name = "accel",
280 .implied_opt_name = "accel",
281 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
8d4e9146 282 .desc = {
12ceaef6
PB
283 /*
284 * no elements => accept any
285 * sanity checking will happen later
286 * when setting accelerator properties
287 */
288 { }
8d4e9146
FK
289 },
290};
291
4d454574
PB
292static QemuOptsList qemu_boot_opts = {
293 .name = "boot-opts",
6ef4716c
MA
294 .implied_opt_name = "order",
295 .merge_lists = true,
4d454574
PB
296 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
297 .desc = {
4d454574
PB
298 {
299 .name = "order",
300 .type = QEMU_OPT_STRING,
301 }, {
302 .name = "once",
303 .type = QEMU_OPT_STRING,
304 }, {
305 .name = "menu",
6ef4716c 306 .type = QEMU_OPT_BOOL,
4d454574
PB
307 }, {
308 .name = "splash",
309 .type = QEMU_OPT_STRING,
310 }, {
311 .name = "splash-time",
6912bb0b 312 .type = QEMU_OPT_NUMBER,
4d454574
PB
313 }, {
314 .name = "reboot-timeout",
ee5d0f89 315 .type = QEMU_OPT_NUMBER,
c8a6ae8b
AK
316 }, {
317 .name = "strict",
e5187b56 318 .type = QEMU_OPT_BOOL,
4d454574
PB
319 },
320 { /*End of list */ }
321 },
322};
323
324static QemuOptsList qemu_add_fd_opts = {
325 .name = "add-fd",
326 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
327 .desc = {
328 {
329 .name = "fd",
330 .type = QEMU_OPT_NUMBER,
331 .help = "file descriptor of which a duplicate is added to fd set",
332 },{
333 .name = "set",
334 .type = QEMU_OPT_NUMBER,
335 .help = "ID of the fd set to add fd to",
336 },{
337 .name = "opaque",
338 .type = QEMU_OPT_STRING,
339 .help = "free-form string used to describe fd",
340 },
341 { /* end of list */ }
342 },
343};
344
345static QemuOptsList qemu_object_opts = {
346 .name = "object",
347 .implied_opt_name = "qom-type",
348 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
349 .desc = {
350 { }
351 },
352};
353
d1a0cf73
SB
354static QemuOptsList qemu_tpmdev_opts = {
355 .name = "tpmdev",
356 .implied_opt_name = "type",
357 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
358 .desc = {
bb716238 359 /* options are defined in the TPM backends */
d1a0cf73
SB
360 { /* end of list */ }
361 },
362};
363
888a6bc6
SM
364static QemuOptsList qemu_realtime_opts = {
365 .name = "realtime",
366 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
367 .desc = {
368 {
369 .name = "mlock",
370 .type = QEMU_OPT_BOOL,
371 },
372 { /* end of list */ }
373 },
374};
375
6f131f13
MT
376static QemuOptsList qemu_overcommit_opts = {
377 .name = "overcommit",
378 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
379 .desc = {
380 {
381 .name = "mem-lock",
382 .type = QEMU_OPT_BOOL,
383 },
384 {
385 .name = "cpu-pm",
386 .type = QEMU_OPT_BOOL,
387 },
388 { /* end of list */ }
389 },
390};
391
5e2ac519
SA
392static QemuOptsList qemu_msg_opts = {
393 .name = "msg",
394 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
395 .desc = {
396 {
397 .name = "timestamp",
398 .type = QEMU_OPT_BOOL,
399 },
2880ffb0
MS
400 {
401 .name = "guest-name",
402 .type = QEMU_OPT_BOOL,
403 .help = "Prepends guest name for error messages but only if "
404 "-name guest is set otherwise option is ignored\n",
405 },
5e2ac519
SA
406 { /* end of list */ }
407 },
408};
409
5d12f961
DDAG
410static QemuOptsList qemu_name_opts = {
411 .name = "name",
412 .implied_opt_name = "guest",
413 .merge_lists = true,
414 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
415 .desc = {
416 {
417 .name = "guest",
418 .type = QEMU_OPT_STRING,
419 .help = "Sets the name of the guest.\n"
420 "This name will be displayed in the SDL window caption.\n"
421 "The name will also be used for the VNC server",
422 }, {
423 .name = "process",
424 .type = QEMU_OPT_STRING,
425 .help = "Sets the name of the QEMU process, as shown in top etc",
8f480de0
DDAG
426 }, {
427 .name = "debug-threads",
428 .type = QEMU_OPT_BOOL,
429 .help = "When enabled, name the individual threads; defaults off.\n"
430 "NOTE: The thread names are for debugging and not a\n"
431 "stable API.",
5d12f961
DDAG
432 },
433 { /* End of list */ }
434 },
435};
436
6e1d3c1c
IM
437static QemuOptsList qemu_mem_opts = {
438 .name = "memory",
439 .implied_opt_name = "size",
440 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
441 .merge_lists = true,
442 .desc = {
443 {
444 .name = "size",
445 .type = QEMU_OPT_SIZE,
446 },
c270fb9e
IM
447 {
448 .name = "slots",
449 .type = QEMU_OPT_NUMBER,
450 },
451 {
452 .name = "maxmem",
453 .type = QEMU_OPT_SIZE,
454 },
6e1d3c1c
IM
455 { /* end of list */ }
456 },
457};
458
1ad9580b
ST
459static QemuOptsList qemu_icount_opts = {
460 .name = "icount",
461 .implied_opt_name = "shift",
462 .merge_lists = true,
463 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
464 .desc = {
465 {
466 .name = "shift",
467 .type = QEMU_OPT_STRING,
a8bfac37
ST
468 }, {
469 .name = "align",
470 .type = QEMU_OPT_BOOL,
f1f4b57e
VC
471 }, {
472 .name = "sleep",
473 .type = QEMU_OPT_BOOL,
4c27b859
PD
474 }, {
475 .name = "rr",
476 .type = QEMU_OPT_STRING,
477 }, {
478 .name = "rrfile",
479 .type = QEMU_OPT_STRING,
9c2037d0
PD
480 }, {
481 .name = "rrsnapshot",
482 .type = QEMU_OPT_STRING,
1ad9580b
ST
483 },
484 { /* end of list */ }
485 },
486};
487
81b2b810
GS
488static QemuOptsList qemu_fw_cfg_opts = {
489 .name = "fw_cfg",
490 .implied_opt_name = "name",
491 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
492 .desc = {
493 {
494 .name = "name",
495 .type = QEMU_OPT_STRING,
496 .help = "Sets the fw_cfg name of the blob to be inserted",
497 }, {
498 .name = "file",
499 .type = QEMU_OPT_STRING,
679be303 500 .help = "Sets the name of the file from which "
81b2b810 501 "the fw_cfg blob will be loaded",
6407d76e
GS
502 }, {
503 .name = "string",
504 .type = QEMU_OPT_STRING,
505 .help = "Sets content of the blob to be inserted from a string",
6552d87c
PMD
506 }, {
507 .name = "gen_id",
508 .type = QEMU_OPT_STRING,
509 .help = "Sets id of the object generating the fw_cfg blob "
510 "to be inserted",
81b2b810
GS
511 },
512 { /* end of list */ }
513 },
514};
515
7f9d6e54
MA
516/**
517 * Get machine options
518 *
519 * Returns: machine options (never null).
520 */
521QemuOpts *qemu_get_machine_opts(void)
522{
e96e5ae8 523 return qemu_find_opts_singleton("machine");
7f9d6e54
MA
524}
525
31459f46
RS
526const char *qemu_get_vm_name(void)
527{
528 return qemu_name;
529}
530
3d3b8303
WX
531static void res_free(void)
532{
012aef07
MA
533 g_free(boot_splash_filedata);
534 boot_splash_filedata = NULL;
3d3b8303
WX
535}
536
28d0de7a 537static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
998bbd74
GH
538{
539 const char *driver = qemu_opt_get(opts, "driver");
540 int i;
541
542 if (!driver)
543 return 0;
544 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
545 if (strcmp(default_list[i].driver, driver) != 0)
546 continue;
547 *(default_list[i].flag) = 0;
548 }
549 return 0;
550}
551
f5bbfba1
LC
552/***********************************************************/
553/* QEMU state */
554
047f7038 555static RunState current_run_state = RUN_STATE_PRECONFIG;
f5bbfba1 556
7fb1cf16
EB
557/* We use RUN_STATE__MAX but any invalid value will do */
558static RunState vmstop_requested = RUN_STATE__MAX;
74892d24
PB
559static QemuMutex vmstop_lock;
560
5db9d4d1
LC
561typedef struct {
562 RunState from;
563 RunState to;
564} RunStateTransition;
565
566static const RunStateTransition runstate_transitions_def[] = {
567 /* from -> to */
047f7038
IM
568 { RUN_STATE_PRECONFIG, RUN_STATE_PRELAUNCH },
569 /* Early switch to inmigrate state to allow -incoming CLI option work
570 * as it used to. TODO: delay actual switching to inmigrate state to
571 * the point after machine is built and remove this hack.
572 */
573 { RUN_STATE_PRECONFIG, RUN_STATE_INMIGRATE },
574
0461d5a6 575 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
eca01d3a 576 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
7ec13c79 577 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
5db9d4d1 578
ca3fc39e
JQ
579 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
580 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
29ed72f1 581 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
ca3fc39e
JQ
582 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
583 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
584 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
585 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
586 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
2a6e6e59 587 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
98799b0d
PB
588 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
589 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
21142ba7 590 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
5db9d4d1 591
0461d5a6 592 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
8a9236f1 593 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
7ec13c79 594 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
5db9d4d1 595
0461d5a6 596 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
8a9236f1 597 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
7ec13c79 598 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
5db9d4d1 599
0461d5a6 600 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
8a9236f1 601 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
505d786f 602 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
7ec13c79 603 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
21142ba7 604 { RUN_STATE_PAUSED, RUN_STATE_COLO},
5db9d4d1 605
0461d5a6 606 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
8a9236f1 607 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
7ec13c79 608 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
5db9d4d1 609
0461d5a6 610 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
8a9236f1 611 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
0461d5a6 612 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
5db9d4d1 613
0461d5a6 614 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
505d786f 615 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
0461d5a6 616 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
7ec13c79 617 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
21142ba7 618 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
5db9d4d1 619
0461d5a6 620 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
7ec13c79 621 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
5db9d4d1 622
21142ba7
HZ
623 { RUN_STATE_COLO, RUN_STATE_RUNNING },
624
0461d5a6
LC
625 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
626 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
627 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
628 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
629 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
630 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
631 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
632 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
633 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
ede085b3 634 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
21142ba7 635 { RUN_STATE_RUNNING, RUN_STATE_COLO},
5db9d4d1 636
0461d5a6 637 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
5db9d4d1 638
0461d5a6 639 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
8a9236f1 640 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
7ec13c79 641 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
5647051f 642 { RUN_STATE_SHUTDOWN, RUN_STATE_COLO },
5db9d4d1 643
ad02b96a
LC
644 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
645 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
646 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
647 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
7ec13c79 648 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
21142ba7 649 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
ad02b96a 650
0461d5a6 651 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
8a9236f1 652 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
7ec13c79 653 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
21142ba7 654 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
5db9d4d1 655
df390768 656 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
fd2a2e1c 657 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
7ec13c79 658 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
ede085b3 659
7fb1cf16 660 { RUN_STATE__MAX, RUN_STATE__MAX },
5db9d4d1
LC
661};
662
7fb1cf16 663static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
0461d5a6 664
f5bbfba1
LC
665bool runstate_check(RunState state)
666{
667 return current_run_state == state;
668}
669
5e0f1940
JQ
670bool runstate_store(char *str, size_t size)
671{
977c736f 672 const char *state = RunState_str(current_run_state);
5e0f1940
JQ
673 size_t len = strlen(state) + 1;
674
675 if (len > size) {
676 return false;
677 }
678 memcpy(str, state, len);
679 return true;
680}
681
4fdcac0e 682static void runstate_init(void)
5db9d4d1
LC
683{
684 const RunStateTransition *p;
685
686 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
7fb1cf16 687 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
5db9d4d1
LC
688 runstate_valid_transitions[p->from][p->to] = true;
689 }
74892d24
PB
690
691 qemu_mutex_init(&vmstop_lock);
5db9d4d1
LC
692}
693
694/* This function will abort() on invalid state transitions */
f5bbfba1
LC
695void runstate_set(RunState new_state)
696{
7fb1cf16 697 assert(new_state < RUN_STATE__MAX);
207c5cd2 698
ff12e3ae 699 trace_runstate_set(current_run_state, RunState_str(current_run_state),
b1939fcd 700 new_state, RunState_str(new_state));
ff12e3ae 701
e92a2d9c
LZ
702 if (current_run_state == new_state) {
703 return;
704 }
705
207c5cd2 706 if (!runstate_valid_transitions[current_run_state][new_state]) {
f61eddcb 707 error_report("invalid runstate transition: '%s' -> '%s'",
977c736f
MA
708 RunState_str(current_run_state),
709 RunState_str(new_state));
5db9d4d1
LC
710 abort();
711 }
ff12e3ae 712
f5bbfba1
LC
713 current_run_state = new_state;
714}
715
1fa9a5e4 716int runstate_is_running(void)
9e37b9dc 717{
1fa9a5e4 718 return runstate_check(RUN_STATE_RUNNING);
9e37b9dc
LC
719}
720
ede085b3
HT
721bool runstate_needs_reset(void)
722{
723 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
df390768 724 runstate_check(RUN_STATE_SHUTDOWN);
ede085b3
HT
725}
726
1fa9a5e4 727StatusInfo *qmp_query_status(Error **errp)
1354869c 728{
1fa9a5e4
LC
729 StatusInfo *info = g_malloc0(sizeof(*info));
730
731 info->running = runstate_is_running();
732 info->singlestep = singlestep;
733 info->status = current_run_state;
734
735 return info;
1354869c
LC
736}
737
2d76e823 738bool qemu_vmstop_requested(RunState *r)
74892d24
PB
739{
740 qemu_mutex_lock(&vmstop_lock);
741 *r = vmstop_requested;
7fb1cf16 742 vmstop_requested = RUN_STATE__MAX;
74892d24 743 qemu_mutex_unlock(&vmstop_lock);
7fb1cf16 744 return *r < RUN_STATE__MAX;
74892d24
PB
745}
746
747void qemu_system_vmstop_request_prepare(void)
748{
749 qemu_mutex_lock(&vmstop_lock);
750}
751
752void qemu_system_vmstop_request(RunState state)
753{
754 vmstop_requested = state;
755 qemu_mutex_unlock(&vmstop_lock);
756 qemu_notify_event();
757}
758
8f0056b7 759/***********************************************************/
eb6a5209 760/* RTC reference time/date access */
ac0989f5 761static time_t qemu_ref_timedate(QEMUClockType clock)
eb6a5209 762{
ac0989f5
AP
763 time_t value = qemu_clock_get_ms(clock) / 1000;
764 switch (clock) {
eb6a5209
AP
765 case QEMU_CLOCK_REALTIME:
766 value -= rtc_realtime_clock_offset;
4e8de606 767 /* fall through */
eb6a5209
AP
768 case QEMU_CLOCK_VIRTUAL:
769 value += rtc_ref_start_datetime;
770 break;
771 case QEMU_CLOCK_HOST:
772 if (rtc_base_type == RTC_BASE_DATETIME) {
773 value -= rtc_host_datetime_offset;
774 }
775 break;
776 default:
777 assert(0);
778 }
779 return value;
2ed1ebcf
PD
780}
781
f6503059
AZ
782void qemu_get_timedate(struct tm *tm, int offset)
783{
ac0989f5 784 time_t ti = qemu_ref_timedate(rtc_clock);
f6503059 785
f6503059 786 ti += offset;
7e166ebd
AP
787
788 switch (rtc_base_type) {
eb6a5209 789 case RTC_BASE_DATETIME:
7e166ebd 790 case RTC_BASE_UTC:
eb7ff6fb 791 gmtime_r(&ti, tm);
7e166ebd
AP
792 break;
793 case RTC_BASE_LOCALTIME:
794 localtime_r(&ti, tm);
795 break;
f6503059 796 }
f6503059
AZ
797}
798
799int qemu_timedate_diff(struct tm *tm)
800{
801 time_t seconds;
802
7e166ebd 803 switch (rtc_base_type) {
eb6a5209 804 case RTC_BASE_DATETIME:
7e166ebd
AP
805 case RTC_BASE_UTC:
806 seconds = mktimegm(tm);
807 break;
808 case RTC_BASE_LOCALTIME:
809 {
810 struct tm tmp = *tm;
811 tmp.tm_isdst = -1; /* use timezone to figure it out */
812 seconds = mktime(&tmp);
813 break;
814 }
7e166ebd
AP
815 default:
816 abort();
817 }
f6503059 818
ac0989f5 819 return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
f6503059
AZ
820}
821
eb6a5209 822static void configure_rtc_base_datetime(const char *startdate)
1ed2fc1f 823{
7e166ebd 824 time_t rtc_start_datetime;
1ed2fc1f
JK
825 struct tm tm;
826
fdaf2d58
TH
827 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
828 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
829 /* OK */
830 } else if (sscanf(startdate, "%d-%d-%d",
831 &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
832 tm.tm_hour = 0;
833 tm.tm_min = 0;
834 tm.tm_sec = 0;
1ed2fc1f 835 } else {
fdaf2d58
TH
836 goto date_fail;
837 }
838 tm.tm_year -= 1900;
839 tm.tm_mon--;
7e166ebd
AP
840 rtc_start_datetime = mktimegm(&tm);
841 if (rtc_start_datetime == -1) {
fdaf2d58 842 date_fail:
7e166ebd 843 error_report("invalid datetime format");
fdaf2d58
TH
844 error_printf("valid formats: "
845 "'2006-06-17T16:01:21' or '2006-06-17'\n");
846 exit(1);
1ed2fc1f 847 }
eb6a5209
AP
848 rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
849 rtc_ref_start_datetime = rtc_start_datetime;
1ed2fc1f
JK
850}
851
852static void configure_rtc(QemuOpts *opts)
853{
854 const char *value;
855
eb6a5209
AP
856 /* Set defaults */
857 rtc_clock = QEMU_CLOCK_HOST;
858 rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
859 rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
860
1ed2fc1f
JK
861 value = qemu_opt_get(opts, "base");
862 if (value) {
863 if (!strcmp(value, "utc")) {
7e166ebd 864 rtc_base_type = RTC_BASE_UTC;
1ed2fc1f 865 } else if (!strcmp(value, "localtime")) {
0194749a 866 Error *blocker = NULL;
7e166ebd 867 rtc_base_type = RTC_BASE_LOCALTIME;
0194749a
PD
868 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
869 "-rtc base=localtime");
870 replay_add_blocker(blocker);
1ed2fc1f 871 } else {
7e166ebd 872 rtc_base_type = RTC_BASE_DATETIME;
eb6a5209 873 configure_rtc_base_datetime(value);
1ed2fc1f
JK
874 }
875 }
6875204c
JK
876 value = qemu_opt_get(opts, "clock");
877 if (value) {
878 if (!strcmp(value, "host")) {
884f17c2 879 rtc_clock = QEMU_CLOCK_HOST;
78808141 880 } else if (!strcmp(value, "rt")) {
884f17c2 881 rtc_clock = QEMU_CLOCK_REALTIME;
6875204c 882 } else if (!strcmp(value, "vm")) {
884f17c2 883 rtc_clock = QEMU_CLOCK_VIRTUAL;
6875204c 884 } else {
f61eddcb 885 error_report("invalid option value '%s'", value);
6875204c
JK
886 exit(1);
887 }
888 }
1ed2fc1f
JK
889 value = qemu_opt_get(opts, "driftfix");
890 if (value) {
7e4c0336 891 if (!strcmp(value, "slew")) {
1fff3c20
PB
892 object_register_sugar_prop("mc146818rtc",
893 "lost_tick_policy",
894 "slew");
7e4c0336 895 } else if (!strcmp(value, "none")) {
433acf0d 896 /* discard is default */
1ed2fc1f 897 } else {
f61eddcb 898 error_report("invalid option value '%s'", value);
1ed2fc1f
JK
899 exit(1);
900 }
901 }
1ed2fc1f
JK
902}
903
28d0de7a 904static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
5d12f961
DDAG
905{
906 const char *proc_name;
907
8f480de0
DDAG
908 if (qemu_opt_get(opts, "debug-threads")) {
909 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
910 }
5d12f961
DDAG
911 qemu_name = qemu_opt_get(opts, "guest");
912
913 proc_name = qemu_opt_get(opts, "process");
914 if (proc_name) {
915 os_set_proc_name(proc_name);
916 }
5b9d313e
DDAG
917
918 return 0;
5d12f961
DDAG
919}
920
f8b6f8ed
MA
921bool defaults_enabled(void)
922{
923 return has_defaults;
924}
925
587ed6be 926#ifndef _WIN32
28d0de7a 927static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
587ed6be
CB
928{
929 int fd, dupfd, flags;
930 int64_t fdset_id;
931 const char *fd_opaque = NULL;
636a30a8 932 AddfdInfo *fdinfo;
587ed6be
CB
933
934 fd = qemu_opt_get_number(opts, "fd", -1);
935 fdset_id = qemu_opt_get_number(opts, "set", -1);
936 fd_opaque = qemu_opt_get(opts, "opaque");
937
938 if (fd < 0) {
6353218b 939 error_setg(errp, "fd option is required and must be non-negative");
587ed6be
CB
940 return -1;
941 }
942
943 if (fd <= STDERR_FILENO) {
6353218b 944 error_setg(errp, "fd cannot be a standard I/O stream");
587ed6be
CB
945 return -1;
946 }
947
948 /*
949 * All fds inherited across exec() necessarily have FD_CLOEXEC
950 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
951 */
952 flags = fcntl(fd, F_GETFD);
953 if (flags == -1 || (flags & FD_CLOEXEC)) {
6353218b 954 error_setg(errp, "fd is not valid or already in use");
587ed6be
CB
955 return -1;
956 }
957
958 if (fdset_id < 0) {
6353218b 959 error_setg(errp, "set option is required and must be non-negative");
587ed6be
CB
960 return -1;
961 }
962
963#ifdef F_DUPFD_CLOEXEC
964 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
965#else
966 dupfd = dup(fd);
967 if (dupfd != -1) {
968 qemu_set_cloexec(dupfd);
969 }
970#endif
971 if (dupfd == -1) {
6353218b 972 error_setg(errp, "error duplicating fd: %s", strerror(errno));
587ed6be
CB
973 return -1;
974 }
975
976 /* add the duplicate fd, and optionally the opaque string, to the fd set */
636a30a8
PB
977 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
978 &error_abort);
979 g_free(fdinfo);
587ed6be
CB
980
981 return 0;
982}
983
28d0de7a 984static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
587ed6be
CB
985{
986 int fd;
987
988 fd = qemu_opt_get_number(opts, "fd", -1);
989 close(fd);
990
991 return 0;
992}
993#endif
994
1ae26a18
AZ
995/***********************************************************/
996/* QEMU Block devices */
997
2292ddae
MA
998#define HD_OPTS "media=disk"
999#define CDROM_OPTS "media=cdrom"
1000#define FD_OPTS ""
1001#define PFLASH_OPTS ""
1002#define MTD_OPTS ""
1003#define SD_OPTS ""
e4bcb14c 1004
28d0de7a 1005static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
9dfd7c7a 1006{
2d0d2837 1007 BlockInterfaceType *block_default_type = opaque;
9dfd7c7a 1008
c4f26c9f 1009 return drive_new(opts, *block_default_type, errp) == NULL;
9dfd7c7a
GH
1010}
1011
28d0de7a 1012static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
9dfd7c7a 1013{
28de2f88 1014 if (qemu_opt_get(opts, "snapshot") == NULL) {
f43e47db 1015 qemu_opt_set(opts, "snapshot", "on", &error_abort);
9dfd7c7a
GH
1016 }
1017 return 0;
1018}
1019
3c42ea66
CB
1020static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1021 int index, const char *optstr)
4e5d9b57
MA
1022{
1023 QemuOpts *opts;
a66c9dc7 1024 DriveInfo *dinfo;
4e5d9b57 1025
4e5d9b57
MA
1026 if (!enable || drive_get_by_index(type, index)) {
1027 return;
1028 }
1029
1030 opts = drive_add(type, index, NULL, optstr);
1031 if (snapshot) {
28d0de7a 1032 drive_enable_snapshot(NULL, opts, NULL);
4e5d9b57 1033 }
a66c9dc7 1034
c4f26c9f 1035 dinfo = drive_new(opts, type, &error_abort);
a66c9dc7
JS
1036 dinfo->is_default = true;
1037
4e5d9b57
MA
1038}
1039
651af51c
MA
1040typedef struct BlockdevOptionsQueueEntry {
1041 BlockdevOptions *bdo;
1042 Location loc;
1043 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
1044} BlockdevOptionsQueueEntry;
1045
1046typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
1047
d11bf9bf
MA
1048static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
1049 MachineClass *machine_class, int snapshot)
1050{
1051 /*
1052 * If the currently selected machine wishes to override the
1053 * units-per-bus property of its default HBA interface type, do so
1054 * now.
1055 */
1056 if (machine_class->units_per_default_bus) {
1057 override_max_devs(machine_class->block_default_type,
1058 machine_class->units_per_default_bus);
1059 }
1060
1061 /* open the virtual block devices */
1062 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
1063 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
1064
1065 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
1066 loc_push_restore(&bdo->loc);
1067 qmp_blockdev_add(bdo->bdo, &error_fatal);
1068 loc_pop(&bdo->loc);
1069 qapi_free_BlockdevOptions(bdo->bdo);
1070 g_free(bdo);
1071 }
25863975 1072 if (snapshot) {
d11bf9bf
MA
1073 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
1074 NULL, NULL);
1075 }
1076 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
1077 &machine_class->block_default_type, &error_fatal)) {
1078 /* We printed help */
1079 exit(0);
1080 }
1081
1082 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
1083 CDROM_OPTS);
1084 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
1085 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
1086
1087}
1088
12b7f57e
MT
1089static QemuOptsList qemu_smp_opts = {
1090 .name = "smp-opts",
1091 .implied_opt_name = "cpus",
1092 .merge_lists = true,
1093 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1094 .desc = {
1095 {
1096 .name = "cpus",
1097 .type = QEMU_OPT_NUMBER,
1098 }, {
1099 .name = "sockets",
1100 .type = QEMU_OPT_NUMBER,
1b458422
LX
1101 }, {
1102 .name = "dies",
1103 .type = QEMU_OPT_NUMBER,
12b7f57e
MT
1104 }, {
1105 .name = "cores",
1106 .type = QEMU_OPT_NUMBER,
1107 }, {
1108 .name = "threads",
1109 .type = QEMU_OPT_NUMBER,
1110 }, {
1111 .name = "maxcpus",
1112 .type = QEMU_OPT_NUMBER,
1113 },
1114 { /*End of list */ }
1115 },
1116};
1117
28d16f38 1118static void realtime_init(void)
888a6bc6 1119{
888a6bc6
SM
1120 if (enable_mlock) {
1121 if (os_mlock() < 0) {
f61eddcb 1122 error_report("locking memory failed");
888a6bc6
SM
1123 exit(1);
1124 }
1125 }
1126}
1127
5e2ac519
SA
1128
1129static void configure_msg(QemuOpts *opts)
1130{
deda497b 1131 error_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
2880ffb0 1132 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
5e2ac519
SA
1133}
1134
a59d31a1 1135
a594cfbf
FB
1136/***********************************************************/
1137/* USB devices */
1138
fb08000c 1139static int usb_device_add(const char *devname)
a594cfbf 1140{
a5d2f727 1141 USBDevice *dev = NULL;
a594cfbf 1142
4bcbe0b6 1143 if (!machine_usb(current_machine)) {
a594cfbf 1144 return -1;
094b287f 1145 }
a594cfbf 1146
0958b4cc 1147 dev = usbdevice_create(devname);
0d92ed30
PB
1148 if (!dev)
1149 return -1;
1150
a594cfbf
FB
1151 return 0;
1152}
1153
bd3c948d
GH
1154static int usb_parse(const char *cmdline)
1155{
59d1c1c2 1156 int r;
fb08000c 1157 r = usb_device_add(cmdline);
59d1c1c2 1158 if (r < 0) {
f61eddcb 1159 error_report("could not add USB device '%s'", cmdline);
59d1c1c2
ST
1160 }
1161 return r;
bd3c948d
GH
1162}
1163
cc1daa40
FB
1164/***********************************************************/
1165/* machine registration */
1166
0056ae24 1167MachineState *current_machine;
cc1daa40 1168
c516cd1b 1169static MachineClass *find_machine(const char *name, GSList *machines)
cc1daa40 1170{
c516cd1b 1171 GSList *el;
cc1daa40 1172
261747f1 1173 for (el = machines; el; el = el->next) {
f2c93021 1174 MachineClass *mc = el->data;
261747f1 1175
f2c93021
MA
1176 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
1177 return mc;
261747f1 1178 }
cc1daa40 1179 }
261747f1 1180
f2c93021 1181 return NULL;
cc1daa40
FB
1182}
1183
c516cd1b 1184static MachineClass *find_default_machine(GSList *machines)
0c257437 1185{
c516cd1b 1186 GSList *el;
6db1857e 1187 MachineClass *default_machineclass = NULL;
0c257437 1188
261747f1 1189 for (el = machines; el; el = el->next) {
f2c93021 1190 MachineClass *mc = el->data;
261747f1 1191
f2c93021 1192 if (mc->is_default) {
6db1857e
PMD
1193 assert(default_machineclass == NULL && "Multiple default machines");
1194 default_machineclass = mc;
0c257437
AL
1195 }
1196 }
261747f1 1197
6db1857e 1198 return default_machineclass;
0c257437
AL
1199}
1200
52eb3dfd
MA
1201static int machine_help_func(QemuOpts *opts, MachineState *machine)
1202{
1203 ObjectProperty *prop;
7746abd8 1204 ObjectPropertyIterator iter;
52eb3dfd
MA
1205
1206 if (!qemu_opt_has_help_opt(opts)) {
1207 return 0;
1208 }
1209
7746abd8
DB
1210 object_property_iter_init(&iter, OBJECT(machine));
1211 while ((prop = object_property_iter_next(&iter))) {
52eb3dfd
MA
1212 if (!prop->set) {
1213 continue;
1214 }
1215
26eaf2cd
MA
1216 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1217 prop->name, prop->type);
52eb3dfd 1218 if (prop->description) {
26eaf2cd 1219 printf(" (%s)\n", prop->description);
52eb3dfd 1220 } else {
26eaf2cd 1221 printf("\n");
52eb3dfd
MA
1222 }
1223 }
1224
1225 return 1;
1226}
1227
2f780b6a 1228struct VMChangeStateEntry {
0bd48850
FB
1229 VMChangeStateHandler *cb;
1230 void *opaque;
2f780b6a 1231 QTAILQ_ENTRY(VMChangeStateEntry) entries;
60dbc5a1 1232 int priority;
0bd48850
FB
1233};
1234
efd7ab22
PB
1235static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head =
1236 QTAILQ_HEAD_INITIALIZER(vm_change_state_head);
0bd48850 1237
60dbc5a1
SH
1238/**
1239 * qemu_add_vm_change_state_handler_prio:
1240 * @cb: the callback to invoke
1241 * @opaque: user data passed to the callback
1242 * @priority: low priorities execute first when the vm runs and the reverse is
1243 * true when the vm stops
1244 *
1245 * Register a callback function that is invoked when the vm starts or stops
1246 * running.
1247 *
1248 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
1249 */
1250VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
1251 VMChangeStateHandler *cb, void *opaque, int priority)
0bd48850
FB
1252{
1253 VMChangeStateEntry *e;
60dbc5a1 1254 VMChangeStateEntry *other;
0bd48850 1255
60dbc5a1 1256 e = g_malloc0(sizeof(*e));
0bd48850
FB
1257 e->cb = cb;
1258 e->opaque = opaque;
60dbc5a1
SH
1259 e->priority = priority;
1260
1261 /* Keep list sorted in ascending priority order */
1262 QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
1263 if (priority < other->priority) {
1264 QTAILQ_INSERT_BEFORE(other, e, entries);
1265 return e;
1266 }
1267 }
1268
1269 QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
0bd48850
FB
1270 return e;
1271}
1272
60dbc5a1
SH
1273VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1274 void *opaque)
1275{
1276 return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
1277}
1278
0bd48850
FB
1279void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1280{
60dbc5a1
SH
1281 QTAILQ_REMOVE(&vm_change_state_head, e, entries);
1282 g_free(e);
0bd48850
FB
1283}
1284
1dfb4dd9 1285void vm_state_notify(int running, RunState state)
0bd48850 1286{
9b10ac86 1287 VMChangeStateEntry *e, *next;
0bd48850 1288
ff12e3ae 1289 trace_vm_state_notify(running, state, RunState_str(state));
94b0b5ff 1290
60dbc5a1
SH
1291 if (running) {
1292 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1293 e->cb(e->opaque, running, state);
1294 }
1295 } else {
1296 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
1297 e->cb(e->opaque, running, state);
1298 }
0bd48850
FB
1299 }
1300}
1301
aedbe192
EB
1302static ShutdownCause reset_requested;
1303static ShutdownCause shutdown_requested;
1304static int shutdown_signal;
f64622c4 1305static pid_t shutdown_pid;
3475187d 1306static int powerdown_requested;
8cf71710 1307static int debug_requested;
95b363b5 1308static int suspend_requested;
047f7038 1309static bool preconfig_exit_requested = true;
4bc78a87 1310static WakeupReason wakeup_reason;
a9552c8e
IM
1311static NotifierList powerdown_notifiers =
1312 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
95b363b5
GH
1313static NotifierList suspend_notifiers =
1314 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1315static NotifierList wakeup_notifiers =
1316 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
2dadd753
YS
1317static NotifierList shutdown_notifiers =
1318 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
4bc78a87 1319static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
bb0c6722 1320
aedbe192 1321ShutdownCause qemu_shutdown_requested_get(void)
1291eb35
AP
1322{
1323 return shutdown_requested;
1324}
1325
aedbe192 1326ShutdownCause qemu_reset_requested_get(void)
1291eb35
AP
1327{
1328 return reset_requested;
1329}
1330
4fdcac0e 1331static int qemu_shutdown_requested(void)
cf7a2fe2 1332{
d73415a3 1333 return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
cf7a2fe2
AJ
1334}
1335
4fdcac0e 1336static void qemu_kill_report(void)
f64622c4 1337{
7af88279 1338 if (!qtest_driver() && shutdown_signal) {
f1d3fb04
PM
1339 if (shutdown_pid == 0) {
1340 /* This happens for eg ^C at the terminal, so it's worth
1341 * avoiding printing an odd message in that case.
1342 */
f61eddcb 1343 error_report("terminating on signal %d", shutdown_signal);
f1d3fb04 1344 } else {
fbe7e332
MP
1345 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1346
1347 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1348 shutdown_signal, shutdown_pid,
1349 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1350 g_free(shutdown_cmd);
f1d3fb04 1351 }
7af88279 1352 shutdown_signal = 0;
f64622c4
GN
1353 }
1354}
1355
aedbe192 1356static ShutdownCause qemu_reset_requested(void)
cf7a2fe2 1357{
aedbe192
EB
1358 ShutdownCause r = reset_requested;
1359
8bd7f71d 1360 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
aedbe192 1361 reset_requested = SHUTDOWN_CAUSE_NONE;
8bd7f71d
PD
1362 return r;
1363 }
aedbe192 1364 return SHUTDOWN_CAUSE_NONE;
cf7a2fe2
AJ
1365}
1366
95b363b5
GH
1367static int qemu_suspend_requested(void)
1368{
1369 int r = suspend_requested;
8bd7f71d
PD
1370 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1371 suspend_requested = 0;
1372 return r;
1373 }
1374 return false;
95b363b5
GH
1375}
1376
4bc78a87 1377static WakeupReason qemu_wakeup_requested(void)
14058196 1378{
4bc78a87 1379 return wakeup_reason;
14058196
LC
1380}
1381
4fdcac0e 1382static int qemu_powerdown_requested(void)
cf7a2fe2
AJ
1383{
1384 int r = powerdown_requested;
1385 powerdown_requested = 0;
1386 return r;
1387}
1388
e568902a
AL
1389static int qemu_debug_requested(void)
1390{
1391 int r = debug_requested;
1392 debug_requested = 0;
1393 return r;
1394}
1395
047f7038
IM
1396void qemu_exit_preconfig_request(void)
1397{
1398 preconfig_exit_requested = true;
1399}
1400
aedbe192
EB
1401/*
1402 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1403 */
1404void qemu_system_reset(ShutdownCause reason)
be522029 1405{
0056ae24
MA
1406 MachineClass *mc;
1407
1408 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1409
efdaf797
DG
1410 cpu_synchronize_all_states();
1411
958db90c 1412 if (mc && mc->reset) {
a0628599 1413 mc->reset(current_machine);
be522029
DG
1414 } else {
1415 qemu_devices_reset();
1416 }
3bf5de52 1417 if (reason && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
ecd7a0d5 1418 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
e063eb1f 1419 }
ea375f9a 1420 cpu_synchronize_all_post_reset();
bb0c6722
FB
1421}
1422
4b5e06c9
NP
1423/*
1424 * Wake the VM after suspend.
1425 */
1426static void qemu_system_wakeup(void)
1427{
1428 MachineClass *mc;
1429
1430 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1431
1432 if (mc && mc->wakeup) {
1433 mc->wakeup(current_machine);
4b5e06c9
NP
1434 }
1435}
1436
c86f106b 1437void qemu_system_guest_panicked(GuestPanicInformation *info)
5f5b5942 1438{
4ada99ad 1439 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
f47291b7 1440
bac05aa9
AS
1441 if (current_cpu) {
1442 current_cpu->crash_occurred = true;
1443 }
c86f106b 1444 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
3ab72385 1445 !!info, info);
5f5b5942 1446 vm_stop(RUN_STATE_GUEST_PANICKED);
864111f4
CB
1447 if (!no_shutdown) {
1448 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
3ab72385 1449 !!info, info);
cf83f140 1450 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
864111f4 1451 }
c86f106b
AN
1452
1453 if (info) {
e8ed97a6 1454 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
4ada99ad 1455 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
f47291b7 1456 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
e8ed97a6
AN
1457 info->u.hyper_v.arg1,
1458 info->u.hyper_v.arg2,
1459 info->u.hyper_v.arg3,
1460 info->u.hyper_v.arg4,
1461 info->u.hyper_v.arg5);
4ada99ad
CB
1462 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1463 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1464 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1465 info->u.s390.core,
1466 S390CrashReason_str(info->u.s390.reason),
1467 info->u.s390.psw_mask,
1468 info->u.s390.psw_addr);
f47291b7 1469 }
c86f106b
AN
1470 qapi_free_GuestPanicInformation(info);
1471 }
5f5b5942
AS
1472}
1473
7dc58dee
ZP
1474void qemu_system_guest_crashloaded(GuestPanicInformation *info)
1475{
1476 qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded");
1477
1478 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN,
1479 !!info, info);
1480
1481 if (info) {
1482 qapi_free_GuestPanicInformation(info);
1483 }
1484}
1485
cf83f140 1486void qemu_system_reset_request(ShutdownCause reason)
bb0c6722 1487{
76ed4b18 1488 if (no_reboot && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
cf83f140 1489 shutdown_requested = reason;
d1beab82 1490 } else {
cf83f140 1491 reset_requested = reason;
d1beab82 1492 }
b4a3d965 1493 cpu_stop_current();
d9f75a4e 1494 qemu_notify_event();
bb0c6722
FB
1495}
1496
95b363b5
GH
1497static void qemu_system_suspend(void)
1498{
1499 pause_all_vcpus();
1500 notifier_list_notify(&suspend_notifiers, NULL);
ad02b96a 1501 runstate_set(RUN_STATE_SUSPENDED);
3ab72385 1502 qapi_event_send_suspend();
95b363b5
GH
1503}
1504
1505void qemu_system_suspend_request(void)
1506{
9abc62f6 1507 if (runstate_check(RUN_STATE_SUSPENDED)) {
95b363b5
GH
1508 return;
1509 }
1510 suspend_requested = 1;
1511 cpu_stop_current();
1512 qemu_notify_event();
1513}
1514
1515void qemu_register_suspend_notifier(Notifier *notifier)
1516{
1517 notifier_list_add(&suspend_notifiers, notifier);
1518}
1519
fb064112 1520void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
95b363b5 1521{
4fed9421
AK
1522 trace_system_wakeup_request(reason);
1523
9abc62f6 1524 if (!runstate_check(RUN_STATE_SUSPENDED)) {
fb064112
DHB
1525 error_setg(errp,
1526 "Unable to wake up: guest is not in suspended state");
95b363b5
GH
1527 return;
1528 }
1529 if (!(wakeup_reason_mask & (1 << reason))) {
1530 return;
1531 }
ad02b96a 1532 runstate_set(RUN_STATE_RUNNING);
4bc78a87 1533 wakeup_reason = reason;
95b363b5 1534 qemu_notify_event();
95b363b5
GH
1535}
1536
1537void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1538{
1539 if (enabled) {
1540 wakeup_reason_mask |= (1 << reason);
1541 } else {
1542 wakeup_reason_mask &= ~(1 << reason);
1543 }
1544}
1545
1546void qemu_register_wakeup_notifier(Notifier *notifier)
1547{
1548 notifier_list_add(&wakeup_notifiers, notifier);
1549}
1550
46ea94ca
DHB
1551void qemu_register_wakeup_support(void)
1552{
1553 wakeup_suspend_enabled = true;
1554}
1555
fb064112 1556bool qemu_wakeup_suspend_enabled(void)
46ea94ca
DHB
1557{
1558 return wakeup_suspend_enabled;
1559}
1560
f64622c4
GN
1561void qemu_system_killed(int signal, pid_t pid)
1562{
1563 shutdown_signal = signal;
1564 shutdown_pid = pid;
d9389b96 1565 no_shutdown = 0;
8bd7f71d
PD
1566
1567 /* Cannot call qemu_system_shutdown_request directly because
1568 * we are in a signal handler.
1569 */
aedbe192 1570 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
8bd7f71d 1571 qemu_notify_event();
f64622c4
GN
1572}
1573
cf83f140 1574void qemu_system_shutdown_request(ShutdownCause reason)
bb0c6722 1575{
cf83f140
EB
1576 trace_qemu_system_shutdown_request(reason);
1577 replay_shutdown_request(reason);
1578 shutdown_requested = reason;
d9f75a4e 1579 qemu_notify_event();
bb0c6722
FB
1580}
1581
013c2f15
IM
1582static void qemu_system_powerdown(void)
1583{
3ab72385 1584 qapi_event_send_powerdown();
013c2f15
IM
1585 notifier_list_notify(&powerdown_notifiers, NULL);
1586}
1587
2dadd753
YS
1588static void qemu_system_shutdown(ShutdownCause cause)
1589{
1590 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
1591 notifier_list_notify(&shutdown_notifiers, &cause);
1592}
1593
3475187d
FB
1594void qemu_system_powerdown_request(void)
1595{
bc78cff9 1596 trace_qemu_system_powerdown_request();
3475187d 1597 powerdown_requested = 1;
d9f75a4e
AL
1598 qemu_notify_event();
1599}
1600
a9552c8e
IM
1601void qemu_register_powerdown_notifier(Notifier *notifier)
1602{
1603 notifier_list_add(&powerdown_notifiers, notifier);
1604}
1605
2dadd753
YS
1606void qemu_register_shutdown_notifier(Notifier *notifier)
1607{
1608 notifier_list_add(&shutdown_notifiers, notifier);
1609}
1610
8cf71710
JK
1611void qemu_system_debug_request(void)
1612{
1613 debug_requested = 1;
83f338f7 1614 qemu_notify_event();
8cf71710
JK
1615}
1616
99435906
PB
1617static bool main_loop_should_exit(void)
1618{
1619 RunState r;
aedbe192
EB
1620 ShutdownCause request;
1621
047f7038
IM
1622 if (preconfig_exit_requested) {
1623 if (runstate_check(RUN_STATE_PRECONFIG)) {
1624 runstate_set(RUN_STATE_PRELAUNCH);
1625 }
1626 preconfig_exit_requested = false;
1627 return true;
1628 }
99435906
PB
1629 if (qemu_debug_requested()) {
1630 vm_stop(RUN_STATE_DEBUG);
1631 }
95b363b5
GH
1632 if (qemu_suspend_requested()) {
1633 qemu_system_suspend();
1634 }
aedbe192
EB
1635 request = qemu_shutdown_requested();
1636 if (request) {
99435906 1637 qemu_kill_report();
2dadd753 1638 qemu_system_shutdown(request);
99435906
PB
1639 if (no_shutdown) {
1640 vm_stop(RUN_STATE_SHUTDOWN);
1641 } else {
1642 return true;
1643 }
1644 }
aedbe192
EB
1645 request = qemu_reset_requested();
1646 if (request) {
99435906 1647 pause_all_vcpus();
aedbe192 1648 qemu_system_reset(request);
99435906 1649 resume_all_vcpus();
ddad81bd
LV
1650 /*
1651 * runstate can change in pause_all_vcpus()
1652 * as iothread mutex is unlocked
1653 */
7ec13c79 1654 if (!runstate_check(RUN_STATE_RUNNING) &&
ddad81bd
LV
1655 !runstate_check(RUN_STATE_INMIGRATE) &&
1656 !runstate_check(RUN_STATE_FINISH_MIGRATE)) {
7ec13c79 1657 runstate_set(RUN_STATE_PRELAUNCH);
99435906
PB
1658 }
1659 }
14058196
LC
1660 if (qemu_wakeup_requested()) {
1661 pause_all_vcpus();
4b5e06c9 1662 qemu_system_wakeup();
4bc78a87
LJ
1663 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1664 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
14058196 1665 resume_all_vcpus();
3ab72385 1666 qapi_event_send_wakeup();
14058196 1667 }
99435906 1668 if (qemu_powerdown_requested()) {
013c2f15 1669 qemu_system_powerdown();
99435906
PB
1670 }
1671 if (qemu_vmstop_requested(&r)) {
1672 vm_stop(r);
1673 }
1674 return false;
1675}
1676
7b733862 1677void qemu_main_loop(void)
43b96858 1678{
8e1b90ec
JK
1679#ifdef CONFIG_PROFILER
1680 int64_t ti;
1681#endif
0f5319ea 1682 while (!main_loop_should_exit()) {
89bfc105 1683#ifdef CONFIG_PROFILER
46481d39 1684 ti = profile_getclock();
89bfc105 1685#endif
e330c118 1686 main_loop_wait(false);
89bfc105 1687#ifdef CONFIG_PROFILER
46481d39 1688 dev_time += profile_getclock() - ti;
89bfc105 1689#endif
0f5319ea 1690 }
b4608c04
FB
1691}
1692
9bd7e6d9
PB
1693static void version(void)
1694{
7e563bfb 1695 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
d915b7bb 1696 QEMU_COPYRIGHT "\n");
9bd7e6d9
PB
1697}
1698
15f82208 1699static void help(int exitcode)
0824d6fc 1700{
a3adb7ad
ME
1701 version();
1702 printf("usage: %s [options] [disk_image]\n\n"
1703 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1704 error_get_progname());
1705
77bd1119
ME
1706#define QEMU_OPTIONS_GENERATE_HELP
1707#include "qemu-options-wrapper.h"
a3adb7ad
ME
1708
1709 printf("\nDuring emulation, the following keys are useful:\n"
3f020d70 1710 "ctrl-alt-f toggle full screen\n"
1711 "ctrl-alt-n switch to virtual console 'n'\n"
1712 "ctrl-alt toggle mouse and keyboard grab\n"
1713 "\n"
f5048cb7
EB
1714 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1715 "\n"
1716 QEMU_HELP_BOTTOM "\n");
a3adb7ad 1717
15f82208 1718 exit(exitcode);
0824d6fc
FB
1719}
1720
cd6f1169
FB
1721#define HAS_ARG 0x0001
1722
cd6f1169
FB
1723typedef struct QEMUOption {
1724 const char *name;
1725 int flags;
1726 int index;
ad96090a 1727 uint32_t arch_mask;
cd6f1169
FB
1728} QEMUOption;
1729
dbed7e40 1730static const QEMUOption qemu_options[] = {
ad96090a 1731 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
77bd1119
ME
1732#define QEMU_OPTIONS_GENERATE_OPTIONS
1733#include "qemu-options-wrapper.h"
cd6f1169 1734 { NULL },
fc01f7e7 1735};
a369da5f 1736
8c9a2b71
EH
1737typedef struct VGAInterfaceInfo {
1738 const char *opt_name; /* option name */
1739 const char *name; /* human-readable name */
c2c7b22d
EH
1740 /* Class names indicating that support is available.
1741 * If no class is specified, the interface is always available */
1742 const char *class_names[2];
8c9a2b71
EH
1743} VGAInterfaceInfo;
1744
53b93511 1745static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
8c9a2b71
EH
1746 [VGA_NONE] = {
1747 .opt_name = "none",
d2fa65cd 1748 .name = "no graphic card",
8c9a2b71
EH
1749 },
1750 [VGA_STD] = {
1751 .opt_name = "std",
1752 .name = "standard VGA",
c2c7b22d 1753 .class_names = { "VGA", "isa-vga" },
8c9a2b71
EH
1754 },
1755 [VGA_CIRRUS] = {
1756 .opt_name = "cirrus",
1757 .name = "Cirrus VGA",
c2c7b22d 1758 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
8c9a2b71
EH
1759 },
1760 [VGA_VMWARE] = {
1761 .opt_name = "vmware",
1762 .name = "VMWare SVGA",
c2c7b22d 1763 .class_names = { "vmware-svga" },
8c9a2b71
EH
1764 },
1765 [VGA_VIRTIO] = {
1766 .opt_name = "virtio",
1767 .name = "Virtio VGA",
c2c7b22d 1768 .class_names = { "virtio-vga" },
8c9a2b71
EH
1769 },
1770 [VGA_QXL] = {
1771 .opt_name = "qxl",
1772 .name = "QXL VGA",
c2c7b22d 1773 .class_names = { "qxl-vga" },
8c9a2b71
EH
1774 },
1775 [VGA_TCX] = {
1776 .opt_name = "tcx",
1777 .name = "TCX framebuffer",
c2c7b22d 1778 .class_names = { "SUNW,tcx" },
8c9a2b71
EH
1779 },
1780 [VGA_CG3] = {
1781 .opt_name = "cg3",
1782 .name = "CG3 framebuffer",
c2c7b22d 1783 .class_names = { "cgthree" },
8c9a2b71
EH
1784 },
1785 [VGA_XENFB] = {
1786 .opt_name = "xenfb",
d2fa65cd 1787 .name = "Xen paravirtualized framebuffer",
8c9a2b71
EH
1788 },
1789};
1790
c2c7b22d
EH
1791static bool vga_interface_available(VGAInterfaceType t)
1792{
53b93511 1793 const VGAInterfaceInfo *ti = &vga_interfaces[t];
c2c7b22d
EH
1794
1795 assert(t < VGA_TYPE_MAX);
1796 return !ti->class_names[0] ||
7ab6e7fc
GH
1797 module_object_class_by_name(ti->class_names[0]) ||
1798 module_object_class_by_name(ti->class_names[1]);
c2c7b22d
EH
1799}
1800
dceb8852
MAL
1801static const char *
1802get_default_vga_model(const MachineClass *machine_class)
1803{
1804 if (machine_class->default_display) {
1805 return machine_class->default_display;
1806 } else if (vga_interface_available(VGA_CIRRUS)) {
1807 return "cirrus";
1808 } else if (vga_interface_available(VGA_STD)) {
1809 return "std";
1810 }
1811
1812 return NULL;
1813}
1814
1815static void select_vgahw(const MachineClass *machine_class, const char *p)
3893c124 1816{
1817 const char *opts;
8c9a2b71 1818 int t;
3893c124 1819
dceb8852
MAL
1820 if (g_str_equal(p, "help")) {
1821 const char *def = get_default_vga_model(machine_class);
1822
1823 for (t = 0; t < VGA_TYPE_MAX; t++) {
1824 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1825
1826 if (vga_interface_available(t) && ti->opt_name) {
1827 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
1828 g_str_equal(ti->opt_name, def) ? " (default)" : "");
1829 }
1830 }
1831 exit(0);
1832 }
1833
d44229c5 1834 assert(vga_interface_type == VGA_NONE);
8c9a2b71 1835 for (t = 0; t < VGA_TYPE_MAX; t++) {
53b93511 1836 const VGAInterfaceInfo *ti = &vga_interfaces[t];
8c9a2b71 1837 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
c2c7b22d 1838 if (!vga_interface_available(t)) {
8c9a2b71
EH
1839 error_report("%s not available", ti->name);
1840 exit(1);
1841 }
1842 vga_interface_type = t;
1843 break;
af87bf29 1844 }
8c9a2b71
EH
1845 }
1846 if (t == VGA_TYPE_MAX) {
3893c124 1847 invalid_vga:
3e515373 1848 error_report("unknown vga type: %s", p);
3893c124 1849 exit(1);
1850 }
cb5a7aa8 1851 while (*opts) {
1852 const char *nextopt;
1853
1854 if (strstart(opts, ",retrace=", &nextopt)) {
1855 opts = nextopt;
1856 if (strstart(opts, "dumb", &nextopt))
1857 vga_retrace_method = VGA_RETRACE_DUMB;
1858 else if (strstart(opts, "precise", &nextopt))
1859 vga_retrace_method = VGA_RETRACE_PRECISE;
1860 else goto invalid_vga;
1861 } else goto invalid_vga;
1862 opts = nextopt;
1863 }
3893c124 1864}
1865
776d1344
GH
1866static void parse_display_qapi(const char *optarg)
1867{
776d1344
GH
1868 DisplayOptions *opts;
1869 Visitor *v;
1870
50beeb68 1871 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
776d1344
GH
1872
1873 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1874 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1875
1876 qapi_free_DisplayOptions(opts);
1877 visit_free(v);
1878}
1879
e1ca8f7e
GH
1880DisplayOptions *qmp_query_display_options(Error **errp)
1881{
1882 return QAPI_CLONE(DisplayOptions, &dpy);
1883}
1884
e3af9f9a 1885static void parse_display(const char *p)
1472a95b
JS
1886{
1887 const char *opts;
1472a95b 1888
c388f408
TH
1889 if (is_help_option(p)) {
1890 qemu_display_help();
1891 exit(0);
1892 }
1893
1472a95b 1894 if (strstart(p, "sdl", &opts)) {
7a61f438
GH
1895 /*
1896 * sdl DisplayType needs hand-crafted parser instead of
1897 * parse_display_qapi() due to some options not in
1898 * DisplayOptions, specifically:
1899 * - frame
1900 * Already deprecated.
1901 * - ctrl_grab + alt_grab
1902 * Not clear yet what happens to them long-term. Should
1903 * replaced by something better or deprecated and dropped.
1904 */
fe91f36a 1905 dpy.type = DISPLAY_TYPE_SDL;
1472a95b
JS
1906 while (*opts) {
1907 const char *nextopt;
1908
09bd7ba9 1909 if (strstart(opts, ",alt_grab=", &nextopt)) {
1472a95b
JS
1910 opts = nextopt;
1911 if (strstart(opts, "on", &nextopt)) {
1912 alt_grab = 1;
1913 } else if (strstart(opts, "off", &nextopt)) {
1914 alt_grab = 0;
1915 } else {
05175535 1916 goto invalid_sdl_args;
1472a95b
JS
1917 }
1918 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1919 opts = nextopt;
1920 if (strstart(opts, "on", &nextopt)) {
1921 ctrl_grab = 1;
1922 } else if (strstart(opts, "off", &nextopt)) {
1923 ctrl_grab = 0;
1924 } else {
05175535 1925 goto invalid_sdl_args;
1472a95b
JS
1926 }
1927 } else if (strstart(opts, ",window_close=", &nextopt)) {
1928 opts = nextopt;
fe91f36a 1929 dpy.has_window_close = true;
1472a95b 1930 if (strstart(opts, "on", &nextopt)) {
fe91f36a 1931 dpy.window_close = true;
1472a95b 1932 } else if (strstart(opts, "off", &nextopt)) {
fe91f36a 1933 dpy.window_close = false;
1472a95b 1934 } else {
05175535 1935 goto invalid_sdl_args;
1472a95b 1936 }
86a088e6
GH
1937 } else if (strstart(opts, ",show-cursor=", &nextopt)) {
1938 opts = nextopt;
1939 dpy.has_show_cursor = true;
1940 if (strstart(opts, "on", &nextopt)) {
1941 dpy.show_cursor = true;
1942 } else if (strstart(opts, "off", &nextopt)) {
1943 dpy.show_cursor = false;
1944 } else {
1945 goto invalid_sdl_args;
1946 }
0b71a5d5
GH
1947 } else if (strstart(opts, ",gl=", &nextopt)) {
1948 opts = nextopt;
fe91f36a 1949 dpy.has_gl = true;
0b71a5d5 1950 if (strstart(opts, "on", &nextopt)) {
62f27922 1951 dpy.gl = DISPLAYGL_MODE_ON;
4867e47c
ET
1952 } else if (strstart(opts, "core", &nextopt)) {
1953 dpy.gl = DISPLAYGL_MODE_CORE;
1954 } else if (strstart(opts, "es", &nextopt)) {
1955 dpy.gl = DISPLAYGL_MODE_ES;
0b71a5d5 1956 } else if (strstart(opts, "off", &nextopt)) {
62f27922 1957 dpy.gl = DISPLAYGL_MODE_OFF;
0b71a5d5
GH
1958 } else {
1959 goto invalid_sdl_args;
1960 }
1472a95b 1961 } else {
05175535 1962 invalid_sdl_args:
3e515373 1963 error_report("invalid SDL option string");
05175535 1964 exit(1);
1472a95b
JS
1965 }
1966 opts = nextopt;
1967 }
3264ff12 1968 } else if (strstart(p, "vnc", &opts)) {
7a61f438
GH
1969 /*
1970 * vnc isn't a (local) DisplayType but a protocol for remote
1971 * display access.
1972 */
4db14629 1973 if (*opts == '=') {
7b1ee0f2 1974 vnc_parse(opts + 1, &error_fatal);
4db14629 1975 } else {
c6bf0f7f 1976 error_report("VNC requires a display argument vnc=<display>");
3264ff12
JS
1977 exit(1);
1978 }
1472a95b 1979 } else {
776d1344 1980 parse_display_qapi(p);
1472a95b 1981 }
1472a95b
JS
1982}
1983
5cea8590
PB
1984char *qemu_find_file(int type, const char *name)
1985{
4524051c 1986 int i;
5cea8590
PB
1987 const char *subdir;
1988 char *buf;
1989
31783203
PM
1990 /* Try the name as a straight path first */
1991 if (access(name, R_OK) == 0) {
4524051c 1992 trace_load_file(name, name);
7267c094 1993 return g_strdup(name);
5cea8590 1994 }
4524051c 1995
5cea8590
PB
1996 switch (type) {
1997 case QEMU_FILE_TYPE_BIOS:
1998 subdir = "";
1999 break;
2000 case QEMU_FILE_TYPE_KEYMAP:
2001 subdir = "keymaps/";
2002 break;
2003 default:
2004 abort();
2005 }
4524051c
GH
2006
2007 for (i = 0; i < data_dir_idx; i++) {
2008 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2009 if (access(buf, R_OK) == 0) {
2010 trace_load_file(name, buf);
2011 return buf;
2012 }
7267c094 2013 g_free(buf);
5cea8590 2014 }
4524051c 2015 return NULL;
5cea8590
PB
2016}
2017
ea1edcd7 2018void qemu_add_data_dir(char *path)
2a1cce90
GH
2019{
2020 int i;
2021
2022 if (path == NULL) {
2023 return;
2024 }
2025 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2026 return;
2027 }
2028 for (i = 0; i < data_dir_idx; i++) {
2029 if (strcmp(data_dir[i], path) == 0) {
ea1edcd7
PB
2030 g_free(path); /* duplicate */
2031 return;
2a1cce90
GH
2032 }
2033 }
ea1edcd7 2034 data_dir[data_dir_idx++] = path;
2a1cce90
GH
2035}
2036
6407d76e
GS
2037static inline bool nonempty_str(const char *str)
2038{
2039 return str && *str;
2040}
2041
81b2b810
GS
2042static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2043{
2044 gchar *buf;
2045 size_t size;
6552d87c 2046 const char *name, *file, *str, *gen_id;
bab47d9a 2047 FWCfgState *fw_cfg = (FWCfgState *) opaque;
81b2b810 2048
bab47d9a 2049 if (fw_cfg == NULL) {
406b6367 2050 error_setg(errp, "fw_cfg device not available");
81b2b810
GS
2051 return -1;
2052 }
2053 name = qemu_opt_get(opts, "name");
2054 file = qemu_opt_get(opts, "file");
6407d76e 2055 str = qemu_opt_get(opts, "string");
6552d87c 2056 gen_id = qemu_opt_get(opts, "gen_id");
6407d76e 2057
6552d87c
PMD
2058 /* we need the name, and exactly one of: file, content string, gen_id */
2059 if (!nonempty_str(name) ||
2060 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
2061 error_setg(errp, "name, plus exactly one of file,"
2062 " string and gen_id, are needed");
81b2b810
GS
2063 return -1;
2064 }
2065 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
406b6367
MA
2066 error_setg(errp, "name too long (max. %d char)",
2067 FW_CFG_MAX_FILE_PATH - 1);
81b2b810
GS
2068 return -1;
2069 }
f7d8afb1
PMD
2070 if (nonempty_str(gen_id)) {
2071 /*
2072 * In this particular case where the content is populated
2073 * internally, the "etc/" namespace protection is relaxed,
2074 * so do not emit a warning.
2075 */
2076 } else if (strncmp(name, "opt/", 4) != 0) {
3dc6f869
AF
2077 warn_report("externally provided fw_cfg item names "
2078 "should be prefixed with \"opt/\"");
81b2b810 2079 }
6407d76e
GS
2080 if (nonempty_str(str)) {
2081 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2082 buf = g_memdup(str, size);
6552d87c 2083 } else if (nonempty_str(gen_id)) {
07719518 2084 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
6552d87c
PMD
2085 return -1;
2086 }
2087 return 0;
6407d76e 2088 } else {
353c7d58
LQ
2089 GError *err = NULL;
2090 if (!g_file_get_contents(file, &buf, &size, &err)) {
2091 error_setg(errp, "can't load %s: %s", file, err->message);
2092 g_error_free(err);
6407d76e
GS
2093 return -1;
2094 }
81b2b810 2095 }
bab47d9a
GH
2096 /* For legacy, keep user files in a specific global order. */
2097 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2098 fw_cfg_add_file(fw_cfg, name, buf, size);
2099 fw_cfg_reset_order_override(fw_cfg);
81b2b810
GS
2100 return 0;
2101}
2102
28d0de7a 2103static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
ff952ba2
MA
2104{
2105 return qdev_device_help(opts);
2106}
2107
28d0de7a 2108static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
f31d07d1
GH
2109{
2110 DeviceState *dev;
2111
cd65f349 2112 dev = qdev_device_add(opts, errp);
f3a85056
JF
2113 if (!dev && *errp) {
2114 error_report_err(*errp);
f31d07d1 2115 return -1;
f3a85056
JF
2116 } else if (dev) {
2117 object_unref(OBJECT(dev));
f006cf7f 2118 }
f31d07d1
GH
2119 return 0;
2120}
2121
28d0de7a 2122static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1a688d3b 2123{
bd2d80b2 2124 Error *local_err = NULL;
1a688d3b 2125
4ad6f6cb 2126 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
0ec846bf 2127 if (local_err) {
dd4af977 2128 error_propagate(errp, local_err);
0ec846bf
AN
2129 return -1;
2130 }
2131 exit(0);
bd2d80b2 2132 }
1a688d3b
GH
2133 return 0;
2134}
2135
758e8e38 2136#ifdef CONFIG_VIRTFS
28d0de7a 2137static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
74db920c 2138{
b836723d 2139 return qemu_fsdev_add(opts, errp);
74db920c
GS
2140}
2141#endif
2142
28d0de7a 2143static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
88589343 2144{
c3e95551 2145 return monitor_init_opts(opts, errp);
88589343
GH
2146}
2147
4821cd4c 2148static void monitor_parse(const char *optarg, const char *mode, bool pretty)
88589343
GH
2149{
2150 static int monitor_device_index = 0;
2151 QemuOpts *opts;
2152 const char *p;
2153 char label[32];
88589343
GH
2154
2155 if (strstart(optarg, "chardev:", &p)) {
2156 snprintf(label, sizeof(label), "%s", p);
2157 } else {
140e065d
JK
2158 snprintf(label, sizeof(label), "compat_monitor%d",
2159 monitor_device_index);
95e30b2a 2160 opts = qemu_chr_parse_compat(label, optarg, true);
88589343 2161 if (!opts) {
f61eddcb 2162 error_report("parse error: %s", optarg);
88589343
GH
2163 exit(1);
2164 }
2165 }
2166
822ac12d 2167 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
f43e47db
MA
2168 qemu_opt_set(opts, "mode", mode, &error_abort);
2169 qemu_opt_set(opts, "chardev", label, &error_abort);
3c45f625
KW
2170 if (!strcmp(mode, "control")) {
2171 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2172 } else {
2173 assert(pretty == false);
2174 }
88589343
GH
2175 monitor_device_index++;
2176}
2177
bd3c948d
GH
2178struct device_config {
2179 enum {
aee1b935 2180 DEV_USB, /* -usbdevice */
aee1b935
GH
2181 DEV_SERIAL, /* -serial */
2182 DEV_PARALLEL, /* -parallel */
c9f398e5 2183 DEV_DEBUGCON, /* -debugcon */
ef0c4a0d 2184 DEV_GDB, /* -gdb, -s */
3ef669e1 2185 DEV_SCLP, /* s390 sclp */
bd3c948d
GH
2186 } type;
2187 const char *cmdline;
d9a5954d 2188 Location loc;
72cf2d4f 2189 QTAILQ_ENTRY(device_config) next;
bd3c948d 2190};
4fdcac0e
BS
2191
2192static QTAILQ_HEAD(, device_config) device_configs =
2193 QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
2194
2195static void add_device_config(int type, const char *cmdline)
2196{
2197 struct device_config *conf;
2198
7267c094 2199 conf = g_malloc0(sizeof(*conf));
bd3c948d
GH
2200 conf->type = type;
2201 conf->cmdline = cmdline;
d9a5954d 2202 loc_save(&conf->loc);
72cf2d4f 2203 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
2204}
2205
2206static int foreach_device_config(int type, int (*func)(const char *cmdline))
2207{
2208 struct device_config *conf;
2209 int rc;
2210
72cf2d4f 2211 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
2212 if (conf->type != type)
2213 continue;
d9a5954d 2214 loc_push_restore(&conf->loc);
bd3c948d 2215 rc = func(conf->cmdline);
d9a5954d 2216 loc_pop(&conf->loc);
28de2f88 2217 if (rc) {
bd3c948d 2218 return rc;
28de2f88 2219 }
bd3c948d
GH
2220 }
2221 return 0;
2222}
2223
998bbd74
GH
2224static int serial_parse(const char *devname)
2225{
6af2692e 2226 int index = num_serial_hds;
998bbd74
GH
2227 char label[32];
2228
2229 if (strcmp(devname, "none") == 0)
2230 return 0;
998bbd74 2231 snprintf(label, sizeof(label), "serial%d", index);
6af2692e
PM
2232 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
2233
4ad6f6cb 2234 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
998bbd74 2235 if (!serial_hds[index]) {
f61eddcb
EH
2236 error_report("could not connect serial device"
2237 " to character backend '%s'", devname);
998bbd74
GH
2238 return -1;
2239 }
6af2692e 2240 num_serial_hds++;
998bbd74
GH
2241 return 0;
2242}
2243
a8d78cd0
PM
2244Chardev *serial_hd(int i)
2245{
2246 assert(i >= 0);
6af2692e 2247 if (i < num_serial_hds) {
a8d78cd0
PM
2248 return serial_hds[i];
2249 }
2250 return NULL;
2251}
2252
b8846a4d
PM
2253int serial_max_hds(void)
2254{
2255 return num_serial_hds;
2256}
2257
6a5e8b0e
GH
2258static int parallel_parse(const char *devname)
2259{
2260 static int index = 0;
2261 char label[32];
2262
2263 if (strcmp(devname, "none") == 0)
2264 return 0;
2265 if (index == MAX_PARALLEL_PORTS) {
f61eddcb 2266 error_report("too many parallel ports");
6a5e8b0e
GH
2267 exit(1);
2268 }
2269 snprintf(label, sizeof(label), "parallel%d", index);
4ad6f6cb 2270 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
6a5e8b0e 2271 if (!parallel_hds[index]) {
f61eddcb
EH
2272 error_report("could not connect parallel device"
2273 " to character backend '%s'", devname);
6a5e8b0e
GH
2274 return -1;
2275 }
2276 index++;
2277 return 0;
2278}
2279
c9f398e5 2280static int debugcon_parse(const char *devname)
4d8b3c63 2281{
c9f398e5
PA
2282 QemuOpts *opts;
2283
4ad6f6cb 2284 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
d7ecf712 2285 error_report("invalid character backend '%s'", devname);
c9f398e5
PA
2286 exit(1);
2287 }
8be7e7e4 2288 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
c9f398e5 2289 if (!opts) {
f61eddcb 2290 error_report("already have a debugcon device");
c9f398e5
PA
2291 exit(1);
2292 }
f43e47db
MA
2293 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2294 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
c9f398e5
PA
2295 return 0;
2296}
2297
2709f263
LE
2298static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2299{
2300 const MachineClass *mc1 = a, *mc2 = b;
2301 int res;
2302
2303 if (mc1->family == NULL) {
2304 if (mc2->family == NULL) {
2305 /* Compare standalone machine types against each other; they sort
2306 * in increasing order.
2307 */
2308 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2309 object_class_get_name(OBJECT_CLASS(mc2)));
2310 }
2311
2312 /* Standalone machine types sort after families. */
2313 return 1;
2314 }
2315
2316 if (mc2->family == NULL) {
2317 /* Families sort before standalone machine types. */
2318 return -1;
2319 }
2320
2321 /* Families sort between each other alphabetically increasingly. */
2322 res = strcmp(mc1->family, mc2->family);
2323 if (res != 0) {
2324 return res;
2325 }
2326
2327 /* Within the same family, machine types sort in decreasing order. */
2328 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2329 object_class_get_name(OBJECT_CLASS(mc1)));
2330}
2331
c516cd1b 2332static MachineClass *machine_parse(const char *name, GSList *machines)
9052ea6b 2333{
12cb82fd 2334 MachineClass *mc;
c516cd1b 2335 GSList *el;
9052ea6b 2336
12cb82fd 2337 if (is_help_option(name)) {
025172d5 2338 printf("Supported machines are:\n");
2709f263 2339 machines = g_slist_sort(machines, machine_class_cmp);
025172d5
MR
2340 for (el = machines; el; el = el->next) {
2341 MachineClass *mc = el->data;
958db90c
MA
2342 if (mc->alias) {
2343 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
025172d5 2344 }
08fe6824
TH
2345 printf("%-20s %s%s%s\n", mc->name, mc->desc,
2346 mc->is_default ? " (default)" : "",
2347 mc->deprecation_reason ? " (deprecated)" : "");
9052ea6b 2348 }
12cb82fd 2349 exit(0);
9052ea6b 2350 }
261747f1 2351
12cb82fd
MA
2352 mc = find_machine(name, machines);
2353 if (!mc) {
2354 error_report("unsupported machine type");
2355 error_printf("Use -machine help to list supported machines\n");
2356 exit(1);
2357 }
2358 return mc;
9052ea6b
JK
2359}
2360
fd42deeb
GH
2361void qemu_add_exit_notifier(Notifier *notify)
2362{
2363 notifier_list_add(&exit_notifiers, notify);
2364}
2365
2366void qemu_remove_exit_notifier(Notifier *notify)
2367{
31552529 2368 notifier_remove(notify);
fd42deeb
GH
2369}
2370
2371static void qemu_run_exit_notifiers(void)
2372{
9e8dd451 2373 notifier_list_notify(&exit_notifiers, NULL);
fd42deeb
GH
2374}
2375
90a84d13
MAL
2376static const char *pid_file;
2377static Notifier qemu_unlink_pidfile_notifier;
2378
2379static void qemu_unlink_pidfile(Notifier *n, void *data)
2380{
2381 if (pid_file) {
2382 unlink(pid_file);
2383 }
2384}
2385
c8ca2a23 2386bool machine_init_done;
71cdd1cb 2387
4cab946a
GN
2388void qemu_add_machine_init_done_notifier(Notifier *notify)
2389{
2390 notifier_list_add(&machine_init_done_notifiers, notify);
71cdd1cb
PB
2391 if (machine_init_done) {
2392 notify->notify(notify, NULL);
2393 }
4cab946a
GN
2394}
2395
b86eacb8
MA
2396void qemu_remove_machine_init_done_notifier(Notifier *notify)
2397{
2398 notifier_remove(notify);
2399}
2400
4cab946a
GN
2401static void qemu_run_machine_init_done_notifiers(void)
2402{
71cdd1cb 2403 machine_init_done = true;
c8ca2a23 2404 notifier_list_notify(&machine_init_done_notifiers, NULL);
4cab946a
GN
2405}
2406
6530a97b
AL
2407static const QEMUOption *lookup_opt(int argc, char **argv,
2408 const char **poptarg, int *poptind)
2409{
2410 const QEMUOption *popt;
2411 int optind = *poptind;
2412 char *r = argv[optind];
2413 const char *optarg;
2414
0f0bc3f1 2415 loc_set_cmdline(argv, optind, 1);
6530a97b
AL
2416 optind++;
2417 /* Treat --foo the same as -foo. */
2418 if (r[1] == '-')
2419 r++;
2420 popt = qemu_options;
2421 for(;;) {
2422 if (!popt->name) {
0f0bc3f1 2423 error_report("invalid option");
6530a97b
AL
2424 exit(1);
2425 }
2426 if (!strcmp(popt->name, r + 1))
2427 break;
2428 popt++;
2429 }
2430 if (popt->flags & HAS_ARG) {
2431 if (optind >= argc) {
0f0bc3f1 2432 error_report("requires an argument");
6530a97b
AL
2433 exit(1);
2434 }
2435 optarg = argv[optind++];
0f0bc3f1 2436 loc_set_cmdline(argv, optind - 2, 2);
6530a97b
AL
2437 } else {
2438 optarg = NULL;
2439 }
2440
2441 *poptarg = optarg;
2442 *poptind = optind;
2443
2444 return popt;
2445}
2446
7580f231 2447static MachineClass *select_machine(void)
34f405ae 2448{
c516cd1b
WY
2449 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
2450 MachineClass *machine_class = find_default_machine(machines);
34f405ae
MA
2451 const char *optarg;
2452 QemuOpts *opts;
2453 Location loc;
2454
2455 loc_push_none(&loc);
2456
2457 opts = qemu_get_machine_opts();
2458 qemu_opts_loc_restore(opts);
2459
2460 optarg = qemu_opt_get(opts, "type");
2461 if (optarg) {
c516cd1b 2462 machine_class = machine_parse(optarg, machines);
34f405ae
MA
2463 }
2464
7580f231 2465 if (!machine_class) {
34f405ae
MA
2466 error_report("No machine specified, and there is no default");
2467 error_printf("Use -machine help to list supported machines\n");
2468 exit(1);
2469 }
2470
2471 loc_pop(&loc);
c516cd1b 2472 g_slist_free(machines);
7580f231 2473 return machine_class;
34f405ae
MA
2474}
2475
e5db4bd8
PB
2476static int object_parse_property_opt(Object *obj,
2477 const char *name, const char *value,
2478 const char *skip, Error **errp)
68d98d3e 2479{
e5db4bd8 2480 if (g_str_equal(name, skip)) {
68d98d3e
AL
2481 return 0;
2482 }
2483
668f62ec 2484 if (!object_property_parse(obj, name, value, errp)) {
68d98d3e
AL
2485 return -1;
2486 }
2487
2488 return 0;
2489}
2490
e5db4bd8
PB
2491static int machine_set_property(void *opaque,
2492 const char *name, const char *value,
2493 Error **errp)
2494{
2495 g_autofree char *qom_name = g_strdup(name);
2496 char *p;
2497
2498 for (p = qom_name; *p; p++) {
2499 if (*p == '_') {
2500 *p = '-';
2501 }
2502 }
2503
6f6e1698
PB
2504 /* Legacy options do not correspond to MachineState properties. */
2505 if (g_str_equal(qom_name, "accel")) {
2506 return 0;
2507 }
46472d82
PB
2508 if (g_str_equal(qom_name, "igd-passthru")) {
2509 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name, value);
2510 return 0;
2511 }
faf20793 2512 if (g_str_equal(qom_name, "kvm-shadow-mem")) {
23b0898e
PB
2513 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value);
2514 return 0;
2515 }
faf20793
SM
2516 if (g_str_equal(qom_name, "kernel-irqchip")) {
2517 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name, value);
2518 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), qom_name, value);
2519 return 0;
2520 }
6f6e1698 2521
e5db4bd8
PB
2522 return object_parse_property_opt(opaque, name, value, "type", errp);
2523}
f08f9271
DB
2524
2525/*
2526 * Initial object creation happens before all other
2527 * QEMU data types are created. The majority of objects
2528 * can be created at this point. The rng-egd object
2529 * cannot be created here, as it depends on the chardev
2530 * already existing.
2531 */
1195fa2b 2532static bool object_create_initial(const char *type, QemuOpts *opts)
f08f9271 2533{
3e9297f3 2534 if (user_creatable_print_help(type, opts)) {
1195fa2b
MAL
2535 exit(0);
2536 }
2537
edfb4389
MA
2538 /*
2539 * Objects should not be made "delayed" without a reason. If you
2540 * add one, state the reason in a comment!
2541 */
2542
2543 /* Reason: rng-egd property "chardev" */
9ea18ed2 2544 if (g_str_equal(type, "rng-egd")) {
f08f9271
DB
2545 return false;
2546 }
7dbb11c8 2547
042cea27 2548#if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
edfb4389 2549 /* Reason: cryptodev-vhost-user property "chardev" */
042cea27
GA
2550 if (g_str_equal(type, "cryptodev-vhost-user")) {
2551 return false;
2552 }
2553#endif
2554
3578389b
CX
2555 /* Reason: vhost-user-blk-server property "node-name" */
2556 if (g_str_equal(type, "vhost-user-blk-server")) {
2557 return false;
2558 }
9d3e12e8 2559 /*
edfb4389 2560 * Reason: filter-* property "netdev" etc.
9d3e12e8
TH
2561 */
2562 if (g_str_equal(type, "filter-buffer") ||
f6d3afb5 2563 g_str_equal(type, "filter-dump") ||
d46f75b2 2564 g_str_equal(type, "filter-mirror") ||
7dce4e6f 2565 g_str_equal(type, "filter-redirector") ||
e6eee8ab 2566 g_str_equal(type, "colo-compare") ||
646c5478
PD
2567 g_str_equal(type, "filter-rewriter") ||
2568 g_str_equal(type, "filter-replay")) {
7dbb11c8
YH
2569 return false;
2570 }
2571
6546d0db
EH
2572 /* Memory allocation by backends needs to be done
2573 * after configure_accelerator() (due to the tcg_enabled()
2574 * checks at memory_region_init_*()).
2575 *
2576 * Also, allocation of large amounts of memory may delay
2577 * chardev initialization for too long, and trigger timeouts
2578 * on software that waits for a monitor socket to be created
2579 * (e.g. libvirt).
2580 */
2581 if (g_str_has_prefix(type, "memory-backend-")) {
2582 return false;
2583 }
2584
f08f9271
DB
2585 return true;
2586}
2587
2588
2589/*
2590 * The remainder of object creation happens after the
9abce56d 2591 * creation of chardev, fsdev, net clients and device data types.
f08f9271 2592 */
1195fa2b 2593static bool object_create_delayed(const char *type, QemuOpts *opts)
f08f9271 2594{
1195fa2b 2595 return !object_create_initial(type, opts);
f08f9271
DB
2596}
2597
2598
2a7b18a3 2599static bool set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
076b35b5 2600 MachineClass *mc)
3b9985e9
MA
2601{
2602 uint64_t sz;
2603 const char *mem_str;
076b35b5 2604 const ram_addr_t default_ram_size = mc->default_ram_size;
3b9985e9 2605 QemuOpts *opts = qemu_find_opts_singleton("memory");
bbe2d25c
EH
2606 Location loc;
2607
2608 loc_push_none(&loc);
2609 qemu_opts_loc_restore(opts);
3b9985e9
MA
2610
2611 sz = 0;
2612 mem_str = qemu_opt_get(opts, "size");
2613 if (mem_str) {
2614 if (!*mem_str) {
2615 error_report("missing 'size' option value");
2616 exit(EXIT_FAILURE);
2617 }
2618
2619 sz = qemu_opt_get_size(opts, "size", ram_size);
2620
2621 /* Fix up legacy suffix-less format */
2622 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2623 uint64_t overflow_check = sz;
2624
3dc54b0e
PMD
2625 sz *= MiB;
2626 if (sz / MiB != overflow_check) {
3b9985e9
MA
2627 error_report("too large 'size' option value");
2628 exit(EXIT_FAILURE);
2629 }
2630 }
2631 }
2632
2633 /* backward compatibility behaviour for case "-m 0" */
2634 if (sz == 0) {
2635 sz = default_ram_size;
2636 }
2637
2638 sz = QEMU_ALIGN_UP(sz, 8192);
5c30ef93
CB
2639 if (mc->fixup_ram_size) {
2640 sz = mc->fixup_ram_size(sz);
2641 }
3b9985e9
MA
2642 ram_size = sz;
2643 if (ram_size != sz) {
2644 error_report("ram size too large");
2645 exit(EXIT_FAILURE);
2646 }
2647
2648 /* store value for the future use */
39101f25 2649 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
3b9985e9
MA
2650 *maxram_size = ram_size;
2651
951f2269 2652 if (qemu_opt_get(opts, "maxmem")) {
3b9985e9
MA
2653 uint64_t slots;
2654
2655 sz = qemu_opt_get_size(opts, "maxmem", 0);
214224ad 2656 slots = qemu_opt_get_number(opts, "slots", 0);
3b9985e9 2657 if (sz < ram_size) {
214224ad
PK
2658 error_report("invalid value of -m option maxmem: "
2659 "maximum memory size (0x%" PRIx64 ") must be at least "
2660 "the initial memory size (0x" RAM_ADDR_FMT ")",
2661 sz, ram_size);
3b9985e9 2662 exit(EXIT_FAILURE);
951f2269 2663 } else if (slots && sz == ram_size) {
214224ad
PK
2664 error_report("invalid value of -m option maxmem: "
2665 "memory slots were specified but maximum memory size "
2666 "(0x%" PRIx64 ") is equal to the initial memory size "
2667 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
3b9985e9
MA
2668 exit(EXIT_FAILURE);
2669 }
2670
3b9985e9
MA
2671 *maxram_size = sz;
2672 *ram_slots = slots;
951f2269
DH
2673 } else if (qemu_opt_get(opts, "slots")) {
2674 error_report("invalid -m option value: missing 'maxmem' option");
3b9985e9
MA
2675 exit(EXIT_FAILURE);
2676 }
bbe2d25c
EH
2677
2678 loc_pop(&loc);
2a7b18a3 2679 return !!mem_str;
3b9985e9
MA
2680}
2681
8d76bfe8
EH
2682static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2683{
2684 GlobalProperty *g;
2685
2686 g = g_malloc0(sizeof(*g));
2687 g->driver = qemu_opt_get(opts, "driver");
2688 g->property = qemu_opt_get(opts, "property");
2689 g->value = qemu_opt_get(opts, "value");
8d76bfe8
EH
2690 qdev_prop_register_global(g);
2691 return 0;
2692}
2693
726401be
EH
2694static int qemu_read_default_config_file(void)
2695{
2696 int ret;
580e6ad6 2697 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
726401be 2698
580e6ad6 2699 ret = qemu_read_config_file(file);
726401be
EH
2700 if (ret < 0 && ret != -ENOENT) {
2701 return ret;
2702 }
2703
2704 return 0;
2705}
2706
a0660e0b
PX
2707static void user_register_global_props(void)
2708{
2709 qemu_opts_foreach(qemu_find_opts("global"),
2710 global_init_func, NULL, NULL);
2711}
2712
7f8b6126
PB
2713static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2714{
8191d368 2715 icount_configure(opts, errp);
7f8b6126
PB
2716 return 0;
2717}
2718
12ceaef6
PB
2719static int accelerator_set_property(void *opaque,
2720 const char *name, const char *value,
2721 Error **errp)
2722{
2723 return object_parse_property_opt(opaque, name, value, "accel", errp);
2724}
2725
deda73e8
PB
2726static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2727{
6f6e1698
PB
2728 bool *p_init_failed = opaque;
2729 const char *acc = qemu_opt_get(opts, "accel");
2730 AccelClass *ac = accel_find(acc);
fc5cf826 2731 AccelState *accel;
6f6e1698 2732 int ret;
eaf65114
TH
2733 bool qtest_with_kvm;
2734
2735 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
6f6e1698
PB
2736
2737 if (!ac) {
8b90f1c5 2738 *p_init_failed = true;
eaf65114
TH
2739 if (!qtest_with_kvm) {
2740 error_report("invalid accelerator %s", acc);
2741 }
6f6e1698
PB
2742 return 0;
2743 }
fc5cf826 2744 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
fe174132 2745 object_apply_compat_props(OBJECT(accel));
12ceaef6
PB
2746 qemu_opt_foreach(opts, accelerator_set_property,
2747 accel,
2748 &error_fatal);
2749
fc5cf826 2750 ret = accel_init_machine(accel, current_machine);
6f6e1698
PB
2751 if (ret < 0) {
2752 *p_init_failed = true;
eaf65114
TH
2753 if (!qtest_with_kvm || ret != -ENOENT) {
2754 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2755 }
6f6e1698
PB
2756 return 0;
2757 }
fe174132 2758
6f6e1698 2759 return 1;
deda73e8
PB
2760}
2761
28a09617 2762static void configure_accelerators(const char *progname)
deda73e8 2763{
28a09617 2764 const char *accel;
28a09617 2765 bool init_failed = false;
28a09617
PB
2766
2767 qemu_opts_foreach(qemu_find_opts("icount"),
2768 do_configure_icount, NULL, &error_fatal);
2769
2770 accel = qemu_opt_get(qemu_get_machine_opts(), "accel");
6f6e1698 2771 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
60ee3552
RH
2772 char **accel_list, **tmp;
2773
6f6e1698
PB
2774 if (accel == NULL) {
2775 /* Select the default accelerator */
755ee1f3
RH
2776 bool have_tcg = accel_find("tcg");
2777 bool have_kvm = accel_find("kvm");
2778
2779 if (have_tcg && have_kvm) {
2780 if (g_str_has_suffix(progname, "kvm")) {
6f6e1698
PB
2781 /* If the program name ends with "kvm", we prefer KVM */
2782 accel = "kvm:tcg";
2783 } else {
2784 accel = "tcg:kvm";
2785 }
755ee1f3
RH
2786 } else if (have_kvm) {
2787 accel = "kvm";
2788 } else if (have_tcg) {
2789 accel = "tcg";
2790 } else {
2791 error_report("No accelerator selected and"
2792 " no default accelerator available");
2793 exit(1);
28a09617
PB
2794 }
2795 }
6f6e1698 2796 accel_list = g_strsplit(accel, ":", 0);
28a09617 2797
a024b090 2798 for (tmp = accel_list; *tmp; tmp++) {
6f6e1698
PB
2799 /*
2800 * Filter invalid accelerators here, to prevent obscenities
2801 * such as "-machine accel=tcg,,thread=single".
2802 */
2803 if (accel_find(*tmp)) {
2804 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
8b90f1c5
PB
2805 } else {
2806 init_failed = true;
2807 error_report("invalid accelerator %s", *tmp);
6f6e1698 2808 }
28a09617 2809 }
adb464ff 2810 g_strfreev(accel_list);
6f6e1698
PB
2811 } else {
2812 if (accel != NULL) {
2813 error_report("The -accel and \"-machine accel=\" options are incompatible");
2814 exit(1);
28a09617
PB
2815 }
2816 }
28a09617 2817
6f6e1698
PB
2818 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2819 do_configure_accelerator, &init_failed, &error_fatal)) {
28a09617 2820 if (!init_failed) {
6f6e1698 2821 error_report("no accelerator found");
28a09617
PB
2822 }
2823 exit(1);
2824 }
2825
eaf65114 2826 if (init_failed && !qtest_chrdev) {
4f7f5893 2827 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
8b90f1c5 2828 error_report("falling back to %s", ac->name);
28a09617
PB
2829 }
2830
740b1759 2831 if (icount_enabled() && !tcg_enabled()) {
deda73e8
PB
2832 error_report("-icount is not allowed with hardware virtualization");
2833 exit(1);
2834 }
2835}
2836
ffac16fa 2837static void create_default_memdev(MachineState *ms, const char *path)
900c0ba3
IM
2838{
2839 Object *obj;
2840 MachineClass *mc = MACHINE_GET_CLASS(ms);
2841
2842 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
2843 if (path) {
5325cc34 2844 object_property_set_str(obj, "mem-path", path, &error_fatal);
900c0ba3 2845 }
5325cc34 2846 object_property_set_int(obj, "size", ms->ram_size, &error_fatal);
900c0ba3 2847 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
d2623129 2848 obj);
d96c4d5f 2849 /* Ensure backend's memory region name is equal to mc->default_ram_id */
5325cc34
MA
2850 object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
2851 false, &error_fatal);
900c0ba3
IM
2852 user_creatable_complete(USER_CREATABLE(obj), &error_fatal);
2853 object_unref(obj);
5325cc34 2854 object_property_set_str(OBJECT(ms), "memory-backend", mc->default_ram_id,
900c0ba3
IM
2855 &error_fatal);
2856}
2857
ea1edcd7
PB
2858/*
2859 * Find a likely location for support files using the location of the binary.
2860 * When running from the build tree this will be "$bindir/pc-bios".
2861 * Otherwise, this is CONFIG_QEMU_DATADIR (possibly relocated).
2862 *
2863 * The caller must use g_free() to free the returned data when it is
2864 * no longer required.
2865 */
2866static char *find_datadir(void)
2867{
2868 g_autofree char *dir = NULL;
2869
2870 dir = g_build_filename(qemu_get_exec_dir(), "pc-bios", NULL);
2871 if (g_file_test(dir, G_FILE_TEST_IS_DIR)) {
2872 return g_steal_pointer(&dir);
2873 }
2874
2875 return get_relocated_path(CONFIG_QEMU_DATADIR);
2876}
2877
58c91595
PB
2878static void qemu_validate_options(void)
2879{
2880 QemuOpts *machine_opts = qemu_get_machine_opts();
2881 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
2882 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
2883 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
2884
2885 if (kernel_filename == NULL) {
2886 if (kernel_cmdline != NULL) {
2887 error_report("-append only allowed with -kernel option");
2888 exit(1);
2889 }
2890
2891 if (initrd_filename != NULL) {
2892 error_report("-initrd only allowed with -kernel option");
2893 exit(1);
2894 }
2895 }
2896
2897 if (incoming && !preconfig_exit_requested) {
2898 error_report("'preconfig' and 'incoming' options are "
2899 "mutually exclusive");
2900 exit(EXIT_FAILURE);
2901 }
2902
2903#ifdef CONFIG_CURSES
2904 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2905 error_report("curses display cannot be used with -daemonize");
2906 exit(1);
2907 }
2908#endif
2909}
2910
0546c060
PB
2911static void qemu_process_early_options(void)
2912{
2913 char **dirs;
2914 int i;
2915
2916#ifdef CONFIG_SECCOMP
2917 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2918 if (olist) {
2919 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2920 }
2921#endif
2922
2923 qemu_opts_foreach(qemu_find_opts("name"),
2924 parse_name, NULL, &error_fatal);
2925
2926#ifndef _WIN32
2927 qemu_opts_foreach(qemu_find_opts("add-fd"),
2928 parse_add_fd, NULL, &error_fatal);
2929
2930 qemu_opts_foreach(qemu_find_opts("add-fd"),
2931 cleanup_add_fd, NULL, &error_fatal);
2932#endif
2933
2934 if (!trace_init_backends()) {
2935 exit(1);
2936 }
2937 trace_init_file();
2938
2939 /* Open the logfile at this point and set the log mask if necessary. */
2940 qemu_set_log_filename(log_file, &error_fatal);
2941 if (log_mask) {
2942 int mask;
2943 mask = qemu_str_to_log_mask(log_mask);
2944 if (!mask) {
2945 qemu_print_log_usage(stdout);
2946 exit(1);
2947 }
2948 qemu_set_log(mask);
2949 } else {
2950 qemu_set_log(0);
2951 }
2952
2953 /* add configured firmware directories */
2954 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
2955 for (i = 0; dirs[i] != NULL; i++) {
2956 qemu_add_data_dir(get_relocated_path(dirs[i]));
2957 }
2958 g_strfreev(dirs);
2959
2960 /* try to find datadir relative to the executable path */
2961 qemu_add_data_dir(find_datadir());
2962}
2963
2964static void qemu_process_help_options(void)
2965{
2966 int i;
2967
2968 /*
2969 * Check for -cpu help and -device help before we call select_machine(),
2970 * which will return an error if the architecture has no default machine
2971 * type and the user did not specify one, so that the user doesn't need
2972 * to say '-cpu help -machine something'.
2973 */
2974 if (cpu_option && is_help_option(cpu_option)) {
2975 list_cpus(cpu_option);
2976 exit(0);
2977 }
2978
2979 if (qemu_opts_foreach(qemu_find_opts("device"),
2980 device_help_func, NULL, NULL)) {
2981 exit(0);
2982 }
2983
2984 /* -L help lists the data directories and exits. */
2985 if (list_data_dirs) {
2986 for (i = 0; i < data_dir_idx; i++) {
2987 printf("%s\n", data_dir[i]);
2988 }
2989 exit(0);
2990 }
2991}
2992
2993static void qemu_maybe_daemonize(const char *pid_file)
2994{
2995 Error *err;
2996
2997 os_daemonize();
2998 rcu_disable_atfork();
2999
3000 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
3001 error_reportf_err(err, "cannot create PID file: ");
3002 exit(1);
3003 }
3004
3005 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
3006 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
3007}
3008
efd7ab22
PB
3009static void qemu_init_subsystems(void)
3010{
3011 Error *err;
3012
3013 os_set_line_buffering();
3014
3015 module_call_init(MODULE_INIT_TRACE);
3016
3017 qemu_init_cpu_list();
3018 qemu_init_cpu_loop();
3019 qemu_mutex_lock_iothread();
3020
3021 atexit(qemu_run_exit_notifiers);
3022
3023 module_call_init(MODULE_INIT_QOM);
3024 module_call_init(MODULE_INIT_MIGRATION);
3025
3026 runstate_init();
3027 precopy_infrastructure_init();
3028 postcopy_infrastructure_init();
3029 monitor_init_globals();
3030
3031 if (qcrypto_init(&err) < 0) {
3032 error_reportf_err(err, "cannot initialize crypto: ");
3033 exit(1);
3034 }
3035
3036 os_setup_early_signal_handling();
3037
3038 bdrv_init_with_whitelist();
3039 socket_init();
3040}
0546c060 3041
d8e4de41
PB
3042/*
3043 * Called after leaving preconfig state. From here on runstate is
3044 * RUN_STATE_PRELAUNCH or RUN_STATE_INMIGRATE.
3045 */
3046static void qemu_init_board(void)
3047{
3048 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
3049
3050 if (machine_class->default_ram_id && current_machine->ram_size &&
3051 numa_uses_legacy_mem() && !current_machine->ram_memdev_id) {
3052 create_default_memdev(current_machine, mem_path);
3053 }
3054
3055 machine_run_board_init(current_machine);
3056
3057 /*
3058 * TODO To drop support for deprecated bogus if=..., move
3059 * drive_check_orphaned() here, replacing this call. Also drop
3060 * its deprecation warning, along with DriveInfo member
3061 * @claimed_by_board.
3062 */
3063 drive_mark_claimed_by_board();
3064
3065 realtime_init();
3066
3067 if (hax_enabled()) {
3068 /* FIXME: why isn't cpu_synchronize_all_post_init enough? */
3069 hax_sync_vcpus();
3070 }
3071}
3072
3073static void qemu_create_cli_devices(void)
3074{
3075 soundhw_init();
3076
3077 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
3078 parse_fw_cfg, fw_cfg_find(), &error_fatal);
3079
3080 /* init USB devices */
3081 if (machine_usb(current_machine)) {
3082 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3083 exit(1);
3084 }
3085
3086 /* init generic devices */
3087 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
3088 qemu_opts_foreach(qemu_find_opts("device"),
3089 device_init_func, NULL, &error_fatal);
3090 rom_reset_order_override();
3091}
3092
3093static void qemu_machine_creation_done(void)
3094{
3095 DisplayState *ds;
3096
3097 cpu_synchronize_all_post_init();
3098
3099 /* Did we create any drives that we failed to create a device for? */
3100 drive_check_orphaned();
3101
3102 /* Don't warn about the default network setup that you get if
3103 * no command line -net or -netdev options are specified. There
3104 * are two cases that we would otherwise complain about:
3105 * (1) board doesn't support a NIC but the implicit "-net nic"
3106 * requested one
3107 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
3108 * sets up a nic that isn't connected to anything.
3109 */
3110 if (!default_net && (!qtest_enabled() || has_defaults)) {
3111 net_check_clients();
3112 }
3113
3114 if (boot_once) {
3115 qemu_boot_set(boot_once, &error_fatal);
3116 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
3117 }
3118
3119 /* init local displays */
3120 ds = init_displaystate();
3121 qemu_display_init(ds, &dpy);
3122
3123 /* must be after terminal init, SDL library changes signal handlers */
3124 os_setup_signal_handling();
3125
3126 /* init remote displays */
3127#ifdef CONFIG_VNC
3128 qemu_opts_foreach(qemu_find_opts("vnc"),
3129 vnc_init_func, NULL, &error_fatal);
3130#endif
3131
3132 if (using_spice) {
3133 qemu_spice.display_init();
3134 }
3135
3136 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3137 exit(1);
3138 }
3139
3140 qdev_machine_creation_done();
3141
3142 /* TODO: once all bus devices are qdevified, this should be done
3143 * when bus is created by qdev.c */
3144 /*
3145 * TODO: If we had a main 'reset container' that the whole system
3146 * lived in, we could reset that using the multi-phase reset
3147 * APIs. For the moment, we just reset the sysbus, which will cause
3148 * all devices hanging off it (and all their child buses, recursively)
3149 * to be reset. Note that this will *not* reset any Device objects
3150 * which are not attached to some part of the qbus tree!
3151 */
3152 qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
3153 qemu_run_machine_init_done_notifiers();
3154
3155 if (rom_check_and_register_reset() != 0) {
3156 error_report("rom check and register reset failed");
3157 exit(1);
3158 }
3159
3160 replay_start();
3161
3162 /* This checkpoint is required by replay to separate prior clock
3163 reading from the other reads, because timer polling functions query
3164 clock values from the log. */
3165 replay_checkpoint(CHECKPOINT_RESET);
3166 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
3167 register_global_state();
3168}
3169
7b733862 3170void qemu_init(int argc, char **argv, char **envp)
0824d6fc 3171{
e4bcb14c 3172 int i;
efd7ab22 3173 int snapshot = 0;
8d4e9146 3174 QemuOpts *opts, *machine_opts;
d1cdd92e 3175 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
03b0ba70 3176 QemuOptsList *olist;
cd6f1169 3177 int optind;
6530a97b 3178 const char *optarg;
d63d307f 3179 const char *loadvm = NULL;
0056ae24 3180 MachineClass *machine_class;
d44229c5 3181 const char *vga_model = NULL;
f29a5614 3182 bool userconfig = true;
cfc58cf3 3183 bool nographic = false;
1f0dfe02 3184 int display_remote = 0;
3b9985e9 3185 ram_addr_t maxram_size;
c270fb9e 3186 uint64_t ram_slots = 0;
abfd9ce3 3187 FILE *vmstate_dump_file = NULL;
ddbb0d09 3188 Error *err = NULL;
2a7b18a3 3189 bool have_custom_ram_size;
651af51c 3190 BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
42229a75 3191 QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
4ebc74db 3192 int mem_prealloc = 0; /* force preallocation of physical target memory */
0b5538c3 3193
4d454574 3194 qemu_add_opts(&qemu_drive_opts);
968854c8
AK
3195 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3196 qemu_add_drive_opts(&qemu_common_drive_opts);
3197 qemu_add_drive_opts(&qemu_drive_opts);
c5f3014b 3198 qemu_add_drive_opts(&bdrv_runtime_opts);
4d454574
PB
3199 qemu_add_opts(&qemu_chardev_opts);
3200 qemu_add_opts(&qemu_device_opts);
3201 qemu_add_opts(&qemu_netdev_opts);
78cd6f7b 3202 qemu_add_opts(&qemu_nic_opts);
4d454574
PB
3203 qemu_add_opts(&qemu_net_opts);
3204 qemu_add_opts(&qemu_rtc_opts);
3205 qemu_add_opts(&qemu_global_opts);
3206 qemu_add_opts(&qemu_mon_opts);
3207 qemu_add_opts(&qemu_trace_opts);
42229a75 3208 qemu_plugin_add_opts();
4d454574
PB
3209 qemu_add_opts(&qemu_option_rom_opts);
3210 qemu_add_opts(&qemu_machine_opts);
8d4e9146 3211 qemu_add_opts(&qemu_accel_opts);
6e1d3c1c 3212 qemu_add_opts(&qemu_mem_opts);
12b7f57e 3213 qemu_add_opts(&qemu_smp_opts);
4d454574 3214 qemu_add_opts(&qemu_boot_opts);
4d454574
PB
3215 qemu_add_opts(&qemu_add_fd_opts);
3216 qemu_add_opts(&qemu_object_opts);
d1a0cf73 3217 qemu_add_opts(&qemu_tpmdev_opts);
888a6bc6 3218 qemu_add_opts(&qemu_realtime_opts);
1fdd4748 3219 qemu_add_opts(&qemu_overcommit_opts);
5e2ac519 3220 qemu_add_opts(&qemu_msg_opts);
5d12f961 3221 qemu_add_opts(&qemu_name_opts);
0042109a 3222 qemu_add_opts(&qemu_numa_opts);
1ad9580b 3223 qemu_add_opts(&qemu_icount_opts);
a38bb079 3224 qemu_add_opts(&qemu_semihosting_config_opts);
81b2b810 3225 qemu_add_opts(&qemu_fw_cfg_opts);
34294e2f 3226 module_call_init(MODULE_INIT_OPTS);
4d454574 3227
efd7ab22
PB
3228 error_init(argv[0]);
3229 qemu_init_exec_dir(argv[0]);
142c6b1a 3230
efd7ab22 3231 qemu_init_subsystems();
41bd639b 3232
292444cb
AL
3233 /* first pass of option parsing */
3234 optind = 1;
3235 while (optind < argc) {
3236 if (argv[optind][0] != '-') {
3237 /* disk image */
28e68d68 3238 optind++;
292444cb
AL
3239 } else {
3240 const QEMUOption *popt;
3241
3242 popt = lookup_opt(argc, argv, &optarg, &optind);
3243 switch (popt->index) {
f29a5614
EH
3244 case QEMU_OPTION_nouserconfig:
3245 userconfig = false;
3246 break;
292444cb
AL
3247 }
3248 }
3249 }
3250
1ea06c39 3251 if (userconfig) {
726401be 3252 if (qemu_read_default_config_file() < 0) {
dcfb0939 3253 exit(1);
292444cb
AL
3254 }
3255 }
3256
3257 /* second pass of option parsing */
cd6f1169 3258 optind = 1;
0824d6fc 3259 for(;;) {
cd6f1169 3260 if (optind >= argc)
0824d6fc 3261 break;
6530a97b 3262 if (argv[optind][0] != '-') {
17f30eae 3263 loc_set_cmdline(argv, optind, 1);
d1cdd92e 3264 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
cd6f1169
FB
3265 } else {
3266 const QEMUOption *popt;
3267
6530a97b 3268 popt = lookup_opt(argc, argv, &optarg, &optind);
ad96090a 3269 if (!(popt->arch_mask & arch_type)) {
da002526 3270 error_report("Option not supported for this target");
ad96090a
BS
3271 exit(1);
3272 }
cd6f1169 3273 switch(popt->index) {
94fc95cd
JM
3274 case QEMU_OPTION_cpu:
3275 /* hw initialization will check this */
c1c8cfe5 3276 cpu_option = optarg;
94fc95cd 3277 break;
cd6f1169 3278 case QEMU_OPTION_hda:
cd6f1169 3279 case QEMU_OPTION_hdb:
cc1daa40
FB
3280 case QEMU_OPTION_hdc:
3281 case QEMU_OPTION_hdd:
2292ddae
MA
3282 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3283 HD_OPTS);
fc01f7e7 3284 break;
42e5f393
MA
3285 case QEMU_OPTION_blockdev:
3286 {
3287 Visitor *v;
651af51c 3288 BlockdevOptionsQueueEntry *bdo;
42e5f393 3289
92917cd2
MA
3290 v = qobject_input_visitor_new_str(optarg, "driver",
3291 &error_fatal);
42e5f393 3292
651af51c 3293 bdo = g_new(BlockdevOptionsQueueEntry, 1);
42e5f393
MA
3294 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3295 &error_fatal);
3296 visit_free(v);
3297 loc_save(&bdo->loc);
3298 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3299 break;
3300 }
e4bcb14c 3301 case QEMU_OPTION_drive:
e2982c3a
MT
3302 if (drive_def(optarg) == NULL) {
3303 exit(1);
3304 }
99efa84d 3305 break;
d058fe03
GH
3306 case QEMU_OPTION_set:
3307 if (qemu_set_option(optarg) != 0)
3308 exit(1);
99efa84d 3309 break;
d0fef6fb
GH
3310 case QEMU_OPTION_global:
3311 if (qemu_global_option(optarg) != 0)
3312 exit(1);
99efa84d 3313 break;
3e3d5815 3314 case QEMU_OPTION_mtdblock:
2292ddae 3315 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3e3d5815 3316 break;
a1bb27b1 3317 case QEMU_OPTION_sd:
80f4d9fc 3318 drive_add(IF_SD, -1, optarg, SD_OPTS);
a1bb27b1 3319 break;
86f55663 3320 case QEMU_OPTION_pflash:
2292ddae 3321 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
86f55663 3322 break;
cd6f1169 3323 case QEMU_OPTION_snapshot:
25863975
PD
3324 {
3325 Error *blocker = NULL;
3326 snapshot = 1;
3327 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
3328 "-snapshot");
3329 replay_add_blocker(blocker);
3330 }
33e3963e 3331 break;
268a362c 3332 case QEMU_OPTION_numa:
70b94331
MA
3333 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3334 optarg, true);
0042109a
WG
3335 if (!opts) {
3336 exit(1);
3337 }
268a362c 3338 break;
1472a95b 3339 case QEMU_OPTION_display:
e3af9f9a 3340 parse_display(optarg);
1472a95b 3341 break;
cd6f1169 3342 case QEMU_OPTION_nographic:
cfc58cf3
EH
3343 olist = qemu_find_opts("machine");
3344 qemu_opts_parse_noisily(olist, "graphics=off", false);
3345 nographic = true;
0c8d7065 3346 dpy.type = DISPLAY_TYPE_NONE;
a20dd508 3347 break;
4d3b6f6e 3348 case QEMU_OPTION_curses:
47b05369 3349#ifdef CONFIG_CURSES
14f130fa 3350 dpy.type = DISPLAY_TYPE_CURSES;
47b05369 3351#else
e08bb301 3352 error_report("curses or iconv support is disabled");
47b05369 3353 exit(1);
4d3b6f6e 3354#endif
47b05369 3355 break;
a171fe39 3356 case QEMU_OPTION_portrait:
9312805d
VK
3357 graphic_rotate = 90;
3358 break;
3359 case QEMU_OPTION_rotate:
3360 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3361 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3362 graphic_rotate != 180 && graphic_rotate != 270) {
f61eddcb 3363 error_report("only 90, 180, 270 deg rotation is available");
9312805d
VK
3364 exit(1);
3365 }
a171fe39 3366 break;
cd6f1169 3367 case QEMU_OPTION_kernel:
8ef3a4be 3368 qemu_opts_set(qemu_find_opts("machine"), NULL, "kernel", optarg,
79087c78 3369 &error_abort);
a0abe474
PM
3370 break;
3371 case QEMU_OPTION_initrd:
8ef3a4be 3372 qemu_opts_set(qemu_find_opts("machine"), NULL, "initrd", optarg,
79087c78 3373 &error_abort);
a20dd508 3374 break;
cd6f1169 3375 case QEMU_OPTION_append:
8ef3a4be 3376 qemu_opts_set(qemu_find_opts("machine"), NULL, "append", optarg,
79087c78 3377 &error_abort);
313aa567 3378 break;
412beee6 3379 case QEMU_OPTION_dtb:
8ef3a4be 3380 qemu_opts_set(qemu_find_opts("machine"), NULL, "dtb", optarg,
79087c78 3381 &error_abort);
412beee6 3382 break;
cd6f1169 3383 case QEMU_OPTION_cdrom:
2292ddae 3384 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
36b486bb 3385 break;
cd6f1169 3386 case QEMU_OPTION_boot:
70b94331
MA
3387 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3388 optarg, true);
8281abd5
MA
3389 if (!opts) {
3390 exit(1);
36b486bb
FB
3391 }
3392 break;
cd6f1169 3393 case QEMU_OPTION_fda:
cd6f1169 3394 case QEMU_OPTION_fdb:
2292ddae
MA
3395 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3396 optarg, FD_OPTS);
c45886db 3397 break;
52ca8d6a
FB
3398 case QEMU_OPTION_no_fd_bootchk:
3399 fd_bootchk = 0;
3400 break;
a1ea458f 3401 case QEMU_OPTION_netdev:
d30300f7 3402 default_net = 0;
3329f07b 3403 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
a1ea458f
MM
3404 exit(1);
3405 }
3406 break;
78cd6f7b
TH
3407 case QEMU_OPTION_nic:
3408 default_net = 0;
3409 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3410 exit(1);
3411 }
3412 break;
7c9d8e07 3413 case QEMU_OPTION_net:
d30300f7 3414 default_net = 0;
3329f07b 3415 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
c4b1fcc0
FB
3416 exit(1);
3417 }
702c651c 3418 break;
f9dadc98
RS
3419#ifdef CONFIG_LIBISCSI
3420 case QEMU_OPTION_iscsi:
70b94331
MA
3421 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3422 optarg, false);
f9dadc98
RS
3423 if (!opts) {
3424 exit(1);
3425 }
3426 break;
c7f74643 3427#endif
1d14ffa9 3428 case QEMU_OPTION_audio_help:
71830221 3429 audio_legacy_help();
1d14ffa9
FB
3430 exit (0);
3431 break;
71830221
KZ
3432 case QEMU_OPTION_audiodev:
3433 audio_parse_option(optarg);
3434 break;
1d14ffa9
FB
3435 case QEMU_OPTION_soundhw:
3436 select_soundhw (optarg);
3437 break;
cd6f1169 3438 case QEMU_OPTION_h:
15f82208 3439 help(0);
cd6f1169 3440 break;
9bd7e6d9
PB
3441 case QEMU_OPTION_version:
3442 version();
3443 exit(0);
3444 break;
3b9985e9 3445 case QEMU_OPTION_m:
70b94331
MA
3446 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3447 optarg, true);
6e1d3c1c
IM
3448 if (!opts) {
3449 exit(EXIT_FAILURE);
3450 }
cd6f1169 3451 break;
d1a0cf73
SB
3452#ifdef CONFIG_TPM
3453 case QEMU_OPTION_tpmdev:
3454 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3455 exit(1);
3456 }
3457 break;
3458#endif
c902760f
MT
3459 case QEMU_OPTION_mempath:
3460 mem_path = optarg;
3461 break;
c902760f
MT
3462 case QEMU_OPTION_mem_prealloc:
3463 mem_prealloc = 1;
3464 break;
cd6f1169 3465 case QEMU_OPTION_d:
c235d738
MF
3466 log_mask = optarg;
3467 break;
3468 case QEMU_OPTION_D:
3469 log_file = optarg;
cd6f1169 3470 break;
3514552e 3471 case QEMU_OPTION_DFILTER:
bd6fee9f 3472 qemu_set_dfilter_ranges(optarg, &error_fatal);
3514552e 3473 break;
9c09a251
RH
3474 case QEMU_OPTION_seed:
3475 qemu_guest_random_seed_main(optarg, &error_fatal);
3476 break;
cd6f1169 3477 case QEMU_OPTION_s:
ef0c4a0d 3478 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
cd6f1169 3479 break;
59030a8c 3480 case QEMU_OPTION_gdb:
ef0c4a0d 3481 add_device_config(DEV_GDB, optarg);
cd6f1169 3482 break;
cd6f1169 3483 case QEMU_OPTION_L:
37146e7e
RJ
3484 if (is_help_option(optarg)) {
3485 list_data_dirs = true;
2a1cce90 3486 } else {
ea1edcd7 3487 qemu_add_data_dir(g_strdup(optarg));
4524051c 3488 }
cd6f1169 3489 break;
1192dad8 3490 case QEMU_OPTION_bios:
8ef3a4be 3491 qemu_opts_set(qemu_find_opts("machine"), NULL, "firmware", optarg,
79087c78 3492 &error_abort);
1192dad8 3493 break;
1b530a6d
AJ
3494 case QEMU_OPTION_singlestep:
3495 singlestep = 1;
3496 break;
cd6f1169 3497 case QEMU_OPTION_S:
3c07f8e8 3498 autostart = 0;
cd6f1169 3499 break;
99efa84d
MA
3500 case QEMU_OPTION_k:
3501 keyboard_layout = optarg;
3502 break;
3893c124 3503 case QEMU_OPTION_vga:
a369da5f 3504 vga_model = optarg;
7f1b17f2 3505 default_vga = 0;
1bfe856e 3506 break;
e9b137c2
FB
3507 case QEMU_OPTION_g:
3508 {
3509 const char *p;
3510 int w, h, depth;
3511 p = optarg;
3512 w = strtol(p, (char **)&p, 10);
3513 if (w <= 0) {
3514 graphic_error:
f61eddcb 3515 error_report("invalid resolution or depth");
e9b137c2
FB
3516 exit(1);
3517 }
3518 if (*p != 'x')
3519 goto graphic_error;
3520 p++;
3521 h = strtol(p, (char **)&p, 10);
3522 if (h <= 0)
3523 goto graphic_error;
3524 if (*p == 'x') {
3525 p++;
3526 depth = strtol(p, (char **)&p, 10);
8ac919a0
LV
3527 if (depth != 1 && depth != 2 && depth != 4 &&
3528 depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
3529 depth != 24 && depth != 32)
3530 goto graphic_error;
3531 } else if (*p == '\0') {
3532 depth = graphic_depth;
3533 } else {
3534 goto graphic_error;
3535 }
3b46e624 3536
e9b137c2
FB
3537 graphic_width = w;
3538 graphic_height = h;
3539 graphic_depth = depth;
3540 }
3541 break;
20d8a3ed
TS
3542 case QEMU_OPTION_echr:
3543 {
3544 char *r;
3545 term_escape_char = strtol(optarg, &r, 0);
3546 if (r == optarg)
3547 printf("Bad argument to echr\n");
3548 break;
3549 }
82c643ff 3550 case QEMU_OPTION_monitor:
6ca5582d 3551 default_monitor = 0;
70e098af 3552 if (strncmp(optarg, "none", 4)) {
4821cd4c 3553 monitor_parse(optarg, "readline", false);
70e098af 3554 }
6ca5582d
GH
3555 break;
3556 case QEMU_OPTION_qmp:
4821cd4c
HR
3557 monitor_parse(optarg, "control", false);
3558 default_monitor = 0;
3559 break;
3560 case QEMU_OPTION_qmp_pretty:
3561 monitor_parse(optarg, "control", true);
2d114dc1 3562 default_monitor = 0;
82c643ff 3563 break;
22a0e04b 3564 case QEMU_OPTION_mon:
70b94331
MA
3565 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3566 true);
22a0e04b 3567 if (!opts) {
22a0e04b
GH
3568 exit(1);
3569 }
2d114dc1 3570 default_monitor = 0;
22a0e04b 3571 break;
191bc01b 3572 case QEMU_OPTION_chardev:
70b94331
MA
3573 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3574 optarg, true);
191bc01b 3575 if (!opts) {
191bc01b
GH
3576 exit(1);
3577 }
191bc01b 3578 break;
74db920c 3579 case QEMU_OPTION_fsdev:
03b0ba70
GH
3580 olist = qemu_find_opts("fsdev");
3581 if (!olist) {
5dfdae81 3582 error_report("fsdev support is disabled");
03b0ba70
GH
3583 exit(1);
3584 }
70b94331 3585 opts = qemu_opts_parse_noisily(olist, optarg, true);
74db920c 3586 if (!opts) {
74db920c
GS
3587 exit(1);
3588 }
3589 break;
3d54abc7 3590 case QEMU_OPTION_virtfs: {
e14ea479
SH
3591 QemuOpts *fsdev;
3592 QemuOpts *device;
1a6ed33c
AM
3593 const char *writeout, *sock_fd, *socket, *path, *security_model,
3594 *multidevs;
3d54abc7 3595
03b0ba70
GH
3596 olist = qemu_find_opts("virtfs");
3597 if (!olist) {
5dfdae81 3598 error_report("virtfs support is disabled");
03b0ba70
GH
3599 exit(1);
3600 }
70b94331 3601 opts = qemu_opts_parse_noisily(olist, optarg, true);
3d54abc7 3602 if (!opts) {
3d54abc7
GS
3603 exit(1);
3604 }
3605
fbcbf101 3606 if (qemu_opt_get(opts, "fsdriver") == NULL ||
99519f0a 3607 qemu_opt_get(opts, "mount_tag") == NULL) {
8afb9000 3608 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
9ce56db6
VJ
3609 exit(1);
3610 }
e14ea479 3611 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3baa0a6a 3612 qemu_opts_id(opts) ?:
8be7e7e4
LC
3613 qemu_opt_get(opts, "mount_tag"),
3614 1, NULL);
e14ea479 3615 if (!fsdev) {
3baa0a6a 3616 error_report("duplicate or invalid fsdev id: %s",
f61eddcb 3617 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
3618 exit(1);
3619 }
d3ab98e6
AK
3620
3621 writeout = qemu_opt_get(opts, "writeout");
3622 if (writeout) {
3623#ifdef CONFIG_SYNC_FILE_RANGE
f43e47db 3624 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
d3ab98e6 3625#else
f61eddcb
EH
3626 error_report("writeout=immediate not supported "
3627 "on this platform");
d3ab98e6
AK
3628 exit(1);
3629#endif
3630 }
f43e47db
MA
3631 qemu_opt_set(fsdev, "fsdriver",
3632 qemu_opt_get(opts, "fsdriver"), &error_abort);
32b69436
GK
3633 path = qemu_opt_get(opts, "path");
3634 if (path) {
3635 qemu_opt_set(fsdev, "path", path, &error_abort);
3636 }
3637 security_model = qemu_opt_get(opts, "security_model");
3638 if (security_model) {
3639 qemu_opt_set(fsdev, "security_model", security_model,
3640 &error_abort);
3641 }
84a87cc4
MK
3642 socket = qemu_opt_get(opts, "socket");
3643 if (socket) {
f43e47db 3644 qemu_opt_set(fsdev, "socket", socket, &error_abort);
84a87cc4 3645 }
4c793dda
MK
3646 sock_fd = qemu_opt_get(opts, "sock_fd");
3647 if (sock_fd) {
f43e47db 3648 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
4c793dda 3649 }
e14ea479 3650
2c74c2cb 3651 qemu_opt_set_bool(fsdev, "readonly",
cccb7967
MA
3652 qemu_opt_get_bool(opts, "readonly", 0),
3653 &error_abort);
1a6ed33c
AM
3654 multidevs = qemu_opt_get(opts, "multidevs");
3655 if (multidevs) {
3656 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3657 }
87ea75d5
PC
3658 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3659 &error_abort);
f43e47db 3660 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
e14ea479 3661 qemu_opt_set(device, "fsdev",
3baa0a6a 3662 qemu_opts_id(fsdev), &error_abort);
e14ea479 3663 qemu_opt_set(device, "mount_tag",
f43e47db 3664 qemu_opt_get(opts, "mount_tag"), &error_abort);
3d54abc7
GS
3665 break;
3666 }
82c643ff 3667 case QEMU_OPTION_serial:
998bbd74
GH
3668 add_device_config(DEV_SERIAL, optarg);
3669 default_serial = 0;
18141ed6
JK
3670 if (strncmp(optarg, "mon:", 4) == 0) {
3671 default_monitor = 0;
3672 }
82c643ff 3673 break;
9dd986cc 3674 case QEMU_OPTION_watchdog:
09aaa160 3675 if (watchdog) {
f61eddcb 3676 error_report("only one watchdog option may be given");
7b733862 3677 exit(1);
09aaa160
MA
3678 }
3679 watchdog = optarg;
9dd986cc
RJ
3680 break;
3681 case QEMU_OPTION_watchdog_action:
3682 if (select_watchdog_action(optarg) == -1) {
3e515373 3683 error_report("unknown -watchdog-action parameter");
9dd986cc
RJ
3684 exit(1);
3685 }
3686 break;
6508fe59 3687 case QEMU_OPTION_parallel:
6a5e8b0e
GH
3688 add_device_config(DEV_PARALLEL, optarg);
3689 default_parallel = 0;
18141ed6
JK
3690 if (strncmp(optarg, "mon:", 4) == 0) {
3691 default_monitor = 0;
3692 }
6508fe59 3693 break;
c9f398e5
PA
3694 case QEMU_OPTION_debugcon:
3695 add_device_config(DEV_DEBUGCON, optarg);
3696 break;
99efa84d
MA
3697 case QEMU_OPTION_loadvm:
3698 loadvm = optarg;
3699 break;
d63d307f 3700 case QEMU_OPTION_full_screen:
0c8d7065
GH
3701 dpy.has_full_screen = true;
3702 dpy.full_screen = true;
d63d307f 3703 break;
3780e197
TS
3704 case QEMU_OPTION_alt_grab:
3705 alt_grab = 1;
3706 break;
0ca9f8a4
DK
3707 case QEMU_OPTION_ctrl_grab:
3708 ctrl_grab = 1;
3709 break;
667accab 3710 case QEMU_OPTION_no_quit:
0c8d7065
GH
3711 dpy.has_window_close = true;
3712 dpy.window_close = false;
667accab 3713 break;
7d957bd8 3714 case QEMU_OPTION_sdl:
24f6ff86 3715#ifdef CONFIG_SDL
fe91f36a 3716 dpy.type = DISPLAY_TYPE_SDL;
7d957bd8 3717 break;
58fc096c 3718#else
f61eddcb 3719 error_report("SDL support is disabled");
58fc096c 3720 exit(1);
667accab 3721#endif
f7cce898 3722 case QEMU_OPTION_pidfile:
93815bc2 3723 pid_file = optarg;
f7cce898 3724 break;
a09db21f
FB
3725 case QEMU_OPTION_win2k_hack:
3726 win2k_install_hack = 1;
3727 break;
8a92ea2f 3728 case QEMU_OPTION_acpitable:
70b94331
MA
3729 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3730 optarg, true);
f46e720a
LE
3731 if (!opts) {
3732 exit(1);
3733 }
9f57061c 3734 acpi_table_add(opts, &error_fatal);
8a92ea2f 3735 break;
b6f6e3d3 3736 case QEMU_OPTION_smbios:
70b94331
MA
3737 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3738 optarg, false);
f46e720a
LE
3739 if (!opts) {
3740 exit(1);
3741 }
1007a37e 3742 smbios_entry_add(opts, &error_fatal);
b6f6e3d3 3743 break;
81b2b810 3744 case QEMU_OPTION_fwcfg:
70b94331
MA
3745 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3746 optarg, true);
81b2b810
GS
3747 if (opts == NULL) {
3748 exit(1);
3749 }
3750 break;
047f7038
IM
3751 case QEMU_OPTION_preconfig:
3752 preconfig_exit_requested = false;
3753 break;
7ba1e619 3754 case QEMU_OPTION_enable_kvm:
303d4e86 3755 olist = qemu_find_opts("machine");
70b94331 3756 qemu_opts_parse_noisily(olist, "accel=kvm", false);
303d4e86 3757 break;
364c3e6b 3758 case QEMU_OPTION_M:
303d4e86
AP
3759 case QEMU_OPTION_machine:
3760 olist = qemu_find_opts("machine");
70b94331 3761 opts = qemu_opts_parse_noisily(olist, optarg, true);
303d4e86 3762 if (!opts) {
303d4e86
AP
3763 exit(1);
3764 }
7ba1e619 3765 break;
a20fa79f 3766 case QEMU_OPTION_accel:
8d4e9146
FK
3767 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3768 optarg, true);
3769 optarg = qemu_opt_get(accel_opts, "accel");
bde4d920 3770 if (!optarg || is_help_option(optarg)) {
cbe6d636
WSM
3771 printf("Accelerators supported in QEMU binary:\n");
3772 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3773 false);
3774 for (el = accel_list; el; el = el->next) {
3775 gchar *typename = g_strdup(object_class_get_name(
3776 OBJECT_CLASS(el->data)));
3777 /* omit qtest which is used for tests only */
3778 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3779 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3780 gchar **optname = g_strsplit(typename,
3781 ACCEL_CLASS_SUFFIX, 0);
3782 printf("%s\n", optname[0]);
4903602e 3783 g_strfreev(optname);
cbe6d636
WSM
3784 }
3785 g_free(typename);
3786 }
3787 g_slist_free(accel_list);
a20fa79f 3788 exit(0);
8d4e9146
FK
3789 }
3790 break;
bb36d470 3791 case QEMU_OPTION_usb:
fa5358c6 3792 olist = qemu_find_opts("machine");
70b94331 3793 qemu_opts_parse_noisily(olist, "usb=on", false);
bb36d470 3794 break;
a594cfbf 3795 case QEMU_OPTION_usbdevice:
a358a3af
TH
3796 error_report("'-usbdevice' is deprecated, please use "
3797 "'-device usb-...' instead");
fa5358c6 3798 olist = qemu_find_opts("machine");
70b94331 3799 qemu_opts_parse_noisily(olist, "usb=on", false);
bd3c948d
GH
3800 add_device_config(DEV_USB, optarg);
3801 break;
3802 case QEMU_OPTION_device:
70b94331
MA
3803 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3804 optarg, true)) {
f31d07d1
GH
3805 exit(1);
3806 }
a594cfbf 3807 break;
6a00d601 3808 case QEMU_OPTION_smp:
70b94331
MA
3809 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3810 optarg, true)) {
6be68d7e
JS
3811 exit(1);
3812 }
6a00d601 3813 break;
99efa84d 3814 case QEMU_OPTION_vnc:
7b1ee0f2 3815 vnc_parse(optarg, &error_fatal);
821601ea 3816 break;
6515b203 3817 case QEMU_OPTION_no_acpi:
17e89077
GH
3818 olist = qemu_find_opts("machine");
3819 qemu_opts_parse_noisily(olist, "acpi=off", false);
6515b203 3820 break;
16b29ae1 3821 case QEMU_OPTION_no_hpet:
0259c78c
EH
3822 olist = qemu_find_opts("machine");
3823 qemu_opts_parse_noisily(olist, "hpet=off", false);
16b29ae1 3824 break;
d1beab82
FB
3825 case QEMU_OPTION_no_reboot:
3826 no_reboot = 1;
3827 break;
b2f76161
AJ
3828 case QEMU_OPTION_no_shutdown:
3829 no_shutdown = 1;
3830 break;
9467cd46 3831 case QEMU_OPTION_show_cursor:
df2ac3cc
GH
3832 warn_report("The -show-cursor option is deprecated. Please "
3833 "add show-cursor=on to your -display options.");
3834 warn_report("When using the default display you can use "
3835 "-display default,show-cursor=on");
09aa82ee
GH
3836 dpy.has_show_cursor = true;
3837 dpy.show_cursor = true;
9467cd46 3838 break;
8fcb1b90 3839 case QEMU_OPTION_uuid:
9c5ce8db 3840 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
4cd70f34 3841 error_report("failed to parse UUID string: wrong format");
8fcb1b90
BS
3842 exit(1);
3843 }
fc3b3295 3844 qemu_uuid_set = true;
8fcb1b90 3845 break;
99efa84d
MA
3846 case QEMU_OPTION_option_rom:
3847 if (nb_option_roms >= MAX_OPTION_ROMS) {
3e515373 3848 error_report("too many option ROMs");
99efa84d
MA
3849 exit(1);
3850 }
70b94331
MA
3851 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3852 optarg, true);
49295ebc
MA
3853 if (!opts) {
3854 exit(1);
3855 }
2e55e842
GN
3856 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3857 option_rom[nb_option_roms].bootindex =
3858 qemu_opt_get_number(opts, "bootindex", -1);
3859 if (!option_rom[nb_option_roms].name) {
f61eddcb 3860 error_report("Option ROM file is not specified");
2e55e842
GN
3861 exit(1);
3862 }
99efa84d
MA
3863 nb_option_roms++;
3864 break;
8e71621f 3865 case QEMU_OPTION_semihosting:
f1672e6f 3866 qemu_semihosting_enable();
a38bb079
LI
3867 break;
3868 case QEMU_OPTION_semihosting_config:
f1672e6f 3869 if (qemu_semihosting_config_options(optarg) != 0) {
a38bb079
LI
3870 exit(1);
3871 }
8e71621f 3872 break;
c35734b2 3873 case QEMU_OPTION_name:
70b94331
MA
3874 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3875 optarg, true);
5d12f961
DDAG
3876 if (!opts) {
3877 exit(1);
3878 }
2880ffb0
MS
3879 /* Capture guest name if -msg guest-name is used later */
3880 error_guest_name = qemu_opt_get(opts, "guest");
c35734b2 3881 break;
66508601
BS
3882 case QEMU_OPTION_prom_env:
3883 if (nb_prom_envs >= MAX_PROM_ENVS) {
3e515373 3884 error_report("too many prom variables");
66508601
BS
3885 exit(1);
3886 }
3887 prom_envs[nb_prom_envs] = optarg;
3888 nb_prom_envs++;
3889 break;
2b8f2d41
AZ
3890 case QEMU_OPTION_old_param:
3891 old_param = 1;
05ebd537 3892 break;
1ed2fc1f 3893 case QEMU_OPTION_rtc:
70b94331
MA
3894 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3895 false);
1ed2fc1f 3896 if (!opts) {
1ed2fc1f 3897 exit(1);
7e0af5d0
FB
3898 }
3899 break;
26a5f13b 3900 case QEMU_OPTION_tb_size:
f940488f
EC
3901#ifndef CONFIG_TCG
3902 error_report("TCG is disabled");
3903 exit(1);
3904#endif
fe174132
PB
3905 warn_report("The -tb-size option is deprecated, use -accel tcg,tb-size instead");
3906 object_register_sugar_prop(ACCEL_CLASS_NAME("tcg"), "tb-size", optarg);
26a5f13b 3907 break;
2e70f6ef 3908 case QEMU_OPTION_icount:
70b94331
MA
3909 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3910 optarg, true);
1ad9580b
ST
3911 if (!icount_opts) {
3912 exit(1);
3913 }
2e70f6ef 3914 break;
5bb7910a 3915 case QEMU_OPTION_incoming:
7c76235a
DDAG
3916 if (!incoming) {
3917 runstate_set(RUN_STATE_INMIGRATE);
3918 }
5bb7910a
AL
3919 incoming = optarg;
3920 break;
d15c05fc 3921 case QEMU_OPTION_only_migratable:
811f8652 3922 only_migratable = 1;
d15c05fc 3923 break;
d8c208dd 3924 case QEMU_OPTION_nodefaults:
d44229c5 3925 has_defaults = 0;
d8c208dd 3926 break;
e37630ca 3927 case QEMU_OPTION_xen_domid:
ad96090a 3928 if (!(xen_available())) {
da002526 3929 error_report("Option not supported for this target");
ad96090a
BS
3930 exit(1);
3931 }
e37630ca
AL
3932 xen_domid = atoi(optarg);
3933 break;
e37630ca 3934 case QEMU_OPTION_xen_attach:
ad96090a 3935 if (!(xen_available())) {
da002526 3936 error_report("Option not supported for this target");
ad96090a
BS
3937 exit(1);
3938 }
e37630ca
AL
3939 xen_mode = XEN_ATTACH;
3940 break;
1c599472
PD
3941 case QEMU_OPTION_xen_domid_restrict:
3942 if (!(xen_available())) {
3943 error_report("Option not supported for this target");
3944 exit(1);
3945 }
3946 xen_domid_restrict = true;
3947 break;
ab6540d5 3948 case QEMU_OPTION_trace:
92eecfff 3949 trace_opt_parse(optarg);
ab6540d5 3950 break;
42229a75
LV
3951 case QEMU_OPTION_plugin:
3952 qemu_plugin_opt_parse(optarg, &plugin_list);
3953 break;
715a664a
GH
3954 case QEMU_OPTION_readconfig:
3955 {
dcfb0939
KW
3956 int ret = qemu_read_config_file(optarg);
3957 if (ret < 0) {
f61eddcb
EH
3958 error_report("read config %s: %s", optarg,
3959 strerror(-ret));
715a664a
GH
3960 exit(1);
3961 }
715a664a
GH
3962 break;
3963 }
29b0040b 3964 case QEMU_OPTION_spice:
9ed345a1
GH
3965 olist = qemu_find_opts_err("spice", NULL);
3966 if (!olist) {
3967 ui_module_load_one("spice-core");
3968 olist = qemu_find_opts("spice");
3969 }
29b0040b 3970 if (!olist) {
5dfdae81 3971 error_report("spice support is disabled");
29b0040b
GH
3972 exit(1);
3973 }
70b94331 3974 opts = qemu_opts_parse_noisily(olist, optarg, false);
29b0040b 3975 if (!opts) {
29b0040b
GH
3976 exit(1);
3977 }
f963e4d0 3978 display_remote++;
29b0040b 3979 break;
715a664a
GH
3980 case QEMU_OPTION_writeconfig:
3981 {
3982 FILE *fp;
3983 if (strcmp(optarg, "-") == 0) {
3984 fp = stdout;
3985 } else {
3986 fp = fopen(optarg, "w");
3987 if (fp == NULL) {
f61eddcb
EH
3988 error_report("open %s: %s", optarg,
3989 strerror(errno));
715a664a
GH
3990 exit(1);
3991 }
3992 }
3993 qemu_config_write(fp);
7fb8b5d9
CG
3994 if (fp != stdout) {
3995 fclose(fp);
3996 }
715a664a
GH
3997 break;
3998 }
c7f0f3b1
AL
3999 case QEMU_OPTION_qtest:
4000 qtest_chrdev = optarg;
4001 break;
4002 case QEMU_OPTION_qtest_log:
4003 qtest_log = optarg;
4004 break;
7d76ad4f 4005 case QEMU_OPTION_sandbox:
2bb814a4
MAL
4006 olist = qemu_find_opts("sandbox");
4007 if (!olist) {
4008#ifndef CONFIG_SECCOMP
4009 error_report("-sandbox support is not enabled "
4010 "in this QEMU binary");
4011#endif
4012 exit(1);
4013 }
4014
4015 opts = qemu_opts_parse_noisily(olist, optarg, true);
7d76ad4f 4016 if (!opts) {
49295ebc 4017 exit(1);
7d76ad4f
EO
4018 }
4019 break;
587ed6be
CB
4020 case QEMU_OPTION_add_fd:
4021#ifndef _WIN32
70b94331
MA
4022 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4023 optarg, false);
587ed6be 4024 if (!opts) {
49295ebc 4025 exit(1);
587ed6be
CB
4026 }
4027#else
4028 error_report("File descriptor passing is disabled on this "
4029 "platform");
4030 exit(1);
4031#endif
4032 break;
68d98d3e 4033 case QEMU_OPTION_object:
70b94331
MA
4034 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4035 optarg, true);
49295ebc
MA
4036 if (!opts) {
4037 exit(1);
4038 }
68d98d3e 4039 break;
888a6bc6 4040 case QEMU_OPTION_realtime:
583f34c4
TH
4041 warn_report("'-realtime mlock=...' is deprecated, please use "
4042 "'-overcommit mem-lock=...' instead");
70b94331
MA
4043 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4044 optarg, false);
888a6bc6
SM
4045 if (!opts) {
4046 exit(1);
4047 }
6f131f13
MT
4048 /* Don't override the -overcommit option if set */
4049 enable_mlock = enable_mlock ||
4050 qemu_opt_get_bool(opts, "mlock", true);
4051 break;
4052 case QEMU_OPTION_overcommit:
4053 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
4054 optarg, false);
4055 if (!opts) {
4056 exit(1);
4057 }
4058 /* Don't override the -realtime option if set */
4059 enable_mlock = enable_mlock ||
4060 qemu_opt_get_bool(opts, "mem-lock", false);
4061 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
888a6bc6 4062 break;
5e2ac519 4063 case QEMU_OPTION_msg:
70b94331
MA
4064 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4065 false);
5e2ac519
SA
4066 if (!opts) {
4067 exit(1);
4068 }
4069 configure_msg(opts);
4070 break;
abfd9ce3 4071 case QEMU_OPTION_dump_vmstate:
522abf69 4072 if (vmstate_dump_file) {
f61eddcb
EH
4073 error_report("only one '-dump-vmstate' "
4074 "option may be given");
522abf69
GA
4075 exit(1);
4076 }
abfd9ce3
AS
4077 vmstate_dump_file = fopen(optarg, "w");
4078 if (vmstate_dump_file == NULL) {
f61eddcb 4079 error_report("open %s: %s", optarg, strerror(errno));
abfd9ce3
AS
4080 exit(1);
4081 }
12df189d
EC
4082 break;
4083 case QEMU_OPTION_enable_sync_profile:
4084 qsp_enable();
abfd9ce3 4085 break;
aec0d0e1
MP
4086 case QEMU_OPTION_nouserconfig:
4087 /* Nothing to be parsed here. Especially, do not error out below. */
4088 break;
59a5264b 4089 default:
1217d6ca
TH
4090 if (os_parse_cmd_args(popt->index, optarg)) {
4091 error_report("Option not supported in this build");
4092 exit(1);
4093 }
cd6f1169 4094 }
0824d6fc
FB
4095 }
4096 }
43fa1e0b
EH
4097 /*
4098 * Clear error location left behind by the loop.
4099 * Best done right after the loop. Do not insert code here!
4100 */
4101 loc_set_none();
364c3e6b 4102
58c91595
PB
4103 qemu_validate_options();
4104
3df261b6 4105 /*
0546c060
PB
4106 * These options affect everything else and should be processed
4107 * before daemonizing.
3df261b6 4108 */
0546c060 4109 qemu_process_early_options();
3df261b6 4110
0546c060
PB
4111 qemu_process_help_options();
4112 qemu_maybe_daemonize(pid_file);
4113
efd7ab22
PB
4114 qemu_init_main_loop(&error_fatal);
4115 cpu_timers_init();
3df261b6 4116
fc4a4734 4117 user_register_global_props();
4c27b859
PD
4118 replay_configure(icount_opts);
4119
eb6a5209
AP
4120 configure_rtc(qemu_find_opts_singleton("rtc"));
4121
7580f231 4122 machine_class = select_machine();
1a3ec8c1 4123 object_set_machine_compat_props(machine_class->compat_props);
076b35b5 4124
2a7b18a3
IM
4125 have_custom_ram_size = set_memory_options(&ram_slots, &maxram_size,
4126 machine_class);
4127
3c75e12e 4128 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
52eb3dfd
MA
4129 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4130 exit(0);
4131 }
0056ae24 4132 object_property_add_child(object_get_root(), "machine",
d2623129 4133 OBJECT(current_machine));
e2fb3fbb
MA
4134 object_property_add_child(container_get(OBJECT(current_machine),
4135 "/unattached"),
d2623129 4136 "sysbus", OBJECT(sysbus_get_default()));
20bccb82
PM
4137
4138 if (machine_class->minimum_page_bits) {
4139 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4140 /* This would be a board error: specifying a minimum smaller than
4141 * a target's compile-time fixed setting.
4142 */
4143 g_assert_not_reached();
4144 }
4145 }
4146
b5c2c3d0 4147 cpu_exec_init_all();
efd7ab22 4148 page_size_init();
0056ae24 4149
958db90c 4150 if (machine_class->hw_version) {
35c2c8dc 4151 qemu_set_hw_version(machine_class->hw_version);
93bfef4c
CV
4152 }
4153
3df8c4f3
PB
4154 machine_smp_parse(current_machine,
4155 qemu_opts_find(qemu_find_opts("smp-opts"), NULL), &error_fatal);
b2097003 4156
ffac16fa
IM
4157 if (mem_prealloc) {
4158 char *val;
4159
4160 val = g_strdup_printf("%d", current_machine->smp.cpus);
4161 object_register_sugar_prop("memory-backend", "prealloc-threads", val);
4162 g_free(val);
4ebc74db 4163 object_register_sugar_prop("memory-backend", "prealloc", "on");
ffac16fa
IM
4164 }
4165
67b724e6
AP
4166 /*
4167 * Get the default machine options from the machine if it is not already
4168 * specified either by the configuration file or by the command line.
4169 */
958db90c 4170 if (machine_class->default_machine_opts) {
25de5935 4171 qemu_opts_set_defaults(qemu_find_opts("machine"),
958db90c 4172 machine_class->default_machine_opts, 0);
67b724e6
AP
4173 }
4174
42229a75
LV
4175 /* process plugin before CPUs are created, but once -smp has been parsed */
4176 if (qemu_plugin_load_list(&plugin_list)) {
4177 exit(1);
4178 }
4179
28d0de7a
MA
4180 qemu_opts_foreach(qemu_find_opts("device"),
4181 default_driver_check, NULL, NULL);
4182 qemu_opts_foreach(qemu_find_opts("global"),
4183 default_driver_check, NULL, NULL);
998bbd74 4184
d44229c5
MW
4185 if (!vga_model && !default_vga) {
4186 vga_interface_type = VGA_DEVICE;
4187 }
958db90c 4188 if (!has_defaults || machine_class->no_serial) {
986c5f78
GH
4189 default_serial = 0;
4190 }
958db90c 4191 if (!has_defaults || machine_class->no_parallel) {
986c5f78
GH
4192 default_parallel = 0;
4193 }
958db90c 4194 if (!has_defaults || machine_class->no_floppy) {
ac33f8fa
GH
4195 default_floppy = 0;
4196 }
958db90c 4197 if (!has_defaults || machine_class->no_cdrom) {
ac33f8fa
GH
4198 default_cdrom = 0;
4199 }
958db90c 4200 if (!has_defaults || machine_class->no_sdcard) {
ac33f8fa
GH
4201 default_sdcard = 0;
4202 }
d44229c5
MW
4203 if (!has_defaults) {
4204 default_monitor = 0;
4205 default_net = 0;
4206 default_vga = 0;
4207 }
986c5f78 4208
ab51b1d5
MT
4209 if (is_daemonized()) {
4210 /* According to documentation and historically, -nographic redirects
4211 * serial port, parallel port and monitor to stdio, which does not work
4212 * with -daemonize. We can redirect these to null instead, but since
4213 * -nographic is legacy, let's just error out.
4214 * We disallow -nographic only if all other ports are not redirected
4215 * explicitly, to not break existing legacy setups which uses
4216 * -nographic _and_ redirects all ports explicitly - this is valid
4217 * usage, -nographic is just a no-op in this case.
4218 */
cfc58cf3 4219 if (nographic
9fd7e96a 4220 && (default_parallel || default_serial || default_monitor)) {
4cd70f34 4221 error_report("-nographic cannot be used with -daemonize");
ab51b1d5
MT
4222 exit(1);
4223 }
ab51b1d5
MT
4224 }
4225
cfc58cf3 4226 if (nographic) {
6a5e8b0e
GH
4227 if (default_parallel)
4228 add_device_config(DEV_PARALLEL, "null");
e1c09175
GH
4229 if (default_serial && default_monitor) {
4230 add_device_config(DEV_SERIAL, "mon:stdio");
4231 } else {
4232 if (default_serial)
4233 add_device_config(DEV_SERIAL, "stdio");
4234 if (default_monitor)
4821cd4c 4235 monitor_parse("stdio", "readline", false);
e1c09175 4236 }
998bbd74
GH
4237 } else {
4238 if (default_serial)
4239 add_device_config(DEV_SERIAL, "vc:80Cx24C");
6a5e8b0e
GH
4240 if (default_parallel)
4241 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
abdeed06 4242 if (default_monitor)
4821cd4c 4243 monitor_parse("vc:80Cx24C", "readline", false);
bc0129d9
AL
4244 }
4245
006decd4
GH
4246#if defined(CONFIG_VNC)
4247 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4248 display_remote++;
4249 }
4250#endif
e3af9f9a 4251 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
898f9d41
GH
4252 if (!qemu_display_find_default(&dpy)) {
4253 dpy.type = DISPLAY_TYPE_NONE;
4254#if defined(CONFIG_VNC)
4255 vnc_parse("localhost:0,to=99,id=default", &error_abort);
15546425 4256#endif
898f9d41 4257 }
15546425 4258 }
db71589f
GH
4259 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4260 dpy.type = DISPLAY_TYPE_NONE;
15546425
AL
4261 }
4262
09bd7ba9
TH
4263 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4264 error_report("-alt-grab and -ctrl-grab are only valid "
f61eddcb 4265 "for SDL, ignoring option");
047d4e15 4266 }
78782712 4267 if (dpy.has_window_close &&
e3af9f9a 4268 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
f61eddcb
EH
4269 error_report("-no-quit is only valid for GTK and SDL, "
4270 "ignoring option");
047d4e15
PW
4271 }
4272
5ee1718f 4273 qemu_display_early_init(&dpy);
777357d7
MAL
4274 qemu_console_early_init();
4275
62f27922 4276 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
0b71a5d5 4277#if defined(CONFIG_OPENGL)
8afb9000 4278 error_report("OpenGL is not supported by the display");
0b71a5d5 4279#else
5dfdae81 4280 error_report("OpenGL support is disabled");
0b71a5d5
GH
4281#endif
4282 exit(1);
4283 }
15546425 4284
7e1e0c11
MA
4285 qemu_opts_foreach(qemu_find_opts("object"),
4286 user_creatable_add_opts_foreach,
4287 object_create_initial, &error_fatal);
a4c7367f 4288
93ab5844
GH
4289 /* spice needs the timers to be initialized by this point */
4290 /* spice must initialize before audio as it changes the default auiodev */
4291 /* spice must initialize before chardevs (for spicevmc and spiceport) */
63be30e6 4292 qemu_spice.init();
93ab5844 4293
dd4af977
MA
4294 qemu_opts_foreach(qemu_find_opts("chardev"),
4295 chardev_init_func, NULL, &error_fatal);
2c189a4e 4296
758e8e38 4297#ifdef CONFIG_VIRTFS
b836723d
MA
4298 qemu_opts_foreach(qemu_find_opts("fsdev"),
4299 fsdev_init_func, NULL, &error_fatal);
74db920c 4300#endif
1a688d3b 4301
cda4aa9a 4302 /*
dea1fb88 4303 * Note: we need to create audio and block backends before
cda4aa9a 4304 * machine_set_property(), so machine properties can refer to
2fa23277 4305 * them.
cda4aa9a
MA
4306 */
4307 configure_blockdev(&bdo_queue, machine_class, snapshot);
dea1fb88 4308 audio_init_audiodevs();
cda4aa9a 4309
6b1b1440 4310 machine_opts = qemu_get_machine_opts();
702832b8
MA
4311 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4312 &error_fatal);
622e99c5
IM
4313 current_machine->ram_size = ram_size;
4314 current_machine->maxram_size = maxram_size;
4315 current_machine->ram_slots = ram_slots;
6b1b1440 4316
0427b625
MA
4317 /*
4318 * Note: uses machine properties such as kernel-irqchip, must run
4319 * after machine_set_property().
4320 */
28a09617 4321 configure_accelerators(argv[0]);
214910a7 4322
0427b625
MA
4323 /*
4324 * Beware, QOM objects created before this point miss global and
4325 * compat properties.
4326 *
4327 * Global properties get set up by qdev_prop_register_global(),
4328 * called from user_register_global_props(), and certain option
4329 * desugaring. Also in CPU feature desugaring (buried in
c1c8cfe5 4330 * parse_cpu_option()), which happens below this point, but may
0427b625 4331 * only target the CPU type, which can only be created after
c1c8cfe5 4332 * parse_cpu_option() returned the type.
0427b625
MA
4333 *
4334 * Machine compat properties: object_set_machine_compat_props().
4335 * Accelerator compat props: object_set_accelerator_compat_props(),
4336 * called from configure_accelerator().
4337 */
4338
08fe6824
TH
4339 if (!qtest_enabled() && machine_class->deprecation_reason) {
4340 error_report("Machine type '%s' is deprecated: %s",
4341 machine_class->name, machine_class->deprecation_reason);
4342 }
4343
2fa23277 4344 /*
0427b625
MA
4345 * Note: creates a QOM object, must run only after global and
4346 * compat properties have been set up.
2fa23277
MA
4347 */
4348 migration_object_init();
4349
2c189a4e 4350 if (qtest_chrdev) {
2b8985f1 4351 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
2c189a4e
DB
4352 }
4353
8281abd5
MA
4354 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4355 if (opts) {
f05f47bb
GA
4356 boot_order = qemu_opt_get(opts, "order");
4357 if (boot_order) {
007b0657 4358 validate_bootdevices(boot_order, &error_fatal);
8281abd5
MA
4359 }
4360
f05f47bb
GA
4361 boot_once = qemu_opt_get(opts, "once");
4362 if (boot_once) {
007b0657 4363 validate_bootdevices(boot_once, &error_fatal);
8281abd5
MA
4364 }
4365
4366 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
e5187b56 4367 boot_strict = qemu_opt_get_bool(opts, "strict", false);
8281abd5
MA
4368 }
4369
f05f47bb
GA
4370 if (!boot_order) {
4371 boot_order = machine_class->default_boot_order;
4372 }
4373
58c91595
PB
4374 if (semihosting_enabled() && !semihosting_get_argc()) {
4375 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
4376 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
a59d31a1
LA
4377 /* fall back to the -kernel/-append */
4378 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4379 }
4380
d30300f7
EH
4381 if (default_net) {
4382 QemuOptsList *net = qemu_find_opts("net");
4383 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4384#ifdef CONFIG_SLIRP
4385 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4386#endif
4387 }
4388
34f708b0
TH
4389 if (net_init_clients(&err) < 0) {
4390 error_report_err(err);
dc1c9fe8 4391 exit(1);
702c651c 4392 }
f1510b2c 4393
7e1e0c11
MA
4394 qemu_opts_foreach(qemu_find_opts("object"),
4395 user_creatable_add_opts_foreach,
4396 object_create_delayed, &error_fatal);
9abce56d 4397
d64072c0
MA
4398 if (tpm_init() < 0) {
4399 exit(1);
4400 }
d1a0cf73 4401
2d5a3a8b
MA
4402 qemu_opts_foreach(qemu_find_opts("mon"),
4403 mon_init_func, NULL, &error_fatal);
157b9319 4404
998bbd74
GH
4405 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4406 exit(1);
6a5e8b0e
GH
4407 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4408 exit(1);
c9f398e5
PA
4409 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4410 exit(1);
2796dae0 4411
619985e9
KF
4412 /* now chardevs have been created we may have semihosting to connect */
4413 qemu_semihosting_connect_chardevs();
e19c520f 4414 qemu_semihosting_console_init();
619985e9 4415
7f1b17f2 4416 /* If no default VGA is requested, the default is "none". */
3605ded5 4417 if (default_vga) {
dceb8852 4418 vga_model = get_default_vga_model(machine_class);
a369da5f 4419 }
d44229c5 4420 if (vga_model) {
dceb8852 4421 select_vgahw(machine_class, vga_model);
d44229c5 4422 }
a369da5f 4423
09aaa160
MA
4424 if (watchdog) {
4425 i = select_watchdog(watchdog);
4426 if (i > 0)
4427 exit (i == 1 ? 1 : 0);
4428 }
4429
8bd7f71d
PD
4430 /* This checkpoint is required by replay to separate prior clock
4431 reading from the other reads, because timer polling functions query
4432 clock values from the log. */
4433 replay_checkpoint(CHECKPOINT_INIT);
1de81d28 4434
3ef96221 4435 current_machine->boot_order = boot_order;
3ef96221 4436
6063d4c0 4437 /* parse features once if machine provides default cpu_type */
2278b939 4438 current_machine->cpu_type = machine_class->default_cpu_type;
c1c8cfe5
EH
4439 if (cpu_option) {
4440 current_machine->cpu_type = parse_cpu_option(cpu_option);
6063d4c0 4441 }
a1b18df9 4442
2a7b18a3
IM
4443 if (current_machine->ram_memdev_id) {
4444 Object *backend;
4445 ram_addr_t backend_size;
4446
4447 backend = object_resolve_path_type(current_machine->ram_memdev_id,
4448 TYPE_MEMORY_BACKEND, NULL);
4951247d
MAL
4449 if (!backend) {
4450 error_report("Memory backend '%s' not found",
4451 current_machine->ram_memdev_id);
4452 exit(EXIT_FAILURE);
4453 }
2a7b18a3
IM
4454 backend_size = object_property_get_uint(backend, "size", &error_abort);
4455 if (have_custom_ram_size && backend_size != ram_size) {
4456 error_report("Size specified by -m option must match size of "
4457 "explicitly specified 'memory-backend' property");
4458 exit(EXIT_FAILURE);
4459 }
1148e4f4
IM
4460 if (mem_path) {
4461 error_report("'-mem-path' can't be used together with"
4462 "'-machine memory-backend'");
4463 exit(EXIT_FAILURE);
4464 }
2a7b18a3
IM
4465 ram_size = backend_size;
4466 }
4467
4468 if (!xen_enabled()) {
4469 /* On 32-bit hosts, QEMU is limited by virtual address space */
4470 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4471 error_report("at most 2047 MB RAM can be simulated");
4472 exit(1);
4473 }
4474 }
4475
d342eb76 4476 parse_numa_opts(current_machine);
6063d4c0 4477
59d55a16
IM
4478 /* do monitor/qmp handling at preconfig state if requested */
4479 qemu_main_loop();
4480
d8e4de41
PB
4481 qemu_init_board();
4482 qemu_create_cli_devices();
4483 qemu_machine_creation_done();
4cab946a 4484
bb3d7702 4485 if (loadvm) {
927d6638 4486 Error *local_err = NULL;
5e22479a 4487 if (load_snapshot(loadvm, &local_err) < 0) {
927d6638 4488 error_report_err(local_err);
05f2401e 4489 autostart = 0;
827beacb 4490 exit(1);
05f2401e
JQ
4491 }
4492 }
bb3d7702
PD
4493 if (replay_mode != REPLAY_MODE_NONE) {
4494 replay_vmstate_init();
4495 }
d63d307f 4496
d828c430 4497 qdev_prop_check_globals();
abfd9ce3
AS
4498 if (vmstate_dump_file) {
4499 /* dump and exit */
4500 dump_vmstate_json_to_file(vmstate_dump_file);
7b733862 4501 exit(0);
abfd9ce3 4502 }
2bb8c10c 4503 if (incoming) {
43eaae28
PB
4504 Error *local_err = NULL;
4505 qemu_start_incoming_migration(incoming, &local_err);
4506 if (local_err) {
c29b77f9 4507 error_reportf_err(local_err, "-incoming %s: ", incoming);
43eaae28 4508 exit(1);
8ca5e801 4509 }
6b99dadc 4510 } else if (autostart) {
c0f4ce77 4511 vm_start();
6b99dadc 4512 }
ffd843bc 4513
7a64c17f 4514 accel_setup_post(current_machine);
eb505be1 4515 os_setup_post();
71e3ceb8 4516
7b733862
AB
4517 return;
4518}
4486e89c 4519
7b733862
AB
4520void qemu_cleanup(void)
4521{
1bb982b8
KF
4522 gdbserver_cleanup();
4523
892ae715
DDAG
4524 /*
4525 * cleaning up the migration object cancels any existing migration
4526 * try to do this early so that it also stops using devices.
4527 */
4528 migration_shutdown();
4529
e6f0ac4d
HR
4530 /*
4531 * We must cancel all block jobs while the block layer is drained,
4532 * or cancelling will be affected by throttling and thus may block
4533 * for an extended period of time.
4534 * vm_shutdown() will bdrv_drain_all(), so we may as well include
4535 * it in the drained section.
4536 * We do not need to end this section, because we do not want any
4537 * requests happening from here on anyway.
4538 */
4539 bdrv_drain_all_begin();
4540
4486e89c
SH
4541 /* No more vcpu or device emulation activity beyond this point */
4542 vm_shutdown();
ae25dccb 4543 replay_finish();
4486e89c 4544
b3b5299d 4545 job_cancel_sync_all();
452589b6 4546 bdrv_close_all();
00d09fdb 4547
3d3b8303 4548 res_free();
b46a8906 4549
aa5cb7f5 4550 /* vhost-user must be cleaned up before chardevs. */
c37cacab 4551 tpm_cleanup();
8caf911d 4552 net_cleanup();
a384c205 4553 audio_cleanup();
2ef45716 4554 monitor_cleanup();
aa5cb7f5 4555 qemu_chr_cleanup();
9d5139e5 4556 user_creatable_cleanup();
474628e8 4557 /* TODO: unref root container, check all devices are ok */
0824d6fc 4558}