blob: 6768cf125185aa0c67932cb56cc41f05f090b4fe [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>
73#include <netinet/in.h>
74#include <netinet/in_systm.h>
75#include <netinet/ip.h>
76#include <netinet/ip_icmp.h> // must come after ip.h
77#include <netinet/udp.h>
78#include <netinet/tcp.h>
79#include <net/if.h>
80#include <syslog.h>
81#include <stropts.h>
Blue Swirl8d32cf02009-10-02 19:32:12 +000082/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
83 discussion about Solaris header problems */
84extern int madvise(caddr_t, size_t, int);
bellard67b915a2004-03-31 23:37:16 +000085#endif
bellard7d3505c2004-05-12 19:32:15 +000086#endif
bellardec530c82006-04-25 22:36:06 +000087#endif
bellard67b915a2004-03-31 23:37:16 +000088
blueswir19892fbf2008-08-24 10:34:20 +000089#if defined(__OpenBSD__)
90#include <util.h>
91#endif
92
ths8a16d272008-07-19 09:56:24 +000093#if defined(CONFIG_VDE)
94#include <libvdeplug.h>
95#endif
96
bellard67b915a2004-03-31 23:37:16 +000097#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000098#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000099#endif
100
bellard73332e52004-04-04 20:22:28 +0000101#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200102#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200103#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000104int qemu_main(int argc, char **argv, char **envp);
105int main(int argc, char **argv)
106{
Stefan Weil59a36a22009-06-18 20:11:03 +0200107 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000108}
109#undef main
110#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000111#endif
bellard73332e52004-04-04 20:22:28 +0000112#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000113
bellard5b0753e2005-03-01 21:37:28 +0000114#ifdef CONFIG_COCOA
115#undef main
116#define main qemu_main
117#endif /* CONFIG_COCOA */
118
blueswir1511d2b12009-03-07 15:32:56 +0000119#include "hw/hw.h"
120#include "hw/boards.h"
121#include "hw/usb.h"
122#include "hw/pcmcia.h"
123#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000124#include "hw/isa.h"
125#include "hw/baum.h"
126#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100127#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000128#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000129#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200130#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200131#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000132#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000133#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000134#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000135#include "monitor.h"
136#include "console.h"
137#include "sysemu.h"
138#include "gdbstub.h"
139#include "qemu-timer.h"
140#include "qemu-char.h"
141#include "cache-utils.h"
142#include "block.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200143#include "block_int.h"
144#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000145#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000146#include "audio/audio.h"
147#include "migration.h"
148#include "kvm.h"
149#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200150#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200151#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200152#include "qemu-objects.h"
blueswir1511d2b12009-03-07 15:32:56 +0000153
bellard0824d6f2003-06-24 13:42:40 +0000154#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000155
blueswir1511d2b12009-03-07 15:32:56 +0000156#include "qemu_socket.h"
157
Jan Kiszkad918f232009-06-24 14:42:30 +0200158#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000159
Blue Swirl72cf2d42009-09-12 07:36:22 +0000160#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000161#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000162#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000163
blueswir19dc63a12008-10-04 07:25:46 +0000164//#define DEBUG_NET
165//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000166
bellard1bfe8562004-07-08 21:17:50 +0000167#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000168
Amit Shah98b19252010-01-20 00:36:52 +0530169#define MAX_VIRTIO_CONSOLES 1
170
Paul Brook5cea8592009-05-30 00:52:44 +0100171static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000172const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000173/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000174 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000175struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
176struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000177enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500178DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000179const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500180ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300181const char *mem_path = NULL;
182#ifdef MAP_POPULATE
183int mem_prealloc = 0; /* force preallocation of physical target memory */
184#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000185int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000186NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000187int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200188int autostart;
balrogf6503052008-02-17 11:42:19 +0000189static int rtc_utc = 1;
190static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200191QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100192int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000193static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000194#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000195static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000196#endif
ths667acca2006-12-11 02:08:05 +0000197int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000198CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000199CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000200CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000201int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000202int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000203int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000204int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000205int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200206int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200207int smp_cores = 1;
208int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000209const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000210int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000211int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000212int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000213int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000214int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000215int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000216int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200217uint8_t irq0override = 1;
blueswir1b9e82a52009-04-05 18:03:31 +0000218#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000219int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000220#endif
Markus Armbruster09aaa162009-08-21 10:31:34 +0200221const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000222const char *option_rom[MAX_OPTION_ROMS];
223int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000224int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000225int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000226const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000227int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500228int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000229unsigned int nb_prom_envs = 0;
230const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200231int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000232
aliguori268a3622009-04-21 22:30:27 +0000233int nb_numa_nodes;
234uint64_t node_mem[MAX_NODES];
235uint64_t node_cpumask[MAX_NODES];
236
blueswir19043b622009-01-21 19:28:13 +0000237static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000238
blueswir18fcb1b92008-09-18 18:29:08 +0000239uint8_t qemu_uuid[16];
240
Jan Kiszka76e30d02009-07-02 00:19:02 +0200241static QEMUBootSetHandler *boot_set_handler;
242static void *boot_set_opaque;
243
Blue Swirld745bef2010-03-29 19:23:49 +0000244int kvm_allowed = 0;
245uint32_t xen_domid;
246enum xen_mode xen_mode = XEN_EMULATE;
247
Marcelo Tosatticc84de92010-02-17 20:14:42 -0200248#ifdef SIGRTMIN
249#define SIG_IPI (SIGRTMIN+4)
250#else
251#define SIG_IPI SIGUSR1
252#endif
253
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100254static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100255static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100256static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100257static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100258static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100259static int default_floppy = 1;
260static int default_cdrom = 1;
261static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100262
263static struct {
264 const char *driver;
265 int *flag;
266} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100267 { .driver = "isa-serial", .flag = &default_serial },
268 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100269 { .driver = "isa-fdc", .flag = &default_floppy },
270 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530271 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
272 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
273 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100274 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100275 { .driver = "cirrus-vga", .flag = &default_vga },
276 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100277};
278
279static int default_driver_check(QemuOpts *opts, void *opaque)
280{
281 const char *driver = qemu_opt_get(opts, "driver");
282 int i;
283
284 if (!driver)
285 return 0;
286 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
287 if (strcmp(default_list[i].driver, driver) != 0)
288 continue;
289 *(default_list[i].flag) = 0;
290 }
291 return 0;
292}
293
bellard0824d6f2003-06-24 13:42:40 +0000294/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000295/* x86 ISA bus support */
296
Anthony Liguoric227f092009-10-01 16:12:16 -0500297target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000298PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000299
Andi Kleen18894652009-07-02 09:34:17 +0200300static void set_proc_name(const char *s)
301{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700302#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200303 char name[16];
304 if (!s)
305 return;
306 name[sizeof(name) - 1] = 0;
307 strncpy(name, s, sizeof(name));
308 /* Could rewrite argv[0] too, but that's a bit more complicated.
309 This simple way is enough for `top'. */
310 prctl(PR_SET_NAME, name);
311#endif
312}
aliguoridf751fa2008-12-04 20:19:35 +0000313
314/***************/
315/* ballooning */
316
317static QEMUBalloonEvent *qemu_balloon_event;
318void *qemu_balloon_event_opaque;
319
320void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
321{
322 qemu_balloon_event = func;
323 qemu_balloon_event_opaque = opaque;
324}
325
Adam Litke625a5be2010-01-26 14:17:35 -0600326int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +0000327{
Adam Litke625a5be2010-01-26 14:17:35 -0600328 if (qemu_balloon_event) {
329 qemu_balloon_event(qemu_balloon_event_opaque, target, cb, opaque);
330 return 1;
331 } else {
332 return 0;
333 }
aliguoridf751fa2008-12-04 20:19:35 +0000334}
335
Adam Litke625a5be2010-01-26 14:17:35 -0600336int qemu_balloon_status(MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +0000337{
Adam Litke625a5be2010-01-26 14:17:35 -0600338 if (qemu_balloon_event) {
339 qemu_balloon_event(qemu_balloon_event_opaque, 0, cb, opaque);
340 return 1;
341 } else {
342 return 0;
343 }
aliguoridf751fa2008-12-04 20:19:35 +0000344}
bellard0824d6f2003-06-24 13:42:40 +0000345
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100346
bellard8a7ddc32004-03-31 19:00:16 +0000347/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100348/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000349
bellard87858c82003-06-27 12:01:39 +0000350/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000351uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000352{
353 union {
354 uint64_t ll;
355 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200356#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000357 uint32_t high, low;
358#else
359 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000360#endif
bellard87858c82003-06-27 12:01:39 +0000361 } l;
362 } u, res;
363 uint64_t rl, rh;
364
365 u.ll = a;
366 rl = (uint64_t)u.l.low * (uint64_t)b;
367 rh = (uint64_t)u.l.high * (uint64_t)b;
368 rh += (rl >> 32);
369 res.l.high = rh / c;
370 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
371 return res.ll;
372}
373
bellardc4b1fcc2004-03-14 21:44:30 +0000374/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000375/* host time/date access */
376void qemu_get_timedate(struct tm *tm, int offset)
377{
378 time_t ti;
379 struct tm *ret;
380
381 time(&ti);
382 ti += offset;
383 if (rtc_date_offset == -1) {
384 if (rtc_utc)
385 ret = gmtime(&ti);
386 else
387 ret = localtime(&ti);
388 } else {
389 ti -= rtc_date_offset;
390 ret = gmtime(&ti);
391 }
392
393 memcpy(tm, ret, sizeof(struct tm));
394}
395
396int qemu_timedate_diff(struct tm *tm)
397{
398 time_t seconds;
399
400 if (rtc_date_offset == -1)
401 if (rtc_utc)
402 seconds = mktimegm(tm);
403 else
404 seconds = mktime(tm);
405 else
406 seconds = mktimegm(tm) + rtc_date_offset;
407
408 return seconds - time(NULL);
409}
410
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300411void rtc_change_mon_event(struct tm *tm)
412{
413 QObject *data;
414
415 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
416 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
417 qobject_decref(data);
418}
419
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200420static void configure_rtc_date_offset(const char *startdate, int legacy)
421{
422 time_t rtc_start_date;
423 struct tm tm;
424
425 if (!strcmp(startdate, "now") && legacy) {
426 rtc_date_offset = -1;
427 } else {
428 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
429 &tm.tm_year,
430 &tm.tm_mon,
431 &tm.tm_mday,
432 &tm.tm_hour,
433 &tm.tm_min,
434 &tm.tm_sec) == 6) {
435 /* OK */
436 } else if (sscanf(startdate, "%d-%d-%d",
437 &tm.tm_year,
438 &tm.tm_mon,
439 &tm.tm_mday) == 3) {
440 tm.tm_hour = 0;
441 tm.tm_min = 0;
442 tm.tm_sec = 0;
443 } else {
444 goto date_fail;
445 }
446 tm.tm_year -= 1900;
447 tm.tm_mon--;
448 rtc_start_date = mktimegm(&tm);
449 if (rtc_start_date == -1) {
450 date_fail:
451 fprintf(stderr, "Invalid date format. Valid formats are:\n"
452 "'2006-06-17T16:01:21' or '2006-06-17'\n");
453 exit(1);
454 }
455 rtc_date_offset = time(NULL) - rtc_start_date;
456 }
457}
458
459static void configure_rtc(QemuOpts *opts)
460{
461 const char *value;
462
463 value = qemu_opt_get(opts, "base");
464 if (value) {
465 if (!strcmp(value, "utc")) {
466 rtc_utc = 1;
467 } else if (!strcmp(value, "localtime")) {
468 rtc_utc = 0;
469 } else {
470 configure_rtc_date_offset(value, 0);
471 }
472 }
Jan Kiszka68752042009-09-15 13:36:04 +0200473 value = qemu_opt_get(opts, "clock");
474 if (value) {
475 if (!strcmp(value, "host")) {
476 rtc_clock = host_clock;
477 } else if (!strcmp(value, "vm")) {
478 rtc_clock = vm_clock;
479 } else {
480 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
481 exit(1);
482 }
483 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200484 value = qemu_opt_get(opts, "driftfix");
485 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000486 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200487 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000488 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200489 rtc_td_hack = 0;
490 } else {
491 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
492 exit(1);
493 }
494 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200495}
496
bellardfd1dff42006-02-01 21:29:26 +0000497#ifdef _WIN32
Anthony Liguori28695482010-03-21 14:13:02 -0500498static void socket_cleanup(void)
bellardfd1dff42006-02-01 21:29:26 +0000499{
500 WSACleanup();
501}
bellard82c643f2004-07-14 17:28:13 +0000502
bellardfd1dff42006-02-01 21:29:26 +0000503static int socket_init(void)
504{
505 WSADATA Data;
506 int ret, err;
507
508 ret = WSAStartup(MAKEWORD(2,2), &Data);
509 if (ret != 0) {
510 err = WSAGetLastError();
511 fprintf(stderr, "WSAStartup: %d\n", err);
512 return -1;
513 }
Anthony Liguori28695482010-03-21 14:13:02 -0500514 atexit(socket_cleanup);
bellardfd1dff42006-02-01 21:29:26 +0000515 return 0;
516}
aurel3264b7b732008-05-05 10:05:31 +0000517#endif
518
balrog1ae26a12008-09-28 23:19:47 +0000519/***********************************************************/
520/* Bluetooth support */
521static int nb_hcis;
522static int cur_hci;
523static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000524
balrog1ae26a12008-09-28 23:19:47 +0000525static struct bt_vlan_s {
526 struct bt_scatternet_s net;
527 int id;
528 struct bt_vlan_s *next;
529} *first_bt_vlan;
530
531/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000532static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000533{
534 struct bt_vlan_s **pvlan, *vlan;
535 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
536 if (vlan->id == id)
537 return &vlan->net;
538 }
539 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
540 vlan->id = id;
541 pvlan = &first_bt_vlan;
542 while (*pvlan != NULL)
543 pvlan = &(*pvlan)->next;
544 *pvlan = vlan;
545 return &vlan->net;
546}
547
548static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
549{
550}
551
552static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
553{
554 return -ENOTSUP;
555}
556
557static struct HCIInfo null_hci = {
558 .cmd_send = null_hci_send,
559 .sco_send = null_hci_send,
560 .acl_send = null_hci_send,
561 .bdaddr_set = null_hci_addr_set,
562};
563
564struct HCIInfo *qemu_next_hci(void)
565{
566 if (cur_hci == nb_hcis)
567 return &null_hci;
568
569 return hci_table[cur_hci++];
570}
571
balrogdc72ac12008-11-09 00:04:26 +0000572static struct HCIInfo *hci_init(const char *str)
573{
574 char *endp;
575 struct bt_scatternet_s *vlan = 0;
576
577 if (!strcmp(str, "null"))
578 /* null */
579 return &null_hci;
580 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
581 /* host[:hciN] */
582 return bt_host_hci(str[4] ? str + 5 : "hci0");
583 else if (!strncmp(str, "hci", 3)) {
584 /* hci[,vlan=n] */
585 if (str[3]) {
586 if (!strncmp(str + 3, ",vlan=", 6)) {
587 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
588 if (*endp)
589 vlan = 0;
590 }
591 } else
592 vlan = qemu_find_bt_vlan(0);
593 if (vlan)
594 return bt_new_hci(vlan);
595 }
596
597 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
598
599 return 0;
600}
601
602static int bt_hci_parse(const char *str)
603{
604 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500605 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000606
607 if (nb_hcis >= MAX_NICS) {
608 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
609 return -1;
610 }
611
612 hci = hci_init(str);
613 if (!hci)
614 return -1;
615
616 bdaddr.b[0] = 0x52;
617 bdaddr.b[1] = 0x54;
618 bdaddr.b[2] = 0x00;
619 bdaddr.b[3] = 0x12;
620 bdaddr.b[4] = 0x34;
621 bdaddr.b[5] = 0x56 + nb_hcis;
622 hci->bdaddr_set(hci, bdaddr.b);
623
624 hci_table[nb_hcis++] = hci;
625
626 return 0;
627}
628
629static void bt_vhci_add(int vlan_id)
630{
631 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
632
633 if (!vlan->slave)
634 fprintf(stderr, "qemu: warning: adding a VHCI to "
635 "an empty scatternet %i\n", vlan_id);
636
637 bt_vhci_init(bt_new_hci(vlan));
638}
639
640static struct bt_device_s *bt_device_add(const char *opt)
641{
642 struct bt_scatternet_s *vlan;
643 int vlan_id = 0;
644 char *endp = strstr(opt, ",vlan=");
645 int len = (endp ? endp - opt : strlen(opt)) + 1;
646 char devname[10];
647
648 pstrcpy(devname, MIN(sizeof(devname), len), opt);
649
650 if (endp) {
651 vlan_id = strtol(endp + 6, &endp, 0);
652 if (*endp) {
653 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
654 return 0;
655 }
656 }
657
658 vlan = qemu_find_bt_vlan(vlan_id);
659
660 if (!vlan->slave)
661 fprintf(stderr, "qemu: warning: adding a slave device to "
662 "an empty scatternet %i\n", vlan_id);
663
664 if (!strcmp(devname, "keyboard"))
665 return bt_keyboard_init(vlan);
666
667 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
668 return 0;
669}
670
671static int bt_parse(const char *opt)
672{
673 const char *endp, *p;
674 int vlan;
675
676 if (strstart(opt, "hci", &endp)) {
677 if (!*endp || *endp == ',') {
678 if (*endp)
679 if (!strstart(endp, ",vlan=", 0))
680 opt = endp + 1;
681
682 return bt_hci_parse(opt);
683 }
684 } else if (strstart(opt, "vhci", &endp)) {
685 if (!*endp || *endp == ',') {
686 if (*endp) {
687 if (strstart(endp, ",vlan=", &p)) {
688 vlan = strtol(p, (char **) &endp, 0);
689 if (*endp) {
690 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
691 return 1;
692 }
693 } else {
694 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
695 return 1;
696 }
697 } else
698 vlan = 0;
699
700 bt_vhci_add(vlan);
701 return 0;
702 }
703 } else if (strstart(opt, "device:", &endp))
704 return !bt_device_add(endp);
705
706 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
707 return 1;
708}
709
balrog1ae26a12008-09-28 23:19:47 +0000710/***********************************************************/
711/* QEMU Block devices */
712
balrog609497a2008-01-14 02:56:53 +0000713#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +0000714#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +0000715#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +0000716#define PFLASH_ALIAS "if=pflash"
717#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +0000718#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +0000719
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200720QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +0000721{
722 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200723 char optstr[1024];
724 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +0000725
thse4bcb142007-12-02 04:51:10 +0000726 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200727 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +0000728 va_end(ap);
729
Markus Armbruster8212c642010-02-10 19:52:18 +0100730 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200731 if (!opts) {
732 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
733 __FUNCTION__, optstr);
734 return NULL;
735 }
736 if (file)
737 qemu_opt_set(opts, "file", file);
738 return opts;
aliguorib01b1112009-02-11 15:20:20 +0000739}
740
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200741DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +0000742{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200743 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +0000744
745 /* seek interface, bus and unit */
746
Blue Swirl72cf2d42009-09-12 07:36:22 +0000747 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200748 if (dinfo->type == type &&
749 dinfo->bus == bus &&
750 dinfo->unit == unit)
751 return dinfo;
752 }
thse4bcb142007-12-02 04:51:10 +0000753
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200754 return NULL;
thse4bcb142007-12-02 04:51:10 +0000755}
756
Gerd Hoffmann2e810b32009-07-31 12:25:38 +0200757DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +0200758{
759 DriveInfo *dinfo;
760
Blue Swirl72cf2d42009-09-12 07:36:22 +0000761 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +0200762 if (strcmp(id, dinfo->id))
763 continue;
764 return dinfo;
765 }
766 return NULL;
767}
768
thsf60d39b2007-12-17 03:55:57 +0000769int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +0000770{
771 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200772 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +0000773
774 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000775 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200776 if(dinfo->type == type &&
777 dinfo->bus > max_bus)
778 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +0000779 }
780 return max_bus;
781}
782
aliguorifa879c62009-01-07 17:32:33 +0000783const char *drive_get_serial(BlockDriverState *bdrv)
784{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200785 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +0000786
Blue Swirl72cf2d42009-09-12 07:36:22 +0000787 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200788 if (dinfo->bdrv == bdrv)
789 return dinfo->serial;
790 }
aliguorifa879c62009-01-07 17:32:33 +0000791
792 return "\0";
793}
794
Kevin Wolff7850092009-11-27 13:25:36 +0100795BlockInterfaceErrorAction drive_get_on_error(
796 BlockDriverState *bdrv, int is_read)
aliguori428c5702009-01-21 18:59:04 +0000797{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200798 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +0000799
Blue Swirl72cf2d42009-09-12 07:36:22 +0000800 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200801 if (dinfo->bdrv == bdrv)
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100802 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200803 }
aliguori428c5702009-01-21 18:59:04 +0000804
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100805 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +0000806}
807
aurel32a1620fa2008-04-29 05:58:01 +0000808static void bdrv_format_print(void *opaque, const char *name)
809{
810 fprintf(stderr, " %s", name);
811}
812
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200813void drive_uninit(DriveInfo *dinfo)
aliguorib01b1112009-02-11 15:20:20 +0000814{
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200815 qemu_opts_del(dinfo->opts);
816 bdrv_delete(dinfo->bdrv);
817 QTAILQ_REMOVE(&drives, dinfo, next);
818 qemu_free(dinfo);
aliguorib01b1112009-02-11 15:20:20 +0000819}
820
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100821static int parse_block_error_action(const char *buf, int is_read)
822{
823 if (!strcmp(buf, "ignore")) {
824 return BLOCK_ERR_IGNORE;
825 } else if (!is_read && !strcmp(buf, "enospc")) {
826 return BLOCK_ERR_STOP_ENOSPC;
827 } else if (!strcmp(buf, "stop")) {
828 return BLOCK_ERR_STOP_ANY;
829 } else if (!strcmp(buf, "report")) {
830 return BLOCK_ERR_REPORT;
831 } else {
832 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
833 buf, is_read ? "read" : "write");
834 return -1;
835 }
836}
837
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200838DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200839 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +0000840{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200841 const char *buf;
842 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +0000843 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200844 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +0000845 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +0000846 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +0000847 enum { MEDIA_DISK, MEDIA_CDROM } media;
848 int bus_id, unit_id;
849 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +0000850 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +0000851 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +0000852 int max_devs;
853 int index;
balrog33f00272007-12-24 14:33:24 +0000854 int cache;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200855 int aio = 0;
Naphtali Sprei59f26892009-10-26 16:25:16 +0200856 int ro = 0;
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100857 int bdrv_flags;
858 int on_read_error, on_write_error;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200859 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200860 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200861 int snapshot = 0;
862
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200863 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +0000864
thse4bcb142007-12-02 04:51:10 +0000865 translation = BIOS_ATA_TRANSLATION_AUTO;
Kevin Wolf0aa217e2009-06-30 13:06:04 +0200866 cache = 1;
thse4bcb142007-12-02 04:51:10 +0000867
Gerd Hoffmann4d007812009-08-31 14:23:57 +0200868 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +0000869 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +0000870 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +0000871 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +0000872 } else {
thsf60d39b2007-12-17 03:55:57 +0000873 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +0000874 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +0000875 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +0000876 }
877 media = MEDIA_DISK;
878
879 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200880 bus_id = qemu_opt_get_number(opts, "bus", 0);
881 unit_id = qemu_opt_get_number(opts, "unit", -1);
882 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +0000883
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200884 cyls = qemu_opt_get_number(opts, "cyls", 0);
885 heads = qemu_opt_get_number(opts, "heads", 0);
886 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +0000887
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200888 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Naphtali Sprei59f26892009-10-26 16:25:16 +0200889 ro = qemu_opt_get_bool(opts, "readonly", 0);
thse4bcb142007-12-02 04:51:10 +0000890
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200891 file = qemu_opt_get(opts, "file");
892 serial = qemu_opt_get(opts, "serial");
893
894 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +0000895 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +0000896 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +0000897 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +0000898 max_devs = MAX_IDE_DEVS;
899 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +0000900 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +0000901 max_devs = MAX_SCSI_DEVS;
902 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +0000903 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +0000904 max_devs = 0;
905 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +0000906 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +0000907 max_devs = 0;
908 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +0000909 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +0000910 max_devs = 0;
911 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +0000912 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +0000913 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +0000914 } else if (!strcmp(buf, "virtio")) {
915 type = IF_VIRTIO;
916 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +0000917 } else if (!strcmp(buf, "xen")) {
918 type = IF_XEN;
919 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +0200920 } else if (!strcmp(buf, "none")) {
921 type = IF_NONE;
922 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +0000923 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200924 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200925 return NULL;
thse4bcb142007-12-02 04:51:10 +0000926 }
927 }
928
thse4bcb142007-12-02 04:51:10 +0000929 if (cyls || heads || secs) {
Blue Swirl5afe3f02009-10-17 09:08:47 +0000930 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200931 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200932 return NULL;
thse4bcb142007-12-02 04:51:10 +0000933 }
Blue Swirl5afe3f02009-10-17 09:08:47 +0000934 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200935 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200936 return NULL;
thse4bcb142007-12-02 04:51:10 +0000937 }
Blue Swirl5afe3f02009-10-17 09:08:47 +0000938 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200939 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200940 return NULL;
thse4bcb142007-12-02 04:51:10 +0000941 }
942 }
943
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200944 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +0000945 if (!cyls) {
946 fprintf(stderr,
947 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200948 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200949 return NULL;
thse4bcb142007-12-02 04:51:10 +0000950 }
951 if (!strcmp(buf, "none"))
952 translation = BIOS_ATA_TRANSLATION_NONE;
953 else if (!strcmp(buf, "lba"))
954 translation = BIOS_ATA_TRANSLATION_LBA;
955 else if (!strcmp(buf, "auto"))
956 translation = BIOS_ATA_TRANSLATION_AUTO;
957 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200958 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200959 return NULL;
thse4bcb142007-12-02 04:51:10 +0000960 }
961 }
962
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200963 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +0000964 if (!strcmp(buf, "disk")) {
965 media = MEDIA_DISK;
966 } else if (!strcmp(buf, "cdrom")) {
967 if (cyls || secs || heads) {
968 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200969 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200970 return NULL;
thse4bcb142007-12-02 04:51:10 +0000971 }
972 media = MEDIA_CDROM;
973 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200974 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200975 return NULL;
thse4bcb142007-12-02 04:51:10 +0000976 }
977 }
978
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200979 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
aliguori9f7965c2008-10-14 14:42:54 +0000980 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +0000981 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +0000982 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +0000983 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +0000984 else if (!strcmp(buf, "writeback"))
985 cache = 2;
balrog33f00272007-12-24 14:33:24 +0000986 else {
987 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200988 return NULL;
balrog33f00272007-12-24 14:33:24 +0000989 }
990 }
991
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200992#ifdef CONFIG_LINUX_AIO
993 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
994 if (!strcmp(buf, "threads"))
995 aio = 0;
996 else if (!strcmp(buf, "native"))
997 aio = 1;
998 else {
999 fprintf(stderr, "qemu: invalid aio option\n");
1000 return NULL;
1001 }
1002 }
1003#endif
1004
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001005 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +00001006 if (strcmp(buf, "?") == 0) {
1007 fprintf(stderr, "qemu: Supported formats:");
1008 bdrv_iterate_format(bdrv_format_print, NULL);
1009 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001010 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +00001011 }
Markus Armbrustereb852012009-10-27 18:41:44 +01001012 drv = bdrv_find_whitelisted_format(buf);
aurel321e72d3b2008-04-28 20:26:45 +00001013 if (!drv) {
1014 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001015 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +00001016 }
1017 }
1018
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001019 on_write_error = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001020 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
aliguori869a5c62009-01-22 19:52:25 +00001021 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00001022 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001023 return NULL;
aliguori428c5702009-01-21 18:59:04 +00001024 }
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001025
1026 on_write_error = parse_block_error_action(buf, 0);
1027 if (on_write_error < 0) {
1028 return NULL;
1029 }
1030 }
1031
1032 on_read_error = BLOCK_ERR_REPORT;
1033 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolff35d68f2009-11-27 13:25:39 +01001034 if (type != IF_IDE && type != IF_VIRTIO) {
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001035 fprintf(stderr, "rerror is no supported by this format\n");
1036 return NULL;
1037 }
1038
1039 on_read_error = parse_block_error_action(buf, 1);
1040 if (on_read_error < 0) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001041 return NULL;
aliguori428c5702009-01-21 18:59:04 +00001042 }
1043 }
1044
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001045 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02001046 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001047 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001048 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02001049 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02001050 }
1051
thse4bcb142007-12-02 04:51:10 +00001052 /* compute bus and unit according index */
1053
1054 if (index != -1) {
1055 if (bus_id != 0 || unit_id != -1) {
1056 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001057 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001058 return NULL;
thse4bcb142007-12-02 04:51:10 +00001059 }
1060 if (max_devs == 0)
1061 {
1062 unit_id = index;
1063 bus_id = 0;
1064 } else {
1065 unit_id = index % max_devs;
1066 bus_id = index / max_devs;
1067 }
1068 }
1069
1070 /* if user doesn't specify a unit_id,
1071 * try to find the first free
1072 */
1073
1074 if (unit_id == -1) {
1075 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001076 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00001077 unit_id++;
1078 if (max_devs && unit_id >= max_devs) {
1079 unit_id -= max_devs;
1080 bus_id++;
1081 }
1082 }
1083 }
1084
1085 /* check unit id */
1086
1087 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001088 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1089 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001090 return NULL;
thse4bcb142007-12-02 04:51:10 +00001091 }
1092
1093 /*
1094 * ignore multiple definitions
1095 */
1096
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001097 if (drive_get(type, bus_id, unit_id) != NULL) {
1098 *fatal_error = 0;
1099 return NULL;
1100 }
thse4bcb142007-12-02 04:51:10 +00001101
1102 /* init */
1103
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001104 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02001105 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001106 dinfo->id = qemu_strdup(buf);
1107 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001108 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001109 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001110 if (type == IF_IDE || type == IF_SCSI)
1111 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1112 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001113 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001114 devname, bus_id, mediastr, unit_id);
1115 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001116 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001117 devname, mediastr, unit_id);
1118 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001119 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001120 dinfo->devaddr = devaddr;
1121 dinfo->type = type;
1122 dinfo->bus = bus_id;
1123 dinfo->unit = unit_id;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001124 dinfo->on_read_error = on_read_error;
1125 dinfo->on_write_error = on_write_error;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001126 dinfo->opts = opts;
1127 if (serial)
1128 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00001129 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00001130
thsf60d39b2007-12-17 03:55:57 +00001131 switch(type) {
thse4bcb142007-12-02 04:51:10 +00001132 case IF_IDE:
1133 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00001134 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00001135 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00001136 switch(media) {
1137 case MEDIA_DISK:
1138 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001139 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1140 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00001141 }
1142 break;
1143 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001144 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00001145 break;
1146 }
1147 break;
1148 case IF_SD:
1149 /* FIXME: This isn't really a floppy, but it's a reasonable
1150 approximation. */
1151 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001152 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00001153 break;
1154 case IF_PFLASH:
1155 case IF_MTD:
1156 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02001157 case IF_VIRTIO:
1158 /* add virtio block device */
1159 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1160 qemu_opt_set(opts, "driver", "virtio-blk-pci");
1161 qemu_opt_set(opts, "drive", dinfo->id);
1162 if (devaddr)
1163 qemu_opt_set(opts, "addr", devaddr);
1164 break;
Paul Brookaae94602009-05-14 22:35:06 +01001165 case IF_COUNT:
1166 abort();
thse4bcb142007-12-02 04:51:10 +00001167 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001168 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001169 *fatal_error = 0;
1170 return NULL;
1171 }
balrog33f00272007-12-24 14:33:24 +00001172 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00001173 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00001174 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00001175 cache = 2; /* always use write-back with snapshot */
1176 }
1177 if (cache == 0) /* no caching */
1178 bdrv_flags |= BDRV_O_NOCACHE;
1179 else if (cache == 2) /* write-back */
1180 bdrv_flags |= BDRV_O_CACHE_WB;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001181
1182 if (aio == 1) {
1183 bdrv_flags |= BDRV_O_NATIVE_AIO;
1184 } else {
1185 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
1186 }
1187
Naphtali Sprei59f26892009-10-26 16:25:16 +02001188 if (ro == 1) {
Naphtali Spreif5edb012010-01-17 16:48:13 +02001189 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY) {
1190 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
Naphtali Sprei59f26892009-10-26 16:25:16 +02001191 return NULL;
1192 }
Naphtali Sprei59f26892009-10-26 16:25:16 +02001193 }
Naphtali Spreib196b152010-01-17 16:48:12 +02001194 /*
1195 * cdrom is read-only. Set it now, after above interface checking
1196 * since readonly attribute not explicitly required, so no error.
1197 */
1198 if (media == MEDIA_CDROM) {
Naphtali Spreif5edb012010-01-17 16:48:13 +02001199 ro = 1;
Naphtali Spreib196b152010-01-17 16:48:12 +02001200 }
Naphtali Spreif5edb012010-01-17 16:48:13 +02001201 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001202
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001203 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05001204 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1205 file, strerror(errno));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001206 return NULL;
thse4bcb142007-12-02 04:51:10 +00001207 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001208
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001209 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00001210 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001211 *fatal_error = 0;
1212 return dinfo;
thse4bcb142007-12-02 04:51:10 +00001213}
1214
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001215static int drive_init_func(QemuOpts *opts, void *opaque)
1216{
1217 QEMUMachine *machine = opaque;
1218 int fatal_error = 0;
1219
1220 if (drive_init(opts, machine, &fatal_error) == NULL) {
1221 if (fatal_error)
1222 return 1;
1223 }
1224 return 0;
1225}
1226
1227static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1228{
1229 if (NULL == qemu_opt_get(opts, "snapshot")) {
1230 qemu_opt_set(opts, "snapshot", "on");
1231 }
1232 return 0;
1233}
1234
Jan Kiszka76e30d02009-07-02 00:19:02 +02001235void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1236{
1237 boot_set_handler = func;
1238 boot_set_opaque = opaque;
1239}
1240
1241int qemu_boot_set(const char *boot_devices)
1242{
1243 if (!boot_set_handler) {
1244 return -EINVAL;
1245 }
1246 return boot_set_handler(boot_set_opaque, boot_devices);
1247}
1248
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001249static int parse_bootdevices(char *devices)
1250{
1251 /* We just do some generic consistency checks */
1252 const char *p;
1253 int bitmap = 0;
1254
1255 for (p = devices; *p != '\0'; p++) {
1256 /* Allowed boot devices are:
1257 * a-b: floppy disk drives
1258 * c-f: IDE disk drives
1259 * g-m: machine implementation dependant drives
1260 * n-p: network devices
1261 * It's up to each machine implementation to check if the given boot
1262 * devices match the actual hardware implementation and firmware
1263 * features.
1264 */
1265 if (*p < 'a' || *p > 'p') {
1266 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1267 exit(1);
1268 }
1269 if (bitmap & (1 << (*p - 'a'))) {
1270 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1271 exit(1);
1272 }
1273 bitmap |= 1 << (*p - 'a');
1274 }
1275 return bitmap;
1276}
1277
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001278static void restore_boot_devices(void *opaque)
1279{
1280 char *standard_boot_devices = opaque;
1281
1282 qemu_boot_set(standard_boot_devices);
1283
1284 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1285 qemu_free(standard_boot_devices);
1286}
1287
aliguori268a3622009-04-21 22:30:27 +00001288static void numa_add(const char *optarg)
1289{
1290 char option[128];
1291 char *endptr;
1292 unsigned long long value, endvalue;
1293 int nodenr;
1294
1295 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1296 if (!strcmp(option, "node")) {
1297 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1298 nodenr = nb_numa_nodes;
1299 } else {
1300 nodenr = strtoull(option, NULL, 10);
1301 }
1302
1303 if (get_param_value(option, 128, "mem", optarg) == 0) {
1304 node_mem[nodenr] = 0;
1305 } else {
1306 value = strtoull(option, &endptr, 0);
1307 switch (*endptr) {
1308 case 0: case 'M': case 'm':
1309 value <<= 20;
1310 break;
1311 case 'G': case 'g':
1312 value <<= 30;
1313 break;
1314 }
1315 node_mem[nodenr] = value;
1316 }
1317 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1318 node_cpumask[nodenr] = 0;
1319 } else {
1320 value = strtoull(option, &endptr, 10);
1321 if (value >= 64) {
1322 value = 63;
1323 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1324 } else {
1325 if (*endptr == '-') {
1326 endvalue = strtoull(endptr+1, &endptr, 10);
1327 if (endvalue >= 63) {
1328 endvalue = 62;
1329 fprintf(stderr,
1330 "only 63 CPUs in NUMA mode supported.\n");
1331 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01001332 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +00001333 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01001334 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +00001335 }
1336 }
1337 node_cpumask[nodenr] = value;
1338 }
1339 nb_numa_nodes++;
1340 }
1341 return;
1342}
1343
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001344static void smp_parse(const char *optarg)
1345{
1346 int smp, sockets = 0, threads = 0, cores = 0;
1347 char *endptr;
1348 char option[128];
1349
1350 smp = strtoul(optarg, &endptr, 10);
1351 if (endptr != optarg) {
1352 if (*endptr == ',') {
1353 endptr++;
1354 }
1355 }
1356 if (get_param_value(option, 128, "sockets", endptr) != 0)
1357 sockets = strtoull(option, NULL, 10);
1358 if (get_param_value(option, 128, "cores", endptr) != 0)
1359 cores = strtoull(option, NULL, 10);
1360 if (get_param_value(option, 128, "threads", endptr) != 0)
1361 threads = strtoull(option, NULL, 10);
1362 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1363 max_cpus = strtoull(option, NULL, 10);
1364
1365 /* compute missing values, prefer sockets over cores over threads */
1366 if (smp == 0 || sockets == 0) {
1367 sockets = sockets > 0 ? sockets : 1;
1368 cores = cores > 0 ? cores : 1;
1369 threads = threads > 0 ? threads : 1;
1370 if (smp == 0) {
1371 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001372 }
1373 } else {
1374 if (cores == 0) {
1375 threads = threads > 0 ? threads : 1;
1376 cores = smp / (sockets * threads);
1377 } else {
Amit Shah5cdc9b72010-01-13 16:24:42 +05301378 if (sockets) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001379 threads = smp / (cores * sockets);
1380 }
1381 }
1382 }
1383 smp_cpus = smp;
1384 smp_cores = cores > 0 ? cores : 1;
1385 smp_threads = threads > 0 ? threads : 1;
1386 if (max_cpus == 0)
1387 max_cpus = smp_cpus;
1388}
1389
bellard330d0412003-07-26 18:11:40 +00001390/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001391/* USB devices */
1392
aliguoric0f4ce72009-03-05 23:01:01 +00001393static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00001394{
1395 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001396 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001397
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001398 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00001399 return -1;
1400
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001401 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1402 dev = usbdevice_create(devname);
1403 if (dev)
1404 goto done;
1405
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001406 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00001407 if (strstart(devname, "host:", &p)) {
1408 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +00001409 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1410 dev = usb_bt_init(devname[2] ? hci_init(p) :
1411 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001412 } else {
1413 return -1;
1414 }
pbrook0d92ed32006-05-21 16:30:15 +00001415 if (!dev)
1416 return -1;
1417
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001418done:
bellarda594cfb2005-11-06 16:13:29 +00001419 return 0;
1420}
1421
aliguori1f3870a2008-08-21 19:27:48 +00001422static int usb_device_del(const char *devname)
1423{
1424 int bus_num, addr;
1425 const char *p;
1426
aliguori5d0c5752008-09-14 01:07:41 +00001427 if (strstart(devname, "host:", &p))
1428 return usb_host_device_close(p);
1429
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001430 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00001431 return -1;
1432
1433 p = strchr(devname, '.');
1434 if (!p)
1435 return -1;
1436 bus_num = strtoul(devname, NULL, 0);
1437 addr = strtoul(p + 1, NULL, 0);
1438
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001439 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001440}
1441
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001442static int usb_parse(const char *cmdline)
1443{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001444 int r;
1445 r = usb_device_add(cmdline, 0);
1446 if (r < 0) {
1447 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1448 }
1449 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001450}
1451
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001452void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001453{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001454 const char *devname = qdict_get_str(qdict, "devname");
1455 if (usb_device_add(devname, 1) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001456 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001457 }
bellarda594cfb2005-11-06 16:13:29 +00001458}
1459
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001460void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001461{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001462 const char *devname = qdict_get_str(qdict, "devname");
1463 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001464 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001465 }
bellarda594cfb2005-11-06 16:13:29 +00001466}
1467
bellardf7cce892004-12-08 22:21:25 +00001468/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001469/* PCMCIA/Cardbus */
1470
1471static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001472 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001473 struct pcmcia_socket_entry_s *next;
1474} *pcmcia_sockets = 0;
1475
Paul Brookbc24a222009-05-10 01:44:56 +01001476void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001477{
1478 struct pcmcia_socket_entry_s *entry;
1479
1480 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1481 entry->socket = socket;
1482 entry->next = pcmcia_sockets;
1483 pcmcia_sockets = entry;
1484}
1485
Paul Brookbc24a222009-05-10 01:44:56 +01001486void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001487{
1488 struct pcmcia_socket_entry_s *entry, **ptr;
1489
1490 ptr = &pcmcia_sockets;
1491 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1492 if (entry->socket == socket) {
1493 *ptr = entry->next;
1494 qemu_free(entry);
1495 }
1496}
1497
aliguori376253e2009-03-05 23:01:23 +00001498void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001499{
1500 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001501
balrog201a51f2007-04-30 00:51:09 +00001502 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001503 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001504
1505 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001506 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1507 iter->socket->attached ? iter->socket->card_string :
1508 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001509}
1510
1511/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001512/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00001513
bellardc4b1fcc2004-03-14 21:44:30 +00001514typedef struct IOHandlerRecord {
1515 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +01001516 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +00001517 IOHandler *fd_read;
1518 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00001519 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00001520 void *opaque;
1521 /* temporary data */
1522 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +01001523 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +00001524} IOHandlerRecord;
1525
Juan Quintela31d4ee62010-03-11 17:55:37 +01001526static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1527 QLIST_HEAD_INITIALIZER(io_handlers);
1528
bellardc4b1fcc2004-03-14 21:44:30 +00001529
bellard7c9d8e02005-11-15 22:16:05 +00001530/* XXX: fd_read_poll should be suppressed, but an API change is
1531 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00001532int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +01001533 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +00001534 IOHandler *fd_read,
1535 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001536 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00001537{
Juan Quintela31d4ee62010-03-11 17:55:37 +01001538 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +00001539
bellard7c9d8e02005-11-15 22:16:05 +00001540 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001541 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001542 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00001543 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00001544 break;
1545 }
bellard8a7ddc32004-03-31 19:00:16 +00001546 }
bellard7c9d8e02005-11-15 22:16:05 +00001547 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001548 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001549 if (ioh->fd == fd)
1550 goto found;
1551 }
1552 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +01001553 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +00001554 found:
1555 ioh->fd = fd;
1556 ioh->fd_read_poll = fd_read_poll;
1557 ioh->fd_read = fd_read;
1558 ioh->fd_write = fd_write;
1559 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00001560 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00001561 }
bellard7c9d8e02005-11-15 22:16:05 +00001562 return 0;
1563}
1564
ths5fafdf22007-09-16 21:08:06 +00001565int qemu_set_fd_handler(int fd,
1566 IOHandler *fd_read,
1567 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001568 void *opaque)
1569{
1570 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00001571}
1572
aliguori56f3a5d2008-10-31 18:07:17 +00001573#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00001574/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00001575/* Polling handling */
1576
1577typedef struct PollingEntry {
1578 PollingFunc *func;
1579 void *opaque;
1580 struct PollingEntry *next;
1581} PollingEntry;
1582
1583static PollingEntry *first_polling_entry;
1584
1585int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1586{
1587 PollingEntry **ppe, *pe;
1588 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00001589 pe->func = func;
1590 pe->opaque = opaque;
1591 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1592 *ppe = pe;
1593 return 0;
1594}
1595
1596void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1597{
1598 PollingEntry **ppe, *pe;
1599 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1600 pe = *ppe;
1601 if (pe->func == func && pe->opaque == opaque) {
1602 *ppe = pe->next;
1603 qemu_free(pe);
1604 break;
1605 }
1606 }
1607}
1608
bellarda18e5242006-06-25 17:18:27 +00001609/***********************************************************/
1610/* Wait objects support */
1611typedef struct WaitObjects {
1612 int num;
1613 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1614 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1615 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1616} WaitObjects;
1617
1618static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00001619
bellarda18e5242006-06-25 17:18:27 +00001620int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1621{
1622 WaitObjects *w = &wait_objects;
1623
1624 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1625 return -1;
1626 w->events[w->num] = handle;
1627 w->func[w->num] = func;
1628 w->opaque[w->num] = opaque;
1629 w->num++;
1630 return 0;
1631}
1632
1633void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1634{
1635 int i, found;
1636 WaitObjects *w = &wait_objects;
1637
1638 found = 0;
1639 for (i = 0; i < w->num; i++) {
1640 if (w->events[i] == handle)
1641 found = 1;
1642 if (found) {
1643 w->events[i] = w->events[i + 1];
1644 w->func[i] = w->func[i + 1];
1645 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00001646 }
bellarda18e5242006-06-25 17:18:27 +00001647 }
1648 if (found)
1649 w->num--;
1650}
1651#endif
1652
bellard8a7ddc32004-03-31 19:00:16 +00001653/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001654/* machine registration */
1655
blueswir1bdaf78e2008-10-04 07:24:27 +00001656static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001657QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001658
1659int qemu_register_machine(QEMUMachine *m)
1660{
1661 QEMUMachine **pm;
1662 pm = &first_machine;
1663 while (*pm != NULL)
1664 pm = &(*pm)->next;
1665 m->next = NULL;
1666 *pm = m;
1667 return 0;
1668}
1669
pbrook9596ebb2007-11-18 01:44:38 +00001670static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001671{
1672 QEMUMachine *m;
1673
1674 for(m = first_machine; m != NULL; m = m->next) {
1675 if (!strcmp(m->name, name))
1676 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001677 if (m->alias && !strcmp(m->alias, name))
1678 return m;
bellardcc1daa42005-06-05 14:49:17 +00001679 }
1680 return NULL;
1681}
1682
Anthony Liguori0c257432009-05-21 20:41:01 -05001683static QEMUMachine *find_default_machine(void)
1684{
1685 QEMUMachine *m;
1686
1687 for(m = first_machine; m != NULL; m = m->next) {
1688 if (m->is_default) {
1689 return m;
1690 }
1691 }
1692 return NULL;
1693}
1694
bellardcc1daa42005-06-05 14:49:17 +00001695/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001696/* main execution loop */
1697
pbrook9596ebb2007-11-18 01:44:38 +00001698static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001699{
aliguori7d957bd2009-01-15 22:14:11 +00001700 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001701 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001702 DisplayChangeListener *dcl = ds->listeners;
1703
Sheng Yang62a27442010-01-26 19:21:16 +08001704 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001705 dpy_refresh(ds);
1706
1707 while (dcl != NULL) {
1708 if (dcl->gui_timer_interval &&
1709 dcl->gui_timer_interval < interval)
1710 interval = dcl->gui_timer_interval;
1711 dcl = dcl->next;
1712 }
1713 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001714}
1715
blueswir19043b622009-01-21 19:28:13 +00001716static void nographic_update(void *opaque)
1717{
1718 uint64_t interval = GUI_REFRESH_INTERVAL;
1719
Sheng Yang62a27442010-01-26 19:21:16 +08001720 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00001721 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1722}
1723
bellard0bd48852005-11-11 00:00:47 +00001724struct vm_change_state_entry {
1725 VMChangeStateHandler *cb;
1726 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001727 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001728};
1729
Blue Swirl72cf2d42009-09-12 07:36:22 +00001730static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001731
1732VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1733 void *opaque)
1734{
1735 VMChangeStateEntry *e;
1736
1737 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001738
1739 e->cb = cb;
1740 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001741 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001742 return e;
1743}
1744
1745void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1746{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001747 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001748 qemu_free (e);
1749}
1750
Blue Swirl296af7c2010-03-29 19:23:50 +00001751void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001752{
1753 VMChangeStateEntry *e;
1754
1755 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001756 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001757 }
1758}
1759
bellard8a7ddc32004-03-31 19:00:16 +00001760void vm_start(void)
1761{
1762 if (!vm_running) {
1763 cpu_enable_ticks();
1764 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001765 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001766 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00001767 }
1768}
1769
bellardbb0c6722004-06-20 12:37:32 +00001770/* reset/shutdown handler */
1771
1772typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001773 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001774 QEMUResetHandler *func;
1775 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001776} QEMUResetEntry;
1777
Blue Swirl72cf2d42009-09-12 07:36:22 +00001778static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1779 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001780static int reset_requested;
1781static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00001782static int powerdown_requested;
Blue Swirl296af7c2010-03-29 19:23:50 +00001783int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00001784static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001785
aurel32cf7a2fe2008-03-18 06:53:05 +00001786int qemu_shutdown_requested(void)
1787{
1788 int r = shutdown_requested;
1789 shutdown_requested = 0;
1790 return r;
1791}
1792
1793int qemu_reset_requested(void)
1794{
1795 int r = reset_requested;
1796 reset_requested = 0;
1797 return r;
1798}
1799
1800int qemu_powerdown_requested(void)
1801{
1802 int r = powerdown_requested;
1803 powerdown_requested = 0;
1804 return r;
1805}
1806
aliguorie5689022009-04-24 18:03:54 +00001807static int qemu_debug_requested(void)
1808{
1809 int r = debug_requested;
1810 debug_requested = 0;
1811 return r;
1812}
1813
aliguori6e29f5d2009-04-24 18:04:02 +00001814static int qemu_vmstop_requested(void)
1815{
1816 int r = vmstop_requested;
1817 vmstop_requested = 0;
1818 return r;
1819}
1820
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001821void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001822{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001823 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001824
bellardbb0c6722004-06-20 12:37:32 +00001825 re->func = func;
1826 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001827 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001828}
1829
Jan Kiszkadda9b292009-07-02 00:19:02 +02001830void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001831{
1832 QEMUResetEntry *re;
1833
Blue Swirl72cf2d42009-09-12 07:36:22 +00001834 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001835 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001836 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001837 qemu_free(re);
1838 return;
1839 }
1840 }
1841}
1842
1843void qemu_system_reset(void)
1844{
1845 QEMUResetEntry *re, *nre;
1846
1847 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001848 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001849 re->func(re->opaque);
1850 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001851 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001852 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001853}
1854
1855void qemu_system_reset_request(void)
1856{
bellardd1beab82006-10-02 19:44:22 +00001857 if (no_reboot) {
1858 shutdown_requested = 1;
1859 } else {
1860 reset_requested = 1;
1861 }
aliguorid9f75a42009-04-24 18:03:11 +00001862 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001863}
1864
1865void qemu_system_shutdown_request(void)
1866{
1867 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001868 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001869}
1870
bellard34751872005-07-02 14:31:34 +00001871void qemu_system_powerdown_request(void)
1872{
1873 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001874 qemu_notify_event();
1875}
1876
ths877cf882007-04-18 18:11:47 +00001877#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00001878static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00001879{
1880 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00001881 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00001882
bellardf3311102006-04-12 20:21:17 +00001883
1884 /* XXX: need to suppress polling by better using win32 events */
1885 ret = 0;
1886 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
1887 ret |= pe->func(pe->opaque);
1888 }
thse6b1e552007-04-18 17:56:02 +00001889 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00001890 int err;
1891 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00001892
aliguori56f3a5d2008-10-31 18:07:17 +00001893 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00001894 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
1895 if (w->func[ret - WAIT_OBJECT_0])
1896 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00001897
ths5fafdf22007-09-16 21:08:06 +00001898 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00001899 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00001900
thse6b1e552007-04-18 17:56:02 +00001901 /* Check if event is signaled */
1902 ret2 = WaitForSingleObject(w->events[i], 0);
1903 if(ret2 == WAIT_OBJECT_0) {
1904 if (w->func[i])
1905 w->func[i](w->opaque[i]);
1906 } else if (ret2 == WAIT_TIMEOUT) {
1907 } else {
1908 err = GetLastError();
1909 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00001910 }
1911 }
bellarda18e5242006-06-25 17:18:27 +00001912 } else if (ret == WAIT_TIMEOUT) {
1913 } else {
1914 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00001915 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00001916 }
bellardf3311102006-04-12 20:21:17 +00001917 }
aliguori56f3a5d2008-10-31 18:07:17 +00001918
1919 *timeout = 0;
1920}
1921#else
blueswir169d64512008-12-07 19:30:18 +00001922static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00001923{
1924}
bellardfd1dff42006-02-01 21:29:26 +00001925#endif
aliguori56f3a5d2008-10-31 18:07:17 +00001926
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001927void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001928{
1929 IOHandlerRecord *ioh;
1930 fd_set rfds, wfds, xfds;
1931 int ret, nfds;
1932 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001933 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001934
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001935 if (nonblocking)
1936 timeout = 0;
1937 else {
1938 timeout = qemu_calculate_timeout();
1939 qemu_bh_update_timeout(&timeout);
1940 }
aliguori56f3a5d2008-10-31 18:07:17 +00001941
1942 host_main_loop_wait(&timeout);
1943
bellardfd1dff42006-02-01 21:29:26 +00001944 /* poll any events */
1945 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001946 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001947 FD_ZERO(&rfds);
1948 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001949 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001950 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00001951 if (ioh->deleted)
1952 continue;
bellardfd1dff42006-02-01 21:29:26 +00001953 if (ioh->fd_read &&
1954 (!ioh->fd_read_poll ||
1955 ioh->fd_read_poll(ioh->opaque) != 0)) {
1956 FD_SET(ioh->fd, &rfds);
1957 if (ioh->fd > nfds)
1958 nfds = ioh->fd;
1959 }
1960 if (ioh->fd_write) {
1961 FD_SET(ioh->fd, &wfds);
1962 if (ioh->fd > nfds)
1963 nfds = ioh->fd;
1964 }
1965 }
ths3b46e622007-09-17 08:09:54 +00001966
aliguori56f3a5d2008-10-31 18:07:17 +00001967 tv.tv_sec = timeout / 1000;
1968 tv.tv_usec = (timeout % 1000) * 1000;
1969
Jan Kiszkad918f232009-06-24 14:42:30 +02001970 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1971
aliguori48708522009-04-24 18:03:49 +00001972 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001973 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001974 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00001975 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001976 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00001977
Juan Quintela31d4ee62010-03-11 17:55:37 +01001978 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
thscafffd42007-02-28 21:59:44 +00001979 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001980 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00001981 qemu_free(ioh);
Juan Quintela4bed9832010-03-11 17:55:41 +01001982 continue;
1983 }
1984 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1985 ioh->fd_read(ioh->opaque);
1986 }
1987 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1988 ioh->fd_write(ioh->opaque);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001989 }
thscafffd42007-02-28 21:59:44 +00001990 }
bellardfd1dff42006-02-01 21:29:26 +00001991 }
Jan Kiszkad918f232009-06-24 14:42:30 +02001992
1993 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001994
Paolo Bonzinib6964822010-03-10 11:38:45 +01001995 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001996
pbrook423f0742007-05-23 00:06:54 +00001997 /* Check bottom-halves last in case any of the earlier events triggered
1998 them. */
1999 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00002000
bellard5905b2e2004-08-01 21:53:26 +00002001}
2002
aliguori43b96852009-04-24 18:03:33 +00002003static int vm_can_run(void)
2004{
2005 if (powerdown_requested)
2006 return 0;
2007 if (reset_requested)
2008 return 0;
2009 if (shutdown_requested)
2010 return 0;
aliguorie5689022009-04-24 18:03:54 +00002011 if (debug_requested)
2012 return 0;
aliguori43b96852009-04-24 18:03:33 +00002013 return 1;
2014}
2015
Blue Swirld9c32312009-08-09 08:42:19 +00002016qemu_irq qemu_system_powerdown;
2017
aliguori43b96852009-04-24 18:03:33 +00002018static void main_loop(void)
2019{
aliguori6e29f5d2009-04-24 18:04:02 +00002020 int r;
aliguori43b96852009-04-24 18:03:33 +00002021
aliguorid6dc3d42009-04-24 18:04:07 +00002022#ifdef CONFIG_IOTHREAD
2023 qemu_system_ready = 1;
2024 qemu_cond_broadcast(&qemu_system_cond);
2025#endif
2026
aliguori6e29f5d2009-04-24 18:04:02 +00002027 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00002028 do {
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002029 bool nonblocking = false;
aliguorie6e35b12009-04-24 18:03:57 +00002030#ifdef CONFIG_PROFILER
2031 int64_t ti;
2032#endif
aliguorid6dc3d42009-04-24 18:04:07 +00002033#ifndef CONFIG_IOTHREAD
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002034 nonblocking = tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00002035#endif
aliguori43b96852009-04-24 18:03:33 +00002036#ifdef CONFIG_PROFILER
2037 ti = profile_getclock();
2038#endif
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01002039 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00002040#ifdef CONFIG_PROFILER
2041 dev_time += profile_getclock() - ti;
2042#endif
aliguorie5689022009-04-24 18:03:54 +00002043 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00002044
Blue Swirl54fc6ea2010-03-29 19:23:46 +00002045 if ((r = qemu_debug_requested())) {
2046 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02002047 }
aliguori43b96852009-04-24 18:03:33 +00002048 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00002049 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00002050 if (no_shutdown) {
2051 vm_stop(0);
2052 no_shutdown = 0;
2053 } else
2054 break;
2055 }
aliguorid6dc3d42009-04-24 18:04:07 +00002056 if (qemu_reset_requested()) {
2057 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00002058 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00002059 resume_all_vcpus();
2060 }
Blue Swirld9c32312009-08-09 08:42:19 +00002061 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00002062 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00002063 qemu_irq_raise(qemu_system_powerdown);
2064 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02002065 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00002066 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02002067 }
aliguori43b96852009-04-24 18:03:33 +00002068 }
aliguorid6dc3d42009-04-24 18:04:07 +00002069 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00002070}
2071
pbrook9bd7e6d2009-04-07 22:58:45 +00002072static void version(void)
2073{
pbrook4a19f1e2009-04-07 23:17:49 +00002074 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00002075}
2076
ths15f82202007-06-29 23:26:08 +00002077static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00002078{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01002079 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00002080#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2081 opt_help
blueswir15824d652009-03-28 06:44:27 +00002082#define DEFHEADING(text) stringify(text) "\n"
2083#include "qemu-options.h"
2084#undef DEF
2085#undef DEFHEADING
2086#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01002087 ;
2088 version();
2089 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03002090 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01002091 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2092 "\n"
2093 "%s\n"
malc3f020d72010-02-08 12:04:56 +03002094 "During emulation, the following keys are useful:\n"
2095 "ctrl-alt-f toggle full screen\n"
2096 "ctrl-alt-n switch to virtual console 'n'\n"
2097 "ctrl-alt toggle mouse and keyboard grab\n"
2098 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01002099 "When using -nographic, press 'ctrl-a h' to get some help.\n",
2100 "qemu",
2101 options_help);
ths15f82202007-06-29 23:26:08 +00002102 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00002103}
2104
bellardcd6f1162004-05-13 22:02:20 +00002105#define HAS_ARG 0x0001
2106
2107enum {
Blue Swirlad960902010-03-29 19:23:52 +00002108#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
blueswir15824d652009-03-28 06:44:27 +00002109 opt_enum,
2110#define DEFHEADING(text)
2111#include "qemu-options.h"
2112#undef DEF
2113#undef DEFHEADING
2114#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00002115};
2116
2117typedef struct QEMUOption {
2118 const char *name;
2119 int flags;
2120 int index;
Blue Swirlad960902010-03-29 19:23:52 +00002121 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00002122} QEMUOption;
2123
blueswir1dbed7e42008-10-01 19:38:09 +00002124static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00002125 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2126#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2127 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00002128#define DEFHEADING(text)
2129#include "qemu-options.h"
2130#undef DEF
2131#undef DEFHEADING
2132#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00002133 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00002134};
malc3893c122008-09-28 00:42:05 +00002135static void select_vgahw (const char *p)
2136{
2137 const char *opts;
2138
Gerd Hoffmann64465292009-12-08 13:11:45 +01002139 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10002140 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00002141 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002142 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00002143 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002144 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00002145 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002146 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00002147 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002148 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00002149 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00002150 invalid_vga:
2151 fprintf(stderr, "Unknown vga type: %s\n", p);
2152 exit(1);
2153 }
malccb5a7aa2008-09-28 00:42:12 +00002154 while (*opts) {
2155 const char *nextopt;
2156
2157 if (strstart(opts, ",retrace=", &nextopt)) {
2158 opts = nextopt;
2159 if (strstart(opts, "dumb", &nextopt))
2160 vga_retrace_method = VGA_RETRACE_DUMB;
2161 else if (strstart(opts, "precise", &nextopt))
2162 vga_retrace_method = VGA_RETRACE_PRECISE;
2163 else goto invalid_vga;
2164 } else goto invalid_vga;
2165 opts = nextopt;
2166 }
malc3893c122008-09-28 00:42:05 +00002167}
2168
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002169static int balloon_parse(const char *arg)
2170{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002171 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002172
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002173 if (strcmp(arg, "none") == 0) {
2174 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002175 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002176
2177 if (!strncmp(arg, "virtio", 6)) {
2178 if (arg[6] == ',') {
2179 /* have params -> parse them */
Markus Armbruster8212c642010-02-10 19:52:18 +01002180 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002181 if (!opts)
2182 return -1;
2183 } else {
2184 /* create empty opts */
2185 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2186 }
2187 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
2188 return 0;
2189 }
2190
2191 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002192}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002193
bellard3587d7e2006-06-26 20:03:44 +00002194#ifdef _WIN32
2195static BOOL WINAPI qemu_ctrl_handler(DWORD type)
2196{
2197 exit(STATUS_CONTROL_C_EXIT);
2198 return TRUE;
2199}
2200#endif
2201
aliguori5b08fc12008-08-21 20:08:03 +00002202#ifndef _WIN32
2203
2204static void termsig_handler(int signal)
2205{
2206 qemu_system_shutdown_request();
2207}
2208
Jan Kiszka7c3370d2009-05-08 12:34:17 +02002209static void sigchld_handler(int signal)
2210{
2211 waitpid(-1, NULL, WNOHANG);
2212}
2213
2214static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00002215{
2216 struct sigaction act;
2217
2218 memset(&act, 0, sizeof(act));
2219 act.sa_handler = termsig_handler;
2220 sigaction(SIGINT, &act, NULL);
2221 sigaction(SIGHUP, &act, NULL);
2222 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02002223
2224 act.sa_handler = sigchld_handler;
2225 act.sa_flags = SA_NOCLDSTOP;
2226 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00002227}
2228
2229#endif
2230
Paul Brook5cea8592009-05-30 00:52:44 +01002231#ifdef _WIN32
2232/* Look for support files in the same directory as the executable. */
2233static char *find_datadir(const char *argv0)
2234{
2235 char *p;
2236 char buf[MAX_PATH];
2237 DWORD len;
2238
2239 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2240 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03002241 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01002242 }
2243
2244 buf[len] = 0;
2245 p = buf + len - 1;
2246 while (p != buf && *p != '\\')
2247 p--;
2248 *p = 0;
2249 if (access(buf, R_OK) == 0) {
2250 return qemu_strdup(buf);
2251 }
2252 return NULL;
2253}
2254#else /* !_WIN32 */
2255
2256/* Find a likely location for support files using the location of the binary.
2257 For installed binaries this will be "$bindir/../share/qemu". When
2258 running from the build tree this will be "$bindir/../pc-bios". */
2259#define SHARE_SUFFIX "/share/qemu"
2260#define BUILD_SUFFIX "/pc-bios"
2261static char *find_datadir(const char *argv0)
2262{
2263 char *dir;
2264 char *p = NULL;
2265 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01002266 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00002267 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01002268
2269#if defined(__linux__)
2270 {
2271 int len;
2272 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2273 if (len > 0) {
2274 buf[len] = 0;
2275 p = buf;
2276 }
2277 }
2278#elif defined(__FreeBSD__)
2279 {
Juergen Lock92c0e652010-03-25 22:07:12 +01002280 static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
2281 size_t len = sizeof(buf) - 1;
2282
2283 *buf = '\0';
2284 if (!sysctl(mib, sizeof(mib)/sizeof(*mib), buf, &len, NULL, 0) &&
2285 *buf) {
2286 buf[sizeof(buf) - 1] = '\0';
Paul Brook5cea8592009-05-30 00:52:44 +01002287 p = buf;
2288 }
2289 }
2290#endif
2291 /* If we don't have any way of figuring out the actual executable
2292 location then try argv[0]. */
2293 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02002294 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01002295 if (!p) {
2296 return NULL;
2297 }
2298 }
2299 dir = dirname(p);
2300 dir = dirname(dir);
2301
Blue Swirl3a417592009-06-09 19:12:21 +00002302 max_len = strlen(dir) +
2303 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
2304 res = qemu_mallocz(max_len);
2305 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01002306 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00002307 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01002308 if (access(res, R_OK)) {
2309 qemu_free(res);
2310 res = NULL;
2311 }
2312 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02002313
Paul Brook5cea8592009-05-30 00:52:44 +01002314 return res;
2315}
2316#undef SHARE_SUFFIX
2317#undef BUILD_SUFFIX
2318#endif
2319
2320char *qemu_find_file(int type, const char *name)
2321{
2322 int len;
2323 const char *subdir;
2324 char *buf;
2325
2326 /* If name contains path separators then try it as a straight path. */
2327 if ((strchr(name, '/') || strchr(name, '\\'))
2328 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02002329 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01002330 }
2331 switch (type) {
2332 case QEMU_FILE_TYPE_BIOS:
2333 subdir = "";
2334 break;
2335 case QEMU_FILE_TYPE_KEYMAP:
2336 subdir = "keymaps/";
2337 break;
2338 default:
2339 abort();
2340 }
2341 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2342 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00002343 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01002344 if (access(buf, R_OK)) {
2345 qemu_free(buf);
2346 return NULL;
2347 }
2348 return buf;
2349}
2350
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002351static int device_help_func(QemuOpts *opts, void *opaque)
2352{
2353 return qdev_device_help(opts);
2354}
2355
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002356static int device_init_func(QemuOpts *opts, void *opaque)
2357{
2358 DeviceState *dev;
2359
2360 dev = qdev_device_add(opts);
2361 if (!dev)
2362 return -1;
2363 return 0;
2364}
2365
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002366static int chardev_init_func(QemuOpts *opts, void *opaque)
2367{
2368 CharDriverState *chr;
2369
2370 chr = qemu_chr_open_opts(opts, NULL);
2371 if (!chr)
2372 return -1;
2373 return 0;
2374}
2375
Gerd Hoffmann88589342009-12-08 13:11:50 +01002376static int mon_init_func(QemuOpts *opts, void *opaque)
2377{
2378 CharDriverState *chr;
2379 const char *chardev;
2380 const char *mode;
2381 int flags;
2382
2383 mode = qemu_opt_get(opts, "mode");
2384 if (mode == NULL) {
2385 mode = "readline";
2386 }
2387 if (strcmp(mode, "readline") == 0) {
2388 flags = MONITOR_USE_READLINE;
2389 } else if (strcmp(mode, "control") == 0) {
2390 flags = MONITOR_USE_CONTROL;
2391 } else {
2392 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2393 exit(1);
2394 }
2395
2396 if (qemu_opt_get_bool(opts, "default", 0))
2397 flags |= MONITOR_IS_DEFAULT;
2398
2399 chardev = qemu_opt_get(opts, "chardev");
2400 chr = qemu_chr_find(chardev);
2401 if (chr == NULL) {
2402 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2403 exit(1);
2404 }
2405
2406 monitor_init(chr, flags);
2407 return 0;
2408}
2409
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002410static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002411{
2412 static int monitor_device_index = 0;
2413 QemuOpts *opts;
2414 const char *p;
2415 char label[32];
2416 int def = 0;
2417
2418 if (strstart(optarg, "chardev:", &p)) {
2419 snprintf(label, sizeof(label), "%s", p);
2420 } else {
2421 if (monitor_device_index) {
2422 snprintf(label, sizeof(label), "monitor%d",
2423 monitor_device_index);
2424 } else {
2425 snprintf(label, sizeof(label), "monitor");
2426 def = 1;
2427 }
2428 opts = qemu_chr_parse_compat(label, optarg);
2429 if (!opts) {
2430 fprintf(stderr, "parse error: %s\n", optarg);
2431 exit(1);
2432 }
2433 }
2434
2435 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
2436 if (!opts) {
2437 fprintf(stderr, "duplicate chardev: %s\n", label);
2438 exit(1);
2439 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002440 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002441 qemu_opt_set(opts, "chardev", label);
2442 if (def)
2443 qemu_opt_set(opts, "default", "on");
2444 monitor_device_index++;
2445}
2446
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002447struct device_config {
2448 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002449 DEV_USB, /* -usbdevice */
2450 DEV_BT, /* -bt */
2451 DEV_SERIAL, /* -serial */
2452 DEV_PARALLEL, /* -parallel */
2453 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002454 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002455 } type;
2456 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002457 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002458};
Blue Swirl72cf2d42009-09-12 07:36:22 +00002459QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002460
2461static void add_device_config(int type, const char *cmdline)
2462{
2463 struct device_config *conf;
2464
2465 conf = qemu_mallocz(sizeof(*conf));
2466 conf->type = type;
2467 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002468 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002469}
2470
2471static int foreach_device_config(int type, int (*func)(const char *cmdline))
2472{
2473 struct device_config *conf;
2474 int rc;
2475
Blue Swirl72cf2d42009-09-12 07:36:22 +00002476 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002477 if (conf->type != type)
2478 continue;
2479 rc = func(conf->cmdline);
2480 if (0 != rc)
2481 return rc;
2482 }
2483 return 0;
2484}
2485
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002486static int serial_parse(const char *devname)
2487{
2488 static int index = 0;
2489 char label[32];
2490
2491 if (strcmp(devname, "none") == 0)
2492 return 0;
2493 if (index == MAX_SERIAL_PORTS) {
2494 fprintf(stderr, "qemu: too many serial ports\n");
2495 exit(1);
2496 }
2497 snprintf(label, sizeof(label), "serial%d", index);
2498 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2499 if (!serial_hds[index]) {
2500 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2501 devname, strerror(errno));
2502 return -1;
2503 }
2504 index++;
2505 return 0;
2506}
2507
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002508static int parallel_parse(const char *devname)
2509{
2510 static int index = 0;
2511 char label[32];
2512
2513 if (strcmp(devname, "none") == 0)
2514 return 0;
2515 if (index == MAX_PARALLEL_PORTS) {
2516 fprintf(stderr, "qemu: too many parallel ports\n");
2517 exit(1);
2518 }
2519 snprintf(label, sizeof(label), "parallel%d", index);
2520 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
2521 if (!parallel_hds[index]) {
2522 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2523 devname, strerror(errno));
2524 return -1;
2525 }
2526 index++;
2527 return 0;
2528}
2529
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002530static int virtcon_parse(const char *devname)
2531{
2532 static int index = 0;
2533 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302534 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002535
2536 if (strcmp(devname, "none") == 0)
2537 return 0;
2538 if (index == MAX_VIRTIO_CONSOLES) {
2539 fprintf(stderr, "qemu: too many virtio consoles\n");
2540 exit(1);
2541 }
Amit Shah392ecf52010-01-21 16:19:23 +05302542
2543 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2544 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2545
2546 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2547 qemu_opt_set(dev_opts, "driver", "virtconsole");
2548
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002549 snprintf(label, sizeof(label), "virtcon%d", index);
2550 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
2551 if (!virtcon_hds[index]) {
2552 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2553 devname, strerror(errno));
2554 return -1;
2555 }
Amit Shah392ecf52010-01-21 16:19:23 +05302556 qemu_opt_set(dev_opts, "chardev", label);
2557
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002558 index++;
2559 return 0;
2560}
2561
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002562static int debugcon_parse(const char *devname)
2563{
2564 QemuOpts *opts;
2565
2566 if (!qemu_chr_open("debugcon", devname, NULL)) {
2567 exit(1);
2568 }
2569 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
2570 if (!opts) {
2571 fprintf(stderr, "qemu: already have a debugcon device\n");
2572 exit(1);
2573 }
2574 qemu_opt_set(opts, "driver", "isa-debugcon");
2575 qemu_opt_set(opts, "chardev", "debugcon");
2576 return 0;
2577}
2578
Anthony Liguori6530a972010-01-22 09:18:06 -06002579static const QEMUOption *lookup_opt(int argc, char **argv,
2580 const char **poptarg, int *poptind)
2581{
2582 const QEMUOption *popt;
2583 int optind = *poptind;
2584 char *r = argv[optind];
2585 const char *optarg;
2586
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002587 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002588 optind++;
2589 /* Treat --foo the same as -foo. */
2590 if (r[1] == '-')
2591 r++;
2592 popt = qemu_options;
2593 for(;;) {
2594 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002595 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002596 exit(1);
2597 }
2598 if (!strcmp(popt->name, r + 1))
2599 break;
2600 popt++;
2601 }
2602 if (popt->flags & HAS_ARG) {
2603 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002604 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002605 exit(1);
2606 }
2607 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002608 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002609 } else {
2610 optarg = NULL;
2611 }
2612
2613 *poptarg = optarg;
2614 *poptind = optind;
2615
2616 return popt;
2617}
2618
malc902b3d52008-12-10 19:18:40 +00002619int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002620{
aliguori59030a82009-04-05 18:43:41 +00002621 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00002622 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00002623 int i;
j_mayer28c5af52007-11-11 01:50:45 +00002624 int snapshot, linux_boot, net_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002625 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002626 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002627 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002628 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00002629 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00002630 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00002631 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002632 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00002633 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002634 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002635 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002636 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002637 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00002638#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00002639 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00002640#endif
bellard26a5f132008-05-28 12:30:31 +00002641 int tb_size;
ths93815bc2007-03-19 15:58:31 +00002642 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002643 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00002644#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00002645 int fd = 0;
2646 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00002647 const char *chroot_dir = NULL;
2648 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00002649#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002650 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06002651 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00002652
Markus Armbruster65abca02010-02-24 14:37:14 +01002653 error_set_progname(argv[0]);
2654
Jan Kiszka68752042009-09-15 13:36:04 +02002655 init_clocks();
2656
malc902b3d52008-12-10 19:18:40 +00002657 qemu_cache_utils_init(envp);
2658
Blue Swirl72cf2d42009-09-12 07:36:22 +00002659 QLIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00002660#ifndef _WIN32
2661 {
2662 struct sigaction act;
2663 sigfillset(&act.sa_mask);
2664 act.sa_flags = 0;
2665 act.sa_handler = SIG_IGN;
2666 sigaction(SIGPIPE, &act, NULL);
2667 }
bellard3587d7e2006-06-26 20:03:44 +00002668#else
2669 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00002670 /* Note: cpu_interrupt() is currently not SMP safe, so we force
2671 QEMU to run on a single CPU */
2672 {
2673 HANDLE h;
2674 DWORD mask, smask;
2675 int i;
2676 h = GetCurrentProcess();
2677 if (GetProcessAffinityMask(h, &mask, &smask)) {
2678 for(i = 0; i < 32; i++) {
2679 if (mask & (1 << i))
2680 break;
2681 }
2682 if (i != 32) {
2683 mask = 1 << i;
2684 SetProcessAffinityMask(h, mask);
2685 }
2686 }
2687 }
bellard67b915a2004-03-31 23:37:16 +00002688#endif
bellardbe995c22006-06-25 16:25:21 +00002689
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002690 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002691 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002692 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00002693 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002694 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002695 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00002696 kernel_filename = NULL;
2697 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00002698 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00002699 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002700
aliguori268a3622009-04-21 22:30:27 +00002701 for (i = 0; i < MAX_NODES; i++) {
2702 node_mem[i] = 0;
2703 node_cpumask[i] = 0;
2704 }
2705
aliguori268a3622009-04-21 22:30:27 +00002706 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002707 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002708
bellard26a5f132008-05-28 12:30:31 +00002709 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00002710 autostart= 1;
2711
Anthony Liguori292444c2010-01-21 10:57:58 -06002712 /* first pass of option parsing */
2713 optind = 1;
2714 while (optind < argc) {
2715 if (argv[optind][0] != '-') {
2716 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002717 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002718 continue;
2719 } else {
2720 const QEMUOption *popt;
2721
2722 popt = lookup_opt(argc, argv, &optarg, &optind);
2723 switch (popt->index) {
2724 case QEMU_OPTION_nodefconfig:
2725 defconfig=0;
2726 break;
2727 }
2728 }
2729 }
2730
2731 if (defconfig) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002732 const char *fname;
Anthony Liguori292444c2010-01-21 10:57:58 -06002733 FILE *fp;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002734
2735 fname = CONFIG_QEMU_CONFDIR "/qemu.conf";
2736 fp = fopen(fname, "r");
Anthony Liguori292444c2010-01-21 10:57:58 -06002737 if (fp) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002738 if (qemu_config_parse(fp, fname) != 0) {
Anthony Liguori292444c2010-01-21 10:57:58 -06002739 exit(1);
2740 }
2741 fclose(fp);
2742 }
2743
Blue Swirlad960902010-03-29 19:23:52 +00002744 fname = arch_config_name;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002745 fp = fopen(fname, "r");
Anthony Liguori292444c2010-01-21 10:57:58 -06002746 if (fp) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002747 if (qemu_config_parse(fp, fname) != 0) {
Anthony Liguori292444c2010-01-21 10:57:58 -06002748 exit(1);
2749 }
2750 fclose(fp);
2751 }
2752 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00002753 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06002754
2755 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002756 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002757 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002758 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002759 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002760 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002761 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00002762 } else {
2763 const QEMUOption *popt;
2764
Anthony Liguori6530a972010-01-22 09:18:06 -06002765 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002766 if (!(popt->arch_mask & arch_type)) {
2767 printf("Option %s not supported for this target\n", popt->name);
2768 exit(1);
2769 }
bellardcd6f1162004-05-13 22:02:20 +00002770 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002771 case QEMU_OPTION_M:
2772 machine = find_machine(optarg);
2773 if (!machine) {
2774 QEMUMachine *m;
2775 printf("Supported machines are:\n");
2776 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01002777 if (m->alias)
2778 printf("%-10s %s (alias of %s)\n",
2779 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00002780 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00002781 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05002782 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00002783 }
ths15f82202007-06-29 23:26:08 +00002784 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00002785 }
2786 break;
j_mayer94fc95c2007-03-05 19:44:02 +00002787 case QEMU_OPTION_cpu:
2788 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00002789 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00002790/* XXX: implement xxx_cpu_list for targets that still miss it */
john cooperb5ec5ce2010-02-20 11:14:59 -06002791#if defined(cpu_list_id)
2792 cpu_list_id(stdout, &fprintf, optarg);
2793#elif defined(cpu_list)
2794 cpu_list(stdout, &fprintf); /* deprecated */
j_mayer94fc95c2007-03-05 19:44:02 +00002795#endif
ths15f82202007-06-29 23:26:08 +00002796 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00002797 } else {
2798 cpu_model = optarg;
2799 }
2800 break;
bellardcd6f1162004-05-13 22:02:20 +00002801 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00002802 initrd_filename = optarg;
2803 break;
bellardcd6f1162004-05-13 22:02:20 +00002804 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00002805 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002806 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00002807 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002808 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00002809 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00002810 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00002811 translation == BIOS_ATA_TRANSLATION_LBA ?
2812 ",trans=lba" :
2813 translation == BIOS_ATA_TRANSLATION_NONE ?
2814 ",trans=none" : "");
2815 break;
bellardcd6f1162004-05-13 22:02:20 +00002816 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002817 case QEMU_OPTION_hdc:
2818 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00002819 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00002820 break;
thse4bcb142007-12-02 04:51:10 +00002821 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00002822 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00002823 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002824 case QEMU_OPTION_set:
2825 if (qemu_set_option(optarg) != 0)
2826 exit(1);
2827 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002828 case QEMU_OPTION_global:
2829 if (qemu_global_option(optarg) != 0)
2830 exit(1);
2831 break;
balrog3e3d5812007-04-30 02:09:25 +00002832 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00002833 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00002834 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002835 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00002836 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00002837 break;
j_mayer86f55662007-04-24 06:52:59 +00002838 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00002839 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00002840 break;
bellardcd6f1162004-05-13 22:02:20 +00002841 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002842 snapshot = 1;
2843 break;
bellardcd6f1162004-05-13 22:02:20 +00002844 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002845 {
bellard330d0412003-07-26 18:11:40 +00002846 const char *p;
2847 p = optarg;
2848 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002849 if (cyls < 1 || cyls > 16383)
2850 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002851 if (*p != ',')
2852 goto chs_fail;
2853 p++;
2854 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002855 if (heads < 1 || heads > 16)
2856 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002857 if (*p != ',')
2858 goto chs_fail;
2859 p++;
2860 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002861 if (secs < 1 || secs > 63)
2862 goto chs_fail;
2863 if (*p == ',') {
2864 p++;
2865 if (!strcmp(p, "none"))
2866 translation = BIOS_ATA_TRANSLATION_NONE;
2867 else if (!strcmp(p, "lba"))
2868 translation = BIOS_ATA_TRANSLATION_LBA;
2869 else if (!strcmp(p, "auto"))
2870 translation = BIOS_ATA_TRANSLATION_AUTO;
2871 else
2872 goto chs_fail;
2873 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002874 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002875 fprintf(stderr, "qemu: invalid physical CHS format\n");
2876 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002877 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002878 if (hda_opts != NULL) {
2879 char num[16];
2880 snprintf(num, sizeof(num), "%d", cyls);
2881 qemu_opt_set(hda_opts, "cyls", num);
2882 snprintf(num, sizeof(num), "%d", heads);
2883 qemu_opt_set(hda_opts, "heads", num);
2884 snprintf(num, sizeof(num), "%d", secs);
2885 qemu_opt_set(hda_opts, "secs", num);
2886 if (translation == BIOS_ATA_TRANSLATION_LBA)
2887 qemu_opt_set(hda_opts, "trans", "lba");
2888 if (translation == BIOS_ATA_TRANSLATION_NONE)
2889 qemu_opt_set(hda_opts, "trans", "none");
2890 }
bellard330d0412003-07-26 18:11:40 +00002891 }
2892 break;
aliguori268a3622009-04-21 22:30:27 +00002893 case QEMU_OPTION_numa:
2894 if (nb_numa_nodes >= MAX_NODES) {
2895 fprintf(stderr, "qemu: too many NUMA nodes\n");
2896 exit(1);
2897 }
2898 numa_add(optarg);
2899 break;
bellardcd6f1162004-05-13 22:02:20 +00002900 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002901 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002902 break;
balrog4d3b6f62008-02-10 16:33:14 +00002903#ifdef CONFIG_CURSES
2904 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002905 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00002906 break;
2907#endif
balroga171fe32007-04-30 01:48:07 +00002908 case QEMU_OPTION_portrait:
2909 graphic_rotate = 1;
2910 break;
bellardcd6f1162004-05-13 22:02:20 +00002911 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002912 kernel_filename = optarg;
2913 break;
bellardcd6f1162004-05-13 22:02:20 +00002914 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002915 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002916 break;
bellardcd6f1162004-05-13 22:02:20 +00002917 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00002918 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00002919 break;
bellardcd6f1162004-05-13 22:02:20 +00002920 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002921 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002922 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002923 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002924 };
2925 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002926 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002927 int legacy = 0;
2928
2929 if (!strchr(optarg, '=')) {
2930 legacy = 1;
2931 pstrcpy(buf, sizeof(buf), optarg);
2932 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2933 fprintf(stderr,
2934 "qemu: unknown boot parameter '%s' in '%s'\n",
2935 buf, optarg);
2936 exit(1);
2937 }
2938
2939 if (legacy ||
2940 get_param_value(buf, sizeof(buf), "order", optarg)) {
2941 boot_devices_bitmap = parse_bootdevices(buf);
2942 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002943 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002944 if (!legacy) {
2945 if (get_param_value(buf, sizeof(buf),
2946 "once", optarg)) {
2947 boot_devices_bitmap |= parse_bootdevices(buf);
2948 standard_boot_devices = qemu_strdup(boot_devices);
2949 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2950 qemu_register_reset(restore_boot_devices,
2951 standard_boot_devices);
2952 }
Jan Kiszka95387492009-07-02 00:19:02 +02002953 if (get_param_value(buf, sizeof(buf),
2954 "menu", optarg)) {
2955 if (!strcmp(buf, "on")) {
2956 boot_menu = 1;
2957 } else if (!strcmp(buf, "off")) {
2958 boot_menu = 0;
2959 } else {
2960 fprintf(stderr,
2961 "qemu: invalid option value '%s'\n",
2962 buf);
2963 exit(1);
2964 }
2965 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002966 }
bellard36b486b2003-11-11 13:36:08 +00002967 }
2968 break;
bellardcd6f1162004-05-13 22:02:20 +00002969 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002970 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00002971 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00002972 break;
bellard52ca8d62006-06-14 16:03:05 +00002973 case QEMU_OPTION_no_fd_bootchk:
2974 fd_bootchk = 0;
2975 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002976 case QEMU_OPTION_netdev:
2977 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2978 exit(1);
2979 }
2980 break;
bellard7c9d8e02005-11-15 22:16:05 +00002981 case QEMU_OPTION_net:
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01002982 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002983 exit(1);
2984 }
bellard702c6512004-04-02 21:21:32 +00002985 break;
bellardc7f74642004-08-24 21:57:12 +00002986#ifdef CONFIG_SLIRP
2987 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002988 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002989 break;
ths47d5d012007-02-20 00:05:08 +00002990 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002991 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002992 break;
bellardc94c8d62004-09-13 21:37:34 +00002993#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00002994 case QEMU_OPTION_smb:
Markus Armbruster07527062009-10-06 12:16:57 +01002995 if (net_slirp_smb(optarg) < 0)
2996 exit(1);
bellard9d728e82004-09-05 23:09:03 +00002997 break;
bellardc94c8d62004-09-13 21:37:34 +00002998#endif
bellard9bf05442004-08-25 22:12:49 +00002999 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01003000 if (net_slirp_redir(optarg) < 0)
3001 exit(1);
bellard9bf05442004-08-25 22:12:49 +00003002 break;
bellardc7f74642004-08-24 21:57:12 +00003003#endif
balrogdc72ac12008-11-09 00:04:26 +00003004 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003005 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00003006 break;
bellard1d14ffa2005-10-30 18:58:22 +00003007 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00003008 if (!(audio_available())) {
3009 printf("Option %s not supported for this target\n", popt->name);
3010 exit(1);
3011 }
bellard1d14ffa2005-10-30 18:58:22 +00003012 AUD_help ();
3013 exit (0);
3014 break;
3015 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00003016 if (!(audio_available())) {
3017 printf("Option %s not supported for this target\n", popt->name);
3018 exit(1);
3019 }
bellard1d14ffa2005-10-30 18:58:22 +00003020 select_soundhw (optarg);
3021 break;
bellardcd6f1162004-05-13 22:02:20 +00003022 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00003023 help(0);
bellardcd6f1162004-05-13 22:02:20 +00003024 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00003025 case QEMU_OPTION_version:
3026 version();
3027 exit(0);
3028 break;
aurel3200f82b82008-04-27 21:12:55 +00003029 case QEMU_OPTION_m: {
3030 uint64_t value;
3031 char *ptr;
3032
3033 value = strtoul(optarg, &ptr, 10);
3034 switch (*ptr) {
3035 case 0: case 'M': case 'm':
3036 value <<= 20;
3037 break;
3038 case 'G': case 'g':
3039 value <<= 30;
3040 break;
3041 default:
3042 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00003043 exit(1);
3044 }
aurel3200f82b82008-04-27 21:12:55 +00003045
3046 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05003047 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00003048 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3049 exit(1);
3050 }
Anthony Liguoric227f092009-10-01 16:12:16 -05003051 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00003052 fprintf(stderr, "qemu: ram size too large\n");
3053 exit(1);
3054 }
3055 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00003056 break;
aurel3200f82b82008-04-27 21:12:55 +00003057 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03003058 case QEMU_OPTION_mempath:
3059 mem_path = optarg;
3060 break;
3061#ifdef MAP_POPULATE
3062 case QEMU_OPTION_mem_prealloc:
3063 mem_prealloc = 1;
3064 break;
3065#endif
bellardcd6f1162004-05-13 22:02:20 +00003066 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003067 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00003068 break;
bellardcd6f1162004-05-13 22:02:20 +00003069 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00003070 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00003071 break;
aliguori59030a82009-04-05 18:43:41 +00003072 case QEMU_OPTION_gdb:
3073 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00003074 break;
bellardcd6f1162004-05-13 22:02:20 +00003075 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01003076 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00003077 break;
j_mayer1192dad2007-10-05 13:08:35 +00003078 case QEMU_OPTION_bios:
3079 bios_name = optarg;
3080 break;
aurel321b530a62009-04-05 20:08:59 +00003081 case QEMU_OPTION_singlestep:
3082 singlestep = 1;
3083 break;
bellardcd6f1162004-05-13 22:02:20 +00003084 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00003085 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00003086 break;
bellard3d11d0e2004-12-12 16:56:30 +00003087 case QEMU_OPTION_k:
3088 keyboard_layout = optarg;
3089 break;
bellardee22c2f2004-06-03 12:49:50 +00003090 case QEMU_OPTION_localtime:
3091 rtc_utc = 0;
3092 break;
malc3893c122008-09-28 00:42:05 +00003093 case QEMU_OPTION_vga:
3094 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00003095 break;
bellarde9b137c2004-06-21 16:46:10 +00003096 case QEMU_OPTION_g:
3097 {
3098 const char *p;
3099 int w, h, depth;
3100 p = optarg;
3101 w = strtol(p, (char **)&p, 10);
3102 if (w <= 0) {
3103 graphic_error:
3104 fprintf(stderr, "qemu: invalid resolution or depth\n");
3105 exit(1);
3106 }
3107 if (*p != 'x')
3108 goto graphic_error;
3109 p++;
3110 h = strtol(p, (char **)&p, 10);
3111 if (h <= 0)
3112 goto graphic_error;
3113 if (*p == 'x') {
3114 p++;
3115 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00003116 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00003117 depth != 24 && depth != 32)
3118 goto graphic_error;
3119 } else if (*p == '\0') {
3120 depth = graphic_depth;
3121 } else {
3122 goto graphic_error;
3123 }
ths3b46e622007-09-17 08:09:54 +00003124
bellarde9b137c2004-06-21 16:46:10 +00003125 graphic_width = w;
3126 graphic_height = h;
3127 graphic_depth = depth;
3128 }
3129 break;
ths20d8a3e2007-02-18 17:04:49 +00003130 case QEMU_OPTION_echr:
3131 {
3132 char *r;
3133 term_escape_char = strtol(optarg, &r, 0);
3134 if (r == optarg)
3135 printf("Bad argument to echr\n");
3136 break;
3137 }
bellard82c643f2004-07-14 17:28:13 +00003138 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003139 monitor_parse(optarg, "readline");
3140 default_monitor = 0;
3141 break;
3142 case QEMU_OPTION_qmp:
3143 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003144 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00003145 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003146 case QEMU_OPTION_mon:
Markus Armbruster8212c642010-02-10 19:52:18 +01003147 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003148 if (!opts) {
3149 fprintf(stderr, "parse error: %s\n", optarg);
3150 exit(1);
3151 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003152 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003153 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003154 case QEMU_OPTION_chardev:
Markus Armbruster8212c642010-02-10 19:52:18 +01003155 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003156 if (!opts) {
3157 fprintf(stderr, "parse error: %s\n", optarg);
3158 exit(1);
3159 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003160 break;
bellard82c643f2004-07-14 17:28:13 +00003161 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003162 add_device_config(DEV_SERIAL, optarg);
3163 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003164 if (strncmp(optarg, "mon:", 4) == 0) {
3165 default_monitor = 0;
3166 }
bellard82c643f2004-07-14 17:28:13 +00003167 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003168 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02003169 if (watchdog) {
3170 fprintf(stderr,
3171 "qemu: only one watchdog option may be given\n");
3172 return 1;
3173 }
3174 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003175 break;
3176 case QEMU_OPTION_watchdog_action:
3177 if (select_watchdog_action(optarg) == -1) {
3178 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3179 exit(1);
3180 }
3181 break;
aliguori51ecf132009-01-15 20:06:40 +00003182 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003183 add_device_config(DEV_VIRTCON, optarg);
3184 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003185 if (strncmp(optarg, "mon:", 4) == 0) {
3186 default_monitor = 0;
3187 }
aliguori51ecf132009-01-15 20:06:40 +00003188 break;
bellard6508fe52005-01-15 12:02:56 +00003189 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003190 add_device_config(DEV_PARALLEL, optarg);
3191 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003192 if (strncmp(optarg, "mon:", 4) == 0) {
3193 default_monitor = 0;
3194 }
bellard6508fe52005-01-15 12:02:56 +00003195 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003196 case QEMU_OPTION_debugcon:
3197 add_device_config(DEV_DEBUGCON, optarg);
3198 break;
bellardd63d3072004-10-03 13:29:03 +00003199 case QEMU_OPTION_loadvm:
3200 loadvm = optarg;
3201 break;
3202 case QEMU_OPTION_full_screen:
3203 full_screen = 1;
3204 break;
ths667acca2006-12-11 02:08:05 +00003205#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00003206 case QEMU_OPTION_no_frame:
3207 no_frame = 1;
3208 break;
ths3780e192007-06-21 21:08:02 +00003209 case QEMU_OPTION_alt_grab:
3210 alt_grab = 1;
3211 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05003212 case QEMU_OPTION_ctrl_grab:
3213 ctrl_grab = 1;
3214 break;
ths667acca2006-12-11 02:08:05 +00003215 case QEMU_OPTION_no_quit:
3216 no_quit = 1;
3217 break;
aliguori7d957bd2009-01-15 22:14:11 +00003218 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003219 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00003220 break;
ths667acca2006-12-11 02:08:05 +00003221#endif
bellardf7cce892004-12-08 22:21:25 +00003222 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00003223 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00003224 break;
bellarda09db212005-04-30 16:10:35 +00003225 case QEMU_OPTION_win2k_hack:
3226 win2k_install_hack = 1;
3227 break;
aliguori73822ec2009-01-15 20:11:34 +00003228 case QEMU_OPTION_rtc_td_hack:
3229 rtc_td_hack = 1;
3230 break;
aliguori8a92ea22009-02-27 20:12:36 +00003231 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003232 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00003233 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00003234 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003235 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00003236 break;
aliguori7ba1e612008-11-05 16:04:33 +00003237 case QEMU_OPTION_enable_kvm:
Blue Swirlad960902010-03-29 19:23:52 +00003238 if (!(kvm_available())) {
3239 printf("Option %s not supported for this target\n", popt->name);
3240 exit(1);
3241 }
aliguori7ba1e612008-11-05 16:04:33 +00003242 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00003243 break;
bellardbb36d472005-11-05 14:22:28 +00003244 case QEMU_OPTION_usb:
3245 usb_enabled = 1;
3246 break;
bellarda594cfb2005-11-06 16:13:29 +00003247 case QEMU_OPTION_usbdevice:
3248 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003249 add_device_config(DEV_USB, optarg);
3250 break;
3251 case QEMU_OPTION_device:
Markus Armbruster8212c642010-02-10 19:52:18 +01003252 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003253 exit(1);
3254 }
bellarda594cfb2005-11-06 16:13:29 +00003255 break;
bellard6a00d602005-11-21 23:25:50 +00003256 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003257 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00003258 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00003259 fprintf(stderr, "Invalid number of CPUs\n");
3260 exit(1);
3261 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02003262 if (max_cpus < smp_cpus) {
3263 fprintf(stderr, "maxcpus must be equal to or greater than "
3264 "smp\n");
3265 exit(1);
3266 }
3267 if (max_cpus > 255) {
3268 fprintf(stderr, "Unsupported number of maxcpus\n");
3269 exit(1);
3270 }
bellard6a00d602005-11-21 23:25:50 +00003271 break;
bellard24236862006-04-30 21:28:36 +00003272 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003273 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00003274 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00003275 break;
bellard6515b202006-05-03 22:02:44 +00003276 case QEMU_OPTION_no_acpi:
3277 acpi_enabled = 0;
3278 break;
aliguori16b29ae2008-12-17 23:28:44 +00003279 case QEMU_OPTION_no_hpet:
3280 no_hpet = 1;
3281 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003282 case QEMU_OPTION_balloon:
3283 if (balloon_parse(optarg) < 0) {
3284 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3285 exit(1);
3286 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03003287 break;
bellardd1beab82006-10-02 19:44:22 +00003288 case QEMU_OPTION_no_reboot:
3289 no_reboot = 1;
3290 break;
aurel32b2f76162008-04-11 21:35:52 +00003291 case QEMU_OPTION_no_shutdown:
3292 no_shutdown = 1;
3293 break;
balrog9467cd42007-05-01 01:34:14 +00003294 case QEMU_OPTION_show_cursor:
3295 cursor_hide = 0;
3296 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003297 case QEMU_OPTION_uuid:
3298 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3299 fprintf(stderr, "Fail to parse UUID string."
3300 " Wrong format.\n");
3301 exit(1);
3302 }
3303 break;
blueswir15824d652009-03-28 06:44:27 +00003304#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00003305 case QEMU_OPTION_daemonize:
3306 daemonize = 1;
3307 break;
blueswir15824d652009-03-28 06:44:27 +00003308#endif
ths9ae02552007-01-05 17:39:04 +00003309 case QEMU_OPTION_option_rom:
3310 if (nb_option_roms >= MAX_OPTION_ROMS) {
3311 fprintf(stderr, "Too many option ROMs\n");
3312 exit(1);
3313 }
3314 option_rom[nb_option_roms] = optarg;
3315 nb_option_roms++;
3316 break;
pbrook8e716212007-01-20 17:12:09 +00003317 case QEMU_OPTION_semihosting:
3318 semihosting_enabled = 1;
3319 break;
thsc35734b2007-03-19 15:17:08 +00003320 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02003321 qemu_name = qemu_strdup(optarg);
3322 {
3323 char *p = strchr(qemu_name, ',');
3324 if (p != NULL) {
3325 *p++ = 0;
3326 if (strncmp(p, "process=", 8)) {
3327 fprintf(stderr, "Unknown subargument %s to -name", p);
3328 exit(1);
3329 }
3330 p += 8;
3331 set_proc_name(p);
3332 }
3333 }
thsc35734b2007-03-19 15:17:08 +00003334 break;
blueswir166508602007-05-01 14:16:52 +00003335 case QEMU_OPTION_prom_env:
3336 if (nb_prom_envs >= MAX_PROM_ENVS) {
3337 fprintf(stderr, "Too many prom variables\n");
3338 exit(1);
3339 }
3340 prom_envs[nb_prom_envs] = optarg;
3341 nb_prom_envs++;
3342 break;
balrog2b8f2d42007-07-27 22:08:46 +00003343 case QEMU_OPTION_old_param:
3344 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003345 break;
thsf3dcfad2007-08-24 01:26:02 +00003346 case QEMU_OPTION_clock:
3347 configure_alarms(optarg);
3348 break;
bellard7e0af5d02007-11-07 16:24:33 +00003349 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003350 configure_rtc_date_offset(optarg, 1);
3351 break;
3352 case QEMU_OPTION_rtc:
Markus Armbruster8212c642010-02-10 19:52:18 +01003353 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003354 if (!opts) {
3355 fprintf(stderr, "parse error: %s\n", optarg);
3356 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003357 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003358 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003359 break;
bellard26a5f132008-05-28 12:30:31 +00003360 case QEMU_OPTION_tb_size:
3361 tb_size = strtol(optarg, NULL, 0);
3362 if (tb_size < 0)
3363 tb_size = 0;
3364 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003365 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003366 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003367 break;
aliguori5bb79102008-10-13 03:12:02 +00003368 case QEMU_OPTION_incoming:
3369 incoming = optarg;
3370 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003371 case QEMU_OPTION_nodefaults:
3372 default_serial = 0;
3373 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003374 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003375 default_monitor = 0;
3376 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003377 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003378 default_floppy = 0;
3379 default_cdrom = 0;
3380 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003381 break;
blueswir15824d652009-03-28 06:44:27 +00003382#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00003383 case QEMU_OPTION_chroot:
3384 chroot_dir = optarg;
3385 break;
3386 case QEMU_OPTION_runas:
3387 run_as = optarg;
3388 break;
blueswir15824d652009-03-28 06:44:27 +00003389#endif
aliguorie37630c2009-04-22 15:19:10 +00003390 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003391 if (!(xen_available())) {
3392 printf("Option %s not supported for this target\n", popt->name);
3393 exit(1);
3394 }
aliguorie37630c2009-04-22 15:19:10 +00003395 xen_domid = atoi(optarg);
3396 break;
3397 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003398 if (!(xen_available())) {
3399 printf("Option %s not supported for this target\n", popt->name);
3400 exit(1);
3401 }
aliguorie37630c2009-04-22 15:19:10 +00003402 xen_mode = XEN_CREATE;
3403 break;
3404 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003405 if (!(xen_available())) {
3406 printf("Option %s not supported for this target\n", popt->name);
3407 exit(1);
3408 }
aliguorie37630c2009-04-22 15:19:10 +00003409 xen_mode = XEN_ATTACH;
3410 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003411 case QEMU_OPTION_readconfig:
3412 {
3413 FILE *fp;
3414 fp = fopen(optarg, "r");
3415 if (fp == NULL) {
3416 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3417 exit(1);
3418 }
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01003419 if (qemu_config_parse(fp, optarg) != 0) {
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003420 exit(1);
3421 }
3422 fclose(fp);
3423 break;
3424 }
3425 case QEMU_OPTION_writeconfig:
3426 {
3427 FILE *fp;
3428 if (strcmp(optarg, "-") == 0) {
3429 fp = stdout;
3430 } else {
3431 fp = fopen(optarg, "w");
3432 if (fp == NULL) {
3433 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3434 exit(1);
3435 }
3436 }
3437 qemu_config_write(fp);
3438 fclose(fp);
3439 break;
3440 }
bellardcd6f1162004-05-13 22:02:20 +00003441 }
bellard0824d6f2003-06-24 13:42:40 +00003442 }
3443 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003444 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003445
Paul Brook5cea8592009-05-30 00:52:44 +01003446 /* If no data_dir is specified then try to find it relative to the
3447 executable path. */
3448 if (!data_dir) {
3449 data_dir = find_datadir(argv[0]);
3450 }
3451 /* If all else fails use the install patch specified when building. */
3452 if (!data_dir) {
3453 data_dir = CONFIG_QEMU_SHAREDIR;
3454 }
3455
Jes Sorensen6be68d72009-07-23 17:03:42 +02003456 /*
3457 * Default to max_cpus = smp_cpus, in case the user doesn't
3458 * specify a max_cpus value.
3459 */
3460 if (!max_cpus)
3461 max_cpus = smp_cpus;
3462
balrog3d878ca2008-10-28 10:59:59 +00003463 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003464 if (smp_cpus > machine->max_cpus) {
3465 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3466 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3467 machine->max_cpus);
3468 exit(1);
3469 }
3470
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003471 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +01003472 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003473
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003474 if (machine->no_serial) {
3475 default_serial = 0;
3476 }
3477 if (machine->no_parallel) {
3478 default_parallel = 0;
3479 }
3480 if (!machine->use_virtcon) {
3481 default_virtcon = 0;
3482 }
3483 if (machine->no_vga) {
3484 default_vga = 0;
3485 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003486 if (machine->no_floppy) {
3487 default_floppy = 0;
3488 }
3489 if (machine->no_cdrom) {
3490 default_cdrom = 0;
3491 }
3492 if (machine->no_sdcard) {
3493 default_sdcard = 0;
3494 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003495
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003496 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003497 if (default_parallel)
3498 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003499 if (default_serial && default_monitor) {
3500 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003501 } else if (default_virtcon && default_monitor) {
3502 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003503 } else {
3504 if (default_serial)
3505 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003506 if (default_virtcon)
3507 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003508 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003509 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003510 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003511 } else {
3512 if (default_serial)
3513 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003514 if (default_parallel)
3515 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003516 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003517 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003518 if (default_virtcon)
3519 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003520 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01003521 if (default_vga)
3522 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00003523
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003524 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
3525 exit(1);
3526
ths71e3ceb2006-12-22 02:11:31 +00003527#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00003528 if (daemonize) {
3529 pid_t pid;
3530
3531 if (pipe(fds) == -1)
3532 exit(1);
3533
3534 pid = fork();
3535 if (pid > 0) {
3536 uint8_t status;
3537 ssize_t len;
3538
3539 close(fds[1]);
3540
3541 again:
ths93815bc2007-03-19 15:58:31 +00003542 len = read(fds[0], &status, 1);
3543 if (len == -1 && (errno == EINTR))
3544 goto again;
3545
3546 if (len != 1)
3547 exit(1);
3548 else if (status == 1) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05003549 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00003550 exit(1);
3551 } else
3552 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00003553 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00003554 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00003555
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003556 close(fds[0]);
3557 qemu_set_cloexec(fds[1]);
3558
ths71e3ceb2006-12-22 02:11:31 +00003559 setsid();
3560
3561 pid = fork();
3562 if (pid > 0)
3563 exit(0);
3564 else if (pid < 0)
3565 exit(1);
3566
3567 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00003568
3569 signal(SIGTSTP, SIG_IGN);
3570 signal(SIGTTOU, SIG_IGN);
3571 signal(SIGTTIN, SIG_IGN);
3572 }
Juha Riihimäki099fe232009-12-03 15:56:03 +02003573#endif
ths71e3ceb2006-12-22 02:11:31 +00003574
thsaa26bb22007-03-25 21:33:06 +00003575 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Juha Riihimäki099fe232009-12-03 15:56:03 +02003576#ifndef _WIN32
ths93815bc2007-03-19 15:58:31 +00003577 if (daemonize) {
3578 uint8_t status = 1;
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01003579 if (write(fds[1], &status, 1) != 1) {
3580 perror("daemonize. Writing to pipe\n");
3581 }
ths93815bc2007-03-19 15:58:31 +00003582 } else
Juha Riihimäki099fe232009-12-03 15:56:03 +02003583#endif
Justin M. Forbes850810d2009-10-01 09:42:56 -05003584 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00003585 exit(1);
3586 }
3587
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003588 if (kvm_enabled()) {
3589 int ret;
3590
3591 ret = kvm_init(smp_cpus);
3592 if (ret < 0) {
3593 fprintf(stderr, "failed to initialize KVM\n");
3594 exit(1);
3595 }
3596 }
3597
aliguori3fcf7b62009-04-24 18:03:25 +00003598 if (qemu_init_main_loop()) {
3599 fprintf(stderr, "qemu_init_main_loop failed\n");
3600 exit(1);
3601 }
bellarda20dd502003-09-30 21:07:02 +00003602 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003603
thsf8d39c02008-07-03 10:01:15 +00003604 if (!linux_boot && *kernel_cmdline != '\0') {
3605 fprintf(stderr, "-append only allowed with -kernel option\n");
3606 exit(1);
3607 }
3608
3609 if (!linux_boot && initrd_filename != NULL) {
3610 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3611 exit(1);
3612 }
3613
Filip Navarabf65f532009-07-27 10:02:04 -05003614#ifndef _WIN32
3615 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00003616 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05003617#endif
ths3b46e622007-09-17 08:09:54 +00003618
aliguori7183b4b2008-11-05 20:40:18 +00003619 if (init_timer_alarm() < 0) {
3620 fprintf(stderr, "could not initialize alarm timer\n");
3621 exit(1);
3622 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003623 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003624
bellardfd1dff42006-02-01 21:29:26 +00003625#ifdef _WIN32
3626 socket_init();
3627#endif
3628
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003629 if (net_init_clients() < 0) {
3630 exit(1);
bellard702c6512004-04-02 21:21:32 +00003631 }
bellardf1510b22003-06-25 00:07:40 +00003632
Glauber Costa406c8df2009-06-17 09:05:30 -04003633 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3634 net_set_boot_mask(net_boot);
3635
balrogdc72ac12008-11-09 00:04:26 +00003636 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003637 if (foreach_device_config(DEV_BT, bt_parse))
3638 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003639
bellard0824d6f2003-06-24 13:42:40 +00003640 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00003641 if (ram_size == 0)
3642 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00003643
bellard26a5f132008-05-28 12:30:31 +00003644 /* init the dynamic translator */
3645 cpu_exec_init_all(tb_size * 1024 * 1024);
3646
Markus Armbrustereb852012009-10-27 18:41:44 +01003647 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003648
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003649 blk_mig_init();
3650
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003651 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003652 /* we always create the cdrom drive, even if no disk is there */
3653 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003654 }
thse4bcb142007-12-02 04:51:10 +00003655
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003656 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003657 /* we always create at least one floppy */
3658 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003659 }
bellardc4b1fcc2004-03-14 21:44:30 +00003660
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003661 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003662 /* we always create one sd slot, even if no card is in it */
3663 drive_add(NULL, SD_ALIAS);
3664 }
balrog9d413d12007-12-04 00:10:34 +00003665
ths96d30e42007-01-07 20:42:14 +00003666 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003667 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02003668 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
3669 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003670 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00003671
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003672 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
3673 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003674
aliguori268a3622009-04-21 22:30:27 +00003675 if (nb_numa_nodes > 0) {
3676 int i;
3677
3678 if (nb_numa_nodes > smp_cpus) {
3679 nb_numa_nodes = smp_cpus;
3680 }
3681
3682 /* If no memory size if given for any node, assume the default case
3683 * and distribute the available memory equally across all nodes
3684 */
3685 for (i = 0; i < nb_numa_nodes; i++) {
3686 if (node_mem[i] != 0)
3687 break;
3688 }
3689 if (i == nb_numa_nodes) {
3690 uint64_t usedmem = 0;
3691
3692 /* On Linux, the each node's border has to be 8MB aligned,
3693 * the final node gets the rest.
3694 */
3695 for (i = 0; i < nb_numa_nodes - 1; i++) {
3696 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3697 usedmem += node_mem[i];
3698 }
3699 node_mem[i] = ram_size - usedmem;
3700 }
3701
3702 for (i = 0; i < nb_numa_nodes; i++) {
3703 if (node_cpumask[i] != 0)
3704 break;
3705 }
3706 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3707 * must cope with this anyway, because there are BIOSes out there in
3708 * real machines which also use this scheme.
3709 */
3710 if (i == nb_numa_nodes) {
3711 for (i = 0; i < smp_cpus; i++) {
3712 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3713 }
3714 }
3715 }
3716
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003717 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3718 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003719 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3720 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003721 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3722 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003723 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3724 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003725
Paul Brookaae94602009-05-14 22:35:06 +01003726 module_call_init(MODULE_INIT_DEVICE);
3727
Markus Armbrusterff952ba2010-01-29 19:48:57 +01003728 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
3729 exit(0);
3730
Markus Armbruster09aaa162009-08-21 10:31:34 +02003731 if (watchdog) {
3732 i = select_watchdog(watchdog);
3733 if (i > 0)
3734 exit (i == 1 ? 1 : 0);
3735 }
3736
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003737 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003738 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003739 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003740 qemu_add_globals();
3741
Paul Brookfbe1b592009-05-13 17:56:25 +01003742 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00003743 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3744
Jan Kiszkaea375f92010-03-01 19:10:30 +01003745 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003746
Juan Quintela67b3b712009-08-28 19:25:15 +02003747#ifndef _WIN32
3748 /* must be after terminal init, SDL library changes signal handlers */
3749 sighandler_setup();
3750#endif
3751
Blue Swirl87d0a282010-03-27 18:24:45 +00003752 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003753
aliguori6f338c32009-02-11 15:21:54 +00003754 current_machine = machine;
3755
aliguori3023f332009-01-16 19:04:14 +00003756 /* init USB devices */
3757 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01003758 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3759 exit(1);
aliguori3023f332009-01-16 19:04:14 +00003760 }
3761
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003762 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003763 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003764 exit(1);
3765
Markus Armbruster668680f2010-02-11 14:44:58 +01003766 net_check_clients();
3767
aliguori3023f332009-01-16 19:04:14 +00003768 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003769 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003770
3771 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003772#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003773 display_type = DT_SDL;
3774#else
3775 display_type = DT_VNC;
3776 vnc_display = "localhost:0,to=99";
3777 show_vnc_port = 1;
3778#endif
3779 }
3780
3781
3782 switch (display_type) {
3783 case DT_NOGRAPHIC:
3784 break;
3785#if defined(CONFIG_CURSES)
3786 case DT_CURSES:
3787 curses_display_init(ds, full_screen);
3788 break;
3789#endif
bellard5b0753e2005-03-01 21:37:28 +00003790#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003791 case DT_SDL:
3792 sdl_display_init(ds, full_screen, no_frame);
3793 break;
bellard5b0753e2005-03-01 21:37:28 +00003794#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003795 case DT_SDL:
3796 cocoa_display_init(ds, full_screen);
3797 break;
bellard313aa562003-08-10 21:52:11 +00003798#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003799 case DT_VNC:
3800 vnc_display_init(ds);
3801 if (vnc_display_open(ds, vnc_display) < 0)
3802 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003803
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003804 if (show_vnc_port) {
3805 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003806 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003807 break;
3808 default:
3809 break;
bellard313aa562003-08-10 21:52:11 +00003810 }
aliguori7d957bd2009-01-15 22:14:11 +00003811 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00003812
aliguori3023f332009-01-16 19:04:14 +00003813 dcl = ds->listeners;
3814 while (dcl != NULL) {
3815 if (dcl->dpy_refresh != NULL) {
3816 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3817 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00003818 }
aliguori3023f332009-01-16 19:04:14 +00003819 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00003820 }
aliguori3023f332009-01-16 19:04:14 +00003821
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003822 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00003823 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3824 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3825 }
3826
Paolo Bonzinib473df62010-02-11 00:29:55 +01003827 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003828
Gerd Hoffmann88589342009-12-08 13:11:50 +01003829 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0)
3830 exit(1);
bellard82c643f2004-07-14 17:28:13 +00003831
aliguori59030a82009-04-05 18:43:41 +00003832 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3833 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3834 gdbstub_dev);
3835 exit(1);
balrog45669e02007-07-02 13:20:17 +00003836 }
balrog45669e02007-07-02 13:20:17 +00003837
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003838 qdev_machine_creation_done();
3839
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003840 if (rom_load_all() != 0) {
3841 fprintf(stderr, "rom loading failed\n");
3842 exit(1);
3843 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003844
Juan Quintela504c2942009-11-12 00:39:13 +01003845 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02003846 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003847 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003848 autostart = 0;
3849 }
3850 }
bellardd63d3072004-10-03 13:29:03 +00003851
Glauber Costa2bb8c102009-07-24 16:20:23 -04003852 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00003853 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03003854 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003855 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003856 }
thsffd843b2006-12-21 19:46:43 +00003857
blueswir1b9e82a52009-04-05 18:03:31 +00003858#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00003859 if (daemonize) {
3860 uint8_t status = 0;
3861 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00003862
3863 again1:
3864 len = write(fds[1], &status, 1);
3865 if (len == -1 && (errno == EINTR))
3866 goto again1;
3867
3868 if (len != 1)
3869 exit(1);
3870
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01003871 if (chdir("/")) {
3872 perror("not able to chdir to /");
3873 exit(1);
3874 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003875 TFR(fd = qemu_open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00003876 if (fd == -1)
3877 exit(1);
aliguori08585322009-02-27 22:09:45 +00003878 }
ths71e3ceb2006-12-22 02:11:31 +00003879
aliguori08585322009-02-27 22:09:45 +00003880 if (run_as) {
3881 pwd = getpwnam(run_as);
3882 if (!pwd) {
3883 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
3884 exit(1);
3885 }
3886 }
ths71e3ceb2006-12-22 02:11:31 +00003887
aliguori08585322009-02-27 22:09:45 +00003888 if (chroot_dir) {
3889 if (chroot(chroot_dir) < 0) {
3890 fprintf(stderr, "chroot failed\n");
3891 exit(1);
3892 }
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01003893 if (chdir("/")) {
3894 perror("not able to chdir to /");
3895 exit(1);
3896 }
aliguori08585322009-02-27 22:09:45 +00003897 }
3898
3899 if (run_as) {
3900 if (setgid(pwd->pw_gid) < 0) {
3901 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
3902 exit(1);
3903 }
3904 if (setuid(pwd->pw_uid) < 0) {
3905 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
3906 exit(1);
3907 }
3908 if (setuid(0) != -1) {
3909 fprintf(stderr, "Dropping privileges failed\n");
3910 exit(1);
3911 }
3912 }
aliguori08585322009-02-27 22:09:45 +00003913
3914 if (daemonize) {
3915 dup2(fd, 0);
3916 dup2(fd, 1);
3917 dup2(fd, 2);
3918
3919 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00003920 }
blueswir1b9e82a52009-04-05 18:03:31 +00003921#endif
ths71e3ceb2006-12-22 02:11:31 +00003922
bellard8a7ddc32004-03-31 19:00:16 +00003923 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003924 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003925 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00003926
bellard0824d6f2003-06-24 13:42:40 +00003927 return 0;
3928}