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