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