omap_intc: Qdevify
[qemu.git] / vl.c
blobbd4a5ce8a2fdeec593e916eb5351be08087b5bd3
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
51 #ifdef CONFIG_BSD
52 #include <sys/stat.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <libutil.h>
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <pty.h>
62 #include <malloc.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
66 #endif
67 #ifdef __sun__
68 #include <sys/stat.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
77 #include <net/if.h>
78 #include <syslog.h>
79 #include <stropts.h>
80 #endif
81 #endif
82 #endif
84 #if defined(__OpenBSD__)
85 #include <util.h>
86 #endif
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
90 #endif
92 #ifdef _WIN32
93 #include <windows.h>
94 #endif
96 #ifdef CONFIG_SDL
97 #if defined(__APPLE__) || defined(main)
98 #include <SDL.h>
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
104 #undef main
105 #define main qemu_main
106 #endif
107 #endif /* CONFIG_SDL */
109 #ifdef CONFIG_COCOA
110 #undef main
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
114 #include <glib.h>
116 #include "hw/hw.h"
117 #include "hw/boards.h"
118 #include "hw/usb.h"
119 #include "hw/pcmcia.h"
120 #include "hw/pc.h"
121 #include "hw/isa.h"
122 #include "hw/baum.h"
123 #include "hw/bt.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
126 #include "hw/xen.h"
127 #include "hw/qdev.h"
128 #include "hw/loader.h"
129 #include "bt-host.h"
130 #include "net.h"
131 #include "net/slirp.h"
132 #include "monitor.h"
133 #include "console.h"
134 #include "sysemu.h"
135 #include "gdbstub.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
139 #include "block.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
142 #include "dma.h"
143 #include "audio/audio.h"
144 #include "migration.h"
145 #include "kvm.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #ifdef CONFIG_VIRTFS
151 #include "fsdev/qemu-fsdev.h"
152 #endif
154 #include "disas.h"
156 #include "qemu_socket.h"
158 #include "slirp/libslirp.h"
160 #include "trace.h"
161 #include "trace/control.h"
162 #include "qemu-queue.h"
163 #include "cpus.h"
164 #include "arch_init.h"
166 #include "ui/qemu-spice.h"
168 //#define DEBUG_NET
169 //#define DEBUG_SLIRP
171 #define DEFAULT_RAM_SIZE 128
173 #define MAX_VIRTIO_CONSOLES 1
175 static const char *data_dir;
176 const char *bios_name = NULL;
177 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
178 DisplayType display_type = DT_DEFAULT;
179 int display_remote = 0;
180 const char* keyboard_layout = NULL;
181 ram_addr_t ram_size;
182 const char *mem_path = NULL;
183 #ifdef MAP_POPULATE
184 int mem_prealloc = 0; /* force preallocation of physical target memory */
185 #endif
186 int nb_nics;
187 NICInfo nd_table[MAX_NICS];
188 int autostart;
189 static int rtc_utc = 1;
190 static int rtc_date_offset = -1; /* -1 means no change */
191 QEMUClock *rtc_clock;
192 int vga_interface_type = VGA_NONE;
193 static int full_screen = 0;
194 #ifdef CONFIG_SDL
195 static int no_frame = 0;
196 #endif
197 int no_quit = 0;
198 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
199 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
200 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
201 int win2k_install_hack = 0;
202 int rtc_td_hack = 0;
203 int usb_enabled = 0;
204 int singlestep = 0;
205 int smp_cpus = 1;
206 int max_cpus = 0;
207 int smp_cores = 1;
208 int smp_threads = 1;
209 #ifdef CONFIG_VNC
210 const char *vnc_display;
211 #endif
212 int acpi_enabled = 1;
213 int no_hpet = 0;
214 int fd_bootchk = 1;
215 int no_reboot = 0;
216 int no_shutdown = 0;
217 int cursor_hide = 1;
218 int graphic_rotate = 0;
219 uint8_t irq0override = 1;
220 const char *watchdog;
221 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
222 int nb_option_roms;
223 int semihosting_enabled = 0;
224 int old_param = 0;
225 const char *qemu_name;
226 int alt_grab = 0;
227 int ctrl_grab = 0;
228 unsigned int nb_prom_envs = 0;
229 const char *prom_envs[MAX_PROM_ENVS];
230 int boot_menu;
231 uint8_t *boot_splash_filedata;
232 int boot_splash_filedata_size;
233 uint8_t qemu_extra_params_fw[2];
235 typedef struct FWBootEntry FWBootEntry;
237 struct FWBootEntry {
238 QTAILQ_ENTRY(FWBootEntry) link;
239 int32_t bootindex;
240 DeviceState *dev;
241 char *suffix;
244 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
246 int nb_numa_nodes;
247 uint64_t node_mem[MAX_NODES];
248 uint64_t node_cpumask[MAX_NODES];
250 static QEMUTimer *nographic_timer;
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 = RSTATE_NO_STATE;
328 typedef struct {
329 RunState from;
330 RunState to;
331 } RunStateTransition;
333 static const RunStateTransition runstate_transitions_def[] = {
334 /* from -> to */
335 { RSTATE_NO_STATE, RSTATE_RUNNING },
336 { RSTATE_NO_STATE, RSTATE_IN_MIGRATE },
337 { RSTATE_NO_STATE, RSTATE_PRE_LAUNCH },
339 { RSTATE_DEBUG, RSTATE_RUNNING },
341 { RSTATE_IN_MIGRATE, RSTATE_RUNNING },
342 { RSTATE_IN_MIGRATE, RSTATE_PRE_LAUNCH },
344 { RSTATE_PANICKED, RSTATE_PAUSED },
346 { RSTATE_IO_ERROR, RSTATE_RUNNING },
348 { RSTATE_PAUSED, RSTATE_RUNNING },
350 { RSTATE_POST_MIGRATE, RSTATE_RUNNING },
352 { RSTATE_PRE_LAUNCH, RSTATE_RUNNING },
353 { RSTATE_PRE_LAUNCH, RSTATE_POST_MIGRATE },
355 { RSTATE_PRE_MIGRATE, RSTATE_RUNNING },
356 { RSTATE_PRE_MIGRATE, RSTATE_POST_MIGRATE },
358 { RSTATE_RESTORE, RSTATE_RUNNING },
360 { RSTATE_RUNNING, RSTATE_DEBUG },
361 { RSTATE_RUNNING, RSTATE_PANICKED },
362 { RSTATE_RUNNING, RSTATE_IO_ERROR },
363 { RSTATE_RUNNING, RSTATE_PAUSED },
364 { RSTATE_RUNNING, RSTATE_PRE_MIGRATE },
365 { RSTATE_RUNNING, RSTATE_RESTORE },
366 { RSTATE_RUNNING, RSTATE_SAVEVM },
367 { RSTATE_RUNNING, RSTATE_SHUTDOWN },
368 { RSTATE_RUNNING, RSTATE_WATCHDOG },
370 { RSTATE_SAVEVM, RSTATE_RUNNING },
372 { RSTATE_SHUTDOWN, RSTATE_PAUSED },
374 { RSTATE_WATCHDOG, RSTATE_RUNNING },
376 { RSTATE_MAX, RSTATE_MAX },
379 static bool runstate_valid_transitions[RSTATE_MAX][RSTATE_MAX];
381 static const char *const runstate_name_tbl[RSTATE_MAX] = {
382 [RSTATE_DEBUG] = "debug",
383 [RSTATE_IN_MIGRATE] = "incoming-migration",
384 [RSTATE_PANICKED] = "internal-error",
385 [RSTATE_IO_ERROR] = "io-error",
386 [RSTATE_PAUSED] = "paused",
387 [RSTATE_POST_MIGRATE] = "post-migrate",
388 [RSTATE_PRE_LAUNCH] = "prelaunch",
389 [RSTATE_PRE_MIGRATE] = "finish-migrate",
390 [RSTATE_RESTORE] = "restore-vm",
391 [RSTATE_RUNNING] = "running",
392 [RSTATE_SAVEVM] = "save-vm",
393 [RSTATE_SHUTDOWN] = "shutdown",
394 [RSTATE_WATCHDOG] = "watchdog",
397 bool runstate_check(RunState state)
399 return current_run_state == state;
402 void runstate_init(void)
404 const RunStateTransition *p;
406 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
408 for (p = &runstate_transitions_def[0]; p->from != RSTATE_MAX; p++) {
409 runstate_valid_transitions[p->from][p->to] = true;
413 /* This function will abort() on invalid state transitions */
414 void runstate_set(RunState new_state)
416 if (new_state >= RSTATE_MAX ||
417 !runstate_valid_transitions[current_run_state][new_state]) {
418 fprintf(stderr, "invalid runstate transition\n");
419 abort();
422 current_run_state = new_state;
425 const char *runstate_as_string(void)
427 assert(current_run_state > RSTATE_NO_STATE &&
428 current_run_state < RSTATE_MAX);
429 return runstate_name_tbl[current_run_state];
432 int runstate_is_running(void)
434 return runstate_check(RSTATE_RUNNING);
437 /***********************************************************/
438 /* real time host monotonic timer */
440 /***********************************************************/
441 /* host time/date access */
442 void qemu_get_timedate(struct tm *tm, int offset)
444 time_t ti;
445 struct tm *ret;
447 time(&ti);
448 ti += offset;
449 if (rtc_date_offset == -1) {
450 if (rtc_utc)
451 ret = gmtime(&ti);
452 else
453 ret = localtime(&ti);
454 } else {
455 ti -= rtc_date_offset;
456 ret = gmtime(&ti);
459 memcpy(tm, ret, sizeof(struct tm));
462 int qemu_timedate_diff(struct tm *tm)
464 time_t seconds;
466 if (rtc_date_offset == -1)
467 if (rtc_utc)
468 seconds = mktimegm(tm);
469 else
470 seconds = mktime(tm);
471 else
472 seconds = mktimegm(tm) + rtc_date_offset;
474 return seconds - time(NULL);
477 void rtc_change_mon_event(struct tm *tm)
479 QObject *data;
481 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
482 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
483 qobject_decref(data);
486 static void configure_rtc_date_offset(const char *startdate, int legacy)
488 time_t rtc_start_date;
489 struct tm tm;
491 if (!strcmp(startdate, "now") && legacy) {
492 rtc_date_offset = -1;
493 } else {
494 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
495 &tm.tm_year,
496 &tm.tm_mon,
497 &tm.tm_mday,
498 &tm.tm_hour,
499 &tm.tm_min,
500 &tm.tm_sec) == 6) {
501 /* OK */
502 } else if (sscanf(startdate, "%d-%d-%d",
503 &tm.tm_year,
504 &tm.tm_mon,
505 &tm.tm_mday) == 3) {
506 tm.tm_hour = 0;
507 tm.tm_min = 0;
508 tm.tm_sec = 0;
509 } else {
510 goto date_fail;
512 tm.tm_year -= 1900;
513 tm.tm_mon--;
514 rtc_start_date = mktimegm(&tm);
515 if (rtc_start_date == -1) {
516 date_fail:
517 fprintf(stderr, "Invalid date format. Valid formats are:\n"
518 "'2006-06-17T16:01:21' or '2006-06-17'\n");
519 exit(1);
521 rtc_date_offset = time(NULL) - rtc_start_date;
525 static void configure_rtc(QemuOpts *opts)
527 const char *value;
529 value = qemu_opt_get(opts, "base");
530 if (value) {
531 if (!strcmp(value, "utc")) {
532 rtc_utc = 1;
533 } else if (!strcmp(value, "localtime")) {
534 rtc_utc = 0;
535 } else {
536 configure_rtc_date_offset(value, 0);
539 value = qemu_opt_get(opts, "clock");
540 if (value) {
541 if (!strcmp(value, "host")) {
542 rtc_clock = host_clock;
543 } else if (!strcmp(value, "vm")) {
544 rtc_clock = vm_clock;
545 } else {
546 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
547 exit(1);
550 value = qemu_opt_get(opts, "driftfix");
551 if (value) {
552 if (!strcmp(value, "slew")) {
553 rtc_td_hack = 1;
554 } else if (!strcmp(value, "none")) {
555 rtc_td_hack = 0;
556 } else {
557 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
558 exit(1);
563 /***********************************************************/
564 /* Bluetooth support */
565 static int nb_hcis;
566 static int cur_hci;
567 static struct HCIInfo *hci_table[MAX_NICS];
569 static struct bt_vlan_s {
570 struct bt_scatternet_s net;
571 int id;
572 struct bt_vlan_s *next;
573 } *first_bt_vlan;
575 /* find or alloc a new bluetooth "VLAN" */
576 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
578 struct bt_vlan_s **pvlan, *vlan;
579 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
580 if (vlan->id == id)
581 return &vlan->net;
583 vlan = g_malloc0(sizeof(struct bt_vlan_s));
584 vlan->id = id;
585 pvlan = &first_bt_vlan;
586 while (*pvlan != NULL)
587 pvlan = &(*pvlan)->next;
588 *pvlan = vlan;
589 return &vlan->net;
592 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
596 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
598 return -ENOTSUP;
601 static struct HCIInfo null_hci = {
602 .cmd_send = null_hci_send,
603 .sco_send = null_hci_send,
604 .acl_send = null_hci_send,
605 .bdaddr_set = null_hci_addr_set,
608 struct HCIInfo *qemu_next_hci(void)
610 if (cur_hci == nb_hcis)
611 return &null_hci;
613 return hci_table[cur_hci++];
616 static struct HCIInfo *hci_init(const char *str)
618 char *endp;
619 struct bt_scatternet_s *vlan = 0;
621 if (!strcmp(str, "null"))
622 /* null */
623 return &null_hci;
624 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
625 /* host[:hciN] */
626 return bt_host_hci(str[4] ? str + 5 : "hci0");
627 else if (!strncmp(str, "hci", 3)) {
628 /* hci[,vlan=n] */
629 if (str[3]) {
630 if (!strncmp(str + 3, ",vlan=", 6)) {
631 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
632 if (*endp)
633 vlan = 0;
635 } else
636 vlan = qemu_find_bt_vlan(0);
637 if (vlan)
638 return bt_new_hci(vlan);
641 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
643 return 0;
646 static int bt_hci_parse(const char *str)
648 struct HCIInfo *hci;
649 bdaddr_t bdaddr;
651 if (nb_hcis >= MAX_NICS) {
652 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
653 return -1;
656 hci = hci_init(str);
657 if (!hci)
658 return -1;
660 bdaddr.b[0] = 0x52;
661 bdaddr.b[1] = 0x54;
662 bdaddr.b[2] = 0x00;
663 bdaddr.b[3] = 0x12;
664 bdaddr.b[4] = 0x34;
665 bdaddr.b[5] = 0x56 + nb_hcis;
666 hci->bdaddr_set(hci, bdaddr.b);
668 hci_table[nb_hcis++] = hci;
670 return 0;
673 static void bt_vhci_add(int vlan_id)
675 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
677 if (!vlan->slave)
678 fprintf(stderr, "qemu: warning: adding a VHCI to "
679 "an empty scatternet %i\n", vlan_id);
681 bt_vhci_init(bt_new_hci(vlan));
684 static struct bt_device_s *bt_device_add(const char *opt)
686 struct bt_scatternet_s *vlan;
687 int vlan_id = 0;
688 char *endp = strstr(opt, ",vlan=");
689 int len = (endp ? endp - opt : strlen(opt)) + 1;
690 char devname[10];
692 pstrcpy(devname, MIN(sizeof(devname), len), opt);
694 if (endp) {
695 vlan_id = strtol(endp + 6, &endp, 0);
696 if (*endp) {
697 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
698 return 0;
702 vlan = qemu_find_bt_vlan(vlan_id);
704 if (!vlan->slave)
705 fprintf(stderr, "qemu: warning: adding a slave device to "
706 "an empty scatternet %i\n", vlan_id);
708 if (!strcmp(devname, "keyboard"))
709 return bt_keyboard_init(vlan);
711 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
712 return 0;
715 static int bt_parse(const char *opt)
717 const char *endp, *p;
718 int vlan;
720 if (strstart(opt, "hci", &endp)) {
721 if (!*endp || *endp == ',') {
722 if (*endp)
723 if (!strstart(endp, ",vlan=", 0))
724 opt = endp + 1;
726 return bt_hci_parse(opt);
728 } else if (strstart(opt, "vhci", &endp)) {
729 if (!*endp || *endp == ',') {
730 if (*endp) {
731 if (strstart(endp, ",vlan=", &p)) {
732 vlan = strtol(p, (char **) &endp, 0);
733 if (*endp) {
734 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
735 return 1;
737 } else {
738 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
739 return 1;
741 } else
742 vlan = 0;
744 bt_vhci_add(vlan);
745 return 0;
747 } else if (strstart(opt, "device:", &endp))
748 return !bt_device_add(endp);
750 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
751 return 1;
754 /***********************************************************/
755 /* QEMU Block devices */
757 #define HD_OPTS "media=disk"
758 #define CDROM_OPTS "media=cdrom"
759 #define FD_OPTS ""
760 #define PFLASH_OPTS ""
761 #define MTD_OPTS ""
762 #define SD_OPTS ""
764 static int drive_init_func(QemuOpts *opts, void *opaque)
766 int *use_scsi = opaque;
768 return drive_init(opts, *use_scsi) == NULL;
771 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
773 if (NULL == qemu_opt_get(opts, "snapshot")) {
774 qemu_opt_set(opts, "snapshot", "on");
776 return 0;
779 static void default_drive(int enable, int snapshot, int use_scsi,
780 BlockInterfaceType type, int index,
781 const char *optstr)
783 QemuOpts *opts;
785 if (type == IF_DEFAULT) {
786 type = use_scsi ? IF_SCSI : IF_IDE;
789 if (!enable || drive_get_by_index(type, index)) {
790 return;
793 opts = drive_add(type, index, NULL, optstr);
794 if (snapshot) {
795 drive_enable_snapshot(opts, NULL);
797 if (!drive_init(opts, use_scsi)) {
798 exit(1);
802 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
804 boot_set_handler = func;
805 boot_set_opaque = opaque;
808 int qemu_boot_set(const char *boot_devices)
810 if (!boot_set_handler) {
811 return -EINVAL;
813 return boot_set_handler(boot_set_opaque, boot_devices);
816 static void validate_bootdevices(char *devices)
818 /* We just do some generic consistency checks */
819 const char *p;
820 int bitmap = 0;
822 for (p = devices; *p != '\0'; p++) {
823 /* Allowed boot devices are:
824 * a-b: floppy disk drives
825 * c-f: IDE disk drives
826 * g-m: machine implementation dependant drives
827 * n-p: network devices
828 * It's up to each machine implementation to check if the given boot
829 * devices match the actual hardware implementation and firmware
830 * features.
832 if (*p < 'a' || *p > 'p') {
833 fprintf(stderr, "Invalid boot device '%c'\n", *p);
834 exit(1);
836 if (bitmap & (1 << (*p - 'a'))) {
837 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
838 exit(1);
840 bitmap |= 1 << (*p - 'a');
844 static void restore_boot_devices(void *opaque)
846 char *standard_boot_devices = opaque;
847 static int first = 1;
849 /* Restore boot order and remove ourselves after the first boot */
850 if (first) {
851 first = 0;
852 return;
855 qemu_boot_set(standard_boot_devices);
857 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
858 g_free(standard_boot_devices);
861 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
862 const char *suffix)
864 FWBootEntry *node, *i;
866 if (bootindex < 0) {
867 return;
870 assert(dev != NULL || suffix != NULL);
872 node = g_malloc0(sizeof(FWBootEntry));
873 node->bootindex = bootindex;
874 node->suffix = suffix ? g_strdup(suffix) : NULL;
875 node->dev = dev;
877 QTAILQ_FOREACH(i, &fw_boot_order, link) {
878 if (i->bootindex == bootindex) {
879 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
880 exit(1);
881 } else if (i->bootindex < bootindex) {
882 continue;
884 QTAILQ_INSERT_BEFORE(i, node, link);
885 return;
887 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
891 * This function returns null terminated string that consist of new line
892 * separated device paths.
894 * memory pointed by "size" is assigned total length of the array in bytes
897 char *get_boot_devices_list(uint32_t *size)
899 FWBootEntry *i;
900 uint32_t total = 0;
901 char *list = NULL;
903 QTAILQ_FOREACH(i, &fw_boot_order, link) {
904 char *devpath = NULL, *bootpath;
905 int len;
907 if (i->dev) {
908 devpath = qdev_get_fw_dev_path(i->dev);
909 assert(devpath);
912 if (i->suffix && devpath) {
913 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
915 bootpath = g_malloc(bootpathlen);
916 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
917 g_free(devpath);
918 } else if (devpath) {
919 bootpath = devpath;
920 } else {
921 bootpath = g_strdup(i->suffix);
922 assert(bootpath);
925 if (total) {
926 list[total-1] = '\n';
928 len = strlen(bootpath) + 1;
929 list = g_realloc(list, total + len);
930 memcpy(&list[total], bootpath, len);
931 total += len;
932 g_free(bootpath);
935 *size = total;
937 return list;
940 static void numa_add(const char *optarg)
942 char option[128];
943 char *endptr;
944 unsigned long long value, endvalue;
945 int nodenr;
947 optarg = get_opt_name(option, 128, optarg, ',') + 1;
948 if (!strcmp(option, "node")) {
949 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
950 nodenr = nb_numa_nodes;
951 } else {
952 nodenr = strtoull(option, NULL, 10);
955 if (get_param_value(option, 128, "mem", optarg) == 0) {
956 node_mem[nodenr] = 0;
957 } else {
958 int64_t sval;
959 sval = strtosz(option, NULL);
960 if (sval < 0) {
961 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
962 exit(1);
964 node_mem[nodenr] = sval;
966 if (get_param_value(option, 128, "cpus", optarg) == 0) {
967 node_cpumask[nodenr] = 0;
968 } else {
969 value = strtoull(option, &endptr, 10);
970 if (value >= 64) {
971 value = 63;
972 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
973 } else {
974 if (*endptr == '-') {
975 endvalue = strtoull(endptr+1, &endptr, 10);
976 if (endvalue >= 63) {
977 endvalue = 62;
978 fprintf(stderr,
979 "only 63 CPUs in NUMA mode supported.\n");
981 value = (2ULL << endvalue) - (1ULL << value);
982 } else {
983 value = 1ULL << value;
986 node_cpumask[nodenr] = value;
988 nb_numa_nodes++;
990 return;
993 static void smp_parse(const char *optarg)
995 int smp, sockets = 0, threads = 0, cores = 0;
996 char *endptr;
997 char option[128];
999 smp = strtoul(optarg, &endptr, 10);
1000 if (endptr != optarg) {
1001 if (*endptr == ',') {
1002 endptr++;
1005 if (get_param_value(option, 128, "sockets", endptr) != 0)
1006 sockets = strtoull(option, NULL, 10);
1007 if (get_param_value(option, 128, "cores", endptr) != 0)
1008 cores = strtoull(option, NULL, 10);
1009 if (get_param_value(option, 128, "threads", endptr) != 0)
1010 threads = strtoull(option, NULL, 10);
1011 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1012 max_cpus = strtoull(option, NULL, 10);
1014 /* compute missing values, prefer sockets over cores over threads */
1015 if (smp == 0 || sockets == 0) {
1016 sockets = sockets > 0 ? sockets : 1;
1017 cores = cores > 0 ? cores : 1;
1018 threads = threads > 0 ? threads : 1;
1019 if (smp == 0) {
1020 smp = cores * threads * sockets;
1022 } else {
1023 if (cores == 0) {
1024 threads = threads > 0 ? threads : 1;
1025 cores = smp / (sockets * threads);
1026 } else {
1027 threads = smp / (cores * sockets);
1030 smp_cpus = smp;
1031 smp_cores = cores > 0 ? cores : 1;
1032 smp_threads = threads > 0 ? threads : 1;
1033 if (max_cpus == 0)
1034 max_cpus = smp_cpus;
1037 /***********************************************************/
1038 /* USB devices */
1040 static int usb_device_add(const char *devname)
1042 const char *p;
1043 USBDevice *dev = NULL;
1045 if (!usb_enabled)
1046 return -1;
1048 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1049 dev = usbdevice_create(devname);
1050 if (dev)
1051 goto done;
1053 /* the other ones */
1054 #ifndef CONFIG_LINUX
1055 /* only the linux version is qdev-ified, usb-bsd still needs this */
1056 if (strstart(devname, "host:", &p)) {
1057 dev = usb_host_device_open(p);
1058 } else
1059 #endif
1060 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1061 dev = usb_bt_init(devname[2] ? hci_init(p) :
1062 bt_new_hci(qemu_find_bt_vlan(0)));
1063 } else {
1064 return -1;
1066 if (!dev)
1067 return -1;
1069 done:
1070 return 0;
1073 static int usb_device_del(const char *devname)
1075 int bus_num, addr;
1076 const char *p;
1078 if (strstart(devname, "host:", &p))
1079 return usb_host_device_close(p);
1081 if (!usb_enabled)
1082 return -1;
1084 p = strchr(devname, '.');
1085 if (!p)
1086 return -1;
1087 bus_num = strtoul(devname, NULL, 0);
1088 addr = strtoul(p + 1, NULL, 0);
1090 return usb_device_delete_addr(bus_num, addr);
1093 static int usb_parse(const char *cmdline)
1095 int r;
1096 r = usb_device_add(cmdline);
1097 if (r < 0) {
1098 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1100 return r;
1103 void do_usb_add(Monitor *mon, const QDict *qdict)
1105 const char *devname = qdict_get_str(qdict, "devname");
1106 if (usb_device_add(devname) < 0) {
1107 error_report("could not add USB device '%s'", devname);
1111 void do_usb_del(Monitor *mon, const QDict *qdict)
1113 const char *devname = qdict_get_str(qdict, "devname");
1114 if (usb_device_del(devname) < 0) {
1115 error_report("could not delete USB device '%s'", devname);
1119 /***********************************************************/
1120 /* PCMCIA/Cardbus */
1122 static struct pcmcia_socket_entry_s {
1123 PCMCIASocket *socket;
1124 struct pcmcia_socket_entry_s *next;
1125 } *pcmcia_sockets = 0;
1127 void pcmcia_socket_register(PCMCIASocket *socket)
1129 struct pcmcia_socket_entry_s *entry;
1131 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1132 entry->socket = socket;
1133 entry->next = pcmcia_sockets;
1134 pcmcia_sockets = entry;
1137 void pcmcia_socket_unregister(PCMCIASocket *socket)
1139 struct pcmcia_socket_entry_s *entry, **ptr;
1141 ptr = &pcmcia_sockets;
1142 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1143 if (entry->socket == socket) {
1144 *ptr = entry->next;
1145 g_free(entry);
1149 void pcmcia_info(Monitor *mon)
1151 struct pcmcia_socket_entry_s *iter;
1153 if (!pcmcia_sockets)
1154 monitor_printf(mon, "No PCMCIA sockets\n");
1156 for (iter = pcmcia_sockets; iter; iter = iter->next)
1157 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1158 iter->socket->attached ? iter->socket->card_string :
1159 "Empty");
1162 /***********************************************************/
1163 /* machine registration */
1165 static QEMUMachine *first_machine = NULL;
1166 QEMUMachine *current_machine = NULL;
1168 int qemu_register_machine(QEMUMachine *m)
1170 QEMUMachine **pm;
1171 pm = &first_machine;
1172 while (*pm != NULL)
1173 pm = &(*pm)->next;
1174 m->next = NULL;
1175 *pm = m;
1176 return 0;
1179 static QEMUMachine *find_machine(const char *name)
1181 QEMUMachine *m;
1183 for(m = first_machine; m != NULL; m = m->next) {
1184 if (!strcmp(m->name, name))
1185 return m;
1186 if (m->alias && !strcmp(m->alias, name))
1187 return m;
1189 return NULL;
1192 static QEMUMachine *find_default_machine(void)
1194 QEMUMachine *m;
1196 for(m = first_machine; m != NULL; m = m->next) {
1197 if (m->is_default) {
1198 return m;
1201 return NULL;
1204 /***********************************************************/
1205 /* main execution loop */
1207 static void gui_update(void *opaque)
1209 uint64_t interval = GUI_REFRESH_INTERVAL;
1210 DisplayState *ds = opaque;
1211 DisplayChangeListener *dcl = ds->listeners;
1213 qemu_flush_coalesced_mmio_buffer();
1214 dpy_refresh(ds);
1216 while (dcl != NULL) {
1217 if (dcl->gui_timer_interval &&
1218 dcl->gui_timer_interval < interval)
1219 interval = dcl->gui_timer_interval;
1220 dcl = dcl->next;
1222 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1225 static void nographic_update(void *opaque)
1227 uint64_t interval = GUI_REFRESH_INTERVAL;
1229 qemu_flush_coalesced_mmio_buffer();
1230 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1233 struct vm_change_state_entry {
1234 VMChangeStateHandler *cb;
1235 void *opaque;
1236 QLIST_ENTRY (vm_change_state_entry) entries;
1239 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1241 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1242 void *opaque)
1244 VMChangeStateEntry *e;
1246 e = g_malloc0(sizeof (*e));
1248 e->cb = cb;
1249 e->opaque = opaque;
1250 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1251 return e;
1254 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1256 QLIST_REMOVE (e, entries);
1257 g_free (e);
1260 void vm_state_notify(int running, RunState state)
1262 VMChangeStateEntry *e;
1264 trace_vm_state_notify(running, state);
1266 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1267 e->cb(e->opaque, running, state);
1271 void vm_start(void)
1273 if (!runstate_is_running()) {
1274 cpu_enable_ticks();
1275 runstate_set(RSTATE_RUNNING);
1276 vm_state_notify(1, RSTATE_RUNNING);
1277 resume_all_vcpus();
1278 monitor_protocol_event(QEVENT_RESUME, NULL);
1282 /* reset/shutdown handler */
1284 typedef struct QEMUResetEntry {
1285 QTAILQ_ENTRY(QEMUResetEntry) entry;
1286 QEMUResetHandler *func;
1287 void *opaque;
1288 } QEMUResetEntry;
1290 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1291 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1292 static int reset_requested;
1293 static int shutdown_requested, shutdown_signal = -1;
1294 static pid_t shutdown_pid;
1295 static int powerdown_requested;
1296 static int debug_requested;
1297 static RunState vmstop_requested = RSTATE_NO_STATE;
1299 int qemu_shutdown_requested_get(void)
1301 return shutdown_requested;
1304 int qemu_reset_requested_get(void)
1306 return reset_requested;
1309 int qemu_shutdown_requested(void)
1311 int r = shutdown_requested;
1312 shutdown_requested = 0;
1313 return r;
1316 void qemu_kill_report(void)
1318 if (shutdown_signal != -1) {
1319 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1320 if (shutdown_pid == 0) {
1321 /* This happens for eg ^C at the terminal, so it's worth
1322 * avoiding printing an odd message in that case.
1324 fputc('\n', stderr);
1325 } else {
1326 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1328 shutdown_signal = -1;
1332 int qemu_reset_requested(void)
1334 int r = reset_requested;
1335 reset_requested = 0;
1336 return r;
1339 int qemu_powerdown_requested(void)
1341 int r = powerdown_requested;
1342 powerdown_requested = 0;
1343 return r;
1346 static int qemu_debug_requested(void)
1348 int r = debug_requested;
1349 debug_requested = 0;
1350 return r;
1353 static RunState qemu_vmstop_requested(void)
1355 RunState s = vmstop_requested;
1356 vmstop_requested = RSTATE_NO_STATE;
1357 return s;
1360 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1362 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1364 re->func = func;
1365 re->opaque = opaque;
1366 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1369 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1371 QEMUResetEntry *re;
1373 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1374 if (re->func == func && re->opaque == opaque) {
1375 QTAILQ_REMOVE(&reset_handlers, re, entry);
1376 g_free(re);
1377 return;
1382 void qemu_system_reset(bool report)
1384 QEMUResetEntry *re, *nre;
1386 /* reset all devices */
1387 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1388 re->func(re->opaque);
1390 if (report) {
1391 monitor_protocol_event(QEVENT_RESET, NULL);
1393 cpu_synchronize_all_post_reset();
1396 void qemu_system_reset_request(void)
1398 if (no_reboot) {
1399 shutdown_requested = 1;
1400 } else {
1401 reset_requested = 1;
1403 cpu_stop_current();
1404 qemu_notify_event();
1407 void qemu_system_killed(int signal, pid_t pid)
1409 shutdown_signal = signal;
1410 shutdown_pid = pid;
1411 no_shutdown = 0;
1412 qemu_system_shutdown_request();
1415 void qemu_system_shutdown_request(void)
1417 shutdown_requested = 1;
1418 qemu_notify_event();
1421 void qemu_system_powerdown_request(void)
1423 powerdown_requested = 1;
1424 qemu_notify_event();
1427 void qemu_system_debug_request(void)
1429 debug_requested = 1;
1430 qemu_notify_event();
1433 void qemu_system_vmstop_request(RunState state)
1435 vmstop_requested = state;
1436 qemu_notify_event();
1439 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1440 static int n_poll_fds;
1441 static int max_priority;
1443 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1444 fd_set *xfds, struct timeval *tv)
1446 GMainContext *context = g_main_context_default();
1447 int i;
1448 int timeout = 0, cur_timeout;
1450 g_main_context_prepare(context, &max_priority);
1452 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1453 poll_fds, ARRAY_SIZE(poll_fds));
1454 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1456 for (i = 0; i < n_poll_fds; i++) {
1457 GPollFD *p = &poll_fds[i];
1459 if ((p->events & G_IO_IN)) {
1460 FD_SET(p->fd, rfds);
1461 *max_fd = MAX(*max_fd, p->fd);
1463 if ((p->events & G_IO_OUT)) {
1464 FD_SET(p->fd, wfds);
1465 *max_fd = MAX(*max_fd, p->fd);
1467 if ((p->events & G_IO_ERR)) {
1468 FD_SET(p->fd, xfds);
1469 *max_fd = MAX(*max_fd, p->fd);
1473 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1474 if (timeout >= 0 && timeout < cur_timeout) {
1475 tv->tv_sec = timeout / 1000;
1476 tv->tv_usec = (timeout % 1000) * 1000;
1480 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1481 bool err)
1483 GMainContext *context = g_main_context_default();
1485 if (!err) {
1486 int i;
1488 for (i = 0; i < n_poll_fds; i++) {
1489 GPollFD *p = &poll_fds[i];
1491 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1492 p->revents |= G_IO_IN;
1494 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1495 p->revents |= G_IO_OUT;
1497 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1498 p->revents |= G_IO_ERR;
1503 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1504 g_main_context_dispatch(context);
1508 int main_loop_wait(int nonblocking)
1510 fd_set rfds, wfds, xfds;
1511 int ret, nfds;
1512 struct timeval tv;
1513 int timeout;
1515 if (nonblocking)
1516 timeout = 0;
1517 else {
1518 timeout = qemu_calculate_timeout();
1519 qemu_bh_update_timeout(&timeout);
1522 os_host_main_loop_wait(&timeout);
1524 tv.tv_sec = timeout / 1000;
1525 tv.tv_usec = (timeout % 1000) * 1000;
1527 /* poll any events */
1528 /* XXX: separate device handlers from system ones */
1529 nfds = -1;
1530 FD_ZERO(&rfds);
1531 FD_ZERO(&wfds);
1532 FD_ZERO(&xfds);
1534 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1535 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1536 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
1538 if (timeout > 0) {
1539 qemu_mutex_unlock_iothread();
1542 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1544 if (timeout > 0) {
1545 qemu_mutex_lock_iothread();
1548 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1549 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1550 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1552 qemu_run_all_timers();
1554 /* Check bottom-halves last in case any of the earlier events triggered
1555 them. */
1556 qemu_bh_poll();
1558 return ret;
1561 qemu_irq qemu_system_powerdown;
1563 static void main_loop(void)
1565 bool nonblocking;
1566 int last_io __attribute__ ((unused)) = 0;
1567 #ifdef CONFIG_PROFILER
1568 int64_t ti;
1569 #endif
1570 int r;
1572 qemu_main_loop_start();
1574 for (;;) {
1575 nonblocking = !kvm_enabled() && last_io > 0;
1576 #ifdef CONFIG_PROFILER
1577 ti = profile_getclock();
1578 #endif
1579 last_io = main_loop_wait(nonblocking);
1580 #ifdef CONFIG_PROFILER
1581 dev_time += profile_getclock() - ti;
1582 #endif
1584 if (qemu_debug_requested()) {
1585 vm_stop(RSTATE_DEBUG);
1587 if (qemu_shutdown_requested()) {
1588 qemu_kill_report();
1589 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1590 if (no_shutdown) {
1591 vm_stop(RSTATE_SHUTDOWN);
1592 } else
1593 break;
1595 if (qemu_reset_requested()) {
1596 pause_all_vcpus();
1597 cpu_synchronize_all_states();
1598 qemu_system_reset(VMRESET_REPORT);
1599 resume_all_vcpus();
1600 if (runstate_check(RSTATE_PANICKED) ||
1601 runstate_check(RSTATE_SHUTDOWN)) {
1602 runstate_set(RSTATE_PAUSED);
1605 if (qemu_powerdown_requested()) {
1606 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1607 qemu_irq_raise(qemu_system_powerdown);
1609 if ((r = qemu_vmstop_requested())) {
1610 vm_stop(r);
1613 bdrv_close_all();
1614 pause_all_vcpus();
1617 static void version(void)
1619 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1622 static void help(int exitcode)
1624 const char *options_help =
1625 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1626 opt_help
1627 #define DEFHEADING(text) stringify(text) "\n"
1628 #include "qemu-options.def"
1629 #undef DEF
1630 #undef DEFHEADING
1631 #undef GEN_DOCS
1633 version();
1634 printf("usage: %s [options] [disk_image]\n"
1635 "\n"
1636 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1637 "\n"
1638 "%s\n"
1639 "During emulation, the following keys are useful:\n"
1640 "ctrl-alt-f toggle full screen\n"
1641 "ctrl-alt-n switch to virtual console 'n'\n"
1642 "ctrl-alt toggle mouse and keyboard grab\n"
1643 "\n"
1644 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1645 "qemu",
1646 options_help);
1647 exit(exitcode);
1650 #define HAS_ARG 0x0001
1652 typedef struct QEMUOption {
1653 const char *name;
1654 int flags;
1655 int index;
1656 uint32_t arch_mask;
1657 } QEMUOption;
1659 static const QEMUOption qemu_options[] = {
1660 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1661 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1662 { option, opt_arg, opt_enum, arch_mask },
1663 #define DEFHEADING(text)
1664 #include "qemu-options.def"
1665 #undef DEF
1666 #undef DEFHEADING
1667 #undef GEN_DOCS
1668 { NULL },
1670 static void select_vgahw (const char *p)
1672 const char *opts;
1674 default_vga = 0;
1675 vga_interface_type = VGA_NONE;
1676 if (strstart(p, "std", &opts)) {
1677 vga_interface_type = VGA_STD;
1678 } else if (strstart(p, "cirrus", &opts)) {
1679 vga_interface_type = VGA_CIRRUS;
1680 } else if (strstart(p, "vmware", &opts)) {
1681 vga_interface_type = VGA_VMWARE;
1682 } else if (strstart(p, "xenfb", &opts)) {
1683 vga_interface_type = VGA_XENFB;
1684 } else if (strstart(p, "qxl", &opts)) {
1685 vga_interface_type = VGA_QXL;
1686 } else if (!strstart(p, "none", &opts)) {
1687 invalid_vga:
1688 fprintf(stderr, "Unknown vga type: %s\n", p);
1689 exit(1);
1691 while (*opts) {
1692 const char *nextopt;
1694 if (strstart(opts, ",retrace=", &nextopt)) {
1695 opts = nextopt;
1696 if (strstart(opts, "dumb", &nextopt))
1697 vga_retrace_method = VGA_RETRACE_DUMB;
1698 else if (strstart(opts, "precise", &nextopt))
1699 vga_retrace_method = VGA_RETRACE_PRECISE;
1700 else goto invalid_vga;
1701 } else goto invalid_vga;
1702 opts = nextopt;
1706 static DisplayType select_display(const char *p)
1708 const char *opts;
1709 DisplayType display = DT_DEFAULT;
1711 if (strstart(p, "sdl", &opts)) {
1712 #ifdef CONFIG_SDL
1713 display = DT_SDL;
1714 while (*opts) {
1715 const char *nextopt;
1717 if (strstart(opts, ",frame=", &nextopt)) {
1718 opts = nextopt;
1719 if (strstart(opts, "on", &nextopt)) {
1720 no_frame = 0;
1721 } else if (strstart(opts, "off", &nextopt)) {
1722 no_frame = 1;
1723 } else {
1724 goto invalid_sdl_args;
1726 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1727 opts = nextopt;
1728 if (strstart(opts, "on", &nextopt)) {
1729 alt_grab = 1;
1730 } else if (strstart(opts, "off", &nextopt)) {
1731 alt_grab = 0;
1732 } else {
1733 goto invalid_sdl_args;
1735 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1736 opts = nextopt;
1737 if (strstart(opts, "on", &nextopt)) {
1738 ctrl_grab = 1;
1739 } else if (strstart(opts, "off", &nextopt)) {
1740 ctrl_grab = 0;
1741 } else {
1742 goto invalid_sdl_args;
1744 } else if (strstart(opts, ",window_close=", &nextopt)) {
1745 opts = nextopt;
1746 if (strstart(opts, "on", &nextopt)) {
1747 no_quit = 0;
1748 } else if (strstart(opts, "off", &nextopt)) {
1749 no_quit = 1;
1750 } else {
1751 goto invalid_sdl_args;
1753 } else {
1754 invalid_sdl_args:
1755 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1756 exit(1);
1758 opts = nextopt;
1760 #else
1761 fprintf(stderr, "SDL support is disabled\n");
1762 exit(1);
1763 #endif
1764 } else if (strstart(p, "vnc", &opts)) {
1765 #ifdef CONFIG_VNC
1766 display_remote++;
1768 if (*opts) {
1769 const char *nextopt;
1771 if (strstart(opts, "=", &nextopt)) {
1772 vnc_display = nextopt;
1775 if (!vnc_display) {
1776 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1777 exit(1);
1779 #else
1780 fprintf(stderr, "VNC support is disabled\n");
1781 exit(1);
1782 #endif
1783 } else if (strstart(p, "curses", &opts)) {
1784 #ifdef CONFIG_CURSES
1785 display = DT_CURSES;
1786 #else
1787 fprintf(stderr, "Curses support is disabled\n");
1788 exit(1);
1789 #endif
1790 } else if (strstart(p, "none", &opts)) {
1791 display = DT_NONE;
1792 } else {
1793 fprintf(stderr, "Unknown display type: %s\n", p);
1794 exit(1);
1797 return display;
1800 static int balloon_parse(const char *arg)
1802 QemuOpts *opts;
1804 if (strcmp(arg, "none") == 0) {
1805 return 0;
1808 if (!strncmp(arg, "virtio", 6)) {
1809 if (arg[6] == ',') {
1810 /* have params -> parse them */
1811 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1812 if (!opts)
1813 return -1;
1814 } else {
1815 /* create empty opts */
1816 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1818 qemu_opt_set(opts, "driver", "virtio-balloon");
1819 return 0;
1822 return -1;
1825 char *qemu_find_file(int type, const char *name)
1827 int len;
1828 const char *subdir;
1829 char *buf;
1831 /* If name contains path separators then try it as a straight path. */
1832 if ((strchr(name, '/') || strchr(name, '\\'))
1833 && access(name, R_OK) == 0) {
1834 return g_strdup(name);
1836 switch (type) {
1837 case QEMU_FILE_TYPE_BIOS:
1838 subdir = "";
1839 break;
1840 case QEMU_FILE_TYPE_KEYMAP:
1841 subdir = "keymaps/";
1842 break;
1843 default:
1844 abort();
1846 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1847 buf = g_malloc0(len);
1848 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1849 if (access(buf, R_OK)) {
1850 g_free(buf);
1851 return NULL;
1853 return buf;
1856 static int device_help_func(QemuOpts *opts, void *opaque)
1858 return qdev_device_help(opts);
1861 static int device_init_func(QemuOpts *opts, void *opaque)
1863 DeviceState *dev;
1865 dev = qdev_device_add(opts);
1866 if (!dev)
1867 return -1;
1868 return 0;
1871 static int chardev_init_func(QemuOpts *opts, void *opaque)
1873 CharDriverState *chr;
1875 chr = qemu_chr_new_from_opts(opts, NULL);
1876 if (!chr)
1877 return -1;
1878 return 0;
1881 #ifdef CONFIG_VIRTFS
1882 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1884 int ret;
1885 ret = qemu_fsdev_add(opts);
1887 return ret;
1889 #endif
1891 static int mon_init_func(QemuOpts *opts, void *opaque)
1893 CharDriverState *chr;
1894 const char *chardev;
1895 const char *mode;
1896 int flags;
1898 mode = qemu_opt_get(opts, "mode");
1899 if (mode == NULL) {
1900 mode = "readline";
1902 if (strcmp(mode, "readline") == 0) {
1903 flags = MONITOR_USE_READLINE;
1904 } else if (strcmp(mode, "control") == 0) {
1905 flags = MONITOR_USE_CONTROL;
1906 } else {
1907 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1908 exit(1);
1911 if (qemu_opt_get_bool(opts, "pretty", 0))
1912 flags |= MONITOR_USE_PRETTY;
1914 if (qemu_opt_get_bool(opts, "default", 0))
1915 flags |= MONITOR_IS_DEFAULT;
1917 chardev = qemu_opt_get(opts, "chardev");
1918 chr = qemu_chr_find(chardev);
1919 if (chr == NULL) {
1920 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1921 exit(1);
1924 monitor_init(chr, flags);
1925 return 0;
1928 static void monitor_parse(const char *optarg, const char *mode)
1930 static int monitor_device_index = 0;
1931 QemuOpts *opts;
1932 const char *p;
1933 char label[32];
1934 int def = 0;
1936 if (strstart(optarg, "chardev:", &p)) {
1937 snprintf(label, sizeof(label), "%s", p);
1938 } else {
1939 snprintf(label, sizeof(label), "compat_monitor%d",
1940 monitor_device_index);
1941 if (monitor_device_index == 0) {
1942 def = 1;
1944 opts = qemu_chr_parse_compat(label, optarg);
1945 if (!opts) {
1946 fprintf(stderr, "parse error: %s\n", optarg);
1947 exit(1);
1951 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1952 if (!opts) {
1953 fprintf(stderr, "duplicate chardev: %s\n", label);
1954 exit(1);
1956 qemu_opt_set(opts, "mode", mode);
1957 qemu_opt_set(opts, "chardev", label);
1958 if (def)
1959 qemu_opt_set(opts, "default", "on");
1960 monitor_device_index++;
1963 struct device_config {
1964 enum {
1965 DEV_USB, /* -usbdevice */
1966 DEV_BT, /* -bt */
1967 DEV_SERIAL, /* -serial */
1968 DEV_PARALLEL, /* -parallel */
1969 DEV_VIRTCON, /* -virtioconsole */
1970 DEV_DEBUGCON, /* -debugcon */
1971 } type;
1972 const char *cmdline;
1973 QTAILQ_ENTRY(device_config) next;
1975 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1977 static void add_device_config(int type, const char *cmdline)
1979 struct device_config *conf;
1981 conf = g_malloc0(sizeof(*conf));
1982 conf->type = type;
1983 conf->cmdline = cmdline;
1984 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1987 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1989 struct device_config *conf;
1990 int rc;
1992 QTAILQ_FOREACH(conf, &device_configs, next) {
1993 if (conf->type != type)
1994 continue;
1995 rc = func(conf->cmdline);
1996 if (0 != rc)
1997 return rc;
1999 return 0;
2002 static int serial_parse(const char *devname)
2004 static int index = 0;
2005 char label[32];
2007 if (strcmp(devname, "none") == 0)
2008 return 0;
2009 if (index == MAX_SERIAL_PORTS) {
2010 fprintf(stderr, "qemu: too many serial ports\n");
2011 exit(1);
2013 snprintf(label, sizeof(label), "serial%d", index);
2014 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2015 if (!serial_hds[index]) {
2016 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2017 devname, strerror(errno));
2018 return -1;
2020 index++;
2021 return 0;
2024 static int parallel_parse(const char *devname)
2026 static int index = 0;
2027 char label[32];
2029 if (strcmp(devname, "none") == 0)
2030 return 0;
2031 if (index == MAX_PARALLEL_PORTS) {
2032 fprintf(stderr, "qemu: too many parallel ports\n");
2033 exit(1);
2035 snprintf(label, sizeof(label), "parallel%d", index);
2036 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2037 if (!parallel_hds[index]) {
2038 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2039 devname, strerror(errno));
2040 return -1;
2042 index++;
2043 return 0;
2046 static int virtcon_parse(const char *devname)
2048 QemuOptsList *device = qemu_find_opts("device");
2049 static int index = 0;
2050 char label[32];
2051 QemuOpts *bus_opts, *dev_opts;
2053 if (strcmp(devname, "none") == 0)
2054 return 0;
2055 if (index == MAX_VIRTIO_CONSOLES) {
2056 fprintf(stderr, "qemu: too many virtio consoles\n");
2057 exit(1);
2060 bus_opts = qemu_opts_create(device, NULL, 0);
2061 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2063 dev_opts = qemu_opts_create(device, NULL, 0);
2064 qemu_opt_set(dev_opts, "driver", "virtconsole");
2066 snprintf(label, sizeof(label), "virtcon%d", index);
2067 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2068 if (!virtcon_hds[index]) {
2069 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2070 devname, strerror(errno));
2071 return -1;
2073 qemu_opt_set(dev_opts, "chardev", label);
2075 index++;
2076 return 0;
2079 static int debugcon_parse(const char *devname)
2081 QemuOpts *opts;
2083 if (!qemu_chr_new("debugcon", devname, NULL)) {
2084 exit(1);
2086 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2087 if (!opts) {
2088 fprintf(stderr, "qemu: already have a debugcon device\n");
2089 exit(1);
2091 qemu_opt_set(opts, "driver", "isa-debugcon");
2092 qemu_opt_set(opts, "chardev", "debugcon");
2093 return 0;
2096 static QEMUMachine *machine_parse(const char *name)
2098 QEMUMachine *m, *machine = NULL;
2100 if (name) {
2101 machine = find_machine(name);
2103 if (machine) {
2104 return machine;
2106 printf("Supported machines are:\n");
2107 for (m = first_machine; m != NULL; m = m->next) {
2108 if (m->alias) {
2109 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2111 printf("%-10s %s%s\n", m->name, m->desc,
2112 m->is_default ? " (default)" : "");
2114 exit(!name || *name != '?');
2117 static int tcg_init(void)
2119 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2120 return 0;
2123 static struct {
2124 const char *opt_name;
2125 const char *name;
2126 int (*available)(void);
2127 int (*init)(void);
2128 int *allowed;
2129 } accel_list[] = {
2130 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2131 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2132 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2135 static int configure_accelerator(void)
2137 const char *p = NULL;
2138 char buf[10];
2139 int i, ret;
2140 bool accel_initalised = 0;
2141 bool init_failed = 0;
2143 QemuOptsList *list = qemu_find_opts("machine");
2144 if (!QTAILQ_EMPTY(&list->head)) {
2145 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2148 if (p == NULL) {
2149 /* Use the default "accelerator", tcg */
2150 p = "tcg";
2153 while (!accel_initalised && *p != '\0') {
2154 if (*p == ':') {
2155 p++;
2157 p = get_opt_name(buf, sizeof (buf), p, ':');
2158 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2159 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2160 *(accel_list[i].allowed) = 1;
2161 ret = accel_list[i].init();
2162 if (ret < 0) {
2163 init_failed = 1;
2164 if (!accel_list[i].available()) {
2165 printf("%s not supported for this target\n",
2166 accel_list[i].name);
2167 } else {
2168 fprintf(stderr, "failed to initialize %s: %s\n",
2169 accel_list[i].name,
2170 strerror(-ret));
2172 *(accel_list[i].allowed) = 0;
2173 } else {
2174 accel_initalised = 1;
2176 break;
2179 if (i == ARRAY_SIZE(accel_list)) {
2180 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2184 if (!accel_initalised) {
2185 fprintf(stderr, "No accelerator found!\n");
2186 exit(1);
2189 if (init_failed) {
2190 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2193 return !accel_initalised;
2196 void qemu_add_exit_notifier(Notifier *notify)
2198 notifier_list_add(&exit_notifiers, notify);
2201 void qemu_remove_exit_notifier(Notifier *notify)
2203 notifier_list_remove(&exit_notifiers, notify);
2206 static void qemu_run_exit_notifiers(void)
2208 notifier_list_notify(&exit_notifiers, NULL);
2211 void qemu_add_machine_init_done_notifier(Notifier *notify)
2213 notifier_list_add(&machine_init_done_notifiers, notify);
2216 static void qemu_run_machine_init_done_notifiers(void)
2218 notifier_list_notify(&machine_init_done_notifiers, NULL);
2221 static const QEMUOption *lookup_opt(int argc, char **argv,
2222 const char **poptarg, int *poptind)
2224 const QEMUOption *popt;
2225 int optind = *poptind;
2226 char *r = argv[optind];
2227 const char *optarg;
2229 loc_set_cmdline(argv, optind, 1);
2230 optind++;
2231 /* Treat --foo the same as -foo. */
2232 if (r[1] == '-')
2233 r++;
2234 popt = qemu_options;
2235 for(;;) {
2236 if (!popt->name) {
2237 error_report("invalid option");
2238 exit(1);
2240 if (!strcmp(popt->name, r + 1))
2241 break;
2242 popt++;
2244 if (popt->flags & HAS_ARG) {
2245 if (optind >= argc) {
2246 error_report("requires an argument");
2247 exit(1);
2249 optarg = argv[optind++];
2250 loc_set_cmdline(argv, optind - 2, 2);
2251 } else {
2252 optarg = NULL;
2255 *poptarg = optarg;
2256 *poptind = optind;
2258 return popt;
2261 static gpointer malloc_and_trace(gsize n_bytes)
2263 void *ptr = malloc(n_bytes);
2264 trace_g_malloc(n_bytes, ptr);
2265 return ptr;
2268 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2270 void *ptr = realloc(mem, n_bytes);
2271 trace_g_realloc(mem, n_bytes, ptr);
2272 return ptr;
2275 static void free_and_trace(gpointer mem)
2277 trace_g_free(mem);
2278 free(mem);
2281 int main(int argc, char **argv, char **envp)
2283 const char *gdbstub_dev = NULL;
2284 int i;
2285 int snapshot, linux_boot;
2286 const char *icount_option = NULL;
2287 const char *initrd_filename;
2288 const char *kernel_filename, *kernel_cmdline;
2289 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2290 DisplayState *ds;
2291 DisplayChangeListener *dcl;
2292 int cyls, heads, secs, translation;
2293 QemuOpts *hda_opts = NULL, *opts;
2294 QemuOptsList *olist;
2295 int optind;
2296 const char *optarg;
2297 const char *loadvm = NULL;
2298 QEMUMachine *machine;
2299 const char *cpu_model;
2300 const char *pid_file = NULL;
2301 const char *incoming = NULL;
2302 #ifdef CONFIG_VNC
2303 int show_vnc_port = 0;
2304 #endif
2305 int defconfig = 1;
2306 const char *log_mask = NULL;
2307 const char *log_file = NULL;
2308 GMemVTable mem_trace = {
2309 .malloc = malloc_and_trace,
2310 .realloc = realloc_and_trace,
2311 .free = free_and_trace,
2313 const char *trace_events = NULL;
2314 const char *trace_file = NULL;
2316 atexit(qemu_run_exit_notifiers);
2317 error_set_progname(argv[0]);
2319 g_mem_set_vtable(&mem_trace);
2320 g_thread_init(NULL);
2322 runstate_init();
2324 init_clocks();
2326 qemu_cache_utils_init(envp);
2328 QLIST_INIT (&vm_change_state_head);
2329 os_setup_early_signal_handling();
2331 module_call_init(MODULE_INIT_MACHINE);
2332 machine = find_default_machine();
2333 cpu_model = NULL;
2334 initrd_filename = NULL;
2335 ram_size = 0;
2336 snapshot = 0;
2337 kernel_filename = NULL;
2338 kernel_cmdline = "";
2339 cyls = heads = secs = 0;
2340 translation = BIOS_ATA_TRANSLATION_AUTO;
2342 for (i = 0; i < MAX_NODES; i++) {
2343 node_mem[i] = 0;
2344 node_cpumask[i] = 0;
2347 nb_numa_nodes = 0;
2348 nb_nics = 0;
2350 autostart= 1;
2352 /* first pass of option parsing */
2353 optind = 1;
2354 while (optind < argc) {
2355 if (argv[optind][0] != '-') {
2356 /* disk image */
2357 optind++;
2358 continue;
2359 } else {
2360 const QEMUOption *popt;
2362 popt = lookup_opt(argc, argv, &optarg, &optind);
2363 switch (popt->index) {
2364 case QEMU_OPTION_nodefconfig:
2365 defconfig=0;
2366 break;
2371 if (defconfig) {
2372 int ret;
2374 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2375 if (ret < 0 && ret != -ENOENT) {
2376 exit(1);
2379 ret = qemu_read_config_file(arch_config_name);
2380 if (ret < 0 && ret != -ENOENT) {
2381 exit(1);
2384 cpudef_init();
2386 /* second pass of option parsing */
2387 optind = 1;
2388 for(;;) {
2389 if (optind >= argc)
2390 break;
2391 if (argv[optind][0] != '-') {
2392 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2393 } else {
2394 const QEMUOption *popt;
2396 popt = lookup_opt(argc, argv, &optarg, &optind);
2397 if (!(popt->arch_mask & arch_type)) {
2398 printf("Option %s not supported for this target\n", popt->name);
2399 exit(1);
2401 switch(popt->index) {
2402 case QEMU_OPTION_M:
2403 machine = machine_parse(optarg);
2404 break;
2405 case QEMU_OPTION_cpu:
2406 /* hw initialization will check this */
2407 if (*optarg == '?') {
2408 list_cpus(stdout, &fprintf, optarg);
2409 exit(0);
2410 } else {
2411 cpu_model = optarg;
2413 break;
2414 case QEMU_OPTION_initrd:
2415 initrd_filename = optarg;
2416 break;
2417 case QEMU_OPTION_hda:
2419 char buf[256];
2420 if (cyls == 0)
2421 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2422 else
2423 snprintf(buf, sizeof(buf),
2424 "%s,cyls=%d,heads=%d,secs=%d%s",
2425 HD_OPTS , cyls, heads, secs,
2426 translation == BIOS_ATA_TRANSLATION_LBA ?
2427 ",trans=lba" :
2428 translation == BIOS_ATA_TRANSLATION_NONE ?
2429 ",trans=none" : "");
2430 drive_add(IF_DEFAULT, 0, optarg, buf);
2431 break;
2433 case QEMU_OPTION_hdb:
2434 case QEMU_OPTION_hdc:
2435 case QEMU_OPTION_hdd:
2436 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2437 HD_OPTS);
2438 break;
2439 case QEMU_OPTION_drive:
2440 if (drive_def(optarg) == NULL) {
2441 exit(1);
2443 break;
2444 case QEMU_OPTION_set:
2445 if (qemu_set_option(optarg) != 0)
2446 exit(1);
2447 break;
2448 case QEMU_OPTION_global:
2449 if (qemu_global_option(optarg) != 0)
2450 exit(1);
2451 break;
2452 case QEMU_OPTION_mtdblock:
2453 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2454 break;
2455 case QEMU_OPTION_sd:
2456 drive_add(IF_SD, 0, optarg, SD_OPTS);
2457 break;
2458 case QEMU_OPTION_pflash:
2459 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2460 break;
2461 case QEMU_OPTION_snapshot:
2462 snapshot = 1;
2463 break;
2464 case QEMU_OPTION_hdachs:
2466 const char *p;
2467 p = optarg;
2468 cyls = strtol(p, (char **)&p, 0);
2469 if (cyls < 1 || cyls > 16383)
2470 goto chs_fail;
2471 if (*p != ',')
2472 goto chs_fail;
2473 p++;
2474 heads = strtol(p, (char **)&p, 0);
2475 if (heads < 1 || heads > 16)
2476 goto chs_fail;
2477 if (*p != ',')
2478 goto chs_fail;
2479 p++;
2480 secs = strtol(p, (char **)&p, 0);
2481 if (secs < 1 || secs > 63)
2482 goto chs_fail;
2483 if (*p == ',') {
2484 p++;
2485 if (!strcmp(p, "none"))
2486 translation = BIOS_ATA_TRANSLATION_NONE;
2487 else if (!strcmp(p, "lba"))
2488 translation = BIOS_ATA_TRANSLATION_LBA;
2489 else if (!strcmp(p, "auto"))
2490 translation = BIOS_ATA_TRANSLATION_AUTO;
2491 else
2492 goto chs_fail;
2493 } else if (*p != '\0') {
2494 chs_fail:
2495 fprintf(stderr, "qemu: invalid physical CHS format\n");
2496 exit(1);
2498 if (hda_opts != NULL) {
2499 char num[16];
2500 snprintf(num, sizeof(num), "%d", cyls);
2501 qemu_opt_set(hda_opts, "cyls", num);
2502 snprintf(num, sizeof(num), "%d", heads);
2503 qemu_opt_set(hda_opts, "heads", num);
2504 snprintf(num, sizeof(num), "%d", secs);
2505 qemu_opt_set(hda_opts, "secs", num);
2506 if (translation == BIOS_ATA_TRANSLATION_LBA)
2507 qemu_opt_set(hda_opts, "trans", "lba");
2508 if (translation == BIOS_ATA_TRANSLATION_NONE)
2509 qemu_opt_set(hda_opts, "trans", "none");
2512 break;
2513 case QEMU_OPTION_numa:
2514 if (nb_numa_nodes >= MAX_NODES) {
2515 fprintf(stderr, "qemu: too many NUMA nodes\n");
2516 exit(1);
2518 numa_add(optarg);
2519 break;
2520 case QEMU_OPTION_display:
2521 display_type = select_display(optarg);
2522 break;
2523 case QEMU_OPTION_nographic:
2524 display_type = DT_NOGRAPHIC;
2525 break;
2526 case QEMU_OPTION_curses:
2527 #ifdef CONFIG_CURSES
2528 display_type = DT_CURSES;
2529 #else
2530 fprintf(stderr, "Curses support is disabled\n");
2531 exit(1);
2532 #endif
2533 break;
2534 case QEMU_OPTION_portrait:
2535 graphic_rotate = 90;
2536 break;
2537 case QEMU_OPTION_rotate:
2538 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2539 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2540 graphic_rotate != 180 && graphic_rotate != 270) {
2541 fprintf(stderr,
2542 "qemu: only 90, 180, 270 deg rotation is available\n");
2543 exit(1);
2545 break;
2546 case QEMU_OPTION_kernel:
2547 kernel_filename = optarg;
2548 break;
2549 case QEMU_OPTION_append:
2550 kernel_cmdline = optarg;
2551 break;
2552 case QEMU_OPTION_cdrom:
2553 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2554 break;
2555 case QEMU_OPTION_boot:
2557 static const char * const params[] = {
2558 "order", "once", "menu",
2559 "splash", "splash-time", NULL
2561 char buf[sizeof(boot_devices)];
2562 char *standard_boot_devices;
2563 int legacy = 0;
2565 if (!strchr(optarg, '=')) {
2566 legacy = 1;
2567 pstrcpy(buf, sizeof(buf), optarg);
2568 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2569 fprintf(stderr,
2570 "qemu: unknown boot parameter '%s' in '%s'\n",
2571 buf, optarg);
2572 exit(1);
2575 if (legacy ||
2576 get_param_value(buf, sizeof(buf), "order", optarg)) {
2577 validate_bootdevices(buf);
2578 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2580 if (!legacy) {
2581 if (get_param_value(buf, sizeof(buf),
2582 "once", optarg)) {
2583 validate_bootdevices(buf);
2584 standard_boot_devices = g_strdup(boot_devices);
2585 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2586 qemu_register_reset(restore_boot_devices,
2587 standard_boot_devices);
2589 if (get_param_value(buf, sizeof(buf),
2590 "menu", optarg)) {
2591 if (!strcmp(buf, "on")) {
2592 boot_menu = 1;
2593 } else if (!strcmp(buf, "off")) {
2594 boot_menu = 0;
2595 } else {
2596 fprintf(stderr,
2597 "qemu: invalid option value '%s'\n",
2598 buf);
2599 exit(1);
2602 qemu_opts_parse(qemu_find_opts("boot-opts"),
2603 optarg, 0);
2606 break;
2607 case QEMU_OPTION_fda:
2608 case QEMU_OPTION_fdb:
2609 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2610 optarg, FD_OPTS);
2611 break;
2612 case QEMU_OPTION_no_fd_bootchk:
2613 fd_bootchk = 0;
2614 break;
2615 case QEMU_OPTION_netdev:
2616 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2617 exit(1);
2619 break;
2620 case QEMU_OPTION_net:
2621 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2622 exit(1);
2624 break;
2625 #ifdef CONFIG_SLIRP
2626 case QEMU_OPTION_tftp:
2627 legacy_tftp_prefix = optarg;
2628 break;
2629 case QEMU_OPTION_bootp:
2630 legacy_bootp_filename = optarg;
2631 break;
2632 case QEMU_OPTION_redir:
2633 if (net_slirp_redir(optarg) < 0)
2634 exit(1);
2635 break;
2636 #endif
2637 case QEMU_OPTION_bt:
2638 add_device_config(DEV_BT, optarg);
2639 break;
2640 case QEMU_OPTION_audio_help:
2641 if (!(audio_available())) {
2642 printf("Option %s not supported for this target\n", popt->name);
2643 exit(1);
2645 AUD_help ();
2646 exit (0);
2647 break;
2648 case QEMU_OPTION_soundhw:
2649 if (!(audio_available())) {
2650 printf("Option %s not supported for this target\n", popt->name);
2651 exit(1);
2653 select_soundhw (optarg);
2654 break;
2655 case QEMU_OPTION_h:
2656 help(0);
2657 break;
2658 case QEMU_OPTION_version:
2659 version();
2660 exit(0);
2661 break;
2662 case QEMU_OPTION_m: {
2663 int64_t value;
2665 value = strtosz(optarg, NULL);
2666 if (value < 0) {
2667 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2668 exit(1);
2671 if (value != (uint64_t)(ram_addr_t)value) {
2672 fprintf(stderr, "qemu: ram size too large\n");
2673 exit(1);
2675 ram_size = value;
2676 break;
2678 case QEMU_OPTION_mempath:
2679 mem_path = optarg;
2680 break;
2681 #ifdef MAP_POPULATE
2682 case QEMU_OPTION_mem_prealloc:
2683 mem_prealloc = 1;
2684 break;
2685 #endif
2686 case QEMU_OPTION_d:
2687 log_mask = optarg;
2688 break;
2689 case QEMU_OPTION_D:
2690 log_file = optarg;
2691 break;
2692 case QEMU_OPTION_s:
2693 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2694 break;
2695 case QEMU_OPTION_gdb:
2696 gdbstub_dev = optarg;
2697 break;
2698 case QEMU_OPTION_L:
2699 data_dir = optarg;
2700 break;
2701 case QEMU_OPTION_bios:
2702 bios_name = optarg;
2703 break;
2704 case QEMU_OPTION_singlestep:
2705 singlestep = 1;
2706 break;
2707 case QEMU_OPTION_S:
2708 autostart = 0;
2709 break;
2710 case QEMU_OPTION_k:
2711 keyboard_layout = optarg;
2712 break;
2713 case QEMU_OPTION_localtime:
2714 rtc_utc = 0;
2715 break;
2716 case QEMU_OPTION_vga:
2717 select_vgahw (optarg);
2718 break;
2719 case QEMU_OPTION_g:
2721 const char *p;
2722 int w, h, depth;
2723 p = optarg;
2724 w = strtol(p, (char **)&p, 10);
2725 if (w <= 0) {
2726 graphic_error:
2727 fprintf(stderr, "qemu: invalid resolution or depth\n");
2728 exit(1);
2730 if (*p != 'x')
2731 goto graphic_error;
2732 p++;
2733 h = strtol(p, (char **)&p, 10);
2734 if (h <= 0)
2735 goto graphic_error;
2736 if (*p == 'x') {
2737 p++;
2738 depth = strtol(p, (char **)&p, 10);
2739 if (depth != 8 && depth != 15 && depth != 16 &&
2740 depth != 24 && depth != 32)
2741 goto graphic_error;
2742 } else if (*p == '\0') {
2743 depth = graphic_depth;
2744 } else {
2745 goto graphic_error;
2748 graphic_width = w;
2749 graphic_height = h;
2750 graphic_depth = depth;
2752 break;
2753 case QEMU_OPTION_echr:
2755 char *r;
2756 term_escape_char = strtol(optarg, &r, 0);
2757 if (r == optarg)
2758 printf("Bad argument to echr\n");
2759 break;
2761 case QEMU_OPTION_monitor:
2762 monitor_parse(optarg, "readline");
2763 default_monitor = 0;
2764 break;
2765 case QEMU_OPTION_qmp:
2766 monitor_parse(optarg, "control");
2767 default_monitor = 0;
2768 break;
2769 case QEMU_OPTION_mon:
2770 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2771 if (!opts) {
2772 exit(1);
2774 default_monitor = 0;
2775 break;
2776 case QEMU_OPTION_chardev:
2777 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2778 if (!opts) {
2779 exit(1);
2781 break;
2782 case QEMU_OPTION_fsdev:
2783 olist = qemu_find_opts("fsdev");
2784 if (!olist) {
2785 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2786 exit(1);
2788 opts = qemu_opts_parse(olist, optarg, 1);
2789 if (!opts) {
2790 fprintf(stderr, "parse error: %s\n", optarg);
2791 exit(1);
2793 break;
2794 case QEMU_OPTION_virtfs: {
2795 QemuOpts *fsdev;
2796 QemuOpts *device;
2798 olist = qemu_find_opts("virtfs");
2799 if (!olist) {
2800 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2801 exit(1);
2803 opts = qemu_opts_parse(olist, optarg, 1);
2804 if (!opts) {
2805 fprintf(stderr, "parse error: %s\n", optarg);
2806 exit(1);
2809 if (qemu_opt_get(opts, "fstype") == NULL ||
2810 qemu_opt_get(opts, "mount_tag") == NULL ||
2811 qemu_opt_get(opts, "path") == NULL ||
2812 qemu_opt_get(opts, "security_model") == NULL) {
2813 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2814 "security_model=[mapped|passthrough|none],"
2815 "mount_tag=tag.\n");
2816 exit(1);
2819 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2820 qemu_opt_get(opts, "mount_tag"), 1);
2821 if (!fsdev) {
2822 fprintf(stderr, "duplicate fsdev id: %s\n",
2823 qemu_opt_get(opts, "mount_tag"));
2824 exit(1);
2826 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2827 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2828 qemu_opt_set(fsdev, "security_model",
2829 qemu_opt_get(opts, "security_model"));
2831 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2832 qemu_opt_set(device, "driver", "virtio-9p-pci");
2833 qemu_opt_set(device, "fsdev",
2834 qemu_opt_get(opts, "mount_tag"));
2835 qemu_opt_set(device, "mount_tag",
2836 qemu_opt_get(opts, "mount_tag"));
2837 break;
2839 case QEMU_OPTION_serial:
2840 add_device_config(DEV_SERIAL, optarg);
2841 default_serial = 0;
2842 if (strncmp(optarg, "mon:", 4) == 0) {
2843 default_monitor = 0;
2845 break;
2846 case QEMU_OPTION_watchdog:
2847 if (watchdog) {
2848 fprintf(stderr,
2849 "qemu: only one watchdog option may be given\n");
2850 return 1;
2852 watchdog = optarg;
2853 break;
2854 case QEMU_OPTION_watchdog_action:
2855 if (select_watchdog_action(optarg) == -1) {
2856 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2857 exit(1);
2859 break;
2860 case QEMU_OPTION_virtiocon:
2861 add_device_config(DEV_VIRTCON, optarg);
2862 default_virtcon = 0;
2863 if (strncmp(optarg, "mon:", 4) == 0) {
2864 default_monitor = 0;
2866 break;
2867 case QEMU_OPTION_parallel:
2868 add_device_config(DEV_PARALLEL, optarg);
2869 default_parallel = 0;
2870 if (strncmp(optarg, "mon:", 4) == 0) {
2871 default_monitor = 0;
2873 break;
2874 case QEMU_OPTION_debugcon:
2875 add_device_config(DEV_DEBUGCON, optarg);
2876 break;
2877 case QEMU_OPTION_loadvm:
2878 loadvm = optarg;
2879 break;
2880 case QEMU_OPTION_full_screen:
2881 full_screen = 1;
2882 break;
2883 #ifdef CONFIG_SDL
2884 case QEMU_OPTION_no_frame:
2885 no_frame = 1;
2886 break;
2887 case QEMU_OPTION_alt_grab:
2888 alt_grab = 1;
2889 break;
2890 case QEMU_OPTION_ctrl_grab:
2891 ctrl_grab = 1;
2892 break;
2893 case QEMU_OPTION_no_quit:
2894 no_quit = 1;
2895 break;
2896 case QEMU_OPTION_sdl:
2897 display_type = DT_SDL;
2898 break;
2899 #else
2900 case QEMU_OPTION_no_frame:
2901 case QEMU_OPTION_alt_grab:
2902 case QEMU_OPTION_ctrl_grab:
2903 case QEMU_OPTION_no_quit:
2904 case QEMU_OPTION_sdl:
2905 fprintf(stderr, "SDL support is disabled\n");
2906 exit(1);
2907 #endif
2908 case QEMU_OPTION_pidfile:
2909 pid_file = optarg;
2910 break;
2911 case QEMU_OPTION_win2k_hack:
2912 win2k_install_hack = 1;
2913 break;
2914 case QEMU_OPTION_rtc_td_hack:
2915 rtc_td_hack = 1;
2916 break;
2917 case QEMU_OPTION_acpitable:
2918 do_acpitable_option(optarg);
2919 break;
2920 case QEMU_OPTION_smbios:
2921 do_smbios_option(optarg);
2922 break;
2923 case QEMU_OPTION_enable_kvm:
2924 olist = qemu_find_opts("machine");
2925 qemu_opts_reset(olist);
2926 qemu_opts_parse(olist, "accel=kvm", 0);
2927 break;
2928 case QEMU_OPTION_machine:
2929 olist = qemu_find_opts("machine");
2930 qemu_opts_reset(olist);
2931 opts = qemu_opts_parse(olist, optarg, 1);
2932 if (!opts) {
2933 fprintf(stderr, "parse error: %s\n", optarg);
2934 exit(1);
2936 optarg = qemu_opt_get(opts, "type");
2937 if (optarg) {
2938 machine = machine_parse(optarg);
2940 break;
2941 case QEMU_OPTION_usb:
2942 usb_enabled = 1;
2943 break;
2944 case QEMU_OPTION_usbdevice:
2945 usb_enabled = 1;
2946 add_device_config(DEV_USB, optarg);
2947 break;
2948 case QEMU_OPTION_device:
2949 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2950 exit(1);
2952 break;
2953 case QEMU_OPTION_smp:
2954 smp_parse(optarg);
2955 if (smp_cpus < 1) {
2956 fprintf(stderr, "Invalid number of CPUs\n");
2957 exit(1);
2959 if (max_cpus < smp_cpus) {
2960 fprintf(stderr, "maxcpus must be equal to or greater than "
2961 "smp\n");
2962 exit(1);
2964 if (max_cpus > 255) {
2965 fprintf(stderr, "Unsupported number of maxcpus\n");
2966 exit(1);
2968 break;
2969 case QEMU_OPTION_vnc:
2970 #ifdef CONFIG_VNC
2971 display_remote++;
2972 vnc_display = optarg;
2973 #else
2974 fprintf(stderr, "VNC support is disabled\n");
2975 exit(1);
2976 #endif
2977 break;
2978 case QEMU_OPTION_no_acpi:
2979 acpi_enabled = 0;
2980 break;
2981 case QEMU_OPTION_no_hpet:
2982 no_hpet = 1;
2983 break;
2984 case QEMU_OPTION_balloon:
2985 if (balloon_parse(optarg) < 0) {
2986 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2987 exit(1);
2989 break;
2990 case QEMU_OPTION_no_reboot:
2991 no_reboot = 1;
2992 break;
2993 case QEMU_OPTION_no_shutdown:
2994 no_shutdown = 1;
2995 break;
2996 case QEMU_OPTION_show_cursor:
2997 cursor_hide = 0;
2998 break;
2999 case QEMU_OPTION_uuid:
3000 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3001 fprintf(stderr, "Fail to parse UUID string."
3002 " Wrong format.\n");
3003 exit(1);
3005 break;
3006 case QEMU_OPTION_option_rom:
3007 if (nb_option_roms >= MAX_OPTION_ROMS) {
3008 fprintf(stderr, "Too many option ROMs\n");
3009 exit(1);
3011 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3012 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3013 option_rom[nb_option_roms].bootindex =
3014 qemu_opt_get_number(opts, "bootindex", -1);
3015 if (!option_rom[nb_option_roms].name) {
3016 fprintf(stderr, "Option ROM file is not specified\n");
3017 exit(1);
3019 nb_option_roms++;
3020 break;
3021 case QEMU_OPTION_semihosting:
3022 semihosting_enabled = 1;
3023 break;
3024 case QEMU_OPTION_name:
3025 qemu_name = g_strdup(optarg);
3027 char *p = strchr(qemu_name, ',');
3028 if (p != NULL) {
3029 *p++ = 0;
3030 if (strncmp(p, "process=", 8)) {
3031 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3032 exit(1);
3034 p += 8;
3035 os_set_proc_name(p);
3038 break;
3039 case QEMU_OPTION_prom_env:
3040 if (nb_prom_envs >= MAX_PROM_ENVS) {
3041 fprintf(stderr, "Too many prom variables\n");
3042 exit(1);
3044 prom_envs[nb_prom_envs] = optarg;
3045 nb_prom_envs++;
3046 break;
3047 case QEMU_OPTION_old_param:
3048 old_param = 1;
3049 break;
3050 case QEMU_OPTION_clock:
3051 configure_alarms(optarg);
3052 break;
3053 case QEMU_OPTION_startdate:
3054 configure_rtc_date_offset(optarg, 1);
3055 break;
3056 case QEMU_OPTION_rtc:
3057 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3058 if (!opts) {
3059 exit(1);
3061 configure_rtc(opts);
3062 break;
3063 case QEMU_OPTION_tb_size:
3064 tcg_tb_size = strtol(optarg, NULL, 0);
3065 if (tcg_tb_size < 0) {
3066 tcg_tb_size = 0;
3068 break;
3069 case QEMU_OPTION_icount:
3070 icount_option = optarg;
3071 break;
3072 case QEMU_OPTION_incoming:
3073 incoming = optarg;
3074 break;
3075 case QEMU_OPTION_nodefaults:
3076 default_serial = 0;
3077 default_parallel = 0;
3078 default_virtcon = 0;
3079 default_monitor = 0;
3080 default_vga = 0;
3081 default_net = 0;
3082 default_floppy = 0;
3083 default_cdrom = 0;
3084 default_sdcard = 0;
3085 break;
3086 case QEMU_OPTION_xen_domid:
3087 if (!(xen_available())) {
3088 printf("Option %s not supported for this target\n", popt->name);
3089 exit(1);
3091 xen_domid = atoi(optarg);
3092 break;
3093 case QEMU_OPTION_xen_create:
3094 if (!(xen_available())) {
3095 printf("Option %s not supported for this target\n", popt->name);
3096 exit(1);
3098 xen_mode = XEN_CREATE;
3099 break;
3100 case QEMU_OPTION_xen_attach:
3101 if (!(xen_available())) {
3102 printf("Option %s not supported for this target\n", popt->name);
3103 exit(1);
3105 xen_mode = XEN_ATTACH;
3106 break;
3107 case QEMU_OPTION_trace:
3109 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3110 if (!opts) {
3111 exit(1);
3113 trace_events = qemu_opt_get(opts, "events");
3114 trace_file = qemu_opt_get(opts, "file");
3115 break;
3117 case QEMU_OPTION_readconfig:
3119 int ret = qemu_read_config_file(optarg);
3120 if (ret < 0) {
3121 fprintf(stderr, "read config %s: %s\n", optarg,
3122 strerror(-ret));
3123 exit(1);
3125 break;
3127 case QEMU_OPTION_spice:
3128 olist = qemu_find_opts("spice");
3129 if (!olist) {
3130 fprintf(stderr, "spice is not supported by this qemu build.\n");
3131 exit(1);
3133 opts = qemu_opts_parse(olist, optarg, 0);
3134 if (!opts) {
3135 fprintf(stderr, "parse error: %s\n", optarg);
3136 exit(1);
3138 break;
3139 case QEMU_OPTION_writeconfig:
3141 FILE *fp;
3142 if (strcmp(optarg, "-") == 0) {
3143 fp = stdout;
3144 } else {
3145 fp = fopen(optarg, "w");
3146 if (fp == NULL) {
3147 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3148 exit(1);
3151 qemu_config_write(fp);
3152 fclose(fp);
3153 break;
3155 default:
3156 os_parse_cmd_args(popt->index, optarg);
3160 loc_set_none();
3162 /* Open the logfile at this point, if necessary. We can't open the logfile
3163 * when encountering either of the logging options (-d or -D) because the
3164 * other one may be encountered later on the command line, changing the
3165 * location or level of logging.
3167 if (log_mask) {
3168 if (log_file) {
3169 set_cpu_log_filename(log_file);
3171 set_cpu_log(log_mask);
3174 if (!trace_backend_init(trace_events, trace_file)) {
3175 exit(1);
3178 /* If no data_dir is specified then try to find it relative to the
3179 executable path. */
3180 if (!data_dir) {
3181 data_dir = os_find_datadir(argv[0]);
3183 /* If all else fails use the install path specified when building. */
3184 if (!data_dir) {
3185 data_dir = CONFIG_QEMU_DATADIR;
3189 * Default to max_cpus = smp_cpus, in case the user doesn't
3190 * specify a max_cpus value.
3192 if (!max_cpus)
3193 max_cpus = smp_cpus;
3195 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3196 if (smp_cpus > machine->max_cpus) {
3197 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3198 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3199 machine->max_cpus);
3200 exit(1);
3204 * Get the default machine options from the machine if it is not already
3205 * specified either by the configuration file or by the command line.
3207 if (machine->default_machine_opts) {
3208 QemuOptsList *list = qemu_find_opts("machine");
3209 const char *p = NULL;
3211 if (!QTAILQ_EMPTY(&list->head)) {
3212 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3214 if (p == NULL) {
3215 qemu_opts_reset(list);
3216 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3217 if (!opts) {
3218 fprintf(stderr, "parse error for machine %s: %s\n",
3219 machine->name, machine->default_machine_opts);
3220 exit(1);
3225 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3226 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3228 if (machine->no_serial) {
3229 default_serial = 0;
3231 if (machine->no_parallel) {
3232 default_parallel = 0;
3234 if (!machine->use_virtcon) {
3235 default_virtcon = 0;
3237 if (machine->no_vga) {
3238 default_vga = 0;
3240 if (machine->no_floppy) {
3241 default_floppy = 0;
3243 if (machine->no_cdrom) {
3244 default_cdrom = 0;
3246 if (machine->no_sdcard) {
3247 default_sdcard = 0;
3250 if (display_type == DT_NOGRAPHIC) {
3251 if (default_parallel)
3252 add_device_config(DEV_PARALLEL, "null");
3253 if (default_serial && default_monitor) {
3254 add_device_config(DEV_SERIAL, "mon:stdio");
3255 } else if (default_virtcon && default_monitor) {
3256 add_device_config(DEV_VIRTCON, "mon:stdio");
3257 } else {
3258 if (default_serial)
3259 add_device_config(DEV_SERIAL, "stdio");
3260 if (default_virtcon)
3261 add_device_config(DEV_VIRTCON, "stdio");
3262 if (default_monitor)
3263 monitor_parse("stdio", "readline");
3265 } else {
3266 if (default_serial)
3267 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3268 if (default_parallel)
3269 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3270 if (default_monitor)
3271 monitor_parse("vc:80Cx24C", "readline");
3272 if (default_virtcon)
3273 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3275 if (default_vga)
3276 vga_interface_type = VGA_CIRRUS;
3278 socket_init();
3280 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3281 exit(1);
3282 #ifdef CONFIG_VIRTFS
3283 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3284 exit(1);
3286 #endif
3288 os_daemonize();
3290 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3291 os_pidfile_error();
3292 exit(1);
3295 /* init the memory */
3296 if (ram_size == 0) {
3297 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3300 configure_accelerator();
3302 if (qemu_init_main_loop()) {
3303 fprintf(stderr, "qemu_init_main_loop failed\n");
3304 exit(1);
3306 linux_boot = (kernel_filename != NULL);
3308 if (!linux_boot && *kernel_cmdline != '\0') {
3309 fprintf(stderr, "-append only allowed with -kernel option\n");
3310 exit(1);
3313 if (!linux_boot && initrd_filename != NULL) {
3314 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3315 exit(1);
3318 os_set_line_buffering();
3320 if (init_timer_alarm() < 0) {
3321 fprintf(stderr, "could not initialize alarm timer\n");
3322 exit(1);
3324 configure_icount(icount_option);
3326 if (net_init_clients() < 0) {
3327 exit(1);
3330 /* init the bluetooth world */
3331 if (foreach_device_config(DEV_BT, bt_parse))
3332 exit(1);
3334 if (!xen_enabled()) {
3335 /* On 32-bit hosts, QEMU is limited by virtual address space */
3336 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3337 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3338 exit(1);
3342 cpu_exec_init_all();
3344 bdrv_init_with_whitelist();
3346 blk_mig_init();
3348 /* open the virtual block devices */
3349 if (snapshot)
3350 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3351 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3352 exit(1);
3354 default_drive(default_cdrom, snapshot, machine->use_scsi,
3355 IF_DEFAULT, 2, CDROM_OPTS);
3356 default_drive(default_floppy, snapshot, machine->use_scsi,
3357 IF_FLOPPY, 0, FD_OPTS);
3358 default_drive(default_sdcard, snapshot, machine->use_scsi,
3359 IF_SD, 0, SD_OPTS);
3361 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3362 ram_load, NULL);
3364 if (nb_numa_nodes > 0) {
3365 int i;
3367 if (nb_numa_nodes > MAX_NODES) {
3368 nb_numa_nodes = MAX_NODES;
3371 /* If no memory size if given for any node, assume the default case
3372 * and distribute the available memory equally across all nodes
3374 for (i = 0; i < nb_numa_nodes; i++) {
3375 if (node_mem[i] != 0)
3376 break;
3378 if (i == nb_numa_nodes) {
3379 uint64_t usedmem = 0;
3381 /* On Linux, the each node's border has to be 8MB aligned,
3382 * the final node gets the rest.
3384 for (i = 0; i < nb_numa_nodes - 1; i++) {
3385 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3386 usedmem += node_mem[i];
3388 node_mem[i] = ram_size - usedmem;
3391 for (i = 0; i < nb_numa_nodes; i++) {
3392 if (node_cpumask[i] != 0)
3393 break;
3395 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3396 * must cope with this anyway, because there are BIOSes out there in
3397 * real machines which also use this scheme.
3399 if (i == nb_numa_nodes) {
3400 for (i = 0; i < smp_cpus; i++) {
3401 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3406 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3407 exit(1);
3410 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3411 exit(1);
3412 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3413 exit(1);
3414 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3415 exit(1);
3416 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3417 exit(1);
3419 module_call_init(MODULE_INIT_DEVICE);
3421 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3422 exit(0);
3424 if (watchdog) {
3425 i = select_watchdog(watchdog);
3426 if (i > 0)
3427 exit (i == 1 ? 1 : 0);
3430 if (machine->compat_props) {
3431 qdev_prop_register_global_list(machine->compat_props);
3433 qemu_add_globals();
3435 machine->init(ram_size, boot_devices,
3436 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3438 cpu_synchronize_all_post_init();
3440 set_numa_modes();
3442 current_machine = machine;
3444 /* init USB devices */
3445 if (usb_enabled) {
3446 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3447 exit(1);
3450 /* init generic devices */
3451 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3452 exit(1);
3454 net_check_clients();
3456 /* just use the first displaystate for the moment */
3457 ds = get_displaystate();
3459 if (using_spice)
3460 display_remote++;
3461 if (display_type == DT_DEFAULT && !display_remote) {
3462 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3463 display_type = DT_SDL;
3464 #elif defined(CONFIG_VNC)
3465 vnc_display = "localhost:0,to=99";
3466 show_vnc_port = 1;
3467 #else
3468 display_type = DT_NONE;
3469 #endif
3473 /* init local displays */
3474 switch (display_type) {
3475 case DT_NOGRAPHIC:
3476 break;
3477 #if defined(CONFIG_CURSES)
3478 case DT_CURSES:
3479 curses_display_init(ds, full_screen);
3480 break;
3481 #endif
3482 #if defined(CONFIG_SDL)
3483 case DT_SDL:
3484 sdl_display_init(ds, full_screen, no_frame);
3485 break;
3486 #elif defined(CONFIG_COCOA)
3487 case DT_SDL:
3488 cocoa_display_init(ds, full_screen);
3489 break;
3490 #endif
3491 default:
3492 break;
3495 /* must be after terminal init, SDL library changes signal handlers */
3496 os_setup_signal_handling();
3498 #ifdef CONFIG_VNC
3499 /* init remote displays */
3500 if (vnc_display) {
3501 vnc_display_init(ds);
3502 if (vnc_display_open(ds, vnc_display) < 0)
3503 exit(1);
3505 if (show_vnc_port) {
3506 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3509 #endif
3510 #ifdef CONFIG_SPICE
3511 if (using_spice && !qxl_enabled) {
3512 qemu_spice_display_init(ds);
3514 #endif
3516 /* display setup */
3517 dpy_resize(ds);
3518 dcl = ds->listeners;
3519 while (dcl != NULL) {
3520 if (dcl->dpy_refresh != NULL) {
3521 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3522 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3523 break;
3525 dcl = dcl->next;
3527 if (ds->gui_timer == NULL) {
3528 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3529 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3531 text_consoles_set_display(ds);
3533 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3534 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3535 gdbstub_dev);
3536 exit(1);
3539 qdev_machine_creation_done();
3541 if (rom_load_all() != 0) {
3542 fprintf(stderr, "rom loading failed\n");
3543 exit(1);
3546 /* TODO: once all bus devices are qdevified, this should be done
3547 * when bus is created by qdev.c */
3548 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3549 qemu_run_machine_init_done_notifiers();
3551 qemu_system_reset(VMRESET_SILENT);
3552 if (loadvm) {
3553 if (load_vmstate(loadvm) < 0) {
3554 autostart = 0;
3558 if (incoming) {
3559 runstate_set(RSTATE_IN_MIGRATE);
3560 int ret = qemu_start_incoming_migration(incoming);
3561 if (ret < 0) {
3562 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3563 incoming, ret);
3564 exit(ret);
3566 } else if (autostart) {
3567 vm_start();
3568 } else {
3569 runstate_set(RSTATE_PRE_LAUNCH);
3572 os_setup_post();
3574 main_loop();
3575 quit_timers();
3576 net_cleanup();
3577 res_free();
3579 return 0;