blob: 2d9f04e4c2aad26ca81b8f22fa1e0d7c1cd4827e [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
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
aliguori08585322009-02-27 22:09:45 +000037#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000038#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000041#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000042#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000043#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000045#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <net/if.h>
47#if defined(__NetBSD__)
48#include <net/if_tap.h>
49#endif
50#ifdef __linux__
51#include <linux/if_tun.h>
52#endif
53#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000054#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000055#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000056#include <sys/select.h>
blueswir1179a2c12009-03-08 08:23:32 +000057#ifdef HOST_BSD
bellard7d3505c2004-05-12 19:32:15 +000058#include <sys/stat.h>
blueswir1c5e97232009-03-07 20:06:23 +000059#if defined(__FreeBSD__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000060#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000061#else
62#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000063#endif
ths5c40d2b2007-06-23 16:03:36 +000064#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
65#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000066#else
blueswir1223f0d72008-09-30 18:12:18 +000067#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000068#include <pty.h>
69#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000070#include <linux/rtc.h>
thsbd494f42007-09-16 20:03:23 +000071
72/* For the benefit of older linux systems which don't supply it,
73 we use a local copy of hpet.h. */
74/* #include <linux/hpet.h> */
75#include "hpet.h"
76
bellarde57a8c02005-11-10 23:58:52 +000077#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000078#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000079#endif
80#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000081#include <sys/stat.h>
82#include <sys/ethernet.h>
83#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000084#include <netinet/arp.h>
85#include <netinet/in.h>
86#include <netinet/in_systm.h>
87#include <netinet/ip.h>
88#include <netinet/ip_icmp.h> // must come after ip.h
89#include <netinet/udp.h>
90#include <netinet/tcp.h>
91#include <net/if.h>
92#include <syslog.h>
93#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000094#endif
bellard7d3505c2004-05-12 19:32:15 +000095#endif
bellardec530c82006-04-25 22:36:06 +000096#endif
bellard67b915a2004-03-31 23:37:16 +000097
blueswir19892fbf2008-08-24 10:34:20 +000098#if defined(__OpenBSD__)
99#include <util.h>
100#endif
101
ths8a16d272008-07-19 09:56:24 +0000102#if defined(CONFIG_VDE)
103#include <libvdeplug.h>
104#endif
105
bellard67b915a2004-03-31 23:37:16 +0000106#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000107#include <windows.h>
bellard7d3505c2004-05-12 19:32:15 +0000108#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000109#include <sys/timeb.h>
ths4fddf622007-12-17 04:42:29 +0000110#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000111#define getopt_long_only getopt_long
112#define memalign(align, size) malloc(size)
113#endif
114
bellard73332e52004-04-04 20:22:28 +0000115#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +0000116#ifdef __APPLE__
Stefan Weil66936652009-06-13 13:19:11 +0200117#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000118int qemu_main(int argc, char **argv, char **envp);
119int main(int argc, char **argv)
120{
121 qemu_main(argc, argv, NULL);
122}
123#undef main
124#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000125#endif
bellard73332e52004-04-04 20:22:28 +0000126#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000127
bellard5b0753e2005-03-01 21:37:28 +0000128#ifdef CONFIG_COCOA
129#undef main
130#define main qemu_main
131#endif /* CONFIG_COCOA */
132
blueswir1511d2b12009-03-07 15:32:56 +0000133#include "hw/hw.h"
134#include "hw/boards.h"
135#include "hw/usb.h"
136#include "hw/pcmcia.h"
137#include "hw/pc.h"
138#include "hw/audiodev.h"
139#include "hw/isa.h"
140#include "hw/baum.h"
141#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100142#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000143#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000144#include "hw/xen.h"
aurel325ef4efa2009-03-10 21:43:35 +0000145#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000146#include "net.h"
147#include "monitor.h"
148#include "console.h"
149#include "sysemu.h"
150#include "gdbstub.h"
151#include "qemu-timer.h"
152#include "qemu-char.h"
153#include "cache-utils.h"
154#include "block.h"
blueswir1a718ace2009-03-28 08:24:44 +0000155#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000156#include "audio/audio.h"
157#include "migration.h"
158#include "kvm.h"
159#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200160#include "qemu-option.h"
blueswir1511d2b12009-03-07 15:32:56 +0000161
bellard0824d6f2003-06-24 13:42:40 +0000162#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000163
bellard8a7ddc32004-03-31 19:00:16 +0000164#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000165
blueswir1511d2b12009-03-07 15:32:56 +0000166#include "qemu_socket.h"
167
168#if defined(CONFIG_SLIRP)
169#include "libslirp.h"
170#endif
171
bellard0824d6f2003-06-24 13:42:40 +0000172//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000173//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000174//#define DEBUG_NET
175//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000176
aliguorid12d51d2009-01-15 21:48:06 +0000177
178#ifdef DEBUG_IOPORT
aliguori93fcfe32009-01-15 22:34:14 +0000179# define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
aliguorid12d51d2009-01-15 21:48:06 +0000180#else
181# define LOG_IOPORT(...) do { } while (0)
182#endif
183
bellard1bfe8562004-07-08 21:17:50 +0000184#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000185
pbrook0d92ed32006-05-21 16:30:15 +0000186/* Max number of USB devices that can be specified on the commandline. */
187#define MAX_USB_CMDLINE 8
188
balrogdc72ac12008-11-09 00:04:26 +0000189/* Max number of bluetooth switches on the commandline. */
190#define MAX_BT_CMDLINE 10
191
bellard7dea1da2003-11-16 15:59:30 +0000192/* XXX: use a two level table to limit memory usage */
193#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000194
Paul Brook5cea8592009-05-30 00:52:44 +0100195static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000196const char *bios_name = NULL;
blueswir1dbed7e42008-10-01 19:38:09 +0000197static void *ioport_opaque[MAX_IOPORTS];
198static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
199static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
thse4bcb142007-12-02 04:51:10 +0000200/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000201 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000202DriveInfo drives_table[MAX_DRIVES+1];
203int nb_drives;
malccb5a7aa2008-09-28 00:42:12 +0000204enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f3322009-01-16 19:04:14 +0000205static DisplayState *display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500206DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000207const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000208int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000209ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000210int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000211NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000212int vm_running;
aliguoric0f4ce72009-03-05 23:01:01 +0000213static int autostart;
balrogf6503052008-02-17 11:42:19 +0000214static int rtc_utc = 1;
215static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000216int cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +0000217int std_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000218int vmsvga_enabled = 0;
aliguori94909d92009-04-22 15:19:53 +0000219int xenfb_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000220#ifdef TARGET_SPARC
221int graphic_width = 1024;
222int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000223int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000224#else
bellard1bfe8562004-07-08 21:17:50 +0000225int graphic_width = 800;
226int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000227int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000228#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000229static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000230#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000231static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000232#endif
ths667acca2006-12-11 02:08:05 +0000233int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000234CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000235CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000236CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000237#ifdef TARGET_I386
238int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000239int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000240#endif
bellardbb36d472005-11-05 14:22:28 +0000241int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000242int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000243int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000244const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000245int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000246int no_hpet = 0;
Eduardo Habkostdf97b922009-06-10 16:34:08 -0300247int no_virtio_balloon = 0;
bellard52ca8d62006-06-14 16:03:05 +0000248int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000249int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000250int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000251int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000252int graphic_rotate = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000253#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000254int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000255#endif
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100256WatchdogTimerModel *watchdog = NULL;
257int watchdog_action = WDT_RESET;
ths9ae02552007-01-05 17:39:04 +0000258const char *option_rom[MAX_OPTION_ROMS];
259int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000260int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000261#ifdef TARGET_ARM
262int old_param = 0;
263#endif
thsc35734b2007-03-19 15:17:08 +0000264const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000265int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000266#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000267unsigned int nb_prom_envs = 0;
268const char *prom_envs[MAX_PROM_ENVS];
269#endif
aliguoriec691c82009-02-11 15:20:37 +0000270int nb_drives_opt;
271struct drive_opt drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000272
aliguori268a3622009-04-21 22:30:27 +0000273int nb_numa_nodes;
274uint64_t node_mem[MAX_NODES];
275uint64_t node_cpumask[MAX_NODES];
276
balrogee5605e2007-12-03 03:01:40 +0000277static CPUState *cur_cpu;
278static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000279static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000280/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000281static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000282/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000283#define MAX_ICOUNT_SHIFT 10
284/* Compensate for varying guest execution speed. */
285static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000286static QEMUTimer *icount_rt_timer;
287static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000288static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000289
blueswir18fcb1b92008-09-18 18:29:08 +0000290uint8_t qemu_uuid[16];
291
bellard0824d6f2003-06-24 13:42:40 +0000292/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000293/* x86 ISA bus support */
294
295target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000296PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000297
aliguori477e3ed2008-07-23 15:19:59 +0000298static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
299static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
300
301static uint32_t ioport_read(int index, uint32_t address)
302{
303 static IOPortReadFunc *default_func[3] = {
304 default_ioport_readb,
305 default_ioport_readw,
306 default_ioport_readl
307 };
308 IOPortReadFunc *func = ioport_read_table[index][address];
309 if (!func)
310 func = default_func[index];
311 return func(ioport_opaque[address], address);
312}
313
314static void ioport_write(int index, uint32_t address, uint32_t data)
315{
316 static IOPortWriteFunc *default_func[3] = {
317 default_ioport_writeb,
318 default_ioport_writew,
319 default_ioport_writel
320 };
321 IOPortWriteFunc *func = ioport_write_table[index][address];
322 if (!func)
323 func = default_func[index];
324 func(ioport_opaque[address], address, data);
325}
326
pbrook9596ebb2007-11-18 01:44:38 +0000327static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000328{
329#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000330 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000331#endif
bellardfc01f7e2003-06-30 10:03:06 +0000332 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000333}
334
pbrook9596ebb2007-11-18 01:44:38 +0000335static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000336{
337#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000338 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000339#endif
340}
341
342/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000343static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000344{
345 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000346 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000347 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000348 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000349 return data;
350}
351
pbrook9596ebb2007-11-18 01:44:38 +0000352static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000353{
aliguori477e3ed2008-07-23 15:19:59 +0000354 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000355 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000356 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000357}
358
pbrook9596ebb2007-11-18 01:44:38 +0000359static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000360{
361#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000362 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000363#endif
364 return 0xffffffff;
365}
366
pbrook9596ebb2007-11-18 01:44:38 +0000367static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000368{
369#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000370 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000371#endif
bellard0824d6f2003-06-24 13:42:40 +0000372}
373
bellardfc01f7e2003-06-30 10:03:06 +0000374/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000375int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000376 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000377{
bellardfc01f7e2003-06-30 10:03:06 +0000378 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000379
bellardc4b1fcc2004-03-14 21:44:30 +0000380 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000381 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000382 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000383 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000384 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000385 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000386 } else {
balrog88fdf562008-04-26 21:11:22 +0000387 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000388 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000389 }
390 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000391 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000392 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000393 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000394 ioport_opaque[i] = opaque;
395 }
bellard0824d6f2003-06-24 13:42:40 +0000396 return 0;
397}
398
bellardfc01f7e2003-06-30 10:03:06 +0000399/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000400int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000401 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000402{
bellardfc01f7e2003-06-30 10:03:06 +0000403 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000404
bellardc4b1fcc2004-03-14 21:44:30 +0000405 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000406 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000407 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000408 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000409 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000410 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000411 } else {
balrog88fdf562008-04-26 21:11:22 +0000412 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000413 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000414 }
415 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000416 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000417 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
418 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000419 ioport_opaque[i] = opaque;
420 }
bellardf1510b22003-06-25 00:07:40 +0000421 return 0;
422}
423
bellard69b91032004-05-18 23:05:28 +0000424void isa_unassign_ioport(int start, int length)
425{
426 int i;
427
428 for(i = start; i < start + length; i++) {
429 ioport_read_table[0][i] = default_ioport_readb;
430 ioport_read_table[1][i] = default_ioport_readw;
431 ioport_read_table[2][i] = default_ioport_readl;
432
433 ioport_write_table[0][i] = default_ioport_writeb;
434 ioport_write_table[1][i] = default_ioport_writew;
435 ioport_write_table[2][i] = default_ioport_writel;
aliguoria7607f72009-02-11 15:21:29 +0000436
437 ioport_opaque[i] = NULL;
bellard69b91032004-05-18 23:05:28 +0000438 }
439}
440
bellard20f32282005-01-03 23:36:21 +0000441/***********************************************************/
442
bellardc45886d2004-01-05 00:02:06 +0000443void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000444{
aliguorid12d51d2009-01-15 21:48:06 +0000445 LOG_IOPORT("outb: %04x %02x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000446 ioport_write(0, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000447#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000448 if (env)
449 env->last_io_time = cpu_get_time_fast();
450#endif
bellard0824d6f2003-06-24 13:42:40 +0000451}
452
bellardc45886d2004-01-05 00:02:06 +0000453void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000454{
aliguorid12d51d2009-01-15 21:48:06 +0000455 LOG_IOPORT("outw: %04x %04x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000456 ioport_write(1, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000457#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000458 if (env)
459 env->last_io_time = cpu_get_time_fast();
460#endif
bellard0824d6f2003-06-24 13:42:40 +0000461}
462
bellardc45886d2004-01-05 00:02:06 +0000463void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000464{
aliguorid12d51d2009-01-15 21:48:06 +0000465 LOG_IOPORT("outl: %04x %08x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000466 ioport_write(2, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000467#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000468 if (env)
469 env->last_io_time = cpu_get_time_fast();
470#endif
bellard0824d6f2003-06-24 13:42:40 +0000471}
472
bellardc45886d2004-01-05 00:02:06 +0000473int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000474{
bellardfd872592004-05-12 19:11:15 +0000475 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000476 val = ioport_read(0, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000477 LOG_IOPORT("inb : %04x %02x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000478#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000479 if (env)
480 env->last_io_time = cpu_get_time_fast();
481#endif
bellardfd872592004-05-12 19:11:15 +0000482 return val;
bellard0824d6f2003-06-24 13:42:40 +0000483}
484
bellardc45886d2004-01-05 00:02:06 +0000485int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000486{
bellardfd872592004-05-12 19:11:15 +0000487 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000488 val = ioport_read(1, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000489 LOG_IOPORT("inw : %04x %04x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000490#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000491 if (env)
492 env->last_io_time = cpu_get_time_fast();
493#endif
bellardfd872592004-05-12 19:11:15 +0000494 return val;
bellard0824d6f2003-06-24 13:42:40 +0000495}
496
bellardc45886d2004-01-05 00:02:06 +0000497int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000498{
bellardfd872592004-05-12 19:11:15 +0000499 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000500 val = ioport_read(2, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000501 LOG_IOPORT("inl : %04x %08x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000502#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000503 if (env)
504 env->last_io_time = cpu_get_time_fast();
505#endif
bellardfd872592004-05-12 19:11:15 +0000506 return val;
bellard0824d6f2003-06-24 13:42:40 +0000507}
508
509/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000510void hw_error(const char *fmt, ...)
511{
512 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000513 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000514
515 va_start(ap, fmt);
516 fprintf(stderr, "qemu: hardware error: ");
517 vfprintf(stderr, fmt, ap);
518 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000519 for(env = first_cpu; env != NULL; env = env->next_cpu) {
520 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000521#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000522 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000523#else
bellard6a00d602005-11-21 23:25:50 +0000524 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000525#endif
bellard6a00d602005-11-21 23:25:50 +0000526 }
bellard0824d6f2003-06-24 13:42:40 +0000527 va_end(ap);
528 abort();
529}
aliguoridf751fa2008-12-04 20:19:35 +0000530
531/***************/
532/* ballooning */
533
534static QEMUBalloonEvent *qemu_balloon_event;
535void *qemu_balloon_event_opaque;
536
537void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
538{
539 qemu_balloon_event = func;
540 qemu_balloon_event_opaque = opaque;
541}
542
543void qemu_balloon(ram_addr_t target)
544{
545 if (qemu_balloon_event)
546 qemu_balloon_event(qemu_balloon_event_opaque, target);
547}
548
549ram_addr_t qemu_balloon_status(void)
550{
551 if (qemu_balloon_event)
552 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
553 return 0;
554}
bellard0824d6f2003-06-24 13:42:40 +0000555
bellard8a7ddc32004-03-31 19:00:16 +0000556/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000557/* keyboard/mouse */
558
559static QEMUPutKBDEvent *qemu_put_kbd_event;
560static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000561static QEMUPutMouseEntry *qemu_put_mouse_event_head;
562static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000563
564void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
565{
566 qemu_put_kbd_event_opaque = opaque;
567 qemu_put_kbd_event = func;
568}
569
ths455204e2007-01-05 16:42:13 +0000570QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
571 void *opaque, int absolute,
572 const char *name)
bellard63066f42004-06-03 18:45:02 +0000573{
ths455204e2007-01-05 16:42:13 +0000574 QEMUPutMouseEntry *s, *cursor;
575
576 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000577
578 s->qemu_put_mouse_event = func;
579 s->qemu_put_mouse_event_opaque = opaque;
580 s->qemu_put_mouse_event_absolute = absolute;
581 s->qemu_put_mouse_event_name = qemu_strdup(name);
582 s->next = NULL;
583
584 if (!qemu_put_mouse_event_head) {
585 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
586 return s;
587 }
588
589 cursor = qemu_put_mouse_event_head;
590 while (cursor->next != NULL)
591 cursor = cursor->next;
592
593 cursor->next = s;
594 qemu_put_mouse_event_current = s;
595
596 return s;
597}
598
599void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
600{
601 QEMUPutMouseEntry *prev = NULL, *cursor;
602
603 if (!qemu_put_mouse_event_head || entry == NULL)
604 return;
605
606 cursor = qemu_put_mouse_event_head;
607 while (cursor != NULL && cursor != entry) {
608 prev = cursor;
609 cursor = cursor->next;
610 }
611
612 if (cursor == NULL) // does not exist or list empty
613 return;
614 else if (prev == NULL) { // entry is head
615 qemu_put_mouse_event_head = cursor->next;
616 if (qemu_put_mouse_event_current == entry)
617 qemu_put_mouse_event_current = cursor->next;
618 qemu_free(entry->qemu_put_mouse_event_name);
619 qemu_free(entry);
620 return;
621 }
622
623 prev->next = entry->next;
624
625 if (qemu_put_mouse_event_current == entry)
626 qemu_put_mouse_event_current = prev;
627
628 qemu_free(entry->qemu_put_mouse_event_name);
629 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000630}
631
632void kbd_put_keycode(int keycode)
633{
634 if (qemu_put_kbd_event) {
635 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
636 }
637}
638
639void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
640{
ths455204e2007-01-05 16:42:13 +0000641 QEMUPutMouseEvent *mouse_event;
642 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000643 int width;
ths455204e2007-01-05 16:42:13 +0000644
645 if (!qemu_put_mouse_event_current) {
646 return;
647 }
648
649 mouse_event =
650 qemu_put_mouse_event_current->qemu_put_mouse_event;
651 mouse_event_opaque =
652 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
653
654 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000655 if (graphic_rotate) {
656 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
657 width = 0x7fff;
658 else
aurel32b94ed572008-03-10 19:34:27 +0000659 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000660 mouse_event(mouse_event_opaque,
661 width - dy, dx, dz, buttons_state);
662 } else
663 mouse_event(mouse_event_opaque,
664 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000665 }
666}
667
bellard09b26c52006-04-12 21:09:08 +0000668int kbd_mouse_is_absolute(void)
669{
ths455204e2007-01-05 16:42:13 +0000670 if (!qemu_put_mouse_event_current)
671 return 0;
672
673 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
674}
675
aliguori376253e2009-03-05 23:01:23 +0000676void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000677{
678 QEMUPutMouseEntry *cursor;
679 int index = 0;
680
681 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000682 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000683 return;
684 }
685
aliguori376253e2009-03-05 23:01:23 +0000686 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000687 cursor = qemu_put_mouse_event_head;
688 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000689 monitor_printf(mon, "%c Mouse #%d: %s\n",
690 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
691 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000692 index++;
693 cursor = cursor->next;
694 }
695}
696
aliguori376253e2009-03-05 23:01:23 +0000697void do_mouse_set(Monitor *mon, int index)
ths455204e2007-01-05 16:42:13 +0000698{
699 QEMUPutMouseEntry *cursor;
700 int i = 0;
701
702 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000703 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000704 return;
705 }
706
707 cursor = qemu_put_mouse_event_head;
708 while (cursor != NULL && index != i) {
709 i++;
710 cursor = cursor->next;
711 }
712
713 if (cursor != NULL)
714 qemu_put_mouse_event_current = cursor;
715 else
aliguori376253e2009-03-05 23:01:23 +0000716 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000717}
718
bellard87858c82003-06-27 12:01:39 +0000719/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000720uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000721{
722 union {
723 uint64_t ll;
724 struct {
725#ifdef WORDS_BIGENDIAN
726 uint32_t high, low;
727#else
728 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000729#endif
bellard87858c82003-06-27 12:01:39 +0000730 } l;
731 } u, res;
732 uint64_t rl, rh;
733
734 u.ll = a;
735 rl = (uint64_t)u.l.low * (uint64_t)b;
736 rh = (uint64_t)u.l.high * (uint64_t)b;
737 rh += (rl >> 32);
738 res.l.high = rh / c;
739 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
740 return res.ll;
741}
742
bellard1dce7c32006-07-13 23:20:22 +0000743/***********************************************************/
744/* real time host monotonic timer */
745
746#define QEMU_TIMER_BASE 1000000000LL
747
748#ifdef WIN32
749
750static int64_t clock_freq;
751
752static void init_get_clock(void)
753{
bellarda8e5ac32006-07-14 09:36:13 +0000754 LARGE_INTEGER freq;
755 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000756 ret = QueryPerformanceFrequency(&freq);
757 if (ret == 0) {
758 fprintf(stderr, "Could not calibrate ticks\n");
759 exit(1);
760 }
761 clock_freq = freq.QuadPart;
762}
763
764static int64_t get_clock(void)
765{
766 LARGE_INTEGER ti;
767 QueryPerformanceCounter(&ti);
768 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
769}
770
771#else
772
773static int use_rt_clock;
774
775static void init_get_clock(void)
776{
777 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000778#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
779 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000780 {
781 struct timespec ts;
782 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
783 use_rt_clock = 1;
784 }
785 }
786#endif
787}
788
789static int64_t get_clock(void)
790{
blueswir1c5e97232009-03-07 20:06:23 +0000791#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
792 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000793 if (use_rt_clock) {
794 struct timespec ts;
795 clock_gettime(CLOCK_MONOTONIC, &ts);
796 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000797 } else
bellard1dce7c32006-07-13 23:20:22 +0000798#endif
799 {
800 /* XXX: using gettimeofday leads to problems if the date
801 changes, so it should be avoided. */
802 struct timeval tv;
803 gettimeofday(&tv, NULL);
804 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
805 }
806}
bellard1dce7c32006-07-13 23:20:22 +0000807#endif
808
pbrook2e70f6e2008-06-29 01:03:05 +0000809/* Return the virtual CPU time, based on the instruction counter. */
810static int64_t cpu_get_icount(void)
811{
812 int64_t icount;
813 CPUState *env = cpu_single_env;;
814 icount = qemu_icount;
815 if (env) {
816 if (!can_do_io(env))
817 fprintf(stderr, "Bad clock read\n");
818 icount -= (env->icount_decr.u16.low + env->icount_extra);
819 }
820 return qemu_icount_bias + (icount << icount_time_shift);
821}
822
bellard1dce7c32006-07-13 23:20:22 +0000823/***********************************************************/
824/* guest cycle counter */
825
826static int64_t cpu_ticks_prev;
827static int64_t cpu_ticks_offset;
828static int64_t cpu_clock_offset;
829static int cpu_ticks_enabled;
830
831/* return the host CPU cycle counter and handle stop/restart */
832int64_t cpu_get_ticks(void)
833{
pbrook2e70f6e2008-06-29 01:03:05 +0000834 if (use_icount) {
835 return cpu_get_icount();
836 }
bellard1dce7c32006-07-13 23:20:22 +0000837 if (!cpu_ticks_enabled) {
838 return cpu_ticks_offset;
839 } else {
840 int64_t ticks;
841 ticks = cpu_get_real_ticks();
842 if (cpu_ticks_prev > ticks) {
843 /* Note: non increasing ticks may happen if the host uses
844 software suspend */
845 cpu_ticks_offset += cpu_ticks_prev - ticks;
846 }
847 cpu_ticks_prev = ticks;
848 return ticks + cpu_ticks_offset;
849 }
850}
851
852/* return the host CPU monotonic timer and handle stop/restart */
853static int64_t cpu_get_clock(void)
854{
855 int64_t ti;
856 if (!cpu_ticks_enabled) {
857 return cpu_clock_offset;
858 } else {
859 ti = get_clock();
860 return ti + cpu_clock_offset;
861 }
862}
863
864/* enable cpu_get_ticks() */
865void cpu_enable_ticks(void)
866{
867 if (!cpu_ticks_enabled) {
868 cpu_ticks_offset -= cpu_get_real_ticks();
869 cpu_clock_offset -= get_clock();
870 cpu_ticks_enabled = 1;
871 }
872}
873
874/* disable cpu_get_ticks() : the clock is stopped. You must not call
875 cpu_get_ticks() after that. */
876void cpu_disable_ticks(void)
877{
878 if (cpu_ticks_enabled) {
879 cpu_ticks_offset = cpu_get_ticks();
880 cpu_clock_offset = cpu_get_clock();
881 cpu_ticks_enabled = 0;
882 }
883}
884
885/***********************************************************/
886/* timers */
ths5fafdf22007-09-16 21:08:06 +0000887
bellard8a7ddc32004-03-31 19:00:16 +0000888#define QEMU_TIMER_REALTIME 0
889#define QEMU_TIMER_VIRTUAL 1
890
891struct QEMUClock {
892 int type;
893 /* XXX: add frequency */
894};
895
896struct QEMUTimer {
897 QEMUClock *clock;
898 int64_t expire_time;
899 QEMUTimerCB *cb;
900 void *opaque;
901 struct QEMUTimer *next;
902};
903
thsc8994012007-08-19 21:56:03 +0000904struct qemu_alarm_timer {
905 char const *name;
thsefe75412007-08-24 01:36:32 +0000906 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000907
908 int (*start)(struct qemu_alarm_timer *t);
909 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000910 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000911 void *priv;
912};
913
thsefe75412007-08-24 01:36:32 +0000914#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000915#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000916
917static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
918{
Jean-Christophe Duboise3323402009-05-17 18:38:39 +0200919 return t && (t->flags & ALARM_FLAG_DYNTICKS);
thsefe75412007-08-24 01:36:32 +0000920}
921
922static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
923{
924 if (!alarm_has_dynticks(t))
925 return;
926
927 t->rearm(t);
928}
929
930/* TODO: MIN_TIMER_REARM_US should be optimized */
931#define MIN_TIMER_REARM_US 250
932
thsc8994012007-08-19 21:56:03 +0000933static struct qemu_alarm_timer *alarm_timer;
934
935#ifdef _WIN32
936
937struct qemu_alarm_win32 {
938 MMRESULT timerId;
thsc8994012007-08-19 21:56:03 +0000939 unsigned int period;
Blue Swirlef28c4b2009-04-25 12:56:37 +0000940} alarm_win32_data = {0, -1};
thsc8994012007-08-19 21:56:03 +0000941
942static int win32_start_timer(struct qemu_alarm_timer *t);
943static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000944static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000945
946#else
947
948static int unix_start_timer(struct qemu_alarm_timer *t);
949static void unix_stop_timer(struct qemu_alarm_timer *t);
950
ths231c6582007-08-26 17:29:15 +0000951#ifdef __linux__
952
thsefe75412007-08-24 01:36:32 +0000953static int dynticks_start_timer(struct qemu_alarm_timer *t);
954static void dynticks_stop_timer(struct qemu_alarm_timer *t);
955static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
956
thsc40ec5a2007-08-19 22:09:40 +0000957static int hpet_start_timer(struct qemu_alarm_timer *t);
958static void hpet_stop_timer(struct qemu_alarm_timer *t);
959
thsc8994012007-08-19 21:56:03 +0000960static int rtc_start_timer(struct qemu_alarm_timer *t);
961static void rtc_stop_timer(struct qemu_alarm_timer *t);
962
thsefe75412007-08-24 01:36:32 +0000963#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000964
965#endif /* _WIN32 */
966
pbrook2e70f6e2008-06-29 01:03:05 +0000967/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000968 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000969 When the guest is idle real and virtual time will be aligned in
970 the IO wait loop. */
971#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
972
973static void icount_adjust(void)
974{
975 int64_t cur_time;
976 int64_t cur_icount;
977 int64_t delta;
978 static int64_t last_delta;
979 /* If the VM is not running, then do nothing. */
980 if (!vm_running)
981 return;
982
983 cur_time = cpu_get_clock();
984 cur_icount = qemu_get_clock(vm_clock);
985 delta = cur_icount - cur_time;
986 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
987 if (delta > 0
988 && last_delta + ICOUNT_WOBBLE < delta * 2
989 && icount_time_shift > 0) {
990 /* The guest is getting too far ahead. Slow time down. */
991 icount_time_shift--;
992 }
993 if (delta < 0
994 && last_delta - ICOUNT_WOBBLE > delta * 2
995 && icount_time_shift < MAX_ICOUNT_SHIFT) {
996 /* The guest is getting too far behind. Speed time up. */
997 icount_time_shift++;
998 }
999 last_delta = delta;
1000 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
1001}
1002
1003static void icount_adjust_rt(void * opaque)
1004{
1005 qemu_mod_timer(icount_rt_timer,
1006 qemu_get_clock(rt_clock) + 1000);
1007 icount_adjust();
1008}
1009
1010static void icount_adjust_vm(void * opaque)
1011{
1012 qemu_mod_timer(icount_vm_timer,
1013 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1014 icount_adjust();
1015}
1016
1017static void init_icount_adjust(void)
1018{
1019 /* Have both realtime and virtual time triggers for speed adjustment.
1020 The realtime trigger catches emulated time passing too slowly,
1021 the virtual time trigger catches emulated time passing too fast.
1022 Realtime triggers occur even when idle, so use them less frequently
1023 than VM triggers. */
1024 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1025 qemu_mod_timer(icount_rt_timer,
1026 qemu_get_clock(rt_clock) + 1000);
1027 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1028 qemu_mod_timer(icount_vm_timer,
1029 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1030}
1031
thsc8994012007-08-19 21:56:03 +00001032static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +00001033#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +00001034#ifdef __linux__
thsefe75412007-08-24 01:36:32 +00001035 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1036 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001037 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +00001038 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001039 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +00001040 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001041#endif
thsefe75412007-08-24 01:36:32 +00001042 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001043#else
thsefe75412007-08-24 01:36:32 +00001044 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1045 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1046 {"win32", 0, win32_start_timer,
1047 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +00001048#endif
1049 {NULL, }
1050};
1051
blueswir13f47aa82008-03-09 06:59:01 +00001052static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +00001053{
1054 int i;
1055
1056 printf("Available alarm timers, in order of precedence:\n");
1057 for (i = 0; alarm_timers[i].name; i++)
1058 printf("%s\n", alarm_timers[i].name);
1059}
1060
1061static void configure_alarms(char const *opt)
1062{
1063 int i;
1064 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +00001065 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +00001066 char *arg;
1067 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +00001068 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +00001069
aurel323adda042008-03-09 23:43:49 +00001070 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +00001071 show_available_alarms();
1072 exit(0);
1073 }
1074
1075 arg = strdup(opt);
1076
1077 /* Reorder the array */
1078 name = strtok(arg, ",");
1079 while (name) {
balroge2b577e2007-09-17 21:25:20 +00001080 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +00001081 if (!strcmp(alarm_timers[i].name, name))
1082 break;
1083 }
1084
1085 if (i == count) {
1086 fprintf(stderr, "Unknown clock %s\n", name);
1087 goto next;
1088 }
1089
1090 if (i < cur)
1091 /* Ignore */
1092 goto next;
1093
1094 /* Swap */
1095 tmp = alarm_timers[i];
1096 alarm_timers[i] = alarm_timers[cur];
1097 alarm_timers[cur] = tmp;
1098
1099 cur++;
1100next:
1101 name = strtok(NULL, ",");
1102 }
1103
1104 free(arg);
1105
1106 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +00001107 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +00001108 for (i = cur; i < count; i++)
1109 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +00001110 } else {
1111 show_available_alarms();
1112 exit(1);
thsf3dcfad2007-08-24 01:26:02 +00001113 }
thsf3dcfad2007-08-24 01:26:02 +00001114}
1115
bellard8a7ddc32004-03-31 19:00:16 +00001116QEMUClock *rt_clock;
1117QEMUClock *vm_clock;
1118
1119static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +00001120
pbrook9596ebb2007-11-18 01:44:38 +00001121static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001122{
1123 QEMUClock *clock;
1124 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +00001125 clock->type = type;
1126 return clock;
1127}
1128
1129QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1130{
1131 QEMUTimer *ts;
1132
1133 ts = qemu_mallocz(sizeof(QEMUTimer));
1134 ts->clock = clock;
1135 ts->cb = cb;
1136 ts->opaque = opaque;
1137 return ts;
1138}
1139
1140void qemu_free_timer(QEMUTimer *ts)
1141{
1142 qemu_free(ts);
1143}
1144
1145/* stop a timer, but do not dealloc it */
1146void qemu_del_timer(QEMUTimer *ts)
1147{
1148 QEMUTimer **pt, *t;
1149
1150 /* NOTE: this code must be signal safe because
1151 qemu_timer_expired() can be called from a signal. */
1152 pt = &active_timers[ts->clock->type];
1153 for(;;) {
1154 t = *pt;
1155 if (!t)
1156 break;
1157 if (t == ts) {
1158 *pt = t->next;
1159 break;
1160 }
1161 pt = &t->next;
1162 }
1163}
1164
1165/* modify the current timer so that it will be fired when current_time
1166 >= expire_time. The corresponding callback will be called. */
1167void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1168{
1169 QEMUTimer **pt, *t;
1170
1171 qemu_del_timer(ts);
1172
1173 /* add the timer in the sorted list */
1174 /* NOTE: this code must be signal safe because
1175 qemu_timer_expired() can be called from a signal. */
1176 pt = &active_timers[ts->clock->type];
1177 for(;;) {
1178 t = *pt;
1179 if (!t)
1180 break;
ths5fafdf22007-09-16 21:08:06 +00001181 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001182 break;
1183 pt = &t->next;
1184 }
1185 ts->expire_time = expire_time;
1186 ts->next = *pt;
1187 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001188
1189 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001190 if (pt == &active_timers[ts->clock->type]) {
1191 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1192 qemu_rearm_alarm_timer(alarm_timer);
1193 }
1194 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +00001195 if (use_icount)
1196 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +00001197 }
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
aliguori50317c72009-04-24 18:03:29 +00001307static void qemu_event_increment(void);
1308
bellard67b915a2004-03-31 23:37:16 +00001309#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001310static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1311 DWORD_PTR dwUser, DWORD_PTR dw1,
1312 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001313#else
bellard8a7ddc32004-03-31 19:00:16 +00001314static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001315#endif
bellard8a7ddc32004-03-31 19:00:16 +00001316{
bellard02ba45c2004-06-25 14:46:23 +00001317#if 0
1318#define DISP_FREQ 1000
1319 {
1320 static int64_t delta_min = INT64_MAX;
1321 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1322 static int count;
1323 ti = qemu_get_clock(vm_clock);
1324 if (last_clock != 0) {
1325 delta = ti - last_clock;
1326 if (delta < delta_min)
1327 delta_min = delta;
1328 if (delta > delta_max)
1329 delta_max = delta;
1330 delta_cum += delta;
1331 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001332 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001333 muldiv64(delta_min, 1000000, ticks_per_sec),
1334 muldiv64(delta_max, 1000000, ticks_per_sec),
1335 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1336 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1337 count = 0;
1338 delta_min = INT64_MAX;
1339 delta_max = 0;
1340 delta_cum = 0;
1341 }
1342 }
1343 last_clock = ti;
1344 }
1345#endif
thsefe75412007-08-24 01:36:32 +00001346 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001347 (!use_icount &&
1348 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1349 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001350 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1351 qemu_get_clock(rt_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001352 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001353 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001354
aliguorid6dc3d42009-04-24 18:04:07 +00001355#ifndef CONFIG_IOTHREAD
1356 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001357 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001358 cpu_exit(next_cpu);
blueswir1640f42e2009-04-19 10:18:01 +00001359#ifdef CONFIG_KQEMU
aliguorid6dc3d42009-04-24 18:04:07 +00001360 if (next_cpu->kqemu_enabled) {
1361 kqemu_cpu_interrupt(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001362 }
balrogee5605e2007-12-03 03:01:40 +00001363#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001364 }
aliguorid6dc3d42009-04-24 18:04:07 +00001365#endif
aliguori43b96852009-04-24 18:03:33 +00001366 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001367 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001368 }
1369}
1370
pbrook2e70f6e2008-06-29 01:03:05 +00001371static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001372{
pbrook2e70f6e2008-06-29 01:03:05 +00001373 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001374
1375 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001376 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1377 qemu_get_clock(vm_clock);
1378 } else {
1379 /* To avoid problems with overflow limit this to 2^32. */
1380 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001381 }
1382
pbrook2e70f6e2008-06-29 01:03:05 +00001383 if (delta < 0)
1384 delta = 0;
thsefe75412007-08-24 01:36:32 +00001385
pbrook2e70f6e2008-06-29 01:03:05 +00001386 return delta;
1387}
1388
blueswir18632fb92008-09-14 13:59:34 +00001389#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001390static uint64_t qemu_next_deadline_dyntick(void)
1391{
1392 int64_t delta;
1393 int64_t rtdelta;
1394
1395 if (use_icount)
1396 delta = INT32_MAX;
1397 else
1398 delta = (qemu_next_deadline() + 999) / 1000;
1399
1400 if (active_timers[QEMU_TIMER_REALTIME]) {
1401 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1402 qemu_get_clock(rt_clock))*1000;
1403 if (rtdelta < delta)
1404 delta = rtdelta;
1405 }
1406
1407 if (delta < MIN_TIMER_REARM_US)
1408 delta = MIN_TIMER_REARM_US;
1409
1410 return delta;
thsefe75412007-08-24 01:36:32 +00001411}
blueswir18632fb92008-09-14 13:59:34 +00001412#endif
thsefe75412007-08-24 01:36:32 +00001413
bellardfd872592004-05-12 19:11:15 +00001414#ifndef _WIN32
1415
aliguori7183b4b2008-11-05 20:40:18 +00001416/* Sets a specific flag */
1417static int fcntl_setfl(int fd, int flag)
1418{
1419 int flags;
1420
1421 flags = fcntl(fd, F_GETFL);
1422 if (flags == -1)
1423 return -errno;
1424
1425 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1426 return -errno;
1427
1428 return 0;
1429}
1430
bellard829309c2004-05-20 13:20:12 +00001431#if defined(__linux__)
1432
bellardfd872592004-05-12 19:11:15 +00001433#define RTC_FREQ 1024
1434
aurel32de9a95f2008-11-11 13:41:01 +00001435static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001436{
thsc8994012007-08-19 21:56:03 +00001437 struct sigaction act;
1438
1439 /* timer signal */
1440 sigfillset(&act.sa_mask);
1441 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001442 act.sa_handler = host_alarm_handler;
1443
1444 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001445 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001446 fcntl(fd, F_SETOWN, getpid());
1447}
1448
thsc40ec5a2007-08-19 22:09:40 +00001449static int hpet_start_timer(struct qemu_alarm_timer *t)
1450{
1451 struct hpet_info info;
1452 int r, fd;
1453
1454 fd = open("/dev/hpet", O_RDONLY);
1455 if (fd < 0)
1456 return -1;
1457
1458 /* Set frequency */
1459 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1460 if (r < 0) {
1461 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1462 "error, but for better emulation accuracy type:\n"
1463 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1464 goto fail;
1465 }
1466
1467 /* Check capabilities */
1468 r = ioctl(fd, HPET_INFO, &info);
1469 if (r < 0)
1470 goto fail;
1471
1472 /* Enable periodic mode */
1473 r = ioctl(fd, HPET_EPI, 0);
1474 if (info.hi_flags && (r < 0))
1475 goto fail;
1476
1477 /* Enable interrupt */
1478 r = ioctl(fd, HPET_IE_ON, 0);
1479 if (r < 0)
1480 goto fail;
1481
1482 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001483 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001484
1485 return 0;
1486fail:
1487 close(fd);
1488 return -1;
1489}
1490
1491static void hpet_stop_timer(struct qemu_alarm_timer *t)
1492{
pbrookfcdc2122007-08-23 20:22:22 +00001493 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001494
1495 close(fd);
1496}
1497
thsc8994012007-08-19 21:56:03 +00001498static int rtc_start_timer(struct qemu_alarm_timer *t)
1499{
1500 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001501 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001502
balrogaeb30be2007-07-02 15:03:13 +00001503 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001504 if (rtc_fd < 0)
1505 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001506 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1507 if (current_rtc_freq != RTC_FREQ &&
1508 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001509 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1510 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1511 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1512 goto fail;
1513 }
1514 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1515 fail:
1516 close(rtc_fd);
1517 return -1;
1518 }
thsc8994012007-08-19 21:56:03 +00001519
1520 enable_sigio_timer(rtc_fd);
1521
pbrookfcdc2122007-08-23 20:22:22 +00001522 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001523
bellardfd872592004-05-12 19:11:15 +00001524 return 0;
1525}
1526
thsc8994012007-08-19 21:56:03 +00001527static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001528{
pbrookfcdc2122007-08-23 20:22:22 +00001529 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001530
1531 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001532}
1533
thsefe75412007-08-24 01:36:32 +00001534static int dynticks_start_timer(struct qemu_alarm_timer *t)
1535{
1536 struct sigevent ev;
1537 timer_t host_timer;
1538 struct sigaction act;
1539
1540 sigfillset(&act.sa_mask);
1541 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001542 act.sa_handler = host_alarm_handler;
1543
1544 sigaction(SIGALRM, &act, NULL);
1545
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001546 /*
1547 * Initialize ev struct to 0 to avoid valgrind complaining
1548 * about uninitialized data in timer_create call
1549 */
1550 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001551 ev.sigev_value.sival_int = 0;
1552 ev.sigev_notify = SIGEV_SIGNAL;
1553 ev.sigev_signo = SIGALRM;
1554
1555 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1556 perror("timer_create");
1557
1558 /* disable dynticks */
1559 fprintf(stderr, "Dynamic Ticks disabled\n");
1560
1561 return -1;
1562 }
1563
blueswir10399bfe2008-11-16 11:37:18 +00001564 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001565
1566 return 0;
1567}
1568
1569static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1570{
blueswir10399bfe2008-11-16 11:37:18 +00001571 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001572
1573 timer_delete(host_timer);
1574}
1575
1576static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1577{
blueswir10399bfe2008-11-16 11:37:18 +00001578 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001579 struct itimerspec timeout;
1580 int64_t nearest_delta_us = INT64_MAX;
1581 int64_t current_us;
1582
1583 if (!active_timers[QEMU_TIMER_REALTIME] &&
1584 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001585 return;
thsefe75412007-08-24 01:36:32 +00001586
pbrook2e70f6e2008-06-29 01:03:05 +00001587 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001588
1589 /* check whether a timer is already running */
1590 if (timer_gettime(host_timer, &timeout)) {
1591 perror("gettime");
1592 fprintf(stderr, "Internal timer error: aborting\n");
1593 exit(1);
1594 }
1595 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1596 if (current_us && current_us <= nearest_delta_us)
1597 return;
1598
1599 timeout.it_interval.tv_sec = 0;
1600 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1601 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1602 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1603 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1604 perror("settime");
1605 fprintf(stderr, "Internal timer error: aborting\n");
1606 exit(1);
1607 }
1608}
1609
ths70744b32007-08-26 17:31:30 +00001610#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001611
thsc8994012007-08-19 21:56:03 +00001612static int unix_start_timer(struct qemu_alarm_timer *t)
1613{
1614 struct sigaction act;
1615 struct itimerval itv;
1616 int err;
1617
1618 /* timer signal */
1619 sigfillset(&act.sa_mask);
1620 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001621 act.sa_handler = host_alarm_handler;
1622
1623 sigaction(SIGALRM, &act, NULL);
1624
1625 itv.it_interval.tv_sec = 0;
1626 /* for i386 kernel 2.6 to get 1 ms */
1627 itv.it_interval.tv_usec = 999;
1628 itv.it_value.tv_sec = 0;
1629 itv.it_value.tv_usec = 10 * 1000;
1630
1631 err = setitimer(ITIMER_REAL, &itv, NULL);
1632 if (err)
1633 return -1;
1634
1635 return 0;
1636}
1637
1638static void unix_stop_timer(struct qemu_alarm_timer *t)
1639{
1640 struct itimerval itv;
1641
1642 memset(&itv, 0, sizeof(itv));
1643 setitimer(ITIMER_REAL, &itv, NULL);
1644}
1645
bellard829309c2004-05-20 13:20:12 +00001646#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001647
aliguorif49e58d2008-11-05 21:22:34 +00001648
thsc8994012007-08-19 21:56:03 +00001649#ifdef _WIN32
1650
1651static int win32_start_timer(struct qemu_alarm_timer *t)
1652{
1653 TIMECAPS tc;
1654 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001655 UINT flags;
thsc8994012007-08-19 21:56:03 +00001656
thsc8994012007-08-19 21:56:03 +00001657 memset(&tc, 0, sizeof(tc));
1658 timeGetDevCaps(&tc, sizeof(tc));
1659
1660 if (data->period < tc.wPeriodMin)
1661 data->period = tc.wPeriodMin;
1662
1663 timeBeginPeriod(data->period);
1664
thsefe75412007-08-24 01:36:32 +00001665 flags = TIME_CALLBACK_FUNCTION;
1666 if (alarm_has_dynticks(t))
1667 flags |= TIME_ONESHOT;
1668 else
1669 flags |= TIME_PERIODIC;
1670
thsc8994012007-08-19 21:56:03 +00001671 data->timerId = timeSetEvent(1, // interval (ms)
1672 data->period, // resolution
1673 host_alarm_handler, // function
1674 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001675 flags);
thsc8994012007-08-19 21:56:03 +00001676
1677 if (!data->timerId) {
1678 perror("Failed to initialize win32 alarm timer");
thsc8994012007-08-19 21:56:03 +00001679 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001680 return -1;
1681 }
1682
thsc8994012007-08-19 21:56:03 +00001683 return 0;
1684}
1685
1686static void win32_stop_timer(struct qemu_alarm_timer *t)
1687{
1688 struct qemu_alarm_win32 *data = t->priv;
1689
1690 timeKillEvent(data->timerId);
1691 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001692}
1693
thsefe75412007-08-24 01:36:32 +00001694static void win32_rearm_timer(struct qemu_alarm_timer *t)
1695{
1696 struct qemu_alarm_win32 *data = t->priv;
1697 uint64_t nearest_delta_us;
1698
1699 if (!active_timers[QEMU_TIMER_REALTIME] &&
1700 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001701 return;
thsefe75412007-08-24 01:36:32 +00001702
pbrook2e70f6e2008-06-29 01:03:05 +00001703 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001704 nearest_delta_us /= 1000;
1705
1706 timeKillEvent(data->timerId);
1707
1708 data->timerId = timeSetEvent(1,
1709 data->period,
1710 host_alarm_handler,
1711 (DWORD)t,
1712 TIME_ONESHOT | TIME_PERIODIC);
1713
1714 if (!data->timerId) {
1715 perror("Failed to re-arm win32 alarm timer");
1716
1717 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001718 exit(1);
1719 }
1720}
1721
thsc8994012007-08-19 21:56:03 +00001722#endif /* _WIN32 */
1723
aliguori7183b4b2008-11-05 20:40:18 +00001724static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001725{
blueswir1223f0d72008-09-30 18:12:18 +00001726 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001727 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001728
thsc8994012007-08-19 21:56:03 +00001729 for (i = 0; alarm_timers[i].name; i++) {
1730 t = &alarm_timers[i];
1731
thsc8994012007-08-19 21:56:03 +00001732 err = t->start(t);
1733 if (!err)
1734 break;
bellard67b915a2004-03-31 23:37:16 +00001735 }
bellardfd872592004-05-12 19:11:15 +00001736
thsc8994012007-08-19 21:56:03 +00001737 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001738 err = -ENOENT;
1739 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001740 }
thsc8994012007-08-19 21:56:03 +00001741
1742 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001743
aliguori6abfbd72008-11-05 20:49:37 +00001744 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001745
1746fail:
aliguori7183b4b2008-11-05 20:40:18 +00001747 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001748}
1749
pbrook9596ebb2007-11-18 01:44:38 +00001750static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001751{
thsc8994012007-08-19 21:56:03 +00001752 alarm_timer->stop(alarm_timer);
1753 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001754}
1755
bellardc4b1fcc2004-03-14 21:44:30 +00001756/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001757/* host time/date access */
1758void qemu_get_timedate(struct tm *tm, int offset)
1759{
1760 time_t ti;
1761 struct tm *ret;
1762
1763 time(&ti);
1764 ti += offset;
1765 if (rtc_date_offset == -1) {
1766 if (rtc_utc)
1767 ret = gmtime(&ti);
1768 else
1769 ret = localtime(&ti);
1770 } else {
1771 ti -= rtc_date_offset;
1772 ret = gmtime(&ti);
1773 }
1774
1775 memcpy(tm, ret, sizeof(struct tm));
1776}
1777
1778int qemu_timedate_diff(struct tm *tm)
1779{
1780 time_t seconds;
1781
1782 if (rtc_date_offset == -1)
1783 if (rtc_utc)
1784 seconds = mktimegm(tm);
1785 else
1786 seconds = mktime(tm);
1787 else
1788 seconds = mktimegm(tm) + rtc_date_offset;
1789
1790 return seconds - time(NULL);
1791}
1792
bellardfd1dff42006-02-01 21:29:26 +00001793#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001794static void socket_cleanup(void)
1795{
1796 WSACleanup();
1797}
bellard82c643f2004-07-14 17:28:13 +00001798
bellardfd1dff42006-02-01 21:29:26 +00001799static int socket_init(void)
1800{
1801 WSADATA Data;
1802 int ret, err;
1803
1804 ret = WSAStartup(MAKEWORD(2,2), &Data);
1805 if (ret != 0) {
1806 err = WSAGetLastError();
1807 fprintf(stderr, "WSAStartup: %d\n", err);
1808 return -1;
1809 }
1810 atexit(socket_cleanup);
1811 return 0;
1812}
aurel3264b7b732008-05-05 10:05:31 +00001813#endif
1814
aliguori63a01ef2008-10-31 19:10:00 +00001815int get_param_value(char *buf, int buf_size,
1816 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001817{
1818 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001819 char option[128];
1820
1821 p = str;
1822 for(;;) {
aliguori268a3622009-04-21 22:30:27 +00001823 p = get_opt_name(option, sizeof(option), p, '=');
bellard7c9d8e02005-11-15 22:16:05 +00001824 if (*p != '=')
1825 break;
1826 p++;
1827 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001828 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001829 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001830 } else {
balrog609497a2008-01-14 02:56:53 +00001831 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001832 }
1833 if (*p != ',')
1834 break;
1835 p++;
1836 }
1837 return 0;
1838}
1839
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001840int check_params(char *buf, int buf_size,
1841 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001842{
1843 const char *p;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001844 int i;
thse4bcb142007-12-02 04:51:10 +00001845
1846 p = str;
aliguori10300212009-04-21 19:56:23 +00001847 while (*p != '\0') {
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001848 p = get_opt_name(buf, buf_size, p, '=');
Jan Kiszkaffad4112009-04-26 18:53:42 +02001849 if (*p != '=') {
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001850 return -1;
Jan Kiszkaffad4112009-04-26 18:53:42 +02001851 }
thse4bcb142007-12-02 04:51:10 +00001852 p++;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001853 for (i = 0; params[i] != NULL; i++) {
1854 if (!strcmp(params[i], buf)) {
thse4bcb142007-12-02 04:51:10 +00001855 break;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001856 }
1857 }
Jan Kiszkaffad4112009-04-26 18:53:42 +02001858 if (params[i] == NULL) {
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001859 return -1;
Jan Kiszkaffad4112009-04-26 18:53:42 +02001860 }
balrog609497a2008-01-14 02:56:53 +00001861 p = get_opt_value(NULL, 0, p);
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001862 if (*p != ',') {
thse4bcb142007-12-02 04:51:10 +00001863 break;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001864 }
thse4bcb142007-12-02 04:51:10 +00001865 p++;
1866 }
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001867 return 0;
thse4bcb142007-12-02 04:51:10 +00001868}
1869
balrog1ae26a12008-09-28 23:19:47 +00001870/***********************************************************/
1871/* Bluetooth support */
1872static int nb_hcis;
1873static int cur_hci;
1874static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001875
balrog1ae26a12008-09-28 23:19:47 +00001876static struct bt_vlan_s {
1877 struct bt_scatternet_s net;
1878 int id;
1879 struct bt_vlan_s *next;
1880} *first_bt_vlan;
1881
1882/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001883static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001884{
1885 struct bt_vlan_s **pvlan, *vlan;
1886 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1887 if (vlan->id == id)
1888 return &vlan->net;
1889 }
1890 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1891 vlan->id = id;
1892 pvlan = &first_bt_vlan;
1893 while (*pvlan != NULL)
1894 pvlan = &(*pvlan)->next;
1895 *pvlan = vlan;
1896 return &vlan->net;
1897}
1898
1899static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1900{
1901}
1902
1903static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1904{
1905 return -ENOTSUP;
1906}
1907
1908static struct HCIInfo null_hci = {
1909 .cmd_send = null_hci_send,
1910 .sco_send = null_hci_send,
1911 .acl_send = null_hci_send,
1912 .bdaddr_set = null_hci_addr_set,
1913};
1914
1915struct HCIInfo *qemu_next_hci(void)
1916{
1917 if (cur_hci == nb_hcis)
1918 return &null_hci;
1919
1920 return hci_table[cur_hci++];
1921}
1922
balrogdc72ac12008-11-09 00:04:26 +00001923static struct HCIInfo *hci_init(const char *str)
1924{
1925 char *endp;
1926 struct bt_scatternet_s *vlan = 0;
1927
1928 if (!strcmp(str, "null"))
1929 /* null */
1930 return &null_hci;
1931 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1932 /* host[:hciN] */
1933 return bt_host_hci(str[4] ? str + 5 : "hci0");
1934 else if (!strncmp(str, "hci", 3)) {
1935 /* hci[,vlan=n] */
1936 if (str[3]) {
1937 if (!strncmp(str + 3, ",vlan=", 6)) {
1938 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1939 if (*endp)
1940 vlan = 0;
1941 }
1942 } else
1943 vlan = qemu_find_bt_vlan(0);
1944 if (vlan)
1945 return bt_new_hci(vlan);
1946 }
1947
1948 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1949
1950 return 0;
1951}
1952
1953static int bt_hci_parse(const char *str)
1954{
1955 struct HCIInfo *hci;
1956 bdaddr_t bdaddr;
1957
1958 if (nb_hcis >= MAX_NICS) {
1959 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1960 return -1;
1961 }
1962
1963 hci = hci_init(str);
1964 if (!hci)
1965 return -1;
1966
1967 bdaddr.b[0] = 0x52;
1968 bdaddr.b[1] = 0x54;
1969 bdaddr.b[2] = 0x00;
1970 bdaddr.b[3] = 0x12;
1971 bdaddr.b[4] = 0x34;
1972 bdaddr.b[5] = 0x56 + nb_hcis;
1973 hci->bdaddr_set(hci, bdaddr.b);
1974
1975 hci_table[nb_hcis++] = hci;
1976
1977 return 0;
1978}
1979
1980static void bt_vhci_add(int vlan_id)
1981{
1982 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1983
1984 if (!vlan->slave)
1985 fprintf(stderr, "qemu: warning: adding a VHCI to "
1986 "an empty scatternet %i\n", vlan_id);
1987
1988 bt_vhci_init(bt_new_hci(vlan));
1989}
1990
1991static struct bt_device_s *bt_device_add(const char *opt)
1992{
1993 struct bt_scatternet_s *vlan;
1994 int vlan_id = 0;
1995 char *endp = strstr(opt, ",vlan=");
1996 int len = (endp ? endp - opt : strlen(opt)) + 1;
1997 char devname[10];
1998
1999 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2000
2001 if (endp) {
2002 vlan_id = strtol(endp + 6, &endp, 0);
2003 if (*endp) {
2004 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2005 return 0;
2006 }
2007 }
2008
2009 vlan = qemu_find_bt_vlan(vlan_id);
2010
2011 if (!vlan->slave)
2012 fprintf(stderr, "qemu: warning: adding a slave device to "
2013 "an empty scatternet %i\n", vlan_id);
2014
2015 if (!strcmp(devname, "keyboard"))
2016 return bt_keyboard_init(vlan);
2017
2018 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2019 return 0;
2020}
2021
2022static int bt_parse(const char *opt)
2023{
2024 const char *endp, *p;
2025 int vlan;
2026
2027 if (strstart(opt, "hci", &endp)) {
2028 if (!*endp || *endp == ',') {
2029 if (*endp)
2030 if (!strstart(endp, ",vlan=", 0))
2031 opt = endp + 1;
2032
2033 return bt_hci_parse(opt);
2034 }
2035 } else if (strstart(opt, "vhci", &endp)) {
2036 if (!*endp || *endp == ',') {
2037 if (*endp) {
2038 if (strstart(endp, ",vlan=", &p)) {
2039 vlan = strtol(p, (char **) &endp, 0);
2040 if (*endp) {
2041 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2042 return 1;
2043 }
2044 } else {
2045 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2046 return 1;
2047 }
2048 } else
2049 vlan = 0;
2050
2051 bt_vhci_add(vlan);
2052 return 0;
2053 }
2054 } else if (strstart(opt, "device:", &endp))
2055 return !bt_device_add(endp);
2056
2057 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2058 return 1;
2059}
2060
balrog1ae26a12008-09-28 23:19:47 +00002061/***********************************************************/
2062/* QEMU Block devices */
2063
balrog609497a2008-01-14 02:56:53 +00002064#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00002065#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00002066#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00002067#define PFLASH_ALIAS "if=pflash"
2068#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00002069#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002070
aliguori7d5aca92009-02-11 15:19:58 +00002071static int drive_opt_get_free_idx(void)
2072{
2073 int index;
2074
2075 for (index = 0; index < MAX_DRIVES; index++)
2076 if (!drives_opt[index].used) {
2077 drives_opt[index].used = 1;
2078 return index;
2079 }
2080
2081 return -1;
2082}
2083
2084static int drive_get_free_idx(void)
2085{
2086 int index;
2087
2088 for (index = 0; index < MAX_DRIVES; index++)
2089 if (!drives_table[index].used) {
2090 drives_table[index].used = 1;
2091 return index;
2092 }
2093
2094 return -1;
2095}
2096
aliguori4d73cd32009-02-11 15:20:46 +00002097int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002098{
2099 va_list ap;
aliguori7d5aca92009-02-11 15:19:58 +00002100 int index = drive_opt_get_free_idx();
thse4bcb142007-12-02 04:51:10 +00002101
aliguori7d5aca92009-02-11 15:19:58 +00002102 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
thse4bcb142007-12-02 04:51:10 +00002103 fprintf(stderr, "qemu: too many drives\n");
aliguori4d73cd32009-02-11 15:20:46 +00002104 return -1;
thse4bcb142007-12-02 04:51:10 +00002105 }
2106
aliguori7d5aca92009-02-11 15:19:58 +00002107 drives_opt[index].file = file;
thse4bcb142007-12-02 04:51:10 +00002108 va_start(ap, fmt);
aliguori7d5aca92009-02-11 15:19:58 +00002109 vsnprintf(drives_opt[index].opt,
balrog609497a2008-01-14 02:56:53 +00002110 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002111 va_end(ap);
2112
aliguori7d5aca92009-02-11 15:19:58 +00002113 nb_drives_opt++;
2114 return index;
thse4bcb142007-12-02 04:51:10 +00002115}
2116
aliguorib01b1112009-02-11 15:20:20 +00002117void drive_remove(int index)
2118{
2119 drives_opt[index].used = 0;
2120 nb_drives_opt--;
2121}
2122
thsf60d39b2007-12-17 03:55:57 +00002123int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002124{
2125 int index;
2126
2127 /* seek interface, bus and unit */
2128
aliguori7d5aca92009-02-11 15:19:58 +00002129 for (index = 0; index < MAX_DRIVES; index++)
thsf60d39b2007-12-17 03:55:57 +00002130 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002131 drives_table[index].bus == bus &&
aliguori7d5aca92009-02-11 15:19:58 +00002132 drives_table[index].unit == unit &&
2133 drives_table[index].used)
thse4bcb142007-12-02 04:51:10 +00002134 return index;
2135
2136 return -1;
2137}
2138
thsf60d39b2007-12-17 03:55:57 +00002139int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002140{
2141 int max_bus;
2142 int index;
2143
2144 max_bus = -1;
2145 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002146 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002147 drives_table[index].bus > max_bus)
2148 max_bus = drives_table[index].bus;
2149 }
2150 return max_bus;
2151}
2152
aliguorifa879c62009-01-07 17:32:33 +00002153const char *drive_get_serial(BlockDriverState *bdrv)
2154{
2155 int index;
2156
2157 for (index = 0; index < nb_drives; index++)
2158 if (drives_table[index].bdrv == bdrv)
2159 return drives_table[index].serial;
2160
2161 return "\0";
2162}
2163
aliguori428c5702009-01-21 18:59:04 +00002164BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2165{
2166 int index;
2167
2168 for (index = 0; index < nb_drives; index++)
2169 if (drives_table[index].bdrv == bdrv)
2170 return drives_table[index].onerror;
2171
aliguoricdad4bd2009-02-28 16:51:01 +00002172 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002173}
2174
aurel32a1620fa2008-04-29 05:58:01 +00002175static void bdrv_format_print(void *opaque, const char *name)
2176{
2177 fprintf(stderr, " %s", name);
2178}
2179
aliguorib01b1112009-02-11 15:20:20 +00002180void drive_uninit(BlockDriverState *bdrv)
2181{
2182 int i;
2183
2184 for (i = 0; i < MAX_DRIVES; i++)
2185 if (drives_table[i].bdrv == bdrv) {
2186 drives_table[i].bdrv = NULL;
2187 drives_table[i].used = 0;
2188 drive_remove(drives_table[i].drive_opt_idx);
2189 nb_drives--;
2190 break;
2191 }
2192}
2193
aliguori4d73cd32009-02-11 15:20:46 +00002194int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
thse4bcb142007-12-02 04:51:10 +00002195{
2196 char buf[128];
2197 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002198 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00002199 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002200 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002201 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002202 enum { MEDIA_DISK, MEDIA_CDROM } media;
2203 int bus_id, unit_id;
2204 int cyls, heads, secs, translation;
2205 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002206 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002207 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002208 int max_devs;
2209 int index;
balrog33f00272007-12-24 14:33:24 +00002210 int cache;
aliguori428c5702009-01-21 18:59:04 +00002211 int bdrv_flags, onerror;
aliguori7d5aca92009-02-11 15:19:58 +00002212 int drives_table_idx;
balrog609497a2008-01-14 02:56:53 +00002213 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002214 static const char * const params[] = { "bus", "unit", "if", "index",
2215 "cyls", "heads", "secs", "trans",
2216 "media", "snapshot", "file",
aliguori428c5702009-01-21 18:59:04 +00002217 "cache", "format", "serial", "werror",
2218 NULL };
thse4bcb142007-12-02 04:51:10 +00002219
Jan Kiszka0aa7a202009-05-08 12:34:17 +02002220 if (check_params(buf, sizeof(buf), params, str) < 0) {
Mark McLoughlincda94b22009-05-28 14:07:31 +01002221 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
2222 buf, str);
thse4bcb142007-12-02 04:51:10 +00002223 return -1;
2224 }
2225
2226 file[0] = 0;
2227 cyls = heads = secs = 0;
2228 bus_id = 0;
2229 unit_id = -1;
2230 translation = BIOS_ATA_TRANSLATION_AUTO;
2231 index = -1;
aliguori4dc822d2008-12-04 21:39:21 +00002232 cache = 3;
thse4bcb142007-12-02 04:51:10 +00002233
blueswir1c9b1ae22008-09-28 18:55:17 +00002234 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002235 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002236 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002237 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002238 } else {
thsf60d39b2007-12-17 03:55:57 +00002239 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002240 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002241 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002242 }
2243 media = MEDIA_DISK;
2244
2245 /* extract parameters */
2246
2247 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2248 bus_id = strtol(buf, NULL, 0);
2249 if (bus_id < 0) {
2250 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2251 return -1;
2252 }
2253 }
2254
2255 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2256 unit_id = strtol(buf, NULL, 0);
2257 if (unit_id < 0) {
2258 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2259 return -1;
2260 }
2261 }
2262
2263 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002264 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002265 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002266 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002267 max_devs = MAX_IDE_DEVS;
2268 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002269 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002270 max_devs = MAX_SCSI_DEVS;
2271 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002272 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002273 max_devs = 0;
2274 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002275 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002276 max_devs = 0;
2277 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002278 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002279 max_devs = 0;
2280 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002281 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002282 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002283 } else if (!strcmp(buf, "virtio")) {
2284 type = IF_VIRTIO;
2285 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002286 } else if (!strcmp(buf, "xen")) {
2287 type = IF_XEN;
2288 max_devs = 0;
2289 } else {
thse4bcb142007-12-02 04:51:10 +00002290 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2291 return -1;
2292 }
2293 }
2294
2295 if (get_param_value(buf, sizeof(buf), "index", str)) {
2296 index = strtol(buf, NULL, 0);
2297 if (index < 0) {
2298 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2299 return -1;
2300 }
2301 }
2302
2303 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2304 cyls = strtol(buf, NULL, 0);
2305 }
2306
2307 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2308 heads = strtol(buf, NULL, 0);
2309 }
2310
2311 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2312 secs = strtol(buf, NULL, 0);
2313 }
2314
2315 if (cyls || heads || secs) {
2316 if (cyls < 1 || cyls > 16383) {
2317 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2318 return -1;
2319 }
2320 if (heads < 1 || heads > 16) {
2321 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2322 return -1;
2323 }
2324 if (secs < 1 || secs > 63) {
2325 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2326 return -1;
2327 }
2328 }
2329
2330 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2331 if (!cyls) {
2332 fprintf(stderr,
2333 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2334 str);
2335 return -1;
2336 }
2337 if (!strcmp(buf, "none"))
2338 translation = BIOS_ATA_TRANSLATION_NONE;
2339 else if (!strcmp(buf, "lba"))
2340 translation = BIOS_ATA_TRANSLATION_LBA;
2341 else if (!strcmp(buf, "auto"))
2342 translation = BIOS_ATA_TRANSLATION_AUTO;
2343 else {
2344 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2345 return -1;
2346 }
2347 }
2348
2349 if (get_param_value(buf, sizeof(buf), "media", str)) {
2350 if (!strcmp(buf, "disk")) {
2351 media = MEDIA_DISK;
2352 } else if (!strcmp(buf, "cdrom")) {
2353 if (cyls || secs || heads) {
2354 fprintf(stderr,
2355 "qemu: '%s' invalid physical CHS format\n", str);
2356 return -1;
2357 }
2358 media = MEDIA_CDROM;
2359 } else {
2360 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2361 return -1;
2362 }
2363 }
2364
2365 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2366 if (!strcmp(buf, "on"))
2367 snapshot = 1;
2368 else if (!strcmp(buf, "off"))
2369 snapshot = 0;
2370 else {
2371 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2372 return -1;
2373 }
2374 }
2375
balrog33f00272007-12-24 14:33:24 +00002376 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002377 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002378 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002379 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002380 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002381 else if (!strcmp(buf, "writeback"))
2382 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002383 else {
2384 fprintf(stderr, "qemu: invalid cache option\n");
2385 return -1;
2386 }
2387 }
2388
aurel321e72d3b2008-04-28 20:26:45 +00002389 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002390 if (strcmp(buf, "?") == 0) {
2391 fprintf(stderr, "qemu: Supported formats:");
2392 bdrv_iterate_format(bdrv_format_print, NULL);
2393 fprintf(stderr, "\n");
2394 return -1;
2395 }
aurel321e72d3b2008-04-28 20:26:45 +00002396 drv = bdrv_find_format(buf);
2397 if (!drv) {
2398 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2399 return -1;
2400 }
2401 }
2402
balrog609497a2008-01-14 02:56:53 +00002403 if (arg->file == NULL)
2404 get_param_value(file, sizeof(file), "file", str);
2405 else
2406 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002407
aliguorifa879c62009-01-07 17:32:33 +00002408 if (!get_param_value(serial, sizeof(serial), "serial", str))
2409 memset(serial, 0, sizeof(serial));
2410
aliguoricdad4bd2009-02-28 16:51:01 +00002411 onerror = BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002412 if (get_param_value(buf, sizeof(serial), "werror", str)) {
aliguori869a5c62009-01-22 19:52:25 +00002413 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002414 fprintf(stderr, "werror is no supported by this format\n");
aliguori428c5702009-01-21 18:59:04 +00002415 return -1;
2416 }
2417 if (!strcmp(buf, "ignore"))
2418 onerror = BLOCK_ERR_IGNORE;
2419 else if (!strcmp(buf, "enospc"))
2420 onerror = BLOCK_ERR_STOP_ENOSPC;
2421 else if (!strcmp(buf, "stop"))
2422 onerror = BLOCK_ERR_STOP_ANY;
2423 else if (!strcmp(buf, "report"))
2424 onerror = BLOCK_ERR_REPORT;
2425 else {
2426 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2427 return -1;
2428 }
2429 }
2430
thse4bcb142007-12-02 04:51:10 +00002431 /* compute bus and unit according index */
2432
2433 if (index != -1) {
2434 if (bus_id != 0 || unit_id != -1) {
2435 fprintf(stderr,
2436 "qemu: '%s' index cannot be used with bus and unit\n", str);
2437 return -1;
2438 }
2439 if (max_devs == 0)
2440 {
2441 unit_id = index;
2442 bus_id = 0;
2443 } else {
2444 unit_id = index % max_devs;
2445 bus_id = index / max_devs;
2446 }
2447 }
2448
2449 /* if user doesn't specify a unit_id,
2450 * try to find the first free
2451 */
2452
2453 if (unit_id == -1) {
2454 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002455 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002456 unit_id++;
2457 if (max_devs && unit_id >= max_devs) {
2458 unit_id -= max_devs;
2459 bus_id++;
2460 }
2461 }
2462 }
2463
2464 /* check unit id */
2465
2466 if (max_devs && unit_id >= max_devs) {
2467 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2468 str, unit_id, max_devs - 1);
2469 return -1;
2470 }
2471
2472 /*
2473 * ignore multiple definitions
2474 */
2475
thsf60d39b2007-12-17 03:55:57 +00002476 if (drive_get_index(type, bus_id, unit_id) != -1)
aliguori4d73cd32009-02-11 15:20:46 +00002477 return -2;
thse4bcb142007-12-02 04:51:10 +00002478
2479 /* init */
2480
thsf60d39b2007-12-17 03:55:57 +00002481 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002482 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002483 if (max_devs)
2484 snprintf(buf, sizeof(buf), "%s%i%s%i",
2485 devname, bus_id, mediastr, unit_id);
2486 else
2487 snprintf(buf, sizeof(buf), "%s%s%i",
2488 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002489 bdrv = bdrv_new(buf);
aliguori7d5aca92009-02-11 15:19:58 +00002490 drives_table_idx = drive_get_free_idx();
2491 drives_table[drives_table_idx].bdrv = bdrv;
2492 drives_table[drives_table_idx].type = type;
2493 drives_table[drives_table_idx].bus = bus_id;
2494 drives_table[drives_table_idx].unit = unit_id;
2495 drives_table[drives_table_idx].onerror = onerror;
aliguorib01b1112009-02-11 15:20:20 +00002496 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
Anthony Liguorie6a6dfe2009-05-01 09:26:16 -05002497 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002498 nb_drives++;
2499
thsf60d39b2007-12-17 03:55:57 +00002500 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002501 case IF_IDE:
2502 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002503 case IF_XEN:
thse4bcb142007-12-02 04:51:10 +00002504 switch(media) {
2505 case MEDIA_DISK:
2506 if (cyls != 0) {
2507 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2508 bdrv_set_translation_hint(bdrv, translation);
2509 }
2510 break;
2511 case MEDIA_CDROM:
2512 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2513 break;
2514 }
2515 break;
2516 case IF_SD:
2517 /* FIXME: This isn't really a floppy, but it's a reasonable
2518 approximation. */
2519 case IF_FLOPPY:
2520 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2521 break;
2522 case IF_PFLASH:
2523 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002524 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002525 break;
Paul Brookaae94602009-05-14 22:35:06 +01002526 case IF_COUNT:
2527 abort();
thse4bcb142007-12-02 04:51:10 +00002528 }
2529 if (!file[0])
aliguori4d73cd32009-02-11 15:20:46 +00002530 return -2;
balrog33f00272007-12-24 14:33:24 +00002531 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002532 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002533 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002534 cache = 2; /* always use write-back with snapshot */
2535 }
2536 if (cache == 0) /* no caching */
2537 bdrv_flags |= BDRV_O_NOCACHE;
2538 else if (cache == 2) /* write-back */
2539 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori4dc822d2008-12-04 21:39:21 +00002540 else if (cache == 3) /* not specified */
2541 bdrv_flags |= BDRV_O_CACHE_DEF;
aliguoric0f4ce72009-03-05 23:01:01 +00002542 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002543 fprintf(stderr, "qemu: could not open disk image %s\n",
2544 file);
2545 return -1;
2546 }
aliguoric0f4ce72009-03-05 23:01:01 +00002547 if (bdrv_key_required(bdrv))
2548 autostart = 0;
aliguori4d73cd32009-02-11 15:20:46 +00002549 return drives_table_idx;
thse4bcb142007-12-02 04:51:10 +00002550}
2551
aliguori268a3622009-04-21 22:30:27 +00002552static void numa_add(const char *optarg)
2553{
2554 char option[128];
2555 char *endptr;
2556 unsigned long long value, endvalue;
2557 int nodenr;
2558
2559 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2560 if (!strcmp(option, "node")) {
2561 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2562 nodenr = nb_numa_nodes;
2563 } else {
2564 nodenr = strtoull(option, NULL, 10);
2565 }
2566
2567 if (get_param_value(option, 128, "mem", optarg) == 0) {
2568 node_mem[nodenr] = 0;
2569 } else {
2570 value = strtoull(option, &endptr, 0);
2571 switch (*endptr) {
2572 case 0: case 'M': case 'm':
2573 value <<= 20;
2574 break;
2575 case 'G': case 'g':
2576 value <<= 30;
2577 break;
2578 }
2579 node_mem[nodenr] = value;
2580 }
2581 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2582 node_cpumask[nodenr] = 0;
2583 } else {
2584 value = strtoull(option, &endptr, 10);
2585 if (value >= 64) {
2586 value = 63;
2587 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2588 } else {
2589 if (*endptr == '-') {
2590 endvalue = strtoull(endptr+1, &endptr, 10);
2591 if (endvalue >= 63) {
2592 endvalue = 62;
2593 fprintf(stderr,
2594 "only 63 CPUs in NUMA mode supported.\n");
2595 }
2596 value = (1 << (endvalue + 1)) - (1 << value);
2597 } else {
2598 value = 1 << value;
2599 }
2600 }
2601 node_cpumask[nodenr] = value;
2602 }
2603 nb_numa_nodes++;
2604 }
2605 return;
2606}
2607
bellard330d0412003-07-26 18:11:40 +00002608/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002609/* USB devices */
2610
pbrook0d92ed32006-05-21 16:30:15 +00002611static USBPort *used_usb_ports;
2612static USBPort *free_usb_ports;
2613
2614/* ??? Maybe change this to register a hub to keep track of the topology. */
2615void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2616 usb_attachfn attach)
2617{
2618 port->opaque = opaque;
2619 port->index = index;
2620 port->attach = attach;
2621 port->next = free_usb_ports;
2622 free_usb_ports = port;
2623}
2624
aliguori4b096fc2008-08-21 19:28:55 +00002625int usb_device_add_dev(USBDevice *dev)
2626{
2627 USBPort *port;
2628
2629 /* Find a USB port to add the device to. */
2630 port = free_usb_ports;
2631 if (!port->next) {
2632 USBDevice *hub;
2633
2634 /* Create a new hub and chain it on. */
2635 free_usb_ports = NULL;
2636 port->next = used_usb_ports;
2637 used_usb_ports = port;
2638
2639 hub = usb_hub_init(VM_USB_HUB_SIZE);
2640 usb_attach(port, hub);
2641 port = free_usb_ports;
2642 }
2643
2644 free_usb_ports = port->next;
2645 port->next = used_usb_ports;
2646 used_usb_ports = port;
2647 usb_attach(port, dev);
2648 return 0;
2649}
2650
aliguoribb5fc202009-03-05 23:01:15 +00002651static void usb_msd_password_cb(void *opaque, int err)
2652{
2653 USBDevice *dev = opaque;
2654
2655 if (!err)
2656 usb_device_add_dev(dev);
2657 else
2658 dev->handle_destroy(dev);
2659}
2660
aliguoric0f4ce72009-03-05 23:01:01 +00002661static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002662{
2663 const char *p;
2664 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002665
pbrook0d92ed32006-05-21 16:30:15 +00002666 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002667 return -1;
2668
2669 if (strstart(devname, "host:", &p)) {
2670 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002671 } else if (!strcmp(devname, "mouse")) {
2672 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002673 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002674 dev = usb_tablet_init();
2675 } else if (!strcmp(devname, "keyboard")) {
2676 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002677 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002678 BlockDriverState *bs;
2679
aliguoribb5fc202009-03-05 23:01:15 +00002680 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002681 if (!dev)
2682 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002683 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002684 if (bdrv_key_required(bs)) {
2685 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002686 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002687 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2688 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002689 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002690 }
2691 }
balrogf6d2a312007-06-10 19:21:04 +00002692 } else if (!strcmp(devname, "wacom-tablet")) {
2693 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002694 } else if (strstart(devname, "serial:", &p)) {
2695 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002696#ifdef CONFIG_BRLAPI
2697 } else if (!strcmp(devname, "braille")) {
2698 dev = usb_baum_init();
2699#endif
balrog6c9f8862008-07-17 20:47:13 +00002700 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002701 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002702
Jan Kiszka10ae5a72009-05-08 12:34:18 +02002703 if (net_client_init(NULL, "nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002704 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002705 nd_table[nic].model = "usb";
2706 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002707 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2708 dev = usb_bt_init(devname[2] ? hci_init(p) :
2709 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002710 } else {
2711 return -1;
2712 }
pbrook0d92ed32006-05-21 16:30:15 +00002713 if (!dev)
2714 return -1;
2715
aliguori4b096fc2008-08-21 19:28:55 +00002716 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002717}
2718
aliguori1f3870a2008-08-21 19:27:48 +00002719int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002720{
pbrook0d92ed32006-05-21 16:30:15 +00002721 USBPort *port;
2722 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002723 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002724
pbrook0d92ed32006-05-21 16:30:15 +00002725 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002726 return -1;
2727
bellarda594cfb2005-11-06 16:13:29 +00002728 if (bus_num != 0)
2729 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002730
2731 lastp = &used_usb_ports;
2732 port = used_usb_ports;
2733 while (port && port->dev->addr != addr) {
2734 lastp = &port->next;
2735 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002736 }
pbrook0d92ed32006-05-21 16:30:15 +00002737
2738 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002739 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002740
bellard059809e2006-07-19 18:06:15 +00002741 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002742 *lastp = port->next;
2743 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002744 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002745 port->next = free_usb_ports;
2746 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002747 return 0;
2748}
2749
aliguori1f3870a2008-08-21 19:27:48 +00002750static int usb_device_del(const char *devname)
2751{
2752 int bus_num, addr;
2753 const char *p;
2754
aliguori5d0c5752008-09-14 01:07:41 +00002755 if (strstart(devname, "host:", &p))
2756 return usb_host_device_close(p);
2757
aliguori1f3870a2008-08-21 19:27:48 +00002758 if (!used_usb_ports)
2759 return -1;
2760
2761 p = strchr(devname, '.');
2762 if (!p)
2763 return -1;
2764 bus_num = strtoul(devname, NULL, 0);
2765 addr = strtoul(p + 1, NULL, 0);
2766
2767 return usb_device_del_addr(bus_num, addr);
2768}
2769
aliguori376253e2009-03-05 23:01:23 +00002770void do_usb_add(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002771{
aliguoric0f4ce72009-03-05 23:01:01 +00002772 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002773}
2774
aliguori376253e2009-03-05 23:01:23 +00002775void do_usb_del(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002776{
aliguori4b096fc2008-08-21 19:28:55 +00002777 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002778}
2779
aliguori376253e2009-03-05 23:01:23 +00002780void usb_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00002781{
2782 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002783 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002784 const char *speed_str;
2785
pbrook0d92ed32006-05-21 16:30:15 +00002786 if (!usb_enabled) {
aliguori376253e2009-03-05 23:01:23 +00002787 monitor_printf(mon, "USB support not enabled\n");
bellarda594cfb2005-11-06 16:13:29 +00002788 return;
2789 }
2790
pbrook0d92ed32006-05-21 16:30:15 +00002791 for (port = used_usb_ports; port; port = port->next) {
2792 dev = port->dev;
2793 if (!dev)
2794 continue;
2795 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002796 case USB_SPEED_LOW:
2797 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002798 break;
ths5fafdf22007-09-16 21:08:06 +00002799 case USB_SPEED_FULL:
2800 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002801 break;
ths5fafdf22007-09-16 21:08:06 +00002802 case USB_SPEED_HIGH:
2803 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002804 break;
2805 default:
ths5fafdf22007-09-16 21:08:06 +00002806 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002807 break;
bellarda594cfb2005-11-06 16:13:29 +00002808 }
aliguori376253e2009-03-05 23:01:23 +00002809 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2810 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002811 }
2812}
2813
bellardf7cce892004-12-08 22:21:25 +00002814/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002815/* PCMCIA/Cardbus */
2816
2817static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002818 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002819 struct pcmcia_socket_entry_s *next;
2820} *pcmcia_sockets = 0;
2821
Paul Brookbc24a222009-05-10 01:44:56 +01002822void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002823{
2824 struct pcmcia_socket_entry_s *entry;
2825
2826 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2827 entry->socket = socket;
2828 entry->next = pcmcia_sockets;
2829 pcmcia_sockets = entry;
2830}
2831
Paul Brookbc24a222009-05-10 01:44:56 +01002832void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002833{
2834 struct pcmcia_socket_entry_s *entry, **ptr;
2835
2836 ptr = &pcmcia_sockets;
2837 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2838 if (entry->socket == socket) {
2839 *ptr = entry->next;
2840 qemu_free(entry);
2841 }
2842}
2843
aliguori376253e2009-03-05 23:01:23 +00002844void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002845{
2846 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002847
balrog201a51f2007-04-30 00:51:09 +00002848 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002849 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002850
2851 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002852 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2853 iter->socket->attached ? iter->socket->card_string :
2854 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002855}
2856
2857/***********************************************************/
aliguori3023f3322009-01-16 19:04:14 +00002858/* register display */
2859
aliguori7b5d76d2009-03-13 15:02:13 +00002860struct DisplayAllocator default_allocator = {
2861 defaultallocator_create_displaysurface,
2862 defaultallocator_resize_displaysurface,
2863 defaultallocator_free_displaysurface
2864};
2865
aliguori3023f3322009-01-16 19:04:14 +00002866void register_displaystate(DisplayState *ds)
2867{
2868 DisplayState **s;
2869 s = &display_state;
2870 while (*s != NULL)
2871 s = &(*s)->next;
2872 ds->next = NULL;
2873 *s = ds;
2874}
2875
2876DisplayState *get_displaystate(void)
2877{
2878 return display_state;
2879}
2880
aliguori7b5d76d2009-03-13 15:02:13 +00002881DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2882{
2883 if(ds->allocator == &default_allocator) ds->allocator = da;
2884 return ds->allocator;
2885}
2886
ths2ff89792007-06-21 23:34:19 +00002887/* dumb display */
2888
aliguori8f391ab2009-01-19 16:34:10 +00002889static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002890{
aliguori8f391ab2009-01-19 16:34:10 +00002891 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002892 ds->allocator = &default_allocator;
2893 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002894 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002895}
2896
2897/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002898/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002899
bellardc4b1fcc2004-03-14 21:44:30 +00002900typedef struct IOHandlerRecord {
2901 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002902 IOCanRWHandler *fd_read_poll;
2903 IOHandler *fd_read;
2904 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002905 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002906 void *opaque;
2907 /* temporary data */
2908 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002909 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002910} IOHandlerRecord;
2911
bellard8a7ddc32004-03-31 19:00:16 +00002912static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002913
bellard7c9d8e02005-11-15 22:16:05 +00002914/* XXX: fd_read_poll should be suppressed, but an API change is
2915 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002916int qemu_set_fd_handler2(int fd,
2917 IOCanRWHandler *fd_read_poll,
2918 IOHandler *fd_read,
2919 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002920 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002921{
bellard8a7ddc32004-03-31 19:00:16 +00002922 IOHandlerRecord **pioh, *ioh;
2923
bellard7c9d8e02005-11-15 22:16:05 +00002924 if (!fd_read && !fd_write) {
2925 pioh = &first_io_handler;
2926 for(;;) {
2927 ioh = *pioh;
2928 if (ioh == NULL)
2929 break;
2930 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002931 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002932 break;
2933 }
2934 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002935 }
bellard7c9d8e02005-11-15 22:16:05 +00002936 } else {
2937 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2938 if (ioh->fd == fd)
2939 goto found;
2940 }
2941 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002942 ioh->next = first_io_handler;
2943 first_io_handler = ioh;
2944 found:
2945 ioh->fd = fd;
2946 ioh->fd_read_poll = fd_read_poll;
2947 ioh->fd_read = fd_read;
2948 ioh->fd_write = fd_write;
2949 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002950 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002951 }
bellard7c9d8e02005-11-15 22:16:05 +00002952 return 0;
2953}
2954
ths5fafdf22007-09-16 21:08:06 +00002955int qemu_set_fd_handler(int fd,
2956 IOHandler *fd_read,
2957 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002958 void *opaque)
2959{
2960 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002961}
2962
aliguori56f3a5d2008-10-31 18:07:17 +00002963#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002964/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002965/* Polling handling */
2966
2967typedef struct PollingEntry {
2968 PollingFunc *func;
2969 void *opaque;
2970 struct PollingEntry *next;
2971} PollingEntry;
2972
2973static PollingEntry *first_polling_entry;
2974
2975int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2976{
2977 PollingEntry **ppe, *pe;
2978 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002979 pe->func = func;
2980 pe->opaque = opaque;
2981 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2982 *ppe = pe;
2983 return 0;
2984}
2985
2986void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2987{
2988 PollingEntry **ppe, *pe;
2989 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2990 pe = *ppe;
2991 if (pe->func == func && pe->opaque == opaque) {
2992 *ppe = pe->next;
2993 qemu_free(pe);
2994 break;
2995 }
2996 }
2997}
2998
bellarda18e5242006-06-25 17:18:27 +00002999/***********************************************************/
3000/* Wait objects support */
3001typedef struct WaitObjects {
3002 int num;
3003 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
3004 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
3005 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
3006} WaitObjects;
3007
3008static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00003009
bellarda18e5242006-06-25 17:18:27 +00003010int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3011{
3012 WaitObjects *w = &wait_objects;
3013
3014 if (w->num >= MAXIMUM_WAIT_OBJECTS)
3015 return -1;
3016 w->events[w->num] = handle;
3017 w->func[w->num] = func;
3018 w->opaque[w->num] = opaque;
3019 w->num++;
3020 return 0;
3021}
3022
3023void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3024{
3025 int i, found;
3026 WaitObjects *w = &wait_objects;
3027
3028 found = 0;
3029 for (i = 0; i < w->num; i++) {
3030 if (w->events[i] == handle)
3031 found = 1;
3032 if (found) {
3033 w->events[i] = w->events[i + 1];
3034 w->func[i] = w->func[i + 1];
3035 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00003036 }
bellarda18e5242006-06-25 17:18:27 +00003037 }
3038 if (found)
3039 w->num--;
3040}
3041#endif
3042
bellard8a7ddc32004-03-31 19:00:16 +00003043/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003044/* ram save/restore */
3045
bellard8a7ddc32004-03-31 19:00:16 +00003046static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3047{
3048 int v;
3049
3050 v = qemu_get_byte(f);
3051 switch(v) {
3052 case 0:
3053 if (qemu_get_buffer(f, buf, len) != len)
3054 return -EIO;
3055 break;
3056 case 1:
3057 v = qemu_get_byte(f);
3058 memset(buf, v, len);
3059 break;
3060 default:
3061 return -EINVAL;
3062 }
aliguori871d2f02008-10-13 03:07:56 +00003063
3064 if (qemu_file_has_error(f))
3065 return -EIO;
3066
bellard8a7ddc32004-03-31 19:00:16 +00003067 return 0;
3068}
3069
bellardc88676f2006-08-06 13:36:11 +00003070static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003071{
aurel3200f82b82008-04-27 21:12:55 +00003072 int ret;
3073 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00003074
pbrook94a6b542009-04-11 17:15:54 +00003075 if (qemu_get_be32(f) != last_ram_offset)
bellard8a7ddc32004-03-31 19:00:16 +00003076 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003077 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
pbrook5579c7f2009-04-11 14:47:08 +00003078 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
bellard8a7ddc32004-03-31 19:00:16 +00003079 if (ret)
3080 return ret;
3081 }
3082 return 0;
3083}
3084
bellardc88676f2006-08-06 13:36:11 +00003085#define BDRV_HASH_BLOCK_SIZE 1024
3086#define IOBUF_SIZE 4096
3087#define RAM_CBLOCK_MAGIC 0xfabe
3088
bellardc88676f2006-08-06 13:36:11 +00003089typedef struct RamDecompressState {
3090 z_stream zstream;
3091 QEMUFile *f;
3092 uint8_t buf[IOBUF_SIZE];
3093} RamDecompressState;
3094
3095static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3096{
3097 int ret;
3098 memset(s, 0, sizeof(*s));
3099 s->f = f;
3100 ret = inflateInit(&s->zstream);
3101 if (ret != Z_OK)
3102 return -1;
3103 return 0;
3104}
3105
3106static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3107{
3108 int ret, clen;
3109
3110 s->zstream.avail_out = len;
3111 s->zstream.next_out = buf;
3112 while (s->zstream.avail_out > 0) {
3113 if (s->zstream.avail_in == 0) {
3114 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3115 return -1;
3116 clen = qemu_get_be16(s->f);
3117 if (clen > IOBUF_SIZE)
3118 return -1;
3119 qemu_get_buffer(s->f, s->buf, clen);
3120 s->zstream.avail_in = clen;
3121 s->zstream.next_in = s->buf;
3122 }
3123 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3124 if (ret != Z_OK && ret != Z_STREAM_END) {
3125 return -1;
3126 }
3127 }
3128 return 0;
3129}
3130
3131static void ram_decompress_close(RamDecompressState *s)
3132{
3133 inflateEnd(&s->zstream);
3134}
3135
aliguori475e4272008-10-06 20:21:51 +00003136#define RAM_SAVE_FLAG_FULL 0x01
3137#define RAM_SAVE_FLAG_COMPRESS 0x02
3138#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3139#define RAM_SAVE_FLAG_PAGE 0x08
3140#define RAM_SAVE_FLAG_EOS 0x10
3141
3142static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003143{
aliguori475e4272008-10-06 20:21:51 +00003144 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3145 uint32_t *array = (uint32_t *)page;
3146 int i;
ths3b46e622007-09-17 08:09:54 +00003147
aliguori475e4272008-10-06 20:21:51 +00003148 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3149 if (array[i] != val)
3150 return 0;
bellardc88676f2006-08-06 13:36:11 +00003151 }
aliguori475e4272008-10-06 20:21:51 +00003152
3153 return 1;
bellardc88676f2006-08-06 13:36:11 +00003154}
3155
aliguori475e4272008-10-06 20:21:51 +00003156static int ram_save_block(QEMUFile *f)
3157{
3158 static ram_addr_t current_addr = 0;
3159 ram_addr_t saved_addr = current_addr;
3160 ram_addr_t addr = 0;
3161 int found = 0;
3162
pbrook94a6b542009-04-11 17:15:54 +00003163 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00003164 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00003165 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00003166
3167 cpu_physical_memory_reset_dirty(current_addr,
3168 current_addr + TARGET_PAGE_SIZE,
3169 MIGRATION_DIRTY_FLAG);
3170
pbrook5579c7f2009-04-11 14:47:08 +00003171 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00003172
pbrook5579c7f2009-04-11 14:47:08 +00003173 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00003174 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00003175 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00003176 } else {
3177 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00003178 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003179 }
3180
3181 found = 1;
3182 break;
3183 }
3184 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00003185 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00003186 }
3187
3188 return found;
3189}
3190
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003191static uint64_t bytes_transferred = 0;
aliguori475e4272008-10-06 20:21:51 +00003192
3193static ram_addr_t ram_save_remaining(void)
3194{
3195 ram_addr_t addr;
3196 ram_addr_t count = 0;
3197
pbrook94a6b542009-04-11 17:15:54 +00003198 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003199 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3200 count++;
3201 }
3202
3203 return count;
3204}
3205
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003206uint64_t ram_bytes_remaining(void)
3207{
3208 return ram_save_remaining() * TARGET_PAGE_SIZE;
3209}
3210
3211uint64_t ram_bytes_transferred(void)
3212{
3213 return bytes_transferred;
3214}
3215
3216uint64_t ram_bytes_total(void)
3217{
3218 return last_ram_offset;
3219}
3220
aliguori475e4272008-10-06 20:21:51 +00003221static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3222{
3223 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003224 uint64_t bytes_transferred_last;
3225 double bwidth = 0;
3226 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00003227
Jan Kiszka9fa06382009-05-22 23:51:45 +02003228 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003229 qemu_file_set_error(f);
3230 return 0;
3231 }
3232
aliguori475e4272008-10-06 20:21:51 +00003233 if (stage == 1) {
3234 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00003235 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003236 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3237 cpu_physical_memory_set_dirty(addr);
3238 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003239
aliguori475e4272008-10-06 20:21:51 +00003240 /* Enable dirty memory tracking */
3241 cpu_physical_memory_set_dirty_tracking(1);
3242
pbrook94a6b542009-04-11 17:15:54 +00003243 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003244 }
3245
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003246 bytes_transferred_last = bytes_transferred;
3247 bwidth = get_clock();
3248
aliguori475e4272008-10-06 20:21:51 +00003249 while (!qemu_file_rate_limit(f)) {
3250 int ret;
3251
3252 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003253 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00003254 if (ret == 0) /* no more blocks */
3255 break;
3256 }
3257
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003258 bwidth = get_clock() - bwidth;
3259 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3260
3261 /* if we haven't transferred anything this round, force expected_time to a
3262 * a very high value, but without crashing */
3263 if (bwidth == 0)
3264 bwidth = 0.000001;
3265
aliguori475e4272008-10-06 20:21:51 +00003266 /* try transferring iterative blocks of memory */
3267
3268 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003269
3270 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003271 while (ram_save_block(f) != 0) {
3272 bytes_transferred += TARGET_PAGE_SIZE;
3273 }
aliguori8215e912009-04-05 19:30:55 +00003274 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003275 }
3276
3277 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3278
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003279 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3280
3281 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00003282}
3283
3284static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003285{
3286 RamDecompressState s1, *s = &s1;
3287 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003288 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003289
bellardc88676f2006-08-06 13:36:11 +00003290 if (ram_decompress_open(s, f) < 0)
3291 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003292 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
bellardc88676f2006-08-06 13:36:11 +00003293 if (ram_decompress_buf(s, buf, 1) < 0) {
3294 fprintf(stderr, "Error while reading ram block header\n");
3295 goto error;
3296 }
3297 if (buf[0] == 0) {
pbrook5579c7f2009-04-11 14:47:08 +00003298 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3299 BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003300 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003301 goto error;
3302 }
aliguori475e4272008-10-06 20:21:51 +00003303 } else {
bellardc88676f2006-08-06 13:36:11 +00003304 error:
3305 printf("Error block header\n");
3306 return -EINVAL;
3307 }
3308 }
3309 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003310
3311 return 0;
3312}
3313
3314static int ram_load(QEMUFile *f, void *opaque, int version_id)
3315{
3316 ram_addr_t addr;
3317 int flags;
3318
3319 if (version_id == 1)
3320 return ram_load_v1(f, opaque);
3321
3322 if (version_id == 2) {
pbrook94a6b542009-04-11 17:15:54 +00003323 if (qemu_get_be32(f) != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003324 return -EINVAL;
3325 return ram_load_dead(f, opaque);
3326 }
3327
3328 if (version_id != 3)
3329 return -EINVAL;
3330
3331 do {
3332 addr = qemu_get_be64(f);
3333
3334 flags = addr & ~TARGET_PAGE_MASK;
3335 addr &= TARGET_PAGE_MASK;
3336
3337 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003338 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003339 return -EINVAL;
3340 }
3341
3342 if (flags & RAM_SAVE_FLAG_FULL) {
3343 if (ram_load_dead(f, opaque) < 0)
3344 return -EINVAL;
3345 }
3346
3347 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3348 uint8_t ch = qemu_get_byte(f);
pbrook5579c7f2009-04-11 14:47:08 +00003349 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003350 } else if (flags & RAM_SAVE_FLAG_PAGE)
pbrook5579c7f2009-04-11 14:47:08 +00003351 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003352 } while (!(flags & RAM_SAVE_FLAG_EOS));
3353
bellardc88676f2006-08-06 13:36:11 +00003354 return 0;
3355}
3356
aliguori9e472e12008-10-08 19:50:24 +00003357void qemu_service_io(void)
3358{
aliguorid9f75a42009-04-24 18:03:11 +00003359 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003360}
3361
bellard8a7ddc32004-03-31 19:00:16 +00003362/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003363/* bottom halves (can be seen as timers which expire ASAP) */
3364
3365struct QEMUBH {
3366 QEMUBHFunc *cb;
3367 void *opaque;
3368 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003369 int idle;
3370 int deleted;
bellard83f64092006-08-01 16:21:11 +00003371 QEMUBH *next;
3372};
3373
3374static QEMUBH *first_bh = NULL;
3375
3376QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3377{
3378 QEMUBH *bh;
3379 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003380 bh->cb = cb;
3381 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003382 bh->next = first_bh;
3383 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003384 return bh;
3385}
3386
bellard6eb57332006-08-06 09:51:25 +00003387int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003388{
aliguori1b435b12008-10-31 17:24:21 +00003389 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003390 int ret;
bellard83f64092006-08-01 16:21:11 +00003391
bellard6eb57332006-08-06 09:51:25 +00003392 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003393 for (bh = first_bh; bh; bh = bh->next) {
3394 if (!bh->deleted && bh->scheduled) {
3395 bh->scheduled = 0;
3396 if (!bh->idle)
3397 ret = 1;
3398 bh->idle = 0;
3399 bh->cb(bh->opaque);
3400 }
bellard83f64092006-08-01 16:21:11 +00003401 }
aliguori1b435b12008-10-31 17:24:21 +00003402
3403 /* remove deleted bhs */
3404 bhp = &first_bh;
3405 while (*bhp) {
3406 bh = *bhp;
3407 if (bh->deleted) {
3408 *bhp = bh->next;
3409 qemu_free(bh);
3410 } else
3411 bhp = &bh->next;
3412 }
3413
bellard6eb57332006-08-06 09:51:25 +00003414 return ret;
bellard83f64092006-08-01 16:21:11 +00003415}
3416
aliguori1b435b12008-10-31 17:24:21 +00003417void qemu_bh_schedule_idle(QEMUBH *bh)
3418{
3419 if (bh->scheduled)
3420 return;
3421 bh->scheduled = 1;
3422 bh->idle = 1;
3423}
3424
bellard83f64092006-08-01 16:21:11 +00003425void qemu_bh_schedule(QEMUBH *bh)
3426{
bellard83f64092006-08-01 16:21:11 +00003427 if (bh->scheduled)
3428 return;
3429 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003430 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003431 /* stop the currently executing CPU to execute the BH ASAP */
aliguorid9f75a42009-04-24 18:03:11 +00003432 qemu_notify_event();
bellard83f64092006-08-01 16:21:11 +00003433}
3434
3435void qemu_bh_cancel(QEMUBH *bh)
3436{
aliguori1b435b12008-10-31 17:24:21 +00003437 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003438}
3439
3440void qemu_bh_delete(QEMUBH *bh)
3441{
aliguori1b435b12008-10-31 17:24:21 +00003442 bh->scheduled = 0;
3443 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003444}
3445
aliguori56f3a5d2008-10-31 18:07:17 +00003446static void qemu_bh_update_timeout(int *timeout)
3447{
3448 QEMUBH *bh;
3449
3450 for (bh = first_bh; bh; bh = bh->next) {
3451 if (!bh->deleted && bh->scheduled) {
3452 if (bh->idle) {
3453 /* idle bottom halves will be polled at least
3454 * every 10ms */
3455 *timeout = MIN(10, *timeout);
3456 } else {
3457 /* non-idle bottom halves will be executed
3458 * immediately */
3459 *timeout = 0;
3460 break;
3461 }
3462 }
3463 }
3464}
3465
bellard83f64092006-08-01 16:21:11 +00003466/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003467/* machine registration */
3468
blueswir1bdaf78e2008-10-04 07:24:27 +00003469static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003470QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003471
3472int qemu_register_machine(QEMUMachine *m)
3473{
3474 QEMUMachine **pm;
3475 pm = &first_machine;
3476 while (*pm != NULL)
3477 pm = &(*pm)->next;
3478 m->next = NULL;
3479 *pm = m;
3480 return 0;
3481}
3482
pbrook9596ebb2007-11-18 01:44:38 +00003483static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003484{
3485 QEMUMachine *m;
3486
3487 for(m = first_machine; m != NULL; m = m->next) {
3488 if (!strcmp(m->name, name))
3489 return m;
3490 }
3491 return NULL;
3492}
3493
Anthony Liguori0c257432009-05-21 20:41:01 -05003494static QEMUMachine *find_default_machine(void)
3495{
3496 QEMUMachine *m;
3497
3498 for(m = first_machine; m != NULL; m = m->next) {
3499 if (m->is_default) {
3500 return m;
3501 }
3502 }
3503 return NULL;
3504}
3505
bellardcc1daa42005-06-05 14:49:17 +00003506/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003507/* main execution loop */
3508
pbrook9596ebb2007-11-18 01:44:38 +00003509static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003510{
aliguori7d957bd2009-01-15 22:14:11 +00003511 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003512 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003513 DisplayChangeListener *dcl = ds->listeners;
3514
3515 dpy_refresh(ds);
3516
3517 while (dcl != NULL) {
3518 if (dcl->gui_timer_interval &&
3519 dcl->gui_timer_interval < interval)
3520 interval = dcl->gui_timer_interval;
3521 dcl = dcl->next;
3522 }
3523 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003524}
3525
blueswir19043b622009-01-21 19:28:13 +00003526static void nographic_update(void *opaque)
3527{
3528 uint64_t interval = GUI_REFRESH_INTERVAL;
3529
3530 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3531}
3532
bellard0bd48852005-11-11 00:00:47 +00003533struct vm_change_state_entry {
3534 VMChangeStateHandler *cb;
3535 void *opaque;
3536 LIST_ENTRY (vm_change_state_entry) entries;
3537};
3538
3539static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3540
3541VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3542 void *opaque)
3543{
3544 VMChangeStateEntry *e;
3545
3546 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003547
3548 e->cb = cb;
3549 e->opaque = opaque;
3550 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3551 return e;
3552}
3553
3554void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3555{
3556 LIST_REMOVE (e, entries);
3557 qemu_free (e);
3558}
3559
aliguori9781e042009-01-22 17:15:29 +00003560static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003561{
3562 VMChangeStateEntry *e;
3563
3564 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003565 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003566 }
3567}
3568
aliguorid6dc3d42009-04-24 18:04:07 +00003569static void resume_all_vcpus(void);
3570static void pause_all_vcpus(void);
3571
bellard8a7ddc32004-03-31 19:00:16 +00003572void vm_start(void)
3573{
3574 if (!vm_running) {
3575 cpu_enable_ticks();
3576 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003577 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003578 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003579 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003580 }
3581}
3582
bellardbb0c6722004-06-20 12:37:32 +00003583/* reset/shutdown handler */
3584
3585typedef struct QEMUResetEntry {
3586 QEMUResetHandler *func;
3587 void *opaque;
Jan Kiszka82176062009-05-02 00:29:37 +02003588 int order;
bellardbb0c6722004-06-20 12:37:32 +00003589 struct QEMUResetEntry *next;
3590} QEMUResetEntry;
3591
3592static QEMUResetEntry *first_reset_entry;
3593static int reset_requested;
3594static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003595static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003596static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003597static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003598
aurel32cf7a2fe2008-03-18 06:53:05 +00003599int qemu_shutdown_requested(void)
3600{
3601 int r = shutdown_requested;
3602 shutdown_requested = 0;
3603 return r;
3604}
3605
3606int qemu_reset_requested(void)
3607{
3608 int r = reset_requested;
3609 reset_requested = 0;
3610 return r;
3611}
3612
3613int qemu_powerdown_requested(void)
3614{
3615 int r = powerdown_requested;
3616 powerdown_requested = 0;
3617 return r;
3618}
3619
aliguorie5689022009-04-24 18:03:54 +00003620static int qemu_debug_requested(void)
3621{
3622 int r = debug_requested;
3623 debug_requested = 0;
3624 return r;
3625}
3626
aliguori6e29f5d2009-04-24 18:04:02 +00003627static int qemu_vmstop_requested(void)
3628{
3629 int r = vmstop_requested;
3630 vmstop_requested = 0;
3631 return r;
3632}
3633
3634static void do_vm_stop(int reason)
3635{
3636 if (vm_running) {
3637 cpu_disable_ticks();
3638 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003639 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003640 vm_state_notify(0, reason);
3641 }
3642}
3643
Jan Kiszka82176062009-05-02 00:29:37 +02003644void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003645{
3646 QEMUResetEntry **pre, *re;
3647
3648 pre = &first_reset_entry;
Jan Kiszka82176062009-05-02 00:29:37 +02003649 while (*pre != NULL && (*pre)->order >= order) {
bellardbb0c6722004-06-20 12:37:32 +00003650 pre = &(*pre)->next;
Jan Kiszka82176062009-05-02 00:29:37 +02003651 }
bellardbb0c6722004-06-20 12:37:32 +00003652 re = qemu_mallocz(sizeof(QEMUResetEntry));
3653 re->func = func;
3654 re->opaque = opaque;
Jan Kiszka82176062009-05-02 00:29:37 +02003655 re->order = order;
bellardbb0c6722004-06-20 12:37:32 +00003656 re->next = NULL;
3657 *pre = re;
3658}
3659
aurel32cf7a2fe2008-03-18 06:53:05 +00003660void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00003661{
3662 QEMUResetEntry *re;
3663
3664 /* reset all devices */
3665 for(re = first_reset_entry; re != NULL; re = re->next) {
3666 re->func(re->opaque);
3667 }
3668}
3669
3670void qemu_system_reset_request(void)
3671{
bellardd1beab82006-10-02 19:44:22 +00003672 if (no_reboot) {
3673 shutdown_requested = 1;
3674 } else {
3675 reset_requested = 1;
3676 }
aliguorid9f75a42009-04-24 18:03:11 +00003677 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003678}
3679
3680void qemu_system_shutdown_request(void)
3681{
3682 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003683 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003684}
3685
bellard34751872005-07-02 14:31:34 +00003686void qemu_system_powerdown_request(void)
3687{
3688 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003689 qemu_notify_event();
3690}
3691
aliguorid6dc3d42009-04-24 18:04:07 +00003692#ifdef CONFIG_IOTHREAD
3693static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003694{
aliguorid6dc3d42009-04-24 18:04:07 +00003695 vmstop_requested = reason;
3696 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003697}
aliguorid6dc3d42009-04-24 18:04:07 +00003698#endif
bellardbb0c6722004-06-20 12:37:32 +00003699
aliguori50317c72009-04-24 18:03:29 +00003700#ifndef _WIN32
3701static int io_thread_fd = -1;
3702
3703static void qemu_event_increment(void)
3704{
3705 static const char byte = 0;
3706
3707 if (io_thread_fd == -1)
3708 return;
3709
3710 write(io_thread_fd, &byte, sizeof(byte));
3711}
3712
3713static void qemu_event_read(void *opaque)
3714{
3715 int fd = (unsigned long)opaque;
3716 ssize_t len;
3717
3718 /* Drain the notify pipe */
3719 do {
3720 char buffer[512];
3721 len = read(fd, buffer, sizeof(buffer));
3722 } while ((len == -1 && errno == EINTR) || len > 0);
3723}
3724
3725static int qemu_event_init(void)
3726{
3727 int err;
3728 int fds[2];
3729
3730 err = pipe(fds);
3731 if (err == -1)
3732 return -errno;
3733
3734 err = fcntl_setfl(fds[0], O_NONBLOCK);
3735 if (err < 0)
3736 goto fail;
3737
3738 err = fcntl_setfl(fds[1], O_NONBLOCK);
3739 if (err < 0)
3740 goto fail;
3741
3742 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3743 (void *)(unsigned long)fds[0]);
3744
3745 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003746 return 0;
3747
aliguori50317c72009-04-24 18:03:29 +00003748fail:
3749 close(fds[0]);
3750 close(fds[1]);
3751 return err;
3752}
3753#else
3754HANDLE qemu_event_handle;
3755
3756static void dummy_event_handler(void *opaque)
3757{
3758}
3759
3760static int qemu_event_init(void)
3761{
3762 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3763 if (!qemu_event_handle) {
3764 perror("Failed CreateEvent");
3765 return -1;
3766 }
3767 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3768 return 0;
3769}
3770
3771static void qemu_event_increment(void)
3772{
3773 SetEvent(qemu_event_handle);
3774}
3775#endif
3776
aliguorid6dc3d42009-04-24 18:04:07 +00003777static int cpu_can_run(CPUState *env)
3778{
3779 if (env->stop)
3780 return 0;
3781 if (env->stopped)
3782 return 0;
3783 return 1;
3784}
3785
3786#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003787static int qemu_init_main_loop(void)
3788{
aliguori50317c72009-04-24 18:03:29 +00003789 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003790}
3791
aliguori0bf46a42009-04-24 18:03:41 +00003792void qemu_init_vcpu(void *_env)
3793{
3794 CPUState *env = _env;
3795
3796 if (kvm_enabled())
3797 kvm_init_vcpu(env);
3798 return;
3799}
3800
aliguori8edac962009-04-24 18:03:45 +00003801int qemu_cpu_self(void *env)
3802{
3803 return 1;
3804}
3805
aliguorid6dc3d42009-04-24 18:04:07 +00003806static void resume_all_vcpus(void)
3807{
3808}
3809
3810static void pause_all_vcpus(void)
3811{
3812}
3813
aliguori8edac962009-04-24 18:03:45 +00003814void qemu_cpu_kick(void *env)
3815{
3816 return;
3817}
3818
aliguorid6dc3d42009-04-24 18:04:07 +00003819void qemu_notify_event(void)
3820{
3821 CPUState *env = cpu_single_env;
3822
3823 if (env) {
3824 cpu_exit(env);
3825#ifdef USE_KQEMU
3826 if (env->kqemu_enabled)
3827 kqemu_cpu_interrupt(env);
3828#endif
3829 }
3830}
3831
aliguori48708522009-04-24 18:03:49 +00003832#define qemu_mutex_lock_iothread() do { } while (0)
3833#define qemu_mutex_unlock_iothread() do { } while (0)
3834
aliguori6e29f5d2009-04-24 18:04:02 +00003835void vm_stop(int reason)
3836{
3837 do_vm_stop(reason);
3838}
3839
aliguorid6dc3d42009-04-24 18:04:07 +00003840#else /* CONFIG_IOTHREAD */
3841
3842#include "qemu-thread.h"
3843
3844QemuMutex qemu_global_mutex;
3845static QemuMutex qemu_fair_mutex;
3846
3847static QemuThread io_thread;
3848
3849static QemuThread *tcg_cpu_thread;
3850static QemuCond *tcg_halt_cond;
3851
3852static int qemu_system_ready;
3853/* cpu creation */
3854static QemuCond qemu_cpu_cond;
3855/* system init */
3856static QemuCond qemu_system_cond;
3857static QemuCond qemu_pause_cond;
3858
3859static void block_io_signals(void);
3860static void unblock_io_signals(void);
3861static int tcg_has_work(void);
3862
3863static int qemu_init_main_loop(void)
3864{
3865 int ret;
3866
3867 ret = qemu_event_init();
3868 if (ret)
3869 return ret;
3870
3871 qemu_cond_init(&qemu_pause_cond);
3872 qemu_mutex_init(&qemu_fair_mutex);
3873 qemu_mutex_init(&qemu_global_mutex);
3874 qemu_mutex_lock(&qemu_global_mutex);
3875
3876 unblock_io_signals();
3877 qemu_thread_self(&io_thread);
3878
3879 return 0;
3880}
3881
3882static void qemu_wait_io_event(CPUState *env)
3883{
3884 while (!tcg_has_work())
3885 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3886
3887 qemu_mutex_unlock(&qemu_global_mutex);
3888
3889 /*
3890 * Users of qemu_global_mutex can be starved, having no chance
3891 * to acquire it since this path will get to it first.
3892 * So use another lock to provide fairness.
3893 */
3894 qemu_mutex_lock(&qemu_fair_mutex);
3895 qemu_mutex_unlock(&qemu_fair_mutex);
3896
3897 qemu_mutex_lock(&qemu_global_mutex);
3898 if (env->stop) {
3899 env->stop = 0;
3900 env->stopped = 1;
3901 qemu_cond_signal(&qemu_pause_cond);
3902 }
3903}
3904
3905static int qemu_cpu_exec(CPUState *env);
3906
3907static void *kvm_cpu_thread_fn(void *arg)
3908{
3909 CPUState *env = arg;
3910
3911 block_io_signals();
3912 qemu_thread_self(env->thread);
3913
3914 /* signal CPU creation */
3915 qemu_mutex_lock(&qemu_global_mutex);
3916 env->created = 1;
3917 qemu_cond_signal(&qemu_cpu_cond);
3918
3919 /* and wait for machine initialization */
3920 while (!qemu_system_ready)
3921 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3922
3923 while (1) {
3924 if (cpu_can_run(env))
3925 qemu_cpu_exec(env);
3926 qemu_wait_io_event(env);
3927 }
3928
3929 return NULL;
3930}
3931
3932static void tcg_cpu_exec(void);
3933
3934static void *tcg_cpu_thread_fn(void *arg)
3935{
3936 CPUState *env = arg;
3937
3938 block_io_signals();
3939 qemu_thread_self(env->thread);
3940
3941 /* signal CPU creation */
3942 qemu_mutex_lock(&qemu_global_mutex);
3943 for (env = first_cpu; env != NULL; env = env->next_cpu)
3944 env->created = 1;
3945 qemu_cond_signal(&qemu_cpu_cond);
3946
3947 /* and wait for machine initialization */
3948 while (!qemu_system_ready)
3949 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3950
3951 while (1) {
3952 tcg_cpu_exec();
3953 qemu_wait_io_event(cur_cpu);
3954 }
3955
3956 return NULL;
3957}
3958
3959void qemu_cpu_kick(void *_env)
3960{
3961 CPUState *env = _env;
3962 qemu_cond_broadcast(env->halt_cond);
3963 if (kvm_enabled())
3964 qemu_thread_signal(env->thread, SIGUSR1);
3965}
3966
3967int qemu_cpu_self(void *env)
3968{
3969 return (cpu_single_env != NULL);
3970}
3971
3972static void cpu_signal(int sig)
3973{
3974 if (cpu_single_env)
3975 cpu_exit(cpu_single_env);
3976}
3977
3978static void block_io_signals(void)
3979{
3980 sigset_t set;
3981 struct sigaction sigact;
3982
3983 sigemptyset(&set);
3984 sigaddset(&set, SIGUSR2);
3985 sigaddset(&set, SIGIO);
3986 sigaddset(&set, SIGALRM);
3987 pthread_sigmask(SIG_BLOCK, &set, NULL);
3988
3989 sigemptyset(&set);
3990 sigaddset(&set, SIGUSR1);
3991 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3992
3993 memset(&sigact, 0, sizeof(sigact));
3994 sigact.sa_handler = cpu_signal;
3995 sigaction(SIGUSR1, &sigact, NULL);
3996}
3997
3998static void unblock_io_signals(void)
3999{
4000 sigset_t set;
4001
4002 sigemptyset(&set);
4003 sigaddset(&set, SIGUSR2);
4004 sigaddset(&set, SIGIO);
4005 sigaddset(&set, SIGALRM);
4006 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
4007
4008 sigemptyset(&set);
4009 sigaddset(&set, SIGUSR1);
4010 pthread_sigmask(SIG_BLOCK, &set, NULL);
4011}
4012
4013static void qemu_signal_lock(unsigned int msecs)
4014{
4015 qemu_mutex_lock(&qemu_fair_mutex);
4016
4017 while (qemu_mutex_trylock(&qemu_global_mutex)) {
4018 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
4019 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
4020 break;
4021 }
4022 qemu_mutex_unlock(&qemu_fair_mutex);
4023}
4024
4025static void qemu_mutex_lock_iothread(void)
4026{
4027 if (kvm_enabled()) {
4028 qemu_mutex_lock(&qemu_fair_mutex);
4029 qemu_mutex_lock(&qemu_global_mutex);
4030 qemu_mutex_unlock(&qemu_fair_mutex);
4031 } else
4032 qemu_signal_lock(100);
4033}
4034
4035static void qemu_mutex_unlock_iothread(void)
4036{
4037 qemu_mutex_unlock(&qemu_global_mutex);
4038}
4039
4040static int all_vcpus_paused(void)
4041{
4042 CPUState *penv = first_cpu;
4043
4044 while (penv) {
4045 if (!penv->stopped)
4046 return 0;
4047 penv = (CPUState *)penv->next_cpu;
4048 }
4049
4050 return 1;
4051}
4052
4053static void pause_all_vcpus(void)
4054{
4055 CPUState *penv = first_cpu;
4056
4057 while (penv) {
4058 penv->stop = 1;
4059 qemu_thread_signal(penv->thread, SIGUSR1);
4060 qemu_cpu_kick(penv);
4061 penv = (CPUState *)penv->next_cpu;
4062 }
4063
4064 while (!all_vcpus_paused()) {
4065 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
4066 penv = first_cpu;
4067 while (penv) {
4068 qemu_thread_signal(penv->thread, SIGUSR1);
4069 penv = (CPUState *)penv->next_cpu;
4070 }
4071 }
4072}
4073
4074static void resume_all_vcpus(void)
4075{
4076 CPUState *penv = first_cpu;
4077
4078 while (penv) {
4079 penv->stop = 0;
4080 penv->stopped = 0;
4081 qemu_thread_signal(penv->thread, SIGUSR1);
4082 qemu_cpu_kick(penv);
4083 penv = (CPUState *)penv->next_cpu;
4084 }
4085}
4086
4087static void tcg_init_vcpu(void *_env)
4088{
4089 CPUState *env = _env;
4090 /* share a single thread for all cpus with TCG */
4091 if (!tcg_cpu_thread) {
4092 env->thread = qemu_mallocz(sizeof(QemuThread));
4093 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
4094 qemu_cond_init(env->halt_cond);
4095 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
4096 while (env->created == 0)
4097 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
4098 tcg_cpu_thread = env->thread;
4099 tcg_halt_cond = env->halt_cond;
4100 } else {
4101 env->thread = tcg_cpu_thread;
4102 env->halt_cond = tcg_halt_cond;
4103 }
4104}
4105
4106static void kvm_start_vcpu(CPUState *env)
4107{
4108 kvm_init_vcpu(env);
4109 env->thread = qemu_mallocz(sizeof(QemuThread));
4110 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
4111 qemu_cond_init(env->halt_cond);
4112 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
4113 while (env->created == 0)
4114 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
4115}
4116
4117void qemu_init_vcpu(void *_env)
4118{
4119 CPUState *env = _env;
4120
4121 if (kvm_enabled())
4122 kvm_start_vcpu(env);
4123 else
4124 tcg_init_vcpu(env);
4125}
4126
4127void qemu_notify_event(void)
4128{
4129 qemu_event_increment();
4130}
4131
4132void vm_stop(int reason)
4133{
4134 QemuThread me;
4135 qemu_thread_self(&me);
4136
4137 if (!qemu_thread_equal(&me, &io_thread)) {
4138 qemu_system_vmstop_request(reason);
4139 /*
4140 * FIXME: should not return to device code in case
4141 * vm_stop() has been requested.
4142 */
4143 if (cpu_single_env) {
4144 cpu_exit(cpu_single_env);
4145 cpu_single_env->stop = 1;
4146 }
4147 return;
4148 }
4149 do_vm_stop(reason);
4150}
4151
4152#endif
4153
4154
ths877cf882007-04-18 18:11:47 +00004155#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00004156static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00004157{
4158 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00004159 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00004160
bellardf3311102006-04-12 20:21:17 +00004161
4162 /* XXX: need to suppress polling by better using win32 events */
4163 ret = 0;
4164 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
4165 ret |= pe->func(pe->opaque);
4166 }
thse6b1e552007-04-18 17:56:02 +00004167 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00004168 int err;
4169 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00004170
aliguori56f3a5d2008-10-31 18:07:17 +00004171 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00004172 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
4173 if (w->func[ret - WAIT_OBJECT_0])
4174 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00004175
ths5fafdf22007-09-16 21:08:06 +00004176 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00004177 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00004178
thse6b1e552007-04-18 17:56:02 +00004179 /* Check if event is signaled */
4180 ret2 = WaitForSingleObject(w->events[i], 0);
4181 if(ret2 == WAIT_OBJECT_0) {
4182 if (w->func[i])
4183 w->func[i](w->opaque[i]);
4184 } else if (ret2 == WAIT_TIMEOUT) {
4185 } else {
4186 err = GetLastError();
4187 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00004188 }
4189 }
bellarda18e5242006-06-25 17:18:27 +00004190 } else if (ret == WAIT_TIMEOUT) {
4191 } else {
4192 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00004193 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00004194 }
bellardf3311102006-04-12 20:21:17 +00004195 }
aliguori56f3a5d2008-10-31 18:07:17 +00004196
4197 *timeout = 0;
4198}
4199#else
blueswir169d64512008-12-07 19:30:18 +00004200static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00004201{
4202}
bellardfd1dff42006-02-01 21:29:26 +00004203#endif
aliguori56f3a5d2008-10-31 18:07:17 +00004204
4205void main_loop_wait(int timeout)
4206{
4207 IOHandlerRecord *ioh;
4208 fd_set rfds, wfds, xfds;
4209 int ret, nfds;
4210 struct timeval tv;
4211
4212 qemu_bh_update_timeout(&timeout);
4213
4214 host_main_loop_wait(&timeout);
4215
bellardfd1dff42006-02-01 21:29:26 +00004216 /* poll any events */
4217 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00004218 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00004219 FD_ZERO(&rfds);
4220 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00004221 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00004222 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00004223 if (ioh->deleted)
4224 continue;
bellardfd1dff42006-02-01 21:29:26 +00004225 if (ioh->fd_read &&
4226 (!ioh->fd_read_poll ||
4227 ioh->fd_read_poll(ioh->opaque) != 0)) {
4228 FD_SET(ioh->fd, &rfds);
4229 if (ioh->fd > nfds)
4230 nfds = ioh->fd;
4231 }
4232 if (ioh->fd_write) {
4233 FD_SET(ioh->fd, &wfds);
4234 if (ioh->fd > nfds)
4235 nfds = ioh->fd;
4236 }
4237 }
ths3b46e622007-09-17 08:09:54 +00004238
aliguori56f3a5d2008-10-31 18:07:17 +00004239 tv.tv_sec = timeout / 1000;
4240 tv.tv_usec = (timeout % 1000) * 1000;
4241
bellarde0356492006-05-01 13:33:02 +00004242#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00004243 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00004244 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
4245 }
4246#endif
aliguori48708522009-04-24 18:03:49 +00004247 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00004248 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00004249 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00004250 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00004251 IOHandlerRecord **pioh;
4252
4253 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00004254 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004255 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00004256 }
ths6ab43fd2007-08-25 01:34:19 +00004257 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004258 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00004259 }
4260 }
thscafffd42007-02-28 21:59:44 +00004261
4262 /* remove deleted IO handlers */
4263 pioh = &first_io_handler;
4264 while (*pioh) {
4265 ioh = *pioh;
4266 if (ioh->deleted) {
4267 *pioh = ioh->next;
4268 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00004269 } else
thscafffd42007-02-28 21:59:44 +00004270 pioh = &ioh->next;
4271 }
bellardfd1dff42006-02-01 21:29:26 +00004272 }
bellarde0356492006-05-01 13:33:02 +00004273#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00004274 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00004275 if (ret < 0) {
4276 FD_ZERO(&rfds);
4277 FD_ZERO(&wfds);
4278 FD_ZERO(&xfds);
4279 }
4280 slirp_select_poll(&rfds, &wfds, &xfds);
4281 }
4282#endif
bellardc20709a2004-04-21 23:27:19 +00004283
aliguori50317c72009-04-24 18:03:29 +00004284 /* rearm timer, if not periodic */
4285 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
4286 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
4287 qemu_rearm_alarm_timer(alarm_timer);
4288 }
4289
aliguori357c6922008-11-25 17:26:09 +00004290 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00004291 if (vm_running) {
4292 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
4293 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
4294 qemu_get_clock(vm_clock));
4295 }
aliguori357c6922008-11-25 17:26:09 +00004296
4297 /* real time timers */
4298 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
4299 qemu_get_clock(rt_clock));
4300
pbrook423f0742007-05-23 00:06:54 +00004301 /* Check bottom-halves last in case any of the earlier events triggered
4302 them. */
4303 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00004304
bellard5905b2e2004-08-01 21:53:26 +00004305}
4306
aliguori43b96852009-04-24 18:03:33 +00004307static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00004308{
aliguori43b96852009-04-24 18:03:33 +00004309 int ret;
bellard89bfc102006-02-08 22:46:31 +00004310#ifdef CONFIG_PROFILER
4311 int64_t ti;
4312#endif
aliguori43b96852009-04-24 18:03:33 +00004313
4314#ifdef CONFIG_PROFILER
4315 ti = profile_getclock();
4316#endif
4317 if (use_icount) {
4318 int64_t count;
4319 int decr;
4320 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4321 env->icount_decr.u16.low = 0;
4322 env->icount_extra = 0;
4323 count = qemu_next_deadline();
4324 count = (count + (1 << icount_time_shift) - 1)
4325 >> icount_time_shift;
4326 qemu_icount += count;
4327 decr = (count > 0xffff) ? 0xffff : count;
4328 count -= decr;
4329 env->icount_decr.u16.low = decr;
4330 env->icount_extra = count;
4331 }
4332 ret = cpu_exec(env);
4333#ifdef CONFIG_PROFILER
4334 qemu_time += profile_getclock() - ti;
4335#endif
4336 if (use_icount) {
4337 /* Fold pending instructions back into the
4338 instruction counter, and clear the interrupt flag. */
4339 qemu_icount -= (env->icount_decr.u16.low
4340 + env->icount_extra);
4341 env->icount_decr.u32 = 0;
4342 env->icount_extra = 0;
4343 }
4344 return ret;
4345}
4346
aliguorie6e35b12009-04-24 18:03:57 +00004347static void tcg_cpu_exec(void)
4348{
aliguorid6dc3d42009-04-24 18:04:07 +00004349 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00004350
4351 if (next_cpu == NULL)
4352 next_cpu = first_cpu;
4353 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4354 CPUState *env = cur_cpu = next_cpu;
4355
4356 if (!vm_running)
4357 break;
4358 if (timer_alarm_pending) {
4359 timer_alarm_pending = 0;
4360 break;
4361 }
aliguorid6dc3d42009-04-24 18:04:07 +00004362 if (cpu_can_run(env))
4363 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00004364 if (ret == EXCP_DEBUG) {
4365 gdb_set_stop_cpu(env);
4366 debug_requested = 1;
4367 break;
4368 }
4369 }
4370}
4371
aliguori43b96852009-04-24 18:03:33 +00004372static int cpu_has_work(CPUState *env)
4373{
aliguorid6dc3d42009-04-24 18:04:07 +00004374 if (env->stop)
4375 return 1;
4376 if (env->stopped)
4377 return 0;
aliguori43b96852009-04-24 18:03:33 +00004378 if (!env->halted)
4379 return 1;
4380 if (qemu_cpu_has_work(env))
4381 return 1;
4382 return 0;
4383}
4384
4385static int tcg_has_work(void)
4386{
bellard6a00d602005-11-21 23:25:50 +00004387 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004388
aliguori43b96852009-04-24 18:03:33 +00004389 for (env = first_cpu; env != NULL; env = env->next_cpu)
4390 if (cpu_has_work(env))
4391 return 1;
4392 return 0;
4393}
bellard15a76442005-11-23 21:01:03 +00004394
aliguori43b96852009-04-24 18:03:33 +00004395static int qemu_calculate_timeout(void)
4396{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004397#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004398 int timeout;
bellard15a76442005-11-23 21:01:03 +00004399
aliguori43b96852009-04-24 18:03:33 +00004400 if (!vm_running)
4401 timeout = 5000;
4402 else if (tcg_has_work())
4403 timeout = 0;
4404 else if (!use_icount)
4405 timeout = 5000;
4406 else {
4407 /* XXX: use timeout computed from timers */
4408 int64_t add;
4409 int64_t delta;
4410 /* Advance virtual time to the next event. */
4411 if (use_icount == 1) {
4412 /* When not using an adaptive execution frequency
4413 we tend to get badly out of sync with real time,
4414 so just delay for a reasonable amount of time. */
4415 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004416 } else {
aliguori43b96852009-04-24 18:03:33 +00004417 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004418 }
aliguori43b96852009-04-24 18:03:33 +00004419 if (delta > 0) {
4420 /* If virtual time is ahead of real time then just
4421 wait for IO. */
4422 timeout = (delta / 1000000) + 1;
4423 } else {
4424 /* Wait for either IO to occur or the next
4425 timer event. */
4426 add = qemu_next_deadline();
4427 /* We advance the timer before checking for IO.
4428 Limit the amount we advance so that early IO
4429 activity won't get the guest too far ahead. */
4430 if (add > 10000000)
4431 add = 10000000;
4432 delta += add;
4433 add = (add + (1 << icount_time_shift) - 1)
4434 >> icount_time_shift;
4435 qemu_icount += add;
4436 timeout = delta / 1000000;
4437 if (timeout < 0)
4438 timeout = 0;
4439 }
bellardb4608c02003-06-27 17:34:32 +00004440 }
aliguori43b96852009-04-24 18:03:33 +00004441
4442 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004443#else /* CONFIG_IOTHREAD */
4444 return 1000;
4445#endif
aliguori43b96852009-04-24 18:03:33 +00004446}
4447
4448static int vm_can_run(void)
4449{
4450 if (powerdown_requested)
4451 return 0;
4452 if (reset_requested)
4453 return 0;
4454 if (shutdown_requested)
4455 return 0;
aliguorie5689022009-04-24 18:03:54 +00004456 if (debug_requested)
4457 return 0;
aliguori43b96852009-04-24 18:03:33 +00004458 return 1;
4459}
4460
4461static void main_loop(void)
4462{
aliguori6e29f5d2009-04-24 18:04:02 +00004463 int r;
aliguori43b96852009-04-24 18:03:33 +00004464
aliguorid6dc3d42009-04-24 18:04:07 +00004465#ifdef CONFIG_IOTHREAD
4466 qemu_system_ready = 1;
4467 qemu_cond_broadcast(&qemu_system_cond);
4468#endif
4469
aliguori6e29f5d2009-04-24 18:04:02 +00004470 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004471 do {
4472#ifdef CONFIG_PROFILER
4473 int64_t ti;
4474#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004475#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004476 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004477#endif
aliguori43b96852009-04-24 18:03:33 +00004478#ifdef CONFIG_PROFILER
4479 ti = profile_getclock();
4480#endif
4481 main_loop_wait(qemu_calculate_timeout());
4482#ifdef CONFIG_PROFILER
4483 dev_time += profile_getclock() - ti;
4484#endif
aliguorie5689022009-04-24 18:03:54 +00004485 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004486
aliguorie5689022009-04-24 18:03:54 +00004487 if (qemu_debug_requested())
aliguori43b96852009-04-24 18:03:33 +00004488 vm_stop(EXCP_DEBUG);
aliguori43b96852009-04-24 18:03:33 +00004489 if (qemu_shutdown_requested()) {
4490 if (no_shutdown) {
4491 vm_stop(0);
4492 no_shutdown = 0;
4493 } else
4494 break;
4495 }
aliguorid6dc3d42009-04-24 18:04:07 +00004496 if (qemu_reset_requested()) {
4497 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004498 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004499 resume_all_vcpus();
4500 }
aliguori43b96852009-04-24 18:03:33 +00004501 if (qemu_powerdown_requested())
4502 qemu_system_powerdown();
aliguori6e29f5d2009-04-24 18:04:02 +00004503 if ((r = qemu_vmstop_requested()))
4504 vm_stop(r);
aliguori43b96852009-04-24 18:03:33 +00004505 }
aliguorid6dc3d42009-04-24 18:04:07 +00004506 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004507}
4508
pbrook9bd7e6d2009-04-07 22:58:45 +00004509static void version(void)
4510{
pbrook4a19f1e2009-04-07 23:17:49 +00004511 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004512}
4513
ths15f82202007-06-29 23:26:08 +00004514static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004515{
pbrook9bd7e6d2009-04-07 22:58:45 +00004516 version();
4517 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004518 "\n"
bellarda20dd502003-09-30 21:07:02 +00004519 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004520 "\n"
blueswir15824d652009-03-28 06:44:27 +00004521#define DEF(option, opt_arg, opt_enum, opt_help) \
4522 opt_help
4523#define DEFHEADING(text) stringify(text) "\n"
4524#include "qemu-options.h"
4525#undef DEF
4526#undef DEFHEADING
4527#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004528 "\n"
bellard82c643f2004-07-14 17:28:13 +00004529 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004530 "ctrl-alt-f toggle full screen\n"
4531 "ctrl-alt-n switch to virtual console 'n'\n"
4532 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004533 "\n"
4534 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4535 ,
bellard0db63472003-10-27 21:37:46 +00004536 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004537 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004538#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004539 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004540 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004541#endif
bellard6e44ba72004-01-18 21:56:49 +00004542 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004543 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004544 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004545}
4546
bellardcd6f1162004-05-13 22:02:20 +00004547#define HAS_ARG 0x0001
4548
4549enum {
blueswir15824d652009-03-28 06:44:27 +00004550#define DEF(option, opt_arg, opt_enum, opt_help) \
4551 opt_enum,
4552#define DEFHEADING(text)
4553#include "qemu-options.h"
4554#undef DEF
4555#undef DEFHEADING
4556#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004557};
4558
4559typedef struct QEMUOption {
4560 const char *name;
4561 int flags;
4562 int index;
4563} QEMUOption;
4564
blueswir1dbed7e42008-10-01 19:38:09 +00004565static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004566 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004567#define DEF(option, opt_arg, opt_enum, opt_help) \
4568 { option, opt_arg, opt_enum },
4569#define DEFHEADING(text)
4570#include "qemu-options.h"
4571#undef DEF
4572#undef DEFHEADING
4573#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004574 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004575};
4576
bellard1d14ffa2005-10-30 18:58:22 +00004577#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004578struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004579#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004580#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004581 {
4582 "pcspk",
4583 "PC speaker",
4584 0,
4585 1,
4586 { .init_isa = pcspk_audio_init }
4587 },
4588#endif
malc4c9b53e2009-01-09 10:46:34 +00004589
4590#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004591 {
4592 "sb16",
4593 "Creative Sound Blaster 16",
4594 0,
4595 1,
4596 { .init_isa = SB16_init }
4597 },
malc4c9b53e2009-01-09 10:46:34 +00004598#endif
bellard6a36d842005-12-18 20:34:32 +00004599
malccc53d262008-06-13 10:48:22 +00004600#ifdef CONFIG_CS4231A
4601 {
4602 "cs4231a",
4603 "CS4231A",
4604 0,
4605 1,
4606 { .init_isa = cs4231a_init }
4607 },
4608#endif
4609
bellard6a36d842005-12-18 20:34:32 +00004610#ifdef CONFIG_ADLIB
4611 {
4612 "adlib",
4613#ifdef HAS_YMF262
4614 "Yamaha YMF262 (OPL3)",
4615#else
4616 "Yamaha YM3812 (OPL2)",
4617#endif
4618 0,
4619 1,
4620 { .init_isa = Adlib_init }
4621 },
4622#endif
4623
4624#ifdef CONFIG_GUS
4625 {
4626 "gus",
4627 "Gravis Ultrasound GF1",
4628 0,
4629 1,
4630 { .init_isa = GUS_init }
4631 },
4632#endif
4633
malc4c9b53e2009-01-09 10:46:34 +00004634#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004635 {
4636 "ac97",
4637 "Intel 82801AA AC97 Audio",
4638 0,
4639 0,
4640 { .init_pci = ac97_init }
4641 },
malc4c9b53e2009-01-09 10:46:34 +00004642#endif
balroge5c9a132008-01-14 04:27:55 +00004643
malc4c9b53e2009-01-09 10:46:34 +00004644#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004645 {
4646 "es1370",
4647 "ENSONIQ AudioPCI ES1370",
4648 0,
4649 0,
4650 { .init_pci = es1370_init }
4651 },
balrogb00052e2007-04-30 02:22:06 +00004652#endif
bellard6a36d842005-12-18 20:34:32 +00004653
malc4c9b53e2009-01-09 10:46:34 +00004654#endif /* HAS_AUDIO_CHOICE */
4655
bellard6a36d842005-12-18 20:34:32 +00004656 { NULL, NULL, 0, 0, { NULL } }
4657};
4658
bellard1d14ffa2005-10-30 18:58:22 +00004659static void select_soundhw (const char *optarg)
4660{
bellard6a36d842005-12-18 20:34:32 +00004661 struct soundhw *c;
4662
bellard1d14ffa2005-10-30 18:58:22 +00004663 if (*optarg == '?') {
4664 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004665
bellard1d14ffa2005-10-30 18:58:22 +00004666 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004667 for (c = soundhw; c->name; ++c) {
4668 printf ("%-11s %s\n", c->name, c->descr);
4669 }
4670 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004671 exit (*optarg != '?');
4672 }
4673 else {
bellard6a36d842005-12-18 20:34:32 +00004674 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004675 const char *p;
4676 char *e;
4677 int bad_card = 0;
4678
bellard6a36d842005-12-18 20:34:32 +00004679 if (!strcmp (optarg, "all")) {
4680 for (c = soundhw; c->name; ++c) {
4681 c->enabled = 1;
4682 }
4683 return;
4684 }
bellard1d14ffa2005-10-30 18:58:22 +00004685
bellard6a36d842005-12-18 20:34:32 +00004686 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004687 while (*p) {
4688 e = strchr (p, ',');
4689 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004690
4691 for (c = soundhw; c->name; ++c) {
4692 if (!strncmp (c->name, p, l)) {
4693 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004694 break;
4695 }
4696 }
bellard6a36d842005-12-18 20:34:32 +00004697
4698 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004699 if (l > 80) {
4700 fprintf (stderr,
4701 "Unknown sound card name (too big to show)\n");
4702 }
4703 else {
4704 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4705 (int) l, p);
4706 }
4707 bad_card = 1;
4708 }
4709 p += l + (e != NULL);
4710 }
4711
4712 if (bad_card)
4713 goto show_valid_cards;
4714 }
4715}
4716#endif
4717
malc3893c122008-09-28 00:42:05 +00004718static void select_vgahw (const char *p)
4719{
4720 const char *opts;
4721
aliguori28b85ed2009-04-22 15:19:48 +00004722 cirrus_vga_enabled = 0;
4723 std_vga_enabled = 0;
4724 vmsvga_enabled = 0;
aliguori94909d92009-04-22 15:19:53 +00004725 xenfb_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004726 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004727 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004728 } else if (strstart(p, "cirrus", &opts)) {
4729 cirrus_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004730 } else if (strstart(p, "vmware", &opts)) {
malc3893c122008-09-28 00:42:05 +00004731 vmsvga_enabled = 1;
aliguori94909d92009-04-22 15:19:53 +00004732 } else if (strstart(p, "xenfb", &opts)) {
4733 xenfb_enabled = 1;
aliguori28b85ed2009-04-22 15:19:48 +00004734 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004735 invalid_vga:
4736 fprintf(stderr, "Unknown vga type: %s\n", p);
4737 exit(1);
4738 }
malccb5a7aa2008-09-28 00:42:12 +00004739 while (*opts) {
4740 const char *nextopt;
4741
4742 if (strstart(opts, ",retrace=", &nextopt)) {
4743 opts = nextopt;
4744 if (strstart(opts, "dumb", &nextopt))
4745 vga_retrace_method = VGA_RETRACE_DUMB;
4746 else if (strstart(opts, "precise", &nextopt))
4747 vga_retrace_method = VGA_RETRACE_PRECISE;
4748 else goto invalid_vga;
4749 } else goto invalid_vga;
4750 opts = nextopt;
4751 }
malc3893c122008-09-28 00:42:05 +00004752}
4753
bellard3587d7e2006-06-26 20:03:44 +00004754#ifdef _WIN32
4755static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4756{
4757 exit(STATUS_CONTROL_C_EXIT);
4758 return TRUE;
4759}
4760#endif
4761
aliguoric4be29f2009-04-17 18:58:14 +00004762int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004763{
4764 int ret;
4765
4766 if(strlen(str) != 36)
4767 return -1;
4768
4769 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4770 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4771 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4772
4773 if(ret != 16)
4774 return -1;
4775
aliguorib6f6e3d2009-04-17 18:59:56 +00004776#ifdef TARGET_I386
4777 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4778#endif
4779
blueswir18fcb1b92008-09-18 18:29:08 +00004780 return 0;
4781}
4782
bellard7c9d8e02005-11-15 22:16:05 +00004783#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004784
aliguori5b08fc12008-08-21 20:08:03 +00004785#ifndef _WIN32
4786
4787static void termsig_handler(int signal)
4788{
4789 qemu_system_shutdown_request();
4790}
4791
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004792static void sigchld_handler(int signal)
4793{
4794 waitpid(-1, NULL, WNOHANG);
4795}
4796
4797static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004798{
4799 struct sigaction act;
4800
4801 memset(&act, 0, sizeof(act));
4802 act.sa_handler = termsig_handler;
4803 sigaction(SIGINT, &act, NULL);
4804 sigaction(SIGHUP, &act, NULL);
4805 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004806
4807 act.sa_handler = sigchld_handler;
4808 act.sa_flags = SA_NOCLDSTOP;
4809 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004810}
4811
4812#endif
4813
Paul Brook5cea8592009-05-30 00:52:44 +01004814#ifdef _WIN32
4815/* Look for support files in the same directory as the executable. */
4816static char *find_datadir(const char *argv0)
4817{
4818 char *p;
4819 char buf[MAX_PATH];
4820 DWORD len;
4821
4822 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4823 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004824 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004825 }
4826
4827 buf[len] = 0;
4828 p = buf + len - 1;
4829 while (p != buf && *p != '\\')
4830 p--;
4831 *p = 0;
4832 if (access(buf, R_OK) == 0) {
4833 return qemu_strdup(buf);
4834 }
4835 return NULL;
4836}
4837#else /* !_WIN32 */
4838
4839/* Find a likely location for support files using the location of the binary.
4840 For installed binaries this will be "$bindir/../share/qemu". When
4841 running from the build tree this will be "$bindir/../pc-bios". */
4842#define SHARE_SUFFIX "/share/qemu"
4843#define BUILD_SUFFIX "/pc-bios"
4844static char *find_datadir(const char *argv0)
4845{
4846 char *dir;
4847 char *p = NULL;
4848 char *res;
4849#ifdef PATH_MAX
4850 char buf[PATH_MAX];
4851#endif
Blue Swirl3a417592009-06-09 19:12:21 +00004852 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004853
4854#if defined(__linux__)
4855 {
4856 int len;
4857 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4858 if (len > 0) {
4859 buf[len] = 0;
4860 p = buf;
4861 }
4862 }
4863#elif defined(__FreeBSD__)
4864 {
4865 int len;
4866 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4867 if (len > 0) {
4868 buf[len] = 0;
4869 p = buf;
4870 }
4871 }
4872#endif
4873 /* If we don't have any way of figuring out the actual executable
4874 location then try argv[0]. */
4875 if (!p) {
4876#ifdef PATH_MAX
4877 p = buf;
4878#endif
4879 p = realpath(argv0, p);
4880 if (!p) {
4881 return NULL;
4882 }
4883 }
4884 dir = dirname(p);
4885 dir = dirname(dir);
4886
Blue Swirl3a417592009-06-09 19:12:21 +00004887 max_len = strlen(dir) +
4888 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4889 res = qemu_mallocz(max_len);
4890 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004891 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004892 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004893 if (access(res, R_OK)) {
4894 qemu_free(res);
4895 res = NULL;
4896 }
4897 }
4898#ifndef PATH_MAX
4899 free(p);
4900#endif
4901 return res;
4902}
4903#undef SHARE_SUFFIX
4904#undef BUILD_SUFFIX
4905#endif
4906
4907char *qemu_find_file(int type, const char *name)
4908{
4909 int len;
4910 const char *subdir;
4911 char *buf;
4912
4913 /* If name contains path separators then try it as a straight path. */
4914 if ((strchr(name, '/') || strchr(name, '\\'))
4915 && access(name, R_OK) == 0) {
4916 return strdup(name);
4917 }
4918 switch (type) {
4919 case QEMU_FILE_TYPE_BIOS:
4920 subdir = "";
4921 break;
4922 case QEMU_FILE_TYPE_KEYMAP:
4923 subdir = "keymaps/";
4924 break;
4925 default:
4926 abort();
4927 }
4928 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4929 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004930 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004931 if (access(buf, R_OK)) {
4932 qemu_free(buf);
4933 return NULL;
4934 }
4935 return buf;
4936}
4937
malc902b3d52008-12-10 19:18:40 +00004938int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004939{
aliguori59030a82009-04-05 18:43:41 +00004940 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004941 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004942 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004943 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004944 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004945 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00004946 const char *boot_devices = "";
aliguori3023f3322009-01-16 19:04:14 +00004947 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004948 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004949 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004950 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004951 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004952 const char *bt_opts[MAX_BT_CMDLINE];
4953 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004954 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004955 int optind;
4956 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004957 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004958 const char *monitor_device;
4959 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004960 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004961 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004962 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004963 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4964 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004965 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004966 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004967 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004968 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004969 int usb_devices_index;
blueswir1b9e82a52009-04-05 18:03:31 +00004970#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004971 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004972#endif
bellard26a5f132008-05-28 12:30:31 +00004973 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004974 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004975 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004976#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004977 int fd = 0;
4978 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004979 const char *chroot_dir = NULL;
4980 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004981#endif
aliguori268a3622009-04-21 22:30:27 +00004982 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004983 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004984
malc902b3d52008-12-10 19:18:40 +00004985 qemu_cache_utils_init(envp);
4986
bellard0bd48852005-11-11 00:00:47 +00004987 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004988#ifndef _WIN32
4989 {
4990 struct sigaction act;
4991 sigfillset(&act.sa_mask);
4992 act.sa_flags = 0;
4993 act.sa_handler = SIG_IGN;
4994 sigaction(SIGPIPE, &act, NULL);
4995 }
bellard3587d7e2006-06-26 20:03:44 +00004996#else
4997 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004998 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4999 QEMU to run on a single CPU */
5000 {
5001 HANDLE h;
5002 DWORD mask, smask;
5003 int i;
5004 h = GetCurrentProcess();
5005 if (GetProcessAffinityMask(h, &mask, &smask)) {
5006 for(i = 0; i < 32; i++) {
5007 if (mask & (1 << i))
5008 break;
5009 }
5010 if (i != 32) {
5011 mask = 1 << i;
5012 SetProcessAffinityMask(h, mask);
5013 }
5014 }
5015 }
bellard67b915a2004-03-31 23:37:16 +00005016#endif
bellardbe995c22006-06-25 16:25:21 +00005017
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05005018 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05005019 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00005020 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00005021 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00005022 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00005023 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00005024 kernel_filename = NULL;
5025 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00005026 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00005027 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00005028 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00005029
aurel32c75a8232008-05-04 00:50:34 +00005030 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00005031 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00005032 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00005033 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00005034
aliguori8290edd2009-02-27 20:14:29 +00005035 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00005036 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00005037 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00005038 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00005039
aliguori1b8fc812009-02-27 20:01:39 +00005040 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00005041 virtio_consoles[i] = NULL;
5042 virtio_console_index = 0;
5043
aliguori268a3622009-04-21 22:30:27 +00005044 for (i = 0; i < MAX_NODES; i++) {
5045 node_mem[i] = 0;
5046 node_cpumask[i] = 0;
5047 }
5048
bellarda594cfb2005-11-06 16:13:29 +00005049 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00005050
bellard7c9d8e02005-11-15 22:16:05 +00005051 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00005052 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00005053 nb_drives = 0;
5054 nb_drives_opt = 0;
aliguori268a3622009-04-21 22:30:27 +00005055 nb_numa_nodes = 0;
thse4bcb142007-12-02 04:51:10 +00005056 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00005057
5058 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00005059
bellard26a5f132008-05-28 12:30:31 +00005060 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00005061 autostart= 1;
5062
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005063 register_watchdogs();
5064
bellardcd6f1162004-05-13 22:02:20 +00005065 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00005066 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00005067 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00005068 break;
bellardcd6f1162004-05-13 22:02:20 +00005069 r = argv[optind];
5070 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00005071 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00005072 } else {
5073 const QEMUOption *popt;
5074
5075 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00005076 /* Treat --foo the same as -foo. */
5077 if (r[1] == '-')
5078 r++;
bellardcd6f1162004-05-13 22:02:20 +00005079 popt = qemu_options;
5080 for(;;) {
5081 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00005082 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00005083 argv[0], r);
5084 exit(1);
5085 }
5086 if (!strcmp(popt->name, r + 1))
5087 break;
5088 popt++;
5089 }
5090 if (popt->flags & HAS_ARG) {
5091 if (optind >= argc) {
5092 fprintf(stderr, "%s: option '%s' requires an argument\n",
5093 argv[0], r);
5094 exit(1);
5095 }
5096 optarg = argv[optind++];
5097 } else {
5098 optarg = NULL;
5099 }
5100
5101 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00005102 case QEMU_OPTION_M:
5103 machine = find_machine(optarg);
5104 if (!machine) {
5105 QEMUMachine *m;
5106 printf("Supported machines are:\n");
5107 for(m = first_machine; m != NULL; m = m->next) {
5108 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00005109 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05005110 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00005111 }
ths15f82202007-06-29 23:26:08 +00005112 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00005113 }
5114 break;
j_mayer94fc95c2007-03-05 19:44:02 +00005115 case QEMU_OPTION_cpu:
5116 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00005117 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00005118/* XXX: implement xxx_cpu_list for targets that still miss it */
5119#if defined(cpu_list)
5120 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00005121#endif
ths15f82202007-06-29 23:26:08 +00005122 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00005123 } else {
5124 cpu_model = optarg;
5125 }
5126 break;
bellardcd6f1162004-05-13 22:02:20 +00005127 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00005128 initrd_filename = optarg;
5129 break;
bellardcd6f1162004-05-13 22:02:20 +00005130 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00005131 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00005132 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00005133 else
balrog609497a2008-01-14 02:56:53 +00005134 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00005135 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00005136 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00005137 translation == BIOS_ATA_TRANSLATION_LBA ?
5138 ",trans=lba" :
5139 translation == BIOS_ATA_TRANSLATION_NONE ?
5140 ",trans=none" : "");
5141 break;
bellardcd6f1162004-05-13 22:02:20 +00005142 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00005143 case QEMU_OPTION_hdc:
5144 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00005145 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00005146 break;
thse4bcb142007-12-02 04:51:10 +00005147 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00005148 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00005149 break;
balrog3e3d5812007-04-30 02:09:25 +00005150 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00005151 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00005152 break;
pbrooka1bb27b2007-04-06 16:49:48 +00005153 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00005154 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00005155 break;
j_mayer86f55662007-04-24 06:52:59 +00005156 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00005157 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00005158 break;
bellardcd6f1162004-05-13 22:02:20 +00005159 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00005160 snapshot = 1;
5161 break;
bellardcd6f1162004-05-13 22:02:20 +00005162 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00005163 {
bellard330d0412003-07-26 18:11:40 +00005164 const char *p;
5165 p = optarg;
5166 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005167 if (cyls < 1 || cyls > 16383)
5168 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005169 if (*p != ',')
5170 goto chs_fail;
5171 p++;
5172 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005173 if (heads < 1 || heads > 16)
5174 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005175 if (*p != ',')
5176 goto chs_fail;
5177 p++;
5178 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005179 if (secs < 1 || secs > 63)
5180 goto chs_fail;
5181 if (*p == ',') {
5182 p++;
5183 if (!strcmp(p, "none"))
5184 translation = BIOS_ATA_TRANSLATION_NONE;
5185 else if (!strcmp(p, "lba"))
5186 translation = BIOS_ATA_TRANSLATION_LBA;
5187 else if (!strcmp(p, "auto"))
5188 translation = BIOS_ATA_TRANSLATION_AUTO;
5189 else
5190 goto chs_fail;
5191 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00005192 chs_fail:
bellard46d47672004-11-16 01:45:27 +00005193 fprintf(stderr, "qemu: invalid physical CHS format\n");
5194 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00005195 }
thse4bcb142007-12-02 04:51:10 +00005196 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00005197 snprintf(drives_opt[hda_index].opt,
5198 sizeof(drives_opt[hda_index].opt),
5199 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
5200 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00005201 translation == BIOS_ATA_TRANSLATION_LBA ?
5202 ",trans=lba" :
5203 translation == BIOS_ATA_TRANSLATION_NONE ?
5204 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00005205 }
5206 break;
aliguori268a3622009-04-21 22:30:27 +00005207 case QEMU_OPTION_numa:
5208 if (nb_numa_nodes >= MAX_NODES) {
5209 fprintf(stderr, "qemu: too many NUMA nodes\n");
5210 exit(1);
5211 }
5212 numa_add(optarg);
5213 break;
bellardcd6f1162004-05-13 22:02:20 +00005214 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005215 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00005216 break;
balrog4d3b6f62008-02-10 16:33:14 +00005217#ifdef CONFIG_CURSES
5218 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005219 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00005220 break;
5221#endif
balroga171fe32007-04-30 01:48:07 +00005222 case QEMU_OPTION_portrait:
5223 graphic_rotate = 1;
5224 break;
bellardcd6f1162004-05-13 22:02:20 +00005225 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005226 kernel_filename = optarg;
5227 break;
bellardcd6f1162004-05-13 22:02:20 +00005228 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005229 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005230 break;
bellardcd6f1162004-05-13 22:02:20 +00005231 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005232 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005233 break;
bellardcd6f1162004-05-13 22:02:20 +00005234 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005235 boot_devices = optarg;
5236 /* We just do some generic consistency checks */
5237 {
5238 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00005239 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00005240
5241 boot_devices_bitmap = 0;
5242 for (p = boot_devices; *p != '\0'; p++) {
5243 /* Allowed boot devices are:
5244 * a b : floppy disk drives
5245 * c ... f : IDE disk drives
5246 * g ... m : machine implementation dependant drives
5247 * n ... p : network devices
5248 * It's up to each machine implementation to check
5249 * if the given boot devices match the actual hardware
5250 * implementation and firmware features.
5251 */
5252 if (*p < 'a' || *p > 'q') {
5253 fprintf(stderr, "Invalid boot device '%c'\n", *p);
5254 exit(1);
5255 }
5256 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
5257 fprintf(stderr,
5258 "Boot device '%c' was given twice\n",*p);
5259 exit(1);
5260 }
5261 boot_devices_bitmap |= 1 << (*p - 'a');
5262 }
bellard36b486b2003-11-11 13:36:08 +00005263 }
5264 break;
bellardcd6f1162004-05-13 22:02:20 +00005265 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005266 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005267 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005268 break;
bellard52ca8d62006-06-14 16:03:05 +00005269#ifdef TARGET_I386
5270 case QEMU_OPTION_no_fd_bootchk:
5271 fd_bootchk = 0;
5272 break;
5273#endif
bellard7c9d8e02005-11-15 22:16:05 +00005274 case QEMU_OPTION_net:
5275 if (nb_net_clients >= MAX_NET_CLIENTS) {
5276 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00005277 exit(1);
5278 }
pbrookfd5f3932008-03-26 20:55:43 +00005279 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00005280 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00005281 break;
bellardc7f74642004-08-24 21:57:12 +00005282#ifdef CONFIG_SLIRP
5283 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00005284 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005285 break;
ths47d5d012007-02-20 00:05:08 +00005286 case QEMU_OPTION_bootp:
5287 bootp_filename = optarg;
5288 break;
bellardc94c8d62004-09-13 21:37:34 +00005289#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005290 case QEMU_OPTION_smb:
5291 net_slirp_smb(optarg);
5292 break;
bellardc94c8d62004-09-13 21:37:34 +00005293#endif
bellard9bf05442004-08-25 22:12:49 +00005294 case QEMU_OPTION_redir:
Alexander Grafc1261d82009-05-26 13:03:26 +02005295 net_slirp_redir(NULL, optarg, NULL);
bellard9bf05442004-08-25 22:12:49 +00005296 break;
bellardc7f74642004-08-24 21:57:12 +00005297#endif
balrogdc72ac12008-11-09 00:04:26 +00005298 case QEMU_OPTION_bt:
5299 if (nb_bt_opts >= MAX_BT_CMDLINE) {
5300 fprintf(stderr, "qemu: too many bluetooth options\n");
5301 exit(1);
5302 }
5303 bt_opts[nb_bt_opts++] = optarg;
5304 break;
bellard1d14ffa2005-10-30 18:58:22 +00005305#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005306 case QEMU_OPTION_audio_help:
5307 AUD_help ();
5308 exit (0);
5309 break;
5310 case QEMU_OPTION_soundhw:
5311 select_soundhw (optarg);
5312 break;
5313#endif
bellardcd6f1162004-05-13 22:02:20 +00005314 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005315 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005316 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005317 case QEMU_OPTION_version:
5318 version();
5319 exit(0);
5320 break;
aurel3200f82b82008-04-27 21:12:55 +00005321 case QEMU_OPTION_m: {
5322 uint64_t value;
5323 char *ptr;
5324
5325 value = strtoul(optarg, &ptr, 10);
5326 switch (*ptr) {
5327 case 0: case 'M': case 'm':
5328 value <<= 20;
5329 break;
5330 case 'G': case 'g':
5331 value <<= 30;
5332 break;
5333 default:
5334 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005335 exit(1);
5336 }
aurel3200f82b82008-04-27 21:12:55 +00005337
5338 /* On 32-bit hosts, QEMU is limited by virtual address space */
5339 if (value > (2047 << 20)
blueswir1640f42e2009-04-19 10:18:01 +00005340#ifndef CONFIG_KQEMU
aurel3200f82b82008-04-27 21:12:55 +00005341 && HOST_LONG_BITS == 32
5342#endif
5343 ) {
5344 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5345 exit(1);
5346 }
5347 if (value != (uint64_t)(ram_addr_t)value) {
5348 fprintf(stderr, "qemu: ram size too large\n");
5349 exit(1);
5350 }
5351 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005352 break;
aurel3200f82b82008-04-27 21:12:55 +00005353 }
bellardcd6f1162004-05-13 22:02:20 +00005354 case QEMU_OPTION_d:
5355 {
5356 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005357 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005358
bellardcd6f1162004-05-13 22:02:20 +00005359 mask = cpu_str_to_log_mask(optarg);
5360 if (!mask) {
5361 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005362 for(item = cpu_log_items; item->mask != 0; item++) {
5363 printf("%-10s %s\n", item->name, item->help);
5364 }
5365 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005366 }
5367 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005368 }
bellardcd6f1162004-05-13 22:02:20 +00005369 break;
bellardcd6f1162004-05-13 22:02:20 +00005370 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005371 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005372 break;
aliguori59030a82009-04-05 18:43:41 +00005373 case QEMU_OPTION_gdb:
5374 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005375 break;
bellardcd6f1162004-05-13 22:02:20 +00005376 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005377 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005378 break;
j_mayer1192dad2007-10-05 13:08:35 +00005379 case QEMU_OPTION_bios:
5380 bios_name = optarg;
5381 break;
aurel321b530a62009-04-05 20:08:59 +00005382 case QEMU_OPTION_singlestep:
5383 singlestep = 1;
5384 break;
bellardcd6f1162004-05-13 22:02:20 +00005385 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005386 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005387 break;
blueswir15824d652009-03-28 06:44:27 +00005388#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00005389 case QEMU_OPTION_k:
5390 keyboard_layout = optarg;
5391 break;
blueswir15824d652009-03-28 06:44:27 +00005392#endif
bellardee22c2f2004-06-03 12:49:50 +00005393 case QEMU_OPTION_localtime:
5394 rtc_utc = 0;
5395 break;
malc3893c122008-09-28 00:42:05 +00005396 case QEMU_OPTION_vga:
5397 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005398 break;
blueswir15824d652009-03-28 06:44:27 +00005399#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005400 case QEMU_OPTION_g:
5401 {
5402 const char *p;
5403 int w, h, depth;
5404 p = optarg;
5405 w = strtol(p, (char **)&p, 10);
5406 if (w <= 0) {
5407 graphic_error:
5408 fprintf(stderr, "qemu: invalid resolution or depth\n");
5409 exit(1);
5410 }
5411 if (*p != 'x')
5412 goto graphic_error;
5413 p++;
5414 h = strtol(p, (char **)&p, 10);
5415 if (h <= 0)
5416 goto graphic_error;
5417 if (*p == 'x') {
5418 p++;
5419 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005420 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005421 depth != 24 && depth != 32)
5422 goto graphic_error;
5423 } else if (*p == '\0') {
5424 depth = graphic_depth;
5425 } else {
5426 goto graphic_error;
5427 }
ths3b46e622007-09-17 08:09:54 +00005428
bellarde9b137c2004-06-21 16:46:10 +00005429 graphic_width = w;
5430 graphic_height = h;
5431 graphic_depth = depth;
5432 }
5433 break;
blueswir15824d652009-03-28 06:44:27 +00005434#endif
ths20d8a3e2007-02-18 17:04:49 +00005435 case QEMU_OPTION_echr:
5436 {
5437 char *r;
5438 term_escape_char = strtol(optarg, &r, 0);
5439 if (r == optarg)
5440 printf("Bad argument to echr\n");
5441 break;
5442 }
bellard82c643f2004-07-14 17:28:13 +00005443 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00005444 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00005445 break;
5446 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005447 if (serial_device_index >= MAX_SERIAL_PORTS) {
5448 fprintf(stderr, "qemu: too many serial ports\n");
5449 exit(1);
5450 }
pbrookfd5f3932008-03-26 20:55:43 +00005451 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005452 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005453 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005454 case QEMU_OPTION_watchdog:
5455 i = select_watchdog(optarg);
5456 if (i > 0)
5457 exit (i == 1 ? 1 : 0);
5458 break;
5459 case QEMU_OPTION_watchdog_action:
5460 if (select_watchdog_action(optarg) == -1) {
5461 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5462 exit(1);
5463 }
5464 break;
aliguori51ecf132009-01-15 20:06:40 +00005465 case QEMU_OPTION_virtiocon:
5466 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5467 fprintf(stderr, "qemu: too many virtio consoles\n");
5468 exit(1);
5469 }
5470 virtio_consoles[virtio_console_index] = optarg;
5471 virtio_console_index++;
5472 break;
bellard6508fe52005-01-15 12:02:56 +00005473 case QEMU_OPTION_parallel:
5474 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5475 fprintf(stderr, "qemu: too many parallel ports\n");
5476 exit(1);
5477 }
pbrookfd5f3932008-03-26 20:55:43 +00005478 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005479 parallel_device_index++;
5480 break;
bellardd63d3072004-10-03 13:29:03 +00005481 case QEMU_OPTION_loadvm:
5482 loadvm = optarg;
5483 break;
5484 case QEMU_OPTION_full_screen:
5485 full_screen = 1;
5486 break;
ths667acca2006-12-11 02:08:05 +00005487#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005488 case QEMU_OPTION_no_frame:
5489 no_frame = 1;
5490 break;
ths3780e192007-06-21 21:08:02 +00005491 case QEMU_OPTION_alt_grab:
5492 alt_grab = 1;
5493 break;
ths667acca2006-12-11 02:08:05 +00005494 case QEMU_OPTION_no_quit:
5495 no_quit = 1;
5496 break;
aliguori7d957bd2009-01-15 22:14:11 +00005497 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005498 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005499 break;
ths667acca2006-12-11 02:08:05 +00005500#endif
bellardf7cce892004-12-08 22:21:25 +00005501 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005502 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005503 break;
bellarda09db212005-04-30 16:10:35 +00005504#ifdef TARGET_I386
5505 case QEMU_OPTION_win2k_hack:
5506 win2k_install_hack = 1;
5507 break;
aliguori73822ec2009-01-15 20:11:34 +00005508 case QEMU_OPTION_rtc_td_hack:
5509 rtc_td_hack = 1;
5510 break;
aliguori8a92ea22009-02-27 20:12:36 +00005511 case QEMU_OPTION_acpitable:
5512 if(acpi_table_add(optarg) < 0) {
5513 fprintf(stderr, "Wrong acpi table provided\n");
5514 exit(1);
5515 }
5516 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005517 case QEMU_OPTION_smbios:
5518 if(smbios_entry_add(optarg) < 0) {
5519 fprintf(stderr, "Wrong smbios provided\n");
5520 exit(1);
5521 }
5522 break;
bellarda09db212005-04-30 16:10:35 +00005523#endif
blueswir1640f42e2009-04-19 10:18:01 +00005524#ifdef CONFIG_KQEMU
bellardd993e022005-02-10 22:00:06 +00005525 case QEMU_OPTION_no_kqemu:
5526 kqemu_allowed = 0;
5527 break;
bellard89bfc102006-02-08 22:46:31 +00005528 case QEMU_OPTION_kernel_kqemu:
5529 kqemu_allowed = 2;
5530 break;
bellardd993e022005-02-10 22:00:06 +00005531#endif
aliguori7ba1e612008-11-05 16:04:33 +00005532#ifdef CONFIG_KVM
5533 case QEMU_OPTION_enable_kvm:
5534 kvm_allowed = 1;
blueswir1640f42e2009-04-19 10:18:01 +00005535#ifdef CONFIG_KQEMU
aliguori7ba1e612008-11-05 16:04:33 +00005536 kqemu_allowed = 0;
5537#endif
5538 break;
5539#endif
bellardbb36d472005-11-05 14:22:28 +00005540 case QEMU_OPTION_usb:
5541 usb_enabled = 1;
5542 break;
bellarda594cfb2005-11-06 16:13:29 +00005543 case QEMU_OPTION_usbdevice:
5544 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00005545 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00005546 fprintf(stderr, "Too many USB devices\n");
5547 exit(1);
5548 }
pbrookfd5f3932008-03-26 20:55:43 +00005549 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00005550 usb_devices_index++;
5551 break;
bellard6a00d602005-11-21 23:25:50 +00005552 case QEMU_OPTION_smp:
5553 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00005554 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005555 fprintf(stderr, "Invalid number of CPUs\n");
5556 exit(1);
5557 }
5558 break;
bellard24236862006-04-30 21:28:36 +00005559 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005560 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005561 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005562 break;
blueswir15824d652009-03-28 06:44:27 +00005563#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005564 case QEMU_OPTION_no_acpi:
5565 acpi_enabled = 0;
5566 break;
aliguori16b29ae2008-12-17 23:28:44 +00005567 case QEMU_OPTION_no_hpet:
5568 no_hpet = 1;
5569 break;
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005570 case QEMU_OPTION_no_virtio_balloon:
5571 no_virtio_balloon = 1;
5572 break;
blueswir15824d652009-03-28 06:44:27 +00005573#endif
bellardd1beab82006-10-02 19:44:22 +00005574 case QEMU_OPTION_no_reboot:
5575 no_reboot = 1;
5576 break;
aurel32b2f76162008-04-11 21:35:52 +00005577 case QEMU_OPTION_no_shutdown:
5578 no_shutdown = 1;
5579 break;
balrog9467cd42007-05-01 01:34:14 +00005580 case QEMU_OPTION_show_cursor:
5581 cursor_hide = 0;
5582 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005583 case QEMU_OPTION_uuid:
5584 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5585 fprintf(stderr, "Fail to parse UUID string."
5586 " Wrong format.\n");
5587 exit(1);
5588 }
5589 break;
blueswir15824d652009-03-28 06:44:27 +00005590#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005591 case QEMU_OPTION_daemonize:
5592 daemonize = 1;
5593 break;
blueswir15824d652009-03-28 06:44:27 +00005594#endif
ths9ae02552007-01-05 17:39:04 +00005595 case QEMU_OPTION_option_rom:
5596 if (nb_option_roms >= MAX_OPTION_ROMS) {
5597 fprintf(stderr, "Too many option ROMs\n");
5598 exit(1);
5599 }
5600 option_rom[nb_option_roms] = optarg;
5601 nb_option_roms++;
5602 break;
blueswir15824d652009-03-28 06:44:27 +00005603#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005604 case QEMU_OPTION_semihosting:
5605 semihosting_enabled = 1;
5606 break;
blueswir15824d652009-03-28 06:44:27 +00005607#endif
thsc35734b2007-03-19 15:17:08 +00005608 case QEMU_OPTION_name:
5609 qemu_name = optarg;
5610 break;
blueswir195efd112008-12-24 20:26:14 +00005611#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005612 case QEMU_OPTION_prom_env:
5613 if (nb_prom_envs >= MAX_PROM_ENVS) {
5614 fprintf(stderr, "Too many prom variables\n");
5615 exit(1);
5616 }
5617 prom_envs[nb_prom_envs] = optarg;
5618 nb_prom_envs++;
5619 break;
5620#endif
balrog2b8f2d42007-07-27 22:08:46 +00005621#ifdef TARGET_ARM
5622 case QEMU_OPTION_old_param:
5623 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005624 break;
balrog2b8f2d42007-07-27 22:08:46 +00005625#endif
thsf3dcfad2007-08-24 01:26:02 +00005626 case QEMU_OPTION_clock:
5627 configure_alarms(optarg);
5628 break;
bellard7e0af5d02007-11-07 16:24:33 +00005629 case QEMU_OPTION_startdate:
5630 {
5631 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005632 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005633 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005634 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005635 } else {
5636 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5637 &tm.tm_year,
5638 &tm.tm_mon,
5639 &tm.tm_mday,
5640 &tm.tm_hour,
5641 &tm.tm_min,
5642 &tm.tm_sec) == 6) {
5643 /* OK */
5644 } else if (sscanf(optarg, "%d-%d-%d",
5645 &tm.tm_year,
5646 &tm.tm_mon,
5647 &tm.tm_mday) == 3) {
5648 tm.tm_hour = 0;
5649 tm.tm_min = 0;
5650 tm.tm_sec = 0;
5651 } else {
5652 goto date_fail;
5653 }
5654 tm.tm_year -= 1900;
5655 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005656 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005657 if (rtc_start_date == -1) {
5658 date_fail:
5659 fprintf(stderr, "Invalid date format. Valid format are:\n"
5660 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5661 exit(1);
5662 }
balrogf6503052008-02-17 11:42:19 +00005663 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005664 }
5665 }
5666 break;
bellard26a5f132008-05-28 12:30:31 +00005667 case QEMU_OPTION_tb_size:
5668 tb_size = strtol(optarg, NULL, 0);
5669 if (tb_size < 0)
5670 tb_size = 0;
5671 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005672 case QEMU_OPTION_icount:
5673 use_icount = 1;
5674 if (strcmp(optarg, "auto") == 0) {
5675 icount_time_shift = -1;
5676 } else {
5677 icount_time_shift = strtol(optarg, NULL, 0);
5678 }
5679 break;
aliguori5bb79102008-10-13 03:12:02 +00005680 case QEMU_OPTION_incoming:
5681 incoming = optarg;
5682 break;
blueswir15824d652009-03-28 06:44:27 +00005683#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005684 case QEMU_OPTION_chroot:
5685 chroot_dir = optarg;
5686 break;
5687 case QEMU_OPTION_runas:
5688 run_as = optarg;
5689 break;
blueswir15824d652009-03-28 06:44:27 +00005690#endif
aliguorie37630c2009-04-22 15:19:10 +00005691#ifdef CONFIG_XEN
5692 case QEMU_OPTION_xen_domid:
5693 xen_domid = atoi(optarg);
5694 break;
5695 case QEMU_OPTION_xen_create:
5696 xen_mode = XEN_CREATE;
5697 break;
5698 case QEMU_OPTION_xen_attach:
5699 xen_mode = XEN_ATTACH;
5700 break;
5701#endif
bellardcd6f1162004-05-13 22:02:20 +00005702 }
bellard0824d6f2003-06-24 13:42:40 +00005703 }
5704 }
bellard330d0412003-07-26 18:11:40 +00005705
Paul Brook5cea8592009-05-30 00:52:44 +01005706 /* If no data_dir is specified then try to find it relative to the
5707 executable path. */
5708 if (!data_dir) {
5709 data_dir = find_datadir(argv[0]);
5710 }
5711 /* If all else fails use the install patch specified when building. */
5712 if (!data_dir) {
5713 data_dir = CONFIG_QEMU_SHAREDIR;
5714 }
5715
blueswir1640f42e2009-04-19 10:18:01 +00005716#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
aliguori7ba1e612008-11-05 16:04:33 +00005717 if (kvm_allowed && kqemu_allowed) {
5718 fprintf(stderr,
5719 "You can not enable both KVM and kqemu at the same time\n");
5720 exit(1);
5721 }
5722#endif
5723
balrog3d878ca2008-10-28 10:59:59 +00005724 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005725 if (smp_cpus > machine->max_cpus) {
5726 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5727 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5728 machine->max_cpus);
5729 exit(1);
5730 }
5731
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005732 if (display_type == DT_NOGRAPHIC) {
aliguoribc0129d2008-08-01 15:12:34 +00005733 if (serial_device_index == 0)
5734 serial_devices[0] = "stdio";
5735 if (parallel_device_index == 0)
5736 parallel_devices[0] = "null";
5737 if (strncmp(monitor_device, "vc", 2) == 0)
5738 monitor_device = "stdio";
5739 }
5740
ths71e3ceb2006-12-22 02:11:31 +00005741#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005742 if (daemonize) {
5743 pid_t pid;
5744
5745 if (pipe(fds) == -1)
5746 exit(1);
5747
5748 pid = fork();
5749 if (pid > 0) {
5750 uint8_t status;
5751 ssize_t len;
5752
5753 close(fds[1]);
5754
5755 again:
ths93815bc2007-03-19 15:58:31 +00005756 len = read(fds[0], &status, 1);
5757 if (len == -1 && (errno == EINTR))
5758 goto again;
5759
5760 if (len != 1)
5761 exit(1);
5762 else if (status == 1) {
5763 fprintf(stderr, "Could not acquire pidfile\n");
5764 exit(1);
5765 } else
5766 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005767 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005768 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005769
5770 setsid();
5771
5772 pid = fork();
5773 if (pid > 0)
5774 exit(0);
5775 else if (pid < 0)
5776 exit(1);
5777
5778 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005779
5780 signal(SIGTSTP, SIG_IGN);
5781 signal(SIGTTOU, SIG_IGN);
5782 signal(SIGTTIN, SIG_IGN);
5783 }
ths71e3ceb2006-12-22 02:11:31 +00005784
thsaa26bb22007-03-25 21:33:06 +00005785 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005786 if (daemonize) {
5787 uint8_t status = 1;
5788 write(fds[1], &status, 1);
5789 } else
5790 fprintf(stderr, "Could not acquire pid file\n");
5791 exit(1);
5792 }
blueswir1b9e82a52009-04-05 18:03:31 +00005793#endif
ths93815bc2007-03-19 15:58:31 +00005794
blueswir1640f42e2009-04-19 10:18:01 +00005795#ifdef CONFIG_KQEMU
bellardff3fbb32006-01-08 10:53:14 +00005796 if (smp_cpus > 1)
5797 kqemu_allowed = 0;
5798#endif
aliguori3fcf7b62009-04-24 18:03:25 +00005799 if (qemu_init_main_loop()) {
5800 fprintf(stderr, "qemu_init_main_loop failed\n");
5801 exit(1);
5802 }
bellarda20dd502003-09-30 21:07:02 +00005803 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005804
thsf8d39c02008-07-03 10:01:15 +00005805 if (!linux_boot && *kernel_cmdline != '\0') {
5806 fprintf(stderr, "-append only allowed with -kernel option\n");
5807 exit(1);
5808 }
5809
5810 if (!linux_boot && initrd_filename != NULL) {
5811 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5812 exit(1);
5813 }
5814
ths96d30e42007-01-07 20:42:14 +00005815 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00005816 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00005817 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00005818 }
bellardb118d612003-06-30 23:36:21 +00005819 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005820
pbrook634fce92006-07-15 17:40:09 +00005821 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005822 if (init_timer_alarm() < 0) {
5823 fprintf(stderr, "could not initialize alarm timer\n");
5824 exit(1);
5825 }
pbrook2e70f6e2008-06-29 01:03:05 +00005826 if (use_icount && icount_time_shift < 0) {
5827 use_icount = 2;
5828 /* 125MIPS seems a reasonable initial guess at the guest speed.
5829 It will be corrected fairly quickly anyway. */
5830 icount_time_shift = 3;
5831 init_icount_adjust();
5832 }
pbrook634fce92006-07-15 17:40:09 +00005833
bellardfd1dff42006-02-01 21:29:26 +00005834#ifdef _WIN32
5835 socket_init();
5836#endif
5837
bellard7c9d8e02005-11-15 22:16:05 +00005838 /* init network clients */
5839 if (nb_net_clients == 0) {
5840 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005841 net_clients[nb_net_clients++] = "nic";
5842#ifdef CONFIG_SLIRP
5843 net_clients[nb_net_clients++] = "user";
5844#endif
bellardc20709a2004-04-21 23:27:19 +00005845 }
5846
bellard7c9d8e02005-11-15 22:16:05 +00005847 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005848 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005849 exit(1);
bellard702c6512004-04-02 21:21:32 +00005850 }
bellardf1510b22003-06-25 00:07:40 +00005851
Glauber Costa406c8df2009-06-17 09:05:30 -04005852 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5853 net_set_boot_mask(net_boot);
5854
5855 net_client_check();
thseec85c22007-01-05 17:41:07 +00005856
balrogdc72ac12008-11-09 00:04:26 +00005857 /* init the bluetooth world */
5858 for (i = 0; i < nb_bt_opts; i++)
5859 if (bt_parse(bt_opts[i]))
5860 exit(1);
5861
bellard0824d6f2003-06-24 13:42:40 +00005862 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005863 if (ram_size == 0)
5864 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005865
blueswir1640f42e2009-04-19 10:18:01 +00005866#ifdef CONFIG_KQEMU
pbrook94a6b542009-04-11 17:15:54 +00005867 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5868 guest ram allocation. It needs to go away. */
5869 if (kqemu_allowed) {
Paul Brook4cfce482009-05-13 18:08:17 +01005870 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
pbrook94a6b542009-04-11 17:15:54 +00005871 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5872 if (!kqemu_phys_ram_base) {
5873 fprintf(stderr, "Could not allocate physical memory\n");
5874 exit(1);
5875 }
balrog7fb4fdc2008-04-24 17:59:27 +00005876 }
pbrook94a6b542009-04-11 17:15:54 +00005877#endif
bellard0824d6f2003-06-24 13:42:40 +00005878
bellard26a5f132008-05-28 12:30:31 +00005879 /* init the dynamic translator */
5880 cpu_exec_init_all(tb_size * 1024 * 1024);
5881
bellard5905b2e2004-08-01 21:53:26 +00005882 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005883
5884 /* we always create the cdrom drive, even if no disk is there */
5885
5886 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005887 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005888
balrog9d413d12007-12-04 00:10:34 +00005889 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005890
5891 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005892 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005893
balrog9d413d12007-12-04 00:10:34 +00005894 /* we always create one sd slot, even if no card is in it */
5895
5896 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005897 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005898
ths96d30e42007-01-07 20:42:14 +00005899 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005900
thse4bcb142007-12-02 04:51:10 +00005901 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005902 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005903 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005904
bellardc88676f2006-08-06 13:36:11 +00005905 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005906 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005907
aliguori3023f3322009-01-16 19:04:14 +00005908#ifndef _WIN32
5909 /* must be after terminal init, SDL library changes signal handlers */
Jan Kiszka7c3370d2009-05-08 12:34:17 +02005910 sighandler_setup();
aliguori3023f3322009-01-16 19:04:14 +00005911#endif
5912
5913 /* Maintain compatibility with multiple stdio monitors */
5914 if (!strcmp(monitor_device,"stdio")) {
5915 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5916 const char *devname = serial_devices[i];
5917 if (devname && !strcmp(devname,"mon:stdio")) {
5918 monitor_device = NULL;
5919 break;
5920 } else if (devname && !strcmp(devname,"stdio")) {
5921 monitor_device = NULL;
5922 serial_devices[i] = "mon:stdio";
5923 break;
5924 }
5925 }
5926 }
5927
aliguori268a3622009-04-21 22:30:27 +00005928 if (nb_numa_nodes > 0) {
5929 int i;
5930
5931 if (nb_numa_nodes > smp_cpus) {
5932 nb_numa_nodes = smp_cpus;
5933 }
5934
5935 /* If no memory size if given for any node, assume the default case
5936 * and distribute the available memory equally across all nodes
5937 */
5938 for (i = 0; i < nb_numa_nodes; i++) {
5939 if (node_mem[i] != 0)
5940 break;
5941 }
5942 if (i == nb_numa_nodes) {
5943 uint64_t usedmem = 0;
5944
5945 /* On Linux, the each node's border has to be 8MB aligned,
5946 * the final node gets the rest.
5947 */
5948 for (i = 0; i < nb_numa_nodes - 1; i++) {
5949 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5950 usedmem += node_mem[i];
5951 }
5952 node_mem[i] = ram_size - usedmem;
5953 }
5954
5955 for (i = 0; i < nb_numa_nodes; i++) {
5956 if (node_cpumask[i] != 0)
5957 break;
5958 }
5959 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5960 * must cope with this anyway, because there are BIOSes out there in
5961 * real machines which also use this scheme.
5962 */
5963 if (i == nb_numa_nodes) {
5964 for (i = 0; i < smp_cpus; i++) {
5965 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5966 }
5967 }
5968 }
5969
aliguori3023f3322009-01-16 19:04:14 +00005970 if (kvm_enabled()) {
5971 int ret;
5972
5973 ret = kvm_init(smp_cpus);
5974 if (ret < 0) {
5975 fprintf(stderr, "failed to initialize KVM\n");
5976 exit(1);
5977 }
5978 }
5979
aliguori4c621802009-01-16 21:48:20 +00005980 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005981 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005982 if (!monitor_hd) {
5983 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5984 exit(1);
5985 }
5986 }
5987
aliguori2796dae2009-01-16 20:23:27 +00005988 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5989 const char *devname = serial_devices[i];
5990 if (devname && strcmp(devname, "none")) {
5991 char label[32];
5992 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005993 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005994 if (!serial_hds[i]) {
5995 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5996 devname);
5997 exit(1);
5998 }
5999 }
6000 }
6001
6002 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
6003 const char *devname = parallel_devices[i];
6004 if (devname && strcmp(devname, "none")) {
6005 char label[32];
6006 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00006007 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00006008 if (!parallel_hds[i]) {
6009 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
6010 devname);
6011 exit(1);
6012 }
6013 }
6014 }
6015
6016 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
6017 const char *devname = virtio_consoles[i];
6018 if (devname && strcmp(devname, "none")) {
6019 char label[32];
6020 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00006021 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00006022 if (!virtcon_hds[i]) {
6023 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
6024 devname);
6025 exit(1);
6026 }
6027 }
6028 }
6029
Paul Brookaae94602009-05-14 22:35:06 +01006030 module_call_init(MODULE_INIT_DEVICE);
6031
Paul Brookfbe1b592009-05-13 17:56:25 +01006032 machine->init(ram_size, boot_devices,
aliguori3023f3322009-01-16 19:04:14 +00006033 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
6034
aliguori268a3622009-04-21 22:30:27 +00006035
6036 for (env = first_cpu; env != NULL; env = env->next_cpu) {
6037 for (i = 0; i < nb_numa_nodes; i++) {
6038 if (node_cpumask[i] & (1 << env->cpu_index)) {
6039 env->numa_node = i;
6040 }
6041 }
6042 }
6043
aliguori6f338c32009-02-11 15:21:54 +00006044 current_machine = machine;
6045
aliguori3023f3322009-01-16 19:04:14 +00006046 /* Set KVM's vcpu state to qemu's initial CPUState. */
6047 if (kvm_enabled()) {
6048 int ret;
6049
6050 ret = kvm_sync_vcpus();
6051 if (ret < 0) {
6052 fprintf(stderr, "failed to initialize vcpus\n");
6053 exit(1);
6054 }
6055 }
6056
6057 /* init USB devices */
6058 if (usb_enabled) {
6059 for(i = 0; i < usb_devices_index; i++) {
aliguoric0f4ce72009-03-05 23:01:01 +00006060 if (usb_device_add(usb_devices[i], 0) < 0) {
aliguori3023f3322009-01-16 19:04:14 +00006061 fprintf(stderr, "Warning: could not add USB device %s\n",
6062 usb_devices[i]);
6063 }
6064 }
6065 }
6066
aliguori8f391ab2009-01-19 16:34:10 +00006067 if (!display_state)
6068 dumb_display_init();
aliguori3023f3322009-01-16 19:04:14 +00006069 /* just use the first displaystate for the moment */
6070 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006071
6072 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006073#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006074 display_type = DT_SDL;
6075#else
6076 display_type = DT_VNC;
6077 vnc_display = "localhost:0,to=99";
6078 show_vnc_port = 1;
6079#endif
6080 }
6081
6082
6083 switch (display_type) {
6084 case DT_NOGRAPHIC:
6085 break;
6086#if defined(CONFIG_CURSES)
6087 case DT_CURSES:
6088 curses_display_init(ds, full_screen);
6089 break;
6090#endif
bellard5b0753e2005-03-01 21:37:28 +00006091#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006092 case DT_SDL:
6093 sdl_display_init(ds, full_screen, no_frame);
6094 break;
bellard5b0753e2005-03-01 21:37:28 +00006095#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006096 case DT_SDL:
6097 cocoa_display_init(ds, full_screen);
6098 break;
bellard313aa562003-08-10 21:52:11 +00006099#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006100 case DT_VNC:
6101 vnc_display_init(ds);
6102 if (vnc_display_open(ds, vnc_display) < 0)
6103 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006104
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006105 if (show_vnc_port) {
6106 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006107 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006108 break;
6109 default:
6110 break;
bellard313aa562003-08-10 21:52:11 +00006111 }
aliguori7d957bd2009-01-15 22:14:11 +00006112 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00006113
aliguori3023f3322009-01-16 19:04:14 +00006114 dcl = ds->listeners;
6115 while (dcl != NULL) {
6116 if (dcl->dpy_refresh != NULL) {
6117 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
6118 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00006119 }
aliguori3023f3322009-01-16 19:04:14 +00006120 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00006121 }
aliguori3023f3322009-01-16 19:04:14 +00006122
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006123 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00006124 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
6125 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
6126 }
6127
aliguori2796dae2009-01-16 20:23:27 +00006128 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00006129 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00006130
aliguori4c621802009-01-16 21:48:20 +00006131 if (monitor_device && monitor_hd)
aliguoricde76ee2009-03-05 23:01:51 +00006132 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
bellard82c643f2004-07-14 17:28:13 +00006133
bellard8d11df92004-08-24 21:13:40 +00006134 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006135 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006136 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00006137 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006138 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00006139 }
bellard82c643f2004-07-14 17:28:13 +00006140 }
bellard82c643f2004-07-14 17:28:13 +00006141
bellard6508fe52005-01-15 12:02:56 +00006142 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006143 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006144 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00006145 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006146 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00006147 }
6148 }
6149
aliguori9ede2fd2009-01-15 20:05:25 +00006150 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
6151 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00006152 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00006153 if (strstart(devname, "vc", 0))
6154 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
6155 }
6156 }
6157
aliguori59030a82009-04-05 18:43:41 +00006158 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
6159 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
6160 gdbstub_dev);
6161 exit(1);
balrog45669e02007-07-02 13:20:17 +00006162 }
balrog45669e02007-07-02 13:20:17 +00006163
bellardd63d3072004-10-03 13:29:03 +00006164 if (loadvm)
aliguori376253e2009-03-05 23:01:23 +00006165 do_loadvm(cur_mon, loadvm);
bellardd63d3072004-10-03 13:29:03 +00006166
aliguori5bb79102008-10-13 03:12:02 +00006167 if (incoming) {
6168 autostart = 0; /* fixme how to deal with -daemonize */
6169 qemu_start_incoming_migration(incoming);
6170 }
6171
aliguoric0f4ce72009-03-05 23:01:01 +00006172 if (autostart)
6173 vm_start();
thsffd843b2006-12-21 19:46:43 +00006174
blueswir1b9e82a52009-04-05 18:03:31 +00006175#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00006176 if (daemonize) {
6177 uint8_t status = 0;
6178 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00006179
6180 again1:
6181 len = write(fds[1], &status, 1);
6182 if (len == -1 && (errno == EINTR))
6183 goto again1;
6184
6185 if (len != 1)
6186 exit(1);
6187
aliguoribd54b862008-07-23 00:58:33 +00006188 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00006189 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00006190 if (fd == -1)
6191 exit(1);
aliguori08585322009-02-27 22:09:45 +00006192 }
ths71e3ceb2006-12-22 02:11:31 +00006193
aliguori08585322009-02-27 22:09:45 +00006194 if (run_as) {
6195 pwd = getpwnam(run_as);
6196 if (!pwd) {
6197 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6198 exit(1);
6199 }
6200 }
ths71e3ceb2006-12-22 02:11:31 +00006201
aliguori08585322009-02-27 22:09:45 +00006202 if (chroot_dir) {
6203 if (chroot(chroot_dir) < 0) {
6204 fprintf(stderr, "chroot failed\n");
6205 exit(1);
6206 }
6207 chdir("/");
6208 }
6209
6210 if (run_as) {
6211 if (setgid(pwd->pw_gid) < 0) {
6212 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6213 exit(1);
6214 }
6215 if (setuid(pwd->pw_uid) < 0) {
6216 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6217 exit(1);
6218 }
6219 if (setuid(0) != -1) {
6220 fprintf(stderr, "Dropping privileges failed\n");
6221 exit(1);
6222 }
6223 }
aliguori08585322009-02-27 22:09:45 +00006224
6225 if (daemonize) {
6226 dup2(fd, 0);
6227 dup2(fd, 1);
6228 dup2(fd, 2);
6229
6230 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00006231 }
blueswir1b9e82a52009-04-05 18:03:31 +00006232#endif
ths71e3ceb2006-12-22 02:11:31 +00006233
bellard8a7ddc32004-03-31 19:00:16 +00006234 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006235 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006236 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006237
bellard0824d6f2003-06-24 13:42:40 +00006238 return 0;
6239}