blob: aaeff23faff9da8982d530207249e6d7d09561b6 [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
aliguori08585322009-02-27 22:09:45 +000055#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000056#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000057#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000058#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000059#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000060#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000061#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000062#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000063#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000064#include <net/if.h>
65#if defined(__NetBSD__)
66#include <net/if_tap.h>
67#endif
68#ifdef __linux__
69#include <linux/if_tun.h>
70#endif
71#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000072#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000073#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000074#include <sys/select.h>
bellard7d3505c2004-05-12 19:32:15 +000075#ifdef _BSD
76#include <sys/stat.h>
blueswir124646c72008-11-07 16:55:48 +000077#ifdef __FreeBSD__
bellard7d3505c2004-05-12 19:32:15 +000078#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000079#else
80#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000081#endif
ths5c40d2b2007-06-23 16:03:36 +000082#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
83#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000084#else
blueswir1223f0d72008-09-30 18:12:18 +000085#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000086#include <pty.h>
87#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000088#include <linux/rtc.h>
thsbd494f42007-09-16 20:03:23 +000089
90/* For the benefit of older linux systems which don't supply it,
91 we use a local copy of hpet.h. */
92/* #include <linux/hpet.h> */
93#include "hpet.h"
94
bellarde57a8c02005-11-10 23:58:52 +000095#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000096#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000097#endif
98#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000099#include <sys/stat.h>
100#include <sys/ethernet.h>
101#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +0000102#include <netinet/arp.h>
103#include <netinet/in.h>
104#include <netinet/in_systm.h>
105#include <netinet/ip.h>
106#include <netinet/ip_icmp.h> // must come after ip.h
107#include <netinet/udp.h>
108#include <netinet/tcp.h>
109#include <net/if.h>
110#include <syslog.h>
111#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +0000112#endif
bellard7d3505c2004-05-12 19:32:15 +0000113#endif
bellardec530c82006-04-25 22:36:06 +0000114#endif
bellard67b915a2004-03-31 23:37:16 +0000115
aliguori03ff3ca2008-09-15 15:51:35 +0000116#include "qemu_socket.h"
117
bellardc20709a2004-04-21 23:27:19 +0000118#if defined(CONFIG_SLIRP)
119#include "libslirp.h"
120#endif
121
blueswir19892fbf2008-08-24 10:34:20 +0000122#if defined(__OpenBSD__)
123#include <util.h>
124#endif
125
ths8a16d272008-07-19 09:56:24 +0000126#if defined(CONFIG_VDE)
127#include <libvdeplug.h>
128#endif
129
bellard67b915a2004-03-31 23:37:16 +0000130#ifdef _WIN32
bellard7d3505c2004-05-12 19:32:15 +0000131#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000132#include <sys/timeb.h>
ths4fddf622007-12-17 04:42:29 +0000133#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000134#define getopt_long_only getopt_long
135#define memalign(align, size) malloc(size)
136#endif
137
bellard73332e52004-04-04 20:22:28 +0000138#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +0000139#ifdef __APPLE__
bellard83fb7ad2004-07-05 21:25:26 +0000140#include <SDL/SDL.h>
malc880fec52009-02-15 20:18:41 +0000141int qemu_main(int argc, char **argv, char **envp);
142int main(int argc, char **argv)
143{
144 qemu_main(argc, argv, NULL);
145}
146#undef main
147#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000148#endif
bellard73332e52004-04-04 20:22:28 +0000149#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000150
bellard5b0753e2005-03-01 21:37:28 +0000151#ifdef CONFIG_COCOA
152#undef main
153#define main qemu_main
154#endif /* CONFIG_COCOA */
155
bellard0824d6f2003-06-24 13:42:40 +0000156#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000157
bellard8a7ddc32004-03-31 19:00:16 +0000158#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000159
bellard0824d6f2003-06-24 13:42:40 +0000160//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000161//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000162//#define DEBUG_NET
163//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000164
aliguorid12d51d2009-01-15 21:48:06 +0000165
166#ifdef DEBUG_IOPORT
aliguori93fcfe32009-01-15 22:34:14 +0000167# define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
aliguorid12d51d2009-01-15 21:48:06 +0000168#else
169# define LOG_IOPORT(...) do { } while (0)
170#endif
171
bellard1bfe8562004-07-08 21:17:50 +0000172#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000173
pbrook0d92ed32006-05-21 16:30:15 +0000174/* Max number of USB devices that can be specified on the commandline. */
175#define MAX_USB_CMDLINE 8
176
balrogdc72ac12008-11-09 00:04:26 +0000177/* Max number of bluetooth switches on the commandline. */
178#define MAX_BT_CMDLINE 10
179
bellard7dea1da2003-11-16 15:59:30 +0000180/* XXX: use a two level table to limit memory usage */
181#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000182
bellard80cabfa2004-03-14 12:20:30 +0000183const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000184const char *bios_name = NULL;
blueswir1dbed7e42008-10-01 19:38:09 +0000185static void *ioport_opaque[MAX_IOPORTS];
186static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
187static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
thse4bcb142007-12-02 04:51:10 +0000188/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000189 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000190DriveInfo drives_table[MAX_DRIVES+1];
191int nb_drives;
blueswir1dbed7e42008-10-01 19:38:09 +0000192static int vga_ram_size;
malccb5a7aa2008-09-28 00:42:12 +0000193enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000194static DisplayState *display_state;
bellarda20dd502003-09-30 21:07:02 +0000195int nographic;
blueswir1dbed7e42008-10-01 19:38:09 +0000196static int curses;
aliguori7d957bd2009-01-15 22:14:11 +0000197static int sdl;
bellard3d11d0e2004-12-12 16:56:30 +0000198const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000199int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000200ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000201int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000202NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000203int vm_running;
aliguoric0f4ce72009-03-05 23:01:01 +0000204static int autostart;
balrogf6503052008-02-17 11:42:19 +0000205static int rtc_utc = 1;
206static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000207int cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +0000208int std_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000209int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000210#ifdef TARGET_SPARC
211int graphic_width = 1024;
212int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000213int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000214#else
bellard1bfe8562004-07-08 21:17:50 +0000215int graphic_width = 800;
216int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000217int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000218#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000219static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000220#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000221static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000222#endif
ths667acca2006-12-11 02:08:05 +0000223int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000224CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000225CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000226CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000227#ifdef TARGET_I386
228int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000229int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000230#endif
bellardbb36d472005-11-05 14:22:28 +0000231int usb_enabled = 0;
bellard6a00d602005-11-21 23:25:50 +0000232int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000233const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000234int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000235int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000236int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000237int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000238int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000239int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000240int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000241int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000242const char *option_rom[MAX_OPTION_ROMS];
243int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000244int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000245#ifdef TARGET_ARM
246int old_param = 0;
247#endif
thsc35734b2007-03-19 15:17:08 +0000248const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000249int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000250#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000251unsigned int nb_prom_envs = 0;
252const char *prom_envs[MAX_PROM_ENVS];
253#endif
aliguoriec691c82009-02-11 15:20:37 +0000254int nb_drives_opt;
255struct drive_opt drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000256
balrogee5605e2007-12-03 03:01:40 +0000257static CPUState *cur_cpu;
258static CPUState *next_cpu;
balrog76ea08f2007-12-16 11:48:54 +0000259static int event_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000260/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000261static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000262/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000263#define MAX_ICOUNT_SHIFT 10
264/* Compensate for varying guest execution speed. */
265static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000266static QEMUTimer *icount_rt_timer;
267static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000268static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000269
blueswir18fcb1b92008-09-18 18:29:08 +0000270uint8_t qemu_uuid[16];
271
bellard0824d6f2003-06-24 13:42:40 +0000272/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000273/* x86 ISA bus support */
274
275target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000276PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000277
aliguori477e3ed2008-07-23 15:19:59 +0000278static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
279static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
280
281static uint32_t ioport_read(int index, uint32_t address)
282{
283 static IOPortReadFunc *default_func[3] = {
284 default_ioport_readb,
285 default_ioport_readw,
286 default_ioport_readl
287 };
288 IOPortReadFunc *func = ioport_read_table[index][address];
289 if (!func)
290 func = default_func[index];
291 return func(ioport_opaque[address], address);
292}
293
294static void ioport_write(int index, uint32_t address, uint32_t data)
295{
296 static IOPortWriteFunc *default_func[3] = {
297 default_ioport_writeb,
298 default_ioport_writew,
299 default_ioport_writel
300 };
301 IOPortWriteFunc *func = ioport_write_table[index][address];
302 if (!func)
303 func = default_func[index];
304 func(ioport_opaque[address], address, data);
305}
306
pbrook9596ebb2007-11-18 01:44:38 +0000307static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000308{
309#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000310 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000311#endif
bellardfc01f7e2003-06-30 10:03:06 +0000312 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000313}
314
pbrook9596ebb2007-11-18 01:44:38 +0000315static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000316{
317#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000318 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000319#endif
320}
321
322/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000323static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000324{
325 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000326 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000327 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000328 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000329 return data;
330}
331
pbrook9596ebb2007-11-18 01:44:38 +0000332static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000333{
aliguori477e3ed2008-07-23 15:19:59 +0000334 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000335 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000336 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000337}
338
pbrook9596ebb2007-11-18 01:44:38 +0000339static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000340{
341#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000342 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000343#endif
344 return 0xffffffff;
345}
346
pbrook9596ebb2007-11-18 01:44:38 +0000347static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000348{
349#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000350 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000351#endif
bellard0824d6f2003-06-24 13:42:40 +0000352}
353
bellardfc01f7e2003-06-30 10:03:06 +0000354/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000355int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000356 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000357{
bellardfc01f7e2003-06-30 10:03:06 +0000358 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000359
bellardc4b1fcc2004-03-14 21:44:30 +0000360 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000361 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000362 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000363 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000364 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000365 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000366 } else {
balrog88fdf562008-04-26 21:11:22 +0000367 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000368 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000369 }
370 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000371 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000372 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000373 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000374 ioport_opaque[i] = opaque;
375 }
bellard0824d6f2003-06-24 13:42:40 +0000376 return 0;
377}
378
bellardfc01f7e2003-06-30 10:03:06 +0000379/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000380int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000381 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000382{
bellardfc01f7e2003-06-30 10:03:06 +0000383 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000384
bellardc4b1fcc2004-03-14 21:44:30 +0000385 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000386 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000387 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000388 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000389 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000390 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000391 } else {
balrog88fdf562008-04-26 21:11:22 +0000392 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000393 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000394 }
395 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000396 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000397 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
398 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000399 ioport_opaque[i] = opaque;
400 }
bellardf1510b22003-06-25 00:07:40 +0000401 return 0;
402}
403
bellard69b91032004-05-18 23:05:28 +0000404void isa_unassign_ioport(int start, int length)
405{
406 int i;
407
408 for(i = start; i < start + length; i++) {
409 ioport_read_table[0][i] = default_ioport_readb;
410 ioport_read_table[1][i] = default_ioport_readw;
411 ioport_read_table[2][i] = default_ioport_readl;
412
413 ioport_write_table[0][i] = default_ioport_writeb;
414 ioport_write_table[1][i] = default_ioport_writew;
415 ioport_write_table[2][i] = default_ioport_writel;
aliguoria7607f72009-02-11 15:21:29 +0000416
417 ioport_opaque[i] = NULL;
bellard69b91032004-05-18 23:05:28 +0000418 }
419}
420
bellard20f32282005-01-03 23:36:21 +0000421/***********************************************************/
422
bellardc45886d2004-01-05 00:02:06 +0000423void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000424{
aliguorid12d51d2009-01-15 21:48:06 +0000425 LOG_IOPORT("outb: %04x %02x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000426 ioport_write(0, 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_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000434{
aliguorid12d51d2009-01-15 21:48:06 +0000435 LOG_IOPORT("outw: %04x %04x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000436 ioport_write(1, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000437#ifdef USE_KQEMU
438 if (env)
439 env->last_io_time = cpu_get_time_fast();
440#endif
bellard0824d6f2003-06-24 13:42:40 +0000441}
442
bellardc45886d2004-01-05 00:02:06 +0000443void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000444{
aliguorid12d51d2009-01-15 21:48:06 +0000445 LOG_IOPORT("outl: %04x %08x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000446 ioport_write(2, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000447#ifdef USE_KQEMU
448 if (env)
449 env->last_io_time = cpu_get_time_fast();
450#endif
bellard0824d6f2003-06-24 13:42:40 +0000451}
452
bellardc45886d2004-01-05 00:02:06 +0000453int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000454{
bellardfd872592004-05-12 19:11:15 +0000455 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000456 val = ioport_read(0, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000457 LOG_IOPORT("inb : %04x %02x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000458#ifdef USE_KQEMU
459 if (env)
460 env->last_io_time = cpu_get_time_fast();
461#endif
bellardfd872592004-05-12 19:11:15 +0000462 return val;
bellard0824d6f2003-06-24 13:42:40 +0000463}
464
bellardc45886d2004-01-05 00:02:06 +0000465int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000466{
bellardfd872592004-05-12 19:11:15 +0000467 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000468 val = ioport_read(1, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000469 LOG_IOPORT("inw : %04x %04x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000470#ifdef USE_KQEMU
471 if (env)
472 env->last_io_time = cpu_get_time_fast();
473#endif
bellardfd872592004-05-12 19:11:15 +0000474 return val;
bellard0824d6f2003-06-24 13:42:40 +0000475}
476
bellardc45886d2004-01-05 00:02:06 +0000477int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000478{
bellardfd872592004-05-12 19:11:15 +0000479 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000480 val = ioport_read(2, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000481 LOG_IOPORT("inl : %04x %08x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000482#ifdef USE_KQEMU
483 if (env)
484 env->last_io_time = cpu_get_time_fast();
485#endif
bellardfd872592004-05-12 19:11:15 +0000486 return val;
bellard0824d6f2003-06-24 13:42:40 +0000487}
488
489/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000490void hw_error(const char *fmt, ...)
491{
492 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000493 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000494
495 va_start(ap, fmt);
496 fprintf(stderr, "qemu: hardware error: ");
497 vfprintf(stderr, fmt, ap);
498 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000499 for(env = first_cpu; env != NULL; env = env->next_cpu) {
500 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000501#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000502 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000503#else
bellard6a00d602005-11-21 23:25:50 +0000504 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000505#endif
bellard6a00d602005-11-21 23:25:50 +0000506 }
bellard0824d6f2003-06-24 13:42:40 +0000507 va_end(ap);
508 abort();
509}
aliguoridf751fa2008-12-04 20:19:35 +0000510
511/***************/
512/* ballooning */
513
514static QEMUBalloonEvent *qemu_balloon_event;
515void *qemu_balloon_event_opaque;
516
517void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
518{
519 qemu_balloon_event = func;
520 qemu_balloon_event_opaque = opaque;
521}
522
523void qemu_balloon(ram_addr_t target)
524{
525 if (qemu_balloon_event)
526 qemu_balloon_event(qemu_balloon_event_opaque, target);
527}
528
529ram_addr_t qemu_balloon_status(void)
530{
531 if (qemu_balloon_event)
532 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
533 return 0;
534}
bellard0824d6f2003-06-24 13:42:40 +0000535
bellard8a7ddc32004-03-31 19:00:16 +0000536/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000537/* keyboard/mouse */
538
539static QEMUPutKBDEvent *qemu_put_kbd_event;
540static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000541static QEMUPutMouseEntry *qemu_put_mouse_event_head;
542static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000543
544void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
545{
546 qemu_put_kbd_event_opaque = opaque;
547 qemu_put_kbd_event = func;
548}
549
ths455204e2007-01-05 16:42:13 +0000550QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
551 void *opaque, int absolute,
552 const char *name)
bellard63066f42004-06-03 18:45:02 +0000553{
ths455204e2007-01-05 16:42:13 +0000554 QEMUPutMouseEntry *s, *cursor;
555
556 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000557
558 s->qemu_put_mouse_event = func;
559 s->qemu_put_mouse_event_opaque = opaque;
560 s->qemu_put_mouse_event_absolute = absolute;
561 s->qemu_put_mouse_event_name = qemu_strdup(name);
562 s->next = NULL;
563
564 if (!qemu_put_mouse_event_head) {
565 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
566 return s;
567 }
568
569 cursor = qemu_put_mouse_event_head;
570 while (cursor->next != NULL)
571 cursor = cursor->next;
572
573 cursor->next = s;
574 qemu_put_mouse_event_current = s;
575
576 return s;
577}
578
579void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
580{
581 QEMUPutMouseEntry *prev = NULL, *cursor;
582
583 if (!qemu_put_mouse_event_head || entry == NULL)
584 return;
585
586 cursor = qemu_put_mouse_event_head;
587 while (cursor != NULL && cursor != entry) {
588 prev = cursor;
589 cursor = cursor->next;
590 }
591
592 if (cursor == NULL) // does not exist or list empty
593 return;
594 else if (prev == NULL) { // entry is head
595 qemu_put_mouse_event_head = cursor->next;
596 if (qemu_put_mouse_event_current == entry)
597 qemu_put_mouse_event_current = cursor->next;
598 qemu_free(entry->qemu_put_mouse_event_name);
599 qemu_free(entry);
600 return;
601 }
602
603 prev->next = entry->next;
604
605 if (qemu_put_mouse_event_current == entry)
606 qemu_put_mouse_event_current = prev;
607
608 qemu_free(entry->qemu_put_mouse_event_name);
609 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000610}
611
612void kbd_put_keycode(int keycode)
613{
614 if (qemu_put_kbd_event) {
615 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
616 }
617}
618
619void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
620{
ths455204e2007-01-05 16:42:13 +0000621 QEMUPutMouseEvent *mouse_event;
622 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000623 int width;
ths455204e2007-01-05 16:42:13 +0000624
625 if (!qemu_put_mouse_event_current) {
626 return;
627 }
628
629 mouse_event =
630 qemu_put_mouse_event_current->qemu_put_mouse_event;
631 mouse_event_opaque =
632 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
633
634 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000635 if (graphic_rotate) {
636 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
637 width = 0x7fff;
638 else
aurel32b94ed572008-03-10 19:34:27 +0000639 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000640 mouse_event(mouse_event_opaque,
641 width - dy, dx, dz, buttons_state);
642 } else
643 mouse_event(mouse_event_opaque,
644 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000645 }
646}
647
bellard09b26c52006-04-12 21:09:08 +0000648int kbd_mouse_is_absolute(void)
649{
ths455204e2007-01-05 16:42:13 +0000650 if (!qemu_put_mouse_event_current)
651 return 0;
652
653 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
654}
655
656void do_info_mice(void)
657{
658 QEMUPutMouseEntry *cursor;
659 int index = 0;
660
661 if (!qemu_put_mouse_event_head) {
662 term_printf("No mouse devices connected\n");
663 return;
664 }
665
666 term_printf("Mouse devices available:\n");
667 cursor = qemu_put_mouse_event_head;
668 while (cursor != NULL) {
669 term_printf("%c Mouse #%d: %s\n",
670 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
671 index, cursor->qemu_put_mouse_event_name);
672 index++;
673 cursor = cursor->next;
674 }
675}
676
677void do_mouse_set(int index)
678{
679 QEMUPutMouseEntry *cursor;
680 int i = 0;
681
682 if (!qemu_put_mouse_event_head) {
683 term_printf("No mouse devices connected\n");
684 return;
685 }
686
687 cursor = qemu_put_mouse_event_head;
688 while (cursor != NULL && index != i) {
689 i++;
690 cursor = cursor->next;
691 }
692
693 if (cursor != NULL)
694 qemu_put_mouse_event_current = cursor;
695 else
696 term_printf("Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000697}
698
bellard87858c82003-06-27 12:01:39 +0000699/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000700uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000701{
702 union {
703 uint64_t ll;
704 struct {
705#ifdef WORDS_BIGENDIAN
706 uint32_t high, low;
707#else
708 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000709#endif
bellard87858c82003-06-27 12:01:39 +0000710 } l;
711 } u, res;
712 uint64_t rl, rh;
713
714 u.ll = a;
715 rl = (uint64_t)u.l.low * (uint64_t)b;
716 rh = (uint64_t)u.l.high * (uint64_t)b;
717 rh += (rl >> 32);
718 res.l.high = rh / c;
719 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
720 return res.ll;
721}
722
bellard1dce7c32006-07-13 23:20:22 +0000723/***********************************************************/
724/* real time host monotonic timer */
725
726#define QEMU_TIMER_BASE 1000000000LL
727
728#ifdef WIN32
729
730static int64_t clock_freq;
731
732static void init_get_clock(void)
733{
bellarda8e5ac32006-07-14 09:36:13 +0000734 LARGE_INTEGER freq;
735 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000736 ret = QueryPerformanceFrequency(&freq);
737 if (ret == 0) {
738 fprintf(stderr, "Could not calibrate ticks\n");
739 exit(1);
740 }
741 clock_freq = freq.QuadPart;
742}
743
744static int64_t get_clock(void)
745{
746 LARGE_INTEGER ti;
747 QueryPerformanceCounter(&ti);
748 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
749}
750
751#else
752
753static int use_rt_clock;
754
755static void init_get_clock(void)
756{
757 use_rt_clock = 0;
aurel3260759372008-10-13 21:08:25 +0000758#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000759 {
760 struct timespec ts;
761 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
762 use_rt_clock = 1;
763 }
764 }
765#endif
766}
767
768static int64_t get_clock(void)
769{
aurel3260759372008-10-13 21:08:25 +0000770#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000771 if (use_rt_clock) {
772 struct timespec ts;
773 clock_gettime(CLOCK_MONOTONIC, &ts);
774 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000775 } else
bellard1dce7c32006-07-13 23:20:22 +0000776#endif
777 {
778 /* XXX: using gettimeofday leads to problems if the date
779 changes, so it should be avoided. */
780 struct timeval tv;
781 gettimeofday(&tv, NULL);
782 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
783 }
784}
bellard1dce7c32006-07-13 23:20:22 +0000785#endif
786
pbrook2e70f6e2008-06-29 01:03:05 +0000787/* Return the virtual CPU time, based on the instruction counter. */
788static int64_t cpu_get_icount(void)
789{
790 int64_t icount;
791 CPUState *env = cpu_single_env;;
792 icount = qemu_icount;
793 if (env) {
794 if (!can_do_io(env))
795 fprintf(stderr, "Bad clock read\n");
796 icount -= (env->icount_decr.u16.low + env->icount_extra);
797 }
798 return qemu_icount_bias + (icount << icount_time_shift);
799}
800
bellard1dce7c32006-07-13 23:20:22 +0000801/***********************************************************/
802/* guest cycle counter */
803
804static int64_t cpu_ticks_prev;
805static int64_t cpu_ticks_offset;
806static int64_t cpu_clock_offset;
807static int cpu_ticks_enabled;
808
809/* return the host CPU cycle counter and handle stop/restart */
810int64_t cpu_get_ticks(void)
811{
pbrook2e70f6e2008-06-29 01:03:05 +0000812 if (use_icount) {
813 return cpu_get_icount();
814 }
bellard1dce7c32006-07-13 23:20:22 +0000815 if (!cpu_ticks_enabled) {
816 return cpu_ticks_offset;
817 } else {
818 int64_t ticks;
819 ticks = cpu_get_real_ticks();
820 if (cpu_ticks_prev > ticks) {
821 /* Note: non increasing ticks may happen if the host uses
822 software suspend */
823 cpu_ticks_offset += cpu_ticks_prev - ticks;
824 }
825 cpu_ticks_prev = ticks;
826 return ticks + cpu_ticks_offset;
827 }
828}
829
830/* return the host CPU monotonic timer and handle stop/restart */
831static int64_t cpu_get_clock(void)
832{
833 int64_t ti;
834 if (!cpu_ticks_enabled) {
835 return cpu_clock_offset;
836 } else {
837 ti = get_clock();
838 return ti + cpu_clock_offset;
839 }
840}
841
842/* enable cpu_get_ticks() */
843void cpu_enable_ticks(void)
844{
845 if (!cpu_ticks_enabled) {
846 cpu_ticks_offset -= cpu_get_real_ticks();
847 cpu_clock_offset -= get_clock();
848 cpu_ticks_enabled = 1;
849 }
850}
851
852/* disable cpu_get_ticks() : the clock is stopped. You must not call
853 cpu_get_ticks() after that. */
854void cpu_disable_ticks(void)
855{
856 if (cpu_ticks_enabled) {
857 cpu_ticks_offset = cpu_get_ticks();
858 cpu_clock_offset = cpu_get_clock();
859 cpu_ticks_enabled = 0;
860 }
861}
862
863/***********************************************************/
864/* timers */
ths5fafdf22007-09-16 21:08:06 +0000865
bellard8a7ddc32004-03-31 19:00:16 +0000866#define QEMU_TIMER_REALTIME 0
867#define QEMU_TIMER_VIRTUAL 1
868
869struct QEMUClock {
870 int type;
871 /* XXX: add frequency */
872};
873
874struct QEMUTimer {
875 QEMUClock *clock;
876 int64_t expire_time;
877 QEMUTimerCB *cb;
878 void *opaque;
879 struct QEMUTimer *next;
880};
881
thsc8994012007-08-19 21:56:03 +0000882struct qemu_alarm_timer {
883 char const *name;
thsefe75412007-08-24 01:36:32 +0000884 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000885
886 int (*start)(struct qemu_alarm_timer *t);
887 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000888 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000889 void *priv;
890};
891
thsefe75412007-08-24 01:36:32 +0000892#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000893#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000894
895static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
896{
897 return t->flags & ALARM_FLAG_DYNTICKS;
898}
899
900static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
901{
902 if (!alarm_has_dynticks(t))
903 return;
904
905 t->rearm(t);
906}
907
908/* TODO: MIN_TIMER_REARM_US should be optimized */
909#define MIN_TIMER_REARM_US 250
910
thsc8994012007-08-19 21:56:03 +0000911static struct qemu_alarm_timer *alarm_timer;
aliguorif49e58d2008-11-05 21:22:34 +0000912#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +0000913static int alarm_timer_rfd, alarm_timer_wfd;
aliguorif49e58d2008-11-05 21:22:34 +0000914#endif
thsc8994012007-08-19 21:56:03 +0000915
916#ifdef _WIN32
917
918struct qemu_alarm_win32 {
919 MMRESULT timerId;
920 HANDLE host_alarm;
921 unsigned int period;
922} alarm_win32_data = {0, NULL, -1};
923
924static int win32_start_timer(struct qemu_alarm_timer *t);
925static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000926static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000927
928#else
929
930static int unix_start_timer(struct qemu_alarm_timer *t);
931static void unix_stop_timer(struct qemu_alarm_timer *t);
932
ths231c6582007-08-26 17:29:15 +0000933#ifdef __linux__
934
thsefe75412007-08-24 01:36:32 +0000935static int dynticks_start_timer(struct qemu_alarm_timer *t);
936static void dynticks_stop_timer(struct qemu_alarm_timer *t);
937static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
938
thsc40ec5a2007-08-19 22:09:40 +0000939static int hpet_start_timer(struct qemu_alarm_timer *t);
940static void hpet_stop_timer(struct qemu_alarm_timer *t);
941
thsc8994012007-08-19 21:56:03 +0000942static int rtc_start_timer(struct qemu_alarm_timer *t);
943static void rtc_stop_timer(struct qemu_alarm_timer *t);
944
thsefe75412007-08-24 01:36:32 +0000945#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000946
947#endif /* _WIN32 */
948
pbrook2e70f6e2008-06-29 01:03:05 +0000949/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000950 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000951 When the guest is idle real and virtual time will be aligned in
952 the IO wait loop. */
953#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
954
955static void icount_adjust(void)
956{
957 int64_t cur_time;
958 int64_t cur_icount;
959 int64_t delta;
960 static int64_t last_delta;
961 /* If the VM is not running, then do nothing. */
962 if (!vm_running)
963 return;
964
965 cur_time = cpu_get_clock();
966 cur_icount = qemu_get_clock(vm_clock);
967 delta = cur_icount - cur_time;
968 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
969 if (delta > 0
970 && last_delta + ICOUNT_WOBBLE < delta * 2
971 && icount_time_shift > 0) {
972 /* The guest is getting too far ahead. Slow time down. */
973 icount_time_shift--;
974 }
975 if (delta < 0
976 && last_delta - ICOUNT_WOBBLE > delta * 2
977 && icount_time_shift < MAX_ICOUNT_SHIFT) {
978 /* The guest is getting too far behind. Speed time up. */
979 icount_time_shift++;
980 }
981 last_delta = delta;
982 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
983}
984
985static void icount_adjust_rt(void * opaque)
986{
987 qemu_mod_timer(icount_rt_timer,
988 qemu_get_clock(rt_clock) + 1000);
989 icount_adjust();
990}
991
992static void icount_adjust_vm(void * opaque)
993{
994 qemu_mod_timer(icount_vm_timer,
995 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
996 icount_adjust();
997}
998
999static void init_icount_adjust(void)
1000{
1001 /* Have both realtime and virtual time triggers for speed adjustment.
1002 The realtime trigger catches emulated time passing too slowly,
1003 the virtual time trigger catches emulated time passing too fast.
1004 Realtime triggers occur even when idle, so use them less frequently
1005 than VM triggers. */
1006 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1007 qemu_mod_timer(icount_rt_timer,
1008 qemu_get_clock(rt_clock) + 1000);
1009 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1010 qemu_mod_timer(icount_vm_timer,
1011 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1012}
1013
thsc8994012007-08-19 21:56:03 +00001014static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +00001015#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +00001016#ifdef __linux__
thsefe75412007-08-24 01:36:32 +00001017 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1018 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001019 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +00001020 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001021 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +00001022 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001023#endif
thsefe75412007-08-24 01:36:32 +00001024 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001025#else
thsefe75412007-08-24 01:36:32 +00001026 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1027 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1028 {"win32", 0, win32_start_timer,
1029 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +00001030#endif
1031 {NULL, }
1032};
1033
blueswir13f47aa82008-03-09 06:59:01 +00001034static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +00001035{
1036 int i;
1037
1038 printf("Available alarm timers, in order of precedence:\n");
1039 for (i = 0; alarm_timers[i].name; i++)
1040 printf("%s\n", alarm_timers[i].name);
1041}
1042
1043static void configure_alarms(char const *opt)
1044{
1045 int i;
1046 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +00001047 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +00001048 char *arg;
1049 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +00001050 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +00001051
aurel323adda042008-03-09 23:43:49 +00001052 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +00001053 show_available_alarms();
1054 exit(0);
1055 }
1056
1057 arg = strdup(opt);
1058
1059 /* Reorder the array */
1060 name = strtok(arg, ",");
1061 while (name) {
balroge2b577e2007-09-17 21:25:20 +00001062 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +00001063 if (!strcmp(alarm_timers[i].name, name))
1064 break;
1065 }
1066
1067 if (i == count) {
1068 fprintf(stderr, "Unknown clock %s\n", name);
1069 goto next;
1070 }
1071
1072 if (i < cur)
1073 /* Ignore */
1074 goto next;
1075
1076 /* Swap */
1077 tmp = alarm_timers[i];
1078 alarm_timers[i] = alarm_timers[cur];
1079 alarm_timers[cur] = tmp;
1080
1081 cur++;
1082next:
1083 name = strtok(NULL, ",");
1084 }
1085
1086 free(arg);
1087
1088 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +00001089 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +00001090 for (i = cur; i < count; i++)
1091 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +00001092 } else {
1093 show_available_alarms();
1094 exit(1);
thsf3dcfad2007-08-24 01:26:02 +00001095 }
thsf3dcfad2007-08-24 01:26:02 +00001096}
1097
bellard8a7ddc32004-03-31 19:00:16 +00001098QEMUClock *rt_clock;
1099QEMUClock *vm_clock;
1100
1101static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +00001102
pbrook9596ebb2007-11-18 01:44:38 +00001103static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001104{
1105 QEMUClock *clock;
1106 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +00001107 clock->type = type;
1108 return clock;
1109}
1110
1111QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1112{
1113 QEMUTimer *ts;
1114
1115 ts = qemu_mallocz(sizeof(QEMUTimer));
1116 ts->clock = clock;
1117 ts->cb = cb;
1118 ts->opaque = opaque;
1119 return ts;
1120}
1121
1122void qemu_free_timer(QEMUTimer *ts)
1123{
1124 qemu_free(ts);
1125}
1126
1127/* stop a timer, but do not dealloc it */
1128void qemu_del_timer(QEMUTimer *ts)
1129{
1130 QEMUTimer **pt, *t;
1131
1132 /* NOTE: this code must be signal safe because
1133 qemu_timer_expired() can be called from a signal. */
1134 pt = &active_timers[ts->clock->type];
1135 for(;;) {
1136 t = *pt;
1137 if (!t)
1138 break;
1139 if (t == ts) {
1140 *pt = t->next;
1141 break;
1142 }
1143 pt = &t->next;
1144 }
1145}
1146
1147/* modify the current timer so that it will be fired when current_time
1148 >= expire_time. The corresponding callback will be called. */
1149void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1150{
1151 QEMUTimer **pt, *t;
1152
1153 qemu_del_timer(ts);
1154
1155 /* add the timer in the sorted list */
1156 /* NOTE: this code must be signal safe because
1157 qemu_timer_expired() can be called from a signal. */
1158 pt = &active_timers[ts->clock->type];
1159 for(;;) {
1160 t = *pt;
1161 if (!t)
1162 break;
ths5fafdf22007-09-16 21:08:06 +00001163 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001164 break;
1165 pt = &t->next;
1166 }
1167 ts->expire_time = expire_time;
1168 ts->next = *pt;
1169 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001170
1171 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001172 if (pt == &active_timers[ts->clock->type]) {
1173 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1174 qemu_rearm_alarm_timer(alarm_timer);
1175 }
1176 /* Interrupt execution to force deadline recalculation. */
1177 if (use_icount && cpu_single_env) {
1178 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1179 }
1180 }
bellard8a7ddc32004-03-31 19:00:16 +00001181}
1182
1183int qemu_timer_pending(QEMUTimer *ts)
1184{
1185 QEMUTimer *t;
1186 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1187 if (t == ts)
1188 return 1;
1189 }
1190 return 0;
1191}
1192
1193static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1194{
1195 if (!timer_head)
1196 return 0;
1197 return (timer_head->expire_time <= current_time);
1198}
1199
1200static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1201{
1202 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001203
bellard8a7ddc32004-03-31 19:00:16 +00001204 for(;;) {
1205 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001206 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001207 break;
1208 /* remove timer from the list before calling the callback */
1209 *ptimer_head = ts->next;
1210 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001211
bellard8a7ddc32004-03-31 19:00:16 +00001212 /* run the callback (the timer list can be modified) */
1213 ts->cb(ts->opaque);
1214 }
1215}
1216
1217int64_t qemu_get_clock(QEMUClock *clock)
1218{
1219 switch(clock->type) {
1220 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001221 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001222 default:
1223 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001224 if (use_icount) {
1225 return cpu_get_icount();
1226 } else {
1227 return cpu_get_clock();
1228 }
bellard8a7ddc32004-03-31 19:00:16 +00001229 }
1230}
1231
bellard1dce7c32006-07-13 23:20:22 +00001232static void init_timers(void)
1233{
1234 init_get_clock();
1235 ticks_per_sec = QEMU_TIMER_BASE;
1236 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1237 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1238}
1239
bellard8a7ddc32004-03-31 19:00:16 +00001240/* save a timer */
1241void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1242{
1243 uint64_t expire_time;
1244
1245 if (qemu_timer_pending(ts)) {
1246 expire_time = ts->expire_time;
1247 } else {
1248 expire_time = -1;
1249 }
1250 qemu_put_be64(f, expire_time);
1251}
1252
1253void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1254{
1255 uint64_t expire_time;
1256
1257 expire_time = qemu_get_be64(f);
1258 if (expire_time != -1) {
1259 qemu_mod_timer(ts, expire_time);
1260 } else {
1261 qemu_del_timer(ts);
1262 }
1263}
1264
1265static void timer_save(QEMUFile *f, void *opaque)
1266{
1267 if (cpu_ticks_enabled) {
1268 hw_error("cannot save state if virtual timers are running");
1269 }
thsbee8d682007-12-16 23:41:11 +00001270 qemu_put_be64(f, cpu_ticks_offset);
1271 qemu_put_be64(f, ticks_per_sec);
1272 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001273}
1274
1275static int timer_load(QEMUFile *f, void *opaque, int version_id)
1276{
bellardc88676f2006-08-06 13:36:11 +00001277 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001278 return -EINVAL;
1279 if (cpu_ticks_enabled) {
1280 return -EINVAL;
1281 }
thsbee8d682007-12-16 23:41:11 +00001282 cpu_ticks_offset=qemu_get_be64(f);
1283 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001284 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001285 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001286 }
bellard8a7ddc32004-03-31 19:00:16 +00001287 return 0;
1288}
1289
bellard67b915a2004-03-31 23:37:16 +00001290#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001291void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001292 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1293#else
bellard8a7ddc32004-03-31 19:00:16 +00001294static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001295#endif
bellard8a7ddc32004-03-31 19:00:16 +00001296{
bellard02ba45c2004-06-25 14:46:23 +00001297#if 0
1298#define DISP_FREQ 1000
1299 {
1300 static int64_t delta_min = INT64_MAX;
1301 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1302 static int count;
1303 ti = qemu_get_clock(vm_clock);
1304 if (last_clock != 0) {
1305 delta = ti - last_clock;
1306 if (delta < delta_min)
1307 delta_min = delta;
1308 if (delta > delta_max)
1309 delta_max = delta;
1310 delta_cum += delta;
1311 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001312 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001313 muldiv64(delta_min, 1000000, ticks_per_sec),
1314 muldiv64(delta_max, 1000000, ticks_per_sec),
1315 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1316 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1317 count = 0;
1318 delta_min = INT64_MAX;
1319 delta_max = 0;
1320 delta_cum = 0;
1321 }
1322 }
1323 last_clock = ti;
1324 }
1325#endif
thsefe75412007-08-24 01:36:32 +00001326 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001327 (!use_icount &&
1328 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1329 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001330 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1331 qemu_get_clock(rt_clock))) {
aliguoric96f1a42008-11-05 20:29:45 +00001332 CPUState *env = next_cpu;
aliguoric96f1a42008-11-05 20:29:45 +00001333
bellard06d9f2f2006-05-01 13:23:04 +00001334#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001335 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1336 SetEvent(data->host_alarm);
aliguorif49e58d2008-11-05 21:22:34 +00001337#else
1338 static const char byte = 0;
aliguoric96f1a42008-11-05 20:29:45 +00001339 write(alarm_timer_wfd, &byte, sizeof(byte));
aliguorif49e58d2008-11-05 21:22:34 +00001340#endif
balrogd5d08332008-01-05 19:41:47 +00001341 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1342
balrog4f8eb8d2007-12-16 12:39:38 +00001343 if (env) {
1344 /* stop the currently executing cpu because a timer occured */
1345 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
bellarda332e112005-09-03 17:55:47 +00001346#ifdef USE_KQEMU
balrog4f8eb8d2007-12-16 12:39:38 +00001347 if (env->kqemu_enabled) {
1348 kqemu_cpu_interrupt(env);
1349 }
balrogee5605e2007-12-03 03:01:40 +00001350#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001351 }
balrogee5605e2007-12-03 03:01:40 +00001352 event_pending = 1;
bellard8a7ddc32004-03-31 19:00:16 +00001353 }
1354}
1355
pbrook2e70f6e2008-06-29 01:03:05 +00001356static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001357{
pbrook2e70f6e2008-06-29 01:03:05 +00001358 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001359
1360 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001361 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1362 qemu_get_clock(vm_clock);
1363 } else {
1364 /* To avoid problems with overflow limit this to 2^32. */
1365 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001366 }
1367
pbrook2e70f6e2008-06-29 01:03:05 +00001368 if (delta < 0)
1369 delta = 0;
thsefe75412007-08-24 01:36:32 +00001370
pbrook2e70f6e2008-06-29 01:03:05 +00001371 return delta;
1372}
1373
blueswir18632fb92008-09-14 13:59:34 +00001374#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001375static uint64_t qemu_next_deadline_dyntick(void)
1376{
1377 int64_t delta;
1378 int64_t rtdelta;
1379
1380 if (use_icount)
1381 delta = INT32_MAX;
1382 else
1383 delta = (qemu_next_deadline() + 999) / 1000;
1384
1385 if (active_timers[QEMU_TIMER_REALTIME]) {
1386 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1387 qemu_get_clock(rt_clock))*1000;
1388 if (rtdelta < delta)
1389 delta = rtdelta;
1390 }
1391
1392 if (delta < MIN_TIMER_REARM_US)
1393 delta = MIN_TIMER_REARM_US;
1394
1395 return delta;
thsefe75412007-08-24 01:36:32 +00001396}
blueswir18632fb92008-09-14 13:59:34 +00001397#endif
thsefe75412007-08-24 01:36:32 +00001398
bellardfd872592004-05-12 19:11:15 +00001399#ifndef _WIN32
1400
aliguori7183b4b2008-11-05 20:40:18 +00001401/* Sets a specific flag */
1402static int fcntl_setfl(int fd, int flag)
1403{
1404 int flags;
1405
1406 flags = fcntl(fd, F_GETFL);
1407 if (flags == -1)
1408 return -errno;
1409
1410 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1411 return -errno;
1412
1413 return 0;
1414}
1415
bellard829309c2004-05-20 13:20:12 +00001416#if defined(__linux__)
1417
bellardfd872592004-05-12 19:11:15 +00001418#define RTC_FREQ 1024
1419
aurel32de9a95f2008-11-11 13:41:01 +00001420static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001421{
thsc8994012007-08-19 21:56:03 +00001422 struct sigaction act;
1423
1424 /* timer signal */
1425 sigfillset(&act.sa_mask);
1426 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001427 act.sa_handler = host_alarm_handler;
1428
1429 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001430 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001431 fcntl(fd, F_SETOWN, getpid());
1432}
1433
thsc40ec5a2007-08-19 22:09:40 +00001434static int hpet_start_timer(struct qemu_alarm_timer *t)
1435{
1436 struct hpet_info info;
1437 int r, fd;
1438
1439 fd = open("/dev/hpet", O_RDONLY);
1440 if (fd < 0)
1441 return -1;
1442
1443 /* Set frequency */
1444 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1445 if (r < 0) {
1446 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1447 "error, but for better emulation accuracy type:\n"
1448 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1449 goto fail;
1450 }
1451
1452 /* Check capabilities */
1453 r = ioctl(fd, HPET_INFO, &info);
1454 if (r < 0)
1455 goto fail;
1456
1457 /* Enable periodic mode */
1458 r = ioctl(fd, HPET_EPI, 0);
1459 if (info.hi_flags && (r < 0))
1460 goto fail;
1461
1462 /* Enable interrupt */
1463 r = ioctl(fd, HPET_IE_ON, 0);
1464 if (r < 0)
1465 goto fail;
1466
1467 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001468 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001469
1470 return 0;
1471fail:
1472 close(fd);
1473 return -1;
1474}
1475
1476static void hpet_stop_timer(struct qemu_alarm_timer *t)
1477{
pbrookfcdc2122007-08-23 20:22:22 +00001478 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001479
1480 close(fd);
1481}
1482
thsc8994012007-08-19 21:56:03 +00001483static int rtc_start_timer(struct qemu_alarm_timer *t)
1484{
1485 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001486 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001487
balrogaeb30be2007-07-02 15:03:13 +00001488 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001489 if (rtc_fd < 0)
1490 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001491 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1492 if (current_rtc_freq != RTC_FREQ &&
1493 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001494 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1495 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1496 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1497 goto fail;
1498 }
1499 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1500 fail:
1501 close(rtc_fd);
1502 return -1;
1503 }
thsc8994012007-08-19 21:56:03 +00001504
1505 enable_sigio_timer(rtc_fd);
1506
pbrookfcdc2122007-08-23 20:22:22 +00001507 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001508
bellardfd872592004-05-12 19:11:15 +00001509 return 0;
1510}
1511
thsc8994012007-08-19 21:56:03 +00001512static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001513{
pbrookfcdc2122007-08-23 20:22:22 +00001514 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001515
1516 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001517}
1518
thsefe75412007-08-24 01:36:32 +00001519static int dynticks_start_timer(struct qemu_alarm_timer *t)
1520{
1521 struct sigevent ev;
1522 timer_t host_timer;
1523 struct sigaction act;
1524
1525 sigfillset(&act.sa_mask);
1526 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001527 act.sa_handler = host_alarm_handler;
1528
1529 sigaction(SIGALRM, &act, NULL);
1530
1531 ev.sigev_value.sival_int = 0;
1532 ev.sigev_notify = SIGEV_SIGNAL;
1533 ev.sigev_signo = SIGALRM;
1534
1535 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1536 perror("timer_create");
1537
1538 /* disable dynticks */
1539 fprintf(stderr, "Dynamic Ticks disabled\n");
1540
1541 return -1;
1542 }
1543
blueswir10399bfe2008-11-16 11:37:18 +00001544 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001545
1546 return 0;
1547}
1548
1549static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1550{
blueswir10399bfe2008-11-16 11:37:18 +00001551 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001552
1553 timer_delete(host_timer);
1554}
1555
1556static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1557{
blueswir10399bfe2008-11-16 11:37:18 +00001558 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001559 struct itimerspec timeout;
1560 int64_t nearest_delta_us = INT64_MAX;
1561 int64_t current_us;
1562
1563 if (!active_timers[QEMU_TIMER_REALTIME] &&
1564 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001565 return;
thsefe75412007-08-24 01:36:32 +00001566
pbrook2e70f6e2008-06-29 01:03:05 +00001567 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001568
1569 /* check whether a timer is already running */
1570 if (timer_gettime(host_timer, &timeout)) {
1571 perror("gettime");
1572 fprintf(stderr, "Internal timer error: aborting\n");
1573 exit(1);
1574 }
1575 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1576 if (current_us && current_us <= nearest_delta_us)
1577 return;
1578
1579 timeout.it_interval.tv_sec = 0;
1580 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1581 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1582 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1583 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1584 perror("settime");
1585 fprintf(stderr, "Internal timer error: aborting\n");
1586 exit(1);
1587 }
1588}
1589
ths70744b32007-08-26 17:31:30 +00001590#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001591
thsc8994012007-08-19 21:56:03 +00001592static int unix_start_timer(struct qemu_alarm_timer *t)
1593{
1594 struct sigaction act;
1595 struct itimerval itv;
1596 int err;
1597
1598 /* timer signal */
1599 sigfillset(&act.sa_mask);
1600 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001601 act.sa_handler = host_alarm_handler;
1602
1603 sigaction(SIGALRM, &act, NULL);
1604
1605 itv.it_interval.tv_sec = 0;
1606 /* for i386 kernel 2.6 to get 1 ms */
1607 itv.it_interval.tv_usec = 999;
1608 itv.it_value.tv_sec = 0;
1609 itv.it_value.tv_usec = 10 * 1000;
1610
1611 err = setitimer(ITIMER_REAL, &itv, NULL);
1612 if (err)
1613 return -1;
1614
1615 return 0;
1616}
1617
1618static void unix_stop_timer(struct qemu_alarm_timer *t)
1619{
1620 struct itimerval itv;
1621
1622 memset(&itv, 0, sizeof(itv));
1623 setitimer(ITIMER_REAL, &itv, NULL);
1624}
1625
bellard829309c2004-05-20 13:20:12 +00001626#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001627
aliguorif49e58d2008-11-05 21:22:34 +00001628static void try_to_rearm_timer(void *opaque)
1629{
1630 struct qemu_alarm_timer *t = opaque;
1631#ifndef _WIN32
1632 ssize_t len;
1633
1634 /* Drain the notify pipe */
1635 do {
1636 char buffer[512];
1637 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1638 } while ((len == -1 && errno == EINTR) || len > 0);
1639#endif
1640
aliguorif49e58d2008-11-05 21:22:34 +00001641 if (t->flags & ALARM_FLAG_EXPIRED) {
1642 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1643 qemu_rearm_alarm_timer(alarm_timer);
1644 }
1645}
1646
thsc8994012007-08-19 21:56:03 +00001647#ifdef _WIN32
1648
1649static int win32_start_timer(struct qemu_alarm_timer *t)
1650{
1651 TIMECAPS tc;
1652 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001653 UINT flags;
thsc8994012007-08-19 21:56:03 +00001654
1655 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1656 if (!data->host_alarm) {
1657 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001658 return -1;
thsc8994012007-08-19 21:56:03 +00001659 }
1660
1661 memset(&tc, 0, sizeof(tc));
1662 timeGetDevCaps(&tc, sizeof(tc));
1663
1664 if (data->period < tc.wPeriodMin)
1665 data->period = tc.wPeriodMin;
1666
1667 timeBeginPeriod(data->period);
1668
thsefe75412007-08-24 01:36:32 +00001669 flags = TIME_CALLBACK_FUNCTION;
1670 if (alarm_has_dynticks(t))
1671 flags |= TIME_ONESHOT;
1672 else
1673 flags |= TIME_PERIODIC;
1674
thsc8994012007-08-19 21:56:03 +00001675 data->timerId = timeSetEvent(1, // interval (ms)
1676 data->period, // resolution
1677 host_alarm_handler, // function
1678 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001679 flags);
thsc8994012007-08-19 21:56:03 +00001680
1681 if (!data->timerId) {
1682 perror("Failed to initialize win32 alarm timer");
1683
1684 timeEndPeriod(data->period);
1685 CloseHandle(data->host_alarm);
1686 return -1;
1687 }
1688
aliguorif49e58d2008-11-05 21:22:34 +00001689 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
thsc8994012007-08-19 21:56:03 +00001690
1691 return 0;
1692}
1693
1694static void win32_stop_timer(struct qemu_alarm_timer *t)
1695{
1696 struct qemu_alarm_win32 *data = t->priv;
1697
1698 timeKillEvent(data->timerId);
1699 timeEndPeriod(data->period);
1700
1701 CloseHandle(data->host_alarm);
1702}
1703
thsefe75412007-08-24 01:36:32 +00001704static void win32_rearm_timer(struct qemu_alarm_timer *t)
1705{
1706 struct qemu_alarm_win32 *data = t->priv;
1707 uint64_t nearest_delta_us;
1708
1709 if (!active_timers[QEMU_TIMER_REALTIME] &&
1710 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001711 return;
thsefe75412007-08-24 01:36:32 +00001712
pbrook2e70f6e2008-06-29 01:03:05 +00001713 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001714 nearest_delta_us /= 1000;
1715
1716 timeKillEvent(data->timerId);
1717
1718 data->timerId = timeSetEvent(1,
1719 data->period,
1720 host_alarm_handler,
1721 (DWORD)t,
1722 TIME_ONESHOT | TIME_PERIODIC);
1723
1724 if (!data->timerId) {
1725 perror("Failed to re-arm win32 alarm timer");
1726
1727 timeEndPeriod(data->period);
1728 CloseHandle(data->host_alarm);
1729 exit(1);
1730 }
1731}
1732
thsc8994012007-08-19 21:56:03 +00001733#endif /* _WIN32 */
1734
aliguori7183b4b2008-11-05 20:40:18 +00001735static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001736{
blueswir1223f0d72008-09-30 18:12:18 +00001737 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001738 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001739
1740#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +00001741 int fds[2];
1742
aliguori7183b4b2008-11-05 20:40:18 +00001743 err = pipe(fds);
1744 if (err == -1)
1745 return -errno;
1746
1747 err = fcntl_setfl(fds[0], O_NONBLOCK);
1748 if (err < 0)
1749 goto fail;
1750
1751 err = fcntl_setfl(fds[1], O_NONBLOCK);
1752 if (err < 0)
1753 goto fail;
1754
aliguoric96f1a42008-11-05 20:29:45 +00001755 alarm_timer_rfd = fds[0];
1756 alarm_timer_wfd = fds[1];
aliguorif49e58d2008-11-05 21:22:34 +00001757#endif
bellard06d9f2f2006-05-01 13:23:04 +00001758
thsc8994012007-08-19 21:56:03 +00001759 for (i = 0; alarm_timers[i].name; i++) {
1760 t = &alarm_timers[i];
1761
thsc8994012007-08-19 21:56:03 +00001762 err = t->start(t);
1763 if (!err)
1764 break;
bellard67b915a2004-03-31 23:37:16 +00001765 }
bellardfd872592004-05-12 19:11:15 +00001766
thsc8994012007-08-19 21:56:03 +00001767 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001768 err = -ENOENT;
1769 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001770 }
thsc8994012007-08-19 21:56:03 +00001771
aliguorif49e58d2008-11-05 21:22:34 +00001772#ifndef _WIN32
aliguori6abfbd72008-11-05 20:49:37 +00001773 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1774 try_to_rearm_timer, NULL, t);
aliguorif49e58d2008-11-05 21:22:34 +00001775#endif
aliguori6abfbd72008-11-05 20:49:37 +00001776
thsc8994012007-08-19 21:56:03 +00001777 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001778
aliguori6abfbd72008-11-05 20:49:37 +00001779 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001780
1781fail:
aliguorif49e58d2008-11-05 21:22:34 +00001782#ifndef _WIN32
aliguori7183b4b2008-11-05 20:40:18 +00001783 close(fds[0]);
1784 close(fds[1]);
aliguorif49e58d2008-11-05 21:22:34 +00001785#endif
aliguori7183b4b2008-11-05 20:40:18 +00001786 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001787}
1788
pbrook9596ebb2007-11-18 01:44:38 +00001789static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001790{
thsc8994012007-08-19 21:56:03 +00001791 alarm_timer->stop(alarm_timer);
1792 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001793}
1794
bellardc4b1fcc2004-03-14 21:44:30 +00001795/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001796/* host time/date access */
1797void qemu_get_timedate(struct tm *tm, int offset)
1798{
1799 time_t ti;
1800 struct tm *ret;
1801
1802 time(&ti);
1803 ti += offset;
1804 if (rtc_date_offset == -1) {
1805 if (rtc_utc)
1806 ret = gmtime(&ti);
1807 else
1808 ret = localtime(&ti);
1809 } else {
1810 ti -= rtc_date_offset;
1811 ret = gmtime(&ti);
1812 }
1813
1814 memcpy(tm, ret, sizeof(struct tm));
1815}
1816
1817int qemu_timedate_diff(struct tm *tm)
1818{
1819 time_t seconds;
1820
1821 if (rtc_date_offset == -1)
1822 if (rtc_utc)
1823 seconds = mktimegm(tm);
1824 else
1825 seconds = mktime(tm);
1826 else
1827 seconds = mktimegm(tm) + rtc_date_offset;
1828
1829 return seconds - time(NULL);
1830}
1831
bellardfd1dff42006-02-01 21:29:26 +00001832#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001833static void socket_cleanup(void)
1834{
1835 WSACleanup();
1836}
bellard82c643f2004-07-14 17:28:13 +00001837
bellardfd1dff42006-02-01 21:29:26 +00001838static int socket_init(void)
1839{
1840 WSADATA Data;
1841 int ret, err;
1842
1843 ret = WSAStartup(MAKEWORD(2,2), &Data);
1844 if (ret != 0) {
1845 err = WSAGetLastError();
1846 fprintf(stderr, "WSAStartup: %d\n", err);
1847 return -1;
1848 }
1849 atexit(socket_cleanup);
1850 return 0;
1851}
aurel3264b7b732008-05-05 10:05:31 +00001852#endif
1853
aliguori63a01ef2008-10-31 19:10:00 +00001854const char *get_opt_name(char *buf, int buf_size, const char *p)
thse4bcb142007-12-02 04:51:10 +00001855{
1856 char *q;
thse4bcb142007-12-02 04:51:10 +00001857
balrog609497a2008-01-14 02:56:53 +00001858 q = buf;
1859 while (*p != '\0' && *p != '=') {
1860 if (q && (q - buf) < buf_size - 1)
1861 *q++ = *p;
1862 p++;
1863 }
1864 if (q)
1865 *q = '\0';
1866
1867 return p;
1868}
1869
aliguori63a01ef2008-10-31 19:10:00 +00001870const char *get_opt_value(char *buf, int buf_size, const char *p)
balrog609497a2008-01-14 02:56:53 +00001871{
1872 char *q;
1873
thse4bcb142007-12-02 04:51:10 +00001874 q = buf;
1875 while (*p != '\0') {
balrog609497a2008-01-14 02:56:53 +00001876 if (*p == ',') {
1877 if (*(p + 1) != ',')
thse4bcb142007-12-02 04:51:10 +00001878 break;
thse4bcb142007-12-02 04:51:10 +00001879 p++;
balrog609497a2008-01-14 02:56:53 +00001880 }
thse4bcb142007-12-02 04:51:10 +00001881 if (q && (q - buf) < buf_size - 1)
1882 *q++ = *p;
1883 p++;
1884 }
1885 if (q)
1886 *q = '\0';
1887
1888 return p;
1889}
1890
aliguori63a01ef2008-10-31 19:10:00 +00001891int get_param_value(char *buf, int buf_size,
1892 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001893{
1894 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001895 char option[128];
1896
1897 p = str;
1898 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001899 p = get_opt_name(option, sizeof(option), p);
bellard7c9d8e02005-11-15 22:16:05 +00001900 if (*p != '=')
1901 break;
1902 p++;
1903 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001904 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001905 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001906 } else {
balrog609497a2008-01-14 02:56:53 +00001907 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001908 }
1909 if (*p != ',')
1910 break;
1911 p++;
1912 }
1913 return 0;
1914}
1915
aliguori63a01ef2008-10-31 19:10:00 +00001916int check_params(char *buf, int buf_size,
1917 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001918{
1919 const char *p;
1920 int i;
1921
1922 p = str;
1923 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001924 p = get_opt_name(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001925 if (*p != '=')
1926 return -1;
1927 p++;
1928 for(i = 0; params[i] != NULL; i++)
1929 if (!strcmp(params[i], buf))
1930 break;
1931 if (params[i] == NULL)
1932 return -1;
balrog609497a2008-01-14 02:56:53 +00001933 p = get_opt_value(NULL, 0, p);
thse4bcb142007-12-02 04:51:10 +00001934 if (*p != ',')
1935 break;
1936 p++;
1937 }
1938 return 0;
1939}
1940
balrog1ae26a12008-09-28 23:19:47 +00001941/***********************************************************/
1942/* Bluetooth support */
1943static int nb_hcis;
1944static int cur_hci;
1945static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001946
balrog1ae26a12008-09-28 23:19:47 +00001947static struct bt_vlan_s {
1948 struct bt_scatternet_s net;
1949 int id;
1950 struct bt_vlan_s *next;
1951} *first_bt_vlan;
1952
1953/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001954static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001955{
1956 struct bt_vlan_s **pvlan, *vlan;
1957 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1958 if (vlan->id == id)
1959 return &vlan->net;
1960 }
1961 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1962 vlan->id = id;
1963 pvlan = &first_bt_vlan;
1964 while (*pvlan != NULL)
1965 pvlan = &(*pvlan)->next;
1966 *pvlan = vlan;
1967 return &vlan->net;
1968}
1969
1970static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1971{
1972}
1973
1974static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1975{
1976 return -ENOTSUP;
1977}
1978
1979static struct HCIInfo null_hci = {
1980 .cmd_send = null_hci_send,
1981 .sco_send = null_hci_send,
1982 .acl_send = null_hci_send,
1983 .bdaddr_set = null_hci_addr_set,
1984};
1985
1986struct HCIInfo *qemu_next_hci(void)
1987{
1988 if (cur_hci == nb_hcis)
1989 return &null_hci;
1990
1991 return hci_table[cur_hci++];
1992}
1993
balrogdc72ac12008-11-09 00:04:26 +00001994static struct HCIInfo *hci_init(const char *str)
1995{
1996 char *endp;
1997 struct bt_scatternet_s *vlan = 0;
1998
1999 if (!strcmp(str, "null"))
2000 /* null */
2001 return &null_hci;
2002 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
2003 /* host[:hciN] */
2004 return bt_host_hci(str[4] ? str + 5 : "hci0");
2005 else if (!strncmp(str, "hci", 3)) {
2006 /* hci[,vlan=n] */
2007 if (str[3]) {
2008 if (!strncmp(str + 3, ",vlan=", 6)) {
2009 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2010 if (*endp)
2011 vlan = 0;
2012 }
2013 } else
2014 vlan = qemu_find_bt_vlan(0);
2015 if (vlan)
2016 return bt_new_hci(vlan);
2017 }
2018
2019 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2020
2021 return 0;
2022}
2023
2024static int bt_hci_parse(const char *str)
2025{
2026 struct HCIInfo *hci;
2027 bdaddr_t bdaddr;
2028
2029 if (nb_hcis >= MAX_NICS) {
2030 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2031 return -1;
2032 }
2033
2034 hci = hci_init(str);
2035 if (!hci)
2036 return -1;
2037
2038 bdaddr.b[0] = 0x52;
2039 bdaddr.b[1] = 0x54;
2040 bdaddr.b[2] = 0x00;
2041 bdaddr.b[3] = 0x12;
2042 bdaddr.b[4] = 0x34;
2043 bdaddr.b[5] = 0x56 + nb_hcis;
2044 hci->bdaddr_set(hci, bdaddr.b);
2045
2046 hci_table[nb_hcis++] = hci;
2047
2048 return 0;
2049}
2050
2051static void bt_vhci_add(int vlan_id)
2052{
2053 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2054
2055 if (!vlan->slave)
2056 fprintf(stderr, "qemu: warning: adding a VHCI to "
2057 "an empty scatternet %i\n", vlan_id);
2058
2059 bt_vhci_init(bt_new_hci(vlan));
2060}
2061
2062static struct bt_device_s *bt_device_add(const char *opt)
2063{
2064 struct bt_scatternet_s *vlan;
2065 int vlan_id = 0;
2066 char *endp = strstr(opt, ",vlan=");
2067 int len = (endp ? endp - opt : strlen(opt)) + 1;
2068 char devname[10];
2069
2070 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2071
2072 if (endp) {
2073 vlan_id = strtol(endp + 6, &endp, 0);
2074 if (*endp) {
2075 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2076 return 0;
2077 }
2078 }
2079
2080 vlan = qemu_find_bt_vlan(vlan_id);
2081
2082 if (!vlan->slave)
2083 fprintf(stderr, "qemu: warning: adding a slave device to "
2084 "an empty scatternet %i\n", vlan_id);
2085
2086 if (!strcmp(devname, "keyboard"))
2087 return bt_keyboard_init(vlan);
2088
2089 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2090 return 0;
2091}
2092
2093static int bt_parse(const char *opt)
2094{
2095 const char *endp, *p;
2096 int vlan;
2097
2098 if (strstart(opt, "hci", &endp)) {
2099 if (!*endp || *endp == ',') {
2100 if (*endp)
2101 if (!strstart(endp, ",vlan=", 0))
2102 opt = endp + 1;
2103
2104 return bt_hci_parse(opt);
2105 }
2106 } else if (strstart(opt, "vhci", &endp)) {
2107 if (!*endp || *endp == ',') {
2108 if (*endp) {
2109 if (strstart(endp, ",vlan=", &p)) {
2110 vlan = strtol(p, (char **) &endp, 0);
2111 if (*endp) {
2112 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2113 return 1;
2114 }
2115 } else {
2116 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2117 return 1;
2118 }
2119 } else
2120 vlan = 0;
2121
2122 bt_vhci_add(vlan);
2123 return 0;
2124 }
2125 } else if (strstart(opt, "device:", &endp))
2126 return !bt_device_add(endp);
2127
2128 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2129 return 1;
2130}
2131
balrog1ae26a12008-09-28 23:19:47 +00002132/***********************************************************/
2133/* QEMU Block devices */
2134
balrog609497a2008-01-14 02:56:53 +00002135#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00002136#ifdef TARGET_PPC
2137#define CDROM_ALIAS "index=1,media=cdrom"
2138#else
2139#define CDROM_ALIAS "index=2,media=cdrom"
2140#endif
2141#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00002142#define PFLASH_ALIAS "if=pflash"
2143#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00002144#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002145
aliguori7d5aca92009-02-11 15:19:58 +00002146static int drive_opt_get_free_idx(void)
2147{
2148 int index;
2149
2150 for (index = 0; index < MAX_DRIVES; index++)
2151 if (!drives_opt[index].used) {
2152 drives_opt[index].used = 1;
2153 return index;
2154 }
2155
2156 return -1;
2157}
2158
2159static int drive_get_free_idx(void)
2160{
2161 int index;
2162
2163 for (index = 0; index < MAX_DRIVES; index++)
2164 if (!drives_table[index].used) {
2165 drives_table[index].used = 1;
2166 return index;
2167 }
2168
2169 return -1;
2170}
2171
aliguori4d73cd32009-02-11 15:20:46 +00002172int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002173{
2174 va_list ap;
aliguori7d5aca92009-02-11 15:19:58 +00002175 int index = drive_opt_get_free_idx();
thse4bcb142007-12-02 04:51:10 +00002176
aliguori7d5aca92009-02-11 15:19:58 +00002177 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
thse4bcb142007-12-02 04:51:10 +00002178 fprintf(stderr, "qemu: too many drives\n");
aliguori4d73cd32009-02-11 15:20:46 +00002179 return -1;
thse4bcb142007-12-02 04:51:10 +00002180 }
2181
aliguori7d5aca92009-02-11 15:19:58 +00002182 drives_opt[index].file = file;
thse4bcb142007-12-02 04:51:10 +00002183 va_start(ap, fmt);
aliguori7d5aca92009-02-11 15:19:58 +00002184 vsnprintf(drives_opt[index].opt,
balrog609497a2008-01-14 02:56:53 +00002185 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002186 va_end(ap);
2187
aliguori7d5aca92009-02-11 15:19:58 +00002188 nb_drives_opt++;
2189 return index;
thse4bcb142007-12-02 04:51:10 +00002190}
2191
aliguorib01b1112009-02-11 15:20:20 +00002192void drive_remove(int index)
2193{
2194 drives_opt[index].used = 0;
2195 nb_drives_opt--;
2196}
2197
thsf60d39b2007-12-17 03:55:57 +00002198int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002199{
2200 int index;
2201
2202 /* seek interface, bus and unit */
2203
aliguori7d5aca92009-02-11 15:19:58 +00002204 for (index = 0; index < MAX_DRIVES; index++)
thsf60d39b2007-12-17 03:55:57 +00002205 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002206 drives_table[index].bus == bus &&
aliguori7d5aca92009-02-11 15:19:58 +00002207 drives_table[index].unit == unit &&
2208 drives_table[index].used)
thse4bcb142007-12-02 04:51:10 +00002209 return index;
2210
2211 return -1;
2212}
2213
thsf60d39b2007-12-17 03:55:57 +00002214int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002215{
2216 int max_bus;
2217 int index;
2218
2219 max_bus = -1;
2220 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002221 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002222 drives_table[index].bus > max_bus)
2223 max_bus = drives_table[index].bus;
2224 }
2225 return max_bus;
2226}
2227
aliguorifa879c62009-01-07 17:32:33 +00002228const char *drive_get_serial(BlockDriverState *bdrv)
2229{
2230 int index;
2231
2232 for (index = 0; index < nb_drives; index++)
2233 if (drives_table[index].bdrv == bdrv)
2234 return drives_table[index].serial;
2235
2236 return "\0";
2237}
2238
aliguori428c5702009-01-21 18:59:04 +00002239BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2240{
2241 int index;
2242
2243 for (index = 0; index < nb_drives; index++)
2244 if (drives_table[index].bdrv == bdrv)
2245 return drives_table[index].onerror;
2246
aliguoricdad4bd2009-02-28 16:51:01 +00002247 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002248}
2249
aurel32a1620fa2008-04-29 05:58:01 +00002250static void bdrv_format_print(void *opaque, const char *name)
2251{
2252 fprintf(stderr, " %s", name);
2253}
2254
aliguorib01b1112009-02-11 15:20:20 +00002255void drive_uninit(BlockDriverState *bdrv)
2256{
2257 int i;
2258
2259 for (i = 0; i < MAX_DRIVES; i++)
2260 if (drives_table[i].bdrv == bdrv) {
2261 drives_table[i].bdrv = NULL;
2262 drives_table[i].used = 0;
2263 drive_remove(drives_table[i].drive_opt_idx);
2264 nb_drives--;
2265 break;
2266 }
2267}
2268
aliguori4d73cd32009-02-11 15:20:46 +00002269int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
thse4bcb142007-12-02 04:51:10 +00002270{
2271 char buf[128];
2272 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002273 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00002274 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002275 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002276 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002277 enum { MEDIA_DISK, MEDIA_CDROM } media;
2278 int bus_id, unit_id;
2279 int cyls, heads, secs, translation;
2280 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002281 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002282 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002283 int max_devs;
2284 int index;
balrog33f00272007-12-24 14:33:24 +00002285 int cache;
aliguori428c5702009-01-21 18:59:04 +00002286 int bdrv_flags, onerror;
aliguori7d5aca92009-02-11 15:19:58 +00002287 int drives_table_idx;
balrog609497a2008-01-14 02:56:53 +00002288 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002289 static const char * const params[] = { "bus", "unit", "if", "index",
2290 "cyls", "heads", "secs", "trans",
2291 "media", "snapshot", "file",
aliguori428c5702009-01-21 18:59:04 +00002292 "cache", "format", "serial", "werror",
2293 NULL };
thse4bcb142007-12-02 04:51:10 +00002294
2295 if (check_params(buf, sizeof(buf), params, str) < 0) {
balrogff993632008-02-10 13:21:25 +00002296 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
thse4bcb142007-12-02 04:51:10 +00002297 buf, str);
2298 return -1;
2299 }
2300
2301 file[0] = 0;
2302 cyls = heads = secs = 0;
2303 bus_id = 0;
2304 unit_id = -1;
2305 translation = BIOS_ATA_TRANSLATION_AUTO;
2306 index = -1;
aliguori4dc822d2008-12-04 21:39:21 +00002307 cache = 3;
thse4bcb142007-12-02 04:51:10 +00002308
blueswir1c9b1ae22008-09-28 18:55:17 +00002309 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002310 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002311 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002312 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002313 } else {
thsf60d39b2007-12-17 03:55:57 +00002314 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002315 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002316 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002317 }
2318 media = MEDIA_DISK;
2319
2320 /* extract parameters */
2321
2322 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2323 bus_id = strtol(buf, NULL, 0);
2324 if (bus_id < 0) {
2325 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2326 return -1;
2327 }
2328 }
2329
2330 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2331 unit_id = strtol(buf, NULL, 0);
2332 if (unit_id < 0) {
2333 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2334 return -1;
2335 }
2336 }
2337
2338 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002339 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002340 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002341 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002342 max_devs = MAX_IDE_DEVS;
2343 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002344 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002345 max_devs = MAX_SCSI_DEVS;
2346 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002347 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002348 max_devs = 0;
2349 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002350 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002351 max_devs = 0;
2352 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002353 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002354 max_devs = 0;
2355 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002356 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002357 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002358 } else if (!strcmp(buf, "virtio")) {
2359 type = IF_VIRTIO;
2360 max_devs = 0;
2361 } else {
thse4bcb142007-12-02 04:51:10 +00002362 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2363 return -1;
2364 }
2365 }
2366
2367 if (get_param_value(buf, sizeof(buf), "index", str)) {
2368 index = strtol(buf, NULL, 0);
2369 if (index < 0) {
2370 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2371 return -1;
2372 }
2373 }
2374
2375 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2376 cyls = strtol(buf, NULL, 0);
2377 }
2378
2379 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2380 heads = strtol(buf, NULL, 0);
2381 }
2382
2383 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2384 secs = strtol(buf, NULL, 0);
2385 }
2386
2387 if (cyls || heads || secs) {
2388 if (cyls < 1 || cyls > 16383) {
2389 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2390 return -1;
2391 }
2392 if (heads < 1 || heads > 16) {
2393 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2394 return -1;
2395 }
2396 if (secs < 1 || secs > 63) {
2397 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2398 return -1;
2399 }
2400 }
2401
2402 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2403 if (!cyls) {
2404 fprintf(stderr,
2405 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2406 str);
2407 return -1;
2408 }
2409 if (!strcmp(buf, "none"))
2410 translation = BIOS_ATA_TRANSLATION_NONE;
2411 else if (!strcmp(buf, "lba"))
2412 translation = BIOS_ATA_TRANSLATION_LBA;
2413 else if (!strcmp(buf, "auto"))
2414 translation = BIOS_ATA_TRANSLATION_AUTO;
2415 else {
2416 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2417 return -1;
2418 }
2419 }
2420
2421 if (get_param_value(buf, sizeof(buf), "media", str)) {
2422 if (!strcmp(buf, "disk")) {
2423 media = MEDIA_DISK;
2424 } else if (!strcmp(buf, "cdrom")) {
2425 if (cyls || secs || heads) {
2426 fprintf(stderr,
2427 "qemu: '%s' invalid physical CHS format\n", str);
2428 return -1;
2429 }
2430 media = MEDIA_CDROM;
2431 } else {
2432 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2433 return -1;
2434 }
2435 }
2436
2437 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2438 if (!strcmp(buf, "on"))
2439 snapshot = 1;
2440 else if (!strcmp(buf, "off"))
2441 snapshot = 0;
2442 else {
2443 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2444 return -1;
2445 }
2446 }
2447
balrog33f00272007-12-24 14:33:24 +00002448 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002449 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002450 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002451 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002452 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002453 else if (!strcmp(buf, "writeback"))
2454 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002455 else {
2456 fprintf(stderr, "qemu: invalid cache option\n");
2457 return -1;
2458 }
2459 }
2460
aurel321e72d3b2008-04-28 20:26:45 +00002461 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002462 if (strcmp(buf, "?") == 0) {
2463 fprintf(stderr, "qemu: Supported formats:");
2464 bdrv_iterate_format(bdrv_format_print, NULL);
2465 fprintf(stderr, "\n");
2466 return -1;
2467 }
aurel321e72d3b2008-04-28 20:26:45 +00002468 drv = bdrv_find_format(buf);
2469 if (!drv) {
2470 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2471 return -1;
2472 }
2473 }
2474
balrog609497a2008-01-14 02:56:53 +00002475 if (arg->file == NULL)
2476 get_param_value(file, sizeof(file), "file", str);
2477 else
2478 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002479
aliguorifa879c62009-01-07 17:32:33 +00002480 if (!get_param_value(serial, sizeof(serial), "serial", str))
2481 memset(serial, 0, sizeof(serial));
2482
aliguoricdad4bd2009-02-28 16:51:01 +00002483 onerror = BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002484 if (get_param_value(buf, sizeof(serial), "werror", str)) {
aliguori869a5c62009-01-22 19:52:25 +00002485 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002486 fprintf(stderr, "werror is no supported by this format\n");
aliguori428c5702009-01-21 18:59:04 +00002487 return -1;
2488 }
2489 if (!strcmp(buf, "ignore"))
2490 onerror = BLOCK_ERR_IGNORE;
2491 else if (!strcmp(buf, "enospc"))
2492 onerror = BLOCK_ERR_STOP_ENOSPC;
2493 else if (!strcmp(buf, "stop"))
2494 onerror = BLOCK_ERR_STOP_ANY;
2495 else if (!strcmp(buf, "report"))
2496 onerror = BLOCK_ERR_REPORT;
2497 else {
2498 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2499 return -1;
2500 }
2501 }
2502
thse4bcb142007-12-02 04:51:10 +00002503 /* compute bus and unit according index */
2504
2505 if (index != -1) {
2506 if (bus_id != 0 || unit_id != -1) {
2507 fprintf(stderr,
2508 "qemu: '%s' index cannot be used with bus and unit\n", str);
2509 return -1;
2510 }
2511 if (max_devs == 0)
2512 {
2513 unit_id = index;
2514 bus_id = 0;
2515 } else {
2516 unit_id = index % max_devs;
2517 bus_id = index / max_devs;
2518 }
2519 }
2520
2521 /* if user doesn't specify a unit_id,
2522 * try to find the first free
2523 */
2524
2525 if (unit_id == -1) {
2526 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002527 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002528 unit_id++;
2529 if (max_devs && unit_id >= max_devs) {
2530 unit_id -= max_devs;
2531 bus_id++;
2532 }
2533 }
2534 }
2535
2536 /* check unit id */
2537
2538 if (max_devs && unit_id >= max_devs) {
2539 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2540 str, unit_id, max_devs - 1);
2541 return -1;
2542 }
2543
2544 /*
2545 * ignore multiple definitions
2546 */
2547
thsf60d39b2007-12-17 03:55:57 +00002548 if (drive_get_index(type, bus_id, unit_id) != -1)
aliguori4d73cd32009-02-11 15:20:46 +00002549 return -2;
thse4bcb142007-12-02 04:51:10 +00002550
2551 /* init */
2552
thsf60d39b2007-12-17 03:55:57 +00002553 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002554 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002555 if (max_devs)
2556 snprintf(buf, sizeof(buf), "%s%i%s%i",
2557 devname, bus_id, mediastr, unit_id);
2558 else
2559 snprintf(buf, sizeof(buf), "%s%s%i",
2560 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002561 bdrv = bdrv_new(buf);
aliguori7d5aca92009-02-11 15:19:58 +00002562 drives_table_idx = drive_get_free_idx();
2563 drives_table[drives_table_idx].bdrv = bdrv;
2564 drives_table[drives_table_idx].type = type;
2565 drives_table[drives_table_idx].bus = bus_id;
2566 drives_table[drives_table_idx].unit = unit_id;
2567 drives_table[drives_table_idx].onerror = onerror;
aliguorib01b1112009-02-11 15:20:20 +00002568 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
aliguorifa879c62009-01-07 17:32:33 +00002569 strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002570 nb_drives++;
2571
thsf60d39b2007-12-17 03:55:57 +00002572 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002573 case IF_IDE:
2574 case IF_SCSI:
2575 switch(media) {
2576 case MEDIA_DISK:
2577 if (cyls != 0) {
2578 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2579 bdrv_set_translation_hint(bdrv, translation);
2580 }
2581 break;
2582 case MEDIA_CDROM:
2583 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2584 break;
2585 }
2586 break;
2587 case IF_SD:
2588 /* FIXME: This isn't really a floppy, but it's a reasonable
2589 approximation. */
2590 case IF_FLOPPY:
2591 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2592 break;
2593 case IF_PFLASH:
2594 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002595 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002596 break;
2597 }
2598 if (!file[0])
aliguori4d73cd32009-02-11 15:20:46 +00002599 return -2;
balrog33f00272007-12-24 14:33:24 +00002600 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002601 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002602 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002603 cache = 2; /* always use write-back with snapshot */
2604 }
2605 if (cache == 0) /* no caching */
2606 bdrv_flags |= BDRV_O_NOCACHE;
2607 else if (cache == 2) /* write-back */
2608 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori4dc822d2008-12-04 21:39:21 +00002609 else if (cache == 3) /* not specified */
2610 bdrv_flags |= BDRV_O_CACHE_DEF;
aliguoric0f4ce72009-03-05 23:01:01 +00002611 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002612 fprintf(stderr, "qemu: could not open disk image %s\n",
2613 file);
2614 return -1;
2615 }
aliguoric0f4ce72009-03-05 23:01:01 +00002616 if (bdrv_key_required(bdrv))
2617 autostart = 0;
aliguori4d73cd32009-02-11 15:20:46 +00002618 return drives_table_idx;
thse4bcb142007-12-02 04:51:10 +00002619}
2620
bellard330d0412003-07-26 18:11:40 +00002621/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002622/* USB devices */
2623
pbrook0d92ed32006-05-21 16:30:15 +00002624static USBPort *used_usb_ports;
2625static USBPort *free_usb_ports;
2626
2627/* ??? Maybe change this to register a hub to keep track of the topology. */
2628void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2629 usb_attachfn attach)
2630{
2631 port->opaque = opaque;
2632 port->index = index;
2633 port->attach = attach;
2634 port->next = free_usb_ports;
2635 free_usb_ports = port;
2636}
2637
aliguori4b096fc2008-08-21 19:28:55 +00002638int usb_device_add_dev(USBDevice *dev)
2639{
2640 USBPort *port;
2641
2642 /* Find a USB port to add the device to. */
2643 port = free_usb_ports;
2644 if (!port->next) {
2645 USBDevice *hub;
2646
2647 /* Create a new hub and chain it on. */
2648 free_usb_ports = NULL;
2649 port->next = used_usb_ports;
2650 used_usb_ports = port;
2651
2652 hub = usb_hub_init(VM_USB_HUB_SIZE);
2653 usb_attach(port, hub);
2654 port = free_usb_ports;
2655 }
2656
2657 free_usb_ports = port->next;
2658 port->next = used_usb_ports;
2659 used_usb_ports = port;
2660 usb_attach(port, dev);
2661 return 0;
2662}
2663
aliguoric0f4ce72009-03-05 23:01:01 +00002664static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002665{
2666 const char *p;
2667 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002668
pbrook0d92ed32006-05-21 16:30:15 +00002669 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002670 return -1;
2671
2672 if (strstart(devname, "host:", &p)) {
2673 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002674 } else if (!strcmp(devname, "mouse")) {
2675 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002676 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002677 dev = usb_tablet_init();
2678 } else if (!strcmp(devname, "keyboard")) {
2679 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002680 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002681 BlockDriverState *bs;
2682
2683 dev = usb_msd_init(p, &bs);
2684 if (!dev)
2685 return -1;
2686 if (bdrv_key_required(bs)) {
2687 autostart = 0;
2688 if (is_hotplug && monitor_read_bdrv_key(bs) < 0) {
2689 dev->handle_destroy(dev);
2690 return -1;
2691 }
2692 }
balrogf6d2a312007-06-10 19:21:04 +00002693 } else if (!strcmp(devname, "wacom-tablet")) {
2694 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002695 } else if (strstart(devname, "serial:", &p)) {
2696 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002697#ifdef CONFIG_BRLAPI
2698 } else if (!strcmp(devname, "braille")) {
2699 dev = usb_baum_init();
2700#endif
balrog6c9f8862008-07-17 20:47:13 +00002701 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002702 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002703
balrog9ad97e62008-07-29 13:16:31 +00002704 if (net_client_init("nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002705 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002706 nd_table[nic].model = "usb";
2707 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002708 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2709 dev = usb_bt_init(devname[2] ? hci_init(p) :
2710 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002711 } else {
2712 return -1;
2713 }
pbrook0d92ed32006-05-21 16:30:15 +00002714 if (!dev)
2715 return -1;
2716
aliguori4b096fc2008-08-21 19:28:55 +00002717 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002718}
2719
aliguori1f3870a2008-08-21 19:27:48 +00002720int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002721{
pbrook0d92ed32006-05-21 16:30:15 +00002722 USBPort *port;
2723 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002724 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002725
pbrook0d92ed32006-05-21 16:30:15 +00002726 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002727 return -1;
2728
bellarda594cfb2005-11-06 16:13:29 +00002729 if (bus_num != 0)
2730 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002731
2732 lastp = &used_usb_ports;
2733 port = used_usb_ports;
2734 while (port && port->dev->addr != addr) {
2735 lastp = &port->next;
2736 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002737 }
pbrook0d92ed32006-05-21 16:30:15 +00002738
2739 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002740 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002741
bellard059809e2006-07-19 18:06:15 +00002742 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002743 *lastp = port->next;
2744 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002745 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002746 port->next = free_usb_ports;
2747 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002748 return 0;
2749}
2750
aliguori1f3870a2008-08-21 19:27:48 +00002751static int usb_device_del(const char *devname)
2752{
2753 int bus_num, addr;
2754 const char *p;
2755
aliguori5d0c5752008-09-14 01:07:41 +00002756 if (strstart(devname, "host:", &p))
2757 return usb_host_device_close(p);
2758
aliguori1f3870a2008-08-21 19:27:48 +00002759 if (!used_usb_ports)
2760 return -1;
2761
2762 p = strchr(devname, '.');
2763 if (!p)
2764 return -1;
2765 bus_num = strtoul(devname, NULL, 0);
2766 addr = strtoul(p + 1, NULL, 0);
2767
2768 return usb_device_del_addr(bus_num, addr);
2769}
2770
bellarda594cfb2005-11-06 16:13:29 +00002771void do_usb_add(const char *devname)
2772{
aliguoric0f4ce72009-03-05 23:01:01 +00002773 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002774}
2775
2776void do_usb_del(const char *devname)
2777{
aliguori4b096fc2008-08-21 19:28:55 +00002778 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002779}
2780
2781void usb_info(void)
2782{
2783 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002784 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002785 const char *speed_str;
2786
pbrook0d92ed32006-05-21 16:30:15 +00002787 if (!usb_enabled) {
bellarda594cfb2005-11-06 16:13:29 +00002788 term_printf("USB support not enabled\n");
2789 return;
2790 }
2791
pbrook0d92ed32006-05-21 16:30:15 +00002792 for (port = used_usb_ports; port; port = port->next) {
2793 dev = port->dev;
2794 if (!dev)
2795 continue;
2796 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002797 case USB_SPEED_LOW:
2798 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002799 break;
ths5fafdf22007-09-16 21:08:06 +00002800 case USB_SPEED_FULL:
2801 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002802 break;
ths5fafdf22007-09-16 21:08:06 +00002803 case USB_SPEED_HIGH:
2804 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002805 break;
2806 default:
ths5fafdf22007-09-16 21:08:06 +00002807 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002808 break;
bellarda594cfb2005-11-06 16:13:29 +00002809 }
ths5fafdf22007-09-16 21:08:06 +00002810 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
bellard1f6e24e2006-06-26 21:00:51 +00002811 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002812 }
2813}
2814
bellardf7cce892004-12-08 22:21:25 +00002815/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002816/* PCMCIA/Cardbus */
2817
2818static struct pcmcia_socket_entry_s {
2819 struct pcmcia_socket_s *socket;
2820 struct pcmcia_socket_entry_s *next;
2821} *pcmcia_sockets = 0;
2822
2823void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2824{
2825 struct pcmcia_socket_entry_s *entry;
2826
2827 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2828 entry->socket = socket;
2829 entry->next = pcmcia_sockets;
2830 pcmcia_sockets = entry;
2831}
2832
2833void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2834{
2835 struct pcmcia_socket_entry_s *entry, **ptr;
2836
2837 ptr = &pcmcia_sockets;
2838 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2839 if (entry->socket == socket) {
2840 *ptr = entry->next;
2841 qemu_free(entry);
2842 }
2843}
2844
2845void pcmcia_info(void)
2846{
2847 struct pcmcia_socket_entry_s *iter;
2848 if (!pcmcia_sockets)
2849 term_printf("No PCMCIA sockets\n");
2850
2851 for (iter = pcmcia_sockets; iter; iter = iter->next)
2852 term_printf("%s: %s\n", iter->socket->slot_string,
2853 iter->socket->attached ? iter->socket->card_string :
2854 "Empty");
2855}
2856
2857/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002858/* register display */
2859
2860void register_displaystate(DisplayState *ds)
2861{
2862 DisplayState **s;
2863 s = &display_state;
2864 while (*s != NULL)
2865 s = &(*s)->next;
2866 ds->next = NULL;
2867 *s = ds;
2868}
2869
2870DisplayState *get_displaystate(void)
2871{
2872 return display_state;
2873}
2874
ths2ff89792007-06-21 23:34:19 +00002875/* dumb display */
2876
aliguori8f391ab2009-01-19 16:34:10 +00002877static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002878{
aliguori8f391ab2009-01-19 16:34:10 +00002879 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7da03b12009-01-21 18:58:51 +00002880 ds->surface = qemu_create_displaysurface(640, 480, 32, 640 * 4);
aliguori8f391ab2009-01-19 16:34:10 +00002881 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002882}
2883
2884/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002885/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002886
bellardc4b1fcc2004-03-14 21:44:30 +00002887#define MAX_IO_HANDLERS 64
2888
2889typedef struct IOHandlerRecord {
2890 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002891 IOCanRWHandler *fd_read_poll;
2892 IOHandler *fd_read;
2893 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002894 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002895 void *opaque;
2896 /* temporary data */
2897 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002898 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002899} IOHandlerRecord;
2900
bellard8a7ddc32004-03-31 19:00:16 +00002901static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002902
bellard7c9d8e02005-11-15 22:16:05 +00002903/* XXX: fd_read_poll should be suppressed, but an API change is
2904 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002905int qemu_set_fd_handler2(int fd,
2906 IOCanRWHandler *fd_read_poll,
2907 IOHandler *fd_read,
2908 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002909 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002910{
bellard8a7ddc32004-03-31 19:00:16 +00002911 IOHandlerRecord **pioh, *ioh;
2912
bellard7c9d8e02005-11-15 22:16:05 +00002913 if (!fd_read && !fd_write) {
2914 pioh = &first_io_handler;
2915 for(;;) {
2916 ioh = *pioh;
2917 if (ioh == NULL)
2918 break;
2919 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002920 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002921 break;
2922 }
2923 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002924 }
bellard7c9d8e02005-11-15 22:16:05 +00002925 } else {
2926 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2927 if (ioh->fd == fd)
2928 goto found;
2929 }
2930 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002931 ioh->next = first_io_handler;
2932 first_io_handler = ioh;
2933 found:
2934 ioh->fd = fd;
2935 ioh->fd_read_poll = fd_read_poll;
2936 ioh->fd_read = fd_read;
2937 ioh->fd_write = fd_write;
2938 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002939 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002940 }
bellard7c9d8e02005-11-15 22:16:05 +00002941 return 0;
2942}
2943
ths5fafdf22007-09-16 21:08:06 +00002944int qemu_set_fd_handler(int fd,
2945 IOHandler *fd_read,
2946 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002947 void *opaque)
2948{
2949 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002950}
2951
aliguori56f3a5d2008-10-31 18:07:17 +00002952#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002953/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002954/* Polling handling */
2955
2956typedef struct PollingEntry {
2957 PollingFunc *func;
2958 void *opaque;
2959 struct PollingEntry *next;
2960} PollingEntry;
2961
2962static PollingEntry *first_polling_entry;
2963
2964int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2965{
2966 PollingEntry **ppe, *pe;
2967 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002968 pe->func = func;
2969 pe->opaque = opaque;
2970 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2971 *ppe = pe;
2972 return 0;
2973}
2974
2975void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2976{
2977 PollingEntry **ppe, *pe;
2978 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2979 pe = *ppe;
2980 if (pe->func == func && pe->opaque == opaque) {
2981 *ppe = pe->next;
2982 qemu_free(pe);
2983 break;
2984 }
2985 }
2986}
2987
bellarda18e5242006-06-25 17:18:27 +00002988/***********************************************************/
2989/* Wait objects support */
2990typedef struct WaitObjects {
2991 int num;
2992 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2993 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2994 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2995} WaitObjects;
2996
2997static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002998
bellarda18e5242006-06-25 17:18:27 +00002999int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3000{
3001 WaitObjects *w = &wait_objects;
3002
3003 if (w->num >= MAXIMUM_WAIT_OBJECTS)
3004 return -1;
3005 w->events[w->num] = handle;
3006 w->func[w->num] = func;
3007 w->opaque[w->num] = opaque;
3008 w->num++;
3009 return 0;
3010}
3011
3012void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3013{
3014 int i, found;
3015 WaitObjects *w = &wait_objects;
3016
3017 found = 0;
3018 for (i = 0; i < w->num; i++) {
3019 if (w->events[i] == handle)
3020 found = 1;
3021 if (found) {
3022 w->events[i] = w->events[i + 1];
3023 w->func[i] = w->func[i + 1];
3024 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00003025 }
bellarda18e5242006-06-25 17:18:27 +00003026 }
3027 if (found)
3028 w->num--;
3029}
3030#endif
3031
bellard8a7ddc32004-03-31 19:00:16 +00003032/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003033/* ram save/restore */
3034
bellard8a7ddc32004-03-31 19:00:16 +00003035static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3036{
3037 int v;
3038
3039 v = qemu_get_byte(f);
3040 switch(v) {
3041 case 0:
3042 if (qemu_get_buffer(f, buf, len) != len)
3043 return -EIO;
3044 break;
3045 case 1:
3046 v = qemu_get_byte(f);
3047 memset(buf, v, len);
3048 break;
3049 default:
3050 return -EINVAL;
3051 }
aliguori871d2f02008-10-13 03:07:56 +00003052
3053 if (qemu_file_has_error(f))
3054 return -EIO;
3055
bellard8a7ddc32004-03-31 19:00:16 +00003056 return 0;
3057}
3058
bellardc88676f2006-08-06 13:36:11 +00003059static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003060{
aurel3200f82b82008-04-27 21:12:55 +00003061 int ret;
3062 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00003063
bellard8a7ddc32004-03-31 19:00:16 +00003064 if (qemu_get_be32(f) != phys_ram_size)
3065 return -EINVAL;
3066 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
3067 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
3068 if (ret)
3069 return ret;
3070 }
3071 return 0;
3072}
3073
bellardc88676f2006-08-06 13:36:11 +00003074#define BDRV_HASH_BLOCK_SIZE 1024
3075#define IOBUF_SIZE 4096
3076#define RAM_CBLOCK_MAGIC 0xfabe
3077
bellardc88676f2006-08-06 13:36:11 +00003078typedef struct RamDecompressState {
3079 z_stream zstream;
3080 QEMUFile *f;
3081 uint8_t buf[IOBUF_SIZE];
3082} RamDecompressState;
3083
3084static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3085{
3086 int ret;
3087 memset(s, 0, sizeof(*s));
3088 s->f = f;
3089 ret = inflateInit(&s->zstream);
3090 if (ret != Z_OK)
3091 return -1;
3092 return 0;
3093}
3094
3095static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3096{
3097 int ret, clen;
3098
3099 s->zstream.avail_out = len;
3100 s->zstream.next_out = buf;
3101 while (s->zstream.avail_out > 0) {
3102 if (s->zstream.avail_in == 0) {
3103 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3104 return -1;
3105 clen = qemu_get_be16(s->f);
3106 if (clen > IOBUF_SIZE)
3107 return -1;
3108 qemu_get_buffer(s->f, s->buf, clen);
3109 s->zstream.avail_in = clen;
3110 s->zstream.next_in = s->buf;
3111 }
3112 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3113 if (ret != Z_OK && ret != Z_STREAM_END) {
3114 return -1;
3115 }
3116 }
3117 return 0;
3118}
3119
3120static void ram_decompress_close(RamDecompressState *s)
3121{
3122 inflateEnd(&s->zstream);
3123}
3124
aliguori475e4272008-10-06 20:21:51 +00003125#define RAM_SAVE_FLAG_FULL 0x01
3126#define RAM_SAVE_FLAG_COMPRESS 0x02
3127#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3128#define RAM_SAVE_FLAG_PAGE 0x08
3129#define RAM_SAVE_FLAG_EOS 0x10
3130
3131static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003132{
aliguori475e4272008-10-06 20:21:51 +00003133 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3134 uint32_t *array = (uint32_t *)page;
3135 int i;
ths3b46e622007-09-17 08:09:54 +00003136
aliguori475e4272008-10-06 20:21:51 +00003137 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3138 if (array[i] != val)
3139 return 0;
bellardc88676f2006-08-06 13:36:11 +00003140 }
aliguori475e4272008-10-06 20:21:51 +00003141
3142 return 1;
bellardc88676f2006-08-06 13:36:11 +00003143}
3144
aliguori475e4272008-10-06 20:21:51 +00003145static int ram_save_block(QEMUFile *f)
3146{
3147 static ram_addr_t current_addr = 0;
3148 ram_addr_t saved_addr = current_addr;
3149 ram_addr_t addr = 0;
3150 int found = 0;
3151
3152 while (addr < phys_ram_size) {
3153 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3154 uint8_t ch;
3155
3156 cpu_physical_memory_reset_dirty(current_addr,
3157 current_addr + TARGET_PAGE_SIZE,
3158 MIGRATION_DIRTY_FLAG);
3159
3160 ch = *(phys_ram_base + current_addr);
3161
3162 if (is_dup_page(phys_ram_base + current_addr, ch)) {
3163 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3164 qemu_put_byte(f, ch);
3165 } else {
3166 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3167 qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
3168 }
3169
3170 found = 1;
3171 break;
3172 }
3173 addr += TARGET_PAGE_SIZE;
3174 current_addr = (saved_addr + addr) % phys_ram_size;
3175 }
3176
3177 return found;
3178}
3179
3180static ram_addr_t ram_save_threshold = 10;
3181
3182static ram_addr_t ram_save_remaining(void)
3183{
3184 ram_addr_t addr;
3185 ram_addr_t count = 0;
3186
3187 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3188 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3189 count++;
3190 }
3191
3192 return count;
3193}
3194
3195static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3196{
3197 ram_addr_t addr;
3198
3199 if (stage == 1) {
3200 /* Make sure all dirty bits are set */
3201 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3202 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3203 cpu_physical_memory_set_dirty(addr);
3204 }
3205
3206 /* Enable dirty memory tracking */
3207 cpu_physical_memory_set_dirty_tracking(1);
3208
3209 qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
3210 }
3211
3212 while (!qemu_file_rate_limit(f)) {
3213 int ret;
3214
3215 ret = ram_save_block(f);
3216 if (ret == 0) /* no more blocks */
3217 break;
3218 }
3219
3220 /* try transferring iterative blocks of memory */
3221
3222 if (stage == 3) {
3223 cpu_physical_memory_set_dirty_tracking(0);
3224
3225 /* flush all remaining blocks regardless of rate limiting */
3226 while (ram_save_block(f) != 0);
3227 }
3228
3229 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3230
3231 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3232}
3233
3234static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003235{
3236 RamDecompressState s1, *s = &s1;
3237 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003238 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003239
bellardc88676f2006-08-06 13:36:11 +00003240 if (ram_decompress_open(s, f) < 0)
3241 return -EINVAL;
3242 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
3243 if (ram_decompress_buf(s, buf, 1) < 0) {
3244 fprintf(stderr, "Error while reading ram block header\n");
3245 goto error;
3246 }
3247 if (buf[0] == 0) {
3248 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003249 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003250 goto error;
3251 }
aliguori475e4272008-10-06 20:21:51 +00003252 } else {
bellardc88676f2006-08-06 13:36:11 +00003253 error:
3254 printf("Error block header\n");
3255 return -EINVAL;
3256 }
3257 }
3258 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003259
3260 return 0;
3261}
3262
3263static int ram_load(QEMUFile *f, void *opaque, int version_id)
3264{
3265 ram_addr_t addr;
3266 int flags;
3267
3268 if (version_id == 1)
3269 return ram_load_v1(f, opaque);
3270
3271 if (version_id == 2) {
3272 if (qemu_get_be32(f) != phys_ram_size)
3273 return -EINVAL;
3274 return ram_load_dead(f, opaque);
3275 }
3276
3277 if (version_id != 3)
3278 return -EINVAL;
3279
3280 do {
3281 addr = qemu_get_be64(f);
3282
3283 flags = addr & ~TARGET_PAGE_MASK;
3284 addr &= TARGET_PAGE_MASK;
3285
3286 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3287 if (addr != phys_ram_size)
3288 return -EINVAL;
3289 }
3290
3291 if (flags & RAM_SAVE_FLAG_FULL) {
3292 if (ram_load_dead(f, opaque) < 0)
3293 return -EINVAL;
3294 }
3295
3296 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3297 uint8_t ch = qemu_get_byte(f);
3298 memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
3299 } else if (flags & RAM_SAVE_FLAG_PAGE)
3300 qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
3301 } while (!(flags & RAM_SAVE_FLAG_EOS));
3302
bellardc88676f2006-08-06 13:36:11 +00003303 return 0;
3304}
3305
aliguori9e472e12008-10-08 19:50:24 +00003306void qemu_service_io(void)
3307{
3308 CPUState *env = cpu_single_env;
3309 if (env) {
3310 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3311#ifdef USE_KQEMU
3312 if (env->kqemu_enabled) {
3313 kqemu_cpu_interrupt(env);
3314 }
3315#endif
3316 }
3317}
3318
bellard8a7ddc32004-03-31 19:00:16 +00003319/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003320/* bottom halves (can be seen as timers which expire ASAP) */
3321
3322struct QEMUBH {
3323 QEMUBHFunc *cb;
3324 void *opaque;
3325 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003326 int idle;
3327 int deleted;
bellard83f64092006-08-01 16:21:11 +00003328 QEMUBH *next;
3329};
3330
3331static QEMUBH *first_bh = NULL;
3332
3333QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3334{
3335 QEMUBH *bh;
3336 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003337 bh->cb = cb;
3338 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003339 bh->next = first_bh;
3340 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003341 return bh;
3342}
3343
bellard6eb57332006-08-06 09:51:25 +00003344int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003345{
aliguori1b435b12008-10-31 17:24:21 +00003346 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003347 int ret;
bellard83f64092006-08-01 16:21:11 +00003348
bellard6eb57332006-08-06 09:51:25 +00003349 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003350 for (bh = first_bh; bh; bh = bh->next) {
3351 if (!bh->deleted && bh->scheduled) {
3352 bh->scheduled = 0;
3353 if (!bh->idle)
3354 ret = 1;
3355 bh->idle = 0;
3356 bh->cb(bh->opaque);
3357 }
bellard83f64092006-08-01 16:21:11 +00003358 }
aliguori1b435b12008-10-31 17:24:21 +00003359
3360 /* remove deleted bhs */
3361 bhp = &first_bh;
3362 while (*bhp) {
3363 bh = *bhp;
3364 if (bh->deleted) {
3365 *bhp = bh->next;
3366 qemu_free(bh);
3367 } else
3368 bhp = &bh->next;
3369 }
3370
bellard6eb57332006-08-06 09:51:25 +00003371 return ret;
bellard83f64092006-08-01 16:21:11 +00003372}
3373
aliguori1b435b12008-10-31 17:24:21 +00003374void qemu_bh_schedule_idle(QEMUBH *bh)
3375{
3376 if (bh->scheduled)
3377 return;
3378 bh->scheduled = 1;
3379 bh->idle = 1;
3380}
3381
bellard83f64092006-08-01 16:21:11 +00003382void qemu_bh_schedule(QEMUBH *bh)
3383{
3384 CPUState *env = cpu_single_env;
3385 if (bh->scheduled)
3386 return;
3387 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003388 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003389 /* stop the currently executing CPU to execute the BH ASAP */
3390 if (env) {
3391 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3392 }
3393}
3394
3395void qemu_bh_cancel(QEMUBH *bh)
3396{
aliguori1b435b12008-10-31 17:24:21 +00003397 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003398}
3399
3400void qemu_bh_delete(QEMUBH *bh)
3401{
aliguori1b435b12008-10-31 17:24:21 +00003402 bh->scheduled = 0;
3403 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003404}
3405
aliguori56f3a5d2008-10-31 18:07:17 +00003406static void qemu_bh_update_timeout(int *timeout)
3407{
3408 QEMUBH *bh;
3409
3410 for (bh = first_bh; bh; bh = bh->next) {
3411 if (!bh->deleted && bh->scheduled) {
3412 if (bh->idle) {
3413 /* idle bottom halves will be polled at least
3414 * every 10ms */
3415 *timeout = MIN(10, *timeout);
3416 } else {
3417 /* non-idle bottom halves will be executed
3418 * immediately */
3419 *timeout = 0;
3420 break;
3421 }
3422 }
3423 }
3424}
3425
bellard83f64092006-08-01 16:21:11 +00003426/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003427/* machine registration */
3428
blueswir1bdaf78e2008-10-04 07:24:27 +00003429static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003430QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003431
3432int qemu_register_machine(QEMUMachine *m)
3433{
3434 QEMUMachine **pm;
3435 pm = &first_machine;
3436 while (*pm != NULL)
3437 pm = &(*pm)->next;
3438 m->next = NULL;
3439 *pm = m;
3440 return 0;
3441}
3442
pbrook9596ebb2007-11-18 01:44:38 +00003443static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003444{
3445 QEMUMachine *m;
3446
3447 for(m = first_machine; m != NULL; m = m->next) {
3448 if (!strcmp(m->name, name))
3449 return m;
3450 }
3451 return NULL;
3452}
3453
3454/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003455/* main execution loop */
3456
pbrook9596ebb2007-11-18 01:44:38 +00003457static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003458{
aliguori7d957bd2009-01-15 22:14:11 +00003459 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003460 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003461 DisplayChangeListener *dcl = ds->listeners;
3462
3463 dpy_refresh(ds);
3464
3465 while (dcl != NULL) {
3466 if (dcl->gui_timer_interval &&
3467 dcl->gui_timer_interval < interval)
3468 interval = dcl->gui_timer_interval;
3469 dcl = dcl->next;
3470 }
3471 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003472}
3473
blueswir19043b622009-01-21 19:28:13 +00003474static void nographic_update(void *opaque)
3475{
3476 uint64_t interval = GUI_REFRESH_INTERVAL;
3477
3478 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3479}
3480
bellard0bd48852005-11-11 00:00:47 +00003481struct vm_change_state_entry {
3482 VMChangeStateHandler *cb;
3483 void *opaque;
3484 LIST_ENTRY (vm_change_state_entry) entries;
3485};
3486
3487static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3488
3489VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3490 void *opaque)
3491{
3492 VMChangeStateEntry *e;
3493
3494 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003495
3496 e->cb = cb;
3497 e->opaque = opaque;
3498 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3499 return e;
3500}
3501
3502void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3503{
3504 LIST_REMOVE (e, entries);
3505 qemu_free (e);
3506}
3507
aliguori9781e042009-01-22 17:15:29 +00003508static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003509{
3510 VMChangeStateEntry *e;
3511
3512 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003513 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003514 }
3515}
3516
bellard8a7ddc32004-03-31 19:00:16 +00003517void vm_start(void)
3518{
3519 if (!vm_running) {
3520 cpu_enable_ticks();
3521 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003522 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003523 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00003524 }
3525}
3526
ths5fafdf22007-09-16 21:08:06 +00003527void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00003528{
3529 if (vm_running) {
3530 cpu_disable_ticks();
3531 vm_running = 0;
aliguori9781e042009-01-22 17:15:29 +00003532 vm_state_notify(0, reason);
bellard8a7ddc32004-03-31 19:00:16 +00003533 }
3534}
3535
bellardbb0c6722004-06-20 12:37:32 +00003536/* reset/shutdown handler */
3537
3538typedef struct QEMUResetEntry {
3539 QEMUResetHandler *func;
3540 void *opaque;
3541 struct QEMUResetEntry *next;
3542} QEMUResetEntry;
3543
3544static QEMUResetEntry *first_reset_entry;
3545static int reset_requested;
3546static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003547static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00003548
aurel32cf7a2fe2008-03-18 06:53:05 +00003549int qemu_shutdown_requested(void)
3550{
3551 int r = shutdown_requested;
3552 shutdown_requested = 0;
3553 return r;
3554}
3555
3556int qemu_reset_requested(void)
3557{
3558 int r = reset_requested;
3559 reset_requested = 0;
3560 return r;
3561}
3562
3563int qemu_powerdown_requested(void)
3564{
3565 int r = powerdown_requested;
3566 powerdown_requested = 0;
3567 return r;
3568}
3569
bellardbb0c6722004-06-20 12:37:32 +00003570void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3571{
3572 QEMUResetEntry **pre, *re;
3573
3574 pre = &first_reset_entry;
3575 while (*pre != NULL)
3576 pre = &(*pre)->next;
3577 re = qemu_mallocz(sizeof(QEMUResetEntry));
3578 re->func = func;
3579 re->opaque = opaque;
3580 re->next = NULL;
3581 *pre = re;
3582}
3583
aurel32cf7a2fe2008-03-18 06:53:05 +00003584void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00003585{
3586 QEMUResetEntry *re;
3587
3588 /* reset all devices */
3589 for(re = first_reset_entry; re != NULL; re = re->next) {
3590 re->func(re->opaque);
3591 }
3592}
3593
3594void qemu_system_reset_request(void)
3595{
bellardd1beab82006-10-02 19:44:22 +00003596 if (no_reboot) {
3597 shutdown_requested = 1;
3598 } else {
3599 reset_requested = 1;
3600 }
bellard6a00d602005-11-21 23:25:50 +00003601 if (cpu_single_env)
3602 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003603}
3604
3605void qemu_system_shutdown_request(void)
3606{
3607 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003608 if (cpu_single_env)
3609 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003610}
3611
bellard34751872005-07-02 14:31:34 +00003612void qemu_system_powerdown_request(void)
3613{
3614 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003615 if (cpu_single_env)
3616 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003617}
3618
ths877cf882007-04-18 18:11:47 +00003619#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003620static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003621{
3622 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003623 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003624
bellardf3311102006-04-12 20:21:17 +00003625
3626 /* XXX: need to suppress polling by better using win32 events */
3627 ret = 0;
3628 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3629 ret |= pe->func(pe->opaque);
3630 }
thse6b1e552007-04-18 17:56:02 +00003631 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003632 int err;
3633 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003634
aliguori56f3a5d2008-10-31 18:07:17 +00003635 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003636 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3637 if (w->func[ret - WAIT_OBJECT_0])
3638 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003639
ths5fafdf22007-09-16 21:08:06 +00003640 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003641 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003642
thse6b1e552007-04-18 17:56:02 +00003643 /* Check if event is signaled */
3644 ret2 = WaitForSingleObject(w->events[i], 0);
3645 if(ret2 == WAIT_OBJECT_0) {
3646 if (w->func[i])
3647 w->func[i](w->opaque[i]);
3648 } else if (ret2 == WAIT_TIMEOUT) {
3649 } else {
3650 err = GetLastError();
3651 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003652 }
3653 }
bellarda18e5242006-06-25 17:18:27 +00003654 } else if (ret == WAIT_TIMEOUT) {
3655 } else {
3656 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003657 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003658 }
bellardf3311102006-04-12 20:21:17 +00003659 }
aliguori56f3a5d2008-10-31 18:07:17 +00003660
3661 *timeout = 0;
3662}
3663#else
blueswir169d64512008-12-07 19:30:18 +00003664static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003665{
3666}
bellardfd1dff42006-02-01 21:29:26 +00003667#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003668
3669void main_loop_wait(int timeout)
3670{
3671 IOHandlerRecord *ioh;
3672 fd_set rfds, wfds, xfds;
3673 int ret, nfds;
3674 struct timeval tv;
3675
3676 qemu_bh_update_timeout(&timeout);
3677
3678 host_main_loop_wait(&timeout);
3679
bellardfd1dff42006-02-01 21:29:26 +00003680 /* poll any events */
3681 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003682 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003683 FD_ZERO(&rfds);
3684 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003685 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003686 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003687 if (ioh->deleted)
3688 continue;
bellardfd1dff42006-02-01 21:29:26 +00003689 if (ioh->fd_read &&
3690 (!ioh->fd_read_poll ||
3691 ioh->fd_read_poll(ioh->opaque) != 0)) {
3692 FD_SET(ioh->fd, &rfds);
3693 if (ioh->fd > nfds)
3694 nfds = ioh->fd;
3695 }
3696 if (ioh->fd_write) {
3697 FD_SET(ioh->fd, &wfds);
3698 if (ioh->fd > nfds)
3699 nfds = ioh->fd;
3700 }
3701 }
ths3b46e622007-09-17 08:09:54 +00003702
aliguori56f3a5d2008-10-31 18:07:17 +00003703 tv.tv_sec = timeout / 1000;
3704 tv.tv_usec = (timeout % 1000) * 1000;
3705
bellarde0356492006-05-01 13:33:02 +00003706#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003707 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003708 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3709 }
3710#endif
3711 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00003712 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003713 IOHandlerRecord **pioh;
3714
3715 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003716 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003717 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003718 }
ths6ab43fd2007-08-25 01:34:19 +00003719 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003720 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003721 }
3722 }
thscafffd42007-02-28 21:59:44 +00003723
3724 /* remove deleted IO handlers */
3725 pioh = &first_io_handler;
3726 while (*pioh) {
3727 ioh = *pioh;
3728 if (ioh->deleted) {
3729 *pioh = ioh->next;
3730 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003731 } else
thscafffd42007-02-28 21:59:44 +00003732 pioh = &ioh->next;
3733 }
bellardfd1dff42006-02-01 21:29:26 +00003734 }
bellarde0356492006-05-01 13:33:02 +00003735#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003736 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003737 if (ret < 0) {
3738 FD_ZERO(&rfds);
3739 FD_ZERO(&wfds);
3740 FD_ZERO(&xfds);
3741 }
3742 slirp_select_poll(&rfds, &wfds, &xfds);
3743 }
3744#endif
bellardc20709a2004-04-21 23:27:19 +00003745
aliguori357c6922008-11-25 17:26:09 +00003746 /* vm time timers */
3747 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3748 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3749 qemu_get_clock(vm_clock));
3750
3751 /* real time timers */
3752 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3753 qemu_get_clock(rt_clock));
3754
pbrook423f0742007-05-23 00:06:54 +00003755 /* Check bottom-halves last in case any of the earlier events triggered
3756 them. */
3757 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003758
bellard5905b2e2004-08-01 21:53:26 +00003759}
3760
pbrook9596ebb2007-11-18 01:44:38 +00003761static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00003762{
3763 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00003764#ifdef CONFIG_PROFILER
3765 int64_t ti;
3766#endif
bellard6a00d602005-11-21 23:25:50 +00003767 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003768
bellard6a00d602005-11-21 23:25:50 +00003769 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00003770 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00003771 for(;;) {
3772 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00003773
bellard15a76442005-11-23 21:01:03 +00003774 for(;;) {
3775 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00003776 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00003777#ifdef CONFIG_PROFILER
3778 ti = profile_getclock();
3779#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003780 if (use_icount) {
3781 int64_t count;
3782 int decr;
3783 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3784 env->icount_decr.u16.low = 0;
3785 env->icount_extra = 0;
3786 count = qemu_next_deadline();
3787 count = (count + (1 << icount_time_shift) - 1)
3788 >> icount_time_shift;
3789 qemu_icount += count;
3790 decr = (count > 0xffff) ? 0xffff : count;
3791 count -= decr;
3792 env->icount_decr.u16.low = decr;
3793 env->icount_extra = count;
3794 }
bellard6a00d602005-11-21 23:25:50 +00003795 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00003796#ifdef CONFIG_PROFILER
3797 qemu_time += profile_getclock() - ti;
3798#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003799 if (use_icount) {
3800 /* Fold pending instructions back into the
3801 instruction counter, and clear the interrupt flag. */
3802 qemu_icount -= (env->icount_decr.u16.low
3803 + env->icount_extra);
3804 env->icount_decr.u32 = 0;
3805 env->icount_extra = 0;
3806 }
balrogee5605e2007-12-03 03:01:40 +00003807 next_cpu = env->next_cpu ?: first_cpu;
aurel3295b01002008-04-04 17:16:35 +00003808 if (event_pending && likely(ret != EXCP_DEBUG)) {
balrogee5605e2007-12-03 03:01:40 +00003809 ret = EXCP_INTERRUPT;
3810 event_pending = 0;
3811 break;
3812 }
pbrookbd967e02007-03-11 18:54:57 +00003813 if (ret == EXCP_HLT) {
3814 /* Give the next CPU a chance to run. */
3815 cur_cpu = env;
3816 continue;
3817 }
bellard15a76442005-11-23 21:01:03 +00003818 if (ret != EXCP_HALTED)
3819 break;
3820 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00003821 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00003822 break;
bellard15a76442005-11-23 21:01:03 +00003823 }
3824 cur_cpu = env;
3825
bellard5905b2e2004-08-01 21:53:26 +00003826 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00003827 ret = EXCP_INTERRUPT;
aurel32b2f76162008-04-11 21:35:52 +00003828 if (no_shutdown) {
3829 vm_stop(0);
3830 no_shutdown = 0;
3831 }
3832 else
3833 break;
bellard5905b2e2004-08-01 21:53:26 +00003834 }
3835 if (reset_requested) {
3836 reset_requested = 0;
3837 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00003838 ret = EXCP_INTERRUPT;
3839 }
3840 if (powerdown_requested) {
3841 powerdown_requested = 0;
3842 qemu_system_powerdown();
3843 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00003844 }
aurel3295b01002008-04-04 17:16:35 +00003845 if (unlikely(ret == EXCP_DEBUG)) {
aliguori880a7572008-11-18 20:30:24 +00003846 gdb_set_stop_cpu(cur_cpu);
bellard5905b2e2004-08-01 21:53:26 +00003847 vm_stop(EXCP_DEBUG);
3848 }
pbrookbd967e02007-03-11 18:54:57 +00003849 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00003850 /* XXX: use timeout computed from timers */
pbrook2e70f6e2008-06-29 01:03:05 +00003851 if (ret == EXCP_HALTED) {
3852 if (use_icount) {
3853 int64_t add;
3854 int64_t delta;
3855 /* Advance virtual time to the next event. */
3856 if (use_icount == 1) {
3857 /* When not using an adaptive execution frequency
3858 we tend to get badly out of sync with real time,
thsbf20dc02008-06-30 17:22:19 +00003859 so just delay for a reasonable amount of time. */
pbrook2e70f6e2008-06-29 01:03:05 +00003860 delta = 0;
3861 } else {
3862 delta = cpu_get_icount() - cpu_get_clock();
3863 }
3864 if (delta > 0) {
3865 /* If virtual time is ahead of real time then just
3866 wait for IO. */
3867 timeout = (delta / 1000000) + 1;
3868 } else {
3869 /* Wait for either IO to occur or the next
3870 timer event. */
3871 add = qemu_next_deadline();
3872 /* We advance the timer before checking for IO.
3873 Limit the amount we advance so that early IO
3874 activity won't get the guest too far ahead. */
3875 if (add > 10000000)
3876 add = 10000000;
3877 delta += add;
3878 add = (add + (1 << icount_time_shift) - 1)
3879 >> icount_time_shift;
3880 qemu_icount += add;
3881 timeout = delta / 1000000;
3882 if (timeout < 0)
3883 timeout = 0;
3884 }
3885 } else {
aliguori0a1af392008-10-31 18:40:25 +00003886 timeout = 5000;
pbrook2e70f6e2008-06-29 01:03:05 +00003887 }
3888 } else {
bellard5905b2e2004-08-01 21:53:26 +00003889 timeout = 0;
pbrook2e70f6e2008-06-29 01:03:05 +00003890 }
bellard5905b2e2004-08-01 21:53:26 +00003891 } else {
blueswir198448f52008-09-30 18:16:09 +00003892 if (shutdown_requested) {
3893 ret = EXCP_INTERRUPT;
aliguori5b08fc12008-08-21 20:08:03 +00003894 break;
blueswir198448f52008-09-30 18:16:09 +00003895 }
aliguori0a1af392008-10-31 18:40:25 +00003896 timeout = 5000;
bellard5905b2e2004-08-01 21:53:26 +00003897 }
bellard89bfc102006-02-08 22:46:31 +00003898#ifdef CONFIG_PROFILER
3899 ti = profile_getclock();
3900#endif
bellard5905b2e2004-08-01 21:53:26 +00003901 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00003902#ifdef CONFIG_PROFILER
3903 dev_time += profile_getclock() - ti;
3904#endif
bellardb4608c02003-06-27 17:34:32 +00003905 }
bellard34865132003-10-05 14:28:56 +00003906 cpu_disable_ticks();
3907 return ret;
bellardb4608c02003-06-27 17:34:32 +00003908}
3909
ths15f82202007-06-29 23:26:08 +00003910static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00003911{
blueswir1d2c639d2009-01-24 18:19:25 +00003912 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3913 and qemu-doc.texi */
bellard68d0f702008-01-06 17:21:48 +00003914 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00003915 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00003916 "\n"
bellarda20dd502003-09-30 21:07:02 +00003917 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00003918 "\n"
bellarda20dd502003-09-30 21:07:02 +00003919 "Standard options:\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003920 "-h or -help display this help and exit\n"
bellardcc1daa42005-06-05 14:49:17 +00003921 "-M machine select emulated machine (-M ? for list)\n"
pbrook5adb4832007-03-08 03:15:18 +00003922 "-cpu cpu select CPU (-cpu ? for list)\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003923 "-smp n set the number of CPUs to 'n' [default=1]\n"
bellardc45886d2004-01-05 00:02:06 +00003924 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00003925 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3926 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003927 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
aurel32a1620fa2008-04-29 05:58:01 +00003928 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3929 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
aliguorifa879c62009-01-07 17:32:33 +00003930 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
thse4bcb142007-12-02 04:51:10 +00003931 " use 'file' as a drive image\n"
balrog3e3d5812007-04-30 02:09:25 +00003932 "-mtdblock file use 'file' as on-board Flash memory image\n"
pbrooka1bb27b2007-04-06 16:49:48 +00003933 "-sd file use 'file' as SecureDigital card image\n"
j_mayer86f55662007-04-24 06:52:59 +00003934 "-pflash file use 'file' as a parallel flash image\n"
thseec85c22007-01-05 17:41:07 +00003935 "-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 +00003936 "-snapshot write to temporary files instead of disk image files\n"
bellarda00bad72004-05-22 21:39:06 +00003937 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellard4ca00742004-12-12 22:20:04 +00003938#ifndef _WIN32
ths667acca2006-12-11 02:08:05 +00003939 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00003940#endif
bellard1d14ffa2005-10-30 18:58:22 +00003941#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00003942 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00003943 "-soundhw c1,... enable audio support\n"
3944 " and only specified sound cards (comma separated list)\n"
3945 " use -soundhw ? to get the list of supported cards\n"
bellard6a36d842005-12-18 20:34:32 +00003946 " use -soundhw all to enable all of them\n"
bellard1d14ffa2005-10-30 18:58:22 +00003947#endif
bellardb389dbf2005-11-06 16:49:55 +00003948 "-usb enable the USB driver (will be the default soon)\n"
3949 "-usbdevice name add the host or guest USB device 'name'\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003950 "-name string set the name of the guest\n"
3951 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
3952 " specify machine UUID\n"
3953 "\n"
3954 "Display options:\n"
3955 "-nographic disable graphical output and redirect serial I/Os to console\n"
3956#ifdef CONFIG_CURSES
3957 "-curses use a curses/ncurses interface instead of SDL\n"
3958#endif
3959#ifdef CONFIG_SDL
3960 "-no-frame open SDL window without a frame and window decorations\n"
3961 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3962 "-no-quit disable SDL window close capability\n"
3963 "-sdl enable SDL\n"
3964#endif
3965 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3966 "-vga [std|cirrus|vmware|none]\n"
3967 " select video card type\n"
3968 "-full-screen start in full screen\n"
bellard6f7e9ae2005-03-13 09:43:36 +00003969#if defined(TARGET_PPC) || defined(TARGET_SPARC)
3970 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00003971#endif
blueswir1d2c639d2009-01-24 18:19:25 +00003972 "-vnc display start a VNC server on display\n"
bellarda20dd502003-09-30 21:07:02 +00003973 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003974 "Network options:\n"
aliguori7a9f6e42009-01-07 17:48:51 +00003975 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
bellard7c9d8e02005-11-15 22:16:05 +00003976 " create a new Network Interface Card and connect it to VLAN 'n'\n"
bellardc20709a2004-04-21 23:27:19 +00003977#ifdef CONFIG_SLIRP
aliguori7a9f6e42009-01-07 17:48:51 +00003978 "-net user[,vlan=n][,name=str][,hostname=host]\n"
pbrook115defd2006-04-16 11:06:58 +00003979 " connect the user mode network stack to VLAN 'n' and send\n"
3980 " hostname 'host' to DHCP clients\n"
bellard7c9d8e02005-11-15 22:16:05 +00003981#endif
bellard7fb843f2006-02-01 23:06:55 +00003982#ifdef _WIN32
aliguori7a9f6e42009-01-07 17:48:51 +00003983 "-net tap[,vlan=n][,name=str],ifname=name\n"
bellard7fb843f2006-02-01 23:06:55 +00003984 " connect the host TAP network interface to VLAN 'n'\n"
3985#else
aliguori7a9f6e42009-01-07 17:48:51 +00003986 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
thsb46a8902007-10-21 23:20:45 +00003987 " connect the host TAP network interface to VLAN 'n' and use the\n"
3988 " network scripts 'file' (default=%s)\n"
3989 " and 'dfile' (default=%s);\n"
3990 " use '[down]script=no' to disable script execution;\n"
bellard7c9d8e02005-11-15 22:16:05 +00003991 " use 'fd=h' to connect to an already opened TAP interface\n"
bellard7fb843f2006-02-01 23:06:55 +00003992#endif
aliguori7a9f6e42009-01-07 17:48:51 +00003993 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
bellard7c9d8e02005-11-15 22:16:05 +00003994 " connect the vlan 'n' to another VLAN using a socket connection\n"
aliguori7a9f6e42009-01-07 17:48:51 +00003995 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
bellard3d830452005-12-18 16:36:49 +00003996 " connect the vlan 'n' to multicast maddr and port\n"
ths8a16d272008-07-19 09:56:24 +00003997#ifdef CONFIG_VDE
aliguori7a9f6e42009-01-07 17:48:51 +00003998 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
ths8a16d272008-07-19 09:56:24 +00003999 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4000 " on host and listening for incoming connections on 'socketpath'.\n"
4001 " Use group 'groupname' and mode 'octalmode' to change default\n"
4002 " ownership and permissions for communication port.\n"
4003#endif
bellard7c9d8e02005-11-15 22:16:05 +00004004 "-net none use it alone to have zero network devices; if no -net option\n"
4005 " is provided, the default is '-net nic -net user'\n"
bellard7c9d8e02005-11-15 22:16:05 +00004006#ifdef CONFIG_SLIRP
ths0db11372007-02-20 00:12:07 +00004007 "-tftp dir allow tftp access to files in dir [-net user]\n"
ths47d5d012007-02-20 00:05:08 +00004008 "-bootp file advertise file in BOOTP replies\n"
bellard7c9d8e02005-11-15 22:16:05 +00004009#ifndef _WIN32
4010 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
bellardc94c8d62004-09-13 21:37:34 +00004011#endif
bellard9bf05442004-08-25 22:12:49 +00004012 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
bellard7c9d8e02005-11-15 22:16:05 +00004013 " redirect TCP or UDP connections from host to guest [-net user]\n"
bellardc20709a2004-04-21 23:27:19 +00004014#endif
bellardc4b1fcc2004-03-14 21:44:30 +00004015 "\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004016 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
4017 "-bt hci,host[:id]\n"
4018 " use host's HCI with the given name\n"
4019 "-bt hci[,vlan=n]\n"
4020 " emulate a standard HCI in virtual scatternet 'n'\n"
4021 "-bt vhci[,vlan=n]\n"
4022 " add host computer to virtual scatternet 'n' using VHCI\n"
4023 "-bt device:dev[,vlan=n]\n"
4024 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4025 "\n"
4026#ifdef TARGET_I386
4027 "\n"
4028 "i386 target only:\n"
4029 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4030 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4031 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4032 "-no-acpi disable ACPI\n"
4033 "-no-hpet disable HPET\n"
aliguori8a92ea22009-02-27 20:12:36 +00004034 "-acpitable [sig=str][,rev=n][,oem_id=str][,oem_table_id=str][,oem_rev=n][,asl_compiler_id=str][,asl_compiler_rev=n][,data=file1[:file2]...]\n"
4035 " ACPI table description\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004036#endif
bellardc4b1fcc2004-03-14 21:44:30 +00004037 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00004038 "-kernel bzImage use 'bzImage' as kernel image\n"
4039 "-append cmdline use 'cmdline' as kernel command line\n"
4040 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00004041 "\n"
bellard330d0412003-07-26 18:11:40 +00004042 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00004043 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00004044 "-parallel dev redirect the parallel port to char device 'dev'\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004045 "-monitor dev redirect the monitor to char device 'dev'\n"
4046 "-pidfile file write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00004047 "-S freeze CPU at startup (use 'c' to start execution)\n"
pbrookcfc34752007-02-22 01:48:01 +00004048 "-s wait gdb connection to port\n"
4049 "-p port set gdb connection port [default=%s]\n"
bellardf193c792004-03-21 17:06:25 +00004050 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004051 "-hdachs c,h,s[,t]\n"
4052 " force hard disk 0 physical geometry and the optional BIOS\n"
bellard46d47672004-11-16 01:45:27 +00004053 " translation (t=none or lba) (usually qemu can guess them)\n"
bellard87b47352006-08-17 17:22:54 +00004054 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004055 "-bios file set the filename for the BIOS\n"
bellardd993e022005-02-10 22:00:06 +00004056#ifdef USE_KQEMU
bellard6515b202006-05-03 22:02:44 +00004057 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
bellardd993e022005-02-10 22:00:06 +00004058 "-no-kqemu disable KQEMU kernel module usage\n"
4059#endif
aliguori7ba1e612008-11-05 16:04:33 +00004060#ifdef CONFIG_KVM
4061 "-enable-kvm enable KVM full virtualization support\n"
4062#endif
bellardd1beab82006-10-02 19:44:22 +00004063 "-no-reboot exit instead of rebooting\n"
aurel32b2f76162008-04-11 21:35:52 +00004064 "-no-shutdown stop before shutdown\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004065 "-loadvm [tag|id]\n"
4066 " start right away with a saved state (loadvm in monitor)\n"
ths71e3ceb2006-12-22 02:11:31 +00004067#ifndef _WIN32
4068 "-daemonize daemonize QEMU after initializing\n"
4069#endif
ths9ae02552007-01-05 17:39:04 +00004070 "-option-rom rom load a file, rom, into the option ROM space\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004071#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4072 "-prom-env variable=value\n"
4073 " set OpenBIOS nvram variables\n"
blueswir166508602007-05-01 14:16:52 +00004074#endif
thsf3dcfad2007-08-24 01:26:02 +00004075 "-clock force the use of the given methods for timer alarm.\n"
aurel323adda042008-03-09 23:43:49 +00004076 " To see what timers are available use -clock ?\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004077 "-localtime set the real time clock to local time [default=utc]\n"
bellardbce61842008-02-01 22:18:51 +00004078 "-startdate select initial date of the clock\n"
pbrook2e70f6e2008-06-29 01:03:05 +00004079 "-icount [N|auto]\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004080 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4081 "-echr chr set terminal escape character instead of ctrl-a\n"
4082 "-virtioconsole c\n"
4083 " set virtio console\n"
4084 "-show-cursor show cursor\n"
4085#if defined(TARGET_ARM) || defined(TARGET_M68K)
4086 "-semihosting semihosting mode\n"
4087#endif
4088#if defined(TARGET_ARM)
4089 "-old-param old param mode\n"
4090#endif
4091 "-tb-size n set TB size\n"
4092 "-incoming p prepare for incoming migration, listen on port p\n"
aliguori08585322009-02-27 22:09:45 +00004093#ifndef _WIN32
4094 "-chroot dir Chroot to dir just before starting the VM.\n"
4095 "-runas user Change to user id user just before starting the VM.\n"
4096#endif
bellard0824d6f2003-06-24 13:42:40 +00004097 "\n"
bellard82c643f2004-07-14 17:28:13 +00004098 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004099 "ctrl-alt-f toggle full screen\n"
4100 "ctrl-alt-n switch to virtual console 'n'\n"
4101 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004102 "\n"
4103 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4104 ,
bellard0db63472003-10-27 21:37:46 +00004105 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004106 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004107#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004108 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004109 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004110#endif
bellard6e44ba72004-01-18 21:56:49 +00004111 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004112 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004113 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004114}
4115
bellardcd6f1162004-05-13 22:02:20 +00004116#define HAS_ARG 0x0001
4117
4118enum {
blueswir1d2c639d2009-01-24 18:19:25 +00004119 /* Please keep in synch with help, qemu_options[] and
4120 qemu-doc.texi */
4121 /* Standard options: */
bellardcd6f1162004-05-13 22:02:20 +00004122 QEMU_OPTION_h,
bellardcc1daa42005-06-05 14:49:17 +00004123 QEMU_OPTION_M,
j_mayer94fc95c2007-03-05 19:44:02 +00004124 QEMU_OPTION_cpu,
blueswir1d2c639d2009-01-24 18:19:25 +00004125 QEMU_OPTION_smp,
bellardcd6f1162004-05-13 22:02:20 +00004126 QEMU_OPTION_fda,
4127 QEMU_OPTION_fdb,
4128 QEMU_OPTION_hda,
4129 QEMU_OPTION_hdb,
4130 QEMU_OPTION_hdc,
4131 QEMU_OPTION_hdd,
4132 QEMU_OPTION_cdrom,
blueswir1d2c639d2009-01-24 18:19:25 +00004133 QEMU_OPTION_drive,
balrog3e3d5812007-04-30 02:09:25 +00004134 QEMU_OPTION_mtdblock,
pbrooka1bb27b2007-04-06 16:49:48 +00004135 QEMU_OPTION_sd,
j_mayer86f55662007-04-24 06:52:59 +00004136 QEMU_OPTION_pflash,
bellardcd6f1162004-05-13 22:02:20 +00004137 QEMU_OPTION_boot,
4138 QEMU_OPTION_snapshot,
4139 QEMU_OPTION_m,
blueswir1d2c639d2009-01-24 18:19:25 +00004140 QEMU_OPTION_k,
bellard1d14ffa2005-10-30 18:58:22 +00004141 QEMU_OPTION_audio_help,
4142 QEMU_OPTION_soundhw,
blueswir1d2c639d2009-01-24 18:19:25 +00004143 QEMU_OPTION_usb,
4144 QEMU_OPTION_usbdevice,
4145 QEMU_OPTION_name,
4146 QEMU_OPTION_uuid,
bellardcd6f1162004-05-13 22:02:20 +00004147
blueswir1d2c639d2009-01-24 18:19:25 +00004148 /* Display options: */
4149 QEMU_OPTION_nographic,
4150 QEMU_OPTION_curses,
4151 QEMU_OPTION_no_frame,
4152 QEMU_OPTION_alt_grab,
4153 QEMU_OPTION_no_quit,
4154 QEMU_OPTION_sdl,
4155 QEMU_OPTION_portrait,
4156 QEMU_OPTION_vga,
4157 QEMU_OPTION_full_screen,
4158 QEMU_OPTION_g,
4159 QEMU_OPTION_vnc,
4160
4161 /* Network options: */
bellard7c9d8e02005-11-15 22:16:05 +00004162 QEMU_OPTION_net,
bellardc7f74642004-08-24 21:57:12 +00004163 QEMU_OPTION_tftp,
ths47d5d012007-02-20 00:05:08 +00004164 QEMU_OPTION_bootp,
bellard9d728e82004-09-05 23:09:03 +00004165 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00004166 QEMU_OPTION_redir,
balrogdc72ac12008-11-09 00:04:26 +00004167 QEMU_OPTION_bt,
bellardcd6f1162004-05-13 22:02:20 +00004168
blueswir1d2c639d2009-01-24 18:19:25 +00004169 /* i386 target only: */
4170 QEMU_OPTION_win2k_hack,
4171 QEMU_OPTION_rtc_td_hack,
4172 QEMU_OPTION_no_fd_bootchk,
4173 QEMU_OPTION_no_acpi,
4174 QEMU_OPTION_no_hpet,
aliguori8a92ea22009-02-27 20:12:36 +00004175 QEMU_OPTION_acpitable,
blueswir1d2c639d2009-01-24 18:19:25 +00004176
4177 /* Linux boot specific: */
bellardcd6f1162004-05-13 22:02:20 +00004178 QEMU_OPTION_kernel,
4179 QEMU_OPTION_append,
4180 QEMU_OPTION_initrd,
4181
blueswir1d2c639d2009-01-24 18:19:25 +00004182 /* Debug/Expert options: */
4183 QEMU_OPTION_serial,
4184 QEMU_OPTION_parallel,
4185 QEMU_OPTION_monitor,
4186 QEMU_OPTION_pidfile,
bellardcd6f1162004-05-13 22:02:20 +00004187 QEMU_OPTION_S,
4188 QEMU_OPTION_s,
4189 QEMU_OPTION_p,
4190 QEMU_OPTION_d,
4191 QEMU_OPTION_hdachs,
4192 QEMU_OPTION_L,
j_mayer1192dad2007-10-05 13:08:35 +00004193 QEMU_OPTION_bios,
bellard89bfc102006-02-08 22:46:31 +00004194 QEMU_OPTION_kernel_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00004195 QEMU_OPTION_no_kqemu,
aliguori7ba1e612008-11-05 16:04:33 +00004196 QEMU_OPTION_enable_kvm,
bellardd1beab82006-10-02 19:44:22 +00004197 QEMU_OPTION_no_reboot,
aurel32b2f76162008-04-11 21:35:52 +00004198 QEMU_OPTION_no_shutdown,
blueswir1d2c639d2009-01-24 18:19:25 +00004199 QEMU_OPTION_loadvm,
ths71e3ceb2006-12-22 02:11:31 +00004200 QEMU_OPTION_daemonize,
ths9ae02552007-01-05 17:39:04 +00004201 QEMU_OPTION_option_rom,
blueswir166508602007-05-01 14:16:52 +00004202 QEMU_OPTION_prom_env,
thsf3dcfad2007-08-24 01:26:02 +00004203 QEMU_OPTION_clock,
blueswir1d2c639d2009-01-24 18:19:25 +00004204 QEMU_OPTION_localtime,
bellard7e0af5d02007-11-07 16:24:33 +00004205 QEMU_OPTION_startdate,
pbrook2e70f6e2008-06-29 01:03:05 +00004206 QEMU_OPTION_icount,
blueswir1d2c639d2009-01-24 18:19:25 +00004207 QEMU_OPTION_echr,
4208 QEMU_OPTION_virtiocon,
4209 QEMU_OPTION_show_cursor,
4210 QEMU_OPTION_semihosting,
4211 QEMU_OPTION_old_param,
4212 QEMU_OPTION_tb_size,
aliguori5bb79102008-10-13 03:12:02 +00004213 QEMU_OPTION_incoming,
aliguori08585322009-02-27 22:09:45 +00004214 QEMU_OPTION_chroot,
4215 QEMU_OPTION_runas,
bellardcd6f1162004-05-13 22:02:20 +00004216};
4217
4218typedef struct QEMUOption {
4219 const char *name;
4220 int flags;
4221 int index;
4222} QEMUOption;
4223
blueswir1dbed7e42008-10-01 19:38:09 +00004224static const QEMUOption qemu_options[] = {
blueswir1d2c639d2009-01-24 18:19:25 +00004225 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4226 qemu-doc.texi */
4227 /* Standard options: */
bellardcd6f1162004-05-13 22:02:20 +00004228 { "h", 0, QEMU_OPTION_h },
pbrook64423fb2007-01-27 17:11:41 +00004229 { "help", 0, QEMU_OPTION_h },
bellardcc1daa42005-06-05 14:49:17 +00004230 { "M", HAS_ARG, QEMU_OPTION_M },
j_mayer94fc95c2007-03-05 19:44:02 +00004231 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
blueswir1d2c639d2009-01-24 18:19:25 +00004232 { "smp", HAS_ARG, QEMU_OPTION_smp },
bellardcd6f1162004-05-13 22:02:20 +00004233 { "fda", HAS_ARG, QEMU_OPTION_fda },
4234 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
4235 { "hda", HAS_ARG, QEMU_OPTION_hda },
4236 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
4237 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
4238 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
4239 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
blueswir1d2c639d2009-01-24 18:19:25 +00004240 { "drive", HAS_ARG, QEMU_OPTION_drive },
balrog3e3d5812007-04-30 02:09:25 +00004241 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
pbrooka1bb27b2007-04-06 16:49:48 +00004242 { "sd", HAS_ARG, QEMU_OPTION_sd },
j_mayer86f55662007-04-24 06:52:59 +00004243 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
bellardcd6f1162004-05-13 22:02:20 +00004244 { "boot", HAS_ARG, QEMU_OPTION_boot },
4245 { "snapshot", 0, QEMU_OPTION_snapshot },
4246 { "m", HAS_ARG, QEMU_OPTION_m },
blueswir1d2c639d2009-01-24 18:19:25 +00004247#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00004248 { "k", HAS_ARG, QEMU_OPTION_k },
blueswir1d2c639d2009-01-24 18:19:25 +00004249#endif
bellard1d14ffa2005-10-30 18:58:22 +00004250#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004251 { "audio-help", 0, QEMU_OPTION_audio_help },
4252 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
4253#endif
blueswir1d2c639d2009-01-24 18:19:25 +00004254 { "usb", 0, QEMU_OPTION_usb },
4255 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
4256 { "name", HAS_ARG, QEMU_OPTION_name },
4257 { "uuid", HAS_ARG, QEMU_OPTION_uuid },
bellardcd6f1162004-05-13 22:02:20 +00004258
blueswir1d2c639d2009-01-24 18:19:25 +00004259 /* Display options: */
4260 { "nographic", 0, QEMU_OPTION_nographic },
4261#ifdef CONFIG_CURSES
4262 { "curses", 0, QEMU_OPTION_curses },
4263#endif
4264#ifdef CONFIG_SDL
4265 { "no-frame", 0, QEMU_OPTION_no_frame },
4266 { "alt-grab", 0, QEMU_OPTION_alt_grab },
4267 { "no-quit", 0, QEMU_OPTION_no_quit },
4268 { "sdl", 0, QEMU_OPTION_sdl },
4269#endif
4270 { "portrait", 0, QEMU_OPTION_portrait },
4271 { "vga", HAS_ARG, QEMU_OPTION_vga },
4272 { "full-screen", 0, QEMU_OPTION_full_screen },
4273#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4274 { "g", 1, QEMU_OPTION_g },
4275#endif
4276 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
4277
4278 /* Network options: */
bellard7c9d8e02005-11-15 22:16:05 +00004279 { "net", HAS_ARG, QEMU_OPTION_net},
bellard158156d2004-05-17 21:13:42 +00004280#ifdef CONFIG_SLIRP
bellardc7f74642004-08-24 21:57:12 +00004281 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
ths47d5d012007-02-20 00:05:08 +00004282 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
bellardc94c8d62004-09-13 21:37:34 +00004283#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004284 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00004285#endif
bellard9bf05442004-08-25 22:12:49 +00004286 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00004287#endif
balrogdc72ac12008-11-09 00:04:26 +00004288 { "bt", HAS_ARG, QEMU_OPTION_bt },
blueswir1d2c639d2009-01-24 18:19:25 +00004289#ifdef TARGET_I386
4290 /* i386 target only: */
4291 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
4292 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack },
4293 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
4294 { "no-acpi", 0, QEMU_OPTION_no_acpi },
4295 { "no-hpet", 0, QEMU_OPTION_no_hpet },
aliguori8a92ea22009-02-27 20:12:36 +00004296 { "acpitable", HAS_ARG, QEMU_OPTION_acpitable },
blueswir1d2c639d2009-01-24 18:19:25 +00004297#endif
bellardcd6f1162004-05-13 22:02:20 +00004298
blueswir1d2c639d2009-01-24 18:19:25 +00004299 /* Linux boot specific: */
bellardcd6f1162004-05-13 22:02:20 +00004300 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
4301 { "append", HAS_ARG, QEMU_OPTION_append },
4302 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
4303
blueswir1d2c639d2009-01-24 18:19:25 +00004304 /* Debug/Expert options: */
4305 { "serial", HAS_ARG, QEMU_OPTION_serial },
4306 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
4307 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
4308 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellardcd6f1162004-05-13 22:02:20 +00004309 { "S", 0, QEMU_OPTION_S },
4310 { "s", 0, QEMU_OPTION_s },
4311 { "p", HAS_ARG, QEMU_OPTION_p },
4312 { "d", HAS_ARG, QEMU_OPTION_d },
4313 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
4314 { "L", HAS_ARG, QEMU_OPTION_L },
j_mayer1192dad2007-10-05 13:08:35 +00004315 { "bios", HAS_ARG, QEMU_OPTION_bios },
bellardd993e022005-02-10 22:00:06 +00004316#ifdef USE_KQEMU
bellard89bfc102006-02-08 22:46:31 +00004317 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
blueswir1d2c639d2009-01-24 18:19:25 +00004318 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
bellardd993e022005-02-10 22:00:06 +00004319#endif
aliguori7ba1e612008-11-05 16:04:33 +00004320#ifdef CONFIG_KVM
4321 { "enable-kvm", 0, QEMU_OPTION_enable_kvm },
4322#endif
bellardd1beab82006-10-02 19:44:22 +00004323 { "no-reboot", 0, QEMU_OPTION_no_reboot },
aurel32b2f76162008-04-11 21:35:52 +00004324 { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
blueswir1d2c639d2009-01-24 18:19:25 +00004325 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
ths71e3ceb2006-12-22 02:11:31 +00004326 { "daemonize", 0, QEMU_OPTION_daemonize },
ths9ae02552007-01-05 17:39:04 +00004327 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
blueswir195efd112008-12-24 20:26:14 +00004328#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00004329 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
4330#endif
blueswir1d2c639d2009-01-24 18:19:25 +00004331 { "clock", HAS_ARG, QEMU_OPTION_clock },
4332 { "localtime", 0, QEMU_OPTION_localtime },
4333 { "startdate", HAS_ARG, QEMU_OPTION_startdate },
4334 { "icount", HAS_ARG, QEMU_OPTION_icount },
4335 { "echr", HAS_ARG, QEMU_OPTION_echr },
4336 { "virtioconsole", HAS_ARG, QEMU_OPTION_virtiocon },
4337 { "show-cursor", 0, QEMU_OPTION_show_cursor },
4338#if defined(TARGET_ARM) || defined(TARGET_M68K)
4339 { "semihosting", 0, QEMU_OPTION_semihosting },
4340#endif
balrog2b8f2d42007-07-27 22:08:46 +00004341#if defined(TARGET_ARM)
4342 { "old-param", 0, QEMU_OPTION_old_param },
4343#endif
bellard26a5f132008-05-28 12:30:31 +00004344 { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
aliguori5bb79102008-10-13 03:12:02 +00004345 { "incoming", HAS_ARG, QEMU_OPTION_incoming },
aliguori08585322009-02-27 22:09:45 +00004346 { "chroot", HAS_ARG, QEMU_OPTION_chroot },
4347 { "runas", HAS_ARG, QEMU_OPTION_runas },
bellardcd6f1162004-05-13 22:02:20 +00004348 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004349};
4350
bellard1d14ffa2005-10-30 18:58:22 +00004351#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004352struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004353#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004354#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004355 {
4356 "pcspk",
4357 "PC speaker",
4358 0,
4359 1,
4360 { .init_isa = pcspk_audio_init }
4361 },
4362#endif
malc4c9b53e2009-01-09 10:46:34 +00004363
4364#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004365 {
4366 "sb16",
4367 "Creative Sound Blaster 16",
4368 0,
4369 1,
4370 { .init_isa = SB16_init }
4371 },
malc4c9b53e2009-01-09 10:46:34 +00004372#endif
bellard6a36d842005-12-18 20:34:32 +00004373
malccc53d262008-06-13 10:48:22 +00004374#ifdef CONFIG_CS4231A
4375 {
4376 "cs4231a",
4377 "CS4231A",
4378 0,
4379 1,
4380 { .init_isa = cs4231a_init }
4381 },
4382#endif
4383
bellard6a36d842005-12-18 20:34:32 +00004384#ifdef CONFIG_ADLIB
4385 {
4386 "adlib",
4387#ifdef HAS_YMF262
4388 "Yamaha YMF262 (OPL3)",
4389#else
4390 "Yamaha YM3812 (OPL2)",
4391#endif
4392 0,
4393 1,
4394 { .init_isa = Adlib_init }
4395 },
4396#endif
4397
4398#ifdef CONFIG_GUS
4399 {
4400 "gus",
4401 "Gravis Ultrasound GF1",
4402 0,
4403 1,
4404 { .init_isa = GUS_init }
4405 },
4406#endif
4407
malc4c9b53e2009-01-09 10:46:34 +00004408#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004409 {
4410 "ac97",
4411 "Intel 82801AA AC97 Audio",
4412 0,
4413 0,
4414 { .init_pci = ac97_init }
4415 },
malc4c9b53e2009-01-09 10:46:34 +00004416#endif
balroge5c9a132008-01-14 04:27:55 +00004417
malc4c9b53e2009-01-09 10:46:34 +00004418#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004419 {
4420 "es1370",
4421 "ENSONIQ AudioPCI ES1370",
4422 0,
4423 0,
4424 { .init_pci = es1370_init }
4425 },
balrogb00052e2007-04-30 02:22:06 +00004426#endif
bellard6a36d842005-12-18 20:34:32 +00004427
malc4c9b53e2009-01-09 10:46:34 +00004428#endif /* HAS_AUDIO_CHOICE */
4429
bellard6a36d842005-12-18 20:34:32 +00004430 { NULL, NULL, 0, 0, { NULL } }
4431};
4432
bellard1d14ffa2005-10-30 18:58:22 +00004433static void select_soundhw (const char *optarg)
4434{
bellard6a36d842005-12-18 20:34:32 +00004435 struct soundhw *c;
4436
bellard1d14ffa2005-10-30 18:58:22 +00004437 if (*optarg == '?') {
4438 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004439
bellard1d14ffa2005-10-30 18:58:22 +00004440 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004441 for (c = soundhw; c->name; ++c) {
4442 printf ("%-11s %s\n", c->name, c->descr);
4443 }
4444 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004445 exit (*optarg != '?');
4446 }
4447 else {
bellard6a36d842005-12-18 20:34:32 +00004448 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004449 const char *p;
4450 char *e;
4451 int bad_card = 0;
4452
bellard6a36d842005-12-18 20:34:32 +00004453 if (!strcmp (optarg, "all")) {
4454 for (c = soundhw; c->name; ++c) {
4455 c->enabled = 1;
4456 }
4457 return;
4458 }
bellard1d14ffa2005-10-30 18:58:22 +00004459
bellard6a36d842005-12-18 20:34:32 +00004460 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004461 while (*p) {
4462 e = strchr (p, ',');
4463 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004464
4465 for (c = soundhw; c->name; ++c) {
4466 if (!strncmp (c->name, p, l)) {
4467 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004468 break;
4469 }
4470 }
bellard6a36d842005-12-18 20:34:32 +00004471
4472 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004473 if (l > 80) {
4474 fprintf (stderr,
4475 "Unknown sound card name (too big to show)\n");
4476 }
4477 else {
4478 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4479 (int) l, p);
4480 }
4481 bad_card = 1;
4482 }
4483 p += l + (e != NULL);
4484 }
4485
4486 if (bad_card)
4487 goto show_valid_cards;
4488 }
4489}
4490#endif
4491
malc3893c122008-09-28 00:42:05 +00004492static void select_vgahw (const char *p)
4493{
4494 const char *opts;
4495
4496 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004497 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004498 cirrus_vga_enabled = 0;
4499 vmsvga_enabled = 0;
4500 } else if (strstart(p, "cirrus", &opts)) {
4501 cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004502 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004503 vmsvga_enabled = 0;
4504 } else if (strstart(p, "vmware", &opts)) {
4505 cirrus_vga_enabled = 0;
aliguoric2b3b412009-01-15 20:37:28 +00004506 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004507 vmsvga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004508 } else if (strstart(p, "none", &opts)) {
4509 cirrus_vga_enabled = 0;
4510 std_vga_enabled = 0;
4511 vmsvga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004512 } else {
4513 invalid_vga:
4514 fprintf(stderr, "Unknown vga type: %s\n", p);
4515 exit(1);
4516 }
malccb5a7aa2008-09-28 00:42:12 +00004517 while (*opts) {
4518 const char *nextopt;
4519
4520 if (strstart(opts, ",retrace=", &nextopt)) {
4521 opts = nextopt;
4522 if (strstart(opts, "dumb", &nextopt))
4523 vga_retrace_method = VGA_RETRACE_DUMB;
4524 else if (strstart(opts, "precise", &nextopt))
4525 vga_retrace_method = VGA_RETRACE_PRECISE;
4526 else goto invalid_vga;
4527 } else goto invalid_vga;
4528 opts = nextopt;
4529 }
malc3893c122008-09-28 00:42:05 +00004530}
4531
bellard3587d7e2006-06-26 20:03:44 +00004532#ifdef _WIN32
4533static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4534{
4535 exit(STATUS_CONTROL_C_EXIT);
4536 return TRUE;
4537}
4538#endif
4539
blueswir18fcb1b92008-09-18 18:29:08 +00004540static int qemu_uuid_parse(const char *str, uint8_t *uuid)
4541{
4542 int ret;
4543
4544 if(strlen(str) != 36)
4545 return -1;
4546
4547 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4548 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4549 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4550
4551 if(ret != 16)
4552 return -1;
4553
4554 return 0;
4555}
4556
bellard7c9d8e02005-11-15 22:16:05 +00004557#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004558
aliguori5b08fc12008-08-21 20:08:03 +00004559#ifndef _WIN32
4560
4561static void termsig_handler(int signal)
4562{
4563 qemu_system_shutdown_request();
4564}
4565
blueswir16f9e3802008-09-09 18:56:59 +00004566static void termsig_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004567{
4568 struct sigaction act;
4569
4570 memset(&act, 0, sizeof(act));
4571 act.sa_handler = termsig_handler;
4572 sigaction(SIGINT, &act, NULL);
4573 sigaction(SIGHUP, &act, NULL);
4574 sigaction(SIGTERM, &act, NULL);
4575}
4576
4577#endif
4578
malc902b3d52008-12-10 19:18:40 +00004579int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004580{
bellard67b915a2004-03-31 23:37:16 +00004581#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00004582 int use_gdbstub;
4583 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00004584#endif
j_mayer28c5af52007-11-11 01:50:45 +00004585 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004586 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004587 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004588 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004589 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00004590 const char *boot_devices = "";
aliguori3023f332009-01-16 19:04:14 +00004591 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004592 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004593 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004594 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004595 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004596 const char *bt_opts[MAX_BT_CMDLINE];
4597 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004598 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004599 int optind;
4600 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004601 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004602 const char *monitor_device;
4603 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004604 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004605 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004606 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004607 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4608 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004609 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004610 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004611 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004612 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004613 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00004614 int fds[2];
bellard26a5f132008-05-28 12:30:31 +00004615 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004616 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004617 const char *incoming = NULL;
aliguori54042bc2009-02-27 22:16:47 +00004618 int fd = 0;
4619 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004620 const char *chroot_dir = NULL;
4621 const char *run_as = NULL;
bellard0bd48852005-11-11 00:00:47 +00004622
malc902b3d52008-12-10 19:18:40 +00004623 qemu_cache_utils_init(envp);
4624
bellard0bd48852005-11-11 00:00:47 +00004625 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004626#ifndef _WIN32
4627 {
4628 struct sigaction act;
4629 sigfillset(&act.sa_mask);
4630 act.sa_flags = 0;
4631 act.sa_handler = SIG_IGN;
4632 sigaction(SIGPIPE, &act, NULL);
4633 }
bellard3587d7e2006-06-26 20:03:44 +00004634#else
4635 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004636 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4637 QEMU to run on a single CPU */
4638 {
4639 HANDLE h;
4640 DWORD mask, smask;
4641 int i;
4642 h = GetCurrentProcess();
4643 if (GetProcessAffinityMask(h, &mask, &smask)) {
4644 for(i = 0; i < 32; i++) {
4645 if (mask & (1 << i))
4646 break;
4647 }
4648 if (i != 32) {
4649 mask = 1 << i;
4650 SetProcessAffinityMask(h, mask);
4651 }
4652 }
4653 }
bellard67b915a2004-03-31 23:37:16 +00004654#endif
bellardbe995c22006-06-25 16:25:21 +00004655
bellardcc1daa42005-06-05 14:49:17 +00004656 register_machines();
4657 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004658 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004659 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004660 ram_size = 0;
bellard313aa562003-08-10 21:52:11 +00004661 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00004662#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00004663 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00004664 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00004665#endif
bellard33e39632003-07-06 17:15:21 +00004666 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004667 nographic = 0;
balrog4d3b6f62008-02-10 16:33:14 +00004668 curses = 0;
bellarda20dd502003-09-30 21:07:02 +00004669 kernel_filename = NULL;
4670 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004671 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004672 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00004673 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00004674
aurel32c75a8232008-05-04 00:50:34 +00004675 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004676 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004677 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004678 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004679
aliguori8290edd2009-02-27 20:14:29 +00004680 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004681 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004682 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004683 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004684
aliguori1b8fc812009-02-27 20:01:39 +00004685 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004686 virtio_consoles[i] = NULL;
4687 virtio_console_index = 0;
4688
bellarda594cfb2005-11-06 16:13:29 +00004689 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004690
bellard7c9d8e02005-11-15 22:16:05 +00004691 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004692 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004693 nb_drives = 0;
4694 nb_drives_opt = 0;
4695 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004696
4697 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004698
bellard26a5f132008-05-28 12:30:31 +00004699 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004700 autostart= 1;
4701
bellardcd6f1162004-05-13 22:02:20 +00004702 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004703 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004704 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004705 break;
bellardcd6f1162004-05-13 22:02:20 +00004706 r = argv[optind];
4707 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004708 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004709 } else {
4710 const QEMUOption *popt;
4711
4712 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004713 /* Treat --foo the same as -foo. */
4714 if (r[1] == '-')
4715 r++;
bellardcd6f1162004-05-13 22:02:20 +00004716 popt = qemu_options;
4717 for(;;) {
4718 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004719 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004720 argv[0], r);
4721 exit(1);
4722 }
4723 if (!strcmp(popt->name, r + 1))
4724 break;
4725 popt++;
4726 }
4727 if (popt->flags & HAS_ARG) {
4728 if (optind >= argc) {
4729 fprintf(stderr, "%s: option '%s' requires an argument\n",
4730 argv[0], r);
4731 exit(1);
4732 }
4733 optarg = argv[optind++];
4734 } else {
4735 optarg = NULL;
4736 }
4737
4738 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004739 case QEMU_OPTION_M:
4740 machine = find_machine(optarg);
4741 if (!machine) {
4742 QEMUMachine *m;
4743 printf("Supported machines are:\n");
4744 for(m = first_machine; m != NULL; m = m->next) {
4745 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004746 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00004747 m == first_machine ? " (default)" : "");
4748 }
ths15f82202007-06-29 23:26:08 +00004749 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004750 }
4751 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004752 case QEMU_OPTION_cpu:
4753 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004754 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004755/* XXX: implement xxx_cpu_list for targets that still miss it */
4756#if defined(cpu_list)
4757 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004758#endif
ths15f82202007-06-29 23:26:08 +00004759 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004760 } else {
4761 cpu_model = optarg;
4762 }
4763 break;
bellardcd6f1162004-05-13 22:02:20 +00004764 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004765 initrd_filename = optarg;
4766 break;
bellardcd6f1162004-05-13 22:02:20 +00004767 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004768 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00004769 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004770 else
balrog609497a2008-01-14 02:56:53 +00004771 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004772 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004773 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004774 translation == BIOS_ATA_TRANSLATION_LBA ?
4775 ",trans=lba" :
4776 translation == BIOS_ATA_TRANSLATION_NONE ?
4777 ",trans=none" : "");
4778 break;
bellardcd6f1162004-05-13 22:02:20 +00004779 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004780 case QEMU_OPTION_hdc:
4781 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004782 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004783 break;
thse4bcb142007-12-02 04:51:10 +00004784 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004785 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004786 break;
balrog3e3d5812007-04-30 02:09:25 +00004787 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004788 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004789 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004790 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004791 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004792 break;
j_mayer86f55662007-04-24 06:52:59 +00004793 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004794 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004795 break;
bellardcd6f1162004-05-13 22:02:20 +00004796 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004797 snapshot = 1;
4798 break;
bellardcd6f1162004-05-13 22:02:20 +00004799 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004800 {
bellard330d0412003-07-26 18:11:40 +00004801 const char *p;
4802 p = optarg;
4803 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004804 if (cyls < 1 || cyls > 16383)
4805 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004806 if (*p != ',')
4807 goto chs_fail;
4808 p++;
4809 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004810 if (heads < 1 || heads > 16)
4811 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004812 if (*p != ',')
4813 goto chs_fail;
4814 p++;
4815 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004816 if (secs < 1 || secs > 63)
4817 goto chs_fail;
4818 if (*p == ',') {
4819 p++;
4820 if (!strcmp(p, "none"))
4821 translation = BIOS_ATA_TRANSLATION_NONE;
4822 else if (!strcmp(p, "lba"))
4823 translation = BIOS_ATA_TRANSLATION_LBA;
4824 else if (!strcmp(p, "auto"))
4825 translation = BIOS_ATA_TRANSLATION_AUTO;
4826 else
4827 goto chs_fail;
4828 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004829 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004830 fprintf(stderr, "qemu: invalid physical CHS format\n");
4831 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004832 }
thse4bcb142007-12-02 04:51:10 +00004833 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00004834 snprintf(drives_opt[hda_index].opt,
4835 sizeof(drives_opt[hda_index].opt),
4836 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4837 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004838 translation == BIOS_ATA_TRANSLATION_LBA ?
4839 ",trans=lba" :
4840 translation == BIOS_ATA_TRANSLATION_NONE ?
4841 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00004842 }
4843 break;
bellardcd6f1162004-05-13 22:02:20 +00004844 case QEMU_OPTION_nographic:
bellarda20dd502003-09-30 21:07:02 +00004845 nographic = 1;
4846 break;
balrog4d3b6f62008-02-10 16:33:14 +00004847#ifdef CONFIG_CURSES
4848 case QEMU_OPTION_curses:
4849 curses = 1;
4850 break;
4851#endif
balroga171fe32007-04-30 01:48:07 +00004852 case QEMU_OPTION_portrait:
4853 graphic_rotate = 1;
4854 break;
bellardcd6f1162004-05-13 22:02:20 +00004855 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004856 kernel_filename = optarg;
4857 break;
bellardcd6f1162004-05-13 22:02:20 +00004858 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004859 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004860 break;
bellardcd6f1162004-05-13 22:02:20 +00004861 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004862 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004863 break;
bellardcd6f1162004-05-13 22:02:20 +00004864 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004865 boot_devices = optarg;
4866 /* We just do some generic consistency checks */
4867 {
4868 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00004869 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00004870
4871 boot_devices_bitmap = 0;
4872 for (p = boot_devices; *p != '\0'; p++) {
4873 /* Allowed boot devices are:
4874 * a b : floppy disk drives
4875 * c ... f : IDE disk drives
4876 * g ... m : machine implementation dependant drives
4877 * n ... p : network devices
4878 * It's up to each machine implementation to check
4879 * if the given boot devices match the actual hardware
4880 * implementation and firmware features.
4881 */
4882 if (*p < 'a' || *p > 'q') {
4883 fprintf(stderr, "Invalid boot device '%c'\n", *p);
4884 exit(1);
4885 }
4886 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4887 fprintf(stderr,
4888 "Boot device '%c' was given twice\n",*p);
4889 exit(1);
4890 }
4891 boot_devices_bitmap |= 1 << (*p - 'a');
4892 }
bellard36b486b2003-11-11 13:36:08 +00004893 }
4894 break;
bellardcd6f1162004-05-13 22:02:20 +00004895 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004896 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004897 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004898 break;
bellard52ca8d62006-06-14 16:03:05 +00004899#ifdef TARGET_I386
4900 case QEMU_OPTION_no_fd_bootchk:
4901 fd_bootchk = 0;
4902 break;
4903#endif
bellard7c9d8e02005-11-15 22:16:05 +00004904 case QEMU_OPTION_net:
4905 if (nb_net_clients >= MAX_NET_CLIENTS) {
4906 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00004907 exit(1);
4908 }
pbrookfd5f3932008-03-26 20:55:43 +00004909 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00004910 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00004911 break;
bellardc7f74642004-08-24 21:57:12 +00004912#ifdef CONFIG_SLIRP
4913 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00004914 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004915 break;
ths47d5d012007-02-20 00:05:08 +00004916 case QEMU_OPTION_bootp:
4917 bootp_filename = optarg;
4918 break;
bellardc94c8d62004-09-13 21:37:34 +00004919#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004920 case QEMU_OPTION_smb:
4921 net_slirp_smb(optarg);
4922 break;
bellardc94c8d62004-09-13 21:37:34 +00004923#endif
bellard9bf05442004-08-25 22:12:49 +00004924 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00004925 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00004926 break;
bellardc7f74642004-08-24 21:57:12 +00004927#endif
balrogdc72ac12008-11-09 00:04:26 +00004928 case QEMU_OPTION_bt:
4929 if (nb_bt_opts >= MAX_BT_CMDLINE) {
4930 fprintf(stderr, "qemu: too many bluetooth options\n");
4931 exit(1);
4932 }
4933 bt_opts[nb_bt_opts++] = optarg;
4934 break;
bellard1d14ffa2005-10-30 18:58:22 +00004935#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004936 case QEMU_OPTION_audio_help:
4937 AUD_help ();
4938 exit (0);
4939 break;
4940 case QEMU_OPTION_soundhw:
4941 select_soundhw (optarg);
4942 break;
4943#endif
bellardcd6f1162004-05-13 22:02:20 +00004944 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00004945 help(0);
bellardcd6f1162004-05-13 22:02:20 +00004946 break;
aurel3200f82b82008-04-27 21:12:55 +00004947 case QEMU_OPTION_m: {
4948 uint64_t value;
4949 char *ptr;
4950
4951 value = strtoul(optarg, &ptr, 10);
4952 switch (*ptr) {
4953 case 0: case 'M': case 'm':
4954 value <<= 20;
4955 break;
4956 case 'G': case 'g':
4957 value <<= 30;
4958 break;
4959 default:
4960 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00004961 exit(1);
4962 }
aurel3200f82b82008-04-27 21:12:55 +00004963
4964 /* On 32-bit hosts, QEMU is limited by virtual address space */
4965 if (value > (2047 << 20)
4966#ifndef USE_KQEMU
4967 && HOST_LONG_BITS == 32
4968#endif
4969 ) {
4970 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4971 exit(1);
4972 }
4973 if (value != (uint64_t)(ram_addr_t)value) {
4974 fprintf(stderr, "qemu: ram size too large\n");
4975 exit(1);
4976 }
4977 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00004978 break;
aurel3200f82b82008-04-27 21:12:55 +00004979 }
bellardcd6f1162004-05-13 22:02:20 +00004980 case QEMU_OPTION_d:
4981 {
4982 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00004983 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00004984
bellardcd6f1162004-05-13 22:02:20 +00004985 mask = cpu_str_to_log_mask(optarg);
4986 if (!mask) {
4987 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00004988 for(item = cpu_log_items; item->mask != 0; item++) {
4989 printf("%-10s %s\n", item->name, item->help);
4990 }
4991 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00004992 }
4993 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00004994 }
bellardcd6f1162004-05-13 22:02:20 +00004995 break;
bellard67b915a2004-03-31 23:37:16 +00004996#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00004997 case QEMU_OPTION_s:
4998 use_gdbstub = 1;
4999 break;
5000 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00005001 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005002 break;
bellard67b915a2004-03-31 23:37:16 +00005003#endif
bellardcd6f1162004-05-13 22:02:20 +00005004 case QEMU_OPTION_L:
5005 bios_dir = optarg;
5006 break;
j_mayer1192dad2007-10-05 13:08:35 +00005007 case QEMU_OPTION_bios:
5008 bios_name = optarg;
5009 break;
bellardcd6f1162004-05-13 22:02:20 +00005010 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005011 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005012 break;
bellard3d11d0e2004-12-12 16:56:30 +00005013 case QEMU_OPTION_k:
5014 keyboard_layout = optarg;
5015 break;
bellardee22c2f2004-06-03 12:49:50 +00005016 case QEMU_OPTION_localtime:
5017 rtc_utc = 0;
5018 break;
malc3893c122008-09-28 00:42:05 +00005019 case QEMU_OPTION_vga:
5020 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005021 break;
bellarde9b137c2004-06-21 16:46:10 +00005022 case QEMU_OPTION_g:
5023 {
5024 const char *p;
5025 int w, h, depth;
5026 p = optarg;
5027 w = strtol(p, (char **)&p, 10);
5028 if (w <= 0) {
5029 graphic_error:
5030 fprintf(stderr, "qemu: invalid resolution or depth\n");
5031 exit(1);
5032 }
5033 if (*p != 'x')
5034 goto graphic_error;
5035 p++;
5036 h = strtol(p, (char **)&p, 10);
5037 if (h <= 0)
5038 goto graphic_error;
5039 if (*p == 'x') {
5040 p++;
5041 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005042 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005043 depth != 24 && depth != 32)
5044 goto graphic_error;
5045 } else if (*p == '\0') {
5046 depth = graphic_depth;
5047 } else {
5048 goto graphic_error;
5049 }
ths3b46e622007-09-17 08:09:54 +00005050
bellarde9b137c2004-06-21 16:46:10 +00005051 graphic_width = w;
5052 graphic_height = h;
5053 graphic_depth = depth;
5054 }
5055 break;
ths20d8a3e2007-02-18 17:04:49 +00005056 case QEMU_OPTION_echr:
5057 {
5058 char *r;
5059 term_escape_char = strtol(optarg, &r, 0);
5060 if (r == optarg)
5061 printf("Bad argument to echr\n");
5062 break;
5063 }
bellard82c643f2004-07-14 17:28:13 +00005064 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00005065 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00005066 break;
5067 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005068 if (serial_device_index >= MAX_SERIAL_PORTS) {
5069 fprintf(stderr, "qemu: too many serial ports\n");
5070 exit(1);
5071 }
pbrookfd5f3932008-03-26 20:55:43 +00005072 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005073 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005074 break;
aliguori51ecf132009-01-15 20:06:40 +00005075 case QEMU_OPTION_virtiocon:
5076 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5077 fprintf(stderr, "qemu: too many virtio consoles\n");
5078 exit(1);
5079 }
5080 virtio_consoles[virtio_console_index] = optarg;
5081 virtio_console_index++;
5082 break;
bellard6508fe52005-01-15 12:02:56 +00005083 case QEMU_OPTION_parallel:
5084 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5085 fprintf(stderr, "qemu: too many parallel ports\n");
5086 exit(1);
5087 }
pbrookfd5f3932008-03-26 20:55:43 +00005088 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005089 parallel_device_index++;
5090 break;
bellardd63d3072004-10-03 13:29:03 +00005091 case QEMU_OPTION_loadvm:
5092 loadvm = optarg;
5093 break;
5094 case QEMU_OPTION_full_screen:
5095 full_screen = 1;
5096 break;
ths667acca2006-12-11 02:08:05 +00005097#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005098 case QEMU_OPTION_no_frame:
5099 no_frame = 1;
5100 break;
ths3780e192007-06-21 21:08:02 +00005101 case QEMU_OPTION_alt_grab:
5102 alt_grab = 1;
5103 break;
ths667acca2006-12-11 02:08:05 +00005104 case QEMU_OPTION_no_quit:
5105 no_quit = 1;
5106 break;
aliguori7d957bd2009-01-15 22:14:11 +00005107 case QEMU_OPTION_sdl:
5108 sdl = 1;
5109 break;
ths667acca2006-12-11 02:08:05 +00005110#endif
bellardf7cce892004-12-08 22:21:25 +00005111 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005112 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005113 break;
bellarda09db212005-04-30 16:10:35 +00005114#ifdef TARGET_I386
5115 case QEMU_OPTION_win2k_hack:
5116 win2k_install_hack = 1;
5117 break;
aliguori73822ec2009-01-15 20:11:34 +00005118 case QEMU_OPTION_rtc_td_hack:
5119 rtc_td_hack = 1;
5120 break;
aliguori8a92ea22009-02-27 20:12:36 +00005121 case QEMU_OPTION_acpitable:
5122 if(acpi_table_add(optarg) < 0) {
5123 fprintf(stderr, "Wrong acpi table provided\n");
5124 exit(1);
5125 }
5126 break;
bellarda09db212005-04-30 16:10:35 +00005127#endif
bellardd993e022005-02-10 22:00:06 +00005128#ifdef USE_KQEMU
5129 case QEMU_OPTION_no_kqemu:
5130 kqemu_allowed = 0;
5131 break;
bellard89bfc102006-02-08 22:46:31 +00005132 case QEMU_OPTION_kernel_kqemu:
5133 kqemu_allowed = 2;
5134 break;
bellardd993e022005-02-10 22:00:06 +00005135#endif
aliguori7ba1e612008-11-05 16:04:33 +00005136#ifdef CONFIG_KVM
5137 case QEMU_OPTION_enable_kvm:
5138 kvm_allowed = 1;
5139#ifdef USE_KQEMU
5140 kqemu_allowed = 0;
5141#endif
5142 break;
5143#endif
bellardbb36d472005-11-05 14:22:28 +00005144 case QEMU_OPTION_usb:
5145 usb_enabled = 1;
5146 break;
bellarda594cfb2005-11-06 16:13:29 +00005147 case QEMU_OPTION_usbdevice:
5148 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00005149 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00005150 fprintf(stderr, "Too many USB devices\n");
5151 exit(1);
5152 }
pbrookfd5f3932008-03-26 20:55:43 +00005153 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00005154 usb_devices_index++;
5155 break;
bellard6a00d602005-11-21 23:25:50 +00005156 case QEMU_OPTION_smp:
5157 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00005158 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005159 fprintf(stderr, "Invalid number of CPUs\n");
5160 exit(1);
5161 }
5162 break;
bellard24236862006-04-30 21:28:36 +00005163 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00005164 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005165 break;
bellard6515b202006-05-03 22:02:44 +00005166 case QEMU_OPTION_no_acpi:
5167 acpi_enabled = 0;
5168 break;
aliguori16b29ae2008-12-17 23:28:44 +00005169 case QEMU_OPTION_no_hpet:
5170 no_hpet = 1;
5171 break;
bellardd1beab82006-10-02 19:44:22 +00005172 case QEMU_OPTION_no_reboot:
5173 no_reboot = 1;
5174 break;
aurel32b2f76162008-04-11 21:35:52 +00005175 case QEMU_OPTION_no_shutdown:
5176 no_shutdown = 1;
5177 break;
balrog9467cd42007-05-01 01:34:14 +00005178 case QEMU_OPTION_show_cursor:
5179 cursor_hide = 0;
5180 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005181 case QEMU_OPTION_uuid:
5182 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5183 fprintf(stderr, "Fail to parse UUID string."
5184 " Wrong format.\n");
5185 exit(1);
5186 }
5187 break;
ths71e3ceb2006-12-22 02:11:31 +00005188 case QEMU_OPTION_daemonize:
5189 daemonize = 1;
5190 break;
ths9ae02552007-01-05 17:39:04 +00005191 case QEMU_OPTION_option_rom:
5192 if (nb_option_roms >= MAX_OPTION_ROMS) {
5193 fprintf(stderr, "Too many option ROMs\n");
5194 exit(1);
5195 }
5196 option_rom[nb_option_roms] = optarg;
5197 nb_option_roms++;
5198 break;
pbrook8e716212007-01-20 17:12:09 +00005199 case QEMU_OPTION_semihosting:
5200 semihosting_enabled = 1;
5201 break;
thsc35734b2007-03-19 15:17:08 +00005202 case QEMU_OPTION_name:
5203 qemu_name = optarg;
5204 break;
blueswir195efd112008-12-24 20:26:14 +00005205#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005206 case QEMU_OPTION_prom_env:
5207 if (nb_prom_envs >= MAX_PROM_ENVS) {
5208 fprintf(stderr, "Too many prom variables\n");
5209 exit(1);
5210 }
5211 prom_envs[nb_prom_envs] = optarg;
5212 nb_prom_envs++;
5213 break;
5214#endif
balrog2b8f2d42007-07-27 22:08:46 +00005215#ifdef TARGET_ARM
5216 case QEMU_OPTION_old_param:
5217 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005218 break;
balrog2b8f2d42007-07-27 22:08:46 +00005219#endif
thsf3dcfad2007-08-24 01:26:02 +00005220 case QEMU_OPTION_clock:
5221 configure_alarms(optarg);
5222 break;
bellard7e0af5d02007-11-07 16:24:33 +00005223 case QEMU_OPTION_startdate:
5224 {
5225 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005226 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005227 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005228 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005229 } else {
5230 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5231 &tm.tm_year,
5232 &tm.tm_mon,
5233 &tm.tm_mday,
5234 &tm.tm_hour,
5235 &tm.tm_min,
5236 &tm.tm_sec) == 6) {
5237 /* OK */
5238 } else if (sscanf(optarg, "%d-%d-%d",
5239 &tm.tm_year,
5240 &tm.tm_mon,
5241 &tm.tm_mday) == 3) {
5242 tm.tm_hour = 0;
5243 tm.tm_min = 0;
5244 tm.tm_sec = 0;
5245 } else {
5246 goto date_fail;
5247 }
5248 tm.tm_year -= 1900;
5249 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005250 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005251 if (rtc_start_date == -1) {
5252 date_fail:
5253 fprintf(stderr, "Invalid date format. Valid format are:\n"
5254 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5255 exit(1);
5256 }
balrogf6503052008-02-17 11:42:19 +00005257 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005258 }
5259 }
5260 break;
bellard26a5f132008-05-28 12:30:31 +00005261 case QEMU_OPTION_tb_size:
5262 tb_size = strtol(optarg, NULL, 0);
5263 if (tb_size < 0)
5264 tb_size = 0;
5265 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005266 case QEMU_OPTION_icount:
5267 use_icount = 1;
5268 if (strcmp(optarg, "auto") == 0) {
5269 icount_time_shift = -1;
5270 } else {
5271 icount_time_shift = strtol(optarg, NULL, 0);
5272 }
5273 break;
aliguori5bb79102008-10-13 03:12:02 +00005274 case QEMU_OPTION_incoming:
5275 incoming = optarg;
5276 break;
aliguori08585322009-02-27 22:09:45 +00005277 case QEMU_OPTION_chroot:
5278 chroot_dir = optarg;
5279 break;
5280 case QEMU_OPTION_runas:
5281 run_as = optarg;
5282 break;
bellardcd6f1162004-05-13 22:02:20 +00005283 }
bellard0824d6f2003-06-24 13:42:40 +00005284 }
5285 }
bellard330d0412003-07-26 18:11:40 +00005286
aliguori7ba1e612008-11-05 16:04:33 +00005287#if defined(CONFIG_KVM) && defined(USE_KQEMU)
5288 if (kvm_allowed && kqemu_allowed) {
5289 fprintf(stderr,
5290 "You can not enable both KVM and kqemu at the same time\n");
5291 exit(1);
5292 }
5293#endif
5294
balrog3d878ca2008-10-28 10:59:59 +00005295 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005296 if (smp_cpus > machine->max_cpus) {
5297 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5298 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5299 machine->max_cpus);
5300 exit(1);
5301 }
5302
aliguoribc0129d2008-08-01 15:12:34 +00005303 if (nographic) {
5304 if (serial_device_index == 0)
5305 serial_devices[0] = "stdio";
5306 if (parallel_device_index == 0)
5307 parallel_devices[0] = "null";
5308 if (strncmp(monitor_device, "vc", 2) == 0)
5309 monitor_device = "stdio";
5310 }
5311
ths71e3ceb2006-12-22 02:11:31 +00005312#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005313 if (daemonize) {
5314 pid_t pid;
5315
5316 if (pipe(fds) == -1)
5317 exit(1);
5318
5319 pid = fork();
5320 if (pid > 0) {
5321 uint8_t status;
5322 ssize_t len;
5323
5324 close(fds[1]);
5325
5326 again:
ths93815bc2007-03-19 15:58:31 +00005327 len = read(fds[0], &status, 1);
5328 if (len == -1 && (errno == EINTR))
5329 goto again;
5330
5331 if (len != 1)
5332 exit(1);
5333 else if (status == 1) {
5334 fprintf(stderr, "Could not acquire pidfile\n");
5335 exit(1);
5336 } else
5337 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005338 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005339 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005340
5341 setsid();
5342
5343 pid = fork();
5344 if (pid > 0)
5345 exit(0);
5346 else if (pid < 0)
5347 exit(1);
5348
5349 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005350
5351 signal(SIGTSTP, SIG_IGN);
5352 signal(SIGTTOU, SIG_IGN);
5353 signal(SIGTTIN, SIG_IGN);
5354 }
5355#endif
5356
thsaa26bb22007-03-25 21:33:06 +00005357 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005358 if (daemonize) {
5359 uint8_t status = 1;
5360 write(fds[1], &status, 1);
5361 } else
5362 fprintf(stderr, "Could not acquire pid file\n");
5363 exit(1);
5364 }
5365
bellardff3fbb32006-01-08 10:53:14 +00005366#ifdef USE_KQEMU
5367 if (smp_cpus > 1)
5368 kqemu_allowed = 0;
5369#endif
bellarda20dd502003-09-30 21:07:02 +00005370 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00005371 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00005372
j_mayer28c5af52007-11-11 01:50:45 +00005373 if (!linux_boot && net_boot == 0 &&
blueswir1f88e4b92008-08-12 15:58:35 +00005374 !machine->nodisk_ok && nb_drives_opt == 0)
ths15f82202007-06-29 23:26:08 +00005375 help(1);
bellard0824d6f2003-06-24 13:42:40 +00005376
thsf8d39c02008-07-03 10:01:15 +00005377 if (!linux_boot && *kernel_cmdline != '\0') {
5378 fprintf(stderr, "-append only allowed with -kernel option\n");
5379 exit(1);
5380 }
5381
5382 if (!linux_boot && initrd_filename != NULL) {
5383 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5384 exit(1);
5385 }
5386
ths96d30e42007-01-07 20:42:14 +00005387 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00005388 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00005389 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00005390 }
bellardb118d612003-06-30 23:36:21 +00005391 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005392
pbrook634fce92006-07-15 17:40:09 +00005393 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005394 if (init_timer_alarm() < 0) {
5395 fprintf(stderr, "could not initialize alarm timer\n");
5396 exit(1);
5397 }
pbrook2e70f6e2008-06-29 01:03:05 +00005398 if (use_icount && icount_time_shift < 0) {
5399 use_icount = 2;
5400 /* 125MIPS seems a reasonable initial guess at the guest speed.
5401 It will be corrected fairly quickly anyway. */
5402 icount_time_shift = 3;
5403 init_icount_adjust();
5404 }
pbrook634fce92006-07-15 17:40:09 +00005405
bellardfd1dff42006-02-01 21:29:26 +00005406#ifdef _WIN32
5407 socket_init();
5408#endif
5409
bellard7c9d8e02005-11-15 22:16:05 +00005410 /* init network clients */
5411 if (nb_net_clients == 0) {
5412 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005413 net_clients[nb_net_clients++] = "nic";
5414#ifdef CONFIG_SLIRP
5415 net_clients[nb_net_clients++] = "user";
5416#endif
bellardc20709a2004-04-21 23:27:19 +00005417 }
5418
bellard7c9d8e02005-11-15 22:16:05 +00005419 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005420 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005421 exit(1);
bellard702c6512004-04-02 21:21:32 +00005422 }
aliguori63a01ef2008-10-31 19:10:00 +00005423 net_client_check();
bellardf1510b22003-06-25 00:07:40 +00005424
thseec85c22007-01-05 17:41:07 +00005425#ifdef TARGET_I386
balroged494d82007-12-11 23:23:52 +00005426 /* XXX: this should be moved in the PC machine instantiation code */
j_mayer28c5af52007-11-11 01:50:45 +00005427 if (net_boot != 0) {
5428 int netroms = 0;
5429 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00005430 const char *model = nd_table[i].model;
5431 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00005432 if (net_boot & (1 << i)) {
5433 if (model == NULL)
5434 model = "ne2k_pci";
5435 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5436 if (get_image_size(buf) > 0) {
5437 if (nb_option_roms >= MAX_OPTION_ROMS) {
5438 fprintf(stderr, "Too many option ROMs\n");
5439 exit(1);
5440 }
5441 option_rom[nb_option_roms] = strdup(buf);
5442 nb_option_roms++;
5443 netroms++;
5444 }
5445 }
thseec85c22007-01-05 17:41:07 +00005446 }
j_mayer28c5af52007-11-11 01:50:45 +00005447 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00005448 fprintf(stderr, "No valid PXE rom found for network device\n");
5449 exit(1);
5450 }
thseec85c22007-01-05 17:41:07 +00005451 }
5452#endif
5453
balrogdc72ac12008-11-09 00:04:26 +00005454 /* init the bluetooth world */
5455 for (i = 0; i < nb_bt_opts; i++)
5456 if (bt_parse(bt_opts[i]))
5457 exit(1);
5458
bellard0824d6f2003-06-24 13:42:40 +00005459 /* init the memory */
balrog7fb4fdc2008-04-24 17:59:27 +00005460 phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
5461
5462 if (machine->ram_require & RAMSIZE_FIXED) {
5463 if (ram_size > 0) {
5464 if (ram_size < phys_ram_size) {
aurel32cd940062008-04-28 20:26:54 +00005465 fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
5466 machine->name, (unsigned long long) phys_ram_size);
balrog7fb4fdc2008-04-24 17:59:27 +00005467 exit(-1);
5468 }
5469
5470 phys_ram_size = ram_size;
5471 } else
5472 ram_size = phys_ram_size;
5473 } else {
aurel324fc5d072008-04-27 21:39:40 +00005474 if (ram_size == 0)
balrog7fb4fdc2008-04-24 17:59:27 +00005475 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5476
5477 phys_ram_size += ram_size;
5478 }
ths9ae02552007-01-05 17:39:04 +00005479
bellardd993e022005-02-10 22:00:06 +00005480 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00005481 if (!phys_ram_base) {
5482 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00005483 exit(1);
5484 }
5485
bellard26a5f132008-05-28 12:30:31 +00005486 /* init the dynamic translator */
5487 cpu_exec_init_all(tb_size * 1024 * 1024);
5488
bellard5905b2e2004-08-01 21:53:26 +00005489 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005490
5491 /* we always create the cdrom drive, even if no disk is there */
5492
5493 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005494 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005495
balrog9d413d12007-12-04 00:10:34 +00005496 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005497
5498 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005499 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005500
balrog9d413d12007-12-04 00:10:34 +00005501 /* we always create one sd slot, even if no card is in it */
5502
5503 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005504 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005505
ths96d30e42007-01-07 20:42:14 +00005506 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005507
thse4bcb142007-12-02 04:51:10 +00005508 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005509 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005510 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005511
bellardc88676f2006-08-06 13:36:11 +00005512 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005513 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005514
aliguori3023f332009-01-16 19:04:14 +00005515#ifndef _WIN32
5516 /* must be after terminal init, SDL library changes signal handlers */
5517 termsig_setup();
5518#endif
5519
5520 /* Maintain compatibility with multiple stdio monitors */
5521 if (!strcmp(monitor_device,"stdio")) {
5522 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5523 const char *devname = serial_devices[i];
5524 if (devname && !strcmp(devname,"mon:stdio")) {
5525 monitor_device = NULL;
5526 break;
5527 } else if (devname && !strcmp(devname,"stdio")) {
5528 monitor_device = NULL;
5529 serial_devices[i] = "mon:stdio";
5530 break;
5531 }
5532 }
5533 }
5534
5535 if (kvm_enabled()) {
5536 int ret;
5537
5538 ret = kvm_init(smp_cpus);
5539 if (ret < 0) {
5540 fprintf(stderr, "failed to initialize KVM\n");
5541 exit(1);
5542 }
5543 }
5544
aliguori4c621802009-01-16 21:48:20 +00005545 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005546 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005547 if (!monitor_hd) {
5548 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5549 exit(1);
5550 }
5551 }
5552
aliguori2796dae2009-01-16 20:23:27 +00005553 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5554 const char *devname = serial_devices[i];
5555 if (devname && strcmp(devname, "none")) {
5556 char label[32];
5557 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005558 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005559 if (!serial_hds[i]) {
5560 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5561 devname);
5562 exit(1);
5563 }
5564 }
5565 }
5566
5567 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5568 const char *devname = parallel_devices[i];
5569 if (devname && strcmp(devname, "none")) {
5570 char label[32];
5571 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005572 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005573 if (!parallel_hds[i]) {
5574 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5575 devname);
5576 exit(1);
5577 }
5578 }
5579 }
5580
5581 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5582 const char *devname = virtio_consoles[i];
5583 if (devname && strcmp(devname, "none")) {
5584 char label[32];
5585 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005586 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005587 if (!virtcon_hds[i]) {
5588 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5589 devname);
5590 exit(1);
5591 }
5592 }
5593 }
5594
aliguori3023f332009-01-16 19:04:14 +00005595 machine->init(ram_size, vga_ram_size, boot_devices,
5596 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5597
aliguori6f338c32009-02-11 15:21:54 +00005598 current_machine = machine;
5599
aliguori3023f332009-01-16 19:04:14 +00005600 /* Set KVM's vcpu state to qemu's initial CPUState. */
5601 if (kvm_enabled()) {
5602 int ret;
5603
5604 ret = kvm_sync_vcpus();
5605 if (ret < 0) {
5606 fprintf(stderr, "failed to initialize vcpus\n");
5607 exit(1);
5608 }
5609 }
5610
5611 /* init USB devices */
5612 if (usb_enabled) {
5613 for(i = 0; i < usb_devices_index; i++) {
aliguoric0f4ce72009-03-05 23:01:01 +00005614 if (usb_device_add(usb_devices[i], 0) < 0) {
aliguori3023f332009-01-16 19:04:14 +00005615 fprintf(stderr, "Warning: could not add USB device %s\n",
5616 usb_devices[i]);
5617 }
5618 }
5619 }
5620
aliguori8f391ab2009-01-19 16:34:10 +00005621 if (!display_state)
5622 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005623 /* just use the first displaystate for the moment */
5624 ds = display_state;
bellard313aa562003-08-10 21:52:11 +00005625 /* terminal init */
bellarda20dd502003-09-30 21:07:02 +00005626 if (nographic) {
balrog4d3b6f62008-02-10 16:33:14 +00005627 if (curses) {
5628 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5629 exit(1);
5630 }
aliguori7d957bd2009-01-15 22:14:11 +00005631 } else {
balrog4d3b6f62008-02-10 16:33:14 +00005632#if defined(CONFIG_CURSES)
aliguori7d957bd2009-01-15 22:14:11 +00005633 if (curses) {
5634 /* At the moment curses cannot be used with other displays */
5635 curses_display_init(ds, full_screen);
5636 } else
balrog4d3b6f62008-02-10 16:33:14 +00005637#endif
aliguori7d957bd2009-01-15 22:14:11 +00005638 {
5639 if (vnc_display != NULL) {
5640 vnc_display_init(ds);
5641 if (vnc_display_open(ds, vnc_display) < 0)
5642 exit(1);
5643 }
bellard5b0753e2005-03-01 21:37:28 +00005644#if defined(CONFIG_SDL)
aliguorid268de02009-01-22 16:18:33 +00005645 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005646 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00005647#elif defined(CONFIG_COCOA)
aliguorid268de02009-01-22 16:18:33 +00005648 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005649 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00005650#endif
aliguori7d957bd2009-01-15 22:14:11 +00005651 }
bellard313aa562003-08-10 21:52:11 +00005652 }
aliguori7d957bd2009-01-15 22:14:11 +00005653 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005654
aliguori3023f332009-01-16 19:04:14 +00005655 dcl = ds->listeners;
5656 while (dcl != NULL) {
5657 if (dcl->dpy_refresh != NULL) {
5658 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5659 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005660 }
aliguori3023f332009-01-16 19:04:14 +00005661 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005662 }
aliguori3023f332009-01-16 19:04:14 +00005663
blueswir19043b622009-01-21 19:28:13 +00005664 if (nographic || (vnc_display && !sdl)) {
5665 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5666 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5667 }
5668
aliguori2796dae2009-01-16 20:23:27 +00005669 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00005670 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00005671
aliguori4c621802009-01-16 21:48:20 +00005672 if (monitor_device && monitor_hd)
ths20d8a3e2007-02-18 17:04:49 +00005673 monitor_init(monitor_hd, !nographic);
bellard82c643f2004-07-14 17:28:13 +00005674
bellard8d11df92004-08-24 21:13:40 +00005675 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005676 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005677 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005678 char label[32];
5679 snprintf(label, sizeof(label), "serial%d", i);
thsaf3a9032007-07-11 23:14:59 +00005680 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005681 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005682 }
bellard82c643f2004-07-14 17:28:13 +00005683 }
bellard82c643f2004-07-14 17:28:13 +00005684
bellard6508fe52005-01-15 12:02:56 +00005685 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005686 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005687 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005688 char label[32];
5689 snprintf(label, sizeof(label), "parallel%d", i);
thsaf3a9032007-07-11 23:14:59 +00005690 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005691 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005692 }
5693 }
5694
aliguori9ede2fd2009-01-15 20:05:25 +00005695 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5696 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005697 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005698 char label[32];
5699 snprintf(label, sizeof(label), "virtcon%d", i);
aliguori9ede2fd2009-01-15 20:05:25 +00005700 if (strstart(devname, "vc", 0))
5701 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5702 }
5703 }
5704
bellard67b915a2004-03-31 23:37:16 +00005705#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00005706 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00005707 /* XXX: use standard host:port notation and modify options
5708 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00005709 if (gdbserver_start(gdbstub_port) < 0) {
5710 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00005711 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00005712 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00005713 }
balrog45669e02007-07-02 13:20:17 +00005714 }
bellard67b915a2004-03-31 23:37:16 +00005715#endif
balrog45669e02007-07-02 13:20:17 +00005716
bellardd63d3072004-10-03 13:29:03 +00005717 if (loadvm)
bellardfaea38e2006-08-05 21:31:00 +00005718 do_loadvm(loadvm);
bellardd63d3072004-10-03 13:29:03 +00005719
aliguori5bb79102008-10-13 03:12:02 +00005720 if (incoming) {
5721 autostart = 0; /* fixme how to deal with -daemonize */
5722 qemu_start_incoming_migration(incoming);
5723 }
5724
aliguoric0f4ce72009-03-05 23:01:01 +00005725 if (autostart)
5726 vm_start();
thsffd843b2006-12-21 19:46:43 +00005727
ths71e3ceb2006-12-22 02:11:31 +00005728 if (daemonize) {
5729 uint8_t status = 0;
5730 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005731
5732 again1:
5733 len = write(fds[1], &status, 1);
5734 if (len == -1 && (errno == EINTR))
5735 goto again1;
5736
5737 if (len != 1)
5738 exit(1);
5739
aliguoribd54b862008-07-23 00:58:33 +00005740 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005741 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005742 if (fd == -1)
5743 exit(1);
aliguori08585322009-02-27 22:09:45 +00005744 }
ths71e3ceb2006-12-22 02:11:31 +00005745
aliguori08585322009-02-27 22:09:45 +00005746#ifndef _WIN32
5747 if (run_as) {
5748 pwd = getpwnam(run_as);
5749 if (!pwd) {
5750 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5751 exit(1);
5752 }
5753 }
ths71e3ceb2006-12-22 02:11:31 +00005754
aliguori08585322009-02-27 22:09:45 +00005755 if (chroot_dir) {
5756 if (chroot(chroot_dir) < 0) {
5757 fprintf(stderr, "chroot failed\n");
5758 exit(1);
5759 }
5760 chdir("/");
5761 }
5762
5763 if (run_as) {
5764 if (setgid(pwd->pw_gid) < 0) {
5765 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5766 exit(1);
5767 }
5768 if (setuid(pwd->pw_uid) < 0) {
5769 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5770 exit(1);
5771 }
5772 if (setuid(0) != -1) {
5773 fprintf(stderr, "Dropping privileges failed\n");
5774 exit(1);
5775 }
5776 }
5777#endif
5778
5779 if (daemonize) {
5780 dup2(fd, 0);
5781 dup2(fd, 1);
5782 dup2(fd, 2);
5783
5784 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00005785 }
5786
bellard8a7ddc32004-03-31 19:00:16 +00005787 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005788 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005789 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005790
bellard0824d6f2003-06-24 13:42:40 +00005791 return 0;
5792}