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