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