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