blob: 437f41af379b18da4e49b955216f4ae26fb179d2 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
blueswir1179a2c12009-03-08 08:23:32 +000032/* Needed early for HOST_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +000036#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000038#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000039#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000042#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000043#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000044#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <net/if.h>
46#if defined(__NetBSD__)
47#include <net/if_tap.h>
48#endif
49#ifdef __linux__
50#include <linux/if_tun.h>
51#endif
52#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000053#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000054#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000055#include <sys/select.h>
blueswir1179a2c12009-03-08 08:23:32 +000056#ifdef HOST_BSD
bellard7d3505c2004-05-12 19:32:15 +000057#include <sys/stat.h>
blueswir1c5e97232009-03-07 20:06:23 +000058#if defined(__FreeBSD__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000059#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000060#else
61#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000062#endif
ths5c40d2b2007-06-23 16:03:36 +000063#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
64#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000065#else
blueswir1223f0d72008-09-30 18:12:18 +000066#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000067#include <pty.h>
68#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000069#include <linux/rtc.h>
thsbd494f42007-09-16 20:03:23 +000070
71/* For the benefit of older linux systems which don't supply it,
72 we use a local copy of hpet.h. */
73/* #include <linux/hpet.h> */
74#include "hpet.h"
75
bellarde57a8c02005-11-10 23:58:52 +000076#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000077#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000078#endif
79#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000080#include <sys/stat.h>
81#include <sys/ethernet.h>
82#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000083#include <netinet/arp.h>
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#include <netinet/ip_icmp.h> // must come after ip.h
88#include <netinet/udp.h>
89#include <netinet/tcp.h>
90#include <net/if.h>
91#include <syslog.h>
92#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000093#endif
bellard7d3505c2004-05-12 19:32:15 +000094#endif
bellardec530c82006-04-25 22:36:06 +000095#endif
bellard67b915a2004-03-31 23:37:16 +000096
blueswir19892fbf2008-08-24 10:34:20 +000097#if defined(__OpenBSD__)
98#include <util.h>
99#endif
100
ths8a16d272008-07-19 09:56:24 +0000101#if defined(CONFIG_VDE)
102#include <libvdeplug.h>
103#endif
104
bellard67b915a2004-03-31 23:37:16 +0000105#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000106#include <windows.h>
bellard7d3505c2004-05-12 19:32:15 +0000107#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000108#include <sys/timeb.h>
ths4fddf622007-12-17 04:42:29 +0000109#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000110#define getopt_long_only getopt_long
111#define memalign(align, size) malloc(size)
112#endif
113
bellard73332e52004-04-04 20:22:28 +0000114#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +0000115#ifdef __APPLE__
bellard83fb7ad2004-07-05 21:25:26 +0000116#include <SDL/SDL.h>
malc880fec52009-02-15 20:18:41 +0000117int qemu_main(int argc, char **argv, char **envp);
118int main(int argc, char **argv)
119{
120 qemu_main(argc, argv, NULL);
121}
122#undef main
123#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000124#endif
bellard73332e52004-04-04 20:22:28 +0000125#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000126
bellard5b0753e2005-03-01 21:37:28 +0000127#ifdef CONFIG_COCOA
128#undef main
129#define main qemu_main
130#endif /* CONFIG_COCOA */
131
blueswir1511d2b12009-03-07 15:32:56 +0000132#include "hw/hw.h"
133#include "hw/boards.h"
134#include "hw/usb.h"
135#include "hw/pcmcia.h"
136#include "hw/pc.h"
137#include "hw/audiodev.h"
138#include "hw/isa.h"
139#include "hw/baum.h"
140#include "hw/bt.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000141#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000142#include "hw/xen.h"
aurel325ef4efa2009-03-10 21:43:35 +0000143#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000144#include "net.h"
145#include "monitor.h"
146#include "console.h"
147#include "sysemu.h"
148#include "gdbstub.h"
149#include "qemu-timer.h"
150#include "qemu-char.h"
151#include "cache-utils.h"
152#include "block.h"
blueswir1a718ace2009-03-28 08:24:44 +0000153#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000154#include "audio/audio.h"
155#include "migration.h"
156#include "kvm.h"
157#include "balloon.h"
158
bellard0824d6f2003-06-24 13:42:40 +0000159#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000160
bellard8a7ddc32004-03-31 19:00:16 +0000161#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000162
blueswir1511d2b12009-03-07 15:32:56 +0000163#include "qemu_socket.h"
164
165#if defined(CONFIG_SLIRP)
166#include "libslirp.h"
167#endif
168
bellard0824d6f2003-06-24 13:42:40 +0000169//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000170//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000171//#define DEBUG_NET
172//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000173
aliguorid12d51d2009-01-15 21:48:06 +0000174
175#ifdef DEBUG_IOPORT
aliguori93fcfe32009-01-15 22:34:14 +0000176# define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
aliguorid12d51d2009-01-15 21:48:06 +0000177#else
178# define LOG_IOPORT(...) do { } while (0)
179#endif
180
bellard1bfe8562004-07-08 21:17:50 +0000181#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000182
pbrook0d92ed32006-05-21 16:30:15 +0000183/* Max number of USB devices that can be specified on the commandline. */
184#define MAX_USB_CMDLINE 8
185
balrogdc72ac12008-11-09 00:04:26 +0000186/* Max number of bluetooth switches on the commandline. */
187#define MAX_BT_CMDLINE 10
188
bellard7dea1da2003-11-16 15:59:30 +0000189/* XXX: use a two level table to limit memory usage */
190#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000191
bellard80cabfa2004-03-14 12:20:30 +0000192const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000193const char *bios_name = NULL;
blueswir1dbed7e42008-10-01 19:38:09 +0000194static void *ioport_opaque[MAX_IOPORTS];
195static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
196static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
thse4bcb142007-12-02 04:51:10 +0000197/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000198 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000199DriveInfo drives_table[MAX_DRIVES+1];
200int nb_drives;
blueswir1dbed7e42008-10-01 19:38:09 +0000201static int vga_ram_size;
malccb5a7aa2008-09-28 00:42:12 +0000202enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000203static DisplayState *display_state;
bellarda20dd502003-09-30 21:07:02 +0000204int nographic;
blueswir1dbed7e42008-10-01 19:38:09 +0000205static int curses;
aliguori7d957bd2009-01-15 22:14:11 +0000206static int sdl;
bellard3d11d0e2004-12-12 16:56:30 +0000207const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000208int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000209ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000210int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000211NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000212int vm_running;
aliguoric0f4ce72009-03-05 23:01:01 +0000213static int autostart;
balrogf6503052008-02-17 11:42:19 +0000214static int rtc_utc = 1;
215static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000216int cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +0000217int std_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000218int vmsvga_enabled = 0;
aliguori94909d92009-04-22 15:19:53 +0000219int xenfb_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000220#ifdef TARGET_SPARC
221int graphic_width = 1024;
222int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000223int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000224#else
bellard1bfe8562004-07-08 21:17:50 +0000225int graphic_width = 800;
226int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000227int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000228#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000229static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000230#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000231static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000232#endif
ths667acca2006-12-11 02:08:05 +0000233int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000234CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000235CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000236CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000237#ifdef TARGET_I386
238int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000239int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000240#endif
bellardbb36d472005-11-05 14:22:28 +0000241int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000242int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000243int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000244const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000245int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000246int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000247int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000248int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000249int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000250int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000251int graphic_rotate = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000252#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000253int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000254#endif
ths9ae02552007-01-05 17:39:04 +0000255const char *option_rom[MAX_OPTION_ROMS];
256int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000257int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000258#ifdef TARGET_ARM
259int old_param = 0;
260#endif
thsc35734b2007-03-19 15:17:08 +0000261const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000262int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000263#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000264unsigned int nb_prom_envs = 0;
265const char *prom_envs[MAX_PROM_ENVS];
266#endif
aliguoriec691c82009-02-11 15:20:37 +0000267int nb_drives_opt;
268struct drive_opt drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000269
aliguori268a3622009-04-21 22:30:27 +0000270int nb_numa_nodes;
271uint64_t node_mem[MAX_NODES];
272uint64_t node_cpumask[MAX_NODES];
273
balrogee5605e2007-12-03 03:01:40 +0000274static CPUState *cur_cpu;
275static CPUState *next_cpu;
balrog76ea08f2007-12-16 11:48:54 +0000276static int event_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000277/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000278static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000279/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000280#define MAX_ICOUNT_SHIFT 10
281/* Compensate for varying guest execution speed. */
282static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000283static QEMUTimer *icount_rt_timer;
284static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000285static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000286
blueswir18fcb1b92008-09-18 18:29:08 +0000287uint8_t qemu_uuid[16];
288
bellard0824d6f2003-06-24 13:42:40 +0000289/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000290/* x86 ISA bus support */
291
292target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000293PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000294
aliguori477e3ed2008-07-23 15:19:59 +0000295static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
296static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
297
298static uint32_t ioport_read(int index, uint32_t address)
299{
300 static IOPortReadFunc *default_func[3] = {
301 default_ioport_readb,
302 default_ioport_readw,
303 default_ioport_readl
304 };
305 IOPortReadFunc *func = ioport_read_table[index][address];
306 if (!func)
307 func = default_func[index];
308 return func(ioport_opaque[address], address);
309}
310
311static void ioport_write(int index, uint32_t address, uint32_t data)
312{
313 static IOPortWriteFunc *default_func[3] = {
314 default_ioport_writeb,
315 default_ioport_writew,
316 default_ioport_writel
317 };
318 IOPortWriteFunc *func = ioport_write_table[index][address];
319 if (!func)
320 func = default_func[index];
321 func(ioport_opaque[address], address, data);
322}
323
pbrook9596ebb2007-11-18 01:44:38 +0000324static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000325{
326#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000327 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000328#endif
bellardfc01f7e2003-06-30 10:03:06 +0000329 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000330}
331
pbrook9596ebb2007-11-18 01:44:38 +0000332static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000333{
334#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000335 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000336#endif
337}
338
339/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000340static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000341{
342 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000343 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000344 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000345 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000346 return data;
347}
348
pbrook9596ebb2007-11-18 01:44:38 +0000349static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000350{
aliguori477e3ed2008-07-23 15:19:59 +0000351 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000352 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000353 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000354}
355
pbrook9596ebb2007-11-18 01:44:38 +0000356static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000357{
358#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000359 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000360#endif
361 return 0xffffffff;
362}
363
pbrook9596ebb2007-11-18 01:44:38 +0000364static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000365{
366#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000367 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000368#endif
bellard0824d6f2003-06-24 13:42:40 +0000369}
370
bellardfc01f7e2003-06-30 10:03:06 +0000371/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000372int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000373 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000374{
bellardfc01f7e2003-06-30 10:03:06 +0000375 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000376
bellardc4b1fcc2004-03-14 21:44:30 +0000377 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000378 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000379 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000380 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000381 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000382 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000383 } else {
balrog88fdf562008-04-26 21:11:22 +0000384 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000385 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000386 }
387 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000388 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000389 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000390 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000391 ioport_opaque[i] = opaque;
392 }
bellard0824d6f2003-06-24 13:42:40 +0000393 return 0;
394}
395
bellardfc01f7e2003-06-30 10:03:06 +0000396/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000397int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000398 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000399{
bellardfc01f7e2003-06-30 10:03:06 +0000400 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000401
bellardc4b1fcc2004-03-14 21:44:30 +0000402 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000403 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000404 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000405 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000406 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000407 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000408 } else {
balrog88fdf562008-04-26 21:11:22 +0000409 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000410 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000411 }
412 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000413 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000414 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
415 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000416 ioport_opaque[i] = opaque;
417 }
bellardf1510b22003-06-25 00:07:40 +0000418 return 0;
419}
420
bellard69b91032004-05-18 23:05:28 +0000421void isa_unassign_ioport(int start, int length)
422{
423 int i;
424
425 for(i = start; i < start + length; i++) {
426 ioport_read_table[0][i] = default_ioport_readb;
427 ioport_read_table[1][i] = default_ioport_readw;
428 ioport_read_table[2][i] = default_ioport_readl;
429
430 ioport_write_table[0][i] = default_ioport_writeb;
431 ioport_write_table[1][i] = default_ioport_writew;
432 ioport_write_table[2][i] = default_ioport_writel;
aliguoria7607f72009-02-11 15:21:29 +0000433
434 ioport_opaque[i] = NULL;
bellard69b91032004-05-18 23:05:28 +0000435 }
436}
437
bellard20f32282005-01-03 23:36:21 +0000438/***********************************************************/
439
bellardc45886d2004-01-05 00:02:06 +0000440void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000441{
aliguorid12d51d2009-01-15 21:48:06 +0000442 LOG_IOPORT("outb: %04x %02x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000443 ioport_write(0, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000444#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000445 if (env)
446 env->last_io_time = cpu_get_time_fast();
447#endif
bellard0824d6f2003-06-24 13:42:40 +0000448}
449
bellardc45886d2004-01-05 00:02:06 +0000450void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000451{
aliguorid12d51d2009-01-15 21:48:06 +0000452 LOG_IOPORT("outw: %04x %04x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000453 ioport_write(1, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000454#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000455 if (env)
456 env->last_io_time = cpu_get_time_fast();
457#endif
bellard0824d6f2003-06-24 13:42:40 +0000458}
459
bellardc45886d2004-01-05 00:02:06 +0000460void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000461{
aliguorid12d51d2009-01-15 21:48:06 +0000462 LOG_IOPORT("outl: %04x %08x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000463 ioport_write(2, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000464#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000465 if (env)
466 env->last_io_time = cpu_get_time_fast();
467#endif
bellard0824d6f2003-06-24 13:42:40 +0000468}
469
bellardc45886d2004-01-05 00:02:06 +0000470int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000471{
bellardfd872592004-05-12 19:11:15 +0000472 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000473 val = ioport_read(0, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000474 LOG_IOPORT("inb : %04x %02x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000475#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000476 if (env)
477 env->last_io_time = cpu_get_time_fast();
478#endif
bellardfd872592004-05-12 19:11:15 +0000479 return val;
bellard0824d6f2003-06-24 13:42:40 +0000480}
481
bellardc45886d2004-01-05 00:02:06 +0000482int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000483{
bellardfd872592004-05-12 19:11:15 +0000484 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000485 val = ioport_read(1, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000486 LOG_IOPORT("inw : %04x %04x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000487#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000488 if (env)
489 env->last_io_time = cpu_get_time_fast();
490#endif
bellardfd872592004-05-12 19:11:15 +0000491 return val;
bellard0824d6f2003-06-24 13:42:40 +0000492}
493
bellardc45886d2004-01-05 00:02:06 +0000494int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000495{
bellardfd872592004-05-12 19:11:15 +0000496 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000497 val = ioport_read(2, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000498 LOG_IOPORT("inl : %04x %08x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000499#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000500 if (env)
501 env->last_io_time = cpu_get_time_fast();
502#endif
bellardfd872592004-05-12 19:11:15 +0000503 return val;
bellard0824d6f2003-06-24 13:42:40 +0000504}
505
506/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000507void hw_error(const char *fmt, ...)
508{
509 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000510 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000511
512 va_start(ap, fmt);
513 fprintf(stderr, "qemu: hardware error: ");
514 vfprintf(stderr, fmt, ap);
515 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000516 for(env = first_cpu; env != NULL; env = env->next_cpu) {
517 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000518#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000519 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000520#else
bellard6a00d602005-11-21 23:25:50 +0000521 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000522#endif
bellard6a00d602005-11-21 23:25:50 +0000523 }
bellard0824d6f2003-06-24 13:42:40 +0000524 va_end(ap);
525 abort();
526}
aliguoridf751fa2008-12-04 20:19:35 +0000527
528/***************/
529/* ballooning */
530
531static QEMUBalloonEvent *qemu_balloon_event;
532void *qemu_balloon_event_opaque;
533
534void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
535{
536 qemu_balloon_event = func;
537 qemu_balloon_event_opaque = opaque;
538}
539
540void qemu_balloon(ram_addr_t target)
541{
542 if (qemu_balloon_event)
543 qemu_balloon_event(qemu_balloon_event_opaque, target);
544}
545
546ram_addr_t qemu_balloon_status(void)
547{
548 if (qemu_balloon_event)
549 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
550 return 0;
551}
bellard0824d6f2003-06-24 13:42:40 +0000552
bellard8a7ddc32004-03-31 19:00:16 +0000553/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000554/* keyboard/mouse */
555
556static QEMUPutKBDEvent *qemu_put_kbd_event;
557static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000558static QEMUPutMouseEntry *qemu_put_mouse_event_head;
559static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000560
561void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
562{
563 qemu_put_kbd_event_opaque = opaque;
564 qemu_put_kbd_event = func;
565}
566
ths455204e2007-01-05 16:42:13 +0000567QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
568 void *opaque, int absolute,
569 const char *name)
bellard63066f42004-06-03 18:45:02 +0000570{
ths455204e2007-01-05 16:42:13 +0000571 QEMUPutMouseEntry *s, *cursor;
572
573 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000574
575 s->qemu_put_mouse_event = func;
576 s->qemu_put_mouse_event_opaque = opaque;
577 s->qemu_put_mouse_event_absolute = absolute;
578 s->qemu_put_mouse_event_name = qemu_strdup(name);
579 s->next = NULL;
580
581 if (!qemu_put_mouse_event_head) {
582 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
583 return s;
584 }
585
586 cursor = qemu_put_mouse_event_head;
587 while (cursor->next != NULL)
588 cursor = cursor->next;
589
590 cursor->next = s;
591 qemu_put_mouse_event_current = s;
592
593 return s;
594}
595
596void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
597{
598 QEMUPutMouseEntry *prev = NULL, *cursor;
599
600 if (!qemu_put_mouse_event_head || entry == NULL)
601 return;
602
603 cursor = qemu_put_mouse_event_head;
604 while (cursor != NULL && cursor != entry) {
605 prev = cursor;
606 cursor = cursor->next;
607 }
608
609 if (cursor == NULL) // does not exist or list empty
610 return;
611 else if (prev == NULL) { // entry is head
612 qemu_put_mouse_event_head = cursor->next;
613 if (qemu_put_mouse_event_current == entry)
614 qemu_put_mouse_event_current = cursor->next;
615 qemu_free(entry->qemu_put_mouse_event_name);
616 qemu_free(entry);
617 return;
618 }
619
620 prev->next = entry->next;
621
622 if (qemu_put_mouse_event_current == entry)
623 qemu_put_mouse_event_current = prev;
624
625 qemu_free(entry->qemu_put_mouse_event_name);
626 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000627}
628
629void kbd_put_keycode(int keycode)
630{
631 if (qemu_put_kbd_event) {
632 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
633 }
634}
635
636void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
637{
ths455204e2007-01-05 16:42:13 +0000638 QEMUPutMouseEvent *mouse_event;
639 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000640 int width;
ths455204e2007-01-05 16:42:13 +0000641
642 if (!qemu_put_mouse_event_current) {
643 return;
644 }
645
646 mouse_event =
647 qemu_put_mouse_event_current->qemu_put_mouse_event;
648 mouse_event_opaque =
649 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
650
651 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000652 if (graphic_rotate) {
653 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
654 width = 0x7fff;
655 else
aurel32b94ed572008-03-10 19:34:27 +0000656 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000657 mouse_event(mouse_event_opaque,
658 width - dy, dx, dz, buttons_state);
659 } else
660 mouse_event(mouse_event_opaque,
661 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000662 }
663}
664
bellard09b26c52006-04-12 21:09:08 +0000665int kbd_mouse_is_absolute(void)
666{
ths455204e2007-01-05 16:42:13 +0000667 if (!qemu_put_mouse_event_current)
668 return 0;
669
670 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
671}
672
aliguori376253e2009-03-05 23:01:23 +0000673void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000674{
675 QEMUPutMouseEntry *cursor;
676 int index = 0;
677
678 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000679 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000680 return;
681 }
682
aliguori376253e2009-03-05 23:01:23 +0000683 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000684 cursor = qemu_put_mouse_event_head;
685 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000686 monitor_printf(mon, "%c Mouse #%d: %s\n",
687 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
688 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000689 index++;
690 cursor = cursor->next;
691 }
692}
693
aliguori376253e2009-03-05 23:01:23 +0000694void do_mouse_set(Monitor *mon, int index)
ths455204e2007-01-05 16:42:13 +0000695{
696 QEMUPutMouseEntry *cursor;
697 int i = 0;
698
699 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000700 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000701 return;
702 }
703
704 cursor = qemu_put_mouse_event_head;
705 while (cursor != NULL && index != i) {
706 i++;
707 cursor = cursor->next;
708 }
709
710 if (cursor != NULL)
711 qemu_put_mouse_event_current = cursor;
712 else
aliguori376253e2009-03-05 23:01:23 +0000713 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000714}
715
bellard87858c82003-06-27 12:01:39 +0000716/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000717uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000718{
719 union {
720 uint64_t ll;
721 struct {
722#ifdef WORDS_BIGENDIAN
723 uint32_t high, low;
724#else
725 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000726#endif
bellard87858c82003-06-27 12:01:39 +0000727 } l;
728 } u, res;
729 uint64_t rl, rh;
730
731 u.ll = a;
732 rl = (uint64_t)u.l.low * (uint64_t)b;
733 rh = (uint64_t)u.l.high * (uint64_t)b;
734 rh += (rl >> 32);
735 res.l.high = rh / c;
736 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
737 return res.ll;
738}
739
bellard1dce7c32006-07-13 23:20:22 +0000740/***********************************************************/
741/* real time host monotonic timer */
742
743#define QEMU_TIMER_BASE 1000000000LL
744
745#ifdef WIN32
746
747static int64_t clock_freq;
748
749static void init_get_clock(void)
750{
bellarda8e5ac32006-07-14 09:36:13 +0000751 LARGE_INTEGER freq;
752 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000753 ret = QueryPerformanceFrequency(&freq);
754 if (ret == 0) {
755 fprintf(stderr, "Could not calibrate ticks\n");
756 exit(1);
757 }
758 clock_freq = freq.QuadPart;
759}
760
761static int64_t get_clock(void)
762{
763 LARGE_INTEGER ti;
764 QueryPerformanceCounter(&ti);
765 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
766}
767
768#else
769
770static int use_rt_clock;
771
772static void init_get_clock(void)
773{
774 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000775#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
776 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000777 {
778 struct timespec ts;
779 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
780 use_rt_clock = 1;
781 }
782 }
783#endif
784}
785
786static int64_t get_clock(void)
787{
blueswir1c5e97232009-03-07 20:06:23 +0000788#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
789 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000790 if (use_rt_clock) {
791 struct timespec ts;
792 clock_gettime(CLOCK_MONOTONIC, &ts);
793 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000794 } else
bellard1dce7c32006-07-13 23:20:22 +0000795#endif
796 {
797 /* XXX: using gettimeofday leads to problems if the date
798 changes, so it should be avoided. */
799 struct timeval tv;
800 gettimeofday(&tv, NULL);
801 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
802 }
803}
bellard1dce7c32006-07-13 23:20:22 +0000804#endif
805
pbrook2e70f6e2008-06-29 01:03:05 +0000806/* Return the virtual CPU time, based on the instruction counter. */
807static int64_t cpu_get_icount(void)
808{
809 int64_t icount;
810 CPUState *env = cpu_single_env;;
811 icount = qemu_icount;
812 if (env) {
813 if (!can_do_io(env))
814 fprintf(stderr, "Bad clock read\n");
815 icount -= (env->icount_decr.u16.low + env->icount_extra);
816 }
817 return qemu_icount_bias + (icount << icount_time_shift);
818}
819
bellard1dce7c32006-07-13 23:20:22 +0000820/***********************************************************/
821/* guest cycle counter */
822
823static int64_t cpu_ticks_prev;
824static int64_t cpu_ticks_offset;
825static int64_t cpu_clock_offset;
826static int cpu_ticks_enabled;
827
828/* return the host CPU cycle counter and handle stop/restart */
829int64_t cpu_get_ticks(void)
830{
pbrook2e70f6e2008-06-29 01:03:05 +0000831 if (use_icount) {
832 return cpu_get_icount();
833 }
bellard1dce7c32006-07-13 23:20:22 +0000834 if (!cpu_ticks_enabled) {
835 return cpu_ticks_offset;
836 } else {
837 int64_t ticks;
838 ticks = cpu_get_real_ticks();
839 if (cpu_ticks_prev > ticks) {
840 /* Note: non increasing ticks may happen if the host uses
841 software suspend */
842 cpu_ticks_offset += cpu_ticks_prev - ticks;
843 }
844 cpu_ticks_prev = ticks;
845 return ticks + cpu_ticks_offset;
846 }
847}
848
849/* return the host CPU monotonic timer and handle stop/restart */
850static int64_t cpu_get_clock(void)
851{
852 int64_t ti;
853 if (!cpu_ticks_enabled) {
854 return cpu_clock_offset;
855 } else {
856 ti = get_clock();
857 return ti + cpu_clock_offset;
858 }
859}
860
861/* enable cpu_get_ticks() */
862void cpu_enable_ticks(void)
863{
864 if (!cpu_ticks_enabled) {
865 cpu_ticks_offset -= cpu_get_real_ticks();
866 cpu_clock_offset -= get_clock();
867 cpu_ticks_enabled = 1;
868 }
869}
870
871/* disable cpu_get_ticks() : the clock is stopped. You must not call
872 cpu_get_ticks() after that. */
873void cpu_disable_ticks(void)
874{
875 if (cpu_ticks_enabled) {
876 cpu_ticks_offset = cpu_get_ticks();
877 cpu_clock_offset = cpu_get_clock();
878 cpu_ticks_enabled = 0;
879 }
880}
881
882/***********************************************************/
883/* timers */
ths5fafdf22007-09-16 21:08:06 +0000884
bellard8a7ddc32004-03-31 19:00:16 +0000885#define QEMU_TIMER_REALTIME 0
886#define QEMU_TIMER_VIRTUAL 1
887
888struct QEMUClock {
889 int type;
890 /* XXX: add frequency */
891};
892
893struct QEMUTimer {
894 QEMUClock *clock;
895 int64_t expire_time;
896 QEMUTimerCB *cb;
897 void *opaque;
898 struct QEMUTimer *next;
899};
900
thsc8994012007-08-19 21:56:03 +0000901struct qemu_alarm_timer {
902 char const *name;
thsefe75412007-08-24 01:36:32 +0000903 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000904
905 int (*start)(struct qemu_alarm_timer *t);
906 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000907 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000908 void *priv;
909};
910
thsefe75412007-08-24 01:36:32 +0000911#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000912#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000913
914static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
915{
916 return t->flags & ALARM_FLAG_DYNTICKS;
917}
918
919static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
920{
921 if (!alarm_has_dynticks(t))
922 return;
923
924 t->rearm(t);
925}
926
927/* TODO: MIN_TIMER_REARM_US should be optimized */
928#define MIN_TIMER_REARM_US 250
929
thsc8994012007-08-19 21:56:03 +0000930static struct qemu_alarm_timer *alarm_timer;
aliguorif49e58d2008-11-05 21:22:34 +0000931#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +0000932static int alarm_timer_rfd, alarm_timer_wfd;
aliguorif49e58d2008-11-05 21:22:34 +0000933#endif
thsc8994012007-08-19 21:56:03 +0000934
935#ifdef _WIN32
936
937struct qemu_alarm_win32 {
938 MMRESULT timerId;
939 HANDLE host_alarm;
940 unsigned int period;
941} alarm_win32_data = {0, NULL, -1};
942
943static int win32_start_timer(struct qemu_alarm_timer *t);
944static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000945static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000946
947#else
948
949static int unix_start_timer(struct qemu_alarm_timer *t);
950static void unix_stop_timer(struct qemu_alarm_timer *t);
951
ths231c6582007-08-26 17:29:15 +0000952#ifdef __linux__
953
thsefe75412007-08-24 01:36:32 +0000954static int dynticks_start_timer(struct qemu_alarm_timer *t);
955static void dynticks_stop_timer(struct qemu_alarm_timer *t);
956static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
957
thsc40ec5a2007-08-19 22:09:40 +0000958static int hpet_start_timer(struct qemu_alarm_timer *t);
959static void hpet_stop_timer(struct qemu_alarm_timer *t);
960
thsc8994012007-08-19 21:56:03 +0000961static int rtc_start_timer(struct qemu_alarm_timer *t);
962static void rtc_stop_timer(struct qemu_alarm_timer *t);
963
thsefe75412007-08-24 01:36:32 +0000964#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000965
966#endif /* _WIN32 */
967
pbrook2e70f6e2008-06-29 01:03:05 +0000968/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000969 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000970 When the guest is idle real and virtual time will be aligned in
971 the IO wait loop. */
972#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
973
974static void icount_adjust(void)
975{
976 int64_t cur_time;
977 int64_t cur_icount;
978 int64_t delta;
979 static int64_t last_delta;
980 /* If the VM is not running, then do nothing. */
981 if (!vm_running)
982 return;
983
984 cur_time = cpu_get_clock();
985 cur_icount = qemu_get_clock(vm_clock);
986 delta = cur_icount - cur_time;
987 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
988 if (delta > 0
989 && last_delta + ICOUNT_WOBBLE < delta * 2
990 && icount_time_shift > 0) {
991 /* The guest is getting too far ahead. Slow time down. */
992 icount_time_shift--;
993 }
994 if (delta < 0
995 && last_delta - ICOUNT_WOBBLE > delta * 2
996 && icount_time_shift < MAX_ICOUNT_SHIFT) {
997 /* The guest is getting too far behind. Speed time up. */
998 icount_time_shift++;
999 }
1000 last_delta = delta;
1001 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
1002}
1003
1004static void icount_adjust_rt(void * opaque)
1005{
1006 qemu_mod_timer(icount_rt_timer,
1007 qemu_get_clock(rt_clock) + 1000);
1008 icount_adjust();
1009}
1010
1011static void icount_adjust_vm(void * opaque)
1012{
1013 qemu_mod_timer(icount_vm_timer,
1014 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1015 icount_adjust();
1016}
1017
1018static void init_icount_adjust(void)
1019{
1020 /* Have both realtime and virtual time triggers for speed adjustment.
1021 The realtime trigger catches emulated time passing too slowly,
1022 the virtual time trigger catches emulated time passing too fast.
1023 Realtime triggers occur even when idle, so use them less frequently
1024 than VM triggers. */
1025 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1026 qemu_mod_timer(icount_rt_timer,
1027 qemu_get_clock(rt_clock) + 1000);
1028 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1029 qemu_mod_timer(icount_vm_timer,
1030 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1031}
1032
thsc8994012007-08-19 21:56:03 +00001033static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +00001034#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +00001035#ifdef __linux__
thsefe75412007-08-24 01:36:32 +00001036 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1037 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001038 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +00001039 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001040 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +00001041 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001042#endif
thsefe75412007-08-24 01:36:32 +00001043 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001044#else
thsefe75412007-08-24 01:36:32 +00001045 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1046 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1047 {"win32", 0, win32_start_timer,
1048 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +00001049#endif
1050 {NULL, }
1051};
1052
blueswir13f47aa82008-03-09 06:59:01 +00001053static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +00001054{
1055 int i;
1056
1057 printf("Available alarm timers, in order of precedence:\n");
1058 for (i = 0; alarm_timers[i].name; i++)
1059 printf("%s\n", alarm_timers[i].name);
1060}
1061
1062static void configure_alarms(char const *opt)
1063{
1064 int i;
1065 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +00001066 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +00001067 char *arg;
1068 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +00001069 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +00001070
aurel323adda042008-03-09 23:43:49 +00001071 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +00001072 show_available_alarms();
1073 exit(0);
1074 }
1075
1076 arg = strdup(opt);
1077
1078 /* Reorder the array */
1079 name = strtok(arg, ",");
1080 while (name) {
balroge2b577e2007-09-17 21:25:20 +00001081 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +00001082 if (!strcmp(alarm_timers[i].name, name))
1083 break;
1084 }
1085
1086 if (i == count) {
1087 fprintf(stderr, "Unknown clock %s\n", name);
1088 goto next;
1089 }
1090
1091 if (i < cur)
1092 /* Ignore */
1093 goto next;
1094
1095 /* Swap */
1096 tmp = alarm_timers[i];
1097 alarm_timers[i] = alarm_timers[cur];
1098 alarm_timers[cur] = tmp;
1099
1100 cur++;
1101next:
1102 name = strtok(NULL, ",");
1103 }
1104
1105 free(arg);
1106
1107 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +00001108 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +00001109 for (i = cur; i < count; i++)
1110 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +00001111 } else {
1112 show_available_alarms();
1113 exit(1);
thsf3dcfad2007-08-24 01:26:02 +00001114 }
thsf3dcfad2007-08-24 01:26:02 +00001115}
1116
bellard8a7ddc32004-03-31 19:00:16 +00001117QEMUClock *rt_clock;
1118QEMUClock *vm_clock;
1119
1120static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +00001121
pbrook9596ebb2007-11-18 01:44:38 +00001122static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001123{
1124 QEMUClock *clock;
1125 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +00001126 clock->type = type;
1127 return clock;
1128}
1129
1130QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1131{
1132 QEMUTimer *ts;
1133
1134 ts = qemu_mallocz(sizeof(QEMUTimer));
1135 ts->clock = clock;
1136 ts->cb = cb;
1137 ts->opaque = opaque;
1138 return ts;
1139}
1140
1141void qemu_free_timer(QEMUTimer *ts)
1142{
1143 qemu_free(ts);
1144}
1145
1146/* stop a timer, but do not dealloc it */
1147void qemu_del_timer(QEMUTimer *ts)
1148{
1149 QEMUTimer **pt, *t;
1150
1151 /* NOTE: this code must be signal safe because
1152 qemu_timer_expired() can be called from a signal. */
1153 pt = &active_timers[ts->clock->type];
1154 for(;;) {
1155 t = *pt;
1156 if (!t)
1157 break;
1158 if (t == ts) {
1159 *pt = t->next;
1160 break;
1161 }
1162 pt = &t->next;
1163 }
1164}
1165
1166/* modify the current timer so that it will be fired when current_time
1167 >= expire_time. The corresponding callback will be called. */
1168void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1169{
1170 QEMUTimer **pt, *t;
1171
1172 qemu_del_timer(ts);
1173
1174 /* add the timer in the sorted list */
1175 /* NOTE: this code must be signal safe because
1176 qemu_timer_expired() can be called from a signal. */
1177 pt = &active_timers[ts->clock->type];
1178 for(;;) {
1179 t = *pt;
1180 if (!t)
1181 break;
ths5fafdf22007-09-16 21:08:06 +00001182 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001183 break;
1184 pt = &t->next;
1185 }
1186 ts->expire_time = expire_time;
1187 ts->next = *pt;
1188 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001189
1190 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001191 if (pt == &active_timers[ts->clock->type]) {
1192 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1193 qemu_rearm_alarm_timer(alarm_timer);
1194 }
1195 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +00001196 if (use_icount)
1197 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +00001198 }
bellard8a7ddc32004-03-31 19:00:16 +00001199}
1200
1201int qemu_timer_pending(QEMUTimer *ts)
1202{
1203 QEMUTimer *t;
1204 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1205 if (t == ts)
1206 return 1;
1207 }
1208 return 0;
1209}
1210
1211static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1212{
1213 if (!timer_head)
1214 return 0;
1215 return (timer_head->expire_time <= current_time);
1216}
1217
1218static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1219{
1220 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001221
bellard8a7ddc32004-03-31 19:00:16 +00001222 for(;;) {
1223 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001224 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001225 break;
1226 /* remove timer from the list before calling the callback */
1227 *ptimer_head = ts->next;
1228 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001229
bellard8a7ddc32004-03-31 19:00:16 +00001230 /* run the callback (the timer list can be modified) */
1231 ts->cb(ts->opaque);
1232 }
1233}
1234
1235int64_t qemu_get_clock(QEMUClock *clock)
1236{
1237 switch(clock->type) {
1238 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001239 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001240 default:
1241 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001242 if (use_icount) {
1243 return cpu_get_icount();
1244 } else {
1245 return cpu_get_clock();
1246 }
bellard8a7ddc32004-03-31 19:00:16 +00001247 }
1248}
1249
bellard1dce7c32006-07-13 23:20:22 +00001250static void init_timers(void)
1251{
1252 init_get_clock();
1253 ticks_per_sec = QEMU_TIMER_BASE;
1254 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1255 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1256}
1257
bellard8a7ddc32004-03-31 19:00:16 +00001258/* save a timer */
1259void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1260{
1261 uint64_t expire_time;
1262
1263 if (qemu_timer_pending(ts)) {
1264 expire_time = ts->expire_time;
1265 } else {
1266 expire_time = -1;
1267 }
1268 qemu_put_be64(f, expire_time);
1269}
1270
1271void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1272{
1273 uint64_t expire_time;
1274
1275 expire_time = qemu_get_be64(f);
1276 if (expire_time != -1) {
1277 qemu_mod_timer(ts, expire_time);
1278 } else {
1279 qemu_del_timer(ts);
1280 }
1281}
1282
1283static void timer_save(QEMUFile *f, void *opaque)
1284{
1285 if (cpu_ticks_enabled) {
1286 hw_error("cannot save state if virtual timers are running");
1287 }
thsbee8d682007-12-16 23:41:11 +00001288 qemu_put_be64(f, cpu_ticks_offset);
1289 qemu_put_be64(f, ticks_per_sec);
1290 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001291}
1292
1293static int timer_load(QEMUFile *f, void *opaque, int version_id)
1294{
bellardc88676f2006-08-06 13:36:11 +00001295 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001296 return -EINVAL;
1297 if (cpu_ticks_enabled) {
1298 return -EINVAL;
1299 }
thsbee8d682007-12-16 23:41:11 +00001300 cpu_ticks_offset=qemu_get_be64(f);
1301 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001302 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001303 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001304 }
bellard8a7ddc32004-03-31 19:00:16 +00001305 return 0;
1306}
1307
bellard67b915a2004-03-31 23:37:16 +00001308#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001309static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1310 DWORD_PTR dwUser, DWORD_PTR dw1,
1311 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001312#else
bellard8a7ddc32004-03-31 19:00:16 +00001313static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001314#endif
bellard8a7ddc32004-03-31 19:00:16 +00001315{
bellard02ba45c2004-06-25 14:46:23 +00001316#if 0
1317#define DISP_FREQ 1000
1318 {
1319 static int64_t delta_min = INT64_MAX;
1320 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1321 static int count;
1322 ti = qemu_get_clock(vm_clock);
1323 if (last_clock != 0) {
1324 delta = ti - last_clock;
1325 if (delta < delta_min)
1326 delta_min = delta;
1327 if (delta > delta_max)
1328 delta_max = delta;
1329 delta_cum += delta;
1330 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001331 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001332 muldiv64(delta_min, 1000000, ticks_per_sec),
1333 muldiv64(delta_max, 1000000, ticks_per_sec),
1334 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1335 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1336 count = 0;
1337 delta_min = INT64_MAX;
1338 delta_max = 0;
1339 delta_cum = 0;
1340 }
1341 }
1342 last_clock = ti;
1343 }
1344#endif
thsefe75412007-08-24 01:36:32 +00001345 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001346 (!use_icount &&
1347 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1348 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001349 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1350 qemu_get_clock(rt_clock))) {
aliguoric96f1a42008-11-05 20:29:45 +00001351 CPUState *env = next_cpu;
aliguoric96f1a42008-11-05 20:29:45 +00001352
bellard06d9f2f2006-05-01 13:23:04 +00001353#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001354 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1355 SetEvent(data->host_alarm);
aliguorif49e58d2008-11-05 21:22:34 +00001356#else
1357 static const char byte = 0;
aliguoric96f1a42008-11-05 20:29:45 +00001358 write(alarm_timer_wfd, &byte, sizeof(byte));
aliguorif49e58d2008-11-05 21:22:34 +00001359#endif
balrogd5d08332008-01-05 19:41:47 +00001360 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1361
balrog4f8eb8d2007-12-16 12:39:38 +00001362 if (env) {
1363 /* stop the currently executing cpu because a timer occured */
aurel323098dba2009-03-07 21:28:24 +00001364 cpu_exit(env);
blueswir1640f42e2009-04-19 10:18:01 +00001365#ifdef CONFIG_KQEMU
balrog4f8eb8d2007-12-16 12:39:38 +00001366 if (env->kqemu_enabled) {
1367 kqemu_cpu_interrupt(env);
1368 }
balrogee5605e2007-12-03 03:01:40 +00001369#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001370 }
balrogee5605e2007-12-03 03:01:40 +00001371 event_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001372 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001373 }
1374}
1375
pbrook2e70f6e2008-06-29 01:03:05 +00001376static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001377{
pbrook2e70f6e2008-06-29 01:03:05 +00001378 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001379
1380 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001381 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1382 qemu_get_clock(vm_clock);
1383 } else {
1384 /* To avoid problems with overflow limit this to 2^32. */
1385 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001386 }
1387
pbrook2e70f6e2008-06-29 01:03:05 +00001388 if (delta < 0)
1389 delta = 0;
thsefe75412007-08-24 01:36:32 +00001390
pbrook2e70f6e2008-06-29 01:03:05 +00001391 return delta;
1392}
1393
blueswir18632fb92008-09-14 13:59:34 +00001394#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001395static uint64_t qemu_next_deadline_dyntick(void)
1396{
1397 int64_t delta;
1398 int64_t rtdelta;
1399
1400 if (use_icount)
1401 delta = INT32_MAX;
1402 else
1403 delta = (qemu_next_deadline() + 999) / 1000;
1404
1405 if (active_timers[QEMU_TIMER_REALTIME]) {
1406 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1407 qemu_get_clock(rt_clock))*1000;
1408 if (rtdelta < delta)
1409 delta = rtdelta;
1410 }
1411
1412 if (delta < MIN_TIMER_REARM_US)
1413 delta = MIN_TIMER_REARM_US;
1414
1415 return delta;
thsefe75412007-08-24 01:36:32 +00001416}
blueswir18632fb92008-09-14 13:59:34 +00001417#endif
thsefe75412007-08-24 01:36:32 +00001418
bellardfd872592004-05-12 19:11:15 +00001419#ifndef _WIN32
1420
aliguori7183b4b2008-11-05 20:40:18 +00001421/* Sets a specific flag */
1422static int fcntl_setfl(int fd, int flag)
1423{
1424 int flags;
1425
1426 flags = fcntl(fd, F_GETFL);
1427 if (flags == -1)
1428 return -errno;
1429
1430 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1431 return -errno;
1432
1433 return 0;
1434}
1435
bellard829309c2004-05-20 13:20:12 +00001436#if defined(__linux__)
1437
bellardfd872592004-05-12 19:11:15 +00001438#define RTC_FREQ 1024
1439
aurel32de9a95f2008-11-11 13:41:01 +00001440static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001441{
thsc8994012007-08-19 21:56:03 +00001442 struct sigaction act;
1443
1444 /* timer signal */
1445 sigfillset(&act.sa_mask);
1446 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001447 act.sa_handler = host_alarm_handler;
1448
1449 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001450 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001451 fcntl(fd, F_SETOWN, getpid());
1452}
1453
thsc40ec5a2007-08-19 22:09:40 +00001454static int hpet_start_timer(struct qemu_alarm_timer *t)
1455{
1456 struct hpet_info info;
1457 int r, fd;
1458
1459 fd = open("/dev/hpet", O_RDONLY);
1460 if (fd < 0)
1461 return -1;
1462
1463 /* Set frequency */
1464 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1465 if (r < 0) {
1466 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1467 "error, but for better emulation accuracy type:\n"
1468 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1469 goto fail;
1470 }
1471
1472 /* Check capabilities */
1473 r = ioctl(fd, HPET_INFO, &info);
1474 if (r < 0)
1475 goto fail;
1476
1477 /* Enable periodic mode */
1478 r = ioctl(fd, HPET_EPI, 0);
1479 if (info.hi_flags && (r < 0))
1480 goto fail;
1481
1482 /* Enable interrupt */
1483 r = ioctl(fd, HPET_IE_ON, 0);
1484 if (r < 0)
1485 goto fail;
1486
1487 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001488 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001489
1490 return 0;
1491fail:
1492 close(fd);
1493 return -1;
1494}
1495
1496static void hpet_stop_timer(struct qemu_alarm_timer *t)
1497{
pbrookfcdc2122007-08-23 20:22:22 +00001498 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001499
1500 close(fd);
1501}
1502
thsc8994012007-08-19 21:56:03 +00001503static int rtc_start_timer(struct qemu_alarm_timer *t)
1504{
1505 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001506 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001507
balrogaeb30be2007-07-02 15:03:13 +00001508 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001509 if (rtc_fd < 0)
1510 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001511 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1512 if (current_rtc_freq != RTC_FREQ &&
1513 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001514 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1515 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1516 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1517 goto fail;
1518 }
1519 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1520 fail:
1521 close(rtc_fd);
1522 return -1;
1523 }
thsc8994012007-08-19 21:56:03 +00001524
1525 enable_sigio_timer(rtc_fd);
1526
pbrookfcdc2122007-08-23 20:22:22 +00001527 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001528
bellardfd872592004-05-12 19:11:15 +00001529 return 0;
1530}
1531
thsc8994012007-08-19 21:56:03 +00001532static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001533{
pbrookfcdc2122007-08-23 20:22:22 +00001534 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001535
1536 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001537}
1538
thsefe75412007-08-24 01:36:32 +00001539static int dynticks_start_timer(struct qemu_alarm_timer *t)
1540{
1541 struct sigevent ev;
1542 timer_t host_timer;
1543 struct sigaction act;
1544
1545 sigfillset(&act.sa_mask);
1546 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001547 act.sa_handler = host_alarm_handler;
1548
1549 sigaction(SIGALRM, &act, NULL);
1550
1551 ev.sigev_value.sival_int = 0;
1552 ev.sigev_notify = SIGEV_SIGNAL;
1553 ev.sigev_signo = SIGALRM;
1554
1555 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1556 perror("timer_create");
1557
1558 /* disable dynticks */
1559 fprintf(stderr, "Dynamic Ticks disabled\n");
1560
1561 return -1;
1562 }
1563
blueswir10399bfe2008-11-16 11:37:18 +00001564 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001565
1566 return 0;
1567}
1568
1569static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1570{
blueswir10399bfe2008-11-16 11:37:18 +00001571 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001572
1573 timer_delete(host_timer);
1574}
1575
1576static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1577{
blueswir10399bfe2008-11-16 11:37:18 +00001578 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001579 struct itimerspec timeout;
1580 int64_t nearest_delta_us = INT64_MAX;
1581 int64_t current_us;
1582
1583 if (!active_timers[QEMU_TIMER_REALTIME] &&
1584 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001585 return;
thsefe75412007-08-24 01:36:32 +00001586
pbrook2e70f6e2008-06-29 01:03:05 +00001587 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001588
1589 /* check whether a timer is already running */
1590 if (timer_gettime(host_timer, &timeout)) {
1591 perror("gettime");
1592 fprintf(stderr, "Internal timer error: aborting\n");
1593 exit(1);
1594 }
1595 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1596 if (current_us && current_us <= nearest_delta_us)
1597 return;
1598
1599 timeout.it_interval.tv_sec = 0;
1600 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1601 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1602 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1603 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1604 perror("settime");
1605 fprintf(stderr, "Internal timer error: aborting\n");
1606 exit(1);
1607 }
1608}
1609
ths70744b32007-08-26 17:31:30 +00001610#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001611
thsc8994012007-08-19 21:56:03 +00001612static int unix_start_timer(struct qemu_alarm_timer *t)
1613{
1614 struct sigaction act;
1615 struct itimerval itv;
1616 int err;
1617
1618 /* timer signal */
1619 sigfillset(&act.sa_mask);
1620 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001621 act.sa_handler = host_alarm_handler;
1622
1623 sigaction(SIGALRM, &act, NULL);
1624
1625 itv.it_interval.tv_sec = 0;
1626 /* for i386 kernel 2.6 to get 1 ms */
1627 itv.it_interval.tv_usec = 999;
1628 itv.it_value.tv_sec = 0;
1629 itv.it_value.tv_usec = 10 * 1000;
1630
1631 err = setitimer(ITIMER_REAL, &itv, NULL);
1632 if (err)
1633 return -1;
1634
1635 return 0;
1636}
1637
1638static void unix_stop_timer(struct qemu_alarm_timer *t)
1639{
1640 struct itimerval itv;
1641
1642 memset(&itv, 0, sizeof(itv));
1643 setitimer(ITIMER_REAL, &itv, NULL);
1644}
1645
bellard829309c2004-05-20 13:20:12 +00001646#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001647
aliguorif49e58d2008-11-05 21:22:34 +00001648static void try_to_rearm_timer(void *opaque)
1649{
1650 struct qemu_alarm_timer *t = opaque;
1651#ifndef _WIN32
1652 ssize_t len;
1653
1654 /* Drain the notify pipe */
1655 do {
1656 char buffer[512];
1657 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1658 } while ((len == -1 && errno == EINTR) || len > 0);
1659#endif
1660
aliguorif49e58d2008-11-05 21:22:34 +00001661 if (t->flags & ALARM_FLAG_EXPIRED) {
1662 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1663 qemu_rearm_alarm_timer(alarm_timer);
1664 }
1665}
1666
thsc8994012007-08-19 21:56:03 +00001667#ifdef _WIN32
1668
1669static int win32_start_timer(struct qemu_alarm_timer *t)
1670{
1671 TIMECAPS tc;
1672 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001673 UINT flags;
thsc8994012007-08-19 21:56:03 +00001674
1675 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1676 if (!data->host_alarm) {
1677 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001678 return -1;
thsc8994012007-08-19 21:56:03 +00001679 }
1680
1681 memset(&tc, 0, sizeof(tc));
1682 timeGetDevCaps(&tc, sizeof(tc));
1683
1684 if (data->period < tc.wPeriodMin)
1685 data->period = tc.wPeriodMin;
1686
1687 timeBeginPeriod(data->period);
1688
thsefe75412007-08-24 01:36:32 +00001689 flags = TIME_CALLBACK_FUNCTION;
1690 if (alarm_has_dynticks(t))
1691 flags |= TIME_ONESHOT;
1692 else
1693 flags |= TIME_PERIODIC;
1694
thsc8994012007-08-19 21:56:03 +00001695 data->timerId = timeSetEvent(1, // interval (ms)
1696 data->period, // resolution
1697 host_alarm_handler, // function
1698 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001699 flags);
thsc8994012007-08-19 21:56:03 +00001700
1701 if (!data->timerId) {
1702 perror("Failed to initialize win32 alarm timer");
1703
1704 timeEndPeriod(data->period);
1705 CloseHandle(data->host_alarm);
1706 return -1;
1707 }
1708
aliguorif49e58d2008-11-05 21:22:34 +00001709 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
thsc8994012007-08-19 21:56:03 +00001710
1711 return 0;
1712}
1713
1714static void win32_stop_timer(struct qemu_alarm_timer *t)
1715{
1716 struct qemu_alarm_win32 *data = t->priv;
1717
1718 timeKillEvent(data->timerId);
1719 timeEndPeriod(data->period);
1720
1721 CloseHandle(data->host_alarm);
1722}
1723
thsefe75412007-08-24 01:36:32 +00001724static void win32_rearm_timer(struct qemu_alarm_timer *t)
1725{
1726 struct qemu_alarm_win32 *data = t->priv;
1727 uint64_t nearest_delta_us;
1728
1729 if (!active_timers[QEMU_TIMER_REALTIME] &&
1730 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001731 return;
thsefe75412007-08-24 01:36:32 +00001732
pbrook2e70f6e2008-06-29 01:03:05 +00001733 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001734 nearest_delta_us /= 1000;
1735
1736 timeKillEvent(data->timerId);
1737
1738 data->timerId = timeSetEvent(1,
1739 data->period,
1740 host_alarm_handler,
1741 (DWORD)t,
1742 TIME_ONESHOT | TIME_PERIODIC);
1743
1744 if (!data->timerId) {
1745 perror("Failed to re-arm win32 alarm timer");
1746
1747 timeEndPeriod(data->period);
1748 CloseHandle(data->host_alarm);
1749 exit(1);
1750 }
1751}
1752
thsc8994012007-08-19 21:56:03 +00001753#endif /* _WIN32 */
1754
aliguori7183b4b2008-11-05 20:40:18 +00001755static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001756{
blueswir1223f0d72008-09-30 18:12:18 +00001757 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001758 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001759
1760#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +00001761 int fds[2];
1762
aliguori7183b4b2008-11-05 20:40:18 +00001763 err = pipe(fds);
1764 if (err == -1)
1765 return -errno;
1766
1767 err = fcntl_setfl(fds[0], O_NONBLOCK);
1768 if (err < 0)
1769 goto fail;
1770
1771 err = fcntl_setfl(fds[1], O_NONBLOCK);
1772 if (err < 0)
1773 goto fail;
1774
aliguoric96f1a42008-11-05 20:29:45 +00001775 alarm_timer_rfd = fds[0];
1776 alarm_timer_wfd = fds[1];
aliguorif49e58d2008-11-05 21:22:34 +00001777#endif
bellard06d9f2f2006-05-01 13:23:04 +00001778
thsc8994012007-08-19 21:56:03 +00001779 for (i = 0; alarm_timers[i].name; i++) {
1780 t = &alarm_timers[i];
1781
thsc8994012007-08-19 21:56:03 +00001782 err = t->start(t);
1783 if (!err)
1784 break;
bellard67b915a2004-03-31 23:37:16 +00001785 }
bellardfd872592004-05-12 19:11:15 +00001786
thsc8994012007-08-19 21:56:03 +00001787 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001788 err = -ENOENT;
1789 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001790 }
thsc8994012007-08-19 21:56:03 +00001791
aliguorif49e58d2008-11-05 21:22:34 +00001792#ifndef _WIN32
aliguori6abfbd72008-11-05 20:49:37 +00001793 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1794 try_to_rearm_timer, NULL, t);
aliguorif49e58d2008-11-05 21:22:34 +00001795#endif
aliguori6abfbd72008-11-05 20:49:37 +00001796
thsc8994012007-08-19 21:56:03 +00001797 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001798
aliguori6abfbd72008-11-05 20:49:37 +00001799 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001800
1801fail:
aliguorif49e58d2008-11-05 21:22:34 +00001802#ifndef _WIN32
aliguori7183b4b2008-11-05 20:40:18 +00001803 close(fds[0]);
1804 close(fds[1]);
aliguorif49e58d2008-11-05 21:22:34 +00001805#endif
aliguori7183b4b2008-11-05 20:40:18 +00001806 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001807}
1808
pbrook9596ebb2007-11-18 01:44:38 +00001809static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001810{
thsc8994012007-08-19 21:56:03 +00001811 alarm_timer->stop(alarm_timer);
1812 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001813}
1814
bellardc4b1fcc2004-03-14 21:44:30 +00001815/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001816/* host time/date access */
1817void qemu_get_timedate(struct tm *tm, int offset)
1818{
1819 time_t ti;
1820 struct tm *ret;
1821
1822 time(&ti);
1823 ti += offset;
1824 if (rtc_date_offset == -1) {
1825 if (rtc_utc)
1826 ret = gmtime(&ti);
1827 else
1828 ret = localtime(&ti);
1829 } else {
1830 ti -= rtc_date_offset;
1831 ret = gmtime(&ti);
1832 }
1833
1834 memcpy(tm, ret, sizeof(struct tm));
1835}
1836
1837int qemu_timedate_diff(struct tm *tm)
1838{
1839 time_t seconds;
1840
1841 if (rtc_date_offset == -1)
1842 if (rtc_utc)
1843 seconds = mktimegm(tm);
1844 else
1845 seconds = mktime(tm);
1846 else
1847 seconds = mktimegm(tm) + rtc_date_offset;
1848
1849 return seconds - time(NULL);
1850}
1851
bellardfd1dff42006-02-01 21:29:26 +00001852#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001853static void socket_cleanup(void)
1854{
1855 WSACleanup();
1856}
bellard82c643f2004-07-14 17:28:13 +00001857
bellardfd1dff42006-02-01 21:29:26 +00001858static int socket_init(void)
1859{
1860 WSADATA Data;
1861 int ret, err;
1862
1863 ret = WSAStartup(MAKEWORD(2,2), &Data);
1864 if (ret != 0) {
1865 err = WSAGetLastError();
1866 fprintf(stderr, "WSAStartup: %d\n", err);
1867 return -1;
1868 }
1869 atexit(socket_cleanup);
1870 return 0;
1871}
aurel3264b7b732008-05-05 10:05:31 +00001872#endif
1873
aliguori268a3622009-04-21 22:30:27 +00001874const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
thse4bcb142007-12-02 04:51:10 +00001875{
1876 char *q;
thse4bcb142007-12-02 04:51:10 +00001877
balrog609497a2008-01-14 02:56:53 +00001878 q = buf;
aliguori268a3622009-04-21 22:30:27 +00001879 while (*p != '\0' && *p != delim) {
balrog609497a2008-01-14 02:56:53 +00001880 if (q && (q - buf) < buf_size - 1)
1881 *q++ = *p;
1882 p++;
1883 }
1884 if (q)
1885 *q = '\0';
1886
1887 return p;
1888}
1889
aliguori63a01ef2008-10-31 19:10:00 +00001890const char *get_opt_value(char *buf, int buf_size, const char *p)
balrog609497a2008-01-14 02:56:53 +00001891{
1892 char *q;
1893
thse4bcb142007-12-02 04:51:10 +00001894 q = buf;
1895 while (*p != '\0') {
balrog609497a2008-01-14 02:56:53 +00001896 if (*p == ',') {
1897 if (*(p + 1) != ',')
thse4bcb142007-12-02 04:51:10 +00001898 break;
thse4bcb142007-12-02 04:51:10 +00001899 p++;
balrog609497a2008-01-14 02:56:53 +00001900 }
thse4bcb142007-12-02 04:51:10 +00001901 if (q && (q - buf) < buf_size - 1)
1902 *q++ = *p;
1903 p++;
1904 }
1905 if (q)
1906 *q = '\0';
1907
1908 return p;
1909}
1910
aliguori63a01ef2008-10-31 19:10:00 +00001911int get_param_value(char *buf, int buf_size,
1912 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001913{
1914 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001915 char option[128];
1916
1917 p = str;
1918 for(;;) {
aliguori268a3622009-04-21 22:30:27 +00001919 p = get_opt_name(option, sizeof(option), p, '=');
bellard7c9d8e02005-11-15 22:16:05 +00001920 if (*p != '=')
1921 break;
1922 p++;
1923 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001924 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001925 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001926 } else {
balrog609497a2008-01-14 02:56:53 +00001927 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001928 }
1929 if (*p != ',')
1930 break;
1931 p++;
1932 }
1933 return 0;
1934}
1935
aliguori63a01ef2008-10-31 19:10:00 +00001936int check_params(char *buf, int buf_size,
1937 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001938{
1939 const char *p;
1940 int i;
1941
1942 p = str;
aliguori10300212009-04-21 19:56:23 +00001943 while (*p != '\0') {
aliguori268a3622009-04-21 22:30:27 +00001944 p = get_opt_name(buf, buf_size, p, '=');
thse4bcb142007-12-02 04:51:10 +00001945 if (*p != '=')
1946 return -1;
1947 p++;
1948 for(i = 0; params[i] != NULL; i++)
1949 if (!strcmp(params[i], buf))
1950 break;
1951 if (params[i] == NULL)
1952 return -1;
balrog609497a2008-01-14 02:56:53 +00001953 p = get_opt_value(NULL, 0, p);
thse4bcb142007-12-02 04:51:10 +00001954 if (*p != ',')
1955 break;
1956 p++;
1957 }
1958 return 0;
1959}
1960
balrog1ae26a12008-09-28 23:19:47 +00001961/***********************************************************/
1962/* Bluetooth support */
1963static int nb_hcis;
1964static int cur_hci;
1965static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001966
balrog1ae26a12008-09-28 23:19:47 +00001967static struct bt_vlan_s {
1968 struct bt_scatternet_s net;
1969 int id;
1970 struct bt_vlan_s *next;
1971} *first_bt_vlan;
1972
1973/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001974static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001975{
1976 struct bt_vlan_s **pvlan, *vlan;
1977 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1978 if (vlan->id == id)
1979 return &vlan->net;
1980 }
1981 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1982 vlan->id = id;
1983 pvlan = &first_bt_vlan;
1984 while (*pvlan != NULL)
1985 pvlan = &(*pvlan)->next;
1986 *pvlan = vlan;
1987 return &vlan->net;
1988}
1989
1990static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1991{
1992}
1993
1994static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1995{
1996 return -ENOTSUP;
1997}
1998
1999static struct HCIInfo null_hci = {
2000 .cmd_send = null_hci_send,
2001 .sco_send = null_hci_send,
2002 .acl_send = null_hci_send,
2003 .bdaddr_set = null_hci_addr_set,
2004};
2005
2006struct HCIInfo *qemu_next_hci(void)
2007{
2008 if (cur_hci == nb_hcis)
2009 return &null_hci;
2010
2011 return hci_table[cur_hci++];
2012}
2013
balrogdc72ac12008-11-09 00:04:26 +00002014static struct HCIInfo *hci_init(const char *str)
2015{
2016 char *endp;
2017 struct bt_scatternet_s *vlan = 0;
2018
2019 if (!strcmp(str, "null"))
2020 /* null */
2021 return &null_hci;
2022 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
2023 /* host[:hciN] */
2024 return bt_host_hci(str[4] ? str + 5 : "hci0");
2025 else if (!strncmp(str, "hci", 3)) {
2026 /* hci[,vlan=n] */
2027 if (str[3]) {
2028 if (!strncmp(str + 3, ",vlan=", 6)) {
2029 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2030 if (*endp)
2031 vlan = 0;
2032 }
2033 } else
2034 vlan = qemu_find_bt_vlan(0);
2035 if (vlan)
2036 return bt_new_hci(vlan);
2037 }
2038
2039 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2040
2041 return 0;
2042}
2043
2044static int bt_hci_parse(const char *str)
2045{
2046 struct HCIInfo *hci;
2047 bdaddr_t bdaddr;
2048
2049 if (nb_hcis >= MAX_NICS) {
2050 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2051 return -1;
2052 }
2053
2054 hci = hci_init(str);
2055 if (!hci)
2056 return -1;
2057
2058 bdaddr.b[0] = 0x52;
2059 bdaddr.b[1] = 0x54;
2060 bdaddr.b[2] = 0x00;
2061 bdaddr.b[3] = 0x12;
2062 bdaddr.b[4] = 0x34;
2063 bdaddr.b[5] = 0x56 + nb_hcis;
2064 hci->bdaddr_set(hci, bdaddr.b);
2065
2066 hci_table[nb_hcis++] = hci;
2067
2068 return 0;
2069}
2070
2071static void bt_vhci_add(int vlan_id)
2072{
2073 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2074
2075 if (!vlan->slave)
2076 fprintf(stderr, "qemu: warning: adding a VHCI to "
2077 "an empty scatternet %i\n", vlan_id);
2078
2079 bt_vhci_init(bt_new_hci(vlan));
2080}
2081
2082static struct bt_device_s *bt_device_add(const char *opt)
2083{
2084 struct bt_scatternet_s *vlan;
2085 int vlan_id = 0;
2086 char *endp = strstr(opt, ",vlan=");
2087 int len = (endp ? endp - opt : strlen(opt)) + 1;
2088 char devname[10];
2089
2090 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2091
2092 if (endp) {
2093 vlan_id = strtol(endp + 6, &endp, 0);
2094 if (*endp) {
2095 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2096 return 0;
2097 }
2098 }
2099
2100 vlan = qemu_find_bt_vlan(vlan_id);
2101
2102 if (!vlan->slave)
2103 fprintf(stderr, "qemu: warning: adding a slave device to "
2104 "an empty scatternet %i\n", vlan_id);
2105
2106 if (!strcmp(devname, "keyboard"))
2107 return bt_keyboard_init(vlan);
2108
2109 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2110 return 0;
2111}
2112
2113static int bt_parse(const char *opt)
2114{
2115 const char *endp, *p;
2116 int vlan;
2117
2118 if (strstart(opt, "hci", &endp)) {
2119 if (!*endp || *endp == ',') {
2120 if (*endp)
2121 if (!strstart(endp, ",vlan=", 0))
2122 opt = endp + 1;
2123
2124 return bt_hci_parse(opt);
2125 }
2126 } else if (strstart(opt, "vhci", &endp)) {
2127 if (!*endp || *endp == ',') {
2128 if (*endp) {
2129 if (strstart(endp, ",vlan=", &p)) {
2130 vlan = strtol(p, (char **) &endp, 0);
2131 if (*endp) {
2132 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2133 return 1;
2134 }
2135 } else {
2136 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2137 return 1;
2138 }
2139 } else
2140 vlan = 0;
2141
2142 bt_vhci_add(vlan);
2143 return 0;
2144 }
2145 } else if (strstart(opt, "device:", &endp))
2146 return !bt_device_add(endp);
2147
2148 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2149 return 1;
2150}
2151
balrog1ae26a12008-09-28 23:19:47 +00002152/***********************************************************/
2153/* QEMU Block devices */
2154
balrog609497a2008-01-14 02:56:53 +00002155#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00002156#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00002157#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00002158#define PFLASH_ALIAS "if=pflash"
2159#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00002160#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002161
aliguori7d5aca92009-02-11 15:19:58 +00002162static int drive_opt_get_free_idx(void)
2163{
2164 int index;
2165
2166 for (index = 0; index < MAX_DRIVES; index++)
2167 if (!drives_opt[index].used) {
2168 drives_opt[index].used = 1;
2169 return index;
2170 }
2171
2172 return -1;
2173}
2174
2175static int drive_get_free_idx(void)
2176{
2177 int index;
2178
2179 for (index = 0; index < MAX_DRIVES; index++)
2180 if (!drives_table[index].used) {
2181 drives_table[index].used = 1;
2182 return index;
2183 }
2184
2185 return -1;
2186}
2187
aliguori4d73cd32009-02-11 15:20:46 +00002188int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002189{
2190 va_list ap;
aliguori7d5aca92009-02-11 15:19:58 +00002191 int index = drive_opt_get_free_idx();
thse4bcb142007-12-02 04:51:10 +00002192
aliguori7d5aca92009-02-11 15:19:58 +00002193 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
thse4bcb142007-12-02 04:51:10 +00002194 fprintf(stderr, "qemu: too many drives\n");
aliguori4d73cd32009-02-11 15:20:46 +00002195 return -1;
thse4bcb142007-12-02 04:51:10 +00002196 }
2197
aliguori7d5aca92009-02-11 15:19:58 +00002198 drives_opt[index].file = file;
thse4bcb142007-12-02 04:51:10 +00002199 va_start(ap, fmt);
aliguori7d5aca92009-02-11 15:19:58 +00002200 vsnprintf(drives_opt[index].opt,
balrog609497a2008-01-14 02:56:53 +00002201 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002202 va_end(ap);
2203
aliguori7d5aca92009-02-11 15:19:58 +00002204 nb_drives_opt++;
2205 return index;
thse4bcb142007-12-02 04:51:10 +00002206}
2207
aliguorib01b1112009-02-11 15:20:20 +00002208void drive_remove(int index)
2209{
2210 drives_opt[index].used = 0;
2211 nb_drives_opt--;
2212}
2213
thsf60d39b2007-12-17 03:55:57 +00002214int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002215{
2216 int index;
2217
2218 /* seek interface, bus and unit */
2219
aliguori7d5aca92009-02-11 15:19:58 +00002220 for (index = 0; index < MAX_DRIVES; index++)
thsf60d39b2007-12-17 03:55:57 +00002221 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002222 drives_table[index].bus == bus &&
aliguori7d5aca92009-02-11 15:19:58 +00002223 drives_table[index].unit == unit &&
2224 drives_table[index].used)
thse4bcb142007-12-02 04:51:10 +00002225 return index;
2226
2227 return -1;
2228}
2229
thsf60d39b2007-12-17 03:55:57 +00002230int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002231{
2232 int max_bus;
2233 int index;
2234
2235 max_bus = -1;
2236 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002237 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002238 drives_table[index].bus > max_bus)
2239 max_bus = drives_table[index].bus;
2240 }
2241 return max_bus;
2242}
2243
aliguorifa879c62009-01-07 17:32:33 +00002244const char *drive_get_serial(BlockDriverState *bdrv)
2245{
2246 int index;
2247
2248 for (index = 0; index < nb_drives; index++)
2249 if (drives_table[index].bdrv == bdrv)
2250 return drives_table[index].serial;
2251
2252 return "\0";
2253}
2254
aliguori428c5702009-01-21 18:59:04 +00002255BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2256{
2257 int index;
2258
2259 for (index = 0; index < nb_drives; index++)
2260 if (drives_table[index].bdrv == bdrv)
2261 return drives_table[index].onerror;
2262
aliguoricdad4bd2009-02-28 16:51:01 +00002263 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002264}
2265
aurel32a1620fa2008-04-29 05:58:01 +00002266static void bdrv_format_print(void *opaque, const char *name)
2267{
2268 fprintf(stderr, " %s", name);
2269}
2270
aliguorib01b1112009-02-11 15:20:20 +00002271void drive_uninit(BlockDriverState *bdrv)
2272{
2273 int i;
2274
2275 for (i = 0; i < MAX_DRIVES; i++)
2276 if (drives_table[i].bdrv == bdrv) {
2277 drives_table[i].bdrv = NULL;
2278 drives_table[i].used = 0;
2279 drive_remove(drives_table[i].drive_opt_idx);
2280 nb_drives--;
2281 break;
2282 }
2283}
2284
aliguori4d73cd32009-02-11 15:20:46 +00002285int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
thse4bcb142007-12-02 04:51:10 +00002286{
2287 char buf[128];
2288 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002289 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00002290 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002291 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002292 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002293 enum { MEDIA_DISK, MEDIA_CDROM } media;
2294 int bus_id, unit_id;
2295 int cyls, heads, secs, translation;
2296 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002297 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002298 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002299 int max_devs;
2300 int index;
balrog33f00272007-12-24 14:33:24 +00002301 int cache;
aliguori428c5702009-01-21 18:59:04 +00002302 int bdrv_flags, onerror;
aliguori7d5aca92009-02-11 15:19:58 +00002303 int drives_table_idx;
balrog609497a2008-01-14 02:56:53 +00002304 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002305 static const char * const params[] = { "bus", "unit", "if", "index",
2306 "cyls", "heads", "secs", "trans",
2307 "media", "snapshot", "file",
aliguori428c5702009-01-21 18:59:04 +00002308 "cache", "format", "serial", "werror",
2309 NULL };
thse4bcb142007-12-02 04:51:10 +00002310
2311 if (check_params(buf, sizeof(buf), params, str) < 0) {
balrogff993632008-02-10 13:21:25 +00002312 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
thse4bcb142007-12-02 04:51:10 +00002313 buf, str);
2314 return -1;
2315 }
2316
2317 file[0] = 0;
2318 cyls = heads = secs = 0;
2319 bus_id = 0;
2320 unit_id = -1;
2321 translation = BIOS_ATA_TRANSLATION_AUTO;
2322 index = -1;
aliguori4dc822d2008-12-04 21:39:21 +00002323 cache = 3;
thse4bcb142007-12-02 04:51:10 +00002324
blueswir1c9b1ae22008-09-28 18:55:17 +00002325 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002326 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002327 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002328 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002329 } else {
thsf60d39b2007-12-17 03:55:57 +00002330 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002331 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002332 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002333 }
2334 media = MEDIA_DISK;
2335
2336 /* extract parameters */
2337
2338 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2339 bus_id = strtol(buf, NULL, 0);
2340 if (bus_id < 0) {
2341 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2342 return -1;
2343 }
2344 }
2345
2346 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2347 unit_id = strtol(buf, NULL, 0);
2348 if (unit_id < 0) {
2349 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2350 return -1;
2351 }
2352 }
2353
2354 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002355 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002356 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002357 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002358 max_devs = MAX_IDE_DEVS;
2359 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002360 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002361 max_devs = MAX_SCSI_DEVS;
2362 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002363 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002364 max_devs = 0;
2365 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002366 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002367 max_devs = 0;
2368 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002369 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002370 max_devs = 0;
2371 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002372 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002373 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002374 } else if (!strcmp(buf, "virtio")) {
2375 type = IF_VIRTIO;
2376 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002377 } else if (!strcmp(buf, "xen")) {
2378 type = IF_XEN;
2379 max_devs = 0;
2380 } else {
thse4bcb142007-12-02 04:51:10 +00002381 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2382 return -1;
2383 }
2384 }
2385
2386 if (get_param_value(buf, sizeof(buf), "index", str)) {
2387 index = strtol(buf, NULL, 0);
2388 if (index < 0) {
2389 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2390 return -1;
2391 }
2392 }
2393
2394 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2395 cyls = strtol(buf, NULL, 0);
2396 }
2397
2398 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2399 heads = strtol(buf, NULL, 0);
2400 }
2401
2402 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2403 secs = strtol(buf, NULL, 0);
2404 }
2405
2406 if (cyls || heads || secs) {
2407 if (cyls < 1 || cyls > 16383) {
2408 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2409 return -1;
2410 }
2411 if (heads < 1 || heads > 16) {
2412 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2413 return -1;
2414 }
2415 if (secs < 1 || secs > 63) {
2416 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2417 return -1;
2418 }
2419 }
2420
2421 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2422 if (!cyls) {
2423 fprintf(stderr,
2424 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2425 str);
2426 return -1;
2427 }
2428 if (!strcmp(buf, "none"))
2429 translation = BIOS_ATA_TRANSLATION_NONE;
2430 else if (!strcmp(buf, "lba"))
2431 translation = BIOS_ATA_TRANSLATION_LBA;
2432 else if (!strcmp(buf, "auto"))
2433 translation = BIOS_ATA_TRANSLATION_AUTO;
2434 else {
2435 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2436 return -1;
2437 }
2438 }
2439
2440 if (get_param_value(buf, sizeof(buf), "media", str)) {
2441 if (!strcmp(buf, "disk")) {
2442 media = MEDIA_DISK;
2443 } else if (!strcmp(buf, "cdrom")) {
2444 if (cyls || secs || heads) {
2445 fprintf(stderr,
2446 "qemu: '%s' invalid physical CHS format\n", str);
2447 return -1;
2448 }
2449 media = MEDIA_CDROM;
2450 } else {
2451 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2452 return -1;
2453 }
2454 }
2455
2456 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2457 if (!strcmp(buf, "on"))
2458 snapshot = 1;
2459 else if (!strcmp(buf, "off"))
2460 snapshot = 0;
2461 else {
2462 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2463 return -1;
2464 }
2465 }
2466
balrog33f00272007-12-24 14:33:24 +00002467 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002468 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002469 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002470 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002471 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002472 else if (!strcmp(buf, "writeback"))
2473 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002474 else {
2475 fprintf(stderr, "qemu: invalid cache option\n");
2476 return -1;
2477 }
2478 }
2479
aurel321e72d3b2008-04-28 20:26:45 +00002480 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002481 if (strcmp(buf, "?") == 0) {
2482 fprintf(stderr, "qemu: Supported formats:");
2483 bdrv_iterate_format(bdrv_format_print, NULL);
2484 fprintf(stderr, "\n");
2485 return -1;
2486 }
aurel321e72d3b2008-04-28 20:26:45 +00002487 drv = bdrv_find_format(buf);
2488 if (!drv) {
2489 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2490 return -1;
2491 }
2492 }
2493
balrog609497a2008-01-14 02:56:53 +00002494 if (arg->file == NULL)
2495 get_param_value(file, sizeof(file), "file", str);
2496 else
2497 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002498
aliguorifa879c62009-01-07 17:32:33 +00002499 if (!get_param_value(serial, sizeof(serial), "serial", str))
2500 memset(serial, 0, sizeof(serial));
2501
aliguoricdad4bd2009-02-28 16:51:01 +00002502 onerror = BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002503 if (get_param_value(buf, sizeof(serial), "werror", str)) {
aliguori869a5c62009-01-22 19:52:25 +00002504 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002505 fprintf(stderr, "werror is no supported by this format\n");
aliguori428c5702009-01-21 18:59:04 +00002506 return -1;
2507 }
2508 if (!strcmp(buf, "ignore"))
2509 onerror = BLOCK_ERR_IGNORE;
2510 else if (!strcmp(buf, "enospc"))
2511 onerror = BLOCK_ERR_STOP_ENOSPC;
2512 else if (!strcmp(buf, "stop"))
2513 onerror = BLOCK_ERR_STOP_ANY;
2514 else if (!strcmp(buf, "report"))
2515 onerror = BLOCK_ERR_REPORT;
2516 else {
2517 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2518 return -1;
2519 }
2520 }
2521
thse4bcb142007-12-02 04:51:10 +00002522 /* compute bus and unit according index */
2523
2524 if (index != -1) {
2525 if (bus_id != 0 || unit_id != -1) {
2526 fprintf(stderr,
2527 "qemu: '%s' index cannot be used with bus and unit\n", str);
2528 return -1;
2529 }
2530 if (max_devs == 0)
2531 {
2532 unit_id = index;
2533 bus_id = 0;
2534 } else {
2535 unit_id = index % max_devs;
2536 bus_id = index / max_devs;
2537 }
2538 }
2539
2540 /* if user doesn't specify a unit_id,
2541 * try to find the first free
2542 */
2543
2544 if (unit_id == -1) {
2545 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002546 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002547 unit_id++;
2548 if (max_devs && unit_id >= max_devs) {
2549 unit_id -= max_devs;
2550 bus_id++;
2551 }
2552 }
2553 }
2554
2555 /* check unit id */
2556
2557 if (max_devs && unit_id >= max_devs) {
2558 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2559 str, unit_id, max_devs - 1);
2560 return -1;
2561 }
2562
2563 /*
2564 * ignore multiple definitions
2565 */
2566
thsf60d39b2007-12-17 03:55:57 +00002567 if (drive_get_index(type, bus_id, unit_id) != -1)
aliguori4d73cd32009-02-11 15:20:46 +00002568 return -2;
thse4bcb142007-12-02 04:51:10 +00002569
2570 /* init */
2571
thsf60d39b2007-12-17 03:55:57 +00002572 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002573 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002574 if (max_devs)
2575 snprintf(buf, sizeof(buf), "%s%i%s%i",
2576 devname, bus_id, mediastr, unit_id);
2577 else
2578 snprintf(buf, sizeof(buf), "%s%s%i",
2579 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002580 bdrv = bdrv_new(buf);
aliguori7d5aca92009-02-11 15:19:58 +00002581 drives_table_idx = drive_get_free_idx();
2582 drives_table[drives_table_idx].bdrv = bdrv;
2583 drives_table[drives_table_idx].type = type;
2584 drives_table[drives_table_idx].bus = bus_id;
2585 drives_table[drives_table_idx].unit = unit_id;
2586 drives_table[drives_table_idx].onerror = onerror;
aliguorib01b1112009-02-11 15:20:20 +00002587 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
aliguorifa879c62009-01-07 17:32:33 +00002588 strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002589 nb_drives++;
2590
thsf60d39b2007-12-17 03:55:57 +00002591 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002592 case IF_IDE:
2593 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002594 case IF_XEN:
thse4bcb142007-12-02 04:51:10 +00002595 switch(media) {
2596 case MEDIA_DISK:
2597 if (cyls != 0) {
2598 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2599 bdrv_set_translation_hint(bdrv, translation);
2600 }
2601 break;
2602 case MEDIA_CDROM:
2603 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2604 break;
2605 }
2606 break;
2607 case IF_SD:
2608 /* FIXME: This isn't really a floppy, but it's a reasonable
2609 approximation. */
2610 case IF_FLOPPY:
2611 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2612 break;
2613 case IF_PFLASH:
2614 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002615 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002616 break;
2617 }
2618 if (!file[0])
aliguori4d73cd32009-02-11 15:20:46 +00002619 return -2;
balrog33f00272007-12-24 14:33:24 +00002620 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002621 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002622 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002623 cache = 2; /* always use write-back with snapshot */
2624 }
2625 if (cache == 0) /* no caching */
2626 bdrv_flags |= BDRV_O_NOCACHE;
2627 else if (cache == 2) /* write-back */
2628 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori4dc822d2008-12-04 21:39:21 +00002629 else if (cache == 3) /* not specified */
2630 bdrv_flags |= BDRV_O_CACHE_DEF;
aliguoric0f4ce72009-03-05 23:01:01 +00002631 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002632 fprintf(stderr, "qemu: could not open disk image %s\n",
2633 file);
2634 return -1;
2635 }
aliguoric0f4ce72009-03-05 23:01:01 +00002636 if (bdrv_key_required(bdrv))
2637 autostart = 0;
aliguori4d73cd32009-02-11 15:20:46 +00002638 return drives_table_idx;
thse4bcb142007-12-02 04:51:10 +00002639}
2640
aliguori268a3622009-04-21 22:30:27 +00002641static void numa_add(const char *optarg)
2642{
2643 char option[128];
2644 char *endptr;
2645 unsigned long long value, endvalue;
2646 int nodenr;
2647
2648 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2649 if (!strcmp(option, "node")) {
2650 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2651 nodenr = nb_numa_nodes;
2652 } else {
2653 nodenr = strtoull(option, NULL, 10);
2654 }
2655
2656 if (get_param_value(option, 128, "mem", optarg) == 0) {
2657 node_mem[nodenr] = 0;
2658 } else {
2659 value = strtoull(option, &endptr, 0);
2660 switch (*endptr) {
2661 case 0: case 'M': case 'm':
2662 value <<= 20;
2663 break;
2664 case 'G': case 'g':
2665 value <<= 30;
2666 break;
2667 }
2668 node_mem[nodenr] = value;
2669 }
2670 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2671 node_cpumask[nodenr] = 0;
2672 } else {
2673 value = strtoull(option, &endptr, 10);
2674 if (value >= 64) {
2675 value = 63;
2676 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2677 } else {
2678 if (*endptr == '-') {
2679 endvalue = strtoull(endptr+1, &endptr, 10);
2680 if (endvalue >= 63) {
2681 endvalue = 62;
2682 fprintf(stderr,
2683 "only 63 CPUs in NUMA mode supported.\n");
2684 }
2685 value = (1 << (endvalue + 1)) - (1 << value);
2686 } else {
2687 value = 1 << value;
2688 }
2689 }
2690 node_cpumask[nodenr] = value;
2691 }
2692 nb_numa_nodes++;
2693 }
2694 return;
2695}
2696
bellard330d0412003-07-26 18:11:40 +00002697/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002698/* USB devices */
2699
pbrook0d92ed32006-05-21 16:30:15 +00002700static USBPort *used_usb_ports;
2701static USBPort *free_usb_ports;
2702
2703/* ??? Maybe change this to register a hub to keep track of the topology. */
2704void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2705 usb_attachfn attach)
2706{
2707 port->opaque = opaque;
2708 port->index = index;
2709 port->attach = attach;
2710 port->next = free_usb_ports;
2711 free_usb_ports = port;
2712}
2713
aliguori4b096fc2008-08-21 19:28:55 +00002714int usb_device_add_dev(USBDevice *dev)
2715{
2716 USBPort *port;
2717
2718 /* Find a USB port to add the device to. */
2719 port = free_usb_ports;
2720 if (!port->next) {
2721 USBDevice *hub;
2722
2723 /* Create a new hub and chain it on. */
2724 free_usb_ports = NULL;
2725 port->next = used_usb_ports;
2726 used_usb_ports = port;
2727
2728 hub = usb_hub_init(VM_USB_HUB_SIZE);
2729 usb_attach(port, hub);
2730 port = free_usb_ports;
2731 }
2732
2733 free_usb_ports = port->next;
2734 port->next = used_usb_ports;
2735 used_usb_ports = port;
2736 usb_attach(port, dev);
2737 return 0;
2738}
2739
aliguoribb5fc202009-03-05 23:01:15 +00002740static void usb_msd_password_cb(void *opaque, int err)
2741{
2742 USBDevice *dev = opaque;
2743
2744 if (!err)
2745 usb_device_add_dev(dev);
2746 else
2747 dev->handle_destroy(dev);
2748}
2749
aliguoric0f4ce72009-03-05 23:01:01 +00002750static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002751{
2752 const char *p;
2753 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002754
pbrook0d92ed32006-05-21 16:30:15 +00002755 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002756 return -1;
2757
2758 if (strstart(devname, "host:", &p)) {
2759 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002760 } else if (!strcmp(devname, "mouse")) {
2761 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002762 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002763 dev = usb_tablet_init();
2764 } else if (!strcmp(devname, "keyboard")) {
2765 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002766 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002767 BlockDriverState *bs;
2768
aliguoribb5fc202009-03-05 23:01:15 +00002769 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002770 if (!dev)
2771 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002772 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002773 if (bdrv_key_required(bs)) {
2774 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002775 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002776 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2777 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002778 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002779 }
2780 }
balrogf6d2a312007-06-10 19:21:04 +00002781 } else if (!strcmp(devname, "wacom-tablet")) {
2782 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002783 } else if (strstart(devname, "serial:", &p)) {
2784 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002785#ifdef CONFIG_BRLAPI
2786 } else if (!strcmp(devname, "braille")) {
2787 dev = usb_baum_init();
2788#endif
balrog6c9f8862008-07-17 20:47:13 +00002789 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002790 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002791
balrog9ad97e62008-07-29 13:16:31 +00002792 if (net_client_init("nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002793 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002794 nd_table[nic].model = "usb";
2795 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002796 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2797 dev = usb_bt_init(devname[2] ? hci_init(p) :
2798 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002799 } else {
2800 return -1;
2801 }
pbrook0d92ed32006-05-21 16:30:15 +00002802 if (!dev)
2803 return -1;
2804
aliguori4b096fc2008-08-21 19:28:55 +00002805 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002806}
2807
aliguori1f3870a2008-08-21 19:27:48 +00002808int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002809{
pbrook0d92ed32006-05-21 16:30:15 +00002810 USBPort *port;
2811 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002812 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002813
pbrook0d92ed32006-05-21 16:30:15 +00002814 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002815 return -1;
2816
bellarda594cfb2005-11-06 16:13:29 +00002817 if (bus_num != 0)
2818 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002819
2820 lastp = &used_usb_ports;
2821 port = used_usb_ports;
2822 while (port && port->dev->addr != addr) {
2823 lastp = &port->next;
2824 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002825 }
pbrook0d92ed32006-05-21 16:30:15 +00002826
2827 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002828 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002829
bellard059809e2006-07-19 18:06:15 +00002830 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002831 *lastp = port->next;
2832 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002833 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002834 port->next = free_usb_ports;
2835 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002836 return 0;
2837}
2838
aliguori1f3870a2008-08-21 19:27:48 +00002839static int usb_device_del(const char *devname)
2840{
2841 int bus_num, addr;
2842 const char *p;
2843
aliguori5d0c5752008-09-14 01:07:41 +00002844 if (strstart(devname, "host:", &p))
2845 return usb_host_device_close(p);
2846
aliguori1f3870a2008-08-21 19:27:48 +00002847 if (!used_usb_ports)
2848 return -1;
2849
2850 p = strchr(devname, '.');
2851 if (!p)
2852 return -1;
2853 bus_num = strtoul(devname, NULL, 0);
2854 addr = strtoul(p + 1, NULL, 0);
2855
2856 return usb_device_del_addr(bus_num, addr);
2857}
2858
aliguori376253e2009-03-05 23:01:23 +00002859void do_usb_add(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002860{
aliguoric0f4ce72009-03-05 23:01:01 +00002861 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002862}
2863
aliguori376253e2009-03-05 23:01:23 +00002864void do_usb_del(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002865{
aliguori4b096fc2008-08-21 19:28:55 +00002866 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002867}
2868
aliguori376253e2009-03-05 23:01:23 +00002869void usb_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00002870{
2871 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002872 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002873 const char *speed_str;
2874
pbrook0d92ed32006-05-21 16:30:15 +00002875 if (!usb_enabled) {
aliguori376253e2009-03-05 23:01:23 +00002876 monitor_printf(mon, "USB support not enabled\n");
bellarda594cfb2005-11-06 16:13:29 +00002877 return;
2878 }
2879
pbrook0d92ed32006-05-21 16:30:15 +00002880 for (port = used_usb_ports; port; port = port->next) {
2881 dev = port->dev;
2882 if (!dev)
2883 continue;
2884 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002885 case USB_SPEED_LOW:
2886 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002887 break;
ths5fafdf22007-09-16 21:08:06 +00002888 case USB_SPEED_FULL:
2889 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002890 break;
ths5fafdf22007-09-16 21:08:06 +00002891 case USB_SPEED_HIGH:
2892 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002893 break;
2894 default:
ths5fafdf22007-09-16 21:08:06 +00002895 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002896 break;
bellarda594cfb2005-11-06 16:13:29 +00002897 }
aliguori376253e2009-03-05 23:01:23 +00002898 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2899 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002900 }
2901}
2902
bellardf7cce892004-12-08 22:21:25 +00002903/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002904/* PCMCIA/Cardbus */
2905
2906static struct pcmcia_socket_entry_s {
2907 struct pcmcia_socket_s *socket;
2908 struct pcmcia_socket_entry_s *next;
2909} *pcmcia_sockets = 0;
2910
2911void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2912{
2913 struct pcmcia_socket_entry_s *entry;
2914
2915 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2916 entry->socket = socket;
2917 entry->next = pcmcia_sockets;
2918 pcmcia_sockets = entry;
2919}
2920
2921void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2922{
2923 struct pcmcia_socket_entry_s *entry, **ptr;
2924
2925 ptr = &pcmcia_sockets;
2926 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2927 if (entry->socket == socket) {
2928 *ptr = entry->next;
2929 qemu_free(entry);
2930 }
2931}
2932
aliguori376253e2009-03-05 23:01:23 +00002933void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002934{
2935 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002936
balrog201a51f2007-04-30 00:51:09 +00002937 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002938 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002939
2940 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002941 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2942 iter->socket->attached ? iter->socket->card_string :
2943 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002944}
2945
2946/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002947/* register display */
2948
aliguori7b5d76d2009-03-13 15:02:13 +00002949struct DisplayAllocator default_allocator = {
2950 defaultallocator_create_displaysurface,
2951 defaultallocator_resize_displaysurface,
2952 defaultallocator_free_displaysurface
2953};
2954
aliguori3023f332009-01-16 19:04:14 +00002955void register_displaystate(DisplayState *ds)
2956{
2957 DisplayState **s;
2958 s = &display_state;
2959 while (*s != NULL)
2960 s = &(*s)->next;
2961 ds->next = NULL;
2962 *s = ds;
2963}
2964
2965DisplayState *get_displaystate(void)
2966{
2967 return display_state;
2968}
2969
aliguori7b5d76d2009-03-13 15:02:13 +00002970DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2971{
2972 if(ds->allocator == &default_allocator) ds->allocator = da;
2973 return ds->allocator;
2974}
2975
ths2ff89792007-06-21 23:34:19 +00002976/* dumb display */
2977
aliguori8f391ab2009-01-19 16:34:10 +00002978static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002979{
aliguori8f391ab2009-01-19 16:34:10 +00002980 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002981 ds->allocator = &default_allocator;
2982 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002983 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002984}
2985
2986/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002987/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002988
bellardc4b1fcc2004-03-14 21:44:30 +00002989typedef struct IOHandlerRecord {
2990 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002991 IOCanRWHandler *fd_read_poll;
2992 IOHandler *fd_read;
2993 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002994 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002995 void *opaque;
2996 /* temporary data */
2997 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002998 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002999} IOHandlerRecord;
3000
bellard8a7ddc32004-03-31 19:00:16 +00003001static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00003002
bellard7c9d8e02005-11-15 22:16:05 +00003003/* XXX: fd_read_poll should be suppressed, but an API change is
3004 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00003005int qemu_set_fd_handler2(int fd,
3006 IOCanRWHandler *fd_read_poll,
3007 IOHandler *fd_read,
3008 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00003009 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00003010{
bellard8a7ddc32004-03-31 19:00:16 +00003011 IOHandlerRecord **pioh, *ioh;
3012
bellard7c9d8e02005-11-15 22:16:05 +00003013 if (!fd_read && !fd_write) {
3014 pioh = &first_io_handler;
3015 for(;;) {
3016 ioh = *pioh;
3017 if (ioh == NULL)
3018 break;
3019 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00003020 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00003021 break;
3022 }
3023 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00003024 }
bellard7c9d8e02005-11-15 22:16:05 +00003025 } else {
3026 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3027 if (ioh->fd == fd)
3028 goto found;
3029 }
3030 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00003031 ioh->next = first_io_handler;
3032 first_io_handler = ioh;
3033 found:
3034 ioh->fd = fd;
3035 ioh->fd_read_poll = fd_read_poll;
3036 ioh->fd_read = fd_read;
3037 ioh->fd_write = fd_write;
3038 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00003039 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00003040 }
bellard7c9d8e02005-11-15 22:16:05 +00003041 return 0;
3042}
3043
ths5fafdf22007-09-16 21:08:06 +00003044int qemu_set_fd_handler(int fd,
3045 IOHandler *fd_read,
3046 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00003047 void *opaque)
3048{
3049 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00003050}
3051
aliguori56f3a5d2008-10-31 18:07:17 +00003052#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00003053/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00003054/* Polling handling */
3055
3056typedef struct PollingEntry {
3057 PollingFunc *func;
3058 void *opaque;
3059 struct PollingEntry *next;
3060} PollingEntry;
3061
3062static PollingEntry *first_polling_entry;
3063
3064int qemu_add_polling_cb(PollingFunc *func, void *opaque)
3065{
3066 PollingEntry **ppe, *pe;
3067 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00003068 pe->func = func;
3069 pe->opaque = opaque;
3070 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
3071 *ppe = pe;
3072 return 0;
3073}
3074
3075void qemu_del_polling_cb(PollingFunc *func, void *opaque)
3076{
3077 PollingEntry **ppe, *pe;
3078 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
3079 pe = *ppe;
3080 if (pe->func == func && pe->opaque == opaque) {
3081 *ppe = pe->next;
3082 qemu_free(pe);
3083 break;
3084 }
3085 }
3086}
3087
bellarda18e5242006-06-25 17:18:27 +00003088/***********************************************************/
3089/* Wait objects support */
3090typedef struct WaitObjects {
3091 int num;
3092 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
3093 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
3094 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
3095} WaitObjects;
3096
3097static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00003098
bellarda18e5242006-06-25 17:18:27 +00003099int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3100{
3101 WaitObjects *w = &wait_objects;
3102
3103 if (w->num >= MAXIMUM_WAIT_OBJECTS)
3104 return -1;
3105 w->events[w->num] = handle;
3106 w->func[w->num] = func;
3107 w->opaque[w->num] = opaque;
3108 w->num++;
3109 return 0;
3110}
3111
3112void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3113{
3114 int i, found;
3115 WaitObjects *w = &wait_objects;
3116
3117 found = 0;
3118 for (i = 0; i < w->num; i++) {
3119 if (w->events[i] == handle)
3120 found = 1;
3121 if (found) {
3122 w->events[i] = w->events[i + 1];
3123 w->func[i] = w->func[i + 1];
3124 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00003125 }
bellarda18e5242006-06-25 17:18:27 +00003126 }
3127 if (found)
3128 w->num--;
3129}
3130#endif
3131
bellard8a7ddc32004-03-31 19:00:16 +00003132/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003133/* ram save/restore */
3134
bellard8a7ddc32004-03-31 19:00:16 +00003135static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3136{
3137 int v;
3138
3139 v = qemu_get_byte(f);
3140 switch(v) {
3141 case 0:
3142 if (qemu_get_buffer(f, buf, len) != len)
3143 return -EIO;
3144 break;
3145 case 1:
3146 v = qemu_get_byte(f);
3147 memset(buf, v, len);
3148 break;
3149 default:
3150 return -EINVAL;
3151 }
aliguori871d2f02008-10-13 03:07:56 +00003152
3153 if (qemu_file_has_error(f))
3154 return -EIO;
3155
bellard8a7ddc32004-03-31 19:00:16 +00003156 return 0;
3157}
3158
bellardc88676f2006-08-06 13:36:11 +00003159static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003160{
aurel3200f82b82008-04-27 21:12:55 +00003161 int ret;
3162 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00003163
pbrook94a6b542009-04-11 17:15:54 +00003164 if (qemu_get_be32(f) != last_ram_offset)
bellard8a7ddc32004-03-31 19:00:16 +00003165 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003166 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
pbrook5579c7f2009-04-11 14:47:08 +00003167 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
bellard8a7ddc32004-03-31 19:00:16 +00003168 if (ret)
3169 return ret;
3170 }
3171 return 0;
3172}
3173
bellardc88676f2006-08-06 13:36:11 +00003174#define BDRV_HASH_BLOCK_SIZE 1024
3175#define IOBUF_SIZE 4096
3176#define RAM_CBLOCK_MAGIC 0xfabe
3177
bellardc88676f2006-08-06 13:36:11 +00003178typedef struct RamDecompressState {
3179 z_stream zstream;
3180 QEMUFile *f;
3181 uint8_t buf[IOBUF_SIZE];
3182} RamDecompressState;
3183
3184static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3185{
3186 int ret;
3187 memset(s, 0, sizeof(*s));
3188 s->f = f;
3189 ret = inflateInit(&s->zstream);
3190 if (ret != Z_OK)
3191 return -1;
3192 return 0;
3193}
3194
3195static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3196{
3197 int ret, clen;
3198
3199 s->zstream.avail_out = len;
3200 s->zstream.next_out = buf;
3201 while (s->zstream.avail_out > 0) {
3202 if (s->zstream.avail_in == 0) {
3203 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3204 return -1;
3205 clen = qemu_get_be16(s->f);
3206 if (clen > IOBUF_SIZE)
3207 return -1;
3208 qemu_get_buffer(s->f, s->buf, clen);
3209 s->zstream.avail_in = clen;
3210 s->zstream.next_in = s->buf;
3211 }
3212 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3213 if (ret != Z_OK && ret != Z_STREAM_END) {
3214 return -1;
3215 }
3216 }
3217 return 0;
3218}
3219
3220static void ram_decompress_close(RamDecompressState *s)
3221{
3222 inflateEnd(&s->zstream);
3223}
3224
aliguori475e4272008-10-06 20:21:51 +00003225#define RAM_SAVE_FLAG_FULL 0x01
3226#define RAM_SAVE_FLAG_COMPRESS 0x02
3227#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3228#define RAM_SAVE_FLAG_PAGE 0x08
3229#define RAM_SAVE_FLAG_EOS 0x10
3230
3231static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003232{
aliguori475e4272008-10-06 20:21:51 +00003233 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3234 uint32_t *array = (uint32_t *)page;
3235 int i;
ths3b46e622007-09-17 08:09:54 +00003236
aliguori475e4272008-10-06 20:21:51 +00003237 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3238 if (array[i] != val)
3239 return 0;
bellardc88676f2006-08-06 13:36:11 +00003240 }
aliguori475e4272008-10-06 20:21:51 +00003241
3242 return 1;
bellardc88676f2006-08-06 13:36:11 +00003243}
3244
aliguori475e4272008-10-06 20:21:51 +00003245static int ram_save_block(QEMUFile *f)
3246{
3247 static ram_addr_t current_addr = 0;
3248 ram_addr_t saved_addr = current_addr;
3249 ram_addr_t addr = 0;
3250 int found = 0;
3251
pbrook94a6b542009-04-11 17:15:54 +00003252 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00003253 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00003254 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00003255
3256 cpu_physical_memory_reset_dirty(current_addr,
3257 current_addr + TARGET_PAGE_SIZE,
3258 MIGRATION_DIRTY_FLAG);
3259
pbrook5579c7f2009-04-11 14:47:08 +00003260 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00003261
pbrook5579c7f2009-04-11 14:47:08 +00003262 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00003263 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00003264 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00003265 } else {
3266 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00003267 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003268 }
3269
3270 found = 1;
3271 break;
3272 }
3273 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00003274 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00003275 }
3276
3277 return found;
3278}
3279
3280static ram_addr_t ram_save_threshold = 10;
3281
3282static ram_addr_t ram_save_remaining(void)
3283{
3284 ram_addr_t addr;
3285 ram_addr_t count = 0;
3286
pbrook94a6b542009-04-11 17:15:54 +00003287 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003288 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3289 count++;
3290 }
3291
3292 return count;
3293}
3294
3295static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3296{
3297 ram_addr_t addr;
3298
3299 if (stage == 1) {
3300 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00003301 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003302 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3303 cpu_physical_memory_set_dirty(addr);
3304 }
3305
3306 /* Enable dirty memory tracking */
3307 cpu_physical_memory_set_dirty_tracking(1);
3308
pbrook94a6b542009-04-11 17:15:54 +00003309 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003310 }
3311
3312 while (!qemu_file_rate_limit(f)) {
3313 int ret;
3314
3315 ret = ram_save_block(f);
3316 if (ret == 0) /* no more blocks */
3317 break;
3318 }
3319
3320 /* try transferring iterative blocks of memory */
3321
3322 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003323
3324 /* flush all remaining blocks regardless of rate limiting */
3325 while (ram_save_block(f) != 0);
aliguori8215e912009-04-05 19:30:55 +00003326 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003327 }
3328
3329 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3330
3331 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3332}
3333
3334static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003335{
3336 RamDecompressState s1, *s = &s1;
3337 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003338 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003339
bellardc88676f2006-08-06 13:36:11 +00003340 if (ram_decompress_open(s, f) < 0)
3341 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003342 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
bellardc88676f2006-08-06 13:36:11 +00003343 if (ram_decompress_buf(s, buf, 1) < 0) {
3344 fprintf(stderr, "Error while reading ram block header\n");
3345 goto error;
3346 }
3347 if (buf[0] == 0) {
pbrook5579c7f2009-04-11 14:47:08 +00003348 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3349 BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003350 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003351 goto error;
3352 }
aliguori475e4272008-10-06 20:21:51 +00003353 } else {
bellardc88676f2006-08-06 13:36:11 +00003354 error:
3355 printf("Error block header\n");
3356 return -EINVAL;
3357 }
3358 }
3359 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003360
3361 return 0;
3362}
3363
3364static int ram_load(QEMUFile *f, void *opaque, int version_id)
3365{
3366 ram_addr_t addr;
3367 int flags;
3368
3369 if (version_id == 1)
3370 return ram_load_v1(f, opaque);
3371
3372 if (version_id == 2) {
pbrook94a6b542009-04-11 17:15:54 +00003373 if (qemu_get_be32(f) != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003374 return -EINVAL;
3375 return ram_load_dead(f, opaque);
3376 }
3377
3378 if (version_id != 3)
3379 return -EINVAL;
3380
3381 do {
3382 addr = qemu_get_be64(f);
3383
3384 flags = addr & ~TARGET_PAGE_MASK;
3385 addr &= TARGET_PAGE_MASK;
3386
3387 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003388 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003389 return -EINVAL;
3390 }
3391
3392 if (flags & RAM_SAVE_FLAG_FULL) {
3393 if (ram_load_dead(f, opaque) < 0)
3394 return -EINVAL;
3395 }
3396
3397 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3398 uint8_t ch = qemu_get_byte(f);
pbrook5579c7f2009-04-11 14:47:08 +00003399 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003400 } else if (flags & RAM_SAVE_FLAG_PAGE)
pbrook5579c7f2009-04-11 14:47:08 +00003401 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003402 } while (!(flags & RAM_SAVE_FLAG_EOS));
3403
bellardc88676f2006-08-06 13:36:11 +00003404 return 0;
3405}
3406
aliguori9e472e12008-10-08 19:50:24 +00003407void qemu_service_io(void)
3408{
aliguorid9f75a42009-04-24 18:03:11 +00003409 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003410}
3411
bellard8a7ddc32004-03-31 19:00:16 +00003412/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003413/* bottom halves (can be seen as timers which expire ASAP) */
3414
3415struct QEMUBH {
3416 QEMUBHFunc *cb;
3417 void *opaque;
3418 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003419 int idle;
3420 int deleted;
bellard83f64092006-08-01 16:21:11 +00003421 QEMUBH *next;
3422};
3423
3424static QEMUBH *first_bh = NULL;
3425
3426QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3427{
3428 QEMUBH *bh;
3429 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003430 bh->cb = cb;
3431 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003432 bh->next = first_bh;
3433 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003434 return bh;
3435}
3436
bellard6eb57332006-08-06 09:51:25 +00003437int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003438{
aliguori1b435b12008-10-31 17:24:21 +00003439 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003440 int ret;
bellard83f64092006-08-01 16:21:11 +00003441
bellard6eb57332006-08-06 09:51:25 +00003442 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003443 for (bh = first_bh; bh; bh = bh->next) {
3444 if (!bh->deleted && bh->scheduled) {
3445 bh->scheduled = 0;
3446 if (!bh->idle)
3447 ret = 1;
3448 bh->idle = 0;
3449 bh->cb(bh->opaque);
3450 }
bellard83f64092006-08-01 16:21:11 +00003451 }
aliguori1b435b12008-10-31 17:24:21 +00003452
3453 /* remove deleted bhs */
3454 bhp = &first_bh;
3455 while (*bhp) {
3456 bh = *bhp;
3457 if (bh->deleted) {
3458 *bhp = bh->next;
3459 qemu_free(bh);
3460 } else
3461 bhp = &bh->next;
3462 }
3463
bellard6eb57332006-08-06 09:51:25 +00003464 return ret;
bellard83f64092006-08-01 16:21:11 +00003465}
3466
aliguori1b435b12008-10-31 17:24:21 +00003467void qemu_bh_schedule_idle(QEMUBH *bh)
3468{
3469 if (bh->scheduled)
3470 return;
3471 bh->scheduled = 1;
3472 bh->idle = 1;
3473}
3474
bellard83f64092006-08-01 16:21:11 +00003475void qemu_bh_schedule(QEMUBH *bh)
3476{
bellard83f64092006-08-01 16:21:11 +00003477 if (bh->scheduled)
3478 return;
3479 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003480 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003481 /* stop the currently executing CPU to execute the BH ASAP */
aliguorid9f75a42009-04-24 18:03:11 +00003482 qemu_notify_event();
bellard83f64092006-08-01 16:21:11 +00003483}
3484
3485void qemu_bh_cancel(QEMUBH *bh)
3486{
aliguori1b435b12008-10-31 17:24:21 +00003487 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003488}
3489
3490void qemu_bh_delete(QEMUBH *bh)
3491{
aliguori1b435b12008-10-31 17:24:21 +00003492 bh->scheduled = 0;
3493 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003494}
3495
aliguori56f3a5d2008-10-31 18:07:17 +00003496static void qemu_bh_update_timeout(int *timeout)
3497{
3498 QEMUBH *bh;
3499
3500 for (bh = first_bh; bh; bh = bh->next) {
3501 if (!bh->deleted && bh->scheduled) {
3502 if (bh->idle) {
3503 /* idle bottom halves will be polled at least
3504 * every 10ms */
3505 *timeout = MIN(10, *timeout);
3506 } else {
3507 /* non-idle bottom halves will be executed
3508 * immediately */
3509 *timeout = 0;
3510 break;
3511 }
3512 }
3513 }
3514}
3515
bellard83f64092006-08-01 16:21:11 +00003516/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003517/* machine registration */
3518
blueswir1bdaf78e2008-10-04 07:24:27 +00003519static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003520QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003521
3522int qemu_register_machine(QEMUMachine *m)
3523{
3524 QEMUMachine **pm;
3525 pm = &first_machine;
3526 while (*pm != NULL)
3527 pm = &(*pm)->next;
3528 m->next = NULL;
3529 *pm = m;
3530 return 0;
3531}
3532
pbrook9596ebb2007-11-18 01:44:38 +00003533static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003534{
3535 QEMUMachine *m;
3536
3537 for(m = first_machine; m != NULL; m = m->next) {
3538 if (!strcmp(m->name, name))
3539 return m;
3540 }
3541 return NULL;
3542}
3543
3544/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003545/* main execution loop */
3546
pbrook9596ebb2007-11-18 01:44:38 +00003547static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003548{
aliguori7d957bd2009-01-15 22:14:11 +00003549 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003550 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003551 DisplayChangeListener *dcl = ds->listeners;
3552
3553 dpy_refresh(ds);
3554
3555 while (dcl != NULL) {
3556 if (dcl->gui_timer_interval &&
3557 dcl->gui_timer_interval < interval)
3558 interval = dcl->gui_timer_interval;
3559 dcl = dcl->next;
3560 }
3561 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003562}
3563
blueswir19043b622009-01-21 19:28:13 +00003564static void nographic_update(void *opaque)
3565{
3566 uint64_t interval = GUI_REFRESH_INTERVAL;
3567
3568 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3569}
3570
bellard0bd48852005-11-11 00:00:47 +00003571struct vm_change_state_entry {
3572 VMChangeStateHandler *cb;
3573 void *opaque;
3574 LIST_ENTRY (vm_change_state_entry) entries;
3575};
3576
3577static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3578
3579VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3580 void *opaque)
3581{
3582 VMChangeStateEntry *e;
3583
3584 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003585
3586 e->cb = cb;
3587 e->opaque = opaque;
3588 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3589 return e;
3590}
3591
3592void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3593{
3594 LIST_REMOVE (e, entries);
3595 qemu_free (e);
3596}
3597
aliguori9781e042009-01-22 17:15:29 +00003598static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003599{
3600 VMChangeStateEntry *e;
3601
3602 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003603 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003604 }
3605}
3606
bellard8a7ddc32004-03-31 19:00:16 +00003607void vm_start(void)
3608{
3609 if (!vm_running) {
3610 cpu_enable_ticks();
3611 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003612 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003613 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00003614 }
3615}
3616
ths5fafdf22007-09-16 21:08:06 +00003617void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00003618{
3619 if (vm_running) {
3620 cpu_disable_ticks();
3621 vm_running = 0;
aliguori9781e042009-01-22 17:15:29 +00003622 vm_state_notify(0, reason);
bellard8a7ddc32004-03-31 19:00:16 +00003623 }
3624}
3625
bellardbb0c6722004-06-20 12:37:32 +00003626/* reset/shutdown handler */
3627
3628typedef struct QEMUResetEntry {
3629 QEMUResetHandler *func;
3630 void *opaque;
3631 struct QEMUResetEntry *next;
3632} QEMUResetEntry;
3633
3634static QEMUResetEntry *first_reset_entry;
3635static int reset_requested;
3636static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003637static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00003638
aurel32cf7a2fe2008-03-18 06:53:05 +00003639int qemu_shutdown_requested(void)
3640{
3641 int r = shutdown_requested;
3642 shutdown_requested = 0;
3643 return r;
3644}
3645
3646int qemu_reset_requested(void)
3647{
3648 int r = reset_requested;
3649 reset_requested = 0;
3650 return r;
3651}
3652
3653int qemu_powerdown_requested(void)
3654{
3655 int r = powerdown_requested;
3656 powerdown_requested = 0;
3657 return r;
3658}
3659
bellardbb0c6722004-06-20 12:37:32 +00003660void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3661{
3662 QEMUResetEntry **pre, *re;
3663
3664 pre = &first_reset_entry;
3665 while (*pre != NULL)
3666 pre = &(*pre)->next;
3667 re = qemu_mallocz(sizeof(QEMUResetEntry));
3668 re->func = func;
3669 re->opaque = opaque;
3670 re->next = NULL;
3671 *pre = re;
3672}
3673
aurel32cf7a2fe2008-03-18 06:53:05 +00003674void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00003675{
3676 QEMUResetEntry *re;
3677
3678 /* reset all devices */
3679 for(re = first_reset_entry; re != NULL; re = re->next) {
3680 re->func(re->opaque);
3681 }
aliguori29203dc2009-04-17 14:26:17 +00003682 if (kvm_enabled())
3683 kvm_sync_vcpus();
bellardbb0c6722004-06-20 12:37:32 +00003684}
3685
3686void qemu_system_reset_request(void)
3687{
bellardd1beab82006-10-02 19:44:22 +00003688 if (no_reboot) {
3689 shutdown_requested = 1;
3690 } else {
3691 reset_requested = 1;
3692 }
aliguorid9f75a42009-04-24 18:03:11 +00003693 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003694}
3695
3696void qemu_system_shutdown_request(void)
3697{
3698 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003699 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003700}
3701
bellard34751872005-07-02 14:31:34 +00003702void qemu_system_powerdown_request(void)
3703{
3704 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003705 qemu_notify_event();
3706}
3707
3708void qemu_notify_event(void)
3709{
3710 CPUState *env = cpu_single_env;
3711
3712 if (env) {
3713 cpu_exit(env);
3714#ifdef USE_KQEMU
3715 if (env->kqemu_enabled)
3716 kqemu_cpu_interrupt(env);
3717#endif
3718 }
bellardbb0c6722004-06-20 12:37:32 +00003719}
3720
ths877cf882007-04-18 18:11:47 +00003721#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003722static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003723{
3724 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003725 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003726
bellardf3311102006-04-12 20:21:17 +00003727
3728 /* XXX: need to suppress polling by better using win32 events */
3729 ret = 0;
3730 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3731 ret |= pe->func(pe->opaque);
3732 }
thse6b1e552007-04-18 17:56:02 +00003733 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003734 int err;
3735 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003736
aliguori56f3a5d2008-10-31 18:07:17 +00003737 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003738 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3739 if (w->func[ret - WAIT_OBJECT_0])
3740 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003741
ths5fafdf22007-09-16 21:08:06 +00003742 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003743 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003744
thse6b1e552007-04-18 17:56:02 +00003745 /* Check if event is signaled */
3746 ret2 = WaitForSingleObject(w->events[i], 0);
3747 if(ret2 == WAIT_OBJECT_0) {
3748 if (w->func[i])
3749 w->func[i](w->opaque[i]);
3750 } else if (ret2 == WAIT_TIMEOUT) {
3751 } else {
3752 err = GetLastError();
3753 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003754 }
3755 }
bellarda18e5242006-06-25 17:18:27 +00003756 } else if (ret == WAIT_TIMEOUT) {
3757 } else {
3758 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003759 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003760 }
bellardf3311102006-04-12 20:21:17 +00003761 }
aliguori56f3a5d2008-10-31 18:07:17 +00003762
3763 *timeout = 0;
3764}
3765#else
blueswir169d64512008-12-07 19:30:18 +00003766static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003767{
3768}
bellardfd1dff42006-02-01 21:29:26 +00003769#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003770
3771void main_loop_wait(int timeout)
3772{
3773 IOHandlerRecord *ioh;
3774 fd_set rfds, wfds, xfds;
3775 int ret, nfds;
3776 struct timeval tv;
3777
3778 qemu_bh_update_timeout(&timeout);
3779
3780 host_main_loop_wait(&timeout);
3781
bellardfd1dff42006-02-01 21:29:26 +00003782 /* poll any events */
3783 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003784 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003785 FD_ZERO(&rfds);
3786 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003787 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003788 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003789 if (ioh->deleted)
3790 continue;
bellardfd1dff42006-02-01 21:29:26 +00003791 if (ioh->fd_read &&
3792 (!ioh->fd_read_poll ||
3793 ioh->fd_read_poll(ioh->opaque) != 0)) {
3794 FD_SET(ioh->fd, &rfds);
3795 if (ioh->fd > nfds)
3796 nfds = ioh->fd;
3797 }
3798 if (ioh->fd_write) {
3799 FD_SET(ioh->fd, &wfds);
3800 if (ioh->fd > nfds)
3801 nfds = ioh->fd;
3802 }
3803 }
ths3b46e622007-09-17 08:09:54 +00003804
aliguori56f3a5d2008-10-31 18:07:17 +00003805 tv.tv_sec = timeout / 1000;
3806 tv.tv_usec = (timeout % 1000) * 1000;
3807
bellarde0356492006-05-01 13:33:02 +00003808#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003809 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003810 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3811 }
3812#endif
3813 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00003814 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003815 IOHandlerRecord **pioh;
3816
3817 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003818 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003819 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003820 }
ths6ab43fd2007-08-25 01:34:19 +00003821 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003822 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003823 }
3824 }
thscafffd42007-02-28 21:59:44 +00003825
3826 /* remove deleted IO handlers */
3827 pioh = &first_io_handler;
3828 while (*pioh) {
3829 ioh = *pioh;
3830 if (ioh->deleted) {
3831 *pioh = ioh->next;
3832 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003833 } else
thscafffd42007-02-28 21:59:44 +00003834 pioh = &ioh->next;
3835 }
bellardfd1dff42006-02-01 21:29:26 +00003836 }
bellarde0356492006-05-01 13:33:02 +00003837#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003838 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003839 if (ret < 0) {
3840 FD_ZERO(&rfds);
3841 FD_ZERO(&wfds);
3842 FD_ZERO(&xfds);
3843 }
3844 slirp_select_poll(&rfds, &wfds, &xfds);
3845 }
3846#endif
bellardc20709a2004-04-21 23:27:19 +00003847
aliguori357c6922008-11-25 17:26:09 +00003848 /* vm time timers */
3849 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3850 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3851 qemu_get_clock(vm_clock));
3852
3853 /* real time timers */
3854 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3855 qemu_get_clock(rt_clock));
3856
pbrook423f0742007-05-23 00:06:54 +00003857 /* Check bottom-halves last in case any of the earlier events triggered
3858 them. */
3859 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003860
bellard5905b2e2004-08-01 21:53:26 +00003861}
3862
pbrook9596ebb2007-11-18 01:44:38 +00003863static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00003864{
3865 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00003866#ifdef CONFIG_PROFILER
3867 int64_t ti;
3868#endif
bellard6a00d602005-11-21 23:25:50 +00003869 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003870
bellard6a00d602005-11-21 23:25:50 +00003871 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00003872 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00003873 for(;;) {
3874 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00003875
bellard15a76442005-11-23 21:01:03 +00003876 for(;;) {
3877 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00003878 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00003879#ifdef CONFIG_PROFILER
3880 ti = profile_getclock();
3881#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003882 if (use_icount) {
3883 int64_t count;
3884 int decr;
3885 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3886 env->icount_decr.u16.low = 0;
3887 env->icount_extra = 0;
3888 count = qemu_next_deadline();
3889 count = (count + (1 << icount_time_shift) - 1)
3890 >> icount_time_shift;
3891 qemu_icount += count;
3892 decr = (count > 0xffff) ? 0xffff : count;
3893 count -= decr;
3894 env->icount_decr.u16.low = decr;
3895 env->icount_extra = count;
3896 }
bellard6a00d602005-11-21 23:25:50 +00003897 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00003898#ifdef CONFIG_PROFILER
3899 qemu_time += profile_getclock() - ti;
3900#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003901 if (use_icount) {
3902 /* Fold pending instructions back into the
3903 instruction counter, and clear the interrupt flag. */
3904 qemu_icount -= (env->icount_decr.u16.low
3905 + env->icount_extra);
3906 env->icount_decr.u32 = 0;
3907 env->icount_extra = 0;
3908 }
balrogee5605e2007-12-03 03:01:40 +00003909 next_cpu = env->next_cpu ?: first_cpu;
aurel3295b01002008-04-04 17:16:35 +00003910 if (event_pending && likely(ret != EXCP_DEBUG)) {
balrogee5605e2007-12-03 03:01:40 +00003911 ret = EXCP_INTERRUPT;
3912 event_pending = 0;
3913 break;
3914 }
pbrookbd967e02007-03-11 18:54:57 +00003915 if (ret == EXCP_HLT) {
3916 /* Give the next CPU a chance to run. */
3917 cur_cpu = env;
3918 continue;
3919 }
bellard15a76442005-11-23 21:01:03 +00003920 if (ret != EXCP_HALTED)
3921 break;
3922 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00003923 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00003924 break;
bellard15a76442005-11-23 21:01:03 +00003925 }
3926 cur_cpu = env;
3927
bellard5905b2e2004-08-01 21:53:26 +00003928 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00003929 ret = EXCP_INTERRUPT;
aurel32b2f76162008-04-11 21:35:52 +00003930 if (no_shutdown) {
3931 vm_stop(0);
3932 no_shutdown = 0;
3933 }
3934 else
3935 break;
bellard5905b2e2004-08-01 21:53:26 +00003936 }
3937 if (reset_requested) {
3938 reset_requested = 0;
3939 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00003940 ret = EXCP_INTERRUPT;
3941 }
3942 if (powerdown_requested) {
3943 powerdown_requested = 0;
3944 qemu_system_powerdown();
3945 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00003946 }
aurel3295b01002008-04-04 17:16:35 +00003947 if (unlikely(ret == EXCP_DEBUG)) {
aliguori880a7572008-11-18 20:30:24 +00003948 gdb_set_stop_cpu(cur_cpu);
bellard5905b2e2004-08-01 21:53:26 +00003949 vm_stop(EXCP_DEBUG);
3950 }
pbrookbd967e02007-03-11 18:54:57 +00003951 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00003952 /* XXX: use timeout computed from timers */
pbrook2e70f6e2008-06-29 01:03:05 +00003953 if (ret == EXCP_HALTED) {
3954 if (use_icount) {
3955 int64_t add;
3956 int64_t delta;
3957 /* Advance virtual time to the next event. */
3958 if (use_icount == 1) {
3959 /* When not using an adaptive execution frequency
3960 we tend to get badly out of sync with real time,
thsbf20dc02008-06-30 17:22:19 +00003961 so just delay for a reasonable amount of time. */
pbrook2e70f6e2008-06-29 01:03:05 +00003962 delta = 0;
3963 } else {
3964 delta = cpu_get_icount() - cpu_get_clock();
3965 }
3966 if (delta > 0) {
3967 /* If virtual time is ahead of real time then just
3968 wait for IO. */
3969 timeout = (delta / 1000000) + 1;
3970 } else {
3971 /* Wait for either IO to occur or the next
3972 timer event. */
3973 add = qemu_next_deadline();
3974 /* We advance the timer before checking for IO.
3975 Limit the amount we advance so that early IO
3976 activity won't get the guest too far ahead. */
3977 if (add > 10000000)
3978 add = 10000000;
3979 delta += add;
3980 add = (add + (1 << icount_time_shift) - 1)
3981 >> icount_time_shift;
3982 qemu_icount += add;
3983 timeout = delta / 1000000;
3984 if (timeout < 0)
3985 timeout = 0;
3986 }
3987 } else {
aliguori0a1af392008-10-31 18:40:25 +00003988 timeout = 5000;
pbrook2e70f6e2008-06-29 01:03:05 +00003989 }
3990 } else {
bellard5905b2e2004-08-01 21:53:26 +00003991 timeout = 0;
pbrook2e70f6e2008-06-29 01:03:05 +00003992 }
bellard5905b2e2004-08-01 21:53:26 +00003993 } else {
blueswir198448f52008-09-30 18:16:09 +00003994 if (shutdown_requested) {
3995 ret = EXCP_INTERRUPT;
aliguori5b08fc12008-08-21 20:08:03 +00003996 break;
blueswir198448f52008-09-30 18:16:09 +00003997 }
aliguori0a1af392008-10-31 18:40:25 +00003998 timeout = 5000;
bellard5905b2e2004-08-01 21:53:26 +00003999 }
bellard89bfc102006-02-08 22:46:31 +00004000#ifdef CONFIG_PROFILER
4001 ti = profile_getclock();
4002#endif
bellard5905b2e2004-08-01 21:53:26 +00004003 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00004004#ifdef CONFIG_PROFILER
4005 dev_time += profile_getclock() - ti;
4006#endif
bellardb4608c02003-06-27 17:34:32 +00004007 }
bellard34865132003-10-05 14:28:56 +00004008 cpu_disable_ticks();
4009 return ret;
bellardb4608c02003-06-27 17:34:32 +00004010}
4011
pbrook9bd7e6d2009-04-07 22:58:45 +00004012static void version(void)
4013{
pbrook4a19f1e2009-04-07 23:17:49 +00004014 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004015}
4016
ths15f82202007-06-29 23:26:08 +00004017static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004018{
pbrook9bd7e6d2009-04-07 22:58:45 +00004019 version();
4020 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004021 "\n"
bellarda20dd502003-09-30 21:07:02 +00004022 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004023 "\n"
blueswir15824d652009-03-28 06:44:27 +00004024#define DEF(option, opt_arg, opt_enum, opt_help) \
4025 opt_help
4026#define DEFHEADING(text) stringify(text) "\n"
4027#include "qemu-options.h"
4028#undef DEF
4029#undef DEFHEADING
4030#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004031 "\n"
bellard82c643f2004-07-14 17:28:13 +00004032 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004033 "ctrl-alt-f toggle full screen\n"
4034 "ctrl-alt-n switch to virtual console 'n'\n"
4035 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004036 "\n"
4037 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4038 ,
bellard0db63472003-10-27 21:37:46 +00004039 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004040 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004041#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004042 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004043 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004044#endif
bellard6e44ba72004-01-18 21:56:49 +00004045 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004046 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004047 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004048}
4049
bellardcd6f1162004-05-13 22:02:20 +00004050#define HAS_ARG 0x0001
4051
4052enum {
blueswir15824d652009-03-28 06:44:27 +00004053#define DEF(option, opt_arg, opt_enum, opt_help) \
4054 opt_enum,
4055#define DEFHEADING(text)
4056#include "qemu-options.h"
4057#undef DEF
4058#undef DEFHEADING
4059#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004060};
4061
4062typedef struct QEMUOption {
4063 const char *name;
4064 int flags;
4065 int index;
4066} QEMUOption;
4067
blueswir1dbed7e42008-10-01 19:38:09 +00004068static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004069 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004070#define DEF(option, opt_arg, opt_enum, opt_help) \
4071 { option, opt_arg, opt_enum },
4072#define DEFHEADING(text)
4073#include "qemu-options.h"
4074#undef DEF
4075#undef DEFHEADING
4076#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004077 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004078};
4079
bellard1d14ffa2005-10-30 18:58:22 +00004080#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004081struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004082#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004083#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004084 {
4085 "pcspk",
4086 "PC speaker",
4087 0,
4088 1,
4089 { .init_isa = pcspk_audio_init }
4090 },
4091#endif
malc4c9b53e2009-01-09 10:46:34 +00004092
4093#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004094 {
4095 "sb16",
4096 "Creative Sound Blaster 16",
4097 0,
4098 1,
4099 { .init_isa = SB16_init }
4100 },
malc4c9b53e2009-01-09 10:46:34 +00004101#endif
bellard6a36d842005-12-18 20:34:32 +00004102
malccc53d262008-06-13 10:48:22 +00004103#ifdef CONFIG_CS4231A
4104 {
4105 "cs4231a",
4106 "CS4231A",
4107 0,
4108 1,
4109 { .init_isa = cs4231a_init }
4110 },
4111#endif
4112
bellard6a36d842005-12-18 20:34:32 +00004113#ifdef CONFIG_ADLIB
4114 {
4115 "adlib",
4116#ifdef HAS_YMF262
4117 "Yamaha YMF262 (OPL3)",
4118#else
4119 "Yamaha YM3812 (OPL2)",
4120#endif
4121 0,
4122 1,
4123 { .init_isa = Adlib_init }
4124 },
4125#endif
4126
4127#ifdef CONFIG_GUS
4128 {
4129 "gus",
4130 "Gravis Ultrasound GF1",
4131 0,
4132 1,
4133 { .init_isa = GUS_init }
4134 },
4135#endif
4136
malc4c9b53e2009-01-09 10:46:34 +00004137#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004138 {
4139 "ac97",
4140 "Intel 82801AA AC97 Audio",
4141 0,
4142 0,
4143 { .init_pci = ac97_init }
4144 },
malc4c9b53e2009-01-09 10:46:34 +00004145#endif
balroge5c9a132008-01-14 04:27:55 +00004146
malc4c9b53e2009-01-09 10:46:34 +00004147#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004148 {
4149 "es1370",
4150 "ENSONIQ AudioPCI ES1370",
4151 0,
4152 0,
4153 { .init_pci = es1370_init }
4154 },
balrogb00052e2007-04-30 02:22:06 +00004155#endif
bellard6a36d842005-12-18 20:34:32 +00004156
malc4c9b53e2009-01-09 10:46:34 +00004157#endif /* HAS_AUDIO_CHOICE */
4158
bellard6a36d842005-12-18 20:34:32 +00004159 { NULL, NULL, 0, 0, { NULL } }
4160};
4161
bellard1d14ffa2005-10-30 18:58:22 +00004162static void select_soundhw (const char *optarg)
4163{
bellard6a36d842005-12-18 20:34:32 +00004164 struct soundhw *c;
4165
bellard1d14ffa2005-10-30 18:58:22 +00004166 if (*optarg == '?') {
4167 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004168
bellard1d14ffa2005-10-30 18:58:22 +00004169 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004170 for (c = soundhw; c->name; ++c) {
4171 printf ("%-11s %s\n", c->name, c->descr);
4172 }
4173 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004174 exit (*optarg != '?');
4175 }
4176 else {
bellard6a36d842005-12-18 20:34:32 +00004177 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004178 const char *p;
4179 char *e;
4180 int bad_card = 0;
4181
bellard6a36d842005-12-18 20:34:32 +00004182 if (!strcmp (optarg, "all")) {
4183 for (c = soundhw; c->name; ++c) {
4184 c->enabled = 1;
4185 }
4186 return;
4187 }
bellard1d14ffa2005-10-30 18:58:22 +00004188
bellard6a36d842005-12-18 20:34:32 +00004189 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004190 while (*p) {
4191 e = strchr (p, ',');
4192 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004193
4194 for (c = soundhw; c->name; ++c) {
4195 if (!strncmp (c->name, p, l)) {
4196 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004197 break;
4198 }
4199 }
bellard6a36d842005-12-18 20:34:32 +00004200
4201 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004202 if (l > 80) {
4203 fprintf (stderr,
4204 "Unknown sound card name (too big to show)\n");
4205 }
4206 else {
4207 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4208 (int) l, p);
4209 }
4210 bad_card = 1;
4211 }
4212 p += l + (e != NULL);
4213 }
4214
4215 if (bad_card)
4216 goto show_valid_cards;
4217 }
4218}
4219#endif
4220
malc3893c122008-09-28 00:42:05 +00004221static void select_vgahw (const char *p)
4222{
4223 const char *opts;
4224
aliguori28b85ed2009-04-22 15:19:48 +00004225 cirrus_vga_enabled = 0;
4226 std_vga_enabled = 0;
4227 vmsvga_enabled = 0;
aliguori94909d92009-04-22 15:19:53 +00004228 xenfb_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004229 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004230 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004231 } else if (strstart(p, "cirrus", &opts)) {
4232 cirrus_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004233 } else if (strstart(p, "vmware", &opts)) {
malc3893c122008-09-28 00:42:05 +00004234 vmsvga_enabled = 1;
aliguori94909d92009-04-22 15:19:53 +00004235 } else if (strstart(p, "xenfb", &opts)) {
4236 xenfb_enabled = 1;
aliguori28b85ed2009-04-22 15:19:48 +00004237 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004238 invalid_vga:
4239 fprintf(stderr, "Unknown vga type: %s\n", p);
4240 exit(1);
4241 }
malccb5a7aa2008-09-28 00:42:12 +00004242 while (*opts) {
4243 const char *nextopt;
4244
4245 if (strstart(opts, ",retrace=", &nextopt)) {
4246 opts = nextopt;
4247 if (strstart(opts, "dumb", &nextopt))
4248 vga_retrace_method = VGA_RETRACE_DUMB;
4249 else if (strstart(opts, "precise", &nextopt))
4250 vga_retrace_method = VGA_RETRACE_PRECISE;
4251 else goto invalid_vga;
4252 } else goto invalid_vga;
4253 opts = nextopt;
4254 }
malc3893c122008-09-28 00:42:05 +00004255}
4256
bellard3587d7e2006-06-26 20:03:44 +00004257#ifdef _WIN32
4258static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4259{
4260 exit(STATUS_CONTROL_C_EXIT);
4261 return TRUE;
4262}
4263#endif
4264
aliguoric4be29f2009-04-17 18:58:14 +00004265int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004266{
4267 int ret;
4268
4269 if(strlen(str) != 36)
4270 return -1;
4271
4272 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4273 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4274 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4275
4276 if(ret != 16)
4277 return -1;
4278
aliguorib6f6e3d2009-04-17 18:59:56 +00004279#ifdef TARGET_I386
4280 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4281#endif
4282
blueswir18fcb1b92008-09-18 18:29:08 +00004283 return 0;
4284}
4285
bellard7c9d8e02005-11-15 22:16:05 +00004286#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004287
aliguori5b08fc12008-08-21 20:08:03 +00004288#ifndef _WIN32
4289
4290static void termsig_handler(int signal)
4291{
4292 qemu_system_shutdown_request();
4293}
4294
blueswir16f9e3802008-09-09 18:56:59 +00004295static void termsig_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004296{
4297 struct sigaction act;
4298
4299 memset(&act, 0, sizeof(act));
4300 act.sa_handler = termsig_handler;
4301 sigaction(SIGINT, &act, NULL);
4302 sigaction(SIGHUP, &act, NULL);
4303 sigaction(SIGTERM, &act, NULL);
4304}
4305
4306#endif
4307
malc902b3d52008-12-10 19:18:40 +00004308int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004309{
bellard67b915a2004-03-31 23:37:16 +00004310#ifdef CONFIG_GDBSTUB
aliguori59030a82009-04-05 18:43:41 +00004311 const char *gdbstub_dev = NULL;
bellard67b915a2004-03-31 23:37:16 +00004312#endif
j_mayer28c5af52007-11-11 01:50:45 +00004313 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004314 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004315 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004316 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004317 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00004318 const char *boot_devices = "";
aliguori3023f332009-01-16 19:04:14 +00004319 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004320 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004321 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004322 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004323 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004324 const char *bt_opts[MAX_BT_CMDLINE];
4325 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004326 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004327 int optind;
4328 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004329 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004330 const char *monitor_device;
4331 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004332 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004333 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004334 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004335 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4336 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004337 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004338 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004339 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004340 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004341 int usb_devices_index;
blueswir1b9e82a52009-04-05 18:03:31 +00004342#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004343 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004344#endif
bellard26a5f132008-05-28 12:30:31 +00004345 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004346 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004347 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004348#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004349 int fd = 0;
4350 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004351 const char *chroot_dir = NULL;
4352 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004353#endif
aliguori268a3622009-04-21 22:30:27 +00004354 CPUState *env;
bellard0bd48852005-11-11 00:00:47 +00004355
malc902b3d52008-12-10 19:18:40 +00004356 qemu_cache_utils_init(envp);
4357
bellard0bd48852005-11-11 00:00:47 +00004358 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004359#ifndef _WIN32
4360 {
4361 struct sigaction act;
4362 sigfillset(&act.sa_mask);
4363 act.sa_flags = 0;
4364 act.sa_handler = SIG_IGN;
4365 sigaction(SIGPIPE, &act, NULL);
4366 }
bellard3587d7e2006-06-26 20:03:44 +00004367#else
4368 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004369 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4370 QEMU to run on a single CPU */
4371 {
4372 HANDLE h;
4373 DWORD mask, smask;
4374 int i;
4375 h = GetCurrentProcess();
4376 if (GetProcessAffinityMask(h, &mask, &smask)) {
4377 for(i = 0; i < 32; i++) {
4378 if (mask & (1 << i))
4379 break;
4380 }
4381 if (i != 32) {
4382 mask = 1 << i;
4383 SetProcessAffinityMask(h, mask);
4384 }
4385 }
4386 }
bellard67b915a2004-03-31 23:37:16 +00004387#endif
bellardbe995c22006-06-25 16:25:21 +00004388
bellardcc1daa42005-06-05 14:49:17 +00004389 register_machines();
4390 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004391 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004392 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004393 ram_size = 0;
bellard313aa562003-08-10 21:52:11 +00004394 vga_ram_size = VGA_RAM_SIZE;
bellard33e39632003-07-06 17:15:21 +00004395 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004396 nographic = 0;
balrog4d3b6f62008-02-10 16:33:14 +00004397 curses = 0;
bellarda20dd502003-09-30 21:07:02 +00004398 kernel_filename = NULL;
4399 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004400 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004401 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00004402 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00004403
aurel32c75a8232008-05-04 00:50:34 +00004404 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004405 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004406 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004407 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004408
aliguori8290edd2009-02-27 20:14:29 +00004409 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004410 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004411 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004412 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004413
aliguori1b8fc812009-02-27 20:01:39 +00004414 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004415 virtio_consoles[i] = NULL;
4416 virtio_console_index = 0;
4417
aliguori268a3622009-04-21 22:30:27 +00004418 for (i = 0; i < MAX_NODES; i++) {
4419 node_mem[i] = 0;
4420 node_cpumask[i] = 0;
4421 }
4422
bellarda594cfb2005-11-06 16:13:29 +00004423 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004424
bellard7c9d8e02005-11-15 22:16:05 +00004425 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004426 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004427 nb_drives = 0;
4428 nb_drives_opt = 0;
aliguori268a3622009-04-21 22:30:27 +00004429 nb_numa_nodes = 0;
thse4bcb142007-12-02 04:51:10 +00004430 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004431
4432 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004433
bellard26a5f132008-05-28 12:30:31 +00004434 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004435 autostart= 1;
4436
bellardcd6f1162004-05-13 22:02:20 +00004437 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004438 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004439 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004440 break;
bellardcd6f1162004-05-13 22:02:20 +00004441 r = argv[optind];
4442 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004443 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004444 } else {
4445 const QEMUOption *popt;
4446
4447 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004448 /* Treat --foo the same as -foo. */
4449 if (r[1] == '-')
4450 r++;
bellardcd6f1162004-05-13 22:02:20 +00004451 popt = qemu_options;
4452 for(;;) {
4453 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004454 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004455 argv[0], r);
4456 exit(1);
4457 }
4458 if (!strcmp(popt->name, r + 1))
4459 break;
4460 popt++;
4461 }
4462 if (popt->flags & HAS_ARG) {
4463 if (optind >= argc) {
4464 fprintf(stderr, "%s: option '%s' requires an argument\n",
4465 argv[0], r);
4466 exit(1);
4467 }
4468 optarg = argv[optind++];
4469 } else {
4470 optarg = NULL;
4471 }
4472
4473 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004474 case QEMU_OPTION_M:
4475 machine = find_machine(optarg);
4476 if (!machine) {
4477 QEMUMachine *m;
4478 printf("Supported machines are:\n");
4479 for(m = first_machine; m != NULL; m = m->next) {
4480 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004481 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00004482 m == first_machine ? " (default)" : "");
4483 }
ths15f82202007-06-29 23:26:08 +00004484 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004485 }
4486 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004487 case QEMU_OPTION_cpu:
4488 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004489 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004490/* XXX: implement xxx_cpu_list for targets that still miss it */
4491#if defined(cpu_list)
4492 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004493#endif
ths15f82202007-06-29 23:26:08 +00004494 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004495 } else {
4496 cpu_model = optarg;
4497 }
4498 break;
bellardcd6f1162004-05-13 22:02:20 +00004499 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004500 initrd_filename = optarg;
4501 break;
bellardcd6f1162004-05-13 22:02:20 +00004502 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004503 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00004504 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004505 else
balrog609497a2008-01-14 02:56:53 +00004506 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004507 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004508 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004509 translation == BIOS_ATA_TRANSLATION_LBA ?
4510 ",trans=lba" :
4511 translation == BIOS_ATA_TRANSLATION_NONE ?
4512 ",trans=none" : "");
4513 break;
bellardcd6f1162004-05-13 22:02:20 +00004514 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004515 case QEMU_OPTION_hdc:
4516 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004517 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004518 break;
thse4bcb142007-12-02 04:51:10 +00004519 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004520 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004521 break;
balrog3e3d5812007-04-30 02:09:25 +00004522 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004523 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004524 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004525 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004526 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004527 break;
j_mayer86f55662007-04-24 06:52:59 +00004528 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004529 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004530 break;
bellardcd6f1162004-05-13 22:02:20 +00004531 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004532 snapshot = 1;
4533 break;
bellardcd6f1162004-05-13 22:02:20 +00004534 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004535 {
bellard330d0412003-07-26 18:11:40 +00004536 const char *p;
4537 p = optarg;
4538 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004539 if (cyls < 1 || cyls > 16383)
4540 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004541 if (*p != ',')
4542 goto chs_fail;
4543 p++;
4544 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004545 if (heads < 1 || heads > 16)
4546 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004547 if (*p != ',')
4548 goto chs_fail;
4549 p++;
4550 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004551 if (secs < 1 || secs > 63)
4552 goto chs_fail;
4553 if (*p == ',') {
4554 p++;
4555 if (!strcmp(p, "none"))
4556 translation = BIOS_ATA_TRANSLATION_NONE;
4557 else if (!strcmp(p, "lba"))
4558 translation = BIOS_ATA_TRANSLATION_LBA;
4559 else if (!strcmp(p, "auto"))
4560 translation = BIOS_ATA_TRANSLATION_AUTO;
4561 else
4562 goto chs_fail;
4563 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004564 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004565 fprintf(stderr, "qemu: invalid physical CHS format\n");
4566 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004567 }
thse4bcb142007-12-02 04:51:10 +00004568 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00004569 snprintf(drives_opt[hda_index].opt,
4570 sizeof(drives_opt[hda_index].opt),
4571 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4572 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004573 translation == BIOS_ATA_TRANSLATION_LBA ?
4574 ",trans=lba" :
4575 translation == BIOS_ATA_TRANSLATION_NONE ?
4576 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00004577 }
4578 break;
aliguori268a3622009-04-21 22:30:27 +00004579 case QEMU_OPTION_numa:
4580 if (nb_numa_nodes >= MAX_NODES) {
4581 fprintf(stderr, "qemu: too many NUMA nodes\n");
4582 exit(1);
4583 }
4584 numa_add(optarg);
4585 break;
bellardcd6f1162004-05-13 22:02:20 +00004586 case QEMU_OPTION_nographic:
bellarda20dd502003-09-30 21:07:02 +00004587 nographic = 1;
4588 break;
balrog4d3b6f62008-02-10 16:33:14 +00004589#ifdef CONFIG_CURSES
4590 case QEMU_OPTION_curses:
4591 curses = 1;
4592 break;
4593#endif
balroga171fe32007-04-30 01:48:07 +00004594 case QEMU_OPTION_portrait:
4595 graphic_rotate = 1;
4596 break;
bellardcd6f1162004-05-13 22:02:20 +00004597 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004598 kernel_filename = optarg;
4599 break;
bellardcd6f1162004-05-13 22:02:20 +00004600 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004601 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004602 break;
bellardcd6f1162004-05-13 22:02:20 +00004603 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004604 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004605 break;
bellardcd6f1162004-05-13 22:02:20 +00004606 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004607 boot_devices = optarg;
4608 /* We just do some generic consistency checks */
4609 {
4610 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00004611 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00004612
4613 boot_devices_bitmap = 0;
4614 for (p = boot_devices; *p != '\0'; p++) {
4615 /* Allowed boot devices are:
4616 * a b : floppy disk drives
4617 * c ... f : IDE disk drives
4618 * g ... m : machine implementation dependant drives
4619 * n ... p : network devices
4620 * It's up to each machine implementation to check
4621 * if the given boot devices match the actual hardware
4622 * implementation and firmware features.
4623 */
4624 if (*p < 'a' || *p > 'q') {
4625 fprintf(stderr, "Invalid boot device '%c'\n", *p);
4626 exit(1);
4627 }
4628 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4629 fprintf(stderr,
4630 "Boot device '%c' was given twice\n",*p);
4631 exit(1);
4632 }
4633 boot_devices_bitmap |= 1 << (*p - 'a');
4634 }
bellard36b486b2003-11-11 13:36:08 +00004635 }
4636 break;
bellardcd6f1162004-05-13 22:02:20 +00004637 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004638 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004639 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004640 break;
bellard52ca8d62006-06-14 16:03:05 +00004641#ifdef TARGET_I386
4642 case QEMU_OPTION_no_fd_bootchk:
4643 fd_bootchk = 0;
4644 break;
4645#endif
bellard7c9d8e02005-11-15 22:16:05 +00004646 case QEMU_OPTION_net:
4647 if (nb_net_clients >= MAX_NET_CLIENTS) {
4648 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00004649 exit(1);
4650 }
pbrookfd5f3932008-03-26 20:55:43 +00004651 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00004652 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00004653 break;
bellardc7f74642004-08-24 21:57:12 +00004654#ifdef CONFIG_SLIRP
4655 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00004656 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004657 break;
ths47d5d012007-02-20 00:05:08 +00004658 case QEMU_OPTION_bootp:
4659 bootp_filename = optarg;
4660 break;
bellardc94c8d62004-09-13 21:37:34 +00004661#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004662 case QEMU_OPTION_smb:
4663 net_slirp_smb(optarg);
4664 break;
bellardc94c8d62004-09-13 21:37:34 +00004665#endif
bellard9bf05442004-08-25 22:12:49 +00004666 case QEMU_OPTION_redir:
aliguorid4ebe192009-04-21 19:56:44 +00004667 net_slirp_redir(NULL, optarg);
bellard9bf05442004-08-25 22:12:49 +00004668 break;
bellardc7f74642004-08-24 21:57:12 +00004669#endif
balrogdc72ac12008-11-09 00:04:26 +00004670 case QEMU_OPTION_bt:
4671 if (nb_bt_opts >= MAX_BT_CMDLINE) {
4672 fprintf(stderr, "qemu: too many bluetooth options\n");
4673 exit(1);
4674 }
4675 bt_opts[nb_bt_opts++] = optarg;
4676 break;
bellard1d14ffa2005-10-30 18:58:22 +00004677#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004678 case QEMU_OPTION_audio_help:
4679 AUD_help ();
4680 exit (0);
4681 break;
4682 case QEMU_OPTION_soundhw:
4683 select_soundhw (optarg);
4684 break;
4685#endif
bellardcd6f1162004-05-13 22:02:20 +00004686 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00004687 help(0);
bellardcd6f1162004-05-13 22:02:20 +00004688 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00004689 case QEMU_OPTION_version:
4690 version();
4691 exit(0);
4692 break;
aurel3200f82b82008-04-27 21:12:55 +00004693 case QEMU_OPTION_m: {
4694 uint64_t value;
4695 char *ptr;
4696
4697 value = strtoul(optarg, &ptr, 10);
4698 switch (*ptr) {
4699 case 0: case 'M': case 'm':
4700 value <<= 20;
4701 break;
4702 case 'G': case 'g':
4703 value <<= 30;
4704 break;
4705 default:
4706 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00004707 exit(1);
4708 }
aurel3200f82b82008-04-27 21:12:55 +00004709
4710 /* On 32-bit hosts, QEMU is limited by virtual address space */
4711 if (value > (2047 << 20)
blueswir1640f42e2009-04-19 10:18:01 +00004712#ifndef CONFIG_KQEMU
aurel3200f82b82008-04-27 21:12:55 +00004713 && HOST_LONG_BITS == 32
4714#endif
4715 ) {
4716 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4717 exit(1);
4718 }
4719 if (value != (uint64_t)(ram_addr_t)value) {
4720 fprintf(stderr, "qemu: ram size too large\n");
4721 exit(1);
4722 }
4723 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00004724 break;
aurel3200f82b82008-04-27 21:12:55 +00004725 }
bellardcd6f1162004-05-13 22:02:20 +00004726 case QEMU_OPTION_d:
4727 {
4728 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00004729 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00004730
bellardcd6f1162004-05-13 22:02:20 +00004731 mask = cpu_str_to_log_mask(optarg);
4732 if (!mask) {
4733 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00004734 for(item = cpu_log_items; item->mask != 0; item++) {
4735 printf("%-10s %s\n", item->name, item->help);
4736 }
4737 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00004738 }
4739 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00004740 }
bellardcd6f1162004-05-13 22:02:20 +00004741 break;
bellard67b915a2004-03-31 23:37:16 +00004742#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00004743 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00004744 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00004745 break;
aliguori59030a82009-04-05 18:43:41 +00004746 case QEMU_OPTION_gdb:
4747 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00004748 break;
bellard67b915a2004-03-31 23:37:16 +00004749#endif
bellardcd6f1162004-05-13 22:02:20 +00004750 case QEMU_OPTION_L:
4751 bios_dir = optarg;
4752 break;
j_mayer1192dad2007-10-05 13:08:35 +00004753 case QEMU_OPTION_bios:
4754 bios_name = optarg;
4755 break;
aurel321b530a62009-04-05 20:08:59 +00004756 case QEMU_OPTION_singlestep:
4757 singlestep = 1;
4758 break;
bellardcd6f1162004-05-13 22:02:20 +00004759 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00004760 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00004761 break;
blueswir15824d652009-03-28 06:44:27 +00004762#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00004763 case QEMU_OPTION_k:
4764 keyboard_layout = optarg;
4765 break;
blueswir15824d652009-03-28 06:44:27 +00004766#endif
bellardee22c2f2004-06-03 12:49:50 +00004767 case QEMU_OPTION_localtime:
4768 rtc_utc = 0;
4769 break;
malc3893c122008-09-28 00:42:05 +00004770 case QEMU_OPTION_vga:
4771 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00004772 break;
blueswir15824d652009-03-28 06:44:27 +00004773#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00004774 case QEMU_OPTION_g:
4775 {
4776 const char *p;
4777 int w, h, depth;
4778 p = optarg;
4779 w = strtol(p, (char **)&p, 10);
4780 if (w <= 0) {
4781 graphic_error:
4782 fprintf(stderr, "qemu: invalid resolution or depth\n");
4783 exit(1);
4784 }
4785 if (*p != 'x')
4786 goto graphic_error;
4787 p++;
4788 h = strtol(p, (char **)&p, 10);
4789 if (h <= 0)
4790 goto graphic_error;
4791 if (*p == 'x') {
4792 p++;
4793 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00004794 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00004795 depth != 24 && depth != 32)
4796 goto graphic_error;
4797 } else if (*p == '\0') {
4798 depth = graphic_depth;
4799 } else {
4800 goto graphic_error;
4801 }
ths3b46e622007-09-17 08:09:54 +00004802
bellarde9b137c2004-06-21 16:46:10 +00004803 graphic_width = w;
4804 graphic_height = h;
4805 graphic_depth = depth;
4806 }
4807 break;
blueswir15824d652009-03-28 06:44:27 +00004808#endif
ths20d8a3e2007-02-18 17:04:49 +00004809 case QEMU_OPTION_echr:
4810 {
4811 char *r;
4812 term_escape_char = strtol(optarg, &r, 0);
4813 if (r == optarg)
4814 printf("Bad argument to echr\n");
4815 break;
4816 }
bellard82c643f2004-07-14 17:28:13 +00004817 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00004818 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00004819 break;
4820 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00004821 if (serial_device_index >= MAX_SERIAL_PORTS) {
4822 fprintf(stderr, "qemu: too many serial ports\n");
4823 exit(1);
4824 }
pbrookfd5f3932008-03-26 20:55:43 +00004825 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00004826 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00004827 break;
aliguori51ecf132009-01-15 20:06:40 +00004828 case QEMU_OPTION_virtiocon:
4829 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
4830 fprintf(stderr, "qemu: too many virtio consoles\n");
4831 exit(1);
4832 }
4833 virtio_consoles[virtio_console_index] = optarg;
4834 virtio_console_index++;
4835 break;
bellard6508fe52005-01-15 12:02:56 +00004836 case QEMU_OPTION_parallel:
4837 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
4838 fprintf(stderr, "qemu: too many parallel ports\n");
4839 exit(1);
4840 }
pbrookfd5f3932008-03-26 20:55:43 +00004841 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00004842 parallel_device_index++;
4843 break;
bellardd63d3072004-10-03 13:29:03 +00004844 case QEMU_OPTION_loadvm:
4845 loadvm = optarg;
4846 break;
4847 case QEMU_OPTION_full_screen:
4848 full_screen = 1;
4849 break;
ths667acca2006-12-11 02:08:05 +00004850#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00004851 case QEMU_OPTION_no_frame:
4852 no_frame = 1;
4853 break;
ths3780e192007-06-21 21:08:02 +00004854 case QEMU_OPTION_alt_grab:
4855 alt_grab = 1;
4856 break;
ths667acca2006-12-11 02:08:05 +00004857 case QEMU_OPTION_no_quit:
4858 no_quit = 1;
4859 break;
aliguori7d957bd2009-01-15 22:14:11 +00004860 case QEMU_OPTION_sdl:
4861 sdl = 1;
4862 break;
ths667acca2006-12-11 02:08:05 +00004863#endif
bellardf7cce892004-12-08 22:21:25 +00004864 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00004865 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00004866 break;
bellarda09db212005-04-30 16:10:35 +00004867#ifdef TARGET_I386
4868 case QEMU_OPTION_win2k_hack:
4869 win2k_install_hack = 1;
4870 break;
aliguori73822ec2009-01-15 20:11:34 +00004871 case QEMU_OPTION_rtc_td_hack:
4872 rtc_td_hack = 1;
4873 break;
aliguori8a92ea22009-02-27 20:12:36 +00004874 case QEMU_OPTION_acpitable:
4875 if(acpi_table_add(optarg) < 0) {
4876 fprintf(stderr, "Wrong acpi table provided\n");
4877 exit(1);
4878 }
4879 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00004880 case QEMU_OPTION_smbios:
4881 if(smbios_entry_add(optarg) < 0) {
4882 fprintf(stderr, "Wrong smbios provided\n");
4883 exit(1);
4884 }
4885 break;
bellarda09db212005-04-30 16:10:35 +00004886#endif
blueswir1640f42e2009-04-19 10:18:01 +00004887#ifdef CONFIG_KQEMU
bellardd993e022005-02-10 22:00:06 +00004888 case QEMU_OPTION_no_kqemu:
4889 kqemu_allowed = 0;
4890 break;
bellard89bfc102006-02-08 22:46:31 +00004891 case QEMU_OPTION_kernel_kqemu:
4892 kqemu_allowed = 2;
4893 break;
bellardd993e022005-02-10 22:00:06 +00004894#endif
aliguori7ba1e612008-11-05 16:04:33 +00004895#ifdef CONFIG_KVM
4896 case QEMU_OPTION_enable_kvm:
4897 kvm_allowed = 1;
blueswir1640f42e2009-04-19 10:18:01 +00004898#ifdef CONFIG_KQEMU
aliguori7ba1e612008-11-05 16:04:33 +00004899 kqemu_allowed = 0;
4900#endif
4901 break;
4902#endif
bellardbb36d472005-11-05 14:22:28 +00004903 case QEMU_OPTION_usb:
4904 usb_enabled = 1;
4905 break;
bellarda594cfb2005-11-06 16:13:29 +00004906 case QEMU_OPTION_usbdevice:
4907 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00004908 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00004909 fprintf(stderr, "Too many USB devices\n");
4910 exit(1);
4911 }
pbrookfd5f3932008-03-26 20:55:43 +00004912 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00004913 usb_devices_index++;
4914 break;
bellard6a00d602005-11-21 23:25:50 +00004915 case QEMU_OPTION_smp:
4916 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00004917 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00004918 fprintf(stderr, "Invalid number of CPUs\n");
4919 exit(1);
4920 }
4921 break;
bellard24236862006-04-30 21:28:36 +00004922 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00004923 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00004924 break;
blueswir15824d652009-03-28 06:44:27 +00004925#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00004926 case QEMU_OPTION_no_acpi:
4927 acpi_enabled = 0;
4928 break;
aliguori16b29ae2008-12-17 23:28:44 +00004929 case QEMU_OPTION_no_hpet:
4930 no_hpet = 1;
4931 break;
blueswir15824d652009-03-28 06:44:27 +00004932#endif
bellardd1beab82006-10-02 19:44:22 +00004933 case QEMU_OPTION_no_reboot:
4934 no_reboot = 1;
4935 break;
aurel32b2f76162008-04-11 21:35:52 +00004936 case QEMU_OPTION_no_shutdown:
4937 no_shutdown = 1;
4938 break;
balrog9467cd42007-05-01 01:34:14 +00004939 case QEMU_OPTION_show_cursor:
4940 cursor_hide = 0;
4941 break;
blueswir18fcb1b92008-09-18 18:29:08 +00004942 case QEMU_OPTION_uuid:
4943 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
4944 fprintf(stderr, "Fail to parse UUID string."
4945 " Wrong format.\n");
4946 exit(1);
4947 }
4948 break;
blueswir15824d652009-03-28 06:44:27 +00004949#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004950 case QEMU_OPTION_daemonize:
4951 daemonize = 1;
4952 break;
blueswir15824d652009-03-28 06:44:27 +00004953#endif
ths9ae02552007-01-05 17:39:04 +00004954 case QEMU_OPTION_option_rom:
4955 if (nb_option_roms >= MAX_OPTION_ROMS) {
4956 fprintf(stderr, "Too many option ROMs\n");
4957 exit(1);
4958 }
4959 option_rom[nb_option_roms] = optarg;
4960 nb_option_roms++;
4961 break;
blueswir15824d652009-03-28 06:44:27 +00004962#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00004963 case QEMU_OPTION_semihosting:
4964 semihosting_enabled = 1;
4965 break;
blueswir15824d652009-03-28 06:44:27 +00004966#endif
thsc35734b2007-03-19 15:17:08 +00004967 case QEMU_OPTION_name:
4968 qemu_name = optarg;
4969 break;
blueswir195efd112008-12-24 20:26:14 +00004970#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00004971 case QEMU_OPTION_prom_env:
4972 if (nb_prom_envs >= MAX_PROM_ENVS) {
4973 fprintf(stderr, "Too many prom variables\n");
4974 exit(1);
4975 }
4976 prom_envs[nb_prom_envs] = optarg;
4977 nb_prom_envs++;
4978 break;
4979#endif
balrog2b8f2d42007-07-27 22:08:46 +00004980#ifdef TARGET_ARM
4981 case QEMU_OPTION_old_param:
4982 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00004983 break;
balrog2b8f2d42007-07-27 22:08:46 +00004984#endif
thsf3dcfad2007-08-24 01:26:02 +00004985 case QEMU_OPTION_clock:
4986 configure_alarms(optarg);
4987 break;
bellard7e0af5d02007-11-07 16:24:33 +00004988 case QEMU_OPTION_startdate:
4989 {
4990 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00004991 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00004992 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00004993 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00004994 } else {
4995 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4996 &tm.tm_year,
4997 &tm.tm_mon,
4998 &tm.tm_mday,
4999 &tm.tm_hour,
5000 &tm.tm_min,
5001 &tm.tm_sec) == 6) {
5002 /* OK */
5003 } else if (sscanf(optarg, "%d-%d-%d",
5004 &tm.tm_year,
5005 &tm.tm_mon,
5006 &tm.tm_mday) == 3) {
5007 tm.tm_hour = 0;
5008 tm.tm_min = 0;
5009 tm.tm_sec = 0;
5010 } else {
5011 goto date_fail;
5012 }
5013 tm.tm_year -= 1900;
5014 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005015 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005016 if (rtc_start_date == -1) {
5017 date_fail:
5018 fprintf(stderr, "Invalid date format. Valid format are:\n"
5019 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5020 exit(1);
5021 }
balrogf6503052008-02-17 11:42:19 +00005022 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005023 }
5024 }
5025 break;
bellard26a5f132008-05-28 12:30:31 +00005026 case QEMU_OPTION_tb_size:
5027 tb_size = strtol(optarg, NULL, 0);
5028 if (tb_size < 0)
5029 tb_size = 0;
5030 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005031 case QEMU_OPTION_icount:
5032 use_icount = 1;
5033 if (strcmp(optarg, "auto") == 0) {
5034 icount_time_shift = -1;
5035 } else {
5036 icount_time_shift = strtol(optarg, NULL, 0);
5037 }
5038 break;
aliguori5bb79102008-10-13 03:12:02 +00005039 case QEMU_OPTION_incoming:
5040 incoming = optarg;
5041 break;
blueswir15824d652009-03-28 06:44:27 +00005042#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005043 case QEMU_OPTION_chroot:
5044 chroot_dir = optarg;
5045 break;
5046 case QEMU_OPTION_runas:
5047 run_as = optarg;
5048 break;
blueswir15824d652009-03-28 06:44:27 +00005049#endif
aliguorie37630c2009-04-22 15:19:10 +00005050#ifdef CONFIG_XEN
5051 case QEMU_OPTION_xen_domid:
5052 xen_domid = atoi(optarg);
5053 break;
5054 case QEMU_OPTION_xen_create:
5055 xen_mode = XEN_CREATE;
5056 break;
5057 case QEMU_OPTION_xen_attach:
5058 xen_mode = XEN_ATTACH;
5059 break;
5060#endif
bellardcd6f1162004-05-13 22:02:20 +00005061 }
bellard0824d6f2003-06-24 13:42:40 +00005062 }
5063 }
bellard330d0412003-07-26 18:11:40 +00005064
blueswir1640f42e2009-04-19 10:18:01 +00005065#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
aliguori7ba1e612008-11-05 16:04:33 +00005066 if (kvm_allowed && kqemu_allowed) {
5067 fprintf(stderr,
5068 "You can not enable both KVM and kqemu at the same time\n");
5069 exit(1);
5070 }
5071#endif
5072
balrog3d878ca2008-10-28 10:59:59 +00005073 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005074 if (smp_cpus > machine->max_cpus) {
5075 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5076 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5077 machine->max_cpus);
5078 exit(1);
5079 }
5080
aliguoribc0129d2008-08-01 15:12:34 +00005081 if (nographic) {
5082 if (serial_device_index == 0)
5083 serial_devices[0] = "stdio";
5084 if (parallel_device_index == 0)
5085 parallel_devices[0] = "null";
5086 if (strncmp(monitor_device, "vc", 2) == 0)
5087 monitor_device = "stdio";
5088 }
5089
ths71e3ceb2006-12-22 02:11:31 +00005090#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005091 if (daemonize) {
5092 pid_t pid;
5093
5094 if (pipe(fds) == -1)
5095 exit(1);
5096
5097 pid = fork();
5098 if (pid > 0) {
5099 uint8_t status;
5100 ssize_t len;
5101
5102 close(fds[1]);
5103
5104 again:
ths93815bc2007-03-19 15:58:31 +00005105 len = read(fds[0], &status, 1);
5106 if (len == -1 && (errno == EINTR))
5107 goto again;
5108
5109 if (len != 1)
5110 exit(1);
5111 else if (status == 1) {
5112 fprintf(stderr, "Could not acquire pidfile\n");
5113 exit(1);
5114 } else
5115 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005116 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005117 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005118
5119 setsid();
5120
5121 pid = fork();
5122 if (pid > 0)
5123 exit(0);
5124 else if (pid < 0)
5125 exit(1);
5126
5127 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005128
5129 signal(SIGTSTP, SIG_IGN);
5130 signal(SIGTTOU, SIG_IGN);
5131 signal(SIGTTIN, SIG_IGN);
5132 }
ths71e3ceb2006-12-22 02:11:31 +00005133
thsaa26bb22007-03-25 21:33:06 +00005134 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005135 if (daemonize) {
5136 uint8_t status = 1;
5137 write(fds[1], &status, 1);
5138 } else
5139 fprintf(stderr, "Could not acquire pid file\n");
5140 exit(1);
5141 }
blueswir1b9e82a52009-04-05 18:03:31 +00005142#endif
ths93815bc2007-03-19 15:58:31 +00005143
blueswir1640f42e2009-04-19 10:18:01 +00005144#ifdef CONFIG_KQEMU
bellardff3fbb32006-01-08 10:53:14 +00005145 if (smp_cpus > 1)
5146 kqemu_allowed = 0;
5147#endif
bellarda20dd502003-09-30 21:07:02 +00005148 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00005149 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00005150
thsf8d39c02008-07-03 10:01:15 +00005151 if (!linux_boot && *kernel_cmdline != '\0') {
5152 fprintf(stderr, "-append only allowed with -kernel option\n");
5153 exit(1);
5154 }
5155
5156 if (!linux_boot && initrd_filename != NULL) {
5157 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5158 exit(1);
5159 }
5160
ths96d30e42007-01-07 20:42:14 +00005161 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00005162 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00005163 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00005164 }
bellardb118d612003-06-30 23:36:21 +00005165 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005166
pbrook634fce92006-07-15 17:40:09 +00005167 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005168 if (init_timer_alarm() < 0) {
5169 fprintf(stderr, "could not initialize alarm timer\n");
5170 exit(1);
5171 }
pbrook2e70f6e2008-06-29 01:03:05 +00005172 if (use_icount && icount_time_shift < 0) {
5173 use_icount = 2;
5174 /* 125MIPS seems a reasonable initial guess at the guest speed.
5175 It will be corrected fairly quickly anyway. */
5176 icount_time_shift = 3;
5177 init_icount_adjust();
5178 }
pbrook634fce92006-07-15 17:40:09 +00005179
bellardfd1dff42006-02-01 21:29:26 +00005180#ifdef _WIN32
5181 socket_init();
5182#endif
5183
bellard7c9d8e02005-11-15 22:16:05 +00005184 /* init network clients */
5185 if (nb_net_clients == 0) {
5186 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005187 net_clients[nb_net_clients++] = "nic";
5188#ifdef CONFIG_SLIRP
5189 net_clients[nb_net_clients++] = "user";
5190#endif
bellardc20709a2004-04-21 23:27:19 +00005191 }
5192
bellard7c9d8e02005-11-15 22:16:05 +00005193 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005194 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005195 exit(1);
bellard702c6512004-04-02 21:21:32 +00005196 }
aliguori63a01ef2008-10-31 19:10:00 +00005197 net_client_check();
bellardf1510b22003-06-25 00:07:40 +00005198
thseec85c22007-01-05 17:41:07 +00005199#ifdef TARGET_I386
balroged494d82007-12-11 23:23:52 +00005200 /* XXX: this should be moved in the PC machine instantiation code */
j_mayer28c5af52007-11-11 01:50:45 +00005201 if (net_boot != 0) {
5202 int netroms = 0;
5203 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00005204 const char *model = nd_table[i].model;
5205 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00005206 if (net_boot & (1 << i)) {
5207 if (model == NULL)
5208 model = "ne2k_pci";
5209 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5210 if (get_image_size(buf) > 0) {
5211 if (nb_option_roms >= MAX_OPTION_ROMS) {
5212 fprintf(stderr, "Too many option ROMs\n");
5213 exit(1);
5214 }
5215 option_rom[nb_option_roms] = strdup(buf);
5216 nb_option_roms++;
5217 netroms++;
5218 }
5219 }
thseec85c22007-01-05 17:41:07 +00005220 }
j_mayer28c5af52007-11-11 01:50:45 +00005221 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00005222 fprintf(stderr, "No valid PXE rom found for network device\n");
5223 exit(1);
5224 }
thseec85c22007-01-05 17:41:07 +00005225 }
5226#endif
5227
balrogdc72ac12008-11-09 00:04:26 +00005228 /* init the bluetooth world */
5229 for (i = 0; i < nb_bt_opts; i++)
5230 if (bt_parse(bt_opts[i]))
5231 exit(1);
5232
bellard0824d6f2003-06-24 13:42:40 +00005233 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005234 if (ram_size == 0)
5235 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005236
blueswir1640f42e2009-04-19 10:18:01 +00005237#ifdef CONFIG_KQEMU
pbrook94a6b542009-04-11 17:15:54 +00005238 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5239 guest ram allocation. It needs to go away. */
5240 if (kqemu_allowed) {
5241 kqemu_phys_ram_size = ram_size + VGA_RAM_SIZE + 4 * 1024 * 1024;
5242 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5243 if (!kqemu_phys_ram_base) {
5244 fprintf(stderr, "Could not allocate physical memory\n");
5245 exit(1);
5246 }
balrog7fb4fdc2008-04-24 17:59:27 +00005247 }
pbrook94a6b542009-04-11 17:15:54 +00005248#endif
bellard0824d6f2003-06-24 13:42:40 +00005249
bellard26a5f132008-05-28 12:30:31 +00005250 /* init the dynamic translator */
5251 cpu_exec_init_all(tb_size * 1024 * 1024);
5252
bellard5905b2e2004-08-01 21:53:26 +00005253 bdrv_init();
aliguori6512a2a2009-03-20 18:26:07 +00005254 dma_helper_init();
thse4bcb142007-12-02 04:51:10 +00005255
5256 /* we always create the cdrom drive, even if no disk is there */
5257
5258 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005259 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005260
balrog9d413d12007-12-04 00:10:34 +00005261 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005262
5263 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005264 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005265
balrog9d413d12007-12-04 00:10:34 +00005266 /* we always create one sd slot, even if no card is in it */
5267
5268 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005269 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005270
ths96d30e42007-01-07 20:42:14 +00005271 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005272
thse4bcb142007-12-02 04:51:10 +00005273 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005274 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005275 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005276
bellardc88676f2006-08-06 13:36:11 +00005277 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005278 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005279
aliguori3023f332009-01-16 19:04:14 +00005280#ifndef _WIN32
5281 /* must be after terminal init, SDL library changes signal handlers */
5282 termsig_setup();
5283#endif
5284
5285 /* Maintain compatibility with multiple stdio monitors */
5286 if (!strcmp(monitor_device,"stdio")) {
5287 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5288 const char *devname = serial_devices[i];
5289 if (devname && !strcmp(devname,"mon:stdio")) {
5290 monitor_device = NULL;
5291 break;
5292 } else if (devname && !strcmp(devname,"stdio")) {
5293 monitor_device = NULL;
5294 serial_devices[i] = "mon:stdio";
5295 break;
5296 }
5297 }
5298 }
5299
aliguori268a3622009-04-21 22:30:27 +00005300 if (nb_numa_nodes > 0) {
5301 int i;
5302
5303 if (nb_numa_nodes > smp_cpus) {
5304 nb_numa_nodes = smp_cpus;
5305 }
5306
5307 /* If no memory size if given for any node, assume the default case
5308 * and distribute the available memory equally across all nodes
5309 */
5310 for (i = 0; i < nb_numa_nodes; i++) {
5311 if (node_mem[i] != 0)
5312 break;
5313 }
5314 if (i == nb_numa_nodes) {
5315 uint64_t usedmem = 0;
5316
5317 /* On Linux, the each node's border has to be 8MB aligned,
5318 * the final node gets the rest.
5319 */
5320 for (i = 0; i < nb_numa_nodes - 1; i++) {
5321 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5322 usedmem += node_mem[i];
5323 }
5324 node_mem[i] = ram_size - usedmem;
5325 }
5326
5327 for (i = 0; i < nb_numa_nodes; i++) {
5328 if (node_cpumask[i] != 0)
5329 break;
5330 }
5331 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5332 * must cope with this anyway, because there are BIOSes out there in
5333 * real machines which also use this scheme.
5334 */
5335 if (i == nb_numa_nodes) {
5336 for (i = 0; i < smp_cpus; i++) {
5337 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5338 }
5339 }
5340 }
5341
aliguori3023f332009-01-16 19:04:14 +00005342 if (kvm_enabled()) {
5343 int ret;
5344
5345 ret = kvm_init(smp_cpus);
5346 if (ret < 0) {
5347 fprintf(stderr, "failed to initialize KVM\n");
5348 exit(1);
5349 }
5350 }
5351
aliguori4c621802009-01-16 21:48:20 +00005352 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005353 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005354 if (!monitor_hd) {
5355 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5356 exit(1);
5357 }
5358 }
5359
aliguori2796dae2009-01-16 20:23:27 +00005360 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5361 const char *devname = serial_devices[i];
5362 if (devname && strcmp(devname, "none")) {
5363 char label[32];
5364 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005365 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005366 if (!serial_hds[i]) {
5367 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5368 devname);
5369 exit(1);
5370 }
5371 }
5372 }
5373
5374 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5375 const char *devname = parallel_devices[i];
5376 if (devname && strcmp(devname, "none")) {
5377 char label[32];
5378 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005379 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005380 if (!parallel_hds[i]) {
5381 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5382 devname);
5383 exit(1);
5384 }
5385 }
5386 }
5387
5388 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5389 const char *devname = virtio_consoles[i];
5390 if (devname && strcmp(devname, "none")) {
5391 char label[32];
5392 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005393 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005394 if (!virtcon_hds[i]) {
5395 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5396 devname);
5397 exit(1);
5398 }
5399 }
5400 }
5401
aliguori3023f332009-01-16 19:04:14 +00005402 machine->init(ram_size, vga_ram_size, boot_devices,
5403 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5404
aliguori268a3622009-04-21 22:30:27 +00005405
5406 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5407 for (i = 0; i < nb_numa_nodes; i++) {
5408 if (node_cpumask[i] & (1 << env->cpu_index)) {
5409 env->numa_node = i;
5410 }
5411 }
5412 }
5413
aliguori6f338c32009-02-11 15:21:54 +00005414 current_machine = machine;
5415
aliguori3023f332009-01-16 19:04:14 +00005416 /* Set KVM's vcpu state to qemu's initial CPUState. */
5417 if (kvm_enabled()) {
5418 int ret;
5419
5420 ret = kvm_sync_vcpus();
5421 if (ret < 0) {
5422 fprintf(stderr, "failed to initialize vcpus\n");
5423 exit(1);
5424 }
5425 }
5426
5427 /* init USB devices */
5428 if (usb_enabled) {
5429 for(i = 0; i < usb_devices_index; i++) {
aliguoric0f4ce72009-03-05 23:01:01 +00005430 if (usb_device_add(usb_devices[i], 0) < 0) {
aliguori3023f332009-01-16 19:04:14 +00005431 fprintf(stderr, "Warning: could not add USB device %s\n",
5432 usb_devices[i]);
5433 }
5434 }
5435 }
5436
aliguori8f391ab2009-01-19 16:34:10 +00005437 if (!display_state)
5438 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005439 /* just use the first displaystate for the moment */
5440 ds = display_state;
bellard313aa562003-08-10 21:52:11 +00005441 /* terminal init */
bellarda20dd502003-09-30 21:07:02 +00005442 if (nographic) {
balrog4d3b6f62008-02-10 16:33:14 +00005443 if (curses) {
5444 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5445 exit(1);
5446 }
aliguori7d957bd2009-01-15 22:14:11 +00005447 } else {
balrog4d3b6f62008-02-10 16:33:14 +00005448#if defined(CONFIG_CURSES)
aliguori7d957bd2009-01-15 22:14:11 +00005449 if (curses) {
5450 /* At the moment curses cannot be used with other displays */
5451 curses_display_init(ds, full_screen);
5452 } else
balrog4d3b6f62008-02-10 16:33:14 +00005453#endif
aliguori7d957bd2009-01-15 22:14:11 +00005454 {
5455 if (vnc_display != NULL) {
5456 vnc_display_init(ds);
5457 if (vnc_display_open(ds, vnc_display) < 0)
5458 exit(1);
5459 }
bellard5b0753e2005-03-01 21:37:28 +00005460#if defined(CONFIG_SDL)
aliguorid268de02009-01-22 16:18:33 +00005461 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005462 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00005463#elif defined(CONFIG_COCOA)
aliguorid268de02009-01-22 16:18:33 +00005464 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005465 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00005466#endif
aliguori7d957bd2009-01-15 22:14:11 +00005467 }
bellard313aa562003-08-10 21:52:11 +00005468 }
aliguori7d957bd2009-01-15 22:14:11 +00005469 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005470
aliguori3023f332009-01-16 19:04:14 +00005471 dcl = ds->listeners;
5472 while (dcl != NULL) {
5473 if (dcl->dpy_refresh != NULL) {
5474 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5475 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005476 }
aliguori3023f332009-01-16 19:04:14 +00005477 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005478 }
aliguori3023f332009-01-16 19:04:14 +00005479
blueswir19043b622009-01-21 19:28:13 +00005480 if (nographic || (vnc_display && !sdl)) {
5481 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5482 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5483 }
5484
aliguori2796dae2009-01-16 20:23:27 +00005485 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00005486 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00005487
aliguori4c621802009-01-16 21:48:20 +00005488 if (monitor_device && monitor_hd)
aliguoricde76ee2009-03-05 23:01:51 +00005489 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
bellard82c643f2004-07-14 17:28:13 +00005490
bellard8d11df92004-08-24 21:13:40 +00005491 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005492 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005493 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005494 char label[32];
5495 snprintf(label, sizeof(label), "serial%d", i);
thsaf3a9032007-07-11 23:14:59 +00005496 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005497 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005498 }
bellard82c643f2004-07-14 17:28:13 +00005499 }
bellard82c643f2004-07-14 17:28:13 +00005500
bellard6508fe52005-01-15 12:02:56 +00005501 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005502 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005503 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005504 char label[32];
5505 snprintf(label, sizeof(label), "parallel%d", i);
thsaf3a9032007-07-11 23:14:59 +00005506 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005507 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005508 }
5509 }
5510
aliguori9ede2fd2009-01-15 20:05:25 +00005511 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5512 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005513 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005514 char label[32];
5515 snprintf(label, sizeof(label), "virtcon%d", i);
aliguori9ede2fd2009-01-15 20:05:25 +00005516 if (strstart(devname, "vc", 0))
5517 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5518 }
5519 }
5520
bellard67b915a2004-03-31 23:37:16 +00005521#ifdef CONFIG_GDBSTUB
aliguori59030a82009-04-05 18:43:41 +00005522 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
5523 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
5524 gdbstub_dev);
5525 exit(1);
balrog45669e02007-07-02 13:20:17 +00005526 }
bellard67b915a2004-03-31 23:37:16 +00005527#endif
balrog45669e02007-07-02 13:20:17 +00005528
bellardd63d3072004-10-03 13:29:03 +00005529 if (loadvm)
aliguori376253e2009-03-05 23:01:23 +00005530 do_loadvm(cur_mon, loadvm);
bellardd63d3072004-10-03 13:29:03 +00005531
aliguori5bb79102008-10-13 03:12:02 +00005532 if (incoming) {
5533 autostart = 0; /* fixme how to deal with -daemonize */
5534 qemu_start_incoming_migration(incoming);
5535 }
5536
aliguoric0f4ce72009-03-05 23:01:01 +00005537 if (autostart)
5538 vm_start();
thsffd843b2006-12-21 19:46:43 +00005539
blueswir1b9e82a52009-04-05 18:03:31 +00005540#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005541 if (daemonize) {
5542 uint8_t status = 0;
5543 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005544
5545 again1:
5546 len = write(fds[1], &status, 1);
5547 if (len == -1 && (errno == EINTR))
5548 goto again1;
5549
5550 if (len != 1)
5551 exit(1);
5552
aliguoribd54b862008-07-23 00:58:33 +00005553 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005554 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005555 if (fd == -1)
5556 exit(1);
aliguori08585322009-02-27 22:09:45 +00005557 }
ths71e3ceb2006-12-22 02:11:31 +00005558
aliguori08585322009-02-27 22:09:45 +00005559 if (run_as) {
5560 pwd = getpwnam(run_as);
5561 if (!pwd) {
5562 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5563 exit(1);
5564 }
5565 }
ths71e3ceb2006-12-22 02:11:31 +00005566
aliguori08585322009-02-27 22:09:45 +00005567 if (chroot_dir) {
5568 if (chroot(chroot_dir) < 0) {
5569 fprintf(stderr, "chroot failed\n");
5570 exit(1);
5571 }
5572 chdir("/");
5573 }
5574
5575 if (run_as) {
5576 if (setgid(pwd->pw_gid) < 0) {
5577 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5578 exit(1);
5579 }
5580 if (setuid(pwd->pw_uid) < 0) {
5581 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5582 exit(1);
5583 }
5584 if (setuid(0) != -1) {
5585 fprintf(stderr, "Dropping privileges failed\n");
5586 exit(1);
5587 }
5588 }
aliguori08585322009-02-27 22:09:45 +00005589
5590 if (daemonize) {
5591 dup2(fd, 0);
5592 dup2(fd, 1);
5593 dup2(fd, 2);
5594
5595 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00005596 }
blueswir1b9e82a52009-04-05 18:03:31 +00005597#endif
ths71e3ceb2006-12-22 02:11:31 +00005598
bellard8a7ddc32004-03-31 19:00:16 +00005599 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005600 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005601 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005602
bellard0824d6f2003-06-24 13:42:40 +00005603 return 0;
5604}