blob: 804c835e4e54bf5cd7736558990596dff0ab4b2e [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
blueswir1d40cdb12009-03-07 16:52:02 +000032/* Needed early for _BSD etc. */
33#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +000036#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000038#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000039#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000042#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000043#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000044#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <net/if.h>
46#if defined(__NetBSD__)
47#include <net/if_tap.h>
48#endif
49#ifdef __linux__
50#include <linux/if_tun.h>
51#endif
52#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000053#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000054#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000055#include <sys/select.h>
bellard7d3505c2004-05-12 19:32:15 +000056#ifdef _BSD
57#include <sys/stat.h>
blueswir124646c72008-11-07 16:55:48 +000058#ifdef __FreeBSD__
bellard7d3505c2004-05-12 19:32:15 +000059#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000060#else
61#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000062#endif
ths5c40d2b2007-06-23 16:03:36 +000063#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
64#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000065#else
blueswir1223f0d72008-09-30 18:12:18 +000066#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000067#include <pty.h>
68#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000069#include <linux/rtc.h>
thsbd494f42007-09-16 20:03:23 +000070
71/* For the benefit of older linux systems which don't supply it,
72 we use a local copy of hpet.h. */
73/* #include <linux/hpet.h> */
74#include "hpet.h"
75
bellarde57a8c02005-11-10 23:58:52 +000076#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000077#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000078#endif
79#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000080#include <sys/stat.h>
81#include <sys/ethernet.h>
82#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000083#include <netinet/arp.h>
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#include <netinet/ip_icmp.h> // must come after ip.h
88#include <netinet/udp.h>
89#include <netinet/tcp.h>
90#include <net/if.h>
91#include <syslog.h>
92#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000093#endif
bellard7d3505c2004-05-12 19:32:15 +000094#endif
bellardec530c82006-04-25 22:36:06 +000095#endif
bellard67b915a2004-03-31 23:37:16 +000096
blueswir19892fbf2008-08-24 10:34:20 +000097#if defined(__OpenBSD__)
98#include <util.h>
99#endif
100
ths8a16d272008-07-19 09:56:24 +0000101#if defined(CONFIG_VDE)
102#include <libvdeplug.h>
103#endif
104
bellard67b915a2004-03-31 23:37:16 +0000105#ifdef _WIN32
bellard7d3505c2004-05-12 19:32:15 +0000106#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000107#include <sys/timeb.h>
ths4fddf622007-12-17 04:42:29 +0000108#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000109#define getopt_long_only getopt_long
110#define memalign(align, size) malloc(size)
111#endif
112
bellard73332e52004-04-04 20:22:28 +0000113#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +0000114#ifdef __APPLE__
bellard83fb7ad2004-07-05 21:25:26 +0000115#include <SDL/SDL.h>
malc880fec52009-02-15 20:18:41 +0000116int qemu_main(int argc, char **argv, char **envp);
117int main(int argc, char **argv)
118{
119 qemu_main(argc, argv, NULL);
120}
121#undef main
122#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000123#endif
bellard73332e52004-04-04 20:22:28 +0000124#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000125
bellard5b0753e2005-03-01 21:37:28 +0000126#ifdef CONFIG_COCOA
127#undef main
128#define main qemu_main
129#endif /* CONFIG_COCOA */
130
blueswir1511d2b12009-03-07 15:32:56 +0000131#include "hw/hw.h"
132#include "hw/boards.h"
133#include "hw/usb.h"
134#include "hw/pcmcia.h"
135#include "hw/pc.h"
136#include "hw/audiodev.h"
137#include "hw/isa.h"
138#include "hw/baum.h"
139#include "hw/bt.h"
140#include "net.h"
141#include "monitor.h"
142#include "console.h"
143#include "sysemu.h"
144#include "gdbstub.h"
145#include "qemu-timer.h"
146#include "qemu-char.h"
147#include "cache-utils.h"
148#include "block.h"
149#include "audio/audio.h"
150#include "migration.h"
151#include "kvm.h"
152#include "balloon.h"
153
bellard0824d6f2003-06-24 13:42:40 +0000154#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000155
bellard8a7ddc32004-03-31 19:00:16 +0000156#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000157
blueswir1511d2b12009-03-07 15:32:56 +0000158#include "qemu_socket.h"
159
160#if defined(CONFIG_SLIRP)
161#include "libslirp.h"
162#endif
163
bellard0824d6f2003-06-24 13:42:40 +0000164//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000165//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000166//#define DEBUG_NET
167//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000168
aliguorid12d51d2009-01-15 21:48:06 +0000169
170#ifdef DEBUG_IOPORT
aliguori93fcfe32009-01-15 22:34:14 +0000171# define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
aliguorid12d51d2009-01-15 21:48:06 +0000172#else
173# define LOG_IOPORT(...) do { } while (0)
174#endif
175
bellard1bfe8562004-07-08 21:17:50 +0000176#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000177
pbrook0d92ed32006-05-21 16:30:15 +0000178/* Max number of USB devices that can be specified on the commandline. */
179#define MAX_USB_CMDLINE 8
180
balrogdc72ac12008-11-09 00:04:26 +0000181/* Max number of bluetooth switches on the commandline. */
182#define MAX_BT_CMDLINE 10
183
bellard7dea1da2003-11-16 15:59:30 +0000184/* XXX: use a two level table to limit memory usage */
185#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000186
bellard80cabfa2004-03-14 12:20:30 +0000187const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000188const char *bios_name = NULL;
blueswir1dbed7e42008-10-01 19:38:09 +0000189static void *ioport_opaque[MAX_IOPORTS];
190static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
191static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
thse4bcb142007-12-02 04:51:10 +0000192/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000193 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000194DriveInfo drives_table[MAX_DRIVES+1];
195int nb_drives;
blueswir1dbed7e42008-10-01 19:38:09 +0000196static int vga_ram_size;
malccb5a7aa2008-09-28 00:42:12 +0000197enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000198static DisplayState *display_state;
bellarda20dd502003-09-30 21:07:02 +0000199int nographic;
blueswir1dbed7e42008-10-01 19:38:09 +0000200static int curses;
aliguori7d957bd2009-01-15 22:14:11 +0000201static int sdl;
bellard3d11d0e2004-12-12 16:56:30 +0000202const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000203int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000204ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000205int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000206NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000207int vm_running;
aliguoric0f4ce72009-03-05 23:01:01 +0000208static int autostart;
balrogf6503052008-02-17 11:42:19 +0000209static int rtc_utc = 1;
210static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000211int cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +0000212int std_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000213int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000214#ifdef TARGET_SPARC
215int graphic_width = 1024;
216int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000217int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000218#else
bellard1bfe8562004-07-08 21:17:50 +0000219int graphic_width = 800;
220int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000221int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000222#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000223static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000224#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000225static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000226#endif
ths667acca2006-12-11 02:08:05 +0000227int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000228CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000229CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000230CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000231#ifdef TARGET_I386
232int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000233int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000234#endif
bellardbb36d472005-11-05 14:22:28 +0000235int usb_enabled = 0;
bellard6a00d602005-11-21 23:25:50 +0000236int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000237const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000238int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000239int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000240int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000241int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000242int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000243int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000244int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000245int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000246const char *option_rom[MAX_OPTION_ROMS];
247int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000248int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000249#ifdef TARGET_ARM
250int old_param = 0;
251#endif
thsc35734b2007-03-19 15:17:08 +0000252const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000253int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000254#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000255unsigned int nb_prom_envs = 0;
256const char *prom_envs[MAX_PROM_ENVS];
257#endif
aliguoriec691c82009-02-11 15:20:37 +0000258int nb_drives_opt;
259struct drive_opt drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000260
balrogee5605e2007-12-03 03:01:40 +0000261static CPUState *cur_cpu;
262static CPUState *next_cpu;
balrog76ea08f2007-12-16 11:48:54 +0000263static int event_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000264/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000265static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000266/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000267#define MAX_ICOUNT_SHIFT 10
268/* Compensate for varying guest execution speed. */
269static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000270static QEMUTimer *icount_rt_timer;
271static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000272static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000273
blueswir18fcb1b92008-09-18 18:29:08 +0000274uint8_t qemu_uuid[16];
275
bellard0824d6f2003-06-24 13:42:40 +0000276/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000277/* x86 ISA bus support */
278
279target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000280PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000281
aliguori477e3ed2008-07-23 15:19:59 +0000282static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
283static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
284
285static uint32_t ioport_read(int index, uint32_t address)
286{
287 static IOPortReadFunc *default_func[3] = {
288 default_ioport_readb,
289 default_ioport_readw,
290 default_ioport_readl
291 };
292 IOPortReadFunc *func = ioport_read_table[index][address];
293 if (!func)
294 func = default_func[index];
295 return func(ioport_opaque[address], address);
296}
297
298static void ioport_write(int index, uint32_t address, uint32_t data)
299{
300 static IOPortWriteFunc *default_func[3] = {
301 default_ioport_writeb,
302 default_ioport_writew,
303 default_ioport_writel
304 };
305 IOPortWriteFunc *func = ioport_write_table[index][address];
306 if (!func)
307 func = default_func[index];
308 func(ioport_opaque[address], address, data);
309}
310
pbrook9596ebb2007-11-18 01:44:38 +0000311static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000312{
313#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000314 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000315#endif
bellardfc01f7e2003-06-30 10:03:06 +0000316 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000317}
318
pbrook9596ebb2007-11-18 01:44:38 +0000319static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000320{
321#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000322 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000323#endif
324}
325
326/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000327static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000328{
329 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000330 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000331 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000332 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000333 return data;
334}
335
pbrook9596ebb2007-11-18 01:44:38 +0000336static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000337{
aliguori477e3ed2008-07-23 15:19:59 +0000338 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000339 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000340 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000341}
342
pbrook9596ebb2007-11-18 01:44:38 +0000343static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000344{
345#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000346 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000347#endif
348 return 0xffffffff;
349}
350
pbrook9596ebb2007-11-18 01:44:38 +0000351static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000352{
353#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000354 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000355#endif
bellard0824d6f2003-06-24 13:42:40 +0000356}
357
bellardfc01f7e2003-06-30 10:03:06 +0000358/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000359int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000360 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000361{
bellardfc01f7e2003-06-30 10:03:06 +0000362 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000363
bellardc4b1fcc2004-03-14 21:44:30 +0000364 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000365 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000366 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000367 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000368 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000369 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000370 } else {
balrog88fdf562008-04-26 21:11:22 +0000371 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000372 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000373 }
374 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000375 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000376 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000377 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000378 ioport_opaque[i] = opaque;
379 }
bellard0824d6f2003-06-24 13:42:40 +0000380 return 0;
381}
382
bellardfc01f7e2003-06-30 10:03:06 +0000383/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000384int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000385 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000386{
bellardfc01f7e2003-06-30 10:03:06 +0000387 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000388
bellardc4b1fcc2004-03-14 21:44:30 +0000389 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000390 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000391 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000392 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000393 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000394 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000395 } else {
balrog88fdf562008-04-26 21:11:22 +0000396 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000397 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000398 }
399 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000400 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000401 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
402 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000403 ioport_opaque[i] = opaque;
404 }
bellardf1510b22003-06-25 00:07:40 +0000405 return 0;
406}
407
bellard69b91032004-05-18 23:05:28 +0000408void isa_unassign_ioport(int start, int length)
409{
410 int i;
411
412 for(i = start; i < start + length; i++) {
413 ioport_read_table[0][i] = default_ioport_readb;
414 ioport_read_table[1][i] = default_ioport_readw;
415 ioport_read_table[2][i] = default_ioport_readl;
416
417 ioport_write_table[0][i] = default_ioport_writeb;
418 ioport_write_table[1][i] = default_ioport_writew;
419 ioport_write_table[2][i] = default_ioport_writel;
aliguoria7607f72009-02-11 15:21:29 +0000420
421 ioport_opaque[i] = NULL;
bellard69b91032004-05-18 23:05:28 +0000422 }
423}
424
bellard20f32282005-01-03 23:36:21 +0000425/***********************************************************/
426
bellardc45886d2004-01-05 00:02:06 +0000427void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000428{
aliguorid12d51d2009-01-15 21:48:06 +0000429 LOG_IOPORT("outb: %04x %02x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000430 ioport_write(0, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000431#ifdef USE_KQEMU
432 if (env)
433 env->last_io_time = cpu_get_time_fast();
434#endif
bellard0824d6f2003-06-24 13:42:40 +0000435}
436
bellardc45886d2004-01-05 00:02:06 +0000437void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000438{
aliguorid12d51d2009-01-15 21:48:06 +0000439 LOG_IOPORT("outw: %04x %04x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000440 ioport_write(1, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000441#ifdef USE_KQEMU
442 if (env)
443 env->last_io_time = cpu_get_time_fast();
444#endif
bellard0824d6f2003-06-24 13:42:40 +0000445}
446
bellardc45886d2004-01-05 00:02:06 +0000447void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000448{
aliguorid12d51d2009-01-15 21:48:06 +0000449 LOG_IOPORT("outl: %04x %08x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000450 ioport_write(2, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000451#ifdef USE_KQEMU
452 if (env)
453 env->last_io_time = cpu_get_time_fast();
454#endif
bellard0824d6f2003-06-24 13:42:40 +0000455}
456
bellardc45886d2004-01-05 00:02:06 +0000457int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000458{
bellardfd872592004-05-12 19:11:15 +0000459 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000460 val = ioport_read(0, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000461 LOG_IOPORT("inb : %04x %02x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000462#ifdef USE_KQEMU
463 if (env)
464 env->last_io_time = cpu_get_time_fast();
465#endif
bellardfd872592004-05-12 19:11:15 +0000466 return val;
bellard0824d6f2003-06-24 13:42:40 +0000467}
468
bellardc45886d2004-01-05 00:02:06 +0000469int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000470{
bellardfd872592004-05-12 19:11:15 +0000471 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000472 val = ioport_read(1, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000473 LOG_IOPORT("inw : %04x %04x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000474#ifdef USE_KQEMU
475 if (env)
476 env->last_io_time = cpu_get_time_fast();
477#endif
bellardfd872592004-05-12 19:11:15 +0000478 return val;
bellard0824d6f2003-06-24 13:42:40 +0000479}
480
bellardc45886d2004-01-05 00:02:06 +0000481int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000482{
bellardfd872592004-05-12 19:11:15 +0000483 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000484 val = ioport_read(2, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000485 LOG_IOPORT("inl : %04x %08x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000486#ifdef USE_KQEMU
487 if (env)
488 env->last_io_time = cpu_get_time_fast();
489#endif
bellardfd872592004-05-12 19:11:15 +0000490 return val;
bellard0824d6f2003-06-24 13:42:40 +0000491}
492
493/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000494void hw_error(const char *fmt, ...)
495{
496 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000497 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000498
499 va_start(ap, fmt);
500 fprintf(stderr, "qemu: hardware error: ");
501 vfprintf(stderr, fmt, ap);
502 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000503 for(env = first_cpu; env != NULL; env = env->next_cpu) {
504 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000505#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000506 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000507#else
bellard6a00d602005-11-21 23:25:50 +0000508 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000509#endif
bellard6a00d602005-11-21 23:25:50 +0000510 }
bellard0824d6f2003-06-24 13:42:40 +0000511 va_end(ap);
512 abort();
513}
aliguoridf751fa2008-12-04 20:19:35 +0000514
515/***************/
516/* ballooning */
517
518static QEMUBalloonEvent *qemu_balloon_event;
519void *qemu_balloon_event_opaque;
520
521void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
522{
523 qemu_balloon_event = func;
524 qemu_balloon_event_opaque = opaque;
525}
526
527void qemu_balloon(ram_addr_t target)
528{
529 if (qemu_balloon_event)
530 qemu_balloon_event(qemu_balloon_event_opaque, target);
531}
532
533ram_addr_t qemu_balloon_status(void)
534{
535 if (qemu_balloon_event)
536 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
537 return 0;
538}
bellard0824d6f2003-06-24 13:42:40 +0000539
bellard8a7ddc32004-03-31 19:00:16 +0000540/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000541/* keyboard/mouse */
542
543static QEMUPutKBDEvent *qemu_put_kbd_event;
544static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000545static QEMUPutMouseEntry *qemu_put_mouse_event_head;
546static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000547
548void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
549{
550 qemu_put_kbd_event_opaque = opaque;
551 qemu_put_kbd_event = func;
552}
553
ths455204e2007-01-05 16:42:13 +0000554QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
555 void *opaque, int absolute,
556 const char *name)
bellard63066f42004-06-03 18:45:02 +0000557{
ths455204e2007-01-05 16:42:13 +0000558 QEMUPutMouseEntry *s, *cursor;
559
560 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000561
562 s->qemu_put_mouse_event = func;
563 s->qemu_put_mouse_event_opaque = opaque;
564 s->qemu_put_mouse_event_absolute = absolute;
565 s->qemu_put_mouse_event_name = qemu_strdup(name);
566 s->next = NULL;
567
568 if (!qemu_put_mouse_event_head) {
569 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
570 return s;
571 }
572
573 cursor = qemu_put_mouse_event_head;
574 while (cursor->next != NULL)
575 cursor = cursor->next;
576
577 cursor->next = s;
578 qemu_put_mouse_event_current = s;
579
580 return s;
581}
582
583void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
584{
585 QEMUPutMouseEntry *prev = NULL, *cursor;
586
587 if (!qemu_put_mouse_event_head || entry == NULL)
588 return;
589
590 cursor = qemu_put_mouse_event_head;
591 while (cursor != NULL && cursor != entry) {
592 prev = cursor;
593 cursor = cursor->next;
594 }
595
596 if (cursor == NULL) // does not exist or list empty
597 return;
598 else if (prev == NULL) { // entry is head
599 qemu_put_mouse_event_head = cursor->next;
600 if (qemu_put_mouse_event_current == entry)
601 qemu_put_mouse_event_current = cursor->next;
602 qemu_free(entry->qemu_put_mouse_event_name);
603 qemu_free(entry);
604 return;
605 }
606
607 prev->next = entry->next;
608
609 if (qemu_put_mouse_event_current == entry)
610 qemu_put_mouse_event_current = prev;
611
612 qemu_free(entry->qemu_put_mouse_event_name);
613 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000614}
615
616void kbd_put_keycode(int keycode)
617{
618 if (qemu_put_kbd_event) {
619 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
620 }
621}
622
623void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
624{
ths455204e2007-01-05 16:42:13 +0000625 QEMUPutMouseEvent *mouse_event;
626 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000627 int width;
ths455204e2007-01-05 16:42:13 +0000628
629 if (!qemu_put_mouse_event_current) {
630 return;
631 }
632
633 mouse_event =
634 qemu_put_mouse_event_current->qemu_put_mouse_event;
635 mouse_event_opaque =
636 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
637
638 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000639 if (graphic_rotate) {
640 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
641 width = 0x7fff;
642 else
aurel32b94ed572008-03-10 19:34:27 +0000643 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000644 mouse_event(mouse_event_opaque,
645 width - dy, dx, dz, buttons_state);
646 } else
647 mouse_event(mouse_event_opaque,
648 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000649 }
650}
651
bellard09b26c52006-04-12 21:09:08 +0000652int kbd_mouse_is_absolute(void)
653{
ths455204e2007-01-05 16:42:13 +0000654 if (!qemu_put_mouse_event_current)
655 return 0;
656
657 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
658}
659
aliguori376253e2009-03-05 23:01:23 +0000660void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000661{
662 QEMUPutMouseEntry *cursor;
663 int index = 0;
664
665 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000666 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000667 return;
668 }
669
aliguori376253e2009-03-05 23:01:23 +0000670 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000671 cursor = qemu_put_mouse_event_head;
672 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000673 monitor_printf(mon, "%c Mouse #%d: %s\n",
674 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
675 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000676 index++;
677 cursor = cursor->next;
678 }
679}
680
aliguori376253e2009-03-05 23:01:23 +0000681void do_mouse_set(Monitor *mon, int index)
ths455204e2007-01-05 16:42:13 +0000682{
683 QEMUPutMouseEntry *cursor;
684 int i = 0;
685
686 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000687 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000688 return;
689 }
690
691 cursor = qemu_put_mouse_event_head;
692 while (cursor != NULL && index != i) {
693 i++;
694 cursor = cursor->next;
695 }
696
697 if (cursor != NULL)
698 qemu_put_mouse_event_current = cursor;
699 else
aliguori376253e2009-03-05 23:01:23 +0000700 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000701}
702
bellard87858c82003-06-27 12:01:39 +0000703/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000704uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000705{
706 union {
707 uint64_t ll;
708 struct {
709#ifdef WORDS_BIGENDIAN
710 uint32_t high, low;
711#else
712 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000713#endif
bellard87858c82003-06-27 12:01:39 +0000714 } l;
715 } u, res;
716 uint64_t rl, rh;
717
718 u.ll = a;
719 rl = (uint64_t)u.l.low * (uint64_t)b;
720 rh = (uint64_t)u.l.high * (uint64_t)b;
721 rh += (rl >> 32);
722 res.l.high = rh / c;
723 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
724 return res.ll;
725}
726
bellard1dce7c32006-07-13 23:20:22 +0000727/***********************************************************/
728/* real time host monotonic timer */
729
730#define QEMU_TIMER_BASE 1000000000LL
731
732#ifdef WIN32
733
734static int64_t clock_freq;
735
736static void init_get_clock(void)
737{
bellarda8e5ac32006-07-14 09:36:13 +0000738 LARGE_INTEGER freq;
739 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000740 ret = QueryPerformanceFrequency(&freq);
741 if (ret == 0) {
742 fprintf(stderr, "Could not calibrate ticks\n");
743 exit(1);
744 }
745 clock_freq = freq.QuadPart;
746}
747
748static int64_t get_clock(void)
749{
750 LARGE_INTEGER ti;
751 QueryPerformanceCounter(&ti);
752 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
753}
754
755#else
756
757static int use_rt_clock;
758
759static void init_get_clock(void)
760{
761 use_rt_clock = 0;
aurel3260759372008-10-13 21:08:25 +0000762#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000763 {
764 struct timespec ts;
765 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
766 use_rt_clock = 1;
767 }
768 }
769#endif
770}
771
772static int64_t get_clock(void)
773{
aurel3260759372008-10-13 21:08:25 +0000774#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000775 if (use_rt_clock) {
776 struct timespec ts;
777 clock_gettime(CLOCK_MONOTONIC, &ts);
778 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000779 } else
bellard1dce7c32006-07-13 23:20:22 +0000780#endif
781 {
782 /* XXX: using gettimeofday leads to problems if the date
783 changes, so it should be avoided. */
784 struct timeval tv;
785 gettimeofday(&tv, NULL);
786 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
787 }
788}
bellard1dce7c32006-07-13 23:20:22 +0000789#endif
790
pbrook2e70f6e2008-06-29 01:03:05 +0000791/* Return the virtual CPU time, based on the instruction counter. */
792static int64_t cpu_get_icount(void)
793{
794 int64_t icount;
795 CPUState *env = cpu_single_env;;
796 icount = qemu_icount;
797 if (env) {
798 if (!can_do_io(env))
799 fprintf(stderr, "Bad clock read\n");
800 icount -= (env->icount_decr.u16.low + env->icount_extra);
801 }
802 return qemu_icount_bias + (icount << icount_time_shift);
803}
804
bellard1dce7c32006-07-13 23:20:22 +0000805/***********************************************************/
806/* guest cycle counter */
807
808static int64_t cpu_ticks_prev;
809static int64_t cpu_ticks_offset;
810static int64_t cpu_clock_offset;
811static int cpu_ticks_enabled;
812
813/* return the host CPU cycle counter and handle stop/restart */
814int64_t cpu_get_ticks(void)
815{
pbrook2e70f6e2008-06-29 01:03:05 +0000816 if (use_icount) {
817 return cpu_get_icount();
818 }
bellard1dce7c32006-07-13 23:20:22 +0000819 if (!cpu_ticks_enabled) {
820 return cpu_ticks_offset;
821 } else {
822 int64_t ticks;
823 ticks = cpu_get_real_ticks();
824 if (cpu_ticks_prev > ticks) {
825 /* Note: non increasing ticks may happen if the host uses
826 software suspend */
827 cpu_ticks_offset += cpu_ticks_prev - ticks;
828 }
829 cpu_ticks_prev = ticks;
830 return ticks + cpu_ticks_offset;
831 }
832}
833
834/* return the host CPU monotonic timer and handle stop/restart */
835static int64_t cpu_get_clock(void)
836{
837 int64_t ti;
838 if (!cpu_ticks_enabled) {
839 return cpu_clock_offset;
840 } else {
841 ti = get_clock();
842 return ti + cpu_clock_offset;
843 }
844}
845
846/* enable cpu_get_ticks() */
847void cpu_enable_ticks(void)
848{
849 if (!cpu_ticks_enabled) {
850 cpu_ticks_offset -= cpu_get_real_ticks();
851 cpu_clock_offset -= get_clock();
852 cpu_ticks_enabled = 1;
853 }
854}
855
856/* disable cpu_get_ticks() : the clock is stopped. You must not call
857 cpu_get_ticks() after that. */
858void cpu_disable_ticks(void)
859{
860 if (cpu_ticks_enabled) {
861 cpu_ticks_offset = cpu_get_ticks();
862 cpu_clock_offset = cpu_get_clock();
863 cpu_ticks_enabled = 0;
864 }
865}
866
867/***********************************************************/
868/* timers */
ths5fafdf22007-09-16 21:08:06 +0000869
bellard8a7ddc32004-03-31 19:00:16 +0000870#define QEMU_TIMER_REALTIME 0
871#define QEMU_TIMER_VIRTUAL 1
872
873struct QEMUClock {
874 int type;
875 /* XXX: add frequency */
876};
877
878struct QEMUTimer {
879 QEMUClock *clock;
880 int64_t expire_time;
881 QEMUTimerCB *cb;
882 void *opaque;
883 struct QEMUTimer *next;
884};
885
thsc8994012007-08-19 21:56:03 +0000886struct qemu_alarm_timer {
887 char const *name;
thsefe75412007-08-24 01:36:32 +0000888 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000889
890 int (*start)(struct qemu_alarm_timer *t);
891 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000892 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000893 void *priv;
894};
895
thsefe75412007-08-24 01:36:32 +0000896#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000897#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000898
899static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
900{
901 return t->flags & ALARM_FLAG_DYNTICKS;
902}
903
904static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
905{
906 if (!alarm_has_dynticks(t))
907 return;
908
909 t->rearm(t);
910}
911
912/* TODO: MIN_TIMER_REARM_US should be optimized */
913#define MIN_TIMER_REARM_US 250
914
thsc8994012007-08-19 21:56:03 +0000915static struct qemu_alarm_timer *alarm_timer;
aliguorif49e58d2008-11-05 21:22:34 +0000916#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +0000917static int alarm_timer_rfd, alarm_timer_wfd;
aliguorif49e58d2008-11-05 21:22:34 +0000918#endif
thsc8994012007-08-19 21:56:03 +0000919
920#ifdef _WIN32
921
922struct qemu_alarm_win32 {
923 MMRESULT timerId;
924 HANDLE host_alarm;
925 unsigned int period;
926} alarm_win32_data = {0, NULL, -1};
927
928static int win32_start_timer(struct qemu_alarm_timer *t);
929static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000930static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000931
932#else
933
934static int unix_start_timer(struct qemu_alarm_timer *t);
935static void unix_stop_timer(struct qemu_alarm_timer *t);
936
ths231c6582007-08-26 17:29:15 +0000937#ifdef __linux__
938
thsefe75412007-08-24 01:36:32 +0000939static int dynticks_start_timer(struct qemu_alarm_timer *t);
940static void dynticks_stop_timer(struct qemu_alarm_timer *t);
941static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
942
thsc40ec5a2007-08-19 22:09:40 +0000943static int hpet_start_timer(struct qemu_alarm_timer *t);
944static void hpet_stop_timer(struct qemu_alarm_timer *t);
945
thsc8994012007-08-19 21:56:03 +0000946static int rtc_start_timer(struct qemu_alarm_timer *t);
947static void rtc_stop_timer(struct qemu_alarm_timer *t);
948
thsefe75412007-08-24 01:36:32 +0000949#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000950
951#endif /* _WIN32 */
952
pbrook2e70f6e2008-06-29 01:03:05 +0000953/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000954 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000955 When the guest is idle real and virtual time will be aligned in
956 the IO wait loop. */
957#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
958
959static void icount_adjust(void)
960{
961 int64_t cur_time;
962 int64_t cur_icount;
963 int64_t delta;
964 static int64_t last_delta;
965 /* If the VM is not running, then do nothing. */
966 if (!vm_running)
967 return;
968
969 cur_time = cpu_get_clock();
970 cur_icount = qemu_get_clock(vm_clock);
971 delta = cur_icount - cur_time;
972 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
973 if (delta > 0
974 && last_delta + ICOUNT_WOBBLE < delta * 2
975 && icount_time_shift > 0) {
976 /* The guest is getting too far ahead. Slow time down. */
977 icount_time_shift--;
978 }
979 if (delta < 0
980 && last_delta - ICOUNT_WOBBLE > delta * 2
981 && icount_time_shift < MAX_ICOUNT_SHIFT) {
982 /* The guest is getting too far behind. Speed time up. */
983 icount_time_shift++;
984 }
985 last_delta = delta;
986 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
987}
988
989static void icount_adjust_rt(void * opaque)
990{
991 qemu_mod_timer(icount_rt_timer,
992 qemu_get_clock(rt_clock) + 1000);
993 icount_adjust();
994}
995
996static void icount_adjust_vm(void * opaque)
997{
998 qemu_mod_timer(icount_vm_timer,
999 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1000 icount_adjust();
1001}
1002
1003static void init_icount_adjust(void)
1004{
1005 /* Have both realtime and virtual time triggers for speed adjustment.
1006 The realtime trigger catches emulated time passing too slowly,
1007 the virtual time trigger catches emulated time passing too fast.
1008 Realtime triggers occur even when idle, so use them less frequently
1009 than VM triggers. */
1010 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1011 qemu_mod_timer(icount_rt_timer,
1012 qemu_get_clock(rt_clock) + 1000);
1013 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1014 qemu_mod_timer(icount_vm_timer,
1015 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1016}
1017
thsc8994012007-08-19 21:56:03 +00001018static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +00001019#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +00001020#ifdef __linux__
thsefe75412007-08-24 01:36:32 +00001021 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1022 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001023 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +00001024 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001025 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +00001026 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001027#endif
thsefe75412007-08-24 01:36:32 +00001028 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001029#else
thsefe75412007-08-24 01:36:32 +00001030 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1031 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1032 {"win32", 0, win32_start_timer,
1033 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +00001034#endif
1035 {NULL, }
1036};
1037
blueswir13f47aa82008-03-09 06:59:01 +00001038static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +00001039{
1040 int i;
1041
1042 printf("Available alarm timers, in order of precedence:\n");
1043 for (i = 0; alarm_timers[i].name; i++)
1044 printf("%s\n", alarm_timers[i].name);
1045}
1046
1047static void configure_alarms(char const *opt)
1048{
1049 int i;
1050 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +00001051 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +00001052 char *arg;
1053 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +00001054 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +00001055
aurel323adda042008-03-09 23:43:49 +00001056 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +00001057 show_available_alarms();
1058 exit(0);
1059 }
1060
1061 arg = strdup(opt);
1062
1063 /* Reorder the array */
1064 name = strtok(arg, ",");
1065 while (name) {
balroge2b577e2007-09-17 21:25:20 +00001066 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +00001067 if (!strcmp(alarm_timers[i].name, name))
1068 break;
1069 }
1070
1071 if (i == count) {
1072 fprintf(stderr, "Unknown clock %s\n", name);
1073 goto next;
1074 }
1075
1076 if (i < cur)
1077 /* Ignore */
1078 goto next;
1079
1080 /* Swap */
1081 tmp = alarm_timers[i];
1082 alarm_timers[i] = alarm_timers[cur];
1083 alarm_timers[cur] = tmp;
1084
1085 cur++;
1086next:
1087 name = strtok(NULL, ",");
1088 }
1089
1090 free(arg);
1091
1092 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +00001093 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +00001094 for (i = cur; i < count; i++)
1095 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +00001096 } else {
1097 show_available_alarms();
1098 exit(1);
thsf3dcfad2007-08-24 01:26:02 +00001099 }
thsf3dcfad2007-08-24 01:26:02 +00001100}
1101
bellard8a7ddc32004-03-31 19:00:16 +00001102QEMUClock *rt_clock;
1103QEMUClock *vm_clock;
1104
1105static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +00001106
pbrook9596ebb2007-11-18 01:44:38 +00001107static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001108{
1109 QEMUClock *clock;
1110 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +00001111 clock->type = type;
1112 return clock;
1113}
1114
1115QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1116{
1117 QEMUTimer *ts;
1118
1119 ts = qemu_mallocz(sizeof(QEMUTimer));
1120 ts->clock = clock;
1121 ts->cb = cb;
1122 ts->opaque = opaque;
1123 return ts;
1124}
1125
1126void qemu_free_timer(QEMUTimer *ts)
1127{
1128 qemu_free(ts);
1129}
1130
1131/* stop a timer, but do not dealloc it */
1132void qemu_del_timer(QEMUTimer *ts)
1133{
1134 QEMUTimer **pt, *t;
1135
1136 /* NOTE: this code must be signal safe because
1137 qemu_timer_expired() can be called from a signal. */
1138 pt = &active_timers[ts->clock->type];
1139 for(;;) {
1140 t = *pt;
1141 if (!t)
1142 break;
1143 if (t == ts) {
1144 *pt = t->next;
1145 break;
1146 }
1147 pt = &t->next;
1148 }
1149}
1150
1151/* modify the current timer so that it will be fired when current_time
1152 >= expire_time. The corresponding callback will be called. */
1153void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1154{
1155 QEMUTimer **pt, *t;
1156
1157 qemu_del_timer(ts);
1158
1159 /* add the timer in the sorted list */
1160 /* NOTE: this code must be signal safe because
1161 qemu_timer_expired() can be called from a signal. */
1162 pt = &active_timers[ts->clock->type];
1163 for(;;) {
1164 t = *pt;
1165 if (!t)
1166 break;
ths5fafdf22007-09-16 21:08:06 +00001167 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001168 break;
1169 pt = &t->next;
1170 }
1171 ts->expire_time = expire_time;
1172 ts->next = *pt;
1173 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001174
1175 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001176 if (pt == &active_timers[ts->clock->type]) {
1177 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1178 qemu_rearm_alarm_timer(alarm_timer);
1179 }
1180 /* Interrupt execution to force deadline recalculation. */
1181 if (use_icount && cpu_single_env) {
1182 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1183 }
1184 }
bellard8a7ddc32004-03-31 19:00:16 +00001185}
1186
1187int qemu_timer_pending(QEMUTimer *ts)
1188{
1189 QEMUTimer *t;
1190 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1191 if (t == ts)
1192 return 1;
1193 }
1194 return 0;
1195}
1196
1197static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1198{
1199 if (!timer_head)
1200 return 0;
1201 return (timer_head->expire_time <= current_time);
1202}
1203
1204static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1205{
1206 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001207
bellard8a7ddc32004-03-31 19:00:16 +00001208 for(;;) {
1209 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001210 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001211 break;
1212 /* remove timer from the list before calling the callback */
1213 *ptimer_head = ts->next;
1214 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001215
bellard8a7ddc32004-03-31 19:00:16 +00001216 /* run the callback (the timer list can be modified) */
1217 ts->cb(ts->opaque);
1218 }
1219}
1220
1221int64_t qemu_get_clock(QEMUClock *clock)
1222{
1223 switch(clock->type) {
1224 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001225 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001226 default:
1227 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001228 if (use_icount) {
1229 return cpu_get_icount();
1230 } else {
1231 return cpu_get_clock();
1232 }
bellard8a7ddc32004-03-31 19:00:16 +00001233 }
1234}
1235
bellard1dce7c32006-07-13 23:20:22 +00001236static void init_timers(void)
1237{
1238 init_get_clock();
1239 ticks_per_sec = QEMU_TIMER_BASE;
1240 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1241 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1242}
1243
bellard8a7ddc32004-03-31 19:00:16 +00001244/* save a timer */
1245void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1246{
1247 uint64_t expire_time;
1248
1249 if (qemu_timer_pending(ts)) {
1250 expire_time = ts->expire_time;
1251 } else {
1252 expire_time = -1;
1253 }
1254 qemu_put_be64(f, expire_time);
1255}
1256
1257void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1258{
1259 uint64_t expire_time;
1260
1261 expire_time = qemu_get_be64(f);
1262 if (expire_time != -1) {
1263 qemu_mod_timer(ts, expire_time);
1264 } else {
1265 qemu_del_timer(ts);
1266 }
1267}
1268
1269static void timer_save(QEMUFile *f, void *opaque)
1270{
1271 if (cpu_ticks_enabled) {
1272 hw_error("cannot save state if virtual timers are running");
1273 }
thsbee8d682007-12-16 23:41:11 +00001274 qemu_put_be64(f, cpu_ticks_offset);
1275 qemu_put_be64(f, ticks_per_sec);
1276 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001277}
1278
1279static int timer_load(QEMUFile *f, void *opaque, int version_id)
1280{
bellardc88676f2006-08-06 13:36:11 +00001281 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001282 return -EINVAL;
1283 if (cpu_ticks_enabled) {
1284 return -EINVAL;
1285 }
thsbee8d682007-12-16 23:41:11 +00001286 cpu_ticks_offset=qemu_get_be64(f);
1287 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001288 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001289 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001290 }
bellard8a7ddc32004-03-31 19:00:16 +00001291 return 0;
1292}
1293
bellard67b915a2004-03-31 23:37:16 +00001294#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001295void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001296 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1297#else
bellard8a7ddc32004-03-31 19:00:16 +00001298static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001299#endif
bellard8a7ddc32004-03-31 19:00:16 +00001300{
bellard02ba45c2004-06-25 14:46:23 +00001301#if 0
1302#define DISP_FREQ 1000
1303 {
1304 static int64_t delta_min = INT64_MAX;
1305 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1306 static int count;
1307 ti = qemu_get_clock(vm_clock);
1308 if (last_clock != 0) {
1309 delta = ti - last_clock;
1310 if (delta < delta_min)
1311 delta_min = delta;
1312 if (delta > delta_max)
1313 delta_max = delta;
1314 delta_cum += delta;
1315 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001316 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001317 muldiv64(delta_min, 1000000, ticks_per_sec),
1318 muldiv64(delta_max, 1000000, ticks_per_sec),
1319 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1320 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1321 count = 0;
1322 delta_min = INT64_MAX;
1323 delta_max = 0;
1324 delta_cum = 0;
1325 }
1326 }
1327 last_clock = ti;
1328 }
1329#endif
thsefe75412007-08-24 01:36:32 +00001330 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001331 (!use_icount &&
1332 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1333 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001334 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1335 qemu_get_clock(rt_clock))) {
aliguoric96f1a42008-11-05 20:29:45 +00001336 CPUState *env = next_cpu;
aliguoric96f1a42008-11-05 20:29:45 +00001337
bellard06d9f2f2006-05-01 13:23:04 +00001338#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001339 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1340 SetEvent(data->host_alarm);
aliguorif49e58d2008-11-05 21:22:34 +00001341#else
1342 static const char byte = 0;
aliguoric96f1a42008-11-05 20:29:45 +00001343 write(alarm_timer_wfd, &byte, sizeof(byte));
aliguorif49e58d2008-11-05 21:22:34 +00001344#endif
balrogd5d08332008-01-05 19:41:47 +00001345 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1346
balrog4f8eb8d2007-12-16 12:39:38 +00001347 if (env) {
1348 /* stop the currently executing cpu because a timer occured */
1349 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
bellarda332e112005-09-03 17:55:47 +00001350#ifdef USE_KQEMU
balrog4f8eb8d2007-12-16 12:39:38 +00001351 if (env->kqemu_enabled) {
1352 kqemu_cpu_interrupt(env);
1353 }
balrogee5605e2007-12-03 03:01:40 +00001354#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001355 }
balrogee5605e2007-12-03 03:01:40 +00001356 event_pending = 1;
bellard8a7ddc32004-03-31 19:00:16 +00001357 }
1358}
1359
pbrook2e70f6e2008-06-29 01:03:05 +00001360static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001361{
pbrook2e70f6e2008-06-29 01:03:05 +00001362 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001363
1364 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001365 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1366 qemu_get_clock(vm_clock);
1367 } else {
1368 /* To avoid problems with overflow limit this to 2^32. */
1369 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001370 }
1371
pbrook2e70f6e2008-06-29 01:03:05 +00001372 if (delta < 0)
1373 delta = 0;
thsefe75412007-08-24 01:36:32 +00001374
pbrook2e70f6e2008-06-29 01:03:05 +00001375 return delta;
1376}
1377
blueswir18632fb92008-09-14 13:59:34 +00001378#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001379static uint64_t qemu_next_deadline_dyntick(void)
1380{
1381 int64_t delta;
1382 int64_t rtdelta;
1383
1384 if (use_icount)
1385 delta = INT32_MAX;
1386 else
1387 delta = (qemu_next_deadline() + 999) / 1000;
1388
1389 if (active_timers[QEMU_TIMER_REALTIME]) {
1390 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1391 qemu_get_clock(rt_clock))*1000;
1392 if (rtdelta < delta)
1393 delta = rtdelta;
1394 }
1395
1396 if (delta < MIN_TIMER_REARM_US)
1397 delta = MIN_TIMER_REARM_US;
1398
1399 return delta;
thsefe75412007-08-24 01:36:32 +00001400}
blueswir18632fb92008-09-14 13:59:34 +00001401#endif
thsefe75412007-08-24 01:36:32 +00001402
bellardfd872592004-05-12 19:11:15 +00001403#ifndef _WIN32
1404
aliguori7183b4b2008-11-05 20:40:18 +00001405/* Sets a specific flag */
1406static int fcntl_setfl(int fd, int flag)
1407{
1408 int flags;
1409
1410 flags = fcntl(fd, F_GETFL);
1411 if (flags == -1)
1412 return -errno;
1413
1414 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1415 return -errno;
1416
1417 return 0;
1418}
1419
bellard829309c2004-05-20 13:20:12 +00001420#if defined(__linux__)
1421
bellardfd872592004-05-12 19:11:15 +00001422#define RTC_FREQ 1024
1423
aurel32de9a95f2008-11-11 13:41:01 +00001424static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001425{
thsc8994012007-08-19 21:56:03 +00001426 struct sigaction act;
1427
1428 /* timer signal */
1429 sigfillset(&act.sa_mask);
1430 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001431 act.sa_handler = host_alarm_handler;
1432
1433 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001434 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001435 fcntl(fd, F_SETOWN, getpid());
1436}
1437
thsc40ec5a2007-08-19 22:09:40 +00001438static int hpet_start_timer(struct qemu_alarm_timer *t)
1439{
1440 struct hpet_info info;
1441 int r, fd;
1442
1443 fd = open("/dev/hpet", O_RDONLY);
1444 if (fd < 0)
1445 return -1;
1446
1447 /* Set frequency */
1448 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1449 if (r < 0) {
1450 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1451 "error, but for better emulation accuracy type:\n"
1452 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1453 goto fail;
1454 }
1455
1456 /* Check capabilities */
1457 r = ioctl(fd, HPET_INFO, &info);
1458 if (r < 0)
1459 goto fail;
1460
1461 /* Enable periodic mode */
1462 r = ioctl(fd, HPET_EPI, 0);
1463 if (info.hi_flags && (r < 0))
1464 goto fail;
1465
1466 /* Enable interrupt */
1467 r = ioctl(fd, HPET_IE_ON, 0);
1468 if (r < 0)
1469 goto fail;
1470
1471 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001472 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001473
1474 return 0;
1475fail:
1476 close(fd);
1477 return -1;
1478}
1479
1480static void hpet_stop_timer(struct qemu_alarm_timer *t)
1481{
pbrookfcdc2122007-08-23 20:22:22 +00001482 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001483
1484 close(fd);
1485}
1486
thsc8994012007-08-19 21:56:03 +00001487static int rtc_start_timer(struct qemu_alarm_timer *t)
1488{
1489 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001490 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001491
balrogaeb30be2007-07-02 15:03:13 +00001492 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001493 if (rtc_fd < 0)
1494 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001495 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1496 if (current_rtc_freq != RTC_FREQ &&
1497 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001498 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1499 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1500 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1501 goto fail;
1502 }
1503 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1504 fail:
1505 close(rtc_fd);
1506 return -1;
1507 }
thsc8994012007-08-19 21:56:03 +00001508
1509 enable_sigio_timer(rtc_fd);
1510
pbrookfcdc2122007-08-23 20:22:22 +00001511 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001512
bellardfd872592004-05-12 19:11:15 +00001513 return 0;
1514}
1515
thsc8994012007-08-19 21:56:03 +00001516static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001517{
pbrookfcdc2122007-08-23 20:22:22 +00001518 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001519
1520 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001521}
1522
thsefe75412007-08-24 01:36:32 +00001523static int dynticks_start_timer(struct qemu_alarm_timer *t)
1524{
1525 struct sigevent ev;
1526 timer_t host_timer;
1527 struct sigaction act;
1528
1529 sigfillset(&act.sa_mask);
1530 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001531 act.sa_handler = host_alarm_handler;
1532
1533 sigaction(SIGALRM, &act, NULL);
1534
1535 ev.sigev_value.sival_int = 0;
1536 ev.sigev_notify = SIGEV_SIGNAL;
1537 ev.sigev_signo = SIGALRM;
1538
1539 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1540 perror("timer_create");
1541
1542 /* disable dynticks */
1543 fprintf(stderr, "Dynamic Ticks disabled\n");
1544
1545 return -1;
1546 }
1547
blueswir10399bfe2008-11-16 11:37:18 +00001548 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001549
1550 return 0;
1551}
1552
1553static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1554{
blueswir10399bfe2008-11-16 11:37:18 +00001555 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001556
1557 timer_delete(host_timer);
1558}
1559
1560static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1561{
blueswir10399bfe2008-11-16 11:37:18 +00001562 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001563 struct itimerspec timeout;
1564 int64_t nearest_delta_us = INT64_MAX;
1565 int64_t current_us;
1566
1567 if (!active_timers[QEMU_TIMER_REALTIME] &&
1568 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001569 return;
thsefe75412007-08-24 01:36:32 +00001570
pbrook2e70f6e2008-06-29 01:03:05 +00001571 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001572
1573 /* check whether a timer is already running */
1574 if (timer_gettime(host_timer, &timeout)) {
1575 perror("gettime");
1576 fprintf(stderr, "Internal timer error: aborting\n");
1577 exit(1);
1578 }
1579 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1580 if (current_us && current_us <= nearest_delta_us)
1581 return;
1582
1583 timeout.it_interval.tv_sec = 0;
1584 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1585 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1586 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1587 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1588 perror("settime");
1589 fprintf(stderr, "Internal timer error: aborting\n");
1590 exit(1);
1591 }
1592}
1593
ths70744b32007-08-26 17:31:30 +00001594#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001595
thsc8994012007-08-19 21:56:03 +00001596static int unix_start_timer(struct qemu_alarm_timer *t)
1597{
1598 struct sigaction act;
1599 struct itimerval itv;
1600 int err;
1601
1602 /* timer signal */
1603 sigfillset(&act.sa_mask);
1604 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001605 act.sa_handler = host_alarm_handler;
1606
1607 sigaction(SIGALRM, &act, NULL);
1608
1609 itv.it_interval.tv_sec = 0;
1610 /* for i386 kernel 2.6 to get 1 ms */
1611 itv.it_interval.tv_usec = 999;
1612 itv.it_value.tv_sec = 0;
1613 itv.it_value.tv_usec = 10 * 1000;
1614
1615 err = setitimer(ITIMER_REAL, &itv, NULL);
1616 if (err)
1617 return -1;
1618
1619 return 0;
1620}
1621
1622static void unix_stop_timer(struct qemu_alarm_timer *t)
1623{
1624 struct itimerval itv;
1625
1626 memset(&itv, 0, sizeof(itv));
1627 setitimer(ITIMER_REAL, &itv, NULL);
1628}
1629
bellard829309c2004-05-20 13:20:12 +00001630#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001631
aliguorif49e58d2008-11-05 21:22:34 +00001632static void try_to_rearm_timer(void *opaque)
1633{
1634 struct qemu_alarm_timer *t = opaque;
1635#ifndef _WIN32
1636 ssize_t len;
1637
1638 /* Drain the notify pipe */
1639 do {
1640 char buffer[512];
1641 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1642 } while ((len == -1 && errno == EINTR) || len > 0);
1643#endif
1644
aliguorif49e58d2008-11-05 21:22:34 +00001645 if (t->flags & ALARM_FLAG_EXPIRED) {
1646 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1647 qemu_rearm_alarm_timer(alarm_timer);
1648 }
1649}
1650
thsc8994012007-08-19 21:56:03 +00001651#ifdef _WIN32
1652
1653static int win32_start_timer(struct qemu_alarm_timer *t)
1654{
1655 TIMECAPS tc;
1656 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001657 UINT flags;
thsc8994012007-08-19 21:56:03 +00001658
1659 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1660 if (!data->host_alarm) {
1661 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001662 return -1;
thsc8994012007-08-19 21:56:03 +00001663 }
1664
1665 memset(&tc, 0, sizeof(tc));
1666 timeGetDevCaps(&tc, sizeof(tc));
1667
1668 if (data->period < tc.wPeriodMin)
1669 data->period = tc.wPeriodMin;
1670
1671 timeBeginPeriod(data->period);
1672
thsefe75412007-08-24 01:36:32 +00001673 flags = TIME_CALLBACK_FUNCTION;
1674 if (alarm_has_dynticks(t))
1675 flags |= TIME_ONESHOT;
1676 else
1677 flags |= TIME_PERIODIC;
1678
thsc8994012007-08-19 21:56:03 +00001679 data->timerId = timeSetEvent(1, // interval (ms)
1680 data->period, // resolution
1681 host_alarm_handler, // function
1682 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001683 flags);
thsc8994012007-08-19 21:56:03 +00001684
1685 if (!data->timerId) {
1686 perror("Failed to initialize win32 alarm timer");
1687
1688 timeEndPeriod(data->period);
1689 CloseHandle(data->host_alarm);
1690 return -1;
1691 }
1692
aliguorif49e58d2008-11-05 21:22:34 +00001693 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
thsc8994012007-08-19 21:56:03 +00001694
1695 return 0;
1696}
1697
1698static void win32_stop_timer(struct qemu_alarm_timer *t)
1699{
1700 struct qemu_alarm_win32 *data = t->priv;
1701
1702 timeKillEvent(data->timerId);
1703 timeEndPeriod(data->period);
1704
1705 CloseHandle(data->host_alarm);
1706}
1707
thsefe75412007-08-24 01:36:32 +00001708static void win32_rearm_timer(struct qemu_alarm_timer *t)
1709{
1710 struct qemu_alarm_win32 *data = t->priv;
1711 uint64_t nearest_delta_us;
1712
1713 if (!active_timers[QEMU_TIMER_REALTIME] &&
1714 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001715 return;
thsefe75412007-08-24 01:36:32 +00001716
pbrook2e70f6e2008-06-29 01:03:05 +00001717 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001718 nearest_delta_us /= 1000;
1719
1720 timeKillEvent(data->timerId);
1721
1722 data->timerId = timeSetEvent(1,
1723 data->period,
1724 host_alarm_handler,
1725 (DWORD)t,
1726 TIME_ONESHOT | TIME_PERIODIC);
1727
1728 if (!data->timerId) {
1729 perror("Failed to re-arm win32 alarm timer");
1730
1731 timeEndPeriod(data->period);
1732 CloseHandle(data->host_alarm);
1733 exit(1);
1734 }
1735}
1736
thsc8994012007-08-19 21:56:03 +00001737#endif /* _WIN32 */
1738
aliguori7183b4b2008-11-05 20:40:18 +00001739static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001740{
blueswir1223f0d72008-09-30 18:12:18 +00001741 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001742 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001743
1744#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +00001745 int fds[2];
1746
aliguori7183b4b2008-11-05 20:40:18 +00001747 err = pipe(fds);
1748 if (err == -1)
1749 return -errno;
1750
1751 err = fcntl_setfl(fds[0], O_NONBLOCK);
1752 if (err < 0)
1753 goto fail;
1754
1755 err = fcntl_setfl(fds[1], O_NONBLOCK);
1756 if (err < 0)
1757 goto fail;
1758
aliguoric96f1a42008-11-05 20:29:45 +00001759 alarm_timer_rfd = fds[0];
1760 alarm_timer_wfd = fds[1];
aliguorif49e58d2008-11-05 21:22:34 +00001761#endif
bellard06d9f2f2006-05-01 13:23:04 +00001762
thsc8994012007-08-19 21:56:03 +00001763 for (i = 0; alarm_timers[i].name; i++) {
1764 t = &alarm_timers[i];
1765
thsc8994012007-08-19 21:56:03 +00001766 err = t->start(t);
1767 if (!err)
1768 break;
bellard67b915a2004-03-31 23:37:16 +00001769 }
bellardfd872592004-05-12 19:11:15 +00001770
thsc8994012007-08-19 21:56:03 +00001771 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001772 err = -ENOENT;
1773 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001774 }
thsc8994012007-08-19 21:56:03 +00001775
aliguorif49e58d2008-11-05 21:22:34 +00001776#ifndef _WIN32
aliguori6abfbd72008-11-05 20:49:37 +00001777 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1778 try_to_rearm_timer, NULL, t);
aliguorif49e58d2008-11-05 21:22:34 +00001779#endif
aliguori6abfbd72008-11-05 20:49:37 +00001780
thsc8994012007-08-19 21:56:03 +00001781 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001782
aliguori6abfbd72008-11-05 20:49:37 +00001783 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001784
1785fail:
aliguorif49e58d2008-11-05 21:22:34 +00001786#ifndef _WIN32
aliguori7183b4b2008-11-05 20:40:18 +00001787 close(fds[0]);
1788 close(fds[1]);
aliguorif49e58d2008-11-05 21:22:34 +00001789#endif
aliguori7183b4b2008-11-05 20:40:18 +00001790 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001791}
1792
pbrook9596ebb2007-11-18 01:44:38 +00001793static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001794{
thsc8994012007-08-19 21:56:03 +00001795 alarm_timer->stop(alarm_timer);
1796 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001797}
1798
bellardc4b1fcc2004-03-14 21:44:30 +00001799/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001800/* host time/date access */
1801void qemu_get_timedate(struct tm *tm, int offset)
1802{
1803 time_t ti;
1804 struct tm *ret;
1805
1806 time(&ti);
1807 ti += offset;
1808 if (rtc_date_offset == -1) {
1809 if (rtc_utc)
1810 ret = gmtime(&ti);
1811 else
1812 ret = localtime(&ti);
1813 } else {
1814 ti -= rtc_date_offset;
1815 ret = gmtime(&ti);
1816 }
1817
1818 memcpy(tm, ret, sizeof(struct tm));
1819}
1820
1821int qemu_timedate_diff(struct tm *tm)
1822{
1823 time_t seconds;
1824
1825 if (rtc_date_offset == -1)
1826 if (rtc_utc)
1827 seconds = mktimegm(tm);
1828 else
1829 seconds = mktime(tm);
1830 else
1831 seconds = mktimegm(tm) + rtc_date_offset;
1832
1833 return seconds - time(NULL);
1834}
1835
bellardfd1dff42006-02-01 21:29:26 +00001836#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001837static void socket_cleanup(void)
1838{
1839 WSACleanup();
1840}
bellard82c643f2004-07-14 17:28:13 +00001841
bellardfd1dff42006-02-01 21:29:26 +00001842static int socket_init(void)
1843{
1844 WSADATA Data;
1845 int ret, err;
1846
1847 ret = WSAStartup(MAKEWORD(2,2), &Data);
1848 if (ret != 0) {
1849 err = WSAGetLastError();
1850 fprintf(stderr, "WSAStartup: %d\n", err);
1851 return -1;
1852 }
1853 atexit(socket_cleanup);
1854 return 0;
1855}
aurel3264b7b732008-05-05 10:05:31 +00001856#endif
1857
aliguori63a01ef2008-10-31 19:10:00 +00001858const char *get_opt_name(char *buf, int buf_size, const char *p)
thse4bcb142007-12-02 04:51:10 +00001859{
1860 char *q;
thse4bcb142007-12-02 04:51:10 +00001861
balrog609497a2008-01-14 02:56:53 +00001862 q = buf;
1863 while (*p != '\0' && *p != '=') {
1864 if (q && (q - buf) < buf_size - 1)
1865 *q++ = *p;
1866 p++;
1867 }
1868 if (q)
1869 *q = '\0';
1870
1871 return p;
1872}
1873
aliguori63a01ef2008-10-31 19:10:00 +00001874const char *get_opt_value(char *buf, int buf_size, const char *p)
balrog609497a2008-01-14 02:56:53 +00001875{
1876 char *q;
1877
thse4bcb142007-12-02 04:51:10 +00001878 q = buf;
1879 while (*p != '\0') {
balrog609497a2008-01-14 02:56:53 +00001880 if (*p == ',') {
1881 if (*(p + 1) != ',')
thse4bcb142007-12-02 04:51:10 +00001882 break;
thse4bcb142007-12-02 04:51:10 +00001883 p++;
balrog609497a2008-01-14 02:56:53 +00001884 }
thse4bcb142007-12-02 04:51:10 +00001885 if (q && (q - buf) < buf_size - 1)
1886 *q++ = *p;
1887 p++;
1888 }
1889 if (q)
1890 *q = '\0';
1891
1892 return p;
1893}
1894
aliguori63a01ef2008-10-31 19:10:00 +00001895int get_param_value(char *buf, int buf_size,
1896 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001897{
1898 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001899 char option[128];
1900
1901 p = str;
1902 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001903 p = get_opt_name(option, sizeof(option), p);
bellard7c9d8e02005-11-15 22:16:05 +00001904 if (*p != '=')
1905 break;
1906 p++;
1907 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001908 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001909 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001910 } else {
balrog609497a2008-01-14 02:56:53 +00001911 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001912 }
1913 if (*p != ',')
1914 break;
1915 p++;
1916 }
1917 return 0;
1918}
1919
aliguori63a01ef2008-10-31 19:10:00 +00001920int check_params(char *buf, int buf_size,
1921 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001922{
1923 const char *p;
1924 int i;
1925
1926 p = str;
1927 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001928 p = get_opt_name(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001929 if (*p != '=')
1930 return -1;
1931 p++;
1932 for(i = 0; params[i] != NULL; i++)
1933 if (!strcmp(params[i], buf))
1934 break;
1935 if (params[i] == NULL)
1936 return -1;
balrog609497a2008-01-14 02:56:53 +00001937 p = get_opt_value(NULL, 0, p);
thse4bcb142007-12-02 04:51:10 +00001938 if (*p != ',')
1939 break;
1940 p++;
1941 }
1942 return 0;
1943}
1944
balrog1ae26a12008-09-28 23:19:47 +00001945/***********************************************************/
1946/* Bluetooth support */
1947static int nb_hcis;
1948static int cur_hci;
1949static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001950
balrog1ae26a12008-09-28 23:19:47 +00001951static struct bt_vlan_s {
1952 struct bt_scatternet_s net;
1953 int id;
1954 struct bt_vlan_s *next;
1955} *first_bt_vlan;
1956
1957/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001958static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001959{
1960 struct bt_vlan_s **pvlan, *vlan;
1961 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1962 if (vlan->id == id)
1963 return &vlan->net;
1964 }
1965 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1966 vlan->id = id;
1967 pvlan = &first_bt_vlan;
1968 while (*pvlan != NULL)
1969 pvlan = &(*pvlan)->next;
1970 *pvlan = vlan;
1971 return &vlan->net;
1972}
1973
1974static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1975{
1976}
1977
1978static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1979{
1980 return -ENOTSUP;
1981}
1982
1983static struct HCIInfo null_hci = {
1984 .cmd_send = null_hci_send,
1985 .sco_send = null_hci_send,
1986 .acl_send = null_hci_send,
1987 .bdaddr_set = null_hci_addr_set,
1988};
1989
1990struct HCIInfo *qemu_next_hci(void)
1991{
1992 if (cur_hci == nb_hcis)
1993 return &null_hci;
1994
1995 return hci_table[cur_hci++];
1996}
1997
balrogdc72ac12008-11-09 00:04:26 +00001998static struct HCIInfo *hci_init(const char *str)
1999{
2000 char *endp;
2001 struct bt_scatternet_s *vlan = 0;
2002
2003 if (!strcmp(str, "null"))
2004 /* null */
2005 return &null_hci;
2006 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
2007 /* host[:hciN] */
2008 return bt_host_hci(str[4] ? str + 5 : "hci0");
2009 else if (!strncmp(str, "hci", 3)) {
2010 /* hci[,vlan=n] */
2011 if (str[3]) {
2012 if (!strncmp(str + 3, ",vlan=", 6)) {
2013 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2014 if (*endp)
2015 vlan = 0;
2016 }
2017 } else
2018 vlan = qemu_find_bt_vlan(0);
2019 if (vlan)
2020 return bt_new_hci(vlan);
2021 }
2022
2023 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2024
2025 return 0;
2026}
2027
2028static int bt_hci_parse(const char *str)
2029{
2030 struct HCIInfo *hci;
2031 bdaddr_t bdaddr;
2032
2033 if (nb_hcis >= MAX_NICS) {
2034 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2035 return -1;
2036 }
2037
2038 hci = hci_init(str);
2039 if (!hci)
2040 return -1;
2041
2042 bdaddr.b[0] = 0x52;
2043 bdaddr.b[1] = 0x54;
2044 bdaddr.b[2] = 0x00;
2045 bdaddr.b[3] = 0x12;
2046 bdaddr.b[4] = 0x34;
2047 bdaddr.b[5] = 0x56 + nb_hcis;
2048 hci->bdaddr_set(hci, bdaddr.b);
2049
2050 hci_table[nb_hcis++] = hci;
2051
2052 return 0;
2053}
2054
2055static void bt_vhci_add(int vlan_id)
2056{
2057 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2058
2059 if (!vlan->slave)
2060 fprintf(stderr, "qemu: warning: adding a VHCI to "
2061 "an empty scatternet %i\n", vlan_id);
2062
2063 bt_vhci_init(bt_new_hci(vlan));
2064}
2065
2066static struct bt_device_s *bt_device_add(const char *opt)
2067{
2068 struct bt_scatternet_s *vlan;
2069 int vlan_id = 0;
2070 char *endp = strstr(opt, ",vlan=");
2071 int len = (endp ? endp - opt : strlen(opt)) + 1;
2072 char devname[10];
2073
2074 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2075
2076 if (endp) {
2077 vlan_id = strtol(endp + 6, &endp, 0);
2078 if (*endp) {
2079 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2080 return 0;
2081 }
2082 }
2083
2084 vlan = qemu_find_bt_vlan(vlan_id);
2085
2086 if (!vlan->slave)
2087 fprintf(stderr, "qemu: warning: adding a slave device to "
2088 "an empty scatternet %i\n", vlan_id);
2089
2090 if (!strcmp(devname, "keyboard"))
2091 return bt_keyboard_init(vlan);
2092
2093 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2094 return 0;
2095}
2096
2097static int bt_parse(const char *opt)
2098{
2099 const char *endp, *p;
2100 int vlan;
2101
2102 if (strstart(opt, "hci", &endp)) {
2103 if (!*endp || *endp == ',') {
2104 if (*endp)
2105 if (!strstart(endp, ",vlan=", 0))
2106 opt = endp + 1;
2107
2108 return bt_hci_parse(opt);
2109 }
2110 } else if (strstart(opt, "vhci", &endp)) {
2111 if (!*endp || *endp == ',') {
2112 if (*endp) {
2113 if (strstart(endp, ",vlan=", &p)) {
2114 vlan = strtol(p, (char **) &endp, 0);
2115 if (*endp) {
2116 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2117 return 1;
2118 }
2119 } else {
2120 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2121 return 1;
2122 }
2123 } else
2124 vlan = 0;
2125
2126 bt_vhci_add(vlan);
2127 return 0;
2128 }
2129 } else if (strstart(opt, "device:", &endp))
2130 return !bt_device_add(endp);
2131
2132 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2133 return 1;
2134}
2135
balrog1ae26a12008-09-28 23:19:47 +00002136/***********************************************************/
2137/* QEMU Block devices */
2138
balrog609497a2008-01-14 02:56:53 +00002139#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00002140#ifdef TARGET_PPC
2141#define CDROM_ALIAS "index=1,media=cdrom"
2142#else
2143#define CDROM_ALIAS "index=2,media=cdrom"
2144#endif
2145#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00002146#define PFLASH_ALIAS "if=pflash"
2147#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00002148#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002149
aliguori7d5aca92009-02-11 15:19:58 +00002150static int drive_opt_get_free_idx(void)
2151{
2152 int index;
2153
2154 for (index = 0; index < MAX_DRIVES; index++)
2155 if (!drives_opt[index].used) {
2156 drives_opt[index].used = 1;
2157 return index;
2158 }
2159
2160 return -1;
2161}
2162
2163static int drive_get_free_idx(void)
2164{
2165 int index;
2166
2167 for (index = 0; index < MAX_DRIVES; index++)
2168 if (!drives_table[index].used) {
2169 drives_table[index].used = 1;
2170 return index;
2171 }
2172
2173 return -1;
2174}
2175
aliguori4d73cd32009-02-11 15:20:46 +00002176int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002177{
2178 va_list ap;
aliguori7d5aca92009-02-11 15:19:58 +00002179 int index = drive_opt_get_free_idx();
thse4bcb142007-12-02 04:51:10 +00002180
aliguori7d5aca92009-02-11 15:19:58 +00002181 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
thse4bcb142007-12-02 04:51:10 +00002182 fprintf(stderr, "qemu: too many drives\n");
aliguori4d73cd32009-02-11 15:20:46 +00002183 return -1;
thse4bcb142007-12-02 04:51:10 +00002184 }
2185
aliguori7d5aca92009-02-11 15:19:58 +00002186 drives_opt[index].file = file;
thse4bcb142007-12-02 04:51:10 +00002187 va_start(ap, fmt);
aliguori7d5aca92009-02-11 15:19:58 +00002188 vsnprintf(drives_opt[index].opt,
balrog609497a2008-01-14 02:56:53 +00002189 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002190 va_end(ap);
2191
aliguori7d5aca92009-02-11 15:19:58 +00002192 nb_drives_opt++;
2193 return index;
thse4bcb142007-12-02 04:51:10 +00002194}
2195
aliguorib01b1112009-02-11 15:20:20 +00002196void drive_remove(int index)
2197{
2198 drives_opt[index].used = 0;
2199 nb_drives_opt--;
2200}
2201
thsf60d39b2007-12-17 03:55:57 +00002202int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002203{
2204 int index;
2205
2206 /* seek interface, bus and unit */
2207
aliguori7d5aca92009-02-11 15:19:58 +00002208 for (index = 0; index < MAX_DRIVES; index++)
thsf60d39b2007-12-17 03:55:57 +00002209 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002210 drives_table[index].bus == bus &&
aliguori7d5aca92009-02-11 15:19:58 +00002211 drives_table[index].unit == unit &&
2212 drives_table[index].used)
thse4bcb142007-12-02 04:51:10 +00002213 return index;
2214
2215 return -1;
2216}
2217
thsf60d39b2007-12-17 03:55:57 +00002218int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002219{
2220 int max_bus;
2221 int index;
2222
2223 max_bus = -1;
2224 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002225 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002226 drives_table[index].bus > max_bus)
2227 max_bus = drives_table[index].bus;
2228 }
2229 return max_bus;
2230}
2231
aliguorifa879c62009-01-07 17:32:33 +00002232const char *drive_get_serial(BlockDriverState *bdrv)
2233{
2234 int index;
2235
2236 for (index = 0; index < nb_drives; index++)
2237 if (drives_table[index].bdrv == bdrv)
2238 return drives_table[index].serial;
2239
2240 return "\0";
2241}
2242
aliguori428c5702009-01-21 18:59:04 +00002243BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2244{
2245 int index;
2246
2247 for (index = 0; index < nb_drives; index++)
2248 if (drives_table[index].bdrv == bdrv)
2249 return drives_table[index].onerror;
2250
aliguoricdad4bd2009-02-28 16:51:01 +00002251 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002252}
2253
aurel32a1620fa2008-04-29 05:58:01 +00002254static void bdrv_format_print(void *opaque, const char *name)
2255{
2256 fprintf(stderr, " %s", name);
2257}
2258
aliguorib01b1112009-02-11 15:20:20 +00002259void drive_uninit(BlockDriverState *bdrv)
2260{
2261 int i;
2262
2263 for (i = 0; i < MAX_DRIVES; i++)
2264 if (drives_table[i].bdrv == bdrv) {
2265 drives_table[i].bdrv = NULL;
2266 drives_table[i].used = 0;
2267 drive_remove(drives_table[i].drive_opt_idx);
2268 nb_drives--;
2269 break;
2270 }
2271}
2272
aliguori4d73cd32009-02-11 15:20:46 +00002273int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
thse4bcb142007-12-02 04:51:10 +00002274{
2275 char buf[128];
2276 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002277 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00002278 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002279 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002280 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002281 enum { MEDIA_DISK, MEDIA_CDROM } media;
2282 int bus_id, unit_id;
2283 int cyls, heads, secs, translation;
2284 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002285 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002286 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002287 int max_devs;
2288 int index;
balrog33f00272007-12-24 14:33:24 +00002289 int cache;
aliguori428c5702009-01-21 18:59:04 +00002290 int bdrv_flags, onerror;
aliguori7d5aca92009-02-11 15:19:58 +00002291 int drives_table_idx;
balrog609497a2008-01-14 02:56:53 +00002292 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002293 static const char * const params[] = { "bus", "unit", "if", "index",
2294 "cyls", "heads", "secs", "trans",
2295 "media", "snapshot", "file",
aliguori428c5702009-01-21 18:59:04 +00002296 "cache", "format", "serial", "werror",
2297 NULL };
thse4bcb142007-12-02 04:51:10 +00002298
2299 if (check_params(buf, sizeof(buf), params, str) < 0) {
balrogff993632008-02-10 13:21:25 +00002300 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
thse4bcb142007-12-02 04:51:10 +00002301 buf, str);
2302 return -1;
2303 }
2304
2305 file[0] = 0;
2306 cyls = heads = secs = 0;
2307 bus_id = 0;
2308 unit_id = -1;
2309 translation = BIOS_ATA_TRANSLATION_AUTO;
2310 index = -1;
aliguori4dc822d2008-12-04 21:39:21 +00002311 cache = 3;
thse4bcb142007-12-02 04:51:10 +00002312
blueswir1c9b1ae22008-09-28 18:55:17 +00002313 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002314 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002315 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002316 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002317 } else {
thsf60d39b2007-12-17 03:55:57 +00002318 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002319 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002320 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002321 }
2322 media = MEDIA_DISK;
2323
2324 /* extract parameters */
2325
2326 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2327 bus_id = strtol(buf, NULL, 0);
2328 if (bus_id < 0) {
2329 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2330 return -1;
2331 }
2332 }
2333
2334 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2335 unit_id = strtol(buf, NULL, 0);
2336 if (unit_id < 0) {
2337 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2338 return -1;
2339 }
2340 }
2341
2342 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002343 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002344 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002345 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002346 max_devs = MAX_IDE_DEVS;
2347 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002348 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002349 max_devs = MAX_SCSI_DEVS;
2350 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002351 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002352 max_devs = 0;
2353 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002354 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002355 max_devs = 0;
2356 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002357 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002358 max_devs = 0;
2359 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002360 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002361 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002362 } else if (!strcmp(buf, "virtio")) {
2363 type = IF_VIRTIO;
2364 max_devs = 0;
2365 } else {
thse4bcb142007-12-02 04:51:10 +00002366 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2367 return -1;
2368 }
2369 }
2370
2371 if (get_param_value(buf, sizeof(buf), "index", str)) {
2372 index = strtol(buf, NULL, 0);
2373 if (index < 0) {
2374 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2375 return -1;
2376 }
2377 }
2378
2379 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2380 cyls = strtol(buf, NULL, 0);
2381 }
2382
2383 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2384 heads = strtol(buf, NULL, 0);
2385 }
2386
2387 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2388 secs = strtol(buf, NULL, 0);
2389 }
2390
2391 if (cyls || heads || secs) {
2392 if (cyls < 1 || cyls > 16383) {
2393 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2394 return -1;
2395 }
2396 if (heads < 1 || heads > 16) {
2397 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2398 return -1;
2399 }
2400 if (secs < 1 || secs > 63) {
2401 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2402 return -1;
2403 }
2404 }
2405
2406 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2407 if (!cyls) {
2408 fprintf(stderr,
2409 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2410 str);
2411 return -1;
2412 }
2413 if (!strcmp(buf, "none"))
2414 translation = BIOS_ATA_TRANSLATION_NONE;
2415 else if (!strcmp(buf, "lba"))
2416 translation = BIOS_ATA_TRANSLATION_LBA;
2417 else if (!strcmp(buf, "auto"))
2418 translation = BIOS_ATA_TRANSLATION_AUTO;
2419 else {
2420 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2421 return -1;
2422 }
2423 }
2424
2425 if (get_param_value(buf, sizeof(buf), "media", str)) {
2426 if (!strcmp(buf, "disk")) {
2427 media = MEDIA_DISK;
2428 } else if (!strcmp(buf, "cdrom")) {
2429 if (cyls || secs || heads) {
2430 fprintf(stderr,
2431 "qemu: '%s' invalid physical CHS format\n", str);
2432 return -1;
2433 }
2434 media = MEDIA_CDROM;
2435 } else {
2436 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2437 return -1;
2438 }
2439 }
2440
2441 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2442 if (!strcmp(buf, "on"))
2443 snapshot = 1;
2444 else if (!strcmp(buf, "off"))
2445 snapshot = 0;
2446 else {
2447 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2448 return -1;
2449 }
2450 }
2451
balrog33f00272007-12-24 14:33:24 +00002452 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002453 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002454 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002455 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002456 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002457 else if (!strcmp(buf, "writeback"))
2458 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002459 else {
2460 fprintf(stderr, "qemu: invalid cache option\n");
2461 return -1;
2462 }
2463 }
2464
aurel321e72d3b2008-04-28 20:26:45 +00002465 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002466 if (strcmp(buf, "?") == 0) {
2467 fprintf(stderr, "qemu: Supported formats:");
2468 bdrv_iterate_format(bdrv_format_print, NULL);
2469 fprintf(stderr, "\n");
2470 return -1;
2471 }
aurel321e72d3b2008-04-28 20:26:45 +00002472 drv = bdrv_find_format(buf);
2473 if (!drv) {
2474 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2475 return -1;
2476 }
2477 }
2478
balrog609497a2008-01-14 02:56:53 +00002479 if (arg->file == NULL)
2480 get_param_value(file, sizeof(file), "file", str);
2481 else
2482 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002483
aliguorifa879c62009-01-07 17:32:33 +00002484 if (!get_param_value(serial, sizeof(serial), "serial", str))
2485 memset(serial, 0, sizeof(serial));
2486
aliguoricdad4bd2009-02-28 16:51:01 +00002487 onerror = BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002488 if (get_param_value(buf, sizeof(serial), "werror", str)) {
aliguori869a5c62009-01-22 19:52:25 +00002489 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002490 fprintf(stderr, "werror is no supported by this format\n");
aliguori428c5702009-01-21 18:59:04 +00002491 return -1;
2492 }
2493 if (!strcmp(buf, "ignore"))
2494 onerror = BLOCK_ERR_IGNORE;
2495 else if (!strcmp(buf, "enospc"))
2496 onerror = BLOCK_ERR_STOP_ENOSPC;
2497 else if (!strcmp(buf, "stop"))
2498 onerror = BLOCK_ERR_STOP_ANY;
2499 else if (!strcmp(buf, "report"))
2500 onerror = BLOCK_ERR_REPORT;
2501 else {
2502 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2503 return -1;
2504 }
2505 }
2506
thse4bcb142007-12-02 04:51:10 +00002507 /* compute bus and unit according index */
2508
2509 if (index != -1) {
2510 if (bus_id != 0 || unit_id != -1) {
2511 fprintf(stderr,
2512 "qemu: '%s' index cannot be used with bus and unit\n", str);
2513 return -1;
2514 }
2515 if (max_devs == 0)
2516 {
2517 unit_id = index;
2518 bus_id = 0;
2519 } else {
2520 unit_id = index % max_devs;
2521 bus_id = index / max_devs;
2522 }
2523 }
2524
2525 /* if user doesn't specify a unit_id,
2526 * try to find the first free
2527 */
2528
2529 if (unit_id == -1) {
2530 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002531 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002532 unit_id++;
2533 if (max_devs && unit_id >= max_devs) {
2534 unit_id -= max_devs;
2535 bus_id++;
2536 }
2537 }
2538 }
2539
2540 /* check unit id */
2541
2542 if (max_devs && unit_id >= max_devs) {
2543 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2544 str, unit_id, max_devs - 1);
2545 return -1;
2546 }
2547
2548 /*
2549 * ignore multiple definitions
2550 */
2551
thsf60d39b2007-12-17 03:55:57 +00002552 if (drive_get_index(type, bus_id, unit_id) != -1)
aliguori4d73cd32009-02-11 15:20:46 +00002553 return -2;
thse4bcb142007-12-02 04:51:10 +00002554
2555 /* init */
2556
thsf60d39b2007-12-17 03:55:57 +00002557 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002558 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002559 if (max_devs)
2560 snprintf(buf, sizeof(buf), "%s%i%s%i",
2561 devname, bus_id, mediastr, unit_id);
2562 else
2563 snprintf(buf, sizeof(buf), "%s%s%i",
2564 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002565 bdrv = bdrv_new(buf);
aliguori7d5aca92009-02-11 15:19:58 +00002566 drives_table_idx = drive_get_free_idx();
2567 drives_table[drives_table_idx].bdrv = bdrv;
2568 drives_table[drives_table_idx].type = type;
2569 drives_table[drives_table_idx].bus = bus_id;
2570 drives_table[drives_table_idx].unit = unit_id;
2571 drives_table[drives_table_idx].onerror = onerror;
aliguorib01b1112009-02-11 15:20:20 +00002572 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
aliguorifa879c62009-01-07 17:32:33 +00002573 strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002574 nb_drives++;
2575
thsf60d39b2007-12-17 03:55:57 +00002576 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002577 case IF_IDE:
2578 case IF_SCSI:
2579 switch(media) {
2580 case MEDIA_DISK:
2581 if (cyls != 0) {
2582 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2583 bdrv_set_translation_hint(bdrv, translation);
2584 }
2585 break;
2586 case MEDIA_CDROM:
2587 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2588 break;
2589 }
2590 break;
2591 case IF_SD:
2592 /* FIXME: This isn't really a floppy, but it's a reasonable
2593 approximation. */
2594 case IF_FLOPPY:
2595 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2596 break;
2597 case IF_PFLASH:
2598 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002599 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002600 break;
2601 }
2602 if (!file[0])
aliguori4d73cd32009-02-11 15:20:46 +00002603 return -2;
balrog33f00272007-12-24 14:33:24 +00002604 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002605 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002606 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002607 cache = 2; /* always use write-back with snapshot */
2608 }
2609 if (cache == 0) /* no caching */
2610 bdrv_flags |= BDRV_O_NOCACHE;
2611 else if (cache == 2) /* write-back */
2612 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori4dc822d2008-12-04 21:39:21 +00002613 else if (cache == 3) /* not specified */
2614 bdrv_flags |= BDRV_O_CACHE_DEF;
aliguoric0f4ce72009-03-05 23:01:01 +00002615 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002616 fprintf(stderr, "qemu: could not open disk image %s\n",
2617 file);
2618 return -1;
2619 }
aliguoric0f4ce72009-03-05 23:01:01 +00002620 if (bdrv_key_required(bdrv))
2621 autostart = 0;
aliguori4d73cd32009-02-11 15:20:46 +00002622 return drives_table_idx;
thse4bcb142007-12-02 04:51:10 +00002623}
2624
bellard330d0412003-07-26 18:11:40 +00002625/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002626/* USB devices */
2627
pbrook0d92ed32006-05-21 16:30:15 +00002628static USBPort *used_usb_ports;
2629static USBPort *free_usb_ports;
2630
2631/* ??? Maybe change this to register a hub to keep track of the topology. */
2632void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2633 usb_attachfn attach)
2634{
2635 port->opaque = opaque;
2636 port->index = index;
2637 port->attach = attach;
2638 port->next = free_usb_ports;
2639 free_usb_ports = port;
2640}
2641
aliguori4b096fc2008-08-21 19:28:55 +00002642int usb_device_add_dev(USBDevice *dev)
2643{
2644 USBPort *port;
2645
2646 /* Find a USB port to add the device to. */
2647 port = free_usb_ports;
2648 if (!port->next) {
2649 USBDevice *hub;
2650
2651 /* Create a new hub and chain it on. */
2652 free_usb_ports = NULL;
2653 port->next = used_usb_ports;
2654 used_usb_ports = port;
2655
2656 hub = usb_hub_init(VM_USB_HUB_SIZE);
2657 usb_attach(port, hub);
2658 port = free_usb_ports;
2659 }
2660
2661 free_usb_ports = port->next;
2662 port->next = used_usb_ports;
2663 used_usb_ports = port;
2664 usb_attach(port, dev);
2665 return 0;
2666}
2667
aliguoribb5fc202009-03-05 23:01:15 +00002668static void usb_msd_password_cb(void *opaque, int err)
2669{
2670 USBDevice *dev = opaque;
2671
2672 if (!err)
2673 usb_device_add_dev(dev);
2674 else
2675 dev->handle_destroy(dev);
2676}
2677
aliguoric0f4ce72009-03-05 23:01:01 +00002678static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002679{
2680 const char *p;
2681 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002682
pbrook0d92ed32006-05-21 16:30:15 +00002683 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002684 return -1;
2685
2686 if (strstart(devname, "host:", &p)) {
2687 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002688 } else if (!strcmp(devname, "mouse")) {
2689 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002690 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002691 dev = usb_tablet_init();
2692 } else if (!strcmp(devname, "keyboard")) {
2693 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002694 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002695 BlockDriverState *bs;
2696
aliguoribb5fc202009-03-05 23:01:15 +00002697 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002698 if (!dev)
2699 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002700 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002701 if (bdrv_key_required(bs)) {
2702 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002703 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002704 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2705 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002706 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002707 }
2708 }
balrogf6d2a312007-06-10 19:21:04 +00002709 } else if (!strcmp(devname, "wacom-tablet")) {
2710 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002711 } else if (strstart(devname, "serial:", &p)) {
2712 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002713#ifdef CONFIG_BRLAPI
2714 } else if (!strcmp(devname, "braille")) {
2715 dev = usb_baum_init();
2716#endif
balrog6c9f8862008-07-17 20:47:13 +00002717 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002718 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002719
balrog9ad97e62008-07-29 13:16:31 +00002720 if (net_client_init("nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002721 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002722 nd_table[nic].model = "usb";
2723 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002724 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2725 dev = usb_bt_init(devname[2] ? hci_init(p) :
2726 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002727 } else {
2728 return -1;
2729 }
pbrook0d92ed32006-05-21 16:30:15 +00002730 if (!dev)
2731 return -1;
2732
aliguori4b096fc2008-08-21 19:28:55 +00002733 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002734}
2735
aliguori1f3870a2008-08-21 19:27:48 +00002736int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002737{
pbrook0d92ed32006-05-21 16:30:15 +00002738 USBPort *port;
2739 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002740 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002741
pbrook0d92ed32006-05-21 16:30:15 +00002742 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002743 return -1;
2744
bellarda594cfb2005-11-06 16:13:29 +00002745 if (bus_num != 0)
2746 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002747
2748 lastp = &used_usb_ports;
2749 port = used_usb_ports;
2750 while (port && port->dev->addr != addr) {
2751 lastp = &port->next;
2752 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002753 }
pbrook0d92ed32006-05-21 16:30:15 +00002754
2755 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002756 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002757
bellard059809e2006-07-19 18:06:15 +00002758 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002759 *lastp = port->next;
2760 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002761 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002762 port->next = free_usb_ports;
2763 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002764 return 0;
2765}
2766
aliguori1f3870a2008-08-21 19:27:48 +00002767static int usb_device_del(const char *devname)
2768{
2769 int bus_num, addr;
2770 const char *p;
2771
aliguori5d0c5752008-09-14 01:07:41 +00002772 if (strstart(devname, "host:", &p))
2773 return usb_host_device_close(p);
2774
aliguori1f3870a2008-08-21 19:27:48 +00002775 if (!used_usb_ports)
2776 return -1;
2777
2778 p = strchr(devname, '.');
2779 if (!p)
2780 return -1;
2781 bus_num = strtoul(devname, NULL, 0);
2782 addr = strtoul(p + 1, NULL, 0);
2783
2784 return usb_device_del_addr(bus_num, addr);
2785}
2786
aliguori376253e2009-03-05 23:01:23 +00002787void do_usb_add(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002788{
aliguoric0f4ce72009-03-05 23:01:01 +00002789 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002790}
2791
aliguori376253e2009-03-05 23:01:23 +00002792void do_usb_del(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002793{
aliguori4b096fc2008-08-21 19:28:55 +00002794 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002795}
2796
aliguori376253e2009-03-05 23:01:23 +00002797void usb_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00002798{
2799 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002800 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002801 const char *speed_str;
2802
pbrook0d92ed32006-05-21 16:30:15 +00002803 if (!usb_enabled) {
aliguori376253e2009-03-05 23:01:23 +00002804 monitor_printf(mon, "USB support not enabled\n");
bellarda594cfb2005-11-06 16:13:29 +00002805 return;
2806 }
2807
pbrook0d92ed32006-05-21 16:30:15 +00002808 for (port = used_usb_ports; port; port = port->next) {
2809 dev = port->dev;
2810 if (!dev)
2811 continue;
2812 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002813 case USB_SPEED_LOW:
2814 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002815 break;
ths5fafdf22007-09-16 21:08:06 +00002816 case USB_SPEED_FULL:
2817 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002818 break;
ths5fafdf22007-09-16 21:08:06 +00002819 case USB_SPEED_HIGH:
2820 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002821 break;
2822 default:
ths5fafdf22007-09-16 21:08:06 +00002823 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002824 break;
bellarda594cfb2005-11-06 16:13:29 +00002825 }
aliguori376253e2009-03-05 23:01:23 +00002826 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2827 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002828 }
2829}
2830
bellardf7cce892004-12-08 22:21:25 +00002831/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002832/* PCMCIA/Cardbus */
2833
2834static struct pcmcia_socket_entry_s {
2835 struct pcmcia_socket_s *socket;
2836 struct pcmcia_socket_entry_s *next;
2837} *pcmcia_sockets = 0;
2838
2839void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2840{
2841 struct pcmcia_socket_entry_s *entry;
2842
2843 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2844 entry->socket = socket;
2845 entry->next = pcmcia_sockets;
2846 pcmcia_sockets = entry;
2847}
2848
2849void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2850{
2851 struct pcmcia_socket_entry_s *entry, **ptr;
2852
2853 ptr = &pcmcia_sockets;
2854 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2855 if (entry->socket == socket) {
2856 *ptr = entry->next;
2857 qemu_free(entry);
2858 }
2859}
2860
aliguori376253e2009-03-05 23:01:23 +00002861void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002862{
2863 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002864
balrog201a51f2007-04-30 00:51:09 +00002865 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002866 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002867
2868 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002869 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2870 iter->socket->attached ? iter->socket->card_string :
2871 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002872}
2873
2874/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002875/* register display */
2876
2877void register_displaystate(DisplayState *ds)
2878{
2879 DisplayState **s;
2880 s = &display_state;
2881 while (*s != NULL)
2882 s = &(*s)->next;
2883 ds->next = NULL;
2884 *s = ds;
2885}
2886
2887DisplayState *get_displaystate(void)
2888{
2889 return display_state;
2890}
2891
ths2ff89792007-06-21 23:34:19 +00002892/* dumb display */
2893
aliguori8f391ab2009-01-19 16:34:10 +00002894static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002895{
aliguori8f391ab2009-01-19 16:34:10 +00002896 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7da03b12009-01-21 18:58:51 +00002897 ds->surface = qemu_create_displaysurface(640, 480, 32, 640 * 4);
aliguori8f391ab2009-01-19 16:34:10 +00002898 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002899}
2900
2901/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002902/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002903
bellardc4b1fcc2004-03-14 21:44:30 +00002904#define MAX_IO_HANDLERS 64
2905
2906typedef struct IOHandlerRecord {
2907 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002908 IOCanRWHandler *fd_read_poll;
2909 IOHandler *fd_read;
2910 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002911 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002912 void *opaque;
2913 /* temporary data */
2914 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002915 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002916} IOHandlerRecord;
2917
bellard8a7ddc32004-03-31 19:00:16 +00002918static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002919
bellard7c9d8e02005-11-15 22:16:05 +00002920/* XXX: fd_read_poll should be suppressed, but an API change is
2921 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002922int qemu_set_fd_handler2(int fd,
2923 IOCanRWHandler *fd_read_poll,
2924 IOHandler *fd_read,
2925 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002926 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002927{
bellard8a7ddc32004-03-31 19:00:16 +00002928 IOHandlerRecord **pioh, *ioh;
2929
bellard7c9d8e02005-11-15 22:16:05 +00002930 if (!fd_read && !fd_write) {
2931 pioh = &first_io_handler;
2932 for(;;) {
2933 ioh = *pioh;
2934 if (ioh == NULL)
2935 break;
2936 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002937 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002938 break;
2939 }
2940 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002941 }
bellard7c9d8e02005-11-15 22:16:05 +00002942 } else {
2943 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2944 if (ioh->fd == fd)
2945 goto found;
2946 }
2947 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002948 ioh->next = first_io_handler;
2949 first_io_handler = ioh;
2950 found:
2951 ioh->fd = fd;
2952 ioh->fd_read_poll = fd_read_poll;
2953 ioh->fd_read = fd_read;
2954 ioh->fd_write = fd_write;
2955 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002956 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002957 }
bellard7c9d8e02005-11-15 22:16:05 +00002958 return 0;
2959}
2960
ths5fafdf22007-09-16 21:08:06 +00002961int qemu_set_fd_handler(int fd,
2962 IOHandler *fd_read,
2963 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002964 void *opaque)
2965{
2966 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002967}
2968
aliguori56f3a5d2008-10-31 18:07:17 +00002969#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002970/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002971/* Polling handling */
2972
2973typedef struct PollingEntry {
2974 PollingFunc *func;
2975 void *opaque;
2976 struct PollingEntry *next;
2977} PollingEntry;
2978
2979static PollingEntry *first_polling_entry;
2980
2981int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2982{
2983 PollingEntry **ppe, *pe;
2984 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002985 pe->func = func;
2986 pe->opaque = opaque;
2987 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2988 *ppe = pe;
2989 return 0;
2990}
2991
2992void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2993{
2994 PollingEntry **ppe, *pe;
2995 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2996 pe = *ppe;
2997 if (pe->func == func && pe->opaque == opaque) {
2998 *ppe = pe->next;
2999 qemu_free(pe);
3000 break;
3001 }
3002 }
3003}
3004
bellarda18e5242006-06-25 17:18:27 +00003005/***********************************************************/
3006/* Wait objects support */
3007typedef struct WaitObjects {
3008 int num;
3009 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
3010 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
3011 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
3012} WaitObjects;
3013
3014static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00003015
bellarda18e5242006-06-25 17:18:27 +00003016int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3017{
3018 WaitObjects *w = &wait_objects;
3019
3020 if (w->num >= MAXIMUM_WAIT_OBJECTS)
3021 return -1;
3022 w->events[w->num] = handle;
3023 w->func[w->num] = func;
3024 w->opaque[w->num] = opaque;
3025 w->num++;
3026 return 0;
3027}
3028
3029void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3030{
3031 int i, found;
3032 WaitObjects *w = &wait_objects;
3033
3034 found = 0;
3035 for (i = 0; i < w->num; i++) {
3036 if (w->events[i] == handle)
3037 found = 1;
3038 if (found) {
3039 w->events[i] = w->events[i + 1];
3040 w->func[i] = w->func[i + 1];
3041 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00003042 }
bellarda18e5242006-06-25 17:18:27 +00003043 }
3044 if (found)
3045 w->num--;
3046}
3047#endif
3048
bellard8a7ddc32004-03-31 19:00:16 +00003049/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003050/* ram save/restore */
3051
bellard8a7ddc32004-03-31 19:00:16 +00003052static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3053{
3054 int v;
3055
3056 v = qemu_get_byte(f);
3057 switch(v) {
3058 case 0:
3059 if (qemu_get_buffer(f, buf, len) != len)
3060 return -EIO;
3061 break;
3062 case 1:
3063 v = qemu_get_byte(f);
3064 memset(buf, v, len);
3065 break;
3066 default:
3067 return -EINVAL;
3068 }
aliguori871d2f02008-10-13 03:07:56 +00003069
3070 if (qemu_file_has_error(f))
3071 return -EIO;
3072
bellard8a7ddc32004-03-31 19:00:16 +00003073 return 0;
3074}
3075
bellardc88676f2006-08-06 13:36:11 +00003076static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003077{
aurel3200f82b82008-04-27 21:12:55 +00003078 int ret;
3079 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00003080
bellard8a7ddc32004-03-31 19:00:16 +00003081 if (qemu_get_be32(f) != phys_ram_size)
3082 return -EINVAL;
3083 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
3084 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
3085 if (ret)
3086 return ret;
3087 }
3088 return 0;
3089}
3090
bellardc88676f2006-08-06 13:36:11 +00003091#define BDRV_HASH_BLOCK_SIZE 1024
3092#define IOBUF_SIZE 4096
3093#define RAM_CBLOCK_MAGIC 0xfabe
3094
bellardc88676f2006-08-06 13:36:11 +00003095typedef struct RamDecompressState {
3096 z_stream zstream;
3097 QEMUFile *f;
3098 uint8_t buf[IOBUF_SIZE];
3099} RamDecompressState;
3100
3101static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3102{
3103 int ret;
3104 memset(s, 0, sizeof(*s));
3105 s->f = f;
3106 ret = inflateInit(&s->zstream);
3107 if (ret != Z_OK)
3108 return -1;
3109 return 0;
3110}
3111
3112static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3113{
3114 int ret, clen;
3115
3116 s->zstream.avail_out = len;
3117 s->zstream.next_out = buf;
3118 while (s->zstream.avail_out > 0) {
3119 if (s->zstream.avail_in == 0) {
3120 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3121 return -1;
3122 clen = qemu_get_be16(s->f);
3123 if (clen > IOBUF_SIZE)
3124 return -1;
3125 qemu_get_buffer(s->f, s->buf, clen);
3126 s->zstream.avail_in = clen;
3127 s->zstream.next_in = s->buf;
3128 }
3129 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3130 if (ret != Z_OK && ret != Z_STREAM_END) {
3131 return -1;
3132 }
3133 }
3134 return 0;
3135}
3136
3137static void ram_decompress_close(RamDecompressState *s)
3138{
3139 inflateEnd(&s->zstream);
3140}
3141
aliguori475e4272008-10-06 20:21:51 +00003142#define RAM_SAVE_FLAG_FULL 0x01
3143#define RAM_SAVE_FLAG_COMPRESS 0x02
3144#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3145#define RAM_SAVE_FLAG_PAGE 0x08
3146#define RAM_SAVE_FLAG_EOS 0x10
3147
3148static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003149{
aliguori475e4272008-10-06 20:21:51 +00003150 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3151 uint32_t *array = (uint32_t *)page;
3152 int i;
ths3b46e622007-09-17 08:09:54 +00003153
aliguori475e4272008-10-06 20:21:51 +00003154 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3155 if (array[i] != val)
3156 return 0;
bellardc88676f2006-08-06 13:36:11 +00003157 }
aliguori475e4272008-10-06 20:21:51 +00003158
3159 return 1;
bellardc88676f2006-08-06 13:36:11 +00003160}
3161
aliguori475e4272008-10-06 20:21:51 +00003162static int ram_save_block(QEMUFile *f)
3163{
3164 static ram_addr_t current_addr = 0;
3165 ram_addr_t saved_addr = current_addr;
3166 ram_addr_t addr = 0;
3167 int found = 0;
3168
3169 while (addr < phys_ram_size) {
3170 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3171 uint8_t ch;
3172
3173 cpu_physical_memory_reset_dirty(current_addr,
3174 current_addr + TARGET_PAGE_SIZE,
3175 MIGRATION_DIRTY_FLAG);
3176
3177 ch = *(phys_ram_base + current_addr);
3178
3179 if (is_dup_page(phys_ram_base + current_addr, ch)) {
3180 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3181 qemu_put_byte(f, ch);
3182 } else {
3183 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3184 qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
3185 }
3186
3187 found = 1;
3188 break;
3189 }
3190 addr += TARGET_PAGE_SIZE;
3191 current_addr = (saved_addr + addr) % phys_ram_size;
3192 }
3193
3194 return found;
3195}
3196
3197static ram_addr_t ram_save_threshold = 10;
3198
3199static ram_addr_t ram_save_remaining(void)
3200{
3201 ram_addr_t addr;
3202 ram_addr_t count = 0;
3203
3204 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3205 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3206 count++;
3207 }
3208
3209 return count;
3210}
3211
3212static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3213{
3214 ram_addr_t addr;
3215
3216 if (stage == 1) {
3217 /* Make sure all dirty bits are set */
3218 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3219 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3220 cpu_physical_memory_set_dirty(addr);
3221 }
3222
3223 /* Enable dirty memory tracking */
3224 cpu_physical_memory_set_dirty_tracking(1);
3225
3226 qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
3227 }
3228
3229 while (!qemu_file_rate_limit(f)) {
3230 int ret;
3231
3232 ret = ram_save_block(f);
3233 if (ret == 0) /* no more blocks */
3234 break;
3235 }
3236
3237 /* try transferring iterative blocks of memory */
3238
3239 if (stage == 3) {
3240 cpu_physical_memory_set_dirty_tracking(0);
3241
3242 /* flush all remaining blocks regardless of rate limiting */
3243 while (ram_save_block(f) != 0);
3244 }
3245
3246 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3247
3248 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3249}
3250
3251static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003252{
3253 RamDecompressState s1, *s = &s1;
3254 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003255 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003256
bellardc88676f2006-08-06 13:36:11 +00003257 if (ram_decompress_open(s, f) < 0)
3258 return -EINVAL;
3259 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
3260 if (ram_decompress_buf(s, buf, 1) < 0) {
3261 fprintf(stderr, "Error while reading ram block header\n");
3262 goto error;
3263 }
3264 if (buf[0] == 0) {
3265 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003266 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003267 goto error;
3268 }
aliguori475e4272008-10-06 20:21:51 +00003269 } else {
bellardc88676f2006-08-06 13:36:11 +00003270 error:
3271 printf("Error block header\n");
3272 return -EINVAL;
3273 }
3274 }
3275 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003276
3277 return 0;
3278}
3279
3280static int ram_load(QEMUFile *f, void *opaque, int version_id)
3281{
3282 ram_addr_t addr;
3283 int flags;
3284
3285 if (version_id == 1)
3286 return ram_load_v1(f, opaque);
3287
3288 if (version_id == 2) {
3289 if (qemu_get_be32(f) != phys_ram_size)
3290 return -EINVAL;
3291 return ram_load_dead(f, opaque);
3292 }
3293
3294 if (version_id != 3)
3295 return -EINVAL;
3296
3297 do {
3298 addr = qemu_get_be64(f);
3299
3300 flags = addr & ~TARGET_PAGE_MASK;
3301 addr &= TARGET_PAGE_MASK;
3302
3303 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3304 if (addr != phys_ram_size)
3305 return -EINVAL;
3306 }
3307
3308 if (flags & RAM_SAVE_FLAG_FULL) {
3309 if (ram_load_dead(f, opaque) < 0)
3310 return -EINVAL;
3311 }
3312
3313 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3314 uint8_t ch = qemu_get_byte(f);
3315 memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
3316 } else if (flags & RAM_SAVE_FLAG_PAGE)
3317 qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
3318 } while (!(flags & RAM_SAVE_FLAG_EOS));
3319
bellardc88676f2006-08-06 13:36:11 +00003320 return 0;
3321}
3322
aliguori9e472e12008-10-08 19:50:24 +00003323void qemu_service_io(void)
3324{
3325 CPUState *env = cpu_single_env;
3326 if (env) {
3327 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3328#ifdef USE_KQEMU
3329 if (env->kqemu_enabled) {
3330 kqemu_cpu_interrupt(env);
3331 }
3332#endif
3333 }
3334}
3335
bellard8a7ddc32004-03-31 19:00:16 +00003336/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003337/* bottom halves (can be seen as timers which expire ASAP) */
3338
3339struct QEMUBH {
3340 QEMUBHFunc *cb;
3341 void *opaque;
3342 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003343 int idle;
3344 int deleted;
bellard83f64092006-08-01 16:21:11 +00003345 QEMUBH *next;
3346};
3347
3348static QEMUBH *first_bh = NULL;
3349
3350QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3351{
3352 QEMUBH *bh;
3353 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003354 bh->cb = cb;
3355 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003356 bh->next = first_bh;
3357 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003358 return bh;
3359}
3360
bellard6eb57332006-08-06 09:51:25 +00003361int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003362{
aliguori1b435b12008-10-31 17:24:21 +00003363 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003364 int ret;
bellard83f64092006-08-01 16:21:11 +00003365
bellard6eb57332006-08-06 09:51:25 +00003366 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003367 for (bh = first_bh; bh; bh = bh->next) {
3368 if (!bh->deleted && bh->scheduled) {
3369 bh->scheduled = 0;
3370 if (!bh->idle)
3371 ret = 1;
3372 bh->idle = 0;
3373 bh->cb(bh->opaque);
3374 }
bellard83f64092006-08-01 16:21:11 +00003375 }
aliguori1b435b12008-10-31 17:24:21 +00003376
3377 /* remove deleted bhs */
3378 bhp = &first_bh;
3379 while (*bhp) {
3380 bh = *bhp;
3381 if (bh->deleted) {
3382 *bhp = bh->next;
3383 qemu_free(bh);
3384 } else
3385 bhp = &bh->next;
3386 }
3387
bellard6eb57332006-08-06 09:51:25 +00003388 return ret;
bellard83f64092006-08-01 16:21:11 +00003389}
3390
aliguori1b435b12008-10-31 17:24:21 +00003391void qemu_bh_schedule_idle(QEMUBH *bh)
3392{
3393 if (bh->scheduled)
3394 return;
3395 bh->scheduled = 1;
3396 bh->idle = 1;
3397}
3398
bellard83f64092006-08-01 16:21:11 +00003399void qemu_bh_schedule(QEMUBH *bh)
3400{
3401 CPUState *env = cpu_single_env;
3402 if (bh->scheduled)
3403 return;
3404 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003405 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003406 /* stop the currently executing CPU to execute the BH ASAP */
3407 if (env) {
3408 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3409 }
3410}
3411
3412void qemu_bh_cancel(QEMUBH *bh)
3413{
aliguori1b435b12008-10-31 17:24:21 +00003414 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003415}
3416
3417void qemu_bh_delete(QEMUBH *bh)
3418{
aliguori1b435b12008-10-31 17:24:21 +00003419 bh->scheduled = 0;
3420 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003421}
3422
aliguori56f3a5d2008-10-31 18:07:17 +00003423static void qemu_bh_update_timeout(int *timeout)
3424{
3425 QEMUBH *bh;
3426
3427 for (bh = first_bh; bh; bh = bh->next) {
3428 if (!bh->deleted && bh->scheduled) {
3429 if (bh->idle) {
3430 /* idle bottom halves will be polled at least
3431 * every 10ms */
3432 *timeout = MIN(10, *timeout);
3433 } else {
3434 /* non-idle bottom halves will be executed
3435 * immediately */
3436 *timeout = 0;
3437 break;
3438 }
3439 }
3440 }
3441}
3442
bellard83f64092006-08-01 16:21:11 +00003443/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003444/* machine registration */
3445
blueswir1bdaf78e2008-10-04 07:24:27 +00003446static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003447QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003448
3449int qemu_register_machine(QEMUMachine *m)
3450{
3451 QEMUMachine **pm;
3452 pm = &first_machine;
3453 while (*pm != NULL)
3454 pm = &(*pm)->next;
3455 m->next = NULL;
3456 *pm = m;
3457 return 0;
3458}
3459
pbrook9596ebb2007-11-18 01:44:38 +00003460static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003461{
3462 QEMUMachine *m;
3463
3464 for(m = first_machine; m != NULL; m = m->next) {
3465 if (!strcmp(m->name, name))
3466 return m;
3467 }
3468 return NULL;
3469}
3470
3471/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003472/* main execution loop */
3473
pbrook9596ebb2007-11-18 01:44:38 +00003474static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003475{
aliguori7d957bd2009-01-15 22:14:11 +00003476 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003477 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003478 DisplayChangeListener *dcl = ds->listeners;
3479
3480 dpy_refresh(ds);
3481
3482 while (dcl != NULL) {
3483 if (dcl->gui_timer_interval &&
3484 dcl->gui_timer_interval < interval)
3485 interval = dcl->gui_timer_interval;
3486 dcl = dcl->next;
3487 }
3488 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003489}
3490
blueswir19043b622009-01-21 19:28:13 +00003491static void nographic_update(void *opaque)
3492{
3493 uint64_t interval = GUI_REFRESH_INTERVAL;
3494
3495 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3496}
3497
bellard0bd48852005-11-11 00:00:47 +00003498struct vm_change_state_entry {
3499 VMChangeStateHandler *cb;
3500 void *opaque;
3501 LIST_ENTRY (vm_change_state_entry) entries;
3502};
3503
3504static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3505
3506VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3507 void *opaque)
3508{
3509 VMChangeStateEntry *e;
3510
3511 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003512
3513 e->cb = cb;
3514 e->opaque = opaque;
3515 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3516 return e;
3517}
3518
3519void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3520{
3521 LIST_REMOVE (e, entries);
3522 qemu_free (e);
3523}
3524
aliguori9781e042009-01-22 17:15:29 +00003525static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003526{
3527 VMChangeStateEntry *e;
3528
3529 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003530 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003531 }
3532}
3533
bellard8a7ddc32004-03-31 19:00:16 +00003534void vm_start(void)
3535{
3536 if (!vm_running) {
3537 cpu_enable_ticks();
3538 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003539 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003540 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00003541 }
3542}
3543
ths5fafdf22007-09-16 21:08:06 +00003544void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00003545{
3546 if (vm_running) {
3547 cpu_disable_ticks();
3548 vm_running = 0;
aliguori9781e042009-01-22 17:15:29 +00003549 vm_state_notify(0, reason);
bellard8a7ddc32004-03-31 19:00:16 +00003550 }
3551}
3552
bellardbb0c6722004-06-20 12:37:32 +00003553/* reset/shutdown handler */
3554
3555typedef struct QEMUResetEntry {
3556 QEMUResetHandler *func;
3557 void *opaque;
3558 struct QEMUResetEntry *next;
3559} QEMUResetEntry;
3560
3561static QEMUResetEntry *first_reset_entry;
3562static int reset_requested;
3563static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003564static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00003565
aurel32cf7a2fe2008-03-18 06:53:05 +00003566int qemu_shutdown_requested(void)
3567{
3568 int r = shutdown_requested;
3569 shutdown_requested = 0;
3570 return r;
3571}
3572
3573int qemu_reset_requested(void)
3574{
3575 int r = reset_requested;
3576 reset_requested = 0;
3577 return r;
3578}
3579
3580int qemu_powerdown_requested(void)
3581{
3582 int r = powerdown_requested;
3583 powerdown_requested = 0;
3584 return r;
3585}
3586
bellardbb0c6722004-06-20 12:37:32 +00003587void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3588{
3589 QEMUResetEntry **pre, *re;
3590
3591 pre = &first_reset_entry;
3592 while (*pre != NULL)
3593 pre = &(*pre)->next;
3594 re = qemu_mallocz(sizeof(QEMUResetEntry));
3595 re->func = func;
3596 re->opaque = opaque;
3597 re->next = NULL;
3598 *pre = re;
3599}
3600
aurel32cf7a2fe2008-03-18 06:53:05 +00003601void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00003602{
3603 QEMUResetEntry *re;
3604
3605 /* reset all devices */
3606 for(re = first_reset_entry; re != NULL; re = re->next) {
3607 re->func(re->opaque);
3608 }
3609}
3610
3611void qemu_system_reset_request(void)
3612{
bellardd1beab82006-10-02 19:44:22 +00003613 if (no_reboot) {
3614 shutdown_requested = 1;
3615 } else {
3616 reset_requested = 1;
3617 }
bellard6a00d602005-11-21 23:25:50 +00003618 if (cpu_single_env)
3619 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003620}
3621
3622void qemu_system_shutdown_request(void)
3623{
3624 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003625 if (cpu_single_env)
3626 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003627}
3628
bellard34751872005-07-02 14:31:34 +00003629void qemu_system_powerdown_request(void)
3630{
3631 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003632 if (cpu_single_env)
3633 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003634}
3635
ths877cf882007-04-18 18:11:47 +00003636#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003637static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003638{
3639 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003640 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003641
bellardf3311102006-04-12 20:21:17 +00003642
3643 /* XXX: need to suppress polling by better using win32 events */
3644 ret = 0;
3645 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3646 ret |= pe->func(pe->opaque);
3647 }
thse6b1e552007-04-18 17:56:02 +00003648 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003649 int err;
3650 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003651
aliguori56f3a5d2008-10-31 18:07:17 +00003652 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003653 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3654 if (w->func[ret - WAIT_OBJECT_0])
3655 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003656
ths5fafdf22007-09-16 21:08:06 +00003657 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003658 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003659
thse6b1e552007-04-18 17:56:02 +00003660 /* Check if event is signaled */
3661 ret2 = WaitForSingleObject(w->events[i], 0);
3662 if(ret2 == WAIT_OBJECT_0) {
3663 if (w->func[i])
3664 w->func[i](w->opaque[i]);
3665 } else if (ret2 == WAIT_TIMEOUT) {
3666 } else {
3667 err = GetLastError();
3668 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003669 }
3670 }
bellarda18e5242006-06-25 17:18:27 +00003671 } else if (ret == WAIT_TIMEOUT) {
3672 } else {
3673 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003674 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003675 }
bellardf3311102006-04-12 20:21:17 +00003676 }
aliguori56f3a5d2008-10-31 18:07:17 +00003677
3678 *timeout = 0;
3679}
3680#else
blueswir169d64512008-12-07 19:30:18 +00003681static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003682{
3683}
bellardfd1dff42006-02-01 21:29:26 +00003684#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003685
3686void main_loop_wait(int timeout)
3687{
3688 IOHandlerRecord *ioh;
3689 fd_set rfds, wfds, xfds;
3690 int ret, nfds;
3691 struct timeval tv;
3692
3693 qemu_bh_update_timeout(&timeout);
3694
3695 host_main_loop_wait(&timeout);
3696
bellardfd1dff42006-02-01 21:29:26 +00003697 /* poll any events */
3698 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003699 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003700 FD_ZERO(&rfds);
3701 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003702 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003703 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003704 if (ioh->deleted)
3705 continue;
bellardfd1dff42006-02-01 21:29:26 +00003706 if (ioh->fd_read &&
3707 (!ioh->fd_read_poll ||
3708 ioh->fd_read_poll(ioh->opaque) != 0)) {
3709 FD_SET(ioh->fd, &rfds);
3710 if (ioh->fd > nfds)
3711 nfds = ioh->fd;
3712 }
3713 if (ioh->fd_write) {
3714 FD_SET(ioh->fd, &wfds);
3715 if (ioh->fd > nfds)
3716 nfds = ioh->fd;
3717 }
3718 }
ths3b46e622007-09-17 08:09:54 +00003719
aliguori56f3a5d2008-10-31 18:07:17 +00003720 tv.tv_sec = timeout / 1000;
3721 tv.tv_usec = (timeout % 1000) * 1000;
3722
bellarde0356492006-05-01 13:33:02 +00003723#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003724 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003725 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3726 }
3727#endif
3728 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00003729 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003730 IOHandlerRecord **pioh;
3731
3732 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003733 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003734 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003735 }
ths6ab43fd2007-08-25 01:34:19 +00003736 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003737 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003738 }
3739 }
thscafffd42007-02-28 21:59:44 +00003740
3741 /* remove deleted IO handlers */
3742 pioh = &first_io_handler;
3743 while (*pioh) {
3744 ioh = *pioh;
3745 if (ioh->deleted) {
3746 *pioh = ioh->next;
3747 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003748 } else
thscafffd42007-02-28 21:59:44 +00003749 pioh = &ioh->next;
3750 }
bellardfd1dff42006-02-01 21:29:26 +00003751 }
bellarde0356492006-05-01 13:33:02 +00003752#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003753 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003754 if (ret < 0) {
3755 FD_ZERO(&rfds);
3756 FD_ZERO(&wfds);
3757 FD_ZERO(&xfds);
3758 }
3759 slirp_select_poll(&rfds, &wfds, &xfds);
3760 }
3761#endif
bellardc20709a2004-04-21 23:27:19 +00003762
aliguori357c6922008-11-25 17:26:09 +00003763 /* vm time timers */
3764 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3765 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3766 qemu_get_clock(vm_clock));
3767
3768 /* real time timers */
3769 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3770 qemu_get_clock(rt_clock));
3771
pbrook423f0742007-05-23 00:06:54 +00003772 /* Check bottom-halves last in case any of the earlier events triggered
3773 them. */
3774 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003775
bellard5905b2e2004-08-01 21:53:26 +00003776}
3777
pbrook9596ebb2007-11-18 01:44:38 +00003778static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00003779{
3780 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00003781#ifdef CONFIG_PROFILER
3782 int64_t ti;
3783#endif
bellard6a00d602005-11-21 23:25:50 +00003784 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003785
bellard6a00d602005-11-21 23:25:50 +00003786 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00003787 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00003788 for(;;) {
3789 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00003790
bellard15a76442005-11-23 21:01:03 +00003791 for(;;) {
3792 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00003793 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00003794#ifdef CONFIG_PROFILER
3795 ti = profile_getclock();
3796#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003797 if (use_icount) {
3798 int64_t count;
3799 int decr;
3800 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3801 env->icount_decr.u16.low = 0;
3802 env->icount_extra = 0;
3803 count = qemu_next_deadline();
3804 count = (count + (1 << icount_time_shift) - 1)
3805 >> icount_time_shift;
3806 qemu_icount += count;
3807 decr = (count > 0xffff) ? 0xffff : count;
3808 count -= decr;
3809 env->icount_decr.u16.low = decr;
3810 env->icount_extra = count;
3811 }
bellard6a00d602005-11-21 23:25:50 +00003812 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00003813#ifdef CONFIG_PROFILER
3814 qemu_time += profile_getclock() - ti;
3815#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003816 if (use_icount) {
3817 /* Fold pending instructions back into the
3818 instruction counter, and clear the interrupt flag. */
3819 qemu_icount -= (env->icount_decr.u16.low
3820 + env->icount_extra);
3821 env->icount_decr.u32 = 0;
3822 env->icount_extra = 0;
3823 }
balrogee5605e2007-12-03 03:01:40 +00003824 next_cpu = env->next_cpu ?: first_cpu;
aurel3295b01002008-04-04 17:16:35 +00003825 if (event_pending && likely(ret != EXCP_DEBUG)) {
balrogee5605e2007-12-03 03:01:40 +00003826 ret = EXCP_INTERRUPT;
3827 event_pending = 0;
3828 break;
3829 }
pbrookbd967e02007-03-11 18:54:57 +00003830 if (ret == EXCP_HLT) {
3831 /* Give the next CPU a chance to run. */
3832 cur_cpu = env;
3833 continue;
3834 }
bellard15a76442005-11-23 21:01:03 +00003835 if (ret != EXCP_HALTED)
3836 break;
3837 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00003838 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00003839 break;
bellard15a76442005-11-23 21:01:03 +00003840 }
3841 cur_cpu = env;
3842
bellard5905b2e2004-08-01 21:53:26 +00003843 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00003844 ret = EXCP_INTERRUPT;
aurel32b2f76162008-04-11 21:35:52 +00003845 if (no_shutdown) {
3846 vm_stop(0);
3847 no_shutdown = 0;
3848 }
3849 else
3850 break;
bellard5905b2e2004-08-01 21:53:26 +00003851 }
3852 if (reset_requested) {
3853 reset_requested = 0;
3854 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00003855 ret = EXCP_INTERRUPT;
3856 }
3857 if (powerdown_requested) {
3858 powerdown_requested = 0;
3859 qemu_system_powerdown();
3860 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00003861 }
aurel3295b01002008-04-04 17:16:35 +00003862 if (unlikely(ret == EXCP_DEBUG)) {
aliguori880a7572008-11-18 20:30:24 +00003863 gdb_set_stop_cpu(cur_cpu);
bellard5905b2e2004-08-01 21:53:26 +00003864 vm_stop(EXCP_DEBUG);
3865 }
pbrookbd967e02007-03-11 18:54:57 +00003866 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00003867 /* XXX: use timeout computed from timers */
pbrook2e70f6e2008-06-29 01:03:05 +00003868 if (ret == EXCP_HALTED) {
3869 if (use_icount) {
3870 int64_t add;
3871 int64_t delta;
3872 /* Advance virtual time to the next event. */
3873 if (use_icount == 1) {
3874 /* When not using an adaptive execution frequency
3875 we tend to get badly out of sync with real time,
thsbf20dc02008-06-30 17:22:19 +00003876 so just delay for a reasonable amount of time. */
pbrook2e70f6e2008-06-29 01:03:05 +00003877 delta = 0;
3878 } else {
3879 delta = cpu_get_icount() - cpu_get_clock();
3880 }
3881 if (delta > 0) {
3882 /* If virtual time is ahead of real time then just
3883 wait for IO. */
3884 timeout = (delta / 1000000) + 1;
3885 } else {
3886 /* Wait for either IO to occur or the next
3887 timer event. */
3888 add = qemu_next_deadline();
3889 /* We advance the timer before checking for IO.
3890 Limit the amount we advance so that early IO
3891 activity won't get the guest too far ahead. */
3892 if (add > 10000000)
3893 add = 10000000;
3894 delta += add;
3895 add = (add + (1 << icount_time_shift) - 1)
3896 >> icount_time_shift;
3897 qemu_icount += add;
3898 timeout = delta / 1000000;
3899 if (timeout < 0)
3900 timeout = 0;
3901 }
3902 } else {
aliguori0a1af392008-10-31 18:40:25 +00003903 timeout = 5000;
pbrook2e70f6e2008-06-29 01:03:05 +00003904 }
3905 } else {
bellard5905b2e2004-08-01 21:53:26 +00003906 timeout = 0;
pbrook2e70f6e2008-06-29 01:03:05 +00003907 }
bellard5905b2e2004-08-01 21:53:26 +00003908 } else {
blueswir198448f52008-09-30 18:16:09 +00003909 if (shutdown_requested) {
3910 ret = EXCP_INTERRUPT;
aliguori5b08fc12008-08-21 20:08:03 +00003911 break;
blueswir198448f52008-09-30 18:16:09 +00003912 }
aliguori0a1af392008-10-31 18:40:25 +00003913 timeout = 5000;
bellard5905b2e2004-08-01 21:53:26 +00003914 }
bellard89bfc102006-02-08 22:46:31 +00003915#ifdef CONFIG_PROFILER
3916 ti = profile_getclock();
3917#endif
bellard5905b2e2004-08-01 21:53:26 +00003918 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00003919#ifdef CONFIG_PROFILER
3920 dev_time += profile_getclock() - ti;
3921#endif
bellardb4608c02003-06-27 17:34:32 +00003922 }
bellard34865132003-10-05 14:28:56 +00003923 cpu_disable_ticks();
3924 return ret;
bellardb4608c02003-06-27 17:34:32 +00003925}
3926
ths15f82202007-06-29 23:26:08 +00003927static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00003928{
blueswir1d2c639d2009-01-24 18:19:25 +00003929 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3930 and qemu-doc.texi */
bellard68d0f702008-01-06 17:21:48 +00003931 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00003932 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00003933 "\n"
bellarda20dd502003-09-30 21:07:02 +00003934 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00003935 "\n"
bellarda20dd502003-09-30 21:07:02 +00003936 "Standard options:\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003937 "-h or -help display this help and exit\n"
bellardcc1daa42005-06-05 14:49:17 +00003938 "-M machine select emulated machine (-M ? for list)\n"
pbrook5adb4832007-03-08 03:15:18 +00003939 "-cpu cpu select CPU (-cpu ? for list)\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003940 "-smp n set the number of CPUs to 'n' [default=1]\n"
bellardc45886d2004-01-05 00:02:06 +00003941 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00003942 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3943 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003944 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
aurel32a1620fa2008-04-29 05:58:01 +00003945 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3946 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
aliguorifa879c62009-01-07 17:32:33 +00003947 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
thse4bcb142007-12-02 04:51:10 +00003948 " use 'file' as a drive image\n"
balrog3e3d5812007-04-30 02:09:25 +00003949 "-mtdblock file use 'file' as on-board Flash memory image\n"
pbrooka1bb27b2007-04-06 16:49:48 +00003950 "-sd file use 'file' as SecureDigital card image\n"
j_mayer86f55662007-04-24 06:52:59 +00003951 "-pflash file use 'file' as a parallel flash image\n"
thseec85c22007-01-05 17:41:07 +00003952 "-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 +00003953 "-snapshot write to temporary files instead of disk image files\n"
bellarda00bad72004-05-22 21:39:06 +00003954 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellard4ca00742004-12-12 22:20:04 +00003955#ifndef _WIN32
ths667acca2006-12-11 02:08:05 +00003956 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00003957#endif
bellard1d14ffa2005-10-30 18:58:22 +00003958#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00003959 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00003960 "-soundhw c1,... enable audio support\n"
3961 " and only specified sound cards (comma separated list)\n"
3962 " use -soundhw ? to get the list of supported cards\n"
bellard6a36d842005-12-18 20:34:32 +00003963 " use -soundhw all to enable all of them\n"
bellard1d14ffa2005-10-30 18:58:22 +00003964#endif
bellardb389dbf2005-11-06 16:49:55 +00003965 "-usb enable the USB driver (will be the default soon)\n"
3966 "-usbdevice name add the host or guest USB device 'name'\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003967 "-name string set the name of the guest\n"
3968 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
3969 " specify machine UUID\n"
3970 "\n"
3971 "Display options:\n"
3972 "-nographic disable graphical output and redirect serial I/Os to console\n"
3973#ifdef CONFIG_CURSES
3974 "-curses use a curses/ncurses interface instead of SDL\n"
3975#endif
3976#ifdef CONFIG_SDL
3977 "-no-frame open SDL window without a frame and window decorations\n"
3978 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3979 "-no-quit disable SDL window close capability\n"
3980 "-sdl enable SDL\n"
3981#endif
3982 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3983 "-vga [std|cirrus|vmware|none]\n"
3984 " select video card type\n"
3985 "-full-screen start in full screen\n"
bellard6f7e9ae2005-03-13 09:43:36 +00003986#if defined(TARGET_PPC) || defined(TARGET_SPARC)
3987 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00003988#endif
blueswir1d2c639d2009-01-24 18:19:25 +00003989 "-vnc display start a VNC server on display\n"
bellarda20dd502003-09-30 21:07:02 +00003990 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003991 "Network options:\n"
aliguori7a9f6e42009-01-07 17:48:51 +00003992 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
bellard7c9d8e02005-11-15 22:16:05 +00003993 " create a new Network Interface Card and connect it to VLAN 'n'\n"
bellardc20709a2004-04-21 23:27:19 +00003994#ifdef CONFIG_SLIRP
aliguori7a9f6e42009-01-07 17:48:51 +00003995 "-net user[,vlan=n][,name=str][,hostname=host]\n"
pbrook115defd2006-04-16 11:06:58 +00003996 " connect the user mode network stack to VLAN 'n' and send\n"
3997 " hostname 'host' to DHCP clients\n"
bellard7c9d8e02005-11-15 22:16:05 +00003998#endif
bellard7fb843f2006-02-01 23:06:55 +00003999#ifdef _WIN32
aliguori7a9f6e42009-01-07 17:48:51 +00004000 "-net tap[,vlan=n][,name=str],ifname=name\n"
bellard7fb843f2006-02-01 23:06:55 +00004001 " connect the host TAP network interface to VLAN 'n'\n"
4002#else
aliguori7a9f6e42009-01-07 17:48:51 +00004003 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
thsb46a8902007-10-21 23:20:45 +00004004 " connect the host TAP network interface to VLAN 'n' and use the\n"
4005 " network scripts 'file' (default=%s)\n"
4006 " and 'dfile' (default=%s);\n"
4007 " use '[down]script=no' to disable script execution;\n"
bellard7c9d8e02005-11-15 22:16:05 +00004008 " use 'fd=h' to connect to an already opened TAP interface\n"
bellard7fb843f2006-02-01 23:06:55 +00004009#endif
aliguori7a9f6e42009-01-07 17:48:51 +00004010 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
bellard7c9d8e02005-11-15 22:16:05 +00004011 " connect the vlan 'n' to another VLAN using a socket connection\n"
aliguori7a9f6e42009-01-07 17:48:51 +00004012 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
bellard3d830452005-12-18 16:36:49 +00004013 " connect the vlan 'n' to multicast maddr and port\n"
ths8a16d272008-07-19 09:56:24 +00004014#ifdef CONFIG_VDE
aliguori7a9f6e42009-01-07 17:48:51 +00004015 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
ths8a16d272008-07-19 09:56:24 +00004016 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4017 " on host and listening for incoming connections on 'socketpath'.\n"
4018 " Use group 'groupname' and mode 'octalmode' to change default\n"
4019 " ownership and permissions for communication port.\n"
4020#endif
bellard7c9d8e02005-11-15 22:16:05 +00004021 "-net none use it alone to have zero network devices; if no -net option\n"
4022 " is provided, the default is '-net nic -net user'\n"
bellard7c9d8e02005-11-15 22:16:05 +00004023#ifdef CONFIG_SLIRP
ths0db11372007-02-20 00:12:07 +00004024 "-tftp dir allow tftp access to files in dir [-net user]\n"
ths47d5d012007-02-20 00:05:08 +00004025 "-bootp file advertise file in BOOTP replies\n"
bellard7c9d8e02005-11-15 22:16:05 +00004026#ifndef _WIN32
4027 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
bellardc94c8d62004-09-13 21:37:34 +00004028#endif
bellard9bf05442004-08-25 22:12:49 +00004029 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
bellard7c9d8e02005-11-15 22:16:05 +00004030 " redirect TCP or UDP connections from host to guest [-net user]\n"
bellardc20709a2004-04-21 23:27:19 +00004031#endif
bellardc4b1fcc2004-03-14 21:44:30 +00004032 "\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004033 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
4034 "-bt hci,host[:id]\n"
4035 " use host's HCI with the given name\n"
4036 "-bt hci[,vlan=n]\n"
4037 " emulate a standard HCI in virtual scatternet 'n'\n"
4038 "-bt vhci[,vlan=n]\n"
4039 " add host computer to virtual scatternet 'n' using VHCI\n"
4040 "-bt device:dev[,vlan=n]\n"
4041 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4042 "\n"
4043#ifdef TARGET_I386
4044 "\n"
4045 "i386 target only:\n"
4046 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4047 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4048 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4049 "-no-acpi disable ACPI\n"
4050 "-no-hpet disable HPET\n"
aliguori8a92ea22009-02-27 20:12:36 +00004051 "-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"
4052 " ACPI table description\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004053#endif
bellardc4b1fcc2004-03-14 21:44:30 +00004054 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00004055 "-kernel bzImage use 'bzImage' as kernel image\n"
4056 "-append cmdline use 'cmdline' as kernel command line\n"
4057 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00004058 "\n"
bellard330d0412003-07-26 18:11:40 +00004059 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00004060 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00004061 "-parallel dev redirect the parallel port to char device 'dev'\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004062 "-monitor dev redirect the monitor to char device 'dev'\n"
4063 "-pidfile file write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00004064 "-S freeze CPU at startup (use 'c' to start execution)\n"
pbrookcfc34752007-02-22 01:48:01 +00004065 "-s wait gdb connection to port\n"
4066 "-p port set gdb connection port [default=%s]\n"
bellardf193c792004-03-21 17:06:25 +00004067 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004068 "-hdachs c,h,s[,t]\n"
4069 " force hard disk 0 physical geometry and the optional BIOS\n"
bellard46d47672004-11-16 01:45:27 +00004070 " translation (t=none or lba) (usually qemu can guess them)\n"
bellard87b47352006-08-17 17:22:54 +00004071 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004072 "-bios file set the filename for the BIOS\n"
bellardd993e022005-02-10 22:00:06 +00004073#ifdef USE_KQEMU
bellard6515b202006-05-03 22:02:44 +00004074 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
bellardd993e022005-02-10 22:00:06 +00004075 "-no-kqemu disable KQEMU kernel module usage\n"
4076#endif
aliguori7ba1e612008-11-05 16:04:33 +00004077#ifdef CONFIG_KVM
4078 "-enable-kvm enable KVM full virtualization support\n"
4079#endif
bellardd1beab82006-10-02 19:44:22 +00004080 "-no-reboot exit instead of rebooting\n"
aurel32b2f76162008-04-11 21:35:52 +00004081 "-no-shutdown stop before shutdown\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004082 "-loadvm [tag|id]\n"
4083 " start right away with a saved state (loadvm in monitor)\n"
ths71e3ceb2006-12-22 02:11:31 +00004084#ifndef _WIN32
4085 "-daemonize daemonize QEMU after initializing\n"
4086#endif
ths9ae02552007-01-05 17:39:04 +00004087 "-option-rom rom load a file, rom, into the option ROM space\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004088#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4089 "-prom-env variable=value\n"
4090 " set OpenBIOS nvram variables\n"
blueswir166508602007-05-01 14:16:52 +00004091#endif
thsf3dcfad2007-08-24 01:26:02 +00004092 "-clock force the use of the given methods for timer alarm.\n"
aurel323adda042008-03-09 23:43:49 +00004093 " To see what timers are available use -clock ?\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004094 "-localtime set the real time clock to local time [default=utc]\n"
bellardbce61842008-02-01 22:18:51 +00004095 "-startdate select initial date of the clock\n"
pbrook2e70f6e2008-06-29 01:03:05 +00004096 "-icount [N|auto]\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004097 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4098 "-echr chr set terminal escape character instead of ctrl-a\n"
4099 "-virtioconsole c\n"
4100 " set virtio console\n"
4101 "-show-cursor show cursor\n"
4102#if defined(TARGET_ARM) || defined(TARGET_M68K)
4103 "-semihosting semihosting mode\n"
4104#endif
4105#if defined(TARGET_ARM)
4106 "-old-param old param mode\n"
4107#endif
4108 "-tb-size n set TB size\n"
4109 "-incoming p prepare for incoming migration, listen on port p\n"
aliguori08585322009-02-27 22:09:45 +00004110#ifndef _WIN32
4111 "-chroot dir Chroot to dir just before starting the VM.\n"
4112 "-runas user Change to user id user just before starting the VM.\n"
4113#endif
bellard0824d6f2003-06-24 13:42:40 +00004114 "\n"
bellard82c643f2004-07-14 17:28:13 +00004115 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004116 "ctrl-alt-f toggle full screen\n"
4117 "ctrl-alt-n switch to virtual console 'n'\n"
4118 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004119 "\n"
4120 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4121 ,
bellard0db63472003-10-27 21:37:46 +00004122 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004123 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004124#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004125 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004126 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004127#endif
bellard6e44ba72004-01-18 21:56:49 +00004128 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004129 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004130 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004131}
4132
bellardcd6f1162004-05-13 22:02:20 +00004133#define HAS_ARG 0x0001
4134
4135enum {
blueswir1d2c639d2009-01-24 18:19:25 +00004136 /* Please keep in synch with help, qemu_options[] and
4137 qemu-doc.texi */
4138 /* Standard options: */
bellardcd6f1162004-05-13 22:02:20 +00004139 QEMU_OPTION_h,
bellardcc1daa42005-06-05 14:49:17 +00004140 QEMU_OPTION_M,
j_mayer94fc95c2007-03-05 19:44:02 +00004141 QEMU_OPTION_cpu,
blueswir1d2c639d2009-01-24 18:19:25 +00004142 QEMU_OPTION_smp,
bellardcd6f1162004-05-13 22:02:20 +00004143 QEMU_OPTION_fda,
4144 QEMU_OPTION_fdb,
4145 QEMU_OPTION_hda,
4146 QEMU_OPTION_hdb,
4147 QEMU_OPTION_hdc,
4148 QEMU_OPTION_hdd,
4149 QEMU_OPTION_cdrom,
blueswir1d2c639d2009-01-24 18:19:25 +00004150 QEMU_OPTION_drive,
balrog3e3d5812007-04-30 02:09:25 +00004151 QEMU_OPTION_mtdblock,
pbrooka1bb27b2007-04-06 16:49:48 +00004152 QEMU_OPTION_sd,
j_mayer86f55662007-04-24 06:52:59 +00004153 QEMU_OPTION_pflash,
bellardcd6f1162004-05-13 22:02:20 +00004154 QEMU_OPTION_boot,
4155 QEMU_OPTION_snapshot,
4156 QEMU_OPTION_m,
blueswir1d2c639d2009-01-24 18:19:25 +00004157 QEMU_OPTION_k,
bellard1d14ffa2005-10-30 18:58:22 +00004158 QEMU_OPTION_audio_help,
4159 QEMU_OPTION_soundhw,
blueswir1d2c639d2009-01-24 18:19:25 +00004160 QEMU_OPTION_usb,
4161 QEMU_OPTION_usbdevice,
4162 QEMU_OPTION_name,
4163 QEMU_OPTION_uuid,
bellardcd6f1162004-05-13 22:02:20 +00004164
blueswir1d2c639d2009-01-24 18:19:25 +00004165 /* Display options: */
4166 QEMU_OPTION_nographic,
4167 QEMU_OPTION_curses,
4168 QEMU_OPTION_no_frame,
4169 QEMU_OPTION_alt_grab,
4170 QEMU_OPTION_no_quit,
4171 QEMU_OPTION_sdl,
4172 QEMU_OPTION_portrait,
4173 QEMU_OPTION_vga,
4174 QEMU_OPTION_full_screen,
4175 QEMU_OPTION_g,
4176 QEMU_OPTION_vnc,
4177
4178 /* Network options: */
bellard7c9d8e02005-11-15 22:16:05 +00004179 QEMU_OPTION_net,
bellardc7f74642004-08-24 21:57:12 +00004180 QEMU_OPTION_tftp,
ths47d5d012007-02-20 00:05:08 +00004181 QEMU_OPTION_bootp,
bellard9d728e82004-09-05 23:09:03 +00004182 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00004183 QEMU_OPTION_redir,
balrogdc72ac12008-11-09 00:04:26 +00004184 QEMU_OPTION_bt,
bellardcd6f1162004-05-13 22:02:20 +00004185
blueswir1d2c639d2009-01-24 18:19:25 +00004186 /* i386 target only: */
4187 QEMU_OPTION_win2k_hack,
4188 QEMU_OPTION_rtc_td_hack,
4189 QEMU_OPTION_no_fd_bootchk,
4190 QEMU_OPTION_no_acpi,
4191 QEMU_OPTION_no_hpet,
aliguori8a92ea22009-02-27 20:12:36 +00004192 QEMU_OPTION_acpitable,
blueswir1d2c639d2009-01-24 18:19:25 +00004193
4194 /* Linux boot specific: */
bellardcd6f1162004-05-13 22:02:20 +00004195 QEMU_OPTION_kernel,
4196 QEMU_OPTION_append,
4197 QEMU_OPTION_initrd,
4198
blueswir1d2c639d2009-01-24 18:19:25 +00004199 /* Debug/Expert options: */
4200 QEMU_OPTION_serial,
4201 QEMU_OPTION_parallel,
4202 QEMU_OPTION_monitor,
4203 QEMU_OPTION_pidfile,
bellardcd6f1162004-05-13 22:02:20 +00004204 QEMU_OPTION_S,
4205 QEMU_OPTION_s,
4206 QEMU_OPTION_p,
4207 QEMU_OPTION_d,
4208 QEMU_OPTION_hdachs,
4209 QEMU_OPTION_L,
j_mayer1192dad2007-10-05 13:08:35 +00004210 QEMU_OPTION_bios,
bellard89bfc102006-02-08 22:46:31 +00004211 QEMU_OPTION_kernel_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00004212 QEMU_OPTION_no_kqemu,
aliguori7ba1e612008-11-05 16:04:33 +00004213 QEMU_OPTION_enable_kvm,
bellardd1beab82006-10-02 19:44:22 +00004214 QEMU_OPTION_no_reboot,
aurel32b2f76162008-04-11 21:35:52 +00004215 QEMU_OPTION_no_shutdown,
blueswir1d2c639d2009-01-24 18:19:25 +00004216 QEMU_OPTION_loadvm,
ths71e3ceb2006-12-22 02:11:31 +00004217 QEMU_OPTION_daemonize,
ths9ae02552007-01-05 17:39:04 +00004218 QEMU_OPTION_option_rom,
blueswir166508602007-05-01 14:16:52 +00004219 QEMU_OPTION_prom_env,
thsf3dcfad2007-08-24 01:26:02 +00004220 QEMU_OPTION_clock,
blueswir1d2c639d2009-01-24 18:19:25 +00004221 QEMU_OPTION_localtime,
bellard7e0af5d02007-11-07 16:24:33 +00004222 QEMU_OPTION_startdate,
pbrook2e70f6e2008-06-29 01:03:05 +00004223 QEMU_OPTION_icount,
blueswir1d2c639d2009-01-24 18:19:25 +00004224 QEMU_OPTION_echr,
4225 QEMU_OPTION_virtiocon,
4226 QEMU_OPTION_show_cursor,
4227 QEMU_OPTION_semihosting,
4228 QEMU_OPTION_old_param,
4229 QEMU_OPTION_tb_size,
aliguori5bb79102008-10-13 03:12:02 +00004230 QEMU_OPTION_incoming,
aliguori08585322009-02-27 22:09:45 +00004231 QEMU_OPTION_chroot,
4232 QEMU_OPTION_runas,
bellardcd6f1162004-05-13 22:02:20 +00004233};
4234
4235typedef struct QEMUOption {
4236 const char *name;
4237 int flags;
4238 int index;
4239} QEMUOption;
4240
blueswir1dbed7e42008-10-01 19:38:09 +00004241static const QEMUOption qemu_options[] = {
blueswir1d2c639d2009-01-24 18:19:25 +00004242 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4243 qemu-doc.texi */
4244 /* Standard options: */
bellardcd6f1162004-05-13 22:02:20 +00004245 { "h", 0, QEMU_OPTION_h },
pbrook64423fb2007-01-27 17:11:41 +00004246 { "help", 0, QEMU_OPTION_h },
bellardcc1daa42005-06-05 14:49:17 +00004247 { "M", HAS_ARG, QEMU_OPTION_M },
j_mayer94fc95c2007-03-05 19:44:02 +00004248 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
blueswir1d2c639d2009-01-24 18:19:25 +00004249 { "smp", HAS_ARG, QEMU_OPTION_smp },
bellardcd6f1162004-05-13 22:02:20 +00004250 { "fda", HAS_ARG, QEMU_OPTION_fda },
4251 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
4252 { "hda", HAS_ARG, QEMU_OPTION_hda },
4253 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
4254 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
4255 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
4256 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
blueswir1d2c639d2009-01-24 18:19:25 +00004257 { "drive", HAS_ARG, QEMU_OPTION_drive },
balrog3e3d5812007-04-30 02:09:25 +00004258 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
pbrooka1bb27b2007-04-06 16:49:48 +00004259 { "sd", HAS_ARG, QEMU_OPTION_sd },
j_mayer86f55662007-04-24 06:52:59 +00004260 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
bellardcd6f1162004-05-13 22:02:20 +00004261 { "boot", HAS_ARG, QEMU_OPTION_boot },
4262 { "snapshot", 0, QEMU_OPTION_snapshot },
4263 { "m", HAS_ARG, QEMU_OPTION_m },
blueswir1d2c639d2009-01-24 18:19:25 +00004264#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00004265 { "k", HAS_ARG, QEMU_OPTION_k },
blueswir1d2c639d2009-01-24 18:19:25 +00004266#endif
bellard1d14ffa2005-10-30 18:58:22 +00004267#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004268 { "audio-help", 0, QEMU_OPTION_audio_help },
4269 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
4270#endif
blueswir1d2c639d2009-01-24 18:19:25 +00004271 { "usb", 0, QEMU_OPTION_usb },
4272 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
4273 { "name", HAS_ARG, QEMU_OPTION_name },
4274 { "uuid", HAS_ARG, QEMU_OPTION_uuid },
bellardcd6f1162004-05-13 22:02:20 +00004275
blueswir1d2c639d2009-01-24 18:19:25 +00004276 /* Display options: */
4277 { "nographic", 0, QEMU_OPTION_nographic },
4278#ifdef CONFIG_CURSES
4279 { "curses", 0, QEMU_OPTION_curses },
4280#endif
4281#ifdef CONFIG_SDL
4282 { "no-frame", 0, QEMU_OPTION_no_frame },
4283 { "alt-grab", 0, QEMU_OPTION_alt_grab },
4284 { "no-quit", 0, QEMU_OPTION_no_quit },
4285 { "sdl", 0, QEMU_OPTION_sdl },
4286#endif
4287 { "portrait", 0, QEMU_OPTION_portrait },
4288 { "vga", HAS_ARG, QEMU_OPTION_vga },
4289 { "full-screen", 0, QEMU_OPTION_full_screen },
4290#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4291 { "g", 1, QEMU_OPTION_g },
4292#endif
4293 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
4294
4295 /* Network options: */
bellard7c9d8e02005-11-15 22:16:05 +00004296 { "net", HAS_ARG, QEMU_OPTION_net},
bellard158156d2004-05-17 21:13:42 +00004297#ifdef CONFIG_SLIRP
bellardc7f74642004-08-24 21:57:12 +00004298 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
ths47d5d012007-02-20 00:05:08 +00004299 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
bellardc94c8d62004-09-13 21:37:34 +00004300#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004301 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00004302#endif
bellard9bf05442004-08-25 22:12:49 +00004303 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00004304#endif
balrogdc72ac12008-11-09 00:04:26 +00004305 { "bt", HAS_ARG, QEMU_OPTION_bt },
blueswir1d2c639d2009-01-24 18:19:25 +00004306#ifdef TARGET_I386
4307 /* i386 target only: */
4308 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
4309 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack },
4310 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
4311 { "no-acpi", 0, QEMU_OPTION_no_acpi },
4312 { "no-hpet", 0, QEMU_OPTION_no_hpet },
aliguori8a92ea22009-02-27 20:12:36 +00004313 { "acpitable", HAS_ARG, QEMU_OPTION_acpitable },
blueswir1d2c639d2009-01-24 18:19:25 +00004314#endif
bellardcd6f1162004-05-13 22:02:20 +00004315
blueswir1d2c639d2009-01-24 18:19:25 +00004316 /* Linux boot specific: */
bellardcd6f1162004-05-13 22:02:20 +00004317 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
4318 { "append", HAS_ARG, QEMU_OPTION_append },
4319 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
4320
blueswir1d2c639d2009-01-24 18:19:25 +00004321 /* Debug/Expert options: */
4322 { "serial", HAS_ARG, QEMU_OPTION_serial },
4323 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
4324 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
4325 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellardcd6f1162004-05-13 22:02:20 +00004326 { "S", 0, QEMU_OPTION_S },
4327 { "s", 0, QEMU_OPTION_s },
4328 { "p", HAS_ARG, QEMU_OPTION_p },
4329 { "d", HAS_ARG, QEMU_OPTION_d },
4330 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
4331 { "L", HAS_ARG, QEMU_OPTION_L },
j_mayer1192dad2007-10-05 13:08:35 +00004332 { "bios", HAS_ARG, QEMU_OPTION_bios },
bellardd993e022005-02-10 22:00:06 +00004333#ifdef USE_KQEMU
bellard89bfc102006-02-08 22:46:31 +00004334 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
blueswir1d2c639d2009-01-24 18:19:25 +00004335 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
bellardd993e022005-02-10 22:00:06 +00004336#endif
aliguori7ba1e612008-11-05 16:04:33 +00004337#ifdef CONFIG_KVM
4338 { "enable-kvm", 0, QEMU_OPTION_enable_kvm },
4339#endif
bellardd1beab82006-10-02 19:44:22 +00004340 { "no-reboot", 0, QEMU_OPTION_no_reboot },
aurel32b2f76162008-04-11 21:35:52 +00004341 { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
blueswir1d2c639d2009-01-24 18:19:25 +00004342 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
ths71e3ceb2006-12-22 02:11:31 +00004343 { "daemonize", 0, QEMU_OPTION_daemonize },
ths9ae02552007-01-05 17:39:04 +00004344 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
blueswir195efd112008-12-24 20:26:14 +00004345#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00004346 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
4347#endif
blueswir1d2c639d2009-01-24 18:19:25 +00004348 { "clock", HAS_ARG, QEMU_OPTION_clock },
4349 { "localtime", 0, QEMU_OPTION_localtime },
4350 { "startdate", HAS_ARG, QEMU_OPTION_startdate },
4351 { "icount", HAS_ARG, QEMU_OPTION_icount },
4352 { "echr", HAS_ARG, QEMU_OPTION_echr },
4353 { "virtioconsole", HAS_ARG, QEMU_OPTION_virtiocon },
4354 { "show-cursor", 0, QEMU_OPTION_show_cursor },
4355#if defined(TARGET_ARM) || defined(TARGET_M68K)
4356 { "semihosting", 0, QEMU_OPTION_semihosting },
4357#endif
balrog2b8f2d42007-07-27 22:08:46 +00004358#if defined(TARGET_ARM)
4359 { "old-param", 0, QEMU_OPTION_old_param },
4360#endif
bellard26a5f132008-05-28 12:30:31 +00004361 { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
aliguori5bb79102008-10-13 03:12:02 +00004362 { "incoming", HAS_ARG, QEMU_OPTION_incoming },
aliguori08585322009-02-27 22:09:45 +00004363 { "chroot", HAS_ARG, QEMU_OPTION_chroot },
4364 { "runas", HAS_ARG, QEMU_OPTION_runas },
bellardcd6f1162004-05-13 22:02:20 +00004365 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004366};
4367
bellard1d14ffa2005-10-30 18:58:22 +00004368#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004369struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004370#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004371#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004372 {
4373 "pcspk",
4374 "PC speaker",
4375 0,
4376 1,
4377 { .init_isa = pcspk_audio_init }
4378 },
4379#endif
malc4c9b53e2009-01-09 10:46:34 +00004380
4381#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004382 {
4383 "sb16",
4384 "Creative Sound Blaster 16",
4385 0,
4386 1,
4387 { .init_isa = SB16_init }
4388 },
malc4c9b53e2009-01-09 10:46:34 +00004389#endif
bellard6a36d842005-12-18 20:34:32 +00004390
malccc53d262008-06-13 10:48:22 +00004391#ifdef CONFIG_CS4231A
4392 {
4393 "cs4231a",
4394 "CS4231A",
4395 0,
4396 1,
4397 { .init_isa = cs4231a_init }
4398 },
4399#endif
4400
bellard6a36d842005-12-18 20:34:32 +00004401#ifdef CONFIG_ADLIB
4402 {
4403 "adlib",
4404#ifdef HAS_YMF262
4405 "Yamaha YMF262 (OPL3)",
4406#else
4407 "Yamaha YM3812 (OPL2)",
4408#endif
4409 0,
4410 1,
4411 { .init_isa = Adlib_init }
4412 },
4413#endif
4414
4415#ifdef CONFIG_GUS
4416 {
4417 "gus",
4418 "Gravis Ultrasound GF1",
4419 0,
4420 1,
4421 { .init_isa = GUS_init }
4422 },
4423#endif
4424
malc4c9b53e2009-01-09 10:46:34 +00004425#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004426 {
4427 "ac97",
4428 "Intel 82801AA AC97 Audio",
4429 0,
4430 0,
4431 { .init_pci = ac97_init }
4432 },
malc4c9b53e2009-01-09 10:46:34 +00004433#endif
balroge5c9a132008-01-14 04:27:55 +00004434
malc4c9b53e2009-01-09 10:46:34 +00004435#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004436 {
4437 "es1370",
4438 "ENSONIQ AudioPCI ES1370",
4439 0,
4440 0,
4441 { .init_pci = es1370_init }
4442 },
balrogb00052e2007-04-30 02:22:06 +00004443#endif
bellard6a36d842005-12-18 20:34:32 +00004444
malc4c9b53e2009-01-09 10:46:34 +00004445#endif /* HAS_AUDIO_CHOICE */
4446
bellard6a36d842005-12-18 20:34:32 +00004447 { NULL, NULL, 0, 0, { NULL } }
4448};
4449
bellard1d14ffa2005-10-30 18:58:22 +00004450static void select_soundhw (const char *optarg)
4451{
bellard6a36d842005-12-18 20:34:32 +00004452 struct soundhw *c;
4453
bellard1d14ffa2005-10-30 18:58:22 +00004454 if (*optarg == '?') {
4455 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004456
bellard1d14ffa2005-10-30 18:58:22 +00004457 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004458 for (c = soundhw; c->name; ++c) {
4459 printf ("%-11s %s\n", c->name, c->descr);
4460 }
4461 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004462 exit (*optarg != '?');
4463 }
4464 else {
bellard6a36d842005-12-18 20:34:32 +00004465 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004466 const char *p;
4467 char *e;
4468 int bad_card = 0;
4469
bellard6a36d842005-12-18 20:34:32 +00004470 if (!strcmp (optarg, "all")) {
4471 for (c = soundhw; c->name; ++c) {
4472 c->enabled = 1;
4473 }
4474 return;
4475 }
bellard1d14ffa2005-10-30 18:58:22 +00004476
bellard6a36d842005-12-18 20:34:32 +00004477 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004478 while (*p) {
4479 e = strchr (p, ',');
4480 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004481
4482 for (c = soundhw; c->name; ++c) {
4483 if (!strncmp (c->name, p, l)) {
4484 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004485 break;
4486 }
4487 }
bellard6a36d842005-12-18 20:34:32 +00004488
4489 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004490 if (l > 80) {
4491 fprintf (stderr,
4492 "Unknown sound card name (too big to show)\n");
4493 }
4494 else {
4495 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4496 (int) l, p);
4497 }
4498 bad_card = 1;
4499 }
4500 p += l + (e != NULL);
4501 }
4502
4503 if (bad_card)
4504 goto show_valid_cards;
4505 }
4506}
4507#endif
4508
malc3893c122008-09-28 00:42:05 +00004509static void select_vgahw (const char *p)
4510{
4511 const char *opts;
4512
4513 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004514 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004515 cirrus_vga_enabled = 0;
4516 vmsvga_enabled = 0;
4517 } else if (strstart(p, "cirrus", &opts)) {
4518 cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004519 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004520 vmsvga_enabled = 0;
4521 } else if (strstart(p, "vmware", &opts)) {
4522 cirrus_vga_enabled = 0;
aliguoric2b3b412009-01-15 20:37:28 +00004523 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004524 vmsvga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004525 } else if (strstart(p, "none", &opts)) {
4526 cirrus_vga_enabled = 0;
4527 std_vga_enabled = 0;
4528 vmsvga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004529 } else {
4530 invalid_vga:
4531 fprintf(stderr, "Unknown vga type: %s\n", p);
4532 exit(1);
4533 }
malccb5a7aa2008-09-28 00:42:12 +00004534 while (*opts) {
4535 const char *nextopt;
4536
4537 if (strstart(opts, ",retrace=", &nextopt)) {
4538 opts = nextopt;
4539 if (strstart(opts, "dumb", &nextopt))
4540 vga_retrace_method = VGA_RETRACE_DUMB;
4541 else if (strstart(opts, "precise", &nextopt))
4542 vga_retrace_method = VGA_RETRACE_PRECISE;
4543 else goto invalid_vga;
4544 } else goto invalid_vga;
4545 opts = nextopt;
4546 }
malc3893c122008-09-28 00:42:05 +00004547}
4548
bellard3587d7e2006-06-26 20:03:44 +00004549#ifdef _WIN32
4550static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4551{
4552 exit(STATUS_CONTROL_C_EXIT);
4553 return TRUE;
4554}
4555#endif
4556
blueswir18fcb1b92008-09-18 18:29:08 +00004557static int qemu_uuid_parse(const char *str, uint8_t *uuid)
4558{
4559 int ret;
4560
4561 if(strlen(str) != 36)
4562 return -1;
4563
4564 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4565 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4566 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4567
4568 if(ret != 16)
4569 return -1;
4570
4571 return 0;
4572}
4573
bellard7c9d8e02005-11-15 22:16:05 +00004574#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004575
aliguori5b08fc12008-08-21 20:08:03 +00004576#ifndef _WIN32
4577
4578static void termsig_handler(int signal)
4579{
4580 qemu_system_shutdown_request();
4581}
4582
blueswir16f9e3802008-09-09 18:56:59 +00004583static void termsig_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004584{
4585 struct sigaction act;
4586
4587 memset(&act, 0, sizeof(act));
4588 act.sa_handler = termsig_handler;
4589 sigaction(SIGINT, &act, NULL);
4590 sigaction(SIGHUP, &act, NULL);
4591 sigaction(SIGTERM, &act, NULL);
4592}
4593
4594#endif
4595
malc902b3d52008-12-10 19:18:40 +00004596int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004597{
bellard67b915a2004-03-31 23:37:16 +00004598#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00004599 int use_gdbstub;
4600 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00004601#endif
j_mayer28c5af52007-11-11 01:50:45 +00004602 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004603 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004604 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004605 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004606 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00004607 const char *boot_devices = "";
aliguori3023f332009-01-16 19:04:14 +00004608 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004609 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004610 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004611 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004612 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004613 const char *bt_opts[MAX_BT_CMDLINE];
4614 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004615 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004616 int optind;
4617 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004618 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004619 const char *monitor_device;
4620 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004621 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004622 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004623 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004624 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4625 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004626 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004627 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004628 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004629 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004630 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00004631 int fds[2];
bellard26a5f132008-05-28 12:30:31 +00004632 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004633 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004634 const char *incoming = NULL;
aliguori54042bc2009-02-27 22:16:47 +00004635 int fd = 0;
4636 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004637 const char *chroot_dir = NULL;
4638 const char *run_as = NULL;
bellard0bd48852005-11-11 00:00:47 +00004639
malc902b3d52008-12-10 19:18:40 +00004640 qemu_cache_utils_init(envp);
4641
bellard0bd48852005-11-11 00:00:47 +00004642 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004643#ifndef _WIN32
4644 {
4645 struct sigaction act;
4646 sigfillset(&act.sa_mask);
4647 act.sa_flags = 0;
4648 act.sa_handler = SIG_IGN;
4649 sigaction(SIGPIPE, &act, NULL);
4650 }
bellard3587d7e2006-06-26 20:03:44 +00004651#else
4652 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004653 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4654 QEMU to run on a single CPU */
4655 {
4656 HANDLE h;
4657 DWORD mask, smask;
4658 int i;
4659 h = GetCurrentProcess();
4660 if (GetProcessAffinityMask(h, &mask, &smask)) {
4661 for(i = 0; i < 32; i++) {
4662 if (mask & (1 << i))
4663 break;
4664 }
4665 if (i != 32) {
4666 mask = 1 << i;
4667 SetProcessAffinityMask(h, mask);
4668 }
4669 }
4670 }
bellard67b915a2004-03-31 23:37:16 +00004671#endif
bellardbe995c22006-06-25 16:25:21 +00004672
bellardcc1daa42005-06-05 14:49:17 +00004673 register_machines();
4674 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004675 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004676 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004677 ram_size = 0;
bellard313aa562003-08-10 21:52:11 +00004678 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00004679#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00004680 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00004681 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00004682#endif
bellard33e39632003-07-06 17:15:21 +00004683 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004684 nographic = 0;
balrog4d3b6f62008-02-10 16:33:14 +00004685 curses = 0;
bellarda20dd502003-09-30 21:07:02 +00004686 kernel_filename = NULL;
4687 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004688 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004689 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00004690 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00004691
aurel32c75a8232008-05-04 00:50:34 +00004692 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004693 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004694 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004695 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004696
aliguori8290edd2009-02-27 20:14:29 +00004697 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004698 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004699 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004700 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004701
aliguori1b8fc812009-02-27 20:01:39 +00004702 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004703 virtio_consoles[i] = NULL;
4704 virtio_console_index = 0;
4705
bellarda594cfb2005-11-06 16:13:29 +00004706 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004707
bellard7c9d8e02005-11-15 22:16:05 +00004708 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004709 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004710 nb_drives = 0;
4711 nb_drives_opt = 0;
4712 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004713
4714 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004715
bellard26a5f132008-05-28 12:30:31 +00004716 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004717 autostart= 1;
4718
bellardcd6f1162004-05-13 22:02:20 +00004719 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004720 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004721 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004722 break;
bellardcd6f1162004-05-13 22:02:20 +00004723 r = argv[optind];
4724 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004725 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004726 } else {
4727 const QEMUOption *popt;
4728
4729 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004730 /* Treat --foo the same as -foo. */
4731 if (r[1] == '-')
4732 r++;
bellardcd6f1162004-05-13 22:02:20 +00004733 popt = qemu_options;
4734 for(;;) {
4735 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004736 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004737 argv[0], r);
4738 exit(1);
4739 }
4740 if (!strcmp(popt->name, r + 1))
4741 break;
4742 popt++;
4743 }
4744 if (popt->flags & HAS_ARG) {
4745 if (optind >= argc) {
4746 fprintf(stderr, "%s: option '%s' requires an argument\n",
4747 argv[0], r);
4748 exit(1);
4749 }
4750 optarg = argv[optind++];
4751 } else {
4752 optarg = NULL;
4753 }
4754
4755 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004756 case QEMU_OPTION_M:
4757 machine = find_machine(optarg);
4758 if (!machine) {
4759 QEMUMachine *m;
4760 printf("Supported machines are:\n");
4761 for(m = first_machine; m != NULL; m = m->next) {
4762 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004763 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00004764 m == first_machine ? " (default)" : "");
4765 }
ths15f82202007-06-29 23:26:08 +00004766 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004767 }
4768 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004769 case QEMU_OPTION_cpu:
4770 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004771 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004772/* XXX: implement xxx_cpu_list for targets that still miss it */
4773#if defined(cpu_list)
4774 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004775#endif
ths15f82202007-06-29 23:26:08 +00004776 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004777 } else {
4778 cpu_model = optarg;
4779 }
4780 break;
bellardcd6f1162004-05-13 22:02:20 +00004781 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004782 initrd_filename = optarg;
4783 break;
bellardcd6f1162004-05-13 22:02:20 +00004784 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004785 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00004786 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004787 else
balrog609497a2008-01-14 02:56:53 +00004788 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004789 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004790 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004791 translation == BIOS_ATA_TRANSLATION_LBA ?
4792 ",trans=lba" :
4793 translation == BIOS_ATA_TRANSLATION_NONE ?
4794 ",trans=none" : "");
4795 break;
bellardcd6f1162004-05-13 22:02:20 +00004796 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004797 case QEMU_OPTION_hdc:
4798 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004799 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004800 break;
thse4bcb142007-12-02 04:51:10 +00004801 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004802 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004803 break;
balrog3e3d5812007-04-30 02:09:25 +00004804 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004805 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004806 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004807 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004808 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004809 break;
j_mayer86f55662007-04-24 06:52:59 +00004810 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004811 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004812 break;
bellardcd6f1162004-05-13 22:02:20 +00004813 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004814 snapshot = 1;
4815 break;
bellardcd6f1162004-05-13 22:02:20 +00004816 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004817 {
bellard330d0412003-07-26 18:11:40 +00004818 const char *p;
4819 p = optarg;
4820 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004821 if (cyls < 1 || cyls > 16383)
4822 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004823 if (*p != ',')
4824 goto chs_fail;
4825 p++;
4826 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004827 if (heads < 1 || heads > 16)
4828 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004829 if (*p != ',')
4830 goto chs_fail;
4831 p++;
4832 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004833 if (secs < 1 || secs > 63)
4834 goto chs_fail;
4835 if (*p == ',') {
4836 p++;
4837 if (!strcmp(p, "none"))
4838 translation = BIOS_ATA_TRANSLATION_NONE;
4839 else if (!strcmp(p, "lba"))
4840 translation = BIOS_ATA_TRANSLATION_LBA;
4841 else if (!strcmp(p, "auto"))
4842 translation = BIOS_ATA_TRANSLATION_AUTO;
4843 else
4844 goto chs_fail;
4845 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004846 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004847 fprintf(stderr, "qemu: invalid physical CHS format\n");
4848 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004849 }
thse4bcb142007-12-02 04:51:10 +00004850 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00004851 snprintf(drives_opt[hda_index].opt,
4852 sizeof(drives_opt[hda_index].opt),
4853 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4854 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004855 translation == BIOS_ATA_TRANSLATION_LBA ?
4856 ",trans=lba" :
4857 translation == BIOS_ATA_TRANSLATION_NONE ?
4858 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00004859 }
4860 break;
bellardcd6f1162004-05-13 22:02:20 +00004861 case QEMU_OPTION_nographic:
bellarda20dd502003-09-30 21:07:02 +00004862 nographic = 1;
4863 break;
balrog4d3b6f62008-02-10 16:33:14 +00004864#ifdef CONFIG_CURSES
4865 case QEMU_OPTION_curses:
4866 curses = 1;
4867 break;
4868#endif
balroga171fe32007-04-30 01:48:07 +00004869 case QEMU_OPTION_portrait:
4870 graphic_rotate = 1;
4871 break;
bellardcd6f1162004-05-13 22:02:20 +00004872 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004873 kernel_filename = optarg;
4874 break;
bellardcd6f1162004-05-13 22:02:20 +00004875 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004876 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004877 break;
bellardcd6f1162004-05-13 22:02:20 +00004878 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004879 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004880 break;
bellardcd6f1162004-05-13 22:02:20 +00004881 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004882 boot_devices = optarg;
4883 /* We just do some generic consistency checks */
4884 {
4885 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00004886 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00004887
4888 boot_devices_bitmap = 0;
4889 for (p = boot_devices; *p != '\0'; p++) {
4890 /* Allowed boot devices are:
4891 * a b : floppy disk drives
4892 * c ... f : IDE disk drives
4893 * g ... m : machine implementation dependant drives
4894 * n ... p : network devices
4895 * It's up to each machine implementation to check
4896 * if the given boot devices match the actual hardware
4897 * implementation and firmware features.
4898 */
4899 if (*p < 'a' || *p > 'q') {
4900 fprintf(stderr, "Invalid boot device '%c'\n", *p);
4901 exit(1);
4902 }
4903 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4904 fprintf(stderr,
4905 "Boot device '%c' was given twice\n",*p);
4906 exit(1);
4907 }
4908 boot_devices_bitmap |= 1 << (*p - 'a');
4909 }
bellard36b486b2003-11-11 13:36:08 +00004910 }
4911 break;
bellardcd6f1162004-05-13 22:02:20 +00004912 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004913 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004914 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004915 break;
bellard52ca8d62006-06-14 16:03:05 +00004916#ifdef TARGET_I386
4917 case QEMU_OPTION_no_fd_bootchk:
4918 fd_bootchk = 0;
4919 break;
4920#endif
bellard7c9d8e02005-11-15 22:16:05 +00004921 case QEMU_OPTION_net:
4922 if (nb_net_clients >= MAX_NET_CLIENTS) {
4923 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00004924 exit(1);
4925 }
pbrookfd5f3932008-03-26 20:55:43 +00004926 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00004927 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00004928 break;
bellardc7f74642004-08-24 21:57:12 +00004929#ifdef CONFIG_SLIRP
4930 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00004931 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004932 break;
ths47d5d012007-02-20 00:05:08 +00004933 case QEMU_OPTION_bootp:
4934 bootp_filename = optarg;
4935 break;
bellardc94c8d62004-09-13 21:37:34 +00004936#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004937 case QEMU_OPTION_smb:
4938 net_slirp_smb(optarg);
4939 break;
bellardc94c8d62004-09-13 21:37:34 +00004940#endif
bellard9bf05442004-08-25 22:12:49 +00004941 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00004942 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00004943 break;
bellardc7f74642004-08-24 21:57:12 +00004944#endif
balrogdc72ac12008-11-09 00:04:26 +00004945 case QEMU_OPTION_bt:
4946 if (nb_bt_opts >= MAX_BT_CMDLINE) {
4947 fprintf(stderr, "qemu: too many bluetooth options\n");
4948 exit(1);
4949 }
4950 bt_opts[nb_bt_opts++] = optarg;
4951 break;
bellard1d14ffa2005-10-30 18:58:22 +00004952#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004953 case QEMU_OPTION_audio_help:
4954 AUD_help ();
4955 exit (0);
4956 break;
4957 case QEMU_OPTION_soundhw:
4958 select_soundhw (optarg);
4959 break;
4960#endif
bellardcd6f1162004-05-13 22:02:20 +00004961 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00004962 help(0);
bellardcd6f1162004-05-13 22:02:20 +00004963 break;
aurel3200f82b82008-04-27 21:12:55 +00004964 case QEMU_OPTION_m: {
4965 uint64_t value;
4966 char *ptr;
4967
4968 value = strtoul(optarg, &ptr, 10);
4969 switch (*ptr) {
4970 case 0: case 'M': case 'm':
4971 value <<= 20;
4972 break;
4973 case 'G': case 'g':
4974 value <<= 30;
4975 break;
4976 default:
4977 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00004978 exit(1);
4979 }
aurel3200f82b82008-04-27 21:12:55 +00004980
4981 /* On 32-bit hosts, QEMU is limited by virtual address space */
4982 if (value > (2047 << 20)
4983#ifndef USE_KQEMU
4984 && HOST_LONG_BITS == 32
4985#endif
4986 ) {
4987 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4988 exit(1);
4989 }
4990 if (value != (uint64_t)(ram_addr_t)value) {
4991 fprintf(stderr, "qemu: ram size too large\n");
4992 exit(1);
4993 }
4994 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00004995 break;
aurel3200f82b82008-04-27 21:12:55 +00004996 }
bellardcd6f1162004-05-13 22:02:20 +00004997 case QEMU_OPTION_d:
4998 {
4999 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005000 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005001
bellardcd6f1162004-05-13 22:02:20 +00005002 mask = cpu_str_to_log_mask(optarg);
5003 if (!mask) {
5004 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005005 for(item = cpu_log_items; item->mask != 0; item++) {
5006 printf("%-10s %s\n", item->name, item->help);
5007 }
5008 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005009 }
5010 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005011 }
bellardcd6f1162004-05-13 22:02:20 +00005012 break;
bellard67b915a2004-03-31 23:37:16 +00005013#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00005014 case QEMU_OPTION_s:
5015 use_gdbstub = 1;
5016 break;
5017 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00005018 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005019 break;
bellard67b915a2004-03-31 23:37:16 +00005020#endif
bellardcd6f1162004-05-13 22:02:20 +00005021 case QEMU_OPTION_L:
5022 bios_dir = optarg;
5023 break;
j_mayer1192dad2007-10-05 13:08:35 +00005024 case QEMU_OPTION_bios:
5025 bios_name = optarg;
5026 break;
bellardcd6f1162004-05-13 22:02:20 +00005027 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005028 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005029 break;
bellard3d11d0e2004-12-12 16:56:30 +00005030 case QEMU_OPTION_k:
5031 keyboard_layout = optarg;
5032 break;
bellardee22c2f2004-06-03 12:49:50 +00005033 case QEMU_OPTION_localtime:
5034 rtc_utc = 0;
5035 break;
malc3893c122008-09-28 00:42:05 +00005036 case QEMU_OPTION_vga:
5037 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005038 break;
bellarde9b137c2004-06-21 16:46:10 +00005039 case QEMU_OPTION_g:
5040 {
5041 const char *p;
5042 int w, h, depth;
5043 p = optarg;
5044 w = strtol(p, (char **)&p, 10);
5045 if (w <= 0) {
5046 graphic_error:
5047 fprintf(stderr, "qemu: invalid resolution or depth\n");
5048 exit(1);
5049 }
5050 if (*p != 'x')
5051 goto graphic_error;
5052 p++;
5053 h = strtol(p, (char **)&p, 10);
5054 if (h <= 0)
5055 goto graphic_error;
5056 if (*p == 'x') {
5057 p++;
5058 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005059 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005060 depth != 24 && depth != 32)
5061 goto graphic_error;
5062 } else if (*p == '\0') {
5063 depth = graphic_depth;
5064 } else {
5065 goto graphic_error;
5066 }
ths3b46e622007-09-17 08:09:54 +00005067
bellarde9b137c2004-06-21 16:46:10 +00005068 graphic_width = w;
5069 graphic_height = h;
5070 graphic_depth = depth;
5071 }
5072 break;
ths20d8a3e2007-02-18 17:04:49 +00005073 case QEMU_OPTION_echr:
5074 {
5075 char *r;
5076 term_escape_char = strtol(optarg, &r, 0);
5077 if (r == optarg)
5078 printf("Bad argument to echr\n");
5079 break;
5080 }
bellard82c643f2004-07-14 17:28:13 +00005081 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00005082 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00005083 break;
5084 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005085 if (serial_device_index >= MAX_SERIAL_PORTS) {
5086 fprintf(stderr, "qemu: too many serial ports\n");
5087 exit(1);
5088 }
pbrookfd5f3932008-03-26 20:55:43 +00005089 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005090 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005091 break;
aliguori51ecf132009-01-15 20:06:40 +00005092 case QEMU_OPTION_virtiocon:
5093 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5094 fprintf(stderr, "qemu: too many virtio consoles\n");
5095 exit(1);
5096 }
5097 virtio_consoles[virtio_console_index] = optarg;
5098 virtio_console_index++;
5099 break;
bellard6508fe52005-01-15 12:02:56 +00005100 case QEMU_OPTION_parallel:
5101 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5102 fprintf(stderr, "qemu: too many parallel ports\n");
5103 exit(1);
5104 }
pbrookfd5f3932008-03-26 20:55:43 +00005105 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005106 parallel_device_index++;
5107 break;
bellardd63d3072004-10-03 13:29:03 +00005108 case QEMU_OPTION_loadvm:
5109 loadvm = optarg;
5110 break;
5111 case QEMU_OPTION_full_screen:
5112 full_screen = 1;
5113 break;
ths667acca2006-12-11 02:08:05 +00005114#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005115 case QEMU_OPTION_no_frame:
5116 no_frame = 1;
5117 break;
ths3780e192007-06-21 21:08:02 +00005118 case QEMU_OPTION_alt_grab:
5119 alt_grab = 1;
5120 break;
ths667acca2006-12-11 02:08:05 +00005121 case QEMU_OPTION_no_quit:
5122 no_quit = 1;
5123 break;
aliguori7d957bd2009-01-15 22:14:11 +00005124 case QEMU_OPTION_sdl:
5125 sdl = 1;
5126 break;
ths667acca2006-12-11 02:08:05 +00005127#endif
bellardf7cce892004-12-08 22:21:25 +00005128 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005129 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005130 break;
bellarda09db212005-04-30 16:10:35 +00005131#ifdef TARGET_I386
5132 case QEMU_OPTION_win2k_hack:
5133 win2k_install_hack = 1;
5134 break;
aliguori73822ec2009-01-15 20:11:34 +00005135 case QEMU_OPTION_rtc_td_hack:
5136 rtc_td_hack = 1;
5137 break;
aliguori8a92ea22009-02-27 20:12:36 +00005138 case QEMU_OPTION_acpitable:
5139 if(acpi_table_add(optarg) < 0) {
5140 fprintf(stderr, "Wrong acpi table provided\n");
5141 exit(1);
5142 }
5143 break;
bellarda09db212005-04-30 16:10:35 +00005144#endif
bellardd993e022005-02-10 22:00:06 +00005145#ifdef USE_KQEMU
5146 case QEMU_OPTION_no_kqemu:
5147 kqemu_allowed = 0;
5148 break;
bellard89bfc102006-02-08 22:46:31 +00005149 case QEMU_OPTION_kernel_kqemu:
5150 kqemu_allowed = 2;
5151 break;
bellardd993e022005-02-10 22:00:06 +00005152#endif
aliguori7ba1e612008-11-05 16:04:33 +00005153#ifdef CONFIG_KVM
5154 case QEMU_OPTION_enable_kvm:
5155 kvm_allowed = 1;
5156#ifdef USE_KQEMU
5157 kqemu_allowed = 0;
5158#endif
5159 break;
5160#endif
bellardbb36d472005-11-05 14:22:28 +00005161 case QEMU_OPTION_usb:
5162 usb_enabled = 1;
5163 break;
bellarda594cfb2005-11-06 16:13:29 +00005164 case QEMU_OPTION_usbdevice:
5165 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00005166 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00005167 fprintf(stderr, "Too many USB devices\n");
5168 exit(1);
5169 }
pbrookfd5f3932008-03-26 20:55:43 +00005170 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00005171 usb_devices_index++;
5172 break;
bellard6a00d602005-11-21 23:25:50 +00005173 case QEMU_OPTION_smp:
5174 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00005175 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005176 fprintf(stderr, "Invalid number of CPUs\n");
5177 exit(1);
5178 }
5179 break;
bellard24236862006-04-30 21:28:36 +00005180 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00005181 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005182 break;
bellard6515b202006-05-03 22:02:44 +00005183 case QEMU_OPTION_no_acpi:
5184 acpi_enabled = 0;
5185 break;
aliguori16b29ae2008-12-17 23:28:44 +00005186 case QEMU_OPTION_no_hpet:
5187 no_hpet = 1;
5188 break;
bellardd1beab82006-10-02 19:44:22 +00005189 case QEMU_OPTION_no_reboot:
5190 no_reboot = 1;
5191 break;
aurel32b2f76162008-04-11 21:35:52 +00005192 case QEMU_OPTION_no_shutdown:
5193 no_shutdown = 1;
5194 break;
balrog9467cd42007-05-01 01:34:14 +00005195 case QEMU_OPTION_show_cursor:
5196 cursor_hide = 0;
5197 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005198 case QEMU_OPTION_uuid:
5199 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5200 fprintf(stderr, "Fail to parse UUID string."
5201 " Wrong format.\n");
5202 exit(1);
5203 }
5204 break;
ths71e3ceb2006-12-22 02:11:31 +00005205 case QEMU_OPTION_daemonize:
5206 daemonize = 1;
5207 break;
ths9ae02552007-01-05 17:39:04 +00005208 case QEMU_OPTION_option_rom:
5209 if (nb_option_roms >= MAX_OPTION_ROMS) {
5210 fprintf(stderr, "Too many option ROMs\n");
5211 exit(1);
5212 }
5213 option_rom[nb_option_roms] = optarg;
5214 nb_option_roms++;
5215 break;
pbrook8e716212007-01-20 17:12:09 +00005216 case QEMU_OPTION_semihosting:
5217 semihosting_enabled = 1;
5218 break;
thsc35734b2007-03-19 15:17:08 +00005219 case QEMU_OPTION_name:
5220 qemu_name = optarg;
5221 break;
blueswir195efd112008-12-24 20:26:14 +00005222#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005223 case QEMU_OPTION_prom_env:
5224 if (nb_prom_envs >= MAX_PROM_ENVS) {
5225 fprintf(stderr, "Too many prom variables\n");
5226 exit(1);
5227 }
5228 prom_envs[nb_prom_envs] = optarg;
5229 nb_prom_envs++;
5230 break;
5231#endif
balrog2b8f2d42007-07-27 22:08:46 +00005232#ifdef TARGET_ARM
5233 case QEMU_OPTION_old_param:
5234 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005235 break;
balrog2b8f2d42007-07-27 22:08:46 +00005236#endif
thsf3dcfad2007-08-24 01:26:02 +00005237 case QEMU_OPTION_clock:
5238 configure_alarms(optarg);
5239 break;
bellard7e0af5d02007-11-07 16:24:33 +00005240 case QEMU_OPTION_startdate:
5241 {
5242 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005243 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005244 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005245 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005246 } else {
5247 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5248 &tm.tm_year,
5249 &tm.tm_mon,
5250 &tm.tm_mday,
5251 &tm.tm_hour,
5252 &tm.tm_min,
5253 &tm.tm_sec) == 6) {
5254 /* OK */
5255 } else if (sscanf(optarg, "%d-%d-%d",
5256 &tm.tm_year,
5257 &tm.tm_mon,
5258 &tm.tm_mday) == 3) {
5259 tm.tm_hour = 0;
5260 tm.tm_min = 0;
5261 tm.tm_sec = 0;
5262 } else {
5263 goto date_fail;
5264 }
5265 tm.tm_year -= 1900;
5266 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005267 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005268 if (rtc_start_date == -1) {
5269 date_fail:
5270 fprintf(stderr, "Invalid date format. Valid format are:\n"
5271 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5272 exit(1);
5273 }
balrogf6503052008-02-17 11:42:19 +00005274 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005275 }
5276 }
5277 break;
bellard26a5f132008-05-28 12:30:31 +00005278 case QEMU_OPTION_tb_size:
5279 tb_size = strtol(optarg, NULL, 0);
5280 if (tb_size < 0)
5281 tb_size = 0;
5282 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005283 case QEMU_OPTION_icount:
5284 use_icount = 1;
5285 if (strcmp(optarg, "auto") == 0) {
5286 icount_time_shift = -1;
5287 } else {
5288 icount_time_shift = strtol(optarg, NULL, 0);
5289 }
5290 break;
aliguori5bb79102008-10-13 03:12:02 +00005291 case QEMU_OPTION_incoming:
5292 incoming = optarg;
5293 break;
aliguori08585322009-02-27 22:09:45 +00005294 case QEMU_OPTION_chroot:
5295 chroot_dir = optarg;
5296 break;
5297 case QEMU_OPTION_runas:
5298 run_as = optarg;
5299 break;
bellardcd6f1162004-05-13 22:02:20 +00005300 }
bellard0824d6f2003-06-24 13:42:40 +00005301 }
5302 }
bellard330d0412003-07-26 18:11:40 +00005303
aliguori7ba1e612008-11-05 16:04:33 +00005304#if defined(CONFIG_KVM) && defined(USE_KQEMU)
5305 if (kvm_allowed && kqemu_allowed) {
5306 fprintf(stderr,
5307 "You can not enable both KVM and kqemu at the same time\n");
5308 exit(1);
5309 }
5310#endif
5311
balrog3d878ca2008-10-28 10:59:59 +00005312 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005313 if (smp_cpus > machine->max_cpus) {
5314 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5315 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5316 machine->max_cpus);
5317 exit(1);
5318 }
5319
aliguoribc0129d2008-08-01 15:12:34 +00005320 if (nographic) {
5321 if (serial_device_index == 0)
5322 serial_devices[0] = "stdio";
5323 if (parallel_device_index == 0)
5324 parallel_devices[0] = "null";
5325 if (strncmp(monitor_device, "vc", 2) == 0)
5326 monitor_device = "stdio";
5327 }
5328
ths71e3ceb2006-12-22 02:11:31 +00005329#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005330 if (daemonize) {
5331 pid_t pid;
5332
5333 if (pipe(fds) == -1)
5334 exit(1);
5335
5336 pid = fork();
5337 if (pid > 0) {
5338 uint8_t status;
5339 ssize_t len;
5340
5341 close(fds[1]);
5342
5343 again:
ths93815bc2007-03-19 15:58:31 +00005344 len = read(fds[0], &status, 1);
5345 if (len == -1 && (errno == EINTR))
5346 goto again;
5347
5348 if (len != 1)
5349 exit(1);
5350 else if (status == 1) {
5351 fprintf(stderr, "Could not acquire pidfile\n");
5352 exit(1);
5353 } else
5354 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005355 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005356 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005357
5358 setsid();
5359
5360 pid = fork();
5361 if (pid > 0)
5362 exit(0);
5363 else if (pid < 0)
5364 exit(1);
5365
5366 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005367
5368 signal(SIGTSTP, SIG_IGN);
5369 signal(SIGTTOU, SIG_IGN);
5370 signal(SIGTTIN, SIG_IGN);
5371 }
5372#endif
5373
thsaa26bb22007-03-25 21:33:06 +00005374 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005375 if (daemonize) {
5376 uint8_t status = 1;
5377 write(fds[1], &status, 1);
5378 } else
5379 fprintf(stderr, "Could not acquire pid file\n");
5380 exit(1);
5381 }
5382
bellardff3fbb32006-01-08 10:53:14 +00005383#ifdef USE_KQEMU
5384 if (smp_cpus > 1)
5385 kqemu_allowed = 0;
5386#endif
bellarda20dd502003-09-30 21:07:02 +00005387 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00005388 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00005389
j_mayer28c5af52007-11-11 01:50:45 +00005390 if (!linux_boot && net_boot == 0 &&
blueswir1f88e4b92008-08-12 15:58:35 +00005391 !machine->nodisk_ok && nb_drives_opt == 0)
ths15f82202007-06-29 23:26:08 +00005392 help(1);
bellard0824d6f2003-06-24 13:42:40 +00005393
thsf8d39c02008-07-03 10:01:15 +00005394 if (!linux_boot && *kernel_cmdline != '\0') {
5395 fprintf(stderr, "-append only allowed with -kernel option\n");
5396 exit(1);
5397 }
5398
5399 if (!linux_boot && initrd_filename != NULL) {
5400 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5401 exit(1);
5402 }
5403
ths96d30e42007-01-07 20:42:14 +00005404 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00005405 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00005406 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00005407 }
bellardb118d612003-06-30 23:36:21 +00005408 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005409
pbrook634fce92006-07-15 17:40:09 +00005410 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005411 if (init_timer_alarm() < 0) {
5412 fprintf(stderr, "could not initialize alarm timer\n");
5413 exit(1);
5414 }
pbrook2e70f6e2008-06-29 01:03:05 +00005415 if (use_icount && icount_time_shift < 0) {
5416 use_icount = 2;
5417 /* 125MIPS seems a reasonable initial guess at the guest speed.
5418 It will be corrected fairly quickly anyway. */
5419 icount_time_shift = 3;
5420 init_icount_adjust();
5421 }
pbrook634fce92006-07-15 17:40:09 +00005422
bellardfd1dff42006-02-01 21:29:26 +00005423#ifdef _WIN32
5424 socket_init();
5425#endif
5426
bellard7c9d8e02005-11-15 22:16:05 +00005427 /* init network clients */
5428 if (nb_net_clients == 0) {
5429 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005430 net_clients[nb_net_clients++] = "nic";
5431#ifdef CONFIG_SLIRP
5432 net_clients[nb_net_clients++] = "user";
5433#endif
bellardc20709a2004-04-21 23:27:19 +00005434 }
5435
bellard7c9d8e02005-11-15 22:16:05 +00005436 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005437 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005438 exit(1);
bellard702c6512004-04-02 21:21:32 +00005439 }
aliguori63a01ef2008-10-31 19:10:00 +00005440 net_client_check();
bellardf1510b22003-06-25 00:07:40 +00005441
thseec85c22007-01-05 17:41:07 +00005442#ifdef TARGET_I386
balroged494d82007-12-11 23:23:52 +00005443 /* XXX: this should be moved in the PC machine instantiation code */
j_mayer28c5af52007-11-11 01:50:45 +00005444 if (net_boot != 0) {
5445 int netroms = 0;
5446 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00005447 const char *model = nd_table[i].model;
5448 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00005449 if (net_boot & (1 << i)) {
5450 if (model == NULL)
5451 model = "ne2k_pci";
5452 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5453 if (get_image_size(buf) > 0) {
5454 if (nb_option_roms >= MAX_OPTION_ROMS) {
5455 fprintf(stderr, "Too many option ROMs\n");
5456 exit(1);
5457 }
5458 option_rom[nb_option_roms] = strdup(buf);
5459 nb_option_roms++;
5460 netroms++;
5461 }
5462 }
thseec85c22007-01-05 17:41:07 +00005463 }
j_mayer28c5af52007-11-11 01:50:45 +00005464 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00005465 fprintf(stderr, "No valid PXE rom found for network device\n");
5466 exit(1);
5467 }
thseec85c22007-01-05 17:41:07 +00005468 }
5469#endif
5470
balrogdc72ac12008-11-09 00:04:26 +00005471 /* init the bluetooth world */
5472 for (i = 0; i < nb_bt_opts; i++)
5473 if (bt_parse(bt_opts[i]))
5474 exit(1);
5475
bellard0824d6f2003-06-24 13:42:40 +00005476 /* init the memory */
balrog7fb4fdc2008-04-24 17:59:27 +00005477 phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
5478
5479 if (machine->ram_require & RAMSIZE_FIXED) {
5480 if (ram_size > 0) {
5481 if (ram_size < phys_ram_size) {
aurel32cd940062008-04-28 20:26:54 +00005482 fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
5483 machine->name, (unsigned long long) phys_ram_size);
balrog7fb4fdc2008-04-24 17:59:27 +00005484 exit(-1);
5485 }
5486
5487 phys_ram_size = ram_size;
5488 } else
5489 ram_size = phys_ram_size;
5490 } else {
aurel324fc5d072008-04-27 21:39:40 +00005491 if (ram_size == 0)
balrog7fb4fdc2008-04-24 17:59:27 +00005492 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5493
5494 phys_ram_size += ram_size;
5495 }
ths9ae02552007-01-05 17:39:04 +00005496
bellardd993e022005-02-10 22:00:06 +00005497 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00005498 if (!phys_ram_base) {
5499 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00005500 exit(1);
5501 }
5502
bellard26a5f132008-05-28 12:30:31 +00005503 /* init the dynamic translator */
5504 cpu_exec_init_all(tb_size * 1024 * 1024);
5505
bellard5905b2e2004-08-01 21:53:26 +00005506 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005507
5508 /* we always create the cdrom drive, even if no disk is there */
5509
5510 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005511 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005512
balrog9d413d12007-12-04 00:10:34 +00005513 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005514
5515 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005516 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005517
balrog9d413d12007-12-04 00:10:34 +00005518 /* we always create one sd slot, even if no card is in it */
5519
5520 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005521 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005522
ths96d30e42007-01-07 20:42:14 +00005523 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005524
thse4bcb142007-12-02 04:51:10 +00005525 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005526 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005527 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005528
bellardc88676f2006-08-06 13:36:11 +00005529 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005530 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005531
aliguori3023f332009-01-16 19:04:14 +00005532#ifndef _WIN32
5533 /* must be after terminal init, SDL library changes signal handlers */
5534 termsig_setup();
5535#endif
5536
5537 /* Maintain compatibility with multiple stdio monitors */
5538 if (!strcmp(monitor_device,"stdio")) {
5539 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5540 const char *devname = serial_devices[i];
5541 if (devname && !strcmp(devname,"mon:stdio")) {
5542 monitor_device = NULL;
5543 break;
5544 } else if (devname && !strcmp(devname,"stdio")) {
5545 monitor_device = NULL;
5546 serial_devices[i] = "mon:stdio";
5547 break;
5548 }
5549 }
5550 }
5551
5552 if (kvm_enabled()) {
5553 int ret;
5554
5555 ret = kvm_init(smp_cpus);
5556 if (ret < 0) {
5557 fprintf(stderr, "failed to initialize KVM\n");
5558 exit(1);
5559 }
5560 }
5561
aliguori4c621802009-01-16 21:48:20 +00005562 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005563 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005564 if (!monitor_hd) {
5565 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5566 exit(1);
5567 }
5568 }
5569
aliguori2796dae2009-01-16 20:23:27 +00005570 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5571 const char *devname = serial_devices[i];
5572 if (devname && strcmp(devname, "none")) {
5573 char label[32];
5574 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005575 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005576 if (!serial_hds[i]) {
5577 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5578 devname);
5579 exit(1);
5580 }
5581 }
5582 }
5583
5584 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5585 const char *devname = parallel_devices[i];
5586 if (devname && strcmp(devname, "none")) {
5587 char label[32];
5588 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005589 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005590 if (!parallel_hds[i]) {
5591 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5592 devname);
5593 exit(1);
5594 }
5595 }
5596 }
5597
5598 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5599 const char *devname = virtio_consoles[i];
5600 if (devname && strcmp(devname, "none")) {
5601 char label[32];
5602 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005603 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005604 if (!virtcon_hds[i]) {
5605 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5606 devname);
5607 exit(1);
5608 }
5609 }
5610 }
5611
aliguori3023f332009-01-16 19:04:14 +00005612 machine->init(ram_size, vga_ram_size, boot_devices,
5613 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5614
aliguori6f338c32009-02-11 15:21:54 +00005615 current_machine = machine;
5616
aliguori3023f332009-01-16 19:04:14 +00005617 /* Set KVM's vcpu state to qemu's initial CPUState. */
5618 if (kvm_enabled()) {
5619 int ret;
5620
5621 ret = kvm_sync_vcpus();
5622 if (ret < 0) {
5623 fprintf(stderr, "failed to initialize vcpus\n");
5624 exit(1);
5625 }
5626 }
5627
5628 /* init USB devices */
5629 if (usb_enabled) {
5630 for(i = 0; i < usb_devices_index; i++) {
aliguoric0f4ce72009-03-05 23:01:01 +00005631 if (usb_device_add(usb_devices[i], 0) < 0) {
aliguori3023f332009-01-16 19:04:14 +00005632 fprintf(stderr, "Warning: could not add USB device %s\n",
5633 usb_devices[i]);
5634 }
5635 }
5636 }
5637
aliguori8f391ab2009-01-19 16:34:10 +00005638 if (!display_state)
5639 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005640 /* just use the first displaystate for the moment */
5641 ds = display_state;
bellard313aa562003-08-10 21:52:11 +00005642 /* terminal init */
bellarda20dd502003-09-30 21:07:02 +00005643 if (nographic) {
balrog4d3b6f62008-02-10 16:33:14 +00005644 if (curses) {
5645 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5646 exit(1);
5647 }
aliguori7d957bd2009-01-15 22:14:11 +00005648 } else {
balrog4d3b6f62008-02-10 16:33:14 +00005649#if defined(CONFIG_CURSES)
aliguori7d957bd2009-01-15 22:14:11 +00005650 if (curses) {
5651 /* At the moment curses cannot be used with other displays */
5652 curses_display_init(ds, full_screen);
5653 } else
balrog4d3b6f62008-02-10 16:33:14 +00005654#endif
aliguori7d957bd2009-01-15 22:14:11 +00005655 {
5656 if (vnc_display != NULL) {
5657 vnc_display_init(ds);
5658 if (vnc_display_open(ds, vnc_display) < 0)
5659 exit(1);
5660 }
bellard5b0753e2005-03-01 21:37:28 +00005661#if defined(CONFIG_SDL)
aliguorid268de02009-01-22 16:18:33 +00005662 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005663 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00005664#elif defined(CONFIG_COCOA)
aliguorid268de02009-01-22 16:18:33 +00005665 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005666 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00005667#endif
aliguori7d957bd2009-01-15 22:14:11 +00005668 }
bellard313aa562003-08-10 21:52:11 +00005669 }
aliguori7d957bd2009-01-15 22:14:11 +00005670 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005671
aliguori3023f332009-01-16 19:04:14 +00005672 dcl = ds->listeners;
5673 while (dcl != NULL) {
5674 if (dcl->dpy_refresh != NULL) {
5675 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5676 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005677 }
aliguori3023f332009-01-16 19:04:14 +00005678 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005679 }
aliguori3023f332009-01-16 19:04:14 +00005680
blueswir19043b622009-01-21 19:28:13 +00005681 if (nographic || (vnc_display && !sdl)) {
5682 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5683 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5684 }
5685
aliguori2796dae2009-01-16 20:23:27 +00005686 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00005687 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00005688
aliguori4c621802009-01-16 21:48:20 +00005689 if (monitor_device && monitor_hd)
aliguoricde76ee2009-03-05 23:01:51 +00005690 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
bellard82c643f2004-07-14 17:28:13 +00005691
bellard8d11df92004-08-24 21:13:40 +00005692 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005693 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005694 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005695 char label[32];
5696 snprintf(label, sizeof(label), "serial%d", i);
thsaf3a9032007-07-11 23:14:59 +00005697 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005698 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005699 }
bellard82c643f2004-07-14 17:28:13 +00005700 }
bellard82c643f2004-07-14 17:28:13 +00005701
bellard6508fe52005-01-15 12:02:56 +00005702 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005703 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005704 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005705 char label[32];
5706 snprintf(label, sizeof(label), "parallel%d", i);
thsaf3a9032007-07-11 23:14:59 +00005707 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005708 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005709 }
5710 }
5711
aliguori9ede2fd2009-01-15 20:05:25 +00005712 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5713 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005714 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005715 char label[32];
5716 snprintf(label, sizeof(label), "virtcon%d", i);
aliguori9ede2fd2009-01-15 20:05:25 +00005717 if (strstart(devname, "vc", 0))
5718 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5719 }
5720 }
5721
bellard67b915a2004-03-31 23:37:16 +00005722#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00005723 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00005724 /* XXX: use standard host:port notation and modify options
5725 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00005726 if (gdbserver_start(gdbstub_port) < 0) {
5727 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00005728 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00005729 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00005730 }
balrog45669e02007-07-02 13:20:17 +00005731 }
bellard67b915a2004-03-31 23:37:16 +00005732#endif
balrog45669e02007-07-02 13:20:17 +00005733
bellardd63d3072004-10-03 13:29:03 +00005734 if (loadvm)
aliguori376253e2009-03-05 23:01:23 +00005735 do_loadvm(cur_mon, loadvm);
bellardd63d3072004-10-03 13:29:03 +00005736
aliguori5bb79102008-10-13 03:12:02 +00005737 if (incoming) {
5738 autostart = 0; /* fixme how to deal with -daemonize */
5739 qemu_start_incoming_migration(incoming);
5740 }
5741
aliguoric0f4ce72009-03-05 23:01:01 +00005742 if (autostart)
5743 vm_start();
thsffd843b2006-12-21 19:46:43 +00005744
ths71e3ceb2006-12-22 02:11:31 +00005745 if (daemonize) {
5746 uint8_t status = 0;
5747 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005748
5749 again1:
5750 len = write(fds[1], &status, 1);
5751 if (len == -1 && (errno == EINTR))
5752 goto again1;
5753
5754 if (len != 1)
5755 exit(1);
5756
aliguoribd54b862008-07-23 00:58:33 +00005757 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005758 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005759 if (fd == -1)
5760 exit(1);
aliguori08585322009-02-27 22:09:45 +00005761 }
ths71e3ceb2006-12-22 02:11:31 +00005762
aliguori08585322009-02-27 22:09:45 +00005763#ifndef _WIN32
5764 if (run_as) {
5765 pwd = getpwnam(run_as);
5766 if (!pwd) {
5767 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5768 exit(1);
5769 }
5770 }
ths71e3ceb2006-12-22 02:11:31 +00005771
aliguori08585322009-02-27 22:09:45 +00005772 if (chroot_dir) {
5773 if (chroot(chroot_dir) < 0) {
5774 fprintf(stderr, "chroot failed\n");
5775 exit(1);
5776 }
5777 chdir("/");
5778 }
5779
5780 if (run_as) {
5781 if (setgid(pwd->pw_gid) < 0) {
5782 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5783 exit(1);
5784 }
5785 if (setuid(pwd->pw_uid) < 0) {
5786 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5787 exit(1);
5788 }
5789 if (setuid(0) != -1) {
5790 fprintf(stderr, "Dropping privileges failed\n");
5791 exit(1);
5792 }
5793 }
5794#endif
5795
5796 if (daemonize) {
5797 dup2(fd, 0);
5798 dup2(fd, 1);
5799 dup2(fd, 2);
5800
5801 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00005802 }
5803
bellard8a7ddc32004-03-31 19:00:16 +00005804 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005805 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005806 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005807
bellard0824d6f2003-06-24 13:42:40 +00005808 return 0;
5809}