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"
170 #include "qemu_socket.h"
172 #include "slirp/libslirp.h"
175 #include "trace/control.h"
176 #include "qemu-queue.h"
178 #include "arch_init.h"
180 #include "ui/qemu-spice.h"
183 #include "tizen/src/maru_display.h"
184 #include "tizen/src/option.h"
185 #include "tizen/src/emul_state.h"
186 #include "tizen/src/skin/maruskin_operation.h"
187 #include "tizen/src/maru_err_table.h"
191 //#define DEBUG_SLIRP
193 #define DEFAULT_RAM_SIZE 128
195 #define MAX_VIRTIO_CONSOLES 1
198 extern int tizen_base_port;
199 int skin_disabled = 0;
202 static const char *data_dir;
203 const char *bios_name = NULL;
204 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
205 DisplayType display_type = DT_DEFAULT;
206 int display_remote = 0;
207 const char* keyboard_layout = NULL;
209 const char *mem_path = NULL;
211 int mem_prealloc = 0; /* force preallocation of physical target memory */
214 NICInfo nd_table[MAX_NICS];
216 static int rtc_utc = 1;
217 static int rtc_date_offset = -1; /* -1 means no change */
218 QEMUClock *rtc_clock;
219 int vga_interface_type = VGA_NONE;
220 static int full_screen = 0;
222 static int no_frame = 0;
225 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
226 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
227 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
228 int win2k_install_hack = 0;
236 const char *vnc_display;
238 int acpi_enabled = 1;
244 int graphic_rotate = 0;
245 const char *watchdog;
246 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
248 int semihosting_enabled = 0;
250 const char *qemu_name;
253 unsigned int nb_prom_envs = 0;
254 const char *prom_envs[MAX_PROM_ENVS];
256 uint8_t *boot_splash_filedata;
257 int boot_splash_filedata_size;
258 uint8_t qemu_extra_params_fw[2];
261 #define VIRTIOGL_DEV_NAME "virtio-gl-pci"
262 #if defined(CONFIG_MARU) && (!defined(CONFIG_DARWIN))
263 extern int gl_acceleration_capability_check(void);
265 int capability_check_gl = 0;
268 typedef struct FWBootEntry FWBootEntry;
271 QTAILQ_ENTRY(FWBootEntry) link;
277 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
280 uint64_t node_mem[MAX_NODES];
281 uint64_t node_cpumask[MAX_NODES];
283 uint8_t qemu_uuid[16];
285 static QEMUBootSetHandler *boot_set_handler;
286 static void *boot_set_opaque;
288 static NotifierList exit_notifiers =
289 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
291 static NotifierList machine_init_done_notifiers =
292 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
294 static int tcg_allowed = 1;
299 enum xen_mode xen_mode = XEN_EMULATE;
300 static int tcg_tb_size;
302 #ifdef CONFIG_OPENGLES
303 int gles2_quality = 100;
305 static int default_serial = 1;
306 static int default_parallel = 1;
307 static int default_virtcon = 1;
308 static int default_monitor = 1;
309 static int default_floppy = 1;
310 static int default_cdrom = 1;
311 static int default_sdcard = 1;
312 static int default_vga = 1;
318 { .driver = "isa-serial", .flag = &default_serial },
319 { .driver = "isa-parallel", .flag = &default_parallel },
320 { .driver = "isa-fdc", .flag = &default_floppy },
321 { .driver = "ide-cd", .flag = &default_cdrom },
322 { .driver = "ide-hd", .flag = &default_cdrom },
323 { .driver = "ide-drive", .flag = &default_cdrom },
324 { .driver = "scsi-cd", .flag = &default_cdrom },
325 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
326 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
327 { .driver = "virtio-serial", .flag = &default_virtcon },
328 { .driver = "VGA", .flag = &default_vga },
329 { .driver = "isa-vga", .flag = &default_vga },
330 { .driver = "cirrus-vga", .flag = &default_vga },
331 { .driver = "isa-cirrus-vga", .flag = &default_vga },
332 { .driver = "vmware-svga", .flag = &default_vga },
333 { .driver = "qxl-vga", .flag = &default_vga },
336 static void res_free(void)
338 if (boot_splash_filedata != NULL) {
339 g_free(boot_splash_filedata);
340 boot_splash_filedata = NULL;
344 static int default_driver_check(QemuOpts *opts, void *opaque)
346 const char *driver = qemu_opt_get(opts, "driver");
351 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
352 if (strcmp(default_list[i].driver, driver) != 0)
354 *(default_list[i].flag) = 0;
359 /***********************************************************/
362 static RunState current_run_state = RUN_STATE_PRELAUNCH;
367 } RunStateTransition;
369 static const RunStateTransition runstate_transitions_def[] = {
371 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
373 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
374 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
376 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
377 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
379 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
380 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
382 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
383 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
385 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
386 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
388 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
389 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
390 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
392 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
393 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
395 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
397 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
398 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
399 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
400 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
401 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
402 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
403 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
404 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
405 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
407 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
409 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
410 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
412 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
413 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
414 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
415 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
417 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
418 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
420 { RUN_STATE_MAX, RUN_STATE_MAX },
423 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
425 bool runstate_check(RunState state)
427 return current_run_state == state;
430 void runstate_init(void)
432 const RunStateTransition *p;
434 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
436 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
437 runstate_valid_transitions[p->from][p->to] = true;
441 /* This function will abort() on invalid state transitions */
442 void runstate_set(RunState new_state)
444 assert(new_state < RUN_STATE_MAX);
446 if (!runstate_valid_transitions[current_run_state][new_state]) {
447 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
448 RunState_lookup[current_run_state],
449 RunState_lookup[new_state]);
453 current_run_state = new_state;
456 int runstate_is_running(void)
458 return runstate_check(RUN_STATE_RUNNING);
461 StatusInfo *qmp_query_status(Error **errp)
463 StatusInfo *info = g_malloc0(sizeof(*info));
465 info->running = runstate_is_running();
466 info->singlestep = singlestep;
467 info->status = current_run_state;
472 /***********************************************************/
473 /* real time host monotonic timer */
475 /***********************************************************/
476 /* host time/date access */
477 void qemu_get_timedate(struct tm *tm, int offset)
484 if (rtc_date_offset == -1) {
488 ret = localtime(&ti);
490 ti -= rtc_date_offset;
494 memcpy(tm, ret, sizeof(struct tm));
497 int qemu_timedate_diff(struct tm *tm)
501 if (rtc_date_offset == -1)
503 seconds = mktimegm(tm);
506 tmp.tm_isdst = -1; /* use timezone to figure it out */
507 seconds = mktime(&tmp);
510 seconds = mktimegm(tm) + rtc_date_offset;
512 return seconds - time(NULL);
515 void rtc_change_mon_event(struct tm *tm)
519 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
520 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
521 qobject_decref(data);
524 static void configure_rtc_date_offset(const char *startdate, int legacy)
526 time_t rtc_start_date;
529 if (!strcmp(startdate, "now") && legacy) {
530 rtc_date_offset = -1;
532 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
540 } else if (sscanf(startdate, "%d-%d-%d",
552 rtc_start_date = mktimegm(&tm);
553 if (rtc_start_date == -1) {
555 fprintf(stderr, "Invalid date format. Valid formats are:\n"
556 "'2006-06-17T16:01:21' or '2006-06-17'\n");
559 rtc_date_offset = time(NULL) - rtc_start_date;
563 static void configure_rtc(QemuOpts *opts)
567 value = qemu_opt_get(opts, "base");
569 if (!strcmp(value, "utc")) {
571 } else if (!strcmp(value, "localtime")) {
574 configure_rtc_date_offset(value, 0);
577 value = qemu_opt_get(opts, "clock");
579 if (!strcmp(value, "host")) {
580 rtc_clock = host_clock;
581 } else if (!strcmp(value, "rt")) {
582 rtc_clock = rt_clock;
583 } else if (!strcmp(value, "vm")) {
584 rtc_clock = vm_clock;
586 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
590 value = qemu_opt_get(opts, "driftfix");
592 if (!strcmp(value, "slew")) {
593 static GlobalProperty slew_lost_ticks[] = {
595 .driver = "mc146818rtc",
596 .property = "lost_tick_policy",
599 { /* end of list */ }
602 qdev_prop_register_global_list(slew_lost_ticks);
603 } else if (!strcmp(value, "none")) {
604 /* discard is default */
606 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
612 /***********************************************************/
613 /* Bluetooth support */
616 static struct HCIInfo *hci_table[MAX_NICS];
618 static struct bt_vlan_s {
619 struct bt_scatternet_s net;
621 struct bt_vlan_s *next;
624 /* find or alloc a new bluetooth "VLAN" */
625 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
627 struct bt_vlan_s **pvlan, *vlan;
628 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
632 vlan = g_malloc0(sizeof(struct bt_vlan_s));
634 pvlan = &first_bt_vlan;
635 while (*pvlan != NULL)
636 pvlan = &(*pvlan)->next;
641 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
645 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
650 static struct HCIInfo null_hci = {
651 .cmd_send = null_hci_send,
652 .sco_send = null_hci_send,
653 .acl_send = null_hci_send,
654 .bdaddr_set = null_hci_addr_set,
657 struct HCIInfo *qemu_next_hci(void)
659 if (cur_hci == nb_hcis)
662 return hci_table[cur_hci++];
665 static struct HCIInfo *hci_init(const char *str)
668 struct bt_scatternet_s *vlan = 0;
670 if (!strcmp(str, "null"))
673 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
675 return bt_host_hci(str[4] ? str + 5 : "hci0");
676 else if (!strncmp(str, "hci", 3)) {
679 if (!strncmp(str + 3, ",vlan=", 6)) {
680 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
685 vlan = qemu_find_bt_vlan(0);
687 return bt_new_hci(vlan);
690 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
695 static int bt_hci_parse(const char *str)
700 if (nb_hcis >= MAX_NICS) {
701 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
714 bdaddr.b[5] = 0x56 + nb_hcis;
715 hci->bdaddr_set(hci, bdaddr.b);
717 hci_table[nb_hcis++] = hci;
722 static void bt_vhci_add(int vlan_id)
724 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
727 fprintf(stderr, "qemu: warning: adding a VHCI to "
728 "an empty scatternet %i\n", vlan_id);
730 bt_vhci_init(bt_new_hci(vlan));
733 static struct bt_device_s *bt_device_add(const char *opt)
735 struct bt_scatternet_s *vlan;
737 char *endp = strstr(opt, ",vlan=");
738 int len = (endp ? endp - opt : strlen(opt)) + 1;
741 pstrcpy(devname, MIN(sizeof(devname), len), opt);
744 vlan_id = strtol(endp + 6, &endp, 0);
746 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
751 vlan = qemu_find_bt_vlan(vlan_id);
754 fprintf(stderr, "qemu: warning: adding a slave device to "
755 "an empty scatternet %i\n", vlan_id);
757 if (!strcmp(devname, "keyboard"))
758 return bt_keyboard_init(vlan);
760 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
764 static int bt_parse(const char *opt)
766 const char *endp, *p;
769 if (strstart(opt, "hci", &endp)) {
770 if (!*endp || *endp == ',') {
772 if (!strstart(endp, ",vlan=", 0))
775 return bt_hci_parse(opt);
777 } else if (strstart(opt, "vhci", &endp)) {
778 if (!*endp || *endp == ',') {
780 if (strstart(endp, ",vlan=", &p)) {
781 vlan = strtol(p, (char **) &endp, 0);
783 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
787 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
796 } else if (strstart(opt, "device:", &endp))
797 return !bt_device_add(endp);
799 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
803 /***********************************************************/
804 /* QEMU Block devices */
806 #define HD_OPTS "media=disk"
807 #define CDROM_OPTS "media=cdrom"
809 #define PFLASH_OPTS ""
813 static int drive_init_func(QemuOpts *opts, void *opaque)
815 int *use_scsi = opaque;
817 return drive_init(opts, *use_scsi) == NULL;
820 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
822 if (NULL == qemu_opt_get(opts, "snapshot")) {
823 qemu_opt_set(opts, "snapshot", "on");
828 static void default_drive(int enable, int snapshot, int use_scsi,
829 BlockInterfaceType type, int index,
834 if (type == IF_DEFAULT) {
835 type = use_scsi ? IF_SCSI : IF_IDE;
838 if (!enable || drive_get_by_index(type, index)) {
842 opts = drive_add(type, index, NULL, optstr);
844 drive_enable_snapshot(opts, NULL);
846 if (!drive_init(opts, use_scsi)) {
851 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
853 boot_set_handler = func;
854 boot_set_opaque = opaque;
857 int qemu_boot_set(const char *boot_devices)
859 if (!boot_set_handler) {
862 return boot_set_handler(boot_set_opaque, boot_devices);
865 static void validate_bootdevices(char *devices)
867 /* We just do some generic consistency checks */
871 for (p = devices; *p != '\0'; p++) {
872 /* Allowed boot devices are:
873 * a-b: floppy disk drives
874 * c-f: IDE disk drives
875 * g-m: machine implementation dependent drives
876 * n-p: network devices
877 * It's up to each machine implementation to check if the given boot
878 * devices match the actual hardware implementation and firmware
881 if (*p < 'a' || *p > 'p') {
882 fprintf(stderr, "Invalid boot device '%c'\n", *p);
885 if (bitmap & (1 << (*p - 'a'))) {
886 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
889 bitmap |= 1 << (*p - 'a');
893 static void restore_boot_devices(void *opaque)
895 char *standard_boot_devices = opaque;
896 static int first = 1;
898 /* Restore boot order and remove ourselves after the first boot */
904 qemu_boot_set(standard_boot_devices);
906 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
907 g_free(standard_boot_devices);
910 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
913 FWBootEntry *node, *i;
919 assert(dev != NULL || suffix != NULL);
921 node = g_malloc0(sizeof(FWBootEntry));
922 node->bootindex = bootindex;
923 node->suffix = suffix ? g_strdup(suffix) : NULL;
926 QTAILQ_FOREACH(i, &fw_boot_order, link) {
927 if (i->bootindex == bootindex) {
928 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
930 } else if (i->bootindex < bootindex) {
933 QTAILQ_INSERT_BEFORE(i, node, link);
936 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
940 * This function returns null terminated string that consist of new line
941 * separated device paths.
943 * memory pointed by "size" is assigned total length of the array in bytes
946 char *get_boot_devices_list(uint32_t *size)
952 QTAILQ_FOREACH(i, &fw_boot_order, link) {
953 char *devpath = NULL, *bootpath;
957 devpath = qdev_get_fw_dev_path(i->dev);
961 if (i->suffix && devpath) {
962 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
964 bootpath = g_malloc(bootpathlen);
965 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
967 } else if (devpath) {
971 bootpath = g_strdup(i->suffix);
975 list[total-1] = '\n';
977 len = strlen(bootpath) + 1;
978 list = g_realloc(list, total + len);
979 memcpy(&list[total], bootpath, len);
989 static void numa_add(const char *optarg)
993 unsigned long long value, endvalue;
996 optarg = get_opt_name(option, 128, optarg, ',') + 1;
997 if (!strcmp(option, "node")) {
998 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
999 nodenr = nb_numa_nodes;
1001 nodenr = strtoull(option, NULL, 10);
1004 if (get_param_value(option, 128, "mem", optarg) == 0) {
1005 node_mem[nodenr] = 0;
1008 sval = strtosz(option, &endptr);
1009 if (sval < 0 || *endptr) {
1010 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1013 node_mem[nodenr] = sval;
1015 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1016 node_cpumask[nodenr] = 0;
1018 value = strtoull(option, &endptr, 10);
1021 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1023 if (*endptr == '-') {
1024 endvalue = strtoull(endptr+1, &endptr, 10);
1025 if (endvalue >= 63) {
1028 "only 63 CPUs in NUMA mode supported.\n");
1030 value = (2ULL << endvalue) - (1ULL << value);
1032 value = 1ULL << value;
1035 node_cpumask[nodenr] = value;
1042 static void smp_parse(const char *optarg)
1044 int smp, sockets = 0, threads = 0, cores = 0;
1048 smp = strtoul(optarg, &endptr, 10);
1049 if (endptr != optarg) {
1050 if (*endptr == ',') {
1054 if (get_param_value(option, 128, "sockets", endptr) != 0)
1055 sockets = strtoull(option, NULL, 10);
1056 if (get_param_value(option, 128, "cores", endptr) != 0)
1057 cores = strtoull(option, NULL, 10);
1058 if (get_param_value(option, 128, "threads", endptr) != 0)
1059 threads = strtoull(option, NULL, 10);
1060 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1061 max_cpus = strtoull(option, NULL, 10);
1063 /* compute missing values, prefer sockets over cores over threads */
1064 if (smp == 0 || sockets == 0) {
1065 sockets = sockets > 0 ? sockets : 1;
1066 cores = cores > 0 ? cores : 1;
1067 threads = threads > 0 ? threads : 1;
1069 smp = cores * threads * sockets;
1073 threads = threads > 0 ? threads : 1;
1074 cores = smp / (sockets * threads);
1076 threads = smp / (cores * sockets);
1080 smp_cores = cores > 0 ? cores : 1;
1081 smp_threads = threads > 0 ? threads : 1;
1083 max_cpus = smp_cpus;
1086 /***********************************************************/
1089 static int usb_device_add(const char *devname)
1092 USBDevice *dev = NULL;
1097 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1098 dev = usbdevice_create(devname);
1102 /* the other ones */
1103 #ifndef CONFIG_LINUX
1104 /* only the linux version is qdev-ified, usb-bsd still needs this */
1105 if (strstart(devname, "host:", &p)) {
1106 dev = usb_host_device_open(usb_bus_find(-1), p);
1109 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1110 dev = usb_bt_init(usb_bus_find(-1),
1111 devname[2] ? hci_init(p)
1112 : bt_new_hci(qemu_find_bt_vlan(0)));
1123 static int usb_device_del(const char *devname)
1128 if (strstart(devname, "host:", &p))
1129 return usb_host_device_close(p);
1134 p = strchr(devname, '.');
1137 bus_num = strtoul(devname, NULL, 0);
1138 addr = strtoul(p + 1, NULL, 0);
1140 return usb_device_delete_addr(bus_num, addr);
1143 static int usb_parse(const char *cmdline)
1146 r = usb_device_add(cmdline);
1148 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1153 void do_usb_add(Monitor *mon, const QDict *qdict)
1155 const char *devname = qdict_get_str(qdict, "devname");
1156 if (usb_device_add(devname) < 0) {
1157 error_report("could not add USB device '%s'", devname);
1161 void do_usb_del(Monitor *mon, const QDict *qdict)
1163 const char *devname = qdict_get_str(qdict, "devname");
1164 if (usb_device_del(devname) < 0) {
1165 error_report("could not delete USB device '%s'", devname);
1169 /***********************************************************/
1170 /* PCMCIA/Cardbus */
1172 static struct pcmcia_socket_entry_s {
1173 PCMCIASocket *socket;
1174 struct pcmcia_socket_entry_s *next;
1175 } *pcmcia_sockets = 0;
1177 void pcmcia_socket_register(PCMCIASocket *socket)
1179 struct pcmcia_socket_entry_s *entry;
1181 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1182 entry->socket = socket;
1183 entry->next = pcmcia_sockets;
1184 pcmcia_sockets = entry;
1187 void pcmcia_socket_unregister(PCMCIASocket *socket)
1189 struct pcmcia_socket_entry_s *entry, **ptr;
1191 ptr = &pcmcia_sockets;
1192 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1193 if (entry->socket == socket) {
1199 void pcmcia_info(Monitor *mon)
1201 struct pcmcia_socket_entry_s *iter;
1203 if (!pcmcia_sockets)
1204 monitor_printf(mon, "No PCMCIA sockets\n");
1206 for (iter = pcmcia_sockets; iter; iter = iter->next)
1207 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1208 iter->socket->attached ? iter->socket->card_string :
1212 /***********************************************************/
1213 /* machine registration */
1215 static QEMUMachine *first_machine = NULL;
1216 QEMUMachine *current_machine = NULL;
1218 int qemu_register_machine(QEMUMachine *m)
1221 pm = &first_machine;
1229 static QEMUMachine *find_machine(const char *name)
1233 for(m = first_machine; m != NULL; m = m->next) {
1234 if (!strcmp(m->name, name))
1236 if (m->alias && !strcmp(m->alias, name))
1242 QEMUMachine *find_default_machine(void)
1246 for(m = first_machine; m != NULL; m = m->next) {
1247 if (m->is_default) {
1254 /***********************************************************/
1255 /* main execution loop */
1257 static void gui_update(void *opaque)
1259 uint64_t interval = GUI_REFRESH_INTERVAL;
1260 DisplayState *ds = opaque;
1261 DisplayChangeListener *dcl = ds->listeners;
1265 while (dcl != NULL) {
1266 if (dcl->gui_timer_interval &&
1267 dcl->gui_timer_interval < interval)
1268 interval = dcl->gui_timer_interval;
1271 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1274 struct vm_change_state_entry {
1275 VMChangeStateHandler *cb;
1277 QLIST_ENTRY (vm_change_state_entry) entries;
1280 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1282 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1285 VMChangeStateEntry *e;
1287 e = g_malloc0(sizeof (*e));
1291 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1295 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1297 QLIST_REMOVE (e, entries);
1301 void vm_state_notify(int running, RunState state)
1303 VMChangeStateEntry *e;
1305 trace_vm_state_notify(running, state);
1307 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1308 e->cb(e->opaque, running, state);
1314 if (!runstate_is_running()) {
1316 runstate_set(RUN_STATE_RUNNING);
1317 vm_state_notify(1, RUN_STATE_RUNNING);
1319 monitor_protocol_event(QEVENT_RESUME, NULL);
1323 /* reset/shutdown handler */
1325 typedef struct QEMUResetEntry {
1326 QTAILQ_ENTRY(QEMUResetEntry) entry;
1327 QEMUResetHandler *func;
1331 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1332 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1333 static int reset_requested;
1334 static int shutdown_requested, shutdown_signal = -1;
1335 static pid_t shutdown_pid;
1336 static int powerdown_requested;
1337 static int debug_requested;
1338 static int suspend_requested;
1339 static NotifierList suspend_notifiers =
1340 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1341 static NotifierList wakeup_notifiers =
1342 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1343 static uint32_t wakeup_reason_mask = ~0;
1344 static RunState vmstop_requested = RUN_STATE_MAX;
1346 int qemu_shutdown_requested_get(void)
1348 return shutdown_requested;
1351 int qemu_reset_requested_get(void)
1353 return reset_requested;
1356 int qemu_shutdown_requested(void)
1358 int r = shutdown_requested;
1359 shutdown_requested = 0;
1363 void qemu_kill_report(void)
1365 if (!qtest_enabled() && shutdown_signal != -1) {
1366 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1367 if (shutdown_pid == 0) {
1368 /* This happens for eg ^C at the terminal, so it's worth
1369 * avoiding printing an odd message in that case.
1371 fputc('\n', stderr);
1373 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1375 shutdown_signal = -1;
1379 int qemu_reset_requested(void)
1381 int r = reset_requested;
1382 reset_requested = 0;
1386 static int qemu_suspend_requested(void)
1388 int r = suspend_requested;
1389 suspend_requested = 0;
1393 int qemu_powerdown_requested(void)
1395 int r = powerdown_requested;
1396 powerdown_requested = 0;
1400 static int qemu_debug_requested(void)
1402 int r = debug_requested;
1403 debug_requested = 0;
1407 /* We use RUN_STATE_MAX but any invalid value will do */
1408 static bool qemu_vmstop_requested(RunState *r)
1410 if (vmstop_requested < RUN_STATE_MAX) {
1411 *r = vmstop_requested;
1412 vmstop_requested = RUN_STATE_MAX;
1419 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1421 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1424 re->opaque = opaque;
1425 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1428 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1432 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1433 if (re->func == func && re->opaque == opaque) {
1434 QTAILQ_REMOVE(&reset_handlers, re, entry);
1441 void qemu_system_reset(bool report)
1443 QEMUResetEntry *re, *nre;
1445 /* reset all devices */
1446 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1447 re->func(re->opaque);
1450 monitor_protocol_event(QEVENT_RESET, NULL);
1452 cpu_synchronize_all_post_reset();
1455 void qemu_system_reset_request(void)
1458 shutdown_requested = 1;
1460 reset_requested = 1;
1463 qemu_notify_event();
1466 static void qemu_system_suspend(void)
1469 notifier_list_notify(&suspend_notifiers, NULL);
1470 runstate_set(RUN_STATE_SUSPENDED);
1471 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1474 void qemu_system_suspend_request(void)
1476 if (runstate_check(RUN_STATE_SUSPENDED)) {
1479 suspend_requested = 1;
1481 qemu_notify_event();
1484 void qemu_register_suspend_notifier(Notifier *notifier)
1486 notifier_list_add(&suspend_notifiers, notifier);
1489 void qemu_system_wakeup_request(WakeupReason reason)
1491 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1494 if (!(wakeup_reason_mask & (1 << reason))) {
1497 runstate_set(RUN_STATE_RUNNING);
1498 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1499 notifier_list_notify(&wakeup_notifiers, &reason);
1500 reset_requested = 1;
1501 qemu_notify_event();
1504 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1507 wakeup_reason_mask |= (1 << reason);
1509 wakeup_reason_mask &= ~(1 << reason);
1513 void qemu_register_wakeup_notifier(Notifier *notifier)
1515 notifier_list_add(&wakeup_notifiers, notifier);
1518 void qemu_system_killed(int signal, pid_t pid)
1520 shutdown_signal = signal;
1525 shutdown_qemu_gracefully();
1527 qemu_system_shutdown_request();
1531 void qemu_system_shutdown_request(void)
1533 shutdown_requested = 1;
1534 qemu_notify_event();
1537 void qemu_system_powerdown_request(void)
1539 powerdown_requested = 1;
1540 qemu_notify_event();
1543 void qemu_system_debug_request(void)
1545 debug_requested = 1;
1546 qemu_notify_event();
1549 void qemu_system_vmstop_request(RunState state)
1551 vmstop_requested = state;
1552 qemu_notify_event();
1555 qemu_irq qemu_system_powerdown;
1557 static bool main_loop_should_exit(void)
1560 if (qemu_debug_requested()) {
1561 vm_stop(RUN_STATE_DEBUG);
1563 if (qemu_suspend_requested()) {
1564 qemu_system_suspend();
1566 if (qemu_shutdown_requested()) {
1568 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1570 vm_stop(RUN_STATE_SHUTDOWN);
1575 if (qemu_reset_requested()) {
1577 cpu_synchronize_all_states();
1578 qemu_system_reset(VMRESET_REPORT);
1580 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1581 runstate_check(RUN_STATE_SHUTDOWN)) {
1582 runstate_set(RUN_STATE_PAUSED);
1585 if (qemu_powerdown_requested()) {
1586 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1587 qemu_irq_raise(qemu_system_powerdown);
1589 if (qemu_vmstop_requested(&r)) {
1595 static void main_loop(void)
1599 #ifdef CONFIG_PROFILER
1606 nonblocking = !(kvm_enabled()|| hax_enabled()) && last_io > 0;
1607 #ifdef CONFIG_PROFILER
1608 ti = profile_getclock();
1610 last_io = main_loop_wait(nonblocking);
1611 #ifdef CONFIG_PROFILER
1612 dev_time += profile_getclock() - ti;
1614 } while (!main_loop_should_exit());
1617 static void version(void)
1619 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1622 static void help(int exitcode)
1625 printf("usage: %s [options] [disk_image]\n\n"
1626 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1627 error_get_progname());
1629 #define QEMU_OPTIONS_GENERATE_HELP
1630 #include "qemu-options-wrapper.h"
1632 printf("\nDuring emulation, the following keys are useful:\n"
1633 "ctrl-alt-f toggle full screen\n"
1634 "ctrl-alt-n switch to virtual console 'n'\n"
1635 "ctrl-alt toggle mouse and keyboard grab\n"
1637 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1642 #define HAS_ARG 0x0001
1644 typedef struct QEMUOption {
1651 static const QEMUOption qemu_options[] = {
1652 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1653 #define QEMU_OPTIONS_GENERATE_OPTIONS
1654 #include "qemu-options-wrapper.h"
1658 static bool vga_available(void)
1660 return qdev_exists("VGA") || qdev_exists("isa-vga");
1663 static bool cirrus_vga_available(void)
1665 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1668 static bool vmware_vga_available(void)
1670 return qdev_exists("vmware-svga");
1673 static void select_vgahw (const char *p)
1677 vga_interface_type = VGA_NONE;
1678 if (strstart(p, "std", &opts)) {
1679 if (vga_available()) {
1680 vga_interface_type = VGA_STD;
1682 fprintf(stderr, "Error: standard VGA not available\n");
1685 } else if (strstart(p, "cirrus", &opts)) {
1686 if (cirrus_vga_available()) {
1687 vga_interface_type = VGA_CIRRUS;
1689 fprintf(stderr, "Error: Cirrus VGA not available\n");
1692 } else if (strstart(p, "vmware", &opts)) {
1693 if (vmware_vga_available()) {
1694 vga_interface_type = VGA_VMWARE;
1696 fprintf(stderr, "Error: VMWare SVGA not available\n");
1699 } else if (strstart(p, "xenfb", &opts)) {
1700 vga_interface_type = VGA_XENFB;
1701 } else if (strstart(p, "qxl", &opts)) {
1702 vga_interface_type = VGA_QXL;
1704 } else if (strstart(p, "maru", &opts)) {
1705 vga_interface_type = VGA_MARU;
1707 } else if (!strstart(p, "none", &opts)) {
1709 fprintf(stderr, "Unknown vga type: %s\n", p);
1713 const char *nextopt;
1715 if (strstart(opts, ",retrace=", &nextopt)) {
1717 if (strstart(opts, "dumb", &nextopt))
1718 vga_retrace_method = VGA_RETRACE_DUMB;
1719 else if (strstart(opts, "precise", &nextopt))
1720 vga_retrace_method = VGA_RETRACE_PRECISE;
1721 else goto invalid_vga;
1722 } else goto invalid_vga;
1727 static DisplayType select_display(const char *p)
1730 DisplayType display = DT_DEFAULT;
1732 if (strstart(p, "sdl", &opts)) {
1736 const char *nextopt;
1738 if (strstart(opts, ",frame=", &nextopt)) {
1740 if (strstart(opts, "on", &nextopt)) {
1742 } else if (strstart(opts, "off", &nextopt)) {
1745 goto invalid_sdl_args;
1747 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1749 if (strstart(opts, "on", &nextopt)) {
1751 } else if (strstart(opts, "off", &nextopt)) {
1754 goto invalid_sdl_args;
1756 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1758 if (strstart(opts, "on", &nextopt)) {
1760 } else if (strstart(opts, "off", &nextopt)) {
1763 goto invalid_sdl_args;
1765 } else if (strstart(opts, ",window_close=", &nextopt)) {
1767 if (strstart(opts, "on", &nextopt)) {
1769 } else if (strstart(opts, "off", &nextopt)) {
1772 goto invalid_sdl_args;
1776 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1782 fprintf(stderr, "SDL support is disabled\n");
1785 } else if (strstart(p, "vnc", &opts)) {
1790 const char *nextopt;
1792 if (strstart(opts, "=", &nextopt)) {
1793 vnc_display = nextopt;
1797 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1801 fprintf(stderr, "VNC support is disabled\n");
1804 } else if (strstart(p, "curses", &opts)) {
1805 #ifdef CONFIG_CURSES
1806 display = DT_CURSES;
1808 fprintf(stderr, "Curses support is disabled\n");
1811 } else if (strstart(p, "none", &opts)) {
1814 fprintf(stderr, "Unknown display type: %s\n", p);
1821 static int balloon_parse(const char *arg)
1825 if (strcmp(arg, "none") == 0) {
1829 if (!strncmp(arg, "virtio", 6)) {
1830 if (arg[6] == ',') {
1831 /* have params -> parse them */
1832 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1836 /* create empty opts */
1837 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1839 qemu_opt_set(opts, "driver", "virtio-balloon");
1846 char *qemu_find_file(int type, const char *name)
1852 /* If name contains path separators then try it as a straight path. */
1853 if ((strchr(name, '/') || strchr(name, '\\'))
1854 && access(name, R_OK) == 0) {
1855 return g_strdup(name);
1858 case QEMU_FILE_TYPE_BIOS:
1861 case QEMU_FILE_TYPE_KEYMAP:
1862 subdir = "keymaps/";
1867 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1868 buf = g_malloc0(len);
1869 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1870 if (access(buf, R_OK)) {
1878 const char *qemu_get_data_dir(void);
1880 const char *qemu_get_data_dir(void)
1886 static int device_help_func(QemuOpts *opts, void *opaque)
1888 return qdev_device_help(opts);
1891 static int device_init_func(QemuOpts *opts, void *opaque)
1895 #ifdef CONFIG_VIRTIO_GL
1896 #if defined(CONFIG_MARU) && (!defined(CONFIG_DARWIN))
1897 // virtio-gl pci device
1899 // ignore virtio-gl-pci device, even if users set it in option.
1900 const char *driver = qemu_opt_get(opts, "driver");
1901 if (driver && (strcmp (driver, VIRTIOGL_DEV_NAME) == 0)) {
1908 dev = qdev_device_add(opts);
1914 static int chardev_init_func(QemuOpts *opts, void *opaque)
1916 CharDriverState *chr;
1918 chr = qemu_chr_new_from_opts(opts, NULL);
1924 #ifdef CONFIG_VIRTFS
1925 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1928 ret = qemu_fsdev_add(opts);
1934 static int mon_init_func(QemuOpts *opts, void *opaque)
1936 CharDriverState *chr;
1937 const char *chardev;
1941 mode = qemu_opt_get(opts, "mode");
1945 if (strcmp(mode, "readline") == 0) {
1946 flags = MONITOR_USE_READLINE;
1947 } else if (strcmp(mode, "control") == 0) {
1948 flags = MONITOR_USE_CONTROL;
1950 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1954 if (qemu_opt_get_bool(opts, "pretty", 0))
1955 flags |= MONITOR_USE_PRETTY;
1957 if (qemu_opt_get_bool(opts, "default", 0))
1958 flags |= MONITOR_IS_DEFAULT;
1960 chardev = qemu_opt_get(opts, "chardev");
1961 chr = qemu_chr_find(chardev);
1963 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1967 monitor_init(chr, flags);
1971 static void monitor_parse(const char *optarg, const char *mode)
1973 static int monitor_device_index = 0;
1979 if (strstart(optarg, "chardev:", &p)) {
1980 snprintf(label, sizeof(label), "%s", p);
1982 snprintf(label, sizeof(label), "compat_monitor%d",
1983 monitor_device_index);
1984 if (monitor_device_index == 0) {
1987 opts = qemu_chr_parse_compat(label, optarg);
1989 fprintf(stderr, "parse error: %s\n", optarg);
1994 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1996 fprintf(stderr, "duplicate chardev: %s\n", label);
1999 qemu_opt_set(opts, "mode", mode);
2000 qemu_opt_set(opts, "chardev", label);
2002 qemu_opt_set(opts, "default", "on");
2003 monitor_device_index++;
2006 struct device_config {
2008 DEV_USB, /* -usbdevice */
2010 DEV_SERIAL, /* -serial */
2011 DEV_PARALLEL, /* -parallel */
2012 DEV_VIRTCON, /* -virtioconsole */
2013 DEV_DEBUGCON, /* -debugcon */
2014 DEV_GDB, /* -gdb, -s */
2016 const char *cmdline;
2018 QTAILQ_ENTRY(device_config) next;
2020 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
2022 static void add_device_config(int type, const char *cmdline)
2024 struct device_config *conf;
2026 conf = g_malloc0(sizeof(*conf));
2028 conf->cmdline = cmdline;
2029 loc_save(&conf->loc);
2030 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2033 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2035 struct device_config *conf;
2038 QTAILQ_FOREACH(conf, &device_configs, next) {
2039 if (conf->type != type)
2041 loc_push_restore(&conf->loc);
2042 rc = func(conf->cmdline);
2043 loc_pop(&conf->loc);
2050 static int serial_parse(const char *devname)
2052 static int index = 0;
2055 if (strcmp(devname, "none") == 0)
2057 if (index == MAX_SERIAL_PORTS) {
2058 fprintf(stderr, "qemu: too many serial ports\n");
2061 snprintf(label, sizeof(label), "serial%d", index);
2062 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2063 if (!serial_hds[index]) {
2064 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2065 devname, strerror(errno));
2072 static int parallel_parse(const char *devname)
2074 static int index = 0;
2077 if (strcmp(devname, "none") == 0)
2079 if (index == MAX_PARALLEL_PORTS) {
2080 fprintf(stderr, "qemu: too many parallel ports\n");
2083 snprintf(label, sizeof(label), "parallel%d", index);
2084 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2085 if (!parallel_hds[index]) {
2086 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2087 devname, strerror(errno));
2094 static int virtcon_parse(const char *devname)
2096 QemuOptsList *device = qemu_find_opts("device");
2097 static int index = 0;
2099 QemuOpts *bus_opts, *dev_opts;
2101 if (strcmp(devname, "none") == 0)
2103 if (index == MAX_VIRTIO_CONSOLES) {
2104 fprintf(stderr, "qemu: too many virtio consoles\n");
2108 bus_opts = qemu_opts_create(device, NULL, 0);
2109 if (arch_type == QEMU_ARCH_S390X) {
2110 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2112 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2115 dev_opts = qemu_opts_create(device, NULL, 0);
2116 qemu_opt_set(dev_opts, "driver", "virtconsole");
2118 snprintf(label, sizeof(label), "virtcon%d", index);
2119 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2120 if (!virtcon_hds[index]) {
2121 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2122 devname, strerror(errno));
2125 qemu_opt_set(dev_opts, "chardev", label);
2131 static int debugcon_parse(const char *devname)
2135 if (!qemu_chr_new("debugcon", devname, NULL)) {
2138 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2140 fprintf(stderr, "qemu: already have a debugcon device\n");
2143 qemu_opt_set(opts, "driver", "isa-debugcon");
2144 qemu_opt_set(opts, "chardev", "debugcon");
2148 static QEMUMachine *machine_parse(const char *name)
2150 QEMUMachine *m, *machine = NULL;
2153 machine = find_machine(name);
2158 printf("Supported machines are:\n");
2159 for (m = first_machine; m != NULL; m = m->next) {
2161 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2163 printf("%-20s %s%s\n", m->name, m->desc,
2164 m->is_default ? " (default)" : "");
2166 exit(!name || *name != '?');
2169 static int tcg_init(void)
2172 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2173 ret = hax_accel_init();
2178 const char *opt_name;
2180 int (*available)(void);
2184 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2185 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2186 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2187 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2190 static int configure_accelerator(void)
2192 const char *p = NULL;
2195 bool accel_initialised = 0;
2196 bool init_failed = 0;
2198 QemuOptsList *list = qemu_find_opts("machine");
2199 if (!QTAILQ_EMPTY(&list->head)) {
2200 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2204 /* Use the default "accelerator", tcg */
2208 while (!accel_initialised && *p != '\0') {
2212 p = get_opt_name(buf, sizeof (buf), p, ':');
2213 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2214 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2215 *(accel_list[i].allowed) = 1;
2216 ret = accel_list[i].init();
2219 if (!accel_list[i].available()) {
2220 printf("%s not supported for this target\n",
2221 accel_list[i].name);
2223 fprintf(stderr, "failed to initialize %s: %s\n",
2227 *(accel_list[i].allowed) = 0;
2229 accel_initialised = 1;
2234 if (i == ARRAY_SIZE(accel_list)) {
2235 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2239 if (!accel_initialised) {
2240 fprintf(stderr, "No accelerator found!\n");
2242 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "No accelerator found.");
2248 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2251 return !accel_initialised;
2254 void qemu_add_exit_notifier(Notifier *notify)
2256 notifier_list_add(&exit_notifiers, notify);
2259 void qemu_remove_exit_notifier(Notifier *notify)
2261 notifier_remove(notify);
2264 static void qemu_run_exit_notifiers(void)
2266 notifier_list_notify(&exit_notifiers, NULL);
2269 void qemu_add_machine_init_done_notifier(Notifier *notify)
2271 notifier_list_add(&machine_init_done_notifiers, notify);
2274 static void qemu_run_machine_init_done_notifiers(void)
2276 notifier_list_notify(&machine_init_done_notifiers, NULL);
2279 static const QEMUOption *lookup_opt(int argc, char **argv,
2280 const char **poptarg, int *poptind)
2282 const QEMUOption *popt;
2283 int optind = *poptind;
2284 char *r = argv[optind];
2287 loc_set_cmdline(argv, optind, 1);
2289 /* Treat --foo the same as -foo. */
2292 popt = qemu_options;
2295 error_report("invalid option");
2297 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "invalid option.");
2301 if (!strcmp(popt->name, r + 1))
2305 if (popt->flags & HAS_ARG) {
2306 if (optind >= argc) {
2307 error_report("requires an argument");
2309 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "requires an argument.");
2313 optarg = argv[optind++];
2314 loc_set_cmdline(argv, optind - 2, 2);
2325 static gpointer malloc_and_trace(gsize n_bytes)
2327 void *ptr = malloc(n_bytes);
2328 trace_g_malloc(n_bytes, ptr);
2332 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2334 void *ptr = realloc(mem, n_bytes);
2335 trace_g_realloc(mem, n_bytes, ptr);
2339 static void free_and_trace(gpointer mem)
2345 // virtio-gl pci device lookup
2347 const char *device_name;
2349 } device_opt_finding_t;
2351 static int find_device_opt (QemuOpts *opts, void *opaque)
2353 device_opt_finding_t *devp = (device_opt_finding_t *) opaque;
2354 if (devp->found == 1) {
2358 const char *str = qemu_opt_get (opts, "driver");
2359 if (strcmp (str, devp->device_name) == 0) {
2365 int use_qemu_display = 0; //0:use tizen qemu sdl, 1:use original qemu sdl
2367 int qemu_init_main_loop(void)
2369 return main_loop_init();
2372 int main(int argc, char **argv, char **envp)
2375 int snapshot, linux_boot;
2376 const char *icount_option = NULL;
2377 const char *initrd_filename;
2378 const char *kernel_filename, *kernel_cmdline;
2379 char* tmp_cmdline = NULL;
2380 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2382 DisplayChangeListener *dcl;
2383 int cyls, heads, secs, translation;
2384 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2385 QemuOptsList *olist;
2388 const char *loadvm = NULL;
2389 QEMUMachine *machine;
2390 const char *cpu_model;
2391 const char *vga_model = "none";
2392 const char *pid_file = NULL;
2393 const char *incoming = NULL;
2395 int show_vnc_port = 0;
2397 bool defconfig = true;
2398 bool userconfig = true;
2399 const char *log_mask = NULL;
2400 const char *log_file = NULL;
2401 GMemVTable mem_trace = {
2402 .malloc = malloc_and_trace,
2403 .realloc = realloc_and_trace,
2404 .free = free_and_trace,
2406 const char *trace_events = NULL;
2407 const char *trace_file = NULL;
2411 char http_proxy[MIDBUF] ={0},https_proxy[MIDBUF] = {0,},
2412 ftp_proxy[MIDBUF] = {0,}, socks_proxy[MIDBUF] = {0,},
2413 dns1[MIDBUF] = {0}, dns2[MIDBUF] = {0};
2416 atexit(qemu_run_exit_notifiers);
2417 error_set_progname(argv[0]);
2419 g_mem_set_vtable(&mem_trace);
2420 if (!g_thread_supported()) {
2421 #if !GLIB_CHECK_VERSION(2, 31, 0)
2422 g_thread_init(NULL);
2424 fprintf(stderr, "glib threading failed to initialize.\n");
2429 module_call_init(MODULE_INIT_QOM);
2434 rtc_clock = host_clock;
2436 qemu_cache_utils_init(envp);
2438 QLIST_INIT (&vm_change_state_head);
2439 os_setup_early_signal_handling();
2441 module_call_init(MODULE_INIT_MACHINE);
2442 machine = find_default_machine();
2446 cyls = heads = secs = 0;
2447 translation = BIOS_ATA_TRANSLATION_AUTO;
2449 for (i = 0; i < MAX_NODES; i++) {
2451 node_cpumask[i] = 0;
2459 /* first pass of option parsing */
2461 while (optind < argc) {
2462 if (argv[optind][0] != '-') {
2467 const QEMUOption *popt;
2469 popt = lookup_opt(argc, argv, &optarg, &optind);
2470 switch (popt->index) {
2471 case QEMU_OPTION_nodefconfig:
2474 case QEMU_OPTION_nouserconfig:
2483 ret = qemu_read_default_config_files(userconfig);
2489 /* second pass of option parsing */
2494 if (argv[optind][0] != '-') {
2495 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2497 const QEMUOption *popt;
2499 popt = lookup_opt(argc, argv, &optarg, &optind);
2500 if (!(popt->arch_mask & arch_type)) {
2501 printf("Option %s not supported for this target\n", popt->name);
2504 switch(popt->index) {
2506 machine = machine_parse(optarg);
2508 case QEMU_OPTION_cpu:
2509 /* hw initialization will check this */
2512 case QEMU_OPTION_hda:
2516 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2518 snprintf(buf, sizeof(buf),
2519 "%s,cyls=%d,heads=%d,secs=%d%s",
2520 HD_OPTS , cyls, heads, secs,
2521 translation == BIOS_ATA_TRANSLATION_LBA ?
2523 translation == BIOS_ATA_TRANSLATION_NONE ?
2524 ",trans=none" : "");
2525 drive_add(IF_DEFAULT, 0, optarg, buf);
2528 case QEMU_OPTION_hdb:
2529 case QEMU_OPTION_hdc:
2530 case QEMU_OPTION_hdd:
2531 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2534 case QEMU_OPTION_drive:
2535 if (drive_def(optarg) == NULL) {
2539 case QEMU_OPTION_set:
2540 if (qemu_set_option(optarg) != 0)
2543 case QEMU_OPTION_global:
2544 if (qemu_global_option(optarg) != 0)
2547 case QEMU_OPTION_mtdblock:
2548 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2550 case QEMU_OPTION_sd:
2551 drive_add(IF_SD, 0, optarg, SD_OPTS);
2553 case QEMU_OPTION_pflash:
2554 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2556 case QEMU_OPTION_snapshot:
2559 case QEMU_OPTION_hdachs:
2563 cyls = strtol(p, (char **)&p, 0);
2564 if (cyls < 1 || cyls > 16383)
2569 heads = strtol(p, (char **)&p, 0);
2570 if (heads < 1 || heads > 16)
2575 secs = strtol(p, (char **)&p, 0);
2576 if (secs < 1 || secs > 63)
2580 if (!strcmp(p, "none"))
2581 translation = BIOS_ATA_TRANSLATION_NONE;
2582 else if (!strcmp(p, "lba"))
2583 translation = BIOS_ATA_TRANSLATION_LBA;
2584 else if (!strcmp(p, "auto"))
2585 translation = BIOS_ATA_TRANSLATION_AUTO;
2588 } else if (*p != '\0') {
2590 fprintf(stderr, "qemu: invalid physical CHS format\n");
2593 if (hda_opts != NULL) {
2595 snprintf(num, sizeof(num), "%d", cyls);
2596 qemu_opt_set(hda_opts, "cyls", num);
2597 snprintf(num, sizeof(num), "%d", heads);
2598 qemu_opt_set(hda_opts, "heads", num);
2599 snprintf(num, sizeof(num), "%d", secs);
2600 qemu_opt_set(hda_opts, "secs", num);
2601 if (translation == BIOS_ATA_TRANSLATION_LBA)
2602 qemu_opt_set(hda_opts, "trans", "lba");
2603 if (translation == BIOS_ATA_TRANSLATION_NONE)
2604 qemu_opt_set(hda_opts, "trans", "none");
2608 case QEMU_OPTION_numa:
2609 if (nb_numa_nodes >= MAX_NODES) {
2610 fprintf(stderr, "qemu: too many NUMA nodes\n");
2615 case QEMU_OPTION_display:
2616 display_type = select_display(optarg);
2618 case QEMU_OPTION_nographic:
2619 display_type = DT_NOGRAPHIC;
2621 case QEMU_OPTION_curses:
2622 #ifdef CONFIG_CURSES
2623 display_type = DT_CURSES;
2625 fprintf(stderr, "Curses support is disabled\n");
2629 case QEMU_OPTION_portrait:
2630 graphic_rotate = 90;
2632 case QEMU_OPTION_rotate:
2633 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2634 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2635 graphic_rotate != 180 && graphic_rotate != 270) {
2637 "qemu: only 90, 180, 270 deg rotation is available\n");
2641 case QEMU_OPTION_kernel:
2642 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2644 case QEMU_OPTION_initrd:
2645 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
2647 case QEMU_OPTION_append:
2649 gethostproxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
2650 gethostDNS(dns1, dns2);
2651 tmp_cmdline = g_strdup_printf("%s sdb_port=%d,"
2652 " http_proxy=%s https_proxy=%s ftp_proxy=%s socks_proxy=%s"
2653 " dns1=%s dns2=%s", optarg, tizen_base_port,
2654 http_proxy, https_proxy, ftp_proxy, socks_proxy,
2656 qemu_opts_set(qemu_find_opts("machine"), 0, "append",
2658 fprintf(stdout, "kernel command : %s\n", tmp_cmdline);
2660 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
2663 case QEMU_OPTION_dtb:
2664 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2666 case QEMU_OPTION_cdrom:
2667 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2669 case QEMU_OPTION_boot:
2671 static const char * const params[] = {
2672 "order", "once", "menu",
2673 "splash", "splash-time", NULL
2675 char buf[sizeof(boot_devices)];
2676 char *standard_boot_devices;
2679 if (!strchr(optarg, '=')) {
2681 pstrcpy(buf, sizeof(buf), optarg);
2682 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2684 "qemu: unknown boot parameter '%s' in '%s'\n",
2690 get_param_value(buf, sizeof(buf), "order", optarg)) {
2691 validate_bootdevices(buf);
2692 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2695 if (get_param_value(buf, sizeof(buf),
2697 validate_bootdevices(buf);
2698 standard_boot_devices = g_strdup(boot_devices);
2699 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2700 qemu_register_reset(restore_boot_devices,
2701 standard_boot_devices);
2703 if (get_param_value(buf, sizeof(buf),
2705 if (!strcmp(buf, "on")) {
2707 } else if (!strcmp(buf, "off")) {
2711 "qemu: invalid option value '%s'\n",
2716 qemu_opts_parse(qemu_find_opts("boot-opts"),
2721 case QEMU_OPTION_fda:
2722 case QEMU_OPTION_fdb:
2723 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2726 case QEMU_OPTION_no_fd_bootchk:
2729 case QEMU_OPTION_netdev:
2730 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2734 case QEMU_OPTION_net:
2735 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2739 #ifdef CONFIG_LIBISCSI
2740 case QEMU_OPTION_iscsi:
2741 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2748 case QEMU_OPTION_tftp:
2749 legacy_tftp_prefix = optarg;
2751 case QEMU_OPTION_bootp:
2752 legacy_bootp_filename = optarg;
2754 case QEMU_OPTION_redir:
2755 if (net_slirp_redir(optarg) < 0)
2759 case QEMU_OPTION_bt:
2760 add_device_config(DEV_BT, optarg);
2762 case QEMU_OPTION_audio_help:
2763 if (!(audio_available())) {
2764 printf("Option %s not supported for this target\n", popt->name);
2770 case QEMU_OPTION_soundhw:
2771 if (!(audio_available())) {
2772 printf("Option %s not supported for this target\n", popt->name);
2775 select_soundhw (optarg);
2780 case QEMU_OPTION_version:
2784 case QEMU_OPTION_m: {
2788 value = strtosz(optarg, &end);
2789 if (value < 0 || *end) {
2790 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2794 if (value != (uint64_t)(ram_addr_t)value) {
2795 fprintf(stderr, "qemu: ram size too large\n");
2801 case QEMU_OPTION_mempath:
2805 case QEMU_OPTION_mem_prealloc:
2816 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2818 case QEMU_OPTION_gdb:
2819 add_device_config(DEV_GDB, optarg);
2824 case QEMU_OPTION_bios:
2827 case QEMU_OPTION_singlestep:
2834 keyboard_layout = optarg;
2836 case QEMU_OPTION_localtime:
2839 case QEMU_OPTION_vga:
2848 w = strtol(p, (char **)&p, 10);
2851 fprintf(stderr, "qemu: invalid resolution or depth\n");
2857 h = strtol(p, (char **)&p, 10);
2862 depth = strtol(p, (char **)&p, 10);
2863 if (depth != 8 && depth != 15 && depth != 16 &&
2864 depth != 24 && depth != 32)
2866 } else if (*p == '\0') {
2867 depth = graphic_depth;
2874 graphic_depth = depth;
2877 case QEMU_OPTION_echr:
2880 term_escape_char = strtol(optarg, &r, 0);
2882 printf("Bad argument to echr\n");
2885 case QEMU_OPTION_monitor:
2886 monitor_parse(optarg, "readline");
2887 default_monitor = 0;
2889 case QEMU_OPTION_qmp:
2890 monitor_parse(optarg, "control");
2891 default_monitor = 0;
2893 case QEMU_OPTION_mon:
2894 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2898 default_monitor = 0;
2900 case QEMU_OPTION_chardev:
2901 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2906 case QEMU_OPTION_fsdev:
2907 olist = qemu_find_opts("fsdev");
2909 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2912 opts = qemu_opts_parse(olist, optarg, 1);
2914 fprintf(stderr, "parse error: %s\n", optarg);
2918 case QEMU_OPTION_virtfs: {
2921 const char *writeout, *sock_fd, *socket;
2923 olist = qemu_find_opts("virtfs");
2925 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2928 opts = qemu_opts_parse(olist, optarg, 1);
2930 fprintf(stderr, "parse error: %s\n", optarg);
2934 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2935 qemu_opt_get(opts, "mount_tag") == NULL) {
2936 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2939 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2940 qemu_opt_get(opts, "mount_tag"), 1);
2942 fprintf(stderr, "duplicate fsdev id: %s\n",
2943 qemu_opt_get(opts, "mount_tag"));
2947 writeout = qemu_opt_get(opts, "writeout");
2949 #ifdef CONFIG_SYNC_FILE_RANGE
2950 qemu_opt_set(fsdev, "writeout", writeout);
2952 fprintf(stderr, "writeout=immediate not supported on "
2957 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2958 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2959 qemu_opt_set(fsdev, "security_model",
2960 qemu_opt_get(opts, "security_model"));
2961 socket = qemu_opt_get(opts, "socket");
2963 qemu_opt_set(fsdev, "socket", socket);
2965 sock_fd = qemu_opt_get(opts, "sock_fd");
2967 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2970 qemu_opt_set_bool(fsdev, "readonly",
2971 qemu_opt_get_bool(opts, "readonly", 0));
2972 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2973 qemu_opt_set(device, "driver", "virtio-9p-pci");
2974 qemu_opt_set(device, "fsdev",
2975 qemu_opt_get(opts, "mount_tag"));
2976 qemu_opt_set(device, "mount_tag",
2977 qemu_opt_get(opts, "mount_tag"));
2980 case QEMU_OPTION_virtfs_synth: {
2984 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2986 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2989 qemu_opt_set(fsdev, "fsdriver", "synth");
2991 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2992 qemu_opt_set(device, "driver", "virtio-9p-pci");
2993 qemu_opt_set(device, "fsdev", "v_synth");
2994 qemu_opt_set(device, "mount_tag", "v_synth");
2997 case QEMU_OPTION_serial:
2998 add_device_config(DEV_SERIAL, optarg);
3000 if (strncmp(optarg, "mon:", 4) == 0) {
3001 default_monitor = 0;
3004 case QEMU_OPTION_watchdog:
3007 "qemu: only one watchdog option may be given\n");
3012 case QEMU_OPTION_watchdog_action:
3013 if (select_watchdog_action(optarg) == -1) {
3014 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3018 case QEMU_OPTION_virtiocon:
3019 add_device_config(DEV_VIRTCON, optarg);
3020 default_virtcon = 0;
3021 if (strncmp(optarg, "mon:", 4) == 0) {
3022 default_monitor = 0;
3025 case QEMU_OPTION_parallel:
3026 add_device_config(DEV_PARALLEL, optarg);
3027 default_parallel = 0;
3028 if (strncmp(optarg, "mon:", 4) == 0) {
3029 default_monitor = 0;
3032 case QEMU_OPTION_debugcon:
3033 add_device_config(DEV_DEBUGCON, optarg);
3035 case QEMU_OPTION_loadvm:
3038 case QEMU_OPTION_full_screen:
3042 case QEMU_OPTION_no_frame:
3045 case QEMU_OPTION_alt_grab:
3048 case QEMU_OPTION_ctrl_grab:
3051 case QEMU_OPTION_no_quit:
3054 case QEMU_OPTION_sdl:
3055 display_type = DT_SDL;
3058 case QEMU_OPTION_no_frame:
3059 case QEMU_OPTION_alt_grab:
3060 case QEMU_OPTION_ctrl_grab:
3061 case QEMU_OPTION_no_quit:
3062 case QEMU_OPTION_sdl:
3063 fprintf(stderr, "SDL support is disabled\n");
3066 case QEMU_OPTION_pidfile:
3069 case QEMU_OPTION_win2k_hack:
3070 win2k_install_hack = 1;
3072 case QEMU_OPTION_rtc_td_hack: {
3073 static GlobalProperty slew_lost_ticks[] = {
3075 .driver = "mc146818rtc",
3076 .property = "lost_tick_policy",
3079 { /* end of list */ }
3082 qdev_prop_register_global_list(slew_lost_ticks);
3085 case QEMU_OPTION_acpitable:
3086 do_acpitable_option(optarg);
3088 case QEMU_OPTION_smbios:
3089 do_smbios_option(optarg);
3091 #ifdef CONFIG_OPENGLES
3092 case QEMU_OPTION_gles2_quality:
3093 gles2_quality = strtoul(optarg, NULL, 10);
3096 case QEMU_OPTION_enable_kvm:
3097 olist = qemu_find_opts("machine");
3098 qemu_opts_parse(olist, "accel=kvm", 0);
3100 case QEMU_OPTION_enable_gl:
3101 #ifdef CONFIG_VIRTIO_GL
3102 #if defined(CONFIG_MARU) && (!defined(CONFIG_DARWIN))
3106 fprintf(stderr, "Virtio GL support is disabled, ignoring -enable-gl\n");
3109 case QEMU_OPTION_machine:
3110 olist = qemu_find_opts("machine");
3111 opts = qemu_opts_parse(olist, optarg, 1);
3113 fprintf(stderr, "parse error: %s\n", optarg);
3116 optarg = qemu_opt_get(opts, "type");
3118 machine = machine_parse(optarg);
3121 case QEMU_OPTION_usb:
3124 case QEMU_OPTION_usbdevice:
3126 add_device_config(DEV_USB, optarg);
3128 case QEMU_OPTION_device:
3129 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3133 case QEMU_OPTION_smp:
3136 fprintf(stderr, "Invalid number of CPUs\n");
3139 if (max_cpus < smp_cpus) {
3140 fprintf(stderr, "maxcpus must be equal to or greater than "
3144 if (max_cpus > 255) {
3145 fprintf(stderr, "Unsupported number of maxcpus\n");
3149 case QEMU_OPTION_vnc:
3152 vnc_display = optarg;
3154 fprintf(stderr, "VNC support is disabled\n");
3158 case QEMU_OPTION_no_acpi:
3161 case QEMU_OPTION_no_hpet:
3164 case QEMU_OPTION_balloon:
3165 if (balloon_parse(optarg) < 0) {
3166 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3170 case QEMU_OPTION_no_reboot:
3173 case QEMU_OPTION_no_shutdown:
3176 case QEMU_OPTION_show_cursor:
3179 case QEMU_OPTION_uuid:
3180 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3181 fprintf(stderr, "Fail to parse UUID string."
3182 " Wrong format.\n");
3186 case QEMU_OPTION_option_rom:
3187 if (nb_option_roms >= MAX_OPTION_ROMS) {
3188 fprintf(stderr, "Too many option ROMs\n");
3191 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3192 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3193 option_rom[nb_option_roms].bootindex =
3194 qemu_opt_get_number(opts, "bootindex", -1);
3195 if (!option_rom[nb_option_roms].name) {
3196 fprintf(stderr, "Option ROM file is not specified\n");
3201 case QEMU_OPTION_semihosting:
3202 semihosting_enabled = 1;
3204 case QEMU_OPTION_name:
3205 qemu_name = g_strdup(optarg);
3207 char *p = strchr(qemu_name, ',');
3210 if (strncmp(p, "process=", 8)) {
3211 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3215 os_set_proc_name(p);
3219 case QEMU_OPTION_prom_env:
3220 if (nb_prom_envs >= MAX_PROM_ENVS) {
3221 fprintf(stderr, "Too many prom variables\n");
3224 prom_envs[nb_prom_envs] = optarg;
3227 case QEMU_OPTION_old_param:
3230 case QEMU_OPTION_clock:
3231 configure_alarms(optarg);
3233 case QEMU_OPTION_startdate:
3234 configure_rtc_date_offset(optarg, 1);
3236 case QEMU_OPTION_rtc:
3237 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3241 configure_rtc(opts);
3243 case QEMU_OPTION_tb_size:
3244 tcg_tb_size = strtol(optarg, NULL, 0);
3245 if (tcg_tb_size < 0) {
3249 case QEMU_OPTION_icount:
3250 icount_option = optarg;
3252 case QEMU_OPTION_incoming:
3254 runstate_set(RUN_STATE_INMIGRATE);
3256 case QEMU_OPTION_nodefaults:
3258 default_parallel = 0;
3259 default_virtcon = 0;
3260 default_monitor = 0;
3267 case QEMU_OPTION_xen_domid:
3268 if (!(xen_available())) {
3269 printf("Option %s not supported for this target\n", popt->name);
3272 xen_domid = atoi(optarg);
3274 case QEMU_OPTION_xen_create:
3275 if (!(xen_available())) {
3276 printf("Option %s not supported for this target\n", popt->name);
3279 xen_mode = XEN_CREATE;
3281 case QEMU_OPTION_xen_attach:
3282 if (!(xen_available())) {
3283 printf("Option %s not supported for this target\n", popt->name);
3286 xen_mode = XEN_ATTACH;
3288 case QEMU_OPTION_trace:
3290 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3294 trace_events = qemu_opt_get(opts, "events");
3295 trace_file = qemu_opt_get(opts, "file");
3298 case QEMU_OPTION_readconfig:
3300 int ret = qemu_read_config_file(optarg);
3302 fprintf(stderr, "read config %s: %s\n", optarg,
3308 case QEMU_OPTION_spice:
3309 olist = qemu_find_opts("spice");
3311 fprintf(stderr, "spice is not supported by this qemu build.\n");
3314 opts = qemu_opts_parse(olist, optarg, 0);
3316 fprintf(stderr, "parse error: %s\n", optarg);
3320 case QEMU_OPTION_writeconfig:
3323 if (strcmp(optarg, "-") == 0) {
3326 fp = fopen(optarg, "w");
3328 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3332 qemu_config_write(fp);
3336 case QEMU_OPTION_qtest:
3337 qtest_chrdev = optarg;
3339 case QEMU_OPTION_qtest_log:
3342 case QEMU_OPTION_enable_hax:
3343 #ifdef CONFIG_HAX_BACKEND
3344 olist = qemu_find_opts("machine");
3345 qemu_opts_reset(olist);
3347 //qemu_opts_parse(olist, "accel=hax", 0);
3350 "HAX support is disabled, ignoring -enable-hax\n");
3354 case QEMU_OPTION_max_touch_point:
3356 int cnt = atoi(optarg);
3357 fprintf(stderr, "maxTouchPoint:%d\n", cnt);
3358 set_emul_max_touch_point(cnt);
3361 case QEMU_OPTION_disable_skin:
3366 os_parse_cmd_args(popt->index, optarg);
3372 /* Init CPU def lists, based on config
3373 * - Must be called after all the qemu_read_config_file() calls
3374 * - Must be called before list_cpus()
3375 * - Must be called before machine->init()
3379 if (cpu_model && *cpu_model == '?') {
3380 list_cpus(stdout, &fprintf, cpu_model);
3384 capability_check_gl = gl_acceleration_capability_check();
3385 if (enable_gl && (capability_check_gl != 0)) {
3387 fprintf (stderr, "Warn: GL acceleration was disabled due to the fail of GL check!\n");
3390 #ifdef CONFIG_VIRTIO_GL
3391 #if defined(CONFIG_MARU) && (!defined(CONFIG_DARWIN))
3393 device_opt_finding_t devp = {VIRTIOGL_DEV_NAME, 0};
3394 qemu_opts_foreach(qemu_find_opts("device"), find_device_opt, &devp, 0);
3395 if (devp.found == 0) {
3396 if (!qemu_opts_parse(qemu_find_opts("device"), VIRTIOGL_DEV_NAME, "driver")) {
3403 // To check host gl driver capability and notify to guest.
3404 gchar *tmp = tmp_cmdline;
3405 tmp_cmdline = g_strdup_printf("%s gles=%d", tmp, enable_gl);
3406 qemu_opts_set(qemu_find_opts("machine"), 0, "append", tmp_cmdline);
3407 fprintf(stdout, "kernel command : %s\n", tmp_cmdline);
3410 /* Open the logfile at this point, if necessary. We can't open the logfile
3411 * when encountering either of the logging options (-d or -D) because the
3412 * other one may be encountered later on the command line, changing the
3413 * location or level of logging.
3417 set_cpu_log_filename(log_file);
3419 set_cpu_log(log_mask);
3422 if (!trace_backend_init(trace_events, trace_file)) {
3426 /* If no data_dir is specified then try to find it relative to the
3429 data_dir = os_find_datadir(argv[0]);
3431 /* If all else fails use the install path specified when building. */
3433 data_dir = CONFIG_QEMU_DATADIR;
3436 if (machine == NULL) {
3437 fprintf(stderr, "No machine found.\n");
3442 * Default to max_cpus = smp_cpus, in case the user doesn't
3443 * specify a max_cpus value.
3446 max_cpus = smp_cpus;
3448 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3449 if (smp_cpus > machine->max_cpus) {
3450 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3451 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3457 * Get the default machine options from the machine if it is not already
3458 * specified either by the configuration file or by the command line.
3460 if (machine->default_machine_opts) {
3461 qemu_opts_set_defaults(qemu_find_opts("machine"),
3462 machine->default_machine_opts, 0);
3465 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3466 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3468 if (machine->no_serial) {
3471 if (machine->no_parallel) {
3472 default_parallel = 0;
3474 if (!machine->use_virtcon) {
3475 default_virtcon = 0;
3477 if (machine->no_floppy) {
3480 if (machine->no_cdrom) {
3483 if (machine->no_sdcard) {
3487 if (display_type == DT_NOGRAPHIC) {
3488 if (default_parallel)
3489 add_device_config(DEV_PARALLEL, "null");
3490 if (default_serial && default_monitor) {
3491 add_device_config(DEV_SERIAL, "mon:stdio");
3492 } else if (default_virtcon && default_monitor) {
3493 add_device_config(DEV_VIRTCON, "mon:stdio");
3496 add_device_config(DEV_SERIAL, "stdio");
3497 if (default_virtcon)
3498 add_device_config(DEV_VIRTCON, "stdio");
3499 if (default_monitor)
3500 monitor_parse("stdio", "readline");
3504 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3505 if (default_parallel)
3506 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3507 if (default_monitor)
3508 monitor_parse("vc:80Cx24C", "readline");
3509 if (default_virtcon)
3510 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3515 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3517 #ifdef CONFIG_VIRTFS
3518 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3525 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3530 /* init the memory */
3531 if (ram_size == 0) {
3532 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3535 hax_pre_init(ram_size);
3537 configure_accelerator();
3539 qemu_init_cpu_loop();
3540 if (qemu_init_main_loop()) {
3541 fprintf(stderr, "qemu_init_main_loop failed\n");
3545 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3547 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3548 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3549 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3551 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3554 if (!kernel_cmdline) {
3555 kernel_cmdline = "";
3558 linux_boot = (kernel_filename != NULL);
3560 if (!linux_boot && *kernel_cmdline != '\0') {
3561 fprintf(stderr, "-append only allowed with -kernel option\n");
3565 if (!linux_boot && initrd_filename != NULL) {
3566 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3570 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3571 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3575 os_set_line_buffering();
3577 if (init_timer_alarm() < 0) {
3578 fprintf(stderr, "could not initialize alarm timer\n");
3583 /* spice needs the timers to be initialized by this point */
3587 if (icount_option && (kvm_enabled() || xen_enabled() || hax_enabled())) {
3588 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3591 configure_icount(icount_option);
3593 if (net_init_clients() < 0) {
3597 /* init the bluetooth world */
3598 if (foreach_device_config(DEV_BT, bt_parse))
3601 if (!xen_enabled()) {
3602 /* On 32-bit hosts, QEMU is limited by virtual address space */
3603 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3604 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3609 cpu_exec_init_all();
3611 bdrv_init_with_whitelist();
3615 /* open the virtual block devices */
3617 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3618 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3621 default_drive(default_cdrom, snapshot, machine->use_scsi,
3622 IF_DEFAULT, 2, CDROM_OPTS);
3623 default_drive(default_floppy, snapshot, machine->use_scsi,
3624 IF_FLOPPY, 0, FD_OPTS);
3625 default_drive(default_sdcard, snapshot, machine->use_scsi,
3628 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3631 if (nb_numa_nodes > 0) {
3634 if (nb_numa_nodes > MAX_NODES) {
3635 nb_numa_nodes = MAX_NODES;
3638 /* If no memory size if given for any node, assume the default case
3639 * and distribute the available memory equally across all nodes
3641 for (i = 0; i < nb_numa_nodes; i++) {
3642 if (node_mem[i] != 0)
3645 if (i == nb_numa_nodes) {
3646 uint64_t usedmem = 0;
3648 /* On Linux, the each node's border has to be 8MB aligned,
3649 * the final node gets the rest.
3651 for (i = 0; i < nb_numa_nodes - 1; i++) {
3652 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3653 usedmem += node_mem[i];
3655 node_mem[i] = ram_size - usedmem;
3658 for (i = 0; i < nb_numa_nodes; i++) {
3659 if (node_cpumask[i] != 0)
3662 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3663 * must cope with this anyway, because there are BIOSes out there in
3664 * real machines which also use this scheme.
3666 if (i == nb_numa_nodes) {
3667 for (i = 0; i < max_cpus; i++) {
3668 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3673 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3677 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3679 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3681 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3683 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3686 /* If no default VGA is requested, the default is "none". */
3687 if (default_vga && cirrus_vga_available()) {
3688 vga_model = "cirrus";
3690 select_vgahw(vga_model);
3692 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3696 i = select_watchdog(watchdog);
3698 exit (i == 1 ? 1 : 0);
3701 if (machine->compat_props) {
3702 qdev_prop_register_global_list(machine->compat_props);
3706 qdev_machine_init();
3708 machine->init(ram_size, boot_devices,
3709 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3711 g_free((gchar *)tmp_cmdline);
3714 cpu_synchronize_all_post_init();
3718 current_machine = machine;
3723 /* init USB devices */
3725 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3729 /* init generic devices */
3730 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3733 net_check_clients();
3735 /* just use the first displaystate for the moment */
3736 ds = get_displaystate();
3740 if (display_type == DT_DEFAULT && !display_remote) {
3741 #if defined(CONFIG_MARU)
3743 display_type = DT_MARU;
3744 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3745 display_type = DT_SDL;
3746 #elif defined(CONFIG_VNC)
3747 vnc_display = "localhost:0,to=99";
3750 display_type = DT_NONE;
3755 /* init local displays */
3756 switch (display_type) {
3759 #if defined(CONFIG_CURSES)
3761 curses_display_init(ds, full_screen);
3764 #if defined(CONFIG_SDL)
3766 sdl_display_init(ds, full_screen, no_frame);
3768 #elif defined(CONFIG_COCOA)
3770 cocoa_display_init(ds, full_screen);
3773 #if defined(CONFIG_MARU)
3775 maru_display_init(ds);
3776 if (skin_disabled == 1) {
3777 set_emul_skin_enable(0);
3779 set_emul_skin_enable(1);
3787 /* must be after terminal init, SDL library changes signal handlers */
3788 os_setup_signal_handling();
3791 /* init remote displays */
3793 vnc_display_init(ds);
3794 if (vnc_display_open(ds, vnc_display) < 0)
3797 if (show_vnc_port) {
3798 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3803 if (using_spice && !qxl_enabled) {
3804 qemu_spice_display_init(ds);
3810 dcl = ds->listeners;
3811 while (dcl != NULL) {
3812 if (dcl->dpy_refresh != NULL) {
3813 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3814 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3819 text_consoles_set_display(ds);
3821 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3825 qdev_machine_creation_done();
3827 if (rom_load_all() != 0) {
3828 fprintf(stderr, "rom loading failed\n");
3832 /* TODO: once all bus devices are qdevified, this should be done
3833 * when bus is created by qdev.c */
3834 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3835 qemu_run_machine_init_done_notifiers();
3837 qemu_system_reset(VMRESET_SILENT);
3839 if (load_vmstate(loadvm) < 0) {
3846 int ret = qemu_start_incoming_migration(incoming, &errp);
3848 if (error_is_set(&errp)) {
3849 fprintf(stderr, "Migrate: %s\n", error_get_pretty(errp));
3852 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3856 } else if (autostart) {