blob: 66dd975f8453d8f240e0cd41b5a3ed005cab6130 [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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "hw/boards.h"
26#include "hw/usb.h"
27#include "hw/pcmcia.h"
28#include "hw/pc.h"
pbrook87ecb682007-11-17 17:14:51 +000029#include "hw/audiodev.h"
30#include "hw/isa.h"
aurel322e4d9fb2008-04-08 06:01:02 +000031#include "hw/baum.h"
balrog1ae26a12008-09-28 23:19:47 +000032#include "hw/bt.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "net.h"
34#include "console.h"
35#include "sysemu.h"
36#include "gdbstub.h"
37#include "qemu-timer.h"
38#include "qemu-char.h"
malc902b3d52008-12-10 19:18:40 +000039#include "cache-utils.h"
pbrook87ecb682007-11-17 17:14:51 +000040#include "block.h"
41#include "audio/audio.h"
aliguori5bb79102008-10-13 03:12:02 +000042#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000043#include "kvm.h"
aliguoridf751fa2008-12-04 20:19:35 +000044#include "balloon.h"
bellard67b915a2004-03-31 23:37:16 +000045
bellard0824d6f2003-06-24 13:42:40 +000046#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000047#include <fcntl.h>
48#include <signal.h>
49#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000050#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000051#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000052#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000053
54#ifndef _WIN32
55#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000056#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000057#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000058#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000059#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000060#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000061#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000062#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000063#include <net/if.h>
64#if defined(__NetBSD__)
65#include <net/if_tap.h>
66#endif
67#ifdef __linux__
68#include <linux/if_tun.h>
69#endif
70#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000071#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000072#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000073#include <sys/select.h>
bellard7d3505c2004-05-12 19:32:15 +000074#ifdef _BSD
75#include <sys/stat.h>
blueswir124646c72008-11-07 16:55:48 +000076#ifdef __FreeBSD__
bellard7d3505c2004-05-12 19:32:15 +000077#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000078#else
79#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000080#endif
ths5c40d2b2007-06-23 16:03:36 +000081#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
82#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000083#else
blueswir1223f0d72008-09-30 18:12:18 +000084#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000085#include <pty.h>
86#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000087#include <linux/rtc.h>
thsbd494f42007-09-16 20:03:23 +000088
89/* For the benefit of older linux systems which don't supply it,
90 we use a local copy of hpet.h. */
91/* #include <linux/hpet.h> */
92#include "hpet.h"
93
bellarde57a8c02005-11-10 23:58:52 +000094#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000095#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000096#endif
97#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000098#include <sys/stat.h>
99#include <sys/ethernet.h>
100#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +0000101#include <netinet/arp.h>
102#include <netinet/in.h>
103#include <netinet/in_systm.h>
104#include <netinet/ip.h>
105#include <netinet/ip_icmp.h> // must come after ip.h
106#include <netinet/udp.h>
107#include <netinet/tcp.h>
108#include <net/if.h>
109#include <syslog.h>
110#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +0000111#endif
bellard7d3505c2004-05-12 19:32:15 +0000112#endif
bellardec530c82006-04-25 22:36:06 +0000113#endif
bellard67b915a2004-03-31 23:37:16 +0000114
aliguori03ff3ca2008-09-15 15:51:35 +0000115#include "qemu_socket.h"
116
bellardc20709a2004-04-21 23:27:19 +0000117#if defined(CONFIG_SLIRP)
118#include "libslirp.h"
119#endif
120
blueswir19892fbf2008-08-24 10:34:20 +0000121#if defined(__OpenBSD__)
122#include <util.h>
123#endif
124
ths8a16d272008-07-19 09:56:24 +0000125#if defined(CONFIG_VDE)
126#include <libvdeplug.h>
127#endif
128
bellard67b915a2004-03-31 23:37:16 +0000129#ifdef _WIN32
bellard7d3505c2004-05-12 19:32:15 +0000130#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000131#include <sys/timeb.h>
ths4fddf622007-12-17 04:42:29 +0000132#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000133#define getopt_long_only getopt_long
134#define memalign(align, size) malloc(size)
135#endif
136
bellard73332e52004-04-04 20:22:28 +0000137#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +0000138#ifdef __APPLE__
bellard83fb7ad2004-07-05 21:25:26 +0000139#include <SDL/SDL.h>
bellard96bcd4f2004-07-10 16:26:15 +0000140#endif
bellard73332e52004-04-04 20:22:28 +0000141#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000142
bellard5b0753e2005-03-01 21:37:28 +0000143#ifdef CONFIG_COCOA
144#undef main
145#define main qemu_main
146#endif /* CONFIG_COCOA */
147
bellard0824d6f2003-06-24 13:42:40 +0000148#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000149
bellard8a7ddc32004-03-31 19:00:16 +0000150#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000151
bellard5a671352003-10-01 00:13:48 +0000152#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
thsb46a8902007-10-21 23:20:45 +0000153#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
pbrooka14d6c82006-12-23 15:37:33 +0000154#ifdef __sun__
155#define SMBD_COMMAND "/usr/sfw/sbin/smbd"
156#else
157#define SMBD_COMMAND "/usr/sbin/smbd"
158#endif
bellardf1510b22003-06-25 00:07:40 +0000159
bellard0824d6f2003-06-24 13:42:40 +0000160//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000161//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000162//#define DEBUG_NET
163//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000164
bellard77d4bc32004-05-26 22:13:53 +0000165#ifdef TARGET_PPC
166#define DEFAULT_RAM_SIZE 144
167#else
bellard1bfe8562004-07-08 21:17:50 +0000168#define DEFAULT_RAM_SIZE 128
bellard77d4bc32004-05-26 22:13:53 +0000169#endif
bellard313aa562003-08-10 21:52:11 +0000170
pbrook0d92ed32006-05-21 16:30:15 +0000171/* Max number of USB devices that can be specified on the commandline. */
172#define MAX_USB_CMDLINE 8
173
balrogdc72ac12008-11-09 00:04:26 +0000174/* Max number of bluetooth switches on the commandline. */
175#define MAX_BT_CMDLINE 10
176
bellard7dea1da2003-11-16 15:59:30 +0000177/* XXX: use a two level table to limit memory usage */
178#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000179
bellard80cabfa2004-03-14 12:20:30 +0000180const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000181const char *bios_name = NULL;
blueswir1dbed7e42008-10-01 19:38:09 +0000182static void *ioport_opaque[MAX_IOPORTS];
183static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
184static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
thse4bcb142007-12-02 04:51:10 +0000185/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000186 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000187DriveInfo drives_table[MAX_DRIVES+1];
188int nb_drives;
blueswir1dbed7e42008-10-01 19:38:09 +0000189static int vga_ram_size;
malccb5a7aa2008-09-28 00:42:12 +0000190enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori0e82f342008-10-31 18:44:40 +0000191DisplayState display_state;
bellarda20dd502003-09-30 21:07:02 +0000192int nographic;
blueswir1dbed7e42008-10-01 19:38:09 +0000193static int curses;
bellard3d11d0e2004-12-12 16:56:30 +0000194const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000195int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000196ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000197int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000198NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000199int vm_running;
balrogf6503052008-02-17 11:42:19 +0000200static int rtc_utc = 1;
201static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000202int cirrus_vga_enabled = 1;
thsd34cab92007-04-02 01:10:46 +0000203int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000204#ifdef TARGET_SPARC
205int graphic_width = 1024;
206int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000207int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000208#else
bellard1bfe8562004-07-08 21:17:50 +0000209int graphic_width = 800;
210int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000211int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000212#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000213static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000214#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000215static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000216#endif
ths667acca2006-12-11 02:08:05 +0000217int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000218CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000219CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
bellarda09db212005-04-30 16:10:35 +0000220#ifdef TARGET_I386
221int win2k_install_hack = 0;
222#endif
bellardbb36d472005-11-05 14:22:28 +0000223int usb_enabled = 0;
bellard6a00d602005-11-21 23:25:50 +0000224int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000225const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000226int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000227int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000228int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000229int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000230int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000231int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000232int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000233int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000234const char *option_rom[MAX_OPTION_ROMS];
235int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000236int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000237#ifdef TARGET_ARM
238int old_param = 0;
239#endif
thsc35734b2007-03-19 15:17:08 +0000240const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000241int alt_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000242#ifdef TARGET_SPARC
243unsigned int nb_prom_envs = 0;
244const char *prom_envs[MAX_PROM_ENVS];
245#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000246static int nb_drives_opt;
247static struct drive_opt {
balrog609497a2008-01-14 02:56:53 +0000248 const char *file;
249 char opt[1024];
250} drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000251
balrogee5605e2007-12-03 03:01:40 +0000252static CPUState *cur_cpu;
253static CPUState *next_cpu;
balrog76ea08f2007-12-16 11:48:54 +0000254static int event_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000255/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000256static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000257/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000258#define MAX_ICOUNT_SHIFT 10
259/* Compensate for varying guest execution speed. */
260static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000261static QEMUTimer *icount_rt_timer;
262static QEMUTimer *icount_vm_timer;
balrogee5605e2007-12-03 03:01:40 +0000263
blueswir18fcb1b92008-09-18 18:29:08 +0000264uint8_t qemu_uuid[16];
265
bellard0824d6f2003-06-24 13:42:40 +0000266/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000267/* x86 ISA bus support */
268
269target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000270PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000271
aliguori477e3ed2008-07-23 15:19:59 +0000272static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
273static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
274
275static uint32_t ioport_read(int index, uint32_t address)
276{
277 static IOPortReadFunc *default_func[3] = {
278 default_ioport_readb,
279 default_ioport_readw,
280 default_ioport_readl
281 };
282 IOPortReadFunc *func = ioport_read_table[index][address];
283 if (!func)
284 func = default_func[index];
285 return func(ioport_opaque[address], address);
286}
287
288static void ioport_write(int index, uint32_t address, uint32_t data)
289{
290 static IOPortWriteFunc *default_func[3] = {
291 default_ioport_writeb,
292 default_ioport_writew,
293 default_ioport_writel
294 };
295 IOPortWriteFunc *func = ioport_write_table[index][address];
296 if (!func)
297 func = default_func[index];
298 func(ioport_opaque[address], address, data);
299}
300
pbrook9596ebb2007-11-18 01:44:38 +0000301static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000302{
303#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000304 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000305#endif
bellardfc01f7e2003-06-30 10:03:06 +0000306 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000307}
308
pbrook9596ebb2007-11-18 01:44:38 +0000309static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000310{
311#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000312 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000313#endif
314}
315
316/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000317static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000318{
319 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000320 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000321 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000322 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000323 return data;
324}
325
pbrook9596ebb2007-11-18 01:44:38 +0000326static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000327{
aliguori477e3ed2008-07-23 15:19:59 +0000328 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000329 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000330 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000331}
332
pbrook9596ebb2007-11-18 01:44:38 +0000333static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000334{
335#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000336 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000337#endif
338 return 0xffffffff;
339}
340
pbrook9596ebb2007-11-18 01:44:38 +0000341static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000342{
343#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000344 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000345#endif
bellard0824d6f2003-06-24 13:42:40 +0000346}
347
bellardfc01f7e2003-06-30 10:03:06 +0000348/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000349int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000350 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000351{
bellardfc01f7e2003-06-30 10:03:06 +0000352 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000353
bellardc4b1fcc2004-03-14 21:44:30 +0000354 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000355 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000356 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000357 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000358 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000359 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000360 } else {
balrog88fdf562008-04-26 21:11:22 +0000361 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000362 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000363 }
364 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000365 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000366 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000367 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000368 ioport_opaque[i] = opaque;
369 }
bellard0824d6f2003-06-24 13:42:40 +0000370 return 0;
371}
372
bellardfc01f7e2003-06-30 10:03:06 +0000373/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000374int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000375 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000376{
bellardfc01f7e2003-06-30 10:03:06 +0000377 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000378
bellardc4b1fcc2004-03-14 21:44:30 +0000379 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000380 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000381 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000382 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000383 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000384 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000385 } else {
balrog88fdf562008-04-26 21:11:22 +0000386 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000387 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000388 }
389 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000390 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000391 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
392 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000393 ioport_opaque[i] = opaque;
394 }
bellardf1510b22003-06-25 00:07:40 +0000395 return 0;
396}
397
bellard69b91032004-05-18 23:05:28 +0000398void isa_unassign_ioport(int start, int length)
399{
400 int i;
401
402 for(i = start; i < start + length; i++) {
403 ioport_read_table[0][i] = default_ioport_readb;
404 ioport_read_table[1][i] = default_ioport_readw;
405 ioport_read_table[2][i] = default_ioport_readl;
406
407 ioport_write_table[0][i] = default_ioport_writeb;
408 ioport_write_table[1][i] = default_ioport_writew;
409 ioport_write_table[2][i] = default_ioport_writel;
410 }
411}
412
bellard20f32282005-01-03 23:36:21 +0000413/***********************************************************/
414
bellardc45886d2004-01-05 00:02:06 +0000415void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000416{
bellardfd872592004-05-12 19:11:15 +0000417#ifdef DEBUG_IOPORT
418 if (loglevel & CPU_LOG_IOPORT)
419 fprintf(logfile, "outb: %04x %02x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000420#endif
aliguori477e3ed2008-07-23 15:19:59 +0000421 ioport_write(0, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000422#ifdef USE_KQEMU
423 if (env)
424 env->last_io_time = cpu_get_time_fast();
425#endif
bellard0824d6f2003-06-24 13:42:40 +0000426}
427
bellardc45886d2004-01-05 00:02:06 +0000428void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000429{
bellardfd872592004-05-12 19:11:15 +0000430#ifdef DEBUG_IOPORT
431 if (loglevel & CPU_LOG_IOPORT)
432 fprintf(logfile, "outw: %04x %04x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000433#endif
aliguori477e3ed2008-07-23 15:19:59 +0000434 ioport_write(1, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000435#ifdef USE_KQEMU
436 if (env)
437 env->last_io_time = cpu_get_time_fast();
438#endif
bellard0824d6f2003-06-24 13:42:40 +0000439}
440
bellardc45886d2004-01-05 00:02:06 +0000441void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000442{
bellardfd872592004-05-12 19:11:15 +0000443#ifdef DEBUG_IOPORT
444 if (loglevel & CPU_LOG_IOPORT)
445 fprintf(logfile, "outl: %04x %08x\n", addr, val);
446#endif
aliguori477e3ed2008-07-23 15:19:59 +0000447 ioport_write(2, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000448#ifdef USE_KQEMU
449 if (env)
450 env->last_io_time = cpu_get_time_fast();
451#endif
bellard0824d6f2003-06-24 13:42:40 +0000452}
453
bellardc45886d2004-01-05 00:02:06 +0000454int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000455{
bellardfd872592004-05-12 19:11:15 +0000456 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000457 val = ioport_read(0, addr);
bellardfd872592004-05-12 19:11:15 +0000458#ifdef DEBUG_IOPORT
459 if (loglevel & CPU_LOG_IOPORT)
460 fprintf(logfile, "inb : %04x %02x\n", addr, val);
461#endif
bellard89bfc102006-02-08 22:46:31 +0000462#ifdef USE_KQEMU
463 if (env)
464 env->last_io_time = cpu_get_time_fast();
465#endif
bellardfd872592004-05-12 19:11:15 +0000466 return val;
bellard0824d6f2003-06-24 13:42:40 +0000467}
468
bellardc45886d2004-01-05 00:02:06 +0000469int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000470{
bellardfd872592004-05-12 19:11:15 +0000471 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000472 val = ioport_read(1, addr);
bellardfd872592004-05-12 19:11:15 +0000473#ifdef DEBUG_IOPORT
474 if (loglevel & CPU_LOG_IOPORT)
475 fprintf(logfile, "inw : %04x %04x\n", addr, val);
476#endif
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);
bellardfd872592004-05-12 19:11:15 +0000488#ifdef DEBUG_IOPORT
489 if (loglevel & CPU_LOG_IOPORT)
490 fprintf(logfile, "inl : %04x %08x\n", addr, val);
491#endif
bellard89bfc102006-02-08 22:46:31 +0000492#ifdef USE_KQEMU
493 if (env)
494 env->last_io_time = cpu_get_time_fast();
495#endif
bellardfd872592004-05-12 19:11:15 +0000496 return val;
bellard0824d6f2003-06-24 13:42:40 +0000497}
498
499/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000500void hw_error(const char *fmt, ...)
501{
502 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000503 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000504
505 va_start(ap, fmt);
506 fprintf(stderr, "qemu: hardware error: ");
507 vfprintf(stderr, fmt, ap);
508 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000509 for(env = first_cpu; env != NULL; env = env->next_cpu) {
510 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000511#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000512 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000513#else
bellard6a00d602005-11-21 23:25:50 +0000514 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000515#endif
bellard6a00d602005-11-21 23:25:50 +0000516 }
bellard0824d6f2003-06-24 13:42:40 +0000517 va_end(ap);
518 abort();
519}
aliguoridf751fa2008-12-04 20:19:35 +0000520
521/***************/
522/* ballooning */
523
524static QEMUBalloonEvent *qemu_balloon_event;
525void *qemu_balloon_event_opaque;
526
527void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
528{
529 qemu_balloon_event = func;
530 qemu_balloon_event_opaque = opaque;
531}
532
533void qemu_balloon(ram_addr_t target)
534{
535 if (qemu_balloon_event)
536 qemu_balloon_event(qemu_balloon_event_opaque, target);
537}
538
539ram_addr_t qemu_balloon_status(void)
540{
541 if (qemu_balloon_event)
542 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
543 return 0;
544}
bellard0824d6f2003-06-24 13:42:40 +0000545
bellard8a7ddc32004-03-31 19:00:16 +0000546/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000547/* keyboard/mouse */
548
549static QEMUPutKBDEvent *qemu_put_kbd_event;
550static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000551static QEMUPutMouseEntry *qemu_put_mouse_event_head;
552static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000553
554void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
555{
556 qemu_put_kbd_event_opaque = opaque;
557 qemu_put_kbd_event = func;
558}
559
ths455204e2007-01-05 16:42:13 +0000560QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
561 void *opaque, int absolute,
562 const char *name)
bellard63066f42004-06-03 18:45:02 +0000563{
ths455204e2007-01-05 16:42:13 +0000564 QEMUPutMouseEntry *s, *cursor;
565
566 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
567 if (!s)
568 return NULL;
569
570 s->qemu_put_mouse_event = func;
571 s->qemu_put_mouse_event_opaque = opaque;
572 s->qemu_put_mouse_event_absolute = absolute;
573 s->qemu_put_mouse_event_name = qemu_strdup(name);
574 s->next = NULL;
575
576 if (!qemu_put_mouse_event_head) {
577 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
578 return s;
579 }
580
581 cursor = qemu_put_mouse_event_head;
582 while (cursor->next != NULL)
583 cursor = cursor->next;
584
585 cursor->next = s;
586 qemu_put_mouse_event_current = s;
587
588 return s;
589}
590
591void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
592{
593 QEMUPutMouseEntry *prev = NULL, *cursor;
594
595 if (!qemu_put_mouse_event_head || entry == NULL)
596 return;
597
598 cursor = qemu_put_mouse_event_head;
599 while (cursor != NULL && cursor != entry) {
600 prev = cursor;
601 cursor = cursor->next;
602 }
603
604 if (cursor == NULL) // does not exist or list empty
605 return;
606 else if (prev == NULL) { // entry is head
607 qemu_put_mouse_event_head = cursor->next;
608 if (qemu_put_mouse_event_current == entry)
609 qemu_put_mouse_event_current = cursor->next;
610 qemu_free(entry->qemu_put_mouse_event_name);
611 qemu_free(entry);
612 return;
613 }
614
615 prev->next = entry->next;
616
617 if (qemu_put_mouse_event_current == entry)
618 qemu_put_mouse_event_current = prev;
619
620 qemu_free(entry->qemu_put_mouse_event_name);
621 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000622}
623
624void kbd_put_keycode(int keycode)
625{
626 if (qemu_put_kbd_event) {
627 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
628 }
629}
630
631void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
632{
ths455204e2007-01-05 16:42:13 +0000633 QEMUPutMouseEvent *mouse_event;
634 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000635 int width;
ths455204e2007-01-05 16:42:13 +0000636
637 if (!qemu_put_mouse_event_current) {
638 return;
639 }
640
641 mouse_event =
642 qemu_put_mouse_event_current->qemu_put_mouse_event;
643 mouse_event_opaque =
644 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
645
646 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000647 if (graphic_rotate) {
648 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
649 width = 0x7fff;
650 else
aurel32b94ed572008-03-10 19:34:27 +0000651 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000652 mouse_event(mouse_event_opaque,
653 width - dy, dx, dz, buttons_state);
654 } else
655 mouse_event(mouse_event_opaque,
656 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000657 }
658}
659
bellard09b26c52006-04-12 21:09:08 +0000660int kbd_mouse_is_absolute(void)
661{
ths455204e2007-01-05 16:42:13 +0000662 if (!qemu_put_mouse_event_current)
663 return 0;
664
665 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
666}
667
668void do_info_mice(void)
669{
670 QEMUPutMouseEntry *cursor;
671 int index = 0;
672
673 if (!qemu_put_mouse_event_head) {
674 term_printf("No mouse devices connected\n");
675 return;
676 }
677
678 term_printf("Mouse devices available:\n");
679 cursor = qemu_put_mouse_event_head;
680 while (cursor != NULL) {
681 term_printf("%c Mouse #%d: %s\n",
682 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
683 index, cursor->qemu_put_mouse_event_name);
684 index++;
685 cursor = cursor->next;
686 }
687}
688
689void do_mouse_set(int index)
690{
691 QEMUPutMouseEntry *cursor;
692 int i = 0;
693
694 if (!qemu_put_mouse_event_head) {
695 term_printf("No mouse devices connected\n");
696 return;
697 }
698
699 cursor = qemu_put_mouse_event_head;
700 while (cursor != NULL && index != i) {
701 i++;
702 cursor = cursor->next;
703 }
704
705 if (cursor != NULL)
706 qemu_put_mouse_event_current = cursor;
707 else
708 term_printf("Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000709}
710
bellard87858c82003-06-27 12:01:39 +0000711/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000712uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000713{
714 union {
715 uint64_t ll;
716 struct {
717#ifdef WORDS_BIGENDIAN
718 uint32_t high, low;
719#else
720 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000721#endif
bellard87858c82003-06-27 12:01:39 +0000722 } l;
723 } u, res;
724 uint64_t rl, rh;
725
726 u.ll = a;
727 rl = (uint64_t)u.l.low * (uint64_t)b;
728 rh = (uint64_t)u.l.high * (uint64_t)b;
729 rh += (rl >> 32);
730 res.l.high = rh / c;
731 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
732 return res.ll;
733}
734
bellard1dce7c32006-07-13 23:20:22 +0000735/***********************************************************/
736/* real time host monotonic timer */
737
738#define QEMU_TIMER_BASE 1000000000LL
739
740#ifdef WIN32
741
742static int64_t clock_freq;
743
744static void init_get_clock(void)
745{
bellarda8e5ac32006-07-14 09:36:13 +0000746 LARGE_INTEGER freq;
747 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000748 ret = QueryPerformanceFrequency(&freq);
749 if (ret == 0) {
750 fprintf(stderr, "Could not calibrate ticks\n");
751 exit(1);
752 }
753 clock_freq = freq.QuadPart;
754}
755
756static int64_t get_clock(void)
757{
758 LARGE_INTEGER ti;
759 QueryPerformanceCounter(&ti);
760 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
761}
762
763#else
764
765static int use_rt_clock;
766
767static void init_get_clock(void)
768{
769 use_rt_clock = 0;
aurel3260759372008-10-13 21:08:25 +0000770#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000771 {
772 struct timespec ts;
773 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
774 use_rt_clock = 1;
775 }
776 }
777#endif
778}
779
780static int64_t get_clock(void)
781{
aurel3260759372008-10-13 21:08:25 +0000782#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000783 if (use_rt_clock) {
784 struct timespec ts;
785 clock_gettime(CLOCK_MONOTONIC, &ts);
786 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000787 } else
bellard1dce7c32006-07-13 23:20:22 +0000788#endif
789 {
790 /* XXX: using gettimeofday leads to problems if the date
791 changes, so it should be avoided. */
792 struct timeval tv;
793 gettimeofday(&tv, NULL);
794 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
795 }
796}
bellard1dce7c32006-07-13 23:20:22 +0000797#endif
798
pbrook2e70f6e2008-06-29 01:03:05 +0000799/* Return the virtual CPU time, based on the instruction counter. */
800static int64_t cpu_get_icount(void)
801{
802 int64_t icount;
803 CPUState *env = cpu_single_env;;
804 icount = qemu_icount;
805 if (env) {
806 if (!can_do_io(env))
807 fprintf(stderr, "Bad clock read\n");
808 icount -= (env->icount_decr.u16.low + env->icount_extra);
809 }
810 return qemu_icount_bias + (icount << icount_time_shift);
811}
812
bellard1dce7c32006-07-13 23:20:22 +0000813/***********************************************************/
814/* guest cycle counter */
815
816static int64_t cpu_ticks_prev;
817static int64_t cpu_ticks_offset;
818static int64_t cpu_clock_offset;
819static int cpu_ticks_enabled;
820
821/* return the host CPU cycle counter and handle stop/restart */
822int64_t cpu_get_ticks(void)
823{
pbrook2e70f6e2008-06-29 01:03:05 +0000824 if (use_icount) {
825 return cpu_get_icount();
826 }
bellard1dce7c32006-07-13 23:20:22 +0000827 if (!cpu_ticks_enabled) {
828 return cpu_ticks_offset;
829 } else {
830 int64_t ticks;
831 ticks = cpu_get_real_ticks();
832 if (cpu_ticks_prev > ticks) {
833 /* Note: non increasing ticks may happen if the host uses
834 software suspend */
835 cpu_ticks_offset += cpu_ticks_prev - ticks;
836 }
837 cpu_ticks_prev = ticks;
838 return ticks + cpu_ticks_offset;
839 }
840}
841
842/* return the host CPU monotonic timer and handle stop/restart */
843static int64_t cpu_get_clock(void)
844{
845 int64_t ti;
846 if (!cpu_ticks_enabled) {
847 return cpu_clock_offset;
848 } else {
849 ti = get_clock();
850 return ti + cpu_clock_offset;
851 }
852}
853
854/* enable cpu_get_ticks() */
855void cpu_enable_ticks(void)
856{
857 if (!cpu_ticks_enabled) {
858 cpu_ticks_offset -= cpu_get_real_ticks();
859 cpu_clock_offset -= get_clock();
860 cpu_ticks_enabled = 1;
861 }
862}
863
864/* disable cpu_get_ticks() : the clock is stopped. You must not call
865 cpu_get_ticks() after that. */
866void cpu_disable_ticks(void)
867{
868 if (cpu_ticks_enabled) {
869 cpu_ticks_offset = cpu_get_ticks();
870 cpu_clock_offset = cpu_get_clock();
871 cpu_ticks_enabled = 0;
872 }
873}
874
875/***********************************************************/
876/* timers */
ths5fafdf22007-09-16 21:08:06 +0000877
bellard8a7ddc32004-03-31 19:00:16 +0000878#define QEMU_TIMER_REALTIME 0
879#define QEMU_TIMER_VIRTUAL 1
880
881struct QEMUClock {
882 int type;
883 /* XXX: add frequency */
884};
885
886struct QEMUTimer {
887 QEMUClock *clock;
888 int64_t expire_time;
889 QEMUTimerCB *cb;
890 void *opaque;
891 struct QEMUTimer *next;
892};
893
thsc8994012007-08-19 21:56:03 +0000894struct qemu_alarm_timer {
895 char const *name;
thsefe75412007-08-24 01:36:32 +0000896 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000897
898 int (*start)(struct qemu_alarm_timer *t);
899 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000900 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000901 void *priv;
902};
903
thsefe75412007-08-24 01:36:32 +0000904#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000905#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000906
907static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
908{
909 return t->flags & ALARM_FLAG_DYNTICKS;
910}
911
912static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
913{
914 if (!alarm_has_dynticks(t))
915 return;
916
917 t->rearm(t);
918}
919
920/* TODO: MIN_TIMER_REARM_US should be optimized */
921#define MIN_TIMER_REARM_US 250
922
thsc8994012007-08-19 21:56:03 +0000923static struct qemu_alarm_timer *alarm_timer;
aliguorif49e58d2008-11-05 21:22:34 +0000924#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +0000925static int alarm_timer_rfd, alarm_timer_wfd;
aliguorif49e58d2008-11-05 21:22:34 +0000926#endif
thsc8994012007-08-19 21:56:03 +0000927
928#ifdef _WIN32
929
930struct qemu_alarm_win32 {
931 MMRESULT timerId;
932 HANDLE host_alarm;
933 unsigned int period;
934} alarm_win32_data = {0, NULL, -1};
935
936static int win32_start_timer(struct qemu_alarm_timer *t);
937static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000938static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000939
940#else
941
942static int unix_start_timer(struct qemu_alarm_timer *t);
943static void unix_stop_timer(struct qemu_alarm_timer *t);
944
ths231c6582007-08-26 17:29:15 +0000945#ifdef __linux__
946
thsefe75412007-08-24 01:36:32 +0000947static int dynticks_start_timer(struct qemu_alarm_timer *t);
948static void dynticks_stop_timer(struct qemu_alarm_timer *t);
949static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
950
thsc40ec5a2007-08-19 22:09:40 +0000951static int hpet_start_timer(struct qemu_alarm_timer *t);
952static void hpet_stop_timer(struct qemu_alarm_timer *t);
953
thsc8994012007-08-19 21:56:03 +0000954static int rtc_start_timer(struct qemu_alarm_timer *t);
955static void rtc_stop_timer(struct qemu_alarm_timer *t);
956
thsefe75412007-08-24 01:36:32 +0000957#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000958
959#endif /* _WIN32 */
960
pbrook2e70f6e2008-06-29 01:03:05 +0000961/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000962 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000963 When the guest is idle real and virtual time will be aligned in
964 the IO wait loop. */
965#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
966
967static void icount_adjust(void)
968{
969 int64_t cur_time;
970 int64_t cur_icount;
971 int64_t delta;
972 static int64_t last_delta;
973 /* If the VM is not running, then do nothing. */
974 if (!vm_running)
975 return;
976
977 cur_time = cpu_get_clock();
978 cur_icount = qemu_get_clock(vm_clock);
979 delta = cur_icount - cur_time;
980 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
981 if (delta > 0
982 && last_delta + ICOUNT_WOBBLE < delta * 2
983 && icount_time_shift > 0) {
984 /* The guest is getting too far ahead. Slow time down. */
985 icount_time_shift--;
986 }
987 if (delta < 0
988 && last_delta - ICOUNT_WOBBLE > delta * 2
989 && icount_time_shift < MAX_ICOUNT_SHIFT) {
990 /* The guest is getting too far behind. Speed time up. */
991 icount_time_shift++;
992 }
993 last_delta = delta;
994 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
995}
996
997static void icount_adjust_rt(void * opaque)
998{
999 qemu_mod_timer(icount_rt_timer,
1000 qemu_get_clock(rt_clock) + 1000);
1001 icount_adjust();
1002}
1003
1004static void icount_adjust_vm(void * opaque)
1005{
1006 qemu_mod_timer(icount_vm_timer,
1007 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1008 icount_adjust();
1009}
1010
1011static void init_icount_adjust(void)
1012{
1013 /* Have both realtime and virtual time triggers for speed adjustment.
1014 The realtime trigger catches emulated time passing too slowly,
1015 the virtual time trigger catches emulated time passing too fast.
1016 Realtime triggers occur even when idle, so use them less frequently
1017 than VM triggers. */
1018 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1019 qemu_mod_timer(icount_rt_timer,
1020 qemu_get_clock(rt_clock) + 1000);
1021 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1022 qemu_mod_timer(icount_vm_timer,
1023 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1024}
1025
thsc8994012007-08-19 21:56:03 +00001026static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +00001027#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +00001028#ifdef __linux__
thsefe75412007-08-24 01:36:32 +00001029 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1030 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001031 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +00001032 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001033 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +00001034 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001035#endif
thsefe75412007-08-24 01:36:32 +00001036 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001037#else
thsefe75412007-08-24 01:36:32 +00001038 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1039 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1040 {"win32", 0, win32_start_timer,
1041 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +00001042#endif
1043 {NULL, }
1044};
1045
blueswir13f47aa82008-03-09 06:59:01 +00001046static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +00001047{
1048 int i;
1049
1050 printf("Available alarm timers, in order of precedence:\n");
1051 for (i = 0; alarm_timers[i].name; i++)
1052 printf("%s\n", alarm_timers[i].name);
1053}
1054
1055static void configure_alarms(char const *opt)
1056{
1057 int i;
1058 int cur = 0;
1059 int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1;
1060 char *arg;
1061 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +00001062 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +00001063
aurel323adda042008-03-09 23:43:49 +00001064 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +00001065 show_available_alarms();
1066 exit(0);
1067 }
1068
1069 arg = strdup(opt);
1070
1071 /* Reorder the array */
1072 name = strtok(arg, ",");
1073 while (name) {
balroge2b577e2007-09-17 21:25:20 +00001074 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +00001075 if (!strcmp(alarm_timers[i].name, name))
1076 break;
1077 }
1078
1079 if (i == count) {
1080 fprintf(stderr, "Unknown clock %s\n", name);
1081 goto next;
1082 }
1083
1084 if (i < cur)
1085 /* Ignore */
1086 goto next;
1087
1088 /* Swap */
1089 tmp = alarm_timers[i];
1090 alarm_timers[i] = alarm_timers[cur];
1091 alarm_timers[cur] = tmp;
1092
1093 cur++;
1094next:
1095 name = strtok(NULL, ",");
1096 }
1097
1098 free(arg);
1099
1100 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +00001101 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +00001102 for (i = cur; i < count; i++)
1103 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +00001104 } else {
1105 show_available_alarms();
1106 exit(1);
thsf3dcfad2007-08-24 01:26:02 +00001107 }
thsf3dcfad2007-08-24 01:26:02 +00001108}
1109
bellard8a7ddc32004-03-31 19:00:16 +00001110QEMUClock *rt_clock;
1111QEMUClock *vm_clock;
1112
1113static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +00001114
pbrook9596ebb2007-11-18 01:44:38 +00001115static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001116{
1117 QEMUClock *clock;
1118 clock = qemu_mallocz(sizeof(QEMUClock));
1119 if (!clock)
1120 return NULL;
1121 clock->type = type;
1122 return clock;
1123}
1124
1125QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1126{
1127 QEMUTimer *ts;
1128
1129 ts = qemu_mallocz(sizeof(QEMUTimer));
1130 ts->clock = clock;
1131 ts->cb = cb;
1132 ts->opaque = opaque;
1133 return ts;
1134}
1135
1136void qemu_free_timer(QEMUTimer *ts)
1137{
1138 qemu_free(ts);
1139}
1140
1141/* stop a timer, but do not dealloc it */
1142void qemu_del_timer(QEMUTimer *ts)
1143{
1144 QEMUTimer **pt, *t;
1145
1146 /* NOTE: this code must be signal safe because
1147 qemu_timer_expired() can be called from a signal. */
1148 pt = &active_timers[ts->clock->type];
1149 for(;;) {
1150 t = *pt;
1151 if (!t)
1152 break;
1153 if (t == ts) {
1154 *pt = t->next;
1155 break;
1156 }
1157 pt = &t->next;
1158 }
1159}
1160
1161/* modify the current timer so that it will be fired when current_time
1162 >= expire_time. The corresponding callback will be called. */
1163void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1164{
1165 QEMUTimer **pt, *t;
1166
1167 qemu_del_timer(ts);
1168
1169 /* add the timer in the sorted list */
1170 /* NOTE: this code must be signal safe because
1171 qemu_timer_expired() can be called from a signal. */
1172 pt = &active_timers[ts->clock->type];
1173 for(;;) {
1174 t = *pt;
1175 if (!t)
1176 break;
ths5fafdf22007-09-16 21:08:06 +00001177 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001178 break;
1179 pt = &t->next;
1180 }
1181 ts->expire_time = expire_time;
1182 ts->next = *pt;
1183 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001184
1185 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001186 if (pt == &active_timers[ts->clock->type]) {
1187 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1188 qemu_rearm_alarm_timer(alarm_timer);
1189 }
1190 /* Interrupt execution to force deadline recalculation. */
1191 if (use_icount && cpu_single_env) {
1192 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1193 }
1194 }
bellard8a7ddc32004-03-31 19:00:16 +00001195}
1196
1197int qemu_timer_pending(QEMUTimer *ts)
1198{
1199 QEMUTimer *t;
1200 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1201 if (t == ts)
1202 return 1;
1203 }
1204 return 0;
1205}
1206
1207static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1208{
1209 if (!timer_head)
1210 return 0;
1211 return (timer_head->expire_time <= current_time);
1212}
1213
1214static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1215{
1216 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001217
bellard8a7ddc32004-03-31 19:00:16 +00001218 for(;;) {
1219 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001220 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001221 break;
1222 /* remove timer from the list before calling the callback */
1223 *ptimer_head = ts->next;
1224 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001225
bellard8a7ddc32004-03-31 19:00:16 +00001226 /* run the callback (the timer list can be modified) */
1227 ts->cb(ts->opaque);
1228 }
1229}
1230
1231int64_t qemu_get_clock(QEMUClock *clock)
1232{
1233 switch(clock->type) {
1234 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001235 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001236 default:
1237 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001238 if (use_icount) {
1239 return cpu_get_icount();
1240 } else {
1241 return cpu_get_clock();
1242 }
bellard8a7ddc32004-03-31 19:00:16 +00001243 }
1244}
1245
bellard1dce7c32006-07-13 23:20:22 +00001246static void init_timers(void)
1247{
1248 init_get_clock();
1249 ticks_per_sec = QEMU_TIMER_BASE;
1250 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1251 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1252}
1253
bellard8a7ddc32004-03-31 19:00:16 +00001254/* save a timer */
1255void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1256{
1257 uint64_t expire_time;
1258
1259 if (qemu_timer_pending(ts)) {
1260 expire_time = ts->expire_time;
1261 } else {
1262 expire_time = -1;
1263 }
1264 qemu_put_be64(f, expire_time);
1265}
1266
1267void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1268{
1269 uint64_t expire_time;
1270
1271 expire_time = qemu_get_be64(f);
1272 if (expire_time != -1) {
1273 qemu_mod_timer(ts, expire_time);
1274 } else {
1275 qemu_del_timer(ts);
1276 }
1277}
1278
1279static void timer_save(QEMUFile *f, void *opaque)
1280{
1281 if (cpu_ticks_enabled) {
1282 hw_error("cannot save state if virtual timers are running");
1283 }
thsbee8d682007-12-16 23:41:11 +00001284 qemu_put_be64(f, cpu_ticks_offset);
1285 qemu_put_be64(f, ticks_per_sec);
1286 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001287}
1288
1289static int timer_load(QEMUFile *f, void *opaque, int version_id)
1290{
bellardc88676f2006-08-06 13:36:11 +00001291 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001292 return -EINVAL;
1293 if (cpu_ticks_enabled) {
1294 return -EINVAL;
1295 }
thsbee8d682007-12-16 23:41:11 +00001296 cpu_ticks_offset=qemu_get_be64(f);
1297 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001298 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001299 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001300 }
bellard8a7ddc32004-03-31 19:00:16 +00001301 return 0;
1302}
1303
bellard67b915a2004-03-31 23:37:16 +00001304#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001305void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001306 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1307#else
bellard8a7ddc32004-03-31 19:00:16 +00001308static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001309#endif
bellard8a7ddc32004-03-31 19:00:16 +00001310{
bellard02ba45c2004-06-25 14:46:23 +00001311#if 0
1312#define DISP_FREQ 1000
1313 {
1314 static int64_t delta_min = INT64_MAX;
1315 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1316 static int count;
1317 ti = qemu_get_clock(vm_clock);
1318 if (last_clock != 0) {
1319 delta = ti - last_clock;
1320 if (delta < delta_min)
1321 delta_min = delta;
1322 if (delta > delta_max)
1323 delta_max = delta;
1324 delta_cum += delta;
1325 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001326 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001327 muldiv64(delta_min, 1000000, ticks_per_sec),
1328 muldiv64(delta_max, 1000000, ticks_per_sec),
1329 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1330 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1331 count = 0;
1332 delta_min = INT64_MAX;
1333 delta_max = 0;
1334 delta_cum = 0;
1335 }
1336 }
1337 last_clock = ti;
1338 }
1339#endif
thsefe75412007-08-24 01:36:32 +00001340 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001341 (!use_icount &&
1342 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1343 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001344 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1345 qemu_get_clock(rt_clock))) {
aliguoric96f1a42008-11-05 20:29:45 +00001346 CPUState *env = next_cpu;
aliguoric96f1a42008-11-05 20:29:45 +00001347
bellard06d9f2f2006-05-01 13:23:04 +00001348#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001349 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1350 SetEvent(data->host_alarm);
aliguorif49e58d2008-11-05 21:22:34 +00001351#else
1352 static const char byte = 0;
aliguoric96f1a42008-11-05 20:29:45 +00001353 write(alarm_timer_wfd, &byte, sizeof(byte));
aliguorif49e58d2008-11-05 21:22:34 +00001354#endif
balrogd5d08332008-01-05 19:41:47 +00001355 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1356
balrog4f8eb8d2007-12-16 12:39:38 +00001357 if (env) {
1358 /* stop the currently executing cpu because a timer occured */
1359 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
bellarda332e112005-09-03 17:55:47 +00001360#ifdef USE_KQEMU
balrog4f8eb8d2007-12-16 12:39:38 +00001361 if (env->kqemu_enabled) {
1362 kqemu_cpu_interrupt(env);
1363 }
balrogee5605e2007-12-03 03:01:40 +00001364#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001365 }
balrogee5605e2007-12-03 03:01:40 +00001366 event_pending = 1;
bellard8a7ddc32004-03-31 19:00:16 +00001367 }
1368}
1369
pbrook2e70f6e2008-06-29 01:03:05 +00001370static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001371{
pbrook2e70f6e2008-06-29 01:03:05 +00001372 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001373
1374 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001375 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1376 qemu_get_clock(vm_clock);
1377 } else {
1378 /* To avoid problems with overflow limit this to 2^32. */
1379 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001380 }
1381
pbrook2e70f6e2008-06-29 01:03:05 +00001382 if (delta < 0)
1383 delta = 0;
thsefe75412007-08-24 01:36:32 +00001384
pbrook2e70f6e2008-06-29 01:03:05 +00001385 return delta;
1386}
1387
blueswir18632fb92008-09-14 13:59:34 +00001388#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001389static uint64_t qemu_next_deadline_dyntick(void)
1390{
1391 int64_t delta;
1392 int64_t rtdelta;
1393
1394 if (use_icount)
1395 delta = INT32_MAX;
1396 else
1397 delta = (qemu_next_deadline() + 999) / 1000;
1398
1399 if (active_timers[QEMU_TIMER_REALTIME]) {
1400 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1401 qemu_get_clock(rt_clock))*1000;
1402 if (rtdelta < delta)
1403 delta = rtdelta;
1404 }
1405
1406 if (delta < MIN_TIMER_REARM_US)
1407 delta = MIN_TIMER_REARM_US;
1408
1409 return delta;
thsefe75412007-08-24 01:36:32 +00001410}
blueswir18632fb92008-09-14 13:59:34 +00001411#endif
thsefe75412007-08-24 01:36:32 +00001412
bellardfd872592004-05-12 19:11:15 +00001413#ifndef _WIN32
1414
aliguori7183b4b2008-11-05 20:40:18 +00001415/* Sets a specific flag */
1416static int fcntl_setfl(int fd, int flag)
1417{
1418 int flags;
1419
1420 flags = fcntl(fd, F_GETFL);
1421 if (flags == -1)
1422 return -errno;
1423
1424 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1425 return -errno;
1426
1427 return 0;
1428}
1429
bellard829309c2004-05-20 13:20:12 +00001430#if defined(__linux__)
1431
bellardfd872592004-05-12 19:11:15 +00001432#define RTC_FREQ 1024
1433
aurel32de9a95f2008-11-11 13:41:01 +00001434static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001435{
thsc8994012007-08-19 21:56:03 +00001436 struct sigaction act;
1437
1438 /* timer signal */
1439 sigfillset(&act.sa_mask);
1440 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001441 act.sa_handler = host_alarm_handler;
1442
1443 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001444 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001445 fcntl(fd, F_SETOWN, getpid());
1446}
1447
thsc40ec5a2007-08-19 22:09:40 +00001448static int hpet_start_timer(struct qemu_alarm_timer *t)
1449{
1450 struct hpet_info info;
1451 int r, fd;
1452
1453 fd = open("/dev/hpet", O_RDONLY);
1454 if (fd < 0)
1455 return -1;
1456
1457 /* Set frequency */
1458 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1459 if (r < 0) {
1460 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1461 "error, but for better emulation accuracy type:\n"
1462 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1463 goto fail;
1464 }
1465
1466 /* Check capabilities */
1467 r = ioctl(fd, HPET_INFO, &info);
1468 if (r < 0)
1469 goto fail;
1470
1471 /* Enable periodic mode */
1472 r = ioctl(fd, HPET_EPI, 0);
1473 if (info.hi_flags && (r < 0))
1474 goto fail;
1475
1476 /* Enable interrupt */
1477 r = ioctl(fd, HPET_IE_ON, 0);
1478 if (r < 0)
1479 goto fail;
1480
1481 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001482 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001483
1484 return 0;
1485fail:
1486 close(fd);
1487 return -1;
1488}
1489
1490static void hpet_stop_timer(struct qemu_alarm_timer *t)
1491{
pbrookfcdc2122007-08-23 20:22:22 +00001492 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001493
1494 close(fd);
1495}
1496
thsc8994012007-08-19 21:56:03 +00001497static int rtc_start_timer(struct qemu_alarm_timer *t)
1498{
1499 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001500 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001501
balrogaeb30be2007-07-02 15:03:13 +00001502 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001503 if (rtc_fd < 0)
1504 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001505 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1506 if (current_rtc_freq != RTC_FREQ &&
1507 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001508 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1509 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1510 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1511 goto fail;
1512 }
1513 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1514 fail:
1515 close(rtc_fd);
1516 return -1;
1517 }
thsc8994012007-08-19 21:56:03 +00001518
1519 enable_sigio_timer(rtc_fd);
1520
pbrookfcdc2122007-08-23 20:22:22 +00001521 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001522
bellardfd872592004-05-12 19:11:15 +00001523 return 0;
1524}
1525
thsc8994012007-08-19 21:56:03 +00001526static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001527{
pbrookfcdc2122007-08-23 20:22:22 +00001528 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001529
1530 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001531}
1532
thsefe75412007-08-24 01:36:32 +00001533static int dynticks_start_timer(struct qemu_alarm_timer *t)
1534{
1535 struct sigevent ev;
1536 timer_t host_timer;
1537 struct sigaction act;
1538
1539 sigfillset(&act.sa_mask);
1540 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001541 act.sa_handler = host_alarm_handler;
1542
1543 sigaction(SIGALRM, &act, NULL);
1544
1545 ev.sigev_value.sival_int = 0;
1546 ev.sigev_notify = SIGEV_SIGNAL;
1547 ev.sigev_signo = SIGALRM;
1548
1549 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1550 perror("timer_create");
1551
1552 /* disable dynticks */
1553 fprintf(stderr, "Dynamic Ticks disabled\n");
1554
1555 return -1;
1556 }
1557
blueswir10399bfe2008-11-16 11:37:18 +00001558 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001559
1560 return 0;
1561}
1562
1563static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1564{
blueswir10399bfe2008-11-16 11:37:18 +00001565 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001566
1567 timer_delete(host_timer);
1568}
1569
1570static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1571{
blueswir10399bfe2008-11-16 11:37:18 +00001572 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001573 struct itimerspec timeout;
1574 int64_t nearest_delta_us = INT64_MAX;
1575 int64_t current_us;
1576
1577 if (!active_timers[QEMU_TIMER_REALTIME] &&
1578 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001579 return;
thsefe75412007-08-24 01:36:32 +00001580
pbrook2e70f6e2008-06-29 01:03:05 +00001581 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001582
1583 /* check whether a timer is already running */
1584 if (timer_gettime(host_timer, &timeout)) {
1585 perror("gettime");
1586 fprintf(stderr, "Internal timer error: aborting\n");
1587 exit(1);
1588 }
1589 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1590 if (current_us && current_us <= nearest_delta_us)
1591 return;
1592
1593 timeout.it_interval.tv_sec = 0;
1594 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1595 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1596 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1597 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1598 perror("settime");
1599 fprintf(stderr, "Internal timer error: aborting\n");
1600 exit(1);
1601 }
1602}
1603
ths70744b32007-08-26 17:31:30 +00001604#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001605
thsc8994012007-08-19 21:56:03 +00001606static int unix_start_timer(struct qemu_alarm_timer *t)
1607{
1608 struct sigaction act;
1609 struct itimerval itv;
1610 int err;
1611
1612 /* timer signal */
1613 sigfillset(&act.sa_mask);
1614 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001615 act.sa_handler = host_alarm_handler;
1616
1617 sigaction(SIGALRM, &act, NULL);
1618
1619 itv.it_interval.tv_sec = 0;
1620 /* for i386 kernel 2.6 to get 1 ms */
1621 itv.it_interval.tv_usec = 999;
1622 itv.it_value.tv_sec = 0;
1623 itv.it_value.tv_usec = 10 * 1000;
1624
1625 err = setitimer(ITIMER_REAL, &itv, NULL);
1626 if (err)
1627 return -1;
1628
1629 return 0;
1630}
1631
1632static void unix_stop_timer(struct qemu_alarm_timer *t)
1633{
1634 struct itimerval itv;
1635
1636 memset(&itv, 0, sizeof(itv));
1637 setitimer(ITIMER_REAL, &itv, NULL);
1638}
1639
bellard829309c2004-05-20 13:20:12 +00001640#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001641
aliguorif49e58d2008-11-05 21:22:34 +00001642static void try_to_rearm_timer(void *opaque)
1643{
1644 struct qemu_alarm_timer *t = opaque;
1645#ifndef _WIN32
1646 ssize_t len;
1647
1648 /* Drain the notify pipe */
1649 do {
1650 char buffer[512];
1651 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1652 } while ((len == -1 && errno == EINTR) || len > 0);
1653#endif
1654
aliguorif49e58d2008-11-05 21:22:34 +00001655 if (t->flags & ALARM_FLAG_EXPIRED) {
1656 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1657 qemu_rearm_alarm_timer(alarm_timer);
1658 }
1659}
1660
thsc8994012007-08-19 21:56:03 +00001661#ifdef _WIN32
1662
1663static int win32_start_timer(struct qemu_alarm_timer *t)
1664{
1665 TIMECAPS tc;
1666 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001667 UINT flags;
thsc8994012007-08-19 21:56:03 +00001668
1669 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1670 if (!data->host_alarm) {
1671 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001672 return -1;
thsc8994012007-08-19 21:56:03 +00001673 }
1674
1675 memset(&tc, 0, sizeof(tc));
1676 timeGetDevCaps(&tc, sizeof(tc));
1677
1678 if (data->period < tc.wPeriodMin)
1679 data->period = tc.wPeriodMin;
1680
1681 timeBeginPeriod(data->period);
1682
thsefe75412007-08-24 01:36:32 +00001683 flags = TIME_CALLBACK_FUNCTION;
1684 if (alarm_has_dynticks(t))
1685 flags |= TIME_ONESHOT;
1686 else
1687 flags |= TIME_PERIODIC;
1688
thsc8994012007-08-19 21:56:03 +00001689 data->timerId = timeSetEvent(1, // interval (ms)
1690 data->period, // resolution
1691 host_alarm_handler, // function
1692 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001693 flags);
thsc8994012007-08-19 21:56:03 +00001694
1695 if (!data->timerId) {
1696 perror("Failed to initialize win32 alarm timer");
1697
1698 timeEndPeriod(data->period);
1699 CloseHandle(data->host_alarm);
1700 return -1;
1701 }
1702
aliguorif49e58d2008-11-05 21:22:34 +00001703 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
thsc8994012007-08-19 21:56:03 +00001704
1705 return 0;
1706}
1707
1708static void win32_stop_timer(struct qemu_alarm_timer *t)
1709{
1710 struct qemu_alarm_win32 *data = t->priv;
1711
1712 timeKillEvent(data->timerId);
1713 timeEndPeriod(data->period);
1714
1715 CloseHandle(data->host_alarm);
1716}
1717
thsefe75412007-08-24 01:36:32 +00001718static void win32_rearm_timer(struct qemu_alarm_timer *t)
1719{
1720 struct qemu_alarm_win32 *data = t->priv;
1721 uint64_t nearest_delta_us;
1722
1723 if (!active_timers[QEMU_TIMER_REALTIME] &&
1724 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001725 return;
thsefe75412007-08-24 01:36:32 +00001726
pbrook2e70f6e2008-06-29 01:03:05 +00001727 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001728 nearest_delta_us /= 1000;
1729
1730 timeKillEvent(data->timerId);
1731
1732 data->timerId = timeSetEvent(1,
1733 data->period,
1734 host_alarm_handler,
1735 (DWORD)t,
1736 TIME_ONESHOT | TIME_PERIODIC);
1737
1738 if (!data->timerId) {
1739 perror("Failed to re-arm win32 alarm timer");
1740
1741 timeEndPeriod(data->period);
1742 CloseHandle(data->host_alarm);
1743 exit(1);
1744 }
1745}
1746
thsc8994012007-08-19 21:56:03 +00001747#endif /* _WIN32 */
1748
aliguori7183b4b2008-11-05 20:40:18 +00001749static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001750{
blueswir1223f0d72008-09-30 18:12:18 +00001751 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001752 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001753
1754#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +00001755 int fds[2];
1756
aliguori7183b4b2008-11-05 20:40:18 +00001757 err = pipe(fds);
1758 if (err == -1)
1759 return -errno;
1760
1761 err = fcntl_setfl(fds[0], O_NONBLOCK);
1762 if (err < 0)
1763 goto fail;
1764
1765 err = fcntl_setfl(fds[1], O_NONBLOCK);
1766 if (err < 0)
1767 goto fail;
1768
aliguoric96f1a42008-11-05 20:29:45 +00001769 alarm_timer_rfd = fds[0];
1770 alarm_timer_wfd = fds[1];
aliguorif49e58d2008-11-05 21:22:34 +00001771#endif
bellard06d9f2f2006-05-01 13:23:04 +00001772
thsc8994012007-08-19 21:56:03 +00001773 for (i = 0; alarm_timers[i].name; i++) {
1774 t = &alarm_timers[i];
1775
thsc8994012007-08-19 21:56:03 +00001776 err = t->start(t);
1777 if (!err)
1778 break;
bellard67b915a2004-03-31 23:37:16 +00001779 }
bellardfd872592004-05-12 19:11:15 +00001780
thsc8994012007-08-19 21:56:03 +00001781 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001782 err = -ENOENT;
1783 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001784 }
thsc8994012007-08-19 21:56:03 +00001785
aliguorif49e58d2008-11-05 21:22:34 +00001786#ifndef _WIN32
aliguori6abfbd72008-11-05 20:49:37 +00001787 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1788 try_to_rearm_timer, NULL, t);
aliguorif49e58d2008-11-05 21:22:34 +00001789#endif
aliguori6abfbd72008-11-05 20:49:37 +00001790
thsc8994012007-08-19 21:56:03 +00001791 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001792
aliguori6abfbd72008-11-05 20:49:37 +00001793 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001794
1795fail:
aliguorif49e58d2008-11-05 21:22:34 +00001796#ifndef _WIN32
aliguori7183b4b2008-11-05 20:40:18 +00001797 close(fds[0]);
1798 close(fds[1]);
aliguorif49e58d2008-11-05 21:22:34 +00001799#endif
aliguori7183b4b2008-11-05 20:40:18 +00001800 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001801}
1802
pbrook9596ebb2007-11-18 01:44:38 +00001803static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001804{
thsc8994012007-08-19 21:56:03 +00001805 alarm_timer->stop(alarm_timer);
1806 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001807}
1808
bellardc4b1fcc2004-03-14 21:44:30 +00001809/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001810/* host time/date access */
1811void qemu_get_timedate(struct tm *tm, int offset)
1812{
1813 time_t ti;
1814 struct tm *ret;
1815
1816 time(&ti);
1817 ti += offset;
1818 if (rtc_date_offset == -1) {
1819 if (rtc_utc)
1820 ret = gmtime(&ti);
1821 else
1822 ret = localtime(&ti);
1823 } else {
1824 ti -= rtc_date_offset;
1825 ret = gmtime(&ti);
1826 }
1827
1828 memcpy(tm, ret, sizeof(struct tm));
1829}
1830
1831int qemu_timedate_diff(struct tm *tm)
1832{
1833 time_t seconds;
1834
1835 if (rtc_date_offset == -1)
1836 if (rtc_utc)
1837 seconds = mktimegm(tm);
1838 else
1839 seconds = mktime(tm);
1840 else
1841 seconds = mktimegm(tm) + rtc_date_offset;
1842
1843 return seconds - time(NULL);
1844}
1845
bellardfd1dff42006-02-01 21:29:26 +00001846#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001847static void socket_cleanup(void)
1848{
1849 WSACleanup();
1850}
bellard82c643f2004-07-14 17:28:13 +00001851
bellardfd1dff42006-02-01 21:29:26 +00001852static int socket_init(void)
1853{
1854 WSADATA Data;
1855 int ret, err;
1856
1857 ret = WSAStartup(MAKEWORD(2,2), &Data);
1858 if (ret != 0) {
1859 err = WSAGetLastError();
1860 fprintf(stderr, "WSAStartup: %d\n", err);
1861 return -1;
1862 }
1863 atexit(socket_cleanup);
1864 return 0;
1865}
aurel3264b7b732008-05-05 10:05:31 +00001866#endif
1867
aliguori63a01ef2008-10-31 19:10:00 +00001868const char *get_opt_name(char *buf, int buf_size, const char *p)
thse4bcb142007-12-02 04:51:10 +00001869{
1870 char *q;
thse4bcb142007-12-02 04:51:10 +00001871
balrog609497a2008-01-14 02:56:53 +00001872 q = buf;
1873 while (*p != '\0' && *p != '=') {
1874 if (q && (q - buf) < buf_size - 1)
1875 *q++ = *p;
1876 p++;
1877 }
1878 if (q)
1879 *q = '\0';
1880
1881 return p;
1882}
1883
aliguori63a01ef2008-10-31 19:10:00 +00001884const char *get_opt_value(char *buf, int buf_size, const char *p)
balrog609497a2008-01-14 02:56:53 +00001885{
1886 char *q;
1887
thse4bcb142007-12-02 04:51:10 +00001888 q = buf;
1889 while (*p != '\0') {
balrog609497a2008-01-14 02:56:53 +00001890 if (*p == ',') {
1891 if (*(p + 1) != ',')
thse4bcb142007-12-02 04:51:10 +00001892 break;
thse4bcb142007-12-02 04:51:10 +00001893 p++;
balrog609497a2008-01-14 02:56:53 +00001894 }
thse4bcb142007-12-02 04:51:10 +00001895 if (q && (q - buf) < buf_size - 1)
1896 *q++ = *p;
1897 p++;
1898 }
1899 if (q)
1900 *q = '\0';
1901
1902 return p;
1903}
1904
aliguori63a01ef2008-10-31 19:10:00 +00001905int get_param_value(char *buf, int buf_size,
1906 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001907{
1908 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001909 char option[128];
1910
1911 p = str;
1912 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001913 p = get_opt_name(option, sizeof(option), p);
bellard7c9d8e02005-11-15 22:16:05 +00001914 if (*p != '=')
1915 break;
1916 p++;
1917 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001918 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001919 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001920 } else {
balrog609497a2008-01-14 02:56:53 +00001921 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001922 }
1923 if (*p != ',')
1924 break;
1925 p++;
1926 }
1927 return 0;
1928}
1929
aliguori63a01ef2008-10-31 19:10:00 +00001930int check_params(char *buf, int buf_size,
1931 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001932{
1933 const char *p;
1934 int i;
1935
1936 p = str;
1937 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001938 p = get_opt_name(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001939 if (*p != '=')
1940 return -1;
1941 p++;
1942 for(i = 0; params[i] != NULL; i++)
1943 if (!strcmp(params[i], buf))
1944 break;
1945 if (params[i] == NULL)
1946 return -1;
balrog609497a2008-01-14 02:56:53 +00001947 p = get_opt_value(NULL, 0, p);
thse4bcb142007-12-02 04:51:10 +00001948 if (*p != ',')
1949 break;
1950 p++;
1951 }
1952 return 0;
1953}
1954
balrog1ae26a12008-09-28 23:19:47 +00001955/***********************************************************/
1956/* Bluetooth support */
1957static int nb_hcis;
1958static int cur_hci;
1959static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001960
balrog1ae26a12008-09-28 23:19:47 +00001961static struct bt_vlan_s {
1962 struct bt_scatternet_s net;
1963 int id;
1964 struct bt_vlan_s *next;
1965} *first_bt_vlan;
1966
1967/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001968static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001969{
1970 struct bt_vlan_s **pvlan, *vlan;
1971 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1972 if (vlan->id == id)
1973 return &vlan->net;
1974 }
1975 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1976 vlan->id = id;
1977 pvlan = &first_bt_vlan;
1978 while (*pvlan != NULL)
1979 pvlan = &(*pvlan)->next;
1980 *pvlan = vlan;
1981 return &vlan->net;
1982}
1983
1984static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1985{
1986}
1987
1988static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1989{
1990 return -ENOTSUP;
1991}
1992
1993static struct HCIInfo null_hci = {
1994 .cmd_send = null_hci_send,
1995 .sco_send = null_hci_send,
1996 .acl_send = null_hci_send,
1997 .bdaddr_set = null_hci_addr_set,
1998};
1999
2000struct HCIInfo *qemu_next_hci(void)
2001{
2002 if (cur_hci == nb_hcis)
2003 return &null_hci;
2004
2005 return hci_table[cur_hci++];
2006}
2007
balrogdc72ac12008-11-09 00:04:26 +00002008static struct HCIInfo *hci_init(const char *str)
2009{
2010 char *endp;
2011 struct bt_scatternet_s *vlan = 0;
2012
2013 if (!strcmp(str, "null"))
2014 /* null */
2015 return &null_hci;
2016 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
2017 /* host[:hciN] */
2018 return bt_host_hci(str[4] ? str + 5 : "hci0");
2019 else if (!strncmp(str, "hci", 3)) {
2020 /* hci[,vlan=n] */
2021 if (str[3]) {
2022 if (!strncmp(str + 3, ",vlan=", 6)) {
2023 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2024 if (*endp)
2025 vlan = 0;
2026 }
2027 } else
2028 vlan = qemu_find_bt_vlan(0);
2029 if (vlan)
2030 return bt_new_hci(vlan);
2031 }
2032
2033 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2034
2035 return 0;
2036}
2037
2038static int bt_hci_parse(const char *str)
2039{
2040 struct HCIInfo *hci;
2041 bdaddr_t bdaddr;
2042
2043 if (nb_hcis >= MAX_NICS) {
2044 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2045 return -1;
2046 }
2047
2048 hci = hci_init(str);
2049 if (!hci)
2050 return -1;
2051
2052 bdaddr.b[0] = 0x52;
2053 bdaddr.b[1] = 0x54;
2054 bdaddr.b[2] = 0x00;
2055 bdaddr.b[3] = 0x12;
2056 bdaddr.b[4] = 0x34;
2057 bdaddr.b[5] = 0x56 + nb_hcis;
2058 hci->bdaddr_set(hci, bdaddr.b);
2059
2060 hci_table[nb_hcis++] = hci;
2061
2062 return 0;
2063}
2064
2065static void bt_vhci_add(int vlan_id)
2066{
2067 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2068
2069 if (!vlan->slave)
2070 fprintf(stderr, "qemu: warning: adding a VHCI to "
2071 "an empty scatternet %i\n", vlan_id);
2072
2073 bt_vhci_init(bt_new_hci(vlan));
2074}
2075
2076static struct bt_device_s *bt_device_add(const char *opt)
2077{
2078 struct bt_scatternet_s *vlan;
2079 int vlan_id = 0;
2080 char *endp = strstr(opt, ",vlan=");
2081 int len = (endp ? endp - opt : strlen(opt)) + 1;
2082 char devname[10];
2083
2084 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2085
2086 if (endp) {
2087 vlan_id = strtol(endp + 6, &endp, 0);
2088 if (*endp) {
2089 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2090 return 0;
2091 }
2092 }
2093
2094 vlan = qemu_find_bt_vlan(vlan_id);
2095
2096 if (!vlan->slave)
2097 fprintf(stderr, "qemu: warning: adding a slave device to "
2098 "an empty scatternet %i\n", vlan_id);
2099
2100 if (!strcmp(devname, "keyboard"))
2101 return bt_keyboard_init(vlan);
2102
2103 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2104 return 0;
2105}
2106
2107static int bt_parse(const char *opt)
2108{
2109 const char *endp, *p;
2110 int vlan;
2111
2112 if (strstart(opt, "hci", &endp)) {
2113 if (!*endp || *endp == ',') {
2114 if (*endp)
2115 if (!strstart(endp, ",vlan=", 0))
2116 opt = endp + 1;
2117
2118 return bt_hci_parse(opt);
2119 }
2120 } else if (strstart(opt, "vhci", &endp)) {
2121 if (!*endp || *endp == ',') {
2122 if (*endp) {
2123 if (strstart(endp, ",vlan=", &p)) {
2124 vlan = strtol(p, (char **) &endp, 0);
2125 if (*endp) {
2126 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2127 return 1;
2128 }
2129 } else {
2130 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2131 return 1;
2132 }
2133 } else
2134 vlan = 0;
2135
2136 bt_vhci_add(vlan);
2137 return 0;
2138 }
2139 } else if (strstart(opt, "device:", &endp))
2140 return !bt_device_add(endp);
2141
2142 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2143 return 1;
2144}
2145
balrog1ae26a12008-09-28 23:19:47 +00002146/***********************************************************/
2147/* QEMU Block devices */
2148
balrog609497a2008-01-14 02:56:53 +00002149#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00002150#ifdef TARGET_PPC
2151#define CDROM_ALIAS "index=1,media=cdrom"
2152#else
2153#define CDROM_ALIAS "index=2,media=cdrom"
2154#endif
2155#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00002156#define PFLASH_ALIAS "if=pflash"
2157#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00002158#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002159
balrog609497a2008-01-14 02:56:53 +00002160static int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002161{
2162 va_list ap;
2163
2164 if (nb_drives_opt >= MAX_DRIVES) {
2165 fprintf(stderr, "qemu: too many drives\n");
2166 exit(1);
2167 }
2168
balrog609497a2008-01-14 02:56:53 +00002169 drives_opt[nb_drives_opt].file = file;
thse4bcb142007-12-02 04:51:10 +00002170 va_start(ap, fmt);
balrog609497a2008-01-14 02:56:53 +00002171 vsnprintf(drives_opt[nb_drives_opt].opt,
2172 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002173 va_end(ap);
2174
2175 return nb_drives_opt++;
2176}
2177
thsf60d39b2007-12-17 03:55:57 +00002178int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002179{
2180 int index;
2181
2182 /* seek interface, bus and unit */
2183
2184 for (index = 0; index < nb_drives; index++)
thsf60d39b2007-12-17 03:55:57 +00002185 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002186 drives_table[index].bus == bus &&
2187 drives_table[index].unit == unit)
2188 return index;
2189
2190 return -1;
2191}
2192
thsf60d39b2007-12-17 03:55:57 +00002193int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002194{
2195 int max_bus;
2196 int index;
2197
2198 max_bus = -1;
2199 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002200 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002201 drives_table[index].bus > max_bus)
2202 max_bus = drives_table[index].bus;
2203 }
2204 return max_bus;
2205}
2206
aurel32a1620fa2008-04-29 05:58:01 +00002207static void bdrv_format_print(void *opaque, const char *name)
2208{
2209 fprintf(stderr, " %s", name);
2210}
2211
balrog609497a2008-01-14 02:56:53 +00002212static int drive_init(struct drive_opt *arg, int snapshot,
2213 QEMUMachine *machine)
thse4bcb142007-12-02 04:51:10 +00002214{
2215 char buf[128];
2216 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002217 char devname[128];
2218 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002219 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002220 enum { MEDIA_DISK, MEDIA_CDROM } media;
2221 int bus_id, unit_id;
2222 int cyls, heads, secs, translation;
2223 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002224 BlockDriver *drv = NULL;
thse4bcb142007-12-02 04:51:10 +00002225 int max_devs;
2226 int index;
balrog33f00272007-12-24 14:33:24 +00002227 int cache;
2228 int bdrv_flags;
balrog609497a2008-01-14 02:56:53 +00002229 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002230 static const char * const params[] = { "bus", "unit", "if", "index",
2231 "cyls", "heads", "secs", "trans",
2232 "media", "snapshot", "file",
2233 "cache", "format", NULL };
thse4bcb142007-12-02 04:51:10 +00002234
2235 if (check_params(buf, sizeof(buf), params, str) < 0) {
balrogff993632008-02-10 13:21:25 +00002236 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
thse4bcb142007-12-02 04:51:10 +00002237 buf, str);
2238 return -1;
2239 }
2240
2241 file[0] = 0;
2242 cyls = heads = secs = 0;
2243 bus_id = 0;
2244 unit_id = -1;
2245 translation = BIOS_ATA_TRANSLATION_AUTO;
2246 index = -1;
aliguori4dc822d2008-12-04 21:39:21 +00002247 cache = 3;
thse4bcb142007-12-02 04:51:10 +00002248
blueswir1c9b1ae22008-09-28 18:55:17 +00002249 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002250 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002251 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002252 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002253 } else {
thsf60d39b2007-12-17 03:55:57 +00002254 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002255 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002256 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002257 }
2258 media = MEDIA_DISK;
2259
2260 /* extract parameters */
2261
2262 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2263 bus_id = strtol(buf, NULL, 0);
2264 if (bus_id < 0) {
2265 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2266 return -1;
2267 }
2268 }
2269
2270 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2271 unit_id = strtol(buf, NULL, 0);
2272 if (unit_id < 0) {
2273 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2274 return -1;
2275 }
2276 }
2277
2278 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002279 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002280 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002281 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002282 max_devs = MAX_IDE_DEVS;
2283 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002284 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002285 max_devs = MAX_SCSI_DEVS;
2286 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002287 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002288 max_devs = 0;
2289 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002290 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002291 max_devs = 0;
2292 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002293 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002294 max_devs = 0;
2295 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002296 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002297 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002298 } else if (!strcmp(buf, "virtio")) {
2299 type = IF_VIRTIO;
2300 max_devs = 0;
2301 } else {
thse4bcb142007-12-02 04:51:10 +00002302 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2303 return -1;
2304 }
2305 }
2306
2307 if (get_param_value(buf, sizeof(buf), "index", str)) {
2308 index = strtol(buf, NULL, 0);
2309 if (index < 0) {
2310 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2311 return -1;
2312 }
2313 }
2314
2315 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2316 cyls = strtol(buf, NULL, 0);
2317 }
2318
2319 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2320 heads = strtol(buf, NULL, 0);
2321 }
2322
2323 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2324 secs = strtol(buf, NULL, 0);
2325 }
2326
2327 if (cyls || heads || secs) {
2328 if (cyls < 1 || cyls > 16383) {
2329 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2330 return -1;
2331 }
2332 if (heads < 1 || heads > 16) {
2333 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2334 return -1;
2335 }
2336 if (secs < 1 || secs > 63) {
2337 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2338 return -1;
2339 }
2340 }
2341
2342 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2343 if (!cyls) {
2344 fprintf(stderr,
2345 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2346 str);
2347 return -1;
2348 }
2349 if (!strcmp(buf, "none"))
2350 translation = BIOS_ATA_TRANSLATION_NONE;
2351 else if (!strcmp(buf, "lba"))
2352 translation = BIOS_ATA_TRANSLATION_LBA;
2353 else if (!strcmp(buf, "auto"))
2354 translation = BIOS_ATA_TRANSLATION_AUTO;
2355 else {
2356 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2357 return -1;
2358 }
2359 }
2360
2361 if (get_param_value(buf, sizeof(buf), "media", str)) {
2362 if (!strcmp(buf, "disk")) {
2363 media = MEDIA_DISK;
2364 } else if (!strcmp(buf, "cdrom")) {
2365 if (cyls || secs || heads) {
2366 fprintf(stderr,
2367 "qemu: '%s' invalid physical CHS format\n", str);
2368 return -1;
2369 }
2370 media = MEDIA_CDROM;
2371 } else {
2372 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2373 return -1;
2374 }
2375 }
2376
2377 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2378 if (!strcmp(buf, "on"))
2379 snapshot = 1;
2380 else if (!strcmp(buf, "off"))
2381 snapshot = 0;
2382 else {
2383 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2384 return -1;
2385 }
2386 }
2387
balrog33f00272007-12-24 14:33:24 +00002388 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002389 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002390 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002391 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002392 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002393 else if (!strcmp(buf, "writeback"))
2394 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002395 else {
2396 fprintf(stderr, "qemu: invalid cache option\n");
2397 return -1;
2398 }
2399 }
2400
aurel321e72d3b2008-04-28 20:26:45 +00002401 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002402 if (strcmp(buf, "?") == 0) {
2403 fprintf(stderr, "qemu: Supported formats:");
2404 bdrv_iterate_format(bdrv_format_print, NULL);
2405 fprintf(stderr, "\n");
2406 return -1;
2407 }
aurel321e72d3b2008-04-28 20:26:45 +00002408 drv = bdrv_find_format(buf);
2409 if (!drv) {
2410 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2411 return -1;
2412 }
2413 }
2414
balrog609497a2008-01-14 02:56:53 +00002415 if (arg->file == NULL)
2416 get_param_value(file, sizeof(file), "file", str);
2417 else
2418 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002419
2420 /* compute bus and unit according index */
2421
2422 if (index != -1) {
2423 if (bus_id != 0 || unit_id != -1) {
2424 fprintf(stderr,
2425 "qemu: '%s' index cannot be used with bus and unit\n", str);
2426 return -1;
2427 }
2428 if (max_devs == 0)
2429 {
2430 unit_id = index;
2431 bus_id = 0;
2432 } else {
2433 unit_id = index % max_devs;
2434 bus_id = index / max_devs;
2435 }
2436 }
2437
2438 /* if user doesn't specify a unit_id,
2439 * try to find the first free
2440 */
2441
2442 if (unit_id == -1) {
2443 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002444 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002445 unit_id++;
2446 if (max_devs && unit_id >= max_devs) {
2447 unit_id -= max_devs;
2448 bus_id++;
2449 }
2450 }
2451 }
2452
2453 /* check unit id */
2454
2455 if (max_devs && unit_id >= max_devs) {
2456 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2457 str, unit_id, max_devs - 1);
2458 return -1;
2459 }
2460
2461 /*
2462 * ignore multiple definitions
2463 */
2464
thsf60d39b2007-12-17 03:55:57 +00002465 if (drive_get_index(type, bus_id, unit_id) != -1)
thse4bcb142007-12-02 04:51:10 +00002466 return 0;
2467
2468 /* init */
2469
thsf60d39b2007-12-17 03:55:57 +00002470 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002471 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002472 if (max_devs)
2473 snprintf(buf, sizeof(buf), "%s%i%s%i",
2474 devname, bus_id, mediastr, unit_id);
2475 else
2476 snprintf(buf, sizeof(buf), "%s%s%i",
2477 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002478 bdrv = bdrv_new(buf);
2479 drives_table[nb_drives].bdrv = bdrv;
thsf60d39b2007-12-17 03:55:57 +00002480 drives_table[nb_drives].type = type;
thse4bcb142007-12-02 04:51:10 +00002481 drives_table[nb_drives].bus = bus_id;
2482 drives_table[nb_drives].unit = unit_id;
2483 nb_drives++;
2484
thsf60d39b2007-12-17 03:55:57 +00002485 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002486 case IF_IDE:
2487 case IF_SCSI:
2488 switch(media) {
2489 case MEDIA_DISK:
2490 if (cyls != 0) {
2491 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2492 bdrv_set_translation_hint(bdrv, translation);
2493 }
2494 break;
2495 case MEDIA_CDROM:
2496 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2497 break;
2498 }
2499 break;
2500 case IF_SD:
2501 /* FIXME: This isn't really a floppy, but it's a reasonable
2502 approximation. */
2503 case IF_FLOPPY:
2504 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2505 break;
2506 case IF_PFLASH:
2507 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002508 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002509 break;
2510 }
2511 if (!file[0])
2512 return 0;
balrog33f00272007-12-24 14:33:24 +00002513 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002514 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002515 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002516 cache = 2; /* always use write-back with snapshot */
2517 }
2518 if (cache == 0) /* no caching */
2519 bdrv_flags |= BDRV_O_NOCACHE;
2520 else if (cache == 2) /* write-back */
2521 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori4dc822d2008-12-04 21:39:21 +00002522 else if (cache == 3) /* not specified */
2523 bdrv_flags |= BDRV_O_CACHE_DEF;
aliguori83ab7952008-08-19 14:44:22 +00002524 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) {
thse4bcb142007-12-02 04:51:10 +00002525 fprintf(stderr, "qemu: could not open disk image %s\n",
2526 file);
2527 return -1;
2528 }
2529 return 0;
2530}
2531
bellard330d0412003-07-26 18:11:40 +00002532/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002533/* USB devices */
2534
pbrook0d92ed32006-05-21 16:30:15 +00002535static USBPort *used_usb_ports;
2536static USBPort *free_usb_ports;
2537
2538/* ??? Maybe change this to register a hub to keep track of the topology. */
2539void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2540 usb_attachfn attach)
2541{
2542 port->opaque = opaque;
2543 port->index = index;
2544 port->attach = attach;
2545 port->next = free_usb_ports;
2546 free_usb_ports = port;
2547}
2548
aliguori4b096fc2008-08-21 19:28:55 +00002549int usb_device_add_dev(USBDevice *dev)
2550{
2551 USBPort *port;
2552
2553 /* Find a USB port to add the device to. */
2554 port = free_usb_ports;
2555 if (!port->next) {
2556 USBDevice *hub;
2557
2558 /* Create a new hub and chain it on. */
2559 free_usb_ports = NULL;
2560 port->next = used_usb_ports;
2561 used_usb_ports = port;
2562
2563 hub = usb_hub_init(VM_USB_HUB_SIZE);
2564 usb_attach(port, hub);
2565 port = free_usb_ports;
2566 }
2567
2568 free_usb_ports = port->next;
2569 port->next = used_usb_ports;
2570 used_usb_ports = port;
2571 usb_attach(port, dev);
2572 return 0;
2573}
2574
bellarda594cfb2005-11-06 16:13:29 +00002575static int usb_device_add(const char *devname)
2576{
2577 const char *p;
2578 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002579
pbrook0d92ed32006-05-21 16:30:15 +00002580 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002581 return -1;
2582
2583 if (strstart(devname, "host:", &p)) {
2584 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002585 } else if (!strcmp(devname, "mouse")) {
2586 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002587 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002588 dev = usb_tablet_init();
2589 } else if (!strcmp(devname, "keyboard")) {
2590 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002591 } else if (strstart(devname, "disk:", &p)) {
2592 dev = usb_msd_init(p);
balrogf6d2a312007-06-10 19:21:04 +00002593 } else if (!strcmp(devname, "wacom-tablet")) {
2594 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002595 } else if (strstart(devname, "serial:", &p)) {
2596 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002597#ifdef CONFIG_BRLAPI
2598 } else if (!strcmp(devname, "braille")) {
2599 dev = usb_baum_init();
2600#endif
balrog6c9f8862008-07-17 20:47:13 +00002601 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002602 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002603
balrog9ad97e62008-07-29 13:16:31 +00002604 if (net_client_init("nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002605 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002606 nd_table[nic].model = "usb";
2607 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002608 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2609 dev = usb_bt_init(devname[2] ? hci_init(p) :
2610 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002611 } else {
2612 return -1;
2613 }
pbrook0d92ed32006-05-21 16:30:15 +00002614 if (!dev)
2615 return -1;
2616
aliguori4b096fc2008-08-21 19:28:55 +00002617 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002618}
2619
aliguori1f3870a2008-08-21 19:27:48 +00002620int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002621{
pbrook0d92ed32006-05-21 16:30:15 +00002622 USBPort *port;
2623 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002624 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002625
pbrook0d92ed32006-05-21 16:30:15 +00002626 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002627 return -1;
2628
bellarda594cfb2005-11-06 16:13:29 +00002629 if (bus_num != 0)
2630 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002631
2632 lastp = &used_usb_ports;
2633 port = used_usb_ports;
2634 while (port && port->dev->addr != addr) {
2635 lastp = &port->next;
2636 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002637 }
pbrook0d92ed32006-05-21 16:30:15 +00002638
2639 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002640 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002641
bellard059809e2006-07-19 18:06:15 +00002642 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002643 *lastp = port->next;
2644 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002645 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002646 port->next = free_usb_ports;
2647 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002648 return 0;
2649}
2650
aliguori1f3870a2008-08-21 19:27:48 +00002651static int usb_device_del(const char *devname)
2652{
2653 int bus_num, addr;
2654 const char *p;
2655
aliguori5d0c5752008-09-14 01:07:41 +00002656 if (strstart(devname, "host:", &p))
2657 return usb_host_device_close(p);
2658
aliguori1f3870a2008-08-21 19:27:48 +00002659 if (!used_usb_ports)
2660 return -1;
2661
2662 p = strchr(devname, '.');
2663 if (!p)
2664 return -1;
2665 bus_num = strtoul(devname, NULL, 0);
2666 addr = strtoul(p + 1, NULL, 0);
2667
2668 return usb_device_del_addr(bus_num, addr);
2669}
2670
bellarda594cfb2005-11-06 16:13:29 +00002671void do_usb_add(const char *devname)
2672{
aliguori4b096fc2008-08-21 19:28:55 +00002673 usb_device_add(devname);
bellarda594cfb2005-11-06 16:13:29 +00002674}
2675
2676void do_usb_del(const char *devname)
2677{
aliguori4b096fc2008-08-21 19:28:55 +00002678 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002679}
2680
2681void usb_info(void)
2682{
2683 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002684 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002685 const char *speed_str;
2686
pbrook0d92ed32006-05-21 16:30:15 +00002687 if (!usb_enabled) {
bellarda594cfb2005-11-06 16:13:29 +00002688 term_printf("USB support not enabled\n");
2689 return;
2690 }
2691
pbrook0d92ed32006-05-21 16:30:15 +00002692 for (port = used_usb_ports; port; port = port->next) {
2693 dev = port->dev;
2694 if (!dev)
2695 continue;
2696 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002697 case USB_SPEED_LOW:
2698 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002699 break;
ths5fafdf22007-09-16 21:08:06 +00002700 case USB_SPEED_FULL:
2701 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002702 break;
ths5fafdf22007-09-16 21:08:06 +00002703 case USB_SPEED_HIGH:
2704 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002705 break;
2706 default:
ths5fafdf22007-09-16 21:08:06 +00002707 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002708 break;
bellarda594cfb2005-11-06 16:13:29 +00002709 }
ths5fafdf22007-09-16 21:08:06 +00002710 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
bellard1f6e24e2006-06-26 21:00:51 +00002711 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002712 }
2713}
2714
bellardf7cce892004-12-08 22:21:25 +00002715/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002716/* PCMCIA/Cardbus */
2717
2718static struct pcmcia_socket_entry_s {
2719 struct pcmcia_socket_s *socket;
2720 struct pcmcia_socket_entry_s *next;
2721} *pcmcia_sockets = 0;
2722
2723void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2724{
2725 struct pcmcia_socket_entry_s *entry;
2726
2727 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2728 entry->socket = socket;
2729 entry->next = pcmcia_sockets;
2730 pcmcia_sockets = entry;
2731}
2732
2733void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2734{
2735 struct pcmcia_socket_entry_s *entry, **ptr;
2736
2737 ptr = &pcmcia_sockets;
2738 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2739 if (entry->socket == socket) {
2740 *ptr = entry->next;
2741 qemu_free(entry);
2742 }
2743}
2744
2745void pcmcia_info(void)
2746{
2747 struct pcmcia_socket_entry_s *iter;
2748 if (!pcmcia_sockets)
2749 term_printf("No PCMCIA sockets\n");
2750
2751 for (iter = pcmcia_sockets; iter; iter = iter->next)
2752 term_printf("%s: %s\n", iter->socket->slot_string,
2753 iter->socket->attached ? iter->socket->card_string :
2754 "Empty");
2755}
2756
2757/***********************************************************/
ths2ff89792007-06-21 23:34:19 +00002758/* dumb display */
2759
2760static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
2761{
2762}
2763
2764static void dumb_resize(DisplayState *ds, int w, int h)
2765{
2766}
2767
ths2ff89792007-06-21 23:34:19 +00002768static void dumb_display_init(DisplayState *ds)
2769{
2770 ds->data = NULL;
2771 ds->linesize = 0;
2772 ds->depth = 0;
2773 ds->dpy_update = dumb_update;
2774 ds->dpy_resize = dumb_resize;
aliguori2ad1a432008-10-31 20:34:40 +00002775 ds->dpy_refresh = NULL;
2776 ds->gui_timer_interval = 0;
aliguoribcfad702008-08-21 20:08:55 +00002777 ds->idle = 1;
ths2ff89792007-06-21 23:34:19 +00002778}
2779
2780/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002781/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002782
bellardc4b1fcc2004-03-14 21:44:30 +00002783#define MAX_IO_HANDLERS 64
2784
2785typedef struct IOHandlerRecord {
2786 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002787 IOCanRWHandler *fd_read_poll;
2788 IOHandler *fd_read;
2789 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002790 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002791 void *opaque;
2792 /* temporary data */
2793 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002794 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002795} IOHandlerRecord;
2796
bellard8a7ddc32004-03-31 19:00:16 +00002797static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002798
bellard7c9d8e02005-11-15 22:16:05 +00002799/* XXX: fd_read_poll should be suppressed, but an API change is
2800 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002801int qemu_set_fd_handler2(int fd,
2802 IOCanRWHandler *fd_read_poll,
2803 IOHandler *fd_read,
2804 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002805 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002806{
bellard8a7ddc32004-03-31 19:00:16 +00002807 IOHandlerRecord **pioh, *ioh;
2808
bellard7c9d8e02005-11-15 22:16:05 +00002809 if (!fd_read && !fd_write) {
2810 pioh = &first_io_handler;
2811 for(;;) {
2812 ioh = *pioh;
2813 if (ioh == NULL)
2814 break;
2815 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002816 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002817 break;
2818 }
2819 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002820 }
bellard7c9d8e02005-11-15 22:16:05 +00002821 } else {
2822 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2823 if (ioh->fd == fd)
2824 goto found;
2825 }
2826 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2827 if (!ioh)
2828 return -1;
2829 ioh->next = first_io_handler;
2830 first_io_handler = ioh;
2831 found:
2832 ioh->fd = fd;
2833 ioh->fd_read_poll = fd_read_poll;
2834 ioh->fd_read = fd_read;
2835 ioh->fd_write = fd_write;
2836 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002837 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002838 }
bellard7c9d8e02005-11-15 22:16:05 +00002839 return 0;
2840}
2841
ths5fafdf22007-09-16 21:08:06 +00002842int qemu_set_fd_handler(int fd,
2843 IOHandler *fd_read,
2844 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002845 void *opaque)
2846{
2847 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002848}
2849
aliguori56f3a5d2008-10-31 18:07:17 +00002850#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002851/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002852/* Polling handling */
2853
2854typedef struct PollingEntry {
2855 PollingFunc *func;
2856 void *opaque;
2857 struct PollingEntry *next;
2858} PollingEntry;
2859
2860static PollingEntry *first_polling_entry;
2861
2862int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2863{
2864 PollingEntry **ppe, *pe;
2865 pe = qemu_mallocz(sizeof(PollingEntry));
2866 if (!pe)
2867 return -1;
2868 pe->func = func;
2869 pe->opaque = opaque;
2870 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2871 *ppe = pe;
2872 return 0;
2873}
2874
2875void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2876{
2877 PollingEntry **ppe, *pe;
2878 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2879 pe = *ppe;
2880 if (pe->func == func && pe->opaque == opaque) {
2881 *ppe = pe->next;
2882 qemu_free(pe);
2883 break;
2884 }
2885 }
2886}
2887
bellarda18e5242006-06-25 17:18:27 +00002888/***********************************************************/
2889/* Wait objects support */
2890typedef struct WaitObjects {
2891 int num;
2892 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2893 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2894 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2895} WaitObjects;
2896
2897static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002898
bellarda18e5242006-06-25 17:18:27 +00002899int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2900{
2901 WaitObjects *w = &wait_objects;
2902
2903 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2904 return -1;
2905 w->events[w->num] = handle;
2906 w->func[w->num] = func;
2907 w->opaque[w->num] = opaque;
2908 w->num++;
2909 return 0;
2910}
2911
2912void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2913{
2914 int i, found;
2915 WaitObjects *w = &wait_objects;
2916
2917 found = 0;
2918 for (i = 0; i < w->num; i++) {
2919 if (w->events[i] == handle)
2920 found = 1;
2921 if (found) {
2922 w->events[i] = w->events[i + 1];
2923 w->func[i] = w->func[i + 1];
2924 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002925 }
bellarda18e5242006-06-25 17:18:27 +00002926 }
2927 if (found)
2928 w->num--;
2929}
2930#endif
2931
bellard8a7ddc32004-03-31 19:00:16 +00002932/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002933/* ram save/restore */
2934
bellard8a7ddc32004-03-31 19:00:16 +00002935static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2936{
2937 int v;
2938
2939 v = qemu_get_byte(f);
2940 switch(v) {
2941 case 0:
2942 if (qemu_get_buffer(f, buf, len) != len)
2943 return -EIO;
2944 break;
2945 case 1:
2946 v = qemu_get_byte(f);
2947 memset(buf, v, len);
2948 break;
2949 default:
2950 return -EINVAL;
2951 }
aliguori871d2f02008-10-13 03:07:56 +00002952
2953 if (qemu_file_has_error(f))
2954 return -EIO;
2955
bellard8a7ddc32004-03-31 19:00:16 +00002956 return 0;
2957}
2958
bellardc88676f2006-08-06 13:36:11 +00002959static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00002960{
aurel3200f82b82008-04-27 21:12:55 +00002961 int ret;
2962 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00002963
bellard8a7ddc32004-03-31 19:00:16 +00002964 if (qemu_get_be32(f) != phys_ram_size)
2965 return -EINVAL;
2966 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2967 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2968 if (ret)
2969 return ret;
2970 }
2971 return 0;
2972}
2973
bellardc88676f2006-08-06 13:36:11 +00002974#define BDRV_HASH_BLOCK_SIZE 1024
2975#define IOBUF_SIZE 4096
2976#define RAM_CBLOCK_MAGIC 0xfabe
2977
bellardc88676f2006-08-06 13:36:11 +00002978typedef struct RamDecompressState {
2979 z_stream zstream;
2980 QEMUFile *f;
2981 uint8_t buf[IOBUF_SIZE];
2982} RamDecompressState;
2983
2984static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
2985{
2986 int ret;
2987 memset(s, 0, sizeof(*s));
2988 s->f = f;
2989 ret = inflateInit(&s->zstream);
2990 if (ret != Z_OK)
2991 return -1;
2992 return 0;
2993}
2994
2995static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
2996{
2997 int ret, clen;
2998
2999 s->zstream.avail_out = len;
3000 s->zstream.next_out = buf;
3001 while (s->zstream.avail_out > 0) {
3002 if (s->zstream.avail_in == 0) {
3003 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3004 return -1;
3005 clen = qemu_get_be16(s->f);
3006 if (clen > IOBUF_SIZE)
3007 return -1;
3008 qemu_get_buffer(s->f, s->buf, clen);
3009 s->zstream.avail_in = clen;
3010 s->zstream.next_in = s->buf;
3011 }
3012 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3013 if (ret != Z_OK && ret != Z_STREAM_END) {
3014 return -1;
3015 }
3016 }
3017 return 0;
3018}
3019
3020static void ram_decompress_close(RamDecompressState *s)
3021{
3022 inflateEnd(&s->zstream);
3023}
3024
aliguori475e4272008-10-06 20:21:51 +00003025#define RAM_SAVE_FLAG_FULL 0x01
3026#define RAM_SAVE_FLAG_COMPRESS 0x02
3027#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3028#define RAM_SAVE_FLAG_PAGE 0x08
3029#define RAM_SAVE_FLAG_EOS 0x10
3030
3031static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003032{
aliguori475e4272008-10-06 20:21:51 +00003033 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3034 uint32_t *array = (uint32_t *)page;
3035 int i;
ths3b46e622007-09-17 08:09:54 +00003036
aliguori475e4272008-10-06 20:21:51 +00003037 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3038 if (array[i] != val)
3039 return 0;
bellardc88676f2006-08-06 13:36:11 +00003040 }
aliguori475e4272008-10-06 20:21:51 +00003041
3042 return 1;
bellardc88676f2006-08-06 13:36:11 +00003043}
3044
aliguori475e4272008-10-06 20:21:51 +00003045static int ram_save_block(QEMUFile *f)
3046{
3047 static ram_addr_t current_addr = 0;
3048 ram_addr_t saved_addr = current_addr;
3049 ram_addr_t addr = 0;
3050 int found = 0;
3051
3052 while (addr < phys_ram_size) {
3053 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3054 uint8_t ch;
3055
3056 cpu_physical_memory_reset_dirty(current_addr,
3057 current_addr + TARGET_PAGE_SIZE,
3058 MIGRATION_DIRTY_FLAG);
3059
3060 ch = *(phys_ram_base + current_addr);
3061
3062 if (is_dup_page(phys_ram_base + current_addr, ch)) {
3063 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3064 qemu_put_byte(f, ch);
3065 } else {
3066 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3067 qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
3068 }
3069
3070 found = 1;
3071 break;
3072 }
3073 addr += TARGET_PAGE_SIZE;
3074 current_addr = (saved_addr + addr) % phys_ram_size;
3075 }
3076
3077 return found;
3078}
3079
3080static ram_addr_t ram_save_threshold = 10;
3081
3082static ram_addr_t ram_save_remaining(void)
3083{
3084 ram_addr_t addr;
3085 ram_addr_t count = 0;
3086
3087 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3088 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3089 count++;
3090 }
3091
3092 return count;
3093}
3094
3095static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3096{
3097 ram_addr_t addr;
3098
3099 if (stage == 1) {
3100 /* Make sure all dirty bits are set */
3101 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3102 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3103 cpu_physical_memory_set_dirty(addr);
3104 }
3105
3106 /* Enable dirty memory tracking */
3107 cpu_physical_memory_set_dirty_tracking(1);
3108
3109 qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
3110 }
3111
3112 while (!qemu_file_rate_limit(f)) {
3113 int ret;
3114
3115 ret = ram_save_block(f);
3116 if (ret == 0) /* no more blocks */
3117 break;
3118 }
3119
3120 /* try transferring iterative blocks of memory */
3121
3122 if (stage == 3) {
3123 cpu_physical_memory_set_dirty_tracking(0);
3124
3125 /* flush all remaining blocks regardless of rate limiting */
3126 while (ram_save_block(f) != 0);
3127 }
3128
3129 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3130
3131 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3132}
3133
3134static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003135{
3136 RamDecompressState s1, *s = &s1;
3137 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003138 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003139
bellardc88676f2006-08-06 13:36:11 +00003140 if (ram_decompress_open(s, f) < 0)
3141 return -EINVAL;
3142 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
3143 if (ram_decompress_buf(s, buf, 1) < 0) {
3144 fprintf(stderr, "Error while reading ram block header\n");
3145 goto error;
3146 }
3147 if (buf[0] == 0) {
3148 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003149 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003150 goto error;
3151 }
aliguori475e4272008-10-06 20:21:51 +00003152 } else {
bellardc88676f2006-08-06 13:36:11 +00003153 error:
3154 printf("Error block header\n");
3155 return -EINVAL;
3156 }
3157 }
3158 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003159
3160 return 0;
3161}
3162
3163static int ram_load(QEMUFile *f, void *opaque, int version_id)
3164{
3165 ram_addr_t addr;
3166 int flags;
3167
3168 if (version_id == 1)
3169 return ram_load_v1(f, opaque);
3170
3171 if (version_id == 2) {
3172 if (qemu_get_be32(f) != phys_ram_size)
3173 return -EINVAL;
3174 return ram_load_dead(f, opaque);
3175 }
3176
3177 if (version_id != 3)
3178 return -EINVAL;
3179
3180 do {
3181 addr = qemu_get_be64(f);
3182
3183 flags = addr & ~TARGET_PAGE_MASK;
3184 addr &= TARGET_PAGE_MASK;
3185
3186 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3187 if (addr != phys_ram_size)
3188 return -EINVAL;
3189 }
3190
3191 if (flags & RAM_SAVE_FLAG_FULL) {
3192 if (ram_load_dead(f, opaque) < 0)
3193 return -EINVAL;
3194 }
3195
3196 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3197 uint8_t ch = qemu_get_byte(f);
3198 memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
3199 } else if (flags & RAM_SAVE_FLAG_PAGE)
3200 qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
3201 } while (!(flags & RAM_SAVE_FLAG_EOS));
3202
bellardc88676f2006-08-06 13:36:11 +00003203 return 0;
3204}
3205
aliguori9e472e12008-10-08 19:50:24 +00003206void qemu_service_io(void)
3207{
3208 CPUState *env = cpu_single_env;
3209 if (env) {
3210 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3211#ifdef USE_KQEMU
3212 if (env->kqemu_enabled) {
3213 kqemu_cpu_interrupt(env);
3214 }
3215#endif
3216 }
3217}
3218
bellard8a7ddc32004-03-31 19:00:16 +00003219/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003220/* bottom halves (can be seen as timers which expire ASAP) */
3221
3222struct QEMUBH {
3223 QEMUBHFunc *cb;
3224 void *opaque;
3225 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003226 int idle;
3227 int deleted;
bellard83f64092006-08-01 16:21:11 +00003228 QEMUBH *next;
3229};
3230
3231static QEMUBH *first_bh = NULL;
3232
3233QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3234{
3235 QEMUBH *bh;
3236 bh = qemu_mallocz(sizeof(QEMUBH));
3237 if (!bh)
3238 return NULL;
3239 bh->cb = cb;
3240 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003241 bh->next = first_bh;
3242 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003243 return bh;
3244}
3245
bellard6eb57332006-08-06 09:51:25 +00003246int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003247{
aliguori1b435b12008-10-31 17:24:21 +00003248 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003249 int ret;
bellard83f64092006-08-01 16:21:11 +00003250
bellard6eb57332006-08-06 09:51:25 +00003251 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003252 for (bh = first_bh; bh; bh = bh->next) {
3253 if (!bh->deleted && bh->scheduled) {
3254 bh->scheduled = 0;
3255 if (!bh->idle)
3256 ret = 1;
3257 bh->idle = 0;
3258 bh->cb(bh->opaque);
3259 }
bellard83f64092006-08-01 16:21:11 +00003260 }
aliguori1b435b12008-10-31 17:24:21 +00003261
3262 /* remove deleted bhs */
3263 bhp = &first_bh;
3264 while (*bhp) {
3265 bh = *bhp;
3266 if (bh->deleted) {
3267 *bhp = bh->next;
3268 qemu_free(bh);
3269 } else
3270 bhp = &bh->next;
3271 }
3272
bellard6eb57332006-08-06 09:51:25 +00003273 return ret;
bellard83f64092006-08-01 16:21:11 +00003274}
3275
aliguori1b435b12008-10-31 17:24:21 +00003276void qemu_bh_schedule_idle(QEMUBH *bh)
3277{
3278 if (bh->scheduled)
3279 return;
3280 bh->scheduled = 1;
3281 bh->idle = 1;
3282}
3283
bellard83f64092006-08-01 16:21:11 +00003284void qemu_bh_schedule(QEMUBH *bh)
3285{
3286 CPUState *env = cpu_single_env;
3287 if (bh->scheduled)
3288 return;
3289 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003290 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003291 /* stop the currently executing CPU to execute the BH ASAP */
3292 if (env) {
3293 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3294 }
3295}
3296
3297void qemu_bh_cancel(QEMUBH *bh)
3298{
aliguori1b435b12008-10-31 17:24:21 +00003299 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003300}
3301
3302void qemu_bh_delete(QEMUBH *bh)
3303{
aliguori1b435b12008-10-31 17:24:21 +00003304 bh->scheduled = 0;
3305 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003306}
3307
aliguori56f3a5d2008-10-31 18:07:17 +00003308static void qemu_bh_update_timeout(int *timeout)
3309{
3310 QEMUBH *bh;
3311
3312 for (bh = first_bh; bh; bh = bh->next) {
3313 if (!bh->deleted && bh->scheduled) {
3314 if (bh->idle) {
3315 /* idle bottom halves will be polled at least
3316 * every 10ms */
3317 *timeout = MIN(10, *timeout);
3318 } else {
3319 /* non-idle bottom halves will be executed
3320 * immediately */
3321 *timeout = 0;
3322 break;
3323 }
3324 }
3325 }
3326}
3327
bellard83f64092006-08-01 16:21:11 +00003328/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003329/* machine registration */
3330
blueswir1bdaf78e2008-10-04 07:24:27 +00003331static QEMUMachine *first_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003332
3333int qemu_register_machine(QEMUMachine *m)
3334{
3335 QEMUMachine **pm;
3336 pm = &first_machine;
3337 while (*pm != NULL)
3338 pm = &(*pm)->next;
3339 m->next = NULL;
3340 *pm = m;
3341 return 0;
3342}
3343
pbrook9596ebb2007-11-18 01:44:38 +00003344static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003345{
3346 QEMUMachine *m;
3347
3348 for(m = first_machine; m != NULL; m = m->next) {
3349 if (!strcmp(m->name, name))
3350 return m;
3351 }
3352 return NULL;
3353}
3354
3355/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003356/* main execution loop */
3357
pbrook9596ebb2007-11-18 01:44:38 +00003358static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003359{
ths740733b2007-06-08 01:57:56 +00003360 DisplayState *ds = opaque;
3361 ds->dpy_refresh(ds);
aurel32f442e082008-03-13 19:20:33 +00003362 qemu_mod_timer(ds->gui_timer,
3363 (ds->gui_timer_interval ?
3364 ds->gui_timer_interval :
3365 GUI_REFRESH_INTERVAL)
3366 + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003367}
3368
bellard0bd48852005-11-11 00:00:47 +00003369struct vm_change_state_entry {
3370 VMChangeStateHandler *cb;
3371 void *opaque;
3372 LIST_ENTRY (vm_change_state_entry) entries;
3373};
3374
3375static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3376
3377VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3378 void *opaque)
3379{
3380 VMChangeStateEntry *e;
3381
3382 e = qemu_mallocz(sizeof (*e));
3383 if (!e)
3384 return NULL;
3385
3386 e->cb = cb;
3387 e->opaque = opaque;
3388 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3389 return e;
3390}
3391
3392void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3393{
3394 LIST_REMOVE (e, entries);
3395 qemu_free (e);
3396}
3397
3398static void vm_state_notify(int running)
3399{
3400 VMChangeStateEntry *e;
3401
3402 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
3403 e->cb(e->opaque, running);
3404 }
3405}
3406
bellard8a7ddc32004-03-31 19:00:16 +00003407/* XXX: support several handlers */
bellard0bd48852005-11-11 00:00:47 +00003408static VMStopHandler *vm_stop_cb;
3409static void *vm_stop_opaque;
bellard8a7ddc32004-03-31 19:00:16 +00003410
3411int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
3412{
3413 vm_stop_cb = cb;
3414 vm_stop_opaque = opaque;
3415 return 0;
3416}
3417
3418void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
3419{
3420 vm_stop_cb = NULL;
3421}
3422
3423void vm_start(void)
3424{
3425 if (!vm_running) {
3426 cpu_enable_ticks();
3427 vm_running = 1;
bellard0bd48852005-11-11 00:00:47 +00003428 vm_state_notify(1);
thsefe75412007-08-24 01:36:32 +00003429 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00003430 }
3431}
3432
ths5fafdf22007-09-16 21:08:06 +00003433void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00003434{
3435 if (vm_running) {
3436 cpu_disable_ticks();
3437 vm_running = 0;
3438 if (reason != 0) {
3439 if (vm_stop_cb) {
3440 vm_stop_cb(vm_stop_opaque, reason);
3441 }
3442 }
bellard0bd48852005-11-11 00:00:47 +00003443 vm_state_notify(0);
bellard8a7ddc32004-03-31 19:00:16 +00003444 }
3445}
3446
bellardbb0c6722004-06-20 12:37:32 +00003447/* reset/shutdown handler */
3448
3449typedef struct QEMUResetEntry {
3450 QEMUResetHandler *func;
3451 void *opaque;
3452 struct QEMUResetEntry *next;
3453} QEMUResetEntry;
3454
3455static QEMUResetEntry *first_reset_entry;
3456static int reset_requested;
3457static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003458static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00003459
aurel32cf7a2fe2008-03-18 06:53:05 +00003460int qemu_shutdown_requested(void)
3461{
3462 int r = shutdown_requested;
3463 shutdown_requested = 0;
3464 return r;
3465}
3466
3467int qemu_reset_requested(void)
3468{
3469 int r = reset_requested;
3470 reset_requested = 0;
3471 return r;
3472}
3473
3474int qemu_powerdown_requested(void)
3475{
3476 int r = powerdown_requested;
3477 powerdown_requested = 0;
3478 return r;
3479}
3480
bellardbb0c6722004-06-20 12:37:32 +00003481void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3482{
3483 QEMUResetEntry **pre, *re;
3484
3485 pre = &first_reset_entry;
3486 while (*pre != NULL)
3487 pre = &(*pre)->next;
3488 re = qemu_mallocz(sizeof(QEMUResetEntry));
3489 re->func = func;
3490 re->opaque = opaque;
3491 re->next = NULL;
3492 *pre = re;
3493}
3494
aurel32cf7a2fe2008-03-18 06:53:05 +00003495void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00003496{
3497 QEMUResetEntry *re;
3498
3499 /* reset all devices */
3500 for(re = first_reset_entry; re != NULL; re = re->next) {
3501 re->func(re->opaque);
3502 }
3503}
3504
3505void qemu_system_reset_request(void)
3506{
bellardd1beab82006-10-02 19:44:22 +00003507 if (no_reboot) {
3508 shutdown_requested = 1;
3509 } else {
3510 reset_requested = 1;
3511 }
bellard6a00d602005-11-21 23:25:50 +00003512 if (cpu_single_env)
3513 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003514}
3515
3516void qemu_system_shutdown_request(void)
3517{
3518 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003519 if (cpu_single_env)
3520 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003521}
3522
bellard34751872005-07-02 14:31:34 +00003523void qemu_system_powerdown_request(void)
3524{
3525 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003526 if (cpu_single_env)
3527 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003528}
3529
ths877cf882007-04-18 18:11:47 +00003530#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003531static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003532{
3533 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003534 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003535
bellardf3311102006-04-12 20:21:17 +00003536
3537 /* XXX: need to suppress polling by better using win32 events */
3538 ret = 0;
3539 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3540 ret |= pe->func(pe->opaque);
3541 }
thse6b1e552007-04-18 17:56:02 +00003542 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003543 int err;
3544 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003545
aliguori56f3a5d2008-10-31 18:07:17 +00003546 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003547 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3548 if (w->func[ret - WAIT_OBJECT_0])
3549 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003550
ths5fafdf22007-09-16 21:08:06 +00003551 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003552 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003553
thse6b1e552007-04-18 17:56:02 +00003554 /* Check if event is signaled */
3555 ret2 = WaitForSingleObject(w->events[i], 0);
3556 if(ret2 == WAIT_OBJECT_0) {
3557 if (w->func[i])
3558 w->func[i](w->opaque[i]);
3559 } else if (ret2 == WAIT_TIMEOUT) {
3560 } else {
3561 err = GetLastError();
3562 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003563 }
3564 }
bellarda18e5242006-06-25 17:18:27 +00003565 } else if (ret == WAIT_TIMEOUT) {
3566 } else {
3567 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003568 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003569 }
bellardf3311102006-04-12 20:21:17 +00003570 }
aliguori56f3a5d2008-10-31 18:07:17 +00003571
3572 *timeout = 0;
3573}
3574#else
blueswir169d64512008-12-07 19:30:18 +00003575static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003576{
3577}
bellardfd1dff42006-02-01 21:29:26 +00003578#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003579
3580void main_loop_wait(int timeout)
3581{
3582 IOHandlerRecord *ioh;
3583 fd_set rfds, wfds, xfds;
3584 int ret, nfds;
3585 struct timeval tv;
3586
3587 qemu_bh_update_timeout(&timeout);
3588
3589 host_main_loop_wait(&timeout);
3590
bellardfd1dff42006-02-01 21:29:26 +00003591 /* poll any events */
3592 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003593 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003594 FD_ZERO(&rfds);
3595 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003596 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003597 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003598 if (ioh->deleted)
3599 continue;
bellardfd1dff42006-02-01 21:29:26 +00003600 if (ioh->fd_read &&
3601 (!ioh->fd_read_poll ||
3602 ioh->fd_read_poll(ioh->opaque) != 0)) {
3603 FD_SET(ioh->fd, &rfds);
3604 if (ioh->fd > nfds)
3605 nfds = ioh->fd;
3606 }
3607 if (ioh->fd_write) {
3608 FD_SET(ioh->fd, &wfds);
3609 if (ioh->fd > nfds)
3610 nfds = ioh->fd;
3611 }
3612 }
ths3b46e622007-09-17 08:09:54 +00003613
aliguori56f3a5d2008-10-31 18:07:17 +00003614 tv.tv_sec = timeout / 1000;
3615 tv.tv_usec = (timeout % 1000) * 1000;
3616
bellarde0356492006-05-01 13:33:02 +00003617#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003618 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003619 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3620 }
3621#endif
3622 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00003623 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003624 IOHandlerRecord **pioh;
3625
3626 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003627 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003628 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003629 }
ths6ab43fd2007-08-25 01:34:19 +00003630 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003631 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003632 }
3633 }
thscafffd42007-02-28 21:59:44 +00003634
3635 /* remove deleted IO handlers */
3636 pioh = &first_io_handler;
3637 while (*pioh) {
3638 ioh = *pioh;
3639 if (ioh->deleted) {
3640 *pioh = ioh->next;
3641 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003642 } else
thscafffd42007-02-28 21:59:44 +00003643 pioh = &ioh->next;
3644 }
bellardfd1dff42006-02-01 21:29:26 +00003645 }
bellarde0356492006-05-01 13:33:02 +00003646#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003647 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003648 if (ret < 0) {
3649 FD_ZERO(&rfds);
3650 FD_ZERO(&wfds);
3651 FD_ZERO(&xfds);
3652 }
3653 slirp_select_poll(&rfds, &wfds, &xfds);
3654 }
3655#endif
bellardc20709a2004-04-21 23:27:19 +00003656
aliguori357c6922008-11-25 17:26:09 +00003657 /* vm time timers */
3658 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3659 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3660 qemu_get_clock(vm_clock));
3661
3662 /* real time timers */
3663 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3664 qemu_get_clock(rt_clock));
3665
pbrook423f0742007-05-23 00:06:54 +00003666 /* Check bottom-halves last in case any of the earlier events triggered
3667 them. */
3668 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003669
bellard5905b2e2004-08-01 21:53:26 +00003670}
3671
pbrook9596ebb2007-11-18 01:44:38 +00003672static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00003673{
3674 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00003675#ifdef CONFIG_PROFILER
3676 int64_t ti;
3677#endif
bellard6a00d602005-11-21 23:25:50 +00003678 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003679
bellard6a00d602005-11-21 23:25:50 +00003680 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00003681 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00003682 for(;;) {
3683 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00003684
bellard15a76442005-11-23 21:01:03 +00003685 for(;;) {
3686 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00003687 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00003688#ifdef CONFIG_PROFILER
3689 ti = profile_getclock();
3690#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003691 if (use_icount) {
3692 int64_t count;
3693 int decr;
3694 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3695 env->icount_decr.u16.low = 0;
3696 env->icount_extra = 0;
3697 count = qemu_next_deadline();
3698 count = (count + (1 << icount_time_shift) - 1)
3699 >> icount_time_shift;
3700 qemu_icount += count;
3701 decr = (count > 0xffff) ? 0xffff : count;
3702 count -= decr;
3703 env->icount_decr.u16.low = decr;
3704 env->icount_extra = count;
3705 }
bellard6a00d602005-11-21 23:25:50 +00003706 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00003707#ifdef CONFIG_PROFILER
3708 qemu_time += profile_getclock() - ti;
3709#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003710 if (use_icount) {
3711 /* Fold pending instructions back into the
3712 instruction counter, and clear the interrupt flag. */
3713 qemu_icount -= (env->icount_decr.u16.low
3714 + env->icount_extra);
3715 env->icount_decr.u32 = 0;
3716 env->icount_extra = 0;
3717 }
balrogee5605e2007-12-03 03:01:40 +00003718 next_cpu = env->next_cpu ?: first_cpu;
aurel3295b01002008-04-04 17:16:35 +00003719 if (event_pending && likely(ret != EXCP_DEBUG)) {
balrogee5605e2007-12-03 03:01:40 +00003720 ret = EXCP_INTERRUPT;
3721 event_pending = 0;
3722 break;
3723 }
pbrookbd967e02007-03-11 18:54:57 +00003724 if (ret == EXCP_HLT) {
3725 /* Give the next CPU a chance to run. */
3726 cur_cpu = env;
3727 continue;
3728 }
bellard15a76442005-11-23 21:01:03 +00003729 if (ret != EXCP_HALTED)
3730 break;
3731 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00003732 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00003733 break;
bellard15a76442005-11-23 21:01:03 +00003734 }
3735 cur_cpu = env;
3736
bellard5905b2e2004-08-01 21:53:26 +00003737 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00003738 ret = EXCP_INTERRUPT;
aurel32b2f76162008-04-11 21:35:52 +00003739 if (no_shutdown) {
3740 vm_stop(0);
3741 no_shutdown = 0;
3742 }
3743 else
3744 break;
bellard5905b2e2004-08-01 21:53:26 +00003745 }
3746 if (reset_requested) {
3747 reset_requested = 0;
3748 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00003749 ret = EXCP_INTERRUPT;
3750 }
3751 if (powerdown_requested) {
3752 powerdown_requested = 0;
3753 qemu_system_powerdown();
3754 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00003755 }
aurel3295b01002008-04-04 17:16:35 +00003756 if (unlikely(ret == EXCP_DEBUG)) {
aliguori880a7572008-11-18 20:30:24 +00003757 gdb_set_stop_cpu(cur_cpu);
bellard5905b2e2004-08-01 21:53:26 +00003758 vm_stop(EXCP_DEBUG);
3759 }
pbrookbd967e02007-03-11 18:54:57 +00003760 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00003761 /* XXX: use timeout computed from timers */
pbrook2e70f6e2008-06-29 01:03:05 +00003762 if (ret == EXCP_HALTED) {
3763 if (use_icount) {
3764 int64_t add;
3765 int64_t delta;
3766 /* Advance virtual time to the next event. */
3767 if (use_icount == 1) {
3768 /* When not using an adaptive execution frequency
3769 we tend to get badly out of sync with real time,
thsbf20dc02008-06-30 17:22:19 +00003770 so just delay for a reasonable amount of time. */
pbrook2e70f6e2008-06-29 01:03:05 +00003771 delta = 0;
3772 } else {
3773 delta = cpu_get_icount() - cpu_get_clock();
3774 }
3775 if (delta > 0) {
3776 /* If virtual time is ahead of real time then just
3777 wait for IO. */
3778 timeout = (delta / 1000000) + 1;
3779 } else {
3780 /* Wait for either IO to occur or the next
3781 timer event. */
3782 add = qemu_next_deadline();
3783 /* We advance the timer before checking for IO.
3784 Limit the amount we advance so that early IO
3785 activity won't get the guest too far ahead. */
3786 if (add > 10000000)
3787 add = 10000000;
3788 delta += add;
3789 add = (add + (1 << icount_time_shift) - 1)
3790 >> icount_time_shift;
3791 qemu_icount += add;
3792 timeout = delta / 1000000;
3793 if (timeout < 0)
3794 timeout = 0;
3795 }
3796 } else {
aliguori0a1af392008-10-31 18:40:25 +00003797 timeout = 5000;
pbrook2e70f6e2008-06-29 01:03:05 +00003798 }
3799 } else {
bellard5905b2e2004-08-01 21:53:26 +00003800 timeout = 0;
pbrook2e70f6e2008-06-29 01:03:05 +00003801 }
bellard5905b2e2004-08-01 21:53:26 +00003802 } else {
blueswir198448f52008-09-30 18:16:09 +00003803 if (shutdown_requested) {
3804 ret = EXCP_INTERRUPT;
aliguori5b08fc12008-08-21 20:08:03 +00003805 break;
blueswir198448f52008-09-30 18:16:09 +00003806 }
aliguori0a1af392008-10-31 18:40:25 +00003807 timeout = 5000;
bellard5905b2e2004-08-01 21:53:26 +00003808 }
bellard89bfc102006-02-08 22:46:31 +00003809#ifdef CONFIG_PROFILER
3810 ti = profile_getclock();
3811#endif
bellard5905b2e2004-08-01 21:53:26 +00003812 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00003813#ifdef CONFIG_PROFILER
3814 dev_time += profile_getclock() - ti;
3815#endif
bellardb4608c02003-06-27 17:34:32 +00003816 }
bellard34865132003-10-05 14:28:56 +00003817 cpu_disable_ticks();
3818 return ret;
bellardb4608c02003-06-27 17:34:32 +00003819}
3820
ths15f82202007-06-29 23:26:08 +00003821static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00003822{
bellard68d0f702008-01-06 17:21:48 +00003823 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00003824 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00003825 "\n"
bellarda20dd502003-09-30 21:07:02 +00003826 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00003827 "\n"
bellarda20dd502003-09-30 21:07:02 +00003828 "Standard options:\n"
bellardcc1daa42005-06-05 14:49:17 +00003829 "-M machine select emulated machine (-M ? for list)\n"
pbrook5adb4832007-03-08 03:15:18 +00003830 "-cpu cpu select CPU (-cpu ? for list)\n"
bellardc45886d2004-01-05 00:02:06 +00003831 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00003832 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3833 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003834 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
aurel32a1620fa2008-04-29 05:58:01 +00003835 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3836 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
aliguori9f7965c2008-10-14 14:42:54 +00003837 " [,cache=writethrough|writeback|none][,format=f]\n"
thse4bcb142007-12-02 04:51:10 +00003838 " use 'file' as a drive image\n"
balrog3e3d5812007-04-30 02:09:25 +00003839 "-mtdblock file use 'file' as on-board Flash memory image\n"
pbrooka1bb27b2007-04-06 16:49:48 +00003840 "-sd file use 'file' as SecureDigital card image\n"
j_mayer86f55662007-04-24 06:52:59 +00003841 "-pflash file use 'file' as a parallel flash image\n"
thseec85c22007-01-05 17:41:07 +00003842 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
ths667acca2006-12-11 02:08:05 +00003843 "-snapshot write to temporary files instead of disk image files\n"
3844#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00003845 "-no-frame open SDL window without a frame and window decorations\n"
ths3780e192007-06-21 21:08:02 +00003846 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
ths667acca2006-12-11 02:08:05 +00003847 "-no-quit disable SDL window close capability\n"
3848#endif
bellard52ca8d62006-06-14 16:03:05 +00003849#ifdef TARGET_I386
3850 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3851#endif
bellarda00bad72004-05-22 21:39:06 +00003852 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellard91fc2112005-12-18 19:09:37 +00003853 "-smp n set the number of CPUs to 'n' [default=1]\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003854 "-nographic disable graphical output and redirect serial I/Os to console\n"
balroga171fe32007-04-30 01:48:07 +00003855 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
bellard4ca00742004-12-12 22:20:04 +00003856#ifndef _WIN32
ths667acca2006-12-11 02:08:05 +00003857 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00003858#endif
bellard1d14ffa2005-10-30 18:58:22 +00003859#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00003860 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00003861 "-soundhw c1,... enable audio support\n"
3862 " and only specified sound cards (comma separated list)\n"
3863 " use -soundhw ? to get the list of supported cards\n"
bellard6a36d842005-12-18 20:34:32 +00003864 " use -soundhw all to enable all of them\n"
bellard1d14ffa2005-10-30 18:58:22 +00003865#endif
malc3893c122008-09-28 00:42:05 +00003866 "-vga [std|cirrus|vmware]\n"
3867 " select video card type\n"
bellard89980282004-06-03 14:04:03 +00003868 "-localtime set the real time clock to local time [default=utc]\n"
bellardd63d3072004-10-03 13:29:03 +00003869 "-full-screen start in full screen\n"
bellarda09db212005-04-30 16:10:35 +00003870#ifdef TARGET_I386
3871 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3872#endif
bellardb389dbf2005-11-06 16:49:55 +00003873 "-usb enable the USB driver (will be the default soon)\n"
3874 "-usbdevice name add the host or guest USB device 'name'\n"
bellard6f7e9ae2005-03-13 09:43:36 +00003875#if defined(TARGET_PPC) || defined(TARGET_SPARC)
3876 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00003877#endif
thsc35734b2007-03-19 15:17:08 +00003878 "-name string set the name of the guest\n"
blueswir18fcb1b92008-09-18 18:29:08 +00003879 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
bellarda20dd502003-09-30 21:07:02 +00003880 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003881 "Network options:\n"
pbrooka41b2ff2006-02-05 04:14:41 +00003882 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
bellard7c9d8e02005-11-15 22:16:05 +00003883 " create a new Network Interface Card and connect it to VLAN 'n'\n"
bellardc20709a2004-04-21 23:27:19 +00003884#ifdef CONFIG_SLIRP
pbrook115defd2006-04-16 11:06:58 +00003885 "-net user[,vlan=n][,hostname=host]\n"
3886 " connect the user mode network stack to VLAN 'n' and send\n"
3887 " hostname 'host' to DHCP clients\n"
bellard7c9d8e02005-11-15 22:16:05 +00003888#endif
bellard7fb843f2006-02-01 23:06:55 +00003889#ifdef _WIN32
3890 "-net tap[,vlan=n],ifname=name\n"
3891 " connect the host TAP network interface to VLAN 'n'\n"
3892#else
thsb46a8902007-10-21 23:20:45 +00003893 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3894 " connect the host TAP network interface to VLAN 'n' and use the\n"
3895 " network scripts 'file' (default=%s)\n"
3896 " and 'dfile' (default=%s);\n"
3897 " use '[down]script=no' to disable script execution;\n"
bellard7c9d8e02005-11-15 22:16:05 +00003898 " use 'fd=h' to connect to an already opened TAP interface\n"
bellard7fb843f2006-02-01 23:06:55 +00003899#endif
bellard6a00d602005-11-21 23:25:50 +00003900 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
bellard7c9d8e02005-11-15 22:16:05 +00003901 " connect the vlan 'n' to another VLAN using a socket connection\n"
bellard3d830452005-12-18 16:36:49 +00003902 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
3903 " connect the vlan 'n' to multicast maddr and port\n"
ths8a16d272008-07-19 09:56:24 +00003904#ifdef CONFIG_VDE
3905 "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3906 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3907 " on host and listening for incoming connections on 'socketpath'.\n"
3908 " Use group 'groupname' and mode 'octalmode' to change default\n"
3909 " ownership and permissions for communication port.\n"
3910#endif
bellard7c9d8e02005-11-15 22:16:05 +00003911 "-net none use it alone to have zero network devices; if no -net option\n"
3912 " is provided, the default is '-net nic -net user'\n"
3913 "\n"
balrogdc72ac12008-11-09 00:04:26 +00003914 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
3915 "-bt hci,host[:id]\n"
3916 " Use host's HCI with the given name\n"
3917 "-bt hci[,vlan=n]\n"
3918 " Emulate a standard HCI in virtual scatternet 'n'\n"
3919 "-bt vhci[,vlan=n]\n"
3920 " Add host computer to virtual scatternet 'n' using VHCI\n"
3921 "-bt device:dev[,vlan=n]\n"
3922 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
3923 "\n"
bellard7c9d8e02005-11-15 22:16:05 +00003924#ifdef CONFIG_SLIRP
ths0db11372007-02-20 00:12:07 +00003925 "-tftp dir allow tftp access to files in dir [-net user]\n"
ths47d5d012007-02-20 00:05:08 +00003926 "-bootp file advertise file in BOOTP replies\n"
bellard7c9d8e02005-11-15 22:16:05 +00003927#ifndef _WIN32
3928 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
bellardc94c8d62004-09-13 21:37:34 +00003929#endif
bellard9bf05442004-08-25 22:12:49 +00003930 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
bellard7c9d8e02005-11-15 22:16:05 +00003931 " redirect TCP or UDP connections from host to guest [-net user]\n"
bellardc20709a2004-04-21 23:27:19 +00003932#endif
bellardc4b1fcc2004-03-14 21:44:30 +00003933 "\n"
3934 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00003935 "-kernel bzImage use 'bzImage' as kernel image\n"
3936 "-append cmdline use 'cmdline' as kernel command line\n"
3937 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00003938 "\n"
bellard330d0412003-07-26 18:11:40 +00003939 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00003940 "-monitor dev redirect the monitor to char device 'dev'\n"
3941 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00003942 "-parallel dev redirect the parallel port to char device 'dev'\n"
bellardf7cce892004-12-08 22:21:25 +00003943 "-pidfile file Write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00003944 "-S freeze CPU at startup (use 'c' to start execution)\n"
pbrookcfc34752007-02-22 01:48:01 +00003945 "-s wait gdb connection to port\n"
3946 "-p port set gdb connection port [default=%s]\n"
bellardf193c792004-03-21 17:06:25 +00003947 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
bellard46d47672004-11-16 01:45:27 +00003948 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3949 " translation (t=none or lba) (usually qemu can guess them)\n"
bellard87b47352006-08-17 17:22:54 +00003950 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
bellardd993e022005-02-10 22:00:06 +00003951#ifdef USE_KQEMU
bellard6515b202006-05-03 22:02:44 +00003952 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
bellardd993e022005-02-10 22:00:06 +00003953 "-no-kqemu disable KQEMU kernel module usage\n"
3954#endif
aliguori7ba1e612008-11-05 16:04:33 +00003955#ifdef CONFIG_KVM
3956 "-enable-kvm enable KVM full virtualization support\n"
3957#endif
bellardbb0c6722004-06-20 12:37:32 +00003958#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00003959 "-no-acpi disable ACPI\n"
aliguori16b29ae2008-12-17 23:28:44 +00003960 "-no-hpet disable HPET\n"
bellardbb0c6722004-06-20 12:37:32 +00003961#endif
balrog4d3b6f62008-02-10 16:33:14 +00003962#ifdef CONFIG_CURSES
3963 "-curses use a curses/ncurses interface instead of SDL\n"
3964#endif
bellardd1beab82006-10-02 19:44:22 +00003965 "-no-reboot exit instead of rebooting\n"
aurel32b2f76162008-04-11 21:35:52 +00003966 "-no-shutdown stop before shutdown\n"
aurel32a8080002008-05-10 23:28:26 +00003967 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
bellard24236862006-04-30 21:28:36 +00003968 "-vnc display start a VNC server on display\n"
ths71e3ceb2006-12-22 02:11:31 +00003969#ifndef _WIN32
3970 "-daemonize daemonize QEMU after initializing\n"
3971#endif
ths9ae02552007-01-05 17:39:04 +00003972 "-option-rom rom load a file, rom, into the option ROM space\n"
blueswir166508602007-05-01 14:16:52 +00003973#ifdef TARGET_SPARC
3974 "-prom-env variable=value set OpenBIOS nvram variables\n"
3975#endif
thsf3dcfad2007-08-24 01:26:02 +00003976 "-clock force the use of the given methods for timer alarm.\n"
aurel323adda042008-03-09 23:43:49 +00003977 " To see what timers are available use -clock ?\n"
bellardbce61842008-02-01 22:18:51 +00003978 "-startdate select initial date of the clock\n"
pbrook2e70f6e2008-06-29 01:03:05 +00003979 "-icount [N|auto]\n"
pbrookdd5d6fe2008-06-29 10:43:16 +00003980 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
bellard0824d6f2003-06-24 13:42:40 +00003981 "\n"
bellard82c643f2004-07-14 17:28:13 +00003982 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00003983 "ctrl-alt-f toggle full screen\n"
3984 "ctrl-alt-n switch to virtual console 'n'\n"
3985 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00003986 "\n"
3987 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3988 ,
bellard0db63472003-10-27 21:37:46 +00003989 "qemu",
bellarda00bad72004-05-22 21:39:06 +00003990 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00003991#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00003992 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00003993 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00003994#endif
bellard6e44ba72004-01-18 21:56:49 +00003995 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00003996 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00003997 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00003998}
3999
bellardcd6f1162004-05-13 22:02:20 +00004000#define HAS_ARG 0x0001
4001
4002enum {
4003 QEMU_OPTION_h,
4004
bellardcc1daa42005-06-05 14:49:17 +00004005 QEMU_OPTION_M,
j_mayer94fc95c2007-03-05 19:44:02 +00004006 QEMU_OPTION_cpu,
bellardcd6f1162004-05-13 22:02:20 +00004007 QEMU_OPTION_fda,
4008 QEMU_OPTION_fdb,
4009 QEMU_OPTION_hda,
4010 QEMU_OPTION_hdb,
4011 QEMU_OPTION_hdc,
4012 QEMU_OPTION_hdd,
thse4bcb142007-12-02 04:51:10 +00004013 QEMU_OPTION_drive,
bellardcd6f1162004-05-13 22:02:20 +00004014 QEMU_OPTION_cdrom,
balrog3e3d5812007-04-30 02:09:25 +00004015 QEMU_OPTION_mtdblock,
pbrooka1bb27b2007-04-06 16:49:48 +00004016 QEMU_OPTION_sd,
j_mayer86f55662007-04-24 06:52:59 +00004017 QEMU_OPTION_pflash,
bellardcd6f1162004-05-13 22:02:20 +00004018 QEMU_OPTION_boot,
4019 QEMU_OPTION_snapshot,
bellard52ca8d62006-06-14 16:03:05 +00004020#ifdef TARGET_I386
4021 QEMU_OPTION_no_fd_bootchk,
4022#endif
bellardcd6f1162004-05-13 22:02:20 +00004023 QEMU_OPTION_m,
4024 QEMU_OPTION_nographic,
balroga171fe32007-04-30 01:48:07 +00004025 QEMU_OPTION_portrait,
bellard1d14ffa2005-10-30 18:58:22 +00004026#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004027 QEMU_OPTION_audio_help,
4028 QEMU_OPTION_soundhw,
4029#endif
bellardcd6f1162004-05-13 22:02:20 +00004030
bellard7c9d8e02005-11-15 22:16:05 +00004031 QEMU_OPTION_net,
bellardc7f74642004-08-24 21:57:12 +00004032 QEMU_OPTION_tftp,
ths47d5d012007-02-20 00:05:08 +00004033 QEMU_OPTION_bootp,
bellard9d728e82004-09-05 23:09:03 +00004034 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00004035 QEMU_OPTION_redir,
balrogdc72ac12008-11-09 00:04:26 +00004036 QEMU_OPTION_bt,
bellardcd6f1162004-05-13 22:02:20 +00004037
4038 QEMU_OPTION_kernel,
4039 QEMU_OPTION_append,
4040 QEMU_OPTION_initrd,
4041
4042 QEMU_OPTION_S,
4043 QEMU_OPTION_s,
4044 QEMU_OPTION_p,
4045 QEMU_OPTION_d,
4046 QEMU_OPTION_hdachs,
4047 QEMU_OPTION_L,
j_mayer1192dad2007-10-05 13:08:35 +00004048 QEMU_OPTION_bios,
bellard3d11d0e2004-12-12 16:56:30 +00004049 QEMU_OPTION_k,
bellardee22c2f2004-06-03 12:49:50 +00004050 QEMU_OPTION_localtime,
bellarde9b137c2004-06-21 16:46:10 +00004051 QEMU_OPTION_g,
malc3893c122008-09-28 00:42:05 +00004052 QEMU_OPTION_vga,
ths20d8a3e2007-02-18 17:04:49 +00004053 QEMU_OPTION_echr,
bellard82c643f2004-07-14 17:28:13 +00004054 QEMU_OPTION_monitor,
4055 QEMU_OPTION_serial,
bellard6508fe52005-01-15 12:02:56 +00004056 QEMU_OPTION_parallel,
bellardd63d3072004-10-03 13:29:03 +00004057 QEMU_OPTION_loadvm,
4058 QEMU_OPTION_full_screen,
ths43523e92007-02-18 18:19:32 +00004059 QEMU_OPTION_no_frame,
ths3780e192007-06-21 21:08:02 +00004060 QEMU_OPTION_alt_grab,
ths667acca2006-12-11 02:08:05 +00004061 QEMU_OPTION_no_quit,
bellardf7cce892004-12-08 22:21:25 +00004062 QEMU_OPTION_pidfile,
bellardd993e022005-02-10 22:00:06 +00004063 QEMU_OPTION_no_kqemu,
bellard89bfc102006-02-08 22:46:31 +00004064 QEMU_OPTION_kernel_kqemu,
aliguori7ba1e612008-11-05 16:04:33 +00004065 QEMU_OPTION_enable_kvm,
bellarda09db212005-04-30 16:10:35 +00004066 QEMU_OPTION_win2k_hack,
bellardbb36d472005-11-05 14:22:28 +00004067 QEMU_OPTION_usb,
bellarda594cfb2005-11-06 16:13:29 +00004068 QEMU_OPTION_usbdevice,
bellard6a00d602005-11-21 23:25:50 +00004069 QEMU_OPTION_smp,
bellard24236862006-04-30 21:28:36 +00004070 QEMU_OPTION_vnc,
bellard6515b202006-05-03 22:02:44 +00004071 QEMU_OPTION_no_acpi,
aliguori16b29ae2008-12-17 23:28:44 +00004072 QEMU_OPTION_no_hpet,
balrog4d3b6f62008-02-10 16:33:14 +00004073 QEMU_OPTION_curses,
bellardd1beab82006-10-02 19:44:22 +00004074 QEMU_OPTION_no_reboot,
aurel32b2f76162008-04-11 21:35:52 +00004075 QEMU_OPTION_no_shutdown,
balrog9467cd42007-05-01 01:34:14 +00004076 QEMU_OPTION_show_cursor,
ths71e3ceb2006-12-22 02:11:31 +00004077 QEMU_OPTION_daemonize,
ths9ae02552007-01-05 17:39:04 +00004078 QEMU_OPTION_option_rom,
thsc35734b2007-03-19 15:17:08 +00004079 QEMU_OPTION_semihosting,
4080 QEMU_OPTION_name,
blueswir166508602007-05-01 14:16:52 +00004081 QEMU_OPTION_prom_env,
balrog2b8f2d42007-07-27 22:08:46 +00004082 QEMU_OPTION_old_param,
thsf3dcfad2007-08-24 01:26:02 +00004083 QEMU_OPTION_clock,
bellard7e0af5d02007-11-07 16:24:33 +00004084 QEMU_OPTION_startdate,
bellard26a5f132008-05-28 12:30:31 +00004085 QEMU_OPTION_tb_size,
pbrook2e70f6e2008-06-29 01:03:05 +00004086 QEMU_OPTION_icount,
blueswir18fcb1b92008-09-18 18:29:08 +00004087 QEMU_OPTION_uuid,
aliguori5bb79102008-10-13 03:12:02 +00004088 QEMU_OPTION_incoming,
bellardcd6f1162004-05-13 22:02:20 +00004089};
4090
4091typedef struct QEMUOption {
4092 const char *name;
4093 int flags;
4094 int index;
4095} QEMUOption;
4096
blueswir1dbed7e42008-10-01 19:38:09 +00004097static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004098 { "h", 0, QEMU_OPTION_h },
pbrook64423fb2007-01-27 17:11:41 +00004099 { "help", 0, QEMU_OPTION_h },
bellardcd6f1162004-05-13 22:02:20 +00004100
bellardcc1daa42005-06-05 14:49:17 +00004101 { "M", HAS_ARG, QEMU_OPTION_M },
j_mayer94fc95c2007-03-05 19:44:02 +00004102 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
bellardcd6f1162004-05-13 22:02:20 +00004103 { "fda", HAS_ARG, QEMU_OPTION_fda },
4104 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
4105 { "hda", HAS_ARG, QEMU_OPTION_hda },
4106 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
4107 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
4108 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
thse4bcb142007-12-02 04:51:10 +00004109 { "drive", HAS_ARG, QEMU_OPTION_drive },
bellardcd6f1162004-05-13 22:02:20 +00004110 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
balrog3e3d5812007-04-30 02:09:25 +00004111 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
pbrooka1bb27b2007-04-06 16:49:48 +00004112 { "sd", HAS_ARG, QEMU_OPTION_sd },
j_mayer86f55662007-04-24 06:52:59 +00004113 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
bellardcd6f1162004-05-13 22:02:20 +00004114 { "boot", HAS_ARG, QEMU_OPTION_boot },
4115 { "snapshot", 0, QEMU_OPTION_snapshot },
bellard52ca8d62006-06-14 16:03:05 +00004116#ifdef TARGET_I386
4117 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
4118#endif
bellardcd6f1162004-05-13 22:02:20 +00004119 { "m", HAS_ARG, QEMU_OPTION_m },
4120 { "nographic", 0, QEMU_OPTION_nographic },
balroga171fe32007-04-30 01:48:07 +00004121 { "portrait", 0, QEMU_OPTION_portrait },
bellard3d11d0e2004-12-12 16:56:30 +00004122 { "k", HAS_ARG, QEMU_OPTION_k },
bellard1d14ffa2005-10-30 18:58:22 +00004123#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004124 { "audio-help", 0, QEMU_OPTION_audio_help },
4125 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
4126#endif
bellardcd6f1162004-05-13 22:02:20 +00004127
bellard7c9d8e02005-11-15 22:16:05 +00004128 { "net", HAS_ARG, QEMU_OPTION_net},
bellard158156d2004-05-17 21:13:42 +00004129#ifdef CONFIG_SLIRP
bellardc7f74642004-08-24 21:57:12 +00004130 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
ths47d5d012007-02-20 00:05:08 +00004131 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
bellardc94c8d62004-09-13 21:37:34 +00004132#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004133 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00004134#endif
bellard9bf05442004-08-25 22:12:49 +00004135 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00004136#endif
balrogdc72ac12008-11-09 00:04:26 +00004137 { "bt", HAS_ARG, QEMU_OPTION_bt },
bellardcd6f1162004-05-13 22:02:20 +00004138
4139 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
4140 { "append", HAS_ARG, QEMU_OPTION_append },
4141 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
4142
4143 { "S", 0, QEMU_OPTION_S },
4144 { "s", 0, QEMU_OPTION_s },
4145 { "p", HAS_ARG, QEMU_OPTION_p },
4146 { "d", HAS_ARG, QEMU_OPTION_d },
4147 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
4148 { "L", HAS_ARG, QEMU_OPTION_L },
j_mayer1192dad2007-10-05 13:08:35 +00004149 { "bios", HAS_ARG, QEMU_OPTION_bios },
bellardd993e022005-02-10 22:00:06 +00004150#ifdef USE_KQEMU
4151 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
bellard89bfc102006-02-08 22:46:31 +00004152 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
bellardd993e022005-02-10 22:00:06 +00004153#endif
aliguori7ba1e612008-11-05 16:04:33 +00004154#ifdef CONFIG_KVM
4155 { "enable-kvm", 0, QEMU_OPTION_enable_kvm },
4156#endif
bellard6f7e9ae2005-03-13 09:43:36 +00004157#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00004158 { "g", 1, QEMU_OPTION_g },
bellard77d4bc32004-05-26 22:13:53 +00004159#endif
bellardee22c2f2004-06-03 12:49:50 +00004160 { "localtime", 0, QEMU_OPTION_localtime },
malc3893c122008-09-28 00:42:05 +00004161 { "vga", HAS_ARG, QEMU_OPTION_vga },
balrog8b6e0722007-06-22 08:23:44 +00004162 { "echr", HAS_ARG, QEMU_OPTION_echr },
4163 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
4164 { "serial", HAS_ARG, QEMU_OPTION_serial },
4165 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
bellardd63d3072004-10-03 13:29:03 +00004166 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
4167 { "full-screen", 0, QEMU_OPTION_full_screen },
ths667acca2006-12-11 02:08:05 +00004168#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00004169 { "no-frame", 0, QEMU_OPTION_no_frame },
ths3780e192007-06-21 21:08:02 +00004170 { "alt-grab", 0, QEMU_OPTION_alt_grab },
ths667acca2006-12-11 02:08:05 +00004171 { "no-quit", 0, QEMU_OPTION_no_quit },
4172#endif
bellardf7cce892004-12-08 22:21:25 +00004173 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellarda09db212005-04-30 16:10:35 +00004174 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
bellarda594cfb2005-11-06 16:13:29 +00004175 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
bellard6a00d602005-11-21 23:25:50 +00004176 { "smp", HAS_ARG, QEMU_OPTION_smp },
bellard24236862006-04-30 21:28:36 +00004177 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
balrog4d3b6f62008-02-10 16:33:14 +00004178#ifdef CONFIG_CURSES
4179 { "curses", 0, QEMU_OPTION_curses },
4180#endif
blueswir18fcb1b92008-09-18 18:29:08 +00004181 { "uuid", HAS_ARG, QEMU_OPTION_uuid },
ths96d30e42007-01-07 20:42:14 +00004182
bellard1f042752004-06-05 13:46:47 +00004183 /* temporary options */
bellarda594cfb2005-11-06 16:13:29 +00004184 { "usb", 0, QEMU_OPTION_usb },
bellard6515b202006-05-03 22:02:44 +00004185 { "no-acpi", 0, QEMU_OPTION_no_acpi },
aliguori16b29ae2008-12-17 23:28:44 +00004186 { "no-hpet", 0, QEMU_OPTION_no_hpet },
bellardd1beab82006-10-02 19:44:22 +00004187 { "no-reboot", 0, QEMU_OPTION_no_reboot },
aurel32b2f76162008-04-11 21:35:52 +00004188 { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
balrog9467cd42007-05-01 01:34:14 +00004189 { "show-cursor", 0, QEMU_OPTION_show_cursor },
ths71e3ceb2006-12-22 02:11:31 +00004190 { "daemonize", 0, QEMU_OPTION_daemonize },
ths9ae02552007-01-05 17:39:04 +00004191 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
pbrooka87295e2007-05-26 15:09:38 +00004192#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00004193 { "semihosting", 0, QEMU_OPTION_semihosting },
4194#endif
thsc35734b2007-03-19 15:17:08 +00004195 { "name", HAS_ARG, QEMU_OPTION_name },
blueswir166508602007-05-01 14:16:52 +00004196#if defined(TARGET_SPARC)
4197 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
4198#endif
balrog2b8f2d42007-07-27 22:08:46 +00004199#if defined(TARGET_ARM)
4200 { "old-param", 0, QEMU_OPTION_old_param },
4201#endif
thsf3dcfad2007-08-24 01:26:02 +00004202 { "clock", HAS_ARG, QEMU_OPTION_clock },
bellard7e0af5d02007-11-07 16:24:33 +00004203 { "startdate", HAS_ARG, QEMU_OPTION_startdate },
bellard26a5f132008-05-28 12:30:31 +00004204 { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
pbrook2e70f6e2008-06-29 01:03:05 +00004205 { "icount", HAS_ARG, QEMU_OPTION_icount },
aliguori5bb79102008-10-13 03:12:02 +00004206 { "incoming", HAS_ARG, QEMU_OPTION_incoming },
bellardcd6f1162004-05-13 22:02:20 +00004207 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004208};
4209
bellard5905b2e2004-08-01 21:53:26 +00004210/* password input */
4211
balrog2bac6012007-04-30 01:34:31 +00004212int qemu_key_check(BlockDriverState *bs, const char *name)
4213{
4214 char password[256];
4215 int i;
4216
4217 if (!bdrv_is_encrypted(bs))
4218 return 0;
4219
4220 term_printf("%s is encrypted.\n", name);
4221 for(i = 0; i < 3; i++) {
4222 monitor_readline("Password: ", 1, password, sizeof(password));
4223 if (bdrv_set_key(bs, password) == 0)
4224 return 0;
4225 term_printf("invalid password\n");
4226 }
4227 return -EPERM;
4228}
4229
aliguori83ab7952008-08-19 14:44:22 +00004230static BlockDriverState *get_bdrv(int index)
4231{
4232 if (index > nb_drives)
4233 return NULL;
4234 return drives_table[index].bdrv;
4235}
4236
bellard5905b2e2004-08-01 21:53:26 +00004237static void read_passwords(void)
4238{
4239 BlockDriverState *bs;
balrog2bac6012007-04-30 01:34:31 +00004240 int i;
bellard5905b2e2004-08-01 21:53:26 +00004241
aliguori83ab7952008-08-19 14:44:22 +00004242 for(i = 0; i < 6; i++) {
4243 bs = get_bdrv(i);
4244 if (bs)
4245 qemu_key_check(bs, bdrv_get_device_name(bs));
bellard5905b2e2004-08-01 21:53:26 +00004246 }
4247}
4248
bellard1d14ffa2005-10-30 18:58:22 +00004249#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004250struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004251#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004252#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004253 {
4254 "pcspk",
4255 "PC speaker",
4256 0,
4257 1,
4258 { .init_isa = pcspk_audio_init }
4259 },
4260#endif
bellard6a36d842005-12-18 20:34:32 +00004261 {
4262 "sb16",
4263 "Creative Sound Blaster 16",
4264 0,
4265 1,
4266 { .init_isa = SB16_init }
4267 },
4268
malccc53d262008-06-13 10:48:22 +00004269#ifdef CONFIG_CS4231A
4270 {
4271 "cs4231a",
4272 "CS4231A",
4273 0,
4274 1,
4275 { .init_isa = cs4231a_init }
4276 },
4277#endif
4278
bellard6a36d842005-12-18 20:34:32 +00004279#ifdef CONFIG_ADLIB
4280 {
4281 "adlib",
4282#ifdef HAS_YMF262
4283 "Yamaha YMF262 (OPL3)",
4284#else
4285 "Yamaha YM3812 (OPL2)",
4286#endif
4287 0,
4288 1,
4289 { .init_isa = Adlib_init }
4290 },
4291#endif
4292
4293#ifdef CONFIG_GUS
4294 {
4295 "gus",
4296 "Gravis Ultrasound GF1",
4297 0,
4298 1,
4299 { .init_isa = GUS_init }
4300 },
4301#endif
4302
balroge5c9a132008-01-14 04:27:55 +00004303#ifdef CONFIG_AC97
4304 {
4305 "ac97",
4306 "Intel 82801AA AC97 Audio",
4307 0,
4308 0,
4309 { .init_pci = ac97_init }
4310 },
4311#endif
4312
bellard6a36d842005-12-18 20:34:32 +00004313 {
4314 "es1370",
4315 "ENSONIQ AudioPCI ES1370",
4316 0,
4317 0,
4318 { .init_pci = es1370_init }
4319 },
balrogb00052e2007-04-30 02:22:06 +00004320#endif
bellard6a36d842005-12-18 20:34:32 +00004321
4322 { NULL, NULL, 0, 0, { NULL } }
4323};
4324
bellard1d14ffa2005-10-30 18:58:22 +00004325static void select_soundhw (const char *optarg)
4326{
bellard6a36d842005-12-18 20:34:32 +00004327 struct soundhw *c;
4328
bellard1d14ffa2005-10-30 18:58:22 +00004329 if (*optarg == '?') {
4330 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004331
bellard1d14ffa2005-10-30 18:58:22 +00004332 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004333 for (c = soundhw; c->name; ++c) {
4334 printf ("%-11s %s\n", c->name, c->descr);
4335 }
4336 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004337 exit (*optarg != '?');
4338 }
4339 else {
bellard6a36d842005-12-18 20:34:32 +00004340 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004341 const char *p;
4342 char *e;
4343 int bad_card = 0;
4344
bellard6a36d842005-12-18 20:34:32 +00004345 if (!strcmp (optarg, "all")) {
4346 for (c = soundhw; c->name; ++c) {
4347 c->enabled = 1;
4348 }
4349 return;
4350 }
bellard1d14ffa2005-10-30 18:58:22 +00004351
bellard6a36d842005-12-18 20:34:32 +00004352 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004353 while (*p) {
4354 e = strchr (p, ',');
4355 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004356
4357 for (c = soundhw; c->name; ++c) {
4358 if (!strncmp (c->name, p, l)) {
4359 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004360 break;
4361 }
4362 }
bellard6a36d842005-12-18 20:34:32 +00004363
4364 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004365 if (l > 80) {
4366 fprintf (stderr,
4367 "Unknown sound card name (too big to show)\n");
4368 }
4369 else {
4370 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4371 (int) l, p);
4372 }
4373 bad_card = 1;
4374 }
4375 p += l + (e != NULL);
4376 }
4377
4378 if (bad_card)
4379 goto show_valid_cards;
4380 }
4381}
4382#endif
4383
malc3893c122008-09-28 00:42:05 +00004384static void select_vgahw (const char *p)
4385{
4386 const char *opts;
4387
4388 if (strstart(p, "std", &opts)) {
4389 cirrus_vga_enabled = 0;
4390 vmsvga_enabled = 0;
4391 } else if (strstart(p, "cirrus", &opts)) {
4392 cirrus_vga_enabled = 1;
4393 vmsvga_enabled = 0;
4394 } else if (strstart(p, "vmware", &opts)) {
4395 cirrus_vga_enabled = 0;
4396 vmsvga_enabled = 1;
4397 } else {
4398 invalid_vga:
4399 fprintf(stderr, "Unknown vga type: %s\n", p);
4400 exit(1);
4401 }
malccb5a7aa2008-09-28 00:42:12 +00004402 while (*opts) {
4403 const char *nextopt;
4404
4405 if (strstart(opts, ",retrace=", &nextopt)) {
4406 opts = nextopt;
4407 if (strstart(opts, "dumb", &nextopt))
4408 vga_retrace_method = VGA_RETRACE_DUMB;
4409 else if (strstart(opts, "precise", &nextopt))
4410 vga_retrace_method = VGA_RETRACE_PRECISE;
4411 else goto invalid_vga;
4412 } else goto invalid_vga;
4413 opts = nextopt;
4414 }
malc3893c122008-09-28 00:42:05 +00004415}
4416
bellard3587d7e2006-06-26 20:03:44 +00004417#ifdef _WIN32
4418static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4419{
4420 exit(STATUS_CONTROL_C_EXIT);
4421 return TRUE;
4422}
4423#endif
4424
blueswir18fcb1b92008-09-18 18:29:08 +00004425static int qemu_uuid_parse(const char *str, uint8_t *uuid)
4426{
4427 int ret;
4428
4429 if(strlen(str) != 36)
4430 return -1;
4431
4432 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4433 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4434 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4435
4436 if(ret != 16)
4437 return -1;
4438
4439 return 0;
4440}
4441
bellard7c9d8e02005-11-15 22:16:05 +00004442#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004443
aliguori5b08fc12008-08-21 20:08:03 +00004444#ifndef _WIN32
4445
4446static void termsig_handler(int signal)
4447{
4448 qemu_system_shutdown_request();
4449}
4450
blueswir16f9e3802008-09-09 18:56:59 +00004451static void termsig_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004452{
4453 struct sigaction act;
4454
4455 memset(&act, 0, sizeof(act));
4456 act.sa_handler = termsig_handler;
4457 sigaction(SIGINT, &act, NULL);
4458 sigaction(SIGHUP, &act, NULL);
4459 sigaction(SIGTERM, &act, NULL);
4460}
4461
4462#endif
4463
malc902b3d52008-12-10 19:18:40 +00004464int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004465{
bellard67b915a2004-03-31 23:37:16 +00004466#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00004467 int use_gdbstub;
4468 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00004469#endif
j_mayer28c5af52007-11-11 01:50:45 +00004470 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004471 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004472 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004473 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004474 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00004475 const char *boot_devices = "";
bellard313aa562003-08-10 21:52:11 +00004476 DisplayState *ds = &display_state;
bellard46d47672004-11-16 01:45:27 +00004477 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004478 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004479 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004480 const char *bt_opts[MAX_BT_CMDLINE];
4481 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004482 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004483 int optind;
4484 const char *r, *optarg;
bellard82c643f2004-07-14 17:28:13 +00004485 CharDriverState *monitor_hd;
pbrookfd5f3932008-03-26 20:55:43 +00004486 const char *monitor_device;
4487 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004488 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004489 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004490 int parallel_device_index;
bellardd63d3072004-10-03 13:29:03 +00004491 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004492 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004493 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004494 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004495 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00004496 int fds[2];
bellard26a5f132008-05-28 12:30:31 +00004497 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004498 const char *pid_file = NULL;
blueswir141bd6392008-10-05 09:56:21 +00004499 int autostart;
aliguori5bb79102008-10-13 03:12:02 +00004500 const char *incoming = NULL;
bellard0bd48852005-11-11 00:00:47 +00004501
malc902b3d52008-12-10 19:18:40 +00004502 qemu_cache_utils_init(envp);
4503
bellard0bd48852005-11-11 00:00:47 +00004504 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004505#ifndef _WIN32
4506 {
4507 struct sigaction act;
4508 sigfillset(&act.sa_mask);
4509 act.sa_flags = 0;
4510 act.sa_handler = SIG_IGN;
4511 sigaction(SIGPIPE, &act, NULL);
4512 }
bellard3587d7e2006-06-26 20:03:44 +00004513#else
4514 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004515 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4516 QEMU to run on a single CPU */
4517 {
4518 HANDLE h;
4519 DWORD mask, smask;
4520 int i;
4521 h = GetCurrentProcess();
4522 if (GetProcessAffinityMask(h, &mask, &smask)) {
4523 for(i = 0; i < 32; i++) {
4524 if (mask & (1 << i))
4525 break;
4526 }
4527 if (i != 32) {
4528 mask = 1 << i;
4529 SetProcessAffinityMask(h, mask);
4530 }
4531 }
4532 }
bellard67b915a2004-03-31 23:37:16 +00004533#endif
bellardbe995c22006-06-25 16:25:21 +00004534
bellardcc1daa42005-06-05 14:49:17 +00004535 register_machines();
4536 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004537 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004538 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004539 ram_size = 0;
bellard313aa562003-08-10 21:52:11 +00004540 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00004541#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00004542 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00004543 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00004544#endif
bellard33e39632003-07-06 17:15:21 +00004545 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004546 nographic = 0;
balrog4d3b6f62008-02-10 16:33:14 +00004547 curses = 0;
bellarda20dd502003-09-30 21:07:02 +00004548 kernel_filename = NULL;
4549 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004550 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004551 translation = BIOS_ATA_TRANSLATION_AUTO;
pbrookc60e08d2008-07-01 16:24:38 +00004552 monitor_device = "vc";
bellardc4b1fcc2004-03-14 21:44:30 +00004553
aurel32c75a8232008-05-04 00:50:34 +00004554 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004555 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004556 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004557 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004558
aurel32c75a8232008-05-04 00:50:34 +00004559 parallel_devices[0] = "vc:640x480";
bellard6508fe52005-01-15 12:02:56 +00004560 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004561 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004562 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004563
bellarda594cfb2005-11-06 16:13:29 +00004564 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004565
bellard7c9d8e02005-11-15 22:16:05 +00004566 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004567 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004568 nb_drives = 0;
4569 nb_drives_opt = 0;
4570 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004571
4572 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004573
bellard26a5f132008-05-28 12:30:31 +00004574 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004575 autostart= 1;
4576
bellardcd6f1162004-05-13 22:02:20 +00004577 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004578 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004579 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004580 break;
bellardcd6f1162004-05-13 22:02:20 +00004581 r = argv[optind];
4582 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004583 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004584 } else {
4585 const QEMUOption *popt;
4586
4587 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004588 /* Treat --foo the same as -foo. */
4589 if (r[1] == '-')
4590 r++;
bellardcd6f1162004-05-13 22:02:20 +00004591 popt = qemu_options;
4592 for(;;) {
4593 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004594 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004595 argv[0], r);
4596 exit(1);
4597 }
4598 if (!strcmp(popt->name, r + 1))
4599 break;
4600 popt++;
4601 }
4602 if (popt->flags & HAS_ARG) {
4603 if (optind >= argc) {
4604 fprintf(stderr, "%s: option '%s' requires an argument\n",
4605 argv[0], r);
4606 exit(1);
4607 }
4608 optarg = argv[optind++];
4609 } else {
4610 optarg = NULL;
4611 }
4612
4613 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004614 case QEMU_OPTION_M:
4615 machine = find_machine(optarg);
4616 if (!machine) {
4617 QEMUMachine *m;
4618 printf("Supported machines are:\n");
4619 for(m = first_machine; m != NULL; m = m->next) {
4620 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004621 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00004622 m == first_machine ? " (default)" : "");
4623 }
ths15f82202007-06-29 23:26:08 +00004624 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004625 }
4626 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004627 case QEMU_OPTION_cpu:
4628 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004629 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004630/* XXX: implement xxx_cpu_list for targets that still miss it */
4631#if defined(cpu_list)
4632 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004633#endif
ths15f82202007-06-29 23:26:08 +00004634 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004635 } else {
4636 cpu_model = optarg;
4637 }
4638 break;
bellardcd6f1162004-05-13 22:02:20 +00004639 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004640 initrd_filename = optarg;
4641 break;
bellardcd6f1162004-05-13 22:02:20 +00004642 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004643 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00004644 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004645 else
balrog609497a2008-01-14 02:56:53 +00004646 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004647 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004648 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004649 translation == BIOS_ATA_TRANSLATION_LBA ?
4650 ",trans=lba" :
4651 translation == BIOS_ATA_TRANSLATION_NONE ?
4652 ",trans=none" : "");
4653 break;
bellardcd6f1162004-05-13 22:02:20 +00004654 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004655 case QEMU_OPTION_hdc:
4656 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004657 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004658 break;
thse4bcb142007-12-02 04:51:10 +00004659 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004660 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004661 break;
balrog3e3d5812007-04-30 02:09:25 +00004662 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004663 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004664 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004665 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004666 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004667 break;
j_mayer86f55662007-04-24 06:52:59 +00004668 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004669 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004670 break;
bellardcd6f1162004-05-13 22:02:20 +00004671 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004672 snapshot = 1;
4673 break;
bellardcd6f1162004-05-13 22:02:20 +00004674 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004675 {
bellard330d0412003-07-26 18:11:40 +00004676 const char *p;
4677 p = optarg;
4678 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004679 if (cyls < 1 || cyls > 16383)
4680 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004681 if (*p != ',')
4682 goto chs_fail;
4683 p++;
4684 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004685 if (heads < 1 || heads > 16)
4686 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004687 if (*p != ',')
4688 goto chs_fail;
4689 p++;
4690 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004691 if (secs < 1 || secs > 63)
4692 goto chs_fail;
4693 if (*p == ',') {
4694 p++;
4695 if (!strcmp(p, "none"))
4696 translation = BIOS_ATA_TRANSLATION_NONE;
4697 else if (!strcmp(p, "lba"))
4698 translation = BIOS_ATA_TRANSLATION_LBA;
4699 else if (!strcmp(p, "auto"))
4700 translation = BIOS_ATA_TRANSLATION_AUTO;
4701 else
4702 goto chs_fail;
4703 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004704 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004705 fprintf(stderr, "qemu: invalid physical CHS format\n");
4706 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004707 }
thse4bcb142007-12-02 04:51:10 +00004708 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00004709 snprintf(drives_opt[hda_index].opt,
4710 sizeof(drives_opt[hda_index].opt),
4711 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4712 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004713 translation == BIOS_ATA_TRANSLATION_LBA ?
4714 ",trans=lba" :
4715 translation == BIOS_ATA_TRANSLATION_NONE ?
4716 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00004717 }
4718 break;
bellardcd6f1162004-05-13 22:02:20 +00004719 case QEMU_OPTION_nographic:
bellarda20dd502003-09-30 21:07:02 +00004720 nographic = 1;
4721 break;
balrog4d3b6f62008-02-10 16:33:14 +00004722#ifdef CONFIG_CURSES
4723 case QEMU_OPTION_curses:
4724 curses = 1;
4725 break;
4726#endif
balroga171fe32007-04-30 01:48:07 +00004727 case QEMU_OPTION_portrait:
4728 graphic_rotate = 1;
4729 break;
bellardcd6f1162004-05-13 22:02:20 +00004730 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004731 kernel_filename = optarg;
4732 break;
bellardcd6f1162004-05-13 22:02:20 +00004733 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004734 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004735 break;
bellardcd6f1162004-05-13 22:02:20 +00004736 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004737 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004738 break;
bellardcd6f1162004-05-13 22:02:20 +00004739 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004740 boot_devices = optarg;
4741 /* We just do some generic consistency checks */
4742 {
4743 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00004744 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00004745
4746 boot_devices_bitmap = 0;
4747 for (p = boot_devices; *p != '\0'; p++) {
4748 /* Allowed boot devices are:
4749 * a b : floppy disk drives
4750 * c ... f : IDE disk drives
4751 * g ... m : machine implementation dependant drives
4752 * n ... p : network devices
4753 * It's up to each machine implementation to check
4754 * if the given boot devices match the actual hardware
4755 * implementation and firmware features.
4756 */
4757 if (*p < 'a' || *p > 'q') {
4758 fprintf(stderr, "Invalid boot device '%c'\n", *p);
4759 exit(1);
4760 }
4761 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4762 fprintf(stderr,
4763 "Boot device '%c' was given twice\n",*p);
4764 exit(1);
4765 }
4766 boot_devices_bitmap |= 1 << (*p - 'a');
4767 }
bellard36b486b2003-11-11 13:36:08 +00004768 }
4769 break;
bellardcd6f1162004-05-13 22:02:20 +00004770 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004771 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004772 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004773 break;
bellard52ca8d62006-06-14 16:03:05 +00004774#ifdef TARGET_I386
4775 case QEMU_OPTION_no_fd_bootchk:
4776 fd_bootchk = 0;
4777 break;
4778#endif
bellard7c9d8e02005-11-15 22:16:05 +00004779 case QEMU_OPTION_net:
4780 if (nb_net_clients >= MAX_NET_CLIENTS) {
4781 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00004782 exit(1);
4783 }
pbrookfd5f3932008-03-26 20:55:43 +00004784 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00004785 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00004786 break;
bellardc7f74642004-08-24 21:57:12 +00004787#ifdef CONFIG_SLIRP
4788 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00004789 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004790 break;
ths47d5d012007-02-20 00:05:08 +00004791 case QEMU_OPTION_bootp:
4792 bootp_filename = optarg;
4793 break;
bellardc94c8d62004-09-13 21:37:34 +00004794#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004795 case QEMU_OPTION_smb:
4796 net_slirp_smb(optarg);
4797 break;
bellardc94c8d62004-09-13 21:37:34 +00004798#endif
bellard9bf05442004-08-25 22:12:49 +00004799 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00004800 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00004801 break;
bellardc7f74642004-08-24 21:57:12 +00004802#endif
balrogdc72ac12008-11-09 00:04:26 +00004803 case QEMU_OPTION_bt:
4804 if (nb_bt_opts >= MAX_BT_CMDLINE) {
4805 fprintf(stderr, "qemu: too many bluetooth options\n");
4806 exit(1);
4807 }
4808 bt_opts[nb_bt_opts++] = optarg;
4809 break;
bellard1d14ffa2005-10-30 18:58:22 +00004810#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004811 case QEMU_OPTION_audio_help:
4812 AUD_help ();
4813 exit (0);
4814 break;
4815 case QEMU_OPTION_soundhw:
4816 select_soundhw (optarg);
4817 break;
4818#endif
bellardcd6f1162004-05-13 22:02:20 +00004819 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00004820 help(0);
bellardcd6f1162004-05-13 22:02:20 +00004821 break;
aurel3200f82b82008-04-27 21:12:55 +00004822 case QEMU_OPTION_m: {
4823 uint64_t value;
4824 char *ptr;
4825
4826 value = strtoul(optarg, &ptr, 10);
4827 switch (*ptr) {
4828 case 0: case 'M': case 'm':
4829 value <<= 20;
4830 break;
4831 case 'G': case 'g':
4832 value <<= 30;
4833 break;
4834 default:
4835 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00004836 exit(1);
4837 }
aurel3200f82b82008-04-27 21:12:55 +00004838
4839 /* On 32-bit hosts, QEMU is limited by virtual address space */
4840 if (value > (2047 << 20)
4841#ifndef USE_KQEMU
4842 && HOST_LONG_BITS == 32
4843#endif
4844 ) {
4845 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4846 exit(1);
4847 }
4848 if (value != (uint64_t)(ram_addr_t)value) {
4849 fprintf(stderr, "qemu: ram size too large\n");
4850 exit(1);
4851 }
4852 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00004853 break;
aurel3200f82b82008-04-27 21:12:55 +00004854 }
bellardcd6f1162004-05-13 22:02:20 +00004855 case QEMU_OPTION_d:
4856 {
4857 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00004858 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00004859
bellardcd6f1162004-05-13 22:02:20 +00004860 mask = cpu_str_to_log_mask(optarg);
4861 if (!mask) {
4862 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00004863 for(item = cpu_log_items; item->mask != 0; item++) {
4864 printf("%-10s %s\n", item->name, item->help);
4865 }
4866 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00004867 }
4868 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00004869 }
bellardcd6f1162004-05-13 22:02:20 +00004870 break;
bellard67b915a2004-03-31 23:37:16 +00004871#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00004872 case QEMU_OPTION_s:
4873 use_gdbstub = 1;
4874 break;
4875 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00004876 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00004877 break;
bellard67b915a2004-03-31 23:37:16 +00004878#endif
bellardcd6f1162004-05-13 22:02:20 +00004879 case QEMU_OPTION_L:
4880 bios_dir = optarg;
4881 break;
j_mayer1192dad2007-10-05 13:08:35 +00004882 case QEMU_OPTION_bios:
4883 bios_name = optarg;
4884 break;
bellardcd6f1162004-05-13 22:02:20 +00004885 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00004886 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00004887 break;
bellard3d11d0e2004-12-12 16:56:30 +00004888 case QEMU_OPTION_k:
4889 keyboard_layout = optarg;
4890 break;
bellardee22c2f2004-06-03 12:49:50 +00004891 case QEMU_OPTION_localtime:
4892 rtc_utc = 0;
4893 break;
malc3893c122008-09-28 00:42:05 +00004894 case QEMU_OPTION_vga:
4895 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00004896 break;
bellarde9b137c2004-06-21 16:46:10 +00004897 case QEMU_OPTION_g:
4898 {
4899 const char *p;
4900 int w, h, depth;
4901 p = optarg;
4902 w = strtol(p, (char **)&p, 10);
4903 if (w <= 0) {
4904 graphic_error:
4905 fprintf(stderr, "qemu: invalid resolution or depth\n");
4906 exit(1);
4907 }
4908 if (*p != 'x')
4909 goto graphic_error;
4910 p++;
4911 h = strtol(p, (char **)&p, 10);
4912 if (h <= 0)
4913 goto graphic_error;
4914 if (*p == 'x') {
4915 p++;
4916 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00004917 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00004918 depth != 24 && depth != 32)
4919 goto graphic_error;
4920 } else if (*p == '\0') {
4921 depth = graphic_depth;
4922 } else {
4923 goto graphic_error;
4924 }
ths3b46e622007-09-17 08:09:54 +00004925
bellarde9b137c2004-06-21 16:46:10 +00004926 graphic_width = w;
4927 graphic_height = h;
4928 graphic_depth = depth;
4929 }
4930 break;
ths20d8a3e2007-02-18 17:04:49 +00004931 case QEMU_OPTION_echr:
4932 {
4933 char *r;
4934 term_escape_char = strtol(optarg, &r, 0);
4935 if (r == optarg)
4936 printf("Bad argument to echr\n");
4937 break;
4938 }
bellard82c643f2004-07-14 17:28:13 +00004939 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00004940 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00004941 break;
4942 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00004943 if (serial_device_index >= MAX_SERIAL_PORTS) {
4944 fprintf(stderr, "qemu: too many serial ports\n");
4945 exit(1);
4946 }
pbrookfd5f3932008-03-26 20:55:43 +00004947 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00004948 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00004949 break;
bellard6508fe52005-01-15 12:02:56 +00004950 case QEMU_OPTION_parallel:
4951 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
4952 fprintf(stderr, "qemu: too many parallel ports\n");
4953 exit(1);
4954 }
pbrookfd5f3932008-03-26 20:55:43 +00004955 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00004956 parallel_device_index++;
4957 break;
bellardd63d3072004-10-03 13:29:03 +00004958 case QEMU_OPTION_loadvm:
4959 loadvm = optarg;
4960 break;
4961 case QEMU_OPTION_full_screen:
4962 full_screen = 1;
4963 break;
ths667acca2006-12-11 02:08:05 +00004964#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00004965 case QEMU_OPTION_no_frame:
4966 no_frame = 1;
4967 break;
ths3780e192007-06-21 21:08:02 +00004968 case QEMU_OPTION_alt_grab:
4969 alt_grab = 1;
4970 break;
ths667acca2006-12-11 02:08:05 +00004971 case QEMU_OPTION_no_quit:
4972 no_quit = 1;
4973 break;
4974#endif
bellardf7cce892004-12-08 22:21:25 +00004975 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00004976 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00004977 break;
bellarda09db212005-04-30 16:10:35 +00004978#ifdef TARGET_I386
4979 case QEMU_OPTION_win2k_hack:
4980 win2k_install_hack = 1;
4981 break;
4982#endif
bellardd993e022005-02-10 22:00:06 +00004983#ifdef USE_KQEMU
4984 case QEMU_OPTION_no_kqemu:
4985 kqemu_allowed = 0;
4986 break;
bellard89bfc102006-02-08 22:46:31 +00004987 case QEMU_OPTION_kernel_kqemu:
4988 kqemu_allowed = 2;
4989 break;
bellardd993e022005-02-10 22:00:06 +00004990#endif
aliguori7ba1e612008-11-05 16:04:33 +00004991#ifdef CONFIG_KVM
4992 case QEMU_OPTION_enable_kvm:
4993 kvm_allowed = 1;
4994#ifdef USE_KQEMU
4995 kqemu_allowed = 0;
4996#endif
4997 break;
4998#endif
bellardbb36d472005-11-05 14:22:28 +00004999 case QEMU_OPTION_usb:
5000 usb_enabled = 1;
5001 break;
bellarda594cfb2005-11-06 16:13:29 +00005002 case QEMU_OPTION_usbdevice:
5003 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00005004 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00005005 fprintf(stderr, "Too many USB devices\n");
5006 exit(1);
5007 }
pbrookfd5f3932008-03-26 20:55:43 +00005008 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00005009 usb_devices_index++;
5010 break;
bellard6a00d602005-11-21 23:25:50 +00005011 case QEMU_OPTION_smp:
5012 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00005013 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005014 fprintf(stderr, "Invalid number of CPUs\n");
5015 exit(1);
5016 }
5017 break;
bellard24236862006-04-30 21:28:36 +00005018 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00005019 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005020 break;
bellard6515b202006-05-03 22:02:44 +00005021 case QEMU_OPTION_no_acpi:
5022 acpi_enabled = 0;
5023 break;
aliguori16b29ae2008-12-17 23:28:44 +00005024 case QEMU_OPTION_no_hpet:
5025 no_hpet = 1;
5026 break;
bellardd1beab82006-10-02 19:44:22 +00005027 case QEMU_OPTION_no_reboot:
5028 no_reboot = 1;
5029 break;
aurel32b2f76162008-04-11 21:35:52 +00005030 case QEMU_OPTION_no_shutdown:
5031 no_shutdown = 1;
5032 break;
balrog9467cd42007-05-01 01:34:14 +00005033 case QEMU_OPTION_show_cursor:
5034 cursor_hide = 0;
5035 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005036 case QEMU_OPTION_uuid:
5037 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5038 fprintf(stderr, "Fail to parse UUID string."
5039 " Wrong format.\n");
5040 exit(1);
5041 }
5042 break;
ths71e3ceb2006-12-22 02:11:31 +00005043 case QEMU_OPTION_daemonize:
5044 daemonize = 1;
5045 break;
ths9ae02552007-01-05 17:39:04 +00005046 case QEMU_OPTION_option_rom:
5047 if (nb_option_roms >= MAX_OPTION_ROMS) {
5048 fprintf(stderr, "Too many option ROMs\n");
5049 exit(1);
5050 }
5051 option_rom[nb_option_roms] = optarg;
5052 nb_option_roms++;
5053 break;
pbrook8e716212007-01-20 17:12:09 +00005054 case QEMU_OPTION_semihosting:
5055 semihosting_enabled = 1;
5056 break;
thsc35734b2007-03-19 15:17:08 +00005057 case QEMU_OPTION_name:
5058 qemu_name = optarg;
5059 break;
blueswir166508602007-05-01 14:16:52 +00005060#ifdef TARGET_SPARC
5061 case QEMU_OPTION_prom_env:
5062 if (nb_prom_envs >= MAX_PROM_ENVS) {
5063 fprintf(stderr, "Too many prom variables\n");
5064 exit(1);
5065 }
5066 prom_envs[nb_prom_envs] = optarg;
5067 nb_prom_envs++;
5068 break;
5069#endif
balrog2b8f2d42007-07-27 22:08:46 +00005070#ifdef TARGET_ARM
5071 case QEMU_OPTION_old_param:
5072 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005073 break;
balrog2b8f2d42007-07-27 22:08:46 +00005074#endif
thsf3dcfad2007-08-24 01:26:02 +00005075 case QEMU_OPTION_clock:
5076 configure_alarms(optarg);
5077 break;
bellard7e0af5d02007-11-07 16:24:33 +00005078 case QEMU_OPTION_startdate:
5079 {
5080 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005081 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005082 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005083 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005084 } else {
5085 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5086 &tm.tm_year,
5087 &tm.tm_mon,
5088 &tm.tm_mday,
5089 &tm.tm_hour,
5090 &tm.tm_min,
5091 &tm.tm_sec) == 6) {
5092 /* OK */
5093 } else if (sscanf(optarg, "%d-%d-%d",
5094 &tm.tm_year,
5095 &tm.tm_mon,
5096 &tm.tm_mday) == 3) {
5097 tm.tm_hour = 0;
5098 tm.tm_min = 0;
5099 tm.tm_sec = 0;
5100 } else {
5101 goto date_fail;
5102 }
5103 tm.tm_year -= 1900;
5104 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005105 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005106 if (rtc_start_date == -1) {
5107 date_fail:
5108 fprintf(stderr, "Invalid date format. Valid format are:\n"
5109 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5110 exit(1);
5111 }
balrogf6503052008-02-17 11:42:19 +00005112 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005113 }
5114 }
5115 break;
bellard26a5f132008-05-28 12:30:31 +00005116 case QEMU_OPTION_tb_size:
5117 tb_size = strtol(optarg, NULL, 0);
5118 if (tb_size < 0)
5119 tb_size = 0;
5120 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005121 case QEMU_OPTION_icount:
5122 use_icount = 1;
5123 if (strcmp(optarg, "auto") == 0) {
5124 icount_time_shift = -1;
5125 } else {
5126 icount_time_shift = strtol(optarg, NULL, 0);
5127 }
5128 break;
aliguori5bb79102008-10-13 03:12:02 +00005129 case QEMU_OPTION_incoming:
5130 incoming = optarg;
5131 break;
bellardcd6f1162004-05-13 22:02:20 +00005132 }
bellard0824d6f2003-06-24 13:42:40 +00005133 }
5134 }
bellard330d0412003-07-26 18:11:40 +00005135
aliguori7ba1e612008-11-05 16:04:33 +00005136#if defined(CONFIG_KVM) && defined(USE_KQEMU)
5137 if (kvm_allowed && kqemu_allowed) {
5138 fprintf(stderr,
5139 "You can not enable both KVM and kqemu at the same time\n");
5140 exit(1);
5141 }
5142#endif
5143
balrog3d878ca2008-10-28 10:59:59 +00005144 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005145 if (smp_cpus > machine->max_cpus) {
5146 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5147 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5148 machine->max_cpus);
5149 exit(1);
5150 }
5151
aliguoribc0129d2008-08-01 15:12:34 +00005152 if (nographic) {
5153 if (serial_device_index == 0)
5154 serial_devices[0] = "stdio";
5155 if (parallel_device_index == 0)
5156 parallel_devices[0] = "null";
5157 if (strncmp(monitor_device, "vc", 2) == 0)
5158 monitor_device = "stdio";
5159 }
5160
ths71e3ceb2006-12-22 02:11:31 +00005161#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005162 if (daemonize) {
5163 pid_t pid;
5164
5165 if (pipe(fds) == -1)
5166 exit(1);
5167
5168 pid = fork();
5169 if (pid > 0) {
5170 uint8_t status;
5171 ssize_t len;
5172
5173 close(fds[1]);
5174
5175 again:
ths93815bc2007-03-19 15:58:31 +00005176 len = read(fds[0], &status, 1);
5177 if (len == -1 && (errno == EINTR))
5178 goto again;
5179
5180 if (len != 1)
5181 exit(1);
5182 else if (status == 1) {
5183 fprintf(stderr, "Could not acquire pidfile\n");
5184 exit(1);
5185 } else
5186 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005187 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005188 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005189
5190 setsid();
5191
5192 pid = fork();
5193 if (pid > 0)
5194 exit(0);
5195 else if (pid < 0)
5196 exit(1);
5197
5198 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005199
5200 signal(SIGTSTP, SIG_IGN);
5201 signal(SIGTTOU, SIG_IGN);
5202 signal(SIGTTIN, SIG_IGN);
5203 }
5204#endif
5205
thsaa26bb22007-03-25 21:33:06 +00005206 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005207 if (daemonize) {
5208 uint8_t status = 1;
5209 write(fds[1], &status, 1);
5210 } else
5211 fprintf(stderr, "Could not acquire pid file\n");
5212 exit(1);
5213 }
5214
bellardff3fbb32006-01-08 10:53:14 +00005215#ifdef USE_KQEMU
5216 if (smp_cpus > 1)
5217 kqemu_allowed = 0;
5218#endif
bellarda20dd502003-09-30 21:07:02 +00005219 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00005220 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00005221
j_mayer28c5af52007-11-11 01:50:45 +00005222 if (!linux_boot && net_boot == 0 &&
blueswir1f88e4b92008-08-12 15:58:35 +00005223 !machine->nodisk_ok && nb_drives_opt == 0)
ths15f82202007-06-29 23:26:08 +00005224 help(1);
bellard0824d6f2003-06-24 13:42:40 +00005225
thsf8d39c02008-07-03 10:01:15 +00005226 if (!linux_boot && *kernel_cmdline != '\0') {
5227 fprintf(stderr, "-append only allowed with -kernel option\n");
5228 exit(1);
5229 }
5230
5231 if (!linux_boot && initrd_filename != NULL) {
5232 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5233 exit(1);
5234 }
5235
ths96d30e42007-01-07 20:42:14 +00005236 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00005237 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00005238 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00005239 }
bellardb118d612003-06-30 23:36:21 +00005240 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005241
pbrook634fce92006-07-15 17:40:09 +00005242 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005243 if (init_timer_alarm() < 0) {
5244 fprintf(stderr, "could not initialize alarm timer\n");
5245 exit(1);
5246 }
pbrook2e70f6e2008-06-29 01:03:05 +00005247 if (use_icount && icount_time_shift < 0) {
5248 use_icount = 2;
5249 /* 125MIPS seems a reasonable initial guess at the guest speed.
5250 It will be corrected fairly quickly anyway. */
5251 icount_time_shift = 3;
5252 init_icount_adjust();
5253 }
pbrook634fce92006-07-15 17:40:09 +00005254
bellardfd1dff42006-02-01 21:29:26 +00005255#ifdef _WIN32
5256 socket_init();
5257#endif
5258
bellard7c9d8e02005-11-15 22:16:05 +00005259 /* init network clients */
5260 if (nb_net_clients == 0) {
5261 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005262 net_clients[nb_net_clients++] = "nic";
5263#ifdef CONFIG_SLIRP
5264 net_clients[nb_net_clients++] = "user";
5265#endif
bellardc20709a2004-04-21 23:27:19 +00005266 }
5267
bellard7c9d8e02005-11-15 22:16:05 +00005268 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005269 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005270 exit(1);
bellard702c6512004-04-02 21:21:32 +00005271 }
aliguori63a01ef2008-10-31 19:10:00 +00005272 net_client_check();
bellardf1510b22003-06-25 00:07:40 +00005273
thseec85c22007-01-05 17:41:07 +00005274#ifdef TARGET_I386
balroged494d82007-12-11 23:23:52 +00005275 /* XXX: this should be moved in the PC machine instantiation code */
j_mayer28c5af52007-11-11 01:50:45 +00005276 if (net_boot != 0) {
5277 int netroms = 0;
5278 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00005279 const char *model = nd_table[i].model;
5280 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00005281 if (net_boot & (1 << i)) {
5282 if (model == NULL)
5283 model = "ne2k_pci";
5284 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5285 if (get_image_size(buf) > 0) {
5286 if (nb_option_roms >= MAX_OPTION_ROMS) {
5287 fprintf(stderr, "Too many option ROMs\n");
5288 exit(1);
5289 }
5290 option_rom[nb_option_roms] = strdup(buf);
5291 nb_option_roms++;
5292 netroms++;
5293 }
5294 }
thseec85c22007-01-05 17:41:07 +00005295 }
j_mayer28c5af52007-11-11 01:50:45 +00005296 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00005297 fprintf(stderr, "No valid PXE rom found for network device\n");
5298 exit(1);
5299 }
thseec85c22007-01-05 17:41:07 +00005300 }
5301#endif
5302
balrogdc72ac12008-11-09 00:04:26 +00005303 /* init the bluetooth world */
5304 for (i = 0; i < nb_bt_opts; i++)
5305 if (bt_parse(bt_opts[i]))
5306 exit(1);
5307
bellard0824d6f2003-06-24 13:42:40 +00005308 /* init the memory */
balrog7fb4fdc2008-04-24 17:59:27 +00005309 phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
5310
5311 if (machine->ram_require & RAMSIZE_FIXED) {
5312 if (ram_size > 0) {
5313 if (ram_size < phys_ram_size) {
aurel32cd940062008-04-28 20:26:54 +00005314 fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
5315 machine->name, (unsigned long long) phys_ram_size);
balrog7fb4fdc2008-04-24 17:59:27 +00005316 exit(-1);
5317 }
5318
5319 phys_ram_size = ram_size;
5320 } else
5321 ram_size = phys_ram_size;
5322 } else {
aurel324fc5d072008-04-27 21:39:40 +00005323 if (ram_size == 0)
balrog7fb4fdc2008-04-24 17:59:27 +00005324 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5325
5326 phys_ram_size += ram_size;
5327 }
ths9ae02552007-01-05 17:39:04 +00005328
bellardd993e022005-02-10 22:00:06 +00005329 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00005330 if (!phys_ram_base) {
5331 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00005332 exit(1);
5333 }
5334
bellard26a5f132008-05-28 12:30:31 +00005335 /* init the dynamic translator */
5336 cpu_exec_init_all(tb_size * 1024 * 1024);
5337
bellard5905b2e2004-08-01 21:53:26 +00005338 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005339
5340 /* we always create the cdrom drive, even if no disk is there */
5341
5342 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005343 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005344
balrog9d413d12007-12-04 00:10:34 +00005345 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005346
5347 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005348 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005349
balrog9d413d12007-12-04 00:10:34 +00005350 /* we always create one sd slot, even if no card is in it */
5351
5352 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005353 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005354
ths96d30e42007-01-07 20:42:14 +00005355 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005356
thse4bcb142007-12-02 04:51:10 +00005357 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005358 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005359 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005360
bellardc88676f2006-08-06 13:36:11 +00005361 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005362 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005363
bellard313aa562003-08-10 21:52:11 +00005364 /* terminal init */
ths740733b2007-06-08 01:57:56 +00005365 memset(&display_state, 0, sizeof(display_state));
bellarda20dd502003-09-30 21:07:02 +00005366 if (nographic) {
balrog4d3b6f62008-02-10 16:33:14 +00005367 if (curses) {
5368 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5369 exit(1);
5370 }
ths2ff89792007-06-21 23:34:19 +00005371 /* nearly nothing to do */
5372 dumb_display_init(ds);
ths73fc9742006-12-22 02:09:07 +00005373 } else if (vnc_display != NULL) {
ths71cab5c2007-08-25 01:35:38 +00005374 vnc_display_init(ds);
5375 if (vnc_display_open(ds, vnc_display) < 0)
5376 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00005377 } else
5378#if defined(CONFIG_CURSES)
5379 if (curses) {
5380 curses_display_init(ds, full_screen);
5381 } else
5382#endif
5383 {
bellard5b0753e2005-03-01 21:37:28 +00005384#if defined(CONFIG_SDL)
ths43523e92007-02-18 18:19:32 +00005385 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00005386#elif defined(CONFIG_COCOA)
5387 cocoa_display_init(ds, full_screen);
pbrook67276f52007-11-15 19:04:08 +00005388#else
5389 dumb_display_init(ds);
bellard313aa562003-08-10 21:52:11 +00005390#endif
5391 }
bellard0824d6f2003-06-24 13:42:40 +00005392
aliguori5b08fc12008-08-21 20:08:03 +00005393#ifndef _WIN32
5394 /* must be after terminal init, SDL library changes signal handlers */
5395 termsig_setup();
5396#endif
5397
ths20d8a3e2007-02-18 17:04:49 +00005398 /* Maintain compatibility with multiple stdio monitors */
5399 if (!strcmp(monitor_device,"stdio")) {
5400 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
pbrookfd5f3932008-03-26 20:55:43 +00005401 const char *devname = serial_devices[i];
5402 if (devname && !strcmp(devname,"mon:stdio")) {
5403 monitor_device = NULL;
ths20d8a3e2007-02-18 17:04:49 +00005404 break;
pbrookfd5f3932008-03-26 20:55:43 +00005405 } else if (devname && !strcmp(devname,"stdio")) {
5406 monitor_device = NULL;
5407 serial_devices[i] = "mon:stdio";
ths20d8a3e2007-02-18 17:04:49 +00005408 break;
5409 }
5410 }
bellard82c643f2004-07-14 17:28:13 +00005411 }
pbrookfd5f3932008-03-26 20:55:43 +00005412 if (monitor_device) {
aliguori5ccfae12008-10-31 17:31:29 +00005413 monitor_hd = qemu_chr_open("monitor", monitor_device);
ths20d8a3e2007-02-18 17:04:49 +00005414 if (!monitor_hd) {
5415 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5416 exit(1);
5417 }
5418 monitor_init(monitor_hd, !nographic);
5419 }
bellard82c643f2004-07-14 17:28:13 +00005420
bellard8d11df92004-08-24 21:13:40 +00005421 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005422 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005423 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005424 char label[32];
5425 snprintf(label, sizeof(label), "serial%d", i);
5426 serial_hds[i] = qemu_chr_open(label, devname);
bellard8d11df92004-08-24 21:13:40 +00005427 if (!serial_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00005428 fprintf(stderr, "qemu: could not open serial device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00005429 devname);
bellard8d11df92004-08-24 21:13:40 +00005430 exit(1);
5431 }
thsaf3a9032007-07-11 23:14:59 +00005432 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005433 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005434 }
bellard82c643f2004-07-14 17:28:13 +00005435 }
bellard82c643f2004-07-14 17:28:13 +00005436
bellard6508fe52005-01-15 12:02:56 +00005437 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005438 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005439 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005440 char label[32];
5441 snprintf(label, sizeof(label), "parallel%d", i);
5442 parallel_hds[i] = qemu_chr_open(label, devname);
bellard6508fe52005-01-15 12:02:56 +00005443 if (!parallel_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00005444 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00005445 devname);
bellard6508fe52005-01-15 12:02:56 +00005446 exit(1);
5447 }
thsaf3a9032007-07-11 23:14:59 +00005448 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005449 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005450 }
5451 }
5452
aliguori7ba1e612008-11-05 16:04:33 +00005453 if (kvm_enabled()) {
5454 int ret;
5455
5456 ret = kvm_init(smp_cpus);
5457 if (ret < 0) {
5458 fprintf(stderr, "failed to initialize KVM\n");
5459 exit(1);
5460 }
5461 }
5462
blueswir1b881c2c2007-11-18 08:46:58 +00005463 machine->init(ram_size, vga_ram_size, boot_devices, ds,
j_mayer94fc95c2007-03-05 19:44:02 +00005464 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
bellard73332e52004-04-04 20:22:28 +00005465
aliguorif5d6f512008-12-15 22:20:42 +00005466 /* Set KVM's vcpu state to qemu's initial CPUState. */
5467 if (kvm_enabled()) {
5468 int ret;
5469
5470 ret = kvm_sync_vcpus();
5471 if (ret < 0) {
5472 fprintf(stderr, "failed to initialize vcpus\n");
5473 exit(1);
5474 }
5475 }
5476
pbrook0d92ed32006-05-21 16:30:15 +00005477 /* init USB devices */
5478 if (usb_enabled) {
5479 for(i = 0; i < usb_devices_index; i++) {
5480 if (usb_device_add(usb_devices[i]) < 0) {
5481 fprintf(stderr, "Warning: could not add USB device %s\n",
5482 usb_devices[i]);
5483 }
5484 }
5485 }
5486
ths740733b2007-06-08 01:57:56 +00005487 if (display_state.dpy_refresh) {
5488 display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
5489 qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
5490 }
bellard7f7f9872003-10-30 01:11:23 +00005491
bellard67b915a2004-03-31 23:37:16 +00005492#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00005493 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00005494 /* XXX: use standard host:port notation and modify options
5495 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00005496 if (gdbserver_start(gdbstub_port) < 0) {
5497 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00005498 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00005499 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00005500 }
balrog45669e02007-07-02 13:20:17 +00005501 }
bellard67b915a2004-03-31 23:37:16 +00005502#endif
balrog45669e02007-07-02 13:20:17 +00005503
bellardd63d3072004-10-03 13:29:03 +00005504 if (loadvm)
bellardfaea38e2006-08-05 21:31:00 +00005505 do_loadvm(loadvm);
bellardd63d3072004-10-03 13:29:03 +00005506
aliguori5bb79102008-10-13 03:12:02 +00005507 if (incoming) {
5508 autostart = 0; /* fixme how to deal with -daemonize */
5509 qemu_start_incoming_migration(incoming);
5510 }
5511
bellard67b915a2004-03-31 23:37:16 +00005512 {
bellard5905b2e2004-08-01 21:53:26 +00005513 /* XXX: simplify init */
aliguori83ab7952008-08-19 14:44:22 +00005514 read_passwords();
pbrook3c07f8e2007-01-21 16:47:01 +00005515 if (autostart) {
bellard5905b2e2004-08-01 21:53:26 +00005516 vm_start();
5517 }
bellard0824d6f2003-06-24 13:42:40 +00005518 }
thsffd843b2006-12-21 19:46:43 +00005519
ths71e3ceb2006-12-22 02:11:31 +00005520 if (daemonize) {
5521 uint8_t status = 0;
5522 ssize_t len;
5523 int fd;
5524
5525 again1:
5526 len = write(fds[1], &status, 1);
5527 if (len == -1 && (errno == EINTR))
5528 goto again1;
5529
5530 if (len != 1)
5531 exit(1);
5532
aliguoribd54b862008-07-23 00:58:33 +00005533 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005534 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005535 if (fd == -1)
5536 exit(1);
5537
5538 dup2(fd, 0);
5539 dup2(fd, 1);
5540 dup2(fd, 2);
5541
5542 close(fd);
5543 }
5544
bellard8a7ddc32004-03-31 19:00:16 +00005545 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005546 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005547 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005548
bellard0824d6f2003-06-24 13:42:40 +00005549 return 0;
5550}