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