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