blob: f596553656ff324da382aacb85716dab7bb238a2 [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"
aurel325ef4efa2009-03-10 21:43:35 +0000142#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000143#include "net.h"
144#include "monitor.h"
145#include "console.h"
146#include "sysemu.h"
147#include "gdbstub.h"
148#include "qemu-timer.h"
149#include "qemu-char.h"
150#include "cache-utils.h"
151#include "block.h"
blueswir1a718ace2009-03-28 08:24:44 +0000152#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000153#include "audio/audio.h"
154#include "migration.h"
155#include "kvm.h"
156#include "balloon.h"
157
bellard0824d6f2003-06-24 13:42:40 +0000158#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000159
bellard8a7ddc32004-03-31 19:00:16 +0000160#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000161
blueswir1511d2b12009-03-07 15:32:56 +0000162#include "qemu_socket.h"
163
164#if defined(CONFIG_SLIRP)
165#include "libslirp.h"
166#endif
167
bellard0824d6f2003-06-24 13:42:40 +0000168//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000169//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000170//#define DEBUG_NET
171//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000172
aliguorid12d51d2009-01-15 21:48:06 +0000173
174#ifdef DEBUG_IOPORT
aliguori93fcfe32009-01-15 22:34:14 +0000175# define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
aliguorid12d51d2009-01-15 21:48:06 +0000176#else
177# define LOG_IOPORT(...) do { } while (0)
178#endif
179
bellard1bfe8562004-07-08 21:17:50 +0000180#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000181
pbrook0d92ed32006-05-21 16:30:15 +0000182/* Max number of USB devices that can be specified on the commandline. */
183#define MAX_USB_CMDLINE 8
184
balrogdc72ac12008-11-09 00:04:26 +0000185/* Max number of bluetooth switches on the commandline. */
186#define MAX_BT_CMDLINE 10
187
bellard7dea1da2003-11-16 15:59:30 +0000188/* XXX: use a two level table to limit memory usage */
189#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000190
bellard80cabfa2004-03-14 12:20:30 +0000191const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000192const char *bios_name = NULL;
blueswir1dbed7e42008-10-01 19:38:09 +0000193static void *ioport_opaque[MAX_IOPORTS];
194static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
195static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
thse4bcb142007-12-02 04:51:10 +0000196/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000197 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000198DriveInfo drives_table[MAX_DRIVES+1];
199int nb_drives;
blueswir1dbed7e42008-10-01 19:38:09 +0000200static int vga_ram_size;
malccb5a7aa2008-09-28 00:42:12 +0000201enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000202static DisplayState *display_state;
bellarda20dd502003-09-30 21:07:02 +0000203int nographic;
blueswir1dbed7e42008-10-01 19:38:09 +0000204static int curses;
aliguori7d957bd2009-01-15 22:14:11 +0000205static int sdl;
bellard3d11d0e2004-12-12 16:56:30 +0000206const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000207int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000208ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000209int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000210NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000211int vm_running;
aliguoric0f4ce72009-03-05 23:01:01 +0000212static int autostart;
balrogf6503052008-02-17 11:42:19 +0000213static int rtc_utc = 1;
214static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000215int cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +0000216int std_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000217int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000218#ifdef TARGET_SPARC
219int graphic_width = 1024;
220int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000221int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000222#else
bellard1bfe8562004-07-08 21:17:50 +0000223int graphic_width = 800;
224int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000225int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000226#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000227static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000228#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000229static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000230#endif
ths667acca2006-12-11 02:08:05 +0000231int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000232CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000233CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000234CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000235#ifdef TARGET_I386
236int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000237int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000238#endif
bellardbb36d472005-11-05 14:22:28 +0000239int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000240int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000241int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000242const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000243int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000244int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000245int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000246int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000247int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000248int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000249int graphic_rotate = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000250#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000251int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000252#endif
ths9ae02552007-01-05 17:39:04 +0000253const char *option_rom[MAX_OPTION_ROMS];
254int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000255int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000256#ifdef TARGET_ARM
257int old_param = 0;
258#endif
thsc35734b2007-03-19 15:17:08 +0000259const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000260int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000261#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000262unsigned int nb_prom_envs = 0;
263const char *prom_envs[MAX_PROM_ENVS];
264#endif
aliguoriec691c82009-02-11 15:20:37 +0000265int nb_drives_opt;
266struct drive_opt drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000267
aliguori268a3622009-04-21 22:30:27 +0000268int nb_numa_nodes;
269uint64_t node_mem[MAX_NODES];
270uint64_t node_cpumask[MAX_NODES];
271
balrogee5605e2007-12-03 03:01:40 +0000272static CPUState *cur_cpu;
273static CPUState *next_cpu;
balrog76ea08f2007-12-16 11:48:54 +0000274static int event_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000275/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000276static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000277/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000278#define MAX_ICOUNT_SHIFT 10
279/* Compensate for varying guest execution speed. */
280static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000281static QEMUTimer *icount_rt_timer;
282static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000283static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000284
blueswir18fcb1b92008-09-18 18:29:08 +0000285uint8_t qemu_uuid[16];
286
bellard0824d6f2003-06-24 13:42:40 +0000287/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000288/* x86 ISA bus support */
289
290target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000291PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000292
aliguori477e3ed2008-07-23 15:19:59 +0000293static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
294static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
295
296static uint32_t ioport_read(int index, uint32_t address)
297{
298 static IOPortReadFunc *default_func[3] = {
299 default_ioport_readb,
300 default_ioport_readw,
301 default_ioport_readl
302 };
303 IOPortReadFunc *func = ioport_read_table[index][address];
304 if (!func)
305 func = default_func[index];
306 return func(ioport_opaque[address], address);
307}
308
309static void ioport_write(int index, uint32_t address, uint32_t data)
310{
311 static IOPortWriteFunc *default_func[3] = {
312 default_ioport_writeb,
313 default_ioport_writew,
314 default_ioport_writel
315 };
316 IOPortWriteFunc *func = ioport_write_table[index][address];
317 if (!func)
318 func = default_func[index];
319 func(ioport_opaque[address], address, data);
320}
321
pbrook9596ebb2007-11-18 01:44:38 +0000322static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000323{
324#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000325 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000326#endif
bellardfc01f7e2003-06-30 10:03:06 +0000327 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000328}
329
pbrook9596ebb2007-11-18 01:44:38 +0000330static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000331{
332#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000333 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000334#endif
335}
336
337/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000338static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000339{
340 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000341 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000342 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000343 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000344 return data;
345}
346
pbrook9596ebb2007-11-18 01:44:38 +0000347static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000348{
aliguori477e3ed2008-07-23 15:19:59 +0000349 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000350 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000351 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000352}
353
pbrook9596ebb2007-11-18 01:44:38 +0000354static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000355{
356#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000357 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000358#endif
359 return 0xffffffff;
360}
361
pbrook9596ebb2007-11-18 01:44:38 +0000362static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000363{
364#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000365 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000366#endif
bellard0824d6f2003-06-24 13:42:40 +0000367}
368
bellardfc01f7e2003-06-30 10:03:06 +0000369/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000370int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000371 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000372{
bellardfc01f7e2003-06-30 10:03:06 +0000373 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000374
bellardc4b1fcc2004-03-14 21:44:30 +0000375 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000376 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000377 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000378 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000379 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000380 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000381 } else {
balrog88fdf562008-04-26 21:11:22 +0000382 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000383 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000384 }
385 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000386 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000387 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000388 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000389 ioport_opaque[i] = opaque;
390 }
bellard0824d6f2003-06-24 13:42:40 +0000391 return 0;
392}
393
bellardfc01f7e2003-06-30 10:03:06 +0000394/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000395int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000396 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000397{
bellardfc01f7e2003-06-30 10:03:06 +0000398 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000399
bellardc4b1fcc2004-03-14 21:44:30 +0000400 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000401 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000402 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000403 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000404 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000405 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000406 } else {
balrog88fdf562008-04-26 21:11:22 +0000407 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000408 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000409 }
410 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000411 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000412 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
413 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000414 ioport_opaque[i] = opaque;
415 }
bellardf1510b22003-06-25 00:07:40 +0000416 return 0;
417}
418
bellard69b91032004-05-18 23:05:28 +0000419void isa_unassign_ioport(int start, int length)
420{
421 int i;
422
423 for(i = start; i < start + length; i++) {
424 ioport_read_table[0][i] = default_ioport_readb;
425 ioport_read_table[1][i] = default_ioport_readw;
426 ioport_read_table[2][i] = default_ioport_readl;
427
428 ioport_write_table[0][i] = default_ioport_writeb;
429 ioport_write_table[1][i] = default_ioport_writew;
430 ioport_write_table[2][i] = default_ioport_writel;
aliguoria7607f72009-02-11 15:21:29 +0000431
432 ioport_opaque[i] = NULL;
bellard69b91032004-05-18 23:05:28 +0000433 }
434}
435
bellard20f32282005-01-03 23:36:21 +0000436/***********************************************************/
437
bellardc45886d2004-01-05 00:02:06 +0000438void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000439{
aliguorid12d51d2009-01-15 21:48:06 +0000440 LOG_IOPORT("outb: %04x %02x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000441 ioport_write(0, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000442#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000443 if (env)
444 env->last_io_time = cpu_get_time_fast();
445#endif
bellard0824d6f2003-06-24 13:42:40 +0000446}
447
bellardc45886d2004-01-05 00:02:06 +0000448void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000449{
aliguorid12d51d2009-01-15 21:48:06 +0000450 LOG_IOPORT("outw: %04x %04x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000451 ioport_write(1, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000452#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000453 if (env)
454 env->last_io_time = cpu_get_time_fast();
455#endif
bellard0824d6f2003-06-24 13:42:40 +0000456}
457
bellardc45886d2004-01-05 00:02:06 +0000458void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000459{
aliguorid12d51d2009-01-15 21:48:06 +0000460 LOG_IOPORT("outl: %04x %08x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000461 ioport_write(2, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000462#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000463 if (env)
464 env->last_io_time = cpu_get_time_fast();
465#endif
bellard0824d6f2003-06-24 13:42:40 +0000466}
467
bellardc45886d2004-01-05 00:02:06 +0000468int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000469{
bellardfd872592004-05-12 19:11:15 +0000470 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000471 val = ioport_read(0, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000472 LOG_IOPORT("inb : %04x %02x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000473#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000474 if (env)
475 env->last_io_time = cpu_get_time_fast();
476#endif
bellardfd872592004-05-12 19:11:15 +0000477 return val;
bellard0824d6f2003-06-24 13:42:40 +0000478}
479
bellardc45886d2004-01-05 00:02:06 +0000480int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000481{
bellardfd872592004-05-12 19:11:15 +0000482 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000483 val = ioport_read(1, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000484 LOG_IOPORT("inw : %04x %04x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000485#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000486 if (env)
487 env->last_io_time = cpu_get_time_fast();
488#endif
bellardfd872592004-05-12 19:11:15 +0000489 return val;
bellard0824d6f2003-06-24 13:42:40 +0000490}
491
bellardc45886d2004-01-05 00:02:06 +0000492int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000493{
bellardfd872592004-05-12 19:11:15 +0000494 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000495 val = ioport_read(2, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000496 LOG_IOPORT("inl : %04x %08x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000497#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000498 if (env)
499 env->last_io_time = cpu_get_time_fast();
500#endif
bellardfd872592004-05-12 19:11:15 +0000501 return val;
bellard0824d6f2003-06-24 13:42:40 +0000502}
503
504/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000505void hw_error(const char *fmt, ...)
506{
507 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000508 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000509
510 va_start(ap, fmt);
511 fprintf(stderr, "qemu: hardware error: ");
512 vfprintf(stderr, fmt, ap);
513 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000514 for(env = first_cpu; env != NULL; env = env->next_cpu) {
515 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000516#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000517 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000518#else
bellard6a00d602005-11-21 23:25:50 +0000519 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000520#endif
bellard6a00d602005-11-21 23:25:50 +0000521 }
bellard0824d6f2003-06-24 13:42:40 +0000522 va_end(ap);
523 abort();
524}
aliguoridf751fa2008-12-04 20:19:35 +0000525
526/***************/
527/* ballooning */
528
529static QEMUBalloonEvent *qemu_balloon_event;
530void *qemu_balloon_event_opaque;
531
532void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
533{
534 qemu_balloon_event = func;
535 qemu_balloon_event_opaque = opaque;
536}
537
538void qemu_balloon(ram_addr_t target)
539{
540 if (qemu_balloon_event)
541 qemu_balloon_event(qemu_balloon_event_opaque, target);
542}
543
544ram_addr_t qemu_balloon_status(void)
545{
546 if (qemu_balloon_event)
547 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
548 return 0;
549}
bellard0824d6f2003-06-24 13:42:40 +0000550
bellard8a7ddc32004-03-31 19:00:16 +0000551/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000552/* keyboard/mouse */
553
554static QEMUPutKBDEvent *qemu_put_kbd_event;
555static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000556static QEMUPutMouseEntry *qemu_put_mouse_event_head;
557static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000558
559void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
560{
561 qemu_put_kbd_event_opaque = opaque;
562 qemu_put_kbd_event = func;
563}
564
ths455204e2007-01-05 16:42:13 +0000565QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
566 void *opaque, int absolute,
567 const char *name)
bellard63066f42004-06-03 18:45:02 +0000568{
ths455204e2007-01-05 16:42:13 +0000569 QEMUPutMouseEntry *s, *cursor;
570
571 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000572
573 s->qemu_put_mouse_event = func;
574 s->qemu_put_mouse_event_opaque = opaque;
575 s->qemu_put_mouse_event_absolute = absolute;
576 s->qemu_put_mouse_event_name = qemu_strdup(name);
577 s->next = NULL;
578
579 if (!qemu_put_mouse_event_head) {
580 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
581 return s;
582 }
583
584 cursor = qemu_put_mouse_event_head;
585 while (cursor->next != NULL)
586 cursor = cursor->next;
587
588 cursor->next = s;
589 qemu_put_mouse_event_current = s;
590
591 return s;
592}
593
594void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
595{
596 QEMUPutMouseEntry *prev = NULL, *cursor;
597
598 if (!qemu_put_mouse_event_head || entry == NULL)
599 return;
600
601 cursor = qemu_put_mouse_event_head;
602 while (cursor != NULL && cursor != entry) {
603 prev = cursor;
604 cursor = cursor->next;
605 }
606
607 if (cursor == NULL) // does not exist or list empty
608 return;
609 else if (prev == NULL) { // entry is head
610 qemu_put_mouse_event_head = cursor->next;
611 if (qemu_put_mouse_event_current == entry)
612 qemu_put_mouse_event_current = cursor->next;
613 qemu_free(entry->qemu_put_mouse_event_name);
614 qemu_free(entry);
615 return;
616 }
617
618 prev->next = entry->next;
619
620 if (qemu_put_mouse_event_current == entry)
621 qemu_put_mouse_event_current = prev;
622
623 qemu_free(entry->qemu_put_mouse_event_name);
624 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000625}
626
627void kbd_put_keycode(int keycode)
628{
629 if (qemu_put_kbd_event) {
630 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
631 }
632}
633
634void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
635{
ths455204e2007-01-05 16:42:13 +0000636 QEMUPutMouseEvent *mouse_event;
637 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000638 int width;
ths455204e2007-01-05 16:42:13 +0000639
640 if (!qemu_put_mouse_event_current) {
641 return;
642 }
643
644 mouse_event =
645 qemu_put_mouse_event_current->qemu_put_mouse_event;
646 mouse_event_opaque =
647 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
648
649 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000650 if (graphic_rotate) {
651 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
652 width = 0x7fff;
653 else
aurel32b94ed572008-03-10 19:34:27 +0000654 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000655 mouse_event(mouse_event_opaque,
656 width - dy, dx, dz, buttons_state);
657 } else
658 mouse_event(mouse_event_opaque,
659 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000660 }
661}
662
bellard09b26c52006-04-12 21:09:08 +0000663int kbd_mouse_is_absolute(void)
664{
ths455204e2007-01-05 16:42:13 +0000665 if (!qemu_put_mouse_event_current)
666 return 0;
667
668 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
669}
670
aliguori376253e2009-03-05 23:01:23 +0000671void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000672{
673 QEMUPutMouseEntry *cursor;
674 int index = 0;
675
676 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000677 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000678 return;
679 }
680
aliguori376253e2009-03-05 23:01:23 +0000681 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000682 cursor = qemu_put_mouse_event_head;
683 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000684 monitor_printf(mon, "%c Mouse #%d: %s\n",
685 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
686 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000687 index++;
688 cursor = cursor->next;
689 }
690}
691
aliguori376253e2009-03-05 23:01:23 +0000692void do_mouse_set(Monitor *mon, int index)
ths455204e2007-01-05 16:42:13 +0000693{
694 QEMUPutMouseEntry *cursor;
695 int i = 0;
696
697 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000698 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000699 return;
700 }
701
702 cursor = qemu_put_mouse_event_head;
703 while (cursor != NULL && index != i) {
704 i++;
705 cursor = cursor->next;
706 }
707
708 if (cursor != NULL)
709 qemu_put_mouse_event_current = cursor;
710 else
aliguori376253e2009-03-05 23:01:23 +0000711 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000712}
713
bellard87858c82003-06-27 12:01:39 +0000714/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000715uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000716{
717 union {
718 uint64_t ll;
719 struct {
720#ifdef WORDS_BIGENDIAN
721 uint32_t high, low;
722#else
723 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000724#endif
bellard87858c82003-06-27 12:01:39 +0000725 } l;
726 } u, res;
727 uint64_t rl, rh;
728
729 u.ll = a;
730 rl = (uint64_t)u.l.low * (uint64_t)b;
731 rh = (uint64_t)u.l.high * (uint64_t)b;
732 rh += (rl >> 32);
733 res.l.high = rh / c;
734 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
735 return res.ll;
736}
737
bellard1dce7c32006-07-13 23:20:22 +0000738/***********************************************************/
739/* real time host monotonic timer */
740
741#define QEMU_TIMER_BASE 1000000000LL
742
743#ifdef WIN32
744
745static int64_t clock_freq;
746
747static void init_get_clock(void)
748{
bellarda8e5ac32006-07-14 09:36:13 +0000749 LARGE_INTEGER freq;
750 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000751 ret = QueryPerformanceFrequency(&freq);
752 if (ret == 0) {
753 fprintf(stderr, "Could not calibrate ticks\n");
754 exit(1);
755 }
756 clock_freq = freq.QuadPart;
757}
758
759static int64_t get_clock(void)
760{
761 LARGE_INTEGER ti;
762 QueryPerformanceCounter(&ti);
763 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
764}
765
766#else
767
768static int use_rt_clock;
769
770static void init_get_clock(void)
771{
772 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000773#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
774 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000775 {
776 struct timespec ts;
777 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
778 use_rt_clock = 1;
779 }
780 }
781#endif
782}
783
784static int64_t get_clock(void)
785{
blueswir1c5e97232009-03-07 20:06:23 +0000786#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
787 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000788 if (use_rt_clock) {
789 struct timespec ts;
790 clock_gettime(CLOCK_MONOTONIC, &ts);
791 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000792 } else
bellard1dce7c32006-07-13 23:20:22 +0000793#endif
794 {
795 /* XXX: using gettimeofday leads to problems if the date
796 changes, so it should be avoided. */
797 struct timeval tv;
798 gettimeofday(&tv, NULL);
799 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
800 }
801}
bellard1dce7c32006-07-13 23:20:22 +0000802#endif
803
pbrook2e70f6e2008-06-29 01:03:05 +0000804/* Return the virtual CPU time, based on the instruction counter. */
805static int64_t cpu_get_icount(void)
806{
807 int64_t icount;
808 CPUState *env = cpu_single_env;;
809 icount = qemu_icount;
810 if (env) {
811 if (!can_do_io(env))
812 fprintf(stderr, "Bad clock read\n");
813 icount -= (env->icount_decr.u16.low + env->icount_extra);
814 }
815 return qemu_icount_bias + (icount << icount_time_shift);
816}
817
bellard1dce7c32006-07-13 23:20:22 +0000818/***********************************************************/
819/* guest cycle counter */
820
821static int64_t cpu_ticks_prev;
822static int64_t cpu_ticks_offset;
823static int64_t cpu_clock_offset;
824static int cpu_ticks_enabled;
825
826/* return the host CPU cycle counter and handle stop/restart */
827int64_t cpu_get_ticks(void)
828{
pbrook2e70f6e2008-06-29 01:03:05 +0000829 if (use_icount) {
830 return cpu_get_icount();
831 }
bellard1dce7c32006-07-13 23:20:22 +0000832 if (!cpu_ticks_enabled) {
833 return cpu_ticks_offset;
834 } else {
835 int64_t ticks;
836 ticks = cpu_get_real_ticks();
837 if (cpu_ticks_prev > ticks) {
838 /* Note: non increasing ticks may happen if the host uses
839 software suspend */
840 cpu_ticks_offset += cpu_ticks_prev - ticks;
841 }
842 cpu_ticks_prev = ticks;
843 return ticks + cpu_ticks_offset;
844 }
845}
846
847/* return the host CPU monotonic timer and handle stop/restart */
848static int64_t cpu_get_clock(void)
849{
850 int64_t ti;
851 if (!cpu_ticks_enabled) {
852 return cpu_clock_offset;
853 } else {
854 ti = get_clock();
855 return ti + cpu_clock_offset;
856 }
857}
858
859/* enable cpu_get_ticks() */
860void cpu_enable_ticks(void)
861{
862 if (!cpu_ticks_enabled) {
863 cpu_ticks_offset -= cpu_get_real_ticks();
864 cpu_clock_offset -= get_clock();
865 cpu_ticks_enabled = 1;
866 }
867}
868
869/* disable cpu_get_ticks() : the clock is stopped. You must not call
870 cpu_get_ticks() after that. */
871void cpu_disable_ticks(void)
872{
873 if (cpu_ticks_enabled) {
874 cpu_ticks_offset = cpu_get_ticks();
875 cpu_clock_offset = cpu_get_clock();
876 cpu_ticks_enabled = 0;
877 }
878}
879
880/***********************************************************/
881/* timers */
ths5fafdf22007-09-16 21:08:06 +0000882
bellard8a7ddc32004-03-31 19:00:16 +0000883#define QEMU_TIMER_REALTIME 0
884#define QEMU_TIMER_VIRTUAL 1
885
886struct QEMUClock {
887 int type;
888 /* XXX: add frequency */
889};
890
891struct QEMUTimer {
892 QEMUClock *clock;
893 int64_t expire_time;
894 QEMUTimerCB *cb;
895 void *opaque;
896 struct QEMUTimer *next;
897};
898
thsc8994012007-08-19 21:56:03 +0000899struct qemu_alarm_timer {
900 char const *name;
thsefe75412007-08-24 01:36:32 +0000901 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000902
903 int (*start)(struct qemu_alarm_timer *t);
904 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000905 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000906 void *priv;
907};
908
thsefe75412007-08-24 01:36:32 +0000909#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000910#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000911
912static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
913{
914 return t->flags & ALARM_FLAG_DYNTICKS;
915}
916
917static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
918{
919 if (!alarm_has_dynticks(t))
920 return;
921
922 t->rearm(t);
923}
924
925/* TODO: MIN_TIMER_REARM_US should be optimized */
926#define MIN_TIMER_REARM_US 250
927
thsc8994012007-08-19 21:56:03 +0000928static struct qemu_alarm_timer *alarm_timer;
aliguorif49e58d2008-11-05 21:22:34 +0000929#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +0000930static int alarm_timer_rfd, alarm_timer_wfd;
aliguorif49e58d2008-11-05 21:22:34 +0000931#endif
thsc8994012007-08-19 21:56:03 +0000932
933#ifdef _WIN32
934
935struct qemu_alarm_win32 {
936 MMRESULT timerId;
937 HANDLE host_alarm;
938 unsigned int period;
939} alarm_win32_data = {0, NULL, -1};
940
941static int win32_start_timer(struct qemu_alarm_timer *t);
942static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000943static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000944
945#else
946
947static int unix_start_timer(struct qemu_alarm_timer *t);
948static void unix_stop_timer(struct qemu_alarm_timer *t);
949
ths231c6582007-08-26 17:29:15 +0000950#ifdef __linux__
951
thsefe75412007-08-24 01:36:32 +0000952static int dynticks_start_timer(struct qemu_alarm_timer *t);
953static void dynticks_stop_timer(struct qemu_alarm_timer *t);
954static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
955
thsc40ec5a2007-08-19 22:09:40 +0000956static int hpet_start_timer(struct qemu_alarm_timer *t);
957static void hpet_stop_timer(struct qemu_alarm_timer *t);
958
thsc8994012007-08-19 21:56:03 +0000959static int rtc_start_timer(struct qemu_alarm_timer *t);
960static void rtc_stop_timer(struct qemu_alarm_timer *t);
961
thsefe75412007-08-24 01:36:32 +0000962#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000963
964#endif /* _WIN32 */
965
pbrook2e70f6e2008-06-29 01:03:05 +0000966/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000967 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000968 When the guest is idle real and virtual time will be aligned in
969 the IO wait loop. */
970#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
971
972static void icount_adjust(void)
973{
974 int64_t cur_time;
975 int64_t cur_icount;
976 int64_t delta;
977 static int64_t last_delta;
978 /* If the VM is not running, then do nothing. */
979 if (!vm_running)
980 return;
981
982 cur_time = cpu_get_clock();
983 cur_icount = qemu_get_clock(vm_clock);
984 delta = cur_icount - cur_time;
985 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
986 if (delta > 0
987 && last_delta + ICOUNT_WOBBLE < delta * 2
988 && icount_time_shift > 0) {
989 /* The guest is getting too far ahead. Slow time down. */
990 icount_time_shift--;
991 }
992 if (delta < 0
993 && last_delta - ICOUNT_WOBBLE > delta * 2
994 && icount_time_shift < MAX_ICOUNT_SHIFT) {
995 /* The guest is getting too far behind. Speed time up. */
996 icount_time_shift++;
997 }
998 last_delta = delta;
999 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
1000}
1001
1002static void icount_adjust_rt(void * opaque)
1003{
1004 qemu_mod_timer(icount_rt_timer,
1005 qemu_get_clock(rt_clock) + 1000);
1006 icount_adjust();
1007}
1008
1009static void icount_adjust_vm(void * opaque)
1010{
1011 qemu_mod_timer(icount_vm_timer,
1012 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1013 icount_adjust();
1014}
1015
1016static void init_icount_adjust(void)
1017{
1018 /* Have both realtime and virtual time triggers for speed adjustment.
1019 The realtime trigger catches emulated time passing too slowly,
1020 the virtual time trigger catches emulated time passing too fast.
1021 Realtime triggers occur even when idle, so use them less frequently
1022 than VM triggers. */
1023 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1024 qemu_mod_timer(icount_rt_timer,
1025 qemu_get_clock(rt_clock) + 1000);
1026 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1027 qemu_mod_timer(icount_vm_timer,
1028 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1029}
1030
thsc8994012007-08-19 21:56:03 +00001031static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +00001032#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +00001033#ifdef __linux__
thsefe75412007-08-24 01:36:32 +00001034 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1035 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001036 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +00001037 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001038 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +00001039 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001040#endif
thsefe75412007-08-24 01:36:32 +00001041 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001042#else
thsefe75412007-08-24 01:36:32 +00001043 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1044 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1045 {"win32", 0, win32_start_timer,
1046 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +00001047#endif
1048 {NULL, }
1049};
1050
blueswir13f47aa82008-03-09 06:59:01 +00001051static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +00001052{
1053 int i;
1054
1055 printf("Available alarm timers, in order of precedence:\n");
1056 for (i = 0; alarm_timers[i].name; i++)
1057 printf("%s\n", alarm_timers[i].name);
1058}
1059
1060static void configure_alarms(char const *opt)
1061{
1062 int i;
1063 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +00001064 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +00001065 char *arg;
1066 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +00001067 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +00001068
aurel323adda042008-03-09 23:43:49 +00001069 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +00001070 show_available_alarms();
1071 exit(0);
1072 }
1073
1074 arg = strdup(opt);
1075
1076 /* Reorder the array */
1077 name = strtok(arg, ",");
1078 while (name) {
balroge2b577e2007-09-17 21:25:20 +00001079 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +00001080 if (!strcmp(alarm_timers[i].name, name))
1081 break;
1082 }
1083
1084 if (i == count) {
1085 fprintf(stderr, "Unknown clock %s\n", name);
1086 goto next;
1087 }
1088
1089 if (i < cur)
1090 /* Ignore */
1091 goto next;
1092
1093 /* Swap */
1094 tmp = alarm_timers[i];
1095 alarm_timers[i] = alarm_timers[cur];
1096 alarm_timers[cur] = tmp;
1097
1098 cur++;
1099next:
1100 name = strtok(NULL, ",");
1101 }
1102
1103 free(arg);
1104
1105 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +00001106 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +00001107 for (i = cur; i < count; i++)
1108 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +00001109 } else {
1110 show_available_alarms();
1111 exit(1);
thsf3dcfad2007-08-24 01:26:02 +00001112 }
thsf3dcfad2007-08-24 01:26:02 +00001113}
1114
bellard8a7ddc32004-03-31 19:00:16 +00001115QEMUClock *rt_clock;
1116QEMUClock *vm_clock;
1117
1118static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +00001119
pbrook9596ebb2007-11-18 01:44:38 +00001120static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001121{
1122 QEMUClock *clock;
1123 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +00001124 clock->type = type;
1125 return clock;
1126}
1127
1128QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1129{
1130 QEMUTimer *ts;
1131
1132 ts = qemu_mallocz(sizeof(QEMUTimer));
1133 ts->clock = clock;
1134 ts->cb = cb;
1135 ts->opaque = opaque;
1136 return ts;
1137}
1138
1139void qemu_free_timer(QEMUTimer *ts)
1140{
1141 qemu_free(ts);
1142}
1143
1144/* stop a timer, but do not dealloc it */
1145void qemu_del_timer(QEMUTimer *ts)
1146{
1147 QEMUTimer **pt, *t;
1148
1149 /* NOTE: this code must be signal safe because
1150 qemu_timer_expired() can be called from a signal. */
1151 pt = &active_timers[ts->clock->type];
1152 for(;;) {
1153 t = *pt;
1154 if (!t)
1155 break;
1156 if (t == ts) {
1157 *pt = t->next;
1158 break;
1159 }
1160 pt = &t->next;
1161 }
1162}
1163
1164/* modify the current timer so that it will be fired when current_time
1165 >= expire_time. The corresponding callback will be called. */
1166void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1167{
1168 QEMUTimer **pt, *t;
1169
1170 qemu_del_timer(ts);
1171
1172 /* add the timer in the sorted list */
1173 /* NOTE: this code must be signal safe because
1174 qemu_timer_expired() can be called from a signal. */
1175 pt = &active_timers[ts->clock->type];
1176 for(;;) {
1177 t = *pt;
1178 if (!t)
1179 break;
ths5fafdf22007-09-16 21:08:06 +00001180 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001181 break;
1182 pt = &t->next;
1183 }
1184 ts->expire_time = expire_time;
1185 ts->next = *pt;
1186 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001187
1188 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001189 if (pt == &active_timers[ts->clock->type]) {
1190 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1191 qemu_rearm_alarm_timer(alarm_timer);
1192 }
1193 /* Interrupt execution to force deadline recalculation. */
1194 if (use_icount && cpu_single_env) {
aurel323098dba2009-03-07 21:28:24 +00001195 cpu_exit(cpu_single_env);
pbrook2e70f6e2008-06-29 01:03:05 +00001196 }
1197 }
bellard8a7ddc32004-03-31 19:00:16 +00001198}
1199
1200int qemu_timer_pending(QEMUTimer *ts)
1201{
1202 QEMUTimer *t;
1203 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1204 if (t == ts)
1205 return 1;
1206 }
1207 return 0;
1208}
1209
1210static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1211{
1212 if (!timer_head)
1213 return 0;
1214 return (timer_head->expire_time <= current_time);
1215}
1216
1217static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1218{
1219 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001220
bellard8a7ddc32004-03-31 19:00:16 +00001221 for(;;) {
1222 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001223 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001224 break;
1225 /* remove timer from the list before calling the callback */
1226 *ptimer_head = ts->next;
1227 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001228
bellard8a7ddc32004-03-31 19:00:16 +00001229 /* run the callback (the timer list can be modified) */
1230 ts->cb(ts->opaque);
1231 }
1232}
1233
1234int64_t qemu_get_clock(QEMUClock *clock)
1235{
1236 switch(clock->type) {
1237 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001238 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001239 default:
1240 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001241 if (use_icount) {
1242 return cpu_get_icount();
1243 } else {
1244 return cpu_get_clock();
1245 }
bellard8a7ddc32004-03-31 19:00:16 +00001246 }
1247}
1248
bellard1dce7c32006-07-13 23:20:22 +00001249static void init_timers(void)
1250{
1251 init_get_clock();
1252 ticks_per_sec = QEMU_TIMER_BASE;
1253 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1254 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1255}
1256
bellard8a7ddc32004-03-31 19:00:16 +00001257/* save a timer */
1258void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1259{
1260 uint64_t expire_time;
1261
1262 if (qemu_timer_pending(ts)) {
1263 expire_time = ts->expire_time;
1264 } else {
1265 expire_time = -1;
1266 }
1267 qemu_put_be64(f, expire_time);
1268}
1269
1270void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1271{
1272 uint64_t expire_time;
1273
1274 expire_time = qemu_get_be64(f);
1275 if (expire_time != -1) {
1276 qemu_mod_timer(ts, expire_time);
1277 } else {
1278 qemu_del_timer(ts);
1279 }
1280}
1281
1282static void timer_save(QEMUFile *f, void *opaque)
1283{
1284 if (cpu_ticks_enabled) {
1285 hw_error("cannot save state if virtual timers are running");
1286 }
thsbee8d682007-12-16 23:41:11 +00001287 qemu_put_be64(f, cpu_ticks_offset);
1288 qemu_put_be64(f, ticks_per_sec);
1289 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001290}
1291
1292static int timer_load(QEMUFile *f, void *opaque, int version_id)
1293{
bellardc88676f2006-08-06 13:36:11 +00001294 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001295 return -EINVAL;
1296 if (cpu_ticks_enabled) {
1297 return -EINVAL;
1298 }
thsbee8d682007-12-16 23:41:11 +00001299 cpu_ticks_offset=qemu_get_be64(f);
1300 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001301 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001302 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001303 }
bellard8a7ddc32004-03-31 19:00:16 +00001304 return 0;
1305}
1306
bellard67b915a2004-03-31 23:37:16 +00001307#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001308static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1309 DWORD_PTR dwUser, DWORD_PTR dw1,
1310 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001311#else
bellard8a7ddc32004-03-31 19:00:16 +00001312static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001313#endif
bellard8a7ddc32004-03-31 19:00:16 +00001314{
bellard02ba45c2004-06-25 14:46:23 +00001315#if 0
1316#define DISP_FREQ 1000
1317 {
1318 static int64_t delta_min = INT64_MAX;
1319 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1320 static int count;
1321 ti = qemu_get_clock(vm_clock);
1322 if (last_clock != 0) {
1323 delta = ti - last_clock;
1324 if (delta < delta_min)
1325 delta_min = delta;
1326 if (delta > delta_max)
1327 delta_max = delta;
1328 delta_cum += delta;
1329 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001330 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001331 muldiv64(delta_min, 1000000, ticks_per_sec),
1332 muldiv64(delta_max, 1000000, ticks_per_sec),
1333 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1334 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1335 count = 0;
1336 delta_min = INT64_MAX;
1337 delta_max = 0;
1338 delta_cum = 0;
1339 }
1340 }
1341 last_clock = ti;
1342 }
1343#endif
thsefe75412007-08-24 01:36:32 +00001344 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001345 (!use_icount &&
1346 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1347 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001348 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1349 qemu_get_clock(rt_clock))) {
aliguoric96f1a42008-11-05 20:29:45 +00001350 CPUState *env = next_cpu;
aliguoric96f1a42008-11-05 20:29:45 +00001351
bellard06d9f2f2006-05-01 13:23:04 +00001352#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001353 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1354 SetEvent(data->host_alarm);
aliguorif49e58d2008-11-05 21:22:34 +00001355#else
1356 static const char byte = 0;
aliguoric96f1a42008-11-05 20:29:45 +00001357 write(alarm_timer_wfd, &byte, sizeof(byte));
aliguorif49e58d2008-11-05 21:22:34 +00001358#endif
balrogd5d08332008-01-05 19:41:47 +00001359 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1360
balrog4f8eb8d2007-12-16 12:39:38 +00001361 if (env) {
1362 /* stop the currently executing cpu because a timer occured */
aurel323098dba2009-03-07 21:28:24 +00001363 cpu_exit(env);
blueswir1640f42e2009-04-19 10:18:01 +00001364#ifdef CONFIG_KQEMU
balrog4f8eb8d2007-12-16 12:39:38 +00001365 if (env->kqemu_enabled) {
1366 kqemu_cpu_interrupt(env);
1367 }
balrogee5605e2007-12-03 03:01:40 +00001368#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001369 }
balrogee5605e2007-12-03 03:01:40 +00001370 event_pending = 1;
bellard8a7ddc32004-03-31 19:00:16 +00001371 }
1372}
1373
pbrook2e70f6e2008-06-29 01:03:05 +00001374static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001375{
pbrook2e70f6e2008-06-29 01:03:05 +00001376 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001377
1378 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001379 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1380 qemu_get_clock(vm_clock);
1381 } else {
1382 /* To avoid problems with overflow limit this to 2^32. */
1383 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001384 }
1385
pbrook2e70f6e2008-06-29 01:03:05 +00001386 if (delta < 0)
1387 delta = 0;
thsefe75412007-08-24 01:36:32 +00001388
pbrook2e70f6e2008-06-29 01:03:05 +00001389 return delta;
1390}
1391
blueswir18632fb92008-09-14 13:59:34 +00001392#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001393static uint64_t qemu_next_deadline_dyntick(void)
1394{
1395 int64_t delta;
1396 int64_t rtdelta;
1397
1398 if (use_icount)
1399 delta = INT32_MAX;
1400 else
1401 delta = (qemu_next_deadline() + 999) / 1000;
1402
1403 if (active_timers[QEMU_TIMER_REALTIME]) {
1404 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1405 qemu_get_clock(rt_clock))*1000;
1406 if (rtdelta < delta)
1407 delta = rtdelta;
1408 }
1409
1410 if (delta < MIN_TIMER_REARM_US)
1411 delta = MIN_TIMER_REARM_US;
1412
1413 return delta;
thsefe75412007-08-24 01:36:32 +00001414}
blueswir18632fb92008-09-14 13:59:34 +00001415#endif
thsefe75412007-08-24 01:36:32 +00001416
bellardfd872592004-05-12 19:11:15 +00001417#ifndef _WIN32
1418
aliguori7183b4b2008-11-05 20:40:18 +00001419/* Sets a specific flag */
1420static int fcntl_setfl(int fd, int flag)
1421{
1422 int flags;
1423
1424 flags = fcntl(fd, F_GETFL);
1425 if (flags == -1)
1426 return -errno;
1427
1428 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1429 return -errno;
1430
1431 return 0;
1432}
1433
bellard829309c2004-05-20 13:20:12 +00001434#if defined(__linux__)
1435
bellardfd872592004-05-12 19:11:15 +00001436#define RTC_FREQ 1024
1437
aurel32de9a95f2008-11-11 13:41:01 +00001438static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001439{
thsc8994012007-08-19 21:56:03 +00001440 struct sigaction act;
1441
1442 /* timer signal */
1443 sigfillset(&act.sa_mask);
1444 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001445 act.sa_handler = host_alarm_handler;
1446
1447 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001448 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001449 fcntl(fd, F_SETOWN, getpid());
1450}
1451
thsc40ec5a2007-08-19 22:09:40 +00001452static int hpet_start_timer(struct qemu_alarm_timer *t)
1453{
1454 struct hpet_info info;
1455 int r, fd;
1456
1457 fd = open("/dev/hpet", O_RDONLY);
1458 if (fd < 0)
1459 return -1;
1460
1461 /* Set frequency */
1462 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1463 if (r < 0) {
1464 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1465 "error, but for better emulation accuracy type:\n"
1466 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1467 goto fail;
1468 }
1469
1470 /* Check capabilities */
1471 r = ioctl(fd, HPET_INFO, &info);
1472 if (r < 0)
1473 goto fail;
1474
1475 /* Enable periodic mode */
1476 r = ioctl(fd, HPET_EPI, 0);
1477 if (info.hi_flags && (r < 0))
1478 goto fail;
1479
1480 /* Enable interrupt */
1481 r = ioctl(fd, HPET_IE_ON, 0);
1482 if (r < 0)
1483 goto fail;
1484
1485 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001486 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001487
1488 return 0;
1489fail:
1490 close(fd);
1491 return -1;
1492}
1493
1494static void hpet_stop_timer(struct qemu_alarm_timer *t)
1495{
pbrookfcdc2122007-08-23 20:22:22 +00001496 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001497
1498 close(fd);
1499}
1500
thsc8994012007-08-19 21:56:03 +00001501static int rtc_start_timer(struct qemu_alarm_timer *t)
1502{
1503 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001504 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001505
balrogaeb30be2007-07-02 15:03:13 +00001506 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001507 if (rtc_fd < 0)
1508 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001509 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1510 if (current_rtc_freq != RTC_FREQ &&
1511 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001512 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1513 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1514 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1515 goto fail;
1516 }
1517 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1518 fail:
1519 close(rtc_fd);
1520 return -1;
1521 }
thsc8994012007-08-19 21:56:03 +00001522
1523 enable_sigio_timer(rtc_fd);
1524
pbrookfcdc2122007-08-23 20:22:22 +00001525 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001526
bellardfd872592004-05-12 19:11:15 +00001527 return 0;
1528}
1529
thsc8994012007-08-19 21:56:03 +00001530static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001531{
pbrookfcdc2122007-08-23 20:22:22 +00001532 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001533
1534 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001535}
1536
thsefe75412007-08-24 01:36:32 +00001537static int dynticks_start_timer(struct qemu_alarm_timer *t)
1538{
1539 struct sigevent ev;
1540 timer_t host_timer;
1541 struct sigaction act;
1542
1543 sigfillset(&act.sa_mask);
1544 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001545 act.sa_handler = host_alarm_handler;
1546
1547 sigaction(SIGALRM, &act, NULL);
1548
1549 ev.sigev_value.sival_int = 0;
1550 ev.sigev_notify = SIGEV_SIGNAL;
1551 ev.sigev_signo = SIGALRM;
1552
1553 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1554 perror("timer_create");
1555
1556 /* disable dynticks */
1557 fprintf(stderr, "Dynamic Ticks disabled\n");
1558
1559 return -1;
1560 }
1561
blueswir10399bfe2008-11-16 11:37:18 +00001562 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001563
1564 return 0;
1565}
1566
1567static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1568{
blueswir10399bfe2008-11-16 11:37:18 +00001569 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001570
1571 timer_delete(host_timer);
1572}
1573
1574static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1575{
blueswir10399bfe2008-11-16 11:37:18 +00001576 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001577 struct itimerspec timeout;
1578 int64_t nearest_delta_us = INT64_MAX;
1579 int64_t current_us;
1580
1581 if (!active_timers[QEMU_TIMER_REALTIME] &&
1582 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001583 return;
thsefe75412007-08-24 01:36:32 +00001584
pbrook2e70f6e2008-06-29 01:03:05 +00001585 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001586
1587 /* check whether a timer is already running */
1588 if (timer_gettime(host_timer, &timeout)) {
1589 perror("gettime");
1590 fprintf(stderr, "Internal timer error: aborting\n");
1591 exit(1);
1592 }
1593 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1594 if (current_us && current_us <= nearest_delta_us)
1595 return;
1596
1597 timeout.it_interval.tv_sec = 0;
1598 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1599 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1600 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1601 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1602 perror("settime");
1603 fprintf(stderr, "Internal timer error: aborting\n");
1604 exit(1);
1605 }
1606}
1607
ths70744b32007-08-26 17:31:30 +00001608#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001609
thsc8994012007-08-19 21:56:03 +00001610static int unix_start_timer(struct qemu_alarm_timer *t)
1611{
1612 struct sigaction act;
1613 struct itimerval itv;
1614 int err;
1615
1616 /* timer signal */
1617 sigfillset(&act.sa_mask);
1618 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001619 act.sa_handler = host_alarm_handler;
1620
1621 sigaction(SIGALRM, &act, NULL);
1622
1623 itv.it_interval.tv_sec = 0;
1624 /* for i386 kernel 2.6 to get 1 ms */
1625 itv.it_interval.tv_usec = 999;
1626 itv.it_value.tv_sec = 0;
1627 itv.it_value.tv_usec = 10 * 1000;
1628
1629 err = setitimer(ITIMER_REAL, &itv, NULL);
1630 if (err)
1631 return -1;
1632
1633 return 0;
1634}
1635
1636static void unix_stop_timer(struct qemu_alarm_timer *t)
1637{
1638 struct itimerval itv;
1639
1640 memset(&itv, 0, sizeof(itv));
1641 setitimer(ITIMER_REAL, &itv, NULL);
1642}
1643
bellard829309c2004-05-20 13:20:12 +00001644#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001645
aliguorif49e58d2008-11-05 21:22:34 +00001646static void try_to_rearm_timer(void *opaque)
1647{
1648 struct qemu_alarm_timer *t = opaque;
1649#ifndef _WIN32
1650 ssize_t len;
1651
1652 /* Drain the notify pipe */
1653 do {
1654 char buffer[512];
1655 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1656 } while ((len == -1 && errno == EINTR) || len > 0);
1657#endif
1658
aliguorif49e58d2008-11-05 21:22:34 +00001659 if (t->flags & ALARM_FLAG_EXPIRED) {
1660 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1661 qemu_rearm_alarm_timer(alarm_timer);
1662 }
1663}
1664
thsc8994012007-08-19 21:56:03 +00001665#ifdef _WIN32
1666
1667static int win32_start_timer(struct qemu_alarm_timer *t)
1668{
1669 TIMECAPS tc;
1670 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001671 UINT flags;
thsc8994012007-08-19 21:56:03 +00001672
1673 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1674 if (!data->host_alarm) {
1675 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001676 return -1;
thsc8994012007-08-19 21:56:03 +00001677 }
1678
1679 memset(&tc, 0, sizeof(tc));
1680 timeGetDevCaps(&tc, sizeof(tc));
1681
1682 if (data->period < tc.wPeriodMin)
1683 data->period = tc.wPeriodMin;
1684
1685 timeBeginPeriod(data->period);
1686
thsefe75412007-08-24 01:36:32 +00001687 flags = TIME_CALLBACK_FUNCTION;
1688 if (alarm_has_dynticks(t))
1689 flags |= TIME_ONESHOT;
1690 else
1691 flags |= TIME_PERIODIC;
1692
thsc8994012007-08-19 21:56:03 +00001693 data->timerId = timeSetEvent(1, // interval (ms)
1694 data->period, // resolution
1695 host_alarm_handler, // function
1696 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001697 flags);
thsc8994012007-08-19 21:56:03 +00001698
1699 if (!data->timerId) {
1700 perror("Failed to initialize win32 alarm timer");
1701
1702 timeEndPeriod(data->period);
1703 CloseHandle(data->host_alarm);
1704 return -1;
1705 }
1706
aliguorif49e58d2008-11-05 21:22:34 +00001707 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
thsc8994012007-08-19 21:56:03 +00001708
1709 return 0;
1710}
1711
1712static void win32_stop_timer(struct qemu_alarm_timer *t)
1713{
1714 struct qemu_alarm_win32 *data = t->priv;
1715
1716 timeKillEvent(data->timerId);
1717 timeEndPeriod(data->period);
1718
1719 CloseHandle(data->host_alarm);
1720}
1721
thsefe75412007-08-24 01:36:32 +00001722static void win32_rearm_timer(struct qemu_alarm_timer *t)
1723{
1724 struct qemu_alarm_win32 *data = t->priv;
1725 uint64_t nearest_delta_us;
1726
1727 if (!active_timers[QEMU_TIMER_REALTIME] &&
1728 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001729 return;
thsefe75412007-08-24 01:36:32 +00001730
pbrook2e70f6e2008-06-29 01:03:05 +00001731 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001732 nearest_delta_us /= 1000;
1733
1734 timeKillEvent(data->timerId);
1735
1736 data->timerId = timeSetEvent(1,
1737 data->period,
1738 host_alarm_handler,
1739 (DWORD)t,
1740 TIME_ONESHOT | TIME_PERIODIC);
1741
1742 if (!data->timerId) {
1743 perror("Failed to re-arm win32 alarm timer");
1744
1745 timeEndPeriod(data->period);
1746 CloseHandle(data->host_alarm);
1747 exit(1);
1748 }
1749}
1750
thsc8994012007-08-19 21:56:03 +00001751#endif /* _WIN32 */
1752
aliguori7183b4b2008-11-05 20:40:18 +00001753static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001754{
blueswir1223f0d72008-09-30 18:12:18 +00001755 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001756 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001757
1758#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +00001759 int fds[2];
1760
aliguori7183b4b2008-11-05 20:40:18 +00001761 err = pipe(fds);
1762 if (err == -1)
1763 return -errno;
1764
1765 err = fcntl_setfl(fds[0], O_NONBLOCK);
1766 if (err < 0)
1767 goto fail;
1768
1769 err = fcntl_setfl(fds[1], O_NONBLOCK);
1770 if (err < 0)
1771 goto fail;
1772
aliguoric96f1a42008-11-05 20:29:45 +00001773 alarm_timer_rfd = fds[0];
1774 alarm_timer_wfd = fds[1];
aliguorif49e58d2008-11-05 21:22:34 +00001775#endif
bellard06d9f2f2006-05-01 13:23:04 +00001776
thsc8994012007-08-19 21:56:03 +00001777 for (i = 0; alarm_timers[i].name; i++) {
1778 t = &alarm_timers[i];
1779
thsc8994012007-08-19 21:56:03 +00001780 err = t->start(t);
1781 if (!err)
1782 break;
bellard67b915a2004-03-31 23:37:16 +00001783 }
bellardfd872592004-05-12 19:11:15 +00001784
thsc8994012007-08-19 21:56:03 +00001785 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001786 err = -ENOENT;
1787 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001788 }
thsc8994012007-08-19 21:56:03 +00001789
aliguorif49e58d2008-11-05 21:22:34 +00001790#ifndef _WIN32
aliguori6abfbd72008-11-05 20:49:37 +00001791 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1792 try_to_rearm_timer, NULL, t);
aliguorif49e58d2008-11-05 21:22:34 +00001793#endif
aliguori6abfbd72008-11-05 20:49:37 +00001794
thsc8994012007-08-19 21:56:03 +00001795 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001796
aliguori6abfbd72008-11-05 20:49:37 +00001797 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001798
1799fail:
aliguorif49e58d2008-11-05 21:22:34 +00001800#ifndef _WIN32
aliguori7183b4b2008-11-05 20:40:18 +00001801 close(fds[0]);
1802 close(fds[1]);
aliguorif49e58d2008-11-05 21:22:34 +00001803#endif
aliguori7183b4b2008-11-05 20:40:18 +00001804 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001805}
1806
pbrook9596ebb2007-11-18 01:44:38 +00001807static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001808{
thsc8994012007-08-19 21:56:03 +00001809 alarm_timer->stop(alarm_timer);
1810 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001811}
1812
bellardc4b1fcc2004-03-14 21:44:30 +00001813/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001814/* host time/date access */
1815void qemu_get_timedate(struct tm *tm, int offset)
1816{
1817 time_t ti;
1818 struct tm *ret;
1819
1820 time(&ti);
1821 ti += offset;
1822 if (rtc_date_offset == -1) {
1823 if (rtc_utc)
1824 ret = gmtime(&ti);
1825 else
1826 ret = localtime(&ti);
1827 } else {
1828 ti -= rtc_date_offset;
1829 ret = gmtime(&ti);
1830 }
1831
1832 memcpy(tm, ret, sizeof(struct tm));
1833}
1834
1835int qemu_timedate_diff(struct tm *tm)
1836{
1837 time_t seconds;
1838
1839 if (rtc_date_offset == -1)
1840 if (rtc_utc)
1841 seconds = mktimegm(tm);
1842 else
1843 seconds = mktime(tm);
1844 else
1845 seconds = mktimegm(tm) + rtc_date_offset;
1846
1847 return seconds - time(NULL);
1848}
1849
bellardfd1dff42006-02-01 21:29:26 +00001850#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001851static void socket_cleanup(void)
1852{
1853 WSACleanup();
1854}
bellard82c643f2004-07-14 17:28:13 +00001855
bellardfd1dff42006-02-01 21:29:26 +00001856static int socket_init(void)
1857{
1858 WSADATA Data;
1859 int ret, err;
1860
1861 ret = WSAStartup(MAKEWORD(2,2), &Data);
1862 if (ret != 0) {
1863 err = WSAGetLastError();
1864 fprintf(stderr, "WSAStartup: %d\n", err);
1865 return -1;
1866 }
1867 atexit(socket_cleanup);
1868 return 0;
1869}
aurel3264b7b732008-05-05 10:05:31 +00001870#endif
1871
aliguori268a3622009-04-21 22:30:27 +00001872const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
thse4bcb142007-12-02 04:51:10 +00001873{
1874 char *q;
thse4bcb142007-12-02 04:51:10 +00001875
balrog609497a2008-01-14 02:56:53 +00001876 q = buf;
aliguori268a3622009-04-21 22:30:27 +00001877 while (*p != '\0' && *p != delim) {
balrog609497a2008-01-14 02:56:53 +00001878 if (q && (q - buf) < buf_size - 1)
1879 *q++ = *p;
1880 p++;
1881 }
1882 if (q)
1883 *q = '\0';
1884
1885 return p;
1886}
1887
aliguori63a01ef2008-10-31 19:10:00 +00001888const char *get_opt_value(char *buf, int buf_size, const char *p)
balrog609497a2008-01-14 02:56:53 +00001889{
1890 char *q;
1891
thse4bcb142007-12-02 04:51:10 +00001892 q = buf;
1893 while (*p != '\0') {
balrog609497a2008-01-14 02:56:53 +00001894 if (*p == ',') {
1895 if (*(p + 1) != ',')
thse4bcb142007-12-02 04:51:10 +00001896 break;
thse4bcb142007-12-02 04:51:10 +00001897 p++;
balrog609497a2008-01-14 02:56:53 +00001898 }
thse4bcb142007-12-02 04:51:10 +00001899 if (q && (q - buf) < buf_size - 1)
1900 *q++ = *p;
1901 p++;
1902 }
1903 if (q)
1904 *q = '\0';
1905
1906 return p;
1907}
1908
aliguori63a01ef2008-10-31 19:10:00 +00001909int get_param_value(char *buf, int buf_size,
1910 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001911{
1912 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001913 char option[128];
1914
1915 p = str;
1916 for(;;) {
aliguori268a3622009-04-21 22:30:27 +00001917 p = get_opt_name(option, sizeof(option), p, '=');
bellard7c9d8e02005-11-15 22:16:05 +00001918 if (*p != '=')
1919 break;
1920 p++;
1921 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001922 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001923 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001924 } else {
balrog609497a2008-01-14 02:56:53 +00001925 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001926 }
1927 if (*p != ',')
1928 break;
1929 p++;
1930 }
1931 return 0;
1932}
1933
aliguori63a01ef2008-10-31 19:10:00 +00001934int check_params(char *buf, int buf_size,
1935 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001936{
1937 const char *p;
1938 int i;
1939
1940 p = str;
aliguori10300212009-04-21 19:56:23 +00001941 while (*p != '\0') {
aliguori268a3622009-04-21 22:30:27 +00001942 p = get_opt_name(buf, buf_size, p, '=');
thse4bcb142007-12-02 04:51:10 +00001943 if (*p != '=')
1944 return -1;
1945 p++;
1946 for(i = 0; params[i] != NULL; i++)
1947 if (!strcmp(params[i], buf))
1948 break;
1949 if (params[i] == NULL)
1950 return -1;
balrog609497a2008-01-14 02:56:53 +00001951 p = get_opt_value(NULL, 0, p);
thse4bcb142007-12-02 04:51:10 +00001952 if (*p != ',')
1953 break;
1954 p++;
1955 }
1956 return 0;
1957}
1958
balrog1ae26a12008-09-28 23:19:47 +00001959/***********************************************************/
1960/* Bluetooth support */
1961static int nb_hcis;
1962static int cur_hci;
1963static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001964
balrog1ae26a12008-09-28 23:19:47 +00001965static struct bt_vlan_s {
1966 struct bt_scatternet_s net;
1967 int id;
1968 struct bt_vlan_s *next;
1969} *first_bt_vlan;
1970
1971/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001972static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001973{
1974 struct bt_vlan_s **pvlan, *vlan;
1975 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1976 if (vlan->id == id)
1977 return &vlan->net;
1978 }
1979 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1980 vlan->id = id;
1981 pvlan = &first_bt_vlan;
1982 while (*pvlan != NULL)
1983 pvlan = &(*pvlan)->next;
1984 *pvlan = vlan;
1985 return &vlan->net;
1986}
1987
1988static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1989{
1990}
1991
1992static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1993{
1994 return -ENOTSUP;
1995}
1996
1997static struct HCIInfo null_hci = {
1998 .cmd_send = null_hci_send,
1999 .sco_send = null_hci_send,
2000 .acl_send = null_hci_send,
2001 .bdaddr_set = null_hci_addr_set,
2002};
2003
2004struct HCIInfo *qemu_next_hci(void)
2005{
2006 if (cur_hci == nb_hcis)
2007 return &null_hci;
2008
2009 return hci_table[cur_hci++];
2010}
2011
balrogdc72ac12008-11-09 00:04:26 +00002012static struct HCIInfo *hci_init(const char *str)
2013{
2014 char *endp;
2015 struct bt_scatternet_s *vlan = 0;
2016
2017 if (!strcmp(str, "null"))
2018 /* null */
2019 return &null_hci;
2020 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
2021 /* host[:hciN] */
2022 return bt_host_hci(str[4] ? str + 5 : "hci0");
2023 else if (!strncmp(str, "hci", 3)) {
2024 /* hci[,vlan=n] */
2025 if (str[3]) {
2026 if (!strncmp(str + 3, ",vlan=", 6)) {
2027 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2028 if (*endp)
2029 vlan = 0;
2030 }
2031 } else
2032 vlan = qemu_find_bt_vlan(0);
2033 if (vlan)
2034 return bt_new_hci(vlan);
2035 }
2036
2037 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2038
2039 return 0;
2040}
2041
2042static int bt_hci_parse(const char *str)
2043{
2044 struct HCIInfo *hci;
2045 bdaddr_t bdaddr;
2046
2047 if (nb_hcis >= MAX_NICS) {
2048 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2049 return -1;
2050 }
2051
2052 hci = hci_init(str);
2053 if (!hci)
2054 return -1;
2055
2056 bdaddr.b[0] = 0x52;
2057 bdaddr.b[1] = 0x54;
2058 bdaddr.b[2] = 0x00;
2059 bdaddr.b[3] = 0x12;
2060 bdaddr.b[4] = 0x34;
2061 bdaddr.b[5] = 0x56 + nb_hcis;
2062 hci->bdaddr_set(hci, bdaddr.b);
2063
2064 hci_table[nb_hcis++] = hci;
2065
2066 return 0;
2067}
2068
2069static void bt_vhci_add(int vlan_id)
2070{
2071 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2072
2073 if (!vlan->slave)
2074 fprintf(stderr, "qemu: warning: adding a VHCI to "
2075 "an empty scatternet %i\n", vlan_id);
2076
2077 bt_vhci_init(bt_new_hci(vlan));
2078}
2079
2080static struct bt_device_s *bt_device_add(const char *opt)
2081{
2082 struct bt_scatternet_s *vlan;
2083 int vlan_id = 0;
2084 char *endp = strstr(opt, ",vlan=");
2085 int len = (endp ? endp - opt : strlen(opt)) + 1;
2086 char devname[10];
2087
2088 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2089
2090 if (endp) {
2091 vlan_id = strtol(endp + 6, &endp, 0);
2092 if (*endp) {
2093 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2094 return 0;
2095 }
2096 }
2097
2098 vlan = qemu_find_bt_vlan(vlan_id);
2099
2100 if (!vlan->slave)
2101 fprintf(stderr, "qemu: warning: adding a slave device to "
2102 "an empty scatternet %i\n", vlan_id);
2103
2104 if (!strcmp(devname, "keyboard"))
2105 return bt_keyboard_init(vlan);
2106
2107 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2108 return 0;
2109}
2110
2111static int bt_parse(const char *opt)
2112{
2113 const char *endp, *p;
2114 int vlan;
2115
2116 if (strstart(opt, "hci", &endp)) {
2117 if (!*endp || *endp == ',') {
2118 if (*endp)
2119 if (!strstart(endp, ",vlan=", 0))
2120 opt = endp + 1;
2121
2122 return bt_hci_parse(opt);
2123 }
2124 } else if (strstart(opt, "vhci", &endp)) {
2125 if (!*endp || *endp == ',') {
2126 if (*endp) {
2127 if (strstart(endp, ",vlan=", &p)) {
2128 vlan = strtol(p, (char **) &endp, 0);
2129 if (*endp) {
2130 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2131 return 1;
2132 }
2133 } else {
2134 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2135 return 1;
2136 }
2137 } else
2138 vlan = 0;
2139
2140 bt_vhci_add(vlan);
2141 return 0;
2142 }
2143 } else if (strstart(opt, "device:", &endp))
2144 return !bt_device_add(endp);
2145
2146 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2147 return 1;
2148}
2149
balrog1ae26a12008-09-28 23:19:47 +00002150/***********************************************************/
2151/* QEMU Block devices */
2152
balrog609497a2008-01-14 02:56:53 +00002153#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00002154#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00002155#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00002156#define PFLASH_ALIAS "if=pflash"
2157#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00002158#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002159
aliguori7d5aca92009-02-11 15:19:58 +00002160static int drive_opt_get_free_idx(void)
2161{
2162 int index;
2163
2164 for (index = 0; index < MAX_DRIVES; index++)
2165 if (!drives_opt[index].used) {
2166 drives_opt[index].used = 1;
2167 return index;
2168 }
2169
2170 return -1;
2171}
2172
2173static int drive_get_free_idx(void)
2174{
2175 int index;
2176
2177 for (index = 0; index < MAX_DRIVES; index++)
2178 if (!drives_table[index].used) {
2179 drives_table[index].used = 1;
2180 return index;
2181 }
2182
2183 return -1;
2184}
2185
aliguori4d73cd32009-02-11 15:20:46 +00002186int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002187{
2188 va_list ap;
aliguori7d5aca92009-02-11 15:19:58 +00002189 int index = drive_opt_get_free_idx();
thse4bcb142007-12-02 04:51:10 +00002190
aliguori7d5aca92009-02-11 15:19:58 +00002191 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
thse4bcb142007-12-02 04:51:10 +00002192 fprintf(stderr, "qemu: too many drives\n");
aliguori4d73cd32009-02-11 15:20:46 +00002193 return -1;
thse4bcb142007-12-02 04:51:10 +00002194 }
2195
aliguori7d5aca92009-02-11 15:19:58 +00002196 drives_opt[index].file = file;
thse4bcb142007-12-02 04:51:10 +00002197 va_start(ap, fmt);
aliguori7d5aca92009-02-11 15:19:58 +00002198 vsnprintf(drives_opt[index].opt,
balrog609497a2008-01-14 02:56:53 +00002199 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002200 va_end(ap);
2201
aliguori7d5aca92009-02-11 15:19:58 +00002202 nb_drives_opt++;
2203 return index;
thse4bcb142007-12-02 04:51:10 +00002204}
2205
aliguorib01b1112009-02-11 15:20:20 +00002206void drive_remove(int index)
2207{
2208 drives_opt[index].used = 0;
2209 nb_drives_opt--;
2210}
2211
thsf60d39b2007-12-17 03:55:57 +00002212int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002213{
2214 int index;
2215
2216 /* seek interface, bus and unit */
2217
aliguori7d5aca92009-02-11 15:19:58 +00002218 for (index = 0; index < MAX_DRIVES; index++)
thsf60d39b2007-12-17 03:55:57 +00002219 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002220 drives_table[index].bus == bus &&
aliguori7d5aca92009-02-11 15:19:58 +00002221 drives_table[index].unit == unit &&
2222 drives_table[index].used)
thse4bcb142007-12-02 04:51:10 +00002223 return index;
2224
2225 return -1;
2226}
2227
thsf60d39b2007-12-17 03:55:57 +00002228int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002229{
2230 int max_bus;
2231 int index;
2232
2233 max_bus = -1;
2234 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002235 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002236 drives_table[index].bus > max_bus)
2237 max_bus = drives_table[index].bus;
2238 }
2239 return max_bus;
2240}
2241
aliguorifa879c62009-01-07 17:32:33 +00002242const char *drive_get_serial(BlockDriverState *bdrv)
2243{
2244 int index;
2245
2246 for (index = 0; index < nb_drives; index++)
2247 if (drives_table[index].bdrv == bdrv)
2248 return drives_table[index].serial;
2249
2250 return "\0";
2251}
2252
aliguori428c5702009-01-21 18:59:04 +00002253BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2254{
2255 int index;
2256
2257 for (index = 0; index < nb_drives; index++)
2258 if (drives_table[index].bdrv == bdrv)
2259 return drives_table[index].onerror;
2260
aliguoricdad4bd2009-02-28 16:51:01 +00002261 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002262}
2263
aurel32a1620fa2008-04-29 05:58:01 +00002264static void bdrv_format_print(void *opaque, const char *name)
2265{
2266 fprintf(stderr, " %s", name);
2267}
2268
aliguorib01b1112009-02-11 15:20:20 +00002269void drive_uninit(BlockDriverState *bdrv)
2270{
2271 int i;
2272
2273 for (i = 0; i < MAX_DRIVES; i++)
2274 if (drives_table[i].bdrv == bdrv) {
2275 drives_table[i].bdrv = NULL;
2276 drives_table[i].used = 0;
2277 drive_remove(drives_table[i].drive_opt_idx);
2278 nb_drives--;
2279 break;
2280 }
2281}
2282
aliguori4d73cd32009-02-11 15:20:46 +00002283int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
thse4bcb142007-12-02 04:51:10 +00002284{
2285 char buf[128];
2286 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002287 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00002288 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002289 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002290 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002291 enum { MEDIA_DISK, MEDIA_CDROM } media;
2292 int bus_id, unit_id;
2293 int cyls, heads, secs, translation;
2294 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002295 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002296 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002297 int max_devs;
2298 int index;
balrog33f00272007-12-24 14:33:24 +00002299 int cache;
aliguori428c5702009-01-21 18:59:04 +00002300 int bdrv_flags, onerror;
aliguori7d5aca92009-02-11 15:19:58 +00002301 int drives_table_idx;
balrog609497a2008-01-14 02:56:53 +00002302 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002303 static const char * const params[] = { "bus", "unit", "if", "index",
2304 "cyls", "heads", "secs", "trans",
2305 "media", "snapshot", "file",
aliguori428c5702009-01-21 18:59:04 +00002306 "cache", "format", "serial", "werror",
2307 NULL };
thse4bcb142007-12-02 04:51:10 +00002308
2309 if (check_params(buf, sizeof(buf), params, str) < 0) {
balrogff993632008-02-10 13:21:25 +00002310 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
thse4bcb142007-12-02 04:51:10 +00002311 buf, str);
2312 return -1;
2313 }
2314
2315 file[0] = 0;
2316 cyls = heads = secs = 0;
2317 bus_id = 0;
2318 unit_id = -1;
2319 translation = BIOS_ATA_TRANSLATION_AUTO;
2320 index = -1;
aliguori4dc822d2008-12-04 21:39:21 +00002321 cache = 3;
thse4bcb142007-12-02 04:51:10 +00002322
blueswir1c9b1ae22008-09-28 18:55:17 +00002323 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002324 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002325 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002326 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002327 } else {
thsf60d39b2007-12-17 03:55:57 +00002328 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002329 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002330 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002331 }
2332 media = MEDIA_DISK;
2333
2334 /* extract parameters */
2335
2336 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2337 bus_id = strtol(buf, NULL, 0);
2338 if (bus_id < 0) {
2339 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2340 return -1;
2341 }
2342 }
2343
2344 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2345 unit_id = strtol(buf, NULL, 0);
2346 if (unit_id < 0) {
2347 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2348 return -1;
2349 }
2350 }
2351
2352 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002353 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002354 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002355 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002356 max_devs = MAX_IDE_DEVS;
2357 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002358 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002359 max_devs = MAX_SCSI_DEVS;
2360 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002361 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002362 max_devs = 0;
2363 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002364 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002365 max_devs = 0;
2366 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002367 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002368 max_devs = 0;
2369 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002370 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002371 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002372 } else if (!strcmp(buf, "virtio")) {
2373 type = IF_VIRTIO;
2374 max_devs = 0;
2375 } else {
thse4bcb142007-12-02 04:51:10 +00002376 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2377 return -1;
2378 }
2379 }
2380
2381 if (get_param_value(buf, sizeof(buf), "index", str)) {
2382 index = strtol(buf, NULL, 0);
2383 if (index < 0) {
2384 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2385 return -1;
2386 }
2387 }
2388
2389 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2390 cyls = strtol(buf, NULL, 0);
2391 }
2392
2393 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2394 heads = strtol(buf, NULL, 0);
2395 }
2396
2397 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2398 secs = strtol(buf, NULL, 0);
2399 }
2400
2401 if (cyls || heads || secs) {
2402 if (cyls < 1 || cyls > 16383) {
2403 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2404 return -1;
2405 }
2406 if (heads < 1 || heads > 16) {
2407 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2408 return -1;
2409 }
2410 if (secs < 1 || secs > 63) {
2411 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2412 return -1;
2413 }
2414 }
2415
2416 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2417 if (!cyls) {
2418 fprintf(stderr,
2419 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2420 str);
2421 return -1;
2422 }
2423 if (!strcmp(buf, "none"))
2424 translation = BIOS_ATA_TRANSLATION_NONE;
2425 else if (!strcmp(buf, "lba"))
2426 translation = BIOS_ATA_TRANSLATION_LBA;
2427 else if (!strcmp(buf, "auto"))
2428 translation = BIOS_ATA_TRANSLATION_AUTO;
2429 else {
2430 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2431 return -1;
2432 }
2433 }
2434
2435 if (get_param_value(buf, sizeof(buf), "media", str)) {
2436 if (!strcmp(buf, "disk")) {
2437 media = MEDIA_DISK;
2438 } else if (!strcmp(buf, "cdrom")) {
2439 if (cyls || secs || heads) {
2440 fprintf(stderr,
2441 "qemu: '%s' invalid physical CHS format\n", str);
2442 return -1;
2443 }
2444 media = MEDIA_CDROM;
2445 } else {
2446 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2447 return -1;
2448 }
2449 }
2450
2451 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2452 if (!strcmp(buf, "on"))
2453 snapshot = 1;
2454 else if (!strcmp(buf, "off"))
2455 snapshot = 0;
2456 else {
2457 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2458 return -1;
2459 }
2460 }
2461
balrog33f00272007-12-24 14:33:24 +00002462 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002463 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002464 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002465 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002466 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002467 else if (!strcmp(buf, "writeback"))
2468 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002469 else {
2470 fprintf(stderr, "qemu: invalid cache option\n");
2471 return -1;
2472 }
2473 }
2474
aurel321e72d3b2008-04-28 20:26:45 +00002475 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002476 if (strcmp(buf, "?") == 0) {
2477 fprintf(stderr, "qemu: Supported formats:");
2478 bdrv_iterate_format(bdrv_format_print, NULL);
2479 fprintf(stderr, "\n");
2480 return -1;
2481 }
aurel321e72d3b2008-04-28 20:26:45 +00002482 drv = bdrv_find_format(buf);
2483 if (!drv) {
2484 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2485 return -1;
2486 }
2487 }
2488
balrog609497a2008-01-14 02:56:53 +00002489 if (arg->file == NULL)
2490 get_param_value(file, sizeof(file), "file", str);
2491 else
2492 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002493
aliguorifa879c62009-01-07 17:32:33 +00002494 if (!get_param_value(serial, sizeof(serial), "serial", str))
2495 memset(serial, 0, sizeof(serial));
2496
aliguoricdad4bd2009-02-28 16:51:01 +00002497 onerror = BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002498 if (get_param_value(buf, sizeof(serial), "werror", str)) {
aliguori869a5c62009-01-22 19:52:25 +00002499 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002500 fprintf(stderr, "werror is no supported by this format\n");
aliguori428c5702009-01-21 18:59:04 +00002501 return -1;
2502 }
2503 if (!strcmp(buf, "ignore"))
2504 onerror = BLOCK_ERR_IGNORE;
2505 else if (!strcmp(buf, "enospc"))
2506 onerror = BLOCK_ERR_STOP_ENOSPC;
2507 else if (!strcmp(buf, "stop"))
2508 onerror = BLOCK_ERR_STOP_ANY;
2509 else if (!strcmp(buf, "report"))
2510 onerror = BLOCK_ERR_REPORT;
2511 else {
2512 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2513 return -1;
2514 }
2515 }
2516
thse4bcb142007-12-02 04:51:10 +00002517 /* compute bus and unit according index */
2518
2519 if (index != -1) {
2520 if (bus_id != 0 || unit_id != -1) {
2521 fprintf(stderr,
2522 "qemu: '%s' index cannot be used with bus and unit\n", str);
2523 return -1;
2524 }
2525 if (max_devs == 0)
2526 {
2527 unit_id = index;
2528 bus_id = 0;
2529 } else {
2530 unit_id = index % max_devs;
2531 bus_id = index / max_devs;
2532 }
2533 }
2534
2535 /* if user doesn't specify a unit_id,
2536 * try to find the first free
2537 */
2538
2539 if (unit_id == -1) {
2540 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002541 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002542 unit_id++;
2543 if (max_devs && unit_id >= max_devs) {
2544 unit_id -= max_devs;
2545 bus_id++;
2546 }
2547 }
2548 }
2549
2550 /* check unit id */
2551
2552 if (max_devs && unit_id >= max_devs) {
2553 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2554 str, unit_id, max_devs - 1);
2555 return -1;
2556 }
2557
2558 /*
2559 * ignore multiple definitions
2560 */
2561
thsf60d39b2007-12-17 03:55:57 +00002562 if (drive_get_index(type, bus_id, unit_id) != -1)
aliguori4d73cd32009-02-11 15:20:46 +00002563 return -2;
thse4bcb142007-12-02 04:51:10 +00002564
2565 /* init */
2566
thsf60d39b2007-12-17 03:55:57 +00002567 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002568 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002569 if (max_devs)
2570 snprintf(buf, sizeof(buf), "%s%i%s%i",
2571 devname, bus_id, mediastr, unit_id);
2572 else
2573 snprintf(buf, sizeof(buf), "%s%s%i",
2574 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002575 bdrv = bdrv_new(buf);
aliguori7d5aca92009-02-11 15:19:58 +00002576 drives_table_idx = drive_get_free_idx();
2577 drives_table[drives_table_idx].bdrv = bdrv;
2578 drives_table[drives_table_idx].type = type;
2579 drives_table[drives_table_idx].bus = bus_id;
2580 drives_table[drives_table_idx].unit = unit_id;
2581 drives_table[drives_table_idx].onerror = onerror;
aliguorib01b1112009-02-11 15:20:20 +00002582 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
aliguorifa879c62009-01-07 17:32:33 +00002583 strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002584 nb_drives++;
2585
thsf60d39b2007-12-17 03:55:57 +00002586 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002587 case IF_IDE:
2588 case IF_SCSI:
2589 switch(media) {
2590 case MEDIA_DISK:
2591 if (cyls != 0) {
2592 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2593 bdrv_set_translation_hint(bdrv, translation);
2594 }
2595 break;
2596 case MEDIA_CDROM:
2597 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2598 break;
2599 }
2600 break;
2601 case IF_SD:
2602 /* FIXME: This isn't really a floppy, but it's a reasonable
2603 approximation. */
2604 case IF_FLOPPY:
2605 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2606 break;
2607 case IF_PFLASH:
2608 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002609 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002610 break;
2611 }
2612 if (!file[0])
aliguori4d73cd32009-02-11 15:20:46 +00002613 return -2;
balrog33f00272007-12-24 14:33:24 +00002614 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002615 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002616 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002617 cache = 2; /* always use write-back with snapshot */
2618 }
2619 if (cache == 0) /* no caching */
2620 bdrv_flags |= BDRV_O_NOCACHE;
2621 else if (cache == 2) /* write-back */
2622 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori4dc822d2008-12-04 21:39:21 +00002623 else if (cache == 3) /* not specified */
2624 bdrv_flags |= BDRV_O_CACHE_DEF;
aliguoric0f4ce72009-03-05 23:01:01 +00002625 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002626 fprintf(stderr, "qemu: could not open disk image %s\n",
2627 file);
2628 return -1;
2629 }
aliguoric0f4ce72009-03-05 23:01:01 +00002630 if (bdrv_key_required(bdrv))
2631 autostart = 0;
aliguori4d73cd32009-02-11 15:20:46 +00002632 return drives_table_idx;
thse4bcb142007-12-02 04:51:10 +00002633}
2634
aliguori268a3622009-04-21 22:30:27 +00002635static void numa_add(const char *optarg)
2636{
2637 char option[128];
2638 char *endptr;
2639 unsigned long long value, endvalue;
2640 int nodenr;
2641
2642 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2643 if (!strcmp(option, "node")) {
2644 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2645 nodenr = nb_numa_nodes;
2646 } else {
2647 nodenr = strtoull(option, NULL, 10);
2648 }
2649
2650 if (get_param_value(option, 128, "mem", optarg) == 0) {
2651 node_mem[nodenr] = 0;
2652 } else {
2653 value = strtoull(option, &endptr, 0);
2654 switch (*endptr) {
2655 case 0: case 'M': case 'm':
2656 value <<= 20;
2657 break;
2658 case 'G': case 'g':
2659 value <<= 30;
2660 break;
2661 }
2662 node_mem[nodenr] = value;
2663 }
2664 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2665 node_cpumask[nodenr] = 0;
2666 } else {
2667 value = strtoull(option, &endptr, 10);
2668 if (value >= 64) {
2669 value = 63;
2670 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2671 } else {
2672 if (*endptr == '-') {
2673 endvalue = strtoull(endptr+1, &endptr, 10);
2674 if (endvalue >= 63) {
2675 endvalue = 62;
2676 fprintf(stderr,
2677 "only 63 CPUs in NUMA mode supported.\n");
2678 }
2679 value = (1 << (endvalue + 1)) - (1 << value);
2680 } else {
2681 value = 1 << value;
2682 }
2683 }
2684 node_cpumask[nodenr] = value;
2685 }
2686 nb_numa_nodes++;
2687 }
2688 return;
2689}
2690
bellard330d0412003-07-26 18:11:40 +00002691/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002692/* USB devices */
2693
pbrook0d92ed32006-05-21 16:30:15 +00002694static USBPort *used_usb_ports;
2695static USBPort *free_usb_ports;
2696
2697/* ??? Maybe change this to register a hub to keep track of the topology. */
2698void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2699 usb_attachfn attach)
2700{
2701 port->opaque = opaque;
2702 port->index = index;
2703 port->attach = attach;
2704 port->next = free_usb_ports;
2705 free_usb_ports = port;
2706}
2707
aliguori4b096fc2008-08-21 19:28:55 +00002708int usb_device_add_dev(USBDevice *dev)
2709{
2710 USBPort *port;
2711
2712 /* Find a USB port to add the device to. */
2713 port = free_usb_ports;
2714 if (!port->next) {
2715 USBDevice *hub;
2716
2717 /* Create a new hub and chain it on. */
2718 free_usb_ports = NULL;
2719 port->next = used_usb_ports;
2720 used_usb_ports = port;
2721
2722 hub = usb_hub_init(VM_USB_HUB_SIZE);
2723 usb_attach(port, hub);
2724 port = free_usb_ports;
2725 }
2726
2727 free_usb_ports = port->next;
2728 port->next = used_usb_ports;
2729 used_usb_ports = port;
2730 usb_attach(port, dev);
2731 return 0;
2732}
2733
aliguoribb5fc202009-03-05 23:01:15 +00002734static void usb_msd_password_cb(void *opaque, int err)
2735{
2736 USBDevice *dev = opaque;
2737
2738 if (!err)
2739 usb_device_add_dev(dev);
2740 else
2741 dev->handle_destroy(dev);
2742}
2743
aliguoric0f4ce72009-03-05 23:01:01 +00002744static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002745{
2746 const char *p;
2747 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002748
pbrook0d92ed32006-05-21 16:30:15 +00002749 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002750 return -1;
2751
2752 if (strstart(devname, "host:", &p)) {
2753 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002754 } else if (!strcmp(devname, "mouse")) {
2755 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002756 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002757 dev = usb_tablet_init();
2758 } else if (!strcmp(devname, "keyboard")) {
2759 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002760 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002761 BlockDriverState *bs;
2762
aliguoribb5fc202009-03-05 23:01:15 +00002763 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002764 if (!dev)
2765 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002766 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002767 if (bdrv_key_required(bs)) {
2768 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002769 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002770 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2771 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002772 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002773 }
2774 }
balrogf6d2a312007-06-10 19:21:04 +00002775 } else if (!strcmp(devname, "wacom-tablet")) {
2776 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002777 } else if (strstart(devname, "serial:", &p)) {
2778 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002779#ifdef CONFIG_BRLAPI
2780 } else if (!strcmp(devname, "braille")) {
2781 dev = usb_baum_init();
2782#endif
balrog6c9f8862008-07-17 20:47:13 +00002783 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002784 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002785
balrog9ad97e62008-07-29 13:16:31 +00002786 if (net_client_init("nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002787 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002788 nd_table[nic].model = "usb";
2789 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002790 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2791 dev = usb_bt_init(devname[2] ? hci_init(p) :
2792 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002793 } else {
2794 return -1;
2795 }
pbrook0d92ed32006-05-21 16:30:15 +00002796 if (!dev)
2797 return -1;
2798
aliguori4b096fc2008-08-21 19:28:55 +00002799 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002800}
2801
aliguori1f3870a2008-08-21 19:27:48 +00002802int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002803{
pbrook0d92ed32006-05-21 16:30:15 +00002804 USBPort *port;
2805 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002806 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002807
pbrook0d92ed32006-05-21 16:30:15 +00002808 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002809 return -1;
2810
bellarda594cfb2005-11-06 16:13:29 +00002811 if (bus_num != 0)
2812 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002813
2814 lastp = &used_usb_ports;
2815 port = used_usb_ports;
2816 while (port && port->dev->addr != addr) {
2817 lastp = &port->next;
2818 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002819 }
pbrook0d92ed32006-05-21 16:30:15 +00002820
2821 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002822 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002823
bellard059809e2006-07-19 18:06:15 +00002824 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002825 *lastp = port->next;
2826 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002827 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002828 port->next = free_usb_ports;
2829 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002830 return 0;
2831}
2832
aliguori1f3870a2008-08-21 19:27:48 +00002833static int usb_device_del(const char *devname)
2834{
2835 int bus_num, addr;
2836 const char *p;
2837
aliguori5d0c5752008-09-14 01:07:41 +00002838 if (strstart(devname, "host:", &p))
2839 return usb_host_device_close(p);
2840
aliguori1f3870a2008-08-21 19:27:48 +00002841 if (!used_usb_ports)
2842 return -1;
2843
2844 p = strchr(devname, '.');
2845 if (!p)
2846 return -1;
2847 bus_num = strtoul(devname, NULL, 0);
2848 addr = strtoul(p + 1, NULL, 0);
2849
2850 return usb_device_del_addr(bus_num, addr);
2851}
2852
aliguori376253e2009-03-05 23:01:23 +00002853void do_usb_add(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002854{
aliguoric0f4ce72009-03-05 23:01:01 +00002855 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002856}
2857
aliguori376253e2009-03-05 23:01:23 +00002858void do_usb_del(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002859{
aliguori4b096fc2008-08-21 19:28:55 +00002860 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002861}
2862
aliguori376253e2009-03-05 23:01:23 +00002863void usb_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00002864{
2865 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002866 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002867 const char *speed_str;
2868
pbrook0d92ed32006-05-21 16:30:15 +00002869 if (!usb_enabled) {
aliguori376253e2009-03-05 23:01:23 +00002870 monitor_printf(mon, "USB support not enabled\n");
bellarda594cfb2005-11-06 16:13:29 +00002871 return;
2872 }
2873
pbrook0d92ed32006-05-21 16:30:15 +00002874 for (port = used_usb_ports; port; port = port->next) {
2875 dev = port->dev;
2876 if (!dev)
2877 continue;
2878 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002879 case USB_SPEED_LOW:
2880 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002881 break;
ths5fafdf22007-09-16 21:08:06 +00002882 case USB_SPEED_FULL:
2883 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002884 break;
ths5fafdf22007-09-16 21:08:06 +00002885 case USB_SPEED_HIGH:
2886 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002887 break;
2888 default:
ths5fafdf22007-09-16 21:08:06 +00002889 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002890 break;
bellarda594cfb2005-11-06 16:13:29 +00002891 }
aliguori376253e2009-03-05 23:01:23 +00002892 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2893 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002894 }
2895}
2896
bellardf7cce892004-12-08 22:21:25 +00002897/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002898/* PCMCIA/Cardbus */
2899
2900static struct pcmcia_socket_entry_s {
2901 struct pcmcia_socket_s *socket;
2902 struct pcmcia_socket_entry_s *next;
2903} *pcmcia_sockets = 0;
2904
2905void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2906{
2907 struct pcmcia_socket_entry_s *entry;
2908
2909 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2910 entry->socket = socket;
2911 entry->next = pcmcia_sockets;
2912 pcmcia_sockets = entry;
2913}
2914
2915void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2916{
2917 struct pcmcia_socket_entry_s *entry, **ptr;
2918
2919 ptr = &pcmcia_sockets;
2920 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2921 if (entry->socket == socket) {
2922 *ptr = entry->next;
2923 qemu_free(entry);
2924 }
2925}
2926
aliguori376253e2009-03-05 23:01:23 +00002927void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002928{
2929 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002930
balrog201a51f2007-04-30 00:51:09 +00002931 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002932 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002933
2934 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002935 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2936 iter->socket->attached ? iter->socket->card_string :
2937 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002938}
2939
2940/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002941/* register display */
2942
aliguori7b5d76d2009-03-13 15:02:13 +00002943struct DisplayAllocator default_allocator = {
2944 defaultallocator_create_displaysurface,
2945 defaultallocator_resize_displaysurface,
2946 defaultallocator_free_displaysurface
2947};
2948
aliguori3023f332009-01-16 19:04:14 +00002949void register_displaystate(DisplayState *ds)
2950{
2951 DisplayState **s;
2952 s = &display_state;
2953 while (*s != NULL)
2954 s = &(*s)->next;
2955 ds->next = NULL;
2956 *s = ds;
2957}
2958
2959DisplayState *get_displaystate(void)
2960{
2961 return display_state;
2962}
2963
aliguori7b5d76d2009-03-13 15:02:13 +00002964DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2965{
2966 if(ds->allocator == &default_allocator) ds->allocator = da;
2967 return ds->allocator;
2968}
2969
ths2ff89792007-06-21 23:34:19 +00002970/* dumb display */
2971
aliguori8f391ab2009-01-19 16:34:10 +00002972static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002973{
aliguori8f391ab2009-01-19 16:34:10 +00002974 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002975 ds->allocator = &default_allocator;
2976 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002977 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002978}
2979
2980/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002981/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002982
bellardc4b1fcc2004-03-14 21:44:30 +00002983typedef struct IOHandlerRecord {
2984 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002985 IOCanRWHandler *fd_read_poll;
2986 IOHandler *fd_read;
2987 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002988 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002989 void *opaque;
2990 /* temporary data */
2991 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002992 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002993} IOHandlerRecord;
2994
bellard8a7ddc32004-03-31 19:00:16 +00002995static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002996
bellard7c9d8e02005-11-15 22:16:05 +00002997/* XXX: fd_read_poll should be suppressed, but an API change is
2998 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002999int qemu_set_fd_handler2(int fd,
3000 IOCanRWHandler *fd_read_poll,
3001 IOHandler *fd_read,
3002 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00003003 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00003004{
bellard8a7ddc32004-03-31 19:00:16 +00003005 IOHandlerRecord **pioh, *ioh;
3006
bellard7c9d8e02005-11-15 22:16:05 +00003007 if (!fd_read && !fd_write) {
3008 pioh = &first_io_handler;
3009 for(;;) {
3010 ioh = *pioh;
3011 if (ioh == NULL)
3012 break;
3013 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00003014 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00003015 break;
3016 }
3017 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00003018 }
bellard7c9d8e02005-11-15 22:16:05 +00003019 } else {
3020 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3021 if (ioh->fd == fd)
3022 goto found;
3023 }
3024 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00003025 ioh->next = first_io_handler;
3026 first_io_handler = ioh;
3027 found:
3028 ioh->fd = fd;
3029 ioh->fd_read_poll = fd_read_poll;
3030 ioh->fd_read = fd_read;
3031 ioh->fd_write = fd_write;
3032 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00003033 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00003034 }
bellard7c9d8e02005-11-15 22:16:05 +00003035 return 0;
3036}
3037
ths5fafdf22007-09-16 21:08:06 +00003038int qemu_set_fd_handler(int fd,
3039 IOHandler *fd_read,
3040 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00003041 void *opaque)
3042{
3043 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00003044}
3045
aliguori56f3a5d2008-10-31 18:07:17 +00003046#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00003047/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00003048/* Polling handling */
3049
3050typedef struct PollingEntry {
3051 PollingFunc *func;
3052 void *opaque;
3053 struct PollingEntry *next;
3054} PollingEntry;
3055
3056static PollingEntry *first_polling_entry;
3057
3058int qemu_add_polling_cb(PollingFunc *func, void *opaque)
3059{
3060 PollingEntry **ppe, *pe;
3061 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00003062 pe->func = func;
3063 pe->opaque = opaque;
3064 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
3065 *ppe = pe;
3066 return 0;
3067}
3068
3069void qemu_del_polling_cb(PollingFunc *func, void *opaque)
3070{
3071 PollingEntry **ppe, *pe;
3072 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
3073 pe = *ppe;
3074 if (pe->func == func && pe->opaque == opaque) {
3075 *ppe = pe->next;
3076 qemu_free(pe);
3077 break;
3078 }
3079 }
3080}
3081
bellarda18e5242006-06-25 17:18:27 +00003082/***********************************************************/
3083/* Wait objects support */
3084typedef struct WaitObjects {
3085 int num;
3086 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
3087 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
3088 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
3089} WaitObjects;
3090
3091static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00003092
bellarda18e5242006-06-25 17:18:27 +00003093int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3094{
3095 WaitObjects *w = &wait_objects;
3096
3097 if (w->num >= MAXIMUM_WAIT_OBJECTS)
3098 return -1;
3099 w->events[w->num] = handle;
3100 w->func[w->num] = func;
3101 w->opaque[w->num] = opaque;
3102 w->num++;
3103 return 0;
3104}
3105
3106void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3107{
3108 int i, found;
3109 WaitObjects *w = &wait_objects;
3110
3111 found = 0;
3112 for (i = 0; i < w->num; i++) {
3113 if (w->events[i] == handle)
3114 found = 1;
3115 if (found) {
3116 w->events[i] = w->events[i + 1];
3117 w->func[i] = w->func[i + 1];
3118 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00003119 }
bellarda18e5242006-06-25 17:18:27 +00003120 }
3121 if (found)
3122 w->num--;
3123}
3124#endif
3125
bellard8a7ddc32004-03-31 19:00:16 +00003126/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003127/* ram save/restore */
3128
bellard8a7ddc32004-03-31 19:00:16 +00003129static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3130{
3131 int v;
3132
3133 v = qemu_get_byte(f);
3134 switch(v) {
3135 case 0:
3136 if (qemu_get_buffer(f, buf, len) != len)
3137 return -EIO;
3138 break;
3139 case 1:
3140 v = qemu_get_byte(f);
3141 memset(buf, v, len);
3142 break;
3143 default:
3144 return -EINVAL;
3145 }
aliguori871d2f02008-10-13 03:07:56 +00003146
3147 if (qemu_file_has_error(f))
3148 return -EIO;
3149
bellard8a7ddc32004-03-31 19:00:16 +00003150 return 0;
3151}
3152
bellardc88676f2006-08-06 13:36:11 +00003153static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003154{
aurel3200f82b82008-04-27 21:12:55 +00003155 int ret;
3156 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00003157
pbrook94a6b542009-04-11 17:15:54 +00003158 if (qemu_get_be32(f) != last_ram_offset)
bellard8a7ddc32004-03-31 19:00:16 +00003159 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003160 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
pbrook5579c7f2009-04-11 14:47:08 +00003161 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
bellard8a7ddc32004-03-31 19:00:16 +00003162 if (ret)
3163 return ret;
3164 }
3165 return 0;
3166}
3167
bellardc88676f2006-08-06 13:36:11 +00003168#define BDRV_HASH_BLOCK_SIZE 1024
3169#define IOBUF_SIZE 4096
3170#define RAM_CBLOCK_MAGIC 0xfabe
3171
bellardc88676f2006-08-06 13:36:11 +00003172typedef struct RamDecompressState {
3173 z_stream zstream;
3174 QEMUFile *f;
3175 uint8_t buf[IOBUF_SIZE];
3176} RamDecompressState;
3177
3178static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3179{
3180 int ret;
3181 memset(s, 0, sizeof(*s));
3182 s->f = f;
3183 ret = inflateInit(&s->zstream);
3184 if (ret != Z_OK)
3185 return -1;
3186 return 0;
3187}
3188
3189static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3190{
3191 int ret, clen;
3192
3193 s->zstream.avail_out = len;
3194 s->zstream.next_out = buf;
3195 while (s->zstream.avail_out > 0) {
3196 if (s->zstream.avail_in == 0) {
3197 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3198 return -1;
3199 clen = qemu_get_be16(s->f);
3200 if (clen > IOBUF_SIZE)
3201 return -1;
3202 qemu_get_buffer(s->f, s->buf, clen);
3203 s->zstream.avail_in = clen;
3204 s->zstream.next_in = s->buf;
3205 }
3206 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3207 if (ret != Z_OK && ret != Z_STREAM_END) {
3208 return -1;
3209 }
3210 }
3211 return 0;
3212}
3213
3214static void ram_decompress_close(RamDecompressState *s)
3215{
3216 inflateEnd(&s->zstream);
3217}
3218
aliguori475e4272008-10-06 20:21:51 +00003219#define RAM_SAVE_FLAG_FULL 0x01
3220#define RAM_SAVE_FLAG_COMPRESS 0x02
3221#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3222#define RAM_SAVE_FLAG_PAGE 0x08
3223#define RAM_SAVE_FLAG_EOS 0x10
3224
3225static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003226{
aliguori475e4272008-10-06 20:21:51 +00003227 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3228 uint32_t *array = (uint32_t *)page;
3229 int i;
ths3b46e622007-09-17 08:09:54 +00003230
aliguori475e4272008-10-06 20:21:51 +00003231 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3232 if (array[i] != val)
3233 return 0;
bellardc88676f2006-08-06 13:36:11 +00003234 }
aliguori475e4272008-10-06 20:21:51 +00003235
3236 return 1;
bellardc88676f2006-08-06 13:36:11 +00003237}
3238
aliguori475e4272008-10-06 20:21:51 +00003239static int ram_save_block(QEMUFile *f)
3240{
3241 static ram_addr_t current_addr = 0;
3242 ram_addr_t saved_addr = current_addr;
3243 ram_addr_t addr = 0;
3244 int found = 0;
3245
pbrook94a6b542009-04-11 17:15:54 +00003246 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00003247 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00003248 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00003249
3250 cpu_physical_memory_reset_dirty(current_addr,
3251 current_addr + TARGET_PAGE_SIZE,
3252 MIGRATION_DIRTY_FLAG);
3253
pbrook5579c7f2009-04-11 14:47:08 +00003254 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00003255
pbrook5579c7f2009-04-11 14:47:08 +00003256 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00003257 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00003258 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00003259 } else {
3260 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00003261 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003262 }
3263
3264 found = 1;
3265 break;
3266 }
3267 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00003268 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00003269 }
3270
3271 return found;
3272}
3273
3274static ram_addr_t ram_save_threshold = 10;
3275
3276static ram_addr_t ram_save_remaining(void)
3277{
3278 ram_addr_t addr;
3279 ram_addr_t count = 0;
3280
pbrook94a6b542009-04-11 17:15:54 +00003281 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003282 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3283 count++;
3284 }
3285
3286 return count;
3287}
3288
3289static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3290{
3291 ram_addr_t addr;
3292
3293 if (stage == 1) {
3294 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00003295 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003296 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3297 cpu_physical_memory_set_dirty(addr);
3298 }
3299
3300 /* Enable dirty memory tracking */
3301 cpu_physical_memory_set_dirty_tracking(1);
3302
pbrook94a6b542009-04-11 17:15:54 +00003303 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003304 }
3305
3306 while (!qemu_file_rate_limit(f)) {
3307 int ret;
3308
3309 ret = ram_save_block(f);
3310 if (ret == 0) /* no more blocks */
3311 break;
3312 }
3313
3314 /* try transferring iterative blocks of memory */
3315
3316 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003317
3318 /* flush all remaining blocks regardless of rate limiting */
3319 while (ram_save_block(f) != 0);
aliguori8215e912009-04-05 19:30:55 +00003320 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003321 }
3322
3323 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3324
3325 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3326}
3327
3328static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003329{
3330 RamDecompressState s1, *s = &s1;
3331 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003332 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003333
bellardc88676f2006-08-06 13:36:11 +00003334 if (ram_decompress_open(s, f) < 0)
3335 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003336 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
bellardc88676f2006-08-06 13:36:11 +00003337 if (ram_decompress_buf(s, buf, 1) < 0) {
3338 fprintf(stderr, "Error while reading ram block header\n");
3339 goto error;
3340 }
3341 if (buf[0] == 0) {
pbrook5579c7f2009-04-11 14:47:08 +00003342 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3343 BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003344 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003345 goto error;
3346 }
aliguori475e4272008-10-06 20:21:51 +00003347 } else {
bellardc88676f2006-08-06 13:36:11 +00003348 error:
3349 printf("Error block header\n");
3350 return -EINVAL;
3351 }
3352 }
3353 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003354
3355 return 0;
3356}
3357
3358static int ram_load(QEMUFile *f, void *opaque, int version_id)
3359{
3360 ram_addr_t addr;
3361 int flags;
3362
3363 if (version_id == 1)
3364 return ram_load_v1(f, opaque);
3365
3366 if (version_id == 2) {
pbrook94a6b542009-04-11 17:15:54 +00003367 if (qemu_get_be32(f) != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003368 return -EINVAL;
3369 return ram_load_dead(f, opaque);
3370 }
3371
3372 if (version_id != 3)
3373 return -EINVAL;
3374
3375 do {
3376 addr = qemu_get_be64(f);
3377
3378 flags = addr & ~TARGET_PAGE_MASK;
3379 addr &= TARGET_PAGE_MASK;
3380
3381 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003382 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003383 return -EINVAL;
3384 }
3385
3386 if (flags & RAM_SAVE_FLAG_FULL) {
3387 if (ram_load_dead(f, opaque) < 0)
3388 return -EINVAL;
3389 }
3390
3391 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3392 uint8_t ch = qemu_get_byte(f);
pbrook5579c7f2009-04-11 14:47:08 +00003393 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003394 } else if (flags & RAM_SAVE_FLAG_PAGE)
pbrook5579c7f2009-04-11 14:47:08 +00003395 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003396 } while (!(flags & RAM_SAVE_FLAG_EOS));
3397
bellardc88676f2006-08-06 13:36:11 +00003398 return 0;
3399}
3400
aliguori9e472e12008-10-08 19:50:24 +00003401void qemu_service_io(void)
3402{
3403 CPUState *env = cpu_single_env;
3404 if (env) {
aurel323098dba2009-03-07 21:28:24 +00003405 cpu_exit(env);
blueswir1640f42e2009-04-19 10:18:01 +00003406#ifdef CONFIG_KQEMU
aliguori9e472e12008-10-08 19:50:24 +00003407 if (env->kqemu_enabled) {
3408 kqemu_cpu_interrupt(env);
3409 }
3410#endif
3411 }
3412}
3413
bellard8a7ddc32004-03-31 19:00:16 +00003414/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003415/* bottom halves (can be seen as timers which expire ASAP) */
3416
3417struct QEMUBH {
3418 QEMUBHFunc *cb;
3419 void *opaque;
3420 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003421 int idle;
3422 int deleted;
bellard83f64092006-08-01 16:21:11 +00003423 QEMUBH *next;
3424};
3425
3426static QEMUBH *first_bh = NULL;
3427
3428QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3429{
3430 QEMUBH *bh;
3431 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003432 bh->cb = cb;
3433 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003434 bh->next = first_bh;
3435 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003436 return bh;
3437}
3438
bellard6eb57332006-08-06 09:51:25 +00003439int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003440{
aliguori1b435b12008-10-31 17:24:21 +00003441 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003442 int ret;
bellard83f64092006-08-01 16:21:11 +00003443
bellard6eb57332006-08-06 09:51:25 +00003444 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003445 for (bh = first_bh; bh; bh = bh->next) {
3446 if (!bh->deleted && bh->scheduled) {
3447 bh->scheduled = 0;
3448 if (!bh->idle)
3449 ret = 1;
3450 bh->idle = 0;
3451 bh->cb(bh->opaque);
3452 }
bellard83f64092006-08-01 16:21:11 +00003453 }
aliguori1b435b12008-10-31 17:24:21 +00003454
3455 /* remove deleted bhs */
3456 bhp = &first_bh;
3457 while (*bhp) {
3458 bh = *bhp;
3459 if (bh->deleted) {
3460 *bhp = bh->next;
3461 qemu_free(bh);
3462 } else
3463 bhp = &bh->next;
3464 }
3465
bellard6eb57332006-08-06 09:51:25 +00003466 return ret;
bellard83f64092006-08-01 16:21:11 +00003467}
3468
aliguori1b435b12008-10-31 17:24:21 +00003469void qemu_bh_schedule_idle(QEMUBH *bh)
3470{
3471 if (bh->scheduled)
3472 return;
3473 bh->scheduled = 1;
3474 bh->idle = 1;
3475}
3476
bellard83f64092006-08-01 16:21:11 +00003477void qemu_bh_schedule(QEMUBH *bh)
3478{
3479 CPUState *env = cpu_single_env;
3480 if (bh->scheduled)
3481 return;
3482 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003483 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003484 /* stop the currently executing CPU to execute the BH ASAP */
3485 if (env) {
aurel323098dba2009-03-07 21:28:24 +00003486 cpu_exit(env);
bellard83f64092006-08-01 16:21:11 +00003487 }
3488}
3489
3490void qemu_bh_cancel(QEMUBH *bh)
3491{
aliguori1b435b12008-10-31 17:24:21 +00003492 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003493}
3494
3495void qemu_bh_delete(QEMUBH *bh)
3496{
aliguori1b435b12008-10-31 17:24:21 +00003497 bh->scheduled = 0;
3498 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003499}
3500
aliguori56f3a5d2008-10-31 18:07:17 +00003501static void qemu_bh_update_timeout(int *timeout)
3502{
3503 QEMUBH *bh;
3504
3505 for (bh = first_bh; bh; bh = bh->next) {
3506 if (!bh->deleted && bh->scheduled) {
3507 if (bh->idle) {
3508 /* idle bottom halves will be polled at least
3509 * every 10ms */
3510 *timeout = MIN(10, *timeout);
3511 } else {
3512 /* non-idle bottom halves will be executed
3513 * immediately */
3514 *timeout = 0;
3515 break;
3516 }
3517 }
3518 }
3519}
3520
bellard83f64092006-08-01 16:21:11 +00003521/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003522/* machine registration */
3523
blueswir1bdaf78e2008-10-04 07:24:27 +00003524static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003525QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003526
3527int qemu_register_machine(QEMUMachine *m)
3528{
3529 QEMUMachine **pm;
3530 pm = &first_machine;
3531 while (*pm != NULL)
3532 pm = &(*pm)->next;
3533 m->next = NULL;
3534 *pm = m;
3535 return 0;
3536}
3537
pbrook9596ebb2007-11-18 01:44:38 +00003538static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003539{
3540 QEMUMachine *m;
3541
3542 for(m = first_machine; m != NULL; m = m->next) {
3543 if (!strcmp(m->name, name))
3544 return m;
3545 }
3546 return NULL;
3547}
3548
3549/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003550/* main execution loop */
3551
pbrook9596ebb2007-11-18 01:44:38 +00003552static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003553{
aliguori7d957bd2009-01-15 22:14:11 +00003554 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003555 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003556 DisplayChangeListener *dcl = ds->listeners;
3557
3558 dpy_refresh(ds);
3559
3560 while (dcl != NULL) {
3561 if (dcl->gui_timer_interval &&
3562 dcl->gui_timer_interval < interval)
3563 interval = dcl->gui_timer_interval;
3564 dcl = dcl->next;
3565 }
3566 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003567}
3568
blueswir19043b622009-01-21 19:28:13 +00003569static void nographic_update(void *opaque)
3570{
3571 uint64_t interval = GUI_REFRESH_INTERVAL;
3572
3573 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3574}
3575
bellard0bd48852005-11-11 00:00:47 +00003576struct vm_change_state_entry {
3577 VMChangeStateHandler *cb;
3578 void *opaque;
3579 LIST_ENTRY (vm_change_state_entry) entries;
3580};
3581
3582static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3583
3584VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3585 void *opaque)
3586{
3587 VMChangeStateEntry *e;
3588
3589 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003590
3591 e->cb = cb;
3592 e->opaque = opaque;
3593 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3594 return e;
3595}
3596
3597void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3598{
3599 LIST_REMOVE (e, entries);
3600 qemu_free (e);
3601}
3602
aliguori9781e042009-01-22 17:15:29 +00003603static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003604{
3605 VMChangeStateEntry *e;
3606
3607 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003608 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003609 }
3610}
3611
bellard8a7ddc32004-03-31 19:00:16 +00003612void vm_start(void)
3613{
3614 if (!vm_running) {
3615 cpu_enable_ticks();
3616 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003617 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003618 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00003619 }
3620}
3621
ths5fafdf22007-09-16 21:08:06 +00003622void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00003623{
3624 if (vm_running) {
3625 cpu_disable_ticks();
3626 vm_running = 0;
aliguori9781e042009-01-22 17:15:29 +00003627 vm_state_notify(0, reason);
bellard8a7ddc32004-03-31 19:00:16 +00003628 }
3629}
3630
bellardbb0c6722004-06-20 12:37:32 +00003631/* reset/shutdown handler */
3632
3633typedef struct QEMUResetEntry {
3634 QEMUResetHandler *func;
3635 void *opaque;
3636 struct QEMUResetEntry *next;
3637} QEMUResetEntry;
3638
3639static QEMUResetEntry *first_reset_entry;
3640static int reset_requested;
3641static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003642static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00003643
aurel32cf7a2fe2008-03-18 06:53:05 +00003644int qemu_shutdown_requested(void)
3645{
3646 int r = shutdown_requested;
3647 shutdown_requested = 0;
3648 return r;
3649}
3650
3651int qemu_reset_requested(void)
3652{
3653 int r = reset_requested;
3654 reset_requested = 0;
3655 return r;
3656}
3657
3658int qemu_powerdown_requested(void)
3659{
3660 int r = powerdown_requested;
3661 powerdown_requested = 0;
3662 return r;
3663}
3664
bellardbb0c6722004-06-20 12:37:32 +00003665void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3666{
3667 QEMUResetEntry **pre, *re;
3668
3669 pre = &first_reset_entry;
3670 while (*pre != NULL)
3671 pre = &(*pre)->next;
3672 re = qemu_mallocz(sizeof(QEMUResetEntry));
3673 re->func = func;
3674 re->opaque = opaque;
3675 re->next = NULL;
3676 *pre = re;
3677}
3678
aurel32cf7a2fe2008-03-18 06:53:05 +00003679void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00003680{
3681 QEMUResetEntry *re;
3682
3683 /* reset all devices */
3684 for(re = first_reset_entry; re != NULL; re = re->next) {
3685 re->func(re->opaque);
3686 }
aliguori29203dc2009-04-17 14:26:17 +00003687 if (kvm_enabled())
3688 kvm_sync_vcpus();
bellardbb0c6722004-06-20 12:37:32 +00003689}
3690
3691void qemu_system_reset_request(void)
3692{
bellardd1beab82006-10-02 19:44:22 +00003693 if (no_reboot) {
3694 shutdown_requested = 1;
3695 } else {
3696 reset_requested = 1;
3697 }
bellard6a00d602005-11-21 23:25:50 +00003698 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003699 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003700}
3701
3702void qemu_system_shutdown_request(void)
3703{
3704 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003705 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003706 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003707}
3708
bellard34751872005-07-02 14:31:34 +00003709void qemu_system_powerdown_request(void)
3710{
3711 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003712 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003713 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003714}
3715
ths877cf882007-04-18 18:11:47 +00003716#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003717static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003718{
3719 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003720 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003721
bellardf3311102006-04-12 20:21:17 +00003722
3723 /* XXX: need to suppress polling by better using win32 events */
3724 ret = 0;
3725 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3726 ret |= pe->func(pe->opaque);
3727 }
thse6b1e552007-04-18 17:56:02 +00003728 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003729 int err;
3730 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003731
aliguori56f3a5d2008-10-31 18:07:17 +00003732 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003733 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3734 if (w->func[ret - WAIT_OBJECT_0])
3735 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003736
ths5fafdf22007-09-16 21:08:06 +00003737 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003738 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003739
thse6b1e552007-04-18 17:56:02 +00003740 /* Check if event is signaled */
3741 ret2 = WaitForSingleObject(w->events[i], 0);
3742 if(ret2 == WAIT_OBJECT_0) {
3743 if (w->func[i])
3744 w->func[i](w->opaque[i]);
3745 } else if (ret2 == WAIT_TIMEOUT) {
3746 } else {
3747 err = GetLastError();
3748 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003749 }
3750 }
bellarda18e5242006-06-25 17:18:27 +00003751 } else if (ret == WAIT_TIMEOUT) {
3752 } else {
3753 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003754 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003755 }
bellardf3311102006-04-12 20:21:17 +00003756 }
aliguori56f3a5d2008-10-31 18:07:17 +00003757
3758 *timeout = 0;
3759}
3760#else
blueswir169d64512008-12-07 19:30:18 +00003761static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003762{
3763}
bellardfd1dff42006-02-01 21:29:26 +00003764#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003765
3766void main_loop_wait(int timeout)
3767{
3768 IOHandlerRecord *ioh;
3769 fd_set rfds, wfds, xfds;
3770 int ret, nfds;
3771 struct timeval tv;
3772
3773 qemu_bh_update_timeout(&timeout);
3774
3775 host_main_loop_wait(&timeout);
3776
bellardfd1dff42006-02-01 21:29:26 +00003777 /* poll any events */
3778 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003779 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003780 FD_ZERO(&rfds);
3781 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003782 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003783 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003784 if (ioh->deleted)
3785 continue;
bellardfd1dff42006-02-01 21:29:26 +00003786 if (ioh->fd_read &&
3787 (!ioh->fd_read_poll ||
3788 ioh->fd_read_poll(ioh->opaque) != 0)) {
3789 FD_SET(ioh->fd, &rfds);
3790 if (ioh->fd > nfds)
3791 nfds = ioh->fd;
3792 }
3793 if (ioh->fd_write) {
3794 FD_SET(ioh->fd, &wfds);
3795 if (ioh->fd > nfds)
3796 nfds = ioh->fd;
3797 }
3798 }
ths3b46e622007-09-17 08:09:54 +00003799
aliguori56f3a5d2008-10-31 18:07:17 +00003800 tv.tv_sec = timeout / 1000;
3801 tv.tv_usec = (timeout % 1000) * 1000;
3802
bellarde0356492006-05-01 13:33:02 +00003803#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003804 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003805 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3806 }
3807#endif
3808 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00003809 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003810 IOHandlerRecord **pioh;
3811
3812 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003813 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003814 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003815 }
ths6ab43fd2007-08-25 01:34:19 +00003816 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003817 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003818 }
3819 }
thscafffd42007-02-28 21:59:44 +00003820
3821 /* remove deleted IO handlers */
3822 pioh = &first_io_handler;
3823 while (*pioh) {
3824 ioh = *pioh;
3825 if (ioh->deleted) {
3826 *pioh = ioh->next;
3827 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003828 } else
thscafffd42007-02-28 21:59:44 +00003829 pioh = &ioh->next;
3830 }
bellardfd1dff42006-02-01 21:29:26 +00003831 }
bellarde0356492006-05-01 13:33:02 +00003832#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003833 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003834 if (ret < 0) {
3835 FD_ZERO(&rfds);
3836 FD_ZERO(&wfds);
3837 FD_ZERO(&xfds);
3838 }
3839 slirp_select_poll(&rfds, &wfds, &xfds);
3840 }
3841#endif
bellardc20709a2004-04-21 23:27:19 +00003842
aliguori357c6922008-11-25 17:26:09 +00003843 /* vm time timers */
3844 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3845 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3846 qemu_get_clock(vm_clock));
3847
3848 /* real time timers */
3849 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3850 qemu_get_clock(rt_clock));
3851
pbrook423f0742007-05-23 00:06:54 +00003852 /* Check bottom-halves last in case any of the earlier events triggered
3853 them. */
3854 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003855
bellard5905b2e2004-08-01 21:53:26 +00003856}
3857
pbrook9596ebb2007-11-18 01:44:38 +00003858static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00003859{
3860 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00003861#ifdef CONFIG_PROFILER
3862 int64_t ti;
3863#endif
bellard6a00d602005-11-21 23:25:50 +00003864 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003865
bellard6a00d602005-11-21 23:25:50 +00003866 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00003867 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00003868 for(;;) {
3869 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00003870
bellard15a76442005-11-23 21:01:03 +00003871 for(;;) {
3872 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00003873 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00003874#ifdef CONFIG_PROFILER
3875 ti = profile_getclock();
3876#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003877 if (use_icount) {
3878 int64_t count;
3879 int decr;
3880 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3881 env->icount_decr.u16.low = 0;
3882 env->icount_extra = 0;
3883 count = qemu_next_deadline();
3884 count = (count + (1 << icount_time_shift) - 1)
3885 >> icount_time_shift;
3886 qemu_icount += count;
3887 decr = (count > 0xffff) ? 0xffff : count;
3888 count -= decr;
3889 env->icount_decr.u16.low = decr;
3890 env->icount_extra = count;
3891 }
bellard6a00d602005-11-21 23:25:50 +00003892 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00003893#ifdef CONFIG_PROFILER
3894 qemu_time += profile_getclock() - ti;
3895#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003896 if (use_icount) {
3897 /* Fold pending instructions back into the
3898 instruction counter, and clear the interrupt flag. */
3899 qemu_icount -= (env->icount_decr.u16.low
3900 + env->icount_extra);
3901 env->icount_decr.u32 = 0;
3902 env->icount_extra = 0;
3903 }
balrogee5605e2007-12-03 03:01:40 +00003904 next_cpu = env->next_cpu ?: first_cpu;
aurel3295b01002008-04-04 17:16:35 +00003905 if (event_pending && likely(ret != EXCP_DEBUG)) {
balrogee5605e2007-12-03 03:01:40 +00003906 ret = EXCP_INTERRUPT;
3907 event_pending = 0;
3908 break;
3909 }
pbrookbd967e02007-03-11 18:54:57 +00003910 if (ret == EXCP_HLT) {
3911 /* Give the next CPU a chance to run. */
3912 cur_cpu = env;
3913 continue;
3914 }
bellard15a76442005-11-23 21:01:03 +00003915 if (ret != EXCP_HALTED)
3916 break;
3917 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00003918 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00003919 break;
bellard15a76442005-11-23 21:01:03 +00003920 }
3921 cur_cpu = env;
3922
bellard5905b2e2004-08-01 21:53:26 +00003923 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00003924 ret = EXCP_INTERRUPT;
aurel32b2f76162008-04-11 21:35:52 +00003925 if (no_shutdown) {
3926 vm_stop(0);
3927 no_shutdown = 0;
3928 }
3929 else
3930 break;
bellard5905b2e2004-08-01 21:53:26 +00003931 }
3932 if (reset_requested) {
3933 reset_requested = 0;
3934 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00003935 ret = EXCP_INTERRUPT;
3936 }
3937 if (powerdown_requested) {
3938 powerdown_requested = 0;
3939 qemu_system_powerdown();
3940 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00003941 }
aurel3295b01002008-04-04 17:16:35 +00003942 if (unlikely(ret == EXCP_DEBUG)) {
aliguori880a7572008-11-18 20:30:24 +00003943 gdb_set_stop_cpu(cur_cpu);
bellard5905b2e2004-08-01 21:53:26 +00003944 vm_stop(EXCP_DEBUG);
3945 }
pbrookbd967e02007-03-11 18:54:57 +00003946 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00003947 /* XXX: use timeout computed from timers */
pbrook2e70f6e2008-06-29 01:03:05 +00003948 if (ret == EXCP_HALTED) {
3949 if (use_icount) {
3950 int64_t add;
3951 int64_t delta;
3952 /* Advance virtual time to the next event. */
3953 if (use_icount == 1) {
3954 /* When not using an adaptive execution frequency
3955 we tend to get badly out of sync with real time,
thsbf20dc02008-06-30 17:22:19 +00003956 so just delay for a reasonable amount of time. */
pbrook2e70f6e2008-06-29 01:03:05 +00003957 delta = 0;
3958 } else {
3959 delta = cpu_get_icount() - cpu_get_clock();
3960 }
3961 if (delta > 0) {
3962 /* If virtual time is ahead of real time then just
3963 wait for IO. */
3964 timeout = (delta / 1000000) + 1;
3965 } else {
3966 /* Wait for either IO to occur or the next
3967 timer event. */
3968 add = qemu_next_deadline();
3969 /* We advance the timer before checking for IO.
3970 Limit the amount we advance so that early IO
3971 activity won't get the guest too far ahead. */
3972 if (add > 10000000)
3973 add = 10000000;
3974 delta += add;
3975 add = (add + (1 << icount_time_shift) - 1)
3976 >> icount_time_shift;
3977 qemu_icount += add;
3978 timeout = delta / 1000000;
3979 if (timeout < 0)
3980 timeout = 0;
3981 }
3982 } else {
aliguori0a1af392008-10-31 18:40:25 +00003983 timeout = 5000;
pbrook2e70f6e2008-06-29 01:03:05 +00003984 }
3985 } else {
bellard5905b2e2004-08-01 21:53:26 +00003986 timeout = 0;
pbrook2e70f6e2008-06-29 01:03:05 +00003987 }
bellard5905b2e2004-08-01 21:53:26 +00003988 } else {
blueswir198448f52008-09-30 18:16:09 +00003989 if (shutdown_requested) {
3990 ret = EXCP_INTERRUPT;
aliguori5b08fc12008-08-21 20:08:03 +00003991 break;
blueswir198448f52008-09-30 18:16:09 +00003992 }
aliguori0a1af392008-10-31 18:40:25 +00003993 timeout = 5000;
bellard5905b2e2004-08-01 21:53:26 +00003994 }
bellard89bfc102006-02-08 22:46:31 +00003995#ifdef CONFIG_PROFILER
3996 ti = profile_getclock();
3997#endif
bellard5905b2e2004-08-01 21:53:26 +00003998 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00003999#ifdef CONFIG_PROFILER
4000 dev_time += profile_getclock() - ti;
4001#endif
bellardb4608c02003-06-27 17:34:32 +00004002 }
bellard34865132003-10-05 14:28:56 +00004003 cpu_disable_ticks();
4004 return ret;
bellardb4608c02003-06-27 17:34:32 +00004005}
4006
pbrook9bd7e6d2009-04-07 22:58:45 +00004007static void version(void)
4008{
pbrook4a19f1e2009-04-07 23:17:49 +00004009 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004010}
4011
ths15f82202007-06-29 23:26:08 +00004012static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004013{
pbrook9bd7e6d2009-04-07 22:58:45 +00004014 version();
4015 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004016 "\n"
bellarda20dd502003-09-30 21:07:02 +00004017 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004018 "\n"
blueswir15824d652009-03-28 06:44:27 +00004019#define DEF(option, opt_arg, opt_enum, opt_help) \
4020 opt_help
4021#define DEFHEADING(text) stringify(text) "\n"
4022#include "qemu-options.h"
4023#undef DEF
4024#undef DEFHEADING
4025#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004026 "\n"
bellard82c643f2004-07-14 17:28:13 +00004027 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004028 "ctrl-alt-f toggle full screen\n"
4029 "ctrl-alt-n switch to virtual console 'n'\n"
4030 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004031 "\n"
4032 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4033 ,
bellard0db63472003-10-27 21:37:46 +00004034 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004035 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004036#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004037 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004038 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004039#endif
bellard6e44ba72004-01-18 21:56:49 +00004040 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004041 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004042 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004043}
4044
bellardcd6f1162004-05-13 22:02:20 +00004045#define HAS_ARG 0x0001
4046
4047enum {
blueswir15824d652009-03-28 06:44:27 +00004048#define DEF(option, opt_arg, opt_enum, opt_help) \
4049 opt_enum,
4050#define DEFHEADING(text)
4051#include "qemu-options.h"
4052#undef DEF
4053#undef DEFHEADING
4054#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004055};
4056
4057typedef struct QEMUOption {
4058 const char *name;
4059 int flags;
4060 int index;
4061} QEMUOption;
4062
blueswir1dbed7e42008-10-01 19:38:09 +00004063static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004064 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004065#define DEF(option, opt_arg, opt_enum, opt_help) \
4066 { option, opt_arg, opt_enum },
4067#define DEFHEADING(text)
4068#include "qemu-options.h"
4069#undef DEF
4070#undef DEFHEADING
4071#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004072 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004073};
4074
bellard1d14ffa2005-10-30 18:58:22 +00004075#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004076struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004077#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004078#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004079 {
4080 "pcspk",
4081 "PC speaker",
4082 0,
4083 1,
4084 { .init_isa = pcspk_audio_init }
4085 },
4086#endif
malc4c9b53e2009-01-09 10:46:34 +00004087
4088#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004089 {
4090 "sb16",
4091 "Creative Sound Blaster 16",
4092 0,
4093 1,
4094 { .init_isa = SB16_init }
4095 },
malc4c9b53e2009-01-09 10:46:34 +00004096#endif
bellard6a36d842005-12-18 20:34:32 +00004097
malccc53d262008-06-13 10:48:22 +00004098#ifdef CONFIG_CS4231A
4099 {
4100 "cs4231a",
4101 "CS4231A",
4102 0,
4103 1,
4104 { .init_isa = cs4231a_init }
4105 },
4106#endif
4107
bellard6a36d842005-12-18 20:34:32 +00004108#ifdef CONFIG_ADLIB
4109 {
4110 "adlib",
4111#ifdef HAS_YMF262
4112 "Yamaha YMF262 (OPL3)",
4113#else
4114 "Yamaha YM3812 (OPL2)",
4115#endif
4116 0,
4117 1,
4118 { .init_isa = Adlib_init }
4119 },
4120#endif
4121
4122#ifdef CONFIG_GUS
4123 {
4124 "gus",
4125 "Gravis Ultrasound GF1",
4126 0,
4127 1,
4128 { .init_isa = GUS_init }
4129 },
4130#endif
4131
malc4c9b53e2009-01-09 10:46:34 +00004132#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004133 {
4134 "ac97",
4135 "Intel 82801AA AC97 Audio",
4136 0,
4137 0,
4138 { .init_pci = ac97_init }
4139 },
malc4c9b53e2009-01-09 10:46:34 +00004140#endif
balroge5c9a132008-01-14 04:27:55 +00004141
malc4c9b53e2009-01-09 10:46:34 +00004142#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004143 {
4144 "es1370",
4145 "ENSONIQ AudioPCI ES1370",
4146 0,
4147 0,
4148 { .init_pci = es1370_init }
4149 },
balrogb00052e2007-04-30 02:22:06 +00004150#endif
bellard6a36d842005-12-18 20:34:32 +00004151
malc4c9b53e2009-01-09 10:46:34 +00004152#endif /* HAS_AUDIO_CHOICE */
4153
bellard6a36d842005-12-18 20:34:32 +00004154 { NULL, NULL, 0, 0, { NULL } }
4155};
4156
bellard1d14ffa2005-10-30 18:58:22 +00004157static void select_soundhw (const char *optarg)
4158{
bellard6a36d842005-12-18 20:34:32 +00004159 struct soundhw *c;
4160
bellard1d14ffa2005-10-30 18:58:22 +00004161 if (*optarg == '?') {
4162 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004163
bellard1d14ffa2005-10-30 18:58:22 +00004164 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004165 for (c = soundhw; c->name; ++c) {
4166 printf ("%-11s %s\n", c->name, c->descr);
4167 }
4168 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004169 exit (*optarg != '?');
4170 }
4171 else {
bellard6a36d842005-12-18 20:34:32 +00004172 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004173 const char *p;
4174 char *e;
4175 int bad_card = 0;
4176
bellard6a36d842005-12-18 20:34:32 +00004177 if (!strcmp (optarg, "all")) {
4178 for (c = soundhw; c->name; ++c) {
4179 c->enabled = 1;
4180 }
4181 return;
4182 }
bellard1d14ffa2005-10-30 18:58:22 +00004183
bellard6a36d842005-12-18 20:34:32 +00004184 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004185 while (*p) {
4186 e = strchr (p, ',');
4187 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004188
4189 for (c = soundhw; c->name; ++c) {
4190 if (!strncmp (c->name, p, l)) {
4191 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004192 break;
4193 }
4194 }
bellard6a36d842005-12-18 20:34:32 +00004195
4196 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004197 if (l > 80) {
4198 fprintf (stderr,
4199 "Unknown sound card name (too big to show)\n");
4200 }
4201 else {
4202 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4203 (int) l, p);
4204 }
4205 bad_card = 1;
4206 }
4207 p += l + (e != NULL);
4208 }
4209
4210 if (bad_card)
4211 goto show_valid_cards;
4212 }
4213}
4214#endif
4215
malc3893c122008-09-28 00:42:05 +00004216static void select_vgahw (const char *p)
4217{
4218 const char *opts;
4219
4220 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004221 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004222 cirrus_vga_enabled = 0;
4223 vmsvga_enabled = 0;
4224 } else if (strstart(p, "cirrus", &opts)) {
4225 cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004226 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004227 vmsvga_enabled = 0;
4228 } else if (strstart(p, "vmware", &opts)) {
4229 cirrus_vga_enabled = 0;
aliguoric2b3b412009-01-15 20:37:28 +00004230 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004231 vmsvga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004232 } else if (strstart(p, "none", &opts)) {
4233 cirrus_vga_enabled = 0;
4234 std_vga_enabled = 0;
4235 vmsvga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004236 } else {
4237 invalid_vga:
4238 fprintf(stderr, "Unknown vga type: %s\n", p);
4239 exit(1);
4240 }
malccb5a7aa2008-09-28 00:42:12 +00004241 while (*opts) {
4242 const char *nextopt;
4243
4244 if (strstart(opts, ",retrace=", &nextopt)) {
4245 opts = nextopt;
4246 if (strstart(opts, "dumb", &nextopt))
4247 vga_retrace_method = VGA_RETRACE_DUMB;
4248 else if (strstart(opts, "precise", &nextopt))
4249 vga_retrace_method = VGA_RETRACE_PRECISE;
4250 else goto invalid_vga;
4251 } else goto invalid_vga;
4252 opts = nextopt;
4253 }
malc3893c122008-09-28 00:42:05 +00004254}
4255
bellard3587d7e2006-06-26 20:03:44 +00004256#ifdef _WIN32
4257static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4258{
4259 exit(STATUS_CONTROL_C_EXIT);
4260 return TRUE;
4261}
4262#endif
4263
aliguoric4be29f2009-04-17 18:58:14 +00004264int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004265{
4266 int ret;
4267
4268 if(strlen(str) != 36)
4269 return -1;
4270
4271 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4272 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4273 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4274
4275 if(ret != 16)
4276 return -1;
4277
aliguorib6f6e3d2009-04-17 18:59:56 +00004278#ifdef TARGET_I386
4279 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4280#endif
4281
blueswir18fcb1b92008-09-18 18:29:08 +00004282 return 0;
4283}
4284
bellard7c9d8e02005-11-15 22:16:05 +00004285#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004286
aliguori5b08fc12008-08-21 20:08:03 +00004287#ifndef _WIN32
4288
4289static void termsig_handler(int signal)
4290{
4291 qemu_system_shutdown_request();
4292}
4293
blueswir16f9e3802008-09-09 18:56:59 +00004294static void termsig_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004295{
4296 struct sigaction act;
4297
4298 memset(&act, 0, sizeof(act));
4299 act.sa_handler = termsig_handler;
4300 sigaction(SIGINT, &act, NULL);
4301 sigaction(SIGHUP, &act, NULL);
4302 sigaction(SIGTERM, &act, NULL);
4303}
4304
4305#endif
4306
malc902b3d52008-12-10 19:18:40 +00004307int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004308{
bellard67b915a2004-03-31 23:37:16 +00004309#ifdef CONFIG_GDBSTUB
aliguori59030a82009-04-05 18:43:41 +00004310 const char *gdbstub_dev = NULL;
bellard67b915a2004-03-31 23:37:16 +00004311#endif
j_mayer28c5af52007-11-11 01:50:45 +00004312 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004313 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004314 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004315 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004316 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00004317 const char *boot_devices = "";
aliguori3023f332009-01-16 19:04:14 +00004318 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004319 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004320 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004321 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004322 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004323 const char *bt_opts[MAX_BT_CMDLINE];
4324 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004325 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004326 int optind;
4327 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004328 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004329 const char *monitor_device;
4330 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004331 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004332 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004333 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004334 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4335 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004336 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004337 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004338 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004339 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004340 int usb_devices_index;
blueswir1b9e82a52009-04-05 18:03:31 +00004341#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004342 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004343#endif
bellard26a5f132008-05-28 12:30:31 +00004344 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004345 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004346 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004347#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004348 int fd = 0;
4349 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004350 const char *chroot_dir = NULL;
4351 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004352#endif
aliguori268a3622009-04-21 22:30:27 +00004353 CPUState *env;
bellard0bd48852005-11-11 00:00:47 +00004354
malc902b3d52008-12-10 19:18:40 +00004355 qemu_cache_utils_init(envp);
4356
bellard0bd48852005-11-11 00:00:47 +00004357 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004358#ifndef _WIN32
4359 {
4360 struct sigaction act;
4361 sigfillset(&act.sa_mask);
4362 act.sa_flags = 0;
4363 act.sa_handler = SIG_IGN;
4364 sigaction(SIGPIPE, &act, NULL);
4365 }
bellard3587d7e2006-06-26 20:03:44 +00004366#else
4367 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004368 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4369 QEMU to run on a single CPU */
4370 {
4371 HANDLE h;
4372 DWORD mask, smask;
4373 int i;
4374 h = GetCurrentProcess();
4375 if (GetProcessAffinityMask(h, &mask, &smask)) {
4376 for(i = 0; i < 32; i++) {
4377 if (mask & (1 << i))
4378 break;
4379 }
4380 if (i != 32) {
4381 mask = 1 << i;
4382 SetProcessAffinityMask(h, mask);
4383 }
4384 }
4385 }
bellard67b915a2004-03-31 23:37:16 +00004386#endif
bellardbe995c22006-06-25 16:25:21 +00004387
bellardcc1daa42005-06-05 14:49:17 +00004388 register_machines();
4389 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004390 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004391 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004392 ram_size = 0;
bellard313aa562003-08-10 21:52:11 +00004393 vga_ram_size = VGA_RAM_SIZE;
bellard33e39632003-07-06 17:15:21 +00004394 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004395 nographic = 0;
balrog4d3b6f62008-02-10 16:33:14 +00004396 curses = 0;
bellarda20dd502003-09-30 21:07:02 +00004397 kernel_filename = NULL;
4398 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004399 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004400 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00004401 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00004402
aurel32c75a8232008-05-04 00:50:34 +00004403 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004404 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004405 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004406 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004407
aliguori8290edd2009-02-27 20:14:29 +00004408 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004409 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004410 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004411 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004412
aliguori1b8fc812009-02-27 20:01:39 +00004413 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004414 virtio_consoles[i] = NULL;
4415 virtio_console_index = 0;
4416
aliguori268a3622009-04-21 22:30:27 +00004417 for (i = 0; i < MAX_NODES; i++) {
4418 node_mem[i] = 0;
4419 node_cpumask[i] = 0;
4420 }
4421
bellarda594cfb2005-11-06 16:13:29 +00004422 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004423
bellard7c9d8e02005-11-15 22:16:05 +00004424 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004425 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004426 nb_drives = 0;
4427 nb_drives_opt = 0;
aliguori268a3622009-04-21 22:30:27 +00004428 nb_numa_nodes = 0;
thse4bcb142007-12-02 04:51:10 +00004429 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004430
4431 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004432
bellard26a5f132008-05-28 12:30:31 +00004433 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004434 autostart= 1;
4435
bellardcd6f1162004-05-13 22:02:20 +00004436 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004437 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004438 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004439 break;
bellardcd6f1162004-05-13 22:02:20 +00004440 r = argv[optind];
4441 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004442 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004443 } else {
4444 const QEMUOption *popt;
4445
4446 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004447 /* Treat --foo the same as -foo. */
4448 if (r[1] == '-')
4449 r++;
bellardcd6f1162004-05-13 22:02:20 +00004450 popt = qemu_options;
4451 for(;;) {
4452 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004453 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004454 argv[0], r);
4455 exit(1);
4456 }
4457 if (!strcmp(popt->name, r + 1))
4458 break;
4459 popt++;
4460 }
4461 if (popt->flags & HAS_ARG) {
4462 if (optind >= argc) {
4463 fprintf(stderr, "%s: option '%s' requires an argument\n",
4464 argv[0], r);
4465 exit(1);
4466 }
4467 optarg = argv[optind++];
4468 } else {
4469 optarg = NULL;
4470 }
4471
4472 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004473 case QEMU_OPTION_M:
4474 machine = find_machine(optarg);
4475 if (!machine) {
4476 QEMUMachine *m;
4477 printf("Supported machines are:\n");
4478 for(m = first_machine; m != NULL; m = m->next) {
4479 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004480 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00004481 m == first_machine ? " (default)" : "");
4482 }
ths15f82202007-06-29 23:26:08 +00004483 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004484 }
4485 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004486 case QEMU_OPTION_cpu:
4487 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004488 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004489/* XXX: implement xxx_cpu_list for targets that still miss it */
4490#if defined(cpu_list)
4491 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004492#endif
ths15f82202007-06-29 23:26:08 +00004493 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004494 } else {
4495 cpu_model = optarg;
4496 }
4497 break;
bellardcd6f1162004-05-13 22:02:20 +00004498 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004499 initrd_filename = optarg;
4500 break;
bellardcd6f1162004-05-13 22:02:20 +00004501 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004502 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00004503 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004504 else
balrog609497a2008-01-14 02:56:53 +00004505 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004506 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004507 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004508 translation == BIOS_ATA_TRANSLATION_LBA ?
4509 ",trans=lba" :
4510 translation == BIOS_ATA_TRANSLATION_NONE ?
4511 ",trans=none" : "");
4512 break;
bellardcd6f1162004-05-13 22:02:20 +00004513 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004514 case QEMU_OPTION_hdc:
4515 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004516 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004517 break;
thse4bcb142007-12-02 04:51:10 +00004518 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004519 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004520 break;
balrog3e3d5812007-04-30 02:09:25 +00004521 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004522 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004523 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004524 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004525 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004526 break;
j_mayer86f55662007-04-24 06:52:59 +00004527 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004528 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004529 break;
bellardcd6f1162004-05-13 22:02:20 +00004530 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004531 snapshot = 1;
4532 break;
bellardcd6f1162004-05-13 22:02:20 +00004533 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004534 {
bellard330d0412003-07-26 18:11:40 +00004535 const char *p;
4536 p = optarg;
4537 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004538 if (cyls < 1 || cyls > 16383)
4539 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004540 if (*p != ',')
4541 goto chs_fail;
4542 p++;
4543 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004544 if (heads < 1 || heads > 16)
4545 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004546 if (*p != ',')
4547 goto chs_fail;
4548 p++;
4549 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004550 if (secs < 1 || secs > 63)
4551 goto chs_fail;
4552 if (*p == ',') {
4553 p++;
4554 if (!strcmp(p, "none"))
4555 translation = BIOS_ATA_TRANSLATION_NONE;
4556 else if (!strcmp(p, "lba"))
4557 translation = BIOS_ATA_TRANSLATION_LBA;
4558 else if (!strcmp(p, "auto"))
4559 translation = BIOS_ATA_TRANSLATION_AUTO;
4560 else
4561 goto chs_fail;
4562 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004563 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004564 fprintf(stderr, "qemu: invalid physical CHS format\n");
4565 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004566 }
thse4bcb142007-12-02 04:51:10 +00004567 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00004568 snprintf(drives_opt[hda_index].opt,
4569 sizeof(drives_opt[hda_index].opt),
4570 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4571 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004572 translation == BIOS_ATA_TRANSLATION_LBA ?
4573 ",trans=lba" :
4574 translation == BIOS_ATA_TRANSLATION_NONE ?
4575 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00004576 }
4577 break;
aliguori268a3622009-04-21 22:30:27 +00004578 case QEMU_OPTION_numa:
4579 if (nb_numa_nodes >= MAX_NODES) {
4580 fprintf(stderr, "qemu: too many NUMA nodes\n");
4581 exit(1);
4582 }
4583 numa_add(optarg);
4584 break;
bellardcd6f1162004-05-13 22:02:20 +00004585 case QEMU_OPTION_nographic:
bellarda20dd502003-09-30 21:07:02 +00004586 nographic = 1;
4587 break;
balrog4d3b6f62008-02-10 16:33:14 +00004588#ifdef CONFIG_CURSES
4589 case QEMU_OPTION_curses:
4590 curses = 1;
4591 break;
4592#endif
balroga171fe32007-04-30 01:48:07 +00004593 case QEMU_OPTION_portrait:
4594 graphic_rotate = 1;
4595 break;
bellardcd6f1162004-05-13 22:02:20 +00004596 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004597 kernel_filename = optarg;
4598 break;
bellardcd6f1162004-05-13 22:02:20 +00004599 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004600 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004601 break;
bellardcd6f1162004-05-13 22:02:20 +00004602 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004603 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004604 break;
bellardcd6f1162004-05-13 22:02:20 +00004605 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004606 boot_devices = optarg;
4607 /* We just do some generic consistency checks */
4608 {
4609 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00004610 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00004611
4612 boot_devices_bitmap = 0;
4613 for (p = boot_devices; *p != '\0'; p++) {
4614 /* Allowed boot devices are:
4615 * a b : floppy disk drives
4616 * c ... f : IDE disk drives
4617 * g ... m : machine implementation dependant drives
4618 * n ... p : network devices
4619 * It's up to each machine implementation to check
4620 * if the given boot devices match the actual hardware
4621 * implementation and firmware features.
4622 */
4623 if (*p < 'a' || *p > 'q') {
4624 fprintf(stderr, "Invalid boot device '%c'\n", *p);
4625 exit(1);
4626 }
4627 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4628 fprintf(stderr,
4629 "Boot device '%c' was given twice\n",*p);
4630 exit(1);
4631 }
4632 boot_devices_bitmap |= 1 << (*p - 'a');
4633 }
bellard36b486b2003-11-11 13:36:08 +00004634 }
4635 break;
bellardcd6f1162004-05-13 22:02:20 +00004636 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004637 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004638 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004639 break;
bellard52ca8d62006-06-14 16:03:05 +00004640#ifdef TARGET_I386
4641 case QEMU_OPTION_no_fd_bootchk:
4642 fd_bootchk = 0;
4643 break;
4644#endif
bellard7c9d8e02005-11-15 22:16:05 +00004645 case QEMU_OPTION_net:
4646 if (nb_net_clients >= MAX_NET_CLIENTS) {
4647 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00004648 exit(1);
4649 }
pbrookfd5f3932008-03-26 20:55:43 +00004650 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00004651 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00004652 break;
bellardc7f74642004-08-24 21:57:12 +00004653#ifdef CONFIG_SLIRP
4654 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00004655 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004656 break;
ths47d5d012007-02-20 00:05:08 +00004657 case QEMU_OPTION_bootp:
4658 bootp_filename = optarg;
4659 break;
bellardc94c8d62004-09-13 21:37:34 +00004660#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004661 case QEMU_OPTION_smb:
4662 net_slirp_smb(optarg);
4663 break;
bellardc94c8d62004-09-13 21:37:34 +00004664#endif
bellard9bf05442004-08-25 22:12:49 +00004665 case QEMU_OPTION_redir:
aliguorid4ebe192009-04-21 19:56:44 +00004666 net_slirp_redir(NULL, optarg);
bellard9bf05442004-08-25 22:12:49 +00004667 break;
bellardc7f74642004-08-24 21:57:12 +00004668#endif
balrogdc72ac12008-11-09 00:04:26 +00004669 case QEMU_OPTION_bt:
4670 if (nb_bt_opts >= MAX_BT_CMDLINE) {
4671 fprintf(stderr, "qemu: too many bluetooth options\n");
4672 exit(1);
4673 }
4674 bt_opts[nb_bt_opts++] = optarg;
4675 break;
bellard1d14ffa2005-10-30 18:58:22 +00004676#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004677 case QEMU_OPTION_audio_help:
4678 AUD_help ();
4679 exit (0);
4680 break;
4681 case QEMU_OPTION_soundhw:
4682 select_soundhw (optarg);
4683 break;
4684#endif
bellardcd6f1162004-05-13 22:02:20 +00004685 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00004686 help(0);
bellardcd6f1162004-05-13 22:02:20 +00004687 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00004688 case QEMU_OPTION_version:
4689 version();
4690 exit(0);
4691 break;
aurel3200f82b82008-04-27 21:12:55 +00004692 case QEMU_OPTION_m: {
4693 uint64_t value;
4694 char *ptr;
4695
4696 value = strtoul(optarg, &ptr, 10);
4697 switch (*ptr) {
4698 case 0: case 'M': case 'm':
4699 value <<= 20;
4700 break;
4701 case 'G': case 'g':
4702 value <<= 30;
4703 break;
4704 default:
4705 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00004706 exit(1);
4707 }
aurel3200f82b82008-04-27 21:12:55 +00004708
4709 /* On 32-bit hosts, QEMU is limited by virtual address space */
4710 if (value > (2047 << 20)
blueswir1640f42e2009-04-19 10:18:01 +00004711#ifndef CONFIG_KQEMU
aurel3200f82b82008-04-27 21:12:55 +00004712 && HOST_LONG_BITS == 32
4713#endif
4714 ) {
4715 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4716 exit(1);
4717 }
4718 if (value != (uint64_t)(ram_addr_t)value) {
4719 fprintf(stderr, "qemu: ram size too large\n");
4720 exit(1);
4721 }
4722 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00004723 break;
aurel3200f82b82008-04-27 21:12:55 +00004724 }
bellardcd6f1162004-05-13 22:02:20 +00004725 case QEMU_OPTION_d:
4726 {
4727 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00004728 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00004729
bellardcd6f1162004-05-13 22:02:20 +00004730 mask = cpu_str_to_log_mask(optarg);
4731 if (!mask) {
4732 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00004733 for(item = cpu_log_items; item->mask != 0; item++) {
4734 printf("%-10s %s\n", item->name, item->help);
4735 }
4736 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00004737 }
4738 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00004739 }
bellardcd6f1162004-05-13 22:02:20 +00004740 break;
bellard67b915a2004-03-31 23:37:16 +00004741#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00004742 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00004743 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00004744 break;
aliguori59030a82009-04-05 18:43:41 +00004745 case QEMU_OPTION_gdb:
4746 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00004747 break;
bellard67b915a2004-03-31 23:37:16 +00004748#endif
bellardcd6f1162004-05-13 22:02:20 +00004749 case QEMU_OPTION_L:
4750 bios_dir = optarg;
4751 break;
j_mayer1192dad2007-10-05 13:08:35 +00004752 case QEMU_OPTION_bios:
4753 bios_name = optarg;
4754 break;
aurel321b530a62009-04-05 20:08:59 +00004755 case QEMU_OPTION_singlestep:
4756 singlestep = 1;
4757 break;
bellardcd6f1162004-05-13 22:02:20 +00004758 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00004759 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00004760 break;
blueswir15824d652009-03-28 06:44:27 +00004761#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00004762 case QEMU_OPTION_k:
4763 keyboard_layout = optarg;
4764 break;
blueswir15824d652009-03-28 06:44:27 +00004765#endif
bellardee22c2f2004-06-03 12:49:50 +00004766 case QEMU_OPTION_localtime:
4767 rtc_utc = 0;
4768 break;
malc3893c122008-09-28 00:42:05 +00004769 case QEMU_OPTION_vga:
4770 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00004771 break;
blueswir15824d652009-03-28 06:44:27 +00004772#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00004773 case QEMU_OPTION_g:
4774 {
4775 const char *p;
4776 int w, h, depth;
4777 p = optarg;
4778 w = strtol(p, (char **)&p, 10);
4779 if (w <= 0) {
4780 graphic_error:
4781 fprintf(stderr, "qemu: invalid resolution or depth\n");
4782 exit(1);
4783 }
4784 if (*p != 'x')
4785 goto graphic_error;
4786 p++;
4787 h = strtol(p, (char **)&p, 10);
4788 if (h <= 0)
4789 goto graphic_error;
4790 if (*p == 'x') {
4791 p++;
4792 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00004793 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00004794 depth != 24 && depth != 32)
4795 goto graphic_error;
4796 } else if (*p == '\0') {
4797 depth = graphic_depth;
4798 } else {
4799 goto graphic_error;
4800 }
ths3b46e622007-09-17 08:09:54 +00004801
bellarde9b137c2004-06-21 16:46:10 +00004802 graphic_width = w;
4803 graphic_height = h;
4804 graphic_depth = depth;
4805 }
4806 break;
blueswir15824d652009-03-28 06:44:27 +00004807#endif
ths20d8a3e2007-02-18 17:04:49 +00004808 case QEMU_OPTION_echr:
4809 {
4810 char *r;
4811 term_escape_char = strtol(optarg, &r, 0);
4812 if (r == optarg)
4813 printf("Bad argument to echr\n");
4814 break;
4815 }
bellard82c643f2004-07-14 17:28:13 +00004816 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00004817 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00004818 break;
4819 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00004820 if (serial_device_index >= MAX_SERIAL_PORTS) {
4821 fprintf(stderr, "qemu: too many serial ports\n");
4822 exit(1);
4823 }
pbrookfd5f3932008-03-26 20:55:43 +00004824 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00004825 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00004826 break;
aliguori51ecf132009-01-15 20:06:40 +00004827 case QEMU_OPTION_virtiocon:
4828 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
4829 fprintf(stderr, "qemu: too many virtio consoles\n");
4830 exit(1);
4831 }
4832 virtio_consoles[virtio_console_index] = optarg;
4833 virtio_console_index++;
4834 break;
bellard6508fe52005-01-15 12:02:56 +00004835 case QEMU_OPTION_parallel:
4836 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
4837 fprintf(stderr, "qemu: too many parallel ports\n");
4838 exit(1);
4839 }
pbrookfd5f3932008-03-26 20:55:43 +00004840 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00004841 parallel_device_index++;
4842 break;
bellardd63d3072004-10-03 13:29:03 +00004843 case QEMU_OPTION_loadvm:
4844 loadvm = optarg;
4845 break;
4846 case QEMU_OPTION_full_screen:
4847 full_screen = 1;
4848 break;
ths667acca2006-12-11 02:08:05 +00004849#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00004850 case QEMU_OPTION_no_frame:
4851 no_frame = 1;
4852 break;
ths3780e192007-06-21 21:08:02 +00004853 case QEMU_OPTION_alt_grab:
4854 alt_grab = 1;
4855 break;
ths667acca2006-12-11 02:08:05 +00004856 case QEMU_OPTION_no_quit:
4857 no_quit = 1;
4858 break;
aliguori7d957bd2009-01-15 22:14:11 +00004859 case QEMU_OPTION_sdl:
4860 sdl = 1;
4861 break;
ths667acca2006-12-11 02:08:05 +00004862#endif
bellardf7cce892004-12-08 22:21:25 +00004863 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00004864 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00004865 break;
bellarda09db212005-04-30 16:10:35 +00004866#ifdef TARGET_I386
4867 case QEMU_OPTION_win2k_hack:
4868 win2k_install_hack = 1;
4869 break;
aliguori73822ec2009-01-15 20:11:34 +00004870 case QEMU_OPTION_rtc_td_hack:
4871 rtc_td_hack = 1;
4872 break;
aliguori8a92ea22009-02-27 20:12:36 +00004873 case QEMU_OPTION_acpitable:
4874 if(acpi_table_add(optarg) < 0) {
4875 fprintf(stderr, "Wrong acpi table provided\n");
4876 exit(1);
4877 }
4878 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00004879 case QEMU_OPTION_smbios:
4880 if(smbios_entry_add(optarg) < 0) {
4881 fprintf(stderr, "Wrong smbios provided\n");
4882 exit(1);
4883 }
4884 break;
bellarda09db212005-04-30 16:10:35 +00004885#endif
blueswir1640f42e2009-04-19 10:18:01 +00004886#ifdef CONFIG_KQEMU
bellardd993e022005-02-10 22:00:06 +00004887 case QEMU_OPTION_no_kqemu:
4888 kqemu_allowed = 0;
4889 break;
bellard89bfc102006-02-08 22:46:31 +00004890 case QEMU_OPTION_kernel_kqemu:
4891 kqemu_allowed = 2;
4892 break;
bellardd993e022005-02-10 22:00:06 +00004893#endif
aliguori7ba1e612008-11-05 16:04:33 +00004894#ifdef CONFIG_KVM
4895 case QEMU_OPTION_enable_kvm:
4896 kvm_allowed = 1;
blueswir1640f42e2009-04-19 10:18:01 +00004897#ifdef CONFIG_KQEMU
aliguori7ba1e612008-11-05 16:04:33 +00004898 kqemu_allowed = 0;
4899#endif
4900 break;
4901#endif
bellardbb36d472005-11-05 14:22:28 +00004902 case QEMU_OPTION_usb:
4903 usb_enabled = 1;
4904 break;
bellarda594cfb2005-11-06 16:13:29 +00004905 case QEMU_OPTION_usbdevice:
4906 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00004907 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00004908 fprintf(stderr, "Too many USB devices\n");
4909 exit(1);
4910 }
pbrookfd5f3932008-03-26 20:55:43 +00004911 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00004912 usb_devices_index++;
4913 break;
bellard6a00d602005-11-21 23:25:50 +00004914 case QEMU_OPTION_smp:
4915 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00004916 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00004917 fprintf(stderr, "Invalid number of CPUs\n");
4918 exit(1);
4919 }
4920 break;
bellard24236862006-04-30 21:28:36 +00004921 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00004922 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00004923 break;
blueswir15824d652009-03-28 06:44:27 +00004924#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00004925 case QEMU_OPTION_no_acpi:
4926 acpi_enabled = 0;
4927 break;
aliguori16b29ae2008-12-17 23:28:44 +00004928 case QEMU_OPTION_no_hpet:
4929 no_hpet = 1;
4930 break;
blueswir15824d652009-03-28 06:44:27 +00004931#endif
bellardd1beab82006-10-02 19:44:22 +00004932 case QEMU_OPTION_no_reboot:
4933 no_reboot = 1;
4934 break;
aurel32b2f76162008-04-11 21:35:52 +00004935 case QEMU_OPTION_no_shutdown:
4936 no_shutdown = 1;
4937 break;
balrog9467cd42007-05-01 01:34:14 +00004938 case QEMU_OPTION_show_cursor:
4939 cursor_hide = 0;
4940 break;
blueswir18fcb1b92008-09-18 18:29:08 +00004941 case QEMU_OPTION_uuid:
4942 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
4943 fprintf(stderr, "Fail to parse UUID string."
4944 " Wrong format.\n");
4945 exit(1);
4946 }
4947 break;
blueswir15824d652009-03-28 06:44:27 +00004948#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004949 case QEMU_OPTION_daemonize:
4950 daemonize = 1;
4951 break;
blueswir15824d652009-03-28 06:44:27 +00004952#endif
ths9ae02552007-01-05 17:39:04 +00004953 case QEMU_OPTION_option_rom:
4954 if (nb_option_roms >= MAX_OPTION_ROMS) {
4955 fprintf(stderr, "Too many option ROMs\n");
4956 exit(1);
4957 }
4958 option_rom[nb_option_roms] = optarg;
4959 nb_option_roms++;
4960 break;
blueswir15824d652009-03-28 06:44:27 +00004961#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00004962 case QEMU_OPTION_semihosting:
4963 semihosting_enabled = 1;
4964 break;
blueswir15824d652009-03-28 06:44:27 +00004965#endif
thsc35734b2007-03-19 15:17:08 +00004966 case QEMU_OPTION_name:
4967 qemu_name = optarg;
4968 break;
blueswir195efd112008-12-24 20:26:14 +00004969#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00004970 case QEMU_OPTION_prom_env:
4971 if (nb_prom_envs >= MAX_PROM_ENVS) {
4972 fprintf(stderr, "Too many prom variables\n");
4973 exit(1);
4974 }
4975 prom_envs[nb_prom_envs] = optarg;
4976 nb_prom_envs++;
4977 break;
4978#endif
balrog2b8f2d42007-07-27 22:08:46 +00004979#ifdef TARGET_ARM
4980 case QEMU_OPTION_old_param:
4981 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00004982 break;
balrog2b8f2d42007-07-27 22:08:46 +00004983#endif
thsf3dcfad2007-08-24 01:26:02 +00004984 case QEMU_OPTION_clock:
4985 configure_alarms(optarg);
4986 break;
bellard7e0af5d02007-11-07 16:24:33 +00004987 case QEMU_OPTION_startdate:
4988 {
4989 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00004990 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00004991 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00004992 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00004993 } else {
4994 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4995 &tm.tm_year,
4996 &tm.tm_mon,
4997 &tm.tm_mday,
4998 &tm.tm_hour,
4999 &tm.tm_min,
5000 &tm.tm_sec) == 6) {
5001 /* OK */
5002 } else if (sscanf(optarg, "%d-%d-%d",
5003 &tm.tm_year,
5004 &tm.tm_mon,
5005 &tm.tm_mday) == 3) {
5006 tm.tm_hour = 0;
5007 tm.tm_min = 0;
5008 tm.tm_sec = 0;
5009 } else {
5010 goto date_fail;
5011 }
5012 tm.tm_year -= 1900;
5013 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005014 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005015 if (rtc_start_date == -1) {
5016 date_fail:
5017 fprintf(stderr, "Invalid date format. Valid format are:\n"
5018 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5019 exit(1);
5020 }
balrogf6503052008-02-17 11:42:19 +00005021 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005022 }
5023 }
5024 break;
bellard26a5f132008-05-28 12:30:31 +00005025 case QEMU_OPTION_tb_size:
5026 tb_size = strtol(optarg, NULL, 0);
5027 if (tb_size < 0)
5028 tb_size = 0;
5029 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005030 case QEMU_OPTION_icount:
5031 use_icount = 1;
5032 if (strcmp(optarg, "auto") == 0) {
5033 icount_time_shift = -1;
5034 } else {
5035 icount_time_shift = strtol(optarg, NULL, 0);
5036 }
5037 break;
aliguori5bb79102008-10-13 03:12:02 +00005038 case QEMU_OPTION_incoming:
5039 incoming = optarg;
5040 break;
blueswir15824d652009-03-28 06:44:27 +00005041#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005042 case QEMU_OPTION_chroot:
5043 chroot_dir = optarg;
5044 break;
5045 case QEMU_OPTION_runas:
5046 run_as = optarg;
5047 break;
blueswir15824d652009-03-28 06:44:27 +00005048#endif
bellardcd6f1162004-05-13 22:02:20 +00005049 }
bellard0824d6f2003-06-24 13:42:40 +00005050 }
5051 }
bellard330d0412003-07-26 18:11:40 +00005052
blueswir1640f42e2009-04-19 10:18:01 +00005053#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
aliguori7ba1e612008-11-05 16:04:33 +00005054 if (kvm_allowed && kqemu_allowed) {
5055 fprintf(stderr,
5056 "You can not enable both KVM and kqemu at the same time\n");
5057 exit(1);
5058 }
5059#endif
5060
balrog3d878ca2008-10-28 10:59:59 +00005061 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005062 if (smp_cpus > machine->max_cpus) {
5063 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5064 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5065 machine->max_cpus);
5066 exit(1);
5067 }
5068
aliguoribc0129d2008-08-01 15:12:34 +00005069 if (nographic) {
5070 if (serial_device_index == 0)
5071 serial_devices[0] = "stdio";
5072 if (parallel_device_index == 0)
5073 parallel_devices[0] = "null";
5074 if (strncmp(monitor_device, "vc", 2) == 0)
5075 monitor_device = "stdio";
5076 }
5077
ths71e3ceb2006-12-22 02:11:31 +00005078#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005079 if (daemonize) {
5080 pid_t pid;
5081
5082 if (pipe(fds) == -1)
5083 exit(1);
5084
5085 pid = fork();
5086 if (pid > 0) {
5087 uint8_t status;
5088 ssize_t len;
5089
5090 close(fds[1]);
5091
5092 again:
ths93815bc2007-03-19 15:58:31 +00005093 len = read(fds[0], &status, 1);
5094 if (len == -1 && (errno == EINTR))
5095 goto again;
5096
5097 if (len != 1)
5098 exit(1);
5099 else if (status == 1) {
5100 fprintf(stderr, "Could not acquire pidfile\n");
5101 exit(1);
5102 } else
5103 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005104 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005105 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005106
5107 setsid();
5108
5109 pid = fork();
5110 if (pid > 0)
5111 exit(0);
5112 else if (pid < 0)
5113 exit(1);
5114
5115 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005116
5117 signal(SIGTSTP, SIG_IGN);
5118 signal(SIGTTOU, SIG_IGN);
5119 signal(SIGTTIN, SIG_IGN);
5120 }
ths71e3ceb2006-12-22 02:11:31 +00005121
thsaa26bb22007-03-25 21:33:06 +00005122 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005123 if (daemonize) {
5124 uint8_t status = 1;
5125 write(fds[1], &status, 1);
5126 } else
5127 fprintf(stderr, "Could not acquire pid file\n");
5128 exit(1);
5129 }
blueswir1b9e82a52009-04-05 18:03:31 +00005130#endif
ths93815bc2007-03-19 15:58:31 +00005131
blueswir1640f42e2009-04-19 10:18:01 +00005132#ifdef CONFIG_KQEMU
bellardff3fbb32006-01-08 10:53:14 +00005133 if (smp_cpus > 1)
5134 kqemu_allowed = 0;
5135#endif
bellarda20dd502003-09-30 21:07:02 +00005136 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00005137 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00005138
thsf8d39c02008-07-03 10:01:15 +00005139 if (!linux_boot && *kernel_cmdline != '\0') {
5140 fprintf(stderr, "-append only allowed with -kernel option\n");
5141 exit(1);
5142 }
5143
5144 if (!linux_boot && initrd_filename != NULL) {
5145 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5146 exit(1);
5147 }
5148
ths96d30e42007-01-07 20:42:14 +00005149 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00005150 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00005151 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00005152 }
bellardb118d612003-06-30 23:36:21 +00005153 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005154
pbrook634fce92006-07-15 17:40:09 +00005155 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005156 if (init_timer_alarm() < 0) {
5157 fprintf(stderr, "could not initialize alarm timer\n");
5158 exit(1);
5159 }
pbrook2e70f6e2008-06-29 01:03:05 +00005160 if (use_icount && icount_time_shift < 0) {
5161 use_icount = 2;
5162 /* 125MIPS seems a reasonable initial guess at the guest speed.
5163 It will be corrected fairly quickly anyway. */
5164 icount_time_shift = 3;
5165 init_icount_adjust();
5166 }
pbrook634fce92006-07-15 17:40:09 +00005167
bellardfd1dff42006-02-01 21:29:26 +00005168#ifdef _WIN32
5169 socket_init();
5170#endif
5171
bellard7c9d8e02005-11-15 22:16:05 +00005172 /* init network clients */
5173 if (nb_net_clients == 0) {
5174 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005175 net_clients[nb_net_clients++] = "nic";
5176#ifdef CONFIG_SLIRP
5177 net_clients[nb_net_clients++] = "user";
5178#endif
bellardc20709a2004-04-21 23:27:19 +00005179 }
5180
bellard7c9d8e02005-11-15 22:16:05 +00005181 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005182 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005183 exit(1);
bellard702c6512004-04-02 21:21:32 +00005184 }
aliguori63a01ef2008-10-31 19:10:00 +00005185 net_client_check();
bellardf1510b22003-06-25 00:07:40 +00005186
thseec85c22007-01-05 17:41:07 +00005187#ifdef TARGET_I386
balroged494d82007-12-11 23:23:52 +00005188 /* XXX: this should be moved in the PC machine instantiation code */
j_mayer28c5af52007-11-11 01:50:45 +00005189 if (net_boot != 0) {
5190 int netroms = 0;
5191 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00005192 const char *model = nd_table[i].model;
5193 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00005194 if (net_boot & (1 << i)) {
5195 if (model == NULL)
5196 model = "ne2k_pci";
5197 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5198 if (get_image_size(buf) > 0) {
5199 if (nb_option_roms >= MAX_OPTION_ROMS) {
5200 fprintf(stderr, "Too many option ROMs\n");
5201 exit(1);
5202 }
5203 option_rom[nb_option_roms] = strdup(buf);
5204 nb_option_roms++;
5205 netroms++;
5206 }
5207 }
thseec85c22007-01-05 17:41:07 +00005208 }
j_mayer28c5af52007-11-11 01:50:45 +00005209 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00005210 fprintf(stderr, "No valid PXE rom found for network device\n");
5211 exit(1);
5212 }
thseec85c22007-01-05 17:41:07 +00005213 }
5214#endif
5215
balrogdc72ac12008-11-09 00:04:26 +00005216 /* init the bluetooth world */
5217 for (i = 0; i < nb_bt_opts; i++)
5218 if (bt_parse(bt_opts[i]))
5219 exit(1);
5220
bellard0824d6f2003-06-24 13:42:40 +00005221 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005222 if (ram_size == 0)
5223 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005224
blueswir1640f42e2009-04-19 10:18:01 +00005225#ifdef CONFIG_KQEMU
pbrook94a6b542009-04-11 17:15:54 +00005226 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5227 guest ram allocation. It needs to go away. */
5228 if (kqemu_allowed) {
5229 kqemu_phys_ram_size = ram_size + VGA_RAM_SIZE + 4 * 1024 * 1024;
5230 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5231 if (!kqemu_phys_ram_base) {
5232 fprintf(stderr, "Could not allocate physical memory\n");
5233 exit(1);
5234 }
balrog7fb4fdc2008-04-24 17:59:27 +00005235 }
pbrook94a6b542009-04-11 17:15:54 +00005236#endif
bellard0824d6f2003-06-24 13:42:40 +00005237
bellard26a5f132008-05-28 12:30:31 +00005238 /* init the dynamic translator */
5239 cpu_exec_init_all(tb_size * 1024 * 1024);
5240
bellard5905b2e2004-08-01 21:53:26 +00005241 bdrv_init();
aliguori6512a2a2009-03-20 18:26:07 +00005242 dma_helper_init();
thse4bcb142007-12-02 04:51:10 +00005243
5244 /* we always create the cdrom drive, even if no disk is there */
5245
5246 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005247 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005248
balrog9d413d12007-12-04 00:10:34 +00005249 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005250
5251 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005252 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005253
balrog9d413d12007-12-04 00:10:34 +00005254 /* we always create one sd slot, even if no card is in it */
5255
5256 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005257 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005258
ths96d30e42007-01-07 20:42:14 +00005259 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005260
thse4bcb142007-12-02 04:51:10 +00005261 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005262 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005263 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005264
bellardc88676f2006-08-06 13:36:11 +00005265 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005266 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005267
aliguori3023f332009-01-16 19:04:14 +00005268#ifndef _WIN32
5269 /* must be after terminal init, SDL library changes signal handlers */
5270 termsig_setup();
5271#endif
5272
5273 /* Maintain compatibility with multiple stdio monitors */
5274 if (!strcmp(monitor_device,"stdio")) {
5275 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5276 const char *devname = serial_devices[i];
5277 if (devname && !strcmp(devname,"mon:stdio")) {
5278 monitor_device = NULL;
5279 break;
5280 } else if (devname && !strcmp(devname,"stdio")) {
5281 monitor_device = NULL;
5282 serial_devices[i] = "mon:stdio";
5283 break;
5284 }
5285 }
5286 }
5287
aliguori268a3622009-04-21 22:30:27 +00005288 if (nb_numa_nodes > 0) {
5289 int i;
5290
5291 if (nb_numa_nodes > smp_cpus) {
5292 nb_numa_nodes = smp_cpus;
5293 }
5294
5295 /* If no memory size if given for any node, assume the default case
5296 * and distribute the available memory equally across all nodes
5297 */
5298 for (i = 0; i < nb_numa_nodes; i++) {
5299 if (node_mem[i] != 0)
5300 break;
5301 }
5302 if (i == nb_numa_nodes) {
5303 uint64_t usedmem = 0;
5304
5305 /* On Linux, the each node's border has to be 8MB aligned,
5306 * the final node gets the rest.
5307 */
5308 for (i = 0; i < nb_numa_nodes - 1; i++) {
5309 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5310 usedmem += node_mem[i];
5311 }
5312 node_mem[i] = ram_size - usedmem;
5313 }
5314
5315 for (i = 0; i < nb_numa_nodes; i++) {
5316 if (node_cpumask[i] != 0)
5317 break;
5318 }
5319 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5320 * must cope with this anyway, because there are BIOSes out there in
5321 * real machines which also use this scheme.
5322 */
5323 if (i == nb_numa_nodes) {
5324 for (i = 0; i < smp_cpus; i++) {
5325 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5326 }
5327 }
5328 }
5329
aliguori3023f332009-01-16 19:04:14 +00005330 if (kvm_enabled()) {
5331 int ret;
5332
5333 ret = kvm_init(smp_cpus);
5334 if (ret < 0) {
5335 fprintf(stderr, "failed to initialize KVM\n");
5336 exit(1);
5337 }
5338 }
5339
aliguori4c621802009-01-16 21:48:20 +00005340 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005341 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005342 if (!monitor_hd) {
5343 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5344 exit(1);
5345 }
5346 }
5347
aliguori2796dae2009-01-16 20:23:27 +00005348 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5349 const char *devname = serial_devices[i];
5350 if (devname && strcmp(devname, "none")) {
5351 char label[32];
5352 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005353 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005354 if (!serial_hds[i]) {
5355 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5356 devname);
5357 exit(1);
5358 }
5359 }
5360 }
5361
5362 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5363 const char *devname = parallel_devices[i];
5364 if (devname && strcmp(devname, "none")) {
5365 char label[32];
5366 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005367 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005368 if (!parallel_hds[i]) {
5369 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5370 devname);
5371 exit(1);
5372 }
5373 }
5374 }
5375
5376 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5377 const char *devname = virtio_consoles[i];
5378 if (devname && strcmp(devname, "none")) {
5379 char label[32];
5380 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005381 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005382 if (!virtcon_hds[i]) {
5383 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5384 devname);
5385 exit(1);
5386 }
5387 }
5388 }
5389
aliguori3023f332009-01-16 19:04:14 +00005390 machine->init(ram_size, vga_ram_size, boot_devices,
5391 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5392
aliguori268a3622009-04-21 22:30:27 +00005393
5394 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5395 for (i = 0; i < nb_numa_nodes; i++) {
5396 if (node_cpumask[i] & (1 << env->cpu_index)) {
5397 env->numa_node = i;
5398 }
5399 }
5400 }
5401
aliguori6f338c32009-02-11 15:21:54 +00005402 current_machine = machine;
5403
aliguori3023f332009-01-16 19:04:14 +00005404 /* Set KVM's vcpu state to qemu's initial CPUState. */
5405 if (kvm_enabled()) {
5406 int ret;
5407
5408 ret = kvm_sync_vcpus();
5409 if (ret < 0) {
5410 fprintf(stderr, "failed to initialize vcpus\n");
5411 exit(1);
5412 }
5413 }
5414
5415 /* init USB devices */
5416 if (usb_enabled) {
5417 for(i = 0; i < usb_devices_index; i++) {
aliguoric0f4ce72009-03-05 23:01:01 +00005418 if (usb_device_add(usb_devices[i], 0) < 0) {
aliguori3023f332009-01-16 19:04:14 +00005419 fprintf(stderr, "Warning: could not add USB device %s\n",
5420 usb_devices[i]);
5421 }
5422 }
5423 }
5424
aliguori8f391ab2009-01-19 16:34:10 +00005425 if (!display_state)
5426 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005427 /* just use the first displaystate for the moment */
5428 ds = display_state;
bellard313aa562003-08-10 21:52:11 +00005429 /* terminal init */
bellarda20dd502003-09-30 21:07:02 +00005430 if (nographic) {
balrog4d3b6f62008-02-10 16:33:14 +00005431 if (curses) {
5432 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5433 exit(1);
5434 }
aliguori7d957bd2009-01-15 22:14:11 +00005435 } else {
balrog4d3b6f62008-02-10 16:33:14 +00005436#if defined(CONFIG_CURSES)
aliguori7d957bd2009-01-15 22:14:11 +00005437 if (curses) {
5438 /* At the moment curses cannot be used with other displays */
5439 curses_display_init(ds, full_screen);
5440 } else
balrog4d3b6f62008-02-10 16:33:14 +00005441#endif
aliguori7d957bd2009-01-15 22:14:11 +00005442 {
5443 if (vnc_display != NULL) {
5444 vnc_display_init(ds);
5445 if (vnc_display_open(ds, vnc_display) < 0)
5446 exit(1);
5447 }
bellard5b0753e2005-03-01 21:37:28 +00005448#if defined(CONFIG_SDL)
aliguorid268de02009-01-22 16:18:33 +00005449 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005450 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00005451#elif defined(CONFIG_COCOA)
aliguorid268de02009-01-22 16:18:33 +00005452 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005453 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00005454#endif
aliguori7d957bd2009-01-15 22:14:11 +00005455 }
bellard313aa562003-08-10 21:52:11 +00005456 }
aliguori7d957bd2009-01-15 22:14:11 +00005457 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005458
aliguori3023f332009-01-16 19:04:14 +00005459 dcl = ds->listeners;
5460 while (dcl != NULL) {
5461 if (dcl->dpy_refresh != NULL) {
5462 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5463 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005464 }
aliguori3023f332009-01-16 19:04:14 +00005465 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005466 }
aliguori3023f332009-01-16 19:04:14 +00005467
blueswir19043b622009-01-21 19:28:13 +00005468 if (nographic || (vnc_display && !sdl)) {
5469 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5470 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5471 }
5472
aliguori2796dae2009-01-16 20:23:27 +00005473 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00005474 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00005475
aliguori4c621802009-01-16 21:48:20 +00005476 if (monitor_device && monitor_hd)
aliguoricde76ee2009-03-05 23:01:51 +00005477 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
bellard82c643f2004-07-14 17:28:13 +00005478
bellard8d11df92004-08-24 21:13:40 +00005479 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005480 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005481 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005482 char label[32];
5483 snprintf(label, sizeof(label), "serial%d", i);
thsaf3a9032007-07-11 23:14:59 +00005484 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005485 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005486 }
bellard82c643f2004-07-14 17:28:13 +00005487 }
bellard82c643f2004-07-14 17:28:13 +00005488
bellard6508fe52005-01-15 12:02:56 +00005489 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005490 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005491 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005492 char label[32];
5493 snprintf(label, sizeof(label), "parallel%d", i);
thsaf3a9032007-07-11 23:14:59 +00005494 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005495 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005496 }
5497 }
5498
aliguori9ede2fd2009-01-15 20:05:25 +00005499 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5500 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005501 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005502 char label[32];
5503 snprintf(label, sizeof(label), "virtcon%d", i);
aliguori9ede2fd2009-01-15 20:05:25 +00005504 if (strstart(devname, "vc", 0))
5505 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5506 }
5507 }
5508
bellard67b915a2004-03-31 23:37:16 +00005509#ifdef CONFIG_GDBSTUB
aliguori59030a82009-04-05 18:43:41 +00005510 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
5511 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
5512 gdbstub_dev);
5513 exit(1);
balrog45669e02007-07-02 13:20:17 +00005514 }
bellard67b915a2004-03-31 23:37:16 +00005515#endif
balrog45669e02007-07-02 13:20:17 +00005516
bellardd63d3072004-10-03 13:29:03 +00005517 if (loadvm)
aliguori376253e2009-03-05 23:01:23 +00005518 do_loadvm(cur_mon, loadvm);
bellardd63d3072004-10-03 13:29:03 +00005519
aliguori5bb79102008-10-13 03:12:02 +00005520 if (incoming) {
5521 autostart = 0; /* fixme how to deal with -daemonize */
5522 qemu_start_incoming_migration(incoming);
5523 }
5524
aliguoric0f4ce72009-03-05 23:01:01 +00005525 if (autostart)
5526 vm_start();
thsffd843b2006-12-21 19:46:43 +00005527
blueswir1b9e82a52009-04-05 18:03:31 +00005528#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005529 if (daemonize) {
5530 uint8_t status = 0;
5531 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005532
5533 again1:
5534 len = write(fds[1], &status, 1);
5535 if (len == -1 && (errno == EINTR))
5536 goto again1;
5537
5538 if (len != 1)
5539 exit(1);
5540
aliguoribd54b862008-07-23 00:58:33 +00005541 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005542 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005543 if (fd == -1)
5544 exit(1);
aliguori08585322009-02-27 22:09:45 +00005545 }
ths71e3ceb2006-12-22 02:11:31 +00005546
aliguori08585322009-02-27 22:09:45 +00005547 if (run_as) {
5548 pwd = getpwnam(run_as);
5549 if (!pwd) {
5550 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5551 exit(1);
5552 }
5553 }
ths71e3ceb2006-12-22 02:11:31 +00005554
aliguori08585322009-02-27 22:09:45 +00005555 if (chroot_dir) {
5556 if (chroot(chroot_dir) < 0) {
5557 fprintf(stderr, "chroot failed\n");
5558 exit(1);
5559 }
5560 chdir("/");
5561 }
5562
5563 if (run_as) {
5564 if (setgid(pwd->pw_gid) < 0) {
5565 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5566 exit(1);
5567 }
5568 if (setuid(pwd->pw_uid) < 0) {
5569 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5570 exit(1);
5571 }
5572 if (setuid(0) != -1) {
5573 fprintf(stderr, "Dropping privileges failed\n");
5574 exit(1);
5575 }
5576 }
aliguori08585322009-02-27 22:09:45 +00005577
5578 if (daemonize) {
5579 dup2(fd, 0);
5580 dup2(fd, 1);
5581 dup2(fd, 2);
5582
5583 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00005584 }
blueswir1b9e82a52009-04-05 18:03:31 +00005585#endif
ths71e3ceb2006-12-22 02:11:31 +00005586
bellard8a7ddc32004-03-31 19:00:16 +00005587 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005588 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005589 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005590
bellard0824d6f2003-06-24 13:42:40 +00005591 return 0;
5592}