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