tcg: Optimize qemu_ld/st by generating slow paths at the end of a block
[qemu.git] / vl.c
blob0f5b07bbde67f29097367857dd3c22bda47134fa
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"
172 //#define DEBUG_NET
173 //#define DEBUG_SLIRP
175 #define DEFAULT_RAM_SIZE 128
177 #define MAX_VIRTIO_CONSOLES 1
179 static const char *data_dir;
180 const char *bios_name = NULL;
181 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
182 DisplayType display_type = DT_DEFAULT;
183 int display_remote = 0;
184 const char* keyboard_layout = NULL;
185 ram_addr_t ram_size;
186 const char *mem_path = NULL;
187 #ifdef MAP_POPULATE
188 int mem_prealloc = 0; /* force preallocation of physical target memory */
189 #endif
190 int nb_nics;
191 NICInfo nd_table[MAX_NICS];
192 int autostart;
193 static int rtc_utc = 1;
194 static int rtc_date_offset = -1; /* -1 means no change */
195 QEMUClock *rtc_clock;
196 int vga_interface_type = VGA_NONE;
197 static int full_screen = 0;
198 #ifdef CONFIG_SDL
199 static int no_frame = 0;
200 #endif
201 int no_quit = 0;
202 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
203 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
204 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
205 int win2k_install_hack = 0;
206 int singlestep = 0;
207 int smp_cpus = 1;
208 int max_cpus = 0;
209 int smp_cores = 1;
210 int smp_threads = 1;
211 #ifdef CONFIG_VNC
212 const char *vnc_display;
213 #endif
214 int acpi_enabled = 1;
215 int no_hpet = 0;
216 int fd_bootchk = 1;
217 int no_reboot = 0;
218 int no_shutdown = 0;
219 int cursor_hide = 1;
220 int graphic_rotate = 0;
221 const char *watchdog;
222 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
223 int nb_option_roms;
224 int semihosting_enabled = 0;
225 int old_param = 0;
226 const char *qemu_name;
227 int alt_grab = 0;
228 int ctrl_grab = 0;
229 unsigned int nb_prom_envs = 0;
230 const char *prom_envs[MAX_PROM_ENVS];
231 int boot_menu;
232 uint8_t *boot_splash_filedata;
233 int boot_splash_filedata_size;
234 uint8_t qemu_extra_params_fw[2];
236 typedef struct FWBootEntry FWBootEntry;
238 struct FWBootEntry {
239 QTAILQ_ENTRY(FWBootEntry) link;
240 int32_t bootindex;
241 DeviceState *dev;
242 char *suffix;
245 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
247 int nb_numa_nodes;
248 uint64_t node_mem[MAX_NODES];
249 unsigned long *node_cpumask[MAX_NODES];
251 uint8_t qemu_uuid[16];
253 static QEMUBootSetHandler *boot_set_handler;
254 static void *boot_set_opaque;
256 static NotifierList exit_notifiers =
257 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
259 static NotifierList machine_init_done_notifiers =
260 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
262 static int tcg_allowed = 1;
263 int kvm_allowed = 0;
264 int xen_allowed = 0;
265 uint32_t xen_domid;
266 enum xen_mode xen_mode = XEN_EMULATE;
267 static int tcg_tb_size;
269 static int default_serial = 1;
270 static int default_parallel = 1;
271 static int default_virtcon = 1;
272 static int default_monitor = 1;
273 static int default_floppy = 1;
274 static int default_cdrom = 1;
275 static int default_sdcard = 1;
276 static int default_vga = 1;
278 static struct {
279 const char *driver;
280 int *flag;
281 } default_list[] = {
282 { .driver = "isa-serial", .flag = &default_serial },
283 { .driver = "isa-parallel", .flag = &default_parallel },
284 { .driver = "isa-fdc", .flag = &default_floppy },
285 { .driver = "ide-cd", .flag = &default_cdrom },
286 { .driver = "ide-hd", .flag = &default_cdrom },
287 { .driver = "ide-drive", .flag = &default_cdrom },
288 { .driver = "scsi-cd", .flag = &default_cdrom },
289 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
290 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
291 { .driver = "virtio-serial", .flag = &default_virtcon },
292 { .driver = "VGA", .flag = &default_vga },
293 { .driver = "isa-vga", .flag = &default_vga },
294 { .driver = "cirrus-vga", .flag = &default_vga },
295 { .driver = "isa-cirrus-vga", .flag = &default_vga },
296 { .driver = "vmware-svga", .flag = &default_vga },
297 { .driver = "qxl-vga", .flag = &default_vga },
300 const char *qemu_get_vm_name(void)
302 return qemu_name;
305 static void res_free(void)
307 if (boot_splash_filedata != NULL) {
308 g_free(boot_splash_filedata);
309 boot_splash_filedata = NULL;
313 static int default_driver_check(QemuOpts *opts, void *opaque)
315 const char *driver = qemu_opt_get(opts, "driver");
316 int i;
318 if (!driver)
319 return 0;
320 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
321 if (strcmp(default_list[i].driver, driver) != 0)
322 continue;
323 *(default_list[i].flag) = 0;
325 return 0;
328 /***********************************************************/
329 /* QEMU state */
331 static RunState current_run_state = RUN_STATE_PRELAUNCH;
333 typedef struct {
334 RunState from;
335 RunState to;
336 } RunStateTransition;
338 static const RunStateTransition runstate_transitions_def[] = {
339 /* from -> to */
340 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
342 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
343 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
345 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
346 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
348 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
349 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
351 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
352 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
354 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
355 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
357 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
358 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
359 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
361 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
362 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
364 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
366 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
367 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
368 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
369 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
370 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
371 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
372 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
373 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
374 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
376 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
378 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
379 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
381 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
382 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
383 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
384 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
386 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
387 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
389 { RUN_STATE_MAX, RUN_STATE_MAX },
392 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
394 bool runstate_check(RunState state)
396 return current_run_state == state;
399 void runstate_init(void)
401 const RunStateTransition *p;
403 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
405 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
406 runstate_valid_transitions[p->from][p->to] = true;
410 /* This function will abort() on invalid state transitions */
411 void runstate_set(RunState new_state)
413 assert(new_state < RUN_STATE_MAX);
415 if (!runstate_valid_transitions[current_run_state][new_state]) {
416 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
417 RunState_lookup[current_run_state],
418 RunState_lookup[new_state]);
419 abort();
422 current_run_state = new_state;
425 int runstate_is_running(void)
427 return runstate_check(RUN_STATE_RUNNING);
430 StatusInfo *qmp_query_status(Error **errp)
432 StatusInfo *info = g_malloc0(sizeof(*info));
434 info->running = runstate_is_running();
435 info->singlestep = singlestep;
436 info->status = current_run_state;
438 return info;
441 /***********************************************************/
442 /* real time host monotonic timer */
444 /***********************************************************/
445 /* host time/date access */
446 void qemu_get_timedate(struct tm *tm, int offset)
448 time_t ti;
449 struct tm *ret;
451 time(&ti);
452 ti += offset;
453 if (rtc_date_offset == -1) {
454 if (rtc_utc)
455 ret = gmtime(&ti);
456 else
457 ret = localtime(&ti);
458 } else {
459 ti -= rtc_date_offset;
460 ret = gmtime(&ti);
463 memcpy(tm, ret, sizeof(struct tm));
466 int qemu_timedate_diff(struct tm *tm)
468 time_t seconds;
470 if (rtc_date_offset == -1)
471 if (rtc_utc)
472 seconds = mktimegm(tm);
473 else {
474 struct tm tmp = *tm;
475 tmp.tm_isdst = -1; /* use timezone to figure it out */
476 seconds = mktime(&tmp);
478 else
479 seconds = mktimegm(tm) + rtc_date_offset;
481 return seconds - time(NULL);
484 void rtc_change_mon_event(struct tm *tm)
486 QObject *data;
488 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
489 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
490 qobject_decref(data);
493 static void configure_rtc_date_offset(const char *startdate, int legacy)
495 time_t rtc_start_date;
496 struct tm tm;
498 if (!strcmp(startdate, "now") && legacy) {
499 rtc_date_offset = -1;
500 } else {
501 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
502 &tm.tm_year,
503 &tm.tm_mon,
504 &tm.tm_mday,
505 &tm.tm_hour,
506 &tm.tm_min,
507 &tm.tm_sec) == 6) {
508 /* OK */
509 } else if (sscanf(startdate, "%d-%d-%d",
510 &tm.tm_year,
511 &tm.tm_mon,
512 &tm.tm_mday) == 3) {
513 tm.tm_hour = 0;
514 tm.tm_min = 0;
515 tm.tm_sec = 0;
516 } else {
517 goto date_fail;
519 tm.tm_year -= 1900;
520 tm.tm_mon--;
521 rtc_start_date = mktimegm(&tm);
522 if (rtc_start_date == -1) {
523 date_fail:
524 fprintf(stderr, "Invalid date format. Valid formats are:\n"
525 "'2006-06-17T16:01:21' or '2006-06-17'\n");
526 exit(1);
528 rtc_date_offset = time(NULL) - rtc_start_date;
532 static void configure_rtc(QemuOpts *opts)
534 const char *value;
536 value = qemu_opt_get(opts, "base");
537 if (value) {
538 if (!strcmp(value, "utc")) {
539 rtc_utc = 1;
540 } else if (!strcmp(value, "localtime")) {
541 rtc_utc = 0;
542 } else {
543 configure_rtc_date_offset(value, 0);
546 value = qemu_opt_get(opts, "clock");
547 if (value) {
548 if (!strcmp(value, "host")) {
549 rtc_clock = host_clock;
550 } else if (!strcmp(value, "rt")) {
551 rtc_clock = rt_clock;
552 } else if (!strcmp(value, "vm")) {
553 rtc_clock = vm_clock;
554 } else {
555 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
556 exit(1);
559 value = qemu_opt_get(opts, "driftfix");
560 if (value) {
561 if (!strcmp(value, "slew")) {
562 static GlobalProperty slew_lost_ticks[] = {
564 .driver = "mc146818rtc",
565 .property = "lost_tick_policy",
566 .value = "slew",
568 { /* end of list */ }
571 qdev_prop_register_global_list(slew_lost_ticks);
572 } else if (!strcmp(value, "none")) {
573 /* discard is default */
574 } else {
575 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
576 exit(1);
581 /***********************************************************/
582 /* Bluetooth support */
583 static int nb_hcis;
584 static int cur_hci;
585 static struct HCIInfo *hci_table[MAX_NICS];
587 static struct bt_vlan_s {
588 struct bt_scatternet_s net;
589 int id;
590 struct bt_vlan_s *next;
591 } *first_bt_vlan;
593 /* find or alloc a new bluetooth "VLAN" */
594 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
596 struct bt_vlan_s **pvlan, *vlan;
597 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
598 if (vlan->id == id)
599 return &vlan->net;
601 vlan = g_malloc0(sizeof(struct bt_vlan_s));
602 vlan->id = id;
603 pvlan = &first_bt_vlan;
604 while (*pvlan != NULL)
605 pvlan = &(*pvlan)->next;
606 *pvlan = vlan;
607 return &vlan->net;
610 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
614 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
616 return -ENOTSUP;
619 static struct HCIInfo null_hci = {
620 .cmd_send = null_hci_send,
621 .sco_send = null_hci_send,
622 .acl_send = null_hci_send,
623 .bdaddr_set = null_hci_addr_set,
626 struct HCIInfo *qemu_next_hci(void)
628 if (cur_hci == nb_hcis)
629 return &null_hci;
631 return hci_table[cur_hci++];
634 static struct HCIInfo *hci_init(const char *str)
636 char *endp;
637 struct bt_scatternet_s *vlan = 0;
639 if (!strcmp(str, "null"))
640 /* null */
641 return &null_hci;
642 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
643 /* host[:hciN] */
644 return bt_host_hci(str[4] ? str + 5 : "hci0");
645 else if (!strncmp(str, "hci", 3)) {
646 /* hci[,vlan=n] */
647 if (str[3]) {
648 if (!strncmp(str + 3, ",vlan=", 6)) {
649 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
650 if (*endp)
651 vlan = 0;
653 } else
654 vlan = qemu_find_bt_vlan(0);
655 if (vlan)
656 return bt_new_hci(vlan);
659 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
661 return 0;
664 static int bt_hci_parse(const char *str)
666 struct HCIInfo *hci;
667 bdaddr_t bdaddr;
669 if (nb_hcis >= MAX_NICS) {
670 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
671 return -1;
674 hci = hci_init(str);
675 if (!hci)
676 return -1;
678 bdaddr.b[0] = 0x52;
679 bdaddr.b[1] = 0x54;
680 bdaddr.b[2] = 0x00;
681 bdaddr.b[3] = 0x12;
682 bdaddr.b[4] = 0x34;
683 bdaddr.b[5] = 0x56 + nb_hcis;
684 hci->bdaddr_set(hci, bdaddr.b);
686 hci_table[nb_hcis++] = hci;
688 return 0;
691 static void bt_vhci_add(int vlan_id)
693 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
695 if (!vlan->slave)
696 fprintf(stderr, "qemu: warning: adding a VHCI to "
697 "an empty scatternet %i\n", vlan_id);
699 bt_vhci_init(bt_new_hci(vlan));
702 static struct bt_device_s *bt_device_add(const char *opt)
704 struct bt_scatternet_s *vlan;
705 int vlan_id = 0;
706 char *endp = strstr(opt, ",vlan=");
707 int len = (endp ? endp - opt : strlen(opt)) + 1;
708 char devname[10];
710 pstrcpy(devname, MIN(sizeof(devname), len), opt);
712 if (endp) {
713 vlan_id = strtol(endp + 6, &endp, 0);
714 if (*endp) {
715 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
716 return 0;
720 vlan = qemu_find_bt_vlan(vlan_id);
722 if (!vlan->slave)
723 fprintf(stderr, "qemu: warning: adding a slave device to "
724 "an empty scatternet %i\n", vlan_id);
726 if (!strcmp(devname, "keyboard"))
727 return bt_keyboard_init(vlan);
729 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
730 return 0;
733 static int bt_parse(const char *opt)
735 const char *endp, *p;
736 int vlan;
738 if (strstart(opt, "hci", &endp)) {
739 if (!*endp || *endp == ',') {
740 if (*endp)
741 if (!strstart(endp, ",vlan=", 0))
742 opt = endp + 1;
744 return bt_hci_parse(opt);
746 } else if (strstart(opt, "vhci", &endp)) {
747 if (!*endp || *endp == ',') {
748 if (*endp) {
749 if (strstart(endp, ",vlan=", &p)) {
750 vlan = strtol(p, (char **) &endp, 0);
751 if (*endp) {
752 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
753 return 1;
755 } else {
756 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
757 return 1;
759 } else
760 vlan = 0;
762 bt_vhci_add(vlan);
763 return 0;
765 } else if (strstart(opt, "device:", &endp))
766 return !bt_device_add(endp);
768 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
769 return 1;
772 static int parse_sandbox(QemuOpts *opts, void *opaque)
774 /* FIXME: change this to true for 1.3 */
775 if (qemu_opt_get_bool(opts, "enable", false)) {
776 #ifdef CONFIG_SECCOMP
777 if (seccomp_start() < 0) {
778 qerror_report(ERROR_CLASS_GENERIC_ERROR,
779 "failed to install seccomp syscall filter in the kernel");
780 return -1;
782 #else
783 qerror_report(ERROR_CLASS_GENERIC_ERROR,
784 "sandboxing request but seccomp is not compiled into this build");
785 return -1;
786 #endif
789 return 0;
792 /*********QEMU USB setting******/
793 bool usb_enabled(bool default_usb)
795 QemuOpts *mach_opts;
796 mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
797 if (mach_opts) {
798 return qemu_opt_get_bool(mach_opts, "usb", default_usb);
800 return default_usb;
803 #ifndef _WIN32
804 static int parse_add_fd(QemuOpts *opts, void *opaque)
806 int fd, dupfd, flags;
807 int64_t fdset_id;
808 const char *fd_opaque = NULL;
810 fd = qemu_opt_get_number(opts, "fd", -1);
811 fdset_id = qemu_opt_get_number(opts, "set", -1);
812 fd_opaque = qemu_opt_get(opts, "opaque");
814 if (fd < 0) {
815 qerror_report(ERROR_CLASS_GENERIC_ERROR,
816 "fd option is required and must be non-negative");
817 return -1;
820 if (fd <= STDERR_FILENO) {
821 qerror_report(ERROR_CLASS_GENERIC_ERROR,
822 "fd cannot be a standard I/O stream");
823 return -1;
827 * All fds inherited across exec() necessarily have FD_CLOEXEC
828 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
830 flags = fcntl(fd, F_GETFD);
831 if (flags == -1 || (flags & FD_CLOEXEC)) {
832 qerror_report(ERROR_CLASS_GENERIC_ERROR,
833 "fd is not valid or already in use");
834 return -1;
837 if (fdset_id < 0) {
838 qerror_report(ERROR_CLASS_GENERIC_ERROR,
839 "set option is required and must be non-negative");
840 return -1;
843 #ifdef F_DUPFD_CLOEXEC
844 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
845 #else
846 dupfd = dup(fd);
847 if (dupfd != -1) {
848 qemu_set_cloexec(dupfd);
850 #endif
851 if (dupfd == -1) {
852 qerror_report(ERROR_CLASS_GENERIC_ERROR,
853 "Error duplicating fd: %s", strerror(errno));
854 return -1;
857 /* add the duplicate fd, and optionally the opaque string, to the fd set */
858 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
859 fd_opaque, NULL);
861 return 0;
864 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
866 int fd;
868 fd = qemu_opt_get_number(opts, "fd", -1);
869 close(fd);
871 return 0;
873 #endif
875 /***********************************************************/
876 /* QEMU Block devices */
878 #define HD_OPTS "media=disk"
879 #define CDROM_OPTS "media=cdrom"
880 #define FD_OPTS ""
881 #define PFLASH_OPTS ""
882 #define MTD_OPTS ""
883 #define SD_OPTS ""
885 static int drive_init_func(QemuOpts *opts, void *opaque)
887 int *use_scsi = opaque;
889 return drive_init(opts, *use_scsi) == NULL;
892 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
894 if (NULL == qemu_opt_get(opts, "snapshot")) {
895 qemu_opt_set(opts, "snapshot", "on");
897 return 0;
900 static void default_drive(int enable, int snapshot, int use_scsi,
901 BlockInterfaceType type, int index,
902 const char *optstr)
904 QemuOpts *opts;
906 if (type == IF_DEFAULT) {
907 type = use_scsi ? IF_SCSI : IF_IDE;
910 if (!enable || drive_get_by_index(type, index)) {
911 return;
914 opts = drive_add(type, index, NULL, optstr);
915 if (snapshot) {
916 drive_enable_snapshot(opts, NULL);
918 if (!drive_init(opts, use_scsi)) {
919 exit(1);
923 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
925 boot_set_handler = func;
926 boot_set_opaque = opaque;
929 int qemu_boot_set(const char *boot_devices)
931 if (!boot_set_handler) {
932 return -EINVAL;
934 return boot_set_handler(boot_set_opaque, boot_devices);
937 static void validate_bootdevices(char *devices)
939 /* We just do some generic consistency checks */
940 const char *p;
941 int bitmap = 0;
943 for (p = devices; *p != '\0'; p++) {
944 /* Allowed boot devices are:
945 * a-b: floppy disk drives
946 * c-f: IDE disk drives
947 * g-m: machine implementation dependent drives
948 * n-p: network devices
949 * It's up to each machine implementation to check if the given boot
950 * devices match the actual hardware implementation and firmware
951 * features.
953 if (*p < 'a' || *p > 'p') {
954 fprintf(stderr, "Invalid boot device '%c'\n", *p);
955 exit(1);
957 if (bitmap & (1 << (*p - 'a'))) {
958 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
959 exit(1);
961 bitmap |= 1 << (*p - 'a');
965 static void restore_boot_devices(void *opaque)
967 char *standard_boot_devices = opaque;
968 static int first = 1;
970 /* Restore boot order and remove ourselves after the first boot */
971 if (first) {
972 first = 0;
973 return;
976 qemu_boot_set(standard_boot_devices);
978 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
979 g_free(standard_boot_devices);
982 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
983 const char *suffix)
985 FWBootEntry *node, *i;
987 if (bootindex < 0) {
988 return;
991 assert(dev != NULL || suffix != NULL);
993 node = g_malloc0(sizeof(FWBootEntry));
994 node->bootindex = bootindex;
995 node->suffix = suffix ? g_strdup(suffix) : NULL;
996 node->dev = dev;
998 QTAILQ_FOREACH(i, &fw_boot_order, link) {
999 if (i->bootindex == bootindex) {
1000 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1001 exit(1);
1002 } else if (i->bootindex < bootindex) {
1003 continue;
1005 QTAILQ_INSERT_BEFORE(i, node, link);
1006 return;
1008 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1012 * This function returns null terminated string that consist of new line
1013 * separated device paths.
1015 * memory pointed by "size" is assigned total length of the array in bytes
1018 char *get_boot_devices_list(uint32_t *size)
1020 FWBootEntry *i;
1021 uint32_t total = 0;
1022 char *list = NULL;
1024 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1025 char *devpath = NULL, *bootpath;
1026 int len;
1028 if (i->dev) {
1029 devpath = qdev_get_fw_dev_path(i->dev);
1030 assert(devpath);
1033 if (i->suffix && devpath) {
1034 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1036 bootpath = g_malloc(bootpathlen);
1037 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1038 g_free(devpath);
1039 } else if (devpath) {
1040 bootpath = devpath;
1041 } else {
1042 assert(i->suffix);
1043 bootpath = g_strdup(i->suffix);
1046 if (total) {
1047 list[total-1] = '\n';
1049 len = strlen(bootpath) + 1;
1050 list = g_realloc(list, total + len);
1051 memcpy(&list[total], bootpath, len);
1052 total += len;
1053 g_free(bootpath);
1056 *size = total;
1058 return list;
1061 static void numa_add(const char *optarg)
1063 char option[128];
1064 char *endptr;
1065 unsigned long long value, endvalue;
1066 int nodenr;
1068 value = endvalue = 0ULL;
1070 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1071 if (!strcmp(option, "node")) {
1072 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1073 nodenr = nb_numa_nodes;
1074 } else {
1075 nodenr = strtoull(option, NULL, 10);
1078 if (get_param_value(option, 128, "mem", optarg) == 0) {
1079 node_mem[nodenr] = 0;
1080 } else {
1081 int64_t sval;
1082 sval = strtosz(option, &endptr);
1083 if (sval < 0 || *endptr) {
1084 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1085 exit(1);
1087 node_mem[nodenr] = sval;
1089 if (get_param_value(option, 128, "cpus", optarg) != 0) {
1090 value = strtoull(option, &endptr, 10);
1091 if (*endptr == '-') {
1092 endvalue = strtoull(endptr+1, &endptr, 10);
1093 } else {
1094 endvalue = value;
1097 if (!(endvalue < MAX_CPUMASK_BITS)) {
1098 endvalue = MAX_CPUMASK_BITS - 1;
1099 fprintf(stderr,
1100 "A max of %d CPUs are supported in a guest\n",
1101 MAX_CPUMASK_BITS);
1104 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
1106 nb_numa_nodes++;
1110 static void smp_parse(const char *optarg)
1112 int smp, sockets = 0, threads = 0, cores = 0;
1113 char *endptr;
1114 char option[128];
1116 smp = strtoul(optarg, &endptr, 10);
1117 if (endptr != optarg) {
1118 if (*endptr == ',') {
1119 endptr++;
1122 if (get_param_value(option, 128, "sockets", endptr) != 0)
1123 sockets = strtoull(option, NULL, 10);
1124 if (get_param_value(option, 128, "cores", endptr) != 0)
1125 cores = strtoull(option, NULL, 10);
1126 if (get_param_value(option, 128, "threads", endptr) != 0)
1127 threads = strtoull(option, NULL, 10);
1128 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1129 max_cpus = strtoull(option, NULL, 10);
1131 /* compute missing values, prefer sockets over cores over threads */
1132 if (smp == 0 || sockets == 0) {
1133 sockets = sockets > 0 ? sockets : 1;
1134 cores = cores > 0 ? cores : 1;
1135 threads = threads > 0 ? threads : 1;
1136 if (smp == 0) {
1137 smp = cores * threads * sockets;
1139 } else {
1140 if (cores == 0) {
1141 threads = threads > 0 ? threads : 1;
1142 cores = smp / (sockets * threads);
1143 } else {
1144 threads = smp / (cores * sockets);
1147 smp_cpus = smp;
1148 smp_cores = cores > 0 ? cores : 1;
1149 smp_threads = threads > 0 ? threads : 1;
1150 if (max_cpus == 0)
1151 max_cpus = smp_cpus;
1154 /***********************************************************/
1155 /* USB devices */
1157 static int usb_device_add(const char *devname)
1159 const char *p;
1160 USBDevice *dev = NULL;
1162 if (!usb_enabled(false)) {
1163 return -1;
1166 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1167 dev = usbdevice_create(devname);
1168 if (dev)
1169 goto done;
1171 /* the other ones */
1172 #ifndef CONFIG_LINUX
1173 /* only the linux version is qdev-ified, usb-bsd still needs this */
1174 if (strstart(devname, "host:", &p)) {
1175 dev = usb_host_device_open(usb_bus_find(-1), p);
1176 } else
1177 #endif
1178 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1179 dev = usb_bt_init(usb_bus_find(-1),
1180 devname[2] ? hci_init(p)
1181 : bt_new_hci(qemu_find_bt_vlan(0)));
1182 } else {
1183 return -1;
1185 if (!dev)
1186 return -1;
1188 done:
1189 return 0;
1192 static int usb_device_del(const char *devname)
1194 int bus_num, addr;
1195 const char *p;
1197 if (strstart(devname, "host:", &p))
1198 return usb_host_device_close(p);
1200 if (!usb_enabled(false)) {
1201 return -1;
1204 p = strchr(devname, '.');
1205 if (!p)
1206 return -1;
1207 bus_num = strtoul(devname, NULL, 0);
1208 addr = strtoul(p + 1, NULL, 0);
1210 return usb_device_delete_addr(bus_num, addr);
1213 static int usb_parse(const char *cmdline)
1215 int r;
1216 r = usb_device_add(cmdline);
1217 if (r < 0) {
1218 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1220 return r;
1223 void do_usb_add(Monitor *mon, const QDict *qdict)
1225 const char *devname = qdict_get_str(qdict, "devname");
1226 if (usb_device_add(devname) < 0) {
1227 error_report("could not add USB device '%s'", devname);
1231 void do_usb_del(Monitor *mon, const QDict *qdict)
1233 const char *devname = qdict_get_str(qdict, "devname");
1234 if (usb_device_del(devname) < 0) {
1235 error_report("could not delete USB device '%s'", devname);
1239 /***********************************************************/
1240 /* PCMCIA/Cardbus */
1242 static struct pcmcia_socket_entry_s {
1243 PCMCIASocket *socket;
1244 struct pcmcia_socket_entry_s *next;
1245 } *pcmcia_sockets = 0;
1247 void pcmcia_socket_register(PCMCIASocket *socket)
1249 struct pcmcia_socket_entry_s *entry;
1251 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1252 entry->socket = socket;
1253 entry->next = pcmcia_sockets;
1254 pcmcia_sockets = entry;
1257 void pcmcia_socket_unregister(PCMCIASocket *socket)
1259 struct pcmcia_socket_entry_s *entry, **ptr;
1261 ptr = &pcmcia_sockets;
1262 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1263 if (entry->socket == socket) {
1264 *ptr = entry->next;
1265 g_free(entry);
1269 void pcmcia_info(Monitor *mon)
1271 struct pcmcia_socket_entry_s *iter;
1273 if (!pcmcia_sockets)
1274 monitor_printf(mon, "No PCMCIA sockets\n");
1276 for (iter = pcmcia_sockets; iter; iter = iter->next)
1277 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1278 iter->socket->attached ? iter->socket->card_string :
1279 "Empty");
1282 /***********************************************************/
1283 /* machine registration */
1285 static QEMUMachine *first_machine = NULL;
1286 QEMUMachine *current_machine = NULL;
1288 int qemu_register_machine(QEMUMachine *m)
1290 QEMUMachine **pm;
1291 pm = &first_machine;
1292 while (*pm != NULL)
1293 pm = &(*pm)->next;
1294 m->next = NULL;
1295 *pm = m;
1296 return 0;
1299 static QEMUMachine *find_machine(const char *name)
1301 QEMUMachine *m;
1303 for(m = first_machine; m != NULL; m = m->next) {
1304 if (!strcmp(m->name, name))
1305 return m;
1306 if (m->alias && !strcmp(m->alias, name))
1307 return m;
1309 return NULL;
1312 QEMUMachine *find_default_machine(void)
1314 QEMUMachine *m;
1316 for(m = first_machine; m != NULL; m = m->next) {
1317 if (m->is_default) {
1318 return m;
1321 return NULL;
1324 MachineInfoList *qmp_query_machines(Error **errp)
1326 MachineInfoList *mach_list = NULL;
1327 QEMUMachine *m;
1329 for (m = first_machine; m; m = m->next) {
1330 MachineInfoList *entry;
1331 MachineInfo *info;
1333 info = g_malloc0(sizeof(*info));
1334 if (m->is_default) {
1335 info->has_is_default = true;
1336 info->is_default = true;
1339 if (m->alias) {
1340 info->has_alias = true;
1341 info->alias = g_strdup(m->alias);
1344 info->name = g_strdup(m->name);
1346 entry = g_malloc0(sizeof(*entry));
1347 entry->value = info;
1348 entry->next = mach_list;
1349 mach_list = entry;
1352 return mach_list;
1355 /***********************************************************/
1356 /* main execution loop */
1358 static void gui_update(void *opaque)
1360 uint64_t interval = GUI_REFRESH_INTERVAL;
1361 DisplayState *ds = opaque;
1362 DisplayChangeListener *dcl;
1364 dpy_refresh(ds);
1366 QLIST_FOREACH(dcl, &ds->listeners, next) {
1367 if (dcl->gui_timer_interval &&
1368 dcl->gui_timer_interval < interval)
1369 interval = dcl->gui_timer_interval;
1371 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1374 void gui_setup_refresh(DisplayState *ds)
1376 DisplayChangeListener *dcl;
1377 bool need_timer = false;
1378 bool have_gfx = false;
1379 bool have_text = false;
1381 QLIST_FOREACH(dcl, &ds->listeners, next) {
1382 if (dcl->dpy_refresh != NULL) {
1383 need_timer = true;
1385 if (dcl->dpy_gfx_update != NULL) {
1386 have_gfx = true;
1388 if (dcl->dpy_text_update != NULL) {
1389 have_text = true;
1393 if (need_timer && ds->gui_timer == NULL) {
1394 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
1395 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
1397 if (!need_timer && ds->gui_timer != NULL) {
1398 qemu_del_timer(ds->gui_timer);
1399 qemu_free_timer(ds->gui_timer);
1400 ds->gui_timer = NULL;
1403 ds->have_gfx = have_gfx;
1404 ds->have_text = have_text;
1407 struct vm_change_state_entry {
1408 VMChangeStateHandler *cb;
1409 void *opaque;
1410 QLIST_ENTRY (vm_change_state_entry) entries;
1413 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1415 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1416 void *opaque)
1418 VMChangeStateEntry *e;
1420 e = g_malloc0(sizeof (*e));
1422 e->cb = cb;
1423 e->opaque = opaque;
1424 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1425 return e;
1428 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1430 QLIST_REMOVE (e, entries);
1431 g_free (e);
1434 void vm_state_notify(int running, RunState state)
1436 VMChangeStateEntry *e;
1438 trace_vm_state_notify(running, state);
1440 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1441 e->cb(e->opaque, running, state);
1445 void vm_start(void)
1447 if (!runstate_is_running()) {
1448 cpu_enable_ticks();
1449 runstate_set(RUN_STATE_RUNNING);
1450 vm_state_notify(1, RUN_STATE_RUNNING);
1451 resume_all_vcpus();
1452 monitor_protocol_event(QEVENT_RESUME, NULL);
1456 /* reset/shutdown handler */
1458 typedef struct QEMUResetEntry {
1459 QTAILQ_ENTRY(QEMUResetEntry) entry;
1460 QEMUResetHandler *func;
1461 void *opaque;
1462 } QEMUResetEntry;
1464 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1465 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1466 static int reset_requested;
1467 static int shutdown_requested, shutdown_signal = -1;
1468 static pid_t shutdown_pid;
1469 static int powerdown_requested;
1470 static int debug_requested;
1471 static int suspend_requested;
1472 static int wakeup_requested;
1473 static NotifierList powerdown_notifiers =
1474 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1475 static NotifierList suspend_notifiers =
1476 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1477 static NotifierList wakeup_notifiers =
1478 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1479 static uint32_t wakeup_reason_mask = ~0;
1480 static RunState vmstop_requested = RUN_STATE_MAX;
1482 int qemu_shutdown_requested_get(void)
1484 return shutdown_requested;
1487 int qemu_reset_requested_get(void)
1489 return reset_requested;
1492 int qemu_shutdown_requested(void)
1494 int r = shutdown_requested;
1495 shutdown_requested = 0;
1496 return r;
1499 void qemu_kill_report(void)
1501 if (!qtest_enabled() && shutdown_signal != -1) {
1502 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1503 if (shutdown_pid == 0) {
1504 /* This happens for eg ^C at the terminal, so it's worth
1505 * avoiding printing an odd message in that case.
1507 fputc('\n', stderr);
1508 } else {
1509 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1511 shutdown_signal = -1;
1515 int qemu_reset_requested(void)
1517 int r = reset_requested;
1518 reset_requested = 0;
1519 return r;
1522 static int qemu_suspend_requested(void)
1524 int r = suspend_requested;
1525 suspend_requested = 0;
1526 return r;
1529 static int qemu_wakeup_requested(void)
1531 int r = wakeup_requested;
1532 wakeup_requested = 0;
1533 return r;
1536 int qemu_powerdown_requested(void)
1538 int r = powerdown_requested;
1539 powerdown_requested = 0;
1540 return r;
1543 static int qemu_debug_requested(void)
1545 int r = debug_requested;
1546 debug_requested = 0;
1547 return r;
1550 /* We use RUN_STATE_MAX but any invalid value will do */
1551 static bool qemu_vmstop_requested(RunState *r)
1553 if (vmstop_requested < RUN_STATE_MAX) {
1554 *r = vmstop_requested;
1555 vmstop_requested = RUN_STATE_MAX;
1556 return true;
1559 return false;
1562 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1564 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1566 re->func = func;
1567 re->opaque = opaque;
1568 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1571 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1573 QEMUResetEntry *re;
1575 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1576 if (re->func == func && re->opaque == opaque) {
1577 QTAILQ_REMOVE(&reset_handlers, re, entry);
1578 g_free(re);
1579 return;
1584 void qemu_devices_reset(void)
1586 QEMUResetEntry *re, *nre;
1588 /* reset all devices */
1589 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1590 re->func(re->opaque);
1594 void qemu_system_reset(bool report)
1596 if (current_machine && current_machine->reset) {
1597 current_machine->reset();
1598 } else {
1599 qemu_devices_reset();
1601 if (report) {
1602 monitor_protocol_event(QEVENT_RESET, NULL);
1604 cpu_synchronize_all_post_reset();
1607 void qemu_system_reset_request(void)
1609 if (no_reboot) {
1610 shutdown_requested = 1;
1611 } else {
1612 reset_requested = 1;
1614 cpu_stop_current();
1615 qemu_notify_event();
1618 static void qemu_system_suspend(void)
1620 pause_all_vcpus();
1621 notifier_list_notify(&suspend_notifiers, NULL);
1622 runstate_set(RUN_STATE_SUSPENDED);
1623 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1626 void qemu_system_suspend_request(void)
1628 if (runstate_check(RUN_STATE_SUSPENDED)) {
1629 return;
1631 suspend_requested = 1;
1632 cpu_stop_current();
1633 qemu_notify_event();
1636 void qemu_register_suspend_notifier(Notifier *notifier)
1638 notifier_list_add(&suspend_notifiers, notifier);
1641 void qemu_system_wakeup_request(WakeupReason reason)
1643 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1644 return;
1646 if (!(wakeup_reason_mask & (1 << reason))) {
1647 return;
1649 runstate_set(RUN_STATE_RUNNING);
1650 notifier_list_notify(&wakeup_notifiers, &reason);
1651 wakeup_requested = 1;
1652 qemu_notify_event();
1655 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1657 if (enabled) {
1658 wakeup_reason_mask |= (1 << reason);
1659 } else {
1660 wakeup_reason_mask &= ~(1 << reason);
1664 void qemu_register_wakeup_notifier(Notifier *notifier)
1666 notifier_list_add(&wakeup_notifiers, notifier);
1669 void qemu_system_killed(int signal, pid_t pid)
1671 shutdown_signal = signal;
1672 shutdown_pid = pid;
1673 no_shutdown = 0;
1674 qemu_system_shutdown_request();
1677 void qemu_system_shutdown_request(void)
1679 shutdown_requested = 1;
1680 qemu_notify_event();
1683 static void qemu_system_powerdown(void)
1685 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1686 notifier_list_notify(&powerdown_notifiers, NULL);
1689 void qemu_system_powerdown_request(void)
1691 powerdown_requested = 1;
1692 qemu_notify_event();
1695 void qemu_register_powerdown_notifier(Notifier *notifier)
1697 notifier_list_add(&powerdown_notifiers, notifier);
1700 void qemu_system_debug_request(void)
1702 debug_requested = 1;
1703 qemu_notify_event();
1706 void qemu_system_vmstop_request(RunState state)
1708 vmstop_requested = state;
1709 qemu_notify_event();
1712 static bool main_loop_should_exit(void)
1714 RunState r;
1715 if (qemu_debug_requested()) {
1716 vm_stop(RUN_STATE_DEBUG);
1718 if (qemu_suspend_requested()) {
1719 qemu_system_suspend();
1721 if (qemu_shutdown_requested()) {
1722 qemu_kill_report();
1723 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1724 if (no_shutdown) {
1725 vm_stop(RUN_STATE_SHUTDOWN);
1726 } else {
1727 return true;
1730 if (qemu_reset_requested()) {
1731 pause_all_vcpus();
1732 cpu_synchronize_all_states();
1733 qemu_system_reset(VMRESET_REPORT);
1734 resume_all_vcpus();
1735 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1736 runstate_check(RUN_STATE_SHUTDOWN)) {
1737 runstate_set(RUN_STATE_PAUSED);
1740 if (qemu_wakeup_requested()) {
1741 pause_all_vcpus();
1742 cpu_synchronize_all_states();
1743 qemu_system_reset(VMRESET_SILENT);
1744 resume_all_vcpus();
1745 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1747 if (qemu_powerdown_requested()) {
1748 qemu_system_powerdown();
1750 if (qemu_vmstop_requested(&r)) {
1751 vm_stop(r);
1753 return false;
1756 static void main_loop(void)
1758 bool nonblocking;
1759 int last_io = 0;
1760 #ifdef CONFIG_PROFILER
1761 int64_t ti;
1762 #endif
1763 do {
1764 nonblocking = !kvm_enabled() && last_io > 0;
1765 #ifdef CONFIG_PROFILER
1766 ti = profile_getclock();
1767 #endif
1768 last_io = main_loop_wait(nonblocking);
1769 #ifdef CONFIG_PROFILER
1770 dev_time += profile_getclock() - ti;
1771 #endif
1772 } while (!main_loop_should_exit());
1775 static void version(void)
1777 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1780 static void help(int exitcode)
1782 version();
1783 printf("usage: %s [options] [disk_image]\n\n"
1784 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1785 error_get_progname());
1787 #define QEMU_OPTIONS_GENERATE_HELP
1788 #include "qemu-options-wrapper.h"
1790 printf("\nDuring emulation, the following keys are useful:\n"
1791 "ctrl-alt-f toggle full screen\n"
1792 "ctrl-alt-n switch to virtual console 'n'\n"
1793 "ctrl-alt toggle mouse and keyboard grab\n"
1794 "\n"
1795 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1797 exit(exitcode);
1800 #define HAS_ARG 0x0001
1802 typedef struct QEMUOption {
1803 const char *name;
1804 int flags;
1805 int index;
1806 uint32_t arch_mask;
1807 } QEMUOption;
1809 static const QEMUOption qemu_options[] = {
1810 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1811 #define QEMU_OPTIONS_GENERATE_OPTIONS
1812 #include "qemu-options-wrapper.h"
1813 { NULL },
1816 static bool vga_available(void)
1818 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1821 static bool cirrus_vga_available(void)
1823 return object_class_by_name("cirrus-vga")
1824 || object_class_by_name("isa-cirrus-vga");
1827 static bool vmware_vga_available(void)
1829 return object_class_by_name("vmware-svga");
1832 static bool qxl_vga_available(void)
1834 return object_class_by_name("qxl-vga");
1837 static void select_vgahw (const char *p)
1839 const char *opts;
1841 vga_interface_type = VGA_NONE;
1842 if (strstart(p, "std", &opts)) {
1843 if (vga_available()) {
1844 vga_interface_type = VGA_STD;
1845 } else {
1846 fprintf(stderr, "Error: standard VGA not available\n");
1847 exit(0);
1849 } else if (strstart(p, "cirrus", &opts)) {
1850 if (cirrus_vga_available()) {
1851 vga_interface_type = VGA_CIRRUS;
1852 } else {
1853 fprintf(stderr, "Error: Cirrus VGA not available\n");
1854 exit(0);
1856 } else if (strstart(p, "vmware", &opts)) {
1857 if (vmware_vga_available()) {
1858 vga_interface_type = VGA_VMWARE;
1859 } else {
1860 fprintf(stderr, "Error: VMWare SVGA not available\n");
1861 exit(0);
1863 } else if (strstart(p, "xenfb", &opts)) {
1864 vga_interface_type = VGA_XENFB;
1865 } else if (strstart(p, "qxl", &opts)) {
1866 if (qxl_vga_available()) {
1867 vga_interface_type = VGA_QXL;
1868 } else {
1869 fprintf(stderr, "Error: QXL VGA not available\n");
1870 exit(0);
1872 } else if (!strstart(p, "none", &opts)) {
1873 invalid_vga:
1874 fprintf(stderr, "Unknown vga type: %s\n", p);
1875 exit(1);
1877 while (*opts) {
1878 const char *nextopt;
1880 if (strstart(opts, ",retrace=", &nextopt)) {
1881 opts = nextopt;
1882 if (strstart(opts, "dumb", &nextopt))
1883 vga_retrace_method = VGA_RETRACE_DUMB;
1884 else if (strstart(opts, "precise", &nextopt))
1885 vga_retrace_method = VGA_RETRACE_PRECISE;
1886 else goto invalid_vga;
1887 } else goto invalid_vga;
1888 opts = nextopt;
1892 static DisplayType select_display(const char *p)
1894 const char *opts;
1895 DisplayType display = DT_DEFAULT;
1897 if (strstart(p, "sdl", &opts)) {
1898 #ifdef CONFIG_SDL
1899 display = DT_SDL;
1900 while (*opts) {
1901 const char *nextopt;
1903 if (strstart(opts, ",frame=", &nextopt)) {
1904 opts = nextopt;
1905 if (strstart(opts, "on", &nextopt)) {
1906 no_frame = 0;
1907 } else if (strstart(opts, "off", &nextopt)) {
1908 no_frame = 1;
1909 } else {
1910 goto invalid_sdl_args;
1912 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1913 opts = nextopt;
1914 if (strstart(opts, "on", &nextopt)) {
1915 alt_grab = 1;
1916 } else if (strstart(opts, "off", &nextopt)) {
1917 alt_grab = 0;
1918 } else {
1919 goto invalid_sdl_args;
1921 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1922 opts = nextopt;
1923 if (strstart(opts, "on", &nextopt)) {
1924 ctrl_grab = 1;
1925 } else if (strstart(opts, "off", &nextopt)) {
1926 ctrl_grab = 0;
1927 } else {
1928 goto invalid_sdl_args;
1930 } else if (strstart(opts, ",window_close=", &nextopt)) {
1931 opts = nextopt;
1932 if (strstart(opts, "on", &nextopt)) {
1933 no_quit = 0;
1934 } else if (strstart(opts, "off", &nextopt)) {
1935 no_quit = 1;
1936 } else {
1937 goto invalid_sdl_args;
1939 } else {
1940 invalid_sdl_args:
1941 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1942 exit(1);
1944 opts = nextopt;
1946 #else
1947 fprintf(stderr, "SDL support is disabled\n");
1948 exit(1);
1949 #endif
1950 } else if (strstart(p, "vnc", &opts)) {
1951 #ifdef CONFIG_VNC
1952 display_remote++;
1954 if (*opts) {
1955 const char *nextopt;
1957 if (strstart(opts, "=", &nextopt)) {
1958 vnc_display = nextopt;
1961 if (!vnc_display) {
1962 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1963 exit(1);
1965 #else
1966 fprintf(stderr, "VNC support is disabled\n");
1967 exit(1);
1968 #endif
1969 } else if (strstart(p, "curses", &opts)) {
1970 #ifdef CONFIG_CURSES
1971 display = DT_CURSES;
1972 #else
1973 fprintf(stderr, "Curses support is disabled\n");
1974 exit(1);
1975 #endif
1976 } else if (strstart(p, "none", &opts)) {
1977 display = DT_NONE;
1978 } else {
1979 fprintf(stderr, "Unknown display type: %s\n", p);
1980 exit(1);
1983 return display;
1986 static int balloon_parse(const char *arg)
1988 QemuOpts *opts;
1990 if (strcmp(arg, "none") == 0) {
1991 return 0;
1994 if (!strncmp(arg, "virtio", 6)) {
1995 if (arg[6] == ',') {
1996 /* have params -> parse them */
1997 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1998 if (!opts)
1999 return -1;
2000 } else {
2001 /* create empty opts */
2002 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
2004 qemu_opt_set(opts, "driver", "virtio-balloon");
2005 return 0;
2008 return -1;
2011 char *qemu_find_file(int type, const char *name)
2013 int len;
2014 const char *subdir;
2015 char *buf;
2017 /* Try the name as a straight path first */
2018 if (access(name, R_OK) == 0) {
2019 return g_strdup(name);
2021 switch (type) {
2022 case QEMU_FILE_TYPE_BIOS:
2023 subdir = "";
2024 break;
2025 case QEMU_FILE_TYPE_KEYMAP:
2026 subdir = "keymaps/";
2027 break;
2028 default:
2029 abort();
2031 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2032 buf = g_malloc0(len);
2033 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2034 if (access(buf, R_OK)) {
2035 g_free(buf);
2036 return NULL;
2038 return buf;
2041 static int device_help_func(QemuOpts *opts, void *opaque)
2043 return qdev_device_help(opts);
2046 static int device_init_func(QemuOpts *opts, void *opaque)
2048 DeviceState *dev;
2050 dev = qdev_device_add(opts);
2051 if (!dev)
2052 return -1;
2053 return 0;
2056 static int chardev_init_func(QemuOpts *opts, void *opaque)
2058 CharDriverState *chr;
2060 chr = qemu_chr_new_from_opts(opts, NULL);
2061 if (!chr)
2062 return -1;
2063 return 0;
2066 #ifdef CONFIG_VIRTFS
2067 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2069 int ret;
2070 ret = qemu_fsdev_add(opts);
2072 return ret;
2074 #endif
2076 static int mon_init_func(QemuOpts *opts, void *opaque)
2078 CharDriverState *chr;
2079 const char *chardev;
2080 const char *mode;
2081 int flags;
2083 mode = qemu_opt_get(opts, "mode");
2084 if (mode == NULL) {
2085 mode = "readline";
2087 if (strcmp(mode, "readline") == 0) {
2088 flags = MONITOR_USE_READLINE;
2089 } else if (strcmp(mode, "control") == 0) {
2090 flags = MONITOR_USE_CONTROL;
2091 } else {
2092 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2093 exit(1);
2096 if (qemu_opt_get_bool(opts, "pretty", 0))
2097 flags |= MONITOR_USE_PRETTY;
2099 if (qemu_opt_get_bool(opts, "default", 0))
2100 flags |= MONITOR_IS_DEFAULT;
2102 chardev = qemu_opt_get(opts, "chardev");
2103 chr = qemu_chr_find(chardev);
2104 if (chr == NULL) {
2105 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2106 exit(1);
2109 monitor_init(chr, flags);
2110 return 0;
2113 static void monitor_parse(const char *optarg, const char *mode)
2115 static int monitor_device_index = 0;
2116 QemuOpts *opts;
2117 const char *p;
2118 char label[32];
2119 int def = 0;
2121 if (strstart(optarg, "chardev:", &p)) {
2122 snprintf(label, sizeof(label), "%s", p);
2123 } else {
2124 snprintf(label, sizeof(label), "compat_monitor%d",
2125 monitor_device_index);
2126 if (monitor_device_index == 0) {
2127 def = 1;
2129 opts = qemu_chr_parse_compat(label, optarg);
2130 if (!opts) {
2131 fprintf(stderr, "parse error: %s\n", optarg);
2132 exit(1);
2136 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2137 if (!opts) {
2138 fprintf(stderr, "duplicate chardev: %s\n", label);
2139 exit(1);
2141 qemu_opt_set(opts, "mode", mode);
2142 qemu_opt_set(opts, "chardev", label);
2143 if (def)
2144 qemu_opt_set(opts, "default", "on");
2145 monitor_device_index++;
2148 struct device_config {
2149 enum {
2150 DEV_USB, /* -usbdevice */
2151 DEV_BT, /* -bt */
2152 DEV_SERIAL, /* -serial */
2153 DEV_PARALLEL, /* -parallel */
2154 DEV_VIRTCON, /* -virtioconsole */
2155 DEV_DEBUGCON, /* -debugcon */
2156 DEV_GDB, /* -gdb, -s */
2157 } type;
2158 const char *cmdline;
2159 Location loc;
2160 QTAILQ_ENTRY(device_config) next;
2162 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
2164 static void add_device_config(int type, const char *cmdline)
2166 struct device_config *conf;
2168 conf = g_malloc0(sizeof(*conf));
2169 conf->type = type;
2170 conf->cmdline = cmdline;
2171 loc_save(&conf->loc);
2172 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2175 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2177 struct device_config *conf;
2178 int rc;
2180 QTAILQ_FOREACH(conf, &device_configs, next) {
2181 if (conf->type != type)
2182 continue;
2183 loc_push_restore(&conf->loc);
2184 rc = func(conf->cmdline);
2185 loc_pop(&conf->loc);
2186 if (0 != rc)
2187 return rc;
2189 return 0;
2192 static int serial_parse(const char *devname)
2194 static int index = 0;
2195 char label[32];
2197 if (strcmp(devname, "none") == 0)
2198 return 0;
2199 if (index == MAX_SERIAL_PORTS) {
2200 fprintf(stderr, "qemu: too many serial ports\n");
2201 exit(1);
2203 snprintf(label, sizeof(label), "serial%d", index);
2204 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2205 if (!serial_hds[index]) {
2206 fprintf(stderr, "qemu: could not connect serial device"
2207 " to character backend '%s'\n", devname);
2208 return -1;
2210 index++;
2211 return 0;
2214 static int parallel_parse(const char *devname)
2216 static int index = 0;
2217 char label[32];
2219 if (strcmp(devname, "none") == 0)
2220 return 0;
2221 if (index == MAX_PARALLEL_PORTS) {
2222 fprintf(stderr, "qemu: too many parallel ports\n");
2223 exit(1);
2225 snprintf(label, sizeof(label), "parallel%d", index);
2226 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2227 if (!parallel_hds[index]) {
2228 fprintf(stderr, "qemu: could not connect parallel device"
2229 " to character backend '%s'\n", devname);
2230 return -1;
2232 index++;
2233 return 0;
2236 static int virtcon_parse(const char *devname)
2238 QemuOptsList *device = qemu_find_opts("device");
2239 static int index = 0;
2240 char label[32];
2241 QemuOpts *bus_opts, *dev_opts;
2243 if (strcmp(devname, "none") == 0)
2244 return 0;
2245 if (index == MAX_VIRTIO_CONSOLES) {
2246 fprintf(stderr, "qemu: too many virtio consoles\n");
2247 exit(1);
2250 bus_opts = qemu_opts_create(device, NULL, 0, NULL);
2251 if (arch_type == QEMU_ARCH_S390X) {
2252 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2253 } else {
2254 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2257 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2258 qemu_opt_set(dev_opts, "driver", "virtconsole");
2260 snprintf(label, sizeof(label), "virtcon%d", index);
2261 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2262 if (!virtcon_hds[index]) {
2263 fprintf(stderr, "qemu: could not connect virtio console"
2264 " to character backend '%s'\n", devname);
2265 return -1;
2267 qemu_opt_set(dev_opts, "chardev", label);
2269 index++;
2270 return 0;
2273 static int debugcon_parse(const char *devname)
2275 QemuOpts *opts;
2277 if (!qemu_chr_new("debugcon", devname, NULL)) {
2278 exit(1);
2280 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2281 if (!opts) {
2282 fprintf(stderr, "qemu: already have a debugcon device\n");
2283 exit(1);
2285 qemu_opt_set(opts, "driver", "isa-debugcon");
2286 qemu_opt_set(opts, "chardev", "debugcon");
2287 return 0;
2290 static QEMUMachine *machine_parse(const char *name)
2292 QEMUMachine *m, *machine = NULL;
2294 if (name) {
2295 machine = find_machine(name);
2297 if (machine) {
2298 return machine;
2300 printf("Supported machines are:\n");
2301 for (m = first_machine; m != NULL; m = m->next) {
2302 if (m->alias) {
2303 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2305 printf("%-20s %s%s\n", m->name, m->desc,
2306 m->is_default ? " (default)" : "");
2308 exit(!name || !is_help_option(name));
2311 static int tcg_init(void)
2313 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2314 return 0;
2317 static struct {
2318 const char *opt_name;
2319 const char *name;
2320 int (*available)(void);
2321 int (*init)(void);
2322 int *allowed;
2323 } accel_list[] = {
2324 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2325 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2326 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2327 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2330 static int configure_accelerator(void)
2332 const char *p = NULL;
2333 char buf[10];
2334 int i, ret;
2335 bool accel_initialised = 0;
2336 bool init_failed = 0;
2338 QemuOptsList *list = qemu_find_opts("machine");
2339 if (!QTAILQ_EMPTY(&list->head)) {
2340 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2343 if (p == NULL) {
2344 /* Use the default "accelerator", tcg */
2345 p = "tcg";
2348 while (!accel_initialised && *p != '\0') {
2349 if (*p == ':') {
2350 p++;
2352 p = get_opt_name(buf, sizeof (buf), p, ':');
2353 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2354 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2355 *(accel_list[i].allowed) = 1;
2356 ret = accel_list[i].init();
2357 if (ret < 0) {
2358 init_failed = 1;
2359 if (!accel_list[i].available()) {
2360 printf("%s not supported for this target\n",
2361 accel_list[i].name);
2362 } else {
2363 fprintf(stderr, "failed to initialize %s: %s\n",
2364 accel_list[i].name,
2365 strerror(-ret));
2367 *(accel_list[i].allowed) = 0;
2368 } else {
2369 accel_initialised = 1;
2371 break;
2374 if (i == ARRAY_SIZE(accel_list)) {
2375 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2379 if (!accel_initialised) {
2380 fprintf(stderr, "No accelerator found!\n");
2381 exit(1);
2384 if (init_failed) {
2385 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2388 return !accel_initialised;
2391 void qemu_add_exit_notifier(Notifier *notify)
2393 notifier_list_add(&exit_notifiers, notify);
2396 void qemu_remove_exit_notifier(Notifier *notify)
2398 notifier_remove(notify);
2401 static void qemu_run_exit_notifiers(void)
2403 notifier_list_notify(&exit_notifiers, NULL);
2406 void qemu_add_machine_init_done_notifier(Notifier *notify)
2408 notifier_list_add(&machine_init_done_notifiers, notify);
2411 static void qemu_run_machine_init_done_notifiers(void)
2413 notifier_list_notify(&machine_init_done_notifiers, NULL);
2416 static const QEMUOption *lookup_opt(int argc, char **argv,
2417 const char **poptarg, int *poptind)
2419 const QEMUOption *popt;
2420 int optind = *poptind;
2421 char *r = argv[optind];
2422 const char *optarg;
2424 loc_set_cmdline(argv, optind, 1);
2425 optind++;
2426 /* Treat --foo the same as -foo. */
2427 if (r[1] == '-')
2428 r++;
2429 popt = qemu_options;
2430 for(;;) {
2431 if (!popt->name) {
2432 error_report("invalid option");
2433 exit(1);
2435 if (!strcmp(popt->name, r + 1))
2436 break;
2437 popt++;
2439 if (popt->flags & HAS_ARG) {
2440 if (optind >= argc) {
2441 error_report("requires an argument");
2442 exit(1);
2444 optarg = argv[optind++];
2445 loc_set_cmdline(argv, optind - 2, 2);
2446 } else {
2447 optarg = NULL;
2450 *poptarg = optarg;
2451 *poptind = optind;
2453 return popt;
2456 static gpointer malloc_and_trace(gsize n_bytes)
2458 void *ptr = malloc(n_bytes);
2459 trace_g_malloc(n_bytes, ptr);
2460 return ptr;
2463 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2465 void *ptr = realloc(mem, n_bytes);
2466 trace_g_realloc(mem, n_bytes, ptr);
2467 return ptr;
2470 static void free_and_trace(gpointer mem)
2472 trace_g_free(mem);
2473 free(mem);
2476 int main(int argc, char **argv, char **envp)
2478 int i;
2479 int snapshot, linux_boot;
2480 const char *icount_option = NULL;
2481 const char *initrd_filename;
2482 const char *kernel_filename, *kernel_cmdline;
2483 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2484 DisplayState *ds;
2485 int cyls, heads, secs, translation;
2486 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2487 QemuOptsList *olist;
2488 int optind;
2489 const char *optarg;
2490 const char *loadvm = NULL;
2491 QEMUMachine *machine;
2492 const char *cpu_model;
2493 const char *vga_model = "none";
2494 const char *pid_file = NULL;
2495 const char *incoming = NULL;
2496 #ifdef CONFIG_VNC
2497 int show_vnc_port = 0;
2498 #endif
2499 bool defconfig = true;
2500 bool userconfig = true;
2501 const char *log_mask = NULL;
2502 const char *log_file = NULL;
2503 GMemVTable mem_trace = {
2504 .malloc = malloc_and_trace,
2505 .realloc = realloc_and_trace,
2506 .free = free_and_trace,
2508 const char *trace_events = NULL;
2509 const char *trace_file = NULL;
2511 atexit(qemu_run_exit_notifiers);
2512 error_set_progname(argv[0]);
2514 g_mem_set_vtable(&mem_trace);
2515 if (!g_thread_supported()) {
2516 #if !GLIB_CHECK_VERSION(2, 31, 0)
2517 g_thread_init(NULL);
2518 #else
2519 fprintf(stderr, "glib threading failed to initialize.\n");
2520 exit(1);
2521 #endif
2524 module_call_init(MODULE_INIT_QOM);
2526 runstate_init();
2528 init_clocks();
2529 rtc_clock = host_clock;
2531 qemu_cache_utils_init(envp);
2533 QLIST_INIT (&vm_change_state_head);
2534 os_setup_early_signal_handling();
2536 module_call_init(MODULE_INIT_MACHINE);
2537 machine = find_default_machine();
2538 cpu_model = NULL;
2539 ram_size = 0;
2540 snapshot = 0;
2541 cyls = heads = secs = 0;
2542 translation = BIOS_ATA_TRANSLATION_AUTO;
2544 for (i = 0; i < MAX_NODES; i++) {
2545 node_mem[i] = 0;
2546 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
2549 nb_numa_nodes = 0;
2550 nb_nics = 0;
2552 autostart= 1;
2554 /* first pass of option parsing */
2555 optind = 1;
2556 while (optind < argc) {
2557 if (argv[optind][0] != '-') {
2558 /* disk image */
2559 optind++;
2560 continue;
2561 } else {
2562 const QEMUOption *popt;
2564 popt = lookup_opt(argc, argv, &optarg, &optind);
2565 switch (popt->index) {
2566 case QEMU_OPTION_nodefconfig:
2567 defconfig = false;
2568 break;
2569 case QEMU_OPTION_nouserconfig:
2570 userconfig = false;
2571 break;
2576 if (defconfig) {
2577 int ret;
2578 ret = qemu_read_default_config_files(userconfig);
2579 if (ret < 0) {
2580 exit(1);
2584 /* second pass of option parsing */
2585 optind = 1;
2586 for(;;) {
2587 if (optind >= argc)
2588 break;
2589 if (argv[optind][0] != '-') {
2590 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2591 } else {
2592 const QEMUOption *popt;
2594 popt = lookup_opt(argc, argv, &optarg, &optind);
2595 if (!(popt->arch_mask & arch_type)) {
2596 printf("Option %s not supported for this target\n", popt->name);
2597 exit(1);
2599 switch(popt->index) {
2600 case QEMU_OPTION_M:
2601 machine = machine_parse(optarg);
2602 break;
2603 case QEMU_OPTION_no_kvm_irqchip: {
2604 olist = qemu_find_opts("machine");
2605 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2606 break;
2608 case QEMU_OPTION_cpu:
2609 /* hw initialization will check this */
2610 cpu_model = optarg;
2611 break;
2612 case QEMU_OPTION_hda:
2614 char buf[256];
2615 if (cyls == 0)
2616 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2617 else
2618 snprintf(buf, sizeof(buf),
2619 "%s,cyls=%d,heads=%d,secs=%d%s",
2620 HD_OPTS , cyls, heads, secs,
2621 translation == BIOS_ATA_TRANSLATION_LBA ?
2622 ",trans=lba" :
2623 translation == BIOS_ATA_TRANSLATION_NONE ?
2624 ",trans=none" : "");
2625 drive_add(IF_DEFAULT, 0, optarg, buf);
2626 break;
2628 case QEMU_OPTION_hdb:
2629 case QEMU_OPTION_hdc:
2630 case QEMU_OPTION_hdd:
2631 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2632 HD_OPTS);
2633 break;
2634 case QEMU_OPTION_drive:
2635 if (drive_def(optarg) == NULL) {
2636 exit(1);
2638 break;
2639 case QEMU_OPTION_set:
2640 if (qemu_set_option(optarg) != 0)
2641 exit(1);
2642 break;
2643 case QEMU_OPTION_global:
2644 if (qemu_global_option(optarg) != 0)
2645 exit(1);
2646 break;
2647 case QEMU_OPTION_mtdblock:
2648 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2649 break;
2650 case QEMU_OPTION_sd:
2651 drive_add(IF_SD, 0, optarg, SD_OPTS);
2652 break;
2653 case QEMU_OPTION_pflash:
2654 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2655 break;
2656 case QEMU_OPTION_snapshot:
2657 snapshot = 1;
2658 break;
2659 case QEMU_OPTION_hdachs:
2661 const char *p;
2662 p = optarg;
2663 cyls = strtol(p, (char **)&p, 0);
2664 if (cyls < 1 || cyls > 16383)
2665 goto chs_fail;
2666 if (*p != ',')
2667 goto chs_fail;
2668 p++;
2669 heads = strtol(p, (char **)&p, 0);
2670 if (heads < 1 || heads > 16)
2671 goto chs_fail;
2672 if (*p != ',')
2673 goto chs_fail;
2674 p++;
2675 secs = strtol(p, (char **)&p, 0);
2676 if (secs < 1 || secs > 63)
2677 goto chs_fail;
2678 if (*p == ',') {
2679 p++;
2680 if (!strcmp(p, "none"))
2681 translation = BIOS_ATA_TRANSLATION_NONE;
2682 else if (!strcmp(p, "lba"))
2683 translation = BIOS_ATA_TRANSLATION_LBA;
2684 else if (!strcmp(p, "auto"))
2685 translation = BIOS_ATA_TRANSLATION_AUTO;
2686 else
2687 goto chs_fail;
2688 } else if (*p != '\0') {
2689 chs_fail:
2690 fprintf(stderr, "qemu: invalid physical CHS format\n");
2691 exit(1);
2693 if (hda_opts != NULL) {
2694 char num[16];
2695 snprintf(num, sizeof(num), "%d", cyls);
2696 qemu_opt_set(hda_opts, "cyls", num);
2697 snprintf(num, sizeof(num), "%d", heads);
2698 qemu_opt_set(hda_opts, "heads", num);
2699 snprintf(num, sizeof(num), "%d", secs);
2700 qemu_opt_set(hda_opts, "secs", num);
2701 if (translation == BIOS_ATA_TRANSLATION_LBA)
2702 qemu_opt_set(hda_opts, "trans", "lba");
2703 if (translation == BIOS_ATA_TRANSLATION_NONE)
2704 qemu_opt_set(hda_opts, "trans", "none");
2707 break;
2708 case QEMU_OPTION_numa:
2709 if (nb_numa_nodes >= MAX_NODES) {
2710 fprintf(stderr, "qemu: too many NUMA nodes\n");
2711 exit(1);
2713 numa_add(optarg);
2714 break;
2715 case QEMU_OPTION_display:
2716 display_type = select_display(optarg);
2717 break;
2718 case QEMU_OPTION_nographic:
2719 display_type = DT_NOGRAPHIC;
2720 break;
2721 case QEMU_OPTION_curses:
2722 #ifdef CONFIG_CURSES
2723 display_type = DT_CURSES;
2724 #else
2725 fprintf(stderr, "Curses support is disabled\n");
2726 exit(1);
2727 #endif
2728 break;
2729 case QEMU_OPTION_portrait:
2730 graphic_rotate = 90;
2731 break;
2732 case QEMU_OPTION_rotate:
2733 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2734 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2735 graphic_rotate != 180 && graphic_rotate != 270) {
2736 fprintf(stderr,
2737 "qemu: only 90, 180, 270 deg rotation is available\n");
2738 exit(1);
2740 break;
2741 case QEMU_OPTION_kernel:
2742 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2743 break;
2744 case QEMU_OPTION_initrd:
2745 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
2746 break;
2747 case QEMU_OPTION_append:
2748 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
2749 break;
2750 case QEMU_OPTION_dtb:
2751 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2752 break;
2753 case QEMU_OPTION_cdrom:
2754 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2755 break;
2756 case QEMU_OPTION_boot:
2758 static const char * const params[] = {
2759 "order", "once", "menu",
2760 "splash", "splash-time",
2761 "reboot-timeout", NULL
2763 char buf[sizeof(boot_devices)];
2764 char *standard_boot_devices;
2765 int legacy = 0;
2767 if (!strchr(optarg, '=')) {
2768 legacy = 1;
2769 pstrcpy(buf, sizeof(buf), optarg);
2770 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2771 fprintf(stderr,
2772 "qemu: unknown boot parameter '%s' in '%s'\n",
2773 buf, optarg);
2774 exit(1);
2777 if (legacy ||
2778 get_param_value(buf, sizeof(buf), "order", optarg)) {
2779 validate_bootdevices(buf);
2780 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2782 if (!legacy) {
2783 if (get_param_value(buf, sizeof(buf),
2784 "once", optarg)) {
2785 validate_bootdevices(buf);
2786 standard_boot_devices = g_strdup(boot_devices);
2787 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2788 qemu_register_reset(restore_boot_devices,
2789 standard_boot_devices);
2791 if (get_param_value(buf, sizeof(buf),
2792 "menu", optarg)) {
2793 if (!strcmp(buf, "on")) {
2794 boot_menu = 1;
2795 } else if (!strcmp(buf, "off")) {
2796 boot_menu = 0;
2797 } else {
2798 fprintf(stderr,
2799 "qemu: invalid option value '%s'\n",
2800 buf);
2801 exit(1);
2804 qemu_opts_parse(qemu_find_opts("boot-opts"),
2805 optarg, 0);
2808 break;
2809 case QEMU_OPTION_fda:
2810 case QEMU_OPTION_fdb:
2811 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2812 optarg, FD_OPTS);
2813 break;
2814 case QEMU_OPTION_no_fd_bootchk:
2815 fd_bootchk = 0;
2816 break;
2817 case QEMU_OPTION_netdev:
2818 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2819 exit(1);
2821 break;
2822 case QEMU_OPTION_net:
2823 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2824 exit(1);
2826 break;
2827 #ifdef CONFIG_LIBISCSI
2828 case QEMU_OPTION_iscsi:
2829 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2830 if (!opts) {
2831 exit(1);
2833 break;
2834 #endif
2835 #ifdef CONFIG_SLIRP
2836 case QEMU_OPTION_tftp:
2837 legacy_tftp_prefix = optarg;
2838 break;
2839 case QEMU_OPTION_bootp:
2840 legacy_bootp_filename = optarg;
2841 break;
2842 case QEMU_OPTION_redir:
2843 if (net_slirp_redir(optarg) < 0)
2844 exit(1);
2845 break;
2846 #endif
2847 case QEMU_OPTION_bt:
2848 add_device_config(DEV_BT, optarg);
2849 break;
2850 case QEMU_OPTION_audio_help:
2851 if (!(audio_available())) {
2852 printf("Option %s not supported for this target\n", popt->name);
2853 exit(1);
2855 AUD_help ();
2856 exit (0);
2857 break;
2858 case QEMU_OPTION_soundhw:
2859 if (!(audio_available())) {
2860 printf("Option %s not supported for this target\n", popt->name);
2861 exit(1);
2863 select_soundhw (optarg);
2864 break;
2865 case QEMU_OPTION_h:
2866 help(0);
2867 break;
2868 case QEMU_OPTION_version:
2869 version();
2870 exit(0);
2871 break;
2872 case QEMU_OPTION_m: {
2873 int64_t value;
2874 uint64_t sz;
2875 char *end;
2877 value = strtosz(optarg, &end);
2878 if (value < 0 || *end) {
2879 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2880 exit(1);
2882 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
2883 ram_size = sz;
2884 if (ram_size != sz) {
2885 fprintf(stderr, "qemu: ram size too large\n");
2886 exit(1);
2888 break;
2890 case QEMU_OPTION_mempath:
2891 mem_path = optarg;
2892 break;
2893 #ifdef MAP_POPULATE
2894 case QEMU_OPTION_mem_prealloc:
2895 mem_prealloc = 1;
2896 break;
2897 #endif
2898 case QEMU_OPTION_d:
2899 log_mask = optarg;
2900 break;
2901 case QEMU_OPTION_D:
2902 log_file = optarg;
2903 break;
2904 case QEMU_OPTION_s:
2905 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2906 break;
2907 case QEMU_OPTION_gdb:
2908 add_device_config(DEV_GDB, optarg);
2909 break;
2910 case QEMU_OPTION_L:
2911 data_dir = optarg;
2912 break;
2913 case QEMU_OPTION_bios:
2914 bios_name = optarg;
2915 break;
2916 case QEMU_OPTION_singlestep:
2917 singlestep = 1;
2918 break;
2919 case QEMU_OPTION_S:
2920 autostart = 0;
2921 break;
2922 case QEMU_OPTION_k:
2923 keyboard_layout = optarg;
2924 break;
2925 case QEMU_OPTION_localtime:
2926 rtc_utc = 0;
2927 break;
2928 case QEMU_OPTION_vga:
2929 vga_model = optarg;
2930 default_vga = 0;
2931 break;
2932 case QEMU_OPTION_g:
2934 const char *p;
2935 int w, h, depth;
2936 p = optarg;
2937 w = strtol(p, (char **)&p, 10);
2938 if (w <= 0) {
2939 graphic_error:
2940 fprintf(stderr, "qemu: invalid resolution or depth\n");
2941 exit(1);
2943 if (*p != 'x')
2944 goto graphic_error;
2945 p++;
2946 h = strtol(p, (char **)&p, 10);
2947 if (h <= 0)
2948 goto graphic_error;
2949 if (*p == 'x') {
2950 p++;
2951 depth = strtol(p, (char **)&p, 10);
2952 if (depth != 8 && depth != 15 && depth != 16 &&
2953 depth != 24 && depth != 32)
2954 goto graphic_error;
2955 } else if (*p == '\0') {
2956 depth = graphic_depth;
2957 } else {
2958 goto graphic_error;
2961 graphic_width = w;
2962 graphic_height = h;
2963 graphic_depth = depth;
2965 break;
2966 case QEMU_OPTION_echr:
2968 char *r;
2969 term_escape_char = strtol(optarg, &r, 0);
2970 if (r == optarg)
2971 printf("Bad argument to echr\n");
2972 break;
2974 case QEMU_OPTION_monitor:
2975 monitor_parse(optarg, "readline");
2976 default_monitor = 0;
2977 break;
2978 case QEMU_OPTION_qmp:
2979 monitor_parse(optarg, "control");
2980 default_monitor = 0;
2981 break;
2982 case QEMU_OPTION_mon:
2983 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2984 if (!opts) {
2985 exit(1);
2987 default_monitor = 0;
2988 break;
2989 case QEMU_OPTION_chardev:
2990 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2991 if (!opts) {
2992 exit(1);
2994 break;
2995 case QEMU_OPTION_fsdev:
2996 olist = qemu_find_opts("fsdev");
2997 if (!olist) {
2998 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2999 exit(1);
3001 opts = qemu_opts_parse(olist, optarg, 1);
3002 if (!opts) {
3003 fprintf(stderr, "parse error: %s\n", optarg);
3004 exit(1);
3006 break;
3007 case QEMU_OPTION_virtfs: {
3008 QemuOpts *fsdev;
3009 QemuOpts *device;
3010 const char *writeout, *sock_fd, *socket;
3012 olist = qemu_find_opts("virtfs");
3013 if (!olist) {
3014 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3015 exit(1);
3017 opts = qemu_opts_parse(olist, optarg, 1);
3018 if (!opts) {
3019 fprintf(stderr, "parse error: %s\n", optarg);
3020 exit(1);
3023 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3024 qemu_opt_get(opts, "mount_tag") == NULL) {
3025 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3026 exit(1);
3028 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3029 qemu_opt_get(opts, "mount_tag"),
3030 1, NULL);
3031 if (!fsdev) {
3032 fprintf(stderr, "duplicate fsdev id: %s\n",
3033 qemu_opt_get(opts, "mount_tag"));
3034 exit(1);
3037 writeout = qemu_opt_get(opts, "writeout");
3038 if (writeout) {
3039 #ifdef CONFIG_SYNC_FILE_RANGE
3040 qemu_opt_set(fsdev, "writeout", writeout);
3041 #else
3042 fprintf(stderr, "writeout=immediate not supported on "
3043 "this platform\n");
3044 exit(1);
3045 #endif
3047 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3048 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3049 qemu_opt_set(fsdev, "security_model",
3050 qemu_opt_get(opts, "security_model"));
3051 socket = qemu_opt_get(opts, "socket");
3052 if (socket) {
3053 qemu_opt_set(fsdev, "socket", socket);
3055 sock_fd = qemu_opt_get(opts, "sock_fd");
3056 if (sock_fd) {
3057 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3060 qemu_opt_set_bool(fsdev, "readonly",
3061 qemu_opt_get_bool(opts, "readonly", 0));
3062 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3063 NULL);
3064 qemu_opt_set(device, "driver", "virtio-9p-pci");
3065 qemu_opt_set(device, "fsdev",
3066 qemu_opt_get(opts, "mount_tag"));
3067 qemu_opt_set(device, "mount_tag",
3068 qemu_opt_get(opts, "mount_tag"));
3069 break;
3071 case QEMU_OPTION_virtfs_synth: {
3072 QemuOpts *fsdev;
3073 QemuOpts *device;
3075 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3076 1, NULL);
3077 if (!fsdev) {
3078 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3079 exit(1);
3081 qemu_opt_set(fsdev, "fsdriver", "synth");
3083 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3084 NULL);
3085 qemu_opt_set(device, "driver", "virtio-9p-pci");
3086 qemu_opt_set(device, "fsdev", "v_synth");
3087 qemu_opt_set(device, "mount_tag", "v_synth");
3088 break;
3090 case QEMU_OPTION_serial:
3091 add_device_config(DEV_SERIAL, optarg);
3092 default_serial = 0;
3093 if (strncmp(optarg, "mon:", 4) == 0) {
3094 default_monitor = 0;
3096 break;
3097 case QEMU_OPTION_watchdog:
3098 if (watchdog) {
3099 fprintf(stderr,
3100 "qemu: only one watchdog option may be given\n");
3101 return 1;
3103 watchdog = optarg;
3104 break;
3105 case QEMU_OPTION_watchdog_action:
3106 if (select_watchdog_action(optarg) == -1) {
3107 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3108 exit(1);
3110 break;
3111 case QEMU_OPTION_virtiocon:
3112 add_device_config(DEV_VIRTCON, optarg);
3113 default_virtcon = 0;
3114 if (strncmp(optarg, "mon:", 4) == 0) {
3115 default_monitor = 0;
3117 break;
3118 case QEMU_OPTION_parallel:
3119 add_device_config(DEV_PARALLEL, optarg);
3120 default_parallel = 0;
3121 if (strncmp(optarg, "mon:", 4) == 0) {
3122 default_monitor = 0;
3124 break;
3125 case QEMU_OPTION_debugcon:
3126 add_device_config(DEV_DEBUGCON, optarg);
3127 break;
3128 case QEMU_OPTION_loadvm:
3129 loadvm = optarg;
3130 break;
3131 case QEMU_OPTION_full_screen:
3132 full_screen = 1;
3133 break;
3134 #ifdef CONFIG_SDL
3135 case QEMU_OPTION_no_frame:
3136 no_frame = 1;
3137 break;
3138 case QEMU_OPTION_alt_grab:
3139 alt_grab = 1;
3140 break;
3141 case QEMU_OPTION_ctrl_grab:
3142 ctrl_grab = 1;
3143 break;
3144 case QEMU_OPTION_no_quit:
3145 no_quit = 1;
3146 break;
3147 case QEMU_OPTION_sdl:
3148 display_type = DT_SDL;
3149 break;
3150 #else
3151 case QEMU_OPTION_no_frame:
3152 case QEMU_OPTION_alt_grab:
3153 case QEMU_OPTION_ctrl_grab:
3154 case QEMU_OPTION_no_quit:
3155 case QEMU_OPTION_sdl:
3156 fprintf(stderr, "SDL support is disabled\n");
3157 exit(1);
3158 #endif
3159 case QEMU_OPTION_pidfile:
3160 pid_file = optarg;
3161 break;
3162 case QEMU_OPTION_win2k_hack:
3163 win2k_install_hack = 1;
3164 break;
3165 case QEMU_OPTION_rtc_td_hack: {
3166 static GlobalProperty slew_lost_ticks[] = {
3168 .driver = "mc146818rtc",
3169 .property = "lost_tick_policy",
3170 .value = "slew",
3172 { /* end of list */ }
3175 qdev_prop_register_global_list(slew_lost_ticks);
3176 break;
3178 case QEMU_OPTION_acpitable:
3179 do_acpitable_option(optarg);
3180 break;
3181 case QEMU_OPTION_smbios:
3182 do_smbios_option(optarg);
3183 break;
3184 case QEMU_OPTION_enable_kvm:
3185 olist = qemu_find_opts("machine");
3186 qemu_opts_parse(olist, "accel=kvm", 0);
3187 break;
3188 case QEMU_OPTION_machine:
3189 olist = qemu_find_opts("machine");
3190 opts = qemu_opts_parse(olist, optarg, 1);
3191 if (!opts) {
3192 fprintf(stderr, "parse error: %s\n", optarg);
3193 exit(1);
3195 optarg = qemu_opt_get(opts, "type");
3196 if (optarg) {
3197 machine = machine_parse(optarg);
3199 break;
3200 case QEMU_OPTION_no_kvm:
3201 olist = qemu_find_opts("machine");
3202 qemu_opts_parse(olist, "accel=tcg", 0);
3203 break;
3204 case QEMU_OPTION_no_kvm_pit: {
3205 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3206 "separately.\n");
3207 break;
3209 case QEMU_OPTION_no_kvm_pit_reinjection: {
3210 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3212 .driver = "kvm-pit",
3213 .property = "lost_tick_policy",
3214 .value = "discard",
3216 { /* end of list */ }
3219 fprintf(stderr, "Warning: option deprecated, use "
3220 "lost_tick_policy property of kvm-pit instead.\n");
3221 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3222 break;
3224 case QEMU_OPTION_usb:
3225 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3226 if (machine_opts) {
3227 qemu_opt_set_bool(machine_opts, "usb", true);
3229 break;
3230 case QEMU_OPTION_usbdevice:
3231 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3232 if (machine_opts) {
3233 qemu_opt_set_bool(machine_opts, "usb", true);
3235 add_device_config(DEV_USB, optarg);
3236 break;
3237 case QEMU_OPTION_device:
3238 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3239 exit(1);
3241 break;
3242 case QEMU_OPTION_smp:
3243 smp_parse(optarg);
3244 if (smp_cpus < 1) {
3245 fprintf(stderr, "Invalid number of CPUs\n");
3246 exit(1);
3248 if (max_cpus < smp_cpus) {
3249 fprintf(stderr, "maxcpus must be equal to or greater than "
3250 "smp\n");
3251 exit(1);
3253 if (max_cpus > 255) {
3254 fprintf(stderr, "Unsupported number of maxcpus\n");
3255 exit(1);
3257 break;
3258 case QEMU_OPTION_vnc:
3259 #ifdef CONFIG_VNC
3260 display_remote++;
3261 vnc_display = optarg;
3262 #else
3263 fprintf(stderr, "VNC support is disabled\n");
3264 exit(1);
3265 #endif
3266 break;
3267 case QEMU_OPTION_no_acpi:
3268 acpi_enabled = 0;
3269 break;
3270 case QEMU_OPTION_no_hpet:
3271 no_hpet = 1;
3272 break;
3273 case QEMU_OPTION_balloon:
3274 if (balloon_parse(optarg) < 0) {
3275 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3276 exit(1);
3278 break;
3279 case QEMU_OPTION_no_reboot:
3280 no_reboot = 1;
3281 break;
3282 case QEMU_OPTION_no_shutdown:
3283 no_shutdown = 1;
3284 break;
3285 case QEMU_OPTION_show_cursor:
3286 cursor_hide = 0;
3287 break;
3288 case QEMU_OPTION_uuid:
3289 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3290 fprintf(stderr, "Fail to parse UUID string."
3291 " Wrong format.\n");
3292 exit(1);
3294 break;
3295 case QEMU_OPTION_option_rom:
3296 if (nb_option_roms >= MAX_OPTION_ROMS) {
3297 fprintf(stderr, "Too many option ROMs\n");
3298 exit(1);
3300 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3301 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3302 option_rom[nb_option_roms].bootindex =
3303 qemu_opt_get_number(opts, "bootindex", -1);
3304 if (!option_rom[nb_option_roms].name) {
3305 fprintf(stderr, "Option ROM file is not specified\n");
3306 exit(1);
3308 nb_option_roms++;
3309 break;
3310 case QEMU_OPTION_semihosting:
3311 semihosting_enabled = 1;
3312 break;
3313 case QEMU_OPTION_tdf:
3314 fprintf(stderr, "Warning: user space PIT time drift fix "
3315 "is no longer supported.\n");
3316 break;
3317 case QEMU_OPTION_name:
3318 qemu_name = g_strdup(optarg);
3320 char *p = strchr(qemu_name, ',');
3321 if (p != NULL) {
3322 *p++ = 0;
3323 if (strncmp(p, "process=", 8)) {
3324 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3325 exit(1);
3327 p += 8;
3328 os_set_proc_name(p);
3331 break;
3332 case QEMU_OPTION_prom_env:
3333 if (nb_prom_envs >= MAX_PROM_ENVS) {
3334 fprintf(stderr, "Too many prom variables\n");
3335 exit(1);
3337 prom_envs[nb_prom_envs] = optarg;
3338 nb_prom_envs++;
3339 break;
3340 case QEMU_OPTION_old_param:
3341 old_param = 1;
3342 break;
3343 case QEMU_OPTION_clock:
3344 configure_alarms(optarg);
3345 break;
3346 case QEMU_OPTION_startdate:
3347 configure_rtc_date_offset(optarg, 1);
3348 break;
3349 case QEMU_OPTION_rtc:
3350 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3351 if (!opts) {
3352 exit(1);
3354 configure_rtc(opts);
3355 break;
3356 case QEMU_OPTION_tb_size:
3357 tcg_tb_size = strtol(optarg, NULL, 0);
3358 if (tcg_tb_size < 0) {
3359 tcg_tb_size = 0;
3361 break;
3362 case QEMU_OPTION_icount:
3363 icount_option = optarg;
3364 break;
3365 case QEMU_OPTION_incoming:
3366 incoming = optarg;
3367 runstate_set(RUN_STATE_INMIGRATE);
3368 break;
3369 case QEMU_OPTION_nodefaults:
3370 default_serial = 0;
3371 default_parallel = 0;
3372 default_virtcon = 0;
3373 default_monitor = 0;
3374 default_net = 0;
3375 default_floppy = 0;
3376 default_cdrom = 0;
3377 default_sdcard = 0;
3378 default_vga = 0;
3379 break;
3380 case QEMU_OPTION_xen_domid:
3381 if (!(xen_available())) {
3382 printf("Option %s not supported for this target\n", popt->name);
3383 exit(1);
3385 xen_domid = atoi(optarg);
3386 break;
3387 case QEMU_OPTION_xen_create:
3388 if (!(xen_available())) {
3389 printf("Option %s not supported for this target\n", popt->name);
3390 exit(1);
3392 xen_mode = XEN_CREATE;
3393 break;
3394 case QEMU_OPTION_xen_attach:
3395 if (!(xen_available())) {
3396 printf("Option %s not supported for this target\n", popt->name);
3397 exit(1);
3399 xen_mode = XEN_ATTACH;
3400 break;
3401 case QEMU_OPTION_trace:
3403 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3404 if (!opts) {
3405 exit(1);
3407 trace_events = qemu_opt_get(opts, "events");
3408 trace_file = qemu_opt_get(opts, "file");
3409 break;
3411 case QEMU_OPTION_readconfig:
3413 int ret = qemu_read_config_file(optarg);
3414 if (ret < 0) {
3415 fprintf(stderr, "read config %s: %s\n", optarg,
3416 strerror(-ret));
3417 exit(1);
3419 break;
3421 case QEMU_OPTION_spice:
3422 olist = qemu_find_opts("spice");
3423 if (!olist) {
3424 fprintf(stderr, "spice is not supported by this qemu build.\n");
3425 exit(1);
3427 opts = qemu_opts_parse(olist, optarg, 0);
3428 if (!opts) {
3429 fprintf(stderr, "parse error: %s\n", optarg);
3430 exit(1);
3432 break;
3433 case QEMU_OPTION_writeconfig:
3435 FILE *fp;
3436 if (strcmp(optarg, "-") == 0) {
3437 fp = stdout;
3438 } else {
3439 fp = fopen(optarg, "w");
3440 if (fp == NULL) {
3441 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3442 exit(1);
3445 qemu_config_write(fp);
3446 fclose(fp);
3447 break;
3449 case QEMU_OPTION_qtest:
3450 qtest_chrdev = optarg;
3451 break;
3452 case QEMU_OPTION_qtest_log:
3453 qtest_log = optarg;
3454 break;
3455 case QEMU_OPTION_sandbox:
3456 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3457 if (!opts) {
3458 exit(0);
3460 break;
3461 case QEMU_OPTION_add_fd:
3462 #ifndef _WIN32
3463 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3464 if (!opts) {
3465 exit(0);
3467 #else
3468 error_report("File descriptor passing is disabled on this "
3469 "platform");
3470 exit(1);
3471 #endif
3472 break;
3473 default:
3474 os_parse_cmd_args(popt->index, optarg);
3478 loc_set_none();
3480 if (qemu_init_main_loop()) {
3481 fprintf(stderr, "qemu_init_main_loop failed\n");
3482 exit(1);
3485 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3486 exit(1);
3489 #ifndef _WIN32
3490 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3491 exit(1);
3494 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3495 exit(1);
3497 #endif
3499 if (machine == NULL) {
3500 fprintf(stderr, "No machine found.\n");
3501 exit(1);
3504 if (machine->hw_version) {
3505 qemu_set_version(machine->hw_version);
3508 /* Init CPU def lists, based on config
3509 * - Must be called after all the qemu_read_config_file() calls
3510 * - Must be called before list_cpus()
3511 * - Must be called before machine->init()
3513 cpudef_init();
3515 if (cpu_model && is_help_option(cpu_model)) {
3516 list_cpus(stdout, &fprintf, cpu_model);
3517 exit(0);
3520 /* Open the logfile at this point, if necessary. We can't open the logfile
3521 * when encountering either of the logging options (-d or -D) because the
3522 * other one may be encountered later on the command line, changing the
3523 * location or level of logging.
3525 if (log_mask) {
3526 if (log_file) {
3527 set_cpu_log_filename(log_file);
3529 set_cpu_log(log_mask);
3532 if (!trace_backend_init(trace_events, trace_file)) {
3533 exit(1);
3536 /* If no data_dir is specified then try to find it relative to the
3537 executable path. */
3538 if (!data_dir) {
3539 data_dir = os_find_datadir(argv[0]);
3541 /* If all else fails use the install path specified when building. */
3542 if (!data_dir) {
3543 data_dir = CONFIG_QEMU_DATADIR;
3547 * Default to max_cpus = smp_cpus, in case the user doesn't
3548 * specify a max_cpus value.
3550 if (!max_cpus)
3551 max_cpus = smp_cpus;
3553 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3554 if (smp_cpus > machine->max_cpus) {
3555 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3556 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3557 machine->max_cpus);
3558 exit(1);
3562 * Get the default machine options from the machine if it is not already
3563 * specified either by the configuration file or by the command line.
3565 if (machine->default_machine_opts) {
3566 qemu_opts_set_defaults(qemu_find_opts("machine"),
3567 machine->default_machine_opts, 0);
3570 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3571 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3573 if (machine->no_serial) {
3574 default_serial = 0;
3576 if (machine->no_parallel) {
3577 default_parallel = 0;
3579 if (!machine->use_virtcon) {
3580 default_virtcon = 0;
3582 if (machine->no_floppy) {
3583 default_floppy = 0;
3585 if (machine->no_cdrom) {
3586 default_cdrom = 0;
3588 if (machine->no_sdcard) {
3589 default_sdcard = 0;
3592 if (display_type == DT_NOGRAPHIC) {
3593 if (default_parallel)
3594 add_device_config(DEV_PARALLEL, "null");
3595 if (default_serial && default_monitor) {
3596 add_device_config(DEV_SERIAL, "mon:stdio");
3597 } else if (default_virtcon && default_monitor) {
3598 add_device_config(DEV_VIRTCON, "mon:stdio");
3599 } else {
3600 if (default_serial)
3601 add_device_config(DEV_SERIAL, "stdio");
3602 if (default_virtcon)
3603 add_device_config(DEV_VIRTCON, "stdio");
3604 if (default_monitor)
3605 monitor_parse("stdio", "readline");
3607 } else {
3608 if (default_serial)
3609 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3610 if (default_parallel)
3611 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3612 if (default_monitor)
3613 monitor_parse("vc:80Cx24C", "readline");
3614 if (default_virtcon)
3615 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3618 socket_init();
3620 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3621 exit(1);
3622 #ifdef CONFIG_VIRTFS
3623 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3624 exit(1);
3626 #endif
3628 os_daemonize();
3630 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3631 os_pidfile_error();
3632 exit(1);
3635 /* init the memory */
3636 if (ram_size == 0) {
3637 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3640 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
3641 != 0) {
3642 exit(0);
3645 configure_accelerator();
3647 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3648 if (machine_opts) {
3649 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3650 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3651 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3652 } else {
3653 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3656 if (!kernel_cmdline) {
3657 kernel_cmdline = "";
3660 linux_boot = (kernel_filename != NULL);
3662 if (!linux_boot && *kernel_cmdline != '\0') {
3663 fprintf(stderr, "-append only allowed with -kernel option\n");
3664 exit(1);
3667 if (!linux_boot && initrd_filename != NULL) {
3668 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3669 exit(1);
3672 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3673 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3674 exit(1);
3677 os_set_line_buffering();
3679 qemu_init_cpu_loop();
3680 qemu_mutex_lock_iothread();
3682 #ifdef CONFIG_SPICE
3683 /* spice needs the timers to be initialized by this point */
3684 qemu_spice_init();
3685 #endif
3687 if (icount_option && (kvm_enabled() || xen_enabled())) {
3688 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3689 exit(1);
3691 configure_icount(icount_option);
3693 if (net_init_clients() < 0) {
3694 exit(1);
3697 /* init the bluetooth world */
3698 if (foreach_device_config(DEV_BT, bt_parse))
3699 exit(1);
3701 if (!xen_enabled()) {
3702 /* On 32-bit hosts, QEMU is limited by virtual address space */
3703 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3704 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3705 exit(1);
3709 cpu_exec_init_all();
3711 bdrv_init_with_whitelist();
3713 blk_mig_init();
3715 /* open the virtual block devices */
3716 if (snapshot)
3717 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3718 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3719 exit(1);
3721 default_drive(default_cdrom, snapshot, machine->use_scsi,
3722 IF_DEFAULT, 2, CDROM_OPTS);
3723 default_drive(default_floppy, snapshot, machine->use_scsi,
3724 IF_FLOPPY, 0, FD_OPTS);
3725 default_drive(default_sdcard, snapshot, machine->use_scsi,
3726 IF_SD, 0, SD_OPTS);
3728 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
3730 if (nb_numa_nodes > 0) {
3731 int i;
3733 if (nb_numa_nodes > MAX_NODES) {
3734 nb_numa_nodes = MAX_NODES;
3737 /* If no memory size if given for any node, assume the default case
3738 * and distribute the available memory equally across all nodes
3740 for (i = 0; i < nb_numa_nodes; i++) {
3741 if (node_mem[i] != 0)
3742 break;
3744 if (i == nb_numa_nodes) {
3745 uint64_t usedmem = 0;
3747 /* On Linux, the each node's border has to be 8MB aligned,
3748 * the final node gets the rest.
3750 for (i = 0; i < nb_numa_nodes - 1; i++) {
3751 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3752 usedmem += node_mem[i];
3754 node_mem[i] = ram_size - usedmem;
3757 for (i = 0; i < nb_numa_nodes; i++) {
3758 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
3759 break;
3762 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3763 * must cope with this anyway, because there are BIOSes out there in
3764 * real machines which also use this scheme.
3766 if (i == nb_numa_nodes) {
3767 for (i = 0; i < max_cpus; i++) {
3768 set_bit(i, node_cpumask[i % nb_numa_nodes]);
3773 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3774 exit(1);
3777 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3778 exit(1);
3779 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3780 exit(1);
3781 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3782 exit(1);
3783 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3784 exit(1);
3786 /* If no default VGA is requested, the default is "none". */
3787 if (default_vga) {
3788 if (cirrus_vga_available()) {
3789 vga_model = "cirrus";
3790 } else if (vga_available()) {
3791 vga_model = "std";
3794 select_vgahw(vga_model);
3796 if (watchdog) {
3797 i = select_watchdog(watchdog);
3798 if (i > 0)
3799 exit (i == 1 ? 1 : 0);
3802 if (machine->compat_props) {
3803 qdev_prop_register_global_list(machine->compat_props);
3805 qemu_add_globals();
3807 qdev_machine_init();
3809 QEMUMachineInitArgs args = { .ram_size = ram_size,
3810 .boot_device = boot_devices,
3811 .kernel_filename = kernel_filename,
3812 .kernel_cmdline = kernel_cmdline,
3813 .initrd_filename = initrd_filename,
3814 .cpu_model = cpu_model };
3815 machine->init(&args);
3817 cpu_synchronize_all_post_init();
3819 set_numa_modes();
3821 current_machine = machine;
3823 /* init USB devices */
3824 if (usb_enabled(false)) {
3825 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3826 exit(1);
3829 /* init generic devices */
3830 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3831 exit(1);
3833 net_check_clients();
3835 /* just use the first displaystate for the moment */
3836 ds = get_displaystate();
3838 if (using_spice)
3839 display_remote++;
3840 if (display_type == DT_DEFAULT && !display_remote) {
3841 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3842 display_type = DT_SDL;
3843 #elif defined(CONFIG_VNC)
3844 vnc_display = "localhost:0,to=99";
3845 show_vnc_port = 1;
3846 #else
3847 display_type = DT_NONE;
3848 #endif
3852 /* init local displays */
3853 switch (display_type) {
3854 case DT_NOGRAPHIC:
3855 break;
3856 #if defined(CONFIG_CURSES)
3857 case DT_CURSES:
3858 if (!is_daemonized()) {
3859 curses_display_init(ds, full_screen);
3861 break;
3862 #endif
3863 #if defined(CONFIG_SDL)
3864 case DT_SDL:
3865 sdl_display_init(ds, full_screen, no_frame);
3866 break;
3867 #elif defined(CONFIG_COCOA)
3868 case DT_SDL:
3869 cocoa_display_init(ds, full_screen);
3870 break;
3871 #endif
3872 default:
3873 break;
3876 /* must be after terminal init, SDL library changes signal handlers */
3877 os_setup_signal_handling();
3879 #ifdef CONFIG_VNC
3880 /* init remote displays */
3881 if (vnc_display) {
3882 Error *local_err = NULL;
3883 vnc_display_init(ds);
3884 vnc_display_open(ds, vnc_display, &local_err);
3885 if (local_err != NULL) {
3886 fprintf(stderr, "Failed to start VNC server on `%s': %s\n",
3887 vnc_display, error_get_pretty(local_err));
3888 error_free(local_err);
3889 exit(1);
3892 if (show_vnc_port) {
3893 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3896 #endif
3897 #ifdef CONFIG_SPICE
3898 if (using_spice && !qxl_enabled) {
3899 qemu_spice_display_init(ds);
3901 #endif
3903 /* display setup */
3904 text_consoles_set_display(ds);
3906 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3907 exit(1);
3910 qdev_machine_creation_done();
3912 if (rom_load_all() != 0) {
3913 fprintf(stderr, "rom loading failed\n");
3914 exit(1);
3917 /* TODO: once all bus devices are qdevified, this should be done
3918 * when bus is created by qdev.c */
3919 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3920 qemu_run_machine_init_done_notifiers();
3922 qemu_system_reset(VMRESET_SILENT);
3923 if (loadvm) {
3924 if (load_vmstate(loadvm) < 0) {
3925 autostart = 0;
3929 if (incoming) {
3930 Error *local_err = NULL;
3931 qemu_start_incoming_migration(incoming, &local_err);
3932 if (local_err) {
3933 fprintf(stderr, "-incoming %s: %s\n", incoming, error_get_pretty(local_err));
3934 error_free(local_err);
3935 exit(1);
3937 } else if (autostart) {
3938 vm_start();
3941 os_setup_post();
3943 resume_all_vcpus();
3944 main_loop();
3945 bdrv_close_all();
3946 pause_all_vcpus();
3947 net_cleanup();
3948 res_free();
3950 return 0;