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