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