blob: 1cb7ce2801a98f7767cf12a644a616b1b720b14f [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
blueswir1511d2b12009-03-07 15:32:56 +0000114#include "hw/hw.h"
115#include "hw/boards.h"
116#include "hw/usb.h"
117#include "hw/pcmcia.h"
118#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000119#include "hw/isa.h"
120#include "hw/baum.h"
121#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100122#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000123#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000124#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200125#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200126#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000127#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000128#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000129#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000130#include "monitor.h"
131#include "console.h"
132#include "sysemu.h"
133#include "gdbstub.h"
134#include "qemu-timer.h"
135#include "qemu-char.h"
136#include "cache-utils.h"
137#include "block.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200138#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200139#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000140#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000141#include "audio/audio.h"
142#include "migration.h"
143#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200144#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200145#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200146#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200147#include "qemu-options.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700148#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530149#include "fsdev/qemu-fsdev.h"
150#endif
blueswir1511d2b12009-03-07 15:32:56 +0000151
bellard0824d6f2003-06-24 13:42:40 +0000152#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000153
blueswir1511d2b12009-03-07 15:32:56 +0000154#include "qemu_socket.h"
155
Jan Kiszkad918f232009-06-24 14:42:30 +0200156#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000157
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000158#include "trace.h"
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000159#include "simpletrace.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000160#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000161#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000162#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000163
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300164#include "ui/qemu-spice.h"
165
blueswir19dc63a12008-10-04 07:25:46 +0000166//#define DEBUG_NET
167//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000168
bellard1bfe8562004-07-08 21:17:50 +0000169#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000170
Amit Shah98b19252010-01-20 00:36:52 +0530171#define MAX_VIRTIO_CONSOLES 1
172
Paul Brook5cea8592009-05-30 00:52:44 +0100173static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000174const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000175enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500176DisplayType display_type = DT_DEFAULT;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +0200177int display_remote = 0;
bellard3d11d0e2004-12-12 16:56:30 +0000178const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500179ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300180const char *mem_path = NULL;
181#ifdef MAP_POPULATE
182int mem_prealloc = 0; /* force preallocation of physical target memory */
183#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000184int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000185NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000186int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200187int autostart;
Amit Shah8e848652010-07-27 15:49:19 +0530188int incoming_expected; /* Started with -incoming and waiting for incoming */
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;
ths73fc9742006-12-22 02:09:07 +0000209const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000210int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000211int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000212int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000213int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000214int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000215int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000216int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200217uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200218const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200219QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000220int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000221int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000222int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000223const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000224int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500225int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000226unsigned int nb_prom_envs = 0;
227const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200228int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000229
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200230typedef struct FWBootEntry FWBootEntry;
231
232struct FWBootEntry {
233 QTAILQ_ENTRY(FWBootEntry) link;
234 int32_t bootindex;
235 DeviceState *dev;
236 char *suffix;
237};
238
239QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
240
aliguori268a3622009-04-21 22:30:27 +0000241int nb_numa_nodes;
242uint64_t node_mem[MAX_NODES];
243uint64_t node_cpumask[MAX_NODES];
244
blueswir19043b622009-01-21 19:28:13 +0000245static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000246
blueswir18fcb1b92008-09-18 18:29:08 +0000247uint8_t qemu_uuid[16];
248
Jan Kiszka76e30d02009-07-02 00:19:02 +0200249static QEMUBootSetHandler *boot_set_handler;
250static void *boot_set_opaque;
251
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200252static NotifierList exit_notifiers =
253 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
254
Gleb Natapov4cab9462010-12-08 13:35:08 +0200255static NotifierList machine_init_done_notifiers =
256 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
257
Blue Swirld745bef2010-03-29 19:23:49 +0000258int kvm_allowed = 0;
259uint32_t xen_domid;
260enum xen_mode xen_mode = XEN_EMULATE;
261
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100262static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100263static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100264static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100265static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100266static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100267static int default_floppy = 1;
268static int default_cdrom = 1;
269static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100270
271static struct {
272 const char *driver;
273 int *flag;
274} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100275 { .driver = "isa-serial", .flag = &default_serial },
276 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100277 { .driver = "isa-fdc", .flag = &default_floppy },
278 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530279 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
280 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
281 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100282 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100283 { .driver = "cirrus-vga", .flag = &default_vga },
284 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100285};
286
287static int default_driver_check(QemuOpts *opts, void *opaque)
288{
289 const char *driver = qemu_opt_get(opts, "driver");
290 int i;
291
292 if (!driver)
293 return 0;
294 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
295 if (strcmp(default_list[i].driver, driver) != 0)
296 continue;
297 *(default_list[i].flag) = 0;
298 }
299 return 0;
300}
301
bellard0824d6f2003-06-24 13:42:40 +0000302/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100303/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000304
bellardc4b1fcc2004-03-14 21:44:30 +0000305/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000306/* host time/date access */
307void qemu_get_timedate(struct tm *tm, int offset)
308{
309 time_t ti;
310 struct tm *ret;
311
312 time(&ti);
313 ti += offset;
314 if (rtc_date_offset == -1) {
315 if (rtc_utc)
316 ret = gmtime(&ti);
317 else
318 ret = localtime(&ti);
319 } else {
320 ti -= rtc_date_offset;
321 ret = gmtime(&ti);
322 }
323
324 memcpy(tm, ret, sizeof(struct tm));
325}
326
327int qemu_timedate_diff(struct tm *tm)
328{
329 time_t seconds;
330
331 if (rtc_date_offset == -1)
332 if (rtc_utc)
333 seconds = mktimegm(tm);
334 else
335 seconds = mktime(tm);
336 else
337 seconds = mktimegm(tm) + rtc_date_offset;
338
339 return seconds - time(NULL);
340}
341
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300342void rtc_change_mon_event(struct tm *tm)
343{
344 QObject *data;
345
346 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
347 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
348 qobject_decref(data);
349}
350
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200351static void configure_rtc_date_offset(const char *startdate, int legacy)
352{
353 time_t rtc_start_date;
354 struct tm tm;
355
356 if (!strcmp(startdate, "now") && legacy) {
357 rtc_date_offset = -1;
358 } else {
359 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
360 &tm.tm_year,
361 &tm.tm_mon,
362 &tm.tm_mday,
363 &tm.tm_hour,
364 &tm.tm_min,
365 &tm.tm_sec) == 6) {
366 /* OK */
367 } else if (sscanf(startdate, "%d-%d-%d",
368 &tm.tm_year,
369 &tm.tm_mon,
370 &tm.tm_mday) == 3) {
371 tm.tm_hour = 0;
372 tm.tm_min = 0;
373 tm.tm_sec = 0;
374 } else {
375 goto date_fail;
376 }
377 tm.tm_year -= 1900;
378 tm.tm_mon--;
379 rtc_start_date = mktimegm(&tm);
380 if (rtc_start_date == -1) {
381 date_fail:
382 fprintf(stderr, "Invalid date format. Valid formats are:\n"
383 "'2006-06-17T16:01:21' or '2006-06-17'\n");
384 exit(1);
385 }
386 rtc_date_offset = time(NULL) - rtc_start_date;
387 }
388}
389
390static void configure_rtc(QemuOpts *opts)
391{
392 const char *value;
393
394 value = qemu_opt_get(opts, "base");
395 if (value) {
396 if (!strcmp(value, "utc")) {
397 rtc_utc = 1;
398 } else if (!strcmp(value, "localtime")) {
399 rtc_utc = 0;
400 } else {
401 configure_rtc_date_offset(value, 0);
402 }
403 }
Jan Kiszka68752042009-09-15 13:36:04 +0200404 value = qemu_opt_get(opts, "clock");
405 if (value) {
406 if (!strcmp(value, "host")) {
407 rtc_clock = host_clock;
408 } else if (!strcmp(value, "vm")) {
409 rtc_clock = vm_clock;
410 } else {
411 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
412 exit(1);
413 }
414 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200415 value = qemu_opt_get(opts, "driftfix");
416 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000417 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200418 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000419 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200420 rtc_td_hack = 0;
421 } else {
422 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
423 exit(1);
424 }
425 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200426}
427
balrog1ae26a12008-09-28 23:19:47 +0000428/***********************************************************/
429/* Bluetooth support */
430static int nb_hcis;
431static int cur_hci;
432static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000433
balrog1ae26a12008-09-28 23:19:47 +0000434static struct bt_vlan_s {
435 struct bt_scatternet_s net;
436 int id;
437 struct bt_vlan_s *next;
438} *first_bt_vlan;
439
440/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000441static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000442{
443 struct bt_vlan_s **pvlan, *vlan;
444 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
445 if (vlan->id == id)
446 return &vlan->net;
447 }
448 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
449 vlan->id = id;
450 pvlan = &first_bt_vlan;
451 while (*pvlan != NULL)
452 pvlan = &(*pvlan)->next;
453 *pvlan = vlan;
454 return &vlan->net;
455}
456
457static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
458{
459}
460
461static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
462{
463 return -ENOTSUP;
464}
465
466static struct HCIInfo null_hci = {
467 .cmd_send = null_hci_send,
468 .sco_send = null_hci_send,
469 .acl_send = null_hci_send,
470 .bdaddr_set = null_hci_addr_set,
471};
472
473struct HCIInfo *qemu_next_hci(void)
474{
475 if (cur_hci == nb_hcis)
476 return &null_hci;
477
478 return hci_table[cur_hci++];
479}
480
balrogdc72ac12008-11-09 00:04:26 +0000481static struct HCIInfo *hci_init(const char *str)
482{
483 char *endp;
484 struct bt_scatternet_s *vlan = 0;
485
486 if (!strcmp(str, "null"))
487 /* null */
488 return &null_hci;
489 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
490 /* host[:hciN] */
491 return bt_host_hci(str[4] ? str + 5 : "hci0");
492 else if (!strncmp(str, "hci", 3)) {
493 /* hci[,vlan=n] */
494 if (str[3]) {
495 if (!strncmp(str + 3, ",vlan=", 6)) {
496 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
497 if (*endp)
498 vlan = 0;
499 }
500 } else
501 vlan = qemu_find_bt_vlan(0);
502 if (vlan)
503 return bt_new_hci(vlan);
504 }
505
506 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
507
508 return 0;
509}
510
511static int bt_hci_parse(const char *str)
512{
513 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500514 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000515
516 if (nb_hcis >= MAX_NICS) {
517 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
518 return -1;
519 }
520
521 hci = hci_init(str);
522 if (!hci)
523 return -1;
524
525 bdaddr.b[0] = 0x52;
526 bdaddr.b[1] = 0x54;
527 bdaddr.b[2] = 0x00;
528 bdaddr.b[3] = 0x12;
529 bdaddr.b[4] = 0x34;
530 bdaddr.b[5] = 0x56 + nb_hcis;
531 hci->bdaddr_set(hci, bdaddr.b);
532
533 hci_table[nb_hcis++] = hci;
534
535 return 0;
536}
537
538static void bt_vhci_add(int vlan_id)
539{
540 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
541
542 if (!vlan->slave)
543 fprintf(stderr, "qemu: warning: adding a VHCI to "
544 "an empty scatternet %i\n", vlan_id);
545
546 bt_vhci_init(bt_new_hci(vlan));
547}
548
549static struct bt_device_s *bt_device_add(const char *opt)
550{
551 struct bt_scatternet_s *vlan;
552 int vlan_id = 0;
553 char *endp = strstr(opt, ",vlan=");
554 int len = (endp ? endp - opt : strlen(opt)) + 1;
555 char devname[10];
556
557 pstrcpy(devname, MIN(sizeof(devname), len), opt);
558
559 if (endp) {
560 vlan_id = strtol(endp + 6, &endp, 0);
561 if (*endp) {
562 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
563 return 0;
564 }
565 }
566
567 vlan = qemu_find_bt_vlan(vlan_id);
568
569 if (!vlan->slave)
570 fprintf(stderr, "qemu: warning: adding a slave device to "
571 "an empty scatternet %i\n", vlan_id);
572
573 if (!strcmp(devname, "keyboard"))
574 return bt_keyboard_init(vlan);
575
576 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
577 return 0;
578}
579
580static int bt_parse(const char *opt)
581{
582 const char *endp, *p;
583 int vlan;
584
585 if (strstart(opt, "hci", &endp)) {
586 if (!*endp || *endp == ',') {
587 if (*endp)
588 if (!strstart(endp, ",vlan=", 0))
589 opt = endp + 1;
590
591 return bt_hci_parse(opt);
592 }
593 } else if (strstart(opt, "vhci", &endp)) {
594 if (!*endp || *endp == ',') {
595 if (*endp) {
596 if (strstart(endp, ",vlan=", &p)) {
597 vlan = strtol(p, (char **) &endp, 0);
598 if (*endp) {
599 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
600 return 1;
601 }
602 } else {
603 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
604 return 1;
605 }
606 } else
607 vlan = 0;
608
609 bt_vhci_add(vlan);
610 return 0;
611 }
612 } else if (strstart(opt, "device:", &endp))
613 return !bt_device_add(endp);
614
615 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
616 return 1;
617}
618
balrog1ae26a12008-09-28 23:19:47 +0000619/***********************************************************/
620/* QEMU Block devices */
621
Markus Armbruster2292dda2011-01-28 11:21:41 +0100622#define HD_OPTS "media=disk"
623#define CDROM_OPTS "media=cdrom"
624#define FD_OPTS ""
625#define PFLASH_OPTS ""
626#define MTD_OPTS ""
627#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +0000628
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200629static int drive_init_func(QemuOpts *opts, void *opaque)
630{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200631 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200632
Markus Armbruster319ae522011-01-28 11:21:46 +0100633 return drive_init(opts, *use_scsi) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200634}
635
636static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
637{
638 if (NULL == qemu_opt_get(opts, "snapshot")) {
639 qemu_opt_set(opts, "snapshot", "on");
640 }
641 return 0;
642}
643
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100644static void default_drive(int enable, int snapshot, int use_scsi,
645 BlockInterfaceType type, int index,
646 const char *optstr)
647{
648 QemuOpts *opts;
649
650 if (type == IF_DEFAULT) {
651 type = use_scsi ? IF_SCSI : IF_IDE;
652 }
653
654 if (!enable || drive_get_by_index(type, index)) {
655 return;
656 }
657
658 opts = drive_add(type, index, NULL, optstr);
659 if (snapshot) {
660 drive_enable_snapshot(opts, NULL);
661 }
Markus Armbruster319ae522011-01-28 11:21:46 +0100662 if (!drive_init(opts, use_scsi)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100663 exit(1);
664 }
665}
666
Jan Kiszka76e30d02009-07-02 00:19:02 +0200667void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
668{
669 boot_set_handler = func;
670 boot_set_opaque = opaque;
671}
672
673int qemu_boot_set(const char *boot_devices)
674{
675 if (!boot_set_handler) {
676 return -EINVAL;
677 }
678 return boot_set_handler(boot_set_opaque, boot_devices);
679}
680
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300681static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200682{
683 /* We just do some generic consistency checks */
684 const char *p;
685 int bitmap = 0;
686
687 for (p = devices; *p != '\0'; p++) {
688 /* Allowed boot devices are:
689 * a-b: floppy disk drives
690 * c-f: IDE disk drives
691 * g-m: machine implementation dependant drives
692 * n-p: network devices
693 * It's up to each machine implementation to check if the given boot
694 * devices match the actual hardware implementation and firmware
695 * features.
696 */
697 if (*p < 'a' || *p > 'p') {
698 fprintf(stderr, "Invalid boot device '%c'\n", *p);
699 exit(1);
700 }
701 if (bitmap & (1 << (*p - 'a'))) {
702 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
703 exit(1);
704 }
705 bitmap |= 1 << (*p - 'a');
706 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200707}
708
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200709static void restore_boot_devices(void *opaque)
710{
711 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600712 static int first = 1;
713
714 /* Restore boot order and remove ourselves after the first boot */
715 if (first) {
716 first = 0;
717 return;
718 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200719
720 qemu_boot_set(standard_boot_devices);
721
722 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
723 qemu_free(standard_boot_devices);
724}
725
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200726void add_boot_device_path(int32_t bootindex, DeviceState *dev,
727 const char *suffix)
728{
729 FWBootEntry *node, *i;
730
731 if (bootindex < 0) {
732 return;
733 }
734
735 assert(dev != NULL || suffix != NULL);
736
737 node = qemu_mallocz(sizeof(FWBootEntry));
738 node->bootindex = bootindex;
Gleb Natapov4fef9302011-02-02 17:34:34 +0200739 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200740 node->dev = dev;
741
742 QTAILQ_FOREACH(i, &fw_boot_order, link) {
743 if (i->bootindex == bootindex) {
744 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
745 exit(1);
746 } else if (i->bootindex < bootindex) {
747 continue;
748 }
749 QTAILQ_INSERT_BEFORE(i, node, link);
750 return;
751 }
752 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
753}
754
Gleb Natapov962630f2010-12-08 13:35:09 +0200755/*
756 * This function returns null terminated string that consist of new line
757 * separated device pathes.
758 *
759 * memory pointed by "size" is assigned total length of the array in bytes
760 *
761 */
762char *get_boot_devices_list(uint32_t *size)
763{
764 FWBootEntry *i;
765 uint32_t total = 0;
766 char *list = NULL;
767
768 QTAILQ_FOREACH(i, &fw_boot_order, link) {
769 char *devpath = NULL, *bootpath;
770 int len;
771
772 if (i->dev) {
773 devpath = qdev_get_fw_dev_path(i->dev);
774 assert(devpath);
775 }
776
777 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +0000778 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
779
780 bootpath = qemu_malloc(bootpathlen);
781 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +0200782 qemu_free(devpath);
783 } else if (devpath) {
784 bootpath = devpath;
785 } else {
Gleb Natapov4fef9302011-02-02 17:34:34 +0200786 bootpath = qemu_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +0200787 assert(bootpath);
788 }
789
790 if (total) {
791 list[total-1] = '\n';
792 }
793 len = strlen(bootpath) + 1;
794 list = qemu_realloc(list, total + len);
795 memcpy(&list[total], bootpath, len);
796 total += len;
797 qemu_free(bootpath);
798 }
799
800 *size = total;
801
802 return list;
803}
804
aliguori268a3622009-04-21 22:30:27 +0000805static void numa_add(const char *optarg)
806{
807 char option[128];
808 char *endptr;
809 unsigned long long value, endvalue;
810 int nodenr;
811
812 optarg = get_opt_name(option, 128, optarg, ',') + 1;
813 if (!strcmp(option, "node")) {
814 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
815 nodenr = nb_numa_nodes;
816 } else {
817 nodenr = strtoull(option, NULL, 10);
818 }
819
820 if (get_param_value(option, 128, "mem", optarg) == 0) {
821 node_mem[nodenr] = 0;
822 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100823 int64_t sval;
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200824 sval = strtosz(option, NULL);
825 if (sval < 0) {
826 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
827 exit(1);
aliguori268a3622009-04-21 22:30:27 +0000828 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200829 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +0000830 }
831 if (get_param_value(option, 128, "cpus", optarg) == 0) {
832 node_cpumask[nodenr] = 0;
833 } else {
834 value = strtoull(option, &endptr, 10);
835 if (value >= 64) {
836 value = 63;
837 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
838 } else {
839 if (*endptr == '-') {
840 endvalue = strtoull(endptr+1, &endptr, 10);
841 if (endvalue >= 63) {
842 endvalue = 62;
843 fprintf(stderr,
844 "only 63 CPUs in NUMA mode supported.\n");
845 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100846 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000847 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100848 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000849 }
850 }
851 node_cpumask[nodenr] = value;
852 }
853 nb_numa_nodes++;
854 }
855 return;
856}
857
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200858static void smp_parse(const char *optarg)
859{
860 int smp, sockets = 0, threads = 0, cores = 0;
861 char *endptr;
862 char option[128];
863
864 smp = strtoul(optarg, &endptr, 10);
865 if (endptr != optarg) {
866 if (*endptr == ',') {
867 endptr++;
868 }
869 }
870 if (get_param_value(option, 128, "sockets", endptr) != 0)
871 sockets = strtoull(option, NULL, 10);
872 if (get_param_value(option, 128, "cores", endptr) != 0)
873 cores = strtoull(option, NULL, 10);
874 if (get_param_value(option, 128, "threads", endptr) != 0)
875 threads = strtoull(option, NULL, 10);
876 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
877 max_cpus = strtoull(option, NULL, 10);
878
879 /* compute missing values, prefer sockets over cores over threads */
880 if (smp == 0 || sockets == 0) {
881 sockets = sockets > 0 ? sockets : 1;
882 cores = cores > 0 ? cores : 1;
883 threads = threads > 0 ? threads : 1;
884 if (smp == 0) {
885 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200886 }
887 } else {
888 if (cores == 0) {
889 threads = threads > 0 ? threads : 1;
890 cores = smp / (sockets * threads);
891 } else {
Joel Schoppdca98162010-07-21 15:05:17 -0500892 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200893 }
894 }
895 smp_cpus = smp;
896 smp_cores = cores > 0 ? cores : 1;
897 smp_threads = threads > 0 ? threads : 1;
898 if (max_cpus == 0)
899 max_cpus = smp_cpus;
900}
901
bellard330d0412003-07-26 18:11:40 +0000902/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +0000903/* USB devices */
904
Markus Armbrusterfb080002010-05-28 15:38:44 +0200905static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +0000906{
907 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200908 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +0000909
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200910 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +0000911 return -1;
912
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +0100913 /* drivers with .usbdevice_name entry in USBDeviceInfo */
914 dev = usbdevice_create(devname);
915 if (dev)
916 goto done;
917
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200918 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +0000919 if (strstart(devname, "host:", &p)) {
920 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +0000921 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
922 dev = usb_bt_init(devname[2] ? hci_init(p) :
923 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +0000924 } else {
925 return -1;
926 }
pbrook0d92ed32006-05-21 16:30:15 +0000927 if (!dev)
928 return -1;
929
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200930done:
bellarda594cfb2005-11-06 16:13:29 +0000931 return 0;
932}
933
aliguori1f3870a2008-08-21 19:27:48 +0000934static int usb_device_del(const char *devname)
935{
936 int bus_num, addr;
937 const char *p;
938
aliguori5d0c5752008-09-14 01:07:41 +0000939 if (strstart(devname, "host:", &p))
940 return usb_host_device_close(p);
941
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200942 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +0000943 return -1;
944
945 p = strchr(devname, '.');
946 if (!p)
947 return -1;
948 bus_num = strtoul(devname, NULL, 0);
949 addr = strtoul(p + 1, NULL, 0);
950
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200951 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000952}
953
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200954static int usb_parse(const char *cmdline)
955{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800956 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +0200957 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800958 if (r < 0) {
959 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
960 }
961 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200962}
963
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300964void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000965{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800966 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +0200967 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100968 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800969 }
bellarda594cfb2005-11-06 16:13:29 +0000970}
971
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300972void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000973{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800974 const char *devname = qdict_get_str(qdict, "devname");
975 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100976 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800977 }
bellarda594cfb2005-11-06 16:13:29 +0000978}
979
bellardf7cce892004-12-08 22:21:25 +0000980/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +0000981/* PCMCIA/Cardbus */
982
983static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +0100984 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +0000985 struct pcmcia_socket_entry_s *next;
986} *pcmcia_sockets = 0;
987
Paul Brookbc24a222009-05-10 01:44:56 +0100988void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000989{
990 struct pcmcia_socket_entry_s *entry;
991
992 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
993 entry->socket = socket;
994 entry->next = pcmcia_sockets;
995 pcmcia_sockets = entry;
996}
997
Paul Brookbc24a222009-05-10 01:44:56 +0100998void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000999{
1000 struct pcmcia_socket_entry_s *entry, **ptr;
1001
1002 ptr = &pcmcia_sockets;
1003 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1004 if (entry->socket == socket) {
1005 *ptr = entry->next;
1006 qemu_free(entry);
1007 }
1008}
1009
aliguori376253e2009-03-05 23:01:23 +00001010void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001011{
1012 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001013
balrog201a51f2007-04-30 00:51:09 +00001014 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001015 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001016
1017 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001018 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1019 iter->socket->attached ? iter->socket->card_string :
1020 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001021}
1022
1023/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001024/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00001025
bellardc4b1fcc2004-03-14 21:44:30 +00001026typedef struct IOHandlerRecord {
1027 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +01001028 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +00001029 IOHandler *fd_read;
1030 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00001031 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00001032 void *opaque;
1033 /* temporary data */
1034 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +01001035 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +00001036} IOHandlerRecord;
1037
Juan Quintela31d4ee62010-03-11 17:55:37 +01001038static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1039 QLIST_HEAD_INITIALIZER(io_handlers);
1040
bellardc4b1fcc2004-03-14 21:44:30 +00001041
bellard7c9d8e02005-11-15 22:16:05 +00001042/* XXX: fd_read_poll should be suppressed, but an API change is
1043 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00001044int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +01001045 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +00001046 IOHandler *fd_read,
1047 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001048 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00001049{
Juan Quintela31d4ee62010-03-11 17:55:37 +01001050 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +00001051
bellard7c9d8e02005-11-15 22:16:05 +00001052 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001053 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001054 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00001055 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00001056 break;
1057 }
bellard8a7ddc32004-03-31 19:00:16 +00001058 }
bellard7c9d8e02005-11-15 22:16:05 +00001059 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001060 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001061 if (ioh->fd == fd)
1062 goto found;
1063 }
1064 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +01001065 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +00001066 found:
1067 ioh->fd = fd;
1068 ioh->fd_read_poll = fd_read_poll;
1069 ioh->fd_read = fd_read;
1070 ioh->fd_write = fd_write;
1071 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00001072 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00001073 }
bellard7c9d8e02005-11-15 22:16:05 +00001074 return 0;
1075}
1076
ths5fafdf22007-09-16 21:08:06 +00001077int qemu_set_fd_handler(int fd,
1078 IOHandler *fd_read,
1079 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001080 void *opaque)
1081{
1082 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00001083}
1084
bellard8a7ddc32004-03-31 19:00:16 +00001085/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001086/* machine registration */
1087
blueswir1bdaf78e2008-10-04 07:24:27 +00001088static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001089QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001090
1091int qemu_register_machine(QEMUMachine *m)
1092{
1093 QEMUMachine **pm;
1094 pm = &first_machine;
1095 while (*pm != NULL)
1096 pm = &(*pm)->next;
1097 m->next = NULL;
1098 *pm = m;
1099 return 0;
1100}
1101
pbrook9596ebb2007-11-18 01:44:38 +00001102static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001103{
1104 QEMUMachine *m;
1105
1106 for(m = first_machine; m != NULL; m = m->next) {
1107 if (!strcmp(m->name, name))
1108 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001109 if (m->alias && !strcmp(m->alias, name))
1110 return m;
bellardcc1daa42005-06-05 14:49:17 +00001111 }
1112 return NULL;
1113}
1114
Anthony Liguori0c257432009-05-21 20:41:01 -05001115static QEMUMachine *find_default_machine(void)
1116{
1117 QEMUMachine *m;
1118
1119 for(m = first_machine; m != NULL; m = m->next) {
1120 if (m->is_default) {
1121 return m;
1122 }
1123 }
1124 return NULL;
1125}
1126
bellardcc1daa42005-06-05 14:49:17 +00001127/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001128/* main execution loop */
1129
pbrook9596ebb2007-11-18 01:44:38 +00001130static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001131{
aliguori7d957bd2009-01-15 22:14:11 +00001132 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001133 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001134 DisplayChangeListener *dcl = ds->listeners;
1135
Sheng Yang62a27442010-01-26 19:21:16 +08001136 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001137 dpy_refresh(ds);
1138
1139 while (dcl != NULL) {
1140 if (dcl->gui_timer_interval &&
1141 dcl->gui_timer_interval < interval)
1142 interval = dcl->gui_timer_interval;
1143 dcl = dcl->next;
1144 }
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001145 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001146}
1147
blueswir19043b622009-01-21 19:28:13 +00001148static void nographic_update(void *opaque)
1149{
1150 uint64_t interval = GUI_REFRESH_INTERVAL;
1151
Sheng Yang62a27442010-01-26 19:21:16 +08001152 qemu_flush_coalesced_mmio_buffer();
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001153 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00001154}
1155
bellard0bd48852005-11-11 00:00:47 +00001156struct vm_change_state_entry {
1157 VMChangeStateHandler *cb;
1158 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001159 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001160};
1161
Blue Swirl72cf2d42009-09-12 07:36:22 +00001162static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001163
1164VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1165 void *opaque)
1166{
1167 VMChangeStateEntry *e;
1168
1169 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001170
1171 e->cb = cb;
1172 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001173 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001174 return e;
1175}
1176
1177void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1178{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001179 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001180 qemu_free (e);
1181}
1182
Blue Swirl296af7c2010-03-29 19:23:50 +00001183void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001184{
1185 VMChangeStateEntry *e;
1186
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001187 trace_vm_state_notify(running, reason);
1188
bellard0bd48852005-11-11 00:00:47 +00001189 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001190 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001191 }
1192}
1193
bellard8a7ddc32004-03-31 19:00:16 +00001194void vm_start(void)
1195{
1196 if (!vm_running) {
1197 cpu_enable_ticks();
1198 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001199 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001200 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001201 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001202 }
1203}
1204
bellardbb0c6722004-06-20 12:37:32 +00001205/* reset/shutdown handler */
1206
1207typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001208 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001209 QEMUResetHandler *func;
1210 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001211} QEMUResetEntry;
1212
Blue Swirl72cf2d42009-09-12 07:36:22 +00001213static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1214 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001215static int reset_requested;
1216static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00001217static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001218static int debug_requested;
1219static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001220
aurel32cf7a2fe2008-03-18 06:53:05 +00001221int qemu_shutdown_requested(void)
1222{
1223 int r = shutdown_requested;
1224 shutdown_requested = 0;
1225 return r;
1226}
1227
1228int qemu_reset_requested(void)
1229{
1230 int r = reset_requested;
1231 reset_requested = 0;
1232 return r;
1233}
1234
1235int qemu_powerdown_requested(void)
1236{
1237 int r = powerdown_requested;
1238 powerdown_requested = 0;
1239 return r;
1240}
1241
aliguorie5689022009-04-24 18:03:54 +00001242static int qemu_debug_requested(void)
1243{
1244 int r = debug_requested;
1245 debug_requested = 0;
1246 return r;
1247}
1248
aliguori6e29f5d2009-04-24 18:04:02 +00001249static int qemu_vmstop_requested(void)
1250{
1251 int r = vmstop_requested;
1252 vmstop_requested = 0;
1253 return r;
1254}
1255
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001256void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001257{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001258 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001259
bellardbb0c6722004-06-20 12:37:32 +00001260 re->func = func;
1261 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001262 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001263}
1264
Jan Kiszkadda9b292009-07-02 00:19:02 +02001265void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001266{
1267 QEMUResetEntry *re;
1268
Blue Swirl72cf2d42009-09-12 07:36:22 +00001269 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001270 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001271 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001272 qemu_free(re);
1273 return;
1274 }
1275 }
1276}
1277
1278void qemu_system_reset(void)
1279{
1280 QEMUResetEntry *re, *nre;
1281
1282 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001283 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001284 re->func(re->opaque);
1285 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001286 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001287 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001288}
1289
1290void qemu_system_reset_request(void)
1291{
bellardd1beab82006-10-02 19:44:22 +00001292 if (no_reboot) {
1293 shutdown_requested = 1;
1294 } else {
1295 reset_requested = 1;
1296 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001297 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001298 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001299}
1300
1301void qemu_system_shutdown_request(void)
1302{
1303 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001304 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001305}
1306
bellard34751872005-07-02 14:31:34 +00001307void qemu_system_powerdown_request(void)
1308{
1309 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001310 qemu_notify_event();
1311}
1312
Jan Kiszka8cf71712011-02-07 12:19:16 +01001313void qemu_system_debug_request(void)
1314{
1315 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001316 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001317}
1318
1319void qemu_system_vmstop_request(int reason)
1320{
1321 vmstop_requested = reason;
1322 qemu_notify_event();
1323}
1324
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001325void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001326{
1327 IOHandlerRecord *ioh;
1328 fd_set rfds, wfds, xfds;
1329 int ret, nfds;
1330 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001331 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001332
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001333 if (nonblocking)
1334 timeout = 0;
1335 else {
1336 timeout = qemu_calculate_timeout();
1337 qemu_bh_update_timeout(&timeout);
1338 }
aliguori56f3a5d2008-10-31 18:07:17 +00001339
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001340 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001341
bellardfd1dff42006-02-01 21:29:26 +00001342 /* poll any events */
1343 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001344 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001345 FD_ZERO(&rfds);
1346 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001347 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001348 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00001349 if (ioh->deleted)
1350 continue;
bellardfd1dff42006-02-01 21:29:26 +00001351 if (ioh->fd_read &&
1352 (!ioh->fd_read_poll ||
1353 ioh->fd_read_poll(ioh->opaque) != 0)) {
1354 FD_SET(ioh->fd, &rfds);
1355 if (ioh->fd > nfds)
1356 nfds = ioh->fd;
1357 }
1358 if (ioh->fd_write) {
1359 FD_SET(ioh->fd, &wfds);
1360 if (ioh->fd > nfds)
1361 nfds = ioh->fd;
1362 }
1363 }
ths3b46e622007-09-17 08:09:54 +00001364
aliguori56f3a5d2008-10-31 18:07:17 +00001365 tv.tv_sec = timeout / 1000;
1366 tv.tv_usec = (timeout % 1000) * 1000;
1367
Jan Kiszkad918f232009-06-24 14:42:30 +02001368 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1369
aliguori48708522009-04-24 18:03:49 +00001370 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001371 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001372 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00001373 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001374 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00001375
Juan Quintela31d4ee62010-03-11 17:55:37 +01001376 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
Stefan Hajnoczi0290b572010-11-03 14:29:44 +00001377 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1378 ioh->fd_read(ioh->opaque);
1379 }
1380 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1381 ioh->fd_write(ioh->opaque);
1382 }
1383
1384 /* Do this last in case read/write handlers marked it for deletion */
thscafffd42007-02-28 21:59:44 +00001385 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001386 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00001387 qemu_free(ioh);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001388 }
thscafffd42007-02-28 21:59:44 +00001389 }
bellardfd1dff42006-02-01 21:29:26 +00001390 }
Jan Kiszkad918f232009-06-24 14:42:30 +02001391
1392 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001393
Paolo Bonzinib6964822010-03-10 11:38:45 +01001394 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001395
pbrook423f0742007-05-23 00:06:54 +00001396 /* Check bottom-halves last in case any of the earlier events triggered
1397 them. */
1398 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001399
bellard5905b2e2004-08-01 21:53:26 +00001400}
1401
Jan Kiszka46481d32011-02-01 22:15:47 +01001402#ifndef CONFIG_IOTHREAD
1403static int vm_request_pending(void)
aliguori43b96852009-04-24 18:03:33 +00001404{
Jan Kiszka46481d32011-02-01 22:15:47 +01001405 return powerdown_requested ||
1406 reset_requested ||
1407 shutdown_requested ||
1408 debug_requested ||
1409 vmstop_requested;
aliguori43b96852009-04-24 18:03:33 +00001410}
Jan Kiszka46481d32011-02-01 22:15:47 +01001411#endif
aliguori43b96852009-04-24 18:03:33 +00001412
Blue Swirld9c32312009-08-09 08:42:19 +00001413qemu_irq qemu_system_powerdown;
1414
aliguori43b96852009-04-24 18:03:33 +00001415static void main_loop(void)
1416{
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01001417 bool nonblocking = false;
1418#ifdef CONFIG_PROFILER
1419 int64_t ti;
1420#endif
aliguori6e29f5d2009-04-24 18:04:02 +00001421 int r;
aliguori43b96852009-04-24 18:03:33 +00001422
Blue Swirl7277e022010-04-12 17:19:06 +00001423 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001424
aliguori6e29f5d2009-04-24 18:04:02 +00001425 for (;;) {
aliguorid6dc3d42009-04-24 18:04:07 +00001426#ifndef CONFIG_IOTHREAD
Jan Kiszka46481d32011-02-01 22:15:47 +01001427 nonblocking = cpu_exec_all();
1428 if (vm_request_pending()) {
1429 nonblocking = true;
1430 }
aliguorid6dc3d42009-04-24 18:04:07 +00001431#endif
aliguori43b96852009-04-24 18:03:33 +00001432#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001433 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00001434#endif
Jan Kiszka46481d32011-02-01 22:15:47 +01001435 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001436#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001437 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00001438#endif
aliguori43b96852009-04-24 18:03:33 +00001439
Jan Kiszka8cf71712011-02-07 12:19:16 +01001440 if (qemu_debug_requested()) {
1441 vm_stop(VMSTOP_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001442 }
aliguori43b96852009-04-24 18:03:33 +00001443 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001444 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001445 if (no_shutdown) {
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001446 vm_stop(VMSTOP_SHUTDOWN);
aliguori43b96852009-04-24 18:03:33 +00001447 no_shutdown = 0;
1448 } else
1449 break;
1450 }
aliguorid6dc3d42009-04-24 18:04:07 +00001451 if (qemu_reset_requested()) {
1452 pause_all_vcpus();
Jan Kiszkaa7ada152011-03-02 08:56:11 +01001453 cpu_synchronize_all_states();
aliguori43b96852009-04-24 18:03:33 +00001454 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00001455 resume_all_vcpus();
1456 }
Blue Swirld9c32312009-08-09 08:42:19 +00001457 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001458 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001459 qemu_irq_raise(qemu_system_powerdown);
1460 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001461 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001462 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001463 }
aliguori43b96852009-04-24 18:03:33 +00001464 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001465 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001466 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001467}
1468
pbrook9bd7e6d2009-04-07 22:58:45 +00001469static void version(void)
1470{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001471 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001472}
1473
ths15f82202007-06-29 23:26:08 +00001474static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001475{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001476 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001477#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1478 opt_help
blueswir15824d652009-03-28 06:44:27 +00001479#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001480#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001481#undef DEF
1482#undef DEFHEADING
1483#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001484 ;
1485 version();
1486 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001487 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001488 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001489 "\n"
1490 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001491 "During emulation, the following keys are useful:\n"
1492 "ctrl-alt-f toggle full screen\n"
1493 "ctrl-alt-n switch to virtual console 'n'\n"
1494 "ctrl-alt toggle mouse and keyboard grab\n"
1495 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001496 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1497 "qemu",
1498 options_help);
ths15f82202007-06-29 23:26:08 +00001499 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001500}
1501
bellardcd6f1162004-05-13 22:02:20 +00001502#define HAS_ARG 0x0001
1503
bellardcd6f1162004-05-13 22:02:20 +00001504typedef struct QEMUOption {
1505 const char *name;
1506 int flags;
1507 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001508 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001509} QEMUOption;
1510
blueswir1dbed7e42008-10-01 19:38:09 +00001511static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001512 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1513#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1514 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001515#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001516#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001517#undef DEF
1518#undef DEFHEADING
1519#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001520 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001521};
malc3893c122008-09-28 00:42:05 +00001522static void select_vgahw (const char *p)
1523{
1524 const char *opts;
1525
Gerd Hoffmann64465292009-12-08 13:11:45 +01001526 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001527 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001528 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001529 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001530 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001531 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001532 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001533 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001534 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001535 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001536 } else if (strstart(p, "qxl", &opts)) {
1537 vga_interface_type = VGA_QXL;
aliguori28b85ed2009-04-22 15:19:48 +00001538 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001539 invalid_vga:
1540 fprintf(stderr, "Unknown vga type: %s\n", p);
1541 exit(1);
1542 }
malccb5a7aa2008-09-28 00:42:12 +00001543 while (*opts) {
1544 const char *nextopt;
1545
1546 if (strstart(opts, ",retrace=", &nextopt)) {
1547 opts = nextopt;
1548 if (strstart(opts, "dumb", &nextopt))
1549 vga_retrace_method = VGA_RETRACE_DUMB;
1550 else if (strstart(opts, "precise", &nextopt))
1551 vga_retrace_method = VGA_RETRACE_PRECISE;
1552 else goto invalid_vga;
1553 } else goto invalid_vga;
1554 opts = nextopt;
1555 }
malc3893c122008-09-28 00:42:05 +00001556}
1557
Jes Sorensen1472a952011-03-16 13:33:31 +01001558static DisplayType select_display(const char *p)
1559{
1560 const char *opts;
1561 DisplayType display = DT_DEFAULT;
1562
1563 if (strstart(p, "sdl", &opts)) {
1564#ifdef CONFIG_SDL
1565 display = DT_SDL;
1566 while (*opts) {
1567 const char *nextopt;
1568
1569 if (strstart(opts, ",frame=", &nextopt)) {
1570 opts = nextopt;
1571 if (strstart(opts, "on", &nextopt)) {
1572 no_frame = 0;
1573 } else if (strstart(opts, "off", &nextopt)) {
1574 no_frame = 1;
1575 } else {
1576 goto invalid_display;
1577 }
1578 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1579 opts = nextopt;
1580 if (strstart(opts, "on", &nextopt)) {
1581 alt_grab = 1;
1582 } else if (strstart(opts, "off", &nextopt)) {
1583 alt_grab = 0;
1584 } else {
1585 goto invalid_display;
1586 }
1587 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1588 opts = nextopt;
1589 if (strstart(opts, "on", &nextopt)) {
1590 ctrl_grab = 1;
1591 } else if (strstart(opts, "off", &nextopt)) {
1592 ctrl_grab = 0;
1593 } else {
1594 goto invalid_display;
1595 }
1596 } else if (strstart(opts, ",window_close=", &nextopt)) {
1597 opts = nextopt;
1598 if (strstart(opts, "on", &nextopt)) {
1599 no_quit = 0;
1600 } else if (strstart(opts, "off", &nextopt)) {
1601 no_quit = 1;
1602 } else {
1603 goto invalid_display;
1604 }
1605 } else {
1606 goto invalid_display;
1607 }
1608 opts = nextopt;
1609 }
1610#else
1611 fprintf(stderr, "SDL support is disabled\n");
1612 exit(1);
1613#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01001614 } else if (strstart(p, "vnc", &opts)) {
1615 display_remote++;
1616
1617 if (*opts) {
1618 const char *nextopt;
1619
1620 if (strstart(opts, "=", &nextopt)) {
1621 vnc_display = nextopt;
1622 }
1623 }
1624 if (!vnc_display) {
1625 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1626 exit(1);
1627 }
Jes Sorensen1472a952011-03-16 13:33:31 +01001628 } else if (strstart(p, "curses", &opts)) {
1629#ifdef CONFIG_CURSES
1630 display = DT_CURSES;
1631#else
1632 fprintf(stderr, "Curses support is disabled\n");
1633 exit(1);
1634#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01001635 } else if (strstart(p, "none", &opts)) {
1636 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01001637 } else {
1638 invalid_display:
1639 fprintf(stderr, "Unknown display type: %s\n", p);
1640 exit(1);
1641 }
1642
1643 return display;
1644}
1645
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001646static int balloon_parse(const char *arg)
1647{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001648 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001649
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001650 if (strcmp(arg, "none") == 0) {
1651 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001652 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001653
1654 if (!strncmp(arg, "virtio", 6)) {
1655 if (arg[6] == ',') {
1656 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001657 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001658 if (!opts)
1659 return -1;
1660 } else {
1661 /* create empty opts */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001662 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001663 }
1664 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1665 return 0;
1666 }
1667
1668 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001669}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001670
Paul Brook5cea8592009-05-30 00:52:44 +01001671char *qemu_find_file(int type, const char *name)
1672{
1673 int len;
1674 const char *subdir;
1675 char *buf;
1676
1677 /* If name contains path separators then try it as a straight path. */
1678 if ((strchr(name, '/') || strchr(name, '\\'))
1679 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02001680 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001681 }
1682 switch (type) {
1683 case QEMU_FILE_TYPE_BIOS:
1684 subdir = "";
1685 break;
1686 case QEMU_FILE_TYPE_KEYMAP:
1687 subdir = "keymaps/";
1688 break;
1689 default:
1690 abort();
1691 }
1692 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1693 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001694 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001695 if (access(buf, R_OK)) {
1696 qemu_free(buf);
1697 return NULL;
1698 }
1699 return buf;
1700}
1701
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001702static int device_help_func(QemuOpts *opts, void *opaque)
1703{
1704 return qdev_device_help(opts);
1705}
1706
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001707static int device_init_func(QemuOpts *opts, void *opaque)
1708{
1709 DeviceState *dev;
1710
1711 dev = qdev_device_add(opts);
1712 if (!dev)
1713 return -1;
1714 return 0;
1715}
1716
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001717static int chardev_init_func(QemuOpts *opts, void *opaque)
1718{
1719 CharDriverState *chr;
1720
1721 chr = qemu_chr_open_opts(opts, NULL);
1722 if (!chr)
1723 return -1;
1724 return 0;
1725}
1726
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07001727#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301728static int fsdev_init_func(QemuOpts *opts, void *opaque)
1729{
1730 int ret;
1731 ret = qemu_fsdev_add(opts);
1732
1733 return ret;
1734}
1735#endif
1736
Gerd Hoffmann88589342009-12-08 13:11:50 +01001737static int mon_init_func(QemuOpts *opts, void *opaque)
1738{
1739 CharDriverState *chr;
1740 const char *chardev;
1741 const char *mode;
1742 int flags;
1743
1744 mode = qemu_opt_get(opts, "mode");
1745 if (mode == NULL) {
1746 mode = "readline";
1747 }
1748 if (strcmp(mode, "readline") == 0) {
1749 flags = MONITOR_USE_READLINE;
1750 } else if (strcmp(mode, "control") == 0) {
1751 flags = MONITOR_USE_CONTROL;
1752 } else {
1753 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1754 exit(1);
1755 }
1756
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01001757 if (qemu_opt_get_bool(opts, "pretty", 0))
1758 flags |= MONITOR_USE_PRETTY;
1759
Gerd Hoffmann88589342009-12-08 13:11:50 +01001760 if (qemu_opt_get_bool(opts, "default", 0))
1761 flags |= MONITOR_IS_DEFAULT;
1762
1763 chardev = qemu_opt_get(opts, "chardev");
1764 chr = qemu_chr_find(chardev);
1765 if (chr == NULL) {
1766 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1767 exit(1);
1768 }
1769
1770 monitor_init(chr, flags);
1771 return 0;
1772}
1773
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001774static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001775{
1776 static int monitor_device_index = 0;
1777 QemuOpts *opts;
1778 const char *p;
1779 char label[32];
1780 int def = 0;
1781
1782 if (strstart(optarg, "chardev:", &p)) {
1783 snprintf(label, sizeof(label), "%s", p);
1784 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001785 snprintf(label, sizeof(label), "compat_monitor%d",
1786 monitor_device_index);
1787 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001788 def = 1;
1789 }
1790 opts = qemu_chr_parse_compat(label, optarg);
1791 if (!opts) {
1792 fprintf(stderr, "parse error: %s\n", optarg);
1793 exit(1);
1794 }
1795 }
1796
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001797 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001798 if (!opts) {
1799 fprintf(stderr, "duplicate chardev: %s\n", label);
1800 exit(1);
1801 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001802 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001803 qemu_opt_set(opts, "chardev", label);
1804 if (def)
1805 qemu_opt_set(opts, "default", "on");
1806 monitor_device_index++;
1807}
1808
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001809struct device_config {
1810 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001811 DEV_USB, /* -usbdevice */
1812 DEV_BT, /* -bt */
1813 DEV_SERIAL, /* -serial */
1814 DEV_PARALLEL, /* -parallel */
1815 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001816 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001817 } type;
1818 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001819 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001820};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001821QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001822
1823static void add_device_config(int type, const char *cmdline)
1824{
1825 struct device_config *conf;
1826
1827 conf = qemu_mallocz(sizeof(*conf));
1828 conf->type = type;
1829 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001830 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001831}
1832
1833static int foreach_device_config(int type, int (*func)(const char *cmdline))
1834{
1835 struct device_config *conf;
1836 int rc;
1837
Blue Swirl72cf2d42009-09-12 07:36:22 +00001838 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001839 if (conf->type != type)
1840 continue;
1841 rc = func(conf->cmdline);
1842 if (0 != rc)
1843 return rc;
1844 }
1845 return 0;
1846}
1847
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001848static int serial_parse(const char *devname)
1849{
1850 static int index = 0;
1851 char label[32];
1852
1853 if (strcmp(devname, "none") == 0)
1854 return 0;
1855 if (index == MAX_SERIAL_PORTS) {
1856 fprintf(stderr, "qemu: too many serial ports\n");
1857 exit(1);
1858 }
1859 snprintf(label, sizeof(label), "serial%d", index);
1860 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1861 if (!serial_hds[index]) {
1862 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1863 devname, strerror(errno));
1864 return -1;
1865 }
1866 index++;
1867 return 0;
1868}
1869
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01001870static int parallel_parse(const char *devname)
1871{
1872 static int index = 0;
1873 char label[32];
1874
1875 if (strcmp(devname, "none") == 0)
1876 return 0;
1877 if (index == MAX_PARALLEL_PORTS) {
1878 fprintf(stderr, "qemu: too many parallel ports\n");
1879 exit(1);
1880 }
1881 snprintf(label, sizeof(label), "parallel%d", index);
1882 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1883 if (!parallel_hds[index]) {
1884 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1885 devname, strerror(errno));
1886 return -1;
1887 }
1888 index++;
1889 return 0;
1890}
1891
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001892static int virtcon_parse(const char *devname)
1893{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001894 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001895 static int index = 0;
1896 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05301897 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001898
1899 if (strcmp(devname, "none") == 0)
1900 return 0;
1901 if (index == MAX_VIRTIO_CONSOLES) {
1902 fprintf(stderr, "qemu: too many virtio consoles\n");
1903 exit(1);
1904 }
Amit Shah392ecf52010-01-21 16:19:23 +05301905
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001906 bus_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301907 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1908
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001909 dev_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301910 qemu_opt_set(dev_opts, "driver", "virtconsole");
1911
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001912 snprintf(label, sizeof(label), "virtcon%d", index);
1913 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1914 if (!virtcon_hds[index]) {
1915 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1916 devname, strerror(errno));
1917 return -1;
1918 }
Amit Shah392ecf52010-01-21 16:19:23 +05301919 qemu_opt_set(dev_opts, "chardev", label);
1920
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001921 index++;
1922 return 0;
1923}
1924
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001925static int debugcon_parse(const char *devname)
1926{
1927 QemuOpts *opts;
1928
1929 if (!qemu_chr_open("debugcon", devname, NULL)) {
1930 exit(1);
1931 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001932 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001933 if (!opts) {
1934 fprintf(stderr, "qemu: already have a debugcon device\n");
1935 exit(1);
1936 }
1937 qemu_opt_set(opts, "driver", "isa-debugcon");
1938 qemu_opt_set(opts, "chardev", "debugcon");
1939 return 0;
1940}
1941
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001942void qemu_add_exit_notifier(Notifier *notify)
1943{
1944 notifier_list_add(&exit_notifiers, notify);
1945}
1946
1947void qemu_remove_exit_notifier(Notifier *notify)
1948{
1949 notifier_list_remove(&exit_notifiers, notify);
1950}
1951
1952static void qemu_run_exit_notifiers(void)
1953{
1954 notifier_list_notify(&exit_notifiers);
1955}
1956
Gleb Natapov4cab9462010-12-08 13:35:08 +02001957void qemu_add_machine_init_done_notifier(Notifier *notify)
1958{
1959 notifier_list_add(&machine_init_done_notifiers, notify);
1960}
1961
1962static void qemu_run_machine_init_done_notifiers(void)
1963{
1964 notifier_list_notify(&machine_init_done_notifiers);
1965}
1966
Anthony Liguori6530a972010-01-22 09:18:06 -06001967static const QEMUOption *lookup_opt(int argc, char **argv,
1968 const char **poptarg, int *poptind)
1969{
1970 const QEMUOption *popt;
1971 int optind = *poptind;
1972 char *r = argv[optind];
1973 const char *optarg;
1974
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001975 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06001976 optind++;
1977 /* Treat --foo the same as -foo. */
1978 if (r[1] == '-')
1979 r++;
1980 popt = qemu_options;
1981 for(;;) {
1982 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001983 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06001984 exit(1);
1985 }
1986 if (!strcmp(popt->name, r + 1))
1987 break;
1988 popt++;
1989 }
1990 if (popt->flags & HAS_ARG) {
1991 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001992 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06001993 exit(1);
1994 }
1995 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001996 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06001997 } else {
1998 optarg = NULL;
1999 }
2000
2001 *poptarg = optarg;
2002 *poptind = optind;
2003
2004 return popt;
2005}
2006
malc902b3d52008-12-10 19:18:40 +00002007int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002008{
aliguori59030a82009-04-05 18:43:41 +00002009 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00002010 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002011 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002012 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002013 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002014 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002015 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00002016 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00002017 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00002018 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002019 QemuOpts *hda_opts = NULL, *opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002020 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002021 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002022 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002023 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002024 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002025 const char *cpu_model;
bellard26a5f132008-05-28 12:30:31 +00002026 int tb_size;
ths93815bc2007-03-19 15:58:31 +00002027 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002028 const char *incoming = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002029 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06002030 int defconfig = 1;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002031 const char *trace_file = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002032
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002033 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002034 error_set_progname(argv[0]);
2035
Jan Kiszka68752042009-09-15 13:36:04 +02002036 init_clocks();
2037
malc902b3d52008-12-10 19:18:40 +00002038 qemu_cache_utils_init(envp);
2039
Blue Swirl72cf2d42009-09-12 07:36:22 +00002040 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002041 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002042
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002043 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002044 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002045 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00002046 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002047 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002048 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00002049 kernel_filename = NULL;
2050 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00002051 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00002052 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002053
aliguori268a3622009-04-21 22:30:27 +00002054 for (i = 0; i < MAX_NODES; i++) {
2055 node_mem[i] = 0;
2056 node_cpumask[i] = 0;
2057 }
2058
aliguori268a3622009-04-21 22:30:27 +00002059 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002060 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002061
bellard26a5f132008-05-28 12:30:31 +00002062 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00002063 autostart= 1;
2064
Anthony Liguori292444c2010-01-21 10:57:58 -06002065 /* first pass of option parsing */
2066 optind = 1;
2067 while (optind < argc) {
2068 if (argv[optind][0] != '-') {
2069 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002070 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002071 continue;
2072 } else {
2073 const QEMUOption *popt;
2074
2075 popt = lookup_opt(argc, argv, &optarg, &optind);
2076 switch (popt->index) {
2077 case QEMU_OPTION_nodefconfig:
2078 defconfig=0;
2079 break;
2080 }
2081 }
2082 }
2083
2084 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002085 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002086
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002087 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02002088 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002089 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002090 }
2091
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002092 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02002093 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002094 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002095 }
2096 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00002097 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06002098
2099 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002100 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002101 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002102 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002103 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002104 if (argv[optind][0] != '-') {
Markus Armbruster2292dda2011-01-28 11:21:41 +01002105 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00002106 } else {
2107 const QEMUOption *popt;
2108
Anthony Liguori6530a972010-01-22 09:18:06 -06002109 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002110 if (!(popt->arch_mask & arch_type)) {
2111 printf("Option %s not supported for this target\n", popt->name);
2112 exit(1);
2113 }
bellardcd6f1162004-05-13 22:02:20 +00002114 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002115 case QEMU_OPTION_M:
2116 machine = find_machine(optarg);
2117 if (!machine) {
2118 QEMUMachine *m;
2119 printf("Supported machines are:\n");
2120 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01002121 if (m->alias)
2122 printf("%-10s %s (alias of %s)\n",
2123 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00002124 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00002125 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05002126 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00002127 }
ths15f82202007-06-29 23:26:08 +00002128 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00002129 }
2130 break;
j_mayer94fc95c2007-03-05 19:44:02 +00002131 case QEMU_OPTION_cpu:
2132 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00002133 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00002134 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00002135 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00002136 } else {
2137 cpu_model = optarg;
2138 }
2139 break;
bellardcd6f1162004-05-13 22:02:20 +00002140 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00002141 initrd_filename = optarg;
2142 break;
bellardcd6f1162004-05-13 22:02:20 +00002143 case QEMU_OPTION_hda:
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002144 {
2145 char buf[256];
2146 if (cyls == 0)
2147 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2148 else
2149 snprintf(buf, sizeof(buf),
2150 "%s,cyls=%d,heads=%d,secs=%d%s",
2151 HD_OPTS , cyls, heads, secs,
2152 translation == BIOS_ATA_TRANSLATION_LBA ?
thse4bcb142007-12-02 04:51:10 +00002153 ",trans=lba" :
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002154 translation == BIOS_ATA_TRANSLATION_NONE ?
thse4bcb142007-12-02 04:51:10 +00002155 ",trans=none" : "");
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002156 drive_add(IF_DEFAULT, 0, optarg, buf);
2157 break;
2158 }
bellardcd6f1162004-05-13 22:02:20 +00002159 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002160 case QEMU_OPTION_hdc:
2161 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002162 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2163 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00002164 break;
thse4bcb142007-12-02 04:51:10 +00002165 case QEMU_OPTION_drive:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002166 drive_def(optarg);
thse4bcb142007-12-02 04:51:10 +00002167 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002168 case QEMU_OPTION_set:
2169 if (qemu_set_option(optarg) != 0)
2170 exit(1);
2171 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002172 case QEMU_OPTION_global:
2173 if (qemu_global_option(optarg) != 0)
2174 exit(1);
2175 break;
balrog3e3d5812007-04-30 02:09:25 +00002176 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002177 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00002178 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002179 case QEMU_OPTION_sd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002180 drive_add(IF_SD, 0, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00002181 break;
j_mayer86f55662007-04-24 06:52:59 +00002182 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002183 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00002184 break;
bellardcd6f1162004-05-13 22:02:20 +00002185 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002186 snapshot = 1;
2187 break;
bellardcd6f1162004-05-13 22:02:20 +00002188 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002189 {
bellard330d0412003-07-26 18:11:40 +00002190 const char *p;
2191 p = optarg;
2192 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002193 if (cyls < 1 || cyls > 16383)
2194 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002195 if (*p != ',')
2196 goto chs_fail;
2197 p++;
2198 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002199 if (heads < 1 || heads > 16)
2200 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002201 if (*p != ',')
2202 goto chs_fail;
2203 p++;
2204 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002205 if (secs < 1 || secs > 63)
2206 goto chs_fail;
2207 if (*p == ',') {
2208 p++;
2209 if (!strcmp(p, "none"))
2210 translation = BIOS_ATA_TRANSLATION_NONE;
2211 else if (!strcmp(p, "lba"))
2212 translation = BIOS_ATA_TRANSLATION_LBA;
2213 else if (!strcmp(p, "auto"))
2214 translation = BIOS_ATA_TRANSLATION_AUTO;
2215 else
2216 goto chs_fail;
2217 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002218 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002219 fprintf(stderr, "qemu: invalid physical CHS format\n");
2220 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002221 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002222 if (hda_opts != NULL) {
2223 char num[16];
2224 snprintf(num, sizeof(num), "%d", cyls);
2225 qemu_opt_set(hda_opts, "cyls", num);
2226 snprintf(num, sizeof(num), "%d", heads);
2227 qemu_opt_set(hda_opts, "heads", num);
2228 snprintf(num, sizeof(num), "%d", secs);
2229 qemu_opt_set(hda_opts, "secs", num);
2230 if (translation == BIOS_ATA_TRANSLATION_LBA)
2231 qemu_opt_set(hda_opts, "trans", "lba");
2232 if (translation == BIOS_ATA_TRANSLATION_NONE)
2233 qemu_opt_set(hda_opts, "trans", "none");
2234 }
bellard330d0412003-07-26 18:11:40 +00002235 }
2236 break;
aliguori268a3622009-04-21 22:30:27 +00002237 case QEMU_OPTION_numa:
2238 if (nb_numa_nodes >= MAX_NODES) {
2239 fprintf(stderr, "qemu: too many NUMA nodes\n");
2240 exit(1);
2241 }
2242 numa_add(optarg);
2243 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01002244 case QEMU_OPTION_display:
2245 display_type = select_display(optarg);
2246 break;
bellardcd6f1162004-05-13 22:02:20 +00002247 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002248 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002249 break;
balrog4d3b6f62008-02-10 16:33:14 +00002250#ifdef CONFIG_CURSES
2251 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002252 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00002253 break;
2254#endif
balroga171fe32007-04-30 01:48:07 +00002255 case QEMU_OPTION_portrait:
2256 graphic_rotate = 1;
2257 break;
bellardcd6f1162004-05-13 22:02:20 +00002258 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002259 kernel_filename = optarg;
2260 break;
bellardcd6f1162004-05-13 22:02:20 +00002261 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002262 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002263 break;
bellardcd6f1162004-05-13 22:02:20 +00002264 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002265 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00002266 break;
bellardcd6f1162004-05-13 22:02:20 +00002267 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002268 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002269 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002270 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002271 };
2272 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002273 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002274 int legacy = 0;
2275
2276 if (!strchr(optarg, '=')) {
2277 legacy = 1;
2278 pstrcpy(buf, sizeof(buf), optarg);
2279 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2280 fprintf(stderr,
2281 "qemu: unknown boot parameter '%s' in '%s'\n",
2282 buf, optarg);
2283 exit(1);
2284 }
2285
2286 if (legacy ||
2287 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002288 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002289 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002290 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002291 if (!legacy) {
2292 if (get_param_value(buf, sizeof(buf),
2293 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002294 validate_bootdevices(buf);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002295 standard_boot_devices = qemu_strdup(boot_devices);
2296 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2297 qemu_register_reset(restore_boot_devices,
2298 standard_boot_devices);
2299 }
Jan Kiszka95387492009-07-02 00:19:02 +02002300 if (get_param_value(buf, sizeof(buf),
2301 "menu", optarg)) {
2302 if (!strcmp(buf, "on")) {
2303 boot_menu = 1;
2304 } else if (!strcmp(buf, "off")) {
2305 boot_menu = 0;
2306 } else {
2307 fprintf(stderr,
2308 "qemu: invalid option value '%s'\n",
2309 buf);
2310 exit(1);
2311 }
2312 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002313 }
bellard36b486b2003-11-11 13:36:08 +00002314 }
2315 break;
bellardcd6f1162004-05-13 22:02:20 +00002316 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002317 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002318 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2319 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00002320 break;
bellard52ca8d62006-06-14 16:03:05 +00002321 case QEMU_OPTION_no_fd_bootchk:
2322 fd_bootchk = 0;
2323 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002324 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002325 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002326 exit(1);
2327 }
2328 break;
bellard7c9d8e02005-11-15 22:16:05 +00002329 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002330 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002331 exit(1);
2332 }
bellard702c6512004-04-02 21:21:32 +00002333 break;
bellardc7f74642004-08-24 21:57:12 +00002334#ifdef CONFIG_SLIRP
2335 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002336 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002337 break;
ths47d5d012007-02-20 00:05:08 +00002338 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002339 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002340 break;
bellard9bf05442004-08-25 22:12:49 +00002341 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002342 if (net_slirp_redir(optarg) < 0)
2343 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002344 break;
bellardc7f74642004-08-24 21:57:12 +00002345#endif
balrogdc72ac12008-11-09 00:04:26 +00002346 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002347 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002348 break;
bellard1d14ffa2005-10-30 18:58:22 +00002349 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002350 if (!(audio_available())) {
2351 printf("Option %s not supported for this target\n", popt->name);
2352 exit(1);
2353 }
bellard1d14ffa2005-10-30 18:58:22 +00002354 AUD_help ();
2355 exit (0);
2356 break;
2357 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002358 if (!(audio_available())) {
2359 printf("Option %s not supported for this target\n", popt->name);
2360 exit(1);
2361 }
bellard1d14ffa2005-10-30 18:58:22 +00002362 select_soundhw (optarg);
2363 break;
bellardcd6f1162004-05-13 22:02:20 +00002364 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002365 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002366 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002367 case QEMU_OPTION_version:
2368 version();
2369 exit(0);
2370 break;
aurel3200f82b82008-04-27 21:12:55 +00002371 case QEMU_OPTION_m: {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002372 int64_t value;
aurel3200f82b82008-04-27 21:12:55 +00002373
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002374 value = strtosz(optarg, NULL);
2375 if (value < 0) {
aurel3200f82b82008-04-27 21:12:55 +00002376 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002377 exit(1);
2378 }
aurel3200f82b82008-04-27 21:12:55 +00002379
2380 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002381 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00002382 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2383 exit(1);
2384 }
Anthony Liguoric227f092009-10-01 16:12:16 -05002385 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002386 fprintf(stderr, "qemu: ram size too large\n");
2387 exit(1);
2388 }
2389 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002390 break;
aurel3200f82b82008-04-27 21:12:55 +00002391 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002392 case QEMU_OPTION_mempath:
2393 mem_path = optarg;
2394 break;
2395#ifdef MAP_POPULATE
2396 case QEMU_OPTION_mem_prealloc:
2397 mem_prealloc = 1;
2398 break;
2399#endif
bellardcd6f1162004-05-13 22:02:20 +00002400 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002401 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00002402 break;
bellardcd6f1162004-05-13 22:02:20 +00002403 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002404 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002405 break;
aliguori59030a82009-04-05 18:43:41 +00002406 case QEMU_OPTION_gdb:
2407 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002408 break;
bellardcd6f1162004-05-13 22:02:20 +00002409 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002410 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002411 break;
j_mayer1192dad2007-10-05 13:08:35 +00002412 case QEMU_OPTION_bios:
2413 bios_name = optarg;
2414 break;
aurel321b530a62009-04-05 20:08:59 +00002415 case QEMU_OPTION_singlestep:
2416 singlestep = 1;
2417 break;
bellardcd6f1162004-05-13 22:02:20 +00002418 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002419 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002420 break;
bellard3d11d0e2004-12-12 16:56:30 +00002421 case QEMU_OPTION_k:
2422 keyboard_layout = optarg;
2423 break;
bellardee22c2f2004-06-03 12:49:50 +00002424 case QEMU_OPTION_localtime:
2425 rtc_utc = 0;
2426 break;
malc3893c122008-09-28 00:42:05 +00002427 case QEMU_OPTION_vga:
2428 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002429 break;
bellarde9b137c2004-06-21 16:46:10 +00002430 case QEMU_OPTION_g:
2431 {
2432 const char *p;
2433 int w, h, depth;
2434 p = optarg;
2435 w = strtol(p, (char **)&p, 10);
2436 if (w <= 0) {
2437 graphic_error:
2438 fprintf(stderr, "qemu: invalid resolution or depth\n");
2439 exit(1);
2440 }
2441 if (*p != 'x')
2442 goto graphic_error;
2443 p++;
2444 h = strtol(p, (char **)&p, 10);
2445 if (h <= 0)
2446 goto graphic_error;
2447 if (*p == 'x') {
2448 p++;
2449 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002450 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002451 depth != 24 && depth != 32)
2452 goto graphic_error;
2453 } else if (*p == '\0') {
2454 depth = graphic_depth;
2455 } else {
2456 goto graphic_error;
2457 }
ths3b46e622007-09-17 08:09:54 +00002458
bellarde9b137c2004-06-21 16:46:10 +00002459 graphic_width = w;
2460 graphic_height = h;
2461 graphic_depth = depth;
2462 }
2463 break;
ths20d8a3e2007-02-18 17:04:49 +00002464 case QEMU_OPTION_echr:
2465 {
2466 char *r;
2467 term_escape_char = strtol(optarg, &r, 0);
2468 if (r == optarg)
2469 printf("Bad argument to echr\n");
2470 break;
2471 }
bellard82c643f2004-07-14 17:28:13 +00002472 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002473 monitor_parse(optarg, "readline");
2474 default_monitor = 0;
2475 break;
2476 case QEMU_OPTION_qmp:
2477 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002478 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002479 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002480 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002481 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002482 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002483 exit(1);
2484 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002485 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002486 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002487 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002488 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002489 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002490 exit(1);
2491 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002492 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302493 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002494 olist = qemu_find_opts("fsdev");
2495 if (!olist) {
2496 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2497 exit(1);
2498 }
2499 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302500 if (!opts) {
2501 fprintf(stderr, "parse error: %s\n", optarg);
2502 exit(1);
2503 }
2504 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302505 case QEMU_OPTION_virtfs: {
2506 char *arg_fsdev = NULL;
2507 char *arg_9p = NULL;
2508 int len = 0;
2509
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002510 olist = qemu_find_opts("virtfs");
2511 if (!olist) {
2512 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2513 exit(1);
2514 }
2515 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302516 if (!opts) {
2517 fprintf(stderr, "parse error: %s\n", optarg);
2518 exit(1);
2519 }
2520
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002521 if (qemu_opt_get(opts, "fstype") == NULL ||
2522 qemu_opt_get(opts, "mount_tag") == NULL ||
2523 qemu_opt_get(opts, "path") == NULL ||
2524 qemu_opt_get(opts, "security_model") == NULL) {
2525 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +05302526 "security_model=[mapped|passthrough|none],"
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002527 "mnt_tag=tag.\n");
2528 exit(1);
2529 }
2530
2531 len = strlen(",id=,path=,security_model=");
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302532 len += strlen(qemu_opt_get(opts, "fstype"));
2533 len += strlen(qemu_opt_get(opts, "mount_tag"));
2534 len += strlen(qemu_opt_get(opts, "path"));
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002535 len += strlen(qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302536 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2537
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302538 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002539 "%s,id=%s,path=%s,security_model=%s",
2540 qemu_opt_get(opts, "fstype"),
2541 qemu_opt_get(opts, "mount_tag"),
2542 qemu_opt_get(opts, "path"),
2543 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302544
2545 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2546 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2547 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2548
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302549 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002550 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2551 qemu_opt_get(opts, "mount_tag"),
2552 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302553
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002554 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302555 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2556 exit(1);
2557 }
2558
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002559 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302560 fprintf(stderr, "parse error [device]: %s\n", optarg);
2561 exit(1);
2562 }
2563
2564 qemu_free(arg_fsdev);
2565 qemu_free(arg_9p);
2566 break;
2567 }
bellard82c643f2004-07-14 17:28:13 +00002568 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002569 add_device_config(DEV_SERIAL, optarg);
2570 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002571 if (strncmp(optarg, "mon:", 4) == 0) {
2572 default_monitor = 0;
2573 }
bellard82c643f2004-07-14 17:28:13 +00002574 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002575 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002576 if (watchdog) {
2577 fprintf(stderr,
2578 "qemu: only one watchdog option may be given\n");
2579 return 1;
2580 }
2581 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002582 break;
2583 case QEMU_OPTION_watchdog_action:
2584 if (select_watchdog_action(optarg) == -1) {
2585 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2586 exit(1);
2587 }
2588 break;
aliguori51ecf132009-01-15 20:06:40 +00002589 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002590 add_device_config(DEV_VIRTCON, optarg);
2591 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002592 if (strncmp(optarg, "mon:", 4) == 0) {
2593 default_monitor = 0;
2594 }
aliguori51ecf132009-01-15 20:06:40 +00002595 break;
bellard6508fe52005-01-15 12:02:56 +00002596 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002597 add_device_config(DEV_PARALLEL, optarg);
2598 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002599 if (strncmp(optarg, "mon:", 4) == 0) {
2600 default_monitor = 0;
2601 }
bellard6508fe52005-01-15 12:02:56 +00002602 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002603 case QEMU_OPTION_debugcon:
2604 add_device_config(DEV_DEBUGCON, optarg);
2605 break;
bellardd63d3072004-10-03 13:29:03 +00002606 case QEMU_OPTION_loadvm:
2607 loadvm = optarg;
2608 break;
2609 case QEMU_OPTION_full_screen:
2610 full_screen = 1;
2611 break;
ths667acca2006-12-11 02:08:05 +00002612#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002613 case QEMU_OPTION_no_frame:
2614 no_frame = 1;
2615 break;
ths3780e192007-06-21 21:08:02 +00002616 case QEMU_OPTION_alt_grab:
2617 alt_grab = 1;
2618 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002619 case QEMU_OPTION_ctrl_grab:
2620 ctrl_grab = 1;
2621 break;
ths667acca2006-12-11 02:08:05 +00002622 case QEMU_OPTION_no_quit:
2623 no_quit = 1;
2624 break;
aliguori7d957bd2009-01-15 22:14:11 +00002625 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002626 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002627 break;
ths667acca2006-12-11 02:08:05 +00002628#endif
bellardf7cce892004-12-08 22:21:25 +00002629 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002630 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002631 break;
bellarda09db212005-04-30 16:10:35 +00002632 case QEMU_OPTION_win2k_hack:
2633 win2k_install_hack = 1;
2634 break;
aliguori73822ec2009-01-15 20:11:34 +00002635 case QEMU_OPTION_rtc_td_hack:
2636 rtc_td_hack = 1;
2637 break;
aliguori8a92ea22009-02-27 20:12:36 +00002638 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002639 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002640 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002641 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002642 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002643 break;
aliguori7ba1e612008-11-05 16:04:33 +00002644 case QEMU_OPTION_enable_kvm:
2645 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00002646 break;
bellardbb36d472005-11-05 14:22:28 +00002647 case QEMU_OPTION_usb:
2648 usb_enabled = 1;
2649 break;
bellarda594cfb2005-11-06 16:13:29 +00002650 case QEMU_OPTION_usbdevice:
2651 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002652 add_device_config(DEV_USB, optarg);
2653 break;
2654 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002655 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002656 exit(1);
2657 }
bellarda594cfb2005-11-06 16:13:29 +00002658 break;
bellard6a00d602005-11-21 23:25:50 +00002659 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002660 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002661 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002662 fprintf(stderr, "Invalid number of CPUs\n");
2663 exit(1);
2664 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002665 if (max_cpus < smp_cpus) {
2666 fprintf(stderr, "maxcpus must be equal to or greater than "
2667 "smp\n");
2668 exit(1);
2669 }
2670 if (max_cpus > 255) {
2671 fprintf(stderr, "Unsupported number of maxcpus\n");
2672 exit(1);
2673 }
bellard6a00d602005-11-21 23:25:50 +00002674 break;
bellard24236862006-04-30 21:28:36 +00002675 case QEMU_OPTION_vnc:
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002676 display_remote++;
ths73fc9742006-12-22 02:09:07 +00002677 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00002678 break;
bellard6515b202006-05-03 22:02:44 +00002679 case QEMU_OPTION_no_acpi:
2680 acpi_enabled = 0;
2681 break;
aliguori16b29ae2008-12-17 23:28:44 +00002682 case QEMU_OPTION_no_hpet:
2683 no_hpet = 1;
2684 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002685 case QEMU_OPTION_balloon:
2686 if (balloon_parse(optarg) < 0) {
2687 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2688 exit(1);
2689 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002690 break;
bellardd1beab82006-10-02 19:44:22 +00002691 case QEMU_OPTION_no_reboot:
2692 no_reboot = 1;
2693 break;
aurel32b2f76162008-04-11 21:35:52 +00002694 case QEMU_OPTION_no_shutdown:
2695 no_shutdown = 1;
2696 break;
balrog9467cd42007-05-01 01:34:14 +00002697 case QEMU_OPTION_show_cursor:
2698 cursor_hide = 0;
2699 break;
blueswir18fcb1b92008-09-18 18:29:08 +00002700 case QEMU_OPTION_uuid:
2701 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2702 fprintf(stderr, "Fail to parse UUID string."
2703 " Wrong format.\n");
2704 exit(1);
2705 }
2706 break;
ths9ae02552007-01-05 17:39:04 +00002707 case QEMU_OPTION_option_rom:
2708 if (nb_option_roms >= MAX_OPTION_ROMS) {
2709 fprintf(stderr, "Too many option ROMs\n");
2710 exit(1);
2711 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02002712 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2713 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2714 option_rom[nb_option_roms].bootindex =
2715 qemu_opt_get_number(opts, "bootindex", -1);
2716 if (!option_rom[nb_option_roms].name) {
2717 fprintf(stderr, "Option ROM file is not specified\n");
2718 exit(1);
2719 }
ths9ae02552007-01-05 17:39:04 +00002720 nb_option_roms++;
2721 break;
pbrook8e716212007-01-20 17:12:09 +00002722 case QEMU_OPTION_semihosting:
2723 semihosting_enabled = 1;
2724 break;
thsc35734b2007-03-19 15:17:08 +00002725 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02002726 qemu_name = qemu_strdup(optarg);
2727 {
2728 char *p = strchr(qemu_name, ',');
2729 if (p != NULL) {
2730 *p++ = 0;
2731 if (strncmp(p, "process=", 8)) {
Aurelien Jarno5697f6a2010-12-27 18:29:20 +01002732 fprintf(stderr, "Unknown subargument %s to -name\n", p);
Andi Kleen18894652009-07-02 09:34:17 +02002733 exit(1);
2734 }
2735 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02002736 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02002737 }
2738 }
thsc35734b2007-03-19 15:17:08 +00002739 break;
blueswir166508602007-05-01 14:16:52 +00002740 case QEMU_OPTION_prom_env:
2741 if (nb_prom_envs >= MAX_PROM_ENVS) {
2742 fprintf(stderr, "Too many prom variables\n");
2743 exit(1);
2744 }
2745 prom_envs[nb_prom_envs] = optarg;
2746 nb_prom_envs++;
2747 break;
balrog2b8f2d42007-07-27 22:08:46 +00002748 case QEMU_OPTION_old_param:
2749 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00002750 break;
thsf3dcfad2007-08-24 01:26:02 +00002751 case QEMU_OPTION_clock:
2752 configure_alarms(optarg);
2753 break;
bellard7e0af5d02007-11-07 16:24:33 +00002754 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002755 configure_rtc_date_offset(optarg, 1);
2756 break;
2757 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002758 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002759 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002760 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00002761 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002762 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00002763 break;
bellard26a5f132008-05-28 12:30:31 +00002764 case QEMU_OPTION_tb_size:
2765 tb_size = strtol(optarg, NULL, 0);
2766 if (tb_size < 0)
2767 tb_size = 0;
2768 break;
pbrook2e70f6e2008-06-29 01:03:05 +00002769 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002770 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00002771 break;
aliguori5bb79102008-10-13 03:12:02 +00002772 case QEMU_OPTION_incoming:
2773 incoming = optarg;
Amit Shah8e848652010-07-27 15:49:19 +05302774 incoming_expected = true;
aliguori5bb79102008-10-13 03:12:02 +00002775 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002776 case QEMU_OPTION_nodefaults:
2777 default_serial = 0;
2778 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002779 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002780 default_monitor = 0;
2781 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01002782 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002783 default_floppy = 0;
2784 default_cdrom = 0;
2785 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002786 break;
aliguorie37630c2009-04-22 15:19:10 +00002787 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00002788 if (!(xen_available())) {
2789 printf("Option %s not supported for this target\n", popt->name);
2790 exit(1);
2791 }
aliguorie37630c2009-04-22 15:19:10 +00002792 xen_domid = atoi(optarg);
2793 break;
2794 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00002795 if (!(xen_available())) {
2796 printf("Option %s not supported for this target\n", popt->name);
2797 exit(1);
2798 }
aliguorie37630c2009-04-22 15:19:10 +00002799 xen_mode = XEN_CREATE;
2800 break;
2801 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00002802 if (!(xen_available())) {
2803 printf("Option %s not supported for this target\n", popt->name);
2804 exit(1);
2805 }
aliguorie37630c2009-04-22 15:19:10 +00002806 xen_mode = XEN_ATTACH;
2807 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002808#ifdef CONFIG_SIMPLE_TRACE
2809 case QEMU_OPTION_trace:
2810 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2811 if (opts) {
2812 trace_file = qemu_opt_get(opts, "file");
2813 }
2814 break;
2815#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002816 case QEMU_OPTION_readconfig:
2817 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002818 int ret = qemu_read_config_file(optarg);
2819 if (ret < 0) {
2820 fprintf(stderr, "read config %s: %s\n", optarg,
2821 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002822 exit(1);
2823 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002824 break;
2825 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03002826 case QEMU_OPTION_spice:
2827 olist = qemu_find_opts("spice");
2828 if (!olist) {
2829 fprintf(stderr, "spice is not supported by this qemu build.\n");
2830 exit(1);
2831 }
2832 opts = qemu_opts_parse(olist, optarg, 0);
2833 if (!opts) {
2834 fprintf(stderr, "parse error: %s\n", optarg);
2835 exit(1);
2836 }
2837 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002838 case QEMU_OPTION_writeconfig:
2839 {
2840 FILE *fp;
2841 if (strcmp(optarg, "-") == 0) {
2842 fp = stdout;
2843 } else {
2844 fp = fopen(optarg, "w");
2845 if (fp == NULL) {
2846 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2847 exit(1);
2848 }
2849 }
2850 qemu_config_write(fp);
2851 fclose(fp);
2852 break;
2853 }
Jes Sorensen59a52642010-06-10 11:42:25 +02002854 default:
2855 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00002856 }
bellard0824d6f2003-06-24 13:42:40 +00002857 }
2858 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002859 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00002860
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00002861 if (!st_init(trace_file)) {
2862 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2863 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002864
Paul Brook5cea8592009-05-30 00:52:44 +01002865 /* If no data_dir is specified then try to find it relative to the
2866 executable path. */
2867 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02002868 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01002869 }
2870 /* If all else fails use the install patch specified when building. */
2871 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02002872 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01002873 }
2874
Jes Sorensen6be68d72009-07-23 17:03:42 +02002875 /*
2876 * Default to max_cpus = smp_cpus, in case the user doesn't
2877 * specify a max_cpus value.
2878 */
2879 if (!max_cpus)
2880 max_cpus = smp_cpus;
2881
balrog3d878ca2008-10-28 10:59:59 +00002882 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00002883 if (smp_cpus > machine->max_cpus) {
2884 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2885 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2886 machine->max_cpus);
2887 exit(1);
2888 }
2889
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002890 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2891 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002892
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002893 if (machine->no_serial) {
2894 default_serial = 0;
2895 }
2896 if (machine->no_parallel) {
2897 default_parallel = 0;
2898 }
2899 if (!machine->use_virtcon) {
2900 default_virtcon = 0;
2901 }
2902 if (machine->no_vga) {
2903 default_vga = 0;
2904 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002905 if (machine->no_floppy) {
2906 default_floppy = 0;
2907 }
2908 if (machine->no_cdrom) {
2909 default_cdrom = 0;
2910 }
2911 if (machine->no_sdcard) {
2912 default_sdcard = 0;
2913 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002914
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002915 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002916 if (default_parallel)
2917 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002918 if (default_serial && default_monitor) {
2919 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002920 } else if (default_virtcon && default_monitor) {
2921 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002922 } else {
2923 if (default_serial)
2924 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002925 if (default_virtcon)
2926 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002927 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002928 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002929 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002930 } else {
2931 if (default_serial)
2932 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002933 if (default_parallel)
2934 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01002935 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002936 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01002937 if (default_virtcon)
2938 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00002939 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01002940 if (default_vga)
2941 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00002942
TeLeMana5829fd2010-04-15 12:37:55 +08002943 socket_init();
2944
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002945 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002946 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002947#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002948 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302949 exit(1);
2950 }
2951#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002952
Jes Sorenseneb505be2010-06-10 11:42:28 +02002953 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00002954
thsaa26bb22007-03-25 21:33:06 +00002955 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02002956 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00002957 exit(1);
2958 }
2959
Paolo Bonzini98c85732010-04-19 18:59:30 +00002960 if (kvm_allowed) {
Jan Kiszkacad1e282011-01-21 21:48:16 +01002961 int ret = kvm_init();
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002962 if (ret < 0) {
Paolo Bonzini98c85732010-04-19 18:59:30 +00002963 if (!kvm_available()) {
2964 printf("KVM not supported for this target\n");
2965 } else {
2966 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2967 }
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002968 exit(1);
2969 }
2970 }
2971
aliguori3fcf7b62009-04-24 18:03:25 +00002972 if (qemu_init_main_loop()) {
2973 fprintf(stderr, "qemu_init_main_loop failed\n");
2974 exit(1);
2975 }
bellarda20dd502003-09-30 21:07:02 +00002976 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00002977
thsf8d39c02008-07-03 10:01:15 +00002978 if (!linux_boot && *kernel_cmdline != '\0') {
2979 fprintf(stderr, "-append only allowed with -kernel option\n");
2980 exit(1);
2981 }
2982
2983 if (!linux_boot && initrd_filename != NULL) {
2984 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2985 exit(1);
2986 }
2987
Jes Sorensen9156d762010-06-10 11:42:30 +02002988 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00002989
aliguori7183b4b2008-11-05 20:40:18 +00002990 if (init_timer_alarm() < 0) {
2991 fprintf(stderr, "could not initialize alarm timer\n");
2992 exit(1);
2993 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002994 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00002995
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01002996 if (net_init_clients() < 0) {
2997 exit(1);
bellard702c6512004-04-02 21:21:32 +00002998 }
bellardf1510b22003-06-25 00:07:40 +00002999
balrogdc72ac12008-11-09 00:04:26 +00003000 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003001 if (foreach_device_config(DEV_BT, bt_parse))
3002 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003003
bellard0824d6f2003-06-24 13:42:40 +00003004 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00003005 if (ram_size == 0)
3006 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00003007
bellard26a5f132008-05-28 12:30:31 +00003008 /* init the dynamic translator */
3009 cpu_exec_init_all(tb_size * 1024 * 1024);
3010
Markus Armbrustereb852012009-10-27 18:41:44 +01003011 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003012
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003013 blk_mig_init();
3014
ths96d30e42007-01-07 20:42:14 +00003015 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003016 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003017 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3018 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003019 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00003020
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01003021 default_drive(default_cdrom, snapshot, machine->use_scsi,
3022 IF_DEFAULT, 2, CDROM_OPTS);
3023 default_drive(default_floppy, snapshot, machine->use_scsi,
3024 IF_FLOPPY, 0, FD_OPTS);
3025 default_drive(default_sdcard, snapshot, machine->use_scsi,
3026 IF_SD, 0, SD_OPTS);
3027
Alex Williamson97ab12d2010-06-25 11:09:50 -06003028 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003029 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003030
aliguori268a3622009-04-21 22:30:27 +00003031 if (nb_numa_nodes > 0) {
3032 int i;
3033
3034 if (nb_numa_nodes > smp_cpus) {
3035 nb_numa_nodes = smp_cpus;
3036 }
3037
3038 /* If no memory size if given for any node, assume the default case
3039 * and distribute the available memory equally across all nodes
3040 */
3041 for (i = 0; i < nb_numa_nodes; i++) {
3042 if (node_mem[i] != 0)
3043 break;
3044 }
3045 if (i == nb_numa_nodes) {
3046 uint64_t usedmem = 0;
3047
3048 /* On Linux, the each node's border has to be 8MB aligned,
3049 * the final node gets the rest.
3050 */
3051 for (i = 0; i < nb_numa_nodes - 1; i++) {
3052 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3053 usedmem += node_mem[i];
3054 }
3055 node_mem[i] = ram_size - usedmem;
3056 }
3057
3058 for (i = 0; i < nb_numa_nodes; i++) {
3059 if (node_cpumask[i] != 0)
3060 break;
3061 }
3062 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3063 * must cope with this anyway, because there are BIOSes out there in
3064 * real machines which also use this scheme.
3065 */
3066 if (i == nb_numa_nodes) {
3067 for (i = 0; i < smp_cpus; i++) {
3068 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3069 }
3070 }
3071 }
3072
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003073 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02003074 exit(1);
3075 }
3076
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003077 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3078 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003079 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3080 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003081 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3082 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003083 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3084 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003085
Paul Brookaae94602009-05-14 22:35:06 +01003086 module_call_init(MODULE_INIT_DEVICE);
3087
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003088 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01003089 exit(0);
3090
Markus Armbruster09aaa162009-08-21 10:31:34 +02003091 if (watchdog) {
3092 i = select_watchdog(watchdog);
3093 if (i > 0)
3094 exit (i == 1 ? 1 : 0);
3095 }
3096
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003097 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003098 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003099 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003100 qemu_add_globals();
3101
Paul Brookfbe1b592009-05-13 17:56:25 +01003102 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00003103 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3104
Jan Kiszkaea375f92010-03-01 19:10:30 +01003105 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003106
Juan Quintela67b3b712009-08-28 19:25:15 +02003107 /* must be after terminal init, SDL library changes signal handlers */
Jes Sorensen8d963e62010-06-10 11:42:22 +02003108 os_setup_signal_handling();
Juan Quintela67b3b712009-08-28 19:25:15 +02003109
Blue Swirl87d0a282010-03-27 18:24:45 +00003110 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003111
aliguori6f338c32009-02-11 15:21:54 +00003112 current_machine = machine;
3113
aliguori3023f332009-01-16 19:04:14 +00003114 /* init USB devices */
3115 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01003116 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3117 exit(1);
aliguori3023f332009-01-16 19:04:14 +00003118 }
3119
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003120 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003121 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003122 exit(1);
3123
Markus Armbruster668680f2010-02-11 14:44:58 +01003124 net_check_clients();
3125
aliguori3023f332009-01-16 19:04:14 +00003126 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003127 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003128
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003129 if (using_spice)
3130 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003131 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003132#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003133 display_type = DT_SDL;
3134#else
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003135 vnc_display = "localhost:0,to=99";
3136 show_vnc_port = 1;
3137#endif
3138 }
3139
3140
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003141 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003142 switch (display_type) {
3143 case DT_NOGRAPHIC:
3144 break;
3145#if defined(CONFIG_CURSES)
3146 case DT_CURSES:
3147 curses_display_init(ds, full_screen);
3148 break;
3149#endif
bellard5b0753e2005-03-01 21:37:28 +00003150#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003151 case DT_SDL:
3152 sdl_display_init(ds, full_screen, no_frame);
3153 break;
bellard5b0753e2005-03-01 21:37:28 +00003154#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003155 case DT_SDL:
3156 cocoa_display_init(ds, full_screen);
3157 break;
bellard313aa562003-08-10 21:52:11 +00003158#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003159 default:
3160 break;
3161 }
3162
3163 /* init remote displays */
3164 if (vnc_display) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003165 vnc_display_init(ds);
3166 if (vnc_display_open(ds, vnc_display) < 0)
3167 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003168
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003169 if (show_vnc_port) {
3170 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003171 }
bellard313aa562003-08-10 21:52:11 +00003172 }
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003173#ifdef CONFIG_SPICE
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02003174 if (using_spice && !qxl_enabled) {
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003175 qemu_spice_display_init(ds);
3176 }
3177#endif
aliguori5b08fc12008-08-21 20:08:03 +00003178
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003179 /* display setup */
3180 dpy_resize(ds);
aliguori3023f332009-01-16 19:04:14 +00003181 dcl = ds->listeners;
3182 while (dcl != NULL) {
3183 if (dcl->dpy_refresh != NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003184 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3185 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09003186 break;
ths20d8a3e2007-02-18 17:04:49 +00003187 }
aliguori3023f332009-01-16 19:04:14 +00003188 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00003189 }
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003190 if (ds->gui_timer == NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003191 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3192 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00003193 }
Paolo Bonzinib473df62010-02-11 00:29:55 +01003194 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003195
aliguori59030a82009-04-05 18:43:41 +00003196 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3197 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3198 gdbstub_dev);
3199 exit(1);
balrog45669e02007-07-02 13:20:17 +00003200 }
balrog45669e02007-07-02 13:20:17 +00003201
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003202 qdev_machine_creation_done();
3203
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003204 if (rom_load_all() != 0) {
3205 fprintf(stderr, "rom loading failed\n");
3206 exit(1);
3207 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003208
Isaku Yamahata80376c32010-12-20 14:33:35 +09003209 /* TODO: once all bus devices are qdevified, this should be done
3210 * when bus is created by qdev.c */
3211 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02003212 qemu_run_machine_init_done_notifiers();
3213
Juan Quintela504c2942009-11-12 00:39:13 +01003214 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02003215 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003216 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003217 autostart = 0;
3218 }
3219 }
bellardd63d3072004-10-03 13:29:03 +00003220
Glauber Costa2bb8c102009-07-24 16:20:23 -04003221 if (incoming) {
Juan Quintela8ca5e802010-06-09 14:10:54 +02003222 int ret = qemu_start_incoming_migration(incoming);
3223 if (ret < 0) {
3224 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3225 incoming, ret);
3226 exit(ret);
3227 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03003228 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003229 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003230 }
thsffd843b2006-12-21 19:46:43 +00003231
Jes Sorenseneb505be2010-06-10 11:42:28 +02003232 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003233
bellard8a7ddc32004-03-31 19:00:16 +00003234 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003235 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003236 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00003237
bellard0824d6f2003-06-24 13:42:40 +00003238 return 0;
3239}