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