qemu-kvm: Drop broken --no-cpu-emulation
[qemu-kvm.git] / vl.c
blob49a4c976ffe20d46aa4ce31e76858795012d8bda
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_reinject = 1;
2171 #endif
2173 int main(int argc, char **argv, char **envp)
2175 const char *gdbstub_dev = NULL;
2176 int i;
2177 int snapshot, linux_boot;
2178 const char *icount_option = NULL;
2179 const char *initrd_filename;
2180 const char *kernel_filename, *kernel_cmdline;
2181 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2182 DisplayState *ds;
2183 DisplayChangeListener *dcl;
2184 int cyls, heads, secs, translation;
2185 QemuOpts *hda_opts = NULL, *opts;
2186 QemuOptsList *olist;
2187 int optind;
2188 const char *optarg;
2189 const char *loadvm = NULL;
2190 QEMUMachine *machine;
2191 const char *cpu_model;
2192 const char *vga_model = NULL;
2193 const char *pid_file = NULL;
2194 const char *incoming = NULL;
2195 #ifdef CONFIG_VNC
2196 int show_vnc_port = 0;
2197 #endif
2198 int defconfig = 1;
2199 const char *log_mask = NULL;
2200 const char *log_file = NULL;
2201 GMemVTable mem_trace = {
2202 .malloc = malloc_and_trace,
2203 .realloc = realloc_and_trace,
2204 .free = free_and_trace,
2206 const char *trace_events = NULL;
2207 const char *trace_file = NULL;
2209 atexit(qemu_run_exit_notifiers);
2210 error_set_progname(argv[0]);
2212 g_mem_set_vtable(&mem_trace);
2213 if (!g_thread_supported()) {
2214 #if !GLIB_CHECK_VERSION(2, 31, 0)
2215 g_thread_init(NULL);
2216 #else
2217 fprintf(stderr, "glib threading failed to initialize.\n");
2218 exit(1);
2219 #endif
2222 runstate_init();
2224 init_clocks();
2225 rtc_clock = host_clock;
2227 qemu_cache_utils_init(envp);
2229 QLIST_INIT (&vm_change_state_head);
2230 os_setup_early_signal_handling();
2232 module_call_init(MODULE_INIT_MACHINE);
2233 machine = find_default_machine();
2234 cpu_model = NULL;
2235 initrd_filename = NULL;
2236 ram_size = 0;
2237 snapshot = 0;
2238 kernel_filename = NULL;
2239 kernel_cmdline = "";
2240 cyls = heads = secs = 0;
2241 translation = BIOS_ATA_TRANSLATION_AUTO;
2243 for (i = 0; i < MAX_NODES; i++) {
2244 node_mem[i] = 0;
2245 node_cpumask[i] = 0;
2248 nb_numa_nodes = 0;
2249 nb_nics = 0;
2251 autostart= 1;
2253 /* first pass of option parsing */
2254 optind = 1;
2255 while (optind < argc) {
2256 if (argv[optind][0] != '-') {
2257 /* disk image */
2258 optind++;
2259 continue;
2260 } else {
2261 const QEMUOption *popt;
2263 popt = lookup_opt(argc, argv, &optarg, &optind);
2264 switch (popt->index) {
2265 case QEMU_OPTION_nodefconfig:
2266 defconfig=0;
2267 break;
2272 if (defconfig) {
2273 int ret;
2275 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2276 if (ret < 0 && ret != -ENOENT) {
2277 exit(1);
2280 ret = qemu_read_config_file(arch_config_name);
2281 if (ret < 0 && ret != -ENOENT) {
2282 exit(1);
2285 cpudef_init();
2287 /* second pass of option parsing */
2288 optind = 1;
2289 for(;;) {
2290 if (optind >= argc)
2291 break;
2292 if (argv[optind][0] != '-') {
2293 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2294 } else {
2295 const QEMUOption *popt;
2297 popt = lookup_opt(argc, argv, &optarg, &optind);
2298 if (!(popt->arch_mask & arch_type)) {
2299 printf("Option %s not supported for this target\n", popt->name);
2300 exit(1);
2302 switch(popt->index) {
2303 case QEMU_OPTION_M:
2304 machine = machine_parse(optarg);
2305 break;
2306 case QEMU_OPTION_cpu:
2307 /* hw initialization will check this */
2308 if (*optarg == '?') {
2309 list_cpus(stdout, &fprintf, optarg);
2310 exit(0);
2311 } else {
2312 cpu_model = optarg;
2314 break;
2315 case QEMU_OPTION_initrd:
2316 initrd_filename = optarg;
2317 break;
2318 case QEMU_OPTION_hda:
2320 char buf[256];
2321 if (cyls == 0)
2322 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2323 else
2324 snprintf(buf, sizeof(buf),
2325 "%s,cyls=%d,heads=%d,secs=%d%s",
2326 HD_OPTS , cyls, heads, secs,
2327 translation == BIOS_ATA_TRANSLATION_LBA ?
2328 ",trans=lba" :
2329 translation == BIOS_ATA_TRANSLATION_NONE ?
2330 ",trans=none" : "");
2331 drive_add(IF_DEFAULT, 0, optarg, buf);
2332 break;
2334 case QEMU_OPTION_hdb:
2335 case QEMU_OPTION_hdc:
2336 case QEMU_OPTION_hdd:
2337 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2338 HD_OPTS);
2339 break;
2340 case QEMU_OPTION_drive:
2341 if (drive_def(optarg) == NULL) {
2342 exit(1);
2344 break;
2345 case QEMU_OPTION_set:
2346 if (qemu_set_option(optarg) != 0)
2347 exit(1);
2348 break;
2349 case QEMU_OPTION_global:
2350 if (qemu_global_option(optarg) != 0)
2351 exit(1);
2352 break;
2353 case QEMU_OPTION_mtdblock:
2354 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2355 break;
2356 case QEMU_OPTION_sd:
2357 drive_add(IF_SD, 0, optarg, SD_OPTS);
2358 break;
2359 case QEMU_OPTION_pflash:
2360 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2361 break;
2362 case QEMU_OPTION_snapshot:
2363 snapshot = 1;
2364 break;
2365 case QEMU_OPTION_hdachs:
2367 const char *p;
2368 p = optarg;
2369 cyls = strtol(p, (char **)&p, 0);
2370 if (cyls < 1 || cyls > 16383)
2371 goto chs_fail;
2372 if (*p != ',')
2373 goto chs_fail;
2374 p++;
2375 heads = strtol(p, (char **)&p, 0);
2376 if (heads < 1 || heads > 16)
2377 goto chs_fail;
2378 if (*p != ',')
2379 goto chs_fail;
2380 p++;
2381 secs = strtol(p, (char **)&p, 0);
2382 if (secs < 1 || secs > 63)
2383 goto chs_fail;
2384 if (*p == ',') {
2385 p++;
2386 if (!strcmp(p, "none"))
2387 translation = BIOS_ATA_TRANSLATION_NONE;
2388 else if (!strcmp(p, "lba"))
2389 translation = BIOS_ATA_TRANSLATION_LBA;
2390 else if (!strcmp(p, "auto"))
2391 translation = BIOS_ATA_TRANSLATION_AUTO;
2392 else
2393 goto chs_fail;
2394 } else if (*p != '\0') {
2395 chs_fail:
2396 fprintf(stderr, "qemu: invalid physical CHS format\n");
2397 exit(1);
2399 if (hda_opts != NULL) {
2400 char num[16];
2401 snprintf(num, sizeof(num), "%d", cyls);
2402 qemu_opt_set(hda_opts, "cyls", num);
2403 snprintf(num, sizeof(num), "%d", heads);
2404 qemu_opt_set(hda_opts, "heads", num);
2405 snprintf(num, sizeof(num), "%d", secs);
2406 qemu_opt_set(hda_opts, "secs", num);
2407 if (translation == BIOS_ATA_TRANSLATION_LBA)
2408 qemu_opt_set(hda_opts, "trans", "lba");
2409 if (translation == BIOS_ATA_TRANSLATION_NONE)
2410 qemu_opt_set(hda_opts, "trans", "none");
2413 break;
2414 case QEMU_OPTION_numa:
2415 if (nb_numa_nodes >= MAX_NODES) {
2416 fprintf(stderr, "qemu: too many NUMA nodes\n");
2417 exit(1);
2419 numa_add(optarg);
2420 break;
2421 case QEMU_OPTION_display:
2422 display_type = select_display(optarg);
2423 break;
2424 case QEMU_OPTION_nographic:
2425 display_type = DT_NOGRAPHIC;
2426 break;
2427 case QEMU_OPTION_curses:
2428 #ifdef CONFIG_CURSES
2429 display_type = DT_CURSES;
2430 #else
2431 fprintf(stderr, "Curses support is disabled\n");
2432 exit(1);
2433 #endif
2434 break;
2435 case QEMU_OPTION_portrait:
2436 graphic_rotate = 90;
2437 break;
2438 case QEMU_OPTION_rotate:
2439 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2440 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2441 graphic_rotate != 180 && graphic_rotate != 270) {
2442 fprintf(stderr,
2443 "qemu: only 90, 180, 270 deg rotation is available\n");
2444 exit(1);
2446 break;
2447 case QEMU_OPTION_kernel:
2448 kernel_filename = optarg;
2449 break;
2450 case QEMU_OPTION_append:
2451 kernel_cmdline = optarg;
2452 break;
2453 case QEMU_OPTION_cdrom:
2454 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2455 break;
2456 case QEMU_OPTION_boot:
2458 static const char * const params[] = {
2459 "order", "once", "menu",
2460 "splash", "splash-time", NULL
2462 char buf[sizeof(boot_devices)];
2463 char *standard_boot_devices;
2464 int legacy = 0;
2466 if (!strchr(optarg, '=')) {
2467 legacy = 1;
2468 pstrcpy(buf, sizeof(buf), optarg);
2469 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2470 fprintf(stderr,
2471 "qemu: unknown boot parameter '%s' in '%s'\n",
2472 buf, optarg);
2473 exit(1);
2476 if (legacy ||
2477 get_param_value(buf, sizeof(buf), "order", optarg)) {
2478 validate_bootdevices(buf);
2479 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2481 if (!legacy) {
2482 if (get_param_value(buf, sizeof(buf),
2483 "once", optarg)) {
2484 validate_bootdevices(buf);
2485 standard_boot_devices = g_strdup(boot_devices);
2486 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2487 qemu_register_reset(restore_boot_devices,
2488 standard_boot_devices);
2490 if (get_param_value(buf, sizeof(buf),
2491 "menu", optarg)) {
2492 if (!strcmp(buf, "on")) {
2493 boot_menu = 1;
2494 } else if (!strcmp(buf, "off")) {
2495 boot_menu = 0;
2496 } else {
2497 fprintf(stderr,
2498 "qemu: invalid option value '%s'\n",
2499 buf);
2500 exit(1);
2503 qemu_opts_parse(qemu_find_opts("boot-opts"),
2504 optarg, 0);
2507 break;
2508 case QEMU_OPTION_fda:
2509 case QEMU_OPTION_fdb:
2510 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2511 optarg, FD_OPTS);
2512 break;
2513 case QEMU_OPTION_no_fd_bootchk:
2514 fd_bootchk = 0;
2515 break;
2516 case QEMU_OPTION_netdev:
2517 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2518 exit(1);
2520 break;
2521 case QEMU_OPTION_net:
2522 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2523 exit(1);
2525 break;
2526 #ifdef CONFIG_SLIRP
2527 case QEMU_OPTION_tftp:
2528 legacy_tftp_prefix = optarg;
2529 break;
2530 case QEMU_OPTION_bootp:
2531 legacy_bootp_filename = optarg;
2532 break;
2533 case QEMU_OPTION_redir:
2534 if (net_slirp_redir(optarg) < 0)
2535 exit(1);
2536 break;
2537 #endif
2538 case QEMU_OPTION_bt:
2539 add_device_config(DEV_BT, optarg);
2540 break;
2541 case QEMU_OPTION_audio_help:
2542 if (!(audio_available())) {
2543 printf("Option %s not supported for this target\n", popt->name);
2544 exit(1);
2546 AUD_help ();
2547 exit (0);
2548 break;
2549 case QEMU_OPTION_soundhw:
2550 if (!(audio_available())) {
2551 printf("Option %s not supported for this target\n", popt->name);
2552 exit(1);
2554 select_soundhw (optarg);
2555 break;
2556 case QEMU_OPTION_h:
2557 help(0);
2558 break;
2559 case QEMU_OPTION_version:
2560 version();
2561 exit(0);
2562 break;
2563 case QEMU_OPTION_m: {
2564 int64_t value;
2565 char *end;
2567 value = strtosz(optarg, &end);
2568 if (value < 0 || *end) {
2569 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2570 exit(1);
2573 if (value != (uint64_t)(ram_addr_t)value) {
2574 fprintf(stderr, "qemu: ram size too large\n");
2575 exit(1);
2577 ram_size = value;
2578 break;
2580 case QEMU_OPTION_mempath:
2581 mem_path = optarg;
2582 break;
2583 #ifdef MAP_POPULATE
2584 case QEMU_OPTION_mem_prealloc:
2585 mem_prealloc = 1;
2586 break;
2587 #endif
2588 case QEMU_OPTION_d:
2589 log_mask = optarg;
2590 break;
2591 case QEMU_OPTION_D:
2592 log_file = optarg;
2593 break;
2594 case QEMU_OPTION_s:
2595 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2596 break;
2597 case QEMU_OPTION_gdb:
2598 gdbstub_dev = optarg;
2599 break;
2600 case QEMU_OPTION_L:
2601 data_dir = optarg;
2602 break;
2603 case QEMU_OPTION_bios:
2604 bios_name = optarg;
2605 break;
2606 case QEMU_OPTION_singlestep:
2607 singlestep = 1;
2608 break;
2609 case QEMU_OPTION_S:
2610 autostart = 0;
2611 break;
2612 case QEMU_OPTION_k:
2613 keyboard_layout = optarg;
2614 break;
2615 case QEMU_OPTION_localtime:
2616 rtc_utc = 0;
2617 break;
2618 case QEMU_OPTION_vga:
2619 vga_model = optarg;
2620 break;
2621 case QEMU_OPTION_g:
2623 const char *p;
2624 int w, h, depth;
2625 p = optarg;
2626 w = strtol(p, (char **)&p, 10);
2627 if (w <= 0) {
2628 graphic_error:
2629 fprintf(stderr, "qemu: invalid resolution or depth\n");
2630 exit(1);
2632 if (*p != 'x')
2633 goto graphic_error;
2634 p++;
2635 h = strtol(p, (char **)&p, 10);
2636 if (h <= 0)
2637 goto graphic_error;
2638 if (*p == 'x') {
2639 p++;
2640 depth = strtol(p, (char **)&p, 10);
2641 if (depth != 8 && depth != 15 && depth != 16 &&
2642 depth != 24 && depth != 32)
2643 goto graphic_error;
2644 } else if (*p == '\0') {
2645 depth = graphic_depth;
2646 } else {
2647 goto graphic_error;
2650 graphic_width = w;
2651 graphic_height = h;
2652 graphic_depth = depth;
2654 break;
2655 case QEMU_OPTION_echr:
2657 char *r;
2658 term_escape_char = strtol(optarg, &r, 0);
2659 if (r == optarg)
2660 printf("Bad argument to echr\n");
2661 break;
2663 case QEMU_OPTION_monitor:
2664 monitor_parse(optarg, "readline");
2665 default_monitor = 0;
2666 break;
2667 case QEMU_OPTION_qmp:
2668 monitor_parse(optarg, "control");
2669 default_monitor = 0;
2670 break;
2671 case QEMU_OPTION_mon:
2672 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2673 if (!opts) {
2674 exit(1);
2676 default_monitor = 0;
2677 break;
2678 case QEMU_OPTION_chardev:
2679 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2680 if (!opts) {
2681 exit(1);
2683 break;
2684 case QEMU_OPTION_fsdev:
2685 olist = qemu_find_opts("fsdev");
2686 if (!olist) {
2687 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2688 exit(1);
2690 opts = qemu_opts_parse(olist, optarg, 1);
2691 if (!opts) {
2692 fprintf(stderr, "parse error: %s\n", optarg);
2693 exit(1);
2695 break;
2696 case QEMU_OPTION_virtfs: {
2697 QemuOpts *fsdev;
2698 QemuOpts *device;
2699 const char *writeout, *sock_fd, *socket;
2701 olist = qemu_find_opts("virtfs");
2702 if (!olist) {
2703 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2704 exit(1);
2706 opts = qemu_opts_parse(olist, optarg, 1);
2707 if (!opts) {
2708 fprintf(stderr, "parse error: %s\n", optarg);
2709 exit(1);
2712 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2713 qemu_opt_get(opts, "mount_tag") == NULL) {
2714 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2715 exit(1);
2717 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2718 qemu_opt_get(opts, "mount_tag"), 1);
2719 if (!fsdev) {
2720 fprintf(stderr, "duplicate fsdev id: %s\n",
2721 qemu_opt_get(opts, "mount_tag"));
2722 exit(1);
2725 writeout = qemu_opt_get(opts, "writeout");
2726 if (writeout) {
2727 #ifdef CONFIG_SYNC_FILE_RANGE
2728 qemu_opt_set(fsdev, "writeout", writeout);
2729 #else
2730 fprintf(stderr, "writeout=immediate not supported on "
2731 "this platform\n");
2732 exit(1);
2733 #endif
2735 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2736 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2737 qemu_opt_set(fsdev, "security_model",
2738 qemu_opt_get(opts, "security_model"));
2739 socket = qemu_opt_get(opts, "socket");
2740 if (socket) {
2741 qemu_opt_set(fsdev, "socket", socket);
2743 sock_fd = qemu_opt_get(opts, "sock_fd");
2744 if (sock_fd) {
2745 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2748 qemu_opt_set_bool(fsdev, "readonly",
2749 qemu_opt_get_bool(opts, "readonly", 0));
2750 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2751 qemu_opt_set(device, "driver", "virtio-9p-pci");
2752 qemu_opt_set(device, "fsdev",
2753 qemu_opt_get(opts, "mount_tag"));
2754 qemu_opt_set(device, "mount_tag",
2755 qemu_opt_get(opts, "mount_tag"));
2756 break;
2758 case QEMU_OPTION_virtfs_synth: {
2759 QemuOpts *fsdev;
2760 QemuOpts *device;
2762 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2763 if (!fsdev) {
2764 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2765 exit(1);
2767 qemu_opt_set(fsdev, "fsdriver", "synth");
2769 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2770 qemu_opt_set(device, "driver", "virtio-9p-pci");
2771 qemu_opt_set(device, "fsdev", "v_synth");
2772 qemu_opt_set(device, "mount_tag", "v_synth");
2773 break;
2775 case QEMU_OPTION_serial:
2776 add_device_config(DEV_SERIAL, optarg);
2777 default_serial = 0;
2778 if (strncmp(optarg, "mon:", 4) == 0) {
2779 default_monitor = 0;
2781 break;
2782 case QEMU_OPTION_watchdog:
2783 if (watchdog) {
2784 fprintf(stderr,
2785 "qemu: only one watchdog option may be given\n");
2786 return 1;
2788 watchdog = optarg;
2789 break;
2790 case QEMU_OPTION_watchdog_action:
2791 if (select_watchdog_action(optarg) == -1) {
2792 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2793 exit(1);
2795 break;
2796 case QEMU_OPTION_virtiocon:
2797 add_device_config(DEV_VIRTCON, optarg);
2798 default_virtcon = 0;
2799 if (strncmp(optarg, "mon:", 4) == 0) {
2800 default_monitor = 0;
2802 break;
2803 case QEMU_OPTION_parallel:
2804 add_device_config(DEV_PARALLEL, optarg);
2805 default_parallel = 0;
2806 if (strncmp(optarg, "mon:", 4) == 0) {
2807 default_monitor = 0;
2809 break;
2810 case QEMU_OPTION_debugcon:
2811 add_device_config(DEV_DEBUGCON, optarg);
2812 break;
2813 case QEMU_OPTION_loadvm:
2814 loadvm = optarg;
2815 break;
2816 case QEMU_OPTION_full_screen:
2817 full_screen = 1;
2818 break;
2819 #ifdef CONFIG_SDL
2820 case QEMU_OPTION_no_frame:
2821 no_frame = 1;
2822 break;
2823 case QEMU_OPTION_alt_grab:
2824 alt_grab = 1;
2825 break;
2826 case QEMU_OPTION_ctrl_grab:
2827 ctrl_grab = 1;
2828 break;
2829 case QEMU_OPTION_no_quit:
2830 no_quit = 1;
2831 break;
2832 case QEMU_OPTION_sdl:
2833 display_type = DT_SDL;
2834 break;
2835 #else
2836 case QEMU_OPTION_no_frame:
2837 case QEMU_OPTION_alt_grab:
2838 case QEMU_OPTION_ctrl_grab:
2839 case QEMU_OPTION_no_quit:
2840 case QEMU_OPTION_sdl:
2841 fprintf(stderr, "SDL support is disabled\n");
2842 exit(1);
2843 #endif
2844 case QEMU_OPTION_pidfile:
2845 pid_file = optarg;
2846 break;
2847 case QEMU_OPTION_win2k_hack:
2848 win2k_install_hack = 1;
2849 break;
2850 case QEMU_OPTION_rtc_td_hack:
2851 rtc_td_hack = 1;
2852 break;
2853 case QEMU_OPTION_acpitable:
2854 do_acpitable_option(optarg);
2855 break;
2856 case QEMU_OPTION_smbios:
2857 do_smbios_option(optarg);
2858 break;
2859 case QEMU_OPTION_enable_kvm:
2860 olist = qemu_find_opts("machine");
2861 qemu_opts_reset(olist);
2862 qemu_opts_parse(olist, "accel=kvm", 0);
2863 break;
2864 case QEMU_OPTION_machine:
2865 olist = qemu_find_opts("machine");
2866 qemu_opts_reset(olist);
2867 opts = qemu_opts_parse(olist, optarg, 1);
2868 if (!opts) {
2869 fprintf(stderr, "parse error: %s\n", optarg);
2870 exit(1);
2872 optarg = qemu_opt_get(opts, "type");
2873 if (optarg) {
2874 machine = machine_parse(optarg);
2876 break;
2877 case QEMU_OPTION_no_kvm:
2878 olist = qemu_find_opts("machine");
2879 qemu_opts_reset(olist);
2880 qemu_opts_parse(olist, "accel=tcg", 0);
2881 break;
2882 #ifdef CONFIG_KVM_OPTIONS
2883 case QEMU_OPTION_no_kvm_irqchip: {
2884 kvm_irqchip = 0;
2885 break;
2887 case QEMU_OPTION_no_kvm_pit: {
2888 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
2889 "separately.\n");
2890 break;
2892 case QEMU_OPTION_no_kvm_pit_reinjection: {
2893 kvm_pit_reinject = 0;
2894 break;
2896 #endif
2897 case QEMU_OPTION_usb:
2898 usb_enabled = 1;
2899 break;
2900 case QEMU_OPTION_usbdevice:
2901 usb_enabled = 1;
2902 add_device_config(DEV_USB, optarg);
2903 break;
2904 case QEMU_OPTION_device:
2905 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2906 exit(1);
2908 break;
2909 case QEMU_OPTION_smp:
2910 smp_parse(optarg);
2911 if (smp_cpus < 1) {
2912 fprintf(stderr, "Invalid number of CPUs\n");
2913 exit(1);
2915 if (max_cpus < smp_cpus) {
2916 fprintf(stderr, "maxcpus must be equal to or greater than "
2917 "smp\n");
2918 exit(1);
2920 if (max_cpus > 255) {
2921 fprintf(stderr, "Unsupported number of maxcpus\n");
2922 exit(1);
2924 break;
2925 case QEMU_OPTION_vnc:
2926 #ifdef CONFIG_VNC
2927 display_remote++;
2928 vnc_display = optarg;
2929 #else
2930 fprintf(stderr, "VNC support is disabled\n");
2931 exit(1);
2932 #endif
2933 break;
2934 case QEMU_OPTION_no_acpi:
2935 acpi_enabled = 0;
2936 break;
2937 case QEMU_OPTION_no_hpet:
2938 no_hpet = 1;
2939 break;
2940 case QEMU_OPTION_balloon:
2941 if (balloon_parse(optarg) < 0) {
2942 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2943 exit(1);
2945 break;
2946 case QEMU_OPTION_no_reboot:
2947 no_reboot = 1;
2948 break;
2949 case QEMU_OPTION_no_shutdown:
2950 no_shutdown = 1;
2951 break;
2952 case QEMU_OPTION_show_cursor:
2953 cursor_hide = 0;
2954 break;
2955 case QEMU_OPTION_uuid:
2956 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2957 fprintf(stderr, "Fail to parse UUID string."
2958 " Wrong format.\n");
2959 exit(1);
2961 break;
2962 case QEMU_OPTION_option_rom:
2963 if (nb_option_roms >= MAX_OPTION_ROMS) {
2964 fprintf(stderr, "Too many option ROMs\n");
2965 exit(1);
2967 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2968 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2969 option_rom[nb_option_roms].bootindex =
2970 qemu_opt_get_number(opts, "bootindex", -1);
2971 if (!option_rom[nb_option_roms].name) {
2972 fprintf(stderr, "Option ROM file is not specified\n");
2973 exit(1);
2975 nb_option_roms++;
2976 break;
2977 case QEMU_OPTION_semihosting:
2978 semihosting_enabled = 1;
2979 break;
2980 case QEMU_OPTION_tdf:
2981 fprintf(stderr, "Warning: user space PIT time drift fix "
2982 "is no longer supported.\n");
2983 break;
2984 case QEMU_OPTION_kvm_shadow_memory:
2985 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2986 break;
2987 case QEMU_OPTION_name:
2988 qemu_name = g_strdup(optarg);
2990 char *p = strchr(qemu_name, ',');
2991 if (p != NULL) {
2992 *p++ = 0;
2993 if (strncmp(p, "process=", 8)) {
2994 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2995 exit(1);
2997 p += 8;
2998 os_set_proc_name(p);
3001 break;
3002 case QEMU_OPTION_prom_env:
3003 if (nb_prom_envs >= MAX_PROM_ENVS) {
3004 fprintf(stderr, "Too many prom variables\n");
3005 exit(1);
3007 prom_envs[nb_prom_envs] = optarg;
3008 nb_prom_envs++;
3009 break;
3010 case QEMU_OPTION_old_param:
3011 old_param = 1;
3012 break;
3013 case QEMU_OPTION_clock:
3014 configure_alarms(optarg);
3015 break;
3016 case QEMU_OPTION_startdate:
3017 configure_rtc_date_offset(optarg, 1);
3018 break;
3019 case QEMU_OPTION_rtc:
3020 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3021 if (!opts) {
3022 exit(1);
3024 configure_rtc(opts);
3025 break;
3026 case QEMU_OPTION_tb_size:
3027 tcg_tb_size = strtol(optarg, NULL, 0);
3028 if (tcg_tb_size < 0) {
3029 tcg_tb_size = 0;
3031 break;
3032 case QEMU_OPTION_icount:
3033 icount_option = optarg;
3034 break;
3035 case QEMU_OPTION_incoming:
3036 incoming = optarg;
3037 break;
3038 case QEMU_OPTION_nodefaults:
3039 default_serial = 0;
3040 default_parallel = 0;
3041 default_virtcon = 0;
3042 default_monitor = 0;
3043 default_net = 0;
3044 default_floppy = 0;
3045 default_cdrom = 0;
3046 default_sdcard = 0;
3047 vga_model = "none";
3048 break;
3049 case QEMU_OPTION_xen_domid:
3050 if (!(xen_available())) {
3051 printf("Option %s not supported for this target\n", popt->name);
3052 exit(1);
3054 xen_domid = atoi(optarg);
3055 break;
3056 case QEMU_OPTION_xen_create:
3057 if (!(xen_available())) {
3058 printf("Option %s not supported for this target\n", popt->name);
3059 exit(1);
3061 xen_mode = XEN_CREATE;
3062 break;
3063 case QEMU_OPTION_xen_attach:
3064 if (!(xen_available())) {
3065 printf("Option %s not supported for this target\n", popt->name);
3066 exit(1);
3068 xen_mode = XEN_ATTACH;
3069 break;
3070 case QEMU_OPTION_trace:
3072 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3073 if (!opts) {
3074 exit(1);
3076 trace_events = qemu_opt_get(opts, "events");
3077 trace_file = qemu_opt_get(opts, "file");
3078 break;
3080 case QEMU_OPTION_readconfig:
3082 int ret = qemu_read_config_file(optarg);
3083 if (ret < 0) {
3084 fprintf(stderr, "read config %s: %s\n", optarg,
3085 strerror(-ret));
3086 exit(1);
3088 break;
3090 case QEMU_OPTION_spice:
3091 olist = qemu_find_opts("spice");
3092 if (!olist) {
3093 fprintf(stderr, "spice is not supported by this qemu build.\n");
3094 exit(1);
3096 opts = qemu_opts_parse(olist, optarg, 0);
3097 if (!opts) {
3098 fprintf(stderr, "parse error: %s\n", optarg);
3099 exit(1);
3101 break;
3102 case QEMU_OPTION_writeconfig:
3104 FILE *fp;
3105 if (strcmp(optarg, "-") == 0) {
3106 fp = stdout;
3107 } else {
3108 fp = fopen(optarg, "w");
3109 if (fp == NULL) {
3110 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3111 exit(1);
3114 qemu_config_write(fp);
3115 fclose(fp);
3116 break;
3118 default:
3119 os_parse_cmd_args(popt->index, optarg);
3123 loc_set_none();
3125 /* Open the logfile at this point, if necessary. We can't open the logfile
3126 * when encountering either of the logging options (-d or -D) because the
3127 * other one may be encountered later on the command line, changing the
3128 * location or level of logging.
3130 if (log_mask) {
3131 if (log_file) {
3132 set_cpu_log_filename(log_file);
3134 set_cpu_log(log_mask);
3137 if (!trace_backend_init(trace_events, trace_file)) {
3138 exit(1);
3141 /* If no data_dir is specified then try to find it relative to the
3142 executable path. */
3143 if (!data_dir) {
3144 data_dir = os_find_datadir(argv[0]);
3146 /* If all else fails use the install path specified when building. */
3147 if (!data_dir) {
3148 data_dir = CONFIG_QEMU_DATADIR;
3151 if (machine == NULL) {
3152 fprintf(stderr, "No machine found.\n");
3153 exit(1);
3157 * Default to max_cpus = smp_cpus, in case the user doesn't
3158 * specify a max_cpus value.
3160 if (!max_cpus)
3161 max_cpus = smp_cpus;
3163 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3164 if (smp_cpus > machine->max_cpus) {
3165 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3166 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3167 machine->max_cpus);
3168 exit(1);
3172 * Get the default machine options from the machine if it is not already
3173 * specified either by the configuration file or by the command line.
3175 if (machine->default_machine_opts) {
3176 QemuOptsList *list = qemu_find_opts("machine");
3177 const char *p = NULL;
3179 if (!QTAILQ_EMPTY(&list->head)) {
3180 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3182 if (p == NULL) {
3183 qemu_opts_reset(list);
3184 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3185 if (!opts) {
3186 fprintf(stderr, "parse error for machine %s: %s\n",
3187 machine->name, machine->default_machine_opts);
3188 exit(1);
3193 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3194 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3196 if (machine->no_serial) {
3197 default_serial = 0;
3199 if (machine->no_parallel) {
3200 default_parallel = 0;
3202 if (!machine->use_virtcon) {
3203 default_virtcon = 0;
3205 if (machine->no_floppy) {
3206 default_floppy = 0;
3208 if (machine->no_cdrom) {
3209 default_cdrom = 0;
3211 if (machine->no_sdcard) {
3212 default_sdcard = 0;
3215 if (display_type == DT_NOGRAPHIC) {
3216 if (default_parallel)
3217 add_device_config(DEV_PARALLEL, "null");
3218 if (default_serial && default_monitor) {
3219 add_device_config(DEV_SERIAL, "mon:stdio");
3220 } else if (default_virtcon && default_monitor) {
3221 add_device_config(DEV_VIRTCON, "mon:stdio");
3222 } else {
3223 if (default_serial)
3224 add_device_config(DEV_SERIAL, "stdio");
3225 if (default_virtcon)
3226 add_device_config(DEV_VIRTCON, "stdio");
3227 if (default_monitor)
3228 monitor_parse("stdio", "readline");
3230 } else {
3231 if (default_serial)
3232 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3233 if (default_parallel)
3234 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3235 if (default_monitor)
3236 monitor_parse("vc:80Cx24C", "readline");
3237 if (default_virtcon)
3238 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3241 socket_init();
3243 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3244 exit(1);
3245 #ifdef CONFIG_VIRTFS
3246 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3247 exit(1);
3249 #endif
3251 os_daemonize();
3253 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3254 os_pidfile_error();
3255 exit(1);
3258 /* init the memory */
3259 if (ram_size == 0) {
3260 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3263 configure_accelerator();
3265 qemu_init_cpu_loop();
3266 if (qemu_init_main_loop()) {
3267 fprintf(stderr, "qemu_init_main_loop failed\n");
3268 exit(1);
3270 linux_boot = (kernel_filename != NULL);
3272 if (!linux_boot && *kernel_cmdline != '\0') {
3273 fprintf(stderr, "-append only allowed with -kernel option\n");
3274 exit(1);
3277 if (!linux_boot && initrd_filename != NULL) {
3278 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3279 exit(1);
3282 os_set_line_buffering();
3284 if (init_timer_alarm() < 0) {
3285 fprintf(stderr, "could not initialize alarm timer\n");
3286 exit(1);
3289 if (icount_option && (kvm_enabled() || xen_enabled())) {
3290 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3291 exit(1);
3293 configure_icount(icount_option);
3295 if (net_init_clients() < 0) {
3296 exit(1);
3299 /* init the bluetooth world */
3300 if (foreach_device_config(DEV_BT, bt_parse))
3301 exit(1);
3303 if (!xen_enabled()) {
3304 /* On 32-bit hosts, QEMU is limited by virtual address space */
3305 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3306 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3307 exit(1);
3311 cpu_exec_init_all();
3313 bdrv_init_with_whitelist();
3315 blk_mig_init();
3317 /* open the virtual block devices */
3318 if (snapshot)
3319 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3320 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3321 exit(1);
3323 default_drive(default_cdrom, snapshot, machine->use_scsi,
3324 IF_DEFAULT, 2, CDROM_OPTS);
3325 default_drive(default_floppy, snapshot, machine->use_scsi,
3326 IF_FLOPPY, 0, FD_OPTS);
3327 default_drive(default_sdcard, snapshot, machine->use_scsi,
3328 IF_SD, 0, SD_OPTS);
3330 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3331 ram_load, NULL);
3333 if (nb_numa_nodes > 0) {
3334 int i;
3336 if (nb_numa_nodes > MAX_NODES) {
3337 nb_numa_nodes = MAX_NODES;
3340 /* If no memory size if given for any node, assume the default case
3341 * and distribute the available memory equally across all nodes
3343 for (i = 0; i < nb_numa_nodes; i++) {
3344 if (node_mem[i] != 0)
3345 break;
3347 if (i == nb_numa_nodes) {
3348 uint64_t usedmem = 0;
3350 /* On Linux, the each node's border has to be 8MB aligned,
3351 * the final node gets the rest.
3353 for (i = 0; i < nb_numa_nodes - 1; i++) {
3354 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3355 usedmem += node_mem[i];
3357 node_mem[i] = ram_size - usedmem;
3360 for (i = 0; i < nb_numa_nodes; i++) {
3361 if (node_cpumask[i] != 0)
3362 break;
3364 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3365 * must cope with this anyway, because there are BIOSes out there in
3366 * real machines which also use this scheme.
3368 if (i == nb_numa_nodes) {
3369 for (i = 0; i < max_cpus; i++) {
3370 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3375 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3376 exit(1);
3379 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3380 exit(1);
3381 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3382 exit(1);
3383 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3384 exit(1);
3385 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3386 exit(1);
3388 module_call_init(MODULE_INIT_DEVICE);
3390 /* must be after qdev registration but before machine init */
3391 if (vga_model) {
3392 select_vgahw(vga_model);
3393 } else if (cirrus_vga_available()) {
3394 select_vgahw("cirrus");
3395 } else {
3396 select_vgahw("none");
3399 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3400 exit(0);
3402 if (watchdog) {
3403 i = select_watchdog(watchdog);
3404 if (i > 0)
3405 exit (i == 1 ? 1 : 0);
3408 if (machine->compat_props) {
3409 qdev_prop_register_global_list(machine->compat_props);
3411 qemu_add_globals();
3413 qdev_machine_init();
3415 machine->init(ram_size, boot_devices,
3416 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3418 cpu_synchronize_all_post_init();
3420 set_numa_modes();
3422 current_machine = machine;
3424 /* init USB devices */
3425 if (usb_enabled) {
3426 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3427 exit(1);
3430 /* init generic devices */
3431 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3432 exit(1);
3434 net_check_clients();
3436 /* just use the first displaystate for the moment */
3437 ds = get_displaystate();
3439 if (using_spice)
3440 display_remote++;
3441 if (display_type == DT_DEFAULT && !display_remote) {
3442 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3443 display_type = DT_SDL;
3444 #elif defined(CONFIG_VNC)
3445 vnc_display = "localhost:0,to=99";
3446 show_vnc_port = 1;
3447 #else
3448 display_type = DT_NONE;
3449 #endif
3453 /* init local displays */
3454 switch (display_type) {
3455 case DT_NOGRAPHIC:
3456 break;
3457 #if defined(CONFIG_CURSES)
3458 case DT_CURSES:
3459 curses_display_init(ds, full_screen);
3460 break;
3461 #endif
3462 #if defined(CONFIG_SDL)
3463 case DT_SDL:
3464 sdl_display_init(ds, full_screen, no_frame);
3465 break;
3466 #elif defined(CONFIG_COCOA)
3467 case DT_SDL:
3468 cocoa_display_init(ds, full_screen);
3469 break;
3470 #endif
3471 default:
3472 break;
3475 /* must be after terminal init, SDL library changes signal handlers */
3476 os_setup_signal_handling();
3478 #ifdef CONFIG_VNC
3479 /* init remote displays */
3480 if (vnc_display) {
3481 vnc_display_init(ds);
3482 if (vnc_display_open(ds, vnc_display) < 0)
3483 exit(1);
3485 if (show_vnc_port) {
3486 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3489 #endif
3490 #ifdef CONFIG_SPICE
3491 if (using_spice && !qxl_enabled) {
3492 qemu_spice_display_init(ds);
3494 #endif
3496 /* display setup */
3497 dpy_resize(ds);
3498 dcl = ds->listeners;
3499 while (dcl != NULL) {
3500 if (dcl->dpy_refresh != NULL) {
3501 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3502 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3503 break;
3505 dcl = dcl->next;
3507 text_consoles_set_display(ds);
3509 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3510 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3511 gdbstub_dev);
3512 exit(1);
3515 qdev_machine_creation_done();
3517 if (rom_load_all() != 0) {
3518 fprintf(stderr, "rom loading failed\n");
3519 exit(1);
3522 /* TODO: once all bus devices are qdevified, this should be done
3523 * when bus is created by qdev.c */
3524 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3525 qemu_run_machine_init_done_notifiers();
3527 qemu_system_reset(VMRESET_SILENT);
3528 if (loadvm) {
3529 if (load_vmstate(loadvm) < 0) {
3530 autostart = 0;
3534 if (incoming) {
3535 runstate_set(RUN_STATE_INMIGRATE);
3536 int ret = qemu_start_incoming_migration(incoming);
3537 if (ret < 0) {
3538 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3539 incoming, ret);
3540 exit(ret);
3542 } else if (autostart) {
3543 vm_start();
3546 os_setup_post();
3548 resume_all_vcpus();
3549 main_loop();
3550 bdrv_close_all();
3551 pause_all_vcpus();
3552 net_cleanup();
3553 res_free();
3555 return 0;