qemu-kvm: Drop redundant cpuid filtering from cpu_x86_cpuid
[qemu-kvm.git] / vl.c
blob569a9a1374ebe2ed246cbd5cadc9292bd35a199e
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 #include "kvm.h"
151 #ifdef CONFIG_VIRTFS
152 #include "fsdev/qemu-fsdev.h"
153 #endif
155 #include "disas.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
161 #include "trace.h"
162 #include "trace/control.h"
163 #include "qemu-queue.h"
164 #include "cpus.h"
165 #include "arch_init.h"
167 #include "ui/qemu-spice.h"
169 //#define DEBUG_NET
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir;
177 const char *bios_name = NULL;
178 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
179 DisplayType display_type = DT_DEFAULT;
180 int display_remote = 0;
181 const char* keyboard_layout = NULL;
182 ram_addr_t ram_size;
183 const char *mem_path = NULL;
184 #ifdef MAP_POPULATE
185 int mem_prealloc = 0; /* force preallocation of physical target memory */
186 #endif
187 int nb_nics;
188 NICInfo nd_table[MAX_NICS];
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 const char *watchdog;
221 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
222 int nb_option_roms;
223 int semihosting_enabled = 0;
224 int time_drift_fix = 0;
225 unsigned int kvm_shadow_memory = 0;
226 int old_param = 0;
227 const char *qemu_name;
228 int alt_grab = 0;
229 int ctrl_grab = 0;
230 unsigned int nb_prom_envs = 0;
231 const char *prom_envs[MAX_PROM_ENVS];
232 const char *nvram = NULL;
233 int boot_menu;
234 uint8_t *boot_splash_filedata;
235 int boot_splash_filedata_size;
236 uint8_t qemu_extra_params_fw[2];
238 typedef struct FWBootEntry FWBootEntry;
240 struct FWBootEntry {
241 QTAILQ_ENTRY(FWBootEntry) link;
242 int32_t bootindex;
243 DeviceState *dev;
244 char *suffix;
247 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
249 int nb_numa_nodes;
250 uint64_t node_mem[MAX_NODES];
251 uint64_t node_cpumask[MAX_NODES];
253 static QEMUTimer *nographic_timer;
255 uint8_t qemu_uuid[16];
257 static QEMUBootSetHandler *boot_set_handler;
258 static void *boot_set_opaque;
260 static NotifierList exit_notifiers =
261 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
263 static NotifierList machine_init_done_notifiers =
264 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
266 static int tcg_allowed = 1;
267 int kvm_allowed = 0;
268 int xen_allowed = 0;
269 uint32_t xen_domid;
270 enum xen_mode xen_mode = XEN_EMULATE;
271 static int tcg_tb_size;
273 static int default_serial = 1;
274 static int default_parallel = 1;
275 static int default_virtcon = 1;
276 static int default_monitor = 1;
277 static int default_vga = 1;
278 static int default_floppy = 1;
279 static int default_cdrom = 1;
280 static int default_sdcard = 1;
282 static struct {
283 const char *driver;
284 int *flag;
285 } default_list[] = {
286 { .driver = "isa-serial", .flag = &default_serial },
287 { .driver = "isa-parallel", .flag = &default_parallel },
288 { .driver = "isa-fdc", .flag = &default_floppy },
289 { .driver = "ide-cd", .flag = &default_cdrom },
290 { .driver = "ide-hd", .flag = &default_cdrom },
291 { .driver = "ide-drive", .flag = &default_cdrom },
292 { .driver = "scsi-cd", .flag = &default_cdrom },
293 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
294 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
295 { .driver = "virtio-serial", .flag = &default_virtcon },
296 { .driver = "VGA", .flag = &default_vga },
297 { .driver = "cirrus-vga", .flag = &default_vga },
298 { .driver = "vmware-svga", .flag = &default_vga },
299 { .driver = "isa-vga", .flag = &default_vga },
300 { .driver = "qxl-vga", .flag = &default_vga },
303 static void res_free(void)
305 if (boot_splash_filedata != NULL) {
306 g_free(boot_splash_filedata);
307 boot_splash_filedata = NULL;
311 static int default_driver_check(QemuOpts *opts, void *opaque)
313 const char *driver = qemu_opt_get(opts, "driver");
314 int i;
316 if (!driver)
317 return 0;
318 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
319 if (strcmp(default_list[i].driver, driver) != 0)
320 continue;
321 *(default_list[i].flag) = 0;
323 return 0;
326 /***********************************************************/
327 /* QEMU state */
329 static RunState current_run_state = RSTATE_NO_STATE;
331 typedef struct {
332 RunState from;
333 RunState to;
334 } RunStateTransition;
336 static const RunStateTransition runstate_transitions_def[] = {
337 /* from -> to */
338 { RSTATE_NO_STATE, RSTATE_RUNNING },
339 { RSTATE_NO_STATE, RSTATE_IN_MIGRATE },
340 { RSTATE_NO_STATE, RSTATE_PRE_LAUNCH },
342 { RSTATE_DEBUG, RSTATE_RUNNING },
344 { RSTATE_IN_MIGRATE, RSTATE_RUNNING },
345 { RSTATE_IN_MIGRATE, RSTATE_PRE_LAUNCH },
347 { RSTATE_PANICKED, RSTATE_PAUSED },
349 { RSTATE_IO_ERROR, RSTATE_RUNNING },
351 { RSTATE_PAUSED, RSTATE_RUNNING },
353 { RSTATE_POST_MIGRATE, RSTATE_RUNNING },
355 { RSTATE_PRE_LAUNCH, RSTATE_RUNNING },
356 { RSTATE_PRE_LAUNCH, RSTATE_POST_MIGRATE },
358 { RSTATE_PRE_MIGRATE, RSTATE_RUNNING },
359 { RSTATE_PRE_MIGRATE, RSTATE_POST_MIGRATE },
361 { RSTATE_RESTORE, RSTATE_RUNNING },
363 { RSTATE_RUNNING, RSTATE_DEBUG },
364 { RSTATE_RUNNING, RSTATE_PANICKED },
365 { RSTATE_RUNNING, RSTATE_IO_ERROR },
366 { RSTATE_RUNNING, RSTATE_PAUSED },
367 { RSTATE_RUNNING, RSTATE_PRE_MIGRATE },
368 { RSTATE_RUNNING, RSTATE_RESTORE },
369 { RSTATE_RUNNING, RSTATE_SAVEVM },
370 { RSTATE_RUNNING, RSTATE_SHUTDOWN },
371 { RSTATE_RUNNING, RSTATE_WATCHDOG },
373 { RSTATE_SAVEVM, RSTATE_RUNNING },
375 { RSTATE_SHUTDOWN, RSTATE_PAUSED },
377 { RSTATE_WATCHDOG, RSTATE_RUNNING },
379 { RSTATE_MAX, RSTATE_MAX },
382 static bool runstate_valid_transitions[RSTATE_MAX][RSTATE_MAX];
384 static const char *const runstate_name_tbl[RSTATE_MAX] = {
385 [RSTATE_DEBUG] = "debug",
386 [RSTATE_IN_MIGRATE] = "incoming-migration",
387 [RSTATE_PANICKED] = "internal-error",
388 [RSTATE_IO_ERROR] = "io-error",
389 [RSTATE_PAUSED] = "paused",
390 [RSTATE_POST_MIGRATE] = "post-migrate",
391 [RSTATE_PRE_LAUNCH] = "prelaunch",
392 [RSTATE_PRE_MIGRATE] = "finish-migrate",
393 [RSTATE_RESTORE] = "restore-vm",
394 [RSTATE_RUNNING] = "running",
395 [RSTATE_SAVEVM] = "save-vm",
396 [RSTATE_SHUTDOWN] = "shutdown",
397 [RSTATE_WATCHDOG] = "watchdog",
400 bool runstate_check(RunState state)
402 return current_run_state == state;
405 void runstate_init(void)
407 const RunStateTransition *p;
409 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
411 for (p = &runstate_transitions_def[0]; p->from != RSTATE_MAX; p++) {
412 runstate_valid_transitions[p->from][p->to] = true;
416 /* This function will abort() on invalid state transitions */
417 void runstate_set(RunState new_state)
419 if (new_state >= RSTATE_MAX ||
420 !runstate_valid_transitions[current_run_state][new_state]) {
421 fprintf(stderr, "invalid runstate transition\n");
422 abort();
425 current_run_state = new_state;
428 const char *runstate_as_string(void)
430 assert(current_run_state > RSTATE_NO_STATE &&
431 current_run_state < RSTATE_MAX);
432 return runstate_name_tbl[current_run_state];
435 int runstate_is_running(void)
437 return runstate_check(RSTATE_RUNNING);
440 /***********************************************************/
441 /* real time host monotonic timer */
443 /***********************************************************/
444 /* host time/date access */
445 void qemu_get_timedate(struct tm *tm, int offset)
447 time_t ti;
448 struct tm *ret;
450 time(&ti);
451 ti += offset;
452 if (rtc_date_offset == -1) {
453 if (rtc_utc)
454 ret = gmtime(&ti);
455 else
456 ret = localtime(&ti);
457 } else {
458 ti -= rtc_date_offset;
459 ret = gmtime(&ti);
462 memcpy(tm, ret, sizeof(struct tm));
465 int qemu_timedate_diff(struct tm *tm)
467 time_t seconds;
469 if (rtc_date_offset == -1)
470 if (rtc_utc)
471 seconds = mktimegm(tm);
472 else
473 seconds = mktime(tm);
474 else
475 seconds = mktimegm(tm) + rtc_date_offset;
477 return seconds - time(NULL);
480 void rtc_change_mon_event(struct tm *tm)
482 QObject *data;
484 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
485 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
486 qobject_decref(data);
489 static void configure_rtc_date_offset(const char *startdate, int legacy)
491 time_t rtc_start_date;
492 struct tm tm;
494 if (!strcmp(startdate, "now") && legacy) {
495 rtc_date_offset = -1;
496 } else {
497 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
498 &tm.tm_year,
499 &tm.tm_mon,
500 &tm.tm_mday,
501 &tm.tm_hour,
502 &tm.tm_min,
503 &tm.tm_sec) == 6) {
504 /* OK */
505 } else if (sscanf(startdate, "%d-%d-%d",
506 &tm.tm_year,
507 &tm.tm_mon,
508 &tm.tm_mday) == 3) {
509 tm.tm_hour = 0;
510 tm.tm_min = 0;
511 tm.tm_sec = 0;
512 } else {
513 goto date_fail;
515 tm.tm_year -= 1900;
516 tm.tm_mon--;
517 rtc_start_date = mktimegm(&tm);
518 if (rtc_start_date == -1) {
519 date_fail:
520 fprintf(stderr, "Invalid date format. Valid formats are:\n"
521 "'2006-06-17T16:01:21' or '2006-06-17'\n");
522 exit(1);
524 rtc_date_offset = time(NULL) - rtc_start_date;
528 static void configure_rtc(QemuOpts *opts)
530 const char *value;
532 value = qemu_opt_get(opts, "base");
533 if (value) {
534 if (!strcmp(value, "utc")) {
535 rtc_utc = 1;
536 } else if (!strcmp(value, "localtime")) {
537 rtc_utc = 0;
538 } else {
539 configure_rtc_date_offset(value, 0);
542 value = qemu_opt_get(opts, "clock");
543 if (value) {
544 if (!strcmp(value, "host")) {
545 rtc_clock = host_clock;
546 } else if (!strcmp(value, "vm")) {
547 rtc_clock = vm_clock;
548 } else {
549 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
550 exit(1);
553 value = qemu_opt_get(opts, "driftfix");
554 if (value) {
555 if (!strcmp(value, "slew")) {
556 rtc_td_hack = 1;
557 } else if (!strcmp(value, "none")) {
558 rtc_td_hack = 0;
559 } else {
560 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
561 exit(1);
566 /***********************************************************/
567 /* Bluetooth support */
568 static int nb_hcis;
569 static int cur_hci;
570 static struct HCIInfo *hci_table[MAX_NICS];
572 static struct bt_vlan_s {
573 struct bt_scatternet_s net;
574 int id;
575 struct bt_vlan_s *next;
576 } *first_bt_vlan;
578 /* find or alloc a new bluetooth "VLAN" */
579 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
581 struct bt_vlan_s **pvlan, *vlan;
582 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
583 if (vlan->id == id)
584 return &vlan->net;
586 vlan = g_malloc0(sizeof(struct bt_vlan_s));
587 vlan->id = id;
588 pvlan = &first_bt_vlan;
589 while (*pvlan != NULL)
590 pvlan = &(*pvlan)->next;
591 *pvlan = vlan;
592 return &vlan->net;
595 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
599 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
601 return -ENOTSUP;
604 static struct HCIInfo null_hci = {
605 .cmd_send = null_hci_send,
606 .sco_send = null_hci_send,
607 .acl_send = null_hci_send,
608 .bdaddr_set = null_hci_addr_set,
611 struct HCIInfo *qemu_next_hci(void)
613 if (cur_hci == nb_hcis)
614 return &null_hci;
616 return hci_table[cur_hci++];
619 static struct HCIInfo *hci_init(const char *str)
621 char *endp;
622 struct bt_scatternet_s *vlan = 0;
624 if (!strcmp(str, "null"))
625 /* null */
626 return &null_hci;
627 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
628 /* host[:hciN] */
629 return bt_host_hci(str[4] ? str + 5 : "hci0");
630 else if (!strncmp(str, "hci", 3)) {
631 /* hci[,vlan=n] */
632 if (str[3]) {
633 if (!strncmp(str + 3, ",vlan=", 6)) {
634 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
635 if (*endp)
636 vlan = 0;
638 } else
639 vlan = qemu_find_bt_vlan(0);
640 if (vlan)
641 return bt_new_hci(vlan);
644 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
646 return 0;
649 static int bt_hci_parse(const char *str)
651 struct HCIInfo *hci;
652 bdaddr_t bdaddr;
654 if (nb_hcis >= MAX_NICS) {
655 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
656 return -1;
659 hci = hci_init(str);
660 if (!hci)
661 return -1;
663 bdaddr.b[0] = 0x52;
664 bdaddr.b[1] = 0x54;
665 bdaddr.b[2] = 0x00;
666 bdaddr.b[3] = 0x12;
667 bdaddr.b[4] = 0x34;
668 bdaddr.b[5] = 0x56 + nb_hcis;
669 hci->bdaddr_set(hci, bdaddr.b);
671 hci_table[nb_hcis++] = hci;
673 return 0;
676 static void bt_vhci_add(int vlan_id)
678 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
680 if (!vlan->slave)
681 fprintf(stderr, "qemu: warning: adding a VHCI to "
682 "an empty scatternet %i\n", vlan_id);
684 bt_vhci_init(bt_new_hci(vlan));
687 static struct bt_device_s *bt_device_add(const char *opt)
689 struct bt_scatternet_s *vlan;
690 int vlan_id = 0;
691 char *endp = strstr(opt, ",vlan=");
692 int len = (endp ? endp - opt : strlen(opt)) + 1;
693 char devname[10];
695 pstrcpy(devname, MIN(sizeof(devname), len), opt);
697 if (endp) {
698 vlan_id = strtol(endp + 6, &endp, 0);
699 if (*endp) {
700 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
701 return 0;
705 vlan = qemu_find_bt_vlan(vlan_id);
707 if (!vlan->slave)
708 fprintf(stderr, "qemu: warning: adding a slave device to "
709 "an empty scatternet %i\n", vlan_id);
711 if (!strcmp(devname, "keyboard"))
712 return bt_keyboard_init(vlan);
714 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
715 return 0;
718 static int bt_parse(const char *opt)
720 const char *endp, *p;
721 int vlan;
723 if (strstart(opt, "hci", &endp)) {
724 if (!*endp || *endp == ',') {
725 if (*endp)
726 if (!strstart(endp, ",vlan=", 0))
727 opt = endp + 1;
729 return bt_hci_parse(opt);
731 } else if (strstart(opt, "vhci", &endp)) {
732 if (!*endp || *endp == ',') {
733 if (*endp) {
734 if (strstart(endp, ",vlan=", &p)) {
735 vlan = strtol(p, (char **) &endp, 0);
736 if (*endp) {
737 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
738 return 1;
740 } else {
741 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
742 return 1;
744 } else
745 vlan = 0;
747 bt_vhci_add(vlan);
748 return 0;
750 } else if (strstart(opt, "device:", &endp))
751 return !bt_device_add(endp);
753 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
754 return 1;
757 /***********************************************************/
758 /* QEMU Block devices */
760 #define HD_OPTS "media=disk"
761 #define CDROM_OPTS "media=cdrom"
762 #define FD_OPTS ""
763 #define PFLASH_OPTS ""
764 #define MTD_OPTS ""
765 #define SD_OPTS ""
767 static int drive_init_func(QemuOpts *opts, void *opaque)
769 int *use_scsi = opaque;
771 return drive_init(opts, *use_scsi) == NULL;
774 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
776 if (NULL == qemu_opt_get(opts, "snapshot")) {
777 qemu_opt_set(opts, "snapshot", "on");
779 return 0;
782 static void default_drive(int enable, int snapshot, int use_scsi,
783 BlockInterfaceType type, int index,
784 const char *optstr)
786 QemuOpts *opts;
788 if (type == IF_DEFAULT) {
789 type = use_scsi ? IF_SCSI : IF_IDE;
792 if (!enable || drive_get_by_index(type, index)) {
793 return;
796 opts = drive_add(type, index, NULL, optstr);
797 if (snapshot) {
798 drive_enable_snapshot(opts, NULL);
800 if (!drive_init(opts, use_scsi)) {
801 exit(1);
805 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
807 boot_set_handler = func;
808 boot_set_opaque = opaque;
811 int qemu_boot_set(const char *boot_devices)
813 if (!boot_set_handler) {
814 return -EINVAL;
816 return boot_set_handler(boot_set_opaque, boot_devices);
819 static void validate_bootdevices(char *devices)
821 /* We just do some generic consistency checks */
822 const char *p;
823 int bitmap = 0;
825 for (p = devices; *p != '\0'; p++) {
826 /* Allowed boot devices are:
827 * a-b: floppy disk drives
828 * c-f: IDE disk drives
829 * g-m: machine implementation dependant drives
830 * n-p: network devices
831 * It's up to each machine implementation to check if the given boot
832 * devices match the actual hardware implementation and firmware
833 * features.
835 if (*p < 'a' || *p > 'p') {
836 fprintf(stderr, "Invalid boot device '%c'\n", *p);
837 exit(1);
839 if (bitmap & (1 << (*p - 'a'))) {
840 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
841 exit(1);
843 bitmap |= 1 << (*p - 'a');
847 static void restore_boot_devices(void *opaque)
849 char *standard_boot_devices = opaque;
850 static int first = 1;
852 /* Restore boot order and remove ourselves after the first boot */
853 if (first) {
854 first = 0;
855 return;
858 qemu_boot_set(standard_boot_devices);
860 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
861 g_free(standard_boot_devices);
864 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
865 const char *suffix)
867 FWBootEntry *node, *i;
869 if (bootindex < 0) {
870 return;
873 assert(dev != NULL || suffix != NULL);
875 node = g_malloc0(sizeof(FWBootEntry));
876 node->bootindex = bootindex;
877 node->suffix = suffix ? g_strdup(suffix) : NULL;
878 node->dev = dev;
880 QTAILQ_FOREACH(i, &fw_boot_order, link) {
881 if (i->bootindex == bootindex) {
882 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
883 exit(1);
884 } else if (i->bootindex < bootindex) {
885 continue;
887 QTAILQ_INSERT_BEFORE(i, node, link);
888 return;
890 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
894 * This function returns null terminated string that consist of new line
895 * separated device paths.
897 * memory pointed by "size" is assigned total length of the array in bytes
900 char *get_boot_devices_list(uint32_t *size)
902 FWBootEntry *i;
903 uint32_t total = 0;
904 char *list = NULL;
906 QTAILQ_FOREACH(i, &fw_boot_order, link) {
907 char *devpath = NULL, *bootpath;
908 int len;
910 if (i->dev) {
911 devpath = qdev_get_fw_dev_path(i->dev);
912 assert(devpath);
915 if (i->suffix && devpath) {
916 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
918 bootpath = g_malloc(bootpathlen);
919 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
920 g_free(devpath);
921 } else if (devpath) {
922 bootpath = devpath;
923 } else {
924 bootpath = g_strdup(i->suffix);
925 assert(bootpath);
928 if (total) {
929 list[total-1] = '\n';
931 len = strlen(bootpath) + 1;
932 list = g_realloc(list, total + len);
933 memcpy(&list[total], bootpath, len);
934 total += len;
935 g_free(bootpath);
938 *size = total;
940 return list;
943 static void numa_add(const char *optarg)
945 char option[128];
946 char *endptr;
947 unsigned long long value, endvalue;
948 int nodenr;
950 optarg = get_opt_name(option, 128, optarg, ',') + 1;
951 if (!strcmp(option, "node")) {
952 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
953 nodenr = nb_numa_nodes;
954 } else {
955 nodenr = strtoull(option, NULL, 10);
958 if (get_param_value(option, 128, "mem", optarg) == 0) {
959 node_mem[nodenr] = 0;
960 } else {
961 int64_t sval;
962 sval = strtosz(option, NULL);
963 if (sval < 0) {
964 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
965 exit(1);
967 node_mem[nodenr] = sval;
969 if (get_param_value(option, 128, "cpus", optarg) == 0) {
970 node_cpumask[nodenr] = 0;
971 } else {
972 value = strtoull(option, &endptr, 10);
973 if (value >= 64) {
974 value = 63;
975 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
976 } else {
977 if (*endptr == '-') {
978 endvalue = strtoull(endptr+1, &endptr, 10);
979 if (endvalue >= 63) {
980 endvalue = 62;
981 fprintf(stderr,
982 "only 63 CPUs in NUMA mode supported.\n");
984 value = (2ULL << endvalue) - (1ULL << value);
985 } else {
986 value = 1ULL << value;
989 node_cpumask[nodenr] = value;
991 nb_numa_nodes++;
993 return;
996 static void smp_parse(const char *optarg)
998 int smp, sockets = 0, threads = 0, cores = 0;
999 char *endptr;
1000 char option[128];
1002 smp = strtoul(optarg, &endptr, 10);
1003 if (endptr != optarg) {
1004 if (*endptr == ',') {
1005 endptr++;
1008 if (get_param_value(option, 128, "sockets", endptr) != 0)
1009 sockets = strtoull(option, NULL, 10);
1010 if (get_param_value(option, 128, "cores", endptr) != 0)
1011 cores = strtoull(option, NULL, 10);
1012 if (get_param_value(option, 128, "threads", endptr) != 0)
1013 threads = strtoull(option, NULL, 10);
1014 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1015 max_cpus = strtoull(option, NULL, 10);
1017 /* compute missing values, prefer sockets over cores over threads */
1018 if (smp == 0 || sockets == 0) {
1019 sockets = sockets > 0 ? sockets : 1;
1020 cores = cores > 0 ? cores : 1;
1021 threads = threads > 0 ? threads : 1;
1022 if (smp == 0) {
1023 smp = cores * threads * sockets;
1025 } else {
1026 if (cores == 0) {
1027 threads = threads > 0 ? threads : 1;
1028 cores = smp / (sockets * threads);
1029 } else {
1030 threads = smp / (cores * sockets);
1033 smp_cpus = smp;
1034 smp_cores = cores > 0 ? cores : 1;
1035 smp_threads = threads > 0 ? threads : 1;
1036 if (max_cpus == 0)
1037 max_cpus = smp_cpus;
1040 /***********************************************************/
1041 /* USB devices */
1043 static int usb_device_add(const char *devname)
1045 const char *p;
1046 USBDevice *dev = NULL;
1048 if (!usb_enabled)
1049 return -1;
1051 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1052 dev = usbdevice_create(devname);
1053 if (dev)
1054 goto done;
1056 /* the other ones */
1057 #ifndef CONFIG_LINUX
1058 /* only the linux version is qdev-ified, usb-bsd still needs this */
1059 if (strstart(devname, "host:", &p)) {
1060 dev = usb_host_device_open(p);
1061 } else
1062 #endif
1063 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1064 dev = usb_bt_init(devname[2] ? hci_init(p) :
1065 bt_new_hci(qemu_find_bt_vlan(0)));
1066 } else {
1067 return -1;
1069 if (!dev)
1070 return -1;
1072 done:
1073 return 0;
1076 static int usb_device_del(const char *devname)
1078 int bus_num, addr;
1079 const char *p;
1081 if (strstart(devname, "host:", &p))
1082 return usb_host_device_close(p);
1084 if (!usb_enabled)
1085 return -1;
1087 p = strchr(devname, '.');
1088 if (!p)
1089 return -1;
1090 bus_num = strtoul(devname, NULL, 0);
1091 addr = strtoul(p + 1, NULL, 0);
1093 return usb_device_delete_addr(bus_num, addr);
1096 static int usb_parse(const char *cmdline)
1098 int r;
1099 r = usb_device_add(cmdline);
1100 if (r < 0) {
1101 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1103 return r;
1106 void do_usb_add(Monitor *mon, const QDict *qdict)
1108 const char *devname = qdict_get_str(qdict, "devname");
1109 if (usb_device_add(devname) < 0) {
1110 error_report("could not add USB device '%s'", devname);
1114 void do_usb_del(Monitor *mon, const QDict *qdict)
1116 const char *devname = qdict_get_str(qdict, "devname");
1117 if (usb_device_del(devname) < 0) {
1118 error_report("could not delete USB device '%s'", devname);
1122 /***********************************************************/
1123 /* PCMCIA/Cardbus */
1125 static struct pcmcia_socket_entry_s {
1126 PCMCIASocket *socket;
1127 struct pcmcia_socket_entry_s *next;
1128 } *pcmcia_sockets = 0;
1130 void pcmcia_socket_register(PCMCIASocket *socket)
1132 struct pcmcia_socket_entry_s *entry;
1134 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1135 entry->socket = socket;
1136 entry->next = pcmcia_sockets;
1137 pcmcia_sockets = entry;
1140 void pcmcia_socket_unregister(PCMCIASocket *socket)
1142 struct pcmcia_socket_entry_s *entry, **ptr;
1144 ptr = &pcmcia_sockets;
1145 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1146 if (entry->socket == socket) {
1147 *ptr = entry->next;
1148 g_free(entry);
1152 void pcmcia_info(Monitor *mon)
1154 struct pcmcia_socket_entry_s *iter;
1156 if (!pcmcia_sockets)
1157 monitor_printf(mon, "No PCMCIA sockets\n");
1159 for (iter = pcmcia_sockets; iter; iter = iter->next)
1160 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1161 iter->socket->attached ? iter->socket->card_string :
1162 "Empty");
1165 /***********************************************************/
1166 /* machine registration */
1168 static QEMUMachine *first_machine = NULL;
1169 QEMUMachine *current_machine = NULL;
1171 int qemu_register_machine(QEMUMachine *m)
1173 QEMUMachine **pm;
1174 pm = &first_machine;
1175 while (*pm != NULL)
1176 pm = &(*pm)->next;
1177 m->next = NULL;
1178 *pm = m;
1179 return 0;
1182 static QEMUMachine *find_machine(const char *name)
1184 QEMUMachine *m;
1186 for(m = first_machine; m != NULL; m = m->next) {
1187 if (!strcmp(m->name, name))
1188 return m;
1189 if (m->alias && !strcmp(m->alias, name))
1190 return m;
1192 return NULL;
1195 static QEMUMachine *find_default_machine(void)
1197 QEMUMachine *m;
1199 for(m = first_machine; m != NULL; m = m->next) {
1200 if (m->is_default) {
1201 return m;
1204 return NULL;
1207 /***********************************************************/
1208 /* main execution loop */
1210 static void gui_update(void *opaque)
1212 uint64_t interval = GUI_REFRESH_INTERVAL;
1213 DisplayState *ds = opaque;
1214 DisplayChangeListener *dcl = ds->listeners;
1216 qemu_flush_coalesced_mmio_buffer();
1217 dpy_refresh(ds);
1219 while (dcl != NULL) {
1220 if (dcl->gui_timer_interval &&
1221 dcl->gui_timer_interval < interval)
1222 interval = dcl->gui_timer_interval;
1223 dcl = dcl->next;
1225 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1228 static void nographic_update(void *opaque)
1230 uint64_t interval = GUI_REFRESH_INTERVAL;
1232 qemu_flush_coalesced_mmio_buffer();
1233 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1236 struct vm_change_state_entry {
1237 VMChangeStateHandler *cb;
1238 void *opaque;
1239 QLIST_ENTRY (vm_change_state_entry) entries;
1242 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1244 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1245 void *opaque)
1247 VMChangeStateEntry *e;
1249 e = g_malloc0(sizeof (*e));
1251 e->cb = cb;
1252 e->opaque = opaque;
1253 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1254 return e;
1257 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1259 QLIST_REMOVE (e, entries);
1260 g_free (e);
1263 void vm_state_notify(int running, RunState state)
1265 VMChangeStateEntry *e;
1267 trace_vm_state_notify(running, state);
1269 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1270 e->cb(e->opaque, running, state);
1274 void vm_start(void)
1276 if (!runstate_is_running()) {
1277 cpu_enable_ticks();
1278 runstate_set(RSTATE_RUNNING);
1279 vm_state_notify(1, RSTATE_RUNNING);
1280 resume_all_vcpus();
1281 monitor_protocol_event(QEVENT_RESUME, NULL);
1285 /* reset/shutdown handler */
1287 typedef struct QEMUResetEntry {
1288 QTAILQ_ENTRY(QEMUResetEntry) entry;
1289 QEMUResetHandler *func;
1290 void *opaque;
1291 } QEMUResetEntry;
1293 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1294 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1295 static int reset_requested;
1296 static int shutdown_requested, shutdown_signal = -1;
1297 static pid_t shutdown_pid;
1298 static int powerdown_requested;
1299 static int debug_requested;
1300 static RunState vmstop_requested = RSTATE_NO_STATE;
1302 int qemu_shutdown_requested_get(void)
1304 return shutdown_requested;
1307 int qemu_reset_requested_get(void)
1309 return reset_requested;
1312 int qemu_shutdown_requested(void)
1314 int r = shutdown_requested;
1315 shutdown_requested = 0;
1316 return r;
1319 void qemu_kill_report(void)
1321 if (shutdown_signal != -1) {
1322 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1323 if (shutdown_pid == 0) {
1324 /* This happens for eg ^C at the terminal, so it's worth
1325 * avoiding printing an odd message in that case.
1327 fputc('\n', stderr);
1328 } else {
1329 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1331 shutdown_signal = -1;
1335 int qemu_reset_requested(void)
1337 int r = reset_requested;
1338 reset_requested = 0;
1339 return r;
1342 int qemu_powerdown_requested(void)
1344 int r = powerdown_requested;
1345 powerdown_requested = 0;
1346 return r;
1349 static int qemu_debug_requested(void)
1351 int r = debug_requested;
1352 debug_requested = 0;
1353 return r;
1356 static RunState qemu_vmstop_requested(void)
1358 RunState s = vmstop_requested;
1359 vmstop_requested = RSTATE_NO_STATE;
1360 return s;
1363 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1365 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1367 re->func = func;
1368 re->opaque = opaque;
1369 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1372 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1374 QEMUResetEntry *re;
1376 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1377 if (re->func == func && re->opaque == opaque) {
1378 QTAILQ_REMOVE(&reset_handlers, re, entry);
1379 g_free(re);
1380 return;
1385 void qemu_system_reset(bool report)
1387 QEMUResetEntry *re, *nre;
1389 /* reset all devices */
1390 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1391 re->func(re->opaque);
1393 if (report) {
1394 monitor_protocol_event(QEVENT_RESET, NULL);
1396 cpu_synchronize_all_post_reset();
1399 void qemu_system_reset_request(void)
1401 if (no_reboot) {
1402 shutdown_requested = 1;
1403 } else {
1404 reset_requested = 1;
1406 cpu_stop_current();
1407 qemu_notify_event();
1410 void qemu_system_killed(int signal, pid_t pid)
1412 shutdown_signal = signal;
1413 shutdown_pid = pid;
1414 no_shutdown = 0;
1415 qemu_system_shutdown_request();
1418 void qemu_system_shutdown_request(void)
1420 shutdown_requested = 1;
1421 qemu_notify_event();
1424 void qemu_system_powerdown_request(void)
1426 powerdown_requested = 1;
1427 qemu_notify_event();
1430 void qemu_system_debug_request(void)
1432 debug_requested = 1;
1433 qemu_notify_event();
1436 void qemu_system_vmstop_request(RunState state)
1438 vmstop_requested = state;
1439 qemu_notify_event();
1442 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1443 static int n_poll_fds;
1444 static int max_priority;
1446 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1447 fd_set *xfds, struct timeval *tv)
1449 GMainContext *context = g_main_context_default();
1450 int i;
1451 int timeout = 0, cur_timeout;
1453 g_main_context_prepare(context, &max_priority);
1455 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1456 poll_fds, ARRAY_SIZE(poll_fds));
1457 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1459 for (i = 0; i < n_poll_fds; i++) {
1460 GPollFD *p = &poll_fds[i];
1462 if ((p->events & G_IO_IN)) {
1463 FD_SET(p->fd, rfds);
1464 *max_fd = MAX(*max_fd, p->fd);
1466 if ((p->events & G_IO_OUT)) {
1467 FD_SET(p->fd, wfds);
1468 *max_fd = MAX(*max_fd, p->fd);
1470 if ((p->events & G_IO_ERR)) {
1471 FD_SET(p->fd, xfds);
1472 *max_fd = MAX(*max_fd, p->fd);
1476 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1477 if (timeout >= 0 && timeout < cur_timeout) {
1478 tv->tv_sec = timeout / 1000;
1479 tv->tv_usec = (timeout % 1000) * 1000;
1483 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1484 bool err)
1486 GMainContext *context = g_main_context_default();
1488 if (!err) {
1489 int i;
1491 for (i = 0; i < n_poll_fds; i++) {
1492 GPollFD *p = &poll_fds[i];
1494 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1495 p->revents |= G_IO_IN;
1497 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1498 p->revents |= G_IO_OUT;
1500 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1501 p->revents |= G_IO_ERR;
1506 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1507 g_main_context_dispatch(context);
1511 int main_loop_wait(int nonblocking)
1513 fd_set rfds, wfds, xfds;
1514 int ret, nfds;
1515 struct timeval tv;
1516 int timeout;
1518 if (nonblocking)
1519 timeout = 0;
1520 else {
1521 timeout = qemu_calculate_timeout();
1522 qemu_bh_update_timeout(&timeout);
1525 os_host_main_loop_wait(&timeout);
1527 tv.tv_sec = timeout / 1000;
1528 tv.tv_usec = (timeout % 1000) * 1000;
1530 /* poll any events */
1531 /* XXX: separate device handlers from system ones */
1532 nfds = -1;
1533 FD_ZERO(&rfds);
1534 FD_ZERO(&wfds);
1535 FD_ZERO(&xfds);
1537 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1538 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1539 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
1541 if (timeout > 0) {
1542 qemu_mutex_unlock_iothread();
1545 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1547 if (timeout > 0) {
1548 qemu_mutex_lock_iothread();
1551 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1552 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1553 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1555 qemu_run_all_timers();
1557 /* Check bottom-halves last in case any of the earlier events triggered
1558 them. */
1559 qemu_bh_poll();
1561 return ret;
1564 qemu_irq qemu_system_powerdown;
1566 static void main_loop(void)
1568 bool nonblocking;
1569 int last_io __attribute__ ((unused)) = 0;
1570 #ifdef CONFIG_PROFILER
1571 int64_t ti;
1572 #endif
1573 int r;
1575 qemu_main_loop_start();
1577 for (;;) {
1578 nonblocking = !kvm_enabled() && last_io > 0;
1579 #ifdef CONFIG_PROFILER
1580 ti = profile_getclock();
1581 #endif
1582 last_io = main_loop_wait(nonblocking);
1583 #ifdef CONFIG_PROFILER
1584 dev_time += profile_getclock() - ti;
1585 #endif
1587 if (qemu_debug_requested()) {
1588 vm_stop(RSTATE_DEBUG);
1590 if (qemu_shutdown_requested()) {
1591 qemu_kill_report();
1592 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1593 if (no_shutdown) {
1594 vm_stop(RSTATE_SHUTDOWN);
1595 } else
1596 break;
1598 if (qemu_reset_requested()) {
1599 pause_all_vcpus();
1600 cpu_synchronize_all_states();
1601 qemu_system_reset(VMRESET_REPORT);
1602 resume_all_vcpus();
1603 if (runstate_check(RSTATE_PANICKED) ||
1604 runstate_check(RSTATE_SHUTDOWN)) {
1605 runstate_set(RSTATE_PAUSED);
1608 if (qemu_powerdown_requested()) {
1609 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1610 qemu_irq_raise(qemu_system_powerdown);
1612 if ((r = qemu_vmstop_requested())) {
1613 vm_stop(r);
1616 bdrv_close_all();
1617 pause_all_vcpus();
1620 static void version(void)
1622 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1625 static void help(int exitcode)
1627 const char *options_help =
1628 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1629 opt_help
1630 #define DEFHEADING(text) stringify(text) "\n"
1631 #include "qemu-options.def"
1632 #undef DEF
1633 #undef DEFHEADING
1634 #undef GEN_DOCS
1636 version();
1637 printf("usage: %s [options] [disk_image]\n"
1638 "\n"
1639 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1640 "\n"
1641 "%s\n"
1642 "During emulation, the following keys are useful:\n"
1643 "ctrl-alt-f toggle full screen\n"
1644 "ctrl-alt-n switch to virtual console 'n'\n"
1645 "ctrl-alt toggle mouse and keyboard grab\n"
1646 "\n"
1647 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1648 "qemu",
1649 options_help);
1650 exit(exitcode);
1653 #define HAS_ARG 0x0001
1655 typedef struct QEMUOption {
1656 const char *name;
1657 int flags;
1658 int index;
1659 uint32_t arch_mask;
1660 } QEMUOption;
1662 static const QEMUOption qemu_options[] = {
1663 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1664 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1665 { option, opt_arg, opt_enum, arch_mask },
1666 #define DEFHEADING(text)
1667 #include "qemu-options.def"
1668 #undef DEF
1669 #undef DEFHEADING
1670 #undef GEN_DOCS
1671 { NULL },
1673 static void select_vgahw (const char *p)
1675 const char *opts;
1677 default_vga = 0;
1678 vga_interface_type = VGA_NONE;
1679 if (strstart(p, "std", &opts)) {
1680 vga_interface_type = VGA_STD;
1681 } else if (strstart(p, "cirrus", &opts)) {
1682 vga_interface_type = VGA_CIRRUS;
1683 } else if (strstart(p, "vmware", &opts)) {
1684 vga_interface_type = VGA_VMWARE;
1685 } else if (strstart(p, "xenfb", &opts)) {
1686 vga_interface_type = VGA_XENFB;
1687 } else if (strstart(p, "qxl", &opts)) {
1688 vga_interface_type = VGA_QXL;
1689 } else if (!strstart(p, "none", &opts)) {
1690 invalid_vga:
1691 fprintf(stderr, "Unknown vga type: %s\n", p);
1692 exit(1);
1694 while (*opts) {
1695 const char *nextopt;
1697 if (strstart(opts, ",retrace=", &nextopt)) {
1698 opts = nextopt;
1699 if (strstart(opts, "dumb", &nextopt))
1700 vga_retrace_method = VGA_RETRACE_DUMB;
1701 else if (strstart(opts, "precise", &nextopt))
1702 vga_retrace_method = VGA_RETRACE_PRECISE;
1703 else goto invalid_vga;
1704 } else goto invalid_vga;
1705 opts = nextopt;
1709 static DisplayType select_display(const char *p)
1711 const char *opts;
1712 DisplayType display = DT_DEFAULT;
1714 if (strstart(p, "sdl", &opts)) {
1715 #ifdef CONFIG_SDL
1716 display = DT_SDL;
1717 while (*opts) {
1718 const char *nextopt;
1720 if (strstart(opts, ",frame=", &nextopt)) {
1721 opts = nextopt;
1722 if (strstart(opts, "on", &nextopt)) {
1723 no_frame = 0;
1724 } else if (strstart(opts, "off", &nextopt)) {
1725 no_frame = 1;
1726 } else {
1727 goto invalid_sdl_args;
1729 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1730 opts = nextopt;
1731 if (strstart(opts, "on", &nextopt)) {
1732 alt_grab = 1;
1733 } else if (strstart(opts, "off", &nextopt)) {
1734 alt_grab = 0;
1735 } else {
1736 goto invalid_sdl_args;
1738 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1739 opts = nextopt;
1740 if (strstart(opts, "on", &nextopt)) {
1741 ctrl_grab = 1;
1742 } else if (strstart(opts, "off", &nextopt)) {
1743 ctrl_grab = 0;
1744 } else {
1745 goto invalid_sdl_args;
1747 } else if (strstart(opts, ",window_close=", &nextopt)) {
1748 opts = nextopt;
1749 if (strstart(opts, "on", &nextopt)) {
1750 no_quit = 0;
1751 } else if (strstart(opts, "off", &nextopt)) {
1752 no_quit = 1;
1753 } else {
1754 goto invalid_sdl_args;
1756 } else {
1757 invalid_sdl_args:
1758 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1759 exit(1);
1761 opts = nextopt;
1763 #else
1764 fprintf(stderr, "SDL support is disabled\n");
1765 exit(1);
1766 #endif
1767 } else if (strstart(p, "vnc", &opts)) {
1768 #ifdef CONFIG_VNC
1769 display_remote++;
1771 if (*opts) {
1772 const char *nextopt;
1774 if (strstart(opts, "=", &nextopt)) {
1775 vnc_display = nextopt;
1778 if (!vnc_display) {
1779 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1780 exit(1);
1782 #else
1783 fprintf(stderr, "VNC support is disabled\n");
1784 exit(1);
1785 #endif
1786 } else if (strstart(p, "curses", &opts)) {
1787 #ifdef CONFIG_CURSES
1788 display = DT_CURSES;
1789 #else
1790 fprintf(stderr, "Curses support is disabled\n");
1791 exit(1);
1792 #endif
1793 } else if (strstart(p, "none", &opts)) {
1794 display = DT_NONE;
1795 } else {
1796 fprintf(stderr, "Unknown display type: %s\n", p);
1797 exit(1);
1800 return display;
1803 static int balloon_parse(const char *arg)
1805 QemuOpts *opts;
1807 if (strcmp(arg, "none") == 0) {
1808 return 0;
1811 if (!strncmp(arg, "virtio", 6)) {
1812 if (arg[6] == ',') {
1813 /* have params -> parse them */
1814 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1815 if (!opts)
1816 return -1;
1817 } else {
1818 /* create empty opts */
1819 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1821 qemu_opt_set(opts, "driver", "virtio-balloon");
1822 return 0;
1825 return -1;
1828 char *qemu_find_file(int type, const char *name)
1830 int len;
1831 const char *subdir;
1832 char *buf;
1834 /* If name contains path separators then try it as a straight path. */
1835 if ((strchr(name, '/') || strchr(name, '\\'))
1836 && access(name, R_OK) == 0) {
1837 return g_strdup(name);
1839 switch (type) {
1840 case QEMU_FILE_TYPE_BIOS:
1841 subdir = "";
1842 break;
1843 case QEMU_FILE_TYPE_KEYMAP:
1844 subdir = "keymaps/";
1845 break;
1846 default:
1847 abort();
1849 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1850 buf = g_malloc0(len);
1851 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1852 if (access(buf, R_OK)) {
1853 g_free(buf);
1854 return NULL;
1856 return buf;
1859 static int device_help_func(QemuOpts *opts, void *opaque)
1861 return qdev_device_help(opts);
1864 static int device_init_func(QemuOpts *opts, void *opaque)
1866 DeviceState *dev;
1868 dev = qdev_device_add(opts);
1869 if (!dev)
1870 return -1;
1871 return 0;
1874 static int chardev_init_func(QemuOpts *opts, void *opaque)
1876 CharDriverState *chr;
1878 chr = qemu_chr_new_from_opts(opts, NULL);
1879 if (!chr)
1880 return -1;
1881 return 0;
1884 #ifdef CONFIG_VIRTFS
1885 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1887 int ret;
1888 ret = qemu_fsdev_add(opts);
1890 return ret;
1892 #endif
1894 static int mon_init_func(QemuOpts *opts, void *opaque)
1896 CharDriverState *chr;
1897 const char *chardev;
1898 const char *mode;
1899 int flags;
1901 mode = qemu_opt_get(opts, "mode");
1902 if (mode == NULL) {
1903 mode = "readline";
1905 if (strcmp(mode, "readline") == 0) {
1906 flags = MONITOR_USE_READLINE;
1907 } else if (strcmp(mode, "control") == 0) {
1908 flags = MONITOR_USE_CONTROL;
1909 } else {
1910 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1911 exit(1);
1914 if (qemu_opt_get_bool(opts, "pretty", 0))
1915 flags |= MONITOR_USE_PRETTY;
1917 if (qemu_opt_get_bool(opts, "default", 0))
1918 flags |= MONITOR_IS_DEFAULT;
1920 chardev = qemu_opt_get(opts, "chardev");
1921 chr = qemu_chr_find(chardev);
1922 if (chr == NULL) {
1923 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1924 exit(1);
1927 monitor_init(chr, flags);
1928 return 0;
1931 static void monitor_parse(const char *optarg, const char *mode)
1933 static int monitor_device_index = 0;
1934 QemuOpts *opts;
1935 const char *p;
1936 char label[32];
1937 int def = 0;
1939 if (strstart(optarg, "chardev:", &p)) {
1940 snprintf(label, sizeof(label), "%s", p);
1941 } else {
1942 snprintf(label, sizeof(label), "compat_monitor%d",
1943 monitor_device_index);
1944 if (monitor_device_index == 0) {
1945 def = 1;
1947 opts = qemu_chr_parse_compat(label, optarg);
1948 if (!opts) {
1949 fprintf(stderr, "parse error: %s\n", optarg);
1950 exit(1);
1954 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1955 if (!opts) {
1956 fprintf(stderr, "duplicate chardev: %s\n", label);
1957 exit(1);
1959 qemu_opt_set(opts, "mode", mode);
1960 qemu_opt_set(opts, "chardev", label);
1961 if (def)
1962 qemu_opt_set(opts, "default", "on");
1963 monitor_device_index++;
1966 struct device_config {
1967 enum {
1968 DEV_USB, /* -usbdevice */
1969 DEV_BT, /* -bt */
1970 DEV_SERIAL, /* -serial */
1971 DEV_PARALLEL, /* -parallel */
1972 DEV_VIRTCON, /* -virtioconsole */
1973 DEV_DEBUGCON, /* -debugcon */
1974 } type;
1975 const char *cmdline;
1976 QTAILQ_ENTRY(device_config) next;
1978 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1980 static void add_device_config(int type, const char *cmdline)
1982 struct device_config *conf;
1984 conf = g_malloc0(sizeof(*conf));
1985 conf->type = type;
1986 conf->cmdline = cmdline;
1987 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1990 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1992 struct device_config *conf;
1993 int rc;
1995 QTAILQ_FOREACH(conf, &device_configs, next) {
1996 if (conf->type != type)
1997 continue;
1998 rc = func(conf->cmdline);
1999 if (0 != rc)
2000 return rc;
2002 return 0;
2005 static int serial_parse(const char *devname)
2007 static int index = 0;
2008 char label[32];
2010 if (strcmp(devname, "none") == 0)
2011 return 0;
2012 if (index == MAX_SERIAL_PORTS) {
2013 fprintf(stderr, "qemu: too many serial ports\n");
2014 exit(1);
2016 snprintf(label, sizeof(label), "serial%d", index);
2017 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2018 if (!serial_hds[index]) {
2019 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2020 devname, strerror(errno));
2021 return -1;
2023 index++;
2024 return 0;
2027 static int parallel_parse(const char *devname)
2029 static int index = 0;
2030 char label[32];
2032 if (strcmp(devname, "none") == 0)
2033 return 0;
2034 if (index == MAX_PARALLEL_PORTS) {
2035 fprintf(stderr, "qemu: too many parallel ports\n");
2036 exit(1);
2038 snprintf(label, sizeof(label), "parallel%d", index);
2039 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2040 if (!parallel_hds[index]) {
2041 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2042 devname, strerror(errno));
2043 return -1;
2045 index++;
2046 return 0;
2049 static int virtcon_parse(const char *devname)
2051 QemuOptsList *device = qemu_find_opts("device");
2052 static int index = 0;
2053 char label[32];
2054 QemuOpts *bus_opts, *dev_opts;
2056 if (strcmp(devname, "none") == 0)
2057 return 0;
2058 if (index == MAX_VIRTIO_CONSOLES) {
2059 fprintf(stderr, "qemu: too many virtio consoles\n");
2060 exit(1);
2063 bus_opts = qemu_opts_create(device, NULL, 0);
2064 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2066 dev_opts = qemu_opts_create(device, NULL, 0);
2067 qemu_opt_set(dev_opts, "driver", "virtconsole");
2069 snprintf(label, sizeof(label), "virtcon%d", index);
2070 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2071 if (!virtcon_hds[index]) {
2072 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2073 devname, strerror(errno));
2074 return -1;
2076 qemu_opt_set(dev_opts, "chardev", label);
2078 index++;
2079 return 0;
2082 static int debugcon_parse(const char *devname)
2084 QemuOpts *opts;
2086 if (!qemu_chr_new("debugcon", devname, NULL)) {
2087 exit(1);
2089 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2090 if (!opts) {
2091 fprintf(stderr, "qemu: already have a debugcon device\n");
2092 exit(1);
2094 qemu_opt_set(opts, "driver", "isa-debugcon");
2095 qemu_opt_set(opts, "chardev", "debugcon");
2096 return 0;
2099 static QEMUMachine *machine_parse(const char *name)
2101 QEMUMachine *m, *machine = NULL;
2103 if (name) {
2104 machine = find_machine(name);
2106 if (machine) {
2107 return machine;
2109 printf("Supported machines are:\n");
2110 for (m = first_machine; m != NULL; m = m->next) {
2111 if (m->alias) {
2112 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2114 printf("%-10s %s%s\n", m->name, m->desc,
2115 m->is_default ? " (default)" : "");
2117 exit(!name || *name != '?');
2120 static int tcg_init(void)
2122 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2123 return 0;
2126 static struct {
2127 const char *opt_name;
2128 const char *name;
2129 int (*available)(void);
2130 int (*init)(void);
2131 int *allowed;
2132 } accel_list[] = {
2133 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2134 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2135 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2138 static int configure_accelerator(void)
2140 const char *p = NULL;
2141 char buf[10];
2142 int i, ret;
2143 bool accel_initalised = 0;
2144 bool init_failed = 0;
2146 QemuOptsList *list = qemu_find_opts("machine");
2147 if (!QTAILQ_EMPTY(&list->head)) {
2148 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2151 if (p == NULL) {
2152 #ifdef CONFIG_KVM
2153 /* Use the default "accelerator", kvm */
2154 p = "kvm";
2155 #else
2156 /* Use the default "accelerator", tcg */
2157 p = "tcg";
2158 #endif
2161 while (!accel_initalised && *p != '\0') {
2162 if (*p == ':') {
2163 p++;
2165 p = get_opt_name(buf, sizeof (buf), p, ':');
2166 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2167 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2168 *(accel_list[i].allowed) = 1;
2169 ret = accel_list[i].init();
2170 if (ret < 0) {
2171 init_failed = 1;
2172 if (!accel_list[i].available()) {
2173 printf("%s not supported for this target\n",
2174 accel_list[i].name);
2175 } else {
2176 fprintf(stderr, "failed to initialize %s: %s\n",
2177 accel_list[i].name,
2178 strerror(-ret));
2180 *(accel_list[i].allowed) = 0;
2181 } else {
2182 accel_initalised = 1;
2184 break;
2187 if (i == ARRAY_SIZE(accel_list)) {
2188 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2192 if (!accel_initalised) {
2193 fprintf(stderr, "No accelerator found!\n");
2194 exit(1);
2197 if (init_failed) {
2198 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2201 return !accel_initalised;
2204 void qemu_add_exit_notifier(Notifier *notify)
2206 notifier_list_add(&exit_notifiers, notify);
2209 void qemu_remove_exit_notifier(Notifier *notify)
2211 notifier_list_remove(&exit_notifiers, notify);
2214 static void qemu_run_exit_notifiers(void)
2216 notifier_list_notify(&exit_notifiers, NULL);
2219 void qemu_add_machine_init_done_notifier(Notifier *notify)
2221 notifier_list_add(&machine_init_done_notifiers, notify);
2224 static void qemu_run_machine_init_done_notifiers(void)
2226 notifier_list_notify(&machine_init_done_notifiers, NULL);
2229 static const QEMUOption *lookup_opt(int argc, char **argv,
2230 const char **poptarg, int *poptind)
2232 const QEMUOption *popt;
2233 int optind = *poptind;
2234 char *r = argv[optind];
2235 const char *optarg;
2237 loc_set_cmdline(argv, optind, 1);
2238 optind++;
2239 /* Treat --foo the same as -foo. */
2240 if (r[1] == '-')
2241 r++;
2242 popt = qemu_options;
2243 for(;;) {
2244 if (!popt->name) {
2245 error_report("invalid option");
2246 exit(1);
2248 if (!strcmp(popt->name, r + 1))
2249 break;
2250 popt++;
2252 if (popt->flags & HAS_ARG) {
2253 if (optind >= argc) {
2254 error_report("requires an argument");
2255 exit(1);
2257 optarg = argv[optind++];
2258 loc_set_cmdline(argv, optind - 2, 2);
2259 } else {
2260 optarg = NULL;
2263 *poptarg = optarg;
2264 *poptind = optind;
2266 return popt;
2269 static gpointer malloc_and_trace(gsize n_bytes)
2271 void *ptr = malloc(n_bytes);
2272 trace_g_malloc(n_bytes, ptr);
2273 return ptr;
2276 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2278 void *ptr = realloc(mem, n_bytes);
2279 trace_g_realloc(mem, n_bytes, ptr);
2280 return ptr;
2283 static void free_and_trace(gpointer mem)
2285 trace_g_free(mem);
2286 free(mem);
2289 int main(int argc, char **argv, char **envp)
2291 const char *gdbstub_dev = NULL;
2292 int i;
2293 int snapshot, linux_boot;
2294 const char *icount_option = NULL;
2295 const char *initrd_filename;
2296 const char *kernel_filename, *kernel_cmdline;
2297 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2298 DisplayState *ds;
2299 DisplayChangeListener *dcl;
2300 int cyls, heads, secs, translation;
2301 QemuOpts *hda_opts = NULL, *opts;
2302 QemuOptsList *olist;
2303 int optind;
2304 const char *optarg;
2305 const char *loadvm = NULL;
2306 QEMUMachine *machine;
2307 const char *cpu_model;
2308 const char *pid_file = NULL;
2309 const char *incoming = NULL;
2310 #ifdef CONFIG_VNC
2311 int show_vnc_port = 0;
2312 #endif
2313 int defconfig = 1;
2314 const char *log_mask = NULL;
2315 const char *log_file = NULL;
2316 GMemVTable mem_trace = {
2317 .malloc = malloc_and_trace,
2318 .realloc = realloc_and_trace,
2319 .free = free_and_trace,
2321 const char *trace_events = NULL;
2322 const char *trace_file = NULL;
2324 atexit(qemu_run_exit_notifiers);
2325 error_set_progname(argv[0]);
2327 g_mem_set_vtable(&mem_trace);
2328 g_thread_init(NULL);
2330 runstate_init();
2332 init_clocks();
2334 qemu_cache_utils_init(envp);
2336 QLIST_INIT (&vm_change_state_head);
2337 os_setup_early_signal_handling();
2339 module_call_init(MODULE_INIT_MACHINE);
2340 machine = find_default_machine();
2341 cpu_model = NULL;
2342 initrd_filename = NULL;
2343 ram_size = 0;
2344 snapshot = 0;
2345 kernel_filename = NULL;
2346 kernel_cmdline = "";
2347 cyls = heads = secs = 0;
2348 translation = BIOS_ATA_TRANSLATION_AUTO;
2350 for (i = 0; i < MAX_NODES; i++) {
2351 node_mem[i] = 0;
2352 node_cpumask[i] = 0;
2355 nb_numa_nodes = 0;
2356 nb_nics = 0;
2358 autostart= 1;
2360 /* first pass of option parsing */
2361 optind = 1;
2362 while (optind < argc) {
2363 if (argv[optind][0] != '-') {
2364 /* disk image */
2365 optind++;
2366 continue;
2367 } else {
2368 const QEMUOption *popt;
2370 popt = lookup_opt(argc, argv, &optarg, &optind);
2371 switch (popt->index) {
2372 case QEMU_OPTION_nodefconfig:
2373 defconfig=0;
2374 break;
2379 if (defconfig) {
2380 int ret;
2382 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2383 if (ret < 0 && ret != -ENOENT) {
2384 exit(1);
2387 ret = qemu_read_config_file(arch_config_name);
2388 if (ret < 0 && ret != -ENOENT) {
2389 exit(1);
2392 cpudef_init();
2394 /* second pass of option parsing */
2395 optind = 1;
2396 for(;;) {
2397 if (optind >= argc)
2398 break;
2399 if (argv[optind][0] != '-') {
2400 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2401 } else {
2402 const QEMUOption *popt;
2404 popt = lookup_opt(argc, argv, &optarg, &optind);
2405 if (!(popt->arch_mask & arch_type)) {
2406 printf("Option %s not supported for this target\n", popt->name);
2407 exit(1);
2409 switch(popt->index) {
2410 case QEMU_OPTION_M:
2411 machine = machine_parse(optarg);
2412 break;
2413 case QEMU_OPTION_cpu:
2414 /* hw initialization will check this */
2415 if (*optarg == '?') {
2416 list_cpus(stdout, &fprintf, optarg);
2417 exit(0);
2418 } else {
2419 cpu_model = optarg;
2421 break;
2422 case QEMU_OPTION_initrd:
2423 initrd_filename = optarg;
2424 break;
2425 case QEMU_OPTION_hda:
2427 char buf[256];
2428 if (cyls == 0)
2429 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2430 else
2431 snprintf(buf, sizeof(buf),
2432 "%s,cyls=%d,heads=%d,secs=%d%s",
2433 HD_OPTS , cyls, heads, secs,
2434 translation == BIOS_ATA_TRANSLATION_LBA ?
2435 ",trans=lba" :
2436 translation == BIOS_ATA_TRANSLATION_NONE ?
2437 ",trans=none" : "");
2438 drive_add(IF_DEFAULT, 0, optarg, buf);
2439 break;
2441 case QEMU_OPTION_hdb:
2442 case QEMU_OPTION_hdc:
2443 case QEMU_OPTION_hdd:
2444 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2445 HD_OPTS);
2446 break;
2447 case QEMU_OPTION_drive:
2448 if (drive_def(optarg) == NULL) {
2449 exit(1);
2451 break;
2452 case QEMU_OPTION_set:
2453 if (qemu_set_option(optarg) != 0)
2454 exit(1);
2455 break;
2456 case QEMU_OPTION_global:
2457 if (qemu_global_option(optarg) != 0)
2458 exit(1);
2459 break;
2460 case QEMU_OPTION_mtdblock:
2461 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2462 break;
2463 case QEMU_OPTION_sd:
2464 drive_add(IF_SD, 0, optarg, SD_OPTS);
2465 break;
2466 case QEMU_OPTION_pflash:
2467 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2468 break;
2469 case QEMU_OPTION_snapshot:
2470 snapshot = 1;
2471 break;
2472 case QEMU_OPTION_hdachs:
2474 const char *p;
2475 p = optarg;
2476 cyls = strtol(p, (char **)&p, 0);
2477 if (cyls < 1 || cyls > 16383)
2478 goto chs_fail;
2479 if (*p != ',')
2480 goto chs_fail;
2481 p++;
2482 heads = strtol(p, (char **)&p, 0);
2483 if (heads < 1 || heads > 16)
2484 goto chs_fail;
2485 if (*p != ',')
2486 goto chs_fail;
2487 p++;
2488 secs = strtol(p, (char **)&p, 0);
2489 if (secs < 1 || secs > 63)
2490 goto chs_fail;
2491 if (*p == ',') {
2492 p++;
2493 if (!strcmp(p, "none"))
2494 translation = BIOS_ATA_TRANSLATION_NONE;
2495 else if (!strcmp(p, "lba"))
2496 translation = BIOS_ATA_TRANSLATION_LBA;
2497 else if (!strcmp(p, "auto"))
2498 translation = BIOS_ATA_TRANSLATION_AUTO;
2499 else
2500 goto chs_fail;
2501 } else if (*p != '\0') {
2502 chs_fail:
2503 fprintf(stderr, "qemu: invalid physical CHS format\n");
2504 exit(1);
2506 if (hda_opts != NULL) {
2507 char num[16];
2508 snprintf(num, sizeof(num), "%d", cyls);
2509 qemu_opt_set(hda_opts, "cyls", num);
2510 snprintf(num, sizeof(num), "%d", heads);
2511 qemu_opt_set(hda_opts, "heads", num);
2512 snprintf(num, sizeof(num), "%d", secs);
2513 qemu_opt_set(hda_opts, "secs", num);
2514 if (translation == BIOS_ATA_TRANSLATION_LBA)
2515 qemu_opt_set(hda_opts, "trans", "lba");
2516 if (translation == BIOS_ATA_TRANSLATION_NONE)
2517 qemu_opt_set(hda_opts, "trans", "none");
2520 break;
2521 case QEMU_OPTION_numa:
2522 if (nb_numa_nodes >= MAX_NODES) {
2523 fprintf(stderr, "qemu: too many NUMA nodes\n");
2524 exit(1);
2526 numa_add(optarg);
2527 break;
2528 case QEMU_OPTION_display:
2529 display_type = select_display(optarg);
2530 break;
2531 case QEMU_OPTION_nographic:
2532 display_type = DT_NOGRAPHIC;
2533 break;
2534 case QEMU_OPTION_curses:
2535 #ifdef CONFIG_CURSES
2536 display_type = DT_CURSES;
2537 #else
2538 fprintf(stderr, "Curses support is disabled\n");
2539 exit(1);
2540 #endif
2541 break;
2542 case QEMU_OPTION_portrait:
2543 graphic_rotate = 90;
2544 break;
2545 case QEMU_OPTION_rotate:
2546 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2547 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2548 graphic_rotate != 180 && graphic_rotate != 270) {
2549 fprintf(stderr,
2550 "qemu: only 90, 180, 270 deg rotation is available\n");
2551 exit(1);
2553 break;
2554 case QEMU_OPTION_kernel:
2555 kernel_filename = optarg;
2556 break;
2557 case QEMU_OPTION_append:
2558 kernel_cmdline = optarg;
2559 break;
2560 case QEMU_OPTION_cdrom:
2561 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2562 break;
2563 case QEMU_OPTION_boot:
2565 static const char * const params[] = {
2566 "order", "once", "menu",
2567 "splash", "splash-time", NULL
2569 char buf[sizeof(boot_devices)];
2570 char *standard_boot_devices;
2571 int legacy = 0;
2573 if (!strchr(optarg, '=')) {
2574 legacy = 1;
2575 pstrcpy(buf, sizeof(buf), optarg);
2576 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2577 fprintf(stderr,
2578 "qemu: unknown boot parameter '%s' in '%s'\n",
2579 buf, optarg);
2580 exit(1);
2583 if (legacy ||
2584 get_param_value(buf, sizeof(buf), "order", optarg)) {
2585 validate_bootdevices(buf);
2586 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2588 if (!legacy) {
2589 if (get_param_value(buf, sizeof(buf),
2590 "once", optarg)) {
2591 validate_bootdevices(buf);
2592 standard_boot_devices = g_strdup(boot_devices);
2593 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2594 qemu_register_reset(restore_boot_devices,
2595 standard_boot_devices);
2597 if (get_param_value(buf, sizeof(buf),
2598 "menu", optarg)) {
2599 if (!strcmp(buf, "on")) {
2600 boot_menu = 1;
2601 } else if (!strcmp(buf, "off")) {
2602 boot_menu = 0;
2603 } else {
2604 fprintf(stderr,
2605 "qemu: invalid option value '%s'\n",
2606 buf);
2607 exit(1);
2610 qemu_opts_parse(qemu_find_opts("boot-opts"),
2611 optarg, 0);
2614 break;
2615 case QEMU_OPTION_fda:
2616 case QEMU_OPTION_fdb:
2617 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2618 optarg, FD_OPTS);
2619 break;
2620 case QEMU_OPTION_no_fd_bootchk:
2621 fd_bootchk = 0;
2622 break;
2623 case QEMU_OPTION_netdev:
2624 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2625 exit(1);
2627 break;
2628 case QEMU_OPTION_net:
2629 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2630 exit(1);
2632 break;
2633 #ifdef CONFIG_SLIRP
2634 case QEMU_OPTION_tftp:
2635 legacy_tftp_prefix = optarg;
2636 break;
2637 case QEMU_OPTION_bootp:
2638 legacy_bootp_filename = optarg;
2639 break;
2640 case QEMU_OPTION_redir:
2641 if (net_slirp_redir(optarg) < 0)
2642 exit(1);
2643 break;
2644 #endif
2645 case QEMU_OPTION_bt:
2646 add_device_config(DEV_BT, optarg);
2647 break;
2648 case QEMU_OPTION_audio_help:
2649 if (!(audio_available())) {
2650 printf("Option %s not supported for this target\n", popt->name);
2651 exit(1);
2653 AUD_help ();
2654 exit (0);
2655 break;
2656 case QEMU_OPTION_soundhw:
2657 if (!(audio_available())) {
2658 printf("Option %s not supported for this target\n", popt->name);
2659 exit(1);
2661 select_soundhw (optarg);
2662 break;
2663 case QEMU_OPTION_h:
2664 help(0);
2665 break;
2666 case QEMU_OPTION_version:
2667 version();
2668 exit(0);
2669 break;
2670 case QEMU_OPTION_m: {
2671 int64_t value;
2673 value = strtosz(optarg, NULL);
2674 if (value < 0) {
2675 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2676 exit(1);
2679 if (value != (uint64_t)(ram_addr_t)value) {
2680 fprintf(stderr, "qemu: ram size too large\n");
2681 exit(1);
2683 ram_size = value;
2684 break;
2686 case QEMU_OPTION_mempath:
2687 mem_path = optarg;
2688 break;
2689 #ifdef MAP_POPULATE
2690 case QEMU_OPTION_mem_prealloc:
2691 mem_prealloc = 1;
2692 break;
2693 #endif
2694 case QEMU_OPTION_d:
2695 log_mask = optarg;
2696 break;
2697 case QEMU_OPTION_D:
2698 log_file = optarg;
2699 break;
2700 case QEMU_OPTION_s:
2701 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2702 break;
2703 case QEMU_OPTION_gdb:
2704 gdbstub_dev = optarg;
2705 break;
2706 case QEMU_OPTION_L:
2707 data_dir = optarg;
2708 break;
2709 case QEMU_OPTION_bios:
2710 bios_name = optarg;
2711 break;
2712 case QEMU_OPTION_singlestep:
2713 singlestep = 1;
2714 break;
2715 case QEMU_OPTION_S:
2716 autostart = 0;
2717 break;
2718 case QEMU_OPTION_k:
2719 keyboard_layout = optarg;
2720 break;
2721 case QEMU_OPTION_localtime:
2722 rtc_utc = 0;
2723 break;
2724 case QEMU_OPTION_vga:
2725 select_vgahw (optarg);
2726 break;
2727 case QEMU_OPTION_g:
2729 const char *p;
2730 int w, h, depth;
2731 p = optarg;
2732 w = strtol(p, (char **)&p, 10);
2733 if (w <= 0) {
2734 graphic_error:
2735 fprintf(stderr, "qemu: invalid resolution or depth\n");
2736 exit(1);
2738 if (*p != 'x')
2739 goto graphic_error;
2740 p++;
2741 h = strtol(p, (char **)&p, 10);
2742 if (h <= 0)
2743 goto graphic_error;
2744 if (*p == 'x') {
2745 p++;
2746 depth = strtol(p, (char **)&p, 10);
2747 if (depth != 8 && depth != 15 && depth != 16 &&
2748 depth != 24 && depth != 32)
2749 goto graphic_error;
2750 } else if (*p == '\0') {
2751 depth = graphic_depth;
2752 } else {
2753 goto graphic_error;
2756 graphic_width = w;
2757 graphic_height = h;
2758 graphic_depth = depth;
2760 break;
2761 case QEMU_OPTION_echr:
2763 char *r;
2764 term_escape_char = strtol(optarg, &r, 0);
2765 if (r == optarg)
2766 printf("Bad argument to echr\n");
2767 break;
2769 case QEMU_OPTION_monitor:
2770 monitor_parse(optarg, "readline");
2771 default_monitor = 0;
2772 break;
2773 case QEMU_OPTION_qmp:
2774 monitor_parse(optarg, "control");
2775 default_monitor = 0;
2776 break;
2777 case QEMU_OPTION_mon:
2778 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2779 if (!opts) {
2780 exit(1);
2782 default_monitor = 0;
2783 break;
2784 case QEMU_OPTION_chardev:
2785 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2786 if (!opts) {
2787 exit(1);
2789 break;
2790 case QEMU_OPTION_fsdev:
2791 olist = qemu_find_opts("fsdev");
2792 if (!olist) {
2793 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2794 exit(1);
2796 opts = qemu_opts_parse(olist, optarg, 1);
2797 if (!opts) {
2798 fprintf(stderr, "parse error: %s\n", optarg);
2799 exit(1);
2801 break;
2802 case QEMU_OPTION_virtfs: {
2803 QemuOpts *fsdev;
2804 QemuOpts *device;
2806 olist = qemu_find_opts("virtfs");
2807 if (!olist) {
2808 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2809 exit(1);
2811 opts = qemu_opts_parse(olist, optarg, 1);
2812 if (!opts) {
2813 fprintf(stderr, "parse error: %s\n", optarg);
2814 exit(1);
2817 if (qemu_opt_get(opts, "fstype") == NULL ||
2818 qemu_opt_get(opts, "mount_tag") == NULL ||
2819 qemu_opt_get(opts, "path") == NULL ||
2820 qemu_opt_get(opts, "security_model") == NULL) {
2821 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2822 "security_model=[mapped|passthrough|none],"
2823 "mount_tag=tag.\n");
2824 exit(1);
2827 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2828 qemu_opt_get(opts, "mount_tag"), 1);
2829 if (!fsdev) {
2830 fprintf(stderr, "duplicate fsdev id: %s\n",
2831 qemu_opt_get(opts, "mount_tag"));
2832 exit(1);
2834 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2835 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2836 qemu_opt_set(fsdev, "security_model",
2837 qemu_opt_get(opts, "security_model"));
2839 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2840 qemu_opt_set(device, "driver", "virtio-9p-pci");
2841 qemu_opt_set(device, "fsdev",
2842 qemu_opt_get(opts, "mount_tag"));
2843 qemu_opt_set(device, "mount_tag",
2844 qemu_opt_get(opts, "mount_tag"));
2845 break;
2847 case QEMU_OPTION_serial:
2848 add_device_config(DEV_SERIAL, optarg);
2849 default_serial = 0;
2850 if (strncmp(optarg, "mon:", 4) == 0) {
2851 default_monitor = 0;
2853 break;
2854 case QEMU_OPTION_watchdog:
2855 if (watchdog) {
2856 fprintf(stderr,
2857 "qemu: only one watchdog option may be given\n");
2858 return 1;
2860 watchdog = optarg;
2861 break;
2862 case QEMU_OPTION_watchdog_action:
2863 if (select_watchdog_action(optarg) == -1) {
2864 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2865 exit(1);
2867 break;
2868 case QEMU_OPTION_virtiocon:
2869 add_device_config(DEV_VIRTCON, optarg);
2870 default_virtcon = 0;
2871 if (strncmp(optarg, "mon:", 4) == 0) {
2872 default_monitor = 0;
2874 break;
2875 case QEMU_OPTION_parallel:
2876 add_device_config(DEV_PARALLEL, optarg);
2877 default_parallel = 0;
2878 if (strncmp(optarg, "mon:", 4) == 0) {
2879 default_monitor = 0;
2881 break;
2882 case QEMU_OPTION_debugcon:
2883 add_device_config(DEV_DEBUGCON, optarg);
2884 break;
2885 case QEMU_OPTION_loadvm:
2886 loadvm = optarg;
2887 break;
2888 case QEMU_OPTION_full_screen:
2889 full_screen = 1;
2890 break;
2891 #ifdef CONFIG_SDL
2892 case QEMU_OPTION_no_frame:
2893 no_frame = 1;
2894 break;
2895 case QEMU_OPTION_alt_grab:
2896 alt_grab = 1;
2897 break;
2898 case QEMU_OPTION_ctrl_grab:
2899 ctrl_grab = 1;
2900 break;
2901 case QEMU_OPTION_no_quit:
2902 no_quit = 1;
2903 break;
2904 case QEMU_OPTION_sdl:
2905 display_type = DT_SDL;
2906 break;
2907 #else
2908 case QEMU_OPTION_no_frame:
2909 case QEMU_OPTION_alt_grab:
2910 case QEMU_OPTION_ctrl_grab:
2911 case QEMU_OPTION_no_quit:
2912 case QEMU_OPTION_sdl:
2913 fprintf(stderr, "SDL support is disabled\n");
2914 exit(1);
2915 #endif
2916 case QEMU_OPTION_pidfile:
2917 pid_file = optarg;
2918 break;
2919 case QEMU_OPTION_win2k_hack:
2920 win2k_install_hack = 1;
2921 break;
2922 case QEMU_OPTION_rtc_td_hack:
2923 rtc_td_hack = 1;
2924 break;
2925 case QEMU_OPTION_acpitable:
2926 do_acpitable_option(optarg);
2927 break;
2928 case QEMU_OPTION_smbios:
2929 do_smbios_option(optarg);
2930 break;
2931 case QEMU_OPTION_enable_kvm:
2932 olist = qemu_find_opts("machine");
2933 qemu_opts_reset(olist);
2934 qemu_opts_parse(olist, "accel=kvm", 0);
2935 break;
2936 case QEMU_OPTION_machine:
2937 olist = qemu_find_opts("machine");
2938 qemu_opts_reset(olist);
2939 opts = qemu_opts_parse(olist, optarg, 1);
2940 if (!opts) {
2941 fprintf(stderr, "parse error: %s\n", optarg);
2942 exit(1);
2944 optarg = qemu_opt_get(opts, "type");
2945 if (optarg) {
2946 machine = machine_parse(optarg);
2948 break;
2949 case QEMU_OPTION_no_kvm:
2950 olist = qemu_find_opts("machine");
2951 qemu_opts_reset(olist);
2952 qemu_opts_parse(olist, "accel=tcg", 0);
2953 break;
2954 #ifdef CONFIG_KVM
2955 case QEMU_OPTION_no_kvm_irqchip: {
2956 kvm_irqchip = 0;
2957 kvm_pit = 0;
2958 break;
2960 case QEMU_OPTION_no_kvm_pit: {
2961 kvm_pit = 0;
2962 break;
2964 case QEMU_OPTION_no_kvm_pit_reinjection: {
2965 kvm_pit_reinject = 0;
2966 break;
2968 #endif
2969 case QEMU_OPTION_usb:
2970 usb_enabled = 1;
2971 break;
2972 case QEMU_OPTION_usbdevice:
2973 usb_enabled = 1;
2974 add_device_config(DEV_USB, optarg);
2975 break;
2976 case QEMU_OPTION_device:
2977 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2978 exit(1);
2980 break;
2981 case QEMU_OPTION_smp:
2982 smp_parse(optarg);
2983 if (smp_cpus < 1) {
2984 fprintf(stderr, "Invalid number of CPUs\n");
2985 exit(1);
2987 if (max_cpus < smp_cpus) {
2988 fprintf(stderr, "maxcpus must be equal to or greater than "
2989 "smp\n");
2990 exit(1);
2992 if (max_cpus > 255) {
2993 fprintf(stderr, "Unsupported number of maxcpus\n");
2994 exit(1);
2996 break;
2997 case QEMU_OPTION_vnc:
2998 #ifdef CONFIG_VNC
2999 display_remote++;
3000 vnc_display = optarg;
3001 #else
3002 fprintf(stderr, "VNC support is disabled\n");
3003 exit(1);
3004 #endif
3005 break;
3006 case QEMU_OPTION_no_acpi:
3007 acpi_enabled = 0;
3008 break;
3009 case QEMU_OPTION_no_hpet:
3010 no_hpet = 1;
3011 break;
3012 case QEMU_OPTION_balloon:
3013 if (balloon_parse(optarg) < 0) {
3014 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3015 exit(1);
3017 break;
3018 case QEMU_OPTION_no_reboot:
3019 no_reboot = 1;
3020 break;
3021 case QEMU_OPTION_no_shutdown:
3022 no_shutdown = 1;
3023 break;
3024 case QEMU_OPTION_show_cursor:
3025 cursor_hide = 0;
3026 break;
3027 case QEMU_OPTION_uuid:
3028 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3029 fprintf(stderr, "Fail to parse UUID string."
3030 " Wrong format.\n");
3031 exit(1);
3033 break;
3034 case QEMU_OPTION_option_rom:
3035 if (nb_option_roms >= MAX_OPTION_ROMS) {
3036 fprintf(stderr, "Too many option ROMs\n");
3037 exit(1);
3039 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3040 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3041 option_rom[nb_option_roms].bootindex =
3042 qemu_opt_get_number(opts, "bootindex", -1);
3043 if (!option_rom[nb_option_roms].name) {
3044 fprintf(stderr, "Option ROM file is not specified\n");
3045 exit(1);
3047 nb_option_roms++;
3048 break;
3049 case QEMU_OPTION_semihosting:
3050 semihosting_enabled = 1;
3051 break;
3052 case QEMU_OPTION_tdf:
3053 time_drift_fix = 1;
3054 break;
3055 case QEMU_OPTION_kvm_shadow_memory:
3056 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
3057 break;
3058 case QEMU_OPTION_name:
3059 qemu_name = g_strdup(optarg);
3061 char *p = strchr(qemu_name, ',');
3062 if (p != NULL) {
3063 *p++ = 0;
3064 if (strncmp(p, "process=", 8)) {
3065 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3066 exit(1);
3068 p += 8;
3069 os_set_proc_name(p);
3072 break;
3073 case QEMU_OPTION_prom_env:
3074 if (nb_prom_envs >= MAX_PROM_ENVS) {
3075 fprintf(stderr, "Too many prom variables\n");
3076 exit(1);
3078 prom_envs[nb_prom_envs] = optarg;
3079 nb_prom_envs++;
3080 break;
3081 case QEMU_OPTION_old_param:
3082 old_param = 1;
3083 break;
3084 case QEMU_OPTION_clock:
3085 configure_alarms(optarg);
3086 break;
3087 case QEMU_OPTION_startdate:
3088 configure_rtc_date_offset(optarg, 1);
3089 break;
3090 case QEMU_OPTION_rtc:
3091 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3092 if (!opts) {
3093 exit(1);
3095 configure_rtc(opts);
3096 break;
3097 case QEMU_OPTION_tb_size:
3098 tcg_tb_size = strtol(optarg, NULL, 0);
3099 if (tcg_tb_size < 0) {
3100 tcg_tb_size = 0;
3102 break;
3103 case QEMU_OPTION_icount:
3104 icount_option = optarg;
3105 break;
3106 case QEMU_OPTION_incoming:
3107 incoming = optarg;
3108 break;
3109 case QEMU_OPTION_nodefaults:
3110 default_serial = 0;
3111 default_parallel = 0;
3112 default_virtcon = 0;
3113 default_monitor = 0;
3114 default_vga = 0;
3115 default_net = 0;
3116 default_floppy = 0;
3117 default_cdrom = 0;
3118 default_sdcard = 0;
3119 break;
3120 #ifndef _WIN32
3121 case QEMU_OPTION_nvram:
3122 nvram = optarg;
3123 break;
3124 #endif
3125 case QEMU_OPTION_xen_domid:
3126 if (!(xen_available())) {
3127 printf("Option %s not supported for this target\n", popt->name);
3128 exit(1);
3130 xen_domid = atoi(optarg);
3131 break;
3132 case QEMU_OPTION_xen_create:
3133 if (!(xen_available())) {
3134 printf("Option %s not supported for this target\n", popt->name);
3135 exit(1);
3137 xen_mode = XEN_CREATE;
3138 break;
3139 case QEMU_OPTION_xen_attach:
3140 if (!(xen_available())) {
3141 printf("Option %s not supported for this target\n", popt->name);
3142 exit(1);
3144 xen_mode = XEN_ATTACH;
3145 break;
3146 case QEMU_OPTION_trace:
3148 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3149 if (!opts) {
3150 exit(1);
3152 trace_events = qemu_opt_get(opts, "events");
3153 trace_file = qemu_opt_get(opts, "file");
3154 break;
3156 case QEMU_OPTION_readconfig:
3158 int ret = qemu_read_config_file(optarg);
3159 if (ret < 0) {
3160 fprintf(stderr, "read config %s: %s\n", optarg,
3161 strerror(-ret));
3162 exit(1);
3164 break;
3166 case QEMU_OPTION_spice:
3167 olist = qemu_find_opts("spice");
3168 if (!olist) {
3169 fprintf(stderr, "spice is not supported by this qemu build.\n");
3170 exit(1);
3172 opts = qemu_opts_parse(olist, optarg, 0);
3173 if (!opts) {
3174 fprintf(stderr, "parse error: %s\n", optarg);
3175 exit(1);
3177 break;
3178 case QEMU_OPTION_writeconfig:
3180 FILE *fp;
3181 if (strcmp(optarg, "-") == 0) {
3182 fp = stdout;
3183 } else {
3184 fp = fopen(optarg, "w");
3185 if (fp == NULL) {
3186 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3187 exit(1);
3190 qemu_config_write(fp);
3191 fclose(fp);
3192 break;
3194 default:
3195 os_parse_cmd_args(popt->index, optarg);
3199 loc_set_none();
3201 /* Open the logfile at this point, if necessary. We can't open the logfile
3202 * when encountering either of the logging options (-d or -D) because the
3203 * other one may be encountered later on the command line, changing the
3204 * location or level of logging.
3206 if (log_mask) {
3207 if (log_file) {
3208 set_cpu_log_filename(log_file);
3210 set_cpu_log(log_mask);
3213 if (!trace_backend_init(trace_events, trace_file)) {
3214 exit(1);
3217 /* If no data_dir is specified then try to find it relative to the
3218 executable path. */
3219 if (!data_dir) {
3220 data_dir = os_find_datadir(argv[0]);
3222 /* If all else fails use the install path specified when building. */
3223 if (!data_dir) {
3224 data_dir = CONFIG_QEMU_DATADIR;
3228 * Default to max_cpus = smp_cpus, in case the user doesn't
3229 * specify a max_cpus value.
3231 if (!max_cpus)
3232 max_cpus = smp_cpus;
3234 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3235 if (smp_cpus > machine->max_cpus) {
3236 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3237 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3238 machine->max_cpus);
3239 exit(1);
3243 * Get the default machine options from the machine if it is not already
3244 * specified either by the configuration file or by the command line.
3246 if (machine->default_machine_opts) {
3247 QemuOptsList *list = qemu_find_opts("machine");
3248 const char *p = NULL;
3250 if (!QTAILQ_EMPTY(&list->head)) {
3251 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3253 if (p == NULL) {
3254 qemu_opts_reset(list);
3255 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3256 if (!opts) {
3257 fprintf(stderr, "parse error for machine %s: %s\n",
3258 machine->name, machine->default_machine_opts);
3259 exit(1);
3264 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3265 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3267 if (machine->no_serial) {
3268 default_serial = 0;
3270 if (machine->no_parallel) {
3271 default_parallel = 0;
3273 if (!machine->use_virtcon) {
3274 default_virtcon = 0;
3276 if (machine->no_vga) {
3277 default_vga = 0;
3279 if (machine->no_floppy) {
3280 default_floppy = 0;
3282 if (machine->no_cdrom) {
3283 default_cdrom = 0;
3285 if (machine->no_sdcard) {
3286 default_sdcard = 0;
3289 if (display_type == DT_NOGRAPHIC) {
3290 if (default_parallel)
3291 add_device_config(DEV_PARALLEL, "null");
3292 if (default_serial && default_monitor) {
3293 add_device_config(DEV_SERIAL, "mon:stdio");
3294 } else if (default_virtcon && default_monitor) {
3295 add_device_config(DEV_VIRTCON, "mon:stdio");
3296 } else {
3297 if (default_serial)
3298 add_device_config(DEV_SERIAL, "stdio");
3299 if (default_virtcon)
3300 add_device_config(DEV_VIRTCON, "stdio");
3301 if (default_monitor)
3302 monitor_parse("stdio", "readline");
3304 } else {
3305 if (default_serial)
3306 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3307 if (default_parallel)
3308 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3309 if (default_monitor)
3310 monitor_parse("vc:80Cx24C", "readline");
3311 if (default_virtcon)
3312 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3314 if (default_vga)
3315 vga_interface_type = VGA_CIRRUS;
3317 socket_init();
3319 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3320 exit(1);
3321 #ifdef CONFIG_VIRTFS
3322 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3323 exit(1);
3325 #endif
3327 os_daemonize();
3329 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3330 os_pidfile_error();
3331 exit(1);
3334 /* init the memory */
3335 if (ram_size == 0) {
3336 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3339 configure_accelerator();
3341 if (qemu_init_main_loop()) {
3342 fprintf(stderr, "qemu_init_main_loop failed\n");
3343 exit(1);
3345 linux_boot = (kernel_filename != NULL);
3347 if (!linux_boot && *kernel_cmdline != '\0') {
3348 fprintf(stderr, "-append only allowed with -kernel option\n");
3349 exit(1);
3352 if (!linux_boot && initrd_filename != NULL) {
3353 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3354 exit(1);
3357 os_set_line_buffering();
3359 if (init_timer_alarm() < 0) {
3360 fprintf(stderr, "could not initialize alarm timer\n");
3361 exit(1);
3363 configure_icount(icount_option);
3365 if (net_init_clients() < 0) {
3366 exit(1);
3369 /* init the bluetooth world */
3370 if (foreach_device_config(DEV_BT, bt_parse))
3371 exit(1);
3373 if (!xen_enabled()) {
3374 /* On 32-bit hosts, QEMU is limited by virtual address space */
3375 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3376 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3377 exit(1);
3381 cpu_exec_init_all();
3383 bdrv_init_with_whitelist();
3385 blk_mig_init();
3387 /* open the virtual block devices */
3388 if (snapshot)
3389 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3390 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3391 exit(1);
3393 default_drive(default_cdrom, snapshot, machine->use_scsi,
3394 IF_DEFAULT, 2, CDROM_OPTS);
3395 default_drive(default_floppy, snapshot, machine->use_scsi,
3396 IF_FLOPPY, 0, FD_OPTS);
3397 default_drive(default_sdcard, snapshot, machine->use_scsi,
3398 IF_SD, 0, SD_OPTS);
3400 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3401 ram_load, NULL);
3403 if (nb_numa_nodes > 0) {
3404 int i;
3406 if (nb_numa_nodes > MAX_NODES) {
3407 nb_numa_nodes = MAX_NODES;
3410 /* If no memory size if given for any node, assume the default case
3411 * and distribute the available memory equally across all nodes
3413 for (i = 0; i < nb_numa_nodes; i++) {
3414 if (node_mem[i] != 0)
3415 break;
3417 if (i == nb_numa_nodes) {
3418 uint64_t usedmem = 0;
3420 /* On Linux, the each node's border has to be 8MB aligned,
3421 * the final node gets the rest.
3423 for (i = 0; i < nb_numa_nodes - 1; i++) {
3424 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3425 usedmem += node_mem[i];
3427 node_mem[i] = ram_size - usedmem;
3430 for (i = 0; i < nb_numa_nodes; i++) {
3431 if (node_cpumask[i] != 0)
3432 break;
3434 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3435 * must cope with this anyway, because there are BIOSes out there in
3436 * real machines which also use this scheme.
3438 if (i == nb_numa_nodes) {
3439 for (i = 0; i < smp_cpus; i++) {
3440 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3445 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3446 exit(1);
3449 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3450 exit(1);
3451 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3452 exit(1);
3453 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3454 exit(1);
3455 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3456 exit(1);
3458 module_call_init(MODULE_INIT_DEVICE);
3460 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3461 exit(0);
3463 if (watchdog) {
3464 i = select_watchdog(watchdog);
3465 if (i > 0)
3466 exit (i == 1 ? 1 : 0);
3469 if (machine->compat_props) {
3470 qdev_prop_register_global_list(machine->compat_props);
3472 qemu_add_globals();
3474 machine->init(ram_size, boot_devices,
3475 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3477 cpu_synchronize_all_post_init();
3479 set_numa_modes();
3481 current_machine = machine;
3483 /* init USB devices */
3484 if (usb_enabled) {
3485 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3486 exit(1);
3489 /* init generic devices */
3490 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3491 exit(1);
3493 net_check_clients();
3495 /* just use the first displaystate for the moment */
3496 ds = get_displaystate();
3498 if (using_spice)
3499 display_remote++;
3500 if (display_type == DT_DEFAULT && !display_remote) {
3501 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3502 display_type = DT_SDL;
3503 #elif defined(CONFIG_VNC)
3504 vnc_display = "localhost:0,to=99";
3505 show_vnc_port = 1;
3506 #else
3507 display_type = DT_NONE;
3508 #endif
3512 /* init local displays */
3513 switch (display_type) {
3514 case DT_NOGRAPHIC:
3515 break;
3516 #if defined(CONFIG_CURSES)
3517 case DT_CURSES:
3518 curses_display_init(ds, full_screen);
3519 break;
3520 #endif
3521 #if defined(CONFIG_SDL)
3522 case DT_SDL:
3523 sdl_display_init(ds, full_screen, no_frame);
3524 break;
3525 #elif defined(CONFIG_COCOA)
3526 case DT_SDL:
3527 cocoa_display_init(ds, full_screen);
3528 break;
3529 #endif
3530 default:
3531 break;
3534 /* must be after terminal init, SDL library changes signal handlers */
3535 os_setup_signal_handling();
3537 #ifdef CONFIG_VNC
3538 /* init remote displays */
3539 if (vnc_display) {
3540 vnc_display_init(ds);
3541 if (vnc_display_open(ds, vnc_display) < 0)
3542 exit(1);
3544 if (show_vnc_port) {
3545 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3548 #endif
3549 #ifdef CONFIG_SPICE
3550 if (using_spice && !qxl_enabled) {
3551 qemu_spice_display_init(ds);
3553 #endif
3555 /* display setup */
3556 dpy_resize(ds);
3557 dcl = ds->listeners;
3558 while (dcl != NULL) {
3559 if (dcl->dpy_refresh != NULL) {
3560 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3561 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3562 break;
3564 dcl = dcl->next;
3566 if (ds->gui_timer == NULL) {
3567 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3568 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3570 text_consoles_set_display(ds);
3572 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3573 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3574 gdbstub_dev);
3575 exit(1);
3578 qdev_machine_creation_done();
3580 if (rom_load_all() != 0) {
3581 fprintf(stderr, "rom loading failed\n");
3582 exit(1);
3585 /* TODO: once all bus devices are qdevified, this should be done
3586 * when bus is created by qdev.c */
3587 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3588 qemu_run_machine_init_done_notifiers();
3590 qemu_system_reset(VMRESET_SILENT);
3591 if (loadvm) {
3592 if (load_vmstate(loadvm) < 0) {
3593 autostart = 0;
3597 if (incoming) {
3598 runstate_set(RSTATE_IN_MIGRATE);
3599 int ret = qemu_start_incoming_migration(incoming);
3600 if (ret < 0) {
3601 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3602 incoming, ret);
3603 exit(ret);
3605 } else if (autostart) {
3606 vm_start();
3607 } else {
3608 runstate_set(RSTATE_PRE_LAUNCH);
3611 os_setup_post();
3613 main_loop();
3614 quit_timers();
3615 net_cleanup();
3616 res_free();
3618 return 0;