Move graphic-related coalesced MMIO flushes to affected device models
[qemu.git] / vl.c
blobdd85a41dcb57ded33d304f7d687d007f887970e0
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 uint8_t irq0override = 1;
221 const char *watchdog;
222 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
223 int nb_option_roms;
224 int semihosting_enabled = 0;
225 int old_param = 0;
226 const char *qemu_name;
227 int alt_grab = 0;
228 int ctrl_grab = 0;
229 unsigned int nb_prom_envs = 0;
230 const char *prom_envs[MAX_PROM_ENVS];
231 int boot_menu;
232 uint8_t *boot_splash_filedata;
233 int boot_splash_filedata_size;
234 uint8_t qemu_extra_params_fw[2];
236 typedef struct FWBootEntry FWBootEntry;
238 struct FWBootEntry {
239 QTAILQ_ENTRY(FWBootEntry) link;
240 int32_t bootindex;
241 DeviceState *dev;
242 char *suffix;
245 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
247 int nb_numa_nodes;
248 uint64_t node_mem[MAX_NODES];
249 uint64_t node_cpumask[MAX_NODES];
251 static QEMUTimer *nographic_timer;
253 uint8_t qemu_uuid[16];
255 static QEMUBootSetHandler *boot_set_handler;
256 static void *boot_set_opaque;
258 static NotifierList exit_notifiers =
259 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
261 static NotifierList machine_init_done_notifiers =
262 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
264 static int tcg_allowed = 1;
265 int kvm_allowed = 0;
266 int xen_allowed = 0;
267 uint32_t xen_domid;
268 enum xen_mode xen_mode = XEN_EMULATE;
269 static int tcg_tb_size;
271 static int default_serial = 1;
272 static int default_parallel = 1;
273 static int default_virtcon = 1;
274 static int default_monitor = 1;
275 static int default_vga = 1;
276 static int default_floppy = 1;
277 static int default_cdrom = 1;
278 static int default_sdcard = 1;
280 static struct {
281 const char *driver;
282 int *flag;
283 } default_list[] = {
284 { .driver = "isa-serial", .flag = &default_serial },
285 { .driver = "isa-parallel", .flag = &default_parallel },
286 { .driver = "isa-fdc", .flag = &default_floppy },
287 { .driver = "ide-cd", .flag = &default_cdrom },
288 { .driver = "ide-hd", .flag = &default_cdrom },
289 { .driver = "ide-drive", .flag = &default_cdrom },
290 { .driver = "scsi-cd", .flag = &default_cdrom },
291 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
292 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
293 { .driver = "virtio-serial", .flag = &default_virtcon },
294 { .driver = "VGA", .flag = &default_vga },
295 { .driver = "cirrus-vga", .flag = &default_vga },
296 { .driver = "vmware-svga", .flag = &default_vga },
297 { .driver = "isa-vga", .flag = &default_vga },
298 { .driver = "qxl-vga", .flag = &default_vga },
301 static void res_free(void)
303 if (boot_splash_filedata != NULL) {
304 g_free(boot_splash_filedata);
305 boot_splash_filedata = NULL;
309 static int default_driver_check(QemuOpts *opts, void *opaque)
311 const char *driver = qemu_opt_get(opts, "driver");
312 int i;
314 if (!driver)
315 return 0;
316 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
317 if (strcmp(default_list[i].driver, driver) != 0)
318 continue;
319 *(default_list[i].flag) = 0;
321 return 0;
324 /***********************************************************/
325 /* QEMU state */
327 static RunState current_run_state = RUN_STATE_PRELAUNCH;
329 typedef struct {
330 RunState from;
331 RunState to;
332 } RunStateTransition;
334 static const RunStateTransition runstate_transitions_def[] = {
335 /* from -> to */
336 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
338 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
339 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
341 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
343 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
345 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
347 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
349 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
350 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
351 { RUN_STATE_PRELAUNCH, RUN_STATE_POSTMIGRATE },
353 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
354 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
356 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
358 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
359 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
360 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
361 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
362 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
363 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
364 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
365 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
366 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
368 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
370 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
372 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
374 { RUN_STATE_MAX, RUN_STATE_MAX },
377 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
379 bool runstate_check(RunState state)
381 return current_run_state == state;
384 void runstate_init(void)
386 const RunStateTransition *p;
388 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
390 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
391 runstate_valid_transitions[p->from][p->to] = true;
395 /* This function will abort() on invalid state transitions */
396 void runstate_set(RunState new_state)
398 if (new_state >= RUN_STATE_MAX ||
399 !runstate_valid_transitions[current_run_state][new_state]) {
400 fprintf(stderr, "invalid runstate transition\n");
401 abort();
404 current_run_state = new_state;
407 int runstate_is_running(void)
409 return runstate_check(RUN_STATE_RUNNING);
412 StatusInfo *qmp_query_status(Error **errp)
414 StatusInfo *info = g_malloc0(sizeof(*info));
416 info->running = runstate_is_running();
417 info->singlestep = singlestep;
418 info->status = current_run_state;
420 return info;
423 /***********************************************************/
424 /* real time host monotonic timer */
426 /***********************************************************/
427 /* host time/date access */
428 void qemu_get_timedate(struct tm *tm, int offset)
430 time_t ti;
431 struct tm *ret;
433 time(&ti);
434 ti += offset;
435 if (rtc_date_offset == -1) {
436 if (rtc_utc)
437 ret = gmtime(&ti);
438 else
439 ret = localtime(&ti);
440 } else {
441 ti -= rtc_date_offset;
442 ret = gmtime(&ti);
445 memcpy(tm, ret, sizeof(struct tm));
448 int qemu_timedate_diff(struct tm *tm)
450 time_t seconds;
452 if (rtc_date_offset == -1)
453 if (rtc_utc)
454 seconds = mktimegm(tm);
455 else
456 seconds = mktime(tm);
457 else
458 seconds = mktimegm(tm) + rtc_date_offset;
460 return seconds - time(NULL);
463 void rtc_change_mon_event(struct tm *tm)
465 QObject *data;
467 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
468 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
469 qobject_decref(data);
472 static void configure_rtc_date_offset(const char *startdate, int legacy)
474 time_t rtc_start_date;
475 struct tm tm;
477 if (!strcmp(startdate, "now") && legacy) {
478 rtc_date_offset = -1;
479 } else {
480 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
481 &tm.tm_year,
482 &tm.tm_mon,
483 &tm.tm_mday,
484 &tm.tm_hour,
485 &tm.tm_min,
486 &tm.tm_sec) == 6) {
487 /* OK */
488 } else if (sscanf(startdate, "%d-%d-%d",
489 &tm.tm_year,
490 &tm.tm_mon,
491 &tm.tm_mday) == 3) {
492 tm.tm_hour = 0;
493 tm.tm_min = 0;
494 tm.tm_sec = 0;
495 } else {
496 goto date_fail;
498 tm.tm_year -= 1900;
499 tm.tm_mon--;
500 rtc_start_date = mktimegm(&tm);
501 if (rtc_start_date == -1) {
502 date_fail:
503 fprintf(stderr, "Invalid date format. Valid formats are:\n"
504 "'2006-06-17T16:01:21' or '2006-06-17'\n");
505 exit(1);
507 rtc_date_offset = time(NULL) - rtc_start_date;
511 static void configure_rtc(QemuOpts *opts)
513 const char *value;
515 value = qemu_opt_get(opts, "base");
516 if (value) {
517 if (!strcmp(value, "utc")) {
518 rtc_utc = 1;
519 } else if (!strcmp(value, "localtime")) {
520 rtc_utc = 0;
521 } else {
522 configure_rtc_date_offset(value, 0);
525 value = qemu_opt_get(opts, "clock");
526 if (value) {
527 if (!strcmp(value, "host")) {
528 rtc_clock = host_clock;
529 } else if (!strcmp(value, "vm")) {
530 rtc_clock = vm_clock;
531 } else {
532 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
533 exit(1);
536 value = qemu_opt_get(opts, "driftfix");
537 if (value) {
538 if (!strcmp(value, "slew")) {
539 rtc_td_hack = 1;
540 } else if (!strcmp(value, "none")) {
541 rtc_td_hack = 0;
542 } else {
543 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
544 exit(1);
549 /***********************************************************/
550 /* Bluetooth support */
551 static int nb_hcis;
552 static int cur_hci;
553 static struct HCIInfo *hci_table[MAX_NICS];
555 static struct bt_vlan_s {
556 struct bt_scatternet_s net;
557 int id;
558 struct bt_vlan_s *next;
559 } *first_bt_vlan;
561 /* find or alloc a new bluetooth "VLAN" */
562 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
564 struct bt_vlan_s **pvlan, *vlan;
565 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
566 if (vlan->id == id)
567 return &vlan->net;
569 vlan = g_malloc0(sizeof(struct bt_vlan_s));
570 vlan->id = id;
571 pvlan = &first_bt_vlan;
572 while (*pvlan != NULL)
573 pvlan = &(*pvlan)->next;
574 *pvlan = vlan;
575 return &vlan->net;
578 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
582 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
584 return -ENOTSUP;
587 static struct HCIInfo null_hci = {
588 .cmd_send = null_hci_send,
589 .sco_send = null_hci_send,
590 .acl_send = null_hci_send,
591 .bdaddr_set = null_hci_addr_set,
594 struct HCIInfo *qemu_next_hci(void)
596 if (cur_hci == nb_hcis)
597 return &null_hci;
599 return hci_table[cur_hci++];
602 static struct HCIInfo *hci_init(const char *str)
604 char *endp;
605 struct bt_scatternet_s *vlan = 0;
607 if (!strcmp(str, "null"))
608 /* null */
609 return &null_hci;
610 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
611 /* host[:hciN] */
612 return bt_host_hci(str[4] ? str + 5 : "hci0");
613 else if (!strncmp(str, "hci", 3)) {
614 /* hci[,vlan=n] */
615 if (str[3]) {
616 if (!strncmp(str + 3, ",vlan=", 6)) {
617 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
618 if (*endp)
619 vlan = 0;
621 } else
622 vlan = qemu_find_bt_vlan(0);
623 if (vlan)
624 return bt_new_hci(vlan);
627 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
629 return 0;
632 static int bt_hci_parse(const char *str)
634 struct HCIInfo *hci;
635 bdaddr_t bdaddr;
637 if (nb_hcis >= MAX_NICS) {
638 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
639 return -1;
642 hci = hci_init(str);
643 if (!hci)
644 return -1;
646 bdaddr.b[0] = 0x52;
647 bdaddr.b[1] = 0x54;
648 bdaddr.b[2] = 0x00;
649 bdaddr.b[3] = 0x12;
650 bdaddr.b[4] = 0x34;
651 bdaddr.b[5] = 0x56 + nb_hcis;
652 hci->bdaddr_set(hci, bdaddr.b);
654 hci_table[nb_hcis++] = hci;
656 return 0;
659 static void bt_vhci_add(int vlan_id)
661 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
663 if (!vlan->slave)
664 fprintf(stderr, "qemu: warning: adding a VHCI to "
665 "an empty scatternet %i\n", vlan_id);
667 bt_vhci_init(bt_new_hci(vlan));
670 static struct bt_device_s *bt_device_add(const char *opt)
672 struct bt_scatternet_s *vlan;
673 int vlan_id = 0;
674 char *endp = strstr(opt, ",vlan=");
675 int len = (endp ? endp - opt : strlen(opt)) + 1;
676 char devname[10];
678 pstrcpy(devname, MIN(sizeof(devname), len), opt);
680 if (endp) {
681 vlan_id = strtol(endp + 6, &endp, 0);
682 if (*endp) {
683 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
684 return 0;
688 vlan = qemu_find_bt_vlan(vlan_id);
690 if (!vlan->slave)
691 fprintf(stderr, "qemu: warning: adding a slave device to "
692 "an empty scatternet %i\n", vlan_id);
694 if (!strcmp(devname, "keyboard"))
695 return bt_keyboard_init(vlan);
697 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
698 return 0;
701 static int bt_parse(const char *opt)
703 const char *endp, *p;
704 int vlan;
706 if (strstart(opt, "hci", &endp)) {
707 if (!*endp || *endp == ',') {
708 if (*endp)
709 if (!strstart(endp, ",vlan=", 0))
710 opt = endp + 1;
712 return bt_hci_parse(opt);
714 } else if (strstart(opt, "vhci", &endp)) {
715 if (!*endp || *endp == ',') {
716 if (*endp) {
717 if (strstart(endp, ",vlan=", &p)) {
718 vlan = strtol(p, (char **) &endp, 0);
719 if (*endp) {
720 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
721 return 1;
723 } else {
724 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
725 return 1;
727 } else
728 vlan = 0;
730 bt_vhci_add(vlan);
731 return 0;
733 } else if (strstart(opt, "device:", &endp))
734 return !bt_device_add(endp);
736 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
737 return 1;
740 /***********************************************************/
741 /* QEMU Block devices */
743 #define HD_OPTS "media=disk"
744 #define CDROM_OPTS "media=cdrom"
745 #define FD_OPTS ""
746 #define PFLASH_OPTS ""
747 #define MTD_OPTS ""
748 #define SD_OPTS ""
750 static int drive_init_func(QemuOpts *opts, void *opaque)
752 int *use_scsi = opaque;
754 return drive_init(opts, *use_scsi) == NULL;
757 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
759 if (NULL == qemu_opt_get(opts, "snapshot")) {
760 qemu_opt_set(opts, "snapshot", "on");
762 return 0;
765 static void default_drive(int enable, int snapshot, int use_scsi,
766 BlockInterfaceType type, int index,
767 const char *optstr)
769 QemuOpts *opts;
771 if (type == IF_DEFAULT) {
772 type = use_scsi ? IF_SCSI : IF_IDE;
775 if (!enable || drive_get_by_index(type, index)) {
776 return;
779 opts = drive_add(type, index, NULL, optstr);
780 if (snapshot) {
781 drive_enable_snapshot(opts, NULL);
783 if (!drive_init(opts, use_scsi)) {
784 exit(1);
788 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
790 boot_set_handler = func;
791 boot_set_opaque = opaque;
794 int qemu_boot_set(const char *boot_devices)
796 if (!boot_set_handler) {
797 return -EINVAL;
799 return boot_set_handler(boot_set_opaque, boot_devices);
802 static void validate_bootdevices(char *devices)
804 /* We just do some generic consistency checks */
805 const char *p;
806 int bitmap = 0;
808 for (p = devices; *p != '\0'; p++) {
809 /* Allowed boot devices are:
810 * a-b: floppy disk drives
811 * c-f: IDE disk drives
812 * g-m: machine implementation dependant drives
813 * n-p: network devices
814 * It's up to each machine implementation to check if the given boot
815 * devices match the actual hardware implementation and firmware
816 * features.
818 if (*p < 'a' || *p > 'p') {
819 fprintf(stderr, "Invalid boot device '%c'\n", *p);
820 exit(1);
822 if (bitmap & (1 << (*p - 'a'))) {
823 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
824 exit(1);
826 bitmap |= 1 << (*p - 'a');
830 static void restore_boot_devices(void *opaque)
832 char *standard_boot_devices = opaque;
833 static int first = 1;
835 /* Restore boot order and remove ourselves after the first boot */
836 if (first) {
837 first = 0;
838 return;
841 qemu_boot_set(standard_boot_devices);
843 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
844 g_free(standard_boot_devices);
847 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
848 const char *suffix)
850 FWBootEntry *node, *i;
852 if (bootindex < 0) {
853 return;
856 assert(dev != NULL || suffix != NULL);
858 node = g_malloc0(sizeof(FWBootEntry));
859 node->bootindex = bootindex;
860 node->suffix = suffix ? g_strdup(suffix) : NULL;
861 node->dev = dev;
863 QTAILQ_FOREACH(i, &fw_boot_order, link) {
864 if (i->bootindex == bootindex) {
865 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
866 exit(1);
867 } else if (i->bootindex < bootindex) {
868 continue;
870 QTAILQ_INSERT_BEFORE(i, node, link);
871 return;
873 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
877 * This function returns null terminated string that consist of new line
878 * separated device paths.
880 * memory pointed by "size" is assigned total length of the array in bytes
883 char *get_boot_devices_list(uint32_t *size)
885 FWBootEntry *i;
886 uint32_t total = 0;
887 char *list = NULL;
889 QTAILQ_FOREACH(i, &fw_boot_order, link) {
890 char *devpath = NULL, *bootpath;
891 int len;
893 if (i->dev) {
894 devpath = qdev_get_fw_dev_path(i->dev);
895 assert(devpath);
898 if (i->suffix && devpath) {
899 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
901 bootpath = g_malloc(bootpathlen);
902 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
903 g_free(devpath);
904 } else if (devpath) {
905 bootpath = devpath;
906 } else {
907 bootpath = g_strdup(i->suffix);
908 assert(bootpath);
911 if (total) {
912 list[total-1] = '\n';
914 len = strlen(bootpath) + 1;
915 list = g_realloc(list, total + len);
916 memcpy(&list[total], bootpath, len);
917 total += len;
918 g_free(bootpath);
921 *size = total;
923 return list;
926 static void numa_add(const char *optarg)
928 char option[128];
929 char *endptr;
930 unsigned long long value, endvalue;
931 int nodenr;
933 optarg = get_opt_name(option, 128, optarg, ',') + 1;
934 if (!strcmp(option, "node")) {
935 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
936 nodenr = nb_numa_nodes;
937 } else {
938 nodenr = strtoull(option, NULL, 10);
941 if (get_param_value(option, 128, "mem", optarg) == 0) {
942 node_mem[nodenr] = 0;
943 } else {
944 int64_t sval;
945 sval = strtosz(option, NULL);
946 if (sval < 0) {
947 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
948 exit(1);
950 node_mem[nodenr] = sval;
952 if (get_param_value(option, 128, "cpus", optarg) == 0) {
953 node_cpumask[nodenr] = 0;
954 } else {
955 value = strtoull(option, &endptr, 10);
956 if (value >= 64) {
957 value = 63;
958 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
959 } else {
960 if (*endptr == '-') {
961 endvalue = strtoull(endptr+1, &endptr, 10);
962 if (endvalue >= 63) {
963 endvalue = 62;
964 fprintf(stderr,
965 "only 63 CPUs in NUMA mode supported.\n");
967 value = (2ULL << endvalue) - (1ULL << value);
968 } else {
969 value = 1ULL << value;
972 node_cpumask[nodenr] = value;
974 nb_numa_nodes++;
976 return;
979 static void smp_parse(const char *optarg)
981 int smp, sockets = 0, threads = 0, cores = 0;
982 char *endptr;
983 char option[128];
985 smp = strtoul(optarg, &endptr, 10);
986 if (endptr != optarg) {
987 if (*endptr == ',') {
988 endptr++;
991 if (get_param_value(option, 128, "sockets", endptr) != 0)
992 sockets = strtoull(option, NULL, 10);
993 if (get_param_value(option, 128, "cores", endptr) != 0)
994 cores = strtoull(option, NULL, 10);
995 if (get_param_value(option, 128, "threads", endptr) != 0)
996 threads = strtoull(option, NULL, 10);
997 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
998 max_cpus = strtoull(option, NULL, 10);
1000 /* compute missing values, prefer sockets over cores over threads */
1001 if (smp == 0 || sockets == 0) {
1002 sockets = sockets > 0 ? sockets : 1;
1003 cores = cores > 0 ? cores : 1;
1004 threads = threads > 0 ? threads : 1;
1005 if (smp == 0) {
1006 smp = cores * threads * sockets;
1008 } else {
1009 if (cores == 0) {
1010 threads = threads > 0 ? threads : 1;
1011 cores = smp / (sockets * threads);
1012 } else {
1013 threads = smp / (cores * sockets);
1016 smp_cpus = smp;
1017 smp_cores = cores > 0 ? cores : 1;
1018 smp_threads = threads > 0 ? threads : 1;
1019 if (max_cpus == 0)
1020 max_cpus = smp_cpus;
1023 /***********************************************************/
1024 /* USB devices */
1026 static int usb_device_add(const char *devname)
1028 const char *p;
1029 USBDevice *dev = NULL;
1031 if (!usb_enabled)
1032 return -1;
1034 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1035 dev = usbdevice_create(devname);
1036 if (dev)
1037 goto done;
1039 /* the other ones */
1040 #ifndef CONFIG_LINUX
1041 /* only the linux version is qdev-ified, usb-bsd still needs this */
1042 if (strstart(devname, "host:", &p)) {
1043 dev = usb_host_device_open(p);
1044 } else
1045 #endif
1046 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1047 dev = usb_bt_init(devname[2] ? hci_init(p) :
1048 bt_new_hci(qemu_find_bt_vlan(0)));
1049 } else {
1050 return -1;
1052 if (!dev)
1053 return -1;
1055 done:
1056 return 0;
1059 static int usb_device_del(const char *devname)
1061 int bus_num, addr;
1062 const char *p;
1064 if (strstart(devname, "host:", &p))
1065 return usb_host_device_close(p);
1067 if (!usb_enabled)
1068 return -1;
1070 p = strchr(devname, '.');
1071 if (!p)
1072 return -1;
1073 bus_num = strtoul(devname, NULL, 0);
1074 addr = strtoul(p + 1, NULL, 0);
1076 return usb_device_delete_addr(bus_num, addr);
1079 static int usb_parse(const char *cmdline)
1081 int r;
1082 r = usb_device_add(cmdline);
1083 if (r < 0) {
1084 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1086 return r;
1089 void do_usb_add(Monitor *mon, const QDict *qdict)
1091 const char *devname = qdict_get_str(qdict, "devname");
1092 if (usb_device_add(devname) < 0) {
1093 error_report("could not add USB device '%s'", devname);
1097 void do_usb_del(Monitor *mon, const QDict *qdict)
1099 const char *devname = qdict_get_str(qdict, "devname");
1100 if (usb_device_del(devname) < 0) {
1101 error_report("could not delete USB device '%s'", devname);
1105 /***********************************************************/
1106 /* PCMCIA/Cardbus */
1108 static struct pcmcia_socket_entry_s {
1109 PCMCIASocket *socket;
1110 struct pcmcia_socket_entry_s *next;
1111 } *pcmcia_sockets = 0;
1113 void pcmcia_socket_register(PCMCIASocket *socket)
1115 struct pcmcia_socket_entry_s *entry;
1117 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1118 entry->socket = socket;
1119 entry->next = pcmcia_sockets;
1120 pcmcia_sockets = entry;
1123 void pcmcia_socket_unregister(PCMCIASocket *socket)
1125 struct pcmcia_socket_entry_s *entry, **ptr;
1127 ptr = &pcmcia_sockets;
1128 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1129 if (entry->socket == socket) {
1130 *ptr = entry->next;
1131 g_free(entry);
1135 void pcmcia_info(Monitor *mon)
1137 struct pcmcia_socket_entry_s *iter;
1139 if (!pcmcia_sockets)
1140 monitor_printf(mon, "No PCMCIA sockets\n");
1142 for (iter = pcmcia_sockets; iter; iter = iter->next)
1143 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1144 iter->socket->attached ? iter->socket->card_string :
1145 "Empty");
1148 /***********************************************************/
1149 /* machine registration */
1151 static QEMUMachine *first_machine = NULL;
1152 QEMUMachine *current_machine = NULL;
1154 int qemu_register_machine(QEMUMachine *m)
1156 QEMUMachine **pm;
1157 pm = &first_machine;
1158 while (*pm != NULL)
1159 pm = &(*pm)->next;
1160 m->next = NULL;
1161 *pm = m;
1162 return 0;
1165 static QEMUMachine *find_machine(const char *name)
1167 QEMUMachine *m;
1169 for(m = first_machine; m != NULL; m = m->next) {
1170 if (!strcmp(m->name, name))
1171 return m;
1172 if (m->alias && !strcmp(m->alias, name))
1173 return m;
1175 return NULL;
1178 static QEMUMachine *find_default_machine(void)
1180 QEMUMachine *m;
1182 for(m = first_machine; m != NULL; m = m->next) {
1183 if (m->is_default) {
1184 return m;
1187 return NULL;
1190 /***********************************************************/
1191 /* main execution loop */
1193 static void gui_update(void *opaque)
1195 uint64_t interval = GUI_REFRESH_INTERVAL;
1196 DisplayState *ds = opaque;
1197 DisplayChangeListener *dcl = ds->listeners;
1199 dpy_refresh(ds);
1201 while (dcl != NULL) {
1202 if (dcl->gui_timer_interval &&
1203 dcl->gui_timer_interval < interval)
1204 interval = dcl->gui_timer_interval;
1205 dcl = dcl->next;
1207 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1210 static void nographic_update(void *opaque)
1212 uint64_t interval = GUI_REFRESH_INTERVAL;
1214 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1217 struct vm_change_state_entry {
1218 VMChangeStateHandler *cb;
1219 void *opaque;
1220 QLIST_ENTRY (vm_change_state_entry) entries;
1223 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1225 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1226 void *opaque)
1228 VMChangeStateEntry *e;
1230 e = g_malloc0(sizeof (*e));
1232 e->cb = cb;
1233 e->opaque = opaque;
1234 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1235 return e;
1238 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1240 QLIST_REMOVE (e, entries);
1241 g_free (e);
1244 void vm_state_notify(int running, RunState state)
1246 VMChangeStateEntry *e;
1248 trace_vm_state_notify(running, state);
1250 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1251 e->cb(e->opaque, running, state);
1255 void vm_start(void)
1257 if (!runstate_is_running()) {
1258 cpu_enable_ticks();
1259 runstate_set(RUN_STATE_RUNNING);
1260 vm_state_notify(1, RUN_STATE_RUNNING);
1261 resume_all_vcpus();
1262 monitor_protocol_event(QEVENT_RESUME, NULL);
1266 /* reset/shutdown handler */
1268 typedef struct QEMUResetEntry {
1269 QTAILQ_ENTRY(QEMUResetEntry) entry;
1270 QEMUResetHandler *func;
1271 void *opaque;
1272 } QEMUResetEntry;
1274 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1275 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1276 static int reset_requested;
1277 static int shutdown_requested, shutdown_signal = -1;
1278 static pid_t shutdown_pid;
1279 static int powerdown_requested;
1280 static int debug_requested;
1281 static RunState vmstop_requested = RUN_STATE_MAX;
1283 int qemu_shutdown_requested_get(void)
1285 return shutdown_requested;
1288 int qemu_reset_requested_get(void)
1290 return reset_requested;
1293 int qemu_shutdown_requested(void)
1295 int r = shutdown_requested;
1296 shutdown_requested = 0;
1297 return r;
1300 void qemu_kill_report(void)
1302 if (shutdown_signal != -1) {
1303 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1304 if (shutdown_pid == 0) {
1305 /* This happens for eg ^C at the terminal, so it's worth
1306 * avoiding printing an odd message in that case.
1308 fputc('\n', stderr);
1309 } else {
1310 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1312 shutdown_signal = -1;
1316 int qemu_reset_requested(void)
1318 int r = reset_requested;
1319 reset_requested = 0;
1320 return r;
1323 int qemu_powerdown_requested(void)
1325 int r = powerdown_requested;
1326 powerdown_requested = 0;
1327 return r;
1330 static int qemu_debug_requested(void)
1332 int r = debug_requested;
1333 debug_requested = 0;
1334 return r;
1337 /* We use RUN_STATE_MAX but any invalid value will do */
1338 static bool qemu_vmstop_requested(RunState *r)
1340 if (vmstop_requested < RUN_STATE_MAX) {
1341 *r = vmstop_requested;
1342 vmstop_requested = RUN_STATE_MAX;
1343 return true;
1346 return false;
1349 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1351 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1353 re->func = func;
1354 re->opaque = opaque;
1355 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1358 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1360 QEMUResetEntry *re;
1362 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1363 if (re->func == func && re->opaque == opaque) {
1364 QTAILQ_REMOVE(&reset_handlers, re, entry);
1365 g_free(re);
1366 return;
1371 void qemu_system_reset(bool report)
1373 QEMUResetEntry *re, *nre;
1375 /* reset all devices */
1376 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1377 re->func(re->opaque);
1379 if (report) {
1380 monitor_protocol_event(QEVENT_RESET, NULL);
1382 cpu_synchronize_all_post_reset();
1385 void qemu_system_reset_request(void)
1387 if (no_reboot) {
1388 shutdown_requested = 1;
1389 } else {
1390 reset_requested = 1;
1392 cpu_stop_current();
1393 qemu_notify_event();
1396 void qemu_system_killed(int signal, pid_t pid)
1398 shutdown_signal = signal;
1399 shutdown_pid = pid;
1400 no_shutdown = 0;
1401 qemu_system_shutdown_request();
1404 void qemu_system_shutdown_request(void)
1406 shutdown_requested = 1;
1407 qemu_notify_event();
1410 void qemu_system_powerdown_request(void)
1412 powerdown_requested = 1;
1413 qemu_notify_event();
1416 void qemu_system_debug_request(void)
1418 debug_requested = 1;
1419 qemu_notify_event();
1422 void qemu_system_vmstop_request(RunState state)
1424 vmstop_requested = state;
1425 qemu_notify_event();
1428 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1429 static int n_poll_fds;
1430 static int max_priority;
1432 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1433 fd_set *xfds, struct timeval *tv)
1435 GMainContext *context = g_main_context_default();
1436 int i;
1437 int timeout = 0, cur_timeout;
1439 g_main_context_prepare(context, &max_priority);
1441 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1442 poll_fds, ARRAY_SIZE(poll_fds));
1443 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1445 for (i = 0; i < n_poll_fds; i++) {
1446 GPollFD *p = &poll_fds[i];
1448 if ((p->events & G_IO_IN)) {
1449 FD_SET(p->fd, rfds);
1450 *max_fd = MAX(*max_fd, p->fd);
1452 if ((p->events & G_IO_OUT)) {
1453 FD_SET(p->fd, wfds);
1454 *max_fd = MAX(*max_fd, p->fd);
1456 if ((p->events & G_IO_ERR)) {
1457 FD_SET(p->fd, xfds);
1458 *max_fd = MAX(*max_fd, p->fd);
1462 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1463 if (timeout >= 0 && timeout < cur_timeout) {
1464 tv->tv_sec = timeout / 1000;
1465 tv->tv_usec = (timeout % 1000) * 1000;
1469 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1470 bool err)
1472 GMainContext *context = g_main_context_default();
1474 if (!err) {
1475 int i;
1477 for (i = 0; i < n_poll_fds; i++) {
1478 GPollFD *p = &poll_fds[i];
1480 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1481 p->revents |= G_IO_IN;
1483 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1484 p->revents |= G_IO_OUT;
1486 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1487 p->revents |= G_IO_ERR;
1492 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1493 g_main_context_dispatch(context);
1497 int main_loop_wait(int nonblocking)
1499 fd_set rfds, wfds, xfds;
1500 int ret, nfds;
1501 struct timeval tv;
1502 int timeout;
1504 if (nonblocking)
1505 timeout = 0;
1506 else {
1507 timeout = qemu_calculate_timeout();
1508 qemu_bh_update_timeout(&timeout);
1511 os_host_main_loop_wait(&timeout);
1513 tv.tv_sec = timeout / 1000;
1514 tv.tv_usec = (timeout % 1000) * 1000;
1516 /* poll any events */
1517 /* XXX: separate device handlers from system ones */
1518 nfds = -1;
1519 FD_ZERO(&rfds);
1520 FD_ZERO(&wfds);
1521 FD_ZERO(&xfds);
1523 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1524 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1525 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
1527 if (timeout > 0) {
1528 qemu_mutex_unlock_iothread();
1531 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1533 if (timeout > 0) {
1534 qemu_mutex_lock_iothread();
1537 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1538 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1539 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1541 qemu_run_all_timers();
1543 /* Check bottom-halves last in case any of the earlier events triggered
1544 them. */
1545 qemu_bh_poll();
1547 return ret;
1550 qemu_irq qemu_system_powerdown;
1552 static void main_loop(void)
1554 bool nonblocking;
1555 int last_io __attribute__ ((unused)) = 0;
1556 #ifdef CONFIG_PROFILER
1557 int64_t ti;
1558 #endif
1559 RunState r;
1561 qemu_main_loop_start();
1563 for (;;) {
1564 nonblocking = !kvm_enabled() && last_io > 0;
1565 #ifdef CONFIG_PROFILER
1566 ti = profile_getclock();
1567 #endif
1568 last_io = main_loop_wait(nonblocking);
1569 #ifdef CONFIG_PROFILER
1570 dev_time += profile_getclock() - ti;
1571 #endif
1573 if (qemu_debug_requested()) {
1574 vm_stop(RUN_STATE_DEBUG);
1576 if (qemu_shutdown_requested()) {
1577 qemu_kill_report();
1578 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1579 if (no_shutdown) {
1580 vm_stop(RUN_STATE_SHUTDOWN);
1581 } else
1582 break;
1584 if (qemu_reset_requested()) {
1585 pause_all_vcpus();
1586 cpu_synchronize_all_states();
1587 qemu_system_reset(VMRESET_REPORT);
1588 resume_all_vcpus();
1589 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1590 runstate_check(RUN_STATE_SHUTDOWN)) {
1591 runstate_set(RUN_STATE_PAUSED);
1594 if (qemu_powerdown_requested()) {
1595 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1596 qemu_irq_raise(qemu_system_powerdown);
1598 if (qemu_vmstop_requested(&r)) {
1599 vm_stop(r);
1602 bdrv_close_all();
1603 pause_all_vcpus();
1606 static void version(void)
1608 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1611 static void help(int exitcode)
1613 const char *options_help =
1614 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1615 opt_help
1616 #define DEFHEADING(text) stringify(text) "\n"
1617 #include "qemu-options.def"
1618 #undef DEF
1619 #undef DEFHEADING
1620 #undef GEN_DOCS
1622 version();
1623 printf("usage: %s [options] [disk_image]\n"
1624 "\n"
1625 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1626 "\n"
1627 "%s\n"
1628 "During emulation, the following keys are useful:\n"
1629 "ctrl-alt-f toggle full screen\n"
1630 "ctrl-alt-n switch to virtual console 'n'\n"
1631 "ctrl-alt toggle mouse and keyboard grab\n"
1632 "\n"
1633 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1634 "qemu",
1635 options_help);
1636 exit(exitcode);
1639 #define HAS_ARG 0x0001
1641 typedef struct QEMUOption {
1642 const char *name;
1643 int flags;
1644 int index;
1645 uint32_t arch_mask;
1646 } QEMUOption;
1648 static const QEMUOption qemu_options[] = {
1649 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1650 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1651 { option, opt_arg, opt_enum, arch_mask },
1652 #define DEFHEADING(text)
1653 #include "qemu-options.def"
1654 #undef DEF
1655 #undef DEFHEADING
1656 #undef GEN_DOCS
1657 { NULL },
1659 static void select_vgahw (const char *p)
1661 const char *opts;
1663 default_vga = 0;
1664 vga_interface_type = VGA_NONE;
1665 if (strstart(p, "std", &opts)) {
1666 vga_interface_type = VGA_STD;
1667 } else if (strstart(p, "cirrus", &opts)) {
1668 vga_interface_type = VGA_CIRRUS;
1669 } else if (strstart(p, "vmware", &opts)) {
1670 vga_interface_type = VGA_VMWARE;
1671 } else if (strstart(p, "xenfb", &opts)) {
1672 vga_interface_type = VGA_XENFB;
1673 } else if (strstart(p, "qxl", &opts)) {
1674 vga_interface_type = VGA_QXL;
1675 } else if (!strstart(p, "none", &opts)) {
1676 invalid_vga:
1677 fprintf(stderr, "Unknown vga type: %s\n", p);
1678 exit(1);
1680 while (*opts) {
1681 const char *nextopt;
1683 if (strstart(opts, ",retrace=", &nextopt)) {
1684 opts = nextopt;
1685 if (strstart(opts, "dumb", &nextopt))
1686 vga_retrace_method = VGA_RETRACE_DUMB;
1687 else if (strstart(opts, "precise", &nextopt))
1688 vga_retrace_method = VGA_RETRACE_PRECISE;
1689 else goto invalid_vga;
1690 } else goto invalid_vga;
1691 opts = nextopt;
1695 static DisplayType select_display(const char *p)
1697 const char *opts;
1698 DisplayType display = DT_DEFAULT;
1700 if (strstart(p, "sdl", &opts)) {
1701 #ifdef CONFIG_SDL
1702 display = DT_SDL;
1703 while (*opts) {
1704 const char *nextopt;
1706 if (strstart(opts, ",frame=", &nextopt)) {
1707 opts = nextopt;
1708 if (strstart(opts, "on", &nextopt)) {
1709 no_frame = 0;
1710 } else if (strstart(opts, "off", &nextopt)) {
1711 no_frame = 1;
1712 } else {
1713 goto invalid_sdl_args;
1715 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1716 opts = nextopt;
1717 if (strstart(opts, "on", &nextopt)) {
1718 alt_grab = 1;
1719 } else if (strstart(opts, "off", &nextopt)) {
1720 alt_grab = 0;
1721 } else {
1722 goto invalid_sdl_args;
1724 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1725 opts = nextopt;
1726 if (strstart(opts, "on", &nextopt)) {
1727 ctrl_grab = 1;
1728 } else if (strstart(opts, "off", &nextopt)) {
1729 ctrl_grab = 0;
1730 } else {
1731 goto invalid_sdl_args;
1733 } else if (strstart(opts, ",window_close=", &nextopt)) {
1734 opts = nextopt;
1735 if (strstart(opts, "on", &nextopt)) {
1736 no_quit = 0;
1737 } else if (strstart(opts, "off", &nextopt)) {
1738 no_quit = 1;
1739 } else {
1740 goto invalid_sdl_args;
1742 } else {
1743 invalid_sdl_args:
1744 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1745 exit(1);
1747 opts = nextopt;
1749 #else
1750 fprintf(stderr, "SDL support is disabled\n");
1751 exit(1);
1752 #endif
1753 } else if (strstart(p, "vnc", &opts)) {
1754 #ifdef CONFIG_VNC
1755 display_remote++;
1757 if (*opts) {
1758 const char *nextopt;
1760 if (strstart(opts, "=", &nextopt)) {
1761 vnc_display = nextopt;
1764 if (!vnc_display) {
1765 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1766 exit(1);
1768 #else
1769 fprintf(stderr, "VNC support is disabled\n");
1770 exit(1);
1771 #endif
1772 } else if (strstart(p, "curses", &opts)) {
1773 #ifdef CONFIG_CURSES
1774 display = DT_CURSES;
1775 #else
1776 fprintf(stderr, "Curses support is disabled\n");
1777 exit(1);
1778 #endif
1779 } else if (strstart(p, "none", &opts)) {
1780 display = DT_NONE;
1781 } else {
1782 fprintf(stderr, "Unknown display type: %s\n", p);
1783 exit(1);
1786 return display;
1789 static int balloon_parse(const char *arg)
1791 QemuOpts *opts;
1793 if (strcmp(arg, "none") == 0) {
1794 return 0;
1797 if (!strncmp(arg, "virtio", 6)) {
1798 if (arg[6] == ',') {
1799 /* have params -> parse them */
1800 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1801 if (!opts)
1802 return -1;
1803 } else {
1804 /* create empty opts */
1805 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1807 qemu_opt_set(opts, "driver", "virtio-balloon");
1808 return 0;
1811 return -1;
1814 char *qemu_find_file(int type, const char *name)
1816 int len;
1817 const char *subdir;
1818 char *buf;
1820 /* If name contains path separators then try it as a straight path. */
1821 if ((strchr(name, '/') || strchr(name, '\\'))
1822 && access(name, R_OK) == 0) {
1823 return g_strdup(name);
1825 switch (type) {
1826 case QEMU_FILE_TYPE_BIOS:
1827 subdir = "";
1828 break;
1829 case QEMU_FILE_TYPE_KEYMAP:
1830 subdir = "keymaps/";
1831 break;
1832 default:
1833 abort();
1835 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1836 buf = g_malloc0(len);
1837 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1838 if (access(buf, R_OK)) {
1839 g_free(buf);
1840 return NULL;
1842 return buf;
1845 static int device_help_func(QemuOpts *opts, void *opaque)
1847 return qdev_device_help(opts);
1850 static int device_init_func(QemuOpts *opts, void *opaque)
1852 DeviceState *dev;
1854 dev = qdev_device_add(opts);
1855 if (!dev)
1856 return -1;
1857 return 0;
1860 static int chardev_init_func(QemuOpts *opts, void *opaque)
1862 CharDriverState *chr;
1864 chr = qemu_chr_new_from_opts(opts, NULL);
1865 if (!chr)
1866 return -1;
1867 return 0;
1870 #ifdef CONFIG_VIRTFS
1871 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1873 int ret;
1874 ret = qemu_fsdev_add(opts);
1876 return ret;
1878 #endif
1880 static int mon_init_func(QemuOpts *opts, void *opaque)
1882 CharDriverState *chr;
1883 const char *chardev;
1884 const char *mode;
1885 int flags;
1887 mode = qemu_opt_get(opts, "mode");
1888 if (mode == NULL) {
1889 mode = "readline";
1891 if (strcmp(mode, "readline") == 0) {
1892 flags = MONITOR_USE_READLINE;
1893 } else if (strcmp(mode, "control") == 0) {
1894 flags = MONITOR_USE_CONTROL;
1895 } else {
1896 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1897 exit(1);
1900 if (qemu_opt_get_bool(opts, "pretty", 0))
1901 flags |= MONITOR_USE_PRETTY;
1903 if (qemu_opt_get_bool(opts, "default", 0))
1904 flags |= MONITOR_IS_DEFAULT;
1906 chardev = qemu_opt_get(opts, "chardev");
1907 chr = qemu_chr_find(chardev);
1908 if (chr == NULL) {
1909 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1910 exit(1);
1913 monitor_init(chr, flags);
1914 return 0;
1917 static void monitor_parse(const char *optarg, const char *mode)
1919 static int monitor_device_index = 0;
1920 QemuOpts *opts;
1921 const char *p;
1922 char label[32];
1923 int def = 0;
1925 if (strstart(optarg, "chardev:", &p)) {
1926 snprintf(label, sizeof(label), "%s", p);
1927 } else {
1928 snprintf(label, sizeof(label), "compat_monitor%d",
1929 monitor_device_index);
1930 if (monitor_device_index == 0) {
1931 def = 1;
1933 opts = qemu_chr_parse_compat(label, optarg);
1934 if (!opts) {
1935 fprintf(stderr, "parse error: %s\n", optarg);
1936 exit(1);
1940 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1941 if (!opts) {
1942 fprintf(stderr, "duplicate chardev: %s\n", label);
1943 exit(1);
1945 qemu_opt_set(opts, "mode", mode);
1946 qemu_opt_set(opts, "chardev", label);
1947 if (def)
1948 qemu_opt_set(opts, "default", "on");
1949 monitor_device_index++;
1952 struct device_config {
1953 enum {
1954 DEV_USB, /* -usbdevice */
1955 DEV_BT, /* -bt */
1956 DEV_SERIAL, /* -serial */
1957 DEV_PARALLEL, /* -parallel */
1958 DEV_VIRTCON, /* -virtioconsole */
1959 DEV_DEBUGCON, /* -debugcon */
1960 } type;
1961 const char *cmdline;
1962 QTAILQ_ENTRY(device_config) next;
1964 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1966 static void add_device_config(int type, const char *cmdline)
1968 struct device_config *conf;
1970 conf = g_malloc0(sizeof(*conf));
1971 conf->type = type;
1972 conf->cmdline = cmdline;
1973 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1976 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1978 struct device_config *conf;
1979 int rc;
1981 QTAILQ_FOREACH(conf, &device_configs, next) {
1982 if (conf->type != type)
1983 continue;
1984 rc = func(conf->cmdline);
1985 if (0 != rc)
1986 return rc;
1988 return 0;
1991 static int serial_parse(const char *devname)
1993 static int index = 0;
1994 char label[32];
1996 if (strcmp(devname, "none") == 0)
1997 return 0;
1998 if (index == MAX_SERIAL_PORTS) {
1999 fprintf(stderr, "qemu: too many serial ports\n");
2000 exit(1);
2002 snprintf(label, sizeof(label), "serial%d", index);
2003 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2004 if (!serial_hds[index]) {
2005 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2006 devname, strerror(errno));
2007 return -1;
2009 index++;
2010 return 0;
2013 static int parallel_parse(const char *devname)
2015 static int index = 0;
2016 char label[32];
2018 if (strcmp(devname, "none") == 0)
2019 return 0;
2020 if (index == MAX_PARALLEL_PORTS) {
2021 fprintf(stderr, "qemu: too many parallel ports\n");
2022 exit(1);
2024 snprintf(label, sizeof(label), "parallel%d", index);
2025 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2026 if (!parallel_hds[index]) {
2027 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2028 devname, strerror(errno));
2029 return -1;
2031 index++;
2032 return 0;
2035 static int virtcon_parse(const char *devname)
2037 QemuOptsList *device = qemu_find_opts("device");
2038 static int index = 0;
2039 char label[32];
2040 QemuOpts *bus_opts, *dev_opts;
2042 if (strcmp(devname, "none") == 0)
2043 return 0;
2044 if (index == MAX_VIRTIO_CONSOLES) {
2045 fprintf(stderr, "qemu: too many virtio consoles\n");
2046 exit(1);
2049 bus_opts = qemu_opts_create(device, NULL, 0);
2050 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2052 dev_opts = qemu_opts_create(device, NULL, 0);
2053 qemu_opt_set(dev_opts, "driver", "virtconsole");
2055 snprintf(label, sizeof(label), "virtcon%d", index);
2056 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2057 if (!virtcon_hds[index]) {
2058 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2059 devname, strerror(errno));
2060 return -1;
2062 qemu_opt_set(dev_opts, "chardev", label);
2064 index++;
2065 return 0;
2068 static int debugcon_parse(const char *devname)
2070 QemuOpts *opts;
2072 if (!qemu_chr_new("debugcon", devname, NULL)) {
2073 exit(1);
2075 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2076 if (!opts) {
2077 fprintf(stderr, "qemu: already have a debugcon device\n");
2078 exit(1);
2080 qemu_opt_set(opts, "driver", "isa-debugcon");
2081 qemu_opt_set(opts, "chardev", "debugcon");
2082 return 0;
2085 static QEMUMachine *machine_parse(const char *name)
2087 QEMUMachine *m, *machine = NULL;
2089 if (name) {
2090 machine = find_machine(name);
2092 if (machine) {
2093 return machine;
2095 printf("Supported machines are:\n");
2096 for (m = first_machine; m != NULL; m = m->next) {
2097 if (m->alias) {
2098 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2100 printf("%-10s %s%s\n", m->name, m->desc,
2101 m->is_default ? " (default)" : "");
2103 exit(!name || *name != '?');
2106 static int tcg_init(void)
2108 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2109 return 0;
2112 static struct {
2113 const char *opt_name;
2114 const char *name;
2115 int (*available)(void);
2116 int (*init)(void);
2117 int *allowed;
2118 } accel_list[] = {
2119 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2120 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2121 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2124 static int configure_accelerator(void)
2126 const char *p = NULL;
2127 char buf[10];
2128 int i, ret;
2129 bool accel_initalised = 0;
2130 bool init_failed = 0;
2132 QemuOptsList *list = qemu_find_opts("machine");
2133 if (!QTAILQ_EMPTY(&list->head)) {
2134 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2137 if (p == NULL) {
2138 /* Use the default "accelerator", tcg */
2139 p = "tcg";
2142 while (!accel_initalised && *p != '\0') {
2143 if (*p == ':') {
2144 p++;
2146 p = get_opt_name(buf, sizeof (buf), p, ':');
2147 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2148 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2149 *(accel_list[i].allowed) = 1;
2150 ret = accel_list[i].init();
2151 if (ret < 0) {
2152 init_failed = 1;
2153 if (!accel_list[i].available()) {
2154 printf("%s not supported for this target\n",
2155 accel_list[i].name);
2156 } else {
2157 fprintf(stderr, "failed to initialize %s: %s\n",
2158 accel_list[i].name,
2159 strerror(-ret));
2161 *(accel_list[i].allowed) = 0;
2162 } else {
2163 accel_initalised = 1;
2165 break;
2168 if (i == ARRAY_SIZE(accel_list)) {
2169 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2173 if (!accel_initalised) {
2174 fprintf(stderr, "No accelerator found!\n");
2175 exit(1);
2178 if (init_failed) {
2179 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2182 return !accel_initalised;
2185 void qemu_add_exit_notifier(Notifier *notify)
2187 notifier_list_add(&exit_notifiers, notify);
2190 void qemu_remove_exit_notifier(Notifier *notify)
2192 notifier_list_remove(&exit_notifiers, notify);
2195 static void qemu_run_exit_notifiers(void)
2197 notifier_list_notify(&exit_notifiers, NULL);
2200 void qemu_add_machine_init_done_notifier(Notifier *notify)
2202 notifier_list_add(&machine_init_done_notifiers, notify);
2205 static void qemu_run_machine_init_done_notifiers(void)
2207 notifier_list_notify(&machine_init_done_notifiers, NULL);
2210 static const QEMUOption *lookup_opt(int argc, char **argv,
2211 const char **poptarg, int *poptind)
2213 const QEMUOption *popt;
2214 int optind = *poptind;
2215 char *r = argv[optind];
2216 const char *optarg;
2218 loc_set_cmdline(argv, optind, 1);
2219 optind++;
2220 /* Treat --foo the same as -foo. */
2221 if (r[1] == '-')
2222 r++;
2223 popt = qemu_options;
2224 for(;;) {
2225 if (!popt->name) {
2226 error_report("invalid option");
2227 exit(1);
2229 if (!strcmp(popt->name, r + 1))
2230 break;
2231 popt++;
2233 if (popt->flags & HAS_ARG) {
2234 if (optind >= argc) {
2235 error_report("requires an argument");
2236 exit(1);
2238 optarg = argv[optind++];
2239 loc_set_cmdline(argv, optind - 2, 2);
2240 } else {
2241 optarg = NULL;
2244 *poptarg = optarg;
2245 *poptind = optind;
2247 return popt;
2250 static gpointer malloc_and_trace(gsize n_bytes)
2252 void *ptr = malloc(n_bytes);
2253 trace_g_malloc(n_bytes, ptr);
2254 return ptr;
2257 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2259 void *ptr = realloc(mem, n_bytes);
2260 trace_g_realloc(mem, n_bytes, ptr);
2261 return ptr;
2264 static void free_and_trace(gpointer mem)
2266 trace_g_free(mem);
2267 free(mem);
2270 int main(int argc, char **argv, char **envp)
2272 const char *gdbstub_dev = NULL;
2273 int i;
2274 int snapshot, linux_boot;
2275 const char *icount_option = NULL;
2276 const char *initrd_filename;
2277 const char *kernel_filename, *kernel_cmdline;
2278 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2279 DisplayState *ds;
2280 DisplayChangeListener *dcl;
2281 int cyls, heads, secs, translation;
2282 QemuOpts *hda_opts = NULL, *opts;
2283 QemuOptsList *olist;
2284 int optind;
2285 const char *optarg;
2286 const char *loadvm = NULL;
2287 QEMUMachine *machine;
2288 const char *cpu_model;
2289 const char *pid_file = NULL;
2290 const char *incoming = NULL;
2291 #ifdef CONFIG_VNC
2292 int show_vnc_port = 0;
2293 #endif
2294 int defconfig = 1;
2295 const char *log_mask = NULL;
2296 const char *log_file = NULL;
2297 GMemVTable mem_trace = {
2298 .malloc = malloc_and_trace,
2299 .realloc = realloc_and_trace,
2300 .free = free_and_trace,
2302 const char *trace_events = NULL;
2303 const char *trace_file = NULL;
2305 atexit(qemu_run_exit_notifiers);
2306 error_set_progname(argv[0]);
2308 g_mem_set_vtable(&mem_trace);
2309 g_thread_init(NULL);
2311 runstate_init();
2313 init_clocks();
2315 qemu_cache_utils_init(envp);
2317 QLIST_INIT (&vm_change_state_head);
2318 os_setup_early_signal_handling();
2320 module_call_init(MODULE_INIT_MACHINE);
2321 machine = find_default_machine();
2322 cpu_model = NULL;
2323 initrd_filename = NULL;
2324 ram_size = 0;
2325 snapshot = 0;
2326 kernel_filename = NULL;
2327 kernel_cmdline = "";
2328 cyls = heads = secs = 0;
2329 translation = BIOS_ATA_TRANSLATION_AUTO;
2331 for (i = 0; i < MAX_NODES; i++) {
2332 node_mem[i] = 0;
2333 node_cpumask[i] = 0;
2336 nb_numa_nodes = 0;
2337 nb_nics = 0;
2339 autostart= 1;
2341 /* first pass of option parsing */
2342 optind = 1;
2343 while (optind < argc) {
2344 if (argv[optind][0] != '-') {
2345 /* disk image */
2346 optind++;
2347 continue;
2348 } else {
2349 const QEMUOption *popt;
2351 popt = lookup_opt(argc, argv, &optarg, &optind);
2352 switch (popt->index) {
2353 case QEMU_OPTION_nodefconfig:
2354 defconfig=0;
2355 break;
2360 if (defconfig) {
2361 int ret;
2363 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2364 if (ret < 0 && ret != -ENOENT) {
2365 exit(1);
2368 ret = qemu_read_config_file(arch_config_name);
2369 if (ret < 0 && ret != -ENOENT) {
2370 exit(1);
2373 cpudef_init();
2375 /* second pass of option parsing */
2376 optind = 1;
2377 for(;;) {
2378 if (optind >= argc)
2379 break;
2380 if (argv[optind][0] != '-') {
2381 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2382 } else {
2383 const QEMUOption *popt;
2385 popt = lookup_opt(argc, argv, &optarg, &optind);
2386 if (!(popt->arch_mask & arch_type)) {
2387 printf("Option %s not supported for this target\n", popt->name);
2388 exit(1);
2390 switch(popt->index) {
2391 case QEMU_OPTION_M:
2392 machine = machine_parse(optarg);
2393 break;
2394 case QEMU_OPTION_cpu:
2395 /* hw initialization will check this */
2396 if (*optarg == '?') {
2397 list_cpus(stdout, &fprintf, optarg);
2398 exit(0);
2399 } else {
2400 cpu_model = optarg;
2402 break;
2403 case QEMU_OPTION_initrd:
2404 initrd_filename = optarg;
2405 break;
2406 case QEMU_OPTION_hda:
2408 char buf[256];
2409 if (cyls == 0)
2410 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2411 else
2412 snprintf(buf, sizeof(buf),
2413 "%s,cyls=%d,heads=%d,secs=%d%s",
2414 HD_OPTS , cyls, heads, secs,
2415 translation == BIOS_ATA_TRANSLATION_LBA ?
2416 ",trans=lba" :
2417 translation == BIOS_ATA_TRANSLATION_NONE ?
2418 ",trans=none" : "");
2419 drive_add(IF_DEFAULT, 0, optarg, buf);
2420 break;
2422 case QEMU_OPTION_hdb:
2423 case QEMU_OPTION_hdc:
2424 case QEMU_OPTION_hdd:
2425 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2426 HD_OPTS);
2427 break;
2428 case QEMU_OPTION_drive:
2429 if (drive_def(optarg) == NULL) {
2430 exit(1);
2432 break;
2433 case QEMU_OPTION_set:
2434 if (qemu_set_option(optarg) != 0)
2435 exit(1);
2436 break;
2437 case QEMU_OPTION_global:
2438 if (qemu_global_option(optarg) != 0)
2439 exit(1);
2440 break;
2441 case QEMU_OPTION_mtdblock:
2442 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2443 break;
2444 case QEMU_OPTION_sd:
2445 drive_add(IF_SD, 0, optarg, SD_OPTS);
2446 break;
2447 case QEMU_OPTION_pflash:
2448 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2449 break;
2450 case QEMU_OPTION_snapshot:
2451 snapshot = 1;
2452 break;
2453 case QEMU_OPTION_hdachs:
2455 const char *p;
2456 p = optarg;
2457 cyls = strtol(p, (char **)&p, 0);
2458 if (cyls < 1 || cyls > 16383)
2459 goto chs_fail;
2460 if (*p != ',')
2461 goto chs_fail;
2462 p++;
2463 heads = strtol(p, (char **)&p, 0);
2464 if (heads < 1 || heads > 16)
2465 goto chs_fail;
2466 if (*p != ',')
2467 goto chs_fail;
2468 p++;
2469 secs = strtol(p, (char **)&p, 0);
2470 if (secs < 1 || secs > 63)
2471 goto chs_fail;
2472 if (*p == ',') {
2473 p++;
2474 if (!strcmp(p, "none"))
2475 translation = BIOS_ATA_TRANSLATION_NONE;
2476 else if (!strcmp(p, "lba"))
2477 translation = BIOS_ATA_TRANSLATION_LBA;
2478 else if (!strcmp(p, "auto"))
2479 translation = BIOS_ATA_TRANSLATION_AUTO;
2480 else
2481 goto chs_fail;
2482 } else if (*p != '\0') {
2483 chs_fail:
2484 fprintf(stderr, "qemu: invalid physical CHS format\n");
2485 exit(1);
2487 if (hda_opts != NULL) {
2488 char num[16];
2489 snprintf(num, sizeof(num), "%d", cyls);
2490 qemu_opt_set(hda_opts, "cyls", num);
2491 snprintf(num, sizeof(num), "%d", heads);
2492 qemu_opt_set(hda_opts, "heads", num);
2493 snprintf(num, sizeof(num), "%d", secs);
2494 qemu_opt_set(hda_opts, "secs", num);
2495 if (translation == BIOS_ATA_TRANSLATION_LBA)
2496 qemu_opt_set(hda_opts, "trans", "lba");
2497 if (translation == BIOS_ATA_TRANSLATION_NONE)
2498 qemu_opt_set(hda_opts, "trans", "none");
2501 break;
2502 case QEMU_OPTION_numa:
2503 if (nb_numa_nodes >= MAX_NODES) {
2504 fprintf(stderr, "qemu: too many NUMA nodes\n");
2505 exit(1);
2507 numa_add(optarg);
2508 break;
2509 case QEMU_OPTION_display:
2510 display_type = select_display(optarg);
2511 break;
2512 case QEMU_OPTION_nographic:
2513 display_type = DT_NOGRAPHIC;
2514 break;
2515 case QEMU_OPTION_curses:
2516 #ifdef CONFIG_CURSES
2517 display_type = DT_CURSES;
2518 #else
2519 fprintf(stderr, "Curses support is disabled\n");
2520 exit(1);
2521 #endif
2522 break;
2523 case QEMU_OPTION_portrait:
2524 graphic_rotate = 90;
2525 break;
2526 case QEMU_OPTION_rotate:
2527 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2528 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2529 graphic_rotate != 180 && graphic_rotate != 270) {
2530 fprintf(stderr,
2531 "qemu: only 90, 180, 270 deg rotation is available\n");
2532 exit(1);
2534 break;
2535 case QEMU_OPTION_kernel:
2536 kernel_filename = optarg;
2537 break;
2538 case QEMU_OPTION_append:
2539 kernel_cmdline = optarg;
2540 break;
2541 case QEMU_OPTION_cdrom:
2542 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2543 break;
2544 case QEMU_OPTION_boot:
2546 static const char * const params[] = {
2547 "order", "once", "menu",
2548 "splash", "splash-time", NULL
2550 char buf[sizeof(boot_devices)];
2551 char *standard_boot_devices;
2552 int legacy = 0;
2554 if (!strchr(optarg, '=')) {
2555 legacy = 1;
2556 pstrcpy(buf, sizeof(buf), optarg);
2557 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2558 fprintf(stderr,
2559 "qemu: unknown boot parameter '%s' in '%s'\n",
2560 buf, optarg);
2561 exit(1);
2564 if (legacy ||
2565 get_param_value(buf, sizeof(buf), "order", optarg)) {
2566 validate_bootdevices(buf);
2567 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2569 if (!legacy) {
2570 if (get_param_value(buf, sizeof(buf),
2571 "once", optarg)) {
2572 validate_bootdevices(buf);
2573 standard_boot_devices = g_strdup(boot_devices);
2574 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2575 qemu_register_reset(restore_boot_devices,
2576 standard_boot_devices);
2578 if (get_param_value(buf, sizeof(buf),
2579 "menu", optarg)) {
2580 if (!strcmp(buf, "on")) {
2581 boot_menu = 1;
2582 } else if (!strcmp(buf, "off")) {
2583 boot_menu = 0;
2584 } else {
2585 fprintf(stderr,
2586 "qemu: invalid option value '%s'\n",
2587 buf);
2588 exit(1);
2591 qemu_opts_parse(qemu_find_opts("boot-opts"),
2592 optarg, 0);
2595 break;
2596 case QEMU_OPTION_fda:
2597 case QEMU_OPTION_fdb:
2598 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2599 optarg, FD_OPTS);
2600 break;
2601 case QEMU_OPTION_no_fd_bootchk:
2602 fd_bootchk = 0;
2603 break;
2604 case QEMU_OPTION_netdev:
2605 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2606 exit(1);
2608 break;
2609 case QEMU_OPTION_net:
2610 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2611 exit(1);
2613 break;
2614 #ifdef CONFIG_SLIRP
2615 case QEMU_OPTION_tftp:
2616 legacy_tftp_prefix = optarg;
2617 break;
2618 case QEMU_OPTION_bootp:
2619 legacy_bootp_filename = optarg;
2620 break;
2621 case QEMU_OPTION_redir:
2622 if (net_slirp_redir(optarg) < 0)
2623 exit(1);
2624 break;
2625 #endif
2626 case QEMU_OPTION_bt:
2627 add_device_config(DEV_BT, optarg);
2628 break;
2629 case QEMU_OPTION_audio_help:
2630 if (!(audio_available())) {
2631 printf("Option %s not supported for this target\n", popt->name);
2632 exit(1);
2634 AUD_help ();
2635 exit (0);
2636 break;
2637 case QEMU_OPTION_soundhw:
2638 if (!(audio_available())) {
2639 printf("Option %s not supported for this target\n", popt->name);
2640 exit(1);
2642 select_soundhw (optarg);
2643 break;
2644 case QEMU_OPTION_h:
2645 help(0);
2646 break;
2647 case QEMU_OPTION_version:
2648 version();
2649 exit(0);
2650 break;
2651 case QEMU_OPTION_m: {
2652 int64_t value;
2654 value = strtosz(optarg, NULL);
2655 if (value < 0) {
2656 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2657 exit(1);
2660 if (value != (uint64_t)(ram_addr_t)value) {
2661 fprintf(stderr, "qemu: ram size too large\n");
2662 exit(1);
2664 ram_size = value;
2665 break;
2667 case QEMU_OPTION_mempath:
2668 mem_path = optarg;
2669 break;
2670 #ifdef MAP_POPULATE
2671 case QEMU_OPTION_mem_prealloc:
2672 mem_prealloc = 1;
2673 break;
2674 #endif
2675 case QEMU_OPTION_d:
2676 log_mask = optarg;
2677 break;
2678 case QEMU_OPTION_D:
2679 log_file = optarg;
2680 break;
2681 case QEMU_OPTION_s:
2682 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2683 break;
2684 case QEMU_OPTION_gdb:
2685 gdbstub_dev = optarg;
2686 break;
2687 case QEMU_OPTION_L:
2688 data_dir = optarg;
2689 break;
2690 case QEMU_OPTION_bios:
2691 bios_name = optarg;
2692 break;
2693 case QEMU_OPTION_singlestep:
2694 singlestep = 1;
2695 break;
2696 case QEMU_OPTION_S:
2697 autostart = 0;
2698 break;
2699 case QEMU_OPTION_k:
2700 keyboard_layout = optarg;
2701 break;
2702 case QEMU_OPTION_localtime:
2703 rtc_utc = 0;
2704 break;
2705 case QEMU_OPTION_vga:
2706 select_vgahw (optarg);
2707 break;
2708 case QEMU_OPTION_g:
2710 const char *p;
2711 int w, h, depth;
2712 p = optarg;
2713 w = strtol(p, (char **)&p, 10);
2714 if (w <= 0) {
2715 graphic_error:
2716 fprintf(stderr, "qemu: invalid resolution or depth\n");
2717 exit(1);
2719 if (*p != 'x')
2720 goto graphic_error;
2721 p++;
2722 h = strtol(p, (char **)&p, 10);
2723 if (h <= 0)
2724 goto graphic_error;
2725 if (*p == 'x') {
2726 p++;
2727 depth = strtol(p, (char **)&p, 10);
2728 if (depth != 8 && depth != 15 && depth != 16 &&
2729 depth != 24 && depth != 32)
2730 goto graphic_error;
2731 } else if (*p == '\0') {
2732 depth = graphic_depth;
2733 } else {
2734 goto graphic_error;
2737 graphic_width = w;
2738 graphic_height = h;
2739 graphic_depth = depth;
2741 break;
2742 case QEMU_OPTION_echr:
2744 char *r;
2745 term_escape_char = strtol(optarg, &r, 0);
2746 if (r == optarg)
2747 printf("Bad argument to echr\n");
2748 break;
2750 case QEMU_OPTION_monitor:
2751 monitor_parse(optarg, "readline");
2752 default_monitor = 0;
2753 break;
2754 case QEMU_OPTION_qmp:
2755 monitor_parse(optarg, "control");
2756 default_monitor = 0;
2757 break;
2758 case QEMU_OPTION_mon:
2759 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2760 if (!opts) {
2761 exit(1);
2763 default_monitor = 0;
2764 break;
2765 case QEMU_OPTION_chardev:
2766 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2767 if (!opts) {
2768 exit(1);
2770 break;
2771 case QEMU_OPTION_fsdev:
2772 olist = qemu_find_opts("fsdev");
2773 if (!olist) {
2774 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2775 exit(1);
2777 opts = qemu_opts_parse(olist, optarg, 1);
2778 if (!opts) {
2779 fprintf(stderr, "parse error: %s\n", optarg);
2780 exit(1);
2782 break;
2783 case QEMU_OPTION_virtfs: {
2784 QemuOpts *fsdev;
2785 QemuOpts *device;
2787 olist = qemu_find_opts("virtfs");
2788 if (!olist) {
2789 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2790 exit(1);
2792 opts = qemu_opts_parse(olist, optarg, 1);
2793 if (!opts) {
2794 fprintf(stderr, "parse error: %s\n", optarg);
2795 exit(1);
2798 if (qemu_opt_get(opts, "fstype") == NULL ||
2799 qemu_opt_get(opts, "mount_tag") == NULL ||
2800 qemu_opt_get(opts, "path") == NULL ||
2801 qemu_opt_get(opts, "security_model") == NULL) {
2802 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2803 "security_model=[mapped|passthrough|none],"
2804 "mount_tag=tag.\n");
2805 exit(1);
2808 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2809 qemu_opt_get(opts, "mount_tag"), 1);
2810 if (!fsdev) {
2811 fprintf(stderr, "duplicate fsdev id: %s\n",
2812 qemu_opt_get(opts, "mount_tag"));
2813 exit(1);
2815 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2816 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2817 qemu_opt_set(fsdev, "security_model",
2818 qemu_opt_get(opts, "security_model"));
2820 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2821 qemu_opt_set(device, "driver", "virtio-9p-pci");
2822 qemu_opt_set(device, "fsdev",
2823 qemu_opt_get(opts, "mount_tag"));
2824 qemu_opt_set(device, "mount_tag",
2825 qemu_opt_get(opts, "mount_tag"));
2826 break;
2828 case QEMU_OPTION_serial:
2829 add_device_config(DEV_SERIAL, optarg);
2830 default_serial = 0;
2831 if (strncmp(optarg, "mon:", 4) == 0) {
2832 default_monitor = 0;
2834 break;
2835 case QEMU_OPTION_watchdog:
2836 if (watchdog) {
2837 fprintf(stderr,
2838 "qemu: only one watchdog option may be given\n");
2839 return 1;
2841 watchdog = optarg;
2842 break;
2843 case QEMU_OPTION_watchdog_action:
2844 if (select_watchdog_action(optarg) == -1) {
2845 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2846 exit(1);
2848 break;
2849 case QEMU_OPTION_virtiocon:
2850 add_device_config(DEV_VIRTCON, optarg);
2851 default_virtcon = 0;
2852 if (strncmp(optarg, "mon:", 4) == 0) {
2853 default_monitor = 0;
2855 break;
2856 case QEMU_OPTION_parallel:
2857 add_device_config(DEV_PARALLEL, optarg);
2858 default_parallel = 0;
2859 if (strncmp(optarg, "mon:", 4) == 0) {
2860 default_monitor = 0;
2862 break;
2863 case QEMU_OPTION_debugcon:
2864 add_device_config(DEV_DEBUGCON, optarg);
2865 break;
2866 case QEMU_OPTION_loadvm:
2867 loadvm = optarg;
2868 break;
2869 case QEMU_OPTION_full_screen:
2870 full_screen = 1;
2871 break;
2872 #ifdef CONFIG_SDL
2873 case QEMU_OPTION_no_frame:
2874 no_frame = 1;
2875 break;
2876 case QEMU_OPTION_alt_grab:
2877 alt_grab = 1;
2878 break;
2879 case QEMU_OPTION_ctrl_grab:
2880 ctrl_grab = 1;
2881 break;
2882 case QEMU_OPTION_no_quit:
2883 no_quit = 1;
2884 break;
2885 case QEMU_OPTION_sdl:
2886 display_type = DT_SDL;
2887 break;
2888 #else
2889 case QEMU_OPTION_no_frame:
2890 case QEMU_OPTION_alt_grab:
2891 case QEMU_OPTION_ctrl_grab:
2892 case QEMU_OPTION_no_quit:
2893 case QEMU_OPTION_sdl:
2894 fprintf(stderr, "SDL support is disabled\n");
2895 exit(1);
2896 #endif
2897 case QEMU_OPTION_pidfile:
2898 pid_file = optarg;
2899 break;
2900 case QEMU_OPTION_win2k_hack:
2901 win2k_install_hack = 1;
2902 break;
2903 case QEMU_OPTION_rtc_td_hack:
2904 rtc_td_hack = 1;
2905 break;
2906 case QEMU_OPTION_acpitable:
2907 do_acpitable_option(optarg);
2908 break;
2909 case QEMU_OPTION_smbios:
2910 do_smbios_option(optarg);
2911 break;
2912 case QEMU_OPTION_enable_kvm:
2913 olist = qemu_find_opts("machine");
2914 qemu_opts_reset(olist);
2915 qemu_opts_parse(olist, "accel=kvm", 0);
2916 break;
2917 case QEMU_OPTION_machine:
2918 olist = qemu_find_opts("machine");
2919 qemu_opts_reset(olist);
2920 opts = qemu_opts_parse(olist, optarg, 1);
2921 if (!opts) {
2922 fprintf(stderr, "parse error: %s\n", optarg);
2923 exit(1);
2925 optarg = qemu_opt_get(opts, "type");
2926 if (optarg) {
2927 machine = machine_parse(optarg);
2929 break;
2930 case QEMU_OPTION_usb:
2931 usb_enabled = 1;
2932 break;
2933 case QEMU_OPTION_usbdevice:
2934 usb_enabled = 1;
2935 add_device_config(DEV_USB, optarg);
2936 break;
2937 case QEMU_OPTION_device:
2938 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2939 exit(1);
2941 break;
2942 case QEMU_OPTION_smp:
2943 smp_parse(optarg);
2944 if (smp_cpus < 1) {
2945 fprintf(stderr, "Invalid number of CPUs\n");
2946 exit(1);
2948 if (max_cpus < smp_cpus) {
2949 fprintf(stderr, "maxcpus must be equal to or greater than "
2950 "smp\n");
2951 exit(1);
2953 if (max_cpus > 255) {
2954 fprintf(stderr, "Unsupported number of maxcpus\n");
2955 exit(1);
2957 break;
2958 case QEMU_OPTION_vnc:
2959 #ifdef CONFIG_VNC
2960 display_remote++;
2961 vnc_display = optarg;
2962 #else
2963 fprintf(stderr, "VNC support is disabled\n");
2964 exit(1);
2965 #endif
2966 break;
2967 case QEMU_OPTION_no_acpi:
2968 acpi_enabled = 0;
2969 break;
2970 case QEMU_OPTION_no_hpet:
2971 no_hpet = 1;
2972 break;
2973 case QEMU_OPTION_balloon:
2974 if (balloon_parse(optarg) < 0) {
2975 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2976 exit(1);
2978 break;
2979 case QEMU_OPTION_no_reboot:
2980 no_reboot = 1;
2981 break;
2982 case QEMU_OPTION_no_shutdown:
2983 no_shutdown = 1;
2984 break;
2985 case QEMU_OPTION_show_cursor:
2986 cursor_hide = 0;
2987 break;
2988 case QEMU_OPTION_uuid:
2989 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2990 fprintf(stderr, "Fail to parse UUID string."
2991 " Wrong format.\n");
2992 exit(1);
2994 break;
2995 case QEMU_OPTION_option_rom:
2996 if (nb_option_roms >= MAX_OPTION_ROMS) {
2997 fprintf(stderr, "Too many option ROMs\n");
2998 exit(1);
3000 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3001 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3002 option_rom[nb_option_roms].bootindex =
3003 qemu_opt_get_number(opts, "bootindex", -1);
3004 if (!option_rom[nb_option_roms].name) {
3005 fprintf(stderr, "Option ROM file is not specified\n");
3006 exit(1);
3008 nb_option_roms++;
3009 break;
3010 case QEMU_OPTION_semihosting:
3011 semihosting_enabled = 1;
3012 break;
3013 case QEMU_OPTION_name:
3014 qemu_name = g_strdup(optarg);
3016 char *p = strchr(qemu_name, ',');
3017 if (p != NULL) {
3018 *p++ = 0;
3019 if (strncmp(p, "process=", 8)) {
3020 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3021 exit(1);
3023 p += 8;
3024 os_set_proc_name(p);
3027 break;
3028 case QEMU_OPTION_prom_env:
3029 if (nb_prom_envs >= MAX_PROM_ENVS) {
3030 fprintf(stderr, "Too many prom variables\n");
3031 exit(1);
3033 prom_envs[nb_prom_envs] = optarg;
3034 nb_prom_envs++;
3035 break;
3036 case QEMU_OPTION_old_param:
3037 old_param = 1;
3038 break;
3039 case QEMU_OPTION_clock:
3040 configure_alarms(optarg);
3041 break;
3042 case QEMU_OPTION_startdate:
3043 configure_rtc_date_offset(optarg, 1);
3044 break;
3045 case QEMU_OPTION_rtc:
3046 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3047 if (!opts) {
3048 exit(1);
3050 configure_rtc(opts);
3051 break;
3052 case QEMU_OPTION_tb_size:
3053 tcg_tb_size = strtol(optarg, NULL, 0);
3054 if (tcg_tb_size < 0) {
3055 tcg_tb_size = 0;
3057 break;
3058 case QEMU_OPTION_icount:
3059 icount_option = optarg;
3060 break;
3061 case QEMU_OPTION_incoming:
3062 incoming = optarg;
3063 break;
3064 case QEMU_OPTION_nodefaults:
3065 default_serial = 0;
3066 default_parallel = 0;
3067 default_virtcon = 0;
3068 default_monitor = 0;
3069 default_vga = 0;
3070 default_net = 0;
3071 default_floppy = 0;
3072 default_cdrom = 0;
3073 default_sdcard = 0;
3074 break;
3075 case QEMU_OPTION_xen_domid:
3076 if (!(xen_available())) {
3077 printf("Option %s not supported for this target\n", popt->name);
3078 exit(1);
3080 xen_domid = atoi(optarg);
3081 break;
3082 case QEMU_OPTION_xen_create:
3083 if (!(xen_available())) {
3084 printf("Option %s not supported for this target\n", popt->name);
3085 exit(1);
3087 xen_mode = XEN_CREATE;
3088 break;
3089 case QEMU_OPTION_xen_attach:
3090 if (!(xen_available())) {
3091 printf("Option %s not supported for this target\n", popt->name);
3092 exit(1);
3094 xen_mode = XEN_ATTACH;
3095 break;
3096 case QEMU_OPTION_trace:
3098 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3099 if (!opts) {
3100 exit(1);
3102 trace_events = qemu_opt_get(opts, "events");
3103 trace_file = qemu_opt_get(opts, "file");
3104 break;
3106 case QEMU_OPTION_readconfig:
3108 int ret = qemu_read_config_file(optarg);
3109 if (ret < 0) {
3110 fprintf(stderr, "read config %s: %s\n", optarg,
3111 strerror(-ret));
3112 exit(1);
3114 break;
3116 case QEMU_OPTION_spice:
3117 olist = qemu_find_opts("spice");
3118 if (!olist) {
3119 fprintf(stderr, "spice is not supported by this qemu build.\n");
3120 exit(1);
3122 opts = qemu_opts_parse(olist, optarg, 0);
3123 if (!opts) {
3124 fprintf(stderr, "parse error: %s\n", optarg);
3125 exit(1);
3127 break;
3128 case QEMU_OPTION_writeconfig:
3130 FILE *fp;
3131 if (strcmp(optarg, "-") == 0) {
3132 fp = stdout;
3133 } else {
3134 fp = fopen(optarg, "w");
3135 if (fp == NULL) {
3136 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3137 exit(1);
3140 qemu_config_write(fp);
3141 fclose(fp);
3142 break;
3144 default:
3145 os_parse_cmd_args(popt->index, optarg);
3149 loc_set_none();
3151 /* Open the logfile at this point, if necessary. We can't open the logfile
3152 * when encountering either of the logging options (-d or -D) because the
3153 * other one may be encountered later on the command line, changing the
3154 * location or level of logging.
3156 if (log_mask) {
3157 if (log_file) {
3158 set_cpu_log_filename(log_file);
3160 set_cpu_log(log_mask);
3163 if (!trace_backend_init(trace_events, trace_file)) {
3164 exit(1);
3167 /* If no data_dir is specified then try to find it relative to the
3168 executable path. */
3169 if (!data_dir) {
3170 data_dir = os_find_datadir(argv[0]);
3172 /* If all else fails use the install path specified when building. */
3173 if (!data_dir) {
3174 data_dir = CONFIG_QEMU_DATADIR;
3178 * Default to max_cpus = smp_cpus, in case the user doesn't
3179 * specify a max_cpus value.
3181 if (!max_cpus)
3182 max_cpus = smp_cpus;
3184 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3185 if (smp_cpus > machine->max_cpus) {
3186 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3187 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3188 machine->max_cpus);
3189 exit(1);
3193 * Get the default machine options from the machine if it is not already
3194 * specified either by the configuration file or by the command line.
3196 if (machine->default_machine_opts) {
3197 QemuOptsList *list = qemu_find_opts("machine");
3198 const char *p = NULL;
3200 if (!QTAILQ_EMPTY(&list->head)) {
3201 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3203 if (p == NULL) {
3204 qemu_opts_reset(list);
3205 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3206 if (!opts) {
3207 fprintf(stderr, "parse error for machine %s: %s\n",
3208 machine->name, machine->default_machine_opts);
3209 exit(1);
3214 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3215 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3217 if (machine->no_serial) {
3218 default_serial = 0;
3220 if (machine->no_parallel) {
3221 default_parallel = 0;
3223 if (!machine->use_virtcon) {
3224 default_virtcon = 0;
3226 if (machine->no_vga) {
3227 default_vga = 0;
3229 if (machine->no_floppy) {
3230 default_floppy = 0;
3232 if (machine->no_cdrom) {
3233 default_cdrom = 0;
3235 if (machine->no_sdcard) {
3236 default_sdcard = 0;
3239 if (display_type == DT_NOGRAPHIC) {
3240 if (default_parallel)
3241 add_device_config(DEV_PARALLEL, "null");
3242 if (default_serial && default_monitor) {
3243 add_device_config(DEV_SERIAL, "mon:stdio");
3244 } else if (default_virtcon && default_monitor) {
3245 add_device_config(DEV_VIRTCON, "mon:stdio");
3246 } else {
3247 if (default_serial)
3248 add_device_config(DEV_SERIAL, "stdio");
3249 if (default_virtcon)
3250 add_device_config(DEV_VIRTCON, "stdio");
3251 if (default_monitor)
3252 monitor_parse("stdio", "readline");
3254 } else {
3255 if (default_serial)
3256 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3257 if (default_parallel)
3258 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3259 if (default_monitor)
3260 monitor_parse("vc:80Cx24C", "readline");
3261 if (default_virtcon)
3262 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3264 if (default_vga)
3265 vga_interface_type = VGA_CIRRUS;
3267 socket_init();
3269 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3270 exit(1);
3271 #ifdef CONFIG_VIRTFS
3272 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3273 exit(1);
3275 #endif
3277 os_daemonize();
3279 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3280 os_pidfile_error();
3281 exit(1);
3284 /* init the memory */
3285 if (ram_size == 0) {
3286 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3289 configure_accelerator();
3291 if (qemu_init_main_loop()) {
3292 fprintf(stderr, "qemu_init_main_loop failed\n");
3293 exit(1);
3295 linux_boot = (kernel_filename != NULL);
3297 if (!linux_boot && *kernel_cmdline != '\0') {
3298 fprintf(stderr, "-append only allowed with -kernel option\n");
3299 exit(1);
3302 if (!linux_boot && initrd_filename != NULL) {
3303 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3304 exit(1);
3307 os_set_line_buffering();
3309 if (init_timer_alarm() < 0) {
3310 fprintf(stderr, "could not initialize alarm timer\n");
3311 exit(1);
3313 configure_icount(icount_option);
3315 if (net_init_clients() < 0) {
3316 exit(1);
3319 /* init the bluetooth world */
3320 if (foreach_device_config(DEV_BT, bt_parse))
3321 exit(1);
3323 if (!xen_enabled()) {
3324 /* On 32-bit hosts, QEMU is limited by virtual address space */
3325 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3326 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3327 exit(1);
3331 cpu_exec_init_all();
3333 bdrv_init_with_whitelist();
3335 blk_mig_init();
3337 /* open the virtual block devices */
3338 if (snapshot)
3339 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3340 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3341 exit(1);
3343 default_drive(default_cdrom, snapshot, machine->use_scsi,
3344 IF_DEFAULT, 2, CDROM_OPTS);
3345 default_drive(default_floppy, snapshot, machine->use_scsi,
3346 IF_FLOPPY, 0, FD_OPTS);
3347 default_drive(default_sdcard, snapshot, machine->use_scsi,
3348 IF_SD, 0, SD_OPTS);
3350 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3351 ram_load, NULL);
3353 if (nb_numa_nodes > 0) {
3354 int i;
3356 if (nb_numa_nodes > MAX_NODES) {
3357 nb_numa_nodes = MAX_NODES;
3360 /* If no memory size if given for any node, assume the default case
3361 * and distribute the available memory equally across all nodes
3363 for (i = 0; i < nb_numa_nodes; i++) {
3364 if (node_mem[i] != 0)
3365 break;
3367 if (i == nb_numa_nodes) {
3368 uint64_t usedmem = 0;
3370 /* On Linux, the each node's border has to be 8MB aligned,
3371 * the final node gets the rest.
3373 for (i = 0; i < nb_numa_nodes - 1; i++) {
3374 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3375 usedmem += node_mem[i];
3377 node_mem[i] = ram_size - usedmem;
3380 for (i = 0; i < nb_numa_nodes; i++) {
3381 if (node_cpumask[i] != 0)
3382 break;
3384 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3385 * must cope with this anyway, because there are BIOSes out there in
3386 * real machines which also use this scheme.
3388 if (i == nb_numa_nodes) {
3389 for (i = 0; i < smp_cpus; i++) {
3390 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3395 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3396 exit(1);
3399 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3400 exit(1);
3401 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3402 exit(1);
3403 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3404 exit(1);
3405 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3406 exit(1);
3408 module_call_init(MODULE_INIT_DEVICE);
3410 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3411 exit(0);
3413 if (watchdog) {
3414 i = select_watchdog(watchdog);
3415 if (i > 0)
3416 exit (i == 1 ? 1 : 0);
3419 if (machine->compat_props) {
3420 qdev_prop_register_global_list(machine->compat_props);
3422 qemu_add_globals();
3424 machine->init(ram_size, boot_devices,
3425 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3427 cpu_synchronize_all_post_init();
3429 set_numa_modes();
3431 current_machine = machine;
3433 /* init USB devices */
3434 if (usb_enabled) {
3435 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3436 exit(1);
3439 /* init generic devices */
3440 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3441 exit(1);
3443 net_check_clients();
3445 /* just use the first displaystate for the moment */
3446 ds = get_displaystate();
3448 if (using_spice)
3449 display_remote++;
3450 if (display_type == DT_DEFAULT && !display_remote) {
3451 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3452 display_type = DT_SDL;
3453 #elif defined(CONFIG_VNC)
3454 vnc_display = "localhost:0,to=99";
3455 show_vnc_port = 1;
3456 #else
3457 display_type = DT_NONE;
3458 #endif
3462 /* init local displays */
3463 switch (display_type) {
3464 case DT_NOGRAPHIC:
3465 break;
3466 #if defined(CONFIG_CURSES)
3467 case DT_CURSES:
3468 curses_display_init(ds, full_screen);
3469 break;
3470 #endif
3471 #if defined(CONFIG_SDL)
3472 case DT_SDL:
3473 sdl_display_init(ds, full_screen, no_frame);
3474 break;
3475 #elif defined(CONFIG_COCOA)
3476 case DT_SDL:
3477 cocoa_display_init(ds, full_screen);
3478 break;
3479 #endif
3480 default:
3481 break;
3484 /* must be after terminal init, SDL library changes signal handlers */
3485 os_setup_signal_handling();
3487 #ifdef CONFIG_VNC
3488 /* init remote displays */
3489 if (vnc_display) {
3490 vnc_display_init(ds);
3491 if (vnc_display_open(ds, vnc_display) < 0)
3492 exit(1);
3494 if (show_vnc_port) {
3495 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3498 #endif
3499 #ifdef CONFIG_SPICE
3500 if (using_spice && !qxl_enabled) {
3501 qemu_spice_display_init(ds);
3503 #endif
3505 /* display setup */
3506 dpy_resize(ds);
3507 dcl = ds->listeners;
3508 while (dcl != NULL) {
3509 if (dcl->dpy_refresh != NULL) {
3510 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3511 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3512 break;
3514 dcl = dcl->next;
3516 if (ds->gui_timer == NULL) {
3517 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3518 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3520 text_consoles_set_display(ds);
3522 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3523 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3524 gdbstub_dev);
3525 exit(1);
3528 qdev_machine_creation_done();
3530 if (rom_load_all() != 0) {
3531 fprintf(stderr, "rom loading failed\n");
3532 exit(1);
3535 /* TODO: once all bus devices are qdevified, this should be done
3536 * when bus is created by qdev.c */
3537 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3538 qemu_run_machine_init_done_notifiers();
3540 qemu_system_reset(VMRESET_SILENT);
3541 if (loadvm) {
3542 if (load_vmstate(loadvm) < 0) {
3543 autostart = 0;
3547 if (incoming) {
3548 runstate_set(RUN_STATE_INMIGRATE);
3549 int ret = qemu_start_incoming_migration(incoming);
3550 if (ret < 0) {
3551 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3552 incoming, ret);
3553 exit(ret);
3555 } else if (autostart) {
3556 vm_start();
3559 os_setup_post();
3561 main_loop();
3562 quit_timers();
3563 net_cleanup();
3564 res_free();
3566 return 0;