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