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