blob: 43d3d0ec21929f6a3f67a5ce1223814f0b23b62d [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>
Juan Quintela71e72a12009-07-27 16:12:56 +020050#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000051#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010052#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000053#include <libutil.h>
Juergen Lock92c0e652010-03-25 22:07:12 +010054#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000055#else
56#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000057#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010058#else
blueswir1223f0d72008-09-30 18:12:18 +000059#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000060#include <pty.h>
61#include <malloc.h>
thsbd494f42007-09-16 20:03:23 +000062
bellarde57a8c02005-11-10 23:58:52 +000063#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000064#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000065#endif
66#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000067#include <sys/stat.h>
68#include <sys/ethernet.h>
69#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000070#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000071#include <netinet/in_systm.h>
72#include <netinet/ip.h>
73#include <netinet/ip_icmp.h> // must come after ip.h
74#include <netinet/udp.h>
75#include <netinet/tcp.h>
76#include <net/if.h>
77#include <syslog.h>
78#include <stropts.h>
Blue Swirl8d32cf02009-10-02 19:32:12 +000079/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
80 discussion about Solaris header problems */
81extern int madvise(caddr_t, size_t, int);
bellard67b915a2004-03-31 23:37:16 +000082#endif
bellard7d3505c2004-05-12 19:32:15 +000083#endif
bellardec530c82006-04-25 22:36:06 +000084#endif
bellard67b915a2004-03-31 23:37:16 +000085
blueswir19892fbf2008-08-24 10:34:20 +000086#if defined(__OpenBSD__)
87#include <util.h>
88#endif
89
ths8a16d272008-07-19 09:56:24 +000090#if defined(CONFIG_VDE)
91#include <libvdeplug.h>
92#endif
93
bellard67b915a2004-03-31 23:37:16 +000094#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000095#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000096#endif
97
bellard73332e52004-04-04 20:22:28 +000098#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +020099#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200100#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000101int qemu_main(int argc, char **argv, char **envp);
102int main(int argc, char **argv)
103{
Stefan Weil59a36a22009-06-18 20:11:03 +0200104 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000105}
106#undef main
107#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000108#endif
bellard73332e52004-04-04 20:22:28 +0000109#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000110
bellard5b0753e2005-03-01 21:37:28 +0000111#ifdef CONFIG_COCOA
112#undef main
113#define main qemu_main
114#endif /* CONFIG_COCOA */
115
blueswir1511d2b12009-03-07 15:32:56 +0000116#include "hw/hw.h"
117#include "hw/boards.h"
118#include "hw/usb.h"
119#include "hw/pcmcia.h"
120#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000121#include "hw/isa.h"
122#include "hw/baum.h"
123#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100124#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000125#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000126#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200127#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200128#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000129#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000130#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000131#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000132#include "monitor.h"
133#include "console.h"
134#include "sysemu.h"
135#include "gdbstub.h"
136#include "qemu-timer.h"
137#include "qemu-char.h"
138#include "cache-utils.h"
139#include "block.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200140#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200141#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000142#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000143#include "audio/audio.h"
144#include "migration.h"
145#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200146#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200147#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200148#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200149#include "qemu-options.h"
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530150#ifdef CONFIG_LINUX
151#include "fsdev/qemu-fsdev.h"
152#endif
blueswir1511d2b12009-03-07 15:32:56 +0000153
bellard0824d6f2003-06-24 13:42:40 +0000154#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000155
blueswir1511d2b12009-03-07 15:32:56 +0000156#include "qemu_socket.h"
157
Jan Kiszkad918f232009-06-24 14:42:30 +0200158#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000159
Blue Swirl72cf2d42009-09-12 07:36:22 +0000160#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000161#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000162#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000163
blueswir19dc63a12008-10-04 07:25:46 +0000164//#define DEBUG_NET
165//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000166
bellard1bfe8562004-07-08 21:17:50 +0000167#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000168
Amit Shah98b19252010-01-20 00:36:52 +0530169#define MAX_VIRTIO_CONSOLES 1
170
Paul Brook5cea8592009-05-30 00:52:44 +0100171static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000172const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000173enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500174DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000175const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500176ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300177const char *mem_path = NULL;
178#ifdef MAP_POPULATE
179int mem_prealloc = 0; /* force preallocation of physical target memory */
180#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000181int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000182NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000183int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200184int autostart;
balrogf6503052008-02-17 11:42:19 +0000185static int rtc_utc = 1;
186static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200187QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100188int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000189static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000190#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000191static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000192#endif
ths667acca2006-12-11 02:08:05 +0000193int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000194CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000195CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000196CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000197int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000198int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000199int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000200int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000201int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200202int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200203int smp_cores = 1;
204int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000205const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000206int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000207int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000208int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000209int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000210int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000211int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000212int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200213uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200214const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000215const char *option_rom[MAX_OPTION_ROMS];
216int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000217int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000218int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000219const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000220int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500221int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000222unsigned int nb_prom_envs = 0;
223const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200224int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000225
aliguori268a3622009-04-21 22:30:27 +0000226int nb_numa_nodes;
227uint64_t node_mem[MAX_NODES];
228uint64_t node_cpumask[MAX_NODES];
229
blueswir19043b622009-01-21 19:28:13 +0000230static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000231
blueswir18fcb1b92008-09-18 18:29:08 +0000232uint8_t qemu_uuid[16];
233
Jan Kiszka76e30d02009-07-02 00:19:02 +0200234static QEMUBootSetHandler *boot_set_handler;
235static void *boot_set_opaque;
236
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200237static NotifierList exit_notifiers =
238 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
239
Blue Swirld745bef2010-03-29 19:23:49 +0000240int kvm_allowed = 0;
241uint32_t xen_domid;
242enum xen_mode xen_mode = XEN_EMULATE;
243
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100244static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100245static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100246static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100247static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100248static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100249static int default_floppy = 1;
250static int default_cdrom = 1;
251static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100252
253static struct {
254 const char *driver;
255 int *flag;
256} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100257 { .driver = "isa-serial", .flag = &default_serial },
258 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100259 { .driver = "isa-fdc", .flag = &default_floppy },
260 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530261 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
262 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
263 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100264 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100265 { .driver = "cirrus-vga", .flag = &default_vga },
266 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100267};
268
269static int default_driver_check(QemuOpts *opts, void *opaque)
270{
271 const char *driver = qemu_opt_get(opts, "driver");
272 int i;
273
274 if (!driver)
275 return 0;
276 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
277 if (strcmp(default_list[i].driver, driver) != 0)
278 continue;
279 *(default_list[i].flag) = 0;
280 }
281 return 0;
282}
283
bellard0824d6f2003-06-24 13:42:40 +0000284/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100285/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000286
bellard87858c82003-06-27 12:01:39 +0000287/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000288uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000289{
290 union {
291 uint64_t ll;
292 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200293#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000294 uint32_t high, low;
295#else
296 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000297#endif
bellard87858c82003-06-27 12:01:39 +0000298 } l;
299 } u, res;
300 uint64_t rl, rh;
301
302 u.ll = a;
303 rl = (uint64_t)u.l.low * (uint64_t)b;
304 rh = (uint64_t)u.l.high * (uint64_t)b;
305 rh += (rl >> 32);
306 res.l.high = rh / c;
307 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
308 return res.ll;
309}
310
bellardc4b1fcc2004-03-14 21:44:30 +0000311/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000312/* host time/date access */
313void qemu_get_timedate(struct tm *tm, int offset)
314{
315 time_t ti;
316 struct tm *ret;
317
318 time(&ti);
319 ti += offset;
320 if (rtc_date_offset == -1) {
321 if (rtc_utc)
322 ret = gmtime(&ti);
323 else
324 ret = localtime(&ti);
325 } else {
326 ti -= rtc_date_offset;
327 ret = gmtime(&ti);
328 }
329
330 memcpy(tm, ret, sizeof(struct tm));
331}
332
333int qemu_timedate_diff(struct tm *tm)
334{
335 time_t seconds;
336
337 if (rtc_date_offset == -1)
338 if (rtc_utc)
339 seconds = mktimegm(tm);
340 else
341 seconds = mktime(tm);
342 else
343 seconds = mktimegm(tm) + rtc_date_offset;
344
345 return seconds - time(NULL);
346}
347
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300348void rtc_change_mon_event(struct tm *tm)
349{
350 QObject *data;
351
352 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
353 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
354 qobject_decref(data);
355}
356
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200357static void configure_rtc_date_offset(const char *startdate, int legacy)
358{
359 time_t rtc_start_date;
360 struct tm tm;
361
362 if (!strcmp(startdate, "now") && legacy) {
363 rtc_date_offset = -1;
364 } else {
365 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
366 &tm.tm_year,
367 &tm.tm_mon,
368 &tm.tm_mday,
369 &tm.tm_hour,
370 &tm.tm_min,
371 &tm.tm_sec) == 6) {
372 /* OK */
373 } else if (sscanf(startdate, "%d-%d-%d",
374 &tm.tm_year,
375 &tm.tm_mon,
376 &tm.tm_mday) == 3) {
377 tm.tm_hour = 0;
378 tm.tm_min = 0;
379 tm.tm_sec = 0;
380 } else {
381 goto date_fail;
382 }
383 tm.tm_year -= 1900;
384 tm.tm_mon--;
385 rtc_start_date = mktimegm(&tm);
386 if (rtc_start_date == -1) {
387 date_fail:
388 fprintf(stderr, "Invalid date format. Valid formats are:\n"
389 "'2006-06-17T16:01:21' or '2006-06-17'\n");
390 exit(1);
391 }
392 rtc_date_offset = time(NULL) - rtc_start_date;
393 }
394}
395
396static void configure_rtc(QemuOpts *opts)
397{
398 const char *value;
399
400 value = qemu_opt_get(opts, "base");
401 if (value) {
402 if (!strcmp(value, "utc")) {
403 rtc_utc = 1;
404 } else if (!strcmp(value, "localtime")) {
405 rtc_utc = 0;
406 } else {
407 configure_rtc_date_offset(value, 0);
408 }
409 }
Jan Kiszka68752042009-09-15 13:36:04 +0200410 value = qemu_opt_get(opts, "clock");
411 if (value) {
412 if (!strcmp(value, "host")) {
413 rtc_clock = host_clock;
414 } else if (!strcmp(value, "vm")) {
415 rtc_clock = vm_clock;
416 } else {
417 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
418 exit(1);
419 }
420 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200421 value = qemu_opt_get(opts, "driftfix");
422 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000423 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200424 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000425 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200426 rtc_td_hack = 0;
427 } else {
428 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
429 exit(1);
430 }
431 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200432}
433
balrog1ae26a12008-09-28 23:19:47 +0000434/***********************************************************/
435/* Bluetooth support */
436static int nb_hcis;
437static int cur_hci;
438static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000439
balrog1ae26a12008-09-28 23:19:47 +0000440static struct bt_vlan_s {
441 struct bt_scatternet_s net;
442 int id;
443 struct bt_vlan_s *next;
444} *first_bt_vlan;
445
446/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000447static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000448{
449 struct bt_vlan_s **pvlan, *vlan;
450 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
451 if (vlan->id == id)
452 return &vlan->net;
453 }
454 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
455 vlan->id = id;
456 pvlan = &first_bt_vlan;
457 while (*pvlan != NULL)
458 pvlan = &(*pvlan)->next;
459 *pvlan = vlan;
460 return &vlan->net;
461}
462
463static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
464{
465}
466
467static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
468{
469 return -ENOTSUP;
470}
471
472static struct HCIInfo null_hci = {
473 .cmd_send = null_hci_send,
474 .sco_send = null_hci_send,
475 .acl_send = null_hci_send,
476 .bdaddr_set = null_hci_addr_set,
477};
478
479struct HCIInfo *qemu_next_hci(void)
480{
481 if (cur_hci == nb_hcis)
482 return &null_hci;
483
484 return hci_table[cur_hci++];
485}
486
balrogdc72ac12008-11-09 00:04:26 +0000487static struct HCIInfo *hci_init(const char *str)
488{
489 char *endp;
490 struct bt_scatternet_s *vlan = 0;
491
492 if (!strcmp(str, "null"))
493 /* null */
494 return &null_hci;
495 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
496 /* host[:hciN] */
497 return bt_host_hci(str[4] ? str + 5 : "hci0");
498 else if (!strncmp(str, "hci", 3)) {
499 /* hci[,vlan=n] */
500 if (str[3]) {
501 if (!strncmp(str + 3, ",vlan=", 6)) {
502 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
503 if (*endp)
504 vlan = 0;
505 }
506 } else
507 vlan = qemu_find_bt_vlan(0);
508 if (vlan)
509 return bt_new_hci(vlan);
510 }
511
512 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
513
514 return 0;
515}
516
517static int bt_hci_parse(const char *str)
518{
519 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500520 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000521
522 if (nb_hcis >= MAX_NICS) {
523 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
524 return -1;
525 }
526
527 hci = hci_init(str);
528 if (!hci)
529 return -1;
530
531 bdaddr.b[0] = 0x52;
532 bdaddr.b[1] = 0x54;
533 bdaddr.b[2] = 0x00;
534 bdaddr.b[3] = 0x12;
535 bdaddr.b[4] = 0x34;
536 bdaddr.b[5] = 0x56 + nb_hcis;
537 hci->bdaddr_set(hci, bdaddr.b);
538
539 hci_table[nb_hcis++] = hci;
540
541 return 0;
542}
543
544static void bt_vhci_add(int vlan_id)
545{
546 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
547
548 if (!vlan->slave)
549 fprintf(stderr, "qemu: warning: adding a VHCI to "
550 "an empty scatternet %i\n", vlan_id);
551
552 bt_vhci_init(bt_new_hci(vlan));
553}
554
555static struct bt_device_s *bt_device_add(const char *opt)
556{
557 struct bt_scatternet_s *vlan;
558 int vlan_id = 0;
559 char *endp = strstr(opt, ",vlan=");
560 int len = (endp ? endp - opt : strlen(opt)) + 1;
561 char devname[10];
562
563 pstrcpy(devname, MIN(sizeof(devname), len), opt);
564
565 if (endp) {
566 vlan_id = strtol(endp + 6, &endp, 0);
567 if (*endp) {
568 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
569 return 0;
570 }
571 }
572
573 vlan = qemu_find_bt_vlan(vlan_id);
574
575 if (!vlan->slave)
576 fprintf(stderr, "qemu: warning: adding a slave device to "
577 "an empty scatternet %i\n", vlan_id);
578
579 if (!strcmp(devname, "keyboard"))
580 return bt_keyboard_init(vlan);
581
582 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
583 return 0;
584}
585
586static int bt_parse(const char *opt)
587{
588 const char *endp, *p;
589 int vlan;
590
591 if (strstart(opt, "hci", &endp)) {
592 if (!*endp || *endp == ',') {
593 if (*endp)
594 if (!strstart(endp, ",vlan=", 0))
595 opt = endp + 1;
596
597 return bt_hci_parse(opt);
598 }
599 } else if (strstart(opt, "vhci", &endp)) {
600 if (!*endp || *endp == ',') {
601 if (*endp) {
602 if (strstart(endp, ",vlan=", &p)) {
603 vlan = strtol(p, (char **) &endp, 0);
604 if (*endp) {
605 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
606 return 1;
607 }
608 } else {
609 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
610 return 1;
611 }
612 } else
613 vlan = 0;
614
615 bt_vhci_add(vlan);
616 return 0;
617 }
618 } else if (strstart(opt, "device:", &endp))
619 return !bt_device_add(endp);
620
621 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
622 return 1;
623}
624
balrog1ae26a12008-09-28 23:19:47 +0000625/***********************************************************/
626/* QEMU Block devices */
627
balrog609497a2008-01-14 02:56:53 +0000628#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +0000629#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +0000630#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +0000631#define PFLASH_ALIAS "if=pflash"
632#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +0000633#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +0000634
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200635static int drive_init_func(QemuOpts *opts, void *opaque)
636{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200637 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200638 int fatal_error = 0;
639
Markus Armbrustera803cb82010-06-02 13:31:55 +0200640 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200641 if (fatal_error)
642 return 1;
643 }
644 return 0;
645}
646
647static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
648{
649 if (NULL == qemu_opt_get(opts, "snapshot")) {
650 qemu_opt_set(opts, "snapshot", "on");
651 }
652 return 0;
653}
654
Jan Kiszka76e30d02009-07-02 00:19:02 +0200655void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
656{
657 boot_set_handler = func;
658 boot_set_opaque = opaque;
659}
660
661int qemu_boot_set(const char *boot_devices)
662{
663 if (!boot_set_handler) {
664 return -EINVAL;
665 }
666 return boot_set_handler(boot_set_opaque, boot_devices);
667}
668
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300669static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200670{
671 /* We just do some generic consistency checks */
672 const char *p;
673 int bitmap = 0;
674
675 for (p = devices; *p != '\0'; p++) {
676 /* Allowed boot devices are:
677 * a-b: floppy disk drives
678 * c-f: IDE disk drives
679 * g-m: machine implementation dependant drives
680 * n-p: network devices
681 * It's up to each machine implementation to check if the given boot
682 * devices match the actual hardware implementation and firmware
683 * features.
684 */
685 if (*p < 'a' || *p > 'p') {
686 fprintf(stderr, "Invalid boot device '%c'\n", *p);
687 exit(1);
688 }
689 if (bitmap & (1 << (*p - 'a'))) {
690 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
691 exit(1);
692 }
693 bitmap |= 1 << (*p - 'a');
694 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200695}
696
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200697static void restore_boot_devices(void *opaque)
698{
699 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600700 static int first = 1;
701
702 /* Restore boot order and remove ourselves after the first boot */
703 if (first) {
704 first = 0;
705 return;
706 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200707
708 qemu_boot_set(standard_boot_devices);
709
710 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
711 qemu_free(standard_boot_devices);
712}
713
aliguori268a3622009-04-21 22:30:27 +0000714static void numa_add(const char *optarg)
715{
716 char option[128];
717 char *endptr;
718 unsigned long long value, endvalue;
719 int nodenr;
720
721 optarg = get_opt_name(option, 128, optarg, ',') + 1;
722 if (!strcmp(option, "node")) {
723 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
724 nodenr = nb_numa_nodes;
725 } else {
726 nodenr = strtoull(option, NULL, 10);
727 }
728
729 if (get_param_value(option, 128, "mem", optarg) == 0) {
730 node_mem[nodenr] = 0;
731 } else {
732 value = strtoull(option, &endptr, 0);
733 switch (*endptr) {
734 case 0: case 'M': case 'm':
735 value <<= 20;
736 break;
737 case 'G': case 'g':
738 value <<= 30;
739 break;
740 }
741 node_mem[nodenr] = value;
742 }
743 if (get_param_value(option, 128, "cpus", optarg) == 0) {
744 node_cpumask[nodenr] = 0;
745 } else {
746 value = strtoull(option, &endptr, 10);
747 if (value >= 64) {
748 value = 63;
749 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
750 } else {
751 if (*endptr == '-') {
752 endvalue = strtoull(endptr+1, &endptr, 10);
753 if (endvalue >= 63) {
754 endvalue = 62;
755 fprintf(stderr,
756 "only 63 CPUs in NUMA mode supported.\n");
757 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100758 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000759 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100760 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000761 }
762 }
763 node_cpumask[nodenr] = value;
764 }
765 nb_numa_nodes++;
766 }
767 return;
768}
769
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200770static void smp_parse(const char *optarg)
771{
772 int smp, sockets = 0, threads = 0, cores = 0;
773 char *endptr;
774 char option[128];
775
776 smp = strtoul(optarg, &endptr, 10);
777 if (endptr != optarg) {
778 if (*endptr == ',') {
779 endptr++;
780 }
781 }
782 if (get_param_value(option, 128, "sockets", endptr) != 0)
783 sockets = strtoull(option, NULL, 10);
784 if (get_param_value(option, 128, "cores", endptr) != 0)
785 cores = strtoull(option, NULL, 10);
786 if (get_param_value(option, 128, "threads", endptr) != 0)
787 threads = strtoull(option, NULL, 10);
788 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
789 max_cpus = strtoull(option, NULL, 10);
790
791 /* compute missing values, prefer sockets over cores over threads */
792 if (smp == 0 || sockets == 0) {
793 sockets = sockets > 0 ? sockets : 1;
794 cores = cores > 0 ? cores : 1;
795 threads = threads > 0 ? threads : 1;
796 if (smp == 0) {
797 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200798 }
799 } else {
800 if (cores == 0) {
801 threads = threads > 0 ? threads : 1;
802 cores = smp / (sockets * threads);
803 } else {
Amit Shah5cdc9b72010-01-13 16:24:42 +0530804 if (sockets) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200805 threads = smp / (cores * sockets);
806 }
807 }
808 }
809 smp_cpus = smp;
810 smp_cores = cores > 0 ? cores : 1;
811 smp_threads = threads > 0 ? threads : 1;
812 if (max_cpus == 0)
813 max_cpus = smp_cpus;
814}
815
bellard330d0412003-07-26 18:11:40 +0000816/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +0000817/* USB devices */
818
Markus Armbrusterfb080002010-05-28 15:38:44 +0200819static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +0000820{
821 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200822 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +0000823
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200824 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +0000825 return -1;
826
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +0100827 /* drivers with .usbdevice_name entry in USBDeviceInfo */
828 dev = usbdevice_create(devname);
829 if (dev)
830 goto done;
831
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200832 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +0000833 if (strstart(devname, "host:", &p)) {
834 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +0000835 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
836 dev = usb_bt_init(devname[2] ? hci_init(p) :
837 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +0000838 } else {
839 return -1;
840 }
pbrook0d92ed32006-05-21 16:30:15 +0000841 if (!dev)
842 return -1;
843
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200844done:
bellarda594cfb2005-11-06 16:13:29 +0000845 return 0;
846}
847
aliguori1f3870a2008-08-21 19:27:48 +0000848static int usb_device_del(const char *devname)
849{
850 int bus_num, addr;
851 const char *p;
852
aliguori5d0c5752008-09-14 01:07:41 +0000853 if (strstart(devname, "host:", &p))
854 return usb_host_device_close(p);
855
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200856 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +0000857 return -1;
858
859 p = strchr(devname, '.');
860 if (!p)
861 return -1;
862 bus_num = strtoul(devname, NULL, 0);
863 addr = strtoul(p + 1, NULL, 0);
864
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200865 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000866}
867
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200868static int usb_parse(const char *cmdline)
869{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800870 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +0200871 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800872 if (r < 0) {
873 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
874 }
875 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200876}
877
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300878void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000879{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800880 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +0200881 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100882 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800883 }
bellarda594cfb2005-11-06 16:13:29 +0000884}
885
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300886void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000887{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800888 const char *devname = qdict_get_str(qdict, "devname");
889 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100890 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800891 }
bellarda594cfb2005-11-06 16:13:29 +0000892}
893
bellardf7cce892004-12-08 22:21:25 +0000894/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +0000895/* PCMCIA/Cardbus */
896
897static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +0100898 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +0000899 struct pcmcia_socket_entry_s *next;
900} *pcmcia_sockets = 0;
901
Paul Brookbc24a222009-05-10 01:44:56 +0100902void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000903{
904 struct pcmcia_socket_entry_s *entry;
905
906 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
907 entry->socket = socket;
908 entry->next = pcmcia_sockets;
909 pcmcia_sockets = entry;
910}
911
Paul Brookbc24a222009-05-10 01:44:56 +0100912void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000913{
914 struct pcmcia_socket_entry_s *entry, **ptr;
915
916 ptr = &pcmcia_sockets;
917 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
918 if (entry->socket == socket) {
919 *ptr = entry->next;
920 qemu_free(entry);
921 }
922}
923
aliguori376253e2009-03-05 23:01:23 +0000924void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +0000925{
926 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +0000927
balrog201a51f2007-04-30 00:51:09 +0000928 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +0000929 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +0000930
931 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +0000932 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
933 iter->socket->attached ? iter->socket->card_string :
934 "Empty");
balrog201a51f2007-04-30 00:51:09 +0000935}
936
937/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +0000938/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +0000939
bellardc4b1fcc2004-03-14 21:44:30 +0000940typedef struct IOHandlerRecord {
941 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +0100942 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +0000943 IOHandler *fd_read;
944 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +0000945 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +0000946 void *opaque;
947 /* temporary data */
948 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +0100949 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +0000950} IOHandlerRecord;
951
Juan Quintela31d4ee62010-03-11 17:55:37 +0100952static QLIST_HEAD(, IOHandlerRecord) io_handlers =
953 QLIST_HEAD_INITIALIZER(io_handlers);
954
bellardc4b1fcc2004-03-14 21:44:30 +0000955
bellard7c9d8e02005-11-15 22:16:05 +0000956/* XXX: fd_read_poll should be suppressed, but an API change is
957 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +0000958int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +0100959 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +0000960 IOHandler *fd_read,
961 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +0000962 void *opaque)
bellardb4608c02003-06-27 17:34:32 +0000963{
Juan Quintela31d4ee62010-03-11 17:55:37 +0100964 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +0000965
bellard7c9d8e02005-11-15 22:16:05 +0000966 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100967 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000968 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +0000969 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +0000970 break;
971 }
bellard8a7ddc32004-03-31 19:00:16 +0000972 }
bellard7c9d8e02005-11-15 22:16:05 +0000973 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100974 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000975 if (ioh->fd == fd)
976 goto found;
977 }
978 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +0100979 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +0000980 found:
981 ioh->fd = fd;
982 ioh->fd_read_poll = fd_read_poll;
983 ioh->fd_read = fd_read;
984 ioh->fd_write = fd_write;
985 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +0000986 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +0000987 }
bellard7c9d8e02005-11-15 22:16:05 +0000988 return 0;
989}
990
ths5fafdf22007-09-16 21:08:06 +0000991int qemu_set_fd_handler(int fd,
992 IOHandler *fd_read,
993 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +0000994 void *opaque)
995{
996 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +0000997}
998
bellard8a7ddc32004-03-31 19:00:16 +0000999/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001000/* machine registration */
1001
blueswir1bdaf78e2008-10-04 07:24:27 +00001002static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001003QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001004
1005int qemu_register_machine(QEMUMachine *m)
1006{
1007 QEMUMachine **pm;
1008 pm = &first_machine;
1009 while (*pm != NULL)
1010 pm = &(*pm)->next;
1011 m->next = NULL;
1012 *pm = m;
1013 return 0;
1014}
1015
pbrook9596ebb2007-11-18 01:44:38 +00001016static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001017{
1018 QEMUMachine *m;
1019
1020 for(m = first_machine; m != NULL; m = m->next) {
1021 if (!strcmp(m->name, name))
1022 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001023 if (m->alias && !strcmp(m->alias, name))
1024 return m;
bellardcc1daa42005-06-05 14:49:17 +00001025 }
1026 return NULL;
1027}
1028
Anthony Liguori0c257432009-05-21 20:41:01 -05001029static QEMUMachine *find_default_machine(void)
1030{
1031 QEMUMachine *m;
1032
1033 for(m = first_machine; m != NULL; m = m->next) {
1034 if (m->is_default) {
1035 return m;
1036 }
1037 }
1038 return NULL;
1039}
1040
bellardcc1daa42005-06-05 14:49:17 +00001041/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001042/* main execution loop */
1043
pbrook9596ebb2007-11-18 01:44:38 +00001044static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001045{
aliguori7d957bd2009-01-15 22:14:11 +00001046 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001047 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001048 DisplayChangeListener *dcl = ds->listeners;
1049
Sheng Yang62a27442010-01-26 19:21:16 +08001050 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001051 dpy_refresh(ds);
1052
1053 while (dcl != NULL) {
1054 if (dcl->gui_timer_interval &&
1055 dcl->gui_timer_interval < interval)
1056 interval = dcl->gui_timer_interval;
1057 dcl = dcl->next;
1058 }
1059 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001060}
1061
blueswir19043b622009-01-21 19:28:13 +00001062static void nographic_update(void *opaque)
1063{
1064 uint64_t interval = GUI_REFRESH_INTERVAL;
1065
Sheng Yang62a27442010-01-26 19:21:16 +08001066 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00001067 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1068}
1069
bellard0bd48852005-11-11 00:00:47 +00001070struct vm_change_state_entry {
1071 VMChangeStateHandler *cb;
1072 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001073 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001074};
1075
Blue Swirl72cf2d42009-09-12 07:36:22 +00001076static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001077
1078VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1079 void *opaque)
1080{
1081 VMChangeStateEntry *e;
1082
1083 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001084
1085 e->cb = cb;
1086 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001087 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001088 return e;
1089}
1090
1091void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1092{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001093 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001094 qemu_free (e);
1095}
1096
Blue Swirl296af7c2010-03-29 19:23:50 +00001097void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001098{
1099 VMChangeStateEntry *e;
1100
1101 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001102 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001103 }
1104}
1105
bellard8a7ddc32004-03-31 19:00:16 +00001106void vm_start(void)
1107{
1108 if (!vm_running) {
1109 cpu_enable_ticks();
1110 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001111 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001112 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001113 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001114 }
1115}
1116
bellardbb0c6722004-06-20 12:37:32 +00001117/* reset/shutdown handler */
1118
1119typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001120 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001121 QEMUResetHandler *func;
1122 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001123} QEMUResetEntry;
1124
Blue Swirl72cf2d42009-09-12 07:36:22 +00001125static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1126 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001127static int reset_requested;
1128static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00001129static int powerdown_requested;
Blue Swirl296af7c2010-03-29 19:23:50 +00001130int debug_requested;
Blue Swirl7277e022010-04-12 17:19:06 +00001131int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001132
aurel32cf7a2fe2008-03-18 06:53:05 +00001133int qemu_shutdown_requested(void)
1134{
1135 int r = shutdown_requested;
1136 shutdown_requested = 0;
1137 return r;
1138}
1139
1140int qemu_reset_requested(void)
1141{
1142 int r = reset_requested;
1143 reset_requested = 0;
1144 return r;
1145}
1146
1147int qemu_powerdown_requested(void)
1148{
1149 int r = powerdown_requested;
1150 powerdown_requested = 0;
1151 return r;
1152}
1153
aliguorie5689022009-04-24 18:03:54 +00001154static int qemu_debug_requested(void)
1155{
1156 int r = debug_requested;
1157 debug_requested = 0;
1158 return r;
1159}
1160
aliguori6e29f5d2009-04-24 18:04:02 +00001161static int qemu_vmstop_requested(void)
1162{
1163 int r = vmstop_requested;
1164 vmstop_requested = 0;
1165 return r;
1166}
1167
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001168void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001169{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001170 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001171
bellardbb0c6722004-06-20 12:37:32 +00001172 re->func = func;
1173 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001174 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001175}
1176
Jan Kiszkadda9b292009-07-02 00:19:02 +02001177void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001178{
1179 QEMUResetEntry *re;
1180
Blue Swirl72cf2d42009-09-12 07:36:22 +00001181 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001182 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001183 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001184 qemu_free(re);
1185 return;
1186 }
1187 }
1188}
1189
1190void qemu_system_reset(void)
1191{
1192 QEMUResetEntry *re, *nre;
1193
1194 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001195 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001196 re->func(re->opaque);
1197 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001198 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001199 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001200}
1201
1202void qemu_system_reset_request(void)
1203{
bellardd1beab82006-10-02 19:44:22 +00001204 if (no_reboot) {
1205 shutdown_requested = 1;
1206 } else {
1207 reset_requested = 1;
1208 }
aliguorid9f75a42009-04-24 18:03:11 +00001209 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001210}
1211
1212void qemu_system_shutdown_request(void)
1213{
1214 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001215 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001216}
1217
bellard34751872005-07-02 14:31:34 +00001218void qemu_system_powerdown_request(void)
1219{
1220 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001221 qemu_notify_event();
1222}
1223
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001224void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001225{
1226 IOHandlerRecord *ioh;
1227 fd_set rfds, wfds, xfds;
1228 int ret, nfds;
1229 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001230 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001231
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001232 if (nonblocking)
1233 timeout = 0;
1234 else {
1235 timeout = qemu_calculate_timeout();
1236 qemu_bh_update_timeout(&timeout);
1237 }
aliguori56f3a5d2008-10-31 18:07:17 +00001238
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001239 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001240
bellardfd1dff42006-02-01 21:29:26 +00001241 /* poll any events */
1242 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001243 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001244 FD_ZERO(&rfds);
1245 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001246 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001247 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00001248 if (ioh->deleted)
1249 continue;
bellardfd1dff42006-02-01 21:29:26 +00001250 if (ioh->fd_read &&
1251 (!ioh->fd_read_poll ||
1252 ioh->fd_read_poll(ioh->opaque) != 0)) {
1253 FD_SET(ioh->fd, &rfds);
1254 if (ioh->fd > nfds)
1255 nfds = ioh->fd;
1256 }
1257 if (ioh->fd_write) {
1258 FD_SET(ioh->fd, &wfds);
1259 if (ioh->fd > nfds)
1260 nfds = ioh->fd;
1261 }
1262 }
ths3b46e622007-09-17 08:09:54 +00001263
aliguori56f3a5d2008-10-31 18:07:17 +00001264 tv.tv_sec = timeout / 1000;
1265 tv.tv_usec = (timeout % 1000) * 1000;
1266
Jan Kiszkad918f232009-06-24 14:42:30 +02001267 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1268
aliguori48708522009-04-24 18:03:49 +00001269 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001270 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001271 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00001272 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001273 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00001274
Juan Quintela31d4ee62010-03-11 17:55:37 +01001275 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
thscafffd42007-02-28 21:59:44 +00001276 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001277 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00001278 qemu_free(ioh);
Juan Quintela4bed9832010-03-11 17:55:41 +01001279 continue;
1280 }
1281 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1282 ioh->fd_read(ioh->opaque);
1283 }
1284 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1285 ioh->fd_write(ioh->opaque);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001286 }
thscafffd42007-02-28 21:59:44 +00001287 }
bellardfd1dff42006-02-01 21:29:26 +00001288 }
Jan Kiszkad918f232009-06-24 14:42:30 +02001289
1290 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001291
Paolo Bonzinib6964822010-03-10 11:38:45 +01001292 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001293
pbrook423f0742007-05-23 00:06:54 +00001294 /* Check bottom-halves last in case any of the earlier events triggered
1295 them. */
1296 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001297
bellard5905b2e2004-08-01 21:53:26 +00001298}
1299
aliguori43b96852009-04-24 18:03:33 +00001300static int vm_can_run(void)
1301{
1302 if (powerdown_requested)
1303 return 0;
1304 if (reset_requested)
1305 return 0;
1306 if (shutdown_requested)
1307 return 0;
aliguorie5689022009-04-24 18:03:54 +00001308 if (debug_requested)
1309 return 0;
aliguori43b96852009-04-24 18:03:33 +00001310 return 1;
1311}
1312
Blue Swirld9c32312009-08-09 08:42:19 +00001313qemu_irq qemu_system_powerdown;
1314
aliguori43b96852009-04-24 18:03:33 +00001315static void main_loop(void)
1316{
aliguori6e29f5d2009-04-24 18:04:02 +00001317 int r;
aliguori43b96852009-04-24 18:03:33 +00001318
Blue Swirl7277e022010-04-12 17:19:06 +00001319 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001320
aliguori6e29f5d2009-04-24 18:04:02 +00001321 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00001322 do {
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001323 bool nonblocking = false;
aliguorie6e35b12009-04-24 18:03:57 +00001324#ifdef CONFIG_PROFILER
1325 int64_t ti;
1326#endif
aliguorid6dc3d42009-04-24 18:04:07 +00001327#ifndef CONFIG_IOTHREAD
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001328 nonblocking = tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00001329#endif
aliguori43b96852009-04-24 18:03:33 +00001330#ifdef CONFIG_PROFILER
1331 ti = profile_getclock();
1332#endif
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001333 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001334#ifdef CONFIG_PROFILER
1335 dev_time += profile_getclock() - ti;
1336#endif
aliguorie5689022009-04-24 18:03:54 +00001337 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00001338
Blue Swirl54fc6ea2010-03-29 19:23:46 +00001339 if ((r = qemu_debug_requested())) {
1340 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001341 }
aliguori43b96852009-04-24 18:03:33 +00001342 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001343 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001344 if (no_shutdown) {
1345 vm_stop(0);
1346 no_shutdown = 0;
1347 } else
1348 break;
1349 }
aliguorid6dc3d42009-04-24 18:04:07 +00001350 if (qemu_reset_requested()) {
1351 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00001352 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00001353 resume_all_vcpus();
1354 }
Blue Swirld9c32312009-08-09 08:42:19 +00001355 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001356 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001357 qemu_irq_raise(qemu_system_powerdown);
1358 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001359 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001360 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001361 }
aliguori43b96852009-04-24 18:03:33 +00001362 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001363 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001364 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001365}
1366
pbrook9bd7e6d2009-04-07 22:58:45 +00001367static void version(void)
1368{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001369 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001370}
1371
ths15f82202007-06-29 23:26:08 +00001372static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001373{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001374 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001375#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1376 opt_help
blueswir15824d652009-03-28 06:44:27 +00001377#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001378#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001379#undef DEF
1380#undef DEFHEADING
1381#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001382 ;
1383 version();
1384 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001385 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001386 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001387 "\n"
1388 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001389 "During emulation, the following keys are useful:\n"
1390 "ctrl-alt-f toggle full screen\n"
1391 "ctrl-alt-n switch to virtual console 'n'\n"
1392 "ctrl-alt toggle mouse and keyboard grab\n"
1393 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001394 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1395 "qemu",
1396 options_help);
ths15f82202007-06-29 23:26:08 +00001397 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001398}
1399
bellardcd6f1162004-05-13 22:02:20 +00001400#define HAS_ARG 0x0001
1401
bellardcd6f1162004-05-13 22:02:20 +00001402typedef struct QEMUOption {
1403 const char *name;
1404 int flags;
1405 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001406 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001407} QEMUOption;
1408
blueswir1dbed7e42008-10-01 19:38:09 +00001409static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001410 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1411#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1412 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001413#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001414#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001415#undef DEF
1416#undef DEFHEADING
1417#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001418 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001419};
malc3893c122008-09-28 00:42:05 +00001420static void select_vgahw (const char *p)
1421{
1422 const char *opts;
1423
Gerd Hoffmann64465292009-12-08 13:11:45 +01001424 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001425 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001426 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001427 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001428 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001429 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001430 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001431 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001432 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001433 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00001434 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001435 invalid_vga:
1436 fprintf(stderr, "Unknown vga type: %s\n", p);
1437 exit(1);
1438 }
malccb5a7aa2008-09-28 00:42:12 +00001439 while (*opts) {
1440 const char *nextopt;
1441
1442 if (strstart(opts, ",retrace=", &nextopt)) {
1443 opts = nextopt;
1444 if (strstart(opts, "dumb", &nextopt))
1445 vga_retrace_method = VGA_RETRACE_DUMB;
1446 else if (strstart(opts, "precise", &nextopt))
1447 vga_retrace_method = VGA_RETRACE_PRECISE;
1448 else goto invalid_vga;
1449 } else goto invalid_vga;
1450 opts = nextopt;
1451 }
malc3893c122008-09-28 00:42:05 +00001452}
1453
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001454static int balloon_parse(const char *arg)
1455{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001456 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001457
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001458 if (strcmp(arg, "none") == 0) {
1459 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001460 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001461
1462 if (!strncmp(arg, "virtio", 6)) {
1463 if (arg[6] == ',') {
1464 /* have params -> parse them */
Markus Armbruster8212c642010-02-10 19:52:18 +01001465 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001466 if (!opts)
1467 return -1;
1468 } else {
1469 /* create empty opts */
1470 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1471 }
1472 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1473 return 0;
1474 }
1475
1476 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001477}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001478
Paul Brook5cea8592009-05-30 00:52:44 +01001479char *qemu_find_file(int type, const char *name)
1480{
1481 int len;
1482 const char *subdir;
1483 char *buf;
1484
1485 /* If name contains path separators then try it as a straight path. */
1486 if ((strchr(name, '/') || strchr(name, '\\'))
1487 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02001488 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001489 }
1490 switch (type) {
1491 case QEMU_FILE_TYPE_BIOS:
1492 subdir = "";
1493 break;
1494 case QEMU_FILE_TYPE_KEYMAP:
1495 subdir = "keymaps/";
1496 break;
1497 default:
1498 abort();
1499 }
1500 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1501 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001502 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001503 if (access(buf, R_OK)) {
1504 qemu_free(buf);
1505 return NULL;
1506 }
1507 return buf;
1508}
1509
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001510static int device_help_func(QemuOpts *opts, void *opaque)
1511{
1512 return qdev_device_help(opts);
1513}
1514
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001515static int device_init_func(QemuOpts *opts, void *opaque)
1516{
1517 DeviceState *dev;
1518
1519 dev = qdev_device_add(opts);
1520 if (!dev)
1521 return -1;
1522 return 0;
1523}
1524
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001525static int chardev_init_func(QemuOpts *opts, void *opaque)
1526{
1527 CharDriverState *chr;
1528
1529 chr = qemu_chr_open_opts(opts, NULL);
1530 if (!chr)
1531 return -1;
1532 return 0;
1533}
1534
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301535#ifdef CONFIG_LINUX
1536static int fsdev_init_func(QemuOpts *opts, void *opaque)
1537{
1538 int ret;
1539 ret = qemu_fsdev_add(opts);
1540
1541 return ret;
1542}
1543#endif
1544
Gerd Hoffmann88589342009-12-08 13:11:50 +01001545static int mon_init_func(QemuOpts *opts, void *opaque)
1546{
1547 CharDriverState *chr;
1548 const char *chardev;
1549 const char *mode;
1550 int flags;
1551
1552 mode = qemu_opt_get(opts, "mode");
1553 if (mode == NULL) {
1554 mode = "readline";
1555 }
1556 if (strcmp(mode, "readline") == 0) {
1557 flags = MONITOR_USE_READLINE;
1558 } else if (strcmp(mode, "control") == 0) {
1559 flags = MONITOR_USE_CONTROL;
1560 } else {
1561 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1562 exit(1);
1563 }
1564
1565 if (qemu_opt_get_bool(opts, "default", 0))
1566 flags |= MONITOR_IS_DEFAULT;
1567
1568 chardev = qemu_opt_get(opts, "chardev");
1569 chr = qemu_chr_find(chardev);
1570 if (chr == NULL) {
1571 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1572 exit(1);
1573 }
1574
1575 monitor_init(chr, flags);
1576 return 0;
1577}
1578
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001579static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001580{
1581 static int monitor_device_index = 0;
1582 QemuOpts *opts;
1583 const char *p;
1584 char label[32];
1585 int def = 0;
1586
1587 if (strstart(optarg, "chardev:", &p)) {
1588 snprintf(label, sizeof(label), "%s", p);
1589 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001590 snprintf(label, sizeof(label), "compat_monitor%d",
1591 monitor_device_index);
1592 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001593 def = 1;
1594 }
1595 opts = qemu_chr_parse_compat(label, optarg);
1596 if (!opts) {
1597 fprintf(stderr, "parse error: %s\n", optarg);
1598 exit(1);
1599 }
1600 }
1601
1602 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
1603 if (!opts) {
1604 fprintf(stderr, "duplicate chardev: %s\n", label);
1605 exit(1);
1606 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001607 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001608 qemu_opt_set(opts, "chardev", label);
1609 if (def)
1610 qemu_opt_set(opts, "default", "on");
1611 monitor_device_index++;
1612}
1613
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001614struct device_config {
1615 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001616 DEV_USB, /* -usbdevice */
1617 DEV_BT, /* -bt */
1618 DEV_SERIAL, /* -serial */
1619 DEV_PARALLEL, /* -parallel */
1620 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001621 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001622 } type;
1623 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001624 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001625};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001626QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001627
1628static void add_device_config(int type, const char *cmdline)
1629{
1630 struct device_config *conf;
1631
1632 conf = qemu_mallocz(sizeof(*conf));
1633 conf->type = type;
1634 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001635 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001636}
1637
1638static int foreach_device_config(int type, int (*func)(const char *cmdline))
1639{
1640 struct device_config *conf;
1641 int rc;
1642
Blue Swirl72cf2d42009-09-12 07:36:22 +00001643 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001644 if (conf->type != type)
1645 continue;
1646 rc = func(conf->cmdline);
1647 if (0 != rc)
1648 return rc;
1649 }
1650 return 0;
1651}
1652
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001653static int serial_parse(const char *devname)
1654{
1655 static int index = 0;
1656 char label[32];
1657
1658 if (strcmp(devname, "none") == 0)
1659 return 0;
1660 if (index == MAX_SERIAL_PORTS) {
1661 fprintf(stderr, "qemu: too many serial ports\n");
1662 exit(1);
1663 }
1664 snprintf(label, sizeof(label), "serial%d", index);
1665 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1666 if (!serial_hds[index]) {
1667 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1668 devname, strerror(errno));
1669 return -1;
1670 }
1671 index++;
1672 return 0;
1673}
1674
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01001675static int parallel_parse(const char *devname)
1676{
1677 static int index = 0;
1678 char label[32];
1679
1680 if (strcmp(devname, "none") == 0)
1681 return 0;
1682 if (index == MAX_PARALLEL_PORTS) {
1683 fprintf(stderr, "qemu: too many parallel ports\n");
1684 exit(1);
1685 }
1686 snprintf(label, sizeof(label), "parallel%d", index);
1687 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1688 if (!parallel_hds[index]) {
1689 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1690 devname, strerror(errno));
1691 return -1;
1692 }
1693 index++;
1694 return 0;
1695}
1696
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001697static int virtcon_parse(const char *devname)
1698{
1699 static int index = 0;
1700 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05301701 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001702
1703 if (strcmp(devname, "none") == 0)
1704 return 0;
1705 if (index == MAX_VIRTIO_CONSOLES) {
1706 fprintf(stderr, "qemu: too many virtio consoles\n");
1707 exit(1);
1708 }
Amit Shah392ecf52010-01-21 16:19:23 +05301709
1710 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1711 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1712
1713 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1714 qemu_opt_set(dev_opts, "driver", "virtconsole");
1715
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001716 snprintf(label, sizeof(label), "virtcon%d", index);
1717 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1718 if (!virtcon_hds[index]) {
1719 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1720 devname, strerror(errno));
1721 return -1;
1722 }
Amit Shah392ecf52010-01-21 16:19:23 +05301723 qemu_opt_set(dev_opts, "chardev", label);
1724
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001725 index++;
1726 return 0;
1727}
1728
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001729static int debugcon_parse(const char *devname)
1730{
1731 QemuOpts *opts;
1732
1733 if (!qemu_chr_open("debugcon", devname, NULL)) {
1734 exit(1);
1735 }
1736 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
1737 if (!opts) {
1738 fprintf(stderr, "qemu: already have a debugcon device\n");
1739 exit(1);
1740 }
1741 qemu_opt_set(opts, "driver", "isa-debugcon");
1742 qemu_opt_set(opts, "chardev", "debugcon");
1743 return 0;
1744}
1745
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001746void qemu_add_exit_notifier(Notifier *notify)
1747{
1748 notifier_list_add(&exit_notifiers, notify);
1749}
1750
1751void qemu_remove_exit_notifier(Notifier *notify)
1752{
1753 notifier_list_remove(&exit_notifiers, notify);
1754}
1755
1756static void qemu_run_exit_notifiers(void)
1757{
1758 notifier_list_notify(&exit_notifiers);
1759}
1760
Anthony Liguori6530a972010-01-22 09:18:06 -06001761static const QEMUOption *lookup_opt(int argc, char **argv,
1762 const char **poptarg, int *poptind)
1763{
1764 const QEMUOption *popt;
1765 int optind = *poptind;
1766 char *r = argv[optind];
1767 const char *optarg;
1768
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001769 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06001770 optind++;
1771 /* Treat --foo the same as -foo. */
1772 if (r[1] == '-')
1773 r++;
1774 popt = qemu_options;
1775 for(;;) {
1776 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001777 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06001778 exit(1);
1779 }
1780 if (!strcmp(popt->name, r + 1))
1781 break;
1782 popt++;
1783 }
1784 if (popt->flags & HAS_ARG) {
1785 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001786 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06001787 exit(1);
1788 }
1789 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001790 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06001791 } else {
1792 optarg = NULL;
1793 }
1794
1795 *poptarg = optarg;
1796 *poptind = optind;
1797
1798 return popt;
1799}
1800
malc902b3d52008-12-10 19:18:40 +00001801int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00001802{
aliguori59030a82009-04-05 18:43:41 +00001803 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00001804 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03001805 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01001806 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00001807 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00001808 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05001809 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00001810 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00001811 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00001812 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001813 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00001814 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06001815 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00001816 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001817 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00001818 const char *cpu_model;
bellard26a5f132008-05-28 12:30:31 +00001819 int tb_size;
ths93815bc2007-03-19 15:58:31 +00001820 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00001821 const char *incoming = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05001822 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06001823 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00001824
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001825 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01001826 error_set_progname(argv[0]);
1827
Jan Kiszka68752042009-09-15 13:36:04 +02001828 init_clocks();
1829
malc902b3d52008-12-10 19:18:40 +00001830 qemu_cache_utils_init(envp);
1831
Blue Swirl72cf2d42009-09-12 07:36:22 +00001832 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02001833 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00001834
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05001835 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05001836 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00001837 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00001838 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00001839 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00001840 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00001841 kernel_filename = NULL;
1842 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00001843 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00001844 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00001845
aliguori268a3622009-04-21 22:30:27 +00001846 for (i = 0; i < MAX_NODES; i++) {
1847 node_mem[i] = 0;
1848 node_cpumask[i] = 0;
1849 }
1850
aliguori268a3622009-04-21 22:30:27 +00001851 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00001852 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00001853
bellard26a5f132008-05-28 12:30:31 +00001854 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00001855 autostart= 1;
1856
Anthony Liguori292444c2010-01-21 10:57:58 -06001857 /* first pass of option parsing */
1858 optind = 1;
1859 while (optind < argc) {
1860 if (argv[optind][0] != '-') {
1861 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06001862 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06001863 continue;
1864 } else {
1865 const QEMUOption *popt;
1866
1867 popt = lookup_opt(argc, argv, &optarg, &optind);
1868 switch (popt->index) {
1869 case QEMU_OPTION_nodefconfig:
1870 defconfig=0;
1871 break;
1872 }
1873 }
1874 }
1875
1876 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001877 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01001878
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001879 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02001880 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001881 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001882 }
1883
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001884 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02001885 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001886 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001887 }
1888 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00001889 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06001890
1891 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00001892 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00001893 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00001894 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00001895 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06001896 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001897 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00001898 } else {
1899 const QEMUOption *popt;
1900
Anthony Liguori6530a972010-01-22 09:18:06 -06001901 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00001902 if (!(popt->arch_mask & arch_type)) {
1903 printf("Option %s not supported for this target\n", popt->name);
1904 exit(1);
1905 }
bellardcd6f1162004-05-13 22:02:20 +00001906 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00001907 case QEMU_OPTION_M:
1908 machine = find_machine(optarg);
1909 if (!machine) {
1910 QEMUMachine *m;
1911 printf("Supported machines are:\n");
1912 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001913 if (m->alias)
1914 printf("%-10s %s (alias of %s)\n",
1915 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00001916 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00001917 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05001918 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00001919 }
ths15f82202007-06-29 23:26:08 +00001920 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00001921 }
1922 break;
j_mayer94fc95c2007-03-05 19:44:02 +00001923 case QEMU_OPTION_cpu:
1924 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00001925 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00001926 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00001927 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00001928 } else {
1929 cpu_model = optarg;
1930 }
1931 break;
bellardcd6f1162004-05-13 22:02:20 +00001932 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00001933 initrd_filename = optarg;
1934 break;
bellardcd6f1162004-05-13 22:02:20 +00001935 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00001936 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001937 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00001938 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001939 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00001940 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00001941 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00001942 translation == BIOS_ATA_TRANSLATION_LBA ?
1943 ",trans=lba" :
1944 translation == BIOS_ATA_TRANSLATION_NONE ?
1945 ",trans=none" : "");
1946 break;
bellardcd6f1162004-05-13 22:02:20 +00001947 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00001948 case QEMU_OPTION_hdc:
1949 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00001950 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00001951 break;
thse4bcb142007-12-02 04:51:10 +00001952 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00001953 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00001954 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02001955 case QEMU_OPTION_set:
1956 if (qemu_set_option(optarg) != 0)
1957 exit(1);
1958 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01001959 case QEMU_OPTION_global:
1960 if (qemu_global_option(optarg) != 0)
1961 exit(1);
1962 break;
balrog3e3d5812007-04-30 02:09:25 +00001963 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00001964 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00001965 break;
pbrooka1bb27b2007-04-06 16:49:48 +00001966 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00001967 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00001968 break;
j_mayer86f55662007-04-24 06:52:59 +00001969 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00001970 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00001971 break;
bellardcd6f1162004-05-13 22:02:20 +00001972 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00001973 snapshot = 1;
1974 break;
bellardcd6f1162004-05-13 22:02:20 +00001975 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00001976 {
bellard330d0412003-07-26 18:11:40 +00001977 const char *p;
1978 p = optarg;
1979 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001980 if (cyls < 1 || cyls > 16383)
1981 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00001982 if (*p != ',')
1983 goto chs_fail;
1984 p++;
1985 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001986 if (heads < 1 || heads > 16)
1987 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00001988 if (*p != ',')
1989 goto chs_fail;
1990 p++;
1991 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001992 if (secs < 1 || secs > 63)
1993 goto chs_fail;
1994 if (*p == ',') {
1995 p++;
1996 if (!strcmp(p, "none"))
1997 translation = BIOS_ATA_TRANSLATION_NONE;
1998 else if (!strcmp(p, "lba"))
1999 translation = BIOS_ATA_TRANSLATION_LBA;
2000 else if (!strcmp(p, "auto"))
2001 translation = BIOS_ATA_TRANSLATION_AUTO;
2002 else
2003 goto chs_fail;
2004 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002005 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002006 fprintf(stderr, "qemu: invalid physical CHS format\n");
2007 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002008 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002009 if (hda_opts != NULL) {
2010 char num[16];
2011 snprintf(num, sizeof(num), "%d", cyls);
2012 qemu_opt_set(hda_opts, "cyls", num);
2013 snprintf(num, sizeof(num), "%d", heads);
2014 qemu_opt_set(hda_opts, "heads", num);
2015 snprintf(num, sizeof(num), "%d", secs);
2016 qemu_opt_set(hda_opts, "secs", num);
2017 if (translation == BIOS_ATA_TRANSLATION_LBA)
2018 qemu_opt_set(hda_opts, "trans", "lba");
2019 if (translation == BIOS_ATA_TRANSLATION_NONE)
2020 qemu_opt_set(hda_opts, "trans", "none");
2021 }
bellard330d0412003-07-26 18:11:40 +00002022 }
2023 break;
aliguori268a3622009-04-21 22:30:27 +00002024 case QEMU_OPTION_numa:
2025 if (nb_numa_nodes >= MAX_NODES) {
2026 fprintf(stderr, "qemu: too many NUMA nodes\n");
2027 exit(1);
2028 }
2029 numa_add(optarg);
2030 break;
bellardcd6f1162004-05-13 22:02:20 +00002031 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002032 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002033 break;
balrog4d3b6f62008-02-10 16:33:14 +00002034#ifdef CONFIG_CURSES
2035 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002036 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00002037 break;
2038#endif
balroga171fe32007-04-30 01:48:07 +00002039 case QEMU_OPTION_portrait:
2040 graphic_rotate = 1;
2041 break;
bellardcd6f1162004-05-13 22:02:20 +00002042 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002043 kernel_filename = optarg;
2044 break;
bellardcd6f1162004-05-13 22:02:20 +00002045 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002046 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002047 break;
bellardcd6f1162004-05-13 22:02:20 +00002048 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00002049 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00002050 break;
bellardcd6f1162004-05-13 22:02:20 +00002051 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002052 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002053 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002054 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002055 };
2056 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002057 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002058 int legacy = 0;
2059
2060 if (!strchr(optarg, '=')) {
2061 legacy = 1;
2062 pstrcpy(buf, sizeof(buf), optarg);
2063 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2064 fprintf(stderr,
2065 "qemu: unknown boot parameter '%s' in '%s'\n",
2066 buf, optarg);
2067 exit(1);
2068 }
2069
2070 if (legacy ||
2071 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002072 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002073 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002074 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002075 if (!legacy) {
2076 if (get_param_value(buf, sizeof(buf),
2077 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002078 validate_bootdevices(buf);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002079 standard_boot_devices = qemu_strdup(boot_devices);
2080 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2081 qemu_register_reset(restore_boot_devices,
2082 standard_boot_devices);
2083 }
Jan Kiszka95387492009-07-02 00:19:02 +02002084 if (get_param_value(buf, sizeof(buf),
2085 "menu", optarg)) {
2086 if (!strcmp(buf, "on")) {
2087 boot_menu = 1;
2088 } else if (!strcmp(buf, "off")) {
2089 boot_menu = 0;
2090 } else {
2091 fprintf(stderr,
2092 "qemu: invalid option value '%s'\n",
2093 buf);
2094 exit(1);
2095 }
2096 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002097 }
bellard36b486b2003-11-11 13:36:08 +00002098 }
2099 break;
bellardcd6f1162004-05-13 22:02:20 +00002100 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002101 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00002102 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00002103 break;
bellard52ca8d62006-06-14 16:03:05 +00002104 case QEMU_OPTION_no_fd_bootchk:
2105 fd_bootchk = 0;
2106 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002107 case QEMU_OPTION_netdev:
2108 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2109 exit(1);
2110 }
2111 break;
bellard7c9d8e02005-11-15 22:16:05 +00002112 case QEMU_OPTION_net:
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01002113 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002114 exit(1);
2115 }
bellard702c6512004-04-02 21:21:32 +00002116 break;
bellardc7f74642004-08-24 21:57:12 +00002117#ifdef CONFIG_SLIRP
2118 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002119 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002120 break;
ths47d5d012007-02-20 00:05:08 +00002121 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002122 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002123 break;
bellard9bf05442004-08-25 22:12:49 +00002124 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002125 if (net_slirp_redir(optarg) < 0)
2126 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002127 break;
bellardc7f74642004-08-24 21:57:12 +00002128#endif
balrogdc72ac12008-11-09 00:04:26 +00002129 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002130 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002131 break;
bellard1d14ffa2005-10-30 18:58:22 +00002132 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002133 if (!(audio_available())) {
2134 printf("Option %s not supported for this target\n", popt->name);
2135 exit(1);
2136 }
bellard1d14ffa2005-10-30 18:58:22 +00002137 AUD_help ();
2138 exit (0);
2139 break;
2140 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002141 if (!(audio_available())) {
2142 printf("Option %s not supported for this target\n", popt->name);
2143 exit(1);
2144 }
bellard1d14ffa2005-10-30 18:58:22 +00002145 select_soundhw (optarg);
2146 break;
bellardcd6f1162004-05-13 22:02:20 +00002147 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002148 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002149 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002150 case QEMU_OPTION_version:
2151 version();
2152 exit(0);
2153 break;
aurel3200f82b82008-04-27 21:12:55 +00002154 case QEMU_OPTION_m: {
2155 uint64_t value;
2156 char *ptr;
2157
2158 value = strtoul(optarg, &ptr, 10);
2159 switch (*ptr) {
2160 case 0: case 'M': case 'm':
2161 value <<= 20;
2162 break;
2163 case 'G': case 'g':
2164 value <<= 30;
2165 break;
2166 default:
2167 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002168 exit(1);
2169 }
aurel3200f82b82008-04-27 21:12:55 +00002170
2171 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002172 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00002173 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2174 exit(1);
2175 }
Anthony Liguoric227f092009-10-01 16:12:16 -05002176 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002177 fprintf(stderr, "qemu: ram size too large\n");
2178 exit(1);
2179 }
2180 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002181 break;
aurel3200f82b82008-04-27 21:12:55 +00002182 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002183 case QEMU_OPTION_mempath:
2184 mem_path = optarg;
2185 break;
2186#ifdef MAP_POPULATE
2187 case QEMU_OPTION_mem_prealloc:
2188 mem_prealloc = 1;
2189 break;
2190#endif
bellardcd6f1162004-05-13 22:02:20 +00002191 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002192 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00002193 break;
bellardcd6f1162004-05-13 22:02:20 +00002194 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002195 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002196 break;
aliguori59030a82009-04-05 18:43:41 +00002197 case QEMU_OPTION_gdb:
2198 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002199 break;
bellardcd6f1162004-05-13 22:02:20 +00002200 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002201 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002202 break;
j_mayer1192dad2007-10-05 13:08:35 +00002203 case QEMU_OPTION_bios:
2204 bios_name = optarg;
2205 break;
aurel321b530a62009-04-05 20:08:59 +00002206 case QEMU_OPTION_singlestep:
2207 singlestep = 1;
2208 break;
bellardcd6f1162004-05-13 22:02:20 +00002209 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002210 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002211 break;
bellard3d11d0e2004-12-12 16:56:30 +00002212 case QEMU_OPTION_k:
2213 keyboard_layout = optarg;
2214 break;
bellardee22c2f2004-06-03 12:49:50 +00002215 case QEMU_OPTION_localtime:
2216 rtc_utc = 0;
2217 break;
malc3893c122008-09-28 00:42:05 +00002218 case QEMU_OPTION_vga:
2219 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002220 break;
bellarde9b137c2004-06-21 16:46:10 +00002221 case QEMU_OPTION_g:
2222 {
2223 const char *p;
2224 int w, h, depth;
2225 p = optarg;
2226 w = strtol(p, (char **)&p, 10);
2227 if (w <= 0) {
2228 graphic_error:
2229 fprintf(stderr, "qemu: invalid resolution or depth\n");
2230 exit(1);
2231 }
2232 if (*p != 'x')
2233 goto graphic_error;
2234 p++;
2235 h = strtol(p, (char **)&p, 10);
2236 if (h <= 0)
2237 goto graphic_error;
2238 if (*p == 'x') {
2239 p++;
2240 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002241 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002242 depth != 24 && depth != 32)
2243 goto graphic_error;
2244 } else if (*p == '\0') {
2245 depth = graphic_depth;
2246 } else {
2247 goto graphic_error;
2248 }
ths3b46e622007-09-17 08:09:54 +00002249
bellarde9b137c2004-06-21 16:46:10 +00002250 graphic_width = w;
2251 graphic_height = h;
2252 graphic_depth = depth;
2253 }
2254 break;
ths20d8a3e2007-02-18 17:04:49 +00002255 case QEMU_OPTION_echr:
2256 {
2257 char *r;
2258 term_escape_char = strtol(optarg, &r, 0);
2259 if (r == optarg)
2260 printf("Bad argument to echr\n");
2261 break;
2262 }
bellard82c643f2004-07-14 17:28:13 +00002263 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002264 monitor_parse(optarg, "readline");
2265 default_monitor = 0;
2266 break;
2267 case QEMU_OPTION_qmp:
2268 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002269 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002270 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002271 case QEMU_OPTION_mon:
Markus Armbruster8212c642010-02-10 19:52:18 +01002272 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002273 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002274 exit(1);
2275 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002276 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002277 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002278 case QEMU_OPTION_chardev:
Markus Armbruster8212c642010-02-10 19:52:18 +01002279 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002280 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002281 exit(1);
2282 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002283 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302284#ifdef CONFIG_LINUX
2285 case QEMU_OPTION_fsdev:
2286 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2287 if (!opts) {
2288 fprintf(stderr, "parse error: %s\n", optarg);
2289 exit(1);
2290 }
2291 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302292 case QEMU_OPTION_virtfs: {
2293 char *arg_fsdev = NULL;
2294 char *arg_9p = NULL;
2295 int len = 0;
2296
2297 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2298 if (!opts) {
2299 fprintf(stderr, "parse error: %s\n", optarg);
2300 exit(1);
2301 }
2302
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002303 if (qemu_opt_get(opts, "fstype") == NULL ||
2304 qemu_opt_get(opts, "mount_tag") == NULL ||
2305 qemu_opt_get(opts, "path") == NULL ||
2306 qemu_opt_get(opts, "security_model") == NULL) {
2307 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2308 "security_model=[mapped|passthrough],"
2309 "mnt_tag=tag.\n");
2310 exit(1);
2311 }
2312
2313 len = strlen(",id=,path=,security_model=");
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302314 len += strlen(qemu_opt_get(opts, "fstype"));
2315 len += strlen(qemu_opt_get(opts, "mount_tag"));
2316 len += strlen(qemu_opt_get(opts, "path"));
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002317 len += strlen(qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302318 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2319
2320 if (!arg_fsdev) {
2321 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2322 optarg);
2323 exit(1);
2324 }
2325
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002326 sprintf(arg_fsdev, "%s,id=%s,path=%s,security_model=%s",
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302327 qemu_opt_get(opts, "fstype"),
2328 qemu_opt_get(opts, "mount_tag"),
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002329 qemu_opt_get(opts, "path"),
2330 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302331
2332 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2333 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2334 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2335
2336 if (!arg_9p) {
2337 fprintf(stderr, "No memory to parse -device for %s\n",
2338 optarg);
2339 exit(1);
2340 }
2341
2342 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2343 qemu_opt_get(opts, "mount_tag"),
2344 qemu_opt_get(opts, "mount_tag"));
2345
2346 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2347 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2348 exit(1);
2349 }
2350
2351 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2352 fprintf(stderr, "parse error [device]: %s\n", optarg);
2353 exit(1);
2354 }
2355
2356 qemu_free(arg_fsdev);
2357 qemu_free(arg_9p);
2358 break;
2359 }
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302360#endif
bellard82c643f2004-07-14 17:28:13 +00002361 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002362 add_device_config(DEV_SERIAL, optarg);
2363 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002364 if (strncmp(optarg, "mon:", 4) == 0) {
2365 default_monitor = 0;
2366 }
bellard82c643f2004-07-14 17:28:13 +00002367 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002368 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002369 if (watchdog) {
2370 fprintf(stderr,
2371 "qemu: only one watchdog option may be given\n");
2372 return 1;
2373 }
2374 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002375 break;
2376 case QEMU_OPTION_watchdog_action:
2377 if (select_watchdog_action(optarg) == -1) {
2378 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2379 exit(1);
2380 }
2381 break;
aliguori51ecf132009-01-15 20:06:40 +00002382 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002383 add_device_config(DEV_VIRTCON, optarg);
2384 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002385 if (strncmp(optarg, "mon:", 4) == 0) {
2386 default_monitor = 0;
2387 }
aliguori51ecf132009-01-15 20:06:40 +00002388 break;
bellard6508fe52005-01-15 12:02:56 +00002389 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002390 add_device_config(DEV_PARALLEL, optarg);
2391 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002392 if (strncmp(optarg, "mon:", 4) == 0) {
2393 default_monitor = 0;
2394 }
bellard6508fe52005-01-15 12:02:56 +00002395 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002396 case QEMU_OPTION_debugcon:
2397 add_device_config(DEV_DEBUGCON, optarg);
2398 break;
bellardd63d3072004-10-03 13:29:03 +00002399 case QEMU_OPTION_loadvm:
2400 loadvm = optarg;
2401 break;
2402 case QEMU_OPTION_full_screen:
2403 full_screen = 1;
2404 break;
ths667acca2006-12-11 02:08:05 +00002405#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002406 case QEMU_OPTION_no_frame:
2407 no_frame = 1;
2408 break;
ths3780e192007-06-21 21:08:02 +00002409 case QEMU_OPTION_alt_grab:
2410 alt_grab = 1;
2411 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002412 case QEMU_OPTION_ctrl_grab:
2413 ctrl_grab = 1;
2414 break;
ths667acca2006-12-11 02:08:05 +00002415 case QEMU_OPTION_no_quit:
2416 no_quit = 1;
2417 break;
aliguori7d957bd2009-01-15 22:14:11 +00002418 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002419 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002420 break;
ths667acca2006-12-11 02:08:05 +00002421#endif
bellardf7cce892004-12-08 22:21:25 +00002422 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002423 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002424 break;
bellarda09db212005-04-30 16:10:35 +00002425 case QEMU_OPTION_win2k_hack:
2426 win2k_install_hack = 1;
2427 break;
aliguori73822ec2009-01-15 20:11:34 +00002428 case QEMU_OPTION_rtc_td_hack:
2429 rtc_td_hack = 1;
2430 break;
aliguori8a92ea22009-02-27 20:12:36 +00002431 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002432 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002433 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002434 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002435 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002436 break;
aliguori7ba1e612008-11-05 16:04:33 +00002437 case QEMU_OPTION_enable_kvm:
2438 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00002439 break;
bellardbb36d472005-11-05 14:22:28 +00002440 case QEMU_OPTION_usb:
2441 usb_enabled = 1;
2442 break;
bellarda594cfb2005-11-06 16:13:29 +00002443 case QEMU_OPTION_usbdevice:
2444 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002445 add_device_config(DEV_USB, optarg);
2446 break;
2447 case QEMU_OPTION_device:
Markus Armbruster8212c642010-02-10 19:52:18 +01002448 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002449 exit(1);
2450 }
bellarda594cfb2005-11-06 16:13:29 +00002451 break;
bellard6a00d602005-11-21 23:25:50 +00002452 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002453 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002454 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002455 fprintf(stderr, "Invalid number of CPUs\n");
2456 exit(1);
2457 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002458 if (max_cpus < smp_cpus) {
2459 fprintf(stderr, "maxcpus must be equal to or greater than "
2460 "smp\n");
2461 exit(1);
2462 }
2463 if (max_cpus > 255) {
2464 fprintf(stderr, "Unsupported number of maxcpus\n");
2465 exit(1);
2466 }
bellard6a00d602005-11-21 23:25:50 +00002467 break;
bellard24236862006-04-30 21:28:36 +00002468 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002469 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00002470 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00002471 break;
bellard6515b202006-05-03 22:02:44 +00002472 case QEMU_OPTION_no_acpi:
2473 acpi_enabled = 0;
2474 break;
aliguori16b29ae2008-12-17 23:28:44 +00002475 case QEMU_OPTION_no_hpet:
2476 no_hpet = 1;
2477 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002478 case QEMU_OPTION_balloon:
2479 if (balloon_parse(optarg) < 0) {
2480 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2481 exit(1);
2482 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002483 break;
bellardd1beab82006-10-02 19:44:22 +00002484 case QEMU_OPTION_no_reboot:
2485 no_reboot = 1;
2486 break;
aurel32b2f76162008-04-11 21:35:52 +00002487 case QEMU_OPTION_no_shutdown:
2488 no_shutdown = 1;
2489 break;
balrog9467cd42007-05-01 01:34:14 +00002490 case QEMU_OPTION_show_cursor:
2491 cursor_hide = 0;
2492 break;
blueswir18fcb1b92008-09-18 18:29:08 +00002493 case QEMU_OPTION_uuid:
2494 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2495 fprintf(stderr, "Fail to parse UUID string."
2496 " Wrong format.\n");
2497 exit(1);
2498 }
2499 break;
ths9ae02552007-01-05 17:39:04 +00002500 case QEMU_OPTION_option_rom:
2501 if (nb_option_roms >= MAX_OPTION_ROMS) {
2502 fprintf(stderr, "Too many option ROMs\n");
2503 exit(1);
2504 }
2505 option_rom[nb_option_roms] = optarg;
2506 nb_option_roms++;
2507 break;
pbrook8e716212007-01-20 17:12:09 +00002508 case QEMU_OPTION_semihosting:
2509 semihosting_enabled = 1;
2510 break;
thsc35734b2007-03-19 15:17:08 +00002511 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02002512 qemu_name = qemu_strdup(optarg);
2513 {
2514 char *p = strchr(qemu_name, ',');
2515 if (p != NULL) {
2516 *p++ = 0;
2517 if (strncmp(p, "process=", 8)) {
2518 fprintf(stderr, "Unknown subargument %s to -name", p);
2519 exit(1);
2520 }
2521 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02002522 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02002523 }
2524 }
thsc35734b2007-03-19 15:17:08 +00002525 break;
blueswir166508602007-05-01 14:16:52 +00002526 case QEMU_OPTION_prom_env:
2527 if (nb_prom_envs >= MAX_PROM_ENVS) {
2528 fprintf(stderr, "Too many prom variables\n");
2529 exit(1);
2530 }
2531 prom_envs[nb_prom_envs] = optarg;
2532 nb_prom_envs++;
2533 break;
balrog2b8f2d42007-07-27 22:08:46 +00002534 case QEMU_OPTION_old_param:
2535 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00002536 break;
thsf3dcfad2007-08-24 01:26:02 +00002537 case QEMU_OPTION_clock:
2538 configure_alarms(optarg);
2539 break;
bellard7e0af5d02007-11-07 16:24:33 +00002540 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002541 configure_rtc_date_offset(optarg, 1);
2542 break;
2543 case QEMU_OPTION_rtc:
Markus Armbruster8212c642010-02-10 19:52:18 +01002544 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002545 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002546 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00002547 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002548 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00002549 break;
bellard26a5f132008-05-28 12:30:31 +00002550 case QEMU_OPTION_tb_size:
2551 tb_size = strtol(optarg, NULL, 0);
2552 if (tb_size < 0)
2553 tb_size = 0;
2554 break;
pbrook2e70f6e2008-06-29 01:03:05 +00002555 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002556 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00002557 break;
aliguori5bb79102008-10-13 03:12:02 +00002558 case QEMU_OPTION_incoming:
2559 incoming = optarg;
2560 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002561 case QEMU_OPTION_nodefaults:
2562 default_serial = 0;
2563 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002564 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002565 default_monitor = 0;
2566 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01002567 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002568 default_floppy = 0;
2569 default_cdrom = 0;
2570 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002571 break;
aliguorie37630c2009-04-22 15:19:10 +00002572 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00002573 if (!(xen_available())) {
2574 printf("Option %s not supported for this target\n", popt->name);
2575 exit(1);
2576 }
aliguorie37630c2009-04-22 15:19:10 +00002577 xen_domid = atoi(optarg);
2578 break;
2579 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00002580 if (!(xen_available())) {
2581 printf("Option %s not supported for this target\n", popt->name);
2582 exit(1);
2583 }
aliguorie37630c2009-04-22 15:19:10 +00002584 xen_mode = XEN_CREATE;
2585 break;
2586 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00002587 if (!(xen_available())) {
2588 printf("Option %s not supported for this target\n", popt->name);
2589 exit(1);
2590 }
aliguorie37630c2009-04-22 15:19:10 +00002591 xen_mode = XEN_ATTACH;
2592 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002593 case QEMU_OPTION_readconfig:
2594 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002595 int ret = qemu_read_config_file(optarg);
2596 if (ret < 0) {
2597 fprintf(stderr, "read config %s: %s\n", optarg,
2598 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002599 exit(1);
2600 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002601 break;
2602 }
2603 case QEMU_OPTION_writeconfig:
2604 {
2605 FILE *fp;
2606 if (strcmp(optarg, "-") == 0) {
2607 fp = stdout;
2608 } else {
2609 fp = fopen(optarg, "w");
2610 if (fp == NULL) {
2611 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2612 exit(1);
2613 }
2614 }
2615 qemu_config_write(fp);
2616 fclose(fp);
2617 break;
2618 }
Jes Sorensen59a52642010-06-10 11:42:25 +02002619 default:
2620 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00002621 }
bellard0824d6f2003-06-24 13:42:40 +00002622 }
2623 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002624 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00002625
Paul Brook5cea8592009-05-30 00:52:44 +01002626 /* If no data_dir is specified then try to find it relative to the
2627 executable path. */
2628 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02002629 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01002630 }
2631 /* If all else fails use the install patch specified when building. */
2632 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02002633 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01002634 }
2635
Jes Sorensen6be68d72009-07-23 17:03:42 +02002636 /*
2637 * Default to max_cpus = smp_cpus, in case the user doesn't
2638 * specify a max_cpus value.
2639 */
2640 if (!max_cpus)
2641 max_cpus = smp_cpus;
2642
balrog3d878ca2008-10-28 10:59:59 +00002643 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00002644 if (smp_cpus > machine->max_cpus) {
2645 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2646 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2647 machine->max_cpus);
2648 exit(1);
2649 }
2650
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002651 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +01002652 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002653
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002654 if (machine->no_serial) {
2655 default_serial = 0;
2656 }
2657 if (machine->no_parallel) {
2658 default_parallel = 0;
2659 }
2660 if (!machine->use_virtcon) {
2661 default_virtcon = 0;
2662 }
2663 if (machine->no_vga) {
2664 default_vga = 0;
2665 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002666 if (machine->no_floppy) {
2667 default_floppy = 0;
2668 }
2669 if (machine->no_cdrom) {
2670 default_cdrom = 0;
2671 }
2672 if (machine->no_sdcard) {
2673 default_sdcard = 0;
2674 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002675
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002676 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002677 if (default_parallel)
2678 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002679 if (default_serial && default_monitor) {
2680 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002681 } else if (default_virtcon && default_monitor) {
2682 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002683 } else {
2684 if (default_serial)
2685 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002686 if (default_virtcon)
2687 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002688 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002689 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002690 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002691 } else {
2692 if (default_serial)
2693 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002694 if (default_parallel)
2695 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01002696 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002697 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01002698 if (default_virtcon)
2699 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00002700 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01002701 if (default_vga)
2702 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00002703
TeLeMana5829fd2010-04-15 12:37:55 +08002704 socket_init();
2705
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002706 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
2707 exit(1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302708#ifdef CONFIG_LINUX
2709 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
2710 exit(1);
2711 }
2712#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002713
Jes Sorenseneb505be2010-06-10 11:42:28 +02002714 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00002715
thsaa26bb22007-03-25 21:33:06 +00002716 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02002717 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00002718 exit(1);
2719 }
2720
Paolo Bonzini98c85732010-04-19 18:59:30 +00002721 if (kvm_allowed) {
2722 int ret = kvm_init(smp_cpus);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002723 if (ret < 0) {
Paolo Bonzini98c85732010-04-19 18:59:30 +00002724 if (!kvm_available()) {
2725 printf("KVM not supported for this target\n");
2726 } else {
2727 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2728 }
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002729 exit(1);
2730 }
2731 }
2732
aliguori3fcf7b62009-04-24 18:03:25 +00002733 if (qemu_init_main_loop()) {
2734 fprintf(stderr, "qemu_init_main_loop failed\n");
2735 exit(1);
2736 }
bellarda20dd502003-09-30 21:07:02 +00002737 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00002738
thsf8d39c02008-07-03 10:01:15 +00002739 if (!linux_boot && *kernel_cmdline != '\0') {
2740 fprintf(stderr, "-append only allowed with -kernel option\n");
2741 exit(1);
2742 }
2743
2744 if (!linux_boot && initrd_filename != NULL) {
2745 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2746 exit(1);
2747 }
2748
Jes Sorensen9156d762010-06-10 11:42:30 +02002749 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00002750
aliguori7183b4b2008-11-05 20:40:18 +00002751 if (init_timer_alarm() < 0) {
2752 fprintf(stderr, "could not initialize alarm timer\n");
2753 exit(1);
2754 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002755 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00002756
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01002757 if (net_init_clients() < 0) {
2758 exit(1);
bellard702c6512004-04-02 21:21:32 +00002759 }
bellardf1510b22003-06-25 00:07:40 +00002760
balrogdc72ac12008-11-09 00:04:26 +00002761 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002762 if (foreach_device_config(DEV_BT, bt_parse))
2763 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00002764
bellard0824d6f2003-06-24 13:42:40 +00002765 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00002766 if (ram_size == 0)
2767 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00002768
bellard26a5f132008-05-28 12:30:31 +00002769 /* init the dynamic translator */
2770 cpu_exec_init_all(tb_size * 1024 * 1024);
2771
Markus Armbrustereb852012009-10-27 18:41:44 +01002772 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00002773
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002774 blk_mig_init();
2775
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002776 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002777 /* we always create the cdrom drive, even if no disk is there */
2778 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002779 }
thse4bcb142007-12-02 04:51:10 +00002780
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002781 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002782 /* we always create at least one floppy */
2783 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002784 }
bellardc4b1fcc2004-03-14 21:44:30 +00002785
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002786 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002787 /* we always create one sd slot, even if no card is in it */
2788 drive_add(NULL, SD_ALIAS);
2789 }
balrog9d413d12007-12-04 00:10:34 +00002790
ths96d30e42007-01-07 20:42:14 +00002791 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002792 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02002793 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
Markus Armbrustera803cb82010-06-02 13:31:55 +02002794 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002795 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00002796
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002797 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
2798 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00002799
aliguori268a3622009-04-21 22:30:27 +00002800 if (nb_numa_nodes > 0) {
2801 int i;
2802
2803 if (nb_numa_nodes > smp_cpus) {
2804 nb_numa_nodes = smp_cpus;
2805 }
2806
2807 /* If no memory size if given for any node, assume the default case
2808 * and distribute the available memory equally across all nodes
2809 */
2810 for (i = 0; i < nb_numa_nodes; i++) {
2811 if (node_mem[i] != 0)
2812 break;
2813 }
2814 if (i == nb_numa_nodes) {
2815 uint64_t usedmem = 0;
2816
2817 /* On Linux, the each node's border has to be 8MB aligned,
2818 * the final node gets the rest.
2819 */
2820 for (i = 0; i < nb_numa_nodes - 1; i++) {
2821 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2822 usedmem += node_mem[i];
2823 }
2824 node_mem[i] = ram_size - usedmem;
2825 }
2826
2827 for (i = 0; i < nb_numa_nodes; i++) {
2828 if (node_cpumask[i] != 0)
2829 break;
2830 }
2831 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2832 * must cope with this anyway, because there are BIOSes out there in
2833 * real machines which also use this scheme.
2834 */
2835 if (i == nb_numa_nodes) {
2836 for (i = 0; i < smp_cpus; i++) {
2837 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2838 }
2839 }
2840 }
2841
Jan Kiszka157b9312010-04-06 16:55:53 +02002842 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
2843 exit(1);
2844 }
2845
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002846 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2847 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002848 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2849 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002850 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2851 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002852 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2853 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00002854
Paul Brookaae94602009-05-14 22:35:06 +01002855 module_call_init(MODULE_INIT_DEVICE);
2856
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002857 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
2858 exit(0);
2859
Markus Armbruster09aaa162009-08-21 10:31:34 +02002860 if (watchdog) {
2861 i = select_watchdog(watchdog);
2862 if (i > 0)
2863 exit (i == 1 ? 1 : 0);
2864 }
2865
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002866 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01002867 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002868 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002869 qemu_add_globals();
2870
Paul Brookfbe1b592009-05-13 17:56:25 +01002871 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00002872 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2873
Jan Kiszkaea375f92010-03-01 19:10:30 +01002874 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00002875
Juan Quintela67b3b712009-08-28 19:25:15 +02002876 /* must be after terminal init, SDL library changes signal handlers */
Jes Sorensen8d963e62010-06-10 11:42:22 +02002877 os_setup_signal_handling();
Juan Quintela67b3b712009-08-28 19:25:15 +02002878
Blue Swirl87d0a282010-03-27 18:24:45 +00002879 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00002880
aliguori6f338c32009-02-11 15:21:54 +00002881 current_machine = machine;
2882
aliguori3023f332009-01-16 19:04:14 +00002883 /* init USB devices */
2884 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01002885 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2886 exit(1);
aliguori3023f332009-01-16 19:04:14 +00002887 }
2888
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002889 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002890 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002891 exit(1);
2892
Markus Armbruster668680f2010-02-11 14:44:58 +01002893 net_check_clients();
2894
aliguori3023f332009-01-16 19:04:14 +00002895 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01002896 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002897
2898 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002899#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002900 display_type = DT_SDL;
2901#else
2902 display_type = DT_VNC;
2903 vnc_display = "localhost:0,to=99";
2904 show_vnc_port = 1;
2905#endif
2906 }
2907
2908
2909 switch (display_type) {
2910 case DT_NOGRAPHIC:
2911 break;
2912#if defined(CONFIG_CURSES)
2913 case DT_CURSES:
2914 curses_display_init(ds, full_screen);
2915 break;
2916#endif
bellard5b0753e2005-03-01 21:37:28 +00002917#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002918 case DT_SDL:
2919 sdl_display_init(ds, full_screen, no_frame);
2920 break;
bellard5b0753e2005-03-01 21:37:28 +00002921#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002922 case DT_SDL:
2923 cocoa_display_init(ds, full_screen);
2924 break;
bellard313aa562003-08-10 21:52:11 +00002925#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002926 case DT_VNC:
2927 vnc_display_init(ds);
2928 if (vnc_display_open(ds, vnc_display) < 0)
2929 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002930
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002931 if (show_vnc_port) {
2932 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002933 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002934 break;
2935 default:
2936 break;
bellard313aa562003-08-10 21:52:11 +00002937 }
aliguori7d957bd2009-01-15 22:14:11 +00002938 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00002939
aliguori3023f332009-01-16 19:04:14 +00002940 dcl = ds->listeners;
2941 while (dcl != NULL) {
2942 if (dcl->dpy_refresh != NULL) {
2943 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2944 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09002945 break;
ths20d8a3e2007-02-18 17:04:49 +00002946 }
aliguori3023f332009-01-16 19:04:14 +00002947 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00002948 }
aliguori3023f332009-01-16 19:04:14 +00002949
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002950 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00002951 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2952 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2953 }
2954
Paolo Bonzinib473df62010-02-11 00:29:55 +01002955 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00002956
aliguori59030a82009-04-05 18:43:41 +00002957 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2958 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2959 gdbstub_dev);
2960 exit(1);
balrog45669e02007-07-02 13:20:17 +00002961 }
balrog45669e02007-07-02 13:20:17 +00002962
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02002963 qdev_machine_creation_done();
2964
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01002965 if (rom_load_all() != 0) {
2966 fprintf(stderr, "rom loading failed\n");
2967 exit(1);
2968 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02002969
Juan Quintela504c2942009-11-12 00:39:13 +01002970 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02002971 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01002972 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02002973 autostart = 0;
2974 }
2975 }
bellardd63d3072004-10-03 13:29:03 +00002976
Glauber Costa2bb8c102009-07-24 16:20:23 -04002977 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00002978 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03002979 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00002980 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03002981 }
thsffd843b2006-12-21 19:46:43 +00002982
Jes Sorenseneb505be2010-06-10 11:42:28 +02002983 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00002984
bellard8a7ddc32004-03-31 19:00:16 +00002985 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00002986 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00002987 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00002988
bellard0824d6f2003-06-24 13:42:40 +00002989 return 0;
2990}