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