Drop the incoming_expected global variable
[qemu.git] / vl.c
blob2fb09f5012704b7312ad158eec377c646289385e
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>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
51 #ifdef CONFIG_BSD
52 #include <sys/stat.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <libutil.h>
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <pty.h>
62 #include <malloc.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
66 #endif
67 #ifdef __sun__
68 #include <sys/stat.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
77 #include <net/if.h>
78 #include <syslog.h>
79 #include <stropts.h>
80 #endif
81 #endif
82 #endif
84 #if defined(__OpenBSD__)
85 #include <util.h>
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 "block.h"
140 #include "blockdev.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 "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #ifdef CONFIG_VIRTFS
151 #include "fsdev/qemu-fsdev.h"
152 #endif
154 #include "disas.h"
156 #include "qemu_socket.h"
158 #include "slirp/libslirp.h"
160 #include "trace.h"
161 #include "trace/control.h"
162 #include "qemu-queue.h"
163 #include "cpus.h"
164 #include "arch_init.h"
166 #include "ui/qemu-spice.h"
168 //#define DEBUG_NET
169 //#define DEBUG_SLIRP
171 #define DEFAULT_RAM_SIZE 128
173 #define MAX_VIRTIO_CONSOLES 1
175 static const char *data_dir;
176 const char *bios_name = NULL;
177 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
178 DisplayType display_type = DT_DEFAULT;
179 int display_remote = 0;
180 const char* keyboard_layout = NULL;
181 ram_addr_t ram_size;
182 const char *mem_path = NULL;
183 #ifdef MAP_POPULATE
184 int mem_prealloc = 0; /* force preallocation of physical target memory */
185 #endif
186 int nb_nics;
187 NICInfo nd_table[MAX_NICS];
188 int vm_running;
189 int autostart;
190 static int rtc_utc = 1;
191 static int rtc_date_offset = -1; /* -1 means no change */
192 QEMUClock *rtc_clock;
193 int vga_interface_type = VGA_NONE;
194 static int full_screen = 0;
195 #ifdef CONFIG_SDL
196 static int no_frame = 0;
197 #endif
198 int no_quit = 0;
199 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
200 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
201 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
202 int win2k_install_hack = 0;
203 int rtc_td_hack = 0;
204 int usb_enabled = 0;
205 int singlestep = 0;
206 int smp_cpus = 1;
207 int max_cpus = 0;
208 int smp_cores = 1;
209 int smp_threads = 1;
210 #ifdef CONFIG_VNC
211 const char *vnc_display;
212 #endif
213 int acpi_enabled = 1;
214 int no_hpet = 0;
215 int fd_bootchk = 1;
216 int no_reboot = 0;
217 int no_shutdown = 0;
218 int cursor_hide = 1;
219 int graphic_rotate = 0;
220 uint8_t irq0override = 1;
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 uint64_t node_cpumask[MAX_NODES];
251 static QEMUTimer *nographic_timer;
253 uint8_t qemu_uuid[16];
255 static QEMUBootSetHandler *boot_set_handler;
256 static void *boot_set_opaque;
258 static NotifierList exit_notifiers =
259 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
261 static NotifierList machine_init_done_notifiers =
262 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
264 static int tcg_allowed = 1;
265 int kvm_allowed = 0;
266 int xen_allowed = 0;
267 uint32_t xen_domid;
268 enum xen_mode xen_mode = XEN_EMULATE;
269 static int tcg_tb_size;
271 static int default_serial = 1;
272 static int default_parallel = 1;
273 static int default_virtcon = 1;
274 static int default_monitor = 1;
275 static int default_vga = 1;
276 static int default_floppy = 1;
277 static int default_cdrom = 1;
278 static int default_sdcard = 1;
280 static struct {
281 const char *driver;
282 int *flag;
283 } default_list[] = {
284 { .driver = "isa-serial", .flag = &default_serial },
285 { .driver = "isa-parallel", .flag = &default_parallel },
286 { .driver = "isa-fdc", .flag = &default_floppy },
287 { .driver = "ide-cd", .flag = &default_cdrom },
288 { .driver = "ide-hd", .flag = &default_cdrom },
289 { .driver = "ide-drive", .flag = &default_cdrom },
290 { .driver = "scsi-cd", .flag = &default_cdrom },
291 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
292 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
293 { .driver = "virtio-serial", .flag = &default_virtcon },
294 { .driver = "VGA", .flag = &default_vga },
295 { .driver = "cirrus-vga", .flag = &default_vga },
296 { .driver = "vmware-svga", .flag = &default_vga },
297 { .driver = "isa-vga", .flag = &default_vga },
298 { .driver = "qxl-vga", .flag = &default_vga },
301 static void res_free(void)
303 if (boot_splash_filedata != NULL) {
304 g_free(boot_splash_filedata);
305 boot_splash_filedata = NULL;
309 static int default_driver_check(QemuOpts *opts, void *opaque)
311 const char *driver = qemu_opt_get(opts, "driver");
312 int i;
314 if (!driver)
315 return 0;
316 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
317 if (strcmp(default_list[i].driver, driver) != 0)
318 continue;
319 *(default_list[i].flag) = 0;
321 return 0;
324 /***********************************************************/
325 /* QEMU state */
327 static RunState current_run_state = RSTATE_NO_STATE;
329 typedef struct {
330 RunState from;
331 RunState to;
332 } RunStateTransition;
334 static const RunStateTransition runstate_transitions_def[] = {
335 /* from -> to */
336 { RSTATE_NO_STATE, RSTATE_RUNNING },
337 { RSTATE_NO_STATE, RSTATE_IN_MIGRATE },
338 { RSTATE_NO_STATE, RSTATE_PRE_LAUNCH },
340 { RSTATE_DEBUG, RSTATE_RUNNING },
342 { RSTATE_IN_MIGRATE, RSTATE_RUNNING },
343 { RSTATE_IN_MIGRATE, RSTATE_PRE_LAUNCH },
345 { RSTATE_PANICKED, RSTATE_PAUSED },
347 { RSTATE_IO_ERROR, RSTATE_RUNNING },
349 { RSTATE_PAUSED, RSTATE_RUNNING },
351 { RSTATE_POST_MIGRATE, RSTATE_RUNNING },
353 { RSTATE_PRE_LAUNCH, RSTATE_RUNNING },
354 { RSTATE_PRE_LAUNCH, RSTATE_POST_MIGRATE },
356 { RSTATE_PRE_MIGRATE, RSTATE_RUNNING },
357 { RSTATE_PRE_MIGRATE, RSTATE_POST_MIGRATE },
359 { RSTATE_RESTORE, RSTATE_RUNNING },
361 { RSTATE_RUNNING, RSTATE_DEBUG },
362 { RSTATE_RUNNING, RSTATE_PANICKED },
363 { RSTATE_RUNNING, RSTATE_IO_ERROR },
364 { RSTATE_RUNNING, RSTATE_PAUSED },
365 { RSTATE_RUNNING, RSTATE_PRE_MIGRATE },
366 { RSTATE_RUNNING, RSTATE_RESTORE },
367 { RSTATE_RUNNING, RSTATE_SAVEVM },
368 { RSTATE_RUNNING, RSTATE_SHUTDOWN },
369 { RSTATE_RUNNING, RSTATE_WATCHDOG },
371 { RSTATE_SAVEVM, RSTATE_RUNNING },
373 { RSTATE_SHUTDOWN, RSTATE_PAUSED },
375 { RSTATE_WATCHDOG, RSTATE_RUNNING },
377 { RSTATE_MAX, RSTATE_MAX },
380 static bool runstate_valid_transitions[RSTATE_MAX][RSTATE_MAX];
382 bool runstate_check(RunState state)
384 return current_run_state == state;
387 void runstate_init(void)
389 const RunStateTransition *p;
391 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
393 for (p = &runstate_transitions_def[0]; p->from != RSTATE_MAX; p++) {
394 runstate_valid_transitions[p->from][p->to] = true;
398 /* This function will abort() on invalid state transitions */
399 void runstate_set(RunState new_state)
401 if (new_state >= RSTATE_MAX ||
402 !runstate_valid_transitions[current_run_state][new_state]) {
403 fprintf(stderr, "invalid runstate transition\n");
404 abort();
407 current_run_state = new_state;
410 /***********************************************************/
411 /* real time host monotonic timer */
413 /***********************************************************/
414 /* host time/date access */
415 void qemu_get_timedate(struct tm *tm, int offset)
417 time_t ti;
418 struct tm *ret;
420 time(&ti);
421 ti += offset;
422 if (rtc_date_offset == -1) {
423 if (rtc_utc)
424 ret = gmtime(&ti);
425 else
426 ret = localtime(&ti);
427 } else {
428 ti -= rtc_date_offset;
429 ret = gmtime(&ti);
432 memcpy(tm, ret, sizeof(struct tm));
435 int qemu_timedate_diff(struct tm *tm)
437 time_t seconds;
439 if (rtc_date_offset == -1)
440 if (rtc_utc)
441 seconds = mktimegm(tm);
442 else
443 seconds = mktime(tm);
444 else
445 seconds = mktimegm(tm) + rtc_date_offset;
447 return seconds - time(NULL);
450 void rtc_change_mon_event(struct tm *tm)
452 QObject *data;
454 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
455 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
456 qobject_decref(data);
459 static void configure_rtc_date_offset(const char *startdate, int legacy)
461 time_t rtc_start_date;
462 struct tm tm;
464 if (!strcmp(startdate, "now") && legacy) {
465 rtc_date_offset = -1;
466 } else {
467 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
468 &tm.tm_year,
469 &tm.tm_mon,
470 &tm.tm_mday,
471 &tm.tm_hour,
472 &tm.tm_min,
473 &tm.tm_sec) == 6) {
474 /* OK */
475 } else if (sscanf(startdate, "%d-%d-%d",
476 &tm.tm_year,
477 &tm.tm_mon,
478 &tm.tm_mday) == 3) {
479 tm.tm_hour = 0;
480 tm.tm_min = 0;
481 tm.tm_sec = 0;
482 } else {
483 goto date_fail;
485 tm.tm_year -= 1900;
486 tm.tm_mon--;
487 rtc_start_date = mktimegm(&tm);
488 if (rtc_start_date == -1) {
489 date_fail:
490 fprintf(stderr, "Invalid date format. Valid formats are:\n"
491 "'2006-06-17T16:01:21' or '2006-06-17'\n");
492 exit(1);
494 rtc_date_offset = time(NULL) - rtc_start_date;
498 static void configure_rtc(QemuOpts *opts)
500 const char *value;
502 value = qemu_opt_get(opts, "base");
503 if (value) {
504 if (!strcmp(value, "utc")) {
505 rtc_utc = 1;
506 } else if (!strcmp(value, "localtime")) {
507 rtc_utc = 0;
508 } else {
509 configure_rtc_date_offset(value, 0);
512 value = qemu_opt_get(opts, "clock");
513 if (value) {
514 if (!strcmp(value, "host")) {
515 rtc_clock = host_clock;
516 } else if (!strcmp(value, "vm")) {
517 rtc_clock = vm_clock;
518 } else {
519 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
520 exit(1);
523 value = qemu_opt_get(opts, "driftfix");
524 if (value) {
525 if (!strcmp(value, "slew")) {
526 rtc_td_hack = 1;
527 } else if (!strcmp(value, "none")) {
528 rtc_td_hack = 0;
529 } else {
530 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
531 exit(1);
536 /***********************************************************/
537 /* Bluetooth support */
538 static int nb_hcis;
539 static int cur_hci;
540 static struct HCIInfo *hci_table[MAX_NICS];
542 static struct bt_vlan_s {
543 struct bt_scatternet_s net;
544 int id;
545 struct bt_vlan_s *next;
546 } *first_bt_vlan;
548 /* find or alloc a new bluetooth "VLAN" */
549 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
551 struct bt_vlan_s **pvlan, *vlan;
552 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
553 if (vlan->id == id)
554 return &vlan->net;
556 vlan = g_malloc0(sizeof(struct bt_vlan_s));
557 vlan->id = id;
558 pvlan = &first_bt_vlan;
559 while (*pvlan != NULL)
560 pvlan = &(*pvlan)->next;
561 *pvlan = vlan;
562 return &vlan->net;
565 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
569 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
571 return -ENOTSUP;
574 static struct HCIInfo null_hci = {
575 .cmd_send = null_hci_send,
576 .sco_send = null_hci_send,
577 .acl_send = null_hci_send,
578 .bdaddr_set = null_hci_addr_set,
581 struct HCIInfo *qemu_next_hci(void)
583 if (cur_hci == nb_hcis)
584 return &null_hci;
586 return hci_table[cur_hci++];
589 static struct HCIInfo *hci_init(const char *str)
591 char *endp;
592 struct bt_scatternet_s *vlan = 0;
594 if (!strcmp(str, "null"))
595 /* null */
596 return &null_hci;
597 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
598 /* host[:hciN] */
599 return bt_host_hci(str[4] ? str + 5 : "hci0");
600 else if (!strncmp(str, "hci", 3)) {
601 /* hci[,vlan=n] */
602 if (str[3]) {
603 if (!strncmp(str + 3, ",vlan=", 6)) {
604 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
605 if (*endp)
606 vlan = 0;
608 } else
609 vlan = qemu_find_bt_vlan(0);
610 if (vlan)
611 return bt_new_hci(vlan);
614 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
616 return 0;
619 static int bt_hci_parse(const char *str)
621 struct HCIInfo *hci;
622 bdaddr_t bdaddr;
624 if (nb_hcis >= MAX_NICS) {
625 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
626 return -1;
629 hci = hci_init(str);
630 if (!hci)
631 return -1;
633 bdaddr.b[0] = 0x52;
634 bdaddr.b[1] = 0x54;
635 bdaddr.b[2] = 0x00;
636 bdaddr.b[3] = 0x12;
637 bdaddr.b[4] = 0x34;
638 bdaddr.b[5] = 0x56 + nb_hcis;
639 hci->bdaddr_set(hci, bdaddr.b);
641 hci_table[nb_hcis++] = hci;
643 return 0;
646 static void bt_vhci_add(int vlan_id)
648 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
650 if (!vlan->slave)
651 fprintf(stderr, "qemu: warning: adding a VHCI to "
652 "an empty scatternet %i\n", vlan_id);
654 bt_vhci_init(bt_new_hci(vlan));
657 static struct bt_device_s *bt_device_add(const char *opt)
659 struct bt_scatternet_s *vlan;
660 int vlan_id = 0;
661 char *endp = strstr(opt, ",vlan=");
662 int len = (endp ? endp - opt : strlen(opt)) + 1;
663 char devname[10];
665 pstrcpy(devname, MIN(sizeof(devname), len), opt);
667 if (endp) {
668 vlan_id = strtol(endp + 6, &endp, 0);
669 if (*endp) {
670 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
671 return 0;
675 vlan = qemu_find_bt_vlan(vlan_id);
677 if (!vlan->slave)
678 fprintf(stderr, "qemu: warning: adding a slave device to "
679 "an empty scatternet %i\n", vlan_id);
681 if (!strcmp(devname, "keyboard"))
682 return bt_keyboard_init(vlan);
684 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
685 return 0;
688 static int bt_parse(const char *opt)
690 const char *endp, *p;
691 int vlan;
693 if (strstart(opt, "hci", &endp)) {
694 if (!*endp || *endp == ',') {
695 if (*endp)
696 if (!strstart(endp, ",vlan=", 0))
697 opt = endp + 1;
699 return bt_hci_parse(opt);
701 } else if (strstart(opt, "vhci", &endp)) {
702 if (!*endp || *endp == ',') {
703 if (*endp) {
704 if (strstart(endp, ",vlan=", &p)) {
705 vlan = strtol(p, (char **) &endp, 0);
706 if (*endp) {
707 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
708 return 1;
710 } else {
711 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
712 return 1;
714 } else
715 vlan = 0;
717 bt_vhci_add(vlan);
718 return 0;
720 } else if (strstart(opt, "device:", &endp))
721 return !bt_device_add(endp);
723 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
724 return 1;
727 /***********************************************************/
728 /* QEMU Block devices */
730 #define HD_OPTS "media=disk"
731 #define CDROM_OPTS "media=cdrom"
732 #define FD_OPTS ""
733 #define PFLASH_OPTS ""
734 #define MTD_OPTS ""
735 #define SD_OPTS ""
737 static int drive_init_func(QemuOpts *opts, void *opaque)
739 int *use_scsi = opaque;
741 return drive_init(opts, *use_scsi) == NULL;
744 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
746 if (NULL == qemu_opt_get(opts, "snapshot")) {
747 qemu_opt_set(opts, "snapshot", "on");
749 return 0;
752 static void default_drive(int enable, int snapshot, int use_scsi,
753 BlockInterfaceType type, int index,
754 const char *optstr)
756 QemuOpts *opts;
758 if (type == IF_DEFAULT) {
759 type = use_scsi ? IF_SCSI : IF_IDE;
762 if (!enable || drive_get_by_index(type, index)) {
763 return;
766 opts = drive_add(type, index, NULL, optstr);
767 if (snapshot) {
768 drive_enable_snapshot(opts, NULL);
770 if (!drive_init(opts, use_scsi)) {
771 exit(1);
775 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
777 boot_set_handler = func;
778 boot_set_opaque = opaque;
781 int qemu_boot_set(const char *boot_devices)
783 if (!boot_set_handler) {
784 return -EINVAL;
786 return boot_set_handler(boot_set_opaque, boot_devices);
789 static void validate_bootdevices(char *devices)
791 /* We just do some generic consistency checks */
792 const char *p;
793 int bitmap = 0;
795 for (p = devices; *p != '\0'; p++) {
796 /* Allowed boot devices are:
797 * a-b: floppy disk drives
798 * c-f: IDE disk drives
799 * g-m: machine implementation dependant drives
800 * n-p: network devices
801 * It's up to each machine implementation to check if the given boot
802 * devices match the actual hardware implementation and firmware
803 * features.
805 if (*p < 'a' || *p > 'p') {
806 fprintf(stderr, "Invalid boot device '%c'\n", *p);
807 exit(1);
809 if (bitmap & (1 << (*p - 'a'))) {
810 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
811 exit(1);
813 bitmap |= 1 << (*p - 'a');
817 static void restore_boot_devices(void *opaque)
819 char *standard_boot_devices = opaque;
820 static int first = 1;
822 /* Restore boot order and remove ourselves after the first boot */
823 if (first) {
824 first = 0;
825 return;
828 qemu_boot_set(standard_boot_devices);
830 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
831 g_free(standard_boot_devices);
834 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
835 const char *suffix)
837 FWBootEntry *node, *i;
839 if (bootindex < 0) {
840 return;
843 assert(dev != NULL || suffix != NULL);
845 node = g_malloc0(sizeof(FWBootEntry));
846 node->bootindex = bootindex;
847 node->suffix = suffix ? g_strdup(suffix) : NULL;
848 node->dev = dev;
850 QTAILQ_FOREACH(i, &fw_boot_order, link) {
851 if (i->bootindex == bootindex) {
852 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
853 exit(1);
854 } else if (i->bootindex < bootindex) {
855 continue;
857 QTAILQ_INSERT_BEFORE(i, node, link);
858 return;
860 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
864 * This function returns null terminated string that consist of new line
865 * separated device paths.
867 * memory pointed by "size" is assigned total length of the array in bytes
870 char *get_boot_devices_list(uint32_t *size)
872 FWBootEntry *i;
873 uint32_t total = 0;
874 char *list = NULL;
876 QTAILQ_FOREACH(i, &fw_boot_order, link) {
877 char *devpath = NULL, *bootpath;
878 int len;
880 if (i->dev) {
881 devpath = qdev_get_fw_dev_path(i->dev);
882 assert(devpath);
885 if (i->suffix && devpath) {
886 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
888 bootpath = g_malloc(bootpathlen);
889 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
890 g_free(devpath);
891 } else if (devpath) {
892 bootpath = devpath;
893 } else {
894 bootpath = g_strdup(i->suffix);
895 assert(bootpath);
898 if (total) {
899 list[total-1] = '\n';
901 len = strlen(bootpath) + 1;
902 list = g_realloc(list, total + len);
903 memcpy(&list[total], bootpath, len);
904 total += len;
905 g_free(bootpath);
908 *size = total;
910 return list;
913 static void numa_add(const char *optarg)
915 char option[128];
916 char *endptr;
917 unsigned long long value, endvalue;
918 int nodenr;
920 optarg = get_opt_name(option, 128, optarg, ',') + 1;
921 if (!strcmp(option, "node")) {
922 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
923 nodenr = nb_numa_nodes;
924 } else {
925 nodenr = strtoull(option, NULL, 10);
928 if (get_param_value(option, 128, "mem", optarg) == 0) {
929 node_mem[nodenr] = 0;
930 } else {
931 int64_t sval;
932 sval = strtosz(option, NULL);
933 if (sval < 0) {
934 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
935 exit(1);
937 node_mem[nodenr] = sval;
939 if (get_param_value(option, 128, "cpus", optarg) == 0) {
940 node_cpumask[nodenr] = 0;
941 } else {
942 value = strtoull(option, &endptr, 10);
943 if (value >= 64) {
944 value = 63;
945 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
946 } else {
947 if (*endptr == '-') {
948 endvalue = strtoull(endptr+1, &endptr, 10);
949 if (endvalue >= 63) {
950 endvalue = 62;
951 fprintf(stderr,
952 "only 63 CPUs in NUMA mode supported.\n");
954 value = (2ULL << endvalue) - (1ULL << value);
955 } else {
956 value = 1ULL << value;
959 node_cpumask[nodenr] = value;
961 nb_numa_nodes++;
963 return;
966 static void smp_parse(const char *optarg)
968 int smp, sockets = 0, threads = 0, cores = 0;
969 char *endptr;
970 char option[128];
972 smp = strtoul(optarg, &endptr, 10);
973 if (endptr != optarg) {
974 if (*endptr == ',') {
975 endptr++;
978 if (get_param_value(option, 128, "sockets", endptr) != 0)
979 sockets = strtoull(option, NULL, 10);
980 if (get_param_value(option, 128, "cores", endptr) != 0)
981 cores = strtoull(option, NULL, 10);
982 if (get_param_value(option, 128, "threads", endptr) != 0)
983 threads = strtoull(option, NULL, 10);
984 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
985 max_cpus = strtoull(option, NULL, 10);
987 /* compute missing values, prefer sockets over cores over threads */
988 if (smp == 0 || sockets == 0) {
989 sockets = sockets > 0 ? sockets : 1;
990 cores = cores > 0 ? cores : 1;
991 threads = threads > 0 ? threads : 1;
992 if (smp == 0) {
993 smp = cores * threads * sockets;
995 } else {
996 if (cores == 0) {
997 threads = threads > 0 ? threads : 1;
998 cores = smp / (sockets * threads);
999 } else {
1000 threads = smp / (cores * sockets);
1003 smp_cpus = smp;
1004 smp_cores = cores > 0 ? cores : 1;
1005 smp_threads = threads > 0 ? threads : 1;
1006 if (max_cpus == 0)
1007 max_cpus = smp_cpus;
1010 /***********************************************************/
1011 /* USB devices */
1013 static int usb_device_add(const char *devname)
1015 const char *p;
1016 USBDevice *dev = NULL;
1018 if (!usb_enabled)
1019 return -1;
1021 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1022 dev = usbdevice_create(devname);
1023 if (dev)
1024 goto done;
1026 /* the other ones */
1027 #ifndef CONFIG_LINUX
1028 /* only the linux version is qdev-ified, usb-bsd still needs this */
1029 if (strstart(devname, "host:", &p)) {
1030 dev = usb_host_device_open(p);
1031 } else
1032 #endif
1033 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1034 dev = usb_bt_init(devname[2] ? hci_init(p) :
1035 bt_new_hci(qemu_find_bt_vlan(0)));
1036 } else {
1037 return -1;
1039 if (!dev)
1040 return -1;
1042 done:
1043 return 0;
1046 static int usb_device_del(const char *devname)
1048 int bus_num, addr;
1049 const char *p;
1051 if (strstart(devname, "host:", &p))
1052 return usb_host_device_close(p);
1054 if (!usb_enabled)
1055 return -1;
1057 p = strchr(devname, '.');
1058 if (!p)
1059 return -1;
1060 bus_num = strtoul(devname, NULL, 0);
1061 addr = strtoul(p + 1, NULL, 0);
1063 return usb_device_delete_addr(bus_num, addr);
1066 static int usb_parse(const char *cmdline)
1068 int r;
1069 r = usb_device_add(cmdline);
1070 if (r < 0) {
1071 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1073 return r;
1076 void do_usb_add(Monitor *mon, const QDict *qdict)
1078 const char *devname = qdict_get_str(qdict, "devname");
1079 if (usb_device_add(devname) < 0) {
1080 error_report("could not add USB device '%s'", devname);
1084 void do_usb_del(Monitor *mon, const QDict *qdict)
1086 const char *devname = qdict_get_str(qdict, "devname");
1087 if (usb_device_del(devname) < 0) {
1088 error_report("could not delete USB device '%s'", devname);
1092 /***********************************************************/
1093 /* PCMCIA/Cardbus */
1095 static struct pcmcia_socket_entry_s {
1096 PCMCIASocket *socket;
1097 struct pcmcia_socket_entry_s *next;
1098 } *pcmcia_sockets = 0;
1100 void pcmcia_socket_register(PCMCIASocket *socket)
1102 struct pcmcia_socket_entry_s *entry;
1104 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1105 entry->socket = socket;
1106 entry->next = pcmcia_sockets;
1107 pcmcia_sockets = entry;
1110 void pcmcia_socket_unregister(PCMCIASocket *socket)
1112 struct pcmcia_socket_entry_s *entry, **ptr;
1114 ptr = &pcmcia_sockets;
1115 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1116 if (entry->socket == socket) {
1117 *ptr = entry->next;
1118 g_free(entry);
1122 void pcmcia_info(Monitor *mon)
1124 struct pcmcia_socket_entry_s *iter;
1126 if (!pcmcia_sockets)
1127 monitor_printf(mon, "No PCMCIA sockets\n");
1129 for (iter = pcmcia_sockets; iter; iter = iter->next)
1130 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1131 iter->socket->attached ? iter->socket->card_string :
1132 "Empty");
1135 /***********************************************************/
1136 /* machine registration */
1138 static QEMUMachine *first_machine = NULL;
1139 QEMUMachine *current_machine = NULL;
1141 int qemu_register_machine(QEMUMachine *m)
1143 QEMUMachine **pm;
1144 pm = &first_machine;
1145 while (*pm != NULL)
1146 pm = &(*pm)->next;
1147 m->next = NULL;
1148 *pm = m;
1149 return 0;
1152 static QEMUMachine *find_machine(const char *name)
1154 QEMUMachine *m;
1156 for(m = first_machine; m != NULL; m = m->next) {
1157 if (!strcmp(m->name, name))
1158 return m;
1159 if (m->alias && !strcmp(m->alias, name))
1160 return m;
1162 return NULL;
1165 static QEMUMachine *find_default_machine(void)
1167 QEMUMachine *m;
1169 for(m = first_machine; m != NULL; m = m->next) {
1170 if (m->is_default) {
1171 return m;
1174 return NULL;
1177 /***********************************************************/
1178 /* main execution loop */
1180 static void gui_update(void *opaque)
1182 uint64_t interval = GUI_REFRESH_INTERVAL;
1183 DisplayState *ds = opaque;
1184 DisplayChangeListener *dcl = ds->listeners;
1186 qemu_flush_coalesced_mmio_buffer();
1187 dpy_refresh(ds);
1189 while (dcl != NULL) {
1190 if (dcl->gui_timer_interval &&
1191 dcl->gui_timer_interval < interval)
1192 interval = dcl->gui_timer_interval;
1193 dcl = dcl->next;
1195 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1198 static void nographic_update(void *opaque)
1200 uint64_t interval = GUI_REFRESH_INTERVAL;
1202 qemu_flush_coalesced_mmio_buffer();
1203 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1206 struct vm_change_state_entry {
1207 VMChangeStateHandler *cb;
1208 void *opaque;
1209 QLIST_ENTRY (vm_change_state_entry) entries;
1212 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1214 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1215 void *opaque)
1217 VMChangeStateEntry *e;
1219 e = g_malloc0(sizeof (*e));
1221 e->cb = cb;
1222 e->opaque = opaque;
1223 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1224 return e;
1227 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1229 QLIST_REMOVE (e, entries);
1230 g_free (e);
1233 void vm_state_notify(int running, RunState state)
1235 VMChangeStateEntry *e;
1237 trace_vm_state_notify(running, state);
1239 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1240 e->cb(e->opaque, running, state);
1244 void vm_start(void)
1246 if (!vm_running) {
1247 cpu_enable_ticks();
1248 vm_running = 1;
1249 runstate_set(RSTATE_RUNNING);
1250 vm_state_notify(1, RSTATE_RUNNING);
1251 resume_all_vcpus();
1252 monitor_protocol_event(QEVENT_RESUME, NULL);
1256 /* reset/shutdown handler */
1258 typedef struct QEMUResetEntry {
1259 QTAILQ_ENTRY(QEMUResetEntry) entry;
1260 QEMUResetHandler *func;
1261 void *opaque;
1262 } QEMUResetEntry;
1264 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1265 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1266 static int reset_requested;
1267 static int shutdown_requested, shutdown_signal = -1;
1268 static pid_t shutdown_pid;
1269 static int powerdown_requested;
1270 static int debug_requested;
1271 static RunState vmstop_requested = RSTATE_NO_STATE;
1273 int qemu_shutdown_requested_get(void)
1275 return shutdown_requested;
1278 int qemu_reset_requested_get(void)
1280 return reset_requested;
1283 int qemu_shutdown_requested(void)
1285 int r = shutdown_requested;
1286 shutdown_requested = 0;
1287 return r;
1290 void qemu_kill_report(void)
1292 if (shutdown_signal != -1) {
1293 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1294 if (shutdown_pid == 0) {
1295 /* This happens for eg ^C at the terminal, so it's worth
1296 * avoiding printing an odd message in that case.
1298 fputc('\n', stderr);
1299 } else {
1300 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1302 shutdown_signal = -1;
1306 int qemu_reset_requested(void)
1308 int r = reset_requested;
1309 reset_requested = 0;
1310 return r;
1313 int qemu_powerdown_requested(void)
1315 int r = powerdown_requested;
1316 powerdown_requested = 0;
1317 return r;
1320 static int qemu_debug_requested(void)
1322 int r = debug_requested;
1323 debug_requested = 0;
1324 return r;
1327 static RunState qemu_vmstop_requested(void)
1329 RunState s = vmstop_requested;
1330 vmstop_requested = RSTATE_NO_STATE;
1331 return s;
1334 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1336 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1338 re->func = func;
1339 re->opaque = opaque;
1340 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1343 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1345 QEMUResetEntry *re;
1347 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1348 if (re->func == func && re->opaque == opaque) {
1349 QTAILQ_REMOVE(&reset_handlers, re, entry);
1350 g_free(re);
1351 return;
1356 void qemu_system_reset(bool report)
1358 QEMUResetEntry *re, *nre;
1360 /* reset all devices */
1361 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1362 re->func(re->opaque);
1364 if (report) {
1365 monitor_protocol_event(QEVENT_RESET, NULL);
1367 cpu_synchronize_all_post_reset();
1370 void qemu_system_reset_request(void)
1372 if (no_reboot) {
1373 shutdown_requested = 1;
1374 } else {
1375 reset_requested = 1;
1377 cpu_stop_current();
1378 qemu_notify_event();
1381 void qemu_system_killed(int signal, pid_t pid)
1383 shutdown_signal = signal;
1384 shutdown_pid = pid;
1385 qemu_system_shutdown_request();
1388 void qemu_system_shutdown_request(void)
1390 shutdown_requested = 1;
1391 qemu_notify_event();
1394 void qemu_system_powerdown_request(void)
1396 powerdown_requested = 1;
1397 qemu_notify_event();
1400 void qemu_system_debug_request(void)
1402 debug_requested = 1;
1403 qemu_notify_event();
1406 void qemu_system_vmstop_request(RunState state)
1408 vmstop_requested = state;
1409 qemu_notify_event();
1412 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1413 static int n_poll_fds;
1414 static int max_priority;
1416 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1417 fd_set *xfds, struct timeval *tv)
1419 GMainContext *context = g_main_context_default();
1420 int i;
1421 int timeout = 0, cur_timeout;
1423 g_main_context_prepare(context, &max_priority);
1425 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1426 poll_fds, ARRAY_SIZE(poll_fds));
1427 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1429 for (i = 0; i < n_poll_fds; i++) {
1430 GPollFD *p = &poll_fds[i];
1432 if ((p->events & G_IO_IN)) {
1433 FD_SET(p->fd, rfds);
1434 *max_fd = MAX(*max_fd, p->fd);
1436 if ((p->events & G_IO_OUT)) {
1437 FD_SET(p->fd, wfds);
1438 *max_fd = MAX(*max_fd, p->fd);
1440 if ((p->events & G_IO_ERR)) {
1441 FD_SET(p->fd, xfds);
1442 *max_fd = MAX(*max_fd, p->fd);
1446 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1447 if (timeout >= 0 && timeout < cur_timeout) {
1448 tv->tv_sec = timeout / 1000;
1449 tv->tv_usec = (timeout % 1000) * 1000;
1453 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1454 bool err)
1456 GMainContext *context = g_main_context_default();
1458 if (!err) {
1459 int i;
1461 for (i = 0; i < n_poll_fds; i++) {
1462 GPollFD *p = &poll_fds[i];
1464 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1465 p->revents |= G_IO_IN;
1467 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1468 p->revents |= G_IO_OUT;
1470 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1471 p->revents |= G_IO_ERR;
1476 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1477 g_main_context_dispatch(context);
1481 int main_loop_wait(int nonblocking)
1483 fd_set rfds, wfds, xfds;
1484 int ret, nfds;
1485 struct timeval tv;
1486 int timeout;
1488 if (nonblocking)
1489 timeout = 0;
1490 else {
1491 timeout = qemu_calculate_timeout();
1492 qemu_bh_update_timeout(&timeout);
1495 os_host_main_loop_wait(&timeout);
1497 tv.tv_sec = timeout / 1000;
1498 tv.tv_usec = (timeout % 1000) * 1000;
1500 /* poll any events */
1501 /* XXX: separate device handlers from system ones */
1502 nfds = -1;
1503 FD_ZERO(&rfds);
1504 FD_ZERO(&wfds);
1505 FD_ZERO(&xfds);
1507 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1508 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1509 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
1511 if (timeout > 0) {
1512 qemu_mutex_unlock_iothread();
1515 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1517 if (timeout > 0) {
1518 qemu_mutex_lock_iothread();
1521 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1522 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1523 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1525 qemu_run_all_timers();
1527 /* Check bottom-halves last in case any of the earlier events triggered
1528 them. */
1529 qemu_bh_poll();
1531 return ret;
1534 qemu_irq qemu_system_powerdown;
1536 static void main_loop(void)
1538 bool nonblocking;
1539 int last_io __attribute__ ((unused)) = 0;
1540 #ifdef CONFIG_PROFILER
1541 int64_t ti;
1542 #endif
1543 int r;
1545 qemu_main_loop_start();
1547 for (;;) {
1548 nonblocking = !kvm_enabled() && last_io > 0;
1549 #ifdef CONFIG_PROFILER
1550 ti = profile_getclock();
1551 #endif
1552 last_io = main_loop_wait(nonblocking);
1553 #ifdef CONFIG_PROFILER
1554 dev_time += profile_getclock() - ti;
1555 #endif
1557 if (qemu_debug_requested()) {
1558 vm_stop(RSTATE_DEBUG);
1560 if (qemu_shutdown_requested()) {
1561 qemu_kill_report();
1562 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1563 if (no_shutdown) {
1564 vm_stop(RSTATE_SHUTDOWN);
1565 } else
1566 break;
1568 if (qemu_reset_requested()) {
1569 pause_all_vcpus();
1570 cpu_synchronize_all_states();
1571 qemu_system_reset(VMRESET_REPORT);
1572 resume_all_vcpus();
1574 if (qemu_powerdown_requested()) {
1575 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1576 qemu_irq_raise(qemu_system_powerdown);
1578 if ((r = qemu_vmstop_requested())) {
1579 vm_stop(r);
1582 bdrv_close_all();
1583 pause_all_vcpus();
1586 static void version(void)
1588 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1591 static void help(int exitcode)
1593 const char *options_help =
1594 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1595 opt_help
1596 #define DEFHEADING(text) stringify(text) "\n"
1597 #include "qemu-options.def"
1598 #undef DEF
1599 #undef DEFHEADING
1600 #undef GEN_DOCS
1602 version();
1603 printf("usage: %s [options] [disk_image]\n"
1604 "\n"
1605 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1606 "\n"
1607 "%s\n"
1608 "During emulation, the following keys are useful:\n"
1609 "ctrl-alt-f toggle full screen\n"
1610 "ctrl-alt-n switch to virtual console 'n'\n"
1611 "ctrl-alt toggle mouse and keyboard grab\n"
1612 "\n"
1613 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1614 "qemu",
1615 options_help);
1616 exit(exitcode);
1619 #define HAS_ARG 0x0001
1621 typedef struct QEMUOption {
1622 const char *name;
1623 int flags;
1624 int index;
1625 uint32_t arch_mask;
1626 } QEMUOption;
1628 static const QEMUOption qemu_options[] = {
1629 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1630 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1631 { option, opt_arg, opt_enum, arch_mask },
1632 #define DEFHEADING(text)
1633 #include "qemu-options.def"
1634 #undef DEF
1635 #undef DEFHEADING
1636 #undef GEN_DOCS
1637 { NULL },
1639 static void select_vgahw (const char *p)
1641 const char *opts;
1643 default_vga = 0;
1644 vga_interface_type = VGA_NONE;
1645 if (strstart(p, "std", &opts)) {
1646 vga_interface_type = VGA_STD;
1647 } else if (strstart(p, "cirrus", &opts)) {
1648 vga_interface_type = VGA_CIRRUS;
1649 } else if (strstart(p, "vmware", &opts)) {
1650 vga_interface_type = VGA_VMWARE;
1651 } else if (strstart(p, "xenfb", &opts)) {
1652 vga_interface_type = VGA_XENFB;
1653 } else if (strstart(p, "qxl", &opts)) {
1654 vga_interface_type = VGA_QXL;
1655 } else if (!strstart(p, "none", &opts)) {
1656 invalid_vga:
1657 fprintf(stderr, "Unknown vga type: %s\n", p);
1658 exit(1);
1660 while (*opts) {
1661 const char *nextopt;
1663 if (strstart(opts, ",retrace=", &nextopt)) {
1664 opts = nextopt;
1665 if (strstart(opts, "dumb", &nextopt))
1666 vga_retrace_method = VGA_RETRACE_DUMB;
1667 else if (strstart(opts, "precise", &nextopt))
1668 vga_retrace_method = VGA_RETRACE_PRECISE;
1669 else goto invalid_vga;
1670 } else goto invalid_vga;
1671 opts = nextopt;
1675 static DisplayType select_display(const char *p)
1677 const char *opts;
1678 DisplayType display = DT_DEFAULT;
1680 if (strstart(p, "sdl", &opts)) {
1681 #ifdef CONFIG_SDL
1682 display = DT_SDL;
1683 while (*opts) {
1684 const char *nextopt;
1686 if (strstart(opts, ",frame=", &nextopt)) {
1687 opts = nextopt;
1688 if (strstart(opts, "on", &nextopt)) {
1689 no_frame = 0;
1690 } else if (strstart(opts, "off", &nextopt)) {
1691 no_frame = 1;
1692 } else {
1693 goto invalid_sdl_args;
1695 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1696 opts = nextopt;
1697 if (strstart(opts, "on", &nextopt)) {
1698 alt_grab = 1;
1699 } else if (strstart(opts, "off", &nextopt)) {
1700 alt_grab = 0;
1701 } else {
1702 goto invalid_sdl_args;
1704 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1705 opts = nextopt;
1706 if (strstart(opts, "on", &nextopt)) {
1707 ctrl_grab = 1;
1708 } else if (strstart(opts, "off", &nextopt)) {
1709 ctrl_grab = 0;
1710 } else {
1711 goto invalid_sdl_args;
1713 } else if (strstart(opts, ",window_close=", &nextopt)) {
1714 opts = nextopt;
1715 if (strstart(opts, "on", &nextopt)) {
1716 no_quit = 0;
1717 } else if (strstart(opts, "off", &nextopt)) {
1718 no_quit = 1;
1719 } else {
1720 goto invalid_sdl_args;
1722 } else {
1723 invalid_sdl_args:
1724 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1725 exit(1);
1727 opts = nextopt;
1729 #else
1730 fprintf(stderr, "SDL support is disabled\n");
1731 exit(1);
1732 #endif
1733 } else if (strstart(p, "vnc", &opts)) {
1734 #ifdef CONFIG_VNC
1735 display_remote++;
1737 if (*opts) {
1738 const char *nextopt;
1740 if (strstart(opts, "=", &nextopt)) {
1741 vnc_display = nextopt;
1744 if (!vnc_display) {
1745 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1746 exit(1);
1748 #else
1749 fprintf(stderr, "VNC support is disabled\n");
1750 exit(1);
1751 #endif
1752 } else if (strstart(p, "curses", &opts)) {
1753 #ifdef CONFIG_CURSES
1754 display = DT_CURSES;
1755 #else
1756 fprintf(stderr, "Curses support is disabled\n");
1757 exit(1);
1758 #endif
1759 } else if (strstart(p, "none", &opts)) {
1760 display = DT_NONE;
1761 } else {
1762 fprintf(stderr, "Unknown display type: %s\n", p);
1763 exit(1);
1766 return display;
1769 static int balloon_parse(const char *arg)
1771 QemuOpts *opts;
1773 if (strcmp(arg, "none") == 0) {
1774 return 0;
1777 if (!strncmp(arg, "virtio", 6)) {
1778 if (arg[6] == ',') {
1779 /* have params -> parse them */
1780 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1781 if (!opts)
1782 return -1;
1783 } else {
1784 /* create empty opts */
1785 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1787 qemu_opt_set(opts, "driver", "virtio-balloon");
1788 return 0;
1791 return -1;
1794 char *qemu_find_file(int type, const char *name)
1796 int len;
1797 const char *subdir;
1798 char *buf;
1800 /* If name contains path separators then try it as a straight path. */
1801 if ((strchr(name, '/') || strchr(name, '\\'))
1802 && access(name, R_OK) == 0) {
1803 return g_strdup(name);
1805 switch (type) {
1806 case QEMU_FILE_TYPE_BIOS:
1807 subdir = "";
1808 break;
1809 case QEMU_FILE_TYPE_KEYMAP:
1810 subdir = "keymaps/";
1811 break;
1812 default:
1813 abort();
1815 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1816 buf = g_malloc0(len);
1817 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1818 if (access(buf, R_OK)) {
1819 g_free(buf);
1820 return NULL;
1822 return buf;
1825 static int device_help_func(QemuOpts *opts, void *opaque)
1827 return qdev_device_help(opts);
1830 static int device_init_func(QemuOpts *opts, void *opaque)
1832 DeviceState *dev;
1834 dev = qdev_device_add(opts);
1835 if (!dev)
1836 return -1;
1837 return 0;
1840 static int chardev_init_func(QemuOpts *opts, void *opaque)
1842 CharDriverState *chr;
1844 chr = qemu_chr_new_from_opts(opts, NULL);
1845 if (!chr)
1846 return -1;
1847 return 0;
1850 #ifdef CONFIG_VIRTFS
1851 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1853 int ret;
1854 ret = qemu_fsdev_add(opts);
1856 return ret;
1858 #endif
1860 static int mon_init_func(QemuOpts *opts, void *opaque)
1862 CharDriverState *chr;
1863 const char *chardev;
1864 const char *mode;
1865 int flags;
1867 mode = qemu_opt_get(opts, "mode");
1868 if (mode == NULL) {
1869 mode = "readline";
1871 if (strcmp(mode, "readline") == 0) {
1872 flags = MONITOR_USE_READLINE;
1873 } else if (strcmp(mode, "control") == 0) {
1874 flags = MONITOR_USE_CONTROL;
1875 } else {
1876 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1877 exit(1);
1880 if (qemu_opt_get_bool(opts, "pretty", 0))
1881 flags |= MONITOR_USE_PRETTY;
1883 if (qemu_opt_get_bool(opts, "default", 0))
1884 flags |= MONITOR_IS_DEFAULT;
1886 chardev = qemu_opt_get(opts, "chardev");
1887 chr = qemu_chr_find(chardev);
1888 if (chr == NULL) {
1889 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1890 exit(1);
1893 monitor_init(chr, flags);
1894 return 0;
1897 static void monitor_parse(const char *optarg, const char *mode)
1899 static int monitor_device_index = 0;
1900 QemuOpts *opts;
1901 const char *p;
1902 char label[32];
1903 int def = 0;
1905 if (strstart(optarg, "chardev:", &p)) {
1906 snprintf(label, sizeof(label), "%s", p);
1907 } else {
1908 snprintf(label, sizeof(label), "compat_monitor%d",
1909 monitor_device_index);
1910 if (monitor_device_index == 0) {
1911 def = 1;
1913 opts = qemu_chr_parse_compat(label, optarg);
1914 if (!opts) {
1915 fprintf(stderr, "parse error: %s\n", optarg);
1916 exit(1);
1920 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1921 if (!opts) {
1922 fprintf(stderr, "duplicate chardev: %s\n", label);
1923 exit(1);
1925 qemu_opt_set(opts, "mode", mode);
1926 qemu_opt_set(opts, "chardev", label);
1927 if (def)
1928 qemu_opt_set(opts, "default", "on");
1929 monitor_device_index++;
1932 struct device_config {
1933 enum {
1934 DEV_USB, /* -usbdevice */
1935 DEV_BT, /* -bt */
1936 DEV_SERIAL, /* -serial */
1937 DEV_PARALLEL, /* -parallel */
1938 DEV_VIRTCON, /* -virtioconsole */
1939 DEV_DEBUGCON, /* -debugcon */
1940 } type;
1941 const char *cmdline;
1942 QTAILQ_ENTRY(device_config) next;
1944 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1946 static void add_device_config(int type, const char *cmdline)
1948 struct device_config *conf;
1950 conf = g_malloc0(sizeof(*conf));
1951 conf->type = type;
1952 conf->cmdline = cmdline;
1953 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1956 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1958 struct device_config *conf;
1959 int rc;
1961 QTAILQ_FOREACH(conf, &device_configs, next) {
1962 if (conf->type != type)
1963 continue;
1964 rc = func(conf->cmdline);
1965 if (0 != rc)
1966 return rc;
1968 return 0;
1971 static int serial_parse(const char *devname)
1973 static int index = 0;
1974 char label[32];
1976 if (strcmp(devname, "none") == 0)
1977 return 0;
1978 if (index == MAX_SERIAL_PORTS) {
1979 fprintf(stderr, "qemu: too many serial ports\n");
1980 exit(1);
1982 snprintf(label, sizeof(label), "serial%d", index);
1983 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1984 if (!serial_hds[index]) {
1985 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1986 devname, strerror(errno));
1987 return -1;
1989 index++;
1990 return 0;
1993 static int parallel_parse(const char *devname)
1995 static int index = 0;
1996 char label[32];
1998 if (strcmp(devname, "none") == 0)
1999 return 0;
2000 if (index == MAX_PARALLEL_PORTS) {
2001 fprintf(stderr, "qemu: too many parallel ports\n");
2002 exit(1);
2004 snprintf(label, sizeof(label), "parallel%d", index);
2005 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2006 if (!parallel_hds[index]) {
2007 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2008 devname, strerror(errno));
2009 return -1;
2011 index++;
2012 return 0;
2015 static int virtcon_parse(const char *devname)
2017 QemuOptsList *device = qemu_find_opts("device");
2018 static int index = 0;
2019 char label[32];
2020 QemuOpts *bus_opts, *dev_opts;
2022 if (strcmp(devname, "none") == 0)
2023 return 0;
2024 if (index == MAX_VIRTIO_CONSOLES) {
2025 fprintf(stderr, "qemu: too many virtio consoles\n");
2026 exit(1);
2029 bus_opts = qemu_opts_create(device, NULL, 0);
2030 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2032 dev_opts = qemu_opts_create(device, NULL, 0);
2033 qemu_opt_set(dev_opts, "driver", "virtconsole");
2035 snprintf(label, sizeof(label), "virtcon%d", index);
2036 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2037 if (!virtcon_hds[index]) {
2038 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2039 devname, strerror(errno));
2040 return -1;
2042 qemu_opt_set(dev_opts, "chardev", label);
2044 index++;
2045 return 0;
2048 static int debugcon_parse(const char *devname)
2050 QemuOpts *opts;
2052 if (!qemu_chr_new("debugcon", devname, NULL)) {
2053 exit(1);
2055 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2056 if (!opts) {
2057 fprintf(stderr, "qemu: already have a debugcon device\n");
2058 exit(1);
2060 qemu_opt_set(opts, "driver", "isa-debugcon");
2061 qemu_opt_set(opts, "chardev", "debugcon");
2062 return 0;
2065 static QEMUMachine *machine_parse(const char *name)
2067 QEMUMachine *m, *machine = NULL;
2069 if (name) {
2070 machine = find_machine(name);
2072 if (machine) {
2073 return machine;
2075 printf("Supported machines are:\n");
2076 for (m = first_machine; m != NULL; m = m->next) {
2077 if (m->alias) {
2078 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2080 printf("%-10s %s%s\n", m->name, m->desc,
2081 m->is_default ? " (default)" : "");
2083 exit(!name || *name != '?');
2086 static int tcg_init(void)
2088 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2089 return 0;
2092 static struct {
2093 const char *opt_name;
2094 const char *name;
2095 int (*available)(void);
2096 int (*init)(void);
2097 int *allowed;
2098 } accel_list[] = {
2099 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2100 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2101 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2104 static int configure_accelerator(void)
2106 const char *p = NULL;
2107 char buf[10];
2108 int i, ret;
2109 bool accel_initalised = 0;
2110 bool init_failed = 0;
2112 QemuOptsList *list = qemu_find_opts("machine");
2113 if (!QTAILQ_EMPTY(&list->head)) {
2114 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2117 if (p == NULL) {
2118 /* Use the default "accelerator", tcg */
2119 p = "tcg";
2122 while (!accel_initalised && *p != '\0') {
2123 if (*p == ':') {
2124 p++;
2126 p = get_opt_name(buf, sizeof (buf), p, ':');
2127 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2128 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2129 *(accel_list[i].allowed) = 1;
2130 ret = accel_list[i].init();
2131 if (ret < 0) {
2132 init_failed = 1;
2133 if (!accel_list[i].available()) {
2134 printf("%s not supported for this target\n",
2135 accel_list[i].name);
2136 } else {
2137 fprintf(stderr, "failed to initialize %s: %s\n",
2138 accel_list[i].name,
2139 strerror(-ret));
2141 *(accel_list[i].allowed) = 0;
2142 } else {
2143 accel_initalised = 1;
2145 break;
2148 if (i == ARRAY_SIZE(accel_list)) {
2149 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2153 if (!accel_initalised) {
2154 fprintf(stderr, "No accelerator found!\n");
2155 exit(1);
2158 if (init_failed) {
2159 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2162 return !accel_initalised;
2165 void qemu_add_exit_notifier(Notifier *notify)
2167 notifier_list_add(&exit_notifiers, notify);
2170 void qemu_remove_exit_notifier(Notifier *notify)
2172 notifier_list_remove(&exit_notifiers, notify);
2175 static void qemu_run_exit_notifiers(void)
2177 notifier_list_notify(&exit_notifiers, NULL);
2180 void qemu_add_machine_init_done_notifier(Notifier *notify)
2182 notifier_list_add(&machine_init_done_notifiers, notify);
2185 static void qemu_run_machine_init_done_notifiers(void)
2187 notifier_list_notify(&machine_init_done_notifiers, NULL);
2190 static const QEMUOption *lookup_opt(int argc, char **argv,
2191 const char **poptarg, int *poptind)
2193 const QEMUOption *popt;
2194 int optind = *poptind;
2195 char *r = argv[optind];
2196 const char *optarg;
2198 loc_set_cmdline(argv, optind, 1);
2199 optind++;
2200 /* Treat --foo the same as -foo. */
2201 if (r[1] == '-')
2202 r++;
2203 popt = qemu_options;
2204 for(;;) {
2205 if (!popt->name) {
2206 error_report("invalid option");
2207 exit(1);
2209 if (!strcmp(popt->name, r + 1))
2210 break;
2211 popt++;
2213 if (popt->flags & HAS_ARG) {
2214 if (optind >= argc) {
2215 error_report("requires an argument");
2216 exit(1);
2218 optarg = argv[optind++];
2219 loc_set_cmdline(argv, optind - 2, 2);
2220 } else {
2221 optarg = NULL;
2224 *poptarg = optarg;
2225 *poptind = optind;
2227 return popt;
2230 static gpointer malloc_and_trace(gsize n_bytes)
2232 void *ptr = malloc(n_bytes);
2233 trace_g_malloc(n_bytes, ptr);
2234 return ptr;
2237 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2239 void *ptr = realloc(mem, n_bytes);
2240 trace_g_realloc(mem, n_bytes, ptr);
2241 return ptr;
2244 static void free_and_trace(gpointer mem)
2246 trace_g_free(mem);
2247 free(mem);
2250 int main(int argc, char **argv, char **envp)
2252 const char *gdbstub_dev = NULL;
2253 int i;
2254 int snapshot, linux_boot;
2255 const char *icount_option = NULL;
2256 const char *initrd_filename;
2257 const char *kernel_filename, *kernel_cmdline;
2258 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2259 DisplayState *ds;
2260 DisplayChangeListener *dcl;
2261 int cyls, heads, secs, translation;
2262 QemuOpts *hda_opts = NULL, *opts;
2263 QemuOptsList *olist;
2264 int optind;
2265 const char *optarg;
2266 const char *loadvm = NULL;
2267 QEMUMachine *machine;
2268 const char *cpu_model;
2269 const char *pid_file = NULL;
2270 const char *incoming = NULL;
2271 #ifdef CONFIG_VNC
2272 int show_vnc_port = 0;
2273 #endif
2274 int defconfig = 1;
2275 const char *log_mask = NULL;
2276 const char *log_file = NULL;
2277 GMemVTable mem_trace = {
2278 .malloc = malloc_and_trace,
2279 .realloc = realloc_and_trace,
2280 .free = free_and_trace,
2282 const char *trace_events = NULL;
2283 const char *trace_file = NULL;
2285 atexit(qemu_run_exit_notifiers);
2286 error_set_progname(argv[0]);
2288 g_mem_set_vtable(&mem_trace);
2289 g_thread_init(NULL);
2291 runstate_init();
2293 init_clocks();
2295 qemu_cache_utils_init(envp);
2297 QLIST_INIT (&vm_change_state_head);
2298 os_setup_early_signal_handling();
2300 module_call_init(MODULE_INIT_MACHINE);
2301 machine = find_default_machine();
2302 cpu_model = NULL;
2303 initrd_filename = NULL;
2304 ram_size = 0;
2305 snapshot = 0;
2306 kernel_filename = NULL;
2307 kernel_cmdline = "";
2308 cyls = heads = secs = 0;
2309 translation = BIOS_ATA_TRANSLATION_AUTO;
2311 for (i = 0; i < MAX_NODES; i++) {
2312 node_mem[i] = 0;
2313 node_cpumask[i] = 0;
2316 nb_numa_nodes = 0;
2317 nb_nics = 0;
2319 autostart= 1;
2321 /* first pass of option parsing */
2322 optind = 1;
2323 while (optind < argc) {
2324 if (argv[optind][0] != '-') {
2325 /* disk image */
2326 optind++;
2327 continue;
2328 } else {
2329 const QEMUOption *popt;
2331 popt = lookup_opt(argc, argv, &optarg, &optind);
2332 switch (popt->index) {
2333 case QEMU_OPTION_nodefconfig:
2334 defconfig=0;
2335 break;
2340 if (defconfig) {
2341 int ret;
2343 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2344 if (ret < 0 && ret != -ENOENT) {
2345 exit(1);
2348 ret = qemu_read_config_file(arch_config_name);
2349 if (ret < 0 && ret != -ENOENT) {
2350 exit(1);
2353 cpudef_init();
2355 /* second pass of option parsing */
2356 optind = 1;
2357 for(;;) {
2358 if (optind >= argc)
2359 break;
2360 if (argv[optind][0] != '-') {
2361 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2362 } else {
2363 const QEMUOption *popt;
2365 popt = lookup_opt(argc, argv, &optarg, &optind);
2366 if (!(popt->arch_mask & arch_type)) {
2367 printf("Option %s not supported for this target\n", popt->name);
2368 exit(1);
2370 switch(popt->index) {
2371 case QEMU_OPTION_M:
2372 machine = machine_parse(optarg);
2373 break;
2374 case QEMU_OPTION_cpu:
2375 /* hw initialization will check this */
2376 if (*optarg == '?') {
2377 list_cpus(stdout, &fprintf, optarg);
2378 exit(0);
2379 } else {
2380 cpu_model = optarg;
2382 break;
2383 case QEMU_OPTION_initrd:
2384 initrd_filename = optarg;
2385 break;
2386 case QEMU_OPTION_hda:
2388 char buf[256];
2389 if (cyls == 0)
2390 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2391 else
2392 snprintf(buf, sizeof(buf),
2393 "%s,cyls=%d,heads=%d,secs=%d%s",
2394 HD_OPTS , cyls, heads, secs,
2395 translation == BIOS_ATA_TRANSLATION_LBA ?
2396 ",trans=lba" :
2397 translation == BIOS_ATA_TRANSLATION_NONE ?
2398 ",trans=none" : "");
2399 drive_add(IF_DEFAULT, 0, optarg, buf);
2400 break;
2402 case QEMU_OPTION_hdb:
2403 case QEMU_OPTION_hdc:
2404 case QEMU_OPTION_hdd:
2405 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2406 HD_OPTS);
2407 break;
2408 case QEMU_OPTION_drive:
2409 if (drive_def(optarg) == NULL) {
2410 exit(1);
2412 break;
2413 case QEMU_OPTION_set:
2414 if (qemu_set_option(optarg) != 0)
2415 exit(1);
2416 break;
2417 case QEMU_OPTION_global:
2418 if (qemu_global_option(optarg) != 0)
2419 exit(1);
2420 break;
2421 case QEMU_OPTION_mtdblock:
2422 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2423 break;
2424 case QEMU_OPTION_sd:
2425 drive_add(IF_SD, 0, optarg, SD_OPTS);
2426 break;
2427 case QEMU_OPTION_pflash:
2428 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2429 break;
2430 case QEMU_OPTION_snapshot:
2431 snapshot = 1;
2432 break;
2433 case QEMU_OPTION_hdachs:
2435 const char *p;
2436 p = optarg;
2437 cyls = strtol(p, (char **)&p, 0);
2438 if (cyls < 1 || cyls > 16383)
2439 goto chs_fail;
2440 if (*p != ',')
2441 goto chs_fail;
2442 p++;
2443 heads = strtol(p, (char **)&p, 0);
2444 if (heads < 1 || heads > 16)
2445 goto chs_fail;
2446 if (*p != ',')
2447 goto chs_fail;
2448 p++;
2449 secs = strtol(p, (char **)&p, 0);
2450 if (secs < 1 || secs > 63)
2451 goto chs_fail;
2452 if (*p == ',') {
2453 p++;
2454 if (!strcmp(p, "none"))
2455 translation = BIOS_ATA_TRANSLATION_NONE;
2456 else if (!strcmp(p, "lba"))
2457 translation = BIOS_ATA_TRANSLATION_LBA;
2458 else if (!strcmp(p, "auto"))
2459 translation = BIOS_ATA_TRANSLATION_AUTO;
2460 else
2461 goto chs_fail;
2462 } else if (*p != '\0') {
2463 chs_fail:
2464 fprintf(stderr, "qemu: invalid physical CHS format\n");
2465 exit(1);
2467 if (hda_opts != NULL) {
2468 char num[16];
2469 snprintf(num, sizeof(num), "%d", cyls);
2470 qemu_opt_set(hda_opts, "cyls", num);
2471 snprintf(num, sizeof(num), "%d", heads);
2472 qemu_opt_set(hda_opts, "heads", num);
2473 snprintf(num, sizeof(num), "%d", secs);
2474 qemu_opt_set(hda_opts, "secs", num);
2475 if (translation == BIOS_ATA_TRANSLATION_LBA)
2476 qemu_opt_set(hda_opts, "trans", "lba");
2477 if (translation == BIOS_ATA_TRANSLATION_NONE)
2478 qemu_opt_set(hda_opts, "trans", "none");
2481 break;
2482 case QEMU_OPTION_numa:
2483 if (nb_numa_nodes >= MAX_NODES) {
2484 fprintf(stderr, "qemu: too many NUMA nodes\n");
2485 exit(1);
2487 numa_add(optarg);
2488 break;
2489 case QEMU_OPTION_display:
2490 display_type = select_display(optarg);
2491 break;
2492 case QEMU_OPTION_nographic:
2493 display_type = DT_NOGRAPHIC;
2494 break;
2495 case QEMU_OPTION_curses:
2496 #ifdef CONFIG_CURSES
2497 display_type = DT_CURSES;
2498 #else
2499 fprintf(stderr, "Curses support is disabled\n");
2500 exit(1);
2501 #endif
2502 break;
2503 case QEMU_OPTION_portrait:
2504 graphic_rotate = 90;
2505 break;
2506 case QEMU_OPTION_rotate:
2507 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2508 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2509 graphic_rotate != 180 && graphic_rotate != 270) {
2510 fprintf(stderr,
2511 "qemu: only 90, 180, 270 deg rotation is available\n");
2512 exit(1);
2514 break;
2515 case QEMU_OPTION_kernel:
2516 kernel_filename = optarg;
2517 break;
2518 case QEMU_OPTION_append:
2519 kernel_cmdline = optarg;
2520 break;
2521 case QEMU_OPTION_cdrom:
2522 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2523 break;
2524 case QEMU_OPTION_boot:
2526 static const char * const params[] = {
2527 "order", "once", "menu",
2528 "splash", "splash-time", NULL
2530 char buf[sizeof(boot_devices)];
2531 char *standard_boot_devices;
2532 int legacy = 0;
2534 if (!strchr(optarg, '=')) {
2535 legacy = 1;
2536 pstrcpy(buf, sizeof(buf), optarg);
2537 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2538 fprintf(stderr,
2539 "qemu: unknown boot parameter '%s' in '%s'\n",
2540 buf, optarg);
2541 exit(1);
2544 if (legacy ||
2545 get_param_value(buf, sizeof(buf), "order", optarg)) {
2546 validate_bootdevices(buf);
2547 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2549 if (!legacy) {
2550 if (get_param_value(buf, sizeof(buf),
2551 "once", optarg)) {
2552 validate_bootdevices(buf);
2553 standard_boot_devices = g_strdup(boot_devices);
2554 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2555 qemu_register_reset(restore_boot_devices,
2556 standard_boot_devices);
2558 if (get_param_value(buf, sizeof(buf),
2559 "menu", optarg)) {
2560 if (!strcmp(buf, "on")) {
2561 boot_menu = 1;
2562 } else if (!strcmp(buf, "off")) {
2563 boot_menu = 0;
2564 } else {
2565 fprintf(stderr,
2566 "qemu: invalid option value '%s'\n",
2567 buf);
2568 exit(1);
2571 qemu_opts_parse(qemu_find_opts("boot-opts"),
2572 optarg, 0);
2575 break;
2576 case QEMU_OPTION_fda:
2577 case QEMU_OPTION_fdb:
2578 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2579 optarg, FD_OPTS);
2580 break;
2581 case QEMU_OPTION_no_fd_bootchk:
2582 fd_bootchk = 0;
2583 break;
2584 case QEMU_OPTION_netdev:
2585 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2586 exit(1);
2588 break;
2589 case QEMU_OPTION_net:
2590 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2591 exit(1);
2593 break;
2594 #ifdef CONFIG_SLIRP
2595 case QEMU_OPTION_tftp:
2596 legacy_tftp_prefix = optarg;
2597 break;
2598 case QEMU_OPTION_bootp:
2599 legacy_bootp_filename = optarg;
2600 break;
2601 case QEMU_OPTION_redir:
2602 if (net_slirp_redir(optarg) < 0)
2603 exit(1);
2604 break;
2605 #endif
2606 case QEMU_OPTION_bt:
2607 add_device_config(DEV_BT, optarg);
2608 break;
2609 case QEMU_OPTION_audio_help:
2610 if (!(audio_available())) {
2611 printf("Option %s not supported for this target\n", popt->name);
2612 exit(1);
2614 AUD_help ();
2615 exit (0);
2616 break;
2617 case QEMU_OPTION_soundhw:
2618 if (!(audio_available())) {
2619 printf("Option %s not supported for this target\n", popt->name);
2620 exit(1);
2622 select_soundhw (optarg);
2623 break;
2624 case QEMU_OPTION_h:
2625 help(0);
2626 break;
2627 case QEMU_OPTION_version:
2628 version();
2629 exit(0);
2630 break;
2631 case QEMU_OPTION_m: {
2632 int64_t value;
2634 value = strtosz(optarg, NULL);
2635 if (value < 0) {
2636 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2637 exit(1);
2640 if (value != (uint64_t)(ram_addr_t)value) {
2641 fprintf(stderr, "qemu: ram size too large\n");
2642 exit(1);
2644 ram_size = value;
2645 break;
2647 case QEMU_OPTION_mempath:
2648 mem_path = optarg;
2649 break;
2650 #ifdef MAP_POPULATE
2651 case QEMU_OPTION_mem_prealloc:
2652 mem_prealloc = 1;
2653 break;
2654 #endif
2655 case QEMU_OPTION_d:
2656 log_mask = optarg;
2657 break;
2658 case QEMU_OPTION_D:
2659 log_file = optarg;
2660 break;
2661 case QEMU_OPTION_s:
2662 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2663 break;
2664 case QEMU_OPTION_gdb:
2665 gdbstub_dev = optarg;
2666 break;
2667 case QEMU_OPTION_L:
2668 data_dir = optarg;
2669 break;
2670 case QEMU_OPTION_bios:
2671 bios_name = optarg;
2672 break;
2673 case QEMU_OPTION_singlestep:
2674 singlestep = 1;
2675 break;
2676 case QEMU_OPTION_S:
2677 autostart = 0;
2678 break;
2679 case QEMU_OPTION_k:
2680 keyboard_layout = optarg;
2681 break;
2682 case QEMU_OPTION_localtime:
2683 rtc_utc = 0;
2684 break;
2685 case QEMU_OPTION_vga:
2686 select_vgahw (optarg);
2687 break;
2688 case QEMU_OPTION_g:
2690 const char *p;
2691 int w, h, depth;
2692 p = optarg;
2693 w = strtol(p, (char **)&p, 10);
2694 if (w <= 0) {
2695 graphic_error:
2696 fprintf(stderr, "qemu: invalid resolution or depth\n");
2697 exit(1);
2699 if (*p != 'x')
2700 goto graphic_error;
2701 p++;
2702 h = strtol(p, (char **)&p, 10);
2703 if (h <= 0)
2704 goto graphic_error;
2705 if (*p == 'x') {
2706 p++;
2707 depth = strtol(p, (char **)&p, 10);
2708 if (depth != 8 && depth != 15 && depth != 16 &&
2709 depth != 24 && depth != 32)
2710 goto graphic_error;
2711 } else if (*p == '\0') {
2712 depth = graphic_depth;
2713 } else {
2714 goto graphic_error;
2717 graphic_width = w;
2718 graphic_height = h;
2719 graphic_depth = depth;
2721 break;
2722 case QEMU_OPTION_echr:
2724 char *r;
2725 term_escape_char = strtol(optarg, &r, 0);
2726 if (r == optarg)
2727 printf("Bad argument to echr\n");
2728 break;
2730 case QEMU_OPTION_monitor:
2731 monitor_parse(optarg, "readline");
2732 default_monitor = 0;
2733 break;
2734 case QEMU_OPTION_qmp:
2735 monitor_parse(optarg, "control");
2736 default_monitor = 0;
2737 break;
2738 case QEMU_OPTION_mon:
2739 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2740 if (!opts) {
2741 exit(1);
2743 default_monitor = 0;
2744 break;
2745 case QEMU_OPTION_chardev:
2746 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2747 if (!opts) {
2748 exit(1);
2750 break;
2751 case QEMU_OPTION_fsdev:
2752 olist = qemu_find_opts("fsdev");
2753 if (!olist) {
2754 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2755 exit(1);
2757 opts = qemu_opts_parse(olist, optarg, 1);
2758 if (!opts) {
2759 fprintf(stderr, "parse error: %s\n", optarg);
2760 exit(1);
2762 break;
2763 case QEMU_OPTION_virtfs: {
2764 QemuOpts *fsdev;
2765 QemuOpts *device;
2767 olist = qemu_find_opts("virtfs");
2768 if (!olist) {
2769 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2770 exit(1);
2772 opts = qemu_opts_parse(olist, optarg, 1);
2773 if (!opts) {
2774 fprintf(stderr, "parse error: %s\n", optarg);
2775 exit(1);
2778 if (qemu_opt_get(opts, "fstype") == NULL ||
2779 qemu_opt_get(opts, "mount_tag") == NULL ||
2780 qemu_opt_get(opts, "path") == NULL ||
2781 qemu_opt_get(opts, "security_model") == NULL) {
2782 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2783 "security_model=[mapped|passthrough|none],"
2784 "mount_tag=tag.\n");
2785 exit(1);
2788 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2789 qemu_opt_get(opts, "mount_tag"), 1);
2790 if (!fsdev) {
2791 fprintf(stderr, "duplicate fsdev id: %s\n",
2792 qemu_opt_get(opts, "mount_tag"));
2793 exit(1);
2795 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2796 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2797 qemu_opt_set(fsdev, "security_model",
2798 qemu_opt_get(opts, "security_model"));
2800 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2801 qemu_opt_set(device, "driver", "virtio-9p-pci");
2802 qemu_opt_set(device, "fsdev",
2803 qemu_opt_get(opts, "mount_tag"));
2804 qemu_opt_set(device, "mount_tag",
2805 qemu_opt_get(opts, "mount_tag"));
2806 break;
2808 case QEMU_OPTION_serial:
2809 add_device_config(DEV_SERIAL, optarg);
2810 default_serial = 0;
2811 if (strncmp(optarg, "mon:", 4) == 0) {
2812 default_monitor = 0;
2814 break;
2815 case QEMU_OPTION_watchdog:
2816 if (watchdog) {
2817 fprintf(stderr,
2818 "qemu: only one watchdog option may be given\n");
2819 return 1;
2821 watchdog = optarg;
2822 break;
2823 case QEMU_OPTION_watchdog_action:
2824 if (select_watchdog_action(optarg) == -1) {
2825 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2826 exit(1);
2828 break;
2829 case QEMU_OPTION_virtiocon:
2830 add_device_config(DEV_VIRTCON, optarg);
2831 default_virtcon = 0;
2832 if (strncmp(optarg, "mon:", 4) == 0) {
2833 default_monitor = 0;
2835 break;
2836 case QEMU_OPTION_parallel:
2837 add_device_config(DEV_PARALLEL, optarg);
2838 default_parallel = 0;
2839 if (strncmp(optarg, "mon:", 4) == 0) {
2840 default_monitor = 0;
2842 break;
2843 case QEMU_OPTION_debugcon:
2844 add_device_config(DEV_DEBUGCON, optarg);
2845 break;
2846 case QEMU_OPTION_loadvm:
2847 loadvm = optarg;
2848 break;
2849 case QEMU_OPTION_full_screen:
2850 full_screen = 1;
2851 break;
2852 #ifdef CONFIG_SDL
2853 case QEMU_OPTION_no_frame:
2854 no_frame = 1;
2855 break;
2856 case QEMU_OPTION_alt_grab:
2857 alt_grab = 1;
2858 break;
2859 case QEMU_OPTION_ctrl_grab:
2860 ctrl_grab = 1;
2861 break;
2862 case QEMU_OPTION_no_quit:
2863 no_quit = 1;
2864 break;
2865 case QEMU_OPTION_sdl:
2866 display_type = DT_SDL;
2867 break;
2868 #else
2869 case QEMU_OPTION_no_frame:
2870 case QEMU_OPTION_alt_grab:
2871 case QEMU_OPTION_ctrl_grab:
2872 case QEMU_OPTION_no_quit:
2873 case QEMU_OPTION_sdl:
2874 fprintf(stderr, "SDL support is disabled\n");
2875 exit(1);
2876 #endif
2877 case QEMU_OPTION_pidfile:
2878 pid_file = optarg;
2879 break;
2880 case QEMU_OPTION_win2k_hack:
2881 win2k_install_hack = 1;
2882 break;
2883 case QEMU_OPTION_rtc_td_hack:
2884 rtc_td_hack = 1;
2885 break;
2886 case QEMU_OPTION_acpitable:
2887 do_acpitable_option(optarg);
2888 break;
2889 case QEMU_OPTION_smbios:
2890 do_smbios_option(optarg);
2891 break;
2892 case QEMU_OPTION_enable_kvm:
2893 olist = qemu_find_opts("machine");
2894 qemu_opts_reset(olist);
2895 qemu_opts_parse(olist, "accel=kvm", 0);
2896 break;
2897 case QEMU_OPTION_machine:
2898 olist = qemu_find_opts("machine");
2899 qemu_opts_reset(olist);
2900 opts = qemu_opts_parse(olist, optarg, 1);
2901 if (!opts) {
2902 fprintf(stderr, "parse error: %s\n", optarg);
2903 exit(1);
2905 optarg = qemu_opt_get(opts, "type");
2906 if (optarg) {
2907 machine = machine_parse(optarg);
2909 break;
2910 case QEMU_OPTION_usb:
2911 usb_enabled = 1;
2912 break;
2913 case QEMU_OPTION_usbdevice:
2914 usb_enabled = 1;
2915 add_device_config(DEV_USB, optarg);
2916 break;
2917 case QEMU_OPTION_device:
2918 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2919 exit(1);
2921 break;
2922 case QEMU_OPTION_smp:
2923 smp_parse(optarg);
2924 if (smp_cpus < 1) {
2925 fprintf(stderr, "Invalid number of CPUs\n");
2926 exit(1);
2928 if (max_cpus < smp_cpus) {
2929 fprintf(stderr, "maxcpus must be equal to or greater than "
2930 "smp\n");
2931 exit(1);
2933 if (max_cpus > 255) {
2934 fprintf(stderr, "Unsupported number of maxcpus\n");
2935 exit(1);
2937 break;
2938 case QEMU_OPTION_vnc:
2939 #ifdef CONFIG_VNC
2940 display_remote++;
2941 vnc_display = optarg;
2942 #else
2943 fprintf(stderr, "VNC support is disabled\n");
2944 exit(1);
2945 #endif
2946 break;
2947 case QEMU_OPTION_no_acpi:
2948 acpi_enabled = 0;
2949 break;
2950 case QEMU_OPTION_no_hpet:
2951 no_hpet = 1;
2952 break;
2953 case QEMU_OPTION_balloon:
2954 if (balloon_parse(optarg) < 0) {
2955 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2956 exit(1);
2958 break;
2959 case QEMU_OPTION_no_reboot:
2960 no_reboot = 1;
2961 break;
2962 case QEMU_OPTION_no_shutdown:
2963 no_shutdown = 1;
2964 break;
2965 case QEMU_OPTION_show_cursor:
2966 cursor_hide = 0;
2967 break;
2968 case QEMU_OPTION_uuid:
2969 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2970 fprintf(stderr, "Fail to parse UUID string."
2971 " Wrong format.\n");
2972 exit(1);
2974 break;
2975 case QEMU_OPTION_option_rom:
2976 if (nb_option_roms >= MAX_OPTION_ROMS) {
2977 fprintf(stderr, "Too many option ROMs\n");
2978 exit(1);
2980 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2981 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2982 option_rom[nb_option_roms].bootindex =
2983 qemu_opt_get_number(opts, "bootindex", -1);
2984 if (!option_rom[nb_option_roms].name) {
2985 fprintf(stderr, "Option ROM file is not specified\n");
2986 exit(1);
2988 nb_option_roms++;
2989 break;
2990 case QEMU_OPTION_semihosting:
2991 semihosting_enabled = 1;
2992 break;
2993 case QEMU_OPTION_name:
2994 qemu_name = g_strdup(optarg);
2996 char *p = strchr(qemu_name, ',');
2997 if (p != NULL) {
2998 *p++ = 0;
2999 if (strncmp(p, "process=", 8)) {
3000 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3001 exit(1);
3003 p += 8;
3004 os_set_proc_name(p);
3007 break;
3008 case QEMU_OPTION_prom_env:
3009 if (nb_prom_envs >= MAX_PROM_ENVS) {
3010 fprintf(stderr, "Too many prom variables\n");
3011 exit(1);
3013 prom_envs[nb_prom_envs] = optarg;
3014 nb_prom_envs++;
3015 break;
3016 case QEMU_OPTION_old_param:
3017 old_param = 1;
3018 break;
3019 case QEMU_OPTION_clock:
3020 configure_alarms(optarg);
3021 break;
3022 case QEMU_OPTION_startdate:
3023 configure_rtc_date_offset(optarg, 1);
3024 break;
3025 case QEMU_OPTION_rtc:
3026 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3027 if (!opts) {
3028 exit(1);
3030 configure_rtc(opts);
3031 break;
3032 case QEMU_OPTION_tb_size:
3033 tcg_tb_size = strtol(optarg, NULL, 0);
3034 if (tcg_tb_size < 0) {
3035 tcg_tb_size = 0;
3037 break;
3038 case QEMU_OPTION_icount:
3039 icount_option = optarg;
3040 break;
3041 case QEMU_OPTION_incoming:
3042 incoming = optarg;
3043 break;
3044 case QEMU_OPTION_nodefaults:
3045 default_serial = 0;
3046 default_parallel = 0;
3047 default_virtcon = 0;
3048 default_monitor = 0;
3049 default_vga = 0;
3050 default_net = 0;
3051 default_floppy = 0;
3052 default_cdrom = 0;
3053 default_sdcard = 0;
3054 break;
3055 case QEMU_OPTION_xen_domid:
3056 if (!(xen_available())) {
3057 printf("Option %s not supported for this target\n", popt->name);
3058 exit(1);
3060 xen_domid = atoi(optarg);
3061 break;
3062 case QEMU_OPTION_xen_create:
3063 if (!(xen_available())) {
3064 printf("Option %s not supported for this target\n", popt->name);
3065 exit(1);
3067 xen_mode = XEN_CREATE;
3068 break;
3069 case QEMU_OPTION_xen_attach:
3070 if (!(xen_available())) {
3071 printf("Option %s not supported for this target\n", popt->name);
3072 exit(1);
3074 xen_mode = XEN_ATTACH;
3075 break;
3076 case QEMU_OPTION_trace:
3078 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3079 if (!opts) {
3080 exit(1);
3082 trace_events = qemu_opt_get(opts, "events");
3083 trace_file = qemu_opt_get(opts, "file");
3084 break;
3086 case QEMU_OPTION_readconfig:
3088 int ret = qemu_read_config_file(optarg);
3089 if (ret < 0) {
3090 fprintf(stderr, "read config %s: %s\n", optarg,
3091 strerror(-ret));
3092 exit(1);
3094 break;
3096 case QEMU_OPTION_spice:
3097 olist = qemu_find_opts("spice");
3098 if (!olist) {
3099 fprintf(stderr, "spice is not supported by this qemu build.\n");
3100 exit(1);
3102 opts = qemu_opts_parse(olist, optarg, 0);
3103 if (!opts) {
3104 fprintf(stderr, "parse error: %s\n", optarg);
3105 exit(1);
3107 break;
3108 case QEMU_OPTION_writeconfig:
3110 FILE *fp;
3111 if (strcmp(optarg, "-") == 0) {
3112 fp = stdout;
3113 } else {
3114 fp = fopen(optarg, "w");
3115 if (fp == NULL) {
3116 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3117 exit(1);
3120 qemu_config_write(fp);
3121 fclose(fp);
3122 break;
3124 default:
3125 os_parse_cmd_args(popt->index, optarg);
3129 loc_set_none();
3131 /* Open the logfile at this point, if necessary. We can't open the logfile
3132 * when encountering either of the logging options (-d or -D) because the
3133 * other one may be encountered later on the command line, changing the
3134 * location or level of logging.
3136 if (log_mask) {
3137 if (log_file) {
3138 set_cpu_log_filename(log_file);
3140 set_cpu_log(log_mask);
3143 if (!trace_backend_init(trace_events, trace_file)) {
3144 exit(1);
3147 /* If no data_dir is specified then try to find it relative to the
3148 executable path. */
3149 if (!data_dir) {
3150 data_dir = os_find_datadir(argv[0]);
3152 /* If all else fails use the install path specified when building. */
3153 if (!data_dir) {
3154 data_dir = CONFIG_QEMU_DATADIR;
3158 * Default to max_cpus = smp_cpus, in case the user doesn't
3159 * specify a max_cpus value.
3161 if (!max_cpus)
3162 max_cpus = smp_cpus;
3164 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3165 if (smp_cpus > machine->max_cpus) {
3166 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3167 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3168 machine->max_cpus);
3169 exit(1);
3173 * Get the default machine options from the machine if it is not already
3174 * specified either by the configuration file or by the command line.
3176 if (machine->default_machine_opts) {
3177 QemuOptsList *list = qemu_find_opts("machine");
3178 const char *p = NULL;
3180 if (!QTAILQ_EMPTY(&list->head)) {
3181 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3183 if (p == NULL) {
3184 qemu_opts_reset(list);
3185 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3186 if (!opts) {
3187 fprintf(stderr, "parse error for machine %s: %s\n",
3188 machine->name, machine->default_machine_opts);
3189 exit(1);
3194 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3195 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3197 if (machine->no_serial) {
3198 default_serial = 0;
3200 if (machine->no_parallel) {
3201 default_parallel = 0;
3203 if (!machine->use_virtcon) {
3204 default_virtcon = 0;
3206 if (machine->no_vga) {
3207 default_vga = 0;
3209 if (machine->no_floppy) {
3210 default_floppy = 0;
3212 if (machine->no_cdrom) {
3213 default_cdrom = 0;
3215 if (machine->no_sdcard) {
3216 default_sdcard = 0;
3219 if (display_type == DT_NOGRAPHIC) {
3220 if (default_parallel)
3221 add_device_config(DEV_PARALLEL, "null");
3222 if (default_serial && default_monitor) {
3223 add_device_config(DEV_SERIAL, "mon:stdio");
3224 } else if (default_virtcon && default_monitor) {
3225 add_device_config(DEV_VIRTCON, "mon:stdio");
3226 } else {
3227 if (default_serial)
3228 add_device_config(DEV_SERIAL, "stdio");
3229 if (default_virtcon)
3230 add_device_config(DEV_VIRTCON, "stdio");
3231 if (default_monitor)
3232 monitor_parse("stdio", "readline");
3234 } else {
3235 if (default_serial)
3236 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3237 if (default_parallel)
3238 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3239 if (default_monitor)
3240 monitor_parse("vc:80Cx24C", "readline");
3241 if (default_virtcon)
3242 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3244 if (default_vga)
3245 vga_interface_type = VGA_CIRRUS;
3247 socket_init();
3249 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3250 exit(1);
3251 #ifdef CONFIG_VIRTFS
3252 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3253 exit(1);
3255 #endif
3257 os_daemonize();
3259 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3260 os_pidfile_error();
3261 exit(1);
3264 /* init the memory */
3265 if (ram_size == 0) {
3266 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3269 configure_accelerator();
3271 if (qemu_init_main_loop()) {
3272 fprintf(stderr, "qemu_init_main_loop failed\n");
3273 exit(1);
3275 linux_boot = (kernel_filename != NULL);
3277 if (!linux_boot && *kernel_cmdline != '\0') {
3278 fprintf(stderr, "-append only allowed with -kernel option\n");
3279 exit(1);
3282 if (!linux_boot && initrd_filename != NULL) {
3283 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3284 exit(1);
3287 os_set_line_buffering();
3289 if (init_timer_alarm() < 0) {
3290 fprintf(stderr, "could not initialize alarm timer\n");
3291 exit(1);
3293 configure_icount(icount_option);
3295 if (net_init_clients() < 0) {
3296 exit(1);
3299 /* init the bluetooth world */
3300 if (foreach_device_config(DEV_BT, bt_parse))
3301 exit(1);
3303 if (!xen_enabled()) {
3304 /* On 32-bit hosts, QEMU is limited by virtual address space */
3305 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3306 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3307 exit(1);
3311 cpu_exec_init_all();
3313 bdrv_init_with_whitelist();
3315 blk_mig_init();
3317 /* open the virtual block devices */
3318 if (snapshot)
3319 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3320 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3321 exit(1);
3323 default_drive(default_cdrom, snapshot, machine->use_scsi,
3324 IF_DEFAULT, 2, CDROM_OPTS);
3325 default_drive(default_floppy, snapshot, machine->use_scsi,
3326 IF_FLOPPY, 0, FD_OPTS);
3327 default_drive(default_sdcard, snapshot, machine->use_scsi,
3328 IF_SD, 0, SD_OPTS);
3330 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3331 ram_load, NULL);
3333 if (nb_numa_nodes > 0) {
3334 int i;
3336 if (nb_numa_nodes > MAX_NODES) {
3337 nb_numa_nodes = MAX_NODES;
3340 /* If no memory size if given for any node, assume the default case
3341 * and distribute the available memory equally across all nodes
3343 for (i = 0; i < nb_numa_nodes; i++) {
3344 if (node_mem[i] != 0)
3345 break;
3347 if (i == nb_numa_nodes) {
3348 uint64_t usedmem = 0;
3350 /* On Linux, the each node's border has to be 8MB aligned,
3351 * the final node gets the rest.
3353 for (i = 0; i < nb_numa_nodes - 1; i++) {
3354 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3355 usedmem += node_mem[i];
3357 node_mem[i] = ram_size - usedmem;
3360 for (i = 0; i < nb_numa_nodes; i++) {
3361 if (node_cpumask[i] != 0)
3362 break;
3364 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3365 * must cope with this anyway, because there are BIOSes out there in
3366 * real machines which also use this scheme.
3368 if (i == nb_numa_nodes) {
3369 for (i = 0; i < smp_cpus; i++) {
3370 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3375 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3376 exit(1);
3379 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3380 exit(1);
3381 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3382 exit(1);
3383 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3384 exit(1);
3385 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3386 exit(1);
3388 module_call_init(MODULE_INIT_DEVICE);
3390 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3391 exit(0);
3393 if (watchdog) {
3394 i = select_watchdog(watchdog);
3395 if (i > 0)
3396 exit (i == 1 ? 1 : 0);
3399 if (machine->compat_props) {
3400 qdev_prop_register_global_list(machine->compat_props);
3402 qemu_add_globals();
3404 machine->init(ram_size, boot_devices,
3405 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3407 cpu_synchronize_all_post_init();
3409 set_numa_modes();
3411 current_machine = machine;
3413 /* init USB devices */
3414 if (usb_enabled) {
3415 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3416 exit(1);
3419 /* init generic devices */
3420 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3421 exit(1);
3423 net_check_clients();
3425 /* just use the first displaystate for the moment */
3426 ds = get_displaystate();
3428 if (using_spice)
3429 display_remote++;
3430 if (display_type == DT_DEFAULT && !display_remote) {
3431 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3432 display_type = DT_SDL;
3433 #elif defined(CONFIG_VNC)
3434 vnc_display = "localhost:0,to=99";
3435 show_vnc_port = 1;
3436 #else
3437 display_type = DT_NONE;
3438 #endif
3442 /* init local displays */
3443 switch (display_type) {
3444 case DT_NOGRAPHIC:
3445 break;
3446 #if defined(CONFIG_CURSES)
3447 case DT_CURSES:
3448 curses_display_init(ds, full_screen);
3449 break;
3450 #endif
3451 #if defined(CONFIG_SDL)
3452 case DT_SDL:
3453 sdl_display_init(ds, full_screen, no_frame);
3454 break;
3455 #elif defined(CONFIG_COCOA)
3456 case DT_SDL:
3457 cocoa_display_init(ds, full_screen);
3458 break;
3459 #endif
3460 default:
3461 break;
3464 /* must be after terminal init, SDL library changes signal handlers */
3465 os_setup_signal_handling();
3467 #ifdef CONFIG_VNC
3468 /* init remote displays */
3469 if (vnc_display) {
3470 vnc_display_init(ds);
3471 if (vnc_display_open(ds, vnc_display) < 0)
3472 exit(1);
3474 if (show_vnc_port) {
3475 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3478 #endif
3479 #ifdef CONFIG_SPICE
3480 if (using_spice && !qxl_enabled) {
3481 qemu_spice_display_init(ds);
3483 #endif
3485 /* display setup */
3486 dpy_resize(ds);
3487 dcl = ds->listeners;
3488 while (dcl != NULL) {
3489 if (dcl->dpy_refresh != NULL) {
3490 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3491 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3492 break;
3494 dcl = dcl->next;
3496 if (ds->gui_timer == NULL) {
3497 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3498 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3500 text_consoles_set_display(ds);
3502 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3503 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3504 gdbstub_dev);
3505 exit(1);
3508 qdev_machine_creation_done();
3510 if (rom_load_all() != 0) {
3511 fprintf(stderr, "rom loading failed\n");
3512 exit(1);
3515 /* TODO: once all bus devices are qdevified, this should be done
3516 * when bus is created by qdev.c */
3517 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3518 qemu_run_machine_init_done_notifiers();
3520 qemu_system_reset(VMRESET_SILENT);
3521 if (loadvm) {
3522 if (load_vmstate(loadvm) < 0) {
3523 autostart = 0;
3527 if (incoming) {
3528 runstate_set(RSTATE_IN_MIGRATE);
3529 int ret = qemu_start_incoming_migration(incoming);
3530 if (ret < 0) {
3531 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3532 incoming, ret);
3533 exit(ret);
3535 } else if (autostart) {
3536 vm_start();
3537 } else {
3538 runstate_set(RSTATE_PRE_LAUNCH);
3541 os_setup_post();
3543 main_loop();
3544 quit_timers();
3545 net_cleanup();
3546 res_free();
3548 return 0;