qemu-kvm: set default accelerator via target.conf
[qemu-kvm.git] / vl.c
blobaeed54aa7ea879c77c58ef5da55c389bee8840a0
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 "qmp-commands.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 int boot_menu;
233 uint8_t *boot_splash_filedata;
234 int boot_splash_filedata_size;
235 uint8_t qemu_extra_params_fw[2];
237 typedef struct FWBootEntry FWBootEntry;
239 struct FWBootEntry {
240 QTAILQ_ENTRY(FWBootEntry) link;
241 int32_t bootindex;
242 DeviceState *dev;
243 char *suffix;
246 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
248 int nb_numa_nodes;
249 uint64_t node_mem[MAX_NODES];
250 uint64_t node_cpumask[MAX_NODES];
252 uint8_t qemu_uuid[16];
254 static QEMUBootSetHandler *boot_set_handler;
255 static void *boot_set_opaque;
257 static NotifierList exit_notifiers =
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
260 static NotifierList machine_init_done_notifiers =
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
263 static int tcg_allowed = 1;
264 int kvm_allowed = 0;
265 int xen_allowed = 0;
266 uint32_t xen_domid;
267 enum xen_mode xen_mode = XEN_EMULATE;
268 static int tcg_tb_size;
270 static int default_serial = 1;
271 static int default_parallel = 1;
272 static int default_virtcon = 1;
273 static int default_monitor = 1;
274 static int default_vga = 1;
275 static int default_floppy = 1;
276 static int default_cdrom = 1;
277 static int default_sdcard = 1;
279 static struct {
280 const char *driver;
281 int *flag;
282 } default_list[] = {
283 { .driver = "isa-serial", .flag = &default_serial },
284 { .driver = "isa-parallel", .flag = &default_parallel },
285 { .driver = "isa-fdc", .flag = &default_floppy },
286 { .driver = "ide-cd", .flag = &default_cdrom },
287 { .driver = "ide-hd", .flag = &default_cdrom },
288 { .driver = "ide-drive", .flag = &default_cdrom },
289 { .driver = "scsi-cd", .flag = &default_cdrom },
290 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
291 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
292 { .driver = "virtio-serial", .flag = &default_virtcon },
293 { .driver = "VGA", .flag = &default_vga },
294 { .driver = "cirrus-vga", .flag = &default_vga },
295 { .driver = "vmware-svga", .flag = &default_vga },
296 { .driver = "isa-vga", .flag = &default_vga },
297 { .driver = "qxl-vga", .flag = &default_vga },
300 static void res_free(void)
302 if (boot_splash_filedata != NULL) {
303 g_free(boot_splash_filedata);
304 boot_splash_filedata = NULL;
308 static int default_driver_check(QemuOpts *opts, void *opaque)
310 const char *driver = qemu_opt_get(opts, "driver");
311 int i;
313 if (!driver)
314 return 0;
315 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
316 if (strcmp(default_list[i].driver, driver) != 0)
317 continue;
318 *(default_list[i].flag) = 0;
320 return 0;
323 /***********************************************************/
324 /* QEMU state */
326 static RunState current_run_state = RUN_STATE_PRELAUNCH;
328 typedef struct {
329 RunState from;
330 RunState to;
331 } RunStateTransition;
333 static const RunStateTransition runstate_transitions_def[] = {
334 /* from -> to */
335 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
337 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
338 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
340 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
342 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
344 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
346 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
348 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
349 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
350 { RUN_STATE_PRELAUNCH, RUN_STATE_POSTMIGRATE },
352 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
353 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
355 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
357 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
358 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
359 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
360 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
361 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
362 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
363 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
364 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
365 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
367 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
369 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
371 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
373 { RUN_STATE_MAX, RUN_STATE_MAX },
376 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
378 bool runstate_check(RunState state)
380 return current_run_state == state;
383 void runstate_init(void)
385 const RunStateTransition *p;
387 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
389 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
390 runstate_valid_transitions[p->from][p->to] = true;
394 /* This function will abort() on invalid state transitions */
395 void runstate_set(RunState new_state)
397 if (new_state >= RUN_STATE_MAX ||
398 !runstate_valid_transitions[current_run_state][new_state]) {
399 fprintf(stderr, "invalid runstate transition\n");
400 abort();
403 current_run_state = new_state;
406 int runstate_is_running(void)
408 return runstate_check(RUN_STATE_RUNNING);
411 StatusInfo *qmp_query_status(Error **errp)
413 StatusInfo *info = g_malloc0(sizeof(*info));
415 info->running = runstate_is_running();
416 info->singlestep = singlestep;
417 info->status = current_run_state;
419 return info;
422 /***********************************************************/
423 /* real time host monotonic timer */
425 /***********************************************************/
426 /* host time/date access */
427 void qemu_get_timedate(struct tm *tm, int offset)
429 time_t ti;
430 struct tm *ret;
432 time(&ti);
433 ti += offset;
434 if (rtc_date_offset == -1) {
435 if (rtc_utc)
436 ret = gmtime(&ti);
437 else
438 ret = localtime(&ti);
439 } else {
440 ti -= rtc_date_offset;
441 ret = gmtime(&ti);
444 memcpy(tm, ret, sizeof(struct tm));
447 int qemu_timedate_diff(struct tm *tm)
449 time_t seconds;
451 if (rtc_date_offset == -1)
452 if (rtc_utc)
453 seconds = mktimegm(tm);
454 else
455 seconds = mktime(tm);
456 else
457 seconds = mktimegm(tm) + rtc_date_offset;
459 return seconds - time(NULL);
462 void rtc_change_mon_event(struct tm *tm)
464 QObject *data;
466 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
467 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
468 qobject_decref(data);
471 static void configure_rtc_date_offset(const char *startdate, int legacy)
473 time_t rtc_start_date;
474 struct tm tm;
476 if (!strcmp(startdate, "now") && legacy) {
477 rtc_date_offset = -1;
478 } else {
479 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
480 &tm.tm_year,
481 &tm.tm_mon,
482 &tm.tm_mday,
483 &tm.tm_hour,
484 &tm.tm_min,
485 &tm.tm_sec) == 6) {
486 /* OK */
487 } else if (sscanf(startdate, "%d-%d-%d",
488 &tm.tm_year,
489 &tm.tm_mon,
490 &tm.tm_mday) == 3) {
491 tm.tm_hour = 0;
492 tm.tm_min = 0;
493 tm.tm_sec = 0;
494 } else {
495 goto date_fail;
497 tm.tm_year -= 1900;
498 tm.tm_mon--;
499 rtc_start_date = mktimegm(&tm);
500 if (rtc_start_date == -1) {
501 date_fail:
502 fprintf(stderr, "Invalid date format. Valid formats are:\n"
503 "'2006-06-17T16:01:21' or '2006-06-17'\n");
504 exit(1);
506 rtc_date_offset = time(NULL) - rtc_start_date;
510 static void configure_rtc(QemuOpts *opts)
512 const char *value;
514 value = qemu_opt_get(opts, "base");
515 if (value) {
516 if (!strcmp(value, "utc")) {
517 rtc_utc = 1;
518 } else if (!strcmp(value, "localtime")) {
519 rtc_utc = 0;
520 } else {
521 configure_rtc_date_offset(value, 0);
524 value = qemu_opt_get(opts, "clock");
525 if (value) {
526 if (!strcmp(value, "host")) {
527 rtc_clock = host_clock;
528 } else if (!strcmp(value, "vm")) {
529 rtc_clock = vm_clock;
530 } else {
531 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
532 exit(1);
535 value = qemu_opt_get(opts, "driftfix");
536 if (value) {
537 if (!strcmp(value, "slew")) {
538 rtc_td_hack = 1;
539 } else if (!strcmp(value, "none")) {
540 rtc_td_hack = 0;
541 } else {
542 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
543 exit(1);
548 /***********************************************************/
549 /* Bluetooth support */
550 static int nb_hcis;
551 static int cur_hci;
552 static struct HCIInfo *hci_table[MAX_NICS];
554 static struct bt_vlan_s {
555 struct bt_scatternet_s net;
556 int id;
557 struct bt_vlan_s *next;
558 } *first_bt_vlan;
560 /* find or alloc a new bluetooth "VLAN" */
561 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
563 struct bt_vlan_s **pvlan, *vlan;
564 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
565 if (vlan->id == id)
566 return &vlan->net;
568 vlan = g_malloc0(sizeof(struct bt_vlan_s));
569 vlan->id = id;
570 pvlan = &first_bt_vlan;
571 while (*pvlan != NULL)
572 pvlan = &(*pvlan)->next;
573 *pvlan = vlan;
574 return &vlan->net;
577 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
581 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
583 return -ENOTSUP;
586 static struct HCIInfo null_hci = {
587 .cmd_send = null_hci_send,
588 .sco_send = null_hci_send,
589 .acl_send = null_hci_send,
590 .bdaddr_set = null_hci_addr_set,
593 struct HCIInfo *qemu_next_hci(void)
595 if (cur_hci == nb_hcis)
596 return &null_hci;
598 return hci_table[cur_hci++];
601 static struct HCIInfo *hci_init(const char *str)
603 char *endp;
604 struct bt_scatternet_s *vlan = 0;
606 if (!strcmp(str, "null"))
607 /* null */
608 return &null_hci;
609 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
610 /* host[:hciN] */
611 return bt_host_hci(str[4] ? str + 5 : "hci0");
612 else if (!strncmp(str, "hci", 3)) {
613 /* hci[,vlan=n] */
614 if (str[3]) {
615 if (!strncmp(str + 3, ",vlan=", 6)) {
616 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
617 if (*endp)
618 vlan = 0;
620 } else
621 vlan = qemu_find_bt_vlan(0);
622 if (vlan)
623 return bt_new_hci(vlan);
626 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
628 return 0;
631 static int bt_hci_parse(const char *str)
633 struct HCIInfo *hci;
634 bdaddr_t bdaddr;
636 if (nb_hcis >= MAX_NICS) {
637 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
638 return -1;
641 hci = hci_init(str);
642 if (!hci)
643 return -1;
645 bdaddr.b[0] = 0x52;
646 bdaddr.b[1] = 0x54;
647 bdaddr.b[2] = 0x00;
648 bdaddr.b[3] = 0x12;
649 bdaddr.b[4] = 0x34;
650 bdaddr.b[5] = 0x56 + nb_hcis;
651 hci->bdaddr_set(hci, bdaddr.b);
653 hci_table[nb_hcis++] = hci;
655 return 0;
658 static void bt_vhci_add(int vlan_id)
660 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
662 if (!vlan->slave)
663 fprintf(stderr, "qemu: warning: adding a VHCI to "
664 "an empty scatternet %i\n", vlan_id);
666 bt_vhci_init(bt_new_hci(vlan));
669 static struct bt_device_s *bt_device_add(const char *opt)
671 struct bt_scatternet_s *vlan;
672 int vlan_id = 0;
673 char *endp = strstr(opt, ",vlan=");
674 int len = (endp ? endp - opt : strlen(opt)) + 1;
675 char devname[10];
677 pstrcpy(devname, MIN(sizeof(devname), len), opt);
679 if (endp) {
680 vlan_id = strtol(endp + 6, &endp, 0);
681 if (*endp) {
682 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
683 return 0;
687 vlan = qemu_find_bt_vlan(vlan_id);
689 if (!vlan->slave)
690 fprintf(stderr, "qemu: warning: adding a slave device to "
691 "an empty scatternet %i\n", vlan_id);
693 if (!strcmp(devname, "keyboard"))
694 return bt_keyboard_init(vlan);
696 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
697 return 0;
700 static int bt_parse(const char *opt)
702 const char *endp, *p;
703 int vlan;
705 if (strstart(opt, "hci", &endp)) {
706 if (!*endp || *endp == ',') {
707 if (*endp)
708 if (!strstart(endp, ",vlan=", 0))
709 opt = endp + 1;
711 return bt_hci_parse(opt);
713 } else if (strstart(opt, "vhci", &endp)) {
714 if (!*endp || *endp == ',') {
715 if (*endp) {
716 if (strstart(endp, ",vlan=", &p)) {
717 vlan = strtol(p, (char **) &endp, 0);
718 if (*endp) {
719 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
720 return 1;
722 } else {
723 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
724 return 1;
726 } else
727 vlan = 0;
729 bt_vhci_add(vlan);
730 return 0;
732 } else if (strstart(opt, "device:", &endp))
733 return !bt_device_add(endp);
735 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
736 return 1;
739 /***********************************************************/
740 /* QEMU Block devices */
742 #define HD_OPTS "media=disk"
743 #define CDROM_OPTS "media=cdrom"
744 #define FD_OPTS ""
745 #define PFLASH_OPTS ""
746 #define MTD_OPTS ""
747 #define SD_OPTS ""
749 static int drive_init_func(QemuOpts *opts, void *opaque)
751 int *use_scsi = opaque;
753 return drive_init(opts, *use_scsi) == NULL;
756 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
758 if (NULL == qemu_opt_get(opts, "snapshot")) {
759 qemu_opt_set(opts, "snapshot", "on");
761 return 0;
764 static void default_drive(int enable, int snapshot, int use_scsi,
765 BlockInterfaceType type, int index,
766 const char *optstr)
768 QemuOpts *opts;
770 if (type == IF_DEFAULT) {
771 type = use_scsi ? IF_SCSI : IF_IDE;
774 if (!enable || drive_get_by_index(type, index)) {
775 return;
778 opts = drive_add(type, index, NULL, optstr);
779 if (snapshot) {
780 drive_enable_snapshot(opts, NULL);
782 if (!drive_init(opts, use_scsi)) {
783 exit(1);
787 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
789 boot_set_handler = func;
790 boot_set_opaque = opaque;
793 int qemu_boot_set(const char *boot_devices)
795 if (!boot_set_handler) {
796 return -EINVAL;
798 return boot_set_handler(boot_set_opaque, boot_devices);
801 static void validate_bootdevices(char *devices)
803 /* We just do some generic consistency checks */
804 const char *p;
805 int bitmap = 0;
807 for (p = devices; *p != '\0'; p++) {
808 /* Allowed boot devices are:
809 * a-b: floppy disk drives
810 * c-f: IDE disk drives
811 * g-m: machine implementation dependant drives
812 * n-p: network devices
813 * It's up to each machine implementation to check if the given boot
814 * devices match the actual hardware implementation and firmware
815 * features.
817 if (*p < 'a' || *p > 'p') {
818 fprintf(stderr, "Invalid boot device '%c'\n", *p);
819 exit(1);
821 if (bitmap & (1 << (*p - 'a'))) {
822 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
823 exit(1);
825 bitmap |= 1 << (*p - 'a');
829 static void restore_boot_devices(void *opaque)
831 char *standard_boot_devices = opaque;
832 static int first = 1;
834 /* Restore boot order and remove ourselves after the first boot */
835 if (first) {
836 first = 0;
837 return;
840 qemu_boot_set(standard_boot_devices);
842 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
843 g_free(standard_boot_devices);
846 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
847 const char *suffix)
849 FWBootEntry *node, *i;
851 if (bootindex < 0) {
852 return;
855 assert(dev != NULL || suffix != NULL);
857 node = g_malloc0(sizeof(FWBootEntry));
858 node->bootindex = bootindex;
859 node->suffix = suffix ? g_strdup(suffix) : NULL;
860 node->dev = dev;
862 QTAILQ_FOREACH(i, &fw_boot_order, link) {
863 if (i->bootindex == bootindex) {
864 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
865 exit(1);
866 } else if (i->bootindex < bootindex) {
867 continue;
869 QTAILQ_INSERT_BEFORE(i, node, link);
870 return;
872 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
876 * This function returns null terminated string that consist of new line
877 * separated device paths.
879 * memory pointed by "size" is assigned total length of the array in bytes
882 char *get_boot_devices_list(uint32_t *size)
884 FWBootEntry *i;
885 uint32_t total = 0;
886 char *list = NULL;
888 QTAILQ_FOREACH(i, &fw_boot_order, link) {
889 char *devpath = NULL, *bootpath;
890 int len;
892 if (i->dev) {
893 devpath = qdev_get_fw_dev_path(i->dev);
894 assert(devpath);
897 if (i->suffix && devpath) {
898 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
900 bootpath = g_malloc(bootpathlen);
901 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
902 g_free(devpath);
903 } else if (devpath) {
904 bootpath = devpath;
905 } else {
906 bootpath = g_strdup(i->suffix);
907 assert(bootpath);
910 if (total) {
911 list[total-1] = '\n';
913 len = strlen(bootpath) + 1;
914 list = g_realloc(list, total + len);
915 memcpy(&list[total], bootpath, len);
916 total += len;
917 g_free(bootpath);
920 *size = total;
922 return list;
925 static void numa_add(const char *optarg)
927 char option[128];
928 char *endptr;
929 unsigned long long value, endvalue;
930 int nodenr;
932 optarg = get_opt_name(option, 128, optarg, ',') + 1;
933 if (!strcmp(option, "node")) {
934 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
935 nodenr = nb_numa_nodes;
936 } else {
937 nodenr = strtoull(option, NULL, 10);
940 if (get_param_value(option, 128, "mem", optarg) == 0) {
941 node_mem[nodenr] = 0;
942 } else {
943 int64_t sval;
944 sval = strtosz(option, NULL);
945 if (sval < 0) {
946 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
947 exit(1);
949 node_mem[nodenr] = sval;
951 if (get_param_value(option, 128, "cpus", optarg) == 0) {
952 node_cpumask[nodenr] = 0;
953 } else {
954 value = strtoull(option, &endptr, 10);
955 if (value >= 64) {
956 value = 63;
957 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
958 } else {
959 if (*endptr == '-') {
960 endvalue = strtoull(endptr+1, &endptr, 10);
961 if (endvalue >= 63) {
962 endvalue = 62;
963 fprintf(stderr,
964 "only 63 CPUs in NUMA mode supported.\n");
966 value = (2ULL << endvalue) - (1ULL << value);
967 } else {
968 value = 1ULL << value;
971 node_cpumask[nodenr] = value;
973 nb_numa_nodes++;
975 return;
978 static void smp_parse(const char *optarg)
980 int smp, sockets = 0, threads = 0, cores = 0;
981 char *endptr;
982 char option[128];
984 smp = strtoul(optarg, &endptr, 10);
985 if (endptr != optarg) {
986 if (*endptr == ',') {
987 endptr++;
990 if (get_param_value(option, 128, "sockets", endptr) != 0)
991 sockets = strtoull(option, NULL, 10);
992 if (get_param_value(option, 128, "cores", endptr) != 0)
993 cores = strtoull(option, NULL, 10);
994 if (get_param_value(option, 128, "threads", endptr) != 0)
995 threads = strtoull(option, NULL, 10);
996 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
997 max_cpus = strtoull(option, NULL, 10);
999 /* compute missing values, prefer sockets over cores over threads */
1000 if (smp == 0 || sockets == 0) {
1001 sockets = sockets > 0 ? sockets : 1;
1002 cores = cores > 0 ? cores : 1;
1003 threads = threads > 0 ? threads : 1;
1004 if (smp == 0) {
1005 smp = cores * threads * sockets;
1007 } else {
1008 if (cores == 0) {
1009 threads = threads > 0 ? threads : 1;
1010 cores = smp / (sockets * threads);
1011 } else {
1012 threads = smp / (cores * sockets);
1015 smp_cpus = smp;
1016 smp_cores = cores > 0 ? cores : 1;
1017 smp_threads = threads > 0 ? threads : 1;
1018 if (max_cpus == 0)
1019 max_cpus = smp_cpus;
1022 /***********************************************************/
1023 /* USB devices */
1025 static int usb_device_add(const char *devname)
1027 const char *p;
1028 USBDevice *dev = NULL;
1030 if (!usb_enabled)
1031 return -1;
1033 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1034 dev = usbdevice_create(devname);
1035 if (dev)
1036 goto done;
1038 /* the other ones */
1039 #ifndef CONFIG_LINUX
1040 /* only the linux version is qdev-ified, usb-bsd still needs this */
1041 if (strstart(devname, "host:", &p)) {
1042 dev = usb_host_device_open(p);
1043 } else
1044 #endif
1045 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1046 dev = usb_bt_init(devname[2] ? hci_init(p) :
1047 bt_new_hci(qemu_find_bt_vlan(0)));
1048 } else {
1049 return -1;
1051 if (!dev)
1052 return -1;
1054 done:
1055 return 0;
1058 static int usb_device_del(const char *devname)
1060 int bus_num, addr;
1061 const char *p;
1063 if (strstart(devname, "host:", &p))
1064 return usb_host_device_close(p);
1066 if (!usb_enabled)
1067 return -1;
1069 p = strchr(devname, '.');
1070 if (!p)
1071 return -1;
1072 bus_num = strtoul(devname, NULL, 0);
1073 addr = strtoul(p + 1, NULL, 0);
1075 return usb_device_delete_addr(bus_num, addr);
1078 static int usb_parse(const char *cmdline)
1080 int r;
1081 r = usb_device_add(cmdline);
1082 if (r < 0) {
1083 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1085 return r;
1088 void do_usb_add(Monitor *mon, const QDict *qdict)
1090 const char *devname = qdict_get_str(qdict, "devname");
1091 if (usb_device_add(devname) < 0) {
1092 error_report("could not add USB device '%s'", devname);
1096 void do_usb_del(Monitor *mon, const QDict *qdict)
1098 const char *devname = qdict_get_str(qdict, "devname");
1099 if (usb_device_del(devname) < 0) {
1100 error_report("could not delete USB device '%s'", devname);
1104 /***********************************************************/
1105 /* PCMCIA/Cardbus */
1107 static struct pcmcia_socket_entry_s {
1108 PCMCIASocket *socket;
1109 struct pcmcia_socket_entry_s *next;
1110 } *pcmcia_sockets = 0;
1112 void pcmcia_socket_register(PCMCIASocket *socket)
1114 struct pcmcia_socket_entry_s *entry;
1116 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1117 entry->socket = socket;
1118 entry->next = pcmcia_sockets;
1119 pcmcia_sockets = entry;
1122 void pcmcia_socket_unregister(PCMCIASocket *socket)
1124 struct pcmcia_socket_entry_s *entry, **ptr;
1126 ptr = &pcmcia_sockets;
1127 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1128 if (entry->socket == socket) {
1129 *ptr = entry->next;
1130 g_free(entry);
1134 void pcmcia_info(Monitor *mon)
1136 struct pcmcia_socket_entry_s *iter;
1138 if (!pcmcia_sockets)
1139 monitor_printf(mon, "No PCMCIA sockets\n");
1141 for (iter = pcmcia_sockets; iter; iter = iter->next)
1142 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1143 iter->socket->attached ? iter->socket->card_string :
1144 "Empty");
1147 /***********************************************************/
1148 /* machine registration */
1150 static QEMUMachine *first_machine = NULL;
1151 QEMUMachine *current_machine = NULL;
1153 int qemu_register_machine(QEMUMachine *m)
1155 QEMUMachine **pm;
1156 pm = &first_machine;
1157 while (*pm != NULL)
1158 pm = &(*pm)->next;
1159 m->next = NULL;
1160 *pm = m;
1161 return 0;
1164 static QEMUMachine *find_machine(const char *name)
1166 QEMUMachine *m;
1168 for(m = first_machine; m != NULL; m = m->next) {
1169 if (!strcmp(m->name, name))
1170 return m;
1171 if (m->alias && !strcmp(m->alias, name))
1172 return m;
1174 return NULL;
1177 static QEMUMachine *find_default_machine(void)
1179 QEMUMachine *m;
1181 for(m = first_machine; m != NULL; m = m->next) {
1182 if (m->is_default) {
1183 return m;
1186 return NULL;
1189 /***********************************************************/
1190 /* main execution loop */
1192 static void gui_update(void *opaque)
1194 uint64_t interval = GUI_REFRESH_INTERVAL;
1195 DisplayState *ds = opaque;
1196 DisplayChangeListener *dcl = ds->listeners;
1198 dpy_refresh(ds);
1200 while (dcl != NULL) {
1201 if (dcl->gui_timer_interval &&
1202 dcl->gui_timer_interval < interval)
1203 interval = dcl->gui_timer_interval;
1204 dcl = dcl->next;
1206 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1209 struct vm_change_state_entry {
1210 VMChangeStateHandler *cb;
1211 void *opaque;
1212 QLIST_ENTRY (vm_change_state_entry) entries;
1215 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1217 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1218 void *opaque)
1220 VMChangeStateEntry *e;
1222 e = g_malloc0(sizeof (*e));
1224 e->cb = cb;
1225 e->opaque = opaque;
1226 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1227 return e;
1230 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1232 QLIST_REMOVE (e, entries);
1233 g_free (e);
1236 void vm_state_notify(int running, RunState state)
1238 VMChangeStateEntry *e;
1240 trace_vm_state_notify(running, state);
1242 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1243 e->cb(e->opaque, running, state);
1247 void vm_start(void)
1249 if (!runstate_is_running()) {
1250 cpu_enable_ticks();
1251 runstate_set(RUN_STATE_RUNNING);
1252 vm_state_notify(1, RUN_STATE_RUNNING);
1253 resume_all_vcpus();
1254 monitor_protocol_event(QEVENT_RESUME, NULL);
1258 /* reset/shutdown handler */
1260 typedef struct QEMUResetEntry {
1261 QTAILQ_ENTRY(QEMUResetEntry) entry;
1262 QEMUResetHandler *func;
1263 void *opaque;
1264 } QEMUResetEntry;
1266 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1267 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1268 static int reset_requested;
1269 static int shutdown_requested, shutdown_signal = -1;
1270 static pid_t shutdown_pid;
1271 static int powerdown_requested;
1272 static int debug_requested;
1273 static RunState vmstop_requested = RUN_STATE_MAX;
1275 int qemu_shutdown_requested_get(void)
1277 return shutdown_requested;
1280 int qemu_reset_requested_get(void)
1282 return reset_requested;
1285 int qemu_shutdown_requested(void)
1287 int r = shutdown_requested;
1288 shutdown_requested = 0;
1289 return r;
1292 void qemu_kill_report(void)
1294 if (shutdown_signal != -1) {
1295 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1296 if (shutdown_pid == 0) {
1297 /* This happens for eg ^C at the terminal, so it's worth
1298 * avoiding printing an odd message in that case.
1300 fputc('\n', stderr);
1301 } else {
1302 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1304 shutdown_signal = -1;
1308 int qemu_reset_requested(void)
1310 int r = reset_requested;
1311 reset_requested = 0;
1312 return r;
1315 int qemu_powerdown_requested(void)
1317 int r = powerdown_requested;
1318 powerdown_requested = 0;
1319 return r;
1322 static int qemu_debug_requested(void)
1324 int r = debug_requested;
1325 debug_requested = 0;
1326 return r;
1329 /* We use RUN_STATE_MAX but any invalid value will do */
1330 static bool qemu_vmstop_requested(RunState *r)
1332 if (vmstop_requested < RUN_STATE_MAX) {
1333 *r = vmstop_requested;
1334 vmstop_requested = RUN_STATE_MAX;
1335 return true;
1338 return false;
1341 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1343 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1345 re->func = func;
1346 re->opaque = opaque;
1347 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1350 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1352 QEMUResetEntry *re;
1354 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1355 if (re->func == func && re->opaque == opaque) {
1356 QTAILQ_REMOVE(&reset_handlers, re, entry);
1357 g_free(re);
1358 return;
1363 void qemu_system_reset(bool report)
1365 QEMUResetEntry *re, *nre;
1367 /* reset all devices */
1368 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1369 re->func(re->opaque);
1371 if (report) {
1372 monitor_protocol_event(QEVENT_RESET, NULL);
1374 cpu_synchronize_all_post_reset();
1377 void qemu_system_reset_request(void)
1379 if (no_reboot) {
1380 shutdown_requested = 1;
1381 } else {
1382 reset_requested = 1;
1384 cpu_stop_current();
1385 qemu_notify_event();
1388 void qemu_system_killed(int signal, pid_t pid)
1390 shutdown_signal = signal;
1391 shutdown_pid = pid;
1392 no_shutdown = 0;
1393 qemu_system_shutdown_request();
1396 void qemu_system_shutdown_request(void)
1398 shutdown_requested = 1;
1399 qemu_notify_event();
1402 void qemu_system_powerdown_request(void)
1404 powerdown_requested = 1;
1405 qemu_notify_event();
1408 void qemu_system_debug_request(void)
1410 debug_requested = 1;
1411 qemu_notify_event();
1414 void qemu_system_vmstop_request(RunState state)
1416 vmstop_requested = state;
1417 qemu_notify_event();
1420 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1421 static int n_poll_fds;
1422 static int max_priority;
1424 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1425 fd_set *xfds, struct timeval *tv)
1427 GMainContext *context = g_main_context_default();
1428 int i;
1429 int timeout = 0, cur_timeout;
1431 g_main_context_prepare(context, &max_priority);
1433 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1434 poll_fds, ARRAY_SIZE(poll_fds));
1435 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1437 for (i = 0; i < n_poll_fds; i++) {
1438 GPollFD *p = &poll_fds[i];
1440 if ((p->events & G_IO_IN)) {
1441 FD_SET(p->fd, rfds);
1442 *max_fd = MAX(*max_fd, p->fd);
1444 if ((p->events & G_IO_OUT)) {
1445 FD_SET(p->fd, wfds);
1446 *max_fd = MAX(*max_fd, p->fd);
1448 if ((p->events & G_IO_ERR)) {
1449 FD_SET(p->fd, xfds);
1450 *max_fd = MAX(*max_fd, p->fd);
1454 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1455 if (timeout >= 0 && timeout < cur_timeout) {
1456 tv->tv_sec = timeout / 1000;
1457 tv->tv_usec = (timeout % 1000) * 1000;
1461 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1462 bool err)
1464 GMainContext *context = g_main_context_default();
1466 if (!err) {
1467 int i;
1469 for (i = 0; i < n_poll_fds; i++) {
1470 GPollFD *p = &poll_fds[i];
1472 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1473 p->revents |= G_IO_IN;
1475 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1476 p->revents |= G_IO_OUT;
1478 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1479 p->revents |= G_IO_ERR;
1484 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1485 g_main_context_dispatch(context);
1489 int main_loop_wait(int nonblocking)
1491 fd_set rfds, wfds, xfds;
1492 int ret, nfds;
1493 struct timeval tv;
1494 int timeout;
1496 if (nonblocking)
1497 timeout = 0;
1498 else {
1499 timeout = qemu_calculate_timeout();
1500 qemu_bh_update_timeout(&timeout);
1503 os_host_main_loop_wait(&timeout);
1505 tv.tv_sec = timeout / 1000;
1506 tv.tv_usec = (timeout % 1000) * 1000;
1508 /* poll any events */
1509 /* XXX: separate device handlers from system ones */
1510 nfds = -1;
1511 FD_ZERO(&rfds);
1512 FD_ZERO(&wfds);
1513 FD_ZERO(&xfds);
1515 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1516 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1517 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
1519 if (timeout > 0) {
1520 qemu_mutex_unlock_iothread();
1523 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1525 if (timeout > 0) {
1526 qemu_mutex_lock_iothread();
1529 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1530 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1531 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1533 qemu_run_all_timers();
1535 /* Check bottom-halves last in case any of the earlier events triggered
1536 them. */
1537 qemu_bh_poll();
1539 return ret;
1542 qemu_irq qemu_system_powerdown;
1544 static void main_loop(void)
1546 bool nonblocking;
1547 int last_io __attribute__ ((unused)) = 0;
1548 #ifdef CONFIG_PROFILER
1549 int64_t ti;
1550 #endif
1551 RunState r;
1553 qemu_main_loop_start();
1555 for (;;) {
1556 nonblocking = !kvm_enabled() && last_io > 0;
1557 #ifdef CONFIG_PROFILER
1558 ti = profile_getclock();
1559 #endif
1560 last_io = main_loop_wait(nonblocking);
1561 #ifdef CONFIG_PROFILER
1562 dev_time += profile_getclock() - ti;
1563 #endif
1565 if (qemu_debug_requested()) {
1566 vm_stop(RUN_STATE_DEBUG);
1568 if (qemu_shutdown_requested()) {
1569 qemu_kill_report();
1570 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1571 if (no_shutdown) {
1572 vm_stop(RUN_STATE_SHUTDOWN);
1573 } else
1574 break;
1576 if (qemu_reset_requested()) {
1577 pause_all_vcpus();
1578 cpu_synchronize_all_states();
1579 qemu_system_reset(VMRESET_REPORT);
1580 resume_all_vcpus();
1581 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1582 runstate_check(RUN_STATE_SHUTDOWN)) {
1583 runstate_set(RUN_STATE_PAUSED);
1586 if (qemu_powerdown_requested()) {
1587 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1588 qemu_irq_raise(qemu_system_powerdown);
1590 if (qemu_vmstop_requested(&r)) {
1591 vm_stop(r);
1594 bdrv_close_all();
1595 pause_all_vcpus();
1598 static void version(void)
1600 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1603 static void help(int exitcode)
1605 const char *options_help =
1606 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1607 opt_help
1608 #define DEFHEADING(text) stringify(text) "\n"
1609 #include "qemu-options.def"
1610 #undef DEF
1611 #undef DEFHEADING
1612 #undef GEN_DOCS
1614 version();
1615 printf("usage: %s [options] [disk_image]\n"
1616 "\n"
1617 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1618 "\n"
1619 "%s\n"
1620 "During emulation, the following keys are useful:\n"
1621 "ctrl-alt-f toggle full screen\n"
1622 "ctrl-alt-n switch to virtual console 'n'\n"
1623 "ctrl-alt toggle mouse and keyboard grab\n"
1624 "\n"
1625 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1626 "qemu",
1627 options_help);
1628 exit(exitcode);
1631 #define HAS_ARG 0x0001
1633 typedef struct QEMUOption {
1634 const char *name;
1635 int flags;
1636 int index;
1637 uint32_t arch_mask;
1638 } QEMUOption;
1640 static const QEMUOption qemu_options[] = {
1641 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1642 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1643 { option, opt_arg, opt_enum, arch_mask },
1644 #define DEFHEADING(text)
1645 #include "qemu-options.def"
1646 #undef DEF
1647 #undef DEFHEADING
1648 #undef GEN_DOCS
1649 { NULL },
1651 static void select_vgahw (const char *p)
1653 const char *opts;
1655 default_vga = 0;
1656 vga_interface_type = VGA_NONE;
1657 if (strstart(p, "std", &opts)) {
1658 vga_interface_type = VGA_STD;
1659 } else if (strstart(p, "cirrus", &opts)) {
1660 vga_interface_type = VGA_CIRRUS;
1661 } else if (strstart(p, "vmware", &opts)) {
1662 vga_interface_type = VGA_VMWARE;
1663 } else if (strstart(p, "xenfb", &opts)) {
1664 vga_interface_type = VGA_XENFB;
1665 } else if (strstart(p, "qxl", &opts)) {
1666 vga_interface_type = VGA_QXL;
1667 } else if (!strstart(p, "none", &opts)) {
1668 invalid_vga:
1669 fprintf(stderr, "Unknown vga type: %s\n", p);
1670 exit(1);
1672 while (*opts) {
1673 const char *nextopt;
1675 if (strstart(opts, ",retrace=", &nextopt)) {
1676 opts = nextopt;
1677 if (strstart(opts, "dumb", &nextopt))
1678 vga_retrace_method = VGA_RETRACE_DUMB;
1679 else if (strstart(opts, "precise", &nextopt))
1680 vga_retrace_method = VGA_RETRACE_PRECISE;
1681 else goto invalid_vga;
1682 } else goto invalid_vga;
1683 opts = nextopt;
1687 static DisplayType select_display(const char *p)
1689 const char *opts;
1690 DisplayType display = DT_DEFAULT;
1692 if (strstart(p, "sdl", &opts)) {
1693 #ifdef CONFIG_SDL
1694 display = DT_SDL;
1695 while (*opts) {
1696 const char *nextopt;
1698 if (strstart(opts, ",frame=", &nextopt)) {
1699 opts = nextopt;
1700 if (strstart(opts, "on", &nextopt)) {
1701 no_frame = 0;
1702 } else if (strstart(opts, "off", &nextopt)) {
1703 no_frame = 1;
1704 } else {
1705 goto invalid_sdl_args;
1707 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1708 opts = nextopt;
1709 if (strstart(opts, "on", &nextopt)) {
1710 alt_grab = 1;
1711 } else if (strstart(opts, "off", &nextopt)) {
1712 alt_grab = 0;
1713 } else {
1714 goto invalid_sdl_args;
1716 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1717 opts = nextopt;
1718 if (strstart(opts, "on", &nextopt)) {
1719 ctrl_grab = 1;
1720 } else if (strstart(opts, "off", &nextopt)) {
1721 ctrl_grab = 0;
1722 } else {
1723 goto invalid_sdl_args;
1725 } else if (strstart(opts, ",window_close=", &nextopt)) {
1726 opts = nextopt;
1727 if (strstart(opts, "on", &nextopt)) {
1728 no_quit = 0;
1729 } else if (strstart(opts, "off", &nextopt)) {
1730 no_quit = 1;
1731 } else {
1732 goto invalid_sdl_args;
1734 } else {
1735 invalid_sdl_args:
1736 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1737 exit(1);
1739 opts = nextopt;
1741 #else
1742 fprintf(stderr, "SDL support is disabled\n");
1743 exit(1);
1744 #endif
1745 } else if (strstart(p, "vnc", &opts)) {
1746 #ifdef CONFIG_VNC
1747 display_remote++;
1749 if (*opts) {
1750 const char *nextopt;
1752 if (strstart(opts, "=", &nextopt)) {
1753 vnc_display = nextopt;
1756 if (!vnc_display) {
1757 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1758 exit(1);
1760 #else
1761 fprintf(stderr, "VNC support is disabled\n");
1762 exit(1);
1763 #endif
1764 } else if (strstart(p, "curses", &opts)) {
1765 #ifdef CONFIG_CURSES
1766 display = DT_CURSES;
1767 #else
1768 fprintf(stderr, "Curses support is disabled\n");
1769 exit(1);
1770 #endif
1771 } else if (strstart(p, "none", &opts)) {
1772 display = DT_NONE;
1773 } else {
1774 fprintf(stderr, "Unknown display type: %s\n", p);
1775 exit(1);
1778 return display;
1781 static int balloon_parse(const char *arg)
1783 QemuOpts *opts;
1785 if (strcmp(arg, "none") == 0) {
1786 return 0;
1789 if (!strncmp(arg, "virtio", 6)) {
1790 if (arg[6] == ',') {
1791 /* have params -> parse them */
1792 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1793 if (!opts)
1794 return -1;
1795 } else {
1796 /* create empty opts */
1797 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1799 qemu_opt_set(opts, "driver", "virtio-balloon");
1800 return 0;
1803 return -1;
1806 char *qemu_find_file(int type, const char *name)
1808 int len;
1809 const char *subdir;
1810 char *buf;
1812 /* If name contains path separators then try it as a straight path. */
1813 if ((strchr(name, '/') || strchr(name, '\\'))
1814 && access(name, R_OK) == 0) {
1815 return g_strdup(name);
1817 switch (type) {
1818 case QEMU_FILE_TYPE_BIOS:
1819 subdir = "";
1820 break;
1821 case QEMU_FILE_TYPE_KEYMAP:
1822 subdir = "keymaps/";
1823 break;
1824 default:
1825 abort();
1827 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1828 buf = g_malloc0(len);
1829 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1830 if (access(buf, R_OK)) {
1831 g_free(buf);
1832 return NULL;
1834 return buf;
1837 static int device_help_func(QemuOpts *opts, void *opaque)
1839 return qdev_device_help(opts);
1842 static int device_init_func(QemuOpts *opts, void *opaque)
1844 DeviceState *dev;
1846 dev = qdev_device_add(opts);
1847 if (!dev)
1848 return -1;
1849 return 0;
1852 static int chardev_init_func(QemuOpts *opts, void *opaque)
1854 CharDriverState *chr;
1856 chr = qemu_chr_new_from_opts(opts, NULL);
1857 if (!chr)
1858 return -1;
1859 return 0;
1862 #ifdef CONFIG_VIRTFS
1863 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1865 int ret;
1866 ret = qemu_fsdev_add(opts);
1868 return ret;
1870 #endif
1872 static int mon_init_func(QemuOpts *opts, void *opaque)
1874 CharDriverState *chr;
1875 const char *chardev;
1876 const char *mode;
1877 int flags;
1879 mode = qemu_opt_get(opts, "mode");
1880 if (mode == NULL) {
1881 mode = "readline";
1883 if (strcmp(mode, "readline") == 0) {
1884 flags = MONITOR_USE_READLINE;
1885 } else if (strcmp(mode, "control") == 0) {
1886 flags = MONITOR_USE_CONTROL;
1887 } else {
1888 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1889 exit(1);
1892 if (qemu_opt_get_bool(opts, "pretty", 0))
1893 flags |= MONITOR_USE_PRETTY;
1895 if (qemu_opt_get_bool(opts, "default", 0))
1896 flags |= MONITOR_IS_DEFAULT;
1898 chardev = qemu_opt_get(opts, "chardev");
1899 chr = qemu_chr_find(chardev);
1900 if (chr == NULL) {
1901 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1902 exit(1);
1905 monitor_init(chr, flags);
1906 return 0;
1909 static void monitor_parse(const char *optarg, const char *mode)
1911 static int monitor_device_index = 0;
1912 QemuOpts *opts;
1913 const char *p;
1914 char label[32];
1915 int def = 0;
1917 if (strstart(optarg, "chardev:", &p)) {
1918 snprintf(label, sizeof(label), "%s", p);
1919 } else {
1920 snprintf(label, sizeof(label), "compat_monitor%d",
1921 monitor_device_index);
1922 if (monitor_device_index == 0) {
1923 def = 1;
1925 opts = qemu_chr_parse_compat(label, optarg);
1926 if (!opts) {
1927 fprintf(stderr, "parse error: %s\n", optarg);
1928 exit(1);
1932 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1933 if (!opts) {
1934 fprintf(stderr, "duplicate chardev: %s\n", label);
1935 exit(1);
1937 qemu_opt_set(opts, "mode", mode);
1938 qemu_opt_set(opts, "chardev", label);
1939 if (def)
1940 qemu_opt_set(opts, "default", "on");
1941 monitor_device_index++;
1944 struct device_config {
1945 enum {
1946 DEV_USB, /* -usbdevice */
1947 DEV_BT, /* -bt */
1948 DEV_SERIAL, /* -serial */
1949 DEV_PARALLEL, /* -parallel */
1950 DEV_VIRTCON, /* -virtioconsole */
1951 DEV_DEBUGCON, /* -debugcon */
1952 } type;
1953 const char *cmdline;
1954 QTAILQ_ENTRY(device_config) next;
1956 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1958 static void add_device_config(int type, const char *cmdline)
1960 struct device_config *conf;
1962 conf = g_malloc0(sizeof(*conf));
1963 conf->type = type;
1964 conf->cmdline = cmdline;
1965 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1968 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1970 struct device_config *conf;
1971 int rc;
1973 QTAILQ_FOREACH(conf, &device_configs, next) {
1974 if (conf->type != type)
1975 continue;
1976 rc = func(conf->cmdline);
1977 if (0 != rc)
1978 return rc;
1980 return 0;
1983 static int serial_parse(const char *devname)
1985 static int index = 0;
1986 char label[32];
1988 if (strcmp(devname, "none") == 0)
1989 return 0;
1990 if (index == MAX_SERIAL_PORTS) {
1991 fprintf(stderr, "qemu: too many serial ports\n");
1992 exit(1);
1994 snprintf(label, sizeof(label), "serial%d", index);
1995 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1996 if (!serial_hds[index]) {
1997 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1998 devname, strerror(errno));
1999 return -1;
2001 index++;
2002 return 0;
2005 static int parallel_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_PARALLEL_PORTS) {
2013 fprintf(stderr, "qemu: too many parallel ports\n");
2014 exit(1);
2016 snprintf(label, sizeof(label), "parallel%d", index);
2017 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2018 if (!parallel_hds[index]) {
2019 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2020 devname, strerror(errno));
2021 return -1;
2023 index++;
2024 return 0;
2027 static int virtcon_parse(const char *devname)
2029 QemuOptsList *device = qemu_find_opts("device");
2030 static int index = 0;
2031 char label[32];
2032 QemuOpts *bus_opts, *dev_opts;
2034 if (strcmp(devname, "none") == 0)
2035 return 0;
2036 if (index == MAX_VIRTIO_CONSOLES) {
2037 fprintf(stderr, "qemu: too many virtio consoles\n");
2038 exit(1);
2041 bus_opts = qemu_opts_create(device, NULL, 0);
2042 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2044 dev_opts = qemu_opts_create(device, NULL, 0);
2045 qemu_opt_set(dev_opts, "driver", "virtconsole");
2047 snprintf(label, sizeof(label), "virtcon%d", index);
2048 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2049 if (!virtcon_hds[index]) {
2050 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2051 devname, strerror(errno));
2052 return -1;
2054 qemu_opt_set(dev_opts, "chardev", label);
2056 index++;
2057 return 0;
2060 static int debugcon_parse(const char *devname)
2062 QemuOpts *opts;
2064 if (!qemu_chr_new("debugcon", devname, NULL)) {
2065 exit(1);
2067 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2068 if (!opts) {
2069 fprintf(stderr, "qemu: already have a debugcon device\n");
2070 exit(1);
2072 qemu_opt_set(opts, "driver", "isa-debugcon");
2073 qemu_opt_set(opts, "chardev", "debugcon");
2074 return 0;
2077 static QEMUMachine *machine_parse(const char *name)
2079 QEMUMachine *m, *machine = NULL;
2081 if (name) {
2082 machine = find_machine(name);
2084 if (machine) {
2085 return machine;
2087 printf("Supported machines are:\n");
2088 for (m = first_machine; m != NULL; m = m->next) {
2089 if (m->alias) {
2090 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2092 printf("%-10s %s%s\n", m->name, m->desc,
2093 m->is_default ? " (default)" : "");
2095 exit(!name || *name != '?');
2098 static int tcg_init(void)
2100 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2101 return 0;
2104 static struct {
2105 const char *opt_name;
2106 const char *name;
2107 int (*available)(void);
2108 int (*init)(void);
2109 int *allowed;
2110 } accel_list[] = {
2111 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2112 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2113 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2116 static int configure_accelerator(void)
2118 const char *p = NULL;
2119 char buf[10];
2120 int i, ret;
2121 bool accel_initalised = 0;
2122 bool init_failed = 0;
2124 QemuOptsList *list = qemu_find_opts("machine");
2125 if (!QTAILQ_EMPTY(&list->head)) {
2126 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2129 if (p == NULL) {
2130 /* Use the default "accelerator", tcg */
2131 p = "tcg";
2134 while (!accel_initalised && *p != '\0') {
2135 if (*p == ':') {
2136 p++;
2138 p = get_opt_name(buf, sizeof (buf), p, ':');
2139 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2140 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2141 *(accel_list[i].allowed) = 1;
2142 ret = accel_list[i].init();
2143 if (ret < 0) {
2144 init_failed = 1;
2145 if (!accel_list[i].available()) {
2146 printf("%s not supported for this target\n",
2147 accel_list[i].name);
2148 } else {
2149 fprintf(stderr, "failed to initialize %s: %s\n",
2150 accel_list[i].name,
2151 strerror(-ret));
2153 *(accel_list[i].allowed) = 0;
2154 } else {
2155 accel_initalised = 1;
2157 break;
2160 if (i == ARRAY_SIZE(accel_list)) {
2161 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2165 if (!accel_initalised) {
2166 fprintf(stderr, "No accelerator found!\n");
2167 exit(1);
2170 if (init_failed) {
2171 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2174 return !accel_initalised;
2177 void qemu_add_exit_notifier(Notifier *notify)
2179 notifier_list_add(&exit_notifiers, notify);
2182 void qemu_remove_exit_notifier(Notifier *notify)
2184 notifier_list_remove(&exit_notifiers, notify);
2187 static void qemu_run_exit_notifiers(void)
2189 notifier_list_notify(&exit_notifiers, NULL);
2192 void qemu_add_machine_init_done_notifier(Notifier *notify)
2194 notifier_list_add(&machine_init_done_notifiers, notify);
2197 static void qemu_run_machine_init_done_notifiers(void)
2199 notifier_list_notify(&machine_init_done_notifiers, NULL);
2202 static const QEMUOption *lookup_opt(int argc, char **argv,
2203 const char **poptarg, int *poptind)
2205 const QEMUOption *popt;
2206 int optind = *poptind;
2207 char *r = argv[optind];
2208 const char *optarg;
2210 loc_set_cmdline(argv, optind, 1);
2211 optind++;
2212 /* Treat --foo the same as -foo. */
2213 if (r[1] == '-')
2214 r++;
2215 popt = qemu_options;
2216 for(;;) {
2217 if (!popt->name) {
2218 error_report("invalid option");
2219 exit(1);
2221 if (!strcmp(popt->name, r + 1))
2222 break;
2223 popt++;
2225 if (popt->flags & HAS_ARG) {
2226 if (optind >= argc) {
2227 error_report("requires an argument");
2228 exit(1);
2230 optarg = argv[optind++];
2231 loc_set_cmdline(argv, optind - 2, 2);
2232 } else {
2233 optarg = NULL;
2236 *poptarg = optarg;
2237 *poptind = optind;
2239 return popt;
2242 static gpointer malloc_and_trace(gsize n_bytes)
2244 void *ptr = malloc(n_bytes);
2245 trace_g_malloc(n_bytes, ptr);
2246 return ptr;
2249 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2251 void *ptr = realloc(mem, n_bytes);
2252 trace_g_realloc(mem, n_bytes, ptr);
2253 return ptr;
2256 static void free_and_trace(gpointer mem)
2258 trace_g_free(mem);
2259 free(mem);
2262 #ifdef CONFIG_KVM_OPTIONS
2263 int kvm_irqchip = 1;
2264 int kvm_pit = 1;
2265 int kvm_pit_reinject = 1;
2266 #endif
2268 int main(int argc, char **argv, char **envp)
2270 const char *gdbstub_dev = NULL;
2271 int i;
2272 int snapshot, linux_boot;
2273 const char *icount_option = NULL;
2274 const char *initrd_filename;
2275 const char *kernel_filename, *kernel_cmdline;
2276 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2277 DisplayState *ds;
2278 DisplayChangeListener *dcl;
2279 int cyls, heads, secs, translation;
2280 QemuOpts *hda_opts = NULL, *opts;
2281 QemuOptsList *olist;
2282 int optind;
2283 const char *optarg;
2284 const char *loadvm = NULL;
2285 QEMUMachine *machine;
2286 const char *cpu_model;
2287 const char *pid_file = NULL;
2288 const char *incoming = NULL;
2289 #ifdef CONFIG_VNC
2290 int show_vnc_port = 0;
2291 #endif
2292 int defconfig = 1;
2293 const char *log_mask = NULL;
2294 const char *log_file = NULL;
2295 GMemVTable mem_trace = {
2296 .malloc = malloc_and_trace,
2297 .realloc = realloc_and_trace,
2298 .free = free_and_trace,
2300 const char *trace_events = NULL;
2301 const char *trace_file = NULL;
2303 atexit(qemu_run_exit_notifiers);
2304 error_set_progname(argv[0]);
2306 g_mem_set_vtable(&mem_trace);
2307 g_thread_init(NULL);
2309 runstate_init();
2311 init_clocks();
2313 qemu_cache_utils_init(envp);
2315 QLIST_INIT (&vm_change_state_head);
2316 os_setup_early_signal_handling();
2318 module_call_init(MODULE_INIT_MACHINE);
2319 machine = find_default_machine();
2320 cpu_model = NULL;
2321 initrd_filename = NULL;
2322 ram_size = 0;
2323 snapshot = 0;
2324 kernel_filename = NULL;
2325 kernel_cmdline = "";
2326 cyls = heads = secs = 0;
2327 translation = BIOS_ATA_TRANSLATION_AUTO;
2329 for (i = 0; i < MAX_NODES; i++) {
2330 node_mem[i] = 0;
2331 node_cpumask[i] = 0;
2334 nb_numa_nodes = 0;
2335 nb_nics = 0;
2337 autostart= 1;
2339 /* first pass of option parsing */
2340 optind = 1;
2341 while (optind < argc) {
2342 if (argv[optind][0] != '-') {
2343 /* disk image */
2344 optind++;
2345 continue;
2346 } else {
2347 const QEMUOption *popt;
2349 popt = lookup_opt(argc, argv, &optarg, &optind);
2350 switch (popt->index) {
2351 case QEMU_OPTION_nodefconfig:
2352 defconfig=0;
2353 break;
2358 if (defconfig) {
2359 int ret;
2361 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2362 if (ret < 0 && ret != -ENOENT) {
2363 exit(1);
2366 ret = qemu_read_config_file(arch_config_name);
2367 if (ret < 0 && ret != -ENOENT) {
2368 exit(1);
2371 cpudef_init();
2373 /* second pass of option parsing */
2374 optind = 1;
2375 for(;;) {
2376 if (optind >= argc)
2377 break;
2378 if (argv[optind][0] != '-') {
2379 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2380 } else {
2381 const QEMUOption *popt;
2383 popt = lookup_opt(argc, argv, &optarg, &optind);
2384 if (!(popt->arch_mask & arch_type)) {
2385 printf("Option %s not supported for this target\n", popt->name);
2386 exit(1);
2388 switch(popt->index) {
2389 case QEMU_OPTION_M:
2390 machine = machine_parse(optarg);
2391 break;
2392 case QEMU_OPTION_cpu:
2393 /* hw initialization will check this */
2394 if (*optarg == '?') {
2395 list_cpus(stdout, &fprintf, optarg);
2396 exit(0);
2397 } else {
2398 cpu_model = optarg;
2400 break;
2401 case QEMU_OPTION_initrd:
2402 initrd_filename = optarg;
2403 break;
2404 case QEMU_OPTION_hda:
2406 char buf[256];
2407 if (cyls == 0)
2408 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2409 else
2410 snprintf(buf, sizeof(buf),
2411 "%s,cyls=%d,heads=%d,secs=%d%s",
2412 HD_OPTS , cyls, heads, secs,
2413 translation == BIOS_ATA_TRANSLATION_LBA ?
2414 ",trans=lba" :
2415 translation == BIOS_ATA_TRANSLATION_NONE ?
2416 ",trans=none" : "");
2417 drive_add(IF_DEFAULT, 0, optarg, buf);
2418 break;
2420 case QEMU_OPTION_hdb:
2421 case QEMU_OPTION_hdc:
2422 case QEMU_OPTION_hdd:
2423 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2424 HD_OPTS);
2425 break;
2426 case QEMU_OPTION_drive:
2427 if (drive_def(optarg) == NULL) {
2428 exit(1);
2430 break;
2431 case QEMU_OPTION_set:
2432 if (qemu_set_option(optarg) != 0)
2433 exit(1);
2434 break;
2435 case QEMU_OPTION_global:
2436 if (qemu_global_option(optarg) != 0)
2437 exit(1);
2438 break;
2439 case QEMU_OPTION_mtdblock:
2440 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2441 break;
2442 case QEMU_OPTION_sd:
2443 drive_add(IF_SD, 0, optarg, SD_OPTS);
2444 break;
2445 case QEMU_OPTION_pflash:
2446 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2447 break;
2448 case QEMU_OPTION_snapshot:
2449 snapshot = 1;
2450 break;
2451 case QEMU_OPTION_hdachs:
2453 const char *p;
2454 p = optarg;
2455 cyls = strtol(p, (char **)&p, 0);
2456 if (cyls < 1 || cyls > 16383)
2457 goto chs_fail;
2458 if (*p != ',')
2459 goto chs_fail;
2460 p++;
2461 heads = strtol(p, (char **)&p, 0);
2462 if (heads < 1 || heads > 16)
2463 goto chs_fail;
2464 if (*p != ',')
2465 goto chs_fail;
2466 p++;
2467 secs = strtol(p, (char **)&p, 0);
2468 if (secs < 1 || secs > 63)
2469 goto chs_fail;
2470 if (*p == ',') {
2471 p++;
2472 if (!strcmp(p, "none"))
2473 translation = BIOS_ATA_TRANSLATION_NONE;
2474 else if (!strcmp(p, "lba"))
2475 translation = BIOS_ATA_TRANSLATION_LBA;
2476 else if (!strcmp(p, "auto"))
2477 translation = BIOS_ATA_TRANSLATION_AUTO;
2478 else
2479 goto chs_fail;
2480 } else if (*p != '\0') {
2481 chs_fail:
2482 fprintf(stderr, "qemu: invalid physical CHS format\n");
2483 exit(1);
2485 if (hda_opts != NULL) {
2486 char num[16];
2487 snprintf(num, sizeof(num), "%d", cyls);
2488 qemu_opt_set(hda_opts, "cyls", num);
2489 snprintf(num, sizeof(num), "%d", heads);
2490 qemu_opt_set(hda_opts, "heads", num);
2491 snprintf(num, sizeof(num), "%d", secs);
2492 qemu_opt_set(hda_opts, "secs", num);
2493 if (translation == BIOS_ATA_TRANSLATION_LBA)
2494 qemu_opt_set(hda_opts, "trans", "lba");
2495 if (translation == BIOS_ATA_TRANSLATION_NONE)
2496 qemu_opt_set(hda_opts, "trans", "none");
2499 break;
2500 case QEMU_OPTION_numa:
2501 if (nb_numa_nodes >= MAX_NODES) {
2502 fprintf(stderr, "qemu: too many NUMA nodes\n");
2503 exit(1);
2505 numa_add(optarg);
2506 break;
2507 case QEMU_OPTION_display:
2508 display_type = select_display(optarg);
2509 break;
2510 case QEMU_OPTION_nographic:
2511 display_type = DT_NOGRAPHIC;
2512 break;
2513 case QEMU_OPTION_curses:
2514 #ifdef CONFIG_CURSES
2515 display_type = DT_CURSES;
2516 #else
2517 fprintf(stderr, "Curses support is disabled\n");
2518 exit(1);
2519 #endif
2520 break;
2521 case QEMU_OPTION_portrait:
2522 graphic_rotate = 90;
2523 break;
2524 case QEMU_OPTION_rotate:
2525 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2526 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2527 graphic_rotate != 180 && graphic_rotate != 270) {
2528 fprintf(stderr,
2529 "qemu: only 90, 180, 270 deg rotation is available\n");
2530 exit(1);
2532 break;
2533 case QEMU_OPTION_kernel:
2534 kernel_filename = optarg;
2535 break;
2536 case QEMU_OPTION_append:
2537 kernel_cmdline = optarg;
2538 break;
2539 case QEMU_OPTION_cdrom:
2540 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2541 break;
2542 case QEMU_OPTION_boot:
2544 static const char * const params[] = {
2545 "order", "once", "menu",
2546 "splash", "splash-time", NULL
2548 char buf[sizeof(boot_devices)];
2549 char *standard_boot_devices;
2550 int legacy = 0;
2552 if (!strchr(optarg, '=')) {
2553 legacy = 1;
2554 pstrcpy(buf, sizeof(buf), optarg);
2555 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2556 fprintf(stderr,
2557 "qemu: unknown boot parameter '%s' in '%s'\n",
2558 buf, optarg);
2559 exit(1);
2562 if (legacy ||
2563 get_param_value(buf, sizeof(buf), "order", optarg)) {
2564 validate_bootdevices(buf);
2565 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2567 if (!legacy) {
2568 if (get_param_value(buf, sizeof(buf),
2569 "once", optarg)) {
2570 validate_bootdevices(buf);
2571 standard_boot_devices = g_strdup(boot_devices);
2572 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2573 qemu_register_reset(restore_boot_devices,
2574 standard_boot_devices);
2576 if (get_param_value(buf, sizeof(buf),
2577 "menu", optarg)) {
2578 if (!strcmp(buf, "on")) {
2579 boot_menu = 1;
2580 } else if (!strcmp(buf, "off")) {
2581 boot_menu = 0;
2582 } else {
2583 fprintf(stderr,
2584 "qemu: invalid option value '%s'\n",
2585 buf);
2586 exit(1);
2589 qemu_opts_parse(qemu_find_opts("boot-opts"),
2590 optarg, 0);
2593 break;
2594 case QEMU_OPTION_fda:
2595 case QEMU_OPTION_fdb:
2596 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2597 optarg, FD_OPTS);
2598 break;
2599 case QEMU_OPTION_no_fd_bootchk:
2600 fd_bootchk = 0;
2601 break;
2602 case QEMU_OPTION_netdev:
2603 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2604 exit(1);
2606 break;
2607 case QEMU_OPTION_net:
2608 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2609 exit(1);
2611 break;
2612 #ifdef CONFIG_SLIRP
2613 case QEMU_OPTION_tftp:
2614 legacy_tftp_prefix = optarg;
2615 break;
2616 case QEMU_OPTION_bootp:
2617 legacy_bootp_filename = optarg;
2618 break;
2619 case QEMU_OPTION_redir:
2620 if (net_slirp_redir(optarg) < 0)
2621 exit(1);
2622 break;
2623 #endif
2624 case QEMU_OPTION_bt:
2625 add_device_config(DEV_BT, optarg);
2626 break;
2627 case QEMU_OPTION_audio_help:
2628 if (!(audio_available())) {
2629 printf("Option %s not supported for this target\n", popt->name);
2630 exit(1);
2632 AUD_help ();
2633 exit (0);
2634 break;
2635 case QEMU_OPTION_soundhw:
2636 if (!(audio_available())) {
2637 printf("Option %s not supported for this target\n", popt->name);
2638 exit(1);
2640 select_soundhw (optarg);
2641 break;
2642 case QEMU_OPTION_h:
2643 help(0);
2644 break;
2645 case QEMU_OPTION_version:
2646 version();
2647 exit(0);
2648 break;
2649 case QEMU_OPTION_m: {
2650 int64_t value;
2652 value = strtosz(optarg, NULL);
2653 if (value < 0) {
2654 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2655 exit(1);
2658 if (value != (uint64_t)(ram_addr_t)value) {
2659 fprintf(stderr, "qemu: ram size too large\n");
2660 exit(1);
2662 ram_size = value;
2663 break;
2665 case QEMU_OPTION_mempath:
2666 mem_path = optarg;
2667 break;
2668 #ifdef MAP_POPULATE
2669 case QEMU_OPTION_mem_prealloc:
2670 mem_prealloc = 1;
2671 break;
2672 #endif
2673 case QEMU_OPTION_d:
2674 log_mask = optarg;
2675 break;
2676 case QEMU_OPTION_D:
2677 log_file = optarg;
2678 break;
2679 case QEMU_OPTION_s:
2680 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2681 break;
2682 case QEMU_OPTION_gdb:
2683 gdbstub_dev = optarg;
2684 break;
2685 case QEMU_OPTION_L:
2686 data_dir = optarg;
2687 break;
2688 case QEMU_OPTION_bios:
2689 bios_name = optarg;
2690 break;
2691 case QEMU_OPTION_singlestep:
2692 singlestep = 1;
2693 break;
2694 case QEMU_OPTION_S:
2695 autostart = 0;
2696 break;
2697 case QEMU_OPTION_k:
2698 keyboard_layout = optarg;
2699 break;
2700 case QEMU_OPTION_localtime:
2701 rtc_utc = 0;
2702 break;
2703 case QEMU_OPTION_vga:
2704 select_vgahw (optarg);
2705 break;
2706 case QEMU_OPTION_g:
2708 const char *p;
2709 int w, h, depth;
2710 p = optarg;
2711 w = strtol(p, (char **)&p, 10);
2712 if (w <= 0) {
2713 graphic_error:
2714 fprintf(stderr, "qemu: invalid resolution or depth\n");
2715 exit(1);
2717 if (*p != 'x')
2718 goto graphic_error;
2719 p++;
2720 h = strtol(p, (char **)&p, 10);
2721 if (h <= 0)
2722 goto graphic_error;
2723 if (*p == 'x') {
2724 p++;
2725 depth = strtol(p, (char **)&p, 10);
2726 if (depth != 8 && depth != 15 && depth != 16 &&
2727 depth != 24 && depth != 32)
2728 goto graphic_error;
2729 } else if (*p == '\0') {
2730 depth = graphic_depth;
2731 } else {
2732 goto graphic_error;
2735 graphic_width = w;
2736 graphic_height = h;
2737 graphic_depth = depth;
2739 break;
2740 case QEMU_OPTION_echr:
2742 char *r;
2743 term_escape_char = strtol(optarg, &r, 0);
2744 if (r == optarg)
2745 printf("Bad argument to echr\n");
2746 break;
2748 case QEMU_OPTION_monitor:
2749 monitor_parse(optarg, "readline");
2750 default_monitor = 0;
2751 break;
2752 case QEMU_OPTION_qmp:
2753 monitor_parse(optarg, "control");
2754 default_monitor = 0;
2755 break;
2756 case QEMU_OPTION_mon:
2757 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2758 if (!opts) {
2759 exit(1);
2761 default_monitor = 0;
2762 break;
2763 case QEMU_OPTION_chardev:
2764 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2765 if (!opts) {
2766 exit(1);
2768 break;
2769 case QEMU_OPTION_fsdev:
2770 olist = qemu_find_opts("fsdev");
2771 if (!olist) {
2772 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2773 exit(1);
2775 opts = qemu_opts_parse(olist, optarg, 1);
2776 if (!opts) {
2777 fprintf(stderr, "parse error: %s\n", optarg);
2778 exit(1);
2780 break;
2781 case QEMU_OPTION_virtfs: {
2782 QemuOpts *fsdev;
2783 QemuOpts *device;
2785 olist = qemu_find_opts("virtfs");
2786 if (!olist) {
2787 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2788 exit(1);
2790 opts = qemu_opts_parse(olist, optarg, 1);
2791 if (!opts) {
2792 fprintf(stderr, "parse error: %s\n", optarg);
2793 exit(1);
2796 if (qemu_opt_get(opts, "fstype") == NULL ||
2797 qemu_opt_get(opts, "mount_tag") == NULL ||
2798 qemu_opt_get(opts, "path") == NULL ||
2799 qemu_opt_get(opts, "security_model") == NULL) {
2800 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2801 "security_model=[mapped|passthrough|none],"
2802 "mount_tag=tag.\n");
2803 exit(1);
2806 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2807 qemu_opt_get(opts, "mount_tag"), 1);
2808 if (!fsdev) {
2809 fprintf(stderr, "duplicate fsdev id: %s\n",
2810 qemu_opt_get(opts, "mount_tag"));
2811 exit(1);
2813 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2814 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2815 qemu_opt_set(fsdev, "security_model",
2816 qemu_opt_get(opts, "security_model"));
2818 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2819 qemu_opt_set(device, "driver", "virtio-9p-pci");
2820 qemu_opt_set(device, "fsdev",
2821 qemu_opt_get(opts, "mount_tag"));
2822 qemu_opt_set(device, "mount_tag",
2823 qemu_opt_get(opts, "mount_tag"));
2824 break;
2826 case QEMU_OPTION_serial:
2827 add_device_config(DEV_SERIAL, optarg);
2828 default_serial = 0;
2829 if (strncmp(optarg, "mon:", 4) == 0) {
2830 default_monitor = 0;
2832 break;
2833 case QEMU_OPTION_watchdog:
2834 if (watchdog) {
2835 fprintf(stderr,
2836 "qemu: only one watchdog option may be given\n");
2837 return 1;
2839 watchdog = optarg;
2840 break;
2841 case QEMU_OPTION_watchdog_action:
2842 if (select_watchdog_action(optarg) == -1) {
2843 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2844 exit(1);
2846 break;
2847 case QEMU_OPTION_virtiocon:
2848 add_device_config(DEV_VIRTCON, optarg);
2849 default_virtcon = 0;
2850 if (strncmp(optarg, "mon:", 4) == 0) {
2851 default_monitor = 0;
2853 break;
2854 case QEMU_OPTION_parallel:
2855 add_device_config(DEV_PARALLEL, optarg);
2856 default_parallel = 0;
2857 if (strncmp(optarg, "mon:", 4) == 0) {
2858 default_monitor = 0;
2860 break;
2861 case QEMU_OPTION_debugcon:
2862 add_device_config(DEV_DEBUGCON, optarg);
2863 break;
2864 case QEMU_OPTION_loadvm:
2865 loadvm = optarg;
2866 break;
2867 case QEMU_OPTION_full_screen:
2868 full_screen = 1;
2869 break;
2870 #ifdef CONFIG_SDL
2871 case QEMU_OPTION_no_frame:
2872 no_frame = 1;
2873 break;
2874 case QEMU_OPTION_alt_grab:
2875 alt_grab = 1;
2876 break;
2877 case QEMU_OPTION_ctrl_grab:
2878 ctrl_grab = 1;
2879 break;
2880 case QEMU_OPTION_no_quit:
2881 no_quit = 1;
2882 break;
2883 case QEMU_OPTION_sdl:
2884 display_type = DT_SDL;
2885 break;
2886 #else
2887 case QEMU_OPTION_no_frame:
2888 case QEMU_OPTION_alt_grab:
2889 case QEMU_OPTION_ctrl_grab:
2890 case QEMU_OPTION_no_quit:
2891 case QEMU_OPTION_sdl:
2892 fprintf(stderr, "SDL support is disabled\n");
2893 exit(1);
2894 #endif
2895 case QEMU_OPTION_pidfile:
2896 pid_file = optarg;
2897 break;
2898 case QEMU_OPTION_win2k_hack:
2899 win2k_install_hack = 1;
2900 break;
2901 case QEMU_OPTION_rtc_td_hack:
2902 rtc_td_hack = 1;
2903 break;
2904 case QEMU_OPTION_acpitable:
2905 do_acpitable_option(optarg);
2906 break;
2907 case QEMU_OPTION_smbios:
2908 do_smbios_option(optarg);
2909 break;
2910 case QEMU_OPTION_enable_kvm:
2911 olist = qemu_find_opts("machine");
2912 qemu_opts_reset(olist);
2913 qemu_opts_parse(olist, "accel=kvm", 0);
2914 break;
2915 case QEMU_OPTION_machine:
2916 olist = qemu_find_opts("machine");
2917 qemu_opts_reset(olist);
2918 opts = qemu_opts_parse(olist, optarg, 1);
2919 if (!opts) {
2920 fprintf(stderr, "parse error: %s\n", optarg);
2921 exit(1);
2923 optarg = qemu_opt_get(opts, "type");
2924 if (optarg) {
2925 machine = machine_parse(optarg);
2927 break;
2928 case QEMU_OPTION_no_kvm:
2929 olist = qemu_find_opts("machine");
2930 qemu_opts_reset(olist);
2931 qemu_opts_parse(olist, "accel=tcg", 0);
2932 break;
2933 #ifdef CONFIG_KVM_OPTIONS
2934 case QEMU_OPTION_no_kvm_irqchip: {
2935 kvm_irqchip = 0;
2936 kvm_pit = 0;
2937 break;
2939 case QEMU_OPTION_no_kvm_pit: {
2940 kvm_pit = 0;
2941 break;
2943 case QEMU_OPTION_no_kvm_pit_reinjection: {
2944 kvm_pit_reinject = 0;
2945 break;
2947 #endif
2948 case QEMU_OPTION_usb:
2949 usb_enabled = 1;
2950 break;
2951 case QEMU_OPTION_usbdevice:
2952 usb_enabled = 1;
2953 add_device_config(DEV_USB, optarg);
2954 break;
2955 case QEMU_OPTION_device:
2956 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2957 exit(1);
2959 break;
2960 case QEMU_OPTION_smp:
2961 smp_parse(optarg);
2962 if (smp_cpus < 1) {
2963 fprintf(stderr, "Invalid number of CPUs\n");
2964 exit(1);
2966 if (max_cpus < smp_cpus) {
2967 fprintf(stderr, "maxcpus must be equal to or greater than "
2968 "smp\n");
2969 exit(1);
2971 if (max_cpus > 255) {
2972 fprintf(stderr, "Unsupported number of maxcpus\n");
2973 exit(1);
2975 break;
2976 case QEMU_OPTION_vnc:
2977 #ifdef CONFIG_VNC
2978 display_remote++;
2979 vnc_display = optarg;
2980 #else
2981 fprintf(stderr, "VNC support is disabled\n");
2982 exit(1);
2983 #endif
2984 break;
2985 case QEMU_OPTION_no_acpi:
2986 acpi_enabled = 0;
2987 break;
2988 case QEMU_OPTION_no_hpet:
2989 no_hpet = 1;
2990 break;
2991 case QEMU_OPTION_balloon:
2992 if (balloon_parse(optarg) < 0) {
2993 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2994 exit(1);
2996 break;
2997 case QEMU_OPTION_no_reboot:
2998 no_reboot = 1;
2999 break;
3000 case QEMU_OPTION_no_shutdown:
3001 no_shutdown = 1;
3002 break;
3003 case QEMU_OPTION_show_cursor:
3004 cursor_hide = 0;
3005 break;
3006 case QEMU_OPTION_uuid:
3007 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3008 fprintf(stderr, "Fail to parse UUID string."
3009 " Wrong format.\n");
3010 exit(1);
3012 break;
3013 case QEMU_OPTION_option_rom:
3014 if (nb_option_roms >= MAX_OPTION_ROMS) {
3015 fprintf(stderr, "Too many option ROMs\n");
3016 exit(1);
3018 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3019 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3020 option_rom[nb_option_roms].bootindex =
3021 qemu_opt_get_number(opts, "bootindex", -1);
3022 if (!option_rom[nb_option_roms].name) {
3023 fprintf(stderr, "Option ROM file is not specified\n");
3024 exit(1);
3026 nb_option_roms++;
3027 break;
3028 case QEMU_OPTION_semihosting:
3029 semihosting_enabled = 1;
3030 break;
3031 case QEMU_OPTION_tdf:
3032 time_drift_fix = 1;
3033 break;
3034 case QEMU_OPTION_kvm_shadow_memory:
3035 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
3036 break;
3037 case QEMU_OPTION_name:
3038 qemu_name = g_strdup(optarg);
3040 char *p = strchr(qemu_name, ',');
3041 if (p != NULL) {
3042 *p++ = 0;
3043 if (strncmp(p, "process=", 8)) {
3044 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3045 exit(1);
3047 p += 8;
3048 os_set_proc_name(p);
3051 break;
3052 case QEMU_OPTION_prom_env:
3053 if (nb_prom_envs >= MAX_PROM_ENVS) {
3054 fprintf(stderr, "Too many prom variables\n");
3055 exit(1);
3057 prom_envs[nb_prom_envs] = optarg;
3058 nb_prom_envs++;
3059 break;
3060 case QEMU_OPTION_old_param:
3061 old_param = 1;
3062 break;
3063 case QEMU_OPTION_clock:
3064 configure_alarms(optarg);
3065 break;
3066 case QEMU_OPTION_startdate:
3067 configure_rtc_date_offset(optarg, 1);
3068 break;
3069 case QEMU_OPTION_rtc:
3070 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3071 if (!opts) {
3072 exit(1);
3074 configure_rtc(opts);
3075 break;
3076 case QEMU_OPTION_tb_size:
3077 tcg_tb_size = strtol(optarg, NULL, 0);
3078 if (tcg_tb_size < 0) {
3079 tcg_tb_size = 0;
3081 break;
3082 case QEMU_OPTION_icount:
3083 icount_option = optarg;
3084 break;
3085 case QEMU_OPTION_incoming:
3086 incoming = optarg;
3087 break;
3088 case QEMU_OPTION_nodefaults:
3089 default_serial = 0;
3090 default_parallel = 0;
3091 default_virtcon = 0;
3092 default_monitor = 0;
3093 default_vga = 0;
3094 default_net = 0;
3095 default_floppy = 0;
3096 default_cdrom = 0;
3097 default_sdcard = 0;
3098 break;
3099 case QEMU_OPTION_xen_domid:
3100 if (!(xen_available())) {
3101 printf("Option %s not supported for this target\n", popt->name);
3102 exit(1);
3104 xen_domid = atoi(optarg);
3105 break;
3106 case QEMU_OPTION_xen_create:
3107 if (!(xen_available())) {
3108 printf("Option %s not supported for this target\n", popt->name);
3109 exit(1);
3111 xen_mode = XEN_CREATE;
3112 break;
3113 case QEMU_OPTION_xen_attach:
3114 if (!(xen_available())) {
3115 printf("Option %s not supported for this target\n", popt->name);
3116 exit(1);
3118 xen_mode = XEN_ATTACH;
3119 break;
3120 case QEMU_OPTION_trace:
3122 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3123 if (!opts) {
3124 exit(1);
3126 trace_events = qemu_opt_get(opts, "events");
3127 trace_file = qemu_opt_get(opts, "file");
3128 break;
3130 case QEMU_OPTION_readconfig:
3132 int ret = qemu_read_config_file(optarg);
3133 if (ret < 0) {
3134 fprintf(stderr, "read config %s: %s\n", optarg,
3135 strerror(-ret));
3136 exit(1);
3138 break;
3140 case QEMU_OPTION_spice:
3141 olist = qemu_find_opts("spice");
3142 if (!olist) {
3143 fprintf(stderr, "spice is not supported by this qemu build.\n");
3144 exit(1);
3146 opts = qemu_opts_parse(olist, optarg, 0);
3147 if (!opts) {
3148 fprintf(stderr, "parse error: %s\n", optarg);
3149 exit(1);
3151 break;
3152 case QEMU_OPTION_writeconfig:
3154 FILE *fp;
3155 if (strcmp(optarg, "-") == 0) {
3156 fp = stdout;
3157 } else {
3158 fp = fopen(optarg, "w");
3159 if (fp == NULL) {
3160 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3161 exit(1);
3164 qemu_config_write(fp);
3165 fclose(fp);
3166 break;
3168 default:
3169 os_parse_cmd_args(popt->index, optarg);
3173 loc_set_none();
3175 /* Open the logfile at this point, if necessary. We can't open the logfile
3176 * when encountering either of the logging options (-d or -D) because the
3177 * other one may be encountered later on the command line, changing the
3178 * location or level of logging.
3180 if (log_mask) {
3181 if (log_file) {
3182 set_cpu_log_filename(log_file);
3184 set_cpu_log(log_mask);
3187 if (!trace_backend_init(trace_events, trace_file)) {
3188 exit(1);
3191 /* If no data_dir is specified then try to find it relative to the
3192 executable path. */
3193 if (!data_dir) {
3194 data_dir = os_find_datadir(argv[0]);
3196 /* If all else fails use the install path specified when building. */
3197 if (!data_dir) {
3198 data_dir = CONFIG_QEMU_DATADIR;
3202 * Default to max_cpus = smp_cpus, in case the user doesn't
3203 * specify a max_cpus value.
3205 if (!max_cpus)
3206 max_cpus = smp_cpus;
3208 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3209 if (smp_cpus > machine->max_cpus) {
3210 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3211 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3212 machine->max_cpus);
3213 exit(1);
3217 * Get the default machine options from the machine if it is not already
3218 * specified either by the configuration file or by the command line.
3220 if (machine->default_machine_opts) {
3221 QemuOptsList *list = qemu_find_opts("machine");
3222 const char *p = NULL;
3224 if (!QTAILQ_EMPTY(&list->head)) {
3225 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3227 if (p == NULL) {
3228 qemu_opts_reset(list);
3229 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3230 if (!opts) {
3231 fprintf(stderr, "parse error for machine %s: %s\n",
3232 machine->name, machine->default_machine_opts);
3233 exit(1);
3238 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3239 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3241 if (machine->no_serial) {
3242 default_serial = 0;
3244 if (machine->no_parallel) {
3245 default_parallel = 0;
3247 if (!machine->use_virtcon) {
3248 default_virtcon = 0;
3250 if (machine->no_vga) {
3251 default_vga = 0;
3253 if (machine->no_floppy) {
3254 default_floppy = 0;
3256 if (machine->no_cdrom) {
3257 default_cdrom = 0;
3259 if (machine->no_sdcard) {
3260 default_sdcard = 0;
3263 if (display_type == DT_NOGRAPHIC) {
3264 if (default_parallel)
3265 add_device_config(DEV_PARALLEL, "null");
3266 if (default_serial && default_monitor) {
3267 add_device_config(DEV_SERIAL, "mon:stdio");
3268 } else if (default_virtcon && default_monitor) {
3269 add_device_config(DEV_VIRTCON, "mon:stdio");
3270 } else {
3271 if (default_serial)
3272 add_device_config(DEV_SERIAL, "stdio");
3273 if (default_virtcon)
3274 add_device_config(DEV_VIRTCON, "stdio");
3275 if (default_monitor)
3276 monitor_parse("stdio", "readline");
3278 } else {
3279 if (default_serial)
3280 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3281 if (default_parallel)
3282 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3283 if (default_monitor)
3284 monitor_parse("vc:80Cx24C", "readline");
3285 if (default_virtcon)
3286 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3288 if (default_vga)
3289 vga_interface_type = VGA_CIRRUS;
3291 socket_init();
3293 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3294 exit(1);
3295 #ifdef CONFIG_VIRTFS
3296 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3297 exit(1);
3299 #endif
3301 os_daemonize();
3303 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3304 os_pidfile_error();
3305 exit(1);
3308 /* init the memory */
3309 if (ram_size == 0) {
3310 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3313 configure_accelerator();
3315 if (qemu_init_main_loop()) {
3316 fprintf(stderr, "qemu_init_main_loop failed\n");
3317 exit(1);
3319 linux_boot = (kernel_filename != NULL);
3321 if (!linux_boot && *kernel_cmdline != '\0') {
3322 fprintf(stderr, "-append only allowed with -kernel option\n");
3323 exit(1);
3326 if (!linux_boot && initrd_filename != NULL) {
3327 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3328 exit(1);
3331 os_set_line_buffering();
3333 if (init_timer_alarm() < 0) {
3334 fprintf(stderr, "could not initialize alarm timer\n");
3335 exit(1);
3337 configure_icount(icount_option);
3339 if (net_init_clients() < 0) {
3340 exit(1);
3343 /* init the bluetooth world */
3344 if (foreach_device_config(DEV_BT, bt_parse))
3345 exit(1);
3347 if (!xen_enabled()) {
3348 /* On 32-bit hosts, QEMU is limited by virtual address space */
3349 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3350 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3351 exit(1);
3355 cpu_exec_init_all();
3357 bdrv_init_with_whitelist();
3359 blk_mig_init();
3361 /* open the virtual block devices */
3362 if (snapshot)
3363 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3364 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3365 exit(1);
3367 default_drive(default_cdrom, snapshot, machine->use_scsi,
3368 IF_DEFAULT, 2, CDROM_OPTS);
3369 default_drive(default_floppy, snapshot, machine->use_scsi,
3370 IF_FLOPPY, 0, FD_OPTS);
3371 default_drive(default_sdcard, snapshot, machine->use_scsi,
3372 IF_SD, 0, SD_OPTS);
3374 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3375 ram_load, NULL);
3377 if (nb_numa_nodes > 0) {
3378 int i;
3380 if (nb_numa_nodes > MAX_NODES) {
3381 nb_numa_nodes = MAX_NODES;
3384 /* If no memory size if given for any node, assume the default case
3385 * and distribute the available memory equally across all nodes
3387 for (i = 0; i < nb_numa_nodes; i++) {
3388 if (node_mem[i] != 0)
3389 break;
3391 if (i == nb_numa_nodes) {
3392 uint64_t usedmem = 0;
3394 /* On Linux, the each node's border has to be 8MB aligned,
3395 * the final node gets the rest.
3397 for (i = 0; i < nb_numa_nodes - 1; i++) {
3398 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3399 usedmem += node_mem[i];
3401 node_mem[i] = ram_size - usedmem;
3404 for (i = 0; i < nb_numa_nodes; i++) {
3405 if (node_cpumask[i] != 0)
3406 break;
3408 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3409 * must cope with this anyway, because there are BIOSes out there in
3410 * real machines which also use this scheme.
3412 if (i == nb_numa_nodes) {
3413 for (i = 0; i < smp_cpus; i++) {
3414 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3419 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3420 exit(1);
3423 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3424 exit(1);
3425 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3426 exit(1);
3427 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3428 exit(1);
3429 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3430 exit(1);
3432 module_call_init(MODULE_INIT_DEVICE);
3434 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3435 exit(0);
3437 if (watchdog) {
3438 i = select_watchdog(watchdog);
3439 if (i > 0)
3440 exit (i == 1 ? 1 : 0);
3443 if (machine->compat_props) {
3444 qdev_prop_register_global_list(machine->compat_props);
3446 qemu_add_globals();
3448 machine->init(ram_size, boot_devices,
3449 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3451 cpu_synchronize_all_post_init();
3453 set_numa_modes();
3455 current_machine = machine;
3457 /* init USB devices */
3458 if (usb_enabled) {
3459 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3460 exit(1);
3463 /* init generic devices */
3464 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3465 exit(1);
3467 net_check_clients();
3469 /* just use the first displaystate for the moment */
3470 ds = get_displaystate();
3472 if (using_spice)
3473 display_remote++;
3474 if (display_type == DT_DEFAULT && !display_remote) {
3475 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3476 display_type = DT_SDL;
3477 #elif defined(CONFIG_VNC)
3478 vnc_display = "localhost:0,to=99";
3479 show_vnc_port = 1;
3480 #else
3481 display_type = DT_NONE;
3482 #endif
3486 /* init local displays */
3487 switch (display_type) {
3488 case DT_NOGRAPHIC:
3489 break;
3490 #if defined(CONFIG_CURSES)
3491 case DT_CURSES:
3492 curses_display_init(ds, full_screen);
3493 break;
3494 #endif
3495 #if defined(CONFIG_SDL)
3496 case DT_SDL:
3497 sdl_display_init(ds, full_screen, no_frame);
3498 break;
3499 #elif defined(CONFIG_COCOA)
3500 case DT_SDL:
3501 cocoa_display_init(ds, full_screen);
3502 break;
3503 #endif
3504 default:
3505 break;
3508 /* must be after terminal init, SDL library changes signal handlers */
3509 os_setup_signal_handling();
3511 #ifdef CONFIG_VNC
3512 /* init remote displays */
3513 if (vnc_display) {
3514 vnc_display_init(ds);
3515 if (vnc_display_open(ds, vnc_display) < 0)
3516 exit(1);
3518 if (show_vnc_port) {
3519 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3522 #endif
3523 #ifdef CONFIG_SPICE
3524 if (using_spice && !qxl_enabled) {
3525 qemu_spice_display_init(ds);
3527 #endif
3529 /* display setup */
3530 dpy_resize(ds);
3531 dcl = ds->listeners;
3532 while (dcl != NULL) {
3533 if (dcl->dpy_refresh != NULL) {
3534 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3535 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3536 break;
3538 dcl = dcl->next;
3540 text_consoles_set_display(ds);
3542 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3543 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3544 gdbstub_dev);
3545 exit(1);
3548 qdev_machine_creation_done();
3550 if (rom_load_all() != 0) {
3551 fprintf(stderr, "rom loading failed\n");
3552 exit(1);
3555 /* TODO: once all bus devices are qdevified, this should be done
3556 * when bus is created by qdev.c */
3557 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3558 qemu_run_machine_init_done_notifiers();
3560 qemu_system_reset(VMRESET_SILENT);
3561 if (loadvm) {
3562 if (load_vmstate(loadvm) < 0) {
3563 autostart = 0;
3567 if (incoming) {
3568 runstate_set(RUN_STATE_INMIGRATE);
3569 int ret = qemu_start_incoming_migration(incoming);
3570 if (ret < 0) {
3571 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3572 incoming, ret);
3573 exit(ret);
3575 } else if (autostart) {
3576 vm_start();
3579 os_setup_post();
3581 main_loop();
3582 quit_timers();
3583 net_cleanup();
3584 res_free();
3586 return 0;