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