]> git.proxmox.com Git - mirror_qemu.git/blame - vl.c
iscsi: reorganize code for parse_initiator_name
[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 29#include <sys/time.h>
c88676f8 30#include <zlib.h>
ee785fed 31#include "bitmap.h"
67b915a5 32
71e72a19 33/* Needed early for CONFIG_BSD etc. */
d40cdb10
BS
34#include "config-host.h"
35
67b915a5 36#ifndef _WIN32
5cea8590 37#include <libgen.h>
67b915a5 38#include <sys/times.h>
f1510b2c 39#include <sys/wait.h>
67b915a5 40#include <termios.h>
67b915a5 41#include <sys/mman.h>
f1510b2c 42#include <sys/ioctl.h>
24646c7e 43#include <sys/resource.h>
f1510b2c 44#include <sys/socket.h>
c94c8d64 45#include <netinet/in.h>
24646c7e 46#include <net/if.h>
24646c7e 47#include <arpa/inet.h>
9d728e8c 48#include <dirent.h>
7c9d8e07 49#include <netdb.h>
cb4b976b 50#include <sys/select.h>
ab6540d5 51
71e72a19 52#ifdef CONFIG_BSD
7d3505c5 53#include <sys/stat.h>
a167ba50 54#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
92c0e657 55#include <sys/sysctl.h>
24646c7e
BS
56#else
57#include <util.h>
128ab2ff 58#endif
bbe813a2 59#else
223f0d72 60#ifdef __linux__
7d3505c5 61#include <malloc.h>
bd494f4c 62
e57a8c0e 63#include <linux/ppdev.h>
5867c88a 64#include <linux/parport.h>
223f0d72
BS
65#endif
66#ifdef __sun__
d5d10bc3
TS
67#include <sys/stat.h>
68#include <sys/ethernet.h>
69#include <sys/sockio.h>
d5d10bc3 70#include <netinet/arp.h>
d5d10bc3
TS
71#include <netinet/in_systm.h>
72#include <netinet/ip.h>
73#include <netinet/ip_icmp.h> // must come after ip.h
74#include <netinet/udp.h>
75#include <netinet/tcp.h>
76#include <net/if.h>
77#include <syslog.h>
78#include <stropts.h>
67b915a5 79#endif
7d3505c5 80#endif
ec530c81 81#endif
67b915a5 82
8a16d273
TS
83#if defined(CONFIG_VDE)
84#include <libvdeplug.h>
85#endif
86
67b915a5 87#ifdef _WIN32
49dc768d 88#include <windows.h>
67b915a5
FB
89#endif
90
73332e5c 91#ifdef CONFIG_SDL
59a36a2f 92#if defined(__APPLE__) || defined(main)
6693665a 93#include <SDL.h>
880fec5d 94int qemu_main(int argc, char **argv, char **envp);
95int main(int argc, char **argv)
96{
59a36a2f 97 return qemu_main(argc, argv, NULL);
880fec5d 98}
99#undef main
100#define main qemu_main
96bcd4f8 101#endif
73332e5c 102#endif /* CONFIG_SDL */
0824d6fc 103
5b0753e0
FB
104#ifdef CONFIG_COCOA
105#undef main
106#define main qemu_main
107#endif /* CONFIG_COCOA */
108
69e5bb68
AL
109#include <glib.h>
110
511d2b14
BS
111#include "hw/hw.h"
112#include "hw/boards.h"
113#include "hw/usb.h"
114#include "hw/pcmcia.h"
115#include "hw/pc.h"
511d2b14
BS
116#include "hw/isa.h"
117#include "hw/baum.h"
118#include "hw/bt.h"
9dd986cc 119#include "hw/watchdog.h"
b6f6e3d3 120#include "hw/smbios.h"
e37630ca 121#include "hw/xen.h"
bd3c948d 122#include "hw/qdev.h"
45a50b16 123#include "hw/loader.h"
5ef4efa4 124#include "bt-host.h"
511d2b14 125#include "net.h"
68ac40d2 126#include "net/slirp.h"
511d2b14
BS
127#include "monitor.h"
128#include "console.h"
129#include "sysemu.h"
130#include "gdbstub.h"
131#include "qemu-timer.h"
132#include "qemu-char.h"
133#include "cache-utils.h"
666daa68 134#include "blockdev.h"
2b584959 135#include "hw/block-common.h"
c163b5ca 136#include "block-migration.h"
a718acec 137#include "dma.h"
511d2b14
BS
138#include "audio/audio.h"
139#include "migration.h"
140#include "kvm.h"
f795e743 141#include "qjson.h"
d3f24367 142#include "qemu-option.h"
7282a033 143#include "qemu-config.h"
59a5264b 144#include "qemu-options.h"
1fa9a5e4 145#include "qmp-commands.h"
44a9b356 146#include "main-loop.h"
758e8e38 147#ifdef CONFIG_VIRTFS
74db920c
GS
148#include "fsdev/qemu-fsdev.h"
149#endif
c7f0f3b1 150#include "qtest.h"
511d2b14 151
0824d6fc 152#include "disas.h"
fc01f7e7 153
511d2b14
BS
154#include "qemu_socket.h"
155
d918f23e 156#include "slirp/libslirp.h"
511d2b14 157
94b0b5ff 158#include "trace.h"
e4858974 159#include "trace/control.h"
72cf2d4f 160#include "qemu-queue.h"
296af7c9 161#include "cpus.h"
ad96090a 162#include "arch_init.h"
0f66998f 163#include "osdep.h"
72cf2d4f 164
29b0040b
GH
165#include "ui/qemu-spice.h"
166
9dc63a1e
BS
167//#define DEBUG_NET
168//#define DEBUG_SLIRP
330d0414 169
1bfe856e 170#define DEFAULT_RAM_SIZE 128
313aa567 171
98b19252
AS
172#define MAX_VIRTIO_CONSOLES 1
173
5cea8590 174static const char *data_dir;
1192dad8 175const char *bios_name = NULL;
cb5a7aa8 176enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
993fbfdb 177DisplayType display_type = DT_DEFAULT;
6b62dc2d 178int display_remote = 0;
3d11d0eb 179const char* keyboard_layout = NULL;
c227f099 180ram_addr_t ram_size;
c902760f
MT
181const char *mem_path = NULL;
182#ifdef MAP_POPULATE
183int mem_prealloc = 0; /* force preallocation of physical target memory */
184#endif
c4b1fcc0 185int nb_nics;
7c9d8e07 186NICInfo nd_table[MAX_NICS];
d399f677 187int autostart;
f6503059
AZ
188static int rtc_utc = 1;
189static int rtc_date_offset = -1; /* -1 means no change */
6875204c 190QEMUClock *rtc_clock;
64465297 191int vga_interface_type = VGA_NONE;
dbed7e40 192static int full_screen = 0;
634a21f6 193#ifdef CONFIG_SDL
dbed7e40 194static int no_frame = 0;
634a21f6 195#endif
667accab 196int no_quit = 0;
8d11df9e 197CharDriverState *serial_hds[MAX_SERIAL_PORTS];
6508fe59 198CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
9ede2fde 199CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
a09db21f 200int win2k_install_hack = 0;
bb36d470 201int usb_enabled = 0;
1b530a6d 202int singlestep = 0;
6a00d601 203int smp_cpus = 1;
6be68d7e 204int max_cpus = 0;
dc6b1c09
AP
205int smp_cores = 1;
206int smp_threads = 1;
821601ea 207#ifdef CONFIG_VNC
73fc9742 208const char *vnc_display;
821601ea 209#endif
6515b203 210int acpi_enabled = 1;
16b29ae1 211int no_hpet = 0;
52ca8d6a 212int fd_bootchk = 1;
d1beab82 213int no_reboot = 0;
b2f76161 214int no_shutdown = 0;
9467cd46 215int cursor_hide = 1;
a171fe39 216int graphic_rotate = 0;
09aaa160 217const char *watchdog;
2e55e842 218QEMUOptionRom option_rom[MAX_OPTION_ROMS];
9ae02555 219int nb_option_roms;
8e71621f 220int semihosting_enabled = 0;
2b8f2d41 221int old_param = 0;
c35734b2 222const char *qemu_name;
3780e197 223int alt_grab = 0;
0ca9f8a4 224int ctrl_grab = 0;
66508601
BS
225unsigned int nb_prom_envs = 0;
226const char *prom_envs[MAX_PROM_ENVS];
95387491 227int boot_menu;
3d3b8303
WX
228uint8_t *boot_splash_filedata;
229int boot_splash_filedata_size;
230uint8_t qemu_extra_params_fw[2];
0824d6fc 231
1ca4d09a
GN
232typedef struct FWBootEntry FWBootEntry;
233
234struct FWBootEntry {
235 QTAILQ_ENTRY(FWBootEntry) link;
236 int32_t bootindex;
237 DeviceState *dev;
238 char *suffix;
239};
240
241QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
242
268a362c
AL
243int nb_numa_nodes;
244uint64_t node_mem[MAX_NODES];
ee785fed 245unsigned long *node_cpumask[MAX_NODES];
268a362c 246
8fcb1b90
BS
247uint8_t qemu_uuid[16];
248
76e30d0f
JK
249static QEMUBootSetHandler *boot_set_handler;
250static void *boot_set_opaque;
251
fd42deeb
GH
252static NotifierList exit_notifiers =
253 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
254
4cab946a
GN
255static NotifierList machine_init_done_notifiers =
256 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
257
303d4e86 258static int tcg_allowed = 1;
d745bef8 259int kvm_allowed = 0;
3285cf4f 260int xen_allowed = 0;
d745bef8
BS
261uint32_t xen_domid;
262enum xen_mode xen_mode = XEN_EMULATE;
d5ab9713 263static int tcg_tb_size;
d745bef8 264
998bbd74 265static int default_serial = 1;
6a5e8b0e 266static int default_parallel = 1;
986c5f78 267static int default_virtcon = 1;
abdeed06 268static int default_monitor = 1;
ac33f8fa
GH
269static int default_floppy = 1;
270static int default_cdrom = 1;
271static int default_sdcard = 1;
7f1b17f2 272static int default_vga = 1;
998bbd74
GH
273
274static struct {
275 const char *driver;
276 int *flag;
277} default_list[] = {
6a5e8b0e
GH
278 { .driver = "isa-serial", .flag = &default_serial },
279 { .driver = "isa-parallel", .flag = &default_parallel },
d8bcbabf 280 { .driver = "isa-fdc", .flag = &default_floppy },
af6bf132
MA
281 { .driver = "ide-cd", .flag = &default_cdrom },
282 { .driver = "ide-hd", .flag = &default_cdrom },
d8bcbabf 283 { .driver = "ide-drive", .flag = &default_cdrom },
af6bf132 284 { .driver = "scsi-cd", .flag = &default_cdrom },
392ecf54
AS
285 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
286 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
287 { .driver = "virtio-serial", .flag = &default_virtcon },
7f1b17f2
PB
288 { .driver = "VGA", .flag = &default_vga },
289 { .driver = "isa-vga", .flag = &default_vga },
290 { .driver = "cirrus-vga", .flag = &default_vga },
291 { .driver = "isa-cirrus-vga", .flag = &default_vga },
292 { .driver = "vmware-svga", .flag = &default_vga },
293 { .driver = "qxl-vga", .flag = &default_vga },
998bbd74
GH
294};
295
3d3b8303
WX
296static void res_free(void)
297{
298 if (boot_splash_filedata != NULL) {
7267c094 299 g_free(boot_splash_filedata);
3d3b8303
WX
300 boot_splash_filedata = NULL;
301 }
302}
303
998bbd74
GH
304static int default_driver_check(QemuOpts *opts, void *opaque)
305{
306 const char *driver = qemu_opt_get(opts, "driver");
307 int i;
308
309 if (!driver)
310 return 0;
311 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
312 if (strcmp(default_list[i].driver, driver) != 0)
313 continue;
314 *(default_list[i].flag) = 0;
315 }
316 return 0;
317}
318
f5bbfba1
LC
319/***********************************************************/
320/* QEMU state */
321
0461d5a6 322static RunState current_run_state = RUN_STATE_PRELAUNCH;
f5bbfba1 323
5db9d4d1
LC
324typedef struct {
325 RunState from;
326 RunState to;
327} RunStateTransition;
328
329static const RunStateTransition runstate_transitions_def[] = {
330 /* from -> to */
0461d5a6 331 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
5db9d4d1 332
0461d5a6
LC
333 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
334 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
5db9d4d1 335
0461d5a6 336 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
8a9236f1 337 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
5db9d4d1 338
0461d5a6 339 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
8a9236f1 340 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
5db9d4d1 341
0461d5a6 342 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
8a9236f1 343 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
5db9d4d1 344
0461d5a6 345 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
8a9236f1 346 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
5db9d4d1 347
0461d5a6 348 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
8a9236f1 349 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
0461d5a6 350 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
5db9d4d1 351
0461d5a6
LC
352 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
353 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
5db9d4d1 354
0461d5a6 355 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
5db9d4d1 356
0461d5a6
LC
357 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
358 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
359 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
360 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
361 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
362 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
363 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
364 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
365 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
5db9d4d1 366
0461d5a6 367 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
5db9d4d1 368
0461d5a6 369 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
8a9236f1 370 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
5db9d4d1 371
ad02b96a
LC
372 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
373 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
374 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
375 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
376
0461d5a6 377 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
8a9236f1 378 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
5db9d4d1 379
0461d5a6 380 { RUN_STATE_MAX, RUN_STATE_MAX },
5db9d4d1
LC
381};
382
0461d5a6
LC
383static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
384
f5bbfba1
LC
385bool runstate_check(RunState state)
386{
387 return current_run_state == state;
388}
389
5db9d4d1
LC
390void runstate_init(void)
391{
392 const RunStateTransition *p;
393
394 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
395
0461d5a6 396 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
5db9d4d1
LC
397 runstate_valid_transitions[p->from][p->to] = true;
398 }
399}
400
401/* This function will abort() on invalid state transitions */
f5bbfba1
LC
402void runstate_set(RunState new_state)
403{
207c5cd2
LC
404 assert(new_state < RUN_STATE_MAX);
405
406 if (!runstate_valid_transitions[current_run_state][new_state]) {
407 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
408 RunState_lookup[current_run_state],
409 RunState_lookup[new_state]);
5db9d4d1
LC
410 abort();
411 }
412
f5bbfba1
LC
413 current_run_state = new_state;
414}
415
1fa9a5e4 416int runstate_is_running(void)
9e37b9dc 417{
1fa9a5e4 418 return runstate_check(RUN_STATE_RUNNING);
9e37b9dc
LC
419}
420
1fa9a5e4 421StatusInfo *qmp_query_status(Error **errp)
1354869c 422{
1fa9a5e4
LC
423 StatusInfo *info = g_malloc0(sizeof(*info));
424
425 info->running = runstate_is_running();
426 info->singlestep = singlestep;
427 info->status = current_run_state;
428
429 return info;
1354869c
LC
430}
431
8f0056b7
PB
432/***********************************************************/
433/* real time host monotonic timer */
09b26c5e 434
f6503059
AZ
435/***********************************************************/
436/* host time/date access */
437void qemu_get_timedate(struct tm *tm, int offset)
438{
439 time_t ti;
440 struct tm *ret;
441
442 time(&ti);
443 ti += offset;
444 if (rtc_date_offset == -1) {
445 if (rtc_utc)
446 ret = gmtime(&ti);
447 else
448 ret = localtime(&ti);
449 } else {
450 ti -= rtc_date_offset;
451 ret = gmtime(&ti);
452 }
453
454 memcpy(tm, ret, sizeof(struct tm));
455}
456
457int qemu_timedate_diff(struct tm *tm)
458{
459 time_t seconds;
460
461 if (rtc_date_offset == -1)
462 if (rtc_utc)
463 seconds = mktimegm(tm);
f54c556c
GN
464 else {
465 struct tm tmp = *tm;
466 tmp.tm_isdst = -1; /* use timezone to figure it out */
467 seconds = mktime(&tmp);
468 }
f6503059
AZ
469 else
470 seconds = mktimegm(tm) + rtc_date_offset;
471
472 return seconds - time(NULL);
473}
474
80cd3478
LC
475void rtc_change_mon_event(struct tm *tm)
476{
477 QObject *data;
478
479 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
480 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
481 qobject_decref(data);
482}
483
1ed2fc1f
JK
484static void configure_rtc_date_offset(const char *startdate, int legacy)
485{
486 time_t rtc_start_date;
487 struct tm tm;
488
489 if (!strcmp(startdate, "now") && legacy) {
490 rtc_date_offset = -1;
491 } else {
492 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
493 &tm.tm_year,
494 &tm.tm_mon,
495 &tm.tm_mday,
496 &tm.tm_hour,
497 &tm.tm_min,
498 &tm.tm_sec) == 6) {
499 /* OK */
500 } else if (sscanf(startdate, "%d-%d-%d",
501 &tm.tm_year,
502 &tm.tm_mon,
503 &tm.tm_mday) == 3) {
504 tm.tm_hour = 0;
505 tm.tm_min = 0;
506 tm.tm_sec = 0;
507 } else {
508 goto date_fail;
509 }
510 tm.tm_year -= 1900;
511 tm.tm_mon--;
512 rtc_start_date = mktimegm(&tm);
513 if (rtc_start_date == -1) {
514 date_fail:
515 fprintf(stderr, "Invalid date format. Valid formats are:\n"
516 "'2006-06-17T16:01:21' or '2006-06-17'\n");
517 exit(1);
518 }
519 rtc_date_offset = time(NULL) - rtc_start_date;
520 }
521}
522
523static void configure_rtc(QemuOpts *opts)
524{
525 const char *value;
526
527 value = qemu_opt_get(opts, "base");
528 if (value) {
529 if (!strcmp(value, "utc")) {
530 rtc_utc = 1;
531 } else if (!strcmp(value, "localtime")) {
532 rtc_utc = 0;
533 } else {
534 configure_rtc_date_offset(value, 0);
535 }
536 }
6875204c
JK
537 value = qemu_opt_get(opts, "clock");
538 if (value) {
539 if (!strcmp(value, "host")) {
540 rtc_clock = host_clock;
78808141
PB
541 } else if (!strcmp(value, "rt")) {
542 rtc_clock = rt_clock;
6875204c
JK
543 } else if (!strcmp(value, "vm")) {
544 rtc_clock = vm_clock;
545 } else {
546 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
547 exit(1);
548 }
549 }
1ed2fc1f
JK
550 value = qemu_opt_get(opts, "driftfix");
551 if (value) {
7e4c0336 552 if (!strcmp(value, "slew")) {
433acf0d
JK
553 static GlobalProperty slew_lost_ticks[] = {
554 {
555 .driver = "mc146818rtc",
556 .property = "lost_tick_policy",
557 .value = "slew",
558 },
559 { /* end of list */ }
560 };
561
562 qdev_prop_register_global_list(slew_lost_ticks);
7e4c0336 563 } else if (!strcmp(value, "none")) {
433acf0d 564 /* discard is default */
1ed2fc1f
JK
565 } else {
566 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
567 exit(1);
568 }
569 }
1ed2fc1f
JK
570}
571
1ae26a18
AZ
572/***********************************************************/
573/* Bluetooth support */
574static int nb_hcis;
575static int cur_hci;
576static struct HCIInfo *hci_table[MAX_NICS];
dc72ac14 577
1ae26a18
AZ
578static struct bt_vlan_s {
579 struct bt_scatternet_s net;
580 int id;
581 struct bt_vlan_s *next;
582} *first_bt_vlan;
583
584/* find or alloc a new bluetooth "VLAN" */
674bb261 585static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1ae26a18
AZ
586{
587 struct bt_vlan_s **pvlan, *vlan;
588 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
589 if (vlan->id == id)
590 return &vlan->net;
591 }
7267c094 592 vlan = g_malloc0(sizeof(struct bt_vlan_s));
1ae26a18
AZ
593 vlan->id = id;
594 pvlan = &first_bt_vlan;
595 while (*pvlan != NULL)
596 pvlan = &(*pvlan)->next;
597 *pvlan = vlan;
598 return &vlan->net;
599}
600
601static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
602{
603}
604
605static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
606{
607 return -ENOTSUP;
608}
609
610static struct HCIInfo null_hci = {
611 .cmd_send = null_hci_send,
612 .sco_send = null_hci_send,
613 .acl_send = null_hci_send,
614 .bdaddr_set = null_hci_addr_set,
615};
616
617struct HCIInfo *qemu_next_hci(void)
618{
619 if (cur_hci == nb_hcis)
620 return &null_hci;
621
622 return hci_table[cur_hci++];
623}
624
dc72ac14
AZ
625static struct HCIInfo *hci_init(const char *str)
626{
627 char *endp;
628 struct bt_scatternet_s *vlan = 0;
629
630 if (!strcmp(str, "null"))
631 /* null */
632 return &null_hci;
633 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
634 /* host[:hciN] */
635 return bt_host_hci(str[4] ? str + 5 : "hci0");
636 else if (!strncmp(str, "hci", 3)) {
637 /* hci[,vlan=n] */
638 if (str[3]) {
639 if (!strncmp(str + 3, ",vlan=", 6)) {
640 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
641 if (*endp)
642 vlan = 0;
643 }
644 } else
645 vlan = qemu_find_bt_vlan(0);
646 if (vlan)
647 return bt_new_hci(vlan);
648 }
649
650 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
651
652 return 0;
653}
654
655static int bt_hci_parse(const char *str)
656{
657 struct HCIInfo *hci;
c227f099 658 bdaddr_t bdaddr;
dc72ac14
AZ
659
660 if (nb_hcis >= MAX_NICS) {
661 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
662 return -1;
663 }
664
665 hci = hci_init(str);
666 if (!hci)
667 return -1;
668
669 bdaddr.b[0] = 0x52;
670 bdaddr.b[1] = 0x54;
671 bdaddr.b[2] = 0x00;
672 bdaddr.b[3] = 0x12;
673 bdaddr.b[4] = 0x34;
674 bdaddr.b[5] = 0x56 + nb_hcis;
675 hci->bdaddr_set(hci, bdaddr.b);
676
677 hci_table[nb_hcis++] = hci;
678
679 return 0;
680}
681
682static void bt_vhci_add(int vlan_id)
683{
684 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
685
686 if (!vlan->slave)
687 fprintf(stderr, "qemu: warning: adding a VHCI to "
688 "an empty scatternet %i\n", vlan_id);
689
690 bt_vhci_init(bt_new_hci(vlan));
691}
692
693static struct bt_device_s *bt_device_add(const char *opt)
694{
695 struct bt_scatternet_s *vlan;
696 int vlan_id = 0;
697 char *endp = strstr(opt, ",vlan=");
698 int len = (endp ? endp - opt : strlen(opt)) + 1;
699 char devname[10];
700
701 pstrcpy(devname, MIN(sizeof(devname), len), opt);
702
703 if (endp) {
704 vlan_id = strtol(endp + 6, &endp, 0);
705 if (*endp) {
706 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
707 return 0;
708 }
709 }
710
711 vlan = qemu_find_bt_vlan(vlan_id);
712
713 if (!vlan->slave)
714 fprintf(stderr, "qemu: warning: adding a slave device to "
715 "an empty scatternet %i\n", vlan_id);
716
717 if (!strcmp(devname, "keyboard"))
718 return bt_keyboard_init(vlan);
719
720 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
721 return 0;
722}
723
724static int bt_parse(const char *opt)
725{
726 const char *endp, *p;
727 int vlan;
728
729 if (strstart(opt, "hci", &endp)) {
730 if (!*endp || *endp == ',') {
731 if (*endp)
732 if (!strstart(endp, ",vlan=", 0))
733 opt = endp + 1;
734
735 return bt_hci_parse(opt);
736 }
737 } else if (strstart(opt, "vhci", &endp)) {
738 if (!*endp || *endp == ',') {
739 if (*endp) {
740 if (strstart(endp, ",vlan=", &p)) {
741 vlan = strtol(p, (char **) &endp, 0);
742 if (*endp) {
743 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
744 return 1;
745 }
746 } else {
747 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
748 return 1;
749 }
750 } else
751 vlan = 0;
752
753 bt_vhci_add(vlan);
754 return 0;
755 }
756 } else if (strstart(opt, "device:", &endp))
757 return !bt_device_add(endp);
758
759 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
760 return 1;
761}
762
1ae26a18
AZ
763/***********************************************************/
764/* QEMU Block devices */
765
2292ddae
MA
766#define HD_OPTS "media=disk"
767#define CDROM_OPTS "media=cdrom"
768#define FD_OPTS ""
769#define PFLASH_OPTS ""
770#define MTD_OPTS ""
771#define SD_OPTS ""
e4bcb14c 772
9dfd7c7a
GH
773static int drive_init_func(QemuOpts *opts, void *opaque)
774{
a803cb8e 775 int *use_scsi = opaque;
9dfd7c7a 776
319ae529 777 return drive_init(opts, *use_scsi) == NULL;
9dfd7c7a
GH
778}
779
780static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
781{
782 if (NULL == qemu_opt_get(opts, "snapshot")) {
783 qemu_opt_set(opts, "snapshot", "on");
784 }
785 return 0;
786}
787
4e5d9b57
MA
788static void default_drive(int enable, int snapshot, int use_scsi,
789 BlockInterfaceType type, int index,
790 const char *optstr)
791{
792 QemuOpts *opts;
793
794 if (type == IF_DEFAULT) {
795 type = use_scsi ? IF_SCSI : IF_IDE;
796 }
797
798 if (!enable || drive_get_by_index(type, index)) {
799 return;
800 }
801
802 opts = drive_add(type, index, NULL, optstr);
803 if (snapshot) {
804 drive_enable_snapshot(opts, NULL);
805 }
319ae529 806 if (!drive_init(opts, use_scsi)) {
4e5d9b57
MA
807 exit(1);
808 }
809}
810
76e30d0f
JK
811void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
812{
813 boot_set_handler = func;
814 boot_set_opaque = opaque;
815}
816
817int qemu_boot_set(const char *boot_devices)
818{
819 if (!boot_set_handler) {
820 return -EINVAL;
821 }
822 return boot_set_handler(boot_set_opaque, boot_devices);
823}
824
4e9e9d6e 825static void validate_bootdevices(char *devices)
ef3adf68
JK
826{
827 /* We just do some generic consistency checks */
828 const char *p;
829 int bitmap = 0;
830
831 for (p = devices; *p != '\0'; p++) {
832 /* Allowed boot devices are:
833 * a-b: floppy disk drives
834 * c-f: IDE disk drives
07f35073 835 * g-m: machine implementation dependent drives
ef3adf68
JK
836 * n-p: network devices
837 * It's up to each machine implementation to check if the given boot
838 * devices match the actual hardware implementation and firmware
839 * features.
840 */
841 if (*p < 'a' || *p > 'p') {
842 fprintf(stderr, "Invalid boot device '%c'\n", *p);
843 exit(1);
844 }
845 if (bitmap & (1 << (*p - 'a'))) {
846 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
847 exit(1);
848 }
849 bitmap |= 1 << (*p - 'a');
850 }
ef3adf68
JK
851}
852
e0f084bf
JK
853static void restore_boot_devices(void *opaque)
854{
855 char *standard_boot_devices = opaque;
37905d6a
AW
856 static int first = 1;
857
858 /* Restore boot order and remove ourselves after the first boot */
859 if (first) {
860 first = 0;
861 return;
862 }
e0f084bf
JK
863
864 qemu_boot_set(standard_boot_devices);
865
866 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
7267c094 867 g_free(standard_boot_devices);
e0f084bf
JK
868}
869
1ca4d09a
GN
870void add_boot_device_path(int32_t bootindex, DeviceState *dev,
871 const char *suffix)
872{
873 FWBootEntry *node, *i;
874
875 if (bootindex < 0) {
876 return;
877 }
878
879 assert(dev != NULL || suffix != NULL);
880
7267c094 881 node = g_malloc0(sizeof(FWBootEntry));
1ca4d09a 882 node->bootindex = bootindex;
7267c094 883 node->suffix = suffix ? g_strdup(suffix) : NULL;
1ca4d09a
GN
884 node->dev = dev;
885
886 QTAILQ_FOREACH(i, &fw_boot_order, link) {
887 if (i->bootindex == bootindex) {
888 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
889 exit(1);
890 } else if (i->bootindex < bootindex) {
891 continue;
892 }
893 QTAILQ_INSERT_BEFORE(i, node, link);
894 return;
895 }
896 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
897}
898
962630f2
GN
899/*
900 * This function returns null terminated string that consist of new line
71785aba 901 * separated device paths.
962630f2
GN
902 *
903 * memory pointed by "size" is assigned total length of the array in bytes
904 *
905 */
906char *get_boot_devices_list(uint32_t *size)
907{
908 FWBootEntry *i;
909 uint32_t total = 0;
910 char *list = NULL;
911
912 QTAILQ_FOREACH(i, &fw_boot_order, link) {
913 char *devpath = NULL, *bootpath;
914 int len;
915
916 if (i->dev) {
917 devpath = qdev_get_fw_dev_path(i->dev);
918 assert(devpath);
919 }
920
921 if (i->suffix && devpath) {
4fd37a98
BS
922 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
923
7267c094 924 bootpath = g_malloc(bootpathlen);
4fd37a98 925 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
7267c094 926 g_free(devpath);
962630f2
GN
927 } else if (devpath) {
928 bootpath = devpath;
929 } else {
1bf6ccd3 930 assert(i->suffix);
7267c094 931 bootpath = g_strdup(i->suffix);
962630f2
GN
932 }
933
934 if (total) {
935 list[total-1] = '\n';
936 }
937 len = strlen(bootpath) + 1;
7267c094 938 list = g_realloc(list, total + len);
962630f2
GN
939 memcpy(&list[total], bootpath, len);
940 total += len;
7267c094 941 g_free(bootpath);
962630f2
GN
942 }
943
944 *size = total;
945
946 return list;
947}
948
268a362c
AL
949static void numa_add(const char *optarg)
950{
951 char option[128];
952 char *endptr;
953 unsigned long long value, endvalue;
954 int nodenr;
955
ee785fed
CV
956 value = endvalue = 0ULL;
957
268a362c
AL
958 optarg = get_opt_name(option, 128, optarg, ',') + 1;
959 if (!strcmp(option, "node")) {
960 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
961 nodenr = nb_numa_nodes;
962 } else {
963 nodenr = strtoull(option, NULL, 10);
964 }
965
966 if (get_param_value(option, 128, "mem", optarg) == 0) {
967 node_mem[nodenr] = 0;
968 } else {
70b4f4bb 969 int64_t sval;
c03417b4
MA
970 sval = strtosz(option, &endptr);
971 if (sval < 0 || *endptr) {
9f9b17a4
JS
972 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
973 exit(1);
268a362c 974 }
9f9b17a4 975 node_mem[nodenr] = sval;
268a362c 976 }
ee785fed 977 if (get_param_value(option, 128, "cpus", optarg) != 0) {
268a362c 978 value = strtoull(option, &endptr, 10);
ee785fed
CV
979 if (*endptr == '-') {
980 endvalue = strtoull(endptr+1, &endptr, 10);
268a362c 981 } else {
ee785fed
CV
982 endvalue = value;
983 }
984
985 if (!(endvalue < MAX_CPUMASK_BITS)) {
986 endvalue = MAX_CPUMASK_BITS - 1;
987 fprintf(stderr,
988 "A max of %d CPUs are supported in a guest\n",
989 MAX_CPUMASK_BITS);
268a362c 990 }
ee785fed
CV
991
992 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
268a362c
AL
993 }
994 nb_numa_nodes++;
995 }
996 return;
997}
998
dc6b1c09
AP
999static void smp_parse(const char *optarg)
1000{
1001 int smp, sockets = 0, threads = 0, cores = 0;
1002 char *endptr;
1003 char option[128];
1004
1005 smp = strtoul(optarg, &endptr, 10);
1006 if (endptr != optarg) {
1007 if (*endptr == ',') {
1008 endptr++;
1009 }
1010 }
1011 if (get_param_value(option, 128, "sockets", endptr) != 0)
1012 sockets = strtoull(option, NULL, 10);
1013 if (get_param_value(option, 128, "cores", endptr) != 0)
1014 cores = strtoull(option, NULL, 10);
1015 if (get_param_value(option, 128, "threads", endptr) != 0)
1016 threads = strtoull(option, NULL, 10);
1017 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1018 max_cpus = strtoull(option, NULL, 10);
1019
1020 /* compute missing values, prefer sockets over cores over threads */
1021 if (smp == 0 || sockets == 0) {
1022 sockets = sockets > 0 ? sockets : 1;
1023 cores = cores > 0 ? cores : 1;
1024 threads = threads > 0 ? threads : 1;
1025 if (smp == 0) {
1026 smp = cores * threads * sockets;
dc6b1c09
AP
1027 }
1028 } else {
1029 if (cores == 0) {
1030 threads = threads > 0 ? threads : 1;
1031 cores = smp / (sockets * threads);
1032 } else {
dca98169 1033 threads = smp / (cores * sockets);
dc6b1c09
AP
1034 }
1035 }
1036 smp_cpus = smp;
1037 smp_cores = cores > 0 ? cores : 1;
1038 smp_threads = threads > 0 ? threads : 1;
1039 if (max_cpus == 0)
1040 max_cpus = smp_cpus;
1041}
1042
a594cfbf
FB
1043/***********************************************************/
1044/* USB devices */
1045
fb08000c 1046static int usb_device_add(const char *devname)
a594cfbf
FB
1047{
1048 const char *p;
a5d2f727 1049 USBDevice *dev = NULL;
a594cfbf 1050
a5d2f727 1051 if (!usb_enabled)
a594cfbf
FB
1052 return -1;
1053
0958b4cc
GH
1054 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1055 dev = usbdevice_create(devname);
1056 if (dev)
1057 goto done;
1058
a5d2f727 1059 /* the other ones */
e447fc63
GH
1060#ifndef CONFIG_LINUX
1061 /* only the linux version is qdev-ified, usb-bsd still needs this */
a594cfbf 1062 if (strstart(devname, "host:", &p)) {
3741715c 1063 dev = usb_host_device_open(usb_bus_find(-1), p);
e447fc63
GH
1064 } else
1065#endif
1066 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
3741715c
JK
1067 dev = usb_bt_init(usb_bus_find(-1),
1068 devname[2] ? hci_init(p)
1069 : bt_new_hci(qemu_find_bt_vlan(0)));
a594cfbf
FB
1070 } else {
1071 return -1;
1072 }
0d92ed30
PB
1073 if (!dev)
1074 return -1;
1075
a5d2f727 1076done:
a594cfbf
FB
1077 return 0;
1078}
1079
1f3870ab
AL
1080static int usb_device_del(const char *devname)
1081{
1082 int bus_num, addr;
1083 const char *p;
1084
5d0c5750
AL
1085 if (strstart(devname, "host:", &p))
1086 return usb_host_device_close(p);
1087
a5d2f727 1088 if (!usb_enabled)
1f3870ab
AL
1089 return -1;
1090
1091 p = strchr(devname, '.');
1092 if (!p)
1093 return -1;
1094 bus_num = strtoul(devname, NULL, 0);
1095 addr = strtoul(p + 1, NULL, 0);
1096
a5d2f727 1097 return usb_device_delete_addr(bus_num, addr);
1f3870ab
AL
1098}
1099
bd3c948d
GH
1100static int usb_parse(const char *cmdline)
1101{
59d1c1c2 1102 int r;
fb08000c 1103 r = usb_device_add(cmdline);
59d1c1c2
ST
1104 if (r < 0) {
1105 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1106 }
1107 return r;
bd3c948d
GH
1108}
1109
d54908a5 1110void do_usb_add(Monitor *mon, const QDict *qdict)
a594cfbf 1111{
59d1c1c2 1112 const char *devname = qdict_get_str(qdict, "devname");
fb08000c 1113 if (usb_device_add(devname) < 0) {
1ecda02b 1114 error_report("could not add USB device '%s'", devname);
59d1c1c2 1115 }
a594cfbf
FB
1116}
1117
d54908a5 1118void do_usb_del(Monitor *mon, const QDict *qdict)
a594cfbf 1119{
59d1c1c2
ST
1120 const char *devname = qdict_get_str(qdict, "devname");
1121 if (usb_device_del(devname) < 0) {
1ecda02b 1122 error_report("could not delete USB device '%s'", devname);
59d1c1c2 1123 }
a594cfbf
FB
1124}
1125
201a51fc
AZ
1126/***********************************************************/
1127/* PCMCIA/Cardbus */
1128
1129static struct pcmcia_socket_entry_s {
bc24a225 1130 PCMCIASocket *socket;
201a51fc
AZ
1131 struct pcmcia_socket_entry_s *next;
1132} *pcmcia_sockets = 0;
1133
bc24a225 1134void pcmcia_socket_register(PCMCIASocket *socket)
201a51fc
AZ
1135{
1136 struct pcmcia_socket_entry_s *entry;
1137
7267c094 1138 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
201a51fc
AZ
1139 entry->socket = socket;
1140 entry->next = pcmcia_sockets;
1141 pcmcia_sockets = entry;
1142}
1143
bc24a225 1144void pcmcia_socket_unregister(PCMCIASocket *socket)
201a51fc
AZ
1145{
1146 struct pcmcia_socket_entry_s *entry, **ptr;
1147
1148 ptr = &pcmcia_sockets;
1149 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1150 if (entry->socket == socket) {
1151 *ptr = entry->next;
7267c094 1152 g_free(entry);
201a51fc
AZ
1153 }
1154}
1155
376253ec 1156void pcmcia_info(Monitor *mon)
201a51fc
AZ
1157{
1158 struct pcmcia_socket_entry_s *iter;
376253ec 1159
201a51fc 1160 if (!pcmcia_sockets)
376253ec 1161 monitor_printf(mon, "No PCMCIA sockets\n");
201a51fc
AZ
1162
1163 for (iter = pcmcia_sockets; iter; iter = iter->next)
376253ec
AL
1164 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1165 iter->socket->attached ? iter->socket->card_string :
1166 "Empty");
201a51fc
AZ
1167}
1168
cc1daa40
FB
1169/***********************************************************/
1170/* machine registration */
1171
bdaf78e0 1172static QEMUMachine *first_machine = NULL;
6f338c34 1173QEMUMachine *current_machine = NULL;
cc1daa40
FB
1174
1175int qemu_register_machine(QEMUMachine *m)
1176{
1177 QEMUMachine **pm;
1178 pm = &first_machine;
1179 while (*pm != NULL)
1180 pm = &(*pm)->next;
1181 m->next = NULL;
1182 *pm = m;
1183 return 0;
1184}
1185
9596ebb7 1186static QEMUMachine *find_machine(const char *name)
cc1daa40
FB
1187{
1188 QEMUMachine *m;
1189
1190 for(m = first_machine; m != NULL; m = m->next) {
1191 if (!strcmp(m->name, name))
1192 return m;
3f6599e6
MM
1193 if (m->alias && !strcmp(m->alias, name))
1194 return m;
cc1daa40
FB
1195 }
1196 return NULL;
1197}
1198
2c8cffa5 1199QEMUMachine *find_default_machine(void)
0c257437
AL
1200{
1201 QEMUMachine *m;
1202
1203 for(m = first_machine; m != NULL; m = m->next) {
1204 if (m->is_default) {
1205 return m;
1206 }
1207 }
1208 return NULL;
1209}
1210
8a7ddc38
FB
1211/***********************************************************/
1212/* main execution loop */
1213
9596ebb7 1214static void gui_update(void *opaque)
8a7ddc38 1215{
7d957bd8 1216 uint64_t interval = GUI_REFRESH_INTERVAL;
740733bb 1217 DisplayState *ds = opaque;
7d957bd8
AL
1218 DisplayChangeListener *dcl = ds->listeners;
1219
1220 dpy_refresh(ds);
1221
1222 while (dcl != NULL) {
1223 if (dcl->gui_timer_interval &&
1224 dcl->gui_timer_interval < interval)
1225 interval = dcl->gui_timer_interval;
1226 dcl = dcl->next;
1227 }
7bd427d8 1228 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
8a7ddc38
FB
1229}
1230
0bd48850
FB
1231struct vm_change_state_entry {
1232 VMChangeStateHandler *cb;
1233 void *opaque;
72cf2d4f 1234 QLIST_ENTRY (vm_change_state_entry) entries;
0bd48850
FB
1235};
1236
72cf2d4f 1237static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
0bd48850
FB
1238
1239VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1240 void *opaque)
1241{
1242 VMChangeStateEntry *e;
1243
7267c094 1244 e = g_malloc0(sizeof (*e));
0bd48850
FB
1245
1246 e->cb = cb;
1247 e->opaque = opaque;
72cf2d4f 1248 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
0bd48850
FB
1249 return e;
1250}
1251
1252void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1253{
72cf2d4f 1254 QLIST_REMOVE (e, entries);
7267c094 1255 g_free (e);
0bd48850
FB
1256}
1257
1dfb4dd9 1258void vm_state_notify(int running, RunState state)
0bd48850
FB
1259{
1260 VMChangeStateEntry *e;
1261
1dfb4dd9 1262 trace_vm_state_notify(running, state);
94b0b5ff 1263
0bd48850 1264 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1dfb4dd9 1265 e->cb(e->opaque, running, state);
0bd48850
FB
1266 }
1267}
1268
8a7ddc38
FB
1269void vm_start(void)
1270{
1354869c 1271 if (!runstate_is_running()) {
8a7ddc38 1272 cpu_enable_ticks();
0461d5a6
LC
1273 runstate_set(RUN_STATE_RUNNING);
1274 vm_state_notify(1, RUN_STATE_RUNNING);
d6dc3d42 1275 resume_all_vcpus();
6ed2c484 1276 monitor_protocol_event(QEVENT_RESUME, NULL);
8a7ddc38
FB
1277 }
1278}
1279
bb0c6722
FB
1280/* reset/shutdown handler */
1281
1282typedef struct QEMUResetEntry {
72cf2d4f 1283 QTAILQ_ENTRY(QEMUResetEntry) entry;
bb0c6722
FB
1284 QEMUResetHandler *func;
1285 void *opaque;
bb0c6722
FB
1286} QEMUResetEntry;
1287
72cf2d4f
BS
1288static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1289 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bb0c6722 1290static int reset_requested;
f64622c4
GN
1291static int shutdown_requested, shutdown_signal = -1;
1292static pid_t shutdown_pid;
3475187d 1293static int powerdown_requested;
8cf71710 1294static int debug_requested;
95b363b5 1295static int suspend_requested;
95b363b5
GH
1296static NotifierList suspend_notifiers =
1297 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1298static NotifierList wakeup_notifiers =
1299 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1300static uint32_t wakeup_reason_mask = ~0;
0461d5a6 1301static RunState vmstop_requested = RUN_STATE_MAX;
bb0c6722 1302
1291eb35
AP
1303int qemu_shutdown_requested_get(void)
1304{
1305 return shutdown_requested;
1306}
1307
1308int qemu_reset_requested_get(void)
1309{
1310 return reset_requested;
1311}
1312
cf7a2fe2
AJ
1313int qemu_shutdown_requested(void)
1314{
1315 int r = shutdown_requested;
1316 shutdown_requested = 0;
1317 return r;
1318}
1319
f64622c4
GN
1320void qemu_kill_report(void)
1321{
c7f0f3b1 1322 if (!qtest_enabled() && shutdown_signal != -1) {
f1d3fb04
PM
1323 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1324 if (shutdown_pid == 0) {
1325 /* This happens for eg ^C at the terminal, so it's worth
1326 * avoiding printing an odd message in that case.
1327 */
1328 fputc('\n', stderr);
1329 } else {
953ffe0f 1330 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
f1d3fb04 1331 }
f64622c4
GN
1332 shutdown_signal = -1;
1333 }
1334}
1335
cf7a2fe2
AJ
1336int qemu_reset_requested(void)
1337{
1338 int r = reset_requested;
1339 reset_requested = 0;
1340 return r;
1341}
1342
95b363b5
GH
1343static int qemu_suspend_requested(void)
1344{
1345 int r = suspend_requested;
1346 suspend_requested = 0;
1347 return r;
1348}
1349
cf7a2fe2
AJ
1350int qemu_powerdown_requested(void)
1351{
1352 int r = powerdown_requested;
1353 powerdown_requested = 0;
1354 return r;
1355}
1356
e568902a
AL
1357static int qemu_debug_requested(void)
1358{
1359 int r = debug_requested;
1360 debug_requested = 0;
1361 return r;
1362}
1363
0461d5a6 1364/* We use RUN_STATE_MAX but any invalid value will do */
0d45b702 1365static bool qemu_vmstop_requested(RunState *r)
6e29f5da 1366{
0461d5a6 1367 if (vmstop_requested < RUN_STATE_MAX) {
0d45b702 1368 *r = vmstop_requested;
0461d5a6 1369 vmstop_requested = RUN_STATE_MAX;
0d45b702
LC
1370 return true;
1371 }
1372
1373 return false;
6e29f5da
AL
1374}
1375
a08d4367 1376void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bb0c6722 1377{
7267c094 1378 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bb0c6722 1379
bb0c6722
FB
1380 re->func = func;
1381 re->opaque = opaque;
72cf2d4f 1382 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bb0c6722
FB
1383}
1384
dda9b29f 1385void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bb0c6722
FB
1386{
1387 QEMUResetEntry *re;
1388
72cf2d4f 1389 QTAILQ_FOREACH(re, &reset_handlers, entry) {
dda9b29f 1390 if (re->func == func && re->opaque == opaque) {
72cf2d4f 1391 QTAILQ_REMOVE(&reset_handlers, re, entry);
7267c094 1392 g_free(re);
dda9b29f
JK
1393 return;
1394 }
1395 }
1396}
1397
e063eb1f 1398void qemu_system_reset(bool report)
dda9b29f
JK
1399{
1400 QEMUResetEntry *re, *nre;
1401
1402 /* reset all devices */
72cf2d4f 1403 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bb0c6722
FB
1404 re->func(re->opaque);
1405 }
e063eb1f
JK
1406 if (report) {
1407 monitor_protocol_event(QEVENT_RESET, NULL);
1408 }
ea375f9a 1409 cpu_synchronize_all_post_reset();
bb0c6722
FB
1410}
1411
1412void qemu_system_reset_request(void)
1413{
d1beab82
FB
1414 if (no_reboot) {
1415 shutdown_requested = 1;
1416 } else {
1417 reset_requested = 1;
1418 }
b4a3d965 1419 cpu_stop_current();
d9f75a4e 1420 qemu_notify_event();
bb0c6722
FB
1421}
1422
95b363b5
GH
1423static void qemu_system_suspend(void)
1424{
1425 pause_all_vcpus();
1426 notifier_list_notify(&suspend_notifiers, NULL);
ad02b96a 1427 runstate_set(RUN_STATE_SUSPENDED);
53370b78 1428 monitor_protocol_event(QEVENT_SUSPEND, NULL);
95b363b5
GH
1429}
1430
1431void qemu_system_suspend_request(void)
1432{
9abc62f6 1433 if (runstate_check(RUN_STATE_SUSPENDED)) {
95b363b5
GH
1434 return;
1435 }
1436 suspend_requested = 1;
1437 cpu_stop_current();
1438 qemu_notify_event();
1439}
1440
1441void qemu_register_suspend_notifier(Notifier *notifier)
1442{
1443 notifier_list_add(&suspend_notifiers, notifier);
1444}
1445
1446void qemu_system_wakeup_request(WakeupReason reason)
1447{
9abc62f6 1448 if (!runstate_check(RUN_STATE_SUSPENDED)) {
95b363b5
GH
1449 return;
1450 }
1451 if (!(wakeup_reason_mask & (1 << reason))) {
1452 return;
1453 }
ad02b96a 1454 runstate_set(RUN_STATE_RUNNING);
53370b78 1455 monitor_protocol_event(QEVENT_WAKEUP, NULL);
95b363b5
GH
1456 notifier_list_notify(&wakeup_notifiers, &reason);
1457 reset_requested = 1;
1458 qemu_notify_event();
95b363b5
GH
1459}
1460
1461void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1462{
1463 if (enabled) {
1464 wakeup_reason_mask |= (1 << reason);
1465 } else {
1466 wakeup_reason_mask &= ~(1 << reason);
1467 }
1468}
1469
1470void qemu_register_wakeup_notifier(Notifier *notifier)
1471{
1472 notifier_list_add(&wakeup_notifiers, notifier);
1473}
1474
f64622c4
GN
1475void qemu_system_killed(int signal, pid_t pid)
1476{
1477 shutdown_signal = signal;
1478 shutdown_pid = pid;
d9389b96 1479 no_shutdown = 0;
f64622c4
GN
1480 qemu_system_shutdown_request();
1481}
1482
bb0c6722
FB
1483void qemu_system_shutdown_request(void)
1484{
1485 shutdown_requested = 1;
d9f75a4e 1486 qemu_notify_event();
bb0c6722
FB
1487}
1488
3475187d
FB
1489void qemu_system_powerdown_request(void)
1490{
1491 powerdown_requested = 1;
d9f75a4e
AL
1492 qemu_notify_event();
1493}
1494
8cf71710
JK
1495void qemu_system_debug_request(void)
1496{
1497 debug_requested = 1;
83f338f7 1498 qemu_notify_event();
8cf71710
JK
1499}
1500
1dfb4dd9 1501void qemu_system_vmstop_request(RunState state)
8cf71710 1502{
1dfb4dd9 1503 vmstop_requested = state;
8cf71710
JK
1504 qemu_notify_event();
1505}
1506
d9c32310
BS
1507qemu_irq qemu_system_powerdown;
1508
99435906
PB
1509static bool main_loop_should_exit(void)
1510{
1511 RunState r;
1512 if (qemu_debug_requested()) {
1513 vm_stop(RUN_STATE_DEBUG);
1514 }
95b363b5
GH
1515 if (qemu_suspend_requested()) {
1516 qemu_system_suspend();
1517 }
99435906
PB
1518 if (qemu_shutdown_requested()) {
1519 qemu_kill_report();
1520 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1521 if (no_shutdown) {
1522 vm_stop(RUN_STATE_SHUTDOWN);
1523 } else {
1524 return true;
1525 }
1526 }
1527 if (qemu_reset_requested()) {
1528 pause_all_vcpus();
1529 cpu_synchronize_all_states();
1530 qemu_system_reset(VMRESET_REPORT);
1531 resume_all_vcpus();
1532 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1533 runstate_check(RUN_STATE_SHUTDOWN)) {
1534 runstate_set(RUN_STATE_PAUSED);
1535 }
1536 }
1537 if (qemu_powerdown_requested()) {
1538 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1539 qemu_irq_raise(qemu_system_powerdown);
1540 }
1541 if (qemu_vmstop_requested(&r)) {
1542 vm_stop(r);
1543 }
1544 return false;
1545}
1546
43b96858
AL
1547static void main_loop(void)
1548{
c9f711a5 1549 bool nonblocking;
99435906 1550 int last_io = 0;
8e1b90ec
JK
1551#ifdef CONFIG_PROFILER
1552 int64_t ti;
1553#endif
99435906 1554 do {
c9f711a5 1555 nonblocking = !kvm_enabled() && last_io > 0;
89bfc105 1556#ifdef CONFIG_PROFILER
46481d39 1557 ti = profile_getclock();
89bfc105 1558#endif
c9f711a5 1559 last_io = main_loop_wait(nonblocking);
89bfc105 1560#ifdef CONFIG_PROFILER
46481d39 1561 dev_time += profile_getclock() - ti;
89bfc105 1562#endif
99435906 1563 } while (!main_loop_should_exit());
b4608c04
FB
1564}
1565
9bd7e6d9
PB
1566static void version(void)
1567{
f75ca1ae 1568 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
9bd7e6d9
PB
1569}
1570
15f82208 1571static void help(int exitcode)
0824d6fc 1572{
a3adb7ad
ME
1573 version();
1574 printf("usage: %s [options] [disk_image]\n\n"
1575 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1576 error_get_progname());
1577
77bd1119
ME
1578#define QEMU_OPTIONS_GENERATE_HELP
1579#include "qemu-options-wrapper.h"
a3adb7ad
ME
1580
1581 printf("\nDuring emulation, the following keys are useful:\n"
3f020d70 1582 "ctrl-alt-f toggle full screen\n"
1583 "ctrl-alt-n switch to virtual console 'n'\n"
1584 "ctrl-alt toggle mouse and keyboard grab\n"
1585 "\n"
a3adb7ad
ME
1586 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1587
15f82208 1588 exit(exitcode);
0824d6fc
FB
1589}
1590
cd6f1169
FB
1591#define HAS_ARG 0x0001
1592
cd6f1169
FB
1593typedef struct QEMUOption {
1594 const char *name;
1595 int flags;
1596 int index;
ad96090a 1597 uint32_t arch_mask;
cd6f1169
FB
1598} QEMUOption;
1599
dbed7e40 1600static const QEMUOption qemu_options[] = {
ad96090a 1601 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
77bd1119
ME
1602#define QEMU_OPTIONS_GENERATE_OPTIONS
1603#include "qemu-options-wrapper.h"
cd6f1169 1604 { NULL },
fc01f7e7 1605};
a369da5f
BS
1606
1607static bool vga_available(void)
1608{
1609 return qdev_exists("VGA") || qdev_exists("isa-vga");
1610}
1611
1612static bool cirrus_vga_available(void)
1613{
1614 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1615}
1616
1617static bool vmware_vga_available(void)
1618{
1619 return qdev_exists("vmware-svga");
1620}
1621
3893c124 1622static void select_vgahw (const char *p)
1623{
1624 const char *opts;
1625
86176759 1626 vga_interface_type = VGA_NONE;
3893c124 1627 if (strstart(p, "std", &opts)) {
a369da5f
BS
1628 if (vga_available()) {
1629 vga_interface_type = VGA_STD;
1630 } else {
1631 fprintf(stderr, "Error: standard VGA not available\n");
1632 exit(0);
1633 }
3893c124 1634 } else if (strstart(p, "cirrus", &opts)) {
a369da5f
BS
1635 if (cirrus_vga_available()) {
1636 vga_interface_type = VGA_CIRRUS;
1637 } else {
1638 fprintf(stderr, "Error: Cirrus VGA not available\n");
1639 exit(0);
1640 }
3893c124 1641 } else if (strstart(p, "vmware", &opts)) {
a369da5f
BS
1642 if (vmware_vga_available()) {
1643 vga_interface_type = VGA_VMWARE;
1644 } else {
1645 fprintf(stderr, "Error: VMWare SVGA not available\n");
1646 exit(0);
1647 }
94909d9f 1648 } else if (strstart(p, "xenfb", &opts)) {
86176759 1649 vga_interface_type = VGA_XENFB;
a19cbfb3
GH
1650 } else if (strstart(p, "qxl", &opts)) {
1651 vga_interface_type = VGA_QXL;
28b85ed8 1652 } else if (!strstart(p, "none", &opts)) {
3893c124 1653 invalid_vga:
1654 fprintf(stderr, "Unknown vga type: %s\n", p);
1655 exit(1);
1656 }
cb5a7aa8 1657 while (*opts) {
1658 const char *nextopt;
1659
1660 if (strstart(opts, ",retrace=", &nextopt)) {
1661 opts = nextopt;
1662 if (strstart(opts, "dumb", &nextopt))
1663 vga_retrace_method = VGA_RETRACE_DUMB;
1664 else if (strstart(opts, "precise", &nextopt))
1665 vga_retrace_method = VGA_RETRACE_PRECISE;
1666 else goto invalid_vga;
1667 } else goto invalid_vga;
1668 opts = nextopt;
1669 }
3893c124 1670}
1671
1472a95b
JS
1672static DisplayType select_display(const char *p)
1673{
1674 const char *opts;
1675 DisplayType display = DT_DEFAULT;
1676
1677 if (strstart(p, "sdl", &opts)) {
1678#ifdef CONFIG_SDL
1679 display = DT_SDL;
1680 while (*opts) {
1681 const char *nextopt;
1682
1683 if (strstart(opts, ",frame=", &nextopt)) {
1684 opts = nextopt;
1685 if (strstart(opts, "on", &nextopt)) {
1686 no_frame = 0;
1687 } else if (strstart(opts, "off", &nextopt)) {
1688 no_frame = 1;
1689 } else {
05175535 1690 goto invalid_sdl_args;
1472a95b
JS
1691 }
1692 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1693 opts = nextopt;
1694 if (strstart(opts, "on", &nextopt)) {
1695 alt_grab = 1;
1696 } else if (strstart(opts, "off", &nextopt)) {
1697 alt_grab = 0;
1698 } else {
05175535 1699 goto invalid_sdl_args;
1472a95b
JS
1700 }
1701 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1702 opts = nextopt;
1703 if (strstart(opts, "on", &nextopt)) {
1704 ctrl_grab = 1;
1705 } else if (strstart(opts, "off", &nextopt)) {
1706 ctrl_grab = 0;
1707 } else {
05175535 1708 goto invalid_sdl_args;
1472a95b
JS
1709 }
1710 } else if (strstart(opts, ",window_close=", &nextopt)) {
1711 opts = nextopt;
1712 if (strstart(opts, "on", &nextopt)) {
1713 no_quit = 0;
1714 } else if (strstart(opts, "off", &nextopt)) {
1715 no_quit = 1;
1716 } else {
05175535 1717 goto invalid_sdl_args;
1472a95b
JS
1718 }
1719 } else {
05175535
PM
1720 invalid_sdl_args:
1721 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1722 exit(1);
1472a95b
JS
1723 }
1724 opts = nextopt;
1725 }
1726#else
1727 fprintf(stderr, "SDL support is disabled\n");
1728 exit(1);
1729#endif
3264ff12 1730 } else if (strstart(p, "vnc", &opts)) {
821601ea 1731#ifdef CONFIG_VNC
3264ff12
JS
1732 display_remote++;
1733
1734 if (*opts) {
1735 const char *nextopt;
1736
1737 if (strstart(opts, "=", &nextopt)) {
1738 vnc_display = nextopt;
1739 }
1740 }
1741 if (!vnc_display) {
1742 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1743 exit(1);
1744 }
821601ea
JS
1745#else
1746 fprintf(stderr, "VNC support is disabled\n");
1747 exit(1);
1748#endif
1472a95b
JS
1749 } else if (strstart(p, "curses", &opts)) {
1750#ifdef CONFIG_CURSES
1751 display = DT_CURSES;
1752#else
1753 fprintf(stderr, "Curses support is disabled\n");
1754 exit(1);
1755#endif
4171d32e
JS
1756 } else if (strstart(p, "none", &opts)) {
1757 display = DT_NONE;
1472a95b 1758 } else {
1472a95b
JS
1759 fprintf(stderr, "Unknown display type: %s\n", p);
1760 exit(1);
1761 }
1762
1763 return display;
1764}
1765
7d4c3d53
MA
1766static int balloon_parse(const char *arg)
1767{
382f0743 1768 QemuOpts *opts;
7d4c3d53 1769
382f0743
GH
1770 if (strcmp(arg, "none") == 0) {
1771 return 0;
1772 }
1773
1774 if (!strncmp(arg, "virtio", 6)) {
1775 if (arg[6] == ',') {
1776 /* have params -> parse them */
3329f07b 1777 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
382f0743
GH
1778 if (!opts)
1779 return -1;
1780 } else {
1781 /* create empty opts */
8be7e7e4 1782 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
7d4c3d53 1783 }
29f82b37 1784 qemu_opt_set(opts, "driver", "virtio-balloon");
382f0743 1785 return 0;
7d4c3d53 1786 }
382f0743
GH
1787
1788 return -1;
7d4c3d53 1789}
7d4c3d53 1790
5cea8590
PB
1791char *qemu_find_file(int type, const char *name)
1792{
1793 int len;
1794 const char *subdir;
1795 char *buf;
1796
31783203
PM
1797 /* Try the name as a straight path first */
1798 if (access(name, R_OK) == 0) {
7267c094 1799 return g_strdup(name);
5cea8590
PB
1800 }
1801 switch (type) {
1802 case QEMU_FILE_TYPE_BIOS:
1803 subdir = "";
1804 break;
1805 case QEMU_FILE_TYPE_KEYMAP:
1806 subdir = "keymaps/";
1807 break;
1808 default:
1809 abort();
1810 }
1811 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
7267c094 1812 buf = g_malloc0(len);
3a41759d 1813 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
5cea8590 1814 if (access(buf, R_OK)) {
7267c094 1815 g_free(buf);
5cea8590
PB
1816 return NULL;
1817 }
1818 return buf;
1819}
1820
ff952ba2
MA
1821static int device_help_func(QemuOpts *opts, void *opaque)
1822{
1823 return qdev_device_help(opts);
1824}
1825
f31d07d1
GH
1826static int device_init_func(QemuOpts *opts, void *opaque)
1827{
1828 DeviceState *dev;
1829
1830 dev = qdev_device_add(opts);
1831 if (!dev)
1832 return -1;
1833 return 0;
1834}
1835
1a688d3b
GH
1836static int chardev_init_func(QemuOpts *opts, void *opaque)
1837{
1838 CharDriverState *chr;
1839
f69554b9 1840 chr = qemu_chr_new_from_opts(opts, NULL);
1a688d3b
GH
1841 if (!chr)
1842 return -1;
1843 return 0;
1844}
1845
758e8e38 1846#ifdef CONFIG_VIRTFS
74db920c
GS
1847static int fsdev_init_func(QemuOpts *opts, void *opaque)
1848{
1849 int ret;
1850 ret = qemu_fsdev_add(opts);
1851
1852 return ret;
1853}
1854#endif
1855
88589343
GH
1856static int mon_init_func(QemuOpts *opts, void *opaque)
1857{
1858 CharDriverState *chr;
1859 const char *chardev;
1860 const char *mode;
1861 int flags;
1862
1863 mode = qemu_opt_get(opts, "mode");
1864 if (mode == NULL) {
1865 mode = "readline";
1866 }
1867 if (strcmp(mode, "readline") == 0) {
1868 flags = MONITOR_USE_READLINE;
1869 } else if (strcmp(mode, "control") == 0) {
1870 flags = MONITOR_USE_CONTROL;
1871 } else {
1872 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1873 exit(1);
1874 }
1875
39eaab9a
DB
1876 if (qemu_opt_get_bool(opts, "pretty", 0))
1877 flags |= MONITOR_USE_PRETTY;
1878
88589343
GH
1879 if (qemu_opt_get_bool(opts, "default", 0))
1880 flags |= MONITOR_IS_DEFAULT;
1881
1882 chardev = qemu_opt_get(opts, "chardev");
1883 chr = qemu_chr_find(chardev);
1884 if (chr == NULL) {
1885 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1886 exit(1);
1887 }
1888
1889 monitor_init(chr, flags);
1890 return 0;
1891}
1892
6ca5582d 1893static void monitor_parse(const char *optarg, const char *mode)
88589343
GH
1894{
1895 static int monitor_device_index = 0;
1896 QemuOpts *opts;
1897 const char *p;
1898 char label[32];
1899 int def = 0;
1900
1901 if (strstart(optarg, "chardev:", &p)) {
1902 snprintf(label, sizeof(label), "%s", p);
1903 } else {
140e065d
JK
1904 snprintf(label, sizeof(label), "compat_monitor%d",
1905 monitor_device_index);
1906 if (monitor_device_index == 0) {
88589343
GH
1907 def = 1;
1908 }
1909 opts = qemu_chr_parse_compat(label, optarg);
1910 if (!opts) {
1911 fprintf(stderr, "parse error: %s\n", optarg);
1912 exit(1);
1913 }
1914 }
1915
8be7e7e4 1916 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
88589343
GH
1917 if (!opts) {
1918 fprintf(stderr, "duplicate chardev: %s\n", label);
1919 exit(1);
1920 }
6ca5582d 1921 qemu_opt_set(opts, "mode", mode);
88589343
GH
1922 qemu_opt_set(opts, "chardev", label);
1923 if (def)
1924 qemu_opt_set(opts, "default", "on");
1925 monitor_device_index++;
1926}
1927
bd3c948d
GH
1928struct device_config {
1929 enum {
aee1b935
GH
1930 DEV_USB, /* -usbdevice */
1931 DEV_BT, /* -bt */
1932 DEV_SERIAL, /* -serial */
1933 DEV_PARALLEL, /* -parallel */
1934 DEV_VIRTCON, /* -virtioconsole */
c9f398e5 1935 DEV_DEBUGCON, /* -debugcon */
ef0c4a0d 1936 DEV_GDB, /* -gdb, -s */
bd3c948d
GH
1937 } type;
1938 const char *cmdline;
d9a5954d 1939 Location loc;
72cf2d4f 1940 QTAILQ_ENTRY(device_config) next;
bd3c948d 1941};
72cf2d4f 1942QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
1943
1944static void add_device_config(int type, const char *cmdline)
1945{
1946 struct device_config *conf;
1947
7267c094 1948 conf = g_malloc0(sizeof(*conf));
bd3c948d
GH
1949 conf->type = type;
1950 conf->cmdline = cmdline;
d9a5954d 1951 loc_save(&conf->loc);
72cf2d4f 1952 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
1953}
1954
1955static int foreach_device_config(int type, int (*func)(const char *cmdline))
1956{
1957 struct device_config *conf;
1958 int rc;
1959
72cf2d4f 1960 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
1961 if (conf->type != type)
1962 continue;
d9a5954d 1963 loc_push_restore(&conf->loc);
bd3c948d 1964 rc = func(conf->cmdline);
d9a5954d 1965 loc_pop(&conf->loc);
bd3c948d
GH
1966 if (0 != rc)
1967 return rc;
1968 }
1969 return 0;
1970}
1971
998bbd74
GH
1972static int serial_parse(const char *devname)
1973{
1974 static int index = 0;
1975 char label[32];
1976
1977 if (strcmp(devname, "none") == 0)
1978 return 0;
1979 if (index == MAX_SERIAL_PORTS) {
1980 fprintf(stderr, "qemu: too many serial ports\n");
1981 exit(1);
1982 }
1983 snprintf(label, sizeof(label), "serial%d", index);
27143a44 1984 serial_hds[index] = qemu_chr_new(label, devname, NULL);
998bbd74 1985 if (!serial_hds[index]) {
52d06136
PM
1986 fprintf(stderr, "qemu: could not connect serial device"
1987 " to character backend '%s'\n", devname);
998bbd74
GH
1988 return -1;
1989 }
1990 index++;
1991 return 0;
1992}
1993
6a5e8b0e
GH
1994static int parallel_parse(const char *devname)
1995{
1996 static int index = 0;
1997 char label[32];
1998
1999 if (strcmp(devname, "none") == 0)
2000 return 0;
2001 if (index == MAX_PARALLEL_PORTS) {
2002 fprintf(stderr, "qemu: too many parallel ports\n");
2003 exit(1);
2004 }
2005 snprintf(label, sizeof(label), "parallel%d", index);
27143a44 2006 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
6a5e8b0e 2007 if (!parallel_hds[index]) {
52d06136
PM
2008 fprintf(stderr, "qemu: could not connect parallel device"
2009 " to character backend '%s'\n", devname);
6a5e8b0e
GH
2010 return -1;
2011 }
2012 index++;
2013 return 0;
2014}
2015
aee1b935
GH
2016static int virtcon_parse(const char *devname)
2017{
3329f07b 2018 QemuOptsList *device = qemu_find_opts("device");
aee1b935
GH
2019 static int index = 0;
2020 char label[32];
392ecf54 2021 QemuOpts *bus_opts, *dev_opts;
aee1b935
GH
2022
2023 if (strcmp(devname, "none") == 0)
2024 return 0;
2025 if (index == MAX_VIRTIO_CONSOLES) {
2026 fprintf(stderr, "qemu: too many virtio consoles\n");
2027 exit(1);
2028 }
392ecf54 2029
8be7e7e4 2030 bus_opts = qemu_opts_create(device, NULL, 0, NULL);
e87f7fc6
AL
2031 if (arch_type == QEMU_ARCH_S390X) {
2032 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2033 } else {
2034 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2035 }
392ecf54 2036
8be7e7e4 2037 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
392ecf54
AS
2038 qemu_opt_set(dev_opts, "driver", "virtconsole");
2039
aee1b935 2040 snprintf(label, sizeof(label), "virtcon%d", index);
27143a44 2041 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
aee1b935 2042 if (!virtcon_hds[index]) {
52d06136
PM
2043 fprintf(stderr, "qemu: could not connect virtio console"
2044 " to character backend '%s'\n", devname);
aee1b935
GH
2045 return -1;
2046 }
392ecf54
AS
2047 qemu_opt_set(dev_opts, "chardev", label);
2048
aee1b935
GH
2049 index++;
2050 return 0;
2051}
2052
c9f398e5
PA
2053static int debugcon_parse(const char *devname)
2054{
2055 QemuOpts *opts;
2056
27143a44 2057 if (!qemu_chr_new("debugcon", devname, NULL)) {
c9f398e5
PA
2058 exit(1);
2059 }
8be7e7e4 2060 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
c9f398e5
PA
2061 if (!opts) {
2062 fprintf(stderr, "qemu: already have a debugcon device\n");
2063 exit(1);
2064 }
2065 qemu_opt_set(opts, "driver", "isa-debugcon");
2066 qemu_opt_set(opts, "chardev", "debugcon");
2067 return 0;
2068}
2069
9052ea6b
JK
2070static QEMUMachine *machine_parse(const char *name)
2071{
2072 QEMUMachine *m, *machine = NULL;
2073
2074 if (name) {
2075 machine = find_machine(name);
2076 }
2077 if (machine) {
2078 return machine;
2079 }
2080 printf("Supported machines are:\n");
2081 for (m = first_machine; m != NULL; m = m->next) {
2082 if (m->alias) {
3b26486a 2083 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
9052ea6b 2084 }
3b26486a 2085 printf("%-20s %s%s\n", m->name, m->desc,
9052ea6b
JK
2086 m->is_default ? " (default)" : "");
2087 }
c8057f95 2088 exit(!name || !is_help_option(name));
9052ea6b
JK
2089}
2090
303d4e86
AP
2091static int tcg_init(void)
2092{
d5ab9713 2093 tcg_exec_init(tcg_tb_size * 1024 * 1024);
303d4e86
AP
2094 return 0;
2095}
2096
2097static struct {
2098 const char *opt_name;
2099 const char *name;
2100 int (*available)(void);
2101 int (*init)(void);
2102 int *allowed;
2103} accel_list[] = {
2104 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
3285cf4f 2105 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
303d4e86 2106 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
c7f0f3b1 2107 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
303d4e86
AP
2108};
2109
2110static int configure_accelerator(void)
2111{
2112 const char *p = NULL;
2113 char buf[10];
2114 int i, ret;
1b785a97 2115 bool accel_initialised = 0;
303d4e86
AP
2116 bool init_failed = 0;
2117
2118 QemuOptsList *list = qemu_find_opts("machine");
2119 if (!QTAILQ_EMPTY(&list->head)) {
2120 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2121 }
2122
2123 if (p == NULL) {
2124 /* Use the default "accelerator", tcg */
2125 p = "tcg";
2126 }
2127
1b785a97 2128 while (!accel_initialised && *p != '\0') {
303d4e86
AP
2129 if (*p == ':') {
2130 p++;
2131 }
2132 p = get_opt_name(buf, sizeof (buf), p, ':');
2133 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2134 if (strcmp(accel_list[i].opt_name, buf) == 0) {
a16c53b1 2135 *(accel_list[i].allowed) = 1;
303d4e86
AP
2136 ret = accel_list[i].init();
2137 if (ret < 0) {
2138 init_failed = 1;
2139 if (!accel_list[i].available()) {
2140 printf("%s not supported for this target\n",
2141 accel_list[i].name);
2142 } else {
2143 fprintf(stderr, "failed to initialize %s: %s\n",
2144 accel_list[i].name,
2145 strerror(-ret));
2146 }
a16c53b1 2147 *(accel_list[i].allowed) = 0;
303d4e86 2148 } else {
1b785a97 2149 accel_initialised = 1;
303d4e86
AP
2150 }
2151 break;
2152 }
2153 }
2154 if (i == ARRAY_SIZE(accel_list)) {
2155 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2156 }
2157 }
2158
1b785a97 2159 if (!accel_initialised) {
303d4e86
AP
2160 fprintf(stderr, "No accelerator found!\n");
2161 exit(1);
2162 }
2163
2164 if (init_failed) {
2165 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2166 }
2167
1b785a97 2168 return !accel_initialised;
303d4e86
AP
2169}
2170
fd42deeb
GH
2171void qemu_add_exit_notifier(Notifier *notify)
2172{
2173 notifier_list_add(&exit_notifiers, notify);
2174}
2175
2176void qemu_remove_exit_notifier(Notifier *notify)
2177{
31552529 2178 notifier_remove(notify);
fd42deeb
GH
2179}
2180
2181static void qemu_run_exit_notifiers(void)
2182{
9e8dd451 2183 notifier_list_notify(&exit_notifiers, NULL);
fd42deeb
GH
2184}
2185
4cab946a
GN
2186void qemu_add_machine_init_done_notifier(Notifier *notify)
2187{
2188 notifier_list_add(&machine_init_done_notifiers, notify);
2189}
2190
2191static void qemu_run_machine_init_done_notifiers(void)
2192{
9e8dd451 2193 notifier_list_notify(&machine_init_done_notifiers, NULL);
4cab946a
GN
2194}
2195
6530a97b
AL
2196static const QEMUOption *lookup_opt(int argc, char **argv,
2197 const char **poptarg, int *poptind)
2198{
2199 const QEMUOption *popt;
2200 int optind = *poptind;
2201 char *r = argv[optind];
2202 const char *optarg;
2203
0f0bc3f1 2204 loc_set_cmdline(argv, optind, 1);
6530a97b
AL
2205 optind++;
2206 /* Treat --foo the same as -foo. */
2207 if (r[1] == '-')
2208 r++;
2209 popt = qemu_options;
2210 for(;;) {
2211 if (!popt->name) {
0f0bc3f1 2212 error_report("invalid option");
6530a97b
AL
2213 exit(1);
2214 }
2215 if (!strcmp(popt->name, r + 1))
2216 break;
2217 popt++;
2218 }
2219 if (popt->flags & HAS_ARG) {
2220 if (optind >= argc) {
0f0bc3f1 2221 error_report("requires an argument");
6530a97b
AL
2222 exit(1);
2223 }
2224 optarg = argv[optind++];
0f0bc3f1 2225 loc_set_cmdline(argv, optind - 2, 2);
6530a97b
AL
2226 } else {
2227 optarg = NULL;
2228 }
2229
2230 *poptarg = optarg;
2231 *poptind = optind;
2232
2233 return popt;
2234}
2235
0750112a
AL
2236static gpointer malloc_and_trace(gsize n_bytes)
2237{
2238 void *ptr = malloc(n_bytes);
a74cd8cc 2239 trace_g_malloc(n_bytes, ptr);
0750112a
AL
2240 return ptr;
2241}
2242
2243static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2244{
2245 void *ptr = realloc(mem, n_bytes);
a74cd8cc 2246 trace_g_realloc(mem, n_bytes, ptr);
0750112a
AL
2247 return ptr;
2248}
2249
2250static void free_and_trace(gpointer mem)
2251{
a74cd8cc 2252 trace_g_free(mem);
0750112a
AL
2253 free(mem);
2254}
2255
d34e8f6e
MR
2256int qemu_init_main_loop(void)
2257{
2258 return main_loop_init();
2259}
2260
902b3d5c 2261int main(int argc, char **argv, char **envp)
0824d6fc 2262{
e4bcb14c 2263 int i;
da1fcfda 2264 int snapshot, linux_boot;
4e3de9e9 2265 const char *icount_option = NULL;
7f7f9873 2266 const char *initrd_filename;
a20dd508 2267 const char *kernel_filename, *kernel_cmdline;
195325a4 2268 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
3023f332 2269 DisplayState *ds;
7d957bd8 2270 DisplayChangeListener *dcl;
46d4767d 2271 int cyls, heads, secs, translation;
967c0da7 2272 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
03b0ba70 2273 QemuOptsList *olist;
cd6f1169 2274 int optind;
6530a97b 2275 const char *optarg;
d63d307f 2276 const char *loadvm = NULL;
cc1daa40 2277 QEMUMachine *machine;
94fc95cd 2278 const char *cpu_model;
7f1b17f2 2279 const char *vga_model = "none";
93815bc2 2280 const char *pid_file = NULL;
5bb7910a 2281 const char *incoming = NULL;
821601ea 2282#ifdef CONFIG_VNC
993fbfdb 2283 int show_vnc_port = 0;
821601ea 2284#endif
3ed2d9ee 2285 bool defconfig = true;
f29a5614 2286 bool userconfig = true;
c235d738
MF
2287 const char *log_mask = NULL;
2288 const char *log_file = NULL;
0750112a
AL
2289 GMemVTable mem_trace = {
2290 .malloc = malloc_and_trace,
2291 .realloc = realloc_and_trace,
2292 .free = free_and_trace,
2293 };
23d15e86 2294 const char *trace_events = NULL;
e4858974 2295 const char *trace_file = NULL;
0b5538c3 2296
fd42deeb 2297 atexit(qemu_run_exit_notifiers);
65abca0a
MA
2298 error_set_progname(argv[0]);
2299
0750112a 2300 g_mem_set_vtable(&mem_trace);
db529aab 2301 if (!g_thread_supported()) {
42ed3727 2302#if !GLIB_CHECK_VERSION(2, 31, 0)
db529aab 2303 g_thread_init(NULL);
42ed3727
AL
2304#else
2305 fprintf(stderr, "glib threading failed to initialize.\n");
2306 exit(1);
2307#endif
db529aab 2308 }
0750112a 2309
1b71f7c1
AF
2310 module_call_init(MODULE_INIT_QOM);
2311
5db9d4d1
LC
2312 runstate_init();
2313
6875204c 2314 init_clocks();
2ff68d07 2315 rtc_clock = host_clock;
6875204c 2316
902b3d5c 2317 qemu_cache_utils_init(envp);
2318
72cf2d4f 2319 QLIST_INIT (&vm_change_state_head);
fe98ac14 2320 os_setup_early_signal_handling();
be995c27 2321
f80f9ec9 2322 module_call_init(MODULE_INIT_MACHINE);
0c257437 2323 machine = find_default_machine();
94fc95cd 2324 cpu_model = NULL;
4fc5d071 2325 ram_size = 0;
33e3963e 2326 snapshot = 0;
c4b1fcc0 2327 cyls = heads = secs = 0;
46d4767d 2328 translation = BIOS_ATA_TRANSLATION_AUTO;
c4b1fcc0 2329
268a362c
AL
2330 for (i = 0; i < MAX_NODES; i++) {
2331 node_mem[i] = 0;
ee785fed 2332 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
268a362c
AL
2333 }
2334
268a362c 2335 nb_numa_nodes = 0;
7c9d8e07 2336 nb_nics = 0;
3b46e624 2337
41bd639b
BS
2338 autostart= 1;
2339
292444cb
AL
2340 /* first pass of option parsing */
2341 optind = 1;
2342 while (optind < argc) {
2343 if (argv[optind][0] != '-') {
2344 /* disk image */
28e68d68 2345 optind++;
292444cb
AL
2346 continue;
2347 } else {
2348 const QEMUOption *popt;
2349
2350 popt = lookup_opt(argc, argv, &optarg, &optind);
2351 switch (popt->index) {
2352 case QEMU_OPTION_nodefconfig:
3ed2d9ee 2353 defconfig = false;
292444cb 2354 break;
f29a5614
EH
2355 case QEMU_OPTION_nouserconfig:
2356 userconfig = false;
2357 break;
292444cb
AL
2358 }
2359 }
2360 }
2361
2362 if (defconfig) {
dcfb0939 2363 int ret;
f29a5614 2364 ret = qemu_read_default_config_files(userconfig);
b5a8fe5e 2365 if (ret < 0) {
dcfb0939 2366 exit(1);
292444cb
AL
2367 }
2368 }
2369
2370 /* second pass of option parsing */
cd6f1169 2371 optind = 1;
0824d6fc 2372 for(;;) {
cd6f1169 2373 if (optind >= argc)
0824d6fc 2374 break;
6530a97b 2375 if (argv[optind][0] != '-') {
2292ddae 2376 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
cd6f1169
FB
2377 } else {
2378 const QEMUOption *popt;
2379
6530a97b 2380 popt = lookup_opt(argc, argv, &optarg, &optind);
ad96090a
BS
2381 if (!(popt->arch_mask & arch_type)) {
2382 printf("Option %s not supported for this target\n", popt->name);
2383 exit(1);
2384 }
cd6f1169 2385 switch(popt->index) {
cc1daa40 2386 case QEMU_OPTION_M:
9052ea6b 2387 machine = machine_parse(optarg);
cc1daa40 2388 break;
94fc95cd
JM
2389 case QEMU_OPTION_cpu:
2390 /* hw initialization will check this */
ecf40bea 2391 cpu_model = optarg;
94fc95cd 2392 break;
cd6f1169 2393 case QEMU_OPTION_hda:
5645b0f4
MA
2394 {
2395 char buf[256];
2396 if (cyls == 0)
2397 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2398 else
2399 snprintf(buf, sizeof(buf),
2400 "%s,cyls=%d,heads=%d,secs=%d%s",
2401 HD_OPTS , cyls, heads, secs,
2402 translation == BIOS_ATA_TRANSLATION_LBA ?
e4bcb14c 2403 ",trans=lba" :
5645b0f4 2404 translation == BIOS_ATA_TRANSLATION_NONE ?
e4bcb14c 2405 ",trans=none" : "");
5645b0f4
MA
2406 drive_add(IF_DEFAULT, 0, optarg, buf);
2407 break;
2408 }
cd6f1169 2409 case QEMU_OPTION_hdb:
cc1daa40
FB
2410 case QEMU_OPTION_hdc:
2411 case QEMU_OPTION_hdd:
2292ddae
MA
2412 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2413 HD_OPTS);
fc01f7e7 2414 break;
e4bcb14c 2415 case QEMU_OPTION_drive:
e2982c3a
MT
2416 if (drive_def(optarg) == NULL) {
2417 exit(1);
2418 }
e4bcb14c 2419 break;
d058fe03
GH
2420 case QEMU_OPTION_set:
2421 if (qemu_set_option(optarg) != 0)
2422 exit(1);
2423 break;
d0fef6fb
GH
2424 case QEMU_OPTION_global:
2425 if (qemu_global_option(optarg) != 0)
2426 exit(1);
2427 break;
3e3d5815 2428 case QEMU_OPTION_mtdblock:
2292ddae 2429 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3e3d5815 2430 break;
a1bb27b1 2431 case QEMU_OPTION_sd:
2292ddae 2432 drive_add(IF_SD, 0, optarg, SD_OPTS);
a1bb27b1 2433 break;
86f55663 2434 case QEMU_OPTION_pflash:
2292ddae 2435 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
86f55663 2436 break;
cd6f1169 2437 case QEMU_OPTION_snapshot:
33e3963e
FB
2438 snapshot = 1;
2439 break;
cd6f1169 2440 case QEMU_OPTION_hdachs:
330d0414 2441 {
330d0414
FB
2442 const char *p;
2443 p = optarg;
2444 cyls = strtol(p, (char **)&p, 0);
46d4767d
FB
2445 if (cyls < 1 || cyls > 16383)
2446 goto chs_fail;
330d0414
FB
2447 if (*p != ',')
2448 goto chs_fail;
2449 p++;
2450 heads = strtol(p, (char **)&p, 0);
46d4767d
FB
2451 if (heads < 1 || heads > 16)
2452 goto chs_fail;
330d0414
FB
2453 if (*p != ',')
2454 goto chs_fail;
2455 p++;
2456 secs = strtol(p, (char **)&p, 0);
46d4767d
FB
2457 if (secs < 1 || secs > 63)
2458 goto chs_fail;
2459 if (*p == ',') {
2460 p++;
2461 if (!strcmp(p, "none"))
2462 translation = BIOS_ATA_TRANSLATION_NONE;
2463 else if (!strcmp(p, "lba"))
2464 translation = BIOS_ATA_TRANSLATION_LBA;
2465 else if (!strcmp(p, "auto"))
2466 translation = BIOS_ATA_TRANSLATION_AUTO;
2467 else
2468 goto chs_fail;
2469 } else if (*p != '\0') {
c4b1fcc0 2470 chs_fail:
46d4767d
FB
2471 fprintf(stderr, "qemu: invalid physical CHS format\n");
2472 exit(1);
c4b1fcc0 2473 }
9dfd7c7a
GH
2474 if (hda_opts != NULL) {
2475 char num[16];
2476 snprintf(num, sizeof(num), "%d", cyls);
2477 qemu_opt_set(hda_opts, "cyls", num);
2478 snprintf(num, sizeof(num), "%d", heads);
2479 qemu_opt_set(hda_opts, "heads", num);
2480 snprintf(num, sizeof(num), "%d", secs);
2481 qemu_opt_set(hda_opts, "secs", num);
2482 if (translation == BIOS_ATA_TRANSLATION_LBA)
2483 qemu_opt_set(hda_opts, "trans", "lba");
2484 if (translation == BIOS_ATA_TRANSLATION_NONE)
2485 qemu_opt_set(hda_opts, "trans", "none");
2486 }
330d0414
FB
2487 }
2488 break;
268a362c
AL
2489 case QEMU_OPTION_numa:
2490 if (nb_numa_nodes >= MAX_NODES) {
2491 fprintf(stderr, "qemu: too many NUMA nodes\n");
2492 exit(1);
2493 }
2494 numa_add(optarg);
2495 break;
1472a95b
JS
2496 case QEMU_OPTION_display:
2497 display_type = select_display(optarg);
2498 break;
cd6f1169 2499 case QEMU_OPTION_nographic:
993fbfdb 2500 display_type = DT_NOGRAPHIC;
a20dd508 2501 break;
4d3b6f6e 2502 case QEMU_OPTION_curses:
47b05369 2503#ifdef CONFIG_CURSES
993fbfdb 2504 display_type = DT_CURSES;
47b05369
JS
2505#else
2506 fprintf(stderr, "Curses support is disabled\n");
2507 exit(1);
4d3b6f6e 2508#endif
47b05369 2509 break;
a171fe39 2510 case QEMU_OPTION_portrait:
9312805d
VK
2511 graphic_rotate = 90;
2512 break;
2513 case QEMU_OPTION_rotate:
2514 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2515 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2516 graphic_rotate != 180 && graphic_rotate != 270) {
2517 fprintf(stderr,
2518 "qemu: only 90, 180, 270 deg rotation is available\n");
2519 exit(1);
2520 }
a171fe39 2521 break;
cd6f1169 2522 case QEMU_OPTION_kernel:
a0abe474
PM
2523 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2524 break;
2525 case QEMU_OPTION_initrd:
2526 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
a20dd508 2527 break;
cd6f1169 2528 case QEMU_OPTION_append:
a0abe474 2529 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
313aa567 2530 break;
412beee6
GL
2531 case QEMU_OPTION_dtb:
2532 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2533 break;
cd6f1169 2534 case QEMU_OPTION_cdrom:
2292ddae 2535 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
36b486bb 2536 break;
cd6f1169 2537 case QEMU_OPTION_boot:
28c5af54 2538 {
ef3adf68 2539 static const char * const params[] = {
3d3b8303
WX
2540 "order", "once", "menu",
2541 "splash", "splash-time", NULL
ef3adf68
JK
2542 };
2543 char buf[sizeof(boot_devices)];
e0f084bf 2544 char *standard_boot_devices;
ef3adf68
JK
2545 int legacy = 0;
2546
2547 if (!strchr(optarg, '=')) {
2548 legacy = 1;
2549 pstrcpy(buf, sizeof(buf), optarg);
2550 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2551 fprintf(stderr,
2552 "qemu: unknown boot parameter '%s' in '%s'\n",
2553 buf, optarg);
2554 exit(1);
2555 }
2556
2557 if (legacy ||
2558 get_param_value(buf, sizeof(buf), "order", optarg)) {
4e9e9d6e 2559 validate_bootdevices(buf);
ef3adf68 2560 pstrcpy(boot_devices, sizeof(boot_devices), buf);
28c5af54 2561 }
e0f084bf
JK
2562 if (!legacy) {
2563 if (get_param_value(buf, sizeof(buf),
2564 "once", optarg)) {
4e9e9d6e 2565 validate_bootdevices(buf);
7267c094 2566 standard_boot_devices = g_strdup(boot_devices);
e0f084bf
JK
2567 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2568 qemu_register_reset(restore_boot_devices,
2569 standard_boot_devices);
2570 }
95387491
JK
2571 if (get_param_value(buf, sizeof(buf),
2572 "menu", optarg)) {
2573 if (!strcmp(buf, "on")) {
2574 boot_menu = 1;
2575 } else if (!strcmp(buf, "off")) {
2576 boot_menu = 0;
2577 } else {
2578 fprintf(stderr,
2579 "qemu: invalid option value '%s'\n",
2580 buf);
2581 exit(1);
2582 }
2583 }
3d3b8303
WX
2584 qemu_opts_parse(qemu_find_opts("boot-opts"),
2585 optarg, 0);
e0f084bf 2586 }
36b486bb
FB
2587 }
2588 break;
cd6f1169 2589 case QEMU_OPTION_fda:
cd6f1169 2590 case QEMU_OPTION_fdb:
2292ddae
MA
2591 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2592 optarg, FD_OPTS);
c45886db 2593 break;
52ca8d6a
FB
2594 case QEMU_OPTION_no_fd_bootchk:
2595 fd_bootchk = 0;
2596 break;
a1ea458f 2597 case QEMU_OPTION_netdev:
3329f07b 2598 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
a1ea458f
MM
2599 exit(1);
2600 }
2601 break;
7c9d8e07 2602 case QEMU_OPTION_net:
3329f07b 2603 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
c4b1fcc0
FB
2604 exit(1);
2605 }
702c651c 2606 break;
f9dadc98
RS
2607#ifdef CONFIG_LIBISCSI
2608 case QEMU_OPTION_iscsi:
2609 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2610 if (!opts) {
2611 exit(1);
2612 }
2613 break;
2614#endif
c7f74643
FB
2615#ifdef CONFIG_SLIRP
2616 case QEMU_OPTION_tftp:
ad196a9d 2617 legacy_tftp_prefix = optarg;
9bf05444 2618 break;
47d5d01a 2619 case QEMU_OPTION_bootp:
ad196a9d 2620 legacy_bootp_filename = optarg;
47d5d01a 2621 break;
9bf05444 2622 case QEMU_OPTION_redir:
0752706d
MA
2623 if (net_slirp_redir(optarg) < 0)
2624 exit(1);
9bf05444 2625 break;
c7f74643 2626#endif
dc72ac14 2627 case QEMU_OPTION_bt:
bd3c948d 2628 add_device_config(DEV_BT, optarg);
dc72ac14 2629 break;
1d14ffa9 2630 case QEMU_OPTION_audio_help:
ad96090a
BS
2631 if (!(audio_available())) {
2632 printf("Option %s not supported for this target\n", popt->name);
2633 exit(1);
2634 }
1d14ffa9
FB
2635 AUD_help ();
2636 exit (0);
2637 break;
2638 case QEMU_OPTION_soundhw:
ad96090a
BS
2639 if (!(audio_available())) {
2640 printf("Option %s not supported for this target\n", popt->name);
2641 exit(1);
2642 }
1d14ffa9
FB
2643 select_soundhw (optarg);
2644 break;
cd6f1169 2645 case QEMU_OPTION_h:
15f82208 2646 help(0);
cd6f1169 2647 break;
9bd7e6d9
PB
2648 case QEMU_OPTION_version:
2649 version();
2650 exit(0);
2651 break;
00f82b8a 2652 case QEMU_OPTION_m: {
70b4f4bb 2653 int64_t value;
961b42b9 2654 char *end;
9f9b17a4 2655
961b42b9
MA
2656 value = strtosz(optarg, &end);
2657 if (value < 0 || *end) {
00f82b8a 2658 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
cd6f1169
FB
2659 exit(1);
2660 }
00f82b8a 2661
c227f099 2662 if (value != (uint64_t)(ram_addr_t)value) {
00f82b8a
AJ
2663 fprintf(stderr, "qemu: ram size too large\n");
2664 exit(1);
2665 }
2666 ram_size = value;
cd6f1169 2667 break;
00f82b8a 2668 }
c902760f
MT
2669 case QEMU_OPTION_mempath:
2670 mem_path = optarg;
2671 break;
2672#ifdef MAP_POPULATE
2673 case QEMU_OPTION_mem_prealloc:
2674 mem_prealloc = 1;
2675 break;
2676#endif
cd6f1169 2677 case QEMU_OPTION_d:
c235d738
MF
2678 log_mask = optarg;
2679 break;
2680 case QEMU_OPTION_D:
2681 log_file = optarg;
cd6f1169 2682 break;
cd6f1169 2683 case QEMU_OPTION_s:
ef0c4a0d 2684 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
cd6f1169 2685 break;
59030a8c 2686 case QEMU_OPTION_gdb:
ef0c4a0d 2687 add_device_config(DEV_GDB, optarg);
cd6f1169 2688 break;
cd6f1169 2689 case QEMU_OPTION_L:
5cea8590 2690 data_dir = optarg;
cd6f1169 2691 break;
1192dad8
JM
2692 case QEMU_OPTION_bios:
2693 bios_name = optarg;
2694 break;
1b530a6d
AJ
2695 case QEMU_OPTION_singlestep:
2696 singlestep = 1;
2697 break;
cd6f1169 2698 case QEMU_OPTION_S:
3c07f8e8 2699 autostart = 0;
cd6f1169 2700 break;
3d11d0eb
FB
2701 case QEMU_OPTION_k:
2702 keyboard_layout = optarg;
2703 break;
ee22c2f7
FB
2704 case QEMU_OPTION_localtime:
2705 rtc_utc = 0;
2706 break;
3893c124 2707 case QEMU_OPTION_vga:
a369da5f 2708 vga_model = optarg;
7f1b17f2 2709 default_vga = 0;
1bfe856e 2710 break;
e9b137c2
FB
2711 case QEMU_OPTION_g:
2712 {
2713 const char *p;
2714 int w, h, depth;
2715 p = optarg;
2716 w = strtol(p, (char **)&p, 10);
2717 if (w <= 0) {
2718 graphic_error:
2719 fprintf(stderr, "qemu: invalid resolution or depth\n");
2720 exit(1);
2721 }
2722 if (*p != 'x')
2723 goto graphic_error;
2724 p++;
2725 h = strtol(p, (char **)&p, 10);
2726 if (h <= 0)
2727 goto graphic_error;
2728 if (*p == 'x') {
2729 p++;
2730 depth = strtol(p, (char **)&p, 10);
5fafdf24 2731 if (depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
2732 depth != 24 && depth != 32)
2733 goto graphic_error;
2734 } else if (*p == '\0') {
2735 depth = graphic_depth;
2736 } else {
2737 goto graphic_error;
2738 }
3b46e624 2739
e9b137c2
FB
2740 graphic_width = w;
2741 graphic_height = h;
2742 graphic_depth = depth;
2743 }
2744 break;
20d8a3ed
TS
2745 case QEMU_OPTION_echr:
2746 {
2747 char *r;
2748 term_escape_char = strtol(optarg, &r, 0);
2749 if (r == optarg)
2750 printf("Bad argument to echr\n");
2751 break;
2752 }
82c643ff 2753 case QEMU_OPTION_monitor:
6ca5582d
GH
2754 monitor_parse(optarg, "readline");
2755 default_monitor = 0;
2756 break;
2757 case QEMU_OPTION_qmp:
2758 monitor_parse(optarg, "control");
2d114dc1 2759 default_monitor = 0;
82c643ff 2760 break;
22a0e04b 2761 case QEMU_OPTION_mon:
3329f07b 2762 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
22a0e04b 2763 if (!opts) {
22a0e04b
GH
2764 exit(1);
2765 }
2d114dc1 2766 default_monitor = 0;
22a0e04b 2767 break;
191bc01b 2768 case QEMU_OPTION_chardev:
3329f07b 2769 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
191bc01b 2770 if (!opts) {
191bc01b
GH
2771 exit(1);
2772 }
191bc01b 2773 break;
74db920c 2774 case QEMU_OPTION_fsdev:
03b0ba70
GH
2775 olist = qemu_find_opts("fsdev");
2776 if (!olist) {
2777 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2778 exit(1);
2779 }
2780 opts = qemu_opts_parse(olist, optarg, 1);
74db920c
GS
2781 if (!opts) {
2782 fprintf(stderr, "parse error: %s\n", optarg);
2783 exit(1);
2784 }
2785 break;
3d54abc7 2786 case QEMU_OPTION_virtfs: {
e14ea479
SH
2787 QemuOpts *fsdev;
2788 QemuOpts *device;
84a87cc4 2789 const char *writeout, *sock_fd, *socket;
3d54abc7 2790
03b0ba70
GH
2791 olist = qemu_find_opts("virtfs");
2792 if (!olist) {
2793 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2794 exit(1);
2795 }
2796 opts = qemu_opts_parse(olist, optarg, 1);
3d54abc7
GS
2797 if (!opts) {
2798 fprintf(stderr, "parse error: %s\n", optarg);
2799 exit(1);
2800 }
2801
fbcbf101 2802 if (qemu_opt_get(opts, "fsdriver") == NULL ||
99519f0a
AK
2803 qemu_opt_get(opts, "mount_tag") == NULL) {
2804 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
9ce56db6
VJ
2805 exit(1);
2806 }
e14ea479 2807 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
8be7e7e4
LC
2808 qemu_opt_get(opts, "mount_tag"),
2809 1, NULL);
e14ea479
SH
2810 if (!fsdev) {
2811 fprintf(stderr, "duplicate fsdev id: %s\n",
2812 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
2813 exit(1);
2814 }
d3ab98e6
AK
2815
2816 writeout = qemu_opt_get(opts, "writeout");
2817 if (writeout) {
2818#ifdef CONFIG_SYNC_FILE_RANGE
2819 qemu_opt_set(fsdev, "writeout", writeout);
2820#else
2821 fprintf(stderr, "writeout=immediate not supported on "
2822 "this platform\n");
2823 exit(1);
2824#endif
2825 }
fbcbf101 2826 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
e14ea479
SH
2827 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2828 qemu_opt_set(fsdev, "security_model",
2829 qemu_opt_get(opts, "security_model"));
84a87cc4
MK
2830 socket = qemu_opt_get(opts, "socket");
2831 if (socket) {
2832 qemu_opt_set(fsdev, "socket", socket);
2833 }
4c793dda
MK
2834 sock_fd = qemu_opt_get(opts, "sock_fd");
2835 if (sock_fd) {
2836 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2837 }
e14ea479 2838
2c74c2cb
MK
2839 qemu_opt_set_bool(fsdev, "readonly",
2840 qemu_opt_get_bool(opts, "readonly", 0));
8be7e7e4
LC
2841 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2842 NULL);
e14ea479
SH
2843 qemu_opt_set(device, "driver", "virtio-9p-pci");
2844 qemu_opt_set(device, "fsdev",
2845 qemu_opt_get(opts, "mount_tag"));
2846 qemu_opt_set(device, "mount_tag",
2847 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
2848 break;
2849 }
9db221ae
AK
2850 case QEMU_OPTION_virtfs_synth: {
2851 QemuOpts *fsdev;
2852 QemuOpts *device;
2853
8be7e7e4
LC
2854 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
2855 1, NULL);
9db221ae
AK
2856 if (!fsdev) {
2857 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2858 exit(1);
2859 }
2860 qemu_opt_set(fsdev, "fsdriver", "synth");
9db221ae 2861
8be7e7e4
LC
2862 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2863 NULL);
9db221ae
AK
2864 qemu_opt_set(device, "driver", "virtio-9p-pci");
2865 qemu_opt_set(device, "fsdev", "v_synth");
2866 qemu_opt_set(device, "mount_tag", "v_synth");
2867 break;
2868 }
82c643ff 2869 case QEMU_OPTION_serial:
998bbd74
GH
2870 add_device_config(DEV_SERIAL, optarg);
2871 default_serial = 0;
18141ed6
JK
2872 if (strncmp(optarg, "mon:", 4) == 0) {
2873 default_monitor = 0;
2874 }
82c643ff 2875 break;
9dd986cc 2876 case QEMU_OPTION_watchdog:
09aaa160
MA
2877 if (watchdog) {
2878 fprintf(stderr,
2879 "qemu: only one watchdog option may be given\n");
2880 return 1;
2881 }
2882 watchdog = optarg;
9dd986cc
RJ
2883 break;
2884 case QEMU_OPTION_watchdog_action:
2885 if (select_watchdog_action(optarg) == -1) {
2886 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2887 exit(1);
2888 }
2889 break;
51ecf136 2890 case QEMU_OPTION_virtiocon:
aee1b935
GH
2891 add_device_config(DEV_VIRTCON, optarg);
2892 default_virtcon = 0;
18141ed6
JK
2893 if (strncmp(optarg, "mon:", 4) == 0) {
2894 default_monitor = 0;
2895 }
51ecf136 2896 break;
6508fe59 2897 case QEMU_OPTION_parallel:
6a5e8b0e
GH
2898 add_device_config(DEV_PARALLEL, optarg);
2899 default_parallel = 0;
18141ed6
JK
2900 if (strncmp(optarg, "mon:", 4) == 0) {
2901 default_monitor = 0;
2902 }
6508fe59 2903 break;
c9f398e5
PA
2904 case QEMU_OPTION_debugcon:
2905 add_device_config(DEV_DEBUGCON, optarg);
2906 break;
d63d307f
FB
2907 case QEMU_OPTION_loadvm:
2908 loadvm = optarg;
2909 break;
2910 case QEMU_OPTION_full_screen:
2911 full_screen = 1;
2912 break;
667accab 2913#ifdef CONFIG_SDL
43523e93
TS
2914 case QEMU_OPTION_no_frame:
2915 no_frame = 1;
2916 break;
3780e197
TS
2917 case QEMU_OPTION_alt_grab:
2918 alt_grab = 1;
2919 break;
0ca9f8a4
DK
2920 case QEMU_OPTION_ctrl_grab:
2921 ctrl_grab = 1;
2922 break;
667accab
TS
2923 case QEMU_OPTION_no_quit:
2924 no_quit = 1;
2925 break;
7d957bd8 2926 case QEMU_OPTION_sdl:
993fbfdb 2927 display_type = DT_SDL;
7d957bd8 2928 break;
58fc096c
JS
2929#else
2930 case QEMU_OPTION_no_frame:
2931 case QEMU_OPTION_alt_grab:
2932 case QEMU_OPTION_ctrl_grab:
2933 case QEMU_OPTION_no_quit:
2934 case QEMU_OPTION_sdl:
2935 fprintf(stderr, "SDL support is disabled\n");
2936 exit(1);
667accab 2937#endif
f7cce898 2938 case QEMU_OPTION_pidfile:
93815bc2 2939 pid_file = optarg;
f7cce898 2940 break;
a09db21f
FB
2941 case QEMU_OPTION_win2k_hack:
2942 win2k_install_hack = 1;
2943 break;
433acf0d
JK
2944 case QEMU_OPTION_rtc_td_hack: {
2945 static GlobalProperty slew_lost_ticks[] = {
2946 {
2947 .driver = "mc146818rtc",
2948 .property = "lost_tick_policy",
2949 .value = "slew",
2950 },
2951 { /* end of list */ }
2952 };
2953
2954 qdev_prop_register_global_list(slew_lost_ticks);
73822ec8 2955 break;
433acf0d 2956 }
8a92ea2f 2957 case QEMU_OPTION_acpitable:
de06f8d1 2958 do_acpitable_option(optarg);
8a92ea2f 2959 break;
b6f6e3d3 2960 case QEMU_OPTION_smbios:
de06f8d1 2961 do_smbios_option(optarg);
b6f6e3d3 2962 break;
7ba1e619 2963 case QEMU_OPTION_enable_kvm:
303d4e86 2964 olist = qemu_find_opts("machine");
303d4e86
AP
2965 qemu_opts_parse(olist, "accel=kvm", 0);
2966 break;
2967 case QEMU_OPTION_machine:
2968 olist = qemu_find_opts("machine");
9052ea6b 2969 opts = qemu_opts_parse(olist, optarg, 1);
303d4e86
AP
2970 if (!opts) {
2971 fprintf(stderr, "parse error: %s\n", optarg);
2972 exit(1);
2973 }
2645c6dc
JK
2974 optarg = qemu_opt_get(opts, "type");
2975 if (optarg) {
2976 machine = machine_parse(optarg);
2977 }
7ba1e619 2978 break;
bb36d470
FB
2979 case QEMU_OPTION_usb:
2980 usb_enabled = 1;
2981 break;
a594cfbf
FB
2982 case QEMU_OPTION_usbdevice:
2983 usb_enabled = 1;
bd3c948d
GH
2984 add_device_config(DEV_USB, optarg);
2985 break;
2986 case QEMU_OPTION_device:
3329f07b 2987 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
f31d07d1
GH
2988 exit(1);
2989 }
a594cfbf 2990 break;
6a00d601 2991 case QEMU_OPTION_smp:
dc6b1c09 2992 smp_parse(optarg);
b2097003 2993 if (smp_cpus < 1) {
6a00d601
FB
2994 fprintf(stderr, "Invalid number of CPUs\n");
2995 exit(1);
2996 }
6be68d7e
JS
2997 if (max_cpus < smp_cpus) {
2998 fprintf(stderr, "maxcpus must be equal to or greater than "
2999 "smp\n");
3000 exit(1);
3001 }
3002 if (max_cpus > 255) {
3003 fprintf(stderr, "Unsupported number of maxcpus\n");
3004 exit(1);
3005 }
6a00d601 3006 break;
24236869 3007 case QEMU_OPTION_vnc:
821601ea 3008#ifdef CONFIG_VNC
6b62dc2d 3009 display_remote++;
821601ea
JS
3010 vnc_display = optarg;
3011#else
3012 fprintf(stderr, "VNC support is disabled\n");
3013 exit(1);
3014#endif
3015 break;
6515b203
FB
3016 case QEMU_OPTION_no_acpi:
3017 acpi_enabled = 0;
3018 break;
16b29ae1
AL
3019 case QEMU_OPTION_no_hpet:
3020 no_hpet = 1;
3021 break;
7d4c3d53
MA
3022 case QEMU_OPTION_balloon:
3023 if (balloon_parse(optarg) < 0) {
3024 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3025 exit(1);
3026 }
df97b920 3027 break;
d1beab82
FB
3028 case QEMU_OPTION_no_reboot:
3029 no_reboot = 1;
3030 break;
b2f76161
AJ
3031 case QEMU_OPTION_no_shutdown:
3032 no_shutdown = 1;
3033 break;
9467cd46
AZ
3034 case QEMU_OPTION_show_cursor:
3035 cursor_hide = 0;
3036 break;
8fcb1b90
BS
3037 case QEMU_OPTION_uuid:
3038 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3039 fprintf(stderr, "Fail to parse UUID string."
3040 " Wrong format.\n");
3041 exit(1);
3042 }
3043 break;
9ae02555
TS
3044 case QEMU_OPTION_option_rom:
3045 if (nb_option_roms >= MAX_OPTION_ROMS) {
3046 fprintf(stderr, "Too many option ROMs\n");
3047 exit(1);
3048 }
2e55e842
GN
3049 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3050 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3051 option_rom[nb_option_roms].bootindex =
3052 qemu_opt_get_number(opts, "bootindex", -1);
3053 if (!option_rom[nb_option_roms].name) {
3054 fprintf(stderr, "Option ROM file is not specified\n");
3055 exit(1);
3056 }
9ae02555
TS
3057 nb_option_roms++;
3058 break;
8e71621f
PB
3059 case QEMU_OPTION_semihosting:
3060 semihosting_enabled = 1;
3061 break;
c35734b2 3062 case QEMU_OPTION_name:
7267c094 3063 qemu_name = g_strdup(optarg);
1889465a
AK
3064 {
3065 char *p = strchr(qemu_name, ',');
3066 if (p != NULL) {
3067 *p++ = 0;
3068 if (strncmp(p, "process=", 8)) {
5697f6ae 3069 fprintf(stderr, "Unknown subargument %s to -name\n", p);
1889465a
AK
3070 exit(1);
3071 }
3072 p += 8;
ce798cf2 3073 os_set_proc_name(p);
1889465a
AK
3074 }
3075 }
c35734b2 3076 break;
66508601
BS
3077 case QEMU_OPTION_prom_env:
3078 if (nb_prom_envs >= MAX_PROM_ENVS) {
3079 fprintf(stderr, "Too many prom variables\n");
3080 exit(1);
3081 }
3082 prom_envs[nb_prom_envs] = optarg;
3083 nb_prom_envs++;
3084 break;
2b8f2d41
AZ
3085 case QEMU_OPTION_old_param:
3086 old_param = 1;
05ebd537 3087 break;
f3dcfada
TS
3088 case QEMU_OPTION_clock:
3089 configure_alarms(optarg);
3090 break;
7e0af5d0 3091 case QEMU_OPTION_startdate:
1ed2fc1f
JK
3092 configure_rtc_date_offset(optarg, 1);
3093 break;
3094 case QEMU_OPTION_rtc:
3329f07b 3095 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
1ed2fc1f 3096 if (!opts) {
1ed2fc1f 3097 exit(1);
7e0af5d0 3098 }
1ed2fc1f 3099 configure_rtc(opts);
7e0af5d0 3100 break;
26a5f13b 3101 case QEMU_OPTION_tb_size:
d5ab9713
JK
3102 tcg_tb_size = strtol(optarg, NULL, 0);
3103 if (tcg_tb_size < 0) {
3104 tcg_tb_size = 0;
3105 }
26a5f13b 3106 break;
2e70f6ef 3107 case QEMU_OPTION_icount:
4e3de9e9 3108 icount_option = optarg;
2e70f6ef 3109 break;
5bb7910a
AL
3110 case QEMU_OPTION_incoming:
3111 incoming = optarg;
81323a6c 3112 runstate_set(RUN_STATE_INMIGRATE);
5bb7910a 3113 break;
d8c208dd
GH
3114 case QEMU_OPTION_nodefaults:
3115 default_serial = 0;
3116 default_parallel = 0;
aee1b935 3117 default_virtcon = 0;
d8c208dd 3118 default_monitor = 0;
cb4522cc 3119 default_net = 0;
ac33f8fa
GH
3120 default_floppy = 0;
3121 default_cdrom = 0;
3122 default_sdcard = 0;
7f1b17f2 3123 default_vga = 0;
d8c208dd 3124 break;
e37630ca 3125 case QEMU_OPTION_xen_domid:
ad96090a
BS
3126 if (!(xen_available())) {
3127 printf("Option %s not supported for this target\n", popt->name);
3128 exit(1);
3129 }
e37630ca
AL
3130 xen_domid = atoi(optarg);
3131 break;
3132 case QEMU_OPTION_xen_create:
ad96090a
BS
3133 if (!(xen_available())) {
3134 printf("Option %s not supported for this target\n", popt->name);
3135 exit(1);
3136 }
e37630ca
AL
3137 xen_mode = XEN_CREATE;
3138 break;
3139 case QEMU_OPTION_xen_attach:
ad96090a
BS
3140 if (!(xen_available())) {
3141 printf("Option %s not supported for this target\n", popt->name);
3142 exit(1);
3143 }
e37630ca
AL
3144 xen_mode = XEN_ATTACH;
3145 break;
ab6540d5 3146 case QEMU_OPTION_trace:
e4858974 3147 {
ab6540d5 3148 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
e4858974
LV
3149 if (!opts) {
3150 exit(1);
ab6540d5 3151 }
23d15e86 3152 trace_events = qemu_opt_get(opts, "events");
e4858974 3153 trace_file = qemu_opt_get(opts, "file");
ab6540d5 3154 break;
e4858974 3155 }
715a664a
GH
3156 case QEMU_OPTION_readconfig:
3157 {
dcfb0939
KW
3158 int ret = qemu_read_config_file(optarg);
3159 if (ret < 0) {
3160 fprintf(stderr, "read config %s: %s\n", optarg,
3161 strerror(-ret));
715a664a
GH
3162 exit(1);
3163 }
715a664a
GH
3164 break;
3165 }
29b0040b
GH
3166 case QEMU_OPTION_spice:
3167 olist = qemu_find_opts("spice");
3168 if (!olist) {
3169 fprintf(stderr, "spice is not supported by this qemu build.\n");
3170 exit(1);
3171 }
3172 opts = qemu_opts_parse(olist, optarg, 0);
3173 if (!opts) {
3174 fprintf(stderr, "parse error: %s\n", optarg);
3175 exit(1);
3176 }
3177 break;
715a664a
GH
3178 case QEMU_OPTION_writeconfig:
3179 {
3180 FILE *fp;
3181 if (strcmp(optarg, "-") == 0) {
3182 fp = stdout;
3183 } else {
3184 fp = fopen(optarg, "w");
3185 if (fp == NULL) {
3186 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3187 exit(1);
3188 }
3189 }
3190 qemu_config_write(fp);
3191 fclose(fp);
3192 break;
3193 }
c7f0f3b1
AL
3194 case QEMU_OPTION_qtest:
3195 qtest_chrdev = optarg;
3196 break;
3197 case QEMU_OPTION_qtest_log:
3198 qtest_log = optarg;
3199 break;
59a5264b
JS
3200 default:
3201 os_parse_cmd_args(popt->index, optarg);
cd6f1169 3202 }
0824d6fc
FB
3203 }
3204 }
0f0bc3f1 3205 loc_set_none();
c235d738 3206
93bfef4c
CV
3207 if (machine->hw_version) {
3208 qemu_set_version(machine->hw_version);
3209 }
3210
ecf40bea
EH
3211 /* Init CPU def lists, based on config
3212 * - Must be called after all the qemu_read_config_file() calls
3213 * - Must be called before list_cpus()
3214 * - Must be called before machine->init()
3215 */
3216 cpudef_init();
3217
c8057f95 3218 if (cpu_model && is_help_option(cpu_model)) {
1d6528af 3219 list_cpus(stdout, &fprintf, cpu_model);
ecf40bea
EH
3220 exit(0);
3221 }
3222
c235d738
MF
3223 /* Open the logfile at this point, if necessary. We can't open the logfile
3224 * when encountering either of the logging options (-d or -D) because the
3225 * other one may be encountered later on the command line, changing the
3226 * location or level of logging.
3227 */
3228 if (log_mask) {
3229 if (log_file) {
3230 set_cpu_log_filename(log_file);
3231 }
3232 set_cpu_log(log_mask);
3233 }
330d0414 3234
23d15e86 3235 if (!trace_backend_init(trace_events, trace_file)) {
e4858974 3236 exit(1);
31d3c9b8 3237 }
0b5538c3 3238
5cea8590
PB
3239 /* If no data_dir is specified then try to find it relative to the
3240 executable path. */
3241 if (!data_dir) {
6170540b 3242 data_dir = os_find_datadir(argv[0]);
5cea8590 3243 }
60474fb5 3244 /* If all else fails use the install path specified when building. */
5cea8590 3245 if (!data_dir) {
1dabe05c 3246 data_dir = CONFIG_QEMU_DATADIR;
5cea8590
PB
3247 }
3248
d423675c
AF
3249 if (machine == NULL) {
3250 fprintf(stderr, "No machine found.\n");
3251 exit(1);
3252 }
3253
6be68d7e
JS
3254 /*
3255 * Default to max_cpus = smp_cpus, in case the user doesn't
3256 * specify a max_cpus value.
3257 */
3258 if (!max_cpus)
3259 max_cpus = smp_cpus;
3260
3d878caa 3261 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
b2097003
AL
3262 if (smp_cpus > machine->max_cpus) {
3263 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3264 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3265 machine->max_cpus);
3266 exit(1);
3267 }
3268
67b724e6
AP
3269 /*
3270 * Get the default machine options from the machine if it is not already
3271 * specified either by the configuration file or by the command line.
3272 */
3273 if (machine->default_machine_opts) {
25de5935
JK
3274 qemu_opts_set_defaults(qemu_find_opts("machine"),
3275 machine->default_machine_opts, 0);
67b724e6
AP
3276 }
3277
3329f07b
GH
3278 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3279 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
998bbd74 3280
986c5f78
GH
3281 if (machine->no_serial) {
3282 default_serial = 0;
3283 }
3284 if (machine->no_parallel) {
3285 default_parallel = 0;
3286 }
3287 if (!machine->use_virtcon) {
3288 default_virtcon = 0;
3289 }
ac33f8fa
GH
3290 if (machine->no_floppy) {
3291 default_floppy = 0;
3292 }
3293 if (machine->no_cdrom) {
3294 default_cdrom = 0;
3295 }
3296 if (machine->no_sdcard) {
3297 default_sdcard = 0;
3298 }
986c5f78 3299
993fbfdb 3300 if (display_type == DT_NOGRAPHIC) {
6a5e8b0e
GH
3301 if (default_parallel)
3302 add_device_config(DEV_PARALLEL, "null");
e1c09175
GH
3303 if (default_serial && default_monitor) {
3304 add_device_config(DEV_SERIAL, "mon:stdio");
986c5f78
GH
3305 } else if (default_virtcon && default_monitor) {
3306 add_device_config(DEV_VIRTCON, "mon:stdio");
e1c09175
GH
3307 } else {
3308 if (default_serial)
3309 add_device_config(DEV_SERIAL, "stdio");
986c5f78
GH
3310 if (default_virtcon)
3311 add_device_config(DEV_VIRTCON, "stdio");
e1c09175 3312 if (default_monitor)
6ca5582d 3313 monitor_parse("stdio", "readline");
e1c09175 3314 }
998bbd74
GH
3315 } else {
3316 if (default_serial)
3317 add_device_config(DEV_SERIAL, "vc:80Cx24C");
6a5e8b0e
GH
3318 if (default_parallel)
3319 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
abdeed06 3320 if (default_monitor)
6ca5582d 3321 monitor_parse("vc:80Cx24C", "readline");
38536da1
AG
3322 if (default_virtcon)
3323 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
bc0129d9
AL
3324 }
3325
a5829fd9
T
3326 socket_init();
3327
3329f07b 3328 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
1a688d3b 3329 exit(1);
758e8e38 3330#ifdef CONFIG_VIRTFS
3329f07b 3331 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
74db920c
GS
3332 exit(1);
3333 }
3334#endif
1a688d3b 3335
eb505be1 3336 os_daemonize();
71e3ceb8 3337
aa26bb2d 3338 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
eb505be1 3339 os_pidfile_error();
93815bc2
TS
3340 exit(1);
3341 }
3342
0ac543de
JK
3343 /* init the memory */
3344 if (ram_size == 0) {
3345 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3346 }
3347
303d4e86 3348 configure_accelerator();
214910a7 3349
d3b12f5d 3350 qemu_init_cpu_loop();
3fcf7b6b
AL
3351 if (qemu_init_main_loop()) {
3352 fprintf(stderr, "qemu_init_main_loop failed\n");
3353 exit(1);
3354 }
a0abe474 3355
967c0da7
PM
3356 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3357 if (machine_opts) {
3358 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3359 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3360 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3361 } else {
3362 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3363 }
3364
a0abe474
PM
3365 if (!kernel_cmdline) {
3366 kernel_cmdline = "";
3367 }
3368
a20dd508 3369 linux_boot = (kernel_filename != NULL);
6c41b272 3370
f8d39c01
TS
3371 if (!linux_boot && *kernel_cmdline != '\0') {
3372 fprintf(stderr, "-append only allowed with -kernel option\n");
3373 exit(1);
3374 }
3375
3376 if (!linux_boot && initrd_filename != NULL) {
3377 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3378 exit(1);
3379 }
3380
412beee6
GL
3381 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3382 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3383 exit(1);
3384 }
3385
9156d763 3386 os_set_line_buffering();
3b46e624 3387
7183b4b4
AL
3388 if (init_timer_alarm() < 0) {
3389 fprintf(stderr, "could not initialize alarm timer\n");
3390 exit(1);
3391 }
0abe905d 3392
ad1be899
AL
3393#ifdef CONFIG_SPICE
3394 /* spice needs the timers to be initialized by this point */
3395 qemu_spice_init();
3396#endif
3397
0abe905d
MF
3398 if (icount_option && (kvm_enabled() || xen_enabled())) {
3399 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3400 exit(1);
3401 }
4e3de9e9 3402 configure_icount(icount_option);
634fce96 3403
dc1c9fe8
MM
3404 if (net_init_clients() < 0) {
3405 exit(1);
702c651c 3406 }
f1510b2c 3407
dc72ac14 3408 /* init the bluetooth world */
bd3c948d
GH
3409 if (foreach_device_config(DEV_BT, bt_parse))
3410 exit(1);
dc72ac14 3411
834e76ea
AP
3412 if (!xen_enabled()) {
3413 /* On 32-bit hosts, QEMU is limited by virtual address space */
3414 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3415 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3416 exit(1);
3417 }
3418 }
9ae02555 3419
d5ab9713 3420 cpu_exec_init_all();
26a5f13b 3421
eb852011 3422 bdrv_init_with_whitelist();
c4b1fcc0 3423
c163b5ca
LS
3424 blk_mig_init();
3425
e4bcb14c 3426 /* open the virtual block devices */
9dfd7c7a 3427 if (snapshot)
3329f07b
GH
3428 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3429 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
9dfd7c7a 3430 exit(1);
3e3d5815 3431
4e5d9b57
MA
3432 default_drive(default_cdrom, snapshot, machine->use_scsi,
3433 IF_DEFAULT, 2, CDROM_OPTS);
3434 default_drive(default_floppy, snapshot, machine->use_scsi,
3435 IF_FLOPPY, 0, FD_OPTS);
3436 default_drive(default_sdcard, snapshot, machine->use_scsi,
3437 IF_SD, 0, SD_OPTS);
3438
7908c78d 3439 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
8a7ddc38 3440
268a362c
AL
3441 if (nb_numa_nodes > 0) {
3442 int i;
3443
ea0e5418
SL
3444 if (nb_numa_nodes > MAX_NODES) {
3445 nb_numa_nodes = MAX_NODES;
268a362c
AL
3446 }
3447
3448 /* If no memory size if given for any node, assume the default case
3449 * and distribute the available memory equally across all nodes
3450 */
3451 for (i = 0; i < nb_numa_nodes; i++) {
3452 if (node_mem[i] != 0)
3453 break;
3454 }
3455 if (i == nb_numa_nodes) {
3456 uint64_t usedmem = 0;
3457
3458 /* On Linux, the each node's border has to be 8MB aligned,
3459 * the final node gets the rest.
3460 */
3461 for (i = 0; i < nb_numa_nodes - 1; i++) {
3462 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3463 usedmem += node_mem[i];
3464 }
3465 node_mem[i] = ram_size - usedmem;
3466 }
3467
3468 for (i = 0; i < nb_numa_nodes; i++) {
ee785fed 3469 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
268a362c 3470 break;
ee785fed 3471 }
268a362c
AL
3472 }
3473 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3474 * must cope with this anyway, because there are BIOSes out there in
3475 * real machines which also use this scheme.
3476 */
3477 if (i == nb_numa_nodes) {
991dfefd 3478 for (i = 0; i < max_cpus; i++) {
ee785fed 3479 set_bit(i, node_cpumask[i % nb_numa_nodes]);
268a362c
AL
3480 }
3481 }
3482 }
3483
3329f07b 3484 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
157b9319
JK
3485 exit(1);
3486 }
3487
998bbd74
GH
3488 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3489 exit(1);
6a5e8b0e
GH
3490 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3491 exit(1);
aee1b935
GH
3492 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3493 exit(1);
c9f398e5
PA
3494 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3495 exit(1);
2796dae0 3496
7f1b17f2
PB
3497 /* If no default VGA is requested, the default is "none". */
3498 if (default_vga && cirrus_vga_available()) {
3499 vga_model = "cirrus";
a369da5f 3500 }
7f1b17f2 3501 select_vgahw(vga_model);
a369da5f 3502
3329f07b 3503 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
ff952ba2
MA
3504 exit(0);
3505
09aaa160
MA
3506 if (watchdog) {
3507 i = select_watchdog(watchdog);
3508 if (i > 0)
3509 exit (i == 1 ? 1 : 0);
3510 }
3511
b6b61144 3512 if (machine->compat_props) {
458fb679 3513 qdev_prop_register_global_list(machine->compat_props);
b6b61144 3514 }
d0fef6fb
GH
3515 qemu_add_globals();
3516
1de81d28
AL
3517 qdev_machine_init();
3518
fbe1b595 3519 machine->init(ram_size, boot_devices,
3023f332
AL
3520 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3521
ea375f9a 3522 cpu_synchronize_all_post_init();
268a362c 3523
87d0a28e 3524 set_numa_modes();
268a362c 3525
6f338c34
AL
3526 current_machine = machine;
3527
3023f332
AL
3528 /* init USB devices */
3529 if (usb_enabled) {
0752706d
MA
3530 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3531 exit(1);
3023f332
AL
3532 }
3533
bd3c948d 3534 /* init generic devices */
3329f07b 3535 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
bd3c948d
GH
3536 exit(1);
3537
668680f7
MA
3538 net_check_clients();
3539
3023f332 3540 /* just use the first displaystate for the moment */
b473df6e 3541 ds = get_displaystate();
993fbfdb 3542
a3e22260
GH
3543 if (using_spice)
3544 display_remote++;
6b62dc2d 3545 if (display_type == DT_DEFAULT && !display_remote) {
993fbfdb
AL
3546#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3547 display_type = DT_SDL;
821601ea 3548#elif defined(CONFIG_VNC)
993fbfdb
AL
3549 vnc_display = "localhost:0,to=99";
3550 show_vnc_port = 1;
821601ea
JS
3551#else
3552 display_type = DT_NONE;
993fbfdb
AL
3553#endif
3554 }
821601ea 3555
993fbfdb 3556
6b62dc2d 3557 /* init local displays */
993fbfdb
AL
3558 switch (display_type) {
3559 case DT_NOGRAPHIC:
3560 break;
4d3b6f6e 3561#if defined(CONFIG_CURSES)
993fbfdb
AL
3562 case DT_CURSES:
3563 curses_display_init(ds, full_screen);
3564 break;
4d3b6f6e 3565#endif
5b0753e0 3566#if defined(CONFIG_SDL)
993fbfdb
AL
3567 case DT_SDL:
3568 sdl_display_init(ds, full_screen, no_frame);
3569 break;
5b0753e0 3570#elif defined(CONFIG_COCOA)
993fbfdb
AL
3571 case DT_SDL:
3572 cocoa_display_init(ds, full_screen);
3573 break;
313aa567 3574#endif
6b62dc2d
GH
3575 default:
3576 break;
3577 }
3578
0ce235a7
GN
3579 /* must be after terminal init, SDL library changes signal handlers */
3580 os_setup_signal_handling();
3581
821601ea 3582#ifdef CONFIG_VNC
6b62dc2d
GH
3583 /* init remote displays */
3584 if (vnc_display) {
993fbfdb 3585 vnc_display_init(ds);
94b204ca
AK
3586 if (vnc_display_open(ds, vnc_display) < 0) {
3587 fprintf(stderr, "Failed to start VNC server on `%s'\n",
3588 vnc_display);
993fbfdb 3589 exit(1);
94b204ca 3590 }
f92f8afe 3591
993fbfdb
AL
3592 if (show_vnc_port) {
3593 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
f92f8afe 3594 }
313aa567 3595 }
821601ea 3596#endif
a3e22260 3597#ifdef CONFIG_SPICE
a19cbfb3 3598 if (using_spice && !qxl_enabled) {
a3e22260
GH
3599 qemu_spice_display_init(ds);
3600 }
3601#endif
5b08fc10 3602
6b62dc2d
GH
3603 /* display setup */
3604 dpy_resize(ds);
3023f332
AL
3605 dcl = ds->listeners;
3606 while (dcl != NULL) {
3607 if (dcl->dpy_refresh != NULL) {
7bd427d8
PB
3608 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3609 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
0f2ad63f 3610 break;
20d8a3ed 3611 }
3023f332 3612 dcl = dcl->next;
20d8a3ed 3613 }
b473df6e 3614 text_consoles_set_display(ds);
2796dae0 3615
ef0c4a0d 3616 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
59030a8c 3617 exit(1);
45669e00 3618 }
45669e00 3619
3418bd25
GH
3620 qdev_machine_creation_done();
3621
15ff7705
GH
3622 if (rom_load_all() != 0) {
3623 fprintf(stderr, "rom loading failed\n");
3624 exit(1);
3625 }
45a50b16 3626
80376c3f
IY
3627 /* TODO: once all bus devices are qdevified, this should be done
3628 * when bus is created by qdev.c */
3629 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4cab946a
GN
3630 qemu_run_machine_init_done_notifiers();
3631
e063eb1f 3632 qemu_system_reset(VMRESET_SILENT);
05f2401e 3633 if (loadvm) {
03cd4655 3634 if (load_vmstate(loadvm) < 0) {
05f2401e
JQ
3635 autostart = 0;
3636 }
3637 }
d63d307f 3638
2bb8c10c 3639 if (incoming) {
d5c5dacc
AK
3640 Error *errp = NULL;
3641 int ret = qemu_start_incoming_migration(incoming, &errp);
8ca5e801 3642 if (ret < 0) {
d5c5dacc
AK
3643 if (error_is_set(&errp)) {
3644 fprintf(stderr, "Migrate: %s\n", error_get_pretty(errp));
3645 error_free(errp);
3646 }
8ca5e801
JQ
3647 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3648 incoming, ret);
3649 exit(ret);
3650 }
6b99dadc 3651 } else if (autostart) {
c0f4ce77 3652 vm_start();
6b99dadc 3653 }
ffd843bc 3654
eb505be1 3655 os_setup_post();
71e3ceb8 3656
99435906 3657 resume_all_vcpus();
8a7ddc38 3658 main_loop();
99435906
PB
3659 bdrv_close_all();
3660 pause_all_vcpus();
63a01ef8 3661 net_cleanup();
3d3b8303 3662 res_free();
b46a8906 3663
0824d6fc
FB
3664 return 0;
3665}