tcg: Add tcg_high_cond
[qemu/kevin.git] / vl.c
blob02dade19fc57040ff02cbd5cd598950fd03e0259
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
67 #ifdef CONFIG_SECCOMP
68 #include "qemu-seccomp.h"
69 #endif
71 #ifdef __sun__
72 #include <sys/stat.h>
73 #include <sys/ethernet.h>
74 #include <sys/sockio.h>
75 #include <netinet/arp.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_icmp.h> // must come after ip.h
79 #include <netinet/udp.h>
80 #include <netinet/tcp.h>
81 #include <net/if.h>
82 #include <syslog.h>
83 #include <stropts.h>
84 #endif
85 #endif
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 "blockdev.h"
140 #include "hw/block-common.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 "qjson.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-options.h"
150 #include "qmp-commands.h"
151 #include "main-loop.h"
152 #ifdef CONFIG_VIRTFS
153 #include "fsdev/qemu-fsdev.h"
154 #endif
155 #include "qtest.h"
157 #include "disas.h"
159 #include "qemu_socket.h"
161 #include "slirp/libslirp.h"
163 #include "trace.h"
164 #include "trace/control.h"
165 #include "qemu-queue.h"
166 #include "cpus.h"
167 #include "arch_init.h"
168 #include "osdep.h"
170 #include "ui/qemu-spice.h"
172 //#define DEBUG_NET
173 //#define DEBUG_SLIRP
175 #define DEFAULT_RAM_SIZE 128
177 #define MAX_VIRTIO_CONSOLES 1
179 static const char *data_dir;
180 const char *bios_name = NULL;
181 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
182 DisplayType display_type = DT_DEFAULT;
183 int display_remote = 0;
184 const char* keyboard_layout = NULL;
185 ram_addr_t ram_size;
186 const char *mem_path = NULL;
187 #ifdef MAP_POPULATE
188 int mem_prealloc = 0; /* force preallocation of physical target memory */
189 #endif
190 int nb_nics;
191 NICInfo nd_table[MAX_NICS];
192 int autostart;
193 static int rtc_utc = 1;
194 static int rtc_date_offset = -1; /* -1 means no change */
195 QEMUClock *rtc_clock;
196 int vga_interface_type = VGA_NONE;
197 static int full_screen = 0;
198 #ifdef CONFIG_SDL
199 static int no_frame = 0;
200 #endif
201 int no_quit = 0;
202 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
203 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
204 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
205 int win2k_install_hack = 0;
206 int usb_enabled = 0;
207 int singlestep = 0;
208 int smp_cpus = 1;
209 int max_cpus = 0;
210 int smp_cores = 1;
211 int smp_threads = 1;
212 #ifdef CONFIG_VNC
213 const char *vnc_display;
214 #endif
215 int acpi_enabled = 1;
216 int no_hpet = 0;
217 int fd_bootchk = 1;
218 int no_reboot = 0;
219 int no_shutdown = 0;
220 int cursor_hide = 1;
221 int graphic_rotate = 0;
222 const char *watchdog;
223 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
224 int nb_option_roms;
225 int semihosting_enabled = 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 unsigned long *node_cpumask[MAX_NODES];
252 uint8_t qemu_uuid[16];
254 static QEMUBootSetHandler *boot_set_handler;
255 static void *boot_set_opaque;
257 static NotifierList exit_notifiers =
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
260 static NotifierList machine_init_done_notifiers =
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
263 static int tcg_allowed = 1;
264 int kvm_allowed = 0;
265 int xen_allowed = 0;
266 uint32_t xen_domid;
267 enum xen_mode xen_mode = XEN_EMULATE;
268 static int tcg_tb_size;
270 static int default_serial = 1;
271 static int default_parallel = 1;
272 static int default_virtcon = 1;
273 static int default_monitor = 1;
274 static int default_floppy = 1;
275 static int default_cdrom = 1;
276 static int default_sdcard = 1;
277 static int default_vga = 1;
279 static struct {
280 const char *driver;
281 int *flag;
282 } default_list[] = {
283 { .driver = "isa-serial", .flag = &default_serial },
284 { .driver = "isa-parallel", .flag = &default_parallel },
285 { .driver = "isa-fdc", .flag = &default_floppy },
286 { .driver = "ide-cd", .flag = &default_cdrom },
287 { .driver = "ide-hd", .flag = &default_cdrom },
288 { .driver = "ide-drive", .flag = &default_cdrom },
289 { .driver = "scsi-cd", .flag = &default_cdrom },
290 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
291 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
292 { .driver = "virtio-serial", .flag = &default_virtcon },
293 { .driver = "VGA", .flag = &default_vga },
294 { .driver = "isa-vga", .flag = &default_vga },
295 { .driver = "cirrus-vga", .flag = &default_vga },
296 { .driver = "isa-cirrus-vga", .flag = &default_vga },
297 { .driver = "vmware-svga", .flag = &default_vga },
298 { .driver = "qxl-vga", .flag = &default_vga },
301 const char *qemu_get_vm_name(void)
303 return qemu_name;
306 static void res_free(void)
308 if (boot_splash_filedata != NULL) {
309 g_free(boot_splash_filedata);
310 boot_splash_filedata = NULL;
314 static int default_driver_check(QemuOpts *opts, void *opaque)
316 const char *driver = qemu_opt_get(opts, "driver");
317 int i;
319 if (!driver)
320 return 0;
321 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
322 if (strcmp(default_list[i].driver, driver) != 0)
323 continue;
324 *(default_list[i].flag) = 0;
326 return 0;
329 /***********************************************************/
330 /* QEMU state */
332 static RunState current_run_state = RUN_STATE_PRELAUNCH;
334 typedef struct {
335 RunState from;
336 RunState to;
337 } RunStateTransition;
339 static const RunStateTransition runstate_transitions_def[] = {
340 /* from -> to */
341 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
343 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
344 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
346 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
347 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
349 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
350 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
352 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
353 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
355 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
356 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
358 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
359 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
360 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
362 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
363 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
365 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
367 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
368 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
369 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
370 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
371 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
372 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
373 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
374 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
375 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
377 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
379 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
380 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
382 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
383 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
384 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
385 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
387 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
388 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
390 { RUN_STATE_MAX, RUN_STATE_MAX },
393 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
395 bool runstate_check(RunState state)
397 return current_run_state == state;
400 void runstate_init(void)
402 const RunStateTransition *p;
404 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
406 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
407 runstate_valid_transitions[p->from][p->to] = true;
411 /* This function will abort() on invalid state transitions */
412 void runstate_set(RunState new_state)
414 assert(new_state < RUN_STATE_MAX);
416 if (!runstate_valid_transitions[current_run_state][new_state]) {
417 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
418 RunState_lookup[current_run_state],
419 RunState_lookup[new_state]);
420 abort();
423 current_run_state = new_state;
426 int runstate_is_running(void)
428 return runstate_check(RUN_STATE_RUNNING);
431 StatusInfo *qmp_query_status(Error **errp)
433 StatusInfo *info = g_malloc0(sizeof(*info));
435 info->running = runstate_is_running();
436 info->singlestep = singlestep;
437 info->status = current_run_state;
439 return info;
442 /***********************************************************/
443 /* real time host monotonic timer */
445 /***********************************************************/
446 /* host time/date access */
447 void qemu_get_timedate(struct tm *tm, int offset)
449 time_t ti;
450 struct tm *ret;
452 time(&ti);
453 ti += offset;
454 if (rtc_date_offset == -1) {
455 if (rtc_utc)
456 ret = gmtime(&ti);
457 else
458 ret = localtime(&ti);
459 } else {
460 ti -= rtc_date_offset;
461 ret = gmtime(&ti);
464 memcpy(tm, ret, sizeof(struct tm));
467 int qemu_timedate_diff(struct tm *tm)
469 time_t seconds;
471 if (rtc_date_offset == -1)
472 if (rtc_utc)
473 seconds = mktimegm(tm);
474 else {
475 struct tm tmp = *tm;
476 tmp.tm_isdst = -1; /* use timezone to figure it out */
477 seconds = mktime(&tmp);
479 else
480 seconds = mktimegm(tm) + rtc_date_offset;
482 return seconds - time(NULL);
485 void rtc_change_mon_event(struct tm *tm)
487 QObject *data;
489 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
490 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
491 qobject_decref(data);
494 static void configure_rtc_date_offset(const char *startdate, int legacy)
496 time_t rtc_start_date;
497 struct tm tm;
499 if (!strcmp(startdate, "now") && legacy) {
500 rtc_date_offset = -1;
501 } else {
502 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
503 &tm.tm_year,
504 &tm.tm_mon,
505 &tm.tm_mday,
506 &tm.tm_hour,
507 &tm.tm_min,
508 &tm.tm_sec) == 6) {
509 /* OK */
510 } else if (sscanf(startdate, "%d-%d-%d",
511 &tm.tm_year,
512 &tm.tm_mon,
513 &tm.tm_mday) == 3) {
514 tm.tm_hour = 0;
515 tm.tm_min = 0;
516 tm.tm_sec = 0;
517 } else {
518 goto date_fail;
520 tm.tm_year -= 1900;
521 tm.tm_mon--;
522 rtc_start_date = mktimegm(&tm);
523 if (rtc_start_date == -1) {
524 date_fail:
525 fprintf(stderr, "Invalid date format. Valid formats are:\n"
526 "'2006-06-17T16:01:21' or '2006-06-17'\n");
527 exit(1);
529 rtc_date_offset = time(NULL) - rtc_start_date;
533 static void configure_rtc(QemuOpts *opts)
535 const char *value;
537 value = qemu_opt_get(opts, "base");
538 if (value) {
539 if (!strcmp(value, "utc")) {
540 rtc_utc = 1;
541 } else if (!strcmp(value, "localtime")) {
542 rtc_utc = 0;
543 } else {
544 configure_rtc_date_offset(value, 0);
547 value = qemu_opt_get(opts, "clock");
548 if (value) {
549 if (!strcmp(value, "host")) {
550 rtc_clock = host_clock;
551 } else if (!strcmp(value, "rt")) {
552 rtc_clock = rt_clock;
553 } else if (!strcmp(value, "vm")) {
554 rtc_clock = vm_clock;
555 } else {
556 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
557 exit(1);
560 value = qemu_opt_get(opts, "driftfix");
561 if (value) {
562 if (!strcmp(value, "slew")) {
563 static GlobalProperty slew_lost_ticks[] = {
565 .driver = "mc146818rtc",
566 .property = "lost_tick_policy",
567 .value = "slew",
569 { /* end of list */ }
572 qdev_prop_register_global_list(slew_lost_ticks);
573 } else if (!strcmp(value, "none")) {
574 /* discard is default */
575 } else {
576 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
577 exit(1);
582 /***********************************************************/
583 /* Bluetooth support */
584 static int nb_hcis;
585 static int cur_hci;
586 static struct HCIInfo *hci_table[MAX_NICS];
588 static struct bt_vlan_s {
589 struct bt_scatternet_s net;
590 int id;
591 struct bt_vlan_s *next;
592 } *first_bt_vlan;
594 /* find or alloc a new bluetooth "VLAN" */
595 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
597 struct bt_vlan_s **pvlan, *vlan;
598 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
599 if (vlan->id == id)
600 return &vlan->net;
602 vlan = g_malloc0(sizeof(struct bt_vlan_s));
603 vlan->id = id;
604 pvlan = &first_bt_vlan;
605 while (*pvlan != NULL)
606 pvlan = &(*pvlan)->next;
607 *pvlan = vlan;
608 return &vlan->net;
611 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
615 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
617 return -ENOTSUP;
620 static struct HCIInfo null_hci = {
621 .cmd_send = null_hci_send,
622 .sco_send = null_hci_send,
623 .acl_send = null_hci_send,
624 .bdaddr_set = null_hci_addr_set,
627 struct HCIInfo *qemu_next_hci(void)
629 if (cur_hci == nb_hcis)
630 return &null_hci;
632 return hci_table[cur_hci++];
635 static struct HCIInfo *hci_init(const char *str)
637 char *endp;
638 struct bt_scatternet_s *vlan = 0;
640 if (!strcmp(str, "null"))
641 /* null */
642 return &null_hci;
643 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
644 /* host[:hciN] */
645 return bt_host_hci(str[4] ? str + 5 : "hci0");
646 else if (!strncmp(str, "hci", 3)) {
647 /* hci[,vlan=n] */
648 if (str[3]) {
649 if (!strncmp(str + 3, ",vlan=", 6)) {
650 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
651 if (*endp)
652 vlan = 0;
654 } else
655 vlan = qemu_find_bt_vlan(0);
656 if (vlan)
657 return bt_new_hci(vlan);
660 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
662 return 0;
665 static int bt_hci_parse(const char *str)
667 struct HCIInfo *hci;
668 bdaddr_t bdaddr;
670 if (nb_hcis >= MAX_NICS) {
671 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
672 return -1;
675 hci = hci_init(str);
676 if (!hci)
677 return -1;
679 bdaddr.b[0] = 0x52;
680 bdaddr.b[1] = 0x54;
681 bdaddr.b[2] = 0x00;
682 bdaddr.b[3] = 0x12;
683 bdaddr.b[4] = 0x34;
684 bdaddr.b[5] = 0x56 + nb_hcis;
685 hci->bdaddr_set(hci, bdaddr.b);
687 hci_table[nb_hcis++] = hci;
689 return 0;
692 static void bt_vhci_add(int vlan_id)
694 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
696 if (!vlan->slave)
697 fprintf(stderr, "qemu: warning: adding a VHCI to "
698 "an empty scatternet %i\n", vlan_id);
700 bt_vhci_init(bt_new_hci(vlan));
703 static struct bt_device_s *bt_device_add(const char *opt)
705 struct bt_scatternet_s *vlan;
706 int vlan_id = 0;
707 char *endp = strstr(opt, ",vlan=");
708 int len = (endp ? endp - opt : strlen(opt)) + 1;
709 char devname[10];
711 pstrcpy(devname, MIN(sizeof(devname), len), opt);
713 if (endp) {
714 vlan_id = strtol(endp + 6, &endp, 0);
715 if (*endp) {
716 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
717 return 0;
721 vlan = qemu_find_bt_vlan(vlan_id);
723 if (!vlan->slave)
724 fprintf(stderr, "qemu: warning: adding a slave device to "
725 "an empty scatternet %i\n", vlan_id);
727 if (!strcmp(devname, "keyboard"))
728 return bt_keyboard_init(vlan);
730 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
731 return 0;
734 static int bt_parse(const char *opt)
736 const char *endp, *p;
737 int vlan;
739 if (strstart(opt, "hci", &endp)) {
740 if (!*endp || *endp == ',') {
741 if (*endp)
742 if (!strstart(endp, ",vlan=", 0))
743 opt = endp + 1;
745 return bt_hci_parse(opt);
747 } else if (strstart(opt, "vhci", &endp)) {
748 if (!*endp || *endp == ',') {
749 if (*endp) {
750 if (strstart(endp, ",vlan=", &p)) {
751 vlan = strtol(p, (char **) &endp, 0);
752 if (*endp) {
753 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
754 return 1;
756 } else {
757 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
758 return 1;
760 } else
761 vlan = 0;
763 bt_vhci_add(vlan);
764 return 0;
766 } else if (strstart(opt, "device:", &endp))
767 return !bt_device_add(endp);
769 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
770 return 1;
773 static int parse_sandbox(QemuOpts *opts, void *opaque)
775 /* FIXME: change this to true for 1.3 */
776 if (qemu_opt_get_bool(opts, "enable", false)) {
777 #ifdef CONFIG_SECCOMP
778 if (seccomp_start() < 0) {
779 qerror_report(ERROR_CLASS_GENERIC_ERROR,
780 "failed to install seccomp syscall filter in the kernel");
781 return -1;
783 #else
784 qerror_report(ERROR_CLASS_GENERIC_ERROR,
785 "sandboxing request but seccomp is not compiled into this build");
786 return -1;
787 #endif
790 return 0;
793 /***********************************************************/
794 /* QEMU Block devices */
796 #define HD_OPTS "media=disk"
797 #define CDROM_OPTS "media=cdrom"
798 #define FD_OPTS ""
799 #define PFLASH_OPTS ""
800 #define MTD_OPTS ""
801 #define SD_OPTS ""
803 static int drive_init_func(QemuOpts *opts, void *opaque)
805 int *use_scsi = opaque;
807 return drive_init(opts, *use_scsi) == NULL;
810 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
812 if (NULL == qemu_opt_get(opts, "snapshot")) {
813 qemu_opt_set(opts, "snapshot", "on");
815 return 0;
818 static void default_drive(int enable, int snapshot, int use_scsi,
819 BlockInterfaceType type, int index,
820 const char *optstr)
822 QemuOpts *opts;
824 if (type == IF_DEFAULT) {
825 type = use_scsi ? IF_SCSI : IF_IDE;
828 if (!enable || drive_get_by_index(type, index)) {
829 return;
832 opts = drive_add(type, index, NULL, optstr);
833 if (snapshot) {
834 drive_enable_snapshot(opts, NULL);
836 if (!drive_init(opts, use_scsi)) {
837 exit(1);
841 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
843 boot_set_handler = func;
844 boot_set_opaque = opaque;
847 int qemu_boot_set(const char *boot_devices)
849 if (!boot_set_handler) {
850 return -EINVAL;
852 return boot_set_handler(boot_set_opaque, boot_devices);
855 static void validate_bootdevices(char *devices)
857 /* We just do some generic consistency checks */
858 const char *p;
859 int bitmap = 0;
861 for (p = devices; *p != '\0'; p++) {
862 /* Allowed boot devices are:
863 * a-b: floppy disk drives
864 * c-f: IDE disk drives
865 * g-m: machine implementation dependent drives
866 * n-p: network devices
867 * It's up to each machine implementation to check if the given boot
868 * devices match the actual hardware implementation and firmware
869 * features.
871 if (*p < 'a' || *p > 'p') {
872 fprintf(stderr, "Invalid boot device '%c'\n", *p);
873 exit(1);
875 if (bitmap & (1 << (*p - 'a'))) {
876 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
877 exit(1);
879 bitmap |= 1 << (*p - 'a');
883 static void restore_boot_devices(void *opaque)
885 char *standard_boot_devices = opaque;
886 static int first = 1;
888 /* Restore boot order and remove ourselves after the first boot */
889 if (first) {
890 first = 0;
891 return;
894 qemu_boot_set(standard_boot_devices);
896 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
897 g_free(standard_boot_devices);
900 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
901 const char *suffix)
903 FWBootEntry *node, *i;
905 if (bootindex < 0) {
906 return;
909 assert(dev != NULL || suffix != NULL);
911 node = g_malloc0(sizeof(FWBootEntry));
912 node->bootindex = bootindex;
913 node->suffix = suffix ? g_strdup(suffix) : NULL;
914 node->dev = dev;
916 QTAILQ_FOREACH(i, &fw_boot_order, link) {
917 if (i->bootindex == bootindex) {
918 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
919 exit(1);
920 } else if (i->bootindex < bootindex) {
921 continue;
923 QTAILQ_INSERT_BEFORE(i, node, link);
924 return;
926 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
930 * This function returns null terminated string that consist of new line
931 * separated device paths.
933 * memory pointed by "size" is assigned total length of the array in bytes
936 char *get_boot_devices_list(uint32_t *size)
938 FWBootEntry *i;
939 uint32_t total = 0;
940 char *list = NULL;
942 QTAILQ_FOREACH(i, &fw_boot_order, link) {
943 char *devpath = NULL, *bootpath;
944 int len;
946 if (i->dev) {
947 devpath = qdev_get_fw_dev_path(i->dev);
948 assert(devpath);
951 if (i->suffix && devpath) {
952 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
954 bootpath = g_malloc(bootpathlen);
955 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
956 g_free(devpath);
957 } else if (devpath) {
958 bootpath = devpath;
959 } else {
960 assert(i->suffix);
961 bootpath = g_strdup(i->suffix);
964 if (total) {
965 list[total-1] = '\n';
967 len = strlen(bootpath) + 1;
968 list = g_realloc(list, total + len);
969 memcpy(&list[total], bootpath, len);
970 total += len;
971 g_free(bootpath);
974 *size = total;
976 return list;
979 static void numa_add(const char *optarg)
981 char option[128];
982 char *endptr;
983 unsigned long long value, endvalue;
984 int nodenr;
986 value = endvalue = 0ULL;
988 optarg = get_opt_name(option, 128, optarg, ',') + 1;
989 if (!strcmp(option, "node")) {
990 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
991 nodenr = nb_numa_nodes;
992 } else {
993 nodenr = strtoull(option, NULL, 10);
996 if (get_param_value(option, 128, "mem", optarg) == 0) {
997 node_mem[nodenr] = 0;
998 } else {
999 int64_t sval;
1000 sval = strtosz(option, &endptr);
1001 if (sval < 0 || *endptr) {
1002 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1003 exit(1);
1005 node_mem[nodenr] = sval;
1007 if (get_param_value(option, 128, "cpus", optarg) != 0) {
1008 value = strtoull(option, &endptr, 10);
1009 if (*endptr == '-') {
1010 endvalue = strtoull(endptr+1, &endptr, 10);
1011 } else {
1012 endvalue = value;
1015 if (!(endvalue < MAX_CPUMASK_BITS)) {
1016 endvalue = MAX_CPUMASK_BITS - 1;
1017 fprintf(stderr,
1018 "A max of %d CPUs are supported in a guest\n",
1019 MAX_CPUMASK_BITS);
1022 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
1024 nb_numa_nodes++;
1026 return;
1029 static void smp_parse(const char *optarg)
1031 int smp, sockets = 0, threads = 0, cores = 0;
1032 char *endptr;
1033 char option[128];
1035 smp = strtoul(optarg, &endptr, 10);
1036 if (endptr != optarg) {
1037 if (*endptr == ',') {
1038 endptr++;
1041 if (get_param_value(option, 128, "sockets", endptr) != 0)
1042 sockets = strtoull(option, NULL, 10);
1043 if (get_param_value(option, 128, "cores", endptr) != 0)
1044 cores = strtoull(option, NULL, 10);
1045 if (get_param_value(option, 128, "threads", endptr) != 0)
1046 threads = strtoull(option, NULL, 10);
1047 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1048 max_cpus = strtoull(option, NULL, 10);
1050 /* compute missing values, prefer sockets over cores over threads */
1051 if (smp == 0 || sockets == 0) {
1052 sockets = sockets > 0 ? sockets : 1;
1053 cores = cores > 0 ? cores : 1;
1054 threads = threads > 0 ? threads : 1;
1055 if (smp == 0) {
1056 smp = cores * threads * sockets;
1058 } else {
1059 if (cores == 0) {
1060 threads = threads > 0 ? threads : 1;
1061 cores = smp / (sockets * threads);
1062 } else {
1063 threads = smp / (cores * sockets);
1066 smp_cpus = smp;
1067 smp_cores = cores > 0 ? cores : 1;
1068 smp_threads = threads > 0 ? threads : 1;
1069 if (max_cpus == 0)
1070 max_cpus = smp_cpus;
1073 /***********************************************************/
1074 /* USB devices */
1076 static int usb_device_add(const char *devname)
1078 const char *p;
1079 USBDevice *dev = NULL;
1081 if (!usb_enabled)
1082 return -1;
1084 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1085 dev = usbdevice_create(devname);
1086 if (dev)
1087 goto done;
1089 /* the other ones */
1090 #ifndef CONFIG_LINUX
1091 /* only the linux version is qdev-ified, usb-bsd still needs this */
1092 if (strstart(devname, "host:", &p)) {
1093 dev = usb_host_device_open(usb_bus_find(-1), p);
1094 } else
1095 #endif
1096 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1097 dev = usb_bt_init(usb_bus_find(-1),
1098 devname[2] ? hci_init(p)
1099 : bt_new_hci(qemu_find_bt_vlan(0)));
1100 } else {
1101 return -1;
1103 if (!dev)
1104 return -1;
1106 done:
1107 return 0;
1110 static int usb_device_del(const char *devname)
1112 int bus_num, addr;
1113 const char *p;
1115 if (strstart(devname, "host:", &p))
1116 return usb_host_device_close(p);
1118 if (!usb_enabled)
1119 return -1;
1121 p = strchr(devname, '.');
1122 if (!p)
1123 return -1;
1124 bus_num = strtoul(devname, NULL, 0);
1125 addr = strtoul(p + 1, NULL, 0);
1127 return usb_device_delete_addr(bus_num, addr);
1130 static int usb_parse(const char *cmdline)
1132 int r;
1133 r = usb_device_add(cmdline);
1134 if (r < 0) {
1135 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1137 return r;
1140 void do_usb_add(Monitor *mon, const QDict *qdict)
1142 const char *devname = qdict_get_str(qdict, "devname");
1143 if (usb_device_add(devname) < 0) {
1144 error_report("could not add USB device '%s'", devname);
1148 void do_usb_del(Monitor *mon, const QDict *qdict)
1150 const char *devname = qdict_get_str(qdict, "devname");
1151 if (usb_device_del(devname) < 0) {
1152 error_report("could not delete USB device '%s'", devname);
1156 /***********************************************************/
1157 /* PCMCIA/Cardbus */
1159 static struct pcmcia_socket_entry_s {
1160 PCMCIASocket *socket;
1161 struct pcmcia_socket_entry_s *next;
1162 } *pcmcia_sockets = 0;
1164 void pcmcia_socket_register(PCMCIASocket *socket)
1166 struct pcmcia_socket_entry_s *entry;
1168 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1169 entry->socket = socket;
1170 entry->next = pcmcia_sockets;
1171 pcmcia_sockets = entry;
1174 void pcmcia_socket_unregister(PCMCIASocket *socket)
1176 struct pcmcia_socket_entry_s *entry, **ptr;
1178 ptr = &pcmcia_sockets;
1179 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1180 if (entry->socket == socket) {
1181 *ptr = entry->next;
1182 g_free(entry);
1186 void pcmcia_info(Monitor *mon)
1188 struct pcmcia_socket_entry_s *iter;
1190 if (!pcmcia_sockets)
1191 monitor_printf(mon, "No PCMCIA sockets\n");
1193 for (iter = pcmcia_sockets; iter; iter = iter->next)
1194 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1195 iter->socket->attached ? iter->socket->card_string :
1196 "Empty");
1199 /***********************************************************/
1200 /* machine registration */
1202 static QEMUMachine *first_machine = NULL;
1203 QEMUMachine *current_machine = NULL;
1205 int qemu_register_machine(QEMUMachine *m)
1207 QEMUMachine **pm;
1208 pm = &first_machine;
1209 while (*pm != NULL)
1210 pm = &(*pm)->next;
1211 m->next = NULL;
1212 *pm = m;
1213 return 0;
1216 static QEMUMachine *find_machine(const char *name)
1218 QEMUMachine *m;
1220 for(m = first_machine; m != NULL; m = m->next) {
1221 if (!strcmp(m->name, name))
1222 return m;
1223 if (m->alias && !strcmp(m->alias, name))
1224 return m;
1226 return NULL;
1229 QEMUMachine *find_default_machine(void)
1231 QEMUMachine *m;
1233 for(m = first_machine; m != NULL; m = m->next) {
1234 if (m->is_default) {
1235 return m;
1238 return NULL;
1241 MachineInfoList *qmp_query_machines(Error **errp)
1243 MachineInfoList *mach_list = NULL;
1244 QEMUMachine *m;
1246 for (m = first_machine; m; m = m->next) {
1247 MachineInfoList *entry;
1248 MachineInfo *info;
1250 info = g_malloc0(sizeof(*info));
1251 if (m->is_default) {
1252 info->has_is_default = true;
1253 info->is_default = true;
1256 if (m->alias) {
1257 info->has_alias = true;
1258 info->alias = g_strdup(m->alias);
1261 info->name = g_strdup(m->name);
1263 entry = g_malloc0(sizeof(*entry));
1264 entry->value = info;
1265 entry->next = mach_list;
1266 mach_list = entry;
1269 return mach_list;
1272 /***********************************************************/
1273 /* main execution loop */
1275 static void gui_update(void *opaque)
1277 uint64_t interval = GUI_REFRESH_INTERVAL;
1278 DisplayState *ds = opaque;
1279 DisplayChangeListener *dcl = ds->listeners;
1281 dpy_refresh(ds);
1283 while (dcl != NULL) {
1284 if (dcl->gui_timer_interval &&
1285 dcl->gui_timer_interval < interval)
1286 interval = dcl->gui_timer_interval;
1287 dcl = dcl->next;
1289 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1292 struct vm_change_state_entry {
1293 VMChangeStateHandler *cb;
1294 void *opaque;
1295 QLIST_ENTRY (vm_change_state_entry) entries;
1298 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1300 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1301 void *opaque)
1303 VMChangeStateEntry *e;
1305 e = g_malloc0(sizeof (*e));
1307 e->cb = cb;
1308 e->opaque = opaque;
1309 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1310 return e;
1313 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1315 QLIST_REMOVE (e, entries);
1316 g_free (e);
1319 void vm_state_notify(int running, RunState state)
1321 VMChangeStateEntry *e;
1323 trace_vm_state_notify(running, state);
1325 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1326 e->cb(e->opaque, running, state);
1330 void vm_start(void)
1332 if (!runstate_is_running()) {
1333 cpu_enable_ticks();
1334 runstate_set(RUN_STATE_RUNNING);
1335 vm_state_notify(1, RUN_STATE_RUNNING);
1336 resume_all_vcpus();
1337 monitor_protocol_event(QEVENT_RESUME, NULL);
1341 /* reset/shutdown handler */
1343 typedef struct QEMUResetEntry {
1344 QTAILQ_ENTRY(QEMUResetEntry) entry;
1345 QEMUResetHandler *func;
1346 void *opaque;
1347 } QEMUResetEntry;
1349 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1350 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1351 static int reset_requested;
1352 static int shutdown_requested, shutdown_signal = -1;
1353 static pid_t shutdown_pid;
1354 static int powerdown_requested;
1355 static int debug_requested;
1356 static int suspend_requested;
1357 static int wakeup_requested;
1358 static NotifierList powerdown_notifiers =
1359 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1360 static NotifierList suspend_notifiers =
1361 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1362 static NotifierList wakeup_notifiers =
1363 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1364 static uint32_t wakeup_reason_mask = ~0;
1365 static RunState vmstop_requested = RUN_STATE_MAX;
1367 int qemu_shutdown_requested_get(void)
1369 return shutdown_requested;
1372 int qemu_reset_requested_get(void)
1374 return reset_requested;
1377 int qemu_shutdown_requested(void)
1379 int r = shutdown_requested;
1380 shutdown_requested = 0;
1381 return r;
1384 void qemu_kill_report(void)
1386 if (!qtest_enabled() && shutdown_signal != -1) {
1387 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1388 if (shutdown_pid == 0) {
1389 /* This happens for eg ^C at the terminal, so it's worth
1390 * avoiding printing an odd message in that case.
1392 fputc('\n', stderr);
1393 } else {
1394 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1396 shutdown_signal = -1;
1400 int qemu_reset_requested(void)
1402 int r = reset_requested;
1403 reset_requested = 0;
1404 return r;
1407 static int qemu_suspend_requested(void)
1409 int r = suspend_requested;
1410 suspend_requested = 0;
1411 return r;
1414 static int qemu_wakeup_requested(void)
1416 int r = wakeup_requested;
1417 wakeup_requested = 0;
1418 return r;
1421 int qemu_powerdown_requested(void)
1423 int r = powerdown_requested;
1424 powerdown_requested = 0;
1425 return r;
1428 static int qemu_debug_requested(void)
1430 int r = debug_requested;
1431 debug_requested = 0;
1432 return r;
1435 /* We use RUN_STATE_MAX but any invalid value will do */
1436 static bool qemu_vmstop_requested(RunState *r)
1438 if (vmstop_requested < RUN_STATE_MAX) {
1439 *r = vmstop_requested;
1440 vmstop_requested = RUN_STATE_MAX;
1441 return true;
1444 return false;
1447 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1449 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1451 re->func = func;
1452 re->opaque = opaque;
1453 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1456 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1458 QEMUResetEntry *re;
1460 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1461 if (re->func == func && re->opaque == opaque) {
1462 QTAILQ_REMOVE(&reset_handlers, re, entry);
1463 g_free(re);
1464 return;
1469 void qemu_devices_reset(void)
1471 QEMUResetEntry *re, *nre;
1473 /* reset all devices */
1474 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1475 re->func(re->opaque);
1479 void qemu_system_reset(bool report)
1481 if (current_machine && current_machine->reset) {
1482 current_machine->reset();
1483 } else {
1484 qemu_devices_reset();
1486 if (report) {
1487 monitor_protocol_event(QEVENT_RESET, NULL);
1489 cpu_synchronize_all_post_reset();
1492 void qemu_system_reset_request(void)
1494 if (no_reboot) {
1495 shutdown_requested = 1;
1496 } else {
1497 reset_requested = 1;
1499 cpu_stop_current();
1500 qemu_notify_event();
1503 static void qemu_system_suspend(void)
1505 pause_all_vcpus();
1506 notifier_list_notify(&suspend_notifiers, NULL);
1507 runstate_set(RUN_STATE_SUSPENDED);
1508 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1511 void qemu_system_suspend_request(void)
1513 if (runstate_check(RUN_STATE_SUSPENDED)) {
1514 return;
1516 suspend_requested = 1;
1517 cpu_stop_current();
1518 qemu_notify_event();
1521 void qemu_register_suspend_notifier(Notifier *notifier)
1523 notifier_list_add(&suspend_notifiers, notifier);
1526 void qemu_system_wakeup_request(WakeupReason reason)
1528 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1529 return;
1531 if (!(wakeup_reason_mask & (1 << reason))) {
1532 return;
1534 runstate_set(RUN_STATE_RUNNING);
1535 notifier_list_notify(&wakeup_notifiers, &reason);
1536 wakeup_requested = 1;
1537 qemu_notify_event();
1540 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1542 if (enabled) {
1543 wakeup_reason_mask |= (1 << reason);
1544 } else {
1545 wakeup_reason_mask &= ~(1 << reason);
1549 void qemu_register_wakeup_notifier(Notifier *notifier)
1551 notifier_list_add(&wakeup_notifiers, notifier);
1554 void qemu_system_killed(int signal, pid_t pid)
1556 shutdown_signal = signal;
1557 shutdown_pid = pid;
1558 no_shutdown = 0;
1559 qemu_system_shutdown_request();
1562 void qemu_system_shutdown_request(void)
1564 shutdown_requested = 1;
1565 qemu_notify_event();
1568 static void qemu_system_powerdown(void)
1570 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1571 notifier_list_notify(&powerdown_notifiers, NULL);
1574 void qemu_system_powerdown_request(void)
1576 powerdown_requested = 1;
1577 qemu_notify_event();
1580 void qemu_register_powerdown_notifier(Notifier *notifier)
1582 notifier_list_add(&powerdown_notifiers, notifier);
1585 void qemu_system_debug_request(void)
1587 debug_requested = 1;
1588 qemu_notify_event();
1591 void qemu_system_vmstop_request(RunState state)
1593 vmstop_requested = state;
1594 qemu_notify_event();
1597 static bool main_loop_should_exit(void)
1599 RunState r;
1600 if (qemu_debug_requested()) {
1601 vm_stop(RUN_STATE_DEBUG);
1603 if (qemu_suspend_requested()) {
1604 qemu_system_suspend();
1606 if (qemu_shutdown_requested()) {
1607 qemu_kill_report();
1608 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1609 if (no_shutdown) {
1610 vm_stop(RUN_STATE_SHUTDOWN);
1611 } else {
1612 return true;
1615 if (qemu_reset_requested()) {
1616 pause_all_vcpus();
1617 cpu_synchronize_all_states();
1618 qemu_system_reset(VMRESET_REPORT);
1619 resume_all_vcpus();
1620 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1621 runstate_check(RUN_STATE_SHUTDOWN)) {
1622 runstate_set(RUN_STATE_PAUSED);
1625 if (qemu_wakeup_requested()) {
1626 pause_all_vcpus();
1627 cpu_synchronize_all_states();
1628 qemu_system_reset(VMRESET_SILENT);
1629 resume_all_vcpus();
1630 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1632 if (qemu_powerdown_requested()) {
1633 qemu_system_powerdown();
1635 if (qemu_vmstop_requested(&r)) {
1636 vm_stop(r);
1638 return false;
1641 static void main_loop(void)
1643 bool nonblocking;
1644 int last_io = 0;
1645 #ifdef CONFIG_PROFILER
1646 int64_t ti;
1647 #endif
1648 do {
1649 nonblocking = !kvm_enabled() && last_io > 0;
1650 #ifdef CONFIG_PROFILER
1651 ti = profile_getclock();
1652 #endif
1653 last_io = main_loop_wait(nonblocking);
1654 #ifdef CONFIG_PROFILER
1655 dev_time += profile_getclock() - ti;
1656 #endif
1657 } while (!main_loop_should_exit());
1660 static void version(void)
1662 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1665 static void help(int exitcode)
1667 version();
1668 printf("usage: %s [options] [disk_image]\n\n"
1669 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1670 error_get_progname());
1672 #define QEMU_OPTIONS_GENERATE_HELP
1673 #include "qemu-options-wrapper.h"
1675 printf("\nDuring emulation, the following keys are useful:\n"
1676 "ctrl-alt-f toggle full screen\n"
1677 "ctrl-alt-n switch to virtual console 'n'\n"
1678 "ctrl-alt toggle mouse and keyboard grab\n"
1679 "\n"
1680 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1682 exit(exitcode);
1685 #define HAS_ARG 0x0001
1687 typedef struct QEMUOption {
1688 const char *name;
1689 int flags;
1690 int index;
1691 uint32_t arch_mask;
1692 } QEMUOption;
1694 static const QEMUOption qemu_options[] = {
1695 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1696 #define QEMU_OPTIONS_GENERATE_OPTIONS
1697 #include "qemu-options-wrapper.h"
1698 { NULL },
1701 static bool vga_available(void)
1703 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1706 static bool cirrus_vga_available(void)
1708 return object_class_by_name("cirrus-vga")
1709 || object_class_by_name("isa-cirrus-vga");
1712 static bool vmware_vga_available(void)
1714 return object_class_by_name("vmware-svga");
1717 static bool qxl_vga_available(void)
1719 return object_class_by_name("qxl-vga");
1722 static void select_vgahw (const char *p)
1724 const char *opts;
1726 vga_interface_type = VGA_NONE;
1727 if (strstart(p, "std", &opts)) {
1728 if (vga_available()) {
1729 vga_interface_type = VGA_STD;
1730 } else {
1731 fprintf(stderr, "Error: standard VGA not available\n");
1732 exit(0);
1734 } else if (strstart(p, "cirrus", &opts)) {
1735 if (cirrus_vga_available()) {
1736 vga_interface_type = VGA_CIRRUS;
1737 } else {
1738 fprintf(stderr, "Error: Cirrus VGA not available\n");
1739 exit(0);
1741 } else if (strstart(p, "vmware", &opts)) {
1742 if (vmware_vga_available()) {
1743 vga_interface_type = VGA_VMWARE;
1744 } else {
1745 fprintf(stderr, "Error: VMWare SVGA not available\n");
1746 exit(0);
1748 } else if (strstart(p, "xenfb", &opts)) {
1749 vga_interface_type = VGA_XENFB;
1750 } else if (strstart(p, "qxl", &opts)) {
1751 if (qxl_vga_available()) {
1752 vga_interface_type = VGA_QXL;
1753 } else {
1754 fprintf(stderr, "Error: QXL VGA not available\n");
1755 exit(0);
1757 } else if (!strstart(p, "none", &opts)) {
1758 invalid_vga:
1759 fprintf(stderr, "Unknown vga type: %s\n", p);
1760 exit(1);
1762 while (*opts) {
1763 const char *nextopt;
1765 if (strstart(opts, ",retrace=", &nextopt)) {
1766 opts = nextopt;
1767 if (strstart(opts, "dumb", &nextopt))
1768 vga_retrace_method = VGA_RETRACE_DUMB;
1769 else if (strstart(opts, "precise", &nextopt))
1770 vga_retrace_method = VGA_RETRACE_PRECISE;
1771 else goto invalid_vga;
1772 } else goto invalid_vga;
1773 opts = nextopt;
1777 static DisplayType select_display(const char *p)
1779 const char *opts;
1780 DisplayType display = DT_DEFAULT;
1782 if (strstart(p, "sdl", &opts)) {
1783 #ifdef CONFIG_SDL
1784 display = DT_SDL;
1785 while (*opts) {
1786 const char *nextopt;
1788 if (strstart(opts, ",frame=", &nextopt)) {
1789 opts = nextopt;
1790 if (strstart(opts, "on", &nextopt)) {
1791 no_frame = 0;
1792 } else if (strstart(opts, "off", &nextopt)) {
1793 no_frame = 1;
1794 } else {
1795 goto invalid_sdl_args;
1797 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1798 opts = nextopt;
1799 if (strstart(opts, "on", &nextopt)) {
1800 alt_grab = 1;
1801 } else if (strstart(opts, "off", &nextopt)) {
1802 alt_grab = 0;
1803 } else {
1804 goto invalid_sdl_args;
1806 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1807 opts = nextopt;
1808 if (strstart(opts, "on", &nextopt)) {
1809 ctrl_grab = 1;
1810 } else if (strstart(opts, "off", &nextopt)) {
1811 ctrl_grab = 0;
1812 } else {
1813 goto invalid_sdl_args;
1815 } else if (strstart(opts, ",window_close=", &nextopt)) {
1816 opts = nextopt;
1817 if (strstart(opts, "on", &nextopt)) {
1818 no_quit = 0;
1819 } else if (strstart(opts, "off", &nextopt)) {
1820 no_quit = 1;
1821 } else {
1822 goto invalid_sdl_args;
1824 } else {
1825 invalid_sdl_args:
1826 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1827 exit(1);
1829 opts = nextopt;
1831 #else
1832 fprintf(stderr, "SDL support is disabled\n");
1833 exit(1);
1834 #endif
1835 } else if (strstart(p, "vnc", &opts)) {
1836 #ifdef CONFIG_VNC
1837 display_remote++;
1839 if (*opts) {
1840 const char *nextopt;
1842 if (strstart(opts, "=", &nextopt)) {
1843 vnc_display = nextopt;
1846 if (!vnc_display) {
1847 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1848 exit(1);
1850 #else
1851 fprintf(stderr, "VNC support is disabled\n");
1852 exit(1);
1853 #endif
1854 } else if (strstart(p, "curses", &opts)) {
1855 #ifdef CONFIG_CURSES
1856 display = DT_CURSES;
1857 #else
1858 fprintf(stderr, "Curses support is disabled\n");
1859 exit(1);
1860 #endif
1861 } else if (strstart(p, "none", &opts)) {
1862 display = DT_NONE;
1863 } else {
1864 fprintf(stderr, "Unknown display type: %s\n", p);
1865 exit(1);
1868 return display;
1871 static int balloon_parse(const char *arg)
1873 QemuOpts *opts;
1875 if (strcmp(arg, "none") == 0) {
1876 return 0;
1879 if (!strncmp(arg, "virtio", 6)) {
1880 if (arg[6] == ',') {
1881 /* have params -> parse them */
1882 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1883 if (!opts)
1884 return -1;
1885 } else {
1886 /* create empty opts */
1887 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
1889 qemu_opt_set(opts, "driver", "virtio-balloon");
1890 return 0;
1893 return -1;
1896 char *qemu_find_file(int type, const char *name)
1898 int len;
1899 const char *subdir;
1900 char *buf;
1902 /* Try the name as a straight path first */
1903 if (access(name, R_OK) == 0) {
1904 return g_strdup(name);
1906 switch (type) {
1907 case QEMU_FILE_TYPE_BIOS:
1908 subdir = "";
1909 break;
1910 case QEMU_FILE_TYPE_KEYMAP:
1911 subdir = "keymaps/";
1912 break;
1913 default:
1914 abort();
1916 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1917 buf = g_malloc0(len);
1918 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1919 if (access(buf, R_OK)) {
1920 g_free(buf);
1921 return NULL;
1923 return buf;
1926 static int device_help_func(QemuOpts *opts, void *opaque)
1928 return qdev_device_help(opts);
1931 static int device_init_func(QemuOpts *opts, void *opaque)
1933 DeviceState *dev;
1935 dev = qdev_device_add(opts);
1936 if (!dev)
1937 return -1;
1938 return 0;
1941 static int chardev_init_func(QemuOpts *opts, void *opaque)
1943 CharDriverState *chr;
1945 chr = qemu_chr_new_from_opts(opts, NULL);
1946 if (!chr)
1947 return -1;
1948 return 0;
1951 #ifdef CONFIG_VIRTFS
1952 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1954 int ret;
1955 ret = qemu_fsdev_add(opts);
1957 return ret;
1959 #endif
1961 static int mon_init_func(QemuOpts *opts, void *opaque)
1963 CharDriverState *chr;
1964 const char *chardev;
1965 const char *mode;
1966 int flags;
1968 mode = qemu_opt_get(opts, "mode");
1969 if (mode == NULL) {
1970 mode = "readline";
1972 if (strcmp(mode, "readline") == 0) {
1973 flags = MONITOR_USE_READLINE;
1974 } else if (strcmp(mode, "control") == 0) {
1975 flags = MONITOR_USE_CONTROL;
1976 } else {
1977 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1978 exit(1);
1981 if (qemu_opt_get_bool(opts, "pretty", 0))
1982 flags |= MONITOR_USE_PRETTY;
1984 if (qemu_opt_get_bool(opts, "default", 0))
1985 flags |= MONITOR_IS_DEFAULT;
1987 chardev = qemu_opt_get(opts, "chardev");
1988 chr = qemu_chr_find(chardev);
1989 if (chr == NULL) {
1990 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1991 exit(1);
1994 monitor_init(chr, flags);
1995 return 0;
1998 static void monitor_parse(const char *optarg, const char *mode)
2000 static int monitor_device_index = 0;
2001 QemuOpts *opts;
2002 const char *p;
2003 char label[32];
2004 int def = 0;
2006 if (strstart(optarg, "chardev:", &p)) {
2007 snprintf(label, sizeof(label), "%s", p);
2008 } else {
2009 snprintf(label, sizeof(label), "compat_monitor%d",
2010 monitor_device_index);
2011 if (monitor_device_index == 0) {
2012 def = 1;
2014 opts = qemu_chr_parse_compat(label, optarg);
2015 if (!opts) {
2016 fprintf(stderr, "parse error: %s\n", optarg);
2017 exit(1);
2021 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2022 if (!opts) {
2023 fprintf(stderr, "duplicate chardev: %s\n", label);
2024 exit(1);
2026 qemu_opt_set(opts, "mode", mode);
2027 qemu_opt_set(opts, "chardev", label);
2028 if (def)
2029 qemu_opt_set(opts, "default", "on");
2030 monitor_device_index++;
2033 struct device_config {
2034 enum {
2035 DEV_USB, /* -usbdevice */
2036 DEV_BT, /* -bt */
2037 DEV_SERIAL, /* -serial */
2038 DEV_PARALLEL, /* -parallel */
2039 DEV_VIRTCON, /* -virtioconsole */
2040 DEV_DEBUGCON, /* -debugcon */
2041 DEV_GDB, /* -gdb, -s */
2042 } type;
2043 const char *cmdline;
2044 Location loc;
2045 QTAILQ_ENTRY(device_config) next;
2047 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
2049 static void add_device_config(int type, const char *cmdline)
2051 struct device_config *conf;
2053 conf = g_malloc0(sizeof(*conf));
2054 conf->type = type;
2055 conf->cmdline = cmdline;
2056 loc_save(&conf->loc);
2057 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2060 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2062 struct device_config *conf;
2063 int rc;
2065 QTAILQ_FOREACH(conf, &device_configs, next) {
2066 if (conf->type != type)
2067 continue;
2068 loc_push_restore(&conf->loc);
2069 rc = func(conf->cmdline);
2070 loc_pop(&conf->loc);
2071 if (0 != rc)
2072 return rc;
2074 return 0;
2077 static int serial_parse(const char *devname)
2079 static int index = 0;
2080 char label[32];
2082 if (strcmp(devname, "none") == 0)
2083 return 0;
2084 if (index == MAX_SERIAL_PORTS) {
2085 fprintf(stderr, "qemu: too many serial ports\n");
2086 exit(1);
2088 snprintf(label, sizeof(label), "serial%d", index);
2089 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2090 if (!serial_hds[index]) {
2091 fprintf(stderr, "qemu: could not connect serial device"
2092 " to character backend '%s'\n", devname);
2093 return -1;
2095 index++;
2096 return 0;
2099 static int parallel_parse(const char *devname)
2101 static int index = 0;
2102 char label[32];
2104 if (strcmp(devname, "none") == 0)
2105 return 0;
2106 if (index == MAX_PARALLEL_PORTS) {
2107 fprintf(stderr, "qemu: too many parallel ports\n");
2108 exit(1);
2110 snprintf(label, sizeof(label), "parallel%d", index);
2111 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2112 if (!parallel_hds[index]) {
2113 fprintf(stderr, "qemu: could not connect parallel device"
2114 " to character backend '%s'\n", devname);
2115 return -1;
2117 index++;
2118 return 0;
2121 static int virtcon_parse(const char *devname)
2123 QemuOptsList *device = qemu_find_opts("device");
2124 static int index = 0;
2125 char label[32];
2126 QemuOpts *bus_opts, *dev_opts;
2128 if (strcmp(devname, "none") == 0)
2129 return 0;
2130 if (index == MAX_VIRTIO_CONSOLES) {
2131 fprintf(stderr, "qemu: too many virtio consoles\n");
2132 exit(1);
2135 bus_opts = qemu_opts_create(device, NULL, 0, NULL);
2136 if (arch_type == QEMU_ARCH_S390X) {
2137 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2138 } else {
2139 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2142 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2143 qemu_opt_set(dev_opts, "driver", "virtconsole");
2145 snprintf(label, sizeof(label), "virtcon%d", index);
2146 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2147 if (!virtcon_hds[index]) {
2148 fprintf(stderr, "qemu: could not connect virtio console"
2149 " to character backend '%s'\n", devname);
2150 return -1;
2152 qemu_opt_set(dev_opts, "chardev", label);
2154 index++;
2155 return 0;
2158 static int debugcon_parse(const char *devname)
2160 QemuOpts *opts;
2162 if (!qemu_chr_new("debugcon", devname, NULL)) {
2163 exit(1);
2165 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2166 if (!opts) {
2167 fprintf(stderr, "qemu: already have a debugcon device\n");
2168 exit(1);
2170 qemu_opt_set(opts, "driver", "isa-debugcon");
2171 qemu_opt_set(opts, "chardev", "debugcon");
2172 return 0;
2175 static QEMUMachine *machine_parse(const char *name)
2177 QEMUMachine *m, *machine = NULL;
2179 if (name) {
2180 machine = find_machine(name);
2182 if (machine) {
2183 return machine;
2185 printf("Supported machines are:\n");
2186 for (m = first_machine; m != NULL; m = m->next) {
2187 if (m->alias) {
2188 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2190 printf("%-20s %s%s\n", m->name, m->desc,
2191 m->is_default ? " (default)" : "");
2193 exit(!name || !is_help_option(name));
2196 static int tcg_init(void)
2198 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2199 return 0;
2202 static struct {
2203 const char *opt_name;
2204 const char *name;
2205 int (*available)(void);
2206 int (*init)(void);
2207 int *allowed;
2208 } accel_list[] = {
2209 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2210 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2211 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2212 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2215 static int configure_accelerator(void)
2217 const char *p = NULL;
2218 char buf[10];
2219 int i, ret;
2220 bool accel_initialised = 0;
2221 bool init_failed = 0;
2223 QemuOptsList *list = qemu_find_opts("machine");
2224 if (!QTAILQ_EMPTY(&list->head)) {
2225 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2228 if (p == NULL) {
2229 /* Use the default "accelerator", tcg */
2230 p = "tcg";
2233 while (!accel_initialised && *p != '\0') {
2234 if (*p == ':') {
2235 p++;
2237 p = get_opt_name(buf, sizeof (buf), p, ':');
2238 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2239 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2240 *(accel_list[i].allowed) = 1;
2241 ret = accel_list[i].init();
2242 if (ret < 0) {
2243 init_failed = 1;
2244 if (!accel_list[i].available()) {
2245 printf("%s not supported for this target\n",
2246 accel_list[i].name);
2247 } else {
2248 fprintf(stderr, "failed to initialize %s: %s\n",
2249 accel_list[i].name,
2250 strerror(-ret));
2252 *(accel_list[i].allowed) = 0;
2253 } else {
2254 accel_initialised = 1;
2256 break;
2259 if (i == ARRAY_SIZE(accel_list)) {
2260 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2264 if (!accel_initialised) {
2265 fprintf(stderr, "No accelerator found!\n");
2266 exit(1);
2269 if (init_failed) {
2270 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2273 return !accel_initialised;
2276 void qemu_add_exit_notifier(Notifier *notify)
2278 notifier_list_add(&exit_notifiers, notify);
2281 void qemu_remove_exit_notifier(Notifier *notify)
2283 notifier_remove(notify);
2286 static void qemu_run_exit_notifiers(void)
2288 notifier_list_notify(&exit_notifiers, NULL);
2291 void qemu_add_machine_init_done_notifier(Notifier *notify)
2293 notifier_list_add(&machine_init_done_notifiers, notify);
2296 static void qemu_run_machine_init_done_notifiers(void)
2298 notifier_list_notify(&machine_init_done_notifiers, NULL);
2301 static const QEMUOption *lookup_opt(int argc, char **argv,
2302 const char **poptarg, int *poptind)
2304 const QEMUOption *popt;
2305 int optind = *poptind;
2306 char *r = argv[optind];
2307 const char *optarg;
2309 loc_set_cmdline(argv, optind, 1);
2310 optind++;
2311 /* Treat --foo the same as -foo. */
2312 if (r[1] == '-')
2313 r++;
2314 popt = qemu_options;
2315 for(;;) {
2316 if (!popt->name) {
2317 error_report("invalid option");
2318 exit(1);
2320 if (!strcmp(popt->name, r + 1))
2321 break;
2322 popt++;
2324 if (popt->flags & HAS_ARG) {
2325 if (optind >= argc) {
2326 error_report("requires an argument");
2327 exit(1);
2329 optarg = argv[optind++];
2330 loc_set_cmdline(argv, optind - 2, 2);
2331 } else {
2332 optarg = NULL;
2335 *poptarg = optarg;
2336 *poptind = optind;
2338 return popt;
2341 static gpointer malloc_and_trace(gsize n_bytes)
2343 void *ptr = malloc(n_bytes);
2344 trace_g_malloc(n_bytes, ptr);
2345 return ptr;
2348 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2350 void *ptr = realloc(mem, n_bytes);
2351 trace_g_realloc(mem, n_bytes, ptr);
2352 return ptr;
2355 static void free_and_trace(gpointer mem)
2357 trace_g_free(mem);
2358 free(mem);
2361 int qemu_init_main_loop(void)
2363 return main_loop_init();
2366 int main(int argc, char **argv, char **envp)
2368 int i;
2369 int snapshot, linux_boot;
2370 const char *icount_option = NULL;
2371 const char *initrd_filename;
2372 const char *kernel_filename, *kernel_cmdline;
2373 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2374 DisplayState *ds;
2375 DisplayChangeListener *dcl;
2376 int cyls, heads, secs, translation;
2377 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2378 QemuOptsList *olist;
2379 int optind;
2380 const char *optarg;
2381 const char *loadvm = NULL;
2382 QEMUMachine *machine;
2383 const char *cpu_model;
2384 const char *vga_model = "none";
2385 const char *pid_file = NULL;
2386 const char *incoming = NULL;
2387 #ifdef CONFIG_VNC
2388 int show_vnc_port = 0;
2389 #endif
2390 bool defconfig = true;
2391 bool userconfig = true;
2392 const char *log_mask = NULL;
2393 const char *log_file = NULL;
2394 GMemVTable mem_trace = {
2395 .malloc = malloc_and_trace,
2396 .realloc = realloc_and_trace,
2397 .free = free_and_trace,
2399 const char *trace_events = NULL;
2400 const char *trace_file = NULL;
2402 atexit(qemu_run_exit_notifiers);
2403 error_set_progname(argv[0]);
2405 g_mem_set_vtable(&mem_trace);
2406 if (!g_thread_supported()) {
2407 #if !GLIB_CHECK_VERSION(2, 31, 0)
2408 g_thread_init(NULL);
2409 #else
2410 fprintf(stderr, "glib threading failed to initialize.\n");
2411 exit(1);
2412 #endif
2415 module_call_init(MODULE_INIT_QOM);
2417 runstate_init();
2419 init_clocks();
2420 rtc_clock = host_clock;
2422 qemu_cache_utils_init(envp);
2424 QLIST_INIT (&vm_change_state_head);
2425 os_setup_early_signal_handling();
2427 module_call_init(MODULE_INIT_MACHINE);
2428 machine = find_default_machine();
2429 cpu_model = NULL;
2430 ram_size = 0;
2431 snapshot = 0;
2432 cyls = heads = secs = 0;
2433 translation = BIOS_ATA_TRANSLATION_AUTO;
2435 for (i = 0; i < MAX_NODES; i++) {
2436 node_mem[i] = 0;
2437 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
2440 nb_numa_nodes = 0;
2441 nb_nics = 0;
2443 autostart= 1;
2445 /* first pass of option parsing */
2446 optind = 1;
2447 while (optind < argc) {
2448 if (argv[optind][0] != '-') {
2449 /* disk image */
2450 optind++;
2451 continue;
2452 } else {
2453 const QEMUOption *popt;
2455 popt = lookup_opt(argc, argv, &optarg, &optind);
2456 switch (popt->index) {
2457 case QEMU_OPTION_nodefconfig:
2458 defconfig = false;
2459 break;
2460 case QEMU_OPTION_nouserconfig:
2461 userconfig = false;
2462 break;
2467 if (defconfig) {
2468 int ret;
2469 ret = qemu_read_default_config_files(userconfig);
2470 if (ret < 0) {
2471 exit(1);
2475 /* second pass of option parsing */
2476 optind = 1;
2477 for(;;) {
2478 if (optind >= argc)
2479 break;
2480 if (argv[optind][0] != '-') {
2481 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2482 } else {
2483 const QEMUOption *popt;
2485 popt = lookup_opt(argc, argv, &optarg, &optind);
2486 if (!(popt->arch_mask & arch_type)) {
2487 printf("Option %s not supported for this target\n", popt->name);
2488 exit(1);
2490 switch(popt->index) {
2491 case QEMU_OPTION_M:
2492 machine = machine_parse(optarg);
2493 break;
2494 case QEMU_OPTION_cpu:
2495 /* hw initialization will check this */
2496 cpu_model = optarg;
2497 break;
2498 case QEMU_OPTION_hda:
2500 char buf[256];
2501 if (cyls == 0)
2502 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2503 else
2504 snprintf(buf, sizeof(buf),
2505 "%s,cyls=%d,heads=%d,secs=%d%s",
2506 HD_OPTS , cyls, heads, secs,
2507 translation == BIOS_ATA_TRANSLATION_LBA ?
2508 ",trans=lba" :
2509 translation == BIOS_ATA_TRANSLATION_NONE ?
2510 ",trans=none" : "");
2511 drive_add(IF_DEFAULT, 0, optarg, buf);
2512 break;
2514 case QEMU_OPTION_hdb:
2515 case QEMU_OPTION_hdc:
2516 case QEMU_OPTION_hdd:
2517 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2518 HD_OPTS);
2519 break;
2520 case QEMU_OPTION_drive:
2521 if (drive_def(optarg) == NULL) {
2522 exit(1);
2524 break;
2525 case QEMU_OPTION_set:
2526 if (qemu_set_option(optarg) != 0)
2527 exit(1);
2528 break;
2529 case QEMU_OPTION_global:
2530 if (qemu_global_option(optarg) != 0)
2531 exit(1);
2532 break;
2533 case QEMU_OPTION_mtdblock:
2534 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2535 break;
2536 case QEMU_OPTION_sd:
2537 drive_add(IF_SD, 0, optarg, SD_OPTS);
2538 break;
2539 case QEMU_OPTION_pflash:
2540 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2541 break;
2542 case QEMU_OPTION_snapshot:
2543 snapshot = 1;
2544 break;
2545 case QEMU_OPTION_hdachs:
2547 const char *p;
2548 p = optarg;
2549 cyls = strtol(p, (char **)&p, 0);
2550 if (cyls < 1 || cyls > 16383)
2551 goto chs_fail;
2552 if (*p != ',')
2553 goto chs_fail;
2554 p++;
2555 heads = strtol(p, (char **)&p, 0);
2556 if (heads < 1 || heads > 16)
2557 goto chs_fail;
2558 if (*p != ',')
2559 goto chs_fail;
2560 p++;
2561 secs = strtol(p, (char **)&p, 0);
2562 if (secs < 1 || secs > 63)
2563 goto chs_fail;
2564 if (*p == ',') {
2565 p++;
2566 if (!strcmp(p, "none"))
2567 translation = BIOS_ATA_TRANSLATION_NONE;
2568 else if (!strcmp(p, "lba"))
2569 translation = BIOS_ATA_TRANSLATION_LBA;
2570 else if (!strcmp(p, "auto"))
2571 translation = BIOS_ATA_TRANSLATION_AUTO;
2572 else
2573 goto chs_fail;
2574 } else if (*p != '\0') {
2575 chs_fail:
2576 fprintf(stderr, "qemu: invalid physical CHS format\n");
2577 exit(1);
2579 if (hda_opts != NULL) {
2580 char num[16];
2581 snprintf(num, sizeof(num), "%d", cyls);
2582 qemu_opt_set(hda_opts, "cyls", num);
2583 snprintf(num, sizeof(num), "%d", heads);
2584 qemu_opt_set(hda_opts, "heads", num);
2585 snprintf(num, sizeof(num), "%d", secs);
2586 qemu_opt_set(hda_opts, "secs", num);
2587 if (translation == BIOS_ATA_TRANSLATION_LBA)
2588 qemu_opt_set(hda_opts, "trans", "lba");
2589 if (translation == BIOS_ATA_TRANSLATION_NONE)
2590 qemu_opt_set(hda_opts, "trans", "none");
2593 break;
2594 case QEMU_OPTION_numa:
2595 if (nb_numa_nodes >= MAX_NODES) {
2596 fprintf(stderr, "qemu: too many NUMA nodes\n");
2597 exit(1);
2599 numa_add(optarg);
2600 break;
2601 case QEMU_OPTION_display:
2602 display_type = select_display(optarg);
2603 break;
2604 case QEMU_OPTION_nographic:
2605 display_type = DT_NOGRAPHIC;
2606 break;
2607 case QEMU_OPTION_curses:
2608 #ifdef CONFIG_CURSES
2609 display_type = DT_CURSES;
2610 #else
2611 fprintf(stderr, "Curses support is disabled\n");
2612 exit(1);
2613 #endif
2614 break;
2615 case QEMU_OPTION_portrait:
2616 graphic_rotate = 90;
2617 break;
2618 case QEMU_OPTION_rotate:
2619 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2620 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2621 graphic_rotate != 180 && graphic_rotate != 270) {
2622 fprintf(stderr,
2623 "qemu: only 90, 180, 270 deg rotation is available\n");
2624 exit(1);
2626 break;
2627 case QEMU_OPTION_kernel:
2628 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2629 break;
2630 case QEMU_OPTION_initrd:
2631 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
2632 break;
2633 case QEMU_OPTION_append:
2634 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
2635 break;
2636 case QEMU_OPTION_dtb:
2637 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2638 break;
2639 case QEMU_OPTION_cdrom:
2640 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2641 break;
2642 case QEMU_OPTION_boot:
2644 static const char * const params[] = {
2645 "order", "once", "menu",
2646 "splash", "splash-time",
2647 "reboot-timeout", NULL
2649 char buf[sizeof(boot_devices)];
2650 char *standard_boot_devices;
2651 int legacy = 0;
2653 if (!strchr(optarg, '=')) {
2654 legacy = 1;
2655 pstrcpy(buf, sizeof(buf), optarg);
2656 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2657 fprintf(stderr,
2658 "qemu: unknown boot parameter '%s' in '%s'\n",
2659 buf, optarg);
2660 exit(1);
2663 if (legacy ||
2664 get_param_value(buf, sizeof(buf), "order", optarg)) {
2665 validate_bootdevices(buf);
2666 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2668 if (!legacy) {
2669 if (get_param_value(buf, sizeof(buf),
2670 "once", optarg)) {
2671 validate_bootdevices(buf);
2672 standard_boot_devices = g_strdup(boot_devices);
2673 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2674 qemu_register_reset(restore_boot_devices,
2675 standard_boot_devices);
2677 if (get_param_value(buf, sizeof(buf),
2678 "menu", optarg)) {
2679 if (!strcmp(buf, "on")) {
2680 boot_menu = 1;
2681 } else if (!strcmp(buf, "off")) {
2682 boot_menu = 0;
2683 } else {
2684 fprintf(stderr,
2685 "qemu: invalid option value '%s'\n",
2686 buf);
2687 exit(1);
2690 qemu_opts_parse(qemu_find_opts("boot-opts"),
2691 optarg, 0);
2694 break;
2695 case QEMU_OPTION_fda:
2696 case QEMU_OPTION_fdb:
2697 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2698 optarg, FD_OPTS);
2699 break;
2700 case QEMU_OPTION_no_fd_bootchk:
2701 fd_bootchk = 0;
2702 break;
2703 case QEMU_OPTION_netdev:
2704 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2705 exit(1);
2707 break;
2708 case QEMU_OPTION_net:
2709 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2710 exit(1);
2712 break;
2713 #ifdef CONFIG_LIBISCSI
2714 case QEMU_OPTION_iscsi:
2715 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2716 if (!opts) {
2717 exit(1);
2719 break;
2720 #endif
2721 #ifdef CONFIG_SLIRP
2722 case QEMU_OPTION_tftp:
2723 legacy_tftp_prefix = optarg;
2724 break;
2725 case QEMU_OPTION_bootp:
2726 legacy_bootp_filename = optarg;
2727 break;
2728 case QEMU_OPTION_redir:
2729 if (net_slirp_redir(optarg) < 0)
2730 exit(1);
2731 break;
2732 #endif
2733 case QEMU_OPTION_bt:
2734 add_device_config(DEV_BT, optarg);
2735 break;
2736 case QEMU_OPTION_audio_help:
2737 if (!(audio_available())) {
2738 printf("Option %s not supported for this target\n", popt->name);
2739 exit(1);
2741 AUD_help ();
2742 exit (0);
2743 break;
2744 case QEMU_OPTION_soundhw:
2745 if (!(audio_available())) {
2746 printf("Option %s not supported for this target\n", popt->name);
2747 exit(1);
2749 select_soundhw (optarg);
2750 break;
2751 case QEMU_OPTION_h:
2752 help(0);
2753 break;
2754 case QEMU_OPTION_version:
2755 version();
2756 exit(0);
2757 break;
2758 case QEMU_OPTION_m: {
2759 int64_t value;
2760 uint64_t sz;
2761 char *end;
2763 value = strtosz(optarg, &end);
2764 if (value < 0 || *end) {
2765 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2766 exit(1);
2768 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
2769 ram_size = sz;
2770 if (ram_size != sz) {
2771 fprintf(stderr, "qemu: ram size too large\n");
2772 exit(1);
2774 break;
2776 case QEMU_OPTION_mempath:
2777 mem_path = optarg;
2778 break;
2779 #ifdef MAP_POPULATE
2780 case QEMU_OPTION_mem_prealloc:
2781 mem_prealloc = 1;
2782 break;
2783 #endif
2784 case QEMU_OPTION_d:
2785 log_mask = optarg;
2786 break;
2787 case QEMU_OPTION_D:
2788 log_file = optarg;
2789 break;
2790 case QEMU_OPTION_s:
2791 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2792 break;
2793 case QEMU_OPTION_gdb:
2794 add_device_config(DEV_GDB, optarg);
2795 break;
2796 case QEMU_OPTION_L:
2797 data_dir = optarg;
2798 break;
2799 case QEMU_OPTION_bios:
2800 bios_name = optarg;
2801 break;
2802 case QEMU_OPTION_singlestep:
2803 singlestep = 1;
2804 break;
2805 case QEMU_OPTION_S:
2806 autostart = 0;
2807 break;
2808 case QEMU_OPTION_k:
2809 keyboard_layout = optarg;
2810 break;
2811 case QEMU_OPTION_localtime:
2812 rtc_utc = 0;
2813 break;
2814 case QEMU_OPTION_vga:
2815 vga_model = optarg;
2816 default_vga = 0;
2817 break;
2818 case QEMU_OPTION_g:
2820 const char *p;
2821 int w, h, depth;
2822 p = optarg;
2823 w = strtol(p, (char **)&p, 10);
2824 if (w <= 0) {
2825 graphic_error:
2826 fprintf(stderr, "qemu: invalid resolution or depth\n");
2827 exit(1);
2829 if (*p != 'x')
2830 goto graphic_error;
2831 p++;
2832 h = strtol(p, (char **)&p, 10);
2833 if (h <= 0)
2834 goto graphic_error;
2835 if (*p == 'x') {
2836 p++;
2837 depth = strtol(p, (char **)&p, 10);
2838 if (depth != 8 && depth != 15 && depth != 16 &&
2839 depth != 24 && depth != 32)
2840 goto graphic_error;
2841 } else if (*p == '\0') {
2842 depth = graphic_depth;
2843 } else {
2844 goto graphic_error;
2847 graphic_width = w;
2848 graphic_height = h;
2849 graphic_depth = depth;
2851 break;
2852 case QEMU_OPTION_echr:
2854 char *r;
2855 term_escape_char = strtol(optarg, &r, 0);
2856 if (r == optarg)
2857 printf("Bad argument to echr\n");
2858 break;
2860 case QEMU_OPTION_monitor:
2861 monitor_parse(optarg, "readline");
2862 default_monitor = 0;
2863 break;
2864 case QEMU_OPTION_qmp:
2865 monitor_parse(optarg, "control");
2866 default_monitor = 0;
2867 break;
2868 case QEMU_OPTION_mon:
2869 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2870 if (!opts) {
2871 exit(1);
2873 default_monitor = 0;
2874 break;
2875 case QEMU_OPTION_chardev:
2876 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2877 if (!opts) {
2878 exit(1);
2880 break;
2881 case QEMU_OPTION_fsdev:
2882 olist = qemu_find_opts("fsdev");
2883 if (!olist) {
2884 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2885 exit(1);
2887 opts = qemu_opts_parse(olist, optarg, 1);
2888 if (!opts) {
2889 fprintf(stderr, "parse error: %s\n", optarg);
2890 exit(1);
2892 break;
2893 case QEMU_OPTION_virtfs: {
2894 QemuOpts *fsdev;
2895 QemuOpts *device;
2896 const char *writeout, *sock_fd, *socket;
2898 olist = qemu_find_opts("virtfs");
2899 if (!olist) {
2900 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2901 exit(1);
2903 opts = qemu_opts_parse(olist, optarg, 1);
2904 if (!opts) {
2905 fprintf(stderr, "parse error: %s\n", optarg);
2906 exit(1);
2909 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2910 qemu_opt_get(opts, "mount_tag") == NULL) {
2911 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2912 exit(1);
2914 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2915 qemu_opt_get(opts, "mount_tag"),
2916 1, NULL);
2917 if (!fsdev) {
2918 fprintf(stderr, "duplicate fsdev id: %s\n",
2919 qemu_opt_get(opts, "mount_tag"));
2920 exit(1);
2923 writeout = qemu_opt_get(opts, "writeout");
2924 if (writeout) {
2925 #ifdef CONFIG_SYNC_FILE_RANGE
2926 qemu_opt_set(fsdev, "writeout", writeout);
2927 #else
2928 fprintf(stderr, "writeout=immediate not supported on "
2929 "this platform\n");
2930 exit(1);
2931 #endif
2933 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2934 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2935 qemu_opt_set(fsdev, "security_model",
2936 qemu_opt_get(opts, "security_model"));
2937 socket = qemu_opt_get(opts, "socket");
2938 if (socket) {
2939 qemu_opt_set(fsdev, "socket", socket);
2941 sock_fd = qemu_opt_get(opts, "sock_fd");
2942 if (sock_fd) {
2943 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2946 qemu_opt_set_bool(fsdev, "readonly",
2947 qemu_opt_get_bool(opts, "readonly", 0));
2948 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2949 NULL);
2950 qemu_opt_set(device, "driver", "virtio-9p-pci");
2951 qemu_opt_set(device, "fsdev",
2952 qemu_opt_get(opts, "mount_tag"));
2953 qemu_opt_set(device, "mount_tag",
2954 qemu_opt_get(opts, "mount_tag"));
2955 break;
2957 case QEMU_OPTION_virtfs_synth: {
2958 QemuOpts *fsdev;
2959 QemuOpts *device;
2961 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
2962 1, NULL);
2963 if (!fsdev) {
2964 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2965 exit(1);
2967 qemu_opt_set(fsdev, "fsdriver", "synth");
2969 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2970 NULL);
2971 qemu_opt_set(device, "driver", "virtio-9p-pci");
2972 qemu_opt_set(device, "fsdev", "v_synth");
2973 qemu_opt_set(device, "mount_tag", "v_synth");
2974 break;
2976 case QEMU_OPTION_serial:
2977 add_device_config(DEV_SERIAL, optarg);
2978 default_serial = 0;
2979 if (strncmp(optarg, "mon:", 4) == 0) {
2980 default_monitor = 0;
2982 break;
2983 case QEMU_OPTION_watchdog:
2984 if (watchdog) {
2985 fprintf(stderr,
2986 "qemu: only one watchdog option may be given\n");
2987 return 1;
2989 watchdog = optarg;
2990 break;
2991 case QEMU_OPTION_watchdog_action:
2992 if (select_watchdog_action(optarg) == -1) {
2993 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2994 exit(1);
2996 break;
2997 case QEMU_OPTION_virtiocon:
2998 add_device_config(DEV_VIRTCON, optarg);
2999 default_virtcon = 0;
3000 if (strncmp(optarg, "mon:", 4) == 0) {
3001 default_monitor = 0;
3003 break;
3004 case QEMU_OPTION_parallel:
3005 add_device_config(DEV_PARALLEL, optarg);
3006 default_parallel = 0;
3007 if (strncmp(optarg, "mon:", 4) == 0) {
3008 default_monitor = 0;
3010 break;
3011 case QEMU_OPTION_debugcon:
3012 add_device_config(DEV_DEBUGCON, optarg);
3013 break;
3014 case QEMU_OPTION_loadvm:
3015 loadvm = optarg;
3016 break;
3017 case QEMU_OPTION_full_screen:
3018 full_screen = 1;
3019 break;
3020 #ifdef CONFIG_SDL
3021 case QEMU_OPTION_no_frame:
3022 no_frame = 1;
3023 break;
3024 case QEMU_OPTION_alt_grab:
3025 alt_grab = 1;
3026 break;
3027 case QEMU_OPTION_ctrl_grab:
3028 ctrl_grab = 1;
3029 break;
3030 case QEMU_OPTION_no_quit:
3031 no_quit = 1;
3032 break;
3033 case QEMU_OPTION_sdl:
3034 display_type = DT_SDL;
3035 break;
3036 #else
3037 case QEMU_OPTION_no_frame:
3038 case QEMU_OPTION_alt_grab:
3039 case QEMU_OPTION_ctrl_grab:
3040 case QEMU_OPTION_no_quit:
3041 case QEMU_OPTION_sdl:
3042 fprintf(stderr, "SDL support is disabled\n");
3043 exit(1);
3044 #endif
3045 case QEMU_OPTION_pidfile:
3046 pid_file = optarg;
3047 break;
3048 case QEMU_OPTION_win2k_hack:
3049 win2k_install_hack = 1;
3050 break;
3051 case QEMU_OPTION_rtc_td_hack: {
3052 static GlobalProperty slew_lost_ticks[] = {
3054 .driver = "mc146818rtc",
3055 .property = "lost_tick_policy",
3056 .value = "slew",
3058 { /* end of list */ }
3061 qdev_prop_register_global_list(slew_lost_ticks);
3062 break;
3064 case QEMU_OPTION_acpitable:
3065 do_acpitable_option(optarg);
3066 break;
3067 case QEMU_OPTION_smbios:
3068 do_smbios_option(optarg);
3069 break;
3070 case QEMU_OPTION_enable_kvm:
3071 olist = qemu_find_opts("machine");
3072 qemu_opts_parse(olist, "accel=kvm", 0);
3073 break;
3074 case QEMU_OPTION_machine:
3075 olist = qemu_find_opts("machine");
3076 opts = qemu_opts_parse(olist, optarg, 1);
3077 if (!opts) {
3078 fprintf(stderr, "parse error: %s\n", optarg);
3079 exit(1);
3081 optarg = qemu_opt_get(opts, "type");
3082 if (optarg) {
3083 machine = machine_parse(optarg);
3085 break;
3086 case QEMU_OPTION_usb:
3087 usb_enabled = 1;
3088 break;
3089 case QEMU_OPTION_usbdevice:
3090 usb_enabled = 1;
3091 add_device_config(DEV_USB, optarg);
3092 break;
3093 case QEMU_OPTION_device:
3094 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3095 exit(1);
3097 break;
3098 case QEMU_OPTION_smp:
3099 smp_parse(optarg);
3100 if (smp_cpus < 1) {
3101 fprintf(stderr, "Invalid number of CPUs\n");
3102 exit(1);
3104 if (max_cpus < smp_cpus) {
3105 fprintf(stderr, "maxcpus must be equal to or greater than "
3106 "smp\n");
3107 exit(1);
3109 if (max_cpus > 255) {
3110 fprintf(stderr, "Unsupported number of maxcpus\n");
3111 exit(1);
3113 break;
3114 case QEMU_OPTION_vnc:
3115 #ifdef CONFIG_VNC
3116 display_remote++;
3117 vnc_display = optarg;
3118 #else
3119 fprintf(stderr, "VNC support is disabled\n");
3120 exit(1);
3121 #endif
3122 break;
3123 case QEMU_OPTION_no_acpi:
3124 acpi_enabled = 0;
3125 break;
3126 case QEMU_OPTION_no_hpet:
3127 no_hpet = 1;
3128 break;
3129 case QEMU_OPTION_balloon:
3130 if (balloon_parse(optarg) < 0) {
3131 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3132 exit(1);
3134 break;
3135 case QEMU_OPTION_no_reboot:
3136 no_reboot = 1;
3137 break;
3138 case QEMU_OPTION_no_shutdown:
3139 no_shutdown = 1;
3140 break;
3141 case QEMU_OPTION_show_cursor:
3142 cursor_hide = 0;
3143 break;
3144 case QEMU_OPTION_uuid:
3145 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3146 fprintf(stderr, "Fail to parse UUID string."
3147 " Wrong format.\n");
3148 exit(1);
3150 break;
3151 case QEMU_OPTION_option_rom:
3152 if (nb_option_roms >= MAX_OPTION_ROMS) {
3153 fprintf(stderr, "Too many option ROMs\n");
3154 exit(1);
3156 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3157 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3158 option_rom[nb_option_roms].bootindex =
3159 qemu_opt_get_number(opts, "bootindex", -1);
3160 if (!option_rom[nb_option_roms].name) {
3161 fprintf(stderr, "Option ROM file is not specified\n");
3162 exit(1);
3164 nb_option_roms++;
3165 break;
3166 case QEMU_OPTION_semihosting:
3167 semihosting_enabled = 1;
3168 break;
3169 case QEMU_OPTION_name:
3170 qemu_name = g_strdup(optarg);
3172 char *p = strchr(qemu_name, ',');
3173 if (p != NULL) {
3174 *p++ = 0;
3175 if (strncmp(p, "process=", 8)) {
3176 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3177 exit(1);
3179 p += 8;
3180 os_set_proc_name(p);
3183 break;
3184 case QEMU_OPTION_prom_env:
3185 if (nb_prom_envs >= MAX_PROM_ENVS) {
3186 fprintf(stderr, "Too many prom variables\n");
3187 exit(1);
3189 prom_envs[nb_prom_envs] = optarg;
3190 nb_prom_envs++;
3191 break;
3192 case QEMU_OPTION_old_param:
3193 old_param = 1;
3194 break;
3195 case QEMU_OPTION_clock:
3196 configure_alarms(optarg);
3197 break;
3198 case QEMU_OPTION_startdate:
3199 configure_rtc_date_offset(optarg, 1);
3200 break;
3201 case QEMU_OPTION_rtc:
3202 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3203 if (!opts) {
3204 exit(1);
3206 configure_rtc(opts);
3207 break;
3208 case QEMU_OPTION_tb_size:
3209 tcg_tb_size = strtol(optarg, NULL, 0);
3210 if (tcg_tb_size < 0) {
3211 tcg_tb_size = 0;
3213 break;
3214 case QEMU_OPTION_icount:
3215 icount_option = optarg;
3216 break;
3217 case QEMU_OPTION_incoming:
3218 incoming = optarg;
3219 runstate_set(RUN_STATE_INMIGRATE);
3220 break;
3221 case QEMU_OPTION_nodefaults:
3222 default_serial = 0;
3223 default_parallel = 0;
3224 default_virtcon = 0;
3225 default_monitor = 0;
3226 default_net = 0;
3227 default_floppy = 0;
3228 default_cdrom = 0;
3229 default_sdcard = 0;
3230 default_vga = 0;
3231 break;
3232 case QEMU_OPTION_xen_domid:
3233 if (!(xen_available())) {
3234 printf("Option %s not supported for this target\n", popt->name);
3235 exit(1);
3237 xen_domid = atoi(optarg);
3238 break;
3239 case QEMU_OPTION_xen_create:
3240 if (!(xen_available())) {
3241 printf("Option %s not supported for this target\n", popt->name);
3242 exit(1);
3244 xen_mode = XEN_CREATE;
3245 break;
3246 case QEMU_OPTION_xen_attach:
3247 if (!(xen_available())) {
3248 printf("Option %s not supported for this target\n", popt->name);
3249 exit(1);
3251 xen_mode = XEN_ATTACH;
3252 break;
3253 case QEMU_OPTION_trace:
3255 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3256 if (!opts) {
3257 exit(1);
3259 trace_events = qemu_opt_get(opts, "events");
3260 trace_file = qemu_opt_get(opts, "file");
3261 break;
3263 case QEMU_OPTION_readconfig:
3265 int ret = qemu_read_config_file(optarg);
3266 if (ret < 0) {
3267 fprintf(stderr, "read config %s: %s\n", optarg,
3268 strerror(-ret));
3269 exit(1);
3271 break;
3273 case QEMU_OPTION_spice:
3274 olist = qemu_find_opts("spice");
3275 if (!olist) {
3276 fprintf(stderr, "spice is not supported by this qemu build.\n");
3277 exit(1);
3279 opts = qemu_opts_parse(olist, optarg, 0);
3280 if (!opts) {
3281 fprintf(stderr, "parse error: %s\n", optarg);
3282 exit(1);
3284 break;
3285 case QEMU_OPTION_writeconfig:
3287 FILE *fp;
3288 if (strcmp(optarg, "-") == 0) {
3289 fp = stdout;
3290 } else {
3291 fp = fopen(optarg, "w");
3292 if (fp == NULL) {
3293 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3294 exit(1);
3297 qemu_config_write(fp);
3298 fclose(fp);
3299 break;
3301 case QEMU_OPTION_qtest:
3302 qtest_chrdev = optarg;
3303 break;
3304 case QEMU_OPTION_qtest_log:
3305 qtest_log = optarg;
3306 break;
3307 case QEMU_OPTION_sandbox:
3308 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3309 if (!opts) {
3310 exit(0);
3312 break;
3313 default:
3314 os_parse_cmd_args(popt->index, optarg);
3318 loc_set_none();
3320 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3321 exit(1);
3324 if (machine == NULL) {
3325 fprintf(stderr, "No machine found.\n");
3326 exit(1);
3329 if (machine->hw_version) {
3330 qemu_set_version(machine->hw_version);
3333 /* Init CPU def lists, based on config
3334 * - Must be called after all the qemu_read_config_file() calls
3335 * - Must be called before list_cpus()
3336 * - Must be called before machine->init()
3338 cpudef_init();
3340 if (cpu_model && is_help_option(cpu_model)) {
3341 list_cpus(stdout, &fprintf, cpu_model);
3342 exit(0);
3345 /* Open the logfile at this point, if necessary. We can't open the logfile
3346 * when encountering either of the logging options (-d or -D) because the
3347 * other one may be encountered later on the command line, changing the
3348 * location or level of logging.
3350 if (log_mask) {
3351 if (log_file) {
3352 set_cpu_log_filename(log_file);
3354 set_cpu_log(log_mask);
3357 if (!trace_backend_init(trace_events, trace_file)) {
3358 exit(1);
3361 /* If no data_dir is specified then try to find it relative to the
3362 executable path. */
3363 if (!data_dir) {
3364 data_dir = os_find_datadir(argv[0]);
3366 /* If all else fails use the install path specified when building. */
3367 if (!data_dir) {
3368 data_dir = CONFIG_QEMU_DATADIR;
3372 * Default to max_cpus = smp_cpus, in case the user doesn't
3373 * specify a max_cpus value.
3375 if (!max_cpus)
3376 max_cpus = smp_cpus;
3378 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3379 if (smp_cpus > machine->max_cpus) {
3380 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3381 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3382 machine->max_cpus);
3383 exit(1);
3387 * Get the default machine options from the machine if it is not already
3388 * specified either by the configuration file or by the command line.
3390 if (machine->default_machine_opts) {
3391 qemu_opts_set_defaults(qemu_find_opts("machine"),
3392 machine->default_machine_opts, 0);
3395 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3396 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3398 if (machine->no_serial) {
3399 default_serial = 0;
3401 if (machine->no_parallel) {
3402 default_parallel = 0;
3404 if (!machine->use_virtcon) {
3405 default_virtcon = 0;
3407 if (machine->no_floppy) {
3408 default_floppy = 0;
3410 if (machine->no_cdrom) {
3411 default_cdrom = 0;
3413 if (machine->no_sdcard) {
3414 default_sdcard = 0;
3417 if (display_type == DT_NOGRAPHIC) {
3418 if (default_parallel)
3419 add_device_config(DEV_PARALLEL, "null");
3420 if (default_serial && default_monitor) {
3421 add_device_config(DEV_SERIAL, "mon:stdio");
3422 } else if (default_virtcon && default_monitor) {
3423 add_device_config(DEV_VIRTCON, "mon:stdio");
3424 } else {
3425 if (default_serial)
3426 add_device_config(DEV_SERIAL, "stdio");
3427 if (default_virtcon)
3428 add_device_config(DEV_VIRTCON, "stdio");
3429 if (default_monitor)
3430 monitor_parse("stdio", "readline");
3432 } else {
3433 if (default_serial)
3434 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3435 if (default_parallel)
3436 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3437 if (default_monitor)
3438 monitor_parse("vc:80Cx24C", "readline");
3439 if (default_virtcon)
3440 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3443 socket_init();
3445 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3446 exit(1);
3447 #ifdef CONFIG_VIRTFS
3448 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3449 exit(1);
3451 #endif
3453 os_daemonize();
3455 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3456 os_pidfile_error();
3457 exit(1);
3460 /* init the memory */
3461 if (ram_size == 0) {
3462 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3465 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
3466 != 0) {
3467 exit(0);
3470 configure_accelerator();
3472 qemu_init_cpu_loop();
3473 if (qemu_init_main_loop()) {
3474 fprintf(stderr, "qemu_init_main_loop failed\n");
3475 exit(1);
3478 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3479 if (machine_opts) {
3480 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3481 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3482 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3483 } else {
3484 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3487 if (!kernel_cmdline) {
3488 kernel_cmdline = "";
3491 linux_boot = (kernel_filename != NULL);
3493 if (!linux_boot && *kernel_cmdline != '\0') {
3494 fprintf(stderr, "-append only allowed with -kernel option\n");
3495 exit(1);
3498 if (!linux_boot && initrd_filename != NULL) {
3499 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3500 exit(1);
3503 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3504 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3505 exit(1);
3508 os_set_line_buffering();
3510 if (init_timer_alarm() < 0) {
3511 fprintf(stderr, "could not initialize alarm timer\n");
3512 exit(1);
3515 #ifdef CONFIG_SPICE
3516 /* spice needs the timers to be initialized by this point */
3517 qemu_spice_init();
3518 #endif
3520 if (icount_option && (kvm_enabled() || xen_enabled())) {
3521 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3522 exit(1);
3524 configure_icount(icount_option);
3526 if (net_init_clients() < 0) {
3527 exit(1);
3530 /* init the bluetooth world */
3531 if (foreach_device_config(DEV_BT, bt_parse))
3532 exit(1);
3534 if (!xen_enabled()) {
3535 /* On 32-bit hosts, QEMU is limited by virtual address space */
3536 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3537 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3538 exit(1);
3542 cpu_exec_init_all();
3544 bdrv_init_with_whitelist();
3546 blk_mig_init();
3548 /* open the virtual block devices */
3549 if (snapshot)
3550 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3551 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3552 exit(1);
3554 default_drive(default_cdrom, snapshot, machine->use_scsi,
3555 IF_DEFAULT, 2, CDROM_OPTS);
3556 default_drive(default_floppy, snapshot, machine->use_scsi,
3557 IF_FLOPPY, 0, FD_OPTS);
3558 default_drive(default_sdcard, snapshot, machine->use_scsi,
3559 IF_SD, 0, SD_OPTS);
3561 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
3563 if (nb_numa_nodes > 0) {
3564 int i;
3566 if (nb_numa_nodes > MAX_NODES) {
3567 nb_numa_nodes = MAX_NODES;
3570 /* If no memory size if given for any node, assume the default case
3571 * and distribute the available memory equally across all nodes
3573 for (i = 0; i < nb_numa_nodes; i++) {
3574 if (node_mem[i] != 0)
3575 break;
3577 if (i == nb_numa_nodes) {
3578 uint64_t usedmem = 0;
3580 /* On Linux, the each node's border has to be 8MB aligned,
3581 * the final node gets the rest.
3583 for (i = 0; i < nb_numa_nodes - 1; i++) {
3584 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3585 usedmem += node_mem[i];
3587 node_mem[i] = ram_size - usedmem;
3590 for (i = 0; i < nb_numa_nodes; i++) {
3591 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
3592 break;
3595 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3596 * must cope with this anyway, because there are BIOSes out there in
3597 * real machines which also use this scheme.
3599 if (i == nb_numa_nodes) {
3600 for (i = 0; i < max_cpus; i++) {
3601 set_bit(i, node_cpumask[i % nb_numa_nodes]);
3606 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3607 exit(1);
3610 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3611 exit(1);
3612 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3613 exit(1);
3614 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3615 exit(1);
3616 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3617 exit(1);
3619 /* If no default VGA is requested, the default is "none". */
3620 if (default_vga) {
3621 if (cirrus_vga_available()) {
3622 vga_model = "cirrus";
3623 } else if (vga_available()) {
3624 vga_model = "std";
3627 select_vgahw(vga_model);
3629 if (watchdog) {
3630 i = select_watchdog(watchdog);
3631 if (i > 0)
3632 exit (i == 1 ? 1 : 0);
3635 if (machine->compat_props) {
3636 qdev_prop_register_global_list(machine->compat_props);
3638 qemu_add_globals();
3640 qdev_machine_init();
3642 machine->init(ram_size, boot_devices,
3643 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3645 cpu_synchronize_all_post_init();
3647 set_numa_modes();
3649 current_machine = machine;
3651 /* init USB devices */
3652 if (usb_enabled) {
3653 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3654 exit(1);
3657 /* init generic devices */
3658 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3659 exit(1);
3661 net_check_clients();
3663 /* just use the first displaystate for the moment */
3664 ds = get_displaystate();
3666 if (using_spice)
3667 display_remote++;
3668 if (display_type == DT_DEFAULT && !display_remote) {
3669 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3670 display_type = DT_SDL;
3671 #elif defined(CONFIG_VNC)
3672 vnc_display = "localhost:0,to=99";
3673 show_vnc_port = 1;
3674 #else
3675 display_type = DT_NONE;
3676 #endif
3680 /* init local displays */
3681 switch (display_type) {
3682 case DT_NOGRAPHIC:
3683 break;
3684 #if defined(CONFIG_CURSES)
3685 case DT_CURSES:
3686 if (!is_daemonized()) {
3687 curses_display_init(ds, full_screen);
3689 break;
3690 #endif
3691 #if defined(CONFIG_SDL)
3692 case DT_SDL:
3693 sdl_display_init(ds, full_screen, no_frame);
3694 break;
3695 #elif defined(CONFIG_COCOA)
3696 case DT_SDL:
3697 cocoa_display_init(ds, full_screen);
3698 break;
3699 #endif
3700 default:
3701 break;
3704 /* must be after terminal init, SDL library changes signal handlers */
3705 os_setup_signal_handling();
3707 #ifdef CONFIG_VNC
3708 /* init remote displays */
3709 if (vnc_display) {
3710 vnc_display_init(ds);
3711 if (vnc_display_open(ds, vnc_display) < 0) {
3712 fprintf(stderr, "Failed to start VNC server on `%s'\n",
3713 vnc_display);
3714 exit(1);
3717 if (show_vnc_port) {
3718 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3721 #endif
3722 #ifdef CONFIG_SPICE
3723 if (using_spice && !qxl_enabled) {
3724 qemu_spice_display_init(ds);
3726 #endif
3728 /* display setup */
3729 dpy_resize(ds);
3730 dcl = ds->listeners;
3731 while (dcl != NULL) {
3732 if (dcl->dpy_refresh != NULL) {
3733 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3734 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3735 break;
3737 dcl = dcl->next;
3739 text_consoles_set_display(ds);
3741 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3742 exit(1);
3745 qdev_machine_creation_done();
3747 if (rom_load_all() != 0) {
3748 fprintf(stderr, "rom loading failed\n");
3749 exit(1);
3752 /* TODO: once all bus devices are qdevified, this should be done
3753 * when bus is created by qdev.c */
3754 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3755 qemu_run_machine_init_done_notifiers();
3757 qemu_system_reset(VMRESET_SILENT);
3758 if (loadvm) {
3759 if (load_vmstate(loadvm) < 0) {
3760 autostart = 0;
3764 if (incoming) {
3765 Error *errp = NULL;
3766 int ret = qemu_start_incoming_migration(incoming, &errp);
3767 if (ret < 0) {
3768 if (error_is_set(&errp)) {
3769 fprintf(stderr, "Migrate: %s\n", error_get_pretty(errp));
3770 error_free(errp);
3772 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3773 incoming, ret);
3774 exit(ret);
3776 } else if (autostart) {
3777 vm_start();
3780 os_setup_post();
3782 resume_all_vcpus();
3783 main_loop();
3784 bdrv_close_all();
3785 pause_all_vcpus();
3786 net_cleanup();
3787 res_free();
3789 return 0;