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