blob: 3828bfd7aeb370bcda5627aabf174228a8d6933d [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"
aurel325ef4efa2009-03-10 21:43:35 +0000141#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000142#include "net.h"
143#include "monitor.h"
144#include "console.h"
145#include "sysemu.h"
146#include "gdbstub.h"
147#include "qemu-timer.h"
148#include "qemu-char.h"
149#include "cache-utils.h"
150#include "block.h"
blueswir1a718ace2009-03-28 08:24:44 +0000151#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000152#include "audio/audio.h"
153#include "migration.h"
154#include "kvm.h"
155#include "balloon.h"
156
bellard0824d6f2003-06-24 13:42:40 +0000157#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000158
bellard8a7ddc32004-03-31 19:00:16 +0000159#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000160
blueswir1511d2b12009-03-07 15:32:56 +0000161#include "qemu_socket.h"
162
163#if defined(CONFIG_SLIRP)
164#include "libslirp.h"
165#endif
166
bellard0824d6f2003-06-24 13:42:40 +0000167//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000168//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000169//#define DEBUG_NET
170//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000171
aliguorid12d51d2009-01-15 21:48:06 +0000172
173#ifdef DEBUG_IOPORT
aliguori93fcfe32009-01-15 22:34:14 +0000174# define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
aliguorid12d51d2009-01-15 21:48:06 +0000175#else
176# define LOG_IOPORT(...) do { } while (0)
177#endif
178
bellard1bfe8562004-07-08 21:17:50 +0000179#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000180
pbrook0d92ed32006-05-21 16:30:15 +0000181/* Max number of USB devices that can be specified on the commandline. */
182#define MAX_USB_CMDLINE 8
183
balrogdc72ac12008-11-09 00:04:26 +0000184/* Max number of bluetooth switches on the commandline. */
185#define MAX_BT_CMDLINE 10
186
bellard7dea1da2003-11-16 15:59:30 +0000187/* XXX: use a two level table to limit memory usage */
188#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000189
bellard80cabfa2004-03-14 12:20:30 +0000190const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000191const char *bios_name = NULL;
blueswir1dbed7e42008-10-01 19:38:09 +0000192static void *ioport_opaque[MAX_IOPORTS];
193static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
194static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
thse4bcb142007-12-02 04:51:10 +0000195/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000196 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000197DriveInfo drives_table[MAX_DRIVES+1];
198int nb_drives;
blueswir1dbed7e42008-10-01 19:38:09 +0000199static int vga_ram_size;
malccb5a7aa2008-09-28 00:42:12 +0000200enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f3322009-01-16 19:04:14 +0000201static DisplayState *display_state;
bellarda20dd502003-09-30 21:07:02 +0000202int nographic;
blueswir1dbed7e42008-10-01 19:38:09 +0000203static int curses;
aliguori7d957bd2009-01-15 22:14:11 +0000204static int sdl;
bellard3d11d0e2004-12-12 16:56:30 +0000205const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000206int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000207ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000208int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000209NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000210int vm_running;
aliguoric0f4ce72009-03-05 23:01:01 +0000211static int autostart;
balrogf6503052008-02-17 11:42:19 +0000212static int rtc_utc = 1;
213static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000214int cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +0000215int std_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000216int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000217#ifdef TARGET_SPARC
218int graphic_width = 1024;
219int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000220int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000221#else
bellard1bfe8562004-07-08 21:17:50 +0000222int graphic_width = 800;
223int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000224int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000225#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000226static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000227#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000228static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000229#endif
ths667acca2006-12-11 02:08:05 +0000230int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000231CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000232CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000233CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000234#ifdef TARGET_I386
235int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000236int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000237#endif
bellardbb36d472005-11-05 14:22:28 +0000238int usb_enabled = 0;
bellard6a00d602005-11-21 23:25:50 +0000239int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000240const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000241int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000242int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000243int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000244int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000245int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000246int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000247int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000248int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000249const char *option_rom[MAX_OPTION_ROMS];
250int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000251int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000252#ifdef TARGET_ARM
253int old_param = 0;
254#endif
thsc35734b2007-03-19 15:17:08 +0000255const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000256int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000257#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000258unsigned int nb_prom_envs = 0;
259const char *prom_envs[MAX_PROM_ENVS];
260#endif
aliguoriec691c82009-02-11 15:20:37 +0000261int nb_drives_opt;
262struct drive_opt drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000263
balrogee5605e2007-12-03 03:01:40 +0000264static CPUState *cur_cpu;
265static CPUState *next_cpu;
balrog76ea08f2007-12-16 11:48:54 +0000266static int event_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000267/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000268static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000269/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000270#define MAX_ICOUNT_SHIFT 10
271/* Compensate for varying guest execution speed. */
272static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000273static QEMUTimer *icount_rt_timer;
274static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000275static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000276
blueswir18fcb1b92008-09-18 18:29:08 +0000277uint8_t qemu_uuid[16];
278
bellard0824d6f2003-06-24 13:42:40 +0000279/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000280/* x86 ISA bus support */
281
282target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000283PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000284
aliguori477e3ed2008-07-23 15:19:59 +0000285static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
286static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
287
288static uint32_t ioport_read(int index, uint32_t address)
289{
290 static IOPortReadFunc *default_func[3] = {
291 default_ioport_readb,
292 default_ioport_readw,
293 default_ioport_readl
294 };
295 IOPortReadFunc *func = ioport_read_table[index][address];
296 if (!func)
297 func = default_func[index];
298 return func(ioport_opaque[address], address);
299}
300
301static void ioport_write(int index, uint32_t address, uint32_t data)
302{
303 static IOPortWriteFunc *default_func[3] = {
304 default_ioport_writeb,
305 default_ioport_writew,
306 default_ioport_writel
307 };
308 IOPortWriteFunc *func = ioport_write_table[index][address];
309 if (!func)
310 func = default_func[index];
311 func(ioport_opaque[address], address, data);
312}
313
pbrook9596ebb2007-11-18 01:44:38 +0000314static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000315{
316#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000317 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000318#endif
bellardfc01f7e2003-06-30 10:03:06 +0000319 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000320}
321
pbrook9596ebb2007-11-18 01:44:38 +0000322static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000323{
324#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000325 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000326#endif
327}
328
329/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000330static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000331{
332 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000333 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000334 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000335 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000336 return data;
337}
338
pbrook9596ebb2007-11-18 01:44:38 +0000339static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000340{
aliguori477e3ed2008-07-23 15:19:59 +0000341 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000342 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000343 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000344}
345
pbrook9596ebb2007-11-18 01:44:38 +0000346static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000347{
348#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000349 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000350#endif
351 return 0xffffffff;
352}
353
pbrook9596ebb2007-11-18 01:44:38 +0000354static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000355{
356#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000357 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000358#endif
bellard0824d6f2003-06-24 13:42:40 +0000359}
360
bellardfc01f7e2003-06-30 10:03:06 +0000361/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000362int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000363 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000364{
bellardfc01f7e2003-06-30 10:03:06 +0000365 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000366
bellardc4b1fcc2004-03-14 21:44:30 +0000367 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000368 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000369 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000370 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000371 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000372 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000373 } else {
balrog88fdf562008-04-26 21:11:22 +0000374 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000375 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000376 }
377 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000378 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000379 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000380 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000381 ioport_opaque[i] = opaque;
382 }
bellard0824d6f2003-06-24 13:42:40 +0000383 return 0;
384}
385
bellardfc01f7e2003-06-30 10:03:06 +0000386/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000387int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000388 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000389{
bellardfc01f7e2003-06-30 10:03:06 +0000390 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000391
bellardc4b1fcc2004-03-14 21:44:30 +0000392 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000393 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000394 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000395 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000396 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000397 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000398 } else {
balrog88fdf562008-04-26 21:11:22 +0000399 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000400 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000401 }
402 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000403 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000404 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
405 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000406 ioport_opaque[i] = opaque;
407 }
bellardf1510b22003-06-25 00:07:40 +0000408 return 0;
409}
410
bellard69b91032004-05-18 23:05:28 +0000411void isa_unassign_ioport(int start, int length)
412{
413 int i;
414
415 for(i = start; i < start + length; i++) {
416 ioport_read_table[0][i] = default_ioport_readb;
417 ioport_read_table[1][i] = default_ioport_readw;
418 ioport_read_table[2][i] = default_ioport_readl;
419
420 ioport_write_table[0][i] = default_ioport_writeb;
421 ioport_write_table[1][i] = default_ioport_writew;
422 ioport_write_table[2][i] = default_ioport_writel;
aliguoria7607f72009-02-11 15:21:29 +0000423
424 ioport_opaque[i] = NULL;
bellard69b91032004-05-18 23:05:28 +0000425 }
426}
427
bellard20f32282005-01-03 23:36:21 +0000428/***********************************************************/
429
bellardc45886d2004-01-05 00:02:06 +0000430void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000431{
aliguorid12d51d2009-01-15 21:48:06 +0000432 LOG_IOPORT("outb: %04x %02x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000433 ioport_write(0, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000434#ifdef USE_KQEMU
435 if (env)
436 env->last_io_time = cpu_get_time_fast();
437#endif
bellard0824d6f2003-06-24 13:42:40 +0000438}
439
bellardc45886d2004-01-05 00:02:06 +0000440void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000441{
aliguorid12d51d2009-01-15 21:48:06 +0000442 LOG_IOPORT("outw: %04x %04x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000443 ioport_write(1, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000444#ifdef USE_KQEMU
445 if (env)
446 env->last_io_time = cpu_get_time_fast();
447#endif
bellard0824d6f2003-06-24 13:42:40 +0000448}
449
bellardc45886d2004-01-05 00:02:06 +0000450void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000451{
aliguorid12d51d2009-01-15 21:48:06 +0000452 LOG_IOPORT("outl: %04x %08x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000453 ioport_write(2, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000454#ifdef USE_KQEMU
455 if (env)
456 env->last_io_time = cpu_get_time_fast();
457#endif
bellard0824d6f2003-06-24 13:42:40 +0000458}
459
bellardc45886d2004-01-05 00:02:06 +0000460int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000461{
bellardfd872592004-05-12 19:11:15 +0000462 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000463 val = ioport_read(0, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000464 LOG_IOPORT("inb : %04x %02x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000465#ifdef USE_KQEMU
466 if (env)
467 env->last_io_time = cpu_get_time_fast();
468#endif
bellardfd872592004-05-12 19:11:15 +0000469 return val;
bellard0824d6f2003-06-24 13:42:40 +0000470}
471
bellardc45886d2004-01-05 00:02:06 +0000472int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000473{
bellardfd872592004-05-12 19:11:15 +0000474 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000475 val = ioport_read(1, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000476 LOG_IOPORT("inw : %04x %04x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000477#ifdef USE_KQEMU
478 if (env)
479 env->last_io_time = cpu_get_time_fast();
480#endif
bellardfd872592004-05-12 19:11:15 +0000481 return val;
bellard0824d6f2003-06-24 13:42:40 +0000482}
483
bellardc45886d2004-01-05 00:02:06 +0000484int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000485{
bellardfd872592004-05-12 19:11:15 +0000486 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000487 val = ioport_read(2, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000488 LOG_IOPORT("inl : %04x %08x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000489#ifdef USE_KQEMU
490 if (env)
491 env->last_io_time = cpu_get_time_fast();
492#endif
bellardfd872592004-05-12 19:11:15 +0000493 return val;
bellard0824d6f2003-06-24 13:42:40 +0000494}
495
496/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000497void hw_error(const char *fmt, ...)
498{
499 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000500 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000501
502 va_start(ap, fmt);
503 fprintf(stderr, "qemu: hardware error: ");
504 vfprintf(stderr, fmt, ap);
505 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000506 for(env = first_cpu; env != NULL; env = env->next_cpu) {
507 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000508#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000509 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000510#else
bellard6a00d602005-11-21 23:25:50 +0000511 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000512#endif
bellard6a00d602005-11-21 23:25:50 +0000513 }
bellard0824d6f2003-06-24 13:42:40 +0000514 va_end(ap);
515 abort();
516}
aliguoridf751fa2008-12-04 20:19:35 +0000517
518/***************/
519/* ballooning */
520
521static QEMUBalloonEvent *qemu_balloon_event;
522void *qemu_balloon_event_opaque;
523
524void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
525{
526 qemu_balloon_event = func;
527 qemu_balloon_event_opaque = opaque;
528}
529
530void qemu_balloon(ram_addr_t target)
531{
532 if (qemu_balloon_event)
533 qemu_balloon_event(qemu_balloon_event_opaque, target);
534}
535
536ram_addr_t qemu_balloon_status(void)
537{
538 if (qemu_balloon_event)
539 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
540 return 0;
541}
bellard0824d6f2003-06-24 13:42:40 +0000542
bellard8a7ddc32004-03-31 19:00:16 +0000543/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000544/* keyboard/mouse */
545
546static QEMUPutKBDEvent *qemu_put_kbd_event;
547static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000548static QEMUPutMouseEntry *qemu_put_mouse_event_head;
549static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000550
551void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
552{
553 qemu_put_kbd_event_opaque = opaque;
554 qemu_put_kbd_event = func;
555}
556
ths455204e2007-01-05 16:42:13 +0000557QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
558 void *opaque, int absolute,
559 const char *name)
bellard63066f42004-06-03 18:45:02 +0000560{
ths455204e2007-01-05 16:42:13 +0000561 QEMUPutMouseEntry *s, *cursor;
562
563 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000564
565 s->qemu_put_mouse_event = func;
566 s->qemu_put_mouse_event_opaque = opaque;
567 s->qemu_put_mouse_event_absolute = absolute;
568 s->qemu_put_mouse_event_name = qemu_strdup(name);
569 s->next = NULL;
570
571 if (!qemu_put_mouse_event_head) {
572 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
573 return s;
574 }
575
576 cursor = qemu_put_mouse_event_head;
577 while (cursor->next != NULL)
578 cursor = cursor->next;
579
580 cursor->next = s;
581 qemu_put_mouse_event_current = s;
582
583 return s;
584}
585
586void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
587{
588 QEMUPutMouseEntry *prev = NULL, *cursor;
589
590 if (!qemu_put_mouse_event_head || entry == NULL)
591 return;
592
593 cursor = qemu_put_mouse_event_head;
594 while (cursor != NULL && cursor != entry) {
595 prev = cursor;
596 cursor = cursor->next;
597 }
598
599 if (cursor == NULL) // does not exist or list empty
600 return;
601 else if (prev == NULL) { // entry is head
602 qemu_put_mouse_event_head = cursor->next;
603 if (qemu_put_mouse_event_current == entry)
604 qemu_put_mouse_event_current = cursor->next;
605 qemu_free(entry->qemu_put_mouse_event_name);
606 qemu_free(entry);
607 return;
608 }
609
610 prev->next = entry->next;
611
612 if (qemu_put_mouse_event_current == entry)
613 qemu_put_mouse_event_current = prev;
614
615 qemu_free(entry->qemu_put_mouse_event_name);
616 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000617}
618
619void kbd_put_keycode(int keycode)
620{
621 if (qemu_put_kbd_event) {
622 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
623 }
624}
625
626void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
627{
ths455204e2007-01-05 16:42:13 +0000628 QEMUPutMouseEvent *mouse_event;
629 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000630 int width;
ths455204e2007-01-05 16:42:13 +0000631
632 if (!qemu_put_mouse_event_current) {
633 return;
634 }
635
636 mouse_event =
637 qemu_put_mouse_event_current->qemu_put_mouse_event;
638 mouse_event_opaque =
639 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
640
641 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000642 if (graphic_rotate) {
643 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
644 width = 0x7fff;
645 else
aurel32b94ed572008-03-10 19:34:27 +0000646 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000647 mouse_event(mouse_event_opaque,
648 width - dy, dx, dz, buttons_state);
649 } else
650 mouse_event(mouse_event_opaque,
651 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000652 }
653}
654
bellard09b26c52006-04-12 21:09:08 +0000655int kbd_mouse_is_absolute(void)
656{
ths455204e2007-01-05 16:42:13 +0000657 if (!qemu_put_mouse_event_current)
658 return 0;
659
660 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
661}
662
aliguori376253e2009-03-05 23:01:23 +0000663void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000664{
665 QEMUPutMouseEntry *cursor;
666 int index = 0;
667
668 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000669 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000670 return;
671 }
672
aliguori376253e2009-03-05 23:01:23 +0000673 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000674 cursor = qemu_put_mouse_event_head;
675 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000676 monitor_printf(mon, "%c Mouse #%d: %s\n",
677 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
678 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000679 index++;
680 cursor = cursor->next;
681 }
682}
683
aliguori376253e2009-03-05 23:01:23 +0000684void do_mouse_set(Monitor *mon, int index)
ths455204e2007-01-05 16:42:13 +0000685{
686 QEMUPutMouseEntry *cursor;
687 int i = 0;
688
689 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000690 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000691 return;
692 }
693
694 cursor = qemu_put_mouse_event_head;
695 while (cursor != NULL && index != i) {
696 i++;
697 cursor = cursor->next;
698 }
699
700 if (cursor != NULL)
701 qemu_put_mouse_event_current = cursor;
702 else
aliguori376253e2009-03-05 23:01:23 +0000703 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000704}
705
bellard87858c82003-06-27 12:01:39 +0000706/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000707uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000708{
709 union {
710 uint64_t ll;
711 struct {
712#ifdef WORDS_BIGENDIAN
713 uint32_t high, low;
714#else
715 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000716#endif
bellard87858c82003-06-27 12:01:39 +0000717 } l;
718 } u, res;
719 uint64_t rl, rh;
720
721 u.ll = a;
722 rl = (uint64_t)u.l.low * (uint64_t)b;
723 rh = (uint64_t)u.l.high * (uint64_t)b;
724 rh += (rl >> 32);
725 res.l.high = rh / c;
726 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
727 return res.ll;
728}
729
bellard1dce7c32006-07-13 23:20:22 +0000730/***********************************************************/
731/* real time host monotonic timer */
732
733#define QEMU_TIMER_BASE 1000000000LL
734
735#ifdef WIN32
736
737static int64_t clock_freq;
738
739static void init_get_clock(void)
740{
bellarda8e5ac32006-07-14 09:36:13 +0000741 LARGE_INTEGER freq;
742 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000743 ret = QueryPerformanceFrequency(&freq);
744 if (ret == 0) {
745 fprintf(stderr, "Could not calibrate ticks\n");
746 exit(1);
747 }
748 clock_freq = freq.QuadPart;
749}
750
751static int64_t get_clock(void)
752{
753 LARGE_INTEGER ti;
754 QueryPerformanceCounter(&ti);
755 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
756}
757
758#else
759
760static int use_rt_clock;
761
762static void init_get_clock(void)
763{
764 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000765#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
766 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000767 {
768 struct timespec ts;
769 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
770 use_rt_clock = 1;
771 }
772 }
773#endif
774}
775
776static int64_t get_clock(void)
777{
blueswir1c5e97232009-03-07 20:06:23 +0000778#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
779 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000780 if (use_rt_clock) {
781 struct timespec ts;
782 clock_gettime(CLOCK_MONOTONIC, &ts);
783 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000784 } else
bellard1dce7c32006-07-13 23:20:22 +0000785#endif
786 {
787 /* XXX: using gettimeofday leads to problems if the date
788 changes, so it should be avoided. */
789 struct timeval tv;
790 gettimeofday(&tv, NULL);
791 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
792 }
793}
bellard1dce7c32006-07-13 23:20:22 +0000794#endif
795
pbrook2e70f6e2008-06-29 01:03:05 +0000796/* Return the virtual CPU time, based on the instruction counter. */
797static int64_t cpu_get_icount(void)
798{
799 int64_t icount;
800 CPUState *env = cpu_single_env;;
801 icount = qemu_icount;
802 if (env) {
803 if (!can_do_io(env))
804 fprintf(stderr, "Bad clock read\n");
805 icount -= (env->icount_decr.u16.low + env->icount_extra);
806 }
807 return qemu_icount_bias + (icount << icount_time_shift);
808}
809
bellard1dce7c32006-07-13 23:20:22 +0000810/***********************************************************/
811/* guest cycle counter */
812
813static int64_t cpu_ticks_prev;
814static int64_t cpu_ticks_offset;
815static int64_t cpu_clock_offset;
816static int cpu_ticks_enabled;
817
818/* return the host CPU cycle counter and handle stop/restart */
819int64_t cpu_get_ticks(void)
820{
pbrook2e70f6e2008-06-29 01:03:05 +0000821 if (use_icount) {
822 return cpu_get_icount();
823 }
bellard1dce7c32006-07-13 23:20:22 +0000824 if (!cpu_ticks_enabled) {
825 return cpu_ticks_offset;
826 } else {
827 int64_t ticks;
828 ticks = cpu_get_real_ticks();
829 if (cpu_ticks_prev > ticks) {
830 /* Note: non increasing ticks may happen if the host uses
831 software suspend */
832 cpu_ticks_offset += cpu_ticks_prev - ticks;
833 }
834 cpu_ticks_prev = ticks;
835 return ticks + cpu_ticks_offset;
836 }
837}
838
839/* return the host CPU monotonic timer and handle stop/restart */
840static int64_t cpu_get_clock(void)
841{
842 int64_t ti;
843 if (!cpu_ticks_enabled) {
844 return cpu_clock_offset;
845 } else {
846 ti = get_clock();
847 return ti + cpu_clock_offset;
848 }
849}
850
851/* enable cpu_get_ticks() */
852void cpu_enable_ticks(void)
853{
854 if (!cpu_ticks_enabled) {
855 cpu_ticks_offset -= cpu_get_real_ticks();
856 cpu_clock_offset -= get_clock();
857 cpu_ticks_enabled = 1;
858 }
859}
860
861/* disable cpu_get_ticks() : the clock is stopped. You must not call
862 cpu_get_ticks() after that. */
863void cpu_disable_ticks(void)
864{
865 if (cpu_ticks_enabled) {
866 cpu_ticks_offset = cpu_get_ticks();
867 cpu_clock_offset = cpu_get_clock();
868 cpu_ticks_enabled = 0;
869 }
870}
871
872/***********************************************************/
873/* timers */
ths5fafdf22007-09-16 21:08:06 +0000874
bellard8a7ddc32004-03-31 19:00:16 +0000875#define QEMU_TIMER_REALTIME 0
876#define QEMU_TIMER_VIRTUAL 1
877
878struct QEMUClock {
879 int type;
880 /* XXX: add frequency */
881};
882
883struct QEMUTimer {
884 QEMUClock *clock;
885 int64_t expire_time;
886 QEMUTimerCB *cb;
887 void *opaque;
888 struct QEMUTimer *next;
889};
890
thsc8994012007-08-19 21:56:03 +0000891struct qemu_alarm_timer {
892 char const *name;
thsefe75412007-08-24 01:36:32 +0000893 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000894
895 int (*start)(struct qemu_alarm_timer *t);
896 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000897 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000898 void *priv;
899};
900
thsefe75412007-08-24 01:36:32 +0000901#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000902#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000903
904static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
905{
906 return t->flags & ALARM_FLAG_DYNTICKS;
907}
908
909static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
910{
911 if (!alarm_has_dynticks(t))
912 return;
913
914 t->rearm(t);
915}
916
917/* TODO: MIN_TIMER_REARM_US should be optimized */
918#define MIN_TIMER_REARM_US 250
919
thsc8994012007-08-19 21:56:03 +0000920static struct qemu_alarm_timer *alarm_timer;
aliguorif49e58d2008-11-05 21:22:34 +0000921#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +0000922static int alarm_timer_rfd, alarm_timer_wfd;
aliguorif49e58d2008-11-05 21:22:34 +0000923#endif
thsc8994012007-08-19 21:56:03 +0000924
925#ifdef _WIN32
926
927struct qemu_alarm_win32 {
928 MMRESULT timerId;
929 HANDLE host_alarm;
930 unsigned int period;
931} alarm_win32_data = {0, NULL, -1};
932
933static int win32_start_timer(struct qemu_alarm_timer *t);
934static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000935static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000936
937#else
938
939static int unix_start_timer(struct qemu_alarm_timer *t);
940static void unix_stop_timer(struct qemu_alarm_timer *t);
941
ths231c6582007-08-26 17:29:15 +0000942#ifdef __linux__
943
thsefe75412007-08-24 01:36:32 +0000944static int dynticks_start_timer(struct qemu_alarm_timer *t);
945static void dynticks_stop_timer(struct qemu_alarm_timer *t);
946static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
947
thsc40ec5a2007-08-19 22:09:40 +0000948static int hpet_start_timer(struct qemu_alarm_timer *t);
949static void hpet_stop_timer(struct qemu_alarm_timer *t);
950
thsc8994012007-08-19 21:56:03 +0000951static int rtc_start_timer(struct qemu_alarm_timer *t);
952static void rtc_stop_timer(struct qemu_alarm_timer *t);
953
thsefe75412007-08-24 01:36:32 +0000954#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000955
956#endif /* _WIN32 */
957
pbrook2e70f6e2008-06-29 01:03:05 +0000958/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000959 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000960 When the guest is idle real and virtual time will be aligned in
961 the IO wait loop. */
962#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
963
964static void icount_adjust(void)
965{
966 int64_t cur_time;
967 int64_t cur_icount;
968 int64_t delta;
969 static int64_t last_delta;
970 /* If the VM is not running, then do nothing. */
971 if (!vm_running)
972 return;
973
974 cur_time = cpu_get_clock();
975 cur_icount = qemu_get_clock(vm_clock);
976 delta = cur_icount - cur_time;
977 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
978 if (delta > 0
979 && last_delta + ICOUNT_WOBBLE < delta * 2
980 && icount_time_shift > 0) {
981 /* The guest is getting too far ahead. Slow time down. */
982 icount_time_shift--;
983 }
984 if (delta < 0
985 && last_delta - ICOUNT_WOBBLE > delta * 2
986 && icount_time_shift < MAX_ICOUNT_SHIFT) {
987 /* The guest is getting too far behind. Speed time up. */
988 icount_time_shift++;
989 }
990 last_delta = delta;
991 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
992}
993
994static void icount_adjust_rt(void * opaque)
995{
996 qemu_mod_timer(icount_rt_timer,
997 qemu_get_clock(rt_clock) + 1000);
998 icount_adjust();
999}
1000
1001static void icount_adjust_vm(void * opaque)
1002{
1003 qemu_mod_timer(icount_vm_timer,
1004 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1005 icount_adjust();
1006}
1007
1008static void init_icount_adjust(void)
1009{
1010 /* Have both realtime and virtual time triggers for speed adjustment.
1011 The realtime trigger catches emulated time passing too slowly,
1012 the virtual time trigger catches emulated time passing too fast.
1013 Realtime triggers occur even when idle, so use them less frequently
1014 than VM triggers. */
1015 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1016 qemu_mod_timer(icount_rt_timer,
1017 qemu_get_clock(rt_clock) + 1000);
1018 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1019 qemu_mod_timer(icount_vm_timer,
1020 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1021}
1022
thsc8994012007-08-19 21:56:03 +00001023static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +00001024#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +00001025#ifdef __linux__
thsefe75412007-08-24 01:36:32 +00001026 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1027 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001028 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +00001029 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001030 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +00001031 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001032#endif
thsefe75412007-08-24 01:36:32 +00001033 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001034#else
thsefe75412007-08-24 01:36:32 +00001035 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1036 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1037 {"win32", 0, win32_start_timer,
1038 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +00001039#endif
1040 {NULL, }
1041};
1042
blueswir13f47aa82008-03-09 06:59:01 +00001043static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +00001044{
1045 int i;
1046
1047 printf("Available alarm timers, in order of precedence:\n");
1048 for (i = 0; alarm_timers[i].name; i++)
1049 printf("%s\n", alarm_timers[i].name);
1050}
1051
1052static void configure_alarms(char const *opt)
1053{
1054 int i;
1055 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +00001056 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +00001057 char *arg;
1058 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +00001059 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +00001060
aurel323adda042008-03-09 23:43:49 +00001061 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +00001062 show_available_alarms();
1063 exit(0);
1064 }
1065
1066 arg = strdup(opt);
1067
1068 /* Reorder the array */
1069 name = strtok(arg, ",");
1070 while (name) {
balroge2b577e2007-09-17 21:25:20 +00001071 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +00001072 if (!strcmp(alarm_timers[i].name, name))
1073 break;
1074 }
1075
1076 if (i == count) {
1077 fprintf(stderr, "Unknown clock %s\n", name);
1078 goto next;
1079 }
1080
1081 if (i < cur)
1082 /* Ignore */
1083 goto next;
1084
1085 /* Swap */
1086 tmp = alarm_timers[i];
1087 alarm_timers[i] = alarm_timers[cur];
1088 alarm_timers[cur] = tmp;
1089
1090 cur++;
1091next:
1092 name = strtok(NULL, ",");
1093 }
1094
1095 free(arg);
1096
1097 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +00001098 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +00001099 for (i = cur; i < count; i++)
1100 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +00001101 } else {
1102 show_available_alarms();
1103 exit(1);
thsf3dcfad2007-08-24 01:26:02 +00001104 }
thsf3dcfad2007-08-24 01:26:02 +00001105}
1106
bellard8a7ddc32004-03-31 19:00:16 +00001107QEMUClock *rt_clock;
1108QEMUClock *vm_clock;
1109
1110static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +00001111
pbrook9596ebb2007-11-18 01:44:38 +00001112static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001113{
1114 QEMUClock *clock;
1115 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +00001116 clock->type = type;
1117 return clock;
1118}
1119
1120QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1121{
1122 QEMUTimer *ts;
1123
1124 ts = qemu_mallocz(sizeof(QEMUTimer));
1125 ts->clock = clock;
1126 ts->cb = cb;
1127 ts->opaque = opaque;
1128 return ts;
1129}
1130
1131void qemu_free_timer(QEMUTimer *ts)
1132{
1133 qemu_free(ts);
1134}
1135
1136/* stop a timer, but do not dealloc it */
1137void qemu_del_timer(QEMUTimer *ts)
1138{
1139 QEMUTimer **pt, *t;
1140
1141 /* NOTE: this code must be signal safe because
1142 qemu_timer_expired() can be called from a signal. */
1143 pt = &active_timers[ts->clock->type];
1144 for(;;) {
1145 t = *pt;
1146 if (!t)
1147 break;
1148 if (t == ts) {
1149 *pt = t->next;
1150 break;
1151 }
1152 pt = &t->next;
1153 }
1154}
1155
1156/* modify the current timer so that it will be fired when current_time
1157 >= expire_time. The corresponding callback will be called. */
1158void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1159{
1160 QEMUTimer **pt, *t;
1161
1162 qemu_del_timer(ts);
1163
1164 /* add the timer in the sorted list */
1165 /* NOTE: this code must be signal safe because
1166 qemu_timer_expired() can be called from a signal. */
1167 pt = &active_timers[ts->clock->type];
1168 for(;;) {
1169 t = *pt;
1170 if (!t)
1171 break;
ths5fafdf22007-09-16 21:08:06 +00001172 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001173 break;
1174 pt = &t->next;
1175 }
1176 ts->expire_time = expire_time;
1177 ts->next = *pt;
1178 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001179
1180 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001181 if (pt == &active_timers[ts->clock->type]) {
1182 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1183 qemu_rearm_alarm_timer(alarm_timer);
1184 }
1185 /* Interrupt execution to force deadline recalculation. */
1186 if (use_icount && cpu_single_env) {
aurel323098dba2009-03-07 21:28:24 +00001187 cpu_exit(cpu_single_env);
pbrook2e70f6e2008-06-29 01:03:05 +00001188 }
1189 }
bellard8a7ddc32004-03-31 19:00:16 +00001190}
1191
1192int qemu_timer_pending(QEMUTimer *ts)
1193{
1194 QEMUTimer *t;
1195 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1196 if (t == ts)
1197 return 1;
1198 }
1199 return 0;
1200}
1201
1202static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1203{
1204 if (!timer_head)
1205 return 0;
1206 return (timer_head->expire_time <= current_time);
1207}
1208
1209static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1210{
1211 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001212
bellard8a7ddc32004-03-31 19:00:16 +00001213 for(;;) {
1214 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001215 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001216 break;
1217 /* remove timer from the list before calling the callback */
1218 *ptimer_head = ts->next;
1219 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001220
bellard8a7ddc32004-03-31 19:00:16 +00001221 /* run the callback (the timer list can be modified) */
1222 ts->cb(ts->opaque);
1223 }
1224}
1225
1226int64_t qemu_get_clock(QEMUClock *clock)
1227{
1228 switch(clock->type) {
1229 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001230 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001231 default:
1232 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001233 if (use_icount) {
1234 return cpu_get_icount();
1235 } else {
1236 return cpu_get_clock();
1237 }
bellard8a7ddc32004-03-31 19:00:16 +00001238 }
1239}
1240
bellard1dce7c32006-07-13 23:20:22 +00001241static void init_timers(void)
1242{
1243 init_get_clock();
1244 ticks_per_sec = QEMU_TIMER_BASE;
1245 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1246 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1247}
1248
bellard8a7ddc32004-03-31 19:00:16 +00001249/* save a timer */
1250void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1251{
1252 uint64_t expire_time;
1253
1254 if (qemu_timer_pending(ts)) {
1255 expire_time = ts->expire_time;
1256 } else {
1257 expire_time = -1;
1258 }
1259 qemu_put_be64(f, expire_time);
1260}
1261
1262void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1263{
1264 uint64_t expire_time;
1265
1266 expire_time = qemu_get_be64(f);
1267 if (expire_time != -1) {
1268 qemu_mod_timer(ts, expire_time);
1269 } else {
1270 qemu_del_timer(ts);
1271 }
1272}
1273
1274static void timer_save(QEMUFile *f, void *opaque)
1275{
1276 if (cpu_ticks_enabled) {
1277 hw_error("cannot save state if virtual timers are running");
1278 }
thsbee8d682007-12-16 23:41:11 +00001279 qemu_put_be64(f, cpu_ticks_offset);
1280 qemu_put_be64(f, ticks_per_sec);
1281 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001282}
1283
1284static int timer_load(QEMUFile *f, void *opaque, int version_id)
1285{
bellardc88676f2006-08-06 13:36:11 +00001286 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001287 return -EINVAL;
1288 if (cpu_ticks_enabled) {
1289 return -EINVAL;
1290 }
thsbee8d682007-12-16 23:41:11 +00001291 cpu_ticks_offset=qemu_get_be64(f);
1292 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001293 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001294 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001295 }
bellard8a7ddc32004-03-31 19:00:16 +00001296 return 0;
1297}
1298
bellard67b915a2004-03-31 23:37:16 +00001299#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001300void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001301 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1302#else
bellard8a7ddc32004-03-31 19:00:16 +00001303static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001304#endif
bellard8a7ddc32004-03-31 19:00:16 +00001305{
bellard02ba45c2004-06-25 14:46:23 +00001306#if 0
1307#define DISP_FREQ 1000
1308 {
1309 static int64_t delta_min = INT64_MAX;
1310 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1311 static int count;
1312 ti = qemu_get_clock(vm_clock);
1313 if (last_clock != 0) {
1314 delta = ti - last_clock;
1315 if (delta < delta_min)
1316 delta_min = delta;
1317 if (delta > delta_max)
1318 delta_max = delta;
1319 delta_cum += delta;
1320 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001321 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001322 muldiv64(delta_min, 1000000, ticks_per_sec),
1323 muldiv64(delta_max, 1000000, ticks_per_sec),
1324 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1325 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1326 count = 0;
1327 delta_min = INT64_MAX;
1328 delta_max = 0;
1329 delta_cum = 0;
1330 }
1331 }
1332 last_clock = ti;
1333 }
1334#endif
thsefe75412007-08-24 01:36:32 +00001335 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001336 (!use_icount &&
1337 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1338 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001339 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1340 qemu_get_clock(rt_clock))) {
aliguoric96f1a42008-11-05 20:29:45 +00001341 CPUState *env = next_cpu;
aliguoric96f1a42008-11-05 20:29:45 +00001342
bellard06d9f2f2006-05-01 13:23:04 +00001343#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001344 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1345 SetEvent(data->host_alarm);
aliguorif49e58d2008-11-05 21:22:34 +00001346#else
1347 static const char byte = 0;
aliguoric96f1a42008-11-05 20:29:45 +00001348 write(alarm_timer_wfd, &byte, sizeof(byte));
aliguorif49e58d2008-11-05 21:22:34 +00001349#endif
balrogd5d08332008-01-05 19:41:47 +00001350 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1351
balrog4f8eb8d2007-12-16 12:39:38 +00001352 if (env) {
1353 /* stop the currently executing cpu because a timer occured */
aurel323098dba2009-03-07 21:28:24 +00001354 cpu_exit(env);
bellarda332e112005-09-03 17:55:47 +00001355#ifdef USE_KQEMU
balrog4f8eb8d2007-12-16 12:39:38 +00001356 if (env->kqemu_enabled) {
1357 kqemu_cpu_interrupt(env);
1358 }
balrogee5605e2007-12-03 03:01:40 +00001359#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001360 }
balrogee5605e2007-12-03 03:01:40 +00001361 event_pending = 1;
bellard8a7ddc32004-03-31 19:00:16 +00001362 }
1363}
1364
pbrook2e70f6e2008-06-29 01:03:05 +00001365static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001366{
pbrook2e70f6e2008-06-29 01:03:05 +00001367 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001368
1369 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001370 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1371 qemu_get_clock(vm_clock);
1372 } else {
1373 /* To avoid problems with overflow limit this to 2^32. */
1374 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001375 }
1376
pbrook2e70f6e2008-06-29 01:03:05 +00001377 if (delta < 0)
1378 delta = 0;
thsefe75412007-08-24 01:36:32 +00001379
pbrook2e70f6e2008-06-29 01:03:05 +00001380 return delta;
1381}
1382
blueswir18632fb92008-09-14 13:59:34 +00001383#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001384static uint64_t qemu_next_deadline_dyntick(void)
1385{
1386 int64_t delta;
1387 int64_t rtdelta;
1388
1389 if (use_icount)
1390 delta = INT32_MAX;
1391 else
1392 delta = (qemu_next_deadline() + 999) / 1000;
1393
1394 if (active_timers[QEMU_TIMER_REALTIME]) {
1395 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1396 qemu_get_clock(rt_clock))*1000;
1397 if (rtdelta < delta)
1398 delta = rtdelta;
1399 }
1400
1401 if (delta < MIN_TIMER_REARM_US)
1402 delta = MIN_TIMER_REARM_US;
1403
1404 return delta;
thsefe75412007-08-24 01:36:32 +00001405}
blueswir18632fb92008-09-14 13:59:34 +00001406#endif
thsefe75412007-08-24 01:36:32 +00001407
bellardfd872592004-05-12 19:11:15 +00001408#ifndef _WIN32
1409
aliguori7183b4b2008-11-05 20:40:18 +00001410/* Sets a specific flag */
1411static int fcntl_setfl(int fd, int flag)
1412{
1413 int flags;
1414
1415 flags = fcntl(fd, F_GETFL);
1416 if (flags == -1)
1417 return -errno;
1418
1419 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1420 return -errno;
1421
1422 return 0;
1423}
1424
bellard829309c2004-05-20 13:20:12 +00001425#if defined(__linux__)
1426
bellardfd872592004-05-12 19:11:15 +00001427#define RTC_FREQ 1024
1428
aurel32de9a95f2008-11-11 13:41:01 +00001429static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001430{
thsc8994012007-08-19 21:56:03 +00001431 struct sigaction act;
1432
1433 /* timer signal */
1434 sigfillset(&act.sa_mask);
1435 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001436 act.sa_handler = host_alarm_handler;
1437
1438 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001439 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001440 fcntl(fd, F_SETOWN, getpid());
1441}
1442
thsc40ec5a2007-08-19 22:09:40 +00001443static int hpet_start_timer(struct qemu_alarm_timer *t)
1444{
1445 struct hpet_info info;
1446 int r, fd;
1447
1448 fd = open("/dev/hpet", O_RDONLY);
1449 if (fd < 0)
1450 return -1;
1451
1452 /* Set frequency */
1453 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1454 if (r < 0) {
1455 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1456 "error, but for better emulation accuracy type:\n"
1457 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1458 goto fail;
1459 }
1460
1461 /* Check capabilities */
1462 r = ioctl(fd, HPET_INFO, &info);
1463 if (r < 0)
1464 goto fail;
1465
1466 /* Enable periodic mode */
1467 r = ioctl(fd, HPET_EPI, 0);
1468 if (info.hi_flags && (r < 0))
1469 goto fail;
1470
1471 /* Enable interrupt */
1472 r = ioctl(fd, HPET_IE_ON, 0);
1473 if (r < 0)
1474 goto fail;
1475
1476 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001477 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001478
1479 return 0;
1480fail:
1481 close(fd);
1482 return -1;
1483}
1484
1485static void hpet_stop_timer(struct qemu_alarm_timer *t)
1486{
pbrookfcdc2122007-08-23 20:22:22 +00001487 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001488
1489 close(fd);
1490}
1491
thsc8994012007-08-19 21:56:03 +00001492static int rtc_start_timer(struct qemu_alarm_timer *t)
1493{
1494 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001495 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001496
balrogaeb30be2007-07-02 15:03:13 +00001497 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001498 if (rtc_fd < 0)
1499 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001500 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1501 if (current_rtc_freq != RTC_FREQ &&
1502 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001503 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1504 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1505 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1506 goto fail;
1507 }
1508 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1509 fail:
1510 close(rtc_fd);
1511 return -1;
1512 }
thsc8994012007-08-19 21:56:03 +00001513
1514 enable_sigio_timer(rtc_fd);
1515
pbrookfcdc2122007-08-23 20:22:22 +00001516 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001517
bellardfd872592004-05-12 19:11:15 +00001518 return 0;
1519}
1520
thsc8994012007-08-19 21:56:03 +00001521static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001522{
pbrookfcdc2122007-08-23 20:22:22 +00001523 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001524
1525 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001526}
1527
thsefe75412007-08-24 01:36:32 +00001528static int dynticks_start_timer(struct qemu_alarm_timer *t)
1529{
1530 struct sigevent ev;
1531 timer_t host_timer;
1532 struct sigaction act;
1533
1534 sigfillset(&act.sa_mask);
1535 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001536 act.sa_handler = host_alarm_handler;
1537
1538 sigaction(SIGALRM, &act, NULL);
1539
1540 ev.sigev_value.sival_int = 0;
1541 ev.sigev_notify = SIGEV_SIGNAL;
1542 ev.sigev_signo = SIGALRM;
1543
1544 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1545 perror("timer_create");
1546
1547 /* disable dynticks */
1548 fprintf(stderr, "Dynamic Ticks disabled\n");
1549
1550 return -1;
1551 }
1552
blueswir10399bfe2008-11-16 11:37:18 +00001553 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001554
1555 return 0;
1556}
1557
1558static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1559{
blueswir10399bfe2008-11-16 11:37:18 +00001560 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001561
1562 timer_delete(host_timer);
1563}
1564
1565static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1566{
blueswir10399bfe2008-11-16 11:37:18 +00001567 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001568 struct itimerspec timeout;
1569 int64_t nearest_delta_us = INT64_MAX;
1570 int64_t current_us;
1571
1572 if (!active_timers[QEMU_TIMER_REALTIME] &&
1573 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001574 return;
thsefe75412007-08-24 01:36:32 +00001575
pbrook2e70f6e2008-06-29 01:03:05 +00001576 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001577
1578 /* check whether a timer is already running */
1579 if (timer_gettime(host_timer, &timeout)) {
1580 perror("gettime");
1581 fprintf(stderr, "Internal timer error: aborting\n");
1582 exit(1);
1583 }
1584 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1585 if (current_us && current_us <= nearest_delta_us)
1586 return;
1587
1588 timeout.it_interval.tv_sec = 0;
1589 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1590 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1591 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1592 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1593 perror("settime");
1594 fprintf(stderr, "Internal timer error: aborting\n");
1595 exit(1);
1596 }
1597}
1598
ths70744b32007-08-26 17:31:30 +00001599#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001600
thsc8994012007-08-19 21:56:03 +00001601static int unix_start_timer(struct qemu_alarm_timer *t)
1602{
1603 struct sigaction act;
1604 struct itimerval itv;
1605 int err;
1606
1607 /* timer signal */
1608 sigfillset(&act.sa_mask);
1609 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001610 act.sa_handler = host_alarm_handler;
1611
1612 sigaction(SIGALRM, &act, NULL);
1613
1614 itv.it_interval.tv_sec = 0;
1615 /* for i386 kernel 2.6 to get 1 ms */
1616 itv.it_interval.tv_usec = 999;
1617 itv.it_value.tv_sec = 0;
1618 itv.it_value.tv_usec = 10 * 1000;
1619
1620 err = setitimer(ITIMER_REAL, &itv, NULL);
1621 if (err)
1622 return -1;
1623
1624 return 0;
1625}
1626
1627static void unix_stop_timer(struct qemu_alarm_timer *t)
1628{
1629 struct itimerval itv;
1630
1631 memset(&itv, 0, sizeof(itv));
1632 setitimer(ITIMER_REAL, &itv, NULL);
1633}
1634
bellard829309c2004-05-20 13:20:12 +00001635#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001636
aliguorif49e58d2008-11-05 21:22:34 +00001637static void try_to_rearm_timer(void *opaque)
1638{
1639 struct qemu_alarm_timer *t = opaque;
1640#ifndef _WIN32
1641 ssize_t len;
1642
1643 /* Drain the notify pipe */
1644 do {
1645 char buffer[512];
1646 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1647 } while ((len == -1 && errno == EINTR) || len > 0);
1648#endif
1649
aliguorif49e58d2008-11-05 21:22:34 +00001650 if (t->flags & ALARM_FLAG_EXPIRED) {
1651 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1652 qemu_rearm_alarm_timer(alarm_timer);
1653 }
1654}
1655
thsc8994012007-08-19 21:56:03 +00001656#ifdef _WIN32
1657
1658static int win32_start_timer(struct qemu_alarm_timer *t)
1659{
1660 TIMECAPS tc;
1661 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001662 UINT flags;
thsc8994012007-08-19 21:56:03 +00001663
1664 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1665 if (!data->host_alarm) {
1666 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001667 return -1;
thsc8994012007-08-19 21:56:03 +00001668 }
1669
1670 memset(&tc, 0, sizeof(tc));
1671 timeGetDevCaps(&tc, sizeof(tc));
1672
1673 if (data->period < tc.wPeriodMin)
1674 data->period = tc.wPeriodMin;
1675
1676 timeBeginPeriod(data->period);
1677
thsefe75412007-08-24 01:36:32 +00001678 flags = TIME_CALLBACK_FUNCTION;
1679 if (alarm_has_dynticks(t))
1680 flags |= TIME_ONESHOT;
1681 else
1682 flags |= TIME_PERIODIC;
1683
thsc8994012007-08-19 21:56:03 +00001684 data->timerId = timeSetEvent(1, // interval (ms)
1685 data->period, // resolution
1686 host_alarm_handler, // function
1687 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001688 flags);
thsc8994012007-08-19 21:56:03 +00001689
1690 if (!data->timerId) {
1691 perror("Failed to initialize win32 alarm timer");
1692
1693 timeEndPeriod(data->period);
1694 CloseHandle(data->host_alarm);
1695 return -1;
1696 }
1697
aliguorif49e58d2008-11-05 21:22:34 +00001698 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
thsc8994012007-08-19 21:56:03 +00001699
1700 return 0;
1701}
1702
1703static void win32_stop_timer(struct qemu_alarm_timer *t)
1704{
1705 struct qemu_alarm_win32 *data = t->priv;
1706
1707 timeKillEvent(data->timerId);
1708 timeEndPeriod(data->period);
1709
1710 CloseHandle(data->host_alarm);
1711}
1712
thsefe75412007-08-24 01:36:32 +00001713static void win32_rearm_timer(struct qemu_alarm_timer *t)
1714{
1715 struct qemu_alarm_win32 *data = t->priv;
1716 uint64_t nearest_delta_us;
1717
1718 if (!active_timers[QEMU_TIMER_REALTIME] &&
1719 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001720 return;
thsefe75412007-08-24 01:36:32 +00001721
pbrook2e70f6e2008-06-29 01:03:05 +00001722 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001723 nearest_delta_us /= 1000;
1724
1725 timeKillEvent(data->timerId);
1726
1727 data->timerId = timeSetEvent(1,
1728 data->period,
1729 host_alarm_handler,
1730 (DWORD)t,
1731 TIME_ONESHOT | TIME_PERIODIC);
1732
1733 if (!data->timerId) {
1734 perror("Failed to re-arm win32 alarm timer");
1735
1736 timeEndPeriod(data->period);
1737 CloseHandle(data->host_alarm);
1738 exit(1);
1739 }
1740}
1741
thsc8994012007-08-19 21:56:03 +00001742#endif /* _WIN32 */
1743
aliguori7183b4b2008-11-05 20:40:18 +00001744static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001745{
blueswir1223f0d72008-09-30 18:12:18 +00001746 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001747 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001748
1749#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +00001750 int fds[2];
1751
aliguori7183b4b2008-11-05 20:40:18 +00001752 err = pipe(fds);
1753 if (err == -1)
1754 return -errno;
1755
1756 err = fcntl_setfl(fds[0], O_NONBLOCK);
1757 if (err < 0)
1758 goto fail;
1759
1760 err = fcntl_setfl(fds[1], O_NONBLOCK);
1761 if (err < 0)
1762 goto fail;
1763
aliguoric96f1a42008-11-05 20:29:45 +00001764 alarm_timer_rfd = fds[0];
1765 alarm_timer_wfd = fds[1];
aliguorif49e58d2008-11-05 21:22:34 +00001766#endif
bellard06d9f2f2006-05-01 13:23:04 +00001767
thsc8994012007-08-19 21:56:03 +00001768 for (i = 0; alarm_timers[i].name; i++) {
1769 t = &alarm_timers[i];
1770
thsc8994012007-08-19 21:56:03 +00001771 err = t->start(t);
1772 if (!err)
1773 break;
bellard67b915a2004-03-31 23:37:16 +00001774 }
bellardfd872592004-05-12 19:11:15 +00001775
thsc8994012007-08-19 21:56:03 +00001776 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001777 err = -ENOENT;
1778 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001779 }
thsc8994012007-08-19 21:56:03 +00001780
aliguorif49e58d2008-11-05 21:22:34 +00001781#ifndef _WIN32
aliguori6abfbd72008-11-05 20:49:37 +00001782 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1783 try_to_rearm_timer, NULL, t);
aliguorif49e58d2008-11-05 21:22:34 +00001784#endif
aliguori6abfbd72008-11-05 20:49:37 +00001785
thsc8994012007-08-19 21:56:03 +00001786 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001787
aliguori6abfbd72008-11-05 20:49:37 +00001788 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001789
1790fail:
aliguorif49e58d2008-11-05 21:22:34 +00001791#ifndef _WIN32
aliguori7183b4b2008-11-05 20:40:18 +00001792 close(fds[0]);
1793 close(fds[1]);
aliguorif49e58d2008-11-05 21:22:34 +00001794#endif
aliguori7183b4b2008-11-05 20:40:18 +00001795 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001796}
1797
pbrook9596ebb2007-11-18 01:44:38 +00001798static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001799{
thsc8994012007-08-19 21:56:03 +00001800 alarm_timer->stop(alarm_timer);
1801 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001802}
1803
bellardc4b1fcc2004-03-14 21:44:30 +00001804/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001805/* host time/date access */
1806void qemu_get_timedate(struct tm *tm, int offset)
1807{
1808 time_t ti;
1809 struct tm *ret;
1810
1811 time(&ti);
1812 ti += offset;
1813 if (rtc_date_offset == -1) {
1814 if (rtc_utc)
1815 ret = gmtime(&ti);
1816 else
1817 ret = localtime(&ti);
1818 } else {
1819 ti -= rtc_date_offset;
1820 ret = gmtime(&ti);
1821 }
1822
1823 memcpy(tm, ret, sizeof(struct tm));
1824}
1825
1826int qemu_timedate_diff(struct tm *tm)
1827{
1828 time_t seconds;
1829
1830 if (rtc_date_offset == -1)
1831 if (rtc_utc)
1832 seconds = mktimegm(tm);
1833 else
1834 seconds = mktime(tm);
1835 else
1836 seconds = mktimegm(tm) + rtc_date_offset;
1837
1838 return seconds - time(NULL);
1839}
1840
bellardfd1dff42006-02-01 21:29:26 +00001841#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001842static void socket_cleanup(void)
1843{
1844 WSACleanup();
1845}
bellard82c643f2004-07-14 17:28:13 +00001846
bellardfd1dff42006-02-01 21:29:26 +00001847static int socket_init(void)
1848{
1849 WSADATA Data;
1850 int ret, err;
1851
1852 ret = WSAStartup(MAKEWORD(2,2), &Data);
1853 if (ret != 0) {
1854 err = WSAGetLastError();
1855 fprintf(stderr, "WSAStartup: %d\n", err);
1856 return -1;
1857 }
1858 atexit(socket_cleanup);
1859 return 0;
1860}
aurel3264b7b732008-05-05 10:05:31 +00001861#endif
1862
aliguori63a01ef2008-10-31 19:10:00 +00001863const char *get_opt_name(char *buf, int buf_size, const char *p)
thse4bcb142007-12-02 04:51:10 +00001864{
1865 char *q;
thse4bcb142007-12-02 04:51:10 +00001866
balrog609497a2008-01-14 02:56:53 +00001867 q = buf;
1868 while (*p != '\0' && *p != '=') {
1869 if (q && (q - buf) < buf_size - 1)
1870 *q++ = *p;
1871 p++;
1872 }
1873 if (q)
1874 *q = '\0';
1875
1876 return p;
1877}
1878
aliguori63a01ef2008-10-31 19:10:00 +00001879const char *get_opt_value(char *buf, int buf_size, const char *p)
balrog609497a2008-01-14 02:56:53 +00001880{
1881 char *q;
1882
thse4bcb142007-12-02 04:51:10 +00001883 q = buf;
1884 while (*p != '\0') {
balrog609497a2008-01-14 02:56:53 +00001885 if (*p == ',') {
1886 if (*(p + 1) != ',')
thse4bcb142007-12-02 04:51:10 +00001887 break;
thse4bcb142007-12-02 04:51:10 +00001888 p++;
balrog609497a2008-01-14 02:56:53 +00001889 }
thse4bcb142007-12-02 04:51:10 +00001890 if (q && (q - buf) < buf_size - 1)
1891 *q++ = *p;
1892 p++;
1893 }
1894 if (q)
1895 *q = '\0';
1896
1897 return p;
1898}
1899
aliguori63a01ef2008-10-31 19:10:00 +00001900int get_param_value(char *buf, int buf_size,
1901 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001902{
1903 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001904 char option[128];
1905
1906 p = str;
1907 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001908 p = get_opt_name(option, sizeof(option), p);
bellard7c9d8e02005-11-15 22:16:05 +00001909 if (*p != '=')
1910 break;
1911 p++;
1912 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001913 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001914 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001915 } else {
balrog609497a2008-01-14 02:56:53 +00001916 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001917 }
1918 if (*p != ',')
1919 break;
1920 p++;
1921 }
1922 return 0;
1923}
1924
aliguori63a01ef2008-10-31 19:10:00 +00001925int check_params(char *buf, int buf_size,
1926 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001927{
1928 const char *p;
1929 int i;
1930
1931 p = str;
1932 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001933 p = get_opt_name(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001934 if (*p != '=')
1935 return -1;
1936 p++;
1937 for(i = 0; params[i] != NULL; i++)
1938 if (!strcmp(params[i], buf))
1939 break;
1940 if (params[i] == NULL)
1941 return -1;
balrog609497a2008-01-14 02:56:53 +00001942 p = get_opt_value(NULL, 0, p);
thse4bcb142007-12-02 04:51:10 +00001943 if (*p != ',')
1944 break;
1945 p++;
1946 }
1947 return 0;
1948}
1949
balrog1ae26a12008-09-28 23:19:47 +00001950/***********************************************************/
1951/* Bluetooth support */
1952static int nb_hcis;
1953static int cur_hci;
1954static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001955
balrog1ae26a12008-09-28 23:19:47 +00001956static struct bt_vlan_s {
1957 struct bt_scatternet_s net;
1958 int id;
1959 struct bt_vlan_s *next;
1960} *first_bt_vlan;
1961
1962/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001963static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001964{
1965 struct bt_vlan_s **pvlan, *vlan;
1966 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1967 if (vlan->id == id)
1968 return &vlan->net;
1969 }
1970 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1971 vlan->id = id;
1972 pvlan = &first_bt_vlan;
1973 while (*pvlan != NULL)
1974 pvlan = &(*pvlan)->next;
1975 *pvlan = vlan;
1976 return &vlan->net;
1977}
1978
1979static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1980{
1981}
1982
1983static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1984{
1985 return -ENOTSUP;
1986}
1987
1988static struct HCIInfo null_hci = {
1989 .cmd_send = null_hci_send,
1990 .sco_send = null_hci_send,
1991 .acl_send = null_hci_send,
1992 .bdaddr_set = null_hci_addr_set,
1993};
1994
1995struct HCIInfo *qemu_next_hci(void)
1996{
1997 if (cur_hci == nb_hcis)
1998 return &null_hci;
1999
2000 return hci_table[cur_hci++];
2001}
2002
balrogdc72ac12008-11-09 00:04:26 +00002003static struct HCIInfo *hci_init(const char *str)
2004{
2005 char *endp;
2006 struct bt_scatternet_s *vlan = 0;
2007
2008 if (!strcmp(str, "null"))
2009 /* null */
2010 return &null_hci;
2011 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
2012 /* host[:hciN] */
2013 return bt_host_hci(str[4] ? str + 5 : "hci0");
2014 else if (!strncmp(str, "hci", 3)) {
2015 /* hci[,vlan=n] */
2016 if (str[3]) {
2017 if (!strncmp(str + 3, ",vlan=", 6)) {
2018 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2019 if (*endp)
2020 vlan = 0;
2021 }
2022 } else
2023 vlan = qemu_find_bt_vlan(0);
2024 if (vlan)
2025 return bt_new_hci(vlan);
2026 }
2027
2028 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2029
2030 return 0;
2031}
2032
2033static int bt_hci_parse(const char *str)
2034{
2035 struct HCIInfo *hci;
2036 bdaddr_t bdaddr;
2037
2038 if (nb_hcis >= MAX_NICS) {
2039 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2040 return -1;
2041 }
2042
2043 hci = hci_init(str);
2044 if (!hci)
2045 return -1;
2046
2047 bdaddr.b[0] = 0x52;
2048 bdaddr.b[1] = 0x54;
2049 bdaddr.b[2] = 0x00;
2050 bdaddr.b[3] = 0x12;
2051 bdaddr.b[4] = 0x34;
2052 bdaddr.b[5] = 0x56 + nb_hcis;
2053 hci->bdaddr_set(hci, bdaddr.b);
2054
2055 hci_table[nb_hcis++] = hci;
2056
2057 return 0;
2058}
2059
2060static void bt_vhci_add(int vlan_id)
2061{
2062 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2063
2064 if (!vlan->slave)
2065 fprintf(stderr, "qemu: warning: adding a VHCI to "
2066 "an empty scatternet %i\n", vlan_id);
2067
2068 bt_vhci_init(bt_new_hci(vlan));
2069}
2070
2071static struct bt_device_s *bt_device_add(const char *opt)
2072{
2073 struct bt_scatternet_s *vlan;
2074 int vlan_id = 0;
2075 char *endp = strstr(opt, ",vlan=");
2076 int len = (endp ? endp - opt : strlen(opt)) + 1;
2077 char devname[10];
2078
2079 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2080
2081 if (endp) {
2082 vlan_id = strtol(endp + 6, &endp, 0);
2083 if (*endp) {
2084 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2085 return 0;
2086 }
2087 }
2088
2089 vlan = qemu_find_bt_vlan(vlan_id);
2090
2091 if (!vlan->slave)
2092 fprintf(stderr, "qemu: warning: adding a slave device to "
2093 "an empty scatternet %i\n", vlan_id);
2094
2095 if (!strcmp(devname, "keyboard"))
2096 return bt_keyboard_init(vlan);
2097
2098 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2099 return 0;
2100}
2101
2102static int bt_parse(const char *opt)
2103{
2104 const char *endp, *p;
2105 int vlan;
2106
2107 if (strstart(opt, "hci", &endp)) {
2108 if (!*endp || *endp == ',') {
2109 if (*endp)
2110 if (!strstart(endp, ",vlan=", 0))
2111 opt = endp + 1;
2112
2113 return bt_hci_parse(opt);
2114 }
2115 } else if (strstart(opt, "vhci", &endp)) {
2116 if (!*endp || *endp == ',') {
2117 if (*endp) {
2118 if (strstart(endp, ",vlan=", &p)) {
2119 vlan = strtol(p, (char **) &endp, 0);
2120 if (*endp) {
2121 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2122 return 1;
2123 }
2124 } else {
2125 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2126 return 1;
2127 }
2128 } else
2129 vlan = 0;
2130
2131 bt_vhci_add(vlan);
2132 return 0;
2133 }
2134 } else if (strstart(opt, "device:", &endp))
2135 return !bt_device_add(endp);
2136
2137 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2138 return 1;
2139}
2140
balrog1ae26a12008-09-28 23:19:47 +00002141/***********************************************************/
2142/* QEMU Block devices */
2143
balrog609497a2008-01-14 02:56:53 +00002144#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00002145#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00002146#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00002147#define PFLASH_ALIAS "if=pflash"
2148#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00002149#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002150
aliguori7d5aca92009-02-11 15:19:58 +00002151static int drive_opt_get_free_idx(void)
2152{
2153 int index;
2154
2155 for (index = 0; index < MAX_DRIVES; index++)
2156 if (!drives_opt[index].used) {
2157 drives_opt[index].used = 1;
2158 return index;
2159 }
2160
2161 return -1;
2162}
2163
2164static int drive_get_free_idx(void)
2165{
2166 int index;
2167
2168 for (index = 0; index < MAX_DRIVES; index++)
2169 if (!drives_table[index].used) {
2170 drives_table[index].used = 1;
2171 return index;
2172 }
2173
2174 return -1;
2175}
2176
aliguori4d73cd32009-02-11 15:20:46 +00002177int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002178{
2179 va_list ap;
aliguori7d5aca92009-02-11 15:19:58 +00002180 int index = drive_opt_get_free_idx();
thse4bcb142007-12-02 04:51:10 +00002181
aliguori7d5aca92009-02-11 15:19:58 +00002182 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
thse4bcb142007-12-02 04:51:10 +00002183 fprintf(stderr, "qemu: too many drives\n");
aliguori4d73cd32009-02-11 15:20:46 +00002184 return -1;
thse4bcb142007-12-02 04:51:10 +00002185 }
2186
aliguori7d5aca92009-02-11 15:19:58 +00002187 drives_opt[index].file = file;
thse4bcb142007-12-02 04:51:10 +00002188 va_start(ap, fmt);
aliguori7d5aca92009-02-11 15:19:58 +00002189 vsnprintf(drives_opt[index].opt,
balrog609497a2008-01-14 02:56:53 +00002190 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002191 va_end(ap);
2192
aliguori7d5aca92009-02-11 15:19:58 +00002193 nb_drives_opt++;
2194 return index;
thse4bcb142007-12-02 04:51:10 +00002195}
2196
aliguorib01b1112009-02-11 15:20:20 +00002197void drive_remove(int index)
2198{
2199 drives_opt[index].used = 0;
2200 nb_drives_opt--;
2201}
2202
thsf60d39b2007-12-17 03:55:57 +00002203int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002204{
2205 int index;
2206
2207 /* seek interface, bus and unit */
2208
aliguori7d5aca92009-02-11 15:19:58 +00002209 for (index = 0; index < MAX_DRIVES; index++)
thsf60d39b2007-12-17 03:55:57 +00002210 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002211 drives_table[index].bus == bus &&
aliguori7d5aca92009-02-11 15:19:58 +00002212 drives_table[index].unit == unit &&
2213 drives_table[index].used)
thse4bcb142007-12-02 04:51:10 +00002214 return index;
2215
2216 return -1;
2217}
2218
thsf60d39b2007-12-17 03:55:57 +00002219int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002220{
2221 int max_bus;
2222 int index;
2223
2224 max_bus = -1;
2225 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002226 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002227 drives_table[index].bus > max_bus)
2228 max_bus = drives_table[index].bus;
2229 }
2230 return max_bus;
2231}
2232
aliguorifa879c62009-01-07 17:32:33 +00002233const char *drive_get_serial(BlockDriverState *bdrv)
2234{
2235 int index;
2236
2237 for (index = 0; index < nb_drives; index++)
2238 if (drives_table[index].bdrv == bdrv)
2239 return drives_table[index].serial;
2240
2241 return "\0";
2242}
2243
aliguori428c5702009-01-21 18:59:04 +00002244BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2245{
2246 int index;
2247
2248 for (index = 0; index < nb_drives; index++)
2249 if (drives_table[index].bdrv == bdrv)
2250 return drives_table[index].onerror;
2251
aliguoricdad4bd2009-02-28 16:51:01 +00002252 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002253}
2254
aurel32a1620fa2008-04-29 05:58:01 +00002255static void bdrv_format_print(void *opaque, const char *name)
2256{
2257 fprintf(stderr, " %s", name);
2258}
2259
aliguorib01b1112009-02-11 15:20:20 +00002260void drive_uninit(BlockDriverState *bdrv)
2261{
2262 int i;
2263
2264 for (i = 0; i < MAX_DRIVES; i++)
2265 if (drives_table[i].bdrv == bdrv) {
2266 drives_table[i].bdrv = NULL;
2267 drives_table[i].used = 0;
2268 drive_remove(drives_table[i].drive_opt_idx);
2269 nb_drives--;
2270 break;
2271 }
2272}
2273
aliguori4d73cd32009-02-11 15:20:46 +00002274int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
thse4bcb142007-12-02 04:51:10 +00002275{
2276 char buf[128];
2277 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002278 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00002279 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002280 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002281 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002282 enum { MEDIA_DISK, MEDIA_CDROM } media;
2283 int bus_id, unit_id;
2284 int cyls, heads, secs, translation;
2285 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002286 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002287 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002288 int max_devs;
2289 int index;
balrog33f00272007-12-24 14:33:24 +00002290 int cache;
aliguori428c5702009-01-21 18:59:04 +00002291 int bdrv_flags, onerror;
aliguori7d5aca92009-02-11 15:19:58 +00002292 int drives_table_idx;
balrog609497a2008-01-14 02:56:53 +00002293 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002294 static const char * const params[] = { "bus", "unit", "if", "index",
2295 "cyls", "heads", "secs", "trans",
2296 "media", "snapshot", "file",
aliguori428c5702009-01-21 18:59:04 +00002297 "cache", "format", "serial", "werror",
2298 NULL };
thse4bcb142007-12-02 04:51:10 +00002299
2300 if (check_params(buf, sizeof(buf), params, str) < 0) {
balrogff993632008-02-10 13:21:25 +00002301 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
thse4bcb142007-12-02 04:51:10 +00002302 buf, str);
2303 return -1;
2304 }
2305
2306 file[0] = 0;
2307 cyls = heads = secs = 0;
2308 bus_id = 0;
2309 unit_id = -1;
2310 translation = BIOS_ATA_TRANSLATION_AUTO;
2311 index = -1;
aliguori4dc822d2008-12-04 21:39:21 +00002312 cache = 3;
thse4bcb142007-12-02 04:51:10 +00002313
blueswir1c9b1ae22008-09-28 18:55:17 +00002314 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002315 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002316 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002317 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002318 } else {
thsf60d39b2007-12-17 03:55:57 +00002319 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002320 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002321 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002322 }
2323 media = MEDIA_DISK;
2324
2325 /* extract parameters */
2326
2327 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2328 bus_id = strtol(buf, NULL, 0);
2329 if (bus_id < 0) {
2330 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2331 return -1;
2332 }
2333 }
2334
2335 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2336 unit_id = strtol(buf, NULL, 0);
2337 if (unit_id < 0) {
2338 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2339 return -1;
2340 }
2341 }
2342
2343 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002344 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002345 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002346 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002347 max_devs = MAX_IDE_DEVS;
2348 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002349 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002350 max_devs = MAX_SCSI_DEVS;
2351 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002352 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002353 max_devs = 0;
2354 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002355 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002356 max_devs = 0;
2357 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002358 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002359 max_devs = 0;
2360 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002361 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002362 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002363 } else if (!strcmp(buf, "virtio")) {
2364 type = IF_VIRTIO;
2365 max_devs = 0;
2366 } else {
thse4bcb142007-12-02 04:51:10 +00002367 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2368 return -1;
2369 }
2370 }
2371
2372 if (get_param_value(buf, sizeof(buf), "index", str)) {
2373 index = strtol(buf, NULL, 0);
2374 if (index < 0) {
2375 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2376 return -1;
2377 }
2378 }
2379
2380 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2381 cyls = strtol(buf, NULL, 0);
2382 }
2383
2384 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2385 heads = strtol(buf, NULL, 0);
2386 }
2387
2388 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2389 secs = strtol(buf, NULL, 0);
2390 }
2391
2392 if (cyls || heads || secs) {
2393 if (cyls < 1 || cyls > 16383) {
2394 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2395 return -1;
2396 }
2397 if (heads < 1 || heads > 16) {
2398 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2399 return -1;
2400 }
2401 if (secs < 1 || secs > 63) {
2402 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2403 return -1;
2404 }
2405 }
2406
2407 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2408 if (!cyls) {
2409 fprintf(stderr,
2410 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2411 str);
2412 return -1;
2413 }
2414 if (!strcmp(buf, "none"))
2415 translation = BIOS_ATA_TRANSLATION_NONE;
2416 else if (!strcmp(buf, "lba"))
2417 translation = BIOS_ATA_TRANSLATION_LBA;
2418 else if (!strcmp(buf, "auto"))
2419 translation = BIOS_ATA_TRANSLATION_AUTO;
2420 else {
2421 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2422 return -1;
2423 }
2424 }
2425
2426 if (get_param_value(buf, sizeof(buf), "media", str)) {
2427 if (!strcmp(buf, "disk")) {
2428 media = MEDIA_DISK;
2429 } else if (!strcmp(buf, "cdrom")) {
2430 if (cyls || secs || heads) {
2431 fprintf(stderr,
2432 "qemu: '%s' invalid physical CHS format\n", str);
2433 return -1;
2434 }
2435 media = MEDIA_CDROM;
2436 } else {
2437 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2438 return -1;
2439 }
2440 }
2441
2442 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2443 if (!strcmp(buf, "on"))
2444 snapshot = 1;
2445 else if (!strcmp(buf, "off"))
2446 snapshot = 0;
2447 else {
2448 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2449 return -1;
2450 }
2451 }
2452
balrog33f00272007-12-24 14:33:24 +00002453 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002454 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002455 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002456 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002457 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002458 else if (!strcmp(buf, "writeback"))
2459 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002460 else {
2461 fprintf(stderr, "qemu: invalid cache option\n");
2462 return -1;
2463 }
2464 }
2465
aurel321e72d3b2008-04-28 20:26:45 +00002466 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002467 if (strcmp(buf, "?") == 0) {
2468 fprintf(stderr, "qemu: Supported formats:");
2469 bdrv_iterate_format(bdrv_format_print, NULL);
2470 fprintf(stderr, "\n");
2471 return -1;
2472 }
aurel321e72d3b2008-04-28 20:26:45 +00002473 drv = bdrv_find_format(buf);
2474 if (!drv) {
2475 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2476 return -1;
2477 }
2478 }
2479
balrog609497a2008-01-14 02:56:53 +00002480 if (arg->file == NULL)
2481 get_param_value(file, sizeof(file), "file", str);
2482 else
2483 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002484
aliguorifa879c62009-01-07 17:32:33 +00002485 if (!get_param_value(serial, sizeof(serial), "serial", str))
2486 memset(serial, 0, sizeof(serial));
2487
aliguoricdad4bd2009-02-28 16:51:01 +00002488 onerror = BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002489 if (get_param_value(buf, sizeof(serial), "werror", str)) {
aliguori869a5c62009-01-22 19:52:25 +00002490 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002491 fprintf(stderr, "werror is no supported by this format\n");
aliguori428c5702009-01-21 18:59:04 +00002492 return -1;
2493 }
2494 if (!strcmp(buf, "ignore"))
2495 onerror = BLOCK_ERR_IGNORE;
2496 else if (!strcmp(buf, "enospc"))
2497 onerror = BLOCK_ERR_STOP_ENOSPC;
2498 else if (!strcmp(buf, "stop"))
2499 onerror = BLOCK_ERR_STOP_ANY;
2500 else if (!strcmp(buf, "report"))
2501 onerror = BLOCK_ERR_REPORT;
2502 else {
2503 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2504 return -1;
2505 }
2506 }
2507
thse4bcb142007-12-02 04:51:10 +00002508 /* compute bus and unit according index */
2509
2510 if (index != -1) {
2511 if (bus_id != 0 || unit_id != -1) {
2512 fprintf(stderr,
2513 "qemu: '%s' index cannot be used with bus and unit\n", str);
2514 return -1;
2515 }
2516 if (max_devs == 0)
2517 {
2518 unit_id = index;
2519 bus_id = 0;
2520 } else {
2521 unit_id = index % max_devs;
2522 bus_id = index / max_devs;
2523 }
2524 }
2525
2526 /* if user doesn't specify a unit_id,
2527 * try to find the first free
2528 */
2529
2530 if (unit_id == -1) {
2531 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002532 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002533 unit_id++;
2534 if (max_devs && unit_id >= max_devs) {
2535 unit_id -= max_devs;
2536 bus_id++;
2537 }
2538 }
2539 }
2540
2541 /* check unit id */
2542
2543 if (max_devs && unit_id >= max_devs) {
2544 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2545 str, unit_id, max_devs - 1);
2546 return -1;
2547 }
2548
2549 /*
2550 * ignore multiple definitions
2551 */
2552
thsf60d39b2007-12-17 03:55:57 +00002553 if (drive_get_index(type, bus_id, unit_id) != -1)
aliguori4d73cd32009-02-11 15:20:46 +00002554 return -2;
thse4bcb142007-12-02 04:51:10 +00002555
2556 /* init */
2557
thsf60d39b2007-12-17 03:55:57 +00002558 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002559 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002560 if (max_devs)
2561 snprintf(buf, sizeof(buf), "%s%i%s%i",
2562 devname, bus_id, mediastr, unit_id);
2563 else
2564 snprintf(buf, sizeof(buf), "%s%s%i",
2565 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002566 bdrv = bdrv_new(buf);
aliguori7d5aca92009-02-11 15:19:58 +00002567 drives_table_idx = drive_get_free_idx();
2568 drives_table[drives_table_idx].bdrv = bdrv;
2569 drives_table[drives_table_idx].type = type;
2570 drives_table[drives_table_idx].bus = bus_id;
2571 drives_table[drives_table_idx].unit = unit_id;
2572 drives_table[drives_table_idx].onerror = onerror;
aliguorib01b1112009-02-11 15:20:20 +00002573 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
aliguorifa879c62009-01-07 17:32:33 +00002574 strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002575 nb_drives++;
2576
thsf60d39b2007-12-17 03:55:57 +00002577 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002578 case IF_IDE:
2579 case IF_SCSI:
2580 switch(media) {
2581 case MEDIA_DISK:
2582 if (cyls != 0) {
2583 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2584 bdrv_set_translation_hint(bdrv, translation);
2585 }
2586 break;
2587 case MEDIA_CDROM:
2588 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2589 break;
2590 }
2591 break;
2592 case IF_SD:
2593 /* FIXME: This isn't really a floppy, but it's a reasonable
2594 approximation. */
2595 case IF_FLOPPY:
2596 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2597 break;
2598 case IF_PFLASH:
2599 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002600 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002601 break;
2602 }
2603 if (!file[0])
aliguori4d73cd32009-02-11 15:20:46 +00002604 return -2;
balrog33f00272007-12-24 14:33:24 +00002605 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002606 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002607 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002608 cache = 2; /* always use write-back with snapshot */
2609 }
2610 if (cache == 0) /* no caching */
2611 bdrv_flags |= BDRV_O_NOCACHE;
2612 else if (cache == 2) /* write-back */
2613 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori4dc822d2008-12-04 21:39:21 +00002614 else if (cache == 3) /* not specified */
2615 bdrv_flags |= BDRV_O_CACHE_DEF;
aliguoric0f4ce72009-03-05 23:01:01 +00002616 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002617 fprintf(stderr, "qemu: could not open disk image %s\n",
2618 file);
2619 return -1;
2620 }
aliguoric0f4ce72009-03-05 23:01:01 +00002621 if (bdrv_key_required(bdrv))
2622 autostart = 0;
aliguori4d73cd32009-02-11 15:20:46 +00002623 return drives_table_idx;
thse4bcb142007-12-02 04:51:10 +00002624}
2625
bellard330d0412003-07-26 18:11:40 +00002626/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002627/* USB devices */
2628
pbrook0d92ed32006-05-21 16:30:15 +00002629static USBPort *used_usb_ports;
2630static USBPort *free_usb_ports;
2631
2632/* ??? Maybe change this to register a hub to keep track of the topology. */
2633void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2634 usb_attachfn attach)
2635{
2636 port->opaque = opaque;
2637 port->index = index;
2638 port->attach = attach;
2639 port->next = free_usb_ports;
2640 free_usb_ports = port;
2641}
2642
aliguori4b096fc2008-08-21 19:28:55 +00002643int usb_device_add_dev(USBDevice *dev)
2644{
2645 USBPort *port;
2646
2647 /* Find a USB port to add the device to. */
2648 port = free_usb_ports;
2649 if (!port->next) {
2650 USBDevice *hub;
2651
2652 /* Create a new hub and chain it on. */
2653 free_usb_ports = NULL;
2654 port->next = used_usb_ports;
2655 used_usb_ports = port;
2656
2657 hub = usb_hub_init(VM_USB_HUB_SIZE);
2658 usb_attach(port, hub);
2659 port = free_usb_ports;
2660 }
2661
2662 free_usb_ports = port->next;
2663 port->next = used_usb_ports;
2664 used_usb_ports = port;
2665 usb_attach(port, dev);
2666 return 0;
2667}
2668
aliguoribb5fc202009-03-05 23:01:15 +00002669static void usb_msd_password_cb(void *opaque, int err)
2670{
2671 USBDevice *dev = opaque;
2672
2673 if (!err)
2674 usb_device_add_dev(dev);
2675 else
2676 dev->handle_destroy(dev);
2677}
2678
aliguoric0f4ce72009-03-05 23:01:01 +00002679static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002680{
2681 const char *p;
2682 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002683
pbrook0d92ed32006-05-21 16:30:15 +00002684 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002685 return -1;
2686
2687 if (strstart(devname, "host:", &p)) {
2688 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002689 } else if (!strcmp(devname, "mouse")) {
2690 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002691 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002692 dev = usb_tablet_init();
2693 } else if (!strcmp(devname, "keyboard")) {
2694 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002695 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002696 BlockDriverState *bs;
2697
aliguoribb5fc202009-03-05 23:01:15 +00002698 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002699 if (!dev)
2700 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002701 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002702 if (bdrv_key_required(bs)) {
2703 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002704 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002705 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2706 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002707 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002708 }
2709 }
balrogf6d2a312007-06-10 19:21:04 +00002710 } else if (!strcmp(devname, "wacom-tablet")) {
2711 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002712 } else if (strstart(devname, "serial:", &p)) {
2713 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002714#ifdef CONFIG_BRLAPI
2715 } else if (!strcmp(devname, "braille")) {
2716 dev = usb_baum_init();
2717#endif
balrog6c9f8862008-07-17 20:47:13 +00002718 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002719 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002720
balrog9ad97e62008-07-29 13:16:31 +00002721 if (net_client_init("nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002722 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002723 nd_table[nic].model = "usb";
2724 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002725 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2726 dev = usb_bt_init(devname[2] ? hci_init(p) :
2727 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002728 } else {
2729 return -1;
2730 }
pbrook0d92ed32006-05-21 16:30:15 +00002731 if (!dev)
2732 return -1;
2733
aliguori4b096fc2008-08-21 19:28:55 +00002734 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002735}
2736
aliguori1f3870a2008-08-21 19:27:48 +00002737int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002738{
pbrook0d92ed32006-05-21 16:30:15 +00002739 USBPort *port;
2740 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002741 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002742
pbrook0d92ed32006-05-21 16:30:15 +00002743 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002744 return -1;
2745
bellarda594cfb2005-11-06 16:13:29 +00002746 if (bus_num != 0)
2747 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002748
2749 lastp = &used_usb_ports;
2750 port = used_usb_ports;
2751 while (port && port->dev->addr != addr) {
2752 lastp = &port->next;
2753 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002754 }
pbrook0d92ed32006-05-21 16:30:15 +00002755
2756 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002757 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002758
bellard059809e2006-07-19 18:06:15 +00002759 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002760 *lastp = port->next;
2761 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002762 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002763 port->next = free_usb_ports;
2764 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002765 return 0;
2766}
2767
aliguori1f3870a2008-08-21 19:27:48 +00002768static int usb_device_del(const char *devname)
2769{
2770 int bus_num, addr;
2771 const char *p;
2772
aliguori5d0c5752008-09-14 01:07:41 +00002773 if (strstart(devname, "host:", &p))
2774 return usb_host_device_close(p);
2775
aliguori1f3870a2008-08-21 19:27:48 +00002776 if (!used_usb_ports)
2777 return -1;
2778
2779 p = strchr(devname, '.');
2780 if (!p)
2781 return -1;
2782 bus_num = strtoul(devname, NULL, 0);
2783 addr = strtoul(p + 1, NULL, 0);
2784
2785 return usb_device_del_addr(bus_num, addr);
2786}
2787
aliguori376253e2009-03-05 23:01:23 +00002788void do_usb_add(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002789{
aliguoric0f4ce72009-03-05 23:01:01 +00002790 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002791}
2792
aliguori376253e2009-03-05 23:01:23 +00002793void do_usb_del(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002794{
aliguori4b096fc2008-08-21 19:28:55 +00002795 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002796}
2797
aliguori376253e2009-03-05 23:01:23 +00002798void usb_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00002799{
2800 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002801 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002802 const char *speed_str;
2803
pbrook0d92ed32006-05-21 16:30:15 +00002804 if (!usb_enabled) {
aliguori376253e2009-03-05 23:01:23 +00002805 monitor_printf(mon, "USB support not enabled\n");
bellarda594cfb2005-11-06 16:13:29 +00002806 return;
2807 }
2808
pbrook0d92ed32006-05-21 16:30:15 +00002809 for (port = used_usb_ports; port; port = port->next) {
2810 dev = port->dev;
2811 if (!dev)
2812 continue;
2813 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002814 case USB_SPEED_LOW:
2815 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002816 break;
ths5fafdf22007-09-16 21:08:06 +00002817 case USB_SPEED_FULL:
2818 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002819 break;
ths5fafdf22007-09-16 21:08:06 +00002820 case USB_SPEED_HIGH:
2821 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002822 break;
2823 default:
ths5fafdf22007-09-16 21:08:06 +00002824 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002825 break;
bellarda594cfb2005-11-06 16:13:29 +00002826 }
aliguori376253e2009-03-05 23:01:23 +00002827 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2828 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002829 }
2830}
2831
bellardf7cce892004-12-08 22:21:25 +00002832/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002833/* PCMCIA/Cardbus */
2834
2835static struct pcmcia_socket_entry_s {
2836 struct pcmcia_socket_s *socket;
2837 struct pcmcia_socket_entry_s *next;
2838} *pcmcia_sockets = 0;
2839
2840void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2841{
2842 struct pcmcia_socket_entry_s *entry;
2843
2844 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2845 entry->socket = socket;
2846 entry->next = pcmcia_sockets;
2847 pcmcia_sockets = entry;
2848}
2849
2850void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2851{
2852 struct pcmcia_socket_entry_s *entry, **ptr;
2853
2854 ptr = &pcmcia_sockets;
2855 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2856 if (entry->socket == socket) {
2857 *ptr = entry->next;
2858 qemu_free(entry);
2859 }
2860}
2861
aliguori376253e2009-03-05 23:01:23 +00002862void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002863{
2864 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002865
balrog201a51f2007-04-30 00:51:09 +00002866 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002867 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002868
2869 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002870 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2871 iter->socket->attached ? iter->socket->card_string :
2872 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002873}
2874
2875/***********************************************************/
aliguori3023f3322009-01-16 19:04:14 +00002876/* register display */
2877
aliguori7b5d76d2009-03-13 15:02:13 +00002878struct DisplayAllocator default_allocator = {
2879 defaultallocator_create_displaysurface,
2880 defaultallocator_resize_displaysurface,
2881 defaultallocator_free_displaysurface
2882};
2883
aliguori3023f3322009-01-16 19:04:14 +00002884void register_displaystate(DisplayState *ds)
2885{
2886 DisplayState **s;
2887 s = &display_state;
2888 while (*s != NULL)
2889 s = &(*s)->next;
2890 ds->next = NULL;
2891 *s = ds;
2892}
2893
2894DisplayState *get_displaystate(void)
2895{
2896 return display_state;
2897}
2898
aliguori7b5d76d2009-03-13 15:02:13 +00002899DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2900{
2901 if(ds->allocator == &default_allocator) ds->allocator = da;
2902 return ds->allocator;
2903}
2904
ths2ff89792007-06-21 23:34:19 +00002905/* dumb display */
2906
aliguori8f391ab2009-01-19 16:34:10 +00002907static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002908{
aliguori8f391ab2009-01-19 16:34:10 +00002909 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002910 ds->allocator = &default_allocator;
2911 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002912 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002913}
2914
2915/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002916/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002917
bellardc4b1fcc2004-03-14 21:44:30 +00002918typedef struct IOHandlerRecord {
2919 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002920 IOCanRWHandler *fd_read_poll;
2921 IOHandler *fd_read;
2922 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002923 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002924 void *opaque;
2925 /* temporary data */
2926 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002927 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002928} IOHandlerRecord;
2929
bellard8a7ddc32004-03-31 19:00:16 +00002930static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002931
bellard7c9d8e02005-11-15 22:16:05 +00002932/* XXX: fd_read_poll should be suppressed, but an API change is
2933 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002934int qemu_set_fd_handler2(int fd,
2935 IOCanRWHandler *fd_read_poll,
2936 IOHandler *fd_read,
2937 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002938 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002939{
bellard8a7ddc32004-03-31 19:00:16 +00002940 IOHandlerRecord **pioh, *ioh;
2941
bellard7c9d8e02005-11-15 22:16:05 +00002942 if (!fd_read && !fd_write) {
2943 pioh = &first_io_handler;
2944 for(;;) {
2945 ioh = *pioh;
2946 if (ioh == NULL)
2947 break;
2948 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002949 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002950 break;
2951 }
2952 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002953 }
bellard7c9d8e02005-11-15 22:16:05 +00002954 } else {
2955 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2956 if (ioh->fd == fd)
2957 goto found;
2958 }
2959 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002960 ioh->next = first_io_handler;
2961 first_io_handler = ioh;
2962 found:
2963 ioh->fd = fd;
2964 ioh->fd_read_poll = fd_read_poll;
2965 ioh->fd_read = fd_read;
2966 ioh->fd_write = fd_write;
2967 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002968 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002969 }
bellard7c9d8e02005-11-15 22:16:05 +00002970 return 0;
2971}
2972
ths5fafdf22007-09-16 21:08:06 +00002973int qemu_set_fd_handler(int fd,
2974 IOHandler *fd_read,
2975 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002976 void *opaque)
2977{
2978 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002979}
2980
aliguori56f3a5d2008-10-31 18:07:17 +00002981#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002982/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002983/* Polling handling */
2984
2985typedef struct PollingEntry {
2986 PollingFunc *func;
2987 void *opaque;
2988 struct PollingEntry *next;
2989} PollingEntry;
2990
2991static PollingEntry *first_polling_entry;
2992
2993int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2994{
2995 PollingEntry **ppe, *pe;
2996 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002997 pe->func = func;
2998 pe->opaque = opaque;
2999 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
3000 *ppe = pe;
3001 return 0;
3002}
3003
3004void qemu_del_polling_cb(PollingFunc *func, void *opaque)
3005{
3006 PollingEntry **ppe, *pe;
3007 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
3008 pe = *ppe;
3009 if (pe->func == func && pe->opaque == opaque) {
3010 *ppe = pe->next;
3011 qemu_free(pe);
3012 break;
3013 }
3014 }
3015}
3016
bellarda18e5242006-06-25 17:18:27 +00003017/***********************************************************/
3018/* Wait objects support */
3019typedef struct WaitObjects {
3020 int num;
3021 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
3022 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
3023 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
3024} WaitObjects;
3025
3026static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00003027
bellarda18e5242006-06-25 17:18:27 +00003028int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3029{
3030 WaitObjects *w = &wait_objects;
3031
3032 if (w->num >= MAXIMUM_WAIT_OBJECTS)
3033 return -1;
3034 w->events[w->num] = handle;
3035 w->func[w->num] = func;
3036 w->opaque[w->num] = opaque;
3037 w->num++;
3038 return 0;
3039}
3040
3041void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3042{
3043 int i, found;
3044 WaitObjects *w = &wait_objects;
3045
3046 found = 0;
3047 for (i = 0; i < w->num; i++) {
3048 if (w->events[i] == handle)
3049 found = 1;
3050 if (found) {
3051 w->events[i] = w->events[i + 1];
3052 w->func[i] = w->func[i + 1];
3053 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00003054 }
bellarda18e5242006-06-25 17:18:27 +00003055 }
3056 if (found)
3057 w->num--;
3058}
3059#endif
3060
bellard8a7ddc32004-03-31 19:00:16 +00003061/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003062/* ram save/restore */
3063
bellard8a7ddc32004-03-31 19:00:16 +00003064static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3065{
3066 int v;
3067
3068 v = qemu_get_byte(f);
3069 switch(v) {
3070 case 0:
3071 if (qemu_get_buffer(f, buf, len) != len)
3072 return -EIO;
3073 break;
3074 case 1:
3075 v = qemu_get_byte(f);
3076 memset(buf, v, len);
3077 break;
3078 default:
3079 return -EINVAL;
3080 }
aliguori871d2f02008-10-13 03:07:56 +00003081
3082 if (qemu_file_has_error(f))
3083 return -EIO;
3084
bellard8a7ddc32004-03-31 19:00:16 +00003085 return 0;
3086}
3087
bellardc88676f2006-08-06 13:36:11 +00003088static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003089{
aurel3200f82b82008-04-27 21:12:55 +00003090 int ret;
3091 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00003092
bellard8a7ddc32004-03-31 19:00:16 +00003093 if (qemu_get_be32(f) != phys_ram_size)
3094 return -EINVAL;
3095 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
3096 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
3097 if (ret)
3098 return ret;
3099 }
3100 return 0;
3101}
3102
bellardc88676f2006-08-06 13:36:11 +00003103#define BDRV_HASH_BLOCK_SIZE 1024
3104#define IOBUF_SIZE 4096
3105#define RAM_CBLOCK_MAGIC 0xfabe
3106
bellardc88676f2006-08-06 13:36:11 +00003107typedef struct RamDecompressState {
3108 z_stream zstream;
3109 QEMUFile *f;
3110 uint8_t buf[IOBUF_SIZE];
3111} RamDecompressState;
3112
3113static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3114{
3115 int ret;
3116 memset(s, 0, sizeof(*s));
3117 s->f = f;
3118 ret = inflateInit(&s->zstream);
3119 if (ret != Z_OK)
3120 return -1;
3121 return 0;
3122}
3123
3124static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3125{
3126 int ret, clen;
3127
3128 s->zstream.avail_out = len;
3129 s->zstream.next_out = buf;
3130 while (s->zstream.avail_out > 0) {
3131 if (s->zstream.avail_in == 0) {
3132 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3133 return -1;
3134 clen = qemu_get_be16(s->f);
3135 if (clen > IOBUF_SIZE)
3136 return -1;
3137 qemu_get_buffer(s->f, s->buf, clen);
3138 s->zstream.avail_in = clen;
3139 s->zstream.next_in = s->buf;
3140 }
3141 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3142 if (ret != Z_OK && ret != Z_STREAM_END) {
3143 return -1;
3144 }
3145 }
3146 return 0;
3147}
3148
3149static void ram_decompress_close(RamDecompressState *s)
3150{
3151 inflateEnd(&s->zstream);
3152}
3153
aliguori475e4272008-10-06 20:21:51 +00003154#define RAM_SAVE_FLAG_FULL 0x01
3155#define RAM_SAVE_FLAG_COMPRESS 0x02
3156#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3157#define RAM_SAVE_FLAG_PAGE 0x08
3158#define RAM_SAVE_FLAG_EOS 0x10
3159
3160static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003161{
aliguori475e4272008-10-06 20:21:51 +00003162 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3163 uint32_t *array = (uint32_t *)page;
3164 int i;
ths3b46e622007-09-17 08:09:54 +00003165
aliguori475e4272008-10-06 20:21:51 +00003166 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3167 if (array[i] != val)
3168 return 0;
bellardc88676f2006-08-06 13:36:11 +00003169 }
aliguori475e4272008-10-06 20:21:51 +00003170
3171 return 1;
bellardc88676f2006-08-06 13:36:11 +00003172}
3173
aliguori475e4272008-10-06 20:21:51 +00003174static int ram_save_block(QEMUFile *f)
3175{
3176 static ram_addr_t current_addr = 0;
3177 ram_addr_t saved_addr = current_addr;
3178 ram_addr_t addr = 0;
3179 int found = 0;
3180
3181 while (addr < phys_ram_size) {
3182 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3183 uint8_t ch;
3184
3185 cpu_physical_memory_reset_dirty(current_addr,
3186 current_addr + TARGET_PAGE_SIZE,
3187 MIGRATION_DIRTY_FLAG);
3188
3189 ch = *(phys_ram_base + current_addr);
3190
3191 if (is_dup_page(phys_ram_base + current_addr, ch)) {
3192 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3193 qemu_put_byte(f, ch);
3194 } else {
3195 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3196 qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
3197 }
3198
3199 found = 1;
3200 break;
3201 }
3202 addr += TARGET_PAGE_SIZE;
3203 current_addr = (saved_addr + addr) % phys_ram_size;
3204 }
3205
3206 return found;
3207}
3208
3209static ram_addr_t ram_save_threshold = 10;
3210
3211static ram_addr_t ram_save_remaining(void)
3212{
3213 ram_addr_t addr;
3214 ram_addr_t count = 0;
3215
3216 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3217 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3218 count++;
3219 }
3220
3221 return count;
3222}
3223
3224static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3225{
3226 ram_addr_t addr;
3227
3228 if (stage == 1) {
3229 /* Make sure all dirty bits are set */
3230 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3231 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3232 cpu_physical_memory_set_dirty(addr);
3233 }
3234
3235 /* Enable dirty memory tracking */
3236 cpu_physical_memory_set_dirty_tracking(1);
3237
3238 qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
3239 }
3240
3241 while (!qemu_file_rate_limit(f)) {
3242 int ret;
3243
3244 ret = ram_save_block(f);
3245 if (ret == 0) /* no more blocks */
3246 break;
3247 }
3248
3249 /* try transferring iterative blocks of memory */
3250
3251 if (stage == 3) {
3252 cpu_physical_memory_set_dirty_tracking(0);
3253
3254 /* flush all remaining blocks regardless of rate limiting */
3255 while (ram_save_block(f) != 0);
3256 }
3257
3258 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3259
3260 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3261}
3262
3263static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003264{
3265 RamDecompressState s1, *s = &s1;
3266 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003267 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003268
bellardc88676f2006-08-06 13:36:11 +00003269 if (ram_decompress_open(s, f) < 0)
3270 return -EINVAL;
3271 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
3272 if (ram_decompress_buf(s, buf, 1) < 0) {
3273 fprintf(stderr, "Error while reading ram block header\n");
3274 goto error;
3275 }
3276 if (buf[0] == 0) {
3277 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003278 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003279 goto error;
3280 }
aliguori475e4272008-10-06 20:21:51 +00003281 } else {
bellardc88676f2006-08-06 13:36:11 +00003282 error:
3283 printf("Error block header\n");
3284 return -EINVAL;
3285 }
3286 }
3287 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003288
3289 return 0;
3290}
3291
3292static int ram_load(QEMUFile *f, void *opaque, int version_id)
3293{
3294 ram_addr_t addr;
3295 int flags;
3296
3297 if (version_id == 1)
3298 return ram_load_v1(f, opaque);
3299
3300 if (version_id == 2) {
3301 if (qemu_get_be32(f) != phys_ram_size)
3302 return -EINVAL;
3303 return ram_load_dead(f, opaque);
3304 }
3305
3306 if (version_id != 3)
3307 return -EINVAL;
3308
3309 do {
3310 addr = qemu_get_be64(f);
3311
3312 flags = addr & ~TARGET_PAGE_MASK;
3313 addr &= TARGET_PAGE_MASK;
3314
3315 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3316 if (addr != phys_ram_size)
3317 return -EINVAL;
3318 }
3319
3320 if (flags & RAM_SAVE_FLAG_FULL) {
3321 if (ram_load_dead(f, opaque) < 0)
3322 return -EINVAL;
3323 }
3324
3325 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3326 uint8_t ch = qemu_get_byte(f);
3327 memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
3328 } else if (flags & RAM_SAVE_FLAG_PAGE)
3329 qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
3330 } while (!(flags & RAM_SAVE_FLAG_EOS));
3331
bellardc88676f2006-08-06 13:36:11 +00003332 return 0;
3333}
3334
aliguori9e472e12008-10-08 19:50:24 +00003335void qemu_service_io(void)
3336{
3337 CPUState *env = cpu_single_env;
3338 if (env) {
aurel323098dba2009-03-07 21:28:24 +00003339 cpu_exit(env);
aliguori9e472e12008-10-08 19:50:24 +00003340#ifdef USE_KQEMU
3341 if (env->kqemu_enabled) {
3342 kqemu_cpu_interrupt(env);
3343 }
3344#endif
3345 }
3346}
3347
bellard8a7ddc32004-03-31 19:00:16 +00003348/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003349/* bottom halves (can be seen as timers which expire ASAP) */
3350
3351struct QEMUBH {
3352 QEMUBHFunc *cb;
3353 void *opaque;
3354 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003355 int idle;
3356 int deleted;
bellard83f64092006-08-01 16:21:11 +00003357 QEMUBH *next;
3358};
3359
3360static QEMUBH *first_bh = NULL;
3361
3362QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3363{
3364 QEMUBH *bh;
3365 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003366 bh->cb = cb;
3367 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003368 bh->next = first_bh;
3369 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003370 return bh;
3371}
3372
bellard6eb57332006-08-06 09:51:25 +00003373int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003374{
aliguori1b435b12008-10-31 17:24:21 +00003375 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003376 int ret;
bellard83f64092006-08-01 16:21:11 +00003377
bellard6eb57332006-08-06 09:51:25 +00003378 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003379 for (bh = first_bh; bh; bh = bh->next) {
3380 if (!bh->deleted && bh->scheduled) {
3381 bh->scheduled = 0;
3382 if (!bh->idle)
3383 ret = 1;
3384 bh->idle = 0;
3385 bh->cb(bh->opaque);
3386 }
bellard83f64092006-08-01 16:21:11 +00003387 }
aliguori1b435b12008-10-31 17:24:21 +00003388
3389 /* remove deleted bhs */
3390 bhp = &first_bh;
3391 while (*bhp) {
3392 bh = *bhp;
3393 if (bh->deleted) {
3394 *bhp = bh->next;
3395 qemu_free(bh);
3396 } else
3397 bhp = &bh->next;
3398 }
3399
bellard6eb57332006-08-06 09:51:25 +00003400 return ret;
bellard83f64092006-08-01 16:21:11 +00003401}
3402
aliguori1b435b12008-10-31 17:24:21 +00003403void qemu_bh_schedule_idle(QEMUBH *bh)
3404{
3405 if (bh->scheduled)
3406 return;
3407 bh->scheduled = 1;
3408 bh->idle = 1;
3409}
3410
bellard83f64092006-08-01 16:21:11 +00003411void qemu_bh_schedule(QEMUBH *bh)
3412{
3413 CPUState *env = cpu_single_env;
3414 if (bh->scheduled)
3415 return;
3416 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003417 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003418 /* stop the currently executing CPU to execute the BH ASAP */
3419 if (env) {
aurel323098dba2009-03-07 21:28:24 +00003420 cpu_exit(env);
bellard83f64092006-08-01 16:21:11 +00003421 }
3422}
3423
3424void qemu_bh_cancel(QEMUBH *bh)
3425{
aliguori1b435b12008-10-31 17:24:21 +00003426 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003427}
3428
3429void qemu_bh_delete(QEMUBH *bh)
3430{
aliguori1b435b12008-10-31 17:24:21 +00003431 bh->scheduled = 0;
3432 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003433}
3434
aliguori56f3a5d2008-10-31 18:07:17 +00003435static void qemu_bh_update_timeout(int *timeout)
3436{
3437 QEMUBH *bh;
3438
3439 for (bh = first_bh; bh; bh = bh->next) {
3440 if (!bh->deleted && bh->scheduled) {
3441 if (bh->idle) {
3442 /* idle bottom halves will be polled at least
3443 * every 10ms */
3444 *timeout = MIN(10, *timeout);
3445 } else {
3446 /* non-idle bottom halves will be executed
3447 * immediately */
3448 *timeout = 0;
3449 break;
3450 }
3451 }
3452 }
3453}
3454
bellard83f64092006-08-01 16:21:11 +00003455/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003456/* machine registration */
3457
blueswir1bdaf78e2008-10-04 07:24:27 +00003458static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003459QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003460
3461int qemu_register_machine(QEMUMachine *m)
3462{
3463 QEMUMachine **pm;
3464 pm = &first_machine;
3465 while (*pm != NULL)
3466 pm = &(*pm)->next;
3467 m->next = NULL;
3468 *pm = m;
3469 return 0;
3470}
3471
pbrook9596ebb2007-11-18 01:44:38 +00003472static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003473{
3474 QEMUMachine *m;
3475
3476 for(m = first_machine; m != NULL; m = m->next) {
3477 if (!strcmp(m->name, name))
3478 return m;
3479 }
3480 return NULL;
3481}
3482
3483/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003484/* main execution loop */
3485
pbrook9596ebb2007-11-18 01:44:38 +00003486static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003487{
aliguori7d957bd2009-01-15 22:14:11 +00003488 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003489 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003490 DisplayChangeListener *dcl = ds->listeners;
3491
3492 dpy_refresh(ds);
3493
3494 while (dcl != NULL) {
3495 if (dcl->gui_timer_interval &&
3496 dcl->gui_timer_interval < interval)
3497 interval = dcl->gui_timer_interval;
3498 dcl = dcl->next;
3499 }
3500 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003501}
3502
blueswir19043b622009-01-21 19:28:13 +00003503static void nographic_update(void *opaque)
3504{
3505 uint64_t interval = GUI_REFRESH_INTERVAL;
3506
3507 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3508}
3509
bellard0bd48852005-11-11 00:00:47 +00003510struct vm_change_state_entry {
3511 VMChangeStateHandler *cb;
3512 void *opaque;
3513 LIST_ENTRY (vm_change_state_entry) entries;
3514};
3515
3516static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3517
3518VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3519 void *opaque)
3520{
3521 VMChangeStateEntry *e;
3522
3523 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003524
3525 e->cb = cb;
3526 e->opaque = opaque;
3527 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3528 return e;
3529}
3530
3531void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3532{
3533 LIST_REMOVE (e, entries);
3534 qemu_free (e);
3535}
3536
aliguori9781e042009-01-22 17:15:29 +00003537static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003538{
3539 VMChangeStateEntry *e;
3540
3541 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003542 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003543 }
3544}
3545
bellard8a7ddc32004-03-31 19:00:16 +00003546void vm_start(void)
3547{
3548 if (!vm_running) {
3549 cpu_enable_ticks();
3550 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003551 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003552 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00003553 }
3554}
3555
ths5fafdf22007-09-16 21:08:06 +00003556void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00003557{
3558 if (vm_running) {
3559 cpu_disable_ticks();
3560 vm_running = 0;
aliguori9781e042009-01-22 17:15:29 +00003561 vm_state_notify(0, reason);
bellard8a7ddc32004-03-31 19:00:16 +00003562 }
3563}
3564
bellardbb0c6722004-06-20 12:37:32 +00003565/* reset/shutdown handler */
3566
3567typedef struct QEMUResetEntry {
3568 QEMUResetHandler *func;
3569 void *opaque;
3570 struct QEMUResetEntry *next;
3571} QEMUResetEntry;
3572
3573static QEMUResetEntry *first_reset_entry;
3574static int reset_requested;
3575static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003576static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00003577
aurel32cf7a2fe2008-03-18 06:53:05 +00003578int qemu_shutdown_requested(void)
3579{
3580 int r = shutdown_requested;
3581 shutdown_requested = 0;
3582 return r;
3583}
3584
3585int qemu_reset_requested(void)
3586{
3587 int r = reset_requested;
3588 reset_requested = 0;
3589 return r;
3590}
3591
3592int qemu_powerdown_requested(void)
3593{
3594 int r = powerdown_requested;
3595 powerdown_requested = 0;
3596 return r;
3597}
3598
bellardbb0c6722004-06-20 12:37:32 +00003599void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3600{
3601 QEMUResetEntry **pre, *re;
3602
3603 pre = &first_reset_entry;
3604 while (*pre != NULL)
3605 pre = &(*pre)->next;
3606 re = qemu_mallocz(sizeof(QEMUResetEntry));
3607 re->func = func;
3608 re->opaque = opaque;
3609 re->next = NULL;
3610 *pre = re;
3611}
3612
aurel32cf7a2fe2008-03-18 06:53:05 +00003613void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00003614{
3615 QEMUResetEntry *re;
3616
3617 /* reset all devices */
3618 for(re = first_reset_entry; re != NULL; re = re->next) {
3619 re->func(re->opaque);
3620 }
3621}
3622
3623void qemu_system_reset_request(void)
3624{
bellardd1beab82006-10-02 19:44:22 +00003625 if (no_reboot) {
3626 shutdown_requested = 1;
3627 } else {
3628 reset_requested = 1;
3629 }
bellard6a00d602005-11-21 23:25:50 +00003630 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003631 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003632}
3633
3634void qemu_system_shutdown_request(void)
3635{
3636 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003637 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003638 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003639}
3640
bellard34751872005-07-02 14:31:34 +00003641void qemu_system_powerdown_request(void)
3642{
3643 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003644 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003645 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003646}
3647
ths877cf882007-04-18 18:11:47 +00003648#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003649static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003650{
3651 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003652 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003653
bellardf3311102006-04-12 20:21:17 +00003654
3655 /* XXX: need to suppress polling by better using win32 events */
3656 ret = 0;
3657 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3658 ret |= pe->func(pe->opaque);
3659 }
thse6b1e552007-04-18 17:56:02 +00003660 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003661 int err;
3662 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003663
aliguori56f3a5d2008-10-31 18:07:17 +00003664 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003665 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3666 if (w->func[ret - WAIT_OBJECT_0])
3667 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003668
ths5fafdf22007-09-16 21:08:06 +00003669 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003670 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003671
thse6b1e552007-04-18 17:56:02 +00003672 /* Check if event is signaled */
3673 ret2 = WaitForSingleObject(w->events[i], 0);
3674 if(ret2 == WAIT_OBJECT_0) {
3675 if (w->func[i])
3676 w->func[i](w->opaque[i]);
3677 } else if (ret2 == WAIT_TIMEOUT) {
3678 } else {
3679 err = GetLastError();
3680 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003681 }
3682 }
bellarda18e5242006-06-25 17:18:27 +00003683 } else if (ret == WAIT_TIMEOUT) {
3684 } else {
3685 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003686 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003687 }
bellardf3311102006-04-12 20:21:17 +00003688 }
aliguori56f3a5d2008-10-31 18:07:17 +00003689
3690 *timeout = 0;
3691}
3692#else
blueswir169d64512008-12-07 19:30:18 +00003693static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003694{
3695}
bellardfd1dff42006-02-01 21:29:26 +00003696#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003697
3698void main_loop_wait(int timeout)
3699{
3700 IOHandlerRecord *ioh;
3701 fd_set rfds, wfds, xfds;
3702 int ret, nfds;
3703 struct timeval tv;
3704
3705 qemu_bh_update_timeout(&timeout);
3706
3707 host_main_loop_wait(&timeout);
3708
bellardfd1dff42006-02-01 21:29:26 +00003709 /* poll any events */
3710 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003711 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003712 FD_ZERO(&rfds);
3713 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003714 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003715 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003716 if (ioh->deleted)
3717 continue;
bellardfd1dff42006-02-01 21:29:26 +00003718 if (ioh->fd_read &&
3719 (!ioh->fd_read_poll ||
3720 ioh->fd_read_poll(ioh->opaque) != 0)) {
3721 FD_SET(ioh->fd, &rfds);
3722 if (ioh->fd > nfds)
3723 nfds = ioh->fd;
3724 }
3725 if (ioh->fd_write) {
3726 FD_SET(ioh->fd, &wfds);
3727 if (ioh->fd > nfds)
3728 nfds = ioh->fd;
3729 }
3730 }
ths3b46e622007-09-17 08:09:54 +00003731
aliguori56f3a5d2008-10-31 18:07:17 +00003732 tv.tv_sec = timeout / 1000;
3733 tv.tv_usec = (timeout % 1000) * 1000;
3734
bellarde0356492006-05-01 13:33:02 +00003735#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003736 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003737 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3738 }
3739#endif
3740 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00003741 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003742 IOHandlerRecord **pioh;
3743
3744 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003745 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003746 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003747 }
ths6ab43fd2007-08-25 01:34:19 +00003748 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003749 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003750 }
3751 }
thscafffd42007-02-28 21:59:44 +00003752
3753 /* remove deleted IO handlers */
3754 pioh = &first_io_handler;
3755 while (*pioh) {
3756 ioh = *pioh;
3757 if (ioh->deleted) {
3758 *pioh = ioh->next;
3759 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003760 } else
thscafffd42007-02-28 21:59:44 +00003761 pioh = &ioh->next;
3762 }
bellardfd1dff42006-02-01 21:29:26 +00003763 }
bellarde0356492006-05-01 13:33:02 +00003764#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003765 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003766 if (ret < 0) {
3767 FD_ZERO(&rfds);
3768 FD_ZERO(&wfds);
3769 FD_ZERO(&xfds);
3770 }
3771 slirp_select_poll(&rfds, &wfds, &xfds);
3772 }
3773#endif
bellardc20709a2004-04-21 23:27:19 +00003774
aliguori357c6922008-11-25 17:26:09 +00003775 /* vm time timers */
3776 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3777 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3778 qemu_get_clock(vm_clock));
3779
3780 /* real time timers */
3781 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3782 qemu_get_clock(rt_clock));
3783
pbrook423f0742007-05-23 00:06:54 +00003784 /* Check bottom-halves last in case any of the earlier events triggered
3785 them. */
3786 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003787
bellard5905b2e2004-08-01 21:53:26 +00003788}
3789
pbrook9596ebb2007-11-18 01:44:38 +00003790static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00003791{
3792 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00003793#ifdef CONFIG_PROFILER
3794 int64_t ti;
3795#endif
bellard6a00d602005-11-21 23:25:50 +00003796 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003797
bellard6a00d602005-11-21 23:25:50 +00003798 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00003799 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00003800 for(;;) {
3801 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00003802
bellard15a76442005-11-23 21:01:03 +00003803 for(;;) {
3804 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00003805 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00003806#ifdef CONFIG_PROFILER
3807 ti = profile_getclock();
3808#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003809 if (use_icount) {
3810 int64_t count;
3811 int decr;
3812 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3813 env->icount_decr.u16.low = 0;
3814 env->icount_extra = 0;
3815 count = qemu_next_deadline();
3816 count = (count + (1 << icount_time_shift) - 1)
3817 >> icount_time_shift;
3818 qemu_icount += count;
3819 decr = (count > 0xffff) ? 0xffff : count;
3820 count -= decr;
3821 env->icount_decr.u16.low = decr;
3822 env->icount_extra = count;
3823 }
bellard6a00d602005-11-21 23:25:50 +00003824 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00003825#ifdef CONFIG_PROFILER
3826 qemu_time += profile_getclock() - ti;
3827#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003828 if (use_icount) {
3829 /* Fold pending instructions back into the
3830 instruction counter, and clear the interrupt flag. */
3831 qemu_icount -= (env->icount_decr.u16.low
3832 + env->icount_extra);
3833 env->icount_decr.u32 = 0;
3834 env->icount_extra = 0;
3835 }
balrogee5605e2007-12-03 03:01:40 +00003836 next_cpu = env->next_cpu ?: first_cpu;
aurel3295b01002008-04-04 17:16:35 +00003837 if (event_pending && likely(ret != EXCP_DEBUG)) {
balrogee5605e2007-12-03 03:01:40 +00003838 ret = EXCP_INTERRUPT;
3839 event_pending = 0;
3840 break;
3841 }
pbrookbd967e02007-03-11 18:54:57 +00003842 if (ret == EXCP_HLT) {
3843 /* Give the next CPU a chance to run. */
3844 cur_cpu = env;
3845 continue;
3846 }
bellard15a76442005-11-23 21:01:03 +00003847 if (ret != EXCP_HALTED)
3848 break;
3849 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00003850 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00003851 break;
bellard15a76442005-11-23 21:01:03 +00003852 }
3853 cur_cpu = env;
3854
bellard5905b2e2004-08-01 21:53:26 +00003855 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00003856 ret = EXCP_INTERRUPT;
aurel32b2f76162008-04-11 21:35:52 +00003857 if (no_shutdown) {
3858 vm_stop(0);
3859 no_shutdown = 0;
3860 }
3861 else
3862 break;
bellard5905b2e2004-08-01 21:53:26 +00003863 }
3864 if (reset_requested) {
3865 reset_requested = 0;
3866 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00003867 ret = EXCP_INTERRUPT;
3868 }
3869 if (powerdown_requested) {
3870 powerdown_requested = 0;
3871 qemu_system_powerdown();
3872 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00003873 }
aurel3295b01002008-04-04 17:16:35 +00003874 if (unlikely(ret == EXCP_DEBUG)) {
aliguori880a7572008-11-18 20:30:24 +00003875 gdb_set_stop_cpu(cur_cpu);
bellard5905b2e2004-08-01 21:53:26 +00003876 vm_stop(EXCP_DEBUG);
3877 }
pbrookbd967e02007-03-11 18:54:57 +00003878 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00003879 /* XXX: use timeout computed from timers */
pbrook2e70f6e2008-06-29 01:03:05 +00003880 if (ret == EXCP_HALTED) {
3881 if (use_icount) {
3882 int64_t add;
3883 int64_t delta;
3884 /* Advance virtual time to the next event. */
3885 if (use_icount == 1) {
3886 /* When not using an adaptive execution frequency
3887 we tend to get badly out of sync with real time,
thsbf20dc02008-06-30 17:22:19 +00003888 so just delay for a reasonable amount of time. */
pbrook2e70f6e2008-06-29 01:03:05 +00003889 delta = 0;
3890 } else {
3891 delta = cpu_get_icount() - cpu_get_clock();
3892 }
3893 if (delta > 0) {
3894 /* If virtual time is ahead of real time then just
3895 wait for IO. */
3896 timeout = (delta / 1000000) + 1;
3897 } else {
3898 /* Wait for either IO to occur or the next
3899 timer event. */
3900 add = qemu_next_deadline();
3901 /* We advance the timer before checking for IO.
3902 Limit the amount we advance so that early IO
3903 activity won't get the guest too far ahead. */
3904 if (add > 10000000)
3905 add = 10000000;
3906 delta += add;
3907 add = (add + (1 << icount_time_shift) - 1)
3908 >> icount_time_shift;
3909 qemu_icount += add;
3910 timeout = delta / 1000000;
3911 if (timeout < 0)
3912 timeout = 0;
3913 }
3914 } else {
aliguori0a1af392008-10-31 18:40:25 +00003915 timeout = 5000;
pbrook2e70f6e2008-06-29 01:03:05 +00003916 }
3917 } else {
bellard5905b2e2004-08-01 21:53:26 +00003918 timeout = 0;
pbrook2e70f6e2008-06-29 01:03:05 +00003919 }
bellard5905b2e2004-08-01 21:53:26 +00003920 } else {
blueswir198448f52008-09-30 18:16:09 +00003921 if (shutdown_requested) {
3922 ret = EXCP_INTERRUPT;
aliguori5b08fc12008-08-21 20:08:03 +00003923 break;
blueswir198448f52008-09-30 18:16:09 +00003924 }
aliguori0a1af392008-10-31 18:40:25 +00003925 timeout = 5000;
bellard5905b2e2004-08-01 21:53:26 +00003926 }
bellard89bfc102006-02-08 22:46:31 +00003927#ifdef CONFIG_PROFILER
3928 ti = profile_getclock();
3929#endif
bellard5905b2e2004-08-01 21:53:26 +00003930 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00003931#ifdef CONFIG_PROFILER
3932 dev_time += profile_getclock() - ti;
3933#endif
bellardb4608c02003-06-27 17:34:32 +00003934 }
bellard34865132003-10-05 14:28:56 +00003935 cpu_disable_ticks();
3936 return ret;
bellardb4608c02003-06-27 17:34:32 +00003937}
3938
ths15f82202007-06-29 23:26:08 +00003939static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00003940{
bellard68d0f702008-01-06 17:21:48 +00003941 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00003942 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00003943 "\n"
bellarda20dd502003-09-30 21:07:02 +00003944 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00003945 "\n"
blueswir15824d652009-03-28 06:44:27 +00003946#define DEF(option, opt_arg, opt_enum, opt_help) \
3947 opt_help
3948#define DEFHEADING(text) stringify(text) "\n"
3949#include "qemu-options.h"
3950#undef DEF
3951#undef DEFHEADING
3952#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00003953 "\n"
bellard82c643f2004-07-14 17:28:13 +00003954 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00003955 "ctrl-alt-f toggle full screen\n"
3956 "ctrl-alt-n switch to virtual console 'n'\n"
3957 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00003958 "\n"
3959 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3960 ,
bellard0db63472003-10-27 21:37:46 +00003961 "qemu",
bellarda00bad72004-05-22 21:39:06 +00003962 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00003963#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00003964 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00003965 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00003966#endif
bellard6e44ba72004-01-18 21:56:49 +00003967 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00003968 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00003969 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00003970}
3971
bellardcd6f1162004-05-13 22:02:20 +00003972#define HAS_ARG 0x0001
3973
3974enum {
blueswir15824d652009-03-28 06:44:27 +00003975#define DEF(option, opt_arg, opt_enum, opt_help) \
3976 opt_enum,
3977#define DEFHEADING(text)
3978#include "qemu-options.h"
3979#undef DEF
3980#undef DEFHEADING
3981#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00003982};
3983
3984typedef struct QEMUOption {
3985 const char *name;
3986 int flags;
3987 int index;
3988} QEMUOption;
3989
blueswir1dbed7e42008-10-01 19:38:09 +00003990static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00003991 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00003992#define DEF(option, opt_arg, opt_enum, opt_help) \
3993 { option, opt_arg, opt_enum },
3994#define DEFHEADING(text)
3995#include "qemu-options.h"
3996#undef DEF
3997#undef DEFHEADING
3998#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00003999 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004000};
4001
bellard1d14ffa2005-10-30 18:58:22 +00004002#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004003struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004004#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004005#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004006 {
4007 "pcspk",
4008 "PC speaker",
4009 0,
4010 1,
4011 { .init_isa = pcspk_audio_init }
4012 },
4013#endif
malc4c9b53e2009-01-09 10:46:34 +00004014
4015#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004016 {
4017 "sb16",
4018 "Creative Sound Blaster 16",
4019 0,
4020 1,
4021 { .init_isa = SB16_init }
4022 },
malc4c9b53e2009-01-09 10:46:34 +00004023#endif
bellard6a36d842005-12-18 20:34:32 +00004024
malccc53d262008-06-13 10:48:22 +00004025#ifdef CONFIG_CS4231A
4026 {
4027 "cs4231a",
4028 "CS4231A",
4029 0,
4030 1,
4031 { .init_isa = cs4231a_init }
4032 },
4033#endif
4034
bellard6a36d842005-12-18 20:34:32 +00004035#ifdef CONFIG_ADLIB
4036 {
4037 "adlib",
4038#ifdef HAS_YMF262
4039 "Yamaha YMF262 (OPL3)",
4040#else
4041 "Yamaha YM3812 (OPL2)",
4042#endif
4043 0,
4044 1,
4045 { .init_isa = Adlib_init }
4046 },
4047#endif
4048
4049#ifdef CONFIG_GUS
4050 {
4051 "gus",
4052 "Gravis Ultrasound GF1",
4053 0,
4054 1,
4055 { .init_isa = GUS_init }
4056 },
4057#endif
4058
malc4c9b53e2009-01-09 10:46:34 +00004059#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004060 {
4061 "ac97",
4062 "Intel 82801AA AC97 Audio",
4063 0,
4064 0,
4065 { .init_pci = ac97_init }
4066 },
malc4c9b53e2009-01-09 10:46:34 +00004067#endif
balroge5c9a132008-01-14 04:27:55 +00004068
malc4c9b53e2009-01-09 10:46:34 +00004069#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004070 {
4071 "es1370",
4072 "ENSONIQ AudioPCI ES1370",
4073 0,
4074 0,
4075 { .init_pci = es1370_init }
4076 },
balrogb00052e2007-04-30 02:22:06 +00004077#endif
bellard6a36d842005-12-18 20:34:32 +00004078
malc4c9b53e2009-01-09 10:46:34 +00004079#endif /* HAS_AUDIO_CHOICE */
4080
bellard6a36d842005-12-18 20:34:32 +00004081 { NULL, NULL, 0, 0, { NULL } }
4082};
4083
bellard1d14ffa2005-10-30 18:58:22 +00004084static void select_soundhw (const char *optarg)
4085{
bellard6a36d842005-12-18 20:34:32 +00004086 struct soundhw *c;
4087
bellard1d14ffa2005-10-30 18:58:22 +00004088 if (*optarg == '?') {
4089 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004090
bellard1d14ffa2005-10-30 18:58:22 +00004091 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004092 for (c = soundhw; c->name; ++c) {
4093 printf ("%-11s %s\n", c->name, c->descr);
4094 }
4095 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004096 exit (*optarg != '?');
4097 }
4098 else {
bellard6a36d842005-12-18 20:34:32 +00004099 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004100 const char *p;
4101 char *e;
4102 int bad_card = 0;
4103
bellard6a36d842005-12-18 20:34:32 +00004104 if (!strcmp (optarg, "all")) {
4105 for (c = soundhw; c->name; ++c) {
4106 c->enabled = 1;
4107 }
4108 return;
4109 }
bellard1d14ffa2005-10-30 18:58:22 +00004110
bellard6a36d842005-12-18 20:34:32 +00004111 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004112 while (*p) {
4113 e = strchr (p, ',');
4114 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004115
4116 for (c = soundhw; c->name; ++c) {
4117 if (!strncmp (c->name, p, l)) {
4118 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004119 break;
4120 }
4121 }
bellard6a36d842005-12-18 20:34:32 +00004122
4123 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004124 if (l > 80) {
4125 fprintf (stderr,
4126 "Unknown sound card name (too big to show)\n");
4127 }
4128 else {
4129 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4130 (int) l, p);
4131 }
4132 bad_card = 1;
4133 }
4134 p += l + (e != NULL);
4135 }
4136
4137 if (bad_card)
4138 goto show_valid_cards;
4139 }
4140}
4141#endif
4142
malc3893c122008-09-28 00:42:05 +00004143static void select_vgahw (const char *p)
4144{
4145 const char *opts;
4146
4147 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004148 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004149 cirrus_vga_enabled = 0;
4150 vmsvga_enabled = 0;
4151 } else if (strstart(p, "cirrus", &opts)) {
4152 cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004153 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004154 vmsvga_enabled = 0;
4155 } else if (strstart(p, "vmware", &opts)) {
4156 cirrus_vga_enabled = 0;
aliguoric2b3b412009-01-15 20:37:28 +00004157 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004158 vmsvga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004159 } else if (strstart(p, "none", &opts)) {
4160 cirrus_vga_enabled = 0;
4161 std_vga_enabled = 0;
4162 vmsvga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004163 } else {
4164 invalid_vga:
4165 fprintf(stderr, "Unknown vga type: %s\n", p);
4166 exit(1);
4167 }
malccb5a7aa2008-09-28 00:42:12 +00004168 while (*opts) {
4169 const char *nextopt;
4170
4171 if (strstart(opts, ",retrace=", &nextopt)) {
4172 opts = nextopt;
4173 if (strstart(opts, "dumb", &nextopt))
4174 vga_retrace_method = VGA_RETRACE_DUMB;
4175 else if (strstart(opts, "precise", &nextopt))
4176 vga_retrace_method = VGA_RETRACE_PRECISE;
4177 else goto invalid_vga;
4178 } else goto invalid_vga;
4179 opts = nextopt;
4180 }
malc3893c122008-09-28 00:42:05 +00004181}
4182
bellard3587d7e2006-06-26 20:03:44 +00004183#ifdef _WIN32
4184static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4185{
4186 exit(STATUS_CONTROL_C_EXIT);
4187 return TRUE;
4188}
4189#endif
4190
blueswir18fcb1b92008-09-18 18:29:08 +00004191static int qemu_uuid_parse(const char *str, uint8_t *uuid)
4192{
4193 int ret;
4194
4195 if(strlen(str) != 36)
4196 return -1;
4197
4198 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4199 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4200 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4201
4202 if(ret != 16)
4203 return -1;
4204
4205 return 0;
4206}
4207
bellard7c9d8e02005-11-15 22:16:05 +00004208#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004209
aliguori5b08fc12008-08-21 20:08:03 +00004210#ifndef _WIN32
4211
4212static void termsig_handler(int signal)
4213{
4214 qemu_system_shutdown_request();
4215}
4216
blueswir16f9e3802008-09-09 18:56:59 +00004217static void termsig_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004218{
4219 struct sigaction act;
4220
4221 memset(&act, 0, sizeof(act));
4222 act.sa_handler = termsig_handler;
4223 sigaction(SIGINT, &act, NULL);
4224 sigaction(SIGHUP, &act, NULL);
4225 sigaction(SIGTERM, &act, NULL);
4226}
4227
4228#endif
4229
malc902b3d52008-12-10 19:18:40 +00004230int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004231{
bellard67b915a2004-03-31 23:37:16 +00004232#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00004233 int use_gdbstub;
4234 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00004235#endif
j_mayer28c5af52007-11-11 01:50:45 +00004236 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004237 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004238 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004239 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004240 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00004241 const char *boot_devices = "";
aliguori3023f3322009-01-16 19:04:14 +00004242 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004243 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004244 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004245 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004246 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004247 const char *bt_opts[MAX_BT_CMDLINE];
4248 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004249 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004250 int optind;
4251 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004252 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004253 const char *monitor_device;
4254 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004255 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004256 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004257 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004258 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4259 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004260 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004261 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004262 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004263 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004264 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00004265 int fds[2];
bellard26a5f132008-05-28 12:30:31 +00004266 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004267 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004268 const char *incoming = NULL;
aliguori54042bc2009-02-27 22:16:47 +00004269 int fd = 0;
4270 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004271 const char *chroot_dir = NULL;
4272 const char *run_as = NULL;
bellard0bd48852005-11-11 00:00:47 +00004273
malc902b3d52008-12-10 19:18:40 +00004274 qemu_cache_utils_init(envp);
4275
bellard0bd48852005-11-11 00:00:47 +00004276 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004277#ifndef _WIN32
4278 {
4279 struct sigaction act;
4280 sigfillset(&act.sa_mask);
4281 act.sa_flags = 0;
4282 act.sa_handler = SIG_IGN;
4283 sigaction(SIGPIPE, &act, NULL);
4284 }
bellard3587d7e2006-06-26 20:03:44 +00004285#else
4286 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004287 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4288 QEMU to run on a single CPU */
4289 {
4290 HANDLE h;
4291 DWORD mask, smask;
4292 int i;
4293 h = GetCurrentProcess();
4294 if (GetProcessAffinityMask(h, &mask, &smask)) {
4295 for(i = 0; i < 32; i++) {
4296 if (mask & (1 << i))
4297 break;
4298 }
4299 if (i != 32) {
4300 mask = 1 << i;
4301 SetProcessAffinityMask(h, mask);
4302 }
4303 }
4304 }
bellard67b915a2004-03-31 23:37:16 +00004305#endif
bellardbe995c22006-06-25 16:25:21 +00004306
bellardcc1daa42005-06-05 14:49:17 +00004307 register_machines();
4308 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004309 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004310 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004311 ram_size = 0;
bellard313aa562003-08-10 21:52:11 +00004312 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00004313#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00004314 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00004315 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00004316#endif
bellard33e39632003-07-06 17:15:21 +00004317 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004318 nographic = 0;
balrog4d3b6f62008-02-10 16:33:14 +00004319 curses = 0;
bellarda20dd502003-09-30 21:07:02 +00004320 kernel_filename = NULL;
4321 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004322 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004323 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00004324 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00004325
aurel32c75a8232008-05-04 00:50:34 +00004326 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004327 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004328 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004329 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004330
aliguori8290edd2009-02-27 20:14:29 +00004331 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004332 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004333 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004334 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004335
aliguori1b8fc812009-02-27 20:01:39 +00004336 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004337 virtio_consoles[i] = NULL;
4338 virtio_console_index = 0;
4339
bellarda594cfb2005-11-06 16:13:29 +00004340 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004341
bellard7c9d8e02005-11-15 22:16:05 +00004342 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004343 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004344 nb_drives = 0;
4345 nb_drives_opt = 0;
4346 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004347
4348 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004349
bellard26a5f132008-05-28 12:30:31 +00004350 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004351 autostart= 1;
4352
bellardcd6f1162004-05-13 22:02:20 +00004353 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004354 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004355 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004356 break;
bellardcd6f1162004-05-13 22:02:20 +00004357 r = argv[optind];
4358 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004359 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004360 } else {
4361 const QEMUOption *popt;
4362
4363 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004364 /* Treat --foo the same as -foo. */
4365 if (r[1] == '-')
4366 r++;
bellardcd6f1162004-05-13 22:02:20 +00004367 popt = qemu_options;
4368 for(;;) {
4369 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004370 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004371 argv[0], r);
4372 exit(1);
4373 }
4374 if (!strcmp(popt->name, r + 1))
4375 break;
4376 popt++;
4377 }
4378 if (popt->flags & HAS_ARG) {
4379 if (optind >= argc) {
4380 fprintf(stderr, "%s: option '%s' requires an argument\n",
4381 argv[0], r);
4382 exit(1);
4383 }
4384 optarg = argv[optind++];
4385 } else {
4386 optarg = NULL;
4387 }
4388
4389 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004390 case QEMU_OPTION_M:
4391 machine = find_machine(optarg);
4392 if (!machine) {
4393 QEMUMachine *m;
4394 printf("Supported machines are:\n");
4395 for(m = first_machine; m != NULL; m = m->next) {
4396 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004397 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00004398 m == first_machine ? " (default)" : "");
4399 }
ths15f82202007-06-29 23:26:08 +00004400 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004401 }
4402 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004403 case QEMU_OPTION_cpu:
4404 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004405 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004406/* XXX: implement xxx_cpu_list for targets that still miss it */
4407#if defined(cpu_list)
4408 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004409#endif
ths15f82202007-06-29 23:26:08 +00004410 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004411 } else {
4412 cpu_model = optarg;
4413 }
4414 break;
bellardcd6f1162004-05-13 22:02:20 +00004415 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004416 initrd_filename = optarg;
4417 break;
bellardcd6f1162004-05-13 22:02:20 +00004418 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004419 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00004420 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004421 else
balrog609497a2008-01-14 02:56:53 +00004422 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004423 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004424 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004425 translation == BIOS_ATA_TRANSLATION_LBA ?
4426 ",trans=lba" :
4427 translation == BIOS_ATA_TRANSLATION_NONE ?
4428 ",trans=none" : "");
4429 break;
bellardcd6f1162004-05-13 22:02:20 +00004430 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004431 case QEMU_OPTION_hdc:
4432 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004433 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004434 break;
thse4bcb142007-12-02 04:51:10 +00004435 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004436 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004437 break;
balrog3e3d5812007-04-30 02:09:25 +00004438 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004439 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004440 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004441 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004442 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004443 break;
j_mayer86f55662007-04-24 06:52:59 +00004444 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004445 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004446 break;
bellardcd6f1162004-05-13 22:02:20 +00004447 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004448 snapshot = 1;
4449 break;
bellardcd6f1162004-05-13 22:02:20 +00004450 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004451 {
bellard330d0412003-07-26 18:11:40 +00004452 const char *p;
4453 p = optarg;
4454 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004455 if (cyls < 1 || cyls > 16383)
4456 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004457 if (*p != ',')
4458 goto chs_fail;
4459 p++;
4460 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004461 if (heads < 1 || heads > 16)
4462 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004463 if (*p != ',')
4464 goto chs_fail;
4465 p++;
4466 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004467 if (secs < 1 || secs > 63)
4468 goto chs_fail;
4469 if (*p == ',') {
4470 p++;
4471 if (!strcmp(p, "none"))
4472 translation = BIOS_ATA_TRANSLATION_NONE;
4473 else if (!strcmp(p, "lba"))
4474 translation = BIOS_ATA_TRANSLATION_LBA;
4475 else if (!strcmp(p, "auto"))
4476 translation = BIOS_ATA_TRANSLATION_AUTO;
4477 else
4478 goto chs_fail;
4479 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004480 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004481 fprintf(stderr, "qemu: invalid physical CHS format\n");
4482 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004483 }
thse4bcb142007-12-02 04:51:10 +00004484 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00004485 snprintf(drives_opt[hda_index].opt,
4486 sizeof(drives_opt[hda_index].opt),
4487 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4488 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004489 translation == BIOS_ATA_TRANSLATION_LBA ?
4490 ",trans=lba" :
4491 translation == BIOS_ATA_TRANSLATION_NONE ?
4492 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00004493 }
4494 break;
bellardcd6f1162004-05-13 22:02:20 +00004495 case QEMU_OPTION_nographic:
bellarda20dd502003-09-30 21:07:02 +00004496 nographic = 1;
4497 break;
balrog4d3b6f62008-02-10 16:33:14 +00004498#ifdef CONFIG_CURSES
4499 case QEMU_OPTION_curses:
4500 curses = 1;
4501 break;
4502#endif
balroga171fe32007-04-30 01:48:07 +00004503 case QEMU_OPTION_portrait:
4504 graphic_rotate = 1;
4505 break;
bellardcd6f1162004-05-13 22:02:20 +00004506 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004507 kernel_filename = optarg;
4508 break;
bellardcd6f1162004-05-13 22:02:20 +00004509 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004510 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004511 break;
bellardcd6f1162004-05-13 22:02:20 +00004512 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004513 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004514 break;
bellardcd6f1162004-05-13 22:02:20 +00004515 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004516 boot_devices = optarg;
4517 /* We just do some generic consistency checks */
4518 {
4519 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00004520 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00004521
4522 boot_devices_bitmap = 0;
4523 for (p = boot_devices; *p != '\0'; p++) {
4524 /* Allowed boot devices are:
4525 * a b : floppy disk drives
4526 * c ... f : IDE disk drives
4527 * g ... m : machine implementation dependant drives
4528 * n ... p : network devices
4529 * It's up to each machine implementation to check
4530 * if the given boot devices match the actual hardware
4531 * implementation and firmware features.
4532 */
4533 if (*p < 'a' || *p > 'q') {
4534 fprintf(stderr, "Invalid boot device '%c'\n", *p);
4535 exit(1);
4536 }
4537 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4538 fprintf(stderr,
4539 "Boot device '%c' was given twice\n",*p);
4540 exit(1);
4541 }
4542 boot_devices_bitmap |= 1 << (*p - 'a');
4543 }
bellard36b486b2003-11-11 13:36:08 +00004544 }
4545 break;
bellardcd6f1162004-05-13 22:02:20 +00004546 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004547 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004548 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004549 break;
bellard52ca8d62006-06-14 16:03:05 +00004550#ifdef TARGET_I386
4551 case QEMU_OPTION_no_fd_bootchk:
4552 fd_bootchk = 0;
4553 break;
4554#endif
bellard7c9d8e02005-11-15 22:16:05 +00004555 case QEMU_OPTION_net:
4556 if (nb_net_clients >= MAX_NET_CLIENTS) {
4557 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00004558 exit(1);
4559 }
pbrookfd5f3932008-03-26 20:55:43 +00004560 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00004561 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00004562 break;
bellardc7f74642004-08-24 21:57:12 +00004563#ifdef CONFIG_SLIRP
4564 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00004565 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004566 break;
ths47d5d012007-02-20 00:05:08 +00004567 case QEMU_OPTION_bootp:
4568 bootp_filename = optarg;
4569 break;
bellardc94c8d62004-09-13 21:37:34 +00004570#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004571 case QEMU_OPTION_smb:
4572 net_slirp_smb(optarg);
4573 break;
bellardc94c8d62004-09-13 21:37:34 +00004574#endif
bellard9bf05442004-08-25 22:12:49 +00004575 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00004576 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00004577 break;
bellardc7f74642004-08-24 21:57:12 +00004578#endif
balrogdc72ac12008-11-09 00:04:26 +00004579 case QEMU_OPTION_bt:
4580 if (nb_bt_opts >= MAX_BT_CMDLINE) {
4581 fprintf(stderr, "qemu: too many bluetooth options\n");
4582 exit(1);
4583 }
4584 bt_opts[nb_bt_opts++] = optarg;
4585 break;
bellard1d14ffa2005-10-30 18:58:22 +00004586#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004587 case QEMU_OPTION_audio_help:
4588 AUD_help ();
4589 exit (0);
4590 break;
4591 case QEMU_OPTION_soundhw:
4592 select_soundhw (optarg);
4593 break;
4594#endif
bellardcd6f1162004-05-13 22:02:20 +00004595 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00004596 help(0);
bellardcd6f1162004-05-13 22:02:20 +00004597 break;
aurel3200f82b82008-04-27 21:12:55 +00004598 case QEMU_OPTION_m: {
4599 uint64_t value;
4600 char *ptr;
4601
4602 value = strtoul(optarg, &ptr, 10);
4603 switch (*ptr) {
4604 case 0: case 'M': case 'm':
4605 value <<= 20;
4606 break;
4607 case 'G': case 'g':
4608 value <<= 30;
4609 break;
4610 default:
4611 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00004612 exit(1);
4613 }
aurel3200f82b82008-04-27 21:12:55 +00004614
4615 /* On 32-bit hosts, QEMU is limited by virtual address space */
4616 if (value > (2047 << 20)
4617#ifndef USE_KQEMU
4618 && HOST_LONG_BITS == 32
4619#endif
4620 ) {
4621 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4622 exit(1);
4623 }
4624 if (value != (uint64_t)(ram_addr_t)value) {
4625 fprintf(stderr, "qemu: ram size too large\n");
4626 exit(1);
4627 }
4628 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00004629 break;
aurel3200f82b82008-04-27 21:12:55 +00004630 }
bellardcd6f1162004-05-13 22:02:20 +00004631 case QEMU_OPTION_d:
4632 {
4633 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00004634 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00004635
bellardcd6f1162004-05-13 22:02:20 +00004636 mask = cpu_str_to_log_mask(optarg);
4637 if (!mask) {
4638 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00004639 for(item = cpu_log_items; item->mask != 0; item++) {
4640 printf("%-10s %s\n", item->name, item->help);
4641 }
4642 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00004643 }
4644 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00004645 }
bellardcd6f1162004-05-13 22:02:20 +00004646 break;
bellard67b915a2004-03-31 23:37:16 +00004647#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00004648 case QEMU_OPTION_s:
4649 use_gdbstub = 1;
4650 break;
4651 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00004652 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00004653 break;
bellard67b915a2004-03-31 23:37:16 +00004654#endif
bellardcd6f1162004-05-13 22:02:20 +00004655 case QEMU_OPTION_L:
4656 bios_dir = optarg;
4657 break;
j_mayer1192dad2007-10-05 13:08:35 +00004658 case QEMU_OPTION_bios:
4659 bios_name = optarg;
4660 break;
bellardcd6f1162004-05-13 22:02:20 +00004661 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00004662 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00004663 break;
blueswir15824d652009-03-28 06:44:27 +00004664#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00004665 case QEMU_OPTION_k:
4666 keyboard_layout = optarg;
4667 break;
blueswir15824d652009-03-28 06:44:27 +00004668#endif
bellardee22c2f2004-06-03 12:49:50 +00004669 case QEMU_OPTION_localtime:
4670 rtc_utc = 0;
4671 break;
malc3893c122008-09-28 00:42:05 +00004672 case QEMU_OPTION_vga:
4673 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00004674 break;
blueswir15824d652009-03-28 06:44:27 +00004675#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00004676 case QEMU_OPTION_g:
4677 {
4678 const char *p;
4679 int w, h, depth;
4680 p = optarg;
4681 w = strtol(p, (char **)&p, 10);
4682 if (w <= 0) {
4683 graphic_error:
4684 fprintf(stderr, "qemu: invalid resolution or depth\n");
4685 exit(1);
4686 }
4687 if (*p != 'x')
4688 goto graphic_error;
4689 p++;
4690 h = strtol(p, (char **)&p, 10);
4691 if (h <= 0)
4692 goto graphic_error;
4693 if (*p == 'x') {
4694 p++;
4695 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00004696 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00004697 depth != 24 && depth != 32)
4698 goto graphic_error;
4699 } else if (*p == '\0') {
4700 depth = graphic_depth;
4701 } else {
4702 goto graphic_error;
4703 }
ths3b46e622007-09-17 08:09:54 +00004704
bellarde9b137c2004-06-21 16:46:10 +00004705 graphic_width = w;
4706 graphic_height = h;
4707 graphic_depth = depth;
4708 }
4709 break;
blueswir15824d652009-03-28 06:44:27 +00004710#endif
ths20d8a3e2007-02-18 17:04:49 +00004711 case QEMU_OPTION_echr:
4712 {
4713 char *r;
4714 term_escape_char = strtol(optarg, &r, 0);
4715 if (r == optarg)
4716 printf("Bad argument to echr\n");
4717 break;
4718 }
bellard82c643f2004-07-14 17:28:13 +00004719 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00004720 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00004721 break;
4722 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00004723 if (serial_device_index >= MAX_SERIAL_PORTS) {
4724 fprintf(stderr, "qemu: too many serial ports\n");
4725 exit(1);
4726 }
pbrookfd5f3932008-03-26 20:55:43 +00004727 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00004728 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00004729 break;
aliguori51ecf132009-01-15 20:06:40 +00004730 case QEMU_OPTION_virtiocon:
4731 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
4732 fprintf(stderr, "qemu: too many virtio consoles\n");
4733 exit(1);
4734 }
4735 virtio_consoles[virtio_console_index] = optarg;
4736 virtio_console_index++;
4737 break;
bellard6508fe52005-01-15 12:02:56 +00004738 case QEMU_OPTION_parallel:
4739 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
4740 fprintf(stderr, "qemu: too many parallel ports\n");
4741 exit(1);
4742 }
pbrookfd5f3932008-03-26 20:55:43 +00004743 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00004744 parallel_device_index++;
4745 break;
bellardd63d3072004-10-03 13:29:03 +00004746 case QEMU_OPTION_loadvm:
4747 loadvm = optarg;
4748 break;
4749 case QEMU_OPTION_full_screen:
4750 full_screen = 1;
4751 break;
ths667acca2006-12-11 02:08:05 +00004752#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00004753 case QEMU_OPTION_no_frame:
4754 no_frame = 1;
4755 break;
ths3780e192007-06-21 21:08:02 +00004756 case QEMU_OPTION_alt_grab:
4757 alt_grab = 1;
4758 break;
ths667acca2006-12-11 02:08:05 +00004759 case QEMU_OPTION_no_quit:
4760 no_quit = 1;
4761 break;
aliguori7d957bd2009-01-15 22:14:11 +00004762 case QEMU_OPTION_sdl:
4763 sdl = 1;
4764 break;
ths667acca2006-12-11 02:08:05 +00004765#endif
bellardf7cce892004-12-08 22:21:25 +00004766 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00004767 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00004768 break;
bellarda09db212005-04-30 16:10:35 +00004769#ifdef TARGET_I386
4770 case QEMU_OPTION_win2k_hack:
4771 win2k_install_hack = 1;
4772 break;
aliguori73822ec2009-01-15 20:11:34 +00004773 case QEMU_OPTION_rtc_td_hack:
4774 rtc_td_hack = 1;
4775 break;
aliguori8a92ea22009-02-27 20:12:36 +00004776 case QEMU_OPTION_acpitable:
4777 if(acpi_table_add(optarg) < 0) {
4778 fprintf(stderr, "Wrong acpi table provided\n");
4779 exit(1);
4780 }
4781 break;
bellarda09db212005-04-30 16:10:35 +00004782#endif
bellardd993e022005-02-10 22:00:06 +00004783#ifdef USE_KQEMU
4784 case QEMU_OPTION_no_kqemu:
4785 kqemu_allowed = 0;
4786 break;
bellard89bfc102006-02-08 22:46:31 +00004787 case QEMU_OPTION_kernel_kqemu:
4788 kqemu_allowed = 2;
4789 break;
bellardd993e022005-02-10 22:00:06 +00004790#endif
aliguori7ba1e612008-11-05 16:04:33 +00004791#ifdef CONFIG_KVM
4792 case QEMU_OPTION_enable_kvm:
4793 kvm_allowed = 1;
4794#ifdef USE_KQEMU
4795 kqemu_allowed = 0;
4796#endif
4797 break;
4798#endif
bellardbb36d472005-11-05 14:22:28 +00004799 case QEMU_OPTION_usb:
4800 usb_enabled = 1;
4801 break;
bellarda594cfb2005-11-06 16:13:29 +00004802 case QEMU_OPTION_usbdevice:
4803 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00004804 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00004805 fprintf(stderr, "Too many USB devices\n");
4806 exit(1);
4807 }
pbrookfd5f3932008-03-26 20:55:43 +00004808 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00004809 usb_devices_index++;
4810 break;
bellard6a00d602005-11-21 23:25:50 +00004811 case QEMU_OPTION_smp:
4812 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00004813 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00004814 fprintf(stderr, "Invalid number of CPUs\n");
4815 exit(1);
4816 }
4817 break;
bellard24236862006-04-30 21:28:36 +00004818 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00004819 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00004820 break;
blueswir15824d652009-03-28 06:44:27 +00004821#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00004822 case QEMU_OPTION_no_acpi:
4823 acpi_enabled = 0;
4824 break;
aliguori16b29ae2008-12-17 23:28:44 +00004825 case QEMU_OPTION_no_hpet:
4826 no_hpet = 1;
4827 break;
blueswir15824d652009-03-28 06:44:27 +00004828#endif
bellardd1beab82006-10-02 19:44:22 +00004829 case QEMU_OPTION_no_reboot:
4830 no_reboot = 1;
4831 break;
aurel32b2f76162008-04-11 21:35:52 +00004832 case QEMU_OPTION_no_shutdown:
4833 no_shutdown = 1;
4834 break;
balrog9467cd42007-05-01 01:34:14 +00004835 case QEMU_OPTION_show_cursor:
4836 cursor_hide = 0;
4837 break;
blueswir18fcb1b92008-09-18 18:29:08 +00004838 case QEMU_OPTION_uuid:
4839 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
4840 fprintf(stderr, "Fail to parse UUID string."
4841 " Wrong format.\n");
4842 exit(1);
4843 }
4844 break;
blueswir15824d652009-03-28 06:44:27 +00004845#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004846 case QEMU_OPTION_daemonize:
4847 daemonize = 1;
4848 break;
blueswir15824d652009-03-28 06:44:27 +00004849#endif
ths9ae02552007-01-05 17:39:04 +00004850 case QEMU_OPTION_option_rom:
4851 if (nb_option_roms >= MAX_OPTION_ROMS) {
4852 fprintf(stderr, "Too many option ROMs\n");
4853 exit(1);
4854 }
4855 option_rom[nb_option_roms] = optarg;
4856 nb_option_roms++;
4857 break;
blueswir15824d652009-03-28 06:44:27 +00004858#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00004859 case QEMU_OPTION_semihosting:
4860 semihosting_enabled = 1;
4861 break;
blueswir15824d652009-03-28 06:44:27 +00004862#endif
thsc35734b2007-03-19 15:17:08 +00004863 case QEMU_OPTION_name:
4864 qemu_name = optarg;
4865 break;
blueswir195efd112008-12-24 20:26:14 +00004866#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00004867 case QEMU_OPTION_prom_env:
4868 if (nb_prom_envs >= MAX_PROM_ENVS) {
4869 fprintf(stderr, "Too many prom variables\n");
4870 exit(1);
4871 }
4872 prom_envs[nb_prom_envs] = optarg;
4873 nb_prom_envs++;
4874 break;
4875#endif
balrog2b8f2d42007-07-27 22:08:46 +00004876#ifdef TARGET_ARM
4877 case QEMU_OPTION_old_param:
4878 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00004879 break;
balrog2b8f2d42007-07-27 22:08:46 +00004880#endif
thsf3dcfad2007-08-24 01:26:02 +00004881 case QEMU_OPTION_clock:
4882 configure_alarms(optarg);
4883 break;
bellard7e0af5d02007-11-07 16:24:33 +00004884 case QEMU_OPTION_startdate:
4885 {
4886 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00004887 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00004888 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00004889 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00004890 } else {
4891 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4892 &tm.tm_year,
4893 &tm.tm_mon,
4894 &tm.tm_mday,
4895 &tm.tm_hour,
4896 &tm.tm_min,
4897 &tm.tm_sec) == 6) {
4898 /* OK */
4899 } else if (sscanf(optarg, "%d-%d-%d",
4900 &tm.tm_year,
4901 &tm.tm_mon,
4902 &tm.tm_mday) == 3) {
4903 tm.tm_hour = 0;
4904 tm.tm_min = 0;
4905 tm.tm_sec = 0;
4906 } else {
4907 goto date_fail;
4908 }
4909 tm.tm_year -= 1900;
4910 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00004911 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00004912 if (rtc_start_date == -1) {
4913 date_fail:
4914 fprintf(stderr, "Invalid date format. Valid format are:\n"
4915 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
4916 exit(1);
4917 }
balrogf6503052008-02-17 11:42:19 +00004918 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00004919 }
4920 }
4921 break;
bellard26a5f132008-05-28 12:30:31 +00004922 case QEMU_OPTION_tb_size:
4923 tb_size = strtol(optarg, NULL, 0);
4924 if (tb_size < 0)
4925 tb_size = 0;
4926 break;
pbrook2e70f6e2008-06-29 01:03:05 +00004927 case QEMU_OPTION_icount:
4928 use_icount = 1;
4929 if (strcmp(optarg, "auto") == 0) {
4930 icount_time_shift = -1;
4931 } else {
4932 icount_time_shift = strtol(optarg, NULL, 0);
4933 }
4934 break;
aliguori5bb79102008-10-13 03:12:02 +00004935 case QEMU_OPTION_incoming:
4936 incoming = optarg;
4937 break;
blueswir15824d652009-03-28 06:44:27 +00004938#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00004939 case QEMU_OPTION_chroot:
4940 chroot_dir = optarg;
4941 break;
4942 case QEMU_OPTION_runas:
4943 run_as = optarg;
4944 break;
blueswir15824d652009-03-28 06:44:27 +00004945#endif
bellardcd6f1162004-05-13 22:02:20 +00004946 }
bellard0824d6f2003-06-24 13:42:40 +00004947 }
4948 }
bellard330d0412003-07-26 18:11:40 +00004949
aliguori7ba1e612008-11-05 16:04:33 +00004950#if defined(CONFIG_KVM) && defined(USE_KQEMU)
4951 if (kvm_allowed && kqemu_allowed) {
4952 fprintf(stderr,
4953 "You can not enable both KVM and kqemu at the same time\n");
4954 exit(1);
4955 }
4956#endif
4957
balrog3d878ca2008-10-28 10:59:59 +00004958 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00004959 if (smp_cpus > machine->max_cpus) {
4960 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
4961 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
4962 machine->max_cpus);
4963 exit(1);
4964 }
4965
aliguoribc0129d2008-08-01 15:12:34 +00004966 if (nographic) {
4967 if (serial_device_index == 0)
4968 serial_devices[0] = "stdio";
4969 if (parallel_device_index == 0)
4970 parallel_devices[0] = "null";
4971 if (strncmp(monitor_device, "vc", 2) == 0)
4972 monitor_device = "stdio";
4973 }
4974
ths71e3ceb2006-12-22 02:11:31 +00004975#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004976 if (daemonize) {
4977 pid_t pid;
4978
4979 if (pipe(fds) == -1)
4980 exit(1);
4981
4982 pid = fork();
4983 if (pid > 0) {
4984 uint8_t status;
4985 ssize_t len;
4986
4987 close(fds[1]);
4988
4989 again:
ths93815bc2007-03-19 15:58:31 +00004990 len = read(fds[0], &status, 1);
4991 if (len == -1 && (errno == EINTR))
4992 goto again;
4993
4994 if (len != 1)
4995 exit(1);
4996 else if (status == 1) {
4997 fprintf(stderr, "Could not acquire pidfile\n");
4998 exit(1);
4999 } else
5000 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005001 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005002 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005003
5004 setsid();
5005
5006 pid = fork();
5007 if (pid > 0)
5008 exit(0);
5009 else if (pid < 0)
5010 exit(1);
5011
5012 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005013
5014 signal(SIGTSTP, SIG_IGN);
5015 signal(SIGTTOU, SIG_IGN);
5016 signal(SIGTTIN, SIG_IGN);
5017 }
5018#endif
5019
thsaa26bb22007-03-25 21:33:06 +00005020 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005021 if (daemonize) {
5022 uint8_t status = 1;
5023 write(fds[1], &status, 1);
5024 } else
5025 fprintf(stderr, "Could not acquire pid file\n");
5026 exit(1);
5027 }
5028
bellardff3fbb32006-01-08 10:53:14 +00005029#ifdef USE_KQEMU
5030 if (smp_cpus > 1)
5031 kqemu_allowed = 0;
5032#endif
bellarda20dd502003-09-30 21:07:02 +00005033 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00005034 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00005035
j_mayer28c5af52007-11-11 01:50:45 +00005036 if (!linux_boot && net_boot == 0 &&
blueswir1f88e4b92008-08-12 15:58:35 +00005037 !machine->nodisk_ok && nb_drives_opt == 0)
ths15f82202007-06-29 23:26:08 +00005038 help(1);
bellard0824d6f2003-06-24 13:42:40 +00005039
thsf8d39c02008-07-03 10:01:15 +00005040 if (!linux_boot && *kernel_cmdline != '\0') {
5041 fprintf(stderr, "-append only allowed with -kernel option\n");
5042 exit(1);
5043 }
5044
5045 if (!linux_boot && initrd_filename != NULL) {
5046 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5047 exit(1);
5048 }
5049
ths96d30e42007-01-07 20:42:14 +00005050 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00005051 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00005052 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00005053 }
bellardb118d612003-06-30 23:36:21 +00005054 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005055
pbrook634fce92006-07-15 17:40:09 +00005056 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005057 if (init_timer_alarm() < 0) {
5058 fprintf(stderr, "could not initialize alarm timer\n");
5059 exit(1);
5060 }
pbrook2e70f6e2008-06-29 01:03:05 +00005061 if (use_icount && icount_time_shift < 0) {
5062 use_icount = 2;
5063 /* 125MIPS seems a reasonable initial guess at the guest speed.
5064 It will be corrected fairly quickly anyway. */
5065 icount_time_shift = 3;
5066 init_icount_adjust();
5067 }
pbrook634fce92006-07-15 17:40:09 +00005068
bellardfd1dff42006-02-01 21:29:26 +00005069#ifdef _WIN32
5070 socket_init();
5071#endif
5072
bellard7c9d8e02005-11-15 22:16:05 +00005073 /* init network clients */
5074 if (nb_net_clients == 0) {
5075 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005076 net_clients[nb_net_clients++] = "nic";
5077#ifdef CONFIG_SLIRP
5078 net_clients[nb_net_clients++] = "user";
5079#endif
bellardc20709a2004-04-21 23:27:19 +00005080 }
5081
bellard7c9d8e02005-11-15 22:16:05 +00005082 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005083 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005084 exit(1);
bellard702c6512004-04-02 21:21:32 +00005085 }
aliguori63a01ef2008-10-31 19:10:00 +00005086 net_client_check();
bellardf1510b22003-06-25 00:07:40 +00005087
thseec85c22007-01-05 17:41:07 +00005088#ifdef TARGET_I386
balroged494d82007-12-11 23:23:52 +00005089 /* XXX: this should be moved in the PC machine instantiation code */
j_mayer28c5af52007-11-11 01:50:45 +00005090 if (net_boot != 0) {
5091 int netroms = 0;
5092 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00005093 const char *model = nd_table[i].model;
5094 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00005095 if (net_boot & (1 << i)) {
5096 if (model == NULL)
5097 model = "ne2k_pci";
5098 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5099 if (get_image_size(buf) > 0) {
5100 if (nb_option_roms >= MAX_OPTION_ROMS) {
5101 fprintf(stderr, "Too many option ROMs\n");
5102 exit(1);
5103 }
5104 option_rom[nb_option_roms] = strdup(buf);
5105 nb_option_roms++;
5106 netroms++;
5107 }
5108 }
thseec85c22007-01-05 17:41:07 +00005109 }
j_mayer28c5af52007-11-11 01:50:45 +00005110 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00005111 fprintf(stderr, "No valid PXE rom found for network device\n");
5112 exit(1);
5113 }
thseec85c22007-01-05 17:41:07 +00005114 }
5115#endif
5116
balrogdc72ac12008-11-09 00:04:26 +00005117 /* init the bluetooth world */
5118 for (i = 0; i < nb_bt_opts; i++)
5119 if (bt_parse(bt_opts[i]))
5120 exit(1);
5121
bellard0824d6f2003-06-24 13:42:40 +00005122 /* init the memory */
balrog7fb4fdc2008-04-24 17:59:27 +00005123 phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
5124
5125 if (machine->ram_require & RAMSIZE_FIXED) {
5126 if (ram_size > 0) {
5127 if (ram_size < phys_ram_size) {
aurel32cd940062008-04-28 20:26:54 +00005128 fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
5129 machine->name, (unsigned long long) phys_ram_size);
balrog7fb4fdc2008-04-24 17:59:27 +00005130 exit(-1);
5131 }
5132
5133 phys_ram_size = ram_size;
5134 } else
5135 ram_size = phys_ram_size;
5136 } else {
aurel324fc5d072008-04-27 21:39:40 +00005137 if (ram_size == 0)
balrog7fb4fdc2008-04-24 17:59:27 +00005138 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5139
5140 phys_ram_size += ram_size;
5141 }
ths9ae02552007-01-05 17:39:04 +00005142
bellardd993e022005-02-10 22:00:06 +00005143 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00005144 if (!phys_ram_base) {
5145 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00005146 exit(1);
5147 }
5148
bellard26a5f132008-05-28 12:30:31 +00005149 /* init the dynamic translator */
5150 cpu_exec_init_all(tb_size * 1024 * 1024);
5151
bellard5905b2e2004-08-01 21:53:26 +00005152 bdrv_init();
aliguori6512a2a2009-03-20 18:26:07 +00005153 dma_helper_init();
thse4bcb142007-12-02 04:51:10 +00005154
5155 /* we always create the cdrom drive, even if no disk is there */
5156
5157 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005158 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005159
balrog9d413d12007-12-04 00:10:34 +00005160 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005161
5162 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005163 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005164
balrog9d413d12007-12-04 00:10:34 +00005165 /* we always create one sd slot, even if no card is in it */
5166
5167 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005168 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005169
ths96d30e42007-01-07 20:42:14 +00005170 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005171
thse4bcb142007-12-02 04:51:10 +00005172 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005173 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005174 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005175
bellardc88676f2006-08-06 13:36:11 +00005176 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005177 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005178
aliguori3023f3322009-01-16 19:04:14 +00005179#ifndef _WIN32
5180 /* must be after terminal init, SDL library changes signal handlers */
5181 termsig_setup();
5182#endif
5183
5184 /* Maintain compatibility with multiple stdio monitors */
5185 if (!strcmp(monitor_device,"stdio")) {
5186 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5187 const char *devname = serial_devices[i];
5188 if (devname && !strcmp(devname,"mon:stdio")) {
5189 monitor_device = NULL;
5190 break;
5191 } else if (devname && !strcmp(devname,"stdio")) {
5192 monitor_device = NULL;
5193 serial_devices[i] = "mon:stdio";
5194 break;
5195 }
5196 }
5197 }
5198
5199 if (kvm_enabled()) {
5200 int ret;
5201
5202 ret = kvm_init(smp_cpus);
5203 if (ret < 0) {
5204 fprintf(stderr, "failed to initialize KVM\n");
5205 exit(1);
5206 }
5207 }
5208
aliguori4c621802009-01-16 21:48:20 +00005209 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005210 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005211 if (!monitor_hd) {
5212 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5213 exit(1);
5214 }
5215 }
5216
aliguori2796dae2009-01-16 20:23:27 +00005217 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5218 const char *devname = serial_devices[i];
5219 if (devname && strcmp(devname, "none")) {
5220 char label[32];
5221 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005222 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005223 if (!serial_hds[i]) {
5224 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5225 devname);
5226 exit(1);
5227 }
5228 }
5229 }
5230
5231 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5232 const char *devname = parallel_devices[i];
5233 if (devname && strcmp(devname, "none")) {
5234 char label[32];
5235 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005236 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005237 if (!parallel_hds[i]) {
5238 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5239 devname);
5240 exit(1);
5241 }
5242 }
5243 }
5244
5245 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5246 const char *devname = virtio_consoles[i];
5247 if (devname && strcmp(devname, "none")) {
5248 char label[32];
5249 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005250 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005251 if (!virtcon_hds[i]) {
5252 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5253 devname);
5254 exit(1);
5255 }
5256 }
5257 }
5258
aliguori3023f3322009-01-16 19:04:14 +00005259 machine->init(ram_size, vga_ram_size, boot_devices,
5260 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5261
aliguori6f338c32009-02-11 15:21:54 +00005262 current_machine = machine;
5263
aliguori3023f3322009-01-16 19:04:14 +00005264 /* Set KVM's vcpu state to qemu's initial CPUState. */
5265 if (kvm_enabled()) {
5266 int ret;
5267
5268 ret = kvm_sync_vcpus();
5269 if (ret < 0) {
5270 fprintf(stderr, "failed to initialize vcpus\n");
5271 exit(1);
5272 }
5273 }
5274
5275 /* init USB devices */
5276 if (usb_enabled) {
5277 for(i = 0; i < usb_devices_index; i++) {
aliguoric0f4ce72009-03-05 23:01:01 +00005278 if (usb_device_add(usb_devices[i], 0) < 0) {
aliguori3023f3322009-01-16 19:04:14 +00005279 fprintf(stderr, "Warning: could not add USB device %s\n",
5280 usb_devices[i]);
5281 }
5282 }
5283 }
5284
aliguori8f391ab2009-01-19 16:34:10 +00005285 if (!display_state)
5286 dumb_display_init();
aliguori3023f3322009-01-16 19:04:14 +00005287 /* just use the first displaystate for the moment */
5288 ds = display_state;
bellard313aa562003-08-10 21:52:11 +00005289 /* terminal init */
bellarda20dd502003-09-30 21:07:02 +00005290 if (nographic) {
balrog4d3b6f62008-02-10 16:33:14 +00005291 if (curses) {
5292 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5293 exit(1);
5294 }
aliguori7d957bd2009-01-15 22:14:11 +00005295 } else {
balrog4d3b6f62008-02-10 16:33:14 +00005296#if defined(CONFIG_CURSES)
aliguori7d957bd2009-01-15 22:14:11 +00005297 if (curses) {
5298 /* At the moment curses cannot be used with other displays */
5299 curses_display_init(ds, full_screen);
5300 } else
balrog4d3b6f62008-02-10 16:33:14 +00005301#endif
aliguori7d957bd2009-01-15 22:14:11 +00005302 {
5303 if (vnc_display != NULL) {
5304 vnc_display_init(ds);
5305 if (vnc_display_open(ds, vnc_display) < 0)
5306 exit(1);
5307 }
bellard5b0753e2005-03-01 21:37:28 +00005308#if defined(CONFIG_SDL)
aliguorid268de02009-01-22 16:18:33 +00005309 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005310 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00005311#elif defined(CONFIG_COCOA)
aliguorid268de02009-01-22 16:18:33 +00005312 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005313 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00005314#endif
aliguori7d957bd2009-01-15 22:14:11 +00005315 }
bellard313aa562003-08-10 21:52:11 +00005316 }
aliguori7d957bd2009-01-15 22:14:11 +00005317 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005318
aliguori3023f3322009-01-16 19:04:14 +00005319 dcl = ds->listeners;
5320 while (dcl != NULL) {
5321 if (dcl->dpy_refresh != NULL) {
5322 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5323 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005324 }
aliguori3023f3322009-01-16 19:04:14 +00005325 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005326 }
aliguori3023f3322009-01-16 19:04:14 +00005327
blueswir19043b622009-01-21 19:28:13 +00005328 if (nographic || (vnc_display && !sdl)) {
5329 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5330 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5331 }
5332
aliguori2796dae2009-01-16 20:23:27 +00005333 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00005334 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00005335
aliguori4c621802009-01-16 21:48:20 +00005336 if (monitor_device && monitor_hd)
aliguoricde76ee2009-03-05 23:01:51 +00005337 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
bellard82c643f2004-07-14 17:28:13 +00005338
bellard8d11df92004-08-24 21:13:40 +00005339 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005340 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005341 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005342 char label[32];
5343 snprintf(label, sizeof(label), "serial%d", i);
thsaf3a9032007-07-11 23:14:59 +00005344 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005345 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005346 }
bellard82c643f2004-07-14 17:28:13 +00005347 }
bellard82c643f2004-07-14 17:28:13 +00005348
bellard6508fe52005-01-15 12:02:56 +00005349 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005350 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005351 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005352 char label[32];
5353 snprintf(label, sizeof(label), "parallel%d", i);
thsaf3a9032007-07-11 23:14:59 +00005354 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005355 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005356 }
5357 }
5358
aliguori9ede2fd2009-01-15 20:05:25 +00005359 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5360 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005361 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005362 char label[32];
5363 snprintf(label, sizeof(label), "virtcon%d", i);
aliguori9ede2fd2009-01-15 20:05:25 +00005364 if (strstart(devname, "vc", 0))
5365 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5366 }
5367 }
5368
bellard67b915a2004-03-31 23:37:16 +00005369#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00005370 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00005371 /* XXX: use standard host:port notation and modify options
5372 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00005373 if (gdbserver_start(gdbstub_port) < 0) {
5374 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00005375 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00005376 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00005377 }
balrog45669e02007-07-02 13:20:17 +00005378 }
bellard67b915a2004-03-31 23:37:16 +00005379#endif
balrog45669e02007-07-02 13:20:17 +00005380
bellardd63d3072004-10-03 13:29:03 +00005381 if (loadvm)
aliguori376253e2009-03-05 23:01:23 +00005382 do_loadvm(cur_mon, loadvm);
bellardd63d3072004-10-03 13:29:03 +00005383
aliguori5bb79102008-10-13 03:12:02 +00005384 if (incoming) {
5385 autostart = 0; /* fixme how to deal with -daemonize */
5386 qemu_start_incoming_migration(incoming);
5387 }
5388
aliguoric0f4ce72009-03-05 23:01:01 +00005389 if (autostart)
5390 vm_start();
thsffd843b2006-12-21 19:46:43 +00005391
ths71e3ceb2006-12-22 02:11:31 +00005392 if (daemonize) {
5393 uint8_t status = 0;
5394 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005395
5396 again1:
5397 len = write(fds[1], &status, 1);
5398 if (len == -1 && (errno == EINTR))
5399 goto again1;
5400
5401 if (len != 1)
5402 exit(1);
5403
aliguoribd54b862008-07-23 00:58:33 +00005404 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005405 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005406 if (fd == -1)
5407 exit(1);
aliguori08585322009-02-27 22:09:45 +00005408 }
ths71e3ceb2006-12-22 02:11:31 +00005409
aliguori08585322009-02-27 22:09:45 +00005410#ifndef _WIN32
5411 if (run_as) {
5412 pwd = getpwnam(run_as);
5413 if (!pwd) {
5414 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5415 exit(1);
5416 }
5417 }
ths71e3ceb2006-12-22 02:11:31 +00005418
aliguori08585322009-02-27 22:09:45 +00005419 if (chroot_dir) {
5420 if (chroot(chroot_dir) < 0) {
5421 fprintf(stderr, "chroot failed\n");
5422 exit(1);
5423 }
5424 chdir("/");
5425 }
5426
5427 if (run_as) {
5428 if (setgid(pwd->pw_gid) < 0) {
5429 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5430 exit(1);
5431 }
5432 if (setuid(pwd->pw_uid) < 0) {
5433 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5434 exit(1);
5435 }
5436 if (setuid(0) != -1) {
5437 fprintf(stderr, "Dropping privileges failed\n");
5438 exit(1);
5439 }
5440 }
5441#endif
5442
5443 if (daemonize) {
5444 dup2(fd, 0);
5445 dup2(fd, 1);
5446 dup2(fd, 2);
5447
5448 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00005449 }
5450
bellard8a7ddc32004-03-31 19:00:16 +00005451 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005452 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005453 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005454
bellard0824d6f2003-06-24 13:42:40 +00005455 return 0;
5456}