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