blob: e29072b5f94313928839b57d30676c93ad30911c [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
bellard0824d6f2003-06-24 13:42:40 +0000152//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000153//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000154//#define DEBUG_NET
155//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000156
bellard77d4bc32004-05-26 22:13:53 +0000157#ifdef TARGET_PPC
158#define DEFAULT_RAM_SIZE 144
159#else
bellard1bfe8562004-07-08 21:17:50 +0000160#define DEFAULT_RAM_SIZE 128
bellard77d4bc32004-05-26 22:13:53 +0000161#endif
bellard313aa562003-08-10 21:52:11 +0000162
pbrook0d92ed32006-05-21 16:30:15 +0000163/* Max number of USB devices that can be specified on the commandline. */
164#define MAX_USB_CMDLINE 8
165
balrogdc72ac12008-11-09 00:04:26 +0000166/* Max number of bluetooth switches on the commandline. */
167#define MAX_BT_CMDLINE 10
168
bellard7dea1da2003-11-16 15:59:30 +0000169/* XXX: use a two level table to limit memory usage */
170#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000171
bellard80cabfa2004-03-14 12:20:30 +0000172const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000173const char *bios_name = NULL;
blueswir1dbed7e42008-10-01 19:38:09 +0000174static void *ioport_opaque[MAX_IOPORTS];
175static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
176static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
thse4bcb142007-12-02 04:51:10 +0000177/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000178 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000179DriveInfo drives_table[MAX_DRIVES+1];
180int nb_drives;
blueswir1dbed7e42008-10-01 19:38:09 +0000181static int vga_ram_size;
malccb5a7aa2008-09-28 00:42:12 +0000182enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori0e82f342008-10-31 18:44:40 +0000183DisplayState display_state;
bellarda20dd502003-09-30 21:07:02 +0000184int nographic;
blueswir1dbed7e42008-10-01 19:38:09 +0000185static int curses;
bellard3d11d0e2004-12-12 16:56:30 +0000186const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000187int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000188ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000189int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000190NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000191int vm_running;
balrogf6503052008-02-17 11:42:19 +0000192static int rtc_utc = 1;
193static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000194int cirrus_vga_enabled = 1;
thsd34cab92007-04-02 01:10:46 +0000195int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000196#ifdef TARGET_SPARC
197int graphic_width = 1024;
198int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000199int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000200#else
bellard1bfe8562004-07-08 21:17:50 +0000201int graphic_width = 800;
202int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000203int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000204#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000205static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000206#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000207static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000208#endif
ths667acca2006-12-11 02:08:05 +0000209int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000210CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000211CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
bellarda09db212005-04-30 16:10:35 +0000212#ifdef TARGET_I386
213int win2k_install_hack = 0;
214#endif
bellardbb36d472005-11-05 14:22:28 +0000215int usb_enabled = 0;
bellard6a00d602005-11-21 23:25:50 +0000216int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000217const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000218int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000219int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000220int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000221int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000222int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000223int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000224int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000225int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000226const char *option_rom[MAX_OPTION_ROMS];
227int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000228int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000229#ifdef TARGET_ARM
230int old_param = 0;
231#endif
thsc35734b2007-03-19 15:17:08 +0000232const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000233int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000234#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000235unsigned int nb_prom_envs = 0;
236const char *prom_envs[MAX_PROM_ENVS];
237#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000238static int nb_drives_opt;
239static struct drive_opt {
balrog609497a2008-01-14 02:56:53 +0000240 const char *file;
241 char opt[1024];
242} drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000243
balrogee5605e2007-12-03 03:01:40 +0000244static CPUState *cur_cpu;
245static CPUState *next_cpu;
balrog76ea08f2007-12-16 11:48:54 +0000246static int event_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000247/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000248static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000249/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000250#define MAX_ICOUNT_SHIFT 10
251/* Compensate for varying guest execution speed. */
252static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000253static QEMUTimer *icount_rt_timer;
254static QEMUTimer *icount_vm_timer;
balrogee5605e2007-12-03 03:01:40 +0000255
blueswir18fcb1b92008-09-18 18:29:08 +0000256uint8_t qemu_uuid[16];
257
bellard0824d6f2003-06-24 13:42:40 +0000258/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000259/* x86 ISA bus support */
260
261target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000262PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000263
aliguori477e3ed2008-07-23 15:19:59 +0000264static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
265static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
266
267static uint32_t ioport_read(int index, uint32_t address)
268{
269 static IOPortReadFunc *default_func[3] = {
270 default_ioport_readb,
271 default_ioport_readw,
272 default_ioport_readl
273 };
274 IOPortReadFunc *func = ioport_read_table[index][address];
275 if (!func)
276 func = default_func[index];
277 return func(ioport_opaque[address], address);
278}
279
280static void ioport_write(int index, uint32_t address, uint32_t data)
281{
282 static IOPortWriteFunc *default_func[3] = {
283 default_ioport_writeb,
284 default_ioport_writew,
285 default_ioport_writel
286 };
287 IOPortWriteFunc *func = ioport_write_table[index][address];
288 if (!func)
289 func = default_func[index];
290 func(ioport_opaque[address], address, data);
291}
292
pbrook9596ebb2007-11-18 01:44:38 +0000293static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000294{
295#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000296 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000297#endif
bellardfc01f7e2003-06-30 10:03:06 +0000298 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000299}
300
pbrook9596ebb2007-11-18 01:44:38 +0000301static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000302{
303#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000304 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000305#endif
306}
307
308/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000309static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000310{
311 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000312 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000313 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000314 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000315 return data;
316}
317
pbrook9596ebb2007-11-18 01:44:38 +0000318static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000319{
aliguori477e3ed2008-07-23 15:19:59 +0000320 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000321 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000322 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000323}
324
pbrook9596ebb2007-11-18 01:44:38 +0000325static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000326{
327#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000328 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000329#endif
330 return 0xffffffff;
331}
332
pbrook9596ebb2007-11-18 01:44:38 +0000333static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000334{
335#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000336 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000337#endif
bellard0824d6f2003-06-24 13:42:40 +0000338}
339
bellardfc01f7e2003-06-30 10:03:06 +0000340/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000341int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000342 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000343{
bellardfc01f7e2003-06-30 10:03:06 +0000344 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000345
bellardc4b1fcc2004-03-14 21:44:30 +0000346 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000347 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000348 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000349 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000350 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000351 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000352 } else {
balrog88fdf562008-04-26 21:11:22 +0000353 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000354 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000355 }
356 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000357 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000358 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000359 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000360 ioport_opaque[i] = opaque;
361 }
bellard0824d6f2003-06-24 13:42:40 +0000362 return 0;
363}
364
bellardfc01f7e2003-06-30 10:03:06 +0000365/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000366int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000367 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000368{
bellardfc01f7e2003-06-30 10:03:06 +0000369 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000370
bellardc4b1fcc2004-03-14 21:44:30 +0000371 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000372 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000373 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000374 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000375 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000376 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000377 } else {
balrog88fdf562008-04-26 21:11:22 +0000378 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000379 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000380 }
381 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000382 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000383 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
384 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000385 ioport_opaque[i] = opaque;
386 }
bellardf1510b22003-06-25 00:07:40 +0000387 return 0;
388}
389
bellard69b91032004-05-18 23:05:28 +0000390void isa_unassign_ioport(int start, int length)
391{
392 int i;
393
394 for(i = start; i < start + length; i++) {
395 ioport_read_table[0][i] = default_ioport_readb;
396 ioport_read_table[1][i] = default_ioport_readw;
397 ioport_read_table[2][i] = default_ioport_readl;
398
399 ioport_write_table[0][i] = default_ioport_writeb;
400 ioport_write_table[1][i] = default_ioport_writew;
401 ioport_write_table[2][i] = default_ioport_writel;
402 }
403}
404
bellard20f32282005-01-03 23:36:21 +0000405/***********************************************************/
406
bellardc45886d2004-01-05 00:02:06 +0000407void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000408{
bellardfd872592004-05-12 19:11:15 +0000409#ifdef DEBUG_IOPORT
410 if (loglevel & CPU_LOG_IOPORT)
411 fprintf(logfile, "outb: %04x %02x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000412#endif
aliguori477e3ed2008-07-23 15:19:59 +0000413 ioport_write(0, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000414#ifdef USE_KQEMU
415 if (env)
416 env->last_io_time = cpu_get_time_fast();
417#endif
bellard0824d6f2003-06-24 13:42:40 +0000418}
419
bellardc45886d2004-01-05 00:02:06 +0000420void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000421{
bellardfd872592004-05-12 19:11:15 +0000422#ifdef DEBUG_IOPORT
423 if (loglevel & CPU_LOG_IOPORT)
424 fprintf(logfile, "outw: %04x %04x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000425#endif
aliguori477e3ed2008-07-23 15:19:59 +0000426 ioport_write(1, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000427#ifdef USE_KQEMU
428 if (env)
429 env->last_io_time = cpu_get_time_fast();
430#endif
bellard0824d6f2003-06-24 13:42:40 +0000431}
432
bellardc45886d2004-01-05 00:02:06 +0000433void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000434{
bellardfd872592004-05-12 19:11:15 +0000435#ifdef DEBUG_IOPORT
436 if (loglevel & CPU_LOG_IOPORT)
437 fprintf(logfile, "outl: %04x %08x\n", addr, val);
438#endif
aliguori477e3ed2008-07-23 15:19:59 +0000439 ioport_write(2, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000440#ifdef USE_KQEMU
441 if (env)
442 env->last_io_time = cpu_get_time_fast();
443#endif
bellard0824d6f2003-06-24 13:42:40 +0000444}
445
bellardc45886d2004-01-05 00:02:06 +0000446int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000447{
bellardfd872592004-05-12 19:11:15 +0000448 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000449 val = ioport_read(0, addr);
bellardfd872592004-05-12 19:11:15 +0000450#ifdef DEBUG_IOPORT
451 if (loglevel & CPU_LOG_IOPORT)
452 fprintf(logfile, "inb : %04x %02x\n", addr, val);
453#endif
bellard89bfc102006-02-08 22:46:31 +0000454#ifdef USE_KQEMU
455 if (env)
456 env->last_io_time = cpu_get_time_fast();
457#endif
bellardfd872592004-05-12 19:11:15 +0000458 return val;
bellard0824d6f2003-06-24 13:42:40 +0000459}
460
bellardc45886d2004-01-05 00:02:06 +0000461int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000462{
bellardfd872592004-05-12 19:11:15 +0000463 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000464 val = ioport_read(1, addr);
bellardfd872592004-05-12 19:11:15 +0000465#ifdef DEBUG_IOPORT
466 if (loglevel & CPU_LOG_IOPORT)
467 fprintf(logfile, "inw : %04x %04x\n", addr, val);
468#endif
bellard89bfc102006-02-08 22:46:31 +0000469#ifdef USE_KQEMU
470 if (env)
471 env->last_io_time = cpu_get_time_fast();
472#endif
bellardfd872592004-05-12 19:11:15 +0000473 return val;
bellard0824d6f2003-06-24 13:42:40 +0000474}
475
bellardc45886d2004-01-05 00:02:06 +0000476int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000477{
bellardfd872592004-05-12 19:11:15 +0000478 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000479 val = ioport_read(2, addr);
bellardfd872592004-05-12 19:11:15 +0000480#ifdef DEBUG_IOPORT
481 if (loglevel & CPU_LOG_IOPORT)
482 fprintf(logfile, "inl : %04x %08x\n", addr, val);
483#endif
bellard89bfc102006-02-08 22:46:31 +0000484#ifdef USE_KQEMU
485 if (env)
486 env->last_io_time = cpu_get_time_fast();
487#endif
bellardfd872592004-05-12 19:11:15 +0000488 return val;
bellard0824d6f2003-06-24 13:42:40 +0000489}
490
491/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000492void hw_error(const char *fmt, ...)
493{
494 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000495 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000496
497 va_start(ap, fmt);
498 fprintf(stderr, "qemu: hardware error: ");
499 vfprintf(stderr, fmt, ap);
500 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000501 for(env = first_cpu; env != NULL; env = env->next_cpu) {
502 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000503#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000504 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000505#else
bellard6a00d602005-11-21 23:25:50 +0000506 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000507#endif
bellard6a00d602005-11-21 23:25:50 +0000508 }
bellard0824d6f2003-06-24 13:42:40 +0000509 va_end(ap);
510 abort();
511}
aliguoridf751fa2008-12-04 20:19:35 +0000512
513/***************/
514/* ballooning */
515
516static QEMUBalloonEvent *qemu_balloon_event;
517void *qemu_balloon_event_opaque;
518
519void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
520{
521 qemu_balloon_event = func;
522 qemu_balloon_event_opaque = opaque;
523}
524
525void qemu_balloon(ram_addr_t target)
526{
527 if (qemu_balloon_event)
528 qemu_balloon_event(qemu_balloon_event_opaque, target);
529}
530
531ram_addr_t qemu_balloon_status(void)
532{
533 if (qemu_balloon_event)
534 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
535 return 0;
536}
bellard0824d6f2003-06-24 13:42:40 +0000537
bellard8a7ddc32004-03-31 19:00:16 +0000538/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000539/* keyboard/mouse */
540
541static QEMUPutKBDEvent *qemu_put_kbd_event;
542static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000543static QEMUPutMouseEntry *qemu_put_mouse_event_head;
544static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000545
546void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
547{
548 qemu_put_kbd_event_opaque = opaque;
549 qemu_put_kbd_event = func;
550}
551
ths455204e2007-01-05 16:42:13 +0000552QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
553 void *opaque, int absolute,
554 const char *name)
bellard63066f42004-06-03 18:45:02 +0000555{
ths455204e2007-01-05 16:42:13 +0000556 QEMUPutMouseEntry *s, *cursor;
557
558 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
559 if (!s)
560 return NULL;
561
562 s->qemu_put_mouse_event = func;
563 s->qemu_put_mouse_event_opaque = opaque;
564 s->qemu_put_mouse_event_absolute = absolute;
565 s->qemu_put_mouse_event_name = qemu_strdup(name);
566 s->next = NULL;
567
568 if (!qemu_put_mouse_event_head) {
569 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
570 return s;
571 }
572
573 cursor = qemu_put_mouse_event_head;
574 while (cursor->next != NULL)
575 cursor = cursor->next;
576
577 cursor->next = s;
578 qemu_put_mouse_event_current = s;
579
580 return s;
581}
582
583void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
584{
585 QEMUPutMouseEntry *prev = NULL, *cursor;
586
587 if (!qemu_put_mouse_event_head || entry == NULL)
588 return;
589
590 cursor = qemu_put_mouse_event_head;
591 while (cursor != NULL && cursor != entry) {
592 prev = cursor;
593 cursor = cursor->next;
594 }
595
596 if (cursor == NULL) // does not exist or list empty
597 return;
598 else if (prev == NULL) { // entry is head
599 qemu_put_mouse_event_head = cursor->next;
600 if (qemu_put_mouse_event_current == entry)
601 qemu_put_mouse_event_current = cursor->next;
602 qemu_free(entry->qemu_put_mouse_event_name);
603 qemu_free(entry);
604 return;
605 }
606
607 prev->next = entry->next;
608
609 if (qemu_put_mouse_event_current == entry)
610 qemu_put_mouse_event_current = prev;
611
612 qemu_free(entry->qemu_put_mouse_event_name);
613 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000614}
615
616void kbd_put_keycode(int keycode)
617{
618 if (qemu_put_kbd_event) {
619 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
620 }
621}
622
623void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
624{
ths455204e2007-01-05 16:42:13 +0000625 QEMUPutMouseEvent *mouse_event;
626 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000627 int width;
ths455204e2007-01-05 16:42:13 +0000628
629 if (!qemu_put_mouse_event_current) {
630 return;
631 }
632
633 mouse_event =
634 qemu_put_mouse_event_current->qemu_put_mouse_event;
635 mouse_event_opaque =
636 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
637
638 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000639 if (graphic_rotate) {
640 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
641 width = 0x7fff;
642 else
aurel32b94ed572008-03-10 19:34:27 +0000643 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000644 mouse_event(mouse_event_opaque,
645 width - dy, dx, dz, buttons_state);
646 } else
647 mouse_event(mouse_event_opaque,
648 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000649 }
650}
651
bellard09b26c52006-04-12 21:09:08 +0000652int kbd_mouse_is_absolute(void)
653{
ths455204e2007-01-05 16:42:13 +0000654 if (!qemu_put_mouse_event_current)
655 return 0;
656
657 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
658}
659
660void do_info_mice(void)
661{
662 QEMUPutMouseEntry *cursor;
663 int index = 0;
664
665 if (!qemu_put_mouse_event_head) {
666 term_printf("No mouse devices connected\n");
667 return;
668 }
669
670 term_printf("Mouse devices available:\n");
671 cursor = qemu_put_mouse_event_head;
672 while (cursor != NULL) {
673 term_printf("%c Mouse #%d: %s\n",
674 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
675 index, cursor->qemu_put_mouse_event_name);
676 index++;
677 cursor = cursor->next;
678 }
679}
680
681void do_mouse_set(int index)
682{
683 QEMUPutMouseEntry *cursor;
684 int i = 0;
685
686 if (!qemu_put_mouse_event_head) {
687 term_printf("No mouse devices connected\n");
688 return;
689 }
690
691 cursor = qemu_put_mouse_event_head;
692 while (cursor != NULL && index != i) {
693 i++;
694 cursor = cursor->next;
695 }
696
697 if (cursor != NULL)
698 qemu_put_mouse_event_current = cursor;
699 else
700 term_printf("Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000701}
702
bellard87858c82003-06-27 12:01:39 +0000703/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000704uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000705{
706 union {
707 uint64_t ll;
708 struct {
709#ifdef WORDS_BIGENDIAN
710 uint32_t high, low;
711#else
712 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000713#endif
bellard87858c82003-06-27 12:01:39 +0000714 } l;
715 } u, res;
716 uint64_t rl, rh;
717
718 u.ll = a;
719 rl = (uint64_t)u.l.low * (uint64_t)b;
720 rh = (uint64_t)u.l.high * (uint64_t)b;
721 rh += (rl >> 32);
722 res.l.high = rh / c;
723 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
724 return res.ll;
725}
726
bellard1dce7c32006-07-13 23:20:22 +0000727/***********************************************************/
728/* real time host monotonic timer */
729
730#define QEMU_TIMER_BASE 1000000000LL
731
732#ifdef WIN32
733
734static int64_t clock_freq;
735
736static void init_get_clock(void)
737{
bellarda8e5ac32006-07-14 09:36:13 +0000738 LARGE_INTEGER freq;
739 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000740 ret = QueryPerformanceFrequency(&freq);
741 if (ret == 0) {
742 fprintf(stderr, "Could not calibrate ticks\n");
743 exit(1);
744 }
745 clock_freq = freq.QuadPart;
746}
747
748static int64_t get_clock(void)
749{
750 LARGE_INTEGER ti;
751 QueryPerformanceCounter(&ti);
752 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
753}
754
755#else
756
757static int use_rt_clock;
758
759static void init_get_clock(void)
760{
761 use_rt_clock = 0;
aurel3260759372008-10-13 21:08:25 +0000762#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000763 {
764 struct timespec ts;
765 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
766 use_rt_clock = 1;
767 }
768 }
769#endif
770}
771
772static int64_t get_clock(void)
773{
aurel3260759372008-10-13 21:08:25 +0000774#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000775 if (use_rt_clock) {
776 struct timespec ts;
777 clock_gettime(CLOCK_MONOTONIC, &ts);
778 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000779 } else
bellard1dce7c32006-07-13 23:20:22 +0000780#endif
781 {
782 /* XXX: using gettimeofday leads to problems if the date
783 changes, so it should be avoided. */
784 struct timeval tv;
785 gettimeofday(&tv, NULL);
786 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
787 }
788}
bellard1dce7c32006-07-13 23:20:22 +0000789#endif
790
pbrook2e70f6e2008-06-29 01:03:05 +0000791/* Return the virtual CPU time, based on the instruction counter. */
792static int64_t cpu_get_icount(void)
793{
794 int64_t icount;
795 CPUState *env = cpu_single_env;;
796 icount = qemu_icount;
797 if (env) {
798 if (!can_do_io(env))
799 fprintf(stderr, "Bad clock read\n");
800 icount -= (env->icount_decr.u16.low + env->icount_extra);
801 }
802 return qemu_icount_bias + (icount << icount_time_shift);
803}
804
bellard1dce7c32006-07-13 23:20:22 +0000805/***********************************************************/
806/* guest cycle counter */
807
808static int64_t cpu_ticks_prev;
809static int64_t cpu_ticks_offset;
810static int64_t cpu_clock_offset;
811static int cpu_ticks_enabled;
812
813/* return the host CPU cycle counter and handle stop/restart */
814int64_t cpu_get_ticks(void)
815{
pbrook2e70f6e2008-06-29 01:03:05 +0000816 if (use_icount) {
817 return cpu_get_icount();
818 }
bellard1dce7c32006-07-13 23:20:22 +0000819 if (!cpu_ticks_enabled) {
820 return cpu_ticks_offset;
821 } else {
822 int64_t ticks;
823 ticks = cpu_get_real_ticks();
824 if (cpu_ticks_prev > ticks) {
825 /* Note: non increasing ticks may happen if the host uses
826 software suspend */
827 cpu_ticks_offset += cpu_ticks_prev - ticks;
828 }
829 cpu_ticks_prev = ticks;
830 return ticks + cpu_ticks_offset;
831 }
832}
833
834/* return the host CPU monotonic timer and handle stop/restart */
835static int64_t cpu_get_clock(void)
836{
837 int64_t ti;
838 if (!cpu_ticks_enabled) {
839 return cpu_clock_offset;
840 } else {
841 ti = get_clock();
842 return ti + cpu_clock_offset;
843 }
844}
845
846/* enable cpu_get_ticks() */
847void cpu_enable_ticks(void)
848{
849 if (!cpu_ticks_enabled) {
850 cpu_ticks_offset -= cpu_get_real_ticks();
851 cpu_clock_offset -= get_clock();
852 cpu_ticks_enabled = 1;
853 }
854}
855
856/* disable cpu_get_ticks() : the clock is stopped. You must not call
857 cpu_get_ticks() after that. */
858void cpu_disable_ticks(void)
859{
860 if (cpu_ticks_enabled) {
861 cpu_ticks_offset = cpu_get_ticks();
862 cpu_clock_offset = cpu_get_clock();
863 cpu_ticks_enabled = 0;
864 }
865}
866
867/***********************************************************/
868/* timers */
ths5fafdf22007-09-16 21:08:06 +0000869
bellard8a7ddc32004-03-31 19:00:16 +0000870#define QEMU_TIMER_REALTIME 0
871#define QEMU_TIMER_VIRTUAL 1
872
873struct QEMUClock {
874 int type;
875 /* XXX: add frequency */
876};
877
878struct QEMUTimer {
879 QEMUClock *clock;
880 int64_t expire_time;
881 QEMUTimerCB *cb;
882 void *opaque;
883 struct QEMUTimer *next;
884};
885
thsc8994012007-08-19 21:56:03 +0000886struct qemu_alarm_timer {
887 char const *name;
thsefe75412007-08-24 01:36:32 +0000888 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000889
890 int (*start)(struct qemu_alarm_timer *t);
891 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000892 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000893 void *priv;
894};
895
thsefe75412007-08-24 01:36:32 +0000896#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000897#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000898
899static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
900{
901 return t->flags & ALARM_FLAG_DYNTICKS;
902}
903
904static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
905{
906 if (!alarm_has_dynticks(t))
907 return;
908
909 t->rearm(t);
910}
911
912/* TODO: MIN_TIMER_REARM_US should be optimized */
913#define MIN_TIMER_REARM_US 250
914
thsc8994012007-08-19 21:56:03 +0000915static struct qemu_alarm_timer *alarm_timer;
aliguorif49e58d2008-11-05 21:22:34 +0000916#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +0000917static int alarm_timer_rfd, alarm_timer_wfd;
aliguorif49e58d2008-11-05 21:22:34 +0000918#endif
thsc8994012007-08-19 21:56:03 +0000919
920#ifdef _WIN32
921
922struct qemu_alarm_win32 {
923 MMRESULT timerId;
924 HANDLE host_alarm;
925 unsigned int period;
926} alarm_win32_data = {0, NULL, -1};
927
928static int win32_start_timer(struct qemu_alarm_timer *t);
929static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000930static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000931
932#else
933
934static int unix_start_timer(struct qemu_alarm_timer *t);
935static void unix_stop_timer(struct qemu_alarm_timer *t);
936
ths231c6582007-08-26 17:29:15 +0000937#ifdef __linux__
938
thsefe75412007-08-24 01:36:32 +0000939static int dynticks_start_timer(struct qemu_alarm_timer *t);
940static void dynticks_stop_timer(struct qemu_alarm_timer *t);
941static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
942
thsc40ec5a2007-08-19 22:09:40 +0000943static int hpet_start_timer(struct qemu_alarm_timer *t);
944static void hpet_stop_timer(struct qemu_alarm_timer *t);
945
thsc8994012007-08-19 21:56:03 +0000946static int rtc_start_timer(struct qemu_alarm_timer *t);
947static void rtc_stop_timer(struct qemu_alarm_timer *t);
948
thsefe75412007-08-24 01:36:32 +0000949#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000950
951#endif /* _WIN32 */
952
pbrook2e70f6e2008-06-29 01:03:05 +0000953/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000954 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000955 When the guest is idle real and virtual time will be aligned in
956 the IO wait loop. */
957#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
958
959static void icount_adjust(void)
960{
961 int64_t cur_time;
962 int64_t cur_icount;
963 int64_t delta;
964 static int64_t last_delta;
965 /* If the VM is not running, then do nothing. */
966 if (!vm_running)
967 return;
968
969 cur_time = cpu_get_clock();
970 cur_icount = qemu_get_clock(vm_clock);
971 delta = cur_icount - cur_time;
972 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
973 if (delta > 0
974 && last_delta + ICOUNT_WOBBLE < delta * 2
975 && icount_time_shift > 0) {
976 /* The guest is getting too far ahead. Slow time down. */
977 icount_time_shift--;
978 }
979 if (delta < 0
980 && last_delta - ICOUNT_WOBBLE > delta * 2
981 && icount_time_shift < MAX_ICOUNT_SHIFT) {
982 /* The guest is getting too far behind. Speed time up. */
983 icount_time_shift++;
984 }
985 last_delta = delta;
986 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
987}
988
989static void icount_adjust_rt(void * opaque)
990{
991 qemu_mod_timer(icount_rt_timer,
992 qemu_get_clock(rt_clock) + 1000);
993 icount_adjust();
994}
995
996static void icount_adjust_vm(void * opaque)
997{
998 qemu_mod_timer(icount_vm_timer,
999 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1000 icount_adjust();
1001}
1002
1003static void init_icount_adjust(void)
1004{
1005 /* Have both realtime and virtual time triggers for speed adjustment.
1006 The realtime trigger catches emulated time passing too slowly,
1007 the virtual time trigger catches emulated time passing too fast.
1008 Realtime triggers occur even when idle, so use them less frequently
1009 than VM triggers. */
1010 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1011 qemu_mod_timer(icount_rt_timer,
1012 qemu_get_clock(rt_clock) + 1000);
1013 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1014 qemu_mod_timer(icount_vm_timer,
1015 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1016}
1017
thsc8994012007-08-19 21:56:03 +00001018static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +00001019#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +00001020#ifdef __linux__
thsefe75412007-08-24 01:36:32 +00001021 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1022 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001023 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +00001024 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001025 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +00001026 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001027#endif
thsefe75412007-08-24 01:36:32 +00001028 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001029#else
thsefe75412007-08-24 01:36:32 +00001030 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1031 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1032 {"win32", 0, win32_start_timer,
1033 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +00001034#endif
1035 {NULL, }
1036};
1037
blueswir13f47aa82008-03-09 06:59:01 +00001038static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +00001039{
1040 int i;
1041
1042 printf("Available alarm timers, in order of precedence:\n");
1043 for (i = 0; alarm_timers[i].name; i++)
1044 printf("%s\n", alarm_timers[i].name);
1045}
1046
1047static void configure_alarms(char const *opt)
1048{
1049 int i;
1050 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +00001051 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +00001052 char *arg;
1053 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +00001054 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +00001055
aurel323adda042008-03-09 23:43:49 +00001056 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +00001057 show_available_alarms();
1058 exit(0);
1059 }
1060
1061 arg = strdup(opt);
1062
1063 /* Reorder the array */
1064 name = strtok(arg, ",");
1065 while (name) {
balroge2b577e2007-09-17 21:25:20 +00001066 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +00001067 if (!strcmp(alarm_timers[i].name, name))
1068 break;
1069 }
1070
1071 if (i == count) {
1072 fprintf(stderr, "Unknown clock %s\n", name);
1073 goto next;
1074 }
1075
1076 if (i < cur)
1077 /* Ignore */
1078 goto next;
1079
1080 /* Swap */
1081 tmp = alarm_timers[i];
1082 alarm_timers[i] = alarm_timers[cur];
1083 alarm_timers[cur] = tmp;
1084
1085 cur++;
1086next:
1087 name = strtok(NULL, ",");
1088 }
1089
1090 free(arg);
1091
1092 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +00001093 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +00001094 for (i = cur; i < count; i++)
1095 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +00001096 } else {
1097 show_available_alarms();
1098 exit(1);
thsf3dcfad2007-08-24 01:26:02 +00001099 }
thsf3dcfad2007-08-24 01:26:02 +00001100}
1101
bellard8a7ddc32004-03-31 19:00:16 +00001102QEMUClock *rt_clock;
1103QEMUClock *vm_clock;
1104
1105static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +00001106
pbrook9596ebb2007-11-18 01:44:38 +00001107static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001108{
1109 QEMUClock *clock;
1110 clock = qemu_mallocz(sizeof(QEMUClock));
1111 if (!clock)
1112 return NULL;
1113 clock->type = type;
1114 return clock;
1115}
1116
1117QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1118{
1119 QEMUTimer *ts;
1120
1121 ts = qemu_mallocz(sizeof(QEMUTimer));
1122 ts->clock = clock;
1123 ts->cb = cb;
1124 ts->opaque = opaque;
1125 return ts;
1126}
1127
1128void qemu_free_timer(QEMUTimer *ts)
1129{
1130 qemu_free(ts);
1131}
1132
1133/* stop a timer, but do not dealloc it */
1134void qemu_del_timer(QEMUTimer *ts)
1135{
1136 QEMUTimer **pt, *t;
1137
1138 /* NOTE: this code must be signal safe because
1139 qemu_timer_expired() can be called from a signal. */
1140 pt = &active_timers[ts->clock->type];
1141 for(;;) {
1142 t = *pt;
1143 if (!t)
1144 break;
1145 if (t == ts) {
1146 *pt = t->next;
1147 break;
1148 }
1149 pt = &t->next;
1150 }
1151}
1152
1153/* modify the current timer so that it will be fired when current_time
1154 >= expire_time. The corresponding callback will be called. */
1155void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1156{
1157 QEMUTimer **pt, *t;
1158
1159 qemu_del_timer(ts);
1160
1161 /* add the timer in the sorted list */
1162 /* NOTE: this code must be signal safe because
1163 qemu_timer_expired() can be called from a signal. */
1164 pt = &active_timers[ts->clock->type];
1165 for(;;) {
1166 t = *pt;
1167 if (!t)
1168 break;
ths5fafdf22007-09-16 21:08:06 +00001169 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001170 break;
1171 pt = &t->next;
1172 }
1173 ts->expire_time = expire_time;
1174 ts->next = *pt;
1175 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001176
1177 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001178 if (pt == &active_timers[ts->clock->type]) {
1179 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1180 qemu_rearm_alarm_timer(alarm_timer);
1181 }
1182 /* Interrupt execution to force deadline recalculation. */
1183 if (use_icount && cpu_single_env) {
1184 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1185 }
1186 }
bellard8a7ddc32004-03-31 19:00:16 +00001187}
1188
1189int qemu_timer_pending(QEMUTimer *ts)
1190{
1191 QEMUTimer *t;
1192 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1193 if (t == ts)
1194 return 1;
1195 }
1196 return 0;
1197}
1198
1199static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1200{
1201 if (!timer_head)
1202 return 0;
1203 return (timer_head->expire_time <= current_time);
1204}
1205
1206static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1207{
1208 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001209
bellard8a7ddc32004-03-31 19:00:16 +00001210 for(;;) {
1211 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001212 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001213 break;
1214 /* remove timer from the list before calling the callback */
1215 *ptimer_head = ts->next;
1216 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001217
bellard8a7ddc32004-03-31 19:00:16 +00001218 /* run the callback (the timer list can be modified) */
1219 ts->cb(ts->opaque);
1220 }
1221}
1222
1223int64_t qemu_get_clock(QEMUClock *clock)
1224{
1225 switch(clock->type) {
1226 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001227 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001228 default:
1229 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001230 if (use_icount) {
1231 return cpu_get_icount();
1232 } else {
1233 return cpu_get_clock();
1234 }
bellard8a7ddc32004-03-31 19:00:16 +00001235 }
1236}
1237
bellard1dce7c32006-07-13 23:20:22 +00001238static void init_timers(void)
1239{
1240 init_get_clock();
1241 ticks_per_sec = QEMU_TIMER_BASE;
1242 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1243 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1244}
1245
bellard8a7ddc32004-03-31 19:00:16 +00001246/* save a timer */
1247void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1248{
1249 uint64_t expire_time;
1250
1251 if (qemu_timer_pending(ts)) {
1252 expire_time = ts->expire_time;
1253 } else {
1254 expire_time = -1;
1255 }
1256 qemu_put_be64(f, expire_time);
1257}
1258
1259void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1260{
1261 uint64_t expire_time;
1262
1263 expire_time = qemu_get_be64(f);
1264 if (expire_time != -1) {
1265 qemu_mod_timer(ts, expire_time);
1266 } else {
1267 qemu_del_timer(ts);
1268 }
1269}
1270
1271static void timer_save(QEMUFile *f, void *opaque)
1272{
1273 if (cpu_ticks_enabled) {
1274 hw_error("cannot save state if virtual timers are running");
1275 }
thsbee8d682007-12-16 23:41:11 +00001276 qemu_put_be64(f, cpu_ticks_offset);
1277 qemu_put_be64(f, ticks_per_sec);
1278 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001279}
1280
1281static int timer_load(QEMUFile *f, void *opaque, int version_id)
1282{
bellardc88676f2006-08-06 13:36:11 +00001283 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001284 return -EINVAL;
1285 if (cpu_ticks_enabled) {
1286 return -EINVAL;
1287 }
thsbee8d682007-12-16 23:41:11 +00001288 cpu_ticks_offset=qemu_get_be64(f);
1289 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001290 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001291 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001292 }
bellard8a7ddc32004-03-31 19:00:16 +00001293 return 0;
1294}
1295
bellard67b915a2004-03-31 23:37:16 +00001296#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001297void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001298 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1299#else
bellard8a7ddc32004-03-31 19:00:16 +00001300static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001301#endif
bellard8a7ddc32004-03-31 19:00:16 +00001302{
bellard02ba45c2004-06-25 14:46:23 +00001303#if 0
1304#define DISP_FREQ 1000
1305 {
1306 static int64_t delta_min = INT64_MAX;
1307 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1308 static int count;
1309 ti = qemu_get_clock(vm_clock);
1310 if (last_clock != 0) {
1311 delta = ti - last_clock;
1312 if (delta < delta_min)
1313 delta_min = delta;
1314 if (delta > delta_max)
1315 delta_max = delta;
1316 delta_cum += delta;
1317 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001318 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001319 muldiv64(delta_min, 1000000, ticks_per_sec),
1320 muldiv64(delta_max, 1000000, ticks_per_sec),
1321 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1322 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1323 count = 0;
1324 delta_min = INT64_MAX;
1325 delta_max = 0;
1326 delta_cum = 0;
1327 }
1328 }
1329 last_clock = ti;
1330 }
1331#endif
thsefe75412007-08-24 01:36:32 +00001332 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001333 (!use_icount &&
1334 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1335 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001336 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1337 qemu_get_clock(rt_clock))) {
aliguoric96f1a42008-11-05 20:29:45 +00001338 CPUState *env = next_cpu;
aliguoric96f1a42008-11-05 20:29:45 +00001339
bellard06d9f2f2006-05-01 13:23:04 +00001340#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001341 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1342 SetEvent(data->host_alarm);
aliguorif49e58d2008-11-05 21:22:34 +00001343#else
1344 static const char byte = 0;
aliguoric96f1a42008-11-05 20:29:45 +00001345 write(alarm_timer_wfd, &byte, sizeof(byte));
aliguorif49e58d2008-11-05 21:22:34 +00001346#endif
balrogd5d08332008-01-05 19:41:47 +00001347 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1348
balrog4f8eb8d2007-12-16 12:39:38 +00001349 if (env) {
1350 /* stop the currently executing cpu because a timer occured */
1351 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
bellarda332e112005-09-03 17:55:47 +00001352#ifdef USE_KQEMU
balrog4f8eb8d2007-12-16 12:39:38 +00001353 if (env->kqemu_enabled) {
1354 kqemu_cpu_interrupt(env);
1355 }
balrogee5605e2007-12-03 03:01:40 +00001356#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001357 }
balrogee5605e2007-12-03 03:01:40 +00001358 event_pending = 1;
bellard8a7ddc32004-03-31 19:00:16 +00001359 }
1360}
1361
pbrook2e70f6e2008-06-29 01:03:05 +00001362static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001363{
pbrook2e70f6e2008-06-29 01:03:05 +00001364 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001365
1366 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001367 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1368 qemu_get_clock(vm_clock);
1369 } else {
1370 /* To avoid problems with overflow limit this to 2^32. */
1371 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001372 }
1373
pbrook2e70f6e2008-06-29 01:03:05 +00001374 if (delta < 0)
1375 delta = 0;
thsefe75412007-08-24 01:36:32 +00001376
pbrook2e70f6e2008-06-29 01:03:05 +00001377 return delta;
1378}
1379
blueswir18632fb92008-09-14 13:59:34 +00001380#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001381static uint64_t qemu_next_deadline_dyntick(void)
1382{
1383 int64_t delta;
1384 int64_t rtdelta;
1385
1386 if (use_icount)
1387 delta = INT32_MAX;
1388 else
1389 delta = (qemu_next_deadline() + 999) / 1000;
1390
1391 if (active_timers[QEMU_TIMER_REALTIME]) {
1392 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1393 qemu_get_clock(rt_clock))*1000;
1394 if (rtdelta < delta)
1395 delta = rtdelta;
1396 }
1397
1398 if (delta < MIN_TIMER_REARM_US)
1399 delta = MIN_TIMER_REARM_US;
1400
1401 return delta;
thsefe75412007-08-24 01:36:32 +00001402}
blueswir18632fb92008-09-14 13:59:34 +00001403#endif
thsefe75412007-08-24 01:36:32 +00001404
bellardfd872592004-05-12 19:11:15 +00001405#ifndef _WIN32
1406
aliguori7183b4b2008-11-05 20:40:18 +00001407/* Sets a specific flag */
1408static int fcntl_setfl(int fd, int flag)
1409{
1410 int flags;
1411
1412 flags = fcntl(fd, F_GETFL);
1413 if (flags == -1)
1414 return -errno;
1415
1416 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1417 return -errno;
1418
1419 return 0;
1420}
1421
bellard829309c2004-05-20 13:20:12 +00001422#if defined(__linux__)
1423
bellardfd872592004-05-12 19:11:15 +00001424#define RTC_FREQ 1024
1425
aurel32de9a95f2008-11-11 13:41:01 +00001426static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001427{
thsc8994012007-08-19 21:56:03 +00001428 struct sigaction act;
1429
1430 /* timer signal */
1431 sigfillset(&act.sa_mask);
1432 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001433 act.sa_handler = host_alarm_handler;
1434
1435 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001436 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001437 fcntl(fd, F_SETOWN, getpid());
1438}
1439
thsc40ec5a2007-08-19 22:09:40 +00001440static int hpet_start_timer(struct qemu_alarm_timer *t)
1441{
1442 struct hpet_info info;
1443 int r, fd;
1444
1445 fd = open("/dev/hpet", O_RDONLY);
1446 if (fd < 0)
1447 return -1;
1448
1449 /* Set frequency */
1450 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1451 if (r < 0) {
1452 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1453 "error, but for better emulation accuracy type:\n"
1454 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1455 goto fail;
1456 }
1457
1458 /* Check capabilities */
1459 r = ioctl(fd, HPET_INFO, &info);
1460 if (r < 0)
1461 goto fail;
1462
1463 /* Enable periodic mode */
1464 r = ioctl(fd, HPET_EPI, 0);
1465 if (info.hi_flags && (r < 0))
1466 goto fail;
1467
1468 /* Enable interrupt */
1469 r = ioctl(fd, HPET_IE_ON, 0);
1470 if (r < 0)
1471 goto fail;
1472
1473 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001474 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001475
1476 return 0;
1477fail:
1478 close(fd);
1479 return -1;
1480}
1481
1482static void hpet_stop_timer(struct qemu_alarm_timer *t)
1483{
pbrookfcdc2122007-08-23 20:22:22 +00001484 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001485
1486 close(fd);
1487}
1488
thsc8994012007-08-19 21:56:03 +00001489static int rtc_start_timer(struct qemu_alarm_timer *t)
1490{
1491 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001492 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001493
balrogaeb30be2007-07-02 15:03:13 +00001494 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001495 if (rtc_fd < 0)
1496 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001497 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1498 if (current_rtc_freq != RTC_FREQ &&
1499 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001500 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1501 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1502 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1503 goto fail;
1504 }
1505 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1506 fail:
1507 close(rtc_fd);
1508 return -1;
1509 }
thsc8994012007-08-19 21:56:03 +00001510
1511 enable_sigio_timer(rtc_fd);
1512
pbrookfcdc2122007-08-23 20:22:22 +00001513 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001514
bellardfd872592004-05-12 19:11:15 +00001515 return 0;
1516}
1517
thsc8994012007-08-19 21:56:03 +00001518static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001519{
pbrookfcdc2122007-08-23 20:22:22 +00001520 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001521
1522 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001523}
1524
thsefe75412007-08-24 01:36:32 +00001525static int dynticks_start_timer(struct qemu_alarm_timer *t)
1526{
1527 struct sigevent ev;
1528 timer_t host_timer;
1529 struct sigaction act;
1530
1531 sigfillset(&act.sa_mask);
1532 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001533 act.sa_handler = host_alarm_handler;
1534
1535 sigaction(SIGALRM, &act, NULL);
1536
1537 ev.sigev_value.sival_int = 0;
1538 ev.sigev_notify = SIGEV_SIGNAL;
1539 ev.sigev_signo = SIGALRM;
1540
1541 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1542 perror("timer_create");
1543
1544 /* disable dynticks */
1545 fprintf(stderr, "Dynamic Ticks disabled\n");
1546
1547 return -1;
1548 }
1549
blueswir10399bfe2008-11-16 11:37:18 +00001550 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001551
1552 return 0;
1553}
1554
1555static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1556{
blueswir10399bfe2008-11-16 11:37:18 +00001557 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001558
1559 timer_delete(host_timer);
1560}
1561
1562static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1563{
blueswir10399bfe2008-11-16 11:37:18 +00001564 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001565 struct itimerspec timeout;
1566 int64_t nearest_delta_us = INT64_MAX;
1567 int64_t current_us;
1568
1569 if (!active_timers[QEMU_TIMER_REALTIME] &&
1570 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001571 return;
thsefe75412007-08-24 01:36:32 +00001572
pbrook2e70f6e2008-06-29 01:03:05 +00001573 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001574
1575 /* check whether a timer is already running */
1576 if (timer_gettime(host_timer, &timeout)) {
1577 perror("gettime");
1578 fprintf(stderr, "Internal timer error: aborting\n");
1579 exit(1);
1580 }
1581 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1582 if (current_us && current_us <= nearest_delta_us)
1583 return;
1584
1585 timeout.it_interval.tv_sec = 0;
1586 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1587 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1588 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1589 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1590 perror("settime");
1591 fprintf(stderr, "Internal timer error: aborting\n");
1592 exit(1);
1593 }
1594}
1595
ths70744b32007-08-26 17:31:30 +00001596#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001597
thsc8994012007-08-19 21:56:03 +00001598static int unix_start_timer(struct qemu_alarm_timer *t)
1599{
1600 struct sigaction act;
1601 struct itimerval itv;
1602 int err;
1603
1604 /* timer signal */
1605 sigfillset(&act.sa_mask);
1606 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001607 act.sa_handler = host_alarm_handler;
1608
1609 sigaction(SIGALRM, &act, NULL);
1610
1611 itv.it_interval.tv_sec = 0;
1612 /* for i386 kernel 2.6 to get 1 ms */
1613 itv.it_interval.tv_usec = 999;
1614 itv.it_value.tv_sec = 0;
1615 itv.it_value.tv_usec = 10 * 1000;
1616
1617 err = setitimer(ITIMER_REAL, &itv, NULL);
1618 if (err)
1619 return -1;
1620
1621 return 0;
1622}
1623
1624static void unix_stop_timer(struct qemu_alarm_timer *t)
1625{
1626 struct itimerval itv;
1627
1628 memset(&itv, 0, sizeof(itv));
1629 setitimer(ITIMER_REAL, &itv, NULL);
1630}
1631
bellard829309c2004-05-20 13:20:12 +00001632#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001633
aliguorif49e58d2008-11-05 21:22:34 +00001634static void try_to_rearm_timer(void *opaque)
1635{
1636 struct qemu_alarm_timer *t = opaque;
1637#ifndef _WIN32
1638 ssize_t len;
1639
1640 /* Drain the notify pipe */
1641 do {
1642 char buffer[512];
1643 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1644 } while ((len == -1 && errno == EINTR) || len > 0);
1645#endif
1646
aliguorif49e58d2008-11-05 21:22:34 +00001647 if (t->flags & ALARM_FLAG_EXPIRED) {
1648 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1649 qemu_rearm_alarm_timer(alarm_timer);
1650 }
1651}
1652
thsc8994012007-08-19 21:56:03 +00001653#ifdef _WIN32
1654
1655static int win32_start_timer(struct qemu_alarm_timer *t)
1656{
1657 TIMECAPS tc;
1658 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001659 UINT flags;
thsc8994012007-08-19 21:56:03 +00001660
1661 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1662 if (!data->host_alarm) {
1663 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001664 return -1;
thsc8994012007-08-19 21:56:03 +00001665 }
1666
1667 memset(&tc, 0, sizeof(tc));
1668 timeGetDevCaps(&tc, sizeof(tc));
1669
1670 if (data->period < tc.wPeriodMin)
1671 data->period = tc.wPeriodMin;
1672
1673 timeBeginPeriod(data->period);
1674
thsefe75412007-08-24 01:36:32 +00001675 flags = TIME_CALLBACK_FUNCTION;
1676 if (alarm_has_dynticks(t))
1677 flags |= TIME_ONESHOT;
1678 else
1679 flags |= TIME_PERIODIC;
1680
thsc8994012007-08-19 21:56:03 +00001681 data->timerId = timeSetEvent(1, // interval (ms)
1682 data->period, // resolution
1683 host_alarm_handler, // function
1684 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001685 flags);
thsc8994012007-08-19 21:56:03 +00001686
1687 if (!data->timerId) {
1688 perror("Failed to initialize win32 alarm timer");
1689
1690 timeEndPeriod(data->period);
1691 CloseHandle(data->host_alarm);
1692 return -1;
1693 }
1694
aliguorif49e58d2008-11-05 21:22:34 +00001695 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
thsc8994012007-08-19 21:56:03 +00001696
1697 return 0;
1698}
1699
1700static void win32_stop_timer(struct qemu_alarm_timer *t)
1701{
1702 struct qemu_alarm_win32 *data = t->priv;
1703
1704 timeKillEvent(data->timerId);
1705 timeEndPeriod(data->period);
1706
1707 CloseHandle(data->host_alarm);
1708}
1709
thsefe75412007-08-24 01:36:32 +00001710static void win32_rearm_timer(struct qemu_alarm_timer *t)
1711{
1712 struct qemu_alarm_win32 *data = t->priv;
1713 uint64_t nearest_delta_us;
1714
1715 if (!active_timers[QEMU_TIMER_REALTIME] &&
1716 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001717 return;
thsefe75412007-08-24 01:36:32 +00001718
pbrook2e70f6e2008-06-29 01:03:05 +00001719 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001720 nearest_delta_us /= 1000;
1721
1722 timeKillEvent(data->timerId);
1723
1724 data->timerId = timeSetEvent(1,
1725 data->period,
1726 host_alarm_handler,
1727 (DWORD)t,
1728 TIME_ONESHOT | TIME_PERIODIC);
1729
1730 if (!data->timerId) {
1731 perror("Failed to re-arm win32 alarm timer");
1732
1733 timeEndPeriod(data->period);
1734 CloseHandle(data->host_alarm);
1735 exit(1);
1736 }
1737}
1738
thsc8994012007-08-19 21:56:03 +00001739#endif /* _WIN32 */
1740
aliguori7183b4b2008-11-05 20:40:18 +00001741static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001742{
blueswir1223f0d72008-09-30 18:12:18 +00001743 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001744 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001745
1746#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +00001747 int fds[2];
1748
aliguori7183b4b2008-11-05 20:40:18 +00001749 err = pipe(fds);
1750 if (err == -1)
1751 return -errno;
1752
1753 err = fcntl_setfl(fds[0], O_NONBLOCK);
1754 if (err < 0)
1755 goto fail;
1756
1757 err = fcntl_setfl(fds[1], O_NONBLOCK);
1758 if (err < 0)
1759 goto fail;
1760
aliguoric96f1a42008-11-05 20:29:45 +00001761 alarm_timer_rfd = fds[0];
1762 alarm_timer_wfd = fds[1];
aliguorif49e58d2008-11-05 21:22:34 +00001763#endif
bellard06d9f2f2006-05-01 13:23:04 +00001764
thsc8994012007-08-19 21:56:03 +00001765 for (i = 0; alarm_timers[i].name; i++) {
1766 t = &alarm_timers[i];
1767
thsc8994012007-08-19 21:56:03 +00001768 err = t->start(t);
1769 if (!err)
1770 break;
bellard67b915a2004-03-31 23:37:16 +00001771 }
bellardfd872592004-05-12 19:11:15 +00001772
thsc8994012007-08-19 21:56:03 +00001773 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001774 err = -ENOENT;
1775 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001776 }
thsc8994012007-08-19 21:56:03 +00001777
aliguorif49e58d2008-11-05 21:22:34 +00001778#ifndef _WIN32
aliguori6abfbd72008-11-05 20:49:37 +00001779 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1780 try_to_rearm_timer, NULL, t);
aliguorif49e58d2008-11-05 21:22:34 +00001781#endif
aliguori6abfbd72008-11-05 20:49:37 +00001782
thsc8994012007-08-19 21:56:03 +00001783 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001784
aliguori6abfbd72008-11-05 20:49:37 +00001785 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001786
1787fail:
aliguorif49e58d2008-11-05 21:22:34 +00001788#ifndef _WIN32
aliguori7183b4b2008-11-05 20:40:18 +00001789 close(fds[0]);
1790 close(fds[1]);
aliguorif49e58d2008-11-05 21:22:34 +00001791#endif
aliguori7183b4b2008-11-05 20:40:18 +00001792 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001793}
1794
pbrook9596ebb2007-11-18 01:44:38 +00001795static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001796{
thsc8994012007-08-19 21:56:03 +00001797 alarm_timer->stop(alarm_timer);
1798 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001799}
1800
bellardc4b1fcc2004-03-14 21:44:30 +00001801/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001802/* host time/date access */
1803void qemu_get_timedate(struct tm *tm, int offset)
1804{
1805 time_t ti;
1806 struct tm *ret;
1807
1808 time(&ti);
1809 ti += offset;
1810 if (rtc_date_offset == -1) {
1811 if (rtc_utc)
1812 ret = gmtime(&ti);
1813 else
1814 ret = localtime(&ti);
1815 } else {
1816 ti -= rtc_date_offset;
1817 ret = gmtime(&ti);
1818 }
1819
1820 memcpy(tm, ret, sizeof(struct tm));
1821}
1822
1823int qemu_timedate_diff(struct tm *tm)
1824{
1825 time_t seconds;
1826
1827 if (rtc_date_offset == -1)
1828 if (rtc_utc)
1829 seconds = mktimegm(tm);
1830 else
1831 seconds = mktime(tm);
1832 else
1833 seconds = mktimegm(tm) + rtc_date_offset;
1834
1835 return seconds - time(NULL);
1836}
1837
bellardfd1dff42006-02-01 21:29:26 +00001838#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001839static void socket_cleanup(void)
1840{
1841 WSACleanup();
1842}
bellard82c643f2004-07-14 17:28:13 +00001843
bellardfd1dff42006-02-01 21:29:26 +00001844static int socket_init(void)
1845{
1846 WSADATA Data;
1847 int ret, err;
1848
1849 ret = WSAStartup(MAKEWORD(2,2), &Data);
1850 if (ret != 0) {
1851 err = WSAGetLastError();
1852 fprintf(stderr, "WSAStartup: %d\n", err);
1853 return -1;
1854 }
1855 atexit(socket_cleanup);
1856 return 0;
1857}
aurel3264b7b732008-05-05 10:05:31 +00001858#endif
1859
aliguori63a01ef2008-10-31 19:10:00 +00001860const char *get_opt_name(char *buf, int buf_size, const char *p)
thse4bcb142007-12-02 04:51:10 +00001861{
1862 char *q;
thse4bcb142007-12-02 04:51:10 +00001863
balrog609497a2008-01-14 02:56:53 +00001864 q = buf;
1865 while (*p != '\0' && *p != '=') {
1866 if (q && (q - buf) < buf_size - 1)
1867 *q++ = *p;
1868 p++;
1869 }
1870 if (q)
1871 *q = '\0';
1872
1873 return p;
1874}
1875
aliguori63a01ef2008-10-31 19:10:00 +00001876const char *get_opt_value(char *buf, int buf_size, const char *p)
balrog609497a2008-01-14 02:56:53 +00001877{
1878 char *q;
1879
thse4bcb142007-12-02 04:51:10 +00001880 q = buf;
1881 while (*p != '\0') {
balrog609497a2008-01-14 02:56:53 +00001882 if (*p == ',') {
1883 if (*(p + 1) != ',')
thse4bcb142007-12-02 04:51:10 +00001884 break;
thse4bcb142007-12-02 04:51:10 +00001885 p++;
balrog609497a2008-01-14 02:56:53 +00001886 }
thse4bcb142007-12-02 04:51:10 +00001887 if (q && (q - buf) < buf_size - 1)
1888 *q++ = *p;
1889 p++;
1890 }
1891 if (q)
1892 *q = '\0';
1893
1894 return p;
1895}
1896
aliguori63a01ef2008-10-31 19:10:00 +00001897int get_param_value(char *buf, int buf_size,
1898 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001899{
1900 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001901 char option[128];
1902
1903 p = str;
1904 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001905 p = get_opt_name(option, sizeof(option), p);
bellard7c9d8e02005-11-15 22:16:05 +00001906 if (*p != '=')
1907 break;
1908 p++;
1909 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001910 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001911 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001912 } else {
balrog609497a2008-01-14 02:56:53 +00001913 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001914 }
1915 if (*p != ',')
1916 break;
1917 p++;
1918 }
1919 return 0;
1920}
1921
aliguori63a01ef2008-10-31 19:10:00 +00001922int check_params(char *buf, int buf_size,
1923 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001924{
1925 const char *p;
1926 int i;
1927
1928 p = str;
1929 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001930 p = get_opt_name(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001931 if (*p != '=')
1932 return -1;
1933 p++;
1934 for(i = 0; params[i] != NULL; i++)
1935 if (!strcmp(params[i], buf))
1936 break;
1937 if (params[i] == NULL)
1938 return -1;
balrog609497a2008-01-14 02:56:53 +00001939 p = get_opt_value(NULL, 0, p);
thse4bcb142007-12-02 04:51:10 +00001940 if (*p != ',')
1941 break;
1942 p++;
1943 }
1944 return 0;
1945}
1946
balrog1ae26a12008-09-28 23:19:47 +00001947/***********************************************************/
1948/* Bluetooth support */
1949static int nb_hcis;
1950static int cur_hci;
1951static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001952
balrog1ae26a12008-09-28 23:19:47 +00001953static struct bt_vlan_s {
1954 struct bt_scatternet_s net;
1955 int id;
1956 struct bt_vlan_s *next;
1957} *first_bt_vlan;
1958
1959/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001960static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001961{
1962 struct bt_vlan_s **pvlan, *vlan;
1963 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1964 if (vlan->id == id)
1965 return &vlan->net;
1966 }
1967 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1968 vlan->id = id;
1969 pvlan = &first_bt_vlan;
1970 while (*pvlan != NULL)
1971 pvlan = &(*pvlan)->next;
1972 *pvlan = vlan;
1973 return &vlan->net;
1974}
1975
1976static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1977{
1978}
1979
1980static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1981{
1982 return -ENOTSUP;
1983}
1984
1985static struct HCIInfo null_hci = {
1986 .cmd_send = null_hci_send,
1987 .sco_send = null_hci_send,
1988 .acl_send = null_hci_send,
1989 .bdaddr_set = null_hci_addr_set,
1990};
1991
1992struct HCIInfo *qemu_next_hci(void)
1993{
1994 if (cur_hci == nb_hcis)
1995 return &null_hci;
1996
1997 return hci_table[cur_hci++];
1998}
1999
balrogdc72ac12008-11-09 00:04:26 +00002000static struct HCIInfo *hci_init(const char *str)
2001{
2002 char *endp;
2003 struct bt_scatternet_s *vlan = 0;
2004
2005 if (!strcmp(str, "null"))
2006 /* null */
2007 return &null_hci;
2008 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
2009 /* host[:hciN] */
2010 return bt_host_hci(str[4] ? str + 5 : "hci0");
2011 else if (!strncmp(str, "hci", 3)) {
2012 /* hci[,vlan=n] */
2013 if (str[3]) {
2014 if (!strncmp(str + 3, ",vlan=", 6)) {
2015 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2016 if (*endp)
2017 vlan = 0;
2018 }
2019 } else
2020 vlan = qemu_find_bt_vlan(0);
2021 if (vlan)
2022 return bt_new_hci(vlan);
2023 }
2024
2025 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2026
2027 return 0;
2028}
2029
2030static int bt_hci_parse(const char *str)
2031{
2032 struct HCIInfo *hci;
2033 bdaddr_t bdaddr;
2034
2035 if (nb_hcis >= MAX_NICS) {
2036 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2037 return -1;
2038 }
2039
2040 hci = hci_init(str);
2041 if (!hci)
2042 return -1;
2043
2044 bdaddr.b[0] = 0x52;
2045 bdaddr.b[1] = 0x54;
2046 bdaddr.b[2] = 0x00;
2047 bdaddr.b[3] = 0x12;
2048 bdaddr.b[4] = 0x34;
2049 bdaddr.b[5] = 0x56 + nb_hcis;
2050 hci->bdaddr_set(hci, bdaddr.b);
2051
2052 hci_table[nb_hcis++] = hci;
2053
2054 return 0;
2055}
2056
2057static void bt_vhci_add(int vlan_id)
2058{
2059 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2060
2061 if (!vlan->slave)
2062 fprintf(stderr, "qemu: warning: adding a VHCI to "
2063 "an empty scatternet %i\n", vlan_id);
2064
2065 bt_vhci_init(bt_new_hci(vlan));
2066}
2067
2068static struct bt_device_s *bt_device_add(const char *opt)
2069{
2070 struct bt_scatternet_s *vlan;
2071 int vlan_id = 0;
2072 char *endp = strstr(opt, ",vlan=");
2073 int len = (endp ? endp - opt : strlen(opt)) + 1;
2074 char devname[10];
2075
2076 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2077
2078 if (endp) {
2079 vlan_id = strtol(endp + 6, &endp, 0);
2080 if (*endp) {
2081 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2082 return 0;
2083 }
2084 }
2085
2086 vlan = qemu_find_bt_vlan(vlan_id);
2087
2088 if (!vlan->slave)
2089 fprintf(stderr, "qemu: warning: adding a slave device to "
2090 "an empty scatternet %i\n", vlan_id);
2091
2092 if (!strcmp(devname, "keyboard"))
2093 return bt_keyboard_init(vlan);
2094
2095 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2096 return 0;
2097}
2098
2099static int bt_parse(const char *opt)
2100{
2101 const char *endp, *p;
2102 int vlan;
2103
2104 if (strstart(opt, "hci", &endp)) {
2105 if (!*endp || *endp == ',') {
2106 if (*endp)
2107 if (!strstart(endp, ",vlan=", 0))
2108 opt = endp + 1;
2109
2110 return bt_hci_parse(opt);
2111 }
2112 } else if (strstart(opt, "vhci", &endp)) {
2113 if (!*endp || *endp == ',') {
2114 if (*endp) {
2115 if (strstart(endp, ",vlan=", &p)) {
2116 vlan = strtol(p, (char **) &endp, 0);
2117 if (*endp) {
2118 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2119 return 1;
2120 }
2121 } else {
2122 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2123 return 1;
2124 }
2125 } else
2126 vlan = 0;
2127
2128 bt_vhci_add(vlan);
2129 return 0;
2130 }
2131 } else if (strstart(opt, "device:", &endp))
2132 return !bt_device_add(endp);
2133
2134 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2135 return 1;
2136}
2137
balrog1ae26a12008-09-28 23:19:47 +00002138/***********************************************************/
2139/* QEMU Block devices */
2140
balrog609497a2008-01-14 02:56:53 +00002141#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00002142#ifdef TARGET_PPC
2143#define CDROM_ALIAS "index=1,media=cdrom"
2144#else
2145#define CDROM_ALIAS "index=2,media=cdrom"
2146#endif
2147#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00002148#define PFLASH_ALIAS "if=pflash"
2149#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00002150#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002151
balrog609497a2008-01-14 02:56:53 +00002152static int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002153{
2154 va_list ap;
2155
2156 if (nb_drives_opt >= MAX_DRIVES) {
2157 fprintf(stderr, "qemu: too many drives\n");
2158 exit(1);
2159 }
2160
balrog609497a2008-01-14 02:56:53 +00002161 drives_opt[nb_drives_opt].file = file;
thse4bcb142007-12-02 04:51:10 +00002162 va_start(ap, fmt);
balrog609497a2008-01-14 02:56:53 +00002163 vsnprintf(drives_opt[nb_drives_opt].opt,
2164 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002165 va_end(ap);
2166
2167 return nb_drives_opt++;
2168}
2169
thsf60d39b2007-12-17 03:55:57 +00002170int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002171{
2172 int index;
2173
2174 /* seek interface, bus and unit */
2175
2176 for (index = 0; index < nb_drives; index++)
thsf60d39b2007-12-17 03:55:57 +00002177 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002178 drives_table[index].bus == bus &&
2179 drives_table[index].unit == unit)
2180 return index;
2181
2182 return -1;
2183}
2184
thsf60d39b2007-12-17 03:55:57 +00002185int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002186{
2187 int max_bus;
2188 int index;
2189
2190 max_bus = -1;
2191 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002192 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002193 drives_table[index].bus > max_bus)
2194 max_bus = drives_table[index].bus;
2195 }
2196 return max_bus;
2197}
2198
aliguorifa879c62009-01-07 17:32:33 +00002199const char *drive_get_serial(BlockDriverState *bdrv)
2200{
2201 int index;
2202
2203 for (index = 0; index < nb_drives; index++)
2204 if (drives_table[index].bdrv == bdrv)
2205 return drives_table[index].serial;
2206
2207 return "\0";
2208}
2209
aurel32a1620fa2008-04-29 05:58:01 +00002210static void bdrv_format_print(void *opaque, const char *name)
2211{
2212 fprintf(stderr, " %s", name);
2213}
2214
balrog609497a2008-01-14 02:56:53 +00002215static int drive_init(struct drive_opt *arg, int snapshot,
2216 QEMUMachine *machine)
thse4bcb142007-12-02 04:51:10 +00002217{
2218 char buf[128];
2219 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002220 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00002221 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002222 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002223 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002224 enum { MEDIA_DISK, MEDIA_CDROM } media;
2225 int bus_id, unit_id;
2226 int cyls, heads, secs, translation;
2227 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002228 BlockDriver *drv = NULL;
thse4bcb142007-12-02 04:51:10 +00002229 int max_devs;
2230 int index;
balrog33f00272007-12-24 14:33:24 +00002231 int cache;
2232 int bdrv_flags;
balrog609497a2008-01-14 02:56:53 +00002233 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002234 static const char * const params[] = { "bus", "unit", "if", "index",
2235 "cyls", "heads", "secs", "trans",
2236 "media", "snapshot", "file",
aliguorifa879c62009-01-07 17:32:33 +00002237 "cache", "format", "serial", NULL };
thse4bcb142007-12-02 04:51:10 +00002238
2239 if (check_params(buf, sizeof(buf), params, str) < 0) {
balrogff993632008-02-10 13:21:25 +00002240 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
thse4bcb142007-12-02 04:51:10 +00002241 buf, str);
2242 return -1;
2243 }
2244
2245 file[0] = 0;
2246 cyls = heads = secs = 0;
2247 bus_id = 0;
2248 unit_id = -1;
2249 translation = BIOS_ATA_TRANSLATION_AUTO;
2250 index = -1;
aliguori4dc822d2008-12-04 21:39:21 +00002251 cache = 3;
thse4bcb142007-12-02 04:51:10 +00002252
blueswir1c9b1ae22008-09-28 18:55:17 +00002253 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002254 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002255 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002256 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002257 } else {
thsf60d39b2007-12-17 03:55:57 +00002258 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002259 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002260 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002261 }
2262 media = MEDIA_DISK;
2263
2264 /* extract parameters */
2265
2266 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2267 bus_id = strtol(buf, NULL, 0);
2268 if (bus_id < 0) {
2269 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2270 return -1;
2271 }
2272 }
2273
2274 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2275 unit_id = strtol(buf, NULL, 0);
2276 if (unit_id < 0) {
2277 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2278 return -1;
2279 }
2280 }
2281
2282 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002283 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002284 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002285 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002286 max_devs = MAX_IDE_DEVS;
2287 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002288 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002289 max_devs = MAX_SCSI_DEVS;
2290 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002291 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002292 max_devs = 0;
2293 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002294 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002295 max_devs = 0;
2296 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002297 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002298 max_devs = 0;
2299 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002300 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002301 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002302 } else if (!strcmp(buf, "virtio")) {
2303 type = IF_VIRTIO;
2304 max_devs = 0;
2305 } else {
thse4bcb142007-12-02 04:51:10 +00002306 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2307 return -1;
2308 }
2309 }
2310
2311 if (get_param_value(buf, sizeof(buf), "index", str)) {
2312 index = strtol(buf, NULL, 0);
2313 if (index < 0) {
2314 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2315 return -1;
2316 }
2317 }
2318
2319 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2320 cyls = strtol(buf, NULL, 0);
2321 }
2322
2323 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2324 heads = strtol(buf, NULL, 0);
2325 }
2326
2327 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2328 secs = strtol(buf, NULL, 0);
2329 }
2330
2331 if (cyls || heads || secs) {
2332 if (cyls < 1 || cyls > 16383) {
2333 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2334 return -1;
2335 }
2336 if (heads < 1 || heads > 16) {
2337 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2338 return -1;
2339 }
2340 if (secs < 1 || secs > 63) {
2341 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2342 return -1;
2343 }
2344 }
2345
2346 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2347 if (!cyls) {
2348 fprintf(stderr,
2349 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2350 str);
2351 return -1;
2352 }
2353 if (!strcmp(buf, "none"))
2354 translation = BIOS_ATA_TRANSLATION_NONE;
2355 else if (!strcmp(buf, "lba"))
2356 translation = BIOS_ATA_TRANSLATION_LBA;
2357 else if (!strcmp(buf, "auto"))
2358 translation = BIOS_ATA_TRANSLATION_AUTO;
2359 else {
2360 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2361 return -1;
2362 }
2363 }
2364
2365 if (get_param_value(buf, sizeof(buf), "media", str)) {
2366 if (!strcmp(buf, "disk")) {
2367 media = MEDIA_DISK;
2368 } else if (!strcmp(buf, "cdrom")) {
2369 if (cyls || secs || heads) {
2370 fprintf(stderr,
2371 "qemu: '%s' invalid physical CHS format\n", str);
2372 return -1;
2373 }
2374 media = MEDIA_CDROM;
2375 } else {
2376 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2377 return -1;
2378 }
2379 }
2380
2381 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2382 if (!strcmp(buf, "on"))
2383 snapshot = 1;
2384 else if (!strcmp(buf, "off"))
2385 snapshot = 0;
2386 else {
2387 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2388 return -1;
2389 }
2390 }
2391
balrog33f00272007-12-24 14:33:24 +00002392 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002393 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002394 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002395 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002396 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002397 else if (!strcmp(buf, "writeback"))
2398 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002399 else {
2400 fprintf(stderr, "qemu: invalid cache option\n");
2401 return -1;
2402 }
2403 }
2404
aurel321e72d3b2008-04-28 20:26:45 +00002405 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002406 if (strcmp(buf, "?") == 0) {
2407 fprintf(stderr, "qemu: Supported formats:");
2408 bdrv_iterate_format(bdrv_format_print, NULL);
2409 fprintf(stderr, "\n");
2410 return -1;
2411 }
aurel321e72d3b2008-04-28 20:26:45 +00002412 drv = bdrv_find_format(buf);
2413 if (!drv) {
2414 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2415 return -1;
2416 }
2417 }
2418
balrog609497a2008-01-14 02:56:53 +00002419 if (arg->file == NULL)
2420 get_param_value(file, sizeof(file), "file", str);
2421 else
2422 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002423
aliguorifa879c62009-01-07 17:32:33 +00002424 if (!get_param_value(serial, sizeof(serial), "serial", str))
2425 memset(serial, 0, sizeof(serial));
2426
thse4bcb142007-12-02 04:51:10 +00002427 /* compute bus and unit according index */
2428
2429 if (index != -1) {
2430 if (bus_id != 0 || unit_id != -1) {
2431 fprintf(stderr,
2432 "qemu: '%s' index cannot be used with bus and unit\n", str);
2433 return -1;
2434 }
2435 if (max_devs == 0)
2436 {
2437 unit_id = index;
2438 bus_id = 0;
2439 } else {
2440 unit_id = index % max_devs;
2441 bus_id = index / max_devs;
2442 }
2443 }
2444
2445 /* if user doesn't specify a unit_id,
2446 * try to find the first free
2447 */
2448
2449 if (unit_id == -1) {
2450 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002451 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002452 unit_id++;
2453 if (max_devs && unit_id >= max_devs) {
2454 unit_id -= max_devs;
2455 bus_id++;
2456 }
2457 }
2458 }
2459
2460 /* check unit id */
2461
2462 if (max_devs && unit_id >= max_devs) {
2463 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2464 str, unit_id, max_devs - 1);
2465 return -1;
2466 }
2467
2468 /*
2469 * ignore multiple definitions
2470 */
2471
thsf60d39b2007-12-17 03:55:57 +00002472 if (drive_get_index(type, bus_id, unit_id) != -1)
thse4bcb142007-12-02 04:51:10 +00002473 return 0;
2474
2475 /* init */
2476
thsf60d39b2007-12-17 03:55:57 +00002477 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002478 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002479 if (max_devs)
2480 snprintf(buf, sizeof(buf), "%s%i%s%i",
2481 devname, bus_id, mediastr, unit_id);
2482 else
2483 snprintf(buf, sizeof(buf), "%s%s%i",
2484 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002485 bdrv = bdrv_new(buf);
2486 drives_table[nb_drives].bdrv = bdrv;
thsf60d39b2007-12-17 03:55:57 +00002487 drives_table[nb_drives].type = type;
thse4bcb142007-12-02 04:51:10 +00002488 drives_table[nb_drives].bus = bus_id;
2489 drives_table[nb_drives].unit = unit_id;
aliguorifa879c62009-01-07 17:32:33 +00002490 strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002491 nb_drives++;
2492
thsf60d39b2007-12-17 03:55:57 +00002493 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002494 case IF_IDE:
2495 case IF_SCSI:
2496 switch(media) {
2497 case MEDIA_DISK:
2498 if (cyls != 0) {
2499 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2500 bdrv_set_translation_hint(bdrv, translation);
2501 }
2502 break;
2503 case MEDIA_CDROM:
2504 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2505 break;
2506 }
2507 break;
2508 case IF_SD:
2509 /* FIXME: This isn't really a floppy, but it's a reasonable
2510 approximation. */
2511 case IF_FLOPPY:
2512 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2513 break;
2514 case IF_PFLASH:
2515 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002516 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002517 break;
2518 }
2519 if (!file[0])
2520 return 0;
balrog33f00272007-12-24 14:33:24 +00002521 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002522 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002523 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002524 cache = 2; /* always use write-back with snapshot */
2525 }
2526 if (cache == 0) /* no caching */
2527 bdrv_flags |= BDRV_O_NOCACHE;
2528 else if (cache == 2) /* write-back */
2529 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori4dc822d2008-12-04 21:39:21 +00002530 else if (cache == 3) /* not specified */
2531 bdrv_flags |= BDRV_O_CACHE_DEF;
aliguori83ab7952008-08-19 14:44:22 +00002532 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) {
thse4bcb142007-12-02 04:51:10 +00002533 fprintf(stderr, "qemu: could not open disk image %s\n",
2534 file);
2535 return -1;
2536 }
2537 return 0;
2538}
2539
bellard330d0412003-07-26 18:11:40 +00002540/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002541/* USB devices */
2542
pbrook0d92ed32006-05-21 16:30:15 +00002543static USBPort *used_usb_ports;
2544static USBPort *free_usb_ports;
2545
2546/* ??? Maybe change this to register a hub to keep track of the topology. */
2547void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2548 usb_attachfn attach)
2549{
2550 port->opaque = opaque;
2551 port->index = index;
2552 port->attach = attach;
2553 port->next = free_usb_ports;
2554 free_usb_ports = port;
2555}
2556
aliguori4b096fc2008-08-21 19:28:55 +00002557int usb_device_add_dev(USBDevice *dev)
2558{
2559 USBPort *port;
2560
2561 /* Find a USB port to add the device to. */
2562 port = free_usb_ports;
2563 if (!port->next) {
2564 USBDevice *hub;
2565
2566 /* Create a new hub and chain it on. */
2567 free_usb_ports = NULL;
2568 port->next = used_usb_ports;
2569 used_usb_ports = port;
2570
2571 hub = usb_hub_init(VM_USB_HUB_SIZE);
2572 usb_attach(port, hub);
2573 port = free_usb_ports;
2574 }
2575
2576 free_usb_ports = port->next;
2577 port->next = used_usb_ports;
2578 used_usb_ports = port;
2579 usb_attach(port, dev);
2580 return 0;
2581}
2582
bellarda594cfb2005-11-06 16:13:29 +00002583static int usb_device_add(const char *devname)
2584{
2585 const char *p;
2586 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002587
pbrook0d92ed32006-05-21 16:30:15 +00002588 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002589 return -1;
2590
2591 if (strstart(devname, "host:", &p)) {
2592 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002593 } else if (!strcmp(devname, "mouse")) {
2594 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002595 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002596 dev = usb_tablet_init();
2597 } else if (!strcmp(devname, "keyboard")) {
2598 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002599 } else if (strstart(devname, "disk:", &p)) {
2600 dev = usb_msd_init(p);
balrogf6d2a312007-06-10 19:21:04 +00002601 } else if (!strcmp(devname, "wacom-tablet")) {
2602 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002603 } else if (strstart(devname, "serial:", &p)) {
2604 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002605#ifdef CONFIG_BRLAPI
2606 } else if (!strcmp(devname, "braille")) {
2607 dev = usb_baum_init();
2608#endif
balrog6c9f8862008-07-17 20:47:13 +00002609 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002610 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002611
balrog9ad97e62008-07-29 13:16:31 +00002612 if (net_client_init("nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002613 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002614 nd_table[nic].model = "usb";
2615 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002616 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2617 dev = usb_bt_init(devname[2] ? hci_init(p) :
2618 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002619 } else {
2620 return -1;
2621 }
pbrook0d92ed32006-05-21 16:30:15 +00002622 if (!dev)
2623 return -1;
2624
aliguori4b096fc2008-08-21 19:28:55 +00002625 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002626}
2627
aliguori1f3870a2008-08-21 19:27:48 +00002628int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002629{
pbrook0d92ed32006-05-21 16:30:15 +00002630 USBPort *port;
2631 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002632 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002633
pbrook0d92ed32006-05-21 16:30:15 +00002634 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002635 return -1;
2636
bellarda594cfb2005-11-06 16:13:29 +00002637 if (bus_num != 0)
2638 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002639
2640 lastp = &used_usb_ports;
2641 port = used_usb_ports;
2642 while (port && port->dev->addr != addr) {
2643 lastp = &port->next;
2644 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002645 }
pbrook0d92ed32006-05-21 16:30:15 +00002646
2647 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002648 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002649
bellard059809e2006-07-19 18:06:15 +00002650 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002651 *lastp = port->next;
2652 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002653 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002654 port->next = free_usb_ports;
2655 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002656 return 0;
2657}
2658
aliguori1f3870a2008-08-21 19:27:48 +00002659static int usb_device_del(const char *devname)
2660{
2661 int bus_num, addr;
2662 const char *p;
2663
aliguori5d0c5752008-09-14 01:07:41 +00002664 if (strstart(devname, "host:", &p))
2665 return usb_host_device_close(p);
2666
aliguori1f3870a2008-08-21 19:27:48 +00002667 if (!used_usb_ports)
2668 return -1;
2669
2670 p = strchr(devname, '.');
2671 if (!p)
2672 return -1;
2673 bus_num = strtoul(devname, NULL, 0);
2674 addr = strtoul(p + 1, NULL, 0);
2675
2676 return usb_device_del_addr(bus_num, addr);
2677}
2678
bellarda594cfb2005-11-06 16:13:29 +00002679void do_usb_add(const char *devname)
2680{
aliguori4b096fc2008-08-21 19:28:55 +00002681 usb_device_add(devname);
bellarda594cfb2005-11-06 16:13:29 +00002682}
2683
2684void do_usb_del(const char *devname)
2685{
aliguori4b096fc2008-08-21 19:28:55 +00002686 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002687}
2688
2689void usb_info(void)
2690{
2691 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002692 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002693 const char *speed_str;
2694
pbrook0d92ed32006-05-21 16:30:15 +00002695 if (!usb_enabled) {
bellarda594cfb2005-11-06 16:13:29 +00002696 term_printf("USB support not enabled\n");
2697 return;
2698 }
2699
pbrook0d92ed32006-05-21 16:30:15 +00002700 for (port = used_usb_ports; port; port = port->next) {
2701 dev = port->dev;
2702 if (!dev)
2703 continue;
2704 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002705 case USB_SPEED_LOW:
2706 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002707 break;
ths5fafdf22007-09-16 21:08:06 +00002708 case USB_SPEED_FULL:
2709 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002710 break;
ths5fafdf22007-09-16 21:08:06 +00002711 case USB_SPEED_HIGH:
2712 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002713 break;
2714 default:
ths5fafdf22007-09-16 21:08:06 +00002715 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002716 break;
bellarda594cfb2005-11-06 16:13:29 +00002717 }
ths5fafdf22007-09-16 21:08:06 +00002718 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
bellard1f6e24e2006-06-26 21:00:51 +00002719 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002720 }
2721}
2722
bellardf7cce892004-12-08 22:21:25 +00002723/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002724/* PCMCIA/Cardbus */
2725
2726static struct pcmcia_socket_entry_s {
2727 struct pcmcia_socket_s *socket;
2728 struct pcmcia_socket_entry_s *next;
2729} *pcmcia_sockets = 0;
2730
2731void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2732{
2733 struct pcmcia_socket_entry_s *entry;
2734
2735 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2736 entry->socket = socket;
2737 entry->next = pcmcia_sockets;
2738 pcmcia_sockets = entry;
2739}
2740
2741void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2742{
2743 struct pcmcia_socket_entry_s *entry, **ptr;
2744
2745 ptr = &pcmcia_sockets;
2746 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2747 if (entry->socket == socket) {
2748 *ptr = entry->next;
2749 qemu_free(entry);
2750 }
2751}
2752
2753void pcmcia_info(void)
2754{
2755 struct pcmcia_socket_entry_s *iter;
2756 if (!pcmcia_sockets)
2757 term_printf("No PCMCIA sockets\n");
2758
2759 for (iter = pcmcia_sockets; iter; iter = iter->next)
2760 term_printf("%s: %s\n", iter->socket->slot_string,
2761 iter->socket->attached ? iter->socket->card_string :
2762 "Empty");
2763}
2764
2765/***********************************************************/
ths2ff89792007-06-21 23:34:19 +00002766/* dumb display */
2767
2768static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
2769{
2770}
2771
2772static void dumb_resize(DisplayState *ds, int w, int h)
2773{
2774}
2775
ths2ff89792007-06-21 23:34:19 +00002776static void dumb_display_init(DisplayState *ds)
2777{
2778 ds->data = NULL;
2779 ds->linesize = 0;
2780 ds->depth = 0;
2781 ds->dpy_update = dumb_update;
2782 ds->dpy_resize = dumb_resize;
aliguori2ad1a432008-10-31 20:34:40 +00002783 ds->dpy_refresh = NULL;
2784 ds->gui_timer_interval = 0;
aliguoribcfad702008-08-21 20:08:55 +00002785 ds->idle = 1;
ths2ff89792007-06-21 23:34:19 +00002786}
2787
2788/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002789/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002790
bellardc4b1fcc2004-03-14 21:44:30 +00002791#define MAX_IO_HANDLERS 64
2792
2793typedef struct IOHandlerRecord {
2794 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002795 IOCanRWHandler *fd_read_poll;
2796 IOHandler *fd_read;
2797 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002798 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002799 void *opaque;
2800 /* temporary data */
2801 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002802 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002803} IOHandlerRecord;
2804
bellard8a7ddc32004-03-31 19:00:16 +00002805static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002806
bellard7c9d8e02005-11-15 22:16:05 +00002807/* XXX: fd_read_poll should be suppressed, but an API change is
2808 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002809int qemu_set_fd_handler2(int fd,
2810 IOCanRWHandler *fd_read_poll,
2811 IOHandler *fd_read,
2812 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002813 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002814{
bellard8a7ddc32004-03-31 19:00:16 +00002815 IOHandlerRecord **pioh, *ioh;
2816
bellard7c9d8e02005-11-15 22:16:05 +00002817 if (!fd_read && !fd_write) {
2818 pioh = &first_io_handler;
2819 for(;;) {
2820 ioh = *pioh;
2821 if (ioh == NULL)
2822 break;
2823 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002824 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002825 break;
2826 }
2827 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002828 }
bellard7c9d8e02005-11-15 22:16:05 +00002829 } else {
2830 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2831 if (ioh->fd == fd)
2832 goto found;
2833 }
2834 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2835 if (!ioh)
2836 return -1;
2837 ioh->next = first_io_handler;
2838 first_io_handler = ioh;
2839 found:
2840 ioh->fd = fd;
2841 ioh->fd_read_poll = fd_read_poll;
2842 ioh->fd_read = fd_read;
2843 ioh->fd_write = fd_write;
2844 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002845 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002846 }
bellard7c9d8e02005-11-15 22:16:05 +00002847 return 0;
2848}
2849
ths5fafdf22007-09-16 21:08:06 +00002850int qemu_set_fd_handler(int fd,
2851 IOHandler *fd_read,
2852 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002853 void *opaque)
2854{
2855 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002856}
2857
aliguori56f3a5d2008-10-31 18:07:17 +00002858#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002859/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002860/* Polling handling */
2861
2862typedef struct PollingEntry {
2863 PollingFunc *func;
2864 void *opaque;
2865 struct PollingEntry *next;
2866} PollingEntry;
2867
2868static PollingEntry *first_polling_entry;
2869
2870int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2871{
2872 PollingEntry **ppe, *pe;
2873 pe = qemu_mallocz(sizeof(PollingEntry));
2874 if (!pe)
2875 return -1;
2876 pe->func = func;
2877 pe->opaque = opaque;
2878 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2879 *ppe = pe;
2880 return 0;
2881}
2882
2883void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2884{
2885 PollingEntry **ppe, *pe;
2886 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2887 pe = *ppe;
2888 if (pe->func == func && pe->opaque == opaque) {
2889 *ppe = pe->next;
2890 qemu_free(pe);
2891 break;
2892 }
2893 }
2894}
2895
bellarda18e5242006-06-25 17:18:27 +00002896/***********************************************************/
2897/* Wait objects support */
2898typedef struct WaitObjects {
2899 int num;
2900 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2901 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2902 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2903} WaitObjects;
2904
2905static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002906
bellarda18e5242006-06-25 17:18:27 +00002907int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2908{
2909 WaitObjects *w = &wait_objects;
2910
2911 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2912 return -1;
2913 w->events[w->num] = handle;
2914 w->func[w->num] = func;
2915 w->opaque[w->num] = opaque;
2916 w->num++;
2917 return 0;
2918}
2919
2920void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2921{
2922 int i, found;
2923 WaitObjects *w = &wait_objects;
2924
2925 found = 0;
2926 for (i = 0; i < w->num; i++) {
2927 if (w->events[i] == handle)
2928 found = 1;
2929 if (found) {
2930 w->events[i] = w->events[i + 1];
2931 w->func[i] = w->func[i + 1];
2932 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002933 }
bellarda18e5242006-06-25 17:18:27 +00002934 }
2935 if (found)
2936 w->num--;
2937}
2938#endif
2939
bellard8a7ddc32004-03-31 19:00:16 +00002940/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002941/* ram save/restore */
2942
bellard8a7ddc32004-03-31 19:00:16 +00002943static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2944{
2945 int v;
2946
2947 v = qemu_get_byte(f);
2948 switch(v) {
2949 case 0:
2950 if (qemu_get_buffer(f, buf, len) != len)
2951 return -EIO;
2952 break;
2953 case 1:
2954 v = qemu_get_byte(f);
2955 memset(buf, v, len);
2956 break;
2957 default:
2958 return -EINVAL;
2959 }
aliguori871d2f02008-10-13 03:07:56 +00002960
2961 if (qemu_file_has_error(f))
2962 return -EIO;
2963
bellard8a7ddc32004-03-31 19:00:16 +00002964 return 0;
2965}
2966
bellardc88676f2006-08-06 13:36:11 +00002967static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00002968{
aurel3200f82b82008-04-27 21:12:55 +00002969 int ret;
2970 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00002971
bellard8a7ddc32004-03-31 19:00:16 +00002972 if (qemu_get_be32(f) != phys_ram_size)
2973 return -EINVAL;
2974 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2975 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2976 if (ret)
2977 return ret;
2978 }
2979 return 0;
2980}
2981
bellardc88676f2006-08-06 13:36:11 +00002982#define BDRV_HASH_BLOCK_SIZE 1024
2983#define IOBUF_SIZE 4096
2984#define RAM_CBLOCK_MAGIC 0xfabe
2985
bellardc88676f2006-08-06 13:36:11 +00002986typedef struct RamDecompressState {
2987 z_stream zstream;
2988 QEMUFile *f;
2989 uint8_t buf[IOBUF_SIZE];
2990} RamDecompressState;
2991
2992static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
2993{
2994 int ret;
2995 memset(s, 0, sizeof(*s));
2996 s->f = f;
2997 ret = inflateInit(&s->zstream);
2998 if (ret != Z_OK)
2999 return -1;
3000 return 0;
3001}
3002
3003static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3004{
3005 int ret, clen;
3006
3007 s->zstream.avail_out = len;
3008 s->zstream.next_out = buf;
3009 while (s->zstream.avail_out > 0) {
3010 if (s->zstream.avail_in == 0) {
3011 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3012 return -1;
3013 clen = qemu_get_be16(s->f);
3014 if (clen > IOBUF_SIZE)
3015 return -1;
3016 qemu_get_buffer(s->f, s->buf, clen);
3017 s->zstream.avail_in = clen;
3018 s->zstream.next_in = s->buf;
3019 }
3020 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3021 if (ret != Z_OK && ret != Z_STREAM_END) {
3022 return -1;
3023 }
3024 }
3025 return 0;
3026}
3027
3028static void ram_decompress_close(RamDecompressState *s)
3029{
3030 inflateEnd(&s->zstream);
3031}
3032
aliguori475e4272008-10-06 20:21:51 +00003033#define RAM_SAVE_FLAG_FULL 0x01
3034#define RAM_SAVE_FLAG_COMPRESS 0x02
3035#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3036#define RAM_SAVE_FLAG_PAGE 0x08
3037#define RAM_SAVE_FLAG_EOS 0x10
3038
3039static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003040{
aliguori475e4272008-10-06 20:21:51 +00003041 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3042 uint32_t *array = (uint32_t *)page;
3043 int i;
ths3b46e622007-09-17 08:09:54 +00003044
aliguori475e4272008-10-06 20:21:51 +00003045 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3046 if (array[i] != val)
3047 return 0;
bellardc88676f2006-08-06 13:36:11 +00003048 }
aliguori475e4272008-10-06 20:21:51 +00003049
3050 return 1;
bellardc88676f2006-08-06 13:36:11 +00003051}
3052
aliguori475e4272008-10-06 20:21:51 +00003053static int ram_save_block(QEMUFile *f)
3054{
3055 static ram_addr_t current_addr = 0;
3056 ram_addr_t saved_addr = current_addr;
3057 ram_addr_t addr = 0;
3058 int found = 0;
3059
3060 while (addr < phys_ram_size) {
3061 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3062 uint8_t ch;
3063
3064 cpu_physical_memory_reset_dirty(current_addr,
3065 current_addr + TARGET_PAGE_SIZE,
3066 MIGRATION_DIRTY_FLAG);
3067
3068 ch = *(phys_ram_base + current_addr);
3069
3070 if (is_dup_page(phys_ram_base + current_addr, ch)) {
3071 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3072 qemu_put_byte(f, ch);
3073 } else {
3074 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3075 qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
3076 }
3077
3078 found = 1;
3079 break;
3080 }
3081 addr += TARGET_PAGE_SIZE;
3082 current_addr = (saved_addr + addr) % phys_ram_size;
3083 }
3084
3085 return found;
3086}
3087
3088static ram_addr_t ram_save_threshold = 10;
3089
3090static ram_addr_t ram_save_remaining(void)
3091{
3092 ram_addr_t addr;
3093 ram_addr_t count = 0;
3094
3095 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3096 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3097 count++;
3098 }
3099
3100 return count;
3101}
3102
3103static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3104{
3105 ram_addr_t addr;
3106
3107 if (stage == 1) {
3108 /* Make sure all dirty bits are set */
3109 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3110 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3111 cpu_physical_memory_set_dirty(addr);
3112 }
3113
3114 /* Enable dirty memory tracking */
3115 cpu_physical_memory_set_dirty_tracking(1);
3116
3117 qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
3118 }
3119
3120 while (!qemu_file_rate_limit(f)) {
3121 int ret;
3122
3123 ret = ram_save_block(f);
3124 if (ret == 0) /* no more blocks */
3125 break;
3126 }
3127
3128 /* try transferring iterative blocks of memory */
3129
3130 if (stage == 3) {
3131 cpu_physical_memory_set_dirty_tracking(0);
3132
3133 /* flush all remaining blocks regardless of rate limiting */
3134 while (ram_save_block(f) != 0);
3135 }
3136
3137 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3138
3139 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3140}
3141
3142static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003143{
3144 RamDecompressState s1, *s = &s1;
3145 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003146 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003147
bellardc88676f2006-08-06 13:36:11 +00003148 if (ram_decompress_open(s, f) < 0)
3149 return -EINVAL;
3150 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
3151 if (ram_decompress_buf(s, buf, 1) < 0) {
3152 fprintf(stderr, "Error while reading ram block header\n");
3153 goto error;
3154 }
3155 if (buf[0] == 0) {
3156 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003157 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003158 goto error;
3159 }
aliguori475e4272008-10-06 20:21:51 +00003160 } else {
bellardc88676f2006-08-06 13:36:11 +00003161 error:
3162 printf("Error block header\n");
3163 return -EINVAL;
3164 }
3165 }
3166 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003167
3168 return 0;
3169}
3170
3171static int ram_load(QEMUFile *f, void *opaque, int version_id)
3172{
3173 ram_addr_t addr;
3174 int flags;
3175
3176 if (version_id == 1)
3177 return ram_load_v1(f, opaque);
3178
3179 if (version_id == 2) {
3180 if (qemu_get_be32(f) != phys_ram_size)
3181 return -EINVAL;
3182 return ram_load_dead(f, opaque);
3183 }
3184
3185 if (version_id != 3)
3186 return -EINVAL;
3187
3188 do {
3189 addr = qemu_get_be64(f);
3190
3191 flags = addr & ~TARGET_PAGE_MASK;
3192 addr &= TARGET_PAGE_MASK;
3193
3194 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3195 if (addr != phys_ram_size)
3196 return -EINVAL;
3197 }
3198
3199 if (flags & RAM_SAVE_FLAG_FULL) {
3200 if (ram_load_dead(f, opaque) < 0)
3201 return -EINVAL;
3202 }
3203
3204 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3205 uint8_t ch = qemu_get_byte(f);
3206 memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
3207 } else if (flags & RAM_SAVE_FLAG_PAGE)
3208 qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
3209 } while (!(flags & RAM_SAVE_FLAG_EOS));
3210
bellardc88676f2006-08-06 13:36:11 +00003211 return 0;
3212}
3213
aliguori9e472e12008-10-08 19:50:24 +00003214void qemu_service_io(void)
3215{
3216 CPUState *env = cpu_single_env;
3217 if (env) {
3218 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3219#ifdef USE_KQEMU
3220 if (env->kqemu_enabled) {
3221 kqemu_cpu_interrupt(env);
3222 }
3223#endif
3224 }
3225}
3226
bellard8a7ddc32004-03-31 19:00:16 +00003227/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003228/* bottom halves (can be seen as timers which expire ASAP) */
3229
3230struct QEMUBH {
3231 QEMUBHFunc *cb;
3232 void *opaque;
3233 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003234 int idle;
3235 int deleted;
bellard83f64092006-08-01 16:21:11 +00003236 QEMUBH *next;
3237};
3238
3239static QEMUBH *first_bh = NULL;
3240
3241QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3242{
3243 QEMUBH *bh;
3244 bh = qemu_mallocz(sizeof(QEMUBH));
3245 if (!bh)
3246 return NULL;
3247 bh->cb = cb;
3248 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003249 bh->next = first_bh;
3250 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003251 return bh;
3252}
3253
bellard6eb57332006-08-06 09:51:25 +00003254int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003255{
aliguori1b435b12008-10-31 17:24:21 +00003256 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003257 int ret;
bellard83f64092006-08-01 16:21:11 +00003258
bellard6eb57332006-08-06 09:51:25 +00003259 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003260 for (bh = first_bh; bh; bh = bh->next) {
3261 if (!bh->deleted && bh->scheduled) {
3262 bh->scheduled = 0;
3263 if (!bh->idle)
3264 ret = 1;
3265 bh->idle = 0;
3266 bh->cb(bh->opaque);
3267 }
bellard83f64092006-08-01 16:21:11 +00003268 }
aliguori1b435b12008-10-31 17:24:21 +00003269
3270 /* remove deleted bhs */
3271 bhp = &first_bh;
3272 while (*bhp) {
3273 bh = *bhp;
3274 if (bh->deleted) {
3275 *bhp = bh->next;
3276 qemu_free(bh);
3277 } else
3278 bhp = &bh->next;
3279 }
3280
bellard6eb57332006-08-06 09:51:25 +00003281 return ret;
bellard83f64092006-08-01 16:21:11 +00003282}
3283
aliguori1b435b12008-10-31 17:24:21 +00003284void qemu_bh_schedule_idle(QEMUBH *bh)
3285{
3286 if (bh->scheduled)
3287 return;
3288 bh->scheduled = 1;
3289 bh->idle = 1;
3290}
3291
bellard83f64092006-08-01 16:21:11 +00003292void qemu_bh_schedule(QEMUBH *bh)
3293{
3294 CPUState *env = cpu_single_env;
3295 if (bh->scheduled)
3296 return;
3297 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003298 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003299 /* stop the currently executing CPU to execute the BH ASAP */
3300 if (env) {
3301 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3302 }
3303}
3304
3305void qemu_bh_cancel(QEMUBH *bh)
3306{
aliguori1b435b12008-10-31 17:24:21 +00003307 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003308}
3309
3310void qemu_bh_delete(QEMUBH *bh)
3311{
aliguori1b435b12008-10-31 17:24:21 +00003312 bh->scheduled = 0;
3313 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003314}
3315
aliguori56f3a5d2008-10-31 18:07:17 +00003316static void qemu_bh_update_timeout(int *timeout)
3317{
3318 QEMUBH *bh;
3319
3320 for (bh = first_bh; bh; bh = bh->next) {
3321 if (!bh->deleted && bh->scheduled) {
3322 if (bh->idle) {
3323 /* idle bottom halves will be polled at least
3324 * every 10ms */
3325 *timeout = MIN(10, *timeout);
3326 } else {
3327 /* non-idle bottom halves will be executed
3328 * immediately */
3329 *timeout = 0;
3330 break;
3331 }
3332 }
3333 }
3334}
3335
bellard83f64092006-08-01 16:21:11 +00003336/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003337/* machine registration */
3338
blueswir1bdaf78e2008-10-04 07:24:27 +00003339static QEMUMachine *first_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003340
3341int qemu_register_machine(QEMUMachine *m)
3342{
3343 QEMUMachine **pm;
3344 pm = &first_machine;
3345 while (*pm != NULL)
3346 pm = &(*pm)->next;
3347 m->next = NULL;
3348 *pm = m;
3349 return 0;
3350}
3351
pbrook9596ebb2007-11-18 01:44:38 +00003352static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003353{
3354 QEMUMachine *m;
3355
3356 for(m = first_machine; m != NULL; m = m->next) {
3357 if (!strcmp(m->name, name))
3358 return m;
3359 }
3360 return NULL;
3361}
3362
3363/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003364/* main execution loop */
3365
pbrook9596ebb2007-11-18 01:44:38 +00003366static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003367{
ths740733b2007-06-08 01:57:56 +00003368 DisplayState *ds = opaque;
3369 ds->dpy_refresh(ds);
aurel32f442e082008-03-13 19:20:33 +00003370 qemu_mod_timer(ds->gui_timer,
3371 (ds->gui_timer_interval ?
3372 ds->gui_timer_interval :
3373 GUI_REFRESH_INTERVAL)
3374 + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003375}
3376
bellard0bd48852005-11-11 00:00:47 +00003377struct vm_change_state_entry {
3378 VMChangeStateHandler *cb;
3379 void *opaque;
3380 LIST_ENTRY (vm_change_state_entry) entries;
3381};
3382
3383static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3384
3385VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3386 void *opaque)
3387{
3388 VMChangeStateEntry *e;
3389
3390 e = qemu_mallocz(sizeof (*e));
3391 if (!e)
3392 return NULL;
3393
3394 e->cb = cb;
3395 e->opaque = opaque;
3396 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3397 return e;
3398}
3399
3400void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3401{
3402 LIST_REMOVE (e, entries);
3403 qemu_free (e);
3404}
3405
3406static void vm_state_notify(int running)
3407{
3408 VMChangeStateEntry *e;
3409
3410 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
3411 e->cb(e->opaque, running);
3412 }
3413}
3414
bellard8a7ddc32004-03-31 19:00:16 +00003415/* XXX: support several handlers */
bellard0bd48852005-11-11 00:00:47 +00003416static VMStopHandler *vm_stop_cb;
3417static void *vm_stop_opaque;
bellard8a7ddc32004-03-31 19:00:16 +00003418
3419int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
3420{
3421 vm_stop_cb = cb;
3422 vm_stop_opaque = opaque;
3423 return 0;
3424}
3425
3426void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
3427{
3428 vm_stop_cb = NULL;
3429}
3430
3431void vm_start(void)
3432{
3433 if (!vm_running) {
3434 cpu_enable_ticks();
3435 vm_running = 1;
bellard0bd48852005-11-11 00:00:47 +00003436 vm_state_notify(1);
thsefe75412007-08-24 01:36:32 +00003437 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00003438 }
3439}
3440
ths5fafdf22007-09-16 21:08:06 +00003441void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00003442{
3443 if (vm_running) {
3444 cpu_disable_ticks();
3445 vm_running = 0;
3446 if (reason != 0) {
3447 if (vm_stop_cb) {
3448 vm_stop_cb(vm_stop_opaque, reason);
3449 }
3450 }
bellard0bd48852005-11-11 00:00:47 +00003451 vm_state_notify(0);
bellard8a7ddc32004-03-31 19:00:16 +00003452 }
3453}
3454
bellardbb0c6722004-06-20 12:37:32 +00003455/* reset/shutdown handler */
3456
3457typedef struct QEMUResetEntry {
3458 QEMUResetHandler *func;
3459 void *opaque;
3460 struct QEMUResetEntry *next;
3461} QEMUResetEntry;
3462
3463static QEMUResetEntry *first_reset_entry;
3464static int reset_requested;
3465static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003466static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00003467
aurel32cf7a2fe2008-03-18 06:53:05 +00003468int qemu_shutdown_requested(void)
3469{
3470 int r = shutdown_requested;
3471 shutdown_requested = 0;
3472 return r;
3473}
3474
3475int qemu_reset_requested(void)
3476{
3477 int r = reset_requested;
3478 reset_requested = 0;
3479 return r;
3480}
3481
3482int qemu_powerdown_requested(void)
3483{
3484 int r = powerdown_requested;
3485 powerdown_requested = 0;
3486 return r;
3487}
3488
bellardbb0c6722004-06-20 12:37:32 +00003489void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3490{
3491 QEMUResetEntry **pre, *re;
3492
3493 pre = &first_reset_entry;
3494 while (*pre != NULL)
3495 pre = &(*pre)->next;
3496 re = qemu_mallocz(sizeof(QEMUResetEntry));
3497 re->func = func;
3498 re->opaque = opaque;
3499 re->next = NULL;
3500 *pre = re;
3501}
3502
aurel32cf7a2fe2008-03-18 06:53:05 +00003503void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00003504{
3505 QEMUResetEntry *re;
3506
3507 /* reset all devices */
3508 for(re = first_reset_entry; re != NULL; re = re->next) {
3509 re->func(re->opaque);
3510 }
3511}
3512
3513void qemu_system_reset_request(void)
3514{
bellardd1beab82006-10-02 19:44:22 +00003515 if (no_reboot) {
3516 shutdown_requested = 1;
3517 } else {
3518 reset_requested = 1;
3519 }
bellard6a00d602005-11-21 23:25:50 +00003520 if (cpu_single_env)
3521 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003522}
3523
3524void qemu_system_shutdown_request(void)
3525{
3526 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003527 if (cpu_single_env)
3528 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003529}
3530
bellard34751872005-07-02 14:31:34 +00003531void qemu_system_powerdown_request(void)
3532{
3533 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003534 if (cpu_single_env)
3535 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003536}
3537
ths877cf882007-04-18 18:11:47 +00003538#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003539static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003540{
3541 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003542 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003543
bellardf3311102006-04-12 20:21:17 +00003544
3545 /* XXX: need to suppress polling by better using win32 events */
3546 ret = 0;
3547 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3548 ret |= pe->func(pe->opaque);
3549 }
thse6b1e552007-04-18 17:56:02 +00003550 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003551 int err;
3552 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003553
aliguori56f3a5d2008-10-31 18:07:17 +00003554 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003555 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3556 if (w->func[ret - WAIT_OBJECT_0])
3557 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003558
ths5fafdf22007-09-16 21:08:06 +00003559 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003560 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003561
thse6b1e552007-04-18 17:56:02 +00003562 /* Check if event is signaled */
3563 ret2 = WaitForSingleObject(w->events[i], 0);
3564 if(ret2 == WAIT_OBJECT_0) {
3565 if (w->func[i])
3566 w->func[i](w->opaque[i]);
3567 } else if (ret2 == WAIT_TIMEOUT) {
3568 } else {
3569 err = GetLastError();
3570 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003571 }
3572 }
bellarda18e5242006-06-25 17:18:27 +00003573 } else if (ret == WAIT_TIMEOUT) {
3574 } else {
3575 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003576 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003577 }
bellardf3311102006-04-12 20:21:17 +00003578 }
aliguori56f3a5d2008-10-31 18:07:17 +00003579
3580 *timeout = 0;
3581}
3582#else
blueswir169d64512008-12-07 19:30:18 +00003583static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003584{
3585}
bellardfd1dff42006-02-01 21:29:26 +00003586#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003587
3588void main_loop_wait(int timeout)
3589{
3590 IOHandlerRecord *ioh;
3591 fd_set rfds, wfds, xfds;
3592 int ret, nfds;
3593 struct timeval tv;
3594
3595 qemu_bh_update_timeout(&timeout);
3596
3597 host_main_loop_wait(&timeout);
3598
bellardfd1dff42006-02-01 21:29:26 +00003599 /* poll any events */
3600 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003601 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003602 FD_ZERO(&rfds);
3603 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003604 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003605 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003606 if (ioh->deleted)
3607 continue;
bellardfd1dff42006-02-01 21:29:26 +00003608 if (ioh->fd_read &&
3609 (!ioh->fd_read_poll ||
3610 ioh->fd_read_poll(ioh->opaque) != 0)) {
3611 FD_SET(ioh->fd, &rfds);
3612 if (ioh->fd > nfds)
3613 nfds = ioh->fd;
3614 }
3615 if (ioh->fd_write) {
3616 FD_SET(ioh->fd, &wfds);
3617 if (ioh->fd > nfds)
3618 nfds = ioh->fd;
3619 }
3620 }
ths3b46e622007-09-17 08:09:54 +00003621
aliguori56f3a5d2008-10-31 18:07:17 +00003622 tv.tv_sec = timeout / 1000;
3623 tv.tv_usec = (timeout % 1000) * 1000;
3624
bellarde0356492006-05-01 13:33:02 +00003625#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003626 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003627 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3628 }
3629#endif
3630 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00003631 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003632 IOHandlerRecord **pioh;
3633
3634 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003635 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003636 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003637 }
ths6ab43fd2007-08-25 01:34:19 +00003638 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003639 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003640 }
3641 }
thscafffd42007-02-28 21:59:44 +00003642
3643 /* remove deleted IO handlers */
3644 pioh = &first_io_handler;
3645 while (*pioh) {
3646 ioh = *pioh;
3647 if (ioh->deleted) {
3648 *pioh = ioh->next;
3649 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003650 } else
thscafffd42007-02-28 21:59:44 +00003651 pioh = &ioh->next;
3652 }
bellardfd1dff42006-02-01 21:29:26 +00003653 }
bellarde0356492006-05-01 13:33:02 +00003654#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003655 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003656 if (ret < 0) {
3657 FD_ZERO(&rfds);
3658 FD_ZERO(&wfds);
3659 FD_ZERO(&xfds);
3660 }
3661 slirp_select_poll(&rfds, &wfds, &xfds);
3662 }
3663#endif
bellardc20709a2004-04-21 23:27:19 +00003664
aliguori357c6922008-11-25 17:26:09 +00003665 /* vm time timers */
3666 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3667 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3668 qemu_get_clock(vm_clock));
3669
3670 /* real time timers */
3671 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3672 qemu_get_clock(rt_clock));
3673
pbrook423f0742007-05-23 00:06:54 +00003674 /* Check bottom-halves last in case any of the earlier events triggered
3675 them. */
3676 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003677
bellard5905b2e2004-08-01 21:53:26 +00003678}
3679
pbrook9596ebb2007-11-18 01:44:38 +00003680static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00003681{
3682 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00003683#ifdef CONFIG_PROFILER
3684 int64_t ti;
3685#endif
bellard6a00d602005-11-21 23:25:50 +00003686 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003687
bellard6a00d602005-11-21 23:25:50 +00003688 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00003689 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00003690 for(;;) {
3691 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00003692
bellard15a76442005-11-23 21:01:03 +00003693 for(;;) {
3694 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00003695 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00003696#ifdef CONFIG_PROFILER
3697 ti = profile_getclock();
3698#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003699 if (use_icount) {
3700 int64_t count;
3701 int decr;
3702 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3703 env->icount_decr.u16.low = 0;
3704 env->icount_extra = 0;
3705 count = qemu_next_deadline();
3706 count = (count + (1 << icount_time_shift) - 1)
3707 >> icount_time_shift;
3708 qemu_icount += count;
3709 decr = (count > 0xffff) ? 0xffff : count;
3710 count -= decr;
3711 env->icount_decr.u16.low = decr;
3712 env->icount_extra = count;
3713 }
bellard6a00d602005-11-21 23:25:50 +00003714 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00003715#ifdef CONFIG_PROFILER
3716 qemu_time += profile_getclock() - ti;
3717#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003718 if (use_icount) {
3719 /* Fold pending instructions back into the
3720 instruction counter, and clear the interrupt flag. */
3721 qemu_icount -= (env->icount_decr.u16.low
3722 + env->icount_extra);
3723 env->icount_decr.u32 = 0;
3724 env->icount_extra = 0;
3725 }
balrogee5605e2007-12-03 03:01:40 +00003726 next_cpu = env->next_cpu ?: first_cpu;
aurel3295b01002008-04-04 17:16:35 +00003727 if (event_pending && likely(ret != EXCP_DEBUG)) {
balrogee5605e2007-12-03 03:01:40 +00003728 ret = EXCP_INTERRUPT;
3729 event_pending = 0;
3730 break;
3731 }
pbrookbd967e02007-03-11 18:54:57 +00003732 if (ret == EXCP_HLT) {
3733 /* Give the next CPU a chance to run. */
3734 cur_cpu = env;
3735 continue;
3736 }
bellard15a76442005-11-23 21:01:03 +00003737 if (ret != EXCP_HALTED)
3738 break;
3739 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00003740 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00003741 break;
bellard15a76442005-11-23 21:01:03 +00003742 }
3743 cur_cpu = env;
3744
bellard5905b2e2004-08-01 21:53:26 +00003745 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00003746 ret = EXCP_INTERRUPT;
aurel32b2f76162008-04-11 21:35:52 +00003747 if (no_shutdown) {
3748 vm_stop(0);
3749 no_shutdown = 0;
3750 }
3751 else
3752 break;
bellard5905b2e2004-08-01 21:53:26 +00003753 }
3754 if (reset_requested) {
3755 reset_requested = 0;
3756 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00003757 ret = EXCP_INTERRUPT;
3758 }
3759 if (powerdown_requested) {
3760 powerdown_requested = 0;
3761 qemu_system_powerdown();
3762 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00003763 }
aurel3295b01002008-04-04 17:16:35 +00003764 if (unlikely(ret == EXCP_DEBUG)) {
aliguori880a7572008-11-18 20:30:24 +00003765 gdb_set_stop_cpu(cur_cpu);
bellard5905b2e2004-08-01 21:53:26 +00003766 vm_stop(EXCP_DEBUG);
3767 }
pbrookbd967e02007-03-11 18:54:57 +00003768 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00003769 /* XXX: use timeout computed from timers */
pbrook2e70f6e2008-06-29 01:03:05 +00003770 if (ret == EXCP_HALTED) {
3771 if (use_icount) {
3772 int64_t add;
3773 int64_t delta;
3774 /* Advance virtual time to the next event. */
3775 if (use_icount == 1) {
3776 /* When not using an adaptive execution frequency
3777 we tend to get badly out of sync with real time,
thsbf20dc02008-06-30 17:22:19 +00003778 so just delay for a reasonable amount of time. */
pbrook2e70f6e2008-06-29 01:03:05 +00003779 delta = 0;
3780 } else {
3781 delta = cpu_get_icount() - cpu_get_clock();
3782 }
3783 if (delta > 0) {
3784 /* If virtual time is ahead of real time then just
3785 wait for IO. */
3786 timeout = (delta / 1000000) + 1;
3787 } else {
3788 /* Wait for either IO to occur or the next
3789 timer event. */
3790 add = qemu_next_deadline();
3791 /* We advance the timer before checking for IO.
3792 Limit the amount we advance so that early IO
3793 activity won't get the guest too far ahead. */
3794 if (add > 10000000)
3795 add = 10000000;
3796 delta += add;
3797 add = (add + (1 << icount_time_shift) - 1)
3798 >> icount_time_shift;
3799 qemu_icount += add;
3800 timeout = delta / 1000000;
3801 if (timeout < 0)
3802 timeout = 0;
3803 }
3804 } else {
aliguori0a1af392008-10-31 18:40:25 +00003805 timeout = 5000;
pbrook2e70f6e2008-06-29 01:03:05 +00003806 }
3807 } else {
bellard5905b2e2004-08-01 21:53:26 +00003808 timeout = 0;
pbrook2e70f6e2008-06-29 01:03:05 +00003809 }
bellard5905b2e2004-08-01 21:53:26 +00003810 } else {
blueswir198448f52008-09-30 18:16:09 +00003811 if (shutdown_requested) {
3812 ret = EXCP_INTERRUPT;
aliguori5b08fc12008-08-21 20:08:03 +00003813 break;
blueswir198448f52008-09-30 18:16:09 +00003814 }
aliguori0a1af392008-10-31 18:40:25 +00003815 timeout = 5000;
bellard5905b2e2004-08-01 21:53:26 +00003816 }
bellard89bfc102006-02-08 22:46:31 +00003817#ifdef CONFIG_PROFILER
3818 ti = profile_getclock();
3819#endif
bellard5905b2e2004-08-01 21:53:26 +00003820 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00003821#ifdef CONFIG_PROFILER
3822 dev_time += profile_getclock() - ti;
3823#endif
bellardb4608c02003-06-27 17:34:32 +00003824 }
bellard34865132003-10-05 14:28:56 +00003825 cpu_disable_ticks();
3826 return ret;
bellardb4608c02003-06-27 17:34:32 +00003827}
3828
ths15f82202007-06-29 23:26:08 +00003829static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00003830{
bellard68d0f702008-01-06 17:21:48 +00003831 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00003832 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00003833 "\n"
bellarda20dd502003-09-30 21:07:02 +00003834 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00003835 "\n"
bellarda20dd502003-09-30 21:07:02 +00003836 "Standard options:\n"
bellardcc1daa42005-06-05 14:49:17 +00003837 "-M machine select emulated machine (-M ? for list)\n"
pbrook5adb4832007-03-08 03:15:18 +00003838 "-cpu cpu select CPU (-cpu ? for list)\n"
bellardc45886d2004-01-05 00:02:06 +00003839 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00003840 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3841 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003842 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
aurel32a1620fa2008-04-29 05:58:01 +00003843 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3844 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
aliguorifa879c62009-01-07 17:32:33 +00003845 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
thse4bcb142007-12-02 04:51:10 +00003846 " use 'file' as a drive image\n"
balrog3e3d5812007-04-30 02:09:25 +00003847 "-mtdblock file use 'file' as on-board Flash memory image\n"
pbrooka1bb27b2007-04-06 16:49:48 +00003848 "-sd file use 'file' as SecureDigital card image\n"
j_mayer86f55662007-04-24 06:52:59 +00003849 "-pflash file use 'file' as a parallel flash image\n"
thseec85c22007-01-05 17:41:07 +00003850 "-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 +00003851 "-snapshot write to temporary files instead of disk image files\n"
3852#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00003853 "-no-frame open SDL window without a frame and window decorations\n"
ths3780e192007-06-21 21:08:02 +00003854 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
ths667acca2006-12-11 02:08:05 +00003855 "-no-quit disable SDL window close capability\n"
3856#endif
bellard52ca8d62006-06-14 16:03:05 +00003857#ifdef TARGET_I386
3858 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3859#endif
bellarda00bad72004-05-22 21:39:06 +00003860 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellard91fc2112005-12-18 19:09:37 +00003861 "-smp n set the number of CPUs to 'n' [default=1]\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003862 "-nographic disable graphical output and redirect serial I/Os to console\n"
balroga171fe32007-04-30 01:48:07 +00003863 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
bellard4ca00742004-12-12 22:20:04 +00003864#ifndef _WIN32
ths667acca2006-12-11 02:08:05 +00003865 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00003866#endif
bellard1d14ffa2005-10-30 18:58:22 +00003867#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00003868 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00003869 "-soundhw c1,... enable audio support\n"
3870 " and only specified sound cards (comma separated list)\n"
3871 " use -soundhw ? to get the list of supported cards\n"
bellard6a36d842005-12-18 20:34:32 +00003872 " use -soundhw all to enable all of them\n"
bellard1d14ffa2005-10-30 18:58:22 +00003873#endif
malc3893c122008-09-28 00:42:05 +00003874 "-vga [std|cirrus|vmware]\n"
3875 " select video card type\n"
bellard89980282004-06-03 14:04:03 +00003876 "-localtime set the real time clock to local time [default=utc]\n"
bellardd63d3072004-10-03 13:29:03 +00003877 "-full-screen start in full screen\n"
bellarda09db212005-04-30 16:10:35 +00003878#ifdef TARGET_I386
3879 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3880#endif
bellardb389dbf2005-11-06 16:49:55 +00003881 "-usb enable the USB driver (will be the default soon)\n"
3882 "-usbdevice name add the host or guest USB device 'name'\n"
bellard6f7e9ae2005-03-13 09:43:36 +00003883#if defined(TARGET_PPC) || defined(TARGET_SPARC)
3884 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00003885#endif
thsc35734b2007-03-19 15:17:08 +00003886 "-name string set the name of the guest\n"
blueswir18fcb1b92008-09-18 18:29:08 +00003887 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
bellarda20dd502003-09-30 21:07:02 +00003888 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003889 "Network options:\n"
aliguori7a9f6e42009-01-07 17:48:51 +00003890 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
bellard7c9d8e02005-11-15 22:16:05 +00003891 " create a new Network Interface Card and connect it to VLAN 'n'\n"
bellardc20709a2004-04-21 23:27:19 +00003892#ifdef CONFIG_SLIRP
aliguori7a9f6e42009-01-07 17:48:51 +00003893 "-net user[,vlan=n][,name=str][,hostname=host]\n"
pbrook115defd2006-04-16 11:06:58 +00003894 " connect the user mode network stack to VLAN 'n' and send\n"
3895 " hostname 'host' to DHCP clients\n"
bellard7c9d8e02005-11-15 22:16:05 +00003896#endif
bellard7fb843f2006-02-01 23:06:55 +00003897#ifdef _WIN32
aliguori7a9f6e42009-01-07 17:48:51 +00003898 "-net tap[,vlan=n][,name=str],ifname=name\n"
bellard7fb843f2006-02-01 23:06:55 +00003899 " connect the host TAP network interface to VLAN 'n'\n"
3900#else
aliguori7a9f6e42009-01-07 17:48:51 +00003901 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
thsb46a8902007-10-21 23:20:45 +00003902 " connect the host TAP network interface to VLAN 'n' and use the\n"
3903 " network scripts 'file' (default=%s)\n"
3904 " and 'dfile' (default=%s);\n"
3905 " use '[down]script=no' to disable script execution;\n"
bellard7c9d8e02005-11-15 22:16:05 +00003906 " use 'fd=h' to connect to an already opened TAP interface\n"
bellard7fb843f2006-02-01 23:06:55 +00003907#endif
aliguori7a9f6e42009-01-07 17:48:51 +00003908 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
bellard7c9d8e02005-11-15 22:16:05 +00003909 " connect the vlan 'n' to another VLAN using a socket connection\n"
aliguori7a9f6e42009-01-07 17:48:51 +00003910 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
bellard3d830452005-12-18 16:36:49 +00003911 " connect the vlan 'n' to multicast maddr and port\n"
ths8a16d272008-07-19 09:56:24 +00003912#ifdef CONFIG_VDE
aliguori7a9f6e42009-01-07 17:48:51 +00003913 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
ths8a16d272008-07-19 09:56:24 +00003914 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3915 " on host and listening for incoming connections on 'socketpath'.\n"
3916 " Use group 'groupname' and mode 'octalmode' to change default\n"
3917 " ownership and permissions for communication port.\n"
3918#endif
bellard7c9d8e02005-11-15 22:16:05 +00003919 "-net none use it alone to have zero network devices; if no -net option\n"
3920 " is provided, the default is '-net nic -net user'\n"
3921 "\n"
balrogdc72ac12008-11-09 00:04:26 +00003922 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
3923 "-bt hci,host[:id]\n"
3924 " Use host's HCI with the given name\n"
3925 "-bt hci[,vlan=n]\n"
3926 " Emulate a standard HCI in virtual scatternet 'n'\n"
3927 "-bt vhci[,vlan=n]\n"
3928 " Add host computer to virtual scatternet 'n' using VHCI\n"
3929 "-bt device:dev[,vlan=n]\n"
3930 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
3931 "\n"
bellard7c9d8e02005-11-15 22:16:05 +00003932#ifdef CONFIG_SLIRP
ths0db11372007-02-20 00:12:07 +00003933 "-tftp dir allow tftp access to files in dir [-net user]\n"
ths47d5d012007-02-20 00:05:08 +00003934 "-bootp file advertise file in BOOTP replies\n"
bellard7c9d8e02005-11-15 22:16:05 +00003935#ifndef _WIN32
3936 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
bellardc94c8d62004-09-13 21:37:34 +00003937#endif
bellard9bf05442004-08-25 22:12:49 +00003938 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
bellard7c9d8e02005-11-15 22:16:05 +00003939 " redirect TCP or UDP connections from host to guest [-net user]\n"
bellardc20709a2004-04-21 23:27:19 +00003940#endif
bellardc4b1fcc2004-03-14 21:44:30 +00003941 "\n"
3942 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00003943 "-kernel bzImage use 'bzImage' as kernel image\n"
3944 "-append cmdline use 'cmdline' as kernel command line\n"
3945 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00003946 "\n"
bellard330d0412003-07-26 18:11:40 +00003947 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00003948 "-monitor dev redirect the monitor to char device 'dev'\n"
3949 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00003950 "-parallel dev redirect the parallel port to char device 'dev'\n"
bellardf7cce892004-12-08 22:21:25 +00003951 "-pidfile file Write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00003952 "-S freeze CPU at startup (use 'c' to start execution)\n"
pbrookcfc34752007-02-22 01:48:01 +00003953 "-s wait gdb connection to port\n"
3954 "-p port set gdb connection port [default=%s]\n"
bellardf193c792004-03-21 17:06:25 +00003955 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
bellard46d47672004-11-16 01:45:27 +00003956 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3957 " translation (t=none or lba) (usually qemu can guess them)\n"
bellard87b47352006-08-17 17:22:54 +00003958 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
bellardd993e022005-02-10 22:00:06 +00003959#ifdef USE_KQEMU
bellard6515b202006-05-03 22:02:44 +00003960 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
bellardd993e022005-02-10 22:00:06 +00003961 "-no-kqemu disable KQEMU kernel module usage\n"
3962#endif
aliguori7ba1e612008-11-05 16:04:33 +00003963#ifdef CONFIG_KVM
3964 "-enable-kvm enable KVM full virtualization support\n"
3965#endif
bellardbb0c6722004-06-20 12:37:32 +00003966#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00003967 "-no-acpi disable ACPI\n"
aliguori16b29ae2008-12-17 23:28:44 +00003968 "-no-hpet disable HPET\n"
bellardbb0c6722004-06-20 12:37:32 +00003969#endif
balrog4d3b6f62008-02-10 16:33:14 +00003970#ifdef CONFIG_CURSES
3971 "-curses use a curses/ncurses interface instead of SDL\n"
3972#endif
bellardd1beab82006-10-02 19:44:22 +00003973 "-no-reboot exit instead of rebooting\n"
aurel32b2f76162008-04-11 21:35:52 +00003974 "-no-shutdown stop before shutdown\n"
aurel32a8080002008-05-10 23:28:26 +00003975 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
bellard24236862006-04-30 21:28:36 +00003976 "-vnc display start a VNC server on display\n"
ths71e3ceb2006-12-22 02:11:31 +00003977#ifndef _WIN32
3978 "-daemonize daemonize QEMU after initializing\n"
3979#endif
ths9ae02552007-01-05 17:39:04 +00003980 "-option-rom rom load a file, rom, into the option ROM space\n"
blueswir166508602007-05-01 14:16:52 +00003981#ifdef TARGET_SPARC
3982 "-prom-env variable=value set OpenBIOS nvram variables\n"
3983#endif
thsf3dcfad2007-08-24 01:26:02 +00003984 "-clock force the use of the given methods for timer alarm.\n"
aurel323adda042008-03-09 23:43:49 +00003985 " To see what timers are available use -clock ?\n"
bellardbce61842008-02-01 22:18:51 +00003986 "-startdate select initial date of the clock\n"
pbrook2e70f6e2008-06-29 01:03:05 +00003987 "-icount [N|auto]\n"
pbrookdd5d6fe2008-06-29 10:43:16 +00003988 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
bellard0824d6f2003-06-24 13:42:40 +00003989 "\n"
bellard82c643f2004-07-14 17:28:13 +00003990 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00003991 "ctrl-alt-f toggle full screen\n"
3992 "ctrl-alt-n switch to virtual console 'n'\n"
3993 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00003994 "\n"
3995 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3996 ,
bellard0db63472003-10-27 21:37:46 +00003997 "qemu",
bellarda00bad72004-05-22 21:39:06 +00003998 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00003999#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004000 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004001 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004002#endif
bellard6e44ba72004-01-18 21:56:49 +00004003 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004004 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004005 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004006}
4007
bellardcd6f1162004-05-13 22:02:20 +00004008#define HAS_ARG 0x0001
4009
4010enum {
4011 QEMU_OPTION_h,
4012
bellardcc1daa42005-06-05 14:49:17 +00004013 QEMU_OPTION_M,
j_mayer94fc95c2007-03-05 19:44:02 +00004014 QEMU_OPTION_cpu,
bellardcd6f1162004-05-13 22:02:20 +00004015 QEMU_OPTION_fda,
4016 QEMU_OPTION_fdb,
4017 QEMU_OPTION_hda,
4018 QEMU_OPTION_hdb,
4019 QEMU_OPTION_hdc,
4020 QEMU_OPTION_hdd,
thse4bcb142007-12-02 04:51:10 +00004021 QEMU_OPTION_drive,
bellardcd6f1162004-05-13 22:02:20 +00004022 QEMU_OPTION_cdrom,
balrog3e3d5812007-04-30 02:09:25 +00004023 QEMU_OPTION_mtdblock,
pbrooka1bb27b2007-04-06 16:49:48 +00004024 QEMU_OPTION_sd,
j_mayer86f55662007-04-24 06:52:59 +00004025 QEMU_OPTION_pflash,
bellardcd6f1162004-05-13 22:02:20 +00004026 QEMU_OPTION_boot,
4027 QEMU_OPTION_snapshot,
bellard52ca8d62006-06-14 16:03:05 +00004028#ifdef TARGET_I386
4029 QEMU_OPTION_no_fd_bootchk,
4030#endif
bellardcd6f1162004-05-13 22:02:20 +00004031 QEMU_OPTION_m,
4032 QEMU_OPTION_nographic,
balroga171fe32007-04-30 01:48:07 +00004033 QEMU_OPTION_portrait,
bellard1d14ffa2005-10-30 18:58:22 +00004034#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004035 QEMU_OPTION_audio_help,
4036 QEMU_OPTION_soundhw,
4037#endif
bellardcd6f1162004-05-13 22:02:20 +00004038
bellard7c9d8e02005-11-15 22:16:05 +00004039 QEMU_OPTION_net,
bellardc7f74642004-08-24 21:57:12 +00004040 QEMU_OPTION_tftp,
ths47d5d012007-02-20 00:05:08 +00004041 QEMU_OPTION_bootp,
bellard9d728e82004-09-05 23:09:03 +00004042 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00004043 QEMU_OPTION_redir,
balrogdc72ac12008-11-09 00:04:26 +00004044 QEMU_OPTION_bt,
bellardcd6f1162004-05-13 22:02:20 +00004045
4046 QEMU_OPTION_kernel,
4047 QEMU_OPTION_append,
4048 QEMU_OPTION_initrd,
4049
4050 QEMU_OPTION_S,
4051 QEMU_OPTION_s,
4052 QEMU_OPTION_p,
4053 QEMU_OPTION_d,
4054 QEMU_OPTION_hdachs,
4055 QEMU_OPTION_L,
j_mayer1192dad2007-10-05 13:08:35 +00004056 QEMU_OPTION_bios,
bellard3d11d0e2004-12-12 16:56:30 +00004057 QEMU_OPTION_k,
bellardee22c2f2004-06-03 12:49:50 +00004058 QEMU_OPTION_localtime,
bellarde9b137c2004-06-21 16:46:10 +00004059 QEMU_OPTION_g,
malc3893c122008-09-28 00:42:05 +00004060 QEMU_OPTION_vga,
ths20d8a3e2007-02-18 17:04:49 +00004061 QEMU_OPTION_echr,
bellard82c643f2004-07-14 17:28:13 +00004062 QEMU_OPTION_monitor,
4063 QEMU_OPTION_serial,
bellard6508fe52005-01-15 12:02:56 +00004064 QEMU_OPTION_parallel,
bellardd63d3072004-10-03 13:29:03 +00004065 QEMU_OPTION_loadvm,
4066 QEMU_OPTION_full_screen,
ths43523e92007-02-18 18:19:32 +00004067 QEMU_OPTION_no_frame,
ths3780e192007-06-21 21:08:02 +00004068 QEMU_OPTION_alt_grab,
ths667acca2006-12-11 02:08:05 +00004069 QEMU_OPTION_no_quit,
bellardf7cce892004-12-08 22:21:25 +00004070 QEMU_OPTION_pidfile,
bellardd993e022005-02-10 22:00:06 +00004071 QEMU_OPTION_no_kqemu,
bellard89bfc102006-02-08 22:46:31 +00004072 QEMU_OPTION_kernel_kqemu,
aliguori7ba1e612008-11-05 16:04:33 +00004073 QEMU_OPTION_enable_kvm,
bellarda09db212005-04-30 16:10:35 +00004074 QEMU_OPTION_win2k_hack,
bellardbb36d472005-11-05 14:22:28 +00004075 QEMU_OPTION_usb,
bellarda594cfb2005-11-06 16:13:29 +00004076 QEMU_OPTION_usbdevice,
bellard6a00d602005-11-21 23:25:50 +00004077 QEMU_OPTION_smp,
bellard24236862006-04-30 21:28:36 +00004078 QEMU_OPTION_vnc,
bellard6515b202006-05-03 22:02:44 +00004079 QEMU_OPTION_no_acpi,
aliguori16b29ae2008-12-17 23:28:44 +00004080 QEMU_OPTION_no_hpet,
balrog4d3b6f62008-02-10 16:33:14 +00004081 QEMU_OPTION_curses,
bellardd1beab82006-10-02 19:44:22 +00004082 QEMU_OPTION_no_reboot,
aurel32b2f76162008-04-11 21:35:52 +00004083 QEMU_OPTION_no_shutdown,
balrog9467cd42007-05-01 01:34:14 +00004084 QEMU_OPTION_show_cursor,
ths71e3ceb2006-12-22 02:11:31 +00004085 QEMU_OPTION_daemonize,
ths9ae02552007-01-05 17:39:04 +00004086 QEMU_OPTION_option_rom,
thsc35734b2007-03-19 15:17:08 +00004087 QEMU_OPTION_semihosting,
4088 QEMU_OPTION_name,
blueswir166508602007-05-01 14:16:52 +00004089 QEMU_OPTION_prom_env,
balrog2b8f2d42007-07-27 22:08:46 +00004090 QEMU_OPTION_old_param,
thsf3dcfad2007-08-24 01:26:02 +00004091 QEMU_OPTION_clock,
bellard7e0af5d02007-11-07 16:24:33 +00004092 QEMU_OPTION_startdate,
bellard26a5f132008-05-28 12:30:31 +00004093 QEMU_OPTION_tb_size,
pbrook2e70f6e2008-06-29 01:03:05 +00004094 QEMU_OPTION_icount,
blueswir18fcb1b92008-09-18 18:29:08 +00004095 QEMU_OPTION_uuid,
aliguori5bb79102008-10-13 03:12:02 +00004096 QEMU_OPTION_incoming,
bellardcd6f1162004-05-13 22:02:20 +00004097};
4098
4099typedef struct QEMUOption {
4100 const char *name;
4101 int flags;
4102 int index;
4103} QEMUOption;
4104
blueswir1dbed7e42008-10-01 19:38:09 +00004105static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004106 { "h", 0, QEMU_OPTION_h },
pbrook64423fb2007-01-27 17:11:41 +00004107 { "help", 0, QEMU_OPTION_h },
bellardcd6f1162004-05-13 22:02:20 +00004108
bellardcc1daa42005-06-05 14:49:17 +00004109 { "M", HAS_ARG, QEMU_OPTION_M },
j_mayer94fc95c2007-03-05 19:44:02 +00004110 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
bellardcd6f1162004-05-13 22:02:20 +00004111 { "fda", HAS_ARG, QEMU_OPTION_fda },
4112 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
4113 { "hda", HAS_ARG, QEMU_OPTION_hda },
4114 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
4115 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
4116 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
thse4bcb142007-12-02 04:51:10 +00004117 { "drive", HAS_ARG, QEMU_OPTION_drive },
bellardcd6f1162004-05-13 22:02:20 +00004118 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
balrog3e3d5812007-04-30 02:09:25 +00004119 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
pbrooka1bb27b2007-04-06 16:49:48 +00004120 { "sd", HAS_ARG, QEMU_OPTION_sd },
j_mayer86f55662007-04-24 06:52:59 +00004121 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
bellardcd6f1162004-05-13 22:02:20 +00004122 { "boot", HAS_ARG, QEMU_OPTION_boot },
4123 { "snapshot", 0, QEMU_OPTION_snapshot },
bellard52ca8d62006-06-14 16:03:05 +00004124#ifdef TARGET_I386
4125 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
4126#endif
bellardcd6f1162004-05-13 22:02:20 +00004127 { "m", HAS_ARG, QEMU_OPTION_m },
4128 { "nographic", 0, QEMU_OPTION_nographic },
balroga171fe32007-04-30 01:48:07 +00004129 { "portrait", 0, QEMU_OPTION_portrait },
bellard3d11d0e2004-12-12 16:56:30 +00004130 { "k", HAS_ARG, QEMU_OPTION_k },
bellard1d14ffa2005-10-30 18:58:22 +00004131#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004132 { "audio-help", 0, QEMU_OPTION_audio_help },
4133 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
4134#endif
bellardcd6f1162004-05-13 22:02:20 +00004135
bellard7c9d8e02005-11-15 22:16:05 +00004136 { "net", HAS_ARG, QEMU_OPTION_net},
bellard158156d2004-05-17 21:13:42 +00004137#ifdef CONFIG_SLIRP
bellardc7f74642004-08-24 21:57:12 +00004138 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
ths47d5d012007-02-20 00:05:08 +00004139 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
bellardc94c8d62004-09-13 21:37:34 +00004140#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004141 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00004142#endif
bellard9bf05442004-08-25 22:12:49 +00004143 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00004144#endif
balrogdc72ac12008-11-09 00:04:26 +00004145 { "bt", HAS_ARG, QEMU_OPTION_bt },
bellardcd6f1162004-05-13 22:02:20 +00004146
4147 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
4148 { "append", HAS_ARG, QEMU_OPTION_append },
4149 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
4150
4151 { "S", 0, QEMU_OPTION_S },
4152 { "s", 0, QEMU_OPTION_s },
4153 { "p", HAS_ARG, QEMU_OPTION_p },
4154 { "d", HAS_ARG, QEMU_OPTION_d },
4155 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
4156 { "L", HAS_ARG, QEMU_OPTION_L },
j_mayer1192dad2007-10-05 13:08:35 +00004157 { "bios", HAS_ARG, QEMU_OPTION_bios },
bellardd993e022005-02-10 22:00:06 +00004158#ifdef USE_KQEMU
4159 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
bellard89bfc102006-02-08 22:46:31 +00004160 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
bellardd993e022005-02-10 22:00:06 +00004161#endif
aliguori7ba1e612008-11-05 16:04:33 +00004162#ifdef CONFIG_KVM
4163 { "enable-kvm", 0, QEMU_OPTION_enable_kvm },
4164#endif
bellard6f7e9ae2005-03-13 09:43:36 +00004165#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00004166 { "g", 1, QEMU_OPTION_g },
bellard77d4bc32004-05-26 22:13:53 +00004167#endif
bellardee22c2f2004-06-03 12:49:50 +00004168 { "localtime", 0, QEMU_OPTION_localtime },
malc3893c122008-09-28 00:42:05 +00004169 { "vga", HAS_ARG, QEMU_OPTION_vga },
balrog8b6e0722007-06-22 08:23:44 +00004170 { "echr", HAS_ARG, QEMU_OPTION_echr },
4171 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
4172 { "serial", HAS_ARG, QEMU_OPTION_serial },
4173 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
bellardd63d3072004-10-03 13:29:03 +00004174 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
4175 { "full-screen", 0, QEMU_OPTION_full_screen },
ths667acca2006-12-11 02:08:05 +00004176#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00004177 { "no-frame", 0, QEMU_OPTION_no_frame },
ths3780e192007-06-21 21:08:02 +00004178 { "alt-grab", 0, QEMU_OPTION_alt_grab },
ths667acca2006-12-11 02:08:05 +00004179 { "no-quit", 0, QEMU_OPTION_no_quit },
4180#endif
bellardf7cce892004-12-08 22:21:25 +00004181 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellarda09db212005-04-30 16:10:35 +00004182 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
bellarda594cfb2005-11-06 16:13:29 +00004183 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
bellard6a00d602005-11-21 23:25:50 +00004184 { "smp", HAS_ARG, QEMU_OPTION_smp },
bellard24236862006-04-30 21:28:36 +00004185 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
balrog4d3b6f62008-02-10 16:33:14 +00004186#ifdef CONFIG_CURSES
4187 { "curses", 0, QEMU_OPTION_curses },
4188#endif
blueswir18fcb1b92008-09-18 18:29:08 +00004189 { "uuid", HAS_ARG, QEMU_OPTION_uuid },
ths96d30e42007-01-07 20:42:14 +00004190
bellard1f042752004-06-05 13:46:47 +00004191 /* temporary options */
bellarda594cfb2005-11-06 16:13:29 +00004192 { "usb", 0, QEMU_OPTION_usb },
bellard6515b202006-05-03 22:02:44 +00004193 { "no-acpi", 0, QEMU_OPTION_no_acpi },
aliguori16b29ae2008-12-17 23:28:44 +00004194 { "no-hpet", 0, QEMU_OPTION_no_hpet },
bellardd1beab82006-10-02 19:44:22 +00004195 { "no-reboot", 0, QEMU_OPTION_no_reboot },
aurel32b2f76162008-04-11 21:35:52 +00004196 { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
balrog9467cd42007-05-01 01:34:14 +00004197 { "show-cursor", 0, QEMU_OPTION_show_cursor },
ths71e3ceb2006-12-22 02:11:31 +00004198 { "daemonize", 0, QEMU_OPTION_daemonize },
ths9ae02552007-01-05 17:39:04 +00004199 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
pbrooka87295e2007-05-26 15:09:38 +00004200#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00004201 { "semihosting", 0, QEMU_OPTION_semihosting },
4202#endif
thsc35734b2007-03-19 15:17:08 +00004203 { "name", HAS_ARG, QEMU_OPTION_name },
blueswir195efd112008-12-24 20:26:14 +00004204#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00004205 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
4206#endif
balrog2b8f2d42007-07-27 22:08:46 +00004207#if defined(TARGET_ARM)
4208 { "old-param", 0, QEMU_OPTION_old_param },
4209#endif
thsf3dcfad2007-08-24 01:26:02 +00004210 { "clock", HAS_ARG, QEMU_OPTION_clock },
bellard7e0af5d02007-11-07 16:24:33 +00004211 { "startdate", HAS_ARG, QEMU_OPTION_startdate },
bellard26a5f132008-05-28 12:30:31 +00004212 { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
pbrook2e70f6e2008-06-29 01:03:05 +00004213 { "icount", HAS_ARG, QEMU_OPTION_icount },
aliguori5bb79102008-10-13 03:12:02 +00004214 { "incoming", HAS_ARG, QEMU_OPTION_incoming },
bellardcd6f1162004-05-13 22:02:20 +00004215 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004216};
4217
bellard5905b2e2004-08-01 21:53:26 +00004218/* password input */
4219
balrog2bac6012007-04-30 01:34:31 +00004220int qemu_key_check(BlockDriverState *bs, const char *name)
4221{
4222 char password[256];
4223 int i;
4224
4225 if (!bdrv_is_encrypted(bs))
4226 return 0;
4227
4228 term_printf("%s is encrypted.\n", name);
4229 for(i = 0; i < 3; i++) {
4230 monitor_readline("Password: ", 1, password, sizeof(password));
4231 if (bdrv_set_key(bs, password) == 0)
4232 return 0;
4233 term_printf("invalid password\n");
4234 }
4235 return -EPERM;
4236}
4237
aliguori83ab7952008-08-19 14:44:22 +00004238static BlockDriverState *get_bdrv(int index)
4239{
4240 if (index > nb_drives)
4241 return NULL;
4242 return drives_table[index].bdrv;
4243}
4244
bellard5905b2e2004-08-01 21:53:26 +00004245static void read_passwords(void)
4246{
4247 BlockDriverState *bs;
balrog2bac6012007-04-30 01:34:31 +00004248 int i;
bellard5905b2e2004-08-01 21:53:26 +00004249
aliguori83ab7952008-08-19 14:44:22 +00004250 for(i = 0; i < 6; i++) {
4251 bs = get_bdrv(i);
4252 if (bs)
4253 qemu_key_check(bs, bdrv_get_device_name(bs));
bellard5905b2e2004-08-01 21:53:26 +00004254 }
4255}
4256
bellard1d14ffa2005-10-30 18:58:22 +00004257#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004258struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004259#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004260#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004261 {
4262 "pcspk",
4263 "PC speaker",
4264 0,
4265 1,
4266 { .init_isa = pcspk_audio_init }
4267 },
4268#endif
malc4c9b53e2009-01-09 10:46:34 +00004269
4270#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004271 {
4272 "sb16",
4273 "Creative Sound Blaster 16",
4274 0,
4275 1,
4276 { .init_isa = SB16_init }
4277 },
malc4c9b53e2009-01-09 10:46:34 +00004278#endif
bellard6a36d842005-12-18 20:34:32 +00004279
malccc53d262008-06-13 10:48:22 +00004280#ifdef CONFIG_CS4231A
4281 {
4282 "cs4231a",
4283 "CS4231A",
4284 0,
4285 1,
4286 { .init_isa = cs4231a_init }
4287 },
4288#endif
4289
bellard6a36d842005-12-18 20:34:32 +00004290#ifdef CONFIG_ADLIB
4291 {
4292 "adlib",
4293#ifdef HAS_YMF262
4294 "Yamaha YMF262 (OPL3)",
4295#else
4296 "Yamaha YM3812 (OPL2)",
4297#endif
4298 0,
4299 1,
4300 { .init_isa = Adlib_init }
4301 },
4302#endif
4303
4304#ifdef CONFIG_GUS
4305 {
4306 "gus",
4307 "Gravis Ultrasound GF1",
4308 0,
4309 1,
4310 { .init_isa = GUS_init }
4311 },
4312#endif
4313
malc4c9b53e2009-01-09 10:46:34 +00004314#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004315 {
4316 "ac97",
4317 "Intel 82801AA AC97 Audio",
4318 0,
4319 0,
4320 { .init_pci = ac97_init }
4321 },
malc4c9b53e2009-01-09 10:46:34 +00004322#endif
balroge5c9a132008-01-14 04:27:55 +00004323
malc4c9b53e2009-01-09 10:46:34 +00004324#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004325 {
4326 "es1370",
4327 "ENSONIQ AudioPCI ES1370",
4328 0,
4329 0,
4330 { .init_pci = es1370_init }
4331 },
balrogb00052e2007-04-30 02:22:06 +00004332#endif
bellard6a36d842005-12-18 20:34:32 +00004333
malc4c9b53e2009-01-09 10:46:34 +00004334#endif /* HAS_AUDIO_CHOICE */
4335
bellard6a36d842005-12-18 20:34:32 +00004336 { NULL, NULL, 0, 0, { NULL } }
4337};
4338
bellard1d14ffa2005-10-30 18:58:22 +00004339static void select_soundhw (const char *optarg)
4340{
bellard6a36d842005-12-18 20:34:32 +00004341 struct soundhw *c;
4342
bellard1d14ffa2005-10-30 18:58:22 +00004343 if (*optarg == '?') {
4344 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004345
bellard1d14ffa2005-10-30 18:58:22 +00004346 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004347 for (c = soundhw; c->name; ++c) {
4348 printf ("%-11s %s\n", c->name, c->descr);
4349 }
4350 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004351 exit (*optarg != '?');
4352 }
4353 else {
bellard6a36d842005-12-18 20:34:32 +00004354 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004355 const char *p;
4356 char *e;
4357 int bad_card = 0;
4358
bellard6a36d842005-12-18 20:34:32 +00004359 if (!strcmp (optarg, "all")) {
4360 for (c = soundhw; c->name; ++c) {
4361 c->enabled = 1;
4362 }
4363 return;
4364 }
bellard1d14ffa2005-10-30 18:58:22 +00004365
bellard6a36d842005-12-18 20:34:32 +00004366 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004367 while (*p) {
4368 e = strchr (p, ',');
4369 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004370
4371 for (c = soundhw; c->name; ++c) {
4372 if (!strncmp (c->name, p, l)) {
4373 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004374 break;
4375 }
4376 }
bellard6a36d842005-12-18 20:34:32 +00004377
4378 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004379 if (l > 80) {
4380 fprintf (stderr,
4381 "Unknown sound card name (too big to show)\n");
4382 }
4383 else {
4384 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4385 (int) l, p);
4386 }
4387 bad_card = 1;
4388 }
4389 p += l + (e != NULL);
4390 }
4391
4392 if (bad_card)
4393 goto show_valid_cards;
4394 }
4395}
4396#endif
4397
malc3893c122008-09-28 00:42:05 +00004398static void select_vgahw (const char *p)
4399{
4400 const char *opts;
4401
4402 if (strstart(p, "std", &opts)) {
4403 cirrus_vga_enabled = 0;
4404 vmsvga_enabled = 0;
4405 } else if (strstart(p, "cirrus", &opts)) {
4406 cirrus_vga_enabled = 1;
4407 vmsvga_enabled = 0;
4408 } else if (strstart(p, "vmware", &opts)) {
4409 cirrus_vga_enabled = 0;
4410 vmsvga_enabled = 1;
4411 } else {
4412 invalid_vga:
4413 fprintf(stderr, "Unknown vga type: %s\n", p);
4414 exit(1);
4415 }
malccb5a7aa2008-09-28 00:42:12 +00004416 while (*opts) {
4417 const char *nextopt;
4418
4419 if (strstart(opts, ",retrace=", &nextopt)) {
4420 opts = nextopt;
4421 if (strstart(opts, "dumb", &nextopt))
4422 vga_retrace_method = VGA_RETRACE_DUMB;
4423 else if (strstart(opts, "precise", &nextopt))
4424 vga_retrace_method = VGA_RETRACE_PRECISE;
4425 else goto invalid_vga;
4426 } else goto invalid_vga;
4427 opts = nextopt;
4428 }
malc3893c122008-09-28 00:42:05 +00004429}
4430
bellard3587d7e2006-06-26 20:03:44 +00004431#ifdef _WIN32
4432static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4433{
4434 exit(STATUS_CONTROL_C_EXIT);
4435 return TRUE;
4436}
4437#endif
4438
blueswir18fcb1b92008-09-18 18:29:08 +00004439static int qemu_uuid_parse(const char *str, uint8_t *uuid)
4440{
4441 int ret;
4442
4443 if(strlen(str) != 36)
4444 return -1;
4445
4446 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4447 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4448 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4449
4450 if(ret != 16)
4451 return -1;
4452
4453 return 0;
4454}
4455
bellard7c9d8e02005-11-15 22:16:05 +00004456#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004457
aliguori5b08fc12008-08-21 20:08:03 +00004458#ifndef _WIN32
4459
4460static void termsig_handler(int signal)
4461{
4462 qemu_system_shutdown_request();
4463}
4464
blueswir16f9e3802008-09-09 18:56:59 +00004465static void termsig_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004466{
4467 struct sigaction act;
4468
4469 memset(&act, 0, sizeof(act));
4470 act.sa_handler = termsig_handler;
4471 sigaction(SIGINT, &act, NULL);
4472 sigaction(SIGHUP, &act, NULL);
4473 sigaction(SIGTERM, &act, NULL);
4474}
4475
4476#endif
4477
malc902b3d52008-12-10 19:18:40 +00004478int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004479{
bellard67b915a2004-03-31 23:37:16 +00004480#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00004481 int use_gdbstub;
4482 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00004483#endif
j_mayer28c5af52007-11-11 01:50:45 +00004484 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004485 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004486 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004487 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004488 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00004489 const char *boot_devices = "";
bellard313aa562003-08-10 21:52:11 +00004490 DisplayState *ds = &display_state;
bellard46d47672004-11-16 01:45:27 +00004491 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004492 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004493 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004494 const char *bt_opts[MAX_BT_CMDLINE];
4495 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004496 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004497 int optind;
4498 const char *r, *optarg;
bellard82c643f2004-07-14 17:28:13 +00004499 CharDriverState *monitor_hd;
pbrookfd5f3932008-03-26 20:55:43 +00004500 const char *monitor_device;
4501 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004502 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004503 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004504 int parallel_device_index;
bellardd63d3072004-10-03 13:29:03 +00004505 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004506 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004507 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004508 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004509 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00004510 int fds[2];
bellard26a5f132008-05-28 12:30:31 +00004511 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004512 const char *pid_file = NULL;
blueswir141bd6392008-10-05 09:56:21 +00004513 int autostart;
aliguori5bb79102008-10-13 03:12:02 +00004514 const char *incoming = NULL;
bellard0bd48852005-11-11 00:00:47 +00004515
malc902b3d52008-12-10 19:18:40 +00004516 qemu_cache_utils_init(envp);
4517
bellard0bd48852005-11-11 00:00:47 +00004518 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004519#ifndef _WIN32
4520 {
4521 struct sigaction act;
4522 sigfillset(&act.sa_mask);
4523 act.sa_flags = 0;
4524 act.sa_handler = SIG_IGN;
4525 sigaction(SIGPIPE, &act, NULL);
4526 }
bellard3587d7e2006-06-26 20:03:44 +00004527#else
4528 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004529 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4530 QEMU to run on a single CPU */
4531 {
4532 HANDLE h;
4533 DWORD mask, smask;
4534 int i;
4535 h = GetCurrentProcess();
4536 if (GetProcessAffinityMask(h, &mask, &smask)) {
4537 for(i = 0; i < 32; i++) {
4538 if (mask & (1 << i))
4539 break;
4540 }
4541 if (i != 32) {
4542 mask = 1 << i;
4543 SetProcessAffinityMask(h, mask);
4544 }
4545 }
4546 }
bellard67b915a2004-03-31 23:37:16 +00004547#endif
bellardbe995c22006-06-25 16:25:21 +00004548
bellardcc1daa42005-06-05 14:49:17 +00004549 register_machines();
4550 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004551 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004552 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004553 ram_size = 0;
bellard313aa562003-08-10 21:52:11 +00004554 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00004555#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00004556 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00004557 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00004558#endif
bellard33e39632003-07-06 17:15:21 +00004559 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004560 nographic = 0;
balrog4d3b6f62008-02-10 16:33:14 +00004561 curses = 0;
bellarda20dd502003-09-30 21:07:02 +00004562 kernel_filename = NULL;
4563 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004564 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004565 translation = BIOS_ATA_TRANSLATION_AUTO;
pbrookc60e08d2008-07-01 16:24:38 +00004566 monitor_device = "vc";
bellardc4b1fcc2004-03-14 21:44:30 +00004567
aurel32c75a8232008-05-04 00:50:34 +00004568 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004569 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004570 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004571 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004572
aurel32c75a8232008-05-04 00:50:34 +00004573 parallel_devices[0] = "vc:640x480";
bellard6508fe52005-01-15 12:02:56 +00004574 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004575 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004576 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004577
bellarda594cfb2005-11-06 16:13:29 +00004578 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004579
bellard7c9d8e02005-11-15 22:16:05 +00004580 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004581 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004582 nb_drives = 0;
4583 nb_drives_opt = 0;
4584 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004585
4586 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004587
bellard26a5f132008-05-28 12:30:31 +00004588 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004589 autostart= 1;
4590
bellardcd6f1162004-05-13 22:02:20 +00004591 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004592 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004593 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004594 break;
bellardcd6f1162004-05-13 22:02:20 +00004595 r = argv[optind];
4596 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004597 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004598 } else {
4599 const QEMUOption *popt;
4600
4601 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004602 /* Treat --foo the same as -foo. */
4603 if (r[1] == '-')
4604 r++;
bellardcd6f1162004-05-13 22:02:20 +00004605 popt = qemu_options;
4606 for(;;) {
4607 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004608 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004609 argv[0], r);
4610 exit(1);
4611 }
4612 if (!strcmp(popt->name, r + 1))
4613 break;
4614 popt++;
4615 }
4616 if (popt->flags & HAS_ARG) {
4617 if (optind >= argc) {
4618 fprintf(stderr, "%s: option '%s' requires an argument\n",
4619 argv[0], r);
4620 exit(1);
4621 }
4622 optarg = argv[optind++];
4623 } else {
4624 optarg = NULL;
4625 }
4626
4627 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004628 case QEMU_OPTION_M:
4629 machine = find_machine(optarg);
4630 if (!machine) {
4631 QEMUMachine *m;
4632 printf("Supported machines are:\n");
4633 for(m = first_machine; m != NULL; m = m->next) {
4634 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004635 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00004636 m == first_machine ? " (default)" : "");
4637 }
ths15f82202007-06-29 23:26:08 +00004638 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004639 }
4640 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004641 case QEMU_OPTION_cpu:
4642 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004643 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004644/* XXX: implement xxx_cpu_list for targets that still miss it */
4645#if defined(cpu_list)
4646 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004647#endif
ths15f82202007-06-29 23:26:08 +00004648 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004649 } else {
4650 cpu_model = optarg;
4651 }
4652 break;
bellardcd6f1162004-05-13 22:02:20 +00004653 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004654 initrd_filename = optarg;
4655 break;
bellardcd6f1162004-05-13 22:02:20 +00004656 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004657 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00004658 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004659 else
balrog609497a2008-01-14 02:56:53 +00004660 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004661 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004662 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004663 translation == BIOS_ATA_TRANSLATION_LBA ?
4664 ",trans=lba" :
4665 translation == BIOS_ATA_TRANSLATION_NONE ?
4666 ",trans=none" : "");
4667 break;
bellardcd6f1162004-05-13 22:02:20 +00004668 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004669 case QEMU_OPTION_hdc:
4670 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004671 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004672 break;
thse4bcb142007-12-02 04:51:10 +00004673 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004674 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004675 break;
balrog3e3d5812007-04-30 02:09:25 +00004676 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004677 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004678 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004679 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004680 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004681 break;
j_mayer86f55662007-04-24 06:52:59 +00004682 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004683 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004684 break;
bellardcd6f1162004-05-13 22:02:20 +00004685 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004686 snapshot = 1;
4687 break;
bellardcd6f1162004-05-13 22:02:20 +00004688 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004689 {
bellard330d0412003-07-26 18:11:40 +00004690 const char *p;
4691 p = optarg;
4692 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004693 if (cyls < 1 || cyls > 16383)
4694 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004695 if (*p != ',')
4696 goto chs_fail;
4697 p++;
4698 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004699 if (heads < 1 || heads > 16)
4700 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004701 if (*p != ',')
4702 goto chs_fail;
4703 p++;
4704 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004705 if (secs < 1 || secs > 63)
4706 goto chs_fail;
4707 if (*p == ',') {
4708 p++;
4709 if (!strcmp(p, "none"))
4710 translation = BIOS_ATA_TRANSLATION_NONE;
4711 else if (!strcmp(p, "lba"))
4712 translation = BIOS_ATA_TRANSLATION_LBA;
4713 else if (!strcmp(p, "auto"))
4714 translation = BIOS_ATA_TRANSLATION_AUTO;
4715 else
4716 goto chs_fail;
4717 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004718 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004719 fprintf(stderr, "qemu: invalid physical CHS format\n");
4720 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004721 }
thse4bcb142007-12-02 04:51:10 +00004722 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00004723 snprintf(drives_opt[hda_index].opt,
4724 sizeof(drives_opt[hda_index].opt),
4725 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4726 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004727 translation == BIOS_ATA_TRANSLATION_LBA ?
4728 ",trans=lba" :
4729 translation == BIOS_ATA_TRANSLATION_NONE ?
4730 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00004731 }
4732 break;
bellardcd6f1162004-05-13 22:02:20 +00004733 case QEMU_OPTION_nographic:
bellarda20dd502003-09-30 21:07:02 +00004734 nographic = 1;
4735 break;
balrog4d3b6f62008-02-10 16:33:14 +00004736#ifdef CONFIG_CURSES
4737 case QEMU_OPTION_curses:
4738 curses = 1;
4739 break;
4740#endif
balroga171fe32007-04-30 01:48:07 +00004741 case QEMU_OPTION_portrait:
4742 graphic_rotate = 1;
4743 break;
bellardcd6f1162004-05-13 22:02:20 +00004744 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004745 kernel_filename = optarg;
4746 break;
bellardcd6f1162004-05-13 22:02:20 +00004747 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004748 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004749 break;
bellardcd6f1162004-05-13 22:02:20 +00004750 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004751 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004752 break;
bellardcd6f1162004-05-13 22:02:20 +00004753 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004754 boot_devices = optarg;
4755 /* We just do some generic consistency checks */
4756 {
4757 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00004758 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00004759
4760 boot_devices_bitmap = 0;
4761 for (p = boot_devices; *p != '\0'; p++) {
4762 /* Allowed boot devices are:
4763 * a b : floppy disk drives
4764 * c ... f : IDE disk drives
4765 * g ... m : machine implementation dependant drives
4766 * n ... p : network devices
4767 * It's up to each machine implementation to check
4768 * if the given boot devices match the actual hardware
4769 * implementation and firmware features.
4770 */
4771 if (*p < 'a' || *p > 'q') {
4772 fprintf(stderr, "Invalid boot device '%c'\n", *p);
4773 exit(1);
4774 }
4775 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4776 fprintf(stderr,
4777 "Boot device '%c' was given twice\n",*p);
4778 exit(1);
4779 }
4780 boot_devices_bitmap |= 1 << (*p - 'a');
4781 }
bellard36b486b2003-11-11 13:36:08 +00004782 }
4783 break;
bellardcd6f1162004-05-13 22:02:20 +00004784 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004785 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004786 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004787 break;
bellard52ca8d62006-06-14 16:03:05 +00004788#ifdef TARGET_I386
4789 case QEMU_OPTION_no_fd_bootchk:
4790 fd_bootchk = 0;
4791 break;
4792#endif
bellard7c9d8e02005-11-15 22:16:05 +00004793 case QEMU_OPTION_net:
4794 if (nb_net_clients >= MAX_NET_CLIENTS) {
4795 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00004796 exit(1);
4797 }
pbrookfd5f3932008-03-26 20:55:43 +00004798 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00004799 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00004800 break;
bellardc7f74642004-08-24 21:57:12 +00004801#ifdef CONFIG_SLIRP
4802 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00004803 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004804 break;
ths47d5d012007-02-20 00:05:08 +00004805 case QEMU_OPTION_bootp:
4806 bootp_filename = optarg;
4807 break;
bellardc94c8d62004-09-13 21:37:34 +00004808#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004809 case QEMU_OPTION_smb:
4810 net_slirp_smb(optarg);
4811 break;
bellardc94c8d62004-09-13 21:37:34 +00004812#endif
bellard9bf05442004-08-25 22:12:49 +00004813 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00004814 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00004815 break;
bellardc7f74642004-08-24 21:57:12 +00004816#endif
balrogdc72ac12008-11-09 00:04:26 +00004817 case QEMU_OPTION_bt:
4818 if (nb_bt_opts >= MAX_BT_CMDLINE) {
4819 fprintf(stderr, "qemu: too many bluetooth options\n");
4820 exit(1);
4821 }
4822 bt_opts[nb_bt_opts++] = optarg;
4823 break;
bellard1d14ffa2005-10-30 18:58:22 +00004824#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004825 case QEMU_OPTION_audio_help:
4826 AUD_help ();
4827 exit (0);
4828 break;
4829 case QEMU_OPTION_soundhw:
4830 select_soundhw (optarg);
4831 break;
4832#endif
bellardcd6f1162004-05-13 22:02:20 +00004833 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00004834 help(0);
bellardcd6f1162004-05-13 22:02:20 +00004835 break;
aurel3200f82b82008-04-27 21:12:55 +00004836 case QEMU_OPTION_m: {
4837 uint64_t value;
4838 char *ptr;
4839
4840 value = strtoul(optarg, &ptr, 10);
4841 switch (*ptr) {
4842 case 0: case 'M': case 'm':
4843 value <<= 20;
4844 break;
4845 case 'G': case 'g':
4846 value <<= 30;
4847 break;
4848 default:
4849 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00004850 exit(1);
4851 }
aurel3200f82b82008-04-27 21:12:55 +00004852
4853 /* On 32-bit hosts, QEMU is limited by virtual address space */
4854 if (value > (2047 << 20)
4855#ifndef USE_KQEMU
4856 && HOST_LONG_BITS == 32
4857#endif
4858 ) {
4859 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4860 exit(1);
4861 }
4862 if (value != (uint64_t)(ram_addr_t)value) {
4863 fprintf(stderr, "qemu: ram size too large\n");
4864 exit(1);
4865 }
4866 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00004867 break;
aurel3200f82b82008-04-27 21:12:55 +00004868 }
bellardcd6f1162004-05-13 22:02:20 +00004869 case QEMU_OPTION_d:
4870 {
4871 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00004872 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00004873
bellardcd6f1162004-05-13 22:02:20 +00004874 mask = cpu_str_to_log_mask(optarg);
4875 if (!mask) {
4876 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00004877 for(item = cpu_log_items; item->mask != 0; item++) {
4878 printf("%-10s %s\n", item->name, item->help);
4879 }
4880 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00004881 }
4882 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00004883 }
bellardcd6f1162004-05-13 22:02:20 +00004884 break;
bellard67b915a2004-03-31 23:37:16 +00004885#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00004886 case QEMU_OPTION_s:
4887 use_gdbstub = 1;
4888 break;
4889 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00004890 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00004891 break;
bellard67b915a2004-03-31 23:37:16 +00004892#endif
bellardcd6f1162004-05-13 22:02:20 +00004893 case QEMU_OPTION_L:
4894 bios_dir = optarg;
4895 break;
j_mayer1192dad2007-10-05 13:08:35 +00004896 case QEMU_OPTION_bios:
4897 bios_name = optarg;
4898 break;
bellardcd6f1162004-05-13 22:02:20 +00004899 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00004900 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00004901 break;
bellard3d11d0e2004-12-12 16:56:30 +00004902 case QEMU_OPTION_k:
4903 keyboard_layout = optarg;
4904 break;
bellardee22c2f2004-06-03 12:49:50 +00004905 case QEMU_OPTION_localtime:
4906 rtc_utc = 0;
4907 break;
malc3893c122008-09-28 00:42:05 +00004908 case QEMU_OPTION_vga:
4909 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00004910 break;
bellarde9b137c2004-06-21 16:46:10 +00004911 case QEMU_OPTION_g:
4912 {
4913 const char *p;
4914 int w, h, depth;
4915 p = optarg;
4916 w = strtol(p, (char **)&p, 10);
4917 if (w <= 0) {
4918 graphic_error:
4919 fprintf(stderr, "qemu: invalid resolution or depth\n");
4920 exit(1);
4921 }
4922 if (*p != 'x')
4923 goto graphic_error;
4924 p++;
4925 h = strtol(p, (char **)&p, 10);
4926 if (h <= 0)
4927 goto graphic_error;
4928 if (*p == 'x') {
4929 p++;
4930 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00004931 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00004932 depth != 24 && depth != 32)
4933 goto graphic_error;
4934 } else if (*p == '\0') {
4935 depth = graphic_depth;
4936 } else {
4937 goto graphic_error;
4938 }
ths3b46e622007-09-17 08:09:54 +00004939
bellarde9b137c2004-06-21 16:46:10 +00004940 graphic_width = w;
4941 graphic_height = h;
4942 graphic_depth = depth;
4943 }
4944 break;
ths20d8a3e2007-02-18 17:04:49 +00004945 case QEMU_OPTION_echr:
4946 {
4947 char *r;
4948 term_escape_char = strtol(optarg, &r, 0);
4949 if (r == optarg)
4950 printf("Bad argument to echr\n");
4951 break;
4952 }
bellard82c643f2004-07-14 17:28:13 +00004953 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00004954 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00004955 break;
4956 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00004957 if (serial_device_index >= MAX_SERIAL_PORTS) {
4958 fprintf(stderr, "qemu: too many serial ports\n");
4959 exit(1);
4960 }
pbrookfd5f3932008-03-26 20:55:43 +00004961 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00004962 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00004963 break;
bellard6508fe52005-01-15 12:02:56 +00004964 case QEMU_OPTION_parallel:
4965 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
4966 fprintf(stderr, "qemu: too many parallel ports\n");
4967 exit(1);
4968 }
pbrookfd5f3932008-03-26 20:55:43 +00004969 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00004970 parallel_device_index++;
4971 break;
bellardd63d3072004-10-03 13:29:03 +00004972 case QEMU_OPTION_loadvm:
4973 loadvm = optarg;
4974 break;
4975 case QEMU_OPTION_full_screen:
4976 full_screen = 1;
4977 break;
ths667acca2006-12-11 02:08:05 +00004978#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00004979 case QEMU_OPTION_no_frame:
4980 no_frame = 1;
4981 break;
ths3780e192007-06-21 21:08:02 +00004982 case QEMU_OPTION_alt_grab:
4983 alt_grab = 1;
4984 break;
ths667acca2006-12-11 02:08:05 +00004985 case QEMU_OPTION_no_quit:
4986 no_quit = 1;
4987 break;
4988#endif
bellardf7cce892004-12-08 22:21:25 +00004989 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00004990 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00004991 break;
bellarda09db212005-04-30 16:10:35 +00004992#ifdef TARGET_I386
4993 case QEMU_OPTION_win2k_hack:
4994 win2k_install_hack = 1;
4995 break;
4996#endif
bellardd993e022005-02-10 22:00:06 +00004997#ifdef USE_KQEMU
4998 case QEMU_OPTION_no_kqemu:
4999 kqemu_allowed = 0;
5000 break;
bellard89bfc102006-02-08 22:46:31 +00005001 case QEMU_OPTION_kernel_kqemu:
5002 kqemu_allowed = 2;
5003 break;
bellardd993e022005-02-10 22:00:06 +00005004#endif
aliguori7ba1e612008-11-05 16:04:33 +00005005#ifdef CONFIG_KVM
5006 case QEMU_OPTION_enable_kvm:
5007 kvm_allowed = 1;
5008#ifdef USE_KQEMU
5009 kqemu_allowed = 0;
5010#endif
5011 break;
5012#endif
bellardbb36d472005-11-05 14:22:28 +00005013 case QEMU_OPTION_usb:
5014 usb_enabled = 1;
5015 break;
bellarda594cfb2005-11-06 16:13:29 +00005016 case QEMU_OPTION_usbdevice:
5017 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00005018 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00005019 fprintf(stderr, "Too many USB devices\n");
5020 exit(1);
5021 }
pbrookfd5f3932008-03-26 20:55:43 +00005022 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00005023 usb_devices_index++;
5024 break;
bellard6a00d602005-11-21 23:25:50 +00005025 case QEMU_OPTION_smp:
5026 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00005027 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005028 fprintf(stderr, "Invalid number of CPUs\n");
5029 exit(1);
5030 }
5031 break;
bellard24236862006-04-30 21:28:36 +00005032 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00005033 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005034 break;
bellard6515b202006-05-03 22:02:44 +00005035 case QEMU_OPTION_no_acpi:
5036 acpi_enabled = 0;
5037 break;
aliguori16b29ae2008-12-17 23:28:44 +00005038 case QEMU_OPTION_no_hpet:
5039 no_hpet = 1;
5040 break;
bellardd1beab82006-10-02 19:44:22 +00005041 case QEMU_OPTION_no_reboot:
5042 no_reboot = 1;
5043 break;
aurel32b2f76162008-04-11 21:35:52 +00005044 case QEMU_OPTION_no_shutdown:
5045 no_shutdown = 1;
5046 break;
balrog9467cd42007-05-01 01:34:14 +00005047 case QEMU_OPTION_show_cursor:
5048 cursor_hide = 0;
5049 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005050 case QEMU_OPTION_uuid:
5051 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5052 fprintf(stderr, "Fail to parse UUID string."
5053 " Wrong format.\n");
5054 exit(1);
5055 }
5056 break;
ths71e3ceb2006-12-22 02:11:31 +00005057 case QEMU_OPTION_daemonize:
5058 daemonize = 1;
5059 break;
ths9ae02552007-01-05 17:39:04 +00005060 case QEMU_OPTION_option_rom:
5061 if (nb_option_roms >= MAX_OPTION_ROMS) {
5062 fprintf(stderr, "Too many option ROMs\n");
5063 exit(1);
5064 }
5065 option_rom[nb_option_roms] = optarg;
5066 nb_option_roms++;
5067 break;
pbrook8e716212007-01-20 17:12:09 +00005068 case QEMU_OPTION_semihosting:
5069 semihosting_enabled = 1;
5070 break;
thsc35734b2007-03-19 15:17:08 +00005071 case QEMU_OPTION_name:
5072 qemu_name = optarg;
5073 break;
blueswir195efd112008-12-24 20:26:14 +00005074#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005075 case QEMU_OPTION_prom_env:
5076 if (nb_prom_envs >= MAX_PROM_ENVS) {
5077 fprintf(stderr, "Too many prom variables\n");
5078 exit(1);
5079 }
5080 prom_envs[nb_prom_envs] = optarg;
5081 nb_prom_envs++;
5082 break;
5083#endif
balrog2b8f2d42007-07-27 22:08:46 +00005084#ifdef TARGET_ARM
5085 case QEMU_OPTION_old_param:
5086 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005087 break;
balrog2b8f2d42007-07-27 22:08:46 +00005088#endif
thsf3dcfad2007-08-24 01:26:02 +00005089 case QEMU_OPTION_clock:
5090 configure_alarms(optarg);
5091 break;
bellard7e0af5d02007-11-07 16:24:33 +00005092 case QEMU_OPTION_startdate:
5093 {
5094 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005095 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005096 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005097 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005098 } else {
5099 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5100 &tm.tm_year,
5101 &tm.tm_mon,
5102 &tm.tm_mday,
5103 &tm.tm_hour,
5104 &tm.tm_min,
5105 &tm.tm_sec) == 6) {
5106 /* OK */
5107 } else if (sscanf(optarg, "%d-%d-%d",
5108 &tm.tm_year,
5109 &tm.tm_mon,
5110 &tm.tm_mday) == 3) {
5111 tm.tm_hour = 0;
5112 tm.tm_min = 0;
5113 tm.tm_sec = 0;
5114 } else {
5115 goto date_fail;
5116 }
5117 tm.tm_year -= 1900;
5118 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005119 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005120 if (rtc_start_date == -1) {
5121 date_fail:
5122 fprintf(stderr, "Invalid date format. Valid format are:\n"
5123 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5124 exit(1);
5125 }
balrogf6503052008-02-17 11:42:19 +00005126 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005127 }
5128 }
5129 break;
bellard26a5f132008-05-28 12:30:31 +00005130 case QEMU_OPTION_tb_size:
5131 tb_size = strtol(optarg, NULL, 0);
5132 if (tb_size < 0)
5133 tb_size = 0;
5134 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005135 case QEMU_OPTION_icount:
5136 use_icount = 1;
5137 if (strcmp(optarg, "auto") == 0) {
5138 icount_time_shift = -1;
5139 } else {
5140 icount_time_shift = strtol(optarg, NULL, 0);
5141 }
5142 break;
aliguori5bb79102008-10-13 03:12:02 +00005143 case QEMU_OPTION_incoming:
5144 incoming = optarg;
5145 break;
bellardcd6f1162004-05-13 22:02:20 +00005146 }
bellard0824d6f2003-06-24 13:42:40 +00005147 }
5148 }
bellard330d0412003-07-26 18:11:40 +00005149
aliguori7ba1e612008-11-05 16:04:33 +00005150#if defined(CONFIG_KVM) && defined(USE_KQEMU)
5151 if (kvm_allowed && kqemu_allowed) {
5152 fprintf(stderr,
5153 "You can not enable both KVM and kqemu at the same time\n");
5154 exit(1);
5155 }
5156#endif
5157
balrog3d878ca2008-10-28 10:59:59 +00005158 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005159 if (smp_cpus > machine->max_cpus) {
5160 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5161 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5162 machine->max_cpus);
5163 exit(1);
5164 }
5165
aliguoribc0129d2008-08-01 15:12:34 +00005166 if (nographic) {
5167 if (serial_device_index == 0)
5168 serial_devices[0] = "stdio";
5169 if (parallel_device_index == 0)
5170 parallel_devices[0] = "null";
5171 if (strncmp(monitor_device, "vc", 2) == 0)
5172 monitor_device = "stdio";
5173 }
5174
ths71e3ceb2006-12-22 02:11:31 +00005175#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005176 if (daemonize) {
5177 pid_t pid;
5178
5179 if (pipe(fds) == -1)
5180 exit(1);
5181
5182 pid = fork();
5183 if (pid > 0) {
5184 uint8_t status;
5185 ssize_t len;
5186
5187 close(fds[1]);
5188
5189 again:
ths93815bc2007-03-19 15:58:31 +00005190 len = read(fds[0], &status, 1);
5191 if (len == -1 && (errno == EINTR))
5192 goto again;
5193
5194 if (len != 1)
5195 exit(1);
5196 else if (status == 1) {
5197 fprintf(stderr, "Could not acquire pidfile\n");
5198 exit(1);
5199 } else
5200 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005201 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005202 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005203
5204 setsid();
5205
5206 pid = fork();
5207 if (pid > 0)
5208 exit(0);
5209 else if (pid < 0)
5210 exit(1);
5211
5212 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005213
5214 signal(SIGTSTP, SIG_IGN);
5215 signal(SIGTTOU, SIG_IGN);
5216 signal(SIGTTIN, SIG_IGN);
5217 }
5218#endif
5219
thsaa26bb22007-03-25 21:33:06 +00005220 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005221 if (daemonize) {
5222 uint8_t status = 1;
5223 write(fds[1], &status, 1);
5224 } else
5225 fprintf(stderr, "Could not acquire pid file\n");
5226 exit(1);
5227 }
5228
bellardff3fbb32006-01-08 10:53:14 +00005229#ifdef USE_KQEMU
5230 if (smp_cpus > 1)
5231 kqemu_allowed = 0;
5232#endif
bellarda20dd502003-09-30 21:07:02 +00005233 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00005234 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00005235
j_mayer28c5af52007-11-11 01:50:45 +00005236 if (!linux_boot && net_boot == 0 &&
blueswir1f88e4b92008-08-12 15:58:35 +00005237 !machine->nodisk_ok && nb_drives_opt == 0)
ths15f82202007-06-29 23:26:08 +00005238 help(1);
bellard0824d6f2003-06-24 13:42:40 +00005239
thsf8d39c02008-07-03 10:01:15 +00005240 if (!linux_boot && *kernel_cmdline != '\0') {
5241 fprintf(stderr, "-append only allowed with -kernel option\n");
5242 exit(1);
5243 }
5244
5245 if (!linux_boot && initrd_filename != NULL) {
5246 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5247 exit(1);
5248 }
5249
ths96d30e42007-01-07 20:42:14 +00005250 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00005251 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00005252 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00005253 }
bellardb118d612003-06-30 23:36:21 +00005254 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005255
pbrook634fce92006-07-15 17:40:09 +00005256 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005257 if (init_timer_alarm() < 0) {
5258 fprintf(stderr, "could not initialize alarm timer\n");
5259 exit(1);
5260 }
pbrook2e70f6e2008-06-29 01:03:05 +00005261 if (use_icount && icount_time_shift < 0) {
5262 use_icount = 2;
5263 /* 125MIPS seems a reasonable initial guess at the guest speed.
5264 It will be corrected fairly quickly anyway. */
5265 icount_time_shift = 3;
5266 init_icount_adjust();
5267 }
pbrook634fce92006-07-15 17:40:09 +00005268
bellardfd1dff42006-02-01 21:29:26 +00005269#ifdef _WIN32
5270 socket_init();
5271#endif
5272
bellard7c9d8e02005-11-15 22:16:05 +00005273 /* init network clients */
5274 if (nb_net_clients == 0) {
5275 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005276 net_clients[nb_net_clients++] = "nic";
5277#ifdef CONFIG_SLIRP
5278 net_clients[nb_net_clients++] = "user";
5279#endif
bellardc20709a2004-04-21 23:27:19 +00005280 }
5281
bellard7c9d8e02005-11-15 22:16:05 +00005282 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005283 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005284 exit(1);
bellard702c6512004-04-02 21:21:32 +00005285 }
aliguori63a01ef2008-10-31 19:10:00 +00005286 net_client_check();
bellardf1510b22003-06-25 00:07:40 +00005287
thseec85c22007-01-05 17:41:07 +00005288#ifdef TARGET_I386
balroged494d82007-12-11 23:23:52 +00005289 /* XXX: this should be moved in the PC machine instantiation code */
j_mayer28c5af52007-11-11 01:50:45 +00005290 if (net_boot != 0) {
5291 int netroms = 0;
5292 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00005293 const char *model = nd_table[i].model;
5294 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00005295 if (net_boot & (1 << i)) {
5296 if (model == NULL)
5297 model = "ne2k_pci";
5298 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5299 if (get_image_size(buf) > 0) {
5300 if (nb_option_roms >= MAX_OPTION_ROMS) {
5301 fprintf(stderr, "Too many option ROMs\n");
5302 exit(1);
5303 }
5304 option_rom[nb_option_roms] = strdup(buf);
5305 nb_option_roms++;
5306 netroms++;
5307 }
5308 }
thseec85c22007-01-05 17:41:07 +00005309 }
j_mayer28c5af52007-11-11 01:50:45 +00005310 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00005311 fprintf(stderr, "No valid PXE rom found for network device\n");
5312 exit(1);
5313 }
thseec85c22007-01-05 17:41:07 +00005314 }
5315#endif
5316
balrogdc72ac12008-11-09 00:04:26 +00005317 /* init the bluetooth world */
5318 for (i = 0; i < nb_bt_opts; i++)
5319 if (bt_parse(bt_opts[i]))
5320 exit(1);
5321
bellard0824d6f2003-06-24 13:42:40 +00005322 /* init the memory */
balrog7fb4fdc2008-04-24 17:59:27 +00005323 phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
5324
5325 if (machine->ram_require & RAMSIZE_FIXED) {
5326 if (ram_size > 0) {
5327 if (ram_size < phys_ram_size) {
aurel32cd940062008-04-28 20:26:54 +00005328 fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
5329 machine->name, (unsigned long long) phys_ram_size);
balrog7fb4fdc2008-04-24 17:59:27 +00005330 exit(-1);
5331 }
5332
5333 phys_ram_size = ram_size;
5334 } else
5335 ram_size = phys_ram_size;
5336 } else {
aurel324fc5d072008-04-27 21:39:40 +00005337 if (ram_size == 0)
balrog7fb4fdc2008-04-24 17:59:27 +00005338 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5339
5340 phys_ram_size += ram_size;
5341 }
ths9ae02552007-01-05 17:39:04 +00005342
bellardd993e022005-02-10 22:00:06 +00005343 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00005344 if (!phys_ram_base) {
5345 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00005346 exit(1);
5347 }
5348
bellard26a5f132008-05-28 12:30:31 +00005349 /* init the dynamic translator */
5350 cpu_exec_init_all(tb_size * 1024 * 1024);
5351
bellard5905b2e2004-08-01 21:53:26 +00005352 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005353
5354 /* we always create the cdrom drive, even if no disk is there */
5355
5356 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005357 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005358
balrog9d413d12007-12-04 00:10:34 +00005359 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005360
5361 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005362 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005363
balrog9d413d12007-12-04 00:10:34 +00005364 /* we always create one sd slot, even if no card is in it */
5365
5366 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005367 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005368
ths96d30e42007-01-07 20:42:14 +00005369 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005370
thse4bcb142007-12-02 04:51:10 +00005371 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005372 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005373 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005374
bellardc88676f2006-08-06 13:36:11 +00005375 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005376 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005377
bellard313aa562003-08-10 21:52:11 +00005378 /* terminal init */
ths740733b2007-06-08 01:57:56 +00005379 memset(&display_state, 0, sizeof(display_state));
bellarda20dd502003-09-30 21:07:02 +00005380 if (nographic) {
balrog4d3b6f62008-02-10 16:33:14 +00005381 if (curses) {
5382 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5383 exit(1);
5384 }
ths2ff89792007-06-21 23:34:19 +00005385 /* nearly nothing to do */
5386 dumb_display_init(ds);
ths73fc9742006-12-22 02:09:07 +00005387 } else if (vnc_display != NULL) {
ths71cab5c2007-08-25 01:35:38 +00005388 vnc_display_init(ds);
5389 if (vnc_display_open(ds, vnc_display) < 0)
5390 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00005391 } else
5392#if defined(CONFIG_CURSES)
5393 if (curses) {
5394 curses_display_init(ds, full_screen);
5395 } else
5396#endif
5397 {
bellard5b0753e2005-03-01 21:37:28 +00005398#if defined(CONFIG_SDL)
ths43523e92007-02-18 18:19:32 +00005399 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00005400#elif defined(CONFIG_COCOA)
5401 cocoa_display_init(ds, full_screen);
pbrook67276f52007-11-15 19:04:08 +00005402#else
5403 dumb_display_init(ds);
bellard313aa562003-08-10 21:52:11 +00005404#endif
5405 }
bellard0824d6f2003-06-24 13:42:40 +00005406
aliguori5b08fc12008-08-21 20:08:03 +00005407#ifndef _WIN32
5408 /* must be after terminal init, SDL library changes signal handlers */
5409 termsig_setup();
5410#endif
5411
ths20d8a3e2007-02-18 17:04:49 +00005412 /* Maintain compatibility with multiple stdio monitors */
5413 if (!strcmp(monitor_device,"stdio")) {
5414 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
pbrookfd5f3932008-03-26 20:55:43 +00005415 const char *devname = serial_devices[i];
5416 if (devname && !strcmp(devname,"mon:stdio")) {
5417 monitor_device = NULL;
ths20d8a3e2007-02-18 17:04:49 +00005418 break;
pbrookfd5f3932008-03-26 20:55:43 +00005419 } else if (devname && !strcmp(devname,"stdio")) {
5420 monitor_device = NULL;
5421 serial_devices[i] = "mon:stdio";
ths20d8a3e2007-02-18 17:04:49 +00005422 break;
5423 }
5424 }
bellard82c643f2004-07-14 17:28:13 +00005425 }
pbrookfd5f3932008-03-26 20:55:43 +00005426 if (monitor_device) {
aliguori5ccfae12008-10-31 17:31:29 +00005427 monitor_hd = qemu_chr_open("monitor", monitor_device);
ths20d8a3e2007-02-18 17:04:49 +00005428 if (!monitor_hd) {
5429 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5430 exit(1);
5431 }
5432 monitor_init(monitor_hd, !nographic);
5433 }
bellard82c643f2004-07-14 17:28:13 +00005434
bellard8d11df92004-08-24 21:13:40 +00005435 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005436 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005437 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005438 char label[32];
5439 snprintf(label, sizeof(label), "serial%d", i);
5440 serial_hds[i] = qemu_chr_open(label, devname);
bellard8d11df92004-08-24 21:13:40 +00005441 if (!serial_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00005442 fprintf(stderr, "qemu: could not open serial device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00005443 devname);
bellard8d11df92004-08-24 21:13:40 +00005444 exit(1);
5445 }
thsaf3a9032007-07-11 23:14:59 +00005446 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005447 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005448 }
bellard82c643f2004-07-14 17:28:13 +00005449 }
bellard82c643f2004-07-14 17:28:13 +00005450
bellard6508fe52005-01-15 12:02:56 +00005451 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005452 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005453 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005454 char label[32];
5455 snprintf(label, sizeof(label), "parallel%d", i);
5456 parallel_hds[i] = qemu_chr_open(label, devname);
bellard6508fe52005-01-15 12:02:56 +00005457 if (!parallel_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00005458 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00005459 devname);
bellard6508fe52005-01-15 12:02:56 +00005460 exit(1);
5461 }
thsaf3a9032007-07-11 23:14:59 +00005462 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005463 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005464 }
5465 }
5466
aliguori7ba1e612008-11-05 16:04:33 +00005467 if (kvm_enabled()) {
5468 int ret;
5469
5470 ret = kvm_init(smp_cpus);
5471 if (ret < 0) {
5472 fprintf(stderr, "failed to initialize KVM\n");
5473 exit(1);
5474 }
5475 }
5476
blueswir1b881c2c2007-11-18 08:46:58 +00005477 machine->init(ram_size, vga_ram_size, boot_devices, ds,
j_mayer94fc95c2007-03-05 19:44:02 +00005478 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
bellard73332e52004-04-04 20:22:28 +00005479
aliguorif5d6f512008-12-15 22:20:42 +00005480 /* Set KVM's vcpu state to qemu's initial CPUState. */
5481 if (kvm_enabled()) {
5482 int ret;
5483
5484 ret = kvm_sync_vcpus();
5485 if (ret < 0) {
5486 fprintf(stderr, "failed to initialize vcpus\n");
5487 exit(1);
5488 }
5489 }
5490
pbrook0d92ed32006-05-21 16:30:15 +00005491 /* init USB devices */
5492 if (usb_enabled) {
5493 for(i = 0; i < usb_devices_index; i++) {
5494 if (usb_device_add(usb_devices[i]) < 0) {
5495 fprintf(stderr, "Warning: could not add USB device %s\n",
5496 usb_devices[i]);
5497 }
5498 }
5499 }
5500
ths740733b2007-06-08 01:57:56 +00005501 if (display_state.dpy_refresh) {
5502 display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
5503 qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
5504 }
bellard7f7f9872003-10-30 01:11:23 +00005505
bellard67b915a2004-03-31 23:37:16 +00005506#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00005507 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00005508 /* XXX: use standard host:port notation and modify options
5509 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00005510 if (gdbserver_start(gdbstub_port) < 0) {
5511 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00005512 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00005513 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00005514 }
balrog45669e02007-07-02 13:20:17 +00005515 }
bellard67b915a2004-03-31 23:37:16 +00005516#endif
balrog45669e02007-07-02 13:20:17 +00005517
bellardd63d3072004-10-03 13:29:03 +00005518 if (loadvm)
bellardfaea38e2006-08-05 21:31:00 +00005519 do_loadvm(loadvm);
bellardd63d3072004-10-03 13:29:03 +00005520
aliguori5bb79102008-10-13 03:12:02 +00005521 if (incoming) {
5522 autostart = 0; /* fixme how to deal with -daemonize */
5523 qemu_start_incoming_migration(incoming);
5524 }
5525
bellard67b915a2004-03-31 23:37:16 +00005526 {
bellard5905b2e2004-08-01 21:53:26 +00005527 /* XXX: simplify init */
aliguori83ab7952008-08-19 14:44:22 +00005528 read_passwords();
pbrook3c07f8e2007-01-21 16:47:01 +00005529 if (autostart) {
bellard5905b2e2004-08-01 21:53:26 +00005530 vm_start();
5531 }
bellard0824d6f2003-06-24 13:42:40 +00005532 }
thsffd843b2006-12-21 19:46:43 +00005533
ths71e3ceb2006-12-22 02:11:31 +00005534 if (daemonize) {
5535 uint8_t status = 0;
5536 ssize_t len;
5537 int fd;
5538
5539 again1:
5540 len = write(fds[1], &status, 1);
5541 if (len == -1 && (errno == EINTR))
5542 goto again1;
5543
5544 if (len != 1)
5545 exit(1);
5546
aliguoribd54b862008-07-23 00:58:33 +00005547 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005548 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005549 if (fd == -1)
5550 exit(1);
5551
5552 dup2(fd, 0);
5553 dup2(fd, 1);
5554 dup2(fd, 2);
5555
5556 close(fd);
5557 }
5558
bellard8a7ddc32004-03-31 19:00:16 +00005559 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005560 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005561 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005562
bellard0824d6f2003-06-24 13:42:40 +00005563 return 0;
5564}