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