blob: 805e11f5c5c9a4f2235d5de1042f9a32fcbdd727 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000038#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000039#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000042#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000043#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000044#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000047#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000048#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000049#include <sys/select.h>
Prerna Saxenaab6540d2010-08-09 11:48:32 +010050#ifdef CONFIG_SIMPLE_TRACE
51#include "trace.h"
52#endif
53
Juan Quintela71e72a12009-07-27 16:12:56 +020054#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000055#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010056#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000057#include <libutil.h>
Juergen Lock92c0e652010-03-25 22:07:12 +010058#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000059#else
60#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000061#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010062#else
blueswir1223f0d72008-09-30 18:12:18 +000063#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000064#include <pty.h>
65#include <malloc.h>
thsbd494f42007-09-16 20:03:23 +000066
bellarde57a8c02005-11-10 23:58:52 +000067#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000068#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000069#endif
70#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000071#include <sys/stat.h>
72#include <sys/ethernet.h>
73#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000074#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000075#include <netinet/in_systm.h>
76#include <netinet/ip.h>
77#include <netinet/ip_icmp.h> // must come after ip.h
78#include <netinet/udp.h>
79#include <netinet/tcp.h>
80#include <net/if.h>
81#include <syslog.h>
82#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000083#endif
bellard7d3505c2004-05-12 19:32:15 +000084#endif
bellardec530c82006-04-25 22:36:06 +000085#endif
bellard67b915a2004-03-31 23:37:16 +000086
blueswir19892fbf2008-08-24 10:34:20 +000087#if defined(__OpenBSD__)
88#include <util.h>
89#endif
90
ths8a16d272008-07-19 09:56:24 +000091#if defined(CONFIG_VDE)
92#include <libvdeplug.h>
93#endif
94
bellard67b915a2004-03-31 23:37:16 +000095#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000096#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000097#endif
98
bellard73332e52004-04-04 20:22:28 +000099#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200100#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200101#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000102int qemu_main(int argc, char **argv, char **envp);
103int main(int argc, char **argv)
104{
Stefan Weil59a36a22009-06-18 20:11:03 +0200105 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000106}
107#undef main
108#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000109#endif
bellard73332e52004-04-04 20:22:28 +0000110#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000111
bellard5b0753e2005-03-01 21:37:28 +0000112#ifdef CONFIG_COCOA
113#undef main
114#define main qemu_main
115#endif /* CONFIG_COCOA */
116
blueswir1511d2b12009-03-07 15:32:56 +0000117#include "hw/hw.h"
118#include "hw/boards.h"
119#include "hw/usb.h"
120#include "hw/pcmcia.h"
121#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000122#include "hw/isa.h"
123#include "hw/baum.h"
124#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100125#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000126#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000127#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200128#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200129#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000130#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000131#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000132#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000133#include "monitor.h"
134#include "console.h"
135#include "sysemu.h"
136#include "gdbstub.h"
137#include "qemu-timer.h"
138#include "qemu-char.h"
139#include "cache-utils.h"
140#include "block.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200141#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200142#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000143#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000144#include "audio/audio.h"
145#include "migration.h"
146#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200147#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200148#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200149#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200150#include "qemu-options.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700151#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530152#include "fsdev/qemu-fsdev.h"
153#endif
blueswir1511d2b12009-03-07 15:32:56 +0000154
bellard0824d6f2003-06-24 13:42:40 +0000155#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000156
blueswir1511d2b12009-03-07 15:32:56 +0000157#include "qemu_socket.h"
158
Jan Kiszkad918f232009-06-24 14:42:30 +0200159#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000161#include "trace.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000162#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000163#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000164#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000165
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300166#include "ui/qemu-spice.h"
167
blueswir19dc63a12008-10-04 07:25:46 +0000168//#define DEBUG_NET
169//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000170
bellard1bfe8562004-07-08 21:17:50 +0000171#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000172
Amit Shah98b19252010-01-20 00:36:52 +0530173#define MAX_VIRTIO_CONSOLES 1
174
Paul Brook5cea8592009-05-30 00:52:44 +0100175static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000176const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000177enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500178DisplayType display_type = DT_DEFAULT;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +0200179int display_remote = 0;
bellard3d11d0e2004-12-12 16:56:30 +0000180const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500181ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300182const char *mem_path = NULL;
183#ifdef MAP_POPULATE
184int mem_prealloc = 0; /* force preallocation of physical target memory */
185#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000186int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000187NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000188int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200189int autostart;
Amit Shah8e848652010-07-27 15:49:19 +0530190int incoming_expected; /* Started with -incoming and waiting for incoming */
balrogf6503052008-02-17 11:42:19 +0000191static int rtc_utc = 1;
192static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200193QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100194int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000195static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000196#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000197static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000198#endif
ths667acca2006-12-11 02:08:05 +0000199int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000200CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000201CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000202CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000203int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000204int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000205int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000206int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000207int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200208int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200209int smp_cores = 1;
210int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000211const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000212int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000213int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000214int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000215int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000216int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000217int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000218int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200219uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200220const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000221const char *option_rom[MAX_OPTION_ROMS];
222int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000223int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000224int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000225const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000226int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500227int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000228unsigned int nb_prom_envs = 0;
229const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200230int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000231
aliguori268a3622009-04-21 22:30:27 +0000232int nb_numa_nodes;
233uint64_t node_mem[MAX_NODES];
234uint64_t node_cpumask[MAX_NODES];
235
blueswir19043b622009-01-21 19:28:13 +0000236static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000237
blueswir18fcb1b92008-09-18 18:29:08 +0000238uint8_t qemu_uuid[16];
239
Jan Kiszka76e30d02009-07-02 00:19:02 +0200240static QEMUBootSetHandler *boot_set_handler;
241static void *boot_set_opaque;
242
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200243static NotifierList exit_notifiers =
244 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
245
Blue Swirld745bef2010-03-29 19:23:49 +0000246int kvm_allowed = 0;
247uint32_t xen_domid;
248enum xen_mode xen_mode = XEN_EMULATE;
249
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100250static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100251static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100252static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100253static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100254static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100255static int default_floppy = 1;
256static int default_cdrom = 1;
257static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100258
259static struct {
260 const char *driver;
261 int *flag;
262} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100263 { .driver = "isa-serial", .flag = &default_serial },
264 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100265 { .driver = "isa-fdc", .flag = &default_floppy },
266 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530267 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
268 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
269 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100270 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100271 { .driver = "cirrus-vga", .flag = &default_vga },
272 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100273};
274
275static int default_driver_check(QemuOpts *opts, void *opaque)
276{
277 const char *driver = qemu_opt_get(opts, "driver");
278 int i;
279
280 if (!driver)
281 return 0;
282 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
283 if (strcmp(default_list[i].driver, driver) != 0)
284 continue;
285 *(default_list[i].flag) = 0;
286 }
287 return 0;
288}
289
bellard0824d6f2003-06-24 13:42:40 +0000290/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100291/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000292
bellardc4b1fcc2004-03-14 21:44:30 +0000293/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000294/* host time/date access */
295void qemu_get_timedate(struct tm *tm, int offset)
296{
297 time_t ti;
298 struct tm *ret;
299
300 time(&ti);
301 ti += offset;
302 if (rtc_date_offset == -1) {
303 if (rtc_utc)
304 ret = gmtime(&ti);
305 else
306 ret = localtime(&ti);
307 } else {
308 ti -= rtc_date_offset;
309 ret = gmtime(&ti);
310 }
311
312 memcpy(tm, ret, sizeof(struct tm));
313}
314
315int qemu_timedate_diff(struct tm *tm)
316{
317 time_t seconds;
318
319 if (rtc_date_offset == -1)
320 if (rtc_utc)
321 seconds = mktimegm(tm);
322 else
323 seconds = mktime(tm);
324 else
325 seconds = mktimegm(tm) + rtc_date_offset;
326
327 return seconds - time(NULL);
328}
329
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300330void rtc_change_mon_event(struct tm *tm)
331{
332 QObject *data;
333
334 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
335 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
336 qobject_decref(data);
337}
338
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200339static void configure_rtc_date_offset(const char *startdate, int legacy)
340{
341 time_t rtc_start_date;
342 struct tm tm;
343
344 if (!strcmp(startdate, "now") && legacy) {
345 rtc_date_offset = -1;
346 } else {
347 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
348 &tm.tm_year,
349 &tm.tm_mon,
350 &tm.tm_mday,
351 &tm.tm_hour,
352 &tm.tm_min,
353 &tm.tm_sec) == 6) {
354 /* OK */
355 } else if (sscanf(startdate, "%d-%d-%d",
356 &tm.tm_year,
357 &tm.tm_mon,
358 &tm.tm_mday) == 3) {
359 tm.tm_hour = 0;
360 tm.tm_min = 0;
361 tm.tm_sec = 0;
362 } else {
363 goto date_fail;
364 }
365 tm.tm_year -= 1900;
366 tm.tm_mon--;
367 rtc_start_date = mktimegm(&tm);
368 if (rtc_start_date == -1) {
369 date_fail:
370 fprintf(stderr, "Invalid date format. Valid formats are:\n"
371 "'2006-06-17T16:01:21' or '2006-06-17'\n");
372 exit(1);
373 }
374 rtc_date_offset = time(NULL) - rtc_start_date;
375 }
376}
377
378static void configure_rtc(QemuOpts *opts)
379{
380 const char *value;
381
382 value = qemu_opt_get(opts, "base");
383 if (value) {
384 if (!strcmp(value, "utc")) {
385 rtc_utc = 1;
386 } else if (!strcmp(value, "localtime")) {
387 rtc_utc = 0;
388 } else {
389 configure_rtc_date_offset(value, 0);
390 }
391 }
Jan Kiszka68752042009-09-15 13:36:04 +0200392 value = qemu_opt_get(opts, "clock");
393 if (value) {
394 if (!strcmp(value, "host")) {
395 rtc_clock = host_clock;
396 } else if (!strcmp(value, "vm")) {
397 rtc_clock = vm_clock;
398 } else {
399 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
400 exit(1);
401 }
402 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200403 value = qemu_opt_get(opts, "driftfix");
404 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000405 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200406 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000407 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200408 rtc_td_hack = 0;
409 } else {
410 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
411 exit(1);
412 }
413 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200414}
415
balrog1ae26a12008-09-28 23:19:47 +0000416/***********************************************************/
417/* Bluetooth support */
418static int nb_hcis;
419static int cur_hci;
420static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000421
balrog1ae26a12008-09-28 23:19:47 +0000422static struct bt_vlan_s {
423 struct bt_scatternet_s net;
424 int id;
425 struct bt_vlan_s *next;
426} *first_bt_vlan;
427
428/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000429static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000430{
431 struct bt_vlan_s **pvlan, *vlan;
432 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
433 if (vlan->id == id)
434 return &vlan->net;
435 }
436 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
437 vlan->id = id;
438 pvlan = &first_bt_vlan;
439 while (*pvlan != NULL)
440 pvlan = &(*pvlan)->next;
441 *pvlan = vlan;
442 return &vlan->net;
443}
444
445static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
446{
447}
448
449static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
450{
451 return -ENOTSUP;
452}
453
454static struct HCIInfo null_hci = {
455 .cmd_send = null_hci_send,
456 .sco_send = null_hci_send,
457 .acl_send = null_hci_send,
458 .bdaddr_set = null_hci_addr_set,
459};
460
461struct HCIInfo *qemu_next_hci(void)
462{
463 if (cur_hci == nb_hcis)
464 return &null_hci;
465
466 return hci_table[cur_hci++];
467}
468
balrogdc72ac12008-11-09 00:04:26 +0000469static struct HCIInfo *hci_init(const char *str)
470{
471 char *endp;
472 struct bt_scatternet_s *vlan = 0;
473
474 if (!strcmp(str, "null"))
475 /* null */
476 return &null_hci;
477 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
478 /* host[:hciN] */
479 return bt_host_hci(str[4] ? str + 5 : "hci0");
480 else if (!strncmp(str, "hci", 3)) {
481 /* hci[,vlan=n] */
482 if (str[3]) {
483 if (!strncmp(str + 3, ",vlan=", 6)) {
484 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
485 if (*endp)
486 vlan = 0;
487 }
488 } else
489 vlan = qemu_find_bt_vlan(0);
490 if (vlan)
491 return bt_new_hci(vlan);
492 }
493
494 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
495
496 return 0;
497}
498
499static int bt_hci_parse(const char *str)
500{
501 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500502 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000503
504 if (nb_hcis >= MAX_NICS) {
505 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
506 return -1;
507 }
508
509 hci = hci_init(str);
510 if (!hci)
511 return -1;
512
513 bdaddr.b[0] = 0x52;
514 bdaddr.b[1] = 0x54;
515 bdaddr.b[2] = 0x00;
516 bdaddr.b[3] = 0x12;
517 bdaddr.b[4] = 0x34;
518 bdaddr.b[5] = 0x56 + nb_hcis;
519 hci->bdaddr_set(hci, bdaddr.b);
520
521 hci_table[nb_hcis++] = hci;
522
523 return 0;
524}
525
526static void bt_vhci_add(int vlan_id)
527{
528 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
529
530 if (!vlan->slave)
531 fprintf(stderr, "qemu: warning: adding a VHCI to "
532 "an empty scatternet %i\n", vlan_id);
533
534 bt_vhci_init(bt_new_hci(vlan));
535}
536
537static struct bt_device_s *bt_device_add(const char *opt)
538{
539 struct bt_scatternet_s *vlan;
540 int vlan_id = 0;
541 char *endp = strstr(opt, ",vlan=");
542 int len = (endp ? endp - opt : strlen(opt)) + 1;
543 char devname[10];
544
545 pstrcpy(devname, MIN(sizeof(devname), len), opt);
546
547 if (endp) {
548 vlan_id = strtol(endp + 6, &endp, 0);
549 if (*endp) {
550 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
551 return 0;
552 }
553 }
554
555 vlan = qemu_find_bt_vlan(vlan_id);
556
557 if (!vlan->slave)
558 fprintf(stderr, "qemu: warning: adding a slave device to "
559 "an empty scatternet %i\n", vlan_id);
560
561 if (!strcmp(devname, "keyboard"))
562 return bt_keyboard_init(vlan);
563
564 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
565 return 0;
566}
567
568static int bt_parse(const char *opt)
569{
570 const char *endp, *p;
571 int vlan;
572
573 if (strstart(opt, "hci", &endp)) {
574 if (!*endp || *endp == ',') {
575 if (*endp)
576 if (!strstart(endp, ",vlan=", 0))
577 opt = endp + 1;
578
579 return bt_hci_parse(opt);
580 }
581 } else if (strstart(opt, "vhci", &endp)) {
582 if (!*endp || *endp == ',') {
583 if (*endp) {
584 if (strstart(endp, ",vlan=", &p)) {
585 vlan = strtol(p, (char **) &endp, 0);
586 if (*endp) {
587 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
588 return 1;
589 }
590 } else {
591 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
592 return 1;
593 }
594 } else
595 vlan = 0;
596
597 bt_vhci_add(vlan);
598 return 0;
599 }
600 } else if (strstart(opt, "device:", &endp))
601 return !bt_device_add(endp);
602
603 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
604 return 1;
605}
606
balrog1ae26a12008-09-28 23:19:47 +0000607/***********************************************************/
608/* QEMU Block devices */
609
balrog609497a2008-01-14 02:56:53 +0000610#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +0000611#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +0000612#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +0000613#define PFLASH_ALIAS "if=pflash"
614#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +0000615#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +0000616
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200617static int drive_init_func(QemuOpts *opts, void *opaque)
618{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200619 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200620 int fatal_error = 0;
621
Markus Armbrustera803cb82010-06-02 13:31:55 +0200622 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200623 if (fatal_error)
624 return 1;
625 }
626 return 0;
627}
628
629static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
630{
631 if (NULL == qemu_opt_get(opts, "snapshot")) {
632 qemu_opt_set(opts, "snapshot", "on");
633 }
634 return 0;
635}
636
Jan Kiszka76e30d02009-07-02 00:19:02 +0200637void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
638{
639 boot_set_handler = func;
640 boot_set_opaque = opaque;
641}
642
643int qemu_boot_set(const char *boot_devices)
644{
645 if (!boot_set_handler) {
646 return -EINVAL;
647 }
648 return boot_set_handler(boot_set_opaque, boot_devices);
649}
650
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300651static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200652{
653 /* We just do some generic consistency checks */
654 const char *p;
655 int bitmap = 0;
656
657 for (p = devices; *p != '\0'; p++) {
658 /* Allowed boot devices are:
659 * a-b: floppy disk drives
660 * c-f: IDE disk drives
661 * g-m: machine implementation dependant drives
662 * n-p: network devices
663 * It's up to each machine implementation to check if the given boot
664 * devices match the actual hardware implementation and firmware
665 * features.
666 */
667 if (*p < 'a' || *p > 'p') {
668 fprintf(stderr, "Invalid boot device '%c'\n", *p);
669 exit(1);
670 }
671 if (bitmap & (1 << (*p - 'a'))) {
672 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
673 exit(1);
674 }
675 bitmap |= 1 << (*p - 'a');
676 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200677}
678
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200679static void restore_boot_devices(void *opaque)
680{
681 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600682 static int first = 1;
683
684 /* Restore boot order and remove ourselves after the first boot */
685 if (first) {
686 first = 0;
687 return;
688 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200689
690 qemu_boot_set(standard_boot_devices);
691
692 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
693 qemu_free(standard_boot_devices);
694}
695
aliguori268a3622009-04-21 22:30:27 +0000696static void numa_add(const char *optarg)
697{
698 char option[128];
699 char *endptr;
700 unsigned long long value, endvalue;
701 int nodenr;
702
703 optarg = get_opt_name(option, 128, optarg, ',') + 1;
704 if (!strcmp(option, "node")) {
705 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
706 nodenr = nb_numa_nodes;
707 } else {
708 nodenr = strtoull(option, NULL, 10);
709 }
710
711 if (get_param_value(option, 128, "mem", optarg) == 0) {
712 node_mem[nodenr] = 0;
713 } else {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200714 ssize_t sval;
715 sval = strtosz(option, NULL);
716 if (sval < 0) {
717 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
718 exit(1);
aliguori268a3622009-04-21 22:30:27 +0000719 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200720 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +0000721 }
722 if (get_param_value(option, 128, "cpus", optarg) == 0) {
723 node_cpumask[nodenr] = 0;
724 } else {
725 value = strtoull(option, &endptr, 10);
726 if (value >= 64) {
727 value = 63;
728 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
729 } else {
730 if (*endptr == '-') {
731 endvalue = strtoull(endptr+1, &endptr, 10);
732 if (endvalue >= 63) {
733 endvalue = 62;
734 fprintf(stderr,
735 "only 63 CPUs in NUMA mode supported.\n");
736 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100737 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000738 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100739 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000740 }
741 }
742 node_cpumask[nodenr] = value;
743 }
744 nb_numa_nodes++;
745 }
746 return;
747}
748
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200749static void smp_parse(const char *optarg)
750{
751 int smp, sockets = 0, threads = 0, cores = 0;
752 char *endptr;
753 char option[128];
754
755 smp = strtoul(optarg, &endptr, 10);
756 if (endptr != optarg) {
757 if (*endptr == ',') {
758 endptr++;
759 }
760 }
761 if (get_param_value(option, 128, "sockets", endptr) != 0)
762 sockets = strtoull(option, NULL, 10);
763 if (get_param_value(option, 128, "cores", endptr) != 0)
764 cores = strtoull(option, NULL, 10);
765 if (get_param_value(option, 128, "threads", endptr) != 0)
766 threads = strtoull(option, NULL, 10);
767 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
768 max_cpus = strtoull(option, NULL, 10);
769
770 /* compute missing values, prefer sockets over cores over threads */
771 if (smp == 0 || sockets == 0) {
772 sockets = sockets > 0 ? sockets : 1;
773 cores = cores > 0 ? cores : 1;
774 threads = threads > 0 ? threads : 1;
775 if (smp == 0) {
776 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200777 }
778 } else {
779 if (cores == 0) {
780 threads = threads > 0 ? threads : 1;
781 cores = smp / (sockets * threads);
782 } else {
Joel Schoppdca98162010-07-21 15:05:17 -0500783 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200784 }
785 }
786 smp_cpus = smp;
787 smp_cores = cores > 0 ? cores : 1;
788 smp_threads = threads > 0 ? threads : 1;
789 if (max_cpus == 0)
790 max_cpus = smp_cpus;
791}
792
bellard330d0412003-07-26 18:11:40 +0000793/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +0000794/* USB devices */
795
Markus Armbrusterfb080002010-05-28 15:38:44 +0200796static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +0000797{
798 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200799 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +0000800
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200801 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +0000802 return -1;
803
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +0100804 /* drivers with .usbdevice_name entry in USBDeviceInfo */
805 dev = usbdevice_create(devname);
806 if (dev)
807 goto done;
808
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200809 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +0000810 if (strstart(devname, "host:", &p)) {
811 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +0000812 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
813 dev = usb_bt_init(devname[2] ? hci_init(p) :
814 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +0000815 } else {
816 return -1;
817 }
pbrook0d92ed32006-05-21 16:30:15 +0000818 if (!dev)
819 return -1;
820
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200821done:
bellarda594cfb2005-11-06 16:13:29 +0000822 return 0;
823}
824
aliguori1f3870a2008-08-21 19:27:48 +0000825static int usb_device_del(const char *devname)
826{
827 int bus_num, addr;
828 const char *p;
829
aliguori5d0c5752008-09-14 01:07:41 +0000830 if (strstart(devname, "host:", &p))
831 return usb_host_device_close(p);
832
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200833 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +0000834 return -1;
835
836 p = strchr(devname, '.');
837 if (!p)
838 return -1;
839 bus_num = strtoul(devname, NULL, 0);
840 addr = strtoul(p + 1, NULL, 0);
841
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200842 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000843}
844
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200845static int usb_parse(const char *cmdline)
846{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800847 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +0200848 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800849 if (r < 0) {
850 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
851 }
852 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200853}
854
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300855void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000856{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800857 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +0200858 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100859 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800860 }
bellarda594cfb2005-11-06 16:13:29 +0000861}
862
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300863void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000864{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800865 const char *devname = qdict_get_str(qdict, "devname");
866 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100867 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800868 }
bellarda594cfb2005-11-06 16:13:29 +0000869}
870
bellardf7cce892004-12-08 22:21:25 +0000871/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +0000872/* PCMCIA/Cardbus */
873
874static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +0100875 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +0000876 struct pcmcia_socket_entry_s *next;
877} *pcmcia_sockets = 0;
878
Paul Brookbc24a222009-05-10 01:44:56 +0100879void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000880{
881 struct pcmcia_socket_entry_s *entry;
882
883 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
884 entry->socket = socket;
885 entry->next = pcmcia_sockets;
886 pcmcia_sockets = entry;
887}
888
Paul Brookbc24a222009-05-10 01:44:56 +0100889void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000890{
891 struct pcmcia_socket_entry_s *entry, **ptr;
892
893 ptr = &pcmcia_sockets;
894 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
895 if (entry->socket == socket) {
896 *ptr = entry->next;
897 qemu_free(entry);
898 }
899}
900
aliguori376253e2009-03-05 23:01:23 +0000901void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +0000902{
903 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +0000904
balrog201a51f2007-04-30 00:51:09 +0000905 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +0000906 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +0000907
908 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +0000909 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
910 iter->socket->attached ? iter->socket->card_string :
911 "Empty");
balrog201a51f2007-04-30 00:51:09 +0000912}
913
914/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +0000915/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +0000916
bellardc4b1fcc2004-03-14 21:44:30 +0000917typedef struct IOHandlerRecord {
918 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +0100919 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +0000920 IOHandler *fd_read;
921 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +0000922 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +0000923 void *opaque;
924 /* temporary data */
925 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +0100926 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +0000927} IOHandlerRecord;
928
Juan Quintela31d4ee62010-03-11 17:55:37 +0100929static QLIST_HEAD(, IOHandlerRecord) io_handlers =
930 QLIST_HEAD_INITIALIZER(io_handlers);
931
bellardc4b1fcc2004-03-14 21:44:30 +0000932
bellard7c9d8e02005-11-15 22:16:05 +0000933/* XXX: fd_read_poll should be suppressed, but an API change is
934 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +0000935int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +0100936 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +0000937 IOHandler *fd_read,
938 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +0000939 void *opaque)
bellardb4608c02003-06-27 17:34:32 +0000940{
Juan Quintela31d4ee62010-03-11 17:55:37 +0100941 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +0000942
bellard7c9d8e02005-11-15 22:16:05 +0000943 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100944 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000945 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +0000946 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +0000947 break;
948 }
bellard8a7ddc32004-03-31 19:00:16 +0000949 }
bellard7c9d8e02005-11-15 22:16:05 +0000950 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100951 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000952 if (ioh->fd == fd)
953 goto found;
954 }
955 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +0100956 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +0000957 found:
958 ioh->fd = fd;
959 ioh->fd_read_poll = fd_read_poll;
960 ioh->fd_read = fd_read;
961 ioh->fd_write = fd_write;
962 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +0000963 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +0000964 }
bellard7c9d8e02005-11-15 22:16:05 +0000965 return 0;
966}
967
ths5fafdf22007-09-16 21:08:06 +0000968int qemu_set_fd_handler(int fd,
969 IOHandler *fd_read,
970 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +0000971 void *opaque)
972{
973 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +0000974}
975
bellard8a7ddc32004-03-31 19:00:16 +0000976/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +0000977/* machine registration */
978
blueswir1bdaf78e2008-10-04 07:24:27 +0000979static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +0000980QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +0000981
982int qemu_register_machine(QEMUMachine *m)
983{
984 QEMUMachine **pm;
985 pm = &first_machine;
986 while (*pm != NULL)
987 pm = &(*pm)->next;
988 m->next = NULL;
989 *pm = m;
990 return 0;
991}
992
pbrook9596ebb2007-11-18 01:44:38 +0000993static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +0000994{
995 QEMUMachine *m;
996
997 for(m = first_machine; m != NULL; m = m->next) {
998 if (!strcmp(m->name, name))
999 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001000 if (m->alias && !strcmp(m->alias, name))
1001 return m;
bellardcc1daa42005-06-05 14:49:17 +00001002 }
1003 return NULL;
1004}
1005
Anthony Liguori0c257432009-05-21 20:41:01 -05001006static QEMUMachine *find_default_machine(void)
1007{
1008 QEMUMachine *m;
1009
1010 for(m = first_machine; m != NULL; m = m->next) {
1011 if (m->is_default) {
1012 return m;
1013 }
1014 }
1015 return NULL;
1016}
1017
bellardcc1daa42005-06-05 14:49:17 +00001018/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001019/* main execution loop */
1020
pbrook9596ebb2007-11-18 01:44:38 +00001021static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001022{
aliguori7d957bd2009-01-15 22:14:11 +00001023 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001024 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001025 DisplayChangeListener *dcl = ds->listeners;
1026
Sheng Yang62a27442010-01-26 19:21:16 +08001027 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001028 dpy_refresh(ds);
1029
1030 while (dcl != NULL) {
1031 if (dcl->gui_timer_interval &&
1032 dcl->gui_timer_interval < interval)
1033 interval = dcl->gui_timer_interval;
1034 dcl = dcl->next;
1035 }
1036 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001037}
1038
blueswir19043b622009-01-21 19:28:13 +00001039static void nographic_update(void *opaque)
1040{
1041 uint64_t interval = GUI_REFRESH_INTERVAL;
1042
Sheng Yang62a27442010-01-26 19:21:16 +08001043 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00001044 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1045}
1046
bellard0bd48852005-11-11 00:00:47 +00001047struct vm_change_state_entry {
1048 VMChangeStateHandler *cb;
1049 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001050 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001051};
1052
Blue Swirl72cf2d42009-09-12 07:36:22 +00001053static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001054
1055VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1056 void *opaque)
1057{
1058 VMChangeStateEntry *e;
1059
1060 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001061
1062 e->cb = cb;
1063 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001064 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001065 return e;
1066}
1067
1068void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1069{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001070 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001071 qemu_free (e);
1072}
1073
Blue Swirl296af7c2010-03-29 19:23:50 +00001074void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001075{
1076 VMChangeStateEntry *e;
1077
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001078 trace_vm_state_notify(running, reason);
1079
bellard0bd48852005-11-11 00:00:47 +00001080 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001081 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001082 }
1083}
1084
bellard8a7ddc32004-03-31 19:00:16 +00001085void vm_start(void)
1086{
1087 if (!vm_running) {
1088 cpu_enable_ticks();
1089 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001090 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001091 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001092 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001093 }
1094}
1095
bellardbb0c6722004-06-20 12:37:32 +00001096/* reset/shutdown handler */
1097
1098typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001099 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001100 QEMUResetHandler *func;
1101 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001102} QEMUResetEntry;
1103
Blue Swirl72cf2d42009-09-12 07:36:22 +00001104static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1105 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001106static int reset_requested;
1107static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00001108static int powerdown_requested;
Blue Swirl296af7c2010-03-29 19:23:50 +00001109int debug_requested;
Blue Swirl7277e022010-04-12 17:19:06 +00001110int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001111
aurel32cf7a2fe2008-03-18 06:53:05 +00001112int qemu_shutdown_requested(void)
1113{
1114 int r = shutdown_requested;
1115 shutdown_requested = 0;
1116 return r;
1117}
1118
1119int qemu_reset_requested(void)
1120{
1121 int r = reset_requested;
1122 reset_requested = 0;
1123 return r;
1124}
1125
1126int qemu_powerdown_requested(void)
1127{
1128 int r = powerdown_requested;
1129 powerdown_requested = 0;
1130 return r;
1131}
1132
aliguorie5689022009-04-24 18:03:54 +00001133static int qemu_debug_requested(void)
1134{
1135 int r = debug_requested;
1136 debug_requested = 0;
1137 return r;
1138}
1139
aliguori6e29f5d2009-04-24 18:04:02 +00001140static int qemu_vmstop_requested(void)
1141{
1142 int r = vmstop_requested;
1143 vmstop_requested = 0;
1144 return r;
1145}
1146
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001147void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001148{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001149 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001150
bellardbb0c6722004-06-20 12:37:32 +00001151 re->func = func;
1152 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001153 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001154}
1155
Jan Kiszkadda9b292009-07-02 00:19:02 +02001156void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001157{
1158 QEMUResetEntry *re;
1159
Blue Swirl72cf2d42009-09-12 07:36:22 +00001160 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001161 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001162 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001163 qemu_free(re);
1164 return;
1165 }
1166 }
1167}
1168
1169void qemu_system_reset(void)
1170{
1171 QEMUResetEntry *re, *nre;
1172
1173 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001174 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001175 re->func(re->opaque);
1176 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001177 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001178 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001179}
1180
1181void qemu_system_reset_request(void)
1182{
bellardd1beab82006-10-02 19:44:22 +00001183 if (no_reboot) {
1184 shutdown_requested = 1;
1185 } else {
1186 reset_requested = 1;
1187 }
aliguorid9f75a42009-04-24 18:03:11 +00001188 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001189}
1190
1191void qemu_system_shutdown_request(void)
1192{
1193 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001194 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001195}
1196
bellard34751872005-07-02 14:31:34 +00001197void qemu_system_powerdown_request(void)
1198{
1199 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001200 qemu_notify_event();
1201}
1202
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001203void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001204{
1205 IOHandlerRecord *ioh;
1206 fd_set rfds, wfds, xfds;
1207 int ret, nfds;
1208 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001209 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001210
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001211 if (nonblocking)
1212 timeout = 0;
1213 else {
1214 timeout = qemu_calculate_timeout();
1215 qemu_bh_update_timeout(&timeout);
1216 }
aliguori56f3a5d2008-10-31 18:07:17 +00001217
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001218 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001219
bellardfd1dff42006-02-01 21:29:26 +00001220 /* poll any events */
1221 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001222 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001223 FD_ZERO(&rfds);
1224 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001225 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001226 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00001227 if (ioh->deleted)
1228 continue;
bellardfd1dff42006-02-01 21:29:26 +00001229 if (ioh->fd_read &&
1230 (!ioh->fd_read_poll ||
1231 ioh->fd_read_poll(ioh->opaque) != 0)) {
1232 FD_SET(ioh->fd, &rfds);
1233 if (ioh->fd > nfds)
1234 nfds = ioh->fd;
1235 }
1236 if (ioh->fd_write) {
1237 FD_SET(ioh->fd, &wfds);
1238 if (ioh->fd > nfds)
1239 nfds = ioh->fd;
1240 }
1241 }
ths3b46e622007-09-17 08:09:54 +00001242
aliguori56f3a5d2008-10-31 18:07:17 +00001243 tv.tv_sec = timeout / 1000;
1244 tv.tv_usec = (timeout % 1000) * 1000;
1245
Jan Kiszkad918f232009-06-24 14:42:30 +02001246 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1247
aliguori48708522009-04-24 18:03:49 +00001248 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001249 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001250 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00001251 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001252 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00001253
Juan Quintela31d4ee62010-03-11 17:55:37 +01001254 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
Stefan Hajnoczi0290b572010-11-03 14:29:44 +00001255 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1256 ioh->fd_read(ioh->opaque);
1257 }
1258 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1259 ioh->fd_write(ioh->opaque);
1260 }
1261
1262 /* Do this last in case read/write handlers marked it for deletion */
thscafffd42007-02-28 21:59:44 +00001263 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001264 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00001265 qemu_free(ioh);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001266 }
thscafffd42007-02-28 21:59:44 +00001267 }
bellardfd1dff42006-02-01 21:29:26 +00001268 }
Jan Kiszkad918f232009-06-24 14:42:30 +02001269
1270 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001271
Paolo Bonzinib6964822010-03-10 11:38:45 +01001272 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001273
pbrook423f0742007-05-23 00:06:54 +00001274 /* Check bottom-halves last in case any of the earlier events triggered
1275 them. */
1276 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001277
bellard5905b2e2004-08-01 21:53:26 +00001278}
1279
aliguori43b96852009-04-24 18:03:33 +00001280static int vm_can_run(void)
1281{
1282 if (powerdown_requested)
1283 return 0;
1284 if (reset_requested)
1285 return 0;
1286 if (shutdown_requested)
1287 return 0;
aliguorie5689022009-04-24 18:03:54 +00001288 if (debug_requested)
1289 return 0;
aliguori43b96852009-04-24 18:03:33 +00001290 return 1;
1291}
1292
Blue Swirld9c32312009-08-09 08:42:19 +00001293qemu_irq qemu_system_powerdown;
1294
aliguori43b96852009-04-24 18:03:33 +00001295static void main_loop(void)
1296{
aliguori6e29f5d2009-04-24 18:04:02 +00001297 int r;
aliguori43b96852009-04-24 18:03:33 +00001298
Blue Swirl7277e022010-04-12 17:19:06 +00001299 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001300
aliguori6e29f5d2009-04-24 18:04:02 +00001301 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00001302 do {
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001303 bool nonblocking = false;
aliguorie6e35b12009-04-24 18:03:57 +00001304#ifdef CONFIG_PROFILER
1305 int64_t ti;
1306#endif
aliguorid6dc3d42009-04-24 18:04:07 +00001307#ifndef CONFIG_IOTHREAD
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001308 nonblocking = cpu_exec_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001309#endif
aliguori43b96852009-04-24 18:03:33 +00001310#ifdef CONFIG_PROFILER
1311 ti = profile_getclock();
1312#endif
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001313 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001314#ifdef CONFIG_PROFILER
1315 dev_time += profile_getclock() - ti;
1316#endif
aliguorie5689022009-04-24 18:03:54 +00001317 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00001318
Blue Swirl54fc6ea2010-03-29 19:23:46 +00001319 if ((r = qemu_debug_requested())) {
1320 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001321 }
aliguori43b96852009-04-24 18:03:33 +00001322 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001323 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001324 if (no_shutdown) {
1325 vm_stop(0);
1326 no_shutdown = 0;
1327 } else
1328 break;
1329 }
aliguorid6dc3d42009-04-24 18:04:07 +00001330 if (qemu_reset_requested()) {
1331 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00001332 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00001333 resume_all_vcpus();
1334 }
Blue Swirld9c32312009-08-09 08:42:19 +00001335 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001336 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001337 qemu_irq_raise(qemu_system_powerdown);
1338 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001339 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001340 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001341 }
aliguori43b96852009-04-24 18:03:33 +00001342 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001343 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001344 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001345}
1346
pbrook9bd7e6d2009-04-07 22:58:45 +00001347static void version(void)
1348{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001349 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001350}
1351
ths15f82202007-06-29 23:26:08 +00001352static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001353{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001354 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001355#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1356 opt_help
blueswir15824d652009-03-28 06:44:27 +00001357#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001358#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001359#undef DEF
1360#undef DEFHEADING
1361#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001362 ;
1363 version();
1364 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001365 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001366 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001367 "\n"
1368 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001369 "During emulation, the following keys are useful:\n"
1370 "ctrl-alt-f toggle full screen\n"
1371 "ctrl-alt-n switch to virtual console 'n'\n"
1372 "ctrl-alt toggle mouse and keyboard grab\n"
1373 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001374 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1375 "qemu",
1376 options_help);
ths15f82202007-06-29 23:26:08 +00001377 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001378}
1379
bellardcd6f1162004-05-13 22:02:20 +00001380#define HAS_ARG 0x0001
1381
bellardcd6f1162004-05-13 22:02:20 +00001382typedef struct QEMUOption {
1383 const char *name;
1384 int flags;
1385 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001386 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001387} QEMUOption;
1388
blueswir1dbed7e42008-10-01 19:38:09 +00001389static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001390 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1391#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1392 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001393#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001394#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001395#undef DEF
1396#undef DEFHEADING
1397#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001398 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001399};
malc3893c122008-09-28 00:42:05 +00001400static void select_vgahw (const char *p)
1401{
1402 const char *opts;
1403
Gerd Hoffmann64465292009-12-08 13:11:45 +01001404 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001405 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001406 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001407 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001408 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001409 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001410 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001411 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001412 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001413 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00001414 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001415 invalid_vga:
1416 fprintf(stderr, "Unknown vga type: %s\n", p);
1417 exit(1);
1418 }
malccb5a7aa2008-09-28 00:42:12 +00001419 while (*opts) {
1420 const char *nextopt;
1421
1422 if (strstart(opts, ",retrace=", &nextopt)) {
1423 opts = nextopt;
1424 if (strstart(opts, "dumb", &nextopt))
1425 vga_retrace_method = VGA_RETRACE_DUMB;
1426 else if (strstart(opts, "precise", &nextopt))
1427 vga_retrace_method = VGA_RETRACE_PRECISE;
1428 else goto invalid_vga;
1429 } else goto invalid_vga;
1430 opts = nextopt;
1431 }
malc3893c122008-09-28 00:42:05 +00001432}
1433
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001434static int balloon_parse(const char *arg)
1435{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001436 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001437
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001438 if (strcmp(arg, "none") == 0) {
1439 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001440 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001441
1442 if (!strncmp(arg, "virtio", 6)) {
1443 if (arg[6] == ',') {
1444 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001445 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001446 if (!opts)
1447 return -1;
1448 } else {
1449 /* create empty opts */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001450 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001451 }
1452 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1453 return 0;
1454 }
1455
1456 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001457}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001458
Paul Brook5cea8592009-05-30 00:52:44 +01001459char *qemu_find_file(int type, const char *name)
1460{
1461 int len;
1462 const char *subdir;
1463 char *buf;
1464
1465 /* If name contains path separators then try it as a straight path. */
1466 if ((strchr(name, '/') || strchr(name, '\\'))
1467 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02001468 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001469 }
1470 switch (type) {
1471 case QEMU_FILE_TYPE_BIOS:
1472 subdir = "";
1473 break;
1474 case QEMU_FILE_TYPE_KEYMAP:
1475 subdir = "keymaps/";
1476 break;
1477 default:
1478 abort();
1479 }
1480 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1481 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001482 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001483 if (access(buf, R_OK)) {
1484 qemu_free(buf);
1485 return NULL;
1486 }
1487 return buf;
1488}
1489
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001490static int device_help_func(QemuOpts *opts, void *opaque)
1491{
1492 return qdev_device_help(opts);
1493}
1494
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001495static int device_init_func(QemuOpts *opts, void *opaque)
1496{
1497 DeviceState *dev;
1498
1499 dev = qdev_device_add(opts);
1500 if (!dev)
1501 return -1;
1502 return 0;
1503}
1504
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001505static int chardev_init_func(QemuOpts *opts, void *opaque)
1506{
1507 CharDriverState *chr;
1508
1509 chr = qemu_chr_open_opts(opts, NULL);
1510 if (!chr)
1511 return -1;
1512 return 0;
1513}
1514
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07001515#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301516static int fsdev_init_func(QemuOpts *opts, void *opaque)
1517{
1518 int ret;
1519 ret = qemu_fsdev_add(opts);
1520
1521 return ret;
1522}
1523#endif
1524
Gerd Hoffmann88589342009-12-08 13:11:50 +01001525static int mon_init_func(QemuOpts *opts, void *opaque)
1526{
1527 CharDriverState *chr;
1528 const char *chardev;
1529 const char *mode;
1530 int flags;
1531
1532 mode = qemu_opt_get(opts, "mode");
1533 if (mode == NULL) {
1534 mode = "readline";
1535 }
1536 if (strcmp(mode, "readline") == 0) {
1537 flags = MONITOR_USE_READLINE;
1538 } else if (strcmp(mode, "control") == 0) {
1539 flags = MONITOR_USE_CONTROL;
1540 } else {
1541 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1542 exit(1);
1543 }
1544
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01001545 if (qemu_opt_get_bool(opts, "pretty", 0))
1546 flags |= MONITOR_USE_PRETTY;
1547
Gerd Hoffmann88589342009-12-08 13:11:50 +01001548 if (qemu_opt_get_bool(opts, "default", 0))
1549 flags |= MONITOR_IS_DEFAULT;
1550
1551 chardev = qemu_opt_get(opts, "chardev");
1552 chr = qemu_chr_find(chardev);
1553 if (chr == NULL) {
1554 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1555 exit(1);
1556 }
1557
1558 monitor_init(chr, flags);
1559 return 0;
1560}
1561
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001562static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001563{
1564 static int monitor_device_index = 0;
1565 QemuOpts *opts;
1566 const char *p;
1567 char label[32];
1568 int def = 0;
1569
1570 if (strstart(optarg, "chardev:", &p)) {
1571 snprintf(label, sizeof(label), "%s", p);
1572 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001573 snprintf(label, sizeof(label), "compat_monitor%d",
1574 monitor_device_index);
1575 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001576 def = 1;
1577 }
1578 opts = qemu_chr_parse_compat(label, optarg);
1579 if (!opts) {
1580 fprintf(stderr, "parse error: %s\n", optarg);
1581 exit(1);
1582 }
1583 }
1584
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001585 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001586 if (!opts) {
1587 fprintf(stderr, "duplicate chardev: %s\n", label);
1588 exit(1);
1589 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001590 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001591 qemu_opt_set(opts, "chardev", label);
1592 if (def)
1593 qemu_opt_set(opts, "default", "on");
1594 monitor_device_index++;
1595}
1596
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001597struct device_config {
1598 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001599 DEV_USB, /* -usbdevice */
1600 DEV_BT, /* -bt */
1601 DEV_SERIAL, /* -serial */
1602 DEV_PARALLEL, /* -parallel */
1603 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001604 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001605 } type;
1606 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001607 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001608};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001609QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001610
1611static void add_device_config(int type, const char *cmdline)
1612{
1613 struct device_config *conf;
1614
1615 conf = qemu_mallocz(sizeof(*conf));
1616 conf->type = type;
1617 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001618 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001619}
1620
1621static int foreach_device_config(int type, int (*func)(const char *cmdline))
1622{
1623 struct device_config *conf;
1624 int rc;
1625
Blue Swirl72cf2d42009-09-12 07:36:22 +00001626 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001627 if (conf->type != type)
1628 continue;
1629 rc = func(conf->cmdline);
1630 if (0 != rc)
1631 return rc;
1632 }
1633 return 0;
1634}
1635
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001636static int serial_parse(const char *devname)
1637{
1638 static int index = 0;
1639 char label[32];
1640
1641 if (strcmp(devname, "none") == 0)
1642 return 0;
1643 if (index == MAX_SERIAL_PORTS) {
1644 fprintf(stderr, "qemu: too many serial ports\n");
1645 exit(1);
1646 }
1647 snprintf(label, sizeof(label), "serial%d", index);
1648 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1649 if (!serial_hds[index]) {
1650 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1651 devname, strerror(errno));
1652 return -1;
1653 }
1654 index++;
1655 return 0;
1656}
1657
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01001658static int parallel_parse(const char *devname)
1659{
1660 static int index = 0;
1661 char label[32];
1662
1663 if (strcmp(devname, "none") == 0)
1664 return 0;
1665 if (index == MAX_PARALLEL_PORTS) {
1666 fprintf(stderr, "qemu: too many parallel ports\n");
1667 exit(1);
1668 }
1669 snprintf(label, sizeof(label), "parallel%d", index);
1670 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1671 if (!parallel_hds[index]) {
1672 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1673 devname, strerror(errno));
1674 return -1;
1675 }
1676 index++;
1677 return 0;
1678}
1679
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001680static int virtcon_parse(const char *devname)
1681{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001682 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001683 static int index = 0;
1684 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05301685 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001686
1687 if (strcmp(devname, "none") == 0)
1688 return 0;
1689 if (index == MAX_VIRTIO_CONSOLES) {
1690 fprintf(stderr, "qemu: too many virtio consoles\n");
1691 exit(1);
1692 }
Amit Shah392ecf52010-01-21 16:19:23 +05301693
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001694 bus_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301695 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1696
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001697 dev_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301698 qemu_opt_set(dev_opts, "driver", "virtconsole");
1699
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001700 snprintf(label, sizeof(label), "virtcon%d", index);
1701 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1702 if (!virtcon_hds[index]) {
1703 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1704 devname, strerror(errno));
1705 return -1;
1706 }
Amit Shah392ecf52010-01-21 16:19:23 +05301707 qemu_opt_set(dev_opts, "chardev", label);
1708
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001709 index++;
1710 return 0;
1711}
1712
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001713static int debugcon_parse(const char *devname)
1714{
1715 QemuOpts *opts;
1716
1717 if (!qemu_chr_open("debugcon", devname, NULL)) {
1718 exit(1);
1719 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001720 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001721 if (!opts) {
1722 fprintf(stderr, "qemu: already have a debugcon device\n");
1723 exit(1);
1724 }
1725 qemu_opt_set(opts, "driver", "isa-debugcon");
1726 qemu_opt_set(opts, "chardev", "debugcon");
1727 return 0;
1728}
1729
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001730void qemu_add_exit_notifier(Notifier *notify)
1731{
1732 notifier_list_add(&exit_notifiers, notify);
1733}
1734
1735void qemu_remove_exit_notifier(Notifier *notify)
1736{
1737 notifier_list_remove(&exit_notifiers, notify);
1738}
1739
1740static void qemu_run_exit_notifiers(void)
1741{
1742 notifier_list_notify(&exit_notifiers);
1743}
1744
Anthony Liguori6530a972010-01-22 09:18:06 -06001745static const QEMUOption *lookup_opt(int argc, char **argv,
1746 const char **poptarg, int *poptind)
1747{
1748 const QEMUOption *popt;
1749 int optind = *poptind;
1750 char *r = argv[optind];
1751 const char *optarg;
1752
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001753 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06001754 optind++;
1755 /* Treat --foo the same as -foo. */
1756 if (r[1] == '-')
1757 r++;
1758 popt = qemu_options;
1759 for(;;) {
1760 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001761 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06001762 exit(1);
1763 }
1764 if (!strcmp(popt->name, r + 1))
1765 break;
1766 popt++;
1767 }
1768 if (popt->flags & HAS_ARG) {
1769 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001770 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06001771 exit(1);
1772 }
1773 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001774 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06001775 } else {
1776 optarg = NULL;
1777 }
1778
1779 *poptarg = optarg;
1780 *poptind = optind;
1781
1782 return popt;
1783}
1784
malc902b3d52008-12-10 19:18:40 +00001785int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00001786{
aliguori59030a82009-04-05 18:43:41 +00001787 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00001788 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03001789 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01001790 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00001791 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00001792 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05001793 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00001794 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00001795 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00001796 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001797 QemuOpts *hda_opts = NULL, *opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02001798 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00001799 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06001800 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00001801 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001802 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00001803 const char *cpu_model;
bellard26a5f132008-05-28 12:30:31 +00001804 int tb_size;
ths93815bc2007-03-19 15:58:31 +00001805 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00001806 const char *incoming = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05001807 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06001808 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00001809
Prerna Saxenaab6540d2010-08-09 11:48:32 +01001810#ifdef CONFIG_SIMPLE_TRACE
1811 const char *trace_file = NULL;
1812#endif
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001813 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01001814 error_set_progname(argv[0]);
1815
Jan Kiszka68752042009-09-15 13:36:04 +02001816 init_clocks();
1817
malc902b3d52008-12-10 19:18:40 +00001818 qemu_cache_utils_init(envp);
1819
Blue Swirl72cf2d42009-09-12 07:36:22 +00001820 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02001821 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00001822
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05001823 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05001824 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00001825 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00001826 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00001827 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00001828 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00001829 kernel_filename = NULL;
1830 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00001831 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00001832 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00001833
aliguori268a3622009-04-21 22:30:27 +00001834 for (i = 0; i < MAX_NODES; i++) {
1835 node_mem[i] = 0;
1836 node_cpumask[i] = 0;
1837 }
1838
aliguori268a3622009-04-21 22:30:27 +00001839 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00001840 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00001841
bellard26a5f132008-05-28 12:30:31 +00001842 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00001843 autostart= 1;
1844
Anthony Liguori292444c2010-01-21 10:57:58 -06001845 /* first pass of option parsing */
1846 optind = 1;
1847 while (optind < argc) {
1848 if (argv[optind][0] != '-') {
1849 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06001850 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06001851 continue;
1852 } else {
1853 const QEMUOption *popt;
1854
1855 popt = lookup_opt(argc, argv, &optarg, &optind);
1856 switch (popt->index) {
1857 case QEMU_OPTION_nodefconfig:
1858 defconfig=0;
1859 break;
1860 }
1861 }
1862 }
1863
1864 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001865 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01001866
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001867 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02001868 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001869 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001870 }
1871
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001872 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02001873 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001874 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001875 }
1876 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00001877 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06001878
1879 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00001880 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00001881 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00001882 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00001883 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06001884 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001885 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00001886 } else {
1887 const QEMUOption *popt;
1888
Anthony Liguori6530a972010-01-22 09:18:06 -06001889 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00001890 if (!(popt->arch_mask & arch_type)) {
1891 printf("Option %s not supported for this target\n", popt->name);
1892 exit(1);
1893 }
bellardcd6f1162004-05-13 22:02:20 +00001894 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00001895 case QEMU_OPTION_M:
1896 machine = find_machine(optarg);
1897 if (!machine) {
1898 QEMUMachine *m;
1899 printf("Supported machines are:\n");
1900 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001901 if (m->alias)
1902 printf("%-10s %s (alias of %s)\n",
1903 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00001904 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00001905 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05001906 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00001907 }
ths15f82202007-06-29 23:26:08 +00001908 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00001909 }
1910 break;
j_mayer94fc95c2007-03-05 19:44:02 +00001911 case QEMU_OPTION_cpu:
1912 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00001913 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00001914 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00001915 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00001916 } else {
1917 cpu_model = optarg;
1918 }
1919 break;
bellardcd6f1162004-05-13 22:02:20 +00001920 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00001921 initrd_filename = optarg;
1922 break;
bellardcd6f1162004-05-13 22:02:20 +00001923 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00001924 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001925 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00001926 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001927 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00001928 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00001929 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00001930 translation == BIOS_ATA_TRANSLATION_LBA ?
1931 ",trans=lba" :
1932 translation == BIOS_ATA_TRANSLATION_NONE ?
1933 ",trans=none" : "");
1934 break;
bellardcd6f1162004-05-13 22:02:20 +00001935 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00001936 case QEMU_OPTION_hdc:
1937 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00001938 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00001939 break;
thse4bcb142007-12-02 04:51:10 +00001940 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00001941 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00001942 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02001943 case QEMU_OPTION_set:
1944 if (qemu_set_option(optarg) != 0)
1945 exit(1);
1946 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01001947 case QEMU_OPTION_global:
1948 if (qemu_global_option(optarg) != 0)
1949 exit(1);
1950 break;
balrog3e3d5812007-04-30 02:09:25 +00001951 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00001952 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00001953 break;
pbrooka1bb27b2007-04-06 16:49:48 +00001954 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00001955 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00001956 break;
j_mayer86f55662007-04-24 06:52:59 +00001957 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00001958 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00001959 break;
bellardcd6f1162004-05-13 22:02:20 +00001960 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00001961 snapshot = 1;
1962 break;
bellardcd6f1162004-05-13 22:02:20 +00001963 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00001964 {
bellard330d0412003-07-26 18:11:40 +00001965 const char *p;
1966 p = optarg;
1967 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001968 if (cyls < 1 || cyls > 16383)
1969 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00001970 if (*p != ',')
1971 goto chs_fail;
1972 p++;
1973 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001974 if (heads < 1 || heads > 16)
1975 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00001976 if (*p != ',')
1977 goto chs_fail;
1978 p++;
1979 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001980 if (secs < 1 || secs > 63)
1981 goto chs_fail;
1982 if (*p == ',') {
1983 p++;
1984 if (!strcmp(p, "none"))
1985 translation = BIOS_ATA_TRANSLATION_NONE;
1986 else if (!strcmp(p, "lba"))
1987 translation = BIOS_ATA_TRANSLATION_LBA;
1988 else if (!strcmp(p, "auto"))
1989 translation = BIOS_ATA_TRANSLATION_AUTO;
1990 else
1991 goto chs_fail;
1992 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00001993 chs_fail:
bellard46d47672004-11-16 01:45:27 +00001994 fprintf(stderr, "qemu: invalid physical CHS format\n");
1995 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00001996 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001997 if (hda_opts != NULL) {
1998 char num[16];
1999 snprintf(num, sizeof(num), "%d", cyls);
2000 qemu_opt_set(hda_opts, "cyls", num);
2001 snprintf(num, sizeof(num), "%d", heads);
2002 qemu_opt_set(hda_opts, "heads", num);
2003 snprintf(num, sizeof(num), "%d", secs);
2004 qemu_opt_set(hda_opts, "secs", num);
2005 if (translation == BIOS_ATA_TRANSLATION_LBA)
2006 qemu_opt_set(hda_opts, "trans", "lba");
2007 if (translation == BIOS_ATA_TRANSLATION_NONE)
2008 qemu_opt_set(hda_opts, "trans", "none");
2009 }
bellard330d0412003-07-26 18:11:40 +00002010 }
2011 break;
aliguori268a3622009-04-21 22:30:27 +00002012 case QEMU_OPTION_numa:
2013 if (nb_numa_nodes >= MAX_NODES) {
2014 fprintf(stderr, "qemu: too many NUMA nodes\n");
2015 exit(1);
2016 }
2017 numa_add(optarg);
2018 break;
bellardcd6f1162004-05-13 22:02:20 +00002019 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002020 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002021 break;
balrog4d3b6f62008-02-10 16:33:14 +00002022#ifdef CONFIG_CURSES
2023 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002024 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00002025 break;
2026#endif
balroga171fe32007-04-30 01:48:07 +00002027 case QEMU_OPTION_portrait:
2028 graphic_rotate = 1;
2029 break;
bellardcd6f1162004-05-13 22:02:20 +00002030 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002031 kernel_filename = optarg;
2032 break;
bellardcd6f1162004-05-13 22:02:20 +00002033 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002034 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002035 break;
bellardcd6f1162004-05-13 22:02:20 +00002036 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00002037 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00002038 break;
bellardcd6f1162004-05-13 22:02:20 +00002039 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002040 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002041 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002042 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002043 };
2044 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002045 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002046 int legacy = 0;
2047
2048 if (!strchr(optarg, '=')) {
2049 legacy = 1;
2050 pstrcpy(buf, sizeof(buf), optarg);
2051 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2052 fprintf(stderr,
2053 "qemu: unknown boot parameter '%s' in '%s'\n",
2054 buf, optarg);
2055 exit(1);
2056 }
2057
2058 if (legacy ||
2059 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002060 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002061 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002062 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002063 if (!legacy) {
2064 if (get_param_value(buf, sizeof(buf),
2065 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002066 validate_bootdevices(buf);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002067 standard_boot_devices = qemu_strdup(boot_devices);
2068 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2069 qemu_register_reset(restore_boot_devices,
2070 standard_boot_devices);
2071 }
Jan Kiszka95387492009-07-02 00:19:02 +02002072 if (get_param_value(buf, sizeof(buf),
2073 "menu", optarg)) {
2074 if (!strcmp(buf, "on")) {
2075 boot_menu = 1;
2076 } else if (!strcmp(buf, "off")) {
2077 boot_menu = 0;
2078 } else {
2079 fprintf(stderr,
2080 "qemu: invalid option value '%s'\n",
2081 buf);
2082 exit(1);
2083 }
2084 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002085 }
bellard36b486b2003-11-11 13:36:08 +00002086 }
2087 break;
bellardcd6f1162004-05-13 22:02:20 +00002088 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002089 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00002090 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00002091 break;
bellard52ca8d62006-06-14 16:03:05 +00002092 case QEMU_OPTION_no_fd_bootchk:
2093 fd_bootchk = 0;
2094 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002095 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002096 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002097 exit(1);
2098 }
2099 break;
bellard7c9d8e02005-11-15 22:16:05 +00002100 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002101 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002102 exit(1);
2103 }
bellard702c6512004-04-02 21:21:32 +00002104 break;
bellardc7f74642004-08-24 21:57:12 +00002105#ifdef CONFIG_SLIRP
2106 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002107 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002108 break;
ths47d5d012007-02-20 00:05:08 +00002109 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002110 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002111 break;
bellard9bf05442004-08-25 22:12:49 +00002112 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002113 if (net_slirp_redir(optarg) < 0)
2114 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002115 break;
bellardc7f74642004-08-24 21:57:12 +00002116#endif
balrogdc72ac12008-11-09 00:04:26 +00002117 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002118 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002119 break;
bellard1d14ffa2005-10-30 18:58:22 +00002120 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002121 if (!(audio_available())) {
2122 printf("Option %s not supported for this target\n", popt->name);
2123 exit(1);
2124 }
bellard1d14ffa2005-10-30 18:58:22 +00002125 AUD_help ();
2126 exit (0);
2127 break;
2128 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002129 if (!(audio_available())) {
2130 printf("Option %s not supported for this target\n", popt->name);
2131 exit(1);
2132 }
bellard1d14ffa2005-10-30 18:58:22 +00002133 select_soundhw (optarg);
2134 break;
bellardcd6f1162004-05-13 22:02:20 +00002135 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002136 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002137 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002138 case QEMU_OPTION_version:
2139 version();
2140 exit(0);
2141 break;
aurel3200f82b82008-04-27 21:12:55 +00002142 case QEMU_OPTION_m: {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002143 ssize_t value;
aurel3200f82b82008-04-27 21:12:55 +00002144
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002145 value = strtosz(optarg, NULL);
2146 if (value < 0) {
aurel3200f82b82008-04-27 21:12:55 +00002147 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002148 exit(1);
2149 }
aurel3200f82b82008-04-27 21:12:55 +00002150
2151 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002152 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00002153 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2154 exit(1);
2155 }
Anthony Liguoric227f092009-10-01 16:12:16 -05002156 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002157 fprintf(stderr, "qemu: ram size too large\n");
2158 exit(1);
2159 }
2160 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002161 break;
aurel3200f82b82008-04-27 21:12:55 +00002162 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002163 case QEMU_OPTION_mempath:
2164 mem_path = optarg;
2165 break;
2166#ifdef MAP_POPULATE
2167 case QEMU_OPTION_mem_prealloc:
2168 mem_prealloc = 1;
2169 break;
2170#endif
bellardcd6f1162004-05-13 22:02:20 +00002171 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002172 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00002173 break;
bellardcd6f1162004-05-13 22:02:20 +00002174 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002175 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002176 break;
aliguori59030a82009-04-05 18:43:41 +00002177 case QEMU_OPTION_gdb:
2178 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002179 break;
bellardcd6f1162004-05-13 22:02:20 +00002180 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002181 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002182 break;
j_mayer1192dad2007-10-05 13:08:35 +00002183 case QEMU_OPTION_bios:
2184 bios_name = optarg;
2185 break;
aurel321b530a62009-04-05 20:08:59 +00002186 case QEMU_OPTION_singlestep:
2187 singlestep = 1;
2188 break;
bellardcd6f1162004-05-13 22:02:20 +00002189 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002190 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002191 break;
bellard3d11d0e2004-12-12 16:56:30 +00002192 case QEMU_OPTION_k:
2193 keyboard_layout = optarg;
2194 break;
bellardee22c2f2004-06-03 12:49:50 +00002195 case QEMU_OPTION_localtime:
2196 rtc_utc = 0;
2197 break;
malc3893c122008-09-28 00:42:05 +00002198 case QEMU_OPTION_vga:
2199 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002200 break;
bellarde9b137c2004-06-21 16:46:10 +00002201 case QEMU_OPTION_g:
2202 {
2203 const char *p;
2204 int w, h, depth;
2205 p = optarg;
2206 w = strtol(p, (char **)&p, 10);
2207 if (w <= 0) {
2208 graphic_error:
2209 fprintf(stderr, "qemu: invalid resolution or depth\n");
2210 exit(1);
2211 }
2212 if (*p != 'x')
2213 goto graphic_error;
2214 p++;
2215 h = strtol(p, (char **)&p, 10);
2216 if (h <= 0)
2217 goto graphic_error;
2218 if (*p == 'x') {
2219 p++;
2220 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002221 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002222 depth != 24 && depth != 32)
2223 goto graphic_error;
2224 } else if (*p == '\0') {
2225 depth = graphic_depth;
2226 } else {
2227 goto graphic_error;
2228 }
ths3b46e622007-09-17 08:09:54 +00002229
bellarde9b137c2004-06-21 16:46:10 +00002230 graphic_width = w;
2231 graphic_height = h;
2232 graphic_depth = depth;
2233 }
2234 break;
ths20d8a3e2007-02-18 17:04:49 +00002235 case QEMU_OPTION_echr:
2236 {
2237 char *r;
2238 term_escape_char = strtol(optarg, &r, 0);
2239 if (r == optarg)
2240 printf("Bad argument to echr\n");
2241 break;
2242 }
bellard82c643f2004-07-14 17:28:13 +00002243 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002244 monitor_parse(optarg, "readline");
2245 default_monitor = 0;
2246 break;
2247 case QEMU_OPTION_qmp:
2248 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002249 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002250 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002251 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002252 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002253 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002254 exit(1);
2255 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002256 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002257 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002258 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002259 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002260 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002261 exit(1);
2262 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002263 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302264 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002265 olist = qemu_find_opts("fsdev");
2266 if (!olist) {
2267 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2268 exit(1);
2269 }
2270 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302271 if (!opts) {
2272 fprintf(stderr, "parse error: %s\n", optarg);
2273 exit(1);
2274 }
2275 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302276 case QEMU_OPTION_virtfs: {
2277 char *arg_fsdev = NULL;
2278 char *arg_9p = NULL;
2279 int len = 0;
2280
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002281 olist = qemu_find_opts("virtfs");
2282 if (!olist) {
2283 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2284 exit(1);
2285 }
2286 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302287 if (!opts) {
2288 fprintf(stderr, "parse error: %s\n", optarg);
2289 exit(1);
2290 }
2291
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002292 if (qemu_opt_get(opts, "fstype") == NULL ||
2293 qemu_opt_get(opts, "mount_tag") == NULL ||
2294 qemu_opt_get(opts, "path") == NULL ||
2295 qemu_opt_get(opts, "security_model") == NULL) {
2296 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +05302297 "security_model=[mapped|passthrough|none],"
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002298 "mnt_tag=tag.\n");
2299 exit(1);
2300 }
2301
2302 len = strlen(",id=,path=,security_model=");
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302303 len += strlen(qemu_opt_get(opts, "fstype"));
2304 len += strlen(qemu_opt_get(opts, "mount_tag"));
2305 len += strlen(qemu_opt_get(opts, "path"));
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002306 len += strlen(qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302307 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2308
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302309 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002310 "%s,id=%s,path=%s,security_model=%s",
2311 qemu_opt_get(opts, "fstype"),
2312 qemu_opt_get(opts, "mount_tag"),
2313 qemu_opt_get(opts, "path"),
2314 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302315
2316 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2317 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2318 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2319
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302320 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002321 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2322 qemu_opt_get(opts, "mount_tag"),
2323 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302324
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002325 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302326 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2327 exit(1);
2328 }
2329
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002330 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302331 fprintf(stderr, "parse error [device]: %s\n", optarg);
2332 exit(1);
2333 }
2334
2335 qemu_free(arg_fsdev);
2336 qemu_free(arg_9p);
2337 break;
2338 }
bellard82c643f2004-07-14 17:28:13 +00002339 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002340 add_device_config(DEV_SERIAL, optarg);
2341 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002342 if (strncmp(optarg, "mon:", 4) == 0) {
2343 default_monitor = 0;
2344 }
bellard82c643f2004-07-14 17:28:13 +00002345 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002346 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002347 if (watchdog) {
2348 fprintf(stderr,
2349 "qemu: only one watchdog option may be given\n");
2350 return 1;
2351 }
2352 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002353 break;
2354 case QEMU_OPTION_watchdog_action:
2355 if (select_watchdog_action(optarg) == -1) {
2356 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2357 exit(1);
2358 }
2359 break;
aliguori51ecf132009-01-15 20:06:40 +00002360 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002361 add_device_config(DEV_VIRTCON, optarg);
2362 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002363 if (strncmp(optarg, "mon:", 4) == 0) {
2364 default_monitor = 0;
2365 }
aliguori51ecf132009-01-15 20:06:40 +00002366 break;
bellard6508fe52005-01-15 12:02:56 +00002367 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002368 add_device_config(DEV_PARALLEL, optarg);
2369 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002370 if (strncmp(optarg, "mon:", 4) == 0) {
2371 default_monitor = 0;
2372 }
bellard6508fe52005-01-15 12:02:56 +00002373 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002374 case QEMU_OPTION_debugcon:
2375 add_device_config(DEV_DEBUGCON, optarg);
2376 break;
bellardd63d3072004-10-03 13:29:03 +00002377 case QEMU_OPTION_loadvm:
2378 loadvm = optarg;
2379 break;
2380 case QEMU_OPTION_full_screen:
2381 full_screen = 1;
2382 break;
ths667acca2006-12-11 02:08:05 +00002383#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002384 case QEMU_OPTION_no_frame:
2385 no_frame = 1;
2386 break;
ths3780e192007-06-21 21:08:02 +00002387 case QEMU_OPTION_alt_grab:
2388 alt_grab = 1;
2389 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002390 case QEMU_OPTION_ctrl_grab:
2391 ctrl_grab = 1;
2392 break;
ths667acca2006-12-11 02:08:05 +00002393 case QEMU_OPTION_no_quit:
2394 no_quit = 1;
2395 break;
aliguori7d957bd2009-01-15 22:14:11 +00002396 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002397 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002398 break;
ths667acca2006-12-11 02:08:05 +00002399#endif
bellardf7cce892004-12-08 22:21:25 +00002400 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002401 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002402 break;
bellarda09db212005-04-30 16:10:35 +00002403 case QEMU_OPTION_win2k_hack:
2404 win2k_install_hack = 1;
2405 break;
aliguori73822ec2009-01-15 20:11:34 +00002406 case QEMU_OPTION_rtc_td_hack:
2407 rtc_td_hack = 1;
2408 break;
aliguori8a92ea22009-02-27 20:12:36 +00002409 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002410 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002411 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002412 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002413 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002414 break;
aliguori7ba1e612008-11-05 16:04:33 +00002415 case QEMU_OPTION_enable_kvm:
2416 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00002417 break;
bellardbb36d472005-11-05 14:22:28 +00002418 case QEMU_OPTION_usb:
2419 usb_enabled = 1;
2420 break;
bellarda594cfb2005-11-06 16:13:29 +00002421 case QEMU_OPTION_usbdevice:
2422 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002423 add_device_config(DEV_USB, optarg);
2424 break;
2425 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002426 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002427 exit(1);
2428 }
bellarda594cfb2005-11-06 16:13:29 +00002429 break;
bellard6a00d602005-11-21 23:25:50 +00002430 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002431 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002432 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002433 fprintf(stderr, "Invalid number of CPUs\n");
2434 exit(1);
2435 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002436 if (max_cpus < smp_cpus) {
2437 fprintf(stderr, "maxcpus must be equal to or greater than "
2438 "smp\n");
2439 exit(1);
2440 }
2441 if (max_cpus > 255) {
2442 fprintf(stderr, "Unsupported number of maxcpus\n");
2443 exit(1);
2444 }
bellard6a00d602005-11-21 23:25:50 +00002445 break;
bellard24236862006-04-30 21:28:36 +00002446 case QEMU_OPTION_vnc:
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002447 display_remote++;
ths73fc9742006-12-22 02:09:07 +00002448 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00002449 break;
bellard6515b202006-05-03 22:02:44 +00002450 case QEMU_OPTION_no_acpi:
2451 acpi_enabled = 0;
2452 break;
aliguori16b29ae2008-12-17 23:28:44 +00002453 case QEMU_OPTION_no_hpet:
2454 no_hpet = 1;
2455 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002456 case QEMU_OPTION_balloon:
2457 if (balloon_parse(optarg) < 0) {
2458 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2459 exit(1);
2460 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002461 break;
bellardd1beab82006-10-02 19:44:22 +00002462 case QEMU_OPTION_no_reboot:
2463 no_reboot = 1;
2464 break;
aurel32b2f76162008-04-11 21:35:52 +00002465 case QEMU_OPTION_no_shutdown:
2466 no_shutdown = 1;
2467 break;
balrog9467cd42007-05-01 01:34:14 +00002468 case QEMU_OPTION_show_cursor:
2469 cursor_hide = 0;
2470 break;
blueswir18fcb1b92008-09-18 18:29:08 +00002471 case QEMU_OPTION_uuid:
2472 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2473 fprintf(stderr, "Fail to parse UUID string."
2474 " Wrong format.\n");
2475 exit(1);
2476 }
2477 break;
ths9ae02552007-01-05 17:39:04 +00002478 case QEMU_OPTION_option_rom:
2479 if (nb_option_roms >= MAX_OPTION_ROMS) {
2480 fprintf(stderr, "Too many option ROMs\n");
2481 exit(1);
2482 }
2483 option_rom[nb_option_roms] = optarg;
2484 nb_option_roms++;
2485 break;
pbrook8e716212007-01-20 17:12:09 +00002486 case QEMU_OPTION_semihosting:
2487 semihosting_enabled = 1;
2488 break;
thsc35734b2007-03-19 15:17:08 +00002489 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02002490 qemu_name = qemu_strdup(optarg);
2491 {
2492 char *p = strchr(qemu_name, ',');
2493 if (p != NULL) {
2494 *p++ = 0;
2495 if (strncmp(p, "process=", 8)) {
2496 fprintf(stderr, "Unknown subargument %s to -name", p);
2497 exit(1);
2498 }
2499 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02002500 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02002501 }
2502 }
thsc35734b2007-03-19 15:17:08 +00002503 break;
blueswir166508602007-05-01 14:16:52 +00002504 case QEMU_OPTION_prom_env:
2505 if (nb_prom_envs >= MAX_PROM_ENVS) {
2506 fprintf(stderr, "Too many prom variables\n");
2507 exit(1);
2508 }
2509 prom_envs[nb_prom_envs] = optarg;
2510 nb_prom_envs++;
2511 break;
balrog2b8f2d42007-07-27 22:08:46 +00002512 case QEMU_OPTION_old_param:
2513 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00002514 break;
thsf3dcfad2007-08-24 01:26:02 +00002515 case QEMU_OPTION_clock:
2516 configure_alarms(optarg);
2517 break;
bellard7e0af5d02007-11-07 16:24:33 +00002518 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002519 configure_rtc_date_offset(optarg, 1);
2520 break;
2521 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002522 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002523 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002524 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00002525 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002526 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00002527 break;
bellard26a5f132008-05-28 12:30:31 +00002528 case QEMU_OPTION_tb_size:
2529 tb_size = strtol(optarg, NULL, 0);
2530 if (tb_size < 0)
2531 tb_size = 0;
2532 break;
pbrook2e70f6e2008-06-29 01:03:05 +00002533 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002534 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00002535 break;
aliguori5bb79102008-10-13 03:12:02 +00002536 case QEMU_OPTION_incoming:
2537 incoming = optarg;
Amit Shah8e848652010-07-27 15:49:19 +05302538 incoming_expected = true;
aliguori5bb79102008-10-13 03:12:02 +00002539 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002540 case QEMU_OPTION_nodefaults:
2541 default_serial = 0;
2542 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002543 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002544 default_monitor = 0;
2545 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01002546 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002547 default_floppy = 0;
2548 default_cdrom = 0;
2549 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002550 break;
aliguorie37630c2009-04-22 15:19:10 +00002551 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00002552 if (!(xen_available())) {
2553 printf("Option %s not supported for this target\n", popt->name);
2554 exit(1);
2555 }
aliguorie37630c2009-04-22 15:19:10 +00002556 xen_domid = atoi(optarg);
2557 break;
2558 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00002559 if (!(xen_available())) {
2560 printf("Option %s not supported for this target\n", popt->name);
2561 exit(1);
2562 }
aliguorie37630c2009-04-22 15:19:10 +00002563 xen_mode = XEN_CREATE;
2564 break;
2565 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00002566 if (!(xen_available())) {
2567 printf("Option %s not supported for this target\n", popt->name);
2568 exit(1);
2569 }
aliguorie37630c2009-04-22 15:19:10 +00002570 xen_mode = XEN_ATTACH;
2571 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002572#ifdef CONFIG_SIMPLE_TRACE
2573 case QEMU_OPTION_trace:
2574 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2575 if (opts) {
2576 trace_file = qemu_opt_get(opts, "file");
2577 }
2578 break;
2579#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002580 case QEMU_OPTION_readconfig:
2581 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002582 int ret = qemu_read_config_file(optarg);
2583 if (ret < 0) {
2584 fprintf(stderr, "read config %s: %s\n", optarg,
2585 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002586 exit(1);
2587 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002588 break;
2589 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03002590 case QEMU_OPTION_spice:
2591 olist = qemu_find_opts("spice");
2592 if (!olist) {
2593 fprintf(stderr, "spice is not supported by this qemu build.\n");
2594 exit(1);
2595 }
2596 opts = qemu_opts_parse(olist, optarg, 0);
2597 if (!opts) {
2598 fprintf(stderr, "parse error: %s\n", optarg);
2599 exit(1);
2600 }
2601 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002602 case QEMU_OPTION_writeconfig:
2603 {
2604 FILE *fp;
2605 if (strcmp(optarg, "-") == 0) {
2606 fp = stdout;
2607 } else {
2608 fp = fopen(optarg, "w");
2609 if (fp == NULL) {
2610 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2611 exit(1);
2612 }
2613 }
2614 qemu_config_write(fp);
2615 fclose(fp);
2616 break;
2617 }
Jes Sorensen59a52642010-06-10 11:42:25 +02002618 default:
2619 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00002620 }
bellard0824d6f2003-06-24 13:42:40 +00002621 }
2622 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002623 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00002624
Paul Brook5cea8592009-05-30 00:52:44 +01002625 /* If no data_dir is specified then try to find it relative to the
2626 executable path. */
2627 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02002628 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01002629 }
2630 /* If all else fails use the install patch specified when building. */
2631 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02002632 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01002633 }
2634
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002635#ifdef CONFIG_SIMPLE_TRACE
2636 /*
2637 * Set the trace file name, if specified.
2638 */
2639 st_set_trace_file(trace_file);
2640#endif
Jes Sorensen6be68d72009-07-23 17:03:42 +02002641 /*
2642 * Default to max_cpus = smp_cpus, in case the user doesn't
2643 * specify a max_cpus value.
2644 */
2645 if (!max_cpus)
2646 max_cpus = smp_cpus;
2647
balrog3d878ca2008-10-28 10:59:59 +00002648 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00002649 if (smp_cpus > machine->max_cpus) {
2650 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2651 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2652 machine->max_cpus);
2653 exit(1);
2654 }
2655
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002656 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2657 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002658
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002659 if (machine->no_serial) {
2660 default_serial = 0;
2661 }
2662 if (machine->no_parallel) {
2663 default_parallel = 0;
2664 }
2665 if (!machine->use_virtcon) {
2666 default_virtcon = 0;
2667 }
2668 if (machine->no_vga) {
2669 default_vga = 0;
2670 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002671 if (machine->no_floppy) {
2672 default_floppy = 0;
2673 }
2674 if (machine->no_cdrom) {
2675 default_cdrom = 0;
2676 }
2677 if (machine->no_sdcard) {
2678 default_sdcard = 0;
2679 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002680
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002681 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002682 if (default_parallel)
2683 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002684 if (default_serial && default_monitor) {
2685 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002686 } else if (default_virtcon && default_monitor) {
2687 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002688 } else {
2689 if (default_serial)
2690 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002691 if (default_virtcon)
2692 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002693 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002694 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002695 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002696 } else {
2697 if (default_serial)
2698 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002699 if (default_parallel)
2700 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01002701 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002702 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01002703 if (default_virtcon)
2704 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00002705 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01002706 if (default_vga)
2707 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00002708
TeLeMana5829fd2010-04-15 12:37:55 +08002709 socket_init();
2710
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002711 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002712 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002713#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002714 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302715 exit(1);
2716 }
2717#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002718
Jes Sorenseneb505be2010-06-10 11:42:28 +02002719 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00002720
thsaa26bb22007-03-25 21:33:06 +00002721 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02002722 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00002723 exit(1);
2724 }
2725
Paolo Bonzini98c85732010-04-19 18:59:30 +00002726 if (kvm_allowed) {
2727 int ret = kvm_init(smp_cpus);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002728 if (ret < 0) {
Paolo Bonzini98c85732010-04-19 18:59:30 +00002729 if (!kvm_available()) {
2730 printf("KVM not supported for this target\n");
2731 } else {
2732 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2733 }
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002734 exit(1);
2735 }
2736 }
2737
aliguori3fcf7b62009-04-24 18:03:25 +00002738 if (qemu_init_main_loop()) {
2739 fprintf(stderr, "qemu_init_main_loop failed\n");
2740 exit(1);
2741 }
bellarda20dd502003-09-30 21:07:02 +00002742 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00002743
thsf8d39c02008-07-03 10:01:15 +00002744 if (!linux_boot && *kernel_cmdline != '\0') {
2745 fprintf(stderr, "-append only allowed with -kernel option\n");
2746 exit(1);
2747 }
2748
2749 if (!linux_boot && initrd_filename != NULL) {
2750 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2751 exit(1);
2752 }
2753
Jes Sorensen9156d762010-06-10 11:42:30 +02002754 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00002755
aliguori7183b4b2008-11-05 20:40:18 +00002756 if (init_timer_alarm() < 0) {
2757 fprintf(stderr, "could not initialize alarm timer\n");
2758 exit(1);
2759 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002760 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00002761
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01002762 if (net_init_clients() < 0) {
2763 exit(1);
bellard702c6512004-04-02 21:21:32 +00002764 }
bellardf1510b22003-06-25 00:07:40 +00002765
balrogdc72ac12008-11-09 00:04:26 +00002766 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002767 if (foreach_device_config(DEV_BT, bt_parse))
2768 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00002769
bellard0824d6f2003-06-24 13:42:40 +00002770 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00002771 if (ram_size == 0)
2772 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00002773
bellard26a5f132008-05-28 12:30:31 +00002774 /* init the dynamic translator */
2775 cpu_exec_init_all(tb_size * 1024 * 1024);
2776
Markus Armbrustereb852012009-10-27 18:41:44 +01002777 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00002778
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002779 blk_mig_init();
2780
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002781 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002782 /* we always create the cdrom drive, even if no disk is there */
2783 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002784 }
thse4bcb142007-12-02 04:51:10 +00002785
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002786 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002787 /* we always create at least one floppy */
2788 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002789 }
bellardc4b1fcc2004-03-14 21:44:30 +00002790
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002791 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002792 /* we always create one sd slot, even if no card is in it */
2793 drive_add(NULL, SD_ALIAS);
2794 }
balrog9d413d12007-12-04 00:10:34 +00002795
ths96d30e42007-01-07 20:42:14 +00002796 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002797 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002798 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2799 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002800 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00002801
Alex Williamson97ab12d2010-06-25 11:09:50 -06002802 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002803 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00002804
aliguori268a3622009-04-21 22:30:27 +00002805 if (nb_numa_nodes > 0) {
2806 int i;
2807
2808 if (nb_numa_nodes > smp_cpus) {
2809 nb_numa_nodes = smp_cpus;
2810 }
2811
2812 /* If no memory size if given for any node, assume the default case
2813 * and distribute the available memory equally across all nodes
2814 */
2815 for (i = 0; i < nb_numa_nodes; i++) {
2816 if (node_mem[i] != 0)
2817 break;
2818 }
2819 if (i == nb_numa_nodes) {
2820 uint64_t usedmem = 0;
2821
2822 /* On Linux, the each node's border has to be 8MB aligned,
2823 * the final node gets the rest.
2824 */
2825 for (i = 0; i < nb_numa_nodes - 1; i++) {
2826 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2827 usedmem += node_mem[i];
2828 }
2829 node_mem[i] = ram_size - usedmem;
2830 }
2831
2832 for (i = 0; i < nb_numa_nodes; i++) {
2833 if (node_cpumask[i] != 0)
2834 break;
2835 }
2836 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2837 * must cope with this anyway, because there are BIOSes out there in
2838 * real machines which also use this scheme.
2839 */
2840 if (i == nb_numa_nodes) {
2841 for (i = 0; i < smp_cpus; i++) {
2842 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2843 }
2844 }
2845 }
2846
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002847 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02002848 exit(1);
2849 }
2850
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002851 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2852 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002853 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2854 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002855 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2856 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002857 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2858 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00002859
Paul Brookaae94602009-05-14 22:35:06 +01002860 module_call_init(MODULE_INIT_DEVICE);
2861
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002862 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002863 exit(0);
2864
Markus Armbruster09aaa162009-08-21 10:31:34 +02002865 if (watchdog) {
2866 i = select_watchdog(watchdog);
2867 if (i > 0)
2868 exit (i == 1 ? 1 : 0);
2869 }
2870
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002871 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01002872 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002873 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002874 qemu_add_globals();
2875
Paul Brookfbe1b592009-05-13 17:56:25 +01002876 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00002877 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2878
Jan Kiszkaea375f92010-03-01 19:10:30 +01002879 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00002880
Juan Quintela67b3b712009-08-28 19:25:15 +02002881 /* must be after terminal init, SDL library changes signal handlers */
Jes Sorensen8d963e62010-06-10 11:42:22 +02002882 os_setup_signal_handling();
Juan Quintela67b3b712009-08-28 19:25:15 +02002883
Blue Swirl87d0a282010-03-27 18:24:45 +00002884 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00002885
aliguori6f338c32009-02-11 15:21:54 +00002886 current_machine = machine;
2887
aliguori3023f332009-01-16 19:04:14 +00002888 /* init USB devices */
2889 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01002890 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2891 exit(1);
aliguori3023f332009-01-16 19:04:14 +00002892 }
2893
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002894 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002895 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002896 exit(1);
2897
Markus Armbruster668680f2010-02-11 14:44:58 +01002898 net_check_clients();
2899
aliguori3023f332009-01-16 19:04:14 +00002900 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01002901 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002902
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02002903 if (using_spice)
2904 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002905 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002906#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002907 display_type = DT_SDL;
2908#else
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002909 vnc_display = "localhost:0,to=99";
2910 show_vnc_port = 1;
2911#endif
2912 }
2913
2914
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002915 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002916 switch (display_type) {
2917 case DT_NOGRAPHIC:
2918 break;
2919#if defined(CONFIG_CURSES)
2920 case DT_CURSES:
2921 curses_display_init(ds, full_screen);
2922 break;
2923#endif
bellard5b0753e2005-03-01 21:37:28 +00002924#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002925 case DT_SDL:
2926 sdl_display_init(ds, full_screen, no_frame);
2927 break;
bellard5b0753e2005-03-01 21:37:28 +00002928#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002929 case DT_SDL:
2930 cocoa_display_init(ds, full_screen);
2931 break;
bellard313aa562003-08-10 21:52:11 +00002932#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002933 default:
2934 break;
2935 }
2936
2937 /* init remote displays */
2938 if (vnc_display) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002939 vnc_display_init(ds);
2940 if (vnc_display_open(ds, vnc_display) < 0)
2941 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002942
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002943 if (show_vnc_port) {
2944 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002945 }
bellard313aa562003-08-10 21:52:11 +00002946 }
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02002947#ifdef CONFIG_SPICE
2948 if (using_spice) {
2949 qemu_spice_display_init(ds);
2950 }
2951#endif
aliguori5b08fc12008-08-21 20:08:03 +00002952
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002953 /* display setup */
2954 dpy_resize(ds);
aliguori3023f332009-01-16 19:04:14 +00002955 dcl = ds->listeners;
2956 while (dcl != NULL) {
2957 if (dcl->dpy_refresh != NULL) {
2958 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2959 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09002960 break;
ths20d8a3e2007-02-18 17:04:49 +00002961 }
aliguori3023f332009-01-16 19:04:14 +00002962 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00002963 }
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002964 if (ds->gui_timer == NULL) {
blueswir19043b622009-01-21 19:28:13 +00002965 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2966 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2967 }
Paolo Bonzinib473df62010-02-11 00:29:55 +01002968 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00002969
aliguori59030a82009-04-05 18:43:41 +00002970 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2971 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2972 gdbstub_dev);
2973 exit(1);
balrog45669e02007-07-02 13:20:17 +00002974 }
balrog45669e02007-07-02 13:20:17 +00002975
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02002976 qdev_machine_creation_done();
2977
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01002978 if (rom_load_all() != 0) {
2979 fprintf(stderr, "rom loading failed\n");
2980 exit(1);
2981 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02002982
Juan Quintela504c2942009-11-12 00:39:13 +01002983 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02002984 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01002985 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02002986 autostart = 0;
2987 }
2988 }
bellardd63d3072004-10-03 13:29:03 +00002989
Glauber Costa2bb8c102009-07-24 16:20:23 -04002990 if (incoming) {
Juan Quintela8ca5e802010-06-09 14:10:54 +02002991 int ret = qemu_start_incoming_migration(incoming);
2992 if (ret < 0) {
2993 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
2994 incoming, ret);
2995 exit(ret);
2996 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03002997 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00002998 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03002999 }
thsffd843b2006-12-21 19:46:43 +00003000
Jes Sorenseneb505be2010-06-10 11:42:28 +02003001 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003002
bellard8a7ddc32004-03-31 19:00:16 +00003003 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003004 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003005 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00003006
bellard0824d6f2003-06-24 13:42:40 +00003007 return 0;
3008}