blob: c58583da48d6309eab9a3cdb09eef9571c599fd1 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000038#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000039#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000042#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000043#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000044#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000047#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000048#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000049#include <sys/select.h>
Prerna Saxenaab6540d2010-08-09 11:48:32 +010050#ifdef CONFIG_SIMPLE_TRACE
51#include "trace.h"
52#endif
53
Juan Quintela71e72a12009-07-27 16:12:56 +020054#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000055#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010056#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000057#include <libutil.h>
Juergen Lock92c0e652010-03-25 22:07:12 +010058#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000059#else
60#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000061#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010062#else
blueswir1223f0d72008-09-30 18:12:18 +000063#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000064#include <pty.h>
65#include <malloc.h>
thsbd494f42007-09-16 20:03:23 +000066
bellarde57a8c02005-11-10 23:58:52 +000067#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000068#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000069#endif
70#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000071#include <sys/stat.h>
72#include <sys/ethernet.h>
73#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000074#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000075#include <netinet/in_systm.h>
76#include <netinet/ip.h>
77#include <netinet/ip_icmp.h> // must come after ip.h
78#include <netinet/udp.h>
79#include <netinet/tcp.h>
80#include <net/if.h>
81#include <syslog.h>
82#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000083#endif
bellard7d3505c2004-05-12 19:32:15 +000084#endif
bellardec530c82006-04-25 22:36:06 +000085#endif
bellard67b915a2004-03-31 23:37:16 +000086
blueswir19892fbf2008-08-24 10:34:20 +000087#if defined(__OpenBSD__)
88#include <util.h>
89#endif
90
ths8a16d272008-07-19 09:56:24 +000091#if defined(CONFIG_VDE)
92#include <libvdeplug.h>
93#endif
94
bellard67b915a2004-03-31 23:37:16 +000095#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000096#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000097#endif
98
bellard73332e52004-04-04 20:22:28 +000099#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200100#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200101#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000102int qemu_main(int argc, char **argv, char **envp);
103int main(int argc, char **argv)
104{
Stefan Weil59a36a22009-06-18 20:11:03 +0200105 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000106}
107#undef main
108#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000109#endif
bellard73332e52004-04-04 20:22:28 +0000110#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000111
bellard5b0753e2005-03-01 21:37:28 +0000112#ifdef CONFIG_COCOA
113#undef main
114#define main qemu_main
115#endif /* CONFIG_COCOA */
116
blueswir1511d2b12009-03-07 15:32:56 +0000117#include "hw/hw.h"
118#include "hw/boards.h"
119#include "hw/usb.h"
120#include "hw/pcmcia.h"
121#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000122#include "hw/isa.h"
123#include "hw/baum.h"
124#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100125#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000126#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000127#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200128#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200129#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000130#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000131#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000132#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000133#include "monitor.h"
134#include "console.h"
135#include "sysemu.h"
136#include "gdbstub.h"
137#include "qemu-timer.h"
138#include "qemu-char.h"
139#include "cache-utils.h"
140#include "block.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200141#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200142#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000143#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000144#include "audio/audio.h"
145#include "migration.h"
146#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200147#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200148#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200149#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200150#include "qemu-options.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700151#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530152#include "fsdev/qemu-fsdev.h"
153#endif
blueswir1511d2b12009-03-07 15:32:56 +0000154
bellard0824d6f2003-06-24 13:42:40 +0000155#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000156
blueswir1511d2b12009-03-07 15:32:56 +0000157#include "qemu_socket.h"
158
Jan Kiszkad918f232009-06-24 14:42:30 +0200159#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
Blue Swirl72cf2d42009-09-12 07:36:22 +0000161#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000162#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000163#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000164
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300165#include "ui/qemu-spice.h"
166
blueswir19dc63a12008-10-04 07:25:46 +0000167//#define DEBUG_NET
168//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000169
bellard1bfe8562004-07-08 21:17:50 +0000170#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000171
Amit Shah98b19252010-01-20 00:36:52 +0530172#define MAX_VIRTIO_CONSOLES 1
173
Paul Brook5cea8592009-05-30 00:52:44 +0100174static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000175const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000176enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500177DisplayType display_type = DT_DEFAULT;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +0200178int display_remote = 0;
bellard3d11d0e2004-12-12 16:56:30 +0000179const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500180ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300181const char *mem_path = NULL;
182#ifdef MAP_POPULATE
183int mem_prealloc = 0; /* force preallocation of physical target memory */
184#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000185int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000186NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000187int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200188int autostart;
Amit Shah8e848652010-07-27 15:49:19 +0530189int incoming_expected; /* Started with -incoming and waiting for incoming */
balrogf6503052008-02-17 11:42:19 +0000190static int rtc_utc = 1;
191static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200192QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100193int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000194static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000195#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000196static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000197#endif
ths667acca2006-12-11 02:08:05 +0000198int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000199CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000200CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000201CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000202int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000203int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000204int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000205int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000206int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200207int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200208int smp_cores = 1;
209int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000210const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000211int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000212int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000213int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000214int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000215int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000216int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000217int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200218uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200219const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000220const char *option_rom[MAX_OPTION_ROMS];
221int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000222int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000223int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000224const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000225int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500226int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000227unsigned int nb_prom_envs = 0;
228const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200229int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000230
aliguori268a3622009-04-21 22:30:27 +0000231int nb_numa_nodes;
232uint64_t node_mem[MAX_NODES];
233uint64_t node_cpumask[MAX_NODES];
234
blueswir19043b622009-01-21 19:28:13 +0000235static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000236
blueswir18fcb1b92008-09-18 18:29:08 +0000237uint8_t qemu_uuid[16];
238
Jan Kiszka76e30d02009-07-02 00:19:02 +0200239static QEMUBootSetHandler *boot_set_handler;
240static void *boot_set_opaque;
241
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200242static NotifierList exit_notifiers =
243 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
244
Blue Swirld745bef2010-03-29 19:23:49 +0000245int kvm_allowed = 0;
246uint32_t xen_domid;
247enum xen_mode xen_mode = XEN_EMULATE;
248
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100249static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100250static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100251static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100252static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100253static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100254static int default_floppy = 1;
255static int default_cdrom = 1;
256static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100257
258static struct {
259 const char *driver;
260 int *flag;
261} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100262 { .driver = "isa-serial", .flag = &default_serial },
263 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100264 { .driver = "isa-fdc", .flag = &default_floppy },
265 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530266 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
267 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
268 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100269 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100270 { .driver = "cirrus-vga", .flag = &default_vga },
271 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100272};
273
274static int default_driver_check(QemuOpts *opts, void *opaque)
275{
276 const char *driver = qemu_opt_get(opts, "driver");
277 int i;
278
279 if (!driver)
280 return 0;
281 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
282 if (strcmp(default_list[i].driver, driver) != 0)
283 continue;
284 *(default_list[i].flag) = 0;
285 }
286 return 0;
287}
288
bellard0824d6f2003-06-24 13:42:40 +0000289/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100290/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000291
bellardc4b1fcc2004-03-14 21:44:30 +0000292/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000293/* host time/date access */
294void qemu_get_timedate(struct tm *tm, int offset)
295{
296 time_t ti;
297 struct tm *ret;
298
299 time(&ti);
300 ti += offset;
301 if (rtc_date_offset == -1) {
302 if (rtc_utc)
303 ret = gmtime(&ti);
304 else
305 ret = localtime(&ti);
306 } else {
307 ti -= rtc_date_offset;
308 ret = gmtime(&ti);
309 }
310
311 memcpy(tm, ret, sizeof(struct tm));
312}
313
314int qemu_timedate_diff(struct tm *tm)
315{
316 time_t seconds;
317
318 if (rtc_date_offset == -1)
319 if (rtc_utc)
320 seconds = mktimegm(tm);
321 else
322 seconds = mktime(tm);
323 else
324 seconds = mktimegm(tm) + rtc_date_offset;
325
326 return seconds - time(NULL);
327}
328
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300329void rtc_change_mon_event(struct tm *tm)
330{
331 QObject *data;
332
333 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
334 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
335 qobject_decref(data);
336}
337
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200338static void configure_rtc_date_offset(const char *startdate, int legacy)
339{
340 time_t rtc_start_date;
341 struct tm tm;
342
343 if (!strcmp(startdate, "now") && legacy) {
344 rtc_date_offset = -1;
345 } else {
346 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
347 &tm.tm_year,
348 &tm.tm_mon,
349 &tm.tm_mday,
350 &tm.tm_hour,
351 &tm.tm_min,
352 &tm.tm_sec) == 6) {
353 /* OK */
354 } else if (sscanf(startdate, "%d-%d-%d",
355 &tm.tm_year,
356 &tm.tm_mon,
357 &tm.tm_mday) == 3) {
358 tm.tm_hour = 0;
359 tm.tm_min = 0;
360 tm.tm_sec = 0;
361 } else {
362 goto date_fail;
363 }
364 tm.tm_year -= 1900;
365 tm.tm_mon--;
366 rtc_start_date = mktimegm(&tm);
367 if (rtc_start_date == -1) {
368 date_fail:
369 fprintf(stderr, "Invalid date format. Valid formats are:\n"
370 "'2006-06-17T16:01:21' or '2006-06-17'\n");
371 exit(1);
372 }
373 rtc_date_offset = time(NULL) - rtc_start_date;
374 }
375}
376
377static void configure_rtc(QemuOpts *opts)
378{
379 const char *value;
380
381 value = qemu_opt_get(opts, "base");
382 if (value) {
383 if (!strcmp(value, "utc")) {
384 rtc_utc = 1;
385 } else if (!strcmp(value, "localtime")) {
386 rtc_utc = 0;
387 } else {
388 configure_rtc_date_offset(value, 0);
389 }
390 }
Jan Kiszka68752042009-09-15 13:36:04 +0200391 value = qemu_opt_get(opts, "clock");
392 if (value) {
393 if (!strcmp(value, "host")) {
394 rtc_clock = host_clock;
395 } else if (!strcmp(value, "vm")) {
396 rtc_clock = vm_clock;
397 } else {
398 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
399 exit(1);
400 }
401 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200402 value = qemu_opt_get(opts, "driftfix");
403 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000404 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200405 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000406 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200407 rtc_td_hack = 0;
408 } else {
409 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
410 exit(1);
411 }
412 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200413}
414
balrog1ae26a12008-09-28 23:19:47 +0000415/***********************************************************/
416/* Bluetooth support */
417static int nb_hcis;
418static int cur_hci;
419static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000420
balrog1ae26a12008-09-28 23:19:47 +0000421static struct bt_vlan_s {
422 struct bt_scatternet_s net;
423 int id;
424 struct bt_vlan_s *next;
425} *first_bt_vlan;
426
427/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000428static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000429{
430 struct bt_vlan_s **pvlan, *vlan;
431 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
432 if (vlan->id == id)
433 return &vlan->net;
434 }
435 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
436 vlan->id = id;
437 pvlan = &first_bt_vlan;
438 while (*pvlan != NULL)
439 pvlan = &(*pvlan)->next;
440 *pvlan = vlan;
441 return &vlan->net;
442}
443
444static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
445{
446}
447
448static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
449{
450 return -ENOTSUP;
451}
452
453static struct HCIInfo null_hci = {
454 .cmd_send = null_hci_send,
455 .sco_send = null_hci_send,
456 .acl_send = null_hci_send,
457 .bdaddr_set = null_hci_addr_set,
458};
459
460struct HCIInfo *qemu_next_hci(void)
461{
462 if (cur_hci == nb_hcis)
463 return &null_hci;
464
465 return hci_table[cur_hci++];
466}
467
balrogdc72ac12008-11-09 00:04:26 +0000468static struct HCIInfo *hci_init(const char *str)
469{
470 char *endp;
471 struct bt_scatternet_s *vlan = 0;
472
473 if (!strcmp(str, "null"))
474 /* null */
475 return &null_hci;
476 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
477 /* host[:hciN] */
478 return bt_host_hci(str[4] ? str + 5 : "hci0");
479 else if (!strncmp(str, "hci", 3)) {
480 /* hci[,vlan=n] */
481 if (str[3]) {
482 if (!strncmp(str + 3, ",vlan=", 6)) {
483 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
484 if (*endp)
485 vlan = 0;
486 }
487 } else
488 vlan = qemu_find_bt_vlan(0);
489 if (vlan)
490 return bt_new_hci(vlan);
491 }
492
493 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
494
495 return 0;
496}
497
498static int bt_hci_parse(const char *str)
499{
500 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500501 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000502
503 if (nb_hcis >= MAX_NICS) {
504 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
505 return -1;
506 }
507
508 hci = hci_init(str);
509 if (!hci)
510 return -1;
511
512 bdaddr.b[0] = 0x52;
513 bdaddr.b[1] = 0x54;
514 bdaddr.b[2] = 0x00;
515 bdaddr.b[3] = 0x12;
516 bdaddr.b[4] = 0x34;
517 bdaddr.b[5] = 0x56 + nb_hcis;
518 hci->bdaddr_set(hci, bdaddr.b);
519
520 hci_table[nb_hcis++] = hci;
521
522 return 0;
523}
524
525static void bt_vhci_add(int vlan_id)
526{
527 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
528
529 if (!vlan->slave)
530 fprintf(stderr, "qemu: warning: adding a VHCI to "
531 "an empty scatternet %i\n", vlan_id);
532
533 bt_vhci_init(bt_new_hci(vlan));
534}
535
536static struct bt_device_s *bt_device_add(const char *opt)
537{
538 struct bt_scatternet_s *vlan;
539 int vlan_id = 0;
540 char *endp = strstr(opt, ",vlan=");
541 int len = (endp ? endp - opt : strlen(opt)) + 1;
542 char devname[10];
543
544 pstrcpy(devname, MIN(sizeof(devname), len), opt);
545
546 if (endp) {
547 vlan_id = strtol(endp + 6, &endp, 0);
548 if (*endp) {
549 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
550 return 0;
551 }
552 }
553
554 vlan = qemu_find_bt_vlan(vlan_id);
555
556 if (!vlan->slave)
557 fprintf(stderr, "qemu: warning: adding a slave device to "
558 "an empty scatternet %i\n", vlan_id);
559
560 if (!strcmp(devname, "keyboard"))
561 return bt_keyboard_init(vlan);
562
563 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
564 return 0;
565}
566
567static int bt_parse(const char *opt)
568{
569 const char *endp, *p;
570 int vlan;
571
572 if (strstart(opt, "hci", &endp)) {
573 if (!*endp || *endp == ',') {
574 if (*endp)
575 if (!strstart(endp, ",vlan=", 0))
576 opt = endp + 1;
577
578 return bt_hci_parse(opt);
579 }
580 } else if (strstart(opt, "vhci", &endp)) {
581 if (!*endp || *endp == ',') {
582 if (*endp) {
583 if (strstart(endp, ",vlan=", &p)) {
584 vlan = strtol(p, (char **) &endp, 0);
585 if (*endp) {
586 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
587 return 1;
588 }
589 } else {
590 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
591 return 1;
592 }
593 } else
594 vlan = 0;
595
596 bt_vhci_add(vlan);
597 return 0;
598 }
599 } else if (strstart(opt, "device:", &endp))
600 return !bt_device_add(endp);
601
602 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
603 return 1;
604}
605
balrog1ae26a12008-09-28 23:19:47 +0000606/***********************************************************/
607/* QEMU Block devices */
608
balrog609497a2008-01-14 02:56:53 +0000609#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +0000610#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +0000611#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +0000612#define PFLASH_ALIAS "if=pflash"
613#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +0000614#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +0000615
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200616static int drive_init_func(QemuOpts *opts, void *opaque)
617{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200618 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200619 int fatal_error = 0;
620
Markus Armbrustera803cb82010-06-02 13:31:55 +0200621 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200622 if (fatal_error)
623 return 1;
624 }
625 return 0;
626}
627
628static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
629{
630 if (NULL == qemu_opt_get(opts, "snapshot")) {
631 qemu_opt_set(opts, "snapshot", "on");
632 }
633 return 0;
634}
635
Jan Kiszka76e30d02009-07-02 00:19:02 +0200636void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
637{
638 boot_set_handler = func;
639 boot_set_opaque = opaque;
640}
641
642int qemu_boot_set(const char *boot_devices)
643{
644 if (!boot_set_handler) {
645 return -EINVAL;
646 }
647 return boot_set_handler(boot_set_opaque, boot_devices);
648}
649
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300650static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200651{
652 /* We just do some generic consistency checks */
653 const char *p;
654 int bitmap = 0;
655
656 for (p = devices; *p != '\0'; p++) {
657 /* Allowed boot devices are:
658 * a-b: floppy disk drives
659 * c-f: IDE disk drives
660 * g-m: machine implementation dependant drives
661 * n-p: network devices
662 * It's up to each machine implementation to check if the given boot
663 * devices match the actual hardware implementation and firmware
664 * features.
665 */
666 if (*p < 'a' || *p > 'p') {
667 fprintf(stderr, "Invalid boot device '%c'\n", *p);
668 exit(1);
669 }
670 if (bitmap & (1 << (*p - 'a'))) {
671 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
672 exit(1);
673 }
674 bitmap |= 1 << (*p - 'a');
675 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200676}
677
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200678static void restore_boot_devices(void *opaque)
679{
680 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600681 static int first = 1;
682
683 /* Restore boot order and remove ourselves after the first boot */
684 if (first) {
685 first = 0;
686 return;
687 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200688
689 qemu_boot_set(standard_boot_devices);
690
691 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
692 qemu_free(standard_boot_devices);
693}
694
aliguori268a3622009-04-21 22:30:27 +0000695static void numa_add(const char *optarg)
696{
697 char option[128];
698 char *endptr;
699 unsigned long long value, endvalue;
700 int nodenr;
701
702 optarg = get_opt_name(option, 128, optarg, ',') + 1;
703 if (!strcmp(option, "node")) {
704 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
705 nodenr = nb_numa_nodes;
706 } else {
707 nodenr = strtoull(option, NULL, 10);
708 }
709
710 if (get_param_value(option, 128, "mem", optarg) == 0) {
711 node_mem[nodenr] = 0;
712 } else {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200713 ssize_t sval;
714 sval = strtosz(option, NULL);
715 if (sval < 0) {
716 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
717 exit(1);
aliguori268a3622009-04-21 22:30:27 +0000718 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200719 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +0000720 }
721 if (get_param_value(option, 128, "cpus", optarg) == 0) {
722 node_cpumask[nodenr] = 0;
723 } else {
724 value = strtoull(option, &endptr, 10);
725 if (value >= 64) {
726 value = 63;
727 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
728 } else {
729 if (*endptr == '-') {
730 endvalue = strtoull(endptr+1, &endptr, 10);
731 if (endvalue >= 63) {
732 endvalue = 62;
733 fprintf(stderr,
734 "only 63 CPUs in NUMA mode supported.\n");
735 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100736 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000737 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100738 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000739 }
740 }
741 node_cpumask[nodenr] = value;
742 }
743 nb_numa_nodes++;
744 }
745 return;
746}
747
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200748static void smp_parse(const char *optarg)
749{
750 int smp, sockets = 0, threads = 0, cores = 0;
751 char *endptr;
752 char option[128];
753
754 smp = strtoul(optarg, &endptr, 10);
755 if (endptr != optarg) {
756 if (*endptr == ',') {
757 endptr++;
758 }
759 }
760 if (get_param_value(option, 128, "sockets", endptr) != 0)
761 sockets = strtoull(option, NULL, 10);
762 if (get_param_value(option, 128, "cores", endptr) != 0)
763 cores = strtoull(option, NULL, 10);
764 if (get_param_value(option, 128, "threads", endptr) != 0)
765 threads = strtoull(option, NULL, 10);
766 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
767 max_cpus = strtoull(option, NULL, 10);
768
769 /* compute missing values, prefer sockets over cores over threads */
770 if (smp == 0 || sockets == 0) {
771 sockets = sockets > 0 ? sockets : 1;
772 cores = cores > 0 ? cores : 1;
773 threads = threads > 0 ? threads : 1;
774 if (smp == 0) {
775 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200776 }
777 } else {
778 if (cores == 0) {
779 threads = threads > 0 ? threads : 1;
780 cores = smp / (sockets * threads);
781 } else {
Joel Schoppdca98162010-07-21 15:05:17 -0500782 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200783 }
784 }
785 smp_cpus = smp;
786 smp_cores = cores > 0 ? cores : 1;
787 smp_threads = threads > 0 ? threads : 1;
788 if (max_cpus == 0)
789 max_cpus = smp_cpus;
790}
791
bellard330d0412003-07-26 18:11:40 +0000792/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +0000793/* USB devices */
794
Markus Armbrusterfb080002010-05-28 15:38:44 +0200795static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +0000796{
797 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200798 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +0000799
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200800 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +0000801 return -1;
802
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +0100803 /* drivers with .usbdevice_name entry in USBDeviceInfo */
804 dev = usbdevice_create(devname);
805 if (dev)
806 goto done;
807
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200808 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +0000809 if (strstart(devname, "host:", &p)) {
810 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +0000811 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
812 dev = usb_bt_init(devname[2] ? hci_init(p) :
813 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +0000814 } else {
815 return -1;
816 }
pbrook0d92ed32006-05-21 16:30:15 +0000817 if (!dev)
818 return -1;
819
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200820done:
bellarda594cfb2005-11-06 16:13:29 +0000821 return 0;
822}
823
aliguori1f3870a2008-08-21 19:27:48 +0000824static int usb_device_del(const char *devname)
825{
826 int bus_num, addr;
827 const char *p;
828
aliguori5d0c5752008-09-14 01:07:41 +0000829 if (strstart(devname, "host:", &p))
830 return usb_host_device_close(p);
831
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200832 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +0000833 return -1;
834
835 p = strchr(devname, '.');
836 if (!p)
837 return -1;
838 bus_num = strtoul(devname, NULL, 0);
839 addr = strtoul(p + 1, NULL, 0);
840
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200841 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000842}
843
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200844static int usb_parse(const char *cmdline)
845{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800846 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +0200847 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800848 if (r < 0) {
849 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
850 }
851 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200852}
853
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300854void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000855{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800856 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +0200857 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100858 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800859 }
bellarda594cfb2005-11-06 16:13:29 +0000860}
861
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300862void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000863{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800864 const char *devname = qdict_get_str(qdict, "devname");
865 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100866 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800867 }
bellarda594cfb2005-11-06 16:13:29 +0000868}
869
bellardf7cce892004-12-08 22:21:25 +0000870/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +0000871/* PCMCIA/Cardbus */
872
873static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +0100874 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +0000875 struct pcmcia_socket_entry_s *next;
876} *pcmcia_sockets = 0;
877
Paul Brookbc24a222009-05-10 01:44:56 +0100878void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000879{
880 struct pcmcia_socket_entry_s *entry;
881
882 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
883 entry->socket = socket;
884 entry->next = pcmcia_sockets;
885 pcmcia_sockets = entry;
886}
887
Paul Brookbc24a222009-05-10 01:44:56 +0100888void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +0000889{
890 struct pcmcia_socket_entry_s *entry, **ptr;
891
892 ptr = &pcmcia_sockets;
893 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
894 if (entry->socket == socket) {
895 *ptr = entry->next;
896 qemu_free(entry);
897 }
898}
899
aliguori376253e2009-03-05 23:01:23 +0000900void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +0000901{
902 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +0000903
balrog201a51f2007-04-30 00:51:09 +0000904 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +0000905 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +0000906
907 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +0000908 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
909 iter->socket->attached ? iter->socket->card_string :
910 "Empty");
balrog201a51f2007-04-30 00:51:09 +0000911}
912
913/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +0000914/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +0000915
bellardc4b1fcc2004-03-14 21:44:30 +0000916typedef struct IOHandlerRecord {
917 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +0100918 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +0000919 IOHandler *fd_read;
920 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +0000921 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +0000922 void *opaque;
923 /* temporary data */
924 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +0100925 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +0000926} IOHandlerRecord;
927
Juan Quintela31d4ee62010-03-11 17:55:37 +0100928static QLIST_HEAD(, IOHandlerRecord) io_handlers =
929 QLIST_HEAD_INITIALIZER(io_handlers);
930
bellardc4b1fcc2004-03-14 21:44:30 +0000931
bellard7c9d8e02005-11-15 22:16:05 +0000932/* XXX: fd_read_poll should be suppressed, but an API change is
933 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +0000934int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +0100935 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +0000936 IOHandler *fd_read,
937 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +0000938 void *opaque)
bellardb4608c02003-06-27 17:34:32 +0000939{
Juan Quintela31d4ee62010-03-11 17:55:37 +0100940 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +0000941
bellard7c9d8e02005-11-15 22:16:05 +0000942 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100943 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000944 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +0000945 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +0000946 break;
947 }
bellard8a7ddc32004-03-31 19:00:16 +0000948 }
bellard7c9d8e02005-11-15 22:16:05 +0000949 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +0100950 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +0000951 if (ioh->fd == fd)
952 goto found;
953 }
954 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +0100955 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +0000956 found:
957 ioh->fd = fd;
958 ioh->fd_read_poll = fd_read_poll;
959 ioh->fd_read = fd_read;
960 ioh->fd_write = fd_write;
961 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +0000962 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +0000963 }
bellard7c9d8e02005-11-15 22:16:05 +0000964 return 0;
965}
966
ths5fafdf22007-09-16 21:08:06 +0000967int qemu_set_fd_handler(int fd,
968 IOHandler *fd_read,
969 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +0000970 void *opaque)
971{
972 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +0000973}
974
bellard8a7ddc32004-03-31 19:00:16 +0000975/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +0000976/* machine registration */
977
blueswir1bdaf78e2008-10-04 07:24:27 +0000978static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +0000979QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +0000980
981int qemu_register_machine(QEMUMachine *m)
982{
983 QEMUMachine **pm;
984 pm = &first_machine;
985 while (*pm != NULL)
986 pm = &(*pm)->next;
987 m->next = NULL;
988 *pm = m;
989 return 0;
990}
991
pbrook9596ebb2007-11-18 01:44:38 +0000992static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +0000993{
994 QEMUMachine *m;
995
996 for(m = first_machine; m != NULL; m = m->next) {
997 if (!strcmp(m->name, name))
998 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +0100999 if (m->alias && !strcmp(m->alias, name))
1000 return m;
bellardcc1daa42005-06-05 14:49:17 +00001001 }
1002 return NULL;
1003}
1004
Anthony Liguori0c257432009-05-21 20:41:01 -05001005static QEMUMachine *find_default_machine(void)
1006{
1007 QEMUMachine *m;
1008
1009 for(m = first_machine; m != NULL; m = m->next) {
1010 if (m->is_default) {
1011 return m;
1012 }
1013 }
1014 return NULL;
1015}
1016
bellardcc1daa42005-06-05 14:49:17 +00001017/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001018/* main execution loop */
1019
pbrook9596ebb2007-11-18 01:44:38 +00001020static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001021{
aliguori7d957bd2009-01-15 22:14:11 +00001022 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001023 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001024 DisplayChangeListener *dcl = ds->listeners;
1025
Sheng Yang62a27442010-01-26 19:21:16 +08001026 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001027 dpy_refresh(ds);
1028
1029 while (dcl != NULL) {
1030 if (dcl->gui_timer_interval &&
1031 dcl->gui_timer_interval < interval)
1032 interval = dcl->gui_timer_interval;
1033 dcl = dcl->next;
1034 }
1035 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001036}
1037
blueswir19043b622009-01-21 19:28:13 +00001038static void nographic_update(void *opaque)
1039{
1040 uint64_t interval = GUI_REFRESH_INTERVAL;
1041
Sheng Yang62a27442010-01-26 19:21:16 +08001042 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00001043 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1044}
1045
bellard0bd48852005-11-11 00:00:47 +00001046struct vm_change_state_entry {
1047 VMChangeStateHandler *cb;
1048 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001049 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001050};
1051
Blue Swirl72cf2d42009-09-12 07:36:22 +00001052static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001053
1054VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1055 void *opaque)
1056{
1057 VMChangeStateEntry *e;
1058
1059 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001060
1061 e->cb = cb;
1062 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001063 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001064 return e;
1065}
1066
1067void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1068{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001069 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001070 qemu_free (e);
1071}
1072
Blue Swirl296af7c2010-03-29 19:23:50 +00001073void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001074{
1075 VMChangeStateEntry *e;
1076
1077 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001078 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001079 }
1080}
1081
bellard8a7ddc32004-03-31 19:00:16 +00001082void vm_start(void)
1083{
1084 if (!vm_running) {
1085 cpu_enable_ticks();
1086 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001087 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001088 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001089 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001090 }
1091}
1092
bellardbb0c6722004-06-20 12:37:32 +00001093/* reset/shutdown handler */
1094
1095typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001096 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001097 QEMUResetHandler *func;
1098 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001099} QEMUResetEntry;
1100
Blue Swirl72cf2d42009-09-12 07:36:22 +00001101static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1102 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001103static int reset_requested;
1104static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00001105static int powerdown_requested;
Blue Swirl296af7c2010-03-29 19:23:50 +00001106int debug_requested;
Blue Swirl7277e022010-04-12 17:19:06 +00001107int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001108
aurel32cf7a2fe2008-03-18 06:53:05 +00001109int qemu_shutdown_requested(void)
1110{
1111 int r = shutdown_requested;
1112 shutdown_requested = 0;
1113 return r;
1114}
1115
1116int qemu_reset_requested(void)
1117{
1118 int r = reset_requested;
1119 reset_requested = 0;
1120 return r;
1121}
1122
1123int qemu_powerdown_requested(void)
1124{
1125 int r = powerdown_requested;
1126 powerdown_requested = 0;
1127 return r;
1128}
1129
aliguorie5689022009-04-24 18:03:54 +00001130static int qemu_debug_requested(void)
1131{
1132 int r = debug_requested;
1133 debug_requested = 0;
1134 return r;
1135}
1136
aliguori6e29f5d2009-04-24 18:04:02 +00001137static int qemu_vmstop_requested(void)
1138{
1139 int r = vmstop_requested;
1140 vmstop_requested = 0;
1141 return r;
1142}
1143
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001144void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001145{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001146 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001147
bellardbb0c6722004-06-20 12:37:32 +00001148 re->func = func;
1149 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001150 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001151}
1152
Jan Kiszkadda9b292009-07-02 00:19:02 +02001153void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001154{
1155 QEMUResetEntry *re;
1156
Blue Swirl72cf2d42009-09-12 07:36:22 +00001157 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001158 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001159 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001160 qemu_free(re);
1161 return;
1162 }
1163 }
1164}
1165
1166void qemu_system_reset(void)
1167{
1168 QEMUResetEntry *re, *nre;
1169
1170 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001171 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001172 re->func(re->opaque);
1173 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001174 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001175 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001176}
1177
1178void qemu_system_reset_request(void)
1179{
bellardd1beab82006-10-02 19:44:22 +00001180 if (no_reboot) {
1181 shutdown_requested = 1;
1182 } else {
1183 reset_requested = 1;
1184 }
aliguorid9f75a42009-04-24 18:03:11 +00001185 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001186}
1187
1188void qemu_system_shutdown_request(void)
1189{
1190 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001191 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001192}
1193
bellard34751872005-07-02 14:31:34 +00001194void qemu_system_powerdown_request(void)
1195{
1196 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001197 qemu_notify_event();
1198}
1199
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001200void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001201{
1202 IOHandlerRecord *ioh;
1203 fd_set rfds, wfds, xfds;
1204 int ret, nfds;
1205 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001206 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001207
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001208 if (nonblocking)
1209 timeout = 0;
1210 else {
1211 timeout = qemu_calculate_timeout();
1212 qemu_bh_update_timeout(&timeout);
1213 }
aliguori56f3a5d2008-10-31 18:07:17 +00001214
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001215 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001216
bellardfd1dff42006-02-01 21:29:26 +00001217 /* poll any events */
1218 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001219 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001220 FD_ZERO(&rfds);
1221 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001222 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001223 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00001224 if (ioh->deleted)
1225 continue;
bellardfd1dff42006-02-01 21:29:26 +00001226 if (ioh->fd_read &&
1227 (!ioh->fd_read_poll ||
1228 ioh->fd_read_poll(ioh->opaque) != 0)) {
1229 FD_SET(ioh->fd, &rfds);
1230 if (ioh->fd > nfds)
1231 nfds = ioh->fd;
1232 }
1233 if (ioh->fd_write) {
1234 FD_SET(ioh->fd, &wfds);
1235 if (ioh->fd > nfds)
1236 nfds = ioh->fd;
1237 }
1238 }
ths3b46e622007-09-17 08:09:54 +00001239
aliguori56f3a5d2008-10-31 18:07:17 +00001240 tv.tv_sec = timeout / 1000;
1241 tv.tv_usec = (timeout % 1000) * 1000;
1242
Jan Kiszkad918f232009-06-24 14:42:30 +02001243 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1244
aliguori48708522009-04-24 18:03:49 +00001245 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001246 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001247 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00001248 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001249 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00001250
Juan Quintela31d4ee62010-03-11 17:55:37 +01001251 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
thscafffd42007-02-28 21:59:44 +00001252 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001253 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00001254 qemu_free(ioh);
Juan Quintela4bed9832010-03-11 17:55:41 +01001255 continue;
1256 }
1257 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1258 ioh->fd_read(ioh->opaque);
1259 }
1260 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1261 ioh->fd_write(ioh->opaque);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001262 }
thscafffd42007-02-28 21:59:44 +00001263 }
bellardfd1dff42006-02-01 21:29:26 +00001264 }
Jan Kiszkad918f232009-06-24 14:42:30 +02001265
1266 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001267
Paolo Bonzinib6964822010-03-10 11:38:45 +01001268 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001269
pbrook423f0742007-05-23 00:06:54 +00001270 /* Check bottom-halves last in case any of the earlier events triggered
1271 them. */
1272 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001273
bellard5905b2e2004-08-01 21:53:26 +00001274}
1275
aliguori43b96852009-04-24 18:03:33 +00001276static int vm_can_run(void)
1277{
1278 if (powerdown_requested)
1279 return 0;
1280 if (reset_requested)
1281 return 0;
1282 if (shutdown_requested)
1283 return 0;
aliguorie5689022009-04-24 18:03:54 +00001284 if (debug_requested)
1285 return 0;
aliguori43b96852009-04-24 18:03:33 +00001286 return 1;
1287}
1288
Blue Swirld9c32312009-08-09 08:42:19 +00001289qemu_irq qemu_system_powerdown;
1290
aliguori43b96852009-04-24 18:03:33 +00001291static void main_loop(void)
1292{
aliguori6e29f5d2009-04-24 18:04:02 +00001293 int r;
aliguori43b96852009-04-24 18:03:33 +00001294
Blue Swirl7277e022010-04-12 17:19:06 +00001295 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001296
aliguori6e29f5d2009-04-24 18:04:02 +00001297 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00001298 do {
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001299 bool nonblocking = false;
aliguorie6e35b12009-04-24 18:03:57 +00001300#ifdef CONFIG_PROFILER
1301 int64_t ti;
1302#endif
aliguorid6dc3d42009-04-24 18:04:07 +00001303#ifndef CONFIG_IOTHREAD
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001304 nonblocking = cpu_exec_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001305#endif
aliguori43b96852009-04-24 18:03:33 +00001306#ifdef CONFIG_PROFILER
1307 ti = profile_getclock();
1308#endif
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001309 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001310#ifdef CONFIG_PROFILER
1311 dev_time += profile_getclock() - ti;
1312#endif
aliguorie5689022009-04-24 18:03:54 +00001313 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00001314
Blue Swirl54fc6ea2010-03-29 19:23:46 +00001315 if ((r = qemu_debug_requested())) {
1316 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001317 }
aliguori43b96852009-04-24 18:03:33 +00001318 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001319 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001320 if (no_shutdown) {
1321 vm_stop(0);
1322 no_shutdown = 0;
1323 } else
1324 break;
1325 }
aliguorid6dc3d42009-04-24 18:04:07 +00001326 if (qemu_reset_requested()) {
1327 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00001328 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00001329 resume_all_vcpus();
1330 }
Blue Swirld9c32312009-08-09 08:42:19 +00001331 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001332 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001333 qemu_irq_raise(qemu_system_powerdown);
1334 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001335 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001336 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001337 }
aliguori43b96852009-04-24 18:03:33 +00001338 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001339 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001340 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001341}
1342
pbrook9bd7e6d2009-04-07 22:58:45 +00001343static void version(void)
1344{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001345 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001346}
1347
ths15f82202007-06-29 23:26:08 +00001348static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001349{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001350 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001351#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1352 opt_help
blueswir15824d652009-03-28 06:44:27 +00001353#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001354#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001355#undef DEF
1356#undef DEFHEADING
1357#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001358 ;
1359 version();
1360 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001361 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001362 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001363 "\n"
1364 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001365 "During emulation, the following keys are useful:\n"
1366 "ctrl-alt-f toggle full screen\n"
1367 "ctrl-alt-n switch to virtual console 'n'\n"
1368 "ctrl-alt toggle mouse and keyboard grab\n"
1369 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001370 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1371 "qemu",
1372 options_help);
ths15f82202007-06-29 23:26:08 +00001373 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001374}
1375
bellardcd6f1162004-05-13 22:02:20 +00001376#define HAS_ARG 0x0001
1377
bellardcd6f1162004-05-13 22:02:20 +00001378typedef struct QEMUOption {
1379 const char *name;
1380 int flags;
1381 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001382 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001383} QEMUOption;
1384
blueswir1dbed7e42008-10-01 19:38:09 +00001385static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001386 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1387#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1388 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001389#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001390#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001391#undef DEF
1392#undef DEFHEADING
1393#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001394 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001395};
malc3893c122008-09-28 00:42:05 +00001396static void select_vgahw (const char *p)
1397{
1398 const char *opts;
1399
Gerd Hoffmann64465292009-12-08 13:11:45 +01001400 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001401 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001402 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001403 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001404 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001405 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001406 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001407 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001408 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001409 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00001410 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001411 invalid_vga:
1412 fprintf(stderr, "Unknown vga type: %s\n", p);
1413 exit(1);
1414 }
malccb5a7aa2008-09-28 00:42:12 +00001415 while (*opts) {
1416 const char *nextopt;
1417
1418 if (strstart(opts, ",retrace=", &nextopt)) {
1419 opts = nextopt;
1420 if (strstart(opts, "dumb", &nextopt))
1421 vga_retrace_method = VGA_RETRACE_DUMB;
1422 else if (strstart(opts, "precise", &nextopt))
1423 vga_retrace_method = VGA_RETRACE_PRECISE;
1424 else goto invalid_vga;
1425 } else goto invalid_vga;
1426 opts = nextopt;
1427 }
malc3893c122008-09-28 00:42:05 +00001428}
1429
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001430static int balloon_parse(const char *arg)
1431{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001432 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001433
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001434 if (strcmp(arg, "none") == 0) {
1435 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001436 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001437
1438 if (!strncmp(arg, "virtio", 6)) {
1439 if (arg[6] == ',') {
1440 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001441 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001442 if (!opts)
1443 return -1;
1444 } else {
1445 /* create empty opts */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001446 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001447 }
1448 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1449 return 0;
1450 }
1451
1452 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001453}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001454
Paul Brook5cea8592009-05-30 00:52:44 +01001455char *qemu_find_file(int type, const char *name)
1456{
1457 int len;
1458 const char *subdir;
1459 char *buf;
1460
1461 /* If name contains path separators then try it as a straight path. */
1462 if ((strchr(name, '/') || strchr(name, '\\'))
1463 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02001464 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001465 }
1466 switch (type) {
1467 case QEMU_FILE_TYPE_BIOS:
1468 subdir = "";
1469 break;
1470 case QEMU_FILE_TYPE_KEYMAP:
1471 subdir = "keymaps/";
1472 break;
1473 default:
1474 abort();
1475 }
1476 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1477 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001478 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001479 if (access(buf, R_OK)) {
1480 qemu_free(buf);
1481 return NULL;
1482 }
1483 return buf;
1484}
1485
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001486static int device_help_func(QemuOpts *opts, void *opaque)
1487{
1488 return qdev_device_help(opts);
1489}
1490
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001491static int device_init_func(QemuOpts *opts, void *opaque)
1492{
1493 DeviceState *dev;
1494
1495 dev = qdev_device_add(opts);
1496 if (!dev)
1497 return -1;
1498 return 0;
1499}
1500
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001501static int chardev_init_func(QemuOpts *opts, void *opaque)
1502{
1503 CharDriverState *chr;
1504
1505 chr = qemu_chr_open_opts(opts, NULL);
1506 if (!chr)
1507 return -1;
1508 return 0;
1509}
1510
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07001511#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301512static int fsdev_init_func(QemuOpts *opts, void *opaque)
1513{
1514 int ret;
1515 ret = qemu_fsdev_add(opts);
1516
1517 return ret;
1518}
1519#endif
1520
Gerd Hoffmann88589342009-12-08 13:11:50 +01001521static int mon_init_func(QemuOpts *opts, void *opaque)
1522{
1523 CharDriverState *chr;
1524 const char *chardev;
1525 const char *mode;
1526 int flags;
1527
1528 mode = qemu_opt_get(opts, "mode");
1529 if (mode == NULL) {
1530 mode = "readline";
1531 }
1532 if (strcmp(mode, "readline") == 0) {
1533 flags = MONITOR_USE_READLINE;
1534 } else if (strcmp(mode, "control") == 0) {
1535 flags = MONITOR_USE_CONTROL;
1536 } else {
1537 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1538 exit(1);
1539 }
1540
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01001541 if (qemu_opt_get_bool(opts, "pretty", 0))
1542 flags |= MONITOR_USE_PRETTY;
1543
Gerd Hoffmann88589342009-12-08 13:11:50 +01001544 if (qemu_opt_get_bool(opts, "default", 0))
1545 flags |= MONITOR_IS_DEFAULT;
1546
1547 chardev = qemu_opt_get(opts, "chardev");
1548 chr = qemu_chr_find(chardev);
1549 if (chr == NULL) {
1550 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1551 exit(1);
1552 }
1553
1554 monitor_init(chr, flags);
1555 return 0;
1556}
1557
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001558static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001559{
1560 static int monitor_device_index = 0;
1561 QemuOpts *opts;
1562 const char *p;
1563 char label[32];
1564 int def = 0;
1565
1566 if (strstart(optarg, "chardev:", &p)) {
1567 snprintf(label, sizeof(label), "%s", p);
1568 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001569 snprintf(label, sizeof(label), "compat_monitor%d",
1570 monitor_device_index);
1571 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001572 def = 1;
1573 }
1574 opts = qemu_chr_parse_compat(label, optarg);
1575 if (!opts) {
1576 fprintf(stderr, "parse error: %s\n", optarg);
1577 exit(1);
1578 }
1579 }
1580
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001581 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001582 if (!opts) {
1583 fprintf(stderr, "duplicate chardev: %s\n", label);
1584 exit(1);
1585 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001586 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001587 qemu_opt_set(opts, "chardev", label);
1588 if (def)
1589 qemu_opt_set(opts, "default", "on");
1590 monitor_device_index++;
1591}
1592
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001593struct device_config {
1594 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001595 DEV_USB, /* -usbdevice */
1596 DEV_BT, /* -bt */
1597 DEV_SERIAL, /* -serial */
1598 DEV_PARALLEL, /* -parallel */
1599 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001600 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001601 } type;
1602 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001603 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001604};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001605QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001606
1607static void add_device_config(int type, const char *cmdline)
1608{
1609 struct device_config *conf;
1610
1611 conf = qemu_mallocz(sizeof(*conf));
1612 conf->type = type;
1613 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001614 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001615}
1616
1617static int foreach_device_config(int type, int (*func)(const char *cmdline))
1618{
1619 struct device_config *conf;
1620 int rc;
1621
Blue Swirl72cf2d42009-09-12 07:36:22 +00001622 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001623 if (conf->type != type)
1624 continue;
1625 rc = func(conf->cmdline);
1626 if (0 != rc)
1627 return rc;
1628 }
1629 return 0;
1630}
1631
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001632static int serial_parse(const char *devname)
1633{
1634 static int index = 0;
1635 char label[32];
1636
1637 if (strcmp(devname, "none") == 0)
1638 return 0;
1639 if (index == MAX_SERIAL_PORTS) {
1640 fprintf(stderr, "qemu: too many serial ports\n");
1641 exit(1);
1642 }
1643 snprintf(label, sizeof(label), "serial%d", index);
1644 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1645 if (!serial_hds[index]) {
1646 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1647 devname, strerror(errno));
1648 return -1;
1649 }
1650 index++;
1651 return 0;
1652}
1653
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01001654static int parallel_parse(const char *devname)
1655{
1656 static int index = 0;
1657 char label[32];
1658
1659 if (strcmp(devname, "none") == 0)
1660 return 0;
1661 if (index == MAX_PARALLEL_PORTS) {
1662 fprintf(stderr, "qemu: too many parallel ports\n");
1663 exit(1);
1664 }
1665 snprintf(label, sizeof(label), "parallel%d", index);
1666 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1667 if (!parallel_hds[index]) {
1668 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1669 devname, strerror(errno));
1670 return -1;
1671 }
1672 index++;
1673 return 0;
1674}
1675
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001676static int virtcon_parse(const char *devname)
1677{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001678 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001679 static int index = 0;
1680 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05301681 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001682
1683 if (strcmp(devname, "none") == 0)
1684 return 0;
1685 if (index == MAX_VIRTIO_CONSOLES) {
1686 fprintf(stderr, "qemu: too many virtio consoles\n");
1687 exit(1);
1688 }
Amit Shah392ecf52010-01-21 16:19:23 +05301689
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001690 bus_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301691 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1692
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001693 dev_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301694 qemu_opt_set(dev_opts, "driver", "virtconsole");
1695
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001696 snprintf(label, sizeof(label), "virtcon%d", index);
1697 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1698 if (!virtcon_hds[index]) {
1699 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1700 devname, strerror(errno));
1701 return -1;
1702 }
Amit Shah392ecf52010-01-21 16:19:23 +05301703 qemu_opt_set(dev_opts, "chardev", label);
1704
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001705 index++;
1706 return 0;
1707}
1708
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001709static int debugcon_parse(const char *devname)
1710{
1711 QemuOpts *opts;
1712
1713 if (!qemu_chr_open("debugcon", devname, NULL)) {
1714 exit(1);
1715 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001716 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001717 if (!opts) {
1718 fprintf(stderr, "qemu: already have a debugcon device\n");
1719 exit(1);
1720 }
1721 qemu_opt_set(opts, "driver", "isa-debugcon");
1722 qemu_opt_set(opts, "chardev", "debugcon");
1723 return 0;
1724}
1725
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001726void qemu_add_exit_notifier(Notifier *notify)
1727{
1728 notifier_list_add(&exit_notifiers, notify);
1729}
1730
1731void qemu_remove_exit_notifier(Notifier *notify)
1732{
1733 notifier_list_remove(&exit_notifiers, notify);
1734}
1735
1736static void qemu_run_exit_notifiers(void)
1737{
1738 notifier_list_notify(&exit_notifiers);
1739}
1740
Anthony Liguori6530a972010-01-22 09:18:06 -06001741static const QEMUOption *lookup_opt(int argc, char **argv,
1742 const char **poptarg, int *poptind)
1743{
1744 const QEMUOption *popt;
1745 int optind = *poptind;
1746 char *r = argv[optind];
1747 const char *optarg;
1748
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001749 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06001750 optind++;
1751 /* Treat --foo the same as -foo. */
1752 if (r[1] == '-')
1753 r++;
1754 popt = qemu_options;
1755 for(;;) {
1756 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001757 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06001758 exit(1);
1759 }
1760 if (!strcmp(popt->name, r + 1))
1761 break;
1762 popt++;
1763 }
1764 if (popt->flags & HAS_ARG) {
1765 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001766 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06001767 exit(1);
1768 }
1769 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01001770 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06001771 } else {
1772 optarg = NULL;
1773 }
1774
1775 *poptarg = optarg;
1776 *poptind = optind;
1777
1778 return popt;
1779}
1780
malc902b3d52008-12-10 19:18:40 +00001781int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00001782{
aliguori59030a82009-04-05 18:43:41 +00001783 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00001784 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03001785 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01001786 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00001787 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00001788 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05001789 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00001790 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00001791 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00001792 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001793 QemuOpts *hda_opts = NULL, *opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02001794 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00001795 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06001796 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00001797 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001798 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00001799 const char *cpu_model;
bellard26a5f132008-05-28 12:30:31 +00001800 int tb_size;
ths93815bc2007-03-19 15:58:31 +00001801 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00001802 const char *incoming = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05001803 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06001804 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00001805
Prerna Saxenaab6540d2010-08-09 11:48:32 +01001806#ifdef CONFIG_SIMPLE_TRACE
1807 const char *trace_file = NULL;
1808#endif
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001809 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01001810 error_set_progname(argv[0]);
1811
Jan Kiszka68752042009-09-15 13:36:04 +02001812 init_clocks();
1813
malc902b3d52008-12-10 19:18:40 +00001814 qemu_cache_utils_init(envp);
1815
Blue Swirl72cf2d42009-09-12 07:36:22 +00001816 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02001817 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00001818
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05001819 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05001820 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00001821 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00001822 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00001823 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00001824 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00001825 kernel_filename = NULL;
1826 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00001827 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00001828 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00001829
aliguori268a3622009-04-21 22:30:27 +00001830 for (i = 0; i < MAX_NODES; i++) {
1831 node_mem[i] = 0;
1832 node_cpumask[i] = 0;
1833 }
1834
aliguori268a3622009-04-21 22:30:27 +00001835 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00001836 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00001837
bellard26a5f132008-05-28 12:30:31 +00001838 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00001839 autostart= 1;
1840
Anthony Liguori292444c2010-01-21 10:57:58 -06001841 /* first pass of option parsing */
1842 optind = 1;
1843 while (optind < argc) {
1844 if (argv[optind][0] != '-') {
1845 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06001846 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06001847 continue;
1848 } else {
1849 const QEMUOption *popt;
1850
1851 popt = lookup_opt(argc, argv, &optarg, &optind);
1852 switch (popt->index) {
1853 case QEMU_OPTION_nodefconfig:
1854 defconfig=0;
1855 break;
1856 }
1857 }
1858 }
1859
1860 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001861 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01001862
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001863 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02001864 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001865 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001866 }
1867
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001868 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02001869 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01001870 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06001871 }
1872 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00001873 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06001874
1875 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00001876 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00001877 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00001878 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00001879 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06001880 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001881 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00001882 } else {
1883 const QEMUOption *popt;
1884
Anthony Liguori6530a972010-01-22 09:18:06 -06001885 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00001886 if (!(popt->arch_mask & arch_type)) {
1887 printf("Option %s not supported for this target\n", popt->name);
1888 exit(1);
1889 }
bellardcd6f1162004-05-13 22:02:20 +00001890 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00001891 case QEMU_OPTION_M:
1892 machine = find_machine(optarg);
1893 if (!machine) {
1894 QEMUMachine *m;
1895 printf("Supported machines are:\n");
1896 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001897 if (m->alias)
1898 printf("%-10s %s (alias of %s)\n",
1899 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00001900 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00001901 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05001902 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00001903 }
ths15f82202007-06-29 23:26:08 +00001904 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00001905 }
1906 break;
j_mayer94fc95c2007-03-05 19:44:02 +00001907 case QEMU_OPTION_cpu:
1908 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00001909 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00001910 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00001911 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00001912 } else {
1913 cpu_model = optarg;
1914 }
1915 break;
bellardcd6f1162004-05-13 22:02:20 +00001916 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00001917 initrd_filename = optarg;
1918 break;
bellardcd6f1162004-05-13 22:02:20 +00001919 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00001920 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001921 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00001922 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001923 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00001924 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00001925 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00001926 translation == BIOS_ATA_TRANSLATION_LBA ?
1927 ",trans=lba" :
1928 translation == BIOS_ATA_TRANSLATION_NONE ?
1929 ",trans=none" : "");
1930 break;
bellardcd6f1162004-05-13 22:02:20 +00001931 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00001932 case QEMU_OPTION_hdc:
1933 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00001934 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00001935 break;
thse4bcb142007-12-02 04:51:10 +00001936 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00001937 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00001938 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02001939 case QEMU_OPTION_set:
1940 if (qemu_set_option(optarg) != 0)
1941 exit(1);
1942 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01001943 case QEMU_OPTION_global:
1944 if (qemu_global_option(optarg) != 0)
1945 exit(1);
1946 break;
balrog3e3d5812007-04-30 02:09:25 +00001947 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00001948 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00001949 break;
pbrooka1bb27b2007-04-06 16:49:48 +00001950 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00001951 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00001952 break;
j_mayer86f55662007-04-24 06:52:59 +00001953 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00001954 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00001955 break;
bellardcd6f1162004-05-13 22:02:20 +00001956 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00001957 snapshot = 1;
1958 break;
bellardcd6f1162004-05-13 22:02:20 +00001959 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00001960 {
bellard330d0412003-07-26 18:11:40 +00001961 const char *p;
1962 p = optarg;
1963 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001964 if (cyls < 1 || cyls > 16383)
1965 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00001966 if (*p != ',')
1967 goto chs_fail;
1968 p++;
1969 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001970 if (heads < 1 || heads > 16)
1971 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00001972 if (*p != ',')
1973 goto chs_fail;
1974 p++;
1975 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00001976 if (secs < 1 || secs > 63)
1977 goto chs_fail;
1978 if (*p == ',') {
1979 p++;
1980 if (!strcmp(p, "none"))
1981 translation = BIOS_ATA_TRANSLATION_NONE;
1982 else if (!strcmp(p, "lba"))
1983 translation = BIOS_ATA_TRANSLATION_LBA;
1984 else if (!strcmp(p, "auto"))
1985 translation = BIOS_ATA_TRANSLATION_AUTO;
1986 else
1987 goto chs_fail;
1988 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00001989 chs_fail:
bellard46d47672004-11-16 01:45:27 +00001990 fprintf(stderr, "qemu: invalid physical CHS format\n");
1991 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00001992 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001993 if (hda_opts != NULL) {
1994 char num[16];
1995 snprintf(num, sizeof(num), "%d", cyls);
1996 qemu_opt_set(hda_opts, "cyls", num);
1997 snprintf(num, sizeof(num), "%d", heads);
1998 qemu_opt_set(hda_opts, "heads", num);
1999 snprintf(num, sizeof(num), "%d", secs);
2000 qemu_opt_set(hda_opts, "secs", num);
2001 if (translation == BIOS_ATA_TRANSLATION_LBA)
2002 qemu_opt_set(hda_opts, "trans", "lba");
2003 if (translation == BIOS_ATA_TRANSLATION_NONE)
2004 qemu_opt_set(hda_opts, "trans", "none");
2005 }
bellard330d0412003-07-26 18:11:40 +00002006 }
2007 break;
aliguori268a3622009-04-21 22:30:27 +00002008 case QEMU_OPTION_numa:
2009 if (nb_numa_nodes >= MAX_NODES) {
2010 fprintf(stderr, "qemu: too many NUMA nodes\n");
2011 exit(1);
2012 }
2013 numa_add(optarg);
2014 break;
bellardcd6f1162004-05-13 22:02:20 +00002015 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002016 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002017 break;
balrog4d3b6f62008-02-10 16:33:14 +00002018#ifdef CONFIG_CURSES
2019 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002020 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00002021 break;
2022#endif
balroga171fe32007-04-30 01:48:07 +00002023 case QEMU_OPTION_portrait:
2024 graphic_rotate = 1;
2025 break;
bellardcd6f1162004-05-13 22:02:20 +00002026 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002027 kernel_filename = optarg;
2028 break;
bellardcd6f1162004-05-13 22:02:20 +00002029 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002030 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002031 break;
bellardcd6f1162004-05-13 22:02:20 +00002032 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00002033 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00002034 break;
bellardcd6f1162004-05-13 22:02:20 +00002035 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002036 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002037 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002038 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002039 };
2040 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002041 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002042 int legacy = 0;
2043
2044 if (!strchr(optarg, '=')) {
2045 legacy = 1;
2046 pstrcpy(buf, sizeof(buf), optarg);
2047 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2048 fprintf(stderr,
2049 "qemu: unknown boot parameter '%s' in '%s'\n",
2050 buf, optarg);
2051 exit(1);
2052 }
2053
2054 if (legacy ||
2055 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002056 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002057 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002058 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002059 if (!legacy) {
2060 if (get_param_value(buf, sizeof(buf),
2061 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002062 validate_bootdevices(buf);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002063 standard_boot_devices = qemu_strdup(boot_devices);
2064 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2065 qemu_register_reset(restore_boot_devices,
2066 standard_boot_devices);
2067 }
Jan Kiszka95387492009-07-02 00:19:02 +02002068 if (get_param_value(buf, sizeof(buf),
2069 "menu", optarg)) {
2070 if (!strcmp(buf, "on")) {
2071 boot_menu = 1;
2072 } else if (!strcmp(buf, "off")) {
2073 boot_menu = 0;
2074 } else {
2075 fprintf(stderr,
2076 "qemu: invalid option value '%s'\n",
2077 buf);
2078 exit(1);
2079 }
2080 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002081 }
bellard36b486b2003-11-11 13:36:08 +00002082 }
2083 break;
bellardcd6f1162004-05-13 22:02:20 +00002084 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002085 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00002086 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00002087 break;
bellard52ca8d62006-06-14 16:03:05 +00002088 case QEMU_OPTION_no_fd_bootchk:
2089 fd_bootchk = 0;
2090 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002091 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002092 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002093 exit(1);
2094 }
2095 break;
bellard7c9d8e02005-11-15 22:16:05 +00002096 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002097 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002098 exit(1);
2099 }
bellard702c6512004-04-02 21:21:32 +00002100 break;
bellardc7f74642004-08-24 21:57:12 +00002101#ifdef CONFIG_SLIRP
2102 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002103 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002104 break;
ths47d5d012007-02-20 00:05:08 +00002105 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002106 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002107 break;
bellard9bf05442004-08-25 22:12:49 +00002108 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002109 if (net_slirp_redir(optarg) < 0)
2110 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002111 break;
bellardc7f74642004-08-24 21:57:12 +00002112#endif
balrogdc72ac12008-11-09 00:04:26 +00002113 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002114 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002115 break;
bellard1d14ffa2005-10-30 18:58:22 +00002116 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002117 if (!(audio_available())) {
2118 printf("Option %s not supported for this target\n", popt->name);
2119 exit(1);
2120 }
bellard1d14ffa2005-10-30 18:58:22 +00002121 AUD_help ();
2122 exit (0);
2123 break;
2124 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002125 if (!(audio_available())) {
2126 printf("Option %s not supported for this target\n", popt->name);
2127 exit(1);
2128 }
bellard1d14ffa2005-10-30 18:58:22 +00002129 select_soundhw (optarg);
2130 break;
bellardcd6f1162004-05-13 22:02:20 +00002131 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002132 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002133 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002134 case QEMU_OPTION_version:
2135 version();
2136 exit(0);
2137 break;
aurel3200f82b82008-04-27 21:12:55 +00002138 case QEMU_OPTION_m: {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002139 ssize_t value;
aurel3200f82b82008-04-27 21:12:55 +00002140
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002141 value = strtosz(optarg, NULL);
2142 if (value < 0) {
aurel3200f82b82008-04-27 21:12:55 +00002143 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002144 exit(1);
2145 }
aurel3200f82b82008-04-27 21:12:55 +00002146
2147 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002148 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00002149 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2150 exit(1);
2151 }
Anthony Liguoric227f092009-10-01 16:12:16 -05002152 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002153 fprintf(stderr, "qemu: ram size too large\n");
2154 exit(1);
2155 }
2156 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002157 break;
aurel3200f82b82008-04-27 21:12:55 +00002158 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002159 case QEMU_OPTION_mempath:
2160 mem_path = optarg;
2161 break;
2162#ifdef MAP_POPULATE
2163 case QEMU_OPTION_mem_prealloc:
2164 mem_prealloc = 1;
2165 break;
2166#endif
bellardcd6f1162004-05-13 22:02:20 +00002167 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002168 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00002169 break;
bellardcd6f1162004-05-13 22:02:20 +00002170 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002171 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002172 break;
aliguori59030a82009-04-05 18:43:41 +00002173 case QEMU_OPTION_gdb:
2174 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002175 break;
bellardcd6f1162004-05-13 22:02:20 +00002176 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002177 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002178 break;
j_mayer1192dad2007-10-05 13:08:35 +00002179 case QEMU_OPTION_bios:
2180 bios_name = optarg;
2181 break;
aurel321b530a62009-04-05 20:08:59 +00002182 case QEMU_OPTION_singlestep:
2183 singlestep = 1;
2184 break;
bellardcd6f1162004-05-13 22:02:20 +00002185 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002186 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002187 break;
bellard3d11d0e2004-12-12 16:56:30 +00002188 case QEMU_OPTION_k:
2189 keyboard_layout = optarg;
2190 break;
bellardee22c2f2004-06-03 12:49:50 +00002191 case QEMU_OPTION_localtime:
2192 rtc_utc = 0;
2193 break;
malc3893c122008-09-28 00:42:05 +00002194 case QEMU_OPTION_vga:
2195 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002196 break;
bellarde9b137c2004-06-21 16:46:10 +00002197 case QEMU_OPTION_g:
2198 {
2199 const char *p;
2200 int w, h, depth;
2201 p = optarg;
2202 w = strtol(p, (char **)&p, 10);
2203 if (w <= 0) {
2204 graphic_error:
2205 fprintf(stderr, "qemu: invalid resolution or depth\n");
2206 exit(1);
2207 }
2208 if (*p != 'x')
2209 goto graphic_error;
2210 p++;
2211 h = strtol(p, (char **)&p, 10);
2212 if (h <= 0)
2213 goto graphic_error;
2214 if (*p == 'x') {
2215 p++;
2216 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002217 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002218 depth != 24 && depth != 32)
2219 goto graphic_error;
2220 } else if (*p == '\0') {
2221 depth = graphic_depth;
2222 } else {
2223 goto graphic_error;
2224 }
ths3b46e622007-09-17 08:09:54 +00002225
bellarde9b137c2004-06-21 16:46:10 +00002226 graphic_width = w;
2227 graphic_height = h;
2228 graphic_depth = depth;
2229 }
2230 break;
ths20d8a3e2007-02-18 17:04:49 +00002231 case QEMU_OPTION_echr:
2232 {
2233 char *r;
2234 term_escape_char = strtol(optarg, &r, 0);
2235 if (r == optarg)
2236 printf("Bad argument to echr\n");
2237 break;
2238 }
bellard82c643f2004-07-14 17:28:13 +00002239 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002240 monitor_parse(optarg, "readline");
2241 default_monitor = 0;
2242 break;
2243 case QEMU_OPTION_qmp:
2244 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002245 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002246 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002247 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002248 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002249 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002250 exit(1);
2251 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002252 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002253 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002254 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002255 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002256 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002257 exit(1);
2258 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002259 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302260 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002261 olist = qemu_find_opts("fsdev");
2262 if (!olist) {
2263 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2264 exit(1);
2265 }
2266 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302267 if (!opts) {
2268 fprintf(stderr, "parse error: %s\n", optarg);
2269 exit(1);
2270 }
2271 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302272 case QEMU_OPTION_virtfs: {
2273 char *arg_fsdev = NULL;
2274 char *arg_9p = NULL;
2275 int len = 0;
2276
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002277 olist = qemu_find_opts("virtfs");
2278 if (!olist) {
2279 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2280 exit(1);
2281 }
2282 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302283 if (!opts) {
2284 fprintf(stderr, "parse error: %s\n", optarg);
2285 exit(1);
2286 }
2287
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002288 if (qemu_opt_get(opts, "fstype") == NULL ||
2289 qemu_opt_get(opts, "mount_tag") == NULL ||
2290 qemu_opt_get(opts, "path") == NULL ||
2291 qemu_opt_get(opts, "security_model") == NULL) {
2292 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +05302293 "security_model=[mapped|passthrough|none],"
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002294 "mnt_tag=tag.\n");
2295 exit(1);
2296 }
2297
2298 len = strlen(",id=,path=,security_model=");
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302299 len += strlen(qemu_opt_get(opts, "fstype"));
2300 len += strlen(qemu_opt_get(opts, "mount_tag"));
2301 len += strlen(qemu_opt_get(opts, "path"));
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002302 len += strlen(qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302303 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2304
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302305 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002306 "%s,id=%s,path=%s,security_model=%s",
2307 qemu_opt_get(opts, "fstype"),
2308 qemu_opt_get(opts, "mount_tag"),
2309 qemu_opt_get(opts, "path"),
2310 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302311
2312 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2313 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2314 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2315
Aneesh Kumar K.Vc93031e2010-09-02 21:47:59 +05302316 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
Blue Swirlcb93bbd2010-08-31 20:16:59 +00002317 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2318 qemu_opt_get(opts, "mount_tag"),
2319 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302320
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002321 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302322 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2323 exit(1);
2324 }
2325
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002326 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302327 fprintf(stderr, "parse error [device]: %s\n", optarg);
2328 exit(1);
2329 }
2330
2331 qemu_free(arg_fsdev);
2332 qemu_free(arg_9p);
2333 break;
2334 }
bellard82c643f2004-07-14 17:28:13 +00002335 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002336 add_device_config(DEV_SERIAL, optarg);
2337 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002338 if (strncmp(optarg, "mon:", 4) == 0) {
2339 default_monitor = 0;
2340 }
bellard82c643f2004-07-14 17:28:13 +00002341 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002342 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002343 if (watchdog) {
2344 fprintf(stderr,
2345 "qemu: only one watchdog option may be given\n");
2346 return 1;
2347 }
2348 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002349 break;
2350 case QEMU_OPTION_watchdog_action:
2351 if (select_watchdog_action(optarg) == -1) {
2352 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2353 exit(1);
2354 }
2355 break;
aliguori51ecf132009-01-15 20:06:40 +00002356 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002357 add_device_config(DEV_VIRTCON, optarg);
2358 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002359 if (strncmp(optarg, "mon:", 4) == 0) {
2360 default_monitor = 0;
2361 }
aliguori51ecf132009-01-15 20:06:40 +00002362 break;
bellard6508fe52005-01-15 12:02:56 +00002363 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002364 add_device_config(DEV_PARALLEL, optarg);
2365 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002366 if (strncmp(optarg, "mon:", 4) == 0) {
2367 default_monitor = 0;
2368 }
bellard6508fe52005-01-15 12:02:56 +00002369 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002370 case QEMU_OPTION_debugcon:
2371 add_device_config(DEV_DEBUGCON, optarg);
2372 break;
bellardd63d3072004-10-03 13:29:03 +00002373 case QEMU_OPTION_loadvm:
2374 loadvm = optarg;
2375 break;
2376 case QEMU_OPTION_full_screen:
2377 full_screen = 1;
2378 break;
ths667acca2006-12-11 02:08:05 +00002379#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002380 case QEMU_OPTION_no_frame:
2381 no_frame = 1;
2382 break;
ths3780e192007-06-21 21:08:02 +00002383 case QEMU_OPTION_alt_grab:
2384 alt_grab = 1;
2385 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002386 case QEMU_OPTION_ctrl_grab:
2387 ctrl_grab = 1;
2388 break;
ths667acca2006-12-11 02:08:05 +00002389 case QEMU_OPTION_no_quit:
2390 no_quit = 1;
2391 break;
aliguori7d957bd2009-01-15 22:14:11 +00002392 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002393 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002394 break;
ths667acca2006-12-11 02:08:05 +00002395#endif
bellardf7cce892004-12-08 22:21:25 +00002396 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002397 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002398 break;
bellarda09db212005-04-30 16:10:35 +00002399 case QEMU_OPTION_win2k_hack:
2400 win2k_install_hack = 1;
2401 break;
aliguori73822ec2009-01-15 20:11:34 +00002402 case QEMU_OPTION_rtc_td_hack:
2403 rtc_td_hack = 1;
2404 break;
aliguori8a92ea22009-02-27 20:12:36 +00002405 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002406 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002407 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002408 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002409 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002410 break;
aliguori7ba1e612008-11-05 16:04:33 +00002411 case QEMU_OPTION_enable_kvm:
2412 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00002413 break;
bellardbb36d472005-11-05 14:22:28 +00002414 case QEMU_OPTION_usb:
2415 usb_enabled = 1;
2416 break;
bellarda594cfb2005-11-06 16:13:29 +00002417 case QEMU_OPTION_usbdevice:
2418 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002419 add_device_config(DEV_USB, optarg);
2420 break;
2421 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002422 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002423 exit(1);
2424 }
bellarda594cfb2005-11-06 16:13:29 +00002425 break;
bellard6a00d602005-11-21 23:25:50 +00002426 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002427 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002428 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002429 fprintf(stderr, "Invalid number of CPUs\n");
2430 exit(1);
2431 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002432 if (max_cpus < smp_cpus) {
2433 fprintf(stderr, "maxcpus must be equal to or greater than "
2434 "smp\n");
2435 exit(1);
2436 }
2437 if (max_cpus > 255) {
2438 fprintf(stderr, "Unsupported number of maxcpus\n");
2439 exit(1);
2440 }
bellard6a00d602005-11-21 23:25:50 +00002441 break;
bellard24236862006-04-30 21:28:36 +00002442 case QEMU_OPTION_vnc:
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002443 display_remote++;
ths73fc9742006-12-22 02:09:07 +00002444 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00002445 break;
bellard6515b202006-05-03 22:02:44 +00002446 case QEMU_OPTION_no_acpi:
2447 acpi_enabled = 0;
2448 break;
aliguori16b29ae2008-12-17 23:28:44 +00002449 case QEMU_OPTION_no_hpet:
2450 no_hpet = 1;
2451 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002452 case QEMU_OPTION_balloon:
2453 if (balloon_parse(optarg) < 0) {
2454 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2455 exit(1);
2456 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002457 break;
bellardd1beab82006-10-02 19:44:22 +00002458 case QEMU_OPTION_no_reboot:
2459 no_reboot = 1;
2460 break;
aurel32b2f76162008-04-11 21:35:52 +00002461 case QEMU_OPTION_no_shutdown:
2462 no_shutdown = 1;
2463 break;
balrog9467cd42007-05-01 01:34:14 +00002464 case QEMU_OPTION_show_cursor:
2465 cursor_hide = 0;
2466 break;
blueswir18fcb1b92008-09-18 18:29:08 +00002467 case QEMU_OPTION_uuid:
2468 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2469 fprintf(stderr, "Fail to parse UUID string."
2470 " Wrong format.\n");
2471 exit(1);
2472 }
2473 break;
ths9ae02552007-01-05 17:39:04 +00002474 case QEMU_OPTION_option_rom:
2475 if (nb_option_roms >= MAX_OPTION_ROMS) {
2476 fprintf(stderr, "Too many option ROMs\n");
2477 exit(1);
2478 }
2479 option_rom[nb_option_roms] = optarg;
2480 nb_option_roms++;
2481 break;
pbrook8e716212007-01-20 17:12:09 +00002482 case QEMU_OPTION_semihosting:
2483 semihosting_enabled = 1;
2484 break;
thsc35734b2007-03-19 15:17:08 +00002485 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02002486 qemu_name = qemu_strdup(optarg);
2487 {
2488 char *p = strchr(qemu_name, ',');
2489 if (p != NULL) {
2490 *p++ = 0;
2491 if (strncmp(p, "process=", 8)) {
2492 fprintf(stderr, "Unknown subargument %s to -name", p);
2493 exit(1);
2494 }
2495 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02002496 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02002497 }
2498 }
thsc35734b2007-03-19 15:17:08 +00002499 break;
blueswir166508602007-05-01 14:16:52 +00002500 case QEMU_OPTION_prom_env:
2501 if (nb_prom_envs >= MAX_PROM_ENVS) {
2502 fprintf(stderr, "Too many prom variables\n");
2503 exit(1);
2504 }
2505 prom_envs[nb_prom_envs] = optarg;
2506 nb_prom_envs++;
2507 break;
balrog2b8f2d42007-07-27 22:08:46 +00002508 case QEMU_OPTION_old_param:
2509 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00002510 break;
thsf3dcfad2007-08-24 01:26:02 +00002511 case QEMU_OPTION_clock:
2512 configure_alarms(optarg);
2513 break;
bellard7e0af5d02007-11-07 16:24:33 +00002514 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002515 configure_rtc_date_offset(optarg, 1);
2516 break;
2517 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002518 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002519 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002520 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00002521 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002522 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00002523 break;
bellard26a5f132008-05-28 12:30:31 +00002524 case QEMU_OPTION_tb_size:
2525 tb_size = strtol(optarg, NULL, 0);
2526 if (tb_size < 0)
2527 tb_size = 0;
2528 break;
pbrook2e70f6e2008-06-29 01:03:05 +00002529 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002530 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00002531 break;
aliguori5bb79102008-10-13 03:12:02 +00002532 case QEMU_OPTION_incoming:
2533 incoming = optarg;
Amit Shah8e848652010-07-27 15:49:19 +05302534 incoming_expected = true;
aliguori5bb79102008-10-13 03:12:02 +00002535 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002536 case QEMU_OPTION_nodefaults:
2537 default_serial = 0;
2538 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002539 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002540 default_monitor = 0;
2541 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01002542 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002543 default_floppy = 0;
2544 default_cdrom = 0;
2545 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002546 break;
aliguorie37630c2009-04-22 15:19:10 +00002547 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00002548 if (!(xen_available())) {
2549 printf("Option %s not supported for this target\n", popt->name);
2550 exit(1);
2551 }
aliguorie37630c2009-04-22 15:19:10 +00002552 xen_domid = atoi(optarg);
2553 break;
2554 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00002555 if (!(xen_available())) {
2556 printf("Option %s not supported for this target\n", popt->name);
2557 exit(1);
2558 }
aliguorie37630c2009-04-22 15:19:10 +00002559 xen_mode = XEN_CREATE;
2560 break;
2561 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00002562 if (!(xen_available())) {
2563 printf("Option %s not supported for this target\n", popt->name);
2564 exit(1);
2565 }
aliguorie37630c2009-04-22 15:19:10 +00002566 xen_mode = XEN_ATTACH;
2567 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002568#ifdef CONFIG_SIMPLE_TRACE
2569 case QEMU_OPTION_trace:
2570 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2571 if (opts) {
2572 trace_file = qemu_opt_get(opts, "file");
2573 }
2574 break;
2575#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002576 case QEMU_OPTION_readconfig:
2577 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002578 int ret = qemu_read_config_file(optarg);
2579 if (ret < 0) {
2580 fprintf(stderr, "read config %s: %s\n", optarg,
2581 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002582 exit(1);
2583 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002584 break;
2585 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03002586 case QEMU_OPTION_spice:
2587 olist = qemu_find_opts("spice");
2588 if (!olist) {
2589 fprintf(stderr, "spice is not supported by this qemu build.\n");
2590 exit(1);
2591 }
2592 opts = qemu_opts_parse(olist, optarg, 0);
2593 if (!opts) {
2594 fprintf(stderr, "parse error: %s\n", optarg);
2595 exit(1);
2596 }
2597 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002598 case QEMU_OPTION_writeconfig:
2599 {
2600 FILE *fp;
2601 if (strcmp(optarg, "-") == 0) {
2602 fp = stdout;
2603 } else {
2604 fp = fopen(optarg, "w");
2605 if (fp == NULL) {
2606 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2607 exit(1);
2608 }
2609 }
2610 qemu_config_write(fp);
2611 fclose(fp);
2612 break;
2613 }
Jes Sorensen59a52642010-06-10 11:42:25 +02002614 default:
2615 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00002616 }
bellard0824d6f2003-06-24 13:42:40 +00002617 }
2618 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002619 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00002620
Paul Brook5cea8592009-05-30 00:52:44 +01002621 /* If no data_dir is specified then try to find it relative to the
2622 executable path. */
2623 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02002624 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01002625 }
2626 /* If all else fails use the install patch specified when building. */
2627 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02002628 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01002629 }
2630
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002631#ifdef CONFIG_SIMPLE_TRACE
2632 /*
2633 * Set the trace file name, if specified.
2634 */
2635 st_set_trace_file(trace_file);
2636#endif
Jes Sorensen6be68d72009-07-23 17:03:42 +02002637 /*
2638 * Default to max_cpus = smp_cpus, in case the user doesn't
2639 * specify a max_cpus value.
2640 */
2641 if (!max_cpus)
2642 max_cpus = smp_cpus;
2643
balrog3d878ca2008-10-28 10:59:59 +00002644 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00002645 if (smp_cpus > machine->max_cpus) {
2646 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2647 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2648 machine->max_cpus);
2649 exit(1);
2650 }
2651
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002652 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2653 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002654
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002655 if (machine->no_serial) {
2656 default_serial = 0;
2657 }
2658 if (machine->no_parallel) {
2659 default_parallel = 0;
2660 }
2661 if (!machine->use_virtcon) {
2662 default_virtcon = 0;
2663 }
2664 if (machine->no_vga) {
2665 default_vga = 0;
2666 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002667 if (machine->no_floppy) {
2668 default_floppy = 0;
2669 }
2670 if (machine->no_cdrom) {
2671 default_cdrom = 0;
2672 }
2673 if (machine->no_sdcard) {
2674 default_sdcard = 0;
2675 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002676
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002677 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002678 if (default_parallel)
2679 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002680 if (default_serial && default_monitor) {
2681 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002682 } else if (default_virtcon && default_monitor) {
2683 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002684 } else {
2685 if (default_serial)
2686 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002687 if (default_virtcon)
2688 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002689 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002690 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002691 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002692 } else {
2693 if (default_serial)
2694 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002695 if (default_parallel)
2696 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01002697 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002698 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01002699 if (default_virtcon)
2700 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00002701 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01002702 if (default_vga)
2703 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00002704
TeLeMana5829fd2010-04-15 12:37:55 +08002705 socket_init();
2706
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002707 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002708 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07002709#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002710 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302711 exit(1);
2712 }
2713#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002714
Jes Sorenseneb505be2010-06-10 11:42:28 +02002715 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00002716
thsaa26bb22007-03-25 21:33:06 +00002717 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02002718 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00002719 exit(1);
2720 }
2721
Paolo Bonzini98c85732010-04-19 18:59:30 +00002722 if (kvm_allowed) {
2723 int ret = kvm_init(smp_cpus);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002724 if (ret < 0) {
Paolo Bonzini98c85732010-04-19 18:59:30 +00002725 if (!kvm_available()) {
2726 printf("KVM not supported for this target\n");
2727 } else {
2728 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2729 }
Marcelo Tosatti214910a2009-09-18 02:41:23 -03002730 exit(1);
2731 }
2732 }
2733
aliguori3fcf7b62009-04-24 18:03:25 +00002734 if (qemu_init_main_loop()) {
2735 fprintf(stderr, "qemu_init_main_loop failed\n");
2736 exit(1);
2737 }
bellarda20dd502003-09-30 21:07:02 +00002738 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00002739
thsf8d39c02008-07-03 10:01:15 +00002740 if (!linux_boot && *kernel_cmdline != '\0') {
2741 fprintf(stderr, "-append only allowed with -kernel option\n");
2742 exit(1);
2743 }
2744
2745 if (!linux_boot && initrd_filename != NULL) {
2746 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2747 exit(1);
2748 }
2749
Jes Sorensen9156d762010-06-10 11:42:30 +02002750 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00002751
aliguori7183b4b2008-11-05 20:40:18 +00002752 if (init_timer_alarm() < 0) {
2753 fprintf(stderr, "could not initialize alarm timer\n");
2754 exit(1);
2755 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002756 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00002757
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01002758 if (net_init_clients() < 0) {
2759 exit(1);
bellard702c6512004-04-02 21:21:32 +00002760 }
bellardf1510b22003-06-25 00:07:40 +00002761
balrogdc72ac12008-11-09 00:04:26 +00002762 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002763 if (foreach_device_config(DEV_BT, bt_parse))
2764 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00002765
bellard0824d6f2003-06-24 13:42:40 +00002766 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00002767 if (ram_size == 0)
2768 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00002769
bellard26a5f132008-05-28 12:30:31 +00002770 /* init the dynamic translator */
2771 cpu_exec_init_all(tb_size * 1024 * 1024);
2772
Markus Armbrustereb852012009-10-27 18:41:44 +01002773 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00002774
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002775 blk_mig_init();
2776
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002777 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002778 /* we always create the cdrom drive, even if no disk is there */
2779 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002780 }
thse4bcb142007-12-02 04:51:10 +00002781
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002782 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002783 /* we always create at least one floppy */
2784 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002785 }
bellardc4b1fcc2004-03-14 21:44:30 +00002786
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002787 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01002788 /* we always create one sd slot, even if no card is in it */
2789 drive_add(NULL, SD_ALIAS);
2790 }
balrog9d413d12007-12-04 00:10:34 +00002791
ths96d30e42007-01-07 20:42:14 +00002792 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002793 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002794 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2795 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002796 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00002797
Alex Williamson97ab12d2010-06-25 11:09:50 -06002798 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02002799 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00002800
aliguori268a3622009-04-21 22:30:27 +00002801 if (nb_numa_nodes > 0) {
2802 int i;
2803
2804 if (nb_numa_nodes > smp_cpus) {
2805 nb_numa_nodes = smp_cpus;
2806 }
2807
2808 /* If no memory size if given for any node, assume the default case
2809 * and distribute the available memory equally across all nodes
2810 */
2811 for (i = 0; i < nb_numa_nodes; i++) {
2812 if (node_mem[i] != 0)
2813 break;
2814 }
2815 if (i == nb_numa_nodes) {
2816 uint64_t usedmem = 0;
2817
2818 /* On Linux, the each node's border has to be 8MB aligned,
2819 * the final node gets the rest.
2820 */
2821 for (i = 0; i < nb_numa_nodes - 1; i++) {
2822 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2823 usedmem += node_mem[i];
2824 }
2825 node_mem[i] = ram_size - usedmem;
2826 }
2827
2828 for (i = 0; i < nb_numa_nodes; i++) {
2829 if (node_cpumask[i] != 0)
2830 break;
2831 }
2832 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2833 * must cope with this anyway, because there are BIOSes out there in
2834 * real machines which also use this scheme.
2835 */
2836 if (i == nb_numa_nodes) {
2837 for (i = 0; i < smp_cpus; i++) {
2838 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2839 }
2840 }
2841 }
2842
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002843 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02002844 exit(1);
2845 }
2846
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002847 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2848 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002849 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2850 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002851 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2852 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002853 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2854 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00002855
Paul Brookaae94602009-05-14 22:35:06 +01002856 module_call_init(MODULE_INIT_DEVICE);
2857
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002858 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002859 exit(0);
2860
Markus Armbruster09aaa162009-08-21 10:31:34 +02002861 if (watchdog) {
2862 i = select_watchdog(watchdog);
2863 if (i > 0)
2864 exit (i == 1 ? 1 : 0);
2865 }
2866
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002867 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01002868 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02002869 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002870 qemu_add_globals();
2871
Paul Brookfbe1b592009-05-13 17:56:25 +01002872 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00002873 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2874
Jan Kiszkaea375f92010-03-01 19:10:30 +01002875 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00002876
Juan Quintela67b3b712009-08-28 19:25:15 +02002877 /* must be after terminal init, SDL library changes signal handlers */
Jes Sorensen8d963e62010-06-10 11:42:22 +02002878 os_setup_signal_handling();
Juan Quintela67b3b712009-08-28 19:25:15 +02002879
Blue Swirl87d0a282010-03-27 18:24:45 +00002880 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00002881
aliguori6f338c32009-02-11 15:21:54 +00002882 current_machine = machine;
2883
aliguori3023f332009-01-16 19:04:14 +00002884 /* init USB devices */
2885 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01002886 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2887 exit(1);
aliguori3023f332009-01-16 19:04:14 +00002888 }
2889
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002890 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002891 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002892 exit(1);
2893
Markus Armbruster668680f2010-02-11 14:44:58 +01002894 net_check_clients();
2895
aliguori3023f332009-01-16 19:04:14 +00002896 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01002897 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002898
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02002899 if (using_spice)
2900 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002901 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002902#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002903 display_type = DT_SDL;
2904#else
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002905 vnc_display = "localhost:0,to=99";
2906 show_vnc_port = 1;
2907#endif
2908 }
2909
2910
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002911 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002912 switch (display_type) {
2913 case DT_NOGRAPHIC:
2914 break;
2915#if defined(CONFIG_CURSES)
2916 case DT_CURSES:
2917 curses_display_init(ds, full_screen);
2918 break;
2919#endif
bellard5b0753e2005-03-01 21:37:28 +00002920#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002921 case DT_SDL:
2922 sdl_display_init(ds, full_screen, no_frame);
2923 break;
bellard5b0753e2005-03-01 21:37:28 +00002924#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002925 case DT_SDL:
2926 cocoa_display_init(ds, full_screen);
2927 break;
bellard313aa562003-08-10 21:52:11 +00002928#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002929 default:
2930 break;
2931 }
2932
2933 /* init remote displays */
2934 if (vnc_display) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002935 vnc_display_init(ds);
2936 if (vnc_display_open(ds, vnc_display) < 0)
2937 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002938
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002939 if (show_vnc_port) {
2940 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05002941 }
bellard313aa562003-08-10 21:52:11 +00002942 }
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02002943#ifdef CONFIG_SPICE
2944 if (using_spice) {
2945 qemu_spice_display_init(ds);
2946 }
2947#endif
aliguori5b08fc12008-08-21 20:08:03 +00002948
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002949 /* display setup */
2950 dpy_resize(ds);
aliguori3023f332009-01-16 19:04:14 +00002951 dcl = ds->listeners;
2952 while (dcl != NULL) {
2953 if (dcl->dpy_refresh != NULL) {
2954 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2955 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09002956 break;
ths20d8a3e2007-02-18 17:04:49 +00002957 }
aliguori3023f332009-01-16 19:04:14 +00002958 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00002959 }
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002960 if (ds->gui_timer == NULL) {
blueswir19043b622009-01-21 19:28:13 +00002961 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2962 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2963 }
Paolo Bonzinib473df62010-02-11 00:29:55 +01002964 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00002965
aliguori59030a82009-04-05 18:43:41 +00002966 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2967 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2968 gdbstub_dev);
2969 exit(1);
balrog45669e02007-07-02 13:20:17 +00002970 }
balrog45669e02007-07-02 13:20:17 +00002971
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02002972 qdev_machine_creation_done();
2973
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01002974 if (rom_load_all() != 0) {
2975 fprintf(stderr, "rom loading failed\n");
2976 exit(1);
2977 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02002978
Juan Quintela504c2942009-11-12 00:39:13 +01002979 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02002980 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01002981 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02002982 autostart = 0;
2983 }
2984 }
bellardd63d3072004-10-03 13:29:03 +00002985
Glauber Costa2bb8c102009-07-24 16:20:23 -04002986 if (incoming) {
Juan Quintela8ca5e802010-06-09 14:10:54 +02002987 int ret = qemu_start_incoming_migration(incoming);
2988 if (ret < 0) {
2989 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
2990 incoming, ret);
2991 exit(ret);
2992 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03002993 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00002994 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03002995 }
thsffd843b2006-12-21 19:46:43 +00002996
Jes Sorenseneb505be2010-06-10 11:42:28 +02002997 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00002998
bellard8a7ddc32004-03-31 19:00:16 +00002999 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003000 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003001 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00003002
bellard0824d6f2003-06-24 13:42:40 +00003003 return 0;
3004}