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
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
37 #include <sys/times.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.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>
84 #if defined(__OpenBSD__)
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
97 #if defined(__APPLE__) || defined(main)
100 int qemu_main(int argc, char **argv, char **envp);
101 int main(int argc, char **argv)
103 return qemu_main(argc, argv, NULL);
106 #define main qemu_main
109 #endif /* CONFIG_SDL */
113 #define main qemu_main
114 #endif /* CONFIG_COCOA */
120 int qemu_main(int argc, char **argv, char **envp);
121 #define main qemu_main
127 #include "hw/boards.h"
129 #include "hw/pcmcia.h"
134 #include "hw/watchdog.h"
135 #include "hw/smbios.h"
138 #include "hw/loader.h"
141 #include "net/slirp.h"
146 #include "qemu-timer.h"
147 #include "qemu-char.h"
148 #include "cache-utils.h"
150 #include "blockdev.h"
151 #include "block-migration.h"
153 #include "audio/audio.h"
154 #include "migration.h"
158 #include "qemu-option.h"
159 #include "qemu-config.h"
160 #include "qemu-options.h"
161 #include "qmp-commands.h"
162 #include "main-loop.h"
164 #include "fsdev/qemu-fsdev.h"
169 #include "qemu_socket.h"
171 #include "slirp/libslirp.h"
174 #include "trace/control.h"
175 #include "qemu-queue.h"
177 #include "arch_init.h"
179 #include "ui/qemu-spice.h"
182 #include "tizen/src/maru_display.h"
183 #include "tizen/src/option.h"
184 #include "tizen/src/emul_state.h"
185 #include "tizen/src/skin/maruskin_operation.h"
186 #include "tizen/src/maru_err_table.h"
190 //#define DEBUG_SLIRP
192 #define DEFAULT_RAM_SIZE 128
194 #define MAX_VIRTIO_CONSOLES 1
197 #define VIRTIOGL_DEV_NAME "virtio-gl-pci"
198 #define MARUCAM_DEV_NAME "maru_camera_pci"
199 extern int tizen_base_port;
200 int skin_disabled = 0;
203 int ret_hax_init = 0;
205 static const char *data_dir;
206 const char *bios_name = NULL;
207 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
208 DisplayType display_type = DT_DEFAULT;
209 int display_remote = 0;
210 const char* keyboard_layout = NULL;
212 const char *mem_path = NULL;
214 int mem_prealloc = 0; /* force preallocation of physical target memory */
217 NICInfo nd_table[MAX_NICS];
219 static int rtc_utc = 1;
220 static int rtc_date_offset = -1; /* -1 means no change */
221 QEMUClock *rtc_clock;
222 int vga_interface_type = VGA_NONE;
223 static int full_screen = 0;
225 static int no_frame = 0;
228 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
229 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
230 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
231 int win2k_install_hack = 0;
240 const char *vnc_display;
242 int acpi_enabled = 1;
244 int hax_disabled = 1;
249 int graphic_rotate = 0;
250 uint8_t irq0override = 1;
251 const char *watchdog;
252 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
254 int semihosting_enabled = 0;
256 const char *qemu_name;
259 unsigned int nb_prom_envs = 0;
260 const char *prom_envs[MAX_PROM_ENVS];
262 uint8_t *boot_splash_filedata;
263 int boot_splash_filedata_size;
264 uint8_t qemu_extra_params_fw[2];
267 #if defined(CONFIG_MARU) && (!defined(CONFIG_DARWIN))
268 extern int gl_acceleration_capability_check (void);
270 int capability_check_gl = 0;
272 #if defined(CONFIG_MARU)
273 extern int marucam_device_check(void);
274 int is_webcam_enabled = 0;
278 typedef struct FWBootEntry FWBootEntry;
281 QTAILQ_ENTRY(FWBootEntry) link;
287 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
290 uint64_t node_mem[MAX_NODES];
291 uint64_t node_cpumask[MAX_NODES];
293 uint8_t qemu_uuid[16];
295 static QEMUBootSetHandler *boot_set_handler;
296 static void *boot_set_opaque;
298 static NotifierList exit_notifiers =
299 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
301 static NotifierList machine_init_done_notifiers =
302 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
304 static int tcg_allowed = 1;
309 enum xen_mode xen_mode = XEN_EMULATE;
310 static int tcg_tb_size;
312 static int default_serial = 1;
313 static int default_parallel = 1;
314 static int default_virtcon = 1;
315 static int default_monitor = 1;
316 static int default_vga = 1;
317 static int default_floppy = 1;
318 static int default_cdrom = 1;
319 static int default_sdcard = 1;
325 { .driver = "isa-serial", .flag = &default_serial },
326 { .driver = "isa-parallel", .flag = &default_parallel },
327 { .driver = "isa-fdc", .flag = &default_floppy },
328 { .driver = "ide-cd", .flag = &default_cdrom },
329 { .driver = "ide-hd", .flag = &default_cdrom },
330 { .driver = "ide-drive", .flag = &default_cdrom },
331 { .driver = "scsi-cd", .flag = &default_cdrom },
332 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
333 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
334 { .driver = "virtio-serial", .flag = &default_virtcon },
335 { .driver = "VGA", .flag = &default_vga },
336 { .driver = "cirrus-vga", .flag = &default_vga },
337 { .driver = "vmware-svga", .flag = &default_vga },
338 { .driver = "isa-vga", .flag = &default_vga },
339 { .driver = "qxl-vga", .flag = &default_vga },
342 static void res_free(void)
344 if (boot_splash_filedata != NULL) {
345 g_free(boot_splash_filedata);
346 boot_splash_filedata = NULL;
350 static int default_driver_check(QemuOpts *opts, void *opaque)
352 const char *driver = qemu_opt_get(opts, "driver");
357 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
358 if (strcmp(default_list[i].driver, driver) != 0)
360 *(default_list[i].flag) = 0;
365 /***********************************************************/
368 static RunState current_run_state = RUN_STATE_PRELAUNCH;
373 } RunStateTransition;
375 static const RunStateTransition runstate_transitions_def[] = {
377 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
379 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
380 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
382 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
383 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
385 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
386 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
388 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
389 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
391 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
392 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
394 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
395 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
396 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
398 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
399 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
401 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
403 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
404 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
405 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
406 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
407 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
408 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
409 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
410 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
411 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
413 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
415 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
416 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
418 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
419 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
421 { RUN_STATE_MAX, RUN_STATE_MAX },
424 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
426 bool runstate_check(RunState state)
428 return current_run_state == state;
431 void runstate_init(void)
433 const RunStateTransition *p;
435 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
437 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
438 runstate_valid_transitions[p->from][p->to] = true;
442 /* This function will abort() on invalid state transitions */
443 void runstate_set(RunState new_state)
445 assert(new_state < RUN_STATE_MAX);
447 if (!runstate_valid_transitions[current_run_state][new_state]) {
448 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
449 RunState_lookup[current_run_state],
450 RunState_lookup[new_state]);
454 current_run_state = new_state;
457 int runstate_is_running(void)
459 return runstate_check(RUN_STATE_RUNNING);
462 StatusInfo *qmp_query_status(Error **errp)
464 StatusInfo *info = g_malloc0(sizeof(*info));
466 info->running = runstate_is_running();
467 info->singlestep = singlestep;
468 info->status = current_run_state;
473 /***********************************************************/
474 /* real time host monotonic timer */
476 /***********************************************************/
477 /* host time/date access */
478 void qemu_get_timedate(struct tm *tm, int offset)
485 if (rtc_date_offset == -1) {
489 ret = localtime(&ti);
491 ti -= rtc_date_offset;
495 memcpy(tm, ret, sizeof(struct tm));
498 int qemu_timedate_diff(struct tm *tm)
502 if (rtc_date_offset == -1)
504 seconds = mktimegm(tm);
507 tmp.tm_isdst = -1; /* use timezone to figure it out */
508 seconds = mktime(&tmp);
511 seconds = mktimegm(tm) + rtc_date_offset;
513 return seconds - time(NULL);
516 void rtc_change_mon_event(struct tm *tm)
520 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
521 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
522 qobject_decref(data);
525 static void configure_rtc_date_offset(const char *startdate, int legacy)
527 time_t rtc_start_date;
530 if (!strcmp(startdate, "now") && legacy) {
531 rtc_date_offset = -1;
533 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
541 } else if (sscanf(startdate, "%d-%d-%d",
553 rtc_start_date = mktimegm(&tm);
554 if (rtc_start_date == -1) {
556 fprintf(stderr, "Invalid date format. Valid formats are:\n"
557 "'2006-06-17T16:01:21' or '2006-06-17'\n");
560 rtc_date_offset = time(NULL) - rtc_start_date;
564 static void configure_rtc(QemuOpts *opts)
568 value = qemu_opt_get(opts, "base");
570 if (!strcmp(value, "utc")) {
572 } else if (!strcmp(value, "localtime")) {
575 configure_rtc_date_offset(value, 0);
578 value = qemu_opt_get(opts, "clock");
580 if (!strcmp(value, "host")) {
581 rtc_clock = host_clock;
582 } else if (!strcmp(value, "vm")) {
583 rtc_clock = vm_clock;
585 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
589 value = qemu_opt_get(opts, "driftfix");
591 if (!strcmp(value, "slew")) {
593 } else if (!strcmp(value, "none")) {
596 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
602 /***********************************************************/
603 /* Bluetooth support */
606 static struct HCIInfo *hci_table[MAX_NICS];
608 static struct bt_vlan_s {
609 struct bt_scatternet_s net;
611 struct bt_vlan_s *next;
614 /* find or alloc a new bluetooth "VLAN" */
615 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
617 struct bt_vlan_s **pvlan, *vlan;
618 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
622 vlan = g_malloc0(sizeof(struct bt_vlan_s));
624 pvlan = &first_bt_vlan;
625 while (*pvlan != NULL)
626 pvlan = &(*pvlan)->next;
631 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
635 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
640 static struct HCIInfo null_hci = {
641 .cmd_send = null_hci_send,
642 .sco_send = null_hci_send,
643 .acl_send = null_hci_send,
644 .bdaddr_set = null_hci_addr_set,
647 struct HCIInfo *qemu_next_hci(void)
649 if (cur_hci == nb_hcis)
652 return hci_table[cur_hci++];
655 static struct HCIInfo *hci_init(const char *str)
658 struct bt_scatternet_s *vlan = 0;
660 if (!strcmp(str, "null"))
663 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
665 return bt_host_hci(str[4] ? str + 5 : "hci0");
666 else if (!strncmp(str, "hci", 3)) {
669 if (!strncmp(str + 3, ",vlan=", 6)) {
670 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
675 vlan = qemu_find_bt_vlan(0);
677 return bt_new_hci(vlan);
680 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
685 static int bt_hci_parse(const char *str)
690 if (nb_hcis >= MAX_NICS) {
691 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
704 bdaddr.b[5] = 0x56 + nb_hcis;
705 hci->bdaddr_set(hci, bdaddr.b);
707 hci_table[nb_hcis++] = hci;
712 static void bt_vhci_add(int vlan_id)
714 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
717 fprintf(stderr, "qemu: warning: adding a VHCI to "
718 "an empty scatternet %i\n", vlan_id);
720 bt_vhci_init(bt_new_hci(vlan));
723 static struct bt_device_s *bt_device_add(const char *opt)
725 struct bt_scatternet_s *vlan;
727 char *endp = strstr(opt, ",vlan=");
728 int len = (endp ? endp - opt : strlen(opt)) + 1;
731 pstrcpy(devname, MIN(sizeof(devname), len), opt);
734 vlan_id = strtol(endp + 6, &endp, 0);
736 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
741 vlan = qemu_find_bt_vlan(vlan_id);
744 fprintf(stderr, "qemu: warning: adding a slave device to "
745 "an empty scatternet %i\n", vlan_id);
747 if (!strcmp(devname, "keyboard"))
748 return bt_keyboard_init(vlan);
750 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
754 static int bt_parse(const char *opt)
756 const char *endp, *p;
759 if (strstart(opt, "hci", &endp)) {
760 if (!*endp || *endp == ',') {
762 if (!strstart(endp, ",vlan=", 0))
765 return bt_hci_parse(opt);
767 } else if (strstart(opt, "vhci", &endp)) {
768 if (!*endp || *endp == ',') {
770 if (strstart(endp, ",vlan=", &p)) {
771 vlan = strtol(p, (char **) &endp, 0);
773 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
777 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
786 } else if (strstart(opt, "device:", &endp))
787 return !bt_device_add(endp);
789 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
793 /***********************************************************/
794 /* QEMU Block devices */
796 #define HD_OPTS "media=disk"
797 #define CDROM_OPTS "media=cdrom"
799 #define PFLASH_OPTS ""
803 static int drive_init_func(QemuOpts *opts, void *opaque)
805 int *use_scsi = opaque;
807 return drive_init(opts, *use_scsi) == NULL;
810 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
812 if (NULL == qemu_opt_get(opts, "snapshot")) {
813 qemu_opt_set(opts, "snapshot", "on");
818 static void default_drive(int enable, int snapshot, int use_scsi,
819 BlockInterfaceType type, int index,
824 if (type == IF_DEFAULT) {
825 type = use_scsi ? IF_SCSI : IF_IDE;
828 if (!enable || drive_get_by_index(type, index)) {
832 opts = drive_add(type, index, NULL, optstr);
834 drive_enable_snapshot(opts, NULL);
836 if (!drive_init(opts, use_scsi)) {
841 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
843 boot_set_handler = func;
844 boot_set_opaque = opaque;
847 int qemu_boot_set(const char *boot_devices)
849 if (!boot_set_handler) {
852 return boot_set_handler(boot_set_opaque, boot_devices);
855 static void validate_bootdevices(char *devices)
857 /* We just do some generic consistency checks */
861 for (p = devices; *p != '\0'; p++) {
862 /* Allowed boot devices are:
863 * a-b: floppy disk drives
864 * c-f: IDE disk drives
865 * g-m: machine implementation dependant drives
866 * n-p: network devices
867 * It's up to each machine implementation to check if the given boot
868 * devices match the actual hardware implementation and firmware
871 if (*p < 'a' || *p > 'p') {
872 fprintf(stderr, "Invalid boot device '%c'\n", *p);
875 if (bitmap & (1 << (*p - 'a'))) {
876 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
879 bitmap |= 1 << (*p - 'a');
883 static void restore_boot_devices(void *opaque)
885 char *standard_boot_devices = opaque;
886 static int first = 1;
888 /* Restore boot order and remove ourselves after the first boot */
894 qemu_boot_set(standard_boot_devices);
896 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
897 g_free(standard_boot_devices);
900 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
903 FWBootEntry *node, *i;
909 assert(dev != NULL || suffix != NULL);
911 node = g_malloc0(sizeof(FWBootEntry));
912 node->bootindex = bootindex;
913 node->suffix = suffix ? g_strdup(suffix) : NULL;
916 QTAILQ_FOREACH(i, &fw_boot_order, link) {
917 if (i->bootindex == bootindex) {
918 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
920 } else if (i->bootindex < bootindex) {
923 QTAILQ_INSERT_BEFORE(i, node, link);
926 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
930 * This function returns null terminated string that consist of new line
931 * separated device paths.
933 * memory pointed by "size" is assigned total length of the array in bytes
936 char *get_boot_devices_list(uint32_t *size)
942 QTAILQ_FOREACH(i, &fw_boot_order, link) {
943 char *devpath = NULL, *bootpath;
947 devpath = qdev_get_fw_dev_path(i->dev);
951 if (i->suffix && devpath) {
952 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
954 bootpath = g_malloc(bootpathlen);
955 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
957 } else if (devpath) {
961 bootpath = g_strdup(i->suffix);
965 list[total-1] = '\n';
967 len = strlen(bootpath) + 1;
968 list = g_realloc(list, total + len);
969 memcpy(&list[total], bootpath, len);
979 static void numa_add(const char *optarg)
983 unsigned long long value, endvalue;
986 optarg = get_opt_name(option, 128, optarg, ',') + 1;
987 if (!strcmp(option, "node")) {
988 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
989 nodenr = nb_numa_nodes;
991 nodenr = strtoull(option, NULL, 10);
994 if (get_param_value(option, 128, "mem", optarg) == 0) {
995 node_mem[nodenr] = 0;
998 sval = strtosz(option, &endptr);
999 if (sval < 0 || *endptr) {
1000 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1003 node_mem[nodenr] = sval;
1005 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1006 node_cpumask[nodenr] = 0;
1008 value = strtoull(option, &endptr, 10);
1011 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1013 if (*endptr == '-') {
1014 endvalue = strtoull(endptr+1, &endptr, 10);
1015 if (endvalue >= 63) {
1018 "only 63 CPUs in NUMA mode supported.\n");
1020 value = (2ULL << endvalue) - (1ULL << value);
1022 value = 1ULL << value;
1025 node_cpumask[nodenr] = value;
1032 static void smp_parse(const char *optarg)
1034 int smp, sockets = 0, threads = 0, cores = 0;
1038 smp = strtoul(optarg, &endptr, 10);
1039 if (endptr != optarg) {
1040 if (*endptr == ',') {
1044 if (get_param_value(option, 128, "sockets", endptr) != 0)
1045 sockets = strtoull(option, NULL, 10);
1046 if (get_param_value(option, 128, "cores", endptr) != 0)
1047 cores = strtoull(option, NULL, 10);
1048 if (get_param_value(option, 128, "threads", endptr) != 0)
1049 threads = strtoull(option, NULL, 10);
1050 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1051 max_cpus = strtoull(option, NULL, 10);
1053 /* compute missing values, prefer sockets over cores over threads */
1054 if (smp == 0 || sockets == 0) {
1055 sockets = sockets > 0 ? sockets : 1;
1056 cores = cores > 0 ? cores : 1;
1057 threads = threads > 0 ? threads : 1;
1059 smp = cores * threads * sockets;
1063 threads = threads > 0 ? threads : 1;
1064 cores = smp / (sockets * threads);
1066 threads = smp / (cores * sockets);
1070 smp_cores = cores > 0 ? cores : 1;
1071 smp_threads = threads > 0 ? threads : 1;
1073 max_cpus = smp_cpus;
1076 /***********************************************************/
1079 static int usb_device_add(const char *devname)
1082 USBDevice *dev = NULL;
1087 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1088 dev = usbdevice_create(devname);
1092 /* the other ones */
1093 #ifndef CONFIG_LINUX
1094 /* only the linux version is qdev-ified, usb-bsd still needs this */
1095 if (strstart(devname, "host:", &p)) {
1096 dev = usb_host_device_open(p);
1099 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1100 dev = usb_bt_init(devname[2] ? hci_init(p) :
1101 bt_new_hci(qemu_find_bt_vlan(0)));
1112 static int usb_device_del(const char *devname)
1117 if (strstart(devname, "host:", &p))
1118 return usb_host_device_close(p);
1123 p = strchr(devname, '.');
1126 bus_num = strtoul(devname, NULL, 0);
1127 addr = strtoul(p + 1, NULL, 0);
1129 return usb_device_delete_addr(bus_num, addr);
1132 static int usb_parse(const char *cmdline)
1135 r = usb_device_add(cmdline);
1137 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1142 void do_usb_add(Monitor *mon, const QDict *qdict)
1144 const char *devname = qdict_get_str(qdict, "devname");
1145 if (usb_device_add(devname) < 0) {
1146 error_report("could not add USB device '%s'", devname);
1150 void do_usb_del(Monitor *mon, const QDict *qdict)
1152 const char *devname = qdict_get_str(qdict, "devname");
1153 if (usb_device_del(devname) < 0) {
1154 error_report("could not delete USB device '%s'", devname);
1158 /***********************************************************/
1159 /* PCMCIA/Cardbus */
1161 static struct pcmcia_socket_entry_s {
1162 PCMCIASocket *socket;
1163 struct pcmcia_socket_entry_s *next;
1164 } *pcmcia_sockets = 0;
1166 void pcmcia_socket_register(PCMCIASocket *socket)
1168 struct pcmcia_socket_entry_s *entry;
1170 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1171 entry->socket = socket;
1172 entry->next = pcmcia_sockets;
1173 pcmcia_sockets = entry;
1176 void pcmcia_socket_unregister(PCMCIASocket *socket)
1178 struct pcmcia_socket_entry_s *entry, **ptr;
1180 ptr = &pcmcia_sockets;
1181 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1182 if (entry->socket == socket) {
1188 void pcmcia_info(Monitor *mon)
1190 struct pcmcia_socket_entry_s *iter;
1192 if (!pcmcia_sockets)
1193 monitor_printf(mon, "No PCMCIA sockets\n");
1195 for (iter = pcmcia_sockets; iter; iter = iter->next)
1196 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1197 iter->socket->attached ? iter->socket->card_string :
1201 /***********************************************************/
1202 /* machine registration */
1204 static QEMUMachine *first_machine = NULL;
1205 QEMUMachine *current_machine = NULL;
1207 int qemu_register_machine(QEMUMachine *m)
1210 pm = &first_machine;
1218 static QEMUMachine *find_machine(const char *name)
1222 for(m = first_machine; m != NULL; m = m->next) {
1223 if (!strcmp(m->name, name))
1225 if (m->alias && !strcmp(m->alias, name))
1231 static QEMUMachine *find_default_machine(void)
1235 for(m = first_machine; m != NULL; m = m->next) {
1236 if (m->is_default) {
1243 /***********************************************************/
1244 /* main execution loop */
1246 static void gui_update(void *opaque)
1248 uint64_t interval = GUI_REFRESH_INTERVAL;
1249 DisplayState *ds = opaque;
1250 DisplayChangeListener *dcl = ds->listeners;
1254 while (dcl != NULL) {
1255 if (dcl->gui_timer_interval &&
1256 dcl->gui_timer_interval < interval)
1257 interval = dcl->gui_timer_interval;
1260 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1263 struct vm_change_state_entry {
1264 VMChangeStateHandler *cb;
1266 QLIST_ENTRY (vm_change_state_entry) entries;
1269 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1271 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1274 VMChangeStateEntry *e;
1276 e = g_malloc0(sizeof (*e));
1280 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1284 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1286 QLIST_REMOVE (e, entries);
1290 void vm_state_notify(int running, RunState state)
1292 VMChangeStateEntry *e;
1294 trace_vm_state_notify(running, state);
1296 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1297 e->cb(e->opaque, running, state);
1303 if (!runstate_is_running()) {
1305 runstate_set(RUN_STATE_RUNNING);
1306 vm_state_notify(1, RUN_STATE_RUNNING);
1308 monitor_protocol_event(QEVENT_RESUME, NULL);
1312 /* reset/shutdown handler */
1314 typedef struct QEMUResetEntry {
1315 QTAILQ_ENTRY(QEMUResetEntry) entry;
1316 QEMUResetHandler *func;
1320 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1321 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1322 static int reset_requested;
1323 static int shutdown_requested, shutdown_signal = -1;
1324 static pid_t shutdown_pid;
1325 static int powerdown_requested;
1326 static int debug_requested;
1327 static RunState vmstop_requested = RUN_STATE_MAX;
1329 int qemu_shutdown_requested_get(void)
1331 return shutdown_requested;
1334 int qemu_reset_requested_get(void)
1336 return reset_requested;
1339 int qemu_shutdown_requested(void)
1341 int r = shutdown_requested;
1342 shutdown_requested = 0;
1346 void qemu_kill_report(void)
1348 if (shutdown_signal != -1) {
1349 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1350 if (shutdown_pid == 0) {
1351 /* This happens for eg ^C at the terminal, so it's worth
1352 * avoiding printing an odd message in that case.
1354 fputc('\n', stderr);
1356 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1358 shutdown_signal = -1;
1362 int qemu_reset_requested(void)
1364 int r = reset_requested;
1365 reset_requested = 0;
1369 int qemu_powerdown_requested(void)
1371 int r = powerdown_requested;
1372 powerdown_requested = 0;
1376 static int qemu_debug_requested(void)
1378 int r = debug_requested;
1379 debug_requested = 0;
1383 /* We use RUN_STATE_MAX but any invalid value will do */
1384 static bool qemu_vmstop_requested(RunState *r)
1386 if (vmstop_requested < RUN_STATE_MAX) {
1387 *r = vmstop_requested;
1388 vmstop_requested = RUN_STATE_MAX;
1395 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1397 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1400 re->opaque = opaque;
1401 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1404 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1408 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1409 if (re->func == func && re->opaque == opaque) {
1410 QTAILQ_REMOVE(&reset_handlers, re, entry);
1417 void qemu_system_reset(bool report)
1419 QEMUResetEntry *re, *nre;
1421 /* reset all devices */
1422 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1423 re->func(re->opaque);
1426 monitor_protocol_event(QEVENT_RESET, NULL);
1428 cpu_synchronize_all_post_reset();
1431 void qemu_system_reset_request(void)
1434 shutdown_requested = 1;
1436 reset_requested = 1;
1439 qemu_notify_event();
1442 void qemu_system_killed(int signal, pid_t pid)
1444 shutdown_signal = signal;
1449 shutdown_qemu_gracefully();
1451 qemu_system_shutdown_request();
1455 void qemu_system_shutdown_request(void)
1457 shutdown_requested = 1;
1458 qemu_notify_event();
1461 void qemu_system_powerdown_request(void)
1463 powerdown_requested = 1;
1464 qemu_notify_event();
1467 void qemu_system_debug_request(void)
1469 debug_requested = 1;
1470 qemu_notify_event();
1473 void qemu_system_vmstop_request(RunState state)
1475 vmstop_requested = state;
1476 qemu_notify_event();
1479 qemu_irq qemu_system_powerdown;
1481 static bool main_loop_should_exit(void)
1484 if (qemu_debug_requested()) {
1485 vm_stop(RUN_STATE_DEBUG);
1487 if (qemu_shutdown_requested()) {
1489 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1491 vm_stop(RUN_STATE_SHUTDOWN);
1496 if (qemu_reset_requested()) {
1498 cpu_synchronize_all_states();
1499 qemu_system_reset(VMRESET_REPORT);
1501 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1502 runstate_check(RUN_STATE_SHUTDOWN)) {
1503 runstate_set(RUN_STATE_PAUSED);
1506 if (qemu_powerdown_requested()) {
1507 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1508 qemu_irq_raise(qemu_system_powerdown);
1510 if (qemu_vmstop_requested(&r)) {
1516 static void main_loop(void)
1520 #ifdef CONFIG_PROFILER
1529 nonblocking = !(kvm_enabled()|| hax_enabled()) && last_io > 0;
1530 #ifdef CONFIG_PROFILER
1531 ti = profile_getclock();
1533 last_io = main_loop_wait(nonblocking);
1534 #ifdef CONFIG_PROFILER
1535 dev_time += profile_getclock() - ti;
1537 } while (!main_loop_should_exit());
1540 static void version(void)
1542 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1545 static void help(int exitcode)
1547 const char *options_help =
1548 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1550 #define DEFHEADING(text) stringify(text) "\n"
1551 #include "qemu-options.def"
1557 printf("usage: %s [options] [disk_image]\n"
1559 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1562 "During emulation, the following keys are useful:\n"
1563 "ctrl-alt-f toggle full screen\n"
1564 "ctrl-alt-n switch to virtual console 'n'\n"
1565 "ctrl-alt toggle mouse and keyboard grab\n"
1567 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1573 #define HAS_ARG 0x0001
1575 typedef struct QEMUOption {
1582 static const QEMUOption qemu_options[] = {
1583 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1584 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1585 { option, opt_arg, opt_enum, arch_mask },
1586 #define DEFHEADING(text)
1587 #include "qemu-options.def"
1593 static void select_vgahw (const char *p)
1598 vga_interface_type = VGA_NONE;
1599 if (strstart(p, "std", &opts)) {
1600 vga_interface_type = VGA_STD;
1601 } else if (strstart(p, "cirrus", &opts)) {
1602 vga_interface_type = VGA_CIRRUS;
1603 } else if (strstart(p, "vmware", &opts)) {
1604 vga_interface_type = VGA_VMWARE;
1605 } else if (strstart(p, "xenfb", &opts)) {
1606 vga_interface_type = VGA_XENFB;
1607 } else if (strstart(p, "qxl", &opts)) {
1608 vga_interface_type = VGA_QXL;
1610 } else if (strstart(p, "maru", &opts)) {
1611 vga_interface_type = VGA_MARU;
1613 } else if (!strstart(p, "none", &opts)) {
1615 fprintf(stderr, "Unknown vga type: %s\n", p);
1619 const char *nextopt;
1621 if (strstart(opts, ",retrace=", &nextopt)) {
1623 if (strstart(opts, "dumb", &nextopt))
1624 vga_retrace_method = VGA_RETRACE_DUMB;
1625 else if (strstart(opts, "precise", &nextopt))
1626 vga_retrace_method = VGA_RETRACE_PRECISE;
1627 else goto invalid_vga;
1628 } else goto invalid_vga;
1633 static DisplayType select_display(const char *p)
1636 DisplayType display = DT_DEFAULT;
1638 if (strstart(p, "sdl", &opts)) {
1642 const char *nextopt;
1644 if (strstart(opts, ",frame=", &nextopt)) {
1646 if (strstart(opts, "on", &nextopt)) {
1648 } else if (strstart(opts, "off", &nextopt)) {
1651 goto invalid_sdl_args;
1653 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1655 if (strstart(opts, "on", &nextopt)) {
1657 } else if (strstart(opts, "off", &nextopt)) {
1660 goto invalid_sdl_args;
1662 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1664 if (strstart(opts, "on", &nextopt)) {
1666 } else if (strstart(opts, "off", &nextopt)) {
1669 goto invalid_sdl_args;
1671 } else if (strstart(opts, ",window_close=", &nextopt)) {
1673 if (strstart(opts, "on", &nextopt)) {
1675 } else if (strstart(opts, "off", &nextopt)) {
1678 goto invalid_sdl_args;
1682 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1688 fprintf(stderr, "SDL support is disabled\n");
1691 } else if (strstart(p, "vnc", &opts)) {
1696 const char *nextopt;
1698 if (strstart(opts, "=", &nextopt)) {
1699 vnc_display = nextopt;
1703 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1707 fprintf(stderr, "VNC support is disabled\n");
1710 } else if (strstart(p, "curses", &opts)) {
1711 #ifdef CONFIG_CURSES
1712 display = DT_CURSES;
1714 fprintf(stderr, "Curses support is disabled\n");
1717 } else if (strstart(p, "none", &opts)) {
1720 fprintf(stderr, "Unknown display type: %s\n", p);
1727 static int balloon_parse(const char *arg)
1731 if (strcmp(arg, "none") == 0) {
1735 if (!strncmp(arg, "virtio", 6)) {
1736 if (arg[6] == ',') {
1737 /* have params -> parse them */
1738 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1742 /* create empty opts */
1743 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1745 qemu_opt_set(opts, "driver", "virtio-balloon");
1752 char *qemu_find_file(int type, const char *name)
1758 /* If name contains path separators then try it as a straight path. */
1759 if ((strchr(name, '/') || strchr(name, '\\'))
1760 && access(name, R_OK) == 0) {
1761 return g_strdup(name);
1764 case QEMU_FILE_TYPE_BIOS:
1767 case QEMU_FILE_TYPE_KEYMAP:
1768 subdir = "keymaps/";
1773 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1774 buf = g_malloc0(len);
1775 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1776 if (access(buf, R_OK)) {
1784 const char *qemu_get_data_dir(void);
1786 const char *qemu_get_data_dir(void)
1792 static int device_help_func(QemuOpts *opts, void *opaque)
1794 return qdev_device_help(opts);
1797 static int device_init_func(QemuOpts *opts, void *opaque)
1801 #if defined(CONFIG_MARU) && (!defined(CONFIG_DARWIN))
1802 // virtio-gl pci device
1804 // ignore virtio-gl-pci device, even if users set it in option.
1805 const char *driver = qemu_opt_get(opts, "driver");
1806 if (driver && (strcmp (driver, VIRTIOGL_DEV_NAME) == 0)) {
1811 #if defined(CONFIG_MARU)
1812 if (!is_webcam_enabled) {
1813 const char *driver = qemu_opt_get(opts, "driver");
1814 if (driver && (strcmp (driver, MARUCAM_DEV_NAME) == 0)) {
1820 dev = qdev_device_add(opts);
1826 static int chardev_init_func(QemuOpts *opts, void *opaque)
1828 CharDriverState *chr;
1830 chr = qemu_chr_new_from_opts(opts, NULL);
1836 #ifdef CONFIG_VIRTFS
1837 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1840 ret = qemu_fsdev_add(opts);
1846 static int mon_init_func(QemuOpts *opts, void *opaque)
1848 CharDriverState *chr;
1849 const char *chardev;
1853 mode = qemu_opt_get(opts, "mode");
1857 if (strcmp(mode, "readline") == 0) {
1858 flags = MONITOR_USE_READLINE;
1859 } else if (strcmp(mode, "control") == 0) {
1860 flags = MONITOR_USE_CONTROL;
1862 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1866 if (qemu_opt_get_bool(opts, "pretty", 0))
1867 flags |= MONITOR_USE_PRETTY;
1869 if (qemu_opt_get_bool(opts, "default", 0))
1870 flags |= MONITOR_IS_DEFAULT;
1872 chardev = qemu_opt_get(opts, "chardev");
1873 chr = qemu_chr_find(chardev);
1875 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1879 monitor_init(chr, flags);
1883 static void monitor_parse(const char *optarg, const char *mode)
1885 static int monitor_device_index = 0;
1891 if (strstart(optarg, "chardev:", &p)) {
1892 snprintf(label, sizeof(label), "%s", p);
1894 snprintf(label, sizeof(label), "compat_monitor%d",
1895 monitor_device_index);
1896 if (monitor_device_index == 0) {
1899 opts = qemu_chr_parse_compat(label, optarg);
1901 fprintf(stderr, "parse error: %s\n", optarg);
1906 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1908 fprintf(stderr, "duplicate chardev: %s\n", label);
1911 qemu_opt_set(opts, "mode", mode);
1912 qemu_opt_set(opts, "chardev", label);
1914 qemu_opt_set(opts, "default", "on");
1915 monitor_device_index++;
1918 struct device_config {
1920 DEV_USB, /* -usbdevice */
1922 DEV_SERIAL, /* -serial */
1923 DEV_PARALLEL, /* -parallel */
1924 DEV_VIRTCON, /* -virtioconsole */
1925 DEV_DEBUGCON, /* -debugcon */
1927 const char *cmdline;
1928 QTAILQ_ENTRY(device_config) next;
1930 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1932 static void add_device_config(int type, const char *cmdline)
1934 struct device_config *conf;
1936 conf = g_malloc0(sizeof(*conf));
1938 conf->cmdline = cmdline;
1939 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1942 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1944 struct device_config *conf;
1947 QTAILQ_FOREACH(conf, &device_configs, next) {
1948 if (conf->type != type)
1950 rc = func(conf->cmdline);
1957 static int serial_parse(const char *devname)
1959 static int index = 0;
1962 if (strcmp(devname, "none") == 0)
1964 if (index == MAX_SERIAL_PORTS) {
1965 fprintf(stderr, "qemu: too many serial ports\n");
1968 snprintf(label, sizeof(label), "serial%d", index);
1969 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1970 if (!serial_hds[index]) {
1971 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1972 devname, strerror(errno));
1979 static int parallel_parse(const char *devname)
1981 static int index = 0;
1984 if (strcmp(devname, "none") == 0)
1986 if (index == MAX_PARALLEL_PORTS) {
1987 fprintf(stderr, "qemu: too many parallel ports\n");
1990 snprintf(label, sizeof(label), "parallel%d", index);
1991 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
1992 if (!parallel_hds[index]) {
1993 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1994 devname, strerror(errno));
2001 static int virtcon_parse(const char *devname)
2003 QemuOptsList *device = qemu_find_opts("device");
2004 static int index = 0;
2006 QemuOpts *bus_opts, *dev_opts;
2008 if (strcmp(devname, "none") == 0)
2010 if (index == MAX_VIRTIO_CONSOLES) {
2011 fprintf(stderr, "qemu: too many virtio consoles\n");
2015 bus_opts = qemu_opts_create(device, NULL, 0);
2016 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2018 dev_opts = qemu_opts_create(device, NULL, 0);
2019 qemu_opt_set(dev_opts, "driver", "virtconsole");
2021 snprintf(label, sizeof(label), "virtcon%d", index);
2022 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2023 if (!virtcon_hds[index]) {
2024 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2025 devname, strerror(errno));
2028 qemu_opt_set(dev_opts, "chardev", label);
2034 static int debugcon_parse(const char *devname)
2038 if (!qemu_chr_new("debugcon", devname, NULL)) {
2041 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2043 fprintf(stderr, "qemu: already have a debugcon device\n");
2046 qemu_opt_set(opts, "driver", "isa-debugcon");
2047 qemu_opt_set(opts, "chardev", "debugcon");
2051 static QEMUMachine *machine_parse(const char *name)
2053 QEMUMachine *m, *machine = NULL;
2056 machine = find_machine(name);
2061 printf("Supported machines are:\n");
2062 for (m = first_machine; m != NULL; m = m->next) {
2064 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2066 printf("%-10s %s%s\n", m->name, m->desc,
2067 m->is_default ? " (default)" : "");
2069 exit(!name || *name != '?');
2072 static int tcg_init(void)
2075 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2081 if (ret && (ret != -ENOSPC))
2082 dprint("No accelerator found.\n");
2084 dprint("HAX is %s and emulator runs in %s mode.\n",
2085 !ret ? "working" : "not working",
2086 !ret ? "fast virt" : "emulation");
2091 dprint("HAX is disabled and emulator runs in emulation mode.\n");
2097 const char *opt_name;
2099 int (*available)(void);
2103 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2104 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2105 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2108 static int configure_accelerator(void)
2110 const char *p = NULL;
2113 bool accel_initalised = 0;
2114 bool init_failed = 0;
2116 QemuOptsList *list = qemu_find_opts("machine");
2117 if (!QTAILQ_EMPTY(&list->head)) {
2118 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2122 /* Use the default "accelerator", tcg */
2126 while (!accel_initalised && *p != '\0') {
2130 p = get_opt_name(buf, sizeof (buf), p, ':');
2131 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2132 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2133 *(accel_list[i].allowed) = 1;
2134 ret = accel_list[i].init();
2137 if (!accel_list[i].available()) {
2138 printf("%s not supported for this target\n",
2139 accel_list[i].name);
2141 fprintf(stderr, "failed to initialize %s: %s\n",
2145 *(accel_list[i].allowed) = 0;
2147 accel_initalised = 1;
2152 if (i == ARRAY_SIZE(accel_list)) {
2153 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2157 if (!accel_initalised) {
2158 fprintf(stderr, "No accelerator found!\n");
2160 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "No accelerator found.");
2166 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2169 return !accel_initalised;
2172 void qemu_add_exit_notifier(Notifier *notify)
2174 notifier_list_add(&exit_notifiers, notify);
2177 void qemu_remove_exit_notifier(Notifier *notify)
2179 notifier_list_remove(&exit_notifiers, notify);
2182 static void qemu_run_exit_notifiers(void)
2184 notifier_list_notify(&exit_notifiers, NULL);
2187 void qemu_add_machine_init_done_notifier(Notifier *notify)
2189 notifier_list_add(&machine_init_done_notifiers, notify);
2192 static void qemu_run_machine_init_done_notifiers(void)
2194 notifier_list_notify(&machine_init_done_notifiers, NULL);
2197 static const QEMUOption *lookup_opt(int argc, char **argv,
2198 const char **poptarg, int *poptind)
2200 const QEMUOption *popt;
2201 int optind = *poptind;
2202 char *r = argv[optind];
2205 loc_set_cmdline(argv, optind, 1);
2207 /* Treat --foo the same as -foo. */
2210 popt = qemu_options;
2213 error_report("invalid option");
2215 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "invalid option.");
2219 if (!strcmp(popt->name, r + 1))
2223 if (popt->flags & HAS_ARG) {
2224 if (optind >= argc) {
2225 error_report("requires an argument");
2227 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "requires an argument.");
2231 optarg = argv[optind++];
2232 loc_set_cmdline(argv, optind - 2, 2);
2243 static gpointer malloc_and_trace(gsize n_bytes)
2245 void *ptr = malloc(n_bytes);
2246 trace_g_malloc(n_bytes, ptr);
2250 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2252 void *ptr = realloc(mem, n_bytes);
2253 trace_g_realloc(mem, n_bytes, ptr);
2257 static void free_and_trace(gpointer mem)
2263 // virtio-gl pci device lookup
2265 const char *device_name;
2267 } device_opt_finding_t;
2269 static int find_device_opt (QemuOpts *opts, void *opaque)
2271 device_opt_finding_t *devp = (device_opt_finding_t *) opaque;
2272 if (devp->found == 1) {
2276 const char *str = qemu_opt_get (opts, "driver");
2277 if (strcmp (str, devp->device_name) == 0) {
2283 int use_qemu_display = 0; //0:use tizen qemu sdl, 1:use original qemu sdl
2284 int main(int argc, char **argv, char **envp)
2286 const char *gdbstub_dev = NULL;
2288 int snapshot, linux_boot;
2289 const char *icount_option = NULL;
2290 const char *initrd_filename;
2291 const char *kernel_filename, *kernel_cmdline;
2292 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2294 DisplayChangeListener *dcl;
2295 int cyls, heads, secs, translation;
2296 QemuOpts *hda_opts = NULL, *opts;
2297 QemuOptsList *olist;
2300 const char *loadvm = NULL;
2301 QEMUMachine *machine;
2302 const char *cpu_model;
2303 const char *pid_file = NULL;
2304 const char *incoming = NULL;
2306 int show_vnc_port = 0;
2309 const char *log_mask = NULL;
2310 const char *log_file = NULL;
2311 GMemVTable mem_trace = {
2312 .malloc = malloc_and_trace,
2313 .realloc = realloc_and_trace,
2314 .free = free_and_trace,
2316 const char *trace_events = NULL;
2317 const char *trace_file = NULL;
2321 char http_proxy[MIDBUF] ={0},https_proxy[MIDBUF] = {0,},
2322 ftp_proxy[MIDBUF] = {0,}, socks_proxy[MIDBUF] = {0,},
2323 dns1[MIDBUF] = {0}, dns2[MIDBUF] = {0};
2326 atexit(qemu_run_exit_notifiers);
2327 error_set_progname(argv[0]);
2329 g_mem_set_vtable(&mem_trace);
2330 if (!g_thread_supported()) {
2331 g_thread_init(NULL);
2337 rtc_clock = host_clock;
2339 qemu_cache_utils_init(envp);
2341 QLIST_INIT (&vm_change_state_head);
2342 os_setup_early_signal_handling();
2344 module_call_init(MODULE_INIT_MACHINE);
2345 machine = find_default_machine();
2347 initrd_filename = NULL;
2350 kernel_filename = NULL;
2351 kernel_cmdline = "";
2352 cyls = heads = secs = 0;
2353 translation = BIOS_ATA_TRANSLATION_AUTO;
2355 for (i = 0; i < MAX_NODES; i++) {
2357 node_cpumask[i] = 0;
2365 /* first pass of option parsing */
2367 while (optind < argc) {
2368 if (argv[optind][0] != '-') {
2373 const QEMUOption *popt;
2375 popt = lookup_opt(argc, argv, &optarg, &optind);
2376 switch (popt->index) {
2377 case QEMU_OPTION_nodefconfig:
2387 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2388 if (ret < 0 && ret != -ENOENT) {
2392 ret = qemu_read_config_file(arch_config_name);
2393 if (ret < 0 && ret != -ENOENT) {
2399 /* second pass of option parsing */
2404 if (argv[optind][0] != '-') {
2405 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2407 const QEMUOption *popt;
2409 popt = lookup_opt(argc, argv, &optarg, &optind);
2410 if (!(popt->arch_mask & arch_type)) {
2411 printf("Option %s not supported for this target\n", popt->name);
2414 switch(popt->index) {
2416 machine = machine_parse(optarg);
2418 case QEMU_OPTION_cpu:
2419 /* hw initialization will check this */
2420 if (*optarg == '?') {
2421 list_cpus(stdout, &fprintf, optarg);
2427 case QEMU_OPTION_initrd:
2428 initrd_filename = optarg;
2430 case QEMU_OPTION_hda:
2434 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2436 snprintf(buf, sizeof(buf),
2437 "%s,cyls=%d,heads=%d,secs=%d%s",
2438 HD_OPTS , cyls, heads, secs,
2439 translation == BIOS_ATA_TRANSLATION_LBA ?
2441 translation == BIOS_ATA_TRANSLATION_NONE ?
2442 ",trans=none" : "");
2443 drive_add(IF_DEFAULT, 0, optarg, buf);
2446 case QEMU_OPTION_hdb:
2447 case QEMU_OPTION_hdc:
2448 case QEMU_OPTION_hdd:
2449 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2452 case QEMU_OPTION_drive:
2453 if (drive_def(optarg) == NULL) {
2457 case QEMU_OPTION_set:
2458 if (qemu_set_option(optarg) != 0)
2461 case QEMU_OPTION_global:
2462 if (qemu_global_option(optarg) != 0)
2465 case QEMU_OPTION_mtdblock:
2466 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2468 case QEMU_OPTION_sd:
2469 drive_add(IF_SD, 0, optarg, SD_OPTS);
2471 case QEMU_OPTION_pflash:
2472 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2474 case QEMU_OPTION_snapshot:
2477 case QEMU_OPTION_hdachs:
2481 cyls = strtol(p, (char **)&p, 0);
2482 if (cyls < 1 || cyls > 16383)
2487 heads = strtol(p, (char **)&p, 0);
2488 if (heads < 1 || heads > 16)
2493 secs = strtol(p, (char **)&p, 0);
2494 if (secs < 1 || secs > 63)
2498 if (!strcmp(p, "none"))
2499 translation = BIOS_ATA_TRANSLATION_NONE;
2500 else if (!strcmp(p, "lba"))
2501 translation = BIOS_ATA_TRANSLATION_LBA;
2502 else if (!strcmp(p, "auto"))
2503 translation = BIOS_ATA_TRANSLATION_AUTO;
2506 } else if (*p != '\0') {
2508 fprintf(stderr, "qemu: invalid physical CHS format\n");
2511 if (hda_opts != NULL) {
2513 snprintf(num, sizeof(num), "%d", cyls);
2514 qemu_opt_set(hda_opts, "cyls", num);
2515 snprintf(num, sizeof(num), "%d", heads);
2516 qemu_opt_set(hda_opts, "heads", num);
2517 snprintf(num, sizeof(num), "%d", secs);
2518 qemu_opt_set(hda_opts, "secs", num);
2519 if (translation == BIOS_ATA_TRANSLATION_LBA)
2520 qemu_opt_set(hda_opts, "trans", "lba");
2521 if (translation == BIOS_ATA_TRANSLATION_NONE)
2522 qemu_opt_set(hda_opts, "trans", "none");
2526 case QEMU_OPTION_numa:
2527 if (nb_numa_nodes >= MAX_NODES) {
2528 fprintf(stderr, "qemu: too many NUMA nodes\n");
2533 case QEMU_OPTION_display:
2534 display_type = select_display(optarg);
2536 case QEMU_OPTION_nographic:
2537 display_type = DT_NOGRAPHIC;
2539 case QEMU_OPTION_curses:
2540 #ifdef CONFIG_CURSES
2541 display_type = DT_CURSES;
2543 fprintf(stderr, "Curses support is disabled\n");
2547 case QEMU_OPTION_portrait:
2548 graphic_rotate = 90;
2550 case QEMU_OPTION_rotate:
2551 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2552 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2553 graphic_rotate != 180 && graphic_rotate != 270) {
2555 "qemu: only 90, 180, 270 deg rotation is available\n");
2559 case QEMU_OPTION_kernel:
2560 kernel_filename = optarg;
2562 case QEMU_OPTION_append:
2564 gethostproxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
2565 gethostDNS(dns1, dns2);
2566 kernel_cmdline = g_strdup_printf("%s sdb_port=%d,"
2567 " http_proxy=%s https_proxy=%s ftp_proxy=%s socks_proxy=%s"
2568 " dns1=%s dns2=%s", optarg, tizen_base_port,
2569 http_proxy, https_proxy, ftp_proxy, socks_proxy,
2571 fprintf(stdout, "kernel command : %s\n", kernel_cmdline);
2573 kernel_cmdline = optarg;
2576 case QEMU_OPTION_cdrom:
2577 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2579 case QEMU_OPTION_boot:
2581 static const char * const params[] = {
2582 "order", "once", "menu",
2583 "splash", "splash-time", NULL
2585 char buf[sizeof(boot_devices)];
2586 char *standard_boot_devices;
2589 if (!strchr(optarg, '=')) {
2591 pstrcpy(buf, sizeof(buf), optarg);
2592 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2594 "qemu: unknown boot parameter '%s' in '%s'\n",
2600 get_param_value(buf, sizeof(buf), "order", optarg)) {
2601 validate_bootdevices(buf);
2602 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2605 if (get_param_value(buf, sizeof(buf),
2607 validate_bootdevices(buf);
2608 standard_boot_devices = g_strdup(boot_devices);
2609 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2610 qemu_register_reset(restore_boot_devices,
2611 standard_boot_devices);
2613 if (get_param_value(buf, sizeof(buf),
2615 if (!strcmp(buf, "on")) {
2617 } else if (!strcmp(buf, "off")) {
2621 "qemu: invalid option value '%s'\n",
2626 qemu_opts_parse(qemu_find_opts("boot-opts"),
2631 case QEMU_OPTION_fda:
2632 case QEMU_OPTION_fdb:
2633 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2636 case QEMU_OPTION_no_fd_bootchk:
2639 case QEMU_OPTION_netdev:
2640 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2644 case QEMU_OPTION_net:
2645 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2650 case QEMU_OPTION_tftp:
2651 legacy_tftp_prefix = optarg;
2653 case QEMU_OPTION_bootp:
2654 legacy_bootp_filename = optarg;
2656 case QEMU_OPTION_redir:
2657 if (net_slirp_redir(optarg) < 0)
2661 case QEMU_OPTION_bt:
2662 add_device_config(DEV_BT, optarg);
2664 case QEMU_OPTION_audio_help:
2665 if (!(audio_available())) {
2666 printf("Option %s not supported for this target\n", popt->name);
2672 case QEMU_OPTION_soundhw:
2673 if (!(audio_available())) {
2674 printf("Option %s not supported for this target\n", popt->name);
2677 select_soundhw (optarg);
2682 case QEMU_OPTION_version:
2686 case QEMU_OPTION_m: {
2690 value = strtosz(optarg, &end);
2691 if (value < 0 || *end) {
2692 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2696 if (value != (uint64_t)(ram_addr_t)value) {
2697 fprintf(stderr, "qemu: ram size too large\n");
2703 case QEMU_OPTION_mempath:
2707 case QEMU_OPTION_mem_prealloc:
2718 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2720 case QEMU_OPTION_gdb:
2721 gdbstub_dev = optarg;
2726 case QEMU_OPTION_bios:
2729 case QEMU_OPTION_singlestep:
2736 keyboard_layout = optarg;
2738 case QEMU_OPTION_localtime:
2741 case QEMU_OPTION_vga:
2742 select_vgahw (optarg);
2749 w = strtol(p, (char **)&p, 10);
2752 fprintf(stderr, "qemu: invalid resolution or depth\n");
2758 h = strtol(p, (char **)&p, 10);
2763 depth = strtol(p, (char **)&p, 10);
2764 if (depth != 8 && depth != 15 && depth != 16 &&
2765 depth != 24 && depth != 32)
2767 } else if (*p == '\0') {
2768 depth = graphic_depth;
2775 graphic_depth = depth;
2778 case QEMU_OPTION_echr:
2781 term_escape_char = strtol(optarg, &r, 0);
2783 printf("Bad argument to echr\n");
2786 case QEMU_OPTION_monitor:
2787 monitor_parse(optarg, "readline");
2788 default_monitor = 0;
2790 case QEMU_OPTION_qmp:
2791 monitor_parse(optarg, "control");
2792 default_monitor = 0;
2794 case QEMU_OPTION_mon:
2795 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2799 default_monitor = 0;
2801 case QEMU_OPTION_chardev:
2802 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2807 case QEMU_OPTION_fsdev:
2808 olist = qemu_find_opts("fsdev");
2810 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2813 opts = qemu_opts_parse(olist, optarg, 1);
2815 fprintf(stderr, "parse error: %s\n", optarg);
2819 case QEMU_OPTION_virtfs: {
2822 const char *writeout;
2824 olist = qemu_find_opts("virtfs");
2826 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2829 opts = qemu_opts_parse(olist, optarg, 1);
2831 fprintf(stderr, "parse error: %s\n", optarg);
2835 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2836 qemu_opt_get(opts, "mount_tag") == NULL ||
2837 qemu_opt_get(opts, "path") == NULL) {
2838 fprintf(stderr, "Usage: -virtfs fsdriver,path=/share_path/,"
2839 "[security_model={mapped|passthrough|none}],"
2840 "mount_tag=tag.\n");
2843 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2844 qemu_opt_get(opts, "mount_tag"), 1);
2846 fprintf(stderr, "duplicate fsdev id: %s\n",
2847 qemu_opt_get(opts, "mount_tag"));
2851 writeout = qemu_opt_get(opts, "writeout");
2853 #ifdef CONFIG_SYNC_FILE_RANGE
2854 qemu_opt_set(fsdev, "writeout", writeout);
2856 fprintf(stderr, "writeout=immediate not supported on "
2861 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2862 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2863 qemu_opt_set(fsdev, "security_model",
2864 qemu_opt_get(opts, "security_model"));
2866 qemu_opt_set_bool(fsdev, "readonly",
2867 qemu_opt_get_bool(opts, "readonly", 0));
2868 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2869 qemu_opt_set(device, "driver", "virtio-9p-pci");
2870 qemu_opt_set(device, "fsdev",
2871 qemu_opt_get(opts, "mount_tag"));
2872 qemu_opt_set(device, "mount_tag",
2873 qemu_opt_get(opts, "mount_tag"));
2876 case QEMU_OPTION_virtfs_synth: {
2880 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2882 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2885 qemu_opt_set(fsdev, "fsdriver", "synth");
2886 qemu_opt_set(fsdev, "path", "/"); /* ignored */
2888 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2889 qemu_opt_set(device, "driver", "virtio-9p-pci");
2890 qemu_opt_set(device, "fsdev", "v_synth");
2891 qemu_opt_set(device, "mount_tag", "v_synth");
2894 case QEMU_OPTION_serial:
2895 add_device_config(DEV_SERIAL, optarg);
2897 if (strncmp(optarg, "mon:", 4) == 0) {
2898 default_monitor = 0;
2901 case QEMU_OPTION_watchdog:
2904 "qemu: only one watchdog option may be given\n");
2909 case QEMU_OPTION_watchdog_action:
2910 if (select_watchdog_action(optarg) == -1) {
2911 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2915 case QEMU_OPTION_virtiocon:
2916 add_device_config(DEV_VIRTCON, optarg);
2917 default_virtcon = 0;
2918 if (strncmp(optarg, "mon:", 4) == 0) {
2919 default_monitor = 0;
2922 case QEMU_OPTION_parallel:
2923 add_device_config(DEV_PARALLEL, optarg);
2924 default_parallel = 0;
2925 if (strncmp(optarg, "mon:", 4) == 0) {
2926 default_monitor = 0;
2929 case QEMU_OPTION_debugcon:
2930 add_device_config(DEV_DEBUGCON, optarg);
2932 case QEMU_OPTION_loadvm:
2935 case QEMU_OPTION_full_screen:
2939 case QEMU_OPTION_no_frame:
2942 case QEMU_OPTION_alt_grab:
2945 case QEMU_OPTION_ctrl_grab:
2948 case QEMU_OPTION_no_quit:
2951 case QEMU_OPTION_sdl:
2952 display_type = DT_SDL;
2955 case QEMU_OPTION_no_frame:
2956 case QEMU_OPTION_alt_grab:
2957 case QEMU_OPTION_ctrl_grab:
2958 case QEMU_OPTION_no_quit:
2959 case QEMU_OPTION_sdl:
2960 fprintf(stderr, "SDL support is disabled\n");
2963 case QEMU_OPTION_pidfile:
2966 case QEMU_OPTION_win2k_hack:
2967 win2k_install_hack = 1;
2969 case QEMU_OPTION_rtc_td_hack:
2972 case QEMU_OPTION_acpitable:
2973 do_acpitable_option(optarg);
2975 case QEMU_OPTION_smbios:
2976 do_smbios_option(optarg);
2978 case QEMU_OPTION_enable_kvm:
2979 olist = qemu_find_opts("machine");
2980 qemu_opts_reset(olist);
2981 qemu_opts_parse(olist, "accel=kvm", 0);
2983 case QEMU_OPTION_enable_gl:
2984 #if defined(CONFIG_MARU) && (!defined(CONFIG_DARWIN))
2988 case QEMU_OPTION_machine:
2989 olist = qemu_find_opts("machine");
2990 qemu_opts_reset(olist);
2991 opts = qemu_opts_parse(olist, optarg, 1);
2993 fprintf(stderr, "parse error: %s\n", optarg);
2996 optarg = qemu_opt_get(opts, "type");
2998 machine = machine_parse(optarg);
3001 case QEMU_OPTION_usb:
3004 case QEMU_OPTION_usbdevice:
3006 add_device_config(DEV_USB, optarg);
3008 case QEMU_OPTION_device:
3009 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3013 case QEMU_OPTION_smp:
3016 fprintf(stderr, "Invalid number of CPUs\n");
3019 if (max_cpus < smp_cpus) {
3020 fprintf(stderr, "maxcpus must be equal to or greater than "
3024 if (max_cpus > 255) {
3025 fprintf(stderr, "Unsupported number of maxcpus\n");
3029 case QEMU_OPTION_vnc:
3032 vnc_display = optarg;
3034 fprintf(stderr, "VNC support is disabled\n");
3038 case QEMU_OPTION_no_acpi:
3041 case QEMU_OPTION_no_hpet:
3044 case QEMU_OPTION_balloon:
3045 if (balloon_parse(optarg) < 0) {
3046 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3050 case QEMU_OPTION_no_reboot:
3053 case QEMU_OPTION_no_shutdown:
3056 case QEMU_OPTION_show_cursor:
3059 case QEMU_OPTION_uuid:
3060 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3061 fprintf(stderr, "Fail to parse UUID string."
3062 " Wrong format.\n");
3066 case QEMU_OPTION_option_rom:
3067 if (nb_option_roms >= MAX_OPTION_ROMS) {
3068 fprintf(stderr, "Too many option ROMs\n");
3071 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3072 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3073 option_rom[nb_option_roms].bootindex =
3074 qemu_opt_get_number(opts, "bootindex", -1);
3075 if (!option_rom[nb_option_roms].name) {
3076 fprintf(stderr, "Option ROM file is not specified\n");
3081 case QEMU_OPTION_semihosting:
3082 semihosting_enabled = 1;
3084 case QEMU_OPTION_name:
3085 qemu_name = g_strdup(optarg);
3087 char *p = strchr(qemu_name, ',');
3090 if (strncmp(p, "process=", 8)) {
3091 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3095 os_set_proc_name(p);
3099 case QEMU_OPTION_prom_env:
3100 if (nb_prom_envs >= MAX_PROM_ENVS) {
3101 fprintf(stderr, "Too many prom variables\n");
3104 prom_envs[nb_prom_envs] = optarg;
3107 case QEMU_OPTION_old_param:
3110 case QEMU_OPTION_clock:
3111 configure_alarms(optarg);
3113 case QEMU_OPTION_startdate:
3114 configure_rtc_date_offset(optarg, 1);
3116 case QEMU_OPTION_rtc:
3117 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3121 configure_rtc(opts);
3123 case QEMU_OPTION_tb_size:
3124 tcg_tb_size = strtol(optarg, NULL, 0);
3125 if (tcg_tb_size < 0) {
3129 case QEMU_OPTION_icount:
3130 icount_option = optarg;
3132 case QEMU_OPTION_incoming:
3135 case QEMU_OPTION_nodefaults:
3137 default_parallel = 0;
3138 default_virtcon = 0;
3139 default_monitor = 0;
3146 case QEMU_OPTION_xen_domid:
3147 if (!(xen_available())) {
3148 printf("Option %s not supported for this target\n", popt->name);
3151 xen_domid = atoi(optarg);
3153 case QEMU_OPTION_xen_create:
3154 if (!(xen_available())) {
3155 printf("Option %s not supported for this target\n", popt->name);
3158 xen_mode = XEN_CREATE;
3160 case QEMU_OPTION_xen_attach:
3161 if (!(xen_available())) {
3162 printf("Option %s not supported for this target\n", popt->name);
3165 xen_mode = XEN_ATTACH;
3167 case QEMU_OPTION_trace:
3169 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3173 trace_events = qemu_opt_get(opts, "events");
3174 trace_file = qemu_opt_get(opts, "file");
3177 case QEMU_OPTION_readconfig:
3179 int ret = qemu_read_config_file(optarg);
3181 fprintf(stderr, "read config %s: %s\n", optarg,
3187 case QEMU_OPTION_spice:
3188 olist = qemu_find_opts("spice");
3190 fprintf(stderr, "spice is not supported by this qemu build.\n");
3193 opts = qemu_opts_parse(olist, optarg, 0);
3195 fprintf(stderr, "parse error: %s\n", optarg);
3199 case QEMU_OPTION_writeconfig:
3202 if (strcmp(optarg, "-") == 0) {
3205 fp = fopen(optarg, "w");
3207 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3211 qemu_config_write(fp);
3215 case QEMU_OPTION_enable_hax:
3216 olist = qemu_find_opts("machine");
3217 qemu_opts_reset(olist);
3219 //qemu_opts_parse(olist, "accel=hax", 0);
3222 case QEMU_OPTION_max_touch_point:
3224 int cnt = atoi(optarg);
3225 fprintf(stderr, "maxTouchPoint:%d\n", cnt);
3226 set_emul_max_touch_point(cnt);
3229 case QEMU_OPTION_disable_skin:
3234 os_parse_cmd_args(popt->index, optarg);
3240 #if defined(CONFIG_MARU) && (!defined(CONFIG_DARWIN))
3242 capability_check_gl = gl_acceleration_capability_check();
3244 if (capability_check_gl != 0) {
3246 fprintf (stderr, "Warn: GL acceleration was disabled due to the fail of GL check!\n");
3249 // To check host gl driver capability and notify to guest.
3250 gchar *tmp = kernel_cmdline;
3251 kernel_cmdline = g_strdup_printf("%s gles=%d", tmp, enable_gl);
3252 fprintf(stdout, "kernel command : %s\n", kernel_cmdline);
3256 device_opt_finding_t devp = {VIRTIOGL_DEV_NAME, 0};
3257 qemu_opts_foreach(qemu_find_opts("device"), find_device_opt, &devp, 0);
3258 if (devp.found == 0) {
3259 if (!qemu_opts_parse(qemu_find_opts("device"), VIRTIOGL_DEV_NAME, "driver")) {
3267 #if defined(CONFIG_MARU)
3268 is_webcam_enabled = marucam_device_check();
3269 if (!is_webcam_enabled) {
3270 fprintf (stderr, "WARNING: Webcam support was disabled due to "
3271 "the fail of webcam capability check!\n");
3274 gchar *tmp_cam_kcmd = kernel_cmdline;
3275 kernel_cmdline = g_strdup_printf("%s enable_cam=%d", tmp_cam_kcmd, is_webcam_enabled);
3276 fprintf(stdout, "kernel command : %s\n", kernel_cmdline);
3277 g_free(tmp_cam_kcmd);
3279 if (is_webcam_enabled) {
3280 device_opt_finding_t devp = {MARUCAM_DEV_NAME, 0};
3281 qemu_opts_foreach(qemu_find_opts("device"), find_device_opt, &devp, 0);
3282 if (devp.found == 0) {
3283 if (!qemu_opts_parse(qemu_find_opts("device"), MARUCAM_DEV_NAME, "driver")) {
3290 /* Open the logfile at this point, if necessary. We can't open the logfile
3291 * when encountering either of the logging options (-d or -D) because the
3292 * other one may be encountered later on the command line, changing the
3293 * location or level of logging.
3297 set_cpu_log_filename(log_file);
3299 set_cpu_log(log_mask);
3302 if (!trace_backend_init(trace_events, trace_file)) {
3306 /* If no data_dir is specified then try to find it relative to the
3309 data_dir = os_find_datadir(argv[0]);
3311 /* If all else fails use the install path specified when building. */
3313 data_dir = CONFIG_QEMU_DATADIR;
3316 if (machine == NULL) {
3317 fprintf(stderr, "No machine found.\n");
3322 * Default to max_cpus = smp_cpus, in case the user doesn't
3323 * specify a max_cpus value.
3326 max_cpus = smp_cpus;
3328 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3329 if (smp_cpus > machine->max_cpus) {
3330 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3331 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3337 * Get the default machine options from the machine if it is not already
3338 * specified either by the configuration file or by the command line.
3340 if (machine->default_machine_opts) {
3341 QemuOptsList *list = qemu_find_opts("machine");
3342 const char *p = NULL;
3344 if (!QTAILQ_EMPTY(&list->head)) {
3345 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3348 qemu_opts_reset(list);
3349 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
3351 fprintf(stderr, "parse error for machine %s: %s\n",
3352 machine->name, machine->default_machine_opts);
3358 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3359 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3361 if (machine->no_serial) {
3364 if (machine->no_parallel) {
3365 default_parallel = 0;
3367 if (!machine->use_virtcon) {
3368 default_virtcon = 0;
3370 if (machine->no_vga) {
3373 if (machine->no_floppy) {
3376 if (machine->no_cdrom) {
3379 if (machine->no_sdcard) {
3383 if (display_type == DT_NOGRAPHIC) {
3384 if (default_parallel)
3385 add_device_config(DEV_PARALLEL, "null");
3386 if (default_serial && default_monitor) {
3387 add_device_config(DEV_SERIAL, "mon:stdio");
3388 } else if (default_virtcon && default_monitor) {
3389 add_device_config(DEV_VIRTCON, "mon:stdio");
3392 add_device_config(DEV_SERIAL, "stdio");
3393 if (default_virtcon)
3394 add_device_config(DEV_VIRTCON, "stdio");
3395 if (default_monitor)
3396 monitor_parse("stdio", "readline");
3400 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3401 if (default_parallel)
3402 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3403 if (default_monitor)
3404 monitor_parse("vc:80Cx24C", "readline");
3405 if (default_virtcon)
3406 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3409 vga_interface_type = VGA_CIRRUS;
3411 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3413 #ifdef CONFIG_VIRTFS
3414 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3421 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3426 /* init the memory */
3427 if (ram_size == 0) {
3428 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3432 hax_pre_init(ram_size);
3435 configure_accelerator();
3437 qemu_init_cpu_loop();
3438 if (qemu_init_main_loop()) {
3439 fprintf(stderr, "qemu_init_main_loop failed\n");
3442 linux_boot = (kernel_filename != NULL);
3444 if (!linux_boot && *kernel_cmdline != '\0') {
3445 fprintf(stderr, "-append only allowed with -kernel option\n");
3449 if (!linux_boot && initrd_filename != NULL) {
3450 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3454 os_set_line_buffering();
3456 if (init_timer_alarm() < 0) {
3457 fprintf(stderr, "could not initialize alarm timer\n");
3461 if (icount_option && (kvm_enabled() || xen_enabled() || hax_enabled())) {
3462 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3465 configure_icount(icount_option);
3467 if (net_init_clients() < 0) {
3471 /* init the bluetooth world */
3472 if (foreach_device_config(DEV_BT, bt_parse))
3475 if (!xen_enabled()) {
3476 /* On 32-bit hosts, QEMU is limited by virtual address space */
3477 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3478 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3483 cpu_exec_init_all();
3485 bdrv_init_with_whitelist();
3489 /* open the virtual block devices */
3491 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3492 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3495 default_drive(default_cdrom, snapshot, machine->use_scsi,
3496 IF_DEFAULT, 2, CDROM_OPTS);
3497 default_drive(default_floppy, snapshot, machine->use_scsi,
3498 IF_FLOPPY, 0, FD_OPTS);
3499 default_drive(default_sdcard, snapshot, machine->use_scsi,
3502 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3505 if (nb_numa_nodes > 0) {
3508 if (nb_numa_nodes > MAX_NODES) {
3509 nb_numa_nodes = MAX_NODES;
3512 /* If no memory size if given for any node, assume the default case
3513 * and distribute the available memory equally across all nodes
3515 for (i = 0; i < nb_numa_nodes; i++) {
3516 if (node_mem[i] != 0)
3519 if (i == nb_numa_nodes) {
3520 uint64_t usedmem = 0;
3522 /* On Linux, the each node's border has to be 8MB aligned,
3523 * the final node gets the rest.
3525 for (i = 0; i < nb_numa_nodes - 1; i++) {
3526 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3527 usedmem += node_mem[i];
3529 node_mem[i] = ram_size - usedmem;
3532 for (i = 0; i < nb_numa_nodes; i++) {
3533 if (node_cpumask[i] != 0)
3536 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3537 * must cope with this anyway, because there are BIOSes out there in
3538 * real machines which also use this scheme.
3540 if (i == nb_numa_nodes) {
3541 for (i = 0; i < smp_cpus; i++) {
3542 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3547 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3551 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3553 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3555 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3557 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3560 module_call_init(MODULE_INIT_DEVICE);
3562 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3566 i = select_watchdog(watchdog);
3568 exit (i == 1 ? 1 : 0);
3571 if (machine->compat_props) {
3572 qdev_prop_register_global_list(machine->compat_props);
3576 machine->init(ram_size, boot_devices,
3577 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3579 g_free((gchar *)kernel_cmdline);
3582 cpu_synchronize_all_post_init();
3586 current_machine = machine;
3593 /* init USB devices */
3595 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3599 /* init generic devices */
3600 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3603 net_check_clients();
3605 /* just use the first displaystate for the moment */
3606 ds = get_displaystate();
3610 if (display_type == DT_DEFAULT && !display_remote) {
3611 #if defined(CONFIG_MARU)
3613 display_type = DT_MARU;
3614 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3615 display_type = DT_SDL;
3616 #elif defined(CONFIG_VNC)
3617 vnc_display = "localhost:0,to=99";
3620 display_type = DT_NONE;
3625 /* init local displays */
3626 switch (display_type) {
3629 #if defined(CONFIG_CURSES)
3631 curses_display_init(ds, full_screen);
3634 #if defined(CONFIG_SDL)
3636 sdl_display_init(ds, full_screen, no_frame);
3638 #elif defined(CONFIG_COCOA)
3640 cocoa_display_init(ds, full_screen);
3643 #if defined(CONFIG_MARU)
3645 maru_display_init(ds);
3646 if (skin_disabled == 1) {
3647 set_emul_skin_enable(0);
3649 set_emul_skin_enable(1);
3657 /* must be after terminal init, SDL library changes signal handlers */
3658 os_setup_signal_handling();
3661 /* init remote displays */
3663 vnc_display_init(ds);
3664 if (vnc_display_open(ds, vnc_display) < 0)
3667 if (show_vnc_port) {
3668 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3673 if (using_spice && !qxl_enabled) {
3674 qemu_spice_display_init(ds);
3680 dcl = ds->listeners;
3681 while (dcl != NULL) {
3682 if (dcl->dpy_refresh != NULL) {
3683 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3684 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3689 text_consoles_set_display(ds);
3691 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3692 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3697 qdev_machine_creation_done();
3699 if (rom_load_all() != 0) {
3700 fprintf(stderr, "rom loading failed\n");
3704 /* TODO: once all bus devices are qdevified, this should be done
3705 * when bus is created by qdev.c */
3706 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3707 qemu_run_machine_init_done_notifiers();
3709 qemu_system_reset(VMRESET_SILENT);
3711 if (load_vmstate(loadvm) < 0) {
3717 runstate_set(RUN_STATE_INMIGRATE);
3718 int ret = qemu_start_incoming_migration(incoming);
3720 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3724 } else if (autostart) {