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