blob: 4fcedbfeb71995f816e8af66c06940fd1910eaec [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>
aliguori08585322009-02-27 22:09:45 +000037#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000038#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000041#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000042#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000043#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000045#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000047#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000048#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000049#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000050#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020051#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000052#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010053#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000054#include <libutil.h>
Juergen Lock92c0e652010-03-25 22:07:12 +010055#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000056#else
57#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000058#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010059#else
blueswir1223f0d72008-09-30 18:12:18 +000060#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000061#include <pty.h>
62#include <malloc.h>
Andi Kleen18894652009-07-02 09:34:17 +020063#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000064
bellarde57a8c02005-11-10 23:58:52 +000065#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000066#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000067#endif
68#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000069#include <sys/stat.h>
70#include <sys/ethernet.h>
71#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000072#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000073#include <netinet/in_systm.h>
74#include <netinet/ip.h>
75#include <netinet/ip_icmp.h> // must come after ip.h
76#include <netinet/udp.h>
77#include <netinet/tcp.h>
78#include <net/if.h>
79#include <syslog.h>
80#include <stropts.h>
Blue Swirl8d32cf02009-10-02 19:32:12 +000081/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
82 discussion about Solaris header problems */
83extern int madvise(caddr_t, size_t, int);
bellard67b915a2004-03-31 23:37:16 +000084#endif
bellard7d3505c2004-05-12 19:32:15 +000085#endif
bellardec530c82006-04-25 22:36:06 +000086#endif
bellard67b915a2004-03-31 23:37:16 +000087
blueswir19892fbf2008-08-24 10:34:20 +000088#if defined(__OpenBSD__)
89#include <util.h>
90#endif
91
ths8a16d272008-07-19 09:56:24 +000092#if defined(CONFIG_VDE)
93#include <libvdeplug.h>
94#endif
95
bellard67b915a2004-03-31 23:37:16 +000096#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000097#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000098#endif
99
bellard73332e52004-04-04 20:22:28 +0000100#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200101#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200102#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000103int qemu_main(int argc, char **argv, char **envp);
104int main(int argc, char **argv)
105{
Stefan Weil59a36a22009-06-18 20:11:03 +0200106 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000107}
108#undef main
109#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000110#endif
bellard73332e52004-04-04 20:22:28 +0000111#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000112
bellard5b0753e2005-03-01 21:37:28 +0000113#ifdef CONFIG_COCOA
114#undef main
115#define main qemu_main
116#endif /* CONFIG_COCOA */
117
blueswir1511d2b12009-03-07 15:32:56 +0000118#include "hw/hw.h"
119#include "hw/boards.h"
120#include "hw/usb.h"
121#include "hw/pcmcia.h"
122#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000123#include "hw/isa.h"
124#include "hw/baum.h"
125#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100126#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000127#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000128#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200129#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200130#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000131#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000132#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000133#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000134#include "monitor.h"
135#include "console.h"
136#include "sysemu.h"
137#include "gdbstub.h"
138#include "qemu-timer.h"
139#include "qemu-char.h"
140#include "cache-utils.h"
141#include "block.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200142#include "block_int.h"
143#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000144#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000145#include "audio/audio.h"
146#include "migration.h"
147#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200148#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200149#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200150#include "qemu-objects.h"
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530151#ifdef CONFIG_LINUX
152#include "fsdev/qemu-fsdev.h"
153#endif
blueswir1511d2b12009-03-07 15:32:56 +0000154
bellard0824d6f2003-06-24 13:42:40 +0000155#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000156
blueswir1511d2b12009-03-07 15:32:56 +0000157#include "qemu_socket.h"
158
Jan Kiszkad918f232009-06-24 14:42:30 +0200159#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
Blue Swirl72cf2d42009-09-12 07:36:22 +0000161#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000162#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000163#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000164
blueswir19dc63a12008-10-04 07:25:46 +0000165//#define DEBUG_NET
166//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000167
bellard1bfe8562004-07-08 21:17:50 +0000168#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000169
Amit Shah98b19252010-01-20 00:36:52 +0530170#define MAX_VIRTIO_CONSOLES 1
171
Paul Brook5cea8592009-05-30 00:52:44 +0100172static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000173const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000174/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000175 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000176struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
177struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000178enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500179DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000180const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500181ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300182const char *mem_path = NULL;
183#ifdef MAP_POPULATE
184int mem_prealloc = 0; /* force preallocation of physical target memory */
185#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000186int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000187NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000188int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200189int autostart;
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;
blueswir1b9e82a52009-04-05 18:03:31 +0000219#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000220int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000221#endif
Markus Armbruster09aaa162009-08-21 10:31:34 +0200222const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000223const char *option_rom[MAX_OPTION_ROMS];
224int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000225int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000226int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000227const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000228int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500229int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000230unsigned int nb_prom_envs = 0;
231const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200232int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000233
aliguori268a3622009-04-21 22:30:27 +0000234int nb_numa_nodes;
235uint64_t node_mem[MAX_NODES];
236uint64_t node_cpumask[MAX_NODES];
237
blueswir19043b622009-01-21 19:28:13 +0000238static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000239
blueswir18fcb1b92008-09-18 18:29:08 +0000240uint8_t qemu_uuid[16];
241
Jan Kiszka76e30d02009-07-02 00:19:02 +0200242static QEMUBootSetHandler *boot_set_handler;
243static void *boot_set_opaque;
244
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/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000290
Andi Kleen18894652009-07-02 09:34:17 +0200291static void set_proc_name(const char *s)
292{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700293#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200294 char name[16];
295 if (!s)
296 return;
297 name[sizeof(name) - 1] = 0;
298 strncpy(name, s, sizeof(name));
299 /* Could rewrite argv[0] too, but that's a bit more complicated.
300 This simple way is enough for `top'. */
301 prctl(PR_SET_NAME, name);
302#endif
303}
aliguoridf751fa2008-12-04 20:19:35 +0000304
bellard8a7ddc32004-03-31 19:00:16 +0000305/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100306/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000307
bellard87858c82003-06-27 12:01:39 +0000308/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000309uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000310{
311 union {
312 uint64_t ll;
313 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200314#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000315 uint32_t high, low;
316#else
317 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000318#endif
bellard87858c82003-06-27 12:01:39 +0000319 } l;
320 } u, res;
321 uint64_t rl, rh;
322
323 u.ll = a;
324 rl = (uint64_t)u.l.low * (uint64_t)b;
325 rh = (uint64_t)u.l.high * (uint64_t)b;
326 rh += (rl >> 32);
327 res.l.high = rh / c;
328 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
329 return res.ll;
330}
331
bellardc4b1fcc2004-03-14 21:44:30 +0000332/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000333/* host time/date access */
334void qemu_get_timedate(struct tm *tm, int offset)
335{
336 time_t ti;
337 struct tm *ret;
338
339 time(&ti);
340 ti += offset;
341 if (rtc_date_offset == -1) {
342 if (rtc_utc)
343 ret = gmtime(&ti);
344 else
345 ret = localtime(&ti);
346 } else {
347 ti -= rtc_date_offset;
348 ret = gmtime(&ti);
349 }
350
351 memcpy(tm, ret, sizeof(struct tm));
352}
353
354int qemu_timedate_diff(struct tm *tm)
355{
356 time_t seconds;
357
358 if (rtc_date_offset == -1)
359 if (rtc_utc)
360 seconds = mktimegm(tm);
361 else
362 seconds = mktime(tm);
363 else
364 seconds = mktimegm(tm) + rtc_date_offset;
365
366 return seconds - time(NULL);
367}
368
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300369void rtc_change_mon_event(struct tm *tm)
370{
371 QObject *data;
372
373 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
374 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
375 qobject_decref(data);
376}
377
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200378static void configure_rtc_date_offset(const char *startdate, int legacy)
379{
380 time_t rtc_start_date;
381 struct tm tm;
382
383 if (!strcmp(startdate, "now") && legacy) {
384 rtc_date_offset = -1;
385 } else {
386 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
387 &tm.tm_year,
388 &tm.tm_mon,
389 &tm.tm_mday,
390 &tm.tm_hour,
391 &tm.tm_min,
392 &tm.tm_sec) == 6) {
393 /* OK */
394 } else if (sscanf(startdate, "%d-%d-%d",
395 &tm.tm_year,
396 &tm.tm_mon,
397 &tm.tm_mday) == 3) {
398 tm.tm_hour = 0;
399 tm.tm_min = 0;
400 tm.tm_sec = 0;
401 } else {
402 goto date_fail;
403 }
404 tm.tm_year -= 1900;
405 tm.tm_mon--;
406 rtc_start_date = mktimegm(&tm);
407 if (rtc_start_date == -1) {
408 date_fail:
409 fprintf(stderr, "Invalid date format. Valid formats are:\n"
410 "'2006-06-17T16:01:21' or '2006-06-17'\n");
411 exit(1);
412 }
413 rtc_date_offset = time(NULL) - rtc_start_date;
414 }
415}
416
417static void configure_rtc(QemuOpts *opts)
418{
419 const char *value;
420
421 value = qemu_opt_get(opts, "base");
422 if (value) {
423 if (!strcmp(value, "utc")) {
424 rtc_utc = 1;
425 } else if (!strcmp(value, "localtime")) {
426 rtc_utc = 0;
427 } else {
428 configure_rtc_date_offset(value, 0);
429 }
430 }
Jan Kiszka68752042009-09-15 13:36:04 +0200431 value = qemu_opt_get(opts, "clock");
432 if (value) {
433 if (!strcmp(value, "host")) {
434 rtc_clock = host_clock;
435 } else if (!strcmp(value, "vm")) {
436 rtc_clock = vm_clock;
437 } else {
438 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
439 exit(1);
440 }
441 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200442 value = qemu_opt_get(opts, "driftfix");
443 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000444 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200445 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000446 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200447 rtc_td_hack = 0;
448 } else {
449 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
450 exit(1);
451 }
452 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200453}
454
balrog1ae26a12008-09-28 23:19:47 +0000455/***********************************************************/
456/* Bluetooth support */
457static int nb_hcis;
458static int cur_hci;
459static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000460
balrog1ae26a12008-09-28 23:19:47 +0000461static struct bt_vlan_s {
462 struct bt_scatternet_s net;
463 int id;
464 struct bt_vlan_s *next;
465} *first_bt_vlan;
466
467/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000468static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000469{
470 struct bt_vlan_s **pvlan, *vlan;
471 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
472 if (vlan->id == id)
473 return &vlan->net;
474 }
475 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
476 vlan->id = id;
477 pvlan = &first_bt_vlan;
478 while (*pvlan != NULL)
479 pvlan = &(*pvlan)->next;
480 *pvlan = vlan;
481 return &vlan->net;
482}
483
484static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
485{
486}
487
488static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
489{
490 return -ENOTSUP;
491}
492
493static struct HCIInfo null_hci = {
494 .cmd_send = null_hci_send,
495 .sco_send = null_hci_send,
496 .acl_send = null_hci_send,
497 .bdaddr_set = null_hci_addr_set,
498};
499
500struct HCIInfo *qemu_next_hci(void)
501{
502 if (cur_hci == nb_hcis)
503 return &null_hci;
504
505 return hci_table[cur_hci++];
506}
507
balrogdc72ac12008-11-09 00:04:26 +0000508static struct HCIInfo *hci_init(const char *str)
509{
510 char *endp;
511 struct bt_scatternet_s *vlan = 0;
512
513 if (!strcmp(str, "null"))
514 /* null */
515 return &null_hci;
516 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
517 /* host[:hciN] */
518 return bt_host_hci(str[4] ? str + 5 : "hci0");
519 else if (!strncmp(str, "hci", 3)) {
520 /* hci[,vlan=n] */
521 if (str[3]) {
522 if (!strncmp(str + 3, ",vlan=", 6)) {
523 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
524 if (*endp)
525 vlan = 0;
526 }
527 } else
528 vlan = qemu_find_bt_vlan(0);
529 if (vlan)
530 return bt_new_hci(vlan);
531 }
532
533 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
534
535 return 0;
536}
537
538static int bt_hci_parse(const char *str)
539{
540 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500541 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000542
543 if (nb_hcis >= MAX_NICS) {
544 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
545 return -1;
546 }
547
548 hci = hci_init(str);
549 if (!hci)
550 return -1;
551
552 bdaddr.b[0] = 0x52;
553 bdaddr.b[1] = 0x54;
554 bdaddr.b[2] = 0x00;
555 bdaddr.b[3] = 0x12;
556 bdaddr.b[4] = 0x34;
557 bdaddr.b[5] = 0x56 + nb_hcis;
558 hci->bdaddr_set(hci, bdaddr.b);
559
560 hci_table[nb_hcis++] = hci;
561
562 return 0;
563}
564
565static void bt_vhci_add(int vlan_id)
566{
567 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
568
569 if (!vlan->slave)
570 fprintf(stderr, "qemu: warning: adding a VHCI to "
571 "an empty scatternet %i\n", vlan_id);
572
573 bt_vhci_init(bt_new_hci(vlan));
574}
575
576static struct bt_device_s *bt_device_add(const char *opt)
577{
578 struct bt_scatternet_s *vlan;
579 int vlan_id = 0;
580 char *endp = strstr(opt, ",vlan=");
581 int len = (endp ? endp - opt : strlen(opt)) + 1;
582 char devname[10];
583
584 pstrcpy(devname, MIN(sizeof(devname), len), opt);
585
586 if (endp) {
587 vlan_id = strtol(endp + 6, &endp, 0);
588 if (*endp) {
589 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
590 return 0;
591 }
592 }
593
594 vlan = qemu_find_bt_vlan(vlan_id);
595
596 if (!vlan->slave)
597 fprintf(stderr, "qemu: warning: adding a slave device to "
598 "an empty scatternet %i\n", vlan_id);
599
600 if (!strcmp(devname, "keyboard"))
601 return bt_keyboard_init(vlan);
602
603 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
604 return 0;
605}
606
607static int bt_parse(const char *opt)
608{
609 const char *endp, *p;
610 int vlan;
611
612 if (strstart(opt, "hci", &endp)) {
613 if (!*endp || *endp == ',') {
614 if (*endp)
615 if (!strstart(endp, ",vlan=", 0))
616 opt = endp + 1;
617
618 return bt_hci_parse(opt);
619 }
620 } else if (strstart(opt, "vhci", &endp)) {
621 if (!*endp || *endp == ',') {
622 if (*endp) {
623 if (strstart(endp, ",vlan=", &p)) {
624 vlan = strtol(p, (char **) &endp, 0);
625 if (*endp) {
626 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
627 return 1;
628 }
629 } else {
630 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
631 return 1;
632 }
633 } else
634 vlan = 0;
635
636 bt_vhci_add(vlan);
637 return 0;
638 }
639 } else if (strstart(opt, "device:", &endp))
640 return !bt_device_add(endp);
641
642 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
643 return 1;
644}
645
balrog1ae26a12008-09-28 23:19:47 +0000646/***********************************************************/
647/* QEMU Block devices */
648
balrog609497a2008-01-14 02:56:53 +0000649#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +0000650#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +0000651#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +0000652#define PFLASH_ALIAS "if=pflash"
653#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +0000654#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +0000655
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200656QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +0000657{
658 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200659 char optstr[1024];
660 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +0000661
thse4bcb142007-12-02 04:51:10 +0000662 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200663 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +0000664 va_end(ap);
665
Markus Armbruster8212c642010-02-10 19:52:18 +0100666 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200667 if (!opts) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200668 return NULL;
669 }
670 if (file)
671 qemu_opt_set(opts, "file", file);
672 return opts;
aliguorib01b1112009-02-11 15:20:20 +0000673}
674
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200675DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +0000676{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200677 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +0000678
679 /* seek interface, bus and unit */
680
Blue Swirl72cf2d42009-09-12 07:36:22 +0000681 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200682 if (dinfo->type == type &&
683 dinfo->bus == bus &&
684 dinfo->unit == unit)
685 return dinfo;
686 }
thse4bcb142007-12-02 04:51:10 +0000687
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200688 return NULL;
thse4bcb142007-12-02 04:51:10 +0000689}
690
Gerd Hoffmann2e810b32009-07-31 12:25:38 +0200691DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +0200692{
693 DriveInfo *dinfo;
694
Blue Swirl72cf2d42009-09-12 07:36:22 +0000695 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +0200696 if (strcmp(id, dinfo->id))
697 continue;
698 return dinfo;
699 }
700 return NULL;
701}
702
thsf60d39b2007-12-17 03:55:57 +0000703int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +0000704{
705 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200706 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +0000707
708 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000709 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200710 if(dinfo->type == type &&
711 dinfo->bus > max_bus)
712 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +0000713 }
714 return max_bus;
715}
716
aliguorifa879c62009-01-07 17:32:33 +0000717const char *drive_get_serial(BlockDriverState *bdrv)
718{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200719 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +0000720
Blue Swirl72cf2d42009-09-12 07:36:22 +0000721 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200722 if (dinfo->bdrv == bdrv)
723 return dinfo->serial;
724 }
aliguorifa879c62009-01-07 17:32:33 +0000725
726 return "\0";
727}
728
Kevin Wolff7850092009-11-27 13:25:36 +0100729BlockInterfaceErrorAction drive_get_on_error(
730 BlockDriverState *bdrv, int is_read)
aliguori428c5702009-01-21 18:59:04 +0000731{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200732 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +0000733
Blue Swirl72cf2d42009-09-12 07:36:22 +0000734 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200735 if (dinfo->bdrv == bdrv)
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100736 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200737 }
aliguori428c5702009-01-21 18:59:04 +0000738
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100739 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +0000740}
741
aurel32a1620fa2008-04-29 05:58:01 +0000742static void bdrv_format_print(void *opaque, const char *name)
743{
744 fprintf(stderr, " %s", name);
745}
746
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200747void drive_uninit(DriveInfo *dinfo)
aliguorib01b1112009-02-11 15:20:20 +0000748{
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200749 qemu_opts_del(dinfo->opts);
750 bdrv_delete(dinfo->bdrv);
751 QTAILQ_REMOVE(&drives, dinfo, next);
752 qemu_free(dinfo);
aliguorib01b1112009-02-11 15:20:20 +0000753}
754
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100755static int parse_block_error_action(const char *buf, int is_read)
756{
757 if (!strcmp(buf, "ignore")) {
758 return BLOCK_ERR_IGNORE;
759 } else if (!is_read && !strcmp(buf, "enospc")) {
760 return BLOCK_ERR_STOP_ENOSPC;
761 } else if (!strcmp(buf, "stop")) {
762 return BLOCK_ERR_STOP_ANY;
763 } else if (!strcmp(buf, "report")) {
764 return BLOCK_ERR_REPORT;
765 } else {
766 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
767 buf, is_read ? "read" : "write");
768 return -1;
769 }
770}
771
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200772DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200773 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +0000774{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200775 const char *buf;
776 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +0000777 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200778 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +0000779 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +0000780 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +0000781 enum { MEDIA_DISK, MEDIA_CDROM } media;
782 int bus_id, unit_id;
783 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +0000784 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +0000785 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +0000786 int max_devs;
787 int index;
Naphtali Sprei59f26892009-10-26 16:25:16 +0200788 int ro = 0;
Christoph Hellwig763b6082010-04-06 19:12:04 +0200789 int bdrv_flags = 0;
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100790 int on_read_error, on_write_error;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200791 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200792 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200793 int snapshot = 0;
794
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200795 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +0000796
thse4bcb142007-12-02 04:51:10 +0000797 translation = BIOS_ATA_TRANSLATION_AUTO;
thse4bcb142007-12-02 04:51:10 +0000798
Gerd Hoffmann4d007812009-08-31 14:23:57 +0200799 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +0000800 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +0000801 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +0000802 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +0000803 } else {
thsf60d39b2007-12-17 03:55:57 +0000804 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +0000805 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +0000806 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +0000807 }
808 media = MEDIA_DISK;
809
810 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200811 bus_id = qemu_opt_get_number(opts, "bus", 0);
812 unit_id = qemu_opt_get_number(opts, "unit", -1);
813 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +0000814
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200815 cyls = qemu_opt_get_number(opts, "cyls", 0);
816 heads = qemu_opt_get_number(opts, "heads", 0);
817 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +0000818
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200819 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Naphtali Sprei59f26892009-10-26 16:25:16 +0200820 ro = qemu_opt_get_bool(opts, "readonly", 0);
thse4bcb142007-12-02 04:51:10 +0000821
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200822 file = qemu_opt_get(opts, "file");
823 serial = qemu_opt_get(opts, "serial");
824
825 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +0000826 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +0000827 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +0000828 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +0000829 max_devs = MAX_IDE_DEVS;
830 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +0000831 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +0000832 max_devs = MAX_SCSI_DEVS;
833 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +0000834 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +0000835 max_devs = 0;
836 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +0000837 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +0000838 max_devs = 0;
839 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +0000840 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +0000841 max_devs = 0;
842 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +0000843 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +0000844 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +0000845 } else if (!strcmp(buf, "virtio")) {
846 type = IF_VIRTIO;
847 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +0000848 } else if (!strcmp(buf, "xen")) {
849 type = IF_XEN;
850 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +0200851 } else if (!strcmp(buf, "none")) {
852 type = IF_NONE;
853 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +0000854 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200855 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200856 return NULL;
thse4bcb142007-12-02 04:51:10 +0000857 }
858 }
859
thse4bcb142007-12-02 04:51:10 +0000860 if (cyls || heads || secs) {
Blue Swirl5afe3f02009-10-17 09:08:47 +0000861 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200862 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200863 return NULL;
thse4bcb142007-12-02 04:51:10 +0000864 }
Blue Swirl5afe3f02009-10-17 09:08:47 +0000865 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200866 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200867 return NULL;
thse4bcb142007-12-02 04:51:10 +0000868 }
Blue Swirl5afe3f02009-10-17 09:08:47 +0000869 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200870 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200871 return NULL;
thse4bcb142007-12-02 04:51:10 +0000872 }
873 }
874
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200875 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +0000876 if (!cyls) {
877 fprintf(stderr,
878 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200879 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200880 return NULL;
thse4bcb142007-12-02 04:51:10 +0000881 }
882 if (!strcmp(buf, "none"))
883 translation = BIOS_ATA_TRANSLATION_NONE;
884 else if (!strcmp(buf, "lba"))
885 translation = BIOS_ATA_TRANSLATION_LBA;
886 else if (!strcmp(buf, "auto"))
887 translation = BIOS_ATA_TRANSLATION_AUTO;
888 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200889 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200890 return NULL;
thse4bcb142007-12-02 04:51:10 +0000891 }
892 }
893
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200894 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +0000895 if (!strcmp(buf, "disk")) {
896 media = MEDIA_DISK;
897 } else if (!strcmp(buf, "cdrom")) {
898 if (cyls || secs || heads) {
899 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200900 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200901 return NULL;
thse4bcb142007-12-02 04:51:10 +0000902 }
903 media = MEDIA_CDROM;
904 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200905 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200906 return NULL;
thse4bcb142007-12-02 04:51:10 +0000907 }
908 }
909
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200910 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
Christoph Hellwig763b6082010-04-06 19:12:04 +0200911 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
912 bdrv_flags |= BDRV_O_NOCACHE;
913 } else if (!strcmp(buf, "writeback")) {
914 bdrv_flags |= BDRV_O_CACHE_WB;
Alexander Graf016f5cf2010-05-26 17:51:49 +0200915 } else if (!strcmp(buf, "unsafe")) {
916 bdrv_flags |= BDRV_O_CACHE_WB;
917 bdrv_flags |= BDRV_O_NO_FLUSH;
Christoph Hellwig763b6082010-04-06 19:12:04 +0200918 } else if (!strcmp(buf, "writethrough")) {
919 /* this is the default */
920 } else {
balrog33f00272007-12-24 14:33:24 +0000921 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200922 return NULL;
balrog33f00272007-12-24 14:33:24 +0000923 }
924 }
925
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200926#ifdef CONFIG_LINUX_AIO
927 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
Christoph Hellwig763b6082010-04-06 19:12:04 +0200928 if (!strcmp(buf, "native")) {
929 bdrv_flags |= BDRV_O_NATIVE_AIO;
930 } else if (!strcmp(buf, "threads")) {
931 /* this is the default */
932 } else {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200933 fprintf(stderr, "qemu: invalid aio option\n");
934 return NULL;
935 }
936 }
937#endif
938
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200939 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +0000940 if (strcmp(buf, "?") == 0) {
941 fprintf(stderr, "qemu: Supported formats:");
942 bdrv_iterate_format(bdrv_format_print, NULL);
943 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200944 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +0000945 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100946 drv = bdrv_find_whitelisted_format(buf);
aurel321e72d3b2008-04-28 20:26:45 +0000947 if (!drv) {
948 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200949 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +0000950 }
951 }
952
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100953 on_write_error = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200954 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Gerd Hoffmanndc33bb32010-05-26 10:44:44 +0200955 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
aliguoriea8a5d72009-01-22 19:52:21 +0000956 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200957 return NULL;
aliguori428c5702009-01-21 18:59:04 +0000958 }
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100959
960 on_write_error = parse_block_error_action(buf, 0);
961 if (on_write_error < 0) {
962 return NULL;
963 }
964 }
965
966 on_read_error = BLOCK_ERR_REPORT;
967 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Gerd Hoffmanndc33bb32010-05-26 10:44:44 +0200968 if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100969 fprintf(stderr, "rerror is no supported by this format\n");
970 return NULL;
971 }
972
973 on_read_error = parse_block_error_action(buf, 1);
974 if (on_read_error < 0) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200975 return NULL;
aliguori428c5702009-01-21 18:59:04 +0000976 }
977 }
978
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200979 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200980 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200981 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200982 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200983 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200984 }
985
thse4bcb142007-12-02 04:51:10 +0000986 /* compute bus and unit according index */
987
988 if (index != -1) {
989 if (bus_id != 0 || unit_id != -1) {
990 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200991 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200992 return NULL;
thse4bcb142007-12-02 04:51:10 +0000993 }
994 if (max_devs == 0)
995 {
996 unit_id = index;
997 bus_id = 0;
998 } else {
999 unit_id = index % max_devs;
1000 bus_id = index / max_devs;
1001 }
1002 }
1003
1004 /* if user doesn't specify a unit_id,
1005 * try to find the first free
1006 */
1007
1008 if (unit_id == -1) {
1009 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001010 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00001011 unit_id++;
1012 if (max_devs && unit_id >= max_devs) {
1013 unit_id -= max_devs;
1014 bus_id++;
1015 }
1016 }
1017 }
1018
1019 /* check unit id */
1020
1021 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001022 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1023 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001024 return NULL;
thse4bcb142007-12-02 04:51:10 +00001025 }
1026
1027 /*
1028 * ignore multiple definitions
1029 */
1030
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001031 if (drive_get(type, bus_id, unit_id) != NULL) {
1032 *fatal_error = 0;
1033 return NULL;
1034 }
thse4bcb142007-12-02 04:51:10 +00001035
1036 /* init */
1037
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001038 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02001039 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001040 dinfo->id = qemu_strdup(buf);
1041 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001042 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001043 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001044 if (type == IF_IDE || type == IF_SCSI)
1045 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1046 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001047 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001048 devname, bus_id, mediastr, unit_id);
1049 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001050 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001051 devname, mediastr, unit_id);
1052 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001053 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001054 dinfo->devaddr = devaddr;
1055 dinfo->type = type;
1056 dinfo->bus = bus_id;
1057 dinfo->unit = unit_id;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001058 dinfo->on_read_error = on_read_error;
1059 dinfo->on_write_error = on_write_error;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001060 dinfo->opts = opts;
1061 if (serial)
1062 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00001063 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00001064
thsf60d39b2007-12-17 03:55:57 +00001065 switch(type) {
thse4bcb142007-12-02 04:51:10 +00001066 case IF_IDE:
1067 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00001068 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00001069 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00001070 switch(media) {
1071 case MEDIA_DISK:
1072 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001073 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1074 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00001075 }
1076 break;
1077 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001078 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00001079 break;
1080 }
1081 break;
1082 case IF_SD:
1083 /* FIXME: This isn't really a floppy, but it's a reasonable
1084 approximation. */
1085 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001086 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00001087 break;
1088 case IF_PFLASH:
1089 case IF_MTD:
1090 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02001091 case IF_VIRTIO:
1092 /* add virtio block device */
1093 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1094 qemu_opt_set(opts, "driver", "virtio-blk-pci");
1095 qemu_opt_set(opts, "drive", dinfo->id);
1096 if (devaddr)
1097 qemu_opt_set(opts, "addr", devaddr);
1098 break;
Paul Brookaae94602009-05-14 22:35:06 +01001099 case IF_COUNT:
1100 abort();
thse4bcb142007-12-02 04:51:10 +00001101 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001102 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001103 *fatal_error = 0;
1104 return NULL;
1105 }
aliguori9f7965c2008-10-14 14:42:54 +00001106 if (snapshot) {
Alexander Grafc3177282010-05-26 21:04:32 +02001107 /* always use cache=unsafe with snapshot */
Christoph Hellwig763b6082010-04-06 19:12:04 +02001108 bdrv_flags &= ~BDRV_O_CACHE_MASK;
Alexander Grafc3177282010-05-26 21:04:32 +02001109 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001110 }
1111
Naphtali Sprei2db7ad52010-03-23 12:17:16 +02001112 if (media == MEDIA_CDROM) {
1113 /* CDROM is fine for any interface, don't check. */
1114 ro = 1;
1115 } else if (ro == 1) {
Gerd Hoffmanndc33bb32010-05-26 10:44:44 +02001116 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
Naphtali Spreif5edb012010-01-17 16:48:13 +02001117 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
Naphtali Sprei59f26892009-10-26 16:25:16 +02001118 return NULL;
1119 }
Naphtali Sprei59f26892009-10-26 16:25:16 +02001120 }
Naphtali Sprei2db7ad52010-03-23 12:17:16 +02001121
Naphtali Spreif5edb012010-01-17 16:48:13 +02001122 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001123
Kevin Wolfd6e90982010-03-31 14:40:27 +02001124 if (bdrv_open(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05001125 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1126 file, strerror(errno));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001127 return NULL;
thse4bcb142007-12-02 04:51:10 +00001128 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001129
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001130 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00001131 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001132 *fatal_error = 0;
1133 return dinfo;
thse4bcb142007-12-02 04:51:10 +00001134}
1135
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001136static int drive_init_func(QemuOpts *opts, void *opaque)
1137{
1138 QEMUMachine *machine = opaque;
1139 int fatal_error = 0;
1140
1141 if (drive_init(opts, machine, &fatal_error) == NULL) {
1142 if (fatal_error)
1143 return 1;
1144 }
1145 return 0;
1146}
1147
1148static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1149{
1150 if (NULL == qemu_opt_get(opts, "snapshot")) {
1151 qemu_opt_set(opts, "snapshot", "on");
1152 }
1153 return 0;
1154}
1155
Jan Kiszka76e30d02009-07-02 00:19:02 +02001156void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1157{
1158 boot_set_handler = func;
1159 boot_set_opaque = opaque;
1160}
1161
1162int qemu_boot_set(const char *boot_devices)
1163{
1164 if (!boot_set_handler) {
1165 return -EINVAL;
1166 }
1167 return boot_set_handler(boot_set_opaque, boot_devices);
1168}
1169
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03001170static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001171{
1172 /* We just do some generic consistency checks */
1173 const char *p;
1174 int bitmap = 0;
1175
1176 for (p = devices; *p != '\0'; p++) {
1177 /* Allowed boot devices are:
1178 * a-b: floppy disk drives
1179 * c-f: IDE disk drives
1180 * g-m: machine implementation dependant drives
1181 * n-p: network devices
1182 * It's up to each machine implementation to check if the given boot
1183 * devices match the actual hardware implementation and firmware
1184 * features.
1185 */
1186 if (*p < 'a' || *p > 'p') {
1187 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1188 exit(1);
1189 }
1190 if (bitmap & (1 << (*p - 'a'))) {
1191 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1192 exit(1);
1193 }
1194 bitmap |= 1 << (*p - 'a');
1195 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001196}
1197
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001198static void restore_boot_devices(void *opaque)
1199{
1200 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -06001201 static int first = 1;
1202
1203 /* Restore boot order and remove ourselves after the first boot */
1204 if (first) {
1205 first = 0;
1206 return;
1207 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001208
1209 qemu_boot_set(standard_boot_devices);
1210
1211 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1212 qemu_free(standard_boot_devices);
1213}
1214
aliguori268a3622009-04-21 22:30:27 +00001215static void numa_add(const char *optarg)
1216{
1217 char option[128];
1218 char *endptr;
1219 unsigned long long value, endvalue;
1220 int nodenr;
1221
1222 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1223 if (!strcmp(option, "node")) {
1224 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1225 nodenr = nb_numa_nodes;
1226 } else {
1227 nodenr = strtoull(option, NULL, 10);
1228 }
1229
1230 if (get_param_value(option, 128, "mem", optarg) == 0) {
1231 node_mem[nodenr] = 0;
1232 } else {
1233 value = strtoull(option, &endptr, 0);
1234 switch (*endptr) {
1235 case 0: case 'M': case 'm':
1236 value <<= 20;
1237 break;
1238 case 'G': case 'g':
1239 value <<= 30;
1240 break;
1241 }
1242 node_mem[nodenr] = value;
1243 }
1244 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1245 node_cpumask[nodenr] = 0;
1246 } else {
1247 value = strtoull(option, &endptr, 10);
1248 if (value >= 64) {
1249 value = 63;
1250 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1251 } else {
1252 if (*endptr == '-') {
1253 endvalue = strtoull(endptr+1, &endptr, 10);
1254 if (endvalue >= 63) {
1255 endvalue = 62;
1256 fprintf(stderr,
1257 "only 63 CPUs in NUMA mode supported.\n");
1258 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01001259 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +00001260 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01001261 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +00001262 }
1263 }
1264 node_cpumask[nodenr] = value;
1265 }
1266 nb_numa_nodes++;
1267 }
1268 return;
1269}
1270
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001271static void smp_parse(const char *optarg)
1272{
1273 int smp, sockets = 0, threads = 0, cores = 0;
1274 char *endptr;
1275 char option[128];
1276
1277 smp = strtoul(optarg, &endptr, 10);
1278 if (endptr != optarg) {
1279 if (*endptr == ',') {
1280 endptr++;
1281 }
1282 }
1283 if (get_param_value(option, 128, "sockets", endptr) != 0)
1284 sockets = strtoull(option, NULL, 10);
1285 if (get_param_value(option, 128, "cores", endptr) != 0)
1286 cores = strtoull(option, NULL, 10);
1287 if (get_param_value(option, 128, "threads", endptr) != 0)
1288 threads = strtoull(option, NULL, 10);
1289 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1290 max_cpus = strtoull(option, NULL, 10);
1291
1292 /* compute missing values, prefer sockets over cores over threads */
1293 if (smp == 0 || sockets == 0) {
1294 sockets = sockets > 0 ? sockets : 1;
1295 cores = cores > 0 ? cores : 1;
1296 threads = threads > 0 ? threads : 1;
1297 if (smp == 0) {
1298 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001299 }
1300 } else {
1301 if (cores == 0) {
1302 threads = threads > 0 ? threads : 1;
1303 cores = smp / (sockets * threads);
1304 } else {
Amit Shah5cdc9b72010-01-13 16:24:42 +05301305 if (sockets) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001306 threads = smp / (cores * sockets);
1307 }
1308 }
1309 }
1310 smp_cpus = smp;
1311 smp_cores = cores > 0 ? cores : 1;
1312 smp_threads = threads > 0 ? threads : 1;
1313 if (max_cpus == 0)
1314 max_cpus = smp_cpus;
1315}
1316
bellard330d0412003-07-26 18:11:40 +00001317/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001318/* USB devices */
1319
aliguoric0f4ce72009-03-05 23:01:01 +00001320static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00001321{
1322 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001323 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001324
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001325 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00001326 return -1;
1327
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001328 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1329 dev = usbdevice_create(devname);
1330 if (dev)
1331 goto done;
1332
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001333 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00001334 if (strstart(devname, "host:", &p)) {
1335 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +00001336 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1337 dev = usb_bt_init(devname[2] ? hci_init(p) :
1338 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001339 } else {
1340 return -1;
1341 }
pbrook0d92ed32006-05-21 16:30:15 +00001342 if (!dev)
1343 return -1;
1344
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001345done:
bellarda594cfb2005-11-06 16:13:29 +00001346 return 0;
1347}
1348
aliguori1f3870a2008-08-21 19:27:48 +00001349static int usb_device_del(const char *devname)
1350{
1351 int bus_num, addr;
1352 const char *p;
1353
aliguori5d0c5752008-09-14 01:07:41 +00001354 if (strstart(devname, "host:", &p))
1355 return usb_host_device_close(p);
1356
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001357 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00001358 return -1;
1359
1360 p = strchr(devname, '.');
1361 if (!p)
1362 return -1;
1363 bus_num = strtoul(devname, NULL, 0);
1364 addr = strtoul(p + 1, NULL, 0);
1365
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001366 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001367}
1368
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001369static int usb_parse(const char *cmdline)
1370{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001371 int r;
1372 r = usb_device_add(cmdline, 0);
1373 if (r < 0) {
1374 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1375 }
1376 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001377}
1378
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001379void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001380{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001381 const char *devname = qdict_get_str(qdict, "devname");
1382 if (usb_device_add(devname, 1) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001383 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001384 }
bellarda594cfb2005-11-06 16:13:29 +00001385}
1386
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001387void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001388{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001389 const char *devname = qdict_get_str(qdict, "devname");
1390 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001391 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001392 }
bellarda594cfb2005-11-06 16:13:29 +00001393}
1394
bellardf7cce892004-12-08 22:21:25 +00001395/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001396/* PCMCIA/Cardbus */
1397
1398static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001399 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001400 struct pcmcia_socket_entry_s *next;
1401} *pcmcia_sockets = 0;
1402
Paul Brookbc24a222009-05-10 01:44:56 +01001403void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001404{
1405 struct pcmcia_socket_entry_s *entry;
1406
1407 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1408 entry->socket = socket;
1409 entry->next = pcmcia_sockets;
1410 pcmcia_sockets = entry;
1411}
1412
Paul Brookbc24a222009-05-10 01:44:56 +01001413void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001414{
1415 struct pcmcia_socket_entry_s *entry, **ptr;
1416
1417 ptr = &pcmcia_sockets;
1418 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1419 if (entry->socket == socket) {
1420 *ptr = entry->next;
1421 qemu_free(entry);
1422 }
1423}
1424
aliguori376253e2009-03-05 23:01:23 +00001425void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001426{
1427 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001428
balrog201a51f2007-04-30 00:51:09 +00001429 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001430 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001431
1432 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001433 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1434 iter->socket->attached ? iter->socket->card_string :
1435 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001436}
1437
1438/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001439/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00001440
bellardc4b1fcc2004-03-14 21:44:30 +00001441typedef struct IOHandlerRecord {
1442 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +01001443 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +00001444 IOHandler *fd_read;
1445 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00001446 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00001447 void *opaque;
1448 /* temporary data */
1449 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +01001450 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +00001451} IOHandlerRecord;
1452
Juan Quintela31d4ee62010-03-11 17:55:37 +01001453static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1454 QLIST_HEAD_INITIALIZER(io_handlers);
1455
bellardc4b1fcc2004-03-14 21:44:30 +00001456
bellard7c9d8e02005-11-15 22:16:05 +00001457/* XXX: fd_read_poll should be suppressed, but an API change is
1458 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00001459int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +01001460 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +00001461 IOHandler *fd_read,
1462 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001463 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00001464{
Juan Quintela31d4ee62010-03-11 17:55:37 +01001465 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +00001466
bellard7c9d8e02005-11-15 22:16:05 +00001467 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001468 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001469 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00001470 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00001471 break;
1472 }
bellard8a7ddc32004-03-31 19:00:16 +00001473 }
bellard7c9d8e02005-11-15 22:16:05 +00001474 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001475 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001476 if (ioh->fd == fd)
1477 goto found;
1478 }
1479 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +01001480 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +00001481 found:
1482 ioh->fd = fd;
1483 ioh->fd_read_poll = fd_read_poll;
1484 ioh->fd_read = fd_read;
1485 ioh->fd_write = fd_write;
1486 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00001487 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00001488 }
bellard7c9d8e02005-11-15 22:16:05 +00001489 return 0;
1490}
1491
ths5fafdf22007-09-16 21:08:06 +00001492int qemu_set_fd_handler(int fd,
1493 IOHandler *fd_read,
1494 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001495 void *opaque)
1496{
1497 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00001498}
1499
bellard8a7ddc32004-03-31 19:00:16 +00001500/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001501/* machine registration */
1502
blueswir1bdaf78e2008-10-04 07:24:27 +00001503static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001504QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001505
1506int qemu_register_machine(QEMUMachine *m)
1507{
1508 QEMUMachine **pm;
1509 pm = &first_machine;
1510 while (*pm != NULL)
1511 pm = &(*pm)->next;
1512 m->next = NULL;
1513 *pm = m;
1514 return 0;
1515}
1516
pbrook9596ebb2007-11-18 01:44:38 +00001517static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001518{
1519 QEMUMachine *m;
1520
1521 for(m = first_machine; m != NULL; m = m->next) {
1522 if (!strcmp(m->name, name))
1523 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001524 if (m->alias && !strcmp(m->alias, name))
1525 return m;
bellardcc1daa42005-06-05 14:49:17 +00001526 }
1527 return NULL;
1528}
1529
Anthony Liguori0c257432009-05-21 20:41:01 -05001530static QEMUMachine *find_default_machine(void)
1531{
1532 QEMUMachine *m;
1533
1534 for(m = first_machine; m != NULL; m = m->next) {
1535 if (m->is_default) {
1536 return m;
1537 }
1538 }
1539 return NULL;
1540}
1541
bellardcc1daa42005-06-05 14:49:17 +00001542/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001543/* main execution loop */
1544
pbrook9596ebb2007-11-18 01:44:38 +00001545static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001546{
aliguori7d957bd2009-01-15 22:14:11 +00001547 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001548 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001549 DisplayChangeListener *dcl = ds->listeners;
1550
Sheng Yang62a27442010-01-26 19:21:16 +08001551 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001552 dpy_refresh(ds);
1553
1554 while (dcl != NULL) {
1555 if (dcl->gui_timer_interval &&
1556 dcl->gui_timer_interval < interval)
1557 interval = dcl->gui_timer_interval;
1558 dcl = dcl->next;
1559 }
1560 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001561}
1562
blueswir19043b622009-01-21 19:28:13 +00001563static void nographic_update(void *opaque)
1564{
1565 uint64_t interval = GUI_REFRESH_INTERVAL;
1566
Sheng Yang62a27442010-01-26 19:21:16 +08001567 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00001568 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1569}
1570
bellard0bd48852005-11-11 00:00:47 +00001571struct vm_change_state_entry {
1572 VMChangeStateHandler *cb;
1573 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001574 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001575};
1576
Blue Swirl72cf2d42009-09-12 07:36:22 +00001577static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001578
1579VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1580 void *opaque)
1581{
1582 VMChangeStateEntry *e;
1583
1584 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001585
1586 e->cb = cb;
1587 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001588 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001589 return e;
1590}
1591
1592void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1593{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001594 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001595 qemu_free (e);
1596}
1597
Blue Swirl296af7c2010-03-29 19:23:50 +00001598void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001599{
1600 VMChangeStateEntry *e;
1601
1602 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001603 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001604 }
1605}
1606
bellard8a7ddc32004-03-31 19:00:16 +00001607void vm_start(void)
1608{
1609 if (!vm_running) {
1610 cpu_enable_ticks();
1611 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001612 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001613 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001614 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001615 }
1616}
1617
bellardbb0c6722004-06-20 12:37:32 +00001618/* reset/shutdown handler */
1619
1620typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001621 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001622 QEMUResetHandler *func;
1623 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001624} QEMUResetEntry;
1625
Blue Swirl72cf2d42009-09-12 07:36:22 +00001626static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1627 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001628static int reset_requested;
1629static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00001630static int powerdown_requested;
Blue Swirl296af7c2010-03-29 19:23:50 +00001631int debug_requested;
Blue Swirl7277e022010-04-12 17:19:06 +00001632int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001633
aurel32cf7a2fe2008-03-18 06:53:05 +00001634int qemu_shutdown_requested(void)
1635{
1636 int r = shutdown_requested;
1637 shutdown_requested = 0;
1638 return r;
1639}
1640
1641int qemu_reset_requested(void)
1642{
1643 int r = reset_requested;
1644 reset_requested = 0;
1645 return r;
1646}
1647
1648int qemu_powerdown_requested(void)
1649{
1650 int r = powerdown_requested;
1651 powerdown_requested = 0;
1652 return r;
1653}
1654
aliguorie5689022009-04-24 18:03:54 +00001655static int qemu_debug_requested(void)
1656{
1657 int r = debug_requested;
1658 debug_requested = 0;
1659 return r;
1660}
1661
aliguori6e29f5d2009-04-24 18:04:02 +00001662static int qemu_vmstop_requested(void)
1663{
1664 int r = vmstop_requested;
1665 vmstop_requested = 0;
1666 return r;
1667}
1668
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001669void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001670{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001671 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001672
bellardbb0c6722004-06-20 12:37:32 +00001673 re->func = func;
1674 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001675 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001676}
1677
Jan Kiszkadda9b292009-07-02 00:19:02 +02001678void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001679{
1680 QEMUResetEntry *re;
1681
Blue Swirl72cf2d42009-09-12 07:36:22 +00001682 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001683 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001684 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001685 qemu_free(re);
1686 return;
1687 }
1688 }
1689}
1690
1691void qemu_system_reset(void)
1692{
1693 QEMUResetEntry *re, *nre;
1694
1695 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001696 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001697 re->func(re->opaque);
1698 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001699 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001700 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001701}
1702
1703void qemu_system_reset_request(void)
1704{
bellardd1beab82006-10-02 19:44:22 +00001705 if (no_reboot) {
1706 shutdown_requested = 1;
1707 } else {
1708 reset_requested = 1;
1709 }
aliguorid9f75a42009-04-24 18:03:11 +00001710 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001711}
1712
1713void qemu_system_shutdown_request(void)
1714{
1715 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001716 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001717}
1718
bellard34751872005-07-02 14:31:34 +00001719void qemu_system_powerdown_request(void)
1720{
1721 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001722 qemu_notify_event();
1723}
1724
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001725void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001726{
1727 IOHandlerRecord *ioh;
1728 fd_set rfds, wfds, xfds;
1729 int ret, nfds;
1730 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001731 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001732
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001733 if (nonblocking)
1734 timeout = 0;
1735 else {
1736 timeout = qemu_calculate_timeout();
1737 qemu_bh_update_timeout(&timeout);
1738 }
aliguori56f3a5d2008-10-31 18:07:17 +00001739
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001740 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001741
bellardfd1dff42006-02-01 21:29:26 +00001742 /* poll any events */
1743 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001744 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001745 FD_ZERO(&rfds);
1746 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001747 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001748 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00001749 if (ioh->deleted)
1750 continue;
bellardfd1dff42006-02-01 21:29:26 +00001751 if (ioh->fd_read &&
1752 (!ioh->fd_read_poll ||
1753 ioh->fd_read_poll(ioh->opaque) != 0)) {
1754 FD_SET(ioh->fd, &rfds);
1755 if (ioh->fd > nfds)
1756 nfds = ioh->fd;
1757 }
1758 if (ioh->fd_write) {
1759 FD_SET(ioh->fd, &wfds);
1760 if (ioh->fd > nfds)
1761 nfds = ioh->fd;
1762 }
1763 }
ths3b46e622007-09-17 08:09:54 +00001764
aliguori56f3a5d2008-10-31 18:07:17 +00001765 tv.tv_sec = timeout / 1000;
1766 tv.tv_usec = (timeout % 1000) * 1000;
1767
Jan Kiszkad918f232009-06-24 14:42:30 +02001768 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1769
aliguori48708522009-04-24 18:03:49 +00001770 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001771 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001772 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00001773 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001774 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00001775
Juan Quintela31d4ee62010-03-11 17:55:37 +01001776 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
thscafffd42007-02-28 21:59:44 +00001777 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001778 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00001779 qemu_free(ioh);
Juan Quintela4bed9832010-03-11 17:55:41 +01001780 continue;
1781 }
1782 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1783 ioh->fd_read(ioh->opaque);
1784 }
1785 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1786 ioh->fd_write(ioh->opaque);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001787 }
thscafffd42007-02-28 21:59:44 +00001788 }
bellardfd1dff42006-02-01 21:29:26 +00001789 }
Jan Kiszkad918f232009-06-24 14:42:30 +02001790
1791 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001792
Paolo Bonzinib6964822010-03-10 11:38:45 +01001793 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001794
pbrook423f0742007-05-23 00:06:54 +00001795 /* Check bottom-halves last in case any of the earlier events triggered
1796 them. */
1797 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001798
bellard5905b2e2004-08-01 21:53:26 +00001799}
1800
aliguori43b96852009-04-24 18:03:33 +00001801static int vm_can_run(void)
1802{
1803 if (powerdown_requested)
1804 return 0;
1805 if (reset_requested)
1806 return 0;
1807 if (shutdown_requested)
1808 return 0;
aliguorie5689022009-04-24 18:03:54 +00001809 if (debug_requested)
1810 return 0;
aliguori43b96852009-04-24 18:03:33 +00001811 return 1;
1812}
1813
Blue Swirld9c32312009-08-09 08:42:19 +00001814qemu_irq qemu_system_powerdown;
1815
aliguori43b96852009-04-24 18:03:33 +00001816static void main_loop(void)
1817{
aliguori6e29f5d2009-04-24 18:04:02 +00001818 int r;
aliguori43b96852009-04-24 18:03:33 +00001819
Blue Swirl7277e022010-04-12 17:19:06 +00001820 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001821
aliguori6e29f5d2009-04-24 18:04:02 +00001822 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00001823 do {
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001824 bool nonblocking = false;
aliguorie6e35b12009-04-24 18:03:57 +00001825#ifdef CONFIG_PROFILER
1826 int64_t ti;
1827#endif
aliguorid6dc3d42009-04-24 18:04:07 +00001828#ifndef CONFIG_IOTHREAD
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001829 nonblocking = tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00001830#endif
aliguori43b96852009-04-24 18:03:33 +00001831#ifdef CONFIG_PROFILER
1832 ti = profile_getclock();
1833#endif
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001834 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001835#ifdef CONFIG_PROFILER
1836 dev_time += profile_getclock() - ti;
1837#endif
aliguorie5689022009-04-24 18:03:54 +00001838 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00001839
Blue Swirl54fc6ea2010-03-29 19:23:46 +00001840 if ((r = qemu_debug_requested())) {
1841 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001842 }
aliguori43b96852009-04-24 18:03:33 +00001843 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001844 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001845 if (no_shutdown) {
1846 vm_stop(0);
1847 no_shutdown = 0;
1848 } else
1849 break;
1850 }
aliguorid6dc3d42009-04-24 18:04:07 +00001851 if (qemu_reset_requested()) {
1852 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00001853 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00001854 resume_all_vcpus();
1855 }
Blue Swirld9c32312009-08-09 08:42:19 +00001856 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001857 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001858 qemu_irq_raise(qemu_system_powerdown);
1859 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001860 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001861 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001862 }
aliguori43b96852009-04-24 18:03:33 +00001863 }
aliguorid6dc3d42009-04-24 18:04:07 +00001864 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001865}
1866
pbrook9bd7e6d2009-04-07 22:58:45 +00001867static void version(void)
1868{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001869 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001870}
1871
ths15f82202007-06-29 23:26:08 +00001872static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001873{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001874 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001875#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1876 opt_help
blueswir15824d652009-03-28 06:44:27 +00001877#define DEFHEADING(text) stringify(text) "\n"
1878#include "qemu-options.h"
1879#undef DEF
1880#undef DEFHEADING
1881#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001882 ;
1883 version();
1884 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001885 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001886 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001887 "\n"
1888 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001889 "During emulation, the following keys are useful:\n"
1890 "ctrl-alt-f toggle full screen\n"
1891 "ctrl-alt-n switch to virtual console 'n'\n"
1892 "ctrl-alt toggle mouse and keyboard grab\n"
1893 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001894 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1895 "qemu",
1896 options_help);
ths15f82202007-06-29 23:26:08 +00001897 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001898}
1899
bellardcd6f1162004-05-13 22:02:20 +00001900#define HAS_ARG 0x0001
1901
1902enum {
Blue Swirlad960902010-03-29 19:23:52 +00001903#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
blueswir15824d652009-03-28 06:44:27 +00001904 opt_enum,
1905#define DEFHEADING(text)
1906#include "qemu-options.h"
1907#undef DEF
1908#undef DEFHEADING
1909#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001910};
1911
1912typedef struct QEMUOption {
1913 const char *name;
1914 int flags;
1915 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001916 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001917} QEMUOption;
1918
blueswir1dbed7e42008-10-01 19:38:09 +00001919static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001920 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1921#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1922 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001923#define DEFHEADING(text)
1924#include "qemu-options.h"
1925#undef DEF
1926#undef DEFHEADING
1927#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001928 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001929};
malc3893c122008-09-28 00:42:05 +00001930static void select_vgahw (const char *p)
1931{
1932 const char *opts;
1933
Gerd Hoffmann64465292009-12-08 13:11:45 +01001934 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001935 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001936 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001937 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001938 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001939 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001940 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001941 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001942 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001943 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00001944 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001945 invalid_vga:
1946 fprintf(stderr, "Unknown vga type: %s\n", p);
1947 exit(1);
1948 }
malccb5a7aa2008-09-28 00:42:12 +00001949 while (*opts) {
1950 const char *nextopt;
1951
1952 if (strstart(opts, ",retrace=", &nextopt)) {
1953 opts = nextopt;
1954 if (strstart(opts, "dumb", &nextopt))
1955 vga_retrace_method = VGA_RETRACE_DUMB;
1956 else if (strstart(opts, "precise", &nextopt))
1957 vga_retrace_method = VGA_RETRACE_PRECISE;
1958 else goto invalid_vga;
1959 } else goto invalid_vga;
1960 opts = nextopt;
1961 }
malc3893c122008-09-28 00:42:05 +00001962}
1963
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001964static int balloon_parse(const char *arg)
1965{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001966 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001967
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001968 if (strcmp(arg, "none") == 0) {
1969 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001970 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001971
1972 if (!strncmp(arg, "virtio", 6)) {
1973 if (arg[6] == ',') {
1974 /* have params -> parse them */
Markus Armbruster8212c642010-02-10 19:52:18 +01001975 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001976 if (!opts)
1977 return -1;
1978 } else {
1979 /* create empty opts */
1980 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1981 }
1982 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1983 return 0;
1984 }
1985
1986 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001987}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001988
aliguori5b08fc12008-08-21 20:08:03 +00001989#ifndef _WIN32
1990
1991static void termsig_handler(int signal)
1992{
1993 qemu_system_shutdown_request();
1994}
1995
Jan Kiszka7c3370d2009-05-08 12:34:17 +02001996static void sigchld_handler(int signal)
1997{
1998 waitpid(-1, NULL, WNOHANG);
1999}
2000
2001static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00002002{
2003 struct sigaction act;
2004
2005 memset(&act, 0, sizeof(act));
2006 act.sa_handler = termsig_handler;
2007 sigaction(SIGINT, &act, NULL);
2008 sigaction(SIGHUP, &act, NULL);
2009 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02002010
2011 act.sa_handler = sigchld_handler;
2012 act.sa_flags = SA_NOCLDSTOP;
2013 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00002014}
2015
2016#endif
2017
Paul Brook5cea8592009-05-30 00:52:44 +01002018#ifdef _WIN32
2019/* Look for support files in the same directory as the executable. */
2020static char *find_datadir(const char *argv0)
2021{
2022 char *p;
2023 char buf[MAX_PATH];
2024 DWORD len;
2025
2026 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2027 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03002028 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01002029 }
2030
2031 buf[len] = 0;
2032 p = buf + len - 1;
2033 while (p != buf && *p != '\\')
2034 p--;
2035 *p = 0;
2036 if (access(buf, R_OK) == 0) {
2037 return qemu_strdup(buf);
2038 }
2039 return NULL;
2040}
2041#else /* !_WIN32 */
2042
2043/* Find a likely location for support files using the location of the binary.
2044 For installed binaries this will be "$bindir/../share/qemu". When
2045 running from the build tree this will be "$bindir/../pc-bios". */
2046#define SHARE_SUFFIX "/share/qemu"
2047#define BUILD_SUFFIX "/pc-bios"
2048static char *find_datadir(const char *argv0)
2049{
2050 char *dir;
2051 char *p = NULL;
2052 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01002053 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00002054 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01002055
2056#if defined(__linux__)
2057 {
2058 int len;
2059 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2060 if (len > 0) {
2061 buf[len] = 0;
2062 p = buf;
2063 }
2064 }
2065#elif defined(__FreeBSD__)
2066 {
Juergen Lock92c0e652010-03-25 22:07:12 +01002067 static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
2068 size_t len = sizeof(buf) - 1;
2069
2070 *buf = '\0';
2071 if (!sysctl(mib, sizeof(mib)/sizeof(*mib), buf, &len, NULL, 0) &&
2072 *buf) {
2073 buf[sizeof(buf) - 1] = '\0';
Paul Brook5cea8592009-05-30 00:52:44 +01002074 p = buf;
2075 }
2076 }
2077#endif
2078 /* If we don't have any way of figuring out the actual executable
2079 location then try argv[0]. */
2080 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02002081 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01002082 if (!p) {
2083 return NULL;
2084 }
2085 }
2086 dir = dirname(p);
2087 dir = dirname(dir);
2088
Blue Swirl3a417592009-06-09 19:12:21 +00002089 max_len = strlen(dir) +
2090 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
2091 res = qemu_mallocz(max_len);
2092 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01002093 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00002094 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01002095 if (access(res, R_OK)) {
2096 qemu_free(res);
2097 res = NULL;
2098 }
2099 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02002100
Paul Brook5cea8592009-05-30 00:52:44 +01002101 return res;
2102}
2103#undef SHARE_SUFFIX
2104#undef BUILD_SUFFIX
2105#endif
2106
2107char *qemu_find_file(int type, const char *name)
2108{
2109 int len;
2110 const char *subdir;
2111 char *buf;
2112
2113 /* If name contains path separators then try it as a straight path. */
2114 if ((strchr(name, '/') || strchr(name, '\\'))
2115 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02002116 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01002117 }
2118 switch (type) {
2119 case QEMU_FILE_TYPE_BIOS:
2120 subdir = "";
2121 break;
2122 case QEMU_FILE_TYPE_KEYMAP:
2123 subdir = "keymaps/";
2124 break;
2125 default:
2126 abort();
2127 }
2128 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2129 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00002130 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01002131 if (access(buf, R_OK)) {
2132 qemu_free(buf);
2133 return NULL;
2134 }
2135 return buf;
2136}
2137
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002138static int device_help_func(QemuOpts *opts, void *opaque)
2139{
2140 return qdev_device_help(opts);
2141}
2142
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002143static int device_init_func(QemuOpts *opts, void *opaque)
2144{
2145 DeviceState *dev;
2146
2147 dev = qdev_device_add(opts);
2148 if (!dev)
2149 return -1;
2150 return 0;
2151}
2152
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002153static int chardev_init_func(QemuOpts *opts, void *opaque)
2154{
2155 CharDriverState *chr;
2156
2157 chr = qemu_chr_open_opts(opts, NULL);
2158 if (!chr)
2159 return -1;
2160 return 0;
2161}
2162
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302163#ifdef CONFIG_LINUX
2164static int fsdev_init_func(QemuOpts *opts, void *opaque)
2165{
2166 int ret;
2167 ret = qemu_fsdev_add(opts);
2168
2169 return ret;
2170}
2171#endif
2172
Gerd Hoffmann88589342009-12-08 13:11:50 +01002173static int mon_init_func(QemuOpts *opts, void *opaque)
2174{
2175 CharDriverState *chr;
2176 const char *chardev;
2177 const char *mode;
2178 int flags;
2179
2180 mode = qemu_opt_get(opts, "mode");
2181 if (mode == NULL) {
2182 mode = "readline";
2183 }
2184 if (strcmp(mode, "readline") == 0) {
2185 flags = MONITOR_USE_READLINE;
2186 } else if (strcmp(mode, "control") == 0) {
2187 flags = MONITOR_USE_CONTROL;
2188 } else {
2189 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2190 exit(1);
2191 }
2192
2193 if (qemu_opt_get_bool(opts, "default", 0))
2194 flags |= MONITOR_IS_DEFAULT;
2195
2196 chardev = qemu_opt_get(opts, "chardev");
2197 chr = qemu_chr_find(chardev);
2198 if (chr == NULL) {
2199 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2200 exit(1);
2201 }
2202
2203 monitor_init(chr, flags);
2204 return 0;
2205}
2206
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002207static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002208{
2209 static int monitor_device_index = 0;
2210 QemuOpts *opts;
2211 const char *p;
2212 char label[32];
2213 int def = 0;
2214
2215 if (strstart(optarg, "chardev:", &p)) {
2216 snprintf(label, sizeof(label), "%s", p);
2217 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02002218 snprintf(label, sizeof(label), "compat_monitor%d",
2219 monitor_device_index);
2220 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01002221 def = 1;
2222 }
2223 opts = qemu_chr_parse_compat(label, optarg);
2224 if (!opts) {
2225 fprintf(stderr, "parse error: %s\n", optarg);
2226 exit(1);
2227 }
2228 }
2229
2230 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
2231 if (!opts) {
2232 fprintf(stderr, "duplicate chardev: %s\n", label);
2233 exit(1);
2234 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002235 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002236 qemu_opt_set(opts, "chardev", label);
2237 if (def)
2238 qemu_opt_set(opts, "default", "on");
2239 monitor_device_index++;
2240}
2241
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002242struct device_config {
2243 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002244 DEV_USB, /* -usbdevice */
2245 DEV_BT, /* -bt */
2246 DEV_SERIAL, /* -serial */
2247 DEV_PARALLEL, /* -parallel */
2248 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002249 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002250 } type;
2251 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002252 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002253};
Blue Swirl72cf2d42009-09-12 07:36:22 +00002254QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002255
2256static void add_device_config(int type, const char *cmdline)
2257{
2258 struct device_config *conf;
2259
2260 conf = qemu_mallocz(sizeof(*conf));
2261 conf->type = type;
2262 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002263 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002264}
2265
2266static int foreach_device_config(int type, int (*func)(const char *cmdline))
2267{
2268 struct device_config *conf;
2269 int rc;
2270
Blue Swirl72cf2d42009-09-12 07:36:22 +00002271 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002272 if (conf->type != type)
2273 continue;
2274 rc = func(conf->cmdline);
2275 if (0 != rc)
2276 return rc;
2277 }
2278 return 0;
2279}
2280
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002281static int serial_parse(const char *devname)
2282{
2283 static int index = 0;
2284 char label[32];
2285
2286 if (strcmp(devname, "none") == 0)
2287 return 0;
2288 if (index == MAX_SERIAL_PORTS) {
2289 fprintf(stderr, "qemu: too many serial ports\n");
2290 exit(1);
2291 }
2292 snprintf(label, sizeof(label), "serial%d", index);
2293 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2294 if (!serial_hds[index]) {
2295 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2296 devname, strerror(errno));
2297 return -1;
2298 }
2299 index++;
2300 return 0;
2301}
2302
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002303static int parallel_parse(const char *devname)
2304{
2305 static int index = 0;
2306 char label[32];
2307
2308 if (strcmp(devname, "none") == 0)
2309 return 0;
2310 if (index == MAX_PARALLEL_PORTS) {
2311 fprintf(stderr, "qemu: too many parallel ports\n");
2312 exit(1);
2313 }
2314 snprintf(label, sizeof(label), "parallel%d", index);
2315 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
2316 if (!parallel_hds[index]) {
2317 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2318 devname, strerror(errno));
2319 return -1;
2320 }
2321 index++;
2322 return 0;
2323}
2324
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002325static int virtcon_parse(const char *devname)
2326{
2327 static int index = 0;
2328 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302329 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002330
2331 if (strcmp(devname, "none") == 0)
2332 return 0;
2333 if (index == MAX_VIRTIO_CONSOLES) {
2334 fprintf(stderr, "qemu: too many virtio consoles\n");
2335 exit(1);
2336 }
Amit Shah392ecf52010-01-21 16:19:23 +05302337
2338 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2339 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2340
2341 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2342 qemu_opt_set(dev_opts, "driver", "virtconsole");
2343
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002344 snprintf(label, sizeof(label), "virtcon%d", index);
2345 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
2346 if (!virtcon_hds[index]) {
2347 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2348 devname, strerror(errno));
2349 return -1;
2350 }
Amit Shah392ecf52010-01-21 16:19:23 +05302351 qemu_opt_set(dev_opts, "chardev", label);
2352
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002353 index++;
2354 return 0;
2355}
2356
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002357static int debugcon_parse(const char *devname)
2358{
2359 QemuOpts *opts;
2360
2361 if (!qemu_chr_open("debugcon", devname, NULL)) {
2362 exit(1);
2363 }
2364 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
2365 if (!opts) {
2366 fprintf(stderr, "qemu: already have a debugcon device\n");
2367 exit(1);
2368 }
2369 qemu_opt_set(opts, "driver", "isa-debugcon");
2370 qemu_opt_set(opts, "chardev", "debugcon");
2371 return 0;
2372}
2373
Anthony Liguori6530a972010-01-22 09:18:06 -06002374static const QEMUOption *lookup_opt(int argc, char **argv,
2375 const char **poptarg, int *poptind)
2376{
2377 const QEMUOption *popt;
2378 int optind = *poptind;
2379 char *r = argv[optind];
2380 const char *optarg;
2381
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002382 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002383 optind++;
2384 /* Treat --foo the same as -foo. */
2385 if (r[1] == '-')
2386 r++;
2387 popt = qemu_options;
2388 for(;;) {
2389 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002390 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002391 exit(1);
2392 }
2393 if (!strcmp(popt->name, r + 1))
2394 break;
2395 popt++;
2396 }
2397 if (popt->flags & HAS_ARG) {
2398 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002399 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002400 exit(1);
2401 }
2402 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002403 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002404 } else {
2405 optarg = NULL;
2406 }
2407
2408 *poptarg = optarg;
2409 *poptind = optind;
2410
2411 return popt;
2412}
2413
malc902b3d52008-12-10 19:18:40 +00002414int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002415{
aliguori59030a82009-04-05 18:43:41 +00002416 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00002417 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002418 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002419 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002420 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002421 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002422 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00002423 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00002424 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00002425 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002426 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00002427 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002428 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002429 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002430 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002431 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00002432#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00002433 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00002434#endif
bellard26a5f132008-05-28 12:30:31 +00002435 int tb_size;
ths93815bc2007-03-19 15:58:31 +00002436 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002437 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00002438#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00002439 int fd = 0;
2440 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00002441 const char *chroot_dir = NULL;
2442 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00002443#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002444 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06002445 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00002446
Markus Armbruster65abca02010-02-24 14:37:14 +01002447 error_set_progname(argv[0]);
2448
Jan Kiszka68752042009-09-15 13:36:04 +02002449 init_clocks();
2450
malc902b3d52008-12-10 19:18:40 +00002451 qemu_cache_utils_init(envp);
2452
Blue Swirl72cf2d42009-09-12 07:36:22 +00002453 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002454 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002455
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002456 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002457 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002458 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00002459 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002460 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002461 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00002462 kernel_filename = NULL;
2463 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00002464 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00002465 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002466
aliguori268a3622009-04-21 22:30:27 +00002467 for (i = 0; i < MAX_NODES; i++) {
2468 node_mem[i] = 0;
2469 node_cpumask[i] = 0;
2470 }
2471
aliguori268a3622009-04-21 22:30:27 +00002472 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002473 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002474
bellard26a5f132008-05-28 12:30:31 +00002475 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00002476 autostart= 1;
2477
Anthony Liguori292444c2010-01-21 10:57:58 -06002478 /* first pass of option parsing */
2479 optind = 1;
2480 while (optind < argc) {
2481 if (argv[optind][0] != '-') {
2482 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002483 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002484 continue;
2485 } else {
2486 const QEMUOption *popt;
2487
2488 popt = lookup_opt(argc, argv, &optarg, &optind);
2489 switch (popt->index) {
2490 case QEMU_OPTION_nodefconfig:
2491 defconfig=0;
2492 break;
2493 }
2494 }
2495 }
2496
2497 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002498 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002499
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002500 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02002501 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002502 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002503 }
2504
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002505 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02002506 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002507 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002508 }
2509 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00002510 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06002511
2512 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002513 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002514 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002515 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002516 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002517 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002518 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00002519 } else {
2520 const QEMUOption *popt;
2521
Anthony Liguori6530a972010-01-22 09:18:06 -06002522 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002523 if (!(popt->arch_mask & arch_type)) {
2524 printf("Option %s not supported for this target\n", popt->name);
2525 exit(1);
2526 }
bellardcd6f1162004-05-13 22:02:20 +00002527 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002528 case QEMU_OPTION_M:
2529 machine = find_machine(optarg);
2530 if (!machine) {
2531 QEMUMachine *m;
2532 printf("Supported machines are:\n");
2533 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01002534 if (m->alias)
2535 printf("%-10s %s (alias of %s)\n",
2536 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00002537 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00002538 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05002539 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00002540 }
ths15f82202007-06-29 23:26:08 +00002541 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00002542 }
2543 break;
j_mayer94fc95c2007-03-05 19:44:02 +00002544 case QEMU_OPTION_cpu:
2545 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00002546 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00002547 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00002548 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00002549 } else {
2550 cpu_model = optarg;
2551 }
2552 break;
bellardcd6f1162004-05-13 22:02:20 +00002553 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00002554 initrd_filename = optarg;
2555 break;
bellardcd6f1162004-05-13 22:02:20 +00002556 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00002557 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002558 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00002559 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002560 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00002561 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00002562 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00002563 translation == BIOS_ATA_TRANSLATION_LBA ?
2564 ",trans=lba" :
2565 translation == BIOS_ATA_TRANSLATION_NONE ?
2566 ",trans=none" : "");
2567 break;
bellardcd6f1162004-05-13 22:02:20 +00002568 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002569 case QEMU_OPTION_hdc:
2570 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00002571 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00002572 break;
thse4bcb142007-12-02 04:51:10 +00002573 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00002574 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00002575 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002576 case QEMU_OPTION_set:
2577 if (qemu_set_option(optarg) != 0)
2578 exit(1);
2579 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002580 case QEMU_OPTION_global:
2581 if (qemu_global_option(optarg) != 0)
2582 exit(1);
2583 break;
balrog3e3d5812007-04-30 02:09:25 +00002584 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00002585 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00002586 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002587 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00002588 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00002589 break;
j_mayer86f55662007-04-24 06:52:59 +00002590 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00002591 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00002592 break;
bellardcd6f1162004-05-13 22:02:20 +00002593 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002594 snapshot = 1;
2595 break;
bellardcd6f1162004-05-13 22:02:20 +00002596 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002597 {
bellard330d0412003-07-26 18:11:40 +00002598 const char *p;
2599 p = optarg;
2600 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002601 if (cyls < 1 || cyls > 16383)
2602 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002603 if (*p != ',')
2604 goto chs_fail;
2605 p++;
2606 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002607 if (heads < 1 || heads > 16)
2608 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002609 if (*p != ',')
2610 goto chs_fail;
2611 p++;
2612 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002613 if (secs < 1 || secs > 63)
2614 goto chs_fail;
2615 if (*p == ',') {
2616 p++;
2617 if (!strcmp(p, "none"))
2618 translation = BIOS_ATA_TRANSLATION_NONE;
2619 else if (!strcmp(p, "lba"))
2620 translation = BIOS_ATA_TRANSLATION_LBA;
2621 else if (!strcmp(p, "auto"))
2622 translation = BIOS_ATA_TRANSLATION_AUTO;
2623 else
2624 goto chs_fail;
2625 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002626 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002627 fprintf(stderr, "qemu: invalid physical CHS format\n");
2628 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002629 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002630 if (hda_opts != NULL) {
2631 char num[16];
2632 snprintf(num, sizeof(num), "%d", cyls);
2633 qemu_opt_set(hda_opts, "cyls", num);
2634 snprintf(num, sizeof(num), "%d", heads);
2635 qemu_opt_set(hda_opts, "heads", num);
2636 snprintf(num, sizeof(num), "%d", secs);
2637 qemu_opt_set(hda_opts, "secs", num);
2638 if (translation == BIOS_ATA_TRANSLATION_LBA)
2639 qemu_opt_set(hda_opts, "trans", "lba");
2640 if (translation == BIOS_ATA_TRANSLATION_NONE)
2641 qemu_opt_set(hda_opts, "trans", "none");
2642 }
bellard330d0412003-07-26 18:11:40 +00002643 }
2644 break;
aliguori268a3622009-04-21 22:30:27 +00002645 case QEMU_OPTION_numa:
2646 if (nb_numa_nodes >= MAX_NODES) {
2647 fprintf(stderr, "qemu: too many NUMA nodes\n");
2648 exit(1);
2649 }
2650 numa_add(optarg);
2651 break;
bellardcd6f1162004-05-13 22:02:20 +00002652 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002653 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002654 break;
balrog4d3b6f62008-02-10 16:33:14 +00002655#ifdef CONFIG_CURSES
2656 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002657 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00002658 break;
2659#endif
balroga171fe32007-04-30 01:48:07 +00002660 case QEMU_OPTION_portrait:
2661 graphic_rotate = 1;
2662 break;
bellardcd6f1162004-05-13 22:02:20 +00002663 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002664 kernel_filename = optarg;
2665 break;
bellardcd6f1162004-05-13 22:02:20 +00002666 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002667 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002668 break;
bellardcd6f1162004-05-13 22:02:20 +00002669 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00002670 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00002671 break;
bellardcd6f1162004-05-13 22:02:20 +00002672 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002673 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002674 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002675 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002676 };
2677 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002678 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002679 int legacy = 0;
2680
2681 if (!strchr(optarg, '=')) {
2682 legacy = 1;
2683 pstrcpy(buf, sizeof(buf), optarg);
2684 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2685 fprintf(stderr,
2686 "qemu: unknown boot parameter '%s' in '%s'\n",
2687 buf, optarg);
2688 exit(1);
2689 }
2690
2691 if (legacy ||
2692 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002693 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002694 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002695 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002696 if (!legacy) {
2697 if (get_param_value(buf, sizeof(buf),
2698 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002699 validate_bootdevices(buf);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002700 standard_boot_devices = qemu_strdup(boot_devices);
2701 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2702 qemu_register_reset(restore_boot_devices,
2703 standard_boot_devices);
2704 }
Jan Kiszka95387492009-07-02 00:19:02 +02002705 if (get_param_value(buf, sizeof(buf),
2706 "menu", optarg)) {
2707 if (!strcmp(buf, "on")) {
2708 boot_menu = 1;
2709 } else if (!strcmp(buf, "off")) {
2710 boot_menu = 0;
2711 } else {
2712 fprintf(stderr,
2713 "qemu: invalid option value '%s'\n",
2714 buf);
2715 exit(1);
2716 }
2717 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002718 }
bellard36b486b2003-11-11 13:36:08 +00002719 }
2720 break;
bellardcd6f1162004-05-13 22:02:20 +00002721 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002722 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00002723 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00002724 break;
bellard52ca8d62006-06-14 16:03:05 +00002725 case QEMU_OPTION_no_fd_bootchk:
2726 fd_bootchk = 0;
2727 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002728 case QEMU_OPTION_netdev:
2729 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2730 exit(1);
2731 }
2732 break;
bellard7c9d8e02005-11-15 22:16:05 +00002733 case QEMU_OPTION_net:
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01002734 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002735 exit(1);
2736 }
bellard702c6512004-04-02 21:21:32 +00002737 break;
bellardc7f74642004-08-24 21:57:12 +00002738#ifdef CONFIG_SLIRP
2739 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002740 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002741 break;
ths47d5d012007-02-20 00:05:08 +00002742 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002743 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002744 break;
bellardc94c8d62004-09-13 21:37:34 +00002745#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00002746 case QEMU_OPTION_smb:
Markus Armbruster07527062009-10-06 12:16:57 +01002747 if (net_slirp_smb(optarg) < 0)
2748 exit(1);
bellard9d728e82004-09-05 23:09:03 +00002749 break;
bellardc94c8d62004-09-13 21:37:34 +00002750#endif
bellard9bf05442004-08-25 22:12:49 +00002751 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002752 if (net_slirp_redir(optarg) < 0)
2753 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002754 break;
bellardc7f74642004-08-24 21:57:12 +00002755#endif
balrogdc72ac12008-11-09 00:04:26 +00002756 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002757 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002758 break;
bellard1d14ffa2005-10-30 18:58:22 +00002759 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002760 if (!(audio_available())) {
2761 printf("Option %s not supported for this target\n", popt->name);
2762 exit(1);
2763 }
bellard1d14ffa2005-10-30 18:58:22 +00002764 AUD_help ();
2765 exit (0);
2766 break;
2767 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002768 if (!(audio_available())) {
2769 printf("Option %s not supported for this target\n", popt->name);
2770 exit(1);
2771 }
bellard1d14ffa2005-10-30 18:58:22 +00002772 select_soundhw (optarg);
2773 break;
bellardcd6f1162004-05-13 22:02:20 +00002774 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002775 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002776 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002777 case QEMU_OPTION_version:
2778 version();
2779 exit(0);
2780 break;
aurel3200f82b82008-04-27 21:12:55 +00002781 case QEMU_OPTION_m: {
2782 uint64_t value;
2783 char *ptr;
2784
2785 value = strtoul(optarg, &ptr, 10);
2786 switch (*ptr) {
2787 case 0: case 'M': case 'm':
2788 value <<= 20;
2789 break;
2790 case 'G': case 'g':
2791 value <<= 30;
2792 break;
2793 default:
2794 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002795 exit(1);
2796 }
aurel3200f82b82008-04-27 21:12:55 +00002797
2798 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002799 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00002800 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2801 exit(1);
2802 }
Anthony Liguoric227f092009-10-01 16:12:16 -05002803 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002804 fprintf(stderr, "qemu: ram size too large\n");
2805 exit(1);
2806 }
2807 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002808 break;
aurel3200f82b82008-04-27 21:12:55 +00002809 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002810 case QEMU_OPTION_mempath:
2811 mem_path = optarg;
2812 break;
2813#ifdef MAP_POPULATE
2814 case QEMU_OPTION_mem_prealloc:
2815 mem_prealloc = 1;
2816 break;
2817#endif
bellardcd6f1162004-05-13 22:02:20 +00002818 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002819 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00002820 break;
bellardcd6f1162004-05-13 22:02:20 +00002821 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002822 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002823 break;
aliguori59030a82009-04-05 18:43:41 +00002824 case QEMU_OPTION_gdb:
2825 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002826 break;
bellardcd6f1162004-05-13 22:02:20 +00002827 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002828 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002829 break;
j_mayer1192dad2007-10-05 13:08:35 +00002830 case QEMU_OPTION_bios:
2831 bios_name = optarg;
2832 break;
aurel321b530a62009-04-05 20:08:59 +00002833 case QEMU_OPTION_singlestep:
2834 singlestep = 1;
2835 break;
bellardcd6f1162004-05-13 22:02:20 +00002836 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002837 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002838 break;
bellard3d11d0e2004-12-12 16:56:30 +00002839 case QEMU_OPTION_k:
2840 keyboard_layout = optarg;
2841 break;
bellardee22c2f2004-06-03 12:49:50 +00002842 case QEMU_OPTION_localtime:
2843 rtc_utc = 0;
2844 break;
malc3893c122008-09-28 00:42:05 +00002845 case QEMU_OPTION_vga:
2846 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002847 break;
bellarde9b137c2004-06-21 16:46:10 +00002848 case QEMU_OPTION_g:
2849 {
2850 const char *p;
2851 int w, h, depth;
2852 p = optarg;
2853 w = strtol(p, (char **)&p, 10);
2854 if (w <= 0) {
2855 graphic_error:
2856 fprintf(stderr, "qemu: invalid resolution or depth\n");
2857 exit(1);
2858 }
2859 if (*p != 'x')
2860 goto graphic_error;
2861 p++;
2862 h = strtol(p, (char **)&p, 10);
2863 if (h <= 0)
2864 goto graphic_error;
2865 if (*p == 'x') {
2866 p++;
2867 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002868 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002869 depth != 24 && depth != 32)
2870 goto graphic_error;
2871 } else if (*p == '\0') {
2872 depth = graphic_depth;
2873 } else {
2874 goto graphic_error;
2875 }
ths3b46e622007-09-17 08:09:54 +00002876
bellarde9b137c2004-06-21 16:46:10 +00002877 graphic_width = w;
2878 graphic_height = h;
2879 graphic_depth = depth;
2880 }
2881 break;
ths20d8a3e2007-02-18 17:04:49 +00002882 case QEMU_OPTION_echr:
2883 {
2884 char *r;
2885 term_escape_char = strtol(optarg, &r, 0);
2886 if (r == optarg)
2887 printf("Bad argument to echr\n");
2888 break;
2889 }
bellard82c643f2004-07-14 17:28:13 +00002890 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002891 monitor_parse(optarg, "readline");
2892 default_monitor = 0;
2893 break;
2894 case QEMU_OPTION_qmp:
2895 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002896 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002897 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002898 case QEMU_OPTION_mon:
Markus Armbruster8212c642010-02-10 19:52:18 +01002899 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002900 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002901 exit(1);
2902 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002903 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002904 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002905 case QEMU_OPTION_chardev:
Markus Armbruster8212c642010-02-10 19:52:18 +01002906 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002907 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002908 exit(1);
2909 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002910 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302911#ifdef CONFIG_LINUX
2912 case QEMU_OPTION_fsdev:
2913 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2914 if (!opts) {
2915 fprintf(stderr, "parse error: %s\n", optarg);
2916 exit(1);
2917 }
2918 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302919 case QEMU_OPTION_virtfs: {
2920 char *arg_fsdev = NULL;
2921 char *arg_9p = NULL;
2922 int len = 0;
2923
2924 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2925 if (!opts) {
2926 fprintf(stderr, "parse error: %s\n", optarg);
2927 exit(1);
2928 }
2929
2930 len = strlen(",id=,path=");
2931 len += strlen(qemu_opt_get(opts, "fstype"));
2932 len += strlen(qemu_opt_get(opts, "mount_tag"));
2933 len += strlen(qemu_opt_get(opts, "path"));
2934 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2935
2936 if (!arg_fsdev) {
2937 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2938 optarg);
2939 exit(1);
2940 }
2941
2942 sprintf(arg_fsdev, "%s,id=%s,path=%s",
2943 qemu_opt_get(opts, "fstype"),
2944 qemu_opt_get(opts, "mount_tag"),
2945 qemu_opt_get(opts, "path"));
2946
2947 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2948 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2949 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2950
2951 if (!arg_9p) {
2952 fprintf(stderr, "No memory to parse -device for %s\n",
2953 optarg);
2954 exit(1);
2955 }
2956
2957 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2958 qemu_opt_get(opts, "mount_tag"),
2959 qemu_opt_get(opts, "mount_tag"));
2960
2961 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2962 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2963 exit(1);
2964 }
2965
2966 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2967 fprintf(stderr, "parse error [device]: %s\n", optarg);
2968 exit(1);
2969 }
2970
2971 qemu_free(arg_fsdev);
2972 qemu_free(arg_9p);
2973 break;
2974 }
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302975#endif
bellard82c643f2004-07-14 17:28:13 +00002976 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002977 add_device_config(DEV_SERIAL, optarg);
2978 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002979 if (strncmp(optarg, "mon:", 4) == 0) {
2980 default_monitor = 0;
2981 }
bellard82c643f2004-07-14 17:28:13 +00002982 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002983 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002984 if (watchdog) {
2985 fprintf(stderr,
2986 "qemu: only one watchdog option may be given\n");
2987 return 1;
2988 }
2989 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002990 break;
2991 case QEMU_OPTION_watchdog_action:
2992 if (select_watchdog_action(optarg) == -1) {
2993 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2994 exit(1);
2995 }
2996 break;
aliguori51ecf132009-01-15 20:06:40 +00002997 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002998 add_device_config(DEV_VIRTCON, optarg);
2999 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003000 if (strncmp(optarg, "mon:", 4) == 0) {
3001 default_monitor = 0;
3002 }
aliguori51ecf132009-01-15 20:06:40 +00003003 break;
bellard6508fe52005-01-15 12:02:56 +00003004 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003005 add_device_config(DEV_PARALLEL, optarg);
3006 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003007 if (strncmp(optarg, "mon:", 4) == 0) {
3008 default_monitor = 0;
3009 }
bellard6508fe52005-01-15 12:02:56 +00003010 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003011 case QEMU_OPTION_debugcon:
3012 add_device_config(DEV_DEBUGCON, optarg);
3013 break;
bellardd63d3072004-10-03 13:29:03 +00003014 case QEMU_OPTION_loadvm:
3015 loadvm = optarg;
3016 break;
3017 case QEMU_OPTION_full_screen:
3018 full_screen = 1;
3019 break;
ths667acca2006-12-11 02:08:05 +00003020#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00003021 case QEMU_OPTION_no_frame:
3022 no_frame = 1;
3023 break;
ths3780e192007-06-21 21:08:02 +00003024 case QEMU_OPTION_alt_grab:
3025 alt_grab = 1;
3026 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05003027 case QEMU_OPTION_ctrl_grab:
3028 ctrl_grab = 1;
3029 break;
ths667acca2006-12-11 02:08:05 +00003030 case QEMU_OPTION_no_quit:
3031 no_quit = 1;
3032 break;
aliguori7d957bd2009-01-15 22:14:11 +00003033 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003034 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00003035 break;
ths667acca2006-12-11 02:08:05 +00003036#endif
bellardf7cce892004-12-08 22:21:25 +00003037 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00003038 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00003039 break;
bellarda09db212005-04-30 16:10:35 +00003040 case QEMU_OPTION_win2k_hack:
3041 win2k_install_hack = 1;
3042 break;
aliguori73822ec2009-01-15 20:11:34 +00003043 case QEMU_OPTION_rtc_td_hack:
3044 rtc_td_hack = 1;
3045 break;
aliguori8a92ea22009-02-27 20:12:36 +00003046 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003047 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00003048 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00003049 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003050 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00003051 break;
aliguori7ba1e612008-11-05 16:04:33 +00003052 case QEMU_OPTION_enable_kvm:
3053 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00003054 break;
bellardbb36d472005-11-05 14:22:28 +00003055 case QEMU_OPTION_usb:
3056 usb_enabled = 1;
3057 break;
bellarda594cfb2005-11-06 16:13:29 +00003058 case QEMU_OPTION_usbdevice:
3059 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003060 add_device_config(DEV_USB, optarg);
3061 break;
3062 case QEMU_OPTION_device:
Markus Armbruster8212c642010-02-10 19:52:18 +01003063 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003064 exit(1);
3065 }
bellarda594cfb2005-11-06 16:13:29 +00003066 break;
bellard6a00d602005-11-21 23:25:50 +00003067 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003068 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00003069 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00003070 fprintf(stderr, "Invalid number of CPUs\n");
3071 exit(1);
3072 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02003073 if (max_cpus < smp_cpus) {
3074 fprintf(stderr, "maxcpus must be equal to or greater than "
3075 "smp\n");
3076 exit(1);
3077 }
3078 if (max_cpus > 255) {
3079 fprintf(stderr, "Unsupported number of maxcpus\n");
3080 exit(1);
3081 }
bellard6a00d602005-11-21 23:25:50 +00003082 break;
bellard24236862006-04-30 21:28:36 +00003083 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003084 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00003085 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00003086 break;
bellard6515b202006-05-03 22:02:44 +00003087 case QEMU_OPTION_no_acpi:
3088 acpi_enabled = 0;
3089 break;
aliguori16b29ae2008-12-17 23:28:44 +00003090 case QEMU_OPTION_no_hpet:
3091 no_hpet = 1;
3092 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003093 case QEMU_OPTION_balloon:
3094 if (balloon_parse(optarg) < 0) {
3095 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3096 exit(1);
3097 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03003098 break;
bellardd1beab82006-10-02 19:44:22 +00003099 case QEMU_OPTION_no_reboot:
3100 no_reboot = 1;
3101 break;
aurel32b2f76162008-04-11 21:35:52 +00003102 case QEMU_OPTION_no_shutdown:
3103 no_shutdown = 1;
3104 break;
balrog9467cd42007-05-01 01:34:14 +00003105 case QEMU_OPTION_show_cursor:
3106 cursor_hide = 0;
3107 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003108 case QEMU_OPTION_uuid:
3109 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3110 fprintf(stderr, "Fail to parse UUID string."
3111 " Wrong format.\n");
3112 exit(1);
3113 }
3114 break;
blueswir15824d652009-03-28 06:44:27 +00003115#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00003116 case QEMU_OPTION_daemonize:
3117 daemonize = 1;
3118 break;
blueswir15824d652009-03-28 06:44:27 +00003119#endif
ths9ae02552007-01-05 17:39:04 +00003120 case QEMU_OPTION_option_rom:
3121 if (nb_option_roms >= MAX_OPTION_ROMS) {
3122 fprintf(stderr, "Too many option ROMs\n");
3123 exit(1);
3124 }
3125 option_rom[nb_option_roms] = optarg;
3126 nb_option_roms++;
3127 break;
pbrook8e716212007-01-20 17:12:09 +00003128 case QEMU_OPTION_semihosting:
3129 semihosting_enabled = 1;
3130 break;
thsc35734b2007-03-19 15:17:08 +00003131 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02003132 qemu_name = qemu_strdup(optarg);
3133 {
3134 char *p = strchr(qemu_name, ',');
3135 if (p != NULL) {
3136 *p++ = 0;
3137 if (strncmp(p, "process=", 8)) {
3138 fprintf(stderr, "Unknown subargument %s to -name", p);
3139 exit(1);
3140 }
3141 p += 8;
3142 set_proc_name(p);
3143 }
3144 }
thsc35734b2007-03-19 15:17:08 +00003145 break;
blueswir166508602007-05-01 14:16:52 +00003146 case QEMU_OPTION_prom_env:
3147 if (nb_prom_envs >= MAX_PROM_ENVS) {
3148 fprintf(stderr, "Too many prom variables\n");
3149 exit(1);
3150 }
3151 prom_envs[nb_prom_envs] = optarg;
3152 nb_prom_envs++;
3153 break;
balrog2b8f2d42007-07-27 22:08:46 +00003154 case QEMU_OPTION_old_param:
3155 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003156 break;
thsf3dcfad2007-08-24 01:26:02 +00003157 case QEMU_OPTION_clock:
3158 configure_alarms(optarg);
3159 break;
bellard7e0af5d02007-11-07 16:24:33 +00003160 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003161 configure_rtc_date_offset(optarg, 1);
3162 break;
3163 case QEMU_OPTION_rtc:
Markus Armbruster8212c642010-02-10 19:52:18 +01003164 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003165 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003166 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003167 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003168 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003169 break;
bellard26a5f132008-05-28 12:30:31 +00003170 case QEMU_OPTION_tb_size:
3171 tb_size = strtol(optarg, NULL, 0);
3172 if (tb_size < 0)
3173 tb_size = 0;
3174 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003175 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003176 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003177 break;
aliguori5bb79102008-10-13 03:12:02 +00003178 case QEMU_OPTION_incoming:
3179 incoming = optarg;
3180 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003181 case QEMU_OPTION_nodefaults:
3182 default_serial = 0;
3183 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003184 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003185 default_monitor = 0;
3186 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003187 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003188 default_floppy = 0;
3189 default_cdrom = 0;
3190 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003191 break;
blueswir15824d652009-03-28 06:44:27 +00003192#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00003193 case QEMU_OPTION_chroot:
3194 chroot_dir = optarg;
3195 break;
3196 case QEMU_OPTION_runas:
3197 run_as = optarg;
3198 break;
blueswir15824d652009-03-28 06:44:27 +00003199#endif
aliguorie37630c2009-04-22 15:19:10 +00003200 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003201 if (!(xen_available())) {
3202 printf("Option %s not supported for this target\n", popt->name);
3203 exit(1);
3204 }
aliguorie37630c2009-04-22 15:19:10 +00003205 xen_domid = atoi(optarg);
3206 break;
3207 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003208 if (!(xen_available())) {
3209 printf("Option %s not supported for this target\n", popt->name);
3210 exit(1);
3211 }
aliguorie37630c2009-04-22 15:19:10 +00003212 xen_mode = XEN_CREATE;
3213 break;
3214 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003215 if (!(xen_available())) {
3216 printf("Option %s not supported for this target\n", popt->name);
3217 exit(1);
3218 }
aliguorie37630c2009-04-22 15:19:10 +00003219 xen_mode = XEN_ATTACH;
3220 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003221 case QEMU_OPTION_readconfig:
3222 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003223 int ret = qemu_read_config_file(optarg);
3224 if (ret < 0) {
3225 fprintf(stderr, "read config %s: %s\n", optarg,
3226 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003227 exit(1);
3228 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003229 break;
3230 }
3231 case QEMU_OPTION_writeconfig:
3232 {
3233 FILE *fp;
3234 if (strcmp(optarg, "-") == 0) {
3235 fp = stdout;
3236 } else {
3237 fp = fopen(optarg, "w");
3238 if (fp == NULL) {
3239 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3240 exit(1);
3241 }
3242 }
3243 qemu_config_write(fp);
3244 fclose(fp);
3245 break;
3246 }
bellardcd6f1162004-05-13 22:02:20 +00003247 }
bellard0824d6f2003-06-24 13:42:40 +00003248 }
3249 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003250 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003251
Paul Brook5cea8592009-05-30 00:52:44 +01003252 /* If no data_dir is specified then try to find it relative to the
3253 executable path. */
3254 if (!data_dir) {
3255 data_dir = find_datadir(argv[0]);
3256 }
3257 /* If all else fails use the install patch specified when building. */
3258 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02003259 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003260 }
3261
Jes Sorensen6be68d72009-07-23 17:03:42 +02003262 /*
3263 * Default to max_cpus = smp_cpus, in case the user doesn't
3264 * specify a max_cpus value.
3265 */
3266 if (!max_cpus)
3267 max_cpus = smp_cpus;
3268
balrog3d878ca2008-10-28 10:59:59 +00003269 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003270 if (smp_cpus > machine->max_cpus) {
3271 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3272 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3273 machine->max_cpus);
3274 exit(1);
3275 }
3276
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003277 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +01003278 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003279
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003280 if (machine->no_serial) {
3281 default_serial = 0;
3282 }
3283 if (machine->no_parallel) {
3284 default_parallel = 0;
3285 }
3286 if (!machine->use_virtcon) {
3287 default_virtcon = 0;
3288 }
3289 if (machine->no_vga) {
3290 default_vga = 0;
3291 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003292 if (machine->no_floppy) {
3293 default_floppy = 0;
3294 }
3295 if (machine->no_cdrom) {
3296 default_cdrom = 0;
3297 }
3298 if (machine->no_sdcard) {
3299 default_sdcard = 0;
3300 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003301
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003302 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003303 if (default_parallel)
3304 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003305 if (default_serial && default_monitor) {
3306 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003307 } else if (default_virtcon && default_monitor) {
3308 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003309 } else {
3310 if (default_serial)
3311 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003312 if (default_virtcon)
3313 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003314 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003315 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003316 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003317 } else {
3318 if (default_serial)
3319 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003320 if (default_parallel)
3321 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003322 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003323 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003324 if (default_virtcon)
3325 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003326 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01003327 if (default_vga)
3328 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00003329
TeLeMana5829fd2010-04-15 12:37:55 +08003330 socket_init();
3331
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003332 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
3333 exit(1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303334#ifdef CONFIG_LINUX
3335 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
3336 exit(1);
3337 }
3338#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003339
ths71e3ceb2006-12-22 02:11:31 +00003340#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00003341 if (daemonize) {
3342 pid_t pid;
3343
3344 if (pipe(fds) == -1)
3345 exit(1);
3346
3347 pid = fork();
3348 if (pid > 0) {
3349 uint8_t status;
3350 ssize_t len;
3351
3352 close(fds[1]);
3353
3354 again:
ths93815bc2007-03-19 15:58:31 +00003355 len = read(fds[0], &status, 1);
3356 if (len == -1 && (errno == EINTR))
3357 goto again;
3358
3359 if (len != 1)
3360 exit(1);
3361 else if (status == 1) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05003362 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00003363 exit(1);
3364 } else
3365 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00003366 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00003367 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00003368
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003369 close(fds[0]);
3370 qemu_set_cloexec(fds[1]);
3371
ths71e3ceb2006-12-22 02:11:31 +00003372 setsid();
3373
3374 pid = fork();
3375 if (pid > 0)
3376 exit(0);
3377 else if (pid < 0)
3378 exit(1);
3379
3380 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00003381
3382 signal(SIGTSTP, SIG_IGN);
3383 signal(SIGTTOU, SIG_IGN);
3384 signal(SIGTTIN, SIG_IGN);
3385 }
Juha Riihimäki099fe232009-12-03 15:56:03 +02003386#endif
ths71e3ceb2006-12-22 02:11:31 +00003387
thsaa26bb22007-03-25 21:33:06 +00003388 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Juha Riihimäki099fe232009-12-03 15:56:03 +02003389#ifndef _WIN32
ths93815bc2007-03-19 15:58:31 +00003390 if (daemonize) {
3391 uint8_t status = 1;
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01003392 if (write(fds[1], &status, 1) != 1) {
3393 perror("daemonize. Writing to pipe\n");
3394 }
ths93815bc2007-03-19 15:58:31 +00003395 } else
Juha Riihimäki099fe232009-12-03 15:56:03 +02003396#endif
Justin M. Forbes850810d2009-10-01 09:42:56 -05003397 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00003398 exit(1);
3399 }
3400
Paolo Bonzini98c85732010-04-19 18:59:30 +00003401 if (kvm_allowed) {
3402 int ret = kvm_init(smp_cpus);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003403 if (ret < 0) {
Paolo Bonzini98c85732010-04-19 18:59:30 +00003404 if (!kvm_available()) {
3405 printf("KVM not supported for this target\n");
3406 } else {
3407 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
3408 }
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003409 exit(1);
3410 }
3411 }
3412
aliguori3fcf7b62009-04-24 18:03:25 +00003413 if (qemu_init_main_loop()) {
3414 fprintf(stderr, "qemu_init_main_loop failed\n");
3415 exit(1);
3416 }
bellarda20dd502003-09-30 21:07:02 +00003417 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003418
thsf8d39c02008-07-03 10:01:15 +00003419 if (!linux_boot && *kernel_cmdline != '\0') {
3420 fprintf(stderr, "-append only allowed with -kernel option\n");
3421 exit(1);
3422 }
3423
3424 if (!linux_boot && initrd_filename != NULL) {
3425 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3426 exit(1);
3427 }
3428
Filip Navarabf65f532009-07-27 10:02:04 -05003429#ifndef _WIN32
3430 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00003431 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05003432#endif
ths3b46e622007-09-17 08:09:54 +00003433
aliguori7183b4b2008-11-05 20:40:18 +00003434 if (init_timer_alarm() < 0) {
3435 fprintf(stderr, "could not initialize alarm timer\n");
3436 exit(1);
3437 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003438 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003439
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003440 if (net_init_clients() < 0) {
3441 exit(1);
bellard702c6512004-04-02 21:21:32 +00003442 }
bellardf1510b22003-06-25 00:07:40 +00003443
balrogdc72ac12008-11-09 00:04:26 +00003444 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003445 if (foreach_device_config(DEV_BT, bt_parse))
3446 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003447
bellard0824d6f2003-06-24 13:42:40 +00003448 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00003449 if (ram_size == 0)
3450 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00003451
bellard26a5f132008-05-28 12:30:31 +00003452 /* init the dynamic translator */
3453 cpu_exec_init_all(tb_size * 1024 * 1024);
3454
Markus Armbrustereb852012009-10-27 18:41:44 +01003455 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003456
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003457 blk_mig_init();
3458
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003459 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003460 /* we always create the cdrom drive, even if no disk is there */
3461 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003462 }
thse4bcb142007-12-02 04:51:10 +00003463
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003464 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003465 /* we always create at least one floppy */
3466 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003467 }
bellardc4b1fcc2004-03-14 21:44:30 +00003468
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003469 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003470 /* we always create one sd slot, even if no card is in it */
3471 drive_add(NULL, SD_ALIAS);
3472 }
balrog9d413d12007-12-04 00:10:34 +00003473
ths96d30e42007-01-07 20:42:14 +00003474 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003475 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02003476 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
3477 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003478 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00003479
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003480 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
3481 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003482
aliguori268a3622009-04-21 22:30:27 +00003483 if (nb_numa_nodes > 0) {
3484 int i;
3485
3486 if (nb_numa_nodes > smp_cpus) {
3487 nb_numa_nodes = smp_cpus;
3488 }
3489
3490 /* If no memory size if given for any node, assume the default case
3491 * and distribute the available memory equally across all nodes
3492 */
3493 for (i = 0; i < nb_numa_nodes; i++) {
3494 if (node_mem[i] != 0)
3495 break;
3496 }
3497 if (i == nb_numa_nodes) {
3498 uint64_t usedmem = 0;
3499
3500 /* On Linux, the each node's border has to be 8MB aligned,
3501 * the final node gets the rest.
3502 */
3503 for (i = 0; i < nb_numa_nodes - 1; i++) {
3504 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3505 usedmem += node_mem[i];
3506 }
3507 node_mem[i] = ram_size - usedmem;
3508 }
3509
3510 for (i = 0; i < nb_numa_nodes; i++) {
3511 if (node_cpumask[i] != 0)
3512 break;
3513 }
3514 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3515 * must cope with this anyway, because there are BIOSes out there in
3516 * real machines which also use this scheme.
3517 */
3518 if (i == nb_numa_nodes) {
3519 for (i = 0; i < smp_cpus; i++) {
3520 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3521 }
3522 }
3523 }
3524
Jan Kiszka157b9312010-04-06 16:55:53 +02003525 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
3526 exit(1);
3527 }
3528
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003529 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3530 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003531 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3532 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003533 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3534 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003535 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3536 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003537
Paul Brookaae94602009-05-14 22:35:06 +01003538 module_call_init(MODULE_INIT_DEVICE);
3539
Markus Armbrusterff952ba2010-01-29 19:48:57 +01003540 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
3541 exit(0);
3542
Markus Armbruster09aaa162009-08-21 10:31:34 +02003543 if (watchdog) {
3544 i = select_watchdog(watchdog);
3545 if (i > 0)
3546 exit (i == 1 ? 1 : 0);
3547 }
3548
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003549 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003550 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003551 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003552 qemu_add_globals();
3553
Paul Brookfbe1b592009-05-13 17:56:25 +01003554 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00003555 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3556
Jan Kiszkaea375f92010-03-01 19:10:30 +01003557 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003558
Juan Quintela67b3b712009-08-28 19:25:15 +02003559#ifndef _WIN32
3560 /* must be after terminal init, SDL library changes signal handlers */
3561 sighandler_setup();
3562#endif
3563
Blue Swirl87d0a282010-03-27 18:24:45 +00003564 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003565
aliguori6f338c32009-02-11 15:21:54 +00003566 current_machine = machine;
3567
aliguori3023f332009-01-16 19:04:14 +00003568 /* init USB devices */
3569 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01003570 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3571 exit(1);
aliguori3023f332009-01-16 19:04:14 +00003572 }
3573
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003574 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003575 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003576 exit(1);
3577
Markus Armbruster668680f2010-02-11 14:44:58 +01003578 net_check_clients();
3579
aliguori3023f332009-01-16 19:04:14 +00003580 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003581 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003582
3583 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003584#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003585 display_type = DT_SDL;
3586#else
3587 display_type = DT_VNC;
3588 vnc_display = "localhost:0,to=99";
3589 show_vnc_port = 1;
3590#endif
3591 }
3592
3593
3594 switch (display_type) {
3595 case DT_NOGRAPHIC:
3596 break;
3597#if defined(CONFIG_CURSES)
3598 case DT_CURSES:
3599 curses_display_init(ds, full_screen);
3600 break;
3601#endif
bellard5b0753e2005-03-01 21:37:28 +00003602#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003603 case DT_SDL:
3604 sdl_display_init(ds, full_screen, no_frame);
3605 break;
bellard5b0753e2005-03-01 21:37:28 +00003606#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003607 case DT_SDL:
3608 cocoa_display_init(ds, full_screen);
3609 break;
bellard313aa562003-08-10 21:52:11 +00003610#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003611 case DT_VNC:
3612 vnc_display_init(ds);
3613 if (vnc_display_open(ds, vnc_display) < 0)
3614 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003615
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003616 if (show_vnc_port) {
3617 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003618 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003619 break;
3620 default:
3621 break;
bellard313aa562003-08-10 21:52:11 +00003622 }
aliguori7d957bd2009-01-15 22:14:11 +00003623 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00003624
aliguori3023f332009-01-16 19:04:14 +00003625 dcl = ds->listeners;
3626 while (dcl != NULL) {
3627 if (dcl->dpy_refresh != NULL) {
3628 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3629 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09003630 break;
ths20d8a3e2007-02-18 17:04:49 +00003631 }
aliguori3023f332009-01-16 19:04:14 +00003632 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00003633 }
aliguori3023f332009-01-16 19:04:14 +00003634
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003635 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00003636 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3637 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3638 }
3639
Paolo Bonzinib473df62010-02-11 00:29:55 +01003640 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003641
aliguori59030a82009-04-05 18:43:41 +00003642 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3643 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3644 gdbstub_dev);
3645 exit(1);
balrog45669e02007-07-02 13:20:17 +00003646 }
balrog45669e02007-07-02 13:20:17 +00003647
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003648 qdev_machine_creation_done();
3649
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003650 if (rom_load_all() != 0) {
3651 fprintf(stderr, "rom loading failed\n");
3652 exit(1);
3653 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003654
Juan Quintela504c2942009-11-12 00:39:13 +01003655 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02003656 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003657 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003658 autostart = 0;
3659 }
3660 }
bellardd63d3072004-10-03 13:29:03 +00003661
Glauber Costa2bb8c102009-07-24 16:20:23 -04003662 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00003663 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03003664 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003665 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003666 }
thsffd843b2006-12-21 19:46:43 +00003667
blueswir1b9e82a52009-04-05 18:03:31 +00003668#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00003669 if (daemonize) {
3670 uint8_t status = 0;
3671 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00003672
3673 again1:
3674 len = write(fds[1], &status, 1);
3675 if (len == -1 && (errno == EINTR))
3676 goto again1;
3677
3678 if (len != 1)
3679 exit(1);
3680
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01003681 if (chdir("/")) {
3682 perror("not able to chdir to /");
3683 exit(1);
3684 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003685 TFR(fd = qemu_open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00003686 if (fd == -1)
3687 exit(1);
aliguori08585322009-02-27 22:09:45 +00003688 }
ths71e3ceb2006-12-22 02:11:31 +00003689
aliguori08585322009-02-27 22:09:45 +00003690 if (run_as) {
3691 pwd = getpwnam(run_as);
3692 if (!pwd) {
3693 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
3694 exit(1);
3695 }
3696 }
ths71e3ceb2006-12-22 02:11:31 +00003697
aliguori08585322009-02-27 22:09:45 +00003698 if (chroot_dir) {
3699 if (chroot(chroot_dir) < 0) {
3700 fprintf(stderr, "chroot failed\n");
3701 exit(1);
3702 }
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01003703 if (chdir("/")) {
3704 perror("not able to chdir to /");
3705 exit(1);
3706 }
aliguori08585322009-02-27 22:09:45 +00003707 }
3708
3709 if (run_as) {
3710 if (setgid(pwd->pw_gid) < 0) {
3711 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
3712 exit(1);
3713 }
3714 if (setuid(pwd->pw_uid) < 0) {
3715 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
3716 exit(1);
3717 }
3718 if (setuid(0) != -1) {
3719 fprintf(stderr, "Dropping privileges failed\n");
3720 exit(1);
3721 }
3722 }
aliguori08585322009-02-27 22:09:45 +00003723
3724 if (daemonize) {
3725 dup2(fd, 0);
3726 dup2(fd, 1);
3727 dup2(fd, 2);
3728
3729 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00003730 }
blueswir1b9e82a52009-04-05 18:03:31 +00003731#endif
ths71e3ceb2006-12-22 02:11:31 +00003732
bellard8a7ddc32004-03-31 19:00:16 +00003733 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003734 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003735 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00003736
bellard0824d6f2003-06-24 13:42:40 +00003737 return 0;
3738}