blob: 939ee87b66b41daa74c8fcd0f1f878a02294d03e [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000038#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000039#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000042#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000043#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000044#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000047#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000048#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000049#include <sys/select.h>
Prerna Saxenaab6540d2010-08-09 11:48:32 +010050#ifdef CONFIG_SIMPLE_TRACE
51#include "trace.h"
52#endif
53
Juan Quintela71e72a12009-07-27 16:12:56 +020054#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000055#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010056#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000057#include <libutil.h>
Juergen Lock92c0e652010-03-25 22:07:12 +010058#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000059#else
60#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000061#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010062#else
blueswir1223f0d72008-09-30 18:12:18 +000063#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000064#include <pty.h>
65#include <malloc.h>
thsbd494f42007-09-16 20:03:23 +000066
bellarde57a8c02005-11-10 23:58:52 +000067#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000068#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000069#endif
70#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000071#include <sys/stat.h>
72#include <sys/ethernet.h>
73#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000074#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000075#include <netinet/in_systm.h>
76#include <netinet/ip.h>
77#include <netinet/ip_icmp.h> // must come after ip.h
78#include <netinet/udp.h>
79#include <netinet/tcp.h>
80#include <net/if.h>
81#include <syslog.h>
82#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000083#endif
bellard7d3505c2004-05-12 19:32:15 +000084#endif
bellardec530c82006-04-25 22:36:06 +000085#endif
bellard67b915a2004-03-31 23:37:16 +000086
blueswir19892fbf2008-08-24 10:34:20 +000087#if defined(__OpenBSD__)
88#include <util.h>
89#endif
90
ths8a16d272008-07-19 09:56:24 +000091#if defined(CONFIG_VDE)
92#include <libvdeplug.h>
93#endif
94
bellard67b915a2004-03-31 23:37:16 +000095#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000096#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000097#endif
98
bellard73332e52004-04-04 20:22:28 +000099#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200100#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200101#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000102int qemu_main(int argc, char **argv, char **envp);
103int main(int argc, char **argv)
104{
Stefan Weil59a36a22009-06-18 20:11:03 +0200105 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000106}
107#undef main
108#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000109#endif
bellard73332e52004-04-04 20:22:28 +0000110#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000111
bellard5b0753e2005-03-01 21:37:28 +0000112#ifdef CONFIG_COCOA
113#undef main
114#define main qemu_main
115#endif /* CONFIG_COCOA */
116
blueswir1511d2b12009-03-07 15:32:56 +0000117#include "hw/hw.h"
118#include "hw/boards.h"
119#include "hw/usb.h"
120#include "hw/pcmcia.h"
121#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000122#include "hw/isa.h"
123#include "hw/baum.h"
124#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100125#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000126#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000127#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200128#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200129#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000130#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000131#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000132#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000133#include "monitor.h"
134#include "console.h"
135#include "sysemu.h"
136#include "gdbstub.h"
137#include "qemu-timer.h"
138#include "qemu-char.h"
139#include "cache-utils.h"
140#include "block.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200141#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200142#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000143#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000144#include "audio/audio.h"
145#include "migration.h"
146#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200147#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200148#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200149#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200150#include "qemu-options.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700151#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530152#include "fsdev/qemu-fsdev.h"
153#endif
blueswir1511d2b12009-03-07 15:32:56 +0000154
bellard0824d6f2003-06-24 13:42:40 +0000155#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000156
blueswir1511d2b12009-03-07 15:32:56 +0000157#include "qemu_socket.h"
158
Jan Kiszkad918f232009-06-24 14:42:30 +0200159#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
Blue Swirl72cf2d42009-09-12 07:36:22 +0000161#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000162#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000163#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000164
blueswir19dc63a12008-10-04 07:25:46 +0000165//#define DEBUG_NET
166//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000167
bellard1bfe8562004-07-08 21:17:50 +0000168#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000169
Amit Shah98b19252010-01-20 00:36:52 +0530170#define MAX_VIRTIO_CONSOLES 1
171
Paul Brook5cea8592009-05-30 00:52:44 +0100172static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000173const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000174enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500175DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000176const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500177ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300178const char *mem_path = NULL;
179#ifdef MAP_POPULATE
180int mem_prealloc = 0; /* force preallocation of physical target memory */
181#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000182int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000183NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000184int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200185int autostart;
Amit Shah8e848652010-07-27 15:49:19 +0530186int incoming_expected; /* Started with -incoming and waiting for incoming */
balrogf6503052008-02-17 11:42:19 +0000187static int rtc_utc = 1;
188static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200189QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100190int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000191static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000192#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000193static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000194#endif
ths667acca2006-12-11 02:08:05 +0000195int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000196CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000197CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000198CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000199int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000200int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000201int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000202int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000203int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200204int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200205int smp_cores = 1;
206int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000207const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000208int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000209int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000210int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000211int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000212int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000213int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000214int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200215uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200216const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000217const char *option_rom[MAX_OPTION_ROMS];
218int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000219int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000220int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000221const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000222int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500223int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000224unsigned int nb_prom_envs = 0;
225const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200226int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000227
aliguori268a3622009-04-21 22:30:27 +0000228int nb_numa_nodes;
229uint64_t node_mem[MAX_NODES];
230uint64_t node_cpumask[MAX_NODES];
231
blueswir19043b622009-01-21 19:28:13 +0000232static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000233
blueswir18fcb1b92008-09-18 18:29:08 +0000234uint8_t qemu_uuid[16];
235
Jan Kiszka76e30d02009-07-02 00:19:02 +0200236static QEMUBootSetHandler *boot_set_handler;
237static void *boot_set_opaque;
238
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200239static NotifierList exit_notifiers =
240 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
241
Blue Swirld745bef2010-03-29 19:23:49 +0000242int kvm_allowed = 0;
243uint32_t xen_domid;
244enum xen_mode xen_mode = XEN_EMULATE;
245
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100246static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100247static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100248static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100249static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100250static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100251static int default_floppy = 1;
252static int default_cdrom = 1;
253static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100254
255static struct {
256 const char *driver;
257 int *flag;
258} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100259 { .driver = "isa-serial", .flag = &default_serial },
260 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100261 { .driver = "isa-fdc", .flag = &default_floppy },
262 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530263 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
264 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
265 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100266 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100267 { .driver = "cirrus-vga", .flag = &default_vga },
268 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100269};
270
271static int default_driver_check(QemuOpts *opts, void *opaque)
272{
273 const char *driver = qemu_opt_get(opts, "driver");
274 int i;
275
276 if (!driver)
277 return 0;
278 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
279 if (strcmp(default_list[i].driver, driver) != 0)
280 continue;
281 *(default_list[i].flag) = 0;
282 }
283 return 0;
284}
285
bellard0824d6f2003-06-24 13:42:40 +0000286/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100287/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000288
bellard87858c82003-06-27 12:01:39 +0000289/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000290uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000291{
292 union {
293 uint64_t ll;
294 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200295#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000296 uint32_t high, low;
297#else
298 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000299#endif
bellard87858c82003-06-27 12:01:39 +0000300 } l;
301 } u, res;
302 uint64_t rl, rh;
303
304 u.ll = a;
305 rl = (uint64_t)u.l.low * (uint64_t)b;
306 rh = (uint64_t)u.l.high * (uint64_t)b;
307 rh += (rl >> 32);
308 res.l.high = rh / c;
309 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
310 return res.ll;
311}
312
bellardc4b1fcc2004-03-14 21:44:30 +0000313/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000314/* host time/date access */
315void qemu_get_timedate(struct tm *tm, int offset)
316{
317 time_t ti;
318 struct tm *ret;
319
320 time(&ti);
321 ti += offset;
322 if (rtc_date_offset == -1) {
323 if (rtc_utc)
324 ret = gmtime(&ti);
325 else
326 ret = localtime(&ti);
327 } else {
328 ti -= rtc_date_offset;
329 ret = gmtime(&ti);
330 }
331
332 memcpy(tm, ret, sizeof(struct tm));
333}
334
335int qemu_timedate_diff(struct tm *tm)
336{
337 time_t seconds;
338
339 if (rtc_date_offset == -1)
340 if (rtc_utc)
341 seconds = mktimegm(tm);
342 else
343 seconds = mktime(tm);
344 else
345 seconds = mktimegm(tm) + rtc_date_offset;
346
347 return seconds - time(NULL);
348}
349
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300350void rtc_change_mon_event(struct tm *tm)
351{
352 QObject *data;
353
354 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
355 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
356 qobject_decref(data);
357}
358
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200359static void configure_rtc_date_offset(const char *startdate, int legacy)
360{
361 time_t rtc_start_date;
362 struct tm tm;
363
364 if (!strcmp(startdate, "now") && legacy) {
365 rtc_date_offset = -1;
366 } else {
367 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
368 &tm.tm_year,
369 &tm.tm_mon,
370 &tm.tm_mday,
371 &tm.tm_hour,
372 &tm.tm_min,
373 &tm.tm_sec) == 6) {
374 /* OK */
375 } else if (sscanf(startdate, "%d-%d-%d",
376 &tm.tm_year,
377 &tm.tm_mon,
378 &tm.tm_mday) == 3) {
379 tm.tm_hour = 0;
380 tm.tm_min = 0;
381 tm.tm_sec = 0;
382 } else {
383 goto date_fail;
384 }
385 tm.tm_year -= 1900;
386 tm.tm_mon--;
387 rtc_start_date = mktimegm(&tm);
388 if (rtc_start_date == -1) {
389 date_fail:
390 fprintf(stderr, "Invalid date format. Valid formats are:\n"
391 "'2006-06-17T16:01:21' or '2006-06-17'\n");
392 exit(1);
393 }
394 rtc_date_offset = time(NULL) - rtc_start_date;
395 }
396}
397
398static void configure_rtc(QemuOpts *opts)
399{
400 const char *value;
401
402 value = qemu_opt_get(opts, "base");
403 if (value) {
404 if (!strcmp(value, "utc")) {
405 rtc_utc = 1;
406 } else if (!strcmp(value, "localtime")) {
407 rtc_utc = 0;
408 } else {
409 configure_rtc_date_offset(value, 0);
410 }
411 }
Jan Kiszka68752042009-09-15 13:36:04 +0200412 value = qemu_opt_get(opts, "clock");
413 if (value) {
414 if (!strcmp(value, "host")) {
415 rtc_clock = host_clock;
416 } else if (!strcmp(value, "vm")) {
417 rtc_clock = vm_clock;
418 } else {
419 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
420 exit(1);
421 }
422 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200423 value = qemu_opt_get(opts, "driftfix");
424 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000425 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200426 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000427 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200428 rtc_td_hack = 0;
429 } else {
430 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
431 exit(1);
432 }
433 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200434}
435
balrog1ae26a12008-09-28 23:19:47 +0000436/***********************************************************/
437/* Bluetooth support */
438static int nb_hcis;
439static int cur_hci;
440static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000441
balrog1ae26a12008-09-28 23:19:47 +0000442static struct bt_vlan_s {
443 struct bt_scatternet_s net;
444 int id;
445 struct bt_vlan_s *next;
446} *first_bt_vlan;
447
448/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000449static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000450{
451 struct bt_vlan_s **pvlan, *vlan;
452 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
453 if (vlan->id == id)
454 return &vlan->net;
455 }
456 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
457 vlan->id = id;
458 pvlan = &first_bt_vlan;
459 while (*pvlan != NULL)
460 pvlan = &(*pvlan)->next;
461 *pvlan = vlan;
462 return &vlan->net;
463}
464
465static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
466{
467}
468
469static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
470{
471 return -ENOTSUP;
472}
473
474static struct HCIInfo null_hci = {
475 .cmd_send = null_hci_send,
476 .sco_send = null_hci_send,
477 .acl_send = null_hci_send,
478 .bdaddr_set = null_hci_addr_set,
479};
480
481struct HCIInfo *qemu_next_hci(void)
482{
483 if (cur_hci == nb_hcis)
484 return &null_hci;
485
486 return hci_table[cur_hci++];
487}
488
balrogdc72ac12008-11-09 00:04:26 +0000489static struct HCIInfo *hci_init(const char *str)
490{
491 char *endp;
492 struct bt_scatternet_s *vlan = 0;
493
494 if (!strcmp(str, "null"))
495 /* null */
496 return &null_hci;
497 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
498 /* host[:hciN] */
499 return bt_host_hci(str[4] ? str + 5 : "hci0");
500 else if (!strncmp(str, "hci", 3)) {
501 /* hci[,vlan=n] */
502 if (str[3]) {
503 if (!strncmp(str + 3, ",vlan=", 6)) {
504 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
505 if (*endp)
506 vlan = 0;
507 }
508 } else
509 vlan = qemu_find_bt_vlan(0);
510 if (vlan)
511 return bt_new_hci(vlan);
512 }
513
514 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
515
516 return 0;
517}
518
519static int bt_hci_parse(const char *str)
520{
521 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500522 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000523
524 if (nb_hcis >= MAX_NICS) {
525 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
526 return -1;
527 }
528
529 hci = hci_init(str);
530 if (!hci)
531 return -1;
532
533 bdaddr.b[0] = 0x52;
534 bdaddr.b[1] = 0x54;
535 bdaddr.b[2] = 0x00;
536 bdaddr.b[3] = 0x12;
537 bdaddr.b[4] = 0x34;
538 bdaddr.b[5] = 0x56 + nb_hcis;
539 hci->bdaddr_set(hci, bdaddr.b);
540
541 hci_table[nb_hcis++] = hci;
542
543 return 0;
544}
545
546static void bt_vhci_add(int vlan_id)
547{
548 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
549
550 if (!vlan->slave)
551 fprintf(stderr, "qemu: warning: adding a VHCI to "
552 "an empty scatternet %i\n", vlan_id);
553
554 bt_vhci_init(bt_new_hci(vlan));
555}
556
557static struct bt_device_s *bt_device_add(const char *opt)
558{
559 struct bt_scatternet_s *vlan;
560 int vlan_id = 0;
561 char *endp = strstr(opt, ",vlan=");
562 int len = (endp ? endp - opt : strlen(opt)) + 1;
563 char devname[10];
564
565 pstrcpy(devname, MIN(sizeof(devname), len), opt);
566
567 if (endp) {
568 vlan_id = strtol(endp + 6, &endp, 0);
569 if (*endp) {
570 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
571 return 0;
572 }
573 }
574
575 vlan = qemu_find_bt_vlan(vlan_id);
576
577 if (!vlan->slave)
578 fprintf(stderr, "qemu: warning: adding a slave device to "
579 "an empty scatternet %i\n", vlan_id);
580
581 if (!strcmp(devname, "keyboard"))
582 return bt_keyboard_init(vlan);
583
584 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
585 return 0;
586}
587
588static int bt_parse(const char *opt)
589{
590 const char *endp, *p;
591 int vlan;
592
593 if (strstart(opt, "hci", &endp)) {
594 if (!*endp || *endp == ',') {
595 if (*endp)
596 if (!strstart(endp, ",vlan=", 0))
597 opt = endp + 1;
598
599 return bt_hci_parse(opt);
600 }
601 } else if (strstart(opt, "vhci", &endp)) {
602 if (!*endp || *endp == ',') {
603 if (*endp) {
604 if (strstart(endp, ",vlan=", &p)) {
605 vlan = strtol(p, (char **) &endp, 0);
606 if (*endp) {
607 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
608 return 1;
609 }
610 } else {
611 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
612 return 1;
613 }
614 } else
615 vlan = 0;
616
617 bt_vhci_add(vlan);
618 return 0;
619 }
620 } else if (strstart(opt, "device:", &endp))
621 return !bt_device_add(endp);
622
623 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
624 return 1;
625}
626
balrog1ae26a12008-09-28 23:19:47 +0000627/***********************************************************/
628/* QEMU Block devices */
629
balrog609497a2008-01-14 02:56:53 +0000630#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +0000631#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +0000632#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +0000633#define PFLASH_ALIAS "if=pflash"
634#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +0000635#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +0000636
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200637static int drive_init_func(QemuOpts *opts, void *opaque)
638{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200639 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200640 int fatal_error = 0;
641
Markus Armbrustera803cb82010-06-02 13:31:55 +0200642 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200643 if (fatal_error)
644 return 1;
645 }
646 return 0;
647}
648
649static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
650{
651 if (NULL == qemu_opt_get(opts, "snapshot")) {
652 qemu_opt_set(opts, "snapshot", "on");
653 }
654 return 0;
655}
656
Jan Kiszka76e30d02009-07-02 00:19:02 +0200657void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
658{
659 boot_set_handler = func;
660 boot_set_opaque = opaque;
661}
662
663int qemu_boot_set(const char *boot_devices)
664{
665 if (!boot_set_handler) {
666 return -EINVAL;
667 }
668 return boot_set_handler(boot_set_opaque, boot_devices);
669}
670
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300671static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200672{
673 /* We just do some generic consistency checks */
674 const char *p;
675 int bitmap = 0;
676
677 for (p = devices; *p != '\0'; p++) {
678 /* Allowed boot devices are:
679 * a-b: floppy disk drives
680 * c-f: IDE disk drives
681 * g-m: machine implementation dependant drives
682 * n-p: network devices
683 * It's up to each machine implementation to check if the given boot
684 * devices match the actual hardware implementation and firmware
685 * features.
686 */
687 if (*p < 'a' || *p > 'p') {
688 fprintf(stderr, "Invalid boot device '%c'\n", *p);
689 exit(1);
690 }
691 if (bitmap & (1 << (*p - 'a'))) {
692 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
693 exit(1);
694 }
695 bitmap |= 1 << (*p - 'a');
696 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200697}
698
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200699static void restore_boot_devices(void *opaque)
700{
701 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600702 static int first = 1;
703
704 /* Restore boot order and remove ourselves after the first boot */
705 if (first) {
706 first = 0;
707 return;
708 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200709
710 qemu_boot_set(standard_boot_devices);
711
712 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
713 qemu_free(standard_boot_devices);
714}
715
aliguori268a3622009-04-21 22:30:27 +0000716static void numa_add(const char *optarg)
717{
718 char option[128];
719 char *endptr;
720 unsigned long long value, endvalue;
721 int nodenr;
722
723 optarg = get_opt_name(option, 128, optarg, ',') + 1;
724 if (!strcmp(option, "node")) {
725 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
726 nodenr = nb_numa_nodes;
727 } else {
728 nodenr = strtoull(option, NULL, 10);
729 }
730
731 if (get_param_value(option, 128, "mem", optarg) == 0) {
732 node_mem[nodenr] = 0;
733 } else {
734 value = strtoull(option, &endptr, 0);
735 switch (*endptr) {
736 case 0: case 'M': case 'm':
737 value <<= 20;
738 break;
739 case 'G': case 'g':
740 value <<= 30;
741 break;
742 }
743 node_mem[nodenr] = value;
744 }
745 if (get_param_value(option, 128, "cpus", optarg) == 0) {
746 node_cpumask[nodenr] = 0;
747 } else {
748 value = strtoull(option, &endptr, 10);
749 if (value >= 64) {
750 value = 63;
751 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
752 } else {
753 if (*endptr == '-') {
754 endvalue = strtoull(endptr+1, &endptr, 10);
755 if (endvalue >= 63) {
756 endvalue = 62;
757 fprintf(stderr,
758 "only 63 CPUs in NUMA mode supported.\n");
759 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100760 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000761 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100762 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000763 }
764 }
765 node_cpumask[nodenr] = value;
766 }
767 nb_numa_nodes++;
768 }
769 return;
770}
771
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200772static void smp_parse(const char *optarg)
773{
774 int smp, sockets = 0, threads = 0, cores = 0;
775 char *endptr;
776 char option[128];
777
778 smp = strtoul(optarg, &endptr, 10);
779 if (endptr != optarg) {
780 if (*endptr == ',') {
781 endptr++;
782 }
783 }
784 if (get_param_value(option, 128, "sockets", endptr) != 0)
785 sockets = strtoull(option, NULL, 10);
786 if (get_param_value(option, 128, "cores", endptr) != 0)
787 cores = strtoull(option, NULL, 10);
788 if (get_param_value(option, 128, "threads", endptr) != 0)
789 threads = strtoull(option, NULL, 10);
790 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
791 max_cpus = strtoull(option, NULL, 10);
792
793 /* compute missing values, prefer sockets over cores over threads */
794 if (smp == 0 || sockets == 0) {
795 sockets = sockets > 0 ? sockets : 1;
796 cores = cores > 0 ? cores : 1;
797 threads = threads > 0 ? threads : 1;
798 if (smp == 0) {
799 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200800 }
801 } else {
802 if (cores == 0) {
803 threads = threads > 0 ? threads : 1;
804 cores = smp / (sockets * threads);
805 } else {
Joel Schoppdca98162010-07-21 15:05:17 -0500806 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200807 }
808 }
809 smp_cpus = smp;
810 smp_cores = cores > 0 ? cores : 1;
811 smp_threads = threads > 0 ? threads : 1;
812 if (max_cpus == 0)
813 max_cpus = smp_cpus;
814}
815
bellard330d0412003-07-26 18:11:40 +0000816/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +0000817/* USB devices */
818
Markus Armbrusterfb080002010-05-28 15:38:44 +0200819static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +0000820{
821 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200822 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +0000823
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200824 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +0000825 return -1;
826
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +0100827 /* drivers with .usbdevice_name entry in USBDeviceInfo */
828 dev = usbdevice_create(devname);
829 if (dev)
830 goto done;
831
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200832 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +0000833 if (strstart(devname, "host:", &p)) {
834 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +0000835 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
836 dev = usb_bt_init(devname[2] ? hci_init(p) :
837 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +0000838 } else {
839 return -1;
840 }
pbrook0d92ed32006-05-21 16:30:15 +0000841 if (!dev)
842 return -1;
843
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200844done:
bellarda594cfb2005-11-06 16:13:29 +0000845 return 0;
846}
847
aliguori1f3870a2008-08-21 19:27:48 +0000848static int usb_device_del(const char *devname)
849{
850 int bus_num, addr;
851 const char *p;
852
aliguori5d0c5752008-09-14 01:07:41 +0000853 if (strstart(devname, "host:", &p))
854 return usb_host_device_close(p);
855
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200856 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +0000857 return -1;
858
859 p = strchr(devname, '.');
860 if (!p)
861 return -1;
862 bus_num = strtoul(devname, NULL, 0);
863 addr = strtoul(p + 1, NULL, 0);
864
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200865 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000866}
867
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200868static int usb_parse(const char *cmdline)
869{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800870 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +0200871 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800872 if (r < 0) {
873 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
874 }
875 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200876}
877
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300878void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000879{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800880 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +0200881 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100882 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800883 }
bellarda594cfb2005-11-06 16:13:29 +0000884}
885
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300886void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000887{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800888 const char *devname = qdict_get_str(qdict, "devname");
889 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100890 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800891 }
bellarda594cfb2005-11-06 16:13:29 +0000892}
893
bellardf7cce892004-12-08 22:21:25 +0000894/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +0000895/* PCMCIA/Cardbus */
896
897static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +0100898 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +0000899 struct pcmcia_socket_entry_s *next;
900} *pcmcia_sockets = 0;
901
Paul Brookbc24a222009-05-10 01:44:56 +0100902void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000903{
904 struct pcmcia_socket_entry_s *entry;
905
906 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
907 entry->socket = socket;
908 entry->next = pcmcia_sockets;
909 pcmcia_sockets = entry;
910}
911
Paul Brookbc24a222009-05-10 01:44:56 +0100912void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000913{
914 struct pcmcia_socket_entry_s *entry, **ptr;
915
916 ptr = &pcmcia_sockets;
917 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
918 if (entry->socket == socket) {
919 *ptr = entry->next;
920 qemu_free(entry);
921 }
922}
923
aliguori376253e2009-03-05 23:01:23 +0000924void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +0000925{
926 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +0000927
balrog201a51f2007-04-30 00:51:09 +0000928 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +0000929 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +0000930
931 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +0000932 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
933 iter->socket->attached ? iter->socket->card_string :
934 "Empty");
balrog201a51f2007-04-30 00:51:09 +0000935}
936
937/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +0000938/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +0000939
bellardc4b1fcc2004-03-14 21:44:30 +0000940typedef struct IOHandlerRecord {
941 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +0100942 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +0000943 IOHandler *fd_read;
944 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +0000945 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +0000946 void *opaque;
947 /* temporary data */
948 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +0100949 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +0000950} IOHandlerRecord;
951
Juan Quintela31d4ee62010-03-11 17:55:37 +0100952static QLIST_HEAD(, IOHandlerRecord) io_handlers =
953 QLIST_HEAD_INITIALIZER(io_handlers);
954
bellardc4b1fcc2004-03-14 21:44:30 +0000955
bellard7c9d8e02005-11-15 22:16:05 +0000956/* XXX: fd_read_poll should be suppressed, but an API change is
957 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +0000958int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +0100959 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +0000960 IOHandler *fd_read,
961 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +0000962 void *opaque)
bellardb4608c02003-06-27 17:34:32 +0000963{
Juan Quintela31d4ee62010-03-11 17:55:37 +0100964 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +0000965
bellard7c9d8e02005-11-15 22:16:05 +0000966 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100967 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000968 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +0000969 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +0000970 break;
971 }
bellard8a7ddc32004-03-31 19:00:16 +0000972 }
bellard7c9d8e02005-11-15 22:16:05 +0000973 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100974 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000975 if (ioh->fd == fd)
976 goto found;
977 }
978 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +0100979 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +0000980 found:
981 ioh->fd = fd;
982 ioh->fd_read_poll = fd_read_poll;
983 ioh->fd_read = fd_read;
984 ioh->fd_write = fd_write;
985 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +0000986 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +0000987 }
bellard7c9d8e02005-11-15 22:16:05 +0000988 return 0;
989}
990
ths5fafdf22007-09-16 21:08:06 +0000991int qemu_set_fd_handler(int fd,
992 IOHandler *fd_read,
993 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +0000994 void *opaque)
995{
996 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +0000997}
998
bellard8a7ddc32004-03-31 19:00:16 +0000999/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001000/* machine registration */
1001
blueswir1bdaf78e2008-10-04 07:24:27 +00001002static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001003QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001004
1005int qemu_register_machine(QEMUMachine *m)
1006{
1007 QEMUMachine **pm;
1008 pm = &first_machine;
1009 while (*pm != NULL)
1010 pm = &(*pm)->next;
1011 m->next = NULL;
1012 *pm = m;
1013 return 0;
1014}
1015
pbrook9596ebb2007-11-18 01:44:38 +00001016static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001017{
1018 QEMUMachine *m;
1019
1020 for(m = first_machine; m != NULL; m = m->next) {
1021 if (!strcmp(m->name, name))
1022 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001023 if (m->alias && !strcmp(m->alias, name))
1024 return m;
bellardcc1daa42005-06-05 14:49:17 +00001025 }
1026 return NULL;
1027}
1028
Anthony Liguori0c257432009-05-21 20:41:01 -05001029static QEMUMachine *find_default_machine(void)
1030{
1031 QEMUMachine *m;
1032
1033 for(m = first_machine; m != NULL; m = m->next) {
1034 if (m->is_default) {
1035 return m;
1036 }
1037 }
1038 return NULL;
1039}
1040
bellardcc1daa42005-06-05 14:49:17 +00001041/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001042/* main execution loop */
1043
pbrook9596ebb2007-11-18 01:44:38 +00001044static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001045{
aliguori7d957bd2009-01-15 22:14:11 +00001046 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001047 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001048 DisplayChangeListener *dcl = ds->listeners;
1049
Sheng Yang62a27442010-01-26 19:21:16 +08001050 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001051 dpy_refresh(ds);
1052
1053 while (dcl != NULL) {
1054 if (dcl->gui_timer_interval &&
1055 dcl->gui_timer_interval < interval)
1056 interval = dcl->gui_timer_interval;
1057 dcl = dcl->next;
1058 }
1059 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001060}
1061
blueswir19043b622009-01-21 19:28:13 +00001062static void nographic_update(void *opaque)
1063{
1064 uint64_t interval = GUI_REFRESH_INTERVAL;
1065
Sheng Yang62a27442010-01-26 19:21:16 +08001066 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00001067 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1068}
1069
bellard0bd48852005-11-11 00:00:47 +00001070struct vm_change_state_entry {
1071 VMChangeStateHandler *cb;
1072 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001073 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001074};
1075
Blue Swirl72cf2d42009-09-12 07:36:22 +00001076static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001077
1078VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1079 void *opaque)
1080{
1081 VMChangeStateEntry *e;
1082
1083 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001084
1085 e->cb = cb;
1086 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001087 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001088 return e;
1089}
1090
1091void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1092{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001093 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001094 qemu_free (e);
1095}
1096
Blue Swirl296af7c2010-03-29 19:23:50 +00001097void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001098{
1099 VMChangeStateEntry *e;
1100
1101 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001102 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001103 }
1104}
1105
bellard8a7ddc32004-03-31 19:00:16 +00001106void vm_start(void)
1107{
1108 if (!vm_running) {
1109 cpu_enable_ticks();
1110 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001111 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001112 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001113 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001114 }
1115}
1116
bellardbb0c6722004-06-20 12:37:32 +00001117/* reset/shutdown handler */
1118
1119typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001120 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001121 QEMUResetHandler *func;
1122 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001123} QEMUResetEntry;
1124
Blue Swirl72cf2d42009-09-12 07:36:22 +00001125static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1126 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001127static int reset_requested;
1128static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00001129static int powerdown_requested;
Blue Swirl296af7c2010-03-29 19:23:50 +00001130int debug_requested;
Blue Swirl7277e022010-04-12 17:19:06 +00001131int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001132
aurel32cf7a2fe2008-03-18 06:53:05 +00001133int qemu_shutdown_requested(void)
1134{
1135 int r = shutdown_requested;
1136 shutdown_requested = 0;
1137 return r;
1138}
1139
1140int qemu_reset_requested(void)
1141{
1142 int r = reset_requested;
1143 reset_requested = 0;
1144 return r;
1145}
1146
1147int qemu_powerdown_requested(void)
1148{
1149 int r = powerdown_requested;
1150 powerdown_requested = 0;
1151 return r;
1152}
1153
aliguorie5689022009-04-24 18:03:54 +00001154static int qemu_debug_requested(void)
1155{
1156 int r = debug_requested;
1157 debug_requested = 0;
1158 return r;
1159}
1160
aliguori6e29f5d2009-04-24 18:04:02 +00001161static int qemu_vmstop_requested(void)
1162{
1163 int r = vmstop_requested;
1164 vmstop_requested = 0;
1165 return r;
1166}
1167
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001168void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001169{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001170 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001171
bellardbb0c6722004-06-20 12:37:32 +00001172 re->func = func;
1173 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001174 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001175}
1176
Jan Kiszkadda9b292009-07-02 00:19:02 +02001177void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001178{
1179 QEMUResetEntry *re;
1180
Blue Swirl72cf2d42009-09-12 07:36:22 +00001181 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001182 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001183 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001184 qemu_free(re);
1185 return;
1186 }
1187 }
1188}
1189
1190void qemu_system_reset(void)
1191{
1192 QEMUResetEntry *re, *nre;
1193
1194 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001195 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001196 re->func(re->opaque);
1197 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001198 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001199 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001200}
1201
1202void qemu_system_reset_request(void)
1203{
bellardd1beab82006-10-02 19:44:22 +00001204 if (no_reboot) {
1205 shutdown_requested = 1;
1206 } else {
1207 reset_requested = 1;
1208 }
aliguorid9f75a42009-04-24 18:03:11 +00001209 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001210}
1211
1212void qemu_system_shutdown_request(void)
1213{
1214 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001215 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001216}
1217
bellard34751872005-07-02 14:31:34 +00001218void qemu_system_powerdown_request(void)
1219{
1220 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001221 qemu_notify_event();
1222}
1223
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001224void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001225{
1226 IOHandlerRecord *ioh;
1227 fd_set rfds, wfds, xfds;
1228 int ret, nfds;
1229 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001230 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001231
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001232 if (nonblocking)
1233 timeout = 0;
1234 else {
1235 timeout = qemu_calculate_timeout();
1236 qemu_bh_update_timeout(&timeout);
1237 }
aliguori56f3a5d2008-10-31 18:07:17 +00001238
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001239 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001240
bellardfd1dff42006-02-01 21:29:26 +00001241 /* poll any events */
1242 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001243 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001244 FD_ZERO(&rfds);
1245 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001246 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001247 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00001248 if (ioh->deleted)
1249 continue;
bellardfd1dff42006-02-01 21:29:26 +00001250 if (ioh->fd_read &&
1251 (!ioh->fd_read_poll ||
1252 ioh->fd_read_poll(ioh->opaque) != 0)) {
1253 FD_SET(ioh->fd, &rfds);
1254 if (ioh->fd > nfds)
1255 nfds = ioh->fd;
1256 }
1257 if (ioh->fd_write) {
1258 FD_SET(ioh->fd, &wfds);
1259 if (ioh->fd > nfds)
1260 nfds = ioh->fd;
1261 }
1262 }
ths3b46e622007-09-17 08:09:54 +00001263
aliguori56f3a5d2008-10-31 18:07:17 +00001264 tv.tv_sec = timeout / 1000;
1265 tv.tv_usec = (timeout % 1000) * 1000;
1266
Jan Kiszkad918f232009-06-24 14:42:30 +02001267 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1268
aliguori48708522009-04-24 18:03:49 +00001269 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001270 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001271 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00001272 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001273 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00001274
Juan Quintela31d4ee62010-03-11 17:55:37 +01001275 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
thscafffd42007-02-28 21:59:44 +00001276 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001277 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00001278 qemu_free(ioh);
Juan Quintela4bed9832010-03-11 17:55:41 +01001279 continue;
1280 }
1281 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1282 ioh->fd_read(ioh->opaque);
1283 }
1284 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1285 ioh->fd_write(ioh->opaque);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001286 }
thscafffd42007-02-28 21:59:44 +00001287 }
bellardfd1dff42006-02-01 21:29:26 +00001288 }
Jan Kiszkad918f232009-06-24 14:42:30 +02001289
1290 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001291
Paolo Bonzinib6964822010-03-10 11:38:45 +01001292 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001293
pbrook423f0742007-05-23 00:06:54 +00001294 /* Check bottom-halves last in case any of the earlier events triggered
1295 them. */
1296 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001297
bellard5905b2e2004-08-01 21:53:26 +00001298}
1299
aliguori43b96852009-04-24 18:03:33 +00001300static int vm_can_run(void)
1301{
1302 if (powerdown_requested)
1303 return 0;
1304 if (reset_requested)
1305 return 0;
1306 if (shutdown_requested)
1307 return 0;
aliguorie5689022009-04-24 18:03:54 +00001308 if (debug_requested)
1309 return 0;
aliguori43b96852009-04-24 18:03:33 +00001310 return 1;
1311}
1312
Blue Swirld9c32312009-08-09 08:42:19 +00001313qemu_irq qemu_system_powerdown;
1314
aliguori43b96852009-04-24 18:03:33 +00001315static void main_loop(void)
1316{
aliguori6e29f5d2009-04-24 18:04:02 +00001317 int r;
aliguori43b96852009-04-24 18:03:33 +00001318
Blue Swirl7277e022010-04-12 17:19:06 +00001319 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001320
aliguori6e29f5d2009-04-24 18:04:02 +00001321 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00001322 do {
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001323 bool nonblocking = false;
aliguorie6e35b12009-04-24 18:03:57 +00001324#ifdef CONFIG_PROFILER
1325 int64_t ti;
1326#endif
aliguorid6dc3d42009-04-24 18:04:07 +00001327#ifndef CONFIG_IOTHREAD
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001328 nonblocking = cpu_exec_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001329#endif
aliguori43b96852009-04-24 18:03:33 +00001330#ifdef CONFIG_PROFILER
1331 ti = profile_getclock();
1332#endif
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001333 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001334#ifdef CONFIG_PROFILER
1335 dev_time += profile_getclock() - ti;
1336#endif
aliguorie5689022009-04-24 18:03:54 +00001337 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00001338
Blue Swirl54fc6ea2010-03-29 19:23:46 +00001339 if ((r = qemu_debug_requested())) {
1340 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001341 }
aliguori43b96852009-04-24 18:03:33 +00001342 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001343 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001344 if (no_shutdown) {
1345 vm_stop(0);
1346 no_shutdown = 0;
1347 } else
1348 break;
1349 }
aliguorid6dc3d42009-04-24 18:04:07 +00001350 if (qemu_reset_requested()) {
1351 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00001352 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00001353 resume_all_vcpus();
1354 }
Blue Swirld9c32312009-08-09 08:42:19 +00001355 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001356 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001357 qemu_irq_raise(qemu_system_powerdown);
1358 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001359 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001360 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001361 }
aliguori43b96852009-04-24 18:03:33 +00001362 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001363 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001364 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001365}
1366
pbrook9bd7e6d2009-04-07 22:58:45 +00001367static void version(void)
1368{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001369 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001370}
1371
ths15f82202007-06-29 23:26:08 +00001372static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001373{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001374 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001375#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1376 opt_help
blueswir15824d652009-03-28 06:44:27 +00001377#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001378#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001379#undef DEF
1380#undef DEFHEADING
1381#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001382 ;
1383 version();
1384 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001385 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001386 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001387 "\n"
1388 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001389 "During emulation, the following keys are useful:\n"
1390 "ctrl-alt-f toggle full screen\n"
1391 "ctrl-alt-n switch to virtual console 'n'\n"
1392 "ctrl-alt toggle mouse and keyboard grab\n"
1393 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001394 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1395 "qemu",
1396 options_help);
ths15f82202007-06-29 23:26:08 +00001397 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001398}
1399
bellardcd6f1162004-05-13 22:02:20 +00001400#define HAS_ARG 0x0001
1401
bellardcd6f1162004-05-13 22:02:20 +00001402typedef struct QEMUOption {
1403 const char *name;
1404 int flags;
1405 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001406 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001407} QEMUOption;
1408
blueswir1dbed7e42008-10-01 19:38:09 +00001409static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001410 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1411#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1412 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001413#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001414#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001415#undef DEF
1416#undef DEFHEADING
1417#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001418 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001419};
malc3893c122008-09-28 00:42:05 +00001420static void select_vgahw (const char *p)
1421{
1422 const char *opts;
1423
Gerd Hoffmann64465292009-12-08 13:11:45 +01001424 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001425 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001426 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001427 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001428 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001429 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001430 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001431 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001432 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001433 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00001434 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001435 invalid_vga:
1436 fprintf(stderr, "Unknown vga type: %s\n", p);
1437 exit(1);
1438 }
malccb5a7aa2008-09-28 00:42:12 +00001439 while (*opts) {
1440 const char *nextopt;
1441
1442 if (strstart(opts, ",retrace=", &nextopt)) {
1443 opts = nextopt;
1444 if (strstart(opts, "dumb", &nextopt))
1445 vga_retrace_method = VGA_RETRACE_DUMB;
1446 else if (strstart(opts, "precise", &nextopt))
1447 vga_retrace_method = VGA_RETRACE_PRECISE;
1448 else goto invalid_vga;
1449 } else goto invalid_vga;
1450 opts = nextopt;
1451 }
malc3893c122008-09-28 00:42:05 +00001452}
1453
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001454static int balloon_parse(const char *arg)
1455{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001456 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001457
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001458 if (strcmp(arg, "none") == 0) {
1459 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001460 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001461
1462 if (!strncmp(arg, "virtio", 6)) {
1463 if (arg[6] == ',') {
1464 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001465 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001466 if (!opts)
1467 return -1;
1468 } else {
1469 /* create empty opts */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001470 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001471 }
1472 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1473 return 0;
1474 }
1475
1476 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001477}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001478
Paul Brook5cea8592009-05-30 00:52:44 +01001479char *qemu_find_file(int type, const char *name)
1480{
1481 int len;
1482 const char *subdir;
1483 char *buf;
1484
1485 /* If name contains path separators then try it as a straight path. */
1486 if ((strchr(name, '/') || strchr(name, '\\'))
1487 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02001488 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001489 }
1490 switch (type) {
1491 case QEMU_FILE_TYPE_BIOS:
1492 subdir = "";
1493 break;
1494 case QEMU_FILE_TYPE_KEYMAP:
1495 subdir = "keymaps/";
1496 break;
1497 default:
1498 abort();
1499 }
1500 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1501 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001502 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001503 if (access(buf, R_OK)) {
1504 qemu_free(buf);
1505 return NULL;
1506 }
1507 return buf;
1508}
1509
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001510static int device_help_func(QemuOpts *opts, void *opaque)
1511{
1512 return qdev_device_help(opts);
1513}
1514
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001515static int device_init_func(QemuOpts *opts, void *opaque)
1516{
1517 DeviceState *dev;
1518
1519 dev = qdev_device_add(opts);
1520 if (!dev)
1521 return -1;
1522 return 0;
1523}
1524
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001525static int chardev_init_func(QemuOpts *opts, void *opaque)
1526{
1527 CharDriverState *chr;
1528
1529 chr = qemu_chr_open_opts(opts, NULL);
1530 if (!chr)
1531 return -1;
1532 return 0;
1533}
1534
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07001535#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301536static int fsdev_init_func(QemuOpts *opts, void *opaque)
1537{
1538 int ret;
1539 ret = qemu_fsdev_add(opts);
1540
1541 return ret;
1542}
1543#endif
1544
Gerd Hoffmann88589342009-12-08 13:11:50 +01001545static int mon_init_func(QemuOpts *opts, void *opaque)
1546{
1547 CharDriverState *chr;
1548 const char *chardev;
1549 const char *mode;
1550 int flags;
1551
1552 mode = qemu_opt_get(opts, "mode");
1553 if (mode == NULL) {
1554 mode = "readline";
1555 }
1556 if (strcmp(mode, "readline") == 0) {
1557 flags = MONITOR_USE_READLINE;
1558 } else if (strcmp(mode, "control") == 0) {
1559 flags = MONITOR_USE_CONTROL;
1560 } else {
1561 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1562 exit(1);
1563 }
1564
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01001565 if (qemu_opt_get_bool(opts, "pretty", 0))
1566 flags |= MONITOR_USE_PRETTY;
1567
Gerd Hoffmann88589342009-12-08 13:11:50 +01001568 if (qemu_opt_get_bool(opts, "default", 0))
1569 flags |= MONITOR_IS_DEFAULT;
1570
1571 chardev = qemu_opt_get(opts, "chardev");
1572 chr = qemu_chr_find(chardev);
1573 if (chr == NULL) {
1574 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1575 exit(1);
1576 }
1577
1578 monitor_init(chr, flags);
1579 return 0;
1580}
1581
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001582static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001583{
1584 static int monitor_device_index = 0;
1585 QemuOpts *opts;
1586 const char *p;
1587 char label[32];
1588 int def = 0;
1589
1590 if (strstart(optarg, "chardev:", &p)) {
1591 snprintf(label, sizeof(label), "%s", p);
1592 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001593 snprintf(label, sizeof(label), "compat_monitor%d",
1594 monitor_device_index);
1595 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001596 def = 1;
1597 }
1598 opts = qemu_chr_parse_compat(label, optarg);
1599 if (!opts) {
1600 fprintf(stderr, "parse error: %s\n", optarg);
1601 exit(1);
1602 }
1603 }
1604
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001605 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001606 if (!opts) {
1607 fprintf(stderr, "duplicate chardev: %s\n", label);
1608 exit(1);
1609 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001610 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001611 qemu_opt_set(opts, "chardev", label);
1612 if (def)
1613 qemu_opt_set(opts, "default", "on");
1614 monitor_device_index++;
1615}
1616
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001617struct device_config {
1618 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001619 DEV_USB, /* -usbdevice */
1620 DEV_BT, /* -bt */
1621 DEV_SERIAL, /* -serial */
1622 DEV_PARALLEL, /* -parallel */
1623 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001624 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001625 } type;
1626 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001627 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001628};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001629QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001630
1631static void add_device_config(int type, const char *cmdline)
1632{
1633 struct device_config *conf;
1634
1635 conf = qemu_mallocz(sizeof(*conf));
1636 conf->type = type;
1637 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001638 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001639}
1640
1641static int foreach_device_config(int type, int (*func)(const char *cmdline))
1642{
1643 struct device_config *conf;
1644 int rc;
1645
Blue Swirl72cf2d42009-09-12 07:36:22 +00001646 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001647 if (conf->type != type)
1648 continue;
1649 rc = func(conf->cmdline);
1650 if (0 != rc)
1651 return rc;
1652 }
1653 return 0;
1654}
1655
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001656static int serial_parse(const char *devname)
1657{
1658 static int index = 0;
1659 char label[32];
1660
1661 if (strcmp(devname, "none") == 0)
1662 return 0;
1663 if (index == MAX_SERIAL_PORTS) {
1664 fprintf(stderr, "qemu: too many serial ports\n");
1665 exit(1);
1666 }
1667 snprintf(label, sizeof(label), "serial%d", index);
1668 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1669 if (!serial_hds[index]) {
1670 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1671 devname, strerror(errno));
1672 return -1;
1673 }
1674 index++;
1675 return 0;
1676}
1677
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01001678static int parallel_parse(const char *devname)
1679{
1680 static int index = 0;
1681 char label[32];
1682
1683 if (strcmp(devname, "none") == 0)
1684 return 0;
1685 if (index == MAX_PARALLEL_PORTS) {
1686 fprintf(stderr, "qemu: too many parallel ports\n");
1687 exit(1);
1688 }
1689 snprintf(label, sizeof(label), "parallel%d", index);
1690 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1691 if (!parallel_hds[index]) {
1692 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1693 devname, strerror(errno));
1694 return -1;
1695 }
1696 index++;
1697 return 0;
1698}
1699
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001700static int virtcon_parse(const char *devname)
1701{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001702 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001703 static int index = 0;
1704 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05301705 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001706
1707 if (strcmp(devname, "none") == 0)
1708 return 0;
1709 if (index == MAX_VIRTIO_CONSOLES) {
1710 fprintf(stderr, "qemu: too many virtio consoles\n");
1711 exit(1);
1712 }
Amit Shah392ecf52010-01-21 16:19:23 +05301713
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001714 bus_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301715 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1716
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001717 dev_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301718 qemu_opt_set(dev_opts, "driver", "virtconsole");
1719
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001720 snprintf(label, sizeof(label), "virtcon%d", index);
1721 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1722 if (!virtcon_hds[index]) {
1723 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1724 devname, strerror(errno));
1725 return -1;
1726 }
Amit Shah392ecf52010-01-21 16:19:23 +05301727 qemu_opt_set(dev_opts, "chardev", label);
1728
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001729 index++;
1730 return 0;
1731}
1732
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001733static int debugcon_parse(const char *devname)
1734{
1735 QemuOpts *opts;
1736
1737 if (!qemu_chr_open("debugcon", devname, NULL)) {
1738 exit(1);
1739 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001740 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001741 if (!opts) {
1742 fprintf(stderr, "qemu: already have a debugcon device\n");
1743 exit(1);
1744 }
1745 qemu_opt_set(opts, "driver", "isa-debugcon");
1746 qemu_opt_set(opts, "chardev", "debugcon");
1747 return 0;
1748}
1749
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001750void qemu_add_exit_notifier(Notifier *notify)
1751{
1752 notifier_list_add(&exit_notifiers, notify);
1753}
1754
1755void qemu_remove_exit_notifier(Notifier *notify)
1756{
1757 notifier_list_remove(&exit_notifiers, notify);
1758}
1759
1760static void qemu_run_exit_notifiers(void)
1761{
1762 notifier_list_notify(&exit_notifiers);
1763}
1764
Anthony Liguori6530a972010-01-22 09:18:06 -06001765static const QEMUOption *lookup_opt(int argc, char **argv,
1766 const char **poptarg, int *poptind)
1767{
1768 const QEMUOption *popt;
1769 int optind = *poptind;
1770 char *r = argv[optind];
1771 const char *optarg;
1772
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001773 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06001774 optind++;
1775 /* Treat --foo the same as -foo. */
1776 if (r[1] == '-')
1777 r++;
1778 popt = qemu_options;
1779 for(;;) {
1780 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001781 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06001782 exit(1);
1783 }
1784 if (!strcmp(popt->name, r + 1))
1785 break;
1786 popt++;
1787 }
1788 if (popt->flags & HAS_ARG) {
1789 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001790 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06001791 exit(1);
1792 }
1793 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001794 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06001795 } else {
1796 optarg = NULL;
1797 }
1798
1799 *poptarg = optarg;
1800 *poptind = optind;
1801
1802 return popt;
1803}
1804
malc902b3d52008-12-10 19:18:40 +00001805int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00001806{
aliguori59030a82009-04-05 18:43:41 +00001807 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00001808 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03001809 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01001810 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00001811 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00001812 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05001813 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00001814 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00001815 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00001816 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001817 QemuOpts *hda_opts = NULL, *opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02001818 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00001819 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06001820 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00001821 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001822 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00001823 const char *cpu_model;
bellard26a5f132008-05-28 12:30:31 +00001824 int tb_size;
ths93815bc2007-03-19 15:58:31 +00001825 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00001826 const char *incoming = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05001827 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06001828 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00001829
Prerna Saxenaab6540d2010-08-09 11:48:32 +01001830#ifdef CONFIG_SIMPLE_TRACE
1831 const char *trace_file = NULL;
1832#endif
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001833 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01001834 error_set_progname(argv[0]);
1835
Jan Kiszka68752042009-09-15 13:36:04 +02001836 init_clocks();
1837
malc902b3d52008-12-10 19:18:40 +00001838 qemu_cache_utils_init(envp);
1839
Blue Swirl72cf2d42009-09-12 07:36:22 +00001840 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02001841 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00001842
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05001843 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05001844 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00001845 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00001846 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00001847 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00001848 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00001849 kernel_filename = NULL;
1850 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00001851 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00001852 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00001853
aliguori268a3622009-04-21 22:30:27 +00001854 for (i = 0; i < MAX_NODES; i++) {
1855 node_mem[i] = 0;
1856 node_cpumask[i] = 0;
1857 }
1858
aliguori268a3622009-04-21 22:30:27 +00001859 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00001860 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00001861
bellard26a5f132008-05-28 12:30:31 +00001862 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00001863 autostart= 1;
1864
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001865#ifdef CONFIG_VIRTFS
1866 qemu_add_opts(&qemu_fsdev_opts);
1867 qemu_add_opts(&qemu_virtfs_opts);
1868#endif
1869
Anthony Liguori292444c2010-01-21 10:57:58 -06001870 /* first pass of option parsing */
1871 optind = 1;
1872 while (optind < argc) {
1873 if (argv[optind][0] != '-') {
1874 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06001875 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06001876 continue;
1877 } else {
1878 const QEMUOption *popt;
1879
1880 popt = lookup_opt(argc, argv, &optarg, &optind);
1881 switch (popt->index) {
1882 case QEMU_OPTION_nodefconfig:
1883 defconfig=0;
1884 break;
1885 }
1886 }
1887 }
1888
1889 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001890 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01001891
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001892 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02001893 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001894 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001895 }
1896
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001897 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02001898 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001899 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001900 }
1901 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00001902 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06001903
1904 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00001905 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00001906 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00001907 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00001908 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06001909 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001910 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00001911 } else {
1912 const QEMUOption *popt;
1913
Anthony Liguori6530a972010-01-22 09:18:06 -06001914 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00001915 if (!(popt->arch_mask & arch_type)) {
1916 printf("Option %s not supported for this target\n", popt->name);
1917 exit(1);
1918 }
bellardcd6f1162004-05-13 22:02:20 +00001919 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00001920 case QEMU_OPTION_M:
1921 machine = find_machine(optarg);
1922 if (!machine) {
1923 QEMUMachine *m;
1924 printf("Supported machines are:\n");
1925 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001926 if (m->alias)
1927 printf("%-10s %s (alias of %s)\n",
1928 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00001929 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00001930 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05001931 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00001932 }
ths15f82202007-06-29 23:26:08 +00001933 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00001934 }
1935 break;
j_mayer94fc95c2007-03-05 19:44:02 +00001936 case QEMU_OPTION_cpu:
1937 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00001938 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00001939 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00001940 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00001941 } else {
1942 cpu_model = optarg;
1943 }
1944 break;
bellardcd6f1162004-05-13 22:02:20 +00001945 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00001946 initrd_filename = optarg;
1947 break;
bellardcd6f1162004-05-13 22:02:20 +00001948 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00001949 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001950 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00001951 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001952 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00001953 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00001954 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00001955 translation == BIOS_ATA_TRANSLATION_LBA ?
1956 ",trans=lba" :
1957 translation == BIOS_ATA_TRANSLATION_NONE ?
1958 ",trans=none" : "");
1959 break;
bellardcd6f1162004-05-13 22:02:20 +00001960 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00001961 case QEMU_OPTION_hdc:
1962 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00001963 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00001964 break;
thse4bcb142007-12-02 04:51:10 +00001965 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00001966 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00001967 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02001968 case QEMU_OPTION_set:
1969 if (qemu_set_option(optarg) != 0)
1970 exit(1);
1971 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01001972 case QEMU_OPTION_global:
1973 if (qemu_global_option(optarg) != 0)
1974 exit(1);
1975 break;
balrog3e3d5812007-04-30 02:09:25 +00001976 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00001977 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00001978 break;
pbrooka1bb27b2007-04-06 16:49:48 +00001979 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00001980 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00001981 break;
j_mayer86f55662007-04-24 06:52:59 +00001982 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00001983 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00001984 break;
bellardcd6f1162004-05-13 22:02:20 +00001985 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00001986 snapshot = 1;
1987 break;
bellardcd6f1162004-05-13 22:02:20 +00001988 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00001989 {
bellard330d0412003-07-26 18:11:40 +00001990 const char *p;
1991 p = optarg;
1992 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001993 if (cyls < 1 || cyls > 16383)
1994 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00001995 if (*p != ',')
1996 goto chs_fail;
1997 p++;
1998 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001999 if (heads < 1 || heads > 16)
2000 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002001 if (*p != ',')
2002 goto chs_fail;
2003 p++;
2004 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002005 if (secs < 1 || secs > 63)
2006 goto chs_fail;
2007 if (*p == ',') {
2008 p++;
2009 if (!strcmp(p, "none"))
2010 translation = BIOS_ATA_TRANSLATION_NONE;
2011 else if (!strcmp(p, "lba"))
2012 translation = BIOS_ATA_TRANSLATION_LBA;
2013 else if (!strcmp(p, "auto"))
2014 translation = BIOS_ATA_TRANSLATION_AUTO;
2015 else
2016 goto chs_fail;
2017 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002018 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002019 fprintf(stderr, "qemu: invalid physical CHS format\n");
2020 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002021 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002022 if (hda_opts != NULL) {
2023 char num[16];
2024 snprintf(num, sizeof(num), "%d", cyls);
2025 qemu_opt_set(hda_opts, "cyls", num);
2026 snprintf(num, sizeof(num), "%d", heads);
2027 qemu_opt_set(hda_opts, "heads", num);
2028 snprintf(num, sizeof(num), "%d", secs);
2029 qemu_opt_set(hda_opts, "secs", num);
2030 if (translation == BIOS_ATA_TRANSLATION_LBA)
2031 qemu_opt_set(hda_opts, "trans", "lba");
2032 if (translation == BIOS_ATA_TRANSLATION_NONE)
2033 qemu_opt_set(hda_opts, "trans", "none");
2034 }
bellard330d0412003-07-26 18:11:40 +00002035 }
2036 break;
aliguori268a3622009-04-21 22:30:27 +00002037 case QEMU_OPTION_numa:
2038 if (nb_numa_nodes >= MAX_NODES) {
2039 fprintf(stderr, "qemu: too many NUMA nodes\n");
2040 exit(1);
2041 }
2042 numa_add(optarg);
2043 break;
bellardcd6f1162004-05-13 22:02:20 +00002044 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002045 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002046 break;
balrog4d3b6f62008-02-10 16:33:14 +00002047#ifdef CONFIG_CURSES
2048 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002049 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00002050 break;
2051#endif
balroga171fe32007-04-30 01:48:07 +00002052 case QEMU_OPTION_portrait:
2053 graphic_rotate = 1;
2054 break;
bellardcd6f1162004-05-13 22:02:20 +00002055 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002056 kernel_filename = optarg;
2057 break;
bellardcd6f1162004-05-13 22:02:20 +00002058 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002059 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002060 break;
bellardcd6f1162004-05-13 22:02:20 +00002061 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00002062 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00002063 break;
bellardcd6f1162004-05-13 22:02:20 +00002064 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002065 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002066 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002067 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002068 };
2069 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002070 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002071 int legacy = 0;
2072
2073 if (!strchr(optarg, '=')) {
2074 legacy = 1;
2075 pstrcpy(buf, sizeof(buf), optarg);
2076 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2077 fprintf(stderr,
2078 "qemu: unknown boot parameter '%s' in '%s'\n",
2079 buf, optarg);
2080 exit(1);
2081 }
2082
2083 if (legacy ||
2084 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002085 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002086 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002087 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002088 if (!legacy) {
2089 if (get_param_value(buf, sizeof(buf),
2090 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002091 validate_bootdevices(buf);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002092 standard_boot_devices = qemu_strdup(boot_devices);
2093 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2094 qemu_register_reset(restore_boot_devices,
2095 standard_boot_devices);
2096 }
Jan Kiszka95387492009-07-02 00:19:02 +02002097 if (get_param_value(buf, sizeof(buf),
2098 "menu", optarg)) {
2099 if (!strcmp(buf, "on")) {
2100 boot_menu = 1;
2101 } else if (!strcmp(buf, "off")) {
2102 boot_menu = 0;
2103 } else {
2104 fprintf(stderr,
2105 "qemu: invalid option value '%s'\n",
2106 buf);
2107 exit(1);
2108 }
2109 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002110 }
bellard36b486b2003-11-11 13:36:08 +00002111 }
2112 break;
bellardcd6f1162004-05-13 22:02:20 +00002113 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002114 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00002115 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00002116 break;
bellard52ca8d62006-06-14 16:03:05 +00002117 case QEMU_OPTION_no_fd_bootchk:
2118 fd_bootchk = 0;
2119 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002120 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002121 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002122 exit(1);
2123 }
2124 break;
bellard7c9d8e02005-11-15 22:16:05 +00002125 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002126 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002127 exit(1);
2128 }
bellard702c6512004-04-02 21:21:32 +00002129 break;
bellardc7f74642004-08-24 21:57:12 +00002130#ifdef CONFIG_SLIRP
2131 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002132 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002133 break;
ths47d5d012007-02-20 00:05:08 +00002134 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002135 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002136 break;
bellard9bf05442004-08-25 22:12:49 +00002137 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002138 if (net_slirp_redir(optarg) < 0)
2139 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002140 break;
bellardc7f74642004-08-24 21:57:12 +00002141#endif
balrogdc72ac12008-11-09 00:04:26 +00002142 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002143 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002144 break;
bellard1d14ffa2005-10-30 18:58:22 +00002145 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002146 if (!(audio_available())) {
2147 printf("Option %s not supported for this target\n", popt->name);
2148 exit(1);
2149 }
bellard1d14ffa2005-10-30 18:58:22 +00002150 AUD_help ();
2151 exit (0);
2152 break;
2153 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002154 if (!(audio_available())) {
2155 printf("Option %s not supported for this target\n", popt->name);
2156 exit(1);
2157 }
bellard1d14ffa2005-10-30 18:58:22 +00002158 select_soundhw (optarg);
2159 break;
bellardcd6f1162004-05-13 22:02:20 +00002160 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002161 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002162 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002163 case QEMU_OPTION_version:
2164 version();
2165 exit(0);
2166 break;
aurel3200f82b82008-04-27 21:12:55 +00002167 case QEMU_OPTION_m: {
2168 uint64_t value;
2169 char *ptr;
2170
2171 value = strtoul(optarg, &ptr, 10);
2172 switch (*ptr) {
2173 case 0: case 'M': case 'm':
2174 value <<= 20;
2175 break;
2176 case 'G': case 'g':
2177 value <<= 30;
2178 break;
2179 default:
2180 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002181 exit(1);
2182 }
aurel3200f82b82008-04-27 21:12:55 +00002183
2184 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002185 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00002186 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2187 exit(1);
2188 }
Anthony Liguoric227f092009-10-01 16:12:16 -05002189 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002190 fprintf(stderr, "qemu: ram size too large\n");
2191 exit(1);
2192 }
2193 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002194 break;
aurel3200f82b82008-04-27 21:12:55 +00002195 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002196 case QEMU_OPTION_mempath:
2197 mem_path = optarg;
2198 break;
2199#ifdef MAP_POPULATE
2200 case QEMU_OPTION_mem_prealloc:
2201 mem_prealloc = 1;
2202 break;
2203#endif
bellardcd6f1162004-05-13 22:02:20 +00002204 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002205 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00002206 break;
bellardcd6f1162004-05-13 22:02:20 +00002207 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002208 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002209 break;
aliguori59030a82009-04-05 18:43:41 +00002210 case QEMU_OPTION_gdb:
2211 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002212 break;
bellardcd6f1162004-05-13 22:02:20 +00002213 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002214 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002215 break;
j_mayer1192dad2007-10-05 13:08:35 +00002216 case QEMU_OPTION_bios:
2217 bios_name = optarg;
2218 break;
aurel321b530a62009-04-05 20:08:59 +00002219 case QEMU_OPTION_singlestep:
2220 singlestep = 1;
2221 break;
bellardcd6f1162004-05-13 22:02:20 +00002222 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002223 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002224 break;
bellard3d11d0e2004-12-12 16:56:30 +00002225 case QEMU_OPTION_k:
2226 keyboard_layout = optarg;
2227 break;
bellardee22c2f2004-06-03 12:49:50 +00002228 case QEMU_OPTION_localtime:
2229 rtc_utc = 0;
2230 break;
malc3893c122008-09-28 00:42:05 +00002231 case QEMU_OPTION_vga:
2232 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002233 break;
bellarde9b137c2004-06-21 16:46:10 +00002234 case QEMU_OPTION_g:
2235 {
2236 const char *p;
2237 int w, h, depth;
2238 p = optarg;
2239 w = strtol(p, (char **)&p, 10);
2240 if (w <= 0) {
2241 graphic_error:
2242 fprintf(stderr, "qemu: invalid resolution or depth\n");
2243 exit(1);
2244 }
2245 if (*p != 'x')
2246 goto graphic_error;
2247 p++;
2248 h = strtol(p, (char **)&p, 10);
2249 if (h <= 0)
2250 goto graphic_error;
2251 if (*p == 'x') {
2252 p++;
2253 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002254 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002255 depth != 24 && depth != 32)
2256 goto graphic_error;
2257 } else if (*p == '\0') {
2258 depth = graphic_depth;
2259 } else {
2260 goto graphic_error;
2261 }
ths3b46e622007-09-17 08:09:54 +00002262
bellarde9b137c2004-06-21 16:46:10 +00002263 graphic_width = w;
2264 graphic_height = h;
2265 graphic_depth = depth;
2266 }
2267 break;
ths20d8a3e2007-02-18 17:04:49 +00002268 case QEMU_OPTION_echr:
2269 {
2270 char *r;
2271 term_escape_char = strtol(optarg, &r, 0);
2272 if (r == optarg)
2273 printf("Bad argument to echr\n");
2274 break;
2275 }
bellard82c643f2004-07-14 17:28:13 +00002276 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002277 monitor_parse(optarg, "readline");
2278 default_monitor = 0;
2279 break;
2280 case QEMU_OPTION_qmp:
2281 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002282 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002283 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002284 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002285 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002286 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002287 exit(1);
2288 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002289 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002290 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002291 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002292 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002293 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002294 exit(1);
2295 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002296 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302297 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002298 olist = qemu_find_opts("fsdev");
2299 if (!olist) {
2300 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2301 exit(1);
2302 }
2303 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302304 if (!opts) {
2305 fprintf(stderr, "parse error: %s\n", optarg);
2306 exit(1);
2307 }
2308 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302309 case QEMU_OPTION_virtfs: {
2310 char *arg_fsdev = NULL;
2311 char *arg_9p = NULL;
2312 int len = 0;
2313
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002314 olist = qemu_find_opts("virtfs");
2315 if (!olist) {
2316 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2317 exit(1);
2318 }
2319 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302320 if (!opts) {
2321 fprintf(stderr, "parse error: %s\n", optarg);
2322 exit(1);
2323 }
2324
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002325 if (qemu_opt_get(opts, "fstype") == NULL ||
2326 qemu_opt_get(opts, "mount_tag") == NULL ||
2327 qemu_opt_get(opts, "path") == NULL ||
2328 qemu_opt_get(opts, "security_model") == NULL) {
2329 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +05302330 "security_model=[mapped|passthrough|none],"
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002331 "mnt_tag=tag.\n");
2332 exit(1);
2333 }
2334
2335 len = strlen(",id=,path=,security_model=");
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302336 len += strlen(qemu_opt_get(opts, "fstype"));
2337 len += strlen(qemu_opt_get(opts, "mount_tag"));
2338 len += strlen(qemu_opt_get(opts, "path"));
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002339 len += strlen(qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302340 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2341
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302342 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002343 "%s,id=%s,path=%s,security_model=%s",
2344 qemu_opt_get(opts, "fstype"),
2345 qemu_opt_get(opts, "mount_tag"),
2346 qemu_opt_get(opts, "path"),
2347 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302348
2349 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2350 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2351 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2352
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302353 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002354 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2355 qemu_opt_get(opts, "mount_tag"),
2356 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302357
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002358 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302359 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2360 exit(1);
2361 }
2362
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002363 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302364 fprintf(stderr, "parse error [device]: %s\n", optarg);
2365 exit(1);
2366 }
2367
2368 qemu_free(arg_fsdev);
2369 qemu_free(arg_9p);
2370 break;
2371 }
bellard82c643f2004-07-14 17:28:13 +00002372 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002373 add_device_config(DEV_SERIAL, optarg);
2374 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002375 if (strncmp(optarg, "mon:", 4) == 0) {
2376 default_monitor = 0;
2377 }
bellard82c643f2004-07-14 17:28:13 +00002378 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002379 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002380 if (watchdog) {
2381 fprintf(stderr,
2382 "qemu: only one watchdog option may be given\n");
2383 return 1;
2384 }
2385 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002386 break;
2387 case QEMU_OPTION_watchdog_action:
2388 if (select_watchdog_action(optarg) == -1) {
2389 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2390 exit(1);
2391 }
2392 break;
aliguori51ecf132009-01-15 20:06:40 +00002393 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002394 add_device_config(DEV_VIRTCON, optarg);
2395 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002396 if (strncmp(optarg, "mon:", 4) == 0) {
2397 default_monitor = 0;
2398 }
aliguori51ecf132009-01-15 20:06:40 +00002399 break;
bellard6508fe52005-01-15 12:02:56 +00002400 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002401 add_device_config(DEV_PARALLEL, optarg);
2402 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002403 if (strncmp(optarg, "mon:", 4) == 0) {
2404 default_monitor = 0;
2405 }
bellard6508fe52005-01-15 12:02:56 +00002406 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002407 case QEMU_OPTION_debugcon:
2408 add_device_config(DEV_DEBUGCON, optarg);
2409 break;
bellardd63d3072004-10-03 13:29:03 +00002410 case QEMU_OPTION_loadvm:
2411 loadvm = optarg;
2412 break;
2413 case QEMU_OPTION_full_screen:
2414 full_screen = 1;
2415 break;
ths667acca2006-12-11 02:08:05 +00002416#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002417 case QEMU_OPTION_no_frame:
2418 no_frame = 1;
2419 break;
ths3780e192007-06-21 21:08:02 +00002420 case QEMU_OPTION_alt_grab:
2421 alt_grab = 1;
2422 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002423 case QEMU_OPTION_ctrl_grab:
2424 ctrl_grab = 1;
2425 break;
ths667acca2006-12-11 02:08:05 +00002426 case QEMU_OPTION_no_quit:
2427 no_quit = 1;
2428 break;
aliguori7d957bd2009-01-15 22:14:11 +00002429 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002430 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002431 break;
ths667acca2006-12-11 02:08:05 +00002432#endif
bellardf7cce892004-12-08 22:21:25 +00002433 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002434 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002435 break;
bellarda09db212005-04-30 16:10:35 +00002436 case QEMU_OPTION_win2k_hack:
2437 win2k_install_hack = 1;
2438 break;
aliguori73822ec2009-01-15 20:11:34 +00002439 case QEMU_OPTION_rtc_td_hack:
2440 rtc_td_hack = 1;
2441 break;
aliguori8a92ea22009-02-27 20:12:36 +00002442 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002443 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002444 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002445 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002446 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002447 break;
aliguori7ba1e612008-11-05 16:04:33 +00002448 case QEMU_OPTION_enable_kvm:
2449 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00002450 break;
bellardbb36d472005-11-05 14:22:28 +00002451 case QEMU_OPTION_usb:
2452 usb_enabled = 1;
2453 break;
bellarda594cfb2005-11-06 16:13:29 +00002454 case QEMU_OPTION_usbdevice:
2455 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002456 add_device_config(DEV_USB, optarg);
2457 break;
2458 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002459 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002460 exit(1);
2461 }
bellarda594cfb2005-11-06 16:13:29 +00002462 break;
bellard6a00d602005-11-21 23:25:50 +00002463 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002464 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002465 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002466 fprintf(stderr, "Invalid number of CPUs\n");
2467 exit(1);
2468 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002469 if (max_cpus < smp_cpus) {
2470 fprintf(stderr, "maxcpus must be equal to or greater than "
2471 "smp\n");
2472 exit(1);
2473 }
2474 if (max_cpus > 255) {
2475 fprintf(stderr, "Unsupported number of maxcpus\n");
2476 exit(1);
2477 }
bellard6a00d602005-11-21 23:25:50 +00002478 break;
bellard24236862006-04-30 21:28:36 +00002479 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002480 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00002481 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00002482 break;
bellard6515b202006-05-03 22:02:44 +00002483 case QEMU_OPTION_no_acpi:
2484 acpi_enabled = 0;
2485 break;
aliguori16b29ae2008-12-17 23:28:44 +00002486 case QEMU_OPTION_no_hpet:
2487 no_hpet = 1;
2488 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002489 case QEMU_OPTION_balloon:
2490 if (balloon_parse(optarg) < 0) {
2491 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2492 exit(1);
2493 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002494 break;
bellardd1beab82006-10-02 19:44:22 +00002495 case QEMU_OPTION_no_reboot:
2496 no_reboot = 1;
2497 break;
aurel32b2f76162008-04-11 21:35:52 +00002498 case QEMU_OPTION_no_shutdown:
2499 no_shutdown = 1;
2500 break;
balrog9467cd42007-05-01 01:34:14 +00002501 case QEMU_OPTION_show_cursor:
2502 cursor_hide = 0;
2503 break;
blueswir18fcb1b92008-09-18 18:29:08 +00002504 case QEMU_OPTION_uuid:
2505 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2506 fprintf(stderr, "Fail to parse UUID string."
2507 " Wrong format.\n");
2508 exit(1);
2509 }
2510 break;
ths9ae02552007-01-05 17:39:04 +00002511 case QEMU_OPTION_option_rom:
2512 if (nb_option_roms >= MAX_OPTION_ROMS) {
2513 fprintf(stderr, "Too many option ROMs\n");
2514 exit(1);
2515 }
2516 option_rom[nb_option_roms] = optarg;
2517 nb_option_roms++;
2518 break;
pbrook8e716212007-01-20 17:12:09 +00002519 case QEMU_OPTION_semihosting:
2520 semihosting_enabled = 1;
2521 break;
thsc35734b2007-03-19 15:17:08 +00002522 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02002523 qemu_name = qemu_strdup(optarg);
2524 {
2525 char *p = strchr(qemu_name, ',');
2526 if (p != NULL) {
2527 *p++ = 0;
2528 if (strncmp(p, "process=", 8)) {
2529 fprintf(stderr, "Unknown subargument %s to -name", p);
2530 exit(1);
2531 }
2532 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02002533 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02002534 }
2535 }
thsc35734b2007-03-19 15:17:08 +00002536 break;
blueswir166508602007-05-01 14:16:52 +00002537 case QEMU_OPTION_prom_env:
2538 if (nb_prom_envs >= MAX_PROM_ENVS) {
2539 fprintf(stderr, "Too many prom variables\n");
2540 exit(1);
2541 }
2542 prom_envs[nb_prom_envs] = optarg;
2543 nb_prom_envs++;
2544 break;
balrog2b8f2d42007-07-27 22:08:46 +00002545 case QEMU_OPTION_old_param:
2546 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00002547 break;
thsf3dcfad2007-08-24 01:26:02 +00002548 case QEMU_OPTION_clock:
2549 configure_alarms(optarg);
2550 break;
bellard7e0af5d02007-11-07 16:24:33 +00002551 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002552 configure_rtc_date_offset(optarg, 1);
2553 break;
2554 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002555 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002556 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002557 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00002558 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002559 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00002560 break;
bellard26a5f132008-05-28 12:30:31 +00002561 case QEMU_OPTION_tb_size:
2562 tb_size = strtol(optarg, NULL, 0);
2563 if (tb_size < 0)
2564 tb_size = 0;
2565 break;
pbrook2e70f6e2008-06-29 01:03:05 +00002566 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002567 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00002568 break;
aliguori5bb79102008-10-13 03:12:02 +00002569 case QEMU_OPTION_incoming:
2570 incoming = optarg;
Amit Shah8e848652010-07-27 15:49:19 +05302571 incoming_expected = true;
aliguori5bb79102008-10-13 03:12:02 +00002572 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002573 case QEMU_OPTION_nodefaults:
2574 default_serial = 0;
2575 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002576 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002577 default_monitor = 0;
2578 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01002579 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002580 default_floppy = 0;
2581 default_cdrom = 0;
2582 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002583 break;
aliguorie37630c2009-04-22 15:19:10 +00002584 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00002585 if (!(xen_available())) {
2586 printf("Option %s not supported for this target\n", popt->name);
2587 exit(1);
2588 }
aliguorie37630c2009-04-22 15:19:10 +00002589 xen_domid = atoi(optarg);
2590 break;
2591 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00002592 if (!(xen_available())) {
2593 printf("Option %s not supported for this target\n", popt->name);
2594 exit(1);
2595 }
aliguorie37630c2009-04-22 15:19:10 +00002596 xen_mode = XEN_CREATE;
2597 break;
2598 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00002599 if (!(xen_available())) {
2600 printf("Option %s not supported for this target\n", popt->name);
2601 exit(1);
2602 }
aliguorie37630c2009-04-22 15:19:10 +00002603 xen_mode = XEN_ATTACH;
2604 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002605#ifdef CONFIG_SIMPLE_TRACE
2606 case QEMU_OPTION_trace:
2607 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2608 if (opts) {
2609 trace_file = qemu_opt_get(opts, "file");
2610 }
2611 break;
2612#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002613 case QEMU_OPTION_readconfig:
2614 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002615 int ret = qemu_read_config_file(optarg);
2616 if (ret < 0) {
2617 fprintf(stderr, "read config %s: %s\n", optarg,
2618 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002619 exit(1);
2620 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002621 break;
2622 }
2623 case QEMU_OPTION_writeconfig:
2624 {
2625 FILE *fp;
2626 if (strcmp(optarg, "-") == 0) {
2627 fp = stdout;
2628 } else {
2629 fp = fopen(optarg, "w");
2630 if (fp == NULL) {
2631 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2632 exit(1);
2633 }
2634 }
2635 qemu_config_write(fp);
2636 fclose(fp);
2637 break;
2638 }
Jes Sorensen59a52642010-06-10 11:42:25 +02002639 default:
2640 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00002641 }
bellard0824d6f2003-06-24 13:42:40 +00002642 }
2643 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002644 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00002645
Paul Brook5cea8592009-05-30 00:52:44 +01002646 /* If no data_dir is specified then try to find it relative to the
2647 executable path. */
2648 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02002649 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01002650 }
2651 /* If all else fails use the install patch specified when building. */
2652 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02002653 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01002654 }
2655
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002656#ifdef CONFIG_SIMPLE_TRACE
2657 /*
2658 * Set the trace file name, if specified.
2659 */
2660 st_set_trace_file(trace_file);
2661#endif
Jes Sorensen6be68d72009-07-23 17:03:42 +02002662 /*
2663 * Default to max_cpus = smp_cpus, in case the user doesn't
2664 * specify a max_cpus value.
2665 */
2666 if (!max_cpus)
2667 max_cpus = smp_cpus;
2668
balrog3d878ca2008-10-28 10:59:59 +00002669 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00002670 if (smp_cpus > machine->max_cpus) {
2671 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2672 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2673 machine->max_cpus);
2674 exit(1);
2675 }
2676
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002677 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2678 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002679
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002680 if (machine->no_serial) {
2681 default_serial = 0;
2682 }
2683 if (machine->no_parallel) {
2684 default_parallel = 0;
2685 }
2686 if (!machine->use_virtcon) {
2687 default_virtcon = 0;
2688 }
2689 if (machine->no_vga) {
2690 default_vga = 0;
2691 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002692 if (machine->no_floppy) {
2693 default_floppy = 0;
2694 }
2695 if (machine->no_cdrom) {
2696 default_cdrom = 0;
2697 }
2698 if (machine->no_sdcard) {
2699 default_sdcard = 0;
2700 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002701
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002702 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002703 if (default_parallel)
2704 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002705 if (default_serial && default_monitor) {
2706 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002707 } else if (default_virtcon && default_monitor) {
2708 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002709 } else {
2710 if (default_serial)
2711 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002712 if (default_virtcon)
2713 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002714 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002715 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002716 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002717 } else {
2718 if (default_serial)
2719 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002720 if (default_parallel)
2721 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01002722 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002723 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01002724 if (default_virtcon)
2725 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00002726 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01002727 if (default_vga)
2728 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00002729
TeLeMana5829fd2010-04-15 12:37:55 +08002730 socket_init();
2731
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002732 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002733 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002734#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002735 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302736 exit(1);
2737 }
2738#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002739
Jes Sorenseneb505be2010-06-10 11:42:28 +02002740 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00002741
thsaa26bb22007-03-25 21:33:06 +00002742 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02002743 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00002744 exit(1);
2745 }
2746
Paolo Bonzini98c85732010-04-19 18:59:30 +00002747 if (kvm_allowed) {
2748 int ret = kvm_init(smp_cpus);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002749 if (ret < 0) {
Paolo Bonzini98c85732010-04-19 18:59:30 +00002750 if (!kvm_available()) {
2751 printf("KVM not supported for this target\n");
2752 } else {
2753 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2754 }
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002755 exit(1);
2756 }
2757 }
2758
aliguori3fcf7b62009-04-24 18:03:25 +00002759 if (qemu_init_main_loop()) {
2760 fprintf(stderr, "qemu_init_main_loop failed\n");
2761 exit(1);
2762 }
bellarda20dd502003-09-30 21:07:02 +00002763 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00002764
thsf8d39c02008-07-03 10:01:15 +00002765 if (!linux_boot && *kernel_cmdline != '\0') {
2766 fprintf(stderr, "-append only allowed with -kernel option\n");
2767 exit(1);
2768 }
2769
2770 if (!linux_boot && initrd_filename != NULL) {
2771 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2772 exit(1);
2773 }
2774
Jes Sorensen9156d762010-06-10 11:42:30 +02002775 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00002776
aliguori7183b4b2008-11-05 20:40:18 +00002777 if (init_timer_alarm() < 0) {
2778 fprintf(stderr, "could not initialize alarm timer\n");
2779 exit(1);
2780 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002781 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00002782
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01002783 if (net_init_clients() < 0) {
2784 exit(1);
bellard702c6512004-04-02 21:21:32 +00002785 }
bellardf1510b22003-06-25 00:07:40 +00002786
balrogdc72ac12008-11-09 00:04:26 +00002787 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002788 if (foreach_device_config(DEV_BT, bt_parse))
2789 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00002790
bellard0824d6f2003-06-24 13:42:40 +00002791 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00002792 if (ram_size == 0)
2793 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00002794
bellard26a5f132008-05-28 12:30:31 +00002795 /* init the dynamic translator */
2796 cpu_exec_init_all(tb_size * 1024 * 1024);
2797
Markus Armbrustereb852012009-10-27 18:41:44 +01002798 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00002799
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002800 blk_mig_init();
2801
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002802 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002803 /* we always create the cdrom drive, even if no disk is there */
2804 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002805 }
thse4bcb142007-12-02 04:51:10 +00002806
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002807 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002808 /* we always create at least one floppy */
2809 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002810 }
bellardc4b1fcc2004-03-14 21:44:30 +00002811
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002812 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002813 /* we always create one sd slot, even if no card is in it */
2814 drive_add(NULL, SD_ALIAS);
2815 }
balrog9d413d12007-12-04 00:10:34 +00002816
ths96d30e42007-01-07 20:42:14 +00002817 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002818 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002819 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2820 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002821 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00002822
Alex Williamson97ab12d2010-06-25 11:09:50 -06002823 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002824 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00002825
aliguori268a3622009-04-21 22:30:27 +00002826 if (nb_numa_nodes > 0) {
2827 int i;
2828
2829 if (nb_numa_nodes > smp_cpus) {
2830 nb_numa_nodes = smp_cpus;
2831 }
2832
2833 /* If no memory size if given for any node, assume the default case
2834 * and distribute the available memory equally across all nodes
2835 */
2836 for (i = 0; i < nb_numa_nodes; i++) {
2837 if (node_mem[i] != 0)
2838 break;
2839 }
2840 if (i == nb_numa_nodes) {
2841 uint64_t usedmem = 0;
2842
2843 /* On Linux, the each node's border has to be 8MB aligned,
2844 * the final node gets the rest.
2845 */
2846 for (i = 0; i < nb_numa_nodes - 1; i++) {
2847 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2848 usedmem += node_mem[i];
2849 }
2850 node_mem[i] = ram_size - usedmem;
2851 }
2852
2853 for (i = 0; i < nb_numa_nodes; i++) {
2854 if (node_cpumask[i] != 0)
2855 break;
2856 }
2857 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2858 * must cope with this anyway, because there are BIOSes out there in
2859 * real machines which also use this scheme.
2860 */
2861 if (i == nb_numa_nodes) {
2862 for (i = 0; i < smp_cpus; i++) {
2863 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2864 }
2865 }
2866 }
2867
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002868 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02002869 exit(1);
2870 }
2871
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002872 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2873 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002874 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2875 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002876 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2877 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002878 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2879 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00002880
Paul Brookaae94602009-05-14 22:35:06 +01002881 module_call_init(MODULE_INIT_DEVICE);
2882
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002883 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002884 exit(0);
2885
Markus Armbruster09aaa162009-08-21 10:31:34 +02002886 if (watchdog) {
2887 i = select_watchdog(watchdog);
2888 if (i > 0)
2889 exit (i == 1 ? 1 : 0);
2890 }
2891
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002892 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01002893 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002894 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002895 qemu_add_globals();
2896
Paul Brookfbe1b592009-05-13 17:56:25 +01002897 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00002898 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2899
Jan Kiszkaea375f92010-03-01 19:10:30 +01002900 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00002901
Juan Quintela67b3b712009-08-28 19:25:15 +02002902 /* must be after terminal init, SDL library changes signal handlers */
Jes Sorensen8d963e62010-06-10 11:42:22 +02002903 os_setup_signal_handling();
Juan Quintela67b3b712009-08-28 19:25:15 +02002904
Blue Swirl87d0a282010-03-27 18:24:45 +00002905 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00002906
aliguori6f338c32009-02-11 15:21:54 +00002907 current_machine = machine;
2908
aliguori3023f332009-01-16 19:04:14 +00002909 /* init USB devices */
2910 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01002911 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2912 exit(1);
aliguori3023f332009-01-16 19:04:14 +00002913 }
2914
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002915 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002916 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002917 exit(1);
2918
Markus Armbruster668680f2010-02-11 14:44:58 +01002919 net_check_clients();
2920
aliguori3023f332009-01-16 19:04:14 +00002921 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01002922 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002923
2924 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002925#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002926 display_type = DT_SDL;
2927#else
2928 display_type = DT_VNC;
2929 vnc_display = "localhost:0,to=99";
2930 show_vnc_port = 1;
2931#endif
2932 }
2933
2934
2935 switch (display_type) {
2936 case DT_NOGRAPHIC:
2937 break;
2938#if defined(CONFIG_CURSES)
2939 case DT_CURSES:
2940 curses_display_init(ds, full_screen);
2941 break;
2942#endif
bellard5b0753e2005-03-01 21:37:28 +00002943#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002944 case DT_SDL:
2945 sdl_display_init(ds, full_screen, no_frame);
2946 break;
bellard5b0753e2005-03-01 21:37:28 +00002947#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002948 case DT_SDL:
2949 cocoa_display_init(ds, full_screen);
2950 break;
bellard313aa562003-08-10 21:52:11 +00002951#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002952 case DT_VNC:
2953 vnc_display_init(ds);
2954 if (vnc_display_open(ds, vnc_display) < 0)
2955 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002956
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002957 if (show_vnc_port) {
2958 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002959 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002960 break;
2961 default:
2962 break;
bellard313aa562003-08-10 21:52:11 +00002963 }
aliguori7d957bd2009-01-15 22:14:11 +00002964 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00002965
aliguori3023f332009-01-16 19:04:14 +00002966 dcl = ds->listeners;
2967 while (dcl != NULL) {
2968 if (dcl->dpy_refresh != NULL) {
2969 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2970 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09002971 break;
ths20d8a3e2007-02-18 17:04:49 +00002972 }
aliguori3023f332009-01-16 19:04:14 +00002973 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00002974 }
aliguori3023f332009-01-16 19:04:14 +00002975
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002976 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00002977 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2978 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2979 }
2980
Paolo Bonzinib473df62010-02-11 00:29:55 +01002981 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00002982
aliguori59030a82009-04-05 18:43:41 +00002983 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2984 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2985 gdbstub_dev);
2986 exit(1);
balrog45669e02007-07-02 13:20:17 +00002987 }
balrog45669e02007-07-02 13:20:17 +00002988
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02002989 qdev_machine_creation_done();
2990
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01002991 if (rom_load_all() != 0) {
2992 fprintf(stderr, "rom loading failed\n");
2993 exit(1);
2994 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02002995
Juan Quintela504c2942009-11-12 00:39:13 +01002996 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02002997 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01002998 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02002999 autostart = 0;
3000 }
3001 }
bellardd63d3072004-10-03 13:29:03 +00003002
Glauber Costa2bb8c102009-07-24 16:20:23 -04003003 if (incoming) {
Juan Quintela8ca5e802010-06-09 14:10:54 +02003004 int ret = qemu_start_incoming_migration(incoming);
3005 if (ret < 0) {
3006 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3007 incoming, ret);
3008 exit(ret);
3009 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03003010 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003011 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003012 }
thsffd843b2006-12-21 19:46:43 +00003013
Jes Sorenseneb505be2010-06-10 11:42:28 +02003014 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003015
bellard8a7ddc32004-03-31 19:00:16 +00003016 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003017 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003018 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00003019
bellard0824d6f2003-06-24 13:42:40 +00003020 return 0;
3021}