blob: 27d9392f773f7c54309447bb86202ee0f6c55de8 [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>
Blue Swirl8d32cf02009-10-02 19:32:12 +000083/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
84 discussion about Solaris header problems */
85extern int madvise(caddr_t, size_t, int);
bellard67b915a2004-03-31 23:37:16 +000086#endif
bellard7d3505c2004-05-12 19:32:15 +000087#endif
bellardec530c82006-04-25 22:36:06 +000088#endif
bellard67b915a2004-03-31 23:37:16 +000089
blueswir19892fbf2008-08-24 10:34:20 +000090#if defined(__OpenBSD__)
91#include <util.h>
92#endif
93
ths8a16d272008-07-19 09:56:24 +000094#if defined(CONFIG_VDE)
95#include <libvdeplug.h>
96#endif
97
bellard67b915a2004-03-31 23:37:16 +000098#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000099#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +0000100#endif
101
bellard73332e52004-04-04 20:22:28 +0000102#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200103#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200104#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000105int qemu_main(int argc, char **argv, char **envp);
106int main(int argc, char **argv)
107{
Stefan Weil59a36a22009-06-18 20:11:03 +0200108 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000109}
110#undef main
111#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000112#endif
bellard73332e52004-04-04 20:22:28 +0000113#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000114
bellard5b0753e2005-03-01 21:37:28 +0000115#ifdef CONFIG_COCOA
116#undef main
117#define main qemu_main
118#endif /* CONFIG_COCOA */
119
blueswir1511d2b12009-03-07 15:32:56 +0000120#include "hw/hw.h"
121#include "hw/boards.h"
122#include "hw/usb.h"
123#include "hw/pcmcia.h"
124#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000125#include "hw/isa.h"
126#include "hw/baum.h"
127#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100128#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000129#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000130#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200131#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200132#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000133#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000134#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000135#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000136#include "monitor.h"
137#include "console.h"
138#include "sysemu.h"
139#include "gdbstub.h"
140#include "qemu-timer.h"
141#include "qemu-char.h"
142#include "cache-utils.h"
143#include "block.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200144#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200145#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000146#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000147#include "audio/audio.h"
148#include "migration.h"
149#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200150#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200151#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200152#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200153#include "qemu-options.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700154#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530155#include "fsdev/qemu-fsdev.h"
156#endif
blueswir1511d2b12009-03-07 15:32:56 +0000157
bellard0824d6f2003-06-24 13:42:40 +0000158#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000159
blueswir1511d2b12009-03-07 15:32:56 +0000160#include "qemu_socket.h"
161
Jan Kiszkad918f232009-06-24 14:42:30 +0200162#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000163
Blue Swirl72cf2d42009-09-12 07:36:22 +0000164#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000165#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000166#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000167
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300168#include "ui/qemu-spice.h"
169
blueswir19dc63a12008-10-04 07:25:46 +0000170//#define DEBUG_NET
171//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000172
bellard1bfe8562004-07-08 21:17:50 +0000173#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000174
Amit Shah98b19252010-01-20 00:36:52 +0530175#define MAX_VIRTIO_CONSOLES 1
176
Paul Brook5cea8592009-05-30 00:52:44 +0100177static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000178const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000179enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500180DisplayType display_type = DT_DEFAULT;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +0200181int display_remote = 0;
bellard3d11d0e2004-12-12 16:56:30 +0000182const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500183ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300184const char *mem_path = NULL;
185#ifdef MAP_POPULATE
186int mem_prealloc = 0; /* force preallocation of physical target memory */
187#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000188int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000189NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000190int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200191int autostart;
Amit Shah8e848652010-07-27 15:49:19 +0530192int incoming_expected; /* Started with -incoming and waiting for incoming */
balrogf6503052008-02-17 11:42:19 +0000193static int rtc_utc = 1;
194static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200195QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100196int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000197static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000198#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000199static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000200#endif
ths667acca2006-12-11 02:08:05 +0000201int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000202CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000203CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000204CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000205int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000206int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000207int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000208int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000209int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200210int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200211int smp_cores = 1;
212int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000213const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000214int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000215int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000216int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000217int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000218int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000219int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000220int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200221uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200222const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000223const char *option_rom[MAX_OPTION_ROMS];
224int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000225int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000226int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000227const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000228int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500229int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000230unsigned int nb_prom_envs = 0;
231const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200232int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000233
aliguori268a3622009-04-21 22:30:27 +0000234int nb_numa_nodes;
235uint64_t node_mem[MAX_NODES];
236uint64_t node_cpumask[MAX_NODES];
237
blueswir19043b622009-01-21 19:28:13 +0000238static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000239
blueswir18fcb1b92008-09-18 18:29:08 +0000240uint8_t qemu_uuid[16];
241
Jan Kiszka76e30d02009-07-02 00:19:02 +0200242static QEMUBootSetHandler *boot_set_handler;
243static void *boot_set_opaque;
244
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200245static NotifierList exit_notifiers =
246 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
247
Blue Swirld745bef2010-03-29 19:23:49 +0000248int kvm_allowed = 0;
249uint32_t xen_domid;
250enum xen_mode xen_mode = XEN_EMULATE;
251
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100252static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100253static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100254static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100255static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100256static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100257static int default_floppy = 1;
258static int default_cdrom = 1;
259static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100260
261static struct {
262 const char *driver;
263 int *flag;
264} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100265 { .driver = "isa-serial", .flag = &default_serial },
266 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100267 { .driver = "isa-fdc", .flag = &default_floppy },
268 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530269 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
270 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
271 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100272 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100273 { .driver = "cirrus-vga", .flag = &default_vga },
274 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100275};
276
277static int default_driver_check(QemuOpts *opts, void *opaque)
278{
279 const char *driver = qemu_opt_get(opts, "driver");
280 int i;
281
282 if (!driver)
283 return 0;
284 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
285 if (strcmp(default_list[i].driver, driver) != 0)
286 continue;
287 *(default_list[i].flag) = 0;
288 }
289 return 0;
290}
291
bellard0824d6f2003-06-24 13:42:40 +0000292/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100293/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000294
bellard87858c82003-06-27 12:01:39 +0000295/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000296uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000297{
298 union {
299 uint64_t ll;
300 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200301#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000302 uint32_t high, low;
303#else
304 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000305#endif
bellard87858c82003-06-27 12:01:39 +0000306 } l;
307 } u, res;
308 uint64_t rl, rh;
309
310 u.ll = a;
311 rl = (uint64_t)u.l.low * (uint64_t)b;
312 rh = (uint64_t)u.l.high * (uint64_t)b;
313 rh += (rl >> 32);
314 res.l.high = rh / c;
315 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
316 return res.ll;
317}
318
bellardc4b1fcc2004-03-14 21:44:30 +0000319/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000320/* host time/date access */
321void qemu_get_timedate(struct tm *tm, int offset)
322{
323 time_t ti;
324 struct tm *ret;
325
326 time(&ti);
327 ti += offset;
328 if (rtc_date_offset == -1) {
329 if (rtc_utc)
330 ret = gmtime(&ti);
331 else
332 ret = localtime(&ti);
333 } else {
334 ti -= rtc_date_offset;
335 ret = gmtime(&ti);
336 }
337
338 memcpy(tm, ret, sizeof(struct tm));
339}
340
341int qemu_timedate_diff(struct tm *tm)
342{
343 time_t seconds;
344
345 if (rtc_date_offset == -1)
346 if (rtc_utc)
347 seconds = mktimegm(tm);
348 else
349 seconds = mktime(tm);
350 else
351 seconds = mktimegm(tm) + rtc_date_offset;
352
353 return seconds - time(NULL);
354}
355
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300356void rtc_change_mon_event(struct tm *tm)
357{
358 QObject *data;
359
360 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
361 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
362 qobject_decref(data);
363}
364
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200365static void configure_rtc_date_offset(const char *startdate, int legacy)
366{
367 time_t rtc_start_date;
368 struct tm tm;
369
370 if (!strcmp(startdate, "now") && legacy) {
371 rtc_date_offset = -1;
372 } else {
373 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
374 &tm.tm_year,
375 &tm.tm_mon,
376 &tm.tm_mday,
377 &tm.tm_hour,
378 &tm.tm_min,
379 &tm.tm_sec) == 6) {
380 /* OK */
381 } else if (sscanf(startdate, "%d-%d-%d",
382 &tm.tm_year,
383 &tm.tm_mon,
384 &tm.tm_mday) == 3) {
385 tm.tm_hour = 0;
386 tm.tm_min = 0;
387 tm.tm_sec = 0;
388 } else {
389 goto date_fail;
390 }
391 tm.tm_year -= 1900;
392 tm.tm_mon--;
393 rtc_start_date = mktimegm(&tm);
394 if (rtc_start_date == -1) {
395 date_fail:
396 fprintf(stderr, "Invalid date format. Valid formats are:\n"
397 "'2006-06-17T16:01:21' or '2006-06-17'\n");
398 exit(1);
399 }
400 rtc_date_offset = time(NULL) - rtc_start_date;
401 }
402}
403
404static void configure_rtc(QemuOpts *opts)
405{
406 const char *value;
407
408 value = qemu_opt_get(opts, "base");
409 if (value) {
410 if (!strcmp(value, "utc")) {
411 rtc_utc = 1;
412 } else if (!strcmp(value, "localtime")) {
413 rtc_utc = 0;
414 } else {
415 configure_rtc_date_offset(value, 0);
416 }
417 }
Jan Kiszka68752042009-09-15 13:36:04 +0200418 value = qemu_opt_get(opts, "clock");
419 if (value) {
420 if (!strcmp(value, "host")) {
421 rtc_clock = host_clock;
422 } else if (!strcmp(value, "vm")) {
423 rtc_clock = vm_clock;
424 } else {
425 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
426 exit(1);
427 }
428 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200429 value = qemu_opt_get(opts, "driftfix");
430 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000431 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200432 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000433 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200434 rtc_td_hack = 0;
435 } else {
436 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
437 exit(1);
438 }
439 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200440}
441
balrog1ae26a12008-09-28 23:19:47 +0000442/***********************************************************/
443/* Bluetooth support */
444static int nb_hcis;
445static int cur_hci;
446static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000447
balrog1ae26a12008-09-28 23:19:47 +0000448static struct bt_vlan_s {
449 struct bt_scatternet_s net;
450 int id;
451 struct bt_vlan_s *next;
452} *first_bt_vlan;
453
454/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000455static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000456{
457 struct bt_vlan_s **pvlan, *vlan;
458 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
459 if (vlan->id == id)
460 return &vlan->net;
461 }
462 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
463 vlan->id = id;
464 pvlan = &first_bt_vlan;
465 while (*pvlan != NULL)
466 pvlan = &(*pvlan)->next;
467 *pvlan = vlan;
468 return &vlan->net;
469}
470
471static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
472{
473}
474
475static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
476{
477 return -ENOTSUP;
478}
479
480static struct HCIInfo null_hci = {
481 .cmd_send = null_hci_send,
482 .sco_send = null_hci_send,
483 .acl_send = null_hci_send,
484 .bdaddr_set = null_hci_addr_set,
485};
486
487struct HCIInfo *qemu_next_hci(void)
488{
489 if (cur_hci == nb_hcis)
490 return &null_hci;
491
492 return hci_table[cur_hci++];
493}
494
balrogdc72ac12008-11-09 00:04:26 +0000495static struct HCIInfo *hci_init(const char *str)
496{
497 char *endp;
498 struct bt_scatternet_s *vlan = 0;
499
500 if (!strcmp(str, "null"))
501 /* null */
502 return &null_hci;
503 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
504 /* host[:hciN] */
505 return bt_host_hci(str[4] ? str + 5 : "hci0");
506 else if (!strncmp(str, "hci", 3)) {
507 /* hci[,vlan=n] */
508 if (str[3]) {
509 if (!strncmp(str + 3, ",vlan=", 6)) {
510 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
511 if (*endp)
512 vlan = 0;
513 }
514 } else
515 vlan = qemu_find_bt_vlan(0);
516 if (vlan)
517 return bt_new_hci(vlan);
518 }
519
520 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
521
522 return 0;
523}
524
525static int bt_hci_parse(const char *str)
526{
527 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500528 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000529
530 if (nb_hcis >= MAX_NICS) {
531 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
532 return -1;
533 }
534
535 hci = hci_init(str);
536 if (!hci)
537 return -1;
538
539 bdaddr.b[0] = 0x52;
540 bdaddr.b[1] = 0x54;
541 bdaddr.b[2] = 0x00;
542 bdaddr.b[3] = 0x12;
543 bdaddr.b[4] = 0x34;
544 bdaddr.b[5] = 0x56 + nb_hcis;
545 hci->bdaddr_set(hci, bdaddr.b);
546
547 hci_table[nb_hcis++] = hci;
548
549 return 0;
550}
551
552static void bt_vhci_add(int vlan_id)
553{
554 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
555
556 if (!vlan->slave)
557 fprintf(stderr, "qemu: warning: adding a VHCI to "
558 "an empty scatternet %i\n", vlan_id);
559
560 bt_vhci_init(bt_new_hci(vlan));
561}
562
563static struct bt_device_s *bt_device_add(const char *opt)
564{
565 struct bt_scatternet_s *vlan;
566 int vlan_id = 0;
567 char *endp = strstr(opt, ",vlan=");
568 int len = (endp ? endp - opt : strlen(opt)) + 1;
569 char devname[10];
570
571 pstrcpy(devname, MIN(sizeof(devname), len), opt);
572
573 if (endp) {
574 vlan_id = strtol(endp + 6, &endp, 0);
575 if (*endp) {
576 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
577 return 0;
578 }
579 }
580
581 vlan = qemu_find_bt_vlan(vlan_id);
582
583 if (!vlan->slave)
584 fprintf(stderr, "qemu: warning: adding a slave device to "
585 "an empty scatternet %i\n", vlan_id);
586
587 if (!strcmp(devname, "keyboard"))
588 return bt_keyboard_init(vlan);
589
590 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
591 return 0;
592}
593
594static int bt_parse(const char *opt)
595{
596 const char *endp, *p;
597 int vlan;
598
599 if (strstart(opt, "hci", &endp)) {
600 if (!*endp || *endp == ',') {
601 if (*endp)
602 if (!strstart(endp, ",vlan=", 0))
603 opt = endp + 1;
604
605 return bt_hci_parse(opt);
606 }
607 } else if (strstart(opt, "vhci", &endp)) {
608 if (!*endp || *endp == ',') {
609 if (*endp) {
610 if (strstart(endp, ",vlan=", &p)) {
611 vlan = strtol(p, (char **) &endp, 0);
612 if (*endp) {
613 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
614 return 1;
615 }
616 } else {
617 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
618 return 1;
619 }
620 } else
621 vlan = 0;
622
623 bt_vhci_add(vlan);
624 return 0;
625 }
626 } else if (strstart(opt, "device:", &endp))
627 return !bt_device_add(endp);
628
629 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
630 return 1;
631}
632
balrog1ae26a12008-09-28 23:19:47 +0000633/***********************************************************/
634/* QEMU Block devices */
635
balrog609497a2008-01-14 02:56:53 +0000636#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +0000637#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +0000638#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +0000639#define PFLASH_ALIAS "if=pflash"
640#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +0000641#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +0000642
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200643static int drive_init_func(QemuOpts *opts, void *opaque)
644{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200645 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200646 int fatal_error = 0;
647
Markus Armbrustera803cb82010-06-02 13:31:55 +0200648 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200649 if (fatal_error)
650 return 1;
651 }
652 return 0;
653}
654
655static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
656{
657 if (NULL == qemu_opt_get(opts, "snapshot")) {
658 qemu_opt_set(opts, "snapshot", "on");
659 }
660 return 0;
661}
662
Jan Kiszka76e30d02009-07-02 00:19:02 +0200663void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
664{
665 boot_set_handler = func;
666 boot_set_opaque = opaque;
667}
668
669int qemu_boot_set(const char *boot_devices)
670{
671 if (!boot_set_handler) {
672 return -EINVAL;
673 }
674 return boot_set_handler(boot_set_opaque, boot_devices);
675}
676
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300677static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200678{
679 /* We just do some generic consistency checks */
680 const char *p;
681 int bitmap = 0;
682
683 for (p = devices; *p != '\0'; p++) {
684 /* Allowed boot devices are:
685 * a-b: floppy disk drives
686 * c-f: IDE disk drives
687 * g-m: machine implementation dependant drives
688 * n-p: network devices
689 * It's up to each machine implementation to check if the given boot
690 * devices match the actual hardware implementation and firmware
691 * features.
692 */
693 if (*p < 'a' || *p > 'p') {
694 fprintf(stderr, "Invalid boot device '%c'\n", *p);
695 exit(1);
696 }
697 if (bitmap & (1 << (*p - 'a'))) {
698 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
699 exit(1);
700 }
701 bitmap |= 1 << (*p - 'a');
702 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200703}
704
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200705static void restore_boot_devices(void *opaque)
706{
707 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600708 static int first = 1;
709
710 /* Restore boot order and remove ourselves after the first boot */
711 if (first) {
712 first = 0;
713 return;
714 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200715
716 qemu_boot_set(standard_boot_devices);
717
718 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
719 qemu_free(standard_boot_devices);
720}
721
aliguori268a3622009-04-21 22:30:27 +0000722static void numa_add(const char *optarg)
723{
724 char option[128];
725 char *endptr;
726 unsigned long long value, endvalue;
727 int nodenr;
728
729 optarg = get_opt_name(option, 128, optarg, ',') + 1;
730 if (!strcmp(option, "node")) {
731 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
732 nodenr = nb_numa_nodes;
733 } else {
734 nodenr = strtoull(option, NULL, 10);
735 }
736
737 if (get_param_value(option, 128, "mem", optarg) == 0) {
738 node_mem[nodenr] = 0;
739 } else {
740 value = strtoull(option, &endptr, 0);
741 switch (*endptr) {
742 case 0: case 'M': case 'm':
743 value <<= 20;
744 break;
745 case 'G': case 'g':
746 value <<= 30;
747 break;
748 }
749 node_mem[nodenr] = value;
750 }
751 if (get_param_value(option, 128, "cpus", optarg) == 0) {
752 node_cpumask[nodenr] = 0;
753 } else {
754 value = strtoull(option, &endptr, 10);
755 if (value >= 64) {
756 value = 63;
757 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
758 } else {
759 if (*endptr == '-') {
760 endvalue = strtoull(endptr+1, &endptr, 10);
761 if (endvalue >= 63) {
762 endvalue = 62;
763 fprintf(stderr,
764 "only 63 CPUs in NUMA mode supported.\n");
765 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100766 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000767 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100768 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000769 }
770 }
771 node_cpumask[nodenr] = value;
772 }
773 nb_numa_nodes++;
774 }
775 return;
776}
777
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200778static void smp_parse(const char *optarg)
779{
780 int smp, sockets = 0, threads = 0, cores = 0;
781 char *endptr;
782 char option[128];
783
784 smp = strtoul(optarg, &endptr, 10);
785 if (endptr != optarg) {
786 if (*endptr == ',') {
787 endptr++;
788 }
789 }
790 if (get_param_value(option, 128, "sockets", endptr) != 0)
791 sockets = strtoull(option, NULL, 10);
792 if (get_param_value(option, 128, "cores", endptr) != 0)
793 cores = strtoull(option, NULL, 10);
794 if (get_param_value(option, 128, "threads", endptr) != 0)
795 threads = strtoull(option, NULL, 10);
796 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
797 max_cpus = strtoull(option, NULL, 10);
798
799 /* compute missing values, prefer sockets over cores over threads */
800 if (smp == 0 || sockets == 0) {
801 sockets = sockets > 0 ? sockets : 1;
802 cores = cores > 0 ? cores : 1;
803 threads = threads > 0 ? threads : 1;
804 if (smp == 0) {
805 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200806 }
807 } else {
808 if (cores == 0) {
809 threads = threads > 0 ? threads : 1;
810 cores = smp / (sockets * threads);
811 } else {
Joel Schoppdca98162010-07-21 15:05:17 -0500812 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200813 }
814 }
815 smp_cpus = smp;
816 smp_cores = cores > 0 ? cores : 1;
817 smp_threads = threads > 0 ? threads : 1;
818 if (max_cpus == 0)
819 max_cpus = smp_cpus;
820}
821
bellard330d0412003-07-26 18:11:40 +0000822/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +0000823/* USB devices */
824
Markus Armbrusterfb080002010-05-28 15:38:44 +0200825static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +0000826{
827 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200828 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +0000829
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200830 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +0000831 return -1;
832
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +0100833 /* drivers with .usbdevice_name entry in USBDeviceInfo */
834 dev = usbdevice_create(devname);
835 if (dev)
836 goto done;
837
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200838 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +0000839 if (strstart(devname, "host:", &p)) {
840 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +0000841 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
842 dev = usb_bt_init(devname[2] ? hci_init(p) :
843 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +0000844 } else {
845 return -1;
846 }
pbrook0d92ed32006-05-21 16:30:15 +0000847 if (!dev)
848 return -1;
849
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200850done:
bellarda594cfb2005-11-06 16:13:29 +0000851 return 0;
852}
853
aliguori1f3870a2008-08-21 19:27:48 +0000854static int usb_device_del(const char *devname)
855{
856 int bus_num, addr;
857 const char *p;
858
aliguori5d0c5752008-09-14 01:07:41 +0000859 if (strstart(devname, "host:", &p))
860 return usb_host_device_close(p);
861
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200862 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +0000863 return -1;
864
865 p = strchr(devname, '.');
866 if (!p)
867 return -1;
868 bus_num = strtoul(devname, NULL, 0);
869 addr = strtoul(p + 1, NULL, 0);
870
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200871 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000872}
873
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200874static int usb_parse(const char *cmdline)
875{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800876 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +0200877 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800878 if (r < 0) {
879 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
880 }
881 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200882}
883
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300884void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000885{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800886 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +0200887 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100888 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800889 }
bellarda594cfb2005-11-06 16:13:29 +0000890}
891
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300892void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000893{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800894 const char *devname = qdict_get_str(qdict, "devname");
895 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100896 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800897 }
bellarda594cfb2005-11-06 16:13:29 +0000898}
899
bellardf7cce892004-12-08 22:21:25 +0000900/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +0000901/* PCMCIA/Cardbus */
902
903static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +0100904 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +0000905 struct pcmcia_socket_entry_s *next;
906} *pcmcia_sockets = 0;
907
Paul Brookbc24a222009-05-10 01:44:56 +0100908void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000909{
910 struct pcmcia_socket_entry_s *entry;
911
912 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
913 entry->socket = socket;
914 entry->next = pcmcia_sockets;
915 pcmcia_sockets = entry;
916}
917
Paul Brookbc24a222009-05-10 01:44:56 +0100918void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000919{
920 struct pcmcia_socket_entry_s *entry, **ptr;
921
922 ptr = &pcmcia_sockets;
923 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
924 if (entry->socket == socket) {
925 *ptr = entry->next;
926 qemu_free(entry);
927 }
928}
929
aliguori376253e2009-03-05 23:01:23 +0000930void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +0000931{
932 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +0000933
balrog201a51f2007-04-30 00:51:09 +0000934 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +0000935 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +0000936
937 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +0000938 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
939 iter->socket->attached ? iter->socket->card_string :
940 "Empty");
balrog201a51f2007-04-30 00:51:09 +0000941}
942
943/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +0000944/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +0000945
bellardc4b1fcc2004-03-14 21:44:30 +0000946typedef struct IOHandlerRecord {
947 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +0100948 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +0000949 IOHandler *fd_read;
950 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +0000951 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +0000952 void *opaque;
953 /* temporary data */
954 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +0100955 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +0000956} IOHandlerRecord;
957
Juan Quintela31d4ee62010-03-11 17:55:37 +0100958static QLIST_HEAD(, IOHandlerRecord) io_handlers =
959 QLIST_HEAD_INITIALIZER(io_handlers);
960
bellardc4b1fcc2004-03-14 21:44:30 +0000961
bellard7c9d8e02005-11-15 22:16:05 +0000962/* XXX: fd_read_poll should be suppressed, but an API change is
963 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +0000964int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +0100965 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +0000966 IOHandler *fd_read,
967 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +0000968 void *opaque)
bellardb4608c02003-06-27 17:34:32 +0000969{
Juan Quintela31d4ee62010-03-11 17:55:37 +0100970 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +0000971
bellard7c9d8e02005-11-15 22:16:05 +0000972 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100973 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000974 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +0000975 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +0000976 break;
977 }
bellard8a7ddc32004-03-31 19:00:16 +0000978 }
bellard7c9d8e02005-11-15 22:16:05 +0000979 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100980 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000981 if (ioh->fd == fd)
982 goto found;
983 }
984 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +0100985 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +0000986 found:
987 ioh->fd = fd;
988 ioh->fd_read_poll = fd_read_poll;
989 ioh->fd_read = fd_read;
990 ioh->fd_write = fd_write;
991 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +0000992 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +0000993 }
bellard7c9d8e02005-11-15 22:16:05 +0000994 return 0;
995}
996
ths5fafdf22007-09-16 21:08:06 +0000997int qemu_set_fd_handler(int fd,
998 IOHandler *fd_read,
999 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001000 void *opaque)
1001{
1002 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00001003}
1004
bellard8a7ddc32004-03-31 19:00:16 +00001005/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001006/* machine registration */
1007
blueswir1bdaf78e2008-10-04 07:24:27 +00001008static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001009QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001010
1011int qemu_register_machine(QEMUMachine *m)
1012{
1013 QEMUMachine **pm;
1014 pm = &first_machine;
1015 while (*pm != NULL)
1016 pm = &(*pm)->next;
1017 m->next = NULL;
1018 *pm = m;
1019 return 0;
1020}
1021
pbrook9596ebb2007-11-18 01:44:38 +00001022static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001023{
1024 QEMUMachine *m;
1025
1026 for(m = first_machine; m != NULL; m = m->next) {
1027 if (!strcmp(m->name, name))
1028 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001029 if (m->alias && !strcmp(m->alias, name))
1030 return m;
bellardcc1daa42005-06-05 14:49:17 +00001031 }
1032 return NULL;
1033}
1034
Anthony Liguori0c257432009-05-21 20:41:01 -05001035static QEMUMachine *find_default_machine(void)
1036{
1037 QEMUMachine *m;
1038
1039 for(m = first_machine; m != NULL; m = m->next) {
1040 if (m->is_default) {
1041 return m;
1042 }
1043 }
1044 return NULL;
1045}
1046
bellardcc1daa42005-06-05 14:49:17 +00001047/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001048/* main execution loop */
1049
pbrook9596ebb2007-11-18 01:44:38 +00001050static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001051{
aliguori7d957bd2009-01-15 22:14:11 +00001052 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001053 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001054 DisplayChangeListener *dcl = ds->listeners;
1055
Sheng Yang62a27442010-01-26 19:21:16 +08001056 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001057 dpy_refresh(ds);
1058
1059 while (dcl != NULL) {
1060 if (dcl->gui_timer_interval &&
1061 dcl->gui_timer_interval < interval)
1062 interval = dcl->gui_timer_interval;
1063 dcl = dcl->next;
1064 }
1065 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001066}
1067
blueswir19043b622009-01-21 19:28:13 +00001068static void nographic_update(void *opaque)
1069{
1070 uint64_t interval = GUI_REFRESH_INTERVAL;
1071
Sheng Yang62a27442010-01-26 19:21:16 +08001072 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00001073 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1074}
1075
bellard0bd48852005-11-11 00:00:47 +00001076struct vm_change_state_entry {
1077 VMChangeStateHandler *cb;
1078 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001079 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001080};
1081
Blue Swirl72cf2d42009-09-12 07:36:22 +00001082static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001083
1084VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1085 void *opaque)
1086{
1087 VMChangeStateEntry *e;
1088
1089 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001090
1091 e->cb = cb;
1092 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001093 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001094 return e;
1095}
1096
1097void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1098{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001099 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001100 qemu_free (e);
1101}
1102
Blue Swirl296af7c2010-03-29 19:23:50 +00001103void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001104{
1105 VMChangeStateEntry *e;
1106
1107 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001108 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001109 }
1110}
1111
bellard8a7ddc32004-03-31 19:00:16 +00001112void vm_start(void)
1113{
1114 if (!vm_running) {
1115 cpu_enable_ticks();
1116 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001117 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001118 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001119 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001120 }
1121}
1122
bellardbb0c6722004-06-20 12:37:32 +00001123/* reset/shutdown handler */
1124
1125typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001126 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001127 QEMUResetHandler *func;
1128 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001129} QEMUResetEntry;
1130
Blue Swirl72cf2d42009-09-12 07:36:22 +00001131static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1132 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001133static int reset_requested;
1134static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00001135static int powerdown_requested;
Blue Swirl296af7c2010-03-29 19:23:50 +00001136int debug_requested;
Blue Swirl7277e022010-04-12 17:19:06 +00001137int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001138
aurel32cf7a2fe2008-03-18 06:53:05 +00001139int qemu_shutdown_requested(void)
1140{
1141 int r = shutdown_requested;
1142 shutdown_requested = 0;
1143 return r;
1144}
1145
1146int qemu_reset_requested(void)
1147{
1148 int r = reset_requested;
1149 reset_requested = 0;
1150 return r;
1151}
1152
1153int qemu_powerdown_requested(void)
1154{
1155 int r = powerdown_requested;
1156 powerdown_requested = 0;
1157 return r;
1158}
1159
aliguorie5689022009-04-24 18:03:54 +00001160static int qemu_debug_requested(void)
1161{
1162 int r = debug_requested;
1163 debug_requested = 0;
1164 return r;
1165}
1166
aliguori6e29f5d2009-04-24 18:04:02 +00001167static int qemu_vmstop_requested(void)
1168{
1169 int r = vmstop_requested;
1170 vmstop_requested = 0;
1171 return r;
1172}
1173
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001174void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001175{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001176 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001177
bellardbb0c6722004-06-20 12:37:32 +00001178 re->func = func;
1179 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001180 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001181}
1182
Jan Kiszkadda9b292009-07-02 00:19:02 +02001183void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001184{
1185 QEMUResetEntry *re;
1186
Blue Swirl72cf2d42009-09-12 07:36:22 +00001187 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001188 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001189 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001190 qemu_free(re);
1191 return;
1192 }
1193 }
1194}
1195
1196void qemu_system_reset(void)
1197{
1198 QEMUResetEntry *re, *nre;
1199
1200 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001201 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001202 re->func(re->opaque);
1203 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001204 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001205 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001206}
1207
1208void qemu_system_reset_request(void)
1209{
bellardd1beab82006-10-02 19:44:22 +00001210 if (no_reboot) {
1211 shutdown_requested = 1;
1212 } else {
1213 reset_requested = 1;
1214 }
aliguorid9f75a42009-04-24 18:03:11 +00001215 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001216}
1217
1218void qemu_system_shutdown_request(void)
1219{
1220 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001221 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001222}
1223
bellard34751872005-07-02 14:31:34 +00001224void qemu_system_powerdown_request(void)
1225{
1226 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001227 qemu_notify_event();
1228}
1229
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001230void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001231{
1232 IOHandlerRecord *ioh;
1233 fd_set rfds, wfds, xfds;
1234 int ret, nfds;
1235 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001236 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001237
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001238 if (nonblocking)
1239 timeout = 0;
1240 else {
1241 timeout = qemu_calculate_timeout();
1242 qemu_bh_update_timeout(&timeout);
1243 }
aliguori56f3a5d2008-10-31 18:07:17 +00001244
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001245 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001246
bellardfd1dff42006-02-01 21:29:26 +00001247 /* poll any events */
1248 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001249 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001250 FD_ZERO(&rfds);
1251 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001252 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001253 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00001254 if (ioh->deleted)
1255 continue;
bellardfd1dff42006-02-01 21:29:26 +00001256 if (ioh->fd_read &&
1257 (!ioh->fd_read_poll ||
1258 ioh->fd_read_poll(ioh->opaque) != 0)) {
1259 FD_SET(ioh->fd, &rfds);
1260 if (ioh->fd > nfds)
1261 nfds = ioh->fd;
1262 }
1263 if (ioh->fd_write) {
1264 FD_SET(ioh->fd, &wfds);
1265 if (ioh->fd > nfds)
1266 nfds = ioh->fd;
1267 }
1268 }
ths3b46e622007-09-17 08:09:54 +00001269
aliguori56f3a5d2008-10-31 18:07:17 +00001270 tv.tv_sec = timeout / 1000;
1271 tv.tv_usec = (timeout % 1000) * 1000;
1272
Jan Kiszkad918f232009-06-24 14:42:30 +02001273 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1274
aliguori48708522009-04-24 18:03:49 +00001275 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001276 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001277 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00001278 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001279 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00001280
Juan Quintela31d4ee62010-03-11 17:55:37 +01001281 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
thscafffd42007-02-28 21:59:44 +00001282 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001283 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00001284 qemu_free(ioh);
Juan Quintela4bed9832010-03-11 17:55:41 +01001285 continue;
1286 }
1287 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1288 ioh->fd_read(ioh->opaque);
1289 }
1290 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1291 ioh->fd_write(ioh->opaque);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001292 }
thscafffd42007-02-28 21:59:44 +00001293 }
bellardfd1dff42006-02-01 21:29:26 +00001294 }
Jan Kiszkad918f232009-06-24 14:42:30 +02001295
1296 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001297
Paolo Bonzinib6964822010-03-10 11:38:45 +01001298 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001299
pbrook423f0742007-05-23 00:06:54 +00001300 /* Check bottom-halves last in case any of the earlier events triggered
1301 them. */
1302 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001303
bellard5905b2e2004-08-01 21:53:26 +00001304}
1305
aliguori43b96852009-04-24 18:03:33 +00001306static int vm_can_run(void)
1307{
1308 if (powerdown_requested)
1309 return 0;
1310 if (reset_requested)
1311 return 0;
1312 if (shutdown_requested)
1313 return 0;
aliguorie5689022009-04-24 18:03:54 +00001314 if (debug_requested)
1315 return 0;
aliguori43b96852009-04-24 18:03:33 +00001316 return 1;
1317}
1318
Blue Swirld9c32312009-08-09 08:42:19 +00001319qemu_irq qemu_system_powerdown;
1320
aliguori43b96852009-04-24 18:03:33 +00001321static void main_loop(void)
1322{
aliguori6e29f5d2009-04-24 18:04:02 +00001323 int r;
aliguori43b96852009-04-24 18:03:33 +00001324
Blue Swirl7277e022010-04-12 17:19:06 +00001325 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001326
aliguori6e29f5d2009-04-24 18:04:02 +00001327 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00001328 do {
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001329 bool nonblocking = false;
aliguorie6e35b12009-04-24 18:03:57 +00001330#ifdef CONFIG_PROFILER
1331 int64_t ti;
1332#endif
aliguorid6dc3d42009-04-24 18:04:07 +00001333#ifndef CONFIG_IOTHREAD
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001334 nonblocking = cpu_exec_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001335#endif
aliguori43b96852009-04-24 18:03:33 +00001336#ifdef CONFIG_PROFILER
1337 ti = profile_getclock();
1338#endif
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001339 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001340#ifdef CONFIG_PROFILER
1341 dev_time += profile_getclock() - ti;
1342#endif
aliguorie5689022009-04-24 18:03:54 +00001343 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00001344
Blue Swirl54fc6ea2010-03-29 19:23:46 +00001345 if ((r = qemu_debug_requested())) {
1346 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001347 }
aliguori43b96852009-04-24 18:03:33 +00001348 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001349 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001350 if (no_shutdown) {
1351 vm_stop(0);
1352 no_shutdown = 0;
1353 } else
1354 break;
1355 }
aliguorid6dc3d42009-04-24 18:04:07 +00001356 if (qemu_reset_requested()) {
1357 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00001358 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00001359 resume_all_vcpus();
1360 }
Blue Swirld9c32312009-08-09 08:42:19 +00001361 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001362 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001363 qemu_irq_raise(qemu_system_powerdown);
1364 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001365 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001366 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001367 }
aliguori43b96852009-04-24 18:03:33 +00001368 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001369 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001370 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001371}
1372
pbrook9bd7e6d2009-04-07 22:58:45 +00001373static void version(void)
1374{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001375 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001376}
1377
ths15f82202007-06-29 23:26:08 +00001378static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001379{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001380 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001381#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1382 opt_help
blueswir15824d652009-03-28 06:44:27 +00001383#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001384#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001385#undef DEF
1386#undef DEFHEADING
1387#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001388 ;
1389 version();
1390 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001391 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001392 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001393 "\n"
1394 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001395 "During emulation, the following keys are useful:\n"
1396 "ctrl-alt-f toggle full screen\n"
1397 "ctrl-alt-n switch to virtual console 'n'\n"
1398 "ctrl-alt toggle mouse and keyboard grab\n"
1399 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001400 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1401 "qemu",
1402 options_help);
ths15f82202007-06-29 23:26:08 +00001403 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001404}
1405
bellardcd6f1162004-05-13 22:02:20 +00001406#define HAS_ARG 0x0001
1407
bellardcd6f1162004-05-13 22:02:20 +00001408typedef struct QEMUOption {
1409 const char *name;
1410 int flags;
1411 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001412 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001413} QEMUOption;
1414
blueswir1dbed7e42008-10-01 19:38:09 +00001415static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001416 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1417#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1418 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001419#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001420#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001421#undef DEF
1422#undef DEFHEADING
1423#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001424 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001425};
malc3893c122008-09-28 00:42:05 +00001426static void select_vgahw (const char *p)
1427{
1428 const char *opts;
1429
Gerd Hoffmann64465292009-12-08 13:11:45 +01001430 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001431 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001432 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001433 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001434 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001435 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001436 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001437 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001438 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001439 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00001440 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001441 invalid_vga:
1442 fprintf(stderr, "Unknown vga type: %s\n", p);
1443 exit(1);
1444 }
malccb5a7aa2008-09-28 00:42:12 +00001445 while (*opts) {
1446 const char *nextopt;
1447
1448 if (strstart(opts, ",retrace=", &nextopt)) {
1449 opts = nextopt;
1450 if (strstart(opts, "dumb", &nextopt))
1451 vga_retrace_method = VGA_RETRACE_DUMB;
1452 else if (strstart(opts, "precise", &nextopt))
1453 vga_retrace_method = VGA_RETRACE_PRECISE;
1454 else goto invalid_vga;
1455 } else goto invalid_vga;
1456 opts = nextopt;
1457 }
malc3893c122008-09-28 00:42:05 +00001458}
1459
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001460static int balloon_parse(const char *arg)
1461{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001462 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001463
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001464 if (strcmp(arg, "none") == 0) {
1465 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001466 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001467
1468 if (!strncmp(arg, "virtio", 6)) {
1469 if (arg[6] == ',') {
1470 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001471 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001472 if (!opts)
1473 return -1;
1474 } else {
1475 /* create empty opts */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001476 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001477 }
1478 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1479 return 0;
1480 }
1481
1482 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001483}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001484
Paul Brook5cea8592009-05-30 00:52:44 +01001485char *qemu_find_file(int type, const char *name)
1486{
1487 int len;
1488 const char *subdir;
1489 char *buf;
1490
1491 /* If name contains path separators then try it as a straight path. */
1492 if ((strchr(name, '/') || strchr(name, '\\'))
1493 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02001494 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001495 }
1496 switch (type) {
1497 case QEMU_FILE_TYPE_BIOS:
1498 subdir = "";
1499 break;
1500 case QEMU_FILE_TYPE_KEYMAP:
1501 subdir = "keymaps/";
1502 break;
1503 default:
1504 abort();
1505 }
1506 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1507 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001508 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001509 if (access(buf, R_OK)) {
1510 qemu_free(buf);
1511 return NULL;
1512 }
1513 return buf;
1514}
1515
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001516static int device_help_func(QemuOpts *opts, void *opaque)
1517{
1518 return qdev_device_help(opts);
1519}
1520
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001521static int device_init_func(QemuOpts *opts, void *opaque)
1522{
1523 DeviceState *dev;
1524
1525 dev = qdev_device_add(opts);
1526 if (!dev)
1527 return -1;
1528 return 0;
1529}
1530
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001531static int chardev_init_func(QemuOpts *opts, void *opaque)
1532{
1533 CharDriverState *chr;
1534
1535 chr = qemu_chr_open_opts(opts, NULL);
1536 if (!chr)
1537 return -1;
1538 return 0;
1539}
1540
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07001541#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301542static int fsdev_init_func(QemuOpts *opts, void *opaque)
1543{
1544 int ret;
1545 ret = qemu_fsdev_add(opts);
1546
1547 return ret;
1548}
1549#endif
1550
Gerd Hoffmann88589342009-12-08 13:11:50 +01001551static int mon_init_func(QemuOpts *opts, void *opaque)
1552{
1553 CharDriverState *chr;
1554 const char *chardev;
1555 const char *mode;
1556 int flags;
1557
1558 mode = qemu_opt_get(opts, "mode");
1559 if (mode == NULL) {
1560 mode = "readline";
1561 }
1562 if (strcmp(mode, "readline") == 0) {
1563 flags = MONITOR_USE_READLINE;
1564 } else if (strcmp(mode, "control") == 0) {
1565 flags = MONITOR_USE_CONTROL;
1566 } else {
1567 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1568 exit(1);
1569 }
1570
1571 if (qemu_opt_get_bool(opts, "default", 0))
1572 flags |= MONITOR_IS_DEFAULT;
1573
1574 chardev = qemu_opt_get(opts, "chardev");
1575 chr = qemu_chr_find(chardev);
1576 if (chr == NULL) {
1577 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1578 exit(1);
1579 }
1580
1581 monitor_init(chr, flags);
1582 return 0;
1583}
1584
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001585static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001586{
1587 static int monitor_device_index = 0;
1588 QemuOpts *opts;
1589 const char *p;
1590 char label[32];
1591 int def = 0;
1592
1593 if (strstart(optarg, "chardev:", &p)) {
1594 snprintf(label, sizeof(label), "%s", p);
1595 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001596 snprintf(label, sizeof(label), "compat_monitor%d",
1597 monitor_device_index);
1598 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001599 def = 1;
1600 }
1601 opts = qemu_chr_parse_compat(label, optarg);
1602 if (!opts) {
1603 fprintf(stderr, "parse error: %s\n", optarg);
1604 exit(1);
1605 }
1606 }
1607
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001608 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001609 if (!opts) {
1610 fprintf(stderr, "duplicate chardev: %s\n", label);
1611 exit(1);
1612 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001613 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001614 qemu_opt_set(opts, "chardev", label);
1615 if (def)
1616 qemu_opt_set(opts, "default", "on");
1617 monitor_device_index++;
1618}
1619
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001620struct device_config {
1621 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001622 DEV_USB, /* -usbdevice */
1623 DEV_BT, /* -bt */
1624 DEV_SERIAL, /* -serial */
1625 DEV_PARALLEL, /* -parallel */
1626 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001627 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001628 } type;
1629 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001630 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001631};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001632QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001633
1634static void add_device_config(int type, const char *cmdline)
1635{
1636 struct device_config *conf;
1637
1638 conf = qemu_mallocz(sizeof(*conf));
1639 conf->type = type;
1640 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001641 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001642}
1643
1644static int foreach_device_config(int type, int (*func)(const char *cmdline))
1645{
1646 struct device_config *conf;
1647 int rc;
1648
Blue Swirl72cf2d42009-09-12 07:36:22 +00001649 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001650 if (conf->type != type)
1651 continue;
1652 rc = func(conf->cmdline);
1653 if (0 != rc)
1654 return rc;
1655 }
1656 return 0;
1657}
1658
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001659static int serial_parse(const char *devname)
1660{
1661 static int index = 0;
1662 char label[32];
1663
1664 if (strcmp(devname, "none") == 0)
1665 return 0;
1666 if (index == MAX_SERIAL_PORTS) {
1667 fprintf(stderr, "qemu: too many serial ports\n");
1668 exit(1);
1669 }
1670 snprintf(label, sizeof(label), "serial%d", index);
1671 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1672 if (!serial_hds[index]) {
1673 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1674 devname, strerror(errno));
1675 return -1;
1676 }
1677 index++;
1678 return 0;
1679}
1680
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01001681static int parallel_parse(const char *devname)
1682{
1683 static int index = 0;
1684 char label[32];
1685
1686 if (strcmp(devname, "none") == 0)
1687 return 0;
1688 if (index == MAX_PARALLEL_PORTS) {
1689 fprintf(stderr, "qemu: too many parallel ports\n");
1690 exit(1);
1691 }
1692 snprintf(label, sizeof(label), "parallel%d", index);
1693 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1694 if (!parallel_hds[index]) {
1695 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1696 devname, strerror(errno));
1697 return -1;
1698 }
1699 index++;
1700 return 0;
1701}
1702
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001703static int virtcon_parse(const char *devname)
1704{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001705 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001706 static int index = 0;
1707 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05301708 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001709
1710 if (strcmp(devname, "none") == 0)
1711 return 0;
1712 if (index == MAX_VIRTIO_CONSOLES) {
1713 fprintf(stderr, "qemu: too many virtio consoles\n");
1714 exit(1);
1715 }
Amit Shah392ecf52010-01-21 16:19:23 +05301716
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001717 bus_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301718 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1719
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001720 dev_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301721 qemu_opt_set(dev_opts, "driver", "virtconsole");
1722
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001723 snprintf(label, sizeof(label), "virtcon%d", index);
1724 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1725 if (!virtcon_hds[index]) {
1726 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1727 devname, strerror(errno));
1728 return -1;
1729 }
Amit Shah392ecf52010-01-21 16:19:23 +05301730 qemu_opt_set(dev_opts, "chardev", label);
1731
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001732 index++;
1733 return 0;
1734}
1735
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001736static int debugcon_parse(const char *devname)
1737{
1738 QemuOpts *opts;
1739
1740 if (!qemu_chr_open("debugcon", devname, NULL)) {
1741 exit(1);
1742 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001743 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001744 if (!opts) {
1745 fprintf(stderr, "qemu: already have a debugcon device\n");
1746 exit(1);
1747 }
1748 qemu_opt_set(opts, "driver", "isa-debugcon");
1749 qemu_opt_set(opts, "chardev", "debugcon");
1750 return 0;
1751}
1752
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001753void qemu_add_exit_notifier(Notifier *notify)
1754{
1755 notifier_list_add(&exit_notifiers, notify);
1756}
1757
1758void qemu_remove_exit_notifier(Notifier *notify)
1759{
1760 notifier_list_remove(&exit_notifiers, notify);
1761}
1762
1763static void qemu_run_exit_notifiers(void)
1764{
1765 notifier_list_notify(&exit_notifiers);
1766}
1767
Anthony Liguori6530a972010-01-22 09:18:06 -06001768static const QEMUOption *lookup_opt(int argc, char **argv,
1769 const char **poptarg, int *poptind)
1770{
1771 const QEMUOption *popt;
1772 int optind = *poptind;
1773 char *r = argv[optind];
1774 const char *optarg;
1775
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001776 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06001777 optind++;
1778 /* Treat --foo the same as -foo. */
1779 if (r[1] == '-')
1780 r++;
1781 popt = qemu_options;
1782 for(;;) {
1783 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001784 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06001785 exit(1);
1786 }
1787 if (!strcmp(popt->name, r + 1))
1788 break;
1789 popt++;
1790 }
1791 if (popt->flags & HAS_ARG) {
1792 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001793 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06001794 exit(1);
1795 }
1796 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001797 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06001798 } else {
1799 optarg = NULL;
1800 }
1801
1802 *poptarg = optarg;
1803 *poptind = optind;
1804
1805 return popt;
1806}
1807
malc902b3d52008-12-10 19:18:40 +00001808int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00001809{
aliguori59030a82009-04-05 18:43:41 +00001810 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00001811 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03001812 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01001813 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00001814 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00001815 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05001816 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00001817 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00001818 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00001819 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001820 QemuOpts *hda_opts = NULL, *opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02001821 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00001822 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06001823 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00001824 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001825 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00001826 const char *cpu_model;
bellard26a5f132008-05-28 12:30:31 +00001827 int tb_size;
ths93815bc2007-03-19 15:58:31 +00001828 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00001829 const char *incoming = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05001830 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06001831 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00001832
Prerna Saxenaab6540d2010-08-09 11:48:32 +01001833#ifdef CONFIG_SIMPLE_TRACE
1834 const char *trace_file = NULL;
1835#endif
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001836 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01001837 error_set_progname(argv[0]);
1838
Jan Kiszka68752042009-09-15 13:36:04 +02001839 init_clocks();
1840
malc902b3d52008-12-10 19:18:40 +00001841 qemu_cache_utils_init(envp);
1842
Blue Swirl72cf2d42009-09-12 07:36:22 +00001843 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02001844 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00001845
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05001846 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05001847 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00001848 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00001849 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00001850 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00001851 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00001852 kernel_filename = NULL;
1853 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00001854 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00001855 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00001856
aliguori268a3622009-04-21 22:30:27 +00001857 for (i = 0; i < MAX_NODES; i++) {
1858 node_mem[i] = 0;
1859 node_cpumask[i] = 0;
1860 }
1861
aliguori268a3622009-04-21 22:30:27 +00001862 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00001863 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00001864
bellard26a5f132008-05-28 12:30:31 +00001865 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00001866 autostart= 1;
1867
Anthony Liguori292444c2010-01-21 10:57:58 -06001868 /* first pass of option parsing */
1869 optind = 1;
1870 while (optind < argc) {
1871 if (argv[optind][0] != '-') {
1872 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06001873 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06001874 continue;
1875 } else {
1876 const QEMUOption *popt;
1877
1878 popt = lookup_opt(argc, argv, &optarg, &optind);
1879 switch (popt->index) {
1880 case QEMU_OPTION_nodefconfig:
1881 defconfig=0;
1882 break;
1883 }
1884 }
1885 }
1886
1887 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001888 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01001889
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001890 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02001891 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001892 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001893 }
1894
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001895 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02001896 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001897 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001898 }
1899 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00001900 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06001901
1902 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00001903 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00001904 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00001905 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00001906 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06001907 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001908 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00001909 } else {
1910 const QEMUOption *popt;
1911
Anthony Liguori6530a972010-01-22 09:18:06 -06001912 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00001913 if (!(popt->arch_mask & arch_type)) {
1914 printf("Option %s not supported for this target\n", popt->name);
1915 exit(1);
1916 }
bellardcd6f1162004-05-13 22:02:20 +00001917 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00001918 case QEMU_OPTION_M:
1919 machine = find_machine(optarg);
1920 if (!machine) {
1921 QEMUMachine *m;
1922 printf("Supported machines are:\n");
1923 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001924 if (m->alias)
1925 printf("%-10s %s (alias of %s)\n",
1926 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00001927 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00001928 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05001929 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00001930 }
ths15f82202007-06-29 23:26:08 +00001931 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00001932 }
1933 break;
j_mayer94fc95c2007-03-05 19:44:02 +00001934 case QEMU_OPTION_cpu:
1935 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00001936 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00001937 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00001938 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00001939 } else {
1940 cpu_model = optarg;
1941 }
1942 break;
bellardcd6f1162004-05-13 22:02:20 +00001943 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00001944 initrd_filename = optarg;
1945 break;
bellardcd6f1162004-05-13 22:02:20 +00001946 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00001947 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001948 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00001949 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001950 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00001951 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00001952 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00001953 translation == BIOS_ATA_TRANSLATION_LBA ?
1954 ",trans=lba" :
1955 translation == BIOS_ATA_TRANSLATION_NONE ?
1956 ",trans=none" : "");
1957 break;
bellardcd6f1162004-05-13 22:02:20 +00001958 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00001959 case QEMU_OPTION_hdc:
1960 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00001961 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00001962 break;
thse4bcb142007-12-02 04:51:10 +00001963 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00001964 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00001965 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02001966 case QEMU_OPTION_set:
1967 if (qemu_set_option(optarg) != 0)
1968 exit(1);
1969 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01001970 case QEMU_OPTION_global:
1971 if (qemu_global_option(optarg) != 0)
1972 exit(1);
1973 break;
balrog3e3d5812007-04-30 02:09:25 +00001974 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00001975 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00001976 break;
pbrooka1bb27b2007-04-06 16:49:48 +00001977 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00001978 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00001979 break;
j_mayer86f55662007-04-24 06:52:59 +00001980 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00001981 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00001982 break;
bellardcd6f1162004-05-13 22:02:20 +00001983 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00001984 snapshot = 1;
1985 break;
bellardcd6f1162004-05-13 22:02:20 +00001986 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00001987 {
bellard330d0412003-07-26 18:11:40 +00001988 const char *p;
1989 p = optarg;
1990 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001991 if (cyls < 1 || cyls > 16383)
1992 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00001993 if (*p != ',')
1994 goto chs_fail;
1995 p++;
1996 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001997 if (heads < 1 || heads > 16)
1998 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00001999 if (*p != ',')
2000 goto chs_fail;
2001 p++;
2002 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002003 if (secs < 1 || secs > 63)
2004 goto chs_fail;
2005 if (*p == ',') {
2006 p++;
2007 if (!strcmp(p, "none"))
2008 translation = BIOS_ATA_TRANSLATION_NONE;
2009 else if (!strcmp(p, "lba"))
2010 translation = BIOS_ATA_TRANSLATION_LBA;
2011 else if (!strcmp(p, "auto"))
2012 translation = BIOS_ATA_TRANSLATION_AUTO;
2013 else
2014 goto chs_fail;
2015 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002016 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002017 fprintf(stderr, "qemu: invalid physical CHS format\n");
2018 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002019 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002020 if (hda_opts != NULL) {
2021 char num[16];
2022 snprintf(num, sizeof(num), "%d", cyls);
2023 qemu_opt_set(hda_opts, "cyls", num);
2024 snprintf(num, sizeof(num), "%d", heads);
2025 qemu_opt_set(hda_opts, "heads", num);
2026 snprintf(num, sizeof(num), "%d", secs);
2027 qemu_opt_set(hda_opts, "secs", num);
2028 if (translation == BIOS_ATA_TRANSLATION_LBA)
2029 qemu_opt_set(hda_opts, "trans", "lba");
2030 if (translation == BIOS_ATA_TRANSLATION_NONE)
2031 qemu_opt_set(hda_opts, "trans", "none");
2032 }
bellard330d0412003-07-26 18:11:40 +00002033 }
2034 break;
aliguori268a3622009-04-21 22:30:27 +00002035 case QEMU_OPTION_numa:
2036 if (nb_numa_nodes >= MAX_NODES) {
2037 fprintf(stderr, "qemu: too many NUMA nodes\n");
2038 exit(1);
2039 }
2040 numa_add(optarg);
2041 break;
bellardcd6f1162004-05-13 22:02:20 +00002042 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002043 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002044 break;
balrog4d3b6f62008-02-10 16:33:14 +00002045#ifdef CONFIG_CURSES
2046 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002047 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00002048 break;
2049#endif
balroga171fe32007-04-30 01:48:07 +00002050 case QEMU_OPTION_portrait:
2051 graphic_rotate = 1;
2052 break;
bellardcd6f1162004-05-13 22:02:20 +00002053 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002054 kernel_filename = optarg;
2055 break;
bellardcd6f1162004-05-13 22:02:20 +00002056 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002057 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002058 break;
bellardcd6f1162004-05-13 22:02:20 +00002059 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00002060 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00002061 break;
bellardcd6f1162004-05-13 22:02:20 +00002062 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002063 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002064 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002065 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002066 };
2067 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002068 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002069 int legacy = 0;
2070
2071 if (!strchr(optarg, '=')) {
2072 legacy = 1;
2073 pstrcpy(buf, sizeof(buf), optarg);
2074 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2075 fprintf(stderr,
2076 "qemu: unknown boot parameter '%s' in '%s'\n",
2077 buf, optarg);
2078 exit(1);
2079 }
2080
2081 if (legacy ||
2082 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002083 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002084 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002085 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002086 if (!legacy) {
2087 if (get_param_value(buf, sizeof(buf),
2088 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002089 validate_bootdevices(buf);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002090 standard_boot_devices = qemu_strdup(boot_devices);
2091 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2092 qemu_register_reset(restore_boot_devices,
2093 standard_boot_devices);
2094 }
Jan Kiszka95387492009-07-02 00:19:02 +02002095 if (get_param_value(buf, sizeof(buf),
2096 "menu", optarg)) {
2097 if (!strcmp(buf, "on")) {
2098 boot_menu = 1;
2099 } else if (!strcmp(buf, "off")) {
2100 boot_menu = 0;
2101 } else {
2102 fprintf(stderr,
2103 "qemu: invalid option value '%s'\n",
2104 buf);
2105 exit(1);
2106 }
2107 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002108 }
bellard36b486b2003-11-11 13:36:08 +00002109 }
2110 break;
bellardcd6f1162004-05-13 22:02:20 +00002111 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002112 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00002113 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00002114 break;
bellard52ca8d62006-06-14 16:03:05 +00002115 case QEMU_OPTION_no_fd_bootchk:
2116 fd_bootchk = 0;
2117 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002118 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002119 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002120 exit(1);
2121 }
2122 break;
bellard7c9d8e02005-11-15 22:16:05 +00002123 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002124 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002125 exit(1);
2126 }
bellard702c6512004-04-02 21:21:32 +00002127 break;
bellardc7f74642004-08-24 21:57:12 +00002128#ifdef CONFIG_SLIRP
2129 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002130 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002131 break;
ths47d5d012007-02-20 00:05:08 +00002132 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002133 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002134 break;
bellard9bf05442004-08-25 22:12:49 +00002135 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002136 if (net_slirp_redir(optarg) < 0)
2137 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002138 break;
bellardc7f74642004-08-24 21:57:12 +00002139#endif
balrogdc72ac12008-11-09 00:04:26 +00002140 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002141 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002142 break;
bellard1d14ffa2005-10-30 18:58:22 +00002143 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002144 if (!(audio_available())) {
2145 printf("Option %s not supported for this target\n", popt->name);
2146 exit(1);
2147 }
bellard1d14ffa2005-10-30 18:58:22 +00002148 AUD_help ();
2149 exit (0);
2150 break;
2151 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002152 if (!(audio_available())) {
2153 printf("Option %s not supported for this target\n", popt->name);
2154 exit(1);
2155 }
bellard1d14ffa2005-10-30 18:58:22 +00002156 select_soundhw (optarg);
2157 break;
bellardcd6f1162004-05-13 22:02:20 +00002158 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002159 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002160 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002161 case QEMU_OPTION_version:
2162 version();
2163 exit(0);
2164 break;
aurel3200f82b82008-04-27 21:12:55 +00002165 case QEMU_OPTION_m: {
2166 uint64_t value;
2167 char *ptr;
2168
2169 value = strtoul(optarg, &ptr, 10);
2170 switch (*ptr) {
2171 case 0: case 'M': case 'm':
2172 value <<= 20;
2173 break;
2174 case 'G': case 'g':
2175 value <<= 30;
2176 break;
2177 default:
2178 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002179 exit(1);
2180 }
aurel3200f82b82008-04-27 21:12:55 +00002181
2182 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002183 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00002184 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2185 exit(1);
2186 }
Anthony Liguoric227f092009-10-01 16:12:16 -05002187 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002188 fprintf(stderr, "qemu: ram size too large\n");
2189 exit(1);
2190 }
2191 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002192 break;
aurel3200f82b82008-04-27 21:12:55 +00002193 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002194 case QEMU_OPTION_mempath:
2195 mem_path = optarg;
2196 break;
2197#ifdef MAP_POPULATE
2198 case QEMU_OPTION_mem_prealloc:
2199 mem_prealloc = 1;
2200 break;
2201#endif
bellardcd6f1162004-05-13 22:02:20 +00002202 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002203 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00002204 break;
bellardcd6f1162004-05-13 22:02:20 +00002205 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002206 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002207 break;
aliguori59030a82009-04-05 18:43:41 +00002208 case QEMU_OPTION_gdb:
2209 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002210 break;
bellardcd6f1162004-05-13 22:02:20 +00002211 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002212 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002213 break;
j_mayer1192dad2007-10-05 13:08:35 +00002214 case QEMU_OPTION_bios:
2215 bios_name = optarg;
2216 break;
aurel321b530a62009-04-05 20:08:59 +00002217 case QEMU_OPTION_singlestep:
2218 singlestep = 1;
2219 break;
bellardcd6f1162004-05-13 22:02:20 +00002220 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002221 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002222 break;
bellard3d11d0e2004-12-12 16:56:30 +00002223 case QEMU_OPTION_k:
2224 keyboard_layout = optarg;
2225 break;
bellardee22c2f2004-06-03 12:49:50 +00002226 case QEMU_OPTION_localtime:
2227 rtc_utc = 0;
2228 break;
malc3893c122008-09-28 00:42:05 +00002229 case QEMU_OPTION_vga:
2230 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002231 break;
bellarde9b137c2004-06-21 16:46:10 +00002232 case QEMU_OPTION_g:
2233 {
2234 const char *p;
2235 int w, h, depth;
2236 p = optarg;
2237 w = strtol(p, (char **)&p, 10);
2238 if (w <= 0) {
2239 graphic_error:
2240 fprintf(stderr, "qemu: invalid resolution or depth\n");
2241 exit(1);
2242 }
2243 if (*p != 'x')
2244 goto graphic_error;
2245 p++;
2246 h = strtol(p, (char **)&p, 10);
2247 if (h <= 0)
2248 goto graphic_error;
2249 if (*p == 'x') {
2250 p++;
2251 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002252 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002253 depth != 24 && depth != 32)
2254 goto graphic_error;
2255 } else if (*p == '\0') {
2256 depth = graphic_depth;
2257 } else {
2258 goto graphic_error;
2259 }
ths3b46e622007-09-17 08:09:54 +00002260
bellarde9b137c2004-06-21 16:46:10 +00002261 graphic_width = w;
2262 graphic_height = h;
2263 graphic_depth = depth;
2264 }
2265 break;
ths20d8a3e2007-02-18 17:04:49 +00002266 case QEMU_OPTION_echr:
2267 {
2268 char *r;
2269 term_escape_char = strtol(optarg, &r, 0);
2270 if (r == optarg)
2271 printf("Bad argument to echr\n");
2272 break;
2273 }
bellard82c643f2004-07-14 17:28:13 +00002274 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002275 monitor_parse(optarg, "readline");
2276 default_monitor = 0;
2277 break;
2278 case QEMU_OPTION_qmp:
2279 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002280 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002281 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002282 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002283 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002284 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002285 exit(1);
2286 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002287 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002288 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002289 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002290 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002291 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002292 exit(1);
2293 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002294 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302295 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002296 olist = qemu_find_opts("fsdev");
2297 if (!olist) {
2298 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2299 exit(1);
2300 }
2301 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302302 if (!opts) {
2303 fprintf(stderr, "parse error: %s\n", optarg);
2304 exit(1);
2305 }
2306 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302307 case QEMU_OPTION_virtfs: {
2308 char *arg_fsdev = NULL;
2309 char *arg_9p = NULL;
2310 int len = 0;
2311
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002312 olist = qemu_find_opts("virtfs");
2313 if (!olist) {
2314 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2315 exit(1);
2316 }
2317 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302318 if (!opts) {
2319 fprintf(stderr, "parse error: %s\n", optarg);
2320 exit(1);
2321 }
2322
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002323 if (qemu_opt_get(opts, "fstype") == NULL ||
2324 qemu_opt_get(opts, "mount_tag") == NULL ||
2325 qemu_opt_get(opts, "path") == NULL ||
2326 qemu_opt_get(opts, "security_model") == NULL) {
2327 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +05302328 "security_model=[mapped|passthrough|none],"
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002329 "mnt_tag=tag.\n");
2330 exit(1);
2331 }
2332
2333 len = strlen(",id=,path=,security_model=");
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302334 len += strlen(qemu_opt_get(opts, "fstype"));
2335 len += strlen(qemu_opt_get(opts, "mount_tag"));
2336 len += strlen(qemu_opt_get(opts, "path"));
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002337 len += strlen(qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302338 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2339
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302340 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002341 "%s,id=%s,path=%s,security_model=%s",
2342 qemu_opt_get(opts, "fstype"),
2343 qemu_opt_get(opts, "mount_tag"),
2344 qemu_opt_get(opts, "path"),
2345 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302346
2347 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2348 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2349 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2350
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302351 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002352 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2353 qemu_opt_get(opts, "mount_tag"),
2354 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302355
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002356 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302357 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2358 exit(1);
2359 }
2360
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002361 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302362 fprintf(stderr, "parse error [device]: %s\n", optarg);
2363 exit(1);
2364 }
2365
2366 qemu_free(arg_fsdev);
2367 qemu_free(arg_9p);
2368 break;
2369 }
bellard82c643f2004-07-14 17:28:13 +00002370 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002371 add_device_config(DEV_SERIAL, optarg);
2372 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002373 if (strncmp(optarg, "mon:", 4) == 0) {
2374 default_monitor = 0;
2375 }
bellard82c643f2004-07-14 17:28:13 +00002376 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002377 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002378 if (watchdog) {
2379 fprintf(stderr,
2380 "qemu: only one watchdog option may be given\n");
2381 return 1;
2382 }
2383 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002384 break;
2385 case QEMU_OPTION_watchdog_action:
2386 if (select_watchdog_action(optarg) == -1) {
2387 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2388 exit(1);
2389 }
2390 break;
aliguori51ecf132009-01-15 20:06:40 +00002391 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002392 add_device_config(DEV_VIRTCON, optarg);
2393 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002394 if (strncmp(optarg, "mon:", 4) == 0) {
2395 default_monitor = 0;
2396 }
aliguori51ecf132009-01-15 20:06:40 +00002397 break;
bellard6508fe52005-01-15 12:02:56 +00002398 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002399 add_device_config(DEV_PARALLEL, optarg);
2400 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002401 if (strncmp(optarg, "mon:", 4) == 0) {
2402 default_monitor = 0;
2403 }
bellard6508fe52005-01-15 12:02:56 +00002404 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002405 case QEMU_OPTION_debugcon:
2406 add_device_config(DEV_DEBUGCON, optarg);
2407 break;
bellardd63d3072004-10-03 13:29:03 +00002408 case QEMU_OPTION_loadvm:
2409 loadvm = optarg;
2410 break;
2411 case QEMU_OPTION_full_screen:
2412 full_screen = 1;
2413 break;
ths667acca2006-12-11 02:08:05 +00002414#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002415 case QEMU_OPTION_no_frame:
2416 no_frame = 1;
2417 break;
ths3780e192007-06-21 21:08:02 +00002418 case QEMU_OPTION_alt_grab:
2419 alt_grab = 1;
2420 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002421 case QEMU_OPTION_ctrl_grab:
2422 ctrl_grab = 1;
2423 break;
ths667acca2006-12-11 02:08:05 +00002424 case QEMU_OPTION_no_quit:
2425 no_quit = 1;
2426 break;
aliguori7d957bd2009-01-15 22:14:11 +00002427 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002428 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002429 break;
ths667acca2006-12-11 02:08:05 +00002430#endif
bellardf7cce892004-12-08 22:21:25 +00002431 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002432 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002433 break;
bellarda09db212005-04-30 16:10:35 +00002434 case QEMU_OPTION_win2k_hack:
2435 win2k_install_hack = 1;
2436 break;
aliguori73822ec2009-01-15 20:11:34 +00002437 case QEMU_OPTION_rtc_td_hack:
2438 rtc_td_hack = 1;
2439 break;
aliguori8a92ea22009-02-27 20:12:36 +00002440 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002441 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002442 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002443 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002444 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002445 break;
aliguori7ba1e612008-11-05 16:04:33 +00002446 case QEMU_OPTION_enable_kvm:
2447 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00002448 break;
bellardbb36d472005-11-05 14:22:28 +00002449 case QEMU_OPTION_usb:
2450 usb_enabled = 1;
2451 break;
bellarda594cfb2005-11-06 16:13:29 +00002452 case QEMU_OPTION_usbdevice:
2453 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002454 add_device_config(DEV_USB, optarg);
2455 break;
2456 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002457 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002458 exit(1);
2459 }
bellarda594cfb2005-11-06 16:13:29 +00002460 break;
bellard6a00d602005-11-21 23:25:50 +00002461 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002462 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002463 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002464 fprintf(stderr, "Invalid number of CPUs\n");
2465 exit(1);
2466 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002467 if (max_cpus < smp_cpus) {
2468 fprintf(stderr, "maxcpus must be equal to or greater than "
2469 "smp\n");
2470 exit(1);
2471 }
2472 if (max_cpus > 255) {
2473 fprintf(stderr, "Unsupported number of maxcpus\n");
2474 exit(1);
2475 }
bellard6a00d602005-11-21 23:25:50 +00002476 break;
bellard24236862006-04-30 21:28:36 +00002477 case QEMU_OPTION_vnc:
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002478 display_remote++;
ths73fc9742006-12-22 02:09:07 +00002479 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00002480 break;
bellard6515b202006-05-03 22:02:44 +00002481 case QEMU_OPTION_no_acpi:
2482 acpi_enabled = 0;
2483 break;
aliguori16b29ae2008-12-17 23:28:44 +00002484 case QEMU_OPTION_no_hpet:
2485 no_hpet = 1;
2486 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002487 case QEMU_OPTION_balloon:
2488 if (balloon_parse(optarg) < 0) {
2489 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2490 exit(1);
2491 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002492 break;
bellardd1beab82006-10-02 19:44:22 +00002493 case QEMU_OPTION_no_reboot:
2494 no_reboot = 1;
2495 break;
aurel32b2f76162008-04-11 21:35:52 +00002496 case QEMU_OPTION_no_shutdown:
2497 no_shutdown = 1;
2498 break;
balrog9467cd42007-05-01 01:34:14 +00002499 case QEMU_OPTION_show_cursor:
2500 cursor_hide = 0;
2501 break;
blueswir18fcb1b92008-09-18 18:29:08 +00002502 case QEMU_OPTION_uuid:
2503 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2504 fprintf(stderr, "Fail to parse UUID string."
2505 " Wrong format.\n");
2506 exit(1);
2507 }
2508 break;
ths9ae02552007-01-05 17:39:04 +00002509 case QEMU_OPTION_option_rom:
2510 if (nb_option_roms >= MAX_OPTION_ROMS) {
2511 fprintf(stderr, "Too many option ROMs\n");
2512 exit(1);
2513 }
2514 option_rom[nb_option_roms] = optarg;
2515 nb_option_roms++;
2516 break;
pbrook8e716212007-01-20 17:12:09 +00002517 case QEMU_OPTION_semihosting:
2518 semihosting_enabled = 1;
2519 break;
thsc35734b2007-03-19 15:17:08 +00002520 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02002521 qemu_name = qemu_strdup(optarg);
2522 {
2523 char *p = strchr(qemu_name, ',');
2524 if (p != NULL) {
2525 *p++ = 0;
2526 if (strncmp(p, "process=", 8)) {
2527 fprintf(stderr, "Unknown subargument %s to -name", p);
2528 exit(1);
2529 }
2530 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02002531 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02002532 }
2533 }
thsc35734b2007-03-19 15:17:08 +00002534 break;
blueswir166508602007-05-01 14:16:52 +00002535 case QEMU_OPTION_prom_env:
2536 if (nb_prom_envs >= MAX_PROM_ENVS) {
2537 fprintf(stderr, "Too many prom variables\n");
2538 exit(1);
2539 }
2540 prom_envs[nb_prom_envs] = optarg;
2541 nb_prom_envs++;
2542 break;
balrog2b8f2d42007-07-27 22:08:46 +00002543 case QEMU_OPTION_old_param:
2544 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00002545 break;
thsf3dcfad2007-08-24 01:26:02 +00002546 case QEMU_OPTION_clock:
2547 configure_alarms(optarg);
2548 break;
bellard7e0af5d02007-11-07 16:24:33 +00002549 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002550 configure_rtc_date_offset(optarg, 1);
2551 break;
2552 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002553 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002554 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002555 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00002556 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002557 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00002558 break;
bellard26a5f132008-05-28 12:30:31 +00002559 case QEMU_OPTION_tb_size:
2560 tb_size = strtol(optarg, NULL, 0);
2561 if (tb_size < 0)
2562 tb_size = 0;
2563 break;
pbrook2e70f6e2008-06-29 01:03:05 +00002564 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002565 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00002566 break;
aliguori5bb79102008-10-13 03:12:02 +00002567 case QEMU_OPTION_incoming:
2568 incoming = optarg;
Amit Shah8e848652010-07-27 15:49:19 +05302569 incoming_expected = true;
aliguori5bb79102008-10-13 03:12:02 +00002570 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002571 case QEMU_OPTION_nodefaults:
2572 default_serial = 0;
2573 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002574 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002575 default_monitor = 0;
2576 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01002577 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002578 default_floppy = 0;
2579 default_cdrom = 0;
2580 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002581 break;
aliguorie37630c2009-04-22 15:19:10 +00002582 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00002583 if (!(xen_available())) {
2584 printf("Option %s not supported for this target\n", popt->name);
2585 exit(1);
2586 }
aliguorie37630c2009-04-22 15:19:10 +00002587 xen_domid = atoi(optarg);
2588 break;
2589 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00002590 if (!(xen_available())) {
2591 printf("Option %s not supported for this target\n", popt->name);
2592 exit(1);
2593 }
aliguorie37630c2009-04-22 15:19:10 +00002594 xen_mode = XEN_CREATE;
2595 break;
2596 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00002597 if (!(xen_available())) {
2598 printf("Option %s not supported for this target\n", popt->name);
2599 exit(1);
2600 }
aliguorie37630c2009-04-22 15:19:10 +00002601 xen_mode = XEN_ATTACH;
2602 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002603#ifdef CONFIG_SIMPLE_TRACE
2604 case QEMU_OPTION_trace:
2605 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2606 if (opts) {
2607 trace_file = qemu_opt_get(opts, "file");
2608 }
2609 break;
2610#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002611 case QEMU_OPTION_readconfig:
2612 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002613 int ret = qemu_read_config_file(optarg);
2614 if (ret < 0) {
2615 fprintf(stderr, "read config %s: %s\n", optarg,
2616 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002617 exit(1);
2618 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002619 break;
2620 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03002621 case QEMU_OPTION_spice:
2622 olist = qemu_find_opts("spice");
2623 if (!olist) {
2624 fprintf(stderr, "spice is not supported by this qemu build.\n");
2625 exit(1);
2626 }
2627 opts = qemu_opts_parse(olist, optarg, 0);
2628 if (!opts) {
2629 fprintf(stderr, "parse error: %s\n", optarg);
2630 exit(1);
2631 }
2632 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002633 case QEMU_OPTION_writeconfig:
2634 {
2635 FILE *fp;
2636 if (strcmp(optarg, "-") == 0) {
2637 fp = stdout;
2638 } else {
2639 fp = fopen(optarg, "w");
2640 if (fp == NULL) {
2641 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2642 exit(1);
2643 }
2644 }
2645 qemu_config_write(fp);
2646 fclose(fp);
2647 break;
2648 }
Jes Sorensen59a52642010-06-10 11:42:25 +02002649 default:
2650 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00002651 }
bellard0824d6f2003-06-24 13:42:40 +00002652 }
2653 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002654 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00002655
Paul Brook5cea8592009-05-30 00:52:44 +01002656 /* If no data_dir is specified then try to find it relative to the
2657 executable path. */
2658 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02002659 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01002660 }
2661 /* If all else fails use the install patch specified when building. */
2662 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02002663 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01002664 }
2665
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002666#ifdef CONFIG_SIMPLE_TRACE
2667 /*
2668 * Set the trace file name, if specified.
2669 */
2670 st_set_trace_file(trace_file);
2671#endif
Jes Sorensen6be68d72009-07-23 17:03:42 +02002672 /*
2673 * Default to max_cpus = smp_cpus, in case the user doesn't
2674 * specify a max_cpus value.
2675 */
2676 if (!max_cpus)
2677 max_cpus = smp_cpus;
2678
balrog3d878ca2008-10-28 10:59:59 +00002679 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00002680 if (smp_cpus > machine->max_cpus) {
2681 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2682 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2683 machine->max_cpus);
2684 exit(1);
2685 }
2686
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002687 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2688 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002689
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002690 if (machine->no_serial) {
2691 default_serial = 0;
2692 }
2693 if (machine->no_parallel) {
2694 default_parallel = 0;
2695 }
2696 if (!machine->use_virtcon) {
2697 default_virtcon = 0;
2698 }
2699 if (machine->no_vga) {
2700 default_vga = 0;
2701 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002702 if (machine->no_floppy) {
2703 default_floppy = 0;
2704 }
2705 if (machine->no_cdrom) {
2706 default_cdrom = 0;
2707 }
2708 if (machine->no_sdcard) {
2709 default_sdcard = 0;
2710 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002711
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002712 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002713 if (default_parallel)
2714 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002715 if (default_serial && default_monitor) {
2716 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002717 } else if (default_virtcon && default_monitor) {
2718 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002719 } else {
2720 if (default_serial)
2721 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002722 if (default_virtcon)
2723 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002724 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002725 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002726 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002727 } else {
2728 if (default_serial)
2729 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002730 if (default_parallel)
2731 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01002732 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002733 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01002734 if (default_virtcon)
2735 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00002736 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01002737 if (default_vga)
2738 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00002739
TeLeMana5829fd2010-04-15 12:37:55 +08002740 socket_init();
2741
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002742 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002743 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002744#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002745 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302746 exit(1);
2747 }
2748#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002749
Jes Sorenseneb505be2010-06-10 11:42:28 +02002750 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00002751
thsaa26bb22007-03-25 21:33:06 +00002752 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02002753 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00002754 exit(1);
2755 }
2756
Paolo Bonzini98c85732010-04-19 18:59:30 +00002757 if (kvm_allowed) {
2758 int ret = kvm_init(smp_cpus);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002759 if (ret < 0) {
Paolo Bonzini98c85732010-04-19 18:59:30 +00002760 if (!kvm_available()) {
2761 printf("KVM not supported for this target\n");
2762 } else {
2763 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2764 }
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002765 exit(1);
2766 }
2767 }
2768
aliguori3fcf7b62009-04-24 18:03:25 +00002769 if (qemu_init_main_loop()) {
2770 fprintf(stderr, "qemu_init_main_loop failed\n");
2771 exit(1);
2772 }
bellarda20dd502003-09-30 21:07:02 +00002773 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00002774
thsf8d39c02008-07-03 10:01:15 +00002775 if (!linux_boot && *kernel_cmdline != '\0') {
2776 fprintf(stderr, "-append only allowed with -kernel option\n");
2777 exit(1);
2778 }
2779
2780 if (!linux_boot && initrd_filename != NULL) {
2781 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2782 exit(1);
2783 }
2784
Jes Sorensen9156d762010-06-10 11:42:30 +02002785 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00002786
aliguori7183b4b2008-11-05 20:40:18 +00002787 if (init_timer_alarm() < 0) {
2788 fprintf(stderr, "could not initialize alarm timer\n");
2789 exit(1);
2790 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002791 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00002792
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01002793 if (net_init_clients() < 0) {
2794 exit(1);
bellard702c6512004-04-02 21:21:32 +00002795 }
bellardf1510b22003-06-25 00:07:40 +00002796
balrogdc72ac12008-11-09 00:04:26 +00002797 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002798 if (foreach_device_config(DEV_BT, bt_parse))
2799 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00002800
bellard0824d6f2003-06-24 13:42:40 +00002801 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00002802 if (ram_size == 0)
2803 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00002804
bellard26a5f132008-05-28 12:30:31 +00002805 /* init the dynamic translator */
2806 cpu_exec_init_all(tb_size * 1024 * 1024);
2807
Markus Armbrustereb852012009-10-27 18:41:44 +01002808 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00002809
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002810 blk_mig_init();
2811
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002812 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002813 /* we always create the cdrom drive, even if no disk is there */
2814 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002815 }
thse4bcb142007-12-02 04:51:10 +00002816
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002817 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002818 /* we always create at least one floppy */
2819 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002820 }
bellardc4b1fcc2004-03-14 21:44:30 +00002821
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002822 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002823 /* we always create one sd slot, even if no card is in it */
2824 drive_add(NULL, SD_ALIAS);
2825 }
balrog9d413d12007-12-04 00:10:34 +00002826
ths96d30e42007-01-07 20:42:14 +00002827 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002828 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002829 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2830 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002831 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00002832
Alex Williamson97ab12d2010-06-25 11:09:50 -06002833 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002834 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00002835
aliguori268a3622009-04-21 22:30:27 +00002836 if (nb_numa_nodes > 0) {
2837 int i;
2838
2839 if (nb_numa_nodes > smp_cpus) {
2840 nb_numa_nodes = smp_cpus;
2841 }
2842
2843 /* If no memory size if given for any node, assume the default case
2844 * and distribute the available memory equally across all nodes
2845 */
2846 for (i = 0; i < nb_numa_nodes; i++) {
2847 if (node_mem[i] != 0)
2848 break;
2849 }
2850 if (i == nb_numa_nodes) {
2851 uint64_t usedmem = 0;
2852
2853 /* On Linux, the each node's border has to be 8MB aligned,
2854 * the final node gets the rest.
2855 */
2856 for (i = 0; i < nb_numa_nodes - 1; i++) {
2857 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2858 usedmem += node_mem[i];
2859 }
2860 node_mem[i] = ram_size - usedmem;
2861 }
2862
2863 for (i = 0; i < nb_numa_nodes; i++) {
2864 if (node_cpumask[i] != 0)
2865 break;
2866 }
2867 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2868 * must cope with this anyway, because there are BIOSes out there in
2869 * real machines which also use this scheme.
2870 */
2871 if (i == nb_numa_nodes) {
2872 for (i = 0; i < smp_cpus; i++) {
2873 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2874 }
2875 }
2876 }
2877
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002878 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02002879 exit(1);
2880 }
2881
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002882 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2883 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002884 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2885 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002886 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2887 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002888 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2889 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00002890
Paul Brookaae94602009-05-14 22:35:06 +01002891 module_call_init(MODULE_INIT_DEVICE);
2892
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002893 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002894 exit(0);
2895
Markus Armbruster09aaa162009-08-21 10:31:34 +02002896 if (watchdog) {
2897 i = select_watchdog(watchdog);
2898 if (i > 0)
2899 exit (i == 1 ? 1 : 0);
2900 }
2901
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002902 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01002903 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002904 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002905 qemu_add_globals();
2906
Paul Brookfbe1b592009-05-13 17:56:25 +01002907 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00002908 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2909
Jan Kiszkaea375f92010-03-01 19:10:30 +01002910 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00002911
Juan Quintela67b3b712009-08-28 19:25:15 +02002912 /* must be after terminal init, SDL library changes signal handlers */
Jes Sorensen8d963e62010-06-10 11:42:22 +02002913 os_setup_signal_handling();
Juan Quintela67b3b712009-08-28 19:25:15 +02002914
Blue Swirl87d0a282010-03-27 18:24:45 +00002915 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00002916
aliguori6f338c32009-02-11 15:21:54 +00002917 current_machine = machine;
2918
aliguori3023f332009-01-16 19:04:14 +00002919 /* init USB devices */
2920 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01002921 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2922 exit(1);
aliguori3023f332009-01-16 19:04:14 +00002923 }
2924
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002925 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002926 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002927 exit(1);
2928
Markus Armbruster668680f2010-02-11 14:44:58 +01002929 net_check_clients();
2930
aliguori3023f332009-01-16 19:04:14 +00002931 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01002932 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002933
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002934 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002935#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002936 display_type = DT_SDL;
2937#else
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002938 vnc_display = "localhost:0,to=99";
2939 show_vnc_port = 1;
2940#endif
2941 }
2942
2943
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002944 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002945 switch (display_type) {
2946 case DT_NOGRAPHIC:
2947 break;
2948#if defined(CONFIG_CURSES)
2949 case DT_CURSES:
2950 curses_display_init(ds, full_screen);
2951 break;
2952#endif
bellard5b0753e2005-03-01 21:37:28 +00002953#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002954 case DT_SDL:
2955 sdl_display_init(ds, full_screen, no_frame);
2956 break;
bellard5b0753e2005-03-01 21:37:28 +00002957#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002958 case DT_SDL:
2959 cocoa_display_init(ds, full_screen);
2960 break;
bellard313aa562003-08-10 21:52:11 +00002961#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002962 default:
2963 break;
2964 }
2965
2966 /* init remote displays */
2967 if (vnc_display) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002968 vnc_display_init(ds);
2969 if (vnc_display_open(ds, vnc_display) < 0)
2970 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002971
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002972 if (show_vnc_port) {
2973 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002974 }
bellard313aa562003-08-10 21:52:11 +00002975 }
aliguori5b08fc12008-08-21 20:08:03 +00002976
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002977 /* display setup */
2978 dpy_resize(ds);
aliguori3023f332009-01-16 19:04:14 +00002979 dcl = ds->listeners;
2980 while (dcl != NULL) {
2981 if (dcl->dpy_refresh != NULL) {
2982 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2983 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09002984 break;
ths20d8a3e2007-02-18 17:04:49 +00002985 }
aliguori3023f332009-01-16 19:04:14 +00002986 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00002987 }
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002988 if (ds->gui_timer == NULL) {
blueswir19043b622009-01-21 19:28:13 +00002989 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2990 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2991 }
Paolo Bonzinib473df62010-02-11 00:29:55 +01002992 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00002993
aliguori59030a82009-04-05 18:43:41 +00002994 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2995 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2996 gdbstub_dev);
2997 exit(1);
balrog45669e02007-07-02 13:20:17 +00002998 }
balrog45669e02007-07-02 13:20:17 +00002999
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003000 qdev_machine_creation_done();
3001
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003002 if (rom_load_all() != 0) {
3003 fprintf(stderr, "rom loading failed\n");
3004 exit(1);
3005 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003006
Juan Quintela504c2942009-11-12 00:39:13 +01003007 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02003008 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003009 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003010 autostart = 0;
3011 }
3012 }
bellardd63d3072004-10-03 13:29:03 +00003013
Glauber Costa2bb8c102009-07-24 16:20:23 -04003014 if (incoming) {
Juan Quintela8ca5e802010-06-09 14:10:54 +02003015 int ret = qemu_start_incoming_migration(incoming);
3016 if (ret < 0) {
3017 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3018 incoming, ret);
3019 exit(ret);
3020 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03003021 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003022 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003023 }
thsffd843b2006-12-21 19:46:43 +00003024
Jes Sorenseneb505be2010-06-10 11:42:28 +02003025 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003026
bellard8a7ddc32004-03-31 19:00:16 +00003027 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003028 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003029 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00003030
bellard0824d6f2003-06-24 13:42:40 +00003031 return 0;
3032}