blob: ba4a151adaf1fa9085aa08958f939bd2943b2734 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * 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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000038#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000039#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000042#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000043#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000044#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000047#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000048#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000049#include <sys/select.h>
Prerna Saxenaab6540d2010-08-09 11:48:32 +010050
Juan Quintela71e72a12009-07-27 16:12:56 +020051#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000052#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010053#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000054#include <libutil.h>
Juergen Lock92c0e652010-03-25 22:07:12 +010055#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000056#else
57#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000058#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010059#else
blueswir1223f0d72008-09-30 18:12:18 +000060#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000061#include <pty.h>
62#include <malloc.h>
thsbd494f42007-09-16 20:03:23 +000063
bellarde57a8c02005-11-10 23:58:52 +000064#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000065#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000066#endif
67#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000068#include <sys/stat.h>
69#include <sys/ethernet.h>
70#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000071#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000072#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>
bellard67b915a2004-03-31 23:37:16 +000080#endif
bellard7d3505c2004-05-12 19:32:15 +000081#endif
bellardec530c82006-04-25 22:36:06 +000082#endif
bellard67b915a2004-03-31 23:37:16 +000083
blueswir19892fbf2008-08-24 10:34:20 +000084#if defined(__OpenBSD__)
85#include <util.h>
86#endif
87
ths8a16d272008-07-19 09:56:24 +000088#if defined(CONFIG_VDE)
89#include <libvdeplug.h>
90#endif
91
bellard67b915a2004-03-31 23:37:16 +000092#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000093#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000094#endif
95
bellard73332e52004-04-04 20:22:28 +000096#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +020097#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +020098#include <SDL.h>
malc880fec52009-02-15 20:18:41 +000099int qemu_main(int argc, char **argv, char **envp);
100int main(int argc, char **argv)
101{
Stefan Weil59a36a22009-06-18 20:11:03 +0200102 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000103}
104#undef main
105#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000106#endif
bellard73332e52004-04-04 20:22:28 +0000107#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000108
bellard5b0753e2005-03-01 21:37:28 +0000109#ifdef CONFIG_COCOA
110#undef main
111#define main qemu_main
112#endif /* CONFIG_COCOA */
113
Anthony Liguori69e5bb62011-08-22 08:12:52 -0500114#include <glib.h>
115
blueswir1511d2b12009-03-07 15:32:56 +0000116#include "hw/hw.h"
117#include "hw/boards.h"
118#include "hw/usb.h"
119#include "hw/pcmcia.h"
120#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000121#include "hw/isa.h"
122#include "hw/baum.h"
123#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100124#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000125#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000126#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200127#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200128#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000129#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000130#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000131#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000132#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"
Markus Armbruster666daa62010-06-02 18:48:27 +0200140#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200141#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000142#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000143#include "audio/audio.h"
144#include "migration.h"
145#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200146#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200147#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200148#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200149#include "qemu-options.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700150#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530151#include "fsdev/qemu-fsdev.h"
152#endif
blueswir1511d2b12009-03-07 15:32:56 +0000153
bellard0824d6f2003-06-24 13:42:40 +0000154#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000155
blueswir1511d2b12009-03-07 15:32:56 +0000156#include "qemu_socket.h"
157
Jan Kiszkad918f232009-06-24 14:42:30 +0200158#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000159
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000160#include "trace.h"
Lluíse4858972011-08-31 20:31:03 +0200161#include "trace/control.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000162#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000163#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000164#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000165
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300166#include "ui/qemu-spice.h"
167
blueswir19dc63a12008-10-04 07:25:46 +0000168//#define DEBUG_NET
169//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000170
bellard1bfe8562004-07-08 21:17:50 +0000171#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000172
Amit Shah98b19252010-01-20 00:36:52 +0530173#define MAX_VIRTIO_CONSOLES 1
174
Paul Brook5cea8592009-05-30 00:52:44 +0100175static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000176const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000177enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500178DisplayType display_type = DT_DEFAULT;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +0200179int display_remote = 0;
bellard3d11d0e2004-12-12 16:56:30 +0000180const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500181ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300182const char *mem_path = NULL;
183#ifdef MAP_POPULATE
184int mem_prealloc = 0; /* force preallocation of physical target memory */
185#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000186int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000187NICInfo nd_table[MAX_NICS];
Paolo Bonzinid399f672009-07-27 23:17:51 +0200188int autostart;
balrogf6503052008-02-17 11:42:19 +0000189static int rtc_utc = 1;
190static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200191QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100192int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000193static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000194#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000195static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000196#endif
ths667acca2006-12-11 02:08:05 +0000197int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000198CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000199CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000200CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000201int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000202int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000203int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000204int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000205int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200206int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200207int smp_cores = 1;
208int smp_threads = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +0100209#ifdef CONFIG_VNC
ths73fc9742006-12-22 02:09:07 +0000210const char *vnc_display;
Jes Sorensen821601e2011-03-16 13:33:36 +0100211#endif
bellard6515b202006-05-03 22:02:44 +0000212int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000213int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000214int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000215int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000216int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000217int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000218int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200219uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200220const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200221QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000222int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000223int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000224int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000225const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000226int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500227int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000228unsigned int nb_prom_envs = 0;
229const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200230int boot_menu;
wayne3d3b8302011-07-27 18:04:55 +0800231uint8_t *boot_splash_filedata;
232int boot_splash_filedata_size;
233uint8_t qemu_extra_params_fw[2];
bellard0824d6f2003-06-24 13:42:40 +0000234
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200235typedef struct FWBootEntry FWBootEntry;
236
237struct FWBootEntry {
238 QTAILQ_ENTRY(FWBootEntry) link;
239 int32_t bootindex;
240 DeviceState *dev;
241 char *suffix;
242};
243
244QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
245
aliguori268a3622009-04-21 22:30:27 +0000246int nb_numa_nodes;
247uint64_t node_mem[MAX_NODES];
248uint64_t node_cpumask[MAX_NODES];
249
blueswir19043b622009-01-21 19:28:13 +0000250static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000251
blueswir18fcb1b92008-09-18 18:29:08 +0000252uint8_t qemu_uuid[16];
253
Jan Kiszka76e30d02009-07-02 00:19:02 +0200254static QEMUBootSetHandler *boot_set_handler;
255static void *boot_set_opaque;
256
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200257static NotifierList exit_notifiers =
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
259
Gleb Natapov4cab9462010-12-08 13:35:08 +0200260static NotifierList machine_init_done_notifiers =
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
262
Anthony PERARD303d4e82010-09-21 20:05:31 +0100263static int tcg_allowed = 1;
Blue Swirld745bef2010-03-29 19:23:49 +0000264int kvm_allowed = 0;
Anthony PERARD3285cf42010-08-19 12:27:56 +0100265int xen_allowed = 0;
Blue Swirld745bef2010-03-29 19:23:49 +0000266uint32_t xen_domid;
267enum xen_mode xen_mode = XEN_EMULATE;
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200268static int tcg_tb_size;
Blue Swirld745bef2010-03-29 19:23:49 +0000269
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100270static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100271static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100272static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100273static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100274static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100275static int default_floppy = 1;
276static int default_cdrom = 1;
277static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100278
279static struct {
280 const char *driver;
281 int *flag;
282} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100283 { .driver = "isa-serial", .flag = &default_serial },
284 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100285 { .driver = "isa-fdc", .flag = &default_floppy },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200286 { .driver = "ide-cd", .flag = &default_cdrom },
287 { .driver = "ide-hd", .flag = &default_cdrom },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100288 { .driver = "ide-drive", .flag = &default_cdrom },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200289 { .driver = "scsi-cd", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530290 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
291 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
292 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100293 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100294 { .driver = "cirrus-vga", .flag = &default_vga },
295 { .driver = "vmware-svga", .flag = &default_vga },
Markus Armbruster0826c712011-06-07 10:34:30 +0200296 { .driver = "isa-vga", .flag = &default_vga },
Gerd Hoffmann42138042011-05-16 09:28:58 +0200297 { .driver = "qxl-vga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100298};
299
wayne3d3b8302011-07-27 18:04:55 +0800300static void res_free(void)
301{
302 if (boot_splash_filedata != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500303 g_free(boot_splash_filedata);
wayne3d3b8302011-07-27 18:04:55 +0800304 boot_splash_filedata = NULL;
305 }
306}
307
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100308static int default_driver_check(QemuOpts *opts, void *opaque)
309{
310 const char *driver = qemu_opt_get(opts, "driver");
311 int i;
312
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;
319 }
320 return 0;
321}
322
bellard0824d6f2003-06-24 13:42:40 +0000323/***********************************************************/
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300324/* QEMU state */
325
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300326static RunState current_run_state = RUN_STATE_PRELAUNCH;
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300327
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300328typedef struct {
329 RunState from;
330 RunState to;
331} RunStateTransition;
332
333static const RunStateTransition runstate_transitions_def[] = {
334 /* from -> to */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300335 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300336
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300337 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
338 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300339
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300340 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300341
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300342 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300343
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300344 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300345
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300346 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300347
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300348 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
349 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
350 { RUN_STATE_PRELAUNCH, RUN_STATE_POSTMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300351
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300352 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
353 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300354
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300355 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300356
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300357 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
358 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
359 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
360 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
361 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
362 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
363 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
364 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
365 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300366
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300367 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300368
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300369 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300370
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300371 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300372
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300373 { RUN_STATE_MAX, RUN_STATE_MAX },
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300374};
375
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300376static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300377
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300378static const char *const runstate_name_tbl[RUN_STATE_MAX] = {
379 [RUN_STATE_DEBUG] = "debug",
380 [RUN_STATE_INMIGRATE] = "incoming-migration",
381 [RUN_STATE_INTERNAL_ERROR] = "internal-error",
382 [RUN_STATE_IO_ERROR] = "io-error",
383 [RUN_STATE_PAUSED] = "paused",
384 [RUN_STATE_POSTMIGRATE] = "post-migrate",
385 [RUN_STATE_PRELAUNCH] = "prelaunch",
386 [RUN_STATE_FINISH_MIGRATE] = "finish-migrate",
387 [RUN_STATE_RESTORE_VM] = "restore-vm",
388 [RUN_STATE_RUNNING] = "running",
389 [RUN_STATE_SAVE_VM] = "save-vm",
390 [RUN_STATE_SHUTDOWN] = "shutdown",
391 [RUN_STATE_WATCHDOG] = "watchdog",
Luiz Capitulino9e37b9d2011-08-29 16:02:57 -0300392};
393
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300394bool runstate_check(RunState state)
395{
396 return current_run_state == state;
397}
398
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300399void runstate_init(void)
400{
401 const RunStateTransition *p;
402
403 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
404
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300405 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300406 runstate_valid_transitions[p->from][p->to] = true;
407 }
408}
409
410/* This function will abort() on invalid state transitions */
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300411void runstate_set(RunState new_state)
412{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300413 if (new_state >= RUN_STATE_MAX ||
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -0300414 !runstate_valid_transitions[current_run_state][new_state]) {
415 fprintf(stderr, "invalid runstate transition\n");
416 abort();
417 }
418
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300419 current_run_state = new_state;
420}
421
Luiz Capitulino9e37b9d2011-08-29 16:02:57 -0300422const char *runstate_as_string(void)
423{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300424 assert(current_run_state < RUN_STATE_MAX);
Luiz Capitulino9e37b9d2011-08-29 16:02:57 -0300425 return runstate_name_tbl[current_run_state];
426}
427
Luiz Capitulino13548692011-07-29 15:36:43 -0300428int runstate_is_running(void)
429{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -0300430 return runstate_check(RUN_STATE_RUNNING);
Luiz Capitulino13548692011-07-29 15:36:43 -0300431}
432
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300433/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100434/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000435
bellardc4b1fcc2004-03-14 21:44:30 +0000436/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000437/* host time/date access */
438void qemu_get_timedate(struct tm *tm, int offset)
439{
440 time_t ti;
441 struct tm *ret;
442
443 time(&ti);
444 ti += offset;
445 if (rtc_date_offset == -1) {
446 if (rtc_utc)
447 ret = gmtime(&ti);
448 else
449 ret = localtime(&ti);
450 } else {
451 ti -= rtc_date_offset;
452 ret = gmtime(&ti);
453 }
454
455 memcpy(tm, ret, sizeof(struct tm));
456}
457
458int qemu_timedate_diff(struct tm *tm)
459{
460 time_t seconds;
461
462 if (rtc_date_offset == -1)
463 if (rtc_utc)
464 seconds = mktimegm(tm);
465 else
466 seconds = mktime(tm);
467 else
468 seconds = mktimegm(tm) + rtc_date_offset;
469
470 return seconds - time(NULL);
471}
472
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300473void rtc_change_mon_event(struct tm *tm)
474{
475 QObject *data;
476
477 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
478 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
479 qobject_decref(data);
480}
481
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200482static void configure_rtc_date_offset(const char *startdate, int legacy)
483{
484 time_t rtc_start_date;
485 struct tm tm;
486
487 if (!strcmp(startdate, "now") && legacy) {
488 rtc_date_offset = -1;
489 } else {
490 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
491 &tm.tm_year,
492 &tm.tm_mon,
493 &tm.tm_mday,
494 &tm.tm_hour,
495 &tm.tm_min,
496 &tm.tm_sec) == 6) {
497 /* OK */
498 } else if (sscanf(startdate, "%d-%d-%d",
499 &tm.tm_year,
500 &tm.tm_mon,
501 &tm.tm_mday) == 3) {
502 tm.tm_hour = 0;
503 tm.tm_min = 0;
504 tm.tm_sec = 0;
505 } else {
506 goto date_fail;
507 }
508 tm.tm_year -= 1900;
509 tm.tm_mon--;
510 rtc_start_date = mktimegm(&tm);
511 if (rtc_start_date == -1) {
512 date_fail:
513 fprintf(stderr, "Invalid date format. Valid formats are:\n"
514 "'2006-06-17T16:01:21' or '2006-06-17'\n");
515 exit(1);
516 }
517 rtc_date_offset = time(NULL) - rtc_start_date;
518 }
519}
520
521static void configure_rtc(QemuOpts *opts)
522{
523 const char *value;
524
525 value = qemu_opt_get(opts, "base");
526 if (value) {
527 if (!strcmp(value, "utc")) {
528 rtc_utc = 1;
529 } else if (!strcmp(value, "localtime")) {
530 rtc_utc = 0;
531 } else {
532 configure_rtc_date_offset(value, 0);
533 }
534 }
Jan Kiszka68752042009-09-15 13:36:04 +0200535 value = qemu_opt_get(opts, "clock");
536 if (value) {
537 if (!strcmp(value, "host")) {
538 rtc_clock = host_clock;
539 } else if (!strcmp(value, "vm")) {
540 rtc_clock = vm_clock;
541 } else {
542 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
543 exit(1);
544 }
545 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200546 value = qemu_opt_get(opts, "driftfix");
547 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000548 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200549 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000550 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200551 rtc_td_hack = 0;
552 } else {
553 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
554 exit(1);
555 }
556 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200557}
558
balrog1ae26a12008-09-28 23:19:47 +0000559/***********************************************************/
560/* Bluetooth support */
561static int nb_hcis;
562static int cur_hci;
563static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000564
balrog1ae26a12008-09-28 23:19:47 +0000565static struct bt_vlan_s {
566 struct bt_scatternet_s net;
567 int id;
568 struct bt_vlan_s *next;
569} *first_bt_vlan;
570
571/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000572static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000573{
574 struct bt_vlan_s **pvlan, *vlan;
575 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
576 if (vlan->id == id)
577 return &vlan->net;
578 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500579 vlan = g_malloc0(sizeof(struct bt_vlan_s));
balrog1ae26a12008-09-28 23:19:47 +0000580 vlan->id = id;
581 pvlan = &first_bt_vlan;
582 while (*pvlan != NULL)
583 pvlan = &(*pvlan)->next;
584 *pvlan = vlan;
585 return &vlan->net;
586}
587
588static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
589{
590}
591
592static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
593{
594 return -ENOTSUP;
595}
596
597static struct HCIInfo null_hci = {
598 .cmd_send = null_hci_send,
599 .sco_send = null_hci_send,
600 .acl_send = null_hci_send,
601 .bdaddr_set = null_hci_addr_set,
602};
603
604struct HCIInfo *qemu_next_hci(void)
605{
606 if (cur_hci == nb_hcis)
607 return &null_hci;
608
609 return hci_table[cur_hci++];
610}
611
balrogdc72ac12008-11-09 00:04:26 +0000612static struct HCIInfo *hci_init(const char *str)
613{
614 char *endp;
615 struct bt_scatternet_s *vlan = 0;
616
617 if (!strcmp(str, "null"))
618 /* null */
619 return &null_hci;
620 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
621 /* host[:hciN] */
622 return bt_host_hci(str[4] ? str + 5 : "hci0");
623 else if (!strncmp(str, "hci", 3)) {
624 /* hci[,vlan=n] */
625 if (str[3]) {
626 if (!strncmp(str + 3, ",vlan=", 6)) {
627 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
628 if (*endp)
629 vlan = 0;
630 }
631 } else
632 vlan = qemu_find_bt_vlan(0);
633 if (vlan)
634 return bt_new_hci(vlan);
635 }
636
637 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
638
639 return 0;
640}
641
642static int bt_hci_parse(const char *str)
643{
644 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500645 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000646
647 if (nb_hcis >= MAX_NICS) {
648 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
649 return -1;
650 }
651
652 hci = hci_init(str);
653 if (!hci)
654 return -1;
655
656 bdaddr.b[0] = 0x52;
657 bdaddr.b[1] = 0x54;
658 bdaddr.b[2] = 0x00;
659 bdaddr.b[3] = 0x12;
660 bdaddr.b[4] = 0x34;
661 bdaddr.b[5] = 0x56 + nb_hcis;
662 hci->bdaddr_set(hci, bdaddr.b);
663
664 hci_table[nb_hcis++] = hci;
665
666 return 0;
667}
668
669static void bt_vhci_add(int vlan_id)
670{
671 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
672
673 if (!vlan->slave)
674 fprintf(stderr, "qemu: warning: adding a VHCI to "
675 "an empty scatternet %i\n", vlan_id);
676
677 bt_vhci_init(bt_new_hci(vlan));
678}
679
680static struct bt_device_s *bt_device_add(const char *opt)
681{
682 struct bt_scatternet_s *vlan;
683 int vlan_id = 0;
684 char *endp = strstr(opt, ",vlan=");
685 int len = (endp ? endp - opt : strlen(opt)) + 1;
686 char devname[10];
687
688 pstrcpy(devname, MIN(sizeof(devname), len), opt);
689
690 if (endp) {
691 vlan_id = strtol(endp + 6, &endp, 0);
692 if (*endp) {
693 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
694 return 0;
695 }
696 }
697
698 vlan = qemu_find_bt_vlan(vlan_id);
699
700 if (!vlan->slave)
701 fprintf(stderr, "qemu: warning: adding a slave device to "
702 "an empty scatternet %i\n", vlan_id);
703
704 if (!strcmp(devname, "keyboard"))
705 return bt_keyboard_init(vlan);
706
707 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
708 return 0;
709}
710
711static int bt_parse(const char *opt)
712{
713 const char *endp, *p;
714 int vlan;
715
716 if (strstart(opt, "hci", &endp)) {
717 if (!*endp || *endp == ',') {
718 if (*endp)
719 if (!strstart(endp, ",vlan=", 0))
720 opt = endp + 1;
721
722 return bt_hci_parse(opt);
723 }
724 } else if (strstart(opt, "vhci", &endp)) {
725 if (!*endp || *endp == ',') {
726 if (*endp) {
727 if (strstart(endp, ",vlan=", &p)) {
728 vlan = strtol(p, (char **) &endp, 0);
729 if (*endp) {
730 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
731 return 1;
732 }
733 } else {
734 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
735 return 1;
736 }
737 } else
738 vlan = 0;
739
740 bt_vhci_add(vlan);
741 return 0;
742 }
743 } else if (strstart(opt, "device:", &endp))
744 return !bt_device_add(endp);
745
746 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
747 return 1;
748}
749
balrog1ae26a12008-09-28 23:19:47 +0000750/***********************************************************/
751/* QEMU Block devices */
752
Markus Armbruster2292dda2011-01-28 11:21:41 +0100753#define HD_OPTS "media=disk"
754#define CDROM_OPTS "media=cdrom"
755#define FD_OPTS ""
756#define PFLASH_OPTS ""
757#define MTD_OPTS ""
758#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +0000759
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200760static int drive_init_func(QemuOpts *opts, void *opaque)
761{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200762 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200763
Markus Armbruster319ae522011-01-28 11:21:46 +0100764 return drive_init(opts, *use_scsi) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200765}
766
767static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
768{
769 if (NULL == qemu_opt_get(opts, "snapshot")) {
770 qemu_opt_set(opts, "snapshot", "on");
771 }
772 return 0;
773}
774
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100775static void default_drive(int enable, int snapshot, int use_scsi,
776 BlockInterfaceType type, int index,
777 const char *optstr)
778{
779 QemuOpts *opts;
780
781 if (type == IF_DEFAULT) {
782 type = use_scsi ? IF_SCSI : IF_IDE;
783 }
784
785 if (!enable || drive_get_by_index(type, index)) {
786 return;
787 }
788
789 opts = drive_add(type, index, NULL, optstr);
790 if (snapshot) {
791 drive_enable_snapshot(opts, NULL);
792 }
Markus Armbruster319ae522011-01-28 11:21:46 +0100793 if (!drive_init(opts, use_scsi)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100794 exit(1);
795 }
796}
797
Jan Kiszka76e30d02009-07-02 00:19:02 +0200798void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
799{
800 boot_set_handler = func;
801 boot_set_opaque = opaque;
802}
803
804int qemu_boot_set(const char *boot_devices)
805{
806 if (!boot_set_handler) {
807 return -EINVAL;
808 }
809 return boot_set_handler(boot_set_opaque, boot_devices);
810}
811
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300812static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200813{
814 /* We just do some generic consistency checks */
815 const char *p;
816 int bitmap = 0;
817
818 for (p = devices; *p != '\0'; p++) {
819 /* Allowed boot devices are:
820 * a-b: floppy disk drives
821 * c-f: IDE disk drives
822 * g-m: machine implementation dependant drives
823 * n-p: network devices
824 * It's up to each machine implementation to check if the given boot
825 * devices match the actual hardware implementation and firmware
826 * features.
827 */
828 if (*p < 'a' || *p > 'p') {
829 fprintf(stderr, "Invalid boot device '%c'\n", *p);
830 exit(1);
831 }
832 if (bitmap & (1 << (*p - 'a'))) {
833 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
834 exit(1);
835 }
836 bitmap |= 1 << (*p - 'a');
837 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200838}
839
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200840static void restore_boot_devices(void *opaque)
841{
842 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600843 static int first = 1;
844
845 /* Restore boot order and remove ourselves after the first boot */
846 if (first) {
847 first = 0;
848 return;
849 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200850
851 qemu_boot_set(standard_boot_devices);
852
853 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
Anthony Liguori7267c092011-08-20 22:09:37 -0500854 g_free(standard_boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200855}
856
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200857void add_boot_device_path(int32_t bootindex, DeviceState *dev,
858 const char *suffix)
859{
860 FWBootEntry *node, *i;
861
862 if (bootindex < 0) {
863 return;
864 }
865
866 assert(dev != NULL || suffix != NULL);
867
Anthony Liguori7267c092011-08-20 22:09:37 -0500868 node = g_malloc0(sizeof(FWBootEntry));
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200869 node->bootindex = bootindex;
Anthony Liguori7267c092011-08-20 22:09:37 -0500870 node->suffix = suffix ? g_strdup(suffix) : NULL;
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200871 node->dev = dev;
872
873 QTAILQ_FOREACH(i, &fw_boot_order, link) {
874 if (i->bootindex == bootindex) {
875 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
876 exit(1);
877 } else if (i->bootindex < bootindex) {
878 continue;
879 }
880 QTAILQ_INSERT_BEFORE(i, node, link);
881 return;
882 }
883 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
884}
885
Gleb Natapov962630f2010-12-08 13:35:09 +0200886/*
887 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +1000888 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +0200889 *
890 * memory pointed by "size" is assigned total length of the array in bytes
891 *
892 */
893char *get_boot_devices_list(uint32_t *size)
894{
895 FWBootEntry *i;
896 uint32_t total = 0;
897 char *list = NULL;
898
899 QTAILQ_FOREACH(i, &fw_boot_order, link) {
900 char *devpath = NULL, *bootpath;
901 int len;
902
903 if (i->dev) {
904 devpath = qdev_get_fw_dev_path(i->dev);
905 assert(devpath);
906 }
907
908 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +0000909 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
910
Anthony Liguori7267c092011-08-20 22:09:37 -0500911 bootpath = g_malloc(bootpathlen);
Blue Swirl4fd37a92010-12-19 14:05:43 +0000912 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -0500913 g_free(devpath);
Gleb Natapov962630f2010-12-08 13:35:09 +0200914 } else if (devpath) {
915 bootpath = devpath;
916 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -0500917 bootpath = g_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +0200918 assert(bootpath);
919 }
920
921 if (total) {
922 list[total-1] = '\n';
923 }
924 len = strlen(bootpath) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500925 list = g_realloc(list, total + len);
Gleb Natapov962630f2010-12-08 13:35:09 +0200926 memcpy(&list[total], bootpath, len);
927 total += len;
Anthony Liguori7267c092011-08-20 22:09:37 -0500928 g_free(bootpath);
Gleb Natapov962630f2010-12-08 13:35:09 +0200929 }
930
931 *size = total;
932
933 return list;
934}
935
aliguori268a3622009-04-21 22:30:27 +0000936static void numa_add(const char *optarg)
937{
938 char option[128];
939 char *endptr;
940 unsigned long long value, endvalue;
941 int nodenr;
942
943 optarg = get_opt_name(option, 128, optarg, ',') + 1;
944 if (!strcmp(option, "node")) {
945 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
946 nodenr = nb_numa_nodes;
947 } else {
948 nodenr = strtoull(option, NULL, 10);
949 }
950
951 if (get_param_value(option, 128, "mem", optarg) == 0) {
952 node_mem[nodenr] = 0;
953 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100954 int64_t sval;
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200955 sval = strtosz(option, NULL);
956 if (sval < 0) {
957 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
958 exit(1);
aliguori268a3622009-04-21 22:30:27 +0000959 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200960 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +0000961 }
962 if (get_param_value(option, 128, "cpus", optarg) == 0) {
963 node_cpumask[nodenr] = 0;
964 } else {
965 value = strtoull(option, &endptr, 10);
966 if (value >= 64) {
967 value = 63;
968 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
969 } else {
970 if (*endptr == '-') {
971 endvalue = strtoull(endptr+1, &endptr, 10);
972 if (endvalue >= 63) {
973 endvalue = 62;
974 fprintf(stderr,
975 "only 63 CPUs in NUMA mode supported.\n");
976 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100977 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000978 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100979 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000980 }
981 }
982 node_cpumask[nodenr] = value;
983 }
984 nb_numa_nodes++;
985 }
986 return;
987}
988
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200989static void smp_parse(const char *optarg)
990{
991 int smp, sockets = 0, threads = 0, cores = 0;
992 char *endptr;
993 char option[128];
994
995 smp = strtoul(optarg, &endptr, 10);
996 if (endptr != optarg) {
997 if (*endptr == ',') {
998 endptr++;
999 }
1000 }
1001 if (get_param_value(option, 128, "sockets", endptr) != 0)
1002 sockets = strtoull(option, NULL, 10);
1003 if (get_param_value(option, 128, "cores", endptr) != 0)
1004 cores = strtoull(option, NULL, 10);
1005 if (get_param_value(option, 128, "threads", endptr) != 0)
1006 threads = strtoull(option, NULL, 10);
1007 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1008 max_cpus = strtoull(option, NULL, 10);
1009
1010 /* compute missing values, prefer sockets over cores over threads */
1011 if (smp == 0 || sockets == 0) {
1012 sockets = sockets > 0 ? sockets : 1;
1013 cores = cores > 0 ? cores : 1;
1014 threads = threads > 0 ? threads : 1;
1015 if (smp == 0) {
1016 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001017 }
1018 } else {
1019 if (cores == 0) {
1020 threads = threads > 0 ? threads : 1;
1021 cores = smp / (sockets * threads);
1022 } else {
Joel Schoppdca98162010-07-21 15:05:17 -05001023 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001024 }
1025 }
1026 smp_cpus = smp;
1027 smp_cores = cores > 0 ? cores : 1;
1028 smp_threads = threads > 0 ? threads : 1;
1029 if (max_cpus == 0)
1030 max_cpus = smp_cpus;
1031}
1032
bellard330d0412003-07-26 18:11:40 +00001033/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001034/* USB devices */
1035
Markus Armbrusterfb080002010-05-28 15:38:44 +02001036static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00001037{
1038 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001039 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001040
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001041 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00001042 return -1;
1043
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001044 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1045 dev = usbdevice_create(devname);
1046 if (dev)
1047 goto done;
1048
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001049 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001050#ifndef CONFIG_LINUX
1051 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +00001052 if (strstart(devname, "host:", &p)) {
1053 dev = usb_host_device_open(p);
Gerd Hoffmanne447fc62011-06-01 14:41:59 +02001054 } else
1055#endif
1056 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
balrogdc72ac12008-11-09 00:04:26 +00001057 dev = usb_bt_init(devname[2] ? hci_init(p) :
1058 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001059 } else {
1060 return -1;
1061 }
pbrook0d92ed32006-05-21 16:30:15 +00001062 if (!dev)
1063 return -1;
1064
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001065done:
bellarda594cfb2005-11-06 16:13:29 +00001066 return 0;
1067}
1068
aliguori1f3870a2008-08-21 19:27:48 +00001069static int usb_device_del(const char *devname)
1070{
1071 int bus_num, addr;
1072 const char *p;
1073
aliguori5d0c5752008-09-14 01:07:41 +00001074 if (strstart(devname, "host:", &p))
1075 return usb_host_device_close(p);
1076
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001077 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00001078 return -1;
1079
1080 p = strchr(devname, '.');
1081 if (!p)
1082 return -1;
1083 bus_num = strtoul(devname, NULL, 0);
1084 addr = strtoul(p + 1, NULL, 0);
1085
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001086 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001087}
1088
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001089static int usb_parse(const char *cmdline)
1090{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001091 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +02001092 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001093 if (r < 0) {
1094 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1095 }
1096 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001097}
1098
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001099void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001100{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001101 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +02001102 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001103 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001104 }
bellarda594cfb2005-11-06 16:13:29 +00001105}
1106
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001107void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001108{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001109 const char *devname = qdict_get_str(qdict, "devname");
1110 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001111 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001112 }
bellarda594cfb2005-11-06 16:13:29 +00001113}
1114
bellardf7cce892004-12-08 22:21:25 +00001115/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001116/* PCMCIA/Cardbus */
1117
1118static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001119 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001120 struct pcmcia_socket_entry_s *next;
1121} *pcmcia_sockets = 0;
1122
Paul Brookbc24a222009-05-10 01:44:56 +01001123void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001124{
1125 struct pcmcia_socket_entry_s *entry;
1126
Anthony Liguori7267c092011-08-20 22:09:37 -05001127 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
balrog201a51f2007-04-30 00:51:09 +00001128 entry->socket = socket;
1129 entry->next = pcmcia_sockets;
1130 pcmcia_sockets = entry;
1131}
1132
Paul Brookbc24a222009-05-10 01:44:56 +01001133void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001134{
1135 struct pcmcia_socket_entry_s *entry, **ptr;
1136
1137 ptr = &pcmcia_sockets;
1138 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1139 if (entry->socket == socket) {
1140 *ptr = entry->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001141 g_free(entry);
balrog201a51f2007-04-30 00:51:09 +00001142 }
1143}
1144
aliguori376253e2009-03-05 23:01:23 +00001145void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001146{
1147 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001148
balrog201a51f2007-04-30 00:51:09 +00001149 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001150 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001151
1152 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001153 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1154 iter->socket->attached ? iter->socket->card_string :
1155 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001156}
1157
1158/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001159/* machine registration */
1160
blueswir1bdaf78e2008-10-04 07:24:27 +00001161static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001162QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001163
1164int qemu_register_machine(QEMUMachine *m)
1165{
1166 QEMUMachine **pm;
1167 pm = &first_machine;
1168 while (*pm != NULL)
1169 pm = &(*pm)->next;
1170 m->next = NULL;
1171 *pm = m;
1172 return 0;
1173}
1174
pbrook9596ebb2007-11-18 01:44:38 +00001175static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001176{
1177 QEMUMachine *m;
1178
1179 for(m = first_machine; m != NULL; m = m->next) {
1180 if (!strcmp(m->name, name))
1181 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001182 if (m->alias && !strcmp(m->alias, name))
1183 return m;
bellardcc1daa42005-06-05 14:49:17 +00001184 }
1185 return NULL;
1186}
1187
Anthony Liguori0c257432009-05-21 20:41:01 -05001188static QEMUMachine *find_default_machine(void)
1189{
1190 QEMUMachine *m;
1191
1192 for(m = first_machine; m != NULL; m = m->next) {
1193 if (m->is_default) {
1194 return m;
1195 }
1196 }
1197 return NULL;
1198}
1199
bellardcc1daa42005-06-05 14:49:17 +00001200/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001201/* main execution loop */
1202
pbrook9596ebb2007-11-18 01:44:38 +00001203static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001204{
aliguori7d957bd2009-01-15 22:14:11 +00001205 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001206 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001207 DisplayChangeListener *dcl = ds->listeners;
1208
Sheng Yang62a27442010-01-26 19:21:16 +08001209 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001210 dpy_refresh(ds);
1211
1212 while (dcl != NULL) {
1213 if (dcl->gui_timer_interval &&
1214 dcl->gui_timer_interval < interval)
1215 interval = dcl->gui_timer_interval;
1216 dcl = dcl->next;
1217 }
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001218 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001219}
1220
blueswir19043b622009-01-21 19:28:13 +00001221static void nographic_update(void *opaque)
1222{
1223 uint64_t interval = GUI_REFRESH_INTERVAL;
1224
Sheng Yang62a27442010-01-26 19:21:16 +08001225 qemu_flush_coalesced_mmio_buffer();
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001226 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00001227}
1228
bellard0bd48852005-11-11 00:00:47 +00001229struct vm_change_state_entry {
1230 VMChangeStateHandler *cb;
1231 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001232 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001233};
1234
Blue Swirl72cf2d42009-09-12 07:36:22 +00001235static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001236
1237VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1238 void *opaque)
1239{
1240 VMChangeStateEntry *e;
1241
Anthony Liguori7267c092011-08-20 22:09:37 -05001242 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001243
1244 e->cb = cb;
1245 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001246 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001247 return e;
1248}
1249
1250void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1251{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001252 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001253 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001254}
1255
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001256void vm_state_notify(int running, RunState state)
bellard0bd48852005-11-11 00:00:47 +00001257{
1258 VMChangeStateEntry *e;
1259
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001260 trace_vm_state_notify(running, state);
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001261
bellard0bd48852005-11-11 00:00:47 +00001262 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001263 e->cb(e->opaque, running, state);
bellard0bd48852005-11-11 00:00:47 +00001264 }
1265}
1266
bellard8a7ddc32004-03-31 19:00:16 +00001267void vm_start(void)
1268{
Luiz Capitulino13548692011-07-29 15:36:43 -03001269 if (!runstate_is_running()) {
bellard8a7ddc32004-03-31 19:00:16 +00001270 cpu_enable_ticks();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001271 runstate_set(RUN_STATE_RUNNING);
1272 vm_state_notify(1, RUN_STATE_RUNNING);
aliguorid6dc3d42009-04-24 18:04:07 +00001273 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001274 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001275 }
1276}
1277
bellardbb0c6722004-06-20 12:37:32 +00001278/* reset/shutdown handler */
1279
1280typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001281 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001282 QEMUResetHandler *func;
1283 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001284} QEMUResetEntry;
1285
Blue Swirl72cf2d42009-09-12 07:36:22 +00001286static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1287 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001288static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001289static int shutdown_requested, shutdown_signal = -1;
1290static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001291static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001292static int debug_requested;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001293static RunState vmstop_requested = RUN_STATE_MAX;
bellardbb0c6722004-06-20 12:37:32 +00001294
Anthony PERARD1291eb32010-07-22 15:52:48 +01001295int qemu_shutdown_requested_get(void)
1296{
1297 return shutdown_requested;
1298}
1299
1300int qemu_reset_requested_get(void)
1301{
1302 return reset_requested;
1303}
1304
aurel32cf7a2fe2008-03-18 06:53:05 +00001305int qemu_shutdown_requested(void)
1306{
1307 int r = shutdown_requested;
1308 shutdown_requested = 0;
1309 return r;
1310}
1311
Gleb Natapovf64622c2011-03-15 13:56:04 +02001312void qemu_kill_report(void)
1313{
1314 if (shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001315 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1316 if (shutdown_pid == 0) {
1317 /* This happens for eg ^C at the terminal, so it's worth
1318 * avoiding printing an odd message in that case.
1319 */
1320 fputc('\n', stderr);
1321 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001322 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001323 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001324 shutdown_signal = -1;
1325 }
1326}
1327
aurel32cf7a2fe2008-03-18 06:53:05 +00001328int qemu_reset_requested(void)
1329{
1330 int r = reset_requested;
1331 reset_requested = 0;
1332 return r;
1333}
1334
1335int qemu_powerdown_requested(void)
1336{
1337 int r = powerdown_requested;
1338 powerdown_requested = 0;
1339 return r;
1340}
1341
aliguorie5689022009-04-24 18:03:54 +00001342static int qemu_debug_requested(void)
1343{
1344 int r = debug_requested;
1345 debug_requested = 0;
1346 return r;
1347}
1348
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001349/* We use RUN_STATE_MAX but any invalid value will do */
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001350static bool qemu_vmstop_requested(RunState *r)
aliguori6e29f5d2009-04-24 18:04:02 +00001351{
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001352 if (vmstop_requested < RUN_STATE_MAX) {
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001353 *r = vmstop_requested;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001354 vmstop_requested = RUN_STATE_MAX;
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001355 return true;
1356 }
1357
1358 return false;
aliguori6e29f5d2009-04-24 18:04:02 +00001359}
1360
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001361void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001362{
Anthony Liguori7267c092011-08-20 22:09:37 -05001363 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001364
bellardbb0c6722004-06-20 12:37:32 +00001365 re->func = func;
1366 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001367 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001368}
1369
Jan Kiszkadda9b292009-07-02 00:19:02 +02001370void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001371{
1372 QEMUResetEntry *re;
1373
Blue Swirl72cf2d42009-09-12 07:36:22 +00001374 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001375 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001376 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001377 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001378 return;
1379 }
1380 }
1381}
1382
Jan Kiszkae063eb12011-06-14 18:29:43 +02001383void qemu_system_reset(bool report)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001384{
1385 QEMUResetEntry *re, *nre;
1386
1387 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001388 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001389 re->func(re->opaque);
1390 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001391 if (report) {
1392 monitor_protocol_event(QEVENT_RESET, NULL);
1393 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001394 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001395}
1396
1397void qemu_system_reset_request(void)
1398{
bellardd1beab82006-10-02 19:44:22 +00001399 if (no_reboot) {
1400 shutdown_requested = 1;
1401 } else {
1402 reset_requested = 1;
1403 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001404 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001405 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001406}
1407
Gleb Natapovf64622c2011-03-15 13:56:04 +02001408void qemu_system_killed(int signal, pid_t pid)
1409{
1410 shutdown_signal = signal;
1411 shutdown_pid = pid;
Kevin Wolfd9389b92011-09-14 15:38:40 +02001412 no_shutdown = 0;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001413 qemu_system_shutdown_request();
1414}
1415
bellardbb0c6722004-06-20 12:37:32 +00001416void qemu_system_shutdown_request(void)
1417{
1418 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001419 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001420}
1421
bellard34751872005-07-02 14:31:34 +00001422void qemu_system_powerdown_request(void)
1423{
1424 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001425 qemu_notify_event();
1426}
1427
Jan Kiszka8cf71712011-02-07 12:19:16 +01001428void qemu_system_debug_request(void)
1429{
1430 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001431 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001432}
1433
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001434void qemu_system_vmstop_request(RunState state)
Jan Kiszka8cf71712011-02-07 12:19:16 +01001435{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001436 vmstop_requested = state;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001437 qemu_notify_event();
1438}
1439
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001440static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1441static int n_poll_fds;
1442static int max_priority;
1443
1444static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1445 fd_set *xfds, struct timeval *tv)
1446{
1447 GMainContext *context = g_main_context_default();
1448 int i;
1449 int timeout = 0, cur_timeout;
1450
1451 g_main_context_prepare(context, &max_priority);
1452
1453 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1454 poll_fds, ARRAY_SIZE(poll_fds));
1455 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1456
1457 for (i = 0; i < n_poll_fds; i++) {
1458 GPollFD *p = &poll_fds[i];
1459
1460 if ((p->events & G_IO_IN)) {
1461 FD_SET(p->fd, rfds);
1462 *max_fd = MAX(*max_fd, p->fd);
1463 }
1464 if ((p->events & G_IO_OUT)) {
1465 FD_SET(p->fd, wfds);
1466 *max_fd = MAX(*max_fd, p->fd);
1467 }
1468 if ((p->events & G_IO_ERR)) {
1469 FD_SET(p->fd, xfds);
1470 *max_fd = MAX(*max_fd, p->fd);
1471 }
1472 }
1473
1474 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1475 if (timeout >= 0 && timeout < cur_timeout) {
1476 tv->tv_sec = timeout / 1000;
1477 tv->tv_usec = (timeout % 1000) * 1000;
1478 }
1479}
1480
1481static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1482 bool err)
1483{
1484 GMainContext *context = g_main_context_default();
1485
1486 if (!err) {
1487 int i;
1488
1489 for (i = 0; i < n_poll_fds; i++) {
1490 GPollFD *p = &poll_fds[i];
1491
1492 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1493 p->revents |= G_IO_IN;
1494 }
1495 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1496 p->revents |= G_IO_OUT;
1497 }
1498 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1499 p->revents |= G_IO_ERR;
1500 }
1501 }
1502 }
1503
1504 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1505 g_main_context_dispatch(context);
1506 }
1507}
1508
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001509int main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001510{
aliguori56f3a5d2008-10-31 18:07:17 +00001511 fd_set rfds, wfds, xfds;
1512 int ret, nfds;
1513 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001514 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001515
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001516 if (nonblocking)
1517 timeout = 0;
1518 else {
1519 timeout = qemu_calculate_timeout();
1520 qemu_bh_update_timeout(&timeout);
1521 }
aliguori56f3a5d2008-10-31 18:07:17 +00001522
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001523 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001524
Paolo Bonzini02981412011-03-09 18:21:09 +01001525 tv.tv_sec = timeout / 1000;
1526 tv.tv_usec = (timeout % 1000) * 1000;
1527
bellardfd1dff42006-02-01 21:29:26 +00001528 /* poll any events */
1529 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001530 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001531 FD_ZERO(&rfds);
1532 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001533 FD_ZERO(&xfds);
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001534
Paolo Bonzini02981412011-03-09 18:21:09 +01001535 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
Jan Kiszkad918f232009-06-24 14:42:30 +02001536 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001537 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
Jan Kiszkad918f232009-06-24 14:42:30 +02001538
Jan Kiszka200668b2011-08-22 17:46:01 +02001539 if (timeout > 0) {
1540 qemu_mutex_unlock_iothread();
1541 }
1542
bellarde0356492006-05-01 13:33:02 +00001543 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
Jan Kiszka200668b2011-08-22 17:46:01 +02001544
1545 if (timeout > 0) {
1546 qemu_mutex_lock_iothread();
1547 }
thscafffd42007-02-28 21:59:44 +00001548
Paolo Bonzini02981412011-03-09 18:21:09 +01001549 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
Jan Kiszkad918f232009-06-24 14:42:30 +02001550 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001551 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001552
Paolo Bonzinib6964822010-03-10 11:38:45 +01001553 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001554
pbrook423f0742007-05-23 00:06:54 +00001555 /* Check bottom-halves last in case any of the earlier events triggered
1556 them. */
1557 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001558
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001559 return ret;
bellard5905b2e2004-08-01 21:53:26 +00001560}
1561
Blue Swirld9c32312009-08-09 08:42:19 +00001562qemu_irq qemu_system_powerdown;
1563
aliguori43b96852009-04-24 18:03:33 +00001564static void main_loop(void)
1565{
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001566 bool nonblocking;
1567 int last_io __attribute__ ((unused)) = 0;
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01001568#ifdef CONFIG_PROFILER
1569 int64_t ti;
1570#endif
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001571 RunState r;
aliguori43b96852009-04-24 18:03:33 +00001572
Blue Swirl7277e022010-04-12 17:19:06 +00001573 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001574
aliguori6e29f5d2009-04-24 18:04:02 +00001575 for (;;) {
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001576 nonblocking = !kvm_enabled() && last_io > 0;
aliguori43b96852009-04-24 18:03:33 +00001577#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001578 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00001579#endif
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001580 last_io = main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001581#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001582 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00001583#endif
aliguori43b96852009-04-24 18:03:33 +00001584
Jan Kiszka8cf71712011-02-07 12:19:16 +01001585 if (qemu_debug_requested()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001586 vm_stop(RUN_STATE_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001587 }
aliguori43b96852009-04-24 18:03:33 +00001588 if (qemu_shutdown_requested()) {
Gleb Natapovf64622c2011-03-15 13:56:04 +02001589 qemu_kill_report();
Blue Swirl242cd002009-12-04 18:05:45 +00001590 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001591 if (no_shutdown) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001592 vm_stop(RUN_STATE_SHUTDOWN);
aliguori43b96852009-04-24 18:03:33 +00001593 } else
1594 break;
1595 }
aliguorid6dc3d42009-04-24 18:04:07 +00001596 if (qemu_reset_requested()) {
1597 pause_all_vcpus();
Jan Kiszkaa7ada152011-03-02 08:56:11 +01001598 cpu_synchronize_all_states();
Jan Kiszkae063eb12011-06-14 18:29:43 +02001599 qemu_system_reset(VMRESET_REPORT);
aliguorid6dc3d42009-04-24 18:04:07 +00001600 resume_all_vcpus();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001601 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1602 runstate_check(RUN_STATE_SHUTDOWN)) {
1603 runstate_set(RUN_STATE_PAUSED);
Luiz Capitulino6667b232011-07-29 15:57:54 -03001604 }
aliguorid6dc3d42009-04-24 18:04:07 +00001605 }
Blue Swirld9c32312009-08-09 08:42:19 +00001606 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001607 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001608 qemu_irq_raise(qemu_system_powerdown);
1609 }
Luiz Capitulino0d45b702011-09-30 14:27:11 -03001610 if (qemu_vmstop_requested(&r)) {
aliguori6e29f5d2009-04-24 18:04:02 +00001611 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001612 }
aliguori43b96852009-04-24 18:03:33 +00001613 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001614 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001615 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001616}
1617
pbrook9bd7e6d2009-04-07 22:58:45 +00001618static void version(void)
1619{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001620 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001621}
1622
ths15f82202007-06-29 23:26:08 +00001623static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001624{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001625 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001626#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1627 opt_help
blueswir15824d652009-03-28 06:44:27 +00001628#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001629#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001630#undef DEF
1631#undef DEFHEADING
1632#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001633 ;
1634 version();
1635 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001636 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001637 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001638 "\n"
1639 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001640 "During emulation, the following keys are useful:\n"
1641 "ctrl-alt-f toggle full screen\n"
1642 "ctrl-alt-n switch to virtual console 'n'\n"
1643 "ctrl-alt toggle mouse and keyboard grab\n"
1644 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001645 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1646 "qemu",
1647 options_help);
ths15f82202007-06-29 23:26:08 +00001648 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001649}
1650
bellardcd6f1162004-05-13 22:02:20 +00001651#define HAS_ARG 0x0001
1652
bellardcd6f1162004-05-13 22:02:20 +00001653typedef struct QEMUOption {
1654 const char *name;
1655 int flags;
1656 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001657 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001658} QEMUOption;
1659
blueswir1dbed7e42008-10-01 19:38:09 +00001660static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001661 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1662#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1663 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001664#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001665#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001666#undef DEF
1667#undef DEFHEADING
1668#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001669 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001670};
malc3893c122008-09-28 00:42:05 +00001671static void select_vgahw (const char *p)
1672{
1673 const char *opts;
1674
Gerd Hoffmann64465292009-12-08 13:11:45 +01001675 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001676 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001677 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001678 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001679 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001680 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001681 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001682 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001683 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001684 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001685 } else if (strstart(p, "qxl", &opts)) {
1686 vga_interface_type = VGA_QXL;
aliguori28b85ed2009-04-22 15:19:48 +00001687 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001688 invalid_vga:
1689 fprintf(stderr, "Unknown vga type: %s\n", p);
1690 exit(1);
1691 }
malccb5a7aa2008-09-28 00:42:12 +00001692 while (*opts) {
1693 const char *nextopt;
1694
1695 if (strstart(opts, ",retrace=", &nextopt)) {
1696 opts = nextopt;
1697 if (strstart(opts, "dumb", &nextopt))
1698 vga_retrace_method = VGA_RETRACE_DUMB;
1699 else if (strstart(opts, "precise", &nextopt))
1700 vga_retrace_method = VGA_RETRACE_PRECISE;
1701 else goto invalid_vga;
1702 } else goto invalid_vga;
1703 opts = nextopt;
1704 }
malc3893c122008-09-28 00:42:05 +00001705}
1706
Jes Sorensen1472a952011-03-16 13:33:31 +01001707static DisplayType select_display(const char *p)
1708{
1709 const char *opts;
1710 DisplayType display = DT_DEFAULT;
1711
1712 if (strstart(p, "sdl", &opts)) {
1713#ifdef CONFIG_SDL
1714 display = DT_SDL;
1715 while (*opts) {
1716 const char *nextopt;
1717
1718 if (strstart(opts, ",frame=", &nextopt)) {
1719 opts = nextopt;
1720 if (strstart(opts, "on", &nextopt)) {
1721 no_frame = 0;
1722 } else if (strstart(opts, "off", &nextopt)) {
1723 no_frame = 1;
1724 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001725 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001726 }
1727 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1728 opts = nextopt;
1729 if (strstart(opts, "on", &nextopt)) {
1730 alt_grab = 1;
1731 } else if (strstart(opts, "off", &nextopt)) {
1732 alt_grab = 0;
1733 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001734 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001735 }
1736 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1737 opts = nextopt;
1738 if (strstart(opts, "on", &nextopt)) {
1739 ctrl_grab = 1;
1740 } else if (strstart(opts, "off", &nextopt)) {
1741 ctrl_grab = 0;
1742 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001743 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001744 }
1745 } else if (strstart(opts, ",window_close=", &nextopt)) {
1746 opts = nextopt;
1747 if (strstart(opts, "on", &nextopt)) {
1748 no_quit = 0;
1749 } else if (strstart(opts, "off", &nextopt)) {
1750 no_quit = 1;
1751 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001752 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001753 }
1754 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001755 invalid_sdl_args:
1756 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1757 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01001758 }
1759 opts = nextopt;
1760 }
1761#else
1762 fprintf(stderr, "SDL support is disabled\n");
1763 exit(1);
1764#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01001765 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001766#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01001767 display_remote++;
1768
1769 if (*opts) {
1770 const char *nextopt;
1771
1772 if (strstart(opts, "=", &nextopt)) {
1773 vnc_display = nextopt;
1774 }
1775 }
1776 if (!vnc_display) {
1777 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1778 exit(1);
1779 }
Jes Sorensen821601e2011-03-16 13:33:36 +01001780#else
1781 fprintf(stderr, "VNC support is disabled\n");
1782 exit(1);
1783#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01001784 } else if (strstart(p, "curses", &opts)) {
1785#ifdef CONFIG_CURSES
1786 display = DT_CURSES;
1787#else
1788 fprintf(stderr, "Curses support is disabled\n");
1789 exit(1);
1790#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01001791 } else if (strstart(p, "none", &opts)) {
1792 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01001793 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01001794 fprintf(stderr, "Unknown display type: %s\n", p);
1795 exit(1);
1796 }
1797
1798 return display;
1799}
1800
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001801static int balloon_parse(const char *arg)
1802{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001803 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001804
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001805 if (strcmp(arg, "none") == 0) {
1806 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001807 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001808
1809 if (!strncmp(arg, "virtio", 6)) {
1810 if (arg[6] == ',') {
1811 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001812 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001813 if (!opts)
1814 return -1;
1815 } else {
1816 /* create empty opts */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001817 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001818 }
Alexander Graf29f82b32011-03-29 15:29:29 +02001819 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001820 return 0;
1821 }
1822
1823 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001824}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001825
Paul Brook5cea8592009-05-30 00:52:44 +01001826char *qemu_find_file(int type, const char *name)
1827{
1828 int len;
1829 const char *subdir;
1830 char *buf;
1831
1832 /* If name contains path separators then try it as a straight path. */
1833 if ((strchr(name, '/') || strchr(name, '\\'))
1834 && access(name, R_OK) == 0) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001835 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001836 }
1837 switch (type) {
1838 case QEMU_FILE_TYPE_BIOS:
1839 subdir = "";
1840 break;
1841 case QEMU_FILE_TYPE_KEYMAP:
1842 subdir = "keymaps/";
1843 break;
1844 default:
1845 abort();
1846 }
1847 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05001848 buf = g_malloc0(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001849 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001850 if (access(buf, R_OK)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001851 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01001852 return NULL;
1853 }
1854 return buf;
1855}
1856
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001857static int device_help_func(QemuOpts *opts, void *opaque)
1858{
1859 return qdev_device_help(opts);
1860}
1861
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001862static int device_init_func(QemuOpts *opts, void *opaque)
1863{
1864 DeviceState *dev;
1865
1866 dev = qdev_device_add(opts);
1867 if (!dev)
1868 return -1;
1869 return 0;
1870}
1871
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001872static int chardev_init_func(QemuOpts *opts, void *opaque)
1873{
1874 CharDriverState *chr;
1875
Anthony Liguorif69554b2011-08-15 11:17:37 -05001876 chr = qemu_chr_new_from_opts(opts, NULL);
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001877 if (!chr)
1878 return -1;
1879 return 0;
1880}
1881
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07001882#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301883static int fsdev_init_func(QemuOpts *opts, void *opaque)
1884{
1885 int ret;
1886 ret = qemu_fsdev_add(opts);
1887
1888 return ret;
1889}
1890#endif
1891
Gerd Hoffmann88589342009-12-08 13:11:50 +01001892static int mon_init_func(QemuOpts *opts, void *opaque)
1893{
1894 CharDriverState *chr;
1895 const char *chardev;
1896 const char *mode;
1897 int flags;
1898
1899 mode = qemu_opt_get(opts, "mode");
1900 if (mode == NULL) {
1901 mode = "readline";
1902 }
1903 if (strcmp(mode, "readline") == 0) {
1904 flags = MONITOR_USE_READLINE;
1905 } else if (strcmp(mode, "control") == 0) {
1906 flags = MONITOR_USE_CONTROL;
1907 } else {
1908 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1909 exit(1);
1910 }
1911
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01001912 if (qemu_opt_get_bool(opts, "pretty", 0))
1913 flags |= MONITOR_USE_PRETTY;
1914
Gerd Hoffmann88589342009-12-08 13:11:50 +01001915 if (qemu_opt_get_bool(opts, "default", 0))
1916 flags |= MONITOR_IS_DEFAULT;
1917
1918 chardev = qemu_opt_get(opts, "chardev");
1919 chr = qemu_chr_find(chardev);
1920 if (chr == NULL) {
1921 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1922 exit(1);
1923 }
1924
1925 monitor_init(chr, flags);
1926 return 0;
1927}
1928
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001929static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001930{
1931 static int monitor_device_index = 0;
1932 QemuOpts *opts;
1933 const char *p;
1934 char label[32];
1935 int def = 0;
1936
1937 if (strstart(optarg, "chardev:", &p)) {
1938 snprintf(label, sizeof(label), "%s", p);
1939 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001940 snprintf(label, sizeof(label), "compat_monitor%d",
1941 monitor_device_index);
1942 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001943 def = 1;
1944 }
1945 opts = qemu_chr_parse_compat(label, optarg);
1946 if (!opts) {
1947 fprintf(stderr, "parse error: %s\n", optarg);
1948 exit(1);
1949 }
1950 }
1951
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001952 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001953 if (!opts) {
1954 fprintf(stderr, "duplicate chardev: %s\n", label);
1955 exit(1);
1956 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001957 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001958 qemu_opt_set(opts, "chardev", label);
1959 if (def)
1960 qemu_opt_set(opts, "default", "on");
1961 monitor_device_index++;
1962}
1963
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001964struct device_config {
1965 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001966 DEV_USB, /* -usbdevice */
1967 DEV_BT, /* -bt */
1968 DEV_SERIAL, /* -serial */
1969 DEV_PARALLEL, /* -parallel */
1970 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001971 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001972 } type;
1973 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001974 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001975};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001976QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001977
1978static void add_device_config(int type, const char *cmdline)
1979{
1980 struct device_config *conf;
1981
Anthony Liguori7267c092011-08-20 22:09:37 -05001982 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001983 conf->type = type;
1984 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001985 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001986}
1987
1988static int foreach_device_config(int type, int (*func)(const char *cmdline))
1989{
1990 struct device_config *conf;
1991 int rc;
1992
Blue Swirl72cf2d42009-09-12 07:36:22 +00001993 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001994 if (conf->type != type)
1995 continue;
1996 rc = func(conf->cmdline);
1997 if (0 != rc)
1998 return rc;
1999 }
2000 return 0;
2001}
2002
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002003static int serial_parse(const char *devname)
2004{
2005 static int index = 0;
2006 char label[32];
2007
2008 if (strcmp(devname, "none") == 0)
2009 return 0;
2010 if (index == MAX_SERIAL_PORTS) {
2011 fprintf(stderr, "qemu: too many serial ports\n");
2012 exit(1);
2013 }
2014 snprintf(label, sizeof(label), "serial%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002015 serial_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002016 if (!serial_hds[index]) {
2017 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2018 devname, strerror(errno));
2019 return -1;
2020 }
2021 index++;
2022 return 0;
2023}
2024
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002025static int parallel_parse(const char *devname)
2026{
2027 static int index = 0;
2028 char label[32];
2029
2030 if (strcmp(devname, "none") == 0)
2031 return 0;
2032 if (index == MAX_PARALLEL_PORTS) {
2033 fprintf(stderr, "qemu: too many parallel ports\n");
2034 exit(1);
2035 }
2036 snprintf(label, sizeof(label), "parallel%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002037 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002038 if (!parallel_hds[index]) {
2039 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2040 devname, strerror(errno));
2041 return -1;
2042 }
2043 index++;
2044 return 0;
2045}
2046
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002047static int virtcon_parse(const char *devname)
2048{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002049 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002050 static int index = 0;
2051 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302052 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002053
2054 if (strcmp(devname, "none") == 0)
2055 return 0;
2056 if (index == MAX_VIRTIO_CONSOLES) {
2057 fprintf(stderr, "qemu: too many virtio consoles\n");
2058 exit(1);
2059 }
Amit Shah392ecf52010-01-21 16:19:23 +05302060
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002061 bus_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05302062 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2063
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002064 dev_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05302065 qemu_opt_set(dev_opts, "driver", "virtconsole");
2066
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002067 snprintf(label, sizeof(label), "virtcon%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05002068 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002069 if (!virtcon_hds[index]) {
2070 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2071 devname, strerror(errno));
2072 return -1;
2073 }
Amit Shah392ecf52010-01-21 16:19:23 +05302074 qemu_opt_set(dev_opts, "chardev", label);
2075
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002076 index++;
2077 return 0;
2078}
2079
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002080static int debugcon_parse(const char *devname)
2081{
2082 QemuOpts *opts;
2083
Anthony Liguori27143a42011-08-15 11:17:36 -05002084 if (!qemu_chr_new("debugcon", devname, NULL)) {
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002085 exit(1);
2086 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002087 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002088 if (!opts) {
2089 fprintf(stderr, "qemu: already have a debugcon device\n");
2090 exit(1);
2091 }
2092 qemu_opt_set(opts, "driver", "isa-debugcon");
2093 qemu_opt_set(opts, "chardev", "debugcon");
2094 return 0;
2095}
2096
Jan Kiszka9052ea62011-07-23 12:38:37 +02002097static QEMUMachine *machine_parse(const char *name)
2098{
2099 QEMUMachine *m, *machine = NULL;
2100
2101 if (name) {
2102 machine = find_machine(name);
2103 }
2104 if (machine) {
2105 return machine;
2106 }
2107 printf("Supported machines are:\n");
2108 for (m = first_machine; m != NULL; m = m->next) {
2109 if (m->alias) {
2110 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2111 }
2112 printf("%-10s %s%s\n", m->name, m->desc,
2113 m->is_default ? " (default)" : "");
2114 }
2115 exit(!name || *name != '?');
2116}
2117
Anthony PERARD303d4e82010-09-21 20:05:31 +01002118static int tcg_init(void)
2119{
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002120 tcg_exec_init(tcg_tb_size * 1024 * 1024);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002121 return 0;
2122}
2123
2124static struct {
2125 const char *opt_name;
2126 const char *name;
2127 int (*available)(void);
2128 int (*init)(void);
2129 int *allowed;
2130} accel_list[] = {
2131 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01002132 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002133 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2134};
2135
2136static int configure_accelerator(void)
2137{
2138 const char *p = NULL;
2139 char buf[10];
2140 int i, ret;
2141 bool accel_initalised = 0;
2142 bool init_failed = 0;
2143
2144 QemuOptsList *list = qemu_find_opts("machine");
2145 if (!QTAILQ_EMPTY(&list->head)) {
2146 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2147 }
2148
2149 if (p == NULL) {
2150 /* Use the default "accelerator", tcg */
2151 p = "tcg";
2152 }
2153
2154 while (!accel_initalised && *p != '\0') {
2155 if (*p == ':') {
2156 p++;
2157 }
2158 p = get_opt_name(buf, sizeof (buf), p, ':');
2159 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2160 if (strcmp(accel_list[i].opt_name, buf) == 0) {
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002161 *(accel_list[i].allowed) = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002162 ret = accel_list[i].init();
2163 if (ret < 0) {
2164 init_failed = 1;
2165 if (!accel_list[i].available()) {
2166 printf("%s not supported for this target\n",
2167 accel_list[i].name);
2168 } else {
2169 fprintf(stderr, "failed to initialize %s: %s\n",
2170 accel_list[i].name,
2171 strerror(-ret));
2172 }
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002173 *(accel_list[i].allowed) = 0;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002174 } else {
2175 accel_initalised = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002176 }
2177 break;
2178 }
2179 }
2180 if (i == ARRAY_SIZE(accel_list)) {
2181 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2182 }
2183 }
2184
2185 if (!accel_initalised) {
2186 fprintf(stderr, "No accelerator found!\n");
2187 exit(1);
2188 }
2189
2190 if (init_failed) {
2191 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2192 }
2193
2194 return !accel_initalised;
2195}
2196
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002197void qemu_add_exit_notifier(Notifier *notify)
2198{
2199 notifier_list_add(&exit_notifiers, notify);
2200}
2201
2202void qemu_remove_exit_notifier(Notifier *notify)
2203{
2204 notifier_list_remove(&exit_notifiers, notify);
2205}
2206
2207static void qemu_run_exit_notifiers(void)
2208{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002209 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002210}
2211
Gleb Natapov4cab9462010-12-08 13:35:08 +02002212void qemu_add_machine_init_done_notifier(Notifier *notify)
2213{
2214 notifier_list_add(&machine_init_done_notifiers, notify);
2215}
2216
2217static void qemu_run_machine_init_done_notifiers(void)
2218{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002219 notifier_list_notify(&machine_init_done_notifiers, NULL);
Gleb Natapov4cab9462010-12-08 13:35:08 +02002220}
2221
Anthony Liguori6530a972010-01-22 09:18:06 -06002222static const QEMUOption *lookup_opt(int argc, char **argv,
2223 const char **poptarg, int *poptind)
2224{
2225 const QEMUOption *popt;
2226 int optind = *poptind;
2227 char *r = argv[optind];
2228 const char *optarg;
2229
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002230 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002231 optind++;
2232 /* Treat --foo the same as -foo. */
2233 if (r[1] == '-')
2234 r++;
2235 popt = qemu_options;
2236 for(;;) {
2237 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002238 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002239 exit(1);
2240 }
2241 if (!strcmp(popt->name, r + 1))
2242 break;
2243 popt++;
2244 }
2245 if (popt->flags & HAS_ARG) {
2246 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002247 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002248 exit(1);
2249 }
2250 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002251 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002252 } else {
2253 optarg = NULL;
2254 }
2255
2256 *poptarg = optarg;
2257 *poptind = optind;
2258
2259 return popt;
2260}
2261
Anthony Liguori07501122011-08-20 22:38:31 -05002262static gpointer malloc_and_trace(gsize n_bytes)
2263{
2264 void *ptr = malloc(n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002265 trace_g_malloc(n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002266 return ptr;
2267}
2268
2269static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2270{
2271 void *ptr = realloc(mem, n_bytes);
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002272 trace_g_realloc(mem, n_bytes, ptr);
Anthony Liguori07501122011-08-20 22:38:31 -05002273 return ptr;
2274}
2275
2276static void free_and_trace(gpointer mem)
2277{
Frediano Ziglioa74cd8c2011-08-31 09:25:35 +02002278 trace_g_free(mem);
Anthony Liguori07501122011-08-20 22:38:31 -05002279 free(mem);
2280}
2281
malc902b3d52008-12-10 19:18:40 +00002282int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002283{
aliguori59030a82009-04-05 18:43:41 +00002284 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00002285 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002286 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002287 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002288 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002289 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002290 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00002291 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00002292 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00002293 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002294 QemuOpts *hda_opts = NULL, *opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002295 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002296 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002297 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002298 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002299 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002300 const char *cpu_model;
ths93815bc2007-03-19 15:58:31 +00002301 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002302 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002303#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002304 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002305#endif
Anthony Liguori292444c2010-01-21 10:57:58 -06002306 int defconfig = 1;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002307 const char *log_mask = NULL;
2308 const char *log_file = NULL;
Anthony Liguori07501122011-08-20 22:38:31 -05002309 GMemVTable mem_trace = {
2310 .malloc = malloc_and_trace,
2311 .realloc = realloc_and_trace,
2312 .free = free_and_trace,
2313 };
Lluís23d15e82011-08-31 20:31:31 +02002314 const char *trace_events = NULL;
Lluíse4858972011-08-31 20:31:03 +02002315 const char *trace_file = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002316
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002317 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002318 error_set_progname(argv[0]);
2319
Anthony Liguori07501122011-08-20 22:38:31 -05002320 g_mem_set_vtable(&mem_trace);
Gerd Hoffmannad8b8182011-09-07 15:02:15 +02002321 g_thread_init(NULL);
Anthony Liguori07501122011-08-20 22:38:31 -05002322
Luiz Capitulino5db9d4d2011-09-05 16:36:31 -03002323 runstate_init();
2324
Jan Kiszka68752042009-09-15 13:36:04 +02002325 init_clocks();
2326
malc902b3d52008-12-10 19:18:40 +00002327 qemu_cache_utils_init(envp);
2328
Blue Swirl72cf2d42009-09-12 07:36:22 +00002329 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002330 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002331
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002332 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002333 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002334 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00002335 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002336 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002337 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00002338 kernel_filename = NULL;
2339 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00002340 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00002341 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002342
aliguori268a3622009-04-21 22:30:27 +00002343 for (i = 0; i < MAX_NODES; i++) {
2344 node_mem[i] = 0;
2345 node_cpumask[i] = 0;
2346 }
2347
aliguori268a3622009-04-21 22:30:27 +00002348 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002349 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002350
blueswir141bd6392008-10-05 09:56:21 +00002351 autostart= 1;
2352
Anthony Liguori292444c2010-01-21 10:57:58 -06002353 /* first pass of option parsing */
2354 optind = 1;
2355 while (optind < argc) {
2356 if (argv[optind][0] != '-') {
2357 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002358 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002359 continue;
2360 } else {
2361 const QEMUOption *popt;
2362
2363 popt = lookup_opt(argc, argv, &optarg, &optind);
2364 switch (popt->index) {
2365 case QEMU_OPTION_nodefconfig:
2366 defconfig=0;
2367 break;
2368 }
2369 }
2370 }
2371
2372 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002373 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002374
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002375 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02002376 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002377 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002378 }
2379
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002380 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02002381 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002382 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002383 }
2384 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00002385 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06002386
2387 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002388 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002389 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002390 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002391 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002392 if (argv[optind][0] != '-') {
Markus Armbruster2292dda2011-01-28 11:21:41 +01002393 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00002394 } else {
2395 const QEMUOption *popt;
2396
Anthony Liguori6530a972010-01-22 09:18:06 -06002397 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002398 if (!(popt->arch_mask & arch_type)) {
2399 printf("Option %s not supported for this target\n", popt->name);
2400 exit(1);
2401 }
bellardcd6f1162004-05-13 22:02:20 +00002402 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002403 case QEMU_OPTION_M:
Jan Kiszka9052ea62011-07-23 12:38:37 +02002404 machine = machine_parse(optarg);
bellardcc1daa42005-06-05 14:49:17 +00002405 break;
j_mayer94fc95c2007-03-05 19:44:02 +00002406 case QEMU_OPTION_cpu:
2407 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00002408 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00002409 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00002410 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00002411 } else {
2412 cpu_model = optarg;
2413 }
2414 break;
bellardcd6f1162004-05-13 22:02:20 +00002415 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00002416 initrd_filename = optarg;
2417 break;
bellardcd6f1162004-05-13 22:02:20 +00002418 case QEMU_OPTION_hda:
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002419 {
2420 char buf[256];
2421 if (cyls == 0)
2422 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2423 else
2424 snprintf(buf, sizeof(buf),
2425 "%s,cyls=%d,heads=%d,secs=%d%s",
2426 HD_OPTS , cyls, heads, secs,
2427 translation == BIOS_ATA_TRANSLATION_LBA ?
thse4bcb142007-12-02 04:51:10 +00002428 ",trans=lba" :
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002429 translation == BIOS_ATA_TRANSLATION_NONE ?
thse4bcb142007-12-02 04:51:10 +00002430 ",trans=none" : "");
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002431 drive_add(IF_DEFAULT, 0, optarg, buf);
2432 break;
2433 }
bellardcd6f1162004-05-13 22:02:20 +00002434 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002435 case QEMU_OPTION_hdc:
2436 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002437 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2438 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00002439 break;
thse4bcb142007-12-02 04:51:10 +00002440 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04002441 if (drive_def(optarg) == NULL) {
2442 exit(1);
2443 }
thse4bcb142007-12-02 04:51:10 +00002444 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002445 case QEMU_OPTION_set:
2446 if (qemu_set_option(optarg) != 0)
2447 exit(1);
2448 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002449 case QEMU_OPTION_global:
2450 if (qemu_global_option(optarg) != 0)
2451 exit(1);
2452 break;
balrog3e3d5812007-04-30 02:09:25 +00002453 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002454 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00002455 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002456 case QEMU_OPTION_sd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002457 drive_add(IF_SD, 0, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00002458 break;
j_mayer86f55662007-04-24 06:52:59 +00002459 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002460 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00002461 break;
bellardcd6f1162004-05-13 22:02:20 +00002462 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002463 snapshot = 1;
2464 break;
bellardcd6f1162004-05-13 22:02:20 +00002465 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002466 {
bellard330d0412003-07-26 18:11:40 +00002467 const char *p;
2468 p = optarg;
2469 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002470 if (cyls < 1 || cyls > 16383)
2471 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002472 if (*p != ',')
2473 goto chs_fail;
2474 p++;
2475 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002476 if (heads < 1 || heads > 16)
2477 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002478 if (*p != ',')
2479 goto chs_fail;
2480 p++;
2481 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002482 if (secs < 1 || secs > 63)
2483 goto chs_fail;
2484 if (*p == ',') {
2485 p++;
2486 if (!strcmp(p, "none"))
2487 translation = BIOS_ATA_TRANSLATION_NONE;
2488 else if (!strcmp(p, "lba"))
2489 translation = BIOS_ATA_TRANSLATION_LBA;
2490 else if (!strcmp(p, "auto"))
2491 translation = BIOS_ATA_TRANSLATION_AUTO;
2492 else
2493 goto chs_fail;
2494 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002495 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002496 fprintf(stderr, "qemu: invalid physical CHS format\n");
2497 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002498 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002499 if (hda_opts != NULL) {
2500 char num[16];
2501 snprintf(num, sizeof(num), "%d", cyls);
2502 qemu_opt_set(hda_opts, "cyls", num);
2503 snprintf(num, sizeof(num), "%d", heads);
2504 qemu_opt_set(hda_opts, "heads", num);
2505 snprintf(num, sizeof(num), "%d", secs);
2506 qemu_opt_set(hda_opts, "secs", num);
2507 if (translation == BIOS_ATA_TRANSLATION_LBA)
2508 qemu_opt_set(hda_opts, "trans", "lba");
2509 if (translation == BIOS_ATA_TRANSLATION_NONE)
2510 qemu_opt_set(hda_opts, "trans", "none");
2511 }
bellard330d0412003-07-26 18:11:40 +00002512 }
2513 break;
aliguori268a3622009-04-21 22:30:27 +00002514 case QEMU_OPTION_numa:
2515 if (nb_numa_nodes >= MAX_NODES) {
2516 fprintf(stderr, "qemu: too many NUMA nodes\n");
2517 exit(1);
2518 }
2519 numa_add(optarg);
2520 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01002521 case QEMU_OPTION_display:
2522 display_type = select_display(optarg);
2523 break;
bellardcd6f1162004-05-13 22:02:20 +00002524 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002525 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002526 break;
balrog4d3b6f62008-02-10 16:33:14 +00002527 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01002528#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002529 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01002530#else
2531 fprintf(stderr, "Curses support is disabled\n");
2532 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00002533#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01002534 break;
balroga171fe32007-04-30 01:48:07 +00002535 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03002536 graphic_rotate = 90;
2537 break;
2538 case QEMU_OPTION_rotate:
2539 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2540 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2541 graphic_rotate != 180 && graphic_rotate != 270) {
2542 fprintf(stderr,
2543 "qemu: only 90, 180, 270 deg rotation is available\n");
2544 exit(1);
2545 }
balroga171fe32007-04-30 01:48:07 +00002546 break;
bellardcd6f1162004-05-13 22:02:20 +00002547 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002548 kernel_filename = optarg;
2549 break;
bellardcd6f1162004-05-13 22:02:20 +00002550 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002551 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002552 break;
bellardcd6f1162004-05-13 22:02:20 +00002553 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002554 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00002555 break;
bellardcd6f1162004-05-13 22:02:20 +00002556 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002557 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002558 static const char * const params[] = {
wayne3d3b8302011-07-27 18:04:55 +08002559 "order", "once", "menu",
2560 "splash", "splash-time", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002561 };
2562 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002563 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002564 int legacy = 0;
2565
2566 if (!strchr(optarg, '=')) {
2567 legacy = 1;
2568 pstrcpy(buf, sizeof(buf), optarg);
2569 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2570 fprintf(stderr,
2571 "qemu: unknown boot parameter '%s' in '%s'\n",
2572 buf, optarg);
2573 exit(1);
2574 }
2575
2576 if (legacy ||
2577 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002578 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002579 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002580 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002581 if (!legacy) {
2582 if (get_param_value(buf, sizeof(buf),
2583 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002584 validate_bootdevices(buf);
Anthony Liguori7267c092011-08-20 22:09:37 -05002585 standard_boot_devices = g_strdup(boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002586 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2587 qemu_register_reset(restore_boot_devices,
2588 standard_boot_devices);
2589 }
Jan Kiszka95387492009-07-02 00:19:02 +02002590 if (get_param_value(buf, sizeof(buf),
2591 "menu", optarg)) {
2592 if (!strcmp(buf, "on")) {
2593 boot_menu = 1;
2594 } else if (!strcmp(buf, "off")) {
2595 boot_menu = 0;
2596 } else {
2597 fprintf(stderr,
2598 "qemu: invalid option value '%s'\n",
2599 buf);
2600 exit(1);
2601 }
2602 }
wayne3d3b8302011-07-27 18:04:55 +08002603 qemu_opts_parse(qemu_find_opts("boot-opts"),
2604 optarg, 0);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002605 }
bellard36b486b2003-11-11 13:36:08 +00002606 }
2607 break;
bellardcd6f1162004-05-13 22:02:20 +00002608 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002609 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002610 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2611 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00002612 break;
bellard52ca8d62006-06-14 16:03:05 +00002613 case QEMU_OPTION_no_fd_bootchk:
2614 fd_bootchk = 0;
2615 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002616 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002617 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002618 exit(1);
2619 }
2620 break;
bellard7c9d8e02005-11-15 22:16:05 +00002621 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002622 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002623 exit(1);
2624 }
bellard702c6512004-04-02 21:21:32 +00002625 break;
bellardc7f74642004-08-24 21:57:12 +00002626#ifdef CONFIG_SLIRP
2627 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002628 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002629 break;
ths47d5d012007-02-20 00:05:08 +00002630 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002631 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002632 break;
bellard9bf05442004-08-25 22:12:49 +00002633 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002634 if (net_slirp_redir(optarg) < 0)
2635 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002636 break;
bellardc7f74642004-08-24 21:57:12 +00002637#endif
balrogdc72ac12008-11-09 00:04:26 +00002638 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002639 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002640 break;
bellard1d14ffa2005-10-30 18:58:22 +00002641 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002642 if (!(audio_available())) {
2643 printf("Option %s not supported for this target\n", popt->name);
2644 exit(1);
2645 }
bellard1d14ffa2005-10-30 18:58:22 +00002646 AUD_help ();
2647 exit (0);
2648 break;
2649 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002650 if (!(audio_available())) {
2651 printf("Option %s not supported for this target\n", popt->name);
2652 exit(1);
2653 }
bellard1d14ffa2005-10-30 18:58:22 +00002654 select_soundhw (optarg);
2655 break;
bellardcd6f1162004-05-13 22:02:20 +00002656 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002657 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002658 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002659 case QEMU_OPTION_version:
2660 version();
2661 exit(0);
2662 break;
aurel3200f82b82008-04-27 21:12:55 +00002663 case QEMU_OPTION_m: {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002664 int64_t value;
aurel3200f82b82008-04-27 21:12:55 +00002665
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002666 value = strtosz(optarg, NULL);
2667 if (value < 0) {
aurel3200f82b82008-04-27 21:12:55 +00002668 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002669 exit(1);
2670 }
aurel3200f82b82008-04-27 21:12:55 +00002671
Anthony Liguoric227f092009-10-01 16:12:16 -05002672 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002673 fprintf(stderr, "qemu: ram size too large\n");
2674 exit(1);
2675 }
2676 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002677 break;
aurel3200f82b82008-04-27 21:12:55 +00002678 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002679 case QEMU_OPTION_mempath:
2680 mem_path = optarg;
2681 break;
2682#ifdef MAP_POPULATE
2683 case QEMU_OPTION_mem_prealloc:
2684 mem_prealloc = 1;
2685 break;
2686#endif
bellardcd6f1162004-05-13 22:02:20 +00002687 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00002688 log_mask = optarg;
2689 break;
2690 case QEMU_OPTION_D:
2691 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002692 break;
bellardcd6f1162004-05-13 22:02:20 +00002693 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002694 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002695 break;
aliguori59030a82009-04-05 18:43:41 +00002696 case QEMU_OPTION_gdb:
2697 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002698 break;
bellardcd6f1162004-05-13 22:02:20 +00002699 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002700 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002701 break;
j_mayer1192dad2007-10-05 13:08:35 +00002702 case QEMU_OPTION_bios:
2703 bios_name = optarg;
2704 break;
aurel321b530a62009-04-05 20:08:59 +00002705 case QEMU_OPTION_singlestep:
2706 singlestep = 1;
2707 break;
bellardcd6f1162004-05-13 22:02:20 +00002708 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002709 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002710 break;
bellard3d11d0e2004-12-12 16:56:30 +00002711 case QEMU_OPTION_k:
2712 keyboard_layout = optarg;
2713 break;
bellardee22c2f2004-06-03 12:49:50 +00002714 case QEMU_OPTION_localtime:
2715 rtc_utc = 0;
2716 break;
malc3893c122008-09-28 00:42:05 +00002717 case QEMU_OPTION_vga:
2718 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002719 break;
bellarde9b137c2004-06-21 16:46:10 +00002720 case QEMU_OPTION_g:
2721 {
2722 const char *p;
2723 int w, h, depth;
2724 p = optarg;
2725 w = strtol(p, (char **)&p, 10);
2726 if (w <= 0) {
2727 graphic_error:
2728 fprintf(stderr, "qemu: invalid resolution or depth\n");
2729 exit(1);
2730 }
2731 if (*p != 'x')
2732 goto graphic_error;
2733 p++;
2734 h = strtol(p, (char **)&p, 10);
2735 if (h <= 0)
2736 goto graphic_error;
2737 if (*p == 'x') {
2738 p++;
2739 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002740 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002741 depth != 24 && depth != 32)
2742 goto graphic_error;
2743 } else if (*p == '\0') {
2744 depth = graphic_depth;
2745 } else {
2746 goto graphic_error;
2747 }
ths3b46e622007-09-17 08:09:54 +00002748
bellarde9b137c2004-06-21 16:46:10 +00002749 graphic_width = w;
2750 graphic_height = h;
2751 graphic_depth = depth;
2752 }
2753 break;
ths20d8a3e2007-02-18 17:04:49 +00002754 case QEMU_OPTION_echr:
2755 {
2756 char *r;
2757 term_escape_char = strtol(optarg, &r, 0);
2758 if (r == optarg)
2759 printf("Bad argument to echr\n");
2760 break;
2761 }
bellard82c643f2004-07-14 17:28:13 +00002762 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002763 monitor_parse(optarg, "readline");
2764 default_monitor = 0;
2765 break;
2766 case QEMU_OPTION_qmp:
2767 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002768 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002769 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002770 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002771 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002772 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002773 exit(1);
2774 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002775 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002776 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002777 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002778 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002779 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002780 exit(1);
2781 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002782 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302783 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002784 olist = qemu_find_opts("fsdev");
2785 if (!olist) {
2786 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2787 exit(1);
2788 }
2789 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302790 if (!opts) {
2791 fprintf(stderr, "parse error: %s\n", optarg);
2792 exit(1);
2793 }
2794 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302795 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002796 QemuOpts *fsdev;
2797 QemuOpts *device;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302798
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002799 olist = qemu_find_opts("virtfs");
2800 if (!olist) {
2801 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2802 exit(1);
2803 }
2804 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302805 if (!opts) {
2806 fprintf(stderr, "parse error: %s\n", optarg);
2807 exit(1);
2808 }
2809
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002810 if (qemu_opt_get(opts, "fstype") == NULL ||
2811 qemu_opt_get(opts, "mount_tag") == NULL ||
2812 qemu_opt_get(opts, "path") == NULL ||
2813 qemu_opt_get(opts, "security_model") == NULL) {
2814 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +05302815 "security_model=[mapped|passthrough|none],"
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002816 "mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002817 exit(1);
2818 }
2819
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002820 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2821 qemu_opt_get(opts, "mount_tag"), 1);
2822 if (!fsdev) {
2823 fprintf(stderr, "duplicate fsdev id: %s\n",
2824 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302825 exit(1);
2826 }
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002827 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2828 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2829 qemu_opt_set(fsdev, "security_model",
2830 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302831
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002832 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2833 qemu_opt_set(device, "driver", "virtio-9p-pci");
2834 qemu_opt_set(device, "fsdev",
2835 qemu_opt_get(opts, "mount_tag"));
2836 qemu_opt_set(device, "mount_tag",
2837 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302838 break;
2839 }
bellard82c643f2004-07-14 17:28:13 +00002840 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002841 add_device_config(DEV_SERIAL, optarg);
2842 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002843 if (strncmp(optarg, "mon:", 4) == 0) {
2844 default_monitor = 0;
2845 }
bellard82c643f2004-07-14 17:28:13 +00002846 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002847 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002848 if (watchdog) {
2849 fprintf(stderr,
2850 "qemu: only one watchdog option may be given\n");
2851 return 1;
2852 }
2853 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002854 break;
2855 case QEMU_OPTION_watchdog_action:
2856 if (select_watchdog_action(optarg) == -1) {
2857 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2858 exit(1);
2859 }
2860 break;
aliguori51ecf132009-01-15 20:06:40 +00002861 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002862 add_device_config(DEV_VIRTCON, optarg);
2863 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002864 if (strncmp(optarg, "mon:", 4) == 0) {
2865 default_monitor = 0;
2866 }
aliguori51ecf132009-01-15 20:06:40 +00002867 break;
bellard6508fe52005-01-15 12:02:56 +00002868 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002869 add_device_config(DEV_PARALLEL, optarg);
2870 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002871 if (strncmp(optarg, "mon:", 4) == 0) {
2872 default_monitor = 0;
2873 }
bellard6508fe52005-01-15 12:02:56 +00002874 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002875 case QEMU_OPTION_debugcon:
2876 add_device_config(DEV_DEBUGCON, optarg);
2877 break;
bellardd63d3072004-10-03 13:29:03 +00002878 case QEMU_OPTION_loadvm:
2879 loadvm = optarg;
2880 break;
2881 case QEMU_OPTION_full_screen:
2882 full_screen = 1;
2883 break;
ths667acca2006-12-11 02:08:05 +00002884#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002885 case QEMU_OPTION_no_frame:
2886 no_frame = 1;
2887 break;
ths3780e192007-06-21 21:08:02 +00002888 case QEMU_OPTION_alt_grab:
2889 alt_grab = 1;
2890 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002891 case QEMU_OPTION_ctrl_grab:
2892 ctrl_grab = 1;
2893 break;
ths667acca2006-12-11 02:08:05 +00002894 case QEMU_OPTION_no_quit:
2895 no_quit = 1;
2896 break;
aliguori7d957bd2009-01-15 22:14:11 +00002897 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002898 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002899 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01002900#else
2901 case QEMU_OPTION_no_frame:
2902 case QEMU_OPTION_alt_grab:
2903 case QEMU_OPTION_ctrl_grab:
2904 case QEMU_OPTION_no_quit:
2905 case QEMU_OPTION_sdl:
2906 fprintf(stderr, "SDL support is disabled\n");
2907 exit(1);
ths667acca2006-12-11 02:08:05 +00002908#endif
bellardf7cce892004-12-08 22:21:25 +00002909 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002910 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002911 break;
bellarda09db212005-04-30 16:10:35 +00002912 case QEMU_OPTION_win2k_hack:
2913 win2k_install_hack = 1;
2914 break;
aliguori73822ec2009-01-15 20:11:34 +00002915 case QEMU_OPTION_rtc_td_hack:
2916 rtc_td_hack = 1;
2917 break;
aliguori8a92ea22009-02-27 20:12:36 +00002918 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002919 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002920 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002921 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002922 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002923 break;
aliguori7ba1e612008-11-05 16:04:33 +00002924 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01002925 olist = qemu_find_opts("machine");
2926 qemu_opts_reset(olist);
2927 qemu_opts_parse(olist, "accel=kvm", 0);
2928 break;
2929 case QEMU_OPTION_machine:
2930 olist = qemu_find_opts("machine");
2931 qemu_opts_reset(olist);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002932 opts = qemu_opts_parse(olist, optarg, 1);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002933 if (!opts) {
2934 fprintf(stderr, "parse error: %s\n", optarg);
2935 exit(1);
2936 }
Jan Kiszka2645c6d2011-07-25 18:11:20 +02002937 optarg = qemu_opt_get(opts, "type");
2938 if (optarg) {
2939 machine = machine_parse(optarg);
2940 }
aliguori7ba1e612008-11-05 16:04:33 +00002941 break;
bellardbb36d472005-11-05 14:22:28 +00002942 case QEMU_OPTION_usb:
2943 usb_enabled = 1;
2944 break;
bellarda594cfb2005-11-06 16:13:29 +00002945 case QEMU_OPTION_usbdevice:
2946 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002947 add_device_config(DEV_USB, optarg);
2948 break;
2949 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002950 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002951 exit(1);
2952 }
bellarda594cfb2005-11-06 16:13:29 +00002953 break;
bellard6a00d602005-11-21 23:25:50 +00002954 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002955 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002956 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002957 fprintf(stderr, "Invalid number of CPUs\n");
2958 exit(1);
2959 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002960 if (max_cpus < smp_cpus) {
2961 fprintf(stderr, "maxcpus must be equal to or greater than "
2962 "smp\n");
2963 exit(1);
2964 }
2965 if (max_cpus > 255) {
2966 fprintf(stderr, "Unsupported number of maxcpus\n");
2967 exit(1);
2968 }
bellard6a00d602005-11-21 23:25:50 +00002969 break;
bellard24236862006-04-30 21:28:36 +00002970 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01002971#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002972 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01002973 vnc_display = optarg;
2974#else
2975 fprintf(stderr, "VNC support is disabled\n");
2976 exit(1);
2977#endif
2978 break;
bellard6515b202006-05-03 22:02:44 +00002979 case QEMU_OPTION_no_acpi:
2980 acpi_enabled = 0;
2981 break;
aliguori16b29ae2008-12-17 23:28:44 +00002982 case QEMU_OPTION_no_hpet:
2983 no_hpet = 1;
2984 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002985 case QEMU_OPTION_balloon:
2986 if (balloon_parse(optarg) < 0) {
2987 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2988 exit(1);
2989 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002990 break;
bellardd1beab82006-10-02 19:44:22 +00002991 case QEMU_OPTION_no_reboot:
2992 no_reboot = 1;
2993 break;
aurel32b2f76162008-04-11 21:35:52 +00002994 case QEMU_OPTION_no_shutdown:
2995 no_shutdown = 1;
2996 break;
balrog9467cd42007-05-01 01:34:14 +00002997 case QEMU_OPTION_show_cursor:
2998 cursor_hide = 0;
2999 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003000 case QEMU_OPTION_uuid:
3001 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3002 fprintf(stderr, "Fail to parse UUID string."
3003 " Wrong format.\n");
3004 exit(1);
3005 }
3006 break;
ths9ae02552007-01-05 17:39:04 +00003007 case QEMU_OPTION_option_rom:
3008 if (nb_option_roms >= MAX_OPTION_ROMS) {
3009 fprintf(stderr, "Too many option ROMs\n");
3010 exit(1);
3011 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02003012 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3013 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3014 option_rom[nb_option_roms].bootindex =
3015 qemu_opt_get_number(opts, "bootindex", -1);
3016 if (!option_rom[nb_option_roms].name) {
3017 fprintf(stderr, "Option ROM file is not specified\n");
3018 exit(1);
3019 }
ths9ae02552007-01-05 17:39:04 +00003020 nb_option_roms++;
3021 break;
pbrook8e716212007-01-20 17:12:09 +00003022 case QEMU_OPTION_semihosting:
3023 semihosting_enabled = 1;
3024 break;
thsc35734b2007-03-19 15:17:08 +00003025 case QEMU_OPTION_name:
Anthony Liguori7267c092011-08-20 22:09:37 -05003026 qemu_name = g_strdup(optarg);
Andi Kleen18894652009-07-02 09:34:17 +02003027 {
3028 char *p = strchr(qemu_name, ',');
3029 if (p != NULL) {
3030 *p++ = 0;
3031 if (strncmp(p, "process=", 8)) {
Aurelien Jarno5697f6a2010-12-27 18:29:20 +01003032 fprintf(stderr, "Unknown subargument %s to -name\n", p);
Andi Kleen18894652009-07-02 09:34:17 +02003033 exit(1);
3034 }
3035 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02003036 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02003037 }
3038 }
thsc35734b2007-03-19 15:17:08 +00003039 break;
blueswir166508602007-05-01 14:16:52 +00003040 case QEMU_OPTION_prom_env:
3041 if (nb_prom_envs >= MAX_PROM_ENVS) {
3042 fprintf(stderr, "Too many prom variables\n");
3043 exit(1);
3044 }
3045 prom_envs[nb_prom_envs] = optarg;
3046 nb_prom_envs++;
3047 break;
balrog2b8f2d42007-07-27 22:08:46 +00003048 case QEMU_OPTION_old_param:
3049 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003050 break;
thsf3dcfad2007-08-24 01:26:02 +00003051 case QEMU_OPTION_clock:
3052 configure_alarms(optarg);
3053 break;
bellard7e0af5d02007-11-07 16:24:33 +00003054 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003055 configure_rtc_date_offset(optarg, 1);
3056 break;
3057 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003058 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003059 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003060 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003061 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003062 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003063 break;
bellard26a5f132008-05-28 12:30:31 +00003064 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003065 tcg_tb_size = strtol(optarg, NULL, 0);
3066 if (tcg_tb_size < 0) {
3067 tcg_tb_size = 0;
3068 }
bellard26a5f132008-05-28 12:30:31 +00003069 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003070 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003071 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003072 break;
aliguori5bb79102008-10-13 03:12:02 +00003073 case QEMU_OPTION_incoming:
3074 incoming = optarg;
3075 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003076 case QEMU_OPTION_nodefaults:
3077 default_serial = 0;
3078 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003079 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003080 default_monitor = 0;
3081 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003082 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003083 default_floppy = 0;
3084 default_cdrom = 0;
3085 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003086 break;
aliguorie37630c2009-04-22 15:19:10 +00003087 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003088 if (!(xen_available())) {
3089 printf("Option %s not supported for this target\n", popt->name);
3090 exit(1);
3091 }
aliguorie37630c2009-04-22 15:19:10 +00003092 xen_domid = atoi(optarg);
3093 break;
3094 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003095 if (!(xen_available())) {
3096 printf("Option %s not supported for this target\n", popt->name);
3097 exit(1);
3098 }
aliguorie37630c2009-04-22 15:19:10 +00003099 xen_mode = XEN_CREATE;
3100 break;
3101 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003102 if (!(xen_available())) {
3103 printf("Option %s not supported for this target\n", popt->name);
3104 exit(1);
3105 }
aliguorie37630c2009-04-22 15:19:10 +00003106 xen_mode = XEN_ATTACH;
3107 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003108 case QEMU_OPTION_trace:
Lluíse4858972011-08-31 20:31:03 +02003109 {
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003110 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
Lluíse4858972011-08-31 20:31:03 +02003111 if (!opts) {
3112 exit(1);
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003113 }
Lluís23d15e82011-08-31 20:31:31 +02003114 trace_events = qemu_opt_get(opts, "events");
Lluíse4858972011-08-31 20:31:03 +02003115 trace_file = qemu_opt_get(opts, "file");
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003116 break;
Lluíse4858972011-08-31 20:31:03 +02003117 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003118 case QEMU_OPTION_readconfig:
3119 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003120 int ret = qemu_read_config_file(optarg);
3121 if (ret < 0) {
3122 fprintf(stderr, "read config %s: %s\n", optarg,
3123 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003124 exit(1);
3125 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003126 break;
3127 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003128 case QEMU_OPTION_spice:
3129 olist = qemu_find_opts("spice");
3130 if (!olist) {
3131 fprintf(stderr, "spice is not supported by this qemu build.\n");
3132 exit(1);
3133 }
3134 opts = qemu_opts_parse(olist, optarg, 0);
3135 if (!opts) {
3136 fprintf(stderr, "parse error: %s\n", optarg);
3137 exit(1);
3138 }
3139 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003140 case QEMU_OPTION_writeconfig:
3141 {
3142 FILE *fp;
3143 if (strcmp(optarg, "-") == 0) {
3144 fp = stdout;
3145 } else {
3146 fp = fopen(optarg, "w");
3147 if (fp == NULL) {
3148 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3149 exit(1);
3150 }
3151 }
3152 qemu_config_write(fp);
3153 fclose(fp);
3154 break;
3155 }
Jes Sorensen59a52642010-06-10 11:42:25 +02003156 default:
3157 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003158 }
bellard0824d6f2003-06-24 13:42:40 +00003159 }
3160 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003161 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003162
Matthew Fernandezc235d732011-06-07 16:32:40 +00003163 /* Open the logfile at this point, if necessary. We can't open the logfile
3164 * when encountering either of the logging options (-d or -D) because the
3165 * other one may be encountered later on the command line, changing the
3166 * location or level of logging.
3167 */
3168 if (log_mask) {
3169 if (log_file) {
3170 set_cpu_log_filename(log_file);
3171 }
3172 set_cpu_log(log_mask);
3173 }
3174
Lluís23d15e82011-08-31 20:31:31 +02003175 if (!trace_backend_init(trace_events, trace_file)) {
Lluíse4858972011-08-31 20:31:03 +02003176 exit(1);
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00003177 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00003178
Paul Brook5cea8592009-05-30 00:52:44 +01003179 /* If no data_dir is specified then try to find it relative to the
3180 executable path. */
3181 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02003182 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01003183 }
Stefan Weil60474fb2011-09-04 15:17:46 +02003184 /* If all else fails use the install path specified when building. */
Paul Brook5cea8592009-05-30 00:52:44 +01003185 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02003186 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003187 }
3188
Jes Sorensen6be68d72009-07-23 17:03:42 +02003189 /*
3190 * Default to max_cpus = smp_cpus, in case the user doesn't
3191 * specify a max_cpus value.
3192 */
3193 if (!max_cpus)
3194 max_cpus = smp_cpus;
3195
balrog3d878ca2008-10-28 10:59:59 +00003196 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003197 if (smp_cpus > machine->max_cpus) {
3198 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3199 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3200 machine->max_cpus);
3201 exit(1);
3202 }
3203
Anthony PERARD67b724e2010-11-22 15:44:15 +00003204 /*
3205 * Get the default machine options from the machine if it is not already
3206 * specified either by the configuration file or by the command line.
3207 */
3208 if (machine->default_machine_opts) {
3209 QemuOptsList *list = qemu_find_opts("machine");
3210 const char *p = NULL;
3211
3212 if (!QTAILQ_EMPTY(&list->head)) {
3213 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3214 }
3215 if (p == NULL) {
Jan Kiszka9052ea62011-07-23 12:38:37 +02003216 qemu_opts_reset(list);
3217 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00003218 if (!opts) {
3219 fprintf(stderr, "parse error for machine %s: %s\n",
3220 machine->name, machine->default_machine_opts);
3221 exit(1);
3222 }
3223 }
3224 }
3225
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003226 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3227 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003228
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003229 if (machine->no_serial) {
3230 default_serial = 0;
3231 }
3232 if (machine->no_parallel) {
3233 default_parallel = 0;
3234 }
3235 if (!machine->use_virtcon) {
3236 default_virtcon = 0;
3237 }
3238 if (machine->no_vga) {
3239 default_vga = 0;
3240 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003241 if (machine->no_floppy) {
3242 default_floppy = 0;
3243 }
3244 if (machine->no_cdrom) {
3245 default_cdrom = 0;
3246 }
3247 if (machine->no_sdcard) {
3248 default_sdcard = 0;
3249 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003250
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003251 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003252 if (default_parallel)
3253 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003254 if (default_serial && default_monitor) {
3255 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003256 } else if (default_virtcon && default_monitor) {
3257 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003258 } else {
3259 if (default_serial)
3260 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003261 if (default_virtcon)
3262 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003263 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003264 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003265 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003266 } else {
3267 if (default_serial)
3268 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003269 if (default_parallel)
3270 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003271 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003272 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003273 if (default_virtcon)
3274 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003275 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01003276 if (default_vga)
3277 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00003278
TeLeMana5829fd2010-04-15 12:37:55 +08003279 socket_init();
3280
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003281 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003282 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07003283#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003284 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303285 exit(1);
3286 }
3287#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003288
Jes Sorenseneb505be2010-06-10 11:42:28 +02003289 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00003290
thsaa26bb22007-03-25 21:33:06 +00003291 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02003292 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00003293 exit(1);
3294 }
3295
Jan Kiszka0ac543d2011-08-15 16:18:57 -07003296 /* init the memory */
3297 if (ram_size == 0) {
3298 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3299 }
3300
Anthony PERARD303d4e82010-09-21 20:05:31 +01003301 configure_accelerator();
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003302
aliguori3fcf7b62009-04-24 18:03:25 +00003303 if (qemu_init_main_loop()) {
3304 fprintf(stderr, "qemu_init_main_loop failed\n");
3305 exit(1);
3306 }
bellarda20dd502003-09-30 21:07:02 +00003307 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003308
thsf8d39c02008-07-03 10:01:15 +00003309 if (!linux_boot && *kernel_cmdline != '\0') {
3310 fprintf(stderr, "-append only allowed with -kernel option\n");
3311 exit(1);
3312 }
3313
3314 if (!linux_boot && initrd_filename != NULL) {
3315 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3316 exit(1);
3317 }
3318
Jes Sorensen9156d762010-06-10 11:42:30 +02003319 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00003320
aliguori7183b4b2008-11-05 20:40:18 +00003321 if (init_timer_alarm() < 0) {
3322 fprintf(stderr, "could not initialize alarm timer\n");
3323 exit(1);
3324 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003325 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003326
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003327 if (net_init_clients() < 0) {
3328 exit(1);
bellard702c6512004-04-02 21:21:32 +00003329 }
bellardf1510b22003-06-25 00:07:40 +00003330
balrogdc72ac12008-11-09 00:04:26 +00003331 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003332 if (foreach_device_config(DEV_BT, bt_parse))
3333 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003334
Anthony PERARD834e76e2011-07-20 08:17:44 +00003335 if (!xen_enabled()) {
3336 /* On 32-bit hosts, QEMU is limited by virtual address space */
3337 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3338 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3339 exit(1);
3340 }
3341 }
balrog7fb4fdc2008-04-24 17:59:27 +00003342
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003343 cpu_exec_init_all();
bellard26a5f132008-05-28 12:30:31 +00003344
Markus Armbrustereb852012009-10-27 18:41:44 +01003345 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003346
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003347 blk_mig_init();
3348
ths96d30e42007-01-07 20:42:14 +00003349 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003350 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003351 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3352 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003353 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00003354
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01003355 default_drive(default_cdrom, snapshot, machine->use_scsi,
3356 IF_DEFAULT, 2, CDROM_OPTS);
3357 default_drive(default_floppy, snapshot, machine->use_scsi,
3358 IF_FLOPPY, 0, FD_OPTS);
3359 default_drive(default_sdcard, snapshot, machine->use_scsi,
3360 IF_SD, 0, SD_OPTS);
3361
Alex Williamson97ab12d2010-06-25 11:09:50 -06003362 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003363 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003364
aliguori268a3622009-04-21 22:30:27 +00003365 if (nb_numa_nodes > 0) {
3366 int i;
3367
Sasha Levinea0e5412011-06-29 23:29:39 -04003368 if (nb_numa_nodes > MAX_NODES) {
3369 nb_numa_nodes = MAX_NODES;
aliguori268a3622009-04-21 22:30:27 +00003370 }
3371
3372 /* If no memory size if given for any node, assume the default case
3373 * and distribute the available memory equally across all nodes
3374 */
3375 for (i = 0; i < nb_numa_nodes; i++) {
3376 if (node_mem[i] != 0)
3377 break;
3378 }
3379 if (i == nb_numa_nodes) {
3380 uint64_t usedmem = 0;
3381
3382 /* On Linux, the each node's border has to be 8MB aligned,
3383 * the final node gets the rest.
3384 */
3385 for (i = 0; i < nb_numa_nodes - 1; i++) {
3386 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3387 usedmem += node_mem[i];
3388 }
3389 node_mem[i] = ram_size - usedmem;
3390 }
3391
3392 for (i = 0; i < nb_numa_nodes; i++) {
3393 if (node_cpumask[i] != 0)
3394 break;
3395 }
3396 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3397 * must cope with this anyway, because there are BIOSes out there in
3398 * real machines which also use this scheme.
3399 */
3400 if (i == nb_numa_nodes) {
3401 for (i = 0; i < smp_cpus; i++) {
3402 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3403 }
3404 }
3405 }
3406
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003407 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02003408 exit(1);
3409 }
3410
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003411 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3412 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003413 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3414 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003415 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3416 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003417 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3418 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003419
Paul Brookaae94602009-05-14 22:35:06 +01003420 module_call_init(MODULE_INIT_DEVICE);
3421
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003422 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01003423 exit(0);
3424
Markus Armbruster09aaa162009-08-21 10:31:34 +02003425 if (watchdog) {
3426 i = select_watchdog(watchdog);
3427 if (i > 0)
3428 exit (i == 1 ? 1 : 0);
3429 }
3430
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003431 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003432 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003433 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003434 qemu_add_globals();
3435
Paul Brookfbe1b592009-05-13 17:56:25 +01003436 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00003437 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3438
Jan Kiszkaea375f92010-03-01 19:10:30 +01003439 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003440
Blue Swirl87d0a282010-03-27 18:24:45 +00003441 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003442
aliguori6f338c32009-02-11 15:21:54 +00003443 current_machine = machine;
3444
aliguori3023f332009-01-16 19:04:14 +00003445 /* init USB devices */
3446 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01003447 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3448 exit(1);
aliguori3023f332009-01-16 19:04:14 +00003449 }
3450
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003451 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003452 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003453 exit(1);
3454
Markus Armbruster668680f2010-02-11 14:44:58 +01003455 net_check_clients();
3456
aliguori3023f332009-01-16 19:04:14 +00003457 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003458 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003459
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003460 if (using_spice)
3461 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003462 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003463#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003464 display_type = DT_SDL;
Jes Sorensen821601e2011-03-16 13:33:36 +01003465#elif defined(CONFIG_VNC)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003466 vnc_display = "localhost:0,to=99";
3467 show_vnc_port = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +01003468#else
3469 display_type = DT_NONE;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003470#endif
3471 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003472
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003473
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003474 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003475 switch (display_type) {
3476 case DT_NOGRAPHIC:
3477 break;
3478#if defined(CONFIG_CURSES)
3479 case DT_CURSES:
3480 curses_display_init(ds, full_screen);
3481 break;
3482#endif
bellard5b0753e2005-03-01 21:37:28 +00003483#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003484 case DT_SDL:
3485 sdl_display_init(ds, full_screen, no_frame);
3486 break;
bellard5b0753e2005-03-01 21:37:28 +00003487#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003488 case DT_SDL:
3489 cocoa_display_init(ds, full_screen);
3490 break;
bellard313aa562003-08-10 21:52:11 +00003491#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003492 default:
3493 break;
3494 }
3495
Gleb Natapov0ce235a2011-03-31 11:27:23 +02003496 /* must be after terminal init, SDL library changes signal handlers */
3497 os_setup_signal_handling();
3498
Jes Sorensen821601e2011-03-16 13:33:36 +01003499#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003500 /* init remote displays */
3501 if (vnc_display) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003502 vnc_display_init(ds);
3503 if (vnc_display_open(ds, vnc_display) < 0)
3504 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003505
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003506 if (show_vnc_port) {
3507 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003508 }
bellard313aa562003-08-10 21:52:11 +00003509 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003510#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003511#ifdef CONFIG_SPICE
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02003512 if (using_spice && !qxl_enabled) {
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003513 qemu_spice_display_init(ds);
3514 }
3515#endif
aliguori5b08fc12008-08-21 20:08:03 +00003516
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003517 /* display setup */
3518 dpy_resize(ds);
aliguori3023f332009-01-16 19:04:14 +00003519 dcl = ds->listeners;
3520 while (dcl != NULL) {
3521 if (dcl->dpy_refresh != NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003522 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3523 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09003524 break;
ths20d8a3e2007-02-18 17:04:49 +00003525 }
aliguori3023f332009-01-16 19:04:14 +00003526 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00003527 }
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003528 if (ds->gui_timer == NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003529 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3530 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00003531 }
Paolo Bonzinib473df62010-02-11 00:29:55 +01003532 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003533
aliguori59030a82009-04-05 18:43:41 +00003534 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3535 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3536 gdbstub_dev);
3537 exit(1);
balrog45669e02007-07-02 13:20:17 +00003538 }
balrog45669e02007-07-02 13:20:17 +00003539
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003540 qdev_machine_creation_done();
3541
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003542 if (rom_load_all() != 0) {
3543 fprintf(stderr, "rom loading failed\n");
3544 exit(1);
3545 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003546
Isaku Yamahata80376c32010-12-20 14:33:35 +09003547 /* TODO: once all bus devices are qdevified, this should be done
3548 * when bus is created by qdev.c */
3549 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02003550 qemu_run_machine_init_done_notifiers();
3551
Jan Kiszkae063eb12011-06-14 18:29:43 +02003552 qemu_system_reset(VMRESET_SILENT);
Juan Quintela05f24012009-08-20 19:42:22 +02003553 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003554 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003555 autostart = 0;
3556 }
3557 }
bellardd63d3072004-10-03 13:29:03 +00003558
Glauber Costa2bb8c102009-07-24 16:20:23 -04003559 if (incoming) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003560 runstate_set(RUN_STATE_INMIGRATE);
Juan Quintela8ca5e802010-06-09 14:10:54 +02003561 int ret = qemu_start_incoming_migration(incoming);
3562 if (ret < 0) {
3563 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3564 incoming, ret);
3565 exit(ret);
3566 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03003567 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003568 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003569 }
thsffd843b2006-12-21 19:46:43 +00003570
Jes Sorenseneb505be2010-06-10 11:42:28 +02003571 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003572
bellard8a7ddc32004-03-31 19:00:16 +00003573 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003574 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003575 net_cleanup();
wayne3d3b8302011-07-27 18:04:55 +08003576 res_free();
thsb46a8902007-10-21 23:20:45 +00003577
bellard0824d6f2003-06-24 13:42:40 +00003578 return 0;
3579}