build: adapt qom/Makefile and move it to Makefile.objs
[qemu/ar7.git] / vl.c
blob148542617dab9203f71db0cb2349e8a4ca91bb7d
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
155 #include "qtest.h"
157 #include "disas.h"
159 #include "qemu_socket.h"
161 #include "slirp/libslirp.h"
163 #include "trace.h"
164 #include "trace/control.h"
165 #include "qemu-queue.h"
166 #include "cpus.h"
167 #include "arch_init.h"
169 #include "ui/qemu-spice.h"
171 //#define DEBUG_NET
172 //#define DEBUG_SLIRP
174 #define DEFAULT_RAM_SIZE 128
176 #define MAX_VIRTIO_CONSOLES 1
178 static const char *data_dir;
179 const char *bios_name = NULL;
180 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
181 DisplayType display_type = DT_DEFAULT;
182 int display_remote = 0;
183 const char* keyboard_layout = NULL;
184 ram_addr_t ram_size;
185 const char *mem_path = NULL;
186 #ifdef MAP_POPULATE
187 int mem_prealloc = 0; /* force preallocation of physical target memory */
188 #endif
189 int nb_nics;
190 NICInfo nd_table[MAX_NICS];
191 int autostart;
192 static int rtc_utc = 1;
193 static int rtc_date_offset = -1; /* -1 means no change */
194 QEMUClock *rtc_clock;
195 int vga_interface_type = VGA_NONE;
196 static int full_screen = 0;
197 #ifdef CONFIG_SDL
198 static int no_frame = 0;
199 #endif
200 int no_quit = 0;
201 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
202 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
203 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
204 int win2k_install_hack = 0;
205 int usb_enabled = 0;
206 int singlestep = 0;
207 int smp_cpus = 1;
208 int max_cpus = 0;
209 int smp_cores = 1;
210 int smp_threads = 1;
211 #ifdef CONFIG_VNC
212 const char *vnc_display;
213 #endif
214 int acpi_enabled = 1;
215 int no_hpet = 0;
216 int fd_bootchk = 1;
217 int no_reboot = 0;
218 int no_shutdown = 0;
219 int cursor_hide = 1;
220 int graphic_rotate = 0;
221 const char *watchdog;
222 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
223 int nb_option_roms;
224 int semihosting_enabled = 0;
225 int old_param = 0;
226 const char *qemu_name;
227 int alt_grab = 0;
228 int ctrl_grab = 0;
229 unsigned int nb_prom_envs = 0;
230 const char *prom_envs[MAX_PROM_ENVS];
231 int boot_menu;
232 uint8_t *boot_splash_filedata;
233 int boot_splash_filedata_size;
234 uint8_t qemu_extra_params_fw[2];
236 typedef struct FWBootEntry FWBootEntry;
238 struct FWBootEntry {
239 QTAILQ_ENTRY(FWBootEntry) link;
240 int32_t bootindex;
241 DeviceState *dev;
242 char *suffix;
245 QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
247 int nb_numa_nodes;
248 uint64_t node_mem[MAX_NODES];
249 uint64_t node_cpumask[MAX_NODES];
251 uint8_t qemu_uuid[16];
253 static QEMUBootSetHandler *boot_set_handler;
254 static void *boot_set_opaque;
256 static NotifierList exit_notifiers =
257 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
259 static NotifierList machine_init_done_notifiers =
260 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
262 static int tcg_allowed = 1;
263 int kvm_allowed = 0;
264 int xen_allowed = 0;
265 uint32_t xen_domid;
266 enum xen_mode xen_mode = XEN_EMULATE;
267 static int tcg_tb_size;
269 static int default_serial = 1;
270 static int default_parallel = 1;
271 static int default_virtcon = 1;
272 static int default_monitor = 1;
273 static int default_floppy = 1;
274 static int default_cdrom = 1;
275 static int default_sdcard = 1;
276 static int default_vga = 1;
278 static struct {
279 const char *driver;
280 int *flag;
281 } default_list[] = {
282 { .driver = "isa-serial", .flag = &default_serial },
283 { .driver = "isa-parallel", .flag = &default_parallel },
284 { .driver = "isa-fdc", .flag = &default_floppy },
285 { .driver = "ide-cd", .flag = &default_cdrom },
286 { .driver = "ide-hd", .flag = &default_cdrom },
287 { .driver = "ide-drive", .flag = &default_cdrom },
288 { .driver = "scsi-cd", .flag = &default_cdrom },
289 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
290 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
291 { .driver = "virtio-serial", .flag = &default_virtcon },
292 { .driver = "VGA", .flag = &default_vga },
293 { .driver = "isa-vga", .flag = &default_vga },
294 { .driver = "cirrus-vga", .flag = &default_vga },
295 { .driver = "isa-cirrus-vga", .flag = &default_vga },
296 { .driver = "vmware-svga", .flag = &default_vga },
297 { .driver = "qxl-vga", .flag = &default_vga },
300 static void res_free(void)
302 if (boot_splash_filedata != NULL) {
303 g_free(boot_splash_filedata);
304 boot_splash_filedata = NULL;
308 static int default_driver_check(QemuOpts *opts, void *opaque)
310 const char *driver = qemu_opt_get(opts, "driver");
311 int i;
313 if (!driver)
314 return 0;
315 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
316 if (strcmp(default_list[i].driver, driver) != 0)
317 continue;
318 *(default_list[i].flag) = 0;
320 return 0;
323 /***********************************************************/
324 /* QEMU state */
326 static RunState current_run_state = RUN_STATE_PRELAUNCH;
328 typedef struct {
329 RunState from;
330 RunState to;
331 } RunStateTransition;
333 static const RunStateTransition runstate_transitions_def[] = {
334 /* from -> to */
335 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
337 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
338 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
340 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
341 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
343 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
344 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
346 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
347 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
349 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
350 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
352 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
353 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
354 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
356 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
357 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
359 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
361 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
362 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
363 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
364 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
365 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
366 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
367 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
368 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
369 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
371 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
373 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
374 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
376 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
377 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
378 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
379 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
381 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
382 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
384 { RUN_STATE_MAX, RUN_STATE_MAX },
387 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
389 bool runstate_check(RunState state)
391 return current_run_state == state;
394 void runstate_init(void)
396 const RunStateTransition *p;
398 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
400 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
401 runstate_valid_transitions[p->from][p->to] = true;
405 /* This function will abort() on invalid state transitions */
406 void runstate_set(RunState new_state)
408 assert(new_state < RUN_STATE_MAX);
410 if (!runstate_valid_transitions[current_run_state][new_state]) {
411 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
412 RunState_lookup[current_run_state],
413 RunState_lookup[new_state]);
414 abort();
417 current_run_state = new_state;
420 int runstate_is_running(void)
422 return runstate_check(RUN_STATE_RUNNING);
425 StatusInfo *qmp_query_status(Error **errp)
427 StatusInfo *info = g_malloc0(sizeof(*info));
429 info->running = runstate_is_running();
430 info->singlestep = singlestep;
431 info->status = current_run_state;
433 return info;
436 /***********************************************************/
437 /* real time host monotonic timer */
439 /***********************************************************/
440 /* host time/date access */
441 void qemu_get_timedate(struct tm *tm, int offset)
443 time_t ti;
444 struct tm *ret;
446 time(&ti);
447 ti += offset;
448 if (rtc_date_offset == -1) {
449 if (rtc_utc)
450 ret = gmtime(&ti);
451 else
452 ret = localtime(&ti);
453 } else {
454 ti -= rtc_date_offset;
455 ret = gmtime(&ti);
458 memcpy(tm, ret, sizeof(struct tm));
461 int qemu_timedate_diff(struct tm *tm)
463 time_t seconds;
465 if (rtc_date_offset == -1)
466 if (rtc_utc)
467 seconds = mktimegm(tm);
468 else {
469 struct tm tmp = *tm;
470 tmp.tm_isdst = -1; /* use timezone to figure it out */
471 seconds = mktime(&tmp);
473 else
474 seconds = mktimegm(tm) + rtc_date_offset;
476 return seconds - time(NULL);
479 void rtc_change_mon_event(struct tm *tm)
481 QObject *data;
483 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
484 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
485 qobject_decref(data);
488 static void configure_rtc_date_offset(const char *startdate, int legacy)
490 time_t rtc_start_date;
491 struct tm tm;
493 if (!strcmp(startdate, "now") && legacy) {
494 rtc_date_offset = -1;
495 } else {
496 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
497 &tm.tm_year,
498 &tm.tm_mon,
499 &tm.tm_mday,
500 &tm.tm_hour,
501 &tm.tm_min,
502 &tm.tm_sec) == 6) {
503 /* OK */
504 } else if (sscanf(startdate, "%d-%d-%d",
505 &tm.tm_year,
506 &tm.tm_mon,
507 &tm.tm_mday) == 3) {
508 tm.tm_hour = 0;
509 tm.tm_min = 0;
510 tm.tm_sec = 0;
511 } else {
512 goto date_fail;
514 tm.tm_year -= 1900;
515 tm.tm_mon--;
516 rtc_start_date = mktimegm(&tm);
517 if (rtc_start_date == -1) {
518 date_fail:
519 fprintf(stderr, "Invalid date format. Valid formats are:\n"
520 "'2006-06-17T16:01:21' or '2006-06-17'\n");
521 exit(1);
523 rtc_date_offset = time(NULL) - rtc_start_date;
527 static void configure_rtc(QemuOpts *opts)
529 const char *value;
531 value = qemu_opt_get(opts, "base");
532 if (value) {
533 if (!strcmp(value, "utc")) {
534 rtc_utc = 1;
535 } else if (!strcmp(value, "localtime")) {
536 rtc_utc = 0;
537 } else {
538 configure_rtc_date_offset(value, 0);
541 value = qemu_opt_get(opts, "clock");
542 if (value) {
543 if (!strcmp(value, "host")) {
544 rtc_clock = host_clock;
545 } else if (!strcmp(value, "rt")) {
546 rtc_clock = rt_clock;
547 } else if (!strcmp(value, "vm")) {
548 rtc_clock = vm_clock;
549 } else {
550 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
551 exit(1);
554 value = qemu_opt_get(opts, "driftfix");
555 if (value) {
556 if (!strcmp(value, "slew")) {
557 static GlobalProperty slew_lost_ticks[] = {
559 .driver = "mc146818rtc",
560 .property = "lost_tick_policy",
561 .value = "slew",
563 { /* end of list */ }
566 qdev_prop_register_global_list(slew_lost_ticks);
567 } else if (!strcmp(value, "none")) {
568 /* discard is default */
569 } else {
570 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
571 exit(1);
576 /***********************************************************/
577 /* Bluetooth support */
578 static int nb_hcis;
579 static int cur_hci;
580 static struct HCIInfo *hci_table[MAX_NICS];
582 static struct bt_vlan_s {
583 struct bt_scatternet_s net;
584 int id;
585 struct bt_vlan_s *next;
586 } *first_bt_vlan;
588 /* find or alloc a new bluetooth "VLAN" */
589 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
591 struct bt_vlan_s **pvlan, *vlan;
592 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
593 if (vlan->id == id)
594 return &vlan->net;
596 vlan = g_malloc0(sizeof(struct bt_vlan_s));
597 vlan->id = id;
598 pvlan = &first_bt_vlan;
599 while (*pvlan != NULL)
600 pvlan = &(*pvlan)->next;
601 *pvlan = vlan;
602 return &vlan->net;
605 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
609 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
611 return -ENOTSUP;
614 static struct HCIInfo null_hci = {
615 .cmd_send = null_hci_send,
616 .sco_send = null_hci_send,
617 .acl_send = null_hci_send,
618 .bdaddr_set = null_hci_addr_set,
621 struct HCIInfo *qemu_next_hci(void)
623 if (cur_hci == nb_hcis)
624 return &null_hci;
626 return hci_table[cur_hci++];
629 static struct HCIInfo *hci_init(const char *str)
631 char *endp;
632 struct bt_scatternet_s *vlan = 0;
634 if (!strcmp(str, "null"))
635 /* null */
636 return &null_hci;
637 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
638 /* host[:hciN] */
639 return bt_host_hci(str[4] ? str + 5 : "hci0");
640 else if (!strncmp(str, "hci", 3)) {
641 /* hci[,vlan=n] */
642 if (str[3]) {
643 if (!strncmp(str + 3, ",vlan=", 6)) {
644 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
645 if (*endp)
646 vlan = 0;
648 } else
649 vlan = qemu_find_bt_vlan(0);
650 if (vlan)
651 return bt_new_hci(vlan);
654 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
656 return 0;
659 static int bt_hci_parse(const char *str)
661 struct HCIInfo *hci;
662 bdaddr_t bdaddr;
664 if (nb_hcis >= MAX_NICS) {
665 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
666 return -1;
669 hci = hci_init(str);
670 if (!hci)
671 return -1;
673 bdaddr.b[0] = 0x52;
674 bdaddr.b[1] = 0x54;
675 bdaddr.b[2] = 0x00;
676 bdaddr.b[3] = 0x12;
677 bdaddr.b[4] = 0x34;
678 bdaddr.b[5] = 0x56 + nb_hcis;
679 hci->bdaddr_set(hci, bdaddr.b);
681 hci_table[nb_hcis++] = hci;
683 return 0;
686 static void bt_vhci_add(int vlan_id)
688 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
690 if (!vlan->slave)
691 fprintf(stderr, "qemu: warning: adding a VHCI to "
692 "an empty scatternet %i\n", vlan_id);
694 bt_vhci_init(bt_new_hci(vlan));
697 static struct bt_device_s *bt_device_add(const char *opt)
699 struct bt_scatternet_s *vlan;
700 int vlan_id = 0;
701 char *endp = strstr(opt, ",vlan=");
702 int len = (endp ? endp - opt : strlen(opt)) + 1;
703 char devname[10];
705 pstrcpy(devname, MIN(sizeof(devname), len), opt);
707 if (endp) {
708 vlan_id = strtol(endp + 6, &endp, 0);
709 if (*endp) {
710 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
711 return 0;
715 vlan = qemu_find_bt_vlan(vlan_id);
717 if (!vlan->slave)
718 fprintf(stderr, "qemu: warning: adding a slave device to "
719 "an empty scatternet %i\n", vlan_id);
721 if (!strcmp(devname, "keyboard"))
722 return bt_keyboard_init(vlan);
724 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
725 return 0;
728 static int bt_parse(const char *opt)
730 const char *endp, *p;
731 int vlan;
733 if (strstart(opt, "hci", &endp)) {
734 if (!*endp || *endp == ',') {
735 if (*endp)
736 if (!strstart(endp, ",vlan=", 0))
737 opt = endp + 1;
739 return bt_hci_parse(opt);
741 } else if (strstart(opt, "vhci", &endp)) {
742 if (!*endp || *endp == ',') {
743 if (*endp) {
744 if (strstart(endp, ",vlan=", &p)) {
745 vlan = strtol(p, (char **) &endp, 0);
746 if (*endp) {
747 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
748 return 1;
750 } else {
751 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
752 return 1;
754 } else
755 vlan = 0;
757 bt_vhci_add(vlan);
758 return 0;
760 } else if (strstart(opt, "device:", &endp))
761 return !bt_device_add(endp);
763 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
764 return 1;
767 /***********************************************************/
768 /* QEMU Block devices */
770 #define HD_OPTS "media=disk"
771 #define CDROM_OPTS "media=cdrom"
772 #define FD_OPTS ""
773 #define PFLASH_OPTS ""
774 #define MTD_OPTS ""
775 #define SD_OPTS ""
777 static int drive_init_func(QemuOpts *opts, void *opaque)
779 int *use_scsi = opaque;
781 return drive_init(opts, *use_scsi) == NULL;
784 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
786 if (NULL == qemu_opt_get(opts, "snapshot")) {
787 qemu_opt_set(opts, "snapshot", "on");
789 return 0;
792 static void default_drive(int enable, int snapshot, int use_scsi,
793 BlockInterfaceType type, int index,
794 const char *optstr)
796 QemuOpts *opts;
798 if (type == IF_DEFAULT) {
799 type = use_scsi ? IF_SCSI : IF_IDE;
802 if (!enable || drive_get_by_index(type, index)) {
803 return;
806 opts = drive_add(type, index, NULL, optstr);
807 if (snapshot) {
808 drive_enable_snapshot(opts, NULL);
810 if (!drive_init(opts, use_scsi)) {
811 exit(1);
815 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
817 boot_set_handler = func;
818 boot_set_opaque = opaque;
821 int qemu_boot_set(const char *boot_devices)
823 if (!boot_set_handler) {
824 return -EINVAL;
826 return boot_set_handler(boot_set_opaque, boot_devices);
829 static void validate_bootdevices(char *devices)
831 /* We just do some generic consistency checks */
832 const char *p;
833 int bitmap = 0;
835 for (p = devices; *p != '\0'; p++) {
836 /* Allowed boot devices are:
837 * a-b: floppy disk drives
838 * c-f: IDE disk drives
839 * g-m: machine implementation dependent drives
840 * n-p: network devices
841 * It's up to each machine implementation to check if the given boot
842 * devices match the actual hardware implementation and firmware
843 * features.
845 if (*p < 'a' || *p > 'p') {
846 fprintf(stderr, "Invalid boot device '%c'\n", *p);
847 exit(1);
849 if (bitmap & (1 << (*p - 'a'))) {
850 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
851 exit(1);
853 bitmap |= 1 << (*p - 'a');
857 static void restore_boot_devices(void *opaque)
859 char *standard_boot_devices = opaque;
860 static int first = 1;
862 /* Restore boot order and remove ourselves after the first boot */
863 if (first) {
864 first = 0;
865 return;
868 qemu_boot_set(standard_boot_devices);
870 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
871 g_free(standard_boot_devices);
874 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
875 const char *suffix)
877 FWBootEntry *node, *i;
879 if (bootindex < 0) {
880 return;
883 assert(dev != NULL || suffix != NULL);
885 node = g_malloc0(sizeof(FWBootEntry));
886 node->bootindex = bootindex;
887 node->suffix = suffix ? g_strdup(suffix) : NULL;
888 node->dev = dev;
890 QTAILQ_FOREACH(i, &fw_boot_order, link) {
891 if (i->bootindex == bootindex) {
892 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
893 exit(1);
894 } else if (i->bootindex < bootindex) {
895 continue;
897 QTAILQ_INSERT_BEFORE(i, node, link);
898 return;
900 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
904 * This function returns null terminated string that consist of new line
905 * separated device paths.
907 * memory pointed by "size" is assigned total length of the array in bytes
910 char *get_boot_devices_list(uint32_t *size)
912 FWBootEntry *i;
913 uint32_t total = 0;
914 char *list = NULL;
916 QTAILQ_FOREACH(i, &fw_boot_order, link) {
917 char *devpath = NULL, *bootpath;
918 int len;
920 if (i->dev) {
921 devpath = qdev_get_fw_dev_path(i->dev);
922 assert(devpath);
925 if (i->suffix && devpath) {
926 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
928 bootpath = g_malloc(bootpathlen);
929 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
930 g_free(devpath);
931 } else if (devpath) {
932 bootpath = devpath;
933 } else {
934 assert(i->suffix);
935 bootpath = g_strdup(i->suffix);
938 if (total) {
939 list[total-1] = '\n';
941 len = strlen(bootpath) + 1;
942 list = g_realloc(list, total + len);
943 memcpy(&list[total], bootpath, len);
944 total += len;
945 g_free(bootpath);
948 *size = total;
950 return list;
953 static void numa_add(const char *optarg)
955 char option[128];
956 char *endptr;
957 unsigned long long value, endvalue;
958 int nodenr;
960 optarg = get_opt_name(option, 128, optarg, ',') + 1;
961 if (!strcmp(option, "node")) {
962 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
963 nodenr = nb_numa_nodes;
964 } else {
965 nodenr = strtoull(option, NULL, 10);
968 if (get_param_value(option, 128, "mem", optarg) == 0) {
969 node_mem[nodenr] = 0;
970 } else {
971 int64_t sval;
972 sval = strtosz(option, &endptr);
973 if (sval < 0 || *endptr) {
974 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
975 exit(1);
977 node_mem[nodenr] = sval;
979 if (get_param_value(option, 128, "cpus", optarg) == 0) {
980 node_cpumask[nodenr] = 0;
981 } else {
982 value = strtoull(option, &endptr, 10);
983 if (value >= 64) {
984 value = 63;
985 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
986 } else {
987 if (*endptr == '-') {
988 endvalue = strtoull(endptr+1, &endptr, 10);
989 if (endvalue >= 63) {
990 endvalue = 62;
991 fprintf(stderr,
992 "only 63 CPUs in NUMA mode supported.\n");
994 value = (2ULL << endvalue) - (1ULL << value);
995 } else {
996 value = 1ULL << value;
999 node_cpumask[nodenr] = value;
1001 nb_numa_nodes++;
1003 return;
1006 static void smp_parse(const char *optarg)
1008 int smp, sockets = 0, threads = 0, cores = 0;
1009 char *endptr;
1010 char option[128];
1012 smp = strtoul(optarg, &endptr, 10);
1013 if (endptr != optarg) {
1014 if (*endptr == ',') {
1015 endptr++;
1018 if (get_param_value(option, 128, "sockets", endptr) != 0)
1019 sockets = strtoull(option, NULL, 10);
1020 if (get_param_value(option, 128, "cores", endptr) != 0)
1021 cores = strtoull(option, NULL, 10);
1022 if (get_param_value(option, 128, "threads", endptr) != 0)
1023 threads = strtoull(option, NULL, 10);
1024 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1025 max_cpus = strtoull(option, NULL, 10);
1027 /* compute missing values, prefer sockets over cores over threads */
1028 if (smp == 0 || sockets == 0) {
1029 sockets = sockets > 0 ? sockets : 1;
1030 cores = cores > 0 ? cores : 1;
1031 threads = threads > 0 ? threads : 1;
1032 if (smp == 0) {
1033 smp = cores * threads * sockets;
1035 } else {
1036 if (cores == 0) {
1037 threads = threads > 0 ? threads : 1;
1038 cores = smp / (sockets * threads);
1039 } else {
1040 threads = smp / (cores * sockets);
1043 smp_cpus = smp;
1044 smp_cores = cores > 0 ? cores : 1;
1045 smp_threads = threads > 0 ? threads : 1;
1046 if (max_cpus == 0)
1047 max_cpus = smp_cpus;
1050 /***********************************************************/
1051 /* USB devices */
1053 static int usb_device_add(const char *devname)
1055 const char *p;
1056 USBDevice *dev = NULL;
1058 if (!usb_enabled)
1059 return -1;
1061 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1062 dev = usbdevice_create(devname);
1063 if (dev)
1064 goto done;
1066 /* the other ones */
1067 #ifndef CONFIG_LINUX
1068 /* only the linux version is qdev-ified, usb-bsd still needs this */
1069 if (strstart(devname, "host:", &p)) {
1070 dev = usb_host_device_open(usb_bus_find(-1), p);
1071 } else
1072 #endif
1073 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1074 dev = usb_bt_init(usb_bus_find(-1),
1075 devname[2] ? hci_init(p)
1076 : bt_new_hci(qemu_find_bt_vlan(0)));
1077 } else {
1078 return -1;
1080 if (!dev)
1081 return -1;
1083 done:
1084 return 0;
1087 static int usb_device_del(const char *devname)
1089 int bus_num, addr;
1090 const char *p;
1092 if (strstart(devname, "host:", &p))
1093 return usb_host_device_close(p);
1095 if (!usb_enabled)
1096 return -1;
1098 p = strchr(devname, '.');
1099 if (!p)
1100 return -1;
1101 bus_num = strtoul(devname, NULL, 0);
1102 addr = strtoul(p + 1, NULL, 0);
1104 return usb_device_delete_addr(bus_num, addr);
1107 static int usb_parse(const char *cmdline)
1109 int r;
1110 r = usb_device_add(cmdline);
1111 if (r < 0) {
1112 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1114 return r;
1117 void do_usb_add(Monitor *mon, const QDict *qdict)
1119 const char *devname = qdict_get_str(qdict, "devname");
1120 if (usb_device_add(devname) < 0) {
1121 error_report("could not add USB device '%s'", devname);
1125 void do_usb_del(Monitor *mon, const QDict *qdict)
1127 const char *devname = qdict_get_str(qdict, "devname");
1128 if (usb_device_del(devname) < 0) {
1129 error_report("could not delete USB device '%s'", devname);
1133 /***********************************************************/
1134 /* PCMCIA/Cardbus */
1136 static struct pcmcia_socket_entry_s {
1137 PCMCIASocket *socket;
1138 struct pcmcia_socket_entry_s *next;
1139 } *pcmcia_sockets = 0;
1141 void pcmcia_socket_register(PCMCIASocket *socket)
1143 struct pcmcia_socket_entry_s *entry;
1145 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1146 entry->socket = socket;
1147 entry->next = pcmcia_sockets;
1148 pcmcia_sockets = entry;
1151 void pcmcia_socket_unregister(PCMCIASocket *socket)
1153 struct pcmcia_socket_entry_s *entry, **ptr;
1155 ptr = &pcmcia_sockets;
1156 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1157 if (entry->socket == socket) {
1158 *ptr = entry->next;
1159 g_free(entry);
1163 void pcmcia_info(Monitor *mon)
1165 struct pcmcia_socket_entry_s *iter;
1167 if (!pcmcia_sockets)
1168 monitor_printf(mon, "No PCMCIA sockets\n");
1170 for (iter = pcmcia_sockets; iter; iter = iter->next)
1171 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1172 iter->socket->attached ? iter->socket->card_string :
1173 "Empty");
1176 /***********************************************************/
1177 /* machine registration */
1179 static QEMUMachine *first_machine = NULL;
1180 QEMUMachine *current_machine = NULL;
1182 int qemu_register_machine(QEMUMachine *m)
1184 QEMUMachine **pm;
1185 pm = &first_machine;
1186 while (*pm != NULL)
1187 pm = &(*pm)->next;
1188 m->next = NULL;
1189 *pm = m;
1190 return 0;
1193 static QEMUMachine *find_machine(const char *name)
1195 QEMUMachine *m;
1197 for(m = first_machine; m != NULL; m = m->next) {
1198 if (!strcmp(m->name, name))
1199 return m;
1200 if (m->alias && !strcmp(m->alias, name))
1201 return m;
1203 return NULL;
1206 QEMUMachine *find_default_machine(void)
1208 QEMUMachine *m;
1210 for(m = first_machine; m != NULL; m = m->next) {
1211 if (m->is_default) {
1212 return m;
1215 return NULL;
1218 /***********************************************************/
1219 /* main execution loop */
1221 static void gui_update(void *opaque)
1223 uint64_t interval = GUI_REFRESH_INTERVAL;
1224 DisplayState *ds = opaque;
1225 DisplayChangeListener *dcl = ds->listeners;
1227 dpy_refresh(ds);
1229 while (dcl != NULL) {
1230 if (dcl->gui_timer_interval &&
1231 dcl->gui_timer_interval < interval)
1232 interval = dcl->gui_timer_interval;
1233 dcl = dcl->next;
1235 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1238 struct vm_change_state_entry {
1239 VMChangeStateHandler *cb;
1240 void *opaque;
1241 QLIST_ENTRY (vm_change_state_entry) entries;
1244 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1246 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1247 void *opaque)
1249 VMChangeStateEntry *e;
1251 e = g_malloc0(sizeof (*e));
1253 e->cb = cb;
1254 e->opaque = opaque;
1255 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1256 return e;
1259 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1261 QLIST_REMOVE (e, entries);
1262 g_free (e);
1265 void vm_state_notify(int running, RunState state)
1267 VMChangeStateEntry *e;
1269 trace_vm_state_notify(running, state);
1271 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1272 e->cb(e->opaque, running, state);
1276 void vm_start(void)
1278 if (!runstate_is_running()) {
1279 cpu_enable_ticks();
1280 runstate_set(RUN_STATE_RUNNING);
1281 vm_state_notify(1, RUN_STATE_RUNNING);
1282 resume_all_vcpus();
1283 monitor_protocol_event(QEVENT_RESUME, NULL);
1287 /* reset/shutdown handler */
1289 typedef struct QEMUResetEntry {
1290 QTAILQ_ENTRY(QEMUResetEntry) entry;
1291 QEMUResetHandler *func;
1292 void *opaque;
1293 } QEMUResetEntry;
1295 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1296 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1297 static int reset_requested;
1298 static int shutdown_requested, shutdown_signal = -1;
1299 static pid_t shutdown_pid;
1300 static int powerdown_requested;
1301 static int debug_requested;
1302 static int suspend_requested;
1303 static NotifierList suspend_notifiers =
1304 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1305 static NotifierList wakeup_notifiers =
1306 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1307 static uint32_t wakeup_reason_mask = ~0;
1308 static RunState vmstop_requested = RUN_STATE_MAX;
1310 int qemu_shutdown_requested_get(void)
1312 return shutdown_requested;
1315 int qemu_reset_requested_get(void)
1317 return reset_requested;
1320 int qemu_shutdown_requested(void)
1322 int r = shutdown_requested;
1323 shutdown_requested = 0;
1324 return r;
1327 void qemu_kill_report(void)
1329 if (!qtest_enabled() && shutdown_signal != -1) {
1330 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1331 if (shutdown_pid == 0) {
1332 /* This happens for eg ^C at the terminal, so it's worth
1333 * avoiding printing an odd message in that case.
1335 fputc('\n', stderr);
1336 } else {
1337 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1339 shutdown_signal = -1;
1343 int qemu_reset_requested(void)
1345 int r = reset_requested;
1346 reset_requested = 0;
1347 return r;
1350 static int qemu_suspend_requested(void)
1352 int r = suspend_requested;
1353 suspend_requested = 0;
1354 return r;
1357 int qemu_powerdown_requested(void)
1359 int r = powerdown_requested;
1360 powerdown_requested = 0;
1361 return r;
1364 static int qemu_debug_requested(void)
1366 int r = debug_requested;
1367 debug_requested = 0;
1368 return r;
1371 /* We use RUN_STATE_MAX but any invalid value will do */
1372 static bool qemu_vmstop_requested(RunState *r)
1374 if (vmstop_requested < RUN_STATE_MAX) {
1375 *r = vmstop_requested;
1376 vmstop_requested = RUN_STATE_MAX;
1377 return true;
1380 return false;
1383 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1385 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1387 re->func = func;
1388 re->opaque = opaque;
1389 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1392 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1394 QEMUResetEntry *re;
1396 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1397 if (re->func == func && re->opaque == opaque) {
1398 QTAILQ_REMOVE(&reset_handlers, re, entry);
1399 g_free(re);
1400 return;
1405 void qemu_system_reset(bool report)
1407 QEMUResetEntry *re, *nre;
1409 /* reset all devices */
1410 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1411 re->func(re->opaque);
1413 if (report) {
1414 monitor_protocol_event(QEVENT_RESET, NULL);
1416 cpu_synchronize_all_post_reset();
1419 void qemu_system_reset_request(void)
1421 if (no_reboot) {
1422 shutdown_requested = 1;
1423 } else {
1424 reset_requested = 1;
1426 cpu_stop_current();
1427 qemu_notify_event();
1430 static void qemu_system_suspend(void)
1432 pause_all_vcpus();
1433 notifier_list_notify(&suspend_notifiers, NULL);
1434 runstate_set(RUN_STATE_SUSPENDED);
1435 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1438 void qemu_system_suspend_request(void)
1440 if (runstate_check(RUN_STATE_SUSPENDED)) {
1441 return;
1443 suspend_requested = 1;
1444 cpu_stop_current();
1445 qemu_notify_event();
1448 void qemu_register_suspend_notifier(Notifier *notifier)
1450 notifier_list_add(&suspend_notifiers, notifier);
1453 void qemu_system_wakeup_request(WakeupReason reason)
1455 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1456 return;
1458 if (!(wakeup_reason_mask & (1 << reason))) {
1459 return;
1461 runstate_set(RUN_STATE_RUNNING);
1462 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1463 notifier_list_notify(&wakeup_notifiers, &reason);
1464 reset_requested = 1;
1465 qemu_notify_event();
1468 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1470 if (enabled) {
1471 wakeup_reason_mask |= (1 << reason);
1472 } else {
1473 wakeup_reason_mask &= ~(1 << reason);
1477 void qemu_register_wakeup_notifier(Notifier *notifier)
1479 notifier_list_add(&wakeup_notifiers, notifier);
1482 void qemu_system_killed(int signal, pid_t pid)
1484 shutdown_signal = signal;
1485 shutdown_pid = pid;
1486 no_shutdown = 0;
1487 qemu_system_shutdown_request();
1490 void qemu_system_shutdown_request(void)
1492 shutdown_requested = 1;
1493 qemu_notify_event();
1496 void qemu_system_powerdown_request(void)
1498 powerdown_requested = 1;
1499 qemu_notify_event();
1502 void qemu_system_debug_request(void)
1504 debug_requested = 1;
1505 qemu_notify_event();
1508 void qemu_system_vmstop_request(RunState state)
1510 vmstop_requested = state;
1511 qemu_notify_event();
1514 qemu_irq qemu_system_powerdown;
1516 static bool main_loop_should_exit(void)
1518 RunState r;
1519 if (qemu_debug_requested()) {
1520 vm_stop(RUN_STATE_DEBUG);
1522 if (qemu_suspend_requested()) {
1523 qemu_system_suspend();
1525 if (qemu_shutdown_requested()) {
1526 qemu_kill_report();
1527 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1528 if (no_shutdown) {
1529 vm_stop(RUN_STATE_SHUTDOWN);
1530 } else {
1531 return true;
1534 if (qemu_reset_requested()) {
1535 pause_all_vcpus();
1536 cpu_synchronize_all_states();
1537 qemu_system_reset(VMRESET_REPORT);
1538 resume_all_vcpus();
1539 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1540 runstate_check(RUN_STATE_SHUTDOWN)) {
1541 runstate_set(RUN_STATE_PAUSED);
1544 if (qemu_powerdown_requested()) {
1545 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1546 qemu_irq_raise(qemu_system_powerdown);
1548 if (qemu_vmstop_requested(&r)) {
1549 vm_stop(r);
1551 return false;
1554 static void main_loop(void)
1556 bool nonblocking;
1557 int last_io = 0;
1558 #ifdef CONFIG_PROFILER
1559 int64_t ti;
1560 #endif
1561 do {
1562 nonblocking = !kvm_enabled() && last_io > 0;
1563 #ifdef CONFIG_PROFILER
1564 ti = profile_getclock();
1565 #endif
1566 last_io = main_loop_wait(nonblocking);
1567 #ifdef CONFIG_PROFILER
1568 dev_time += profile_getclock() - ti;
1569 #endif
1570 } while (!main_loop_should_exit());
1573 static void version(void)
1575 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1578 static void help(int exitcode)
1580 version();
1581 printf("usage: %s [options] [disk_image]\n\n"
1582 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1583 error_get_progname());
1585 #define QEMU_OPTIONS_GENERATE_HELP
1586 #include "qemu-options-wrapper.h"
1588 printf("\nDuring emulation, the following keys are useful:\n"
1589 "ctrl-alt-f toggle full screen\n"
1590 "ctrl-alt-n switch to virtual console 'n'\n"
1591 "ctrl-alt toggle mouse and keyboard grab\n"
1592 "\n"
1593 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1595 exit(exitcode);
1598 #define HAS_ARG 0x0001
1600 typedef struct QEMUOption {
1601 const char *name;
1602 int flags;
1603 int index;
1604 uint32_t arch_mask;
1605 } QEMUOption;
1607 static const QEMUOption qemu_options[] = {
1608 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1609 #define QEMU_OPTIONS_GENERATE_OPTIONS
1610 #include "qemu-options-wrapper.h"
1611 { NULL },
1614 static bool vga_available(void)
1616 return qdev_exists("VGA") || qdev_exists("isa-vga");
1619 static bool cirrus_vga_available(void)
1621 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1624 static bool vmware_vga_available(void)
1626 return qdev_exists("vmware-svga");
1629 static void select_vgahw (const char *p)
1631 const char *opts;
1633 vga_interface_type = VGA_NONE;
1634 if (strstart(p, "std", &opts)) {
1635 if (vga_available()) {
1636 vga_interface_type = VGA_STD;
1637 } else {
1638 fprintf(stderr, "Error: standard VGA not available\n");
1639 exit(0);
1641 } else if (strstart(p, "cirrus", &opts)) {
1642 if (cirrus_vga_available()) {
1643 vga_interface_type = VGA_CIRRUS;
1644 } else {
1645 fprintf(stderr, "Error: Cirrus VGA not available\n");
1646 exit(0);
1648 } else if (strstart(p, "vmware", &opts)) {
1649 if (vmware_vga_available()) {
1650 vga_interface_type = VGA_VMWARE;
1651 } else {
1652 fprintf(stderr, "Error: VMWare SVGA not available\n");
1653 exit(0);
1655 } else if (strstart(p, "xenfb", &opts)) {
1656 vga_interface_type = VGA_XENFB;
1657 } else if (strstart(p, "qxl", &opts)) {
1658 vga_interface_type = VGA_QXL;
1659 } else if (!strstart(p, "none", &opts)) {
1660 invalid_vga:
1661 fprintf(stderr, "Unknown vga type: %s\n", p);
1662 exit(1);
1664 while (*opts) {
1665 const char *nextopt;
1667 if (strstart(opts, ",retrace=", &nextopt)) {
1668 opts = nextopt;
1669 if (strstart(opts, "dumb", &nextopt))
1670 vga_retrace_method = VGA_RETRACE_DUMB;
1671 else if (strstart(opts, "precise", &nextopt))
1672 vga_retrace_method = VGA_RETRACE_PRECISE;
1673 else goto invalid_vga;
1674 } else goto invalid_vga;
1675 opts = nextopt;
1679 static DisplayType select_display(const char *p)
1681 const char *opts;
1682 DisplayType display = DT_DEFAULT;
1684 if (strstart(p, "sdl", &opts)) {
1685 #ifdef CONFIG_SDL
1686 display = DT_SDL;
1687 while (*opts) {
1688 const char *nextopt;
1690 if (strstart(opts, ",frame=", &nextopt)) {
1691 opts = nextopt;
1692 if (strstart(opts, "on", &nextopt)) {
1693 no_frame = 0;
1694 } else if (strstart(opts, "off", &nextopt)) {
1695 no_frame = 1;
1696 } else {
1697 goto invalid_sdl_args;
1699 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1700 opts = nextopt;
1701 if (strstart(opts, "on", &nextopt)) {
1702 alt_grab = 1;
1703 } else if (strstart(opts, "off", &nextopt)) {
1704 alt_grab = 0;
1705 } else {
1706 goto invalid_sdl_args;
1708 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1709 opts = nextopt;
1710 if (strstart(opts, "on", &nextopt)) {
1711 ctrl_grab = 1;
1712 } else if (strstart(opts, "off", &nextopt)) {
1713 ctrl_grab = 0;
1714 } else {
1715 goto invalid_sdl_args;
1717 } else if (strstart(opts, ",window_close=", &nextopt)) {
1718 opts = nextopt;
1719 if (strstart(opts, "on", &nextopt)) {
1720 no_quit = 0;
1721 } else if (strstart(opts, "off", &nextopt)) {
1722 no_quit = 1;
1723 } else {
1724 goto invalid_sdl_args;
1726 } else {
1727 invalid_sdl_args:
1728 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1729 exit(1);
1731 opts = nextopt;
1733 #else
1734 fprintf(stderr, "SDL support is disabled\n");
1735 exit(1);
1736 #endif
1737 } else if (strstart(p, "vnc", &opts)) {
1738 #ifdef CONFIG_VNC
1739 display_remote++;
1741 if (*opts) {
1742 const char *nextopt;
1744 if (strstart(opts, "=", &nextopt)) {
1745 vnc_display = nextopt;
1748 if (!vnc_display) {
1749 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1750 exit(1);
1752 #else
1753 fprintf(stderr, "VNC support is disabled\n");
1754 exit(1);
1755 #endif
1756 } else if (strstart(p, "curses", &opts)) {
1757 #ifdef CONFIG_CURSES
1758 display = DT_CURSES;
1759 #else
1760 fprintf(stderr, "Curses support is disabled\n");
1761 exit(1);
1762 #endif
1763 } else if (strstart(p, "none", &opts)) {
1764 display = DT_NONE;
1765 } else {
1766 fprintf(stderr, "Unknown display type: %s\n", p);
1767 exit(1);
1770 return display;
1773 static int balloon_parse(const char *arg)
1775 QemuOpts *opts;
1777 if (strcmp(arg, "none") == 0) {
1778 return 0;
1781 if (!strncmp(arg, "virtio", 6)) {
1782 if (arg[6] == ',') {
1783 /* have params -> parse them */
1784 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1785 if (!opts)
1786 return -1;
1787 } else {
1788 /* create empty opts */
1789 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
1791 qemu_opt_set(opts, "driver", "virtio-balloon");
1792 return 0;
1795 return -1;
1798 char *qemu_find_file(int type, const char *name)
1800 int len;
1801 const char *subdir;
1802 char *buf;
1804 /* If name contains path separators then try it as a straight path. */
1805 if ((strchr(name, '/') || strchr(name, '\\'))
1806 && access(name, R_OK) == 0) {
1807 return g_strdup(name);
1809 switch (type) {
1810 case QEMU_FILE_TYPE_BIOS:
1811 subdir = "";
1812 break;
1813 case QEMU_FILE_TYPE_KEYMAP:
1814 subdir = "keymaps/";
1815 break;
1816 default:
1817 abort();
1819 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1820 buf = g_malloc0(len);
1821 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1822 if (access(buf, R_OK)) {
1823 g_free(buf);
1824 return NULL;
1826 return buf;
1829 static int device_help_func(QemuOpts *opts, void *opaque)
1831 return qdev_device_help(opts);
1834 static int device_init_func(QemuOpts *opts, void *opaque)
1836 DeviceState *dev;
1838 dev = qdev_device_add(opts);
1839 if (!dev)
1840 return -1;
1841 return 0;
1844 static int chardev_init_func(QemuOpts *opts, void *opaque)
1846 CharDriverState *chr;
1848 chr = qemu_chr_new_from_opts(opts, NULL);
1849 if (!chr)
1850 return -1;
1851 return 0;
1854 #ifdef CONFIG_VIRTFS
1855 static int fsdev_init_func(QemuOpts *opts, void *opaque)
1857 int ret;
1858 ret = qemu_fsdev_add(opts);
1860 return ret;
1862 #endif
1864 static int mon_init_func(QemuOpts *opts, void *opaque)
1866 CharDriverState *chr;
1867 const char *chardev;
1868 const char *mode;
1869 int flags;
1871 mode = qemu_opt_get(opts, "mode");
1872 if (mode == NULL) {
1873 mode = "readline";
1875 if (strcmp(mode, "readline") == 0) {
1876 flags = MONITOR_USE_READLINE;
1877 } else if (strcmp(mode, "control") == 0) {
1878 flags = MONITOR_USE_CONTROL;
1879 } else {
1880 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1881 exit(1);
1884 if (qemu_opt_get_bool(opts, "pretty", 0))
1885 flags |= MONITOR_USE_PRETTY;
1887 if (qemu_opt_get_bool(opts, "default", 0))
1888 flags |= MONITOR_IS_DEFAULT;
1890 chardev = qemu_opt_get(opts, "chardev");
1891 chr = qemu_chr_find(chardev);
1892 if (chr == NULL) {
1893 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1894 exit(1);
1897 monitor_init(chr, flags);
1898 return 0;
1901 static void monitor_parse(const char *optarg, const char *mode)
1903 static int monitor_device_index = 0;
1904 QemuOpts *opts;
1905 const char *p;
1906 char label[32];
1907 int def = 0;
1909 if (strstart(optarg, "chardev:", &p)) {
1910 snprintf(label, sizeof(label), "%s", p);
1911 } else {
1912 snprintf(label, sizeof(label), "compat_monitor%d",
1913 monitor_device_index);
1914 if (monitor_device_index == 0) {
1915 def = 1;
1917 opts = qemu_chr_parse_compat(label, optarg);
1918 if (!opts) {
1919 fprintf(stderr, "parse error: %s\n", optarg);
1920 exit(1);
1924 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
1925 if (!opts) {
1926 fprintf(stderr, "duplicate chardev: %s\n", label);
1927 exit(1);
1929 qemu_opt_set(opts, "mode", mode);
1930 qemu_opt_set(opts, "chardev", label);
1931 if (def)
1932 qemu_opt_set(opts, "default", "on");
1933 monitor_device_index++;
1936 struct device_config {
1937 enum {
1938 DEV_USB, /* -usbdevice */
1939 DEV_BT, /* -bt */
1940 DEV_SERIAL, /* -serial */
1941 DEV_PARALLEL, /* -parallel */
1942 DEV_VIRTCON, /* -virtioconsole */
1943 DEV_DEBUGCON, /* -debugcon */
1944 DEV_GDB, /* -gdb, -s */
1945 } type;
1946 const char *cmdline;
1947 Location loc;
1948 QTAILQ_ENTRY(device_config) next;
1950 QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1952 static void add_device_config(int type, const char *cmdline)
1954 struct device_config *conf;
1956 conf = g_malloc0(sizeof(*conf));
1957 conf->type = type;
1958 conf->cmdline = cmdline;
1959 loc_save(&conf->loc);
1960 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1963 static int foreach_device_config(int type, int (*func)(const char *cmdline))
1965 struct device_config *conf;
1966 int rc;
1968 QTAILQ_FOREACH(conf, &device_configs, next) {
1969 if (conf->type != type)
1970 continue;
1971 loc_push_restore(&conf->loc);
1972 rc = func(conf->cmdline);
1973 loc_pop(&conf->loc);
1974 if (0 != rc)
1975 return rc;
1977 return 0;
1980 static int serial_parse(const char *devname)
1982 static int index = 0;
1983 char label[32];
1985 if (strcmp(devname, "none") == 0)
1986 return 0;
1987 if (index == MAX_SERIAL_PORTS) {
1988 fprintf(stderr, "qemu: too many serial ports\n");
1989 exit(1);
1991 snprintf(label, sizeof(label), "serial%d", index);
1992 serial_hds[index] = qemu_chr_new(label, devname, NULL);
1993 if (!serial_hds[index]) {
1994 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1995 devname, strerror(errno));
1996 return -1;
1998 index++;
1999 return 0;
2002 static int parallel_parse(const char *devname)
2004 static int index = 0;
2005 char label[32];
2007 if (strcmp(devname, "none") == 0)
2008 return 0;
2009 if (index == MAX_PARALLEL_PORTS) {
2010 fprintf(stderr, "qemu: too many parallel ports\n");
2011 exit(1);
2013 snprintf(label, sizeof(label), "parallel%d", index);
2014 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2015 if (!parallel_hds[index]) {
2016 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2017 devname, strerror(errno));
2018 return -1;
2020 index++;
2021 return 0;
2024 static int virtcon_parse(const char *devname)
2026 QemuOptsList *device = qemu_find_opts("device");
2027 static int index = 0;
2028 char label[32];
2029 QemuOpts *bus_opts, *dev_opts;
2031 if (strcmp(devname, "none") == 0)
2032 return 0;
2033 if (index == MAX_VIRTIO_CONSOLES) {
2034 fprintf(stderr, "qemu: too many virtio consoles\n");
2035 exit(1);
2038 bus_opts = qemu_opts_create(device, NULL, 0, NULL);
2039 if (arch_type == QEMU_ARCH_S390X) {
2040 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2041 } else {
2042 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2045 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2046 qemu_opt_set(dev_opts, "driver", "virtconsole");
2048 snprintf(label, sizeof(label), "virtcon%d", index);
2049 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2050 if (!virtcon_hds[index]) {
2051 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2052 devname, strerror(errno));
2053 return -1;
2055 qemu_opt_set(dev_opts, "chardev", label);
2057 index++;
2058 return 0;
2061 static int debugcon_parse(const char *devname)
2063 QemuOpts *opts;
2065 if (!qemu_chr_new("debugcon", devname, NULL)) {
2066 exit(1);
2068 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2069 if (!opts) {
2070 fprintf(stderr, "qemu: already have a debugcon device\n");
2071 exit(1);
2073 qemu_opt_set(opts, "driver", "isa-debugcon");
2074 qemu_opt_set(opts, "chardev", "debugcon");
2075 return 0;
2078 static QEMUMachine *machine_parse(const char *name)
2080 QEMUMachine *m, *machine = NULL;
2082 if (name) {
2083 machine = find_machine(name);
2085 if (machine) {
2086 return machine;
2088 printf("Supported machines are:\n");
2089 for (m = first_machine; m != NULL; m = m->next) {
2090 if (m->alias) {
2091 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2093 printf("%-20s %s%s\n", m->name, m->desc,
2094 m->is_default ? " (default)" : "");
2096 exit(!name || *name != '?');
2099 static int tcg_init(void)
2101 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2102 return 0;
2105 static struct {
2106 const char *opt_name;
2107 const char *name;
2108 int (*available)(void);
2109 int (*init)(void);
2110 int *allowed;
2111 } accel_list[] = {
2112 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2113 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2114 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2115 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2118 static int configure_accelerator(void)
2120 const char *p = NULL;
2121 char buf[10];
2122 int i, ret;
2123 bool accel_initialised = 0;
2124 bool init_failed = 0;
2126 QemuOptsList *list = qemu_find_opts("machine");
2127 if (!QTAILQ_EMPTY(&list->head)) {
2128 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2131 if (p == NULL) {
2132 /* Use the default "accelerator", tcg */
2133 p = "tcg";
2136 while (!accel_initialised && *p != '\0') {
2137 if (*p == ':') {
2138 p++;
2140 p = get_opt_name(buf, sizeof (buf), p, ':');
2141 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2142 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2143 *(accel_list[i].allowed) = 1;
2144 ret = accel_list[i].init();
2145 if (ret < 0) {
2146 init_failed = 1;
2147 if (!accel_list[i].available()) {
2148 printf("%s not supported for this target\n",
2149 accel_list[i].name);
2150 } else {
2151 fprintf(stderr, "failed to initialize %s: %s\n",
2152 accel_list[i].name,
2153 strerror(-ret));
2155 *(accel_list[i].allowed) = 0;
2156 } else {
2157 accel_initialised = 1;
2159 break;
2162 if (i == ARRAY_SIZE(accel_list)) {
2163 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2167 if (!accel_initialised) {
2168 fprintf(stderr, "No accelerator found!\n");
2169 exit(1);
2172 if (init_failed) {
2173 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2176 return !accel_initialised;
2179 void qemu_add_exit_notifier(Notifier *notify)
2181 notifier_list_add(&exit_notifiers, notify);
2184 void qemu_remove_exit_notifier(Notifier *notify)
2186 notifier_remove(notify);
2189 static void qemu_run_exit_notifiers(void)
2191 notifier_list_notify(&exit_notifiers, NULL);
2194 void qemu_add_machine_init_done_notifier(Notifier *notify)
2196 notifier_list_add(&machine_init_done_notifiers, notify);
2199 static void qemu_run_machine_init_done_notifiers(void)
2201 notifier_list_notify(&machine_init_done_notifiers, NULL);
2204 static const QEMUOption *lookup_opt(int argc, char **argv,
2205 const char **poptarg, int *poptind)
2207 const QEMUOption *popt;
2208 int optind = *poptind;
2209 char *r = argv[optind];
2210 const char *optarg;
2212 loc_set_cmdline(argv, optind, 1);
2213 optind++;
2214 /* Treat --foo the same as -foo. */
2215 if (r[1] == '-')
2216 r++;
2217 popt = qemu_options;
2218 for(;;) {
2219 if (!popt->name) {
2220 error_report("invalid option");
2221 exit(1);
2223 if (!strcmp(popt->name, r + 1))
2224 break;
2225 popt++;
2227 if (popt->flags & HAS_ARG) {
2228 if (optind >= argc) {
2229 error_report("requires an argument");
2230 exit(1);
2232 optarg = argv[optind++];
2233 loc_set_cmdline(argv, optind - 2, 2);
2234 } else {
2235 optarg = NULL;
2238 *poptarg = optarg;
2239 *poptind = optind;
2241 return popt;
2244 static gpointer malloc_and_trace(gsize n_bytes)
2246 void *ptr = malloc(n_bytes);
2247 trace_g_malloc(n_bytes, ptr);
2248 return ptr;
2251 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2253 void *ptr = realloc(mem, n_bytes);
2254 trace_g_realloc(mem, n_bytes, ptr);
2255 return ptr;
2258 static void free_and_trace(gpointer mem)
2260 trace_g_free(mem);
2261 free(mem);
2264 int qemu_init_main_loop(void)
2266 return main_loop_init();
2269 int main(int argc, char **argv, char **envp)
2271 int i;
2272 int snapshot, linux_boot;
2273 const char *icount_option = NULL;
2274 const char *initrd_filename;
2275 const char *kernel_filename, *kernel_cmdline;
2276 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2277 DisplayState *ds;
2278 DisplayChangeListener *dcl;
2279 int cyls, heads, secs, translation;
2280 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2281 QemuOptsList *olist;
2282 int optind;
2283 const char *optarg;
2284 const char *loadvm = NULL;
2285 QEMUMachine *machine;
2286 const char *cpu_model;
2287 const char *vga_model = "none";
2288 const char *pid_file = NULL;
2289 const char *incoming = NULL;
2290 #ifdef CONFIG_VNC
2291 int show_vnc_port = 0;
2292 #endif
2293 bool defconfig = true;
2294 bool userconfig = true;
2295 const char *log_mask = NULL;
2296 const char *log_file = NULL;
2297 GMemVTable mem_trace = {
2298 .malloc = malloc_and_trace,
2299 .realloc = realloc_and_trace,
2300 .free = free_and_trace,
2302 const char *trace_events = NULL;
2303 const char *trace_file = NULL;
2305 atexit(qemu_run_exit_notifiers);
2306 error_set_progname(argv[0]);
2308 g_mem_set_vtable(&mem_trace);
2309 if (!g_thread_supported()) {
2310 #if !GLIB_CHECK_VERSION(2, 31, 0)
2311 g_thread_init(NULL);
2312 #else
2313 fprintf(stderr, "glib threading failed to initialize.\n");
2314 exit(1);
2315 #endif
2318 module_call_init(MODULE_INIT_QOM);
2320 runstate_init();
2322 init_clocks();
2323 rtc_clock = host_clock;
2325 qemu_cache_utils_init(envp);
2327 QLIST_INIT (&vm_change_state_head);
2328 os_setup_early_signal_handling();
2330 module_call_init(MODULE_INIT_MACHINE);
2331 machine = find_default_machine();
2332 cpu_model = NULL;
2333 ram_size = 0;
2334 snapshot = 0;
2335 cyls = heads = secs = 0;
2336 translation = BIOS_ATA_TRANSLATION_AUTO;
2338 for (i = 0; i < MAX_NODES; i++) {
2339 node_mem[i] = 0;
2340 node_cpumask[i] = 0;
2343 nb_numa_nodes = 0;
2344 nb_nics = 0;
2346 autostart= 1;
2348 /* first pass of option parsing */
2349 optind = 1;
2350 while (optind < argc) {
2351 if (argv[optind][0] != '-') {
2352 /* disk image */
2353 optind++;
2354 continue;
2355 } else {
2356 const QEMUOption *popt;
2358 popt = lookup_opt(argc, argv, &optarg, &optind);
2359 switch (popt->index) {
2360 case QEMU_OPTION_nodefconfig:
2361 defconfig = false;
2362 break;
2363 case QEMU_OPTION_nouserconfig:
2364 userconfig = false;
2365 break;
2370 if (defconfig) {
2371 int ret;
2372 ret = qemu_read_default_config_files(userconfig);
2373 if (ret < 0) {
2374 exit(1);
2378 /* second pass of option parsing */
2379 optind = 1;
2380 for(;;) {
2381 if (optind >= argc)
2382 break;
2383 if (argv[optind][0] != '-') {
2384 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2385 } else {
2386 const QEMUOption *popt;
2388 popt = lookup_opt(argc, argv, &optarg, &optind);
2389 if (!(popt->arch_mask & arch_type)) {
2390 printf("Option %s not supported for this target\n", popt->name);
2391 exit(1);
2393 switch(popt->index) {
2394 case QEMU_OPTION_M:
2395 machine = machine_parse(optarg);
2396 break;
2397 case QEMU_OPTION_cpu:
2398 /* hw initialization will check this */
2399 cpu_model = optarg;
2400 break;
2401 case QEMU_OPTION_hda:
2403 char buf[256];
2404 if (cyls == 0)
2405 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2406 else
2407 snprintf(buf, sizeof(buf),
2408 "%s,cyls=%d,heads=%d,secs=%d%s",
2409 HD_OPTS , cyls, heads, secs,
2410 translation == BIOS_ATA_TRANSLATION_LBA ?
2411 ",trans=lba" :
2412 translation == BIOS_ATA_TRANSLATION_NONE ?
2413 ",trans=none" : "");
2414 drive_add(IF_DEFAULT, 0, optarg, buf);
2415 break;
2417 case QEMU_OPTION_hdb:
2418 case QEMU_OPTION_hdc:
2419 case QEMU_OPTION_hdd:
2420 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2421 HD_OPTS);
2422 break;
2423 case QEMU_OPTION_drive:
2424 if (drive_def(optarg) == NULL) {
2425 exit(1);
2427 break;
2428 case QEMU_OPTION_set:
2429 if (qemu_set_option(optarg) != 0)
2430 exit(1);
2431 break;
2432 case QEMU_OPTION_global:
2433 if (qemu_global_option(optarg) != 0)
2434 exit(1);
2435 break;
2436 case QEMU_OPTION_mtdblock:
2437 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2438 break;
2439 case QEMU_OPTION_sd:
2440 drive_add(IF_SD, 0, optarg, SD_OPTS);
2441 break;
2442 case QEMU_OPTION_pflash:
2443 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2444 break;
2445 case QEMU_OPTION_snapshot:
2446 snapshot = 1;
2447 break;
2448 case QEMU_OPTION_hdachs:
2450 const char *p;
2451 p = optarg;
2452 cyls = strtol(p, (char **)&p, 0);
2453 if (cyls < 1 || cyls > 16383)
2454 goto chs_fail;
2455 if (*p != ',')
2456 goto chs_fail;
2457 p++;
2458 heads = strtol(p, (char **)&p, 0);
2459 if (heads < 1 || heads > 16)
2460 goto chs_fail;
2461 if (*p != ',')
2462 goto chs_fail;
2463 p++;
2464 secs = strtol(p, (char **)&p, 0);
2465 if (secs < 1 || secs > 63)
2466 goto chs_fail;
2467 if (*p == ',') {
2468 p++;
2469 if (!strcmp(p, "none"))
2470 translation = BIOS_ATA_TRANSLATION_NONE;
2471 else if (!strcmp(p, "lba"))
2472 translation = BIOS_ATA_TRANSLATION_LBA;
2473 else if (!strcmp(p, "auto"))
2474 translation = BIOS_ATA_TRANSLATION_AUTO;
2475 else
2476 goto chs_fail;
2477 } else if (*p != '\0') {
2478 chs_fail:
2479 fprintf(stderr, "qemu: invalid physical CHS format\n");
2480 exit(1);
2482 if (hda_opts != NULL) {
2483 char num[16];
2484 snprintf(num, sizeof(num), "%d", cyls);
2485 qemu_opt_set(hda_opts, "cyls", num);
2486 snprintf(num, sizeof(num), "%d", heads);
2487 qemu_opt_set(hda_opts, "heads", num);
2488 snprintf(num, sizeof(num), "%d", secs);
2489 qemu_opt_set(hda_opts, "secs", num);
2490 if (translation == BIOS_ATA_TRANSLATION_LBA)
2491 qemu_opt_set(hda_opts, "trans", "lba");
2492 if (translation == BIOS_ATA_TRANSLATION_NONE)
2493 qemu_opt_set(hda_opts, "trans", "none");
2496 break;
2497 case QEMU_OPTION_numa:
2498 if (nb_numa_nodes >= MAX_NODES) {
2499 fprintf(stderr, "qemu: too many NUMA nodes\n");
2500 exit(1);
2502 numa_add(optarg);
2503 break;
2504 case QEMU_OPTION_display:
2505 display_type = select_display(optarg);
2506 break;
2507 case QEMU_OPTION_nographic:
2508 display_type = DT_NOGRAPHIC;
2509 break;
2510 case QEMU_OPTION_curses:
2511 #ifdef CONFIG_CURSES
2512 display_type = DT_CURSES;
2513 #else
2514 fprintf(stderr, "Curses support is disabled\n");
2515 exit(1);
2516 #endif
2517 break;
2518 case QEMU_OPTION_portrait:
2519 graphic_rotate = 90;
2520 break;
2521 case QEMU_OPTION_rotate:
2522 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2523 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2524 graphic_rotate != 180 && graphic_rotate != 270) {
2525 fprintf(stderr,
2526 "qemu: only 90, 180, 270 deg rotation is available\n");
2527 exit(1);
2529 break;
2530 case QEMU_OPTION_kernel:
2531 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2532 break;
2533 case QEMU_OPTION_initrd:
2534 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
2535 break;
2536 case QEMU_OPTION_append:
2537 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
2538 break;
2539 case QEMU_OPTION_dtb:
2540 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
2541 break;
2542 case QEMU_OPTION_cdrom:
2543 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2544 break;
2545 case QEMU_OPTION_boot:
2547 static const char * const params[] = {
2548 "order", "once", "menu",
2549 "splash", "splash-time", NULL
2551 char buf[sizeof(boot_devices)];
2552 char *standard_boot_devices;
2553 int legacy = 0;
2555 if (!strchr(optarg, '=')) {
2556 legacy = 1;
2557 pstrcpy(buf, sizeof(buf), optarg);
2558 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2559 fprintf(stderr,
2560 "qemu: unknown boot parameter '%s' in '%s'\n",
2561 buf, optarg);
2562 exit(1);
2565 if (legacy ||
2566 get_param_value(buf, sizeof(buf), "order", optarg)) {
2567 validate_bootdevices(buf);
2568 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2570 if (!legacy) {
2571 if (get_param_value(buf, sizeof(buf),
2572 "once", optarg)) {
2573 validate_bootdevices(buf);
2574 standard_boot_devices = g_strdup(boot_devices);
2575 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2576 qemu_register_reset(restore_boot_devices,
2577 standard_boot_devices);
2579 if (get_param_value(buf, sizeof(buf),
2580 "menu", optarg)) {
2581 if (!strcmp(buf, "on")) {
2582 boot_menu = 1;
2583 } else if (!strcmp(buf, "off")) {
2584 boot_menu = 0;
2585 } else {
2586 fprintf(stderr,
2587 "qemu: invalid option value '%s'\n",
2588 buf);
2589 exit(1);
2592 qemu_opts_parse(qemu_find_opts("boot-opts"),
2593 optarg, 0);
2596 break;
2597 case QEMU_OPTION_fda:
2598 case QEMU_OPTION_fdb:
2599 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2600 optarg, FD_OPTS);
2601 break;
2602 case QEMU_OPTION_no_fd_bootchk:
2603 fd_bootchk = 0;
2604 break;
2605 case QEMU_OPTION_netdev:
2606 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2607 exit(1);
2609 break;
2610 case QEMU_OPTION_net:
2611 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2612 exit(1);
2614 break;
2615 #ifdef CONFIG_LIBISCSI
2616 case QEMU_OPTION_iscsi:
2617 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2618 if (!opts) {
2619 exit(1);
2621 break;
2622 #endif
2623 #ifdef CONFIG_SLIRP
2624 case QEMU_OPTION_tftp:
2625 legacy_tftp_prefix = optarg;
2626 break;
2627 case QEMU_OPTION_bootp:
2628 legacy_bootp_filename = optarg;
2629 break;
2630 case QEMU_OPTION_redir:
2631 if (net_slirp_redir(optarg) < 0)
2632 exit(1);
2633 break;
2634 #endif
2635 case QEMU_OPTION_bt:
2636 add_device_config(DEV_BT, optarg);
2637 break;
2638 case QEMU_OPTION_audio_help:
2639 if (!(audio_available())) {
2640 printf("Option %s not supported for this target\n", popt->name);
2641 exit(1);
2643 AUD_help ();
2644 exit (0);
2645 break;
2646 case QEMU_OPTION_soundhw:
2647 if (!(audio_available())) {
2648 printf("Option %s not supported for this target\n", popt->name);
2649 exit(1);
2651 select_soundhw (optarg);
2652 break;
2653 case QEMU_OPTION_h:
2654 help(0);
2655 break;
2656 case QEMU_OPTION_version:
2657 version();
2658 exit(0);
2659 break;
2660 case QEMU_OPTION_m: {
2661 int64_t value;
2662 char *end;
2664 value = strtosz(optarg, &end);
2665 if (value < 0 || *end) {
2666 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2667 exit(1);
2670 if (value != (uint64_t)(ram_addr_t)value) {
2671 fprintf(stderr, "qemu: ram size too large\n");
2672 exit(1);
2674 ram_size = value;
2675 break;
2677 case QEMU_OPTION_mempath:
2678 mem_path = optarg;
2679 break;
2680 #ifdef MAP_POPULATE
2681 case QEMU_OPTION_mem_prealloc:
2682 mem_prealloc = 1;
2683 break;
2684 #endif
2685 case QEMU_OPTION_d:
2686 log_mask = optarg;
2687 break;
2688 case QEMU_OPTION_D:
2689 log_file = optarg;
2690 break;
2691 case QEMU_OPTION_s:
2692 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
2693 break;
2694 case QEMU_OPTION_gdb:
2695 add_device_config(DEV_GDB, optarg);
2696 break;
2697 case QEMU_OPTION_L:
2698 data_dir = optarg;
2699 break;
2700 case QEMU_OPTION_bios:
2701 bios_name = optarg;
2702 break;
2703 case QEMU_OPTION_singlestep:
2704 singlestep = 1;
2705 break;
2706 case QEMU_OPTION_S:
2707 autostart = 0;
2708 break;
2709 case QEMU_OPTION_k:
2710 keyboard_layout = optarg;
2711 break;
2712 case QEMU_OPTION_localtime:
2713 rtc_utc = 0;
2714 break;
2715 case QEMU_OPTION_vga:
2716 vga_model = optarg;
2717 default_vga = 0;
2718 break;
2719 case QEMU_OPTION_g:
2721 const char *p;
2722 int w, h, depth;
2723 p = optarg;
2724 w = strtol(p, (char **)&p, 10);
2725 if (w <= 0) {
2726 graphic_error:
2727 fprintf(stderr, "qemu: invalid resolution or depth\n");
2728 exit(1);
2730 if (*p != 'x')
2731 goto graphic_error;
2732 p++;
2733 h = strtol(p, (char **)&p, 10);
2734 if (h <= 0)
2735 goto graphic_error;
2736 if (*p == 'x') {
2737 p++;
2738 depth = strtol(p, (char **)&p, 10);
2739 if (depth != 8 && depth != 15 && depth != 16 &&
2740 depth != 24 && depth != 32)
2741 goto graphic_error;
2742 } else if (*p == '\0') {
2743 depth = graphic_depth;
2744 } else {
2745 goto graphic_error;
2748 graphic_width = w;
2749 graphic_height = h;
2750 graphic_depth = depth;
2752 break;
2753 case QEMU_OPTION_echr:
2755 char *r;
2756 term_escape_char = strtol(optarg, &r, 0);
2757 if (r == optarg)
2758 printf("Bad argument to echr\n");
2759 break;
2761 case QEMU_OPTION_monitor:
2762 monitor_parse(optarg, "readline");
2763 default_monitor = 0;
2764 break;
2765 case QEMU_OPTION_qmp:
2766 monitor_parse(optarg, "control");
2767 default_monitor = 0;
2768 break;
2769 case QEMU_OPTION_mon:
2770 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2771 if (!opts) {
2772 exit(1);
2774 default_monitor = 0;
2775 break;
2776 case QEMU_OPTION_chardev:
2777 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2778 if (!opts) {
2779 exit(1);
2781 break;
2782 case QEMU_OPTION_fsdev:
2783 olist = qemu_find_opts("fsdev");
2784 if (!olist) {
2785 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2786 exit(1);
2788 opts = qemu_opts_parse(olist, optarg, 1);
2789 if (!opts) {
2790 fprintf(stderr, "parse error: %s\n", optarg);
2791 exit(1);
2793 break;
2794 case QEMU_OPTION_virtfs: {
2795 QemuOpts *fsdev;
2796 QemuOpts *device;
2797 const char *writeout, *sock_fd, *socket;
2799 olist = qemu_find_opts("virtfs");
2800 if (!olist) {
2801 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2802 exit(1);
2804 opts = qemu_opts_parse(olist, optarg, 1);
2805 if (!opts) {
2806 fprintf(stderr, "parse error: %s\n", optarg);
2807 exit(1);
2810 if (qemu_opt_get(opts, "fsdriver") == NULL ||
2811 qemu_opt_get(opts, "mount_tag") == NULL) {
2812 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2813 exit(1);
2815 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2816 qemu_opt_get(opts, "mount_tag"),
2817 1, NULL);
2818 if (!fsdev) {
2819 fprintf(stderr, "duplicate fsdev id: %s\n",
2820 qemu_opt_get(opts, "mount_tag"));
2821 exit(1);
2824 writeout = qemu_opt_get(opts, "writeout");
2825 if (writeout) {
2826 #ifdef CONFIG_SYNC_FILE_RANGE
2827 qemu_opt_set(fsdev, "writeout", writeout);
2828 #else
2829 fprintf(stderr, "writeout=immediate not supported on "
2830 "this platform\n");
2831 exit(1);
2832 #endif
2834 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
2835 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2836 qemu_opt_set(fsdev, "security_model",
2837 qemu_opt_get(opts, "security_model"));
2838 socket = qemu_opt_get(opts, "socket");
2839 if (socket) {
2840 qemu_opt_set(fsdev, "socket", socket);
2842 sock_fd = qemu_opt_get(opts, "sock_fd");
2843 if (sock_fd) {
2844 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2847 qemu_opt_set_bool(fsdev, "readonly",
2848 qemu_opt_get_bool(opts, "readonly", 0));
2849 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2850 NULL);
2851 qemu_opt_set(device, "driver", "virtio-9p-pci");
2852 qemu_opt_set(device, "fsdev",
2853 qemu_opt_get(opts, "mount_tag"));
2854 qemu_opt_set(device, "mount_tag",
2855 qemu_opt_get(opts, "mount_tag"));
2856 break;
2858 case QEMU_OPTION_virtfs_synth: {
2859 QemuOpts *fsdev;
2860 QemuOpts *device;
2862 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
2863 1, NULL);
2864 if (!fsdev) {
2865 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2866 exit(1);
2868 qemu_opt_set(fsdev, "fsdriver", "synth");
2870 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2871 NULL);
2872 qemu_opt_set(device, "driver", "virtio-9p-pci");
2873 qemu_opt_set(device, "fsdev", "v_synth");
2874 qemu_opt_set(device, "mount_tag", "v_synth");
2875 break;
2877 case QEMU_OPTION_serial:
2878 add_device_config(DEV_SERIAL, optarg);
2879 default_serial = 0;
2880 if (strncmp(optarg, "mon:", 4) == 0) {
2881 default_monitor = 0;
2883 break;
2884 case QEMU_OPTION_watchdog:
2885 if (watchdog) {
2886 fprintf(stderr,
2887 "qemu: only one watchdog option may be given\n");
2888 return 1;
2890 watchdog = optarg;
2891 break;
2892 case QEMU_OPTION_watchdog_action:
2893 if (select_watchdog_action(optarg) == -1) {
2894 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2895 exit(1);
2897 break;
2898 case QEMU_OPTION_virtiocon:
2899 add_device_config(DEV_VIRTCON, optarg);
2900 default_virtcon = 0;
2901 if (strncmp(optarg, "mon:", 4) == 0) {
2902 default_monitor = 0;
2904 break;
2905 case QEMU_OPTION_parallel:
2906 add_device_config(DEV_PARALLEL, optarg);
2907 default_parallel = 0;
2908 if (strncmp(optarg, "mon:", 4) == 0) {
2909 default_monitor = 0;
2911 break;
2912 case QEMU_OPTION_debugcon:
2913 add_device_config(DEV_DEBUGCON, optarg);
2914 break;
2915 case QEMU_OPTION_loadvm:
2916 loadvm = optarg;
2917 break;
2918 case QEMU_OPTION_full_screen:
2919 full_screen = 1;
2920 break;
2921 #ifdef CONFIG_SDL
2922 case QEMU_OPTION_no_frame:
2923 no_frame = 1;
2924 break;
2925 case QEMU_OPTION_alt_grab:
2926 alt_grab = 1;
2927 break;
2928 case QEMU_OPTION_ctrl_grab:
2929 ctrl_grab = 1;
2930 break;
2931 case QEMU_OPTION_no_quit:
2932 no_quit = 1;
2933 break;
2934 case QEMU_OPTION_sdl:
2935 display_type = DT_SDL;
2936 break;
2937 #else
2938 case QEMU_OPTION_no_frame:
2939 case QEMU_OPTION_alt_grab:
2940 case QEMU_OPTION_ctrl_grab:
2941 case QEMU_OPTION_no_quit:
2942 case QEMU_OPTION_sdl:
2943 fprintf(stderr, "SDL support is disabled\n");
2944 exit(1);
2945 #endif
2946 case QEMU_OPTION_pidfile:
2947 pid_file = optarg;
2948 break;
2949 case QEMU_OPTION_win2k_hack:
2950 win2k_install_hack = 1;
2951 break;
2952 case QEMU_OPTION_rtc_td_hack: {
2953 static GlobalProperty slew_lost_ticks[] = {
2955 .driver = "mc146818rtc",
2956 .property = "lost_tick_policy",
2957 .value = "slew",
2959 { /* end of list */ }
2962 qdev_prop_register_global_list(slew_lost_ticks);
2963 break;
2965 case QEMU_OPTION_acpitable:
2966 do_acpitable_option(optarg);
2967 break;
2968 case QEMU_OPTION_smbios:
2969 do_smbios_option(optarg);
2970 break;
2971 case QEMU_OPTION_enable_kvm:
2972 olist = qemu_find_opts("machine");
2973 qemu_opts_parse(olist, "accel=kvm", 0);
2974 break;
2975 case QEMU_OPTION_machine:
2976 olist = qemu_find_opts("machine");
2977 opts = qemu_opts_parse(olist, optarg, 1);
2978 if (!opts) {
2979 fprintf(stderr, "parse error: %s\n", optarg);
2980 exit(1);
2982 optarg = qemu_opt_get(opts, "type");
2983 if (optarg) {
2984 machine = machine_parse(optarg);
2986 break;
2987 case QEMU_OPTION_usb:
2988 usb_enabled = 1;
2989 break;
2990 case QEMU_OPTION_usbdevice:
2991 usb_enabled = 1;
2992 add_device_config(DEV_USB, optarg);
2993 break;
2994 case QEMU_OPTION_device:
2995 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2996 exit(1);
2998 break;
2999 case QEMU_OPTION_smp:
3000 smp_parse(optarg);
3001 if (smp_cpus < 1) {
3002 fprintf(stderr, "Invalid number of CPUs\n");
3003 exit(1);
3005 if (max_cpus < smp_cpus) {
3006 fprintf(stderr, "maxcpus must be equal to or greater than "
3007 "smp\n");
3008 exit(1);
3010 if (max_cpus > 255) {
3011 fprintf(stderr, "Unsupported number of maxcpus\n");
3012 exit(1);
3014 break;
3015 case QEMU_OPTION_vnc:
3016 #ifdef CONFIG_VNC
3017 display_remote++;
3018 vnc_display = optarg;
3019 #else
3020 fprintf(stderr, "VNC support is disabled\n");
3021 exit(1);
3022 #endif
3023 break;
3024 case QEMU_OPTION_no_acpi:
3025 acpi_enabled = 0;
3026 break;
3027 case QEMU_OPTION_no_hpet:
3028 no_hpet = 1;
3029 break;
3030 case QEMU_OPTION_balloon:
3031 if (balloon_parse(optarg) < 0) {
3032 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3033 exit(1);
3035 break;
3036 case QEMU_OPTION_no_reboot:
3037 no_reboot = 1;
3038 break;
3039 case QEMU_OPTION_no_shutdown:
3040 no_shutdown = 1;
3041 break;
3042 case QEMU_OPTION_show_cursor:
3043 cursor_hide = 0;
3044 break;
3045 case QEMU_OPTION_uuid:
3046 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3047 fprintf(stderr, "Fail to parse UUID string."
3048 " Wrong format.\n");
3049 exit(1);
3051 break;
3052 case QEMU_OPTION_option_rom:
3053 if (nb_option_roms >= MAX_OPTION_ROMS) {
3054 fprintf(stderr, "Too many option ROMs\n");
3055 exit(1);
3057 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3058 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3059 option_rom[nb_option_roms].bootindex =
3060 qemu_opt_get_number(opts, "bootindex", -1);
3061 if (!option_rom[nb_option_roms].name) {
3062 fprintf(stderr, "Option ROM file is not specified\n");
3063 exit(1);
3065 nb_option_roms++;
3066 break;
3067 case QEMU_OPTION_semihosting:
3068 semihosting_enabled = 1;
3069 break;
3070 case QEMU_OPTION_name:
3071 qemu_name = g_strdup(optarg);
3073 char *p = strchr(qemu_name, ',');
3074 if (p != NULL) {
3075 *p++ = 0;
3076 if (strncmp(p, "process=", 8)) {
3077 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3078 exit(1);
3080 p += 8;
3081 os_set_proc_name(p);
3084 break;
3085 case QEMU_OPTION_prom_env:
3086 if (nb_prom_envs >= MAX_PROM_ENVS) {
3087 fprintf(stderr, "Too many prom variables\n");
3088 exit(1);
3090 prom_envs[nb_prom_envs] = optarg;
3091 nb_prom_envs++;
3092 break;
3093 case QEMU_OPTION_old_param:
3094 old_param = 1;
3095 break;
3096 case QEMU_OPTION_clock:
3097 configure_alarms(optarg);
3098 break;
3099 case QEMU_OPTION_startdate:
3100 configure_rtc_date_offset(optarg, 1);
3101 break;
3102 case QEMU_OPTION_rtc:
3103 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3104 if (!opts) {
3105 exit(1);
3107 configure_rtc(opts);
3108 break;
3109 case QEMU_OPTION_tb_size:
3110 tcg_tb_size = strtol(optarg, NULL, 0);
3111 if (tcg_tb_size < 0) {
3112 tcg_tb_size = 0;
3114 break;
3115 case QEMU_OPTION_icount:
3116 icount_option = optarg;
3117 break;
3118 case QEMU_OPTION_incoming:
3119 incoming = optarg;
3120 runstate_set(RUN_STATE_INMIGRATE);
3121 break;
3122 case QEMU_OPTION_nodefaults:
3123 default_serial = 0;
3124 default_parallel = 0;
3125 default_virtcon = 0;
3126 default_monitor = 0;
3127 default_net = 0;
3128 default_floppy = 0;
3129 default_cdrom = 0;
3130 default_sdcard = 0;
3131 default_vga = 0;
3132 break;
3133 case QEMU_OPTION_xen_domid:
3134 if (!(xen_available())) {
3135 printf("Option %s not supported for this target\n", popt->name);
3136 exit(1);
3138 xen_domid = atoi(optarg);
3139 break;
3140 case QEMU_OPTION_xen_create:
3141 if (!(xen_available())) {
3142 printf("Option %s not supported for this target\n", popt->name);
3143 exit(1);
3145 xen_mode = XEN_CREATE;
3146 break;
3147 case QEMU_OPTION_xen_attach:
3148 if (!(xen_available())) {
3149 printf("Option %s not supported for this target\n", popt->name);
3150 exit(1);
3152 xen_mode = XEN_ATTACH;
3153 break;
3154 case QEMU_OPTION_trace:
3156 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3157 if (!opts) {
3158 exit(1);
3160 trace_events = qemu_opt_get(opts, "events");
3161 trace_file = qemu_opt_get(opts, "file");
3162 break;
3164 case QEMU_OPTION_readconfig:
3166 int ret = qemu_read_config_file(optarg);
3167 if (ret < 0) {
3168 fprintf(stderr, "read config %s: %s\n", optarg,
3169 strerror(-ret));
3170 exit(1);
3172 break;
3174 case QEMU_OPTION_spice:
3175 olist = qemu_find_opts("spice");
3176 if (!olist) {
3177 fprintf(stderr, "spice is not supported by this qemu build.\n");
3178 exit(1);
3180 opts = qemu_opts_parse(olist, optarg, 0);
3181 if (!opts) {
3182 fprintf(stderr, "parse error: %s\n", optarg);
3183 exit(1);
3185 break;
3186 case QEMU_OPTION_writeconfig:
3188 FILE *fp;
3189 if (strcmp(optarg, "-") == 0) {
3190 fp = stdout;
3191 } else {
3192 fp = fopen(optarg, "w");
3193 if (fp == NULL) {
3194 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3195 exit(1);
3198 qemu_config_write(fp);
3199 fclose(fp);
3200 break;
3202 case QEMU_OPTION_qtest:
3203 qtest_chrdev = optarg;
3204 break;
3205 case QEMU_OPTION_qtest_log:
3206 qtest_log = optarg;
3207 break;
3208 default:
3209 os_parse_cmd_args(popt->index, optarg);
3213 loc_set_none();
3215 /* Init CPU def lists, based on config
3216 * - Must be called after all the qemu_read_config_file() calls
3217 * - Must be called before list_cpus()
3218 * - Must be called before machine->init()
3220 cpudef_init();
3222 if (cpu_model && *cpu_model == '?') {
3223 list_cpus(stdout, &fprintf, cpu_model);
3224 exit(0);
3227 /* Open the logfile at this point, if necessary. We can't open the logfile
3228 * when encountering either of the logging options (-d or -D) because the
3229 * other one may be encountered later on the command line, changing the
3230 * location or level of logging.
3232 if (log_mask) {
3233 if (log_file) {
3234 set_cpu_log_filename(log_file);
3236 set_cpu_log(log_mask);
3239 if (!trace_backend_init(trace_events, trace_file)) {
3240 exit(1);
3243 /* If no data_dir is specified then try to find it relative to the
3244 executable path. */
3245 if (!data_dir) {
3246 data_dir = os_find_datadir(argv[0]);
3248 /* If all else fails use the install path specified when building. */
3249 if (!data_dir) {
3250 data_dir = CONFIG_QEMU_DATADIR;
3253 if (machine == NULL) {
3254 fprintf(stderr, "No machine found.\n");
3255 exit(1);
3259 * Default to max_cpus = smp_cpus, in case the user doesn't
3260 * specify a max_cpus value.
3262 if (!max_cpus)
3263 max_cpus = smp_cpus;
3265 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3266 if (smp_cpus > machine->max_cpus) {
3267 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3268 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3269 machine->max_cpus);
3270 exit(1);
3274 * Get the default machine options from the machine if it is not already
3275 * specified either by the configuration file or by the command line.
3277 if (machine->default_machine_opts) {
3278 qemu_opts_set_defaults(qemu_find_opts("machine"),
3279 machine->default_machine_opts, 0);
3282 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3283 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3285 if (machine->no_serial) {
3286 default_serial = 0;
3288 if (machine->no_parallel) {
3289 default_parallel = 0;
3291 if (!machine->use_virtcon) {
3292 default_virtcon = 0;
3294 if (machine->no_floppy) {
3295 default_floppy = 0;
3297 if (machine->no_cdrom) {
3298 default_cdrom = 0;
3300 if (machine->no_sdcard) {
3301 default_sdcard = 0;
3304 if (display_type == DT_NOGRAPHIC) {
3305 if (default_parallel)
3306 add_device_config(DEV_PARALLEL, "null");
3307 if (default_serial && default_monitor) {
3308 add_device_config(DEV_SERIAL, "mon:stdio");
3309 } else if (default_virtcon && default_monitor) {
3310 add_device_config(DEV_VIRTCON, "mon:stdio");
3311 } else {
3312 if (default_serial)
3313 add_device_config(DEV_SERIAL, "stdio");
3314 if (default_virtcon)
3315 add_device_config(DEV_VIRTCON, "stdio");
3316 if (default_monitor)
3317 monitor_parse("stdio", "readline");
3319 } else {
3320 if (default_serial)
3321 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3322 if (default_parallel)
3323 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3324 if (default_monitor)
3325 monitor_parse("vc:80Cx24C", "readline");
3326 if (default_virtcon)
3327 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3330 socket_init();
3332 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3333 exit(1);
3334 #ifdef CONFIG_VIRTFS
3335 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3336 exit(1);
3338 #endif
3340 os_daemonize();
3342 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3343 os_pidfile_error();
3344 exit(1);
3347 /* init the memory */
3348 if (ram_size == 0) {
3349 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3352 configure_accelerator();
3354 qemu_init_cpu_loop();
3355 if (qemu_init_main_loop()) {
3356 fprintf(stderr, "qemu_init_main_loop failed\n");
3357 exit(1);
3360 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
3361 if (machine_opts) {
3362 kernel_filename = qemu_opt_get(machine_opts, "kernel");
3363 initrd_filename = qemu_opt_get(machine_opts, "initrd");
3364 kernel_cmdline = qemu_opt_get(machine_opts, "append");
3365 } else {
3366 kernel_filename = initrd_filename = kernel_cmdline = NULL;
3369 if (!kernel_cmdline) {
3370 kernel_cmdline = "";
3373 linux_boot = (kernel_filename != NULL);
3375 if (!linux_boot && *kernel_cmdline != '\0') {
3376 fprintf(stderr, "-append only allowed with -kernel option\n");
3377 exit(1);
3380 if (!linux_boot && initrd_filename != NULL) {
3381 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3382 exit(1);
3385 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
3386 fprintf(stderr, "-dtb only allowed with -kernel option\n");
3387 exit(1);
3390 os_set_line_buffering();
3392 if (init_timer_alarm() < 0) {
3393 fprintf(stderr, "could not initialize alarm timer\n");
3394 exit(1);
3397 #ifdef CONFIG_SPICE
3398 /* spice needs the timers to be initialized by this point */
3399 qemu_spice_init();
3400 #endif
3402 if (icount_option && (kvm_enabled() || xen_enabled())) {
3403 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3404 exit(1);
3406 configure_icount(icount_option);
3408 if (net_init_clients() < 0) {
3409 exit(1);
3412 /* init the bluetooth world */
3413 if (foreach_device_config(DEV_BT, bt_parse))
3414 exit(1);
3416 if (!xen_enabled()) {
3417 /* On 32-bit hosts, QEMU is limited by virtual address space */
3418 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3419 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3420 exit(1);
3424 cpu_exec_init_all();
3426 bdrv_init_with_whitelist();
3428 blk_mig_init();
3430 /* open the virtual block devices */
3431 if (snapshot)
3432 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3433 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3434 exit(1);
3436 default_drive(default_cdrom, snapshot, machine->use_scsi,
3437 IF_DEFAULT, 2, CDROM_OPTS);
3438 default_drive(default_floppy, snapshot, machine->use_scsi,
3439 IF_FLOPPY, 0, FD_OPTS);
3440 default_drive(default_sdcard, snapshot, machine->use_scsi,
3441 IF_SD, 0, SD_OPTS);
3443 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3444 ram_load, NULL);
3446 if (nb_numa_nodes > 0) {
3447 int i;
3449 if (nb_numa_nodes > MAX_NODES) {
3450 nb_numa_nodes = MAX_NODES;
3453 /* If no memory size if given for any node, assume the default case
3454 * and distribute the available memory equally across all nodes
3456 for (i = 0; i < nb_numa_nodes; i++) {
3457 if (node_mem[i] != 0)
3458 break;
3460 if (i == nb_numa_nodes) {
3461 uint64_t usedmem = 0;
3463 /* On Linux, the each node's border has to be 8MB aligned,
3464 * the final node gets the rest.
3466 for (i = 0; i < nb_numa_nodes - 1; i++) {
3467 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3468 usedmem += node_mem[i];
3470 node_mem[i] = ram_size - usedmem;
3473 for (i = 0; i < nb_numa_nodes; i++) {
3474 if (node_cpumask[i] != 0)
3475 break;
3477 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3478 * must cope with this anyway, because there are BIOSes out there in
3479 * real machines which also use this scheme.
3481 if (i == nb_numa_nodes) {
3482 for (i = 0; i < max_cpus; i++) {
3483 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3488 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3489 exit(1);
3492 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3493 exit(1);
3494 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3495 exit(1);
3496 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3497 exit(1);
3498 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3499 exit(1);
3501 /* If no default VGA is requested, the default is "none". */
3502 if (default_vga && cirrus_vga_available()) {
3503 vga_model = "cirrus";
3505 select_vgahw(vga_model);
3507 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3508 exit(0);
3510 if (watchdog) {
3511 i = select_watchdog(watchdog);
3512 if (i > 0)
3513 exit (i == 1 ? 1 : 0);
3516 if (machine->compat_props) {
3517 qdev_prop_register_global_list(machine->compat_props);
3519 qemu_add_globals();
3521 qdev_machine_init();
3523 machine->init(ram_size, boot_devices,
3524 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3526 cpu_synchronize_all_post_init();
3528 set_numa_modes();
3530 current_machine = machine;
3532 /* init USB devices */
3533 if (usb_enabled) {
3534 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3535 exit(1);
3538 /* init generic devices */
3539 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3540 exit(1);
3542 net_check_clients();
3544 /* just use the first displaystate for the moment */
3545 ds = get_displaystate();
3547 if (using_spice)
3548 display_remote++;
3549 if (display_type == DT_DEFAULT && !display_remote) {
3550 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3551 display_type = DT_SDL;
3552 #elif defined(CONFIG_VNC)
3553 vnc_display = "localhost:0,to=99";
3554 show_vnc_port = 1;
3555 #else
3556 display_type = DT_NONE;
3557 #endif
3561 /* init local displays */
3562 switch (display_type) {
3563 case DT_NOGRAPHIC:
3564 break;
3565 #if defined(CONFIG_CURSES)
3566 case DT_CURSES:
3567 curses_display_init(ds, full_screen);
3568 break;
3569 #endif
3570 #if defined(CONFIG_SDL)
3571 case DT_SDL:
3572 sdl_display_init(ds, full_screen, no_frame);
3573 break;
3574 #elif defined(CONFIG_COCOA)
3575 case DT_SDL:
3576 cocoa_display_init(ds, full_screen);
3577 break;
3578 #endif
3579 default:
3580 break;
3583 /* must be after terminal init, SDL library changes signal handlers */
3584 os_setup_signal_handling();
3586 #ifdef CONFIG_VNC
3587 /* init remote displays */
3588 if (vnc_display) {
3589 vnc_display_init(ds);
3590 if (vnc_display_open(ds, vnc_display) < 0)
3591 exit(1);
3593 if (show_vnc_port) {
3594 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3597 #endif
3598 #ifdef CONFIG_SPICE
3599 if (using_spice && !qxl_enabled) {
3600 qemu_spice_display_init(ds);
3602 #endif
3604 /* display setup */
3605 dpy_resize(ds);
3606 dcl = ds->listeners;
3607 while (dcl != NULL) {
3608 if (dcl->dpy_refresh != NULL) {
3609 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3610 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3611 break;
3613 dcl = dcl->next;
3615 text_consoles_set_display(ds);
3617 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
3618 exit(1);
3621 qdev_machine_creation_done();
3623 if (rom_load_all() != 0) {
3624 fprintf(stderr, "rom loading failed\n");
3625 exit(1);
3628 /* TODO: once all bus devices are qdevified, this should be done
3629 * when bus is created by qdev.c */
3630 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3631 qemu_run_machine_init_done_notifiers();
3633 qemu_system_reset(VMRESET_SILENT);
3634 if (loadvm) {
3635 if (load_vmstate(loadvm) < 0) {
3636 autostart = 0;
3640 if (incoming) {
3641 Error *errp = NULL;
3642 int ret = qemu_start_incoming_migration(incoming, &errp);
3643 if (ret < 0) {
3644 if (error_is_set(&errp)) {
3645 fprintf(stderr, "Migrate: %s\n", error_get_pretty(errp));
3646 error_free(errp);
3648 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3649 incoming, ret);
3650 exit(ret);
3652 } else if (autostart) {
3653 vm_start();
3656 os_setup_post();
3658 resume_all_vcpus();
3659 main_loop();
3660 bdrv_close_all();
3661 pause_all_vcpus();
3662 net_cleanup();
3663 res_free();
3665 return 0;