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
33 /* Needed early for CONFIG_BSD etc. */
34 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
54 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
68 #include "qemu-seccomp.h"
73 #include <sys/ethernet.h>
74 #include <sys/sockio.h>
75 #include <netinet/arp.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_icmp.h> // must come after ip.h
79 #include <netinet/udp.h>
80 #include <netinet/tcp.h>
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"
149 #include "blockdev.h"
150 #include "hw/block-common.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"
181 #include "ui/qemu-spice.h"
184 #include "tizen/src/maru_common.h"
185 #include "tizen/src/emulator.h"
186 #include "tizen/src/maru_err_table.h"
187 #include "tizen/src/emul_state.h"
188 #include "tizen/src/maru_display.h"
189 #include "tizen/src/skin/maruskin_operation.h"
193 //#define DEBUG_SLIRP
195 #define DEFAULT_RAM_SIZE 128
197 #define MAX_VIRTIO_CONSOLES 1
200 int skin_disabled = 0;
202 extern int enable_gl;
203 extern int enable_yagl;
206 static const char *data_dir;
207 const char *bios_name = NULL;
208 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
209 DisplayType display_type = DT_DEFAULT;
210 int display_remote = 0;
211 const char* keyboard_layout = NULL;
213 const char *mem_path = NULL;
215 int mem_prealloc = 0; /* force preallocation of physical target memory */
218 NICInfo nd_table[MAX_NICS];
220 static int rtc_utc = 1;
221 static int rtc_date_offset = -1; /* -1 means no change */
222 QEMUClock *rtc_clock;
223 int vga_interface_type = VGA_NONE;
224 static int full_screen = 0;
226 static int no_frame = 0;
229 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
230 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
231 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
232 int win2k_install_hack = 0;
240 const char *vnc_display;
242 int acpi_enabled = 1;
248 int graphic_rotate = 0;
249 const char *watchdog;
250 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
252 int semihosting_enabled = 0;
254 const char *qemu_name;
257 unsigned int nb_prom_envs = 0;
258 const char *prom_envs[MAX_PROM_ENVS];
260 uint8_t *boot_splash_filedata;
261 int boot_splash_filedata_size;
262 uint8_t qemu_extra_params_fw[2];
265 typedef struct FWBootEntry FWBootEntry;
268 QTAILQ_ENTRY(FWBootEntry) link;
274 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
277 uint64_t node_mem[MAX_NODES];
278 unsigned long *node_cpumask[MAX_NODES];
280 uint8_t qemu_uuid[16];
282 static QEMUBootSetHandler *boot_set_handler;
283 static void *boot_set_opaque;
285 static NotifierList exit_notifiers =
286 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
288 static NotifierList machine_init_done_notifiers =
289 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
291 static int tcg_allowed = 1;
296 enum xen_mode xen_mode = XEN_EMULATE;
297 static int tcg_tb_size;
299 #ifdef CONFIG_OPENGLES
300 int gles2_quality = 100;
302 static int default_serial = 1;
303 static int default_parallel = 1;
304 static int default_virtcon = 1;
305 static int default_monitor = 1;
306 static int default_floppy = 1;
307 static int default_cdrom = 1;
308 static int default_sdcard = 1;
309 static int default_vga = 1;
315 { .driver = "isa-serial", .flag = &default_serial },
316 { .driver = "isa-parallel", .flag = &default_parallel },
317 { .driver = "isa-fdc", .flag = &default_floppy },
318 { .driver = "ide-cd", .flag = &default_cdrom },
319 { .driver = "ide-hd", .flag = &default_cdrom },
320 { .driver = "ide-drive", .flag = &default_cdrom },
321 { .driver = "scsi-cd", .flag = &default_cdrom },
322 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
323 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
324 { .driver = "virtio-serial", .flag = &default_virtcon },
325 { .driver = "VGA", .flag = &default_vga },
326 { .driver = "isa-vga", .flag = &default_vga },
327 { .driver = "cirrus-vga", .flag = &default_vga },
328 { .driver = "isa-cirrus-vga", .flag = &default_vga },
329 { .driver = "vmware-svga", .flag = &default_vga },
330 { .driver = "qxl-vga", .flag = &default_vga },
333 const char *qemu_get_vm_name(void)
338 static void res_free(void)
340 if (boot_splash_filedata != NULL) {
341 g_free(boot_splash_filedata);
342 boot_splash_filedata = NULL;
346 static int default_driver_check(QemuOpts *opts, void *opaque)
348 const char *driver = qemu_opt_get(opts, "driver");
353 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
354 if (strcmp(default_list[i].driver, driver) != 0)
356 *(default_list[i].flag) = 0;
361 /***********************************************************/
364 static RunState current_run_state = RUN_STATE_PRELAUNCH;
369 } RunStateTransition;
371 static const RunStateTransition runstate_transitions_def[] = {
373 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
375 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
376 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
378 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
379 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
381 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
382 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
384 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
385 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
387 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
388 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
390 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
391 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
392 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
394 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
395 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
397 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
399 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
400 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
401 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
402 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
403 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
404 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
405 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
406 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
407 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
409 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
411 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
412 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
414 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
415 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
416 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
417 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
419 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
420 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
422 { RUN_STATE_MAX, RUN_STATE_MAX },
425 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
427 bool runstate_check(RunState state)
429 return current_run_state == state;
432 void runstate_init(void)
434 const RunStateTransition *p;
436 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
438 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
439 runstate_valid_transitions[p->from][p->to] = true;
443 /* This function will abort() on invalid state transitions */
444 void runstate_set(RunState new_state)
446 assert(new_state < RUN_STATE_MAX);
448 if (!runstate_valid_transitions[current_run_state][new_state]) {
449 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
450 RunState_lookup[current_run_state],
451 RunState_lookup[new_state]);
455 current_run_state = new_state;
458 int runstate_is_running(void)
460 return runstate_check(RUN_STATE_RUNNING);
463 StatusInfo *qmp_query_status(Error **errp)
465 StatusInfo *info = g_malloc0(sizeof(*info));
467 info->running = runstate_is_running();
468 info->singlestep = singlestep;
469 info->status = current_run_state;
474 /***********************************************************/
475 /* real time host monotonic timer */
477 /***********************************************************/
478 /* host time/date access */
479 void qemu_get_timedate(struct tm *tm, int offset)
486 if (rtc_date_offset == -1) {
490 ret = localtime(&ti);
492 ti -= rtc_date_offset;
496 memcpy(tm, ret, sizeof(struct tm));
499 int qemu_timedate_diff(struct tm *tm)
503 if (rtc_date_offset == -1)
505 seconds = mktimegm(tm);
508 tmp.tm_isdst = -1; /* use timezone to figure it out */
509 seconds = mktime(&tmp);
512 seconds = mktimegm(tm) + rtc_date_offset;
514 return seconds - time(NULL);
517 void rtc_change_mon_event(struct tm *tm)
521 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
522 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
523 qobject_decref(data);
526 static void configure_rtc_date_offset(const char *startdate, int legacy)
528 time_t rtc_start_date;
531 if (!strcmp(startdate, "now") && legacy) {
532 rtc_date_offset = -1;
534 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
542 } else if (sscanf(startdate, "%d-%d-%d",
554 rtc_start_date = mktimegm(&tm);
555 if (rtc_start_date == -1) {
557 fprintf(stderr, "Invalid date format. Valid formats are:\n"
558 "'2006-06-17T16:01:21' or '2006-06-17'\n");
561 rtc_date_offset = time(NULL) - rtc_start_date;
565 static void configure_rtc(QemuOpts *opts)
569 value = qemu_opt_get(opts, "base");
571 if (!strcmp(value, "utc")) {
573 } else if (!strcmp(value, "localtime")) {
576 configure_rtc_date_offset(value, 0);
579 value = qemu_opt_get(opts, "clock");
581 if (!strcmp(value, "host")) {
582 rtc_clock = host_clock;
583 } else if (!strcmp(value, "rt")) {
584 rtc_clock = rt_clock;
585 } else if (!strcmp(value, "vm")) {
586 rtc_clock = vm_clock;
588 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
592 value = qemu_opt_get(opts, "driftfix");
594 if (!strcmp(value, "slew")) {
595 static GlobalProperty slew_lost_ticks[] = {
597 .driver = "mc146818rtc",
598 .property = "lost_tick_policy",
601 { /* end of list */ }
604 qdev_prop_register_global_list(slew_lost_ticks);
605 } else if (!strcmp(value, "none")) {
606 /* discard is default */
608 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
614 /***********************************************************/
615 /* Bluetooth support */
618 static struct HCIInfo *hci_table[MAX_NICS];
620 static struct bt_vlan_s {
621 struct bt_scatternet_s net;
623 struct bt_vlan_s *next;
626 /* find or alloc a new bluetooth "VLAN" */
627 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
629 struct bt_vlan_s **pvlan, *vlan;
630 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
634 vlan = g_malloc0(sizeof(struct bt_vlan_s));
636 pvlan = &first_bt_vlan;
637 while (*pvlan != NULL)
638 pvlan = &(*pvlan)->next;
643 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
647 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
652 static struct HCIInfo null_hci = {
653 .cmd_send = null_hci_send,
654 .sco_send = null_hci_send,
655 .acl_send = null_hci_send,
656 .bdaddr_set = null_hci_addr_set,
659 struct HCIInfo *qemu_next_hci(void)
661 if (cur_hci == nb_hcis)
664 return hci_table[cur_hci++];
667 static struct HCIInfo *hci_init(const char *str)
670 struct bt_scatternet_s *vlan = 0;
672 if (!strcmp(str, "null"))
675 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
677 return bt_host_hci(str[4] ? str + 5 : "hci0");
678 else if (!strncmp(str, "hci", 3)) {
681 if (!strncmp(str + 3, ",vlan=", 6)) {
682 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
687 vlan = qemu_find_bt_vlan(0);
689 return bt_new_hci(vlan);
692 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
697 static int bt_hci_parse(const char *str)
702 if (nb_hcis >= MAX_NICS) {
703 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
716 bdaddr.b[5] = 0x56 + nb_hcis;
717 hci->bdaddr_set(hci, bdaddr.b);
719 hci_table[nb_hcis++] = hci;
724 static void bt_vhci_add(int vlan_id)
726 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
729 fprintf(stderr, "qemu: warning: adding a VHCI to "
730 "an empty scatternet %i\n", vlan_id);
732 bt_vhci_init(bt_new_hci(vlan));
735 static struct bt_device_s *bt_device_add(const char *opt)
737 struct bt_scatternet_s *vlan;
739 char *endp = strstr(opt, ",vlan=");
740 int len = (endp ? endp - opt : strlen(opt)) + 1;
743 pstrcpy(devname, MIN(sizeof(devname), len), opt);
746 vlan_id = strtol(endp + 6, &endp, 0);
748 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
753 vlan = qemu_find_bt_vlan(vlan_id);
756 fprintf(stderr, "qemu: warning: adding a slave device to "
757 "an empty scatternet %i\n", vlan_id);
759 if (!strcmp(devname, "keyboard"))
760 return bt_keyboard_init(vlan);
762 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
766 static int bt_parse(const char *opt)
768 const char *endp, *p;
771 if (strstart(opt, "hci", &endp)) {
772 if (!*endp || *endp == ',') {
774 if (!strstart(endp, ",vlan=", 0))
777 return bt_hci_parse(opt);
779 } else if (strstart(opt, "vhci", &endp)) {
780 if (!*endp || *endp == ',') {
782 if (strstart(endp, ",vlan=", &p)) {
783 vlan = strtol(p, (char **) &endp, 0);
785 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
789 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
798 } else if (strstart(opt, "device:", &endp))
799 return !bt_device_add(endp);
801 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
805 static int parse_sandbox(QemuOpts *opts, void *opaque)
807 /* FIXME: change this to true for 1.3 */
808 if (qemu_opt_get_bool(opts, "enable", false)) {
809 #ifdef CONFIG_SECCOMP
810 if (seccomp_start() < 0) {
811 qerror_report(ERROR_CLASS_GENERIC_ERROR,
812 "failed to install seccomp syscall filter in the kernel");
816 qerror_report(ERROR_CLASS_GENERIC_ERROR,
817 "sandboxing request but seccomp is not compiled into this build");
825 /***********************************************************/
826 /* QEMU Block devices */
828 #define HD_OPTS "media=disk"
829 #define CDROM_OPTS "media=cdrom"
831 #define PFLASH_OPTS ""
835 static int drive_init_func(QemuOpts *opts, void *opaque)
837 int *use_scsi = opaque;
839 return drive_init(opts, *use_scsi) == NULL;
842 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
844 if (NULL == qemu_opt_get(opts, "snapshot")) {
845 qemu_opt_set(opts, "snapshot", "on");
850 static void default_drive(int enable, int snapshot, int use_scsi,
851 BlockInterfaceType type, int index,
856 if (type == IF_DEFAULT) {
857 type = use_scsi ? IF_SCSI : IF_IDE;
860 if (!enable || drive_get_by_index(type, index)) {
864 opts = drive_add(type, index, NULL, optstr);
866 drive_enable_snapshot(opts, NULL);
868 if (!drive_init(opts, use_scsi)) {
873 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
875 boot_set_handler = func;
876 boot_set_opaque = opaque;
879 int qemu_boot_set(const char *boot_devices)
881 if (!boot_set_handler) {
884 return boot_set_handler(boot_set_opaque, boot_devices);
887 static void validate_bootdevices(char *devices)
889 /* We just do some generic consistency checks */
893 for (p = devices; *p != '\0'; p++) {
894 /* Allowed boot devices are:
895 * a-b: floppy disk drives
896 * c-f: IDE disk drives
897 * g-m: machine implementation dependent drives
898 * n-p: network devices
899 * It's up to each machine implementation to check if the given boot
900 * devices match the actual hardware implementation and firmware
903 if (*p < 'a' || *p > 'p') {
904 fprintf(stderr, "Invalid boot device '%c'\n", *p);
907 if (bitmap & (1 << (*p - 'a'))) {
908 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
911 bitmap |= 1 << (*p - 'a');
915 static void restore_boot_devices(void *opaque)
917 char *standard_boot_devices = opaque;
918 static int first = 1;
920 /* Restore boot order and remove ourselves after the first boot */
926 qemu_boot_set(standard_boot_devices);
928 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
929 g_free(standard_boot_devices);
932 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
935 FWBootEntry *node, *i;
941 assert(dev != NULL || suffix != NULL);
943 node = g_malloc0(sizeof(FWBootEntry));
944 node->bootindex = bootindex;
945 node->suffix = suffix ? g_strdup(suffix) : NULL;
948 QTAILQ_FOREACH(i, &fw_boot_order, link) {
949 if (i->bootindex == bootindex) {
950 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
952 } else if (i->bootindex < bootindex) {
955 QTAILQ_INSERT_BEFORE(i, node, link);
958 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
962 * This function returns null terminated string that consist of new line
963 * separated device paths.
965 * memory pointed by "size" is assigned total length of the array in bytes
968 char *get_boot_devices_list(uint32_t *size)
974 QTAILQ_FOREACH(i, &fw_boot_order, link) {
975 char *devpath = NULL, *bootpath;
979 devpath = qdev_get_fw_dev_path(i->dev);
983 if (i->suffix && devpath) {
984 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
986 bootpath = g_malloc(bootpathlen);
987 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
989 } else if (devpath) {
993 bootpath = g_strdup(i->suffix);
997 list[total-1] = '\n';
999 len = strlen(bootpath) + 1;
1000 list = g_realloc(list, total + len);
1001 memcpy(&list[total], bootpath, len);
1011 static void numa_add(const char *optarg)
1015 unsigned long long value, endvalue;
1018 value = endvalue = 0ULL;
1020 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1021 if (!strcmp(option, "node")) {
1022 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1023 nodenr = nb_numa_nodes;
1025 nodenr = strtoull(option, NULL, 10);
1028 if (get_param_value(option, 128, "mem", optarg) == 0) {
1029 node_mem[nodenr] = 0;
1032 sval = strtosz(option, &endptr);
1033 if (sval < 0 || *endptr) {
1034 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1037 node_mem[nodenr] = sval;
1039 if (get_param_value(option, 128, "cpus", optarg) != 0) {
1040 value = strtoull(option, &endptr, 10);
1041 if (*endptr == '-') {
1042 endvalue = strtoull(endptr+1, &endptr, 10);
1047 if (!(endvalue < MAX_CPUMASK_BITS)) {
1048 endvalue = MAX_CPUMASK_BITS - 1;
1050 "A max of %d CPUs are supported in a guest\n",
1054 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
1061 static void smp_parse(const char *optarg)
1063 int smp, sockets = 0, threads = 0, cores = 0;
1067 smp = strtoul(optarg, &endptr, 10);
1068 if (endptr != optarg) {
1069 if (*endptr == ',') {
1073 if (get_param_value(option, 128, "sockets", endptr) != 0)
1074 sockets = strtoull(option, NULL, 10);
1075 if (get_param_value(option, 128, "cores", endptr) != 0)
1076 cores = strtoull(option, NULL, 10);
1077 if (get_param_value(option, 128, "threads", endptr) != 0)
1078 threads = strtoull(option, NULL, 10);
1079 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1080 max_cpus = strtoull(option, NULL, 10);
1082 /* compute missing values, prefer sockets over cores over threads */
1083 if (smp == 0 || sockets == 0) {
1084 sockets = sockets > 0 ? sockets : 1;
1085 cores = cores > 0 ? cores : 1;
1086 threads = threads > 0 ? threads : 1;
1088 smp = cores * threads * sockets;
1092 threads = threads > 0 ? threads : 1;
1093 cores = smp / (sockets * threads);
1095 threads = smp / (cores * sockets);
1099 smp_cores = cores > 0 ? cores : 1;
1100 smp_threads = threads > 0 ? threads : 1;
1102 max_cpus = smp_cpus;
1105 /***********************************************************/
1108 static int usb_device_add(const char *devname)
1111 USBDevice *dev = NULL;
1116 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1117 dev = usbdevice_create(devname);
1121 /* the other ones */
1122 #ifndef CONFIG_LINUX
1123 /* only the linux version is qdev-ified, usb-bsd still needs this */
1124 if (strstart(devname, "host:", &p)) {
1125 dev = usb_host_device_open(usb_bus_find(-1), p);
1128 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1129 dev = usb_bt_init(usb_bus_find(-1),
1130 devname[2] ? hci_init(p)
1131 : bt_new_hci(qemu_find_bt_vlan(0)));
1142 static int usb_device_del(const char *devname)
1147 if (strstart(devname, "host:", &p))
1148 return usb_host_device_close(p);
1153 p = strchr(devname, '.');
1156 bus_num = strtoul(devname, NULL, 0);
1157 addr = strtoul(p + 1, NULL, 0);
1159 return usb_device_delete_addr(bus_num, addr);
1162 static int usb_parse(const char *cmdline)
1165 r = usb_device_add(cmdline);
1167 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1172 void do_usb_add(Monitor *mon, const QDict *qdict)
1174 const char *devname = qdict_get_str(qdict, "devname");
1175 if (usb_device_add(devname) < 0) {
1176 error_report("could not add USB device '%s'", devname);
1180 void do_usb_del(Monitor *mon, const QDict *qdict)
1182 const char *devname = qdict_get_str(qdict, "devname");
1183 if (usb_device_del(devname) < 0) {
1184 error_report("could not delete USB device '%s'", devname);
1188 /***********************************************************/
1189 /* PCMCIA/Cardbus */
1191 static struct pcmcia_socket_entry_s {
1192 PCMCIASocket *socket;
1193 struct pcmcia_socket_entry_s *next;
1194 } *pcmcia_sockets = 0;
1196 void pcmcia_socket_register(PCMCIASocket *socket)
1198 struct pcmcia_socket_entry_s *entry;
1200 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1201 entry->socket = socket;
1202 entry->next = pcmcia_sockets;
1203 pcmcia_sockets = entry;
1206 void pcmcia_socket_unregister(PCMCIASocket *socket)
1208 struct pcmcia_socket_entry_s *entry, **ptr;
1210 ptr = &pcmcia_sockets;
1211 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1212 if (entry->socket == socket) {
1218 void pcmcia_info(Monitor *mon)
1220 struct pcmcia_socket_entry_s *iter;
1222 if (!pcmcia_sockets)
1223 monitor_printf(mon, "No PCMCIA sockets\n");
1225 for (iter = pcmcia_sockets; iter; iter = iter->next)
1226 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1227 iter->socket->attached ? iter->socket->card_string :
1231 /***********************************************************/
1232 /* machine registration */
1234 static QEMUMachine *first_machine = NULL;
1235 QEMUMachine *current_machine = NULL;
1237 int qemu_register_machine(QEMUMachine *m)
1240 pm = &first_machine;
1248 static QEMUMachine *find_machine(const char *name)
1252 for(m = first_machine; m != NULL; m = m->next) {
1253 if (!strcmp(m->name, name))
1255 if (m->alias && !strcmp(m->alias, name))
1261 QEMUMachine *find_default_machine(void)
1265 for(m = first_machine; m != NULL; m = m->next) {
1266 if (m->is_default) {
1273 MachineInfoList *qmp_query_machines(Error **errp)
1275 MachineInfoList *mach_list = NULL;
1278 for (m = first_machine; m; m = m->next) {
1279 MachineInfoList *entry;
1282 info = g_malloc0(sizeof(*info));
1283 if (m->is_default) {
1284 info->has_is_default = true;
1285 info->is_default = true;
1289 info->has_alias = true;
1290 info->alias = g_strdup(m->alias);
1293 info->name = g_strdup(m->name);
1295 entry = g_malloc0(sizeof(*entry));
1296 entry->value = info;
1297 entry->next = mach_list;
1304 /***********************************************************/
1305 /* main execution loop */
1307 static void gui_update(void *opaque)
1309 uint64_t interval = GUI_REFRESH_INTERVAL;
1310 DisplayState *ds = opaque;
1311 DisplayChangeListener *dcl = ds->listeners;
1315 while (dcl != NULL) {
1316 if (dcl->gui_timer_interval &&
1317 dcl->gui_timer_interval < interval)
1318 interval = dcl->gui_timer_interval;
1321 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1324 struct vm_change_state_entry {
1325 VMChangeStateHandler *cb;
1327 QLIST_ENTRY (vm_change_state_entry) entries;
1330 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1332 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1335 VMChangeStateEntry *e;
1337 e = g_malloc0(sizeof (*e));
1341 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1345 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1347 QLIST_REMOVE (e, entries);
1351 void vm_state_notify(int running, RunState state)
1353 VMChangeStateEntry *e;
1355 trace_vm_state_notify(running, state);
1357 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1358 e->cb(e->opaque, running, state);
1364 if (!runstate_is_running()) {
1366 runstate_set(RUN_STATE_RUNNING);
1367 vm_state_notify(1, RUN_STATE_RUNNING);
1369 monitor_protocol_event(QEVENT_RESUME, NULL);
1373 /* reset/shutdown handler */
1375 typedef struct QEMUResetEntry {
1376 QTAILQ_ENTRY(QEMUResetEntry) entry;
1377 QEMUResetHandler *func;
1381 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1382 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1383 static int reset_requested;
1384 static int shutdown_requested, shutdown_signal = -1;
1385 static pid_t shutdown_pid;
1386 static int powerdown_requested;
1387 static int debug_requested;
1388 static int suspend_requested;
1389 static int wakeup_requested;
1390 static NotifierList suspend_notifiers =
1391 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1392 static NotifierList wakeup_notifiers =
1393 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1394 static uint32_t wakeup_reason_mask = ~0;
1395 static RunState vmstop_requested = RUN_STATE_MAX;
1397 int qemu_shutdown_requested_get(void)
1399 return shutdown_requested;
1402 int qemu_reset_requested_get(void)
1404 return reset_requested;
1407 int qemu_shutdown_requested(void)
1409 int r = shutdown_requested;
1410 shutdown_requested = 0;
1414 void qemu_kill_report(void)
1416 if (!qtest_enabled() && shutdown_signal != -1) {
1417 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1418 if (shutdown_pid == 0) {
1419 /* This happens for eg ^C at the terminal, so it's worth
1420 * avoiding printing an odd message in that case.
1422 fputc('\n', stderr);
1424 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1426 shutdown_signal = -1;
1430 int qemu_reset_requested(void)
1432 int r = reset_requested;
1433 reset_requested = 0;
1437 static int qemu_suspend_requested(void)
1439 int r = suspend_requested;
1440 suspend_requested = 0;
1444 static int qemu_wakeup_requested(void)
1446 int r = wakeup_requested;
1447 wakeup_requested = 0;
1451 int qemu_powerdown_requested(void)
1453 int r = powerdown_requested;
1454 powerdown_requested = 0;
1458 static int qemu_debug_requested(void)
1460 int r = debug_requested;
1461 debug_requested = 0;
1465 /* We use RUN_STATE_MAX but any invalid value will do */
1466 static bool qemu_vmstop_requested(RunState *r)
1468 if (vmstop_requested < RUN_STATE_MAX) {
1469 *r = vmstop_requested;
1470 vmstop_requested = RUN_STATE_MAX;
1477 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1479 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1482 re->opaque = opaque;
1483 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1486 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1490 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1491 if (re->func == func && re->opaque == opaque) {
1492 QTAILQ_REMOVE(&reset_handlers, re, entry);
1499 void qemu_devices_reset(void)
1501 QEMUResetEntry *re, *nre;
1503 /* reset all devices */
1504 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1505 re->func(re->opaque);
1509 void qemu_system_reset(bool report)
1511 if (current_machine && current_machine->reset) {
1512 current_machine->reset();
1514 qemu_devices_reset();
1517 monitor_protocol_event(QEVENT_RESET, NULL);
1519 cpu_synchronize_all_post_reset();
1522 void qemu_system_reset_request(void)
1525 shutdown_requested = 1;
1527 reset_requested = 1;
1530 qemu_notify_event();
1533 static void qemu_system_suspend(void)
1536 notifier_list_notify(&suspend_notifiers, NULL);
1537 runstate_set(RUN_STATE_SUSPENDED);
1538 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1541 void qemu_system_suspend_request(void)
1543 if (runstate_check(RUN_STATE_SUSPENDED)) {
1546 suspend_requested = 1;
1548 qemu_notify_event();
1551 void qemu_register_suspend_notifier(Notifier *notifier)
1553 notifier_list_add(&suspend_notifiers, notifier);
1556 void qemu_system_wakeup_request(WakeupReason reason)
1558 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1561 if (!(wakeup_reason_mask & (1 << reason))) {
1564 runstate_set(RUN_STATE_RUNNING);
1565 notifier_list_notify(&wakeup_notifiers, &reason);
1566 wakeup_requested = 1;
1567 qemu_notify_event();
1570 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1573 wakeup_reason_mask |= (1 << reason);
1575 wakeup_reason_mask &= ~(1 << reason);
1579 void qemu_register_wakeup_notifier(Notifier *notifier)
1581 notifier_list_add(&wakeup_notifiers, notifier);
1584 void qemu_system_killed(int signal, pid_t pid)
1586 shutdown_signal = signal;
1591 shutdown_qemu_gracefully();
1593 qemu_system_shutdown_request();
1597 void qemu_system_shutdown_request(void)
1599 shutdown_requested = 1;
1600 qemu_notify_event();
1603 void qemu_system_powerdown_request(void)
1605 powerdown_requested = 1;
1606 qemu_notify_event();
1609 void qemu_system_debug_request(void)
1611 debug_requested = 1;
1612 qemu_notify_event();
1615 void qemu_system_vmstop_request(RunState state)
1617 vmstop_requested = state;
1618 qemu_notify_event();
1621 qemu_irq qemu_system_powerdown;
1623 static bool main_loop_should_exit(void)
1626 if (qemu_debug_requested()) {
1627 vm_stop(RUN_STATE_DEBUG);
1629 if (qemu_suspend_requested()) {
1630 qemu_system_suspend();
1632 if (qemu_shutdown_requested()) {
1634 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1636 vm_stop(RUN_STATE_SHUTDOWN);
1641 if (qemu_reset_requested()) {
1643 cpu_synchronize_all_states();
1644 qemu_system_reset(VMRESET_REPORT);
1646 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1647 runstate_check(RUN_STATE_SHUTDOWN)) {
1648 runstate_set(RUN_STATE_PAUSED);
1651 if (qemu_wakeup_requested()) {
1653 cpu_synchronize_all_states();
1654 qemu_system_reset(VMRESET_SILENT);
1656 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1658 if (qemu_powerdown_requested()) {
1659 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1660 qemu_irq_raise(qemu_system_powerdown);
1662 if (qemu_vmstop_requested(&r)) {
1668 static void main_loop(void)
1672 #ifdef CONFIG_PROFILER
1679 nonblocking = !(kvm_enabled()|| hax_enabled()) && last_io > 0;
1680 #ifdef CONFIG_PROFILER
1681 ti = profile_getclock();
1683 last_io = main_loop_wait(nonblocking);
1684 #ifdef CONFIG_PROFILER
1685 dev_time += profile_getclock() - ti;
1687 } while (!main_loop_should_exit());
1690 static void version(void)
1692 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1695 static void help(int exitcode)
1698 printf("usage: %s [options] [disk_image]\n\n"
1699 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1700 error_get_progname());
1702 #define QEMU_OPTIONS_GENERATE_HELP
1703 #include "qemu-options-wrapper.h"
1705 printf("\nDuring emulation, the following keys are useful:\n"
1706 "ctrl-alt-f toggle full screen\n"
1707 "ctrl-alt-n switch to virtual console 'n'\n"
1708 "ctrl-alt toggle mouse and keyboard grab\n"
1710 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1715 #define HAS_ARG 0x0001
1717 typedef struct QEMUOption {
1724 static const QEMUOption qemu_options[] = {
1725 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1726 #define QEMU_OPTIONS_GENERATE_OPTIONS
1727 #include "qemu-options-wrapper.h"
1731 static bool vga_available(void)
1733 return qdev_exists("VGA") || qdev_exists("isa-vga");
1736 static bool cirrus_vga_available(void)
1738 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1741 static bool vmware_vga_available(void)
1743 return qdev_exists("vmware-svga");
1746 static void select_vgahw (const char *p)
1750 vga_interface_type = VGA_NONE;
1751 if (strstart(p, "std", &opts)) {
1752 if (vga_available()) {
1753 vga_interface_type = VGA_STD;
1755 fprintf(stderr, "Error: standard VGA not available\n");
1758 } else if (strstart(p, "cirrus", &opts)) {
1759 if (cirrus_vga_available()) {
1760 vga_interface_type = VGA_CIRRUS;
1762 fprintf(stderr, "Error: Cirrus VGA not available\n");
1765 } else if (strstart(p, "vmware", &opts)) {
1766 if (vmware_vga_available()) {
1767 vga_interface_type = VGA_VMWARE;
1769 fprintf(stderr, "Error: VMWare SVGA not available\n");
1772 } else if (strstart(p, "xenfb", &opts)) {
1773 vga_interface_type = VGA_XENFB;
1774 } else if (strstart(p, "qxl", &opts)) {
1775 vga_interface_type = VGA_QXL;
1777 } else if (strstart(p, "maru", &opts)) {
1778 vga_interface_type = VGA_MARU;
1780 } else if (!strstart(p, "none", &opts)) {
1782 fprintf(stderr, "Unknown vga type: %s\n", p);
1786 const char *nextopt;
1788 if (strstart(opts, ",retrace=", &nextopt)) {
1790 if (strstart(opts, "dumb", &nextopt))
1791 vga_retrace_method = VGA_RETRACE_DUMB;
1792 else if (strstart(opts, "precise", &nextopt))
1793 vga_retrace_method = VGA_RETRACE_PRECISE;
1794 else goto invalid_vga;
1795 } else goto invalid_vga;
1800 static DisplayType select_display(const char *p)
1803 DisplayType display = DT_DEFAULT;
1805 if (strstart(p, "sdl", &opts)) {
1809 const char *nextopt;
1811 if (strstart(opts, ",frame=", &nextopt)) {
1813 if (strstart(opts, "on", &nextopt)) {
1815 } else if (strstart(opts, "off", &nextopt)) {
1818 goto invalid_sdl_args;
1820 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1822 if (strstart(opts, "on", &nextopt)) {
1824 } else if (strstart(opts, "off", &nextopt)) {
1827 goto invalid_sdl_args;
1829 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1831 if (strstart(opts, "on", &nextopt)) {
1833 } else if (strstart(opts, "off", &nextopt)) {
1836 goto invalid_sdl_args;
1838 } else if (strstart(opts, ",window_close=", &nextopt)) {
1840 if (strstart(opts, "on", &nextopt)) {
1842 } else if (strstart(opts, "off", &nextopt)) {
1845 goto invalid_sdl_args;
1849 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1855 fprintf(stderr, "SDL support is disabled\n");
1858 } else if (strstart(p, "vnc", &opts)) {
1863 const char *nextopt;
1865 if (strstart(opts, "=", &nextopt)) {
1866 vnc_display = nextopt;
1870 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1874 fprintf(stderr, "VNC support is disabled\n");
1877 } else if (strstart(p, "curses", &opts)) {
1878 #ifdef CONFIG_CURSES
1879 display = DT_CURSES;
1881 fprintf(stderr, "Curses support is disabled\n");
1884 } else if (strstart(p, "none", &opts)) {
1887 fprintf(stderr, "Unknown display type: %s\n", p);
1894 static int balloon_parse(const char *arg)
1898 if (strcmp(arg, "none") == 0) {
1902 if (!strncmp(arg, "virtio", 6)) {
1903 if (arg[6] == ',') {
1904 /* have params -> parse them */
1905 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1909 /* create empty opts */
1910 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
1912 qemu_opt_set(opts, "driver", "virtio-balloon");
1919 char *qemu_find_file(int type, const char *name)
1925 /* Try the name as a straight path first */
1926 if (access(name, R_OK) == 0) {
1927 return g_strdup(name);
1930 case QEMU_FILE_TYPE_BIOS:
1933 case QEMU_FILE_TYPE_KEYMAP:
1934 subdir = "keymaps/";
1939 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1940 buf = g_malloc0(len);
1941 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1942 if (access(buf, R_OK)) {
1950 const char *qemu_get_data_dir(void);
1952 const char *qemu_get_data_dir(void)
1958 static int device_help_func(QemuOpts *opts, void *opaque)
1960 return qdev_device_help(opts);
1963 static int device_init_func(QemuOpts *opts, void *opaque)
1968 if(maru_device_check(opts) == -1) {
1974 dev = qdev_device_add(opts);
1980 static int chardev_init_func(QemuOpts *opts, void *opaque)
1982 CharDriverState *chr;
1984 chr = qemu_chr_new_from_opts(opts, NULL);
1990 #ifdef CONFIG_VIRTFS
1991 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1994 ret = qemu_fsdev_add(opts);
2000 static int mon_init_func(QemuOpts *opts, void *opaque)
2002 CharDriverState *chr;
2003 const char *chardev;
2007 mode = qemu_opt_get(opts, "mode");
2011 if (strcmp(mode, "readline") == 0) {
2012 flags = MONITOR_USE_READLINE;
2013 } else if (strcmp(mode, "control") == 0) {
2014 flags = MONITOR_USE_CONTROL;
2016 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2020 if (qemu_opt_get_bool(opts, "pretty", 0))
2021 flags |= MONITOR_USE_PRETTY;
2023 if (qemu_opt_get_bool(opts, "default", 0))
2024 flags |= MONITOR_IS_DEFAULT;
2026 chardev = qemu_opt_get(opts, "chardev");
2027 chr = qemu_chr_find(chardev);
2029 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2033 monitor_init(chr, flags);
2037 static void monitor_parse(const char *optarg, const char *mode)
2039 static int monitor_device_index = 0;
2045 if (strstart(optarg, "chardev:", &p)) {
2046 snprintf(label, sizeof(label), "%s", p);
2048 snprintf(label, sizeof(label), "compat_monitor%d",
2049 monitor_device_index);
2050 if (monitor_device_index == 0) {
2053 opts = qemu_chr_parse_compat(label, optarg);
2055 fprintf(stderr, "parse error: %s\n", optarg);
2060 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2062 fprintf(stderr, "duplicate chardev: %s\n", label);
2065 qemu_opt_set(opts, "mode", mode);
2066 qemu_opt_set(opts, "chardev", label);
2068 qemu_opt_set(opts, "default", "on");
2069 monitor_device_index++;
2072 struct device_config {
2074 DEV_USB, /* -usbdevice */
2076 DEV_SERIAL, /* -serial */
2077 DEV_PARALLEL, /* -parallel */
2078 DEV_VIRTCON, /* -virtioconsole */
2079 DEV_DEBUGCON, /* -debugcon */
2080 DEV_GDB, /* -gdb, -s */
2082 const char *cmdline;
2084 QTAILQ_ENTRY(device_config) next;
2086 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
2088 static void add_device_config(int type, const char *cmdline)
2090 struct device_config *conf;
2092 conf = g_malloc0(sizeof(*conf));
2094 conf->cmdline = cmdline;
2095 loc_save(&conf->loc);
2096 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2099 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2101 struct device_config *conf;
2104 QTAILQ_FOREACH(conf, &device_configs, next) {
2105 if (conf->type != type)
2107 loc_push_restore(&conf->loc);
2108 rc = func(conf->cmdline);
2109 loc_pop(&conf->loc);
2116 static int serial_parse(const char *devname)
2118 static int index = 0;
2121 if (strcmp(devname, "none") == 0)
2123 if (index == MAX_SERIAL_PORTS) {
2124 fprintf(stderr, "qemu: too many serial ports\n");
2127 snprintf(label, sizeof(label), "serial%d", index);
2128 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2129 if (!serial_hds[index]) {
2130 fprintf(stderr, "qemu: could not connect serial device"
2131 " to character backend '%s'\n", devname);
2138 static int parallel_parse(const char *devname)
2140 static int index = 0;
2143 if (strcmp(devname, "none") == 0)
2145 if (index == MAX_PARALLEL_PORTS) {
2146 fprintf(stderr, "qemu: too many parallel ports\n");
2149 snprintf(label, sizeof(label), "parallel%d", index);
2150 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2151 if (!parallel_hds[index]) {
2152 fprintf(stderr, "qemu: could not connect parallel device"
2153 " to character backend '%s'\n", devname);
2160 static int virtcon_parse(const char *devname)
2162 QemuOptsList *device = qemu_find_opts("device");
2163 static int index = 0;
2165 QemuOpts *bus_opts, *dev_opts;
2167 if (strcmp(devname, "none") == 0)
2169 if (index == MAX_VIRTIO_CONSOLES) {
2170 fprintf(stderr, "qemu: too many virtio consoles\n");
2174 bus_opts = qemu_opts_create(device, NULL, 0, NULL);
2175 if (arch_type == QEMU_ARCH_S390X) {
2176 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2178 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2181 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2182 qemu_opt_set(dev_opts, "driver", "virtconsole");
2184 snprintf(label, sizeof(label), "virtcon%d", index);
2185 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2186 if (!virtcon_hds[index]) {
2187 fprintf(stderr, "qemu: could not connect virtio console"
2188 " to character backend '%s'\n", devname);
2191 qemu_opt_set(dev_opts, "chardev", label);
2197 static int debugcon_parse(const char *devname)
2201 if (!qemu_chr_new("debugcon", devname, NULL)) {
2204 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2206 fprintf(stderr, "qemu: already have a debugcon device\n");
2209 qemu_opt_set(opts, "driver", "isa-debugcon");
2210 qemu_opt_set(opts, "chardev", "debugcon");
2214 static QEMUMachine *machine_parse(const char *name)
2216 QEMUMachine *m, *machine = NULL;
2219 machine = find_machine(name);
2224 printf("Supported machines are:\n");
2225 for (m = first_machine; m != NULL; m = m->next) {
2227 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2229 printf("%-20s %s%s\n", m->name, m->desc,
2230 m->is_default ? " (default)" : "");
2232 exit(!name || !is_help_option(name));
2235 static int tcg_init(void)
2238 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2239 ret = hax_accel_init();
2244 const char *opt_name;
2246 int (*available)(void);
2250 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2251 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2252 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2253 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2256 static int configure_accelerator(void)
2258 const char *p = NULL;
2261 bool accel_initialised = 0;
2262 bool init_failed = 0;
2264 QemuOptsList *list = qemu_find_opts("machine");
2265 if (!QTAILQ_EMPTY(&list->head)) {
2266 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2270 /* Use the default "accelerator", tcg */
2274 while (!accel_initialised && *p != '\0') {
2278 p = get_opt_name(buf, sizeof (buf), p, ':');
2279 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2280 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2281 *(accel_list[i].allowed) = 1;
2282 ret = accel_list[i].init();
2285 if (!accel_list[i].available()) {
2286 printf("%s not supported for this target\n",
2287 accel_list[i].name);
2289 fprintf(stderr, "failed to initialize %s: %s\n",
2293 *(accel_list[i].allowed) = 0;
2295 accel_initialised = 1;
2300 if (i == ARRAY_SIZE(accel_list)) {
2301 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2305 if (!accel_initialised) {
2306 fprintf(stderr, "No accelerator found!\n");
2308 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "No accelerator found.");
2314 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2317 return !accel_initialised;
2320 void qemu_add_exit_notifier(Notifier *notify)
2322 notifier_list_add(&exit_notifiers, notify);
2325 void qemu_remove_exit_notifier(Notifier *notify)
2327 notifier_remove(notify);
2330 static void qemu_run_exit_notifiers(void)
2332 notifier_list_notify(&exit_notifiers, NULL);
2335 void qemu_add_machine_init_done_notifier(Notifier *notify)
2337 notifier_list_add(&machine_init_done_notifiers, notify);
2340 static void qemu_run_machine_init_done_notifiers(void)
2342 notifier_list_notify(&machine_init_done_notifiers, NULL);
2345 static const QEMUOption *lookup_opt(int argc, char **argv,
2346 const char **poptarg, int *poptind)
2348 const QEMUOption *popt;
2349 int optind = *poptind;
2350 char *r = argv[optind];
2353 loc_set_cmdline(argv, optind, 1);
2355 /* Treat --foo the same as -foo. */
2358 popt = qemu_options;
2361 error_report("invalid option");
2363 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "invalid option.");
2367 if (!strcmp(popt->name, r + 1))
2371 if (popt->flags & HAS_ARG) {
2372 if (optind >= argc) {
2373 error_report("requires an argument");
2375 maru_register_exit_msg(MARU_EXIT_UNKNOWN, "requires an argument.");
2379 optarg = argv[optind++];
2380 loc_set_cmdline(argv, optind - 2, 2);
2391 static gpointer malloc_and_trace(gsize n_bytes)
2393 void *ptr = malloc(n_bytes);
2394 trace_g_malloc(n_bytes, ptr);
2398 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2400 void *ptr = realloc(mem, n_bytes);
2401 trace_g_realloc(mem, n_bytes, ptr);
2405 static void free_and_trace(gpointer mem)
2411 int qemu_init_main_loop(void)
2413 return main_loop_init();
2417 int use_qemu_display = 0; //0:use tizen qemu sdl, 1:use original qemu sdl
2418 // W/A for preserve larger continuous heap for RAM.
2419 void *preallocated_ptr = 0;
2422 int main(int argc, char **argv, char **envp)
2425 int snapshot, linux_boot;
2426 const char *icount_option = NULL;
2427 const char *initrd_filename;
2428 const char *kernel_filename, *kernel_cmdline;
2429 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2431 DisplayChangeListener *dcl;
2432 int cyls, heads, secs, translation;
2433 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2434 QemuOptsList *olist;
2437 const char *loadvm = NULL;
2438 QEMUMachine *machine;
2439 const char *cpu_model;
2440 const char *vga_model = "none";
2441 const char *pid_file = NULL;
2442 const char *incoming = NULL;
2444 int show_vnc_port = 0;
2446 bool defconfig = true;
2447 bool userconfig = true;
2448 const char *log_mask = NULL;
2449 const char *log_file = NULL;
2450 GMemVTable mem_trace = {
2451 .malloc = malloc_and_trace,
2452 .realloc = realloc_and_trace,
2453 .free = free_and_trace,
2455 const char *trace_events = NULL;
2456 const char *trace_file = NULL;
2458 atexit(qemu_run_exit_notifiers);
2459 error_set_progname(argv[0]);
2461 g_mem_set_vtable(&mem_trace);
2462 if (!g_thread_supported()) {
2463 #if !GLIB_CHECK_VERSION(2, 31, 0)
2464 g_thread_init(NULL);
2466 fprintf(stderr, "glib threading failed to initialize.\n");
2471 module_call_init(MODULE_INIT_QOM);
2476 rtc_clock = host_clock;
2478 qemu_cache_utils_init(envp);
2480 QLIST_INIT (&vm_change_state_head);
2481 os_setup_early_signal_handling();
2483 module_call_init(MODULE_INIT_MACHINE);
2484 machine = find_default_machine();
2488 cyls = heads = secs = 0;
2489 translation = BIOS_ATA_TRANSLATION_AUTO;
2491 for (i = 0; i < MAX_NODES; i++) {
2493 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
2501 /* first pass of option parsing */
2503 while (optind < argc) {
2504 if (argv[optind][0] != '-') {
2509 const QEMUOption *popt;
2511 popt = lookup_opt(argc, argv, &optarg, &optind);
2512 switch (popt->index) {
2513 case QEMU_OPTION_nodefconfig:
2516 case QEMU_OPTION_nouserconfig:
2525 ret = qemu_read_default_config_files(userconfig);
2531 /* second pass of option parsing */
2536 if (argv[optind][0] != '-') {
2537 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2539 const QEMUOption *popt;
2541 popt = lookup_opt(argc, argv, &optarg, &optind);
2542 if (!(popt->arch_mask & arch_type)) {
2543 printf("Option %s not supported for this target\n", popt->name);
2546 switch(popt->index) {
2548 machine = machine_parse(optarg);
2550 case QEMU_OPTION_cpu:
2551 /* hw initialization will check this */
2554 case QEMU_OPTION_hda:
2558 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2560 snprintf(buf, sizeof(buf),
2561 "%s,cyls=%d,heads=%d,secs=%d%s",
2562 HD_OPTS , cyls, heads, secs,
2563 translation == BIOS_ATA_TRANSLATION_LBA ?
2565 translation == BIOS_ATA_TRANSLATION_NONE ?
2566 ",trans=none" : "");
2567 drive_add(IF_DEFAULT, 0, optarg, buf);
2570 case QEMU_OPTION_hdb:
2571 case QEMU_OPTION_hdc:
2572 case QEMU_OPTION_hdd:
2573 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2576 case QEMU_OPTION_drive:
2577 if (drive_def(optarg) == NULL) {
2581 case QEMU_OPTION_set:
2582 if (qemu_set_option(optarg) != 0)
2585 case QEMU_OPTION_global:
2586 if (qemu_global_option(optarg) != 0)
2589 case QEMU_OPTION_mtdblock:
2590 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2592 case QEMU_OPTION_sd:
2593 drive_add(IF_SD, 0, optarg, SD_OPTS);
2595 case QEMU_OPTION_pflash:
2596 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2598 case QEMU_OPTION_snapshot:
2601 case QEMU_OPTION_hdachs:
2605 cyls = strtol(p, (char **)&p, 0);
2606 if (cyls < 1 || cyls > 16383)
2611 heads = strtol(p, (char **)&p, 0);
2612 if (heads < 1 || heads > 16)
2617 secs = strtol(p, (char **)&p, 0);
2618 if (secs < 1 || secs > 63)
2622 if (!strcmp(p, "none"))
2623 translation = BIOS_ATA_TRANSLATION_NONE;
2624 else if (!strcmp(p, "lba"))
2625 translation = BIOS_ATA_TRANSLATION_LBA;
2626 else if (!strcmp(p, "auto"))
2627 translation = BIOS_ATA_TRANSLATION_AUTO;
2630 } else if (*p != '\0') {
2632 fprintf(stderr, "qemu: invalid physical CHS format\n");
2635 if (hda_opts != NULL) {
2637 snprintf(num, sizeof(num), "%d", cyls);
2638 qemu_opt_set(hda_opts, "cyls", num);
2639 snprintf(num, sizeof(num), "%d", heads);
2640 qemu_opt_set(hda_opts, "heads", num);
2641 snprintf(num, sizeof(num), "%d", secs);
2642 qemu_opt_set(hda_opts, "secs", num);
2643 if (translation == BIOS_ATA_TRANSLATION_LBA)
2644 qemu_opt_set(hda_opts, "trans", "lba");
2645 if (translation == BIOS_ATA_TRANSLATION_NONE)
2646 qemu_opt_set(hda_opts, "trans", "none");
2650 case QEMU_OPTION_numa:
2651 if (nb_numa_nodes >= MAX_NODES) {
2652 fprintf(stderr, "qemu: too many NUMA nodes\n");
2657 case QEMU_OPTION_display:
2658 display_type = select_display(optarg);
2660 case QEMU_OPTION_nographic:
2661 display_type = DT_NOGRAPHIC;
2663 case QEMU_OPTION_curses:
2664 #ifdef CONFIG_CURSES
2665 display_type = DT_CURSES;
2667 fprintf(stderr, "Curses support is disabled\n");
2671 case QEMU_OPTION_portrait:
2672 graphic_rotate = 90;
2674 case QEMU_OPTION_rotate:
2675 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2676 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2677 graphic_rotate != 180 && graphic_rotate != 270) {
2679 "qemu: only 90, 180, 270 deg rotation is available\n");
2683 case QEMU_OPTION_kernel:
2684 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2686 case QEMU_OPTION_initrd:
2687 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
2689 case QEMU_OPTION_append:
2690 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
2692 case QEMU_OPTION_dtb:
2693 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2695 case QEMU_OPTION_cdrom:
2696 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2698 case QEMU_OPTION_boot:
2700 static const char * const params[] = {
2701 "order", "once", "menu",
2702 "splash", "splash-time", NULL
2704 char buf[sizeof(boot_devices)];
2705 char *standard_boot_devices;
2708 if (!strchr(optarg, '=')) {
2710 pstrcpy(buf, sizeof(buf), optarg);
2711 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2713 "qemu: unknown boot parameter '%s' in '%s'\n",
2719 get_param_value(buf, sizeof(buf), "order", optarg)) {
2720 validate_bootdevices(buf);
2721 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2724 if (get_param_value(buf, sizeof(buf),
2726 validate_bootdevices(buf);
2727 standard_boot_devices = g_strdup(boot_devices);
2728 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2729 qemu_register_reset(restore_boot_devices,
2730 standard_boot_devices);
2732 if (get_param_value(buf, sizeof(buf),
2734 if (!strcmp(buf, "on")) {
2736 } else if (!strcmp(buf, "off")) {
2740 "qemu: invalid option value '%s'\n",
2745 qemu_opts_parse(qemu_find_opts("boot-opts"),
2750 case QEMU_OPTION_fda:
2751 case QEMU_OPTION_fdb:
2752 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2755 case QEMU_OPTION_no_fd_bootchk:
2758 case QEMU_OPTION_netdev:
2759 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2763 case QEMU_OPTION_net:
2764 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2768 #ifdef CONFIG_LIBISCSI
2769 case QEMU_OPTION_iscsi:
2770 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2777 case QEMU_OPTION_tftp:
2778 legacy_tftp_prefix = optarg;
2780 case QEMU_OPTION_bootp:
2781 legacy_bootp_filename = optarg;
2783 case QEMU_OPTION_redir:
2784 if (net_slirp_redir(optarg) < 0)
2788 case QEMU_OPTION_bt:
2789 add_device_config(DEV_BT, optarg);
2791 case QEMU_OPTION_audio_help:
2792 if (!(audio_available())) {
2793 printf("Option %s not supported for this target\n", popt->name);
2799 case QEMU_OPTION_soundhw:
2800 if (!(audio_available())) {
2801 printf("Option %s not supported for this target\n", popt->name);
2804 select_soundhw (optarg);
2809 case QEMU_OPTION_version:
2813 case QEMU_OPTION_m: {
2818 value = strtosz(optarg, &end);
2819 if (value < 0 || *end) {
2820 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2823 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
2825 if (ram_size != sz) {
2826 fprintf(stderr, "qemu: ram size too large\n");
2831 case QEMU_OPTION_mempath:
2835 case QEMU_OPTION_mem_prealloc:
2846 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2848 case QEMU_OPTION_gdb:
2849 add_device_config(DEV_GDB, optarg);
2854 case QEMU_OPTION_bios:
2857 case QEMU_OPTION_singlestep:
2864 keyboard_layout = optarg;
2866 case QEMU_OPTION_localtime:
2869 case QEMU_OPTION_vga:
2878 w = strtol(p, (char **)&p, 10);
2881 fprintf(stderr, "qemu: invalid resolution or depth\n");
2887 h = strtol(p, (char **)&p, 10);
2892 depth = strtol(p, (char **)&p, 10);
2893 if (depth != 8 && depth != 15 && depth != 16 &&
2894 depth != 24 && depth != 32)
2896 } else if (*p == '\0') {
2897 depth = graphic_depth;
2904 graphic_depth = depth;
2907 case QEMU_OPTION_echr:
2910 term_escape_char = strtol(optarg, &r, 0);
2912 printf("Bad argument to echr\n");
2915 case QEMU_OPTION_monitor:
2916 monitor_parse(optarg, "readline");
2917 default_monitor = 0;
2919 case QEMU_OPTION_qmp:
2920 monitor_parse(optarg, "control");
2921 default_monitor = 0;
2923 case QEMU_OPTION_mon:
2924 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2928 default_monitor = 0;
2930 case QEMU_OPTION_chardev:
2931 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2936 case QEMU_OPTION_fsdev:
2937 olist = qemu_find_opts("fsdev");
2939 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2942 opts = qemu_opts_parse(olist, optarg, 1);
2944 fprintf(stderr, "parse error: %s\n", optarg);
2948 case QEMU_OPTION_virtfs: {
2951 const char *writeout, *sock_fd, *socket;
2953 olist = qemu_find_opts("virtfs");
2955 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2958 opts = qemu_opts_parse(olist, optarg, 1);
2960 fprintf(stderr, "parse error: %s\n", optarg);
2964 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2965 qemu_opt_get(opts, "mount_tag") == NULL) {
2966 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2969 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2970 qemu_opt_get(opts, "mount_tag"),
2973 fprintf(stderr, "duplicate fsdev id: %s\n",
2974 qemu_opt_get(opts, "mount_tag"));
2978 writeout = qemu_opt_get(opts, "writeout");
2980 #ifdef CONFIG_SYNC_FILE_RANGE
2981 qemu_opt_set(fsdev, "writeout", writeout);
2983 fprintf(stderr, "writeout=immediate not supported on "
2988 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2989 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2990 qemu_opt_set(fsdev, "security_model",
2991 qemu_opt_get(opts, "security_model"));
2992 socket = qemu_opt_get(opts, "socket");
2994 qemu_opt_set(fsdev, "socket", socket);
2996 sock_fd = qemu_opt_get(opts, "sock_fd");
2998 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3001 qemu_opt_set_bool(fsdev, "readonly",
3002 qemu_opt_get_bool(opts, "readonly", 0));
3003 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3005 qemu_opt_set(device, "driver", "virtio-9p-pci");
3006 qemu_opt_set(device, "fsdev",
3007 qemu_opt_get(opts, "mount_tag"));
3008 qemu_opt_set(device, "mount_tag",
3009 qemu_opt_get(opts, "mount_tag"));
3012 case QEMU_OPTION_virtfs_synth: {
3016 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3019 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3022 qemu_opt_set(fsdev, "fsdriver", "synth");
3024 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3026 qemu_opt_set(device, "driver", "virtio-9p-pci");
3027 qemu_opt_set(device, "fsdev", "v_synth");
3028 qemu_opt_set(device, "mount_tag", "v_synth");
3031 case QEMU_OPTION_serial:
3032 add_device_config(DEV_SERIAL, optarg);
3034 if (strncmp(optarg, "mon:", 4) == 0) {
3035 default_monitor = 0;
3038 case QEMU_OPTION_watchdog:
3041 "qemu: only one watchdog option may be given\n");
3046 case QEMU_OPTION_watchdog_action:
3047 if (select_watchdog_action(optarg) == -1) {
3048 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3052 case QEMU_OPTION_virtiocon:
3053 add_device_config(DEV_VIRTCON, optarg);
3054 default_virtcon = 0;
3055 if (strncmp(optarg, "mon:", 4) == 0) {
3056 default_monitor = 0;
3059 case QEMU_OPTION_parallel:
3060 add_device_config(DEV_PARALLEL, optarg);
3061 default_parallel = 0;
3062 if (strncmp(optarg, "mon:", 4) == 0) {
3063 default_monitor = 0;
3066 case QEMU_OPTION_debugcon:
3067 add_device_config(DEV_DEBUGCON, optarg);
3069 case QEMU_OPTION_loadvm:
3072 case QEMU_OPTION_full_screen:
3076 case QEMU_OPTION_no_frame:
3079 case QEMU_OPTION_alt_grab:
3082 case QEMU_OPTION_ctrl_grab:
3085 case QEMU_OPTION_no_quit:
3088 case QEMU_OPTION_sdl:
3089 display_type = DT_SDL;
3092 case QEMU_OPTION_no_frame:
3093 case QEMU_OPTION_alt_grab:
3094 case QEMU_OPTION_ctrl_grab:
3095 case QEMU_OPTION_no_quit:
3096 case QEMU_OPTION_sdl:
3097 fprintf(stderr, "SDL support is disabled\n");
3100 case QEMU_OPTION_pidfile:
3103 case QEMU_OPTION_win2k_hack:
3104 win2k_install_hack = 1;
3106 case QEMU_OPTION_rtc_td_hack: {
3107 static GlobalProperty slew_lost_ticks[] = {
3109 .driver = "mc146818rtc",
3110 .property = "lost_tick_policy",
3113 { /* end of list */ }
3116 qdev_prop_register_global_list(slew_lost_ticks);
3119 case QEMU_OPTION_acpitable:
3120 do_acpitable_option(optarg);
3122 case QEMU_OPTION_smbios:
3123 do_smbios_option(optarg);
3125 #ifdef CONFIG_OPENGLES
3126 case QEMU_OPTION_gles2_quality:
3127 gles2_quality = strtoul(optarg, NULL, 10);
3130 case QEMU_OPTION_enable_kvm:
3131 olist = qemu_find_opts("machine");
3132 qemu_opts_parse(olist, "accel=kvm", 0);
3134 case QEMU_OPTION_enable_gl:
3135 #if defined(CONFIG_MARU) && defined(CONFIG_GL_BACKEND)
3138 fprintf(stderr, "Virtio GL support is disabled, ignoring -enable-gl\n");
3141 case QEMU_OPTION_machine:
3142 olist = qemu_find_opts("machine");
3143 opts = qemu_opts_parse(olist, optarg, 1);
3145 fprintf(stderr, "parse error: %s\n", optarg);
3148 optarg = qemu_opt_get(opts, "type");
3150 machine = machine_parse(optarg);
3153 case QEMU_OPTION_usb:
3156 case QEMU_OPTION_usbdevice:
3158 add_device_config(DEV_USB, optarg);
3160 case QEMU_OPTION_device:
3161 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3165 case QEMU_OPTION_smp:
3168 fprintf(stderr, "Invalid number of CPUs\n");
3171 if (max_cpus < smp_cpus) {
3172 fprintf(stderr, "maxcpus must be equal to or greater than "
3176 if (max_cpus > 255) {
3177 fprintf(stderr, "Unsupported number of maxcpus\n");
3181 case QEMU_OPTION_vnc:
3184 vnc_display = optarg;
3186 fprintf(stderr, "VNC support is disabled\n");
3190 case QEMU_OPTION_no_acpi:
3193 case QEMU_OPTION_no_hpet:
3196 case QEMU_OPTION_balloon:
3197 if (balloon_parse(optarg) < 0) {
3198 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3202 case QEMU_OPTION_no_reboot:
3205 case QEMU_OPTION_no_shutdown:
3208 case QEMU_OPTION_show_cursor:
3211 case QEMU_OPTION_uuid:
3212 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3213 fprintf(stderr, "Fail to parse UUID string."
3214 " Wrong format.\n");
3218 case QEMU_OPTION_option_rom:
3219 if (nb_option_roms >= MAX_OPTION_ROMS) {
3220 fprintf(stderr, "Too many option ROMs\n");
3223 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3224 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3225 option_rom[nb_option_roms].bootindex =
3226 qemu_opt_get_number(opts, "bootindex", -1);
3227 if (!option_rom[nb_option_roms].name) {
3228 fprintf(stderr, "Option ROM file is not specified\n");
3233 case QEMU_OPTION_semihosting:
3234 semihosting_enabled = 1;
3236 case QEMU_OPTION_name:
3237 qemu_name = g_strdup(optarg);
3239 char *p = strchr(qemu_name, ',');
3242 if (strncmp(p, "process=", 8)) {
3243 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3247 os_set_proc_name(p);
3251 case QEMU_OPTION_prom_env:
3252 if (nb_prom_envs >= MAX_PROM_ENVS) {
3253 fprintf(stderr, "Too many prom variables\n");
3256 prom_envs[nb_prom_envs] = optarg;
3259 case QEMU_OPTION_old_param:
3262 case QEMU_OPTION_clock:
3263 configure_alarms(optarg);
3265 case QEMU_OPTION_startdate:
3266 configure_rtc_date_offset(optarg, 1);
3268 case QEMU_OPTION_rtc:
3269 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3273 configure_rtc(opts);
3275 case QEMU_OPTION_tb_size:
3276 tcg_tb_size = strtol(optarg, NULL, 0);
3277 if (tcg_tb_size < 0) {
3281 case QEMU_OPTION_icount:
3282 icount_option = optarg;
3284 case QEMU_OPTION_incoming:
3286 runstate_set(RUN_STATE_INMIGRATE);
3288 case QEMU_OPTION_nodefaults:
3290 default_parallel = 0;
3291 default_virtcon = 0;
3292 default_monitor = 0;
3299 case QEMU_OPTION_xen_domid:
3300 if (!(xen_available())) {
3301 printf("Option %s not supported for this target\n", popt->name);
3304 xen_domid = atoi(optarg);
3306 case QEMU_OPTION_xen_create:
3307 if (!(xen_available())) {
3308 printf("Option %s not supported for this target\n", popt->name);
3311 xen_mode = XEN_CREATE;
3313 case QEMU_OPTION_xen_attach:
3314 if (!(xen_available())) {
3315 printf("Option %s not supported for this target\n", popt->name);
3318 xen_mode = XEN_ATTACH;
3320 case QEMU_OPTION_trace:
3322 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3326 trace_events = qemu_opt_get(opts, "events");
3327 trace_file = qemu_opt_get(opts, "file");
3330 case QEMU_OPTION_readconfig:
3332 int ret = qemu_read_config_file(optarg);
3334 fprintf(stderr, "read config %s: %s\n", optarg,
3340 case QEMU_OPTION_spice:
3341 olist = qemu_find_opts("spice");
3343 fprintf(stderr, "spice is not supported by this qemu build.\n");
3346 opts = qemu_opts_parse(olist, optarg, 0);
3348 fprintf(stderr, "parse error: %s\n", optarg);
3352 case QEMU_OPTION_writeconfig:
3355 if (strcmp(optarg, "-") == 0) {
3358 fp = fopen(optarg, "w");
3360 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3364 qemu_config_write(fp);
3368 case QEMU_OPTION_qtest:
3369 qtest_chrdev = optarg;
3371 case QEMU_OPTION_qtest_log:
3374 case QEMU_OPTION_sandbox:
3375 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3380 case QEMU_OPTION_enable_hax:
3381 #ifdef CONFIG_HAX_BACKEND
3382 olist = qemu_find_opts("machine");
3383 //qemu_opts_reset(olist);
3385 //qemu_opts_parse(olist, "accel=hax", 0);
3388 "HAX support is disabled, ignoring -enable-hax\n");
3392 case QEMU_OPTION_max_touch_point:
3394 int cnt = atoi(optarg);
3395 fprintf(stderr, "maxTouchPoint:%d\n", cnt);
3396 set_emul_max_touch_point(cnt);
3399 case QEMU_OPTION_disable_skin:
3404 os_parse_cmd_args(popt->index, optarg);
3410 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3414 if (machine == NULL) {
3415 fprintf(stderr, "No machine found.\n");
3419 if (machine->hw_version) {
3420 qemu_set_version(machine->hw_version);
3423 /* Init CPU def lists, based on config
3424 * - Must be called after all the qemu_read_config_file() calls
3425 * - Must be called before list_cpus()
3426 * - Must be called before machine->init()
3430 if (cpu_model && is_help_option(cpu_model)) {
3431 list_cpus(stdout, &fprintf, cpu_model);
3436 /* Open the logfile at this point, if necessary. We can't open the logfile
3437 * when encountering either of the logging options (-d or -D) because the
3438 * other one may be encountered later on the command line, changing the
3439 * location or level of logging.
3443 set_cpu_log_filename(log_file);
3445 set_cpu_log(log_mask);
3448 if (!trace_backend_init(trace_events, trace_file)) {
3452 /* If no data_dir is specified then try to find it relative to the
3455 data_dir = os_find_datadir(argv[0]);
3457 /* If all else fails use the install path specified when building. */
3459 data_dir = CONFIG_QEMU_DATADIR;
3463 * Default to max_cpus = smp_cpus, in case the user doesn't
3464 * specify a max_cpus value.
3467 max_cpus = smp_cpus;
3469 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3470 if (smp_cpus > machine->max_cpus) {
3471 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3472 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3478 * Get the default machine options from the machine if it is not already
3479 * specified either by the configuration file or by the command line.
3481 if (machine->default_machine_opts) {
3482 qemu_opts_set_defaults(qemu_find_opts("machine"),
3483 machine->default_machine_opts, 0);
3486 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3487 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3489 if (machine->no_serial) {
3492 if (machine->no_parallel) {
3493 default_parallel = 0;
3495 if (!machine->use_virtcon) {
3496 default_virtcon = 0;
3498 if (machine->no_floppy) {
3501 if (machine->no_cdrom) {
3504 if (machine->no_sdcard) {
3508 if (display_type == DT_NOGRAPHIC) {
3509 if (default_parallel)
3510 add_device_config(DEV_PARALLEL, "null");
3511 if (default_serial && default_monitor) {
3512 add_device_config(DEV_SERIAL, "mon:stdio");
3513 } else if (default_virtcon && default_monitor) {
3514 add_device_config(DEV_VIRTCON, "mon:stdio");
3517 add_device_config(DEV_SERIAL, "stdio");
3518 if (default_virtcon)
3519 add_device_config(DEV_VIRTCON, "stdio");
3520 if (default_monitor)
3521 monitor_parse("stdio", "readline");
3525 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3526 if (default_parallel)
3527 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3528 if (default_monitor)
3529 monitor_parse("vc:80Cx24C", "readline");
3530 if (default_virtcon)
3531 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3535 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3537 #ifdef CONFIG_VIRTFS
3538 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3545 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3550 /* init the memory */
3551 if (ram_size == 0) {
3552 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3555 // W/A for preserve larger continuous heap for RAM.
3556 preallocated_ptr = qemu_vmalloc(ram_size);
3559 hax_pre_init(ram_size);
3561 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
3566 configure_accelerator();
3568 qemu_init_cpu_loop();
3569 if (qemu_init_main_loop()) {
3570 fprintf(stderr, "qemu_init_main_loop failed\n");
3574 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3576 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3577 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3578 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3580 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3583 if (!kernel_cmdline) {
3584 kernel_cmdline = "";
3587 linux_boot = (kernel_filename != NULL);
3589 if (!linux_boot && *kernel_cmdline != '\0') {
3590 fprintf(stderr, "-append only allowed with -kernel option\n");
3594 if (!linux_boot && initrd_filename != NULL) {
3595 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3599 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3600 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3604 os_set_line_buffering();
3606 if (init_timer_alarm() < 0) {
3607 fprintf(stderr, "could not initialize alarm timer\n");
3612 /* spice needs the timers to be initialized by this point */
3616 if (icount_option && (kvm_enabled() || xen_enabled() || hax_enabled())) {
3617 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3620 configure_icount(icount_option);
3622 if (net_init_clients() < 0) {
3626 /* init the bluetooth world */
3627 if (foreach_device_config(DEV_BT, bt_parse))
3630 if (!xen_enabled()) {
3631 /* On 32-bit hosts, QEMU is limited by virtual address space */
3632 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3633 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3638 cpu_exec_init_all();
3640 bdrv_init_with_whitelist();
3644 /* open the virtual block devices */
3646 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3647 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3650 default_drive(default_cdrom, snapshot, machine->use_scsi,
3651 IF_DEFAULT, 2, CDROM_OPTS);
3652 default_drive(default_floppy, snapshot, machine->use_scsi,
3653 IF_FLOPPY, 0, FD_OPTS);
3654 default_drive(default_sdcard, snapshot, machine->use_scsi,
3657 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
3659 if (nb_numa_nodes > 0) {
3662 if (nb_numa_nodes > MAX_NODES) {
3663 nb_numa_nodes = MAX_NODES;
3666 /* If no memory size if given for any node, assume the default case
3667 * and distribute the available memory equally across all nodes
3669 for (i = 0; i < nb_numa_nodes; i++) {
3670 if (node_mem[i] != 0)
3673 if (i == nb_numa_nodes) {
3674 uint64_t usedmem = 0;
3676 /* On Linux, the each node's border has to be 8MB aligned,
3677 * the final node gets the rest.
3679 for (i = 0; i < nb_numa_nodes - 1; i++) {
3680 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3681 usedmem += node_mem[i];
3683 node_mem[i] = ram_size - usedmem;
3686 for (i = 0; i < nb_numa_nodes; i++) {
3687 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
3691 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3692 * must cope with this anyway, because there are BIOSes out there in
3693 * real machines which also use this scheme.
3695 if (i == nb_numa_nodes) {
3696 for (i = 0; i < max_cpus; i++) {
3697 set_bit(i, node_cpumask[i % nb_numa_nodes]);
3702 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3706 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3708 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3710 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3712 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3715 /* If no default VGA is requested, the default is "none". */
3716 if (default_vga && cirrus_vga_available()) {
3717 vga_model = "cirrus";
3719 select_vgahw(vga_model);
3722 i = select_watchdog(watchdog);
3724 exit (i == 1 ? 1 : 0);
3727 if (machine->compat_props) {
3728 qdev_prop_register_global_list(machine->compat_props);
3732 qdev_machine_init();
3735 // return variable points different address from input variable.
3736 kernel_cmdline = prepare_maru_devices(kernel_cmdline);
3739 machine->init(ram_size, boot_devices,
3740 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3742 cpu_synchronize_all_post_init();
3746 current_machine = machine;
3748 if (hax_enabled()) {
3752 /* init USB devices */
3754 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3758 /* init generic devices */
3759 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3762 net_check_clients();
3764 /* just use the first displaystate for the moment */
3765 ds = get_displaystate();
3769 if (display_type == DT_DEFAULT && !display_remote) {
3770 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3771 display_type = DT_SDL;
3772 #elif defined(CONFIG_VNC)
3773 vnc_display = "localhost:0,to=99";
3775 #elif defined(CONFIG_MARU) && defined (__APPLE__)
3776 display_type = DT_MARU;
3778 display_type = DT_NONE;
3783 /* init local displays */
3784 switch (display_type) {
3787 #if defined(CONFIG_CURSES)
3789 curses_display_init(ds, full_screen);
3792 #if defined(CONFIG_SDL)
3794 #if defined(CONFIG_MARU)
3795 /* use tizen qemu sdl */
3796 maru_display_init(ds);
3798 if (skin_disabled == 1) {
3799 //do not start skin client process
3800 set_emul_skin_enable(0);
3802 set_emul_skin_enable(1);
3805 sdl_display_init(ds, full_screen, no_frame);
3808 #elif defined(CONFIG_COCOA)
3810 cocoa_display_init(ds, full_screen);
3813 #if defined(CONFIG_MARU) && defined(__APPLE__)
3815 maru_display_init(ds);
3816 if (skin_disabled == 1) {
3817 //do not start skin client process
3818 set_emul_skin_enable(0);
3820 set_emul_skin_enable(1);
3828 /* must be after terminal init, SDL library changes signal handlers */
3829 os_setup_signal_handling();
3832 /* init remote displays */
3834 vnc_display_init(ds);
3835 if (vnc_display_open(ds, vnc_display) < 0) {
3836 fprintf(stderr, "Failed to start VNC server on `%s'\n",
3841 if (show_vnc_port) {
3842 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3847 if (using_spice && !qxl_enabled) {
3848 qemu_spice_display_init(ds);
3854 dcl = ds->listeners;
3855 while (dcl != NULL) {
3856 if (dcl->dpy_refresh != NULL) {
3857 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3858 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3863 text_consoles_set_display(ds);
3865 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3869 qdev_machine_creation_done();
3871 if (rom_load_all() != 0) {
3872 fprintf(stderr, "rom loading failed\n");
3876 /* TODO: once all bus devices are qdevified, this should be done
3877 * when bus is created by qdev.c */
3878 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3879 qemu_run_machine_init_done_notifiers();
3881 qemu_system_reset(VMRESET_SILENT);
3883 if (load_vmstate(loadvm) < 0) {
3890 int ret = qemu_start_incoming_migration(incoming, &errp);
3892 if (error_is_set(&errp)) {
3893 fprintf(stderr, "Migrate: %s\n", error_get_pretty(errp));
3896 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3900 } else if (autostart) {