Merge commit 'f711df67d611e4762966a249742a5f7499e19f99' into upstream-merge
[qemu-kvm/stefanha.git] / vl.c
blobe3c89195334be612582f66b66d981bc4fa671c99
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>
50 #ifdef CONFIG_SIMPLE_TRACE
51 #include "trace.h"
52 #endif
54 #ifdef CONFIG_BSD
55 #include <sys/stat.h>
56 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
57 #include <libutil.h>
58 #include <sys/sysctl.h>
59 #else
60 #include <util.h>
61 #endif
62 #else
63 #ifdef __linux__
64 #include <pty.h>
65 #include <malloc.h>
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
69 #endif
70 #ifdef __sun__
71 #include <sys/stat.h>
72 #include <sys/ethernet.h>
73 #include <sys/sockio.h>
74 #include <netinet/arp.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h> // must come after ip.h
78 #include <netinet/udp.h>
79 #include <netinet/tcp.h>
80 #include <net/if.h>
81 #include <syslog.h>
82 #include <stropts.h>
83 #endif
84 #endif
85 #endif
87 #if defined(__OpenBSD__)
88 #include <util.h>
89 #endif
91 #if defined(CONFIG_VDE)
92 #include <libvdeplug.h>
93 #endif
95 #ifdef _WIN32
96 #include <windows.h>
97 #endif
99 #ifdef CONFIG_SDL
100 #if defined(__APPLE__) || defined(main)
101 #include <SDL.h>
102 int qemu_main(int argc, char **argv, char **envp);
103 int main(int argc, char **argv)
105 return qemu_main(argc, argv, NULL);
107 #undef main
108 #define main qemu_main
109 #endif
110 #endif /* CONFIG_SDL */
112 #ifdef CONFIG_COCOA
113 #undef main
114 #define main qemu_main
115 #endif /* CONFIG_COCOA */
117 #include "hw/hw.h"
118 #include "hw/boards.h"
119 #include "hw/usb.h"
120 #include "hw/pcmcia.h"
121 #include "hw/pc.h"
122 #include "hw/isa.h"
123 #include "hw/baum.h"
124 #include "hw/bt.h"
125 #include "hw/watchdog.h"
126 #include "hw/smbios.h"
127 #include "hw/xen.h"
128 #include "hw/qdev.h"
129 #include "hw/loader.h"
130 #include "bt-host.h"
131 #include "net.h"
132 #include "net/slirp.h"
133 #include "monitor.h"
134 #include "console.h"
135 #include "sysemu.h"
136 #include "gdbstub.h"
137 #include "qemu-timer.h"
138 #include "qemu-char.h"
139 #include "cache-utils.h"
140 #include "block.h"
141 #include "blockdev.h"
142 #include "block-migration.h"
143 #include "dma.h"
144 #include "audio/audio.h"
145 #include "migration.h"
146 #include "kvm.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-objects.h"
150 #include "qemu-options.h"
151 #include "hw/device-assignment.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 "qemu-queue.h"
164 #include "cpus.h"
165 #include "arch_init.h"
167 #include "ui/qemu-spice.h"
169 //#define DEBUG_NET
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir;
177 const char *bios_name = NULL;
178 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
179 DisplayType display_type = DT_DEFAULT;
180 int display_remote = 0;
181 const char* keyboard_layout = NULL;
182 ram_addr_t ram_size;
183 const char *mem_path = NULL;
184 #ifdef MAP_POPULATE
185 int mem_prealloc = 0; /* force preallocation of physical target memory */
186 #endif
187 int nb_nics;
188 NICInfo nd_table[MAX_NICS];
189 int vm_running;
190 int autostart;
191 int incoming_expected; /* Started with -incoming and waiting for incoming */
192 static int rtc_utc = 1;
193 static int rtc_date_offset = -1; /* -1 means no change */
194 QEMUClock *rtc_clock;
195 int vga_interface_type = VGA_NONE;
196 static int full_screen = 0;
197 #ifdef CONFIG_SDL
198 static int no_frame = 0;
199 #endif
200 int no_quit = 0;
201 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
202 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
203 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
204 int win2k_install_hack = 0;
205 int rtc_td_hack = 0;
206 int usb_enabled = 0;
207 int singlestep = 0;
208 int smp_cpus = 1;
209 int max_cpus = 0;
210 int smp_cores = 1;
211 int smp_threads = 1;
212 const char *vnc_display;
213 int acpi_enabled = 1;
214 int no_hpet = 0;
215 int fd_bootchk = 1;
216 int no_reboot = 0;
217 int no_shutdown = 0;
218 int cursor_hide = 1;
219 int graphic_rotate = 0;
220 uint8_t irq0override = 1;
221 const char *watchdog;
222 const char *option_rom[MAX_OPTION_ROMS];
223 int nb_option_roms;
224 int semihosting_enabled = 0;
225 int time_drift_fix = 0;
226 unsigned int kvm_shadow_memory = 0;
227 int old_param = 0;
228 const char *qemu_name;
229 int alt_grab = 0;
230 int ctrl_grab = 0;
231 unsigned int nb_prom_envs = 0;
232 const char *prom_envs[MAX_PROM_ENVS];
233 const char *nvram = NULL;
234 int boot_menu;
236 int nb_numa_nodes;
237 uint64_t node_mem[MAX_NODES];
238 uint64_t node_cpumask[MAX_NODES];
240 static QEMUTimer *nographic_timer;
242 uint8_t qemu_uuid[16];
244 static QEMUBootSetHandler *boot_set_handler;
245 static void *boot_set_opaque;
247 static NotifierList exit_notifiers =
248 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
250 int kvm_allowed = 1;
251 uint32_t xen_domid;
252 enum xen_mode xen_mode = XEN_EMULATE;
254 static int default_serial = 1;
255 static int default_parallel = 1;
256 static int default_virtcon = 1;
257 static int default_monitor = 1;
258 static int default_vga = 1;
259 static int default_floppy = 1;
260 static int default_cdrom = 1;
261 static int default_sdcard = 1;
263 static struct {
264 const char *driver;
265 int *flag;
266 } default_list[] = {
267 { .driver = "isa-serial", .flag = &default_serial },
268 { .driver = "isa-parallel", .flag = &default_parallel },
269 { .driver = "isa-fdc", .flag = &default_floppy },
270 { .driver = "ide-drive", .flag = &default_cdrom },
271 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
272 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
273 { .driver = "virtio-serial", .flag = &default_virtcon },
274 { .driver = "VGA", .flag = &default_vga },
275 { .driver = "cirrus-vga", .flag = &default_vga },
276 { .driver = "vmware-svga", .flag = &default_vga },
279 static int default_driver_check(QemuOpts *opts, void *opaque)
281 const char *driver = qemu_opt_get(opts, "driver");
282 int i;
284 if (!driver)
285 return 0;
286 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
287 if (strcmp(default_list[i].driver, driver) != 0)
288 continue;
289 *(default_list[i].flag) = 0;
291 return 0;
294 /***********************************************************/
295 /* real time host monotonic timer */
297 /***********************************************************/
298 /* host time/date access */
299 void qemu_get_timedate(struct tm *tm, int offset)
301 time_t ti;
302 struct tm *ret;
304 time(&ti);
305 ti += offset;
306 if (rtc_date_offset == -1) {
307 if (rtc_utc)
308 ret = gmtime(&ti);
309 else
310 ret = localtime(&ti);
311 } else {
312 ti -= rtc_date_offset;
313 ret = gmtime(&ti);
316 memcpy(tm, ret, sizeof(struct tm));
319 int qemu_timedate_diff(struct tm *tm)
321 time_t seconds;
323 if (rtc_date_offset == -1)
324 if (rtc_utc)
325 seconds = mktimegm(tm);
326 else
327 seconds = mktime(tm);
328 else
329 seconds = mktimegm(tm) + rtc_date_offset;
331 return seconds - time(NULL);
334 void rtc_change_mon_event(struct tm *tm)
336 QObject *data;
338 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
339 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
340 qobject_decref(data);
343 static void configure_rtc_date_offset(const char *startdate, int legacy)
345 time_t rtc_start_date;
346 struct tm tm;
348 if (!strcmp(startdate, "now") && legacy) {
349 rtc_date_offset = -1;
350 } else {
351 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
352 &tm.tm_year,
353 &tm.tm_mon,
354 &tm.tm_mday,
355 &tm.tm_hour,
356 &tm.tm_min,
357 &tm.tm_sec) == 6) {
358 /* OK */
359 } else if (sscanf(startdate, "%d-%d-%d",
360 &tm.tm_year,
361 &tm.tm_mon,
362 &tm.tm_mday) == 3) {
363 tm.tm_hour = 0;
364 tm.tm_min = 0;
365 tm.tm_sec = 0;
366 } else {
367 goto date_fail;
369 tm.tm_year -= 1900;
370 tm.tm_mon--;
371 rtc_start_date = mktimegm(&tm);
372 if (rtc_start_date == -1) {
373 date_fail:
374 fprintf(stderr, "Invalid date format. Valid formats are:\n"
375 "'2006-06-17T16:01:21' or '2006-06-17'\n");
376 exit(1);
378 rtc_date_offset = time(NULL) - rtc_start_date;
382 static void configure_rtc(QemuOpts *opts)
384 const char *value;
386 value = qemu_opt_get(opts, "base");
387 if (value) {
388 if (!strcmp(value, "utc")) {
389 rtc_utc = 1;
390 } else if (!strcmp(value, "localtime")) {
391 rtc_utc = 0;
392 } else {
393 configure_rtc_date_offset(value, 0);
396 value = qemu_opt_get(opts, "clock");
397 if (value) {
398 if (!strcmp(value, "host")) {
399 rtc_clock = host_clock;
400 } else if (!strcmp(value, "vm")) {
401 rtc_clock = vm_clock;
402 } else {
403 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
404 exit(1);
407 value = qemu_opt_get(opts, "driftfix");
408 if (value) {
409 if (!strcmp(value, "slew")) {
410 rtc_td_hack = 1;
411 } else if (!strcmp(value, "none")) {
412 rtc_td_hack = 0;
413 } else {
414 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
415 exit(1);
420 /***********************************************************/
421 /* Bluetooth support */
422 static int nb_hcis;
423 static int cur_hci;
424 static struct HCIInfo *hci_table[MAX_NICS];
426 static struct bt_vlan_s {
427 struct bt_scatternet_s net;
428 int id;
429 struct bt_vlan_s *next;
430 } *first_bt_vlan;
432 /* find or alloc a new bluetooth "VLAN" */
433 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
435 struct bt_vlan_s **pvlan, *vlan;
436 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
437 if (vlan->id == id)
438 return &vlan->net;
440 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
441 vlan->id = id;
442 pvlan = &first_bt_vlan;
443 while (*pvlan != NULL)
444 pvlan = &(*pvlan)->next;
445 *pvlan = vlan;
446 return &vlan->net;
449 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
453 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
455 return -ENOTSUP;
458 static struct HCIInfo null_hci = {
459 .cmd_send = null_hci_send,
460 .sco_send = null_hci_send,
461 .acl_send = null_hci_send,
462 .bdaddr_set = null_hci_addr_set,
465 struct HCIInfo *qemu_next_hci(void)
467 if (cur_hci == nb_hcis)
468 return &null_hci;
470 return hci_table[cur_hci++];
473 static struct HCIInfo *hci_init(const char *str)
475 char *endp;
476 struct bt_scatternet_s *vlan = 0;
478 if (!strcmp(str, "null"))
479 /* null */
480 return &null_hci;
481 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
482 /* host[:hciN] */
483 return bt_host_hci(str[4] ? str + 5 : "hci0");
484 else if (!strncmp(str, "hci", 3)) {
485 /* hci[,vlan=n] */
486 if (str[3]) {
487 if (!strncmp(str + 3, ",vlan=", 6)) {
488 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
489 if (*endp)
490 vlan = 0;
492 } else
493 vlan = qemu_find_bt_vlan(0);
494 if (vlan)
495 return bt_new_hci(vlan);
498 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
500 return 0;
503 static int bt_hci_parse(const char *str)
505 struct HCIInfo *hci;
506 bdaddr_t bdaddr;
508 if (nb_hcis >= MAX_NICS) {
509 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
510 return -1;
513 hci = hci_init(str);
514 if (!hci)
515 return -1;
517 bdaddr.b[0] = 0x52;
518 bdaddr.b[1] = 0x54;
519 bdaddr.b[2] = 0x00;
520 bdaddr.b[3] = 0x12;
521 bdaddr.b[4] = 0x34;
522 bdaddr.b[5] = 0x56 + nb_hcis;
523 hci->bdaddr_set(hci, bdaddr.b);
525 hci_table[nb_hcis++] = hci;
527 return 0;
530 static void bt_vhci_add(int vlan_id)
532 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
534 if (!vlan->slave)
535 fprintf(stderr, "qemu: warning: adding a VHCI to "
536 "an empty scatternet %i\n", vlan_id);
538 bt_vhci_init(bt_new_hci(vlan));
541 static struct bt_device_s *bt_device_add(const char *opt)
543 struct bt_scatternet_s *vlan;
544 int vlan_id = 0;
545 char *endp = strstr(opt, ",vlan=");
546 int len = (endp ? endp - opt : strlen(opt)) + 1;
547 char devname[10];
549 pstrcpy(devname, MIN(sizeof(devname), len), opt);
551 if (endp) {
552 vlan_id = strtol(endp + 6, &endp, 0);
553 if (*endp) {
554 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
555 return 0;
559 vlan = qemu_find_bt_vlan(vlan_id);
561 if (!vlan->slave)
562 fprintf(stderr, "qemu: warning: adding a slave device to "
563 "an empty scatternet %i\n", vlan_id);
565 if (!strcmp(devname, "keyboard"))
566 return bt_keyboard_init(vlan);
568 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
569 return 0;
572 static int bt_parse(const char *opt)
574 const char *endp, *p;
575 int vlan;
577 if (strstart(opt, "hci", &endp)) {
578 if (!*endp || *endp == ',') {
579 if (*endp)
580 if (!strstart(endp, ",vlan=", 0))
581 opt = endp + 1;
583 return bt_hci_parse(opt);
585 } else if (strstart(opt, "vhci", &endp)) {
586 if (!*endp || *endp == ',') {
587 if (*endp) {
588 if (strstart(endp, ",vlan=", &p)) {
589 vlan = strtol(p, (char **) &endp, 0);
590 if (*endp) {
591 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
592 return 1;
594 } else {
595 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
596 return 1;
598 } else
599 vlan = 0;
601 bt_vhci_add(vlan);
602 return 0;
604 } else if (strstart(opt, "device:", &endp))
605 return !bt_device_add(endp);
607 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
608 return 1;
611 /***********************************************************/
612 /* QEMU Block devices */
614 #define HD_ALIAS "index=%d,media=disk"
615 #define CDROM_ALIAS "index=2,media=cdrom"
616 #define FD_ALIAS "index=%d,if=floppy"
617 #define PFLASH_ALIAS "if=pflash"
618 #define MTD_ALIAS "if=mtd"
619 #define SD_ALIAS "index=0,if=sd"
621 static int drive_init_func(QemuOpts *opts, void *opaque)
623 int *use_scsi = opaque;
624 int fatal_error = 0;
626 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
627 if (fatal_error)
628 return 1;
630 return 0;
633 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
635 if (NULL == qemu_opt_get(opts, "snapshot")) {
636 qemu_opt_set(opts, "snapshot", "on");
638 return 0;
641 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
643 boot_set_handler = func;
644 boot_set_opaque = opaque;
647 int qemu_boot_set(const char *boot_devices)
649 if (!boot_set_handler) {
650 return -EINVAL;
652 return boot_set_handler(boot_set_opaque, boot_devices);
655 static void validate_bootdevices(char *devices)
657 /* We just do some generic consistency checks */
658 const char *p;
659 int bitmap = 0;
661 for (p = devices; *p != '\0'; p++) {
662 /* Allowed boot devices are:
663 * a-b: floppy disk drives
664 * c-f: IDE disk drives
665 * g-m: machine implementation dependant drives
666 * n-p: network devices
667 * It's up to each machine implementation to check if the given boot
668 * devices match the actual hardware implementation and firmware
669 * features.
671 if (*p < 'a' || *p > 'p') {
672 fprintf(stderr, "Invalid boot device '%c'\n", *p);
673 exit(1);
675 if (bitmap & (1 << (*p - 'a'))) {
676 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
677 exit(1);
679 bitmap |= 1 << (*p - 'a');
683 static void restore_boot_devices(void *opaque)
685 char *standard_boot_devices = opaque;
686 static int first = 1;
688 /* Restore boot order and remove ourselves after the first boot */
689 if (first) {
690 first = 0;
691 return;
694 qemu_boot_set(standard_boot_devices);
696 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
697 qemu_free(standard_boot_devices);
700 static void numa_add(const char *optarg)
702 char option[128];
703 char *endptr;
704 unsigned long long value, endvalue;
705 int nodenr;
707 optarg = get_opt_name(option, 128, optarg, ',') + 1;
708 if (!strcmp(option, "node")) {
709 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
710 nodenr = nb_numa_nodes;
711 } else {
712 nodenr = strtoull(option, NULL, 10);
715 if (get_param_value(option, 128, "mem", optarg) == 0) {
716 node_mem[nodenr] = 0;
717 } else {
718 ssize_t sval;
719 sval = strtosz(option, NULL);
720 if (sval < 0) {
721 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
722 exit(1);
724 node_mem[nodenr] = sval;
726 if (get_param_value(option, 128, "cpus", optarg) == 0) {
727 node_cpumask[nodenr] = 0;
728 } else {
729 value = strtoull(option, &endptr, 10);
730 if (value >= 64) {
731 value = 63;
732 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
733 } else {
734 if (*endptr == '-') {
735 endvalue = strtoull(endptr+1, &endptr, 10);
736 if (endvalue >= 63) {
737 endvalue = 62;
738 fprintf(stderr,
739 "only 63 CPUs in NUMA mode supported.\n");
741 value = (2ULL << endvalue) - (1ULL << value);
742 } else {
743 value = 1ULL << value;
746 node_cpumask[nodenr] = value;
748 nb_numa_nodes++;
750 return;
753 static void smp_parse(const char *optarg)
755 int smp, sockets = 0, threads = 0, cores = 0;
756 char *endptr;
757 char option[128];
759 smp = strtoul(optarg, &endptr, 10);
760 if (endptr != optarg) {
761 if (*endptr == ',') {
762 endptr++;
765 if (get_param_value(option, 128, "sockets", endptr) != 0)
766 sockets = strtoull(option, NULL, 10);
767 if (get_param_value(option, 128, "cores", endptr) != 0)
768 cores = strtoull(option, NULL, 10);
769 if (get_param_value(option, 128, "threads", endptr) != 0)
770 threads = strtoull(option, NULL, 10);
771 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
772 max_cpus = strtoull(option, NULL, 10);
774 /* compute missing values, prefer sockets over cores over threads */
775 if (smp == 0 || sockets == 0) {
776 sockets = sockets > 0 ? sockets : 1;
777 cores = cores > 0 ? cores : 1;
778 threads = threads > 0 ? threads : 1;
779 if (smp == 0) {
780 smp = cores * threads * sockets;
782 } else {
783 if (cores == 0) {
784 threads = threads > 0 ? threads : 1;
785 cores = smp / (sockets * threads);
786 } else {
787 threads = smp / (cores * sockets);
790 smp_cpus = smp;
791 smp_cores = cores > 0 ? cores : 1;
792 smp_threads = threads > 0 ? threads : 1;
793 if (max_cpus == 0)
794 max_cpus = smp_cpus;
797 /***********************************************************/
798 /* USB devices */
800 static int usb_device_add(const char *devname)
802 const char *p;
803 USBDevice *dev = NULL;
805 if (!usb_enabled)
806 return -1;
808 /* drivers with .usbdevice_name entry in USBDeviceInfo */
809 dev = usbdevice_create(devname);
810 if (dev)
811 goto done;
813 /* the other ones */
814 if (strstart(devname, "host:", &p)) {
815 dev = usb_host_device_open(p);
816 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
817 dev = usb_bt_init(devname[2] ? hci_init(p) :
818 bt_new_hci(qemu_find_bt_vlan(0)));
819 } else {
820 return -1;
822 if (!dev)
823 return -1;
825 done:
826 return 0;
829 static int usb_device_del(const char *devname)
831 int bus_num, addr;
832 const char *p;
834 if (strstart(devname, "host:", &p))
835 return usb_host_device_close(p);
837 if (!usb_enabled)
838 return -1;
840 p = strchr(devname, '.');
841 if (!p)
842 return -1;
843 bus_num = strtoul(devname, NULL, 0);
844 addr = strtoul(p + 1, NULL, 0);
846 return usb_device_delete_addr(bus_num, addr);
849 static int usb_parse(const char *cmdline)
851 int r;
852 r = usb_device_add(cmdline);
853 if (r < 0) {
854 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
856 return r;
859 void do_usb_add(Monitor *mon, const QDict *qdict)
861 const char *devname = qdict_get_str(qdict, "devname");
862 if (usb_device_add(devname) < 0) {
863 error_report("could not add USB device '%s'", devname);
867 void do_usb_del(Monitor *mon, const QDict *qdict)
869 const char *devname = qdict_get_str(qdict, "devname");
870 if (usb_device_del(devname) < 0) {
871 error_report("could not delete USB device '%s'", devname);
875 /***********************************************************/
876 /* PCMCIA/Cardbus */
878 static struct pcmcia_socket_entry_s {
879 PCMCIASocket *socket;
880 struct pcmcia_socket_entry_s *next;
881 } *pcmcia_sockets = 0;
883 void pcmcia_socket_register(PCMCIASocket *socket)
885 struct pcmcia_socket_entry_s *entry;
887 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
888 entry->socket = socket;
889 entry->next = pcmcia_sockets;
890 pcmcia_sockets = entry;
893 void pcmcia_socket_unregister(PCMCIASocket *socket)
895 struct pcmcia_socket_entry_s *entry, **ptr;
897 ptr = &pcmcia_sockets;
898 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
899 if (entry->socket == socket) {
900 *ptr = entry->next;
901 qemu_free(entry);
905 void pcmcia_info(Monitor *mon)
907 struct pcmcia_socket_entry_s *iter;
909 if (!pcmcia_sockets)
910 monitor_printf(mon, "No PCMCIA sockets\n");
912 for (iter = pcmcia_sockets; iter; iter = iter->next)
913 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
914 iter->socket->attached ? iter->socket->card_string :
915 "Empty");
918 /***********************************************************/
919 /* I/O handling */
921 typedef struct IOHandlerRecord {
922 int fd;
923 IOCanReadHandler *fd_read_poll;
924 IOHandler *fd_read;
925 IOHandler *fd_write;
926 int deleted;
927 void *opaque;
928 /* temporary data */
929 struct pollfd *ufd;
930 QLIST_ENTRY(IOHandlerRecord) next;
931 } IOHandlerRecord;
933 static QLIST_HEAD(, IOHandlerRecord) io_handlers =
934 QLIST_HEAD_INITIALIZER(io_handlers);
937 /* XXX: fd_read_poll should be suppressed, but an API change is
938 necessary in the character devices to suppress fd_can_read(). */
939 int qemu_set_fd_handler2(int fd,
940 IOCanReadHandler *fd_read_poll,
941 IOHandler *fd_read,
942 IOHandler *fd_write,
943 void *opaque)
945 IOHandlerRecord *ioh;
947 if (!fd_read && !fd_write) {
948 QLIST_FOREACH(ioh, &io_handlers, next) {
949 if (ioh->fd == fd) {
950 ioh->deleted = 1;
951 break;
954 } else {
955 QLIST_FOREACH(ioh, &io_handlers, next) {
956 if (ioh->fd == fd)
957 goto found;
959 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
960 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
961 found:
962 ioh->fd = fd;
963 ioh->fd_read_poll = fd_read_poll;
964 ioh->fd_read = fd_read;
965 ioh->fd_write = fd_write;
966 ioh->opaque = opaque;
967 ioh->deleted = 0;
969 qemu_notify_event();
970 return 0;
973 int qemu_set_fd_handler(int fd,
974 IOHandler *fd_read,
975 IOHandler *fd_write,
976 void *opaque)
978 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
981 /***********************************************************/
982 /* machine registration */
984 static QEMUMachine *first_machine = NULL;
985 QEMUMachine *current_machine = NULL;
987 int qemu_register_machine(QEMUMachine *m)
989 QEMUMachine **pm;
990 pm = &first_machine;
991 while (*pm != NULL)
992 pm = &(*pm)->next;
993 m->next = NULL;
994 *pm = m;
995 return 0;
998 static QEMUMachine *find_machine(const char *name)
1000 QEMUMachine *m;
1002 for(m = first_machine; m != NULL; m = m->next) {
1003 if (!strcmp(m->name, name))
1004 return m;
1005 if (m->alias && !strcmp(m->alias, name))
1006 return m;
1008 return NULL;
1011 static QEMUMachine *find_default_machine(void)
1013 QEMUMachine *m;
1015 for(m = first_machine; m != NULL; m = m->next) {
1016 if (m->is_default) {
1017 return m;
1020 return NULL;
1023 /***********************************************************/
1024 /* main execution loop */
1026 static void gui_update(void *opaque)
1028 uint64_t interval = GUI_REFRESH_INTERVAL;
1029 DisplayState *ds = opaque;
1030 DisplayChangeListener *dcl = ds->listeners;
1032 qemu_flush_coalesced_mmio_buffer();
1033 dpy_refresh(ds);
1035 while (dcl != NULL) {
1036 if (dcl->gui_timer_interval &&
1037 dcl->gui_timer_interval < interval)
1038 interval = dcl->gui_timer_interval;
1039 dcl = dcl->next;
1041 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1044 static void nographic_update(void *opaque)
1046 uint64_t interval = GUI_REFRESH_INTERVAL;
1048 qemu_flush_coalesced_mmio_buffer();
1049 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1052 struct vm_change_state_entry {
1053 VMChangeStateHandler *cb;
1054 void *opaque;
1055 QLIST_ENTRY (vm_change_state_entry) entries;
1058 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1060 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1061 void *opaque)
1063 VMChangeStateEntry *e;
1065 e = qemu_mallocz(sizeof (*e));
1067 e->cb = cb;
1068 e->opaque = opaque;
1069 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1070 return e;
1073 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1075 QLIST_REMOVE (e, entries);
1076 qemu_free (e);
1079 void vm_state_notify(int running, int reason)
1081 VMChangeStateEntry *e;
1083 trace_vm_state_notify(running, reason);
1085 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1086 e->cb(e->opaque, running, reason);
1090 void vm_start(void)
1092 if (!vm_running) {
1093 cpu_enable_ticks();
1094 vm_running = 1;
1095 vm_state_notify(1, 0);
1096 resume_all_vcpus();
1097 monitor_protocol_event(QEVENT_RESUME, NULL);
1101 /* reset/shutdown handler */
1103 typedef struct QEMUResetEntry {
1104 QTAILQ_ENTRY(QEMUResetEntry) entry;
1105 QEMUResetHandler *func;
1106 void *opaque;
1107 } QEMUResetEntry;
1109 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1110 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1111 static int reset_requested;
1112 static int shutdown_requested;
1113 static int powerdown_requested;
1114 int debug_requested;
1115 int vmstop_requested;
1117 int qemu_no_shutdown(void)
1119 int r = no_shutdown;
1120 no_shutdown = 0;
1121 return r;
1124 int qemu_shutdown_requested(void)
1126 int r = shutdown_requested;
1127 shutdown_requested = 0;
1128 return r;
1131 int qemu_reset_requested(void)
1133 int r = reset_requested;
1134 reset_requested = 0;
1135 return r;
1138 int qemu_powerdown_requested(void)
1140 int r = powerdown_requested;
1141 powerdown_requested = 0;
1142 return r;
1145 static int qemu_debug_requested(void)
1147 int r = debug_requested;
1148 debug_requested = 0;
1149 return r;
1152 static int qemu_vmstop_requested(void)
1154 int r = vmstop_requested;
1155 vmstop_requested = 0;
1156 return r;
1159 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1161 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1163 re->func = func;
1164 re->opaque = opaque;
1165 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1168 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1170 QEMUResetEntry *re;
1172 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1173 if (re->func == func && re->opaque == opaque) {
1174 QTAILQ_REMOVE(&reset_handlers, re, entry);
1175 qemu_free(re);
1176 return;
1181 void qemu_system_reset(void)
1183 QEMUResetEntry *re, *nre;
1185 /* reset all devices */
1186 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1187 re->func(re->opaque);
1189 monitor_protocol_event(QEVENT_RESET, NULL);
1190 cpu_synchronize_all_post_reset();
1193 void qemu_system_reset_request(void)
1195 if (no_reboot) {
1196 shutdown_requested = 1;
1197 } else {
1198 reset_requested = 1;
1200 if (cpu_single_env) {
1201 cpu_single_env->stopped = 1;
1202 cpu_exit(cpu_single_env);
1204 qemu_notify_event();
1207 void qemu_system_shutdown_request(void)
1209 shutdown_requested = 1;
1210 qemu_notify_event();
1213 void qemu_system_powerdown_request(void)
1215 powerdown_requested = 1;
1216 qemu_notify_event();
1219 void main_loop_wait(int nonblocking)
1221 IOHandlerRecord *ioh;
1222 fd_set rfds, wfds, xfds;
1223 int ret, nfds;
1224 struct timeval tv;
1225 int timeout;
1227 if (nonblocking)
1228 timeout = 0;
1229 else {
1230 timeout = qemu_calculate_timeout();
1231 qemu_bh_update_timeout(&timeout);
1234 os_host_main_loop_wait(&timeout);
1236 /* poll any events */
1237 /* XXX: separate device handlers from system ones */
1238 nfds = -1;
1239 FD_ZERO(&rfds);
1240 FD_ZERO(&wfds);
1241 FD_ZERO(&xfds);
1242 QLIST_FOREACH(ioh, &io_handlers, next) {
1243 if (ioh->deleted)
1244 continue;
1245 if (ioh->fd_read &&
1246 (!ioh->fd_read_poll ||
1247 ioh->fd_read_poll(ioh->opaque) != 0)) {
1248 FD_SET(ioh->fd, &rfds);
1249 if (ioh->fd > nfds)
1250 nfds = ioh->fd;
1252 if (ioh->fd_write) {
1253 FD_SET(ioh->fd, &wfds);
1254 if (ioh->fd > nfds)
1255 nfds = ioh->fd;
1259 tv.tv_sec = timeout / 1000;
1260 tv.tv_usec = (timeout % 1000) * 1000;
1262 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1264 qemu_mutex_unlock_iothread();
1265 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1266 qemu_mutex_lock_iothread();
1267 if (ret > 0) {
1268 IOHandlerRecord *pioh;
1270 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1271 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1272 ioh->fd_read(ioh->opaque);
1273 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
1274 FD_CLR(ioh->fd, &rfds);
1276 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1277 ioh->fd_write(ioh->opaque);
1280 /* Do this last in case read/write handlers marked it for deletion */
1281 if (ioh->deleted) {
1282 QLIST_REMOVE(ioh, next);
1283 qemu_free(ioh);
1288 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1290 qemu_run_all_timers();
1292 /* Check bottom-halves last in case any of the earlier events triggered
1293 them. */
1294 qemu_bh_poll();
1298 static int vm_can_run(void)
1300 if (powerdown_requested)
1301 return 0;
1302 if (reset_requested)
1303 return 0;
1304 if (shutdown_requested)
1305 return 0;
1306 if (debug_requested)
1307 return 0;
1308 return 1;
1311 qemu_irq qemu_system_powerdown;
1313 static void main_loop(void)
1315 int r;
1317 if (kvm_enabled()) {
1318 kvm_main_loop();
1319 cpu_disable_ticks();
1320 return;
1323 qemu_main_loop_start();
1325 for (;;) {
1326 do {
1327 bool nonblocking = false;
1328 #ifdef CONFIG_PROFILER
1329 int64_t ti;
1330 #endif
1331 #ifndef CONFIG_IOTHREAD
1332 nonblocking = cpu_exec_all();
1333 #endif
1334 #ifdef CONFIG_PROFILER
1335 ti = profile_getclock();
1336 #endif
1337 main_loop_wait(nonblocking);
1338 #ifdef CONFIG_PROFILER
1339 dev_time += profile_getclock() - ti;
1340 #endif
1341 } while (vm_can_run());
1343 if ((r = qemu_debug_requested())) {
1344 vm_stop(r);
1346 if (qemu_shutdown_requested()) {
1347 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1348 if (no_shutdown) {
1349 vm_stop(0);
1350 no_shutdown = 0;
1351 } else
1352 break;
1354 if (qemu_reset_requested()) {
1355 pause_all_vcpus();
1356 qemu_system_reset();
1357 resume_all_vcpus();
1359 if (qemu_powerdown_requested()) {
1360 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1361 qemu_irq_raise(qemu_system_powerdown);
1363 if ((r = qemu_vmstop_requested())) {
1364 vm_stop(r);
1367 bdrv_close_all();
1368 pause_all_vcpus();
1371 static void version(void)
1373 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1376 static void help(int exitcode)
1378 const char *options_help =
1379 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1380 opt_help
1381 #define DEFHEADING(text) stringify(text) "\n"
1382 #include "qemu-options.def"
1383 #undef DEF
1384 #undef DEFHEADING
1385 #undef GEN_DOCS
1387 version();
1388 printf("usage: %s [options] [disk_image]\n"
1389 "\n"
1390 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1391 "\n"
1392 "%s\n"
1393 "During emulation, the following keys are useful:\n"
1394 "ctrl-alt-f toggle full screen\n"
1395 "ctrl-alt-n switch to virtual console 'n'\n"
1396 "ctrl-alt toggle mouse and keyboard grab\n"
1397 "\n"
1398 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1399 "qemu",
1400 options_help);
1401 exit(exitcode);
1404 #define HAS_ARG 0x0001
1406 typedef struct QEMUOption {
1407 const char *name;
1408 int flags;
1409 int index;
1410 uint32_t arch_mask;
1411 } QEMUOption;
1413 static const QEMUOption qemu_options[] = {
1414 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1415 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1416 { option, opt_arg, opt_enum, arch_mask },
1417 #define DEFHEADING(text)
1418 #include "qemu-options.def"
1419 #undef DEF
1420 #undef DEFHEADING
1421 #undef GEN_DOCS
1422 { NULL },
1424 static void select_vgahw (const char *p)
1426 const char *opts;
1428 default_vga = 0;
1429 vga_interface_type = VGA_NONE;
1430 if (strstart(p, "std", &opts)) {
1431 vga_interface_type = VGA_STD;
1432 } else if (strstart(p, "cirrus", &opts)) {
1433 vga_interface_type = VGA_CIRRUS;
1434 } else if (strstart(p, "vmware", &opts)) {
1435 vga_interface_type = VGA_VMWARE;
1436 } else if (strstart(p, "xenfb", &opts)) {
1437 vga_interface_type = VGA_XENFB;
1438 } else if (!strstart(p, "none", &opts)) {
1439 invalid_vga:
1440 fprintf(stderr, "Unknown vga type: %s\n", p);
1441 exit(1);
1443 while (*opts) {
1444 const char *nextopt;
1446 if (strstart(opts, ",retrace=", &nextopt)) {
1447 opts = nextopt;
1448 if (strstart(opts, "dumb", &nextopt))
1449 vga_retrace_method = VGA_RETRACE_DUMB;
1450 else if (strstart(opts, "precise", &nextopt))
1451 vga_retrace_method = VGA_RETRACE_PRECISE;
1452 else goto invalid_vga;
1453 } else goto invalid_vga;
1454 opts = nextopt;
1458 static int balloon_parse(const char *arg)
1460 QemuOpts *opts;
1462 if (strcmp(arg, "none") == 0) {
1463 return 0;
1466 if (!strncmp(arg, "virtio", 6)) {
1467 if (arg[6] == ',') {
1468 /* have params -> parse them */
1469 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1470 if (!opts)
1471 return -1;
1472 } else {
1473 /* create empty opts */
1474 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1476 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1477 return 0;
1480 return -1;
1483 char *qemu_find_file(int type, const char *name)
1485 int len;
1486 const char *subdir;
1487 char *buf;
1489 /* If name contains path separators then try it as a straight path. */
1490 if ((strchr(name, '/') || strchr(name, '\\'))
1491 && access(name, R_OK) == 0) {
1492 return qemu_strdup(name);
1494 switch (type) {
1495 case QEMU_FILE_TYPE_BIOS:
1496 subdir = "";
1497 break;
1498 case QEMU_FILE_TYPE_KEYMAP:
1499 subdir = "keymaps/";
1500 break;
1501 default:
1502 abort();
1504 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1505 buf = qemu_mallocz(len);
1506 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1507 if (access(buf, R_OK)) {
1508 qemu_free(buf);
1509 return NULL;
1511 return buf;
1514 static int device_help_func(QemuOpts *opts, void *opaque)
1516 return qdev_device_help(opts);
1519 static int device_init_func(QemuOpts *opts, void *opaque)
1521 DeviceState *dev;
1523 dev = qdev_device_add(opts);
1524 if (!dev)
1525 return -1;
1526 return 0;
1529 static int chardev_init_func(QemuOpts *opts, void *opaque)
1531 CharDriverState *chr;
1533 chr = qemu_chr_open_opts(opts, NULL);
1534 if (!chr)
1535 return -1;
1536 return 0;
1539 #ifdef CONFIG_VIRTFS
1540 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1542 int ret;
1543 ret = qemu_fsdev_add(opts);
1545 return ret;
1547 #endif
1549 static int mon_init_func(QemuOpts *opts, void *opaque)
1551 CharDriverState *chr;
1552 const char *chardev;
1553 const char *mode;
1554 int flags;
1556 mode = qemu_opt_get(opts, "mode");
1557 if (mode == NULL) {
1558 mode = "readline";
1560 if (strcmp(mode, "readline") == 0) {
1561 flags = MONITOR_USE_READLINE;
1562 } else if (strcmp(mode, "control") == 0) {
1563 flags = MONITOR_USE_CONTROL;
1564 } else {
1565 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1566 exit(1);
1569 if (qemu_opt_get_bool(opts, "pretty", 0))
1570 flags |= MONITOR_USE_PRETTY;
1572 if (qemu_opt_get_bool(opts, "default", 0))
1573 flags |= MONITOR_IS_DEFAULT;
1575 chardev = qemu_opt_get(opts, "chardev");
1576 chr = qemu_chr_find(chardev);
1577 if (chr == NULL) {
1578 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1579 exit(1);
1582 monitor_init(chr, flags);
1583 return 0;
1586 static void monitor_parse(const char *optarg, const char *mode)
1588 static int monitor_device_index = 0;
1589 QemuOpts *opts;
1590 const char *p;
1591 char label[32];
1592 int def = 0;
1594 if (strstart(optarg, "chardev:", &p)) {
1595 snprintf(label, sizeof(label), "%s", p);
1596 } else {
1597 snprintf(label, sizeof(label), "compat_monitor%d",
1598 monitor_device_index);
1599 if (monitor_device_index == 0) {
1600 def = 1;
1602 opts = qemu_chr_parse_compat(label, optarg);
1603 if (!opts) {
1604 fprintf(stderr, "parse error: %s\n", optarg);
1605 exit(1);
1609 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1610 if (!opts) {
1611 fprintf(stderr, "duplicate chardev: %s\n", label);
1612 exit(1);
1614 qemu_opt_set(opts, "mode", mode);
1615 qemu_opt_set(opts, "chardev", label);
1616 if (def)
1617 qemu_opt_set(opts, "default", "on");
1618 monitor_device_index++;
1621 struct device_config {
1622 enum {
1623 DEV_USB, /* -usbdevice */
1624 DEV_BT, /* -bt */
1625 DEV_SERIAL, /* -serial */
1626 DEV_PARALLEL, /* -parallel */
1627 DEV_VIRTCON, /* -virtioconsole */
1628 DEV_DEBUGCON, /* -debugcon */
1629 } type;
1630 const char *cmdline;
1631 QTAILQ_ENTRY(device_config) next;
1633 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1635 static void add_device_config(int type, const char *cmdline)
1637 struct device_config *conf;
1639 conf = qemu_mallocz(sizeof(*conf));
1640 conf->type = type;
1641 conf->cmdline = cmdline;
1642 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1645 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1647 struct device_config *conf;
1648 int rc;
1650 QTAILQ_FOREACH(conf, &device_configs, next) {
1651 if (conf->type != type)
1652 continue;
1653 rc = func(conf->cmdline);
1654 if (0 != rc)
1655 return rc;
1657 return 0;
1660 static int serial_parse(const char *devname)
1662 static int index = 0;
1663 char label[32];
1665 if (strcmp(devname, "none") == 0)
1666 return 0;
1667 if (index == MAX_SERIAL_PORTS) {
1668 fprintf(stderr, "qemu: too many serial ports\n");
1669 exit(1);
1671 snprintf(label, sizeof(label), "serial%d", index);
1672 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1673 if (!serial_hds[index]) {
1674 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1675 devname, strerror(errno));
1676 return -1;
1678 index++;
1679 return 0;
1682 static int parallel_parse(const char *devname)
1684 static int index = 0;
1685 char label[32];
1687 if (strcmp(devname, "none") == 0)
1688 return 0;
1689 if (index == MAX_PARALLEL_PORTS) {
1690 fprintf(stderr, "qemu: too many parallel ports\n");
1691 exit(1);
1693 snprintf(label, sizeof(label), "parallel%d", index);
1694 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1695 if (!parallel_hds[index]) {
1696 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1697 devname, strerror(errno));
1698 return -1;
1700 index++;
1701 return 0;
1704 static int virtcon_parse(const char *devname)
1706 QemuOptsList *device = qemu_find_opts("device");
1707 static int index = 0;
1708 char label[32];
1709 QemuOpts *bus_opts, *dev_opts;
1711 if (strcmp(devname, "none") == 0)
1712 return 0;
1713 if (index == MAX_VIRTIO_CONSOLES) {
1714 fprintf(stderr, "qemu: too many virtio consoles\n");
1715 exit(1);
1718 bus_opts = qemu_opts_create(device, NULL, 0);
1719 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1721 dev_opts = qemu_opts_create(device, NULL, 0);
1722 qemu_opt_set(dev_opts, "driver", "virtconsole");
1724 snprintf(label, sizeof(label), "virtcon%d", index);
1725 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1726 if (!virtcon_hds[index]) {
1727 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1728 devname, strerror(errno));
1729 return -1;
1731 qemu_opt_set(dev_opts, "chardev", label);
1733 index++;
1734 return 0;
1737 static int debugcon_parse(const char *devname)
1739 QemuOpts *opts;
1741 if (!qemu_chr_open("debugcon", devname, NULL)) {
1742 exit(1);
1744 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1745 if (!opts) {
1746 fprintf(stderr, "qemu: already have a debugcon device\n");
1747 exit(1);
1749 qemu_opt_set(opts, "driver", "isa-debugcon");
1750 qemu_opt_set(opts, "chardev", "debugcon");
1751 return 0;
1754 void qemu_add_exit_notifier(Notifier *notify)
1756 notifier_list_add(&exit_notifiers, notify);
1759 void qemu_remove_exit_notifier(Notifier *notify)
1761 notifier_list_remove(&exit_notifiers, notify);
1764 static void qemu_run_exit_notifiers(void)
1766 notifier_list_notify(&exit_notifiers);
1769 static const QEMUOption *lookup_opt(int argc, char **argv,
1770 const char **poptarg, int *poptind)
1772 const QEMUOption *popt;
1773 int optind = *poptind;
1774 char *r = argv[optind];
1775 const char *optarg;
1777 loc_set_cmdline(argv, optind, 1);
1778 optind++;
1779 /* Treat --foo the same as -foo. */
1780 if (r[1] == '-')
1781 r++;
1782 popt = qemu_options;
1783 for(;;) {
1784 if (!popt->name) {
1785 error_report("invalid option");
1786 exit(1);
1788 if (!strcmp(popt->name, r + 1))
1789 break;
1790 popt++;
1792 if (popt->flags & HAS_ARG) {
1793 if (optind >= argc) {
1794 error_report("requires an argument");
1795 exit(1);
1797 optarg = argv[optind++];
1798 loc_set_cmdline(argv, optind - 2, 2);
1799 } else {
1800 optarg = NULL;
1803 *poptarg = optarg;
1804 *poptind = optind;
1806 return popt;
1809 int main(int argc, char **argv, char **envp)
1811 const char *gdbstub_dev = NULL;
1812 int i;
1813 int snapshot, linux_boot;
1814 const char *icount_option = NULL;
1815 const char *initrd_filename;
1816 const char *kernel_filename, *kernel_cmdline;
1817 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1818 DisplayState *ds;
1819 DisplayChangeListener *dcl;
1820 int cyls, heads, secs, translation;
1821 QemuOpts *hda_opts = NULL, *opts;
1822 QemuOptsList *olist;
1823 int optind;
1824 const char *optarg;
1825 const char *loadvm = NULL;
1826 QEMUMachine *machine;
1827 const char *cpu_model;
1828 int tb_size;
1829 const char *pid_file = NULL;
1830 const char *incoming = NULL;
1831 int show_vnc_port = 0;
1832 int defconfig = 1;
1834 #ifdef CONFIG_SIMPLE_TRACE
1835 const char *trace_file = NULL;
1836 #endif
1837 atexit(qemu_run_exit_notifiers);
1838 error_set_progname(argv[0]);
1840 init_clocks();
1842 qemu_cache_utils_init(envp);
1844 QLIST_INIT (&vm_change_state_head);
1845 os_setup_early_signal_handling();
1847 module_call_init(MODULE_INIT_MACHINE);
1848 machine = find_default_machine();
1849 cpu_model = NULL;
1850 initrd_filename = NULL;
1851 ram_size = 0;
1852 snapshot = 0;
1853 kernel_filename = NULL;
1854 kernel_cmdline = "";
1855 cyls = heads = secs = 0;
1856 translation = BIOS_ATA_TRANSLATION_AUTO;
1858 for (i = 0; i < MAX_NODES; i++) {
1859 node_mem[i] = 0;
1860 node_cpumask[i] = 0;
1863 nb_numa_nodes = 0;
1864 nb_nics = 0;
1866 tb_size = 0;
1867 autostart= 1;
1869 /* first pass of option parsing */
1870 optind = 1;
1871 while (optind < argc) {
1872 if (argv[optind][0] != '-') {
1873 /* disk image */
1874 optind++;
1875 continue;
1876 } else {
1877 const QEMUOption *popt;
1879 popt = lookup_opt(argc, argv, &optarg, &optind);
1880 switch (popt->index) {
1881 case QEMU_OPTION_nodefconfig:
1882 defconfig=0;
1883 break;
1888 if (defconfig) {
1889 int ret;
1891 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1892 if (ret < 0 && ret != -ENOENT) {
1893 exit(1);
1896 ret = qemu_read_config_file(arch_config_name);
1897 if (ret < 0 && ret != -ENOENT) {
1898 exit(1);
1901 cpudef_init();
1903 /* second pass of option parsing */
1904 optind = 1;
1905 for(;;) {
1906 if (optind >= argc)
1907 break;
1908 if (argv[optind][0] != '-') {
1909 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1910 } else {
1911 const QEMUOption *popt;
1913 popt = lookup_opt(argc, argv, &optarg, &optind);
1914 if (!(popt->arch_mask & arch_type)) {
1915 printf("Option %s not supported for this target\n", popt->name);
1916 exit(1);
1918 switch(popt->index) {
1919 case QEMU_OPTION_M:
1920 machine = find_machine(optarg);
1921 if (!machine) {
1922 QEMUMachine *m;
1923 printf("Supported machines are:\n");
1924 for(m = first_machine; m != NULL; m = m->next) {
1925 if (m->alias)
1926 printf("%-10s %s (alias of %s)\n",
1927 m->alias, m->desc, m->name);
1928 printf("%-10s %s%s\n",
1929 m->name, m->desc,
1930 m->is_default ? " (default)" : "");
1932 exit(*optarg != '?');
1934 break;
1935 case QEMU_OPTION_cpu:
1936 /* hw initialization will check this */
1937 if (*optarg == '?') {
1938 list_cpus(stdout, &fprintf, optarg);
1939 exit(0);
1940 } else {
1941 cpu_model = optarg;
1943 break;
1944 case QEMU_OPTION_initrd:
1945 initrd_filename = optarg;
1946 break;
1947 case QEMU_OPTION_hda:
1948 if (cyls == 0)
1949 hda_opts = drive_add(optarg, HD_ALIAS, 0);
1950 else
1951 hda_opts = drive_add(optarg, HD_ALIAS
1952 ",cyls=%d,heads=%d,secs=%d%s",
1953 0, cyls, heads, secs,
1954 translation == BIOS_ATA_TRANSLATION_LBA ?
1955 ",trans=lba" :
1956 translation == BIOS_ATA_TRANSLATION_NONE ?
1957 ",trans=none" : "");
1958 break;
1959 case QEMU_OPTION_hdb:
1960 case QEMU_OPTION_hdc:
1961 case QEMU_OPTION_hdd:
1962 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1963 break;
1964 case QEMU_OPTION_drive:
1965 drive_add(NULL, "%s", optarg);
1966 break;
1967 case QEMU_OPTION_set:
1968 if (qemu_set_option(optarg) != 0)
1969 exit(1);
1970 break;
1971 case QEMU_OPTION_global:
1972 if (qemu_global_option(optarg) != 0)
1973 exit(1);
1974 break;
1975 case QEMU_OPTION_mtdblock:
1976 drive_add(optarg, MTD_ALIAS);
1977 break;
1978 case QEMU_OPTION_sd:
1979 drive_add(optarg, SD_ALIAS);
1980 break;
1981 case QEMU_OPTION_pflash:
1982 drive_add(optarg, PFLASH_ALIAS);
1983 break;
1984 case QEMU_OPTION_snapshot:
1985 snapshot = 1;
1986 break;
1987 case QEMU_OPTION_hdachs:
1989 const char *p;
1990 p = optarg;
1991 cyls = strtol(p, (char **)&p, 0);
1992 if (cyls < 1 || cyls > 16383)
1993 goto chs_fail;
1994 if (*p != ',')
1995 goto chs_fail;
1996 p++;
1997 heads = strtol(p, (char **)&p, 0);
1998 if (heads < 1 || heads > 16)
1999 goto chs_fail;
2000 if (*p != ',')
2001 goto chs_fail;
2002 p++;
2003 secs = strtol(p, (char **)&p, 0);
2004 if (secs < 1 || secs > 63)
2005 goto chs_fail;
2006 if (*p == ',') {
2007 p++;
2008 if (!strcmp(p, "none"))
2009 translation = BIOS_ATA_TRANSLATION_NONE;
2010 else if (!strcmp(p, "lba"))
2011 translation = BIOS_ATA_TRANSLATION_LBA;
2012 else if (!strcmp(p, "auto"))
2013 translation = BIOS_ATA_TRANSLATION_AUTO;
2014 else
2015 goto chs_fail;
2016 } else if (*p != '\0') {
2017 chs_fail:
2018 fprintf(stderr, "qemu: invalid physical CHS format\n");
2019 exit(1);
2021 if (hda_opts != NULL) {
2022 char num[16];
2023 snprintf(num, sizeof(num), "%d", cyls);
2024 qemu_opt_set(hda_opts, "cyls", num);
2025 snprintf(num, sizeof(num), "%d", heads);
2026 qemu_opt_set(hda_opts, "heads", num);
2027 snprintf(num, sizeof(num), "%d", secs);
2028 qemu_opt_set(hda_opts, "secs", num);
2029 if (translation == BIOS_ATA_TRANSLATION_LBA)
2030 qemu_opt_set(hda_opts, "trans", "lba");
2031 if (translation == BIOS_ATA_TRANSLATION_NONE)
2032 qemu_opt_set(hda_opts, "trans", "none");
2035 break;
2036 case QEMU_OPTION_numa:
2037 if (nb_numa_nodes >= MAX_NODES) {
2038 fprintf(stderr, "qemu: too many NUMA nodes\n");
2039 exit(1);
2041 numa_add(optarg);
2042 break;
2043 case QEMU_OPTION_nographic:
2044 display_type = DT_NOGRAPHIC;
2045 break;
2046 #ifdef CONFIG_CURSES
2047 case QEMU_OPTION_curses:
2048 display_type = DT_CURSES;
2049 break;
2050 #endif
2051 case QEMU_OPTION_portrait:
2052 graphic_rotate = 1;
2053 break;
2054 case QEMU_OPTION_kernel:
2055 kernel_filename = optarg;
2056 break;
2057 case QEMU_OPTION_append:
2058 kernel_cmdline = optarg;
2059 break;
2060 case QEMU_OPTION_cdrom:
2061 drive_add(optarg, CDROM_ALIAS);
2062 break;
2063 case QEMU_OPTION_boot:
2065 static const char * const params[] = {
2066 "order", "once", "menu", NULL
2068 char buf[sizeof(boot_devices)];
2069 char *standard_boot_devices;
2070 int legacy = 0;
2072 if (!strchr(optarg, '=')) {
2073 legacy = 1;
2074 pstrcpy(buf, sizeof(buf), optarg);
2075 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2076 fprintf(stderr,
2077 "qemu: unknown boot parameter '%s' in '%s'\n",
2078 buf, optarg);
2079 exit(1);
2082 if (legacy ||
2083 get_param_value(buf, sizeof(buf), "order", optarg)) {
2084 validate_bootdevices(buf);
2085 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2087 if (!legacy) {
2088 if (get_param_value(buf, sizeof(buf),
2089 "once", optarg)) {
2090 validate_bootdevices(buf);
2091 standard_boot_devices = qemu_strdup(boot_devices);
2092 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2093 qemu_register_reset(restore_boot_devices,
2094 standard_boot_devices);
2096 if (get_param_value(buf, sizeof(buf),
2097 "menu", optarg)) {
2098 if (!strcmp(buf, "on")) {
2099 boot_menu = 1;
2100 } else if (!strcmp(buf, "off")) {
2101 boot_menu = 0;
2102 } else {
2103 fprintf(stderr,
2104 "qemu: invalid option value '%s'\n",
2105 buf);
2106 exit(1);
2111 break;
2112 case QEMU_OPTION_fda:
2113 case QEMU_OPTION_fdb:
2114 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2115 break;
2116 case QEMU_OPTION_no_fd_bootchk:
2117 fd_bootchk = 0;
2118 break;
2119 case QEMU_OPTION_netdev:
2120 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2121 exit(1);
2123 break;
2124 case QEMU_OPTION_net:
2125 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2126 exit(1);
2128 break;
2129 #ifdef CONFIG_SLIRP
2130 case QEMU_OPTION_tftp:
2131 legacy_tftp_prefix = optarg;
2132 break;
2133 case QEMU_OPTION_bootp:
2134 legacy_bootp_filename = optarg;
2135 break;
2136 case QEMU_OPTION_redir:
2137 if (net_slirp_redir(optarg) < 0)
2138 exit(1);
2139 break;
2140 #endif
2141 case QEMU_OPTION_bt:
2142 add_device_config(DEV_BT, optarg);
2143 break;
2144 case QEMU_OPTION_audio_help:
2145 if (!(audio_available())) {
2146 printf("Option %s not supported for this target\n", popt->name);
2147 exit(1);
2149 AUD_help ();
2150 exit (0);
2151 break;
2152 case QEMU_OPTION_soundhw:
2153 if (!(audio_available())) {
2154 printf("Option %s not supported for this target\n", popt->name);
2155 exit(1);
2157 select_soundhw (optarg);
2158 break;
2159 case QEMU_OPTION_h:
2160 help(0);
2161 break;
2162 case QEMU_OPTION_version:
2163 version();
2164 exit(0);
2165 break;
2166 case QEMU_OPTION_m: {
2167 ssize_t value;
2169 value = strtosz(optarg, NULL);
2170 if (value < 0) {
2171 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2172 exit(1);
2175 /* On 32-bit hosts, QEMU is limited by virtual address space */
2176 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2177 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2178 exit(1);
2180 if (value != (uint64_t)(ram_addr_t)value) {
2181 fprintf(stderr, "qemu: ram size too large\n");
2182 exit(1);
2184 ram_size = value;
2185 break;
2187 case QEMU_OPTION_mempath:
2188 mem_path = optarg;
2189 break;
2190 #ifdef MAP_POPULATE
2191 case QEMU_OPTION_mem_prealloc:
2192 mem_prealloc = 1;
2193 break;
2194 #endif
2195 case QEMU_OPTION_d:
2196 set_cpu_log(optarg);
2197 break;
2198 case QEMU_OPTION_s:
2199 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2200 break;
2201 case QEMU_OPTION_gdb:
2202 gdbstub_dev = optarg;
2203 break;
2204 case QEMU_OPTION_L:
2205 data_dir = optarg;
2206 break;
2207 case QEMU_OPTION_bios:
2208 bios_name = optarg;
2209 break;
2210 case QEMU_OPTION_singlestep:
2211 singlestep = 1;
2212 break;
2213 case QEMU_OPTION_S:
2214 autostart = 0;
2215 break;
2216 case QEMU_OPTION_k:
2217 keyboard_layout = optarg;
2218 break;
2219 case QEMU_OPTION_localtime:
2220 rtc_utc = 0;
2221 break;
2222 case QEMU_OPTION_vga:
2223 select_vgahw (optarg);
2224 break;
2225 case QEMU_OPTION_g:
2227 const char *p;
2228 int w, h, depth;
2229 p = optarg;
2230 w = strtol(p, (char **)&p, 10);
2231 if (w <= 0) {
2232 graphic_error:
2233 fprintf(stderr, "qemu: invalid resolution or depth\n");
2234 exit(1);
2236 if (*p != 'x')
2237 goto graphic_error;
2238 p++;
2239 h = strtol(p, (char **)&p, 10);
2240 if (h <= 0)
2241 goto graphic_error;
2242 if (*p == 'x') {
2243 p++;
2244 depth = strtol(p, (char **)&p, 10);
2245 if (depth != 8 && depth != 15 && depth != 16 &&
2246 depth != 24 && depth != 32)
2247 goto graphic_error;
2248 } else if (*p == '\0') {
2249 depth = graphic_depth;
2250 } else {
2251 goto graphic_error;
2254 graphic_width = w;
2255 graphic_height = h;
2256 graphic_depth = depth;
2258 break;
2259 case QEMU_OPTION_echr:
2261 char *r;
2262 term_escape_char = strtol(optarg, &r, 0);
2263 if (r == optarg)
2264 printf("Bad argument to echr\n");
2265 break;
2267 case QEMU_OPTION_monitor:
2268 monitor_parse(optarg, "readline");
2269 default_monitor = 0;
2270 break;
2271 case QEMU_OPTION_qmp:
2272 monitor_parse(optarg, "control");
2273 default_monitor = 0;
2274 break;
2275 case QEMU_OPTION_mon:
2276 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2277 if (!opts) {
2278 exit(1);
2280 default_monitor = 0;
2281 break;
2282 case QEMU_OPTION_chardev:
2283 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2284 if (!opts) {
2285 exit(1);
2287 break;
2288 case QEMU_OPTION_fsdev:
2289 olist = qemu_find_opts("fsdev");
2290 if (!olist) {
2291 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2292 exit(1);
2294 opts = qemu_opts_parse(olist, optarg, 1);
2295 if (!opts) {
2296 fprintf(stderr, "parse error: %s\n", optarg);
2297 exit(1);
2299 break;
2300 case QEMU_OPTION_virtfs: {
2301 char *arg_fsdev = NULL;
2302 char *arg_9p = NULL;
2303 int len = 0;
2305 olist = qemu_find_opts("virtfs");
2306 if (!olist) {
2307 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2308 exit(1);
2310 opts = qemu_opts_parse(olist, optarg, 1);
2311 if (!opts) {
2312 fprintf(stderr, "parse error: %s\n", optarg);
2313 exit(1);
2316 if (qemu_opt_get(opts, "fstype") == NULL ||
2317 qemu_opt_get(opts, "mount_tag") == NULL ||
2318 qemu_opt_get(opts, "path") == NULL ||
2319 qemu_opt_get(opts, "security_model") == NULL) {
2320 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2321 "security_model=[mapped|passthrough|none],"
2322 "mnt_tag=tag.\n");
2323 exit(1);
2326 len = strlen(",id=,path=,security_model=");
2327 len += strlen(qemu_opt_get(opts, "fstype"));
2328 len += strlen(qemu_opt_get(opts, "mount_tag"));
2329 len += strlen(qemu_opt_get(opts, "path"));
2330 len += strlen(qemu_opt_get(opts, "security_model"));
2331 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2333 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2334 "%s,id=%s,path=%s,security_model=%s",
2335 qemu_opt_get(opts, "fstype"),
2336 qemu_opt_get(opts, "mount_tag"),
2337 qemu_opt_get(opts, "path"),
2338 qemu_opt_get(opts, "security_model"));
2340 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2341 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2342 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2344 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2345 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2346 qemu_opt_get(opts, "mount_tag"),
2347 qemu_opt_get(opts, "mount_tag"));
2349 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2350 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2351 exit(1);
2354 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2355 fprintf(stderr, "parse error [device]: %s\n", optarg);
2356 exit(1);
2359 qemu_free(arg_fsdev);
2360 qemu_free(arg_9p);
2361 break;
2363 case QEMU_OPTION_serial:
2364 add_device_config(DEV_SERIAL, optarg);
2365 default_serial = 0;
2366 if (strncmp(optarg, "mon:", 4) == 0) {
2367 default_monitor = 0;
2369 break;
2370 case QEMU_OPTION_watchdog:
2371 if (watchdog) {
2372 fprintf(stderr,
2373 "qemu: only one watchdog option may be given\n");
2374 return 1;
2376 watchdog = optarg;
2377 break;
2378 case QEMU_OPTION_watchdog_action:
2379 if (select_watchdog_action(optarg) == -1) {
2380 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2381 exit(1);
2383 break;
2384 case QEMU_OPTION_virtiocon:
2385 add_device_config(DEV_VIRTCON, optarg);
2386 default_virtcon = 0;
2387 if (strncmp(optarg, "mon:", 4) == 0) {
2388 default_monitor = 0;
2390 break;
2391 case QEMU_OPTION_parallel:
2392 add_device_config(DEV_PARALLEL, optarg);
2393 default_parallel = 0;
2394 if (strncmp(optarg, "mon:", 4) == 0) {
2395 default_monitor = 0;
2397 break;
2398 case QEMU_OPTION_debugcon:
2399 add_device_config(DEV_DEBUGCON, optarg);
2400 break;
2401 case QEMU_OPTION_loadvm:
2402 loadvm = optarg;
2403 break;
2404 case QEMU_OPTION_full_screen:
2405 full_screen = 1;
2406 break;
2407 #ifdef CONFIG_SDL
2408 case QEMU_OPTION_no_frame:
2409 no_frame = 1;
2410 break;
2411 case QEMU_OPTION_alt_grab:
2412 alt_grab = 1;
2413 break;
2414 case QEMU_OPTION_ctrl_grab:
2415 ctrl_grab = 1;
2416 break;
2417 case QEMU_OPTION_no_quit:
2418 no_quit = 1;
2419 break;
2420 case QEMU_OPTION_sdl:
2421 display_type = DT_SDL;
2422 break;
2423 #endif
2424 case QEMU_OPTION_pidfile:
2425 pid_file = optarg;
2426 break;
2427 case QEMU_OPTION_win2k_hack:
2428 win2k_install_hack = 1;
2429 break;
2430 case QEMU_OPTION_rtc_td_hack:
2431 rtc_td_hack = 1;
2432 break;
2433 case QEMU_OPTION_acpitable:
2434 do_acpitable_option(optarg);
2435 break;
2436 case QEMU_OPTION_smbios:
2437 do_smbios_option(optarg);
2438 break;
2439 #ifdef OBSOLETE_KVM_IMPL
2440 case QEMU_OPTION_enable_kvm:
2441 kvm_allowed = 1;
2442 #endif
2443 break;
2444 case QEMU_OPTION_no_kvm:
2445 kvm_allowed = 0;
2446 #ifdef CONFIG_NO_CPU_EMULATION
2447 fprintf(stderr, "cpu emulation not configured\n");
2448 exit(1);
2449 #endif
2450 break;
2451 #ifdef CONFIG_KVM
2452 case QEMU_OPTION_no_kvm_irqchip: {
2453 kvm_irqchip = 0;
2454 kvm_pit = 0;
2455 break;
2457 case QEMU_OPTION_no_kvm_pit: {
2458 kvm_pit = 0;
2459 break;
2461 case QEMU_OPTION_no_kvm_pit_reinjection: {
2462 kvm_pit_reinject = 0;
2463 break;
2465 case QEMU_OPTION_enable_nesting: {
2466 kvm_nested = 1;
2467 break;
2469 #endif
2470 case QEMU_OPTION_usb:
2471 usb_enabled = 1;
2472 break;
2473 case QEMU_OPTION_usbdevice:
2474 usb_enabled = 1;
2475 add_device_config(DEV_USB, optarg);
2476 break;
2477 case QEMU_OPTION_device:
2478 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2479 exit(1);
2481 break;
2482 case QEMU_OPTION_smp:
2483 smp_parse(optarg);
2484 if (smp_cpus < 1) {
2485 fprintf(stderr, "Invalid number of CPUs\n");
2486 exit(1);
2488 if (max_cpus < smp_cpus) {
2489 fprintf(stderr, "maxcpus must be equal to or greater than "
2490 "smp\n");
2491 exit(1);
2493 if (max_cpus > 255) {
2494 fprintf(stderr, "Unsupported number of maxcpus\n");
2495 exit(1);
2497 break;
2498 case QEMU_OPTION_vnc:
2499 display_remote++;
2500 vnc_display = optarg;
2501 break;
2502 case QEMU_OPTION_no_acpi:
2503 acpi_enabled = 0;
2504 break;
2505 case QEMU_OPTION_no_hpet:
2506 no_hpet = 1;
2507 break;
2508 case QEMU_OPTION_balloon:
2509 if (balloon_parse(optarg) < 0) {
2510 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2511 exit(1);
2513 break;
2514 case QEMU_OPTION_no_reboot:
2515 no_reboot = 1;
2516 break;
2517 case QEMU_OPTION_no_shutdown:
2518 no_shutdown = 1;
2519 break;
2520 case QEMU_OPTION_show_cursor:
2521 cursor_hide = 0;
2522 break;
2523 case QEMU_OPTION_uuid:
2524 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2525 fprintf(stderr, "Fail to parse UUID string."
2526 " Wrong format.\n");
2527 exit(1);
2529 break;
2530 case QEMU_OPTION_option_rom:
2531 if (nb_option_roms >= MAX_OPTION_ROMS) {
2532 fprintf(stderr, "Too many option ROMs\n");
2533 exit(1);
2535 option_rom[nb_option_roms] = optarg;
2536 nb_option_roms++;
2537 break;
2538 case QEMU_OPTION_semihosting:
2539 semihosting_enabled = 1;
2540 break;
2541 case QEMU_OPTION_tdf:
2542 time_drift_fix = 1;
2543 break;
2544 case QEMU_OPTION_kvm_shadow_memory:
2545 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
2546 break;
2547 case QEMU_OPTION_name:
2548 qemu_name = qemu_strdup(optarg);
2550 char *p = strchr(qemu_name, ',');
2551 if (p != NULL) {
2552 *p++ = 0;
2553 if (strncmp(p, "process=", 8)) {
2554 fprintf(stderr, "Unknown subargument %s to -name", p);
2555 exit(1);
2557 p += 8;
2558 os_set_proc_name(p);
2561 break;
2562 case QEMU_OPTION_prom_env:
2563 if (nb_prom_envs >= MAX_PROM_ENVS) {
2564 fprintf(stderr, "Too many prom variables\n");
2565 exit(1);
2567 prom_envs[nb_prom_envs] = optarg;
2568 nb_prom_envs++;
2569 break;
2570 case QEMU_OPTION_old_param:
2571 old_param = 1;
2572 break;
2573 case QEMU_OPTION_clock:
2574 configure_alarms(optarg);
2575 break;
2576 case QEMU_OPTION_startdate:
2577 configure_rtc_date_offset(optarg, 1);
2578 break;
2579 case QEMU_OPTION_rtc:
2580 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2581 if (!opts) {
2582 exit(1);
2584 configure_rtc(opts);
2585 break;
2586 case QEMU_OPTION_tb_size:
2587 tb_size = strtol(optarg, NULL, 0);
2588 if (tb_size < 0)
2589 tb_size = 0;
2590 break;
2591 case QEMU_OPTION_icount:
2592 icount_option = optarg;
2593 break;
2594 case QEMU_OPTION_incoming:
2595 incoming = optarg;
2596 incoming_expected = true;
2597 break;
2598 case QEMU_OPTION_nodefaults:
2599 default_serial = 0;
2600 default_parallel = 0;
2601 default_virtcon = 0;
2602 default_monitor = 0;
2603 default_vga = 0;
2604 default_net = 0;
2605 default_floppy = 0;
2606 default_cdrom = 0;
2607 default_sdcard = 0;
2608 break;
2609 #ifndef _WIN32
2610 case QEMU_OPTION_nvram:
2611 nvram = optarg;
2612 break;
2613 #endif
2614 case QEMU_OPTION_xen_domid:
2615 if (!(xen_available())) {
2616 printf("Option %s not supported for this target\n", popt->name);
2617 exit(1);
2619 xen_domid = atoi(optarg);
2620 break;
2621 case QEMU_OPTION_xen_create:
2622 if (!(xen_available())) {
2623 printf("Option %s not supported for this target\n", popt->name);
2624 exit(1);
2626 xen_mode = XEN_CREATE;
2627 break;
2628 case QEMU_OPTION_xen_attach:
2629 if (!(xen_available())) {
2630 printf("Option %s not supported for this target\n", popt->name);
2631 exit(1);
2633 xen_mode = XEN_ATTACH;
2634 break;
2635 #ifdef CONFIG_SIMPLE_TRACE
2636 case QEMU_OPTION_trace:
2637 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2638 if (opts) {
2639 trace_file = qemu_opt_get(opts, "file");
2641 break;
2642 #endif
2643 case QEMU_OPTION_readconfig:
2645 int ret = qemu_read_config_file(optarg);
2646 if (ret < 0) {
2647 fprintf(stderr, "read config %s: %s\n", optarg,
2648 strerror(-ret));
2649 exit(1);
2651 break;
2653 case QEMU_OPTION_spice:
2654 olist = qemu_find_opts("spice");
2655 if (!olist) {
2656 fprintf(stderr, "spice is not supported by this qemu build.\n");
2657 exit(1);
2659 opts = qemu_opts_parse(olist, optarg, 0);
2660 if (!opts) {
2661 fprintf(stderr, "parse error: %s\n", optarg);
2662 exit(1);
2664 break;
2665 case QEMU_OPTION_writeconfig:
2667 FILE *fp;
2668 if (strcmp(optarg, "-") == 0) {
2669 fp = stdout;
2670 } else {
2671 fp = fopen(optarg, "w");
2672 if (fp == NULL) {
2673 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2674 exit(1);
2677 qemu_config_write(fp);
2678 fclose(fp);
2679 break;
2681 default:
2682 os_parse_cmd_args(popt->index, optarg);
2686 loc_set_none();
2688 /* If no data_dir is specified then try to find it relative to the
2689 executable path. */
2690 if (!data_dir) {
2691 data_dir = os_find_datadir(argv[0]);
2693 /* If all else fails use the install patch specified when building. */
2694 if (!data_dir) {
2695 data_dir = CONFIG_QEMU_DATADIR;
2698 #ifdef CONFIG_SIMPLE_TRACE
2700 * Set the trace file name, if specified.
2702 st_set_trace_file(trace_file);
2703 #endif
2705 * Default to max_cpus = smp_cpus, in case the user doesn't
2706 * specify a max_cpus value.
2708 if (!max_cpus)
2709 max_cpus = smp_cpus;
2711 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2712 if (smp_cpus > machine->max_cpus) {
2713 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2714 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2715 machine->max_cpus);
2716 exit(1);
2719 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2720 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2722 if (machine->no_serial) {
2723 default_serial = 0;
2725 if (machine->no_parallel) {
2726 default_parallel = 0;
2728 if (!machine->use_virtcon) {
2729 default_virtcon = 0;
2731 if (machine->no_vga) {
2732 default_vga = 0;
2734 if (machine->no_floppy) {
2735 default_floppy = 0;
2737 if (machine->no_cdrom) {
2738 default_cdrom = 0;
2740 if (machine->no_sdcard) {
2741 default_sdcard = 0;
2744 if (display_type == DT_NOGRAPHIC) {
2745 if (default_parallel)
2746 add_device_config(DEV_PARALLEL, "null");
2747 if (default_serial && default_monitor) {
2748 add_device_config(DEV_SERIAL, "mon:stdio");
2749 } else if (default_virtcon && default_monitor) {
2750 add_device_config(DEV_VIRTCON, "mon:stdio");
2751 } else {
2752 if (default_serial)
2753 add_device_config(DEV_SERIAL, "stdio");
2754 if (default_virtcon)
2755 add_device_config(DEV_VIRTCON, "stdio");
2756 if (default_monitor)
2757 monitor_parse("stdio", "readline");
2759 } else {
2760 if (default_serial)
2761 add_device_config(DEV_SERIAL, "vc:80Cx24C");
2762 if (default_parallel)
2763 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2764 if (default_monitor)
2765 monitor_parse("vc:80Cx24C", "readline");
2766 if (default_virtcon)
2767 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2769 if (default_vga)
2770 vga_interface_type = VGA_CIRRUS;
2772 socket_init();
2774 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2775 exit(1);
2776 #ifdef CONFIG_VIRTFS
2777 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2778 exit(1);
2780 #endif
2782 os_daemonize();
2784 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2785 os_pidfile_error();
2786 exit(1);
2789 if (kvm_allowed) {
2790 int ret = kvm_init(smp_cpus);
2791 if (ret < 0) {
2792 #if defined(OBSOLETE_KVM_IMPL) || defined(CONFIG_NO_CPU_EMULATION)
2793 if (!kvm_available()) {
2794 printf("KVM not supported for this target\n");
2795 } else {
2796 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2798 exit(1);
2799 #endif
2800 #ifdef CONFIG_KVM
2801 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
2802 kvm_allowed = 0;
2803 #endif
2807 if (qemu_init_main_loop()) {
2808 fprintf(stderr, "qemu_init_main_loop failed\n");
2809 exit(1);
2811 linux_boot = (kernel_filename != NULL);
2813 if (!linux_boot && *kernel_cmdline != '\0') {
2814 fprintf(stderr, "-append only allowed with -kernel option\n");
2815 exit(1);
2818 if (!linux_boot && initrd_filename != NULL) {
2819 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2820 exit(1);
2823 os_set_line_buffering();
2825 if (init_timer_alarm() < 0) {
2826 fprintf(stderr, "could not initialize alarm timer\n");
2827 exit(1);
2829 configure_icount(icount_option);
2831 if (net_init_clients() < 0) {
2832 exit(1);
2835 /* init the bluetooth world */
2836 if (foreach_device_config(DEV_BT, bt_parse))
2837 exit(1);
2839 /* init the memory */
2840 if (ram_size == 0)
2841 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2843 /* init the dynamic translator */
2844 cpu_exec_init_all(tb_size * 1024 * 1024);
2846 bdrv_init_with_whitelist();
2848 blk_mig_init();
2850 if (default_cdrom) {
2851 /* we always create the cdrom drive, even if no disk is there */
2852 drive_add(NULL, CDROM_ALIAS);
2855 if (default_floppy) {
2856 /* we always create at least one floppy */
2857 drive_add(NULL, FD_ALIAS, 0);
2860 if (default_sdcard) {
2861 /* we always create one sd slot, even if no card is in it */
2862 drive_add(NULL, SD_ALIAS);
2865 /* open the virtual block devices */
2866 if (snapshot)
2867 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2868 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2869 exit(1);
2871 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2872 ram_load, NULL);
2874 if (nb_numa_nodes > 0) {
2875 int i;
2877 if (nb_numa_nodes > smp_cpus) {
2878 nb_numa_nodes = smp_cpus;
2881 /* If no memory size if given for any node, assume the default case
2882 * and distribute the available memory equally across all nodes
2884 for (i = 0; i < nb_numa_nodes; i++) {
2885 if (node_mem[i] != 0)
2886 break;
2888 if (i == nb_numa_nodes) {
2889 uint64_t usedmem = 0;
2891 /* On Linux, the each node's border has to be 8MB aligned,
2892 * the final node gets the rest.
2894 for (i = 0; i < nb_numa_nodes - 1; i++) {
2895 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2896 usedmem += node_mem[i];
2898 node_mem[i] = ram_size - usedmem;
2901 for (i = 0; i < nb_numa_nodes; i++) {
2902 if (node_cpumask[i] != 0)
2903 break;
2905 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2906 * must cope with this anyway, because there are BIOSes out there in
2907 * real machines which also use this scheme.
2909 if (i == nb_numa_nodes) {
2910 for (i = 0; i < smp_cpus; i++) {
2911 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2916 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2917 exit(1);
2920 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2921 exit(1);
2922 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2923 exit(1);
2924 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2925 exit(1);
2926 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2927 exit(1);
2929 module_call_init(MODULE_INIT_DEVICE);
2931 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2932 exit(0);
2934 if (watchdog) {
2935 i = select_watchdog(watchdog);
2936 if (i > 0)
2937 exit (i == 1 ? 1 : 0);
2940 if (machine->compat_props) {
2941 qdev_prop_register_global_list(machine->compat_props);
2943 qemu_add_globals();
2945 machine->init(ram_size, boot_devices,
2946 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2948 cpu_synchronize_all_post_init();
2950 /* must be after terminal init, SDL library changes signal handlers */
2951 os_setup_signal_handling();
2953 set_numa_modes();
2955 current_machine = machine;
2957 /* init USB devices */
2958 if (usb_enabled) {
2959 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2960 exit(1);
2963 /* init generic devices */
2964 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
2965 exit(1);
2967 net_check_clients();
2969 /* just use the first displaystate for the moment */
2970 ds = get_displaystate();
2972 if (using_spice)
2973 display_remote++;
2974 if (display_type == DT_DEFAULT && !display_remote) {
2975 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2976 display_type = DT_SDL;
2977 #else
2978 vnc_display = "localhost:0,to=99";
2979 show_vnc_port = 1;
2980 #endif
2984 /* init local displays */
2985 switch (display_type) {
2986 case DT_NOGRAPHIC:
2987 break;
2988 #if defined(CONFIG_CURSES)
2989 case DT_CURSES:
2990 curses_display_init(ds, full_screen);
2991 break;
2992 #endif
2993 #if defined(CONFIG_SDL)
2994 case DT_SDL:
2995 sdl_display_init(ds, full_screen, no_frame);
2996 break;
2997 #elif defined(CONFIG_COCOA)
2998 case DT_SDL:
2999 cocoa_display_init(ds, full_screen);
3000 break;
3001 #endif
3002 default:
3003 break;
3006 /* init remote displays */
3007 if (vnc_display) {
3008 vnc_display_init(ds);
3009 if (vnc_display_open(ds, vnc_display) < 0)
3010 exit(1);
3012 if (show_vnc_port) {
3013 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3016 #ifdef CONFIG_SPICE
3017 if (using_spice) {
3018 qemu_spice_display_init(ds);
3020 #endif
3022 /* display setup */
3023 dpy_resize(ds);
3024 dcl = ds->listeners;
3025 while (dcl != NULL) {
3026 if (dcl->dpy_refresh != NULL) {
3027 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3028 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3029 break;
3031 dcl = dcl->next;
3033 if (ds->gui_timer == NULL) {
3034 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3035 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3037 text_consoles_set_display(ds);
3039 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3040 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3041 gdbstub_dev);
3042 exit(1);
3045 qdev_machine_creation_done();
3047 if (rom_load_all() != 0) {
3048 fprintf(stderr, "rom loading failed\n");
3049 exit(1);
3052 qemu_system_reset();
3053 if (loadvm) {
3054 if (load_vmstate(loadvm) < 0) {
3055 autostart = 0;
3059 if (incoming) {
3060 int ret = qemu_start_incoming_migration(incoming);
3061 if (ret < 0) {
3062 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3063 incoming, ret);
3064 exit(ret);
3066 } else if (autostart) {
3067 vm_start();
3070 os_setup_post();
3072 main_loop();
3073 quit_timers();
3074 net_cleanup();
3076 return 0;