PPC: e500mc: Enable processor control
[qemu/agraf.git] / vl.c
blob2d464cf65c611dea7593dce907bf4d804794be0f
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <libgen.h>
37 #include <sys/times.h>
38 #include <sys/wait.h>
39 #include <termios.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <net/if.h>
46 #include <arpa/inet.h>
47 #include <dirent.h>
48 #include <netdb.h>
49 #include <sys/select.h>
51 #ifdef CONFIG_BSD
52 #include <sys/stat.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <libutil.h>
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <pty.h>
62 #include <malloc.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
66 #endif
67 #ifdef __sun__
68 #include <sys/stat.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
77 #include <net/if.h>
78 #include <syslog.h>
79 #include <stropts.h>
80 #endif
81 #endif
82 #endif
84 #if defined(__OpenBSD__)
85 #include <util.h>
86 #endif
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
90 #endif
92 #ifdef _WIN32
93 #include <windows.h>
94 #endif
96 #ifdef CONFIG_SDL
97 #if defined(__APPLE__) || defined(main)
98 #include <SDL.h>
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
104 #undef main
105 #define main qemu_main
106 #endif
107 #endif /* CONFIG_SDL */
109 #ifdef CONFIG_COCOA
110 #undef main
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
114 #include <glib.h>
116 #include "hw/hw.h"
117 #include "hw/boards.h"
118 #include "hw/usb.h"
119 #include "hw/pcmcia.h"
120 #include "hw/pc.h"
121 #include "hw/isa.h"
122 #include "hw/baum.h"
123 #include "hw/bt.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
126 #include "hw/xen.h"
127 #include "hw/qdev.h"
128 #include "hw/loader.h"
129 #include "bt-host.h"
130 #include "net.h"
131 #include "net/slirp.h"
132 #include "monitor.h"
133 #include "console.h"
134 #include "sysemu.h"
135 #include "gdbstub.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
139 #include "block.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
142 #include "dma.h"
143 #include "audio/audio.h"
144 #include "migration.h"
145 #include "kvm.h"
146 #include "qjson.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-options.h"
150 #include "qmp-commands.h"
151 #include "main-loop.h"
152 #ifdef CONFIG_VIRTFS
153 #include "fsdev/qemu-fsdev.h"
154 #endif
156 #include "disas.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
162 #include "trace.h"
163 #include "trace/control.h"
164 #include "qemu-queue.h"
165 #include "cpus.h"
166 #include "arch_init.h"
168 #include "ui/qemu-spice.h"
170 //#define DEBUG_NET
171 //#define DEBUG_SLIRP
173 #define DEFAULT_RAM_SIZE 128
175 #define MAX_VIRTIO_CONSOLES 1
177 static const char *data_dir;
178 const char *bios_name = NULL;
179 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
180 DisplayType display_type = DT_DEFAULT;
181 int display_remote = 0;
182 const char* keyboard_layout = NULL;
183 ram_addr_t ram_size;
184 const char *mem_path = NULL;
185 #ifdef MAP_POPULATE
186 int mem_prealloc = 0; /* force preallocation of physical target memory */
187 #endif
188 int nb_nics;
189 NICInfo nd_table[MAX_NICS];
190 int autostart;
191 static int rtc_utc = 1;
192 static int rtc_date_offset = -1; /* -1 means no change */
193 QEMUClock *rtc_clock;
194 int vga_interface_type = VGA_NONE;
195 static int full_screen = 0;
196 #ifdef CONFIG_SDL
197 static int no_frame = 0;
198 #endif
199 int no_quit = 0;
200 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
201 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
202 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
203 int win2k_install_hack = 0;
204 int usb_enabled = 0;
205 int singlestep = 0;
206 int smp_cpus = 1;
207 int max_cpus = 0;
208 int smp_cores = 1;
209 int smp_threads = 1;
210 #ifdef CONFIG_VNC
211 const char *vnc_display;
212 #endif
213 int acpi_enabled = 1;
214 int no_hpet = 0;
215 int fd_bootchk = 1;
216 int no_reboot = 0;
217 int no_shutdown = 0;
218 int cursor_hide = 1;
219 int graphic_rotate = 0;
220 const char *watchdog;
221 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
222 int nb_option_roms;
223 int semihosting_enabled = 0;
224 int old_param = 0;
225 const char *qemu_name;
226 int alt_grab = 0;
227 int ctrl_grab = 0;
228 unsigned int nb_prom_envs = 0;
229 const char *prom_envs[MAX_PROM_ENVS];
230 int boot_menu;
231 uint8_t *boot_splash_filedata;
232 int boot_splash_filedata_size;
233 uint8_t qemu_extra_params_fw[2];
235 typedef struct FWBootEntry FWBootEntry;
237 struct FWBootEntry {
238 QTAILQ_ENTRY(FWBootEntry) link;
239 int32_t bootindex;
240 DeviceState *dev;
241 char *suffix;
244 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
246 int nb_numa_nodes;
247 uint64_t node_mem[MAX_NODES];
248 uint64_t node_cpumask[MAX_NODES];
250 uint8_t qemu_uuid[16];
252 static QEMUBootSetHandler *boot_set_handler;
253 static void *boot_set_opaque;
255 static NotifierList exit_notifiers =
256 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
258 static NotifierList machine_init_done_notifiers =
259 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
261 static int tcg_allowed = 1;
262 int kvm_allowed = 0;
263 int xen_allowed = 0;
264 uint32_t xen_domid;
265 enum xen_mode xen_mode = XEN_EMULATE;
266 static int tcg_tb_size;
268 static int default_serial = 1;
269 static int default_parallel = 1;
270 static int default_virtcon = 1;
271 static int default_monitor = 1;
272 static int default_floppy = 1;
273 static int default_cdrom = 1;
274 static int default_sdcard = 1;
276 static struct {
277 const char *driver;
278 int *flag;
279 } default_list[] = {
280 { .driver = "isa-serial", .flag = &default_serial },
281 { .driver = "isa-parallel", .flag = &default_parallel },
282 { .driver = "isa-fdc", .flag = &default_floppy },
283 { .driver = "ide-cd", .flag = &default_cdrom },
284 { .driver = "ide-hd", .flag = &default_cdrom },
285 { .driver = "ide-drive", .flag = &default_cdrom },
286 { .driver = "scsi-cd", .flag = &default_cdrom },
287 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
288 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
289 { .driver = "virtio-serial", .flag = &default_virtcon },
292 static void res_free(void)
294 if (boot_splash_filedata != NULL) {
295 g_free(boot_splash_filedata);
296 boot_splash_filedata = NULL;
300 static int default_driver_check(QemuOpts *opts, void *opaque)
302 const char *driver = qemu_opt_get(opts, "driver");
303 int i;
305 if (!driver)
306 return 0;
307 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
308 if (strcmp(default_list[i].driver, driver) != 0)
309 continue;
310 *(default_list[i].flag) = 0;
312 return 0;
315 /***********************************************************/
316 /* QEMU state */
318 static RunState current_run_state = RUN_STATE_PRELAUNCH;
320 typedef struct {
321 RunState from;
322 RunState to;
323 } RunStateTransition;
325 static const RunStateTransition runstate_transitions_def[] = {
326 /* from -> to */
327 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
329 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
330 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
332 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
333 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
335 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
336 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
338 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
339 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
341 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
342 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
344 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
345 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
346 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
348 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
349 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
351 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
353 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
354 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
355 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
356 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
357 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
358 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
359 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
360 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
361 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
363 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
365 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
366 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
368 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
369 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
371 { RUN_STATE_MAX, RUN_STATE_MAX },
374 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
376 bool runstate_check(RunState state)
378 return current_run_state == state;
381 void runstate_init(void)
383 const RunStateTransition *p;
385 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
387 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
388 runstate_valid_transitions[p->from][p->to] = true;
392 /* This function will abort() on invalid state transitions */
393 void runstate_set(RunState new_state)
395 assert(new_state < RUN_STATE_MAX);
397 if (!runstate_valid_transitions[current_run_state][new_state]) {
398 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
399 RunState_lookup[current_run_state],
400 RunState_lookup[new_state]);
401 abort();
404 current_run_state = new_state;
407 int runstate_is_running(void)
409 return runstate_check(RUN_STATE_RUNNING);
412 StatusInfo *qmp_query_status(Error **errp)
414 StatusInfo *info = g_malloc0(sizeof(*info));
416 info->running = runstate_is_running();
417 info->singlestep = singlestep;
418 info->status = current_run_state;
420 return info;
423 /***********************************************************/
424 /* real time host monotonic timer */
426 /***********************************************************/
427 /* host time/date access */
428 void qemu_get_timedate(struct tm *tm, int offset)
430 time_t ti;
431 struct tm *ret;
433 time(&ti);
434 ti += offset;
435 if (rtc_date_offset == -1) {
436 if (rtc_utc)
437 ret = gmtime(&ti);
438 else
439 ret = localtime(&ti);
440 } else {
441 ti -= rtc_date_offset;
442 ret = gmtime(&ti);
445 memcpy(tm, ret, sizeof(struct tm));
448 int qemu_timedate_diff(struct tm *tm)
450 time_t seconds;
452 if (rtc_date_offset == -1)
453 if (rtc_utc)
454 seconds = mktimegm(tm);
455 else {
456 struct tm tmp = *tm;
457 tmp.tm_isdst = -1; /* use timezone to figure it out */
458 seconds = mktime(&tmp);
460 else
461 seconds = mktimegm(tm) + rtc_date_offset;
463 return seconds - time(NULL);
466 void rtc_change_mon_event(struct tm *tm)
468 QObject *data;
470 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
471 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
472 qobject_decref(data);
475 static void configure_rtc_date_offset(const char *startdate, int legacy)
477 time_t rtc_start_date;
478 struct tm tm;
480 if (!strcmp(startdate, "now") && legacy) {
481 rtc_date_offset = -1;
482 } else {
483 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
484 &tm.tm_year,
485 &tm.tm_mon,
486 &tm.tm_mday,
487 &tm.tm_hour,
488 &tm.tm_min,
489 &tm.tm_sec) == 6) {
490 /* OK */
491 } else if (sscanf(startdate, "%d-%d-%d",
492 &tm.tm_year,
493 &tm.tm_mon,
494 &tm.tm_mday) == 3) {
495 tm.tm_hour = 0;
496 tm.tm_min = 0;
497 tm.tm_sec = 0;
498 } else {
499 goto date_fail;
501 tm.tm_year -= 1900;
502 tm.tm_mon--;
503 rtc_start_date = mktimegm(&tm);
504 if (rtc_start_date == -1) {
505 date_fail:
506 fprintf(stderr, "Invalid date format. Valid formats are:\n"
507 "'2006-06-17T16:01:21' or '2006-06-17'\n");
508 exit(1);
510 rtc_date_offset = time(NULL) - rtc_start_date;
514 static void configure_rtc(QemuOpts *opts)
516 const char *value;
518 value = qemu_opt_get(opts, "base");
519 if (value) {
520 if (!strcmp(value, "utc")) {
521 rtc_utc = 1;
522 } else if (!strcmp(value, "localtime")) {
523 rtc_utc = 0;
524 } else {
525 configure_rtc_date_offset(value, 0);
528 value = qemu_opt_get(opts, "clock");
529 if (value) {
530 if (!strcmp(value, "host")) {
531 rtc_clock = host_clock;
532 } else if (!strcmp(value, "vm")) {
533 rtc_clock = vm_clock;
534 } else {
535 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
536 exit(1);
539 value = qemu_opt_get(opts, "driftfix");
540 if (value) {
541 if (!strcmp(value, "slew")) {
542 static GlobalProperty slew_lost_ticks[] = {
544 .driver = "mc146818rtc",
545 .property = "lost_tick_policy",
546 .value = "slew",
548 { /* end of list */ }
551 qdev_prop_register_global_list(slew_lost_ticks);
552 } else if (!strcmp(value, "none")) {
553 /* discard is default */
554 } else {
555 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
556 exit(1);
561 /***********************************************************/
562 /* Bluetooth support */
563 static int nb_hcis;
564 static int cur_hci;
565 static struct HCIInfo *hci_table[MAX_NICS];
567 static struct bt_vlan_s {
568 struct bt_scatternet_s net;
569 int id;
570 struct bt_vlan_s *next;
571 } *first_bt_vlan;
573 /* find or alloc a new bluetooth "VLAN" */
574 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
576 struct bt_vlan_s **pvlan, *vlan;
577 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
578 if (vlan->id == id)
579 return &vlan->net;
581 vlan = g_malloc0(sizeof(struct bt_vlan_s));
582 vlan->id = id;
583 pvlan = &first_bt_vlan;
584 while (*pvlan != NULL)
585 pvlan = &(*pvlan)->next;
586 *pvlan = vlan;
587 return &vlan->net;
590 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
594 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
596 return -ENOTSUP;
599 static struct HCIInfo null_hci = {
600 .cmd_send = null_hci_send,
601 .sco_send = null_hci_send,
602 .acl_send = null_hci_send,
603 .bdaddr_set = null_hci_addr_set,
606 struct HCIInfo *qemu_next_hci(void)
608 if (cur_hci == nb_hcis)
609 return &null_hci;
611 return hci_table[cur_hci++];
614 static struct HCIInfo *hci_init(const char *str)
616 char *endp;
617 struct bt_scatternet_s *vlan = 0;
619 if (!strcmp(str, "null"))
620 /* null */
621 return &null_hci;
622 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
623 /* host[:hciN] */
624 return bt_host_hci(str[4] ? str + 5 : "hci0");
625 else if (!strncmp(str, "hci", 3)) {
626 /* hci[,vlan=n] */
627 if (str[3]) {
628 if (!strncmp(str + 3, ",vlan=", 6)) {
629 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
630 if (*endp)
631 vlan = 0;
633 } else
634 vlan = qemu_find_bt_vlan(0);
635 if (vlan)
636 return bt_new_hci(vlan);
639 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
641 return 0;
644 static int bt_hci_parse(const char *str)
646 struct HCIInfo *hci;
647 bdaddr_t bdaddr;
649 if (nb_hcis >= MAX_NICS) {
650 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
651 return -1;
654 hci = hci_init(str);
655 if (!hci)
656 return -1;
658 bdaddr.b[0] = 0x52;
659 bdaddr.b[1] = 0x54;
660 bdaddr.b[2] = 0x00;
661 bdaddr.b[3] = 0x12;
662 bdaddr.b[4] = 0x34;
663 bdaddr.b[5] = 0x56 + nb_hcis;
664 hci->bdaddr_set(hci, bdaddr.b);
666 hci_table[nb_hcis++] = hci;
668 return 0;
671 static void bt_vhci_add(int vlan_id)
673 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
675 if (!vlan->slave)
676 fprintf(stderr, "qemu: warning: adding a VHCI to "
677 "an empty scatternet %i\n", vlan_id);
679 bt_vhci_init(bt_new_hci(vlan));
682 static struct bt_device_s *bt_device_add(const char *opt)
684 struct bt_scatternet_s *vlan;
685 int vlan_id = 0;
686 char *endp = strstr(opt, ",vlan=");
687 int len = (endp ? endp - opt : strlen(opt)) + 1;
688 char devname[10];
690 pstrcpy(devname, MIN(sizeof(devname), len), opt);
692 if (endp) {
693 vlan_id = strtol(endp + 6, &endp, 0);
694 if (*endp) {
695 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
696 return 0;
700 vlan = qemu_find_bt_vlan(vlan_id);
702 if (!vlan->slave)
703 fprintf(stderr, "qemu: warning: adding a slave device to "
704 "an empty scatternet %i\n", vlan_id);
706 if (!strcmp(devname, "keyboard"))
707 return bt_keyboard_init(vlan);
709 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
710 return 0;
713 static int bt_parse(const char *opt)
715 const char *endp, *p;
716 int vlan;
718 if (strstart(opt, "hci", &endp)) {
719 if (!*endp || *endp == ',') {
720 if (*endp)
721 if (!strstart(endp, ",vlan=", 0))
722 opt = endp + 1;
724 return bt_hci_parse(opt);
726 } else if (strstart(opt, "vhci", &endp)) {
727 if (!*endp || *endp == ',') {
728 if (*endp) {
729 if (strstart(endp, ",vlan=", &p)) {
730 vlan = strtol(p, (char **) &endp, 0);
731 if (*endp) {
732 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
733 return 1;
735 } else {
736 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
737 return 1;
739 } else
740 vlan = 0;
742 bt_vhci_add(vlan);
743 return 0;
745 } else if (strstart(opt, "device:", &endp))
746 return !bt_device_add(endp);
748 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
749 return 1;
752 /***********************************************************/
753 /* QEMU Block devices */
755 #define HD_OPTS "media=disk"
756 #define CDROM_OPTS "media=cdrom"
757 #define FD_OPTS ""
758 #define PFLASH_OPTS ""
759 #define MTD_OPTS ""
760 #define SD_OPTS ""
762 static int drive_init_func(QemuOpts *opts, void *opaque)
764 int *use_scsi = opaque;
766 return drive_init(opts, *use_scsi) == NULL;
769 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
771 if (NULL == qemu_opt_get(opts, "snapshot")) {
772 qemu_opt_set(opts, "snapshot", "on");
774 return 0;
777 static void default_drive(int enable, int snapshot, int use_scsi,
778 BlockInterfaceType type, int index,
779 const char *optstr)
781 QemuOpts *opts;
783 if (type == IF_DEFAULT) {
784 type = use_scsi ? IF_SCSI : IF_IDE;
787 if (!enable || drive_get_by_index(type, index)) {
788 return;
791 opts = drive_add(type, index, NULL, optstr);
792 if (snapshot) {
793 drive_enable_snapshot(opts, NULL);
795 if (!drive_init(opts, use_scsi)) {
796 exit(1);
800 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
802 boot_set_handler = func;
803 boot_set_opaque = opaque;
806 int qemu_boot_set(const char *boot_devices)
808 if (!boot_set_handler) {
809 return -EINVAL;
811 return boot_set_handler(boot_set_opaque, boot_devices);
814 static void validate_bootdevices(char *devices)
816 /* We just do some generic consistency checks */
817 const char *p;
818 int bitmap = 0;
820 for (p = devices; *p != '\0'; p++) {
821 /* Allowed boot devices are:
822 * a-b: floppy disk drives
823 * c-f: IDE disk drives
824 * g-m: machine implementation dependent drives
825 * n-p: network devices
826 * It's up to each machine implementation to check if the given boot
827 * devices match the actual hardware implementation and firmware
828 * features.
830 if (*p < 'a' || *p > 'p') {
831 fprintf(stderr, "Invalid boot device '%c'\n", *p);
832 exit(1);
834 if (bitmap & (1 << (*p - 'a'))) {
835 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
836 exit(1);
838 bitmap |= 1 << (*p - 'a');
842 static void restore_boot_devices(void *opaque)
844 char *standard_boot_devices = opaque;
845 static int first = 1;
847 /* Restore boot order and remove ourselves after the first boot */
848 if (first) {
849 first = 0;
850 return;
853 qemu_boot_set(standard_boot_devices);
855 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
856 g_free(standard_boot_devices);
859 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
860 const char *suffix)
862 FWBootEntry *node, *i;
864 if (bootindex < 0) {
865 return;
868 assert(dev != NULL || suffix != NULL);
870 node = g_malloc0(sizeof(FWBootEntry));
871 node->bootindex = bootindex;
872 node->suffix = suffix ? g_strdup(suffix) : NULL;
873 node->dev = dev;
875 QTAILQ_FOREACH(i, &fw_boot_order, link) {
876 if (i->bootindex == bootindex) {
877 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
878 exit(1);
879 } else if (i->bootindex < bootindex) {
880 continue;
882 QTAILQ_INSERT_BEFORE(i, node, link);
883 return;
885 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
889 * This function returns null terminated string that consist of new line
890 * separated device paths.
892 * memory pointed by "size" is assigned total length of the array in bytes
895 char *get_boot_devices_list(uint32_t *size)
897 FWBootEntry *i;
898 uint32_t total = 0;
899 char *list = NULL;
901 QTAILQ_FOREACH(i, &fw_boot_order, link) {
902 char *devpath = NULL, *bootpath;
903 int len;
905 if (i->dev) {
906 devpath = qdev_get_fw_dev_path(i->dev);
907 assert(devpath);
910 if (i->suffix && devpath) {
911 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
913 bootpath = g_malloc(bootpathlen);
914 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
915 g_free(devpath);
916 } else if (devpath) {
917 bootpath = devpath;
918 } else {
919 assert(i->suffix);
920 bootpath = g_strdup(i->suffix);
923 if (total) {
924 list[total-1] = '\n';
926 len = strlen(bootpath) + 1;
927 list = g_realloc(list, total + len);
928 memcpy(&list[total], bootpath, len);
929 total += len;
930 g_free(bootpath);
933 *size = total;
935 return list;
938 static void numa_add(const char *optarg)
940 char option[128];
941 char *endptr;
942 unsigned long long value, endvalue;
943 int nodenr;
945 optarg = get_opt_name(option, 128, optarg, ',') + 1;
946 if (!strcmp(option, "node")) {
947 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
948 nodenr = nb_numa_nodes;
949 } else {
950 nodenr = strtoull(option, NULL, 10);
953 if (get_param_value(option, 128, "mem", optarg) == 0) {
954 node_mem[nodenr] = 0;
955 } else {
956 int64_t sval;
957 sval = strtosz(option, &endptr);
958 if (sval < 0 || *endptr) {
959 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
960 exit(1);
962 node_mem[nodenr] = sval;
964 if (get_param_value(option, 128, "cpus", optarg) == 0) {
965 node_cpumask[nodenr] = 0;
966 } else {
967 value = strtoull(option, &endptr, 10);
968 if (value >= 64) {
969 value = 63;
970 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
971 } else {
972 if (*endptr == '-') {
973 endvalue = strtoull(endptr+1, &endptr, 10);
974 if (endvalue >= 63) {
975 endvalue = 62;
976 fprintf(stderr,
977 "only 63 CPUs in NUMA mode supported.\n");
979 value = (2ULL << endvalue) - (1ULL << value);
980 } else {
981 value = 1ULL << value;
984 node_cpumask[nodenr] = value;
986 nb_numa_nodes++;
988 return;
991 static void smp_parse(const char *optarg)
993 int smp, sockets = 0, threads = 0, cores = 0;
994 char *endptr;
995 char option[128];
997 smp = strtoul(optarg, &endptr, 10);
998 if (endptr != optarg) {
999 if (*endptr == ',') {
1000 endptr++;
1003 if (get_param_value(option, 128, "sockets", endptr) != 0)
1004 sockets = strtoull(option, NULL, 10);
1005 if (get_param_value(option, 128, "cores", endptr) != 0)
1006 cores = strtoull(option, NULL, 10);
1007 if (get_param_value(option, 128, "threads", endptr) != 0)
1008 threads = strtoull(option, NULL, 10);
1009 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1010 max_cpus = strtoull(option, NULL, 10);
1012 /* compute missing values, prefer sockets over cores over threads */
1013 if (smp == 0 || sockets == 0) {
1014 sockets = sockets > 0 ? sockets : 1;
1015 cores = cores > 0 ? cores : 1;
1016 threads = threads > 0 ? threads : 1;
1017 if (smp == 0) {
1018 smp = cores * threads * sockets;
1020 } else {
1021 if (cores == 0) {
1022 threads = threads > 0 ? threads : 1;
1023 cores = smp / (sockets * threads);
1024 } else {
1025 threads = smp / (cores * sockets);
1028 smp_cpus = smp;
1029 smp_cores = cores > 0 ? cores : 1;
1030 smp_threads = threads > 0 ? threads : 1;
1031 if (max_cpus == 0)
1032 max_cpus = smp_cpus;
1035 /***********************************************************/
1036 /* USB devices */
1038 static int usb_device_add(const char *devname)
1040 const char *p;
1041 USBDevice *dev = NULL;
1043 if (!usb_enabled)
1044 return -1;
1046 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1047 dev = usbdevice_create(devname);
1048 if (dev)
1049 goto done;
1051 /* the other ones */
1052 #ifndef CONFIG_LINUX
1053 /* only the linux version is qdev-ified, usb-bsd still needs this */
1054 if (strstart(devname, "host:", &p)) {
1055 dev = usb_host_device_open(p);
1056 } else
1057 #endif
1058 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1059 dev = usb_bt_init(devname[2] ? hci_init(p) :
1060 bt_new_hci(qemu_find_bt_vlan(0)));
1061 } else {
1062 return -1;
1064 if (!dev)
1065 return -1;
1067 done:
1068 return 0;
1071 static int usb_device_del(const char *devname)
1073 int bus_num, addr;
1074 const char *p;
1076 if (strstart(devname, "host:", &p))
1077 return usb_host_device_close(p);
1079 if (!usb_enabled)
1080 return -1;
1082 p = strchr(devname, '.');
1083 if (!p)
1084 return -1;
1085 bus_num = strtoul(devname, NULL, 0);
1086 addr = strtoul(p + 1, NULL, 0);
1088 return usb_device_delete_addr(bus_num, addr);
1091 static int usb_parse(const char *cmdline)
1093 int r;
1094 r = usb_device_add(cmdline);
1095 if (r < 0) {
1096 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1098 return r;
1101 void do_usb_add(Monitor *mon, const QDict *qdict)
1103 const char *devname = qdict_get_str(qdict, "devname");
1104 if (usb_device_add(devname) < 0) {
1105 error_report("could not add USB device '%s'", devname);
1109 void do_usb_del(Monitor *mon, const QDict *qdict)
1111 const char *devname = qdict_get_str(qdict, "devname");
1112 if (usb_device_del(devname) < 0) {
1113 error_report("could not delete USB device '%s'", devname);
1117 /***********************************************************/
1118 /* PCMCIA/Cardbus */
1120 static struct pcmcia_socket_entry_s {
1121 PCMCIASocket *socket;
1122 struct pcmcia_socket_entry_s *next;
1123 } *pcmcia_sockets = 0;
1125 void pcmcia_socket_register(PCMCIASocket *socket)
1127 struct pcmcia_socket_entry_s *entry;
1129 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1130 entry->socket = socket;
1131 entry->next = pcmcia_sockets;
1132 pcmcia_sockets = entry;
1135 void pcmcia_socket_unregister(PCMCIASocket *socket)
1137 struct pcmcia_socket_entry_s *entry, **ptr;
1139 ptr = &pcmcia_sockets;
1140 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1141 if (entry->socket == socket) {
1142 *ptr = entry->next;
1143 g_free(entry);
1147 void pcmcia_info(Monitor *mon)
1149 struct pcmcia_socket_entry_s *iter;
1151 if (!pcmcia_sockets)
1152 monitor_printf(mon, "No PCMCIA sockets\n");
1154 for (iter = pcmcia_sockets; iter; iter = iter->next)
1155 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1156 iter->socket->attached ? iter->socket->card_string :
1157 "Empty");
1160 /***********************************************************/
1161 /* machine registration */
1163 static QEMUMachine *first_machine = NULL;
1164 QEMUMachine *current_machine = NULL;
1166 int qemu_register_machine(QEMUMachine *m)
1168 QEMUMachine **pm;
1169 pm = &first_machine;
1170 while (*pm != NULL)
1171 pm = &(*pm)->next;
1172 m->next = NULL;
1173 *pm = m;
1174 return 0;
1177 static QEMUMachine *find_machine(const char *name)
1179 QEMUMachine *m;
1181 for(m = first_machine; m != NULL; m = m->next) {
1182 if (!strcmp(m->name, name))
1183 return m;
1184 if (m->alias && !strcmp(m->alias, name))
1185 return m;
1187 return NULL;
1190 static QEMUMachine *find_default_machine(void)
1192 QEMUMachine *m;
1194 for(m = first_machine; m != NULL; m = m->next) {
1195 if (m->is_default) {
1196 return m;
1199 return NULL;
1202 /***********************************************************/
1203 /* main execution loop */
1205 static void gui_update(void *opaque)
1207 uint64_t interval = GUI_REFRESH_INTERVAL;
1208 DisplayState *ds = opaque;
1209 DisplayChangeListener *dcl = ds->listeners;
1211 dpy_refresh(ds);
1213 while (dcl != NULL) {
1214 if (dcl->gui_timer_interval &&
1215 dcl->gui_timer_interval < interval)
1216 interval = dcl->gui_timer_interval;
1217 dcl = dcl->next;
1219 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1222 struct vm_change_state_entry {
1223 VMChangeStateHandler *cb;
1224 void *opaque;
1225 QLIST_ENTRY (vm_change_state_entry) entries;
1228 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1230 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1231 void *opaque)
1233 VMChangeStateEntry *e;
1235 e = g_malloc0(sizeof (*e));
1237 e->cb = cb;
1238 e->opaque = opaque;
1239 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1240 return e;
1243 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1245 QLIST_REMOVE (e, entries);
1246 g_free (e);
1249 void vm_state_notify(int running, RunState state)
1251 VMChangeStateEntry *e;
1253 trace_vm_state_notify(running, state);
1255 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1256 e->cb(e->opaque, running, state);
1260 void vm_start(void)
1262 if (!runstate_is_running()) {
1263 cpu_enable_ticks();
1264 runstate_set(RUN_STATE_RUNNING);
1265 vm_state_notify(1, RUN_STATE_RUNNING);
1266 resume_all_vcpus();
1267 monitor_protocol_event(QEVENT_RESUME, NULL);
1271 /* reset/shutdown handler */
1273 typedef struct QEMUResetEntry {
1274 QTAILQ_ENTRY(QEMUResetEntry) entry;
1275 QEMUResetHandler *func;
1276 void *opaque;
1277 } QEMUResetEntry;
1279 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1280 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1281 static int reset_requested;
1282 static int shutdown_requested, shutdown_signal = -1;
1283 static pid_t shutdown_pid;
1284 static int powerdown_requested;
1285 static int debug_requested;
1286 static RunState vmstop_requested = RUN_STATE_MAX;
1288 int qemu_shutdown_requested_get(void)
1290 return shutdown_requested;
1293 int qemu_reset_requested_get(void)
1295 return reset_requested;
1298 int qemu_shutdown_requested(void)
1300 int r = shutdown_requested;
1301 shutdown_requested = 0;
1302 return r;
1305 void qemu_kill_report(void)
1307 if (shutdown_signal != -1) {
1308 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1309 if (shutdown_pid == 0) {
1310 /* This happens for eg ^C at the terminal, so it's worth
1311 * avoiding printing an odd message in that case.
1313 fputc('\n', stderr);
1314 } else {
1315 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1317 shutdown_signal = -1;
1321 int qemu_reset_requested(void)
1323 int r = reset_requested;
1324 reset_requested = 0;
1325 return r;
1328 int qemu_powerdown_requested(void)
1330 int r = powerdown_requested;
1331 powerdown_requested = 0;
1332 return r;
1335 static int qemu_debug_requested(void)
1337 int r = debug_requested;
1338 debug_requested = 0;
1339 return r;
1342 /* We use RUN_STATE_MAX but any invalid value will do */
1343 static bool qemu_vmstop_requested(RunState *r)
1345 if (vmstop_requested < RUN_STATE_MAX) {
1346 *r = vmstop_requested;
1347 vmstop_requested = RUN_STATE_MAX;
1348 return true;
1351 return false;
1354 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1356 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1358 re->func = func;
1359 re->opaque = opaque;
1360 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1363 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1365 QEMUResetEntry *re;
1367 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1368 if (re->func == func && re->opaque == opaque) {
1369 QTAILQ_REMOVE(&reset_handlers, re, entry);
1370 g_free(re);
1371 return;
1376 void qemu_system_reset(bool report)
1378 QEMUResetEntry *re, *nre;
1380 /* reset all devices */
1381 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1382 re->func(re->opaque);
1384 if (report) {
1385 monitor_protocol_event(QEVENT_RESET, NULL);
1387 cpu_synchronize_all_post_reset();
1390 void qemu_system_reset_request(void)
1392 if (no_reboot) {
1393 shutdown_requested = 1;
1394 } else {
1395 reset_requested = 1;
1397 cpu_stop_current();
1398 qemu_notify_event();
1401 void qemu_system_killed(int signal, pid_t pid)
1403 shutdown_signal = signal;
1404 shutdown_pid = pid;
1405 no_shutdown = 0;
1406 qemu_system_shutdown_request();
1409 void qemu_system_shutdown_request(void)
1411 shutdown_requested = 1;
1412 qemu_notify_event();
1415 void qemu_system_powerdown_request(void)
1417 powerdown_requested = 1;
1418 qemu_notify_event();
1421 void qemu_system_debug_request(void)
1423 debug_requested = 1;
1424 qemu_notify_event();
1427 void qemu_system_vmstop_request(RunState state)
1429 vmstop_requested = state;
1430 qemu_notify_event();
1433 qemu_irq qemu_system_powerdown;
1435 static bool main_loop_should_exit(void)
1437 RunState r;
1438 if (qemu_debug_requested()) {
1439 vm_stop(RUN_STATE_DEBUG);
1441 if (qemu_shutdown_requested()) {
1442 qemu_kill_report();
1443 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1444 if (no_shutdown) {
1445 vm_stop(RUN_STATE_SHUTDOWN);
1446 } else {
1447 return true;
1450 if (qemu_reset_requested()) {
1451 pause_all_vcpus();
1452 cpu_synchronize_all_states();
1453 qemu_system_reset(VMRESET_REPORT);
1454 resume_all_vcpus();
1455 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1456 runstate_check(RUN_STATE_SHUTDOWN)) {
1457 runstate_set(RUN_STATE_PAUSED);
1460 if (qemu_powerdown_requested()) {
1461 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1462 qemu_irq_raise(qemu_system_powerdown);
1464 if (qemu_vmstop_requested(&r)) {
1465 vm_stop(r);
1467 return false;
1470 static void main_loop(void)
1472 bool nonblocking;
1473 int last_io = 0;
1474 #ifdef CONFIG_PROFILER
1475 int64_t ti;
1476 #endif
1477 do {
1478 nonblocking = !kvm_enabled() && last_io > 0;
1479 #ifdef CONFIG_PROFILER
1480 ti = profile_getclock();
1481 #endif
1482 last_io = main_loop_wait(nonblocking);
1483 #ifdef CONFIG_PROFILER
1484 dev_time += profile_getclock() - ti;
1485 #endif
1486 } while (!main_loop_should_exit());
1489 static void version(void)
1491 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1494 static void help(int exitcode)
1496 version();
1497 printf("usage: %s [options] [disk_image]\n\n"
1498 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1499 error_get_progname());
1501 #define QEMU_OPTIONS_GENERATE_HELP
1502 #include "qemu-options-wrapper.h"
1504 printf("\nDuring emulation, the following keys are useful:\n"
1505 "ctrl-alt-f toggle full screen\n"
1506 "ctrl-alt-n switch to virtual console 'n'\n"
1507 "ctrl-alt toggle mouse and keyboard grab\n"
1508 "\n"
1509 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1511 exit(exitcode);
1514 #define HAS_ARG 0x0001
1516 typedef struct QEMUOption {
1517 const char *name;
1518 int flags;
1519 int index;
1520 uint32_t arch_mask;
1521 } QEMUOption;
1523 static const QEMUOption qemu_options[] = {
1524 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1525 #define QEMU_OPTIONS_GENERATE_OPTIONS
1526 #include "qemu-options-wrapper.h"
1527 { NULL },
1530 static bool vga_available(void)
1532 return qdev_exists("VGA") || qdev_exists("isa-vga");
1535 static bool cirrus_vga_available(void)
1537 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1540 static bool vmware_vga_available(void)
1542 return qdev_exists("vmware-svga");
1545 static void select_vgahw (const char *p)
1547 const char *opts;
1549 vga_interface_type = VGA_NONE;
1550 if (strstart(p, "std", &opts)) {
1551 if (vga_available()) {
1552 vga_interface_type = VGA_STD;
1553 } else {
1554 fprintf(stderr, "Error: standard VGA not available\n");
1555 exit(0);
1557 } else if (strstart(p, "cirrus", &opts)) {
1558 if (cirrus_vga_available()) {
1559 vga_interface_type = VGA_CIRRUS;
1560 } else {
1561 fprintf(stderr, "Error: Cirrus VGA not available\n");
1562 exit(0);
1564 } else if (strstart(p, "vmware", &opts)) {
1565 if (vmware_vga_available()) {
1566 vga_interface_type = VGA_VMWARE;
1567 } else {
1568 fprintf(stderr, "Error: VMWare SVGA not available\n");
1569 exit(0);
1571 } else if (strstart(p, "xenfb", &opts)) {
1572 vga_interface_type = VGA_XENFB;
1573 } else if (strstart(p, "qxl", &opts)) {
1574 vga_interface_type = VGA_QXL;
1575 } else if (!strstart(p, "none", &opts)) {
1576 invalid_vga:
1577 fprintf(stderr, "Unknown vga type: %s\n", p);
1578 exit(1);
1580 while (*opts) {
1581 const char *nextopt;
1583 if (strstart(opts, ",retrace=", &nextopt)) {
1584 opts = nextopt;
1585 if (strstart(opts, "dumb", &nextopt))
1586 vga_retrace_method = VGA_RETRACE_DUMB;
1587 else if (strstart(opts, "precise", &nextopt))
1588 vga_retrace_method = VGA_RETRACE_PRECISE;
1589 else goto invalid_vga;
1590 } else goto invalid_vga;
1591 opts = nextopt;
1595 static DisplayType select_display(const char *p)
1597 const char *opts;
1598 DisplayType display = DT_DEFAULT;
1600 if (strstart(p, "sdl", &opts)) {
1601 #ifdef CONFIG_SDL
1602 display = DT_SDL;
1603 while (*opts) {
1604 const char *nextopt;
1606 if (strstart(opts, ",frame=", &nextopt)) {
1607 opts = nextopt;
1608 if (strstart(opts, "on", &nextopt)) {
1609 no_frame = 0;
1610 } else if (strstart(opts, "off", &nextopt)) {
1611 no_frame = 1;
1612 } else {
1613 goto invalid_sdl_args;
1615 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1616 opts = nextopt;
1617 if (strstart(opts, "on", &nextopt)) {
1618 alt_grab = 1;
1619 } else if (strstart(opts, "off", &nextopt)) {
1620 alt_grab = 0;
1621 } else {
1622 goto invalid_sdl_args;
1624 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1625 opts = nextopt;
1626 if (strstart(opts, "on", &nextopt)) {
1627 ctrl_grab = 1;
1628 } else if (strstart(opts, "off", &nextopt)) {
1629 ctrl_grab = 0;
1630 } else {
1631 goto invalid_sdl_args;
1633 } else if (strstart(opts, ",window_close=", &nextopt)) {
1634 opts = nextopt;
1635 if (strstart(opts, "on", &nextopt)) {
1636 no_quit = 0;
1637 } else if (strstart(opts, "off", &nextopt)) {
1638 no_quit = 1;
1639 } else {
1640 goto invalid_sdl_args;
1642 } else {
1643 invalid_sdl_args:
1644 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1645 exit(1);
1647 opts = nextopt;
1649 #else
1650 fprintf(stderr, "SDL support is disabled\n");
1651 exit(1);
1652 #endif
1653 } else if (strstart(p, "vnc", &opts)) {
1654 #ifdef CONFIG_VNC
1655 display_remote++;
1657 if (*opts) {
1658 const char *nextopt;
1660 if (strstart(opts, "=", &nextopt)) {
1661 vnc_display = nextopt;
1664 if (!vnc_display) {
1665 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1666 exit(1);
1668 #else
1669 fprintf(stderr, "VNC support is disabled\n");
1670 exit(1);
1671 #endif
1672 } else if (strstart(p, "curses", &opts)) {
1673 #ifdef CONFIG_CURSES
1674 display = DT_CURSES;
1675 #else
1676 fprintf(stderr, "Curses support is disabled\n");
1677 exit(1);
1678 #endif
1679 } else if (strstart(p, "none", &opts)) {
1680 display = DT_NONE;
1681 } else {
1682 fprintf(stderr, "Unknown display type: %s\n", p);
1683 exit(1);
1686 return display;
1689 static int balloon_parse(const char *arg)
1691 QemuOpts *opts;
1693 if (strcmp(arg, "none") == 0) {
1694 return 0;
1697 if (!strncmp(arg, "virtio", 6)) {
1698 if (arg[6] == ',') {
1699 /* have params -> parse them */
1700 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1701 if (!opts)
1702 return -1;
1703 } else {
1704 /* create empty opts */
1705 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1707 qemu_opt_set(opts, "driver", "virtio-balloon");
1708 return 0;
1711 return -1;
1714 char *qemu_find_file(int type, const char *name)
1716 int len;
1717 const char *subdir;
1718 char *buf;
1720 /* If name contains path separators then try it as a straight path. */
1721 if ((strchr(name, '/') || strchr(name, '\\'))
1722 && access(name, R_OK) == 0) {
1723 return g_strdup(name);
1725 switch (type) {
1726 case QEMU_FILE_TYPE_BIOS:
1727 subdir = "";
1728 break;
1729 case QEMU_FILE_TYPE_KEYMAP:
1730 subdir = "keymaps/";
1731 break;
1732 default:
1733 abort();
1735 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1736 buf = g_malloc0(len);
1737 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1738 if (access(buf, R_OK)) {
1739 g_free(buf);
1740 return NULL;
1742 return buf;
1745 static int device_help_func(QemuOpts *opts, void *opaque)
1747 return qdev_device_help(opts);
1750 static int device_init_func(QemuOpts *opts, void *opaque)
1752 DeviceState *dev;
1754 dev = qdev_device_add(opts);
1755 if (!dev)
1756 return -1;
1757 return 0;
1760 static int chardev_init_func(QemuOpts *opts, void *opaque)
1762 CharDriverState *chr;
1764 chr = qemu_chr_new_from_opts(opts, NULL);
1765 if (!chr)
1766 return -1;
1767 return 0;
1770 #ifdef CONFIG_VIRTFS
1771 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1773 int ret;
1774 ret = qemu_fsdev_add(opts);
1776 return ret;
1778 #endif
1780 static int mon_init_func(QemuOpts *opts, void *opaque)
1782 CharDriverState *chr;
1783 const char *chardev;
1784 const char *mode;
1785 int flags;
1787 mode = qemu_opt_get(opts, "mode");
1788 if (mode == NULL) {
1789 mode = "readline";
1791 if (strcmp(mode, "readline") == 0) {
1792 flags = MONITOR_USE_READLINE;
1793 } else if (strcmp(mode, "control") == 0) {
1794 flags = MONITOR_USE_CONTROL;
1795 } else {
1796 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1797 exit(1);
1800 if (qemu_opt_get_bool(opts, "pretty", 0))
1801 flags |= MONITOR_USE_PRETTY;
1803 if (qemu_opt_get_bool(opts, "default", 0))
1804 flags |= MONITOR_IS_DEFAULT;
1806 chardev = qemu_opt_get(opts, "chardev");
1807 chr = qemu_chr_find(chardev);
1808 if (chr == NULL) {
1809 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1810 exit(1);
1813 monitor_init(chr, flags);
1814 return 0;
1817 static void monitor_parse(const char *optarg, const char *mode)
1819 static int monitor_device_index = 0;
1820 QemuOpts *opts;
1821 const char *p;
1822 char label[32];
1823 int def = 0;
1825 if (strstart(optarg, "chardev:", &p)) {
1826 snprintf(label, sizeof(label), "%s", p);
1827 } else {
1828 snprintf(label, sizeof(label), "compat_monitor%d",
1829 monitor_device_index);
1830 if (monitor_device_index == 0) {
1831 def = 1;
1833 opts = qemu_chr_parse_compat(label, optarg);
1834 if (!opts) {
1835 fprintf(stderr, "parse error: %s\n", optarg);
1836 exit(1);
1840 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1841 if (!opts) {
1842 fprintf(stderr, "duplicate chardev: %s\n", label);
1843 exit(1);
1845 qemu_opt_set(opts, "mode", mode);
1846 qemu_opt_set(opts, "chardev", label);
1847 if (def)
1848 qemu_opt_set(opts, "default", "on");
1849 monitor_device_index++;
1852 struct device_config {
1853 enum {
1854 DEV_USB, /* -usbdevice */
1855 DEV_BT, /* -bt */
1856 DEV_SERIAL, /* -serial */
1857 DEV_PARALLEL, /* -parallel */
1858 DEV_VIRTCON, /* -virtioconsole */
1859 DEV_DEBUGCON, /* -debugcon */
1860 } type;
1861 const char *cmdline;
1862 QTAILQ_ENTRY(device_config) next;
1864 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1866 static void add_device_config(int type, const char *cmdline)
1868 struct device_config *conf;
1870 conf = g_malloc0(sizeof(*conf));
1871 conf->type = type;
1872 conf->cmdline = cmdline;
1873 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1876 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1878 struct device_config *conf;
1879 int rc;
1881 QTAILQ_FOREACH(conf, &device_configs, next) {
1882 if (conf->type != type)
1883 continue;
1884 rc = func(conf->cmdline);
1885 if (0 != rc)
1886 return rc;
1888 return 0;
1891 static int serial_parse(const char *devname)
1893 static int index = 0;
1894 char label[32];
1896 if (strcmp(devname, "none") == 0)
1897 return 0;
1898 if (index == MAX_SERIAL_PORTS) {
1899 fprintf(stderr, "qemu: too many serial ports\n");
1900 exit(1);
1902 snprintf(label, sizeof(label), "serial%d", index);
1903 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1904 if (!serial_hds[index]) {
1905 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1906 devname, strerror(errno));
1907 return -1;
1909 index++;
1910 return 0;
1913 static int parallel_parse(const char *devname)
1915 static int index = 0;
1916 char label[32];
1918 if (strcmp(devname, "none") == 0)
1919 return 0;
1920 if (index == MAX_PARALLEL_PORTS) {
1921 fprintf(stderr, "qemu: too many parallel ports\n");
1922 exit(1);
1924 snprintf(label, sizeof(label), "parallel%d", index);
1925 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
1926 if (!parallel_hds[index]) {
1927 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1928 devname, strerror(errno));
1929 return -1;
1931 index++;
1932 return 0;
1935 static int virtcon_parse(const char *devname)
1937 QemuOptsList *device = qemu_find_opts("device");
1938 static int index = 0;
1939 char label[32];
1940 QemuOpts *bus_opts, *dev_opts;
1942 if (strcmp(devname, "none") == 0)
1943 return 0;
1944 if (index == MAX_VIRTIO_CONSOLES) {
1945 fprintf(stderr, "qemu: too many virtio consoles\n");
1946 exit(1);
1949 bus_opts = qemu_opts_create(device, NULL, 0);
1950 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1952 dev_opts = qemu_opts_create(device, NULL, 0);
1953 qemu_opt_set(dev_opts, "driver", "virtconsole");
1955 snprintf(label, sizeof(label), "virtcon%d", index);
1956 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
1957 if (!virtcon_hds[index]) {
1958 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1959 devname, strerror(errno));
1960 return -1;
1962 qemu_opt_set(dev_opts, "chardev", label);
1964 index++;
1965 return 0;
1968 static int debugcon_parse(const char *devname)
1970 QemuOpts *opts;
1972 if (!qemu_chr_new("debugcon", devname, NULL)) {
1973 exit(1);
1975 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1976 if (!opts) {
1977 fprintf(stderr, "qemu: already have a debugcon device\n");
1978 exit(1);
1980 qemu_opt_set(opts, "driver", "isa-debugcon");
1981 qemu_opt_set(opts, "chardev", "debugcon");
1982 return 0;
1985 static QEMUMachine *machine_parse(const char *name)
1987 QEMUMachine *m, *machine = NULL;
1989 if (name) {
1990 machine = find_machine(name);
1992 if (machine) {
1993 return machine;
1995 printf("Supported machines are:\n");
1996 for (m = first_machine; m != NULL; m = m->next) {
1997 if (m->alias) {
1998 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2000 printf("%-10s %s%s\n", m->name, m->desc,
2001 m->is_default ? " (default)" : "");
2003 exit(!name || *name != '?');
2006 static int tcg_init(void)
2008 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2009 return 0;
2012 static struct {
2013 const char *opt_name;
2014 const char *name;
2015 int (*available)(void);
2016 int (*init)(void);
2017 int *allowed;
2018 } accel_list[] = {
2019 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2020 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2021 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2024 static int configure_accelerator(void)
2026 const char *p = NULL;
2027 char buf[10];
2028 int i, ret;
2029 bool accel_initalised = 0;
2030 bool init_failed = 0;
2032 QemuOptsList *list = qemu_find_opts("machine");
2033 if (!QTAILQ_EMPTY(&list->head)) {
2034 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2037 if (p == NULL) {
2038 /* Use the default "accelerator", tcg */
2039 p = "tcg";
2042 while (!accel_initalised && *p != '\0') {
2043 if (*p == ':') {
2044 p++;
2046 p = get_opt_name(buf, sizeof (buf), p, ':');
2047 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2048 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2049 *(accel_list[i].allowed) = 1;
2050 ret = accel_list[i].init();
2051 if (ret < 0) {
2052 init_failed = 1;
2053 if (!accel_list[i].available()) {
2054 printf("%s not supported for this target\n",
2055 accel_list[i].name);
2056 } else {
2057 fprintf(stderr, "failed to initialize %s: %s\n",
2058 accel_list[i].name,
2059 strerror(-ret));
2061 *(accel_list[i].allowed) = 0;
2062 } else {
2063 accel_initalised = 1;
2065 break;
2068 if (i == ARRAY_SIZE(accel_list)) {
2069 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2073 if (!accel_initalised) {
2074 fprintf(stderr, "No accelerator found!\n");
2075 exit(1);
2078 if (init_failed) {
2079 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2082 return !accel_initalised;
2085 void qemu_add_exit_notifier(Notifier *notify)
2087 notifier_list_add(&exit_notifiers, notify);
2090 void qemu_remove_exit_notifier(Notifier *notify)
2092 notifier_list_remove(&exit_notifiers, notify);
2095 static void qemu_run_exit_notifiers(void)
2097 notifier_list_notify(&exit_notifiers, NULL);
2100 void qemu_add_machine_init_done_notifier(Notifier *notify)
2102 notifier_list_add(&machine_init_done_notifiers, notify);
2105 static void qemu_run_machine_init_done_notifiers(void)
2107 notifier_list_notify(&machine_init_done_notifiers, NULL);
2110 static const QEMUOption *lookup_opt(int argc, char **argv,
2111 const char **poptarg, int *poptind)
2113 const QEMUOption *popt;
2114 int optind = *poptind;
2115 char *r = argv[optind];
2116 const char *optarg;
2118 loc_set_cmdline(argv, optind, 1);
2119 optind++;
2120 /* Treat --foo the same as -foo. */
2121 if (r[1] == '-')
2122 r++;
2123 popt = qemu_options;
2124 for(;;) {
2125 if (!popt->name) {
2126 error_report("invalid option");
2127 exit(1);
2129 if (!strcmp(popt->name, r + 1))
2130 break;
2131 popt++;
2133 if (popt->flags & HAS_ARG) {
2134 if (optind >= argc) {
2135 error_report("requires an argument");
2136 exit(1);
2138 optarg = argv[optind++];
2139 loc_set_cmdline(argv, optind - 2, 2);
2140 } else {
2141 optarg = NULL;
2144 *poptarg = optarg;
2145 *poptind = optind;
2147 return popt;
2150 static gpointer malloc_and_trace(gsize n_bytes)
2152 void *ptr = malloc(n_bytes);
2153 trace_g_malloc(n_bytes, ptr);
2154 return ptr;
2157 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2159 void *ptr = realloc(mem, n_bytes);
2160 trace_g_realloc(mem, n_bytes, ptr);
2161 return ptr;
2164 static void free_and_trace(gpointer mem)
2166 trace_g_free(mem);
2167 free(mem);
2170 int qemu_init_main_loop(void)
2172 return main_loop_init();
2175 int main(int argc, char **argv, char **envp)
2177 const char *gdbstub_dev = NULL;
2178 int i;
2179 int snapshot, linux_boot;
2180 const char *icount_option = NULL;
2181 const char *initrd_filename;
2182 const char *kernel_filename, *kernel_cmdline;
2183 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2184 DisplayState *ds;
2185 DisplayChangeListener *dcl;
2186 int cyls, heads, secs, translation;
2187 QemuOpts *hda_opts = NULL, *opts;
2188 QemuOptsList *olist;
2189 int optind;
2190 const char *optarg;
2191 const char *loadvm = NULL;
2192 QEMUMachine *machine;
2193 const char *cpu_model;
2194 const char *vga_model = NULL;
2195 const char *pid_file = NULL;
2196 const char *incoming = NULL;
2197 #ifdef CONFIG_VNC
2198 int show_vnc_port = 0;
2199 #endif
2200 int defconfig = 1;
2201 const char *log_mask = NULL;
2202 const char *log_file = NULL;
2203 GMemVTable mem_trace = {
2204 .malloc = malloc_and_trace,
2205 .realloc = realloc_and_trace,
2206 .free = free_and_trace,
2208 const char *trace_events = NULL;
2209 const char *trace_file = NULL;
2211 atexit(qemu_run_exit_notifiers);
2212 error_set_progname(argv[0]);
2214 g_mem_set_vtable(&mem_trace);
2215 if (!g_thread_supported()) {
2216 #if !GLIB_CHECK_VERSION(2, 31, 0)
2217 g_thread_init(NULL);
2218 #else
2219 fprintf(stderr, "glib threading failed to initialize.\n");
2220 exit(1);
2221 #endif
2224 runstate_init();
2226 init_clocks();
2227 rtc_clock = host_clock;
2229 qemu_cache_utils_init(envp);
2231 QLIST_INIT (&vm_change_state_head);
2232 os_setup_early_signal_handling();
2234 module_call_init(MODULE_INIT_MACHINE);
2235 machine = find_default_machine();
2236 cpu_model = NULL;
2237 initrd_filename = NULL;
2238 ram_size = 0;
2239 snapshot = 0;
2240 kernel_filename = NULL;
2241 kernel_cmdline = "";
2242 cyls = heads = secs = 0;
2243 translation = BIOS_ATA_TRANSLATION_AUTO;
2245 for (i = 0; i < MAX_NODES; i++) {
2246 node_mem[i] = 0;
2247 node_cpumask[i] = 0;
2250 nb_numa_nodes = 0;
2251 nb_nics = 0;
2253 autostart= 1;
2255 /* first pass of option parsing */
2256 optind = 1;
2257 while (optind < argc) {
2258 if (argv[optind][0] != '-') {
2259 /* disk image */
2260 optind++;
2261 continue;
2262 } else {
2263 const QEMUOption *popt;
2265 popt = lookup_opt(argc, argv, &optarg, &optind);
2266 switch (popt->index) {
2267 case QEMU_OPTION_nodefconfig:
2268 defconfig=0;
2269 break;
2274 if (defconfig) {
2275 int ret;
2277 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2278 if (ret < 0 && ret != -ENOENT) {
2279 exit(1);
2282 ret = qemu_read_config_file(arch_config_name);
2283 if (ret < 0 && ret != -ENOENT) {
2284 exit(1);
2287 cpudef_init();
2289 /* second pass of option parsing */
2290 optind = 1;
2291 for(;;) {
2292 if (optind >= argc)
2293 break;
2294 if (argv[optind][0] != '-') {
2295 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2296 } else {
2297 const QEMUOption *popt;
2299 popt = lookup_opt(argc, argv, &optarg, &optind);
2300 if (!(popt->arch_mask & arch_type)) {
2301 printf("Option %s not supported for this target\n", popt->name);
2302 exit(1);
2304 switch(popt->index) {
2305 case QEMU_OPTION_M:
2306 machine = machine_parse(optarg);
2307 break;
2308 case QEMU_OPTION_cpu:
2309 /* hw initialization will check this */
2310 if (*optarg == '?') {
2311 list_cpus(stdout, &fprintf, optarg);
2312 exit(0);
2313 } else {
2314 cpu_model = optarg;
2316 break;
2317 case QEMU_OPTION_initrd:
2318 initrd_filename = optarg;
2319 break;
2320 case QEMU_OPTION_hda:
2322 char buf[256];
2323 if (cyls == 0)
2324 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2325 else
2326 snprintf(buf, sizeof(buf),
2327 "%s,cyls=%d,heads=%d,secs=%d%s",
2328 HD_OPTS , cyls, heads, secs,
2329 translation == BIOS_ATA_TRANSLATION_LBA ?
2330 ",trans=lba" :
2331 translation == BIOS_ATA_TRANSLATION_NONE ?
2332 ",trans=none" : "");
2333 drive_add(IF_DEFAULT, 0, optarg, buf);
2334 break;
2336 case QEMU_OPTION_hdb:
2337 case QEMU_OPTION_hdc:
2338 case QEMU_OPTION_hdd:
2339 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2340 HD_OPTS);
2341 break;
2342 case QEMU_OPTION_drive:
2343 if (drive_def(optarg) == NULL) {
2344 exit(1);
2346 break;
2347 case QEMU_OPTION_set:
2348 if (qemu_set_option(optarg) != 0)
2349 exit(1);
2350 break;
2351 case QEMU_OPTION_global:
2352 if (qemu_global_option(optarg) != 0)
2353 exit(1);
2354 break;
2355 case QEMU_OPTION_mtdblock:
2356 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2357 break;
2358 case QEMU_OPTION_sd:
2359 drive_add(IF_SD, 0, optarg, SD_OPTS);
2360 break;
2361 case QEMU_OPTION_pflash:
2362 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2363 break;
2364 case QEMU_OPTION_snapshot:
2365 snapshot = 1;
2366 break;
2367 case QEMU_OPTION_hdachs:
2369 const char *p;
2370 p = optarg;
2371 cyls = strtol(p, (char **)&p, 0);
2372 if (cyls < 1 || cyls > 16383)
2373 goto chs_fail;
2374 if (*p != ',')
2375 goto chs_fail;
2376 p++;
2377 heads = strtol(p, (char **)&p, 0);
2378 if (heads < 1 || heads > 16)
2379 goto chs_fail;
2380 if (*p != ',')
2381 goto chs_fail;
2382 p++;
2383 secs = strtol(p, (char **)&p, 0);
2384 if (secs < 1 || secs > 63)
2385 goto chs_fail;
2386 if (*p == ',') {
2387 p++;
2388 if (!strcmp(p, "none"))
2389 translation = BIOS_ATA_TRANSLATION_NONE;
2390 else if (!strcmp(p, "lba"))
2391 translation = BIOS_ATA_TRANSLATION_LBA;
2392 else if (!strcmp(p, "auto"))
2393 translation = BIOS_ATA_TRANSLATION_AUTO;
2394 else
2395 goto chs_fail;
2396 } else if (*p != '\0') {
2397 chs_fail:
2398 fprintf(stderr, "qemu: invalid physical CHS format\n");
2399 exit(1);
2401 if (hda_opts != NULL) {
2402 char num[16];
2403 snprintf(num, sizeof(num), "%d", cyls);
2404 qemu_opt_set(hda_opts, "cyls", num);
2405 snprintf(num, sizeof(num), "%d", heads);
2406 qemu_opt_set(hda_opts, "heads", num);
2407 snprintf(num, sizeof(num), "%d", secs);
2408 qemu_opt_set(hda_opts, "secs", num);
2409 if (translation == BIOS_ATA_TRANSLATION_LBA)
2410 qemu_opt_set(hda_opts, "trans", "lba");
2411 if (translation == BIOS_ATA_TRANSLATION_NONE)
2412 qemu_opt_set(hda_opts, "trans", "none");
2415 break;
2416 case QEMU_OPTION_numa:
2417 if (nb_numa_nodes >= MAX_NODES) {
2418 fprintf(stderr, "qemu: too many NUMA nodes\n");
2419 exit(1);
2421 numa_add(optarg);
2422 break;
2423 case QEMU_OPTION_display:
2424 display_type = select_display(optarg);
2425 break;
2426 case QEMU_OPTION_nographic:
2427 display_type = DT_NOGRAPHIC;
2428 break;
2429 case QEMU_OPTION_curses:
2430 #ifdef CONFIG_CURSES
2431 display_type = DT_CURSES;
2432 #else
2433 fprintf(stderr, "Curses support is disabled\n");
2434 exit(1);
2435 #endif
2436 break;
2437 case QEMU_OPTION_portrait:
2438 graphic_rotate = 90;
2439 break;
2440 case QEMU_OPTION_rotate:
2441 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2442 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2443 graphic_rotate != 180 && graphic_rotate != 270) {
2444 fprintf(stderr,
2445 "qemu: only 90, 180, 270 deg rotation is available\n");
2446 exit(1);
2448 break;
2449 case QEMU_OPTION_kernel:
2450 kernel_filename = optarg;
2451 break;
2452 case QEMU_OPTION_append:
2453 kernel_cmdline = optarg;
2454 break;
2455 case QEMU_OPTION_cdrom:
2456 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2457 break;
2458 case QEMU_OPTION_boot:
2460 static const char * const params[] = {
2461 "order", "once", "menu",
2462 "splash", "splash-time", NULL
2464 char buf[sizeof(boot_devices)];
2465 char *standard_boot_devices;
2466 int legacy = 0;
2468 if (!strchr(optarg, '=')) {
2469 legacy = 1;
2470 pstrcpy(buf, sizeof(buf), optarg);
2471 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2472 fprintf(stderr,
2473 "qemu: unknown boot parameter '%s' in '%s'\n",
2474 buf, optarg);
2475 exit(1);
2478 if (legacy ||
2479 get_param_value(buf, sizeof(buf), "order", optarg)) {
2480 validate_bootdevices(buf);
2481 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2483 if (!legacy) {
2484 if (get_param_value(buf, sizeof(buf),
2485 "once", optarg)) {
2486 validate_bootdevices(buf);
2487 standard_boot_devices = g_strdup(boot_devices);
2488 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2489 qemu_register_reset(restore_boot_devices,
2490 standard_boot_devices);
2492 if (get_param_value(buf, sizeof(buf),
2493 "menu", optarg)) {
2494 if (!strcmp(buf, "on")) {
2495 boot_menu = 1;
2496 } else if (!strcmp(buf, "off")) {
2497 boot_menu = 0;
2498 } else {
2499 fprintf(stderr,
2500 "qemu: invalid option value '%s'\n",
2501 buf);
2502 exit(1);
2505 qemu_opts_parse(qemu_find_opts("boot-opts"),
2506 optarg, 0);
2509 break;
2510 case QEMU_OPTION_fda:
2511 case QEMU_OPTION_fdb:
2512 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2513 optarg, FD_OPTS);
2514 break;
2515 case QEMU_OPTION_no_fd_bootchk:
2516 fd_bootchk = 0;
2517 break;
2518 case QEMU_OPTION_netdev:
2519 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2520 exit(1);
2522 break;
2523 case QEMU_OPTION_net:
2524 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2525 exit(1);
2527 break;
2528 #ifdef CONFIG_SLIRP
2529 case QEMU_OPTION_tftp:
2530 legacy_tftp_prefix = optarg;
2531 break;
2532 case QEMU_OPTION_bootp:
2533 legacy_bootp_filename = optarg;
2534 break;
2535 case QEMU_OPTION_redir:
2536 if (net_slirp_redir(optarg) < 0)
2537 exit(1);
2538 break;
2539 #endif
2540 case QEMU_OPTION_bt:
2541 add_device_config(DEV_BT, optarg);
2542 break;
2543 case QEMU_OPTION_audio_help:
2544 if (!(audio_available())) {
2545 printf("Option %s not supported for this target\n", popt->name);
2546 exit(1);
2548 AUD_help ();
2549 exit (0);
2550 break;
2551 case QEMU_OPTION_soundhw:
2552 if (!(audio_available())) {
2553 printf("Option %s not supported for this target\n", popt->name);
2554 exit(1);
2556 select_soundhw (optarg);
2557 break;
2558 case QEMU_OPTION_h:
2559 help(0);
2560 break;
2561 case QEMU_OPTION_version:
2562 version();
2563 exit(0);
2564 break;
2565 case QEMU_OPTION_m: {
2566 int64_t value;
2567 char *end;
2569 value = strtosz(optarg, &end);
2570 if (value < 0 || *end) {
2571 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2572 exit(1);
2575 if (value != (uint64_t)(ram_addr_t)value) {
2576 fprintf(stderr, "qemu: ram size too large\n");
2577 exit(1);
2579 ram_size = value;
2580 break;
2582 case QEMU_OPTION_mempath:
2583 mem_path = optarg;
2584 break;
2585 #ifdef MAP_POPULATE
2586 case QEMU_OPTION_mem_prealloc:
2587 mem_prealloc = 1;
2588 break;
2589 #endif
2590 case QEMU_OPTION_d:
2591 log_mask = optarg;
2592 break;
2593 case QEMU_OPTION_D:
2594 log_file = optarg;
2595 break;
2596 case QEMU_OPTION_s:
2597 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2598 break;
2599 case QEMU_OPTION_gdb:
2600 gdbstub_dev = optarg;
2601 break;
2602 case QEMU_OPTION_L:
2603 data_dir = optarg;
2604 break;
2605 case QEMU_OPTION_bios:
2606 bios_name = optarg;
2607 break;
2608 case QEMU_OPTION_singlestep:
2609 singlestep = 1;
2610 break;
2611 case QEMU_OPTION_S:
2612 autostart = 0;
2613 break;
2614 case QEMU_OPTION_k:
2615 keyboard_layout = optarg;
2616 break;
2617 case QEMU_OPTION_localtime:
2618 rtc_utc = 0;
2619 break;
2620 case QEMU_OPTION_vga:
2621 vga_model = optarg;
2622 break;
2623 case QEMU_OPTION_g:
2625 const char *p;
2626 int w, h, depth;
2627 p = optarg;
2628 w = strtol(p, (char **)&p, 10);
2629 if (w <= 0) {
2630 graphic_error:
2631 fprintf(stderr, "qemu: invalid resolution or depth\n");
2632 exit(1);
2634 if (*p != 'x')
2635 goto graphic_error;
2636 p++;
2637 h = strtol(p, (char **)&p, 10);
2638 if (h <= 0)
2639 goto graphic_error;
2640 if (*p == 'x') {
2641 p++;
2642 depth = strtol(p, (char **)&p, 10);
2643 if (depth != 8 && depth != 15 && depth != 16 &&
2644 depth != 24 && depth != 32)
2645 goto graphic_error;
2646 } else if (*p == '\0') {
2647 depth = graphic_depth;
2648 } else {
2649 goto graphic_error;
2652 graphic_width = w;
2653 graphic_height = h;
2654 graphic_depth = depth;
2656 break;
2657 case QEMU_OPTION_echr:
2659 char *r;
2660 term_escape_char = strtol(optarg, &r, 0);
2661 if (r == optarg)
2662 printf("Bad argument to echr\n");
2663 break;
2665 case QEMU_OPTION_monitor:
2666 monitor_parse(optarg, "readline");
2667 default_monitor = 0;
2668 break;
2669 case QEMU_OPTION_qmp:
2670 monitor_parse(optarg, "control");
2671 default_monitor = 0;
2672 break;
2673 case QEMU_OPTION_mon:
2674 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2675 if (!opts) {
2676 exit(1);
2678 default_monitor = 0;
2679 break;
2680 case QEMU_OPTION_chardev:
2681 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2682 if (!opts) {
2683 exit(1);
2685 break;
2686 case QEMU_OPTION_fsdev:
2687 olist = qemu_find_opts("fsdev");
2688 if (!olist) {
2689 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2690 exit(1);
2692 opts = qemu_opts_parse(olist, optarg, 1);
2693 if (!opts) {
2694 fprintf(stderr, "parse error: %s\n", optarg);
2695 exit(1);
2697 break;
2698 case QEMU_OPTION_virtfs: {
2699 QemuOpts *fsdev;
2700 QemuOpts *device;
2701 const char *writeout, *sock_fd, *socket;
2703 olist = qemu_find_opts("virtfs");
2704 if (!olist) {
2705 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2706 exit(1);
2708 opts = qemu_opts_parse(olist, optarg, 1);
2709 if (!opts) {
2710 fprintf(stderr, "parse error: %s\n", optarg);
2711 exit(1);
2714 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2715 qemu_opt_get(opts, "mount_tag") == NULL) {
2716 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2717 exit(1);
2719 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2720 qemu_opt_get(opts, "mount_tag"), 1);
2721 if (!fsdev) {
2722 fprintf(stderr, "duplicate fsdev id: %s\n",
2723 qemu_opt_get(opts, "mount_tag"));
2724 exit(1);
2727 writeout = qemu_opt_get(opts, "writeout");
2728 if (writeout) {
2729 #ifdef CONFIG_SYNC_FILE_RANGE
2730 qemu_opt_set(fsdev, "writeout", writeout);
2731 #else
2732 fprintf(stderr, "writeout=immediate not supported on "
2733 "this platform\n");
2734 exit(1);
2735 #endif
2737 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2738 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2739 qemu_opt_set(fsdev, "security_model",
2740 qemu_opt_get(opts, "security_model"));
2741 socket = qemu_opt_get(opts, "socket");
2742 if (socket) {
2743 qemu_opt_set(fsdev, "socket", socket);
2745 sock_fd = qemu_opt_get(opts, "sock_fd");
2746 if (sock_fd) {
2747 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2750 qemu_opt_set_bool(fsdev, "readonly",
2751 qemu_opt_get_bool(opts, "readonly", 0));
2752 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2753 qemu_opt_set(device, "driver", "virtio-9p-pci");
2754 qemu_opt_set(device, "fsdev",
2755 qemu_opt_get(opts, "mount_tag"));
2756 qemu_opt_set(device, "mount_tag",
2757 qemu_opt_get(opts, "mount_tag"));
2758 break;
2760 case QEMU_OPTION_virtfs_synth: {
2761 QemuOpts *fsdev;
2762 QemuOpts *device;
2764 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2765 if (!fsdev) {
2766 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2767 exit(1);
2769 qemu_opt_set(fsdev, "fsdriver", "synth");
2771 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2772 qemu_opt_set(device, "driver", "virtio-9p-pci");
2773 qemu_opt_set(device, "fsdev", "v_synth");
2774 qemu_opt_set(device, "mount_tag", "v_synth");
2775 break;
2777 case QEMU_OPTION_serial:
2778 add_device_config(DEV_SERIAL, optarg);
2779 default_serial = 0;
2780 if (strncmp(optarg, "mon:", 4) == 0) {
2781 default_monitor = 0;
2783 break;
2784 case QEMU_OPTION_watchdog:
2785 if (watchdog) {
2786 fprintf(stderr,
2787 "qemu: only one watchdog option may be given\n");
2788 return 1;
2790 watchdog = optarg;
2791 break;
2792 case QEMU_OPTION_watchdog_action:
2793 if (select_watchdog_action(optarg) == -1) {
2794 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2795 exit(1);
2797 break;
2798 case QEMU_OPTION_virtiocon:
2799 add_device_config(DEV_VIRTCON, optarg);
2800 default_virtcon = 0;
2801 if (strncmp(optarg, "mon:", 4) == 0) {
2802 default_monitor = 0;
2804 break;
2805 case QEMU_OPTION_parallel:
2806 add_device_config(DEV_PARALLEL, optarg);
2807 default_parallel = 0;
2808 if (strncmp(optarg, "mon:", 4) == 0) {
2809 default_monitor = 0;
2811 break;
2812 case QEMU_OPTION_debugcon:
2813 add_device_config(DEV_DEBUGCON, optarg);
2814 break;
2815 case QEMU_OPTION_loadvm:
2816 loadvm = optarg;
2817 break;
2818 case QEMU_OPTION_full_screen:
2819 full_screen = 1;
2820 break;
2821 #ifdef CONFIG_SDL
2822 case QEMU_OPTION_no_frame:
2823 no_frame = 1;
2824 break;
2825 case QEMU_OPTION_alt_grab:
2826 alt_grab = 1;
2827 break;
2828 case QEMU_OPTION_ctrl_grab:
2829 ctrl_grab = 1;
2830 break;
2831 case QEMU_OPTION_no_quit:
2832 no_quit = 1;
2833 break;
2834 case QEMU_OPTION_sdl:
2835 display_type = DT_SDL;
2836 break;
2837 #else
2838 case QEMU_OPTION_no_frame:
2839 case QEMU_OPTION_alt_grab:
2840 case QEMU_OPTION_ctrl_grab:
2841 case QEMU_OPTION_no_quit:
2842 case QEMU_OPTION_sdl:
2843 fprintf(stderr, "SDL support is disabled\n");
2844 exit(1);
2845 #endif
2846 case QEMU_OPTION_pidfile:
2847 pid_file = optarg;
2848 break;
2849 case QEMU_OPTION_win2k_hack:
2850 win2k_install_hack = 1;
2851 break;
2852 case QEMU_OPTION_rtc_td_hack: {
2853 static GlobalProperty slew_lost_ticks[] = {
2855 .driver = "mc146818rtc",
2856 .property = "lost_tick_policy",
2857 .value = "slew",
2859 { /* end of list */ }
2862 qdev_prop_register_global_list(slew_lost_ticks);
2863 break;
2865 case QEMU_OPTION_acpitable:
2866 do_acpitable_option(optarg);
2867 break;
2868 case QEMU_OPTION_smbios:
2869 do_smbios_option(optarg);
2870 break;
2871 case QEMU_OPTION_enable_kvm:
2872 olist = qemu_find_opts("machine");
2873 qemu_opts_reset(olist);
2874 qemu_opts_parse(olist, "accel=kvm", 0);
2875 break;
2876 case QEMU_OPTION_machine:
2877 olist = qemu_find_opts("machine");
2878 qemu_opts_reset(olist);
2879 opts = qemu_opts_parse(olist, optarg, 1);
2880 if (!opts) {
2881 fprintf(stderr, "parse error: %s\n", optarg);
2882 exit(1);
2884 optarg = qemu_opt_get(opts, "type");
2885 if (optarg) {
2886 machine = machine_parse(optarg);
2888 break;
2889 case QEMU_OPTION_usb:
2890 usb_enabled = 1;
2891 break;
2892 case QEMU_OPTION_usbdevice:
2893 usb_enabled = 1;
2894 add_device_config(DEV_USB, optarg);
2895 break;
2896 case QEMU_OPTION_device:
2897 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2898 exit(1);
2900 break;
2901 case QEMU_OPTION_smp:
2902 smp_parse(optarg);
2903 if (smp_cpus < 1) {
2904 fprintf(stderr, "Invalid number of CPUs\n");
2905 exit(1);
2907 if (max_cpus < smp_cpus) {
2908 fprintf(stderr, "maxcpus must be equal to or greater than "
2909 "smp\n");
2910 exit(1);
2912 if (max_cpus > 255) {
2913 fprintf(stderr, "Unsupported number of maxcpus\n");
2914 exit(1);
2916 break;
2917 case QEMU_OPTION_vnc:
2918 #ifdef CONFIG_VNC
2919 display_remote++;
2920 vnc_display = optarg;
2921 #else
2922 fprintf(stderr, "VNC support is disabled\n");
2923 exit(1);
2924 #endif
2925 break;
2926 case QEMU_OPTION_no_acpi:
2927 acpi_enabled = 0;
2928 break;
2929 case QEMU_OPTION_no_hpet:
2930 no_hpet = 1;
2931 break;
2932 case QEMU_OPTION_balloon:
2933 if (balloon_parse(optarg) < 0) {
2934 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2935 exit(1);
2937 break;
2938 case QEMU_OPTION_no_reboot:
2939 no_reboot = 1;
2940 break;
2941 case QEMU_OPTION_no_shutdown:
2942 no_shutdown = 1;
2943 break;
2944 case QEMU_OPTION_show_cursor:
2945 cursor_hide = 0;
2946 break;
2947 case QEMU_OPTION_uuid:
2948 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2949 fprintf(stderr, "Fail to parse UUID string."
2950 " Wrong format.\n");
2951 exit(1);
2953 break;
2954 case QEMU_OPTION_option_rom:
2955 if (nb_option_roms >= MAX_OPTION_ROMS) {
2956 fprintf(stderr, "Too many option ROMs\n");
2957 exit(1);
2959 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2960 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2961 option_rom[nb_option_roms].bootindex =
2962 qemu_opt_get_number(opts, "bootindex", -1);
2963 if (!option_rom[nb_option_roms].name) {
2964 fprintf(stderr, "Option ROM file is not specified\n");
2965 exit(1);
2967 nb_option_roms++;
2968 break;
2969 case QEMU_OPTION_semihosting:
2970 semihosting_enabled = 1;
2971 break;
2972 case QEMU_OPTION_name:
2973 qemu_name = g_strdup(optarg);
2975 char *p = strchr(qemu_name, ',');
2976 if (p != NULL) {
2977 *p++ = 0;
2978 if (strncmp(p, "process=", 8)) {
2979 fprintf(stderr, "Unknown subargument %s to -name\n", p);
2980 exit(1);
2982 p += 8;
2983 os_set_proc_name(p);
2986 break;
2987 case QEMU_OPTION_prom_env:
2988 if (nb_prom_envs >= MAX_PROM_ENVS) {
2989 fprintf(stderr, "Too many prom variables\n");
2990 exit(1);
2992 prom_envs[nb_prom_envs] = optarg;
2993 nb_prom_envs++;
2994 break;
2995 case QEMU_OPTION_old_param:
2996 old_param = 1;
2997 break;
2998 case QEMU_OPTION_clock:
2999 configure_alarms(optarg);
3000 break;
3001 case QEMU_OPTION_startdate:
3002 configure_rtc_date_offset(optarg, 1);
3003 break;
3004 case QEMU_OPTION_rtc:
3005 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3006 if (!opts) {
3007 exit(1);
3009 configure_rtc(opts);
3010 break;
3011 case QEMU_OPTION_tb_size:
3012 tcg_tb_size = strtol(optarg, NULL, 0);
3013 if (tcg_tb_size < 0) {
3014 tcg_tb_size = 0;
3016 break;
3017 case QEMU_OPTION_icount:
3018 icount_option = optarg;
3019 break;
3020 case QEMU_OPTION_incoming:
3021 incoming = optarg;
3022 break;
3023 case QEMU_OPTION_nodefaults:
3024 default_serial = 0;
3025 default_parallel = 0;
3026 default_virtcon = 0;
3027 default_monitor = 0;
3028 default_net = 0;
3029 default_floppy = 0;
3030 default_cdrom = 0;
3031 default_sdcard = 0;
3032 vga_model = "none";
3033 break;
3034 case QEMU_OPTION_xen_domid:
3035 if (!(xen_available())) {
3036 printf("Option %s not supported for this target\n", popt->name);
3037 exit(1);
3039 xen_domid = atoi(optarg);
3040 break;
3041 case QEMU_OPTION_xen_create:
3042 if (!(xen_available())) {
3043 printf("Option %s not supported for this target\n", popt->name);
3044 exit(1);
3046 xen_mode = XEN_CREATE;
3047 break;
3048 case QEMU_OPTION_xen_attach:
3049 if (!(xen_available())) {
3050 printf("Option %s not supported for this target\n", popt->name);
3051 exit(1);
3053 xen_mode = XEN_ATTACH;
3054 break;
3055 case QEMU_OPTION_trace:
3057 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3058 if (!opts) {
3059 exit(1);
3061 trace_events = qemu_opt_get(opts, "events");
3062 trace_file = qemu_opt_get(opts, "file");
3063 break;
3065 case QEMU_OPTION_readconfig:
3067 int ret = qemu_read_config_file(optarg);
3068 if (ret < 0) {
3069 fprintf(stderr, "read config %s: %s\n", optarg,
3070 strerror(-ret));
3071 exit(1);
3073 break;
3075 case QEMU_OPTION_spice:
3076 olist = qemu_find_opts("spice");
3077 if (!olist) {
3078 fprintf(stderr, "spice is not supported by this qemu build.\n");
3079 exit(1);
3081 opts = qemu_opts_parse(olist, optarg, 0);
3082 if (!opts) {
3083 fprintf(stderr, "parse error: %s\n", optarg);
3084 exit(1);
3086 break;
3087 case QEMU_OPTION_writeconfig:
3089 FILE *fp;
3090 if (strcmp(optarg, "-") == 0) {
3091 fp = stdout;
3092 } else {
3093 fp = fopen(optarg, "w");
3094 if (fp == NULL) {
3095 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3096 exit(1);
3099 qemu_config_write(fp);
3100 fclose(fp);
3101 break;
3103 default:
3104 os_parse_cmd_args(popt->index, optarg);
3108 loc_set_none();
3110 /* Open the logfile at this point, if necessary. We can't open the logfile
3111 * when encountering either of the logging options (-d or -D) because the
3112 * other one may be encountered later on the command line, changing the
3113 * location or level of logging.
3115 if (log_mask) {
3116 if (log_file) {
3117 set_cpu_log_filename(log_file);
3119 set_cpu_log(log_mask);
3122 if (!trace_backend_init(trace_events, trace_file)) {
3123 exit(1);
3126 /* If no data_dir is specified then try to find it relative to the
3127 executable path. */
3128 if (!data_dir) {
3129 data_dir = os_find_datadir(argv[0]);
3131 /* If all else fails use the install path specified when building. */
3132 if (!data_dir) {
3133 data_dir = CONFIG_QEMU_DATADIR;
3136 if (machine == NULL) {
3137 fprintf(stderr, "No machine found.\n");
3138 exit(1);
3142 * Default to max_cpus = smp_cpus, in case the user doesn't
3143 * specify a max_cpus value.
3145 if (!max_cpus)
3146 max_cpus = smp_cpus;
3148 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3149 if (smp_cpus > machine->max_cpus) {
3150 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3151 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3152 machine->max_cpus);
3153 exit(1);
3157 * Get the default machine options from the machine if it is not already
3158 * specified either by the configuration file or by the command line.
3160 if (machine->default_machine_opts) {
3161 qemu_opts_set_defaults(qemu_find_opts("machine"),
3162 machine->default_machine_opts, 0);
3165 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3166 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3168 if (machine->no_serial) {
3169 default_serial = 0;
3171 if (machine->no_parallel) {
3172 default_parallel = 0;
3174 if (!machine->use_virtcon) {
3175 default_virtcon = 0;
3177 if (machine->no_floppy) {
3178 default_floppy = 0;
3180 if (machine->no_cdrom) {
3181 default_cdrom = 0;
3183 if (machine->no_sdcard) {
3184 default_sdcard = 0;
3187 if (display_type == DT_NOGRAPHIC) {
3188 if (default_parallel)
3189 add_device_config(DEV_PARALLEL, "null");
3190 if (default_serial && default_monitor) {
3191 add_device_config(DEV_SERIAL, "mon:stdio");
3192 } else if (default_virtcon && default_monitor) {
3193 add_device_config(DEV_VIRTCON, "mon:stdio");
3194 } else {
3195 if (default_serial)
3196 add_device_config(DEV_SERIAL, "stdio");
3197 if (default_virtcon)
3198 add_device_config(DEV_VIRTCON, "stdio");
3199 if (default_monitor)
3200 monitor_parse("stdio", "readline");
3202 } else {
3203 if (default_serial)
3204 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3205 if (default_parallel)
3206 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3207 if (default_monitor)
3208 monitor_parse("vc:80Cx24C", "readline");
3209 if (default_virtcon)
3210 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3213 socket_init();
3215 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3216 exit(1);
3217 #ifdef CONFIG_VIRTFS
3218 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3219 exit(1);
3221 #endif
3223 os_daemonize();
3225 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3226 os_pidfile_error();
3227 exit(1);
3230 /* init the memory */
3231 if (ram_size == 0) {
3232 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3235 configure_accelerator();
3237 qemu_init_cpu_loop();
3238 if (qemu_init_main_loop()) {
3239 fprintf(stderr, "qemu_init_main_loop failed\n");
3240 exit(1);
3242 linux_boot = (kernel_filename != NULL);
3244 if (!linux_boot && *kernel_cmdline != '\0') {
3245 fprintf(stderr, "-append only allowed with -kernel option\n");
3246 exit(1);
3249 if (!linux_boot && initrd_filename != NULL) {
3250 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3251 exit(1);
3254 os_set_line_buffering();
3256 if (init_timer_alarm() < 0) {
3257 fprintf(stderr, "could not initialize alarm timer\n");
3258 exit(1);
3261 if (icount_option && (kvm_enabled() || xen_enabled())) {
3262 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3263 exit(1);
3265 configure_icount(icount_option);
3267 if (net_init_clients() < 0) {
3268 exit(1);
3271 /* init the bluetooth world */
3272 if (foreach_device_config(DEV_BT, bt_parse))
3273 exit(1);
3275 if (!xen_enabled()) {
3276 /* On 32-bit hosts, QEMU is limited by virtual address space */
3277 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3278 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3279 exit(1);
3283 cpu_exec_init_all();
3285 bdrv_init_with_whitelist();
3287 blk_mig_init();
3289 /* open the virtual block devices */
3290 if (snapshot)
3291 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3292 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3293 exit(1);
3295 default_drive(default_cdrom, snapshot, machine->use_scsi,
3296 IF_DEFAULT, 2, CDROM_OPTS);
3297 default_drive(default_floppy, snapshot, machine->use_scsi,
3298 IF_FLOPPY, 0, FD_OPTS);
3299 default_drive(default_sdcard, snapshot, machine->use_scsi,
3300 IF_SD, 0, SD_OPTS);
3302 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3303 ram_load, NULL);
3305 if (nb_numa_nodes > 0) {
3306 int i;
3308 if (nb_numa_nodes > MAX_NODES) {
3309 nb_numa_nodes = MAX_NODES;
3312 /* If no memory size if given for any node, assume the default case
3313 * and distribute the available memory equally across all nodes
3315 for (i = 0; i < nb_numa_nodes; i++) {
3316 if (node_mem[i] != 0)
3317 break;
3319 if (i == nb_numa_nodes) {
3320 uint64_t usedmem = 0;
3322 /* On Linux, the each node's border has to be 8MB aligned,
3323 * the final node gets the rest.
3325 for (i = 0; i < nb_numa_nodes - 1; i++) {
3326 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3327 usedmem += node_mem[i];
3329 node_mem[i] = ram_size - usedmem;
3332 for (i = 0; i < nb_numa_nodes; i++) {
3333 if (node_cpumask[i] != 0)
3334 break;
3336 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3337 * must cope with this anyway, because there are BIOSes out there in
3338 * real machines which also use this scheme.
3340 if (i == nb_numa_nodes) {
3341 for (i = 0; i < max_cpus; i++) {
3342 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3347 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3348 exit(1);
3351 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3352 exit(1);
3353 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3354 exit(1);
3355 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3356 exit(1);
3357 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3358 exit(1);
3360 module_call_init(MODULE_INIT_DEVICE);
3362 /* must be after qdev registration but before machine init */
3363 if (vga_model) {
3364 select_vgahw(vga_model);
3365 } else if (cirrus_vga_available()) {
3366 select_vgahw("cirrus");
3367 } else {
3368 select_vgahw("none");
3371 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3372 exit(0);
3374 if (watchdog) {
3375 i = select_watchdog(watchdog);
3376 if (i > 0)
3377 exit (i == 1 ? 1 : 0);
3380 if (machine->compat_props) {
3381 qdev_prop_register_global_list(machine->compat_props);
3383 qemu_add_globals();
3385 qdev_machine_init();
3387 machine->init(ram_size, boot_devices,
3388 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3390 cpu_synchronize_all_post_init();
3392 set_numa_modes();
3394 current_machine = machine;
3396 /* init USB devices */
3397 if (usb_enabled) {
3398 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3399 exit(1);
3402 /* init generic devices */
3403 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3404 exit(1);
3406 net_check_clients();
3408 /* just use the first displaystate for the moment */
3409 ds = get_displaystate();
3411 if (using_spice)
3412 display_remote++;
3413 if (display_type == DT_DEFAULT && !display_remote) {
3414 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3415 display_type = DT_SDL;
3416 #elif defined(CONFIG_VNC)
3417 vnc_display = "localhost:0,to=99";
3418 show_vnc_port = 1;
3419 #else
3420 display_type = DT_NONE;
3421 #endif
3425 /* init local displays */
3426 switch (display_type) {
3427 case DT_NOGRAPHIC:
3428 break;
3429 #if defined(CONFIG_CURSES)
3430 case DT_CURSES:
3431 curses_display_init(ds, full_screen);
3432 break;
3433 #endif
3434 #if defined(CONFIG_SDL)
3435 case DT_SDL:
3436 sdl_display_init(ds, full_screen, no_frame);
3437 break;
3438 #elif defined(CONFIG_COCOA)
3439 case DT_SDL:
3440 cocoa_display_init(ds, full_screen);
3441 break;
3442 #endif
3443 default:
3444 break;
3447 /* must be after terminal init, SDL library changes signal handlers */
3448 os_setup_signal_handling();
3450 #ifdef CONFIG_VNC
3451 /* init remote displays */
3452 if (vnc_display) {
3453 vnc_display_init(ds);
3454 if (vnc_display_open(ds, vnc_display) < 0)
3455 exit(1);
3457 if (show_vnc_port) {
3458 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3461 #endif
3462 #ifdef CONFIG_SPICE
3463 if (using_spice && !qxl_enabled) {
3464 qemu_spice_display_init(ds);
3466 #endif
3468 /* display setup */
3469 dpy_resize(ds);
3470 dcl = ds->listeners;
3471 while (dcl != NULL) {
3472 if (dcl->dpy_refresh != NULL) {
3473 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3474 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3475 break;
3477 dcl = dcl->next;
3479 text_consoles_set_display(ds);
3481 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3482 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3483 gdbstub_dev);
3484 exit(1);
3487 qdev_machine_creation_done();
3489 if (rom_load_all() != 0) {
3490 fprintf(stderr, "rom loading failed\n");
3491 exit(1);
3494 /* TODO: once all bus devices are qdevified, this should be done
3495 * when bus is created by qdev.c */
3496 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3497 qemu_run_machine_init_done_notifiers();
3499 qemu_system_reset(VMRESET_SILENT);
3500 if (loadvm) {
3501 if (load_vmstate(loadvm) < 0) {
3502 autostart = 0;
3506 if (incoming) {
3507 runstate_set(RUN_STATE_INMIGRATE);
3508 int ret = qemu_start_incoming_migration(incoming);
3509 if (ret < 0) {
3510 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3511 incoming, ret);
3512 exit(ret);
3514 } else if (autostart) {
3515 vm_start();
3518 os_setup_post();
3520 resume_all_vcpus();
3521 main_loop();
3522 bdrv_close_all();
3523 pause_all_vcpus();
3524 net_cleanup();
3525 res_free();
3527 return 0;