qemu_rearm_alarm_timer: do not call rearm if the next deadline is INT64_MAX
[qemu/ar7.git] / vl.c
blobe71cb30ecfc09d89d0cbc9e82585f61b44445083
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>
31 #include "bitmap.h"
33 /* Needed early for CONFIG_BSD etc. */
34 #include "config-host.h"
36 #ifndef _WIN32
37 #include <libgen.h>
38 #include <sys/times.h>
39 #include <sys/wait.h>
40 #include <termios.h>
41 #include <sys/mman.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <net/if.h>
47 #include <arpa/inet.h>
48 #include <dirent.h>
49 #include <netdb.h>
50 #include <sys/select.h>
52 #ifdef CONFIG_BSD
53 #include <sys/stat.h>
54 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <malloc.h>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
65 #endif
66 #ifdef __sun__
67 #include <sys/stat.h>
68 #include <sys/ethernet.h>
69 #include <sys/sockio.h>
70 #include <netinet/arp.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_icmp.h> // must come after ip.h
74 #include <netinet/udp.h>
75 #include <netinet/tcp.h>
76 #include <net/if.h>
77 #include <syslog.h>
78 #include <stropts.h>
79 #endif
80 #endif
81 #endif
83 #if defined(CONFIG_VDE)
84 #include <libvdeplug.h>
85 #endif
87 #ifdef _WIN32
88 #include <windows.h>
89 #endif
91 #ifdef CONFIG_SDL
92 #if defined(__APPLE__) || defined(main)
93 #include <SDL.h>
94 int qemu_main(int argc, char **argv, char **envp);
95 int main(int argc, char **argv)
97 return qemu_main(argc, argv, NULL);
99 #undef main
100 #define main qemu_main
101 #endif
102 #endif /* CONFIG_SDL */
104 #ifdef CONFIG_COCOA
105 #undef main
106 #define main qemu_main
107 #endif /* CONFIG_COCOA */
109 #include <glib.h>
111 #include "hw/hw.h"
112 #include "hw/boards.h"
113 #include "hw/usb.h"
114 #include "hw/pcmcia.h"
115 #include "hw/pc.h"
116 #include "hw/isa.h"
117 #include "hw/baum.h"
118 #include "hw/bt.h"
119 #include "hw/watchdog.h"
120 #include "hw/smbios.h"
121 #include "hw/xen.h"
122 #include "hw/qdev.h"
123 #include "hw/loader.h"
124 #include "bt-host.h"
125 #include "net.h"
126 #include "net/slirp.h"
127 #include "monitor.h"
128 #include "console.h"
129 #include "sysemu.h"
130 #include "gdbstub.h"
131 #include "qemu-timer.h"
132 #include "qemu-char.h"
133 #include "cache-utils.h"
134 #include "blockdev.h"
135 #include "hw/block-common.h"
136 #include "block-migration.h"
137 #include "dma.h"
138 #include "audio/audio.h"
139 #include "migration.h"
140 #include "kvm.h"
141 #include "qjson.h"
142 #include "qemu-option.h"
143 #include "qemu-config.h"
144 #include "qemu-options.h"
145 #include "qmp-commands.h"
146 #include "main-loop.h"
147 #ifdef CONFIG_VIRTFS
148 #include "fsdev/qemu-fsdev.h"
149 #endif
150 #include "qtest.h"
152 #include "disas.h"
154 #include "qemu_socket.h"
156 #include "slirp/libslirp.h"
158 #include "trace.h"
159 #include "trace/control.h"
160 #include "qemu-queue.h"
161 #include "cpus.h"
162 #include "arch_init.h"
163 #include "osdep.h"
165 #include "ui/qemu-spice.h"
167 //#define DEBUG_NET
168 //#define DEBUG_SLIRP
170 #define DEFAULT_RAM_SIZE 128
172 #define MAX_VIRTIO_CONSOLES 1
174 static const char *data_dir;
175 const char *bios_name = NULL;
176 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
177 DisplayType display_type = DT_DEFAULT;
178 int display_remote = 0;
179 const char* keyboard_layout = NULL;
180 ram_addr_t ram_size;
181 const char *mem_path = NULL;
182 #ifdef MAP_POPULATE
183 int mem_prealloc = 0; /* force preallocation of physical target memory */
184 #endif
185 int nb_nics;
186 NICInfo nd_table[MAX_NICS];
187 int autostart;
188 static int rtc_utc = 1;
189 static int rtc_date_offset = -1; /* -1 means no change */
190 QEMUClock *rtc_clock;
191 int vga_interface_type = VGA_NONE;
192 static int full_screen = 0;
193 #ifdef CONFIG_SDL
194 static int no_frame = 0;
195 #endif
196 int no_quit = 0;
197 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
198 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
199 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
200 int win2k_install_hack = 0;
201 int usb_enabled = 0;
202 int singlestep = 0;
203 int smp_cpus = 1;
204 int max_cpus = 0;
205 int smp_cores = 1;
206 int smp_threads = 1;
207 #ifdef CONFIG_VNC
208 const char *vnc_display;
209 #endif
210 int acpi_enabled = 1;
211 int no_hpet = 0;
212 int fd_bootchk = 1;
213 int no_reboot = 0;
214 int no_shutdown = 0;
215 int cursor_hide = 1;
216 int graphic_rotate = 0;
217 const char *watchdog;
218 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
219 int nb_option_roms;
220 int semihosting_enabled = 0;
221 int old_param = 0;
222 const char *qemu_name;
223 int alt_grab = 0;
224 int ctrl_grab = 0;
225 unsigned int nb_prom_envs = 0;
226 const char *prom_envs[MAX_PROM_ENVS];
227 int boot_menu;
228 uint8_t *boot_splash_filedata;
229 int boot_splash_filedata_size;
230 uint8_t qemu_extra_params_fw[2];
232 typedef struct FWBootEntry FWBootEntry;
234 struct FWBootEntry {
235 QTAILQ_ENTRY(FWBootEntry) link;
236 int32_t bootindex;
237 DeviceState *dev;
238 char *suffix;
241 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
243 int nb_numa_nodes;
244 uint64_t node_mem[MAX_NODES];
245 unsigned long *node_cpumask[MAX_NODES];
247 uint8_t qemu_uuid[16];
249 static QEMUBootSetHandler *boot_set_handler;
250 static void *boot_set_opaque;
252 static NotifierList exit_notifiers =
253 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
255 static NotifierList machine_init_done_notifiers =
256 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
258 static int tcg_allowed = 1;
259 int kvm_allowed = 0;
260 int xen_allowed = 0;
261 uint32_t xen_domid;
262 enum xen_mode xen_mode = XEN_EMULATE;
263 static int tcg_tb_size;
265 static int default_serial = 1;
266 static int default_parallel = 1;
267 static int default_virtcon = 1;
268 static int default_monitor = 1;
269 static int default_floppy = 1;
270 static int default_cdrom = 1;
271 static int default_sdcard = 1;
272 static int default_vga = 1;
274 static struct {
275 const char *driver;
276 int *flag;
277 } default_list[] = {
278 { .driver = "isa-serial", .flag = &default_serial },
279 { .driver = "isa-parallel", .flag = &default_parallel },
280 { .driver = "isa-fdc", .flag = &default_floppy },
281 { .driver = "ide-cd", .flag = &default_cdrom },
282 { .driver = "ide-hd", .flag = &default_cdrom },
283 { .driver = "ide-drive", .flag = &default_cdrom },
284 { .driver = "scsi-cd", .flag = &default_cdrom },
285 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
286 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
287 { .driver = "virtio-serial", .flag = &default_virtcon },
288 { .driver = "VGA", .flag = &default_vga },
289 { .driver = "isa-vga", .flag = &default_vga },
290 { .driver = "cirrus-vga", .flag = &default_vga },
291 { .driver = "isa-cirrus-vga", .flag = &default_vga },
292 { .driver = "vmware-svga", .flag = &default_vga },
293 { .driver = "qxl-vga", .flag = &default_vga },
296 static void res_free(void)
298 if (boot_splash_filedata != NULL) {
299 g_free(boot_splash_filedata);
300 boot_splash_filedata = NULL;
304 static int default_driver_check(QemuOpts *opts, void *opaque)
306 const char *driver = qemu_opt_get(opts, "driver");
307 int i;
309 if (!driver)
310 return 0;
311 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
312 if (strcmp(default_list[i].driver, driver) != 0)
313 continue;
314 *(default_list[i].flag) = 0;
316 return 0;
319 /***********************************************************/
320 /* QEMU state */
322 static RunState current_run_state = RUN_STATE_PRELAUNCH;
324 typedef struct {
325 RunState from;
326 RunState to;
327 } RunStateTransition;
329 static const RunStateTransition runstate_transitions_def[] = {
330 /* from -> to */
331 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
333 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
334 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
336 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
337 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
339 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
340 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
342 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
343 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
345 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
346 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
348 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
349 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
350 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
352 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
353 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
355 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
357 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
358 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
359 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
360 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
361 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
362 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
363 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
364 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
365 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
367 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
369 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
370 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
372 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
373 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
374 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
375 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
377 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
378 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
380 { RUN_STATE_MAX, RUN_STATE_MAX },
383 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
385 bool runstate_check(RunState state)
387 return current_run_state == state;
390 void runstate_init(void)
392 const RunStateTransition *p;
394 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
396 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
397 runstate_valid_transitions[p->from][p->to] = true;
401 /* This function will abort() on invalid state transitions */
402 void runstate_set(RunState new_state)
404 assert(new_state < RUN_STATE_MAX);
406 if (!runstate_valid_transitions[current_run_state][new_state]) {
407 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
408 RunState_lookup[current_run_state],
409 RunState_lookup[new_state]);
410 abort();
413 current_run_state = new_state;
416 int runstate_is_running(void)
418 return runstate_check(RUN_STATE_RUNNING);
421 StatusInfo *qmp_query_status(Error **errp)
423 StatusInfo *info = g_malloc0(sizeof(*info));
425 info->running = runstate_is_running();
426 info->singlestep = singlestep;
427 info->status = current_run_state;
429 return info;
432 /***********************************************************/
433 /* real time host monotonic timer */
435 /***********************************************************/
436 /* host time/date access */
437 void qemu_get_timedate(struct tm *tm, int offset)
439 time_t ti;
440 struct tm *ret;
442 time(&ti);
443 ti += offset;
444 if (rtc_date_offset == -1) {
445 if (rtc_utc)
446 ret = gmtime(&ti);
447 else
448 ret = localtime(&ti);
449 } else {
450 ti -= rtc_date_offset;
451 ret = gmtime(&ti);
454 memcpy(tm, ret, sizeof(struct tm));
457 int qemu_timedate_diff(struct tm *tm)
459 time_t seconds;
461 if (rtc_date_offset == -1)
462 if (rtc_utc)
463 seconds = mktimegm(tm);
464 else {
465 struct tm tmp = *tm;
466 tmp.tm_isdst = -1; /* use timezone to figure it out */
467 seconds = mktime(&tmp);
469 else
470 seconds = mktimegm(tm) + rtc_date_offset;
472 return seconds - time(NULL);
475 void rtc_change_mon_event(struct tm *tm)
477 QObject *data;
479 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
480 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
481 qobject_decref(data);
484 static void configure_rtc_date_offset(const char *startdate, int legacy)
486 time_t rtc_start_date;
487 struct tm tm;
489 if (!strcmp(startdate, "now") && legacy) {
490 rtc_date_offset = -1;
491 } else {
492 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
493 &tm.tm_year,
494 &tm.tm_mon,
495 &tm.tm_mday,
496 &tm.tm_hour,
497 &tm.tm_min,
498 &tm.tm_sec) == 6) {
499 /* OK */
500 } else if (sscanf(startdate, "%d-%d-%d",
501 &tm.tm_year,
502 &tm.tm_mon,
503 &tm.tm_mday) == 3) {
504 tm.tm_hour = 0;
505 tm.tm_min = 0;
506 tm.tm_sec = 0;
507 } else {
508 goto date_fail;
510 tm.tm_year -= 1900;
511 tm.tm_mon--;
512 rtc_start_date = mktimegm(&tm);
513 if (rtc_start_date == -1) {
514 date_fail:
515 fprintf(stderr, "Invalid date format. Valid formats are:\n"
516 "'2006-06-17T16:01:21' or '2006-06-17'\n");
517 exit(1);
519 rtc_date_offset = time(NULL) - rtc_start_date;
523 static void configure_rtc(QemuOpts *opts)
525 const char *value;
527 value = qemu_opt_get(opts, "base");
528 if (value) {
529 if (!strcmp(value, "utc")) {
530 rtc_utc = 1;
531 } else if (!strcmp(value, "localtime")) {
532 rtc_utc = 0;
533 } else {
534 configure_rtc_date_offset(value, 0);
537 value = qemu_opt_get(opts, "clock");
538 if (value) {
539 if (!strcmp(value, "host")) {
540 rtc_clock = host_clock;
541 } else if (!strcmp(value, "rt")) {
542 rtc_clock = rt_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 static GlobalProperty slew_lost_ticks[] = {
555 .driver = "mc146818rtc",
556 .property = "lost_tick_policy",
557 .value = "slew",
559 { /* end of list */ }
562 qdev_prop_register_global_list(slew_lost_ticks);
563 } else if (!strcmp(value, "none")) {
564 /* discard is default */
565 } else {
566 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
567 exit(1);
572 /***********************************************************/
573 /* Bluetooth support */
574 static int nb_hcis;
575 static int cur_hci;
576 static struct HCIInfo *hci_table[MAX_NICS];
578 static struct bt_vlan_s {
579 struct bt_scatternet_s net;
580 int id;
581 struct bt_vlan_s *next;
582 } *first_bt_vlan;
584 /* find or alloc a new bluetooth "VLAN" */
585 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
587 struct bt_vlan_s **pvlan, *vlan;
588 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
589 if (vlan->id == id)
590 return &vlan->net;
592 vlan = g_malloc0(sizeof(struct bt_vlan_s));
593 vlan->id = id;
594 pvlan = &first_bt_vlan;
595 while (*pvlan != NULL)
596 pvlan = &(*pvlan)->next;
597 *pvlan = vlan;
598 return &vlan->net;
601 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
605 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
607 return -ENOTSUP;
610 static struct HCIInfo null_hci = {
611 .cmd_send = null_hci_send,
612 .sco_send = null_hci_send,
613 .acl_send = null_hci_send,
614 .bdaddr_set = null_hci_addr_set,
617 struct HCIInfo *qemu_next_hci(void)
619 if (cur_hci == nb_hcis)
620 return &null_hci;
622 return hci_table[cur_hci++];
625 static struct HCIInfo *hci_init(const char *str)
627 char *endp;
628 struct bt_scatternet_s *vlan = 0;
630 if (!strcmp(str, "null"))
631 /* null */
632 return &null_hci;
633 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
634 /* host[:hciN] */
635 return bt_host_hci(str[4] ? str + 5 : "hci0");
636 else if (!strncmp(str, "hci", 3)) {
637 /* hci[,vlan=n] */
638 if (str[3]) {
639 if (!strncmp(str + 3, ",vlan=", 6)) {
640 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
641 if (*endp)
642 vlan = 0;
644 } else
645 vlan = qemu_find_bt_vlan(0);
646 if (vlan)
647 return bt_new_hci(vlan);
650 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
652 return 0;
655 static int bt_hci_parse(const char *str)
657 struct HCIInfo *hci;
658 bdaddr_t bdaddr;
660 if (nb_hcis >= MAX_NICS) {
661 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
662 return -1;
665 hci = hci_init(str);
666 if (!hci)
667 return -1;
669 bdaddr.b[0] = 0x52;
670 bdaddr.b[1] = 0x54;
671 bdaddr.b[2] = 0x00;
672 bdaddr.b[3] = 0x12;
673 bdaddr.b[4] = 0x34;
674 bdaddr.b[5] = 0x56 + nb_hcis;
675 hci->bdaddr_set(hci, bdaddr.b);
677 hci_table[nb_hcis++] = hci;
679 return 0;
682 static void bt_vhci_add(int vlan_id)
684 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
686 if (!vlan->slave)
687 fprintf(stderr, "qemu: warning: adding a VHCI to "
688 "an empty scatternet %i\n", vlan_id);
690 bt_vhci_init(bt_new_hci(vlan));
693 static struct bt_device_s *bt_device_add(const char *opt)
695 struct bt_scatternet_s *vlan;
696 int vlan_id = 0;
697 char *endp = strstr(opt, ",vlan=");
698 int len = (endp ? endp - opt : strlen(opt)) + 1;
699 char devname[10];
701 pstrcpy(devname, MIN(sizeof(devname), len), opt);
703 if (endp) {
704 vlan_id = strtol(endp + 6, &endp, 0);
705 if (*endp) {
706 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
707 return 0;
711 vlan = qemu_find_bt_vlan(vlan_id);
713 if (!vlan->slave)
714 fprintf(stderr, "qemu: warning: adding a slave device to "
715 "an empty scatternet %i\n", vlan_id);
717 if (!strcmp(devname, "keyboard"))
718 return bt_keyboard_init(vlan);
720 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
721 return 0;
724 static int bt_parse(const char *opt)
726 const char *endp, *p;
727 int vlan;
729 if (strstart(opt, "hci", &endp)) {
730 if (!*endp || *endp == ',') {
731 if (*endp)
732 if (!strstart(endp, ",vlan=", 0))
733 opt = endp + 1;
735 return bt_hci_parse(opt);
737 } else if (strstart(opt, "vhci", &endp)) {
738 if (!*endp || *endp == ',') {
739 if (*endp) {
740 if (strstart(endp, ",vlan=", &p)) {
741 vlan = strtol(p, (char **) &endp, 0);
742 if (*endp) {
743 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
744 return 1;
746 } else {
747 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
748 return 1;
750 } else
751 vlan = 0;
753 bt_vhci_add(vlan);
754 return 0;
756 } else if (strstart(opt, "device:", &endp))
757 return !bt_device_add(endp);
759 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
760 return 1;
763 /***********************************************************/
764 /* QEMU Block devices */
766 #define HD_OPTS "media=disk"
767 #define CDROM_OPTS "media=cdrom"
768 #define FD_OPTS ""
769 #define PFLASH_OPTS ""
770 #define MTD_OPTS ""
771 #define SD_OPTS ""
773 static int drive_init_func(QemuOpts *opts, void *opaque)
775 int *use_scsi = opaque;
777 return drive_init(opts, *use_scsi) == NULL;
780 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
782 if (NULL == qemu_opt_get(opts, "snapshot")) {
783 qemu_opt_set(opts, "snapshot", "on");
785 return 0;
788 static void default_drive(int enable, int snapshot, int use_scsi,
789 BlockInterfaceType type, int index,
790 const char *optstr)
792 QemuOpts *opts;
794 if (type == IF_DEFAULT) {
795 type = use_scsi ? IF_SCSI : IF_IDE;
798 if (!enable || drive_get_by_index(type, index)) {
799 return;
802 opts = drive_add(type, index, NULL, optstr);
803 if (snapshot) {
804 drive_enable_snapshot(opts, NULL);
806 if (!drive_init(opts, use_scsi)) {
807 exit(1);
811 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
813 boot_set_handler = func;
814 boot_set_opaque = opaque;
817 int qemu_boot_set(const char *boot_devices)
819 if (!boot_set_handler) {
820 return -EINVAL;
822 return boot_set_handler(boot_set_opaque, boot_devices);
825 static void validate_bootdevices(char *devices)
827 /* We just do some generic consistency checks */
828 const char *p;
829 int bitmap = 0;
831 for (p = devices; *p != '\0'; p++) {
832 /* Allowed boot devices are:
833 * a-b: floppy disk drives
834 * c-f: IDE disk drives
835 * g-m: machine implementation dependent drives
836 * n-p: network devices
837 * It's up to each machine implementation to check if the given boot
838 * devices match the actual hardware implementation and firmware
839 * features.
841 if (*p < 'a' || *p > 'p') {
842 fprintf(stderr, "Invalid boot device '%c'\n", *p);
843 exit(1);
845 if (bitmap & (1 << (*p - 'a'))) {
846 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
847 exit(1);
849 bitmap |= 1 << (*p - 'a');
853 static void restore_boot_devices(void *opaque)
855 char *standard_boot_devices = opaque;
856 static int first = 1;
858 /* Restore boot order and remove ourselves after the first boot */
859 if (first) {
860 first = 0;
861 return;
864 qemu_boot_set(standard_boot_devices);
866 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
867 g_free(standard_boot_devices);
870 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
871 const char *suffix)
873 FWBootEntry *node, *i;
875 if (bootindex < 0) {
876 return;
879 assert(dev != NULL || suffix != NULL);
881 node = g_malloc0(sizeof(FWBootEntry));
882 node->bootindex = bootindex;
883 node->suffix = suffix ? g_strdup(suffix) : NULL;
884 node->dev = dev;
886 QTAILQ_FOREACH(i, &fw_boot_order, link) {
887 if (i->bootindex == bootindex) {
888 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
889 exit(1);
890 } else if (i->bootindex < bootindex) {
891 continue;
893 QTAILQ_INSERT_BEFORE(i, node, link);
894 return;
896 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
900 * This function returns null terminated string that consist of new line
901 * separated device paths.
903 * memory pointed by "size" is assigned total length of the array in bytes
906 char *get_boot_devices_list(uint32_t *size)
908 FWBootEntry *i;
909 uint32_t total = 0;
910 char *list = NULL;
912 QTAILQ_FOREACH(i, &fw_boot_order, link) {
913 char *devpath = NULL, *bootpath;
914 int len;
916 if (i->dev) {
917 devpath = qdev_get_fw_dev_path(i->dev);
918 assert(devpath);
921 if (i->suffix && devpath) {
922 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
924 bootpath = g_malloc(bootpathlen);
925 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
926 g_free(devpath);
927 } else if (devpath) {
928 bootpath = devpath;
929 } else {
930 assert(i->suffix);
931 bootpath = g_strdup(i->suffix);
934 if (total) {
935 list[total-1] = '\n';
937 len = strlen(bootpath) + 1;
938 list = g_realloc(list, total + len);
939 memcpy(&list[total], bootpath, len);
940 total += len;
941 g_free(bootpath);
944 *size = total;
946 return list;
949 static void numa_add(const char *optarg)
951 char option[128];
952 char *endptr;
953 unsigned long long value, endvalue;
954 int nodenr;
956 value = endvalue = 0ULL;
958 optarg = get_opt_name(option, 128, optarg, ',') + 1;
959 if (!strcmp(option, "node")) {
960 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
961 nodenr = nb_numa_nodes;
962 } else {
963 nodenr = strtoull(option, NULL, 10);
966 if (get_param_value(option, 128, "mem", optarg) == 0) {
967 node_mem[nodenr] = 0;
968 } else {
969 int64_t sval;
970 sval = strtosz(option, &endptr);
971 if (sval < 0 || *endptr) {
972 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
973 exit(1);
975 node_mem[nodenr] = sval;
977 if (get_param_value(option, 128, "cpus", optarg) != 0) {
978 value = strtoull(option, &endptr, 10);
979 if (*endptr == '-') {
980 endvalue = strtoull(endptr+1, &endptr, 10);
981 } else {
982 endvalue = value;
985 if (!(endvalue < MAX_CPUMASK_BITS)) {
986 endvalue = MAX_CPUMASK_BITS - 1;
987 fprintf(stderr,
988 "A max of %d CPUs are supported in a guest\n",
989 MAX_CPUMASK_BITS);
992 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
994 nb_numa_nodes++;
996 return;
999 static void smp_parse(const char *optarg)
1001 int smp, sockets = 0, threads = 0, cores = 0;
1002 char *endptr;
1003 char option[128];
1005 smp = strtoul(optarg, &endptr, 10);
1006 if (endptr != optarg) {
1007 if (*endptr == ',') {
1008 endptr++;
1011 if (get_param_value(option, 128, "sockets", endptr) != 0)
1012 sockets = strtoull(option, NULL, 10);
1013 if (get_param_value(option, 128, "cores", endptr) != 0)
1014 cores = strtoull(option, NULL, 10);
1015 if (get_param_value(option, 128, "threads", endptr) != 0)
1016 threads = strtoull(option, NULL, 10);
1017 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1018 max_cpus = strtoull(option, NULL, 10);
1020 /* compute missing values, prefer sockets over cores over threads */
1021 if (smp == 0 || sockets == 0) {
1022 sockets = sockets > 0 ? sockets : 1;
1023 cores = cores > 0 ? cores : 1;
1024 threads = threads > 0 ? threads : 1;
1025 if (smp == 0) {
1026 smp = cores * threads * sockets;
1028 } else {
1029 if (cores == 0) {
1030 threads = threads > 0 ? threads : 1;
1031 cores = smp / (sockets * threads);
1032 } else {
1033 threads = smp / (cores * sockets);
1036 smp_cpus = smp;
1037 smp_cores = cores > 0 ? cores : 1;
1038 smp_threads = threads > 0 ? threads : 1;
1039 if (max_cpus == 0)
1040 max_cpus = smp_cpus;
1043 /***********************************************************/
1044 /* USB devices */
1046 static int usb_device_add(const char *devname)
1048 const char *p;
1049 USBDevice *dev = NULL;
1051 if (!usb_enabled)
1052 return -1;
1054 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1055 dev = usbdevice_create(devname);
1056 if (dev)
1057 goto done;
1059 /* the other ones */
1060 #ifndef CONFIG_LINUX
1061 /* only the linux version is qdev-ified, usb-bsd still needs this */
1062 if (strstart(devname, "host:", &p)) {
1063 dev = usb_host_device_open(usb_bus_find(-1), p);
1064 } else
1065 #endif
1066 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1067 dev = usb_bt_init(usb_bus_find(-1),
1068 devname[2] ? hci_init(p)
1069 : bt_new_hci(qemu_find_bt_vlan(0)));
1070 } else {
1071 return -1;
1073 if (!dev)
1074 return -1;
1076 done:
1077 return 0;
1080 static int usb_device_del(const char *devname)
1082 int bus_num, addr;
1083 const char *p;
1085 if (strstart(devname, "host:", &p))
1086 return usb_host_device_close(p);
1088 if (!usb_enabled)
1089 return -1;
1091 p = strchr(devname, '.');
1092 if (!p)
1093 return -1;
1094 bus_num = strtoul(devname, NULL, 0);
1095 addr = strtoul(p + 1, NULL, 0);
1097 return usb_device_delete_addr(bus_num, addr);
1100 static int usb_parse(const char *cmdline)
1102 int r;
1103 r = usb_device_add(cmdline);
1104 if (r < 0) {
1105 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1107 return r;
1110 void do_usb_add(Monitor *mon, const QDict *qdict)
1112 const char *devname = qdict_get_str(qdict, "devname");
1113 if (usb_device_add(devname) < 0) {
1114 error_report("could not add USB device '%s'", devname);
1118 void do_usb_del(Monitor *mon, const QDict *qdict)
1120 const char *devname = qdict_get_str(qdict, "devname");
1121 if (usb_device_del(devname) < 0) {
1122 error_report("could not delete USB device '%s'", devname);
1126 /***********************************************************/
1127 /* PCMCIA/Cardbus */
1129 static struct pcmcia_socket_entry_s {
1130 PCMCIASocket *socket;
1131 struct pcmcia_socket_entry_s *next;
1132 } *pcmcia_sockets = 0;
1134 void pcmcia_socket_register(PCMCIASocket *socket)
1136 struct pcmcia_socket_entry_s *entry;
1138 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1139 entry->socket = socket;
1140 entry->next = pcmcia_sockets;
1141 pcmcia_sockets = entry;
1144 void pcmcia_socket_unregister(PCMCIASocket *socket)
1146 struct pcmcia_socket_entry_s *entry, **ptr;
1148 ptr = &pcmcia_sockets;
1149 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1150 if (entry->socket == socket) {
1151 *ptr = entry->next;
1152 g_free(entry);
1156 void pcmcia_info(Monitor *mon)
1158 struct pcmcia_socket_entry_s *iter;
1160 if (!pcmcia_sockets)
1161 monitor_printf(mon, "No PCMCIA sockets\n");
1163 for (iter = pcmcia_sockets; iter; iter = iter->next)
1164 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1165 iter->socket->attached ? iter->socket->card_string :
1166 "Empty");
1169 /***********************************************************/
1170 /* machine registration */
1172 static QEMUMachine *first_machine = NULL;
1173 QEMUMachine *current_machine = NULL;
1175 int qemu_register_machine(QEMUMachine *m)
1177 QEMUMachine **pm;
1178 pm = &first_machine;
1179 while (*pm != NULL)
1180 pm = &(*pm)->next;
1181 m->next = NULL;
1182 *pm = m;
1183 return 0;
1186 static QEMUMachine *find_machine(const char *name)
1188 QEMUMachine *m;
1190 for(m = first_machine; m != NULL; m = m->next) {
1191 if (!strcmp(m->name, name))
1192 return m;
1193 if (m->alias && !strcmp(m->alias, name))
1194 return m;
1196 return NULL;
1199 QEMUMachine *find_default_machine(void)
1201 QEMUMachine *m;
1203 for(m = first_machine; m != NULL; m = m->next) {
1204 if (m->is_default) {
1205 return m;
1208 return NULL;
1211 /***********************************************************/
1212 /* main execution loop */
1214 static void gui_update(void *opaque)
1216 uint64_t interval = GUI_REFRESH_INTERVAL;
1217 DisplayState *ds = opaque;
1218 DisplayChangeListener *dcl = ds->listeners;
1220 dpy_refresh(ds);
1222 while (dcl != NULL) {
1223 if (dcl->gui_timer_interval &&
1224 dcl->gui_timer_interval < interval)
1225 interval = dcl->gui_timer_interval;
1226 dcl = dcl->next;
1228 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1231 struct vm_change_state_entry {
1232 VMChangeStateHandler *cb;
1233 void *opaque;
1234 QLIST_ENTRY (vm_change_state_entry) entries;
1237 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1239 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1240 void *opaque)
1242 VMChangeStateEntry *e;
1244 e = g_malloc0(sizeof (*e));
1246 e->cb = cb;
1247 e->opaque = opaque;
1248 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1249 return e;
1252 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1254 QLIST_REMOVE (e, entries);
1255 g_free (e);
1258 void vm_state_notify(int running, RunState state)
1260 VMChangeStateEntry *e;
1262 trace_vm_state_notify(running, state);
1264 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1265 e->cb(e->opaque, running, state);
1269 void vm_start(void)
1271 if (!runstate_is_running()) {
1272 cpu_enable_ticks();
1273 runstate_set(RUN_STATE_RUNNING);
1274 vm_state_notify(1, RUN_STATE_RUNNING);
1275 resume_all_vcpus();
1276 monitor_protocol_event(QEVENT_RESUME, NULL);
1280 /* reset/shutdown handler */
1282 typedef struct QEMUResetEntry {
1283 QTAILQ_ENTRY(QEMUResetEntry) entry;
1284 QEMUResetHandler *func;
1285 void *opaque;
1286 } QEMUResetEntry;
1288 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1289 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1290 static int reset_requested;
1291 static int shutdown_requested, shutdown_signal = -1;
1292 static pid_t shutdown_pid;
1293 static int powerdown_requested;
1294 static int debug_requested;
1295 static int suspend_requested;
1296 static NotifierList suspend_notifiers =
1297 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1298 static NotifierList wakeup_notifiers =
1299 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1300 static uint32_t wakeup_reason_mask = ~0;
1301 static RunState vmstop_requested = RUN_STATE_MAX;
1303 int qemu_shutdown_requested_get(void)
1305 return shutdown_requested;
1308 int qemu_reset_requested_get(void)
1310 return reset_requested;
1313 int qemu_shutdown_requested(void)
1315 int r = shutdown_requested;
1316 shutdown_requested = 0;
1317 return r;
1320 void qemu_kill_report(void)
1322 if (!qtest_enabled() && shutdown_signal != -1) {
1323 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1324 if (shutdown_pid == 0) {
1325 /* This happens for eg ^C at the terminal, so it's worth
1326 * avoiding printing an odd message in that case.
1328 fputc('\n', stderr);
1329 } else {
1330 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1332 shutdown_signal = -1;
1336 int qemu_reset_requested(void)
1338 int r = reset_requested;
1339 reset_requested = 0;
1340 return r;
1343 static int qemu_suspend_requested(void)
1345 int r = suspend_requested;
1346 suspend_requested = 0;
1347 return r;
1350 int qemu_powerdown_requested(void)
1352 int r = powerdown_requested;
1353 powerdown_requested = 0;
1354 return r;
1357 static int qemu_debug_requested(void)
1359 int r = debug_requested;
1360 debug_requested = 0;
1361 return r;
1364 /* We use RUN_STATE_MAX but any invalid value will do */
1365 static bool qemu_vmstop_requested(RunState *r)
1367 if (vmstop_requested < RUN_STATE_MAX) {
1368 *r = vmstop_requested;
1369 vmstop_requested = RUN_STATE_MAX;
1370 return true;
1373 return false;
1376 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1378 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1380 re->func = func;
1381 re->opaque = opaque;
1382 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1385 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1387 QEMUResetEntry *re;
1389 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1390 if (re->func == func && re->opaque == opaque) {
1391 QTAILQ_REMOVE(&reset_handlers, re, entry);
1392 g_free(re);
1393 return;
1398 void qemu_system_reset(bool report)
1400 QEMUResetEntry *re, *nre;
1402 /* reset all devices */
1403 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1404 re->func(re->opaque);
1406 if (report) {
1407 monitor_protocol_event(QEVENT_RESET, NULL);
1409 cpu_synchronize_all_post_reset();
1412 void qemu_system_reset_request(void)
1414 if (no_reboot) {
1415 shutdown_requested = 1;
1416 } else {
1417 reset_requested = 1;
1419 cpu_stop_current();
1420 qemu_notify_event();
1423 static void qemu_system_suspend(void)
1425 pause_all_vcpus();
1426 notifier_list_notify(&suspend_notifiers, NULL);
1427 runstate_set(RUN_STATE_SUSPENDED);
1428 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1431 void qemu_system_suspend_request(void)
1433 if (runstate_check(RUN_STATE_SUSPENDED)) {
1434 return;
1436 suspend_requested = 1;
1437 cpu_stop_current();
1438 qemu_notify_event();
1441 void qemu_register_suspend_notifier(Notifier *notifier)
1443 notifier_list_add(&suspend_notifiers, notifier);
1446 void qemu_system_wakeup_request(WakeupReason reason)
1448 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1449 return;
1451 if (!(wakeup_reason_mask & (1 << reason))) {
1452 return;
1454 runstate_set(RUN_STATE_RUNNING);
1455 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1456 notifier_list_notify(&wakeup_notifiers, &reason);
1457 reset_requested = 1;
1458 qemu_notify_event();
1461 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1463 if (enabled) {
1464 wakeup_reason_mask |= (1 << reason);
1465 } else {
1466 wakeup_reason_mask &= ~(1 << reason);
1470 void qemu_register_wakeup_notifier(Notifier *notifier)
1472 notifier_list_add(&wakeup_notifiers, notifier);
1475 void qemu_system_killed(int signal, pid_t pid)
1477 shutdown_signal = signal;
1478 shutdown_pid = pid;
1479 no_shutdown = 0;
1480 qemu_system_shutdown_request();
1483 void qemu_system_shutdown_request(void)
1485 shutdown_requested = 1;
1486 qemu_notify_event();
1489 void qemu_system_powerdown_request(void)
1491 powerdown_requested = 1;
1492 qemu_notify_event();
1495 void qemu_system_debug_request(void)
1497 debug_requested = 1;
1498 qemu_notify_event();
1501 void qemu_system_vmstop_request(RunState state)
1503 vmstop_requested = state;
1504 qemu_notify_event();
1507 qemu_irq qemu_system_powerdown;
1509 static bool main_loop_should_exit(void)
1511 RunState r;
1512 if (qemu_debug_requested()) {
1513 vm_stop(RUN_STATE_DEBUG);
1515 if (qemu_suspend_requested()) {
1516 qemu_system_suspend();
1518 if (qemu_shutdown_requested()) {
1519 qemu_kill_report();
1520 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1521 if (no_shutdown) {
1522 vm_stop(RUN_STATE_SHUTDOWN);
1523 } else {
1524 return true;
1527 if (qemu_reset_requested()) {
1528 pause_all_vcpus();
1529 cpu_synchronize_all_states();
1530 qemu_system_reset(VMRESET_REPORT);
1531 resume_all_vcpus();
1532 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1533 runstate_check(RUN_STATE_SHUTDOWN)) {
1534 runstate_set(RUN_STATE_PAUSED);
1537 if (qemu_powerdown_requested()) {
1538 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1539 qemu_irq_raise(qemu_system_powerdown);
1541 if (qemu_vmstop_requested(&r)) {
1542 vm_stop(r);
1544 return false;
1547 static void main_loop(void)
1549 bool nonblocking;
1550 int last_io = 0;
1551 #ifdef CONFIG_PROFILER
1552 int64_t ti;
1553 #endif
1554 do {
1555 nonblocking = !kvm_enabled() && last_io > 0;
1556 #ifdef CONFIG_PROFILER
1557 ti = profile_getclock();
1558 #endif
1559 last_io = main_loop_wait(nonblocking);
1560 #ifdef CONFIG_PROFILER
1561 dev_time += profile_getclock() - ti;
1562 #endif
1563 } while (!main_loop_should_exit());
1566 static void version(void)
1568 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1571 static void help(int exitcode)
1573 version();
1574 printf("usage: %s [options] [disk_image]\n\n"
1575 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1576 error_get_progname());
1578 #define QEMU_OPTIONS_GENERATE_HELP
1579 #include "qemu-options-wrapper.h"
1581 printf("\nDuring emulation, the following keys are useful:\n"
1582 "ctrl-alt-f toggle full screen\n"
1583 "ctrl-alt-n switch to virtual console 'n'\n"
1584 "ctrl-alt toggle mouse and keyboard grab\n"
1585 "\n"
1586 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1588 exit(exitcode);
1591 #define HAS_ARG 0x0001
1593 typedef struct QEMUOption {
1594 const char *name;
1595 int flags;
1596 int index;
1597 uint32_t arch_mask;
1598 } QEMUOption;
1600 static const QEMUOption qemu_options[] = {
1601 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1602 #define QEMU_OPTIONS_GENERATE_OPTIONS
1603 #include "qemu-options-wrapper.h"
1604 { NULL },
1607 static bool vga_available(void)
1609 return qdev_exists("VGA") || qdev_exists("isa-vga");
1612 static bool cirrus_vga_available(void)
1614 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1617 static bool vmware_vga_available(void)
1619 return qdev_exists("vmware-svga");
1622 static void select_vgahw (const char *p)
1624 const char *opts;
1626 vga_interface_type = VGA_NONE;
1627 if (strstart(p, "std", &opts)) {
1628 if (vga_available()) {
1629 vga_interface_type = VGA_STD;
1630 } else {
1631 fprintf(stderr, "Error: standard VGA not available\n");
1632 exit(0);
1634 } else if (strstart(p, "cirrus", &opts)) {
1635 if (cirrus_vga_available()) {
1636 vga_interface_type = VGA_CIRRUS;
1637 } else {
1638 fprintf(stderr, "Error: Cirrus VGA not available\n");
1639 exit(0);
1641 } else if (strstart(p, "vmware", &opts)) {
1642 if (vmware_vga_available()) {
1643 vga_interface_type = VGA_VMWARE;
1644 } else {
1645 fprintf(stderr, "Error: VMWare SVGA not available\n");
1646 exit(0);
1648 } else if (strstart(p, "xenfb", &opts)) {
1649 vga_interface_type = VGA_XENFB;
1650 } else if (strstart(p, "qxl", &opts)) {
1651 vga_interface_type = VGA_QXL;
1652 } else if (!strstart(p, "none", &opts)) {
1653 invalid_vga:
1654 fprintf(stderr, "Unknown vga type: %s\n", p);
1655 exit(1);
1657 while (*opts) {
1658 const char *nextopt;
1660 if (strstart(opts, ",retrace=", &nextopt)) {
1661 opts = nextopt;
1662 if (strstart(opts, "dumb", &nextopt))
1663 vga_retrace_method = VGA_RETRACE_DUMB;
1664 else if (strstart(opts, "precise", &nextopt))
1665 vga_retrace_method = VGA_RETRACE_PRECISE;
1666 else goto invalid_vga;
1667 } else goto invalid_vga;
1668 opts = nextopt;
1672 static DisplayType select_display(const char *p)
1674 const char *opts;
1675 DisplayType display = DT_DEFAULT;
1677 if (strstart(p, "sdl", &opts)) {
1678 #ifdef CONFIG_SDL
1679 display = DT_SDL;
1680 while (*opts) {
1681 const char *nextopt;
1683 if (strstart(opts, ",frame=", &nextopt)) {
1684 opts = nextopt;
1685 if (strstart(opts, "on", &nextopt)) {
1686 no_frame = 0;
1687 } else if (strstart(opts, "off", &nextopt)) {
1688 no_frame = 1;
1689 } else {
1690 goto invalid_sdl_args;
1692 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1693 opts = nextopt;
1694 if (strstart(opts, "on", &nextopt)) {
1695 alt_grab = 1;
1696 } else if (strstart(opts, "off", &nextopt)) {
1697 alt_grab = 0;
1698 } else {
1699 goto invalid_sdl_args;
1701 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1702 opts = nextopt;
1703 if (strstart(opts, "on", &nextopt)) {
1704 ctrl_grab = 1;
1705 } else if (strstart(opts, "off", &nextopt)) {
1706 ctrl_grab = 0;
1707 } else {
1708 goto invalid_sdl_args;
1710 } else if (strstart(opts, ",window_close=", &nextopt)) {
1711 opts = nextopt;
1712 if (strstart(opts, "on", &nextopt)) {
1713 no_quit = 0;
1714 } else if (strstart(opts, "off", &nextopt)) {
1715 no_quit = 1;
1716 } else {
1717 goto invalid_sdl_args;
1719 } else {
1720 invalid_sdl_args:
1721 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1722 exit(1);
1724 opts = nextopt;
1726 #else
1727 fprintf(stderr, "SDL support is disabled\n");
1728 exit(1);
1729 #endif
1730 } else if (strstart(p, "vnc", &opts)) {
1731 #ifdef CONFIG_VNC
1732 display_remote++;
1734 if (*opts) {
1735 const char *nextopt;
1737 if (strstart(opts, "=", &nextopt)) {
1738 vnc_display = nextopt;
1741 if (!vnc_display) {
1742 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1743 exit(1);
1745 #else
1746 fprintf(stderr, "VNC support is disabled\n");
1747 exit(1);
1748 #endif
1749 } else if (strstart(p, "curses", &opts)) {
1750 #ifdef CONFIG_CURSES
1751 display = DT_CURSES;
1752 #else
1753 fprintf(stderr, "Curses support is disabled\n");
1754 exit(1);
1755 #endif
1756 } else if (strstart(p, "none", &opts)) {
1757 display = DT_NONE;
1758 } else {
1759 fprintf(stderr, "Unknown display type: %s\n", p);
1760 exit(1);
1763 return display;
1766 static int balloon_parse(const char *arg)
1768 QemuOpts *opts;
1770 if (strcmp(arg, "none") == 0) {
1771 return 0;
1774 if (!strncmp(arg, "virtio", 6)) {
1775 if (arg[6] == ',') {
1776 /* have params -> parse them */
1777 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1778 if (!opts)
1779 return -1;
1780 } else {
1781 /* create empty opts */
1782 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
1784 qemu_opt_set(opts, "driver", "virtio-balloon");
1785 return 0;
1788 return -1;
1791 char *qemu_find_file(int type, const char *name)
1793 int len;
1794 const char *subdir;
1795 char *buf;
1797 /* Try the name as a straight path first */
1798 if (access(name, R_OK) == 0) {
1799 return g_strdup(name);
1801 switch (type) {
1802 case QEMU_FILE_TYPE_BIOS:
1803 subdir = "";
1804 break;
1805 case QEMU_FILE_TYPE_KEYMAP:
1806 subdir = "keymaps/";
1807 break;
1808 default:
1809 abort();
1811 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1812 buf = g_malloc0(len);
1813 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1814 if (access(buf, R_OK)) {
1815 g_free(buf);
1816 return NULL;
1818 return buf;
1821 static int device_help_func(QemuOpts *opts, void *opaque)
1823 return qdev_device_help(opts);
1826 static int device_init_func(QemuOpts *opts, void *opaque)
1828 DeviceState *dev;
1830 dev = qdev_device_add(opts);
1831 if (!dev)
1832 return -1;
1833 return 0;
1836 static int chardev_init_func(QemuOpts *opts, void *opaque)
1838 CharDriverState *chr;
1840 chr = qemu_chr_new_from_opts(opts, NULL);
1841 if (!chr)
1842 return -1;
1843 return 0;
1846 #ifdef CONFIG_VIRTFS
1847 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1849 int ret;
1850 ret = qemu_fsdev_add(opts);
1852 return ret;
1854 #endif
1856 static int mon_init_func(QemuOpts *opts, void *opaque)
1858 CharDriverState *chr;
1859 const char *chardev;
1860 const char *mode;
1861 int flags;
1863 mode = qemu_opt_get(opts, "mode");
1864 if (mode == NULL) {
1865 mode = "readline";
1867 if (strcmp(mode, "readline") == 0) {
1868 flags = MONITOR_USE_READLINE;
1869 } else if (strcmp(mode, "control") == 0) {
1870 flags = MONITOR_USE_CONTROL;
1871 } else {
1872 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1873 exit(1);
1876 if (qemu_opt_get_bool(opts, "pretty", 0))
1877 flags |= MONITOR_USE_PRETTY;
1879 if (qemu_opt_get_bool(opts, "default", 0))
1880 flags |= MONITOR_IS_DEFAULT;
1882 chardev = qemu_opt_get(opts, "chardev");
1883 chr = qemu_chr_find(chardev);
1884 if (chr == NULL) {
1885 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1886 exit(1);
1889 monitor_init(chr, flags);
1890 return 0;
1893 static void monitor_parse(const char *optarg, const char *mode)
1895 static int monitor_device_index = 0;
1896 QemuOpts *opts;
1897 const char *p;
1898 char label[32];
1899 int def = 0;
1901 if (strstart(optarg, "chardev:", &p)) {
1902 snprintf(label, sizeof(label), "%s", p);
1903 } else {
1904 snprintf(label, sizeof(label), "compat_monitor%d",
1905 monitor_device_index);
1906 if (monitor_device_index == 0) {
1907 def = 1;
1909 opts = qemu_chr_parse_compat(label, optarg);
1910 if (!opts) {
1911 fprintf(stderr, "parse error: %s\n", optarg);
1912 exit(1);
1916 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
1917 if (!opts) {
1918 fprintf(stderr, "duplicate chardev: %s\n", label);
1919 exit(1);
1921 qemu_opt_set(opts, "mode", mode);
1922 qemu_opt_set(opts, "chardev", label);
1923 if (def)
1924 qemu_opt_set(opts, "default", "on");
1925 monitor_device_index++;
1928 struct device_config {
1929 enum {
1930 DEV_USB, /* -usbdevice */
1931 DEV_BT, /* -bt */
1932 DEV_SERIAL, /* -serial */
1933 DEV_PARALLEL, /* -parallel */
1934 DEV_VIRTCON, /* -virtioconsole */
1935 DEV_DEBUGCON, /* -debugcon */
1936 DEV_GDB, /* -gdb, -s */
1937 } type;
1938 const char *cmdline;
1939 Location loc;
1940 QTAILQ_ENTRY(device_config) next;
1942 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1944 static void add_device_config(int type, const char *cmdline)
1946 struct device_config *conf;
1948 conf = g_malloc0(sizeof(*conf));
1949 conf->type = type;
1950 conf->cmdline = cmdline;
1951 loc_save(&conf->loc);
1952 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1955 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1957 struct device_config *conf;
1958 int rc;
1960 QTAILQ_FOREACH(conf, &device_configs, next) {
1961 if (conf->type != type)
1962 continue;
1963 loc_push_restore(&conf->loc);
1964 rc = func(conf->cmdline);
1965 loc_pop(&conf->loc);
1966 if (0 != rc)
1967 return rc;
1969 return 0;
1972 static int serial_parse(const char *devname)
1974 static int index = 0;
1975 char label[32];
1977 if (strcmp(devname, "none") == 0)
1978 return 0;
1979 if (index == MAX_SERIAL_PORTS) {
1980 fprintf(stderr, "qemu: too many serial ports\n");
1981 exit(1);
1983 snprintf(label, sizeof(label), "serial%d", index);
1984 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1985 if (!serial_hds[index]) {
1986 fprintf(stderr, "qemu: could not connect serial device"
1987 " to character backend '%s'\n", devname);
1988 return -1;
1990 index++;
1991 return 0;
1994 static int parallel_parse(const char *devname)
1996 static int index = 0;
1997 char label[32];
1999 if (strcmp(devname, "none") == 0)
2000 return 0;
2001 if (index == MAX_PARALLEL_PORTS) {
2002 fprintf(stderr, "qemu: too many parallel ports\n");
2003 exit(1);
2005 snprintf(label, sizeof(label), "parallel%d", index);
2006 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2007 if (!parallel_hds[index]) {
2008 fprintf(stderr, "qemu: could not connect parallel device"
2009 " to character backend '%s'\n", devname);
2010 return -1;
2012 index++;
2013 return 0;
2016 static int virtcon_parse(const char *devname)
2018 QemuOptsList *device = qemu_find_opts("device");
2019 static int index = 0;
2020 char label[32];
2021 QemuOpts *bus_opts, *dev_opts;
2023 if (strcmp(devname, "none") == 0)
2024 return 0;
2025 if (index == MAX_VIRTIO_CONSOLES) {
2026 fprintf(stderr, "qemu: too many virtio consoles\n");
2027 exit(1);
2030 bus_opts = qemu_opts_create(device, NULL, 0, NULL);
2031 if (arch_type == QEMU_ARCH_S390X) {
2032 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2033 } else {
2034 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2037 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2038 qemu_opt_set(dev_opts, "driver", "virtconsole");
2040 snprintf(label, sizeof(label), "virtcon%d", index);
2041 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2042 if (!virtcon_hds[index]) {
2043 fprintf(stderr, "qemu: could not connect virtio console"
2044 " to character backend '%s'\n", devname);
2045 return -1;
2047 qemu_opt_set(dev_opts, "chardev", label);
2049 index++;
2050 return 0;
2053 static int debugcon_parse(const char *devname)
2055 QemuOpts *opts;
2057 if (!qemu_chr_new("debugcon", devname, NULL)) {
2058 exit(1);
2060 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2061 if (!opts) {
2062 fprintf(stderr, "qemu: already have a debugcon device\n");
2063 exit(1);
2065 qemu_opt_set(opts, "driver", "isa-debugcon");
2066 qemu_opt_set(opts, "chardev", "debugcon");
2067 return 0;
2070 static QEMUMachine *machine_parse(const char *name)
2072 QEMUMachine *m, *machine = NULL;
2074 if (name) {
2075 machine = find_machine(name);
2077 if (machine) {
2078 return machine;
2080 printf("Supported machines are:\n");
2081 for (m = first_machine; m != NULL; m = m->next) {
2082 if (m->alias) {
2083 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2085 printf("%-20s %s%s\n", m->name, m->desc,
2086 m->is_default ? " (default)" : "");
2088 exit(!name || !is_help_option(name));
2091 static int tcg_init(void)
2093 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2094 return 0;
2097 static struct {
2098 const char *opt_name;
2099 const char *name;
2100 int (*available)(void);
2101 int (*init)(void);
2102 int *allowed;
2103 } accel_list[] = {
2104 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2105 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2106 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2107 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2110 static int configure_accelerator(void)
2112 const char *p = NULL;
2113 char buf[10];
2114 int i, ret;
2115 bool accel_initialised = 0;
2116 bool init_failed = 0;
2118 QemuOptsList *list = qemu_find_opts("machine");
2119 if (!QTAILQ_EMPTY(&list->head)) {
2120 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2123 if (p == NULL) {
2124 /* Use the default "accelerator", tcg */
2125 p = "tcg";
2128 while (!accel_initialised && *p != '\0') {
2129 if (*p == ':') {
2130 p++;
2132 p = get_opt_name(buf, sizeof (buf), p, ':');
2133 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2134 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2135 *(accel_list[i].allowed) = 1;
2136 ret = accel_list[i].init();
2137 if (ret < 0) {
2138 init_failed = 1;
2139 if (!accel_list[i].available()) {
2140 printf("%s not supported for this target\n",
2141 accel_list[i].name);
2142 } else {
2143 fprintf(stderr, "failed to initialize %s: %s\n",
2144 accel_list[i].name,
2145 strerror(-ret));
2147 *(accel_list[i].allowed) = 0;
2148 } else {
2149 accel_initialised = 1;
2151 break;
2154 if (i == ARRAY_SIZE(accel_list)) {
2155 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2159 if (!accel_initialised) {
2160 fprintf(stderr, "No accelerator found!\n");
2161 exit(1);
2164 if (init_failed) {
2165 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2168 return !accel_initialised;
2171 void qemu_add_exit_notifier(Notifier *notify)
2173 notifier_list_add(&exit_notifiers, notify);
2176 void qemu_remove_exit_notifier(Notifier *notify)
2178 notifier_remove(notify);
2181 static void qemu_run_exit_notifiers(void)
2183 notifier_list_notify(&exit_notifiers, NULL);
2186 void qemu_add_machine_init_done_notifier(Notifier *notify)
2188 notifier_list_add(&machine_init_done_notifiers, notify);
2191 static void qemu_run_machine_init_done_notifiers(void)
2193 notifier_list_notify(&machine_init_done_notifiers, NULL);
2196 static const QEMUOption *lookup_opt(int argc, char **argv,
2197 const char **poptarg, int *poptind)
2199 const QEMUOption *popt;
2200 int optind = *poptind;
2201 char *r = argv[optind];
2202 const char *optarg;
2204 loc_set_cmdline(argv, optind, 1);
2205 optind++;
2206 /* Treat --foo the same as -foo. */
2207 if (r[1] == '-')
2208 r++;
2209 popt = qemu_options;
2210 for(;;) {
2211 if (!popt->name) {
2212 error_report("invalid option");
2213 exit(1);
2215 if (!strcmp(popt->name, r + 1))
2216 break;
2217 popt++;
2219 if (popt->flags & HAS_ARG) {
2220 if (optind >= argc) {
2221 error_report("requires an argument");
2222 exit(1);
2224 optarg = argv[optind++];
2225 loc_set_cmdline(argv, optind - 2, 2);
2226 } else {
2227 optarg = NULL;
2230 *poptarg = optarg;
2231 *poptind = optind;
2233 return popt;
2236 static gpointer malloc_and_trace(gsize n_bytes)
2238 void *ptr = malloc(n_bytes);
2239 trace_g_malloc(n_bytes, ptr);
2240 return ptr;
2243 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2245 void *ptr = realloc(mem, n_bytes);
2246 trace_g_realloc(mem, n_bytes, ptr);
2247 return ptr;
2250 static void free_and_trace(gpointer mem)
2252 trace_g_free(mem);
2253 free(mem);
2256 int qemu_init_main_loop(void)
2258 return main_loop_init();
2261 int main(int argc, char **argv, char **envp)
2263 int i;
2264 int snapshot, linux_boot;
2265 const char *icount_option = NULL;
2266 const char *initrd_filename;
2267 const char *kernel_filename, *kernel_cmdline;
2268 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2269 DisplayState *ds;
2270 DisplayChangeListener *dcl;
2271 int cyls, heads, secs, translation;
2272 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2273 QemuOptsList *olist;
2274 int optind;
2275 const char *optarg;
2276 const char *loadvm = NULL;
2277 QEMUMachine *machine;
2278 const char *cpu_model;
2279 const char *vga_model = "none";
2280 const char *pid_file = NULL;
2281 const char *incoming = NULL;
2282 #ifdef CONFIG_VNC
2283 int show_vnc_port = 0;
2284 #endif
2285 bool defconfig = true;
2286 bool userconfig = true;
2287 const char *log_mask = NULL;
2288 const char *log_file = NULL;
2289 GMemVTable mem_trace = {
2290 .malloc = malloc_and_trace,
2291 .realloc = realloc_and_trace,
2292 .free = free_and_trace,
2294 const char *trace_events = NULL;
2295 const char *trace_file = NULL;
2297 atexit(qemu_run_exit_notifiers);
2298 error_set_progname(argv[0]);
2300 g_mem_set_vtable(&mem_trace);
2301 if (!g_thread_supported()) {
2302 #if !GLIB_CHECK_VERSION(2, 31, 0)
2303 g_thread_init(NULL);
2304 #else
2305 fprintf(stderr, "glib threading failed to initialize.\n");
2306 exit(1);
2307 #endif
2310 module_call_init(MODULE_INIT_QOM);
2312 runstate_init();
2314 init_clocks();
2315 rtc_clock = host_clock;
2317 qemu_cache_utils_init(envp);
2319 QLIST_INIT (&vm_change_state_head);
2320 os_setup_early_signal_handling();
2322 module_call_init(MODULE_INIT_MACHINE);
2323 machine = find_default_machine();
2324 cpu_model = NULL;
2325 ram_size = 0;
2326 snapshot = 0;
2327 cyls = heads = secs = 0;
2328 translation = BIOS_ATA_TRANSLATION_AUTO;
2330 for (i = 0; i < MAX_NODES; i++) {
2331 node_mem[i] = 0;
2332 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
2335 nb_numa_nodes = 0;
2336 nb_nics = 0;
2338 autostart= 1;
2340 /* first pass of option parsing */
2341 optind = 1;
2342 while (optind < argc) {
2343 if (argv[optind][0] != '-') {
2344 /* disk image */
2345 optind++;
2346 continue;
2347 } else {
2348 const QEMUOption *popt;
2350 popt = lookup_opt(argc, argv, &optarg, &optind);
2351 switch (popt->index) {
2352 case QEMU_OPTION_nodefconfig:
2353 defconfig = false;
2354 break;
2355 case QEMU_OPTION_nouserconfig:
2356 userconfig = false;
2357 break;
2362 if (defconfig) {
2363 int ret;
2364 ret = qemu_read_default_config_files(userconfig);
2365 if (ret < 0) {
2366 exit(1);
2370 /* second pass of option parsing */
2371 optind = 1;
2372 for(;;) {
2373 if (optind >= argc)
2374 break;
2375 if (argv[optind][0] != '-') {
2376 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2377 } else {
2378 const QEMUOption *popt;
2380 popt = lookup_opt(argc, argv, &optarg, &optind);
2381 if (!(popt->arch_mask & arch_type)) {
2382 printf("Option %s not supported for this target\n", popt->name);
2383 exit(1);
2385 switch(popt->index) {
2386 case QEMU_OPTION_M:
2387 machine = machine_parse(optarg);
2388 break;
2389 case QEMU_OPTION_cpu:
2390 /* hw initialization will check this */
2391 cpu_model = optarg;
2392 break;
2393 case QEMU_OPTION_hda:
2395 char buf[256];
2396 if (cyls == 0)
2397 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2398 else
2399 snprintf(buf, sizeof(buf),
2400 "%s,cyls=%d,heads=%d,secs=%d%s",
2401 HD_OPTS , cyls, heads, secs,
2402 translation == BIOS_ATA_TRANSLATION_LBA ?
2403 ",trans=lba" :
2404 translation == BIOS_ATA_TRANSLATION_NONE ?
2405 ",trans=none" : "");
2406 drive_add(IF_DEFAULT, 0, optarg, buf);
2407 break;
2409 case QEMU_OPTION_hdb:
2410 case QEMU_OPTION_hdc:
2411 case QEMU_OPTION_hdd:
2412 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2413 HD_OPTS);
2414 break;
2415 case QEMU_OPTION_drive:
2416 if (drive_def(optarg) == NULL) {
2417 exit(1);
2419 break;
2420 case QEMU_OPTION_set:
2421 if (qemu_set_option(optarg) != 0)
2422 exit(1);
2423 break;
2424 case QEMU_OPTION_global:
2425 if (qemu_global_option(optarg) != 0)
2426 exit(1);
2427 break;
2428 case QEMU_OPTION_mtdblock:
2429 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2430 break;
2431 case QEMU_OPTION_sd:
2432 drive_add(IF_SD, 0, optarg, SD_OPTS);
2433 break;
2434 case QEMU_OPTION_pflash:
2435 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2436 break;
2437 case QEMU_OPTION_snapshot:
2438 snapshot = 1;
2439 break;
2440 case QEMU_OPTION_hdachs:
2442 const char *p;
2443 p = optarg;
2444 cyls = strtol(p, (char **)&p, 0);
2445 if (cyls < 1 || cyls > 16383)
2446 goto chs_fail;
2447 if (*p != ',')
2448 goto chs_fail;
2449 p++;
2450 heads = strtol(p, (char **)&p, 0);
2451 if (heads < 1 || heads > 16)
2452 goto chs_fail;
2453 if (*p != ',')
2454 goto chs_fail;
2455 p++;
2456 secs = strtol(p, (char **)&p, 0);
2457 if (secs < 1 || secs > 63)
2458 goto chs_fail;
2459 if (*p == ',') {
2460 p++;
2461 if (!strcmp(p, "none"))
2462 translation = BIOS_ATA_TRANSLATION_NONE;
2463 else if (!strcmp(p, "lba"))
2464 translation = BIOS_ATA_TRANSLATION_LBA;
2465 else if (!strcmp(p, "auto"))
2466 translation = BIOS_ATA_TRANSLATION_AUTO;
2467 else
2468 goto chs_fail;
2469 } else if (*p != '\0') {
2470 chs_fail:
2471 fprintf(stderr, "qemu: invalid physical CHS format\n");
2472 exit(1);
2474 if (hda_opts != NULL) {
2475 char num[16];
2476 snprintf(num, sizeof(num), "%d", cyls);
2477 qemu_opt_set(hda_opts, "cyls", num);
2478 snprintf(num, sizeof(num), "%d", heads);
2479 qemu_opt_set(hda_opts, "heads", num);
2480 snprintf(num, sizeof(num), "%d", secs);
2481 qemu_opt_set(hda_opts, "secs", num);
2482 if (translation == BIOS_ATA_TRANSLATION_LBA)
2483 qemu_opt_set(hda_opts, "trans", "lba");
2484 if (translation == BIOS_ATA_TRANSLATION_NONE)
2485 qemu_opt_set(hda_opts, "trans", "none");
2488 break;
2489 case QEMU_OPTION_numa:
2490 if (nb_numa_nodes >= MAX_NODES) {
2491 fprintf(stderr, "qemu: too many NUMA nodes\n");
2492 exit(1);
2494 numa_add(optarg);
2495 break;
2496 case QEMU_OPTION_display:
2497 display_type = select_display(optarg);
2498 break;
2499 case QEMU_OPTION_nographic:
2500 display_type = DT_NOGRAPHIC;
2501 break;
2502 case QEMU_OPTION_curses:
2503 #ifdef CONFIG_CURSES
2504 display_type = DT_CURSES;
2505 #else
2506 fprintf(stderr, "Curses support is disabled\n");
2507 exit(1);
2508 #endif
2509 break;
2510 case QEMU_OPTION_portrait:
2511 graphic_rotate = 90;
2512 break;
2513 case QEMU_OPTION_rotate:
2514 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2515 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2516 graphic_rotate != 180 && graphic_rotate != 270) {
2517 fprintf(stderr,
2518 "qemu: only 90, 180, 270 deg rotation is available\n");
2519 exit(1);
2521 break;
2522 case QEMU_OPTION_kernel:
2523 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2524 break;
2525 case QEMU_OPTION_initrd:
2526 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
2527 break;
2528 case QEMU_OPTION_append:
2529 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
2530 break;
2531 case QEMU_OPTION_dtb:
2532 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2533 break;
2534 case QEMU_OPTION_cdrom:
2535 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2536 break;
2537 case QEMU_OPTION_boot:
2539 static const char * const params[] = {
2540 "order", "once", "menu",
2541 "splash", "splash-time", NULL
2543 char buf[sizeof(boot_devices)];
2544 char *standard_boot_devices;
2545 int legacy = 0;
2547 if (!strchr(optarg, '=')) {
2548 legacy = 1;
2549 pstrcpy(buf, sizeof(buf), optarg);
2550 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2551 fprintf(stderr,
2552 "qemu: unknown boot parameter '%s' in '%s'\n",
2553 buf, optarg);
2554 exit(1);
2557 if (legacy ||
2558 get_param_value(buf, sizeof(buf), "order", optarg)) {
2559 validate_bootdevices(buf);
2560 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2562 if (!legacy) {
2563 if (get_param_value(buf, sizeof(buf),
2564 "once", optarg)) {
2565 validate_bootdevices(buf);
2566 standard_boot_devices = g_strdup(boot_devices);
2567 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2568 qemu_register_reset(restore_boot_devices,
2569 standard_boot_devices);
2571 if (get_param_value(buf, sizeof(buf),
2572 "menu", optarg)) {
2573 if (!strcmp(buf, "on")) {
2574 boot_menu = 1;
2575 } else if (!strcmp(buf, "off")) {
2576 boot_menu = 0;
2577 } else {
2578 fprintf(stderr,
2579 "qemu: invalid option value '%s'\n",
2580 buf);
2581 exit(1);
2584 qemu_opts_parse(qemu_find_opts("boot-opts"),
2585 optarg, 0);
2588 break;
2589 case QEMU_OPTION_fda:
2590 case QEMU_OPTION_fdb:
2591 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2592 optarg, FD_OPTS);
2593 break;
2594 case QEMU_OPTION_no_fd_bootchk:
2595 fd_bootchk = 0;
2596 break;
2597 case QEMU_OPTION_netdev:
2598 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2599 exit(1);
2601 break;
2602 case QEMU_OPTION_net:
2603 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2604 exit(1);
2606 break;
2607 #ifdef CONFIG_LIBISCSI
2608 case QEMU_OPTION_iscsi:
2609 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2610 if (!opts) {
2611 exit(1);
2613 break;
2614 #endif
2615 #ifdef CONFIG_SLIRP
2616 case QEMU_OPTION_tftp:
2617 legacy_tftp_prefix = optarg;
2618 break;
2619 case QEMU_OPTION_bootp:
2620 legacy_bootp_filename = optarg;
2621 break;
2622 case QEMU_OPTION_redir:
2623 if (net_slirp_redir(optarg) < 0)
2624 exit(1);
2625 break;
2626 #endif
2627 case QEMU_OPTION_bt:
2628 add_device_config(DEV_BT, optarg);
2629 break;
2630 case QEMU_OPTION_audio_help:
2631 if (!(audio_available())) {
2632 printf("Option %s not supported for this target\n", popt->name);
2633 exit(1);
2635 AUD_help ();
2636 exit (0);
2637 break;
2638 case QEMU_OPTION_soundhw:
2639 if (!(audio_available())) {
2640 printf("Option %s not supported for this target\n", popt->name);
2641 exit(1);
2643 select_soundhw (optarg);
2644 break;
2645 case QEMU_OPTION_h:
2646 help(0);
2647 break;
2648 case QEMU_OPTION_version:
2649 version();
2650 exit(0);
2651 break;
2652 case QEMU_OPTION_m: {
2653 int64_t value;
2654 char *end;
2656 value = strtosz(optarg, &end);
2657 if (value < 0 || *end) {
2658 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2659 exit(1);
2662 if (value != (uint64_t)(ram_addr_t)value) {
2663 fprintf(stderr, "qemu: ram size too large\n");
2664 exit(1);
2666 ram_size = value;
2667 break;
2669 case QEMU_OPTION_mempath:
2670 mem_path = optarg;
2671 break;
2672 #ifdef MAP_POPULATE
2673 case QEMU_OPTION_mem_prealloc:
2674 mem_prealloc = 1;
2675 break;
2676 #endif
2677 case QEMU_OPTION_d:
2678 log_mask = optarg;
2679 break;
2680 case QEMU_OPTION_D:
2681 log_file = optarg;
2682 break;
2683 case QEMU_OPTION_s:
2684 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2685 break;
2686 case QEMU_OPTION_gdb:
2687 add_device_config(DEV_GDB, optarg);
2688 break;
2689 case QEMU_OPTION_L:
2690 data_dir = optarg;
2691 break;
2692 case QEMU_OPTION_bios:
2693 bios_name = optarg;
2694 break;
2695 case QEMU_OPTION_singlestep:
2696 singlestep = 1;
2697 break;
2698 case QEMU_OPTION_S:
2699 autostart = 0;
2700 break;
2701 case QEMU_OPTION_k:
2702 keyboard_layout = optarg;
2703 break;
2704 case QEMU_OPTION_localtime:
2705 rtc_utc = 0;
2706 break;
2707 case QEMU_OPTION_vga:
2708 vga_model = optarg;
2709 default_vga = 0;
2710 break;
2711 case QEMU_OPTION_g:
2713 const char *p;
2714 int w, h, depth;
2715 p = optarg;
2716 w = strtol(p, (char **)&p, 10);
2717 if (w <= 0) {
2718 graphic_error:
2719 fprintf(stderr, "qemu: invalid resolution or depth\n");
2720 exit(1);
2722 if (*p != 'x')
2723 goto graphic_error;
2724 p++;
2725 h = strtol(p, (char **)&p, 10);
2726 if (h <= 0)
2727 goto graphic_error;
2728 if (*p == 'x') {
2729 p++;
2730 depth = strtol(p, (char **)&p, 10);
2731 if (depth != 8 && depth != 15 && depth != 16 &&
2732 depth != 24 && depth != 32)
2733 goto graphic_error;
2734 } else if (*p == '\0') {
2735 depth = graphic_depth;
2736 } else {
2737 goto graphic_error;
2740 graphic_width = w;
2741 graphic_height = h;
2742 graphic_depth = depth;
2744 break;
2745 case QEMU_OPTION_echr:
2747 char *r;
2748 term_escape_char = strtol(optarg, &r, 0);
2749 if (r == optarg)
2750 printf("Bad argument to echr\n");
2751 break;
2753 case QEMU_OPTION_monitor:
2754 monitor_parse(optarg, "readline");
2755 default_monitor = 0;
2756 break;
2757 case QEMU_OPTION_qmp:
2758 monitor_parse(optarg, "control");
2759 default_monitor = 0;
2760 break;
2761 case QEMU_OPTION_mon:
2762 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2763 if (!opts) {
2764 exit(1);
2766 default_monitor = 0;
2767 break;
2768 case QEMU_OPTION_chardev:
2769 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2770 if (!opts) {
2771 exit(1);
2773 break;
2774 case QEMU_OPTION_fsdev:
2775 olist = qemu_find_opts("fsdev");
2776 if (!olist) {
2777 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2778 exit(1);
2780 opts = qemu_opts_parse(olist, optarg, 1);
2781 if (!opts) {
2782 fprintf(stderr, "parse error: %s\n", optarg);
2783 exit(1);
2785 break;
2786 case QEMU_OPTION_virtfs: {
2787 QemuOpts *fsdev;
2788 QemuOpts *device;
2789 const char *writeout, *sock_fd, *socket;
2791 olist = qemu_find_opts("virtfs");
2792 if (!olist) {
2793 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2794 exit(1);
2796 opts = qemu_opts_parse(olist, optarg, 1);
2797 if (!opts) {
2798 fprintf(stderr, "parse error: %s\n", optarg);
2799 exit(1);
2802 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2803 qemu_opt_get(opts, "mount_tag") == NULL) {
2804 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2805 exit(1);
2807 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2808 qemu_opt_get(opts, "mount_tag"),
2809 1, NULL);
2810 if (!fsdev) {
2811 fprintf(stderr, "duplicate fsdev id: %s\n",
2812 qemu_opt_get(opts, "mount_tag"));
2813 exit(1);
2816 writeout = qemu_opt_get(opts, "writeout");
2817 if (writeout) {
2818 #ifdef CONFIG_SYNC_FILE_RANGE
2819 qemu_opt_set(fsdev, "writeout", writeout);
2820 #else
2821 fprintf(stderr, "writeout=immediate not supported on "
2822 "this platform\n");
2823 exit(1);
2824 #endif
2826 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
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"));
2830 socket = qemu_opt_get(opts, "socket");
2831 if (socket) {
2832 qemu_opt_set(fsdev, "socket", socket);
2834 sock_fd = qemu_opt_get(opts, "sock_fd");
2835 if (sock_fd) {
2836 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2839 qemu_opt_set_bool(fsdev, "readonly",
2840 qemu_opt_get_bool(opts, "readonly", 0));
2841 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2842 NULL);
2843 qemu_opt_set(device, "driver", "virtio-9p-pci");
2844 qemu_opt_set(device, "fsdev",
2845 qemu_opt_get(opts, "mount_tag"));
2846 qemu_opt_set(device, "mount_tag",
2847 qemu_opt_get(opts, "mount_tag"));
2848 break;
2850 case QEMU_OPTION_virtfs_synth: {
2851 QemuOpts *fsdev;
2852 QemuOpts *device;
2854 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
2855 1, NULL);
2856 if (!fsdev) {
2857 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2858 exit(1);
2860 qemu_opt_set(fsdev, "fsdriver", "synth");
2862 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2863 NULL);
2864 qemu_opt_set(device, "driver", "virtio-9p-pci");
2865 qemu_opt_set(device, "fsdev", "v_synth");
2866 qemu_opt_set(device, "mount_tag", "v_synth");
2867 break;
2869 case QEMU_OPTION_serial:
2870 add_device_config(DEV_SERIAL, optarg);
2871 default_serial = 0;
2872 if (strncmp(optarg, "mon:", 4) == 0) {
2873 default_monitor = 0;
2875 break;
2876 case QEMU_OPTION_watchdog:
2877 if (watchdog) {
2878 fprintf(stderr,
2879 "qemu: only one watchdog option may be given\n");
2880 return 1;
2882 watchdog = optarg;
2883 break;
2884 case QEMU_OPTION_watchdog_action:
2885 if (select_watchdog_action(optarg) == -1) {
2886 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2887 exit(1);
2889 break;
2890 case QEMU_OPTION_virtiocon:
2891 add_device_config(DEV_VIRTCON, optarg);
2892 default_virtcon = 0;
2893 if (strncmp(optarg, "mon:", 4) == 0) {
2894 default_monitor = 0;
2896 break;
2897 case QEMU_OPTION_parallel:
2898 add_device_config(DEV_PARALLEL, optarg);
2899 default_parallel = 0;
2900 if (strncmp(optarg, "mon:", 4) == 0) {
2901 default_monitor = 0;
2903 break;
2904 case QEMU_OPTION_debugcon:
2905 add_device_config(DEV_DEBUGCON, optarg);
2906 break;
2907 case QEMU_OPTION_loadvm:
2908 loadvm = optarg;
2909 break;
2910 case QEMU_OPTION_full_screen:
2911 full_screen = 1;
2912 break;
2913 #ifdef CONFIG_SDL
2914 case QEMU_OPTION_no_frame:
2915 no_frame = 1;
2916 break;
2917 case QEMU_OPTION_alt_grab:
2918 alt_grab = 1;
2919 break;
2920 case QEMU_OPTION_ctrl_grab:
2921 ctrl_grab = 1;
2922 break;
2923 case QEMU_OPTION_no_quit:
2924 no_quit = 1;
2925 break;
2926 case QEMU_OPTION_sdl:
2927 display_type = DT_SDL;
2928 break;
2929 #else
2930 case QEMU_OPTION_no_frame:
2931 case QEMU_OPTION_alt_grab:
2932 case QEMU_OPTION_ctrl_grab:
2933 case QEMU_OPTION_no_quit:
2934 case QEMU_OPTION_sdl:
2935 fprintf(stderr, "SDL support is disabled\n");
2936 exit(1);
2937 #endif
2938 case QEMU_OPTION_pidfile:
2939 pid_file = optarg;
2940 break;
2941 case QEMU_OPTION_win2k_hack:
2942 win2k_install_hack = 1;
2943 break;
2944 case QEMU_OPTION_rtc_td_hack: {
2945 static GlobalProperty slew_lost_ticks[] = {
2947 .driver = "mc146818rtc",
2948 .property = "lost_tick_policy",
2949 .value = "slew",
2951 { /* end of list */ }
2954 qdev_prop_register_global_list(slew_lost_ticks);
2955 break;
2957 case QEMU_OPTION_acpitable:
2958 do_acpitable_option(optarg);
2959 break;
2960 case QEMU_OPTION_smbios:
2961 do_smbios_option(optarg);
2962 break;
2963 case QEMU_OPTION_enable_kvm:
2964 olist = qemu_find_opts("machine");
2965 qemu_opts_parse(olist, "accel=kvm", 0);
2966 break;
2967 case QEMU_OPTION_machine:
2968 olist = qemu_find_opts("machine");
2969 opts = qemu_opts_parse(olist, optarg, 1);
2970 if (!opts) {
2971 fprintf(stderr, "parse error: %s\n", optarg);
2972 exit(1);
2974 optarg = qemu_opt_get(opts, "type");
2975 if (optarg) {
2976 machine = machine_parse(optarg);
2978 break;
2979 case QEMU_OPTION_usb:
2980 usb_enabled = 1;
2981 break;
2982 case QEMU_OPTION_usbdevice:
2983 usb_enabled = 1;
2984 add_device_config(DEV_USB, optarg);
2985 break;
2986 case QEMU_OPTION_device:
2987 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2988 exit(1);
2990 break;
2991 case QEMU_OPTION_smp:
2992 smp_parse(optarg);
2993 if (smp_cpus < 1) {
2994 fprintf(stderr, "Invalid number of CPUs\n");
2995 exit(1);
2997 if (max_cpus < smp_cpus) {
2998 fprintf(stderr, "maxcpus must be equal to or greater than "
2999 "smp\n");
3000 exit(1);
3002 if (max_cpus > 255) {
3003 fprintf(stderr, "Unsupported number of maxcpus\n");
3004 exit(1);
3006 break;
3007 case QEMU_OPTION_vnc:
3008 #ifdef CONFIG_VNC
3009 display_remote++;
3010 vnc_display = optarg;
3011 #else
3012 fprintf(stderr, "VNC support is disabled\n");
3013 exit(1);
3014 #endif
3015 break;
3016 case QEMU_OPTION_no_acpi:
3017 acpi_enabled = 0;
3018 break;
3019 case QEMU_OPTION_no_hpet:
3020 no_hpet = 1;
3021 break;
3022 case QEMU_OPTION_balloon:
3023 if (balloon_parse(optarg) < 0) {
3024 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3025 exit(1);
3027 break;
3028 case QEMU_OPTION_no_reboot:
3029 no_reboot = 1;
3030 break;
3031 case QEMU_OPTION_no_shutdown:
3032 no_shutdown = 1;
3033 break;
3034 case QEMU_OPTION_show_cursor:
3035 cursor_hide = 0;
3036 break;
3037 case QEMU_OPTION_uuid:
3038 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3039 fprintf(stderr, "Fail to parse UUID string."
3040 " Wrong format.\n");
3041 exit(1);
3043 break;
3044 case QEMU_OPTION_option_rom:
3045 if (nb_option_roms >= MAX_OPTION_ROMS) {
3046 fprintf(stderr, "Too many option ROMs\n");
3047 exit(1);
3049 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3050 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3051 option_rom[nb_option_roms].bootindex =
3052 qemu_opt_get_number(opts, "bootindex", -1);
3053 if (!option_rom[nb_option_roms].name) {
3054 fprintf(stderr, "Option ROM file is not specified\n");
3055 exit(1);
3057 nb_option_roms++;
3058 break;
3059 case QEMU_OPTION_semihosting:
3060 semihosting_enabled = 1;
3061 break;
3062 case QEMU_OPTION_name:
3063 qemu_name = g_strdup(optarg);
3065 char *p = strchr(qemu_name, ',');
3066 if (p != NULL) {
3067 *p++ = 0;
3068 if (strncmp(p, "process=", 8)) {
3069 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3070 exit(1);
3072 p += 8;
3073 os_set_proc_name(p);
3076 break;
3077 case QEMU_OPTION_prom_env:
3078 if (nb_prom_envs >= MAX_PROM_ENVS) {
3079 fprintf(stderr, "Too many prom variables\n");
3080 exit(1);
3082 prom_envs[nb_prom_envs] = optarg;
3083 nb_prom_envs++;
3084 break;
3085 case QEMU_OPTION_old_param:
3086 old_param = 1;
3087 break;
3088 case QEMU_OPTION_clock:
3089 configure_alarms(optarg);
3090 break;
3091 case QEMU_OPTION_startdate:
3092 configure_rtc_date_offset(optarg, 1);
3093 break;
3094 case QEMU_OPTION_rtc:
3095 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3096 if (!opts) {
3097 exit(1);
3099 configure_rtc(opts);
3100 break;
3101 case QEMU_OPTION_tb_size:
3102 tcg_tb_size = strtol(optarg, NULL, 0);
3103 if (tcg_tb_size < 0) {
3104 tcg_tb_size = 0;
3106 break;
3107 case QEMU_OPTION_icount:
3108 icount_option = optarg;
3109 break;
3110 case QEMU_OPTION_incoming:
3111 incoming = optarg;
3112 runstate_set(RUN_STATE_INMIGRATE);
3113 break;
3114 case QEMU_OPTION_nodefaults:
3115 default_serial = 0;
3116 default_parallel = 0;
3117 default_virtcon = 0;
3118 default_monitor = 0;
3119 default_net = 0;
3120 default_floppy = 0;
3121 default_cdrom = 0;
3122 default_sdcard = 0;
3123 default_vga = 0;
3124 break;
3125 case QEMU_OPTION_xen_domid:
3126 if (!(xen_available())) {
3127 printf("Option %s not supported for this target\n", popt->name);
3128 exit(1);
3130 xen_domid = atoi(optarg);
3131 break;
3132 case QEMU_OPTION_xen_create:
3133 if (!(xen_available())) {
3134 printf("Option %s not supported for this target\n", popt->name);
3135 exit(1);
3137 xen_mode = XEN_CREATE;
3138 break;
3139 case QEMU_OPTION_xen_attach:
3140 if (!(xen_available())) {
3141 printf("Option %s not supported for this target\n", popt->name);
3142 exit(1);
3144 xen_mode = XEN_ATTACH;
3145 break;
3146 case QEMU_OPTION_trace:
3148 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3149 if (!opts) {
3150 exit(1);
3152 trace_events = qemu_opt_get(opts, "events");
3153 trace_file = qemu_opt_get(opts, "file");
3154 break;
3156 case QEMU_OPTION_readconfig:
3158 int ret = qemu_read_config_file(optarg);
3159 if (ret < 0) {
3160 fprintf(stderr, "read config %s: %s\n", optarg,
3161 strerror(-ret));
3162 exit(1);
3164 break;
3166 case QEMU_OPTION_spice:
3167 olist = qemu_find_opts("spice");
3168 if (!olist) {
3169 fprintf(stderr, "spice is not supported by this qemu build.\n");
3170 exit(1);
3172 opts = qemu_opts_parse(olist, optarg, 0);
3173 if (!opts) {
3174 fprintf(stderr, "parse error: %s\n", optarg);
3175 exit(1);
3177 break;
3178 case QEMU_OPTION_writeconfig:
3180 FILE *fp;
3181 if (strcmp(optarg, "-") == 0) {
3182 fp = stdout;
3183 } else {
3184 fp = fopen(optarg, "w");
3185 if (fp == NULL) {
3186 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3187 exit(1);
3190 qemu_config_write(fp);
3191 fclose(fp);
3192 break;
3194 case QEMU_OPTION_qtest:
3195 qtest_chrdev = optarg;
3196 break;
3197 case QEMU_OPTION_qtest_log:
3198 qtest_log = optarg;
3199 break;
3200 default:
3201 os_parse_cmd_args(popt->index, optarg);
3205 loc_set_none();
3207 if (machine->hw_version) {
3208 qemu_set_version(machine->hw_version);
3211 /* Init CPU def lists, based on config
3212 * - Must be called after all the qemu_read_config_file() calls
3213 * - Must be called before list_cpus()
3214 * - Must be called before machine->init()
3216 cpudef_init();
3218 if (cpu_model && is_help_option(cpu_model)) {
3219 list_cpus(stdout, &fprintf, cpu_model);
3220 exit(0);
3223 /* Open the logfile at this point, if necessary. We can't open the logfile
3224 * when encountering either of the logging options (-d or -D) because the
3225 * other one may be encountered later on the command line, changing the
3226 * location or level of logging.
3228 if (log_mask) {
3229 if (log_file) {
3230 set_cpu_log_filename(log_file);
3232 set_cpu_log(log_mask);
3235 if (!trace_backend_init(trace_events, trace_file)) {
3236 exit(1);
3239 /* If no data_dir is specified then try to find it relative to the
3240 executable path. */
3241 if (!data_dir) {
3242 data_dir = os_find_datadir(argv[0]);
3244 /* If all else fails use the install path specified when building. */
3245 if (!data_dir) {
3246 data_dir = CONFIG_QEMU_DATADIR;
3249 if (machine == NULL) {
3250 fprintf(stderr, "No machine found.\n");
3251 exit(1);
3255 * Default to max_cpus = smp_cpus, in case the user doesn't
3256 * specify a max_cpus value.
3258 if (!max_cpus)
3259 max_cpus = smp_cpus;
3261 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3262 if (smp_cpus > machine->max_cpus) {
3263 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3264 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3265 machine->max_cpus);
3266 exit(1);
3270 * Get the default machine options from the machine if it is not already
3271 * specified either by the configuration file or by the command line.
3273 if (machine->default_machine_opts) {
3274 qemu_opts_set_defaults(qemu_find_opts("machine"),
3275 machine->default_machine_opts, 0);
3278 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3279 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3281 if (machine->no_serial) {
3282 default_serial = 0;
3284 if (machine->no_parallel) {
3285 default_parallel = 0;
3287 if (!machine->use_virtcon) {
3288 default_virtcon = 0;
3290 if (machine->no_floppy) {
3291 default_floppy = 0;
3293 if (machine->no_cdrom) {
3294 default_cdrom = 0;
3296 if (machine->no_sdcard) {
3297 default_sdcard = 0;
3300 if (display_type == DT_NOGRAPHIC) {
3301 if (default_parallel)
3302 add_device_config(DEV_PARALLEL, "null");
3303 if (default_serial && default_monitor) {
3304 add_device_config(DEV_SERIAL, "mon:stdio");
3305 } else if (default_virtcon && default_monitor) {
3306 add_device_config(DEV_VIRTCON, "mon:stdio");
3307 } else {
3308 if (default_serial)
3309 add_device_config(DEV_SERIAL, "stdio");
3310 if (default_virtcon)
3311 add_device_config(DEV_VIRTCON, "stdio");
3312 if (default_monitor)
3313 monitor_parse("stdio", "readline");
3315 } else {
3316 if (default_serial)
3317 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3318 if (default_parallel)
3319 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3320 if (default_monitor)
3321 monitor_parse("vc:80Cx24C", "readline");
3322 if (default_virtcon)
3323 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3326 socket_init();
3328 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3329 exit(1);
3330 #ifdef CONFIG_VIRTFS
3331 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3332 exit(1);
3334 #endif
3336 os_daemonize();
3338 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3339 os_pidfile_error();
3340 exit(1);
3343 /* init the memory */
3344 if (ram_size == 0) {
3345 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3348 configure_accelerator();
3350 qemu_init_cpu_loop();
3351 if (qemu_init_main_loop()) {
3352 fprintf(stderr, "qemu_init_main_loop failed\n");
3353 exit(1);
3356 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3357 if (machine_opts) {
3358 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3359 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3360 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3361 } else {
3362 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3365 if (!kernel_cmdline) {
3366 kernel_cmdline = "";
3369 linux_boot = (kernel_filename != NULL);
3371 if (!linux_boot && *kernel_cmdline != '\0') {
3372 fprintf(stderr, "-append only allowed with -kernel option\n");
3373 exit(1);
3376 if (!linux_boot && initrd_filename != NULL) {
3377 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3378 exit(1);
3381 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3382 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3383 exit(1);
3386 os_set_line_buffering();
3388 if (init_timer_alarm() < 0) {
3389 fprintf(stderr, "could not initialize alarm timer\n");
3390 exit(1);
3393 #ifdef CONFIG_SPICE
3394 /* spice needs the timers to be initialized by this point */
3395 qemu_spice_init();
3396 #endif
3398 if (icount_option && (kvm_enabled() || xen_enabled())) {
3399 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3400 exit(1);
3402 configure_icount(icount_option);
3404 if (net_init_clients() < 0) {
3405 exit(1);
3408 /* init the bluetooth world */
3409 if (foreach_device_config(DEV_BT, bt_parse))
3410 exit(1);
3412 if (!xen_enabled()) {
3413 /* On 32-bit hosts, QEMU is limited by virtual address space */
3414 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3415 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3416 exit(1);
3420 cpu_exec_init_all();
3422 bdrv_init_with_whitelist();
3424 blk_mig_init();
3426 /* open the virtual block devices */
3427 if (snapshot)
3428 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3429 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3430 exit(1);
3432 default_drive(default_cdrom, snapshot, machine->use_scsi,
3433 IF_DEFAULT, 2, CDROM_OPTS);
3434 default_drive(default_floppy, snapshot, machine->use_scsi,
3435 IF_FLOPPY, 0, FD_OPTS);
3436 default_drive(default_sdcard, snapshot, machine->use_scsi,
3437 IF_SD, 0, SD_OPTS);
3439 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
3441 if (nb_numa_nodes > 0) {
3442 int i;
3444 if (nb_numa_nodes > MAX_NODES) {
3445 nb_numa_nodes = MAX_NODES;
3448 /* If no memory size if given for any node, assume the default case
3449 * and distribute the available memory equally across all nodes
3451 for (i = 0; i < nb_numa_nodes; i++) {
3452 if (node_mem[i] != 0)
3453 break;
3455 if (i == nb_numa_nodes) {
3456 uint64_t usedmem = 0;
3458 /* On Linux, the each node's border has to be 8MB aligned,
3459 * the final node gets the rest.
3461 for (i = 0; i < nb_numa_nodes - 1; i++) {
3462 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3463 usedmem += node_mem[i];
3465 node_mem[i] = ram_size - usedmem;
3468 for (i = 0; i < nb_numa_nodes; i++) {
3469 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
3470 break;
3473 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3474 * must cope with this anyway, because there are BIOSes out there in
3475 * real machines which also use this scheme.
3477 if (i == nb_numa_nodes) {
3478 for (i = 0; i < max_cpus; i++) {
3479 set_bit(i, node_cpumask[i % nb_numa_nodes]);
3484 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3485 exit(1);
3488 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3489 exit(1);
3490 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3491 exit(1);
3492 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3493 exit(1);
3494 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3495 exit(1);
3497 /* If no default VGA is requested, the default is "none". */
3498 if (default_vga && cirrus_vga_available()) {
3499 vga_model = "cirrus";
3501 select_vgahw(vga_model);
3503 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3504 exit(0);
3506 if (watchdog) {
3507 i = select_watchdog(watchdog);
3508 if (i > 0)
3509 exit (i == 1 ? 1 : 0);
3512 if (machine->compat_props) {
3513 qdev_prop_register_global_list(machine->compat_props);
3515 qemu_add_globals();
3517 qdev_machine_init();
3519 machine->init(ram_size, boot_devices,
3520 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3522 cpu_synchronize_all_post_init();
3524 set_numa_modes();
3526 current_machine = machine;
3528 /* init USB devices */
3529 if (usb_enabled) {
3530 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3531 exit(1);
3534 /* init generic devices */
3535 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3536 exit(1);
3538 net_check_clients();
3540 /* just use the first displaystate for the moment */
3541 ds = get_displaystate();
3543 if (using_spice)
3544 display_remote++;
3545 if (display_type == DT_DEFAULT && !display_remote) {
3546 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3547 display_type = DT_SDL;
3548 #elif defined(CONFIG_VNC)
3549 vnc_display = "localhost:0,to=99";
3550 show_vnc_port = 1;
3551 #else
3552 display_type = DT_NONE;
3553 #endif
3557 /* init local displays */
3558 switch (display_type) {
3559 case DT_NOGRAPHIC:
3560 break;
3561 #if defined(CONFIG_CURSES)
3562 case DT_CURSES:
3563 curses_display_init(ds, full_screen);
3564 break;
3565 #endif
3566 #if defined(CONFIG_SDL)
3567 case DT_SDL:
3568 sdl_display_init(ds, full_screen, no_frame);
3569 break;
3570 #elif defined(CONFIG_COCOA)
3571 case DT_SDL:
3572 cocoa_display_init(ds, full_screen);
3573 break;
3574 #endif
3575 default:
3576 break;
3579 /* must be after terminal init, SDL library changes signal handlers */
3580 os_setup_signal_handling();
3582 #ifdef CONFIG_VNC
3583 /* init remote displays */
3584 if (vnc_display) {
3585 vnc_display_init(ds);
3586 if (vnc_display_open(ds, vnc_display) < 0) {
3587 fprintf(stderr, "Failed to start VNC server on `%s'\n",
3588 vnc_display);
3589 exit(1);
3592 if (show_vnc_port) {
3593 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3596 #endif
3597 #ifdef CONFIG_SPICE
3598 if (using_spice && !qxl_enabled) {
3599 qemu_spice_display_init(ds);
3601 #endif
3603 /* display setup */
3604 dpy_resize(ds);
3605 dcl = ds->listeners;
3606 while (dcl != NULL) {
3607 if (dcl->dpy_refresh != NULL) {
3608 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3609 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3610 break;
3612 dcl = dcl->next;
3614 text_consoles_set_display(ds);
3616 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3617 exit(1);
3620 qdev_machine_creation_done();
3622 if (rom_load_all() != 0) {
3623 fprintf(stderr, "rom loading failed\n");
3624 exit(1);
3627 /* TODO: once all bus devices are qdevified, this should be done
3628 * when bus is created by qdev.c */
3629 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3630 qemu_run_machine_init_done_notifiers();
3632 qemu_system_reset(VMRESET_SILENT);
3633 if (loadvm) {
3634 if (load_vmstate(loadvm) < 0) {
3635 autostart = 0;
3639 if (incoming) {
3640 Error *errp = NULL;
3641 int ret = qemu_start_incoming_migration(incoming, &errp);
3642 if (ret < 0) {
3643 if (error_is_set(&errp)) {
3644 fprintf(stderr, "Migrate: %s\n", error_get_pretty(errp));
3645 error_free(errp);
3647 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3648 incoming, ret);
3649 exit(ret);
3651 } else if (autostart) {
3652 vm_start();
3655 os_setup_post();
3657 resume_all_vcpus();
3658 main_loop();
3659 bdrv_close_all();
3660 pause_all_vcpus();
3661 net_cleanup();
3662 res_free();
3664 return 0;