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