qemu-kvm: Fix GSI handling with in-kernel irqchip
[qemu-kvm.git] / vl.c
blob1048b22891ae3b4febce67bbaed344428e8129ca
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
51 #ifdef CONFIG_BSD
52 #include <sys/stat.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <libutil.h>
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <pty.h>
62 #include <malloc.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
66 #endif
67 #ifdef __sun__
68 #include <sys/stat.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
77 #include <net/if.h>
78 #include <syslog.h>
79 #include <stropts.h>
80 #endif
81 #endif
82 #endif
84 #if defined(__OpenBSD__)
85 #include <util.h>
86 #endif
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
90 #endif
92 #ifdef _WIN32
93 #include <windows.h>
94 #endif
96 #ifdef CONFIG_SDL
97 #if defined(__APPLE__) || defined(main)
98 #include <SDL.h>
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
104 #undef main
105 #define main qemu_main
106 #endif
107 #endif /* CONFIG_SDL */
109 #ifdef CONFIG_COCOA
110 #undef main
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
114 #include <glib.h>
116 #include "hw/hw.h"
117 #include "hw/boards.h"
118 #include "hw/usb.h"
119 #include "hw/pcmcia.h"
120 #include "hw/pc.h"
121 #include "hw/isa.h"
122 #include "hw/baum.h"
123 #include "hw/bt.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
126 #include "hw/xen.h"
127 #include "hw/qdev.h"
128 #include "hw/loader.h"
129 #include "bt-host.h"
130 #include "net.h"
131 #include "net/slirp.h"
132 #include "monitor.h"
133 #include "console.h"
134 #include "sysemu.h"
135 #include "gdbstub.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
139 #include "block.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
142 #include "dma.h"
143 #include "audio/audio.h"
144 #include "migration.h"
145 #include "kvm.h"
146 #include "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
156 #include "disas.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
162 #include "trace.h"
163 #include "trace/control.h"
164 #include "qemu-queue.h"
165 #include "cpus.h"
166 #include "arch_init.h"
168 #include "ui/qemu-spice.h"
170 //#define DEBUG_NET
171 //#define DEBUG_SLIRP
173 #define DEFAULT_RAM_SIZE 128
175 #define MAX_VIRTIO_CONSOLES 1
177 static const char *data_dir;
178 const char *bios_name = NULL;
179 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
180 DisplayType display_type = DT_DEFAULT;
181 int display_remote = 0;
182 const char* keyboard_layout = NULL;
183 ram_addr_t ram_size;
184 const char *mem_path = NULL;
185 #ifdef MAP_POPULATE
186 int mem_prealloc = 0; /* force preallocation of physical target memory */
187 #endif
188 int nb_nics;
189 NICInfo nd_table[MAX_NICS];
190 int autostart;
191 static int rtc_utc = 1;
192 static int rtc_date_offset = -1; /* -1 means no change */
193 QEMUClock *rtc_clock;
194 int vga_interface_type = VGA_NONE;
195 static int full_screen = 0;
196 #ifdef CONFIG_SDL
197 static int no_frame = 0;
198 #endif
199 int no_quit = 0;
200 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
201 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
202 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
203 int win2k_install_hack = 0;
204 int rtc_td_hack = 0;
205 int usb_enabled = 0;
206 int singlestep = 0;
207 int smp_cpus = 1;
208 int max_cpus = 0;
209 int smp_cores = 1;
210 int smp_threads = 1;
211 #ifdef CONFIG_VNC
212 const char *vnc_display;
213 #endif
214 int acpi_enabled = 1;
215 int no_hpet = 0;
216 int fd_bootchk = 1;
217 int no_reboot = 0;
218 int no_shutdown = 0;
219 int cursor_hide = 1;
220 int graphic_rotate = 0;
221 const char *watchdog;
222 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
223 int nb_option_roms;
224 int semihosting_enabled = 0;
225 unsigned int kvm_shadow_memory = 0;
226 int old_param = 0;
227 const char *qemu_name;
228 int alt_grab = 0;
229 int ctrl_grab = 0;
230 unsigned int nb_prom_envs = 0;
231 const char *prom_envs[MAX_PROM_ENVS];
232 int boot_menu;
233 uint8_t *boot_splash_filedata;
234 int boot_splash_filedata_size;
235 uint8_t qemu_extra_params_fw[2];
237 typedef struct FWBootEntry FWBootEntry;
239 struct FWBootEntry {
240 QTAILQ_ENTRY(FWBootEntry) link;
241 int32_t bootindex;
242 DeviceState *dev;
243 char *suffix;
246 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
248 int nb_numa_nodes;
249 uint64_t node_mem[MAX_NODES];
250 uint64_t node_cpumask[MAX_NODES];
252 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;
278 static struct {
279 const char *driver;
280 int *flag;
281 } default_list[] = {
282 { .driver = "isa-serial", .flag = &default_serial },
283 { .driver = "isa-parallel", .flag = &default_parallel },
284 { .driver = "isa-fdc", .flag = &default_floppy },
285 { .driver = "ide-cd", .flag = &default_cdrom },
286 { .driver = "ide-hd", .flag = &default_cdrom },
287 { .driver = "ide-drive", .flag = &default_cdrom },
288 { .driver = "scsi-cd", .flag = &default_cdrom },
289 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
290 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
291 { .driver = "virtio-serial", .flag = &default_virtcon },
294 static void res_free(void)
296 if (boot_splash_filedata != NULL) {
297 g_free(boot_splash_filedata);
298 boot_splash_filedata = NULL;
302 static int default_driver_check(QemuOpts *opts, void *opaque)
304 const char *driver = qemu_opt_get(opts, "driver");
305 int i;
307 if (!driver)
308 return 0;
309 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
310 if (strcmp(default_list[i].driver, driver) != 0)
311 continue;
312 *(default_list[i].flag) = 0;
314 return 0;
317 /***********************************************************/
318 /* QEMU state */
320 static RunState current_run_state = RUN_STATE_PRELAUNCH;
322 typedef struct {
323 RunState from;
324 RunState to;
325 } RunStateTransition;
327 static const RunStateTransition runstate_transitions_def[] = {
328 /* from -> to */
329 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
331 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
332 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
334 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
335 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
337 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
338 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
340 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
341 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
343 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
344 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
346 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
347 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
348 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
350 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
351 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
353 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
355 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
356 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
357 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
358 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
359 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
360 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
361 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
362 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
363 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
365 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
367 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
368 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
370 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
371 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
373 { RUN_STATE_MAX, RUN_STATE_MAX },
376 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
378 bool runstate_check(RunState state)
380 return current_run_state == state;
383 void runstate_init(void)
385 const RunStateTransition *p;
387 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
389 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
390 runstate_valid_transitions[p->from][p->to] = true;
394 /* This function will abort() on invalid state transitions */
395 void runstate_set(RunState new_state)
397 assert(new_state < RUN_STATE_MAX);
399 if (!runstate_valid_transitions[current_run_state][new_state]) {
400 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
401 RunState_lookup[current_run_state],
402 RunState_lookup[new_state]);
403 abort();
406 current_run_state = new_state;
409 int runstate_is_running(void)
411 return runstate_check(RUN_STATE_RUNNING);
414 StatusInfo *qmp_query_status(Error **errp)
416 StatusInfo *info = g_malloc0(sizeof(*info));
418 info->running = runstate_is_running();
419 info->singlestep = singlestep;
420 info->status = current_run_state;
422 return info;
425 /***********************************************************/
426 /* real time host monotonic timer */
428 /***********************************************************/
429 /* host time/date access */
430 void qemu_get_timedate(struct tm *tm, int offset)
432 time_t ti;
433 struct tm *ret;
435 time(&ti);
436 ti += offset;
437 if (rtc_date_offset == -1) {
438 if (rtc_utc)
439 ret = gmtime(&ti);
440 else
441 ret = localtime(&ti);
442 } else {
443 ti -= rtc_date_offset;
444 ret = gmtime(&ti);
447 memcpy(tm, ret, sizeof(struct tm));
450 int qemu_timedate_diff(struct tm *tm)
452 time_t seconds;
454 if (rtc_date_offset == -1)
455 if (rtc_utc)
456 seconds = mktimegm(tm);
457 else {
458 struct tm tmp = *tm;
459 tmp.tm_isdst = -1; /* use timezone to figure it out */
460 seconds = mktime(&tmp);
462 else
463 seconds = mktimegm(tm) + rtc_date_offset;
465 return seconds - time(NULL);
468 void rtc_change_mon_event(struct tm *tm)
470 QObject *data;
472 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
473 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
474 qobject_decref(data);
477 static void configure_rtc_date_offset(const char *startdate, int legacy)
479 time_t rtc_start_date;
480 struct tm tm;
482 if (!strcmp(startdate, "now") && legacy) {
483 rtc_date_offset = -1;
484 } else {
485 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
486 &tm.tm_year,
487 &tm.tm_mon,
488 &tm.tm_mday,
489 &tm.tm_hour,
490 &tm.tm_min,
491 &tm.tm_sec) == 6) {
492 /* OK */
493 } else if (sscanf(startdate, "%d-%d-%d",
494 &tm.tm_year,
495 &tm.tm_mon,
496 &tm.tm_mday) == 3) {
497 tm.tm_hour = 0;
498 tm.tm_min = 0;
499 tm.tm_sec = 0;
500 } else {
501 goto date_fail;
503 tm.tm_year -= 1900;
504 tm.tm_mon--;
505 rtc_start_date = mktimegm(&tm);
506 if (rtc_start_date == -1) {
507 date_fail:
508 fprintf(stderr, "Invalid date format. Valid formats are:\n"
509 "'2006-06-17T16:01:21' or '2006-06-17'\n");
510 exit(1);
512 rtc_date_offset = time(NULL) - rtc_start_date;
516 static void configure_rtc(QemuOpts *opts)
518 const char *value;
520 value = qemu_opt_get(opts, "base");
521 if (value) {
522 if (!strcmp(value, "utc")) {
523 rtc_utc = 1;
524 } else if (!strcmp(value, "localtime")) {
525 rtc_utc = 0;
526 } else {
527 configure_rtc_date_offset(value, 0);
530 value = qemu_opt_get(opts, "clock");
531 if (value) {
532 if (!strcmp(value, "host")) {
533 rtc_clock = host_clock;
534 } else if (!strcmp(value, "vm")) {
535 rtc_clock = vm_clock;
536 } else {
537 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
538 exit(1);
541 value = qemu_opt_get(opts, "driftfix");
542 if (value) {
543 if (!strcmp(value, "slew")) {
544 rtc_td_hack = 1;
545 } else if (!strcmp(value, "none")) {
546 rtc_td_hack = 0;
547 } else {
548 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
549 exit(1);
554 /***********************************************************/
555 /* Bluetooth support */
556 static int nb_hcis;
557 static int cur_hci;
558 static struct HCIInfo *hci_table[MAX_NICS];
560 static struct bt_vlan_s {
561 struct bt_scatternet_s net;
562 int id;
563 struct bt_vlan_s *next;
564 } *first_bt_vlan;
566 /* find or alloc a new bluetooth "VLAN" */
567 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
569 struct bt_vlan_s **pvlan, *vlan;
570 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
571 if (vlan->id == id)
572 return &vlan->net;
574 vlan = g_malloc0(sizeof(struct bt_vlan_s));
575 vlan->id = id;
576 pvlan = &first_bt_vlan;
577 while (*pvlan != NULL)
578 pvlan = &(*pvlan)->next;
579 *pvlan = vlan;
580 return &vlan->net;
583 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
587 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
589 return -ENOTSUP;
592 static struct HCIInfo null_hci = {
593 .cmd_send = null_hci_send,
594 .sco_send = null_hci_send,
595 .acl_send = null_hci_send,
596 .bdaddr_set = null_hci_addr_set,
599 struct HCIInfo *qemu_next_hci(void)
601 if (cur_hci == nb_hcis)
602 return &null_hci;
604 return hci_table[cur_hci++];
607 static struct HCIInfo *hci_init(const char *str)
609 char *endp;
610 struct bt_scatternet_s *vlan = 0;
612 if (!strcmp(str, "null"))
613 /* null */
614 return &null_hci;
615 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
616 /* host[:hciN] */
617 return bt_host_hci(str[4] ? str + 5 : "hci0");
618 else if (!strncmp(str, "hci", 3)) {
619 /* hci[,vlan=n] */
620 if (str[3]) {
621 if (!strncmp(str + 3, ",vlan=", 6)) {
622 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
623 if (*endp)
624 vlan = 0;
626 } else
627 vlan = qemu_find_bt_vlan(0);
628 if (vlan)
629 return bt_new_hci(vlan);
632 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
634 return 0;
637 static int bt_hci_parse(const char *str)
639 struct HCIInfo *hci;
640 bdaddr_t bdaddr;
642 if (nb_hcis >= MAX_NICS) {
643 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
644 return -1;
647 hci = hci_init(str);
648 if (!hci)
649 return -1;
651 bdaddr.b[0] = 0x52;
652 bdaddr.b[1] = 0x54;
653 bdaddr.b[2] = 0x00;
654 bdaddr.b[3] = 0x12;
655 bdaddr.b[4] = 0x34;
656 bdaddr.b[5] = 0x56 + nb_hcis;
657 hci->bdaddr_set(hci, bdaddr.b);
659 hci_table[nb_hcis++] = hci;
661 return 0;
664 static void bt_vhci_add(int vlan_id)
666 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
668 if (!vlan->slave)
669 fprintf(stderr, "qemu: warning: adding a VHCI to "
670 "an empty scatternet %i\n", vlan_id);
672 bt_vhci_init(bt_new_hci(vlan));
675 static struct bt_device_s *bt_device_add(const char *opt)
677 struct bt_scatternet_s *vlan;
678 int vlan_id = 0;
679 char *endp = strstr(opt, ",vlan=");
680 int len = (endp ? endp - opt : strlen(opt)) + 1;
681 char devname[10];
683 pstrcpy(devname, MIN(sizeof(devname), len), opt);
685 if (endp) {
686 vlan_id = strtol(endp + 6, &endp, 0);
687 if (*endp) {
688 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
689 return 0;
693 vlan = qemu_find_bt_vlan(vlan_id);
695 if (!vlan->slave)
696 fprintf(stderr, "qemu: warning: adding a slave device to "
697 "an empty scatternet %i\n", vlan_id);
699 if (!strcmp(devname, "keyboard"))
700 return bt_keyboard_init(vlan);
702 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
703 return 0;
706 static int bt_parse(const char *opt)
708 const char *endp, *p;
709 int vlan;
711 if (strstart(opt, "hci", &endp)) {
712 if (!*endp || *endp == ',') {
713 if (*endp)
714 if (!strstart(endp, ",vlan=", 0))
715 opt = endp + 1;
717 return bt_hci_parse(opt);
719 } else if (strstart(opt, "vhci", &endp)) {
720 if (!*endp || *endp == ',') {
721 if (*endp) {
722 if (strstart(endp, ",vlan=", &p)) {
723 vlan = strtol(p, (char **) &endp, 0);
724 if (*endp) {
725 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
726 return 1;
728 } else {
729 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
730 return 1;
732 } else
733 vlan = 0;
735 bt_vhci_add(vlan);
736 return 0;
738 } else if (strstart(opt, "device:", &endp))
739 return !bt_device_add(endp);
741 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
742 return 1;
745 /***********************************************************/
746 /* QEMU Block devices */
748 #define HD_OPTS "media=disk"
749 #define CDROM_OPTS "media=cdrom"
750 #define FD_OPTS ""
751 #define PFLASH_OPTS ""
752 #define MTD_OPTS ""
753 #define SD_OPTS ""
755 static int drive_init_func(QemuOpts *opts, void *opaque)
757 int *use_scsi = opaque;
759 return drive_init(opts, *use_scsi) == NULL;
762 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
764 if (NULL == qemu_opt_get(opts, "snapshot")) {
765 qemu_opt_set(opts, "snapshot", "on");
767 return 0;
770 static void default_drive(int enable, int snapshot, int use_scsi,
771 BlockInterfaceType type, int index,
772 const char *optstr)
774 QemuOpts *opts;
776 if (type == IF_DEFAULT) {
777 type = use_scsi ? IF_SCSI : IF_IDE;
780 if (!enable || drive_get_by_index(type, index)) {
781 return;
784 opts = drive_add(type, index, NULL, optstr);
785 if (snapshot) {
786 drive_enable_snapshot(opts, NULL);
788 if (!drive_init(opts, use_scsi)) {
789 exit(1);
793 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
795 boot_set_handler = func;
796 boot_set_opaque = opaque;
799 int qemu_boot_set(const char *boot_devices)
801 if (!boot_set_handler) {
802 return -EINVAL;
804 return boot_set_handler(boot_set_opaque, boot_devices);
807 static void validate_bootdevices(char *devices)
809 /* We just do some generic consistency checks */
810 const char *p;
811 int bitmap = 0;
813 for (p = devices; *p != '\0'; p++) {
814 /* Allowed boot devices are:
815 * a-b: floppy disk drives
816 * c-f: IDE disk drives
817 * g-m: machine implementation dependent drives
818 * n-p: network devices
819 * It's up to each machine implementation to check if the given boot
820 * devices match the actual hardware implementation and firmware
821 * features.
823 if (*p < 'a' || *p > 'p') {
824 fprintf(stderr, "Invalid boot device '%c'\n", *p);
825 exit(1);
827 if (bitmap & (1 << (*p - 'a'))) {
828 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
829 exit(1);
831 bitmap |= 1 << (*p - 'a');
835 static void restore_boot_devices(void *opaque)
837 char *standard_boot_devices = opaque;
838 static int first = 1;
840 /* Restore boot order and remove ourselves after the first boot */
841 if (first) {
842 first = 0;
843 return;
846 qemu_boot_set(standard_boot_devices);
848 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
849 g_free(standard_boot_devices);
852 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
853 const char *suffix)
855 FWBootEntry *node, *i;
857 if (bootindex < 0) {
858 return;
861 assert(dev != NULL || suffix != NULL);
863 node = g_malloc0(sizeof(FWBootEntry));
864 node->bootindex = bootindex;
865 node->suffix = suffix ? g_strdup(suffix) : NULL;
866 node->dev = dev;
868 QTAILQ_FOREACH(i, &fw_boot_order, link) {
869 if (i->bootindex == bootindex) {
870 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
871 exit(1);
872 } else if (i->bootindex < bootindex) {
873 continue;
875 QTAILQ_INSERT_BEFORE(i, node, link);
876 return;
878 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
882 * This function returns null terminated string that consist of new line
883 * separated device paths.
885 * memory pointed by "size" is assigned total length of the array in bytes
888 char *get_boot_devices_list(uint32_t *size)
890 FWBootEntry *i;
891 uint32_t total = 0;
892 char *list = NULL;
894 QTAILQ_FOREACH(i, &fw_boot_order, link) {
895 char *devpath = NULL, *bootpath;
896 int len;
898 if (i->dev) {
899 devpath = qdev_get_fw_dev_path(i->dev);
900 assert(devpath);
903 if (i->suffix && devpath) {
904 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
906 bootpath = g_malloc(bootpathlen);
907 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
908 g_free(devpath);
909 } else if (devpath) {
910 bootpath = devpath;
911 } else {
912 assert(i->suffix);
913 bootpath = g_strdup(i->suffix);
916 if (total) {
917 list[total-1] = '\n';
919 len = strlen(bootpath) + 1;
920 list = g_realloc(list, total + len);
921 memcpy(&list[total], bootpath, len);
922 total += len;
923 g_free(bootpath);
926 *size = total;
928 return list;
931 static void numa_add(const char *optarg)
933 char option[128];
934 char *endptr;
935 unsigned long long value, endvalue;
936 int nodenr;
938 optarg = get_opt_name(option, 128, optarg, ',') + 1;
939 if (!strcmp(option, "node")) {
940 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
941 nodenr = nb_numa_nodes;
942 } else {
943 nodenr = strtoull(option, NULL, 10);
946 if (get_param_value(option, 128, "mem", optarg) == 0) {
947 node_mem[nodenr] = 0;
948 } else {
949 int64_t sval;
950 sval = strtosz(option, &endptr);
951 if (sval < 0 || *endptr) {
952 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
953 exit(1);
955 node_mem[nodenr] = sval;
957 if (get_param_value(option, 128, "cpus", optarg) == 0) {
958 node_cpumask[nodenr] = 0;
959 } else {
960 value = strtoull(option, &endptr, 10);
961 if (value >= 64) {
962 value = 63;
963 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
964 } else {
965 if (*endptr == '-') {
966 endvalue = strtoull(endptr+1, &endptr, 10);
967 if (endvalue >= 63) {
968 endvalue = 62;
969 fprintf(stderr,
970 "only 63 CPUs in NUMA mode supported.\n");
972 value = (2ULL << endvalue) - (1ULL << value);
973 } else {
974 value = 1ULL << value;
977 node_cpumask[nodenr] = value;
979 nb_numa_nodes++;
981 return;
984 static void smp_parse(const char *optarg)
986 int smp, sockets = 0, threads = 0, cores = 0;
987 char *endptr;
988 char option[128];
990 smp = strtoul(optarg, &endptr, 10);
991 if (endptr != optarg) {
992 if (*endptr == ',') {
993 endptr++;
996 if (get_param_value(option, 128, "sockets", endptr) != 0)
997 sockets = strtoull(option, NULL, 10);
998 if (get_param_value(option, 128, "cores", endptr) != 0)
999 cores = strtoull(option, NULL, 10);
1000 if (get_param_value(option, 128, "threads", endptr) != 0)
1001 threads = strtoull(option, NULL, 10);
1002 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1003 max_cpus = strtoull(option, NULL, 10);
1005 /* compute missing values, prefer sockets over cores over threads */
1006 if (smp == 0 || sockets == 0) {
1007 sockets = sockets > 0 ? sockets : 1;
1008 cores = cores > 0 ? cores : 1;
1009 threads = threads > 0 ? threads : 1;
1010 if (smp == 0) {
1011 smp = cores * threads * sockets;
1013 } else {
1014 if (cores == 0) {
1015 threads = threads > 0 ? threads : 1;
1016 cores = smp / (sockets * threads);
1017 } else {
1018 threads = smp / (cores * sockets);
1021 smp_cpus = smp;
1022 smp_cores = cores > 0 ? cores : 1;
1023 smp_threads = threads > 0 ? threads : 1;
1024 if (max_cpus == 0)
1025 max_cpus = smp_cpus;
1028 /***********************************************************/
1029 /* USB devices */
1031 static int usb_device_add(const char *devname)
1033 const char *p;
1034 USBDevice *dev = NULL;
1036 if (!usb_enabled)
1037 return -1;
1039 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1040 dev = usbdevice_create(devname);
1041 if (dev)
1042 goto done;
1044 /* the other ones */
1045 #ifndef CONFIG_LINUX
1046 /* only the linux version is qdev-ified, usb-bsd still needs this */
1047 if (strstart(devname, "host:", &p)) {
1048 dev = usb_host_device_open(p);
1049 } else
1050 #endif
1051 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1052 dev = usb_bt_init(devname[2] ? hci_init(p) :
1053 bt_new_hci(qemu_find_bt_vlan(0)));
1054 } else {
1055 return -1;
1057 if (!dev)
1058 return -1;
1060 done:
1061 return 0;
1064 static int usb_device_del(const char *devname)
1066 int bus_num, addr;
1067 const char *p;
1069 if (strstart(devname, "host:", &p))
1070 return usb_host_device_close(p);
1072 if (!usb_enabled)
1073 return -1;
1075 p = strchr(devname, '.');
1076 if (!p)
1077 return -1;
1078 bus_num = strtoul(devname, NULL, 0);
1079 addr = strtoul(p + 1, NULL, 0);
1081 return usb_device_delete_addr(bus_num, addr);
1084 static int usb_parse(const char *cmdline)
1086 int r;
1087 r = usb_device_add(cmdline);
1088 if (r < 0) {
1089 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1091 return r;
1094 void do_usb_add(Monitor *mon, const QDict *qdict)
1096 const char *devname = qdict_get_str(qdict, "devname");
1097 if (usb_device_add(devname) < 0) {
1098 error_report("could not add USB device '%s'", devname);
1102 void do_usb_del(Monitor *mon, const QDict *qdict)
1104 const char *devname = qdict_get_str(qdict, "devname");
1105 if (usb_device_del(devname) < 0) {
1106 error_report("could not delete USB device '%s'", devname);
1110 /***********************************************************/
1111 /* PCMCIA/Cardbus */
1113 static struct pcmcia_socket_entry_s {
1114 PCMCIASocket *socket;
1115 struct pcmcia_socket_entry_s *next;
1116 } *pcmcia_sockets = 0;
1118 void pcmcia_socket_register(PCMCIASocket *socket)
1120 struct pcmcia_socket_entry_s *entry;
1122 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1123 entry->socket = socket;
1124 entry->next = pcmcia_sockets;
1125 pcmcia_sockets = entry;
1128 void pcmcia_socket_unregister(PCMCIASocket *socket)
1130 struct pcmcia_socket_entry_s *entry, **ptr;
1132 ptr = &pcmcia_sockets;
1133 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1134 if (entry->socket == socket) {
1135 *ptr = entry->next;
1136 g_free(entry);
1140 void pcmcia_info(Monitor *mon)
1142 struct pcmcia_socket_entry_s *iter;
1144 if (!pcmcia_sockets)
1145 monitor_printf(mon, "No PCMCIA sockets\n");
1147 for (iter = pcmcia_sockets; iter; iter = iter->next)
1148 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1149 iter->socket->attached ? iter->socket->card_string :
1150 "Empty");
1153 /***********************************************************/
1154 /* machine registration */
1156 static QEMUMachine *first_machine = NULL;
1157 QEMUMachine *current_machine = NULL;
1159 int qemu_register_machine(QEMUMachine *m)
1161 QEMUMachine **pm;
1162 pm = &first_machine;
1163 while (*pm != NULL)
1164 pm = &(*pm)->next;
1165 m->next = NULL;
1166 *pm = m;
1167 return 0;
1170 static QEMUMachine *find_machine(const char *name)
1172 QEMUMachine *m;
1174 for(m = first_machine; m != NULL; m = m->next) {
1175 if (!strcmp(m->name, name))
1176 return m;
1177 if (m->alias && !strcmp(m->alias, name))
1178 return m;
1180 return NULL;
1183 static QEMUMachine *find_default_machine(void)
1185 QEMUMachine *m;
1187 for(m = first_machine; m != NULL; m = m->next) {
1188 if (m->is_default) {
1189 return m;
1192 return NULL;
1195 /***********************************************************/
1196 /* main execution loop */
1198 static void gui_update(void *opaque)
1200 uint64_t interval = GUI_REFRESH_INTERVAL;
1201 DisplayState *ds = opaque;
1202 DisplayChangeListener *dcl = ds->listeners;
1204 dpy_refresh(ds);
1206 while (dcl != NULL) {
1207 if (dcl->gui_timer_interval &&
1208 dcl->gui_timer_interval < interval)
1209 interval = dcl->gui_timer_interval;
1210 dcl = dcl->next;
1212 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1215 struct vm_change_state_entry {
1216 VMChangeStateHandler *cb;
1217 void *opaque;
1218 QLIST_ENTRY (vm_change_state_entry) entries;
1221 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1223 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1224 void *opaque)
1226 VMChangeStateEntry *e;
1228 e = g_malloc0(sizeof (*e));
1230 e->cb = cb;
1231 e->opaque = opaque;
1232 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1233 return e;
1236 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1238 QLIST_REMOVE (e, entries);
1239 g_free (e);
1242 void vm_state_notify(int running, RunState state)
1244 VMChangeStateEntry *e;
1246 trace_vm_state_notify(running, state);
1248 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1249 e->cb(e->opaque, running, state);
1253 void vm_start(void)
1255 if (!runstate_is_running()) {
1256 cpu_enable_ticks();
1257 runstate_set(RUN_STATE_RUNNING);
1258 vm_state_notify(1, RUN_STATE_RUNNING);
1259 resume_all_vcpus();
1260 monitor_protocol_event(QEVENT_RESUME, NULL);
1264 /* reset/shutdown handler */
1266 typedef struct QEMUResetEntry {
1267 QTAILQ_ENTRY(QEMUResetEntry) entry;
1268 QEMUResetHandler *func;
1269 void *opaque;
1270 } QEMUResetEntry;
1272 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1273 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1274 static int reset_requested;
1275 static int shutdown_requested, shutdown_signal = -1;
1276 static pid_t shutdown_pid;
1277 static int powerdown_requested;
1278 static int debug_requested;
1279 static RunState vmstop_requested = RUN_STATE_MAX;
1281 int qemu_shutdown_requested_get(void)
1283 return shutdown_requested;
1286 int qemu_reset_requested_get(void)
1288 return reset_requested;
1291 int qemu_shutdown_requested(void)
1293 int r = shutdown_requested;
1294 shutdown_requested = 0;
1295 return r;
1298 void qemu_kill_report(void)
1300 if (shutdown_signal != -1) {
1301 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1302 if (shutdown_pid == 0) {
1303 /* This happens for eg ^C at the terminal, so it's worth
1304 * avoiding printing an odd message in that case.
1306 fputc('\n', stderr);
1307 } else {
1308 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1310 shutdown_signal = -1;
1314 int qemu_reset_requested(void)
1316 int r = reset_requested;
1317 reset_requested = 0;
1318 return r;
1321 int qemu_powerdown_requested(void)
1323 int r = powerdown_requested;
1324 powerdown_requested = 0;
1325 return r;
1328 static int qemu_debug_requested(void)
1330 int r = debug_requested;
1331 debug_requested = 0;
1332 return r;
1335 /* We use RUN_STATE_MAX but any invalid value will do */
1336 static bool qemu_vmstop_requested(RunState *r)
1338 if (vmstop_requested < RUN_STATE_MAX) {
1339 *r = vmstop_requested;
1340 vmstop_requested = RUN_STATE_MAX;
1341 return true;
1344 return false;
1347 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1349 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1351 re->func = func;
1352 re->opaque = opaque;
1353 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1356 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1358 QEMUResetEntry *re;
1360 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1361 if (re->func == func && re->opaque == opaque) {
1362 QTAILQ_REMOVE(&reset_handlers, re, entry);
1363 g_free(re);
1364 return;
1369 void qemu_system_reset(bool report)
1371 QEMUResetEntry *re, *nre;
1373 /* reset all devices */
1374 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1375 re->func(re->opaque);
1377 if (report) {
1378 monitor_protocol_event(QEVENT_RESET, NULL);
1380 cpu_synchronize_all_post_reset();
1383 void qemu_system_reset_request(void)
1385 if (no_reboot) {
1386 shutdown_requested = 1;
1387 } else {
1388 reset_requested = 1;
1390 cpu_stop_current();
1391 qemu_notify_event();
1394 void qemu_system_killed(int signal, pid_t pid)
1396 shutdown_signal = signal;
1397 shutdown_pid = pid;
1398 no_shutdown = 0;
1399 qemu_system_shutdown_request();
1402 void qemu_system_shutdown_request(void)
1404 shutdown_requested = 1;
1405 qemu_notify_event();
1408 void qemu_system_powerdown_request(void)
1410 powerdown_requested = 1;
1411 qemu_notify_event();
1414 void qemu_system_debug_request(void)
1416 debug_requested = 1;
1417 qemu_notify_event();
1420 void qemu_system_vmstop_request(RunState state)
1422 vmstop_requested = state;
1423 qemu_notify_event();
1426 qemu_irq qemu_system_powerdown;
1428 static bool main_loop_should_exit(void)
1430 RunState r;
1431 if (qemu_debug_requested()) {
1432 vm_stop(RUN_STATE_DEBUG);
1434 if (qemu_shutdown_requested()) {
1435 qemu_kill_report();
1436 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1437 if (no_shutdown) {
1438 vm_stop(RUN_STATE_SHUTDOWN);
1439 } else {
1440 return true;
1443 if (qemu_reset_requested()) {
1444 pause_all_vcpus();
1445 cpu_synchronize_all_states();
1446 qemu_system_reset(VMRESET_REPORT);
1447 resume_all_vcpus();
1448 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1449 runstate_check(RUN_STATE_SHUTDOWN)) {
1450 runstate_set(RUN_STATE_PAUSED);
1453 if (qemu_powerdown_requested()) {
1454 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1455 qemu_irq_raise(qemu_system_powerdown);
1457 if (qemu_vmstop_requested(&r)) {
1458 vm_stop(r);
1460 return false;
1463 static void main_loop(void)
1465 bool nonblocking;
1466 int last_io = 0;
1467 #ifdef CONFIG_PROFILER
1468 int64_t ti;
1469 #endif
1470 do {
1471 nonblocking = !kvm_enabled() && last_io > 0;
1472 #ifdef CONFIG_PROFILER
1473 ti = profile_getclock();
1474 #endif
1475 last_io = main_loop_wait(nonblocking);
1476 #ifdef CONFIG_PROFILER
1477 dev_time += profile_getclock() - ti;
1478 #endif
1479 } while (!main_loop_should_exit());
1482 static void version(void)
1484 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1487 static void help(int exitcode)
1489 version();
1490 printf("usage: %s [options] [disk_image]\n\n"
1491 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1492 error_get_progname());
1494 #define QEMU_OPTIONS_GENERATE_HELP
1495 #include "qemu-options-wrapper.h"
1497 printf("\nDuring emulation, the following keys are useful:\n"
1498 "ctrl-alt-f toggle full screen\n"
1499 "ctrl-alt-n switch to virtual console 'n'\n"
1500 "ctrl-alt toggle mouse and keyboard grab\n"
1501 "\n"
1502 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1504 exit(exitcode);
1507 #define HAS_ARG 0x0001
1509 typedef struct QEMUOption {
1510 const char *name;
1511 int flags;
1512 int index;
1513 uint32_t arch_mask;
1514 } QEMUOption;
1516 static const QEMUOption qemu_options[] = {
1517 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1518 #define QEMU_OPTIONS_GENERATE_OPTIONS
1519 #include "qemu-options-wrapper.h"
1520 { NULL },
1523 static bool vga_available(void)
1525 return qdev_exists("VGA") || qdev_exists("isa-vga");
1528 static bool cirrus_vga_available(void)
1530 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1533 static bool vmware_vga_available(void)
1535 return qdev_exists("vmware-svga");
1538 static void select_vgahw (const char *p)
1540 const char *opts;
1542 vga_interface_type = VGA_NONE;
1543 if (strstart(p, "std", &opts)) {
1544 if (vga_available()) {
1545 vga_interface_type = VGA_STD;
1546 } else {
1547 fprintf(stderr, "Error: standard VGA not available\n");
1548 exit(0);
1550 } else if (strstart(p, "cirrus", &opts)) {
1551 if (cirrus_vga_available()) {
1552 vga_interface_type = VGA_CIRRUS;
1553 } else {
1554 fprintf(stderr, "Error: Cirrus VGA not available\n");
1555 exit(0);
1557 } else if (strstart(p, "vmware", &opts)) {
1558 if (vmware_vga_available()) {
1559 vga_interface_type = VGA_VMWARE;
1560 } else {
1561 fprintf(stderr, "Error: VMWare SVGA not available\n");
1562 exit(0);
1564 } else if (strstart(p, "xenfb", &opts)) {
1565 vga_interface_type = VGA_XENFB;
1566 } else if (strstart(p, "qxl", &opts)) {
1567 vga_interface_type = VGA_QXL;
1568 } else if (!strstart(p, "none", &opts)) {
1569 invalid_vga:
1570 fprintf(stderr, "Unknown vga type: %s\n", p);
1571 exit(1);
1573 while (*opts) {
1574 const char *nextopt;
1576 if (strstart(opts, ",retrace=", &nextopt)) {
1577 opts = nextopt;
1578 if (strstart(opts, "dumb", &nextopt))
1579 vga_retrace_method = VGA_RETRACE_DUMB;
1580 else if (strstart(opts, "precise", &nextopt))
1581 vga_retrace_method = VGA_RETRACE_PRECISE;
1582 else goto invalid_vga;
1583 } else goto invalid_vga;
1584 opts = nextopt;
1588 static DisplayType select_display(const char *p)
1590 const char *opts;
1591 DisplayType display = DT_DEFAULT;
1593 if (strstart(p, "sdl", &opts)) {
1594 #ifdef CONFIG_SDL
1595 display = DT_SDL;
1596 while (*opts) {
1597 const char *nextopt;
1599 if (strstart(opts, ",frame=", &nextopt)) {
1600 opts = nextopt;
1601 if (strstart(opts, "on", &nextopt)) {
1602 no_frame = 0;
1603 } else if (strstart(opts, "off", &nextopt)) {
1604 no_frame = 1;
1605 } else {
1606 goto invalid_sdl_args;
1608 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1609 opts = nextopt;
1610 if (strstart(opts, "on", &nextopt)) {
1611 alt_grab = 1;
1612 } else if (strstart(opts, "off", &nextopt)) {
1613 alt_grab = 0;
1614 } else {
1615 goto invalid_sdl_args;
1617 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1618 opts = nextopt;
1619 if (strstart(opts, "on", &nextopt)) {
1620 ctrl_grab = 1;
1621 } else if (strstart(opts, "off", &nextopt)) {
1622 ctrl_grab = 0;
1623 } else {
1624 goto invalid_sdl_args;
1626 } else if (strstart(opts, ",window_close=", &nextopt)) {
1627 opts = nextopt;
1628 if (strstart(opts, "on", &nextopt)) {
1629 no_quit = 0;
1630 } else if (strstart(opts, "off", &nextopt)) {
1631 no_quit = 1;
1632 } else {
1633 goto invalid_sdl_args;
1635 } else {
1636 invalid_sdl_args:
1637 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1638 exit(1);
1640 opts = nextopt;
1642 #else
1643 fprintf(stderr, "SDL support is disabled\n");
1644 exit(1);
1645 #endif
1646 } else if (strstart(p, "vnc", &opts)) {
1647 #ifdef CONFIG_VNC
1648 display_remote++;
1650 if (*opts) {
1651 const char *nextopt;
1653 if (strstart(opts, "=", &nextopt)) {
1654 vnc_display = nextopt;
1657 if (!vnc_display) {
1658 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1659 exit(1);
1661 #else
1662 fprintf(stderr, "VNC support is disabled\n");
1663 exit(1);
1664 #endif
1665 } else if (strstart(p, "curses", &opts)) {
1666 #ifdef CONFIG_CURSES
1667 display = DT_CURSES;
1668 #else
1669 fprintf(stderr, "Curses support is disabled\n");
1670 exit(1);
1671 #endif
1672 } else if (strstart(p, "none", &opts)) {
1673 display = DT_NONE;
1674 } else {
1675 fprintf(stderr, "Unknown display type: %s\n", p);
1676 exit(1);
1679 return display;
1682 static int balloon_parse(const char *arg)
1684 QemuOpts *opts;
1686 if (strcmp(arg, "none") == 0) {
1687 return 0;
1690 if (!strncmp(arg, "virtio", 6)) {
1691 if (arg[6] == ',') {
1692 /* have params -> parse them */
1693 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1694 if (!opts)
1695 return -1;
1696 } else {
1697 /* create empty opts */
1698 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1700 qemu_opt_set(opts, "driver", "virtio-balloon");
1701 return 0;
1704 return -1;
1707 char *qemu_find_file(int type, const char *name)
1709 int len;
1710 const char *subdir;
1711 char *buf;
1713 /* If name contains path separators then try it as a straight path. */
1714 if ((strchr(name, '/') || strchr(name, '\\'))
1715 && access(name, R_OK) == 0) {
1716 return g_strdup(name);
1718 switch (type) {
1719 case QEMU_FILE_TYPE_BIOS:
1720 subdir = "";
1721 break;
1722 case QEMU_FILE_TYPE_KEYMAP:
1723 subdir = "keymaps/";
1724 break;
1725 default:
1726 abort();
1728 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1729 buf = g_malloc0(len);
1730 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1731 if (access(buf, R_OK)) {
1732 g_free(buf);
1733 return NULL;
1735 return buf;
1738 static int device_help_func(QemuOpts *opts, void *opaque)
1740 return qdev_device_help(opts);
1743 static int device_init_func(QemuOpts *opts, void *opaque)
1745 DeviceState *dev;
1747 dev = qdev_device_add(opts);
1748 if (!dev)
1749 return -1;
1750 return 0;
1753 static int chardev_init_func(QemuOpts *opts, void *opaque)
1755 CharDriverState *chr;
1757 chr = qemu_chr_new_from_opts(opts, NULL);
1758 if (!chr)
1759 return -1;
1760 return 0;
1763 #ifdef CONFIG_VIRTFS
1764 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1766 int ret;
1767 ret = qemu_fsdev_add(opts);
1769 return ret;
1771 #endif
1773 static int mon_init_func(QemuOpts *opts, void *opaque)
1775 CharDriverState *chr;
1776 const char *chardev;
1777 const char *mode;
1778 int flags;
1780 mode = qemu_opt_get(opts, "mode");
1781 if (mode == NULL) {
1782 mode = "readline";
1784 if (strcmp(mode, "readline") == 0) {
1785 flags = MONITOR_USE_READLINE;
1786 } else if (strcmp(mode, "control") == 0) {
1787 flags = MONITOR_USE_CONTROL;
1788 } else {
1789 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1790 exit(1);
1793 if (qemu_opt_get_bool(opts, "pretty", 0))
1794 flags |= MONITOR_USE_PRETTY;
1796 if (qemu_opt_get_bool(opts, "default", 0))
1797 flags |= MONITOR_IS_DEFAULT;
1799 chardev = qemu_opt_get(opts, "chardev");
1800 chr = qemu_chr_find(chardev);
1801 if (chr == NULL) {
1802 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1803 exit(1);
1806 monitor_init(chr, flags);
1807 return 0;
1810 static void monitor_parse(const char *optarg, const char *mode)
1812 static int monitor_device_index = 0;
1813 QemuOpts *opts;
1814 const char *p;
1815 char label[32];
1816 int def = 0;
1818 if (strstart(optarg, "chardev:", &p)) {
1819 snprintf(label, sizeof(label), "%s", p);
1820 } else {
1821 snprintf(label, sizeof(label), "compat_monitor%d",
1822 monitor_device_index);
1823 if (monitor_device_index == 0) {
1824 def = 1;
1826 opts = qemu_chr_parse_compat(label, optarg);
1827 if (!opts) {
1828 fprintf(stderr, "parse error: %s\n", optarg);
1829 exit(1);
1833 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1834 if (!opts) {
1835 fprintf(stderr, "duplicate chardev: %s\n", label);
1836 exit(1);
1838 qemu_opt_set(opts, "mode", mode);
1839 qemu_opt_set(opts, "chardev", label);
1840 if (def)
1841 qemu_opt_set(opts, "default", "on");
1842 monitor_device_index++;
1845 struct device_config {
1846 enum {
1847 DEV_USB, /* -usbdevice */
1848 DEV_BT, /* -bt */
1849 DEV_SERIAL, /* -serial */
1850 DEV_PARALLEL, /* -parallel */
1851 DEV_VIRTCON, /* -virtioconsole */
1852 DEV_DEBUGCON, /* -debugcon */
1853 } type;
1854 const char *cmdline;
1855 QTAILQ_ENTRY(device_config) next;
1857 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1859 static void add_device_config(int type, const char *cmdline)
1861 struct device_config *conf;
1863 conf = g_malloc0(sizeof(*conf));
1864 conf->type = type;
1865 conf->cmdline = cmdline;
1866 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1869 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1871 struct device_config *conf;
1872 int rc;
1874 QTAILQ_FOREACH(conf, &device_configs, next) {
1875 if (conf->type != type)
1876 continue;
1877 rc = func(conf->cmdline);
1878 if (0 != rc)
1879 return rc;
1881 return 0;
1884 static int serial_parse(const char *devname)
1886 static int index = 0;
1887 char label[32];
1889 if (strcmp(devname, "none") == 0)
1890 return 0;
1891 if (index == MAX_SERIAL_PORTS) {
1892 fprintf(stderr, "qemu: too many serial ports\n");
1893 exit(1);
1895 snprintf(label, sizeof(label), "serial%d", index);
1896 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1897 if (!serial_hds[index]) {
1898 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1899 devname, strerror(errno));
1900 return -1;
1902 index++;
1903 return 0;
1906 static int parallel_parse(const char *devname)
1908 static int index = 0;
1909 char label[32];
1911 if (strcmp(devname, "none") == 0)
1912 return 0;
1913 if (index == MAX_PARALLEL_PORTS) {
1914 fprintf(stderr, "qemu: too many parallel ports\n");
1915 exit(1);
1917 snprintf(label, sizeof(label), "parallel%d", index);
1918 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
1919 if (!parallel_hds[index]) {
1920 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1921 devname, strerror(errno));
1922 return -1;
1924 index++;
1925 return 0;
1928 static int virtcon_parse(const char *devname)
1930 QemuOptsList *device = qemu_find_opts("device");
1931 static int index = 0;
1932 char label[32];
1933 QemuOpts *bus_opts, *dev_opts;
1935 if (strcmp(devname, "none") == 0)
1936 return 0;
1937 if (index == MAX_VIRTIO_CONSOLES) {
1938 fprintf(stderr, "qemu: too many virtio consoles\n");
1939 exit(1);
1942 bus_opts = qemu_opts_create(device, NULL, 0);
1943 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1945 dev_opts = qemu_opts_create(device, NULL, 0);
1946 qemu_opt_set(dev_opts, "driver", "virtconsole");
1948 snprintf(label, sizeof(label), "virtcon%d", index);
1949 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
1950 if (!virtcon_hds[index]) {
1951 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1952 devname, strerror(errno));
1953 return -1;
1955 qemu_opt_set(dev_opts, "chardev", label);
1957 index++;
1958 return 0;
1961 static int debugcon_parse(const char *devname)
1963 QemuOpts *opts;
1965 if (!qemu_chr_new("debugcon", devname, NULL)) {
1966 exit(1);
1968 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1969 if (!opts) {
1970 fprintf(stderr, "qemu: already have a debugcon device\n");
1971 exit(1);
1973 qemu_opt_set(opts, "driver", "isa-debugcon");
1974 qemu_opt_set(opts, "chardev", "debugcon");
1975 return 0;
1978 static QEMUMachine *machine_parse(const char *name)
1980 QEMUMachine *m, *machine = NULL;
1982 if (name) {
1983 machine = find_machine(name);
1985 if (machine) {
1986 return machine;
1988 printf("Supported machines are:\n");
1989 for (m = first_machine; m != NULL; m = m->next) {
1990 if (m->alias) {
1991 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
1993 printf("%-10s %s%s\n", m->name, m->desc,
1994 m->is_default ? " (default)" : "");
1996 exit(!name || *name != '?');
1999 static int tcg_init(void)
2001 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2002 return 0;
2005 static struct {
2006 const char *opt_name;
2007 const char *name;
2008 int (*available)(void);
2009 int (*init)(void);
2010 int *allowed;
2011 } accel_list[] = {
2012 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2013 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2014 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2017 static int configure_accelerator(void)
2019 const char *p = NULL;
2020 char buf[10];
2021 int i, ret;
2022 bool accel_initalised = 0;
2023 bool init_failed = 0;
2025 QemuOptsList *list = qemu_find_opts("machine");
2026 if (!QTAILQ_EMPTY(&list->head)) {
2027 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2030 if (p == NULL) {
2031 #ifdef CONFIG_KVM_OPTIONS
2032 /* Use the default "accelerator", kvm */
2033 p = "kvm";
2034 #else
2035 /* Use the default "accelerator", tcg */
2036 p = "tcg";
2037 #endif
2040 while (!accel_initalised && *p != '\0') {
2041 if (*p == ':') {
2042 p++;
2044 p = get_opt_name(buf, sizeof (buf), p, ':');
2045 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2046 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2047 *(accel_list[i].allowed) = 1;
2048 ret = accel_list[i].init();
2049 if (ret < 0) {
2050 init_failed = 1;
2051 if (!accel_list[i].available()) {
2052 printf("%s not supported for this target\n",
2053 accel_list[i].name);
2054 } else {
2055 fprintf(stderr, "failed to initialize %s: %s\n",
2056 accel_list[i].name,
2057 strerror(-ret));
2059 *(accel_list[i].allowed) = 0;
2060 } else {
2061 accel_initalised = 1;
2063 break;
2066 if (i == ARRAY_SIZE(accel_list)) {
2067 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2071 if (!accel_initalised) {
2072 fprintf(stderr, "No accelerator found!\n");
2073 exit(1);
2076 if (init_failed) {
2077 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2080 return !accel_initalised;
2083 void qemu_add_exit_notifier(Notifier *notify)
2085 notifier_list_add(&exit_notifiers, notify);
2088 void qemu_remove_exit_notifier(Notifier *notify)
2090 notifier_list_remove(&exit_notifiers, notify);
2093 static void qemu_run_exit_notifiers(void)
2095 notifier_list_notify(&exit_notifiers, NULL);
2098 void qemu_add_machine_init_done_notifier(Notifier *notify)
2100 notifier_list_add(&machine_init_done_notifiers, notify);
2103 static void qemu_run_machine_init_done_notifiers(void)
2105 notifier_list_notify(&machine_init_done_notifiers, NULL);
2108 static const QEMUOption *lookup_opt(int argc, char **argv,
2109 const char **poptarg, int *poptind)
2111 const QEMUOption *popt;
2112 int optind = *poptind;
2113 char *r = argv[optind];
2114 const char *optarg;
2116 loc_set_cmdline(argv, optind, 1);
2117 optind++;
2118 /* Treat --foo the same as -foo. */
2119 if (r[1] == '-')
2120 r++;
2121 popt = qemu_options;
2122 for(;;) {
2123 if (!popt->name) {
2124 error_report("invalid option");
2125 exit(1);
2127 if (!strcmp(popt->name, r + 1))
2128 break;
2129 popt++;
2131 if (popt->flags & HAS_ARG) {
2132 if (optind >= argc) {
2133 error_report("requires an argument");
2134 exit(1);
2136 optarg = argv[optind++];
2137 loc_set_cmdline(argv, optind - 2, 2);
2138 } else {
2139 optarg = NULL;
2142 *poptarg = optarg;
2143 *poptind = optind;
2145 return popt;
2148 static gpointer malloc_and_trace(gsize n_bytes)
2150 void *ptr = malloc(n_bytes);
2151 trace_g_malloc(n_bytes, ptr);
2152 return ptr;
2155 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2157 void *ptr = realloc(mem, n_bytes);
2158 trace_g_realloc(mem, n_bytes, ptr);
2159 return ptr;
2162 static void free_and_trace(gpointer mem)
2164 trace_g_free(mem);
2165 free(mem);
2168 #ifdef CONFIG_KVM_OPTIONS
2169 int kvm_irqchip = 1;
2170 int kvm_pit = 1;
2171 int kvm_pit_reinject = 1;
2172 #endif
2174 int main(int argc, char **argv, char **envp)
2176 const char *gdbstub_dev = NULL;
2177 int i;
2178 int snapshot, linux_boot;
2179 const char *icount_option = NULL;
2180 const char *initrd_filename;
2181 const char *kernel_filename, *kernel_cmdline;
2182 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2183 DisplayState *ds;
2184 DisplayChangeListener *dcl;
2185 int cyls, heads, secs, translation;
2186 QemuOpts *hda_opts = NULL, *opts;
2187 QemuOptsList *olist;
2188 int optind;
2189 const char *optarg;
2190 const char *loadvm = NULL;
2191 QEMUMachine *machine;
2192 const char *cpu_model;
2193 const char *vga_model = NULL;
2194 const char *pid_file = NULL;
2195 const char *incoming = NULL;
2196 #ifdef CONFIG_VNC
2197 int show_vnc_port = 0;
2198 #endif
2199 int defconfig = 1;
2200 const char *log_mask = NULL;
2201 const char *log_file = NULL;
2202 GMemVTable mem_trace = {
2203 .malloc = malloc_and_trace,
2204 .realloc = realloc_and_trace,
2205 .free = free_and_trace,
2207 const char *trace_events = NULL;
2208 const char *trace_file = NULL;
2210 atexit(qemu_run_exit_notifiers);
2211 error_set_progname(argv[0]);
2213 g_mem_set_vtable(&mem_trace);
2214 if (!g_thread_supported()) {
2215 #if !GLIB_CHECK_VERSION(2, 31, 0)
2216 g_thread_init(NULL);
2217 #else
2218 fprintf(stderr, "glib threading failed to initialize.\n");
2219 exit(1);
2220 #endif
2223 runstate_init();
2225 init_clocks();
2226 rtc_clock = host_clock;
2228 qemu_cache_utils_init(envp);
2230 QLIST_INIT (&vm_change_state_head);
2231 os_setup_early_signal_handling();
2233 module_call_init(MODULE_INIT_MACHINE);
2234 machine = find_default_machine();
2235 cpu_model = NULL;
2236 initrd_filename = NULL;
2237 ram_size = 0;
2238 snapshot = 0;
2239 kernel_filename = NULL;
2240 kernel_cmdline = "";
2241 cyls = heads = secs = 0;
2242 translation = BIOS_ATA_TRANSLATION_AUTO;
2244 for (i = 0; i < MAX_NODES; i++) {
2245 node_mem[i] = 0;
2246 node_cpumask[i] = 0;
2249 nb_numa_nodes = 0;
2250 nb_nics = 0;
2252 autostart= 1;
2254 /* first pass of option parsing */
2255 optind = 1;
2256 while (optind < argc) {
2257 if (argv[optind][0] != '-') {
2258 /* disk image */
2259 optind++;
2260 continue;
2261 } else {
2262 const QEMUOption *popt;
2264 popt = lookup_opt(argc, argv, &optarg, &optind);
2265 switch (popt->index) {
2266 case QEMU_OPTION_nodefconfig:
2267 defconfig=0;
2268 break;
2273 if (defconfig) {
2274 int ret;
2276 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2277 if (ret < 0 && ret != -ENOENT) {
2278 exit(1);
2281 ret = qemu_read_config_file(arch_config_name);
2282 if (ret < 0 && ret != -ENOENT) {
2283 exit(1);
2286 cpudef_init();
2288 /* second pass of option parsing */
2289 optind = 1;
2290 for(;;) {
2291 if (optind >= argc)
2292 break;
2293 if (argv[optind][0] != '-') {
2294 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2295 } else {
2296 const QEMUOption *popt;
2298 popt = lookup_opt(argc, argv, &optarg, &optind);
2299 if (!(popt->arch_mask & arch_type)) {
2300 printf("Option %s not supported for this target\n", popt->name);
2301 exit(1);
2303 switch(popt->index) {
2304 case QEMU_OPTION_M:
2305 machine = machine_parse(optarg);
2306 break;
2307 case QEMU_OPTION_cpu:
2308 /* hw initialization will check this */
2309 if (*optarg == '?') {
2310 list_cpus(stdout, &fprintf, optarg);
2311 exit(0);
2312 } else {
2313 cpu_model = optarg;
2315 break;
2316 case QEMU_OPTION_initrd:
2317 initrd_filename = optarg;
2318 break;
2319 case QEMU_OPTION_hda:
2321 char buf[256];
2322 if (cyls == 0)
2323 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2324 else
2325 snprintf(buf, sizeof(buf),
2326 "%s,cyls=%d,heads=%d,secs=%d%s",
2327 HD_OPTS , cyls, heads, secs,
2328 translation == BIOS_ATA_TRANSLATION_LBA ?
2329 ",trans=lba" :
2330 translation == BIOS_ATA_TRANSLATION_NONE ?
2331 ",trans=none" : "");
2332 drive_add(IF_DEFAULT, 0, optarg, buf);
2333 break;
2335 case QEMU_OPTION_hdb:
2336 case QEMU_OPTION_hdc:
2337 case QEMU_OPTION_hdd:
2338 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2339 HD_OPTS);
2340 break;
2341 case QEMU_OPTION_drive:
2342 if (drive_def(optarg) == NULL) {
2343 exit(1);
2345 break;
2346 case QEMU_OPTION_set:
2347 if (qemu_set_option(optarg) != 0)
2348 exit(1);
2349 break;
2350 case QEMU_OPTION_global:
2351 if (qemu_global_option(optarg) != 0)
2352 exit(1);
2353 break;
2354 case QEMU_OPTION_mtdblock:
2355 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2356 break;
2357 case QEMU_OPTION_sd:
2358 drive_add(IF_SD, 0, optarg, SD_OPTS);
2359 break;
2360 case QEMU_OPTION_pflash:
2361 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2362 break;
2363 case QEMU_OPTION_snapshot:
2364 snapshot = 1;
2365 break;
2366 case QEMU_OPTION_hdachs:
2368 const char *p;
2369 p = optarg;
2370 cyls = strtol(p, (char **)&p, 0);
2371 if (cyls < 1 || cyls > 16383)
2372 goto chs_fail;
2373 if (*p != ',')
2374 goto chs_fail;
2375 p++;
2376 heads = strtol(p, (char **)&p, 0);
2377 if (heads < 1 || heads > 16)
2378 goto chs_fail;
2379 if (*p != ',')
2380 goto chs_fail;
2381 p++;
2382 secs = strtol(p, (char **)&p, 0);
2383 if (secs < 1 || secs > 63)
2384 goto chs_fail;
2385 if (*p == ',') {
2386 p++;
2387 if (!strcmp(p, "none"))
2388 translation = BIOS_ATA_TRANSLATION_NONE;
2389 else if (!strcmp(p, "lba"))
2390 translation = BIOS_ATA_TRANSLATION_LBA;
2391 else if (!strcmp(p, "auto"))
2392 translation = BIOS_ATA_TRANSLATION_AUTO;
2393 else
2394 goto chs_fail;
2395 } else if (*p != '\0') {
2396 chs_fail:
2397 fprintf(stderr, "qemu: invalid physical CHS format\n");
2398 exit(1);
2400 if (hda_opts != NULL) {
2401 char num[16];
2402 snprintf(num, sizeof(num), "%d", cyls);
2403 qemu_opt_set(hda_opts, "cyls", num);
2404 snprintf(num, sizeof(num), "%d", heads);
2405 qemu_opt_set(hda_opts, "heads", num);
2406 snprintf(num, sizeof(num), "%d", secs);
2407 qemu_opt_set(hda_opts, "secs", num);
2408 if (translation == BIOS_ATA_TRANSLATION_LBA)
2409 qemu_opt_set(hda_opts, "trans", "lba");
2410 if (translation == BIOS_ATA_TRANSLATION_NONE)
2411 qemu_opt_set(hda_opts, "trans", "none");
2414 break;
2415 case QEMU_OPTION_numa:
2416 if (nb_numa_nodes >= MAX_NODES) {
2417 fprintf(stderr, "qemu: too many NUMA nodes\n");
2418 exit(1);
2420 numa_add(optarg);
2421 break;
2422 case QEMU_OPTION_display:
2423 display_type = select_display(optarg);
2424 break;
2425 case QEMU_OPTION_nographic:
2426 display_type = DT_NOGRAPHIC;
2427 break;
2428 case QEMU_OPTION_curses:
2429 #ifdef CONFIG_CURSES
2430 display_type = DT_CURSES;
2431 #else
2432 fprintf(stderr, "Curses support is disabled\n");
2433 exit(1);
2434 #endif
2435 break;
2436 case QEMU_OPTION_portrait:
2437 graphic_rotate = 90;
2438 break;
2439 case QEMU_OPTION_rotate:
2440 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2441 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2442 graphic_rotate != 180 && graphic_rotate != 270) {
2443 fprintf(stderr,
2444 "qemu: only 90, 180, 270 deg rotation is available\n");
2445 exit(1);
2447 break;
2448 case QEMU_OPTION_kernel:
2449 kernel_filename = optarg;
2450 break;
2451 case QEMU_OPTION_append:
2452 kernel_cmdline = optarg;
2453 break;
2454 case QEMU_OPTION_cdrom:
2455 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2456 break;
2457 case QEMU_OPTION_boot:
2459 static const char * const params[] = {
2460 "order", "once", "menu",
2461 "splash", "splash-time", NULL
2463 char buf[sizeof(boot_devices)];
2464 char *standard_boot_devices;
2465 int legacy = 0;
2467 if (!strchr(optarg, '=')) {
2468 legacy = 1;
2469 pstrcpy(buf, sizeof(buf), optarg);
2470 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2471 fprintf(stderr,
2472 "qemu: unknown boot parameter '%s' in '%s'\n",
2473 buf, optarg);
2474 exit(1);
2477 if (legacy ||
2478 get_param_value(buf, sizeof(buf), "order", optarg)) {
2479 validate_bootdevices(buf);
2480 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2482 if (!legacy) {
2483 if (get_param_value(buf, sizeof(buf),
2484 "once", optarg)) {
2485 validate_bootdevices(buf);
2486 standard_boot_devices = g_strdup(boot_devices);
2487 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2488 qemu_register_reset(restore_boot_devices,
2489 standard_boot_devices);
2491 if (get_param_value(buf, sizeof(buf),
2492 "menu", optarg)) {
2493 if (!strcmp(buf, "on")) {
2494 boot_menu = 1;
2495 } else if (!strcmp(buf, "off")) {
2496 boot_menu = 0;
2497 } else {
2498 fprintf(stderr,
2499 "qemu: invalid option value '%s'\n",
2500 buf);
2501 exit(1);
2504 qemu_opts_parse(qemu_find_opts("boot-opts"),
2505 optarg, 0);
2508 break;
2509 case QEMU_OPTION_fda:
2510 case QEMU_OPTION_fdb:
2511 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2512 optarg, FD_OPTS);
2513 break;
2514 case QEMU_OPTION_no_fd_bootchk:
2515 fd_bootchk = 0;
2516 break;
2517 case QEMU_OPTION_netdev:
2518 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2519 exit(1);
2521 break;
2522 case QEMU_OPTION_net:
2523 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2524 exit(1);
2526 break;
2527 #ifdef CONFIG_SLIRP
2528 case QEMU_OPTION_tftp:
2529 legacy_tftp_prefix = optarg;
2530 break;
2531 case QEMU_OPTION_bootp:
2532 legacy_bootp_filename = optarg;
2533 break;
2534 case QEMU_OPTION_redir:
2535 if (net_slirp_redir(optarg) < 0)
2536 exit(1);
2537 break;
2538 #endif
2539 case QEMU_OPTION_bt:
2540 add_device_config(DEV_BT, optarg);
2541 break;
2542 case QEMU_OPTION_audio_help:
2543 if (!(audio_available())) {
2544 printf("Option %s not supported for this target\n", popt->name);
2545 exit(1);
2547 AUD_help ();
2548 exit (0);
2549 break;
2550 case QEMU_OPTION_soundhw:
2551 if (!(audio_available())) {
2552 printf("Option %s not supported for this target\n", popt->name);
2553 exit(1);
2555 select_soundhw (optarg);
2556 break;
2557 case QEMU_OPTION_h:
2558 help(0);
2559 break;
2560 case QEMU_OPTION_version:
2561 version();
2562 exit(0);
2563 break;
2564 case QEMU_OPTION_m: {
2565 int64_t value;
2566 char *end;
2568 value = strtosz(optarg, &end);
2569 if (value < 0 || *end) {
2570 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2571 exit(1);
2574 if (value != (uint64_t)(ram_addr_t)value) {
2575 fprintf(stderr, "qemu: ram size too large\n");
2576 exit(1);
2578 ram_size = value;
2579 break;
2581 case QEMU_OPTION_mempath:
2582 mem_path = optarg;
2583 break;
2584 #ifdef MAP_POPULATE
2585 case QEMU_OPTION_mem_prealloc:
2586 mem_prealloc = 1;
2587 break;
2588 #endif
2589 case QEMU_OPTION_d:
2590 log_mask = optarg;
2591 break;
2592 case QEMU_OPTION_D:
2593 log_file = optarg;
2594 break;
2595 case QEMU_OPTION_s:
2596 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2597 break;
2598 case QEMU_OPTION_gdb:
2599 gdbstub_dev = optarg;
2600 break;
2601 case QEMU_OPTION_L:
2602 data_dir = optarg;
2603 break;
2604 case QEMU_OPTION_bios:
2605 bios_name = optarg;
2606 break;
2607 case QEMU_OPTION_singlestep:
2608 singlestep = 1;
2609 break;
2610 case QEMU_OPTION_S:
2611 autostart = 0;
2612 break;
2613 case QEMU_OPTION_k:
2614 keyboard_layout = optarg;
2615 break;
2616 case QEMU_OPTION_localtime:
2617 rtc_utc = 0;
2618 break;
2619 case QEMU_OPTION_vga:
2620 vga_model = optarg;
2621 break;
2622 case QEMU_OPTION_g:
2624 const char *p;
2625 int w, h, depth;
2626 p = optarg;
2627 w = strtol(p, (char **)&p, 10);
2628 if (w <= 0) {
2629 graphic_error:
2630 fprintf(stderr, "qemu: invalid resolution or depth\n");
2631 exit(1);
2633 if (*p != 'x')
2634 goto graphic_error;
2635 p++;
2636 h = strtol(p, (char **)&p, 10);
2637 if (h <= 0)
2638 goto graphic_error;
2639 if (*p == 'x') {
2640 p++;
2641 depth = strtol(p, (char **)&p, 10);
2642 if (depth != 8 && depth != 15 && depth != 16 &&
2643 depth != 24 && depth != 32)
2644 goto graphic_error;
2645 } else if (*p == '\0') {
2646 depth = graphic_depth;
2647 } else {
2648 goto graphic_error;
2651 graphic_width = w;
2652 graphic_height = h;
2653 graphic_depth = depth;
2655 break;
2656 case QEMU_OPTION_echr:
2658 char *r;
2659 term_escape_char = strtol(optarg, &r, 0);
2660 if (r == optarg)
2661 printf("Bad argument to echr\n");
2662 break;
2664 case QEMU_OPTION_monitor:
2665 monitor_parse(optarg, "readline");
2666 default_monitor = 0;
2667 break;
2668 case QEMU_OPTION_qmp:
2669 monitor_parse(optarg, "control");
2670 default_monitor = 0;
2671 break;
2672 case QEMU_OPTION_mon:
2673 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2674 if (!opts) {
2675 exit(1);
2677 default_monitor = 0;
2678 break;
2679 case QEMU_OPTION_chardev:
2680 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2681 if (!opts) {
2682 exit(1);
2684 break;
2685 case QEMU_OPTION_fsdev:
2686 olist = qemu_find_opts("fsdev");
2687 if (!olist) {
2688 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2689 exit(1);
2691 opts = qemu_opts_parse(olist, optarg, 1);
2692 if (!opts) {
2693 fprintf(stderr, "parse error: %s\n", optarg);
2694 exit(1);
2696 break;
2697 case QEMU_OPTION_virtfs: {
2698 QemuOpts *fsdev;
2699 QemuOpts *device;
2700 const char *writeout, *sock_fd, *socket;
2702 olist = qemu_find_opts("virtfs");
2703 if (!olist) {
2704 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2705 exit(1);
2707 opts = qemu_opts_parse(olist, optarg, 1);
2708 if (!opts) {
2709 fprintf(stderr, "parse error: %s\n", optarg);
2710 exit(1);
2713 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2714 qemu_opt_get(opts, "mount_tag") == NULL) {
2715 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2716 exit(1);
2718 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2719 qemu_opt_get(opts, "mount_tag"), 1);
2720 if (!fsdev) {
2721 fprintf(stderr, "duplicate fsdev id: %s\n",
2722 qemu_opt_get(opts, "mount_tag"));
2723 exit(1);
2726 writeout = qemu_opt_get(opts, "writeout");
2727 if (writeout) {
2728 #ifdef CONFIG_SYNC_FILE_RANGE
2729 qemu_opt_set(fsdev, "writeout", writeout);
2730 #else
2731 fprintf(stderr, "writeout=immediate not supported on "
2732 "this platform\n");
2733 exit(1);
2734 #endif
2736 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2737 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2738 qemu_opt_set(fsdev, "security_model",
2739 qemu_opt_get(opts, "security_model"));
2740 socket = qemu_opt_get(opts, "socket");
2741 if (socket) {
2742 qemu_opt_set(fsdev, "socket", socket);
2744 sock_fd = qemu_opt_get(opts, "sock_fd");
2745 if (sock_fd) {
2746 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2749 qemu_opt_set_bool(fsdev, "readonly",
2750 qemu_opt_get_bool(opts, "readonly", 0));
2751 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2752 qemu_opt_set(device, "driver", "virtio-9p-pci");
2753 qemu_opt_set(device, "fsdev",
2754 qemu_opt_get(opts, "mount_tag"));
2755 qemu_opt_set(device, "mount_tag",
2756 qemu_opt_get(opts, "mount_tag"));
2757 break;
2759 case QEMU_OPTION_virtfs_synth: {
2760 QemuOpts *fsdev;
2761 QemuOpts *device;
2763 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2764 if (!fsdev) {
2765 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2766 exit(1);
2768 qemu_opt_set(fsdev, "fsdriver", "synth");
2770 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2771 qemu_opt_set(device, "driver", "virtio-9p-pci");
2772 qemu_opt_set(device, "fsdev", "v_synth");
2773 qemu_opt_set(device, "mount_tag", "v_synth");
2774 break;
2776 case QEMU_OPTION_serial:
2777 add_device_config(DEV_SERIAL, optarg);
2778 default_serial = 0;
2779 if (strncmp(optarg, "mon:", 4) == 0) {
2780 default_monitor = 0;
2782 break;
2783 case QEMU_OPTION_watchdog:
2784 if (watchdog) {
2785 fprintf(stderr,
2786 "qemu: only one watchdog option may be given\n");
2787 return 1;
2789 watchdog = optarg;
2790 break;
2791 case QEMU_OPTION_watchdog_action:
2792 if (select_watchdog_action(optarg) == -1) {
2793 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2794 exit(1);
2796 break;
2797 case QEMU_OPTION_virtiocon:
2798 add_device_config(DEV_VIRTCON, optarg);
2799 default_virtcon = 0;
2800 if (strncmp(optarg, "mon:", 4) == 0) {
2801 default_monitor = 0;
2803 break;
2804 case QEMU_OPTION_parallel:
2805 add_device_config(DEV_PARALLEL, optarg);
2806 default_parallel = 0;
2807 if (strncmp(optarg, "mon:", 4) == 0) {
2808 default_monitor = 0;
2810 break;
2811 case QEMU_OPTION_debugcon:
2812 add_device_config(DEV_DEBUGCON, optarg);
2813 break;
2814 case QEMU_OPTION_loadvm:
2815 loadvm = optarg;
2816 break;
2817 case QEMU_OPTION_full_screen:
2818 full_screen = 1;
2819 break;
2820 #ifdef CONFIG_SDL
2821 case QEMU_OPTION_no_frame:
2822 no_frame = 1;
2823 break;
2824 case QEMU_OPTION_alt_grab:
2825 alt_grab = 1;
2826 break;
2827 case QEMU_OPTION_ctrl_grab:
2828 ctrl_grab = 1;
2829 break;
2830 case QEMU_OPTION_no_quit:
2831 no_quit = 1;
2832 break;
2833 case QEMU_OPTION_sdl:
2834 display_type = DT_SDL;
2835 break;
2836 #else
2837 case QEMU_OPTION_no_frame:
2838 case QEMU_OPTION_alt_grab:
2839 case QEMU_OPTION_ctrl_grab:
2840 case QEMU_OPTION_no_quit:
2841 case QEMU_OPTION_sdl:
2842 fprintf(stderr, "SDL support is disabled\n");
2843 exit(1);
2844 #endif
2845 case QEMU_OPTION_pidfile:
2846 pid_file = optarg;
2847 break;
2848 case QEMU_OPTION_win2k_hack:
2849 win2k_install_hack = 1;
2850 break;
2851 case QEMU_OPTION_rtc_td_hack:
2852 rtc_td_hack = 1;
2853 break;
2854 case QEMU_OPTION_acpitable:
2855 do_acpitable_option(optarg);
2856 break;
2857 case QEMU_OPTION_smbios:
2858 do_smbios_option(optarg);
2859 break;
2860 case QEMU_OPTION_enable_kvm:
2861 olist = qemu_find_opts("machine");
2862 qemu_opts_reset(olist);
2863 qemu_opts_parse(olist, "accel=kvm", 0);
2864 break;
2865 case QEMU_OPTION_machine:
2866 olist = qemu_find_opts("machine");
2867 qemu_opts_reset(olist);
2868 opts = qemu_opts_parse(olist, optarg, 1);
2869 if (!opts) {
2870 fprintf(stderr, "parse error: %s\n", optarg);
2871 exit(1);
2873 optarg = qemu_opt_get(opts, "type");
2874 if (optarg) {
2875 machine = machine_parse(optarg);
2877 break;
2878 case QEMU_OPTION_no_kvm:
2879 olist = qemu_find_opts("machine");
2880 qemu_opts_reset(olist);
2881 qemu_opts_parse(olist, "accel=tcg", 0);
2882 break;
2883 #ifdef CONFIG_KVM_OPTIONS
2884 case QEMU_OPTION_no_kvm_irqchip: {
2885 kvm_irqchip = 0;
2886 kvm_pit = 0;
2887 break;
2889 case QEMU_OPTION_no_kvm_pit: {
2890 kvm_pit = 0;
2891 break;
2893 case QEMU_OPTION_no_kvm_pit_reinjection: {
2894 kvm_pit_reinject = 0;
2895 break;
2897 #endif
2898 case QEMU_OPTION_usb:
2899 usb_enabled = 1;
2900 break;
2901 case QEMU_OPTION_usbdevice:
2902 usb_enabled = 1;
2903 add_device_config(DEV_USB, optarg);
2904 break;
2905 case QEMU_OPTION_device:
2906 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2907 exit(1);
2909 break;
2910 case QEMU_OPTION_smp:
2911 smp_parse(optarg);
2912 if (smp_cpus < 1) {
2913 fprintf(stderr, "Invalid number of CPUs\n");
2914 exit(1);
2916 if (max_cpus < smp_cpus) {
2917 fprintf(stderr, "maxcpus must be equal to or greater than "
2918 "smp\n");
2919 exit(1);
2921 if (max_cpus > 255) {
2922 fprintf(stderr, "Unsupported number of maxcpus\n");
2923 exit(1);
2925 break;
2926 case QEMU_OPTION_vnc:
2927 #ifdef CONFIG_VNC
2928 display_remote++;
2929 vnc_display = optarg;
2930 #else
2931 fprintf(stderr, "VNC support is disabled\n");
2932 exit(1);
2933 #endif
2934 break;
2935 case QEMU_OPTION_no_acpi:
2936 acpi_enabled = 0;
2937 break;
2938 case QEMU_OPTION_no_hpet:
2939 no_hpet = 1;
2940 break;
2941 case QEMU_OPTION_balloon:
2942 if (balloon_parse(optarg) < 0) {
2943 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2944 exit(1);
2946 break;
2947 case QEMU_OPTION_no_reboot:
2948 no_reboot = 1;
2949 break;
2950 case QEMU_OPTION_no_shutdown:
2951 no_shutdown = 1;
2952 break;
2953 case QEMU_OPTION_show_cursor:
2954 cursor_hide = 0;
2955 break;
2956 case QEMU_OPTION_uuid:
2957 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2958 fprintf(stderr, "Fail to parse UUID string."
2959 " Wrong format.\n");
2960 exit(1);
2962 break;
2963 case QEMU_OPTION_option_rom:
2964 if (nb_option_roms >= MAX_OPTION_ROMS) {
2965 fprintf(stderr, "Too many option ROMs\n");
2966 exit(1);
2968 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2969 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2970 option_rom[nb_option_roms].bootindex =
2971 qemu_opt_get_number(opts, "bootindex", -1);
2972 if (!option_rom[nb_option_roms].name) {
2973 fprintf(stderr, "Option ROM file is not specified\n");
2974 exit(1);
2976 nb_option_roms++;
2977 break;
2978 case QEMU_OPTION_semihosting:
2979 semihosting_enabled = 1;
2980 break;
2981 case QEMU_OPTION_tdf:
2982 fprintf(stderr, "Warning: user space PIT time drift fix "
2983 "is no longer supported.\n");
2984 break;
2985 case QEMU_OPTION_kvm_shadow_memory:
2986 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2987 break;
2988 case QEMU_OPTION_name:
2989 qemu_name = g_strdup(optarg);
2991 char *p = strchr(qemu_name, ',');
2992 if (p != NULL) {
2993 *p++ = 0;
2994 if (strncmp(p, "process=", 8)) {
2995 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2996 exit(1);
2998 p += 8;
2999 os_set_proc_name(p);
3002 break;
3003 case QEMU_OPTION_prom_env:
3004 if (nb_prom_envs >= MAX_PROM_ENVS) {
3005 fprintf(stderr, "Too many prom variables\n");
3006 exit(1);
3008 prom_envs[nb_prom_envs] = optarg;
3009 nb_prom_envs++;
3010 break;
3011 case QEMU_OPTION_old_param:
3012 old_param = 1;
3013 break;
3014 case QEMU_OPTION_clock:
3015 configure_alarms(optarg);
3016 break;
3017 case QEMU_OPTION_startdate:
3018 configure_rtc_date_offset(optarg, 1);
3019 break;
3020 case QEMU_OPTION_rtc:
3021 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3022 if (!opts) {
3023 exit(1);
3025 configure_rtc(opts);
3026 break;
3027 case QEMU_OPTION_tb_size:
3028 tcg_tb_size = strtol(optarg, NULL, 0);
3029 if (tcg_tb_size < 0) {
3030 tcg_tb_size = 0;
3032 break;
3033 case QEMU_OPTION_icount:
3034 icount_option = optarg;
3035 break;
3036 case QEMU_OPTION_incoming:
3037 incoming = optarg;
3038 break;
3039 case QEMU_OPTION_nodefaults:
3040 default_serial = 0;
3041 default_parallel = 0;
3042 default_virtcon = 0;
3043 default_monitor = 0;
3044 default_net = 0;
3045 default_floppy = 0;
3046 default_cdrom = 0;
3047 default_sdcard = 0;
3048 vga_model = "none";
3049 break;
3050 case QEMU_OPTION_xen_domid:
3051 if (!(xen_available())) {
3052 printf("Option %s not supported for this target\n", popt->name);
3053 exit(1);
3055 xen_domid = atoi(optarg);
3056 break;
3057 case QEMU_OPTION_xen_create:
3058 if (!(xen_available())) {
3059 printf("Option %s not supported for this target\n", popt->name);
3060 exit(1);
3062 xen_mode = XEN_CREATE;
3063 break;
3064 case QEMU_OPTION_xen_attach:
3065 if (!(xen_available())) {
3066 printf("Option %s not supported for this target\n", popt->name);
3067 exit(1);
3069 xen_mode = XEN_ATTACH;
3070 break;
3071 case QEMU_OPTION_trace:
3073 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3074 if (!opts) {
3075 exit(1);
3077 trace_events = qemu_opt_get(opts, "events");
3078 trace_file = qemu_opt_get(opts, "file");
3079 break;
3081 case QEMU_OPTION_readconfig:
3083 int ret = qemu_read_config_file(optarg);
3084 if (ret < 0) {
3085 fprintf(stderr, "read config %s: %s\n", optarg,
3086 strerror(-ret));
3087 exit(1);
3089 break;
3091 case QEMU_OPTION_spice:
3092 olist = qemu_find_opts("spice");
3093 if (!olist) {
3094 fprintf(stderr, "spice is not supported by this qemu build.\n");
3095 exit(1);
3097 opts = qemu_opts_parse(olist, optarg, 0);
3098 if (!opts) {
3099 fprintf(stderr, "parse error: %s\n", optarg);
3100 exit(1);
3102 break;
3103 case QEMU_OPTION_writeconfig:
3105 FILE *fp;
3106 if (strcmp(optarg, "-") == 0) {
3107 fp = stdout;
3108 } else {
3109 fp = fopen(optarg, "w");
3110 if (fp == NULL) {
3111 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3112 exit(1);
3115 qemu_config_write(fp);
3116 fclose(fp);
3117 break;
3119 default:
3120 os_parse_cmd_args(popt->index, optarg);
3124 loc_set_none();
3126 /* Open the logfile at this point, if necessary. We can't open the logfile
3127 * when encountering either of the logging options (-d or -D) because the
3128 * other one may be encountered later on the command line, changing the
3129 * location or level of logging.
3131 if (log_mask) {
3132 if (log_file) {
3133 set_cpu_log_filename(log_file);
3135 set_cpu_log(log_mask);
3138 if (!trace_backend_init(trace_events, trace_file)) {
3139 exit(1);
3142 /* If no data_dir is specified then try to find it relative to the
3143 executable path. */
3144 if (!data_dir) {
3145 data_dir = os_find_datadir(argv[0]);
3147 /* If all else fails use the install path specified when building. */
3148 if (!data_dir) {
3149 data_dir = CONFIG_QEMU_DATADIR;
3152 if (machine == NULL) {
3153 fprintf(stderr, "No machine found.\n");
3154 exit(1);
3158 * Default to max_cpus = smp_cpus, in case the user doesn't
3159 * specify a max_cpus value.
3161 if (!max_cpus)
3162 max_cpus = smp_cpus;
3164 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3165 if (smp_cpus > machine->max_cpus) {
3166 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3167 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3168 machine->max_cpus);
3169 exit(1);
3173 * Get the default machine options from the machine if it is not already
3174 * specified either by the configuration file or by the command line.
3176 if (machine->default_machine_opts) {
3177 QemuOptsList *list = qemu_find_opts("machine");
3178 const char *p = NULL;
3180 if (!QTAILQ_EMPTY(&list->head)) {
3181 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3183 if (p == NULL) {
3184 qemu_opts_reset(list);
3185 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3186 if (!opts) {
3187 fprintf(stderr, "parse error for machine %s: %s\n",
3188 machine->name, machine->default_machine_opts);
3189 exit(1);
3194 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3195 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3197 if (machine->no_serial) {
3198 default_serial = 0;
3200 if (machine->no_parallel) {
3201 default_parallel = 0;
3203 if (!machine->use_virtcon) {
3204 default_virtcon = 0;
3206 if (machine->no_floppy) {
3207 default_floppy = 0;
3209 if (machine->no_cdrom) {
3210 default_cdrom = 0;
3212 if (machine->no_sdcard) {
3213 default_sdcard = 0;
3216 if (display_type == DT_NOGRAPHIC) {
3217 if (default_parallel)
3218 add_device_config(DEV_PARALLEL, "null");
3219 if (default_serial && default_monitor) {
3220 add_device_config(DEV_SERIAL, "mon:stdio");
3221 } else if (default_virtcon && default_monitor) {
3222 add_device_config(DEV_VIRTCON, "mon:stdio");
3223 } else {
3224 if (default_serial)
3225 add_device_config(DEV_SERIAL, "stdio");
3226 if (default_virtcon)
3227 add_device_config(DEV_VIRTCON, "stdio");
3228 if (default_monitor)
3229 monitor_parse("stdio", "readline");
3231 } else {
3232 if (default_serial)
3233 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3234 if (default_parallel)
3235 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3236 if (default_monitor)
3237 monitor_parse("vc:80Cx24C", "readline");
3238 if (default_virtcon)
3239 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3242 socket_init();
3244 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3245 exit(1);
3246 #ifdef CONFIG_VIRTFS
3247 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3248 exit(1);
3250 #endif
3252 os_daemonize();
3254 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3255 os_pidfile_error();
3256 exit(1);
3259 /* init the memory */
3260 if (ram_size == 0) {
3261 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3264 configure_accelerator();
3266 qemu_init_cpu_loop();
3267 if (qemu_init_main_loop()) {
3268 fprintf(stderr, "qemu_init_main_loop failed\n");
3269 exit(1);
3271 linux_boot = (kernel_filename != NULL);
3273 if (!linux_boot && *kernel_cmdline != '\0') {
3274 fprintf(stderr, "-append only allowed with -kernel option\n");
3275 exit(1);
3278 if (!linux_boot && initrd_filename != NULL) {
3279 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3280 exit(1);
3283 os_set_line_buffering();
3285 if (init_timer_alarm() < 0) {
3286 fprintf(stderr, "could not initialize alarm timer\n");
3287 exit(1);
3290 if (icount_option && (kvm_enabled() || xen_enabled())) {
3291 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3292 exit(1);
3294 configure_icount(icount_option);
3296 if (net_init_clients() < 0) {
3297 exit(1);
3300 /* init the bluetooth world */
3301 if (foreach_device_config(DEV_BT, bt_parse))
3302 exit(1);
3304 if (!xen_enabled()) {
3305 /* On 32-bit hosts, QEMU is limited by virtual address space */
3306 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3307 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3308 exit(1);
3312 cpu_exec_init_all();
3314 bdrv_init_with_whitelist();
3316 blk_mig_init();
3318 /* open the virtual block devices */
3319 if (snapshot)
3320 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3321 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3322 exit(1);
3324 default_drive(default_cdrom, snapshot, machine->use_scsi,
3325 IF_DEFAULT, 2, CDROM_OPTS);
3326 default_drive(default_floppy, snapshot, machine->use_scsi,
3327 IF_FLOPPY, 0, FD_OPTS);
3328 default_drive(default_sdcard, snapshot, machine->use_scsi,
3329 IF_SD, 0, SD_OPTS);
3331 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3332 ram_load, NULL);
3334 if (nb_numa_nodes > 0) {
3335 int i;
3337 if (nb_numa_nodes > MAX_NODES) {
3338 nb_numa_nodes = MAX_NODES;
3341 /* If no memory size if given for any node, assume the default case
3342 * and distribute the available memory equally across all nodes
3344 for (i = 0; i < nb_numa_nodes; i++) {
3345 if (node_mem[i] != 0)
3346 break;
3348 if (i == nb_numa_nodes) {
3349 uint64_t usedmem = 0;
3351 /* On Linux, the each node's border has to be 8MB aligned,
3352 * the final node gets the rest.
3354 for (i = 0; i < nb_numa_nodes - 1; i++) {
3355 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3356 usedmem += node_mem[i];
3358 node_mem[i] = ram_size - usedmem;
3361 for (i = 0; i < nb_numa_nodes; i++) {
3362 if (node_cpumask[i] != 0)
3363 break;
3365 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3366 * must cope with this anyway, because there are BIOSes out there in
3367 * real machines which also use this scheme.
3369 if (i == nb_numa_nodes) {
3370 for (i = 0; i < max_cpus; i++) {
3371 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3376 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3377 exit(1);
3380 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3381 exit(1);
3382 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3383 exit(1);
3384 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3385 exit(1);
3386 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3387 exit(1);
3389 module_call_init(MODULE_INIT_DEVICE);
3391 /* must be after qdev registration but before machine init */
3392 if (vga_model) {
3393 select_vgahw(vga_model);
3394 } else if (cirrus_vga_available()) {
3395 select_vgahw("cirrus");
3396 } else {
3397 select_vgahw("none");
3400 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3401 exit(0);
3403 if (watchdog) {
3404 i = select_watchdog(watchdog);
3405 if (i > 0)
3406 exit (i == 1 ? 1 : 0);
3409 if (machine->compat_props) {
3410 qdev_prop_register_global_list(machine->compat_props);
3412 qemu_add_globals();
3414 qdev_machine_init();
3416 machine->init(ram_size, boot_devices,
3417 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3419 cpu_synchronize_all_post_init();
3421 set_numa_modes();
3423 current_machine = machine;
3425 /* init USB devices */
3426 if (usb_enabled) {
3427 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3428 exit(1);
3431 /* init generic devices */
3432 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3433 exit(1);
3435 net_check_clients();
3437 /* just use the first displaystate for the moment */
3438 ds = get_displaystate();
3440 if (using_spice)
3441 display_remote++;
3442 if (display_type == DT_DEFAULT && !display_remote) {
3443 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3444 display_type = DT_SDL;
3445 #elif defined(CONFIG_VNC)
3446 vnc_display = "localhost:0,to=99";
3447 show_vnc_port = 1;
3448 #else
3449 display_type = DT_NONE;
3450 #endif
3454 /* init local displays */
3455 switch (display_type) {
3456 case DT_NOGRAPHIC:
3457 break;
3458 #if defined(CONFIG_CURSES)
3459 case DT_CURSES:
3460 curses_display_init(ds, full_screen);
3461 break;
3462 #endif
3463 #if defined(CONFIG_SDL)
3464 case DT_SDL:
3465 sdl_display_init(ds, full_screen, no_frame);
3466 break;
3467 #elif defined(CONFIG_COCOA)
3468 case DT_SDL:
3469 cocoa_display_init(ds, full_screen);
3470 break;
3471 #endif
3472 default:
3473 break;
3476 /* must be after terminal init, SDL library changes signal handlers */
3477 os_setup_signal_handling();
3479 #ifdef CONFIG_VNC
3480 /* init remote displays */
3481 if (vnc_display) {
3482 vnc_display_init(ds);
3483 if (vnc_display_open(ds, vnc_display) < 0)
3484 exit(1);
3486 if (show_vnc_port) {
3487 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3490 #endif
3491 #ifdef CONFIG_SPICE
3492 if (using_spice && !qxl_enabled) {
3493 qemu_spice_display_init(ds);
3495 #endif
3497 /* display setup */
3498 dpy_resize(ds);
3499 dcl = ds->listeners;
3500 while (dcl != NULL) {
3501 if (dcl->dpy_refresh != NULL) {
3502 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3503 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3504 break;
3506 dcl = dcl->next;
3508 text_consoles_set_display(ds);
3510 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3511 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3512 gdbstub_dev);
3513 exit(1);
3516 qdev_machine_creation_done();
3518 if (rom_load_all() != 0) {
3519 fprintf(stderr, "rom loading failed\n");
3520 exit(1);
3523 /* TODO: once all bus devices are qdevified, this should be done
3524 * when bus is created by qdev.c */
3525 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3526 qemu_run_machine_init_done_notifiers();
3528 qemu_system_reset(VMRESET_SILENT);
3529 if (loadvm) {
3530 if (load_vmstate(loadvm) < 0) {
3531 autostart = 0;
3535 if (incoming) {
3536 runstate_set(RUN_STATE_INMIGRATE);
3537 int ret = qemu_start_incoming_migration(incoming);
3538 if (ret < 0) {
3539 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3540 incoming, ret);
3541 exit(ret);
3543 } else if (autostart) {
3544 vm_start();
3547 os_setup_post();
3549 resume_all_vcpus();
3550 main_loop();
3551 bdrv_close_all();
3552 pause_all_vcpus();
3553 net_cleanup();
3554 res_free();
3556 return 0;