blob: b7417008ad35d4fa45b5c46025f0327502942fbb [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
blueswir1179a2c12009-03-08 08:23:32 +000032/* Needed early for HOST_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +000036#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000038#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000039#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000042#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000043#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000044#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <net/if.h>
46#if defined(__NetBSD__)
47#include <net/if_tap.h>
48#endif
49#ifdef __linux__
50#include <linux/if_tun.h>
51#endif
52#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000053#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000054#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000055#include <sys/select.h>
blueswir1179a2c12009-03-08 08:23:32 +000056#ifdef HOST_BSD
bellard7d3505c2004-05-12 19:32:15 +000057#include <sys/stat.h>
blueswir1c5e97232009-03-07 20:06:23 +000058#if defined(__FreeBSD__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000059#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000060#else
61#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000062#endif
ths5c40d2b2007-06-23 16:03:36 +000063#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
64#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000065#else
blueswir1223f0d72008-09-30 18:12:18 +000066#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000067#include <pty.h>
68#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000069#include <linux/rtc.h>
thsbd494f42007-09-16 20:03:23 +000070
71/* For the benefit of older linux systems which don't supply it,
72 we use a local copy of hpet.h. */
73/* #include <linux/hpet.h> */
74#include "hpet.h"
75
bellarde57a8c02005-11-10 23:58:52 +000076#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000077#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000078#endif
79#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000080#include <sys/stat.h>
81#include <sys/ethernet.h>
82#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000083#include <netinet/arp.h>
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#include <netinet/ip_icmp.h> // must come after ip.h
88#include <netinet/udp.h>
89#include <netinet/tcp.h>
90#include <net/if.h>
91#include <syslog.h>
92#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000093#endif
bellard7d3505c2004-05-12 19:32:15 +000094#endif
bellardec530c82006-04-25 22:36:06 +000095#endif
bellard67b915a2004-03-31 23:37:16 +000096
blueswir19892fbf2008-08-24 10:34:20 +000097#if defined(__OpenBSD__)
98#include <util.h>
99#endif
100
ths8a16d272008-07-19 09:56:24 +0000101#if defined(CONFIG_VDE)
102#include <libvdeplug.h>
103#endif
104
bellard67b915a2004-03-31 23:37:16 +0000105#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000106#include <windows.h>
bellard7d3505c2004-05-12 19:32:15 +0000107#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000108#include <sys/timeb.h>
ths4fddf622007-12-17 04:42:29 +0000109#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000110#define getopt_long_only getopt_long
111#define memalign(align, size) malloc(size)
112#endif
113
bellard73332e52004-04-04 20:22:28 +0000114#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +0000115#ifdef __APPLE__
bellard83fb7ad2004-07-05 21:25:26 +0000116#include <SDL/SDL.h>
malc880fec52009-02-15 20:18:41 +0000117int qemu_main(int argc, char **argv, char **envp);
118int main(int argc, char **argv)
119{
120 qemu_main(argc, argv, NULL);
121}
122#undef main
123#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000124#endif
bellard73332e52004-04-04 20:22:28 +0000125#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000126
bellard5b0753e2005-03-01 21:37:28 +0000127#ifdef CONFIG_COCOA
128#undef main
129#define main qemu_main
130#endif /* CONFIG_COCOA */
131
blueswir1511d2b12009-03-07 15:32:56 +0000132#include "hw/hw.h"
133#include "hw/boards.h"
134#include "hw/usb.h"
135#include "hw/pcmcia.h"
136#include "hw/pc.h"
137#include "hw/audiodev.h"
138#include "hw/isa.h"
139#include "hw/baum.h"
140#include "hw/bt.h"
141#include "net.h"
142#include "monitor.h"
143#include "console.h"
144#include "sysemu.h"
145#include "gdbstub.h"
146#include "qemu-timer.h"
147#include "qemu-char.h"
148#include "cache-utils.h"
149#include "block.h"
150#include "audio/audio.h"
151#include "migration.h"
152#include "kvm.h"
153#include "balloon.h"
154
bellard0824d6f2003-06-24 13:42:40 +0000155#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000156
bellard8a7ddc32004-03-31 19:00:16 +0000157#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000158
blueswir1511d2b12009-03-07 15:32:56 +0000159#include "qemu_socket.h"
160
161#if defined(CONFIG_SLIRP)
162#include "libslirp.h"
163#endif
164
bellard0824d6f2003-06-24 13:42:40 +0000165//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000166//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000167//#define DEBUG_NET
168//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000169
aliguorid12d51d2009-01-15 21:48:06 +0000170
171#ifdef DEBUG_IOPORT
aliguori93fcfe32009-01-15 22:34:14 +0000172# define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
aliguorid12d51d2009-01-15 21:48:06 +0000173#else
174# define LOG_IOPORT(...) do { } while (0)
175#endif
176
bellard1bfe8562004-07-08 21:17:50 +0000177#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000178
pbrook0d92ed32006-05-21 16:30:15 +0000179/* Max number of USB devices that can be specified on the commandline. */
180#define MAX_USB_CMDLINE 8
181
balrogdc72ac12008-11-09 00:04:26 +0000182/* Max number of bluetooth switches on the commandline. */
183#define MAX_BT_CMDLINE 10
184
bellard7dea1da2003-11-16 15:59:30 +0000185/* XXX: use a two level table to limit memory usage */
186#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000187
bellard80cabfa2004-03-14 12:20:30 +0000188const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000189const char *bios_name = NULL;
blueswir1dbed7e42008-10-01 19:38:09 +0000190static void *ioport_opaque[MAX_IOPORTS];
191static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
192static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
thse4bcb142007-12-02 04:51:10 +0000193/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000194 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000195DriveInfo drives_table[MAX_DRIVES+1];
196int nb_drives;
blueswir1dbed7e42008-10-01 19:38:09 +0000197static int vga_ram_size;
malccb5a7aa2008-09-28 00:42:12 +0000198enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000199static DisplayState *display_state;
bellarda20dd502003-09-30 21:07:02 +0000200int nographic;
blueswir1dbed7e42008-10-01 19:38:09 +0000201static int curses;
aliguori7d957bd2009-01-15 22:14:11 +0000202static int sdl;
bellard3d11d0e2004-12-12 16:56:30 +0000203const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000204int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000205ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000206int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000207NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000208int vm_running;
aliguoric0f4ce72009-03-05 23:01:01 +0000209static int autostart;
balrogf6503052008-02-17 11:42:19 +0000210static int rtc_utc = 1;
211static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000212int cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +0000213int std_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000214int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000215#ifdef TARGET_SPARC
216int graphic_width = 1024;
217int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000218int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000219#else
bellard1bfe8562004-07-08 21:17:50 +0000220int graphic_width = 800;
221int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000222int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000223#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000224static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000225#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000226static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000227#endif
ths667acca2006-12-11 02:08:05 +0000228int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000229CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000230CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000231CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000232#ifdef TARGET_I386
233int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000234int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000235#endif
bellardbb36d472005-11-05 14:22:28 +0000236int usb_enabled = 0;
bellard6a00d602005-11-21 23:25:50 +0000237int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000238const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000239int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000240int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000241int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000242int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000243int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000244int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000245int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000246int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000247const char *option_rom[MAX_OPTION_ROMS];
248int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000249int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000250#ifdef TARGET_ARM
251int old_param = 0;
252#endif
thsc35734b2007-03-19 15:17:08 +0000253const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000254int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000255#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000256unsigned int nb_prom_envs = 0;
257const char *prom_envs[MAX_PROM_ENVS];
258#endif
aliguoriec691c82009-02-11 15:20:37 +0000259int nb_drives_opt;
260struct drive_opt drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000261
balrogee5605e2007-12-03 03:01:40 +0000262static CPUState *cur_cpu;
263static CPUState *next_cpu;
balrog76ea08f2007-12-16 11:48:54 +0000264static int event_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000265/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000266static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000267/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000268#define MAX_ICOUNT_SHIFT 10
269/* Compensate for varying guest execution speed. */
270static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000271static QEMUTimer *icount_rt_timer;
272static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000273static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000274
blueswir18fcb1b92008-09-18 18:29:08 +0000275uint8_t qemu_uuid[16];
276
bellard0824d6f2003-06-24 13:42:40 +0000277/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000278/* x86 ISA bus support */
279
280target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000281PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000282
aliguori477e3ed2008-07-23 15:19:59 +0000283static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
284static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
285
286static uint32_t ioport_read(int index, uint32_t address)
287{
288 static IOPortReadFunc *default_func[3] = {
289 default_ioport_readb,
290 default_ioport_readw,
291 default_ioport_readl
292 };
293 IOPortReadFunc *func = ioport_read_table[index][address];
294 if (!func)
295 func = default_func[index];
296 return func(ioport_opaque[address], address);
297}
298
299static void ioport_write(int index, uint32_t address, uint32_t data)
300{
301 static IOPortWriteFunc *default_func[3] = {
302 default_ioport_writeb,
303 default_ioport_writew,
304 default_ioport_writel
305 };
306 IOPortWriteFunc *func = ioport_write_table[index][address];
307 if (!func)
308 func = default_func[index];
309 func(ioport_opaque[address], address, data);
310}
311
pbrook9596ebb2007-11-18 01:44:38 +0000312static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000313{
314#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000315 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000316#endif
bellardfc01f7e2003-06-30 10:03:06 +0000317 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000318}
319
pbrook9596ebb2007-11-18 01:44:38 +0000320static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000321{
322#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000323 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000324#endif
325}
326
327/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000328static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000329{
330 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000331 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000332 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000333 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000334 return data;
335}
336
pbrook9596ebb2007-11-18 01:44:38 +0000337static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000338{
aliguori477e3ed2008-07-23 15:19:59 +0000339 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000340 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000341 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000342}
343
pbrook9596ebb2007-11-18 01:44:38 +0000344static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000345{
346#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000347 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000348#endif
349 return 0xffffffff;
350}
351
pbrook9596ebb2007-11-18 01:44:38 +0000352static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000353{
354#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000355 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000356#endif
bellard0824d6f2003-06-24 13:42:40 +0000357}
358
bellardfc01f7e2003-06-30 10:03:06 +0000359/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000360int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000361 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000362{
bellardfc01f7e2003-06-30 10:03:06 +0000363 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000364
bellardc4b1fcc2004-03-14 21:44:30 +0000365 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000366 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000367 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000368 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000369 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000370 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000371 } else {
balrog88fdf562008-04-26 21:11:22 +0000372 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000373 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000374 }
375 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000376 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000377 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000378 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000379 ioport_opaque[i] = opaque;
380 }
bellard0824d6f2003-06-24 13:42:40 +0000381 return 0;
382}
383
bellardfc01f7e2003-06-30 10:03:06 +0000384/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000385int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000386 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000387{
bellardfc01f7e2003-06-30 10:03:06 +0000388 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000389
bellardc4b1fcc2004-03-14 21:44:30 +0000390 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000391 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000392 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000393 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000394 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000395 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000396 } else {
balrog88fdf562008-04-26 21:11:22 +0000397 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000398 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000399 }
400 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000401 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000402 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
403 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000404 ioport_opaque[i] = opaque;
405 }
bellardf1510b22003-06-25 00:07:40 +0000406 return 0;
407}
408
bellard69b91032004-05-18 23:05:28 +0000409void isa_unassign_ioport(int start, int length)
410{
411 int i;
412
413 for(i = start; i < start + length; i++) {
414 ioport_read_table[0][i] = default_ioport_readb;
415 ioport_read_table[1][i] = default_ioport_readw;
416 ioport_read_table[2][i] = default_ioport_readl;
417
418 ioport_write_table[0][i] = default_ioport_writeb;
419 ioport_write_table[1][i] = default_ioport_writew;
420 ioport_write_table[2][i] = default_ioport_writel;
aliguoria7607f72009-02-11 15:21:29 +0000421
422 ioport_opaque[i] = NULL;
bellard69b91032004-05-18 23:05:28 +0000423 }
424}
425
bellard20f32282005-01-03 23:36:21 +0000426/***********************************************************/
427
bellardc45886d2004-01-05 00:02:06 +0000428void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000429{
aliguorid12d51d2009-01-15 21:48:06 +0000430 LOG_IOPORT("outb: %04x %02x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000431 ioport_write(0, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000432#ifdef USE_KQEMU
433 if (env)
434 env->last_io_time = cpu_get_time_fast();
435#endif
bellard0824d6f2003-06-24 13:42:40 +0000436}
437
bellardc45886d2004-01-05 00:02:06 +0000438void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000439{
aliguorid12d51d2009-01-15 21:48:06 +0000440 LOG_IOPORT("outw: %04x %04x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000441 ioport_write(1, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000442#ifdef USE_KQEMU
443 if (env)
444 env->last_io_time = cpu_get_time_fast();
445#endif
bellard0824d6f2003-06-24 13:42:40 +0000446}
447
bellardc45886d2004-01-05 00:02:06 +0000448void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000449{
aliguorid12d51d2009-01-15 21:48:06 +0000450 LOG_IOPORT("outl: %04x %08x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000451 ioport_write(2, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000452#ifdef USE_KQEMU
453 if (env)
454 env->last_io_time = cpu_get_time_fast();
455#endif
bellard0824d6f2003-06-24 13:42:40 +0000456}
457
bellardc45886d2004-01-05 00:02:06 +0000458int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000459{
bellardfd872592004-05-12 19:11:15 +0000460 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000461 val = ioport_read(0, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000462 LOG_IOPORT("inb : %04x %02x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000463#ifdef USE_KQEMU
464 if (env)
465 env->last_io_time = cpu_get_time_fast();
466#endif
bellardfd872592004-05-12 19:11:15 +0000467 return val;
bellard0824d6f2003-06-24 13:42:40 +0000468}
469
bellardc45886d2004-01-05 00:02:06 +0000470int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000471{
bellardfd872592004-05-12 19:11:15 +0000472 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000473 val = ioport_read(1, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000474 LOG_IOPORT("inw : %04x %04x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000475#ifdef USE_KQEMU
476 if (env)
477 env->last_io_time = cpu_get_time_fast();
478#endif
bellardfd872592004-05-12 19:11:15 +0000479 return val;
bellard0824d6f2003-06-24 13:42:40 +0000480}
481
bellardc45886d2004-01-05 00:02:06 +0000482int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000483{
bellardfd872592004-05-12 19:11:15 +0000484 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000485 val = ioport_read(2, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000486 LOG_IOPORT("inl : %04x %08x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000487#ifdef USE_KQEMU
488 if (env)
489 env->last_io_time = cpu_get_time_fast();
490#endif
bellardfd872592004-05-12 19:11:15 +0000491 return val;
bellard0824d6f2003-06-24 13:42:40 +0000492}
493
494/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000495void hw_error(const char *fmt, ...)
496{
497 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000498 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000499
500 va_start(ap, fmt);
501 fprintf(stderr, "qemu: hardware error: ");
502 vfprintf(stderr, fmt, ap);
503 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000504 for(env = first_cpu; env != NULL; env = env->next_cpu) {
505 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000506#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000507 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000508#else
bellard6a00d602005-11-21 23:25:50 +0000509 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000510#endif
bellard6a00d602005-11-21 23:25:50 +0000511 }
bellard0824d6f2003-06-24 13:42:40 +0000512 va_end(ap);
513 abort();
514}
aliguoridf751fa2008-12-04 20:19:35 +0000515
516/***************/
517/* ballooning */
518
519static QEMUBalloonEvent *qemu_balloon_event;
520void *qemu_balloon_event_opaque;
521
522void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
523{
524 qemu_balloon_event = func;
525 qemu_balloon_event_opaque = opaque;
526}
527
528void qemu_balloon(ram_addr_t target)
529{
530 if (qemu_balloon_event)
531 qemu_balloon_event(qemu_balloon_event_opaque, target);
532}
533
534ram_addr_t qemu_balloon_status(void)
535{
536 if (qemu_balloon_event)
537 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
538 return 0;
539}
bellard0824d6f2003-06-24 13:42:40 +0000540
bellard8a7ddc32004-03-31 19:00:16 +0000541/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000542/* keyboard/mouse */
543
544static QEMUPutKBDEvent *qemu_put_kbd_event;
545static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000546static QEMUPutMouseEntry *qemu_put_mouse_event_head;
547static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000548
549void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
550{
551 qemu_put_kbd_event_opaque = opaque;
552 qemu_put_kbd_event = func;
553}
554
ths455204e2007-01-05 16:42:13 +0000555QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
556 void *opaque, int absolute,
557 const char *name)
bellard63066f42004-06-03 18:45:02 +0000558{
ths455204e2007-01-05 16:42:13 +0000559 QEMUPutMouseEntry *s, *cursor;
560
561 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000562
563 s->qemu_put_mouse_event = func;
564 s->qemu_put_mouse_event_opaque = opaque;
565 s->qemu_put_mouse_event_absolute = absolute;
566 s->qemu_put_mouse_event_name = qemu_strdup(name);
567 s->next = NULL;
568
569 if (!qemu_put_mouse_event_head) {
570 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
571 return s;
572 }
573
574 cursor = qemu_put_mouse_event_head;
575 while (cursor->next != NULL)
576 cursor = cursor->next;
577
578 cursor->next = s;
579 qemu_put_mouse_event_current = s;
580
581 return s;
582}
583
584void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
585{
586 QEMUPutMouseEntry *prev = NULL, *cursor;
587
588 if (!qemu_put_mouse_event_head || entry == NULL)
589 return;
590
591 cursor = qemu_put_mouse_event_head;
592 while (cursor != NULL && cursor != entry) {
593 prev = cursor;
594 cursor = cursor->next;
595 }
596
597 if (cursor == NULL) // does not exist or list empty
598 return;
599 else if (prev == NULL) { // entry is head
600 qemu_put_mouse_event_head = cursor->next;
601 if (qemu_put_mouse_event_current == entry)
602 qemu_put_mouse_event_current = cursor->next;
603 qemu_free(entry->qemu_put_mouse_event_name);
604 qemu_free(entry);
605 return;
606 }
607
608 prev->next = entry->next;
609
610 if (qemu_put_mouse_event_current == entry)
611 qemu_put_mouse_event_current = prev;
612
613 qemu_free(entry->qemu_put_mouse_event_name);
614 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000615}
616
617void kbd_put_keycode(int keycode)
618{
619 if (qemu_put_kbd_event) {
620 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
621 }
622}
623
624void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
625{
ths455204e2007-01-05 16:42:13 +0000626 QEMUPutMouseEvent *mouse_event;
627 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000628 int width;
ths455204e2007-01-05 16:42:13 +0000629
630 if (!qemu_put_mouse_event_current) {
631 return;
632 }
633
634 mouse_event =
635 qemu_put_mouse_event_current->qemu_put_mouse_event;
636 mouse_event_opaque =
637 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
638
639 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000640 if (graphic_rotate) {
641 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
642 width = 0x7fff;
643 else
aurel32b94ed572008-03-10 19:34:27 +0000644 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000645 mouse_event(mouse_event_opaque,
646 width - dy, dx, dz, buttons_state);
647 } else
648 mouse_event(mouse_event_opaque,
649 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000650 }
651}
652
bellard09b26c52006-04-12 21:09:08 +0000653int kbd_mouse_is_absolute(void)
654{
ths455204e2007-01-05 16:42:13 +0000655 if (!qemu_put_mouse_event_current)
656 return 0;
657
658 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
659}
660
aliguori376253e2009-03-05 23:01:23 +0000661void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000662{
663 QEMUPutMouseEntry *cursor;
664 int index = 0;
665
666 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000667 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000668 return;
669 }
670
aliguori376253e2009-03-05 23:01:23 +0000671 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000672 cursor = qemu_put_mouse_event_head;
673 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000674 monitor_printf(mon, "%c Mouse #%d: %s\n",
675 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
676 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000677 index++;
678 cursor = cursor->next;
679 }
680}
681
aliguori376253e2009-03-05 23:01:23 +0000682void do_mouse_set(Monitor *mon, int index)
ths455204e2007-01-05 16:42:13 +0000683{
684 QEMUPutMouseEntry *cursor;
685 int i = 0;
686
687 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000688 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000689 return;
690 }
691
692 cursor = qemu_put_mouse_event_head;
693 while (cursor != NULL && index != i) {
694 i++;
695 cursor = cursor->next;
696 }
697
698 if (cursor != NULL)
699 qemu_put_mouse_event_current = cursor;
700 else
aliguori376253e2009-03-05 23:01:23 +0000701 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000702}
703
bellard87858c82003-06-27 12:01:39 +0000704/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000705uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000706{
707 union {
708 uint64_t ll;
709 struct {
710#ifdef WORDS_BIGENDIAN
711 uint32_t high, low;
712#else
713 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000714#endif
bellard87858c82003-06-27 12:01:39 +0000715 } l;
716 } u, res;
717 uint64_t rl, rh;
718
719 u.ll = a;
720 rl = (uint64_t)u.l.low * (uint64_t)b;
721 rh = (uint64_t)u.l.high * (uint64_t)b;
722 rh += (rl >> 32);
723 res.l.high = rh / c;
724 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
725 return res.ll;
726}
727
bellard1dce7c32006-07-13 23:20:22 +0000728/***********************************************************/
729/* real time host monotonic timer */
730
731#define QEMU_TIMER_BASE 1000000000LL
732
733#ifdef WIN32
734
735static int64_t clock_freq;
736
737static void init_get_clock(void)
738{
bellarda8e5ac32006-07-14 09:36:13 +0000739 LARGE_INTEGER freq;
740 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000741 ret = QueryPerformanceFrequency(&freq);
742 if (ret == 0) {
743 fprintf(stderr, "Could not calibrate ticks\n");
744 exit(1);
745 }
746 clock_freq = freq.QuadPart;
747}
748
749static int64_t get_clock(void)
750{
751 LARGE_INTEGER ti;
752 QueryPerformanceCounter(&ti);
753 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
754}
755
756#else
757
758static int use_rt_clock;
759
760static void init_get_clock(void)
761{
762 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000763#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
764 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000765 {
766 struct timespec ts;
767 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
768 use_rt_clock = 1;
769 }
770 }
771#endif
772}
773
774static int64_t get_clock(void)
775{
blueswir1c5e97232009-03-07 20:06:23 +0000776#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
777 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000778 if (use_rt_clock) {
779 struct timespec ts;
780 clock_gettime(CLOCK_MONOTONIC, &ts);
781 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000782 } else
bellard1dce7c32006-07-13 23:20:22 +0000783#endif
784 {
785 /* XXX: using gettimeofday leads to problems if the date
786 changes, so it should be avoided. */
787 struct timeval tv;
788 gettimeofday(&tv, NULL);
789 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
790 }
791}
bellard1dce7c32006-07-13 23:20:22 +0000792#endif
793
pbrook2e70f6e2008-06-29 01:03:05 +0000794/* Return the virtual CPU time, based on the instruction counter. */
795static int64_t cpu_get_icount(void)
796{
797 int64_t icount;
798 CPUState *env = cpu_single_env;;
799 icount = qemu_icount;
800 if (env) {
801 if (!can_do_io(env))
802 fprintf(stderr, "Bad clock read\n");
803 icount -= (env->icount_decr.u16.low + env->icount_extra);
804 }
805 return qemu_icount_bias + (icount << icount_time_shift);
806}
807
bellard1dce7c32006-07-13 23:20:22 +0000808/***********************************************************/
809/* guest cycle counter */
810
811static int64_t cpu_ticks_prev;
812static int64_t cpu_ticks_offset;
813static int64_t cpu_clock_offset;
814static int cpu_ticks_enabled;
815
816/* return the host CPU cycle counter and handle stop/restart */
817int64_t cpu_get_ticks(void)
818{
pbrook2e70f6e2008-06-29 01:03:05 +0000819 if (use_icount) {
820 return cpu_get_icount();
821 }
bellard1dce7c32006-07-13 23:20:22 +0000822 if (!cpu_ticks_enabled) {
823 return cpu_ticks_offset;
824 } else {
825 int64_t ticks;
826 ticks = cpu_get_real_ticks();
827 if (cpu_ticks_prev > ticks) {
828 /* Note: non increasing ticks may happen if the host uses
829 software suspend */
830 cpu_ticks_offset += cpu_ticks_prev - ticks;
831 }
832 cpu_ticks_prev = ticks;
833 return ticks + cpu_ticks_offset;
834 }
835}
836
837/* return the host CPU monotonic timer and handle stop/restart */
838static int64_t cpu_get_clock(void)
839{
840 int64_t ti;
841 if (!cpu_ticks_enabled) {
842 return cpu_clock_offset;
843 } else {
844 ti = get_clock();
845 return ti + cpu_clock_offset;
846 }
847}
848
849/* enable cpu_get_ticks() */
850void cpu_enable_ticks(void)
851{
852 if (!cpu_ticks_enabled) {
853 cpu_ticks_offset -= cpu_get_real_ticks();
854 cpu_clock_offset -= get_clock();
855 cpu_ticks_enabled = 1;
856 }
857}
858
859/* disable cpu_get_ticks() : the clock is stopped. You must not call
860 cpu_get_ticks() after that. */
861void cpu_disable_ticks(void)
862{
863 if (cpu_ticks_enabled) {
864 cpu_ticks_offset = cpu_get_ticks();
865 cpu_clock_offset = cpu_get_clock();
866 cpu_ticks_enabled = 0;
867 }
868}
869
870/***********************************************************/
871/* timers */
ths5fafdf22007-09-16 21:08:06 +0000872
bellard8a7ddc32004-03-31 19:00:16 +0000873#define QEMU_TIMER_REALTIME 0
874#define QEMU_TIMER_VIRTUAL 1
875
876struct QEMUClock {
877 int type;
878 /* XXX: add frequency */
879};
880
881struct QEMUTimer {
882 QEMUClock *clock;
883 int64_t expire_time;
884 QEMUTimerCB *cb;
885 void *opaque;
886 struct QEMUTimer *next;
887};
888
thsc8994012007-08-19 21:56:03 +0000889struct qemu_alarm_timer {
890 char const *name;
thsefe75412007-08-24 01:36:32 +0000891 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000892
893 int (*start)(struct qemu_alarm_timer *t);
894 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000895 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000896 void *priv;
897};
898
thsefe75412007-08-24 01:36:32 +0000899#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000900#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000901
902static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
903{
904 return t->flags & ALARM_FLAG_DYNTICKS;
905}
906
907static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
908{
909 if (!alarm_has_dynticks(t))
910 return;
911
912 t->rearm(t);
913}
914
915/* TODO: MIN_TIMER_REARM_US should be optimized */
916#define MIN_TIMER_REARM_US 250
917
thsc8994012007-08-19 21:56:03 +0000918static struct qemu_alarm_timer *alarm_timer;
aliguorif49e58d2008-11-05 21:22:34 +0000919#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +0000920static int alarm_timer_rfd, alarm_timer_wfd;
aliguorif49e58d2008-11-05 21:22:34 +0000921#endif
thsc8994012007-08-19 21:56:03 +0000922
923#ifdef _WIN32
924
925struct qemu_alarm_win32 {
926 MMRESULT timerId;
927 HANDLE host_alarm;
928 unsigned int period;
929} alarm_win32_data = {0, NULL, -1};
930
931static int win32_start_timer(struct qemu_alarm_timer *t);
932static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000933static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000934
935#else
936
937static int unix_start_timer(struct qemu_alarm_timer *t);
938static void unix_stop_timer(struct qemu_alarm_timer *t);
939
ths231c6582007-08-26 17:29:15 +0000940#ifdef __linux__
941
thsefe75412007-08-24 01:36:32 +0000942static int dynticks_start_timer(struct qemu_alarm_timer *t);
943static void dynticks_stop_timer(struct qemu_alarm_timer *t);
944static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
945
thsc40ec5a2007-08-19 22:09:40 +0000946static int hpet_start_timer(struct qemu_alarm_timer *t);
947static void hpet_stop_timer(struct qemu_alarm_timer *t);
948
thsc8994012007-08-19 21:56:03 +0000949static int rtc_start_timer(struct qemu_alarm_timer *t);
950static void rtc_stop_timer(struct qemu_alarm_timer *t);
951
thsefe75412007-08-24 01:36:32 +0000952#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000953
954#endif /* _WIN32 */
955
pbrook2e70f6e2008-06-29 01:03:05 +0000956/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000957 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000958 When the guest is idle real and virtual time will be aligned in
959 the IO wait loop. */
960#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
961
962static void icount_adjust(void)
963{
964 int64_t cur_time;
965 int64_t cur_icount;
966 int64_t delta;
967 static int64_t last_delta;
968 /* If the VM is not running, then do nothing. */
969 if (!vm_running)
970 return;
971
972 cur_time = cpu_get_clock();
973 cur_icount = qemu_get_clock(vm_clock);
974 delta = cur_icount - cur_time;
975 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
976 if (delta > 0
977 && last_delta + ICOUNT_WOBBLE < delta * 2
978 && icount_time_shift > 0) {
979 /* The guest is getting too far ahead. Slow time down. */
980 icount_time_shift--;
981 }
982 if (delta < 0
983 && last_delta - ICOUNT_WOBBLE > delta * 2
984 && icount_time_shift < MAX_ICOUNT_SHIFT) {
985 /* The guest is getting too far behind. Speed time up. */
986 icount_time_shift++;
987 }
988 last_delta = delta;
989 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
990}
991
992static void icount_adjust_rt(void * opaque)
993{
994 qemu_mod_timer(icount_rt_timer,
995 qemu_get_clock(rt_clock) + 1000);
996 icount_adjust();
997}
998
999static void icount_adjust_vm(void * opaque)
1000{
1001 qemu_mod_timer(icount_vm_timer,
1002 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1003 icount_adjust();
1004}
1005
1006static void init_icount_adjust(void)
1007{
1008 /* Have both realtime and virtual time triggers for speed adjustment.
1009 The realtime trigger catches emulated time passing too slowly,
1010 the virtual time trigger catches emulated time passing too fast.
1011 Realtime triggers occur even when idle, so use them less frequently
1012 than VM triggers. */
1013 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1014 qemu_mod_timer(icount_rt_timer,
1015 qemu_get_clock(rt_clock) + 1000);
1016 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1017 qemu_mod_timer(icount_vm_timer,
1018 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1019}
1020
thsc8994012007-08-19 21:56:03 +00001021static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +00001022#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +00001023#ifdef __linux__
thsefe75412007-08-24 01:36:32 +00001024 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1025 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001026 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +00001027 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001028 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +00001029 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001030#endif
thsefe75412007-08-24 01:36:32 +00001031 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001032#else
thsefe75412007-08-24 01:36:32 +00001033 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1034 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1035 {"win32", 0, win32_start_timer,
1036 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +00001037#endif
1038 {NULL, }
1039};
1040
blueswir13f47aa82008-03-09 06:59:01 +00001041static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +00001042{
1043 int i;
1044
1045 printf("Available alarm timers, in order of precedence:\n");
1046 for (i = 0; alarm_timers[i].name; i++)
1047 printf("%s\n", alarm_timers[i].name);
1048}
1049
1050static void configure_alarms(char const *opt)
1051{
1052 int i;
1053 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +00001054 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +00001055 char *arg;
1056 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +00001057 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +00001058
aurel323adda042008-03-09 23:43:49 +00001059 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +00001060 show_available_alarms();
1061 exit(0);
1062 }
1063
1064 arg = strdup(opt);
1065
1066 /* Reorder the array */
1067 name = strtok(arg, ",");
1068 while (name) {
balroge2b577e2007-09-17 21:25:20 +00001069 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +00001070 if (!strcmp(alarm_timers[i].name, name))
1071 break;
1072 }
1073
1074 if (i == count) {
1075 fprintf(stderr, "Unknown clock %s\n", name);
1076 goto next;
1077 }
1078
1079 if (i < cur)
1080 /* Ignore */
1081 goto next;
1082
1083 /* Swap */
1084 tmp = alarm_timers[i];
1085 alarm_timers[i] = alarm_timers[cur];
1086 alarm_timers[cur] = tmp;
1087
1088 cur++;
1089next:
1090 name = strtok(NULL, ",");
1091 }
1092
1093 free(arg);
1094
1095 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +00001096 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +00001097 for (i = cur; i < count; i++)
1098 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +00001099 } else {
1100 show_available_alarms();
1101 exit(1);
thsf3dcfad2007-08-24 01:26:02 +00001102 }
thsf3dcfad2007-08-24 01:26:02 +00001103}
1104
bellard8a7ddc32004-03-31 19:00:16 +00001105QEMUClock *rt_clock;
1106QEMUClock *vm_clock;
1107
1108static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +00001109
pbrook9596ebb2007-11-18 01:44:38 +00001110static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001111{
1112 QEMUClock *clock;
1113 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +00001114 clock->type = type;
1115 return clock;
1116}
1117
1118QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1119{
1120 QEMUTimer *ts;
1121
1122 ts = qemu_mallocz(sizeof(QEMUTimer));
1123 ts->clock = clock;
1124 ts->cb = cb;
1125 ts->opaque = opaque;
1126 return ts;
1127}
1128
1129void qemu_free_timer(QEMUTimer *ts)
1130{
1131 qemu_free(ts);
1132}
1133
1134/* stop a timer, but do not dealloc it */
1135void qemu_del_timer(QEMUTimer *ts)
1136{
1137 QEMUTimer **pt, *t;
1138
1139 /* NOTE: this code must be signal safe because
1140 qemu_timer_expired() can be called from a signal. */
1141 pt = &active_timers[ts->clock->type];
1142 for(;;) {
1143 t = *pt;
1144 if (!t)
1145 break;
1146 if (t == ts) {
1147 *pt = t->next;
1148 break;
1149 }
1150 pt = &t->next;
1151 }
1152}
1153
1154/* modify the current timer so that it will be fired when current_time
1155 >= expire_time. The corresponding callback will be called. */
1156void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1157{
1158 QEMUTimer **pt, *t;
1159
1160 qemu_del_timer(ts);
1161
1162 /* add the timer in the sorted list */
1163 /* NOTE: this code must be signal safe because
1164 qemu_timer_expired() can be called from a signal. */
1165 pt = &active_timers[ts->clock->type];
1166 for(;;) {
1167 t = *pt;
1168 if (!t)
1169 break;
ths5fafdf22007-09-16 21:08:06 +00001170 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001171 break;
1172 pt = &t->next;
1173 }
1174 ts->expire_time = expire_time;
1175 ts->next = *pt;
1176 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001177
1178 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001179 if (pt == &active_timers[ts->clock->type]) {
1180 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1181 qemu_rearm_alarm_timer(alarm_timer);
1182 }
1183 /* Interrupt execution to force deadline recalculation. */
1184 if (use_icount && cpu_single_env) {
aurel323098dba2009-03-07 21:28:24 +00001185 cpu_exit(cpu_single_env);
pbrook2e70f6e2008-06-29 01:03:05 +00001186 }
1187 }
bellard8a7ddc32004-03-31 19:00:16 +00001188}
1189
1190int qemu_timer_pending(QEMUTimer *ts)
1191{
1192 QEMUTimer *t;
1193 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1194 if (t == ts)
1195 return 1;
1196 }
1197 return 0;
1198}
1199
1200static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1201{
1202 if (!timer_head)
1203 return 0;
1204 return (timer_head->expire_time <= current_time);
1205}
1206
1207static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1208{
1209 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001210
bellard8a7ddc32004-03-31 19:00:16 +00001211 for(;;) {
1212 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001213 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001214 break;
1215 /* remove timer from the list before calling the callback */
1216 *ptimer_head = ts->next;
1217 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001218
bellard8a7ddc32004-03-31 19:00:16 +00001219 /* run the callback (the timer list can be modified) */
1220 ts->cb(ts->opaque);
1221 }
1222}
1223
1224int64_t qemu_get_clock(QEMUClock *clock)
1225{
1226 switch(clock->type) {
1227 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001228 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001229 default:
1230 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001231 if (use_icount) {
1232 return cpu_get_icount();
1233 } else {
1234 return cpu_get_clock();
1235 }
bellard8a7ddc32004-03-31 19:00:16 +00001236 }
1237}
1238
bellard1dce7c32006-07-13 23:20:22 +00001239static void init_timers(void)
1240{
1241 init_get_clock();
1242 ticks_per_sec = QEMU_TIMER_BASE;
1243 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1244 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1245}
1246
bellard8a7ddc32004-03-31 19:00:16 +00001247/* save a timer */
1248void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1249{
1250 uint64_t expire_time;
1251
1252 if (qemu_timer_pending(ts)) {
1253 expire_time = ts->expire_time;
1254 } else {
1255 expire_time = -1;
1256 }
1257 qemu_put_be64(f, expire_time);
1258}
1259
1260void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1261{
1262 uint64_t expire_time;
1263
1264 expire_time = qemu_get_be64(f);
1265 if (expire_time != -1) {
1266 qemu_mod_timer(ts, expire_time);
1267 } else {
1268 qemu_del_timer(ts);
1269 }
1270}
1271
1272static void timer_save(QEMUFile *f, void *opaque)
1273{
1274 if (cpu_ticks_enabled) {
1275 hw_error("cannot save state if virtual timers are running");
1276 }
thsbee8d682007-12-16 23:41:11 +00001277 qemu_put_be64(f, cpu_ticks_offset);
1278 qemu_put_be64(f, ticks_per_sec);
1279 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001280}
1281
1282static int timer_load(QEMUFile *f, void *opaque, int version_id)
1283{
bellardc88676f2006-08-06 13:36:11 +00001284 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001285 return -EINVAL;
1286 if (cpu_ticks_enabled) {
1287 return -EINVAL;
1288 }
thsbee8d682007-12-16 23:41:11 +00001289 cpu_ticks_offset=qemu_get_be64(f);
1290 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001291 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001292 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001293 }
bellard8a7ddc32004-03-31 19:00:16 +00001294 return 0;
1295}
1296
bellard67b915a2004-03-31 23:37:16 +00001297#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001298void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001299 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1300#else
bellard8a7ddc32004-03-31 19:00:16 +00001301static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001302#endif
bellard8a7ddc32004-03-31 19:00:16 +00001303{
bellard02ba45c2004-06-25 14:46:23 +00001304#if 0
1305#define DISP_FREQ 1000
1306 {
1307 static int64_t delta_min = INT64_MAX;
1308 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1309 static int count;
1310 ti = qemu_get_clock(vm_clock);
1311 if (last_clock != 0) {
1312 delta = ti - last_clock;
1313 if (delta < delta_min)
1314 delta_min = delta;
1315 if (delta > delta_max)
1316 delta_max = delta;
1317 delta_cum += delta;
1318 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001319 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001320 muldiv64(delta_min, 1000000, ticks_per_sec),
1321 muldiv64(delta_max, 1000000, ticks_per_sec),
1322 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1323 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1324 count = 0;
1325 delta_min = INT64_MAX;
1326 delta_max = 0;
1327 delta_cum = 0;
1328 }
1329 }
1330 last_clock = ti;
1331 }
1332#endif
thsefe75412007-08-24 01:36:32 +00001333 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001334 (!use_icount &&
1335 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1336 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001337 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1338 qemu_get_clock(rt_clock))) {
aliguoric96f1a42008-11-05 20:29:45 +00001339 CPUState *env = next_cpu;
aliguoric96f1a42008-11-05 20:29:45 +00001340
bellard06d9f2f2006-05-01 13:23:04 +00001341#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001342 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1343 SetEvent(data->host_alarm);
aliguorif49e58d2008-11-05 21:22:34 +00001344#else
1345 static const char byte = 0;
aliguoric96f1a42008-11-05 20:29:45 +00001346 write(alarm_timer_wfd, &byte, sizeof(byte));
aliguorif49e58d2008-11-05 21:22:34 +00001347#endif
balrogd5d08332008-01-05 19:41:47 +00001348 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1349
balrog4f8eb8d2007-12-16 12:39:38 +00001350 if (env) {
1351 /* stop the currently executing cpu because a timer occured */
aurel323098dba2009-03-07 21:28:24 +00001352 cpu_exit(env);
bellarda332e112005-09-03 17:55:47 +00001353#ifdef USE_KQEMU
balrog4f8eb8d2007-12-16 12:39:38 +00001354 if (env->kqemu_enabled) {
1355 kqemu_cpu_interrupt(env);
1356 }
balrogee5605e2007-12-03 03:01:40 +00001357#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001358 }
balrogee5605e2007-12-03 03:01:40 +00001359 event_pending = 1;
bellard8a7ddc32004-03-31 19:00:16 +00001360 }
1361}
1362
pbrook2e70f6e2008-06-29 01:03:05 +00001363static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001364{
pbrook2e70f6e2008-06-29 01:03:05 +00001365 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001366
1367 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001368 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1369 qemu_get_clock(vm_clock);
1370 } else {
1371 /* To avoid problems with overflow limit this to 2^32. */
1372 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001373 }
1374
pbrook2e70f6e2008-06-29 01:03:05 +00001375 if (delta < 0)
1376 delta = 0;
thsefe75412007-08-24 01:36:32 +00001377
pbrook2e70f6e2008-06-29 01:03:05 +00001378 return delta;
1379}
1380
blueswir18632fb92008-09-14 13:59:34 +00001381#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001382static uint64_t qemu_next_deadline_dyntick(void)
1383{
1384 int64_t delta;
1385 int64_t rtdelta;
1386
1387 if (use_icount)
1388 delta = INT32_MAX;
1389 else
1390 delta = (qemu_next_deadline() + 999) / 1000;
1391
1392 if (active_timers[QEMU_TIMER_REALTIME]) {
1393 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1394 qemu_get_clock(rt_clock))*1000;
1395 if (rtdelta < delta)
1396 delta = rtdelta;
1397 }
1398
1399 if (delta < MIN_TIMER_REARM_US)
1400 delta = MIN_TIMER_REARM_US;
1401
1402 return delta;
thsefe75412007-08-24 01:36:32 +00001403}
blueswir18632fb92008-09-14 13:59:34 +00001404#endif
thsefe75412007-08-24 01:36:32 +00001405
bellardfd872592004-05-12 19:11:15 +00001406#ifndef _WIN32
1407
aliguori7183b4b2008-11-05 20:40:18 +00001408/* Sets a specific flag */
1409static int fcntl_setfl(int fd, int flag)
1410{
1411 int flags;
1412
1413 flags = fcntl(fd, F_GETFL);
1414 if (flags == -1)
1415 return -errno;
1416
1417 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1418 return -errno;
1419
1420 return 0;
1421}
1422
bellard829309c2004-05-20 13:20:12 +00001423#if defined(__linux__)
1424
bellardfd872592004-05-12 19:11:15 +00001425#define RTC_FREQ 1024
1426
aurel32de9a95f2008-11-11 13:41:01 +00001427static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001428{
thsc8994012007-08-19 21:56:03 +00001429 struct sigaction act;
1430
1431 /* timer signal */
1432 sigfillset(&act.sa_mask);
1433 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001434 act.sa_handler = host_alarm_handler;
1435
1436 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001437 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001438 fcntl(fd, F_SETOWN, getpid());
1439}
1440
thsc40ec5a2007-08-19 22:09:40 +00001441static int hpet_start_timer(struct qemu_alarm_timer *t)
1442{
1443 struct hpet_info info;
1444 int r, fd;
1445
1446 fd = open("/dev/hpet", O_RDONLY);
1447 if (fd < 0)
1448 return -1;
1449
1450 /* Set frequency */
1451 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1452 if (r < 0) {
1453 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1454 "error, but for better emulation accuracy type:\n"
1455 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1456 goto fail;
1457 }
1458
1459 /* Check capabilities */
1460 r = ioctl(fd, HPET_INFO, &info);
1461 if (r < 0)
1462 goto fail;
1463
1464 /* Enable periodic mode */
1465 r = ioctl(fd, HPET_EPI, 0);
1466 if (info.hi_flags && (r < 0))
1467 goto fail;
1468
1469 /* Enable interrupt */
1470 r = ioctl(fd, HPET_IE_ON, 0);
1471 if (r < 0)
1472 goto fail;
1473
1474 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001475 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001476
1477 return 0;
1478fail:
1479 close(fd);
1480 return -1;
1481}
1482
1483static void hpet_stop_timer(struct qemu_alarm_timer *t)
1484{
pbrookfcdc2122007-08-23 20:22:22 +00001485 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001486
1487 close(fd);
1488}
1489
thsc8994012007-08-19 21:56:03 +00001490static int rtc_start_timer(struct qemu_alarm_timer *t)
1491{
1492 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001493 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001494
balrogaeb30be2007-07-02 15:03:13 +00001495 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001496 if (rtc_fd < 0)
1497 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001498 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1499 if (current_rtc_freq != RTC_FREQ &&
1500 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001501 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1502 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1503 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1504 goto fail;
1505 }
1506 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1507 fail:
1508 close(rtc_fd);
1509 return -1;
1510 }
thsc8994012007-08-19 21:56:03 +00001511
1512 enable_sigio_timer(rtc_fd);
1513
pbrookfcdc2122007-08-23 20:22:22 +00001514 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001515
bellardfd872592004-05-12 19:11:15 +00001516 return 0;
1517}
1518
thsc8994012007-08-19 21:56:03 +00001519static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001520{
pbrookfcdc2122007-08-23 20:22:22 +00001521 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001522
1523 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001524}
1525
thsefe75412007-08-24 01:36:32 +00001526static int dynticks_start_timer(struct qemu_alarm_timer *t)
1527{
1528 struct sigevent ev;
1529 timer_t host_timer;
1530 struct sigaction act;
1531
1532 sigfillset(&act.sa_mask);
1533 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001534 act.sa_handler = host_alarm_handler;
1535
1536 sigaction(SIGALRM, &act, NULL);
1537
1538 ev.sigev_value.sival_int = 0;
1539 ev.sigev_notify = SIGEV_SIGNAL;
1540 ev.sigev_signo = SIGALRM;
1541
1542 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1543 perror("timer_create");
1544
1545 /* disable dynticks */
1546 fprintf(stderr, "Dynamic Ticks disabled\n");
1547
1548 return -1;
1549 }
1550
blueswir10399bfe2008-11-16 11:37:18 +00001551 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001552
1553 return 0;
1554}
1555
1556static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1557{
blueswir10399bfe2008-11-16 11:37:18 +00001558 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001559
1560 timer_delete(host_timer);
1561}
1562
1563static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1564{
blueswir10399bfe2008-11-16 11:37:18 +00001565 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001566 struct itimerspec timeout;
1567 int64_t nearest_delta_us = INT64_MAX;
1568 int64_t current_us;
1569
1570 if (!active_timers[QEMU_TIMER_REALTIME] &&
1571 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001572 return;
thsefe75412007-08-24 01:36:32 +00001573
pbrook2e70f6e2008-06-29 01:03:05 +00001574 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001575
1576 /* check whether a timer is already running */
1577 if (timer_gettime(host_timer, &timeout)) {
1578 perror("gettime");
1579 fprintf(stderr, "Internal timer error: aborting\n");
1580 exit(1);
1581 }
1582 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1583 if (current_us && current_us <= nearest_delta_us)
1584 return;
1585
1586 timeout.it_interval.tv_sec = 0;
1587 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1588 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1589 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1590 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1591 perror("settime");
1592 fprintf(stderr, "Internal timer error: aborting\n");
1593 exit(1);
1594 }
1595}
1596
ths70744b32007-08-26 17:31:30 +00001597#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001598
thsc8994012007-08-19 21:56:03 +00001599static int unix_start_timer(struct qemu_alarm_timer *t)
1600{
1601 struct sigaction act;
1602 struct itimerval itv;
1603 int err;
1604
1605 /* timer signal */
1606 sigfillset(&act.sa_mask);
1607 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001608 act.sa_handler = host_alarm_handler;
1609
1610 sigaction(SIGALRM, &act, NULL);
1611
1612 itv.it_interval.tv_sec = 0;
1613 /* for i386 kernel 2.6 to get 1 ms */
1614 itv.it_interval.tv_usec = 999;
1615 itv.it_value.tv_sec = 0;
1616 itv.it_value.tv_usec = 10 * 1000;
1617
1618 err = setitimer(ITIMER_REAL, &itv, NULL);
1619 if (err)
1620 return -1;
1621
1622 return 0;
1623}
1624
1625static void unix_stop_timer(struct qemu_alarm_timer *t)
1626{
1627 struct itimerval itv;
1628
1629 memset(&itv, 0, sizeof(itv));
1630 setitimer(ITIMER_REAL, &itv, NULL);
1631}
1632
bellard829309c2004-05-20 13:20:12 +00001633#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001634
aliguorif49e58d2008-11-05 21:22:34 +00001635static void try_to_rearm_timer(void *opaque)
1636{
1637 struct qemu_alarm_timer *t = opaque;
1638#ifndef _WIN32
1639 ssize_t len;
1640
1641 /* Drain the notify pipe */
1642 do {
1643 char buffer[512];
1644 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1645 } while ((len == -1 && errno == EINTR) || len > 0);
1646#endif
1647
aliguorif49e58d2008-11-05 21:22:34 +00001648 if (t->flags & ALARM_FLAG_EXPIRED) {
1649 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1650 qemu_rearm_alarm_timer(alarm_timer);
1651 }
1652}
1653
thsc8994012007-08-19 21:56:03 +00001654#ifdef _WIN32
1655
1656static int win32_start_timer(struct qemu_alarm_timer *t)
1657{
1658 TIMECAPS tc;
1659 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001660 UINT flags;
thsc8994012007-08-19 21:56:03 +00001661
1662 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1663 if (!data->host_alarm) {
1664 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001665 return -1;
thsc8994012007-08-19 21:56:03 +00001666 }
1667
1668 memset(&tc, 0, sizeof(tc));
1669 timeGetDevCaps(&tc, sizeof(tc));
1670
1671 if (data->period < tc.wPeriodMin)
1672 data->period = tc.wPeriodMin;
1673
1674 timeBeginPeriod(data->period);
1675
thsefe75412007-08-24 01:36:32 +00001676 flags = TIME_CALLBACK_FUNCTION;
1677 if (alarm_has_dynticks(t))
1678 flags |= TIME_ONESHOT;
1679 else
1680 flags |= TIME_PERIODIC;
1681
thsc8994012007-08-19 21:56:03 +00001682 data->timerId = timeSetEvent(1, // interval (ms)
1683 data->period, // resolution
1684 host_alarm_handler, // function
1685 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001686 flags);
thsc8994012007-08-19 21:56:03 +00001687
1688 if (!data->timerId) {
1689 perror("Failed to initialize win32 alarm timer");
1690
1691 timeEndPeriod(data->period);
1692 CloseHandle(data->host_alarm);
1693 return -1;
1694 }
1695
aliguorif49e58d2008-11-05 21:22:34 +00001696 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
thsc8994012007-08-19 21:56:03 +00001697
1698 return 0;
1699}
1700
1701static void win32_stop_timer(struct qemu_alarm_timer *t)
1702{
1703 struct qemu_alarm_win32 *data = t->priv;
1704
1705 timeKillEvent(data->timerId);
1706 timeEndPeriod(data->period);
1707
1708 CloseHandle(data->host_alarm);
1709}
1710
thsefe75412007-08-24 01:36:32 +00001711static void win32_rearm_timer(struct qemu_alarm_timer *t)
1712{
1713 struct qemu_alarm_win32 *data = t->priv;
1714 uint64_t nearest_delta_us;
1715
1716 if (!active_timers[QEMU_TIMER_REALTIME] &&
1717 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001718 return;
thsefe75412007-08-24 01:36:32 +00001719
pbrook2e70f6e2008-06-29 01:03:05 +00001720 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001721 nearest_delta_us /= 1000;
1722
1723 timeKillEvent(data->timerId);
1724
1725 data->timerId = timeSetEvent(1,
1726 data->period,
1727 host_alarm_handler,
1728 (DWORD)t,
1729 TIME_ONESHOT | TIME_PERIODIC);
1730
1731 if (!data->timerId) {
1732 perror("Failed to re-arm win32 alarm timer");
1733
1734 timeEndPeriod(data->period);
1735 CloseHandle(data->host_alarm);
1736 exit(1);
1737 }
1738}
1739
thsc8994012007-08-19 21:56:03 +00001740#endif /* _WIN32 */
1741
aliguori7183b4b2008-11-05 20:40:18 +00001742static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001743{
blueswir1223f0d72008-09-30 18:12:18 +00001744 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001745 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001746
1747#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +00001748 int fds[2];
1749
aliguori7183b4b2008-11-05 20:40:18 +00001750 err = pipe(fds);
1751 if (err == -1)
1752 return -errno;
1753
1754 err = fcntl_setfl(fds[0], O_NONBLOCK);
1755 if (err < 0)
1756 goto fail;
1757
1758 err = fcntl_setfl(fds[1], O_NONBLOCK);
1759 if (err < 0)
1760 goto fail;
1761
aliguoric96f1a42008-11-05 20:29:45 +00001762 alarm_timer_rfd = fds[0];
1763 alarm_timer_wfd = fds[1];
aliguorif49e58d2008-11-05 21:22:34 +00001764#endif
bellard06d9f2f2006-05-01 13:23:04 +00001765
thsc8994012007-08-19 21:56:03 +00001766 for (i = 0; alarm_timers[i].name; i++) {
1767 t = &alarm_timers[i];
1768
thsc8994012007-08-19 21:56:03 +00001769 err = t->start(t);
1770 if (!err)
1771 break;
bellard67b915a2004-03-31 23:37:16 +00001772 }
bellardfd872592004-05-12 19:11:15 +00001773
thsc8994012007-08-19 21:56:03 +00001774 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001775 err = -ENOENT;
1776 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001777 }
thsc8994012007-08-19 21:56:03 +00001778
aliguorif49e58d2008-11-05 21:22:34 +00001779#ifndef _WIN32
aliguori6abfbd72008-11-05 20:49:37 +00001780 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1781 try_to_rearm_timer, NULL, t);
aliguorif49e58d2008-11-05 21:22:34 +00001782#endif
aliguori6abfbd72008-11-05 20:49:37 +00001783
thsc8994012007-08-19 21:56:03 +00001784 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001785
aliguori6abfbd72008-11-05 20:49:37 +00001786 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001787
1788fail:
aliguorif49e58d2008-11-05 21:22:34 +00001789#ifndef _WIN32
aliguori7183b4b2008-11-05 20:40:18 +00001790 close(fds[0]);
1791 close(fds[1]);
aliguorif49e58d2008-11-05 21:22:34 +00001792#endif
aliguori7183b4b2008-11-05 20:40:18 +00001793 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001794}
1795
pbrook9596ebb2007-11-18 01:44:38 +00001796static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001797{
thsc8994012007-08-19 21:56:03 +00001798 alarm_timer->stop(alarm_timer);
1799 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001800}
1801
bellardc4b1fcc2004-03-14 21:44:30 +00001802/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001803/* host time/date access */
1804void qemu_get_timedate(struct tm *tm, int offset)
1805{
1806 time_t ti;
1807 struct tm *ret;
1808
1809 time(&ti);
1810 ti += offset;
1811 if (rtc_date_offset == -1) {
1812 if (rtc_utc)
1813 ret = gmtime(&ti);
1814 else
1815 ret = localtime(&ti);
1816 } else {
1817 ti -= rtc_date_offset;
1818 ret = gmtime(&ti);
1819 }
1820
1821 memcpy(tm, ret, sizeof(struct tm));
1822}
1823
1824int qemu_timedate_diff(struct tm *tm)
1825{
1826 time_t seconds;
1827
1828 if (rtc_date_offset == -1)
1829 if (rtc_utc)
1830 seconds = mktimegm(tm);
1831 else
1832 seconds = mktime(tm);
1833 else
1834 seconds = mktimegm(tm) + rtc_date_offset;
1835
1836 return seconds - time(NULL);
1837}
1838
bellardfd1dff42006-02-01 21:29:26 +00001839#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001840static void socket_cleanup(void)
1841{
1842 WSACleanup();
1843}
bellard82c643f2004-07-14 17:28:13 +00001844
bellardfd1dff42006-02-01 21:29:26 +00001845static int socket_init(void)
1846{
1847 WSADATA Data;
1848 int ret, err;
1849
1850 ret = WSAStartup(MAKEWORD(2,2), &Data);
1851 if (ret != 0) {
1852 err = WSAGetLastError();
1853 fprintf(stderr, "WSAStartup: %d\n", err);
1854 return -1;
1855 }
1856 atexit(socket_cleanup);
1857 return 0;
1858}
aurel3264b7b732008-05-05 10:05:31 +00001859#endif
1860
aliguori63a01ef2008-10-31 19:10:00 +00001861const char *get_opt_name(char *buf, int buf_size, const char *p)
thse4bcb142007-12-02 04:51:10 +00001862{
1863 char *q;
thse4bcb142007-12-02 04:51:10 +00001864
balrog609497a2008-01-14 02:56:53 +00001865 q = buf;
1866 while (*p != '\0' && *p != '=') {
1867 if (q && (q - buf) < buf_size - 1)
1868 *q++ = *p;
1869 p++;
1870 }
1871 if (q)
1872 *q = '\0';
1873
1874 return p;
1875}
1876
aliguori63a01ef2008-10-31 19:10:00 +00001877const char *get_opt_value(char *buf, int buf_size, const char *p)
balrog609497a2008-01-14 02:56:53 +00001878{
1879 char *q;
1880
thse4bcb142007-12-02 04:51:10 +00001881 q = buf;
1882 while (*p != '\0') {
balrog609497a2008-01-14 02:56:53 +00001883 if (*p == ',') {
1884 if (*(p + 1) != ',')
thse4bcb142007-12-02 04:51:10 +00001885 break;
thse4bcb142007-12-02 04:51:10 +00001886 p++;
balrog609497a2008-01-14 02:56:53 +00001887 }
thse4bcb142007-12-02 04:51:10 +00001888 if (q && (q - buf) < buf_size - 1)
1889 *q++ = *p;
1890 p++;
1891 }
1892 if (q)
1893 *q = '\0';
1894
1895 return p;
1896}
1897
aliguori63a01ef2008-10-31 19:10:00 +00001898int get_param_value(char *buf, int buf_size,
1899 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001900{
1901 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001902 char option[128];
1903
1904 p = str;
1905 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001906 p = get_opt_name(option, sizeof(option), p);
bellard7c9d8e02005-11-15 22:16:05 +00001907 if (*p != '=')
1908 break;
1909 p++;
1910 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001911 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001912 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001913 } else {
balrog609497a2008-01-14 02:56:53 +00001914 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001915 }
1916 if (*p != ',')
1917 break;
1918 p++;
1919 }
1920 return 0;
1921}
1922
aliguori63a01ef2008-10-31 19:10:00 +00001923int check_params(char *buf, int buf_size,
1924 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001925{
1926 const char *p;
1927 int i;
1928
1929 p = str;
1930 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001931 p = get_opt_name(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001932 if (*p != '=')
1933 return -1;
1934 p++;
1935 for(i = 0; params[i] != NULL; i++)
1936 if (!strcmp(params[i], buf))
1937 break;
1938 if (params[i] == NULL)
1939 return -1;
balrog609497a2008-01-14 02:56:53 +00001940 p = get_opt_value(NULL, 0, p);
thse4bcb142007-12-02 04:51:10 +00001941 if (*p != ',')
1942 break;
1943 p++;
1944 }
1945 return 0;
1946}
1947
balrog1ae26a12008-09-28 23:19:47 +00001948/***********************************************************/
1949/* Bluetooth support */
1950static int nb_hcis;
1951static int cur_hci;
1952static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001953
balrog1ae26a12008-09-28 23:19:47 +00001954static struct bt_vlan_s {
1955 struct bt_scatternet_s net;
1956 int id;
1957 struct bt_vlan_s *next;
1958} *first_bt_vlan;
1959
1960/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001961static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001962{
1963 struct bt_vlan_s **pvlan, *vlan;
1964 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1965 if (vlan->id == id)
1966 return &vlan->net;
1967 }
1968 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1969 vlan->id = id;
1970 pvlan = &first_bt_vlan;
1971 while (*pvlan != NULL)
1972 pvlan = &(*pvlan)->next;
1973 *pvlan = vlan;
1974 return &vlan->net;
1975}
1976
1977static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1978{
1979}
1980
1981static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1982{
1983 return -ENOTSUP;
1984}
1985
1986static struct HCIInfo null_hci = {
1987 .cmd_send = null_hci_send,
1988 .sco_send = null_hci_send,
1989 .acl_send = null_hci_send,
1990 .bdaddr_set = null_hci_addr_set,
1991};
1992
1993struct HCIInfo *qemu_next_hci(void)
1994{
1995 if (cur_hci == nb_hcis)
1996 return &null_hci;
1997
1998 return hci_table[cur_hci++];
1999}
2000
balrogdc72ac12008-11-09 00:04:26 +00002001static struct HCIInfo *hci_init(const char *str)
2002{
2003 char *endp;
2004 struct bt_scatternet_s *vlan = 0;
2005
2006 if (!strcmp(str, "null"))
2007 /* null */
2008 return &null_hci;
2009 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
2010 /* host[:hciN] */
2011 return bt_host_hci(str[4] ? str + 5 : "hci0");
2012 else if (!strncmp(str, "hci", 3)) {
2013 /* hci[,vlan=n] */
2014 if (str[3]) {
2015 if (!strncmp(str + 3, ",vlan=", 6)) {
2016 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2017 if (*endp)
2018 vlan = 0;
2019 }
2020 } else
2021 vlan = qemu_find_bt_vlan(0);
2022 if (vlan)
2023 return bt_new_hci(vlan);
2024 }
2025
2026 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2027
2028 return 0;
2029}
2030
2031static int bt_hci_parse(const char *str)
2032{
2033 struct HCIInfo *hci;
2034 bdaddr_t bdaddr;
2035
2036 if (nb_hcis >= MAX_NICS) {
2037 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2038 return -1;
2039 }
2040
2041 hci = hci_init(str);
2042 if (!hci)
2043 return -1;
2044
2045 bdaddr.b[0] = 0x52;
2046 bdaddr.b[1] = 0x54;
2047 bdaddr.b[2] = 0x00;
2048 bdaddr.b[3] = 0x12;
2049 bdaddr.b[4] = 0x34;
2050 bdaddr.b[5] = 0x56 + nb_hcis;
2051 hci->bdaddr_set(hci, bdaddr.b);
2052
2053 hci_table[nb_hcis++] = hci;
2054
2055 return 0;
2056}
2057
2058static void bt_vhci_add(int vlan_id)
2059{
2060 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2061
2062 if (!vlan->slave)
2063 fprintf(stderr, "qemu: warning: adding a VHCI to "
2064 "an empty scatternet %i\n", vlan_id);
2065
2066 bt_vhci_init(bt_new_hci(vlan));
2067}
2068
2069static struct bt_device_s *bt_device_add(const char *opt)
2070{
2071 struct bt_scatternet_s *vlan;
2072 int vlan_id = 0;
2073 char *endp = strstr(opt, ",vlan=");
2074 int len = (endp ? endp - opt : strlen(opt)) + 1;
2075 char devname[10];
2076
2077 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2078
2079 if (endp) {
2080 vlan_id = strtol(endp + 6, &endp, 0);
2081 if (*endp) {
2082 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2083 return 0;
2084 }
2085 }
2086
2087 vlan = qemu_find_bt_vlan(vlan_id);
2088
2089 if (!vlan->slave)
2090 fprintf(stderr, "qemu: warning: adding a slave device to "
2091 "an empty scatternet %i\n", vlan_id);
2092
2093 if (!strcmp(devname, "keyboard"))
2094 return bt_keyboard_init(vlan);
2095
2096 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2097 return 0;
2098}
2099
2100static int bt_parse(const char *opt)
2101{
2102 const char *endp, *p;
2103 int vlan;
2104
2105 if (strstart(opt, "hci", &endp)) {
2106 if (!*endp || *endp == ',') {
2107 if (*endp)
2108 if (!strstart(endp, ",vlan=", 0))
2109 opt = endp + 1;
2110
2111 return bt_hci_parse(opt);
2112 }
2113 } else if (strstart(opt, "vhci", &endp)) {
2114 if (!*endp || *endp == ',') {
2115 if (*endp) {
2116 if (strstart(endp, ",vlan=", &p)) {
2117 vlan = strtol(p, (char **) &endp, 0);
2118 if (*endp) {
2119 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2120 return 1;
2121 }
2122 } else {
2123 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2124 return 1;
2125 }
2126 } else
2127 vlan = 0;
2128
2129 bt_vhci_add(vlan);
2130 return 0;
2131 }
2132 } else if (strstart(opt, "device:", &endp))
2133 return !bt_device_add(endp);
2134
2135 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2136 return 1;
2137}
2138
balrog1ae26a12008-09-28 23:19:47 +00002139/***********************************************************/
2140/* QEMU Block devices */
2141
balrog609497a2008-01-14 02:56:53 +00002142#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00002143#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00002144#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00002145#define PFLASH_ALIAS "if=pflash"
2146#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00002147#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002148
aliguori7d5aca92009-02-11 15:19:58 +00002149static int drive_opt_get_free_idx(void)
2150{
2151 int index;
2152
2153 for (index = 0; index < MAX_DRIVES; index++)
2154 if (!drives_opt[index].used) {
2155 drives_opt[index].used = 1;
2156 return index;
2157 }
2158
2159 return -1;
2160}
2161
2162static int drive_get_free_idx(void)
2163{
2164 int index;
2165
2166 for (index = 0; index < MAX_DRIVES; index++)
2167 if (!drives_table[index].used) {
2168 drives_table[index].used = 1;
2169 return index;
2170 }
2171
2172 return -1;
2173}
2174
aliguori4d73cd32009-02-11 15:20:46 +00002175int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002176{
2177 va_list ap;
aliguori7d5aca92009-02-11 15:19:58 +00002178 int index = drive_opt_get_free_idx();
thse4bcb142007-12-02 04:51:10 +00002179
aliguori7d5aca92009-02-11 15:19:58 +00002180 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
thse4bcb142007-12-02 04:51:10 +00002181 fprintf(stderr, "qemu: too many drives\n");
aliguori4d73cd32009-02-11 15:20:46 +00002182 return -1;
thse4bcb142007-12-02 04:51:10 +00002183 }
2184
aliguori7d5aca92009-02-11 15:19:58 +00002185 drives_opt[index].file = file;
thse4bcb142007-12-02 04:51:10 +00002186 va_start(ap, fmt);
aliguori7d5aca92009-02-11 15:19:58 +00002187 vsnprintf(drives_opt[index].opt,
balrog609497a2008-01-14 02:56:53 +00002188 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002189 va_end(ap);
2190
aliguori7d5aca92009-02-11 15:19:58 +00002191 nb_drives_opt++;
2192 return index;
thse4bcb142007-12-02 04:51:10 +00002193}
2194
aliguorib01b1112009-02-11 15:20:20 +00002195void drive_remove(int index)
2196{
2197 drives_opt[index].used = 0;
2198 nb_drives_opt--;
2199}
2200
thsf60d39b2007-12-17 03:55:57 +00002201int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002202{
2203 int index;
2204
2205 /* seek interface, bus and unit */
2206
aliguori7d5aca92009-02-11 15:19:58 +00002207 for (index = 0; index < MAX_DRIVES; index++)
thsf60d39b2007-12-17 03:55:57 +00002208 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002209 drives_table[index].bus == bus &&
aliguori7d5aca92009-02-11 15:19:58 +00002210 drives_table[index].unit == unit &&
2211 drives_table[index].used)
thse4bcb142007-12-02 04:51:10 +00002212 return index;
2213
2214 return -1;
2215}
2216
thsf60d39b2007-12-17 03:55:57 +00002217int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002218{
2219 int max_bus;
2220 int index;
2221
2222 max_bus = -1;
2223 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002224 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002225 drives_table[index].bus > max_bus)
2226 max_bus = drives_table[index].bus;
2227 }
2228 return max_bus;
2229}
2230
aliguorifa879c62009-01-07 17:32:33 +00002231const char *drive_get_serial(BlockDriverState *bdrv)
2232{
2233 int index;
2234
2235 for (index = 0; index < nb_drives; index++)
2236 if (drives_table[index].bdrv == bdrv)
2237 return drives_table[index].serial;
2238
2239 return "\0";
2240}
2241
aliguori428c5702009-01-21 18:59:04 +00002242BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2243{
2244 int index;
2245
2246 for (index = 0; index < nb_drives; index++)
2247 if (drives_table[index].bdrv == bdrv)
2248 return drives_table[index].onerror;
2249
aliguoricdad4bd2009-02-28 16:51:01 +00002250 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002251}
2252
aurel32a1620fa2008-04-29 05:58:01 +00002253static void bdrv_format_print(void *opaque, const char *name)
2254{
2255 fprintf(stderr, " %s", name);
2256}
2257
aliguorib01b1112009-02-11 15:20:20 +00002258void drive_uninit(BlockDriverState *bdrv)
2259{
2260 int i;
2261
2262 for (i = 0; i < MAX_DRIVES; i++)
2263 if (drives_table[i].bdrv == bdrv) {
2264 drives_table[i].bdrv = NULL;
2265 drives_table[i].used = 0;
2266 drive_remove(drives_table[i].drive_opt_idx);
2267 nb_drives--;
2268 break;
2269 }
2270}
2271
aliguori4d73cd32009-02-11 15:20:46 +00002272int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
thse4bcb142007-12-02 04:51:10 +00002273{
2274 char buf[128];
2275 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002276 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00002277 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002278 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002279 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002280 enum { MEDIA_DISK, MEDIA_CDROM } media;
2281 int bus_id, unit_id;
2282 int cyls, heads, secs, translation;
2283 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002284 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002285 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002286 int max_devs;
2287 int index;
balrog33f00272007-12-24 14:33:24 +00002288 int cache;
aliguori428c5702009-01-21 18:59:04 +00002289 int bdrv_flags, onerror;
aliguori7d5aca92009-02-11 15:19:58 +00002290 int drives_table_idx;
balrog609497a2008-01-14 02:56:53 +00002291 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002292 static const char * const params[] = { "bus", "unit", "if", "index",
2293 "cyls", "heads", "secs", "trans",
2294 "media", "snapshot", "file",
aliguori428c5702009-01-21 18:59:04 +00002295 "cache", "format", "serial", "werror",
2296 NULL };
thse4bcb142007-12-02 04:51:10 +00002297
2298 if (check_params(buf, sizeof(buf), params, str) < 0) {
balrogff993632008-02-10 13:21:25 +00002299 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
thse4bcb142007-12-02 04:51:10 +00002300 buf, str);
2301 return -1;
2302 }
2303
2304 file[0] = 0;
2305 cyls = heads = secs = 0;
2306 bus_id = 0;
2307 unit_id = -1;
2308 translation = BIOS_ATA_TRANSLATION_AUTO;
2309 index = -1;
aliguori4dc822d2008-12-04 21:39:21 +00002310 cache = 3;
thse4bcb142007-12-02 04:51:10 +00002311
blueswir1c9b1ae22008-09-28 18:55:17 +00002312 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002313 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002314 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002315 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002316 } else {
thsf60d39b2007-12-17 03:55:57 +00002317 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002318 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002319 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002320 }
2321 media = MEDIA_DISK;
2322
2323 /* extract parameters */
2324
2325 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2326 bus_id = strtol(buf, NULL, 0);
2327 if (bus_id < 0) {
2328 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2329 return -1;
2330 }
2331 }
2332
2333 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2334 unit_id = strtol(buf, NULL, 0);
2335 if (unit_id < 0) {
2336 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2337 return -1;
2338 }
2339 }
2340
2341 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002342 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002343 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002344 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002345 max_devs = MAX_IDE_DEVS;
2346 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002347 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002348 max_devs = MAX_SCSI_DEVS;
2349 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002350 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002351 max_devs = 0;
2352 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002353 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002354 max_devs = 0;
2355 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002356 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002357 max_devs = 0;
2358 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002359 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002360 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002361 } else if (!strcmp(buf, "virtio")) {
2362 type = IF_VIRTIO;
2363 max_devs = 0;
2364 } else {
thse4bcb142007-12-02 04:51:10 +00002365 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2366 return -1;
2367 }
2368 }
2369
2370 if (get_param_value(buf, sizeof(buf), "index", str)) {
2371 index = strtol(buf, NULL, 0);
2372 if (index < 0) {
2373 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2374 return -1;
2375 }
2376 }
2377
2378 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2379 cyls = strtol(buf, NULL, 0);
2380 }
2381
2382 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2383 heads = strtol(buf, NULL, 0);
2384 }
2385
2386 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2387 secs = strtol(buf, NULL, 0);
2388 }
2389
2390 if (cyls || heads || secs) {
2391 if (cyls < 1 || cyls > 16383) {
2392 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2393 return -1;
2394 }
2395 if (heads < 1 || heads > 16) {
2396 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2397 return -1;
2398 }
2399 if (secs < 1 || secs > 63) {
2400 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2401 return -1;
2402 }
2403 }
2404
2405 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2406 if (!cyls) {
2407 fprintf(stderr,
2408 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2409 str);
2410 return -1;
2411 }
2412 if (!strcmp(buf, "none"))
2413 translation = BIOS_ATA_TRANSLATION_NONE;
2414 else if (!strcmp(buf, "lba"))
2415 translation = BIOS_ATA_TRANSLATION_LBA;
2416 else if (!strcmp(buf, "auto"))
2417 translation = BIOS_ATA_TRANSLATION_AUTO;
2418 else {
2419 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2420 return -1;
2421 }
2422 }
2423
2424 if (get_param_value(buf, sizeof(buf), "media", str)) {
2425 if (!strcmp(buf, "disk")) {
2426 media = MEDIA_DISK;
2427 } else if (!strcmp(buf, "cdrom")) {
2428 if (cyls || secs || heads) {
2429 fprintf(stderr,
2430 "qemu: '%s' invalid physical CHS format\n", str);
2431 return -1;
2432 }
2433 media = MEDIA_CDROM;
2434 } else {
2435 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2436 return -1;
2437 }
2438 }
2439
2440 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2441 if (!strcmp(buf, "on"))
2442 snapshot = 1;
2443 else if (!strcmp(buf, "off"))
2444 snapshot = 0;
2445 else {
2446 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2447 return -1;
2448 }
2449 }
2450
balrog33f00272007-12-24 14:33:24 +00002451 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002452 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002453 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002454 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002455 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002456 else if (!strcmp(buf, "writeback"))
2457 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002458 else {
2459 fprintf(stderr, "qemu: invalid cache option\n");
2460 return -1;
2461 }
2462 }
2463
aurel321e72d3b2008-04-28 20:26:45 +00002464 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002465 if (strcmp(buf, "?") == 0) {
2466 fprintf(stderr, "qemu: Supported formats:");
2467 bdrv_iterate_format(bdrv_format_print, NULL);
2468 fprintf(stderr, "\n");
2469 return -1;
2470 }
aurel321e72d3b2008-04-28 20:26:45 +00002471 drv = bdrv_find_format(buf);
2472 if (!drv) {
2473 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2474 return -1;
2475 }
2476 }
2477
balrog609497a2008-01-14 02:56:53 +00002478 if (arg->file == NULL)
2479 get_param_value(file, sizeof(file), "file", str);
2480 else
2481 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002482
aliguorifa879c62009-01-07 17:32:33 +00002483 if (!get_param_value(serial, sizeof(serial), "serial", str))
2484 memset(serial, 0, sizeof(serial));
2485
aliguoricdad4bd2009-02-28 16:51:01 +00002486 onerror = BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002487 if (get_param_value(buf, sizeof(serial), "werror", str)) {
aliguori869a5c62009-01-22 19:52:25 +00002488 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002489 fprintf(stderr, "werror is no supported by this format\n");
aliguori428c5702009-01-21 18:59:04 +00002490 return -1;
2491 }
2492 if (!strcmp(buf, "ignore"))
2493 onerror = BLOCK_ERR_IGNORE;
2494 else if (!strcmp(buf, "enospc"))
2495 onerror = BLOCK_ERR_STOP_ENOSPC;
2496 else if (!strcmp(buf, "stop"))
2497 onerror = BLOCK_ERR_STOP_ANY;
2498 else if (!strcmp(buf, "report"))
2499 onerror = BLOCK_ERR_REPORT;
2500 else {
2501 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2502 return -1;
2503 }
2504 }
2505
thse4bcb142007-12-02 04:51:10 +00002506 /* compute bus and unit according index */
2507
2508 if (index != -1) {
2509 if (bus_id != 0 || unit_id != -1) {
2510 fprintf(stderr,
2511 "qemu: '%s' index cannot be used with bus and unit\n", str);
2512 return -1;
2513 }
2514 if (max_devs == 0)
2515 {
2516 unit_id = index;
2517 bus_id = 0;
2518 } else {
2519 unit_id = index % max_devs;
2520 bus_id = index / max_devs;
2521 }
2522 }
2523
2524 /* if user doesn't specify a unit_id,
2525 * try to find the first free
2526 */
2527
2528 if (unit_id == -1) {
2529 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002530 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002531 unit_id++;
2532 if (max_devs && unit_id >= max_devs) {
2533 unit_id -= max_devs;
2534 bus_id++;
2535 }
2536 }
2537 }
2538
2539 /* check unit id */
2540
2541 if (max_devs && unit_id >= max_devs) {
2542 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2543 str, unit_id, max_devs - 1);
2544 return -1;
2545 }
2546
2547 /*
2548 * ignore multiple definitions
2549 */
2550
thsf60d39b2007-12-17 03:55:57 +00002551 if (drive_get_index(type, bus_id, unit_id) != -1)
aliguori4d73cd32009-02-11 15:20:46 +00002552 return -2;
thse4bcb142007-12-02 04:51:10 +00002553
2554 /* init */
2555
thsf60d39b2007-12-17 03:55:57 +00002556 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002557 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002558 if (max_devs)
2559 snprintf(buf, sizeof(buf), "%s%i%s%i",
2560 devname, bus_id, mediastr, unit_id);
2561 else
2562 snprintf(buf, sizeof(buf), "%s%s%i",
2563 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002564 bdrv = bdrv_new(buf);
aliguori7d5aca92009-02-11 15:19:58 +00002565 drives_table_idx = drive_get_free_idx();
2566 drives_table[drives_table_idx].bdrv = bdrv;
2567 drives_table[drives_table_idx].type = type;
2568 drives_table[drives_table_idx].bus = bus_id;
2569 drives_table[drives_table_idx].unit = unit_id;
2570 drives_table[drives_table_idx].onerror = onerror;
aliguorib01b1112009-02-11 15:20:20 +00002571 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
aliguorifa879c62009-01-07 17:32:33 +00002572 strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002573 nb_drives++;
2574
thsf60d39b2007-12-17 03:55:57 +00002575 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002576 case IF_IDE:
2577 case IF_SCSI:
2578 switch(media) {
2579 case MEDIA_DISK:
2580 if (cyls != 0) {
2581 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2582 bdrv_set_translation_hint(bdrv, translation);
2583 }
2584 break;
2585 case MEDIA_CDROM:
2586 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2587 break;
2588 }
2589 break;
2590 case IF_SD:
2591 /* FIXME: This isn't really a floppy, but it's a reasonable
2592 approximation. */
2593 case IF_FLOPPY:
2594 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2595 break;
2596 case IF_PFLASH:
2597 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002598 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002599 break;
2600 }
2601 if (!file[0])
aliguori4d73cd32009-02-11 15:20:46 +00002602 return -2;
balrog33f00272007-12-24 14:33:24 +00002603 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002604 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002605 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002606 cache = 2; /* always use write-back with snapshot */
2607 }
2608 if (cache == 0) /* no caching */
2609 bdrv_flags |= BDRV_O_NOCACHE;
2610 else if (cache == 2) /* write-back */
2611 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori4dc822d2008-12-04 21:39:21 +00002612 else if (cache == 3) /* not specified */
2613 bdrv_flags |= BDRV_O_CACHE_DEF;
aliguoric0f4ce72009-03-05 23:01:01 +00002614 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002615 fprintf(stderr, "qemu: could not open disk image %s\n",
2616 file);
2617 return -1;
2618 }
aliguoric0f4ce72009-03-05 23:01:01 +00002619 if (bdrv_key_required(bdrv))
2620 autostart = 0;
aliguori4d73cd32009-02-11 15:20:46 +00002621 return drives_table_idx;
thse4bcb142007-12-02 04:51:10 +00002622}
2623
bellard330d0412003-07-26 18:11:40 +00002624/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002625/* USB devices */
2626
pbrook0d92ed32006-05-21 16:30:15 +00002627static USBPort *used_usb_ports;
2628static USBPort *free_usb_ports;
2629
2630/* ??? Maybe change this to register a hub to keep track of the topology. */
2631void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2632 usb_attachfn attach)
2633{
2634 port->opaque = opaque;
2635 port->index = index;
2636 port->attach = attach;
2637 port->next = free_usb_ports;
2638 free_usb_ports = port;
2639}
2640
aliguori4b096fc2008-08-21 19:28:55 +00002641int usb_device_add_dev(USBDevice *dev)
2642{
2643 USBPort *port;
2644
2645 /* Find a USB port to add the device to. */
2646 port = free_usb_ports;
2647 if (!port->next) {
2648 USBDevice *hub;
2649
2650 /* Create a new hub and chain it on. */
2651 free_usb_ports = NULL;
2652 port->next = used_usb_ports;
2653 used_usb_ports = port;
2654
2655 hub = usb_hub_init(VM_USB_HUB_SIZE);
2656 usb_attach(port, hub);
2657 port = free_usb_ports;
2658 }
2659
2660 free_usb_ports = port->next;
2661 port->next = used_usb_ports;
2662 used_usb_ports = port;
2663 usb_attach(port, dev);
2664 return 0;
2665}
2666
aliguoribb5fc202009-03-05 23:01:15 +00002667static void usb_msd_password_cb(void *opaque, int err)
2668{
2669 USBDevice *dev = opaque;
2670
2671 if (!err)
2672 usb_device_add_dev(dev);
2673 else
2674 dev->handle_destroy(dev);
2675}
2676
aliguoric0f4ce72009-03-05 23:01:01 +00002677static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002678{
2679 const char *p;
2680 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002681
pbrook0d92ed32006-05-21 16:30:15 +00002682 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002683 return -1;
2684
2685 if (strstart(devname, "host:", &p)) {
2686 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002687 } else if (!strcmp(devname, "mouse")) {
2688 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002689 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002690 dev = usb_tablet_init();
2691 } else if (!strcmp(devname, "keyboard")) {
2692 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002693 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002694 BlockDriverState *bs;
2695
aliguoribb5fc202009-03-05 23:01:15 +00002696 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002697 if (!dev)
2698 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002699 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002700 if (bdrv_key_required(bs)) {
2701 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002702 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002703 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2704 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002705 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002706 }
2707 }
balrogf6d2a312007-06-10 19:21:04 +00002708 } else if (!strcmp(devname, "wacom-tablet")) {
2709 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002710 } else if (strstart(devname, "serial:", &p)) {
2711 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002712#ifdef CONFIG_BRLAPI
2713 } else if (!strcmp(devname, "braille")) {
2714 dev = usb_baum_init();
2715#endif
balrog6c9f8862008-07-17 20:47:13 +00002716 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002717 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002718
balrog9ad97e62008-07-29 13:16:31 +00002719 if (net_client_init("nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002720 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002721 nd_table[nic].model = "usb";
2722 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002723 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2724 dev = usb_bt_init(devname[2] ? hci_init(p) :
2725 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002726 } else {
2727 return -1;
2728 }
pbrook0d92ed32006-05-21 16:30:15 +00002729 if (!dev)
2730 return -1;
2731
aliguori4b096fc2008-08-21 19:28:55 +00002732 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002733}
2734
aliguori1f3870a2008-08-21 19:27:48 +00002735int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002736{
pbrook0d92ed32006-05-21 16:30:15 +00002737 USBPort *port;
2738 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002739 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002740
pbrook0d92ed32006-05-21 16:30:15 +00002741 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002742 return -1;
2743
bellarda594cfb2005-11-06 16:13:29 +00002744 if (bus_num != 0)
2745 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002746
2747 lastp = &used_usb_ports;
2748 port = used_usb_ports;
2749 while (port && port->dev->addr != addr) {
2750 lastp = &port->next;
2751 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002752 }
pbrook0d92ed32006-05-21 16:30:15 +00002753
2754 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002755 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002756
bellard059809e2006-07-19 18:06:15 +00002757 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002758 *lastp = port->next;
2759 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002760 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002761 port->next = free_usb_ports;
2762 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002763 return 0;
2764}
2765
aliguori1f3870a2008-08-21 19:27:48 +00002766static int usb_device_del(const char *devname)
2767{
2768 int bus_num, addr;
2769 const char *p;
2770
aliguori5d0c5752008-09-14 01:07:41 +00002771 if (strstart(devname, "host:", &p))
2772 return usb_host_device_close(p);
2773
aliguori1f3870a2008-08-21 19:27:48 +00002774 if (!used_usb_ports)
2775 return -1;
2776
2777 p = strchr(devname, '.');
2778 if (!p)
2779 return -1;
2780 bus_num = strtoul(devname, NULL, 0);
2781 addr = strtoul(p + 1, NULL, 0);
2782
2783 return usb_device_del_addr(bus_num, addr);
2784}
2785
aliguori376253e2009-03-05 23:01:23 +00002786void do_usb_add(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002787{
aliguoric0f4ce72009-03-05 23:01:01 +00002788 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002789}
2790
aliguori376253e2009-03-05 23:01:23 +00002791void do_usb_del(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002792{
aliguori4b096fc2008-08-21 19:28:55 +00002793 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002794}
2795
aliguori376253e2009-03-05 23:01:23 +00002796void usb_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00002797{
2798 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002799 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002800 const char *speed_str;
2801
pbrook0d92ed32006-05-21 16:30:15 +00002802 if (!usb_enabled) {
aliguori376253e2009-03-05 23:01:23 +00002803 monitor_printf(mon, "USB support not enabled\n");
bellarda594cfb2005-11-06 16:13:29 +00002804 return;
2805 }
2806
pbrook0d92ed32006-05-21 16:30:15 +00002807 for (port = used_usb_ports; port; port = port->next) {
2808 dev = port->dev;
2809 if (!dev)
2810 continue;
2811 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002812 case USB_SPEED_LOW:
2813 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002814 break;
ths5fafdf22007-09-16 21:08:06 +00002815 case USB_SPEED_FULL:
2816 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002817 break;
ths5fafdf22007-09-16 21:08:06 +00002818 case USB_SPEED_HIGH:
2819 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002820 break;
2821 default:
ths5fafdf22007-09-16 21:08:06 +00002822 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002823 break;
bellarda594cfb2005-11-06 16:13:29 +00002824 }
aliguori376253e2009-03-05 23:01:23 +00002825 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2826 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002827 }
2828}
2829
bellardf7cce892004-12-08 22:21:25 +00002830/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002831/* PCMCIA/Cardbus */
2832
2833static struct pcmcia_socket_entry_s {
2834 struct pcmcia_socket_s *socket;
2835 struct pcmcia_socket_entry_s *next;
2836} *pcmcia_sockets = 0;
2837
2838void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2839{
2840 struct pcmcia_socket_entry_s *entry;
2841
2842 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2843 entry->socket = socket;
2844 entry->next = pcmcia_sockets;
2845 pcmcia_sockets = entry;
2846}
2847
2848void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2849{
2850 struct pcmcia_socket_entry_s *entry, **ptr;
2851
2852 ptr = &pcmcia_sockets;
2853 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2854 if (entry->socket == socket) {
2855 *ptr = entry->next;
2856 qemu_free(entry);
2857 }
2858}
2859
aliguori376253e2009-03-05 23:01:23 +00002860void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002861{
2862 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002863
balrog201a51f2007-04-30 00:51:09 +00002864 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002865 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002866
2867 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002868 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2869 iter->socket->attached ? iter->socket->card_string :
2870 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002871}
2872
2873/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002874/* register display */
2875
2876void register_displaystate(DisplayState *ds)
2877{
2878 DisplayState **s;
2879 s = &display_state;
2880 while (*s != NULL)
2881 s = &(*s)->next;
2882 ds->next = NULL;
2883 *s = ds;
2884}
2885
2886DisplayState *get_displaystate(void)
2887{
2888 return display_state;
2889}
2890
ths2ff89792007-06-21 23:34:19 +00002891/* dumb display */
2892
aliguori8f391ab2009-01-19 16:34:10 +00002893static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002894{
aliguori8f391ab2009-01-19 16:34:10 +00002895 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7da03b12009-01-21 18:58:51 +00002896 ds->surface = qemu_create_displaysurface(640, 480, 32, 640 * 4);
aliguori8f391ab2009-01-19 16:34:10 +00002897 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002898}
2899
2900/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002901/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002902
bellardc4b1fcc2004-03-14 21:44:30 +00002903#define MAX_IO_HANDLERS 64
2904
2905typedef struct IOHandlerRecord {
2906 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002907 IOCanRWHandler *fd_read_poll;
2908 IOHandler *fd_read;
2909 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002910 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002911 void *opaque;
2912 /* temporary data */
2913 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002914 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002915} IOHandlerRecord;
2916
bellard8a7ddc32004-03-31 19:00:16 +00002917static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002918
bellard7c9d8e02005-11-15 22:16:05 +00002919/* XXX: fd_read_poll should be suppressed, but an API change is
2920 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002921int qemu_set_fd_handler2(int fd,
2922 IOCanRWHandler *fd_read_poll,
2923 IOHandler *fd_read,
2924 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002925 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002926{
bellard8a7ddc32004-03-31 19:00:16 +00002927 IOHandlerRecord **pioh, *ioh;
2928
bellard7c9d8e02005-11-15 22:16:05 +00002929 if (!fd_read && !fd_write) {
2930 pioh = &first_io_handler;
2931 for(;;) {
2932 ioh = *pioh;
2933 if (ioh == NULL)
2934 break;
2935 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002936 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002937 break;
2938 }
2939 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002940 }
bellard7c9d8e02005-11-15 22:16:05 +00002941 } else {
2942 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2943 if (ioh->fd == fd)
2944 goto found;
2945 }
2946 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002947 ioh->next = first_io_handler;
2948 first_io_handler = ioh;
2949 found:
2950 ioh->fd = fd;
2951 ioh->fd_read_poll = fd_read_poll;
2952 ioh->fd_read = fd_read;
2953 ioh->fd_write = fd_write;
2954 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002955 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002956 }
bellard7c9d8e02005-11-15 22:16:05 +00002957 return 0;
2958}
2959
ths5fafdf22007-09-16 21:08:06 +00002960int qemu_set_fd_handler(int fd,
2961 IOHandler *fd_read,
2962 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002963 void *opaque)
2964{
2965 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002966}
2967
aliguori56f3a5d2008-10-31 18:07:17 +00002968#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002969/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002970/* Polling handling */
2971
2972typedef struct PollingEntry {
2973 PollingFunc *func;
2974 void *opaque;
2975 struct PollingEntry *next;
2976} PollingEntry;
2977
2978static PollingEntry *first_polling_entry;
2979
2980int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2981{
2982 PollingEntry **ppe, *pe;
2983 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002984 pe->func = func;
2985 pe->opaque = opaque;
2986 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2987 *ppe = pe;
2988 return 0;
2989}
2990
2991void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2992{
2993 PollingEntry **ppe, *pe;
2994 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2995 pe = *ppe;
2996 if (pe->func == func && pe->opaque == opaque) {
2997 *ppe = pe->next;
2998 qemu_free(pe);
2999 break;
3000 }
3001 }
3002}
3003
bellarda18e5242006-06-25 17:18:27 +00003004/***********************************************************/
3005/* Wait objects support */
3006typedef struct WaitObjects {
3007 int num;
3008 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
3009 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
3010 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
3011} WaitObjects;
3012
3013static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00003014
bellarda18e5242006-06-25 17:18:27 +00003015int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3016{
3017 WaitObjects *w = &wait_objects;
3018
3019 if (w->num >= MAXIMUM_WAIT_OBJECTS)
3020 return -1;
3021 w->events[w->num] = handle;
3022 w->func[w->num] = func;
3023 w->opaque[w->num] = opaque;
3024 w->num++;
3025 return 0;
3026}
3027
3028void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3029{
3030 int i, found;
3031 WaitObjects *w = &wait_objects;
3032
3033 found = 0;
3034 for (i = 0; i < w->num; i++) {
3035 if (w->events[i] == handle)
3036 found = 1;
3037 if (found) {
3038 w->events[i] = w->events[i + 1];
3039 w->func[i] = w->func[i + 1];
3040 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00003041 }
bellarda18e5242006-06-25 17:18:27 +00003042 }
3043 if (found)
3044 w->num--;
3045}
3046#endif
3047
bellard8a7ddc32004-03-31 19:00:16 +00003048/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003049/* ram save/restore */
3050
bellard8a7ddc32004-03-31 19:00:16 +00003051static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3052{
3053 int v;
3054
3055 v = qemu_get_byte(f);
3056 switch(v) {
3057 case 0:
3058 if (qemu_get_buffer(f, buf, len) != len)
3059 return -EIO;
3060 break;
3061 case 1:
3062 v = qemu_get_byte(f);
3063 memset(buf, v, len);
3064 break;
3065 default:
3066 return -EINVAL;
3067 }
aliguori871d2f02008-10-13 03:07:56 +00003068
3069 if (qemu_file_has_error(f))
3070 return -EIO;
3071
bellard8a7ddc32004-03-31 19:00:16 +00003072 return 0;
3073}
3074
bellardc88676f2006-08-06 13:36:11 +00003075static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003076{
aurel3200f82b82008-04-27 21:12:55 +00003077 int ret;
3078 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00003079
bellard8a7ddc32004-03-31 19:00:16 +00003080 if (qemu_get_be32(f) != phys_ram_size)
3081 return -EINVAL;
3082 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
3083 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
3084 if (ret)
3085 return ret;
3086 }
3087 return 0;
3088}
3089
bellardc88676f2006-08-06 13:36:11 +00003090#define BDRV_HASH_BLOCK_SIZE 1024
3091#define IOBUF_SIZE 4096
3092#define RAM_CBLOCK_MAGIC 0xfabe
3093
bellardc88676f2006-08-06 13:36:11 +00003094typedef struct RamDecompressState {
3095 z_stream zstream;
3096 QEMUFile *f;
3097 uint8_t buf[IOBUF_SIZE];
3098} RamDecompressState;
3099
3100static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3101{
3102 int ret;
3103 memset(s, 0, sizeof(*s));
3104 s->f = f;
3105 ret = inflateInit(&s->zstream);
3106 if (ret != Z_OK)
3107 return -1;
3108 return 0;
3109}
3110
3111static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3112{
3113 int ret, clen;
3114
3115 s->zstream.avail_out = len;
3116 s->zstream.next_out = buf;
3117 while (s->zstream.avail_out > 0) {
3118 if (s->zstream.avail_in == 0) {
3119 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3120 return -1;
3121 clen = qemu_get_be16(s->f);
3122 if (clen > IOBUF_SIZE)
3123 return -1;
3124 qemu_get_buffer(s->f, s->buf, clen);
3125 s->zstream.avail_in = clen;
3126 s->zstream.next_in = s->buf;
3127 }
3128 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3129 if (ret != Z_OK && ret != Z_STREAM_END) {
3130 return -1;
3131 }
3132 }
3133 return 0;
3134}
3135
3136static void ram_decompress_close(RamDecompressState *s)
3137{
3138 inflateEnd(&s->zstream);
3139}
3140
aliguori475e4272008-10-06 20:21:51 +00003141#define RAM_SAVE_FLAG_FULL 0x01
3142#define RAM_SAVE_FLAG_COMPRESS 0x02
3143#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3144#define RAM_SAVE_FLAG_PAGE 0x08
3145#define RAM_SAVE_FLAG_EOS 0x10
3146
3147static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003148{
aliguori475e4272008-10-06 20:21:51 +00003149 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3150 uint32_t *array = (uint32_t *)page;
3151 int i;
ths3b46e622007-09-17 08:09:54 +00003152
aliguori475e4272008-10-06 20:21:51 +00003153 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3154 if (array[i] != val)
3155 return 0;
bellardc88676f2006-08-06 13:36:11 +00003156 }
aliguori475e4272008-10-06 20:21:51 +00003157
3158 return 1;
bellardc88676f2006-08-06 13:36:11 +00003159}
3160
aliguori475e4272008-10-06 20:21:51 +00003161static int ram_save_block(QEMUFile *f)
3162{
3163 static ram_addr_t current_addr = 0;
3164 ram_addr_t saved_addr = current_addr;
3165 ram_addr_t addr = 0;
3166 int found = 0;
3167
3168 while (addr < phys_ram_size) {
3169 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3170 uint8_t ch;
3171
3172 cpu_physical_memory_reset_dirty(current_addr,
3173 current_addr + TARGET_PAGE_SIZE,
3174 MIGRATION_DIRTY_FLAG);
3175
3176 ch = *(phys_ram_base + current_addr);
3177
3178 if (is_dup_page(phys_ram_base + current_addr, ch)) {
3179 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3180 qemu_put_byte(f, ch);
3181 } else {
3182 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3183 qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
3184 }
3185
3186 found = 1;
3187 break;
3188 }
3189 addr += TARGET_PAGE_SIZE;
3190 current_addr = (saved_addr + addr) % phys_ram_size;
3191 }
3192
3193 return found;
3194}
3195
3196static ram_addr_t ram_save_threshold = 10;
3197
3198static ram_addr_t ram_save_remaining(void)
3199{
3200 ram_addr_t addr;
3201 ram_addr_t count = 0;
3202
3203 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3204 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3205 count++;
3206 }
3207
3208 return count;
3209}
3210
3211static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3212{
3213 ram_addr_t addr;
3214
3215 if (stage == 1) {
3216 /* Make sure all dirty bits are set */
3217 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3218 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3219 cpu_physical_memory_set_dirty(addr);
3220 }
3221
3222 /* Enable dirty memory tracking */
3223 cpu_physical_memory_set_dirty_tracking(1);
3224
3225 qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
3226 }
3227
3228 while (!qemu_file_rate_limit(f)) {
3229 int ret;
3230
3231 ret = ram_save_block(f);
3232 if (ret == 0) /* no more blocks */
3233 break;
3234 }
3235
3236 /* try transferring iterative blocks of memory */
3237
3238 if (stage == 3) {
3239 cpu_physical_memory_set_dirty_tracking(0);
3240
3241 /* flush all remaining blocks regardless of rate limiting */
3242 while (ram_save_block(f) != 0);
3243 }
3244
3245 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3246
3247 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3248}
3249
3250static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003251{
3252 RamDecompressState s1, *s = &s1;
3253 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003254 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003255
bellardc88676f2006-08-06 13:36:11 +00003256 if (ram_decompress_open(s, f) < 0)
3257 return -EINVAL;
3258 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
3259 if (ram_decompress_buf(s, buf, 1) < 0) {
3260 fprintf(stderr, "Error while reading ram block header\n");
3261 goto error;
3262 }
3263 if (buf[0] == 0) {
3264 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003265 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003266 goto error;
3267 }
aliguori475e4272008-10-06 20:21:51 +00003268 } else {
bellardc88676f2006-08-06 13:36:11 +00003269 error:
3270 printf("Error block header\n");
3271 return -EINVAL;
3272 }
3273 }
3274 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003275
3276 return 0;
3277}
3278
3279static int ram_load(QEMUFile *f, void *opaque, int version_id)
3280{
3281 ram_addr_t addr;
3282 int flags;
3283
3284 if (version_id == 1)
3285 return ram_load_v1(f, opaque);
3286
3287 if (version_id == 2) {
3288 if (qemu_get_be32(f) != phys_ram_size)
3289 return -EINVAL;
3290 return ram_load_dead(f, opaque);
3291 }
3292
3293 if (version_id != 3)
3294 return -EINVAL;
3295
3296 do {
3297 addr = qemu_get_be64(f);
3298
3299 flags = addr & ~TARGET_PAGE_MASK;
3300 addr &= TARGET_PAGE_MASK;
3301
3302 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3303 if (addr != phys_ram_size)
3304 return -EINVAL;
3305 }
3306
3307 if (flags & RAM_SAVE_FLAG_FULL) {
3308 if (ram_load_dead(f, opaque) < 0)
3309 return -EINVAL;
3310 }
3311
3312 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3313 uint8_t ch = qemu_get_byte(f);
3314 memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
3315 } else if (flags & RAM_SAVE_FLAG_PAGE)
3316 qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
3317 } while (!(flags & RAM_SAVE_FLAG_EOS));
3318
bellardc88676f2006-08-06 13:36:11 +00003319 return 0;
3320}
3321
aliguori9e472e12008-10-08 19:50:24 +00003322void qemu_service_io(void)
3323{
3324 CPUState *env = cpu_single_env;
3325 if (env) {
aurel323098dba2009-03-07 21:28:24 +00003326 cpu_exit(env);
aliguori9e472e12008-10-08 19:50:24 +00003327#ifdef USE_KQEMU
3328 if (env->kqemu_enabled) {
3329 kqemu_cpu_interrupt(env);
3330 }
3331#endif
3332 }
3333}
3334
bellard8a7ddc32004-03-31 19:00:16 +00003335/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003336/* bottom halves (can be seen as timers which expire ASAP) */
3337
3338struct QEMUBH {
3339 QEMUBHFunc *cb;
3340 void *opaque;
3341 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003342 int idle;
3343 int deleted;
bellard83f64092006-08-01 16:21:11 +00003344 QEMUBH *next;
3345};
3346
3347static QEMUBH *first_bh = NULL;
3348
3349QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3350{
3351 QEMUBH *bh;
3352 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003353 bh->cb = cb;
3354 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003355 bh->next = first_bh;
3356 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003357 return bh;
3358}
3359
bellard6eb57332006-08-06 09:51:25 +00003360int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003361{
aliguori1b435b12008-10-31 17:24:21 +00003362 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003363 int ret;
bellard83f64092006-08-01 16:21:11 +00003364
bellard6eb57332006-08-06 09:51:25 +00003365 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003366 for (bh = first_bh; bh; bh = bh->next) {
3367 if (!bh->deleted && bh->scheduled) {
3368 bh->scheduled = 0;
3369 if (!bh->idle)
3370 ret = 1;
3371 bh->idle = 0;
3372 bh->cb(bh->opaque);
3373 }
bellard83f64092006-08-01 16:21:11 +00003374 }
aliguori1b435b12008-10-31 17:24:21 +00003375
3376 /* remove deleted bhs */
3377 bhp = &first_bh;
3378 while (*bhp) {
3379 bh = *bhp;
3380 if (bh->deleted) {
3381 *bhp = bh->next;
3382 qemu_free(bh);
3383 } else
3384 bhp = &bh->next;
3385 }
3386
bellard6eb57332006-08-06 09:51:25 +00003387 return ret;
bellard83f64092006-08-01 16:21:11 +00003388}
3389
aliguori1b435b12008-10-31 17:24:21 +00003390void qemu_bh_schedule_idle(QEMUBH *bh)
3391{
3392 if (bh->scheduled)
3393 return;
3394 bh->scheduled = 1;
3395 bh->idle = 1;
3396}
3397
bellard83f64092006-08-01 16:21:11 +00003398void qemu_bh_schedule(QEMUBH *bh)
3399{
3400 CPUState *env = cpu_single_env;
3401 if (bh->scheduled)
3402 return;
3403 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003404 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003405 /* stop the currently executing CPU to execute the BH ASAP */
3406 if (env) {
aurel323098dba2009-03-07 21:28:24 +00003407 cpu_exit(env);
bellard83f64092006-08-01 16:21:11 +00003408 }
3409}
3410
3411void qemu_bh_cancel(QEMUBH *bh)
3412{
aliguori1b435b12008-10-31 17:24:21 +00003413 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003414}
3415
3416void qemu_bh_delete(QEMUBH *bh)
3417{
aliguori1b435b12008-10-31 17:24:21 +00003418 bh->scheduled = 0;
3419 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003420}
3421
aliguori56f3a5d2008-10-31 18:07:17 +00003422static void qemu_bh_update_timeout(int *timeout)
3423{
3424 QEMUBH *bh;
3425
3426 for (bh = first_bh; bh; bh = bh->next) {
3427 if (!bh->deleted && bh->scheduled) {
3428 if (bh->idle) {
3429 /* idle bottom halves will be polled at least
3430 * every 10ms */
3431 *timeout = MIN(10, *timeout);
3432 } else {
3433 /* non-idle bottom halves will be executed
3434 * immediately */
3435 *timeout = 0;
3436 break;
3437 }
3438 }
3439 }
3440}
3441
bellard83f64092006-08-01 16:21:11 +00003442/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003443/* machine registration */
3444
blueswir1bdaf78e2008-10-04 07:24:27 +00003445static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003446QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003447
3448int qemu_register_machine(QEMUMachine *m)
3449{
3450 QEMUMachine **pm;
3451 pm = &first_machine;
3452 while (*pm != NULL)
3453 pm = &(*pm)->next;
3454 m->next = NULL;
3455 *pm = m;
3456 return 0;
3457}
3458
pbrook9596ebb2007-11-18 01:44:38 +00003459static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003460{
3461 QEMUMachine *m;
3462
3463 for(m = first_machine; m != NULL; m = m->next) {
3464 if (!strcmp(m->name, name))
3465 return m;
3466 }
3467 return NULL;
3468}
3469
3470/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003471/* main execution loop */
3472
pbrook9596ebb2007-11-18 01:44:38 +00003473static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003474{
aliguori7d957bd2009-01-15 22:14:11 +00003475 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003476 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003477 DisplayChangeListener *dcl = ds->listeners;
3478
3479 dpy_refresh(ds);
3480
3481 while (dcl != NULL) {
3482 if (dcl->gui_timer_interval &&
3483 dcl->gui_timer_interval < interval)
3484 interval = dcl->gui_timer_interval;
3485 dcl = dcl->next;
3486 }
3487 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003488}
3489
blueswir19043b622009-01-21 19:28:13 +00003490static void nographic_update(void *opaque)
3491{
3492 uint64_t interval = GUI_REFRESH_INTERVAL;
3493
3494 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3495}
3496
bellard0bd48852005-11-11 00:00:47 +00003497struct vm_change_state_entry {
3498 VMChangeStateHandler *cb;
3499 void *opaque;
3500 LIST_ENTRY (vm_change_state_entry) entries;
3501};
3502
3503static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3504
3505VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3506 void *opaque)
3507{
3508 VMChangeStateEntry *e;
3509
3510 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003511
3512 e->cb = cb;
3513 e->opaque = opaque;
3514 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3515 return e;
3516}
3517
3518void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3519{
3520 LIST_REMOVE (e, entries);
3521 qemu_free (e);
3522}
3523
aliguori9781e042009-01-22 17:15:29 +00003524static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003525{
3526 VMChangeStateEntry *e;
3527
3528 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003529 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003530 }
3531}
3532
bellard8a7ddc32004-03-31 19:00:16 +00003533void vm_start(void)
3534{
3535 if (!vm_running) {
3536 cpu_enable_ticks();
3537 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003538 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003539 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00003540 }
3541}
3542
ths5fafdf22007-09-16 21:08:06 +00003543void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00003544{
3545 if (vm_running) {
3546 cpu_disable_ticks();
3547 vm_running = 0;
aliguori9781e042009-01-22 17:15:29 +00003548 vm_state_notify(0, reason);
bellard8a7ddc32004-03-31 19:00:16 +00003549 }
3550}
3551
bellardbb0c6722004-06-20 12:37:32 +00003552/* reset/shutdown handler */
3553
3554typedef struct QEMUResetEntry {
3555 QEMUResetHandler *func;
3556 void *opaque;
3557 struct QEMUResetEntry *next;
3558} QEMUResetEntry;
3559
3560static QEMUResetEntry *first_reset_entry;
3561static int reset_requested;
3562static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003563static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00003564
aurel32cf7a2fe2008-03-18 06:53:05 +00003565int qemu_shutdown_requested(void)
3566{
3567 int r = shutdown_requested;
3568 shutdown_requested = 0;
3569 return r;
3570}
3571
3572int qemu_reset_requested(void)
3573{
3574 int r = reset_requested;
3575 reset_requested = 0;
3576 return r;
3577}
3578
3579int qemu_powerdown_requested(void)
3580{
3581 int r = powerdown_requested;
3582 powerdown_requested = 0;
3583 return r;
3584}
3585
bellardbb0c6722004-06-20 12:37:32 +00003586void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3587{
3588 QEMUResetEntry **pre, *re;
3589
3590 pre = &first_reset_entry;
3591 while (*pre != NULL)
3592 pre = &(*pre)->next;
3593 re = qemu_mallocz(sizeof(QEMUResetEntry));
3594 re->func = func;
3595 re->opaque = opaque;
3596 re->next = NULL;
3597 *pre = re;
3598}
3599
aurel32cf7a2fe2008-03-18 06:53:05 +00003600void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00003601{
3602 QEMUResetEntry *re;
3603
3604 /* reset all devices */
3605 for(re = first_reset_entry; re != NULL; re = re->next) {
3606 re->func(re->opaque);
3607 }
3608}
3609
3610void qemu_system_reset_request(void)
3611{
bellardd1beab82006-10-02 19:44:22 +00003612 if (no_reboot) {
3613 shutdown_requested = 1;
3614 } else {
3615 reset_requested = 1;
3616 }
bellard6a00d602005-11-21 23:25:50 +00003617 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003618 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003619}
3620
3621void qemu_system_shutdown_request(void)
3622{
3623 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003624 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003625 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003626}
3627
bellard34751872005-07-02 14:31:34 +00003628void qemu_system_powerdown_request(void)
3629{
3630 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003631 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003632 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003633}
3634
ths877cf882007-04-18 18:11:47 +00003635#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003636static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003637{
3638 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003639 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003640
bellardf3311102006-04-12 20:21:17 +00003641
3642 /* XXX: need to suppress polling by better using win32 events */
3643 ret = 0;
3644 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3645 ret |= pe->func(pe->opaque);
3646 }
thse6b1e552007-04-18 17:56:02 +00003647 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003648 int err;
3649 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003650
aliguori56f3a5d2008-10-31 18:07:17 +00003651 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003652 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3653 if (w->func[ret - WAIT_OBJECT_0])
3654 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003655
ths5fafdf22007-09-16 21:08:06 +00003656 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003657 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003658
thse6b1e552007-04-18 17:56:02 +00003659 /* Check if event is signaled */
3660 ret2 = WaitForSingleObject(w->events[i], 0);
3661 if(ret2 == WAIT_OBJECT_0) {
3662 if (w->func[i])
3663 w->func[i](w->opaque[i]);
3664 } else if (ret2 == WAIT_TIMEOUT) {
3665 } else {
3666 err = GetLastError();
3667 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003668 }
3669 }
bellarda18e5242006-06-25 17:18:27 +00003670 } else if (ret == WAIT_TIMEOUT) {
3671 } else {
3672 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003673 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003674 }
bellardf3311102006-04-12 20:21:17 +00003675 }
aliguori56f3a5d2008-10-31 18:07:17 +00003676
3677 *timeout = 0;
3678}
3679#else
blueswir169d64512008-12-07 19:30:18 +00003680static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003681{
3682}
bellardfd1dff42006-02-01 21:29:26 +00003683#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003684
3685void main_loop_wait(int timeout)
3686{
3687 IOHandlerRecord *ioh;
3688 fd_set rfds, wfds, xfds;
3689 int ret, nfds;
3690 struct timeval tv;
3691
3692 qemu_bh_update_timeout(&timeout);
3693
3694 host_main_loop_wait(&timeout);
3695
bellardfd1dff42006-02-01 21:29:26 +00003696 /* poll any events */
3697 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003698 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003699 FD_ZERO(&rfds);
3700 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003701 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003702 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003703 if (ioh->deleted)
3704 continue;
bellardfd1dff42006-02-01 21:29:26 +00003705 if (ioh->fd_read &&
3706 (!ioh->fd_read_poll ||
3707 ioh->fd_read_poll(ioh->opaque) != 0)) {
3708 FD_SET(ioh->fd, &rfds);
3709 if (ioh->fd > nfds)
3710 nfds = ioh->fd;
3711 }
3712 if (ioh->fd_write) {
3713 FD_SET(ioh->fd, &wfds);
3714 if (ioh->fd > nfds)
3715 nfds = ioh->fd;
3716 }
3717 }
ths3b46e622007-09-17 08:09:54 +00003718
aliguori56f3a5d2008-10-31 18:07:17 +00003719 tv.tv_sec = timeout / 1000;
3720 tv.tv_usec = (timeout % 1000) * 1000;
3721
bellarde0356492006-05-01 13:33:02 +00003722#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003723 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003724 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3725 }
3726#endif
3727 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00003728 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003729 IOHandlerRecord **pioh;
3730
3731 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003732 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003733 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003734 }
ths6ab43fd2007-08-25 01:34:19 +00003735 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003736 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003737 }
3738 }
thscafffd42007-02-28 21:59:44 +00003739
3740 /* remove deleted IO handlers */
3741 pioh = &first_io_handler;
3742 while (*pioh) {
3743 ioh = *pioh;
3744 if (ioh->deleted) {
3745 *pioh = ioh->next;
3746 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003747 } else
thscafffd42007-02-28 21:59:44 +00003748 pioh = &ioh->next;
3749 }
bellardfd1dff42006-02-01 21:29:26 +00003750 }
bellarde0356492006-05-01 13:33:02 +00003751#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003752 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003753 if (ret < 0) {
3754 FD_ZERO(&rfds);
3755 FD_ZERO(&wfds);
3756 FD_ZERO(&xfds);
3757 }
3758 slirp_select_poll(&rfds, &wfds, &xfds);
3759 }
3760#endif
bellardc20709a2004-04-21 23:27:19 +00003761
aliguori357c6922008-11-25 17:26:09 +00003762 /* vm time timers */
3763 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3764 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3765 qemu_get_clock(vm_clock));
3766
3767 /* real time timers */
3768 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3769 qemu_get_clock(rt_clock));
3770
pbrook423f0742007-05-23 00:06:54 +00003771 /* Check bottom-halves last in case any of the earlier events triggered
3772 them. */
3773 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003774
bellard5905b2e2004-08-01 21:53:26 +00003775}
3776
pbrook9596ebb2007-11-18 01:44:38 +00003777static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00003778{
3779 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00003780#ifdef CONFIG_PROFILER
3781 int64_t ti;
3782#endif
bellard6a00d602005-11-21 23:25:50 +00003783 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003784
bellard6a00d602005-11-21 23:25:50 +00003785 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00003786 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00003787 for(;;) {
3788 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00003789
bellard15a76442005-11-23 21:01:03 +00003790 for(;;) {
3791 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00003792 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00003793#ifdef CONFIG_PROFILER
3794 ti = profile_getclock();
3795#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003796 if (use_icount) {
3797 int64_t count;
3798 int decr;
3799 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3800 env->icount_decr.u16.low = 0;
3801 env->icount_extra = 0;
3802 count = qemu_next_deadline();
3803 count = (count + (1 << icount_time_shift) - 1)
3804 >> icount_time_shift;
3805 qemu_icount += count;
3806 decr = (count > 0xffff) ? 0xffff : count;
3807 count -= decr;
3808 env->icount_decr.u16.low = decr;
3809 env->icount_extra = count;
3810 }
bellard6a00d602005-11-21 23:25:50 +00003811 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00003812#ifdef CONFIG_PROFILER
3813 qemu_time += profile_getclock() - ti;
3814#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003815 if (use_icount) {
3816 /* Fold pending instructions back into the
3817 instruction counter, and clear the interrupt flag. */
3818 qemu_icount -= (env->icount_decr.u16.low
3819 + env->icount_extra);
3820 env->icount_decr.u32 = 0;
3821 env->icount_extra = 0;
3822 }
balrogee5605e2007-12-03 03:01:40 +00003823 next_cpu = env->next_cpu ?: first_cpu;
aurel3295b01002008-04-04 17:16:35 +00003824 if (event_pending && likely(ret != EXCP_DEBUG)) {
balrogee5605e2007-12-03 03:01:40 +00003825 ret = EXCP_INTERRUPT;
3826 event_pending = 0;
3827 break;
3828 }
pbrookbd967e02007-03-11 18:54:57 +00003829 if (ret == EXCP_HLT) {
3830 /* Give the next CPU a chance to run. */
3831 cur_cpu = env;
3832 continue;
3833 }
bellard15a76442005-11-23 21:01:03 +00003834 if (ret != EXCP_HALTED)
3835 break;
3836 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00003837 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00003838 break;
bellard15a76442005-11-23 21:01:03 +00003839 }
3840 cur_cpu = env;
3841
bellard5905b2e2004-08-01 21:53:26 +00003842 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00003843 ret = EXCP_INTERRUPT;
aurel32b2f76162008-04-11 21:35:52 +00003844 if (no_shutdown) {
3845 vm_stop(0);
3846 no_shutdown = 0;
3847 }
3848 else
3849 break;
bellard5905b2e2004-08-01 21:53:26 +00003850 }
3851 if (reset_requested) {
3852 reset_requested = 0;
3853 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00003854 ret = EXCP_INTERRUPT;
3855 }
3856 if (powerdown_requested) {
3857 powerdown_requested = 0;
3858 qemu_system_powerdown();
3859 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00003860 }
aurel3295b01002008-04-04 17:16:35 +00003861 if (unlikely(ret == EXCP_DEBUG)) {
aliguori880a7572008-11-18 20:30:24 +00003862 gdb_set_stop_cpu(cur_cpu);
bellard5905b2e2004-08-01 21:53:26 +00003863 vm_stop(EXCP_DEBUG);
3864 }
pbrookbd967e02007-03-11 18:54:57 +00003865 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00003866 /* XXX: use timeout computed from timers */
pbrook2e70f6e2008-06-29 01:03:05 +00003867 if (ret == EXCP_HALTED) {
3868 if (use_icount) {
3869 int64_t add;
3870 int64_t delta;
3871 /* Advance virtual time to the next event. */
3872 if (use_icount == 1) {
3873 /* When not using an adaptive execution frequency
3874 we tend to get badly out of sync with real time,
thsbf20dc02008-06-30 17:22:19 +00003875 so just delay for a reasonable amount of time. */
pbrook2e70f6e2008-06-29 01:03:05 +00003876 delta = 0;
3877 } else {
3878 delta = cpu_get_icount() - cpu_get_clock();
3879 }
3880 if (delta > 0) {
3881 /* If virtual time is ahead of real time then just
3882 wait for IO. */
3883 timeout = (delta / 1000000) + 1;
3884 } else {
3885 /* Wait for either IO to occur or the next
3886 timer event. */
3887 add = qemu_next_deadline();
3888 /* We advance the timer before checking for IO.
3889 Limit the amount we advance so that early IO
3890 activity won't get the guest too far ahead. */
3891 if (add > 10000000)
3892 add = 10000000;
3893 delta += add;
3894 add = (add + (1 << icount_time_shift) - 1)
3895 >> icount_time_shift;
3896 qemu_icount += add;
3897 timeout = delta / 1000000;
3898 if (timeout < 0)
3899 timeout = 0;
3900 }
3901 } else {
aliguori0a1af392008-10-31 18:40:25 +00003902 timeout = 5000;
pbrook2e70f6e2008-06-29 01:03:05 +00003903 }
3904 } else {
bellard5905b2e2004-08-01 21:53:26 +00003905 timeout = 0;
pbrook2e70f6e2008-06-29 01:03:05 +00003906 }
bellard5905b2e2004-08-01 21:53:26 +00003907 } else {
blueswir198448f52008-09-30 18:16:09 +00003908 if (shutdown_requested) {
3909 ret = EXCP_INTERRUPT;
aliguori5b08fc12008-08-21 20:08:03 +00003910 break;
blueswir198448f52008-09-30 18:16:09 +00003911 }
aliguori0a1af392008-10-31 18:40:25 +00003912 timeout = 5000;
bellard5905b2e2004-08-01 21:53:26 +00003913 }
bellard89bfc102006-02-08 22:46:31 +00003914#ifdef CONFIG_PROFILER
3915 ti = profile_getclock();
3916#endif
bellard5905b2e2004-08-01 21:53:26 +00003917 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00003918#ifdef CONFIG_PROFILER
3919 dev_time += profile_getclock() - ti;
3920#endif
bellardb4608c02003-06-27 17:34:32 +00003921 }
bellard34865132003-10-05 14:28:56 +00003922 cpu_disable_ticks();
3923 return ret;
bellardb4608c02003-06-27 17:34:32 +00003924}
3925
ths15f82202007-06-29 23:26:08 +00003926static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00003927{
blueswir1d2c639d2009-01-24 18:19:25 +00003928 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3929 and qemu-doc.texi */
bellard68d0f702008-01-06 17:21:48 +00003930 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00003931 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00003932 "\n"
bellarda20dd502003-09-30 21:07:02 +00003933 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00003934 "\n"
bellarda20dd502003-09-30 21:07:02 +00003935 "Standard options:\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003936 "-h or -help display this help and exit\n"
bellardcc1daa42005-06-05 14:49:17 +00003937 "-M machine select emulated machine (-M ? for list)\n"
pbrook5adb4832007-03-08 03:15:18 +00003938 "-cpu cpu select CPU (-cpu ? for list)\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003939 "-smp n set the number of CPUs to 'n' [default=1]\n"
bellardc45886d2004-01-05 00:02:06 +00003940 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00003941 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3942 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003943 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
aurel32a1620fa2008-04-29 05:58:01 +00003944 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3945 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
aliguorifa879c62009-01-07 17:32:33 +00003946 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
thse4bcb142007-12-02 04:51:10 +00003947 " use 'file' as a drive image\n"
balrog3e3d5812007-04-30 02:09:25 +00003948 "-mtdblock file use 'file' as on-board Flash memory image\n"
pbrooka1bb27b2007-04-06 16:49:48 +00003949 "-sd file use 'file' as SecureDigital card image\n"
j_mayer86f55662007-04-24 06:52:59 +00003950 "-pflash file use 'file' as a parallel flash image\n"
thseec85c22007-01-05 17:41:07 +00003951 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
ths667acca2006-12-11 02:08:05 +00003952 "-snapshot write to temporary files instead of disk image files\n"
bellarda00bad72004-05-22 21:39:06 +00003953 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellard4ca00742004-12-12 22:20:04 +00003954#ifndef _WIN32
ths667acca2006-12-11 02:08:05 +00003955 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00003956#endif
bellard1d14ffa2005-10-30 18:58:22 +00003957#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00003958 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00003959 "-soundhw c1,... enable audio support\n"
3960 " and only specified sound cards (comma separated list)\n"
3961 " use -soundhw ? to get the list of supported cards\n"
bellard6a36d842005-12-18 20:34:32 +00003962 " use -soundhw all to enable all of them\n"
bellard1d14ffa2005-10-30 18:58:22 +00003963#endif
bellardb389dbf2005-11-06 16:49:55 +00003964 "-usb enable the USB driver (will be the default soon)\n"
3965 "-usbdevice name add the host or guest USB device 'name'\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003966 "-name string set the name of the guest\n"
3967 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
3968 " specify machine UUID\n"
3969 "\n"
3970 "Display options:\n"
3971 "-nographic disable graphical output and redirect serial I/Os to console\n"
3972#ifdef CONFIG_CURSES
3973 "-curses use a curses/ncurses interface instead of SDL\n"
3974#endif
3975#ifdef CONFIG_SDL
3976 "-no-frame open SDL window without a frame and window decorations\n"
3977 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3978 "-no-quit disable SDL window close capability\n"
3979 "-sdl enable SDL\n"
3980#endif
3981 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3982 "-vga [std|cirrus|vmware|none]\n"
3983 " select video card type\n"
3984 "-full-screen start in full screen\n"
bellard6f7e9ae2005-03-13 09:43:36 +00003985#if defined(TARGET_PPC) || defined(TARGET_SPARC)
3986 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00003987#endif
blueswir1d2c639d2009-01-24 18:19:25 +00003988 "-vnc display start a VNC server on display\n"
bellarda20dd502003-09-30 21:07:02 +00003989 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003990 "Network options:\n"
aliguori7a9f6e42009-01-07 17:48:51 +00003991 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
bellard7c9d8e02005-11-15 22:16:05 +00003992 " create a new Network Interface Card and connect it to VLAN 'n'\n"
bellardc20709a2004-04-21 23:27:19 +00003993#ifdef CONFIG_SLIRP
aliguori7a9f6e42009-01-07 17:48:51 +00003994 "-net user[,vlan=n][,name=str][,hostname=host]\n"
pbrook115defd2006-04-16 11:06:58 +00003995 " connect the user mode network stack to VLAN 'n' and send\n"
3996 " hostname 'host' to DHCP clients\n"
bellard7c9d8e02005-11-15 22:16:05 +00003997#endif
bellard7fb843f2006-02-01 23:06:55 +00003998#ifdef _WIN32
aliguori7a9f6e42009-01-07 17:48:51 +00003999 "-net tap[,vlan=n][,name=str],ifname=name\n"
bellard7fb843f2006-02-01 23:06:55 +00004000 " connect the host TAP network interface to VLAN 'n'\n"
4001#else
aliguori7a9f6e42009-01-07 17:48:51 +00004002 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
thsb46a8902007-10-21 23:20:45 +00004003 " connect the host TAP network interface to VLAN 'n' and use the\n"
4004 " network scripts 'file' (default=%s)\n"
4005 " and 'dfile' (default=%s);\n"
4006 " use '[down]script=no' to disable script execution;\n"
bellard7c9d8e02005-11-15 22:16:05 +00004007 " use 'fd=h' to connect to an already opened TAP interface\n"
bellard7fb843f2006-02-01 23:06:55 +00004008#endif
aliguori7a9f6e42009-01-07 17:48:51 +00004009 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
bellard7c9d8e02005-11-15 22:16:05 +00004010 " connect the vlan 'n' to another VLAN using a socket connection\n"
aliguori7a9f6e42009-01-07 17:48:51 +00004011 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
bellard3d830452005-12-18 16:36:49 +00004012 " connect the vlan 'n' to multicast maddr and port\n"
ths8a16d272008-07-19 09:56:24 +00004013#ifdef CONFIG_VDE
aliguori7a9f6e42009-01-07 17:48:51 +00004014 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
ths8a16d272008-07-19 09:56:24 +00004015 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4016 " on host and listening for incoming connections on 'socketpath'.\n"
4017 " Use group 'groupname' and mode 'octalmode' to change default\n"
4018 " ownership and permissions for communication port.\n"
4019#endif
bellard7c9d8e02005-11-15 22:16:05 +00004020 "-net none use it alone to have zero network devices; if no -net option\n"
4021 " is provided, the default is '-net nic -net user'\n"
bellard7c9d8e02005-11-15 22:16:05 +00004022#ifdef CONFIG_SLIRP
ths0db11372007-02-20 00:12:07 +00004023 "-tftp dir allow tftp access to files in dir [-net user]\n"
ths47d5d012007-02-20 00:05:08 +00004024 "-bootp file advertise file in BOOTP replies\n"
bellard7c9d8e02005-11-15 22:16:05 +00004025#ifndef _WIN32
4026 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
bellardc94c8d62004-09-13 21:37:34 +00004027#endif
bellard9bf05442004-08-25 22:12:49 +00004028 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
bellard7c9d8e02005-11-15 22:16:05 +00004029 " redirect TCP or UDP connections from host to guest [-net user]\n"
bellardc20709a2004-04-21 23:27:19 +00004030#endif
bellardc4b1fcc2004-03-14 21:44:30 +00004031 "\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004032 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
4033 "-bt hci,host[:id]\n"
4034 " use host's HCI with the given name\n"
4035 "-bt hci[,vlan=n]\n"
4036 " emulate a standard HCI in virtual scatternet 'n'\n"
4037 "-bt vhci[,vlan=n]\n"
4038 " add host computer to virtual scatternet 'n' using VHCI\n"
4039 "-bt device:dev[,vlan=n]\n"
4040 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4041 "\n"
4042#ifdef TARGET_I386
4043 "\n"
4044 "i386 target only:\n"
4045 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4046 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4047 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4048 "-no-acpi disable ACPI\n"
4049 "-no-hpet disable HPET\n"
aliguori8a92ea22009-02-27 20:12:36 +00004050 "-acpitable [sig=str][,rev=n][,oem_id=str][,oem_table_id=str][,oem_rev=n][,asl_compiler_id=str][,asl_compiler_rev=n][,data=file1[:file2]...]\n"
4051 " ACPI table description\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004052#endif
bellardc4b1fcc2004-03-14 21:44:30 +00004053 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00004054 "-kernel bzImage use 'bzImage' as kernel image\n"
4055 "-append cmdline use 'cmdline' as kernel command line\n"
4056 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00004057 "\n"
bellard330d0412003-07-26 18:11:40 +00004058 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00004059 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00004060 "-parallel dev redirect the parallel port to char device 'dev'\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004061 "-monitor dev redirect the monitor to char device 'dev'\n"
4062 "-pidfile file write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00004063 "-S freeze CPU at startup (use 'c' to start execution)\n"
pbrookcfc34752007-02-22 01:48:01 +00004064 "-s wait gdb connection to port\n"
4065 "-p port set gdb connection port [default=%s]\n"
bellardf193c792004-03-21 17:06:25 +00004066 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004067 "-hdachs c,h,s[,t]\n"
4068 " force hard disk 0 physical geometry and the optional BIOS\n"
bellard46d47672004-11-16 01:45:27 +00004069 " translation (t=none or lba) (usually qemu can guess them)\n"
bellard87b47352006-08-17 17:22:54 +00004070 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004071 "-bios file set the filename for the BIOS\n"
bellardd993e022005-02-10 22:00:06 +00004072#ifdef USE_KQEMU
bellard6515b202006-05-03 22:02:44 +00004073 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
bellardd993e022005-02-10 22:00:06 +00004074 "-no-kqemu disable KQEMU kernel module usage\n"
4075#endif
aliguori7ba1e612008-11-05 16:04:33 +00004076#ifdef CONFIG_KVM
4077 "-enable-kvm enable KVM full virtualization support\n"
4078#endif
bellardd1beab82006-10-02 19:44:22 +00004079 "-no-reboot exit instead of rebooting\n"
aurel32b2f76162008-04-11 21:35:52 +00004080 "-no-shutdown stop before shutdown\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004081 "-loadvm [tag|id]\n"
4082 " start right away with a saved state (loadvm in monitor)\n"
ths71e3ceb2006-12-22 02:11:31 +00004083#ifndef _WIN32
4084 "-daemonize daemonize QEMU after initializing\n"
4085#endif
ths9ae02552007-01-05 17:39:04 +00004086 "-option-rom rom load a file, rom, into the option ROM space\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004087#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4088 "-prom-env variable=value\n"
4089 " set OpenBIOS nvram variables\n"
blueswir166508602007-05-01 14:16:52 +00004090#endif
thsf3dcfad2007-08-24 01:26:02 +00004091 "-clock force the use of the given methods for timer alarm.\n"
aurel323adda042008-03-09 23:43:49 +00004092 " To see what timers are available use -clock ?\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004093 "-localtime set the real time clock to local time [default=utc]\n"
bellardbce61842008-02-01 22:18:51 +00004094 "-startdate select initial date of the clock\n"
pbrook2e70f6e2008-06-29 01:03:05 +00004095 "-icount [N|auto]\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004096 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4097 "-echr chr set terminal escape character instead of ctrl-a\n"
4098 "-virtioconsole c\n"
4099 " set virtio console\n"
4100 "-show-cursor show cursor\n"
4101#if defined(TARGET_ARM) || defined(TARGET_M68K)
4102 "-semihosting semihosting mode\n"
4103#endif
4104#if defined(TARGET_ARM)
4105 "-old-param old param mode\n"
4106#endif
4107 "-tb-size n set TB size\n"
4108 "-incoming p prepare for incoming migration, listen on port p\n"
aliguori08585322009-02-27 22:09:45 +00004109#ifndef _WIN32
4110 "-chroot dir Chroot to dir just before starting the VM.\n"
4111 "-runas user Change to user id user just before starting the VM.\n"
4112#endif
bellard0824d6f2003-06-24 13:42:40 +00004113 "\n"
bellard82c643f2004-07-14 17:28:13 +00004114 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004115 "ctrl-alt-f toggle full screen\n"
4116 "ctrl-alt-n switch to virtual console 'n'\n"
4117 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004118 "\n"
4119 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4120 ,
bellard0db63472003-10-27 21:37:46 +00004121 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004122 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004123#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004124 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004125 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004126#endif
bellard6e44ba72004-01-18 21:56:49 +00004127 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004128 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004129 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004130}
4131
bellardcd6f1162004-05-13 22:02:20 +00004132#define HAS_ARG 0x0001
4133
4134enum {
blueswir1d2c639d2009-01-24 18:19:25 +00004135 /* Please keep in synch with help, qemu_options[] and
4136 qemu-doc.texi */
4137 /* Standard options: */
bellardcd6f1162004-05-13 22:02:20 +00004138 QEMU_OPTION_h,
bellardcc1daa42005-06-05 14:49:17 +00004139 QEMU_OPTION_M,
j_mayer94fc95c2007-03-05 19:44:02 +00004140 QEMU_OPTION_cpu,
blueswir1d2c639d2009-01-24 18:19:25 +00004141 QEMU_OPTION_smp,
bellardcd6f1162004-05-13 22:02:20 +00004142 QEMU_OPTION_fda,
4143 QEMU_OPTION_fdb,
4144 QEMU_OPTION_hda,
4145 QEMU_OPTION_hdb,
4146 QEMU_OPTION_hdc,
4147 QEMU_OPTION_hdd,
4148 QEMU_OPTION_cdrom,
blueswir1d2c639d2009-01-24 18:19:25 +00004149 QEMU_OPTION_drive,
balrog3e3d5812007-04-30 02:09:25 +00004150 QEMU_OPTION_mtdblock,
pbrooka1bb27b2007-04-06 16:49:48 +00004151 QEMU_OPTION_sd,
j_mayer86f55662007-04-24 06:52:59 +00004152 QEMU_OPTION_pflash,
bellardcd6f1162004-05-13 22:02:20 +00004153 QEMU_OPTION_boot,
4154 QEMU_OPTION_snapshot,
4155 QEMU_OPTION_m,
blueswir1d2c639d2009-01-24 18:19:25 +00004156 QEMU_OPTION_k,
bellard1d14ffa2005-10-30 18:58:22 +00004157 QEMU_OPTION_audio_help,
4158 QEMU_OPTION_soundhw,
blueswir1d2c639d2009-01-24 18:19:25 +00004159 QEMU_OPTION_usb,
4160 QEMU_OPTION_usbdevice,
4161 QEMU_OPTION_name,
4162 QEMU_OPTION_uuid,
bellardcd6f1162004-05-13 22:02:20 +00004163
blueswir1d2c639d2009-01-24 18:19:25 +00004164 /* Display options: */
4165 QEMU_OPTION_nographic,
4166 QEMU_OPTION_curses,
4167 QEMU_OPTION_no_frame,
4168 QEMU_OPTION_alt_grab,
4169 QEMU_OPTION_no_quit,
4170 QEMU_OPTION_sdl,
4171 QEMU_OPTION_portrait,
4172 QEMU_OPTION_vga,
4173 QEMU_OPTION_full_screen,
4174 QEMU_OPTION_g,
4175 QEMU_OPTION_vnc,
4176
4177 /* Network options: */
bellard7c9d8e02005-11-15 22:16:05 +00004178 QEMU_OPTION_net,
bellardc7f74642004-08-24 21:57:12 +00004179 QEMU_OPTION_tftp,
ths47d5d012007-02-20 00:05:08 +00004180 QEMU_OPTION_bootp,
bellard9d728e82004-09-05 23:09:03 +00004181 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00004182 QEMU_OPTION_redir,
balrogdc72ac12008-11-09 00:04:26 +00004183 QEMU_OPTION_bt,
bellardcd6f1162004-05-13 22:02:20 +00004184
blueswir1d2c639d2009-01-24 18:19:25 +00004185 /* i386 target only: */
4186 QEMU_OPTION_win2k_hack,
4187 QEMU_OPTION_rtc_td_hack,
4188 QEMU_OPTION_no_fd_bootchk,
4189 QEMU_OPTION_no_acpi,
4190 QEMU_OPTION_no_hpet,
aliguori8a92ea22009-02-27 20:12:36 +00004191 QEMU_OPTION_acpitable,
blueswir1d2c639d2009-01-24 18:19:25 +00004192
4193 /* Linux boot specific: */
bellardcd6f1162004-05-13 22:02:20 +00004194 QEMU_OPTION_kernel,
4195 QEMU_OPTION_append,
4196 QEMU_OPTION_initrd,
4197
blueswir1d2c639d2009-01-24 18:19:25 +00004198 /* Debug/Expert options: */
4199 QEMU_OPTION_serial,
4200 QEMU_OPTION_parallel,
4201 QEMU_OPTION_monitor,
4202 QEMU_OPTION_pidfile,
bellardcd6f1162004-05-13 22:02:20 +00004203 QEMU_OPTION_S,
4204 QEMU_OPTION_s,
4205 QEMU_OPTION_p,
4206 QEMU_OPTION_d,
4207 QEMU_OPTION_hdachs,
4208 QEMU_OPTION_L,
j_mayer1192dad2007-10-05 13:08:35 +00004209 QEMU_OPTION_bios,
bellard89bfc102006-02-08 22:46:31 +00004210 QEMU_OPTION_kernel_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00004211 QEMU_OPTION_no_kqemu,
aliguori7ba1e612008-11-05 16:04:33 +00004212 QEMU_OPTION_enable_kvm,
bellardd1beab82006-10-02 19:44:22 +00004213 QEMU_OPTION_no_reboot,
aurel32b2f76162008-04-11 21:35:52 +00004214 QEMU_OPTION_no_shutdown,
blueswir1d2c639d2009-01-24 18:19:25 +00004215 QEMU_OPTION_loadvm,
ths71e3ceb2006-12-22 02:11:31 +00004216 QEMU_OPTION_daemonize,
ths9ae02552007-01-05 17:39:04 +00004217 QEMU_OPTION_option_rom,
blueswir166508602007-05-01 14:16:52 +00004218 QEMU_OPTION_prom_env,
thsf3dcfad2007-08-24 01:26:02 +00004219 QEMU_OPTION_clock,
blueswir1d2c639d2009-01-24 18:19:25 +00004220 QEMU_OPTION_localtime,
bellard7e0af5d02007-11-07 16:24:33 +00004221 QEMU_OPTION_startdate,
pbrook2e70f6e2008-06-29 01:03:05 +00004222 QEMU_OPTION_icount,
blueswir1d2c639d2009-01-24 18:19:25 +00004223 QEMU_OPTION_echr,
4224 QEMU_OPTION_virtiocon,
4225 QEMU_OPTION_show_cursor,
4226 QEMU_OPTION_semihosting,
4227 QEMU_OPTION_old_param,
4228 QEMU_OPTION_tb_size,
aliguori5bb79102008-10-13 03:12:02 +00004229 QEMU_OPTION_incoming,
aliguori08585322009-02-27 22:09:45 +00004230 QEMU_OPTION_chroot,
4231 QEMU_OPTION_runas,
bellardcd6f1162004-05-13 22:02:20 +00004232};
4233
4234typedef struct QEMUOption {
4235 const char *name;
4236 int flags;
4237 int index;
4238} QEMUOption;
4239
blueswir1dbed7e42008-10-01 19:38:09 +00004240static const QEMUOption qemu_options[] = {
blueswir1d2c639d2009-01-24 18:19:25 +00004241 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4242 qemu-doc.texi */
4243 /* Standard options: */
bellardcd6f1162004-05-13 22:02:20 +00004244 { "h", 0, QEMU_OPTION_h },
pbrook64423fb2007-01-27 17:11:41 +00004245 { "help", 0, QEMU_OPTION_h },
bellardcc1daa42005-06-05 14:49:17 +00004246 { "M", HAS_ARG, QEMU_OPTION_M },
j_mayer94fc95c2007-03-05 19:44:02 +00004247 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
blueswir1d2c639d2009-01-24 18:19:25 +00004248 { "smp", HAS_ARG, QEMU_OPTION_smp },
bellardcd6f1162004-05-13 22:02:20 +00004249 { "fda", HAS_ARG, QEMU_OPTION_fda },
4250 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
4251 { "hda", HAS_ARG, QEMU_OPTION_hda },
4252 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
4253 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
4254 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
4255 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
blueswir1d2c639d2009-01-24 18:19:25 +00004256 { "drive", HAS_ARG, QEMU_OPTION_drive },
balrog3e3d5812007-04-30 02:09:25 +00004257 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
pbrooka1bb27b2007-04-06 16:49:48 +00004258 { "sd", HAS_ARG, QEMU_OPTION_sd },
j_mayer86f55662007-04-24 06:52:59 +00004259 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
bellardcd6f1162004-05-13 22:02:20 +00004260 { "boot", HAS_ARG, QEMU_OPTION_boot },
4261 { "snapshot", 0, QEMU_OPTION_snapshot },
4262 { "m", HAS_ARG, QEMU_OPTION_m },
blueswir1d2c639d2009-01-24 18:19:25 +00004263#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00004264 { "k", HAS_ARG, QEMU_OPTION_k },
blueswir1d2c639d2009-01-24 18:19:25 +00004265#endif
bellard1d14ffa2005-10-30 18:58:22 +00004266#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004267 { "audio-help", 0, QEMU_OPTION_audio_help },
4268 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
4269#endif
blueswir1d2c639d2009-01-24 18:19:25 +00004270 { "usb", 0, QEMU_OPTION_usb },
4271 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
4272 { "name", HAS_ARG, QEMU_OPTION_name },
4273 { "uuid", HAS_ARG, QEMU_OPTION_uuid },
bellardcd6f1162004-05-13 22:02:20 +00004274
blueswir1d2c639d2009-01-24 18:19:25 +00004275 /* Display options: */
4276 { "nographic", 0, QEMU_OPTION_nographic },
4277#ifdef CONFIG_CURSES
4278 { "curses", 0, QEMU_OPTION_curses },
4279#endif
4280#ifdef CONFIG_SDL
4281 { "no-frame", 0, QEMU_OPTION_no_frame },
4282 { "alt-grab", 0, QEMU_OPTION_alt_grab },
4283 { "no-quit", 0, QEMU_OPTION_no_quit },
4284 { "sdl", 0, QEMU_OPTION_sdl },
4285#endif
4286 { "portrait", 0, QEMU_OPTION_portrait },
4287 { "vga", HAS_ARG, QEMU_OPTION_vga },
4288 { "full-screen", 0, QEMU_OPTION_full_screen },
4289#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4290 { "g", 1, QEMU_OPTION_g },
4291#endif
4292 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
4293
4294 /* Network options: */
bellard7c9d8e02005-11-15 22:16:05 +00004295 { "net", HAS_ARG, QEMU_OPTION_net},
bellard158156d2004-05-17 21:13:42 +00004296#ifdef CONFIG_SLIRP
bellardc7f74642004-08-24 21:57:12 +00004297 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
ths47d5d012007-02-20 00:05:08 +00004298 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
bellardc94c8d62004-09-13 21:37:34 +00004299#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004300 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00004301#endif
bellard9bf05442004-08-25 22:12:49 +00004302 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00004303#endif
balrogdc72ac12008-11-09 00:04:26 +00004304 { "bt", HAS_ARG, QEMU_OPTION_bt },
blueswir1d2c639d2009-01-24 18:19:25 +00004305#ifdef TARGET_I386
4306 /* i386 target only: */
4307 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
4308 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack },
4309 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
4310 { "no-acpi", 0, QEMU_OPTION_no_acpi },
4311 { "no-hpet", 0, QEMU_OPTION_no_hpet },
aliguori8a92ea22009-02-27 20:12:36 +00004312 { "acpitable", HAS_ARG, QEMU_OPTION_acpitable },
blueswir1d2c639d2009-01-24 18:19:25 +00004313#endif
bellardcd6f1162004-05-13 22:02:20 +00004314
blueswir1d2c639d2009-01-24 18:19:25 +00004315 /* Linux boot specific: */
bellardcd6f1162004-05-13 22:02:20 +00004316 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
4317 { "append", HAS_ARG, QEMU_OPTION_append },
4318 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
4319
blueswir1d2c639d2009-01-24 18:19:25 +00004320 /* Debug/Expert options: */
4321 { "serial", HAS_ARG, QEMU_OPTION_serial },
4322 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
4323 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
4324 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellardcd6f1162004-05-13 22:02:20 +00004325 { "S", 0, QEMU_OPTION_S },
4326 { "s", 0, QEMU_OPTION_s },
4327 { "p", HAS_ARG, QEMU_OPTION_p },
4328 { "d", HAS_ARG, QEMU_OPTION_d },
4329 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
4330 { "L", HAS_ARG, QEMU_OPTION_L },
j_mayer1192dad2007-10-05 13:08:35 +00004331 { "bios", HAS_ARG, QEMU_OPTION_bios },
bellardd993e022005-02-10 22:00:06 +00004332#ifdef USE_KQEMU
bellard89bfc102006-02-08 22:46:31 +00004333 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
blueswir1d2c639d2009-01-24 18:19:25 +00004334 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
bellardd993e022005-02-10 22:00:06 +00004335#endif
aliguori7ba1e612008-11-05 16:04:33 +00004336#ifdef CONFIG_KVM
4337 { "enable-kvm", 0, QEMU_OPTION_enable_kvm },
4338#endif
bellardd1beab82006-10-02 19:44:22 +00004339 { "no-reboot", 0, QEMU_OPTION_no_reboot },
aurel32b2f76162008-04-11 21:35:52 +00004340 { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
blueswir1d2c639d2009-01-24 18:19:25 +00004341 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
ths71e3ceb2006-12-22 02:11:31 +00004342 { "daemonize", 0, QEMU_OPTION_daemonize },
ths9ae02552007-01-05 17:39:04 +00004343 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
blueswir195efd112008-12-24 20:26:14 +00004344#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00004345 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
4346#endif
blueswir1d2c639d2009-01-24 18:19:25 +00004347 { "clock", HAS_ARG, QEMU_OPTION_clock },
4348 { "localtime", 0, QEMU_OPTION_localtime },
4349 { "startdate", HAS_ARG, QEMU_OPTION_startdate },
4350 { "icount", HAS_ARG, QEMU_OPTION_icount },
4351 { "echr", HAS_ARG, QEMU_OPTION_echr },
4352 { "virtioconsole", HAS_ARG, QEMU_OPTION_virtiocon },
4353 { "show-cursor", 0, QEMU_OPTION_show_cursor },
4354#if defined(TARGET_ARM) || defined(TARGET_M68K)
4355 { "semihosting", 0, QEMU_OPTION_semihosting },
4356#endif
balrog2b8f2d42007-07-27 22:08:46 +00004357#if defined(TARGET_ARM)
4358 { "old-param", 0, QEMU_OPTION_old_param },
4359#endif
bellard26a5f132008-05-28 12:30:31 +00004360 { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
aliguori5bb79102008-10-13 03:12:02 +00004361 { "incoming", HAS_ARG, QEMU_OPTION_incoming },
aliguori08585322009-02-27 22:09:45 +00004362 { "chroot", HAS_ARG, QEMU_OPTION_chroot },
4363 { "runas", HAS_ARG, QEMU_OPTION_runas },
bellardcd6f1162004-05-13 22:02:20 +00004364 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004365};
4366
bellard1d14ffa2005-10-30 18:58:22 +00004367#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004368struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004369#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004370#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004371 {
4372 "pcspk",
4373 "PC speaker",
4374 0,
4375 1,
4376 { .init_isa = pcspk_audio_init }
4377 },
4378#endif
malc4c9b53e2009-01-09 10:46:34 +00004379
4380#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004381 {
4382 "sb16",
4383 "Creative Sound Blaster 16",
4384 0,
4385 1,
4386 { .init_isa = SB16_init }
4387 },
malc4c9b53e2009-01-09 10:46:34 +00004388#endif
bellard6a36d842005-12-18 20:34:32 +00004389
malccc53d262008-06-13 10:48:22 +00004390#ifdef CONFIG_CS4231A
4391 {
4392 "cs4231a",
4393 "CS4231A",
4394 0,
4395 1,
4396 { .init_isa = cs4231a_init }
4397 },
4398#endif
4399
bellard6a36d842005-12-18 20:34:32 +00004400#ifdef CONFIG_ADLIB
4401 {
4402 "adlib",
4403#ifdef HAS_YMF262
4404 "Yamaha YMF262 (OPL3)",
4405#else
4406 "Yamaha YM3812 (OPL2)",
4407#endif
4408 0,
4409 1,
4410 { .init_isa = Adlib_init }
4411 },
4412#endif
4413
4414#ifdef CONFIG_GUS
4415 {
4416 "gus",
4417 "Gravis Ultrasound GF1",
4418 0,
4419 1,
4420 { .init_isa = GUS_init }
4421 },
4422#endif
4423
malc4c9b53e2009-01-09 10:46:34 +00004424#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004425 {
4426 "ac97",
4427 "Intel 82801AA AC97 Audio",
4428 0,
4429 0,
4430 { .init_pci = ac97_init }
4431 },
malc4c9b53e2009-01-09 10:46:34 +00004432#endif
balroge5c9a132008-01-14 04:27:55 +00004433
malc4c9b53e2009-01-09 10:46:34 +00004434#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004435 {
4436 "es1370",
4437 "ENSONIQ AudioPCI ES1370",
4438 0,
4439 0,
4440 { .init_pci = es1370_init }
4441 },
balrogb00052e2007-04-30 02:22:06 +00004442#endif
bellard6a36d842005-12-18 20:34:32 +00004443
malc4c9b53e2009-01-09 10:46:34 +00004444#endif /* HAS_AUDIO_CHOICE */
4445
bellard6a36d842005-12-18 20:34:32 +00004446 { NULL, NULL, 0, 0, { NULL } }
4447};
4448
bellard1d14ffa2005-10-30 18:58:22 +00004449static void select_soundhw (const char *optarg)
4450{
bellard6a36d842005-12-18 20:34:32 +00004451 struct soundhw *c;
4452
bellard1d14ffa2005-10-30 18:58:22 +00004453 if (*optarg == '?') {
4454 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004455
bellard1d14ffa2005-10-30 18:58:22 +00004456 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004457 for (c = soundhw; c->name; ++c) {
4458 printf ("%-11s %s\n", c->name, c->descr);
4459 }
4460 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004461 exit (*optarg != '?');
4462 }
4463 else {
bellard6a36d842005-12-18 20:34:32 +00004464 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004465 const char *p;
4466 char *e;
4467 int bad_card = 0;
4468
bellard6a36d842005-12-18 20:34:32 +00004469 if (!strcmp (optarg, "all")) {
4470 for (c = soundhw; c->name; ++c) {
4471 c->enabled = 1;
4472 }
4473 return;
4474 }
bellard1d14ffa2005-10-30 18:58:22 +00004475
bellard6a36d842005-12-18 20:34:32 +00004476 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004477 while (*p) {
4478 e = strchr (p, ',');
4479 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004480
4481 for (c = soundhw; c->name; ++c) {
4482 if (!strncmp (c->name, p, l)) {
4483 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004484 break;
4485 }
4486 }
bellard6a36d842005-12-18 20:34:32 +00004487
4488 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004489 if (l > 80) {
4490 fprintf (stderr,
4491 "Unknown sound card name (too big to show)\n");
4492 }
4493 else {
4494 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4495 (int) l, p);
4496 }
4497 bad_card = 1;
4498 }
4499 p += l + (e != NULL);
4500 }
4501
4502 if (bad_card)
4503 goto show_valid_cards;
4504 }
4505}
4506#endif
4507
malc3893c122008-09-28 00:42:05 +00004508static void select_vgahw (const char *p)
4509{
4510 const char *opts;
4511
4512 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004513 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004514 cirrus_vga_enabled = 0;
4515 vmsvga_enabled = 0;
4516 } else if (strstart(p, "cirrus", &opts)) {
4517 cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004518 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004519 vmsvga_enabled = 0;
4520 } else if (strstart(p, "vmware", &opts)) {
4521 cirrus_vga_enabled = 0;
aliguoric2b3b412009-01-15 20:37:28 +00004522 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004523 vmsvga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004524 } else if (strstart(p, "none", &opts)) {
4525 cirrus_vga_enabled = 0;
4526 std_vga_enabled = 0;
4527 vmsvga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004528 } else {
4529 invalid_vga:
4530 fprintf(stderr, "Unknown vga type: %s\n", p);
4531 exit(1);
4532 }
malccb5a7aa2008-09-28 00:42:12 +00004533 while (*opts) {
4534 const char *nextopt;
4535
4536 if (strstart(opts, ",retrace=", &nextopt)) {
4537 opts = nextopt;
4538 if (strstart(opts, "dumb", &nextopt))
4539 vga_retrace_method = VGA_RETRACE_DUMB;
4540 else if (strstart(opts, "precise", &nextopt))
4541 vga_retrace_method = VGA_RETRACE_PRECISE;
4542 else goto invalid_vga;
4543 } else goto invalid_vga;
4544 opts = nextopt;
4545 }
malc3893c122008-09-28 00:42:05 +00004546}
4547
bellard3587d7e2006-06-26 20:03:44 +00004548#ifdef _WIN32
4549static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4550{
4551 exit(STATUS_CONTROL_C_EXIT);
4552 return TRUE;
4553}
4554#endif
4555
blueswir18fcb1b92008-09-18 18:29:08 +00004556static int qemu_uuid_parse(const char *str, uint8_t *uuid)
4557{
4558 int ret;
4559
4560 if(strlen(str) != 36)
4561 return -1;
4562
4563 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4564 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4565 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4566
4567 if(ret != 16)
4568 return -1;
4569
4570 return 0;
4571}
4572
bellard7c9d8e02005-11-15 22:16:05 +00004573#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004574
aliguori5b08fc12008-08-21 20:08:03 +00004575#ifndef _WIN32
4576
4577static void termsig_handler(int signal)
4578{
4579 qemu_system_shutdown_request();
4580}
4581
blueswir16f9e3802008-09-09 18:56:59 +00004582static void termsig_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004583{
4584 struct sigaction act;
4585
4586 memset(&act, 0, sizeof(act));
4587 act.sa_handler = termsig_handler;
4588 sigaction(SIGINT, &act, NULL);
4589 sigaction(SIGHUP, &act, NULL);
4590 sigaction(SIGTERM, &act, NULL);
4591}
4592
4593#endif
4594
malc902b3d52008-12-10 19:18:40 +00004595int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004596{
bellard67b915a2004-03-31 23:37:16 +00004597#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00004598 int use_gdbstub;
4599 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00004600#endif
j_mayer28c5af52007-11-11 01:50:45 +00004601 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004602 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004603 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004604 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004605 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00004606 const char *boot_devices = "";
aliguori3023f332009-01-16 19:04:14 +00004607 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004608 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004609 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004610 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004611 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004612 const char *bt_opts[MAX_BT_CMDLINE];
4613 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004614 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004615 int optind;
4616 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004617 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004618 const char *monitor_device;
4619 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004620 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004621 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004622 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004623 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4624 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004625 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004626 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004627 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004628 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004629 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00004630 int fds[2];
bellard26a5f132008-05-28 12:30:31 +00004631 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004632 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004633 const char *incoming = NULL;
aliguori54042bc2009-02-27 22:16:47 +00004634 int fd = 0;
4635 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004636 const char *chroot_dir = NULL;
4637 const char *run_as = NULL;
bellard0bd48852005-11-11 00:00:47 +00004638
malc902b3d52008-12-10 19:18:40 +00004639 qemu_cache_utils_init(envp);
4640
bellard0bd48852005-11-11 00:00:47 +00004641 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004642#ifndef _WIN32
4643 {
4644 struct sigaction act;
4645 sigfillset(&act.sa_mask);
4646 act.sa_flags = 0;
4647 act.sa_handler = SIG_IGN;
4648 sigaction(SIGPIPE, &act, NULL);
4649 }
bellard3587d7e2006-06-26 20:03:44 +00004650#else
4651 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004652 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4653 QEMU to run on a single CPU */
4654 {
4655 HANDLE h;
4656 DWORD mask, smask;
4657 int i;
4658 h = GetCurrentProcess();
4659 if (GetProcessAffinityMask(h, &mask, &smask)) {
4660 for(i = 0; i < 32; i++) {
4661 if (mask & (1 << i))
4662 break;
4663 }
4664 if (i != 32) {
4665 mask = 1 << i;
4666 SetProcessAffinityMask(h, mask);
4667 }
4668 }
4669 }
bellard67b915a2004-03-31 23:37:16 +00004670#endif
bellardbe995c22006-06-25 16:25:21 +00004671
bellardcc1daa42005-06-05 14:49:17 +00004672 register_machines();
4673 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004674 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004675 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004676 ram_size = 0;
bellard313aa562003-08-10 21:52:11 +00004677 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00004678#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00004679 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00004680 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00004681#endif
bellard33e39632003-07-06 17:15:21 +00004682 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004683 nographic = 0;
balrog4d3b6f62008-02-10 16:33:14 +00004684 curses = 0;
bellarda20dd502003-09-30 21:07:02 +00004685 kernel_filename = NULL;
4686 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004687 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004688 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00004689 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00004690
aurel32c75a8232008-05-04 00:50:34 +00004691 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004692 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004693 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004694 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004695
aliguori8290edd2009-02-27 20:14:29 +00004696 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004697 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004698 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004699 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004700
aliguori1b8fc812009-02-27 20:01:39 +00004701 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004702 virtio_consoles[i] = NULL;
4703 virtio_console_index = 0;
4704
bellarda594cfb2005-11-06 16:13:29 +00004705 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004706
bellard7c9d8e02005-11-15 22:16:05 +00004707 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004708 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004709 nb_drives = 0;
4710 nb_drives_opt = 0;
4711 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004712
4713 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004714
bellard26a5f132008-05-28 12:30:31 +00004715 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004716 autostart= 1;
4717
bellardcd6f1162004-05-13 22:02:20 +00004718 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004719 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004720 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004721 break;
bellardcd6f1162004-05-13 22:02:20 +00004722 r = argv[optind];
4723 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004724 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004725 } else {
4726 const QEMUOption *popt;
4727
4728 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004729 /* Treat --foo the same as -foo. */
4730 if (r[1] == '-')
4731 r++;
bellardcd6f1162004-05-13 22:02:20 +00004732 popt = qemu_options;
4733 for(;;) {
4734 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004735 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004736 argv[0], r);
4737 exit(1);
4738 }
4739 if (!strcmp(popt->name, r + 1))
4740 break;
4741 popt++;
4742 }
4743 if (popt->flags & HAS_ARG) {
4744 if (optind >= argc) {
4745 fprintf(stderr, "%s: option '%s' requires an argument\n",
4746 argv[0], r);
4747 exit(1);
4748 }
4749 optarg = argv[optind++];
4750 } else {
4751 optarg = NULL;
4752 }
4753
4754 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004755 case QEMU_OPTION_M:
4756 machine = find_machine(optarg);
4757 if (!machine) {
4758 QEMUMachine *m;
4759 printf("Supported machines are:\n");
4760 for(m = first_machine; m != NULL; m = m->next) {
4761 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004762 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00004763 m == first_machine ? " (default)" : "");
4764 }
ths15f82202007-06-29 23:26:08 +00004765 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004766 }
4767 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004768 case QEMU_OPTION_cpu:
4769 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004770 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004771/* XXX: implement xxx_cpu_list for targets that still miss it */
4772#if defined(cpu_list)
4773 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004774#endif
ths15f82202007-06-29 23:26:08 +00004775 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004776 } else {
4777 cpu_model = optarg;
4778 }
4779 break;
bellardcd6f1162004-05-13 22:02:20 +00004780 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004781 initrd_filename = optarg;
4782 break;
bellardcd6f1162004-05-13 22:02:20 +00004783 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004784 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00004785 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004786 else
balrog609497a2008-01-14 02:56:53 +00004787 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004788 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004789 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004790 translation == BIOS_ATA_TRANSLATION_LBA ?
4791 ",trans=lba" :
4792 translation == BIOS_ATA_TRANSLATION_NONE ?
4793 ",trans=none" : "");
4794 break;
bellardcd6f1162004-05-13 22:02:20 +00004795 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004796 case QEMU_OPTION_hdc:
4797 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004798 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004799 break;
thse4bcb142007-12-02 04:51:10 +00004800 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004801 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004802 break;
balrog3e3d5812007-04-30 02:09:25 +00004803 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004804 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004805 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004806 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004807 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004808 break;
j_mayer86f55662007-04-24 06:52:59 +00004809 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004810 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004811 break;
bellardcd6f1162004-05-13 22:02:20 +00004812 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004813 snapshot = 1;
4814 break;
bellardcd6f1162004-05-13 22:02:20 +00004815 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004816 {
bellard330d0412003-07-26 18:11:40 +00004817 const char *p;
4818 p = optarg;
4819 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004820 if (cyls < 1 || cyls > 16383)
4821 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004822 if (*p != ',')
4823 goto chs_fail;
4824 p++;
4825 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004826 if (heads < 1 || heads > 16)
4827 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004828 if (*p != ',')
4829 goto chs_fail;
4830 p++;
4831 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004832 if (secs < 1 || secs > 63)
4833 goto chs_fail;
4834 if (*p == ',') {
4835 p++;
4836 if (!strcmp(p, "none"))
4837 translation = BIOS_ATA_TRANSLATION_NONE;
4838 else if (!strcmp(p, "lba"))
4839 translation = BIOS_ATA_TRANSLATION_LBA;
4840 else if (!strcmp(p, "auto"))
4841 translation = BIOS_ATA_TRANSLATION_AUTO;
4842 else
4843 goto chs_fail;
4844 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004845 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004846 fprintf(stderr, "qemu: invalid physical CHS format\n");
4847 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004848 }
thse4bcb142007-12-02 04:51:10 +00004849 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00004850 snprintf(drives_opt[hda_index].opt,
4851 sizeof(drives_opt[hda_index].opt),
4852 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4853 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004854 translation == BIOS_ATA_TRANSLATION_LBA ?
4855 ",trans=lba" :
4856 translation == BIOS_ATA_TRANSLATION_NONE ?
4857 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00004858 }
4859 break;
bellardcd6f1162004-05-13 22:02:20 +00004860 case QEMU_OPTION_nographic:
bellarda20dd502003-09-30 21:07:02 +00004861 nographic = 1;
4862 break;
balrog4d3b6f62008-02-10 16:33:14 +00004863#ifdef CONFIG_CURSES
4864 case QEMU_OPTION_curses:
4865 curses = 1;
4866 break;
4867#endif
balroga171fe32007-04-30 01:48:07 +00004868 case QEMU_OPTION_portrait:
4869 graphic_rotate = 1;
4870 break;
bellardcd6f1162004-05-13 22:02:20 +00004871 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004872 kernel_filename = optarg;
4873 break;
bellardcd6f1162004-05-13 22:02:20 +00004874 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004875 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004876 break;
bellardcd6f1162004-05-13 22:02:20 +00004877 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004878 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004879 break;
bellardcd6f1162004-05-13 22:02:20 +00004880 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004881 boot_devices = optarg;
4882 /* We just do some generic consistency checks */
4883 {
4884 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00004885 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00004886
4887 boot_devices_bitmap = 0;
4888 for (p = boot_devices; *p != '\0'; p++) {
4889 /* Allowed boot devices are:
4890 * a b : floppy disk drives
4891 * c ... f : IDE disk drives
4892 * g ... m : machine implementation dependant drives
4893 * n ... p : network devices
4894 * It's up to each machine implementation to check
4895 * if the given boot devices match the actual hardware
4896 * implementation and firmware features.
4897 */
4898 if (*p < 'a' || *p > 'q') {
4899 fprintf(stderr, "Invalid boot device '%c'\n", *p);
4900 exit(1);
4901 }
4902 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4903 fprintf(stderr,
4904 "Boot device '%c' was given twice\n",*p);
4905 exit(1);
4906 }
4907 boot_devices_bitmap |= 1 << (*p - 'a');
4908 }
bellard36b486b2003-11-11 13:36:08 +00004909 }
4910 break;
bellardcd6f1162004-05-13 22:02:20 +00004911 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004912 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004913 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004914 break;
bellard52ca8d62006-06-14 16:03:05 +00004915#ifdef TARGET_I386
4916 case QEMU_OPTION_no_fd_bootchk:
4917 fd_bootchk = 0;
4918 break;
4919#endif
bellard7c9d8e02005-11-15 22:16:05 +00004920 case QEMU_OPTION_net:
4921 if (nb_net_clients >= MAX_NET_CLIENTS) {
4922 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00004923 exit(1);
4924 }
pbrookfd5f3932008-03-26 20:55:43 +00004925 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00004926 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00004927 break;
bellardc7f74642004-08-24 21:57:12 +00004928#ifdef CONFIG_SLIRP
4929 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00004930 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004931 break;
ths47d5d012007-02-20 00:05:08 +00004932 case QEMU_OPTION_bootp:
4933 bootp_filename = optarg;
4934 break;
bellardc94c8d62004-09-13 21:37:34 +00004935#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004936 case QEMU_OPTION_smb:
4937 net_slirp_smb(optarg);
4938 break;
bellardc94c8d62004-09-13 21:37:34 +00004939#endif
bellard9bf05442004-08-25 22:12:49 +00004940 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00004941 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00004942 break;
bellardc7f74642004-08-24 21:57:12 +00004943#endif
balrogdc72ac12008-11-09 00:04:26 +00004944 case QEMU_OPTION_bt:
4945 if (nb_bt_opts >= MAX_BT_CMDLINE) {
4946 fprintf(stderr, "qemu: too many bluetooth options\n");
4947 exit(1);
4948 }
4949 bt_opts[nb_bt_opts++] = optarg;
4950 break;
bellard1d14ffa2005-10-30 18:58:22 +00004951#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004952 case QEMU_OPTION_audio_help:
4953 AUD_help ();
4954 exit (0);
4955 break;
4956 case QEMU_OPTION_soundhw:
4957 select_soundhw (optarg);
4958 break;
4959#endif
bellardcd6f1162004-05-13 22:02:20 +00004960 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00004961 help(0);
bellardcd6f1162004-05-13 22:02:20 +00004962 break;
aurel3200f82b82008-04-27 21:12:55 +00004963 case QEMU_OPTION_m: {
4964 uint64_t value;
4965 char *ptr;
4966
4967 value = strtoul(optarg, &ptr, 10);
4968 switch (*ptr) {
4969 case 0: case 'M': case 'm':
4970 value <<= 20;
4971 break;
4972 case 'G': case 'g':
4973 value <<= 30;
4974 break;
4975 default:
4976 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00004977 exit(1);
4978 }
aurel3200f82b82008-04-27 21:12:55 +00004979
4980 /* On 32-bit hosts, QEMU is limited by virtual address space */
4981 if (value > (2047 << 20)
4982#ifndef USE_KQEMU
4983 && HOST_LONG_BITS == 32
4984#endif
4985 ) {
4986 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4987 exit(1);
4988 }
4989 if (value != (uint64_t)(ram_addr_t)value) {
4990 fprintf(stderr, "qemu: ram size too large\n");
4991 exit(1);
4992 }
4993 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00004994 break;
aurel3200f82b82008-04-27 21:12:55 +00004995 }
bellardcd6f1162004-05-13 22:02:20 +00004996 case QEMU_OPTION_d:
4997 {
4998 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00004999 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005000
bellardcd6f1162004-05-13 22:02:20 +00005001 mask = cpu_str_to_log_mask(optarg);
5002 if (!mask) {
5003 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005004 for(item = cpu_log_items; item->mask != 0; item++) {
5005 printf("%-10s %s\n", item->name, item->help);
5006 }
5007 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005008 }
5009 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005010 }
bellardcd6f1162004-05-13 22:02:20 +00005011 break;
bellard67b915a2004-03-31 23:37:16 +00005012#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00005013 case QEMU_OPTION_s:
5014 use_gdbstub = 1;
5015 break;
5016 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00005017 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005018 break;
bellard67b915a2004-03-31 23:37:16 +00005019#endif
bellardcd6f1162004-05-13 22:02:20 +00005020 case QEMU_OPTION_L:
5021 bios_dir = optarg;
5022 break;
j_mayer1192dad2007-10-05 13:08:35 +00005023 case QEMU_OPTION_bios:
5024 bios_name = optarg;
5025 break;
bellardcd6f1162004-05-13 22:02:20 +00005026 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005027 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005028 break;
bellard3d11d0e2004-12-12 16:56:30 +00005029 case QEMU_OPTION_k:
5030 keyboard_layout = optarg;
5031 break;
bellardee22c2f2004-06-03 12:49:50 +00005032 case QEMU_OPTION_localtime:
5033 rtc_utc = 0;
5034 break;
malc3893c122008-09-28 00:42:05 +00005035 case QEMU_OPTION_vga:
5036 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005037 break;
bellarde9b137c2004-06-21 16:46:10 +00005038 case QEMU_OPTION_g:
5039 {
5040 const char *p;
5041 int w, h, depth;
5042 p = optarg;
5043 w = strtol(p, (char **)&p, 10);
5044 if (w <= 0) {
5045 graphic_error:
5046 fprintf(stderr, "qemu: invalid resolution or depth\n");
5047 exit(1);
5048 }
5049 if (*p != 'x')
5050 goto graphic_error;
5051 p++;
5052 h = strtol(p, (char **)&p, 10);
5053 if (h <= 0)
5054 goto graphic_error;
5055 if (*p == 'x') {
5056 p++;
5057 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005058 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005059 depth != 24 && depth != 32)
5060 goto graphic_error;
5061 } else if (*p == '\0') {
5062 depth = graphic_depth;
5063 } else {
5064 goto graphic_error;
5065 }
ths3b46e622007-09-17 08:09:54 +00005066
bellarde9b137c2004-06-21 16:46:10 +00005067 graphic_width = w;
5068 graphic_height = h;
5069 graphic_depth = depth;
5070 }
5071 break;
ths20d8a3e2007-02-18 17:04:49 +00005072 case QEMU_OPTION_echr:
5073 {
5074 char *r;
5075 term_escape_char = strtol(optarg, &r, 0);
5076 if (r == optarg)
5077 printf("Bad argument to echr\n");
5078 break;
5079 }
bellard82c643f2004-07-14 17:28:13 +00005080 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00005081 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00005082 break;
5083 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005084 if (serial_device_index >= MAX_SERIAL_PORTS) {
5085 fprintf(stderr, "qemu: too many serial ports\n");
5086 exit(1);
5087 }
pbrookfd5f3932008-03-26 20:55:43 +00005088 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005089 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005090 break;
aliguori51ecf132009-01-15 20:06:40 +00005091 case QEMU_OPTION_virtiocon:
5092 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5093 fprintf(stderr, "qemu: too many virtio consoles\n");
5094 exit(1);
5095 }
5096 virtio_consoles[virtio_console_index] = optarg;
5097 virtio_console_index++;
5098 break;
bellard6508fe52005-01-15 12:02:56 +00005099 case QEMU_OPTION_parallel:
5100 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5101 fprintf(stderr, "qemu: too many parallel ports\n");
5102 exit(1);
5103 }
pbrookfd5f3932008-03-26 20:55:43 +00005104 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005105 parallel_device_index++;
5106 break;
bellardd63d3072004-10-03 13:29:03 +00005107 case QEMU_OPTION_loadvm:
5108 loadvm = optarg;
5109 break;
5110 case QEMU_OPTION_full_screen:
5111 full_screen = 1;
5112 break;
ths667acca2006-12-11 02:08:05 +00005113#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005114 case QEMU_OPTION_no_frame:
5115 no_frame = 1;
5116 break;
ths3780e192007-06-21 21:08:02 +00005117 case QEMU_OPTION_alt_grab:
5118 alt_grab = 1;
5119 break;
ths667acca2006-12-11 02:08:05 +00005120 case QEMU_OPTION_no_quit:
5121 no_quit = 1;
5122 break;
aliguori7d957bd2009-01-15 22:14:11 +00005123 case QEMU_OPTION_sdl:
5124 sdl = 1;
5125 break;
ths667acca2006-12-11 02:08:05 +00005126#endif
bellardf7cce892004-12-08 22:21:25 +00005127 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005128 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005129 break;
bellarda09db212005-04-30 16:10:35 +00005130#ifdef TARGET_I386
5131 case QEMU_OPTION_win2k_hack:
5132 win2k_install_hack = 1;
5133 break;
aliguori73822ec2009-01-15 20:11:34 +00005134 case QEMU_OPTION_rtc_td_hack:
5135 rtc_td_hack = 1;
5136 break;
aliguori8a92ea22009-02-27 20:12:36 +00005137 case QEMU_OPTION_acpitable:
5138 if(acpi_table_add(optarg) < 0) {
5139 fprintf(stderr, "Wrong acpi table provided\n");
5140 exit(1);
5141 }
5142 break;
bellarda09db212005-04-30 16:10:35 +00005143#endif
bellardd993e022005-02-10 22:00:06 +00005144#ifdef USE_KQEMU
5145 case QEMU_OPTION_no_kqemu:
5146 kqemu_allowed = 0;
5147 break;
bellard89bfc102006-02-08 22:46:31 +00005148 case QEMU_OPTION_kernel_kqemu:
5149 kqemu_allowed = 2;
5150 break;
bellardd993e022005-02-10 22:00:06 +00005151#endif
aliguori7ba1e612008-11-05 16:04:33 +00005152#ifdef CONFIG_KVM
5153 case QEMU_OPTION_enable_kvm:
5154 kvm_allowed = 1;
5155#ifdef USE_KQEMU
5156 kqemu_allowed = 0;
5157#endif
5158 break;
5159#endif
bellardbb36d472005-11-05 14:22:28 +00005160 case QEMU_OPTION_usb:
5161 usb_enabled = 1;
5162 break;
bellarda594cfb2005-11-06 16:13:29 +00005163 case QEMU_OPTION_usbdevice:
5164 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00005165 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00005166 fprintf(stderr, "Too many USB devices\n");
5167 exit(1);
5168 }
pbrookfd5f3932008-03-26 20:55:43 +00005169 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00005170 usb_devices_index++;
5171 break;
bellard6a00d602005-11-21 23:25:50 +00005172 case QEMU_OPTION_smp:
5173 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00005174 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005175 fprintf(stderr, "Invalid number of CPUs\n");
5176 exit(1);
5177 }
5178 break;
bellard24236862006-04-30 21:28:36 +00005179 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00005180 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005181 break;
bellard6515b202006-05-03 22:02:44 +00005182 case QEMU_OPTION_no_acpi:
5183 acpi_enabled = 0;
5184 break;
aliguori16b29ae2008-12-17 23:28:44 +00005185 case QEMU_OPTION_no_hpet:
5186 no_hpet = 1;
5187 break;
bellardd1beab82006-10-02 19:44:22 +00005188 case QEMU_OPTION_no_reboot:
5189 no_reboot = 1;
5190 break;
aurel32b2f76162008-04-11 21:35:52 +00005191 case QEMU_OPTION_no_shutdown:
5192 no_shutdown = 1;
5193 break;
balrog9467cd42007-05-01 01:34:14 +00005194 case QEMU_OPTION_show_cursor:
5195 cursor_hide = 0;
5196 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005197 case QEMU_OPTION_uuid:
5198 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5199 fprintf(stderr, "Fail to parse UUID string."
5200 " Wrong format.\n");
5201 exit(1);
5202 }
5203 break;
ths71e3ceb2006-12-22 02:11:31 +00005204 case QEMU_OPTION_daemonize:
5205 daemonize = 1;
5206 break;
ths9ae02552007-01-05 17:39:04 +00005207 case QEMU_OPTION_option_rom:
5208 if (nb_option_roms >= MAX_OPTION_ROMS) {
5209 fprintf(stderr, "Too many option ROMs\n");
5210 exit(1);
5211 }
5212 option_rom[nb_option_roms] = optarg;
5213 nb_option_roms++;
5214 break;
pbrook8e716212007-01-20 17:12:09 +00005215 case QEMU_OPTION_semihosting:
5216 semihosting_enabled = 1;
5217 break;
thsc35734b2007-03-19 15:17:08 +00005218 case QEMU_OPTION_name:
5219 qemu_name = optarg;
5220 break;
blueswir195efd112008-12-24 20:26:14 +00005221#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005222 case QEMU_OPTION_prom_env:
5223 if (nb_prom_envs >= MAX_PROM_ENVS) {
5224 fprintf(stderr, "Too many prom variables\n");
5225 exit(1);
5226 }
5227 prom_envs[nb_prom_envs] = optarg;
5228 nb_prom_envs++;
5229 break;
5230#endif
balrog2b8f2d42007-07-27 22:08:46 +00005231#ifdef TARGET_ARM
5232 case QEMU_OPTION_old_param:
5233 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005234 break;
balrog2b8f2d42007-07-27 22:08:46 +00005235#endif
thsf3dcfad2007-08-24 01:26:02 +00005236 case QEMU_OPTION_clock:
5237 configure_alarms(optarg);
5238 break;
bellard7e0af5d02007-11-07 16:24:33 +00005239 case QEMU_OPTION_startdate:
5240 {
5241 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005242 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005243 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005244 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005245 } else {
5246 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5247 &tm.tm_year,
5248 &tm.tm_mon,
5249 &tm.tm_mday,
5250 &tm.tm_hour,
5251 &tm.tm_min,
5252 &tm.tm_sec) == 6) {
5253 /* OK */
5254 } else if (sscanf(optarg, "%d-%d-%d",
5255 &tm.tm_year,
5256 &tm.tm_mon,
5257 &tm.tm_mday) == 3) {
5258 tm.tm_hour = 0;
5259 tm.tm_min = 0;
5260 tm.tm_sec = 0;
5261 } else {
5262 goto date_fail;
5263 }
5264 tm.tm_year -= 1900;
5265 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005266 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005267 if (rtc_start_date == -1) {
5268 date_fail:
5269 fprintf(stderr, "Invalid date format. Valid format are:\n"
5270 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5271 exit(1);
5272 }
balrogf6503052008-02-17 11:42:19 +00005273 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005274 }
5275 }
5276 break;
bellard26a5f132008-05-28 12:30:31 +00005277 case QEMU_OPTION_tb_size:
5278 tb_size = strtol(optarg, NULL, 0);
5279 if (tb_size < 0)
5280 tb_size = 0;
5281 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005282 case QEMU_OPTION_icount:
5283 use_icount = 1;
5284 if (strcmp(optarg, "auto") == 0) {
5285 icount_time_shift = -1;
5286 } else {
5287 icount_time_shift = strtol(optarg, NULL, 0);
5288 }
5289 break;
aliguori5bb79102008-10-13 03:12:02 +00005290 case QEMU_OPTION_incoming:
5291 incoming = optarg;
5292 break;
aliguori08585322009-02-27 22:09:45 +00005293 case QEMU_OPTION_chroot:
5294 chroot_dir = optarg;
5295 break;
5296 case QEMU_OPTION_runas:
5297 run_as = optarg;
5298 break;
bellardcd6f1162004-05-13 22:02:20 +00005299 }
bellard0824d6f2003-06-24 13:42:40 +00005300 }
5301 }
bellard330d0412003-07-26 18:11:40 +00005302
aliguori7ba1e612008-11-05 16:04:33 +00005303#if defined(CONFIG_KVM) && defined(USE_KQEMU)
5304 if (kvm_allowed && kqemu_allowed) {
5305 fprintf(stderr,
5306 "You can not enable both KVM and kqemu at the same time\n");
5307 exit(1);
5308 }
5309#endif
5310
balrog3d878ca2008-10-28 10:59:59 +00005311 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005312 if (smp_cpus > machine->max_cpus) {
5313 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5314 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5315 machine->max_cpus);
5316 exit(1);
5317 }
5318
aliguoribc0129d2008-08-01 15:12:34 +00005319 if (nographic) {
5320 if (serial_device_index == 0)
5321 serial_devices[0] = "stdio";
5322 if (parallel_device_index == 0)
5323 parallel_devices[0] = "null";
5324 if (strncmp(monitor_device, "vc", 2) == 0)
5325 monitor_device = "stdio";
5326 }
5327
ths71e3ceb2006-12-22 02:11:31 +00005328#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005329 if (daemonize) {
5330 pid_t pid;
5331
5332 if (pipe(fds) == -1)
5333 exit(1);
5334
5335 pid = fork();
5336 if (pid > 0) {
5337 uint8_t status;
5338 ssize_t len;
5339
5340 close(fds[1]);
5341
5342 again:
ths93815bc2007-03-19 15:58:31 +00005343 len = read(fds[0], &status, 1);
5344 if (len == -1 && (errno == EINTR))
5345 goto again;
5346
5347 if (len != 1)
5348 exit(1);
5349 else if (status == 1) {
5350 fprintf(stderr, "Could not acquire pidfile\n");
5351 exit(1);
5352 } else
5353 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005354 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005355 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005356
5357 setsid();
5358
5359 pid = fork();
5360 if (pid > 0)
5361 exit(0);
5362 else if (pid < 0)
5363 exit(1);
5364
5365 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005366
5367 signal(SIGTSTP, SIG_IGN);
5368 signal(SIGTTOU, SIG_IGN);
5369 signal(SIGTTIN, SIG_IGN);
5370 }
5371#endif
5372
thsaa26bb22007-03-25 21:33:06 +00005373 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005374 if (daemonize) {
5375 uint8_t status = 1;
5376 write(fds[1], &status, 1);
5377 } else
5378 fprintf(stderr, "Could not acquire pid file\n");
5379 exit(1);
5380 }
5381
bellardff3fbb32006-01-08 10:53:14 +00005382#ifdef USE_KQEMU
5383 if (smp_cpus > 1)
5384 kqemu_allowed = 0;
5385#endif
bellarda20dd502003-09-30 21:07:02 +00005386 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00005387 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00005388
j_mayer28c5af52007-11-11 01:50:45 +00005389 if (!linux_boot && net_boot == 0 &&
blueswir1f88e4b92008-08-12 15:58:35 +00005390 !machine->nodisk_ok && nb_drives_opt == 0)
ths15f82202007-06-29 23:26:08 +00005391 help(1);
bellard0824d6f2003-06-24 13:42:40 +00005392
thsf8d39c02008-07-03 10:01:15 +00005393 if (!linux_boot && *kernel_cmdline != '\0') {
5394 fprintf(stderr, "-append only allowed with -kernel option\n");
5395 exit(1);
5396 }
5397
5398 if (!linux_boot && initrd_filename != NULL) {
5399 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5400 exit(1);
5401 }
5402
ths96d30e42007-01-07 20:42:14 +00005403 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00005404 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00005405 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00005406 }
bellardb118d612003-06-30 23:36:21 +00005407 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005408
pbrook634fce92006-07-15 17:40:09 +00005409 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005410 if (init_timer_alarm() < 0) {
5411 fprintf(stderr, "could not initialize alarm timer\n");
5412 exit(1);
5413 }
pbrook2e70f6e2008-06-29 01:03:05 +00005414 if (use_icount && icount_time_shift < 0) {
5415 use_icount = 2;
5416 /* 125MIPS seems a reasonable initial guess at the guest speed.
5417 It will be corrected fairly quickly anyway. */
5418 icount_time_shift = 3;
5419 init_icount_adjust();
5420 }
pbrook634fce92006-07-15 17:40:09 +00005421
bellardfd1dff42006-02-01 21:29:26 +00005422#ifdef _WIN32
5423 socket_init();
5424#endif
5425
bellard7c9d8e02005-11-15 22:16:05 +00005426 /* init network clients */
5427 if (nb_net_clients == 0) {
5428 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005429 net_clients[nb_net_clients++] = "nic";
5430#ifdef CONFIG_SLIRP
5431 net_clients[nb_net_clients++] = "user";
5432#endif
bellardc20709a2004-04-21 23:27:19 +00005433 }
5434
bellard7c9d8e02005-11-15 22:16:05 +00005435 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005436 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005437 exit(1);
bellard702c6512004-04-02 21:21:32 +00005438 }
aliguori63a01ef2008-10-31 19:10:00 +00005439 net_client_check();
bellardf1510b22003-06-25 00:07:40 +00005440
thseec85c22007-01-05 17:41:07 +00005441#ifdef TARGET_I386
balroged494d82007-12-11 23:23:52 +00005442 /* XXX: this should be moved in the PC machine instantiation code */
j_mayer28c5af52007-11-11 01:50:45 +00005443 if (net_boot != 0) {
5444 int netroms = 0;
5445 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00005446 const char *model = nd_table[i].model;
5447 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00005448 if (net_boot & (1 << i)) {
5449 if (model == NULL)
5450 model = "ne2k_pci";
5451 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5452 if (get_image_size(buf) > 0) {
5453 if (nb_option_roms >= MAX_OPTION_ROMS) {
5454 fprintf(stderr, "Too many option ROMs\n");
5455 exit(1);
5456 }
5457 option_rom[nb_option_roms] = strdup(buf);
5458 nb_option_roms++;
5459 netroms++;
5460 }
5461 }
thseec85c22007-01-05 17:41:07 +00005462 }
j_mayer28c5af52007-11-11 01:50:45 +00005463 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00005464 fprintf(stderr, "No valid PXE rom found for network device\n");
5465 exit(1);
5466 }
thseec85c22007-01-05 17:41:07 +00005467 }
5468#endif
5469
balrogdc72ac12008-11-09 00:04:26 +00005470 /* init the bluetooth world */
5471 for (i = 0; i < nb_bt_opts; i++)
5472 if (bt_parse(bt_opts[i]))
5473 exit(1);
5474
bellard0824d6f2003-06-24 13:42:40 +00005475 /* init the memory */
balrog7fb4fdc2008-04-24 17:59:27 +00005476 phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
5477
5478 if (machine->ram_require & RAMSIZE_FIXED) {
5479 if (ram_size > 0) {
5480 if (ram_size < phys_ram_size) {
aurel32cd940062008-04-28 20:26:54 +00005481 fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
5482 machine->name, (unsigned long long) phys_ram_size);
balrog7fb4fdc2008-04-24 17:59:27 +00005483 exit(-1);
5484 }
5485
5486 phys_ram_size = ram_size;
5487 } else
5488 ram_size = phys_ram_size;
5489 } else {
aurel324fc5d072008-04-27 21:39:40 +00005490 if (ram_size == 0)
balrog7fb4fdc2008-04-24 17:59:27 +00005491 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5492
5493 phys_ram_size += ram_size;
5494 }
ths9ae02552007-01-05 17:39:04 +00005495
bellardd993e022005-02-10 22:00:06 +00005496 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00005497 if (!phys_ram_base) {
5498 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00005499 exit(1);
5500 }
5501
bellard26a5f132008-05-28 12:30:31 +00005502 /* init the dynamic translator */
5503 cpu_exec_init_all(tb_size * 1024 * 1024);
5504
bellard5905b2e2004-08-01 21:53:26 +00005505 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005506
5507 /* we always create the cdrom drive, even if no disk is there */
5508
5509 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005510 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005511
balrog9d413d12007-12-04 00:10:34 +00005512 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005513
5514 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005515 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005516
balrog9d413d12007-12-04 00:10:34 +00005517 /* we always create one sd slot, even if no card is in it */
5518
5519 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005520 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005521
ths96d30e42007-01-07 20:42:14 +00005522 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005523
thse4bcb142007-12-02 04:51:10 +00005524 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005525 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005526 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005527
bellardc88676f2006-08-06 13:36:11 +00005528 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005529 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005530
aliguori3023f332009-01-16 19:04:14 +00005531#ifndef _WIN32
5532 /* must be after terminal init, SDL library changes signal handlers */
5533 termsig_setup();
5534#endif
5535
5536 /* Maintain compatibility with multiple stdio monitors */
5537 if (!strcmp(monitor_device,"stdio")) {
5538 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5539 const char *devname = serial_devices[i];
5540 if (devname && !strcmp(devname,"mon:stdio")) {
5541 monitor_device = NULL;
5542 break;
5543 } else if (devname && !strcmp(devname,"stdio")) {
5544 monitor_device = NULL;
5545 serial_devices[i] = "mon:stdio";
5546 break;
5547 }
5548 }
5549 }
5550
5551 if (kvm_enabled()) {
5552 int ret;
5553
5554 ret = kvm_init(smp_cpus);
5555 if (ret < 0) {
5556 fprintf(stderr, "failed to initialize KVM\n");
5557 exit(1);
5558 }
5559 }
5560
aliguori4c621802009-01-16 21:48:20 +00005561 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005562 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005563 if (!monitor_hd) {
5564 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5565 exit(1);
5566 }
5567 }
5568
aliguori2796dae2009-01-16 20:23:27 +00005569 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5570 const char *devname = serial_devices[i];
5571 if (devname && strcmp(devname, "none")) {
5572 char label[32];
5573 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005574 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005575 if (!serial_hds[i]) {
5576 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5577 devname);
5578 exit(1);
5579 }
5580 }
5581 }
5582
5583 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5584 const char *devname = parallel_devices[i];
5585 if (devname && strcmp(devname, "none")) {
5586 char label[32];
5587 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005588 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005589 if (!parallel_hds[i]) {
5590 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5591 devname);
5592 exit(1);
5593 }
5594 }
5595 }
5596
5597 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5598 const char *devname = virtio_consoles[i];
5599 if (devname && strcmp(devname, "none")) {
5600 char label[32];
5601 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005602 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005603 if (!virtcon_hds[i]) {
5604 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5605 devname);
5606 exit(1);
5607 }
5608 }
5609 }
5610
aliguori3023f332009-01-16 19:04:14 +00005611 machine->init(ram_size, vga_ram_size, boot_devices,
5612 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5613
aliguori6f338c32009-02-11 15:21:54 +00005614 current_machine = machine;
5615
aliguori3023f332009-01-16 19:04:14 +00005616 /* Set KVM's vcpu state to qemu's initial CPUState. */
5617 if (kvm_enabled()) {
5618 int ret;
5619
5620 ret = kvm_sync_vcpus();
5621 if (ret < 0) {
5622 fprintf(stderr, "failed to initialize vcpus\n");
5623 exit(1);
5624 }
5625 }
5626
5627 /* init USB devices */
5628 if (usb_enabled) {
5629 for(i = 0; i < usb_devices_index; i++) {
aliguoric0f4ce72009-03-05 23:01:01 +00005630 if (usb_device_add(usb_devices[i], 0) < 0) {
aliguori3023f332009-01-16 19:04:14 +00005631 fprintf(stderr, "Warning: could not add USB device %s\n",
5632 usb_devices[i]);
5633 }
5634 }
5635 }
5636
aliguori8f391ab2009-01-19 16:34:10 +00005637 if (!display_state)
5638 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005639 /* just use the first displaystate for the moment */
5640 ds = display_state;
bellard313aa562003-08-10 21:52:11 +00005641 /* terminal init */
bellarda20dd502003-09-30 21:07:02 +00005642 if (nographic) {
balrog4d3b6f62008-02-10 16:33:14 +00005643 if (curses) {
5644 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5645 exit(1);
5646 }
aliguori7d957bd2009-01-15 22:14:11 +00005647 } else {
balrog4d3b6f62008-02-10 16:33:14 +00005648#if defined(CONFIG_CURSES)
aliguori7d957bd2009-01-15 22:14:11 +00005649 if (curses) {
5650 /* At the moment curses cannot be used with other displays */
5651 curses_display_init(ds, full_screen);
5652 } else
balrog4d3b6f62008-02-10 16:33:14 +00005653#endif
aliguori7d957bd2009-01-15 22:14:11 +00005654 {
5655 if (vnc_display != NULL) {
5656 vnc_display_init(ds);
5657 if (vnc_display_open(ds, vnc_display) < 0)
5658 exit(1);
5659 }
bellard5b0753e2005-03-01 21:37:28 +00005660#if defined(CONFIG_SDL)
aliguorid268de02009-01-22 16:18:33 +00005661 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005662 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00005663#elif defined(CONFIG_COCOA)
aliguorid268de02009-01-22 16:18:33 +00005664 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005665 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00005666#endif
aliguori7d957bd2009-01-15 22:14:11 +00005667 }
bellard313aa562003-08-10 21:52:11 +00005668 }
aliguori7d957bd2009-01-15 22:14:11 +00005669 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005670
aliguori3023f332009-01-16 19:04:14 +00005671 dcl = ds->listeners;
5672 while (dcl != NULL) {
5673 if (dcl->dpy_refresh != NULL) {
5674 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5675 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005676 }
aliguori3023f332009-01-16 19:04:14 +00005677 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005678 }
aliguori3023f332009-01-16 19:04:14 +00005679
blueswir19043b622009-01-21 19:28:13 +00005680 if (nographic || (vnc_display && !sdl)) {
5681 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5682 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5683 }
5684
aliguori2796dae2009-01-16 20:23:27 +00005685 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00005686 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00005687
aliguori4c621802009-01-16 21:48:20 +00005688 if (monitor_device && monitor_hd)
aliguoricde76ee2009-03-05 23:01:51 +00005689 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
bellard82c643f2004-07-14 17:28:13 +00005690
bellard8d11df92004-08-24 21:13:40 +00005691 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005692 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005693 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005694 char label[32];
5695 snprintf(label, sizeof(label), "serial%d", i);
thsaf3a9032007-07-11 23:14:59 +00005696 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005697 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005698 }
bellard82c643f2004-07-14 17:28:13 +00005699 }
bellard82c643f2004-07-14 17:28:13 +00005700
bellard6508fe52005-01-15 12:02:56 +00005701 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005702 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005703 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005704 char label[32];
5705 snprintf(label, sizeof(label), "parallel%d", i);
thsaf3a9032007-07-11 23:14:59 +00005706 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005707 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005708 }
5709 }
5710
aliguori9ede2fd2009-01-15 20:05:25 +00005711 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5712 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005713 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005714 char label[32];
5715 snprintf(label, sizeof(label), "virtcon%d", i);
aliguori9ede2fd2009-01-15 20:05:25 +00005716 if (strstart(devname, "vc", 0))
5717 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5718 }
5719 }
5720
bellard67b915a2004-03-31 23:37:16 +00005721#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00005722 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00005723 /* XXX: use standard host:port notation and modify options
5724 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00005725 if (gdbserver_start(gdbstub_port) < 0) {
5726 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00005727 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00005728 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00005729 }
balrog45669e02007-07-02 13:20:17 +00005730 }
bellard67b915a2004-03-31 23:37:16 +00005731#endif
balrog45669e02007-07-02 13:20:17 +00005732
bellardd63d3072004-10-03 13:29:03 +00005733 if (loadvm)
aliguori376253e2009-03-05 23:01:23 +00005734 do_loadvm(cur_mon, loadvm);
bellardd63d3072004-10-03 13:29:03 +00005735
aliguori5bb79102008-10-13 03:12:02 +00005736 if (incoming) {
5737 autostart = 0; /* fixme how to deal with -daemonize */
5738 qemu_start_incoming_migration(incoming);
5739 }
5740
aliguoric0f4ce72009-03-05 23:01:01 +00005741 if (autostart)
5742 vm_start();
thsffd843b2006-12-21 19:46:43 +00005743
ths71e3ceb2006-12-22 02:11:31 +00005744 if (daemonize) {
5745 uint8_t status = 0;
5746 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005747
5748 again1:
5749 len = write(fds[1], &status, 1);
5750 if (len == -1 && (errno == EINTR))
5751 goto again1;
5752
5753 if (len != 1)
5754 exit(1);
5755
aliguoribd54b862008-07-23 00:58:33 +00005756 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005757 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005758 if (fd == -1)
5759 exit(1);
aliguori08585322009-02-27 22:09:45 +00005760 }
ths71e3ceb2006-12-22 02:11:31 +00005761
aliguori08585322009-02-27 22:09:45 +00005762#ifndef _WIN32
5763 if (run_as) {
5764 pwd = getpwnam(run_as);
5765 if (!pwd) {
5766 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5767 exit(1);
5768 }
5769 }
ths71e3ceb2006-12-22 02:11:31 +00005770
aliguori08585322009-02-27 22:09:45 +00005771 if (chroot_dir) {
5772 if (chroot(chroot_dir) < 0) {
5773 fprintf(stderr, "chroot failed\n");
5774 exit(1);
5775 }
5776 chdir("/");
5777 }
5778
5779 if (run_as) {
5780 if (setgid(pwd->pw_gid) < 0) {
5781 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5782 exit(1);
5783 }
5784 if (setuid(pwd->pw_uid) < 0) {
5785 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5786 exit(1);
5787 }
5788 if (setuid(0) != -1) {
5789 fprintf(stderr, "Dropping privileges failed\n");
5790 exit(1);
5791 }
5792 }
5793#endif
5794
5795 if (daemonize) {
5796 dup2(fd, 0);
5797 dup2(fd, 1);
5798 dup2(fd, 2);
5799
5800 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00005801 }
5802
bellard8a7ddc32004-03-31 19:00:16 +00005803 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005804 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005805 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005806
bellard0824d6f2003-06-24 13:42:40 +00005807 return 0;
5808}