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