blob: b14f7b0123574ea51d1a2229cfe2ca14bec2184c [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
blueswir1179a2c12009-03-08 08:23:32 +000032/* Needed early for HOST_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +000036#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000038#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000039#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000042#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000043#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000044#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <net/if.h>
46#if defined(__NetBSD__)
47#include <net/if_tap.h>
48#endif
49#ifdef __linux__
50#include <linux/if_tun.h>
51#endif
52#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000053#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000054#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000055#include <sys/select.h>
blueswir1179a2c12009-03-08 08:23:32 +000056#ifdef HOST_BSD
bellard7d3505c2004-05-12 19:32:15 +000057#include <sys/stat.h>
blueswir1c5e97232009-03-07 20:06:23 +000058#if defined(__FreeBSD__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000059#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000060#else
61#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000062#endif
ths5c40d2b2007-06-23 16:03:36 +000063#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
64#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000065#else
blueswir1223f0d72008-09-30 18:12:18 +000066#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000067#include <pty.h>
68#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000069#include <linux/rtc.h>
thsbd494f42007-09-16 20:03:23 +000070
71/* For the benefit of older linux systems which don't supply it,
72 we use a local copy of hpet.h. */
73/* #include <linux/hpet.h> */
74#include "hpet.h"
75
bellarde57a8c02005-11-10 23:58:52 +000076#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000077#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000078#endif
79#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000080#include <sys/stat.h>
81#include <sys/ethernet.h>
82#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000083#include <netinet/arp.h>
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#include <netinet/ip_icmp.h> // must come after ip.h
88#include <netinet/udp.h>
89#include <netinet/tcp.h>
90#include <net/if.h>
91#include <syslog.h>
92#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000093#endif
bellard7d3505c2004-05-12 19:32:15 +000094#endif
bellardec530c82006-04-25 22:36:06 +000095#endif
bellard67b915a2004-03-31 23:37:16 +000096
blueswir19892fbf2008-08-24 10:34:20 +000097#if defined(__OpenBSD__)
98#include <util.h>
99#endif
100
ths8a16d272008-07-19 09:56:24 +0000101#if defined(CONFIG_VDE)
102#include <libvdeplug.h>
103#endif
104
bellard67b915a2004-03-31 23:37:16 +0000105#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000106#include <windows.h>
bellard7d3505c2004-05-12 19:32:15 +0000107#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000108#include <sys/timeb.h>
ths4fddf622007-12-17 04:42:29 +0000109#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000110#define getopt_long_only getopt_long
111#define memalign(align, size) malloc(size)
112#endif
113
bellard73332e52004-04-04 20:22:28 +0000114#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +0000115#ifdef __APPLE__
bellard83fb7ad2004-07-05 21:25:26 +0000116#include <SDL/SDL.h>
malc880fec52009-02-15 20:18:41 +0000117int qemu_main(int argc, char **argv, char **envp);
118int main(int argc, char **argv)
119{
120 qemu_main(argc, argv, NULL);
121}
122#undef main
123#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000124#endif
bellard73332e52004-04-04 20:22:28 +0000125#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000126
bellard5b0753e2005-03-01 21:37:28 +0000127#ifdef CONFIG_COCOA
128#undef main
129#define main qemu_main
130#endif /* CONFIG_COCOA */
131
blueswir1511d2b12009-03-07 15:32:56 +0000132#include "hw/hw.h"
133#include "hw/boards.h"
134#include "hw/usb.h"
135#include "hw/pcmcia.h"
136#include "hw/pc.h"
137#include "hw/audiodev.h"
138#include "hw/isa.h"
139#include "hw/baum.h"
140#include "hw/bt.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000141#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000142#include "hw/xen.h"
aurel325ef4efa2009-03-10 21:43:35 +0000143#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000144#include "net.h"
145#include "monitor.h"
146#include "console.h"
147#include "sysemu.h"
148#include "gdbstub.h"
149#include "qemu-timer.h"
150#include "qemu-char.h"
151#include "cache-utils.h"
152#include "block.h"
blueswir1a718ace2009-03-28 08:24:44 +0000153#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000154#include "audio/audio.h"
155#include "migration.h"
156#include "kvm.h"
157#include "balloon.h"
158
bellard0824d6f2003-06-24 13:42:40 +0000159#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000160
bellard8a7ddc32004-03-31 19:00:16 +0000161#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000162
blueswir1511d2b12009-03-07 15:32:56 +0000163#include "qemu_socket.h"
164
165#if defined(CONFIG_SLIRP)
166#include "libslirp.h"
167#endif
168
bellard0824d6f2003-06-24 13:42:40 +0000169//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000170//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000171//#define DEBUG_NET
172//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000173
aliguorid12d51d2009-01-15 21:48:06 +0000174
175#ifdef DEBUG_IOPORT
aliguori93fcfe32009-01-15 22:34:14 +0000176# define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
aliguorid12d51d2009-01-15 21:48:06 +0000177#else
178# define LOG_IOPORT(...) do { } while (0)
179#endif
180
bellard1bfe8562004-07-08 21:17:50 +0000181#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000182
pbrook0d92ed32006-05-21 16:30:15 +0000183/* Max number of USB devices that can be specified on the commandline. */
184#define MAX_USB_CMDLINE 8
185
balrogdc72ac12008-11-09 00:04:26 +0000186/* Max number of bluetooth switches on the commandline. */
187#define MAX_BT_CMDLINE 10
188
bellard7dea1da2003-11-16 15:59:30 +0000189/* XXX: use a two level table to limit memory usage */
190#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000191
bellard80cabfa2004-03-14 12:20:30 +0000192const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000193const char *bios_name = NULL;
blueswir1dbed7e42008-10-01 19:38:09 +0000194static void *ioport_opaque[MAX_IOPORTS];
195static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
196static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
thse4bcb142007-12-02 04:51:10 +0000197/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000198 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000199DriveInfo drives_table[MAX_DRIVES+1];
200int nb_drives;
blueswir1dbed7e42008-10-01 19:38:09 +0000201static int vga_ram_size;
malccb5a7aa2008-09-28 00:42:12 +0000202enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000203static DisplayState *display_state;
bellarda20dd502003-09-30 21:07:02 +0000204int nographic;
blueswir1dbed7e42008-10-01 19:38:09 +0000205static int curses;
aliguori7d957bd2009-01-15 22:14:11 +0000206static int sdl;
bellard3d11d0e2004-12-12 16:56:30 +0000207const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000208int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000209ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000210int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000211NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000212int vm_running;
aliguoric0f4ce72009-03-05 23:01:01 +0000213static int autostart;
balrogf6503052008-02-17 11:42:19 +0000214static int rtc_utc = 1;
215static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000216int cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +0000217int std_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000218int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000219#ifdef TARGET_SPARC
220int graphic_width = 1024;
221int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000222int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000223#else
bellard1bfe8562004-07-08 21:17:50 +0000224int graphic_width = 800;
225int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000226int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000227#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000228static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000229#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000230static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000231#endif
ths667acca2006-12-11 02:08:05 +0000232int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000233CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000234CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000235CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000236#ifdef TARGET_I386
237int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000238int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000239#endif
bellardbb36d472005-11-05 14:22:28 +0000240int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000241int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000242int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000243const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000244int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000245int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000246int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000247int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000248int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000249int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000250int graphic_rotate = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000251#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000252int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000253#endif
ths9ae02552007-01-05 17:39:04 +0000254const char *option_rom[MAX_OPTION_ROMS];
255int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000256int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000257#ifdef TARGET_ARM
258int old_param = 0;
259#endif
thsc35734b2007-03-19 15:17:08 +0000260const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000261int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000262#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000263unsigned int nb_prom_envs = 0;
264const char *prom_envs[MAX_PROM_ENVS];
265#endif
aliguoriec691c82009-02-11 15:20:37 +0000266int nb_drives_opt;
267struct drive_opt drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000268
aliguori268a3622009-04-21 22:30:27 +0000269int nb_numa_nodes;
270uint64_t node_mem[MAX_NODES];
271uint64_t node_cpumask[MAX_NODES];
272
balrogee5605e2007-12-03 03:01:40 +0000273static CPUState *cur_cpu;
274static CPUState *next_cpu;
balrog76ea08f2007-12-16 11:48:54 +0000275static int event_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000276/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000277static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000278/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000279#define MAX_ICOUNT_SHIFT 10
280/* Compensate for varying guest execution speed. */
281static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000282static QEMUTimer *icount_rt_timer;
283static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000284static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000285
blueswir18fcb1b92008-09-18 18:29:08 +0000286uint8_t qemu_uuid[16];
287
bellard0824d6f2003-06-24 13:42:40 +0000288/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000289/* x86 ISA bus support */
290
291target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000292PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000293
aliguori477e3ed2008-07-23 15:19:59 +0000294static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
295static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
296
297static uint32_t ioport_read(int index, uint32_t address)
298{
299 static IOPortReadFunc *default_func[3] = {
300 default_ioport_readb,
301 default_ioport_readw,
302 default_ioport_readl
303 };
304 IOPortReadFunc *func = ioport_read_table[index][address];
305 if (!func)
306 func = default_func[index];
307 return func(ioport_opaque[address], address);
308}
309
310static void ioport_write(int index, uint32_t address, uint32_t data)
311{
312 static IOPortWriteFunc *default_func[3] = {
313 default_ioport_writeb,
314 default_ioport_writew,
315 default_ioport_writel
316 };
317 IOPortWriteFunc *func = ioport_write_table[index][address];
318 if (!func)
319 func = default_func[index];
320 func(ioport_opaque[address], address, data);
321}
322
pbrook9596ebb2007-11-18 01:44:38 +0000323static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000324{
325#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000326 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000327#endif
bellardfc01f7e2003-06-30 10:03:06 +0000328 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000329}
330
pbrook9596ebb2007-11-18 01:44:38 +0000331static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000332{
333#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000334 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000335#endif
336}
337
338/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000339static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000340{
341 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000342 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000343 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000344 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000345 return data;
346}
347
pbrook9596ebb2007-11-18 01:44:38 +0000348static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000349{
aliguori477e3ed2008-07-23 15:19:59 +0000350 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000351 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000352 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000353}
354
pbrook9596ebb2007-11-18 01:44:38 +0000355static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000356{
357#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000358 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000359#endif
360 return 0xffffffff;
361}
362
pbrook9596ebb2007-11-18 01:44:38 +0000363static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000364{
365#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000366 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000367#endif
bellard0824d6f2003-06-24 13:42:40 +0000368}
369
bellardfc01f7e2003-06-30 10:03:06 +0000370/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000371int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000372 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000373{
bellardfc01f7e2003-06-30 10:03:06 +0000374 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000375
bellardc4b1fcc2004-03-14 21:44:30 +0000376 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000377 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000378 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000379 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000380 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000381 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000382 } else {
balrog88fdf562008-04-26 21:11:22 +0000383 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000384 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000385 }
386 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000387 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000388 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000389 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000390 ioport_opaque[i] = opaque;
391 }
bellard0824d6f2003-06-24 13:42:40 +0000392 return 0;
393}
394
bellardfc01f7e2003-06-30 10:03:06 +0000395/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000396int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000397 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000398{
bellardfc01f7e2003-06-30 10:03:06 +0000399 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000400
bellardc4b1fcc2004-03-14 21:44:30 +0000401 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000402 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000403 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000404 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000405 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000406 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000407 } else {
balrog88fdf562008-04-26 21:11:22 +0000408 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000409 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000410 }
411 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000412 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000413 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
414 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000415 ioport_opaque[i] = opaque;
416 }
bellardf1510b22003-06-25 00:07:40 +0000417 return 0;
418}
419
bellard69b91032004-05-18 23:05:28 +0000420void isa_unassign_ioport(int start, int length)
421{
422 int i;
423
424 for(i = start; i < start + length; i++) {
425 ioport_read_table[0][i] = default_ioport_readb;
426 ioport_read_table[1][i] = default_ioport_readw;
427 ioport_read_table[2][i] = default_ioport_readl;
428
429 ioport_write_table[0][i] = default_ioport_writeb;
430 ioport_write_table[1][i] = default_ioport_writew;
431 ioport_write_table[2][i] = default_ioport_writel;
aliguoria7607f72009-02-11 15:21:29 +0000432
433 ioport_opaque[i] = NULL;
bellard69b91032004-05-18 23:05:28 +0000434 }
435}
436
bellard20f32282005-01-03 23:36:21 +0000437/***********************************************************/
438
bellardc45886d2004-01-05 00:02:06 +0000439void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000440{
aliguorid12d51d2009-01-15 21:48:06 +0000441 LOG_IOPORT("outb: %04x %02x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000442 ioport_write(0, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000443#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000444 if (env)
445 env->last_io_time = cpu_get_time_fast();
446#endif
bellard0824d6f2003-06-24 13:42:40 +0000447}
448
bellardc45886d2004-01-05 00:02:06 +0000449void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000450{
aliguorid12d51d2009-01-15 21:48:06 +0000451 LOG_IOPORT("outw: %04x %04x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000452 ioport_write(1, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000453#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000454 if (env)
455 env->last_io_time = cpu_get_time_fast();
456#endif
bellard0824d6f2003-06-24 13:42:40 +0000457}
458
bellardc45886d2004-01-05 00:02:06 +0000459void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000460{
aliguorid12d51d2009-01-15 21:48:06 +0000461 LOG_IOPORT("outl: %04x %08x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000462 ioport_write(2, addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000463#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000464 if (env)
465 env->last_io_time = cpu_get_time_fast();
466#endif
bellard0824d6f2003-06-24 13:42:40 +0000467}
468
bellardc45886d2004-01-05 00:02:06 +0000469int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000470{
bellardfd872592004-05-12 19:11:15 +0000471 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000472 val = ioport_read(0, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000473 LOG_IOPORT("inb : %04x %02x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000474#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000475 if (env)
476 env->last_io_time = cpu_get_time_fast();
477#endif
bellardfd872592004-05-12 19:11:15 +0000478 return val;
bellard0824d6f2003-06-24 13:42:40 +0000479}
480
bellardc45886d2004-01-05 00:02:06 +0000481int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000482{
bellardfd872592004-05-12 19:11:15 +0000483 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000484 val = ioport_read(1, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000485 LOG_IOPORT("inw : %04x %04x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000486#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000487 if (env)
488 env->last_io_time = cpu_get_time_fast();
489#endif
bellardfd872592004-05-12 19:11:15 +0000490 return val;
bellard0824d6f2003-06-24 13:42:40 +0000491}
492
bellardc45886d2004-01-05 00:02:06 +0000493int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000494{
bellardfd872592004-05-12 19:11:15 +0000495 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000496 val = ioport_read(2, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000497 LOG_IOPORT("inl : %04x %08x\n", addr, val);
blueswir1640f42e2009-04-19 10:18:01 +0000498#ifdef CONFIG_KQEMU
bellard89bfc102006-02-08 22:46:31 +0000499 if (env)
500 env->last_io_time = cpu_get_time_fast();
501#endif
bellardfd872592004-05-12 19:11:15 +0000502 return val;
bellard0824d6f2003-06-24 13:42:40 +0000503}
504
505/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000506void hw_error(const char *fmt, ...)
507{
508 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000509 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000510
511 va_start(ap, fmt);
512 fprintf(stderr, "qemu: hardware error: ");
513 vfprintf(stderr, fmt, ap);
514 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000515 for(env = first_cpu; env != NULL; env = env->next_cpu) {
516 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000517#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000518 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000519#else
bellard6a00d602005-11-21 23:25:50 +0000520 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000521#endif
bellard6a00d602005-11-21 23:25:50 +0000522 }
bellard0824d6f2003-06-24 13:42:40 +0000523 va_end(ap);
524 abort();
525}
aliguoridf751fa2008-12-04 20:19:35 +0000526
527/***************/
528/* ballooning */
529
530static QEMUBalloonEvent *qemu_balloon_event;
531void *qemu_balloon_event_opaque;
532
533void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
534{
535 qemu_balloon_event = func;
536 qemu_balloon_event_opaque = opaque;
537}
538
539void qemu_balloon(ram_addr_t target)
540{
541 if (qemu_balloon_event)
542 qemu_balloon_event(qemu_balloon_event_opaque, target);
543}
544
545ram_addr_t qemu_balloon_status(void)
546{
547 if (qemu_balloon_event)
548 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
549 return 0;
550}
bellard0824d6f2003-06-24 13:42:40 +0000551
bellard8a7ddc32004-03-31 19:00:16 +0000552/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000553/* keyboard/mouse */
554
555static QEMUPutKBDEvent *qemu_put_kbd_event;
556static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000557static QEMUPutMouseEntry *qemu_put_mouse_event_head;
558static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000559
560void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
561{
562 qemu_put_kbd_event_opaque = opaque;
563 qemu_put_kbd_event = func;
564}
565
ths455204e2007-01-05 16:42:13 +0000566QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
567 void *opaque, int absolute,
568 const char *name)
bellard63066f42004-06-03 18:45:02 +0000569{
ths455204e2007-01-05 16:42:13 +0000570 QEMUPutMouseEntry *s, *cursor;
571
572 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000573
574 s->qemu_put_mouse_event = func;
575 s->qemu_put_mouse_event_opaque = opaque;
576 s->qemu_put_mouse_event_absolute = absolute;
577 s->qemu_put_mouse_event_name = qemu_strdup(name);
578 s->next = NULL;
579
580 if (!qemu_put_mouse_event_head) {
581 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
582 return s;
583 }
584
585 cursor = qemu_put_mouse_event_head;
586 while (cursor->next != NULL)
587 cursor = cursor->next;
588
589 cursor->next = s;
590 qemu_put_mouse_event_current = s;
591
592 return s;
593}
594
595void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
596{
597 QEMUPutMouseEntry *prev = NULL, *cursor;
598
599 if (!qemu_put_mouse_event_head || entry == NULL)
600 return;
601
602 cursor = qemu_put_mouse_event_head;
603 while (cursor != NULL && cursor != entry) {
604 prev = cursor;
605 cursor = cursor->next;
606 }
607
608 if (cursor == NULL) // does not exist or list empty
609 return;
610 else if (prev == NULL) { // entry is head
611 qemu_put_mouse_event_head = cursor->next;
612 if (qemu_put_mouse_event_current == entry)
613 qemu_put_mouse_event_current = cursor->next;
614 qemu_free(entry->qemu_put_mouse_event_name);
615 qemu_free(entry);
616 return;
617 }
618
619 prev->next = entry->next;
620
621 if (qemu_put_mouse_event_current == entry)
622 qemu_put_mouse_event_current = prev;
623
624 qemu_free(entry->qemu_put_mouse_event_name);
625 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000626}
627
628void kbd_put_keycode(int keycode)
629{
630 if (qemu_put_kbd_event) {
631 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
632 }
633}
634
635void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
636{
ths455204e2007-01-05 16:42:13 +0000637 QEMUPutMouseEvent *mouse_event;
638 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000639 int width;
ths455204e2007-01-05 16:42:13 +0000640
641 if (!qemu_put_mouse_event_current) {
642 return;
643 }
644
645 mouse_event =
646 qemu_put_mouse_event_current->qemu_put_mouse_event;
647 mouse_event_opaque =
648 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
649
650 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000651 if (graphic_rotate) {
652 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
653 width = 0x7fff;
654 else
aurel32b94ed572008-03-10 19:34:27 +0000655 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000656 mouse_event(mouse_event_opaque,
657 width - dy, dx, dz, buttons_state);
658 } else
659 mouse_event(mouse_event_opaque,
660 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000661 }
662}
663
bellard09b26c52006-04-12 21:09:08 +0000664int kbd_mouse_is_absolute(void)
665{
ths455204e2007-01-05 16:42:13 +0000666 if (!qemu_put_mouse_event_current)
667 return 0;
668
669 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
670}
671
aliguori376253e2009-03-05 23:01:23 +0000672void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000673{
674 QEMUPutMouseEntry *cursor;
675 int index = 0;
676
677 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000678 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000679 return;
680 }
681
aliguori376253e2009-03-05 23:01:23 +0000682 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000683 cursor = qemu_put_mouse_event_head;
684 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000685 monitor_printf(mon, "%c Mouse #%d: %s\n",
686 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
687 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000688 index++;
689 cursor = cursor->next;
690 }
691}
692
aliguori376253e2009-03-05 23:01:23 +0000693void do_mouse_set(Monitor *mon, int index)
ths455204e2007-01-05 16:42:13 +0000694{
695 QEMUPutMouseEntry *cursor;
696 int i = 0;
697
698 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000699 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000700 return;
701 }
702
703 cursor = qemu_put_mouse_event_head;
704 while (cursor != NULL && index != i) {
705 i++;
706 cursor = cursor->next;
707 }
708
709 if (cursor != NULL)
710 qemu_put_mouse_event_current = cursor;
711 else
aliguori376253e2009-03-05 23:01:23 +0000712 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000713}
714
bellard87858c82003-06-27 12:01:39 +0000715/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000716uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000717{
718 union {
719 uint64_t ll;
720 struct {
721#ifdef WORDS_BIGENDIAN
722 uint32_t high, low;
723#else
724 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000725#endif
bellard87858c82003-06-27 12:01:39 +0000726 } l;
727 } u, res;
728 uint64_t rl, rh;
729
730 u.ll = a;
731 rl = (uint64_t)u.l.low * (uint64_t)b;
732 rh = (uint64_t)u.l.high * (uint64_t)b;
733 rh += (rl >> 32);
734 res.l.high = rh / c;
735 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
736 return res.ll;
737}
738
bellard1dce7c32006-07-13 23:20:22 +0000739/***********************************************************/
740/* real time host monotonic timer */
741
742#define QEMU_TIMER_BASE 1000000000LL
743
744#ifdef WIN32
745
746static int64_t clock_freq;
747
748static void init_get_clock(void)
749{
bellarda8e5ac32006-07-14 09:36:13 +0000750 LARGE_INTEGER freq;
751 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000752 ret = QueryPerformanceFrequency(&freq);
753 if (ret == 0) {
754 fprintf(stderr, "Could not calibrate ticks\n");
755 exit(1);
756 }
757 clock_freq = freq.QuadPart;
758}
759
760static int64_t get_clock(void)
761{
762 LARGE_INTEGER ti;
763 QueryPerformanceCounter(&ti);
764 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
765}
766
767#else
768
769static int use_rt_clock;
770
771static void init_get_clock(void)
772{
773 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000774#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
775 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000776 {
777 struct timespec ts;
778 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
779 use_rt_clock = 1;
780 }
781 }
782#endif
783}
784
785static int64_t get_clock(void)
786{
blueswir1c5e97232009-03-07 20:06:23 +0000787#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
788 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000789 if (use_rt_clock) {
790 struct timespec ts;
791 clock_gettime(CLOCK_MONOTONIC, &ts);
792 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000793 } else
bellard1dce7c32006-07-13 23:20:22 +0000794#endif
795 {
796 /* XXX: using gettimeofday leads to problems if the date
797 changes, so it should be avoided. */
798 struct timeval tv;
799 gettimeofday(&tv, NULL);
800 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
801 }
802}
bellard1dce7c32006-07-13 23:20:22 +0000803#endif
804
pbrook2e70f6e2008-06-29 01:03:05 +0000805/* Return the virtual CPU time, based on the instruction counter. */
806static int64_t cpu_get_icount(void)
807{
808 int64_t icount;
809 CPUState *env = cpu_single_env;;
810 icount = qemu_icount;
811 if (env) {
812 if (!can_do_io(env))
813 fprintf(stderr, "Bad clock read\n");
814 icount -= (env->icount_decr.u16.low + env->icount_extra);
815 }
816 return qemu_icount_bias + (icount << icount_time_shift);
817}
818
bellard1dce7c32006-07-13 23:20:22 +0000819/***********************************************************/
820/* guest cycle counter */
821
822static int64_t cpu_ticks_prev;
823static int64_t cpu_ticks_offset;
824static int64_t cpu_clock_offset;
825static int cpu_ticks_enabled;
826
827/* return the host CPU cycle counter and handle stop/restart */
828int64_t cpu_get_ticks(void)
829{
pbrook2e70f6e2008-06-29 01:03:05 +0000830 if (use_icount) {
831 return cpu_get_icount();
832 }
bellard1dce7c32006-07-13 23:20:22 +0000833 if (!cpu_ticks_enabled) {
834 return cpu_ticks_offset;
835 } else {
836 int64_t ticks;
837 ticks = cpu_get_real_ticks();
838 if (cpu_ticks_prev > ticks) {
839 /* Note: non increasing ticks may happen if the host uses
840 software suspend */
841 cpu_ticks_offset += cpu_ticks_prev - ticks;
842 }
843 cpu_ticks_prev = ticks;
844 return ticks + cpu_ticks_offset;
845 }
846}
847
848/* return the host CPU monotonic timer and handle stop/restart */
849static int64_t cpu_get_clock(void)
850{
851 int64_t ti;
852 if (!cpu_ticks_enabled) {
853 return cpu_clock_offset;
854 } else {
855 ti = get_clock();
856 return ti + cpu_clock_offset;
857 }
858}
859
860/* enable cpu_get_ticks() */
861void cpu_enable_ticks(void)
862{
863 if (!cpu_ticks_enabled) {
864 cpu_ticks_offset -= cpu_get_real_ticks();
865 cpu_clock_offset -= get_clock();
866 cpu_ticks_enabled = 1;
867 }
868}
869
870/* disable cpu_get_ticks() : the clock is stopped. You must not call
871 cpu_get_ticks() after that. */
872void cpu_disable_ticks(void)
873{
874 if (cpu_ticks_enabled) {
875 cpu_ticks_offset = cpu_get_ticks();
876 cpu_clock_offset = cpu_get_clock();
877 cpu_ticks_enabled = 0;
878 }
879}
880
881/***********************************************************/
882/* timers */
ths5fafdf22007-09-16 21:08:06 +0000883
bellard8a7ddc32004-03-31 19:00:16 +0000884#define QEMU_TIMER_REALTIME 0
885#define QEMU_TIMER_VIRTUAL 1
886
887struct QEMUClock {
888 int type;
889 /* XXX: add frequency */
890};
891
892struct QEMUTimer {
893 QEMUClock *clock;
894 int64_t expire_time;
895 QEMUTimerCB *cb;
896 void *opaque;
897 struct QEMUTimer *next;
898};
899
thsc8994012007-08-19 21:56:03 +0000900struct qemu_alarm_timer {
901 char const *name;
thsefe75412007-08-24 01:36:32 +0000902 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000903
904 int (*start)(struct qemu_alarm_timer *t);
905 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000906 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000907 void *priv;
908};
909
thsefe75412007-08-24 01:36:32 +0000910#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000911#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000912
913static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
914{
915 return t->flags & ALARM_FLAG_DYNTICKS;
916}
917
918static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
919{
920 if (!alarm_has_dynticks(t))
921 return;
922
923 t->rearm(t);
924}
925
926/* TODO: MIN_TIMER_REARM_US should be optimized */
927#define MIN_TIMER_REARM_US 250
928
thsc8994012007-08-19 21:56:03 +0000929static struct qemu_alarm_timer *alarm_timer;
aliguorif49e58d2008-11-05 21:22:34 +0000930#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +0000931static int alarm_timer_rfd, alarm_timer_wfd;
aliguorif49e58d2008-11-05 21:22:34 +0000932#endif
thsc8994012007-08-19 21:56:03 +0000933
934#ifdef _WIN32
935
936struct qemu_alarm_win32 {
937 MMRESULT timerId;
938 HANDLE host_alarm;
939 unsigned int period;
940} alarm_win32_data = {0, NULL, -1};
941
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. */
1195 if (use_icount && cpu_single_env) {
aurel323098dba2009-03-07 21:28:24 +00001196 cpu_exit(cpu_single_env);
pbrook2e70f6e2008-06-29 01:03:05 +00001197 }
1198 }
bellard8a7ddc32004-03-31 19:00:16 +00001199}
1200
1201int qemu_timer_pending(QEMUTimer *ts)
1202{
1203 QEMUTimer *t;
1204 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1205 if (t == ts)
1206 return 1;
1207 }
1208 return 0;
1209}
1210
1211static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1212{
1213 if (!timer_head)
1214 return 0;
1215 return (timer_head->expire_time <= current_time);
1216}
1217
1218static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1219{
1220 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001221
bellard8a7ddc32004-03-31 19:00:16 +00001222 for(;;) {
1223 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001224 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001225 break;
1226 /* remove timer from the list before calling the callback */
1227 *ptimer_head = ts->next;
1228 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001229
bellard8a7ddc32004-03-31 19:00:16 +00001230 /* run the callback (the timer list can be modified) */
1231 ts->cb(ts->opaque);
1232 }
1233}
1234
1235int64_t qemu_get_clock(QEMUClock *clock)
1236{
1237 switch(clock->type) {
1238 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001239 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001240 default:
1241 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001242 if (use_icount) {
1243 return cpu_get_icount();
1244 } else {
1245 return cpu_get_clock();
1246 }
bellard8a7ddc32004-03-31 19:00:16 +00001247 }
1248}
1249
bellard1dce7c32006-07-13 23:20:22 +00001250static void init_timers(void)
1251{
1252 init_get_clock();
1253 ticks_per_sec = QEMU_TIMER_BASE;
1254 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1255 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1256}
1257
bellard8a7ddc32004-03-31 19:00:16 +00001258/* save a timer */
1259void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1260{
1261 uint64_t expire_time;
1262
1263 if (qemu_timer_pending(ts)) {
1264 expire_time = ts->expire_time;
1265 } else {
1266 expire_time = -1;
1267 }
1268 qemu_put_be64(f, expire_time);
1269}
1270
1271void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1272{
1273 uint64_t expire_time;
1274
1275 expire_time = qemu_get_be64(f);
1276 if (expire_time != -1) {
1277 qemu_mod_timer(ts, expire_time);
1278 } else {
1279 qemu_del_timer(ts);
1280 }
1281}
1282
1283static void timer_save(QEMUFile *f, void *opaque)
1284{
1285 if (cpu_ticks_enabled) {
1286 hw_error("cannot save state if virtual timers are running");
1287 }
thsbee8d682007-12-16 23:41:11 +00001288 qemu_put_be64(f, cpu_ticks_offset);
1289 qemu_put_be64(f, ticks_per_sec);
1290 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001291}
1292
1293static int timer_load(QEMUFile *f, void *opaque, int version_id)
1294{
bellardc88676f2006-08-06 13:36:11 +00001295 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001296 return -EINVAL;
1297 if (cpu_ticks_enabled) {
1298 return -EINVAL;
1299 }
thsbee8d682007-12-16 23:41:11 +00001300 cpu_ticks_offset=qemu_get_be64(f);
1301 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001302 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001303 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001304 }
bellard8a7ddc32004-03-31 19:00:16 +00001305 return 0;
1306}
1307
bellard67b915a2004-03-31 23:37:16 +00001308#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001309static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1310 DWORD_PTR dwUser, DWORD_PTR dw1,
1311 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001312#else
bellard8a7ddc32004-03-31 19:00:16 +00001313static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001314#endif
bellard8a7ddc32004-03-31 19:00:16 +00001315{
bellard02ba45c2004-06-25 14:46:23 +00001316#if 0
1317#define DISP_FREQ 1000
1318 {
1319 static int64_t delta_min = INT64_MAX;
1320 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1321 static int count;
1322 ti = qemu_get_clock(vm_clock);
1323 if (last_clock != 0) {
1324 delta = ti - last_clock;
1325 if (delta < delta_min)
1326 delta_min = delta;
1327 if (delta > delta_max)
1328 delta_max = delta;
1329 delta_cum += delta;
1330 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001331 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001332 muldiv64(delta_min, 1000000, ticks_per_sec),
1333 muldiv64(delta_max, 1000000, ticks_per_sec),
1334 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1335 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1336 count = 0;
1337 delta_min = INT64_MAX;
1338 delta_max = 0;
1339 delta_cum = 0;
1340 }
1341 }
1342 last_clock = ti;
1343 }
1344#endif
thsefe75412007-08-24 01:36:32 +00001345 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001346 (!use_icount &&
1347 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1348 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001349 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1350 qemu_get_clock(rt_clock))) {
aliguoric96f1a42008-11-05 20:29:45 +00001351 CPUState *env = next_cpu;
aliguoric96f1a42008-11-05 20:29:45 +00001352
bellard06d9f2f2006-05-01 13:23:04 +00001353#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001354 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1355 SetEvent(data->host_alarm);
aliguorif49e58d2008-11-05 21:22:34 +00001356#else
1357 static const char byte = 0;
aliguoric96f1a42008-11-05 20:29:45 +00001358 write(alarm_timer_wfd, &byte, sizeof(byte));
aliguorif49e58d2008-11-05 21:22:34 +00001359#endif
balrogd5d08332008-01-05 19:41:47 +00001360 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1361
balrog4f8eb8d2007-12-16 12:39:38 +00001362 if (env) {
1363 /* stop the currently executing cpu because a timer occured */
aurel323098dba2009-03-07 21:28:24 +00001364 cpu_exit(env);
blueswir1640f42e2009-04-19 10:18:01 +00001365#ifdef CONFIG_KQEMU
balrog4f8eb8d2007-12-16 12:39:38 +00001366 if (env->kqemu_enabled) {
1367 kqemu_cpu_interrupt(env);
1368 }
balrogee5605e2007-12-03 03:01:40 +00001369#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001370 }
balrogee5605e2007-12-03 03:01:40 +00001371 event_pending = 1;
bellard8a7ddc32004-03-31 19:00:16 +00001372 }
1373}
1374
pbrook2e70f6e2008-06-29 01:03:05 +00001375static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001376{
pbrook2e70f6e2008-06-29 01:03:05 +00001377 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001378
1379 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001380 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1381 qemu_get_clock(vm_clock);
1382 } else {
1383 /* To avoid problems with overflow limit this to 2^32. */
1384 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001385 }
1386
pbrook2e70f6e2008-06-29 01:03:05 +00001387 if (delta < 0)
1388 delta = 0;
thsefe75412007-08-24 01:36:32 +00001389
pbrook2e70f6e2008-06-29 01:03:05 +00001390 return delta;
1391}
1392
blueswir18632fb92008-09-14 13:59:34 +00001393#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001394static uint64_t qemu_next_deadline_dyntick(void)
1395{
1396 int64_t delta;
1397 int64_t rtdelta;
1398
1399 if (use_icount)
1400 delta = INT32_MAX;
1401 else
1402 delta = (qemu_next_deadline() + 999) / 1000;
1403
1404 if (active_timers[QEMU_TIMER_REALTIME]) {
1405 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1406 qemu_get_clock(rt_clock))*1000;
1407 if (rtdelta < delta)
1408 delta = rtdelta;
1409 }
1410
1411 if (delta < MIN_TIMER_REARM_US)
1412 delta = MIN_TIMER_REARM_US;
1413
1414 return delta;
thsefe75412007-08-24 01:36:32 +00001415}
blueswir18632fb92008-09-14 13:59:34 +00001416#endif
thsefe75412007-08-24 01:36:32 +00001417
bellardfd872592004-05-12 19:11:15 +00001418#ifndef _WIN32
1419
aliguori7183b4b2008-11-05 20:40:18 +00001420/* Sets a specific flag */
1421static int fcntl_setfl(int fd, int flag)
1422{
1423 int flags;
1424
1425 flags = fcntl(fd, F_GETFL);
1426 if (flags == -1)
1427 return -errno;
1428
1429 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1430 return -errno;
1431
1432 return 0;
1433}
1434
bellard829309c2004-05-20 13:20:12 +00001435#if defined(__linux__)
1436
bellardfd872592004-05-12 19:11:15 +00001437#define RTC_FREQ 1024
1438
aurel32de9a95f2008-11-11 13:41:01 +00001439static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001440{
thsc8994012007-08-19 21:56:03 +00001441 struct sigaction act;
1442
1443 /* timer signal */
1444 sigfillset(&act.sa_mask);
1445 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001446 act.sa_handler = host_alarm_handler;
1447
1448 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001449 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001450 fcntl(fd, F_SETOWN, getpid());
1451}
1452
thsc40ec5a2007-08-19 22:09:40 +00001453static int hpet_start_timer(struct qemu_alarm_timer *t)
1454{
1455 struct hpet_info info;
1456 int r, fd;
1457
1458 fd = open("/dev/hpet", O_RDONLY);
1459 if (fd < 0)
1460 return -1;
1461
1462 /* Set frequency */
1463 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1464 if (r < 0) {
1465 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1466 "error, but for better emulation accuracy type:\n"
1467 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1468 goto fail;
1469 }
1470
1471 /* Check capabilities */
1472 r = ioctl(fd, HPET_INFO, &info);
1473 if (r < 0)
1474 goto fail;
1475
1476 /* Enable periodic mode */
1477 r = ioctl(fd, HPET_EPI, 0);
1478 if (info.hi_flags && (r < 0))
1479 goto fail;
1480
1481 /* Enable interrupt */
1482 r = ioctl(fd, HPET_IE_ON, 0);
1483 if (r < 0)
1484 goto fail;
1485
1486 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001487 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001488
1489 return 0;
1490fail:
1491 close(fd);
1492 return -1;
1493}
1494
1495static void hpet_stop_timer(struct qemu_alarm_timer *t)
1496{
pbrookfcdc2122007-08-23 20:22:22 +00001497 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001498
1499 close(fd);
1500}
1501
thsc8994012007-08-19 21:56:03 +00001502static int rtc_start_timer(struct qemu_alarm_timer *t)
1503{
1504 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001505 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001506
balrogaeb30be2007-07-02 15:03:13 +00001507 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001508 if (rtc_fd < 0)
1509 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001510 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1511 if (current_rtc_freq != RTC_FREQ &&
1512 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001513 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1514 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1515 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1516 goto fail;
1517 }
1518 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1519 fail:
1520 close(rtc_fd);
1521 return -1;
1522 }
thsc8994012007-08-19 21:56:03 +00001523
1524 enable_sigio_timer(rtc_fd);
1525
pbrookfcdc2122007-08-23 20:22:22 +00001526 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001527
bellardfd872592004-05-12 19:11:15 +00001528 return 0;
1529}
1530
thsc8994012007-08-19 21:56:03 +00001531static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001532{
pbrookfcdc2122007-08-23 20:22:22 +00001533 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001534
1535 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001536}
1537
thsefe75412007-08-24 01:36:32 +00001538static int dynticks_start_timer(struct qemu_alarm_timer *t)
1539{
1540 struct sigevent ev;
1541 timer_t host_timer;
1542 struct sigaction act;
1543
1544 sigfillset(&act.sa_mask);
1545 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001546 act.sa_handler = host_alarm_handler;
1547
1548 sigaction(SIGALRM, &act, NULL);
1549
1550 ev.sigev_value.sival_int = 0;
1551 ev.sigev_notify = SIGEV_SIGNAL;
1552 ev.sigev_signo = SIGALRM;
1553
1554 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1555 perror("timer_create");
1556
1557 /* disable dynticks */
1558 fprintf(stderr, "Dynamic Ticks disabled\n");
1559
1560 return -1;
1561 }
1562
blueswir10399bfe2008-11-16 11:37:18 +00001563 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001564
1565 return 0;
1566}
1567
1568static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1569{
blueswir10399bfe2008-11-16 11:37:18 +00001570 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001571
1572 timer_delete(host_timer);
1573}
1574
1575static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1576{
blueswir10399bfe2008-11-16 11:37:18 +00001577 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001578 struct itimerspec timeout;
1579 int64_t nearest_delta_us = INT64_MAX;
1580 int64_t current_us;
1581
1582 if (!active_timers[QEMU_TIMER_REALTIME] &&
1583 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001584 return;
thsefe75412007-08-24 01:36:32 +00001585
pbrook2e70f6e2008-06-29 01:03:05 +00001586 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001587
1588 /* check whether a timer is already running */
1589 if (timer_gettime(host_timer, &timeout)) {
1590 perror("gettime");
1591 fprintf(stderr, "Internal timer error: aborting\n");
1592 exit(1);
1593 }
1594 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1595 if (current_us && current_us <= nearest_delta_us)
1596 return;
1597
1598 timeout.it_interval.tv_sec = 0;
1599 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1600 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1601 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1602 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1603 perror("settime");
1604 fprintf(stderr, "Internal timer error: aborting\n");
1605 exit(1);
1606 }
1607}
1608
ths70744b32007-08-26 17:31:30 +00001609#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001610
thsc8994012007-08-19 21:56:03 +00001611static int unix_start_timer(struct qemu_alarm_timer *t)
1612{
1613 struct sigaction act;
1614 struct itimerval itv;
1615 int err;
1616
1617 /* timer signal */
1618 sigfillset(&act.sa_mask);
1619 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001620 act.sa_handler = host_alarm_handler;
1621
1622 sigaction(SIGALRM, &act, NULL);
1623
1624 itv.it_interval.tv_sec = 0;
1625 /* for i386 kernel 2.6 to get 1 ms */
1626 itv.it_interval.tv_usec = 999;
1627 itv.it_value.tv_sec = 0;
1628 itv.it_value.tv_usec = 10 * 1000;
1629
1630 err = setitimer(ITIMER_REAL, &itv, NULL);
1631 if (err)
1632 return -1;
1633
1634 return 0;
1635}
1636
1637static void unix_stop_timer(struct qemu_alarm_timer *t)
1638{
1639 struct itimerval itv;
1640
1641 memset(&itv, 0, sizeof(itv));
1642 setitimer(ITIMER_REAL, &itv, NULL);
1643}
1644
bellard829309c2004-05-20 13:20:12 +00001645#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001646
aliguorif49e58d2008-11-05 21:22:34 +00001647static void try_to_rearm_timer(void *opaque)
1648{
1649 struct qemu_alarm_timer *t = opaque;
1650#ifndef _WIN32
1651 ssize_t len;
1652
1653 /* Drain the notify pipe */
1654 do {
1655 char buffer[512];
1656 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1657 } while ((len == -1 && errno == EINTR) || len > 0);
1658#endif
1659
aliguorif49e58d2008-11-05 21:22:34 +00001660 if (t->flags & ALARM_FLAG_EXPIRED) {
1661 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1662 qemu_rearm_alarm_timer(alarm_timer);
1663 }
1664}
1665
thsc8994012007-08-19 21:56:03 +00001666#ifdef _WIN32
1667
1668static int win32_start_timer(struct qemu_alarm_timer *t)
1669{
1670 TIMECAPS tc;
1671 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001672 UINT flags;
thsc8994012007-08-19 21:56:03 +00001673
1674 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1675 if (!data->host_alarm) {
1676 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001677 return -1;
thsc8994012007-08-19 21:56:03 +00001678 }
1679
1680 memset(&tc, 0, sizeof(tc));
1681 timeGetDevCaps(&tc, sizeof(tc));
1682
1683 if (data->period < tc.wPeriodMin)
1684 data->period = tc.wPeriodMin;
1685
1686 timeBeginPeriod(data->period);
1687
thsefe75412007-08-24 01:36:32 +00001688 flags = TIME_CALLBACK_FUNCTION;
1689 if (alarm_has_dynticks(t))
1690 flags |= TIME_ONESHOT;
1691 else
1692 flags |= TIME_PERIODIC;
1693
thsc8994012007-08-19 21:56:03 +00001694 data->timerId = timeSetEvent(1, // interval (ms)
1695 data->period, // resolution
1696 host_alarm_handler, // function
1697 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001698 flags);
thsc8994012007-08-19 21:56:03 +00001699
1700 if (!data->timerId) {
1701 perror("Failed to initialize win32 alarm timer");
1702
1703 timeEndPeriod(data->period);
1704 CloseHandle(data->host_alarm);
1705 return -1;
1706 }
1707
aliguorif49e58d2008-11-05 21:22:34 +00001708 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
thsc8994012007-08-19 21:56:03 +00001709
1710 return 0;
1711}
1712
1713static void win32_stop_timer(struct qemu_alarm_timer *t)
1714{
1715 struct qemu_alarm_win32 *data = t->priv;
1716
1717 timeKillEvent(data->timerId);
1718 timeEndPeriod(data->period);
1719
1720 CloseHandle(data->host_alarm);
1721}
1722
thsefe75412007-08-24 01:36:32 +00001723static void win32_rearm_timer(struct qemu_alarm_timer *t)
1724{
1725 struct qemu_alarm_win32 *data = t->priv;
1726 uint64_t nearest_delta_us;
1727
1728 if (!active_timers[QEMU_TIMER_REALTIME] &&
1729 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001730 return;
thsefe75412007-08-24 01:36:32 +00001731
pbrook2e70f6e2008-06-29 01:03:05 +00001732 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001733 nearest_delta_us /= 1000;
1734
1735 timeKillEvent(data->timerId);
1736
1737 data->timerId = timeSetEvent(1,
1738 data->period,
1739 host_alarm_handler,
1740 (DWORD)t,
1741 TIME_ONESHOT | TIME_PERIODIC);
1742
1743 if (!data->timerId) {
1744 perror("Failed to re-arm win32 alarm timer");
1745
1746 timeEndPeriod(data->period);
1747 CloseHandle(data->host_alarm);
1748 exit(1);
1749 }
1750}
1751
thsc8994012007-08-19 21:56:03 +00001752#endif /* _WIN32 */
1753
aliguori7183b4b2008-11-05 20:40:18 +00001754static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001755{
blueswir1223f0d72008-09-30 18:12:18 +00001756 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001757 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001758
1759#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +00001760 int fds[2];
1761
aliguori7183b4b2008-11-05 20:40:18 +00001762 err = pipe(fds);
1763 if (err == -1)
1764 return -errno;
1765
1766 err = fcntl_setfl(fds[0], O_NONBLOCK);
1767 if (err < 0)
1768 goto fail;
1769
1770 err = fcntl_setfl(fds[1], O_NONBLOCK);
1771 if (err < 0)
1772 goto fail;
1773
aliguoric96f1a42008-11-05 20:29:45 +00001774 alarm_timer_rfd = fds[0];
1775 alarm_timer_wfd = fds[1];
aliguorif49e58d2008-11-05 21:22:34 +00001776#endif
bellard06d9f2f2006-05-01 13:23:04 +00001777
thsc8994012007-08-19 21:56:03 +00001778 for (i = 0; alarm_timers[i].name; i++) {
1779 t = &alarm_timers[i];
1780
thsc8994012007-08-19 21:56:03 +00001781 err = t->start(t);
1782 if (!err)
1783 break;
bellard67b915a2004-03-31 23:37:16 +00001784 }
bellardfd872592004-05-12 19:11:15 +00001785
thsc8994012007-08-19 21:56:03 +00001786 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001787 err = -ENOENT;
1788 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001789 }
thsc8994012007-08-19 21:56:03 +00001790
aliguorif49e58d2008-11-05 21:22:34 +00001791#ifndef _WIN32
aliguori6abfbd72008-11-05 20:49:37 +00001792 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1793 try_to_rearm_timer, NULL, t);
aliguorif49e58d2008-11-05 21:22:34 +00001794#endif
aliguori6abfbd72008-11-05 20:49:37 +00001795
thsc8994012007-08-19 21:56:03 +00001796 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001797
aliguori6abfbd72008-11-05 20:49:37 +00001798 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001799
1800fail:
aliguorif49e58d2008-11-05 21:22:34 +00001801#ifndef _WIN32
aliguori7183b4b2008-11-05 20:40:18 +00001802 close(fds[0]);
1803 close(fds[1]);
aliguorif49e58d2008-11-05 21:22:34 +00001804#endif
aliguori7183b4b2008-11-05 20:40:18 +00001805 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001806}
1807
pbrook9596ebb2007-11-18 01:44:38 +00001808static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001809{
thsc8994012007-08-19 21:56:03 +00001810 alarm_timer->stop(alarm_timer);
1811 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001812}
1813
bellardc4b1fcc2004-03-14 21:44:30 +00001814/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001815/* host time/date access */
1816void qemu_get_timedate(struct tm *tm, int offset)
1817{
1818 time_t ti;
1819 struct tm *ret;
1820
1821 time(&ti);
1822 ti += offset;
1823 if (rtc_date_offset == -1) {
1824 if (rtc_utc)
1825 ret = gmtime(&ti);
1826 else
1827 ret = localtime(&ti);
1828 } else {
1829 ti -= rtc_date_offset;
1830 ret = gmtime(&ti);
1831 }
1832
1833 memcpy(tm, ret, sizeof(struct tm));
1834}
1835
1836int qemu_timedate_diff(struct tm *tm)
1837{
1838 time_t seconds;
1839
1840 if (rtc_date_offset == -1)
1841 if (rtc_utc)
1842 seconds = mktimegm(tm);
1843 else
1844 seconds = mktime(tm);
1845 else
1846 seconds = mktimegm(tm) + rtc_date_offset;
1847
1848 return seconds - time(NULL);
1849}
1850
bellardfd1dff42006-02-01 21:29:26 +00001851#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001852static void socket_cleanup(void)
1853{
1854 WSACleanup();
1855}
bellard82c643f2004-07-14 17:28:13 +00001856
bellardfd1dff42006-02-01 21:29:26 +00001857static int socket_init(void)
1858{
1859 WSADATA Data;
1860 int ret, err;
1861
1862 ret = WSAStartup(MAKEWORD(2,2), &Data);
1863 if (ret != 0) {
1864 err = WSAGetLastError();
1865 fprintf(stderr, "WSAStartup: %d\n", err);
1866 return -1;
1867 }
1868 atexit(socket_cleanup);
1869 return 0;
1870}
aurel3264b7b732008-05-05 10:05:31 +00001871#endif
1872
aliguori268a3622009-04-21 22:30:27 +00001873const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
thse4bcb142007-12-02 04:51:10 +00001874{
1875 char *q;
thse4bcb142007-12-02 04:51:10 +00001876
balrog609497a2008-01-14 02:56:53 +00001877 q = buf;
aliguori268a3622009-04-21 22:30:27 +00001878 while (*p != '\0' && *p != delim) {
balrog609497a2008-01-14 02:56:53 +00001879 if (q && (q - buf) < buf_size - 1)
1880 *q++ = *p;
1881 p++;
1882 }
1883 if (q)
1884 *q = '\0';
1885
1886 return p;
1887}
1888
aliguori63a01ef2008-10-31 19:10:00 +00001889const char *get_opt_value(char *buf, int buf_size, const char *p)
balrog609497a2008-01-14 02:56:53 +00001890{
1891 char *q;
1892
thse4bcb142007-12-02 04:51:10 +00001893 q = buf;
1894 while (*p != '\0') {
balrog609497a2008-01-14 02:56:53 +00001895 if (*p == ',') {
1896 if (*(p + 1) != ',')
thse4bcb142007-12-02 04:51:10 +00001897 break;
thse4bcb142007-12-02 04:51:10 +00001898 p++;
balrog609497a2008-01-14 02:56:53 +00001899 }
thse4bcb142007-12-02 04:51:10 +00001900 if (q && (q - buf) < buf_size - 1)
1901 *q++ = *p;
1902 p++;
1903 }
1904 if (q)
1905 *q = '\0';
1906
1907 return p;
1908}
1909
aliguori63a01ef2008-10-31 19:10:00 +00001910int get_param_value(char *buf, int buf_size,
1911 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001912{
1913 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001914 char option[128];
1915
1916 p = str;
1917 for(;;) {
aliguori268a3622009-04-21 22:30:27 +00001918 p = get_opt_name(option, sizeof(option), p, '=');
bellard7c9d8e02005-11-15 22:16:05 +00001919 if (*p != '=')
1920 break;
1921 p++;
1922 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001923 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001924 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001925 } else {
balrog609497a2008-01-14 02:56:53 +00001926 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001927 }
1928 if (*p != ',')
1929 break;
1930 p++;
1931 }
1932 return 0;
1933}
1934
aliguori63a01ef2008-10-31 19:10:00 +00001935int check_params(char *buf, int buf_size,
1936 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001937{
1938 const char *p;
1939 int i;
1940
1941 p = str;
aliguori10300212009-04-21 19:56:23 +00001942 while (*p != '\0') {
aliguori268a3622009-04-21 22:30:27 +00001943 p = get_opt_name(buf, buf_size, p, '=');
thse4bcb142007-12-02 04:51:10 +00001944 if (*p != '=')
1945 return -1;
1946 p++;
1947 for(i = 0; params[i] != NULL; i++)
1948 if (!strcmp(params[i], buf))
1949 break;
1950 if (params[i] == NULL)
1951 return -1;
balrog609497a2008-01-14 02:56:53 +00001952 p = get_opt_value(NULL, 0, p);
thse4bcb142007-12-02 04:51:10 +00001953 if (*p != ',')
1954 break;
1955 p++;
1956 }
1957 return 0;
1958}
1959
balrog1ae26a12008-09-28 23:19:47 +00001960/***********************************************************/
1961/* Bluetooth support */
1962static int nb_hcis;
1963static int cur_hci;
1964static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001965
balrog1ae26a12008-09-28 23:19:47 +00001966static struct bt_vlan_s {
1967 struct bt_scatternet_s net;
1968 int id;
1969 struct bt_vlan_s *next;
1970} *first_bt_vlan;
1971
1972/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001973static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001974{
1975 struct bt_vlan_s **pvlan, *vlan;
1976 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1977 if (vlan->id == id)
1978 return &vlan->net;
1979 }
1980 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1981 vlan->id = id;
1982 pvlan = &first_bt_vlan;
1983 while (*pvlan != NULL)
1984 pvlan = &(*pvlan)->next;
1985 *pvlan = vlan;
1986 return &vlan->net;
1987}
1988
1989static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1990{
1991}
1992
1993static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1994{
1995 return -ENOTSUP;
1996}
1997
1998static struct HCIInfo null_hci = {
1999 .cmd_send = null_hci_send,
2000 .sco_send = null_hci_send,
2001 .acl_send = null_hci_send,
2002 .bdaddr_set = null_hci_addr_set,
2003};
2004
2005struct HCIInfo *qemu_next_hci(void)
2006{
2007 if (cur_hci == nb_hcis)
2008 return &null_hci;
2009
2010 return hci_table[cur_hci++];
2011}
2012
balrogdc72ac12008-11-09 00:04:26 +00002013static struct HCIInfo *hci_init(const char *str)
2014{
2015 char *endp;
2016 struct bt_scatternet_s *vlan = 0;
2017
2018 if (!strcmp(str, "null"))
2019 /* null */
2020 return &null_hci;
2021 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
2022 /* host[:hciN] */
2023 return bt_host_hci(str[4] ? str + 5 : "hci0");
2024 else if (!strncmp(str, "hci", 3)) {
2025 /* hci[,vlan=n] */
2026 if (str[3]) {
2027 if (!strncmp(str + 3, ",vlan=", 6)) {
2028 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2029 if (*endp)
2030 vlan = 0;
2031 }
2032 } else
2033 vlan = qemu_find_bt_vlan(0);
2034 if (vlan)
2035 return bt_new_hci(vlan);
2036 }
2037
2038 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2039
2040 return 0;
2041}
2042
2043static int bt_hci_parse(const char *str)
2044{
2045 struct HCIInfo *hci;
2046 bdaddr_t bdaddr;
2047
2048 if (nb_hcis >= MAX_NICS) {
2049 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2050 return -1;
2051 }
2052
2053 hci = hci_init(str);
2054 if (!hci)
2055 return -1;
2056
2057 bdaddr.b[0] = 0x52;
2058 bdaddr.b[1] = 0x54;
2059 bdaddr.b[2] = 0x00;
2060 bdaddr.b[3] = 0x12;
2061 bdaddr.b[4] = 0x34;
2062 bdaddr.b[5] = 0x56 + nb_hcis;
2063 hci->bdaddr_set(hci, bdaddr.b);
2064
2065 hci_table[nb_hcis++] = hci;
2066
2067 return 0;
2068}
2069
2070static void bt_vhci_add(int vlan_id)
2071{
2072 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2073
2074 if (!vlan->slave)
2075 fprintf(stderr, "qemu: warning: adding a VHCI to "
2076 "an empty scatternet %i\n", vlan_id);
2077
2078 bt_vhci_init(bt_new_hci(vlan));
2079}
2080
2081static struct bt_device_s *bt_device_add(const char *opt)
2082{
2083 struct bt_scatternet_s *vlan;
2084 int vlan_id = 0;
2085 char *endp = strstr(opt, ",vlan=");
2086 int len = (endp ? endp - opt : strlen(opt)) + 1;
2087 char devname[10];
2088
2089 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2090
2091 if (endp) {
2092 vlan_id = strtol(endp + 6, &endp, 0);
2093 if (*endp) {
2094 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2095 return 0;
2096 }
2097 }
2098
2099 vlan = qemu_find_bt_vlan(vlan_id);
2100
2101 if (!vlan->slave)
2102 fprintf(stderr, "qemu: warning: adding a slave device to "
2103 "an empty scatternet %i\n", vlan_id);
2104
2105 if (!strcmp(devname, "keyboard"))
2106 return bt_keyboard_init(vlan);
2107
2108 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2109 return 0;
2110}
2111
2112static int bt_parse(const char *opt)
2113{
2114 const char *endp, *p;
2115 int vlan;
2116
2117 if (strstart(opt, "hci", &endp)) {
2118 if (!*endp || *endp == ',') {
2119 if (*endp)
2120 if (!strstart(endp, ",vlan=", 0))
2121 opt = endp + 1;
2122
2123 return bt_hci_parse(opt);
2124 }
2125 } else if (strstart(opt, "vhci", &endp)) {
2126 if (!*endp || *endp == ',') {
2127 if (*endp) {
2128 if (strstart(endp, ",vlan=", &p)) {
2129 vlan = strtol(p, (char **) &endp, 0);
2130 if (*endp) {
2131 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2132 return 1;
2133 }
2134 } else {
2135 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2136 return 1;
2137 }
2138 } else
2139 vlan = 0;
2140
2141 bt_vhci_add(vlan);
2142 return 0;
2143 }
2144 } else if (strstart(opt, "device:", &endp))
2145 return !bt_device_add(endp);
2146
2147 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2148 return 1;
2149}
2150
balrog1ae26a12008-09-28 23:19:47 +00002151/***********************************************************/
2152/* QEMU Block devices */
2153
balrog609497a2008-01-14 02:56:53 +00002154#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00002155#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00002156#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00002157#define PFLASH_ALIAS "if=pflash"
2158#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00002159#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002160
aliguori7d5aca92009-02-11 15:19:58 +00002161static int drive_opt_get_free_idx(void)
2162{
2163 int index;
2164
2165 for (index = 0; index < MAX_DRIVES; index++)
2166 if (!drives_opt[index].used) {
2167 drives_opt[index].used = 1;
2168 return index;
2169 }
2170
2171 return -1;
2172}
2173
2174static int drive_get_free_idx(void)
2175{
2176 int index;
2177
2178 for (index = 0; index < MAX_DRIVES; index++)
2179 if (!drives_table[index].used) {
2180 drives_table[index].used = 1;
2181 return index;
2182 }
2183
2184 return -1;
2185}
2186
aliguori4d73cd32009-02-11 15:20:46 +00002187int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002188{
2189 va_list ap;
aliguori7d5aca92009-02-11 15:19:58 +00002190 int index = drive_opt_get_free_idx();
thse4bcb142007-12-02 04:51:10 +00002191
aliguori7d5aca92009-02-11 15:19:58 +00002192 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
thse4bcb142007-12-02 04:51:10 +00002193 fprintf(stderr, "qemu: too many drives\n");
aliguori4d73cd32009-02-11 15:20:46 +00002194 return -1;
thse4bcb142007-12-02 04:51:10 +00002195 }
2196
aliguori7d5aca92009-02-11 15:19:58 +00002197 drives_opt[index].file = file;
thse4bcb142007-12-02 04:51:10 +00002198 va_start(ap, fmt);
aliguori7d5aca92009-02-11 15:19:58 +00002199 vsnprintf(drives_opt[index].opt,
balrog609497a2008-01-14 02:56:53 +00002200 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002201 va_end(ap);
2202
aliguori7d5aca92009-02-11 15:19:58 +00002203 nb_drives_opt++;
2204 return index;
thse4bcb142007-12-02 04:51:10 +00002205}
2206
aliguorib01b1112009-02-11 15:20:20 +00002207void drive_remove(int index)
2208{
2209 drives_opt[index].used = 0;
2210 nb_drives_opt--;
2211}
2212
thsf60d39b2007-12-17 03:55:57 +00002213int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002214{
2215 int index;
2216
2217 /* seek interface, bus and unit */
2218
aliguori7d5aca92009-02-11 15:19:58 +00002219 for (index = 0; index < MAX_DRIVES; index++)
thsf60d39b2007-12-17 03:55:57 +00002220 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002221 drives_table[index].bus == bus &&
aliguori7d5aca92009-02-11 15:19:58 +00002222 drives_table[index].unit == unit &&
2223 drives_table[index].used)
thse4bcb142007-12-02 04:51:10 +00002224 return index;
2225
2226 return -1;
2227}
2228
thsf60d39b2007-12-17 03:55:57 +00002229int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002230{
2231 int max_bus;
2232 int index;
2233
2234 max_bus = -1;
2235 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002236 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002237 drives_table[index].bus > max_bus)
2238 max_bus = drives_table[index].bus;
2239 }
2240 return max_bus;
2241}
2242
aliguorifa879c62009-01-07 17:32:33 +00002243const char *drive_get_serial(BlockDriverState *bdrv)
2244{
2245 int index;
2246
2247 for (index = 0; index < nb_drives; index++)
2248 if (drives_table[index].bdrv == bdrv)
2249 return drives_table[index].serial;
2250
2251 return "\0";
2252}
2253
aliguori428c5702009-01-21 18:59:04 +00002254BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2255{
2256 int index;
2257
2258 for (index = 0; index < nb_drives; index++)
2259 if (drives_table[index].bdrv == bdrv)
2260 return drives_table[index].onerror;
2261
aliguoricdad4bd2009-02-28 16:51:01 +00002262 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002263}
2264
aurel32a1620fa2008-04-29 05:58:01 +00002265static void bdrv_format_print(void *opaque, const char *name)
2266{
2267 fprintf(stderr, " %s", name);
2268}
2269
aliguorib01b1112009-02-11 15:20:20 +00002270void drive_uninit(BlockDriverState *bdrv)
2271{
2272 int i;
2273
2274 for (i = 0; i < MAX_DRIVES; i++)
2275 if (drives_table[i].bdrv == bdrv) {
2276 drives_table[i].bdrv = NULL;
2277 drives_table[i].used = 0;
2278 drive_remove(drives_table[i].drive_opt_idx);
2279 nb_drives--;
2280 break;
2281 }
2282}
2283
aliguori4d73cd32009-02-11 15:20:46 +00002284int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
thse4bcb142007-12-02 04:51:10 +00002285{
2286 char buf[128];
2287 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002288 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00002289 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002290 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002291 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002292 enum { MEDIA_DISK, MEDIA_CDROM } media;
2293 int bus_id, unit_id;
2294 int cyls, heads, secs, translation;
2295 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002296 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002297 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002298 int max_devs;
2299 int index;
balrog33f00272007-12-24 14:33:24 +00002300 int cache;
aliguori428c5702009-01-21 18:59:04 +00002301 int bdrv_flags, onerror;
aliguori7d5aca92009-02-11 15:19:58 +00002302 int drives_table_idx;
balrog609497a2008-01-14 02:56:53 +00002303 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002304 static const char * const params[] = { "bus", "unit", "if", "index",
2305 "cyls", "heads", "secs", "trans",
2306 "media", "snapshot", "file",
aliguori428c5702009-01-21 18:59:04 +00002307 "cache", "format", "serial", "werror",
2308 NULL };
thse4bcb142007-12-02 04:51:10 +00002309
2310 if (check_params(buf, sizeof(buf), params, str) < 0) {
balrogff993632008-02-10 13:21:25 +00002311 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
thse4bcb142007-12-02 04:51:10 +00002312 buf, str);
2313 return -1;
2314 }
2315
2316 file[0] = 0;
2317 cyls = heads = secs = 0;
2318 bus_id = 0;
2319 unit_id = -1;
2320 translation = BIOS_ATA_TRANSLATION_AUTO;
2321 index = -1;
aliguori4dc822d2008-12-04 21:39:21 +00002322 cache = 3;
thse4bcb142007-12-02 04:51:10 +00002323
blueswir1c9b1ae22008-09-28 18:55:17 +00002324 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002325 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002326 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002327 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002328 } else {
thsf60d39b2007-12-17 03:55:57 +00002329 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002330 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002331 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002332 }
2333 media = MEDIA_DISK;
2334
2335 /* extract parameters */
2336
2337 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2338 bus_id = strtol(buf, NULL, 0);
2339 if (bus_id < 0) {
2340 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2341 return -1;
2342 }
2343 }
2344
2345 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2346 unit_id = strtol(buf, NULL, 0);
2347 if (unit_id < 0) {
2348 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2349 return -1;
2350 }
2351 }
2352
2353 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002354 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002355 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002356 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002357 max_devs = MAX_IDE_DEVS;
2358 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002359 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002360 max_devs = MAX_SCSI_DEVS;
2361 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002362 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002363 max_devs = 0;
2364 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002365 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002366 max_devs = 0;
2367 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002368 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002369 max_devs = 0;
2370 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002371 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002372 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002373 } else if (!strcmp(buf, "virtio")) {
2374 type = IF_VIRTIO;
2375 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002376 } else if (!strcmp(buf, "xen")) {
2377 type = IF_XEN;
2378 max_devs = 0;
2379 } else {
thse4bcb142007-12-02 04:51:10 +00002380 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2381 return -1;
2382 }
2383 }
2384
2385 if (get_param_value(buf, sizeof(buf), "index", str)) {
2386 index = strtol(buf, NULL, 0);
2387 if (index < 0) {
2388 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2389 return -1;
2390 }
2391 }
2392
2393 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2394 cyls = strtol(buf, NULL, 0);
2395 }
2396
2397 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2398 heads = strtol(buf, NULL, 0);
2399 }
2400
2401 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2402 secs = strtol(buf, NULL, 0);
2403 }
2404
2405 if (cyls || heads || secs) {
2406 if (cyls < 1 || cyls > 16383) {
2407 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2408 return -1;
2409 }
2410 if (heads < 1 || heads > 16) {
2411 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2412 return -1;
2413 }
2414 if (secs < 1 || secs > 63) {
2415 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2416 return -1;
2417 }
2418 }
2419
2420 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2421 if (!cyls) {
2422 fprintf(stderr,
2423 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2424 str);
2425 return -1;
2426 }
2427 if (!strcmp(buf, "none"))
2428 translation = BIOS_ATA_TRANSLATION_NONE;
2429 else if (!strcmp(buf, "lba"))
2430 translation = BIOS_ATA_TRANSLATION_LBA;
2431 else if (!strcmp(buf, "auto"))
2432 translation = BIOS_ATA_TRANSLATION_AUTO;
2433 else {
2434 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2435 return -1;
2436 }
2437 }
2438
2439 if (get_param_value(buf, sizeof(buf), "media", str)) {
2440 if (!strcmp(buf, "disk")) {
2441 media = MEDIA_DISK;
2442 } else if (!strcmp(buf, "cdrom")) {
2443 if (cyls || secs || heads) {
2444 fprintf(stderr,
2445 "qemu: '%s' invalid physical CHS format\n", str);
2446 return -1;
2447 }
2448 media = MEDIA_CDROM;
2449 } else {
2450 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2451 return -1;
2452 }
2453 }
2454
2455 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2456 if (!strcmp(buf, "on"))
2457 snapshot = 1;
2458 else if (!strcmp(buf, "off"))
2459 snapshot = 0;
2460 else {
2461 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2462 return -1;
2463 }
2464 }
2465
balrog33f00272007-12-24 14:33:24 +00002466 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002467 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002468 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002469 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002470 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002471 else if (!strcmp(buf, "writeback"))
2472 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002473 else {
2474 fprintf(stderr, "qemu: invalid cache option\n");
2475 return -1;
2476 }
2477 }
2478
aurel321e72d3b2008-04-28 20:26:45 +00002479 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002480 if (strcmp(buf, "?") == 0) {
2481 fprintf(stderr, "qemu: Supported formats:");
2482 bdrv_iterate_format(bdrv_format_print, NULL);
2483 fprintf(stderr, "\n");
2484 return -1;
2485 }
aurel321e72d3b2008-04-28 20:26:45 +00002486 drv = bdrv_find_format(buf);
2487 if (!drv) {
2488 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2489 return -1;
2490 }
2491 }
2492
balrog609497a2008-01-14 02:56:53 +00002493 if (arg->file == NULL)
2494 get_param_value(file, sizeof(file), "file", str);
2495 else
2496 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002497
aliguorifa879c62009-01-07 17:32:33 +00002498 if (!get_param_value(serial, sizeof(serial), "serial", str))
2499 memset(serial, 0, sizeof(serial));
2500
aliguoricdad4bd2009-02-28 16:51:01 +00002501 onerror = BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002502 if (get_param_value(buf, sizeof(serial), "werror", str)) {
aliguori869a5c62009-01-22 19:52:25 +00002503 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002504 fprintf(stderr, "werror is no supported by this format\n");
aliguori428c5702009-01-21 18:59:04 +00002505 return -1;
2506 }
2507 if (!strcmp(buf, "ignore"))
2508 onerror = BLOCK_ERR_IGNORE;
2509 else if (!strcmp(buf, "enospc"))
2510 onerror = BLOCK_ERR_STOP_ENOSPC;
2511 else if (!strcmp(buf, "stop"))
2512 onerror = BLOCK_ERR_STOP_ANY;
2513 else if (!strcmp(buf, "report"))
2514 onerror = BLOCK_ERR_REPORT;
2515 else {
2516 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2517 return -1;
2518 }
2519 }
2520
thse4bcb142007-12-02 04:51:10 +00002521 /* compute bus and unit according index */
2522
2523 if (index != -1) {
2524 if (bus_id != 0 || unit_id != -1) {
2525 fprintf(stderr,
2526 "qemu: '%s' index cannot be used with bus and unit\n", str);
2527 return -1;
2528 }
2529 if (max_devs == 0)
2530 {
2531 unit_id = index;
2532 bus_id = 0;
2533 } else {
2534 unit_id = index % max_devs;
2535 bus_id = index / max_devs;
2536 }
2537 }
2538
2539 /* if user doesn't specify a unit_id,
2540 * try to find the first free
2541 */
2542
2543 if (unit_id == -1) {
2544 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002545 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002546 unit_id++;
2547 if (max_devs && unit_id >= max_devs) {
2548 unit_id -= max_devs;
2549 bus_id++;
2550 }
2551 }
2552 }
2553
2554 /* check unit id */
2555
2556 if (max_devs && unit_id >= max_devs) {
2557 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2558 str, unit_id, max_devs - 1);
2559 return -1;
2560 }
2561
2562 /*
2563 * ignore multiple definitions
2564 */
2565
thsf60d39b2007-12-17 03:55:57 +00002566 if (drive_get_index(type, bus_id, unit_id) != -1)
aliguori4d73cd32009-02-11 15:20:46 +00002567 return -2;
thse4bcb142007-12-02 04:51:10 +00002568
2569 /* init */
2570
thsf60d39b2007-12-17 03:55:57 +00002571 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002572 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002573 if (max_devs)
2574 snprintf(buf, sizeof(buf), "%s%i%s%i",
2575 devname, bus_id, mediastr, unit_id);
2576 else
2577 snprintf(buf, sizeof(buf), "%s%s%i",
2578 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002579 bdrv = bdrv_new(buf);
aliguori7d5aca92009-02-11 15:19:58 +00002580 drives_table_idx = drive_get_free_idx();
2581 drives_table[drives_table_idx].bdrv = bdrv;
2582 drives_table[drives_table_idx].type = type;
2583 drives_table[drives_table_idx].bus = bus_id;
2584 drives_table[drives_table_idx].unit = unit_id;
2585 drives_table[drives_table_idx].onerror = onerror;
aliguorib01b1112009-02-11 15:20:20 +00002586 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
aliguorifa879c62009-01-07 17:32:33 +00002587 strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002588 nb_drives++;
2589
thsf60d39b2007-12-17 03:55:57 +00002590 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002591 case IF_IDE:
2592 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002593 case IF_XEN:
thse4bcb142007-12-02 04:51:10 +00002594 switch(media) {
2595 case MEDIA_DISK:
2596 if (cyls != 0) {
2597 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2598 bdrv_set_translation_hint(bdrv, translation);
2599 }
2600 break;
2601 case MEDIA_CDROM:
2602 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2603 break;
2604 }
2605 break;
2606 case IF_SD:
2607 /* FIXME: This isn't really a floppy, but it's a reasonable
2608 approximation. */
2609 case IF_FLOPPY:
2610 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2611 break;
2612 case IF_PFLASH:
2613 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002614 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002615 break;
2616 }
2617 if (!file[0])
aliguori4d73cd32009-02-11 15:20:46 +00002618 return -2;
balrog33f00272007-12-24 14:33:24 +00002619 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002620 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002621 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002622 cache = 2; /* always use write-back with snapshot */
2623 }
2624 if (cache == 0) /* no caching */
2625 bdrv_flags |= BDRV_O_NOCACHE;
2626 else if (cache == 2) /* write-back */
2627 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori4dc822d2008-12-04 21:39:21 +00002628 else if (cache == 3) /* not specified */
2629 bdrv_flags |= BDRV_O_CACHE_DEF;
aliguoric0f4ce72009-03-05 23:01:01 +00002630 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002631 fprintf(stderr, "qemu: could not open disk image %s\n",
2632 file);
2633 return -1;
2634 }
aliguoric0f4ce72009-03-05 23:01:01 +00002635 if (bdrv_key_required(bdrv))
2636 autostart = 0;
aliguori4d73cd32009-02-11 15:20:46 +00002637 return drives_table_idx;
thse4bcb142007-12-02 04:51:10 +00002638}
2639
aliguori268a3622009-04-21 22:30:27 +00002640static void numa_add(const char *optarg)
2641{
2642 char option[128];
2643 char *endptr;
2644 unsigned long long value, endvalue;
2645 int nodenr;
2646
2647 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2648 if (!strcmp(option, "node")) {
2649 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2650 nodenr = nb_numa_nodes;
2651 } else {
2652 nodenr = strtoull(option, NULL, 10);
2653 }
2654
2655 if (get_param_value(option, 128, "mem", optarg) == 0) {
2656 node_mem[nodenr] = 0;
2657 } else {
2658 value = strtoull(option, &endptr, 0);
2659 switch (*endptr) {
2660 case 0: case 'M': case 'm':
2661 value <<= 20;
2662 break;
2663 case 'G': case 'g':
2664 value <<= 30;
2665 break;
2666 }
2667 node_mem[nodenr] = value;
2668 }
2669 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2670 node_cpumask[nodenr] = 0;
2671 } else {
2672 value = strtoull(option, &endptr, 10);
2673 if (value >= 64) {
2674 value = 63;
2675 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2676 } else {
2677 if (*endptr == '-') {
2678 endvalue = strtoull(endptr+1, &endptr, 10);
2679 if (endvalue >= 63) {
2680 endvalue = 62;
2681 fprintf(stderr,
2682 "only 63 CPUs in NUMA mode supported.\n");
2683 }
2684 value = (1 << (endvalue + 1)) - (1 << value);
2685 } else {
2686 value = 1 << value;
2687 }
2688 }
2689 node_cpumask[nodenr] = value;
2690 }
2691 nb_numa_nodes++;
2692 }
2693 return;
2694}
2695
bellard330d0412003-07-26 18:11:40 +00002696/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002697/* USB devices */
2698
pbrook0d92ed32006-05-21 16:30:15 +00002699static USBPort *used_usb_ports;
2700static USBPort *free_usb_ports;
2701
2702/* ??? Maybe change this to register a hub to keep track of the topology. */
2703void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2704 usb_attachfn attach)
2705{
2706 port->opaque = opaque;
2707 port->index = index;
2708 port->attach = attach;
2709 port->next = free_usb_ports;
2710 free_usb_ports = port;
2711}
2712
aliguori4b096fc2008-08-21 19:28:55 +00002713int usb_device_add_dev(USBDevice *dev)
2714{
2715 USBPort *port;
2716
2717 /* Find a USB port to add the device to. */
2718 port = free_usb_ports;
2719 if (!port->next) {
2720 USBDevice *hub;
2721
2722 /* Create a new hub and chain it on. */
2723 free_usb_ports = NULL;
2724 port->next = used_usb_ports;
2725 used_usb_ports = port;
2726
2727 hub = usb_hub_init(VM_USB_HUB_SIZE);
2728 usb_attach(port, hub);
2729 port = free_usb_ports;
2730 }
2731
2732 free_usb_ports = port->next;
2733 port->next = used_usb_ports;
2734 used_usb_ports = port;
2735 usb_attach(port, dev);
2736 return 0;
2737}
2738
aliguoribb5fc202009-03-05 23:01:15 +00002739static void usb_msd_password_cb(void *opaque, int err)
2740{
2741 USBDevice *dev = opaque;
2742
2743 if (!err)
2744 usb_device_add_dev(dev);
2745 else
2746 dev->handle_destroy(dev);
2747}
2748
aliguoric0f4ce72009-03-05 23:01:01 +00002749static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002750{
2751 const char *p;
2752 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002753
pbrook0d92ed32006-05-21 16:30:15 +00002754 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002755 return -1;
2756
2757 if (strstart(devname, "host:", &p)) {
2758 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002759 } else if (!strcmp(devname, "mouse")) {
2760 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002761 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002762 dev = usb_tablet_init();
2763 } else if (!strcmp(devname, "keyboard")) {
2764 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002765 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002766 BlockDriverState *bs;
2767
aliguoribb5fc202009-03-05 23:01:15 +00002768 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002769 if (!dev)
2770 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002771 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002772 if (bdrv_key_required(bs)) {
2773 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002774 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002775 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2776 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002777 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002778 }
2779 }
balrogf6d2a312007-06-10 19:21:04 +00002780 } else if (!strcmp(devname, "wacom-tablet")) {
2781 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002782 } else if (strstart(devname, "serial:", &p)) {
2783 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002784#ifdef CONFIG_BRLAPI
2785 } else if (!strcmp(devname, "braille")) {
2786 dev = usb_baum_init();
2787#endif
balrog6c9f8862008-07-17 20:47:13 +00002788 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002789 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002790
balrog9ad97e62008-07-29 13:16:31 +00002791 if (net_client_init("nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002792 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002793 nd_table[nic].model = "usb";
2794 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002795 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2796 dev = usb_bt_init(devname[2] ? hci_init(p) :
2797 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002798 } else {
2799 return -1;
2800 }
pbrook0d92ed32006-05-21 16:30:15 +00002801 if (!dev)
2802 return -1;
2803
aliguori4b096fc2008-08-21 19:28:55 +00002804 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002805}
2806
aliguori1f3870a2008-08-21 19:27:48 +00002807int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002808{
pbrook0d92ed32006-05-21 16:30:15 +00002809 USBPort *port;
2810 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002811 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002812
pbrook0d92ed32006-05-21 16:30:15 +00002813 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002814 return -1;
2815
bellarda594cfb2005-11-06 16:13:29 +00002816 if (bus_num != 0)
2817 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002818
2819 lastp = &used_usb_ports;
2820 port = used_usb_ports;
2821 while (port && port->dev->addr != addr) {
2822 lastp = &port->next;
2823 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002824 }
pbrook0d92ed32006-05-21 16:30:15 +00002825
2826 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002827 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002828
bellard059809e2006-07-19 18:06:15 +00002829 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002830 *lastp = port->next;
2831 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002832 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002833 port->next = free_usb_ports;
2834 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002835 return 0;
2836}
2837
aliguori1f3870a2008-08-21 19:27:48 +00002838static int usb_device_del(const char *devname)
2839{
2840 int bus_num, addr;
2841 const char *p;
2842
aliguori5d0c5752008-09-14 01:07:41 +00002843 if (strstart(devname, "host:", &p))
2844 return usb_host_device_close(p);
2845
aliguori1f3870a2008-08-21 19:27:48 +00002846 if (!used_usb_ports)
2847 return -1;
2848
2849 p = strchr(devname, '.');
2850 if (!p)
2851 return -1;
2852 bus_num = strtoul(devname, NULL, 0);
2853 addr = strtoul(p + 1, NULL, 0);
2854
2855 return usb_device_del_addr(bus_num, addr);
2856}
2857
aliguori376253e2009-03-05 23:01:23 +00002858void do_usb_add(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002859{
aliguoric0f4ce72009-03-05 23:01:01 +00002860 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002861}
2862
aliguori376253e2009-03-05 23:01:23 +00002863void do_usb_del(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002864{
aliguori4b096fc2008-08-21 19:28:55 +00002865 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002866}
2867
aliguori376253e2009-03-05 23:01:23 +00002868void usb_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00002869{
2870 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002871 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002872 const char *speed_str;
2873
pbrook0d92ed32006-05-21 16:30:15 +00002874 if (!usb_enabled) {
aliguori376253e2009-03-05 23:01:23 +00002875 monitor_printf(mon, "USB support not enabled\n");
bellarda594cfb2005-11-06 16:13:29 +00002876 return;
2877 }
2878
pbrook0d92ed32006-05-21 16:30:15 +00002879 for (port = used_usb_ports; port; port = port->next) {
2880 dev = port->dev;
2881 if (!dev)
2882 continue;
2883 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002884 case USB_SPEED_LOW:
2885 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002886 break;
ths5fafdf22007-09-16 21:08:06 +00002887 case USB_SPEED_FULL:
2888 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002889 break;
ths5fafdf22007-09-16 21:08:06 +00002890 case USB_SPEED_HIGH:
2891 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002892 break;
2893 default:
ths5fafdf22007-09-16 21:08:06 +00002894 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002895 break;
bellarda594cfb2005-11-06 16:13:29 +00002896 }
aliguori376253e2009-03-05 23:01:23 +00002897 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2898 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002899 }
2900}
2901
bellardf7cce892004-12-08 22:21:25 +00002902/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002903/* PCMCIA/Cardbus */
2904
2905static struct pcmcia_socket_entry_s {
2906 struct pcmcia_socket_s *socket;
2907 struct pcmcia_socket_entry_s *next;
2908} *pcmcia_sockets = 0;
2909
2910void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2911{
2912 struct pcmcia_socket_entry_s *entry;
2913
2914 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2915 entry->socket = socket;
2916 entry->next = pcmcia_sockets;
2917 pcmcia_sockets = entry;
2918}
2919
2920void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2921{
2922 struct pcmcia_socket_entry_s *entry, **ptr;
2923
2924 ptr = &pcmcia_sockets;
2925 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2926 if (entry->socket == socket) {
2927 *ptr = entry->next;
2928 qemu_free(entry);
2929 }
2930}
2931
aliguori376253e2009-03-05 23:01:23 +00002932void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002933{
2934 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002935
balrog201a51f2007-04-30 00:51:09 +00002936 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002937 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002938
2939 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002940 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2941 iter->socket->attached ? iter->socket->card_string :
2942 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002943}
2944
2945/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002946/* register display */
2947
aliguori7b5d76d2009-03-13 15:02:13 +00002948struct DisplayAllocator default_allocator = {
2949 defaultallocator_create_displaysurface,
2950 defaultallocator_resize_displaysurface,
2951 defaultallocator_free_displaysurface
2952};
2953
aliguori3023f332009-01-16 19:04:14 +00002954void register_displaystate(DisplayState *ds)
2955{
2956 DisplayState **s;
2957 s = &display_state;
2958 while (*s != NULL)
2959 s = &(*s)->next;
2960 ds->next = NULL;
2961 *s = ds;
2962}
2963
2964DisplayState *get_displaystate(void)
2965{
2966 return display_state;
2967}
2968
aliguori7b5d76d2009-03-13 15:02:13 +00002969DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2970{
2971 if(ds->allocator == &default_allocator) ds->allocator = da;
2972 return ds->allocator;
2973}
2974
ths2ff89792007-06-21 23:34:19 +00002975/* dumb display */
2976
aliguori8f391ab2009-01-19 16:34:10 +00002977static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002978{
aliguori8f391ab2009-01-19 16:34:10 +00002979 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002980 ds->allocator = &default_allocator;
2981 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002982 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002983}
2984
2985/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002986/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002987
bellardc4b1fcc2004-03-14 21:44:30 +00002988typedef struct IOHandlerRecord {
2989 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002990 IOCanRWHandler *fd_read_poll;
2991 IOHandler *fd_read;
2992 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002993 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002994 void *opaque;
2995 /* temporary data */
2996 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002997 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002998} IOHandlerRecord;
2999
bellard8a7ddc32004-03-31 19:00:16 +00003000static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00003001
bellard7c9d8e02005-11-15 22:16:05 +00003002/* XXX: fd_read_poll should be suppressed, but an API change is
3003 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00003004int qemu_set_fd_handler2(int fd,
3005 IOCanRWHandler *fd_read_poll,
3006 IOHandler *fd_read,
3007 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00003008 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00003009{
bellard8a7ddc32004-03-31 19:00:16 +00003010 IOHandlerRecord **pioh, *ioh;
3011
bellard7c9d8e02005-11-15 22:16:05 +00003012 if (!fd_read && !fd_write) {
3013 pioh = &first_io_handler;
3014 for(;;) {
3015 ioh = *pioh;
3016 if (ioh == NULL)
3017 break;
3018 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00003019 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00003020 break;
3021 }
3022 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00003023 }
bellard7c9d8e02005-11-15 22:16:05 +00003024 } else {
3025 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3026 if (ioh->fd == fd)
3027 goto found;
3028 }
3029 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00003030 ioh->next = first_io_handler;
3031 first_io_handler = ioh;
3032 found:
3033 ioh->fd = fd;
3034 ioh->fd_read_poll = fd_read_poll;
3035 ioh->fd_read = fd_read;
3036 ioh->fd_write = fd_write;
3037 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00003038 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00003039 }
bellard7c9d8e02005-11-15 22:16:05 +00003040 return 0;
3041}
3042
ths5fafdf22007-09-16 21:08:06 +00003043int qemu_set_fd_handler(int fd,
3044 IOHandler *fd_read,
3045 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00003046 void *opaque)
3047{
3048 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00003049}
3050
aliguori56f3a5d2008-10-31 18:07:17 +00003051#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00003052/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00003053/* Polling handling */
3054
3055typedef struct PollingEntry {
3056 PollingFunc *func;
3057 void *opaque;
3058 struct PollingEntry *next;
3059} PollingEntry;
3060
3061static PollingEntry *first_polling_entry;
3062
3063int qemu_add_polling_cb(PollingFunc *func, void *opaque)
3064{
3065 PollingEntry **ppe, *pe;
3066 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00003067 pe->func = func;
3068 pe->opaque = opaque;
3069 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
3070 *ppe = pe;
3071 return 0;
3072}
3073
3074void qemu_del_polling_cb(PollingFunc *func, void *opaque)
3075{
3076 PollingEntry **ppe, *pe;
3077 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
3078 pe = *ppe;
3079 if (pe->func == func && pe->opaque == opaque) {
3080 *ppe = pe->next;
3081 qemu_free(pe);
3082 break;
3083 }
3084 }
3085}
3086
bellarda18e5242006-06-25 17:18:27 +00003087/***********************************************************/
3088/* Wait objects support */
3089typedef struct WaitObjects {
3090 int num;
3091 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
3092 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
3093 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
3094} WaitObjects;
3095
3096static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00003097
bellarda18e5242006-06-25 17:18:27 +00003098int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3099{
3100 WaitObjects *w = &wait_objects;
3101
3102 if (w->num >= MAXIMUM_WAIT_OBJECTS)
3103 return -1;
3104 w->events[w->num] = handle;
3105 w->func[w->num] = func;
3106 w->opaque[w->num] = opaque;
3107 w->num++;
3108 return 0;
3109}
3110
3111void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3112{
3113 int i, found;
3114 WaitObjects *w = &wait_objects;
3115
3116 found = 0;
3117 for (i = 0; i < w->num; i++) {
3118 if (w->events[i] == handle)
3119 found = 1;
3120 if (found) {
3121 w->events[i] = w->events[i + 1];
3122 w->func[i] = w->func[i + 1];
3123 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00003124 }
bellarda18e5242006-06-25 17:18:27 +00003125 }
3126 if (found)
3127 w->num--;
3128}
3129#endif
3130
bellard8a7ddc32004-03-31 19:00:16 +00003131/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003132/* ram save/restore */
3133
bellard8a7ddc32004-03-31 19:00:16 +00003134static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3135{
3136 int v;
3137
3138 v = qemu_get_byte(f);
3139 switch(v) {
3140 case 0:
3141 if (qemu_get_buffer(f, buf, len) != len)
3142 return -EIO;
3143 break;
3144 case 1:
3145 v = qemu_get_byte(f);
3146 memset(buf, v, len);
3147 break;
3148 default:
3149 return -EINVAL;
3150 }
aliguori871d2f02008-10-13 03:07:56 +00003151
3152 if (qemu_file_has_error(f))
3153 return -EIO;
3154
bellard8a7ddc32004-03-31 19:00:16 +00003155 return 0;
3156}
3157
bellardc88676f2006-08-06 13:36:11 +00003158static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003159{
aurel3200f82b82008-04-27 21:12:55 +00003160 int ret;
3161 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00003162
pbrook94a6b542009-04-11 17:15:54 +00003163 if (qemu_get_be32(f) != last_ram_offset)
bellard8a7ddc32004-03-31 19:00:16 +00003164 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003165 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
pbrook5579c7f2009-04-11 14:47:08 +00003166 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
bellard8a7ddc32004-03-31 19:00:16 +00003167 if (ret)
3168 return ret;
3169 }
3170 return 0;
3171}
3172
bellardc88676f2006-08-06 13:36:11 +00003173#define BDRV_HASH_BLOCK_SIZE 1024
3174#define IOBUF_SIZE 4096
3175#define RAM_CBLOCK_MAGIC 0xfabe
3176
bellardc88676f2006-08-06 13:36:11 +00003177typedef struct RamDecompressState {
3178 z_stream zstream;
3179 QEMUFile *f;
3180 uint8_t buf[IOBUF_SIZE];
3181} RamDecompressState;
3182
3183static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3184{
3185 int ret;
3186 memset(s, 0, sizeof(*s));
3187 s->f = f;
3188 ret = inflateInit(&s->zstream);
3189 if (ret != Z_OK)
3190 return -1;
3191 return 0;
3192}
3193
3194static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3195{
3196 int ret, clen;
3197
3198 s->zstream.avail_out = len;
3199 s->zstream.next_out = buf;
3200 while (s->zstream.avail_out > 0) {
3201 if (s->zstream.avail_in == 0) {
3202 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3203 return -1;
3204 clen = qemu_get_be16(s->f);
3205 if (clen > IOBUF_SIZE)
3206 return -1;
3207 qemu_get_buffer(s->f, s->buf, clen);
3208 s->zstream.avail_in = clen;
3209 s->zstream.next_in = s->buf;
3210 }
3211 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3212 if (ret != Z_OK && ret != Z_STREAM_END) {
3213 return -1;
3214 }
3215 }
3216 return 0;
3217}
3218
3219static void ram_decompress_close(RamDecompressState *s)
3220{
3221 inflateEnd(&s->zstream);
3222}
3223
aliguori475e4272008-10-06 20:21:51 +00003224#define RAM_SAVE_FLAG_FULL 0x01
3225#define RAM_SAVE_FLAG_COMPRESS 0x02
3226#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3227#define RAM_SAVE_FLAG_PAGE 0x08
3228#define RAM_SAVE_FLAG_EOS 0x10
3229
3230static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003231{
aliguori475e4272008-10-06 20:21:51 +00003232 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3233 uint32_t *array = (uint32_t *)page;
3234 int i;
ths3b46e622007-09-17 08:09:54 +00003235
aliguori475e4272008-10-06 20:21:51 +00003236 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3237 if (array[i] != val)
3238 return 0;
bellardc88676f2006-08-06 13:36:11 +00003239 }
aliguori475e4272008-10-06 20:21:51 +00003240
3241 return 1;
bellardc88676f2006-08-06 13:36:11 +00003242}
3243
aliguori475e4272008-10-06 20:21:51 +00003244static int ram_save_block(QEMUFile *f)
3245{
3246 static ram_addr_t current_addr = 0;
3247 ram_addr_t saved_addr = current_addr;
3248 ram_addr_t addr = 0;
3249 int found = 0;
3250
pbrook94a6b542009-04-11 17:15:54 +00003251 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00003252 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00003253 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00003254
3255 cpu_physical_memory_reset_dirty(current_addr,
3256 current_addr + TARGET_PAGE_SIZE,
3257 MIGRATION_DIRTY_FLAG);
3258
pbrook5579c7f2009-04-11 14:47:08 +00003259 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00003260
pbrook5579c7f2009-04-11 14:47:08 +00003261 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00003262 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00003263 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00003264 } else {
3265 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00003266 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003267 }
3268
3269 found = 1;
3270 break;
3271 }
3272 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00003273 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00003274 }
3275
3276 return found;
3277}
3278
3279static ram_addr_t ram_save_threshold = 10;
3280
3281static ram_addr_t ram_save_remaining(void)
3282{
3283 ram_addr_t addr;
3284 ram_addr_t count = 0;
3285
pbrook94a6b542009-04-11 17:15:54 +00003286 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003287 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3288 count++;
3289 }
3290
3291 return count;
3292}
3293
3294static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3295{
3296 ram_addr_t addr;
3297
3298 if (stage == 1) {
3299 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00003300 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003301 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3302 cpu_physical_memory_set_dirty(addr);
3303 }
3304
3305 /* Enable dirty memory tracking */
3306 cpu_physical_memory_set_dirty_tracking(1);
3307
pbrook94a6b542009-04-11 17:15:54 +00003308 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003309 }
3310
3311 while (!qemu_file_rate_limit(f)) {
3312 int ret;
3313
3314 ret = ram_save_block(f);
3315 if (ret == 0) /* no more blocks */
3316 break;
3317 }
3318
3319 /* try transferring iterative blocks of memory */
3320
3321 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003322
3323 /* flush all remaining blocks regardless of rate limiting */
3324 while (ram_save_block(f) != 0);
aliguori8215e912009-04-05 19:30:55 +00003325 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003326 }
3327
3328 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3329
3330 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3331}
3332
3333static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003334{
3335 RamDecompressState s1, *s = &s1;
3336 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003337 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003338
bellardc88676f2006-08-06 13:36:11 +00003339 if (ram_decompress_open(s, f) < 0)
3340 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003341 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
bellardc88676f2006-08-06 13:36:11 +00003342 if (ram_decompress_buf(s, buf, 1) < 0) {
3343 fprintf(stderr, "Error while reading ram block header\n");
3344 goto error;
3345 }
3346 if (buf[0] == 0) {
pbrook5579c7f2009-04-11 14:47:08 +00003347 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3348 BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003349 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003350 goto error;
3351 }
aliguori475e4272008-10-06 20:21:51 +00003352 } else {
bellardc88676f2006-08-06 13:36:11 +00003353 error:
3354 printf("Error block header\n");
3355 return -EINVAL;
3356 }
3357 }
3358 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003359
3360 return 0;
3361}
3362
3363static int ram_load(QEMUFile *f, void *opaque, int version_id)
3364{
3365 ram_addr_t addr;
3366 int flags;
3367
3368 if (version_id == 1)
3369 return ram_load_v1(f, opaque);
3370
3371 if (version_id == 2) {
pbrook94a6b542009-04-11 17:15:54 +00003372 if (qemu_get_be32(f) != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003373 return -EINVAL;
3374 return ram_load_dead(f, opaque);
3375 }
3376
3377 if (version_id != 3)
3378 return -EINVAL;
3379
3380 do {
3381 addr = qemu_get_be64(f);
3382
3383 flags = addr & ~TARGET_PAGE_MASK;
3384 addr &= TARGET_PAGE_MASK;
3385
3386 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003387 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003388 return -EINVAL;
3389 }
3390
3391 if (flags & RAM_SAVE_FLAG_FULL) {
3392 if (ram_load_dead(f, opaque) < 0)
3393 return -EINVAL;
3394 }
3395
3396 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3397 uint8_t ch = qemu_get_byte(f);
pbrook5579c7f2009-04-11 14:47:08 +00003398 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003399 } else if (flags & RAM_SAVE_FLAG_PAGE)
pbrook5579c7f2009-04-11 14:47:08 +00003400 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003401 } while (!(flags & RAM_SAVE_FLAG_EOS));
3402
bellardc88676f2006-08-06 13:36:11 +00003403 return 0;
3404}
3405
aliguori9e472e12008-10-08 19:50:24 +00003406void qemu_service_io(void)
3407{
3408 CPUState *env = cpu_single_env;
3409 if (env) {
aurel323098dba2009-03-07 21:28:24 +00003410 cpu_exit(env);
blueswir1640f42e2009-04-19 10:18:01 +00003411#ifdef CONFIG_KQEMU
aliguori9e472e12008-10-08 19:50:24 +00003412 if (env->kqemu_enabled) {
3413 kqemu_cpu_interrupt(env);
3414 }
3415#endif
3416 }
3417}
3418
bellard8a7ddc32004-03-31 19:00:16 +00003419/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003420/* bottom halves (can be seen as timers which expire ASAP) */
3421
3422struct QEMUBH {
3423 QEMUBHFunc *cb;
3424 void *opaque;
3425 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003426 int idle;
3427 int deleted;
bellard83f64092006-08-01 16:21:11 +00003428 QEMUBH *next;
3429};
3430
3431static QEMUBH *first_bh = NULL;
3432
3433QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3434{
3435 QEMUBH *bh;
3436 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003437 bh->cb = cb;
3438 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003439 bh->next = first_bh;
3440 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003441 return bh;
3442}
3443
bellard6eb57332006-08-06 09:51:25 +00003444int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003445{
aliguori1b435b12008-10-31 17:24:21 +00003446 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003447 int ret;
bellard83f64092006-08-01 16:21:11 +00003448
bellard6eb57332006-08-06 09:51:25 +00003449 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003450 for (bh = first_bh; bh; bh = bh->next) {
3451 if (!bh->deleted && bh->scheduled) {
3452 bh->scheduled = 0;
3453 if (!bh->idle)
3454 ret = 1;
3455 bh->idle = 0;
3456 bh->cb(bh->opaque);
3457 }
bellard83f64092006-08-01 16:21:11 +00003458 }
aliguori1b435b12008-10-31 17:24:21 +00003459
3460 /* remove deleted bhs */
3461 bhp = &first_bh;
3462 while (*bhp) {
3463 bh = *bhp;
3464 if (bh->deleted) {
3465 *bhp = bh->next;
3466 qemu_free(bh);
3467 } else
3468 bhp = &bh->next;
3469 }
3470
bellard6eb57332006-08-06 09:51:25 +00003471 return ret;
bellard83f64092006-08-01 16:21:11 +00003472}
3473
aliguori1b435b12008-10-31 17:24:21 +00003474void qemu_bh_schedule_idle(QEMUBH *bh)
3475{
3476 if (bh->scheduled)
3477 return;
3478 bh->scheduled = 1;
3479 bh->idle = 1;
3480}
3481
bellard83f64092006-08-01 16:21:11 +00003482void qemu_bh_schedule(QEMUBH *bh)
3483{
3484 CPUState *env = cpu_single_env;
3485 if (bh->scheduled)
3486 return;
3487 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003488 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003489 /* stop the currently executing CPU to execute the BH ASAP */
3490 if (env) {
aurel323098dba2009-03-07 21:28:24 +00003491 cpu_exit(env);
bellard83f64092006-08-01 16:21:11 +00003492 }
3493}
3494
3495void qemu_bh_cancel(QEMUBH *bh)
3496{
aliguori1b435b12008-10-31 17:24:21 +00003497 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003498}
3499
3500void qemu_bh_delete(QEMUBH *bh)
3501{
aliguori1b435b12008-10-31 17:24:21 +00003502 bh->scheduled = 0;
3503 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003504}
3505
aliguori56f3a5d2008-10-31 18:07:17 +00003506static void qemu_bh_update_timeout(int *timeout)
3507{
3508 QEMUBH *bh;
3509
3510 for (bh = first_bh; bh; bh = bh->next) {
3511 if (!bh->deleted && bh->scheduled) {
3512 if (bh->idle) {
3513 /* idle bottom halves will be polled at least
3514 * every 10ms */
3515 *timeout = MIN(10, *timeout);
3516 } else {
3517 /* non-idle bottom halves will be executed
3518 * immediately */
3519 *timeout = 0;
3520 break;
3521 }
3522 }
3523 }
3524}
3525
bellard83f64092006-08-01 16:21:11 +00003526/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003527/* machine registration */
3528
blueswir1bdaf78e2008-10-04 07:24:27 +00003529static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003530QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003531
3532int qemu_register_machine(QEMUMachine *m)
3533{
3534 QEMUMachine **pm;
3535 pm = &first_machine;
3536 while (*pm != NULL)
3537 pm = &(*pm)->next;
3538 m->next = NULL;
3539 *pm = m;
3540 return 0;
3541}
3542
pbrook9596ebb2007-11-18 01:44:38 +00003543static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003544{
3545 QEMUMachine *m;
3546
3547 for(m = first_machine; m != NULL; m = m->next) {
3548 if (!strcmp(m->name, name))
3549 return m;
3550 }
3551 return NULL;
3552}
3553
3554/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003555/* main execution loop */
3556
pbrook9596ebb2007-11-18 01:44:38 +00003557static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003558{
aliguori7d957bd2009-01-15 22:14:11 +00003559 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003560 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003561 DisplayChangeListener *dcl = ds->listeners;
3562
3563 dpy_refresh(ds);
3564
3565 while (dcl != NULL) {
3566 if (dcl->gui_timer_interval &&
3567 dcl->gui_timer_interval < interval)
3568 interval = dcl->gui_timer_interval;
3569 dcl = dcl->next;
3570 }
3571 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003572}
3573
blueswir19043b622009-01-21 19:28:13 +00003574static void nographic_update(void *opaque)
3575{
3576 uint64_t interval = GUI_REFRESH_INTERVAL;
3577
3578 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3579}
3580
bellard0bd48852005-11-11 00:00:47 +00003581struct vm_change_state_entry {
3582 VMChangeStateHandler *cb;
3583 void *opaque;
3584 LIST_ENTRY (vm_change_state_entry) entries;
3585};
3586
3587static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3588
3589VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3590 void *opaque)
3591{
3592 VMChangeStateEntry *e;
3593
3594 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003595
3596 e->cb = cb;
3597 e->opaque = opaque;
3598 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3599 return e;
3600}
3601
3602void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3603{
3604 LIST_REMOVE (e, entries);
3605 qemu_free (e);
3606}
3607
aliguori9781e042009-01-22 17:15:29 +00003608static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003609{
3610 VMChangeStateEntry *e;
3611
3612 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003613 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003614 }
3615}
3616
bellard8a7ddc32004-03-31 19:00:16 +00003617void vm_start(void)
3618{
3619 if (!vm_running) {
3620 cpu_enable_ticks();
3621 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003622 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003623 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00003624 }
3625}
3626
ths5fafdf22007-09-16 21:08:06 +00003627void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00003628{
3629 if (vm_running) {
3630 cpu_disable_ticks();
3631 vm_running = 0;
aliguori9781e042009-01-22 17:15:29 +00003632 vm_state_notify(0, reason);
bellard8a7ddc32004-03-31 19:00:16 +00003633 }
3634}
3635
bellardbb0c6722004-06-20 12:37:32 +00003636/* reset/shutdown handler */
3637
3638typedef struct QEMUResetEntry {
3639 QEMUResetHandler *func;
3640 void *opaque;
3641 struct QEMUResetEntry *next;
3642} QEMUResetEntry;
3643
3644static QEMUResetEntry *first_reset_entry;
3645static int reset_requested;
3646static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003647static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00003648
aurel32cf7a2fe2008-03-18 06:53:05 +00003649int qemu_shutdown_requested(void)
3650{
3651 int r = shutdown_requested;
3652 shutdown_requested = 0;
3653 return r;
3654}
3655
3656int qemu_reset_requested(void)
3657{
3658 int r = reset_requested;
3659 reset_requested = 0;
3660 return r;
3661}
3662
3663int qemu_powerdown_requested(void)
3664{
3665 int r = powerdown_requested;
3666 powerdown_requested = 0;
3667 return r;
3668}
3669
bellardbb0c6722004-06-20 12:37:32 +00003670void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3671{
3672 QEMUResetEntry **pre, *re;
3673
3674 pre = &first_reset_entry;
3675 while (*pre != NULL)
3676 pre = &(*pre)->next;
3677 re = qemu_mallocz(sizeof(QEMUResetEntry));
3678 re->func = func;
3679 re->opaque = opaque;
3680 re->next = NULL;
3681 *pre = re;
3682}
3683
aurel32cf7a2fe2008-03-18 06:53:05 +00003684void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00003685{
3686 QEMUResetEntry *re;
3687
3688 /* reset all devices */
3689 for(re = first_reset_entry; re != NULL; re = re->next) {
3690 re->func(re->opaque);
3691 }
aliguori29203dc2009-04-17 14:26:17 +00003692 if (kvm_enabled())
3693 kvm_sync_vcpus();
bellardbb0c6722004-06-20 12:37:32 +00003694}
3695
3696void qemu_system_reset_request(void)
3697{
bellardd1beab82006-10-02 19:44:22 +00003698 if (no_reboot) {
3699 shutdown_requested = 1;
3700 } else {
3701 reset_requested = 1;
3702 }
bellard6a00d602005-11-21 23:25:50 +00003703 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003704 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003705}
3706
3707void qemu_system_shutdown_request(void)
3708{
3709 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003710 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003711 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003712}
3713
bellard34751872005-07-02 14:31:34 +00003714void qemu_system_powerdown_request(void)
3715{
3716 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003717 if (cpu_single_env)
aurel323098dba2009-03-07 21:28:24 +00003718 cpu_exit(cpu_single_env);
bellardbb0c6722004-06-20 12:37:32 +00003719}
3720
ths877cf882007-04-18 18:11:47 +00003721#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003722static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003723{
3724 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003725 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003726
bellardf3311102006-04-12 20:21:17 +00003727
3728 /* XXX: need to suppress polling by better using win32 events */
3729 ret = 0;
3730 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3731 ret |= pe->func(pe->opaque);
3732 }
thse6b1e552007-04-18 17:56:02 +00003733 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003734 int err;
3735 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003736
aliguori56f3a5d2008-10-31 18:07:17 +00003737 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003738 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3739 if (w->func[ret - WAIT_OBJECT_0])
3740 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003741
ths5fafdf22007-09-16 21:08:06 +00003742 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003743 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003744
thse6b1e552007-04-18 17:56:02 +00003745 /* Check if event is signaled */
3746 ret2 = WaitForSingleObject(w->events[i], 0);
3747 if(ret2 == WAIT_OBJECT_0) {
3748 if (w->func[i])
3749 w->func[i](w->opaque[i]);
3750 } else if (ret2 == WAIT_TIMEOUT) {
3751 } else {
3752 err = GetLastError();
3753 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003754 }
3755 }
bellarda18e5242006-06-25 17:18:27 +00003756 } else if (ret == WAIT_TIMEOUT) {
3757 } else {
3758 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003759 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003760 }
bellardf3311102006-04-12 20:21:17 +00003761 }
aliguori56f3a5d2008-10-31 18:07:17 +00003762
3763 *timeout = 0;
3764}
3765#else
blueswir169d64512008-12-07 19:30:18 +00003766static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003767{
3768}
bellardfd1dff42006-02-01 21:29:26 +00003769#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003770
3771void main_loop_wait(int timeout)
3772{
3773 IOHandlerRecord *ioh;
3774 fd_set rfds, wfds, xfds;
3775 int ret, nfds;
3776 struct timeval tv;
3777
3778 qemu_bh_update_timeout(&timeout);
3779
3780 host_main_loop_wait(&timeout);
3781
bellardfd1dff42006-02-01 21:29:26 +00003782 /* poll any events */
3783 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003784 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003785 FD_ZERO(&rfds);
3786 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003787 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003788 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003789 if (ioh->deleted)
3790 continue;
bellardfd1dff42006-02-01 21:29:26 +00003791 if (ioh->fd_read &&
3792 (!ioh->fd_read_poll ||
3793 ioh->fd_read_poll(ioh->opaque) != 0)) {
3794 FD_SET(ioh->fd, &rfds);
3795 if (ioh->fd > nfds)
3796 nfds = ioh->fd;
3797 }
3798 if (ioh->fd_write) {
3799 FD_SET(ioh->fd, &wfds);
3800 if (ioh->fd > nfds)
3801 nfds = ioh->fd;
3802 }
3803 }
ths3b46e622007-09-17 08:09:54 +00003804
aliguori56f3a5d2008-10-31 18:07:17 +00003805 tv.tv_sec = timeout / 1000;
3806 tv.tv_usec = (timeout % 1000) * 1000;
3807
bellarde0356492006-05-01 13:33:02 +00003808#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003809 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003810 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3811 }
3812#endif
3813 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00003814 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003815 IOHandlerRecord **pioh;
3816
3817 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003818 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003819 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003820 }
ths6ab43fd2007-08-25 01:34:19 +00003821 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003822 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003823 }
3824 }
thscafffd42007-02-28 21:59:44 +00003825
3826 /* remove deleted IO handlers */
3827 pioh = &first_io_handler;
3828 while (*pioh) {
3829 ioh = *pioh;
3830 if (ioh->deleted) {
3831 *pioh = ioh->next;
3832 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003833 } else
thscafffd42007-02-28 21:59:44 +00003834 pioh = &ioh->next;
3835 }
bellardfd1dff42006-02-01 21:29:26 +00003836 }
bellarde0356492006-05-01 13:33:02 +00003837#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003838 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003839 if (ret < 0) {
3840 FD_ZERO(&rfds);
3841 FD_ZERO(&wfds);
3842 FD_ZERO(&xfds);
3843 }
3844 slirp_select_poll(&rfds, &wfds, &xfds);
3845 }
3846#endif
bellardc20709a2004-04-21 23:27:19 +00003847
aliguori357c6922008-11-25 17:26:09 +00003848 /* vm time timers */
3849 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3850 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3851 qemu_get_clock(vm_clock));
3852
3853 /* real time timers */
3854 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3855 qemu_get_clock(rt_clock));
3856
pbrook423f0742007-05-23 00:06:54 +00003857 /* Check bottom-halves last in case any of the earlier events triggered
3858 them. */
3859 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003860
bellard5905b2e2004-08-01 21:53:26 +00003861}
3862
pbrook9596ebb2007-11-18 01:44:38 +00003863static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00003864{
3865 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00003866#ifdef CONFIG_PROFILER
3867 int64_t ti;
3868#endif
bellard6a00d602005-11-21 23:25:50 +00003869 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003870
bellard6a00d602005-11-21 23:25:50 +00003871 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00003872 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00003873 for(;;) {
3874 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00003875
bellard15a76442005-11-23 21:01:03 +00003876 for(;;) {
3877 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00003878 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00003879#ifdef CONFIG_PROFILER
3880 ti = profile_getclock();
3881#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003882 if (use_icount) {
3883 int64_t count;
3884 int decr;
3885 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3886 env->icount_decr.u16.low = 0;
3887 env->icount_extra = 0;
3888 count = qemu_next_deadline();
3889 count = (count + (1 << icount_time_shift) - 1)
3890 >> icount_time_shift;
3891 qemu_icount += count;
3892 decr = (count > 0xffff) ? 0xffff : count;
3893 count -= decr;
3894 env->icount_decr.u16.low = decr;
3895 env->icount_extra = count;
3896 }
bellard6a00d602005-11-21 23:25:50 +00003897 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00003898#ifdef CONFIG_PROFILER
3899 qemu_time += profile_getclock() - ti;
3900#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003901 if (use_icount) {
3902 /* Fold pending instructions back into the
3903 instruction counter, and clear the interrupt flag. */
3904 qemu_icount -= (env->icount_decr.u16.low
3905 + env->icount_extra);
3906 env->icount_decr.u32 = 0;
3907 env->icount_extra = 0;
3908 }
balrogee5605e2007-12-03 03:01:40 +00003909 next_cpu = env->next_cpu ?: first_cpu;
aurel3295b01002008-04-04 17:16:35 +00003910 if (event_pending && likely(ret != EXCP_DEBUG)) {
balrogee5605e2007-12-03 03:01:40 +00003911 ret = EXCP_INTERRUPT;
3912 event_pending = 0;
3913 break;
3914 }
pbrookbd967e02007-03-11 18:54:57 +00003915 if (ret == EXCP_HLT) {
3916 /* Give the next CPU a chance to run. */
3917 cur_cpu = env;
3918 continue;
3919 }
bellard15a76442005-11-23 21:01:03 +00003920 if (ret != EXCP_HALTED)
3921 break;
3922 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00003923 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00003924 break;
bellard15a76442005-11-23 21:01:03 +00003925 }
3926 cur_cpu = env;
3927
bellard5905b2e2004-08-01 21:53:26 +00003928 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00003929 ret = EXCP_INTERRUPT;
aurel32b2f76162008-04-11 21:35:52 +00003930 if (no_shutdown) {
3931 vm_stop(0);
3932 no_shutdown = 0;
3933 }
3934 else
3935 break;
bellard5905b2e2004-08-01 21:53:26 +00003936 }
3937 if (reset_requested) {
3938 reset_requested = 0;
3939 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00003940 ret = EXCP_INTERRUPT;
3941 }
3942 if (powerdown_requested) {
3943 powerdown_requested = 0;
3944 qemu_system_powerdown();
3945 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00003946 }
aurel3295b01002008-04-04 17:16:35 +00003947 if (unlikely(ret == EXCP_DEBUG)) {
aliguori880a7572008-11-18 20:30:24 +00003948 gdb_set_stop_cpu(cur_cpu);
bellard5905b2e2004-08-01 21:53:26 +00003949 vm_stop(EXCP_DEBUG);
3950 }
pbrookbd967e02007-03-11 18:54:57 +00003951 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00003952 /* XXX: use timeout computed from timers */
pbrook2e70f6e2008-06-29 01:03:05 +00003953 if (ret == EXCP_HALTED) {
3954 if (use_icount) {
3955 int64_t add;
3956 int64_t delta;
3957 /* Advance virtual time to the next event. */
3958 if (use_icount == 1) {
3959 /* When not using an adaptive execution frequency
3960 we tend to get badly out of sync with real time,
thsbf20dc02008-06-30 17:22:19 +00003961 so just delay for a reasonable amount of time. */
pbrook2e70f6e2008-06-29 01:03:05 +00003962 delta = 0;
3963 } else {
3964 delta = cpu_get_icount() - cpu_get_clock();
3965 }
3966 if (delta > 0) {
3967 /* If virtual time is ahead of real time then just
3968 wait for IO. */
3969 timeout = (delta / 1000000) + 1;
3970 } else {
3971 /* Wait for either IO to occur or the next
3972 timer event. */
3973 add = qemu_next_deadline();
3974 /* We advance the timer before checking for IO.
3975 Limit the amount we advance so that early IO
3976 activity won't get the guest too far ahead. */
3977 if (add > 10000000)
3978 add = 10000000;
3979 delta += add;
3980 add = (add + (1 << icount_time_shift) - 1)
3981 >> icount_time_shift;
3982 qemu_icount += add;
3983 timeout = delta / 1000000;
3984 if (timeout < 0)
3985 timeout = 0;
3986 }
3987 } else {
aliguori0a1af392008-10-31 18:40:25 +00003988 timeout = 5000;
pbrook2e70f6e2008-06-29 01:03:05 +00003989 }
3990 } else {
bellard5905b2e2004-08-01 21:53:26 +00003991 timeout = 0;
pbrook2e70f6e2008-06-29 01:03:05 +00003992 }
bellard5905b2e2004-08-01 21:53:26 +00003993 } else {
blueswir198448f52008-09-30 18:16:09 +00003994 if (shutdown_requested) {
3995 ret = EXCP_INTERRUPT;
aliguori5b08fc12008-08-21 20:08:03 +00003996 break;
blueswir198448f52008-09-30 18:16:09 +00003997 }
aliguori0a1af392008-10-31 18:40:25 +00003998 timeout = 5000;
bellard5905b2e2004-08-01 21:53:26 +00003999 }
bellard89bfc102006-02-08 22:46:31 +00004000#ifdef CONFIG_PROFILER
4001 ti = profile_getclock();
4002#endif
bellard5905b2e2004-08-01 21:53:26 +00004003 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00004004#ifdef CONFIG_PROFILER
4005 dev_time += profile_getclock() - ti;
4006#endif
bellardb4608c02003-06-27 17:34:32 +00004007 }
bellard34865132003-10-05 14:28:56 +00004008 cpu_disable_ticks();
4009 return ret;
bellardb4608c02003-06-27 17:34:32 +00004010}
4011
pbrook9bd7e6d2009-04-07 22:58:45 +00004012static void version(void)
4013{
pbrook4a19f1e2009-04-07 23:17:49 +00004014 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004015}
4016
ths15f82202007-06-29 23:26:08 +00004017static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004018{
pbrook9bd7e6d2009-04-07 22:58:45 +00004019 version();
4020 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004021 "\n"
bellarda20dd502003-09-30 21:07:02 +00004022 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004023 "\n"
blueswir15824d652009-03-28 06:44:27 +00004024#define DEF(option, opt_arg, opt_enum, opt_help) \
4025 opt_help
4026#define DEFHEADING(text) stringify(text) "\n"
4027#include "qemu-options.h"
4028#undef DEF
4029#undef DEFHEADING
4030#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004031 "\n"
bellard82c643f2004-07-14 17:28:13 +00004032 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004033 "ctrl-alt-f toggle full screen\n"
4034 "ctrl-alt-n switch to virtual console 'n'\n"
4035 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004036 "\n"
4037 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4038 ,
bellard0db63472003-10-27 21:37:46 +00004039 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004040 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004041#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004042 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004043 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004044#endif
bellard6e44ba72004-01-18 21:56:49 +00004045 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004046 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004047 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004048}
4049
bellardcd6f1162004-05-13 22:02:20 +00004050#define HAS_ARG 0x0001
4051
4052enum {
blueswir15824d652009-03-28 06:44:27 +00004053#define DEF(option, opt_arg, opt_enum, opt_help) \
4054 opt_enum,
4055#define DEFHEADING(text)
4056#include "qemu-options.h"
4057#undef DEF
4058#undef DEFHEADING
4059#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004060};
4061
4062typedef struct QEMUOption {
4063 const char *name;
4064 int flags;
4065 int index;
4066} QEMUOption;
4067
blueswir1dbed7e42008-10-01 19:38:09 +00004068static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004069 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004070#define DEF(option, opt_arg, opt_enum, opt_help) \
4071 { option, opt_arg, opt_enum },
4072#define DEFHEADING(text)
4073#include "qemu-options.h"
4074#undef DEF
4075#undef DEFHEADING
4076#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004077 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004078};
4079
bellard1d14ffa2005-10-30 18:58:22 +00004080#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004081struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004082#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004083#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004084 {
4085 "pcspk",
4086 "PC speaker",
4087 0,
4088 1,
4089 { .init_isa = pcspk_audio_init }
4090 },
4091#endif
malc4c9b53e2009-01-09 10:46:34 +00004092
4093#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004094 {
4095 "sb16",
4096 "Creative Sound Blaster 16",
4097 0,
4098 1,
4099 { .init_isa = SB16_init }
4100 },
malc4c9b53e2009-01-09 10:46:34 +00004101#endif
bellard6a36d842005-12-18 20:34:32 +00004102
malccc53d262008-06-13 10:48:22 +00004103#ifdef CONFIG_CS4231A
4104 {
4105 "cs4231a",
4106 "CS4231A",
4107 0,
4108 1,
4109 { .init_isa = cs4231a_init }
4110 },
4111#endif
4112
bellard6a36d842005-12-18 20:34:32 +00004113#ifdef CONFIG_ADLIB
4114 {
4115 "adlib",
4116#ifdef HAS_YMF262
4117 "Yamaha YMF262 (OPL3)",
4118#else
4119 "Yamaha YM3812 (OPL2)",
4120#endif
4121 0,
4122 1,
4123 { .init_isa = Adlib_init }
4124 },
4125#endif
4126
4127#ifdef CONFIG_GUS
4128 {
4129 "gus",
4130 "Gravis Ultrasound GF1",
4131 0,
4132 1,
4133 { .init_isa = GUS_init }
4134 },
4135#endif
4136
malc4c9b53e2009-01-09 10:46:34 +00004137#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004138 {
4139 "ac97",
4140 "Intel 82801AA AC97 Audio",
4141 0,
4142 0,
4143 { .init_pci = ac97_init }
4144 },
malc4c9b53e2009-01-09 10:46:34 +00004145#endif
balroge5c9a132008-01-14 04:27:55 +00004146
malc4c9b53e2009-01-09 10:46:34 +00004147#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004148 {
4149 "es1370",
4150 "ENSONIQ AudioPCI ES1370",
4151 0,
4152 0,
4153 { .init_pci = es1370_init }
4154 },
balrogb00052e2007-04-30 02:22:06 +00004155#endif
bellard6a36d842005-12-18 20:34:32 +00004156
malc4c9b53e2009-01-09 10:46:34 +00004157#endif /* HAS_AUDIO_CHOICE */
4158
bellard6a36d842005-12-18 20:34:32 +00004159 { NULL, NULL, 0, 0, { NULL } }
4160};
4161
bellard1d14ffa2005-10-30 18:58:22 +00004162static void select_soundhw (const char *optarg)
4163{
bellard6a36d842005-12-18 20:34:32 +00004164 struct soundhw *c;
4165
bellard1d14ffa2005-10-30 18:58:22 +00004166 if (*optarg == '?') {
4167 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004168
bellard1d14ffa2005-10-30 18:58:22 +00004169 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004170 for (c = soundhw; c->name; ++c) {
4171 printf ("%-11s %s\n", c->name, c->descr);
4172 }
4173 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004174 exit (*optarg != '?');
4175 }
4176 else {
bellard6a36d842005-12-18 20:34:32 +00004177 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004178 const char *p;
4179 char *e;
4180 int bad_card = 0;
4181
bellard6a36d842005-12-18 20:34:32 +00004182 if (!strcmp (optarg, "all")) {
4183 for (c = soundhw; c->name; ++c) {
4184 c->enabled = 1;
4185 }
4186 return;
4187 }
bellard1d14ffa2005-10-30 18:58:22 +00004188
bellard6a36d842005-12-18 20:34:32 +00004189 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004190 while (*p) {
4191 e = strchr (p, ',');
4192 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004193
4194 for (c = soundhw; c->name; ++c) {
4195 if (!strncmp (c->name, p, l)) {
4196 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004197 break;
4198 }
4199 }
bellard6a36d842005-12-18 20:34:32 +00004200
4201 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004202 if (l > 80) {
4203 fprintf (stderr,
4204 "Unknown sound card name (too big to show)\n");
4205 }
4206 else {
4207 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4208 (int) l, p);
4209 }
4210 bad_card = 1;
4211 }
4212 p += l + (e != NULL);
4213 }
4214
4215 if (bad_card)
4216 goto show_valid_cards;
4217 }
4218}
4219#endif
4220
malc3893c122008-09-28 00:42:05 +00004221static void select_vgahw (const char *p)
4222{
4223 const char *opts;
4224
aliguori28b85ed2009-04-22 15:19:48 +00004225 cirrus_vga_enabled = 0;
4226 std_vga_enabled = 0;
4227 vmsvga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004228 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004229 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004230 } else if (strstart(p, "cirrus", &opts)) {
4231 cirrus_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004232 } else if (strstart(p, "vmware", &opts)) {
malc3893c122008-09-28 00:42:05 +00004233 vmsvga_enabled = 1;
aliguori28b85ed2009-04-22 15:19:48 +00004234 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004235 invalid_vga:
4236 fprintf(stderr, "Unknown vga type: %s\n", p);
4237 exit(1);
4238 }
malccb5a7aa2008-09-28 00:42:12 +00004239 while (*opts) {
4240 const char *nextopt;
4241
4242 if (strstart(opts, ",retrace=", &nextopt)) {
4243 opts = nextopt;
4244 if (strstart(opts, "dumb", &nextopt))
4245 vga_retrace_method = VGA_RETRACE_DUMB;
4246 else if (strstart(opts, "precise", &nextopt))
4247 vga_retrace_method = VGA_RETRACE_PRECISE;
4248 else goto invalid_vga;
4249 } else goto invalid_vga;
4250 opts = nextopt;
4251 }
malc3893c122008-09-28 00:42:05 +00004252}
4253
bellard3587d7e2006-06-26 20:03:44 +00004254#ifdef _WIN32
4255static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4256{
4257 exit(STATUS_CONTROL_C_EXIT);
4258 return TRUE;
4259}
4260#endif
4261
aliguoric4be29f2009-04-17 18:58:14 +00004262int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004263{
4264 int ret;
4265
4266 if(strlen(str) != 36)
4267 return -1;
4268
4269 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4270 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4271 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4272
4273 if(ret != 16)
4274 return -1;
4275
aliguorib6f6e3d2009-04-17 18:59:56 +00004276#ifdef TARGET_I386
4277 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4278#endif
4279
blueswir18fcb1b92008-09-18 18:29:08 +00004280 return 0;
4281}
4282
bellard7c9d8e02005-11-15 22:16:05 +00004283#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004284
aliguori5b08fc12008-08-21 20:08:03 +00004285#ifndef _WIN32
4286
4287static void termsig_handler(int signal)
4288{
4289 qemu_system_shutdown_request();
4290}
4291
blueswir16f9e3802008-09-09 18:56:59 +00004292static void termsig_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004293{
4294 struct sigaction act;
4295
4296 memset(&act, 0, sizeof(act));
4297 act.sa_handler = termsig_handler;
4298 sigaction(SIGINT, &act, NULL);
4299 sigaction(SIGHUP, &act, NULL);
4300 sigaction(SIGTERM, &act, NULL);
4301}
4302
4303#endif
4304
malc902b3d52008-12-10 19:18:40 +00004305int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004306{
bellard67b915a2004-03-31 23:37:16 +00004307#ifdef CONFIG_GDBSTUB
aliguori59030a82009-04-05 18:43:41 +00004308 const char *gdbstub_dev = NULL;
bellard67b915a2004-03-31 23:37:16 +00004309#endif
j_mayer28c5af52007-11-11 01:50:45 +00004310 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004311 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004312 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004313 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004314 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00004315 const char *boot_devices = "";
aliguori3023f332009-01-16 19:04:14 +00004316 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004317 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004318 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004319 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004320 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004321 const char *bt_opts[MAX_BT_CMDLINE];
4322 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004323 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004324 int optind;
4325 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004326 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004327 const char *monitor_device;
4328 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004329 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004330 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004331 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004332 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4333 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004334 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004335 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004336 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004337 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004338 int usb_devices_index;
blueswir1b9e82a52009-04-05 18:03:31 +00004339#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004340 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004341#endif
bellard26a5f132008-05-28 12:30:31 +00004342 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004343 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004344 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004345#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004346 int fd = 0;
4347 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004348 const char *chroot_dir = NULL;
4349 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004350#endif
aliguori268a3622009-04-21 22:30:27 +00004351 CPUState *env;
bellard0bd48852005-11-11 00:00:47 +00004352
malc902b3d52008-12-10 19:18:40 +00004353 qemu_cache_utils_init(envp);
4354
bellard0bd48852005-11-11 00:00:47 +00004355 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004356#ifndef _WIN32
4357 {
4358 struct sigaction act;
4359 sigfillset(&act.sa_mask);
4360 act.sa_flags = 0;
4361 act.sa_handler = SIG_IGN;
4362 sigaction(SIGPIPE, &act, NULL);
4363 }
bellard3587d7e2006-06-26 20:03:44 +00004364#else
4365 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004366 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4367 QEMU to run on a single CPU */
4368 {
4369 HANDLE h;
4370 DWORD mask, smask;
4371 int i;
4372 h = GetCurrentProcess();
4373 if (GetProcessAffinityMask(h, &mask, &smask)) {
4374 for(i = 0; i < 32; i++) {
4375 if (mask & (1 << i))
4376 break;
4377 }
4378 if (i != 32) {
4379 mask = 1 << i;
4380 SetProcessAffinityMask(h, mask);
4381 }
4382 }
4383 }
bellard67b915a2004-03-31 23:37:16 +00004384#endif
bellardbe995c22006-06-25 16:25:21 +00004385
bellardcc1daa42005-06-05 14:49:17 +00004386 register_machines();
4387 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004388 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004389 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004390 ram_size = 0;
bellard313aa562003-08-10 21:52:11 +00004391 vga_ram_size = VGA_RAM_SIZE;
bellard33e39632003-07-06 17:15:21 +00004392 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004393 nographic = 0;
balrog4d3b6f62008-02-10 16:33:14 +00004394 curses = 0;
bellarda20dd502003-09-30 21:07:02 +00004395 kernel_filename = NULL;
4396 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004397 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004398 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00004399 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00004400
aurel32c75a8232008-05-04 00:50:34 +00004401 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004402 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004403 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004404 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004405
aliguori8290edd2009-02-27 20:14:29 +00004406 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004407 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004408 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004409 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004410
aliguori1b8fc812009-02-27 20:01:39 +00004411 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004412 virtio_consoles[i] = NULL;
4413 virtio_console_index = 0;
4414
aliguori268a3622009-04-21 22:30:27 +00004415 for (i = 0; i < MAX_NODES; i++) {
4416 node_mem[i] = 0;
4417 node_cpumask[i] = 0;
4418 }
4419
bellarda594cfb2005-11-06 16:13:29 +00004420 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004421
bellard7c9d8e02005-11-15 22:16:05 +00004422 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004423 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004424 nb_drives = 0;
4425 nb_drives_opt = 0;
aliguori268a3622009-04-21 22:30:27 +00004426 nb_numa_nodes = 0;
thse4bcb142007-12-02 04:51:10 +00004427 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004428
4429 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004430
bellard26a5f132008-05-28 12:30:31 +00004431 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004432 autostart= 1;
4433
bellardcd6f1162004-05-13 22:02:20 +00004434 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004435 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004436 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004437 break;
bellardcd6f1162004-05-13 22:02:20 +00004438 r = argv[optind];
4439 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004440 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004441 } else {
4442 const QEMUOption *popt;
4443
4444 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004445 /* Treat --foo the same as -foo. */
4446 if (r[1] == '-')
4447 r++;
bellardcd6f1162004-05-13 22:02:20 +00004448 popt = qemu_options;
4449 for(;;) {
4450 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004451 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004452 argv[0], r);
4453 exit(1);
4454 }
4455 if (!strcmp(popt->name, r + 1))
4456 break;
4457 popt++;
4458 }
4459 if (popt->flags & HAS_ARG) {
4460 if (optind >= argc) {
4461 fprintf(stderr, "%s: option '%s' requires an argument\n",
4462 argv[0], r);
4463 exit(1);
4464 }
4465 optarg = argv[optind++];
4466 } else {
4467 optarg = NULL;
4468 }
4469
4470 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004471 case QEMU_OPTION_M:
4472 machine = find_machine(optarg);
4473 if (!machine) {
4474 QEMUMachine *m;
4475 printf("Supported machines are:\n");
4476 for(m = first_machine; m != NULL; m = m->next) {
4477 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004478 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00004479 m == first_machine ? " (default)" : "");
4480 }
ths15f82202007-06-29 23:26:08 +00004481 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004482 }
4483 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004484 case QEMU_OPTION_cpu:
4485 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004486 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004487/* XXX: implement xxx_cpu_list for targets that still miss it */
4488#if defined(cpu_list)
4489 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004490#endif
ths15f82202007-06-29 23:26:08 +00004491 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004492 } else {
4493 cpu_model = optarg;
4494 }
4495 break;
bellardcd6f1162004-05-13 22:02:20 +00004496 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004497 initrd_filename = optarg;
4498 break;
bellardcd6f1162004-05-13 22:02:20 +00004499 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004500 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00004501 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004502 else
balrog609497a2008-01-14 02:56:53 +00004503 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004504 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004505 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004506 translation == BIOS_ATA_TRANSLATION_LBA ?
4507 ",trans=lba" :
4508 translation == BIOS_ATA_TRANSLATION_NONE ?
4509 ",trans=none" : "");
4510 break;
bellardcd6f1162004-05-13 22:02:20 +00004511 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004512 case QEMU_OPTION_hdc:
4513 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004514 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004515 break;
thse4bcb142007-12-02 04:51:10 +00004516 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004517 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004518 break;
balrog3e3d5812007-04-30 02:09:25 +00004519 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004520 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004521 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004522 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004523 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004524 break;
j_mayer86f55662007-04-24 06:52:59 +00004525 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004526 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004527 break;
bellardcd6f1162004-05-13 22:02:20 +00004528 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004529 snapshot = 1;
4530 break;
bellardcd6f1162004-05-13 22:02:20 +00004531 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004532 {
bellard330d0412003-07-26 18:11:40 +00004533 const char *p;
4534 p = optarg;
4535 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004536 if (cyls < 1 || cyls > 16383)
4537 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004538 if (*p != ',')
4539 goto chs_fail;
4540 p++;
4541 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004542 if (heads < 1 || heads > 16)
4543 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004544 if (*p != ',')
4545 goto chs_fail;
4546 p++;
4547 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004548 if (secs < 1 || secs > 63)
4549 goto chs_fail;
4550 if (*p == ',') {
4551 p++;
4552 if (!strcmp(p, "none"))
4553 translation = BIOS_ATA_TRANSLATION_NONE;
4554 else if (!strcmp(p, "lba"))
4555 translation = BIOS_ATA_TRANSLATION_LBA;
4556 else if (!strcmp(p, "auto"))
4557 translation = BIOS_ATA_TRANSLATION_AUTO;
4558 else
4559 goto chs_fail;
4560 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004561 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004562 fprintf(stderr, "qemu: invalid physical CHS format\n");
4563 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004564 }
thse4bcb142007-12-02 04:51:10 +00004565 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00004566 snprintf(drives_opt[hda_index].opt,
4567 sizeof(drives_opt[hda_index].opt),
4568 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4569 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004570 translation == BIOS_ATA_TRANSLATION_LBA ?
4571 ",trans=lba" :
4572 translation == BIOS_ATA_TRANSLATION_NONE ?
4573 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00004574 }
4575 break;
aliguori268a3622009-04-21 22:30:27 +00004576 case QEMU_OPTION_numa:
4577 if (nb_numa_nodes >= MAX_NODES) {
4578 fprintf(stderr, "qemu: too many NUMA nodes\n");
4579 exit(1);
4580 }
4581 numa_add(optarg);
4582 break;
bellardcd6f1162004-05-13 22:02:20 +00004583 case QEMU_OPTION_nographic:
bellarda20dd502003-09-30 21:07:02 +00004584 nographic = 1;
4585 break;
balrog4d3b6f62008-02-10 16:33:14 +00004586#ifdef CONFIG_CURSES
4587 case QEMU_OPTION_curses:
4588 curses = 1;
4589 break;
4590#endif
balroga171fe32007-04-30 01:48:07 +00004591 case QEMU_OPTION_portrait:
4592 graphic_rotate = 1;
4593 break;
bellardcd6f1162004-05-13 22:02:20 +00004594 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004595 kernel_filename = optarg;
4596 break;
bellardcd6f1162004-05-13 22:02:20 +00004597 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004598 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004599 break;
bellardcd6f1162004-05-13 22:02:20 +00004600 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004601 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004602 break;
bellardcd6f1162004-05-13 22:02:20 +00004603 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004604 boot_devices = optarg;
4605 /* We just do some generic consistency checks */
4606 {
4607 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00004608 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00004609
4610 boot_devices_bitmap = 0;
4611 for (p = boot_devices; *p != '\0'; p++) {
4612 /* Allowed boot devices are:
4613 * a b : floppy disk drives
4614 * c ... f : IDE disk drives
4615 * g ... m : machine implementation dependant drives
4616 * n ... p : network devices
4617 * It's up to each machine implementation to check
4618 * if the given boot devices match the actual hardware
4619 * implementation and firmware features.
4620 */
4621 if (*p < 'a' || *p > 'q') {
4622 fprintf(stderr, "Invalid boot device '%c'\n", *p);
4623 exit(1);
4624 }
4625 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4626 fprintf(stderr,
4627 "Boot device '%c' was given twice\n",*p);
4628 exit(1);
4629 }
4630 boot_devices_bitmap |= 1 << (*p - 'a');
4631 }
bellard36b486b2003-11-11 13:36:08 +00004632 }
4633 break;
bellardcd6f1162004-05-13 22:02:20 +00004634 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004635 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004636 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004637 break;
bellard52ca8d62006-06-14 16:03:05 +00004638#ifdef TARGET_I386
4639 case QEMU_OPTION_no_fd_bootchk:
4640 fd_bootchk = 0;
4641 break;
4642#endif
bellard7c9d8e02005-11-15 22:16:05 +00004643 case QEMU_OPTION_net:
4644 if (nb_net_clients >= MAX_NET_CLIENTS) {
4645 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00004646 exit(1);
4647 }
pbrookfd5f3932008-03-26 20:55:43 +00004648 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00004649 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00004650 break;
bellardc7f74642004-08-24 21:57:12 +00004651#ifdef CONFIG_SLIRP
4652 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00004653 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004654 break;
ths47d5d012007-02-20 00:05:08 +00004655 case QEMU_OPTION_bootp:
4656 bootp_filename = optarg;
4657 break;
bellardc94c8d62004-09-13 21:37:34 +00004658#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004659 case QEMU_OPTION_smb:
4660 net_slirp_smb(optarg);
4661 break;
bellardc94c8d62004-09-13 21:37:34 +00004662#endif
bellard9bf05442004-08-25 22:12:49 +00004663 case QEMU_OPTION_redir:
aliguorid4ebe192009-04-21 19:56:44 +00004664 net_slirp_redir(NULL, optarg);
bellard9bf05442004-08-25 22:12:49 +00004665 break;
bellardc7f74642004-08-24 21:57:12 +00004666#endif
balrogdc72ac12008-11-09 00:04:26 +00004667 case QEMU_OPTION_bt:
4668 if (nb_bt_opts >= MAX_BT_CMDLINE) {
4669 fprintf(stderr, "qemu: too many bluetooth options\n");
4670 exit(1);
4671 }
4672 bt_opts[nb_bt_opts++] = optarg;
4673 break;
bellard1d14ffa2005-10-30 18:58:22 +00004674#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004675 case QEMU_OPTION_audio_help:
4676 AUD_help ();
4677 exit (0);
4678 break;
4679 case QEMU_OPTION_soundhw:
4680 select_soundhw (optarg);
4681 break;
4682#endif
bellardcd6f1162004-05-13 22:02:20 +00004683 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00004684 help(0);
bellardcd6f1162004-05-13 22:02:20 +00004685 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00004686 case QEMU_OPTION_version:
4687 version();
4688 exit(0);
4689 break;
aurel3200f82b82008-04-27 21:12:55 +00004690 case QEMU_OPTION_m: {
4691 uint64_t value;
4692 char *ptr;
4693
4694 value = strtoul(optarg, &ptr, 10);
4695 switch (*ptr) {
4696 case 0: case 'M': case 'm':
4697 value <<= 20;
4698 break;
4699 case 'G': case 'g':
4700 value <<= 30;
4701 break;
4702 default:
4703 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00004704 exit(1);
4705 }
aurel3200f82b82008-04-27 21:12:55 +00004706
4707 /* On 32-bit hosts, QEMU is limited by virtual address space */
4708 if (value > (2047 << 20)
blueswir1640f42e2009-04-19 10:18:01 +00004709#ifndef CONFIG_KQEMU
aurel3200f82b82008-04-27 21:12:55 +00004710 && HOST_LONG_BITS == 32
4711#endif
4712 ) {
4713 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4714 exit(1);
4715 }
4716 if (value != (uint64_t)(ram_addr_t)value) {
4717 fprintf(stderr, "qemu: ram size too large\n");
4718 exit(1);
4719 }
4720 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00004721 break;
aurel3200f82b82008-04-27 21:12:55 +00004722 }
bellardcd6f1162004-05-13 22:02:20 +00004723 case QEMU_OPTION_d:
4724 {
4725 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00004726 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00004727
bellardcd6f1162004-05-13 22:02:20 +00004728 mask = cpu_str_to_log_mask(optarg);
4729 if (!mask) {
4730 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00004731 for(item = cpu_log_items; item->mask != 0; item++) {
4732 printf("%-10s %s\n", item->name, item->help);
4733 }
4734 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00004735 }
4736 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00004737 }
bellardcd6f1162004-05-13 22:02:20 +00004738 break;
bellard67b915a2004-03-31 23:37:16 +00004739#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00004740 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00004741 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00004742 break;
aliguori59030a82009-04-05 18:43:41 +00004743 case QEMU_OPTION_gdb:
4744 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00004745 break;
bellard67b915a2004-03-31 23:37:16 +00004746#endif
bellardcd6f1162004-05-13 22:02:20 +00004747 case QEMU_OPTION_L:
4748 bios_dir = optarg;
4749 break;
j_mayer1192dad2007-10-05 13:08:35 +00004750 case QEMU_OPTION_bios:
4751 bios_name = optarg;
4752 break;
aurel321b530a62009-04-05 20:08:59 +00004753 case QEMU_OPTION_singlestep:
4754 singlestep = 1;
4755 break;
bellardcd6f1162004-05-13 22:02:20 +00004756 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00004757 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00004758 break;
blueswir15824d652009-03-28 06:44:27 +00004759#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00004760 case QEMU_OPTION_k:
4761 keyboard_layout = optarg;
4762 break;
blueswir15824d652009-03-28 06:44:27 +00004763#endif
bellardee22c2f2004-06-03 12:49:50 +00004764 case QEMU_OPTION_localtime:
4765 rtc_utc = 0;
4766 break;
malc3893c122008-09-28 00:42:05 +00004767 case QEMU_OPTION_vga:
4768 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00004769 break;
blueswir15824d652009-03-28 06:44:27 +00004770#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00004771 case QEMU_OPTION_g:
4772 {
4773 const char *p;
4774 int w, h, depth;
4775 p = optarg;
4776 w = strtol(p, (char **)&p, 10);
4777 if (w <= 0) {
4778 graphic_error:
4779 fprintf(stderr, "qemu: invalid resolution or depth\n");
4780 exit(1);
4781 }
4782 if (*p != 'x')
4783 goto graphic_error;
4784 p++;
4785 h = strtol(p, (char **)&p, 10);
4786 if (h <= 0)
4787 goto graphic_error;
4788 if (*p == 'x') {
4789 p++;
4790 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00004791 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00004792 depth != 24 && depth != 32)
4793 goto graphic_error;
4794 } else if (*p == '\0') {
4795 depth = graphic_depth;
4796 } else {
4797 goto graphic_error;
4798 }
ths3b46e622007-09-17 08:09:54 +00004799
bellarde9b137c2004-06-21 16:46:10 +00004800 graphic_width = w;
4801 graphic_height = h;
4802 graphic_depth = depth;
4803 }
4804 break;
blueswir15824d652009-03-28 06:44:27 +00004805#endif
ths20d8a3e2007-02-18 17:04:49 +00004806 case QEMU_OPTION_echr:
4807 {
4808 char *r;
4809 term_escape_char = strtol(optarg, &r, 0);
4810 if (r == optarg)
4811 printf("Bad argument to echr\n");
4812 break;
4813 }
bellard82c643f2004-07-14 17:28:13 +00004814 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00004815 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00004816 break;
4817 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00004818 if (serial_device_index >= MAX_SERIAL_PORTS) {
4819 fprintf(stderr, "qemu: too many serial ports\n");
4820 exit(1);
4821 }
pbrookfd5f3932008-03-26 20:55:43 +00004822 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00004823 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00004824 break;
aliguori51ecf132009-01-15 20:06:40 +00004825 case QEMU_OPTION_virtiocon:
4826 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
4827 fprintf(stderr, "qemu: too many virtio consoles\n");
4828 exit(1);
4829 }
4830 virtio_consoles[virtio_console_index] = optarg;
4831 virtio_console_index++;
4832 break;
bellard6508fe52005-01-15 12:02:56 +00004833 case QEMU_OPTION_parallel:
4834 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
4835 fprintf(stderr, "qemu: too many parallel ports\n");
4836 exit(1);
4837 }
pbrookfd5f3932008-03-26 20:55:43 +00004838 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00004839 parallel_device_index++;
4840 break;
bellardd63d3072004-10-03 13:29:03 +00004841 case QEMU_OPTION_loadvm:
4842 loadvm = optarg;
4843 break;
4844 case QEMU_OPTION_full_screen:
4845 full_screen = 1;
4846 break;
ths667acca2006-12-11 02:08:05 +00004847#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00004848 case QEMU_OPTION_no_frame:
4849 no_frame = 1;
4850 break;
ths3780e192007-06-21 21:08:02 +00004851 case QEMU_OPTION_alt_grab:
4852 alt_grab = 1;
4853 break;
ths667acca2006-12-11 02:08:05 +00004854 case QEMU_OPTION_no_quit:
4855 no_quit = 1;
4856 break;
aliguori7d957bd2009-01-15 22:14:11 +00004857 case QEMU_OPTION_sdl:
4858 sdl = 1;
4859 break;
ths667acca2006-12-11 02:08:05 +00004860#endif
bellardf7cce892004-12-08 22:21:25 +00004861 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00004862 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00004863 break;
bellarda09db212005-04-30 16:10:35 +00004864#ifdef TARGET_I386
4865 case QEMU_OPTION_win2k_hack:
4866 win2k_install_hack = 1;
4867 break;
aliguori73822ec2009-01-15 20:11:34 +00004868 case QEMU_OPTION_rtc_td_hack:
4869 rtc_td_hack = 1;
4870 break;
aliguori8a92ea22009-02-27 20:12:36 +00004871 case QEMU_OPTION_acpitable:
4872 if(acpi_table_add(optarg) < 0) {
4873 fprintf(stderr, "Wrong acpi table provided\n");
4874 exit(1);
4875 }
4876 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00004877 case QEMU_OPTION_smbios:
4878 if(smbios_entry_add(optarg) < 0) {
4879 fprintf(stderr, "Wrong smbios provided\n");
4880 exit(1);
4881 }
4882 break;
bellarda09db212005-04-30 16:10:35 +00004883#endif
blueswir1640f42e2009-04-19 10:18:01 +00004884#ifdef CONFIG_KQEMU
bellardd993e022005-02-10 22:00:06 +00004885 case QEMU_OPTION_no_kqemu:
4886 kqemu_allowed = 0;
4887 break;
bellard89bfc102006-02-08 22:46:31 +00004888 case QEMU_OPTION_kernel_kqemu:
4889 kqemu_allowed = 2;
4890 break;
bellardd993e022005-02-10 22:00:06 +00004891#endif
aliguori7ba1e612008-11-05 16:04:33 +00004892#ifdef CONFIG_KVM
4893 case QEMU_OPTION_enable_kvm:
4894 kvm_allowed = 1;
blueswir1640f42e2009-04-19 10:18:01 +00004895#ifdef CONFIG_KQEMU
aliguori7ba1e612008-11-05 16:04:33 +00004896 kqemu_allowed = 0;
4897#endif
4898 break;
4899#endif
bellardbb36d472005-11-05 14:22:28 +00004900 case QEMU_OPTION_usb:
4901 usb_enabled = 1;
4902 break;
bellarda594cfb2005-11-06 16:13:29 +00004903 case QEMU_OPTION_usbdevice:
4904 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00004905 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00004906 fprintf(stderr, "Too many USB devices\n");
4907 exit(1);
4908 }
pbrookfd5f3932008-03-26 20:55:43 +00004909 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00004910 usb_devices_index++;
4911 break;
bellard6a00d602005-11-21 23:25:50 +00004912 case QEMU_OPTION_smp:
4913 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00004914 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00004915 fprintf(stderr, "Invalid number of CPUs\n");
4916 exit(1);
4917 }
4918 break;
bellard24236862006-04-30 21:28:36 +00004919 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00004920 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00004921 break;
blueswir15824d652009-03-28 06:44:27 +00004922#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00004923 case QEMU_OPTION_no_acpi:
4924 acpi_enabled = 0;
4925 break;
aliguori16b29ae2008-12-17 23:28:44 +00004926 case QEMU_OPTION_no_hpet:
4927 no_hpet = 1;
4928 break;
blueswir15824d652009-03-28 06:44:27 +00004929#endif
bellardd1beab82006-10-02 19:44:22 +00004930 case QEMU_OPTION_no_reboot:
4931 no_reboot = 1;
4932 break;
aurel32b2f76162008-04-11 21:35:52 +00004933 case QEMU_OPTION_no_shutdown:
4934 no_shutdown = 1;
4935 break;
balrog9467cd42007-05-01 01:34:14 +00004936 case QEMU_OPTION_show_cursor:
4937 cursor_hide = 0;
4938 break;
blueswir18fcb1b92008-09-18 18:29:08 +00004939 case QEMU_OPTION_uuid:
4940 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
4941 fprintf(stderr, "Fail to parse UUID string."
4942 " Wrong format.\n");
4943 exit(1);
4944 }
4945 break;
blueswir15824d652009-03-28 06:44:27 +00004946#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004947 case QEMU_OPTION_daemonize:
4948 daemonize = 1;
4949 break;
blueswir15824d652009-03-28 06:44:27 +00004950#endif
ths9ae02552007-01-05 17:39:04 +00004951 case QEMU_OPTION_option_rom:
4952 if (nb_option_roms >= MAX_OPTION_ROMS) {
4953 fprintf(stderr, "Too many option ROMs\n");
4954 exit(1);
4955 }
4956 option_rom[nb_option_roms] = optarg;
4957 nb_option_roms++;
4958 break;
blueswir15824d652009-03-28 06:44:27 +00004959#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00004960 case QEMU_OPTION_semihosting:
4961 semihosting_enabled = 1;
4962 break;
blueswir15824d652009-03-28 06:44:27 +00004963#endif
thsc35734b2007-03-19 15:17:08 +00004964 case QEMU_OPTION_name:
4965 qemu_name = optarg;
4966 break;
blueswir195efd112008-12-24 20:26:14 +00004967#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00004968 case QEMU_OPTION_prom_env:
4969 if (nb_prom_envs >= MAX_PROM_ENVS) {
4970 fprintf(stderr, "Too many prom variables\n");
4971 exit(1);
4972 }
4973 prom_envs[nb_prom_envs] = optarg;
4974 nb_prom_envs++;
4975 break;
4976#endif
balrog2b8f2d42007-07-27 22:08:46 +00004977#ifdef TARGET_ARM
4978 case QEMU_OPTION_old_param:
4979 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00004980 break;
balrog2b8f2d42007-07-27 22:08:46 +00004981#endif
thsf3dcfad2007-08-24 01:26:02 +00004982 case QEMU_OPTION_clock:
4983 configure_alarms(optarg);
4984 break;
bellard7e0af5d02007-11-07 16:24:33 +00004985 case QEMU_OPTION_startdate:
4986 {
4987 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00004988 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00004989 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00004990 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00004991 } else {
4992 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4993 &tm.tm_year,
4994 &tm.tm_mon,
4995 &tm.tm_mday,
4996 &tm.tm_hour,
4997 &tm.tm_min,
4998 &tm.tm_sec) == 6) {
4999 /* OK */
5000 } else if (sscanf(optarg, "%d-%d-%d",
5001 &tm.tm_year,
5002 &tm.tm_mon,
5003 &tm.tm_mday) == 3) {
5004 tm.tm_hour = 0;
5005 tm.tm_min = 0;
5006 tm.tm_sec = 0;
5007 } else {
5008 goto date_fail;
5009 }
5010 tm.tm_year -= 1900;
5011 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005012 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005013 if (rtc_start_date == -1) {
5014 date_fail:
5015 fprintf(stderr, "Invalid date format. Valid format are:\n"
5016 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5017 exit(1);
5018 }
balrogf6503052008-02-17 11:42:19 +00005019 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005020 }
5021 }
5022 break;
bellard26a5f132008-05-28 12:30:31 +00005023 case QEMU_OPTION_tb_size:
5024 tb_size = strtol(optarg, NULL, 0);
5025 if (tb_size < 0)
5026 tb_size = 0;
5027 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005028 case QEMU_OPTION_icount:
5029 use_icount = 1;
5030 if (strcmp(optarg, "auto") == 0) {
5031 icount_time_shift = -1;
5032 } else {
5033 icount_time_shift = strtol(optarg, NULL, 0);
5034 }
5035 break;
aliguori5bb79102008-10-13 03:12:02 +00005036 case QEMU_OPTION_incoming:
5037 incoming = optarg;
5038 break;
blueswir15824d652009-03-28 06:44:27 +00005039#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005040 case QEMU_OPTION_chroot:
5041 chroot_dir = optarg;
5042 break;
5043 case QEMU_OPTION_runas:
5044 run_as = optarg;
5045 break;
blueswir15824d652009-03-28 06:44:27 +00005046#endif
aliguorie37630c2009-04-22 15:19:10 +00005047#ifdef CONFIG_XEN
5048 case QEMU_OPTION_xen_domid:
5049 xen_domid = atoi(optarg);
5050 break;
5051 case QEMU_OPTION_xen_create:
5052 xen_mode = XEN_CREATE;
5053 break;
5054 case QEMU_OPTION_xen_attach:
5055 xen_mode = XEN_ATTACH;
5056 break;
5057#endif
bellardcd6f1162004-05-13 22:02:20 +00005058 }
bellard0824d6f2003-06-24 13:42:40 +00005059 }
5060 }
bellard330d0412003-07-26 18:11:40 +00005061
blueswir1640f42e2009-04-19 10:18:01 +00005062#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
aliguori7ba1e612008-11-05 16:04:33 +00005063 if (kvm_allowed && kqemu_allowed) {
5064 fprintf(stderr,
5065 "You can not enable both KVM and kqemu at the same time\n");
5066 exit(1);
5067 }
5068#endif
5069
balrog3d878ca2008-10-28 10:59:59 +00005070 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005071 if (smp_cpus > machine->max_cpus) {
5072 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5073 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5074 machine->max_cpus);
5075 exit(1);
5076 }
5077
aliguoribc0129d2008-08-01 15:12:34 +00005078 if (nographic) {
5079 if (serial_device_index == 0)
5080 serial_devices[0] = "stdio";
5081 if (parallel_device_index == 0)
5082 parallel_devices[0] = "null";
5083 if (strncmp(monitor_device, "vc", 2) == 0)
5084 monitor_device = "stdio";
5085 }
5086
ths71e3ceb2006-12-22 02:11:31 +00005087#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005088 if (daemonize) {
5089 pid_t pid;
5090
5091 if (pipe(fds) == -1)
5092 exit(1);
5093
5094 pid = fork();
5095 if (pid > 0) {
5096 uint8_t status;
5097 ssize_t len;
5098
5099 close(fds[1]);
5100
5101 again:
ths93815bc2007-03-19 15:58:31 +00005102 len = read(fds[0], &status, 1);
5103 if (len == -1 && (errno == EINTR))
5104 goto again;
5105
5106 if (len != 1)
5107 exit(1);
5108 else if (status == 1) {
5109 fprintf(stderr, "Could not acquire pidfile\n");
5110 exit(1);
5111 } else
5112 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005113 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005114 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005115
5116 setsid();
5117
5118 pid = fork();
5119 if (pid > 0)
5120 exit(0);
5121 else if (pid < 0)
5122 exit(1);
5123
5124 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005125
5126 signal(SIGTSTP, SIG_IGN);
5127 signal(SIGTTOU, SIG_IGN);
5128 signal(SIGTTIN, SIG_IGN);
5129 }
ths71e3ceb2006-12-22 02:11:31 +00005130
thsaa26bb22007-03-25 21:33:06 +00005131 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005132 if (daemonize) {
5133 uint8_t status = 1;
5134 write(fds[1], &status, 1);
5135 } else
5136 fprintf(stderr, "Could not acquire pid file\n");
5137 exit(1);
5138 }
blueswir1b9e82a52009-04-05 18:03:31 +00005139#endif
ths93815bc2007-03-19 15:58:31 +00005140
blueswir1640f42e2009-04-19 10:18:01 +00005141#ifdef CONFIG_KQEMU
bellardff3fbb32006-01-08 10:53:14 +00005142 if (smp_cpus > 1)
5143 kqemu_allowed = 0;
5144#endif
bellarda20dd502003-09-30 21:07:02 +00005145 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00005146 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00005147
thsf8d39c02008-07-03 10:01:15 +00005148 if (!linux_boot && *kernel_cmdline != '\0') {
5149 fprintf(stderr, "-append only allowed with -kernel option\n");
5150 exit(1);
5151 }
5152
5153 if (!linux_boot && initrd_filename != NULL) {
5154 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5155 exit(1);
5156 }
5157
ths96d30e42007-01-07 20:42:14 +00005158 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00005159 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00005160 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00005161 }
bellardb118d612003-06-30 23:36:21 +00005162 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005163
pbrook634fce92006-07-15 17:40:09 +00005164 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005165 if (init_timer_alarm() < 0) {
5166 fprintf(stderr, "could not initialize alarm timer\n");
5167 exit(1);
5168 }
pbrook2e70f6e2008-06-29 01:03:05 +00005169 if (use_icount && icount_time_shift < 0) {
5170 use_icount = 2;
5171 /* 125MIPS seems a reasonable initial guess at the guest speed.
5172 It will be corrected fairly quickly anyway. */
5173 icount_time_shift = 3;
5174 init_icount_adjust();
5175 }
pbrook634fce92006-07-15 17:40:09 +00005176
bellardfd1dff42006-02-01 21:29:26 +00005177#ifdef _WIN32
5178 socket_init();
5179#endif
5180
bellard7c9d8e02005-11-15 22:16:05 +00005181 /* init network clients */
5182 if (nb_net_clients == 0) {
5183 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005184 net_clients[nb_net_clients++] = "nic";
5185#ifdef CONFIG_SLIRP
5186 net_clients[nb_net_clients++] = "user";
5187#endif
bellardc20709a2004-04-21 23:27:19 +00005188 }
5189
bellard7c9d8e02005-11-15 22:16:05 +00005190 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005191 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005192 exit(1);
bellard702c6512004-04-02 21:21:32 +00005193 }
aliguori63a01ef2008-10-31 19:10:00 +00005194 net_client_check();
bellardf1510b22003-06-25 00:07:40 +00005195
thseec85c22007-01-05 17:41:07 +00005196#ifdef TARGET_I386
balroged494d82007-12-11 23:23:52 +00005197 /* XXX: this should be moved in the PC machine instantiation code */
j_mayer28c5af52007-11-11 01:50:45 +00005198 if (net_boot != 0) {
5199 int netroms = 0;
5200 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00005201 const char *model = nd_table[i].model;
5202 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00005203 if (net_boot & (1 << i)) {
5204 if (model == NULL)
5205 model = "ne2k_pci";
5206 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5207 if (get_image_size(buf) > 0) {
5208 if (nb_option_roms >= MAX_OPTION_ROMS) {
5209 fprintf(stderr, "Too many option ROMs\n");
5210 exit(1);
5211 }
5212 option_rom[nb_option_roms] = strdup(buf);
5213 nb_option_roms++;
5214 netroms++;
5215 }
5216 }
thseec85c22007-01-05 17:41:07 +00005217 }
j_mayer28c5af52007-11-11 01:50:45 +00005218 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00005219 fprintf(stderr, "No valid PXE rom found for network device\n");
5220 exit(1);
5221 }
thseec85c22007-01-05 17:41:07 +00005222 }
5223#endif
5224
balrogdc72ac12008-11-09 00:04:26 +00005225 /* init the bluetooth world */
5226 for (i = 0; i < nb_bt_opts; i++)
5227 if (bt_parse(bt_opts[i]))
5228 exit(1);
5229
bellard0824d6f2003-06-24 13:42:40 +00005230 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005231 if (ram_size == 0)
5232 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005233
blueswir1640f42e2009-04-19 10:18:01 +00005234#ifdef CONFIG_KQEMU
pbrook94a6b542009-04-11 17:15:54 +00005235 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5236 guest ram allocation. It needs to go away. */
5237 if (kqemu_allowed) {
5238 kqemu_phys_ram_size = ram_size + VGA_RAM_SIZE + 4 * 1024 * 1024;
5239 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5240 if (!kqemu_phys_ram_base) {
5241 fprintf(stderr, "Could not allocate physical memory\n");
5242 exit(1);
5243 }
balrog7fb4fdc2008-04-24 17:59:27 +00005244 }
pbrook94a6b542009-04-11 17:15:54 +00005245#endif
bellard0824d6f2003-06-24 13:42:40 +00005246
bellard26a5f132008-05-28 12:30:31 +00005247 /* init the dynamic translator */
5248 cpu_exec_init_all(tb_size * 1024 * 1024);
5249
bellard5905b2e2004-08-01 21:53:26 +00005250 bdrv_init();
aliguori6512a2a2009-03-20 18:26:07 +00005251 dma_helper_init();
thse4bcb142007-12-02 04:51:10 +00005252
5253 /* we always create the cdrom drive, even if no disk is there */
5254
5255 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005256 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005257
balrog9d413d12007-12-04 00:10:34 +00005258 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005259
5260 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005261 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005262
balrog9d413d12007-12-04 00:10:34 +00005263 /* we always create one sd slot, even if no card is in it */
5264
5265 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005266 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005267
ths96d30e42007-01-07 20:42:14 +00005268 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005269
thse4bcb142007-12-02 04:51:10 +00005270 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005271 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005272 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005273
bellardc88676f2006-08-06 13:36:11 +00005274 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005275 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005276
aliguori3023f332009-01-16 19:04:14 +00005277#ifndef _WIN32
5278 /* must be after terminal init, SDL library changes signal handlers */
5279 termsig_setup();
5280#endif
5281
5282 /* Maintain compatibility with multiple stdio monitors */
5283 if (!strcmp(monitor_device,"stdio")) {
5284 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5285 const char *devname = serial_devices[i];
5286 if (devname && !strcmp(devname,"mon:stdio")) {
5287 monitor_device = NULL;
5288 break;
5289 } else if (devname && !strcmp(devname,"stdio")) {
5290 monitor_device = NULL;
5291 serial_devices[i] = "mon:stdio";
5292 break;
5293 }
5294 }
5295 }
5296
aliguori268a3622009-04-21 22:30:27 +00005297 if (nb_numa_nodes > 0) {
5298 int i;
5299
5300 if (nb_numa_nodes > smp_cpus) {
5301 nb_numa_nodes = smp_cpus;
5302 }
5303
5304 /* If no memory size if given for any node, assume the default case
5305 * and distribute the available memory equally across all nodes
5306 */
5307 for (i = 0; i < nb_numa_nodes; i++) {
5308 if (node_mem[i] != 0)
5309 break;
5310 }
5311 if (i == nb_numa_nodes) {
5312 uint64_t usedmem = 0;
5313
5314 /* On Linux, the each node's border has to be 8MB aligned,
5315 * the final node gets the rest.
5316 */
5317 for (i = 0; i < nb_numa_nodes - 1; i++) {
5318 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5319 usedmem += node_mem[i];
5320 }
5321 node_mem[i] = ram_size - usedmem;
5322 }
5323
5324 for (i = 0; i < nb_numa_nodes; i++) {
5325 if (node_cpumask[i] != 0)
5326 break;
5327 }
5328 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5329 * must cope with this anyway, because there are BIOSes out there in
5330 * real machines which also use this scheme.
5331 */
5332 if (i == nb_numa_nodes) {
5333 for (i = 0; i < smp_cpus; i++) {
5334 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5335 }
5336 }
5337 }
5338
aliguori3023f332009-01-16 19:04:14 +00005339 if (kvm_enabled()) {
5340 int ret;
5341
5342 ret = kvm_init(smp_cpus);
5343 if (ret < 0) {
5344 fprintf(stderr, "failed to initialize KVM\n");
5345 exit(1);
5346 }
5347 }
5348
aliguori4c621802009-01-16 21:48:20 +00005349 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005350 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005351 if (!monitor_hd) {
5352 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5353 exit(1);
5354 }
5355 }
5356
aliguori2796dae2009-01-16 20:23:27 +00005357 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5358 const char *devname = serial_devices[i];
5359 if (devname && strcmp(devname, "none")) {
5360 char label[32];
5361 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005362 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005363 if (!serial_hds[i]) {
5364 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5365 devname);
5366 exit(1);
5367 }
5368 }
5369 }
5370
5371 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5372 const char *devname = parallel_devices[i];
5373 if (devname && strcmp(devname, "none")) {
5374 char label[32];
5375 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005376 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005377 if (!parallel_hds[i]) {
5378 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5379 devname);
5380 exit(1);
5381 }
5382 }
5383 }
5384
5385 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5386 const char *devname = virtio_consoles[i];
5387 if (devname && strcmp(devname, "none")) {
5388 char label[32];
5389 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005390 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005391 if (!virtcon_hds[i]) {
5392 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5393 devname);
5394 exit(1);
5395 }
5396 }
5397 }
5398
aliguori3023f332009-01-16 19:04:14 +00005399 machine->init(ram_size, vga_ram_size, boot_devices,
5400 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5401
aliguori268a3622009-04-21 22:30:27 +00005402
5403 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5404 for (i = 0; i < nb_numa_nodes; i++) {
5405 if (node_cpumask[i] & (1 << env->cpu_index)) {
5406 env->numa_node = i;
5407 }
5408 }
5409 }
5410
aliguori6f338c32009-02-11 15:21:54 +00005411 current_machine = machine;
5412
aliguori3023f332009-01-16 19:04:14 +00005413 /* Set KVM's vcpu state to qemu's initial CPUState. */
5414 if (kvm_enabled()) {
5415 int ret;
5416
5417 ret = kvm_sync_vcpus();
5418 if (ret < 0) {
5419 fprintf(stderr, "failed to initialize vcpus\n");
5420 exit(1);
5421 }
5422 }
5423
5424 /* init USB devices */
5425 if (usb_enabled) {
5426 for(i = 0; i < usb_devices_index; i++) {
aliguoric0f4ce72009-03-05 23:01:01 +00005427 if (usb_device_add(usb_devices[i], 0) < 0) {
aliguori3023f332009-01-16 19:04:14 +00005428 fprintf(stderr, "Warning: could not add USB device %s\n",
5429 usb_devices[i]);
5430 }
5431 }
5432 }
5433
aliguori8f391ab2009-01-19 16:34:10 +00005434 if (!display_state)
5435 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005436 /* just use the first displaystate for the moment */
5437 ds = display_state;
bellard313aa562003-08-10 21:52:11 +00005438 /* terminal init */
bellarda20dd502003-09-30 21:07:02 +00005439 if (nographic) {
balrog4d3b6f62008-02-10 16:33:14 +00005440 if (curses) {
5441 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5442 exit(1);
5443 }
aliguori7d957bd2009-01-15 22:14:11 +00005444 } else {
balrog4d3b6f62008-02-10 16:33:14 +00005445#if defined(CONFIG_CURSES)
aliguori7d957bd2009-01-15 22:14:11 +00005446 if (curses) {
5447 /* At the moment curses cannot be used with other displays */
5448 curses_display_init(ds, full_screen);
5449 } else
balrog4d3b6f62008-02-10 16:33:14 +00005450#endif
aliguori7d957bd2009-01-15 22:14:11 +00005451 {
5452 if (vnc_display != NULL) {
5453 vnc_display_init(ds);
5454 if (vnc_display_open(ds, vnc_display) < 0)
5455 exit(1);
5456 }
bellard5b0753e2005-03-01 21:37:28 +00005457#if defined(CONFIG_SDL)
aliguorid268de02009-01-22 16:18:33 +00005458 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005459 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00005460#elif defined(CONFIG_COCOA)
aliguorid268de02009-01-22 16:18:33 +00005461 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005462 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00005463#endif
aliguori7d957bd2009-01-15 22:14:11 +00005464 }
bellard313aa562003-08-10 21:52:11 +00005465 }
aliguori7d957bd2009-01-15 22:14:11 +00005466 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005467
aliguori3023f332009-01-16 19:04:14 +00005468 dcl = ds->listeners;
5469 while (dcl != NULL) {
5470 if (dcl->dpy_refresh != NULL) {
5471 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5472 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005473 }
aliguori3023f332009-01-16 19:04:14 +00005474 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005475 }
aliguori3023f332009-01-16 19:04:14 +00005476
blueswir19043b622009-01-21 19:28:13 +00005477 if (nographic || (vnc_display && !sdl)) {
5478 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5479 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5480 }
5481
aliguori2796dae2009-01-16 20:23:27 +00005482 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00005483 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00005484
aliguori4c621802009-01-16 21:48:20 +00005485 if (monitor_device && monitor_hd)
aliguoricde76ee2009-03-05 23:01:51 +00005486 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
bellard82c643f2004-07-14 17:28:13 +00005487
bellard8d11df92004-08-24 21:13:40 +00005488 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005489 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005490 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005491 char label[32];
5492 snprintf(label, sizeof(label), "serial%d", i);
thsaf3a9032007-07-11 23:14:59 +00005493 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005494 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005495 }
bellard82c643f2004-07-14 17:28:13 +00005496 }
bellard82c643f2004-07-14 17:28:13 +00005497
bellard6508fe52005-01-15 12:02:56 +00005498 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005499 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005500 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005501 char label[32];
5502 snprintf(label, sizeof(label), "parallel%d", i);
thsaf3a9032007-07-11 23:14:59 +00005503 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005504 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005505 }
5506 }
5507
aliguori9ede2fd2009-01-15 20:05:25 +00005508 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5509 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005510 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005511 char label[32];
5512 snprintf(label, sizeof(label), "virtcon%d", i);
aliguori9ede2fd2009-01-15 20:05:25 +00005513 if (strstart(devname, "vc", 0))
5514 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5515 }
5516 }
5517
bellard67b915a2004-03-31 23:37:16 +00005518#ifdef CONFIG_GDBSTUB
aliguori59030a82009-04-05 18:43:41 +00005519 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
5520 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
5521 gdbstub_dev);
5522 exit(1);
balrog45669e02007-07-02 13:20:17 +00005523 }
bellard67b915a2004-03-31 23:37:16 +00005524#endif
balrog45669e02007-07-02 13:20:17 +00005525
bellardd63d3072004-10-03 13:29:03 +00005526 if (loadvm)
aliguori376253e2009-03-05 23:01:23 +00005527 do_loadvm(cur_mon, loadvm);
bellardd63d3072004-10-03 13:29:03 +00005528
aliguori5bb79102008-10-13 03:12:02 +00005529 if (incoming) {
5530 autostart = 0; /* fixme how to deal with -daemonize */
5531 qemu_start_incoming_migration(incoming);
5532 }
5533
aliguoric0f4ce72009-03-05 23:01:01 +00005534 if (autostart)
5535 vm_start();
thsffd843b2006-12-21 19:46:43 +00005536
blueswir1b9e82a52009-04-05 18:03:31 +00005537#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005538 if (daemonize) {
5539 uint8_t status = 0;
5540 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005541
5542 again1:
5543 len = write(fds[1], &status, 1);
5544 if (len == -1 && (errno == EINTR))
5545 goto again1;
5546
5547 if (len != 1)
5548 exit(1);
5549
aliguoribd54b862008-07-23 00:58:33 +00005550 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005551 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005552 if (fd == -1)
5553 exit(1);
aliguori08585322009-02-27 22:09:45 +00005554 }
ths71e3ceb2006-12-22 02:11:31 +00005555
aliguori08585322009-02-27 22:09:45 +00005556 if (run_as) {
5557 pwd = getpwnam(run_as);
5558 if (!pwd) {
5559 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5560 exit(1);
5561 }
5562 }
ths71e3ceb2006-12-22 02:11:31 +00005563
aliguori08585322009-02-27 22:09:45 +00005564 if (chroot_dir) {
5565 if (chroot(chroot_dir) < 0) {
5566 fprintf(stderr, "chroot failed\n");
5567 exit(1);
5568 }
5569 chdir("/");
5570 }
5571
5572 if (run_as) {
5573 if (setgid(pwd->pw_gid) < 0) {
5574 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5575 exit(1);
5576 }
5577 if (setuid(pwd->pw_uid) < 0) {
5578 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5579 exit(1);
5580 }
5581 if (setuid(0) != -1) {
5582 fprintf(stderr, "Dropping privileges failed\n");
5583 exit(1);
5584 }
5585 }
aliguori08585322009-02-27 22:09:45 +00005586
5587 if (daemonize) {
5588 dup2(fd, 0);
5589 dup2(fd, 1);
5590 dup2(fd, 2);
5591
5592 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00005593 }
blueswir1b9e82a52009-04-05 18:03:31 +00005594#endif
ths71e3ceb2006-12-22 02:11:31 +00005595
bellard8a7ddc32004-03-31 19:00:16 +00005596 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005597 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005598 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005599
bellard0824d6f2003-06-24 13:42:40 +00005600 return 0;
5601}