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