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