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