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