blob: 844d6a50b6a183c85a34b90d20c0d4032865742c [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#ifdef CONFIG_SIMPLE_TRACE
51#include "trace.h"
52#endif
53
Juan Quintela71e72a12009-07-27 16:12:56 +020054#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000055#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010056#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000057#include <libutil.h>
Juergen Lock92c0e652010-03-25 22:07:12 +010058#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000059#else
60#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000061#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010062#else
blueswir1223f0d72008-09-30 18:12:18 +000063#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000064#include <pty.h>
65#include <malloc.h>
thsbd494f42007-09-16 20:03:23 +000066
bellarde57a8c02005-11-10 23:58:52 +000067#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000068#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000069#endif
70#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000071#include <sys/stat.h>
72#include <sys/ethernet.h>
73#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000074#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000075#include <netinet/in_systm.h>
76#include <netinet/ip.h>
77#include <netinet/ip_icmp.h> // must come after ip.h
78#include <netinet/udp.h>
79#include <netinet/tcp.h>
80#include <net/if.h>
81#include <syslog.h>
82#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000083#endif
bellard7d3505c2004-05-12 19:32:15 +000084#endif
bellardec530c82006-04-25 22:36:06 +000085#endif
bellard67b915a2004-03-31 23:37:16 +000086
blueswir19892fbf2008-08-24 10:34:20 +000087#if defined(__OpenBSD__)
88#include <util.h>
89#endif
90
ths8a16d272008-07-19 09:56:24 +000091#if defined(CONFIG_VDE)
92#include <libvdeplug.h>
93#endif
94
bellard67b915a2004-03-31 23:37:16 +000095#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000096#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000097#endif
98
bellard73332e52004-04-04 20:22:28 +000099#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200100#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200101#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000102int qemu_main(int argc, char **argv, char **envp);
103int main(int argc, char **argv)
104{
Stefan Weil59a36a22009-06-18 20:11:03 +0200105 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000106}
107#undef main
108#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000109#endif
bellard73332e52004-04-04 20:22:28 +0000110#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000111
bellard5b0753e2005-03-01 21:37:28 +0000112#ifdef CONFIG_COCOA
113#undef main
114#define main qemu_main
115#endif /* CONFIG_COCOA */
116
blueswir1511d2b12009-03-07 15:32:56 +0000117#include "hw/hw.h"
118#include "hw/boards.h"
119#include "hw/usb.h"
120#include "hw/pcmcia.h"
121#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000122#include "hw/isa.h"
123#include "hw/baum.h"
124#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100125#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000126#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000127#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200128#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200129#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000130#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000131#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000132#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000133#include "monitor.h"
134#include "console.h"
135#include "sysemu.h"
136#include "gdbstub.h"
137#include "qemu-timer.h"
138#include "qemu-char.h"
139#include "cache-utils.h"
140#include "block.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200141#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200142#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000143#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000144#include "audio/audio.h"
145#include "migration.h"
146#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200147#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200148#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200149#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200150#include "qemu-options.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700151#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530152#include "fsdev/qemu-fsdev.h"
153#endif
blueswir1511d2b12009-03-07 15:32:56 +0000154
bellard0824d6f2003-06-24 13:42:40 +0000155#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000156
blueswir1511d2b12009-03-07 15:32:56 +0000157#include "qemu_socket.h"
158
Jan Kiszkad918f232009-06-24 14:42:30 +0200159#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000161#include "trace.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000162#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000163#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000164#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000165
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300166#include "ui/qemu-spice.h"
167
blueswir19dc63a12008-10-04 07:25:46 +0000168//#define DEBUG_NET
169//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000170
bellard1bfe8562004-07-08 21:17:50 +0000171#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000172
Amit Shah98b19252010-01-20 00:36:52 +0530173#define MAX_VIRTIO_CONSOLES 1
174
Paul Brook5cea8592009-05-30 00:52:44 +0100175static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000176const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000177enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500178DisplayType display_type = DT_DEFAULT;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +0200179int display_remote = 0;
bellard3d11d0e2004-12-12 16:56:30 +0000180const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500181ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300182const char *mem_path = NULL;
183#ifdef MAP_POPULATE
184int mem_prealloc = 0; /* force preallocation of physical target memory */
185#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000186int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000187NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000188int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200189int autostart;
Amit Shah8e848652010-07-27 15:49:19 +0530190int incoming_expected; /* Started with -incoming and waiting for incoming */
balrogf6503052008-02-17 11:42:19 +0000191static int rtc_utc = 1;
192static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200193QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100194int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000195static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000196#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000197static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000198#endif
ths667acca2006-12-11 02:08:05 +0000199int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000200CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000201CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000202CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000203int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000204int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000205int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000206int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000207int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200208int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200209int smp_cores = 1;
210int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000211const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000212int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000213int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000214int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000215int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000216int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000217int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000218int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200219uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200220const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200221QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000222int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000223int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000224int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000225const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000226int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500227int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000228unsigned int nb_prom_envs = 0;
229const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200230int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000231
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200232typedef struct FWBootEntry FWBootEntry;
233
234struct FWBootEntry {
235 QTAILQ_ENTRY(FWBootEntry) link;
236 int32_t bootindex;
237 DeviceState *dev;
238 char *suffix;
239};
240
241QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
242
aliguori268a3622009-04-21 22:30:27 +0000243int nb_numa_nodes;
244uint64_t node_mem[MAX_NODES];
245uint64_t node_cpumask[MAX_NODES];
246
blueswir19043b622009-01-21 19:28:13 +0000247static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000248
blueswir18fcb1b92008-09-18 18:29:08 +0000249uint8_t qemu_uuid[16];
250
Jan Kiszka76e30d02009-07-02 00:19:02 +0200251static QEMUBootSetHandler *boot_set_handler;
252static void *boot_set_opaque;
253
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200254static NotifierList exit_notifiers =
255 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
256
Blue Swirld745bef2010-03-29 19:23:49 +0000257int kvm_allowed = 0;
258uint32_t xen_domid;
259enum xen_mode xen_mode = XEN_EMULATE;
260
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100261static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100262static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100263static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100264static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100265static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100266static int default_floppy = 1;
267static int default_cdrom = 1;
268static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100269
270static struct {
271 const char *driver;
272 int *flag;
273} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100274 { .driver = "isa-serial", .flag = &default_serial },
275 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100276 { .driver = "isa-fdc", .flag = &default_floppy },
277 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530278 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
279 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
280 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100281 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100282 { .driver = "cirrus-vga", .flag = &default_vga },
283 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100284};
285
286static int default_driver_check(QemuOpts *opts, void *opaque)
287{
288 const char *driver = qemu_opt_get(opts, "driver");
289 int i;
290
291 if (!driver)
292 return 0;
293 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
294 if (strcmp(default_list[i].driver, driver) != 0)
295 continue;
296 *(default_list[i].flag) = 0;
297 }
298 return 0;
299}
300
bellard0824d6f2003-06-24 13:42:40 +0000301/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100302/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000303
bellardc4b1fcc2004-03-14 21:44:30 +0000304/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000305/* host time/date access */
306void qemu_get_timedate(struct tm *tm, int offset)
307{
308 time_t ti;
309 struct tm *ret;
310
311 time(&ti);
312 ti += offset;
313 if (rtc_date_offset == -1) {
314 if (rtc_utc)
315 ret = gmtime(&ti);
316 else
317 ret = localtime(&ti);
318 } else {
319 ti -= rtc_date_offset;
320 ret = gmtime(&ti);
321 }
322
323 memcpy(tm, ret, sizeof(struct tm));
324}
325
326int qemu_timedate_diff(struct tm *tm)
327{
328 time_t seconds;
329
330 if (rtc_date_offset == -1)
331 if (rtc_utc)
332 seconds = mktimegm(tm);
333 else
334 seconds = mktime(tm);
335 else
336 seconds = mktimegm(tm) + rtc_date_offset;
337
338 return seconds - time(NULL);
339}
340
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300341void rtc_change_mon_event(struct tm *tm)
342{
343 QObject *data;
344
345 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
346 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
347 qobject_decref(data);
348}
349
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200350static void configure_rtc_date_offset(const char *startdate, int legacy)
351{
352 time_t rtc_start_date;
353 struct tm tm;
354
355 if (!strcmp(startdate, "now") && legacy) {
356 rtc_date_offset = -1;
357 } else {
358 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
359 &tm.tm_year,
360 &tm.tm_mon,
361 &tm.tm_mday,
362 &tm.tm_hour,
363 &tm.tm_min,
364 &tm.tm_sec) == 6) {
365 /* OK */
366 } else if (sscanf(startdate, "%d-%d-%d",
367 &tm.tm_year,
368 &tm.tm_mon,
369 &tm.tm_mday) == 3) {
370 tm.tm_hour = 0;
371 tm.tm_min = 0;
372 tm.tm_sec = 0;
373 } else {
374 goto date_fail;
375 }
376 tm.tm_year -= 1900;
377 tm.tm_mon--;
378 rtc_start_date = mktimegm(&tm);
379 if (rtc_start_date == -1) {
380 date_fail:
381 fprintf(stderr, "Invalid date format. Valid formats are:\n"
382 "'2006-06-17T16:01:21' or '2006-06-17'\n");
383 exit(1);
384 }
385 rtc_date_offset = time(NULL) - rtc_start_date;
386 }
387}
388
389static void configure_rtc(QemuOpts *opts)
390{
391 const char *value;
392
393 value = qemu_opt_get(opts, "base");
394 if (value) {
395 if (!strcmp(value, "utc")) {
396 rtc_utc = 1;
397 } else if (!strcmp(value, "localtime")) {
398 rtc_utc = 0;
399 } else {
400 configure_rtc_date_offset(value, 0);
401 }
402 }
Jan Kiszka68752042009-09-15 13:36:04 +0200403 value = qemu_opt_get(opts, "clock");
404 if (value) {
405 if (!strcmp(value, "host")) {
406 rtc_clock = host_clock;
407 } else if (!strcmp(value, "vm")) {
408 rtc_clock = vm_clock;
409 } else {
410 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
411 exit(1);
412 }
413 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200414 value = qemu_opt_get(opts, "driftfix");
415 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000416 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200417 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000418 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200419 rtc_td_hack = 0;
420 } else {
421 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
422 exit(1);
423 }
424 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200425}
426
balrog1ae26a12008-09-28 23:19:47 +0000427/***********************************************************/
428/* Bluetooth support */
429static int nb_hcis;
430static int cur_hci;
431static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000432
balrog1ae26a12008-09-28 23:19:47 +0000433static struct bt_vlan_s {
434 struct bt_scatternet_s net;
435 int id;
436 struct bt_vlan_s *next;
437} *first_bt_vlan;
438
439/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000440static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000441{
442 struct bt_vlan_s **pvlan, *vlan;
443 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
444 if (vlan->id == id)
445 return &vlan->net;
446 }
447 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
448 vlan->id = id;
449 pvlan = &first_bt_vlan;
450 while (*pvlan != NULL)
451 pvlan = &(*pvlan)->next;
452 *pvlan = vlan;
453 return &vlan->net;
454}
455
456static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
457{
458}
459
460static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
461{
462 return -ENOTSUP;
463}
464
465static struct HCIInfo null_hci = {
466 .cmd_send = null_hci_send,
467 .sco_send = null_hci_send,
468 .acl_send = null_hci_send,
469 .bdaddr_set = null_hci_addr_set,
470};
471
472struct HCIInfo *qemu_next_hci(void)
473{
474 if (cur_hci == nb_hcis)
475 return &null_hci;
476
477 return hci_table[cur_hci++];
478}
479
balrogdc72ac12008-11-09 00:04:26 +0000480static struct HCIInfo *hci_init(const char *str)
481{
482 char *endp;
483 struct bt_scatternet_s *vlan = 0;
484
485 if (!strcmp(str, "null"))
486 /* null */
487 return &null_hci;
488 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
489 /* host[:hciN] */
490 return bt_host_hci(str[4] ? str + 5 : "hci0");
491 else if (!strncmp(str, "hci", 3)) {
492 /* hci[,vlan=n] */
493 if (str[3]) {
494 if (!strncmp(str + 3, ",vlan=", 6)) {
495 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
496 if (*endp)
497 vlan = 0;
498 }
499 } else
500 vlan = qemu_find_bt_vlan(0);
501 if (vlan)
502 return bt_new_hci(vlan);
503 }
504
505 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
506
507 return 0;
508}
509
510static int bt_hci_parse(const char *str)
511{
512 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500513 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000514
515 if (nb_hcis >= MAX_NICS) {
516 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
517 return -1;
518 }
519
520 hci = hci_init(str);
521 if (!hci)
522 return -1;
523
524 bdaddr.b[0] = 0x52;
525 bdaddr.b[1] = 0x54;
526 bdaddr.b[2] = 0x00;
527 bdaddr.b[3] = 0x12;
528 bdaddr.b[4] = 0x34;
529 bdaddr.b[5] = 0x56 + nb_hcis;
530 hci->bdaddr_set(hci, bdaddr.b);
531
532 hci_table[nb_hcis++] = hci;
533
534 return 0;
535}
536
537static void bt_vhci_add(int vlan_id)
538{
539 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
540
541 if (!vlan->slave)
542 fprintf(stderr, "qemu: warning: adding a VHCI to "
543 "an empty scatternet %i\n", vlan_id);
544
545 bt_vhci_init(bt_new_hci(vlan));
546}
547
548static struct bt_device_s *bt_device_add(const char *opt)
549{
550 struct bt_scatternet_s *vlan;
551 int vlan_id = 0;
552 char *endp = strstr(opt, ",vlan=");
553 int len = (endp ? endp - opt : strlen(opt)) + 1;
554 char devname[10];
555
556 pstrcpy(devname, MIN(sizeof(devname), len), opt);
557
558 if (endp) {
559 vlan_id = strtol(endp + 6, &endp, 0);
560 if (*endp) {
561 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
562 return 0;
563 }
564 }
565
566 vlan = qemu_find_bt_vlan(vlan_id);
567
568 if (!vlan->slave)
569 fprintf(stderr, "qemu: warning: adding a slave device to "
570 "an empty scatternet %i\n", vlan_id);
571
572 if (!strcmp(devname, "keyboard"))
573 return bt_keyboard_init(vlan);
574
575 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
576 return 0;
577}
578
579static int bt_parse(const char *opt)
580{
581 const char *endp, *p;
582 int vlan;
583
584 if (strstart(opt, "hci", &endp)) {
585 if (!*endp || *endp == ',') {
586 if (*endp)
587 if (!strstart(endp, ",vlan=", 0))
588 opt = endp + 1;
589
590 return bt_hci_parse(opt);
591 }
592 } else if (strstart(opt, "vhci", &endp)) {
593 if (!*endp || *endp == ',') {
594 if (*endp) {
595 if (strstart(endp, ",vlan=", &p)) {
596 vlan = strtol(p, (char **) &endp, 0);
597 if (*endp) {
598 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
599 return 1;
600 }
601 } else {
602 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
603 return 1;
604 }
605 } else
606 vlan = 0;
607
608 bt_vhci_add(vlan);
609 return 0;
610 }
611 } else if (strstart(opt, "device:", &endp))
612 return !bt_device_add(endp);
613
614 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
615 return 1;
616}
617
balrog1ae26a12008-09-28 23:19:47 +0000618/***********************************************************/
619/* QEMU Block devices */
620
balrog609497a2008-01-14 02:56:53 +0000621#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +0000622#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +0000623#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +0000624#define PFLASH_ALIAS "if=pflash"
625#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +0000626#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +0000627
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200628static int drive_init_func(QemuOpts *opts, void *opaque)
629{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200630 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200631 int fatal_error = 0;
632
Markus Armbrustera803cb82010-06-02 13:31:55 +0200633 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200634 if (fatal_error)
635 return 1;
636 }
637 return 0;
638}
639
640static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
641{
642 if (NULL == qemu_opt_get(opts, "snapshot")) {
643 qemu_opt_set(opts, "snapshot", "on");
644 }
645 return 0;
646}
647
Jan Kiszka76e30d02009-07-02 00:19:02 +0200648void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
649{
650 boot_set_handler = func;
651 boot_set_opaque = opaque;
652}
653
654int qemu_boot_set(const char *boot_devices)
655{
656 if (!boot_set_handler) {
657 return -EINVAL;
658 }
659 return boot_set_handler(boot_set_opaque, boot_devices);
660}
661
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300662static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200663{
664 /* We just do some generic consistency checks */
665 const char *p;
666 int bitmap = 0;
667
668 for (p = devices; *p != '\0'; p++) {
669 /* Allowed boot devices are:
670 * a-b: floppy disk drives
671 * c-f: IDE disk drives
672 * g-m: machine implementation dependant drives
673 * n-p: network devices
674 * It's up to each machine implementation to check if the given boot
675 * devices match the actual hardware implementation and firmware
676 * features.
677 */
678 if (*p < 'a' || *p > 'p') {
679 fprintf(stderr, "Invalid boot device '%c'\n", *p);
680 exit(1);
681 }
682 if (bitmap & (1 << (*p - 'a'))) {
683 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
684 exit(1);
685 }
686 bitmap |= 1 << (*p - 'a');
687 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200688}
689
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200690static void restore_boot_devices(void *opaque)
691{
692 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600693 static int first = 1;
694
695 /* Restore boot order and remove ourselves after the first boot */
696 if (first) {
697 first = 0;
698 return;
699 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200700
701 qemu_boot_set(standard_boot_devices);
702
703 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
704 qemu_free(standard_boot_devices);
705}
706
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200707void add_boot_device_path(int32_t bootindex, DeviceState *dev,
708 const char *suffix)
709{
710 FWBootEntry *node, *i;
711
712 if (bootindex < 0) {
713 return;
714 }
715
716 assert(dev != NULL || suffix != NULL);
717
718 node = qemu_mallocz(sizeof(FWBootEntry));
719 node->bootindex = bootindex;
720 node->suffix = strdup(suffix);
721 node->dev = dev;
722
723 QTAILQ_FOREACH(i, &fw_boot_order, link) {
724 if (i->bootindex == bootindex) {
725 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
726 exit(1);
727 } else if (i->bootindex < bootindex) {
728 continue;
729 }
730 QTAILQ_INSERT_BEFORE(i, node, link);
731 return;
732 }
733 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
734}
735
aliguori268a3622009-04-21 22:30:27 +0000736static void numa_add(const char *optarg)
737{
738 char option[128];
739 char *endptr;
740 unsigned long long value, endvalue;
741 int nodenr;
742
743 optarg = get_opt_name(option, 128, optarg, ',') + 1;
744 if (!strcmp(option, "node")) {
745 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
746 nodenr = nb_numa_nodes;
747 } else {
748 nodenr = strtoull(option, NULL, 10);
749 }
750
751 if (get_param_value(option, 128, "mem", optarg) == 0) {
752 node_mem[nodenr] = 0;
753 } else {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200754 ssize_t sval;
755 sval = strtosz(option, NULL);
756 if (sval < 0) {
757 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
758 exit(1);
aliguori268a3622009-04-21 22:30:27 +0000759 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200760 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +0000761 }
762 if (get_param_value(option, 128, "cpus", optarg) == 0) {
763 node_cpumask[nodenr] = 0;
764 } else {
765 value = strtoull(option, &endptr, 10);
766 if (value >= 64) {
767 value = 63;
768 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
769 } else {
770 if (*endptr == '-') {
771 endvalue = strtoull(endptr+1, &endptr, 10);
772 if (endvalue >= 63) {
773 endvalue = 62;
774 fprintf(stderr,
775 "only 63 CPUs in NUMA mode supported.\n");
776 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100777 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000778 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100779 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000780 }
781 }
782 node_cpumask[nodenr] = value;
783 }
784 nb_numa_nodes++;
785 }
786 return;
787}
788
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200789static void smp_parse(const char *optarg)
790{
791 int smp, sockets = 0, threads = 0, cores = 0;
792 char *endptr;
793 char option[128];
794
795 smp = strtoul(optarg, &endptr, 10);
796 if (endptr != optarg) {
797 if (*endptr == ',') {
798 endptr++;
799 }
800 }
801 if (get_param_value(option, 128, "sockets", endptr) != 0)
802 sockets = strtoull(option, NULL, 10);
803 if (get_param_value(option, 128, "cores", endptr) != 0)
804 cores = strtoull(option, NULL, 10);
805 if (get_param_value(option, 128, "threads", endptr) != 0)
806 threads = strtoull(option, NULL, 10);
807 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
808 max_cpus = strtoull(option, NULL, 10);
809
810 /* compute missing values, prefer sockets over cores over threads */
811 if (smp == 0 || sockets == 0) {
812 sockets = sockets > 0 ? sockets : 1;
813 cores = cores > 0 ? cores : 1;
814 threads = threads > 0 ? threads : 1;
815 if (smp == 0) {
816 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200817 }
818 } else {
819 if (cores == 0) {
820 threads = threads > 0 ? threads : 1;
821 cores = smp / (sockets * threads);
822 } else {
Joel Schoppdca98162010-07-21 15:05:17 -0500823 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200824 }
825 }
826 smp_cpus = smp;
827 smp_cores = cores > 0 ? cores : 1;
828 smp_threads = threads > 0 ? threads : 1;
829 if (max_cpus == 0)
830 max_cpus = smp_cpus;
831}
832
bellard330d0412003-07-26 18:11:40 +0000833/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +0000834/* USB devices */
835
Markus Armbrusterfb080002010-05-28 15:38:44 +0200836static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +0000837{
838 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200839 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +0000840
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200841 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +0000842 return -1;
843
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +0100844 /* drivers with .usbdevice_name entry in USBDeviceInfo */
845 dev = usbdevice_create(devname);
846 if (dev)
847 goto done;
848
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200849 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +0000850 if (strstart(devname, "host:", &p)) {
851 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +0000852 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
853 dev = usb_bt_init(devname[2] ? hci_init(p) :
854 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +0000855 } else {
856 return -1;
857 }
pbrook0d92ed32006-05-21 16:30:15 +0000858 if (!dev)
859 return -1;
860
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200861done:
bellarda594cfb2005-11-06 16:13:29 +0000862 return 0;
863}
864
aliguori1f3870a2008-08-21 19:27:48 +0000865static int usb_device_del(const char *devname)
866{
867 int bus_num, addr;
868 const char *p;
869
aliguori5d0c5752008-09-14 01:07:41 +0000870 if (strstart(devname, "host:", &p))
871 return usb_host_device_close(p);
872
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200873 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +0000874 return -1;
875
876 p = strchr(devname, '.');
877 if (!p)
878 return -1;
879 bus_num = strtoul(devname, NULL, 0);
880 addr = strtoul(p + 1, NULL, 0);
881
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200882 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000883}
884
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200885static int usb_parse(const char *cmdline)
886{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800887 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +0200888 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800889 if (r < 0) {
890 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
891 }
892 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200893}
894
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300895void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000896{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800897 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +0200898 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100899 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800900 }
bellarda594cfb2005-11-06 16:13:29 +0000901}
902
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300903void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000904{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800905 const char *devname = qdict_get_str(qdict, "devname");
906 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100907 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800908 }
bellarda594cfb2005-11-06 16:13:29 +0000909}
910
bellardf7cce892004-12-08 22:21:25 +0000911/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +0000912/* PCMCIA/Cardbus */
913
914static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +0100915 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +0000916 struct pcmcia_socket_entry_s *next;
917} *pcmcia_sockets = 0;
918
Paul Brookbc24a222009-05-10 01:44:56 +0100919void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000920{
921 struct pcmcia_socket_entry_s *entry;
922
923 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
924 entry->socket = socket;
925 entry->next = pcmcia_sockets;
926 pcmcia_sockets = entry;
927}
928
Paul Brookbc24a222009-05-10 01:44:56 +0100929void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000930{
931 struct pcmcia_socket_entry_s *entry, **ptr;
932
933 ptr = &pcmcia_sockets;
934 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
935 if (entry->socket == socket) {
936 *ptr = entry->next;
937 qemu_free(entry);
938 }
939}
940
aliguori376253e2009-03-05 23:01:23 +0000941void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +0000942{
943 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +0000944
balrog201a51f2007-04-30 00:51:09 +0000945 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +0000946 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +0000947
948 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +0000949 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
950 iter->socket->attached ? iter->socket->card_string :
951 "Empty");
balrog201a51f2007-04-30 00:51:09 +0000952}
953
954/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +0000955/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +0000956
bellardc4b1fcc2004-03-14 21:44:30 +0000957typedef struct IOHandlerRecord {
958 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +0100959 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +0000960 IOHandler *fd_read;
961 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +0000962 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +0000963 void *opaque;
964 /* temporary data */
965 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +0100966 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +0000967} IOHandlerRecord;
968
Juan Quintela31d4ee62010-03-11 17:55:37 +0100969static QLIST_HEAD(, IOHandlerRecord) io_handlers =
970 QLIST_HEAD_INITIALIZER(io_handlers);
971
bellardc4b1fcc2004-03-14 21:44:30 +0000972
bellard7c9d8e02005-11-15 22:16:05 +0000973/* XXX: fd_read_poll should be suppressed, but an API change is
974 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +0000975int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +0100976 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +0000977 IOHandler *fd_read,
978 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +0000979 void *opaque)
bellardb4608c02003-06-27 17:34:32 +0000980{
Juan Quintela31d4ee62010-03-11 17:55:37 +0100981 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +0000982
bellard7c9d8e02005-11-15 22:16:05 +0000983 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100984 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000985 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +0000986 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +0000987 break;
988 }
bellard8a7ddc32004-03-31 19:00:16 +0000989 }
bellard7c9d8e02005-11-15 22:16:05 +0000990 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100991 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000992 if (ioh->fd == fd)
993 goto found;
994 }
995 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +0100996 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +0000997 found:
998 ioh->fd = fd;
999 ioh->fd_read_poll = fd_read_poll;
1000 ioh->fd_read = fd_read;
1001 ioh->fd_write = fd_write;
1002 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00001003 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00001004 }
bellard7c9d8e02005-11-15 22:16:05 +00001005 return 0;
1006}
1007
ths5fafdf22007-09-16 21:08:06 +00001008int qemu_set_fd_handler(int fd,
1009 IOHandler *fd_read,
1010 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001011 void *opaque)
1012{
1013 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00001014}
1015
bellard8a7ddc32004-03-31 19:00:16 +00001016/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001017/* machine registration */
1018
blueswir1bdaf78e2008-10-04 07:24:27 +00001019static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001020QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001021
1022int qemu_register_machine(QEMUMachine *m)
1023{
1024 QEMUMachine **pm;
1025 pm = &first_machine;
1026 while (*pm != NULL)
1027 pm = &(*pm)->next;
1028 m->next = NULL;
1029 *pm = m;
1030 return 0;
1031}
1032
pbrook9596ebb2007-11-18 01:44:38 +00001033static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001034{
1035 QEMUMachine *m;
1036
1037 for(m = first_machine; m != NULL; m = m->next) {
1038 if (!strcmp(m->name, name))
1039 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001040 if (m->alias && !strcmp(m->alias, name))
1041 return m;
bellardcc1daa42005-06-05 14:49:17 +00001042 }
1043 return NULL;
1044}
1045
Anthony Liguori0c257432009-05-21 20:41:01 -05001046static QEMUMachine *find_default_machine(void)
1047{
1048 QEMUMachine *m;
1049
1050 for(m = first_machine; m != NULL; m = m->next) {
1051 if (m->is_default) {
1052 return m;
1053 }
1054 }
1055 return NULL;
1056}
1057
bellardcc1daa42005-06-05 14:49:17 +00001058/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001059/* main execution loop */
1060
pbrook9596ebb2007-11-18 01:44:38 +00001061static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001062{
aliguori7d957bd2009-01-15 22:14:11 +00001063 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001064 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001065 DisplayChangeListener *dcl = ds->listeners;
1066
Sheng Yang62a27442010-01-26 19:21:16 +08001067 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001068 dpy_refresh(ds);
1069
1070 while (dcl != NULL) {
1071 if (dcl->gui_timer_interval &&
1072 dcl->gui_timer_interval < interval)
1073 interval = dcl->gui_timer_interval;
1074 dcl = dcl->next;
1075 }
1076 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001077}
1078
blueswir19043b622009-01-21 19:28:13 +00001079static void nographic_update(void *opaque)
1080{
1081 uint64_t interval = GUI_REFRESH_INTERVAL;
1082
Sheng Yang62a27442010-01-26 19:21:16 +08001083 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00001084 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1085}
1086
bellard0bd48852005-11-11 00:00:47 +00001087struct vm_change_state_entry {
1088 VMChangeStateHandler *cb;
1089 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001090 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001091};
1092
Blue Swirl72cf2d42009-09-12 07:36:22 +00001093static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001094
1095VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1096 void *opaque)
1097{
1098 VMChangeStateEntry *e;
1099
1100 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001101
1102 e->cb = cb;
1103 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001104 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001105 return e;
1106}
1107
1108void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1109{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001110 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001111 qemu_free (e);
1112}
1113
Blue Swirl296af7c2010-03-29 19:23:50 +00001114void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001115{
1116 VMChangeStateEntry *e;
1117
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001118 trace_vm_state_notify(running, reason);
1119
bellard0bd48852005-11-11 00:00:47 +00001120 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001121 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001122 }
1123}
1124
bellard8a7ddc32004-03-31 19:00:16 +00001125void vm_start(void)
1126{
1127 if (!vm_running) {
1128 cpu_enable_ticks();
1129 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001130 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001131 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001132 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001133 }
1134}
1135
bellardbb0c6722004-06-20 12:37:32 +00001136/* reset/shutdown handler */
1137
1138typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001139 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001140 QEMUResetHandler *func;
1141 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001142} QEMUResetEntry;
1143
Blue Swirl72cf2d42009-09-12 07:36:22 +00001144static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1145 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001146static int reset_requested;
1147static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00001148static int powerdown_requested;
Blue Swirl296af7c2010-03-29 19:23:50 +00001149int debug_requested;
Blue Swirl7277e022010-04-12 17:19:06 +00001150int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001151
aurel32cf7a2fe2008-03-18 06:53:05 +00001152int qemu_shutdown_requested(void)
1153{
1154 int r = shutdown_requested;
1155 shutdown_requested = 0;
1156 return r;
1157}
1158
1159int qemu_reset_requested(void)
1160{
1161 int r = reset_requested;
1162 reset_requested = 0;
1163 return r;
1164}
1165
1166int qemu_powerdown_requested(void)
1167{
1168 int r = powerdown_requested;
1169 powerdown_requested = 0;
1170 return r;
1171}
1172
aliguorie5689022009-04-24 18:03:54 +00001173static int qemu_debug_requested(void)
1174{
1175 int r = debug_requested;
1176 debug_requested = 0;
1177 return r;
1178}
1179
aliguori6e29f5d2009-04-24 18:04:02 +00001180static int qemu_vmstop_requested(void)
1181{
1182 int r = vmstop_requested;
1183 vmstop_requested = 0;
1184 return r;
1185}
1186
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001187void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001188{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001189 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001190
bellardbb0c6722004-06-20 12:37:32 +00001191 re->func = func;
1192 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001193 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001194}
1195
Jan Kiszkadda9b292009-07-02 00:19:02 +02001196void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001197{
1198 QEMUResetEntry *re;
1199
Blue Swirl72cf2d42009-09-12 07:36:22 +00001200 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001201 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001202 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001203 qemu_free(re);
1204 return;
1205 }
1206 }
1207}
1208
1209void qemu_system_reset(void)
1210{
1211 QEMUResetEntry *re, *nre;
1212
1213 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001214 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001215 re->func(re->opaque);
1216 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001217 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001218 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001219}
1220
1221void qemu_system_reset_request(void)
1222{
bellardd1beab82006-10-02 19:44:22 +00001223 if (no_reboot) {
1224 shutdown_requested = 1;
1225 } else {
1226 reset_requested = 1;
1227 }
aliguorid9f75a42009-04-24 18:03:11 +00001228 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001229}
1230
1231void qemu_system_shutdown_request(void)
1232{
1233 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001234 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001235}
1236
bellard34751872005-07-02 14:31:34 +00001237void qemu_system_powerdown_request(void)
1238{
1239 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001240 qemu_notify_event();
1241}
1242
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001243void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001244{
1245 IOHandlerRecord *ioh;
1246 fd_set rfds, wfds, xfds;
1247 int ret, nfds;
1248 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001249 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001250
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001251 if (nonblocking)
1252 timeout = 0;
1253 else {
1254 timeout = qemu_calculate_timeout();
1255 qemu_bh_update_timeout(&timeout);
1256 }
aliguori56f3a5d2008-10-31 18:07:17 +00001257
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001258 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001259
bellardfd1dff42006-02-01 21:29:26 +00001260 /* poll any events */
1261 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001262 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001263 FD_ZERO(&rfds);
1264 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001265 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001266 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00001267 if (ioh->deleted)
1268 continue;
bellardfd1dff42006-02-01 21:29:26 +00001269 if (ioh->fd_read &&
1270 (!ioh->fd_read_poll ||
1271 ioh->fd_read_poll(ioh->opaque) != 0)) {
1272 FD_SET(ioh->fd, &rfds);
1273 if (ioh->fd > nfds)
1274 nfds = ioh->fd;
1275 }
1276 if (ioh->fd_write) {
1277 FD_SET(ioh->fd, &wfds);
1278 if (ioh->fd > nfds)
1279 nfds = ioh->fd;
1280 }
1281 }
ths3b46e622007-09-17 08:09:54 +00001282
aliguori56f3a5d2008-10-31 18:07:17 +00001283 tv.tv_sec = timeout / 1000;
1284 tv.tv_usec = (timeout % 1000) * 1000;
1285
Jan Kiszkad918f232009-06-24 14:42:30 +02001286 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1287
aliguori48708522009-04-24 18:03:49 +00001288 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001289 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001290 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00001291 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001292 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00001293
Juan Quintela31d4ee62010-03-11 17:55:37 +01001294 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
Stefan Hajnoczi0290b572010-11-03 14:29:44 +00001295 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1296 ioh->fd_read(ioh->opaque);
1297 }
1298 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1299 ioh->fd_write(ioh->opaque);
1300 }
1301
1302 /* Do this last in case read/write handlers marked it for deletion */
thscafffd42007-02-28 21:59:44 +00001303 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001304 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00001305 qemu_free(ioh);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001306 }
thscafffd42007-02-28 21:59:44 +00001307 }
bellardfd1dff42006-02-01 21:29:26 +00001308 }
Jan Kiszkad918f232009-06-24 14:42:30 +02001309
1310 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001311
Paolo Bonzinib6964822010-03-10 11:38:45 +01001312 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001313
pbrook423f0742007-05-23 00:06:54 +00001314 /* Check bottom-halves last in case any of the earlier events triggered
1315 them. */
1316 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001317
bellard5905b2e2004-08-01 21:53:26 +00001318}
1319
aliguori43b96852009-04-24 18:03:33 +00001320static int vm_can_run(void)
1321{
1322 if (powerdown_requested)
1323 return 0;
1324 if (reset_requested)
1325 return 0;
1326 if (shutdown_requested)
1327 return 0;
aliguorie5689022009-04-24 18:03:54 +00001328 if (debug_requested)
1329 return 0;
aliguori43b96852009-04-24 18:03:33 +00001330 return 1;
1331}
1332
Blue Swirld9c32312009-08-09 08:42:19 +00001333qemu_irq qemu_system_powerdown;
1334
aliguori43b96852009-04-24 18:03:33 +00001335static void main_loop(void)
1336{
aliguori6e29f5d2009-04-24 18:04:02 +00001337 int r;
aliguori43b96852009-04-24 18:03:33 +00001338
Blue Swirl7277e022010-04-12 17:19:06 +00001339 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001340
aliguori6e29f5d2009-04-24 18:04:02 +00001341 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00001342 do {
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001343 bool nonblocking = false;
aliguorie6e35b12009-04-24 18:03:57 +00001344#ifdef CONFIG_PROFILER
1345 int64_t ti;
1346#endif
aliguorid6dc3d42009-04-24 18:04:07 +00001347#ifndef CONFIG_IOTHREAD
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001348 nonblocking = cpu_exec_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001349#endif
aliguori43b96852009-04-24 18:03:33 +00001350#ifdef CONFIG_PROFILER
1351 ti = profile_getclock();
1352#endif
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001353 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001354#ifdef CONFIG_PROFILER
1355 dev_time += profile_getclock() - ti;
1356#endif
aliguorie5689022009-04-24 18:03:54 +00001357 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00001358
Blue Swirl54fc6ea2010-03-29 19:23:46 +00001359 if ((r = qemu_debug_requested())) {
1360 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001361 }
aliguori43b96852009-04-24 18:03:33 +00001362 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001363 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001364 if (no_shutdown) {
1365 vm_stop(0);
1366 no_shutdown = 0;
1367 } else
1368 break;
1369 }
aliguorid6dc3d42009-04-24 18:04:07 +00001370 if (qemu_reset_requested()) {
1371 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00001372 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00001373 resume_all_vcpus();
1374 }
Blue Swirld9c32312009-08-09 08:42:19 +00001375 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001376 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001377 qemu_irq_raise(qemu_system_powerdown);
1378 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001379 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001380 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001381 }
aliguori43b96852009-04-24 18:03:33 +00001382 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001383 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001384 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001385}
1386
pbrook9bd7e6d2009-04-07 22:58:45 +00001387static void version(void)
1388{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001389 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001390}
1391
ths15f82202007-06-29 23:26:08 +00001392static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001393{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001394 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001395#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1396 opt_help
blueswir15824d652009-03-28 06:44:27 +00001397#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001398#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001399#undef DEF
1400#undef DEFHEADING
1401#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001402 ;
1403 version();
1404 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001405 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001406 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001407 "\n"
1408 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001409 "During emulation, the following keys are useful:\n"
1410 "ctrl-alt-f toggle full screen\n"
1411 "ctrl-alt-n switch to virtual console 'n'\n"
1412 "ctrl-alt toggle mouse and keyboard grab\n"
1413 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001414 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1415 "qemu",
1416 options_help);
ths15f82202007-06-29 23:26:08 +00001417 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001418}
1419
bellardcd6f1162004-05-13 22:02:20 +00001420#define HAS_ARG 0x0001
1421
bellardcd6f1162004-05-13 22:02:20 +00001422typedef struct QEMUOption {
1423 const char *name;
1424 int flags;
1425 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001426 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001427} QEMUOption;
1428
blueswir1dbed7e42008-10-01 19:38:09 +00001429static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001430 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1431#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1432 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001433#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001434#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001435#undef DEF
1436#undef DEFHEADING
1437#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001438 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001439};
malc3893c122008-09-28 00:42:05 +00001440static void select_vgahw (const char *p)
1441{
1442 const char *opts;
1443
Gerd Hoffmann64465292009-12-08 13:11:45 +01001444 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001445 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001446 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001447 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001448 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001449 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001450 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001451 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001452 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001453 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00001454 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001455 invalid_vga:
1456 fprintf(stderr, "Unknown vga type: %s\n", p);
1457 exit(1);
1458 }
malccb5a7aa2008-09-28 00:42:12 +00001459 while (*opts) {
1460 const char *nextopt;
1461
1462 if (strstart(opts, ",retrace=", &nextopt)) {
1463 opts = nextopt;
1464 if (strstart(opts, "dumb", &nextopt))
1465 vga_retrace_method = VGA_RETRACE_DUMB;
1466 else if (strstart(opts, "precise", &nextopt))
1467 vga_retrace_method = VGA_RETRACE_PRECISE;
1468 else goto invalid_vga;
1469 } else goto invalid_vga;
1470 opts = nextopt;
1471 }
malc3893c122008-09-28 00:42:05 +00001472}
1473
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001474static int balloon_parse(const char *arg)
1475{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001476 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001477
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001478 if (strcmp(arg, "none") == 0) {
1479 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001480 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001481
1482 if (!strncmp(arg, "virtio", 6)) {
1483 if (arg[6] == ',') {
1484 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001485 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001486 if (!opts)
1487 return -1;
1488 } else {
1489 /* create empty opts */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001490 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001491 }
1492 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1493 return 0;
1494 }
1495
1496 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001497}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001498
Paul Brook5cea8592009-05-30 00:52:44 +01001499char *qemu_find_file(int type, const char *name)
1500{
1501 int len;
1502 const char *subdir;
1503 char *buf;
1504
1505 /* If name contains path separators then try it as a straight path. */
1506 if ((strchr(name, '/') || strchr(name, '\\'))
1507 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02001508 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001509 }
1510 switch (type) {
1511 case QEMU_FILE_TYPE_BIOS:
1512 subdir = "";
1513 break;
1514 case QEMU_FILE_TYPE_KEYMAP:
1515 subdir = "keymaps/";
1516 break;
1517 default:
1518 abort();
1519 }
1520 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1521 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001522 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001523 if (access(buf, R_OK)) {
1524 qemu_free(buf);
1525 return NULL;
1526 }
1527 return buf;
1528}
1529
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001530static int device_help_func(QemuOpts *opts, void *opaque)
1531{
1532 return qdev_device_help(opts);
1533}
1534
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001535static int device_init_func(QemuOpts *opts, void *opaque)
1536{
1537 DeviceState *dev;
1538
1539 dev = qdev_device_add(opts);
1540 if (!dev)
1541 return -1;
1542 return 0;
1543}
1544
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001545static int chardev_init_func(QemuOpts *opts, void *opaque)
1546{
1547 CharDriverState *chr;
1548
1549 chr = qemu_chr_open_opts(opts, NULL);
1550 if (!chr)
1551 return -1;
1552 return 0;
1553}
1554
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07001555#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301556static int fsdev_init_func(QemuOpts *opts, void *opaque)
1557{
1558 int ret;
1559 ret = qemu_fsdev_add(opts);
1560
1561 return ret;
1562}
1563#endif
1564
Gerd Hoffmann88589342009-12-08 13:11:50 +01001565static int mon_init_func(QemuOpts *opts, void *opaque)
1566{
1567 CharDriverState *chr;
1568 const char *chardev;
1569 const char *mode;
1570 int flags;
1571
1572 mode = qemu_opt_get(opts, "mode");
1573 if (mode == NULL) {
1574 mode = "readline";
1575 }
1576 if (strcmp(mode, "readline") == 0) {
1577 flags = MONITOR_USE_READLINE;
1578 } else if (strcmp(mode, "control") == 0) {
1579 flags = MONITOR_USE_CONTROL;
1580 } else {
1581 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1582 exit(1);
1583 }
1584
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01001585 if (qemu_opt_get_bool(opts, "pretty", 0))
1586 flags |= MONITOR_USE_PRETTY;
1587
Gerd Hoffmann88589342009-12-08 13:11:50 +01001588 if (qemu_opt_get_bool(opts, "default", 0))
1589 flags |= MONITOR_IS_DEFAULT;
1590
1591 chardev = qemu_opt_get(opts, "chardev");
1592 chr = qemu_chr_find(chardev);
1593 if (chr == NULL) {
1594 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1595 exit(1);
1596 }
1597
1598 monitor_init(chr, flags);
1599 return 0;
1600}
1601
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001602static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001603{
1604 static int monitor_device_index = 0;
1605 QemuOpts *opts;
1606 const char *p;
1607 char label[32];
1608 int def = 0;
1609
1610 if (strstart(optarg, "chardev:", &p)) {
1611 snprintf(label, sizeof(label), "%s", p);
1612 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001613 snprintf(label, sizeof(label), "compat_monitor%d",
1614 monitor_device_index);
1615 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001616 def = 1;
1617 }
1618 opts = qemu_chr_parse_compat(label, optarg);
1619 if (!opts) {
1620 fprintf(stderr, "parse error: %s\n", optarg);
1621 exit(1);
1622 }
1623 }
1624
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001625 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001626 if (!opts) {
1627 fprintf(stderr, "duplicate chardev: %s\n", label);
1628 exit(1);
1629 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001630 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001631 qemu_opt_set(opts, "chardev", label);
1632 if (def)
1633 qemu_opt_set(opts, "default", "on");
1634 monitor_device_index++;
1635}
1636
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001637struct device_config {
1638 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001639 DEV_USB, /* -usbdevice */
1640 DEV_BT, /* -bt */
1641 DEV_SERIAL, /* -serial */
1642 DEV_PARALLEL, /* -parallel */
1643 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001644 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001645 } type;
1646 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001647 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001648};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001649QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001650
1651static void add_device_config(int type, const char *cmdline)
1652{
1653 struct device_config *conf;
1654
1655 conf = qemu_mallocz(sizeof(*conf));
1656 conf->type = type;
1657 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001658 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001659}
1660
1661static int foreach_device_config(int type, int (*func)(const char *cmdline))
1662{
1663 struct device_config *conf;
1664 int rc;
1665
Blue Swirl72cf2d42009-09-12 07:36:22 +00001666 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001667 if (conf->type != type)
1668 continue;
1669 rc = func(conf->cmdline);
1670 if (0 != rc)
1671 return rc;
1672 }
1673 return 0;
1674}
1675
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001676static int serial_parse(const char *devname)
1677{
1678 static int index = 0;
1679 char label[32];
1680
1681 if (strcmp(devname, "none") == 0)
1682 return 0;
1683 if (index == MAX_SERIAL_PORTS) {
1684 fprintf(stderr, "qemu: too many serial ports\n");
1685 exit(1);
1686 }
1687 snprintf(label, sizeof(label), "serial%d", index);
1688 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1689 if (!serial_hds[index]) {
1690 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1691 devname, strerror(errno));
1692 return -1;
1693 }
1694 index++;
1695 return 0;
1696}
1697
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01001698static int parallel_parse(const char *devname)
1699{
1700 static int index = 0;
1701 char label[32];
1702
1703 if (strcmp(devname, "none") == 0)
1704 return 0;
1705 if (index == MAX_PARALLEL_PORTS) {
1706 fprintf(stderr, "qemu: too many parallel ports\n");
1707 exit(1);
1708 }
1709 snprintf(label, sizeof(label), "parallel%d", index);
1710 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1711 if (!parallel_hds[index]) {
1712 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1713 devname, strerror(errno));
1714 return -1;
1715 }
1716 index++;
1717 return 0;
1718}
1719
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001720static int virtcon_parse(const char *devname)
1721{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001722 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001723 static int index = 0;
1724 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05301725 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001726
1727 if (strcmp(devname, "none") == 0)
1728 return 0;
1729 if (index == MAX_VIRTIO_CONSOLES) {
1730 fprintf(stderr, "qemu: too many virtio consoles\n");
1731 exit(1);
1732 }
Amit Shah392ecf52010-01-21 16:19:23 +05301733
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001734 bus_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301735 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1736
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001737 dev_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301738 qemu_opt_set(dev_opts, "driver", "virtconsole");
1739
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001740 snprintf(label, sizeof(label), "virtcon%d", index);
1741 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1742 if (!virtcon_hds[index]) {
1743 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1744 devname, strerror(errno));
1745 return -1;
1746 }
Amit Shah392ecf52010-01-21 16:19:23 +05301747 qemu_opt_set(dev_opts, "chardev", label);
1748
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001749 index++;
1750 return 0;
1751}
1752
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001753static int debugcon_parse(const char *devname)
1754{
1755 QemuOpts *opts;
1756
1757 if (!qemu_chr_open("debugcon", devname, NULL)) {
1758 exit(1);
1759 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001760 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001761 if (!opts) {
1762 fprintf(stderr, "qemu: already have a debugcon device\n");
1763 exit(1);
1764 }
1765 qemu_opt_set(opts, "driver", "isa-debugcon");
1766 qemu_opt_set(opts, "chardev", "debugcon");
1767 return 0;
1768}
1769
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001770void qemu_add_exit_notifier(Notifier *notify)
1771{
1772 notifier_list_add(&exit_notifiers, notify);
1773}
1774
1775void qemu_remove_exit_notifier(Notifier *notify)
1776{
1777 notifier_list_remove(&exit_notifiers, notify);
1778}
1779
1780static void qemu_run_exit_notifiers(void)
1781{
1782 notifier_list_notify(&exit_notifiers);
1783}
1784
Anthony Liguori6530a972010-01-22 09:18:06 -06001785static const QEMUOption *lookup_opt(int argc, char **argv,
1786 const char **poptarg, int *poptind)
1787{
1788 const QEMUOption *popt;
1789 int optind = *poptind;
1790 char *r = argv[optind];
1791 const char *optarg;
1792
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001793 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06001794 optind++;
1795 /* Treat --foo the same as -foo. */
1796 if (r[1] == '-')
1797 r++;
1798 popt = qemu_options;
1799 for(;;) {
1800 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001801 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06001802 exit(1);
1803 }
1804 if (!strcmp(popt->name, r + 1))
1805 break;
1806 popt++;
1807 }
1808 if (popt->flags & HAS_ARG) {
1809 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001810 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06001811 exit(1);
1812 }
1813 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001814 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06001815 } else {
1816 optarg = NULL;
1817 }
1818
1819 *poptarg = optarg;
1820 *poptind = optind;
1821
1822 return popt;
1823}
1824
malc902b3d52008-12-10 19:18:40 +00001825int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00001826{
aliguori59030a82009-04-05 18:43:41 +00001827 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00001828 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03001829 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01001830 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00001831 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00001832 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05001833 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00001834 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00001835 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00001836 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001837 QemuOpts *hda_opts = NULL, *opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02001838 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00001839 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06001840 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00001841 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001842 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00001843 const char *cpu_model;
bellard26a5f132008-05-28 12:30:31 +00001844 int tb_size;
ths93815bc2007-03-19 15:58:31 +00001845 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00001846 const char *incoming = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05001847 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06001848 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00001849
Prerna Saxenaab6540d2010-08-09 11:48:32 +01001850#ifdef CONFIG_SIMPLE_TRACE
1851 const char *trace_file = NULL;
1852#endif
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001853 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01001854 error_set_progname(argv[0]);
1855
Jan Kiszka68752042009-09-15 13:36:04 +02001856 init_clocks();
1857
malc902b3d52008-12-10 19:18:40 +00001858 qemu_cache_utils_init(envp);
1859
Blue Swirl72cf2d42009-09-12 07:36:22 +00001860 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02001861 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00001862
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05001863 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05001864 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00001865 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00001866 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00001867 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00001868 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00001869 kernel_filename = NULL;
1870 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00001871 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00001872 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00001873
aliguori268a3622009-04-21 22:30:27 +00001874 for (i = 0; i < MAX_NODES; i++) {
1875 node_mem[i] = 0;
1876 node_cpumask[i] = 0;
1877 }
1878
aliguori268a3622009-04-21 22:30:27 +00001879 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00001880 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00001881
bellard26a5f132008-05-28 12:30:31 +00001882 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00001883 autostart= 1;
1884
Anthony Liguori292444c2010-01-21 10:57:58 -06001885 /* first pass of option parsing */
1886 optind = 1;
1887 while (optind < argc) {
1888 if (argv[optind][0] != '-') {
1889 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06001890 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06001891 continue;
1892 } else {
1893 const QEMUOption *popt;
1894
1895 popt = lookup_opt(argc, argv, &optarg, &optind);
1896 switch (popt->index) {
1897 case QEMU_OPTION_nodefconfig:
1898 defconfig=0;
1899 break;
1900 }
1901 }
1902 }
1903
1904 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001905 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01001906
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001907 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02001908 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001909 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001910 }
1911
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001912 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02001913 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001914 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001915 }
1916 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00001917 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06001918
1919 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00001920 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00001921 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00001922 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00001923 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06001924 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001925 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00001926 } else {
1927 const QEMUOption *popt;
1928
Anthony Liguori6530a972010-01-22 09:18:06 -06001929 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00001930 if (!(popt->arch_mask & arch_type)) {
1931 printf("Option %s not supported for this target\n", popt->name);
1932 exit(1);
1933 }
bellardcd6f1162004-05-13 22:02:20 +00001934 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00001935 case QEMU_OPTION_M:
1936 machine = find_machine(optarg);
1937 if (!machine) {
1938 QEMUMachine *m;
1939 printf("Supported machines are:\n");
1940 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001941 if (m->alias)
1942 printf("%-10s %s (alias of %s)\n",
1943 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00001944 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00001945 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05001946 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00001947 }
ths15f82202007-06-29 23:26:08 +00001948 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00001949 }
1950 break;
j_mayer94fc95c2007-03-05 19:44:02 +00001951 case QEMU_OPTION_cpu:
1952 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00001953 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00001954 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00001955 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00001956 } else {
1957 cpu_model = optarg;
1958 }
1959 break;
bellardcd6f1162004-05-13 22:02:20 +00001960 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00001961 initrd_filename = optarg;
1962 break;
bellardcd6f1162004-05-13 22:02:20 +00001963 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00001964 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001965 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00001966 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001967 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00001968 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00001969 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00001970 translation == BIOS_ATA_TRANSLATION_LBA ?
1971 ",trans=lba" :
1972 translation == BIOS_ATA_TRANSLATION_NONE ?
1973 ",trans=none" : "");
1974 break;
bellardcd6f1162004-05-13 22:02:20 +00001975 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00001976 case QEMU_OPTION_hdc:
1977 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00001978 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00001979 break;
thse4bcb142007-12-02 04:51:10 +00001980 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00001981 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00001982 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02001983 case QEMU_OPTION_set:
1984 if (qemu_set_option(optarg) != 0)
1985 exit(1);
1986 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01001987 case QEMU_OPTION_global:
1988 if (qemu_global_option(optarg) != 0)
1989 exit(1);
1990 break;
balrog3e3d5812007-04-30 02:09:25 +00001991 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00001992 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00001993 break;
pbrooka1bb27b2007-04-06 16:49:48 +00001994 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00001995 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00001996 break;
j_mayer86f55662007-04-24 06:52:59 +00001997 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00001998 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00001999 break;
bellardcd6f1162004-05-13 22:02:20 +00002000 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002001 snapshot = 1;
2002 break;
bellardcd6f1162004-05-13 22:02:20 +00002003 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002004 {
bellard330d0412003-07-26 18:11:40 +00002005 const char *p;
2006 p = optarg;
2007 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002008 if (cyls < 1 || cyls > 16383)
2009 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002010 if (*p != ',')
2011 goto chs_fail;
2012 p++;
2013 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002014 if (heads < 1 || heads > 16)
2015 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002016 if (*p != ',')
2017 goto chs_fail;
2018 p++;
2019 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002020 if (secs < 1 || secs > 63)
2021 goto chs_fail;
2022 if (*p == ',') {
2023 p++;
2024 if (!strcmp(p, "none"))
2025 translation = BIOS_ATA_TRANSLATION_NONE;
2026 else if (!strcmp(p, "lba"))
2027 translation = BIOS_ATA_TRANSLATION_LBA;
2028 else if (!strcmp(p, "auto"))
2029 translation = BIOS_ATA_TRANSLATION_AUTO;
2030 else
2031 goto chs_fail;
2032 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002033 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002034 fprintf(stderr, "qemu: invalid physical CHS format\n");
2035 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002036 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002037 if (hda_opts != NULL) {
2038 char num[16];
2039 snprintf(num, sizeof(num), "%d", cyls);
2040 qemu_opt_set(hda_opts, "cyls", num);
2041 snprintf(num, sizeof(num), "%d", heads);
2042 qemu_opt_set(hda_opts, "heads", num);
2043 snprintf(num, sizeof(num), "%d", secs);
2044 qemu_opt_set(hda_opts, "secs", num);
2045 if (translation == BIOS_ATA_TRANSLATION_LBA)
2046 qemu_opt_set(hda_opts, "trans", "lba");
2047 if (translation == BIOS_ATA_TRANSLATION_NONE)
2048 qemu_opt_set(hda_opts, "trans", "none");
2049 }
bellard330d0412003-07-26 18:11:40 +00002050 }
2051 break;
aliguori268a3622009-04-21 22:30:27 +00002052 case QEMU_OPTION_numa:
2053 if (nb_numa_nodes >= MAX_NODES) {
2054 fprintf(stderr, "qemu: too many NUMA nodes\n");
2055 exit(1);
2056 }
2057 numa_add(optarg);
2058 break;
bellardcd6f1162004-05-13 22:02:20 +00002059 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002060 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002061 break;
balrog4d3b6f62008-02-10 16:33:14 +00002062#ifdef CONFIG_CURSES
2063 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002064 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00002065 break;
2066#endif
balroga171fe32007-04-30 01:48:07 +00002067 case QEMU_OPTION_portrait:
2068 graphic_rotate = 1;
2069 break;
bellardcd6f1162004-05-13 22:02:20 +00002070 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002071 kernel_filename = optarg;
2072 break;
bellardcd6f1162004-05-13 22:02:20 +00002073 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002074 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002075 break;
bellardcd6f1162004-05-13 22:02:20 +00002076 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00002077 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00002078 break;
bellardcd6f1162004-05-13 22:02:20 +00002079 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002080 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002081 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002082 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002083 };
2084 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002085 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002086 int legacy = 0;
2087
2088 if (!strchr(optarg, '=')) {
2089 legacy = 1;
2090 pstrcpy(buf, sizeof(buf), optarg);
2091 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2092 fprintf(stderr,
2093 "qemu: unknown boot parameter '%s' in '%s'\n",
2094 buf, optarg);
2095 exit(1);
2096 }
2097
2098 if (legacy ||
2099 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002100 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002101 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002102 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002103 if (!legacy) {
2104 if (get_param_value(buf, sizeof(buf),
2105 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002106 validate_bootdevices(buf);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002107 standard_boot_devices = qemu_strdup(boot_devices);
2108 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2109 qemu_register_reset(restore_boot_devices,
2110 standard_boot_devices);
2111 }
Jan Kiszka95387492009-07-02 00:19:02 +02002112 if (get_param_value(buf, sizeof(buf),
2113 "menu", optarg)) {
2114 if (!strcmp(buf, "on")) {
2115 boot_menu = 1;
2116 } else if (!strcmp(buf, "off")) {
2117 boot_menu = 0;
2118 } else {
2119 fprintf(stderr,
2120 "qemu: invalid option value '%s'\n",
2121 buf);
2122 exit(1);
2123 }
2124 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002125 }
bellard36b486b2003-11-11 13:36:08 +00002126 }
2127 break;
bellardcd6f1162004-05-13 22:02:20 +00002128 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002129 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00002130 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00002131 break;
bellard52ca8d62006-06-14 16:03:05 +00002132 case QEMU_OPTION_no_fd_bootchk:
2133 fd_bootchk = 0;
2134 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002135 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002136 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002137 exit(1);
2138 }
2139 break;
bellard7c9d8e02005-11-15 22:16:05 +00002140 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002141 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002142 exit(1);
2143 }
bellard702c6512004-04-02 21:21:32 +00002144 break;
bellardc7f74642004-08-24 21:57:12 +00002145#ifdef CONFIG_SLIRP
2146 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002147 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002148 break;
ths47d5d012007-02-20 00:05:08 +00002149 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002150 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002151 break;
bellard9bf05442004-08-25 22:12:49 +00002152 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002153 if (net_slirp_redir(optarg) < 0)
2154 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002155 break;
bellardc7f74642004-08-24 21:57:12 +00002156#endif
balrogdc72ac12008-11-09 00:04:26 +00002157 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002158 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002159 break;
bellard1d14ffa2005-10-30 18:58:22 +00002160 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002161 if (!(audio_available())) {
2162 printf("Option %s not supported for this target\n", popt->name);
2163 exit(1);
2164 }
bellard1d14ffa2005-10-30 18:58:22 +00002165 AUD_help ();
2166 exit (0);
2167 break;
2168 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002169 if (!(audio_available())) {
2170 printf("Option %s not supported for this target\n", popt->name);
2171 exit(1);
2172 }
bellard1d14ffa2005-10-30 18:58:22 +00002173 select_soundhw (optarg);
2174 break;
bellardcd6f1162004-05-13 22:02:20 +00002175 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002176 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002177 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002178 case QEMU_OPTION_version:
2179 version();
2180 exit(0);
2181 break;
aurel3200f82b82008-04-27 21:12:55 +00002182 case QEMU_OPTION_m: {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002183 ssize_t value;
aurel3200f82b82008-04-27 21:12:55 +00002184
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002185 value = strtosz(optarg, NULL);
2186 if (value < 0) {
aurel3200f82b82008-04-27 21:12:55 +00002187 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002188 exit(1);
2189 }
aurel3200f82b82008-04-27 21:12:55 +00002190
2191 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002192 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00002193 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2194 exit(1);
2195 }
Anthony Liguoric227f092009-10-01 16:12:16 -05002196 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002197 fprintf(stderr, "qemu: ram size too large\n");
2198 exit(1);
2199 }
2200 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002201 break;
aurel3200f82b82008-04-27 21:12:55 +00002202 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002203 case QEMU_OPTION_mempath:
2204 mem_path = optarg;
2205 break;
2206#ifdef MAP_POPULATE
2207 case QEMU_OPTION_mem_prealloc:
2208 mem_prealloc = 1;
2209 break;
2210#endif
bellardcd6f1162004-05-13 22:02:20 +00002211 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002212 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00002213 break;
bellardcd6f1162004-05-13 22:02:20 +00002214 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002215 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002216 break;
aliguori59030a82009-04-05 18:43:41 +00002217 case QEMU_OPTION_gdb:
2218 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002219 break;
bellardcd6f1162004-05-13 22:02:20 +00002220 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002221 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002222 break;
j_mayer1192dad2007-10-05 13:08:35 +00002223 case QEMU_OPTION_bios:
2224 bios_name = optarg;
2225 break;
aurel321b530a62009-04-05 20:08:59 +00002226 case QEMU_OPTION_singlestep:
2227 singlestep = 1;
2228 break;
bellardcd6f1162004-05-13 22:02:20 +00002229 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002230 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002231 break;
bellard3d11d0e2004-12-12 16:56:30 +00002232 case QEMU_OPTION_k:
2233 keyboard_layout = optarg;
2234 break;
bellardee22c2f2004-06-03 12:49:50 +00002235 case QEMU_OPTION_localtime:
2236 rtc_utc = 0;
2237 break;
malc3893c122008-09-28 00:42:05 +00002238 case QEMU_OPTION_vga:
2239 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002240 break;
bellarde9b137c2004-06-21 16:46:10 +00002241 case QEMU_OPTION_g:
2242 {
2243 const char *p;
2244 int w, h, depth;
2245 p = optarg;
2246 w = strtol(p, (char **)&p, 10);
2247 if (w <= 0) {
2248 graphic_error:
2249 fprintf(stderr, "qemu: invalid resolution or depth\n");
2250 exit(1);
2251 }
2252 if (*p != 'x')
2253 goto graphic_error;
2254 p++;
2255 h = strtol(p, (char **)&p, 10);
2256 if (h <= 0)
2257 goto graphic_error;
2258 if (*p == 'x') {
2259 p++;
2260 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002261 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002262 depth != 24 && depth != 32)
2263 goto graphic_error;
2264 } else if (*p == '\0') {
2265 depth = graphic_depth;
2266 } else {
2267 goto graphic_error;
2268 }
ths3b46e622007-09-17 08:09:54 +00002269
bellarde9b137c2004-06-21 16:46:10 +00002270 graphic_width = w;
2271 graphic_height = h;
2272 graphic_depth = depth;
2273 }
2274 break;
ths20d8a3e2007-02-18 17:04:49 +00002275 case QEMU_OPTION_echr:
2276 {
2277 char *r;
2278 term_escape_char = strtol(optarg, &r, 0);
2279 if (r == optarg)
2280 printf("Bad argument to echr\n");
2281 break;
2282 }
bellard82c643f2004-07-14 17:28:13 +00002283 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002284 monitor_parse(optarg, "readline");
2285 default_monitor = 0;
2286 break;
2287 case QEMU_OPTION_qmp:
2288 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002289 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002290 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002291 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002292 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002293 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002294 exit(1);
2295 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002296 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002297 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002298 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002299 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002300 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002301 exit(1);
2302 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002303 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302304 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002305 olist = qemu_find_opts("fsdev");
2306 if (!olist) {
2307 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2308 exit(1);
2309 }
2310 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302311 if (!opts) {
2312 fprintf(stderr, "parse error: %s\n", optarg);
2313 exit(1);
2314 }
2315 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302316 case QEMU_OPTION_virtfs: {
2317 char *arg_fsdev = NULL;
2318 char *arg_9p = NULL;
2319 int len = 0;
2320
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002321 olist = qemu_find_opts("virtfs");
2322 if (!olist) {
2323 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2324 exit(1);
2325 }
2326 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302327 if (!opts) {
2328 fprintf(stderr, "parse error: %s\n", optarg);
2329 exit(1);
2330 }
2331
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002332 if (qemu_opt_get(opts, "fstype") == NULL ||
2333 qemu_opt_get(opts, "mount_tag") == NULL ||
2334 qemu_opt_get(opts, "path") == NULL ||
2335 qemu_opt_get(opts, "security_model") == NULL) {
2336 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +05302337 "security_model=[mapped|passthrough|none],"
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002338 "mnt_tag=tag.\n");
2339 exit(1);
2340 }
2341
2342 len = strlen(",id=,path=,security_model=");
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302343 len += strlen(qemu_opt_get(opts, "fstype"));
2344 len += strlen(qemu_opt_get(opts, "mount_tag"));
2345 len += strlen(qemu_opt_get(opts, "path"));
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002346 len += strlen(qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302347 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2348
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302349 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002350 "%s,id=%s,path=%s,security_model=%s",
2351 qemu_opt_get(opts, "fstype"),
2352 qemu_opt_get(opts, "mount_tag"),
2353 qemu_opt_get(opts, "path"),
2354 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302355
2356 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2357 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2358 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2359
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302360 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002361 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2362 qemu_opt_get(opts, "mount_tag"),
2363 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302364
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002365 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302366 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2367 exit(1);
2368 }
2369
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002370 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302371 fprintf(stderr, "parse error [device]: %s\n", optarg);
2372 exit(1);
2373 }
2374
2375 qemu_free(arg_fsdev);
2376 qemu_free(arg_9p);
2377 break;
2378 }
bellard82c643f2004-07-14 17:28:13 +00002379 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002380 add_device_config(DEV_SERIAL, optarg);
2381 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002382 if (strncmp(optarg, "mon:", 4) == 0) {
2383 default_monitor = 0;
2384 }
bellard82c643f2004-07-14 17:28:13 +00002385 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002386 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002387 if (watchdog) {
2388 fprintf(stderr,
2389 "qemu: only one watchdog option may be given\n");
2390 return 1;
2391 }
2392 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002393 break;
2394 case QEMU_OPTION_watchdog_action:
2395 if (select_watchdog_action(optarg) == -1) {
2396 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2397 exit(1);
2398 }
2399 break;
aliguori51ecf132009-01-15 20:06:40 +00002400 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002401 add_device_config(DEV_VIRTCON, optarg);
2402 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002403 if (strncmp(optarg, "mon:", 4) == 0) {
2404 default_monitor = 0;
2405 }
aliguori51ecf132009-01-15 20:06:40 +00002406 break;
bellard6508fe52005-01-15 12:02:56 +00002407 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002408 add_device_config(DEV_PARALLEL, optarg);
2409 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002410 if (strncmp(optarg, "mon:", 4) == 0) {
2411 default_monitor = 0;
2412 }
bellard6508fe52005-01-15 12:02:56 +00002413 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002414 case QEMU_OPTION_debugcon:
2415 add_device_config(DEV_DEBUGCON, optarg);
2416 break;
bellardd63d3072004-10-03 13:29:03 +00002417 case QEMU_OPTION_loadvm:
2418 loadvm = optarg;
2419 break;
2420 case QEMU_OPTION_full_screen:
2421 full_screen = 1;
2422 break;
ths667acca2006-12-11 02:08:05 +00002423#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002424 case QEMU_OPTION_no_frame:
2425 no_frame = 1;
2426 break;
ths3780e192007-06-21 21:08:02 +00002427 case QEMU_OPTION_alt_grab:
2428 alt_grab = 1;
2429 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002430 case QEMU_OPTION_ctrl_grab:
2431 ctrl_grab = 1;
2432 break;
ths667acca2006-12-11 02:08:05 +00002433 case QEMU_OPTION_no_quit:
2434 no_quit = 1;
2435 break;
aliguori7d957bd2009-01-15 22:14:11 +00002436 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002437 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002438 break;
ths667acca2006-12-11 02:08:05 +00002439#endif
bellardf7cce892004-12-08 22:21:25 +00002440 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002441 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002442 break;
bellarda09db212005-04-30 16:10:35 +00002443 case QEMU_OPTION_win2k_hack:
2444 win2k_install_hack = 1;
2445 break;
aliguori73822ec2009-01-15 20:11:34 +00002446 case QEMU_OPTION_rtc_td_hack:
2447 rtc_td_hack = 1;
2448 break;
aliguori8a92ea22009-02-27 20:12:36 +00002449 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002450 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002451 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002452 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002453 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002454 break;
aliguori7ba1e612008-11-05 16:04:33 +00002455 case QEMU_OPTION_enable_kvm:
2456 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00002457 break;
bellardbb36d472005-11-05 14:22:28 +00002458 case QEMU_OPTION_usb:
2459 usb_enabled = 1;
2460 break;
bellarda594cfb2005-11-06 16:13:29 +00002461 case QEMU_OPTION_usbdevice:
2462 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002463 add_device_config(DEV_USB, optarg);
2464 break;
2465 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002466 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002467 exit(1);
2468 }
bellarda594cfb2005-11-06 16:13:29 +00002469 break;
bellard6a00d602005-11-21 23:25:50 +00002470 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002471 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002472 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002473 fprintf(stderr, "Invalid number of CPUs\n");
2474 exit(1);
2475 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002476 if (max_cpus < smp_cpus) {
2477 fprintf(stderr, "maxcpus must be equal to or greater than "
2478 "smp\n");
2479 exit(1);
2480 }
2481 if (max_cpus > 255) {
2482 fprintf(stderr, "Unsupported number of maxcpus\n");
2483 exit(1);
2484 }
bellard6a00d602005-11-21 23:25:50 +00002485 break;
bellard24236862006-04-30 21:28:36 +00002486 case QEMU_OPTION_vnc:
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002487 display_remote++;
ths73fc9742006-12-22 02:09:07 +00002488 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00002489 break;
bellard6515b202006-05-03 22:02:44 +00002490 case QEMU_OPTION_no_acpi:
2491 acpi_enabled = 0;
2492 break;
aliguori16b29ae2008-12-17 23:28:44 +00002493 case QEMU_OPTION_no_hpet:
2494 no_hpet = 1;
2495 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002496 case QEMU_OPTION_balloon:
2497 if (balloon_parse(optarg) < 0) {
2498 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2499 exit(1);
2500 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002501 break;
bellardd1beab82006-10-02 19:44:22 +00002502 case QEMU_OPTION_no_reboot:
2503 no_reboot = 1;
2504 break;
aurel32b2f76162008-04-11 21:35:52 +00002505 case QEMU_OPTION_no_shutdown:
2506 no_shutdown = 1;
2507 break;
balrog9467cd42007-05-01 01:34:14 +00002508 case QEMU_OPTION_show_cursor:
2509 cursor_hide = 0;
2510 break;
blueswir18fcb1b92008-09-18 18:29:08 +00002511 case QEMU_OPTION_uuid:
2512 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2513 fprintf(stderr, "Fail to parse UUID string."
2514 " Wrong format.\n");
2515 exit(1);
2516 }
2517 break;
ths9ae02552007-01-05 17:39:04 +00002518 case QEMU_OPTION_option_rom:
2519 if (nb_option_roms >= MAX_OPTION_ROMS) {
2520 fprintf(stderr, "Too many option ROMs\n");
2521 exit(1);
2522 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02002523 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2524 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2525 option_rom[nb_option_roms].bootindex =
2526 qemu_opt_get_number(opts, "bootindex", -1);
2527 if (!option_rom[nb_option_roms].name) {
2528 fprintf(stderr, "Option ROM file is not specified\n");
2529 exit(1);
2530 }
ths9ae02552007-01-05 17:39:04 +00002531 nb_option_roms++;
2532 break;
pbrook8e716212007-01-20 17:12:09 +00002533 case QEMU_OPTION_semihosting:
2534 semihosting_enabled = 1;
2535 break;
thsc35734b2007-03-19 15:17:08 +00002536 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02002537 qemu_name = qemu_strdup(optarg);
2538 {
2539 char *p = strchr(qemu_name, ',');
2540 if (p != NULL) {
2541 *p++ = 0;
2542 if (strncmp(p, "process=", 8)) {
2543 fprintf(stderr, "Unknown subargument %s to -name", p);
2544 exit(1);
2545 }
2546 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02002547 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02002548 }
2549 }
thsc35734b2007-03-19 15:17:08 +00002550 break;
blueswir166508602007-05-01 14:16:52 +00002551 case QEMU_OPTION_prom_env:
2552 if (nb_prom_envs >= MAX_PROM_ENVS) {
2553 fprintf(stderr, "Too many prom variables\n");
2554 exit(1);
2555 }
2556 prom_envs[nb_prom_envs] = optarg;
2557 nb_prom_envs++;
2558 break;
balrog2b8f2d42007-07-27 22:08:46 +00002559 case QEMU_OPTION_old_param:
2560 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00002561 break;
thsf3dcfad2007-08-24 01:26:02 +00002562 case QEMU_OPTION_clock:
2563 configure_alarms(optarg);
2564 break;
bellard7e0af5d02007-11-07 16:24:33 +00002565 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002566 configure_rtc_date_offset(optarg, 1);
2567 break;
2568 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002569 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002570 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002571 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00002572 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002573 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00002574 break;
bellard26a5f132008-05-28 12:30:31 +00002575 case QEMU_OPTION_tb_size:
2576 tb_size = strtol(optarg, NULL, 0);
2577 if (tb_size < 0)
2578 tb_size = 0;
2579 break;
pbrook2e70f6e2008-06-29 01:03:05 +00002580 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002581 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00002582 break;
aliguori5bb79102008-10-13 03:12:02 +00002583 case QEMU_OPTION_incoming:
2584 incoming = optarg;
Amit Shah8e848652010-07-27 15:49:19 +05302585 incoming_expected = true;
aliguori5bb79102008-10-13 03:12:02 +00002586 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002587 case QEMU_OPTION_nodefaults:
2588 default_serial = 0;
2589 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002590 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002591 default_monitor = 0;
2592 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01002593 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002594 default_floppy = 0;
2595 default_cdrom = 0;
2596 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002597 break;
aliguorie37630c2009-04-22 15:19:10 +00002598 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00002599 if (!(xen_available())) {
2600 printf("Option %s not supported for this target\n", popt->name);
2601 exit(1);
2602 }
aliguorie37630c2009-04-22 15:19:10 +00002603 xen_domid = atoi(optarg);
2604 break;
2605 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00002606 if (!(xen_available())) {
2607 printf("Option %s not supported for this target\n", popt->name);
2608 exit(1);
2609 }
aliguorie37630c2009-04-22 15:19:10 +00002610 xen_mode = XEN_CREATE;
2611 break;
2612 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00002613 if (!(xen_available())) {
2614 printf("Option %s not supported for this target\n", popt->name);
2615 exit(1);
2616 }
aliguorie37630c2009-04-22 15:19:10 +00002617 xen_mode = XEN_ATTACH;
2618 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002619#ifdef CONFIG_SIMPLE_TRACE
2620 case QEMU_OPTION_trace:
2621 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2622 if (opts) {
2623 trace_file = qemu_opt_get(opts, "file");
2624 }
2625 break;
2626#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002627 case QEMU_OPTION_readconfig:
2628 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002629 int ret = qemu_read_config_file(optarg);
2630 if (ret < 0) {
2631 fprintf(stderr, "read config %s: %s\n", optarg,
2632 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002633 exit(1);
2634 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002635 break;
2636 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03002637 case QEMU_OPTION_spice:
2638 olist = qemu_find_opts("spice");
2639 if (!olist) {
2640 fprintf(stderr, "spice is not supported by this qemu build.\n");
2641 exit(1);
2642 }
2643 opts = qemu_opts_parse(olist, optarg, 0);
2644 if (!opts) {
2645 fprintf(stderr, "parse error: %s\n", optarg);
2646 exit(1);
2647 }
2648 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002649 case QEMU_OPTION_writeconfig:
2650 {
2651 FILE *fp;
2652 if (strcmp(optarg, "-") == 0) {
2653 fp = stdout;
2654 } else {
2655 fp = fopen(optarg, "w");
2656 if (fp == NULL) {
2657 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2658 exit(1);
2659 }
2660 }
2661 qemu_config_write(fp);
2662 fclose(fp);
2663 break;
2664 }
Jes Sorensen59a52642010-06-10 11:42:25 +02002665 default:
2666 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00002667 }
bellard0824d6f2003-06-24 13:42:40 +00002668 }
2669 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002670 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00002671
Paul Brook5cea8592009-05-30 00:52:44 +01002672 /* If no data_dir is specified then try to find it relative to the
2673 executable path. */
2674 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02002675 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01002676 }
2677 /* If all else fails use the install patch specified when building. */
2678 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02002679 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01002680 }
2681
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002682#ifdef CONFIG_SIMPLE_TRACE
2683 /*
2684 * Set the trace file name, if specified.
2685 */
2686 st_set_trace_file(trace_file);
2687#endif
Jes Sorensen6be68d72009-07-23 17:03:42 +02002688 /*
2689 * Default to max_cpus = smp_cpus, in case the user doesn't
2690 * specify a max_cpus value.
2691 */
2692 if (!max_cpus)
2693 max_cpus = smp_cpus;
2694
balrog3d878ca2008-10-28 10:59:59 +00002695 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00002696 if (smp_cpus > machine->max_cpus) {
2697 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2698 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2699 machine->max_cpus);
2700 exit(1);
2701 }
2702
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002703 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2704 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002705
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002706 if (machine->no_serial) {
2707 default_serial = 0;
2708 }
2709 if (machine->no_parallel) {
2710 default_parallel = 0;
2711 }
2712 if (!machine->use_virtcon) {
2713 default_virtcon = 0;
2714 }
2715 if (machine->no_vga) {
2716 default_vga = 0;
2717 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002718 if (machine->no_floppy) {
2719 default_floppy = 0;
2720 }
2721 if (machine->no_cdrom) {
2722 default_cdrom = 0;
2723 }
2724 if (machine->no_sdcard) {
2725 default_sdcard = 0;
2726 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002727
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002728 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002729 if (default_parallel)
2730 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002731 if (default_serial && default_monitor) {
2732 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002733 } else if (default_virtcon && default_monitor) {
2734 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002735 } else {
2736 if (default_serial)
2737 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002738 if (default_virtcon)
2739 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002740 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002741 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002742 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002743 } else {
2744 if (default_serial)
2745 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002746 if (default_parallel)
2747 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01002748 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002749 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01002750 if (default_virtcon)
2751 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00002752 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01002753 if (default_vga)
2754 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00002755
TeLeMana5829fd2010-04-15 12:37:55 +08002756 socket_init();
2757
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002758 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002759 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002760#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002761 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302762 exit(1);
2763 }
2764#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002765
Jes Sorenseneb505be2010-06-10 11:42:28 +02002766 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00002767
thsaa26bb22007-03-25 21:33:06 +00002768 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02002769 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00002770 exit(1);
2771 }
2772
Paolo Bonzini98c85732010-04-19 18:59:30 +00002773 if (kvm_allowed) {
2774 int ret = kvm_init(smp_cpus);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002775 if (ret < 0) {
Paolo Bonzini98c85732010-04-19 18:59:30 +00002776 if (!kvm_available()) {
2777 printf("KVM not supported for this target\n");
2778 } else {
2779 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2780 }
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002781 exit(1);
2782 }
2783 }
2784
aliguori3fcf7b62009-04-24 18:03:25 +00002785 if (qemu_init_main_loop()) {
2786 fprintf(stderr, "qemu_init_main_loop failed\n");
2787 exit(1);
2788 }
bellarda20dd502003-09-30 21:07:02 +00002789 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00002790
thsf8d39c02008-07-03 10:01:15 +00002791 if (!linux_boot && *kernel_cmdline != '\0') {
2792 fprintf(stderr, "-append only allowed with -kernel option\n");
2793 exit(1);
2794 }
2795
2796 if (!linux_boot && initrd_filename != NULL) {
2797 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2798 exit(1);
2799 }
2800
Jes Sorensen9156d762010-06-10 11:42:30 +02002801 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00002802
aliguori7183b4b2008-11-05 20:40:18 +00002803 if (init_timer_alarm() < 0) {
2804 fprintf(stderr, "could not initialize alarm timer\n");
2805 exit(1);
2806 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002807 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00002808
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01002809 if (net_init_clients() < 0) {
2810 exit(1);
bellard702c6512004-04-02 21:21:32 +00002811 }
bellardf1510b22003-06-25 00:07:40 +00002812
balrogdc72ac12008-11-09 00:04:26 +00002813 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002814 if (foreach_device_config(DEV_BT, bt_parse))
2815 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00002816
bellard0824d6f2003-06-24 13:42:40 +00002817 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00002818 if (ram_size == 0)
2819 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00002820
bellard26a5f132008-05-28 12:30:31 +00002821 /* init the dynamic translator */
2822 cpu_exec_init_all(tb_size * 1024 * 1024);
2823
Markus Armbrustereb852012009-10-27 18:41:44 +01002824 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00002825
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002826 blk_mig_init();
2827
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002828 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002829 /* we always create the cdrom drive, even if no disk is there */
2830 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002831 }
thse4bcb142007-12-02 04:51:10 +00002832
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002833 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002834 /* we always create at least one floppy */
2835 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002836 }
bellardc4b1fcc2004-03-14 21:44:30 +00002837
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002838 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002839 /* we always create one sd slot, even if no card is in it */
2840 drive_add(NULL, SD_ALIAS);
2841 }
balrog9d413d12007-12-04 00:10:34 +00002842
ths96d30e42007-01-07 20:42:14 +00002843 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002844 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002845 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2846 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002847 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00002848
Alex Williamson97ab12d2010-06-25 11:09:50 -06002849 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002850 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00002851
aliguori268a3622009-04-21 22:30:27 +00002852 if (nb_numa_nodes > 0) {
2853 int i;
2854
2855 if (nb_numa_nodes > smp_cpus) {
2856 nb_numa_nodes = smp_cpus;
2857 }
2858
2859 /* If no memory size if given for any node, assume the default case
2860 * and distribute the available memory equally across all nodes
2861 */
2862 for (i = 0; i < nb_numa_nodes; i++) {
2863 if (node_mem[i] != 0)
2864 break;
2865 }
2866 if (i == nb_numa_nodes) {
2867 uint64_t usedmem = 0;
2868
2869 /* On Linux, the each node's border has to be 8MB aligned,
2870 * the final node gets the rest.
2871 */
2872 for (i = 0; i < nb_numa_nodes - 1; i++) {
2873 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2874 usedmem += node_mem[i];
2875 }
2876 node_mem[i] = ram_size - usedmem;
2877 }
2878
2879 for (i = 0; i < nb_numa_nodes; i++) {
2880 if (node_cpumask[i] != 0)
2881 break;
2882 }
2883 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2884 * must cope with this anyway, because there are BIOSes out there in
2885 * real machines which also use this scheme.
2886 */
2887 if (i == nb_numa_nodes) {
2888 for (i = 0; i < smp_cpus; i++) {
2889 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2890 }
2891 }
2892 }
2893
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002894 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02002895 exit(1);
2896 }
2897
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002898 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2899 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002900 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2901 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002902 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2903 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002904 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2905 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00002906
Paul Brookaae94602009-05-14 22:35:06 +01002907 module_call_init(MODULE_INIT_DEVICE);
2908
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002909 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002910 exit(0);
2911
Markus Armbruster09aaa162009-08-21 10:31:34 +02002912 if (watchdog) {
2913 i = select_watchdog(watchdog);
2914 if (i > 0)
2915 exit (i == 1 ? 1 : 0);
2916 }
2917
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002918 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01002919 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002920 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002921 qemu_add_globals();
2922
Paul Brookfbe1b592009-05-13 17:56:25 +01002923 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00002924 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2925
Jan Kiszkaea375f92010-03-01 19:10:30 +01002926 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00002927
Juan Quintela67b3b712009-08-28 19:25:15 +02002928 /* must be after terminal init, SDL library changes signal handlers */
Jes Sorensen8d963e62010-06-10 11:42:22 +02002929 os_setup_signal_handling();
Juan Quintela67b3b712009-08-28 19:25:15 +02002930
Blue Swirl87d0a282010-03-27 18:24:45 +00002931 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00002932
aliguori6f338c32009-02-11 15:21:54 +00002933 current_machine = machine;
2934
aliguori3023f332009-01-16 19:04:14 +00002935 /* init USB devices */
2936 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01002937 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2938 exit(1);
aliguori3023f332009-01-16 19:04:14 +00002939 }
2940
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002941 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002942 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002943 exit(1);
2944
Markus Armbruster668680f2010-02-11 14:44:58 +01002945 net_check_clients();
2946
aliguori3023f332009-01-16 19:04:14 +00002947 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01002948 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002949
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02002950 if (using_spice)
2951 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002952 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002953#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002954 display_type = DT_SDL;
2955#else
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002956 vnc_display = "localhost:0,to=99";
2957 show_vnc_port = 1;
2958#endif
2959 }
2960
2961
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002962 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002963 switch (display_type) {
2964 case DT_NOGRAPHIC:
2965 break;
2966#if defined(CONFIG_CURSES)
2967 case DT_CURSES:
2968 curses_display_init(ds, full_screen);
2969 break;
2970#endif
bellard5b0753e2005-03-01 21:37:28 +00002971#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002972 case DT_SDL:
2973 sdl_display_init(ds, full_screen, no_frame);
2974 break;
bellard5b0753e2005-03-01 21:37:28 +00002975#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002976 case DT_SDL:
2977 cocoa_display_init(ds, full_screen);
2978 break;
bellard313aa562003-08-10 21:52:11 +00002979#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002980 default:
2981 break;
2982 }
2983
2984 /* init remote displays */
2985 if (vnc_display) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002986 vnc_display_init(ds);
2987 if (vnc_display_open(ds, vnc_display) < 0)
2988 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002989
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002990 if (show_vnc_port) {
2991 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002992 }
bellard313aa562003-08-10 21:52:11 +00002993 }
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02002994#ifdef CONFIG_SPICE
2995 if (using_spice) {
2996 qemu_spice_display_init(ds);
2997 }
2998#endif
aliguori5b08fc12008-08-21 20:08:03 +00002999
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003000 /* display setup */
3001 dpy_resize(ds);
aliguori3023f332009-01-16 19:04:14 +00003002 dcl = ds->listeners;
3003 while (dcl != NULL) {
3004 if (dcl->dpy_refresh != NULL) {
3005 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3006 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09003007 break;
ths20d8a3e2007-02-18 17:04:49 +00003008 }
aliguori3023f332009-01-16 19:04:14 +00003009 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00003010 }
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003011 if (ds->gui_timer == NULL) {
blueswir19043b622009-01-21 19:28:13 +00003012 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3013 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3014 }
Paolo Bonzinib473df62010-02-11 00:29:55 +01003015 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003016
aliguori59030a82009-04-05 18:43:41 +00003017 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3018 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3019 gdbstub_dev);
3020 exit(1);
balrog45669e02007-07-02 13:20:17 +00003021 }
balrog45669e02007-07-02 13:20:17 +00003022
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003023 qdev_machine_creation_done();
3024
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003025 if (rom_load_all() != 0) {
3026 fprintf(stderr, "rom loading failed\n");
3027 exit(1);
3028 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003029
Anthony Liguoriec990eb2010-11-19 18:55:59 +09003030 qemu_register_reset((void *)qbus_reset_all, sysbus_get_default());
Juan Quintela504c2942009-11-12 00:39:13 +01003031 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02003032 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003033 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003034 autostart = 0;
3035 }
3036 }
bellardd63d3072004-10-03 13:29:03 +00003037
Glauber Costa2bb8c102009-07-24 16:20:23 -04003038 if (incoming) {
Juan Quintela8ca5e802010-06-09 14:10:54 +02003039 int ret = qemu_start_incoming_migration(incoming);
3040 if (ret < 0) {
3041 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3042 incoming, ret);
3043 exit(ret);
3044 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03003045 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003046 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003047 }
thsffd843b2006-12-21 19:46:43 +00003048
Jes Sorenseneb505be2010-06-10 11:42:28 +02003049 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003050
bellard8a7ddc32004-03-31 19:00:16 +00003051 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003052 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003053 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00003054
bellard0824d6f2003-06-24 13:42:40 +00003055 return 0;
3056}