qemu-kvm: Remove broken ia64 fragments
[qemu-kvm.git] / vl.c
blobb0358e93e1774c073bfc98ae9b2abb0e6874b0fa
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
51 #ifdef CONFIG_BSD
52 #include <sys/stat.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <libutil.h>
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <pty.h>
62 #include <malloc.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
66 #endif
67 #ifdef __sun__
68 #include <sys/stat.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
77 #include <net/if.h>
78 #include <syslog.h>
79 #include <stropts.h>
80 #endif
81 #endif
82 #endif
84 #if defined(__OpenBSD__)
85 #include <util.h>
86 #endif
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
90 #endif
92 #ifdef _WIN32
93 #include <windows.h>
94 #endif
96 #ifdef CONFIG_SDL
97 #if defined(__APPLE__) || defined(main)
98 #include <SDL.h>
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
104 #undef main
105 #define main qemu_main
106 #endif
107 #endif /* CONFIG_SDL */
109 #ifdef CONFIG_COCOA
110 #undef main
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
114 #include <glib.h>
116 #include "hw/hw.h"
117 #include "hw/boards.h"
118 #include "hw/usb.h"
119 #include "hw/pcmcia.h"
120 #include "hw/pc.h"
121 #include "hw/isa.h"
122 #include "hw/baum.h"
123 #include "hw/bt.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
126 #include "hw/xen.h"
127 #include "hw/qdev.h"
128 #include "hw/loader.h"
129 #include "bt-host.h"
130 #include "net.h"
131 #include "net/slirp.h"
132 #include "monitor.h"
133 #include "console.h"
134 #include "sysemu.h"
135 #include "gdbstub.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
139 #include "block.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
142 #include "dma.h"
143 #include "audio/audio.h"
144 #include "migration.h"
145 #include "kvm.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #include "kvm.h"
151 #ifdef CONFIG_VIRTFS
152 #include "fsdev/qemu-fsdev.h"
153 #endif
155 #include "disas.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
161 #include "trace.h"
162 #include "trace/control.h"
163 #include "qemu-queue.h"
164 #include "cpus.h"
165 #include "arch_init.h"
167 #include "ui/qemu-spice.h"
169 //#define DEBUG_NET
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir;
177 const char *bios_name = NULL;
178 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
179 DisplayType display_type = DT_DEFAULT;
180 int display_remote = 0;
181 const char* keyboard_layout = NULL;
182 ram_addr_t ram_size;
183 const char *mem_path = NULL;
184 #ifdef MAP_POPULATE
185 int mem_prealloc = 0; /* force preallocation of physical target memory */
186 #endif
187 int nb_nics;
188 NICInfo nd_table[MAX_NICS];
189 int autostart;
190 static int rtc_utc = 1;
191 static int rtc_date_offset = -1; /* -1 means no change */
192 QEMUClock *rtc_clock;
193 int vga_interface_type = VGA_NONE;
194 static int full_screen = 0;
195 #ifdef CONFIG_SDL
196 static int no_frame = 0;
197 #endif
198 int no_quit = 0;
199 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
200 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
201 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
202 int win2k_install_hack = 0;
203 int rtc_td_hack = 0;
204 int usb_enabled = 0;
205 int singlestep = 0;
206 int smp_cpus = 1;
207 int max_cpus = 0;
208 int smp_cores = 1;
209 int smp_threads = 1;
210 #ifdef CONFIG_VNC
211 const char *vnc_display;
212 #endif
213 int acpi_enabled = 1;
214 int no_hpet = 0;
215 int fd_bootchk = 1;
216 int no_reboot = 0;
217 int no_shutdown = 0;
218 int cursor_hide = 1;
219 int graphic_rotate = 0;
220 const char *watchdog;
221 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
222 int nb_option_roms;
223 int semihosting_enabled = 0;
224 int time_drift_fix = 0;
225 unsigned int kvm_shadow_memory = 0;
226 int old_param = 0;
227 const char *qemu_name;
228 int alt_grab = 0;
229 int ctrl_grab = 0;
230 unsigned int nb_prom_envs = 0;
231 const char *prom_envs[MAX_PROM_ENVS];
232 int boot_menu;
233 uint8_t *boot_splash_filedata;
234 int boot_splash_filedata_size;
235 uint8_t qemu_extra_params_fw[2];
237 typedef struct FWBootEntry FWBootEntry;
239 struct FWBootEntry {
240 QTAILQ_ENTRY(FWBootEntry) link;
241 int32_t bootindex;
242 DeviceState *dev;
243 char *suffix;
246 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
248 int nb_numa_nodes;
249 uint64_t node_mem[MAX_NODES];
250 uint64_t node_cpumask[MAX_NODES];
252 static QEMUTimer *nographic_timer;
254 uint8_t qemu_uuid[16];
256 static QEMUBootSetHandler *boot_set_handler;
257 static void *boot_set_opaque;
259 static NotifierList exit_notifiers =
260 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
262 static NotifierList machine_init_done_notifiers =
263 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
265 static int tcg_allowed = 1;
266 int kvm_allowed = 0;
267 int xen_allowed = 0;
268 uint32_t xen_domid;
269 enum xen_mode xen_mode = XEN_EMULATE;
270 static int tcg_tb_size;
272 static int default_serial = 1;
273 static int default_parallel = 1;
274 static int default_virtcon = 1;
275 static int default_monitor = 1;
276 static int default_vga = 1;
277 static int default_floppy = 1;
278 static int default_cdrom = 1;
279 static int default_sdcard = 1;
281 static struct {
282 const char *driver;
283 int *flag;
284 } default_list[] = {
285 { .driver = "isa-serial", .flag = &default_serial },
286 { .driver = "isa-parallel", .flag = &default_parallel },
287 { .driver = "isa-fdc", .flag = &default_floppy },
288 { .driver = "ide-cd", .flag = &default_cdrom },
289 { .driver = "ide-hd", .flag = &default_cdrom },
290 { .driver = "ide-drive", .flag = &default_cdrom },
291 { .driver = "scsi-cd", .flag = &default_cdrom },
292 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
293 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
294 { .driver = "virtio-serial", .flag = &default_virtcon },
295 { .driver = "VGA", .flag = &default_vga },
296 { .driver = "cirrus-vga", .flag = &default_vga },
297 { .driver = "vmware-svga", .flag = &default_vga },
298 { .driver = "isa-vga", .flag = &default_vga },
299 { .driver = "qxl-vga", .flag = &default_vga },
302 static void res_free(void)
304 if (boot_splash_filedata != NULL) {
305 g_free(boot_splash_filedata);
306 boot_splash_filedata = NULL;
310 static int default_driver_check(QemuOpts *opts, void *opaque)
312 const char *driver = qemu_opt_get(opts, "driver");
313 int i;
315 if (!driver)
316 return 0;
317 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
318 if (strcmp(default_list[i].driver, driver) != 0)
319 continue;
320 *(default_list[i].flag) = 0;
322 return 0;
325 /***********************************************************/
326 /* QEMU state */
328 static RunState current_run_state = RSTATE_NO_STATE;
330 typedef struct {
331 RunState from;
332 RunState to;
333 } RunStateTransition;
335 static const RunStateTransition runstate_transitions_def[] = {
336 /* from -> to */
337 { RSTATE_NO_STATE, RSTATE_RUNNING },
338 { RSTATE_NO_STATE, RSTATE_IN_MIGRATE },
339 { RSTATE_NO_STATE, RSTATE_PRE_LAUNCH },
341 { RSTATE_DEBUG, RSTATE_RUNNING },
343 { RSTATE_IN_MIGRATE, RSTATE_RUNNING },
344 { RSTATE_IN_MIGRATE, RSTATE_PRE_LAUNCH },
346 { RSTATE_PANICKED, RSTATE_PAUSED },
348 { RSTATE_IO_ERROR, RSTATE_RUNNING },
350 { RSTATE_PAUSED, RSTATE_RUNNING },
352 { RSTATE_POST_MIGRATE, RSTATE_RUNNING },
354 { RSTATE_PRE_LAUNCH, RSTATE_RUNNING },
355 { RSTATE_PRE_LAUNCH, RSTATE_POST_MIGRATE },
357 { RSTATE_PRE_MIGRATE, RSTATE_RUNNING },
358 { RSTATE_PRE_MIGRATE, RSTATE_POST_MIGRATE },
360 { RSTATE_RESTORE, RSTATE_RUNNING },
362 { RSTATE_RUNNING, RSTATE_DEBUG },
363 { RSTATE_RUNNING, RSTATE_PANICKED },
364 { RSTATE_RUNNING, RSTATE_IO_ERROR },
365 { RSTATE_RUNNING, RSTATE_PAUSED },
366 { RSTATE_RUNNING, RSTATE_PRE_MIGRATE },
367 { RSTATE_RUNNING, RSTATE_RESTORE },
368 { RSTATE_RUNNING, RSTATE_SAVEVM },
369 { RSTATE_RUNNING, RSTATE_SHUTDOWN },
370 { RSTATE_RUNNING, RSTATE_WATCHDOG },
372 { RSTATE_SAVEVM, RSTATE_RUNNING },
374 { RSTATE_SHUTDOWN, RSTATE_PAUSED },
376 { RSTATE_WATCHDOG, RSTATE_RUNNING },
378 { RSTATE_MAX, RSTATE_MAX },
381 static bool runstate_valid_transitions[RSTATE_MAX][RSTATE_MAX];
383 static const char *const runstate_name_tbl[RSTATE_MAX] = {
384 [RSTATE_DEBUG] = "debug",
385 [RSTATE_IN_MIGRATE] = "incoming-migration",
386 [RSTATE_PANICKED] = "internal-error",
387 [RSTATE_IO_ERROR] = "io-error",
388 [RSTATE_PAUSED] = "paused",
389 [RSTATE_POST_MIGRATE] = "post-migrate",
390 [RSTATE_PRE_LAUNCH] = "prelaunch",
391 [RSTATE_PRE_MIGRATE] = "finish-migrate",
392 [RSTATE_RESTORE] = "restore-vm",
393 [RSTATE_RUNNING] = "running",
394 [RSTATE_SAVEVM] = "save-vm",
395 [RSTATE_SHUTDOWN] = "shutdown",
396 [RSTATE_WATCHDOG] = "watchdog",
399 bool runstate_check(RunState state)
401 return current_run_state == state;
404 void runstate_init(void)
406 const RunStateTransition *p;
408 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
410 for (p = &runstate_transitions_def[0]; p->from != RSTATE_MAX; p++) {
411 runstate_valid_transitions[p->from][p->to] = true;
415 /* This function will abort() on invalid state transitions */
416 void runstate_set(RunState new_state)
418 if (new_state >= RSTATE_MAX ||
419 !runstate_valid_transitions[current_run_state][new_state]) {
420 fprintf(stderr, "invalid runstate transition\n");
421 abort();
424 current_run_state = new_state;
427 const char *runstate_as_string(void)
429 assert(current_run_state > RSTATE_NO_STATE &&
430 current_run_state < RSTATE_MAX);
431 return runstate_name_tbl[current_run_state];
434 int runstate_is_running(void)
436 return runstate_check(RSTATE_RUNNING);
439 /***********************************************************/
440 /* real time host monotonic timer */
442 /***********************************************************/
443 /* host time/date access */
444 void qemu_get_timedate(struct tm *tm, int offset)
446 time_t ti;
447 struct tm *ret;
449 time(&ti);
450 ti += offset;
451 if (rtc_date_offset == -1) {
452 if (rtc_utc)
453 ret = gmtime(&ti);
454 else
455 ret = localtime(&ti);
456 } else {
457 ti -= rtc_date_offset;
458 ret = gmtime(&ti);
461 memcpy(tm, ret, sizeof(struct tm));
464 int qemu_timedate_diff(struct tm *tm)
466 time_t seconds;
468 if (rtc_date_offset == -1)
469 if (rtc_utc)
470 seconds = mktimegm(tm);
471 else
472 seconds = mktime(tm);
473 else
474 seconds = mktimegm(tm) + rtc_date_offset;
476 return seconds - time(NULL);
479 void rtc_change_mon_event(struct tm *tm)
481 QObject *data;
483 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
484 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
485 qobject_decref(data);
488 static void configure_rtc_date_offset(const char *startdate, int legacy)
490 time_t rtc_start_date;
491 struct tm tm;
493 if (!strcmp(startdate, "now") && legacy) {
494 rtc_date_offset = -1;
495 } else {
496 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
497 &tm.tm_year,
498 &tm.tm_mon,
499 &tm.tm_mday,
500 &tm.tm_hour,
501 &tm.tm_min,
502 &tm.tm_sec) == 6) {
503 /* OK */
504 } else if (sscanf(startdate, "%d-%d-%d",
505 &tm.tm_year,
506 &tm.tm_mon,
507 &tm.tm_mday) == 3) {
508 tm.tm_hour = 0;
509 tm.tm_min = 0;
510 tm.tm_sec = 0;
511 } else {
512 goto date_fail;
514 tm.tm_year -= 1900;
515 tm.tm_mon--;
516 rtc_start_date = mktimegm(&tm);
517 if (rtc_start_date == -1) {
518 date_fail:
519 fprintf(stderr, "Invalid date format. Valid formats are:\n"
520 "'2006-06-17T16:01:21' or '2006-06-17'\n");
521 exit(1);
523 rtc_date_offset = time(NULL) - rtc_start_date;
527 static void configure_rtc(QemuOpts *opts)
529 const char *value;
531 value = qemu_opt_get(opts, "base");
532 if (value) {
533 if (!strcmp(value, "utc")) {
534 rtc_utc = 1;
535 } else if (!strcmp(value, "localtime")) {
536 rtc_utc = 0;
537 } else {
538 configure_rtc_date_offset(value, 0);
541 value = qemu_opt_get(opts, "clock");
542 if (value) {
543 if (!strcmp(value, "host")) {
544 rtc_clock = host_clock;
545 } else if (!strcmp(value, "vm")) {
546 rtc_clock = vm_clock;
547 } else {
548 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
549 exit(1);
552 value = qemu_opt_get(opts, "driftfix");
553 if (value) {
554 if (!strcmp(value, "slew")) {
555 rtc_td_hack = 1;
556 } else if (!strcmp(value, "none")) {
557 rtc_td_hack = 0;
558 } else {
559 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
560 exit(1);
565 /***********************************************************/
566 /* Bluetooth support */
567 static int nb_hcis;
568 static int cur_hci;
569 static struct HCIInfo *hci_table[MAX_NICS];
571 static struct bt_vlan_s {
572 struct bt_scatternet_s net;
573 int id;
574 struct bt_vlan_s *next;
575 } *first_bt_vlan;
577 /* find or alloc a new bluetooth "VLAN" */
578 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
580 struct bt_vlan_s **pvlan, *vlan;
581 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
582 if (vlan->id == id)
583 return &vlan->net;
585 vlan = g_malloc0(sizeof(struct bt_vlan_s));
586 vlan->id = id;
587 pvlan = &first_bt_vlan;
588 while (*pvlan != NULL)
589 pvlan = &(*pvlan)->next;
590 *pvlan = vlan;
591 return &vlan->net;
594 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
598 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
600 return -ENOTSUP;
603 static struct HCIInfo null_hci = {
604 .cmd_send = null_hci_send,
605 .sco_send = null_hci_send,
606 .acl_send = null_hci_send,
607 .bdaddr_set = null_hci_addr_set,
610 struct HCIInfo *qemu_next_hci(void)
612 if (cur_hci == nb_hcis)
613 return &null_hci;
615 return hci_table[cur_hci++];
618 static struct HCIInfo *hci_init(const char *str)
620 char *endp;
621 struct bt_scatternet_s *vlan = 0;
623 if (!strcmp(str, "null"))
624 /* null */
625 return &null_hci;
626 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
627 /* host[:hciN] */
628 return bt_host_hci(str[4] ? str + 5 : "hci0");
629 else if (!strncmp(str, "hci", 3)) {
630 /* hci[,vlan=n] */
631 if (str[3]) {
632 if (!strncmp(str + 3, ",vlan=", 6)) {
633 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
634 if (*endp)
635 vlan = 0;
637 } else
638 vlan = qemu_find_bt_vlan(0);
639 if (vlan)
640 return bt_new_hci(vlan);
643 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
645 return 0;
648 static int bt_hci_parse(const char *str)
650 struct HCIInfo *hci;
651 bdaddr_t bdaddr;
653 if (nb_hcis >= MAX_NICS) {
654 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
655 return -1;
658 hci = hci_init(str);
659 if (!hci)
660 return -1;
662 bdaddr.b[0] = 0x52;
663 bdaddr.b[1] = 0x54;
664 bdaddr.b[2] = 0x00;
665 bdaddr.b[3] = 0x12;
666 bdaddr.b[4] = 0x34;
667 bdaddr.b[5] = 0x56 + nb_hcis;
668 hci->bdaddr_set(hci, bdaddr.b);
670 hci_table[nb_hcis++] = hci;
672 return 0;
675 static void bt_vhci_add(int vlan_id)
677 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
679 if (!vlan->slave)
680 fprintf(stderr, "qemu: warning: adding a VHCI to "
681 "an empty scatternet %i\n", vlan_id);
683 bt_vhci_init(bt_new_hci(vlan));
686 static struct bt_device_s *bt_device_add(const char *opt)
688 struct bt_scatternet_s *vlan;
689 int vlan_id = 0;
690 char *endp = strstr(opt, ",vlan=");
691 int len = (endp ? endp - opt : strlen(opt)) + 1;
692 char devname[10];
694 pstrcpy(devname, MIN(sizeof(devname), len), opt);
696 if (endp) {
697 vlan_id = strtol(endp + 6, &endp, 0);
698 if (*endp) {
699 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
700 return 0;
704 vlan = qemu_find_bt_vlan(vlan_id);
706 if (!vlan->slave)
707 fprintf(stderr, "qemu: warning: adding a slave device to "
708 "an empty scatternet %i\n", vlan_id);
710 if (!strcmp(devname, "keyboard"))
711 return bt_keyboard_init(vlan);
713 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
714 return 0;
717 static int bt_parse(const char *opt)
719 const char *endp, *p;
720 int vlan;
722 if (strstart(opt, "hci", &endp)) {
723 if (!*endp || *endp == ',') {
724 if (*endp)
725 if (!strstart(endp, ",vlan=", 0))
726 opt = endp + 1;
728 return bt_hci_parse(opt);
730 } else if (strstart(opt, "vhci", &endp)) {
731 if (!*endp || *endp == ',') {
732 if (*endp) {
733 if (strstart(endp, ",vlan=", &p)) {
734 vlan = strtol(p, (char **) &endp, 0);
735 if (*endp) {
736 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
737 return 1;
739 } else {
740 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
741 return 1;
743 } else
744 vlan = 0;
746 bt_vhci_add(vlan);
747 return 0;
749 } else if (strstart(opt, "device:", &endp))
750 return !bt_device_add(endp);
752 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
753 return 1;
756 /***********************************************************/
757 /* QEMU Block devices */
759 #define HD_OPTS "media=disk"
760 #define CDROM_OPTS "media=cdrom"
761 #define FD_OPTS ""
762 #define PFLASH_OPTS ""
763 #define MTD_OPTS ""
764 #define SD_OPTS ""
766 static int drive_init_func(QemuOpts *opts, void *opaque)
768 int *use_scsi = opaque;
770 return drive_init(opts, *use_scsi) == NULL;
773 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
775 if (NULL == qemu_opt_get(opts, "snapshot")) {
776 qemu_opt_set(opts, "snapshot", "on");
778 return 0;
781 static void default_drive(int enable, int snapshot, int use_scsi,
782 BlockInterfaceType type, int index,
783 const char *optstr)
785 QemuOpts *opts;
787 if (type == IF_DEFAULT) {
788 type = use_scsi ? IF_SCSI : IF_IDE;
791 if (!enable || drive_get_by_index(type, index)) {
792 return;
795 opts = drive_add(type, index, NULL, optstr);
796 if (snapshot) {
797 drive_enable_snapshot(opts, NULL);
799 if (!drive_init(opts, use_scsi)) {
800 exit(1);
804 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
806 boot_set_handler = func;
807 boot_set_opaque = opaque;
810 int qemu_boot_set(const char *boot_devices)
812 if (!boot_set_handler) {
813 return -EINVAL;
815 return boot_set_handler(boot_set_opaque, boot_devices);
818 static void validate_bootdevices(char *devices)
820 /* We just do some generic consistency checks */
821 const char *p;
822 int bitmap = 0;
824 for (p = devices; *p != '\0'; p++) {
825 /* Allowed boot devices are:
826 * a-b: floppy disk drives
827 * c-f: IDE disk drives
828 * g-m: machine implementation dependant drives
829 * n-p: network devices
830 * It's up to each machine implementation to check if the given boot
831 * devices match the actual hardware implementation and firmware
832 * features.
834 if (*p < 'a' || *p > 'p') {
835 fprintf(stderr, "Invalid boot device '%c'\n", *p);
836 exit(1);
838 if (bitmap & (1 << (*p - 'a'))) {
839 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
840 exit(1);
842 bitmap |= 1 << (*p - 'a');
846 static void restore_boot_devices(void *opaque)
848 char *standard_boot_devices = opaque;
849 static int first = 1;
851 /* Restore boot order and remove ourselves after the first boot */
852 if (first) {
853 first = 0;
854 return;
857 qemu_boot_set(standard_boot_devices);
859 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
860 g_free(standard_boot_devices);
863 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
864 const char *suffix)
866 FWBootEntry *node, *i;
868 if (bootindex < 0) {
869 return;
872 assert(dev != NULL || suffix != NULL);
874 node = g_malloc0(sizeof(FWBootEntry));
875 node->bootindex = bootindex;
876 node->suffix = suffix ? g_strdup(suffix) : NULL;
877 node->dev = dev;
879 QTAILQ_FOREACH(i, &fw_boot_order, link) {
880 if (i->bootindex == bootindex) {
881 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
882 exit(1);
883 } else if (i->bootindex < bootindex) {
884 continue;
886 QTAILQ_INSERT_BEFORE(i, node, link);
887 return;
889 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
893 * This function returns null terminated string that consist of new line
894 * separated device paths.
896 * memory pointed by "size" is assigned total length of the array in bytes
899 char *get_boot_devices_list(uint32_t *size)
901 FWBootEntry *i;
902 uint32_t total = 0;
903 char *list = NULL;
905 QTAILQ_FOREACH(i, &fw_boot_order, link) {
906 char *devpath = NULL, *bootpath;
907 int len;
909 if (i->dev) {
910 devpath = qdev_get_fw_dev_path(i->dev);
911 assert(devpath);
914 if (i->suffix && devpath) {
915 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
917 bootpath = g_malloc(bootpathlen);
918 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
919 g_free(devpath);
920 } else if (devpath) {
921 bootpath = devpath;
922 } else {
923 bootpath = g_strdup(i->suffix);
924 assert(bootpath);
927 if (total) {
928 list[total-1] = '\n';
930 len = strlen(bootpath) + 1;
931 list = g_realloc(list, total + len);
932 memcpy(&list[total], bootpath, len);
933 total += len;
934 g_free(bootpath);
937 *size = total;
939 return list;
942 static void numa_add(const char *optarg)
944 char option[128];
945 char *endptr;
946 unsigned long long value, endvalue;
947 int nodenr;
949 optarg = get_opt_name(option, 128, optarg, ',') + 1;
950 if (!strcmp(option, "node")) {
951 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
952 nodenr = nb_numa_nodes;
953 } else {
954 nodenr = strtoull(option, NULL, 10);
957 if (get_param_value(option, 128, "mem", optarg) == 0) {
958 node_mem[nodenr] = 0;
959 } else {
960 int64_t sval;
961 sval = strtosz(option, NULL);
962 if (sval < 0) {
963 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
964 exit(1);
966 node_mem[nodenr] = sval;
968 if (get_param_value(option, 128, "cpus", optarg) == 0) {
969 node_cpumask[nodenr] = 0;
970 } else {
971 value = strtoull(option, &endptr, 10);
972 if (value >= 64) {
973 value = 63;
974 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
975 } else {
976 if (*endptr == '-') {
977 endvalue = strtoull(endptr+1, &endptr, 10);
978 if (endvalue >= 63) {
979 endvalue = 62;
980 fprintf(stderr,
981 "only 63 CPUs in NUMA mode supported.\n");
983 value = (2ULL << endvalue) - (1ULL << value);
984 } else {
985 value = 1ULL << value;
988 node_cpumask[nodenr] = value;
990 nb_numa_nodes++;
992 return;
995 static void smp_parse(const char *optarg)
997 int smp, sockets = 0, threads = 0, cores = 0;
998 char *endptr;
999 char option[128];
1001 smp = strtoul(optarg, &endptr, 10);
1002 if (endptr != optarg) {
1003 if (*endptr == ',') {
1004 endptr++;
1007 if (get_param_value(option, 128, "sockets", endptr) != 0)
1008 sockets = strtoull(option, NULL, 10);
1009 if (get_param_value(option, 128, "cores", endptr) != 0)
1010 cores = strtoull(option, NULL, 10);
1011 if (get_param_value(option, 128, "threads", endptr) != 0)
1012 threads = strtoull(option, NULL, 10);
1013 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1014 max_cpus = strtoull(option, NULL, 10);
1016 /* compute missing values, prefer sockets over cores over threads */
1017 if (smp == 0 || sockets == 0) {
1018 sockets = sockets > 0 ? sockets : 1;
1019 cores = cores > 0 ? cores : 1;
1020 threads = threads > 0 ? threads : 1;
1021 if (smp == 0) {
1022 smp = cores * threads * sockets;
1024 } else {
1025 if (cores == 0) {
1026 threads = threads > 0 ? threads : 1;
1027 cores = smp / (sockets * threads);
1028 } else {
1029 threads = smp / (cores * sockets);
1032 smp_cpus = smp;
1033 smp_cores = cores > 0 ? cores : 1;
1034 smp_threads = threads > 0 ? threads : 1;
1035 if (max_cpus == 0)
1036 max_cpus = smp_cpus;
1039 /***********************************************************/
1040 /* USB devices */
1042 static int usb_device_add(const char *devname)
1044 const char *p;
1045 USBDevice *dev = NULL;
1047 if (!usb_enabled)
1048 return -1;
1050 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1051 dev = usbdevice_create(devname);
1052 if (dev)
1053 goto done;
1055 /* the other ones */
1056 #ifndef CONFIG_LINUX
1057 /* only the linux version is qdev-ified, usb-bsd still needs this */
1058 if (strstart(devname, "host:", &p)) {
1059 dev = usb_host_device_open(p);
1060 } else
1061 #endif
1062 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1063 dev = usb_bt_init(devname[2] ? hci_init(p) :
1064 bt_new_hci(qemu_find_bt_vlan(0)));
1065 } else {
1066 return -1;
1068 if (!dev)
1069 return -1;
1071 done:
1072 return 0;
1075 static int usb_device_del(const char *devname)
1077 int bus_num, addr;
1078 const char *p;
1080 if (strstart(devname, "host:", &p))
1081 return usb_host_device_close(p);
1083 if (!usb_enabled)
1084 return -1;
1086 p = strchr(devname, '.');
1087 if (!p)
1088 return -1;
1089 bus_num = strtoul(devname, NULL, 0);
1090 addr = strtoul(p + 1, NULL, 0);
1092 return usb_device_delete_addr(bus_num, addr);
1095 static int usb_parse(const char *cmdline)
1097 int r;
1098 r = usb_device_add(cmdline);
1099 if (r < 0) {
1100 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1102 return r;
1105 void do_usb_add(Monitor *mon, const QDict *qdict)
1107 const char *devname = qdict_get_str(qdict, "devname");
1108 if (usb_device_add(devname) < 0) {
1109 error_report("could not add USB device '%s'", devname);
1113 void do_usb_del(Monitor *mon, const QDict *qdict)
1115 const char *devname = qdict_get_str(qdict, "devname");
1116 if (usb_device_del(devname) < 0) {
1117 error_report("could not delete USB device '%s'", devname);
1121 /***********************************************************/
1122 /* PCMCIA/Cardbus */
1124 static struct pcmcia_socket_entry_s {
1125 PCMCIASocket *socket;
1126 struct pcmcia_socket_entry_s *next;
1127 } *pcmcia_sockets = 0;
1129 void pcmcia_socket_register(PCMCIASocket *socket)
1131 struct pcmcia_socket_entry_s *entry;
1133 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1134 entry->socket = socket;
1135 entry->next = pcmcia_sockets;
1136 pcmcia_sockets = entry;
1139 void pcmcia_socket_unregister(PCMCIASocket *socket)
1141 struct pcmcia_socket_entry_s *entry, **ptr;
1143 ptr = &pcmcia_sockets;
1144 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1145 if (entry->socket == socket) {
1146 *ptr = entry->next;
1147 g_free(entry);
1151 void pcmcia_info(Monitor *mon)
1153 struct pcmcia_socket_entry_s *iter;
1155 if (!pcmcia_sockets)
1156 monitor_printf(mon, "No PCMCIA sockets\n");
1158 for (iter = pcmcia_sockets; iter; iter = iter->next)
1159 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1160 iter->socket->attached ? iter->socket->card_string :
1161 "Empty");
1164 /***********************************************************/
1165 /* machine registration */
1167 static QEMUMachine *first_machine = NULL;
1168 QEMUMachine *current_machine = NULL;
1170 int qemu_register_machine(QEMUMachine *m)
1172 QEMUMachine **pm;
1173 pm = &first_machine;
1174 while (*pm != NULL)
1175 pm = &(*pm)->next;
1176 m->next = NULL;
1177 *pm = m;
1178 return 0;
1181 static QEMUMachine *find_machine(const char *name)
1183 QEMUMachine *m;
1185 for(m = first_machine; m != NULL; m = m->next) {
1186 if (!strcmp(m->name, name))
1187 return m;
1188 if (m->alias && !strcmp(m->alias, name))
1189 return m;
1191 return NULL;
1194 static QEMUMachine *find_default_machine(void)
1196 QEMUMachine *m;
1198 for(m = first_machine; m != NULL; m = m->next) {
1199 if (m->is_default) {
1200 return m;
1203 return NULL;
1206 /***********************************************************/
1207 /* main execution loop */
1209 static void gui_update(void *opaque)
1211 uint64_t interval = GUI_REFRESH_INTERVAL;
1212 DisplayState *ds = opaque;
1213 DisplayChangeListener *dcl = ds->listeners;
1215 qemu_flush_coalesced_mmio_buffer();
1216 dpy_refresh(ds);
1218 while (dcl != NULL) {
1219 if (dcl->gui_timer_interval &&
1220 dcl->gui_timer_interval < interval)
1221 interval = dcl->gui_timer_interval;
1222 dcl = dcl->next;
1224 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1227 static void nographic_update(void *opaque)
1229 uint64_t interval = GUI_REFRESH_INTERVAL;
1231 qemu_flush_coalesced_mmio_buffer();
1232 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1235 struct vm_change_state_entry {
1236 VMChangeStateHandler *cb;
1237 void *opaque;
1238 QLIST_ENTRY (vm_change_state_entry) entries;
1241 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1243 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1244 void *opaque)
1246 VMChangeStateEntry *e;
1248 e = g_malloc0(sizeof (*e));
1250 e->cb = cb;
1251 e->opaque = opaque;
1252 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1253 return e;
1256 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1258 QLIST_REMOVE (e, entries);
1259 g_free (e);
1262 void vm_state_notify(int running, RunState state)
1264 VMChangeStateEntry *e;
1266 trace_vm_state_notify(running, state);
1268 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1269 e->cb(e->opaque, running, state);
1273 void vm_start(void)
1275 if (!runstate_is_running()) {
1276 cpu_enable_ticks();
1277 runstate_set(RSTATE_RUNNING);
1278 vm_state_notify(1, RSTATE_RUNNING);
1279 resume_all_vcpus();
1280 monitor_protocol_event(QEVENT_RESUME, NULL);
1284 /* reset/shutdown handler */
1286 typedef struct QEMUResetEntry {
1287 QTAILQ_ENTRY(QEMUResetEntry) entry;
1288 QEMUResetHandler *func;
1289 void *opaque;
1290 } QEMUResetEntry;
1292 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1293 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1294 static int reset_requested;
1295 static int shutdown_requested, shutdown_signal = -1;
1296 static pid_t shutdown_pid;
1297 static int powerdown_requested;
1298 static int debug_requested;
1299 static RunState vmstop_requested = RSTATE_NO_STATE;
1301 int qemu_shutdown_requested_get(void)
1303 return shutdown_requested;
1306 int qemu_reset_requested_get(void)
1308 return reset_requested;
1311 int qemu_shutdown_requested(void)
1313 int r = shutdown_requested;
1314 shutdown_requested = 0;
1315 return r;
1318 void qemu_kill_report(void)
1320 if (shutdown_signal != -1) {
1321 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1322 if (shutdown_pid == 0) {
1323 /* This happens for eg ^C at the terminal, so it's worth
1324 * avoiding printing an odd message in that case.
1326 fputc('\n', stderr);
1327 } else {
1328 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1330 shutdown_signal = -1;
1334 int qemu_reset_requested(void)
1336 int r = reset_requested;
1337 reset_requested = 0;
1338 return r;
1341 int qemu_powerdown_requested(void)
1343 int r = powerdown_requested;
1344 powerdown_requested = 0;
1345 return r;
1348 static int qemu_debug_requested(void)
1350 int r = debug_requested;
1351 debug_requested = 0;
1352 return r;
1355 static RunState qemu_vmstop_requested(void)
1357 RunState s = vmstop_requested;
1358 vmstop_requested = RSTATE_NO_STATE;
1359 return s;
1362 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1364 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1366 re->func = func;
1367 re->opaque = opaque;
1368 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1371 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1373 QEMUResetEntry *re;
1375 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1376 if (re->func == func && re->opaque == opaque) {
1377 QTAILQ_REMOVE(&reset_handlers, re, entry);
1378 g_free(re);
1379 return;
1384 void qemu_system_reset(bool report)
1386 QEMUResetEntry *re, *nre;
1388 /* reset all devices */
1389 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1390 re->func(re->opaque);
1392 if (report) {
1393 monitor_protocol_event(QEVENT_RESET, NULL);
1395 cpu_synchronize_all_post_reset();
1398 void qemu_system_reset_request(void)
1400 if (no_reboot) {
1401 shutdown_requested = 1;
1402 } else {
1403 reset_requested = 1;
1405 cpu_stop_current();
1406 qemu_notify_event();
1409 void qemu_system_killed(int signal, pid_t pid)
1411 shutdown_signal = signal;
1412 shutdown_pid = pid;
1413 no_shutdown = 0;
1414 qemu_system_shutdown_request();
1417 void qemu_system_shutdown_request(void)
1419 shutdown_requested = 1;
1420 qemu_notify_event();
1423 void qemu_system_powerdown_request(void)
1425 powerdown_requested = 1;
1426 qemu_notify_event();
1429 void qemu_system_debug_request(void)
1431 debug_requested = 1;
1432 qemu_notify_event();
1435 void qemu_system_vmstop_request(RunState state)
1437 vmstop_requested = state;
1438 qemu_notify_event();
1441 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1442 static int n_poll_fds;
1443 static int max_priority;
1445 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1446 fd_set *xfds, struct timeval *tv)
1448 GMainContext *context = g_main_context_default();
1449 int i;
1450 int timeout = 0, cur_timeout;
1452 g_main_context_prepare(context, &max_priority);
1454 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1455 poll_fds, ARRAY_SIZE(poll_fds));
1456 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1458 for (i = 0; i < n_poll_fds; i++) {
1459 GPollFD *p = &poll_fds[i];
1461 if ((p->events & G_IO_IN)) {
1462 FD_SET(p->fd, rfds);
1463 *max_fd = MAX(*max_fd, p->fd);
1465 if ((p->events & G_IO_OUT)) {
1466 FD_SET(p->fd, wfds);
1467 *max_fd = MAX(*max_fd, p->fd);
1469 if ((p->events & G_IO_ERR)) {
1470 FD_SET(p->fd, xfds);
1471 *max_fd = MAX(*max_fd, p->fd);
1475 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1476 if (timeout >= 0 && timeout < cur_timeout) {
1477 tv->tv_sec = timeout / 1000;
1478 tv->tv_usec = (timeout % 1000) * 1000;
1482 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1483 bool err)
1485 GMainContext *context = g_main_context_default();
1487 if (!err) {
1488 int i;
1490 for (i = 0; i < n_poll_fds; i++) {
1491 GPollFD *p = &poll_fds[i];
1493 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1494 p->revents |= G_IO_IN;
1496 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1497 p->revents |= G_IO_OUT;
1499 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1500 p->revents |= G_IO_ERR;
1505 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1506 g_main_context_dispatch(context);
1510 int main_loop_wait(int nonblocking)
1512 fd_set rfds, wfds, xfds;
1513 int ret, nfds;
1514 struct timeval tv;
1515 int timeout;
1517 if (nonblocking)
1518 timeout = 0;
1519 else {
1520 timeout = qemu_calculate_timeout();
1521 qemu_bh_update_timeout(&timeout);
1524 os_host_main_loop_wait(&timeout);
1526 tv.tv_sec = timeout / 1000;
1527 tv.tv_usec = (timeout % 1000) * 1000;
1529 /* poll any events */
1530 /* XXX: separate device handlers from system ones */
1531 nfds = -1;
1532 FD_ZERO(&rfds);
1533 FD_ZERO(&wfds);
1534 FD_ZERO(&xfds);
1536 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1537 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1538 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
1540 if (timeout > 0) {
1541 qemu_mutex_unlock_iothread();
1544 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1546 if (timeout > 0) {
1547 qemu_mutex_lock_iothread();
1550 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1551 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1552 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1554 qemu_run_all_timers();
1556 /* Check bottom-halves last in case any of the earlier events triggered
1557 them. */
1558 qemu_bh_poll();
1560 return ret;
1563 qemu_irq qemu_system_powerdown;
1565 static void main_loop(void)
1567 bool nonblocking;
1568 int last_io __attribute__ ((unused)) = 0;
1569 #ifdef CONFIG_PROFILER
1570 int64_t ti;
1571 #endif
1572 int r;
1574 qemu_main_loop_start();
1576 for (;;) {
1577 nonblocking = !kvm_enabled() && last_io > 0;
1578 #ifdef CONFIG_PROFILER
1579 ti = profile_getclock();
1580 #endif
1581 last_io = main_loop_wait(nonblocking);
1582 #ifdef CONFIG_PROFILER
1583 dev_time += profile_getclock() - ti;
1584 #endif
1586 if (qemu_debug_requested()) {
1587 vm_stop(RSTATE_DEBUG);
1589 if (qemu_shutdown_requested()) {
1590 qemu_kill_report();
1591 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1592 if (no_shutdown) {
1593 vm_stop(RSTATE_SHUTDOWN);
1594 } else
1595 break;
1597 if (qemu_reset_requested()) {
1598 pause_all_vcpus();
1599 cpu_synchronize_all_states();
1600 qemu_system_reset(VMRESET_REPORT);
1601 resume_all_vcpus();
1602 if (runstate_check(RSTATE_PANICKED) ||
1603 runstate_check(RSTATE_SHUTDOWN)) {
1604 runstate_set(RSTATE_PAUSED);
1607 if (qemu_powerdown_requested()) {
1608 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1609 qemu_irq_raise(qemu_system_powerdown);
1611 if ((r = qemu_vmstop_requested())) {
1612 vm_stop(r);
1615 bdrv_close_all();
1616 pause_all_vcpus();
1619 static void version(void)
1621 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1624 static void help(int exitcode)
1626 const char *options_help =
1627 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1628 opt_help
1629 #define DEFHEADING(text) stringify(text) "\n"
1630 #include "qemu-options.def"
1631 #undef DEF
1632 #undef DEFHEADING
1633 #undef GEN_DOCS
1635 version();
1636 printf("usage: %s [options] [disk_image]\n"
1637 "\n"
1638 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1639 "\n"
1640 "%s\n"
1641 "During emulation, the following keys are useful:\n"
1642 "ctrl-alt-f toggle full screen\n"
1643 "ctrl-alt-n switch to virtual console 'n'\n"
1644 "ctrl-alt toggle mouse and keyboard grab\n"
1645 "\n"
1646 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1647 "qemu",
1648 options_help);
1649 exit(exitcode);
1652 #define HAS_ARG 0x0001
1654 typedef struct QEMUOption {
1655 const char *name;
1656 int flags;
1657 int index;
1658 uint32_t arch_mask;
1659 } QEMUOption;
1661 static const QEMUOption qemu_options[] = {
1662 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1663 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1664 { option, opt_arg, opt_enum, arch_mask },
1665 #define DEFHEADING(text)
1666 #include "qemu-options.def"
1667 #undef DEF
1668 #undef DEFHEADING
1669 #undef GEN_DOCS
1670 { NULL },
1672 static void select_vgahw (const char *p)
1674 const char *opts;
1676 default_vga = 0;
1677 vga_interface_type = VGA_NONE;
1678 if (strstart(p, "std", &opts)) {
1679 vga_interface_type = VGA_STD;
1680 } else if (strstart(p, "cirrus", &opts)) {
1681 vga_interface_type = VGA_CIRRUS;
1682 } else if (strstart(p, "vmware", &opts)) {
1683 vga_interface_type = VGA_VMWARE;
1684 } else if (strstart(p, "xenfb", &opts)) {
1685 vga_interface_type = VGA_XENFB;
1686 } else if (strstart(p, "qxl", &opts)) {
1687 vga_interface_type = VGA_QXL;
1688 } else if (!strstart(p, "none", &opts)) {
1689 invalid_vga:
1690 fprintf(stderr, "Unknown vga type: %s\n", p);
1691 exit(1);
1693 while (*opts) {
1694 const char *nextopt;
1696 if (strstart(opts, ",retrace=", &nextopt)) {
1697 opts = nextopt;
1698 if (strstart(opts, "dumb", &nextopt))
1699 vga_retrace_method = VGA_RETRACE_DUMB;
1700 else if (strstart(opts, "precise", &nextopt))
1701 vga_retrace_method = VGA_RETRACE_PRECISE;
1702 else goto invalid_vga;
1703 } else goto invalid_vga;
1704 opts = nextopt;
1708 static DisplayType select_display(const char *p)
1710 const char *opts;
1711 DisplayType display = DT_DEFAULT;
1713 if (strstart(p, "sdl", &opts)) {
1714 #ifdef CONFIG_SDL
1715 display = DT_SDL;
1716 while (*opts) {
1717 const char *nextopt;
1719 if (strstart(opts, ",frame=", &nextopt)) {
1720 opts = nextopt;
1721 if (strstart(opts, "on", &nextopt)) {
1722 no_frame = 0;
1723 } else if (strstart(opts, "off", &nextopt)) {
1724 no_frame = 1;
1725 } else {
1726 goto invalid_sdl_args;
1728 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1729 opts = nextopt;
1730 if (strstart(opts, "on", &nextopt)) {
1731 alt_grab = 1;
1732 } else if (strstart(opts, "off", &nextopt)) {
1733 alt_grab = 0;
1734 } else {
1735 goto invalid_sdl_args;
1737 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1738 opts = nextopt;
1739 if (strstart(opts, "on", &nextopt)) {
1740 ctrl_grab = 1;
1741 } else if (strstart(opts, "off", &nextopt)) {
1742 ctrl_grab = 0;
1743 } else {
1744 goto invalid_sdl_args;
1746 } else if (strstart(opts, ",window_close=", &nextopt)) {
1747 opts = nextopt;
1748 if (strstart(opts, "on", &nextopt)) {
1749 no_quit = 0;
1750 } else if (strstart(opts, "off", &nextopt)) {
1751 no_quit = 1;
1752 } else {
1753 goto invalid_sdl_args;
1755 } else {
1756 invalid_sdl_args:
1757 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1758 exit(1);
1760 opts = nextopt;
1762 #else
1763 fprintf(stderr, "SDL support is disabled\n");
1764 exit(1);
1765 #endif
1766 } else if (strstart(p, "vnc", &opts)) {
1767 #ifdef CONFIG_VNC
1768 display_remote++;
1770 if (*opts) {
1771 const char *nextopt;
1773 if (strstart(opts, "=", &nextopt)) {
1774 vnc_display = nextopt;
1777 if (!vnc_display) {
1778 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1779 exit(1);
1781 #else
1782 fprintf(stderr, "VNC support is disabled\n");
1783 exit(1);
1784 #endif
1785 } else if (strstart(p, "curses", &opts)) {
1786 #ifdef CONFIG_CURSES
1787 display = DT_CURSES;
1788 #else
1789 fprintf(stderr, "Curses support is disabled\n");
1790 exit(1);
1791 #endif
1792 } else if (strstart(p, "none", &opts)) {
1793 display = DT_NONE;
1794 } else {
1795 fprintf(stderr, "Unknown display type: %s\n", p);
1796 exit(1);
1799 return display;
1802 static int balloon_parse(const char *arg)
1804 QemuOpts *opts;
1806 if (strcmp(arg, "none") == 0) {
1807 return 0;
1810 if (!strncmp(arg, "virtio", 6)) {
1811 if (arg[6] == ',') {
1812 /* have params -> parse them */
1813 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1814 if (!opts)
1815 return -1;
1816 } else {
1817 /* create empty opts */
1818 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1820 qemu_opt_set(opts, "driver", "virtio-balloon");
1821 return 0;
1824 return -1;
1827 char *qemu_find_file(int type, const char *name)
1829 int len;
1830 const char *subdir;
1831 char *buf;
1833 /* If name contains path separators then try it as a straight path. */
1834 if ((strchr(name, '/') || strchr(name, '\\'))
1835 && access(name, R_OK) == 0) {
1836 return g_strdup(name);
1838 switch (type) {
1839 case QEMU_FILE_TYPE_BIOS:
1840 subdir = "";
1841 break;
1842 case QEMU_FILE_TYPE_KEYMAP:
1843 subdir = "keymaps/";
1844 break;
1845 default:
1846 abort();
1848 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1849 buf = g_malloc0(len);
1850 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1851 if (access(buf, R_OK)) {
1852 g_free(buf);
1853 return NULL;
1855 return buf;
1858 static int device_help_func(QemuOpts *opts, void *opaque)
1860 return qdev_device_help(opts);
1863 static int device_init_func(QemuOpts *opts, void *opaque)
1865 DeviceState *dev;
1867 dev = qdev_device_add(opts);
1868 if (!dev)
1869 return -1;
1870 return 0;
1873 static int chardev_init_func(QemuOpts *opts, void *opaque)
1875 CharDriverState *chr;
1877 chr = qemu_chr_new_from_opts(opts, NULL);
1878 if (!chr)
1879 return -1;
1880 return 0;
1883 #ifdef CONFIG_VIRTFS
1884 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1886 int ret;
1887 ret = qemu_fsdev_add(opts);
1889 return ret;
1891 #endif
1893 static int mon_init_func(QemuOpts *opts, void *opaque)
1895 CharDriverState *chr;
1896 const char *chardev;
1897 const char *mode;
1898 int flags;
1900 mode = qemu_opt_get(opts, "mode");
1901 if (mode == NULL) {
1902 mode = "readline";
1904 if (strcmp(mode, "readline") == 0) {
1905 flags = MONITOR_USE_READLINE;
1906 } else if (strcmp(mode, "control") == 0) {
1907 flags = MONITOR_USE_CONTROL;
1908 } else {
1909 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1910 exit(1);
1913 if (qemu_opt_get_bool(opts, "pretty", 0))
1914 flags |= MONITOR_USE_PRETTY;
1916 if (qemu_opt_get_bool(opts, "default", 0))
1917 flags |= MONITOR_IS_DEFAULT;
1919 chardev = qemu_opt_get(opts, "chardev");
1920 chr = qemu_chr_find(chardev);
1921 if (chr == NULL) {
1922 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1923 exit(1);
1926 monitor_init(chr, flags);
1927 return 0;
1930 static void monitor_parse(const char *optarg, const char *mode)
1932 static int monitor_device_index = 0;
1933 QemuOpts *opts;
1934 const char *p;
1935 char label[32];
1936 int def = 0;
1938 if (strstart(optarg, "chardev:", &p)) {
1939 snprintf(label, sizeof(label), "%s", p);
1940 } else {
1941 snprintf(label, sizeof(label), "compat_monitor%d",
1942 monitor_device_index);
1943 if (monitor_device_index == 0) {
1944 def = 1;
1946 opts = qemu_chr_parse_compat(label, optarg);
1947 if (!opts) {
1948 fprintf(stderr, "parse error: %s\n", optarg);
1949 exit(1);
1953 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1954 if (!opts) {
1955 fprintf(stderr, "duplicate chardev: %s\n", label);
1956 exit(1);
1958 qemu_opt_set(opts, "mode", mode);
1959 qemu_opt_set(opts, "chardev", label);
1960 if (def)
1961 qemu_opt_set(opts, "default", "on");
1962 monitor_device_index++;
1965 struct device_config {
1966 enum {
1967 DEV_USB, /* -usbdevice */
1968 DEV_BT, /* -bt */
1969 DEV_SERIAL, /* -serial */
1970 DEV_PARALLEL, /* -parallel */
1971 DEV_VIRTCON, /* -virtioconsole */
1972 DEV_DEBUGCON, /* -debugcon */
1973 } type;
1974 const char *cmdline;
1975 QTAILQ_ENTRY(device_config) next;
1977 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1979 static void add_device_config(int type, const char *cmdline)
1981 struct device_config *conf;
1983 conf = g_malloc0(sizeof(*conf));
1984 conf->type = type;
1985 conf->cmdline = cmdline;
1986 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1989 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1991 struct device_config *conf;
1992 int rc;
1994 QTAILQ_FOREACH(conf, &device_configs, next) {
1995 if (conf->type != type)
1996 continue;
1997 rc = func(conf->cmdline);
1998 if (0 != rc)
1999 return rc;
2001 return 0;
2004 static int serial_parse(const char *devname)
2006 static int index = 0;
2007 char label[32];
2009 if (strcmp(devname, "none") == 0)
2010 return 0;
2011 if (index == MAX_SERIAL_PORTS) {
2012 fprintf(stderr, "qemu: too many serial ports\n");
2013 exit(1);
2015 snprintf(label, sizeof(label), "serial%d", index);
2016 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2017 if (!serial_hds[index]) {
2018 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2019 devname, strerror(errno));
2020 return -1;
2022 index++;
2023 return 0;
2026 static int parallel_parse(const char *devname)
2028 static int index = 0;
2029 char label[32];
2031 if (strcmp(devname, "none") == 0)
2032 return 0;
2033 if (index == MAX_PARALLEL_PORTS) {
2034 fprintf(stderr, "qemu: too many parallel ports\n");
2035 exit(1);
2037 snprintf(label, sizeof(label), "parallel%d", index);
2038 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2039 if (!parallel_hds[index]) {
2040 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2041 devname, strerror(errno));
2042 return -1;
2044 index++;
2045 return 0;
2048 static int virtcon_parse(const char *devname)
2050 QemuOptsList *device = qemu_find_opts("device");
2051 static int index = 0;
2052 char label[32];
2053 QemuOpts *bus_opts, *dev_opts;
2055 if (strcmp(devname, "none") == 0)
2056 return 0;
2057 if (index == MAX_VIRTIO_CONSOLES) {
2058 fprintf(stderr, "qemu: too many virtio consoles\n");
2059 exit(1);
2062 bus_opts = qemu_opts_create(device, NULL, 0);
2063 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2065 dev_opts = qemu_opts_create(device, NULL, 0);
2066 qemu_opt_set(dev_opts, "driver", "virtconsole");
2068 snprintf(label, sizeof(label), "virtcon%d", index);
2069 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2070 if (!virtcon_hds[index]) {
2071 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2072 devname, strerror(errno));
2073 return -1;
2075 qemu_opt_set(dev_opts, "chardev", label);
2077 index++;
2078 return 0;
2081 static int debugcon_parse(const char *devname)
2083 QemuOpts *opts;
2085 if (!qemu_chr_new("debugcon", devname, NULL)) {
2086 exit(1);
2088 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2089 if (!opts) {
2090 fprintf(stderr, "qemu: already have a debugcon device\n");
2091 exit(1);
2093 qemu_opt_set(opts, "driver", "isa-debugcon");
2094 qemu_opt_set(opts, "chardev", "debugcon");
2095 return 0;
2098 static QEMUMachine *machine_parse(const char *name)
2100 QEMUMachine *m, *machine = NULL;
2102 if (name) {
2103 machine = find_machine(name);
2105 if (machine) {
2106 return machine;
2108 printf("Supported machines are:\n");
2109 for (m = first_machine; m != NULL; m = m->next) {
2110 if (m->alias) {
2111 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2113 printf("%-10s %s%s\n", m->name, m->desc,
2114 m->is_default ? " (default)" : "");
2116 exit(!name || *name != '?');
2119 static int tcg_init(void)
2121 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2122 return 0;
2125 static struct {
2126 const char *opt_name;
2127 const char *name;
2128 int (*available)(void);
2129 int (*init)(void);
2130 int *allowed;
2131 } accel_list[] = {
2132 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2133 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2134 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2137 static int configure_accelerator(void)
2139 const char *p = NULL;
2140 char buf[10];
2141 int i, ret;
2142 bool accel_initalised = 0;
2143 bool init_failed = 0;
2145 QemuOptsList *list = qemu_find_opts("machine");
2146 if (!QTAILQ_EMPTY(&list->head)) {
2147 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2150 if (p == NULL) {
2151 #ifdef CONFIG_KVM
2152 /* Use the default "accelerator", kvm */
2153 p = "kvm";
2154 #else
2155 /* Use the default "accelerator", tcg */
2156 p = "tcg";
2157 #endif
2160 while (!accel_initalised && *p != '\0') {
2161 if (*p == ':') {
2162 p++;
2164 p = get_opt_name(buf, sizeof (buf), p, ':');
2165 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2166 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2167 *(accel_list[i].allowed) = 1;
2168 ret = accel_list[i].init();
2169 if (ret < 0) {
2170 init_failed = 1;
2171 if (!accel_list[i].available()) {
2172 printf("%s not supported for this target\n",
2173 accel_list[i].name);
2174 } else {
2175 fprintf(stderr, "failed to initialize %s: %s\n",
2176 accel_list[i].name,
2177 strerror(-ret));
2179 *(accel_list[i].allowed) = 0;
2180 } else {
2181 accel_initalised = 1;
2183 break;
2186 if (i == ARRAY_SIZE(accel_list)) {
2187 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2191 if (!accel_initalised) {
2192 fprintf(stderr, "No accelerator found!\n");
2193 exit(1);
2196 if (init_failed) {
2197 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2200 return !accel_initalised;
2203 void qemu_add_exit_notifier(Notifier *notify)
2205 notifier_list_add(&exit_notifiers, notify);
2208 void qemu_remove_exit_notifier(Notifier *notify)
2210 notifier_list_remove(&exit_notifiers, notify);
2213 static void qemu_run_exit_notifiers(void)
2215 notifier_list_notify(&exit_notifiers, NULL);
2218 void qemu_add_machine_init_done_notifier(Notifier *notify)
2220 notifier_list_add(&machine_init_done_notifiers, notify);
2223 static void qemu_run_machine_init_done_notifiers(void)
2225 notifier_list_notify(&machine_init_done_notifiers, NULL);
2228 static const QEMUOption *lookup_opt(int argc, char **argv,
2229 const char **poptarg, int *poptind)
2231 const QEMUOption *popt;
2232 int optind = *poptind;
2233 char *r = argv[optind];
2234 const char *optarg;
2236 loc_set_cmdline(argv, optind, 1);
2237 optind++;
2238 /* Treat --foo the same as -foo. */
2239 if (r[1] == '-')
2240 r++;
2241 popt = qemu_options;
2242 for(;;) {
2243 if (!popt->name) {
2244 error_report("invalid option");
2245 exit(1);
2247 if (!strcmp(popt->name, r + 1))
2248 break;
2249 popt++;
2251 if (popt->flags & HAS_ARG) {
2252 if (optind >= argc) {
2253 error_report("requires an argument");
2254 exit(1);
2256 optarg = argv[optind++];
2257 loc_set_cmdline(argv, optind - 2, 2);
2258 } else {
2259 optarg = NULL;
2262 *poptarg = optarg;
2263 *poptind = optind;
2265 return popt;
2268 static gpointer malloc_and_trace(gsize n_bytes)
2270 void *ptr = malloc(n_bytes);
2271 trace_g_malloc(n_bytes, ptr);
2272 return ptr;
2275 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2277 void *ptr = realloc(mem, n_bytes);
2278 trace_g_realloc(mem, n_bytes, ptr);
2279 return ptr;
2282 static void free_and_trace(gpointer mem)
2284 trace_g_free(mem);
2285 free(mem);
2288 int main(int argc, char **argv, char **envp)
2290 const char *gdbstub_dev = NULL;
2291 int i;
2292 int snapshot, linux_boot;
2293 const char *icount_option = NULL;
2294 const char *initrd_filename;
2295 const char *kernel_filename, *kernel_cmdline;
2296 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2297 DisplayState *ds;
2298 DisplayChangeListener *dcl;
2299 int cyls, heads, secs, translation;
2300 QemuOpts *hda_opts = NULL, *opts;
2301 QemuOptsList *olist;
2302 int optind;
2303 const char *optarg;
2304 const char *loadvm = NULL;
2305 QEMUMachine *machine;
2306 const char *cpu_model;
2307 const char *pid_file = NULL;
2308 const char *incoming = NULL;
2309 #ifdef CONFIG_VNC
2310 int show_vnc_port = 0;
2311 #endif
2312 int defconfig = 1;
2313 const char *log_mask = NULL;
2314 const char *log_file = NULL;
2315 GMemVTable mem_trace = {
2316 .malloc = malloc_and_trace,
2317 .realloc = realloc_and_trace,
2318 .free = free_and_trace,
2320 const char *trace_events = NULL;
2321 const char *trace_file = NULL;
2323 atexit(qemu_run_exit_notifiers);
2324 error_set_progname(argv[0]);
2326 g_mem_set_vtable(&mem_trace);
2327 g_thread_init(NULL);
2329 runstate_init();
2331 init_clocks();
2333 qemu_cache_utils_init(envp);
2335 QLIST_INIT (&vm_change_state_head);
2336 os_setup_early_signal_handling();
2338 module_call_init(MODULE_INIT_MACHINE);
2339 machine = find_default_machine();
2340 cpu_model = NULL;
2341 initrd_filename = NULL;
2342 ram_size = 0;
2343 snapshot = 0;
2344 kernel_filename = NULL;
2345 kernel_cmdline = "";
2346 cyls = heads = secs = 0;
2347 translation = BIOS_ATA_TRANSLATION_AUTO;
2349 for (i = 0; i < MAX_NODES; i++) {
2350 node_mem[i] = 0;
2351 node_cpumask[i] = 0;
2354 nb_numa_nodes = 0;
2355 nb_nics = 0;
2357 autostart= 1;
2359 /* first pass of option parsing */
2360 optind = 1;
2361 while (optind < argc) {
2362 if (argv[optind][0] != '-') {
2363 /* disk image */
2364 optind++;
2365 continue;
2366 } else {
2367 const QEMUOption *popt;
2369 popt = lookup_opt(argc, argv, &optarg, &optind);
2370 switch (popt->index) {
2371 case QEMU_OPTION_nodefconfig:
2372 defconfig=0;
2373 break;
2378 if (defconfig) {
2379 int ret;
2381 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2382 if (ret < 0 && ret != -ENOENT) {
2383 exit(1);
2386 ret = qemu_read_config_file(arch_config_name);
2387 if (ret < 0 && ret != -ENOENT) {
2388 exit(1);
2391 cpudef_init();
2393 /* second pass of option parsing */
2394 optind = 1;
2395 for(;;) {
2396 if (optind >= argc)
2397 break;
2398 if (argv[optind][0] != '-') {
2399 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2400 } else {
2401 const QEMUOption *popt;
2403 popt = lookup_opt(argc, argv, &optarg, &optind);
2404 if (!(popt->arch_mask & arch_type)) {
2405 printf("Option %s not supported for this target\n", popt->name);
2406 exit(1);
2408 switch(popt->index) {
2409 case QEMU_OPTION_M:
2410 machine = machine_parse(optarg);
2411 break;
2412 case QEMU_OPTION_cpu:
2413 /* hw initialization will check this */
2414 if (*optarg == '?') {
2415 list_cpus(stdout, &fprintf, optarg);
2416 exit(0);
2417 } else {
2418 cpu_model = optarg;
2420 break;
2421 case QEMU_OPTION_initrd:
2422 initrd_filename = optarg;
2423 break;
2424 case QEMU_OPTION_hda:
2426 char buf[256];
2427 if (cyls == 0)
2428 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2429 else
2430 snprintf(buf, sizeof(buf),
2431 "%s,cyls=%d,heads=%d,secs=%d%s",
2432 HD_OPTS , cyls, heads, secs,
2433 translation == BIOS_ATA_TRANSLATION_LBA ?
2434 ",trans=lba" :
2435 translation == BIOS_ATA_TRANSLATION_NONE ?
2436 ",trans=none" : "");
2437 drive_add(IF_DEFAULT, 0, optarg, buf);
2438 break;
2440 case QEMU_OPTION_hdb:
2441 case QEMU_OPTION_hdc:
2442 case QEMU_OPTION_hdd:
2443 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2444 HD_OPTS);
2445 break;
2446 case QEMU_OPTION_drive:
2447 if (drive_def(optarg) == NULL) {
2448 exit(1);
2450 break;
2451 case QEMU_OPTION_set:
2452 if (qemu_set_option(optarg) != 0)
2453 exit(1);
2454 break;
2455 case QEMU_OPTION_global:
2456 if (qemu_global_option(optarg) != 0)
2457 exit(1);
2458 break;
2459 case QEMU_OPTION_mtdblock:
2460 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2461 break;
2462 case QEMU_OPTION_sd:
2463 drive_add(IF_SD, 0, optarg, SD_OPTS);
2464 break;
2465 case QEMU_OPTION_pflash:
2466 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2467 break;
2468 case QEMU_OPTION_snapshot:
2469 snapshot = 1;
2470 break;
2471 case QEMU_OPTION_hdachs:
2473 const char *p;
2474 p = optarg;
2475 cyls = strtol(p, (char **)&p, 0);
2476 if (cyls < 1 || cyls > 16383)
2477 goto chs_fail;
2478 if (*p != ',')
2479 goto chs_fail;
2480 p++;
2481 heads = strtol(p, (char **)&p, 0);
2482 if (heads < 1 || heads > 16)
2483 goto chs_fail;
2484 if (*p != ',')
2485 goto chs_fail;
2486 p++;
2487 secs = strtol(p, (char **)&p, 0);
2488 if (secs < 1 || secs > 63)
2489 goto chs_fail;
2490 if (*p == ',') {
2491 p++;
2492 if (!strcmp(p, "none"))
2493 translation = BIOS_ATA_TRANSLATION_NONE;
2494 else if (!strcmp(p, "lba"))
2495 translation = BIOS_ATA_TRANSLATION_LBA;
2496 else if (!strcmp(p, "auto"))
2497 translation = BIOS_ATA_TRANSLATION_AUTO;
2498 else
2499 goto chs_fail;
2500 } else if (*p != '\0') {
2501 chs_fail:
2502 fprintf(stderr, "qemu: invalid physical CHS format\n");
2503 exit(1);
2505 if (hda_opts != NULL) {
2506 char num[16];
2507 snprintf(num, sizeof(num), "%d", cyls);
2508 qemu_opt_set(hda_opts, "cyls", num);
2509 snprintf(num, sizeof(num), "%d", heads);
2510 qemu_opt_set(hda_opts, "heads", num);
2511 snprintf(num, sizeof(num), "%d", secs);
2512 qemu_opt_set(hda_opts, "secs", num);
2513 if (translation == BIOS_ATA_TRANSLATION_LBA)
2514 qemu_opt_set(hda_opts, "trans", "lba");
2515 if (translation == BIOS_ATA_TRANSLATION_NONE)
2516 qemu_opt_set(hda_opts, "trans", "none");
2519 break;
2520 case QEMU_OPTION_numa:
2521 if (nb_numa_nodes >= MAX_NODES) {
2522 fprintf(stderr, "qemu: too many NUMA nodes\n");
2523 exit(1);
2525 numa_add(optarg);
2526 break;
2527 case QEMU_OPTION_display:
2528 display_type = select_display(optarg);
2529 break;
2530 case QEMU_OPTION_nographic:
2531 display_type = DT_NOGRAPHIC;
2532 break;
2533 case QEMU_OPTION_curses:
2534 #ifdef CONFIG_CURSES
2535 display_type = DT_CURSES;
2536 #else
2537 fprintf(stderr, "Curses support is disabled\n");
2538 exit(1);
2539 #endif
2540 break;
2541 case QEMU_OPTION_portrait:
2542 graphic_rotate = 90;
2543 break;
2544 case QEMU_OPTION_rotate:
2545 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2546 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2547 graphic_rotate != 180 && graphic_rotate != 270) {
2548 fprintf(stderr,
2549 "qemu: only 90, 180, 270 deg rotation is available\n");
2550 exit(1);
2552 break;
2553 case QEMU_OPTION_kernel:
2554 kernel_filename = optarg;
2555 break;
2556 case QEMU_OPTION_append:
2557 kernel_cmdline = optarg;
2558 break;
2559 case QEMU_OPTION_cdrom:
2560 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2561 break;
2562 case QEMU_OPTION_boot:
2564 static const char * const params[] = {
2565 "order", "once", "menu",
2566 "splash", "splash-time", NULL
2568 char buf[sizeof(boot_devices)];
2569 char *standard_boot_devices;
2570 int legacy = 0;
2572 if (!strchr(optarg, '=')) {
2573 legacy = 1;
2574 pstrcpy(buf, sizeof(buf), optarg);
2575 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2576 fprintf(stderr,
2577 "qemu: unknown boot parameter '%s' in '%s'\n",
2578 buf, optarg);
2579 exit(1);
2582 if (legacy ||
2583 get_param_value(buf, sizeof(buf), "order", optarg)) {
2584 validate_bootdevices(buf);
2585 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2587 if (!legacy) {
2588 if (get_param_value(buf, sizeof(buf),
2589 "once", optarg)) {
2590 validate_bootdevices(buf);
2591 standard_boot_devices = g_strdup(boot_devices);
2592 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2593 qemu_register_reset(restore_boot_devices,
2594 standard_boot_devices);
2596 if (get_param_value(buf, sizeof(buf),
2597 "menu", optarg)) {
2598 if (!strcmp(buf, "on")) {
2599 boot_menu = 1;
2600 } else if (!strcmp(buf, "off")) {
2601 boot_menu = 0;
2602 } else {
2603 fprintf(stderr,
2604 "qemu: invalid option value '%s'\n",
2605 buf);
2606 exit(1);
2609 qemu_opts_parse(qemu_find_opts("boot-opts"),
2610 optarg, 0);
2613 break;
2614 case QEMU_OPTION_fda:
2615 case QEMU_OPTION_fdb:
2616 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2617 optarg, FD_OPTS);
2618 break;
2619 case QEMU_OPTION_no_fd_bootchk:
2620 fd_bootchk = 0;
2621 break;
2622 case QEMU_OPTION_netdev:
2623 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2624 exit(1);
2626 break;
2627 case QEMU_OPTION_net:
2628 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2629 exit(1);
2631 break;
2632 #ifdef CONFIG_SLIRP
2633 case QEMU_OPTION_tftp:
2634 legacy_tftp_prefix = optarg;
2635 break;
2636 case QEMU_OPTION_bootp:
2637 legacy_bootp_filename = optarg;
2638 break;
2639 case QEMU_OPTION_redir:
2640 if (net_slirp_redir(optarg) < 0)
2641 exit(1);
2642 break;
2643 #endif
2644 case QEMU_OPTION_bt:
2645 add_device_config(DEV_BT, optarg);
2646 break;
2647 case QEMU_OPTION_audio_help:
2648 if (!(audio_available())) {
2649 printf("Option %s not supported for this target\n", popt->name);
2650 exit(1);
2652 AUD_help ();
2653 exit (0);
2654 break;
2655 case QEMU_OPTION_soundhw:
2656 if (!(audio_available())) {
2657 printf("Option %s not supported for this target\n", popt->name);
2658 exit(1);
2660 select_soundhw (optarg);
2661 break;
2662 case QEMU_OPTION_h:
2663 help(0);
2664 break;
2665 case QEMU_OPTION_version:
2666 version();
2667 exit(0);
2668 break;
2669 case QEMU_OPTION_m: {
2670 int64_t value;
2672 value = strtosz(optarg, NULL);
2673 if (value < 0) {
2674 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2675 exit(1);
2678 if (value != (uint64_t)(ram_addr_t)value) {
2679 fprintf(stderr, "qemu: ram size too large\n");
2680 exit(1);
2682 ram_size = value;
2683 break;
2685 case QEMU_OPTION_mempath:
2686 mem_path = optarg;
2687 break;
2688 #ifdef MAP_POPULATE
2689 case QEMU_OPTION_mem_prealloc:
2690 mem_prealloc = 1;
2691 break;
2692 #endif
2693 case QEMU_OPTION_d:
2694 log_mask = optarg;
2695 break;
2696 case QEMU_OPTION_D:
2697 log_file = optarg;
2698 break;
2699 case QEMU_OPTION_s:
2700 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2701 break;
2702 case QEMU_OPTION_gdb:
2703 gdbstub_dev = optarg;
2704 break;
2705 case QEMU_OPTION_L:
2706 data_dir = optarg;
2707 break;
2708 case QEMU_OPTION_bios:
2709 bios_name = optarg;
2710 break;
2711 case QEMU_OPTION_singlestep:
2712 singlestep = 1;
2713 break;
2714 case QEMU_OPTION_S:
2715 autostart = 0;
2716 break;
2717 case QEMU_OPTION_k:
2718 keyboard_layout = optarg;
2719 break;
2720 case QEMU_OPTION_localtime:
2721 rtc_utc = 0;
2722 break;
2723 case QEMU_OPTION_vga:
2724 select_vgahw (optarg);
2725 break;
2726 case QEMU_OPTION_g:
2728 const char *p;
2729 int w, h, depth;
2730 p = optarg;
2731 w = strtol(p, (char **)&p, 10);
2732 if (w <= 0) {
2733 graphic_error:
2734 fprintf(stderr, "qemu: invalid resolution or depth\n");
2735 exit(1);
2737 if (*p != 'x')
2738 goto graphic_error;
2739 p++;
2740 h = strtol(p, (char **)&p, 10);
2741 if (h <= 0)
2742 goto graphic_error;
2743 if (*p == 'x') {
2744 p++;
2745 depth = strtol(p, (char **)&p, 10);
2746 if (depth != 8 && depth != 15 && depth != 16 &&
2747 depth != 24 && depth != 32)
2748 goto graphic_error;
2749 } else if (*p == '\0') {
2750 depth = graphic_depth;
2751 } else {
2752 goto graphic_error;
2755 graphic_width = w;
2756 graphic_height = h;
2757 graphic_depth = depth;
2759 break;
2760 case QEMU_OPTION_echr:
2762 char *r;
2763 term_escape_char = strtol(optarg, &r, 0);
2764 if (r == optarg)
2765 printf("Bad argument to echr\n");
2766 break;
2768 case QEMU_OPTION_monitor:
2769 monitor_parse(optarg, "readline");
2770 default_monitor = 0;
2771 break;
2772 case QEMU_OPTION_qmp:
2773 monitor_parse(optarg, "control");
2774 default_monitor = 0;
2775 break;
2776 case QEMU_OPTION_mon:
2777 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2778 if (!opts) {
2779 exit(1);
2781 default_monitor = 0;
2782 break;
2783 case QEMU_OPTION_chardev:
2784 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2785 if (!opts) {
2786 exit(1);
2788 break;
2789 case QEMU_OPTION_fsdev:
2790 olist = qemu_find_opts("fsdev");
2791 if (!olist) {
2792 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2793 exit(1);
2795 opts = qemu_opts_parse(olist, optarg, 1);
2796 if (!opts) {
2797 fprintf(stderr, "parse error: %s\n", optarg);
2798 exit(1);
2800 break;
2801 case QEMU_OPTION_virtfs: {
2802 QemuOpts *fsdev;
2803 QemuOpts *device;
2805 olist = qemu_find_opts("virtfs");
2806 if (!olist) {
2807 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2808 exit(1);
2810 opts = qemu_opts_parse(olist, optarg, 1);
2811 if (!opts) {
2812 fprintf(stderr, "parse error: %s\n", optarg);
2813 exit(1);
2816 if (qemu_opt_get(opts, "fstype") == NULL ||
2817 qemu_opt_get(opts, "mount_tag") == NULL ||
2818 qemu_opt_get(opts, "path") == NULL ||
2819 qemu_opt_get(opts, "security_model") == NULL) {
2820 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2821 "security_model=[mapped|passthrough|none],"
2822 "mount_tag=tag.\n");
2823 exit(1);
2826 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2827 qemu_opt_get(opts, "mount_tag"), 1);
2828 if (!fsdev) {
2829 fprintf(stderr, "duplicate fsdev id: %s\n",
2830 qemu_opt_get(opts, "mount_tag"));
2831 exit(1);
2833 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2834 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2835 qemu_opt_set(fsdev, "security_model",
2836 qemu_opt_get(opts, "security_model"));
2838 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2839 qemu_opt_set(device, "driver", "virtio-9p-pci");
2840 qemu_opt_set(device, "fsdev",
2841 qemu_opt_get(opts, "mount_tag"));
2842 qemu_opt_set(device, "mount_tag",
2843 qemu_opt_get(opts, "mount_tag"));
2844 break;
2846 case QEMU_OPTION_serial:
2847 add_device_config(DEV_SERIAL, optarg);
2848 default_serial = 0;
2849 if (strncmp(optarg, "mon:", 4) == 0) {
2850 default_monitor = 0;
2852 break;
2853 case QEMU_OPTION_watchdog:
2854 if (watchdog) {
2855 fprintf(stderr,
2856 "qemu: only one watchdog option may be given\n");
2857 return 1;
2859 watchdog = optarg;
2860 break;
2861 case QEMU_OPTION_watchdog_action:
2862 if (select_watchdog_action(optarg) == -1) {
2863 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2864 exit(1);
2866 break;
2867 case QEMU_OPTION_virtiocon:
2868 add_device_config(DEV_VIRTCON, optarg);
2869 default_virtcon = 0;
2870 if (strncmp(optarg, "mon:", 4) == 0) {
2871 default_monitor = 0;
2873 break;
2874 case QEMU_OPTION_parallel:
2875 add_device_config(DEV_PARALLEL, optarg);
2876 default_parallel = 0;
2877 if (strncmp(optarg, "mon:", 4) == 0) {
2878 default_monitor = 0;
2880 break;
2881 case QEMU_OPTION_debugcon:
2882 add_device_config(DEV_DEBUGCON, optarg);
2883 break;
2884 case QEMU_OPTION_loadvm:
2885 loadvm = optarg;
2886 break;
2887 case QEMU_OPTION_full_screen:
2888 full_screen = 1;
2889 break;
2890 #ifdef CONFIG_SDL
2891 case QEMU_OPTION_no_frame:
2892 no_frame = 1;
2893 break;
2894 case QEMU_OPTION_alt_grab:
2895 alt_grab = 1;
2896 break;
2897 case QEMU_OPTION_ctrl_grab:
2898 ctrl_grab = 1;
2899 break;
2900 case QEMU_OPTION_no_quit:
2901 no_quit = 1;
2902 break;
2903 case QEMU_OPTION_sdl:
2904 display_type = DT_SDL;
2905 break;
2906 #else
2907 case QEMU_OPTION_no_frame:
2908 case QEMU_OPTION_alt_grab:
2909 case QEMU_OPTION_ctrl_grab:
2910 case QEMU_OPTION_no_quit:
2911 case QEMU_OPTION_sdl:
2912 fprintf(stderr, "SDL support is disabled\n");
2913 exit(1);
2914 #endif
2915 case QEMU_OPTION_pidfile:
2916 pid_file = optarg;
2917 break;
2918 case QEMU_OPTION_win2k_hack:
2919 win2k_install_hack = 1;
2920 break;
2921 case QEMU_OPTION_rtc_td_hack:
2922 rtc_td_hack = 1;
2923 break;
2924 case QEMU_OPTION_acpitable:
2925 do_acpitable_option(optarg);
2926 break;
2927 case QEMU_OPTION_smbios:
2928 do_smbios_option(optarg);
2929 break;
2930 case QEMU_OPTION_enable_kvm:
2931 olist = qemu_find_opts("machine");
2932 qemu_opts_reset(olist);
2933 qemu_opts_parse(olist, "accel=kvm", 0);
2934 break;
2935 case QEMU_OPTION_machine:
2936 olist = qemu_find_opts("machine");
2937 qemu_opts_reset(olist);
2938 opts = qemu_opts_parse(olist, optarg, 1);
2939 if (!opts) {
2940 fprintf(stderr, "parse error: %s\n", optarg);
2941 exit(1);
2943 optarg = qemu_opt_get(opts, "type");
2944 if (optarg) {
2945 machine = machine_parse(optarg);
2947 break;
2948 case QEMU_OPTION_no_kvm:
2949 olist = qemu_find_opts("machine");
2950 qemu_opts_reset(olist);
2951 qemu_opts_parse(olist, "accel=tcg", 0);
2952 break;
2953 #ifdef CONFIG_KVM
2954 case QEMU_OPTION_no_kvm_irqchip: {
2955 kvm_irqchip = 0;
2956 kvm_pit = 0;
2957 break;
2959 case QEMU_OPTION_no_kvm_pit: {
2960 kvm_pit = 0;
2961 break;
2963 case QEMU_OPTION_no_kvm_pit_reinjection: {
2964 kvm_pit_reinject = 0;
2965 break;
2967 #endif
2968 case QEMU_OPTION_usb:
2969 usb_enabled = 1;
2970 break;
2971 case QEMU_OPTION_usbdevice:
2972 usb_enabled = 1;
2973 add_device_config(DEV_USB, optarg);
2974 break;
2975 case QEMU_OPTION_device:
2976 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2977 exit(1);
2979 break;
2980 case QEMU_OPTION_smp:
2981 smp_parse(optarg);
2982 if (smp_cpus < 1) {
2983 fprintf(stderr, "Invalid number of CPUs\n");
2984 exit(1);
2986 if (max_cpus < smp_cpus) {
2987 fprintf(stderr, "maxcpus must be equal to or greater than "
2988 "smp\n");
2989 exit(1);
2991 if (max_cpus > 255) {
2992 fprintf(stderr, "Unsupported number of maxcpus\n");
2993 exit(1);
2995 break;
2996 case QEMU_OPTION_vnc:
2997 #ifdef CONFIG_VNC
2998 display_remote++;
2999 vnc_display = optarg;
3000 #else
3001 fprintf(stderr, "VNC support is disabled\n");
3002 exit(1);
3003 #endif
3004 break;
3005 case QEMU_OPTION_no_acpi:
3006 acpi_enabled = 0;
3007 break;
3008 case QEMU_OPTION_no_hpet:
3009 no_hpet = 1;
3010 break;
3011 case QEMU_OPTION_balloon:
3012 if (balloon_parse(optarg) < 0) {
3013 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3014 exit(1);
3016 break;
3017 case QEMU_OPTION_no_reboot:
3018 no_reboot = 1;
3019 break;
3020 case QEMU_OPTION_no_shutdown:
3021 no_shutdown = 1;
3022 break;
3023 case QEMU_OPTION_show_cursor:
3024 cursor_hide = 0;
3025 break;
3026 case QEMU_OPTION_uuid:
3027 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3028 fprintf(stderr, "Fail to parse UUID string."
3029 " Wrong format.\n");
3030 exit(1);
3032 break;
3033 case QEMU_OPTION_option_rom:
3034 if (nb_option_roms >= MAX_OPTION_ROMS) {
3035 fprintf(stderr, "Too many option ROMs\n");
3036 exit(1);
3038 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3039 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3040 option_rom[nb_option_roms].bootindex =
3041 qemu_opt_get_number(opts, "bootindex", -1);
3042 if (!option_rom[nb_option_roms].name) {
3043 fprintf(stderr, "Option ROM file is not specified\n");
3044 exit(1);
3046 nb_option_roms++;
3047 break;
3048 case QEMU_OPTION_semihosting:
3049 semihosting_enabled = 1;
3050 break;
3051 case QEMU_OPTION_tdf:
3052 time_drift_fix = 1;
3053 break;
3054 case QEMU_OPTION_kvm_shadow_memory:
3055 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
3056 break;
3057 case QEMU_OPTION_name:
3058 qemu_name = g_strdup(optarg);
3060 char *p = strchr(qemu_name, ',');
3061 if (p != NULL) {
3062 *p++ = 0;
3063 if (strncmp(p, "process=", 8)) {
3064 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3065 exit(1);
3067 p += 8;
3068 os_set_proc_name(p);
3071 break;
3072 case QEMU_OPTION_prom_env:
3073 if (nb_prom_envs >= MAX_PROM_ENVS) {
3074 fprintf(stderr, "Too many prom variables\n");
3075 exit(1);
3077 prom_envs[nb_prom_envs] = optarg;
3078 nb_prom_envs++;
3079 break;
3080 case QEMU_OPTION_old_param:
3081 old_param = 1;
3082 break;
3083 case QEMU_OPTION_clock:
3084 configure_alarms(optarg);
3085 break;
3086 case QEMU_OPTION_startdate:
3087 configure_rtc_date_offset(optarg, 1);
3088 break;
3089 case QEMU_OPTION_rtc:
3090 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3091 if (!opts) {
3092 exit(1);
3094 configure_rtc(opts);
3095 break;
3096 case QEMU_OPTION_tb_size:
3097 tcg_tb_size = strtol(optarg, NULL, 0);
3098 if (tcg_tb_size < 0) {
3099 tcg_tb_size = 0;
3101 break;
3102 case QEMU_OPTION_icount:
3103 icount_option = optarg;
3104 break;
3105 case QEMU_OPTION_incoming:
3106 incoming = optarg;
3107 break;
3108 case QEMU_OPTION_nodefaults:
3109 default_serial = 0;
3110 default_parallel = 0;
3111 default_virtcon = 0;
3112 default_monitor = 0;
3113 default_vga = 0;
3114 default_net = 0;
3115 default_floppy = 0;
3116 default_cdrom = 0;
3117 default_sdcard = 0;
3118 break;
3119 case QEMU_OPTION_xen_domid:
3120 if (!(xen_available())) {
3121 printf("Option %s not supported for this target\n", popt->name);
3122 exit(1);
3124 xen_domid = atoi(optarg);
3125 break;
3126 case QEMU_OPTION_xen_create:
3127 if (!(xen_available())) {
3128 printf("Option %s not supported for this target\n", popt->name);
3129 exit(1);
3131 xen_mode = XEN_CREATE;
3132 break;
3133 case QEMU_OPTION_xen_attach:
3134 if (!(xen_available())) {
3135 printf("Option %s not supported for this target\n", popt->name);
3136 exit(1);
3138 xen_mode = XEN_ATTACH;
3139 break;
3140 case QEMU_OPTION_trace:
3142 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3143 if (!opts) {
3144 exit(1);
3146 trace_events = qemu_opt_get(opts, "events");
3147 trace_file = qemu_opt_get(opts, "file");
3148 break;
3150 case QEMU_OPTION_readconfig:
3152 int ret = qemu_read_config_file(optarg);
3153 if (ret < 0) {
3154 fprintf(stderr, "read config %s: %s\n", optarg,
3155 strerror(-ret));
3156 exit(1);
3158 break;
3160 case QEMU_OPTION_spice:
3161 olist = qemu_find_opts("spice");
3162 if (!olist) {
3163 fprintf(stderr, "spice is not supported by this qemu build.\n");
3164 exit(1);
3166 opts = qemu_opts_parse(olist, optarg, 0);
3167 if (!opts) {
3168 fprintf(stderr, "parse error: %s\n", optarg);
3169 exit(1);
3171 break;
3172 case QEMU_OPTION_writeconfig:
3174 FILE *fp;
3175 if (strcmp(optarg, "-") == 0) {
3176 fp = stdout;
3177 } else {
3178 fp = fopen(optarg, "w");
3179 if (fp == NULL) {
3180 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3181 exit(1);
3184 qemu_config_write(fp);
3185 fclose(fp);
3186 break;
3188 default:
3189 os_parse_cmd_args(popt->index, optarg);
3193 loc_set_none();
3195 /* Open the logfile at this point, if necessary. We can't open the logfile
3196 * when encountering either of the logging options (-d or -D) because the
3197 * other one may be encountered later on the command line, changing the
3198 * location or level of logging.
3200 if (log_mask) {
3201 if (log_file) {
3202 set_cpu_log_filename(log_file);
3204 set_cpu_log(log_mask);
3207 if (!trace_backend_init(trace_events, trace_file)) {
3208 exit(1);
3211 /* If no data_dir is specified then try to find it relative to the
3212 executable path. */
3213 if (!data_dir) {
3214 data_dir = os_find_datadir(argv[0]);
3216 /* If all else fails use the install path specified when building. */
3217 if (!data_dir) {
3218 data_dir = CONFIG_QEMU_DATADIR;
3222 * Default to max_cpus = smp_cpus, in case the user doesn't
3223 * specify a max_cpus value.
3225 if (!max_cpus)
3226 max_cpus = smp_cpus;
3228 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3229 if (smp_cpus > machine->max_cpus) {
3230 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3231 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3232 machine->max_cpus);
3233 exit(1);
3237 * Get the default machine options from the machine if it is not already
3238 * specified either by the configuration file or by the command line.
3240 if (machine->default_machine_opts) {
3241 QemuOptsList *list = qemu_find_opts("machine");
3242 const char *p = NULL;
3244 if (!QTAILQ_EMPTY(&list->head)) {
3245 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3247 if (p == NULL) {
3248 qemu_opts_reset(list);
3249 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3250 if (!opts) {
3251 fprintf(stderr, "parse error for machine %s: %s\n",
3252 machine->name, machine->default_machine_opts);
3253 exit(1);
3258 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3259 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3261 if (machine->no_serial) {
3262 default_serial = 0;
3264 if (machine->no_parallel) {
3265 default_parallel = 0;
3267 if (!machine->use_virtcon) {
3268 default_virtcon = 0;
3270 if (machine->no_vga) {
3271 default_vga = 0;
3273 if (machine->no_floppy) {
3274 default_floppy = 0;
3276 if (machine->no_cdrom) {
3277 default_cdrom = 0;
3279 if (machine->no_sdcard) {
3280 default_sdcard = 0;
3283 if (display_type == DT_NOGRAPHIC) {
3284 if (default_parallel)
3285 add_device_config(DEV_PARALLEL, "null");
3286 if (default_serial && default_monitor) {
3287 add_device_config(DEV_SERIAL, "mon:stdio");
3288 } else if (default_virtcon && default_monitor) {
3289 add_device_config(DEV_VIRTCON, "mon:stdio");
3290 } else {
3291 if (default_serial)
3292 add_device_config(DEV_SERIAL, "stdio");
3293 if (default_virtcon)
3294 add_device_config(DEV_VIRTCON, "stdio");
3295 if (default_monitor)
3296 monitor_parse("stdio", "readline");
3298 } else {
3299 if (default_serial)
3300 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3301 if (default_parallel)
3302 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3303 if (default_monitor)
3304 monitor_parse("vc:80Cx24C", "readline");
3305 if (default_virtcon)
3306 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3308 if (default_vga)
3309 vga_interface_type = VGA_CIRRUS;
3311 socket_init();
3313 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3314 exit(1);
3315 #ifdef CONFIG_VIRTFS
3316 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3317 exit(1);
3319 #endif
3321 os_daemonize();
3323 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3324 os_pidfile_error();
3325 exit(1);
3328 /* init the memory */
3329 if (ram_size == 0) {
3330 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3333 configure_accelerator();
3335 if (qemu_init_main_loop()) {
3336 fprintf(stderr, "qemu_init_main_loop failed\n");
3337 exit(1);
3339 linux_boot = (kernel_filename != NULL);
3341 if (!linux_boot && *kernel_cmdline != '\0') {
3342 fprintf(stderr, "-append only allowed with -kernel option\n");
3343 exit(1);
3346 if (!linux_boot && initrd_filename != NULL) {
3347 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3348 exit(1);
3351 os_set_line_buffering();
3353 if (init_timer_alarm() < 0) {
3354 fprintf(stderr, "could not initialize alarm timer\n");
3355 exit(1);
3357 configure_icount(icount_option);
3359 if (net_init_clients() < 0) {
3360 exit(1);
3363 /* init the bluetooth world */
3364 if (foreach_device_config(DEV_BT, bt_parse))
3365 exit(1);
3367 if (!xen_enabled()) {
3368 /* On 32-bit hosts, QEMU is limited by virtual address space */
3369 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3370 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3371 exit(1);
3375 cpu_exec_init_all();
3377 bdrv_init_with_whitelist();
3379 blk_mig_init();
3381 /* open the virtual block devices */
3382 if (snapshot)
3383 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3384 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3385 exit(1);
3387 default_drive(default_cdrom, snapshot, machine->use_scsi,
3388 IF_DEFAULT, 2, CDROM_OPTS);
3389 default_drive(default_floppy, snapshot, machine->use_scsi,
3390 IF_FLOPPY, 0, FD_OPTS);
3391 default_drive(default_sdcard, snapshot, machine->use_scsi,
3392 IF_SD, 0, SD_OPTS);
3394 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3395 ram_load, NULL);
3397 if (nb_numa_nodes > 0) {
3398 int i;
3400 if (nb_numa_nodes > MAX_NODES) {
3401 nb_numa_nodes = MAX_NODES;
3404 /* If no memory size if given for any node, assume the default case
3405 * and distribute the available memory equally across all nodes
3407 for (i = 0; i < nb_numa_nodes; i++) {
3408 if (node_mem[i] != 0)
3409 break;
3411 if (i == nb_numa_nodes) {
3412 uint64_t usedmem = 0;
3414 /* On Linux, the each node's border has to be 8MB aligned,
3415 * the final node gets the rest.
3417 for (i = 0; i < nb_numa_nodes - 1; i++) {
3418 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3419 usedmem += node_mem[i];
3421 node_mem[i] = ram_size - usedmem;
3424 for (i = 0; i < nb_numa_nodes; i++) {
3425 if (node_cpumask[i] != 0)
3426 break;
3428 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3429 * must cope with this anyway, because there are BIOSes out there in
3430 * real machines which also use this scheme.
3432 if (i == nb_numa_nodes) {
3433 for (i = 0; i < smp_cpus; i++) {
3434 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3439 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3440 exit(1);
3443 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3444 exit(1);
3445 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3446 exit(1);
3447 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3448 exit(1);
3449 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3450 exit(1);
3452 module_call_init(MODULE_INIT_DEVICE);
3454 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3455 exit(0);
3457 if (watchdog) {
3458 i = select_watchdog(watchdog);
3459 if (i > 0)
3460 exit (i == 1 ? 1 : 0);
3463 if (machine->compat_props) {
3464 qdev_prop_register_global_list(machine->compat_props);
3466 qemu_add_globals();
3468 machine->init(ram_size, boot_devices,
3469 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3471 cpu_synchronize_all_post_init();
3473 set_numa_modes();
3475 current_machine = machine;
3477 /* init USB devices */
3478 if (usb_enabled) {
3479 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3480 exit(1);
3483 /* init generic devices */
3484 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3485 exit(1);
3487 net_check_clients();
3489 /* just use the first displaystate for the moment */
3490 ds = get_displaystate();
3492 if (using_spice)
3493 display_remote++;
3494 if (display_type == DT_DEFAULT && !display_remote) {
3495 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3496 display_type = DT_SDL;
3497 #elif defined(CONFIG_VNC)
3498 vnc_display = "localhost:0,to=99";
3499 show_vnc_port = 1;
3500 #else
3501 display_type = DT_NONE;
3502 #endif
3506 /* init local displays */
3507 switch (display_type) {
3508 case DT_NOGRAPHIC:
3509 break;
3510 #if defined(CONFIG_CURSES)
3511 case DT_CURSES:
3512 curses_display_init(ds, full_screen);
3513 break;
3514 #endif
3515 #if defined(CONFIG_SDL)
3516 case DT_SDL:
3517 sdl_display_init(ds, full_screen, no_frame);
3518 break;
3519 #elif defined(CONFIG_COCOA)
3520 case DT_SDL:
3521 cocoa_display_init(ds, full_screen);
3522 break;
3523 #endif
3524 default:
3525 break;
3528 /* must be after terminal init, SDL library changes signal handlers */
3529 os_setup_signal_handling();
3531 #ifdef CONFIG_VNC
3532 /* init remote displays */
3533 if (vnc_display) {
3534 vnc_display_init(ds);
3535 if (vnc_display_open(ds, vnc_display) < 0)
3536 exit(1);
3538 if (show_vnc_port) {
3539 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3542 #endif
3543 #ifdef CONFIG_SPICE
3544 if (using_spice && !qxl_enabled) {
3545 qemu_spice_display_init(ds);
3547 #endif
3549 /* display setup */
3550 dpy_resize(ds);
3551 dcl = ds->listeners;
3552 while (dcl != NULL) {
3553 if (dcl->dpy_refresh != NULL) {
3554 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3555 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3556 break;
3558 dcl = dcl->next;
3560 if (ds->gui_timer == NULL) {
3561 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3562 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3564 text_consoles_set_display(ds);
3566 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3567 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3568 gdbstub_dev);
3569 exit(1);
3572 qdev_machine_creation_done();
3574 if (rom_load_all() != 0) {
3575 fprintf(stderr, "rom loading failed\n");
3576 exit(1);
3579 /* TODO: once all bus devices are qdevified, this should be done
3580 * when bus is created by qdev.c */
3581 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3582 qemu_run_machine_init_done_notifiers();
3584 qemu_system_reset(VMRESET_SILENT);
3585 if (loadvm) {
3586 if (load_vmstate(loadvm) < 0) {
3587 autostart = 0;
3591 if (incoming) {
3592 runstate_set(RSTATE_IN_MIGRATE);
3593 int ret = qemu_start_incoming_migration(incoming);
3594 if (ret < 0) {
3595 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3596 incoming, ret);
3597 exit(ret);
3599 } else if (autostart) {
3600 vm_start();
3601 } else {
3602 runstate_set(RSTATE_PRE_LAUNCH);
3605 os_setup_post();
3607 main_loop();
3608 quit_timers();
3609 net_cleanup();
3610 res_free();
3612 return 0;