HACKING: List areas where we may rely on impdef C behaviour
[qemu.git] / vl.c
bloba3ab3841a7dffa06872a2779bf820ae20c33e30d
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
31 #include "bitmap.h"
33 /* Needed early for CONFIG_BSD etc. */
34 #include "config-host.h"
36 #ifndef _WIN32
37 #include <libgen.h>
38 #include <sys/times.h>
39 #include <sys/wait.h>
40 #include <termios.h>
41 #include <sys/mman.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <net/if.h>
47 #include <arpa/inet.h>
48 #include <dirent.h>
49 #include <netdb.h>
50 #include <sys/select.h>
52 #ifdef CONFIG_BSD
53 #include <sys/stat.h>
54 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <malloc.h>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
65 #endif
67 #ifdef CONFIG_SECCOMP
68 #include "qemu-seccomp.h"
69 #endif
71 #ifdef __sun__
72 #include <sys/stat.h>
73 #include <sys/ethernet.h>
74 #include <sys/sockio.h>
75 #include <netinet/arp.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_icmp.h> // must come after ip.h
79 #include <netinet/udp.h>
80 #include <netinet/tcp.h>
81 #include <net/if.h>
82 #include <syslog.h>
83 #include <stropts.h>
84 #endif
85 #endif
86 #endif
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
90 #endif
92 #ifdef _WIN32
93 #include <windows.h>
94 #endif
96 #ifdef CONFIG_SDL
97 #if defined(__APPLE__) || defined(main)
98 #include <SDL.h>
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
104 #undef main
105 #define main qemu_main
106 #endif
107 #endif /* CONFIG_SDL */
109 #ifdef CONFIG_COCOA
110 #undef main
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
114 #include <glib.h>
116 #include "hw/hw.h"
117 #include "hw/boards.h"
118 #include "hw/usb.h"
119 #include "hw/pcmcia.h"
120 #include "hw/pc.h"
121 #include "hw/isa.h"
122 #include "hw/baum.h"
123 #include "hw/bt.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
126 #include "hw/xen.h"
127 #include "hw/qdev.h"
128 #include "hw/loader.h"
129 #include "bt-host.h"
130 #include "net.h"
131 #include "net/slirp.h"
132 #include "monitor.h"
133 #include "console.h"
134 #include "sysemu.h"
135 #include "gdbstub.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
139 #include "blockdev.h"
140 #include "hw/block-common.h"
141 #include "block-migration.h"
142 #include "dma.h"
143 #include "audio/audio.h"
144 #include "migration.h"
145 #include "kvm.h"
146 #include "qjson.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-options.h"
150 #include "qmp-commands.h"
151 #include "main-loop.h"
152 #ifdef CONFIG_VIRTFS
153 #include "fsdev/qemu-fsdev.h"
154 #endif
155 #include "qtest.h"
157 #include "disas.h"
159 #include "qemu_socket.h"
161 #include "slirp/libslirp.h"
163 #include "trace.h"
164 #include "trace/control.h"
165 #include "qemu-queue.h"
166 #include "cpus.h"
167 #include "arch_init.h"
168 #include "osdep.h"
170 #include "ui/qemu-spice.h"
171 #include "qapi/string-input-visitor.h"
173 //#define DEBUG_NET
174 //#define DEBUG_SLIRP
176 #define DEFAULT_RAM_SIZE 128
178 #define MAX_VIRTIO_CONSOLES 1
180 static const char *data_dir;
181 const char *bios_name = NULL;
182 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
183 DisplayType display_type = DT_DEFAULT;
184 static int display_remote;
185 const char* keyboard_layout = NULL;
186 ram_addr_t ram_size;
187 const char *mem_path = NULL;
188 #ifdef MAP_POPULATE
189 int mem_prealloc = 0; /* force preallocation of physical target memory */
190 #endif
191 int nb_nics;
192 NICInfo nd_table[MAX_NICS];
193 int autostart;
194 static int rtc_utc = 1;
195 static int rtc_date_offset = -1; /* -1 means no change */
196 QEMUClock *rtc_clock;
197 int vga_interface_type = VGA_NONE;
198 static int full_screen = 0;
199 #ifdef CONFIG_SDL
200 static int no_frame = 0;
201 #endif
202 int no_quit = 0;
203 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
204 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
205 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
206 int win2k_install_hack = 0;
207 int singlestep = 0;
208 int smp_cpus = 1;
209 int max_cpus = 0;
210 int smp_cores = 1;
211 int smp_threads = 1;
212 #ifdef CONFIG_VNC
213 const char *vnc_display;
214 #endif
215 int acpi_enabled = 1;
216 int no_hpet = 0;
217 int fd_bootchk = 1;
218 static int no_reboot;
219 int no_shutdown = 0;
220 int cursor_hide = 1;
221 int graphic_rotate = 0;
222 const char *watchdog;
223 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
224 int nb_option_roms;
225 int semihosting_enabled = 0;
226 int old_param = 0;
227 const char *qemu_name;
228 int alt_grab = 0;
229 int ctrl_grab = 0;
230 unsigned int nb_prom_envs = 0;
231 const char *prom_envs[MAX_PROM_ENVS];
232 int boot_menu;
233 uint8_t *boot_splash_filedata;
234 int boot_splash_filedata_size;
235 uint8_t qemu_extra_params_fw[2];
237 typedef struct FWBootEntry FWBootEntry;
239 struct FWBootEntry {
240 QTAILQ_ENTRY(FWBootEntry) link;
241 int32_t bootindex;
242 DeviceState *dev;
243 char *suffix;
246 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
247 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
249 int nb_numa_nodes;
250 uint64_t node_mem[MAX_NODES];
251 unsigned long *node_cpumask[MAX_NODES];
253 uint8_t qemu_uuid[16];
255 static QEMUBootSetHandler *boot_set_handler;
256 static void *boot_set_opaque;
258 static NotifierList exit_notifiers =
259 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
261 static NotifierList machine_init_done_notifiers =
262 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
264 static int tcg_allowed = 1;
265 int kvm_allowed = 0;
266 int xen_allowed = 0;
267 uint32_t xen_domid;
268 enum xen_mode xen_mode = XEN_EMULATE;
269 static int tcg_tb_size;
271 static int default_serial = 1;
272 static int default_parallel = 1;
273 static int default_virtcon = 1;
274 static int default_monitor = 1;
275 static int default_floppy = 1;
276 static int default_cdrom = 1;
277 static int default_sdcard = 1;
278 static int default_vga = 1;
280 static struct {
281 const char *driver;
282 int *flag;
283 } default_list[] = {
284 { .driver = "isa-serial", .flag = &default_serial },
285 { .driver = "isa-parallel", .flag = &default_parallel },
286 { .driver = "isa-fdc", .flag = &default_floppy },
287 { .driver = "ide-cd", .flag = &default_cdrom },
288 { .driver = "ide-hd", .flag = &default_cdrom },
289 { .driver = "ide-drive", .flag = &default_cdrom },
290 { .driver = "scsi-cd", .flag = &default_cdrom },
291 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
292 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
293 { .driver = "virtio-serial", .flag = &default_virtcon },
294 { .driver = "VGA", .flag = &default_vga },
295 { .driver = "isa-vga", .flag = &default_vga },
296 { .driver = "cirrus-vga", .flag = &default_vga },
297 { .driver = "isa-cirrus-vga", .flag = &default_vga },
298 { .driver = "vmware-svga", .flag = &default_vga },
299 { .driver = "qxl-vga", .flag = &default_vga },
302 const char *qemu_get_vm_name(void)
304 return qemu_name;
307 static void res_free(void)
309 if (boot_splash_filedata != NULL) {
310 g_free(boot_splash_filedata);
311 boot_splash_filedata = NULL;
315 static int default_driver_check(QemuOpts *opts, void *opaque)
317 const char *driver = qemu_opt_get(opts, "driver");
318 int i;
320 if (!driver)
321 return 0;
322 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
323 if (strcmp(default_list[i].driver, driver) != 0)
324 continue;
325 *(default_list[i].flag) = 0;
327 return 0;
330 /***********************************************************/
331 /* QEMU state */
333 static RunState current_run_state = RUN_STATE_PRELAUNCH;
335 typedef struct {
336 RunState from;
337 RunState to;
338 } RunStateTransition;
340 static const RunStateTransition runstate_transitions_def[] = {
341 /* from -> to */
342 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
344 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
345 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
347 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
348 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
350 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
351 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
353 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
354 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
356 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
357 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
359 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
360 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
361 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
363 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
364 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
366 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
368 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
369 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
370 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
371 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
372 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
373 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
374 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
375 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
376 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
378 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
380 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
381 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
383 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
384 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
385 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
386 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
388 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
389 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
391 { RUN_STATE_MAX, RUN_STATE_MAX },
394 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
396 bool runstate_check(RunState state)
398 return current_run_state == state;
401 static void runstate_init(void)
403 const RunStateTransition *p;
405 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
407 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
408 runstate_valid_transitions[p->from][p->to] = true;
412 /* This function will abort() on invalid state transitions */
413 void runstate_set(RunState new_state)
415 assert(new_state < RUN_STATE_MAX);
417 if (!runstate_valid_transitions[current_run_state][new_state]) {
418 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
419 RunState_lookup[current_run_state],
420 RunState_lookup[new_state]);
421 abort();
424 current_run_state = new_state;
427 int runstate_is_running(void)
429 return runstate_check(RUN_STATE_RUNNING);
432 StatusInfo *qmp_query_status(Error **errp)
434 StatusInfo *info = g_malloc0(sizeof(*info));
436 info->running = runstate_is_running();
437 info->singlestep = singlestep;
438 info->status = current_run_state;
440 return info;
443 /***********************************************************/
444 /* real time host monotonic timer */
446 /***********************************************************/
447 /* host time/date access */
448 void qemu_get_timedate(struct tm *tm, int offset)
450 time_t ti;
451 struct tm *ret;
453 time(&ti);
454 ti += offset;
455 if (rtc_date_offset == -1) {
456 if (rtc_utc)
457 ret = gmtime(&ti);
458 else
459 ret = localtime(&ti);
460 } else {
461 ti -= rtc_date_offset;
462 ret = gmtime(&ti);
465 memcpy(tm, ret, sizeof(struct tm));
468 int qemu_timedate_diff(struct tm *tm)
470 time_t seconds;
472 if (rtc_date_offset == -1)
473 if (rtc_utc)
474 seconds = mktimegm(tm);
475 else {
476 struct tm tmp = *tm;
477 tmp.tm_isdst = -1; /* use timezone to figure it out */
478 seconds = mktime(&tmp);
480 else
481 seconds = mktimegm(tm) + rtc_date_offset;
483 return seconds - time(NULL);
486 void rtc_change_mon_event(struct tm *tm)
488 QObject *data;
490 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
491 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
492 qobject_decref(data);
495 static void configure_rtc_date_offset(const char *startdate, int legacy)
497 time_t rtc_start_date;
498 struct tm tm;
500 if (!strcmp(startdate, "now") && legacy) {
501 rtc_date_offset = -1;
502 } else {
503 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
504 &tm.tm_year,
505 &tm.tm_mon,
506 &tm.tm_mday,
507 &tm.tm_hour,
508 &tm.tm_min,
509 &tm.tm_sec) == 6) {
510 /* OK */
511 } else if (sscanf(startdate, "%d-%d-%d",
512 &tm.tm_year,
513 &tm.tm_mon,
514 &tm.tm_mday) == 3) {
515 tm.tm_hour = 0;
516 tm.tm_min = 0;
517 tm.tm_sec = 0;
518 } else {
519 goto date_fail;
521 tm.tm_year -= 1900;
522 tm.tm_mon--;
523 rtc_start_date = mktimegm(&tm);
524 if (rtc_start_date == -1) {
525 date_fail:
526 fprintf(stderr, "Invalid date format. Valid formats are:\n"
527 "'2006-06-17T16:01:21' or '2006-06-17'\n");
528 exit(1);
530 rtc_date_offset = time(NULL) - rtc_start_date;
534 static void configure_rtc(QemuOpts *opts)
536 const char *value;
538 value = qemu_opt_get(opts, "base");
539 if (value) {
540 if (!strcmp(value, "utc")) {
541 rtc_utc = 1;
542 } else if (!strcmp(value, "localtime")) {
543 rtc_utc = 0;
544 } else {
545 configure_rtc_date_offset(value, 0);
548 value = qemu_opt_get(opts, "clock");
549 if (value) {
550 if (!strcmp(value, "host")) {
551 rtc_clock = host_clock;
552 } else if (!strcmp(value, "rt")) {
553 rtc_clock = rt_clock;
554 } else if (!strcmp(value, "vm")) {
555 rtc_clock = vm_clock;
556 } else {
557 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
558 exit(1);
561 value = qemu_opt_get(opts, "driftfix");
562 if (value) {
563 if (!strcmp(value, "slew")) {
564 static GlobalProperty slew_lost_ticks[] = {
566 .driver = "mc146818rtc",
567 .property = "lost_tick_policy",
568 .value = "slew",
570 { /* end of list */ }
573 qdev_prop_register_global_list(slew_lost_ticks);
574 } else if (!strcmp(value, "none")) {
575 /* discard is default */
576 } else {
577 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
578 exit(1);
583 /***********************************************************/
584 /* Bluetooth support */
585 static int nb_hcis;
586 static int cur_hci;
587 static struct HCIInfo *hci_table[MAX_NICS];
589 static struct bt_vlan_s {
590 struct bt_scatternet_s net;
591 int id;
592 struct bt_vlan_s *next;
593 } *first_bt_vlan;
595 /* find or alloc a new bluetooth "VLAN" */
596 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
598 struct bt_vlan_s **pvlan, *vlan;
599 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
600 if (vlan->id == id)
601 return &vlan->net;
603 vlan = g_malloc0(sizeof(struct bt_vlan_s));
604 vlan->id = id;
605 pvlan = &first_bt_vlan;
606 while (*pvlan != NULL)
607 pvlan = &(*pvlan)->next;
608 *pvlan = vlan;
609 return &vlan->net;
612 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
616 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
618 return -ENOTSUP;
621 static struct HCIInfo null_hci = {
622 .cmd_send = null_hci_send,
623 .sco_send = null_hci_send,
624 .acl_send = null_hci_send,
625 .bdaddr_set = null_hci_addr_set,
628 struct HCIInfo *qemu_next_hci(void)
630 if (cur_hci == nb_hcis)
631 return &null_hci;
633 return hci_table[cur_hci++];
636 static struct HCIInfo *hci_init(const char *str)
638 char *endp;
639 struct bt_scatternet_s *vlan = 0;
641 if (!strcmp(str, "null"))
642 /* null */
643 return &null_hci;
644 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
645 /* host[:hciN] */
646 return bt_host_hci(str[4] ? str + 5 : "hci0");
647 else if (!strncmp(str, "hci", 3)) {
648 /* hci[,vlan=n] */
649 if (str[3]) {
650 if (!strncmp(str + 3, ",vlan=", 6)) {
651 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
652 if (*endp)
653 vlan = 0;
655 } else
656 vlan = qemu_find_bt_vlan(0);
657 if (vlan)
658 return bt_new_hci(vlan);
661 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
663 return 0;
666 static int bt_hci_parse(const char *str)
668 struct HCIInfo *hci;
669 bdaddr_t bdaddr;
671 if (nb_hcis >= MAX_NICS) {
672 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
673 return -1;
676 hci = hci_init(str);
677 if (!hci)
678 return -1;
680 bdaddr.b[0] = 0x52;
681 bdaddr.b[1] = 0x54;
682 bdaddr.b[2] = 0x00;
683 bdaddr.b[3] = 0x12;
684 bdaddr.b[4] = 0x34;
685 bdaddr.b[5] = 0x56 + nb_hcis;
686 hci->bdaddr_set(hci, bdaddr.b);
688 hci_table[nb_hcis++] = hci;
690 return 0;
693 static void bt_vhci_add(int vlan_id)
695 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
697 if (!vlan->slave)
698 fprintf(stderr, "qemu: warning: adding a VHCI to "
699 "an empty scatternet %i\n", vlan_id);
701 bt_vhci_init(bt_new_hci(vlan));
704 static struct bt_device_s *bt_device_add(const char *opt)
706 struct bt_scatternet_s *vlan;
707 int vlan_id = 0;
708 char *endp = strstr(opt, ",vlan=");
709 int len = (endp ? endp - opt : strlen(opt)) + 1;
710 char devname[10];
712 pstrcpy(devname, MIN(sizeof(devname), len), opt);
714 if (endp) {
715 vlan_id = strtol(endp + 6, &endp, 0);
716 if (*endp) {
717 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
718 return 0;
722 vlan = qemu_find_bt_vlan(vlan_id);
724 if (!vlan->slave)
725 fprintf(stderr, "qemu: warning: adding a slave device to "
726 "an empty scatternet %i\n", vlan_id);
728 if (!strcmp(devname, "keyboard"))
729 return bt_keyboard_init(vlan);
731 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
732 return 0;
735 static int bt_parse(const char *opt)
737 const char *endp, *p;
738 int vlan;
740 if (strstart(opt, "hci", &endp)) {
741 if (!*endp || *endp == ',') {
742 if (*endp)
743 if (!strstart(endp, ",vlan=", 0))
744 opt = endp + 1;
746 return bt_hci_parse(opt);
748 } else if (strstart(opt, "vhci", &endp)) {
749 if (!*endp || *endp == ',') {
750 if (*endp) {
751 if (strstart(endp, ",vlan=", &p)) {
752 vlan = strtol(p, (char **) &endp, 0);
753 if (*endp) {
754 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
755 return 1;
757 } else {
758 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
759 return 1;
761 } else
762 vlan = 0;
764 bt_vhci_add(vlan);
765 return 0;
767 } else if (strstart(opt, "device:", &endp))
768 return !bt_device_add(endp);
770 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
771 return 1;
774 static int parse_sandbox(QemuOpts *opts, void *opaque)
776 /* FIXME: change this to true for 1.3 */
777 if (qemu_opt_get_bool(opts, "enable", false)) {
778 #ifdef CONFIG_SECCOMP
779 if (seccomp_start() < 0) {
780 qerror_report(ERROR_CLASS_GENERIC_ERROR,
781 "failed to install seccomp syscall filter in the kernel");
782 return -1;
784 #else
785 qerror_report(ERROR_CLASS_GENERIC_ERROR,
786 "sandboxing request but seccomp is not compiled into this build");
787 return -1;
788 #endif
791 return 0;
794 /*********QEMU USB setting******/
795 bool usb_enabled(bool default_usb)
797 QemuOpts *mach_opts;
798 mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
799 if (mach_opts) {
800 return qemu_opt_get_bool(mach_opts, "usb", default_usb);
802 return default_usb;
805 #ifndef _WIN32
806 static int parse_add_fd(QemuOpts *opts, void *opaque)
808 int fd, dupfd, flags;
809 int64_t fdset_id;
810 const char *fd_opaque = NULL;
812 fd = qemu_opt_get_number(opts, "fd", -1);
813 fdset_id = qemu_opt_get_number(opts, "set", -1);
814 fd_opaque = qemu_opt_get(opts, "opaque");
816 if (fd < 0) {
817 qerror_report(ERROR_CLASS_GENERIC_ERROR,
818 "fd option is required and must be non-negative");
819 return -1;
822 if (fd <= STDERR_FILENO) {
823 qerror_report(ERROR_CLASS_GENERIC_ERROR,
824 "fd cannot be a standard I/O stream");
825 return -1;
829 * All fds inherited across exec() necessarily have FD_CLOEXEC
830 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
832 flags = fcntl(fd, F_GETFD);
833 if (flags == -1 || (flags & FD_CLOEXEC)) {
834 qerror_report(ERROR_CLASS_GENERIC_ERROR,
835 "fd is not valid or already in use");
836 return -1;
839 if (fdset_id < 0) {
840 qerror_report(ERROR_CLASS_GENERIC_ERROR,
841 "set option is required and must be non-negative");
842 return -1;
845 #ifdef F_DUPFD_CLOEXEC
846 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
847 #else
848 dupfd = dup(fd);
849 if (dupfd != -1) {
850 qemu_set_cloexec(dupfd);
852 #endif
853 if (dupfd == -1) {
854 qerror_report(ERROR_CLASS_GENERIC_ERROR,
855 "Error duplicating fd: %s", strerror(errno));
856 return -1;
859 /* add the duplicate fd, and optionally the opaque string, to the fd set */
860 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
861 fd_opaque, NULL);
863 return 0;
866 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
868 int fd;
870 fd = qemu_opt_get_number(opts, "fd", -1);
871 close(fd);
873 return 0;
875 #endif
877 /***********************************************************/
878 /* QEMU Block devices */
880 #define HD_OPTS "media=disk"
881 #define CDROM_OPTS "media=cdrom"
882 #define FD_OPTS ""
883 #define PFLASH_OPTS ""
884 #define MTD_OPTS ""
885 #define SD_OPTS ""
887 static int drive_init_func(QemuOpts *opts, void *opaque)
889 int *use_scsi = opaque;
891 return drive_init(opts, *use_scsi) == NULL;
894 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
896 if (NULL == qemu_opt_get(opts, "snapshot")) {
897 qemu_opt_set(opts, "snapshot", "on");
899 return 0;
902 static void default_drive(int enable, int snapshot, int use_scsi,
903 BlockInterfaceType type, int index,
904 const char *optstr)
906 QemuOpts *opts;
908 if (type == IF_DEFAULT) {
909 type = use_scsi ? IF_SCSI : IF_IDE;
912 if (!enable || drive_get_by_index(type, index)) {
913 return;
916 opts = drive_add(type, index, NULL, optstr);
917 if (snapshot) {
918 drive_enable_snapshot(opts, NULL);
920 if (!drive_init(opts, use_scsi)) {
921 exit(1);
925 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
927 boot_set_handler = func;
928 boot_set_opaque = opaque;
931 int qemu_boot_set(const char *boot_devices)
933 if (!boot_set_handler) {
934 return -EINVAL;
936 return boot_set_handler(boot_set_opaque, boot_devices);
939 static void validate_bootdevices(char *devices)
941 /* We just do some generic consistency checks */
942 const char *p;
943 int bitmap = 0;
945 for (p = devices; *p != '\0'; p++) {
946 /* Allowed boot devices are:
947 * a-b: floppy disk drives
948 * c-f: IDE disk drives
949 * g-m: machine implementation dependent drives
950 * n-p: network devices
951 * It's up to each machine implementation to check if the given boot
952 * devices match the actual hardware implementation and firmware
953 * features.
955 if (*p < 'a' || *p > 'p') {
956 fprintf(stderr, "Invalid boot device '%c'\n", *p);
957 exit(1);
959 if (bitmap & (1 << (*p - 'a'))) {
960 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
961 exit(1);
963 bitmap |= 1 << (*p - 'a');
967 static void restore_boot_devices(void *opaque)
969 char *standard_boot_devices = opaque;
970 static int first = 1;
972 /* Restore boot order and remove ourselves after the first boot */
973 if (first) {
974 first = 0;
975 return;
978 qemu_boot_set(standard_boot_devices);
980 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
981 g_free(standard_boot_devices);
984 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
985 const char *suffix)
987 FWBootEntry *node, *i;
989 if (bootindex < 0) {
990 return;
993 assert(dev != NULL || suffix != NULL);
995 node = g_malloc0(sizeof(FWBootEntry));
996 node->bootindex = bootindex;
997 node->suffix = suffix ? g_strdup(suffix) : NULL;
998 node->dev = dev;
1000 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1001 if (i->bootindex == bootindex) {
1002 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1003 exit(1);
1004 } else if (i->bootindex < bootindex) {
1005 continue;
1007 QTAILQ_INSERT_BEFORE(i, node, link);
1008 return;
1010 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1014 * This function returns null terminated string that consist of new line
1015 * separated device paths.
1017 * memory pointed by "size" is assigned total length of the array in bytes
1020 char *get_boot_devices_list(uint32_t *size)
1022 FWBootEntry *i;
1023 uint32_t total = 0;
1024 char *list = NULL;
1026 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1027 char *devpath = NULL, *bootpath;
1028 int len;
1030 if (i->dev) {
1031 devpath = qdev_get_fw_dev_path(i->dev);
1032 assert(devpath);
1035 if (i->suffix && devpath) {
1036 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1038 bootpath = g_malloc(bootpathlen);
1039 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1040 g_free(devpath);
1041 } else if (devpath) {
1042 bootpath = devpath;
1043 } else {
1044 assert(i->suffix);
1045 bootpath = g_strdup(i->suffix);
1048 if (total) {
1049 list[total-1] = '\n';
1051 len = strlen(bootpath) + 1;
1052 list = g_realloc(list, total + len);
1053 memcpy(&list[total], bootpath, len);
1054 total += len;
1055 g_free(bootpath);
1058 *size = total;
1060 return list;
1063 static void numa_add(const char *optarg)
1065 char option[128];
1066 char *endptr;
1067 unsigned long long value, endvalue;
1068 int nodenr;
1070 value = endvalue = 0ULL;
1072 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1073 if (!strcmp(option, "node")) {
1074 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1075 nodenr = nb_numa_nodes;
1076 } else {
1077 nodenr = strtoull(option, NULL, 10);
1080 if (get_param_value(option, 128, "mem", optarg) == 0) {
1081 node_mem[nodenr] = 0;
1082 } else {
1083 int64_t sval;
1084 sval = strtosz(option, &endptr);
1085 if (sval < 0 || *endptr) {
1086 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1087 exit(1);
1089 node_mem[nodenr] = sval;
1091 if (get_param_value(option, 128, "cpus", optarg) != 0) {
1092 value = strtoull(option, &endptr, 10);
1093 if (*endptr == '-') {
1094 endvalue = strtoull(endptr+1, &endptr, 10);
1095 } else {
1096 endvalue = value;
1099 if (!(endvalue < MAX_CPUMASK_BITS)) {
1100 endvalue = MAX_CPUMASK_BITS - 1;
1101 fprintf(stderr,
1102 "A max of %d CPUs are supported in a guest\n",
1103 MAX_CPUMASK_BITS);
1106 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
1108 nb_numa_nodes++;
1112 static void smp_parse(const char *optarg)
1114 int smp, sockets = 0, threads = 0, cores = 0;
1115 char *endptr;
1116 char option[128];
1118 smp = strtoul(optarg, &endptr, 10);
1119 if (endptr != optarg) {
1120 if (*endptr == ',') {
1121 endptr++;
1124 if (get_param_value(option, 128, "sockets", endptr) != 0)
1125 sockets = strtoull(option, NULL, 10);
1126 if (get_param_value(option, 128, "cores", endptr) != 0)
1127 cores = strtoull(option, NULL, 10);
1128 if (get_param_value(option, 128, "threads", endptr) != 0)
1129 threads = strtoull(option, NULL, 10);
1130 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1131 max_cpus = strtoull(option, NULL, 10);
1133 /* compute missing values, prefer sockets over cores over threads */
1134 if (smp == 0 || sockets == 0) {
1135 sockets = sockets > 0 ? sockets : 1;
1136 cores = cores > 0 ? cores : 1;
1137 threads = threads > 0 ? threads : 1;
1138 if (smp == 0) {
1139 smp = cores * threads * sockets;
1141 } else {
1142 if (cores == 0) {
1143 threads = threads > 0 ? threads : 1;
1144 cores = smp / (sockets * threads);
1145 } else {
1146 threads = smp / (cores * sockets);
1149 smp_cpus = smp;
1150 smp_cores = cores > 0 ? cores : 1;
1151 smp_threads = threads > 0 ? threads : 1;
1152 if (max_cpus == 0)
1153 max_cpus = smp_cpus;
1156 /***********************************************************/
1157 /* USB devices */
1159 static int usb_device_add(const char *devname)
1161 const char *p;
1162 USBDevice *dev = NULL;
1164 if (!usb_enabled(false)) {
1165 return -1;
1168 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1169 dev = usbdevice_create(devname);
1170 if (dev)
1171 goto done;
1173 /* the other ones */
1174 #ifndef CONFIG_LINUX
1175 /* only the linux version is qdev-ified, usb-bsd still needs this */
1176 if (strstart(devname, "host:", &p)) {
1177 dev = usb_host_device_open(usb_bus_find(-1), p);
1178 } else
1179 #endif
1180 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1181 dev = usb_bt_init(usb_bus_find(-1),
1182 devname[2] ? hci_init(p)
1183 : bt_new_hci(qemu_find_bt_vlan(0)));
1184 } else {
1185 return -1;
1187 if (!dev)
1188 return -1;
1190 done:
1191 return 0;
1194 static int usb_device_del(const char *devname)
1196 int bus_num, addr;
1197 const char *p;
1199 if (strstart(devname, "host:", &p))
1200 return usb_host_device_close(p);
1202 if (!usb_enabled(false)) {
1203 return -1;
1206 p = strchr(devname, '.');
1207 if (!p)
1208 return -1;
1209 bus_num = strtoul(devname, NULL, 0);
1210 addr = strtoul(p + 1, NULL, 0);
1212 return usb_device_delete_addr(bus_num, addr);
1215 static int usb_parse(const char *cmdline)
1217 int r;
1218 r = usb_device_add(cmdline);
1219 if (r < 0) {
1220 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1222 return r;
1225 void do_usb_add(Monitor *mon, const QDict *qdict)
1227 const char *devname = qdict_get_str(qdict, "devname");
1228 if (usb_device_add(devname) < 0) {
1229 error_report("could not add USB device '%s'", devname);
1233 void do_usb_del(Monitor *mon, const QDict *qdict)
1235 const char *devname = qdict_get_str(qdict, "devname");
1236 if (usb_device_del(devname) < 0) {
1237 error_report("could not delete USB device '%s'", devname);
1241 /***********************************************************/
1242 /* PCMCIA/Cardbus */
1244 static struct pcmcia_socket_entry_s {
1245 PCMCIASocket *socket;
1246 struct pcmcia_socket_entry_s *next;
1247 } *pcmcia_sockets = 0;
1249 void pcmcia_socket_register(PCMCIASocket *socket)
1251 struct pcmcia_socket_entry_s *entry;
1253 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1254 entry->socket = socket;
1255 entry->next = pcmcia_sockets;
1256 pcmcia_sockets = entry;
1259 void pcmcia_socket_unregister(PCMCIASocket *socket)
1261 struct pcmcia_socket_entry_s *entry, **ptr;
1263 ptr = &pcmcia_sockets;
1264 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1265 if (entry->socket == socket) {
1266 *ptr = entry->next;
1267 g_free(entry);
1271 void pcmcia_info(Monitor *mon)
1273 struct pcmcia_socket_entry_s *iter;
1275 if (!pcmcia_sockets)
1276 monitor_printf(mon, "No PCMCIA sockets\n");
1278 for (iter = pcmcia_sockets; iter; iter = iter->next)
1279 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1280 iter->socket->attached ? iter->socket->card_string :
1281 "Empty");
1284 /***********************************************************/
1285 /* machine registration */
1287 static QEMUMachine *first_machine = NULL;
1288 QEMUMachine *current_machine = NULL;
1290 int qemu_register_machine(QEMUMachine *m)
1292 QEMUMachine **pm;
1293 pm = &first_machine;
1294 while (*pm != NULL)
1295 pm = &(*pm)->next;
1296 m->next = NULL;
1297 *pm = m;
1298 return 0;
1301 static QEMUMachine *find_machine(const char *name)
1303 QEMUMachine *m;
1305 for(m = first_machine; m != NULL; m = m->next) {
1306 if (!strcmp(m->name, name))
1307 return m;
1308 if (m->alias && !strcmp(m->alias, name))
1309 return m;
1311 return NULL;
1314 QEMUMachine *find_default_machine(void)
1316 QEMUMachine *m;
1318 for(m = first_machine; m != NULL; m = m->next) {
1319 if (m->is_default) {
1320 return m;
1323 return NULL;
1326 MachineInfoList *qmp_query_machines(Error **errp)
1328 MachineInfoList *mach_list = NULL;
1329 QEMUMachine *m;
1331 for (m = first_machine; m; m = m->next) {
1332 MachineInfoList *entry;
1333 MachineInfo *info;
1335 info = g_malloc0(sizeof(*info));
1336 if (m->is_default) {
1337 info->has_is_default = true;
1338 info->is_default = true;
1341 if (m->alias) {
1342 info->has_alias = true;
1343 info->alias = g_strdup(m->alias);
1346 info->name = g_strdup(m->name);
1348 entry = g_malloc0(sizeof(*entry));
1349 entry->value = info;
1350 entry->next = mach_list;
1351 mach_list = entry;
1354 return mach_list;
1357 /***********************************************************/
1358 /* main execution loop */
1360 static void gui_update(void *opaque)
1362 uint64_t interval = GUI_REFRESH_INTERVAL;
1363 DisplayState *ds = opaque;
1364 DisplayChangeListener *dcl;
1366 dpy_refresh(ds);
1368 QLIST_FOREACH(dcl, &ds->listeners, next) {
1369 if (dcl->gui_timer_interval &&
1370 dcl->gui_timer_interval < interval)
1371 interval = dcl->gui_timer_interval;
1373 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1376 void gui_setup_refresh(DisplayState *ds)
1378 DisplayChangeListener *dcl;
1379 bool need_timer = false;
1380 bool have_gfx = false;
1381 bool have_text = false;
1383 QLIST_FOREACH(dcl, &ds->listeners, next) {
1384 if (dcl->dpy_refresh != NULL) {
1385 need_timer = true;
1387 if (dcl->dpy_gfx_update != NULL) {
1388 have_gfx = true;
1390 if (dcl->dpy_text_update != NULL) {
1391 have_text = true;
1395 if (need_timer && ds->gui_timer == NULL) {
1396 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
1397 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
1399 if (!need_timer && ds->gui_timer != NULL) {
1400 qemu_del_timer(ds->gui_timer);
1401 qemu_free_timer(ds->gui_timer);
1402 ds->gui_timer = NULL;
1405 ds->have_gfx = have_gfx;
1406 ds->have_text = have_text;
1409 struct vm_change_state_entry {
1410 VMChangeStateHandler *cb;
1411 void *opaque;
1412 QLIST_ENTRY (vm_change_state_entry) entries;
1415 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1417 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1418 void *opaque)
1420 VMChangeStateEntry *e;
1422 e = g_malloc0(sizeof (*e));
1424 e->cb = cb;
1425 e->opaque = opaque;
1426 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1427 return e;
1430 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1432 QLIST_REMOVE (e, entries);
1433 g_free (e);
1436 void vm_state_notify(int running, RunState state)
1438 VMChangeStateEntry *e;
1440 trace_vm_state_notify(running, state);
1442 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1443 e->cb(e->opaque, running, state);
1447 void vm_start(void)
1449 if (!runstate_is_running()) {
1450 cpu_enable_ticks();
1451 runstate_set(RUN_STATE_RUNNING);
1452 vm_state_notify(1, RUN_STATE_RUNNING);
1453 resume_all_vcpus();
1454 monitor_protocol_event(QEVENT_RESUME, NULL);
1458 /* reset/shutdown handler */
1460 typedef struct QEMUResetEntry {
1461 QTAILQ_ENTRY(QEMUResetEntry) entry;
1462 QEMUResetHandler *func;
1463 void *opaque;
1464 } QEMUResetEntry;
1466 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1467 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1468 static int reset_requested;
1469 static int shutdown_requested, shutdown_signal = -1;
1470 static pid_t shutdown_pid;
1471 static int powerdown_requested;
1472 static int debug_requested;
1473 static int suspend_requested;
1474 static int wakeup_requested;
1475 static NotifierList powerdown_notifiers =
1476 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1477 static NotifierList suspend_notifiers =
1478 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1479 static NotifierList wakeup_notifiers =
1480 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1481 static uint32_t wakeup_reason_mask = ~0;
1482 static RunState vmstop_requested = RUN_STATE_MAX;
1484 int qemu_shutdown_requested_get(void)
1486 return shutdown_requested;
1489 int qemu_reset_requested_get(void)
1491 return reset_requested;
1494 static int qemu_shutdown_requested(void)
1496 int r = shutdown_requested;
1497 shutdown_requested = 0;
1498 return r;
1501 static void qemu_kill_report(void)
1503 if (!qtest_enabled() && shutdown_signal != -1) {
1504 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1505 if (shutdown_pid == 0) {
1506 /* This happens for eg ^C at the terminal, so it's worth
1507 * avoiding printing an odd message in that case.
1509 fputc('\n', stderr);
1510 } else {
1511 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1513 shutdown_signal = -1;
1517 static int qemu_reset_requested(void)
1519 int r = reset_requested;
1520 reset_requested = 0;
1521 return r;
1524 static int qemu_suspend_requested(void)
1526 int r = suspend_requested;
1527 suspend_requested = 0;
1528 return r;
1531 static int qemu_wakeup_requested(void)
1533 int r = wakeup_requested;
1534 wakeup_requested = 0;
1535 return r;
1538 static int qemu_powerdown_requested(void)
1540 int r = powerdown_requested;
1541 powerdown_requested = 0;
1542 return r;
1545 static int qemu_debug_requested(void)
1547 int r = debug_requested;
1548 debug_requested = 0;
1549 return r;
1552 /* We use RUN_STATE_MAX but any invalid value will do */
1553 static bool qemu_vmstop_requested(RunState *r)
1555 if (vmstop_requested < RUN_STATE_MAX) {
1556 *r = vmstop_requested;
1557 vmstop_requested = RUN_STATE_MAX;
1558 return true;
1561 return false;
1564 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1566 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1568 re->func = func;
1569 re->opaque = opaque;
1570 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1573 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1575 QEMUResetEntry *re;
1577 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1578 if (re->func == func && re->opaque == opaque) {
1579 QTAILQ_REMOVE(&reset_handlers, re, entry);
1580 g_free(re);
1581 return;
1586 void qemu_devices_reset(void)
1588 QEMUResetEntry *re, *nre;
1590 /* reset all devices */
1591 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1592 re->func(re->opaque);
1596 void qemu_system_reset(bool report)
1598 if (current_machine && current_machine->reset) {
1599 current_machine->reset();
1600 } else {
1601 qemu_devices_reset();
1603 if (report) {
1604 monitor_protocol_event(QEVENT_RESET, NULL);
1606 cpu_synchronize_all_post_reset();
1609 void qemu_system_reset_request(void)
1611 if (no_reboot) {
1612 shutdown_requested = 1;
1613 } else {
1614 reset_requested = 1;
1616 cpu_stop_current();
1617 qemu_notify_event();
1620 static void qemu_system_suspend(void)
1622 pause_all_vcpus();
1623 notifier_list_notify(&suspend_notifiers, NULL);
1624 runstate_set(RUN_STATE_SUSPENDED);
1625 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1628 void qemu_system_suspend_request(void)
1630 if (runstate_check(RUN_STATE_SUSPENDED)) {
1631 return;
1633 suspend_requested = 1;
1634 cpu_stop_current();
1635 qemu_notify_event();
1638 void qemu_register_suspend_notifier(Notifier *notifier)
1640 notifier_list_add(&suspend_notifiers, notifier);
1643 void qemu_system_wakeup_request(WakeupReason reason)
1645 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1646 return;
1648 if (!(wakeup_reason_mask & (1 << reason))) {
1649 return;
1651 runstate_set(RUN_STATE_RUNNING);
1652 notifier_list_notify(&wakeup_notifiers, &reason);
1653 wakeup_requested = 1;
1654 qemu_notify_event();
1657 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1659 if (enabled) {
1660 wakeup_reason_mask |= (1 << reason);
1661 } else {
1662 wakeup_reason_mask &= ~(1 << reason);
1666 void qemu_register_wakeup_notifier(Notifier *notifier)
1668 notifier_list_add(&wakeup_notifiers, notifier);
1671 void qemu_system_killed(int signal, pid_t pid)
1673 shutdown_signal = signal;
1674 shutdown_pid = pid;
1675 no_shutdown = 0;
1676 qemu_system_shutdown_request();
1679 void qemu_system_shutdown_request(void)
1681 shutdown_requested = 1;
1682 qemu_notify_event();
1685 static void qemu_system_powerdown(void)
1687 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1688 notifier_list_notify(&powerdown_notifiers, NULL);
1691 void qemu_system_powerdown_request(void)
1693 powerdown_requested = 1;
1694 qemu_notify_event();
1697 void qemu_register_powerdown_notifier(Notifier *notifier)
1699 notifier_list_add(&powerdown_notifiers, notifier);
1702 void qemu_system_debug_request(void)
1704 debug_requested = 1;
1705 qemu_notify_event();
1708 void qemu_system_vmstop_request(RunState state)
1710 vmstop_requested = state;
1711 qemu_notify_event();
1714 static bool main_loop_should_exit(void)
1716 RunState r;
1717 if (qemu_debug_requested()) {
1718 vm_stop(RUN_STATE_DEBUG);
1720 if (qemu_suspend_requested()) {
1721 qemu_system_suspend();
1723 if (qemu_shutdown_requested()) {
1724 qemu_kill_report();
1725 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1726 if (no_shutdown) {
1727 vm_stop(RUN_STATE_SHUTDOWN);
1728 } else {
1729 return true;
1732 if (qemu_reset_requested()) {
1733 pause_all_vcpus();
1734 cpu_synchronize_all_states();
1735 qemu_system_reset(VMRESET_REPORT);
1736 resume_all_vcpus();
1737 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1738 runstate_check(RUN_STATE_SHUTDOWN)) {
1739 runstate_set(RUN_STATE_PAUSED);
1742 if (qemu_wakeup_requested()) {
1743 pause_all_vcpus();
1744 cpu_synchronize_all_states();
1745 qemu_system_reset(VMRESET_SILENT);
1746 resume_all_vcpus();
1747 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1749 if (qemu_powerdown_requested()) {
1750 qemu_system_powerdown();
1752 if (qemu_vmstop_requested(&r)) {
1753 vm_stop(r);
1755 return false;
1758 static void main_loop(void)
1760 bool nonblocking;
1761 int last_io = 0;
1762 #ifdef CONFIG_PROFILER
1763 int64_t ti;
1764 #endif
1765 do {
1766 nonblocking = !kvm_enabled() && last_io > 0;
1767 #ifdef CONFIG_PROFILER
1768 ti = profile_getclock();
1769 #endif
1770 last_io = main_loop_wait(nonblocking);
1771 #ifdef CONFIG_PROFILER
1772 dev_time += profile_getclock() - ti;
1773 #endif
1774 } while (!main_loop_should_exit());
1777 static void version(void)
1779 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1782 static void help(int exitcode)
1784 version();
1785 printf("usage: %s [options] [disk_image]\n\n"
1786 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1787 error_get_progname());
1789 #define QEMU_OPTIONS_GENERATE_HELP
1790 #include "qemu-options-wrapper.h"
1792 printf("\nDuring emulation, the following keys are useful:\n"
1793 "ctrl-alt-f toggle full screen\n"
1794 "ctrl-alt-n switch to virtual console 'n'\n"
1795 "ctrl-alt toggle mouse and keyboard grab\n"
1796 "\n"
1797 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1799 exit(exitcode);
1802 #define HAS_ARG 0x0001
1804 typedef struct QEMUOption {
1805 const char *name;
1806 int flags;
1807 int index;
1808 uint32_t arch_mask;
1809 } QEMUOption;
1811 static const QEMUOption qemu_options[] = {
1812 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1813 #define QEMU_OPTIONS_GENERATE_OPTIONS
1814 #include "qemu-options-wrapper.h"
1815 { NULL },
1818 static bool vga_available(void)
1820 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1823 static bool cirrus_vga_available(void)
1825 return object_class_by_name("cirrus-vga")
1826 || object_class_by_name("isa-cirrus-vga");
1829 static bool vmware_vga_available(void)
1831 return object_class_by_name("vmware-svga");
1834 static bool qxl_vga_available(void)
1836 return object_class_by_name("qxl-vga");
1839 static void select_vgahw (const char *p)
1841 const char *opts;
1843 vga_interface_type = VGA_NONE;
1844 if (strstart(p, "std", &opts)) {
1845 if (vga_available()) {
1846 vga_interface_type = VGA_STD;
1847 } else {
1848 fprintf(stderr, "Error: standard VGA not available\n");
1849 exit(0);
1851 } else if (strstart(p, "cirrus", &opts)) {
1852 if (cirrus_vga_available()) {
1853 vga_interface_type = VGA_CIRRUS;
1854 } else {
1855 fprintf(stderr, "Error: Cirrus VGA not available\n");
1856 exit(0);
1858 } else if (strstart(p, "vmware", &opts)) {
1859 if (vmware_vga_available()) {
1860 vga_interface_type = VGA_VMWARE;
1861 } else {
1862 fprintf(stderr, "Error: VMWare SVGA not available\n");
1863 exit(0);
1865 } else if (strstart(p, "xenfb", &opts)) {
1866 vga_interface_type = VGA_XENFB;
1867 } else if (strstart(p, "qxl", &opts)) {
1868 if (qxl_vga_available()) {
1869 vga_interface_type = VGA_QXL;
1870 } else {
1871 fprintf(stderr, "Error: QXL VGA not available\n");
1872 exit(0);
1874 } else if (!strstart(p, "none", &opts)) {
1875 invalid_vga:
1876 fprintf(stderr, "Unknown vga type: %s\n", p);
1877 exit(1);
1879 while (*opts) {
1880 const char *nextopt;
1882 if (strstart(opts, ",retrace=", &nextopt)) {
1883 opts = nextopt;
1884 if (strstart(opts, "dumb", &nextopt))
1885 vga_retrace_method = VGA_RETRACE_DUMB;
1886 else if (strstart(opts, "precise", &nextopt))
1887 vga_retrace_method = VGA_RETRACE_PRECISE;
1888 else goto invalid_vga;
1889 } else goto invalid_vga;
1890 opts = nextopt;
1894 static DisplayType select_display(const char *p)
1896 const char *opts;
1897 DisplayType display = DT_DEFAULT;
1899 if (strstart(p, "sdl", &opts)) {
1900 #ifdef CONFIG_SDL
1901 display = DT_SDL;
1902 while (*opts) {
1903 const char *nextopt;
1905 if (strstart(opts, ",frame=", &nextopt)) {
1906 opts = nextopt;
1907 if (strstart(opts, "on", &nextopt)) {
1908 no_frame = 0;
1909 } else if (strstart(opts, "off", &nextopt)) {
1910 no_frame = 1;
1911 } else {
1912 goto invalid_sdl_args;
1914 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1915 opts = nextopt;
1916 if (strstart(opts, "on", &nextopt)) {
1917 alt_grab = 1;
1918 } else if (strstart(opts, "off", &nextopt)) {
1919 alt_grab = 0;
1920 } else {
1921 goto invalid_sdl_args;
1923 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1924 opts = nextopt;
1925 if (strstart(opts, "on", &nextopt)) {
1926 ctrl_grab = 1;
1927 } else if (strstart(opts, "off", &nextopt)) {
1928 ctrl_grab = 0;
1929 } else {
1930 goto invalid_sdl_args;
1932 } else if (strstart(opts, ",window_close=", &nextopt)) {
1933 opts = nextopt;
1934 if (strstart(opts, "on", &nextopt)) {
1935 no_quit = 0;
1936 } else if (strstart(opts, "off", &nextopt)) {
1937 no_quit = 1;
1938 } else {
1939 goto invalid_sdl_args;
1941 } else {
1942 invalid_sdl_args:
1943 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1944 exit(1);
1946 opts = nextopt;
1948 #else
1949 fprintf(stderr, "SDL support is disabled\n");
1950 exit(1);
1951 #endif
1952 } else if (strstart(p, "vnc", &opts)) {
1953 #ifdef CONFIG_VNC
1954 display_remote++;
1956 if (*opts) {
1957 const char *nextopt;
1959 if (strstart(opts, "=", &nextopt)) {
1960 vnc_display = nextopt;
1963 if (!vnc_display) {
1964 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1965 exit(1);
1967 #else
1968 fprintf(stderr, "VNC support is disabled\n");
1969 exit(1);
1970 #endif
1971 } else if (strstart(p, "curses", &opts)) {
1972 #ifdef CONFIG_CURSES
1973 display = DT_CURSES;
1974 #else
1975 fprintf(stderr, "Curses support is disabled\n");
1976 exit(1);
1977 #endif
1978 } else if (strstart(p, "none", &opts)) {
1979 display = DT_NONE;
1980 } else {
1981 fprintf(stderr, "Unknown display type: %s\n", p);
1982 exit(1);
1985 return display;
1988 static int balloon_parse(const char *arg)
1990 QemuOpts *opts;
1992 if (strcmp(arg, "none") == 0) {
1993 return 0;
1996 if (!strncmp(arg, "virtio", 6)) {
1997 if (arg[6] == ',') {
1998 /* have params -> parse them */
1999 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2000 if (!opts)
2001 return -1;
2002 } else {
2003 /* create empty opts */
2004 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
2006 qemu_opt_set(opts, "driver", "virtio-balloon");
2007 return 0;
2010 return -1;
2013 char *qemu_find_file(int type, const char *name)
2015 int len;
2016 const char *subdir;
2017 char *buf;
2019 /* Try the name as a straight path first */
2020 if (access(name, R_OK) == 0) {
2021 return g_strdup(name);
2023 switch (type) {
2024 case QEMU_FILE_TYPE_BIOS:
2025 subdir = "";
2026 break;
2027 case QEMU_FILE_TYPE_KEYMAP:
2028 subdir = "keymaps/";
2029 break;
2030 default:
2031 abort();
2033 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2034 buf = g_malloc0(len);
2035 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2036 if (access(buf, R_OK)) {
2037 g_free(buf);
2038 return NULL;
2040 return buf;
2043 static int device_help_func(QemuOpts *opts, void *opaque)
2045 return qdev_device_help(opts);
2048 static int device_init_func(QemuOpts *opts, void *opaque)
2050 DeviceState *dev;
2052 dev = qdev_device_add(opts);
2053 if (!dev)
2054 return -1;
2055 return 0;
2058 static int chardev_init_func(QemuOpts *opts, void *opaque)
2060 CharDriverState *chr;
2062 chr = qemu_chr_new_from_opts(opts, NULL);
2063 if (!chr)
2064 return -1;
2065 return 0;
2068 #ifdef CONFIG_VIRTFS
2069 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2071 int ret;
2072 ret = qemu_fsdev_add(opts);
2074 return ret;
2076 #endif
2078 static int mon_init_func(QemuOpts *opts, void *opaque)
2080 CharDriverState *chr;
2081 const char *chardev;
2082 const char *mode;
2083 int flags;
2085 mode = qemu_opt_get(opts, "mode");
2086 if (mode == NULL) {
2087 mode = "readline";
2089 if (strcmp(mode, "readline") == 0) {
2090 flags = MONITOR_USE_READLINE;
2091 } else if (strcmp(mode, "control") == 0) {
2092 flags = MONITOR_USE_CONTROL;
2093 } else {
2094 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2095 exit(1);
2098 if (qemu_opt_get_bool(opts, "pretty", 0))
2099 flags |= MONITOR_USE_PRETTY;
2101 if (qemu_opt_get_bool(opts, "default", 0))
2102 flags |= MONITOR_IS_DEFAULT;
2104 chardev = qemu_opt_get(opts, "chardev");
2105 chr = qemu_chr_find(chardev);
2106 if (chr == NULL) {
2107 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2108 exit(1);
2111 monitor_init(chr, flags);
2112 return 0;
2115 static void monitor_parse(const char *optarg, const char *mode)
2117 static int monitor_device_index = 0;
2118 QemuOpts *opts;
2119 const char *p;
2120 char label[32];
2121 int def = 0;
2123 if (strstart(optarg, "chardev:", &p)) {
2124 snprintf(label, sizeof(label), "%s", p);
2125 } else {
2126 snprintf(label, sizeof(label), "compat_monitor%d",
2127 monitor_device_index);
2128 if (monitor_device_index == 0) {
2129 def = 1;
2131 opts = qemu_chr_parse_compat(label, optarg);
2132 if (!opts) {
2133 fprintf(stderr, "parse error: %s\n", optarg);
2134 exit(1);
2138 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2139 if (!opts) {
2140 fprintf(stderr, "duplicate chardev: %s\n", label);
2141 exit(1);
2143 qemu_opt_set(opts, "mode", mode);
2144 qemu_opt_set(opts, "chardev", label);
2145 if (def)
2146 qemu_opt_set(opts, "default", "on");
2147 monitor_device_index++;
2150 struct device_config {
2151 enum {
2152 DEV_USB, /* -usbdevice */
2153 DEV_BT, /* -bt */
2154 DEV_SERIAL, /* -serial */
2155 DEV_PARALLEL, /* -parallel */
2156 DEV_VIRTCON, /* -virtioconsole */
2157 DEV_DEBUGCON, /* -debugcon */
2158 DEV_GDB, /* -gdb, -s */
2159 } type;
2160 const char *cmdline;
2161 Location loc;
2162 QTAILQ_ENTRY(device_config) next;
2165 static QTAILQ_HEAD(, device_config) device_configs =
2166 QTAILQ_HEAD_INITIALIZER(device_configs);
2168 static void add_device_config(int type, const char *cmdline)
2170 struct device_config *conf;
2172 conf = g_malloc0(sizeof(*conf));
2173 conf->type = type;
2174 conf->cmdline = cmdline;
2175 loc_save(&conf->loc);
2176 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2179 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2181 struct device_config *conf;
2182 int rc;
2184 QTAILQ_FOREACH(conf, &device_configs, next) {
2185 if (conf->type != type)
2186 continue;
2187 loc_push_restore(&conf->loc);
2188 rc = func(conf->cmdline);
2189 loc_pop(&conf->loc);
2190 if (0 != rc)
2191 return rc;
2193 return 0;
2196 static int serial_parse(const char *devname)
2198 static int index = 0;
2199 char label[32];
2201 if (strcmp(devname, "none") == 0)
2202 return 0;
2203 if (index == MAX_SERIAL_PORTS) {
2204 fprintf(stderr, "qemu: too many serial ports\n");
2205 exit(1);
2207 snprintf(label, sizeof(label), "serial%d", index);
2208 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2209 if (!serial_hds[index]) {
2210 fprintf(stderr, "qemu: could not connect serial device"
2211 " to character backend '%s'\n", devname);
2212 return -1;
2214 index++;
2215 return 0;
2218 static int parallel_parse(const char *devname)
2220 static int index = 0;
2221 char label[32];
2223 if (strcmp(devname, "none") == 0)
2224 return 0;
2225 if (index == MAX_PARALLEL_PORTS) {
2226 fprintf(stderr, "qemu: too many parallel ports\n");
2227 exit(1);
2229 snprintf(label, sizeof(label), "parallel%d", index);
2230 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2231 if (!parallel_hds[index]) {
2232 fprintf(stderr, "qemu: could not connect parallel device"
2233 " to character backend '%s'\n", devname);
2234 return -1;
2236 index++;
2237 return 0;
2240 static int virtcon_parse(const char *devname)
2242 QemuOptsList *device = qemu_find_opts("device");
2243 static int index = 0;
2244 char label[32];
2245 QemuOpts *bus_opts, *dev_opts;
2247 if (strcmp(devname, "none") == 0)
2248 return 0;
2249 if (index == MAX_VIRTIO_CONSOLES) {
2250 fprintf(stderr, "qemu: too many virtio consoles\n");
2251 exit(1);
2254 bus_opts = qemu_opts_create(device, NULL, 0, NULL);
2255 if (arch_type == QEMU_ARCH_S390X) {
2256 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2257 } else {
2258 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2261 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2262 qemu_opt_set(dev_opts, "driver", "virtconsole");
2264 snprintf(label, sizeof(label), "virtcon%d", index);
2265 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2266 if (!virtcon_hds[index]) {
2267 fprintf(stderr, "qemu: could not connect virtio console"
2268 " to character backend '%s'\n", devname);
2269 return -1;
2271 qemu_opt_set(dev_opts, "chardev", label);
2273 index++;
2274 return 0;
2277 static int debugcon_parse(const char *devname)
2279 QemuOpts *opts;
2281 if (!qemu_chr_new("debugcon", devname, NULL)) {
2282 exit(1);
2284 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2285 if (!opts) {
2286 fprintf(stderr, "qemu: already have a debugcon device\n");
2287 exit(1);
2289 qemu_opt_set(opts, "driver", "isa-debugcon");
2290 qemu_opt_set(opts, "chardev", "debugcon");
2291 return 0;
2294 static QEMUMachine *machine_parse(const char *name)
2296 QEMUMachine *m, *machine = NULL;
2298 if (name) {
2299 machine = find_machine(name);
2301 if (machine) {
2302 return machine;
2304 printf("Supported machines are:\n");
2305 for (m = first_machine; m != NULL; m = m->next) {
2306 if (m->alias) {
2307 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2309 printf("%-20s %s%s\n", m->name, m->desc,
2310 m->is_default ? " (default)" : "");
2312 exit(!name || !is_help_option(name));
2315 static int tcg_init(void)
2317 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2318 return 0;
2321 static struct {
2322 const char *opt_name;
2323 const char *name;
2324 int (*available)(void);
2325 int (*init)(void);
2326 int *allowed;
2327 } accel_list[] = {
2328 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2329 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2330 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2331 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2334 static int configure_accelerator(void)
2336 const char *p = NULL;
2337 char buf[10];
2338 int i, ret;
2339 bool accel_initialised = 0;
2340 bool init_failed = 0;
2342 QemuOptsList *list = qemu_find_opts("machine");
2343 if (!QTAILQ_EMPTY(&list->head)) {
2344 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2347 if (p == NULL) {
2348 /* Use the default "accelerator", tcg */
2349 p = "tcg";
2352 while (!accel_initialised && *p != '\0') {
2353 if (*p == ':') {
2354 p++;
2356 p = get_opt_name(buf, sizeof (buf), p, ':');
2357 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2358 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2359 *(accel_list[i].allowed) = 1;
2360 ret = accel_list[i].init();
2361 if (ret < 0) {
2362 init_failed = 1;
2363 if (!accel_list[i].available()) {
2364 printf("%s not supported for this target\n",
2365 accel_list[i].name);
2366 } else {
2367 fprintf(stderr, "failed to initialize %s: %s\n",
2368 accel_list[i].name,
2369 strerror(-ret));
2371 *(accel_list[i].allowed) = 0;
2372 } else {
2373 accel_initialised = 1;
2375 break;
2378 if (i == ARRAY_SIZE(accel_list)) {
2379 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2383 if (!accel_initialised) {
2384 fprintf(stderr, "No accelerator found!\n");
2385 exit(1);
2388 if (init_failed) {
2389 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2392 return !accel_initialised;
2395 void qemu_add_exit_notifier(Notifier *notify)
2397 notifier_list_add(&exit_notifiers, notify);
2400 void qemu_remove_exit_notifier(Notifier *notify)
2402 notifier_remove(notify);
2405 static void qemu_run_exit_notifiers(void)
2407 notifier_list_notify(&exit_notifiers, NULL);
2410 void qemu_add_machine_init_done_notifier(Notifier *notify)
2412 notifier_list_add(&machine_init_done_notifiers, notify);
2415 static void qemu_run_machine_init_done_notifiers(void)
2417 notifier_list_notify(&machine_init_done_notifiers, NULL);
2420 static const QEMUOption *lookup_opt(int argc, char **argv,
2421 const char **poptarg, int *poptind)
2423 const QEMUOption *popt;
2424 int optind = *poptind;
2425 char *r = argv[optind];
2426 const char *optarg;
2428 loc_set_cmdline(argv, optind, 1);
2429 optind++;
2430 /* Treat --foo the same as -foo. */
2431 if (r[1] == '-')
2432 r++;
2433 popt = qemu_options;
2434 for(;;) {
2435 if (!popt->name) {
2436 error_report("invalid option");
2437 exit(1);
2439 if (!strcmp(popt->name, r + 1))
2440 break;
2441 popt++;
2443 if (popt->flags & HAS_ARG) {
2444 if (optind >= argc) {
2445 error_report("requires an argument");
2446 exit(1);
2448 optarg = argv[optind++];
2449 loc_set_cmdline(argv, optind - 2, 2);
2450 } else {
2451 optarg = NULL;
2454 *poptarg = optarg;
2455 *poptind = optind;
2457 return popt;
2460 static gpointer malloc_and_trace(gsize n_bytes)
2462 void *ptr = malloc(n_bytes);
2463 trace_g_malloc(n_bytes, ptr);
2464 return ptr;
2467 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2469 void *ptr = realloc(mem, n_bytes);
2470 trace_g_realloc(mem, n_bytes, ptr);
2471 return ptr;
2474 static void free_and_trace(gpointer mem)
2476 trace_g_free(mem);
2477 free(mem);
2480 static int object_set_property(const char *name, const char *value, void *opaque)
2482 Object *obj = OBJECT(opaque);
2483 StringInputVisitor *siv;
2484 Error *local_err = NULL;
2486 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0) {
2487 return 0;
2490 siv = string_input_visitor_new(value);
2491 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2492 string_input_visitor_cleanup(siv);
2494 if (local_err) {
2495 qerror_report_err(local_err);
2496 error_free(local_err);
2497 return -1;
2500 return 0;
2503 static int object_create(QemuOpts *opts, void *opaque)
2505 const char *type = qemu_opt_get(opts, "qom-type");
2506 const char *id = qemu_opts_id(opts);
2507 Object *obj;
2509 g_assert(type != NULL);
2511 if (id == NULL) {
2512 qerror_report(QERR_MISSING_PARAMETER, "id");
2513 return -1;
2516 obj = object_new(type);
2517 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2518 return -1;
2521 object_property_add_child(container_get(object_get_root(), "/objects"),
2522 id, obj, NULL);
2524 return 0;
2527 int main(int argc, char **argv, char **envp)
2529 int i;
2530 int snapshot, linux_boot;
2531 const char *icount_option = NULL;
2532 const char *initrd_filename;
2533 const char *kernel_filename, *kernel_cmdline;
2534 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2535 DisplayState *ds;
2536 int cyls, heads, secs, translation;
2537 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2538 QemuOptsList *olist;
2539 int optind;
2540 const char *optarg;
2541 const char *loadvm = NULL;
2542 QEMUMachine *machine;
2543 const char *cpu_model;
2544 const char *vga_model = "none";
2545 const char *pid_file = NULL;
2546 const char *incoming = NULL;
2547 #ifdef CONFIG_VNC
2548 int show_vnc_port = 0;
2549 #endif
2550 bool defconfig = true;
2551 bool userconfig = true;
2552 const char *log_mask = NULL;
2553 const char *log_file = NULL;
2554 GMemVTable mem_trace = {
2555 .malloc = malloc_and_trace,
2556 .realloc = realloc_and_trace,
2557 .free = free_and_trace,
2559 const char *trace_events = NULL;
2560 const char *trace_file = NULL;
2562 atexit(qemu_run_exit_notifiers);
2563 error_set_progname(argv[0]);
2565 g_mem_set_vtable(&mem_trace);
2566 if (!g_thread_supported()) {
2567 #if !GLIB_CHECK_VERSION(2, 31, 0)
2568 g_thread_init(NULL);
2569 #else
2570 fprintf(stderr, "glib threading failed to initialize.\n");
2571 exit(1);
2572 #endif
2575 module_call_init(MODULE_INIT_QOM);
2577 runstate_init();
2579 init_clocks();
2580 rtc_clock = host_clock;
2582 qemu_cache_utils_init(envp);
2584 QLIST_INIT (&vm_change_state_head);
2585 os_setup_early_signal_handling();
2587 module_call_init(MODULE_INIT_MACHINE);
2588 machine = find_default_machine();
2589 cpu_model = NULL;
2590 ram_size = 0;
2591 snapshot = 0;
2592 cyls = heads = secs = 0;
2593 translation = BIOS_ATA_TRANSLATION_AUTO;
2595 for (i = 0; i < MAX_NODES; i++) {
2596 node_mem[i] = 0;
2597 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
2600 nb_numa_nodes = 0;
2601 nb_nics = 0;
2603 autostart= 1;
2605 /* first pass of option parsing */
2606 optind = 1;
2607 while (optind < argc) {
2608 if (argv[optind][0] != '-') {
2609 /* disk image */
2610 optind++;
2611 continue;
2612 } else {
2613 const QEMUOption *popt;
2615 popt = lookup_opt(argc, argv, &optarg, &optind);
2616 switch (popt->index) {
2617 case QEMU_OPTION_nodefconfig:
2618 defconfig = false;
2619 break;
2620 case QEMU_OPTION_nouserconfig:
2621 userconfig = false;
2622 break;
2627 if (defconfig) {
2628 int ret;
2629 ret = qemu_read_default_config_files(userconfig);
2630 if (ret < 0) {
2631 exit(1);
2635 /* second pass of option parsing */
2636 optind = 1;
2637 for(;;) {
2638 if (optind >= argc)
2639 break;
2640 if (argv[optind][0] != '-') {
2641 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2642 } else {
2643 const QEMUOption *popt;
2645 popt = lookup_opt(argc, argv, &optarg, &optind);
2646 if (!(popt->arch_mask & arch_type)) {
2647 printf("Option %s not supported for this target\n", popt->name);
2648 exit(1);
2650 switch(popt->index) {
2651 case QEMU_OPTION_M:
2652 machine = machine_parse(optarg);
2653 break;
2654 case QEMU_OPTION_no_kvm_irqchip: {
2655 olist = qemu_find_opts("machine");
2656 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2657 break;
2659 case QEMU_OPTION_cpu:
2660 /* hw initialization will check this */
2661 cpu_model = optarg;
2662 break;
2663 case QEMU_OPTION_hda:
2665 char buf[256];
2666 if (cyls == 0)
2667 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2668 else
2669 snprintf(buf, sizeof(buf),
2670 "%s,cyls=%d,heads=%d,secs=%d%s",
2671 HD_OPTS , cyls, heads, secs,
2672 translation == BIOS_ATA_TRANSLATION_LBA ?
2673 ",trans=lba" :
2674 translation == BIOS_ATA_TRANSLATION_NONE ?
2675 ",trans=none" : "");
2676 drive_add(IF_DEFAULT, 0, optarg, buf);
2677 break;
2679 case QEMU_OPTION_hdb:
2680 case QEMU_OPTION_hdc:
2681 case QEMU_OPTION_hdd:
2682 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2683 HD_OPTS);
2684 break;
2685 case QEMU_OPTION_drive:
2686 if (drive_def(optarg) == NULL) {
2687 exit(1);
2689 break;
2690 case QEMU_OPTION_set:
2691 if (qemu_set_option(optarg) != 0)
2692 exit(1);
2693 break;
2694 case QEMU_OPTION_global:
2695 if (qemu_global_option(optarg) != 0)
2696 exit(1);
2697 break;
2698 case QEMU_OPTION_mtdblock:
2699 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2700 break;
2701 case QEMU_OPTION_sd:
2702 drive_add(IF_SD, 0, optarg, SD_OPTS);
2703 break;
2704 case QEMU_OPTION_pflash:
2705 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2706 break;
2707 case QEMU_OPTION_snapshot:
2708 snapshot = 1;
2709 break;
2710 case QEMU_OPTION_hdachs:
2712 const char *p;
2713 p = optarg;
2714 cyls = strtol(p, (char **)&p, 0);
2715 if (cyls < 1 || cyls > 16383)
2716 goto chs_fail;
2717 if (*p != ',')
2718 goto chs_fail;
2719 p++;
2720 heads = strtol(p, (char **)&p, 0);
2721 if (heads < 1 || heads > 16)
2722 goto chs_fail;
2723 if (*p != ',')
2724 goto chs_fail;
2725 p++;
2726 secs = strtol(p, (char **)&p, 0);
2727 if (secs < 1 || secs > 63)
2728 goto chs_fail;
2729 if (*p == ',') {
2730 p++;
2731 if (!strcmp(p, "none"))
2732 translation = BIOS_ATA_TRANSLATION_NONE;
2733 else if (!strcmp(p, "lba"))
2734 translation = BIOS_ATA_TRANSLATION_LBA;
2735 else if (!strcmp(p, "auto"))
2736 translation = BIOS_ATA_TRANSLATION_AUTO;
2737 else
2738 goto chs_fail;
2739 } else if (*p != '\0') {
2740 chs_fail:
2741 fprintf(stderr, "qemu: invalid physical CHS format\n");
2742 exit(1);
2744 if (hda_opts != NULL) {
2745 char num[16];
2746 snprintf(num, sizeof(num), "%d", cyls);
2747 qemu_opt_set(hda_opts, "cyls", num);
2748 snprintf(num, sizeof(num), "%d", heads);
2749 qemu_opt_set(hda_opts, "heads", num);
2750 snprintf(num, sizeof(num), "%d", secs);
2751 qemu_opt_set(hda_opts, "secs", num);
2752 if (translation == BIOS_ATA_TRANSLATION_LBA)
2753 qemu_opt_set(hda_opts, "trans", "lba");
2754 if (translation == BIOS_ATA_TRANSLATION_NONE)
2755 qemu_opt_set(hda_opts, "trans", "none");
2758 break;
2759 case QEMU_OPTION_numa:
2760 if (nb_numa_nodes >= MAX_NODES) {
2761 fprintf(stderr, "qemu: too many NUMA nodes\n");
2762 exit(1);
2764 numa_add(optarg);
2765 break;
2766 case QEMU_OPTION_display:
2767 display_type = select_display(optarg);
2768 break;
2769 case QEMU_OPTION_nographic:
2770 display_type = DT_NOGRAPHIC;
2771 break;
2772 case QEMU_OPTION_curses:
2773 #ifdef CONFIG_CURSES
2774 display_type = DT_CURSES;
2775 #else
2776 fprintf(stderr, "Curses support is disabled\n");
2777 exit(1);
2778 #endif
2779 break;
2780 case QEMU_OPTION_portrait:
2781 graphic_rotate = 90;
2782 break;
2783 case QEMU_OPTION_rotate:
2784 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2785 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2786 graphic_rotate != 180 && graphic_rotate != 270) {
2787 fprintf(stderr,
2788 "qemu: only 90, 180, 270 deg rotation is available\n");
2789 exit(1);
2791 break;
2792 case QEMU_OPTION_kernel:
2793 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2794 break;
2795 case QEMU_OPTION_initrd:
2796 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
2797 break;
2798 case QEMU_OPTION_append:
2799 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
2800 break;
2801 case QEMU_OPTION_dtb:
2802 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2803 break;
2804 case QEMU_OPTION_cdrom:
2805 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2806 break;
2807 case QEMU_OPTION_boot:
2809 static const char * const params[] = {
2810 "order", "once", "menu",
2811 "splash", "splash-time",
2812 "reboot-timeout", NULL
2814 char buf[sizeof(boot_devices)];
2815 char *standard_boot_devices;
2816 int legacy = 0;
2818 if (!strchr(optarg, '=')) {
2819 legacy = 1;
2820 pstrcpy(buf, sizeof(buf), optarg);
2821 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2822 fprintf(stderr,
2823 "qemu: unknown boot parameter '%s' in '%s'\n",
2824 buf, optarg);
2825 exit(1);
2828 if (legacy ||
2829 get_param_value(buf, sizeof(buf), "order", optarg)) {
2830 validate_bootdevices(buf);
2831 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2833 if (!legacy) {
2834 if (get_param_value(buf, sizeof(buf),
2835 "once", optarg)) {
2836 validate_bootdevices(buf);
2837 standard_boot_devices = g_strdup(boot_devices);
2838 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2839 qemu_register_reset(restore_boot_devices,
2840 standard_boot_devices);
2842 if (get_param_value(buf, sizeof(buf),
2843 "menu", optarg)) {
2844 if (!strcmp(buf, "on")) {
2845 boot_menu = 1;
2846 } else if (!strcmp(buf, "off")) {
2847 boot_menu = 0;
2848 } else {
2849 fprintf(stderr,
2850 "qemu: invalid option value '%s'\n",
2851 buf);
2852 exit(1);
2855 qemu_opts_parse(qemu_find_opts("boot-opts"),
2856 optarg, 0);
2859 break;
2860 case QEMU_OPTION_fda:
2861 case QEMU_OPTION_fdb:
2862 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2863 optarg, FD_OPTS);
2864 break;
2865 case QEMU_OPTION_no_fd_bootchk:
2866 fd_bootchk = 0;
2867 break;
2868 case QEMU_OPTION_netdev:
2869 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2870 exit(1);
2872 break;
2873 case QEMU_OPTION_net:
2874 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2875 exit(1);
2877 break;
2878 #ifdef CONFIG_LIBISCSI
2879 case QEMU_OPTION_iscsi:
2880 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2881 if (!opts) {
2882 exit(1);
2884 break;
2885 #endif
2886 #ifdef CONFIG_SLIRP
2887 case QEMU_OPTION_tftp:
2888 legacy_tftp_prefix = optarg;
2889 break;
2890 case QEMU_OPTION_bootp:
2891 legacy_bootp_filename = optarg;
2892 break;
2893 case QEMU_OPTION_redir:
2894 if (net_slirp_redir(optarg) < 0)
2895 exit(1);
2896 break;
2897 #endif
2898 case QEMU_OPTION_bt:
2899 add_device_config(DEV_BT, optarg);
2900 break;
2901 case QEMU_OPTION_audio_help:
2902 if (!(audio_available())) {
2903 printf("Option %s not supported for this target\n", popt->name);
2904 exit(1);
2906 AUD_help ();
2907 exit (0);
2908 break;
2909 case QEMU_OPTION_soundhw:
2910 if (!(audio_available())) {
2911 printf("Option %s not supported for this target\n", popt->name);
2912 exit(1);
2914 select_soundhw (optarg);
2915 break;
2916 case QEMU_OPTION_h:
2917 help(0);
2918 break;
2919 case QEMU_OPTION_version:
2920 version();
2921 exit(0);
2922 break;
2923 case QEMU_OPTION_m: {
2924 int64_t value;
2925 uint64_t sz;
2926 char *end;
2928 value = strtosz(optarg, &end);
2929 if (value < 0 || *end) {
2930 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2931 exit(1);
2933 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
2934 ram_size = sz;
2935 if (ram_size != sz) {
2936 fprintf(stderr, "qemu: ram size too large\n");
2937 exit(1);
2939 break;
2941 case QEMU_OPTION_mempath:
2942 mem_path = optarg;
2943 break;
2944 #ifdef MAP_POPULATE
2945 case QEMU_OPTION_mem_prealloc:
2946 mem_prealloc = 1;
2947 break;
2948 #endif
2949 case QEMU_OPTION_d:
2950 log_mask = optarg;
2951 break;
2952 case QEMU_OPTION_D:
2953 log_file = optarg;
2954 break;
2955 case QEMU_OPTION_s:
2956 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2957 break;
2958 case QEMU_OPTION_gdb:
2959 add_device_config(DEV_GDB, optarg);
2960 break;
2961 case QEMU_OPTION_L:
2962 data_dir = optarg;
2963 break;
2964 case QEMU_OPTION_bios:
2965 bios_name = optarg;
2966 break;
2967 case QEMU_OPTION_singlestep:
2968 singlestep = 1;
2969 break;
2970 case QEMU_OPTION_S:
2971 autostart = 0;
2972 break;
2973 case QEMU_OPTION_k:
2974 keyboard_layout = optarg;
2975 break;
2976 case QEMU_OPTION_localtime:
2977 rtc_utc = 0;
2978 break;
2979 case QEMU_OPTION_vga:
2980 vga_model = optarg;
2981 default_vga = 0;
2982 break;
2983 case QEMU_OPTION_g:
2985 const char *p;
2986 int w, h, depth;
2987 p = optarg;
2988 w = strtol(p, (char **)&p, 10);
2989 if (w <= 0) {
2990 graphic_error:
2991 fprintf(stderr, "qemu: invalid resolution or depth\n");
2992 exit(1);
2994 if (*p != 'x')
2995 goto graphic_error;
2996 p++;
2997 h = strtol(p, (char **)&p, 10);
2998 if (h <= 0)
2999 goto graphic_error;
3000 if (*p == 'x') {
3001 p++;
3002 depth = strtol(p, (char **)&p, 10);
3003 if (depth != 8 && depth != 15 && depth != 16 &&
3004 depth != 24 && depth != 32)
3005 goto graphic_error;
3006 } else if (*p == '\0') {
3007 depth = graphic_depth;
3008 } else {
3009 goto graphic_error;
3012 graphic_width = w;
3013 graphic_height = h;
3014 graphic_depth = depth;
3016 break;
3017 case QEMU_OPTION_echr:
3019 char *r;
3020 term_escape_char = strtol(optarg, &r, 0);
3021 if (r == optarg)
3022 printf("Bad argument to echr\n");
3023 break;
3025 case QEMU_OPTION_monitor:
3026 monitor_parse(optarg, "readline");
3027 default_monitor = 0;
3028 break;
3029 case QEMU_OPTION_qmp:
3030 monitor_parse(optarg, "control");
3031 default_monitor = 0;
3032 break;
3033 case QEMU_OPTION_mon:
3034 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3035 if (!opts) {
3036 exit(1);
3038 default_monitor = 0;
3039 break;
3040 case QEMU_OPTION_chardev:
3041 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3042 if (!opts) {
3043 exit(1);
3045 break;
3046 case QEMU_OPTION_fsdev:
3047 olist = qemu_find_opts("fsdev");
3048 if (!olist) {
3049 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3050 exit(1);
3052 opts = qemu_opts_parse(olist, optarg, 1);
3053 if (!opts) {
3054 fprintf(stderr, "parse error: %s\n", optarg);
3055 exit(1);
3057 break;
3058 case QEMU_OPTION_virtfs: {
3059 QemuOpts *fsdev;
3060 QemuOpts *device;
3061 const char *writeout, *sock_fd, *socket;
3063 olist = qemu_find_opts("virtfs");
3064 if (!olist) {
3065 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3066 exit(1);
3068 opts = qemu_opts_parse(olist, optarg, 1);
3069 if (!opts) {
3070 fprintf(stderr, "parse error: %s\n", optarg);
3071 exit(1);
3074 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3075 qemu_opt_get(opts, "mount_tag") == NULL) {
3076 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3077 exit(1);
3079 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3080 qemu_opt_get(opts, "mount_tag"),
3081 1, NULL);
3082 if (!fsdev) {
3083 fprintf(stderr, "duplicate fsdev id: %s\n",
3084 qemu_opt_get(opts, "mount_tag"));
3085 exit(1);
3088 writeout = qemu_opt_get(opts, "writeout");
3089 if (writeout) {
3090 #ifdef CONFIG_SYNC_FILE_RANGE
3091 qemu_opt_set(fsdev, "writeout", writeout);
3092 #else
3093 fprintf(stderr, "writeout=immediate not supported on "
3094 "this platform\n");
3095 exit(1);
3096 #endif
3098 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3099 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3100 qemu_opt_set(fsdev, "security_model",
3101 qemu_opt_get(opts, "security_model"));
3102 socket = qemu_opt_get(opts, "socket");
3103 if (socket) {
3104 qemu_opt_set(fsdev, "socket", socket);
3106 sock_fd = qemu_opt_get(opts, "sock_fd");
3107 if (sock_fd) {
3108 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3111 qemu_opt_set_bool(fsdev, "readonly",
3112 qemu_opt_get_bool(opts, "readonly", 0));
3113 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3114 NULL);
3115 qemu_opt_set(device, "driver", "virtio-9p-pci");
3116 qemu_opt_set(device, "fsdev",
3117 qemu_opt_get(opts, "mount_tag"));
3118 qemu_opt_set(device, "mount_tag",
3119 qemu_opt_get(opts, "mount_tag"));
3120 break;
3122 case QEMU_OPTION_virtfs_synth: {
3123 QemuOpts *fsdev;
3124 QemuOpts *device;
3126 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3127 1, NULL);
3128 if (!fsdev) {
3129 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3130 exit(1);
3132 qemu_opt_set(fsdev, "fsdriver", "synth");
3134 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3135 NULL);
3136 qemu_opt_set(device, "driver", "virtio-9p-pci");
3137 qemu_opt_set(device, "fsdev", "v_synth");
3138 qemu_opt_set(device, "mount_tag", "v_synth");
3139 break;
3141 case QEMU_OPTION_serial:
3142 add_device_config(DEV_SERIAL, optarg);
3143 default_serial = 0;
3144 if (strncmp(optarg, "mon:", 4) == 0) {
3145 default_monitor = 0;
3147 break;
3148 case QEMU_OPTION_watchdog:
3149 if (watchdog) {
3150 fprintf(stderr,
3151 "qemu: only one watchdog option may be given\n");
3152 return 1;
3154 watchdog = optarg;
3155 break;
3156 case QEMU_OPTION_watchdog_action:
3157 if (select_watchdog_action(optarg) == -1) {
3158 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3159 exit(1);
3161 break;
3162 case QEMU_OPTION_virtiocon:
3163 add_device_config(DEV_VIRTCON, optarg);
3164 default_virtcon = 0;
3165 if (strncmp(optarg, "mon:", 4) == 0) {
3166 default_monitor = 0;
3168 break;
3169 case QEMU_OPTION_parallel:
3170 add_device_config(DEV_PARALLEL, optarg);
3171 default_parallel = 0;
3172 if (strncmp(optarg, "mon:", 4) == 0) {
3173 default_monitor = 0;
3175 break;
3176 case QEMU_OPTION_debugcon:
3177 add_device_config(DEV_DEBUGCON, optarg);
3178 break;
3179 case QEMU_OPTION_loadvm:
3180 loadvm = optarg;
3181 break;
3182 case QEMU_OPTION_full_screen:
3183 full_screen = 1;
3184 break;
3185 #ifdef CONFIG_SDL
3186 case QEMU_OPTION_no_frame:
3187 no_frame = 1;
3188 break;
3189 case QEMU_OPTION_alt_grab:
3190 alt_grab = 1;
3191 break;
3192 case QEMU_OPTION_ctrl_grab:
3193 ctrl_grab = 1;
3194 break;
3195 case QEMU_OPTION_no_quit:
3196 no_quit = 1;
3197 break;
3198 case QEMU_OPTION_sdl:
3199 display_type = DT_SDL;
3200 break;
3201 #else
3202 case QEMU_OPTION_no_frame:
3203 case QEMU_OPTION_alt_grab:
3204 case QEMU_OPTION_ctrl_grab:
3205 case QEMU_OPTION_no_quit:
3206 case QEMU_OPTION_sdl:
3207 fprintf(stderr, "SDL support is disabled\n");
3208 exit(1);
3209 #endif
3210 case QEMU_OPTION_pidfile:
3211 pid_file = optarg;
3212 break;
3213 case QEMU_OPTION_win2k_hack:
3214 win2k_install_hack = 1;
3215 break;
3216 case QEMU_OPTION_rtc_td_hack: {
3217 static GlobalProperty slew_lost_ticks[] = {
3219 .driver = "mc146818rtc",
3220 .property = "lost_tick_policy",
3221 .value = "slew",
3223 { /* end of list */ }
3226 qdev_prop_register_global_list(slew_lost_ticks);
3227 break;
3229 case QEMU_OPTION_acpitable:
3230 do_acpitable_option(optarg);
3231 break;
3232 case QEMU_OPTION_smbios:
3233 do_smbios_option(optarg);
3234 break;
3235 case QEMU_OPTION_enable_kvm:
3236 olist = qemu_find_opts("machine");
3237 qemu_opts_parse(olist, "accel=kvm", 0);
3238 break;
3239 case QEMU_OPTION_machine:
3240 olist = qemu_find_opts("machine");
3241 opts = qemu_opts_parse(olist, optarg, 1);
3242 if (!opts) {
3243 fprintf(stderr, "parse error: %s\n", optarg);
3244 exit(1);
3246 optarg = qemu_opt_get(opts, "type");
3247 if (optarg) {
3248 machine = machine_parse(optarg);
3250 break;
3251 case QEMU_OPTION_no_kvm:
3252 olist = qemu_find_opts("machine");
3253 qemu_opts_parse(olist, "accel=tcg", 0);
3254 break;
3255 case QEMU_OPTION_no_kvm_pit: {
3256 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3257 "separately.\n");
3258 break;
3260 case QEMU_OPTION_no_kvm_pit_reinjection: {
3261 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3263 .driver = "kvm-pit",
3264 .property = "lost_tick_policy",
3265 .value = "discard",
3267 { /* end of list */ }
3270 fprintf(stderr, "Warning: option deprecated, use "
3271 "lost_tick_policy property of kvm-pit instead.\n");
3272 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3273 break;
3275 case QEMU_OPTION_usb:
3276 olist = qemu_find_opts("machine");
3277 qemu_opts_parse(olist, "usb=on", 0);
3278 break;
3279 case QEMU_OPTION_usbdevice:
3280 olist = qemu_find_opts("machine");
3281 qemu_opts_parse(olist, "usb=on", 0);
3282 add_device_config(DEV_USB, optarg);
3283 break;
3284 case QEMU_OPTION_device:
3285 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3286 exit(1);
3288 break;
3289 case QEMU_OPTION_smp:
3290 smp_parse(optarg);
3291 if (smp_cpus < 1) {
3292 fprintf(stderr, "Invalid number of CPUs\n");
3293 exit(1);
3295 if (max_cpus < smp_cpus) {
3296 fprintf(stderr, "maxcpus must be equal to or greater than "
3297 "smp\n");
3298 exit(1);
3300 if (max_cpus > 255) {
3301 fprintf(stderr, "Unsupported number of maxcpus\n");
3302 exit(1);
3304 break;
3305 case QEMU_OPTION_vnc:
3306 #ifdef CONFIG_VNC
3307 display_remote++;
3308 vnc_display = optarg;
3309 #else
3310 fprintf(stderr, "VNC support is disabled\n");
3311 exit(1);
3312 #endif
3313 break;
3314 case QEMU_OPTION_no_acpi:
3315 acpi_enabled = 0;
3316 break;
3317 case QEMU_OPTION_no_hpet:
3318 no_hpet = 1;
3319 break;
3320 case QEMU_OPTION_balloon:
3321 if (balloon_parse(optarg) < 0) {
3322 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3323 exit(1);
3325 break;
3326 case QEMU_OPTION_no_reboot:
3327 no_reboot = 1;
3328 break;
3329 case QEMU_OPTION_no_shutdown:
3330 no_shutdown = 1;
3331 break;
3332 case QEMU_OPTION_show_cursor:
3333 cursor_hide = 0;
3334 break;
3335 case QEMU_OPTION_uuid:
3336 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3337 fprintf(stderr, "Fail to parse UUID string."
3338 " Wrong format.\n");
3339 exit(1);
3341 break;
3342 case QEMU_OPTION_option_rom:
3343 if (nb_option_roms >= MAX_OPTION_ROMS) {
3344 fprintf(stderr, "Too many option ROMs\n");
3345 exit(1);
3347 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3348 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3349 option_rom[nb_option_roms].bootindex =
3350 qemu_opt_get_number(opts, "bootindex", -1);
3351 if (!option_rom[nb_option_roms].name) {
3352 fprintf(stderr, "Option ROM file is not specified\n");
3353 exit(1);
3355 nb_option_roms++;
3356 break;
3357 case QEMU_OPTION_semihosting:
3358 semihosting_enabled = 1;
3359 break;
3360 case QEMU_OPTION_tdf:
3361 fprintf(stderr, "Warning: user space PIT time drift fix "
3362 "is no longer supported.\n");
3363 break;
3364 case QEMU_OPTION_name:
3365 qemu_name = g_strdup(optarg);
3367 char *p = strchr(qemu_name, ',');
3368 if (p != NULL) {
3369 *p++ = 0;
3370 if (strncmp(p, "process=", 8)) {
3371 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3372 exit(1);
3374 p += 8;
3375 os_set_proc_name(p);
3378 break;
3379 case QEMU_OPTION_prom_env:
3380 if (nb_prom_envs >= MAX_PROM_ENVS) {
3381 fprintf(stderr, "Too many prom variables\n");
3382 exit(1);
3384 prom_envs[nb_prom_envs] = optarg;
3385 nb_prom_envs++;
3386 break;
3387 case QEMU_OPTION_old_param:
3388 old_param = 1;
3389 break;
3390 case QEMU_OPTION_clock:
3391 configure_alarms(optarg);
3392 break;
3393 case QEMU_OPTION_startdate:
3394 configure_rtc_date_offset(optarg, 1);
3395 break;
3396 case QEMU_OPTION_rtc:
3397 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3398 if (!opts) {
3399 exit(1);
3401 configure_rtc(opts);
3402 break;
3403 case QEMU_OPTION_tb_size:
3404 tcg_tb_size = strtol(optarg, NULL, 0);
3405 if (tcg_tb_size < 0) {
3406 tcg_tb_size = 0;
3408 break;
3409 case QEMU_OPTION_icount:
3410 icount_option = optarg;
3411 break;
3412 case QEMU_OPTION_incoming:
3413 incoming = optarg;
3414 runstate_set(RUN_STATE_INMIGRATE);
3415 break;
3416 case QEMU_OPTION_nodefaults:
3417 default_serial = 0;
3418 default_parallel = 0;
3419 default_virtcon = 0;
3420 default_monitor = 0;
3421 default_net = 0;
3422 default_floppy = 0;
3423 default_cdrom = 0;
3424 default_sdcard = 0;
3425 default_vga = 0;
3426 break;
3427 case QEMU_OPTION_xen_domid:
3428 if (!(xen_available())) {
3429 printf("Option %s not supported for this target\n", popt->name);
3430 exit(1);
3432 xen_domid = atoi(optarg);
3433 break;
3434 case QEMU_OPTION_xen_create:
3435 if (!(xen_available())) {
3436 printf("Option %s not supported for this target\n", popt->name);
3437 exit(1);
3439 xen_mode = XEN_CREATE;
3440 break;
3441 case QEMU_OPTION_xen_attach:
3442 if (!(xen_available())) {
3443 printf("Option %s not supported for this target\n", popt->name);
3444 exit(1);
3446 xen_mode = XEN_ATTACH;
3447 break;
3448 case QEMU_OPTION_trace:
3450 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3451 if (!opts) {
3452 exit(1);
3454 trace_events = qemu_opt_get(opts, "events");
3455 trace_file = qemu_opt_get(opts, "file");
3456 break;
3458 case QEMU_OPTION_readconfig:
3460 int ret = qemu_read_config_file(optarg);
3461 if (ret < 0) {
3462 fprintf(stderr, "read config %s: %s\n", optarg,
3463 strerror(-ret));
3464 exit(1);
3466 break;
3468 case QEMU_OPTION_spice:
3469 olist = qemu_find_opts("spice");
3470 if (!olist) {
3471 fprintf(stderr, "spice is not supported by this qemu build.\n");
3472 exit(1);
3474 opts = qemu_opts_parse(olist, optarg, 0);
3475 if (!opts) {
3476 fprintf(stderr, "parse error: %s\n", optarg);
3477 exit(1);
3479 break;
3480 case QEMU_OPTION_writeconfig:
3482 FILE *fp;
3483 if (strcmp(optarg, "-") == 0) {
3484 fp = stdout;
3485 } else {
3486 fp = fopen(optarg, "w");
3487 if (fp == NULL) {
3488 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3489 exit(1);
3492 qemu_config_write(fp);
3493 fclose(fp);
3494 break;
3496 case QEMU_OPTION_qtest:
3497 qtest_chrdev = optarg;
3498 break;
3499 case QEMU_OPTION_qtest_log:
3500 qtest_log = optarg;
3501 break;
3502 case QEMU_OPTION_sandbox:
3503 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3504 if (!opts) {
3505 exit(0);
3507 break;
3508 case QEMU_OPTION_add_fd:
3509 #ifndef _WIN32
3510 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3511 if (!opts) {
3512 exit(0);
3514 #else
3515 error_report("File descriptor passing is disabled on this "
3516 "platform");
3517 exit(1);
3518 #endif
3519 break;
3520 case QEMU_OPTION_object:
3521 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3522 break;
3523 default:
3524 os_parse_cmd_args(popt->index, optarg);
3528 loc_set_none();
3530 if (qemu_init_main_loop()) {
3531 fprintf(stderr, "qemu_init_main_loop failed\n");
3532 exit(1);
3535 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3536 exit(1);
3539 #ifndef _WIN32
3540 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3541 exit(1);
3544 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3545 exit(1);
3547 #endif
3549 if (machine == NULL) {
3550 fprintf(stderr, "No machine found.\n");
3551 exit(1);
3554 if (machine->hw_version) {
3555 qemu_set_version(machine->hw_version);
3558 if (qemu_opts_foreach(qemu_find_opts("object"),
3559 object_create, NULL, 0) != 0) {
3560 exit(1);
3563 /* Init CPU def lists, based on config
3564 * - Must be called after all the qemu_read_config_file() calls
3565 * - Must be called before list_cpus()
3566 * - Must be called before machine->init()
3568 cpudef_init();
3570 if (cpu_model && is_help_option(cpu_model)) {
3571 list_cpus(stdout, &fprintf, cpu_model);
3572 exit(0);
3575 /* Open the logfile at this point, if necessary. We can't open the logfile
3576 * when encountering either of the logging options (-d or -D) because the
3577 * other one may be encountered later on the command line, changing the
3578 * location or level of logging.
3580 if (log_mask) {
3581 if (log_file) {
3582 set_cpu_log_filename(log_file);
3584 set_cpu_log(log_mask);
3587 if (!trace_backend_init(trace_events, trace_file)) {
3588 exit(1);
3591 /* If no data_dir is specified then try to find it relative to the
3592 executable path. */
3593 if (!data_dir) {
3594 data_dir = os_find_datadir(argv[0]);
3596 /* If all else fails use the install path specified when building. */
3597 if (!data_dir) {
3598 data_dir = CONFIG_QEMU_DATADIR;
3602 * Default to max_cpus = smp_cpus, in case the user doesn't
3603 * specify a max_cpus value.
3605 if (!max_cpus)
3606 max_cpus = smp_cpus;
3608 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3609 if (smp_cpus > machine->max_cpus) {
3610 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3611 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3612 machine->max_cpus);
3613 exit(1);
3617 * Get the default machine options from the machine if it is not already
3618 * specified either by the configuration file or by the command line.
3620 if (machine->default_machine_opts) {
3621 qemu_opts_set_defaults(qemu_find_opts("machine"),
3622 machine->default_machine_opts, 0);
3625 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3626 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3628 if (machine->no_serial) {
3629 default_serial = 0;
3631 if (machine->no_parallel) {
3632 default_parallel = 0;
3634 if (!machine->use_virtcon) {
3635 default_virtcon = 0;
3637 if (machine->no_floppy) {
3638 default_floppy = 0;
3640 if (machine->no_cdrom) {
3641 default_cdrom = 0;
3643 if (machine->no_sdcard) {
3644 default_sdcard = 0;
3647 if (display_type == DT_NOGRAPHIC) {
3648 if (default_parallel)
3649 add_device_config(DEV_PARALLEL, "null");
3650 if (default_serial && default_monitor) {
3651 add_device_config(DEV_SERIAL, "mon:stdio");
3652 } else if (default_virtcon && default_monitor) {
3653 add_device_config(DEV_VIRTCON, "mon:stdio");
3654 } else {
3655 if (default_serial)
3656 add_device_config(DEV_SERIAL, "stdio");
3657 if (default_virtcon)
3658 add_device_config(DEV_VIRTCON, "stdio");
3659 if (default_monitor)
3660 monitor_parse("stdio", "readline");
3662 } else {
3663 if (default_serial)
3664 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3665 if (default_parallel)
3666 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3667 if (default_monitor)
3668 monitor_parse("vc:80Cx24C", "readline");
3669 if (default_virtcon)
3670 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3673 socket_init();
3675 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3676 exit(1);
3677 #ifdef CONFIG_VIRTFS
3678 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3679 exit(1);
3681 #endif
3683 os_daemonize();
3685 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3686 os_pidfile_error();
3687 exit(1);
3690 /* init the memory */
3691 if (ram_size == 0) {
3692 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3695 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
3696 != 0) {
3697 exit(0);
3700 configure_accelerator();
3702 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3703 if (machine_opts) {
3704 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3705 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3706 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3707 } else {
3708 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3711 if (!kernel_cmdline) {
3712 kernel_cmdline = "";
3715 linux_boot = (kernel_filename != NULL);
3717 if (!linux_boot && *kernel_cmdline != '\0') {
3718 fprintf(stderr, "-append only allowed with -kernel option\n");
3719 exit(1);
3722 if (!linux_boot && initrd_filename != NULL) {
3723 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3724 exit(1);
3727 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3728 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3729 exit(1);
3732 os_set_line_buffering();
3734 qemu_init_cpu_loop();
3735 qemu_mutex_lock_iothread();
3737 #ifdef CONFIG_SPICE
3738 /* spice needs the timers to be initialized by this point */
3739 qemu_spice_init();
3740 #endif
3742 if (icount_option && (kvm_enabled() || xen_enabled())) {
3743 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3744 exit(1);
3746 configure_icount(icount_option);
3748 if (net_init_clients() < 0) {
3749 exit(1);
3752 /* init the bluetooth world */
3753 if (foreach_device_config(DEV_BT, bt_parse))
3754 exit(1);
3756 if (!xen_enabled()) {
3757 /* On 32-bit hosts, QEMU is limited by virtual address space */
3758 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3759 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3760 exit(1);
3764 cpu_exec_init_all();
3766 bdrv_init_with_whitelist();
3768 blk_mig_init();
3770 /* open the virtual block devices */
3771 if (snapshot)
3772 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3773 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3774 exit(1);
3776 default_drive(default_cdrom, snapshot, machine->use_scsi,
3777 IF_DEFAULT, 2, CDROM_OPTS);
3778 default_drive(default_floppy, snapshot, machine->use_scsi,
3779 IF_FLOPPY, 0, FD_OPTS);
3780 default_drive(default_sdcard, snapshot, machine->use_scsi,
3781 IF_SD, 0, SD_OPTS);
3783 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
3785 if (nb_numa_nodes > 0) {
3786 int i;
3788 if (nb_numa_nodes > MAX_NODES) {
3789 nb_numa_nodes = MAX_NODES;
3792 /* If no memory size if given for any node, assume the default case
3793 * and distribute the available memory equally across all nodes
3795 for (i = 0; i < nb_numa_nodes; i++) {
3796 if (node_mem[i] != 0)
3797 break;
3799 if (i == nb_numa_nodes) {
3800 uint64_t usedmem = 0;
3802 /* On Linux, the each node's border has to be 8MB aligned,
3803 * the final node gets the rest.
3805 for (i = 0; i < nb_numa_nodes - 1; i++) {
3806 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3807 usedmem += node_mem[i];
3809 node_mem[i] = ram_size - usedmem;
3812 for (i = 0; i < nb_numa_nodes; i++) {
3813 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
3814 break;
3817 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3818 * must cope with this anyway, because there are BIOSes out there in
3819 * real machines which also use this scheme.
3821 if (i == nb_numa_nodes) {
3822 for (i = 0; i < max_cpus; i++) {
3823 set_bit(i, node_cpumask[i % nb_numa_nodes]);
3828 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3829 exit(1);
3832 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3833 exit(1);
3834 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3835 exit(1);
3836 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3837 exit(1);
3838 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3839 exit(1);
3841 /* If no default VGA is requested, the default is "none". */
3842 if (default_vga) {
3843 if (cirrus_vga_available()) {
3844 vga_model = "cirrus";
3845 } else if (vga_available()) {
3846 vga_model = "std";
3849 select_vgahw(vga_model);
3851 if (watchdog) {
3852 i = select_watchdog(watchdog);
3853 if (i > 0)
3854 exit (i == 1 ? 1 : 0);
3857 if (machine->compat_props) {
3858 qdev_prop_register_global_list(machine->compat_props);
3860 qemu_add_globals();
3862 qdev_machine_init();
3864 QEMUMachineInitArgs args = { .ram_size = ram_size,
3865 .boot_device = boot_devices,
3866 .kernel_filename = kernel_filename,
3867 .kernel_cmdline = kernel_cmdline,
3868 .initrd_filename = initrd_filename,
3869 .cpu_model = cpu_model };
3870 machine->init(&args);
3872 cpu_synchronize_all_post_init();
3874 set_numa_modes();
3876 current_machine = machine;
3878 /* init USB devices */
3879 if (usb_enabled(false)) {
3880 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3881 exit(1);
3884 /* init generic devices */
3885 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3886 exit(1);
3888 net_check_clients();
3890 /* just use the first displaystate for the moment */
3891 ds = get_displaystate();
3893 if (using_spice)
3894 display_remote++;
3895 if (display_type == DT_DEFAULT && !display_remote) {
3896 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3897 display_type = DT_SDL;
3898 #elif defined(CONFIG_VNC)
3899 vnc_display = "localhost:0,to=99";
3900 show_vnc_port = 1;
3901 #else
3902 display_type = DT_NONE;
3903 #endif
3907 /* init local displays */
3908 switch (display_type) {
3909 case DT_NOGRAPHIC:
3910 break;
3911 #if defined(CONFIG_CURSES)
3912 case DT_CURSES:
3913 if (!is_daemonized()) {
3914 curses_display_init(ds, full_screen);
3916 break;
3917 #endif
3918 #if defined(CONFIG_SDL)
3919 case DT_SDL:
3920 sdl_display_init(ds, full_screen, no_frame);
3921 break;
3922 #elif defined(CONFIG_COCOA)
3923 case DT_SDL:
3924 cocoa_display_init(ds, full_screen);
3925 break;
3926 #endif
3927 default:
3928 break;
3931 /* must be after terminal init, SDL library changes signal handlers */
3932 os_setup_signal_handling();
3934 #ifdef CONFIG_VNC
3935 /* init remote displays */
3936 if (vnc_display) {
3937 Error *local_err = NULL;
3938 vnc_display_init(ds);
3939 vnc_display_open(ds, vnc_display, &local_err);
3940 if (local_err != NULL) {
3941 fprintf(stderr, "Failed to start VNC server on `%s': %s\n",
3942 vnc_display, error_get_pretty(local_err));
3943 error_free(local_err);
3944 exit(1);
3947 if (show_vnc_port) {
3948 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3951 #endif
3952 #ifdef CONFIG_SPICE
3953 if (using_spice && !qxl_enabled) {
3954 qemu_spice_display_init(ds);
3956 #endif
3958 /* display setup */
3959 text_consoles_set_display(ds);
3961 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3962 exit(1);
3965 qdev_machine_creation_done();
3967 if (rom_load_all() != 0) {
3968 fprintf(stderr, "rom loading failed\n");
3969 exit(1);
3972 /* TODO: once all bus devices are qdevified, this should be done
3973 * when bus is created by qdev.c */
3974 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3975 qemu_run_machine_init_done_notifiers();
3977 qemu_system_reset(VMRESET_SILENT);
3978 if (loadvm) {
3979 if (load_vmstate(loadvm) < 0) {
3980 autostart = 0;
3984 if (incoming) {
3985 Error *local_err = NULL;
3986 qemu_start_incoming_migration(incoming, &local_err);
3987 if (local_err) {
3988 fprintf(stderr, "-incoming %s: %s\n", incoming, error_get_pretty(local_err));
3989 error_free(local_err);
3990 exit(1);
3992 } else if (autostart) {
3993 vm_start();
3996 os_setup_post();
3998 resume_all_vcpus();
3999 main_loop();
4000 bdrv_close_all();
4001 pause_all_vcpus();
4002 net_cleanup();
4003 res_free();
4005 return 0;