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